vhost: fix features ack
[qemu/aliguori-queue.git] / hw / virtio-console.c
blobbd44ec6b6e2d6b357f944e06913c4d83f4309369
1 /*
2 * Virtio Console and Generic Serial Port Devices
4 * Copyright Red Hat, Inc. 2009
6 * Authors:
7 * Amit Shah <amit.shah@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
13 #include "qemu-char.h"
14 #include "virtio-serial.h"
16 typedef struct VirtConsole {
17 VirtIOSerialPort port;
18 CharDriverState *chr;
19 } VirtConsole;
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)
25 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
26 ssize_t ret;
28 ret = qemu_chr_write(vcon->chr, buf, len);
30 return ret < 0 ? 0 : ret;
33 /* Readiness of the guest to accept data on a port */
34 static int chr_can_read(void *opaque)
36 VirtConsole *vcon = opaque;
38 return virtio_serial_guest_ready(&vcon->port);
41 /* Send data from a char device over to the guest */
42 static void chr_read(void *opaque, const uint8_t *buf, int size)
44 VirtConsole *vcon = opaque;
46 virtio_serial_write(&vcon->port, buf, size);
49 static void chr_event(void *opaque, int event)
51 VirtConsole *vcon = opaque;
53 switch (event) {
54 case CHR_EVENT_OPENED: {
55 virtio_serial_open(&vcon->port);
56 break;
58 case CHR_EVENT_CLOSED:
59 virtio_serial_close(&vcon->port);
60 break;
64 /* Virtio Console Ports */
65 static int virtconsole_initfn(VirtIOSerialDevice *dev)
67 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
68 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
70 port->info = dev->info;
72 port->is_console = true;
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;
79 return 0;
82 static int virtconsole_exitfn(VirtIOSerialDevice *dev)
84 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
85 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
87 if (vcon->chr) {
88 port->info->have_data = NULL;
89 qemu_chr_close(vcon->chr);
92 return 0;
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(),
108 static void virtconsole_register(void)
110 virtio_serial_port_qdev_register(&virtconsole_info);
112 device_init(virtconsole_register)
114 /* Generic Virtio Serial Ports */
115 static int virtserialport_initfn(VirtIOSerialDevice *dev)
117 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
118 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
120 port->info = dev->info;
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;
127 return 0;
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(),
142 static void virtserialport_register(void)
144 virtio_serial_port_qdev_register(&virtserialport_info);
146 device_init(virtserialport_register)