2 * Virtio Console and Generic Serial Port Devices
4 * Copyright Red Hat, Inc. 2009
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 "virtio-serial.h"
16 typedef struct VirtConsole
{
17 VirtIOSerialPort port
;
22 /* Callback function that's called when the guest sends us data */
23 static size_t flush_buf(VirtIOSerialPort
*port
, const uint8_t *buf
, size_t len
)
25 VirtConsole
*vcon
= DO_UPCAST(VirtConsole
, port
, port
);
28 ret
= qemu_chr_write(vcon
->chr
, buf
, len
);
30 return ret
< 0 ? 0 : ret
;
33 /* Readiness of the guest to accept data on a port */
34 static int chr_can_read(void *opaque
)
36 VirtConsole
*vcon
= opaque
;
38 return virtio_serial_guest_ready(&vcon
->port
);
41 /* Send data from a char device over to the guest */
42 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
44 VirtConsole
*vcon
= opaque
;
46 virtio_serial_write(&vcon
->port
, buf
, size
);
49 static void chr_event(void *opaque
, int event
)
51 VirtConsole
*vcon
= opaque
;
54 case CHR_EVENT_OPENED
: {
55 virtio_serial_open(&vcon
->port
);
58 case CHR_EVENT_CLOSED
:
59 virtio_serial_close(&vcon
->port
);
64 /* Virtio Console Ports */
65 static int virtconsole_initfn(VirtIOSerialDevice
*dev
)
67 VirtIOSerialPort
*port
= DO_UPCAST(VirtIOSerialPort
, dev
, &dev
->qdev
);
68 VirtConsole
*vcon
= DO_UPCAST(VirtConsole
, port
, port
);
70 port
->info
= dev
->info
;
72 port
->is_console
= true;
75 qemu_chr_add_handlers(vcon
->chr
, chr_can_read
, chr_read
, chr_event
,
77 port
->info
->have_data
= flush_buf
;
82 static int virtconsole_exitfn(VirtIOSerialDevice
*dev
)
84 VirtIOSerialPort
*port
= DO_UPCAST(VirtIOSerialPort
, dev
, &dev
->qdev
);
85 VirtConsole
*vcon
= DO_UPCAST(VirtConsole
, port
, port
);
88 port
->info
->have_data
= NULL
;
89 qemu_chr_close(vcon
->chr
);
95 static VirtIOSerialPortInfo virtconsole_info
= {
96 .qdev
.name
= "virtconsole",
97 .qdev
.size
= sizeof(VirtConsole
),
98 .init
= virtconsole_initfn
,
99 .exit
= virtconsole_exitfn
,
100 .qdev
.props
= (Property
[]) {
101 DEFINE_PROP_UINT8("is_console", VirtConsole
, port
.is_console
, 1),
102 DEFINE_PROP_CHR("chardev", VirtConsole
, chr
),
103 DEFINE_PROP_STRING("name", VirtConsole
, port
.name
),
104 DEFINE_PROP_END_OF_LIST(),
108 static void virtconsole_register(void)
110 virtio_serial_port_qdev_register(&virtconsole_info
);
112 device_init(virtconsole_register
)
114 /* Generic Virtio Serial Ports */
115 static int virtserialport_initfn(VirtIOSerialDevice
*dev
)
117 VirtIOSerialPort
*port
= DO_UPCAST(VirtIOSerialPort
, dev
, &dev
->qdev
);
118 VirtConsole
*vcon
= DO_UPCAST(VirtConsole
, port
, port
);
120 port
->info
= dev
->info
;
123 qemu_chr_add_handlers(vcon
->chr
, chr_can_read
, chr_read
, chr_event
,
125 port
->info
->have_data
= flush_buf
;
130 static VirtIOSerialPortInfo virtserialport_info
= {
131 .qdev
.name
= "virtserialport",
132 .qdev
.size
= sizeof(VirtConsole
),
133 .init
= virtserialport_initfn
,
134 .exit
= virtconsole_exitfn
,
135 .qdev
.props
= (Property
[]) {
136 DEFINE_PROP_CHR("chardev", VirtConsole
, chr
),
137 DEFINE_PROP_STRING("name", VirtConsole
, port
.name
),
138 DEFINE_PROP_END_OF_LIST(),
142 static void virtserialport_register(void)
144 virtio_serial_port_qdev_register(&virtserialport_info
);
146 device_init(virtserialport_register
)