root / hw / virtio-console.c @ 75b0646f
History | View | Annotate | Download (4 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 | 98b19252 | Amit Shah | #include "qemu-char.h" |
14 | 0b8b716d | Amit Shah | #include "qemu-error.h" |
15 | 98b19252 | Amit Shah | #include "virtio-serial.h" |
16 | 98b19252 | Amit Shah | |
17 | 98b19252 | Amit Shah | typedef struct VirtConsole { |
18 | 98b19252 | Amit Shah | VirtIOSerialPort port; |
19 | 98b19252 | Amit Shah | CharDriverState *chr; |
20 | 98b19252 | Amit Shah | } VirtConsole; |
21 | 98b19252 | Amit Shah | |
22 | 98b19252 | Amit Shah | |
23 | 98b19252 | Amit Shah | /* Callback function that's called when the guest sends us data */
|
24 | e300ac27 | Amit Shah | static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len) |
25 | 98b19252 | Amit Shah | { |
26 | 98b19252 | Amit Shah | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
27 | 98b19252 | Amit Shah | |
28 | e300ac27 | Amit Shah | return qemu_chr_write(vcon->chr, buf, len);
|
29 | 98b19252 | Amit Shah | } |
30 | 98b19252 | Amit Shah | |
31 | 0b6d2266 | Hans de Goede | /* Callback function that's called when the guest opens the port */
|
32 | 0b6d2266 | Hans de Goede | static void guest_open(VirtIOSerialPort *port) |
33 | 0b6d2266 | Hans de Goede | { |
34 | 0b6d2266 | Hans de Goede | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
35 | 0b6d2266 | Hans de Goede | |
36 | 0b6d2266 | Hans de Goede | qemu_chr_guest_open(vcon->chr); |
37 | 0b6d2266 | Hans de Goede | } |
38 | 0b6d2266 | Hans de Goede | |
39 | 0b6d2266 | Hans de Goede | /* Callback function that's called when the guest closes the port */
|
40 | 0b6d2266 | Hans de Goede | static void guest_close(VirtIOSerialPort *port) |
41 | 0b6d2266 | Hans de Goede | { |
42 | 0b6d2266 | Hans de Goede | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
43 | 0b6d2266 | Hans de Goede | |
44 | 0b6d2266 | Hans de Goede | qemu_chr_guest_close(vcon->chr); |
45 | 0b6d2266 | Hans de Goede | } |
46 | 0b6d2266 | Hans de Goede | |
47 | 98b19252 | Amit Shah | /* Readiness of the guest to accept data on a port */
|
48 | 98b19252 | Amit Shah | static int chr_can_read(void *opaque) |
49 | 98b19252 | Amit Shah | { |
50 | 98b19252 | Amit Shah | VirtConsole *vcon = opaque; |
51 | 98b19252 | Amit Shah | |
52 | 98b19252 | Amit Shah | return virtio_serial_guest_ready(&vcon->port);
|
53 | 98b19252 | Amit Shah | } |
54 | 98b19252 | Amit Shah | |
55 | 98b19252 | Amit Shah | /* Send data from a char device over to the guest */
|
56 | 98b19252 | Amit Shah | static void chr_read(void *opaque, const uint8_t *buf, int size) |
57 | 98b19252 | Amit Shah | { |
58 | 98b19252 | Amit Shah | VirtConsole *vcon = opaque; |
59 | 98b19252 | Amit Shah | |
60 | 98b19252 | Amit Shah | virtio_serial_write(&vcon->port, buf, size); |
61 | 98b19252 | Amit Shah | } |
62 | 98b19252 | Amit Shah | |
63 | 98b19252 | Amit Shah | static void chr_event(void *opaque, int event) |
64 | 98b19252 | Amit Shah | { |
65 | 98b19252 | Amit Shah | VirtConsole *vcon = opaque; |
66 | 98b19252 | Amit Shah | |
67 | 98b19252 | Amit Shah | switch (event) {
|
68 | 28eaf465 | Amit Shah | case CHR_EVENT_OPENED:
|
69 | 98b19252 | Amit Shah | virtio_serial_open(&vcon->port); |
70 | 98b19252 | Amit Shah | break;
|
71 | 98b19252 | Amit Shah | case CHR_EVENT_CLOSED:
|
72 | 98b19252 | Amit Shah | virtio_serial_close(&vcon->port); |
73 | 98b19252 | Amit Shah | break;
|
74 | 98b19252 | Amit Shah | } |
75 | 98b19252 | Amit Shah | } |
76 | 98b19252 | Amit Shah | |
77 | 7edfe652 | Markus Armbruster | static int virtconsole_initfn(VirtIOSerialPort *port) |
78 | 98b19252 | Amit Shah | { |
79 | 7edfe652 | Markus Armbruster | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
80 | a15bb0d6 | Markus Armbruster | VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev, |
81 | a15bb0d6 | Markus Armbruster | vcon->port.dev.info); |
82 | a15bb0d6 | Markus Armbruster | |
83 | 7edfe652 | Markus Armbruster | if (port->id == 0 && !info->is_console) { |
84 | 7edfe652 | Markus Armbruster | error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility.");
|
85 | 7edfe652 | Markus Armbruster | return -1; |
86 | 7edfe652 | Markus Armbruster | } |
87 | 7edfe652 | Markus Armbruster | |
88 | 98b19252 | Amit Shah | if (vcon->chr) {
|
89 | 98b19252 | Amit Shah | qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event, |
90 | 98b19252 | Amit Shah | vcon); |
91 | a15bb0d6 | Markus Armbruster | info->have_data = flush_buf; |
92 | a15bb0d6 | Markus Armbruster | info->guest_open = guest_open; |
93 | a15bb0d6 | Markus Armbruster | info->guest_close = guest_close; |
94 | 98b19252 | Amit Shah | } |
95 | cbe77b61 | Amit Shah | |
96 | 7edfe652 | Markus Armbruster | return 0; |
97 | cbe77b61 | Amit Shah | } |
98 | cbe77b61 | Amit Shah | |
99 | a43f9c90 | Gerd Hoffmann | static int virtconsole_exitfn(VirtIOSerialPort *port) |
100 | 98b19252 | Amit Shah | { |
101 | 98b19252 | Amit Shah | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
102 | 98b19252 | Amit Shah | |
103 | 98b19252 | Amit Shah | if (vcon->chr) {
|
104 | f9a90f18 | Amit Shah | /*
|
105 | f9a90f18 | Amit Shah | * Instead of closing the chardev, free it so it can be used
|
106 | f9a90f18 | Amit Shah | * for other purposes.
|
107 | f9a90f18 | Amit Shah | */
|
108 | f9a90f18 | Amit Shah | qemu_chr_add_handlers(vcon->chr, NULL, NULL, NULL, NULL); |
109 | 98b19252 | Amit Shah | } |
110 | 98b19252 | Amit Shah | |
111 | 98b19252 | Amit Shah | return 0; |
112 | 98b19252 | Amit Shah | } |
113 | 98b19252 | Amit Shah | |
114 | 98b19252 | Amit Shah | static VirtIOSerialPortInfo virtconsole_info = {
|
115 | 98b19252 | Amit Shah | .qdev.name = "virtconsole",
|
116 | 98b19252 | Amit Shah | .qdev.size = sizeof(VirtConsole),
|
117 | 2a3d57ce | Markus Armbruster | .is_console = true,
|
118 | 98b19252 | Amit Shah | .init = virtconsole_initfn, |
119 | 98b19252 | Amit Shah | .exit = virtconsole_exitfn, |
120 | 98b19252 | Amit Shah | .qdev.props = (Property[]) { |
121 | 055b889f | Amit Shah | DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
|
122 | 98b19252 | Amit Shah | DEFINE_PROP_CHR("chardev", VirtConsole, chr),
|
123 | 160600fd | Amit Shah | DEFINE_PROP_STRING("name", VirtConsole, port.name),
|
124 | 98b19252 | Amit Shah | DEFINE_PROP_END_OF_LIST(), |
125 | 98b19252 | Amit Shah | }, |
126 | 98b19252 | Amit Shah | }; |
127 | 98b19252 | Amit Shah | |
128 | 98b19252 | Amit Shah | static void virtconsole_register(void) |
129 | 98b19252 | Amit Shah | { |
130 | 98b19252 | Amit Shah | virtio_serial_port_qdev_register(&virtconsole_info); |
131 | 98b19252 | Amit Shah | } |
132 | 98b19252 | Amit Shah | device_init(virtconsole_register) |
133 | b60c470b | Amit Shah | |
134 | b60c470b | Amit Shah | static VirtIOSerialPortInfo virtserialport_info = {
|
135 | b60c470b | Amit Shah | .qdev.name = "virtserialport",
|
136 | b60c470b | Amit Shah | .qdev.size = sizeof(VirtConsole),
|
137 | 7edfe652 | Markus Armbruster | .init = virtconsole_initfn, |
138 | b60c470b | Amit Shah | .exit = virtconsole_exitfn, |
139 | b60c470b | Amit Shah | .qdev.props = (Property[]) { |
140 | 055b889f | Amit Shah | DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
|
141 | b60c470b | Amit Shah | DEFINE_PROP_CHR("chardev", VirtConsole, chr),
|
142 | b60c470b | Amit Shah | DEFINE_PROP_STRING("name", VirtConsole, port.name),
|
143 | b60c470b | Amit Shah | DEFINE_PROP_END_OF_LIST(), |
144 | b60c470b | Amit Shah | }, |
145 | b60c470b | Amit Shah | }; |
146 | b60c470b | Amit Shah | |
147 | b60c470b | Amit Shah | static void virtserialport_register(void) |
148 | b60c470b | Amit Shah | { |
149 | b60c470b | Amit Shah | virtio_serial_port_qdev_register(&virtserialport_info); |
150 | b60c470b | Amit Shah | } |
151 | b60c470b | Amit Shah | device_init(virtserialport_register) |