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