ppc: Don't update NIP in lswi/lswx/stswi/stswx
[qemu/kevin.git] / hw / char / virtio-console.c
blob4f0e03d3b70f5fc492127d1497bfdc647913ac45
1 /*
2 * Virtio Console and Generic Serial Port Devices
4 * Copyright Red Hat, Inc. 2009, 2010
6 * Authors:
7 * Amit Shah <amit.shah@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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"
20 #define TYPE_VIRTIO_CONSOLE_SERIAL_PORT "virtserialport"
21 #define VIRTIO_CONSOLE(obj) \
22 OBJECT_CHECK(VirtConsole, (obj), TYPE_VIRTIO_CONSOLE_SERIAL_PORT)
24 typedef struct VirtConsole {
25 VirtIOSerialPort parent_obj;
27 CharDriverState *chr;
28 guint watch;
29 } VirtConsole;
32 * Callback function that's called from chardevs when backend becomes
33 * writable.
35 static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond,
36 void *opaque)
38 VirtConsole *vcon = opaque;
40 vcon->watch = 0;
41 virtio_serial_throttle_port(VIRTIO_SERIAL_PORT(vcon), false);
42 return FALSE;
45 /* Callback function that's called when the guest sends us data */
46 static ssize_t flush_buf(VirtIOSerialPort *port,
47 const uint8_t *buf, ssize_t len)
49 VirtConsole *vcon = VIRTIO_CONSOLE(port);
50 ssize_t ret;
52 if (!vcon->chr) {
53 /* If there's no backend, we can just say we consumed all data. */
54 return len;
57 ret = qemu_chr_fe_write(vcon->chr, buf, len);
58 trace_virtio_console_flush_buf(port->id, len, ret);
60 if (ret < len) {
61 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
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
67 * this connection.
69 if (ret < 0)
70 ret = 0;
71 if (!k->is_console) {
72 virtio_serial_throttle_port(port, true);
73 if (!vcon->watch) {
74 vcon->watch = qemu_chr_fe_add_watch(vcon->chr,
75 G_IO_OUT|G_IO_HUP,
76 chr_write_unblocked, vcon);
80 return ret;
83 /* Callback function that's called when the guest opens/closes the port */
84 static void set_guest_connected(VirtIOSerialPort *port, int guest_connected)
86 VirtConsole *vcon = VIRTIO_CONSOLE(port);
87 DeviceState *dev = DEVICE(port);
88 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
90 if (vcon->chr && !k->is_console) {
91 qemu_chr_fe_set_open(vcon->chr, guest_connected);
94 if (dev->id) {
95 qapi_event_send_vserport_change(dev->id, guest_connected,
96 &error_abort);
100 static void guest_writable(VirtIOSerialPort *port)
102 VirtConsole *vcon = VIRTIO_CONSOLE(port);
104 if (vcon->chr) {
105 qemu_chr_accept_input(vcon->chr);
109 /* Readiness of the guest to accept data on a port */
110 static int chr_can_read(void *opaque)
112 VirtConsole *vcon = opaque;
114 return virtio_serial_guest_ready(VIRTIO_SERIAL_PORT(vcon));
117 /* Send data from a char device over to the guest */
118 static void chr_read(void *opaque, const uint8_t *buf, int size)
120 VirtConsole *vcon = opaque;
121 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
123 trace_virtio_console_chr_read(port->id, size);
124 virtio_serial_write(port, buf, size);
127 static void chr_event(void *opaque, int event)
129 VirtConsole *vcon = opaque;
130 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
132 trace_virtio_console_chr_event(port->id, event);
133 switch (event) {
134 case CHR_EVENT_OPENED:
135 virtio_serial_open(port);
136 break;
137 case CHR_EVENT_CLOSED:
138 if (vcon->watch) {
139 g_source_remove(vcon->watch);
140 vcon->watch = 0;
142 virtio_serial_close(port);
143 break;
147 static void virtconsole_realize(DeviceState *dev, Error **errp)
149 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
150 VirtConsole *vcon = VIRTIO_CONSOLE(dev);
151 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(dev);
153 if (port->id == 0 && !k->is_console) {
154 error_setg(errp, "Port number 0 on virtio-serial devices reserved "
155 "for virtconsole devices for backward compatibility.");
156 return;
159 if (vcon->chr) {
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.
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
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);
182 static void virtconsole_unrealize(DeviceState *dev, Error **errp)
184 VirtConsole *vcon = VIRTIO_CONSOLE(dev);
186 if (vcon->watch) {
187 g_source_remove(vcon->watch);
191 static void virtconsole_class_init(ObjectClass *klass, void *data)
193 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
195 k->is_console = true;
198 static const TypeInfo virtconsole_info = {
199 .name = "virtconsole",
200 .parent = TYPE_VIRTIO_CONSOLE_SERIAL_PORT,
201 .class_init = virtconsole_class_init,
204 static Property virtserialport_properties[] = {
205 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
206 DEFINE_PROP_END_OF_LIST(),
209 static void virtserialport_class_init(ObjectClass *klass, void *data)
211 DeviceClass *dc = DEVICE_CLASS(klass);
212 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
214 k->realize = virtconsole_realize;
215 k->unrealize = virtconsole_unrealize;
216 k->have_data = flush_buf;
217 k->set_guest_connected = set_guest_connected;
218 k->guest_writable = guest_writable;
219 dc->props = virtserialport_properties;
222 static const TypeInfo virtserialport_info = {
223 .name = TYPE_VIRTIO_CONSOLE_SERIAL_PORT,
224 .parent = TYPE_VIRTIO_SERIAL_PORT,
225 .instance_size = sizeof(VirtConsole),
226 .class_init = virtserialport_class_init,
229 static void virtconsole_register_types(void)
231 type_register_static(&virtserialport_info);
232 type_register_static(&virtconsole_info);
235 type_init(virtconsole_register_types)