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