1 /* 2 * Virtio Console and Generic Serial Port Devices 3 * 4 * Copyright Red Hat, Inc. 2009, 2010 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 "sysemu/char.h" 14 #include "qemu/error-report.h" 15 #include "trace.h" 16 #include "hw/virtio/virtio-serial.h" 17 18 #define TYPE_VIRTIO_CONSOLE_SERIAL_PORT "virtserialport" 19 #define VIRTIO_CONSOLE(obj) \ 20 OBJECT_CHECK(VirtConsole, (obj), TYPE_VIRTIO_CONSOLE_SERIAL_PORT) 21 22 typedef struct VirtConsole { 23 VirtIOSerialPort parent_obj; 24 25 CharDriverState *chr; 26 guint watch; 27 } VirtConsole; 28 29 /* 30 * Callback function that's called from chardevs when backend becomes 31 * writable. 32 */ 33 static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond, 34 void *opaque) 35 { 36 VirtConsole *vcon = opaque; 37 38 vcon->watch = 0; 39 virtio_serial_throttle_port(VIRTIO_SERIAL_PORT(vcon), false); 40 return FALSE; 41 } 42 43 /* Callback function that's called when the guest sends us data */ 44 static ssize_t flush_buf(VirtIOSerialPort *port, 45 const uint8_t *buf, ssize_t len) 46 { 47 VirtConsole *vcon = VIRTIO_CONSOLE(port); 48 ssize_t ret; 49 50 if (!vcon->chr) { 51 /* If there's no backend, we can just say we consumed all data. */ 52 return len; 53 } 54 55 ret = qemu_chr_fe_write(vcon->chr, buf, len); 56 trace_virtio_console_flush_buf(port->id, len, ret); 57 58 if (ret < len) { 59 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port); 60 61 /* 62 * Ideally we'd get a better error code than just -1, but 63 * that's what the chardev interface gives us right now. If 64 * we had a finer-grained message, like -EPIPE, we could close 65 * this connection. 66 */ 67 if (ret < 0) 68 ret = 0; 69 if (!k->is_console) { 70 virtio_serial_throttle_port(port, true); 71 if (!vcon->watch) { 72 vcon->watch = qemu_chr_fe_add_watch(vcon->chr, G_IO_OUT, 73 chr_write_unblocked, vcon); 74 } 75 } 76 } 77 return ret; 78 } 79 80 /* Callback function that's called when the guest opens/closes the port */ 81 static void set_guest_connected(VirtIOSerialPort *port, int guest_connected) 82 { 83 VirtConsole *vcon = VIRTIO_CONSOLE(port); 84 85 if (!vcon->chr) { 86 return; 87 } 88 qemu_chr_fe_set_open(vcon->chr, guest_connected); 89 } 90 91 /* Readiness of the guest to accept data on a port */ 92 static int chr_can_read(void *opaque) 93 { 94 VirtConsole *vcon = opaque; 95 96 return virtio_serial_guest_ready(VIRTIO_SERIAL_PORT(vcon)); 97 } 98 99 /* Send data from a char device over to the guest */ 100 static void chr_read(void *opaque, const uint8_t *buf, int size) 101 { 102 VirtConsole *vcon = opaque; 103 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon); 104 105 trace_virtio_console_chr_read(port->id, size); 106 virtio_serial_write(port, buf, size); 107 } 108 109 static void chr_event(void *opaque, int event) 110 { 111 VirtConsole *vcon = opaque; 112 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon); 113 114 trace_virtio_console_chr_event(port->id, event); 115 switch (event) { 116 case CHR_EVENT_OPENED: 117 virtio_serial_open(port); 118 break; 119 case CHR_EVENT_CLOSED: 120 if (vcon->watch) { 121 g_source_remove(vcon->watch); 122 vcon->watch = 0; 123 } 124 virtio_serial_close(port); 125 break; 126 } 127 } 128 129 static void virtconsole_realize(DeviceState *dev, Error **errp) 130 { 131 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev); 132 VirtConsole *vcon = VIRTIO_CONSOLE(dev); 133 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(dev); 134 135 if (port->id == 0 && !k->is_console) { 136 error_setg(errp, "Port number 0 on virtio-serial devices reserved " 137 "for virtconsole devices for backward compatibility."); 138 return; 139 } 140 141 if (vcon->chr) { 142 vcon->chr->explicit_fe_open = 1; 143 qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event, 144 vcon); 145 } 146 } 147 148 static void virtconsole_unrealize(DeviceState *dev, Error **errp) 149 { 150 VirtConsole *vcon = VIRTIO_CONSOLE(dev); 151 152 if (vcon->watch) { 153 g_source_remove(vcon->watch); 154 } 155 } 156 157 static void virtconsole_class_init(ObjectClass *klass, void *data) 158 { 159 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass); 160 161 k->is_console = true; 162 } 163 164 static const TypeInfo virtconsole_info = { 165 .name = "virtconsole", 166 .parent = TYPE_VIRTIO_CONSOLE_SERIAL_PORT, 167 .class_init = virtconsole_class_init, 168 }; 169 170 static Property virtserialport_properties[] = { 171 DEFINE_PROP_CHR("chardev", VirtConsole, chr), 172 DEFINE_PROP_END_OF_LIST(), 173 }; 174 175 static void virtserialport_class_init(ObjectClass *klass, void *data) 176 { 177 DeviceClass *dc = DEVICE_CLASS(klass); 178 VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass); 179 180 k->realize = virtconsole_realize; 181 k->unrealize = virtconsole_unrealize; 182 k->have_data = flush_buf; 183 k->set_guest_connected = set_guest_connected; 184 dc->props = virtserialport_properties; 185 } 186 187 static const TypeInfo virtserialport_info = { 188 .name = TYPE_VIRTIO_CONSOLE_SERIAL_PORT, 189 .parent = TYPE_VIRTIO_SERIAL_PORT, 190 .instance_size = sizeof(VirtConsole), 191 .class_init = virtserialport_class_init, 192 }; 193 194 static void virtconsole_register_types(void) 195 { 196 type_register_static(&virtserialport_info); 197 type_register_static(&virtconsole_info); 198 } 199 200 type_init(virtconsole_register_types) 201