2 * Virtio Console and Generic Serial Port Devices
4 * Copyright Red Hat, Inc. 2009, 2010
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 "char/char.h"
14 #include "qemu/error-report.h"
16 #include "hw/virtio-serial.h"
18 typedef struct VirtConsole
{
19 VirtIOSerialPort port
;
24 * Callback function that's called from chardevs when backend becomes
27 static gboolean
chr_write_unblocked(GIOChannel
*chan
, GIOCondition cond
,
30 VirtConsole
*vcon
= opaque
;
32 virtio_serial_throttle_port(&vcon
->port
, false);
36 /* Callback function that's called when the guest sends us data */
37 static ssize_t
flush_buf(VirtIOSerialPort
*port
, const uint8_t *buf
, size_t len
)
39 VirtConsole
*vcon
= DO_UPCAST(VirtConsole
, port
, port
);
43 /* If there's no backend, we can just say we consumed all data. */
47 ret
= qemu_chr_fe_write(vcon
->chr
, buf
, len
);
48 trace_virtio_console_flush_buf(port
->id
, len
, ret
);
51 VirtIOSerialPortClass
*k
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
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
61 virtio_serial_throttle_port(port
, true);
62 qemu_chr_fe_add_watch(vcon
->chr
, G_IO_OUT
, chr_write_unblocked
,
69 /* Callback function that's called when the guest opens/closes the port */
70 static void set_guest_connected(VirtIOSerialPort
*port
, int guest_connected
)
72 VirtConsole
*vcon
= DO_UPCAST(VirtConsole
, port
, port
);
77 qemu_chr_fe_set_open(vcon
->chr
, guest_connected
);
80 /* Readiness of the guest to accept data on a port */
81 static int chr_can_read(void *opaque
)
83 VirtConsole
*vcon
= opaque
;
85 return virtio_serial_guest_ready(&vcon
->port
);
88 /* Send data from a char device over to the guest */
89 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
91 VirtConsole
*vcon
= opaque
;
93 trace_virtio_console_chr_read(vcon
->port
.id
, size
);
94 virtio_serial_write(&vcon
->port
, buf
, size
);
97 static void chr_event(void *opaque
, int event
)
99 VirtConsole
*vcon
= opaque
;
101 trace_virtio_console_chr_event(vcon
->port
.id
, event
);
103 case CHR_EVENT_OPENED
:
104 virtio_serial_open(&vcon
->port
);
106 case CHR_EVENT_CLOSED
:
107 virtio_serial_close(&vcon
->port
);
112 static int virtconsole_initfn(VirtIOSerialPort
*port
)
114 VirtConsole
*vcon
= DO_UPCAST(VirtConsole
, port
, port
);
115 VirtIOSerialPortClass
*k
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
117 if (port
->id
== 0 && !k
->is_console
) {
118 error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility.");
123 vcon
->chr
->explicit_fe_open
= 1;
124 qemu_chr_add_handlers(vcon
->chr
, chr_can_read
, chr_read
, chr_event
,
131 static Property virtconsole_properties
[] = {
132 DEFINE_PROP_CHR("chardev", VirtConsole
, chr
),
133 DEFINE_PROP_END_OF_LIST(),
136 static void virtconsole_class_init(ObjectClass
*klass
, void *data
)
138 DeviceClass
*dc
= DEVICE_CLASS(klass
);
139 VirtIOSerialPortClass
*k
= VIRTIO_SERIAL_PORT_CLASS(klass
);
141 k
->is_console
= true;
142 k
->init
= virtconsole_initfn
;
143 k
->have_data
= flush_buf
;
144 k
->set_guest_connected
= set_guest_connected
;
145 dc
->props
= virtconsole_properties
;
148 static const TypeInfo virtconsole_info
= {
149 .name
= "virtconsole",
150 .parent
= TYPE_VIRTIO_SERIAL_PORT
,
151 .instance_size
= sizeof(VirtConsole
),
152 .class_init
= virtconsole_class_init
,
155 static Property virtserialport_properties
[] = {
156 DEFINE_PROP_CHR("chardev", VirtConsole
, chr
),
157 DEFINE_PROP_END_OF_LIST(),
160 static void virtserialport_class_init(ObjectClass
*klass
, void *data
)
162 DeviceClass
*dc
= DEVICE_CLASS(klass
);
163 VirtIOSerialPortClass
*k
= VIRTIO_SERIAL_PORT_CLASS(klass
);
165 k
->init
= virtconsole_initfn
;
166 k
->have_data
= flush_buf
;
167 k
->set_guest_connected
= set_guest_connected
;
168 dc
->props
= virtserialport_properties
;
171 static const TypeInfo virtserialport_info
= {
172 .name
= "virtserialport",
173 .parent
= TYPE_VIRTIO_SERIAL_PORT
,
174 .instance_size
= sizeof(VirtConsole
),
175 .class_init
= virtserialport_class_init
,
178 static void virtconsole_register_types(void)
180 type_register_static(&virtconsole_info
);
181 type_register_static(&virtserialport_info
);
184 type_init(virtconsole_register_types
)