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