Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Virtio Console and Generic Serial Port Devices |
| 3 | * |
Amit Shah | 71c092e | 2010-04-27 18:04:02 +0530 | [diff] [blame] | 4 | * Copyright Red Hat, Inc. 2009, 2010 |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 5 | * |
| 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 Shah | 0b8b716 | 2011-02-03 13:05:07 +0530 | [diff] [blame] | 14 | #include "qemu-error.h" |
Amit Shah | d02e4fa | 2011-07-05 16:37:49 +0530 | [diff] [blame] | 15 | #include "trace.h" |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 16 | #include "virtio-serial.h" |
| 17 | |
| 18 | typedef 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 Shah | e300ac2 | 2010-12-13 17:50:07 +0530 | [diff] [blame] | 25 | static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len) |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 26 | { |
| 27 | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
Amit Shah | d02e4fa | 2011-07-05 16:37:49 +0530 | [diff] [blame] | 28 | ssize_t ret; |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 29 | |
Amit Shah | 6640422 | 2011-12-21 12:28:27 +0530 | [diff] [blame] | 30 | if (!vcon->chr) { |
| 31 | /* If there's no backend, we can just say we consumed all data. */ |
| 32 | return len; |
| 33 | } |
| 34 | |
Anthony Liguori | 2cc6e0a | 2011-08-15 11:17:28 -0500 | [diff] [blame] | 35 | ret = qemu_chr_fe_write(vcon->chr, buf, len); |
Amit Shah | d02e4fa | 2011-07-05 16:37:49 +0530 | [diff] [blame] | 36 | trace_virtio_console_flush_buf(port->id, len, ret); |
Amit Shah | 0219d73 | 2011-07-07 17:35:27 +0530 | [diff] [blame] | 37 | |
| 38 | if (ret < 0) { |
| 39 | /* |
| 40 | * Ideally we'd get a better error code than just -1, but |
| 41 | * that's what the chardev interface gives us right now. If |
| 42 | * we had a finer-grained message, like -EPIPE, we could close |
| 43 | * this connection. Absent such error messages, the most we |
| 44 | * can do is to return 0 here. |
| 45 | * |
| 46 | * This will prevent stray -1 values to go to |
| 47 | * virtio-serial-bus.c and cause abort()s in |
| 48 | * do_flush_queued_data(). |
| 49 | */ |
| 50 | ret = 0; |
| 51 | } |
Amit Shah | d02e4fa | 2011-07-05 16:37:49 +0530 | [diff] [blame] | 52 | return ret; |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 53 | } |
| 54 | |
Hans de Goede | 0b6d226 | 2011-03-24 11:12:03 +0100 | [diff] [blame] | 55 | /* Callback function that's called when the guest opens the port */ |
| 56 | static void guest_open(VirtIOSerialPort *port) |
| 57 | { |
| 58 | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
| 59 | |
Amit Shah | 6640422 | 2011-12-21 12:28:27 +0530 | [diff] [blame] | 60 | if (!vcon->chr) { |
| 61 | return; |
| 62 | } |
Anthony Liguori | c9d830e | 2011-08-15 11:17:32 -0500 | [diff] [blame] | 63 | qemu_chr_fe_open(vcon->chr); |
Hans de Goede | 0b6d226 | 2011-03-24 11:12:03 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* Callback function that's called when the guest closes the port */ |
| 67 | static void guest_close(VirtIOSerialPort *port) |
| 68 | { |
| 69 | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
| 70 | |
Amit Shah | 6640422 | 2011-12-21 12:28:27 +0530 | [diff] [blame] | 71 | if (!vcon->chr) { |
| 72 | return; |
| 73 | } |
Anthony Liguori | 2817822 | 2011-08-15 11:17:33 -0500 | [diff] [blame] | 74 | qemu_chr_fe_close(vcon->chr); |
Hans de Goede | 0b6d226 | 2011-03-24 11:12:03 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 77 | /* Readiness of the guest to accept data on a port */ |
| 78 | static int chr_can_read(void *opaque) |
| 79 | { |
| 80 | VirtConsole *vcon = opaque; |
| 81 | |
| 82 | return virtio_serial_guest_ready(&vcon->port); |
| 83 | } |
| 84 | |
| 85 | /* Send data from a char device over to the guest */ |
| 86 | static void chr_read(void *opaque, const uint8_t *buf, int size) |
| 87 | { |
| 88 | VirtConsole *vcon = opaque; |
| 89 | |
Amit Shah | d02e4fa | 2011-07-05 16:37:49 +0530 | [diff] [blame] | 90 | trace_virtio_console_chr_read(vcon->port.id, size); |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 91 | virtio_serial_write(&vcon->port, buf, size); |
| 92 | } |
| 93 | |
| 94 | static void chr_event(void *opaque, int event) |
| 95 | { |
| 96 | VirtConsole *vcon = opaque; |
| 97 | |
Amit Shah | d02e4fa | 2011-07-05 16:37:49 +0530 | [diff] [blame] | 98 | trace_virtio_console_chr_event(vcon->port.id, event); |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 99 | switch (event) { |
Amit Shah | 28eaf46 | 2010-12-10 17:10:43 +0530 | [diff] [blame] | 100 | case CHR_EVENT_OPENED: |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 101 | virtio_serial_open(&vcon->port); |
| 102 | break; |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 103 | case CHR_EVENT_CLOSED: |
| 104 | virtio_serial_close(&vcon->port); |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | |
Markus Armbruster | 7edfe65 | 2011-05-25 14:21:14 +0200 | [diff] [blame] | 109 | static int virtconsole_initfn(VirtIOSerialPort *port) |
Amit Shah | cbe77b6 | 2010-04-05 14:20:29 +0530 | [diff] [blame] | 110 | { |
Markus Armbruster | 7edfe65 | 2011-05-25 14:21:14 +0200 | [diff] [blame] | 111 | VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port); |
Anthony Liguori | f82e35e | 2011-12-04 12:38:12 -0600 | [diff] [blame] | 112 | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port); |
Markus Armbruster | a15bb0d | 2011-05-25 14:21:13 +0200 | [diff] [blame] | 113 | |
Anthony Liguori | f82e35e | 2011-12-04 12:38:12 -0600 | [diff] [blame] | 114 | if (port->id == 0 && !k->is_console) { |
Markus Armbruster | 7edfe65 | 2011-05-25 14:21:14 +0200 | [diff] [blame] | 115 | error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility."); |
| 116 | return -1; |
| 117 | } |
| 118 | |
Amit Shah | cbe77b6 | 2010-04-05 14:20:29 +0530 | [diff] [blame] | 119 | if (vcon->chr) { |
| 120 | qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event, |
| 121 | vcon); |
Amit Shah | cbe77b6 | 2010-04-05 14:20:29 +0530 | [diff] [blame] | 122 | } |
Markus Armbruster | 7edfe65 | 2011-05-25 14:21:14 +0200 | [diff] [blame] | 123 | |
Amit Shah | cbe77b6 | 2010-04-05 14:20:29 +0530 | [diff] [blame] | 124 | return 0; |
| 125 | } |
| 126 | |
Anthony Liguori | f82e35e | 2011-12-04 12:38:12 -0600 | [diff] [blame] | 127 | static Property virtconsole_properties[] = { |
| 128 | DEFINE_PROP_CHR("chardev", VirtConsole, chr), |
| 129 | DEFINE_PROP_END_OF_LIST(), |
| 130 | }; |
| 131 | |
| 132 | static void virtconsole_class_init(ObjectClass *klass, void *data) |
| 133 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 134 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | f82e35e | 2011-12-04 12:38:12 -0600 | [diff] [blame] | 135 | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass); |
| 136 | |
| 137 | k->is_console = true; |
| 138 | k->init = virtconsole_initfn; |
| 139 | k->have_data = flush_buf; |
| 140 | k->guest_open = guest_open; |
| 141 | k->guest_close = guest_close; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 142 | dc->props = virtconsole_properties; |
Anthony Liguori | f82e35e | 2011-12-04 12:38:12 -0600 | [diff] [blame] | 143 | } |
| 144 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 145 | static TypeInfo virtconsole_info = { |
| 146 | .name = "virtconsole", |
| 147 | .parent = TYPE_VIRTIO_SERIAL_PORT, |
| 148 | .instance_size = sizeof(VirtConsole), |
| 149 | .class_init = virtconsole_class_init, |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 150 | }; |
| 151 | |
Anthony Liguori | f82e35e | 2011-12-04 12:38:12 -0600 | [diff] [blame] | 152 | static Property virtserialport_properties[] = { |
| 153 | DEFINE_PROP_CHR("chardev", VirtConsole, chr), |
| 154 | DEFINE_PROP_END_OF_LIST(), |
| 155 | }; |
| 156 | |
| 157 | static void virtserialport_class_init(ObjectClass *klass, void *data) |
| 158 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 159 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | f82e35e | 2011-12-04 12:38:12 -0600 | [diff] [blame] | 160 | VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass); |
| 161 | |
| 162 | k->init = virtconsole_initfn; |
| 163 | k->have_data = flush_buf; |
| 164 | k->guest_open = guest_open; |
| 165 | k->guest_close = guest_close; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 166 | dc->props = virtserialport_properties; |
Anthony Liguori | f82e35e | 2011-12-04 12:38:12 -0600 | [diff] [blame] | 167 | } |
| 168 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 169 | static TypeInfo virtserialport_info = { |
| 170 | .name = "virtserialport", |
| 171 | .parent = TYPE_VIRTIO_SERIAL_PORT, |
| 172 | .instance_size = sizeof(VirtConsole), |
| 173 | .class_init = virtserialport_class_init, |
Amit Shah | b60c470 | 2010-01-20 00:36:56 +0530 | [diff] [blame] | 174 | }; |
| 175 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 176 | static void virtconsole_register_types(void) |
Amit Shah | b60c470 | 2010-01-20 00:36:56 +0530 | [diff] [blame] | 177 | { |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 178 | type_register_static(&virtconsole_info); |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 179 | type_register_static(&virtserialport_info); |
Amit Shah | b60c470 | 2010-01-20 00:36:56 +0530 | [diff] [blame] | 180 | } |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 181 | |
| 182 | type_init(virtconsole_register_types) |