]>
Commit | Line | Data |
---|---|---|
98b19252 AS |
1 | /* |
2 | * Virtio Console and Generic Serial Port Devices | |
3 | * | |
71c092e9 | 4 | * Copyright Red Hat, Inc. 2009, 2010 |
98b19252 AS |
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 | ||
9b8bfe21 | 13 | #include "qemu/osdep.h" |
dccfcd0e | 14 | #include "sysemu/char.h" |
1de7afc9 | 15 | #include "qemu/error-report.h" |
d02e4fa4 | 16 | #include "trace.h" |
0d09e41a | 17 | #include "hw/virtio/virtio-serial.h" |
e2ae6159 | 18 | #include "qapi-event.h" |
98b19252 | 19 | |
be21c336 | 20 | #define TYPE_VIRTIO_CONSOLE_SERIAL_PORT "virtserialport" |
0399a381 | 21 | #define VIRTIO_CONSOLE(obj) \ |
be21c336 | 22 | OBJECT_CHECK(VirtConsole, (obj), TYPE_VIRTIO_CONSOLE_SERIAL_PORT) |
0399a381 | 23 | |
98b19252 | 24 | typedef struct VirtConsole { |
0399a381 AF |
25 | VirtIOSerialPort parent_obj; |
26 | ||
98b19252 | 27 | CharDriverState *chr; |
c3d6b96e | 28 | guint watch; |
98b19252 AS |
29 | } VirtConsole; |
30 | ||
7df4d457 AS |
31 | /* |
32 | * Callback function that's called from chardevs when backend becomes | |
33 | * writable. | |
34 | */ | |
35 | static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond, | |
36 | void *opaque) | |
37 | { | |
38 | VirtConsole *vcon = opaque; | |
39 | ||
c3d6b96e | 40 | vcon->watch = 0; |
0399a381 | 41 | virtio_serial_throttle_port(VIRTIO_SERIAL_PORT(vcon), false); |
7df4d457 AS |
42 | return FALSE; |
43 | } | |
98b19252 AS |
44 | |
45 | /* Callback function that's called when the guest sends us data */ | |
f9fb0532 HG |
46 | static ssize_t flush_buf(VirtIOSerialPort *port, |
47 | const uint8_t *buf, ssize_t len) | |
98b19252 | 48 | { |
0399a381 | 49 | VirtConsole *vcon = VIRTIO_CONSOLE(port); |
d02e4fa4 | 50 | ssize_t ret; |
98b19252 | 51 | |
6640422c AS |
52 | if (!vcon->chr) { |
53 | /* If there's no backend, we can just say we consumed all data. */ | |
54 | return len; | |
55 | } | |
56 | ||
2cc6e0a1 | 57 | ret = qemu_chr_fe_write(vcon->chr, buf, len); |
d02e4fa4 | 58 | trace_virtio_console_flush_buf(port->id, len, ret); |
0219d732 | 59 | |
f9fb0532 | 60 | if (ret < len) { |
d6258c93 AS |
61 | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port); |
62 | ||
0219d732 AS |
63 | /* |
64 | * Ideally we'd get a better error code than just -1, but | |
65 | * that's what the chardev interface gives us right now. If | |
66 | * we had a finer-grained message, like -EPIPE, we could close | |
d6258c93 | 67 | * this connection. |
0219d732 | 68 | */ |
f9fb0532 HG |
69 | if (ret < 0) |
70 | ret = 0; | |
d6258c93 AS |
71 | if (!k->is_console) { |
72 | virtio_serial_throttle_port(port, true); | |
c3d6b96e | 73 | if (!vcon->watch) { |
e02bc6de RPM |
74 | vcon->watch = qemu_chr_fe_add_watch(vcon->chr, |
75 | G_IO_OUT|G_IO_HUP, | |
c3d6b96e HG |
76 | chr_write_unblocked, vcon); |
77 | } | |
d6258c93 | 78 | } |
0219d732 | 79 | } |
d02e4fa4 | 80 | return ret; |
98b19252 AS |
81 | } |
82 | ||
b2c1394a HG |
83 | /* Callback function that's called when the guest opens/closes the port */ |
84 | static void set_guest_connected(VirtIOSerialPort *port, int guest_connected) | |
0b6d2266 | 85 | { |
0399a381 | 86 | VirtConsole *vcon = VIRTIO_CONSOLE(port); |
e2ae6159 | 87 | DeviceState *dev = DEVICE(port); |
bce6261e | 88 | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port); |
0b6d2266 | 89 | |
bce6261e | 90 | if (vcon->chr && !k->is_console) { |
e2ae6159 LE |
91 | qemu_chr_fe_set_open(vcon->chr, guest_connected); |
92 | } | |
93 | ||
94 | if (dev->id) { | |
95 | qapi_event_send_vserport_change(dev->id, guest_connected, | |
96 | &error_abort); | |
6640422c | 97 | } |
0b6d2266 HG |
98 | } |
99 | ||
246ca55f MAL |
100 | static void guest_writable(VirtIOSerialPort *port) |
101 | { | |
102 | VirtConsole *vcon = VIRTIO_CONSOLE(port); | |
103 | ||
104 | if (vcon->chr) { | |
105 | qemu_chr_accept_input(vcon->chr); | |
106 | } | |
107 | } | |
108 | ||
98b19252 AS |
109 | /* Readiness of the guest to accept data on a port */ |
110 | static int chr_can_read(void *opaque) | |
111 | { | |
112 | VirtConsole *vcon = opaque; | |
113 | ||
0399a381 | 114 | return virtio_serial_guest_ready(VIRTIO_SERIAL_PORT(vcon)); |
98b19252 AS |
115 | } |
116 | ||
117 | /* Send data from a char device over to the guest */ | |
118 | static void chr_read(void *opaque, const uint8_t *buf, int size) | |
119 | { | |
120 | VirtConsole *vcon = opaque; | |
0399a381 | 121 | VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon); |
98b19252 | 122 | |
0399a381 AF |
123 | trace_virtio_console_chr_read(port->id, size); |
124 | virtio_serial_write(port, buf, size); | |
98b19252 AS |
125 | } |
126 | ||
127 | static void chr_event(void *opaque, int event) | |
128 | { | |
129 | VirtConsole *vcon = opaque; | |
0399a381 | 130 | VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon); |
98b19252 | 131 | |
0399a381 | 132 | trace_virtio_console_chr_event(port->id, event); |
98b19252 | 133 | switch (event) { |
28eaf465 | 134 | case CHR_EVENT_OPENED: |
0399a381 | 135 | virtio_serial_open(port); |
98b19252 | 136 | break; |
98b19252 | 137 | case CHR_EVENT_CLOSED: |
c3d6b96e HG |
138 | if (vcon->watch) { |
139 | g_source_remove(vcon->watch); | |
140 | vcon->watch = 0; | |
141 | } | |
0399a381 | 142 | virtio_serial_close(port); |
98b19252 AS |
143 | break; |
144 | } | |
145 | } | |
146 | ||
2ef66625 | 147 | static void virtconsole_realize(DeviceState *dev, Error **errp) |
98b19252 | 148 | { |
2ef66625 AF |
149 | VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev); |
150 | VirtConsole *vcon = VIRTIO_CONSOLE(dev); | |
151 | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(dev); | |
a15bb0d6 | 152 | |
f82e35e3 | 153 | if (port->id == 0 && !k->is_console) { |
2ef66625 AF |
154 | error_setg(errp, "Port number 0 on virtio-serial devices reserved " |
155 | "for virtconsole devices for backward compatibility."); | |
156 | return; | |
7edfe652 MA |
157 | } |
158 | ||
98b19252 | 159 | if (vcon->chr) { |
bce6261e DB |
160 | /* |
161 | * For consoles we don't block guest data transfer just | |
162 | * because nothing is connected - we'll just let it go | |
163 | * whetherever the chardev wants - /dev/null probably. | |
164 | * | |
165 | * For serial ports we need 100% reliable data transfer | |
166 | * so we use the opened/closed signals from chardev to | |
167 | * trigger open/close of the device | |
168 | */ | |
169 | if (k->is_console) { | |
170 | vcon->chr->explicit_fe_open = 0; | |
171 | qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, | |
172 | NULL, vcon); | |
173 | virtio_serial_open(port); | |
174 | } else { | |
175 | vcon->chr->explicit_fe_open = 1; | |
176 | qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, | |
177 | chr_event, vcon); | |
178 | } | |
98b19252 | 179 | } |
cbe77b61 AS |
180 | } |
181 | ||
2ef66625 | 182 | static void virtconsole_unrealize(DeviceState *dev, Error **errp) |
c3d6b96e | 183 | { |
2ef66625 | 184 | VirtConsole *vcon = VIRTIO_CONSOLE(dev); |
c3d6b96e HG |
185 | |
186 | if (vcon->watch) { | |
187 | g_source_remove(vcon->watch); | |
188 | } | |
c3d6b96e HG |
189 | } |
190 | ||
f82e35e3 AL |
191 | static void virtconsole_class_init(ObjectClass *klass, void *data) |
192 | { | |
193 | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass); | |
194 | ||
195 | k->is_console = true; | |
f82e35e3 AL |
196 | } |
197 | ||
8c43a6f0 | 198 | static const TypeInfo virtconsole_info = { |
be21c336 AF |
199 | .name = "virtconsole", |
200 | .parent = TYPE_VIRTIO_CONSOLE_SERIAL_PORT, | |
39bffca2 | 201 | .class_init = virtconsole_class_init, |
98b19252 AS |
202 | }; |
203 | ||
f82e35e3 AL |
204 | static Property virtserialport_properties[] = { |
205 | DEFINE_PROP_CHR("chardev", VirtConsole, chr), | |
206 | DEFINE_PROP_END_OF_LIST(), | |
207 | }; | |
208 | ||
209 | static void virtserialport_class_init(ObjectClass *klass, void *data) | |
210 | { | |
39bffca2 | 211 | DeviceClass *dc = DEVICE_CLASS(klass); |
f82e35e3 AL |
212 | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass); |
213 | ||
2ef66625 AF |
214 | k->realize = virtconsole_realize; |
215 | k->unrealize = virtconsole_unrealize; | |
f82e35e3 | 216 | k->have_data = flush_buf; |
b2c1394a | 217 | k->set_guest_connected = set_guest_connected; |
246ca55f | 218 | k->guest_writable = guest_writable; |
39bffca2 | 219 | dc->props = virtserialport_properties; |
f82e35e3 AL |
220 | } |
221 | ||
8c43a6f0 | 222 | static const TypeInfo virtserialport_info = { |
be21c336 | 223 | .name = TYPE_VIRTIO_CONSOLE_SERIAL_PORT, |
39bffca2 AL |
224 | .parent = TYPE_VIRTIO_SERIAL_PORT, |
225 | .instance_size = sizeof(VirtConsole), | |
226 | .class_init = virtserialport_class_init, | |
b60c470b AS |
227 | }; |
228 | ||
83f7d43a | 229 | static void virtconsole_register_types(void) |
b60c470b | 230 | { |
39bffca2 | 231 | type_register_static(&virtserialport_info); |
be21c336 | 232 | type_register_static(&virtconsole_info); |
b60c470b | 233 | } |
83f7d43a AF |
234 | |
235 | type_init(virtconsole_register_types) |