root / hw / virtio-console.c @ a43f9c90
History | View | Annotate | Download (3.5 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 | e300ac27 | Amit Shah | static ssize_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 | |
27 | e300ac27 | Amit Shah | return 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 | 28eaf465 | Amit Shah | case CHR_EVENT_OPENED:
|
52 | 98b19252 | Amit Shah | virtio_serial_open(&vcon->port); |
53 | 98b19252 | Amit Shah | break;
|
54 | 98b19252 | Amit Shah | case CHR_EVENT_CLOSED:
|
55 | 98b19252 | Amit Shah | virtio_serial_close(&vcon->port); |
56 | 98b19252 | Amit Shah | break;
|
57 | 98b19252 | Amit Shah | } |
58 | 98b19252 | Amit Shah | } |
59 | 98b19252 | Amit Shah | |
60 | a43f9c90 | Gerd Hoffmann | static int generic_port_init(VirtConsole *vcon, VirtIOSerialPort *port) |
61 | 98b19252 | Amit Shah | { |
62 | 98b19252 | Amit Shah | if (vcon->chr) {
|
63 | 98b19252 | Amit Shah | qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event, |
64 | 98b19252 | Amit Shah | vcon); |
65 | cbe77b61 | Amit Shah | vcon->port.info->have_data = flush_buf; |
66 | 98b19252 | Amit Shah | } |
67 | 98b19252 | Amit Shah | return 0; |
68 | 98b19252 | Amit Shah | } |
69 | 98b19252 | Amit Shah | |
70 | cbe77b61 | Amit Shah | /* Virtio Console Ports */
|
71 | a43f9c90 | Gerd Hoffmann | static int virtconsole_initfn(VirtIOSerialPort *port) |
72 | cbe77b61 | Amit Shah | { |
73 | cbe77b61 | Amit Shah | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
74 | cbe77b61 | Amit Shah | |
75 | cbe77b61 | Amit Shah | port->is_console = true;
|
76 | a43f9c90 | Gerd Hoffmann | return generic_port_init(vcon, port);
|
77 | cbe77b61 | Amit Shah | } |
78 | cbe77b61 | Amit Shah | |
79 | a43f9c90 | Gerd Hoffmann | static int virtconsole_exitfn(VirtIOSerialPort *port) |
80 | 98b19252 | Amit Shah | { |
81 | 98b19252 | Amit Shah | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
82 | 98b19252 | Amit Shah | |
83 | 98b19252 | Amit Shah | if (vcon->chr) {
|
84 | 98b19252 | Amit Shah | port->info->have_data = NULL;
|
85 | 98b19252 | Amit Shah | qemu_chr_close(vcon->chr); |
86 | 98b19252 | Amit Shah | } |
87 | 98b19252 | Amit Shah | |
88 | 98b19252 | Amit Shah | return 0; |
89 | 98b19252 | Amit Shah | } |
90 | 98b19252 | Amit Shah | |
91 | 98b19252 | Amit Shah | static VirtIOSerialPortInfo virtconsole_info = {
|
92 | 98b19252 | Amit Shah | .qdev.name = "virtconsole",
|
93 | 98b19252 | Amit Shah | .qdev.size = sizeof(VirtConsole),
|
94 | 98b19252 | Amit Shah | .init = virtconsole_initfn, |
95 | 98b19252 | Amit Shah | .exit = virtconsole_exitfn, |
96 | 98b19252 | Amit Shah | .qdev.props = (Property[]) { |
97 | 98b19252 | Amit Shah | DEFINE_PROP_UINT8("is_console", VirtConsole, port.is_console, 1), |
98 | 055b889f | Amit Shah | DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
|
99 | 98b19252 | Amit Shah | DEFINE_PROP_CHR("chardev", VirtConsole, chr),
|
100 | 160600fd | Amit Shah | DEFINE_PROP_STRING("name", VirtConsole, port.name),
|
101 | 98b19252 | Amit Shah | DEFINE_PROP_END_OF_LIST(), |
102 | 98b19252 | Amit Shah | }, |
103 | 98b19252 | Amit Shah | }; |
104 | 98b19252 | Amit Shah | |
105 | 98b19252 | Amit Shah | static void virtconsole_register(void) |
106 | 98b19252 | Amit Shah | { |
107 | 98b19252 | Amit Shah | virtio_serial_port_qdev_register(&virtconsole_info); |
108 | 98b19252 | Amit Shah | } |
109 | 98b19252 | Amit Shah | device_init(virtconsole_register) |
110 | b60c470b | Amit Shah | |
111 | b60c470b | Amit Shah | /* Generic Virtio Serial Ports */
|
112 | a43f9c90 | Gerd Hoffmann | static int virtserialport_initfn(VirtIOSerialPort *port) |
113 | b60c470b | Amit Shah | { |
114 | b60c470b | Amit Shah | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
115 | b60c470b | Amit Shah | |
116 | a43f9c90 | Gerd Hoffmann | return generic_port_init(vcon, port);
|
117 | b60c470b | Amit Shah | } |
118 | b60c470b | Amit Shah | |
119 | b60c470b | Amit Shah | static VirtIOSerialPortInfo virtserialport_info = {
|
120 | b60c470b | Amit Shah | .qdev.name = "virtserialport",
|
121 | b60c470b | Amit Shah | .qdev.size = sizeof(VirtConsole),
|
122 | b60c470b | Amit Shah | .init = virtserialport_initfn, |
123 | b60c470b | Amit Shah | .exit = virtconsole_exitfn, |
124 | b60c470b | Amit Shah | .qdev.props = (Property[]) { |
125 | 055b889f | Amit Shah | DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
|
126 | b60c470b | Amit Shah | DEFINE_PROP_CHR("chardev", VirtConsole, chr),
|
127 | b60c470b | Amit Shah | DEFINE_PROP_STRING("name", VirtConsole, port.name),
|
128 | b60c470b | Amit Shah | DEFINE_PROP_END_OF_LIST(), |
129 | b60c470b | Amit Shah | }, |
130 | b60c470b | Amit Shah | }; |
131 | b60c470b | Amit Shah | |
132 | b60c470b | Amit Shah | static void virtserialport_register(void) |
133 | b60c470b | Amit Shah | { |
134 | b60c470b | Amit Shah | virtio_serial_port_qdev_register(&virtserialport_info); |
135 | b60c470b | Amit Shah | } |
136 | b60c470b | Amit Shah | device_init(virtserialport_register) |