Statistics
| Branch: | Revision:

root / hw / virtio-console.c @ 52ce6f05

History | View | Annotate | Download (4.5 kB)

1 98b19252 Amit Shah
/*
2 98b19252 Amit Shah
 * Virtio Console and Generic Serial Port Devices
3 98b19252 Amit Shah
 *
4 71c092e9 Amit Shah
 * Copyright Red Hat, Inc. 2009, 2010
5 98b19252 Amit Shah
 *
6 98b19252 Amit Shah
 * Authors:
7 98b19252 Amit Shah
 *  Amit Shah <amit.shah@redhat.com>
8 98b19252 Amit Shah
 *
9 98b19252 Amit Shah
 * This work is licensed under the terms of the GNU GPL, version 2.  See
10 98b19252 Amit Shah
 * the COPYING file in the top-level directory.
11 98b19252 Amit Shah
 */
12 98b19252 Amit Shah
13 98b19252 Amit Shah
#include "qemu-char.h"
14 0b8b716d Amit Shah
#include "qemu-error.h"
15 d02e4fa4 Amit Shah
#include "trace.h"
16 98b19252 Amit Shah
#include "virtio-serial.h"
17 98b19252 Amit Shah
18 98b19252 Amit Shah
typedef struct VirtConsole {
19 98b19252 Amit Shah
    VirtIOSerialPort port;
20 98b19252 Amit Shah
    CharDriverState *chr;
21 98b19252 Amit Shah
} VirtConsole;
22 98b19252 Amit Shah
23 98b19252 Amit Shah
24 98b19252 Amit Shah
/* Callback function that's called when the guest sends us data */
25 e300ac27 Amit Shah
static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
26 98b19252 Amit Shah
{
27 98b19252 Amit Shah
    VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
28 d02e4fa4 Amit Shah
    ssize_t ret;
29 98b19252 Amit Shah
30 2cc6e0a1 Anthony Liguori
    ret = qemu_chr_fe_write(vcon->chr, buf, len);
31 d02e4fa4 Amit Shah
    trace_virtio_console_flush_buf(port->id, len, ret);
32 0219d732 Amit Shah
33 0219d732 Amit Shah
    if (ret < 0) {
34 0219d732 Amit Shah
        /*
35 0219d732 Amit Shah
         * Ideally we'd get a better error code than just -1, but
36 0219d732 Amit Shah
         * that's what the chardev interface gives us right now.  If
37 0219d732 Amit Shah
         * we had a finer-grained message, like -EPIPE, we could close
38 0219d732 Amit Shah
         * this connection.  Absent such error messages, the most we
39 0219d732 Amit Shah
         * can do is to return 0 here.
40 0219d732 Amit Shah
         *
41 0219d732 Amit Shah
         * This will prevent stray -1 values to go to
42 0219d732 Amit Shah
         * virtio-serial-bus.c and cause abort()s in
43 0219d732 Amit Shah
         * do_flush_queued_data().
44 0219d732 Amit Shah
         */
45 0219d732 Amit Shah
        ret = 0;
46 0219d732 Amit Shah
    }
47 d02e4fa4 Amit Shah
    return ret;
48 98b19252 Amit Shah
}
49 98b19252 Amit Shah
50 0b6d2266 Hans de Goede
/* Callback function that's called when the guest opens the port */
51 0b6d2266 Hans de Goede
static void guest_open(VirtIOSerialPort *port)
52 0b6d2266 Hans de Goede
{
53 0b6d2266 Hans de Goede
    VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
54 0b6d2266 Hans de Goede
55 c9d830ed Anthony Liguori
    qemu_chr_fe_open(vcon->chr);
56 0b6d2266 Hans de Goede
}
57 0b6d2266 Hans de Goede
58 0b6d2266 Hans de Goede
/* Callback function that's called when the guest closes the port */
59 0b6d2266 Hans de Goede
static void guest_close(VirtIOSerialPort *port)
60 0b6d2266 Hans de Goede
{
61 0b6d2266 Hans de Goede
    VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
62 0b6d2266 Hans de Goede
63 2817822d Anthony Liguori
    qemu_chr_fe_close(vcon->chr);
64 0b6d2266 Hans de Goede
}
65 0b6d2266 Hans de Goede
66 98b19252 Amit Shah
/* Readiness of the guest to accept data on a port */
67 98b19252 Amit Shah
static int chr_can_read(void *opaque)
68 98b19252 Amit Shah
{
69 98b19252 Amit Shah
    VirtConsole *vcon = opaque;
70 98b19252 Amit Shah
71 98b19252 Amit Shah
    return virtio_serial_guest_ready(&vcon->port);
72 98b19252 Amit Shah
}
73 98b19252 Amit Shah
74 98b19252 Amit Shah
/* Send data from a char device over to the guest */
75 98b19252 Amit Shah
static void chr_read(void *opaque, const uint8_t *buf, int size)
76 98b19252 Amit Shah
{
77 98b19252 Amit Shah
    VirtConsole *vcon = opaque;
78 98b19252 Amit Shah
79 d02e4fa4 Amit Shah
    trace_virtio_console_chr_read(vcon->port.id, size);
80 98b19252 Amit Shah
    virtio_serial_write(&vcon->port, buf, size);
81 98b19252 Amit Shah
}
82 98b19252 Amit Shah
83 98b19252 Amit Shah
static void chr_event(void *opaque, int event)
84 98b19252 Amit Shah
{
85 98b19252 Amit Shah
    VirtConsole *vcon = opaque;
86 98b19252 Amit Shah
87 d02e4fa4 Amit Shah
    trace_virtio_console_chr_event(vcon->port.id, event);
88 98b19252 Amit Shah
    switch (event) {
89 28eaf465 Amit Shah
    case CHR_EVENT_OPENED:
90 98b19252 Amit Shah
        virtio_serial_open(&vcon->port);
91 98b19252 Amit Shah
        break;
92 98b19252 Amit Shah
    case CHR_EVENT_CLOSED:
93 98b19252 Amit Shah
        virtio_serial_close(&vcon->port);
94 98b19252 Amit Shah
        break;
95 98b19252 Amit Shah
    }
96 98b19252 Amit Shah
}
97 98b19252 Amit Shah
98 7edfe652 Markus Armbruster
static int virtconsole_initfn(VirtIOSerialPort *port)
99 98b19252 Amit Shah
{
100 7edfe652 Markus Armbruster
    VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
101 a15bb0d6 Markus Armbruster
    VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev,
102 a15bb0d6 Markus Armbruster
                                           vcon->port.dev.info);
103 a15bb0d6 Markus Armbruster
104 7edfe652 Markus Armbruster
    if (port->id == 0 && !info->is_console) {
105 7edfe652 Markus Armbruster
        error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility.");
106 7edfe652 Markus Armbruster
        return -1;
107 7edfe652 Markus Armbruster
    }
108 7edfe652 Markus Armbruster
109 98b19252 Amit Shah
    if (vcon->chr) {
110 98b19252 Amit Shah
        qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
111 98b19252 Amit Shah
                              vcon);
112 a15bb0d6 Markus Armbruster
        info->have_data = flush_buf;
113 a15bb0d6 Markus Armbruster
        info->guest_open = guest_open;
114 a15bb0d6 Markus Armbruster
        info->guest_close = guest_close;
115 98b19252 Amit Shah
    }
116 cbe77b61 Amit Shah
117 7edfe652 Markus Armbruster
    return 0;
118 cbe77b61 Amit Shah
}
119 cbe77b61 Amit Shah
120 a43f9c90 Gerd Hoffmann
static int virtconsole_exitfn(VirtIOSerialPort *port)
121 98b19252 Amit Shah
{
122 98b19252 Amit Shah
    VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
123 98b19252 Amit Shah
124 98b19252 Amit Shah
    if (vcon->chr) {
125 f9a90f18 Amit Shah
        /*
126 f9a90f18 Amit Shah
         * Instead of closing the chardev, free it so it can be used
127 f9a90f18 Amit Shah
         * for other purposes.
128 f9a90f18 Amit Shah
         */
129 f9a90f18 Amit Shah
        qemu_chr_add_handlers(vcon->chr, NULL, NULL, NULL, NULL);
130 98b19252 Amit Shah
    }
131 98b19252 Amit Shah
132 98b19252 Amit Shah
    return 0;
133 98b19252 Amit Shah
}
134 98b19252 Amit Shah
135 98b19252 Amit Shah
static VirtIOSerialPortInfo virtconsole_info = {
136 98b19252 Amit Shah
    .qdev.name     = "virtconsole",
137 98b19252 Amit Shah
    .qdev.size     = sizeof(VirtConsole),
138 2a3d57ce Markus Armbruster
    .is_console    = true,
139 98b19252 Amit Shah
    .init          = virtconsole_initfn,
140 98b19252 Amit Shah
    .exit          = virtconsole_exitfn,
141 98b19252 Amit Shah
    .qdev.props = (Property[]) {
142 98b19252 Amit Shah
        DEFINE_PROP_CHR("chardev", VirtConsole, chr),
143 98b19252 Amit Shah
        DEFINE_PROP_END_OF_LIST(),
144 98b19252 Amit Shah
    },
145 98b19252 Amit Shah
};
146 98b19252 Amit Shah
147 98b19252 Amit Shah
static void virtconsole_register(void)
148 98b19252 Amit Shah
{
149 98b19252 Amit Shah
    virtio_serial_port_qdev_register(&virtconsole_info);
150 98b19252 Amit Shah
}
151 98b19252 Amit Shah
device_init(virtconsole_register)
152 b60c470b Amit Shah
153 b60c470b Amit Shah
static VirtIOSerialPortInfo virtserialport_info = {
154 b60c470b Amit Shah
    .qdev.name     = "virtserialport",
155 b60c470b Amit Shah
    .qdev.size     = sizeof(VirtConsole),
156 7edfe652 Markus Armbruster
    .init          = virtconsole_initfn,
157 b60c470b Amit Shah
    .exit          = virtconsole_exitfn,
158 b60c470b Amit Shah
    .qdev.props = (Property[]) {
159 b60c470b Amit Shah
        DEFINE_PROP_CHR("chardev", VirtConsole, chr),
160 b60c470b Amit Shah
        DEFINE_PROP_END_OF_LIST(),
161 b60c470b Amit Shah
    },
162 b60c470b Amit Shah
};
163 b60c470b Amit Shah
164 b60c470b Amit Shah
static void virtserialport_register(void)
165 b60c470b Amit Shah
{
166 b60c470b Amit Shah
    virtio_serial_port_qdev_register(&virtserialport_info);
167 b60c470b Amit Shah
}
168 b60c470b Amit Shah
device_init(virtserialport_register)