blob: de539c4eacfb252c6d738998f8c838094a01965f [file] [log] [blame]
Amit Shah98b19252010-01-20 00:36:52 +05301/*
2 * Virtio Console and Generic Serial Port Devices
3 *
Amit Shah71c092e2010-04-27 18:04:02 +05304 * Copyright Red Hat, Inc. 2009, 2010
Amit Shah98b19252010-01-20 00:36:52 +05305 *
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"
Amit Shah0b8b7162011-02-03 13:05:07 +053014#include "qemu-error.h"
Amit Shah98b19252010-01-20 00:36:52 +053015#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 */
Amit Shahe300ac22010-12-13 17:50:07 +053024static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
Amit Shah98b19252010-01-20 00:36:52 +053025{
26 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
Amit Shah98b19252010-01-20 00:36:52 +053027
Amit Shahe300ac22010-12-13 17:50:07 +053028 return qemu_chr_write(vcon->chr, buf, len);
Amit Shah98b19252010-01-20 00:36:52 +053029}
30
Hans de Goede0b6d2262011-03-24 11:12:03 +010031/* 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
Amit Shah98b19252010-01-20 00:36:52 +053047/* 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) {
Amit Shah28eaf462010-12-10 17:10:43 +053068 case CHR_EVENT_OPENED:
Amit Shah98b19252010-01-20 00:36:52 +053069 virtio_serial_open(&vcon->port);
70 break;
Amit Shah98b19252010-01-20 00:36:52 +053071 case CHR_EVENT_CLOSED:
72 virtio_serial_close(&vcon->port);
73 break;
74 }
75}
76
Gerd Hoffmanna43f9c92011-02-24 11:14:12 +053077static int generic_port_init(VirtConsole *vcon, VirtIOSerialPort *port)
Amit Shahcbe77b62010-04-05 14:20:29 +053078{
Amit Shahcbe77b62010-04-05 14:20:29 +053079 if (vcon->chr) {
80 qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
81 vcon);
82 vcon->port.info->have_data = flush_buf;
Hans de Goede0b6d2262011-03-24 11:12:03 +010083 vcon->port.info->guest_open = guest_open;
84 vcon->port.info->guest_close = guest_close;
Amit Shahcbe77b62010-04-05 14:20:29 +053085 }
86 return 0;
87}
88
Amit Shah98b19252010-01-20 00:36:52 +053089/* Virtio Console Ports */
Gerd Hoffmanna43f9c92011-02-24 11:14:12 +053090static int virtconsole_initfn(VirtIOSerialPort *port)
Amit Shah98b19252010-01-20 00:36:52 +053091{
Amit Shah98b19252010-01-20 00:36:52 +053092 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
93
Amit Shah98b19252010-01-20 00:36:52 +053094 port->is_console = true;
Gerd Hoffmanna43f9c92011-02-24 11:14:12 +053095 return generic_port_init(vcon, port);
Amit Shah98b19252010-01-20 00:36:52 +053096}
97
Gerd Hoffmanna43f9c92011-02-24 11:14:12 +053098static int virtconsole_exitfn(VirtIOSerialPort *port)
Amit Shah98b19252010-01-20 00:36:52 +053099{
Amit Shah98b19252010-01-20 00:36:52 +0530100 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
101
102 if (vcon->chr) {
Amit Shahf9a90f12011-03-15 14:13:09 +0530103 /*
104 * Instead of closing the chardev, free it so it can be used
105 * for other purposes.
106 */
107 qemu_chr_add_handlers(vcon->chr, NULL, NULL, NULL, NULL);
Amit Shah98b19252010-01-20 00:36:52 +0530108 }
109
110 return 0;
111}
112
113static VirtIOSerialPortInfo virtconsole_info = {
114 .qdev.name = "virtconsole",
115 .qdev.size = sizeof(VirtConsole),
116 .init = virtconsole_initfn,
117 .exit = virtconsole_exitfn,
118 .qdev.props = (Property[]) {
119 DEFINE_PROP_UINT8("is_console", VirtConsole, port.is_console, 1),
Amit Shah055b8892010-04-27 18:03:59 +0530120 DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
Amit Shah98b19252010-01-20 00:36:52 +0530121 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
Amit Shah160600f2010-01-20 00:36:54 +0530122 DEFINE_PROP_STRING("name", VirtConsole, port.name),
Amit Shah98b19252010-01-20 00:36:52 +0530123 DEFINE_PROP_END_OF_LIST(),
124 },
125};
126
127static void virtconsole_register(void)
128{
129 virtio_serial_port_qdev_register(&virtconsole_info);
130}
131device_init(virtconsole_register)
Amit Shahb60c4702010-01-20 00:36:56 +0530132
133/* Generic Virtio Serial Ports */
Gerd Hoffmanna43f9c92011-02-24 11:14:12 +0530134static int virtserialport_initfn(VirtIOSerialPort *port)
Amit Shahb60c4702010-01-20 00:36:56 +0530135{
Amit Shahb60c4702010-01-20 00:36:56 +0530136 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
137
Amit Shah0b8b7162011-02-03 13:05:07 +0530138 if (port->id == 0) {
139 /*
140 * Disallow a generic port at id 0, that's reserved for
141 * console ports.
142 */
143 error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility.");
144 return -1;
145 }
Gerd Hoffmanna43f9c92011-02-24 11:14:12 +0530146 return generic_port_init(vcon, port);
Amit Shahb60c4702010-01-20 00:36:56 +0530147}
148
149static VirtIOSerialPortInfo virtserialport_info = {
150 .qdev.name = "virtserialport",
151 .qdev.size = sizeof(VirtConsole),
152 .init = virtserialport_initfn,
153 .exit = virtconsole_exitfn,
154 .qdev.props = (Property[]) {
Amit Shah055b8892010-04-27 18:03:59 +0530155 DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
Amit Shahb60c4702010-01-20 00:36:56 +0530156 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
157 DEFINE_PROP_STRING("name", VirtConsole, port.name),
158 DEFINE_PROP_END_OF_LIST(),
159 },
160};
161
162static void virtserialport_register(void)
163{
164 virtio_serial_port_qdev_register(&virtserialport_info);
165}
166device_init(virtserialport_register)