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 "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
);
52 * Ideally we'd get a better error code than just -1, but
53 * that's what the chardev interface gives us right now. If
54 * we had a finer-grained message, like -EPIPE, we could close
55 * this connection. Absent such error messages, the most we
56 * can do is to return 0 here.
58 * This will prevent stray -1 values to go to
59 * virtio-serial-bus.c and cause abort()s in
60 * do_flush_queued_data().
63 qemu_chr_fe_add_watch(vcon
->chr
, G_IO_OUT
, chr_write_unblocked
, vcon
);
68 /* Callback function that's called when the guest opens the port */
69 static void guest_open(VirtIOSerialPort
*port
)
71 VirtConsole
*vcon
= DO_UPCAST(VirtConsole
, port
, port
);
76 qemu_chr_fe_open(vcon
->chr
);
79 /* Callback function that's called when the guest closes the port */
80 static void guest_close(VirtIOSerialPort
*port
)
82 VirtConsole
*vcon
= DO_UPCAST(VirtConsole
, port
, port
);
87 qemu_chr_fe_close(vcon
->chr
);
90 /* Readiness of the guest to accept data on a port */
91 static int chr_can_read(void *opaque
)
93 VirtConsole
*vcon
= opaque
;
95 return virtio_serial_guest_ready(&vcon
->port
);
98 /* Send data from a char device over to the guest */
99 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
101 VirtConsole
*vcon
= opaque
;
103 trace_virtio_console_chr_read(vcon
->port
.id
, size
);
104 virtio_serial_write(&vcon
->port
, buf
, size
);
107 static void chr_event(void *opaque
, int event
)
109 VirtConsole
*vcon
= opaque
;
111 trace_virtio_console_chr_event(vcon
->port
.id
, event
);
113 case CHR_EVENT_OPENED
:
114 virtio_serial_open(&vcon
->port
);
116 case CHR_EVENT_CLOSED
:
117 virtio_serial_close(&vcon
->port
);
122 static int virtconsole_initfn(VirtIOSerialPort
*port
)
124 VirtConsole
*vcon
= DO_UPCAST(VirtConsole
, port
, port
);
125 VirtIOSerialPortClass
*k
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
127 if (port
->id
== 0 && !k
->is_console
) {
128 error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility.");
133 qemu_chr_add_handlers(vcon
->chr
, chr_can_read
, chr_read
, chr_event
,
140 static Property virtconsole_properties
[] = {
141 DEFINE_PROP_CHR("chardev", VirtConsole
, chr
),
142 DEFINE_PROP_END_OF_LIST(),
145 static void virtconsole_class_init(ObjectClass
*klass
, void *data
)
147 DeviceClass
*dc
= DEVICE_CLASS(klass
);
148 VirtIOSerialPortClass
*k
= VIRTIO_SERIAL_PORT_CLASS(klass
);
150 k
->is_console
= true;
151 k
->init
= virtconsole_initfn
;
152 k
->have_data
= flush_buf
;
153 k
->guest_open
= guest_open
;
154 k
->guest_close
= guest_close
;
155 dc
->props
= virtconsole_properties
;
158 static const TypeInfo virtconsole_info
= {
159 .name
= "virtconsole",
160 .parent
= TYPE_VIRTIO_SERIAL_PORT
,
161 .instance_size
= sizeof(VirtConsole
),
162 .class_init
= virtconsole_class_init
,
165 static Property virtserialport_properties
[] = {
166 DEFINE_PROP_CHR("chardev", VirtConsole
, chr
),
167 DEFINE_PROP_END_OF_LIST(),
170 static void virtserialport_class_init(ObjectClass
*klass
, void *data
)
172 DeviceClass
*dc
= DEVICE_CLASS(klass
);
173 VirtIOSerialPortClass
*k
= VIRTIO_SERIAL_PORT_CLASS(klass
);
175 k
->init
= virtconsole_initfn
;
176 k
->have_data
= flush_buf
;
177 k
->guest_open
= guest_open
;
178 k
->guest_close
= guest_close
;
179 dc
->props
= virtserialport_properties
;
182 static const TypeInfo virtserialport_info
= {
183 .name
= "virtserialport",
184 .parent
= TYPE_VIRTIO_SERIAL_PORT
,
185 .instance_size
= sizeof(VirtConsole
),
186 .class_init
= virtserialport_class_init
,
189 static void virtconsole_register_types(void)
191 type_register_static(&virtconsole_info
);
192 type_register_static(&virtserialport_info
);
195 type_init(virtconsole_register_types
)