blob: cffee3d4706ececdbf37afc8d49508d8908ceb25 [file] [log] [blame]
Amit Shah98b19252010-01-20 00:36:52 +05301/*
2 * Virtio Console and Generic Serial Port Devices
3 *
Amit Shah71c092e2010-04-27 18:04:02 +05304 * Copyright Red Hat, Inc. 2009, 2010
Amit Shah98b19252010-01-20 00:36:52 +05305 *
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-char.h"
Amit Shah0b8b7162011-02-03 13:05:07 +053014#include "qemu-error.h"
Amit Shahd02e4fa2011-07-05 16:37:49 +053015#include "trace.h"
Amit Shah98b19252010-01-20 00:36:52 +053016#include "virtio-serial.h"
17
18typedef struct VirtConsole {
19 VirtIOSerialPort port;
20 CharDriverState *chr;
21} VirtConsole;
22
23
24/* Callback function that's called when the guest sends us data */
Amit Shahe300ac22010-12-13 17:50:07 +053025static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
Amit Shah98b19252010-01-20 00:36:52 +053026{
27 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
Amit Shahd02e4fa2011-07-05 16:37:49 +053028 ssize_t ret;
Amit Shah98b19252010-01-20 00:36:52 +053029
Amit Shah66404222011-12-21 12:28:27 +053030 if (!vcon->chr) {
31 /* If there's no backend, we can just say we consumed all data. */
32 return len;
33 }
34
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -050035 ret = qemu_chr_fe_write(vcon->chr, buf, len);
Amit Shahd02e4fa2011-07-05 16:37:49 +053036 trace_virtio_console_flush_buf(port->id, len, ret);
Amit Shah0219d732011-07-07 17:35:27 +053037
38 if (ret < 0) {
39 /*
40 * Ideally we'd get a better error code than just -1, but
41 * that's what the chardev interface gives us right now. If
42 * we had a finer-grained message, like -EPIPE, we could close
43 * this connection. Absent such error messages, the most we
44 * can do is to return 0 here.
45 *
46 * This will prevent stray -1 values to go to
47 * virtio-serial-bus.c and cause abort()s in
48 * do_flush_queued_data().
49 */
50 ret = 0;
51 }
Amit Shahd02e4fa2011-07-05 16:37:49 +053052 return ret;
Amit Shah98b19252010-01-20 00:36:52 +053053}
54
Hans de Goede0b6d2262011-03-24 11:12:03 +010055/* Callback function that's called when the guest opens the port */
56static void guest_open(VirtIOSerialPort *port)
57{
58 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
59
Amit Shah66404222011-12-21 12:28:27 +053060 if (!vcon->chr) {
61 return;
62 }
Anthony Liguoric9d830e2011-08-15 11:17:32 -050063 qemu_chr_fe_open(vcon->chr);
Hans de Goede0b6d2262011-03-24 11:12:03 +010064}
65
66/* Callback function that's called when the guest closes the port */
67static void guest_close(VirtIOSerialPort *port)
68{
69 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
70
Amit Shah66404222011-12-21 12:28:27 +053071 if (!vcon->chr) {
72 return;
73 }
Anthony Liguori28178222011-08-15 11:17:33 -050074 qemu_chr_fe_close(vcon->chr);
Hans de Goede0b6d2262011-03-24 11:12:03 +010075}
76
Amit Shah98b19252010-01-20 00:36:52 +053077/* Readiness of the guest to accept data on a port */
78static int chr_can_read(void *opaque)
79{
80 VirtConsole *vcon = opaque;
81
82 return virtio_serial_guest_ready(&vcon->port);
83}
84
85/* Send data from a char device over to the guest */
86static void chr_read(void *opaque, const uint8_t *buf, int size)
87{
88 VirtConsole *vcon = opaque;
89
Amit Shahd02e4fa2011-07-05 16:37:49 +053090 trace_virtio_console_chr_read(vcon->port.id, size);
Amit Shah98b19252010-01-20 00:36:52 +053091 virtio_serial_write(&vcon->port, buf, size);
92}
93
94static void chr_event(void *opaque, int event)
95{
96 VirtConsole *vcon = opaque;
97
Amit Shahd02e4fa2011-07-05 16:37:49 +053098 trace_virtio_console_chr_event(vcon->port.id, event);
Amit Shah98b19252010-01-20 00:36:52 +053099 switch (event) {
Amit Shah28eaf462010-12-10 17:10:43 +0530100 case CHR_EVENT_OPENED:
Amit Shah98b19252010-01-20 00:36:52 +0530101 virtio_serial_open(&vcon->port);
102 break;
Amit Shah98b19252010-01-20 00:36:52 +0530103 case CHR_EVENT_CLOSED:
104 virtio_serial_close(&vcon->port);
105 break;
106 }
107}
108
Markus Armbruster7edfe652011-05-25 14:21:14 +0200109static int virtconsole_initfn(VirtIOSerialPort *port)
Amit Shahcbe77b62010-04-05 14:20:29 +0530110{
Markus Armbruster7edfe652011-05-25 14:21:14 +0200111 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
Anthony Liguorif82e35e2011-12-04 12:38:12 -0600112 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
Markus Armbrustera15bb0d2011-05-25 14:21:13 +0200113
Anthony Liguorif82e35e2011-12-04 12:38:12 -0600114 if (port->id == 0 && !k->is_console) {
Markus Armbruster7edfe652011-05-25 14:21:14 +0200115 error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility.");
116 return -1;
117 }
118
Amit Shahcbe77b62010-04-05 14:20:29 +0530119 if (vcon->chr) {
120 qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
121 vcon);
Amit Shahcbe77b62010-04-05 14:20:29 +0530122 }
Markus Armbruster7edfe652011-05-25 14:21:14 +0200123
Amit Shahcbe77b62010-04-05 14:20:29 +0530124 return 0;
125}
126
Anthony Liguorif82e35e2011-12-04 12:38:12 -0600127static Property virtconsole_properties[] = {
128 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
129 DEFINE_PROP_END_OF_LIST(),
130};
131
132static void virtconsole_class_init(ObjectClass *klass, void *data)
133{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600134 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguorif82e35e2011-12-04 12:38:12 -0600135 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
136
137 k->is_console = true;
138 k->init = virtconsole_initfn;
139 k->have_data = flush_buf;
140 k->guest_open = guest_open;
141 k->guest_close = guest_close;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600142 dc->props = virtconsole_properties;
Anthony Liguorif82e35e2011-12-04 12:38:12 -0600143}
144
Anthony Liguori39bffca2011-12-07 21:34:16 -0600145static TypeInfo virtconsole_info = {
146 .name = "virtconsole",
147 .parent = TYPE_VIRTIO_SERIAL_PORT,
148 .instance_size = sizeof(VirtConsole),
149 .class_init = virtconsole_class_init,
Amit Shah98b19252010-01-20 00:36:52 +0530150};
151
Anthony Liguorif82e35e2011-12-04 12:38:12 -0600152static Property virtserialport_properties[] = {
153 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
154 DEFINE_PROP_END_OF_LIST(),
155};
156
157static void virtserialport_class_init(ObjectClass *klass, void *data)
158{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600159 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguorif82e35e2011-12-04 12:38:12 -0600160 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
161
162 k->init = virtconsole_initfn;
163 k->have_data = flush_buf;
164 k->guest_open = guest_open;
165 k->guest_close = guest_close;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600166 dc->props = virtserialport_properties;
Anthony Liguorif82e35e2011-12-04 12:38:12 -0600167}
168
Anthony Liguori39bffca2011-12-07 21:34:16 -0600169static TypeInfo virtserialport_info = {
170 .name = "virtserialport",
171 .parent = TYPE_VIRTIO_SERIAL_PORT,
172 .instance_size = sizeof(VirtConsole),
173 .class_init = virtserialport_class_init,
Amit Shahb60c4702010-01-20 00:36:56 +0530174};
175
Andreas Färber83f7d432012-02-09 15:20:55 +0100176static void virtconsole_register_types(void)
Amit Shahb60c4702010-01-20 00:36:56 +0530177{
Andreas Färber83f7d432012-02-09 15:20:55 +0100178 type_register_static(&virtconsole_info);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600179 type_register_static(&virtserialport_info);
Amit Shahb60c4702010-01-20 00:36:56 +0530180}
Andreas Färber83f7d432012-02-09 15:20:55 +0100181
182type_init(virtconsole_register_types)