2 * Virtio Console and Generic Serial Port Devices
4 * Copyright Red Hat, Inc. 2009, 2010
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
13 #include "qemu-char.h"
14 #include "qemu-error.h"
16 #include "virtio-serial.h"
18 typedef struct VirtConsole {
19 VirtIOSerialPort port;
24 /* Callback function that's called when the guest sends us data */
25 static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
27 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
30 ret = qemu_chr_write(vcon->chr, buf, len);
31 trace_virtio_console_flush_buf(port->id, len, ret);
35 * Ideally we'd get a better error code than just -1, but
36 * that's what the chardev interface gives us right now. If
37 * we had a finer-grained message, like -EPIPE, we could close
38 * this connection. Absent such error messages, the most we
39 * can do is to return 0 here.
41 * This will prevent stray -1 values to go to
42 * virtio-serial-bus.c and cause abort()s in
43 * do_flush_queued_data().
50 /* Callback function that's called when the guest opens the port */
51 static void guest_open(VirtIOSerialPort *port)
53 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
55 qemu_chr_guest_open(vcon->chr);
58 /* Callback function that's called when the guest closes the port */
59 static void guest_close(VirtIOSerialPort *port)
61 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
63 qemu_chr_guest_close(vcon->chr);
66 /* Readiness of the guest to accept data on a port */
67 static int chr_can_read(void *opaque)
69 VirtConsole *vcon = opaque;
71 return virtio_serial_guest_ready(&vcon->port);
74 /* Send data from a char device over to the guest */
75 static void chr_read(void *opaque, const uint8_t *buf, int size)
77 VirtConsole *vcon = opaque;
79 trace_virtio_console_chr_read(vcon->port.id, size);
80 virtio_serial_write(&vcon->port, buf, size);
83 static void chr_event(void *opaque, int event)
85 VirtConsole *vcon = opaque;
87 trace_virtio_console_chr_event(vcon->port.id, event);
89 case CHR_EVENT_OPENED:
90 virtio_serial_open(&vcon->port);
92 case CHR_EVENT_CLOSED:
93 virtio_serial_close(&vcon->port);
98 static int virtconsole_initfn(VirtIOSerialPort *port)
100 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
101 VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev,
102 vcon->port.dev.info);
104 if (port->id == 0 && !info->is_console) {
105 error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility.");
110 qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
112 info->have_data = flush_buf;
113 info->guest_open = guest_open;
114 info->guest_close = guest_close;
120 static int virtconsole_exitfn(VirtIOSerialPort *port)
122 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
126 * Instead of closing the chardev, free it so it can be used
127 * for other purposes.
129 qemu_chr_add_handlers(vcon->chr, NULL, NULL, NULL, NULL);
135 static VirtIOSerialPortInfo virtconsole_info = {
136 .qdev.name = "virtconsole",
137 .qdev.size = sizeof(VirtConsole),
139 .init = virtconsole_initfn,
140 .exit = virtconsole_exitfn,
141 .qdev.props = (Property[]) {
142 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
143 DEFINE_PROP_END_OF_LIST(),
147 static void virtconsole_register(void)
149 virtio_serial_port_qdev_register(&virtconsole_info);
151 device_init(virtconsole_register)
153 static VirtIOSerialPortInfo virtserialport_info = {
154 .qdev.name = "virtserialport",
155 .qdev.size = sizeof(VirtConsole),
156 .init = virtconsole_initfn,
157 .exit = virtconsole_exitfn,
158 .qdev.props = (Property[]) {
159 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
160 DEFINE_PROP_END_OF_LIST(),
164 static void virtserialport_register(void)
166 virtio_serial_port_qdev_register(&virtserialport_info);
168 device_init(virtserialport_register)