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