1
2
3
4
5
6
7
8
9
10
11
12
13#include "qemu/osdep.h"
14#include "chardev/char-fe.h"
15#include "qemu/error-report.h"
16#include "qemu/module.h"
17#include "trace.h"
18#include "hw/virtio/virtio-serial.h"
19#include "qapi/error.h"
20#include "qapi/qapi-events-char.h"
21
22#define TYPE_VIRTIO_CONSOLE_SERIAL_PORT "virtserialport"
23#define VIRTIO_CONSOLE(obj) \
24 OBJECT_CHECK(VirtConsole, (obj), TYPE_VIRTIO_CONSOLE_SERIAL_PORT)
25
26typedef struct VirtConsole {
27 VirtIOSerialPort parent_obj;
28
29 CharBackend chr;
30 guint watch;
31} VirtConsole;
32
33
34
35
36
37static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond,
38 void *opaque)
39{
40 VirtConsole *vcon = opaque;
41
42 vcon->watch = 0;
43 virtio_serial_throttle_port(VIRTIO_SERIAL_PORT(vcon), false);
44 return FALSE;
45}
46
47
48static ssize_t flush_buf(VirtIOSerialPort *port,
49 const uint8_t *buf, ssize_t len)
50{
51 VirtConsole *vcon = VIRTIO_CONSOLE(port);
52 ssize_t ret;
53
54 if (!qemu_chr_fe_backend_connected(&vcon->chr)) {
55
56 return len;
57 }
58
59 ret = qemu_chr_fe_write(&vcon->chr, buf, len);
60 trace_virtio_console_flush_buf(port->id, len, ret);
61
62 if (ret < len) {
63 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
64
65
66
67
68
69
70
71 if (ret < 0)
72 ret = 0;
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 if (!k->is_console) {
95 virtio_serial_throttle_port(port, true);
96 if (!vcon->watch) {
97 vcon->watch = qemu_chr_fe_add_watch(&vcon->chr,
98 G_IO_OUT|G_IO_HUP,
99 chr_write_unblocked, vcon);
100 }
101 }
102 }
103 return ret;
104}
105
106
107static void set_guest_connected(VirtIOSerialPort *port, int guest_connected)
108{
109 VirtConsole *vcon = VIRTIO_CONSOLE(port);
110 DeviceState *dev = DEVICE(port);
111 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
112
113 if (!k->is_console) {
114 qemu_chr_fe_set_open(&vcon->chr, guest_connected);
115 }
116
117 if (dev->id) {
118 qapi_event_send_vserport_change(dev->id, guest_connected);
119 }
120}
121
122static void guest_writable(VirtIOSerialPort *port)
123{
124 VirtConsole *vcon = VIRTIO_CONSOLE(port);
125
126 qemu_chr_fe_accept_input(&vcon->chr);
127}
128
129
130static int chr_can_read(void *opaque)
131{
132 VirtConsole *vcon = opaque;
133
134 return virtio_serial_guest_ready(VIRTIO_SERIAL_PORT(vcon));
135}
136
137
138static void chr_read(void *opaque, const uint8_t *buf, int size)
139{
140 VirtConsole *vcon = opaque;
141 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
142
143 trace_virtio_console_chr_read(port->id, size);
144 virtio_serial_write(port, buf, size);
145}
146
147static void chr_event(void *opaque, int event)
148{
149 VirtConsole *vcon = opaque;
150 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
151
152 trace_virtio_console_chr_event(port->id, event);
153 switch (event) {
154 case CHR_EVENT_OPENED:
155 virtio_serial_open(port);
156 break;
157 case CHR_EVENT_CLOSED:
158 if (vcon->watch) {
159 g_source_remove(vcon->watch);
160 vcon->watch = 0;
161 }
162 virtio_serial_close(port);
163 break;
164 }
165}
166
167static int chr_be_change(void *opaque)
168{
169 VirtConsole *vcon = opaque;
170 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
171 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
172
173 if (k->is_console) {
174 qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
175 NULL, chr_be_change, vcon, NULL, true);
176 } else {
177 qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
178 chr_event, chr_be_change, vcon, NULL, false);
179 }
180
181 if (vcon->watch) {
182 g_source_remove(vcon->watch);
183 vcon->watch = qemu_chr_fe_add_watch(&vcon->chr,
184 G_IO_OUT | G_IO_HUP,
185 chr_write_unblocked, vcon);
186 }
187
188 return 0;
189}
190
191static void virtconsole_enable_backend(VirtIOSerialPort *port, bool enable)
192{
193 VirtConsole *vcon = VIRTIO_CONSOLE(port);
194
195 if (!qemu_chr_fe_backend_connected(&vcon->chr)) {
196 return;
197 }
198
199 if (enable) {
200 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
201
202 qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
203 k->is_console ? NULL : chr_event,
204 chr_be_change, vcon, NULL, false);
205 } else {
206 qemu_chr_fe_set_handlers(&vcon->chr, NULL, NULL, NULL,
207 NULL, NULL, NULL, false);
208 }
209}
210
211static void virtconsole_realize(DeviceState *dev, Error **errp)
212{
213 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
214 VirtConsole *vcon = VIRTIO_CONSOLE(dev);
215 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(dev);
216
217 if (port->id == 0 && !k->is_console) {
218 error_setg(errp, "Port number 0 on virtio-serial devices reserved "
219 "for virtconsole devices for backward compatibility.");
220 return;
221 }
222
223 if (qemu_chr_fe_backend_connected(&vcon->chr)) {
224
225
226
227
228
229
230
231
232
233 if (k->is_console) {
234 qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
235 NULL, chr_be_change,
236 vcon, NULL, true);
237 virtio_serial_open(port);
238 } else {
239 qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
240 chr_event, chr_be_change,
241 vcon, NULL, false);
242 }
243 }
244}
245
246static void virtconsole_unrealize(DeviceState *dev, Error **errp)
247{
248 VirtConsole *vcon = VIRTIO_CONSOLE(dev);
249
250 if (vcon->watch) {
251 g_source_remove(vcon->watch);
252 }
253}
254
255static void virtconsole_class_init(ObjectClass *klass, void *data)
256{
257 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
258
259 k->is_console = true;
260}
261
262static const TypeInfo virtconsole_info = {
263 .name = "virtconsole",
264 .parent = TYPE_VIRTIO_CONSOLE_SERIAL_PORT,
265 .class_init = virtconsole_class_init,
266};
267
268static Property virtserialport_properties[] = {
269 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
270 DEFINE_PROP_END_OF_LIST(),
271};
272
273static void virtserialport_class_init(ObjectClass *klass, void *data)
274{
275 DeviceClass *dc = DEVICE_CLASS(klass);
276 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
277
278 k->realize = virtconsole_realize;
279 k->unrealize = virtconsole_unrealize;
280 k->have_data = flush_buf;
281 k->set_guest_connected = set_guest_connected;
282 k->enable_backend = virtconsole_enable_backend;
283 k->guest_writable = guest_writable;
284 dc->props = virtserialport_properties;
285}
286
287static const TypeInfo virtserialport_info = {
288 .name = TYPE_VIRTIO_CONSOLE_SERIAL_PORT,
289 .parent = TYPE_VIRTIO_SERIAL_PORT,
290 .instance_size = sizeof(VirtConsole),
291 .class_init = virtserialport_class_init,
292};
293
294static void virtconsole_register_types(void)
295{
296 type_register_static(&virtserialport_info);
297 type_register_static(&virtconsole_info);
298}
299
300type_init(virtconsole_register_types)
301