]> git.proxmox.com Git - qemu.git/blob - hw/virtio-console.c
virtio-console: Rename virtio-serial.c back to virtio-console.c
[qemu.git] / hw / virtio-console.c
1 /*
2 * Virtio Console and Generic Serial Port Devices
3 *
4 * Copyright Red Hat, Inc. 2009
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"
14 #include "virtio-serial.h"
15
16 typedef struct VirtConsole {
17 VirtIOSerialPort port;
18 CharDriverState *chr;
19 } VirtConsole;
20
21
22 /* Callback function that's called when the guest sends us data */
23 static size_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
24 {
25 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
26 ssize_t ret;
27
28 ret = qemu_chr_write(vcon->chr, buf, len);
29
30 return ret < 0 ? 0 : ret;
31 }
32
33 /* Readiness of the guest to accept data on a port */
34 static int chr_can_read(void *opaque)
35 {
36 VirtConsole *vcon = opaque;
37
38 return virtio_serial_guest_ready(&vcon->port);
39 }
40
41 /* Send data from a char device over to the guest */
42 static void chr_read(void *opaque, const uint8_t *buf, int size)
43 {
44 VirtConsole *vcon = opaque;
45
46 virtio_serial_write(&vcon->port, buf, size);
47 }
48
49 static void chr_event(void *opaque, int event)
50 {
51 VirtConsole *vcon = opaque;
52
53 switch (event) {
54 case CHR_EVENT_OPENED: {
55 virtio_serial_open(&vcon->port);
56 break;
57 }
58 case CHR_EVENT_CLOSED:
59 virtio_serial_close(&vcon->port);
60 break;
61 }
62 }
63
64 /* Virtio Console Ports */
65 static int virtconsole_initfn(VirtIOSerialDevice *dev)
66 {
67 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
68 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
69
70 port->info = dev->info;
71
72 port->is_console = true;
73
74 if (vcon->chr) {
75 qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
76 vcon);
77 port->info->have_data = flush_buf;
78 }
79 return 0;
80 }
81
82 static int virtconsole_exitfn(VirtIOSerialDevice *dev)
83 {
84 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
85 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
86
87 if (vcon->chr) {
88 port->info->have_data = NULL;
89 qemu_chr_close(vcon->chr);
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),
102 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
103 DEFINE_PROP_STRING("name", VirtConsole, port.name),
104 DEFINE_PROP_END_OF_LIST(),
105 },
106 };
107
108 static void virtconsole_register(void)
109 {
110 virtio_serial_port_qdev_register(&virtconsole_info);
111 }
112 device_init(virtconsole_register)
113
114 /* Generic Virtio Serial Ports */
115 static int virtserialport_initfn(VirtIOSerialDevice *dev)
116 {
117 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
118 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
119
120 port->info = dev->info;
121
122 if (vcon->chr) {
123 qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
124 vcon);
125 port->info->have_data = flush_buf;
126 }
127 return 0;
128 }
129
130 static VirtIOSerialPortInfo virtserialport_info = {
131 .qdev.name = "virtserialport",
132 .qdev.size = sizeof(VirtConsole),
133 .init = virtserialport_initfn,
134 .exit = virtconsole_exitfn,
135 .qdev.props = (Property[]) {
136 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
137 DEFINE_PROP_STRING("name", VirtConsole, port.name),
138 DEFINE_PROP_END_OF_LIST(),
139 },
140 };
141
142 static void virtserialport_register(void)
143 {
144 virtio_serial_port_qdev_register(&virtserialport_info);
145 }
146 device_init(virtserialport_register)