root / hw / virtio-console.c @ e6e055c9
History | View | Annotate | Download (3.8 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 | 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 | 1e4476aa | Amit Shah | static void 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 | |
27 | 1e4476aa | Amit Shah | qemu_chr_write(vcon->chr, buf, len); |
28 | 98b19252 | Amit Shah | } |
29 | 98b19252 | Amit Shah | |
30 | 98b19252 | Amit Shah | /* Readiness of the guest to accept data on a port */
|
31 | 98b19252 | Amit Shah | static int chr_can_read(void *opaque) |
32 | 98b19252 | Amit Shah | { |
33 | 98b19252 | Amit Shah | VirtConsole *vcon = opaque; |
34 | 98b19252 | Amit Shah | |
35 | 98b19252 | Amit Shah | return virtio_serial_guest_ready(&vcon->port);
|
36 | 98b19252 | Amit Shah | } |
37 | 98b19252 | Amit Shah | |
38 | 98b19252 | Amit Shah | /* Send data from a char device over to the guest */
|
39 | 98b19252 | Amit Shah | static void chr_read(void *opaque, const uint8_t *buf, int size) |
40 | 98b19252 | Amit Shah | { |
41 | 98b19252 | Amit Shah | VirtConsole *vcon = opaque; |
42 | 98b19252 | Amit Shah | |
43 | 98b19252 | Amit Shah | virtio_serial_write(&vcon->port, buf, size); |
44 | 98b19252 | Amit Shah | } |
45 | 98b19252 | Amit Shah | |
46 | 98b19252 | Amit Shah | static void chr_event(void *opaque, int event) |
47 | 98b19252 | Amit Shah | { |
48 | 98b19252 | Amit Shah | VirtConsole *vcon = opaque; |
49 | 98b19252 | Amit Shah | |
50 | 98b19252 | Amit Shah | switch (event) {
|
51 | 98b19252 | Amit Shah | case CHR_EVENT_OPENED: {
|
52 | 98b19252 | Amit Shah | virtio_serial_open(&vcon->port); |
53 | 98b19252 | Amit Shah | break;
|
54 | 98b19252 | Amit Shah | } |
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 | 98b19252 | Amit Shah | /* Virtio Console Ports */
|
62 | 98b19252 | Amit Shah | static int virtconsole_initfn(VirtIOSerialDevice *dev) |
63 | 98b19252 | Amit Shah | { |
64 | 98b19252 | Amit Shah | VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev); |
65 | 98b19252 | Amit Shah | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
66 | 98b19252 | Amit Shah | |
67 | 98b19252 | Amit Shah | port->info = dev->info; |
68 | 98b19252 | Amit Shah | |
69 | 98b19252 | Amit Shah | port->is_console = true;
|
70 | 98b19252 | Amit Shah | |
71 | 98b19252 | Amit Shah | if (vcon->chr) {
|
72 | 98b19252 | Amit Shah | qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event, |
73 | 98b19252 | Amit Shah | vcon); |
74 | 98b19252 | Amit Shah | port->info->have_data = flush_buf; |
75 | 98b19252 | Amit Shah | } |
76 | 98b19252 | Amit Shah | return 0; |
77 | 98b19252 | Amit Shah | } |
78 | 98b19252 | Amit Shah | |
79 | 98b19252 | Amit Shah | static int virtconsole_exitfn(VirtIOSerialDevice *dev) |
80 | 98b19252 | Amit Shah | { |
81 | 98b19252 | Amit Shah | VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev); |
82 | 98b19252 | Amit Shah | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
83 | 98b19252 | Amit Shah | |
84 | 98b19252 | Amit Shah | if (vcon->chr) {
|
85 | 98b19252 | Amit Shah | port->info->have_data = NULL;
|
86 | 98b19252 | Amit Shah | qemu_chr_close(vcon->chr); |
87 | 98b19252 | Amit Shah | } |
88 | 98b19252 | Amit Shah | |
89 | 98b19252 | Amit Shah | return 0; |
90 | 98b19252 | Amit Shah | } |
91 | 98b19252 | Amit Shah | |
92 | 98b19252 | Amit Shah | static VirtIOSerialPortInfo virtconsole_info = {
|
93 | 98b19252 | Amit Shah | .qdev.name = "virtconsole",
|
94 | 98b19252 | Amit Shah | .qdev.size = sizeof(VirtConsole),
|
95 | 98b19252 | Amit Shah | .init = virtconsole_initfn, |
96 | 98b19252 | Amit Shah | .exit = virtconsole_exitfn, |
97 | 98b19252 | Amit Shah | .qdev.props = (Property[]) { |
98 | 98b19252 | Amit Shah | DEFINE_PROP_UINT8("is_console", VirtConsole, port.is_console, 1), |
99 | 055b889f | Amit Shah | DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
|
100 | 98b19252 | Amit Shah | DEFINE_PROP_CHR("chardev", VirtConsole, chr),
|
101 | 160600fd | Amit Shah | DEFINE_PROP_STRING("name", VirtConsole, port.name),
|
102 | 98b19252 | Amit Shah | DEFINE_PROP_END_OF_LIST(), |
103 | 98b19252 | Amit Shah | }, |
104 | 98b19252 | Amit Shah | }; |
105 | 98b19252 | Amit Shah | |
106 | 98b19252 | Amit Shah | static void virtconsole_register(void) |
107 | 98b19252 | Amit Shah | { |
108 | 98b19252 | Amit Shah | virtio_serial_port_qdev_register(&virtconsole_info); |
109 | 98b19252 | Amit Shah | } |
110 | 98b19252 | Amit Shah | device_init(virtconsole_register) |
111 | b60c470b | Amit Shah | |
112 | b60c470b | Amit Shah | /* Generic Virtio Serial Ports */
|
113 | b60c470b | Amit Shah | static int virtserialport_initfn(VirtIOSerialDevice *dev) |
114 | b60c470b | Amit Shah | { |
115 | b60c470b | Amit Shah | VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev); |
116 | b60c470b | Amit Shah | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
117 | b60c470b | Amit Shah | |
118 | b60c470b | Amit Shah | port->info = dev->info; |
119 | b60c470b | Amit Shah | |
120 | b60c470b | Amit Shah | if (vcon->chr) {
|
121 | b60c470b | Amit Shah | qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event, |
122 | b60c470b | Amit Shah | vcon); |
123 | b60c470b | Amit Shah | port->info->have_data = flush_buf; |
124 | b60c470b | Amit Shah | } |
125 | b60c470b | Amit Shah | return 0; |
126 | b60c470b | Amit Shah | } |
127 | b60c470b | Amit Shah | |
128 | b60c470b | Amit Shah | static VirtIOSerialPortInfo virtserialport_info = {
|
129 | b60c470b | Amit Shah | .qdev.name = "virtserialport",
|
130 | b60c470b | Amit Shah | .qdev.size = sizeof(VirtConsole),
|
131 | b60c470b | Amit Shah | .init = virtserialport_initfn, |
132 | b60c470b | Amit Shah | .exit = virtconsole_exitfn, |
133 | b60c470b | Amit Shah | .qdev.props = (Property[]) { |
134 | 055b889f | Amit Shah | DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
|
135 | b60c470b | Amit Shah | DEFINE_PROP_CHR("chardev", VirtConsole, chr),
|
136 | b60c470b | Amit Shah | DEFINE_PROP_STRING("name", VirtConsole, port.name),
|
137 | b60c470b | Amit Shah | DEFINE_PROP_END_OF_LIST(), |
138 | b60c470b | Amit Shah | }, |
139 | b60c470b | Amit Shah | }; |
140 | b60c470b | Amit Shah | |
141 | b60c470b | Amit Shah | static void virtserialport_register(void) |
142 | b60c470b | Amit Shah | { |
143 | b60c470b | Amit Shah | virtio_serial_port_qdev_register(&virtserialport_info); |
144 | b60c470b | Amit Shah | } |
145 | b60c470b | Amit Shah | device_init(virtserialport_register) |