vl: avoid closing stdout with 'writeconfig'
[qemu/ar7.git] / hw / char / virtio-console.c
blob6c8be0fe26c5314fc0d96b1d235ba27cb9c3afd6
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"
18 #define TYPE_VIRTIO_CONSOLE_SERIAL_PORT "virtserialport"
19 #define VIRTIO_CONSOLE(obj) \
20 OBJECT_CHECK(VirtConsole, (obj), TYPE_VIRTIO_CONSOLE_SERIAL_PORT)
22 typedef struct VirtConsole {
23 VirtIOSerialPort parent_obj;
25 CharDriverState *chr;
26 guint watch;
27 } VirtConsole;
30 * Callback function that's called from chardevs when backend becomes
31 * writable.
33 static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond,
34 void *opaque)
36 VirtConsole *vcon = opaque;
38 vcon->watch = 0;
39 virtio_serial_throttle_port(VIRTIO_SERIAL_PORT(vcon), false);
40 return FALSE;
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)
47 VirtConsole *vcon = VIRTIO_CONSOLE(port);
48 ssize_t ret;
50 if (!vcon->chr) {
51 /* If there's no backend, we can just say we consumed all data. */
52 return len;
55 ret = qemu_chr_fe_write(vcon->chr, buf, len);
56 trace_virtio_console_flush_buf(port->id, len, ret);
58 if (ret < len) {
59 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
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.
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);
77 return ret;
80 /* Callback function that's called when the guest opens/closes the port */
81 static void set_guest_connected(VirtIOSerialPort *port, int guest_connected)
83 VirtConsole *vcon = VIRTIO_CONSOLE(port);
85 if (!vcon->chr) {
86 return;
88 qemu_chr_fe_set_open(vcon->chr, guest_connected);
91 /* Readiness of the guest to accept data on a port */
92 static int chr_can_read(void *opaque)
94 VirtConsole *vcon = opaque;
96 return virtio_serial_guest_ready(VIRTIO_SERIAL_PORT(vcon));
99 /* Send data from a char device over to the guest */
100 static void chr_read(void *opaque, const uint8_t *buf, int size)
102 VirtConsole *vcon = opaque;
103 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
105 trace_virtio_console_chr_read(port->id, size);
106 virtio_serial_write(port, buf, size);
109 static void chr_event(void *opaque, int event)
111 VirtConsole *vcon = opaque;
112 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
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;
124 virtio_serial_close(port);
125 break;
129 static void virtconsole_realize(DeviceState *dev, Error **errp)
131 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
132 VirtConsole *vcon = VIRTIO_CONSOLE(dev);
133 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(dev);
135 if (port->id == 0 && !k->is_console) {
136 error_setg(errp, "Port number 0 on virtio-serial devices reserved "
137 "for virtconsole devices for backward compatibility.");
138 return;
141 if (vcon->chr) {
142 vcon->chr->explicit_fe_open = 1;
143 qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
144 vcon);
148 static void virtconsole_unrealize(DeviceState *dev, Error **errp)
150 VirtConsole *vcon = VIRTIO_CONSOLE(dev);
152 if (vcon->watch) {
153 g_source_remove(vcon->watch);
157 static void virtconsole_class_init(ObjectClass *klass, void *data)
159 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
161 k->is_console = true;
164 static const TypeInfo virtconsole_info = {
165 .name = "virtconsole",
166 .parent = TYPE_VIRTIO_CONSOLE_SERIAL_PORT,
167 .class_init = virtconsole_class_init,
170 static Property virtserialport_properties[] = {
171 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
172 DEFINE_PROP_END_OF_LIST(),
175 static void virtserialport_class_init(ObjectClass *klass, void *data)
177 DeviceClass *dc = DEVICE_CLASS(klass);
178 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
180 k->realize = virtconsole_realize;
181 k->unrealize = virtconsole_unrealize;
182 k->have_data = flush_buf;
183 k->set_guest_connected = set_guest_connected;
184 dc->props = virtserialport_properties;
187 static const TypeInfo virtserialport_info = {
188 .name = TYPE_VIRTIO_CONSOLE_SERIAL_PORT,
189 .parent = TYPE_VIRTIO_SERIAL_PORT,
190 .instance_size = sizeof(VirtConsole),
191 .class_init = virtserialport_class_init,
194 static void virtconsole_register_types(void)
196 type_register_static(&virtserialport_info);
197 type_register_static(&virtconsole_info);
200 type_init(virtconsole_register_types)