Statistics
| Branch: | Revision:

root / hw / virtio-console.c @ 0647b949

History | View | Annotate | Download (4.4 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 98b19252 Amit Shah
#include "virtio-serial.h"
16 98b19252 Amit Shah
17 98b19252 Amit Shah
typedef struct VirtConsole {
18 98b19252 Amit Shah
    VirtIOSerialPort port;
19 98b19252 Amit Shah
    CharDriverState *chr;
20 98b19252 Amit Shah
} VirtConsole;
21 98b19252 Amit Shah
22 98b19252 Amit Shah
23 98b19252 Amit Shah
/* Callback function that's called when the guest sends us data */
24 e300ac27 Amit Shah
static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
25 98b19252 Amit Shah
{
26 98b19252 Amit Shah
    VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
27 98b19252 Amit Shah
28 e300ac27 Amit Shah
    return qemu_chr_write(vcon->chr, buf, len);
29 98b19252 Amit Shah
}
30 98b19252 Amit Shah
31 0b6d2266 Hans de Goede
/* Callback function that's called when the guest opens the port */
32 0b6d2266 Hans de Goede
static void guest_open(VirtIOSerialPort *port)
33 0b6d2266 Hans de Goede
{
34 0b6d2266 Hans de Goede
    VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
35 0b6d2266 Hans de Goede
36 0b6d2266 Hans de Goede
    qemu_chr_guest_open(vcon->chr);
37 0b6d2266 Hans de Goede
}
38 0b6d2266 Hans de Goede
39 0b6d2266 Hans de Goede
/* Callback function that's called when the guest closes the port */
40 0b6d2266 Hans de Goede
static void guest_close(VirtIOSerialPort *port)
41 0b6d2266 Hans de Goede
{
42 0b6d2266 Hans de Goede
    VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
43 0b6d2266 Hans de Goede
44 0b6d2266 Hans de Goede
    qemu_chr_guest_close(vcon->chr);
45 0b6d2266 Hans de Goede
}
46 0b6d2266 Hans de Goede
47 98b19252 Amit Shah
/* Readiness of the guest to accept data on a port */
48 98b19252 Amit Shah
static int chr_can_read(void *opaque)
49 98b19252 Amit Shah
{
50 98b19252 Amit Shah
    VirtConsole *vcon = opaque;
51 98b19252 Amit Shah
52 98b19252 Amit Shah
    return virtio_serial_guest_ready(&vcon->port);
53 98b19252 Amit Shah
}
54 98b19252 Amit Shah
55 98b19252 Amit Shah
/* Send data from a char device over to the guest */
56 98b19252 Amit Shah
static void chr_read(void *opaque, const uint8_t *buf, int size)
57 98b19252 Amit Shah
{
58 98b19252 Amit Shah
    VirtConsole *vcon = opaque;
59 98b19252 Amit Shah
60 98b19252 Amit Shah
    virtio_serial_write(&vcon->port, buf, size);
61 98b19252 Amit Shah
}
62 98b19252 Amit Shah
63 98b19252 Amit Shah
static void chr_event(void *opaque, int event)
64 98b19252 Amit Shah
{
65 98b19252 Amit Shah
    VirtConsole *vcon = opaque;
66 98b19252 Amit Shah
67 98b19252 Amit Shah
    switch (event) {
68 28eaf465 Amit Shah
    case CHR_EVENT_OPENED:
69 98b19252 Amit Shah
        virtio_serial_open(&vcon->port);
70 98b19252 Amit Shah
        break;
71 98b19252 Amit Shah
    case CHR_EVENT_CLOSED:
72 98b19252 Amit Shah
        virtio_serial_close(&vcon->port);
73 98b19252 Amit Shah
        break;
74 98b19252 Amit Shah
    }
75 98b19252 Amit Shah
}
76 98b19252 Amit Shah
77 a43f9c90 Gerd Hoffmann
static int generic_port_init(VirtConsole *vcon, VirtIOSerialPort *port)
78 98b19252 Amit Shah
{
79 98b19252 Amit Shah
    if (vcon->chr) {
80 98b19252 Amit Shah
        qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
81 98b19252 Amit Shah
                              vcon);
82 cbe77b61 Amit Shah
        vcon->port.info->have_data = flush_buf;
83 0b6d2266 Hans de Goede
        vcon->port.info->guest_open = guest_open;
84 0b6d2266 Hans de Goede
        vcon->port.info->guest_close = guest_close;
85 98b19252 Amit Shah
    }
86 98b19252 Amit Shah
    return 0;
87 98b19252 Amit Shah
}
88 98b19252 Amit Shah
89 cbe77b61 Amit Shah
/* Virtio Console Ports */
90 a43f9c90 Gerd Hoffmann
static int virtconsole_initfn(VirtIOSerialPort *port)
91 cbe77b61 Amit Shah
{
92 cbe77b61 Amit Shah
    VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
93 cbe77b61 Amit Shah
94 cbe77b61 Amit Shah
    port->is_console = true;
95 a43f9c90 Gerd Hoffmann
    return generic_port_init(vcon, port);
96 cbe77b61 Amit Shah
}
97 cbe77b61 Amit Shah
98 a43f9c90 Gerd Hoffmann
static int virtconsole_exitfn(VirtIOSerialPort *port)
99 98b19252 Amit Shah
{
100 98b19252 Amit Shah
    VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
101 98b19252 Amit Shah
102 98b19252 Amit Shah
    if (vcon->chr) {
103 f9a90f18 Amit Shah
        /*
104 f9a90f18 Amit Shah
         * Instead of closing the chardev, free it so it can be used
105 f9a90f18 Amit Shah
         * for other purposes.
106 f9a90f18 Amit Shah
         */
107 f9a90f18 Amit Shah
        qemu_chr_add_handlers(vcon->chr, NULL, NULL, NULL, NULL);
108 98b19252 Amit Shah
    }
109 98b19252 Amit Shah
110 98b19252 Amit Shah
    return 0;
111 98b19252 Amit Shah
}
112 98b19252 Amit Shah
113 98b19252 Amit Shah
static VirtIOSerialPortInfo virtconsole_info = {
114 98b19252 Amit Shah
    .qdev.name     = "virtconsole",
115 98b19252 Amit Shah
    .qdev.size     = sizeof(VirtConsole),
116 98b19252 Amit Shah
    .init          = virtconsole_initfn,
117 98b19252 Amit Shah
    .exit          = virtconsole_exitfn,
118 98b19252 Amit Shah
    .qdev.props = (Property[]) {
119 98b19252 Amit Shah
        DEFINE_PROP_UINT8("is_console", VirtConsole, port.is_console, 1),
120 055b889f Amit Shah
        DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
121 98b19252 Amit Shah
        DEFINE_PROP_CHR("chardev", VirtConsole, chr),
122 160600fd Amit Shah
        DEFINE_PROP_STRING("name", VirtConsole, port.name),
123 98b19252 Amit Shah
        DEFINE_PROP_END_OF_LIST(),
124 98b19252 Amit Shah
    },
125 98b19252 Amit Shah
};
126 98b19252 Amit Shah
127 98b19252 Amit Shah
static void virtconsole_register(void)
128 98b19252 Amit Shah
{
129 98b19252 Amit Shah
    virtio_serial_port_qdev_register(&virtconsole_info);
130 98b19252 Amit Shah
}
131 98b19252 Amit Shah
device_init(virtconsole_register)
132 b60c470b Amit Shah
133 b60c470b Amit Shah
/* Generic Virtio Serial Ports */
134 a43f9c90 Gerd Hoffmann
static int virtserialport_initfn(VirtIOSerialPort *port)
135 b60c470b Amit Shah
{
136 b60c470b Amit Shah
    VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
137 b60c470b Amit Shah
138 0b8b716d Amit Shah
    if (port->id == 0) {
139 0b8b716d Amit Shah
        /*
140 0b8b716d Amit Shah
         * Disallow a generic port at id 0, that's reserved for
141 0b8b716d Amit Shah
         * console ports.
142 0b8b716d Amit Shah
         */
143 0b8b716d Amit Shah
        error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility.");
144 0b8b716d Amit Shah
        return -1;
145 0b8b716d Amit Shah
    }
146 a43f9c90 Gerd Hoffmann
    return generic_port_init(vcon, port);
147 b60c470b Amit Shah
}
148 b60c470b Amit Shah
149 b60c470b Amit Shah
static VirtIOSerialPortInfo virtserialport_info = {
150 b60c470b Amit Shah
    .qdev.name     = "virtserialport",
151 b60c470b Amit Shah
    .qdev.size     = sizeof(VirtConsole),
152 b60c470b Amit Shah
    .init          = virtserialport_initfn,
153 b60c470b Amit Shah
    .exit          = virtconsole_exitfn,
154 b60c470b Amit Shah
    .qdev.props = (Property[]) {
155 055b889f Amit Shah
        DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
156 b60c470b Amit Shah
        DEFINE_PROP_CHR("chardev", VirtConsole, chr),
157 b60c470b Amit Shah
        DEFINE_PROP_STRING("name", VirtConsole, port.name),
158 b60c470b Amit Shah
        DEFINE_PROP_END_OF_LIST(),
159 b60c470b Amit Shah
    },
160 b60c470b Amit Shah
};
161 b60c470b Amit Shah
162 b60c470b Amit Shah
static void virtserialport_register(void)
163 b60c470b Amit Shah
{
164 b60c470b Amit Shah
    virtio_serial_port_qdev_register(&virtserialport_info);
165 b60c470b Amit Shah
}
166 b60c470b Amit Shah
device_init(virtserialport_register)