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