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"
15 #include "virtio-serial.h"
17 typedef struct VirtConsole
{
18 VirtIOSerialPort port
;
23 /* Callback function that's called when the guest sends us data */
24 static ssize_t
flush_buf(VirtIOSerialPort
*port
, const uint8_t *buf
, size_t len
)
26 VirtConsole
*vcon
= DO_UPCAST(VirtConsole
, port
, port
);
28 return qemu_chr_write(vcon
->chr
, buf
, len
);
31 /* Readiness of the guest to accept data on a port */
32 static int chr_can_read(void *opaque
)
34 VirtConsole
*vcon
= opaque
;
36 return virtio_serial_guest_ready(&vcon
->port
);
39 /* Send data from a char device over to the guest */
40 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
42 VirtConsole
*vcon
= opaque
;
44 virtio_serial_write(&vcon
->port
, buf
, size
);
47 static void chr_event(void *opaque
, int event
)
49 VirtConsole
*vcon
= opaque
;
52 case CHR_EVENT_OPENED
:
53 virtio_serial_open(&vcon
->port
);
55 case CHR_EVENT_CLOSED
:
56 virtio_serial_close(&vcon
->port
);
61 static int generic_port_init(VirtConsole
*vcon
, VirtIOSerialPort
*port
)
64 qemu_chr_add_handlers(vcon
->chr
, chr_can_read
, chr_read
, chr_event
,
66 vcon
->port
.info
->have_data
= flush_buf
;
71 /* Virtio Console Ports */
72 static int virtconsole_initfn(VirtIOSerialPort
*port
)
74 VirtConsole
*vcon
= DO_UPCAST(VirtConsole
, port
, port
);
76 port
->is_console
= true;
77 return generic_port_init(vcon
, port
);
80 static int virtconsole_exitfn(VirtIOSerialPort
*port
)
82 VirtConsole
*vcon
= DO_UPCAST(VirtConsole
, port
, port
);
85 port
->info
->have_data
= NULL
;
86 qemu_chr_close(vcon
->chr
);
92 static VirtIOSerialPortInfo virtconsole_info
= {
93 .qdev
.name
= "virtconsole",
94 .qdev
.size
= sizeof(VirtConsole
),
95 .init
= virtconsole_initfn
,
96 .exit
= virtconsole_exitfn
,
97 .qdev
.props
= (Property
[]) {
98 DEFINE_PROP_UINT8("is_console", VirtConsole
, port
.is_console
, 1),
99 DEFINE_PROP_UINT32("nr", VirtConsole
, port
.id
, VIRTIO_CONSOLE_BAD_ID
),
100 DEFINE_PROP_CHR("chardev", VirtConsole
, chr
),
101 DEFINE_PROP_STRING("name", VirtConsole
, port
.name
),
102 DEFINE_PROP_END_OF_LIST(),
106 static void virtconsole_register(void)
108 virtio_serial_port_qdev_register(&virtconsole_info
);
110 device_init(virtconsole_register
)
112 /* Generic Virtio Serial Ports */
113 static int virtserialport_initfn(VirtIOSerialPort
*port
)
115 VirtConsole
*vcon
= DO_UPCAST(VirtConsole
, port
, port
);
119 * Disallow a generic port at id 0, that's reserved for
122 error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility.");
125 return generic_port_init(vcon
, port
);
128 static VirtIOSerialPortInfo virtserialport_info
= {
129 .qdev
.name
= "virtserialport",
130 .qdev
.size
= sizeof(VirtConsole
),
131 .init
= virtserialport_initfn
,
132 .exit
= virtconsole_exitfn
,
133 .qdev
.props
= (Property
[]) {
134 DEFINE_PROP_UINT32("nr", VirtConsole
, port
.id
, VIRTIO_CONSOLE_BAD_ID
),
135 DEFINE_PROP_CHR("chardev", VirtConsole
, chr
),
136 DEFINE_PROP_STRING("name", VirtConsole
, port
.name
),
137 DEFINE_PROP_END_OF_LIST(),
141 static void virtserialport_register(void)
143 virtio_serial_port_qdev_register(&virtserialport_info
);
145 device_init(virtserialport_register
)