root / hw / virtio-console.c @ a43f9c90
History | View | Annotate | Download (3.5 kB)
1 |
/*
|
---|---|
2 |
* Virtio Console and Generic Serial Port Devices
|
3 |
*
|
4 |
* Copyright Red Hat, Inc. 2009, 2010
|
5 |
*
|
6 |
* Authors:
|
7 |
* Amit Shah <amit.shah@redhat.com>
|
8 |
*
|
9 |
* This work is licensed under the terms of the GNU GPL, version 2. See
|
10 |
* the COPYING file in the top-level directory.
|
11 |
*/
|
12 |
|
13 |
#include "qemu-char.h" |
14 |
#include "virtio-serial.h" |
15 |
|
16 |
typedef struct VirtConsole { |
17 |
VirtIOSerialPort port; |
18 |
CharDriverState *chr; |
19 |
} VirtConsole; |
20 |
|
21 |
|
22 |
/* Callback function that's called when the guest sends us data */
|
23 |
static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len) |
24 |
{ |
25 |
VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
26 |
|
27 |
return qemu_chr_write(vcon->chr, buf, len);
|
28 |
} |
29 |
|
30 |
/* Readiness of the guest to accept data on a port */
|
31 |
static int chr_can_read(void *opaque) |
32 |
{ |
33 |
VirtConsole *vcon = opaque; |
34 |
|
35 |
return virtio_serial_guest_ready(&vcon->port);
|
36 |
} |
37 |
|
38 |
/* Send data from a char device over to the guest */
|
39 |
static void chr_read(void *opaque, const uint8_t *buf, int size) |
40 |
{ |
41 |
VirtConsole *vcon = opaque; |
42 |
|
43 |
virtio_serial_write(&vcon->port, buf, size); |
44 |
} |
45 |
|
46 |
static void chr_event(void *opaque, int event) |
47 |
{ |
48 |
VirtConsole *vcon = opaque; |
49 |
|
50 |
switch (event) {
|
51 |
case CHR_EVENT_OPENED:
|
52 |
virtio_serial_open(&vcon->port); |
53 |
break;
|
54 |
case CHR_EVENT_CLOSED:
|
55 |
virtio_serial_close(&vcon->port); |
56 |
break;
|
57 |
} |
58 |
} |
59 |
|
60 |
static int generic_port_init(VirtConsole *vcon, VirtIOSerialPort *port) |
61 |
{ |
62 |
if (vcon->chr) {
|
63 |
qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event, |
64 |
vcon); |
65 |
vcon->port.info->have_data = flush_buf; |
66 |
} |
67 |
return 0; |
68 |
} |
69 |
|
70 |
/* Virtio Console Ports */
|
71 |
static int virtconsole_initfn(VirtIOSerialPort *port) |
72 |
{ |
73 |
VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
74 |
|
75 |
port->is_console = true;
|
76 |
return generic_port_init(vcon, port);
|
77 |
} |
78 |
|
79 |
static int virtconsole_exitfn(VirtIOSerialPort *port) |
80 |
{ |
81 |
VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
82 |
|
83 |
if (vcon->chr) {
|
84 |
port->info->have_data = NULL;
|
85 |
qemu_chr_close(vcon->chr); |
86 |
} |
87 |
|
88 |
return 0; |
89 |
} |
90 |
|
91 |
static VirtIOSerialPortInfo virtconsole_info = {
|
92 |
.qdev.name = "virtconsole",
|
93 |
.qdev.size = sizeof(VirtConsole),
|
94 |
.init = virtconsole_initfn, |
95 |
.exit = virtconsole_exitfn, |
96 |
.qdev.props = (Property[]) { |
97 |
DEFINE_PROP_UINT8("is_console", VirtConsole, port.is_console, 1), |
98 |
DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
|
99 |
DEFINE_PROP_CHR("chardev", VirtConsole, chr),
|
100 |
DEFINE_PROP_STRING("name", VirtConsole, port.name),
|
101 |
DEFINE_PROP_END_OF_LIST(), |
102 |
}, |
103 |
}; |
104 |
|
105 |
static void virtconsole_register(void) |
106 |
{ |
107 |
virtio_serial_port_qdev_register(&virtconsole_info); |
108 |
} |
109 |
device_init(virtconsole_register) |
110 |
|
111 |
/* Generic Virtio Serial Ports */
|
112 |
static int virtserialport_initfn(VirtIOSerialPort *port) |
113 |
{ |
114 |
VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
115 |
|
116 |
return generic_port_init(vcon, port);
|
117 |
} |
118 |
|
119 |
static VirtIOSerialPortInfo virtserialport_info = {
|
120 |
.qdev.name = "virtserialport",
|
121 |
.qdev.size = sizeof(VirtConsole),
|
122 |
.init = virtserialport_initfn, |
123 |
.exit = virtconsole_exitfn, |
124 |
.qdev.props = (Property[]) { |
125 |
DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
|
126 |
DEFINE_PROP_CHR("chardev", VirtConsole, chr),
|
127 |
DEFINE_PROP_STRING("name", VirtConsole, port.name),
|
128 |
DEFINE_PROP_END_OF_LIST(), |
129 |
}, |
130 |
}; |
131 |
|
132 |
static void virtserialport_register(void) |
133 |
{ |
134 |
virtio_serial_port_qdev_register(&virtserialport_info); |
135 |
} |
136 |
device_init(virtserialport_register) |