root / hw / virtio-console.c @ efdef95f
History | View | Annotate | Download (3.9 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 | 98b19252 | Amit Shah | /* Readiness of the guest to accept data on a port */
|
32 | 98b19252 | Amit Shah | static int chr_can_read(void *opaque) |
33 | 98b19252 | Amit Shah | { |
34 | 98b19252 | Amit Shah | VirtConsole *vcon = opaque; |
35 | 98b19252 | Amit Shah | |
36 | 98b19252 | Amit Shah | return virtio_serial_guest_ready(&vcon->port);
|
37 | 98b19252 | Amit Shah | } |
38 | 98b19252 | Amit Shah | |
39 | 98b19252 | Amit Shah | /* Send data from a char device over to the guest */
|
40 | 98b19252 | Amit Shah | static void chr_read(void *opaque, const uint8_t *buf, int size) |
41 | 98b19252 | Amit Shah | { |
42 | 98b19252 | Amit Shah | VirtConsole *vcon = opaque; |
43 | 98b19252 | Amit Shah | |
44 | 98b19252 | Amit Shah | virtio_serial_write(&vcon->port, buf, size); |
45 | 98b19252 | Amit Shah | } |
46 | 98b19252 | Amit Shah | |
47 | 98b19252 | Amit Shah | static void chr_event(void *opaque, int event) |
48 | 98b19252 | Amit Shah | { |
49 | 98b19252 | Amit Shah | VirtConsole *vcon = opaque; |
50 | 98b19252 | Amit Shah | |
51 | 98b19252 | Amit Shah | switch (event) {
|
52 | 28eaf465 | Amit Shah | case CHR_EVENT_OPENED:
|
53 | 98b19252 | Amit Shah | virtio_serial_open(&vcon->port); |
54 | 98b19252 | Amit Shah | break;
|
55 | 98b19252 | Amit Shah | case CHR_EVENT_CLOSED:
|
56 | 98b19252 | Amit Shah | virtio_serial_close(&vcon->port); |
57 | 98b19252 | Amit Shah | break;
|
58 | 98b19252 | Amit Shah | } |
59 | 98b19252 | Amit Shah | } |
60 | 98b19252 | Amit Shah | |
61 | a43f9c90 | Gerd Hoffmann | static int generic_port_init(VirtConsole *vcon, VirtIOSerialPort *port) |
62 | 98b19252 | Amit Shah | { |
63 | 98b19252 | Amit Shah | if (vcon->chr) {
|
64 | 98b19252 | Amit Shah | qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event, |
65 | 98b19252 | Amit Shah | vcon); |
66 | cbe77b61 | Amit Shah | vcon->port.info->have_data = flush_buf; |
67 | 98b19252 | Amit Shah | } |
68 | 98b19252 | Amit Shah | return 0; |
69 | 98b19252 | Amit Shah | } |
70 | 98b19252 | Amit Shah | |
71 | cbe77b61 | Amit Shah | /* Virtio Console Ports */
|
72 | a43f9c90 | Gerd Hoffmann | static int virtconsole_initfn(VirtIOSerialPort *port) |
73 | cbe77b61 | Amit Shah | { |
74 | cbe77b61 | Amit Shah | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
75 | cbe77b61 | Amit Shah | |
76 | cbe77b61 | Amit Shah | port->is_console = true;
|
77 | a43f9c90 | Gerd Hoffmann | return generic_port_init(vcon, port);
|
78 | cbe77b61 | Amit Shah | } |
79 | cbe77b61 | Amit Shah | |
80 | a43f9c90 | Gerd Hoffmann | static int virtconsole_exitfn(VirtIOSerialPort *port) |
81 | 98b19252 | Amit Shah | { |
82 | 98b19252 | Amit Shah | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
83 | 98b19252 | Amit Shah | |
84 | 98b19252 | Amit Shah | if (vcon->chr) {
|
85 | f9a90f18 | Amit Shah | /*
|
86 | f9a90f18 | Amit Shah | * Instead of closing the chardev, free it so it can be used
|
87 | f9a90f18 | Amit Shah | * for other purposes.
|
88 | f9a90f18 | Amit Shah | */
|
89 | f9a90f18 | Amit Shah | qemu_chr_add_handlers(vcon->chr, NULL, NULL, NULL, NULL); |
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 | 055b889f | Amit Shah | DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
|
103 | 98b19252 | Amit Shah | DEFINE_PROP_CHR("chardev", VirtConsole, chr),
|
104 | 160600fd | Amit Shah | DEFINE_PROP_STRING("name", VirtConsole, port.name),
|
105 | 98b19252 | Amit Shah | DEFINE_PROP_END_OF_LIST(), |
106 | 98b19252 | Amit Shah | }, |
107 | 98b19252 | Amit Shah | }; |
108 | 98b19252 | Amit Shah | |
109 | 98b19252 | Amit Shah | static void virtconsole_register(void) |
110 | 98b19252 | Amit Shah | { |
111 | 98b19252 | Amit Shah | virtio_serial_port_qdev_register(&virtconsole_info); |
112 | 98b19252 | Amit Shah | } |
113 | 98b19252 | Amit Shah | device_init(virtconsole_register) |
114 | b60c470b | Amit Shah | |
115 | b60c470b | Amit Shah | /* Generic Virtio Serial Ports */
|
116 | a43f9c90 | Gerd Hoffmann | static int virtserialport_initfn(VirtIOSerialPort *port) |
117 | b60c470b | Amit Shah | { |
118 | b60c470b | Amit Shah | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
119 | b60c470b | Amit Shah | |
120 | 0b8b716d | Amit Shah | if (port->id == 0) { |
121 | 0b8b716d | Amit Shah | /*
|
122 | 0b8b716d | Amit Shah | * Disallow a generic port at id 0, that's reserved for
|
123 | 0b8b716d | Amit Shah | * console ports.
|
124 | 0b8b716d | Amit Shah | */
|
125 | 0b8b716d | Amit Shah | error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility.");
|
126 | 0b8b716d | Amit Shah | return -1; |
127 | 0b8b716d | Amit Shah | } |
128 | a43f9c90 | Gerd Hoffmann | return generic_port_init(vcon, port);
|
129 | b60c470b | Amit Shah | } |
130 | b60c470b | Amit Shah | |
131 | b60c470b | Amit Shah | static VirtIOSerialPortInfo virtserialport_info = {
|
132 | b60c470b | Amit Shah | .qdev.name = "virtserialport",
|
133 | b60c470b | Amit Shah | .qdev.size = sizeof(VirtConsole),
|
134 | b60c470b | Amit Shah | .init = virtserialport_initfn, |
135 | b60c470b | Amit Shah | .exit = virtconsole_exitfn, |
136 | b60c470b | Amit Shah | .qdev.props = (Property[]) { |
137 | 055b889f | Amit Shah | DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
|
138 | b60c470b | Amit Shah | DEFINE_PROP_CHR("chardev", VirtConsole, chr),
|
139 | b60c470b | Amit Shah | DEFINE_PROP_STRING("name", VirtConsole, port.name),
|
140 | b60c470b | Amit Shah | DEFINE_PROP_END_OF_LIST(), |
141 | b60c470b | Amit Shah | }, |
142 | b60c470b | Amit Shah | }; |
143 | b60c470b | Amit Shah | |
144 | b60c470b | Amit Shah | static void virtserialport_register(void) |
145 | b60c470b | Amit Shah | { |
146 | b60c470b | Amit Shah | virtio_serial_port_qdev_register(&virtserialport_info); |
147 | b60c470b | Amit Shah | } |
148 | b60c470b | Amit Shah | device_init(virtserialport_register) |