]> git.proxmox.com Git - qemu.git/blob - hw/virtio-console.c
b3ee074a87648f743b3c4f1823d4e78d9b2711f4
[qemu.git] / hw / virtio-console.c
1 /*
2 * Virtio Console and Generic Serial Port Devices
3 *
4 * Copyright Red Hat, Inc. 2009, 2010
5 *
6 * Authors:
7 * Amit Shah <amit.shah@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 */
12
13 #include "char/char.h"
14 #include "qemu/error-report.h"
15 #include "trace.h"
16 #include "virtio-serial.h"
17
18 typedef struct VirtConsole {
19 VirtIOSerialPort port;
20 CharDriverState *chr;
21 } VirtConsole;
22
23 /*
24 * Callback function that's called from chardevs when backend becomes
25 * writable.
26 */
27 static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond,
28 void *opaque)
29 {
30 VirtConsole *vcon = opaque;
31
32 virtio_serial_throttle_port(&vcon->port, false);
33 return FALSE;
34 }
35
36 /* Callback function that's called when the guest sends us data */
37 static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
38 {
39 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
40 ssize_t ret;
41
42 if (!vcon->chr) {
43 /* If there's no backend, we can just say we consumed all data. */
44 return len;
45 }
46
47 ret = qemu_chr_fe_write(vcon->chr, buf, len);
48 trace_virtio_console_flush_buf(port->id, len, ret);
49
50 if (ret < 0) {
51 /*
52 * Ideally we'd get a better error code than just -1, but
53 * that's what the chardev interface gives us right now. If
54 * we had a finer-grained message, like -EPIPE, we could close
55 * this connection. Absent such error messages, the most we
56 * can do is to return 0 here.
57 *
58 * This will prevent stray -1 values to go to
59 * virtio-serial-bus.c and cause abort()s in
60 * do_flush_queued_data().
61 */
62 ret = 0;
63 qemu_chr_fe_add_watch(vcon->chr, G_IO_OUT, chr_write_unblocked, vcon);
64 }
65 return ret;
66 }
67
68 /* Callback function that's called when the guest opens the port */
69 static void guest_open(VirtIOSerialPort *port)
70 {
71 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
72
73 if (!vcon->chr) {
74 return;
75 }
76 qemu_chr_fe_open(vcon->chr);
77 }
78
79 /* Callback function that's called when the guest closes the port */
80 static void guest_close(VirtIOSerialPort *port)
81 {
82 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
83
84 if (!vcon->chr) {
85 return;
86 }
87 qemu_chr_fe_close(vcon->chr);
88 }
89
90 /* Readiness of the guest to accept data on a port */
91 static int chr_can_read(void *opaque)
92 {
93 VirtConsole *vcon = opaque;
94
95 return virtio_serial_guest_ready(&vcon->port);
96 }
97
98 /* Send data from a char device over to the guest */
99 static void chr_read(void *opaque, const uint8_t *buf, int size)
100 {
101 VirtConsole *vcon = opaque;
102
103 trace_virtio_console_chr_read(vcon->port.id, size);
104 virtio_serial_write(&vcon->port, buf, size);
105 }
106
107 static void chr_event(void *opaque, int event)
108 {
109 VirtConsole *vcon = opaque;
110
111 trace_virtio_console_chr_event(vcon->port.id, event);
112 switch (event) {
113 case CHR_EVENT_OPENED:
114 virtio_serial_open(&vcon->port);
115 break;
116 case CHR_EVENT_CLOSED:
117 virtio_serial_close(&vcon->port);
118 break;
119 }
120 }
121
122 static int virtconsole_initfn(VirtIOSerialPort *port)
123 {
124 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
125 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
126
127 if (port->id == 0 && !k->is_console) {
128 error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility.");
129 return -1;
130 }
131
132 if (vcon->chr) {
133 qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
134 vcon);
135 }
136
137 return 0;
138 }
139
140 static Property virtconsole_properties[] = {
141 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
142 DEFINE_PROP_END_OF_LIST(),
143 };
144
145 static void virtconsole_class_init(ObjectClass *klass, void *data)
146 {
147 DeviceClass *dc = DEVICE_CLASS(klass);
148 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
149
150 k->is_console = true;
151 k->init = virtconsole_initfn;
152 k->have_data = flush_buf;
153 k->guest_open = guest_open;
154 k->guest_close = guest_close;
155 dc->props = virtconsole_properties;
156 }
157
158 static const TypeInfo virtconsole_info = {
159 .name = "virtconsole",
160 .parent = TYPE_VIRTIO_SERIAL_PORT,
161 .instance_size = sizeof(VirtConsole),
162 .class_init = virtconsole_class_init,
163 };
164
165 static Property virtserialport_properties[] = {
166 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
167 DEFINE_PROP_END_OF_LIST(),
168 };
169
170 static void virtserialport_class_init(ObjectClass *klass, void *data)
171 {
172 DeviceClass *dc = DEVICE_CLASS(klass);
173 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
174
175 k->init = virtconsole_initfn;
176 k->have_data = flush_buf;
177 k->guest_open = guest_open;
178 k->guest_close = guest_close;
179 dc->props = virtserialport_properties;
180 }
181
182 static const TypeInfo virtserialport_info = {
183 .name = "virtserialport",
184 .parent = TYPE_VIRTIO_SERIAL_PORT,
185 .instance_size = sizeof(VirtConsole),
186 .class_init = virtserialport_class_init,
187 };
188
189 static void virtconsole_register_types(void)
190 {
191 type_register_static(&virtconsole_info);
192 type_register_static(&virtserialport_info);
193 }
194
195 type_init(virtconsole_register_types)