root / hw / virtio-console.c @ d6258c93
History | View | Annotate | Download (5.2 kB)
1 | 98b19252 | Amit Shah | /*
|
---|---|---|---|
2 | 98b19252 | Amit Shah | * Virtio Console and Generic Serial Port Devices
|
3 | 98b19252 | Amit Shah | *
|
4 | 71c092e9 | Amit Shah | * Copyright Red Hat, Inc. 2009, 2010
|
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 | 927d4878 | Paolo Bonzini | #include "char/char.h" |
14 | 1de7afc9 | Paolo Bonzini | #include "qemu/error-report.h" |
15 | d02e4fa4 | Amit Shah | #include "trace.h" |
16 | 98b19252 | Amit Shah | #include "virtio-serial.h" |
17 | 98b19252 | Amit Shah | |
18 | 98b19252 | Amit Shah | typedef struct VirtConsole { |
19 | 98b19252 | Amit Shah | VirtIOSerialPort port; |
20 | 98b19252 | Amit Shah | CharDriverState *chr; |
21 | 98b19252 | Amit Shah | } VirtConsole; |
22 | 98b19252 | Amit Shah | |
23 | 7df4d457 | Amit Shah | /*
|
24 | 7df4d457 | Amit Shah | * Callback function that's called from chardevs when backend becomes
|
25 | 7df4d457 | Amit Shah | * writable.
|
26 | 7df4d457 | Amit Shah | */
|
27 | 7df4d457 | Amit Shah | static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond,
|
28 | 7df4d457 | Amit Shah | void *opaque)
|
29 | 7df4d457 | Amit Shah | { |
30 | 7df4d457 | Amit Shah | VirtConsole *vcon = opaque; |
31 | 7df4d457 | Amit Shah | |
32 | 7df4d457 | Amit Shah | virtio_serial_throttle_port(&vcon->port, false);
|
33 | 7df4d457 | Amit Shah | return FALSE;
|
34 | 7df4d457 | Amit Shah | } |
35 | 98b19252 | Amit Shah | |
36 | 98b19252 | Amit Shah | /* Callback function that's called when the guest sends us data */
|
37 | e300ac27 | Amit Shah | static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len) |
38 | 98b19252 | Amit Shah | { |
39 | 98b19252 | Amit Shah | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
40 | d02e4fa4 | Amit Shah | ssize_t ret; |
41 | 98b19252 | Amit Shah | |
42 | 6640422c | Amit Shah | if (!vcon->chr) {
|
43 | 6640422c | Amit Shah | /* If there's no backend, we can just say we consumed all data. */
|
44 | 6640422c | Amit Shah | return len;
|
45 | 6640422c | Amit Shah | } |
46 | 6640422c | Amit Shah | |
47 | 2cc6e0a1 | Anthony Liguori | ret = qemu_chr_fe_write(vcon->chr, buf, len); |
48 | d02e4fa4 | Amit Shah | trace_virtio_console_flush_buf(port->id, len, ret); |
49 | 0219d732 | Amit Shah | |
50 | d6258c93 | Amit Shah | if (ret <= 0) { |
51 | d6258c93 | Amit Shah | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port); |
52 | d6258c93 | Amit Shah | |
53 | 0219d732 | Amit Shah | /*
|
54 | 0219d732 | Amit Shah | * Ideally we'd get a better error code than just -1, but
|
55 | 0219d732 | Amit Shah | * that's what the chardev interface gives us right now. If
|
56 | 0219d732 | Amit Shah | * we had a finer-grained message, like -EPIPE, we could close
|
57 | d6258c93 | Amit Shah | * this connection.
|
58 | 0219d732 | Amit Shah | */
|
59 | 0219d732 | Amit Shah | ret = 0;
|
60 | d6258c93 | Amit Shah | if (!k->is_console) {
|
61 | d6258c93 | Amit Shah | virtio_serial_throttle_port(port, true);
|
62 | d6258c93 | Amit Shah | qemu_chr_fe_add_watch(vcon->chr, G_IO_OUT, chr_write_unblocked, |
63 | d6258c93 | Amit Shah | vcon); |
64 | d6258c93 | Amit Shah | } |
65 | 0219d732 | Amit Shah | } |
66 | d02e4fa4 | Amit Shah | return ret;
|
67 | 98b19252 | Amit Shah | } |
68 | 98b19252 | Amit Shah | |
69 | 0b6d2266 | Hans de Goede | /* Callback function that's called when the guest opens the port */
|
70 | 0b6d2266 | Hans de Goede | static void guest_open(VirtIOSerialPort *port) |
71 | 0b6d2266 | Hans de Goede | { |
72 | 0b6d2266 | Hans de Goede | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
73 | 0b6d2266 | Hans de Goede | |
74 | 6640422c | Amit Shah | if (!vcon->chr) {
|
75 | 6640422c | Amit Shah | return;
|
76 | 6640422c | Amit Shah | } |
77 | c9d830ed | Anthony Liguori | qemu_chr_fe_open(vcon->chr); |
78 | 0b6d2266 | Hans de Goede | } |
79 | 0b6d2266 | Hans de Goede | |
80 | 0b6d2266 | Hans de Goede | /* Callback function that's called when the guest closes the port */
|
81 | 0b6d2266 | Hans de Goede | static void guest_close(VirtIOSerialPort *port) |
82 | 0b6d2266 | Hans de Goede | { |
83 | 0b6d2266 | Hans de Goede | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
84 | 0b6d2266 | Hans de Goede | |
85 | 6640422c | Amit Shah | if (!vcon->chr) {
|
86 | 6640422c | Amit Shah | return;
|
87 | 6640422c | Amit Shah | } |
88 | 2817822d | Anthony Liguori | qemu_chr_fe_close(vcon->chr); |
89 | 0b6d2266 | Hans de Goede | } |
90 | 0b6d2266 | Hans de Goede | |
91 | 98b19252 | Amit Shah | /* Readiness of the guest to accept data on a port */
|
92 | 98b19252 | Amit Shah | static int chr_can_read(void *opaque) |
93 | 98b19252 | Amit Shah | { |
94 | 98b19252 | Amit Shah | VirtConsole *vcon = opaque; |
95 | 98b19252 | Amit Shah | |
96 | 98b19252 | Amit Shah | return virtio_serial_guest_ready(&vcon->port);
|
97 | 98b19252 | Amit Shah | } |
98 | 98b19252 | Amit Shah | |
99 | 98b19252 | Amit Shah | /* Send data from a char device over to the guest */
|
100 | 98b19252 | Amit Shah | static void chr_read(void *opaque, const uint8_t *buf, int size) |
101 | 98b19252 | Amit Shah | { |
102 | 98b19252 | Amit Shah | VirtConsole *vcon = opaque; |
103 | 98b19252 | Amit Shah | |
104 | d02e4fa4 | Amit Shah | trace_virtio_console_chr_read(vcon->port.id, size); |
105 | 98b19252 | Amit Shah | virtio_serial_write(&vcon->port, buf, size); |
106 | 98b19252 | Amit Shah | } |
107 | 98b19252 | Amit Shah | |
108 | 98b19252 | Amit Shah | static void chr_event(void *opaque, int event) |
109 | 98b19252 | Amit Shah | { |
110 | 98b19252 | Amit Shah | VirtConsole *vcon = opaque; |
111 | 98b19252 | Amit Shah | |
112 | d02e4fa4 | Amit Shah | trace_virtio_console_chr_event(vcon->port.id, event); |
113 | 98b19252 | Amit Shah | switch (event) {
|
114 | 28eaf465 | Amit Shah | case CHR_EVENT_OPENED:
|
115 | 98b19252 | Amit Shah | virtio_serial_open(&vcon->port); |
116 | 98b19252 | Amit Shah | break;
|
117 | 98b19252 | Amit Shah | case CHR_EVENT_CLOSED:
|
118 | 98b19252 | Amit Shah | virtio_serial_close(&vcon->port); |
119 | 98b19252 | Amit Shah | break;
|
120 | 98b19252 | Amit Shah | } |
121 | 98b19252 | Amit Shah | } |
122 | 98b19252 | Amit Shah | |
123 | 7edfe652 | Markus Armbruster | static int virtconsole_initfn(VirtIOSerialPort *port) |
124 | 98b19252 | Amit Shah | { |
125 | 7edfe652 | Markus Armbruster | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
126 | f82e35e3 | Anthony Liguori | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port); |
127 | a15bb0d6 | Markus Armbruster | |
128 | f82e35e3 | Anthony Liguori | if (port->id == 0 && !k->is_console) { |
129 | 7edfe652 | Markus Armbruster | error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility.");
|
130 | 7edfe652 | Markus Armbruster | return -1; |
131 | 7edfe652 | Markus Armbruster | } |
132 | 7edfe652 | Markus Armbruster | |
133 | 98b19252 | Amit Shah | if (vcon->chr) {
|
134 | 98b19252 | Amit Shah | qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event, |
135 | 98b19252 | Amit Shah | vcon); |
136 | 98b19252 | Amit Shah | } |
137 | cbe77b61 | Amit Shah | |
138 | 7edfe652 | Markus Armbruster | return 0; |
139 | cbe77b61 | Amit Shah | } |
140 | cbe77b61 | Amit Shah | |
141 | f82e35e3 | Anthony Liguori | static Property virtconsole_properties[] = {
|
142 | f82e35e3 | Anthony Liguori | DEFINE_PROP_CHR("chardev", VirtConsole, chr),
|
143 | f82e35e3 | Anthony Liguori | DEFINE_PROP_END_OF_LIST(), |
144 | f82e35e3 | Anthony Liguori | }; |
145 | f82e35e3 | Anthony Liguori | |
146 | f82e35e3 | Anthony Liguori | static void virtconsole_class_init(ObjectClass *klass, void *data) |
147 | f82e35e3 | Anthony Liguori | { |
148 | 39bffca2 | Anthony Liguori | DeviceClass *dc = DEVICE_CLASS(klass); |
149 | f82e35e3 | Anthony Liguori | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass); |
150 | f82e35e3 | Anthony Liguori | |
151 | f82e35e3 | Anthony Liguori | k->is_console = true;
|
152 | f82e35e3 | Anthony Liguori | k->init = virtconsole_initfn; |
153 | f82e35e3 | Anthony Liguori | k->have_data = flush_buf; |
154 | f82e35e3 | Anthony Liguori | k->guest_open = guest_open; |
155 | f82e35e3 | Anthony Liguori | k->guest_close = guest_close; |
156 | 39bffca2 | Anthony Liguori | dc->props = virtconsole_properties; |
157 | f82e35e3 | Anthony Liguori | } |
158 | f82e35e3 | Anthony Liguori | |
159 | 8c43a6f0 | Andreas Färber | static const TypeInfo virtconsole_info = { |
160 | 39bffca2 | Anthony Liguori | .name = "virtconsole",
|
161 | 39bffca2 | Anthony Liguori | .parent = TYPE_VIRTIO_SERIAL_PORT, |
162 | 39bffca2 | Anthony Liguori | .instance_size = sizeof(VirtConsole),
|
163 | 39bffca2 | Anthony Liguori | .class_init = virtconsole_class_init, |
164 | 98b19252 | Amit Shah | }; |
165 | 98b19252 | Amit Shah | |
166 | f82e35e3 | Anthony Liguori | static Property virtserialport_properties[] = {
|
167 | f82e35e3 | Anthony Liguori | DEFINE_PROP_CHR("chardev", VirtConsole, chr),
|
168 | f82e35e3 | Anthony Liguori | DEFINE_PROP_END_OF_LIST(), |
169 | f82e35e3 | Anthony Liguori | }; |
170 | f82e35e3 | Anthony Liguori | |
171 | f82e35e3 | Anthony Liguori | static void virtserialport_class_init(ObjectClass *klass, void *data) |
172 | f82e35e3 | Anthony Liguori | { |
173 | 39bffca2 | Anthony Liguori | DeviceClass *dc = DEVICE_CLASS(klass); |
174 | f82e35e3 | Anthony Liguori | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass); |
175 | f82e35e3 | Anthony Liguori | |
176 | f82e35e3 | Anthony Liguori | k->init = virtconsole_initfn; |
177 | f82e35e3 | Anthony Liguori | k->have_data = flush_buf; |
178 | f82e35e3 | Anthony Liguori | k->guest_open = guest_open; |
179 | f82e35e3 | Anthony Liguori | k->guest_close = guest_close; |
180 | 39bffca2 | Anthony Liguori | dc->props = virtserialport_properties; |
181 | f82e35e3 | Anthony Liguori | } |
182 | f82e35e3 | Anthony Liguori | |
183 | 8c43a6f0 | Andreas Färber | static const TypeInfo virtserialport_info = { |
184 | 39bffca2 | Anthony Liguori | .name = "virtserialport",
|
185 | 39bffca2 | Anthony Liguori | .parent = TYPE_VIRTIO_SERIAL_PORT, |
186 | 39bffca2 | Anthony Liguori | .instance_size = sizeof(VirtConsole),
|
187 | 39bffca2 | Anthony Liguori | .class_init = virtserialport_class_init, |
188 | b60c470b | Amit Shah | }; |
189 | b60c470b | Amit Shah | |
190 | 83f7d43a | Andreas Färber | static void virtconsole_register_types(void) |
191 | b60c470b | Amit Shah | { |
192 | 83f7d43a | Andreas Färber | type_register_static(&virtconsole_info); |
193 | 39bffca2 | Anthony Liguori | type_register_static(&virtserialport_info); |
194 | b60c470b | Amit Shah | } |
195 | 83f7d43a | Andreas Färber | |
196 | 83f7d43a | Andreas Färber | type_init(virtconsole_register_types) |