Statistics
| Branch: | Revision:

root / hw / virtio-console.c @ a15bb0d6

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 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 a15bb0d6 Markus Armbruster
    VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev,
80 a15bb0d6 Markus Armbruster
                                           vcon->port.dev.info);
81 a15bb0d6 Markus Armbruster
82 98b19252 Amit Shah
    if (vcon->chr) {
83 98b19252 Amit Shah
        qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
84 98b19252 Amit Shah
                              vcon);
85 a15bb0d6 Markus Armbruster
        info->have_data = flush_buf;
86 a15bb0d6 Markus Armbruster
        info->guest_open = guest_open;
87 a15bb0d6 Markus Armbruster
        info->guest_close = guest_close;
88 98b19252 Amit Shah
    }
89 98b19252 Amit Shah
    return 0;
90 98b19252 Amit Shah
}
91 98b19252 Amit Shah
92 cbe77b61 Amit Shah
/* Virtio Console Ports */
93 a43f9c90 Gerd Hoffmann
static int virtconsole_initfn(VirtIOSerialPort *port)
94 cbe77b61 Amit Shah
{
95 cbe77b61 Amit Shah
    VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
96 cbe77b61 Amit Shah
97 a43f9c90 Gerd Hoffmann
    return generic_port_init(vcon, port);
98 cbe77b61 Amit Shah
}
99 cbe77b61 Amit Shah
100 a43f9c90 Gerd Hoffmann
static int virtconsole_exitfn(VirtIOSerialPort *port)
101 98b19252 Amit Shah
{
102 98b19252 Amit Shah
    VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
103 98b19252 Amit Shah
104 98b19252 Amit Shah
    if (vcon->chr) {
105 f9a90f18 Amit Shah
        /*
106 f9a90f18 Amit Shah
         * Instead of closing the chardev, free it so it can be used
107 f9a90f18 Amit Shah
         * for other purposes.
108 f9a90f18 Amit Shah
         */
109 f9a90f18 Amit Shah
        qemu_chr_add_handlers(vcon->chr, NULL, NULL, NULL, NULL);
110 98b19252 Amit Shah
    }
111 98b19252 Amit Shah
112 98b19252 Amit Shah
    return 0;
113 98b19252 Amit Shah
}
114 98b19252 Amit Shah
115 98b19252 Amit Shah
static VirtIOSerialPortInfo virtconsole_info = {
116 98b19252 Amit Shah
    .qdev.name     = "virtconsole",
117 98b19252 Amit Shah
    .qdev.size     = sizeof(VirtConsole),
118 2a3d57ce Markus Armbruster
    .is_console    = true,
119 98b19252 Amit Shah
    .init          = virtconsole_initfn,
120 98b19252 Amit Shah
    .exit          = virtconsole_exitfn,
121 98b19252 Amit Shah
    .qdev.props = (Property[]) {
122 055b889f Amit Shah
        DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
123 98b19252 Amit Shah
        DEFINE_PROP_CHR("chardev", VirtConsole, chr),
124 160600fd Amit Shah
        DEFINE_PROP_STRING("name", VirtConsole, port.name),
125 98b19252 Amit Shah
        DEFINE_PROP_END_OF_LIST(),
126 98b19252 Amit Shah
    },
127 98b19252 Amit Shah
};
128 98b19252 Amit Shah
129 98b19252 Amit Shah
static void virtconsole_register(void)
130 98b19252 Amit Shah
{
131 98b19252 Amit Shah
    virtio_serial_port_qdev_register(&virtconsole_info);
132 98b19252 Amit Shah
}
133 98b19252 Amit Shah
device_init(virtconsole_register)
134 b60c470b Amit Shah
135 b60c470b Amit Shah
/* Generic Virtio Serial Ports */
136 a43f9c90 Gerd Hoffmann
static int virtserialport_initfn(VirtIOSerialPort *port)
137 b60c470b Amit Shah
{
138 b60c470b Amit Shah
    VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
139 b60c470b Amit Shah
140 0b8b716d Amit Shah
    if (port->id == 0) {
141 0b8b716d Amit Shah
        /*
142 0b8b716d Amit Shah
         * Disallow a generic port at id 0, that's reserved for
143 0b8b716d Amit Shah
         * console ports.
144 0b8b716d Amit Shah
         */
145 0b8b716d Amit Shah
        error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility.");
146 0b8b716d Amit Shah
        return -1;
147 0b8b716d Amit Shah
    }
148 a43f9c90 Gerd Hoffmann
    return generic_port_init(vcon, port);
149 b60c470b Amit Shah
}
150 b60c470b Amit Shah
151 b60c470b Amit Shah
static VirtIOSerialPortInfo virtserialport_info = {
152 b60c470b Amit Shah
    .qdev.name     = "virtserialport",
153 b60c470b Amit Shah
    .qdev.size     = sizeof(VirtConsole),
154 b60c470b Amit Shah
    .init          = virtserialport_initfn,
155 b60c470b Amit Shah
    .exit          = virtconsole_exitfn,
156 b60c470b Amit Shah
    .qdev.props = (Property[]) {
157 055b889f Amit Shah
        DEFINE_PROP_UINT32("nr", VirtConsole, port.id, VIRTIO_CONSOLE_BAD_ID),
158 b60c470b Amit Shah
        DEFINE_PROP_CHR("chardev", VirtConsole, chr),
159 b60c470b Amit Shah
        DEFINE_PROP_STRING("name", VirtConsole, port.name),
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)