2 * Virtio Serial / Console Support
4 * Copyright IBM, Corp. 2008
5 * Copyright Red Hat, Inc. 2009, 2010
8 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
9 * Amit Shah <amit.shah@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
15 #ifndef _QEMU_VIRTIO_SERIAL_H
16 #define _QEMU_VIRTIO_SERIAL_H
21 /* == Interface shared between the guest kernel and qemu == */
23 /* The Virtio ID for virtio console / serial ports */
24 #define VIRTIO_ID_CONSOLE 3
26 /* Features supported */
27 #define VIRTIO_CONSOLE_F_MULTIPORT 1
29 #define VIRTIO_CONSOLE_BAD_ID (~(uint32_t)0)
31 struct virtio_console_config
{
33 * These two fields are used by VIRTIO_CONSOLE_F_SIZE which
34 * isn't implemented here yet
39 uint32_t max_nr_ports
;
40 } __attribute__((packed
));
42 struct virtio_console_control
{
43 uint32_t id
; /* Port number */
44 uint16_t event
; /* The kind of control event (see below) */
45 uint16_t value
; /* Extra information for the key */
48 struct virtio_serial_conf
{
49 /* Max. number of ports we can have for a virtio-serial device */
50 uint32_t max_virtserial_ports
;
53 /* Some events for the internal messages (control packets) */
54 #define VIRTIO_CONSOLE_DEVICE_READY 0
55 #define VIRTIO_CONSOLE_PORT_ADD 1
56 #define VIRTIO_CONSOLE_PORT_REMOVE 2
57 #define VIRTIO_CONSOLE_PORT_READY 3
58 #define VIRTIO_CONSOLE_CONSOLE_PORT 4
59 #define VIRTIO_CONSOLE_RESIZE 5
60 #define VIRTIO_CONSOLE_PORT_OPEN 6
61 #define VIRTIO_CONSOLE_PORT_NAME 7
63 /* == In-qemu interface == */
65 typedef struct VirtIOSerial VirtIOSerial
;
66 typedef struct VirtIOSerialBus VirtIOSerialBus
;
67 typedef struct VirtIOSerialPort VirtIOSerialPort
;
68 typedef struct VirtIOSerialPortInfo VirtIOSerialPortInfo
;
71 * This is the state that's shared between all the ports. Some of the
72 * state is configurable via command-line options. Some of it can be
73 * set by individual devices in their initfn routines. Some of the
74 * state is set by the generic qdev device init routine.
76 struct VirtIOSerialPort
{
79 QTAILQ_ENTRY(VirtIOSerialPort
) next
;
82 * This field gives us the virtio device as well as the qdev bus
83 * that we are associated with
90 * This name is sent to the guest and exported via sysfs.
91 * The guest could create symlinks based on this information.
92 * The name is in the reverse fqdn format, like org.qemu.console.0
97 * This id helps identify ports between the guest and the host.
98 * The guest sends a "header" with this id with each data packet
99 * that it sends and the host can then find out which associated
100 * device to send out this data to
105 * This is the elem that we pop from the virtqueue. A slow
106 * backend that consumes guest data (e.g. the file backend for
107 * qemu chardevs) can cause the guest to block till all the output
108 * is flushed. This isn't desired, so we keep a note of the last
109 * element popped and continue consuming it once the backend
110 * becomes writable again.
112 VirtQueueElement elem
;
115 * The index and the offset into the iov buffer that was popped in
122 * When unthrottling we use a bottom-half to call flush_queued_data.
126 /* Is the corresponding guest device open? */
127 bool guest_connected
;
128 /* Is this device open for IO on the host? */
130 /* Do apps not want to receive data? */
134 struct VirtIOSerialPortInfo
{
137 /* Is this a device that binds with hvc in the guest? */
141 * The per-port (or per-app) init function that's called when a
142 * new device is found on the bus.
144 int (*init
)(VirtIOSerialPort
*port
);
146 * Per-port exit function that's called when a port gets
147 * hot-unplugged or removed.
149 int (*exit
)(VirtIOSerialPort
*port
);
151 /* Callbacks for guest events */
152 /* Guest opened device. */
153 void (*guest_open
)(VirtIOSerialPort
*port
);
154 /* Guest closed device. */
155 void (*guest_close
)(VirtIOSerialPort
*port
);
157 /* Guest is now ready to accept data (virtqueues set up). */
158 void (*guest_ready
)(VirtIOSerialPort
*port
);
161 * Guest wrote some data to the port. This data is handed over to
162 * the app via this callback. The app can return a size less than
163 * 'len'. In this case, throttling will be enabled for this port.
165 ssize_t (*have_data
)(VirtIOSerialPort
*port
, const uint8_t *buf
,
169 /* Interface to the virtio-serial bus */
172 * Individual ports/apps should call this function to register the port
173 * with the virtio-serial bus
175 void virtio_serial_port_qdev_register(VirtIOSerialPortInfo
*info
);
178 * Open a connection to the port
179 * Returns 0 on success (always).
181 int virtio_serial_open(VirtIOSerialPort
*port
);
184 * Close the connection to the port
185 * Returns 0 on success (always).
187 int virtio_serial_close(VirtIOSerialPort
*port
);
192 ssize_t
virtio_serial_write(VirtIOSerialPort
*port
, const uint8_t *buf
,
196 * Query whether a guest is ready to receive data.
198 size_t virtio_serial_guest_ready(VirtIOSerialPort
*port
);
201 * Flow control: Ports can signal to the virtio-serial core to stop
202 * sending data or re-start sending data, depending on the 'throttle'
205 void virtio_serial_throttle_port(VirtIOSerialPort
*port
, bool throttle
);