]>
Commit | Line | Data |
---|---|---|
98b19252 AS |
1 | /* |
2 | * Virtio Console and Generic Serial Port Devices | |
3 | * | |
71c092e9 | 4 | * Copyright Red Hat, Inc. 2009, 2010 |
98b19252 AS |
5 | * |
6 | * Authors: | |
7 | * Amit Shah <amit.shah@redhat.com> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
10 | * the COPYING file in the top-level directory. | |
11 | */ | |
12 | ||
13 | #include "qemu-char.h" | |
0b8b716d | 14 | #include "qemu-error.h" |
98b19252 AS |
15 | #include "virtio-serial.h" |
16 | ||
17 | typedef struct VirtConsole { | |
18 | VirtIOSerialPort port; | |
19 | CharDriverState *chr; | |
20 | } VirtConsole; | |
21 | ||
22 | ||
23 | /* Callback function that's called when the guest sends us data */ | |
e300ac27 | 24 | static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len) |
98b19252 AS |
25 | { |
26 | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); | |
98b19252 | 27 | |
e300ac27 | 28 | return qemu_chr_write(vcon->chr, buf, len); |
98b19252 AS |
29 | } |
30 | ||
31 | /* Readiness of the guest to accept data on a port */ | |
32 | static int chr_can_read(void *opaque) | |
33 | { | |
34 | VirtConsole *vcon = opaque; | |
35 | ||
36 | return virtio_serial_guest_ready(&vcon->port); | |
37 | } | |
38 | ||
39 | /* Send data from a char device over to the guest */ | |
40 | static void chr_read(void *opaque, const uint8_t *buf, int size) | |
41 | { | |
42 | VirtConsole *vcon = opaque; | |
43 | ||
44 | virtio_serial_write(&vcon->port, buf, size); | |
45 | } | |
46 | ||
47 | static void chr_event(void *opaque, int event) | |
48 | { | |
49 | VirtConsole *vcon = opaque; | |
50 | ||
51 | switch (event) { | |
28eaf465 | 52 | case CHR_EVENT_OPENED: |
98b19252 AS |
53 | virtio_serial_open(&vcon->port); |
54 | break; | |
98b19252 AS |
55 | case CHR_EVENT_CLOSED: |
56 | virtio_serial_close(&vcon->port); | |
57 | break; | |
58 | } | |
59 | } | |
60 | ||
a43f9c90 | 61 | static int generic_port_init(VirtConsole *vcon, VirtIOSerialPort *port) |
98b19252 | 62 | { |
98b19252 AS |
63 | if (vcon->chr) { |
64 | qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event, | |
65 | vcon); | |
cbe77b61 | 66 | vcon->port.info->have_data = flush_buf; |
98b19252 AS |
67 | } |
68 | return 0; | |
69 | } | |
70 | ||
cbe77b61 | 71 | /* Virtio Console Ports */ |
a43f9c90 | 72 | static int virtconsole_initfn(VirtIOSerialPort *port) |
cbe77b61 | 73 | { |
cbe77b61 AS |
74 | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
75 | ||
76 | port->is_console = true; | |
a43f9c90 | 77 | return generic_port_init(vcon, port); |
cbe77b61 AS |
78 | } |
79 | ||
a43f9c90 | 80 | static int virtconsole_exitfn(VirtIOSerialPort *port) |
98b19252 | 81 | { |
98b19252 AS |
82 | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
83 | ||
84 | if (vcon->chr) { | |
f9a90f18 AS |
85 | /* |
86 | * Instead of closing the chardev, free it so it can be used | |
87 | * for other purposes. | |
88 | */ | |
89 | qemu_chr_add_handlers(vcon->chr, NULL, NULL, NULL, NULL); | |
98b19252 AS |
90 | } |
91 | ||
92 | return 0; | |
93 | } | |
94 | ||
95 | static VirtIOSerialPortInfo virtconsole_info = { | |
96 | .qdev.name = "virtconsole", | |
97 | .qdev.size = sizeof(VirtConsole), | |
98 | .init = virtconsole_initfn, | |
99 | .exit = virtconsole_exitfn, | |
100 | .qdev.props = (Property[]) { | |
101 | DEFINE_PROP_UINT8("is_console", VirtConsole, port.is_console, 1), | |
055b889f | 102 | DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID), |
98b19252 | 103 | DEFINE_PROP_CHR("chardev", VirtConsole, chr), |
160600fd | 104 | DEFINE_PROP_STRING("name", VirtConsole, port.name), |
98b19252 AS |
105 | DEFINE_PROP_END_OF_LIST(), |
106 | }, | |
107 | }; | |
108 | ||
109 | static void virtconsole_register(void) | |
110 | { | |
111 | virtio_serial_port_qdev_register(&virtconsole_info); | |
112 | } | |
113 | device_init(virtconsole_register) | |
b60c470b AS |
114 | |
115 | /* Generic Virtio Serial Ports */ | |
a43f9c90 | 116 | static int virtserialport_initfn(VirtIOSerialPort *port) |
b60c470b | 117 | { |
b60c470b AS |
118 | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
119 | ||
0b8b716d AS |
120 | if (port->id == 0) { |
121 | /* | |
122 | * Disallow a generic port at id 0, that's reserved for | |
123 | * console ports. | |
124 | */ | |
125 | error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility."); | |
126 | return -1; | |
127 | } | |
a43f9c90 | 128 | return generic_port_init(vcon, port); |
b60c470b AS |
129 | } |
130 | ||
131 | static VirtIOSerialPortInfo virtserialport_info = { | |
132 | .qdev.name = "virtserialport", | |
133 | .qdev.size = sizeof(VirtConsole), | |
134 | .init = virtserialport_initfn, | |
135 | .exit = virtconsole_exitfn, | |
136 | .qdev.props = (Property[]) { | |
055b889f | 137 | DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID), |
b60c470b AS |
138 | DEFINE_PROP_CHR("chardev", VirtConsole, chr), |
139 | DEFINE_PROP_STRING("name", VirtConsole, port.name), | |
140 | DEFINE_PROP_END_OF_LIST(), | |
141 | }, | |
142 | }; | |
143 | ||
144 | static void virtserialport_register(void) | |
145 | { | |
146 | virtio_serial_port_qdev_register(&virtserialport_info); | |
147 | } | |
148 | device_init(virtserialport_register) |