qxl-logger: add timestamp to command log
[qemu.git] / hw / virtio-console.c
blobb076331d379ec60c8be9f0063cd27fda90d2e581
1 /*
2 * Virtio Console and Generic Serial Port Devices
4 * Copyright Red Hat, Inc. 2009, 2010
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 "qemu-error.h"
15 #include "virtio-serial.h"
17 typedef struct VirtConsole {
18 VirtIOSerialPort port;
19 CharDriverState *chr;
20 } VirtConsole;
23 /* Callback function that's called when the guest sends us data */
24 static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
26 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
28 return qemu_chr_write(vcon->chr, buf, len);
31 /* Callback function that's called when the guest opens the port */
32 static void guest_open(VirtIOSerialPort *port)
34 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
36 qemu_chr_guest_open(vcon->chr);
39 /* Callback function that's called when the guest closes the port */
40 static void guest_close(VirtIOSerialPort *port)
42 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
44 qemu_chr_guest_close(vcon->chr);
47 /* Readiness of the guest to accept data on a port */
48 static int chr_can_read(void *opaque)
50 VirtConsole *vcon = opaque;
52 return virtio_serial_guest_ready(&vcon->port);
55 /* Send data from a char device over to the guest */
56 static void chr_read(void *opaque, const uint8_t *buf, int size)
58 VirtConsole *vcon = opaque;
60 virtio_serial_write(&vcon->port, buf, size);
63 static void chr_event(void *opaque, int event)
65 VirtConsole *vcon = opaque;
67 switch (event) {
68 case CHR_EVENT_OPENED:
69 virtio_serial_open(&vcon->port);
70 break;
71 case CHR_EVENT_CLOSED:
72 virtio_serial_close(&vcon->port);
73 break;
77 static int virtconsole_initfn(VirtIOSerialPort *port)
79 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
80 VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev,
81 vcon->port.dev.info);
83 if (port->id == 0 && !info->is_console) {
84 error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility.");
85 return -1;
88 if (vcon->chr) {
89 qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
90 vcon);
91 info->have_data = flush_buf;
92 info->guest_open = guest_open;
93 info->guest_close = guest_close;
96 return 0;
99 static int virtconsole_exitfn(VirtIOSerialPort *port)
101 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
103 if (vcon->chr) {
105 * Instead of closing the chardev, free it so it can be used
106 * for other purposes.
108 qemu_chr_add_handlers(vcon->chr, NULL, NULL, NULL, NULL);
111 return 0;
114 static VirtIOSerialPortInfo virtconsole_info = {
115 .qdev.name = "virtconsole",
116 .qdev.size = sizeof(VirtConsole),
117 .is_console = true,
118 .init = virtconsole_initfn,
119 .exit = virtconsole_exitfn,
120 .qdev.props = (Property[]) {
121 DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
122 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
123 DEFINE_PROP_STRING("name", VirtConsole, port.name),
124 DEFINE_PROP_END_OF_LIST(),
128 static void virtconsole_register(void)
130 virtio_serial_port_qdev_register(&virtconsole_info);
132 device_init(virtconsole_register)
134 static VirtIOSerialPortInfo virtserialport_info = {
135 .qdev.name = "virtserialport",
136 .qdev.size = sizeof(VirtConsole),
137 .init = virtconsole_initfn,
138 .exit = virtconsole_exitfn,
139 .qdev.props = (Property[]) {
140 DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
141 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
142 DEFINE_PROP_STRING("name", VirtConsole, port.name),
143 DEFINE_PROP_END_OF_LIST(),
147 static void virtserialport_register(void)
149 virtio_serial_port_qdev_register(&virtserialport_info);
151 device_init(virtserialport_register)