xref: /openbmc/qemu/hw/char/virtio-console.c (revision 9c4218e9)
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 "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"
19 
20 #define TYPE_VIRTIO_CONSOLE_SERIAL_PORT "virtserialport"
21 #define VIRTIO_CONSOLE(obj) \
22     OBJECT_CHECK(VirtConsole, (obj), TYPE_VIRTIO_CONSOLE_SERIAL_PORT)
23 
24 typedef struct VirtConsole {
25     VirtIOSerialPort parent_obj;
26 
27     CharDriverState *chr;
28     guint watch;
29 } VirtConsole;
30 
31 /*
32  * Callback function that's called from chardevs when backend becomes
33  * writable.
34  */
35 static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond,
36                                     void *opaque)
37 {
38     VirtConsole *vcon = opaque;
39 
40     vcon->watch = 0;
41     virtio_serial_throttle_port(VIRTIO_SERIAL_PORT(vcon), false);
42     return FALSE;
43 }
44 
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)
48 {
49     VirtConsole *vcon = VIRTIO_CONSOLE(port);
50     ssize_t ret;
51 
52     if (!vcon->chr) {
53         /* If there's no backend, we can just say we consumed all data. */
54         return len;
55     }
56 
57     ret = qemu_chr_fe_write(vcon->chr, buf, len);
58     trace_virtio_console_flush_buf(port->id, len, ret);
59 
60     if (ret < len) {
61         VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
62 
63         /*
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.
68          */
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);
77             }
78         }
79     }
80     return ret;
81 }
82 
83 /* Callback function that's called when the guest opens/closes the port */
84 static void set_guest_connected(VirtIOSerialPort *port, int guest_connected)
85 {
86     VirtConsole *vcon = VIRTIO_CONSOLE(port);
87     DeviceState *dev = DEVICE(port);
88 
89     if (vcon->chr) {
90         qemu_chr_fe_set_open(vcon->chr, guest_connected);
91     }
92 
93     if (dev->id) {
94         qapi_event_send_vserport_change(dev->id, guest_connected,
95                                         &error_abort);
96     }
97 }
98 
99 static void guest_writable(VirtIOSerialPort *port)
100 {
101     VirtConsole *vcon = VIRTIO_CONSOLE(port);
102 
103     if (vcon->chr) {
104         qemu_chr_accept_input(vcon->chr);
105     }
106 }
107 
108 /* Readiness of the guest to accept data on a port */
109 static int chr_can_read(void *opaque)
110 {
111     VirtConsole *vcon = opaque;
112 
113     return virtio_serial_guest_ready(VIRTIO_SERIAL_PORT(vcon));
114 }
115 
116 /* Send data from a char device over to the guest */
117 static void chr_read(void *opaque, const uint8_t *buf, int size)
118 {
119     VirtConsole *vcon = opaque;
120     VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
121 
122     trace_virtio_console_chr_read(port->id, size);
123     virtio_serial_write(port, buf, size);
124 }
125 
126 static void chr_event(void *opaque, int event)
127 {
128     VirtConsole *vcon = opaque;
129     VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
130 
131     trace_virtio_console_chr_event(port->id, event);
132     switch (event) {
133     case CHR_EVENT_OPENED:
134         virtio_serial_open(port);
135         break;
136     case CHR_EVENT_CLOSED:
137         if (vcon->watch) {
138             g_source_remove(vcon->watch);
139             vcon->watch = 0;
140         }
141         virtio_serial_close(port);
142         break;
143     }
144 }
145 
146 static void virtconsole_realize(DeviceState *dev, Error **errp)
147 {
148     VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
149     VirtConsole *vcon = VIRTIO_CONSOLE(dev);
150     VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(dev);
151 
152     if (port->id == 0 && !k->is_console) {
153         error_setg(errp, "Port number 0 on virtio-serial devices reserved "
154                    "for virtconsole devices for backward compatibility.");
155         return;
156     }
157 
158     if (vcon->chr) {
159         vcon->chr->explicit_fe_open = 1;
160         qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
161                               vcon);
162     }
163 }
164 
165 static void virtconsole_unrealize(DeviceState *dev, Error **errp)
166 {
167     VirtConsole *vcon = VIRTIO_CONSOLE(dev);
168 
169     if (vcon->watch) {
170         g_source_remove(vcon->watch);
171     }
172 }
173 
174 static void virtconsole_class_init(ObjectClass *klass, void *data)
175 {
176     VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
177 
178     k->is_console = true;
179 }
180 
181 static const TypeInfo virtconsole_info = {
182     .name          = "virtconsole",
183     .parent        = TYPE_VIRTIO_CONSOLE_SERIAL_PORT,
184     .class_init    = virtconsole_class_init,
185 };
186 
187 static Property virtserialport_properties[] = {
188     DEFINE_PROP_CHR("chardev", VirtConsole, chr),
189     DEFINE_PROP_END_OF_LIST(),
190 };
191 
192 static void virtserialport_class_init(ObjectClass *klass, void *data)
193 {
194     DeviceClass *dc = DEVICE_CLASS(klass);
195     VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
196 
197     k->realize = virtconsole_realize;
198     k->unrealize = virtconsole_unrealize;
199     k->have_data = flush_buf;
200     k->set_guest_connected = set_guest_connected;
201     k->guest_writable = guest_writable;
202     dc->props = virtserialport_properties;
203 }
204 
205 static const TypeInfo virtserialport_info = {
206     .name          = TYPE_VIRTIO_CONSOLE_SERIAL_PORT,
207     .parent        = TYPE_VIRTIO_SERIAL_PORT,
208     .instance_size = sizeof(VirtConsole),
209     .class_init    = virtserialport_class_init,
210 };
211 
212 static void virtconsole_register_types(void)
213 {
214     type_register_static(&virtserialport_info);
215     type_register_static(&virtconsole_info);
216 }
217 
218 type_init(virtconsole_register_types)
219