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 "qemu-char.h"
14 #include "qemu-error.h"
16 #include "virtio-serial.h"
18 typedef struct VirtConsole
{
19 VirtIOSerialPort port
;
24 /* Callback function that's called when the guest sends us data */
25 static ssize_t
flush_buf(VirtIOSerialPort
*port
, const uint8_t *buf
, size_t len
)
27 VirtConsole
*vcon
= DO_UPCAST(VirtConsole
, port
, port
);
31 /* If there's no backend, we can just say we consumed all data. */
35 ret
= qemu_chr_fe_write(vcon
->chr
, buf
, len
);
36 trace_virtio_console_flush_buf(port
->id
, len
, ret
);
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.
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().
55 /* Callback function that's called when the guest opens the port */
56 static void guest_open(VirtIOSerialPort
*port
)
58 VirtConsole
*vcon
= DO_UPCAST(VirtConsole
, port
, port
);
63 qemu_chr_fe_open(vcon
->chr
);
66 /* Callback function that's called when the guest closes the port */
67 static void guest_close(VirtIOSerialPort
*port
)
69 VirtConsole
*vcon
= DO_UPCAST(VirtConsole
, port
, port
);
74 qemu_chr_fe_close(vcon
->chr
);
77 /* Readiness of the guest to accept data on a port */
78 static int chr_can_read(void *opaque
)
80 VirtConsole
*vcon
= opaque
;
82 return virtio_serial_guest_ready(&vcon
->port
);
85 /* Send data from a char device over to the guest */
86 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
88 VirtConsole
*vcon
= opaque
;
90 trace_virtio_console_chr_read(vcon
->port
.id
, size
);
91 virtio_serial_write(&vcon
->port
, buf
, size
);
94 static void chr_event(void *opaque
, int event
)
96 VirtConsole
*vcon
= opaque
;
98 trace_virtio_console_chr_event(vcon
->port
.id
, event
);
100 case CHR_EVENT_OPENED
:
101 virtio_serial_open(&vcon
->port
);
103 case CHR_EVENT_CLOSED
:
104 virtio_serial_close(&vcon
->port
);
109 static int virtconsole_initfn(VirtIOSerialPort
*port
)
111 VirtConsole
*vcon
= DO_UPCAST(VirtConsole
, port
, port
);
112 VirtIOSerialPortInfo
*info
= DO_UPCAST(VirtIOSerialPortInfo
, qdev
,
113 vcon
->port
.dev
.info
);
115 if (port
->id
== 0 && !info
->is_console
) {
116 error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility.");
121 qemu_chr_add_handlers(vcon
->chr
, chr_can_read
, chr_read
, chr_event
,
123 info
->have_data
= flush_buf
;
124 info
->guest_open
= guest_open
;
125 info
->guest_close
= guest_close
;
131 static int virtconsole_exitfn(VirtIOSerialPort
*port
)
133 VirtConsole
*vcon
= DO_UPCAST(VirtConsole
, port
, port
);
137 * Instead of closing the chardev, free it so it can be used
138 * for other purposes.
140 qemu_chr_add_handlers(vcon
->chr
, NULL
, NULL
, NULL
, NULL
);
146 static VirtIOSerialPortInfo virtconsole_info
= {
147 .qdev
.name
= "virtconsole",
148 .qdev
.size
= sizeof(VirtConsole
),
150 .init
= virtconsole_initfn
,
151 .exit
= virtconsole_exitfn
,
152 .qdev
.props
= (Property
[]) {
153 DEFINE_PROP_CHR("chardev", VirtConsole
, chr
),
154 DEFINE_PROP_END_OF_LIST(),
158 static void virtconsole_register(void)
160 virtio_serial_port_qdev_register(&virtconsole_info
);
162 device_init(virtconsole_register
)
164 static VirtIOSerialPortInfo virtserialport_info
= {
165 .qdev
.name
= "virtserialport",
166 .qdev
.size
= sizeof(VirtConsole
),
167 .init
= virtconsole_initfn
,
168 .exit
= virtconsole_exitfn
,
169 .qdev
.props
= (Property
[]) {
170 DEFINE_PROP_CHR("chardev", VirtConsole
, chr
),
171 DEFINE_PROP_END_OF_LIST(),
175 static void virtserialport_register(void)
177 virtio_serial_port_qdev_register(&virtserialport_info
);
179 device_init(virtserialport_register
)