]> git.proxmox.com Git - mirror_qemu.git/blame - hw/virtio-console.c
virtio-serial: Drop useless property is_console
[mirror_qemu.git] / hw / virtio-console.c
CommitLineData
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
17typedef 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 24static 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
0b6d2266
HG
31/* Callback function that's called when the guest opens the port */
32static void guest_open(VirtIOSerialPort *port)
33{
34 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
35
36 qemu_chr_guest_open(vcon->chr);
37}
38
39/* Callback function that's called when the guest closes the port */
40static void guest_close(VirtIOSerialPort *port)
41{
42 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
43
44 qemu_chr_guest_close(vcon->chr);
45}
46
98b19252
AS
47/* Readiness of the guest to accept data on a port */
48static int chr_can_read(void *opaque)
49{
50 VirtConsole *vcon = opaque;
51
52 return virtio_serial_guest_ready(&vcon->port);
53}
54
55/* Send data from a char device over to the guest */
56static void chr_read(void *opaque, const uint8_t *buf, int size)
57{
58 VirtConsole *vcon = opaque;
59
60 virtio_serial_write(&vcon->port, buf, size);
61}
62
63static void chr_event(void *opaque, int event)
64{
65 VirtConsole *vcon = opaque;
66
67 switch (event) {
28eaf465 68 case CHR_EVENT_OPENED:
98b19252
AS
69 virtio_serial_open(&vcon->port);
70 break;
98b19252
AS
71 case CHR_EVENT_CLOSED:
72 virtio_serial_close(&vcon->port);
73 break;
74 }
75}
76
a43f9c90 77static int generic_port_init(VirtConsole *vcon, VirtIOSerialPort *port)
98b19252 78{
98b19252
AS
79 if (vcon->chr) {
80 qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
81 vcon);
cbe77b61 82 vcon->port.info->have_data = flush_buf;
0b6d2266
HG
83 vcon->port.info->guest_open = guest_open;
84 vcon->port.info->guest_close = guest_close;
98b19252
AS
85 }
86 return 0;
87}
88
cbe77b61 89/* Virtio Console Ports */
a43f9c90 90static int virtconsole_initfn(VirtIOSerialPort *port)
cbe77b61 91{
cbe77b61
AS
92 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
93
a43f9c90 94 return generic_port_init(vcon, port);
cbe77b61
AS
95}
96
a43f9c90 97static int virtconsole_exitfn(VirtIOSerialPort *port)
98b19252 98{
98b19252
AS
99 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
100
101 if (vcon->chr) {
f9a90f18
AS
102 /*
103 * Instead of closing the chardev, free it so it can be used
104 * for other purposes.
105 */
106 qemu_chr_add_handlers(vcon->chr, NULL, NULL, NULL, NULL);
98b19252
AS
107 }
108
109 return 0;
110}
111
112static VirtIOSerialPortInfo virtconsole_info = {
113 .qdev.name = "virtconsole",
114 .qdev.size = sizeof(VirtConsole),
2a3d57ce 115 .is_console = true,
98b19252
AS
116 .init = virtconsole_initfn,
117 .exit = virtconsole_exitfn,
118 .qdev.props = (Property[]) {
055b889f 119 DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
98b19252 120 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
160600fd 121 DEFINE_PROP_STRING("name", VirtConsole, port.name),
98b19252
AS
122 DEFINE_PROP_END_OF_LIST(),
123 },
124};
125
126static void virtconsole_register(void)
127{
128 virtio_serial_port_qdev_register(&virtconsole_info);
129}
130device_init(virtconsole_register)
b60c470b
AS
131
132/* Generic Virtio Serial Ports */
a43f9c90 133static int virtserialport_initfn(VirtIOSerialPort *port)
b60c470b 134{
b60c470b
AS
135 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
136
0b8b716d
AS
137 if (port->id == 0) {
138 /*
139 * Disallow a generic port at id 0, that's reserved for
140 * console ports.
141 */
142 error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility.");
143 return -1;
144 }
a43f9c90 145 return generic_port_init(vcon, port);
b60c470b
AS
146}
147
148static VirtIOSerialPortInfo virtserialport_info = {
149 .qdev.name = "virtserialport",
150 .qdev.size = sizeof(VirtConsole),
151 .init = virtserialport_initfn,
152 .exit = virtconsole_exitfn,
153 .qdev.props = (Property[]) {
055b889f 154 DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
b60c470b
AS
155 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
156 DEFINE_PROP_STRING("name", VirtConsole, port.name),
157 DEFINE_PROP_END_OF_LIST(),
158 },
159};
160
161static void virtserialport_register(void)
162{
163 virtio_serial_port_qdev_register(&virtserialport_info);
164}
165device_init(virtserialport_register)