hw/timer/hpet.c: Avoid signed integer overflow which results in bugs on OSX
[qemu/ar7.git] / hw / char / virtio-console.c
blob2a867cb4e6f33f9b6b5dd1b6ae44c40715fc7f55
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 "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"
19 #define TYPE_VIRTIO_CONSOLE_SERIAL_PORT "virtserialport"
20 #define VIRTIO_CONSOLE(obj) \
21 OBJECT_CHECK(VirtConsole, (obj), TYPE_VIRTIO_CONSOLE_SERIAL_PORT)
23 typedef struct VirtConsole {
24 VirtIOSerialPort parent_obj;
26 CharDriverState *chr;
27 guint watch;
28 } VirtConsole;
31 * Callback function that's called from chardevs when backend becomes
32 * writable.
34 static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond,
35 void *opaque)
37 VirtConsole *vcon = opaque;
39 vcon->watch = 0;
40 virtio_serial_throttle_port(VIRTIO_SERIAL_PORT(vcon), false);
41 return FALSE;
44 /* Callback function that's called when the guest sends us data */
45 static ssize_t flush_buf(VirtIOSerialPort *port,
46 const uint8_t *buf, ssize_t len)
48 VirtConsole *vcon = VIRTIO_CONSOLE(port);
49 ssize_t ret;
51 if (!vcon->chr) {
52 /* If there's no backend, we can just say we consumed all data. */
53 return len;
56 ret = qemu_chr_fe_write(vcon->chr, buf, len);
57 trace_virtio_console_flush_buf(port->id, len, ret);
59 if (ret < len) {
60 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
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
66 * this connection.
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);
79 return ret;
82 /* Callback function that's called when the guest opens/closes the port */
83 static void set_guest_connected(VirtIOSerialPort *port, int guest_connected)
85 VirtConsole *vcon = VIRTIO_CONSOLE(port);
86 DeviceState *dev = DEVICE(port);
88 if (vcon->chr) {
89 qemu_chr_fe_set_open(vcon->chr, guest_connected);
92 if (dev->id) {
93 qapi_event_send_vserport_change(dev->id, guest_connected,
94 &error_abort);
98 static void guest_writable(VirtIOSerialPort *port)
100 VirtConsole *vcon = VIRTIO_CONSOLE(port);
102 if (vcon->chr) {
103 qemu_chr_accept_input(vcon->chr);
107 /* Readiness of the guest to accept data on a port */
108 static int chr_can_read(void *opaque)
110 VirtConsole *vcon = opaque;
112 return virtio_serial_guest_ready(VIRTIO_SERIAL_PORT(vcon));
115 /* Send data from a char device over to the guest */
116 static void chr_read(void *opaque, const uint8_t *buf, int size)
118 VirtConsole *vcon = opaque;
119 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
121 trace_virtio_console_chr_read(port->id, size);
122 virtio_serial_write(port, buf, size);
125 static void chr_event(void *opaque, int event)
127 VirtConsole *vcon = opaque;
128 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
130 trace_virtio_console_chr_event(port->id, event);
131 switch (event) {
132 case CHR_EVENT_OPENED:
133 virtio_serial_open(port);
134 break;
135 case CHR_EVENT_CLOSED:
136 if (vcon->watch) {
137 g_source_remove(vcon->watch);
138 vcon->watch = 0;
140 virtio_serial_close(port);
141 break;
145 static void virtconsole_realize(DeviceState *dev, Error **errp)
147 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
148 VirtConsole *vcon = VIRTIO_CONSOLE(dev);
149 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(dev);
151 if (port->id == 0 && !k->is_console) {
152 error_setg(errp, "Port number 0 on virtio-serial devices reserved "
153 "for virtconsole devices for backward compatibility.");
154 return;
157 if (vcon->chr) {
158 vcon->chr->explicit_fe_open = 1;
159 qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
160 vcon);
164 static void virtconsole_unrealize(DeviceState *dev, Error **errp)
166 VirtConsole *vcon = VIRTIO_CONSOLE(dev);
168 if (vcon->watch) {
169 g_source_remove(vcon->watch);
173 static void virtconsole_class_init(ObjectClass *klass, void *data)
175 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
177 k->is_console = true;
180 static const TypeInfo virtconsole_info = {
181 .name = "virtconsole",
182 .parent = TYPE_VIRTIO_CONSOLE_SERIAL_PORT,
183 .class_init = virtconsole_class_init,
186 static Property virtserialport_properties[] = {
187 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
188 DEFINE_PROP_END_OF_LIST(),
191 static void virtserialport_class_init(ObjectClass *klass, void *data)
193 DeviceClass *dc = DEVICE_CLASS(klass);
194 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
196 k->realize = virtconsole_realize;
197 k->unrealize = virtconsole_unrealize;
198 k->have_data = flush_buf;
199 k->set_guest_connected = set_guest_connected;
200 k->guest_writable = guest_writable;
201 dc->props = virtserialport_properties;
204 static const TypeInfo virtserialport_info = {
205 .name = TYPE_VIRTIO_CONSOLE_SERIAL_PORT,
206 .parent = TYPE_VIRTIO_SERIAL_PORT,
207 .instance_size = sizeof(VirtConsole),
208 .class_init = virtserialport_class_init,
211 static void virtconsole_register_types(void)
213 type_register_static(&virtserialport_info);
214 type_register_static(&virtconsole_info);
217 type_init(virtconsole_register_types)