]> git.proxmox.com Git - mirror_qemu.git/blame - hw/char/virtio-console.c
sysemu: avoid proliferation of include/ subdirectories
[mirror_qemu.git] / hw / char / virtio-console.c
CommitLineData
98b19252
AS
1/*
2 * Virtio Console and Generic Serial Port Devices
3 *
71c092e9 4 * Copyright Red Hat, Inc. 2009, 2010
98b19252
AS
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
dccfcd0e 13#include "sysemu/char.h"
1de7afc9 14#include "qemu/error-report.h"
d02e4fa4 15#include "trace.h"
0d09e41a 16#include "hw/virtio/virtio-serial.h"
98b19252
AS
17
18typedef struct VirtConsole {
19 VirtIOSerialPort port;
20 CharDriverState *chr;
21} VirtConsole;
22
7df4d457
AS
23/*
24 * Callback function that's called from chardevs when backend becomes
25 * writable.
26 */
27static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond,
28 void *opaque)
29{
30 VirtConsole *vcon = opaque;
31
32 virtio_serial_throttle_port(&vcon->port, false);
33 return FALSE;
34}
98b19252
AS
35
36/* Callback function that's called when the guest sends us data */
e300ac27 37static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
98b19252
AS
38{
39 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
d02e4fa4 40 ssize_t ret;
98b19252 41
6640422c
AS
42 if (!vcon->chr) {
43 /* If there's no backend, we can just say we consumed all data. */
44 return len;
45 }
46
2cc6e0a1 47 ret = qemu_chr_fe_write(vcon->chr, buf, len);
d02e4fa4 48 trace_virtio_console_flush_buf(port->id, len, ret);
0219d732 49
d6258c93
AS
50 if (ret <= 0) {
51 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
52
0219d732
AS
53 /*
54 * Ideally we'd get a better error code than just -1, but
55 * that's what the chardev interface gives us right now. If
56 * we had a finer-grained message, like -EPIPE, we could close
d6258c93 57 * this connection.
0219d732
AS
58 */
59 ret = 0;
d6258c93
AS
60 if (!k->is_console) {
61 virtio_serial_throttle_port(port, true);
62 qemu_chr_fe_add_watch(vcon->chr, G_IO_OUT, chr_write_unblocked,
63 vcon);
64 }
0219d732 65 }
d02e4fa4 66 return ret;
98b19252
AS
67}
68
b2c1394a
HG
69/* Callback function that's called when the guest opens/closes the port */
70static void set_guest_connected(VirtIOSerialPort *port, int guest_connected)
0b6d2266
HG
71{
72 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
73
6640422c
AS
74 if (!vcon->chr) {
75 return;
76 }
b2c1394a 77 qemu_chr_fe_set_open(vcon->chr, guest_connected);
0b6d2266
HG
78}
79
98b19252
AS
80/* Readiness of the guest to accept data on a port */
81static int chr_can_read(void *opaque)
82{
83 VirtConsole *vcon = opaque;
84
85 return virtio_serial_guest_ready(&vcon->port);
86}
87
88/* Send data from a char device over to the guest */
89static void chr_read(void *opaque, const uint8_t *buf, int size)
90{
91 VirtConsole *vcon = opaque;
92
d02e4fa4 93 trace_virtio_console_chr_read(vcon->port.id, size);
98b19252
AS
94 virtio_serial_write(&vcon->port, buf, size);
95}
96
97static void chr_event(void *opaque, int event)
98{
99 VirtConsole *vcon = opaque;
100
d02e4fa4 101 trace_virtio_console_chr_event(vcon->port.id, event);
98b19252 102 switch (event) {
28eaf465 103 case CHR_EVENT_OPENED:
98b19252
AS
104 virtio_serial_open(&vcon->port);
105 break;
98b19252
AS
106 case CHR_EVENT_CLOSED:
107 virtio_serial_close(&vcon->port);
108 break;
109 }
110}
111
7edfe652 112static int virtconsole_initfn(VirtIOSerialPort *port)
98b19252 113{
7edfe652 114 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
f82e35e3 115 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
a15bb0d6 116
f82e35e3 117 if (port->id == 0 && !k->is_console) {
7edfe652
MA
118 error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility.");
119 return -1;
120 }
121
98b19252 122 if (vcon->chr) {
19083228 123 vcon->chr->explicit_fe_open = 1;
98b19252
AS
124 qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
125 vcon);
98b19252 126 }
cbe77b61 127
7edfe652 128 return 0;
cbe77b61
AS
129}
130
f82e35e3
AL
131static Property virtconsole_properties[] = {
132 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
133 DEFINE_PROP_END_OF_LIST(),
134};
135
136static void virtconsole_class_init(ObjectClass *klass, void *data)
137{
39bffca2 138 DeviceClass *dc = DEVICE_CLASS(klass);
f82e35e3
AL
139 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
140
141 k->is_console = true;
142 k->init = virtconsole_initfn;
143 k->have_data = flush_buf;
b2c1394a 144 k->set_guest_connected = set_guest_connected;
39bffca2 145 dc->props = virtconsole_properties;
f82e35e3
AL
146}
147
8c43a6f0 148static const TypeInfo virtconsole_info = {
39bffca2
AL
149 .name = "virtconsole",
150 .parent = TYPE_VIRTIO_SERIAL_PORT,
151 .instance_size = sizeof(VirtConsole),
152 .class_init = virtconsole_class_init,
98b19252
AS
153};
154
f82e35e3
AL
155static Property virtserialport_properties[] = {
156 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
157 DEFINE_PROP_END_OF_LIST(),
158};
159
160static void virtserialport_class_init(ObjectClass *klass, void *data)
161{
39bffca2 162 DeviceClass *dc = DEVICE_CLASS(klass);
f82e35e3
AL
163 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
164
165 k->init = virtconsole_initfn;
166 k->have_data = flush_buf;
b2c1394a 167 k->set_guest_connected = set_guest_connected;
39bffca2 168 dc->props = virtserialport_properties;
f82e35e3
AL
169}
170
8c43a6f0 171static const TypeInfo virtserialport_info = {
39bffca2
AL
172 .name = "virtserialport",
173 .parent = TYPE_VIRTIO_SERIAL_PORT,
174 .instance_size = sizeof(VirtConsole),
175 .class_init = virtserialport_class_init,
b60c470b
AS
176};
177
83f7d43a 178static void virtconsole_register_types(void)
b60c470b 179{
83f7d43a 180 type_register_static(&virtconsole_info);
39bffca2 181 type_register_static(&virtserialport_info);
b60c470b 182}
83f7d43a
AF
183
184type_init(virtconsole_register_types)