blob: 21f752b72936c7828a80b8c657801e023c047ada [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 Shahd02e4fa2011-07-05 16:37:49 +053015#include "trace.h"
Amit Shah98b19252010-01-20 00:36:52 +053016#include "virtio-serial.h"
17
18typedef struct VirtConsole {
19 VirtIOSerialPort port;
20 CharDriverState *chr;
21} VirtConsole;
22
23
24/* Callback function that's called when the guest sends us data */
Amit Shahe300ac22010-12-13 17:50:07 +053025static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
Amit Shah98b19252010-01-20 00:36:52 +053026{
27 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
Amit Shahd02e4fa2011-07-05 16:37:49 +053028 ssize_t ret;
Amit Shah98b19252010-01-20 00:36:52 +053029
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -050030 ret = qemu_chr_fe_write(vcon->chr, buf, len);
Amit Shahd02e4fa2011-07-05 16:37:49 +053031 trace_virtio_console_flush_buf(port->id, len, ret);
Amit Shah0219d732011-07-07 17:35:27 +053032
33 if (ret < 0) {
34 /*
35 * Ideally we'd get a better error code than just -1, but
36 * that's what the chardev interface gives us right now. If
37 * we had a finer-grained message, like -EPIPE, we could close
38 * this connection. Absent such error messages, the most we
39 * can do is to return 0 here.
40 *
41 * This will prevent stray -1 values to go to
42 * virtio-serial-bus.c and cause abort()s in
43 * do_flush_queued_data().
44 */
45 ret = 0;
46 }
Amit Shahd02e4fa2011-07-05 16:37:49 +053047 return ret;
Amit Shah98b19252010-01-20 00:36:52 +053048}
49
Hans de Goede0b6d2262011-03-24 11:12:03 +010050/* Callback function that's called when the guest opens the port */
51static void guest_open(VirtIOSerialPort *port)
52{
53 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
54
Anthony Liguoric9d830e2011-08-15 11:17:32 -050055 qemu_chr_fe_open(vcon->chr);
Hans de Goede0b6d2262011-03-24 11:12:03 +010056}
57
58/* Callback function that's called when the guest closes the port */
59static void guest_close(VirtIOSerialPort *port)
60{
61 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
62
63 qemu_chr_guest_close(vcon->chr);
64}
65
Amit Shah98b19252010-01-20 00:36:52 +053066/* Readiness of the guest to accept data on a port */
67static int chr_can_read(void *opaque)
68{
69 VirtConsole *vcon = opaque;
70
71 return virtio_serial_guest_ready(&vcon->port);
72}
73
74/* Send data from a char device over to the guest */
75static void chr_read(void *opaque, const uint8_t *buf, int size)
76{
77 VirtConsole *vcon = opaque;
78
Amit Shahd02e4fa2011-07-05 16:37:49 +053079 trace_virtio_console_chr_read(vcon->port.id, size);
Amit Shah98b19252010-01-20 00:36:52 +053080 virtio_serial_write(&vcon->port, buf, size);
81}
82
83static void chr_event(void *opaque, int event)
84{
85 VirtConsole *vcon = opaque;
86
Amit Shahd02e4fa2011-07-05 16:37:49 +053087 trace_virtio_console_chr_event(vcon->port.id, event);
Amit Shah98b19252010-01-20 00:36:52 +053088 switch (event) {
Amit Shah28eaf462010-12-10 17:10:43 +053089 case CHR_EVENT_OPENED:
Amit Shah98b19252010-01-20 00:36:52 +053090 virtio_serial_open(&vcon->port);
91 break;
Amit Shah98b19252010-01-20 00:36:52 +053092 case CHR_EVENT_CLOSED:
93 virtio_serial_close(&vcon->port);
94 break;
95 }
96}
97
Markus Armbruster7edfe652011-05-25 14:21:14 +020098static int virtconsole_initfn(VirtIOSerialPort *port)
Amit Shahcbe77b62010-04-05 14:20:29 +053099{
Markus Armbruster7edfe652011-05-25 14:21:14 +0200100 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
Markus Armbrustera15bb0d2011-05-25 14:21:13 +0200101 VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev,
102 vcon->port.dev.info);
103
Markus Armbruster7edfe652011-05-25 14:21:14 +0200104 if (port->id == 0 && !info->is_console) {
105 error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility.");
106 return -1;
107 }
108
Amit Shahcbe77b62010-04-05 14:20:29 +0530109 if (vcon->chr) {
110 qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
111 vcon);
Markus Armbrustera15bb0d2011-05-25 14:21:13 +0200112 info->have_data = flush_buf;
113 info->guest_open = guest_open;
114 info->guest_close = guest_close;
Amit Shahcbe77b62010-04-05 14:20:29 +0530115 }
Markus Armbruster7edfe652011-05-25 14:21:14 +0200116
Amit Shahcbe77b62010-04-05 14:20:29 +0530117 return 0;
118}
119
Gerd Hoffmanna43f9c92011-02-24 11:14:12 +0530120static int virtconsole_exitfn(VirtIOSerialPort *port)
Amit Shah98b19252010-01-20 00:36:52 +0530121{
Amit Shah98b19252010-01-20 00:36:52 +0530122 VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
123
124 if (vcon->chr) {
Amit Shahf9a90f12011-03-15 14:13:09 +0530125 /*
126 * Instead of closing the chardev, free it so it can be used
127 * for other purposes.
128 */
129 qemu_chr_add_handlers(vcon->chr, NULL, NULL, NULL, NULL);
Amit Shah98b19252010-01-20 00:36:52 +0530130 }
131
132 return 0;
133}
134
135static VirtIOSerialPortInfo virtconsole_info = {
136 .qdev.name = "virtconsole",
137 .qdev.size = sizeof(VirtConsole),
Markus Armbruster2a3d57c2011-05-25 14:21:11 +0200138 .is_console = true,
Amit Shah98b19252010-01-20 00:36:52 +0530139 .init = virtconsole_initfn,
140 .exit = virtconsole_exitfn,
141 .qdev.props = (Property[]) {
Amit Shah98b19252010-01-20 00:36:52 +0530142 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
Amit Shah98b19252010-01-20 00:36:52 +0530143 DEFINE_PROP_END_OF_LIST(),
144 },
145};
146
147static void virtconsole_register(void)
148{
149 virtio_serial_port_qdev_register(&virtconsole_info);
150}
151device_init(virtconsole_register)
Amit Shahb60c4702010-01-20 00:36:56 +0530152
Amit Shahb60c4702010-01-20 00:36:56 +0530153static VirtIOSerialPortInfo virtserialport_info = {
154 .qdev.name = "virtserialport",
155 .qdev.size = sizeof(VirtConsole),
Markus Armbruster7edfe652011-05-25 14:21:14 +0200156 .init = virtconsole_initfn,
Amit Shahb60c4702010-01-20 00:36:56 +0530157 .exit = virtconsole_exitfn,
158 .qdev.props = (Property[]) {
159 DEFINE_PROP_CHR("chardev", VirtConsole, chr),
Amit Shahb60c4702010-01-20 00:36:56 +0530160 DEFINE_PROP_END_OF_LIST(),
161 },
162};
163
164static void virtserialport_register(void)
165{
166 virtio_serial_port_qdev_register(&virtserialport_info);
167}
168device_init(virtserialport_register)