Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 1 | #include "qemu/osdep.h" |
Daniel P. Berrange | 0ab8ed1 | 2017-01-25 16:14:15 +0000 | [diff] [blame] | 2 | #include "trace-root.h" |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 3 | #include "ui/qemu-spice.h" |
Paolo Bonzini | dccfcd0 | 2013-04-08 16:55:25 +0200 | [diff] [blame] | 4 | #include "sysemu/char.h" |
Marc-André Lureau | 096b489 | 2016-11-30 17:55:20 +0400 | [diff] [blame] | 5 | #include "qemu/error-report.h" |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 6 | #include <spice.h> |
Marc-André Lureau | 5a49d3e | 2012-12-05 16:15:34 +0100 | [diff] [blame] | 7 | #include <spice/protocol.h> |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 8 | |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 9 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 10 | typedef struct SpiceChardev { |
| 11 | Chardev parent; |
Marc-André Lureau | 41ac54b | 2016-10-21 23:44:44 +0300 | [diff] [blame] | 12 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 13 | SpiceCharDeviceInstance sin; |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 14 | bool active; |
Hans de Goede | ae893e5 | 2013-04-05 11:30:22 +0200 | [diff] [blame] | 15 | bool blocked; |
Alon Levy | b010cec | 2013-04-05 11:30:23 +0200 | [diff] [blame] | 16 | const uint8_t *datapos; |
| 17 | int datalen; |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 18 | QLIST_ENTRY(SpiceChardev) next; |
| 19 | } SpiceChardev; |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 20 | |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 21 | #define TYPE_CHARDEV_SPICE "chardev-spice" |
| 22 | #define TYPE_CHARDEV_SPICEVMC "chardev-spicevmc" |
| 23 | #define TYPE_CHARDEV_SPICEPORT "chardev-spiceport" |
| 24 | |
| 25 | #define SPICE_CHARDEV(obj) OBJECT_CHECK(SpiceChardev, (obj), TYPE_CHARDEV_SPICE) |
| 26 | |
Hans de Goede | ae893e5 | 2013-04-05 11:30:22 +0200 | [diff] [blame] | 27 | typedef struct SpiceCharSource { |
| 28 | GSource source; |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 29 | SpiceChardev *scd; |
Hans de Goede | ae893e5 | 2013-04-05 11:30:22 +0200 | [diff] [blame] | 30 | } SpiceCharSource; |
| 31 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 32 | static QLIST_HEAD(, SpiceChardev) spice_chars = |
Marc-André Lureau | 7a5448c | 2012-12-05 16:15:35 +0100 | [diff] [blame] | 33 | QLIST_HEAD_INITIALIZER(spice_chars); |
| 34 | |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 35 | static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len) |
| 36 | { |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 37 | SpiceChardev *scd = container_of(sin, SpiceChardev, sin); |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 38 | Chardev *chr = CHARDEV(scd); |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 39 | ssize_t out = 0; |
| 40 | ssize_t last_out; |
| 41 | uint8_t* p = (uint8_t*)buf; |
| 42 | |
| 43 | while (len > 0) { |
Marc-André Lureau | 41ac54b | 2016-10-21 23:44:44 +0300 | [diff] [blame] | 44 | int can_write = qemu_chr_be_can_write(chr); |
Hans de Goede | 75c439b | 2013-04-05 11:30:24 +0200 | [diff] [blame] | 45 | last_out = MIN(len, can_write); |
Marc-André Lureau | 07a54d7 | 2012-12-05 16:15:32 +0100 | [diff] [blame] | 46 | if (last_out <= 0) { |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 47 | break; |
| 48 | } |
Marc-André Lureau | 41ac54b | 2016-10-21 23:44:44 +0300 | [diff] [blame] | 49 | qemu_chr_be_write(chr, p, last_out); |
Hans de Goede | 35106c2 | 2011-03-22 16:28:41 +0100 | [diff] [blame] | 50 | out += last_out; |
| 51 | len -= last_out; |
| 52 | p += last_out; |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 55 | trace_spice_vmc_write(out, len + out); |
| 56 | return out; |
| 57 | } |
| 58 | |
| 59 | static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len) |
| 60 | { |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 61 | SpiceChardev *scd = container_of(sin, SpiceChardev, sin); |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 62 | int bytes = MIN(len, scd->datalen); |
| 63 | |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 64 | if (bytes > 0) { |
| 65 | memcpy(buf, scd->datapos, bytes); |
| 66 | scd->datapos += bytes; |
| 67 | scd->datalen -= bytes; |
| 68 | assert(scd->datalen >= 0); |
Hans de Goede | ae893e5 | 2013-04-05 11:30:22 +0200 | [diff] [blame] | 69 | } |
| 70 | if (scd->datalen == 0) { |
| 71 | scd->datapos = 0; |
| 72 | scd->blocked = false; |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 73 | } |
| 74 | trace_spice_vmc_read(bytes, len); |
| 75 | return bytes; |
| 76 | } |
| 77 | |
Marc-André Lureau | 5a49d3e | 2012-12-05 16:15:34 +0100 | [diff] [blame] | 78 | #if SPICE_SERVER_VERSION >= 0x000c02 |
| 79 | static void vmc_event(SpiceCharDeviceInstance *sin, uint8_t event) |
| 80 | { |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 81 | SpiceChardev *scd = container_of(sin, SpiceChardev, sin); |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 82 | Chardev *chr = CHARDEV(scd); |
Marc-André Lureau | 5a49d3e | 2012-12-05 16:15:34 +0100 | [diff] [blame] | 83 | int chr_event; |
| 84 | |
| 85 | switch (event) { |
| 86 | case SPICE_PORT_EVENT_BREAK: |
| 87 | chr_event = CHR_EVENT_BREAK; |
| 88 | break; |
| 89 | default: |
Marc-André Lureau | 5a49d3e | 2012-12-05 16:15:34 +0100 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | |
Marc-André Lureau | 5a49d3e | 2012-12-05 16:15:34 +0100 | [diff] [blame] | 93 | trace_spice_vmc_event(chr_event); |
Marc-André Lureau | 41ac54b | 2016-10-21 23:44:44 +0300 | [diff] [blame] | 94 | qemu_chr_be_event(chr, chr_event); |
Marc-André Lureau | 5a49d3e | 2012-12-05 16:15:34 +0100 | [diff] [blame] | 95 | } |
| 96 | #endif |
| 97 | |
Hans de Goede | f76e4c7 | 2011-11-19 10:22:44 +0100 | [diff] [blame] | 98 | static void vmc_state(SpiceCharDeviceInstance *sin, int connected) |
| 99 | { |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 100 | SpiceChardev *scd = container_of(sin, SpiceChardev, sin); |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 101 | Chardev *chr = CHARDEV(scd); |
Hans de Goede | f76e4c7 | 2011-11-19 10:22:44 +0100 | [diff] [blame] | 102 | |
Marc-André Lureau | 41ac54b | 2016-10-21 23:44:44 +0300 | [diff] [blame] | 103 | if ((chr->be_open && connected) || |
| 104 | (!chr->be_open && !connected)) { |
Hans de Goede | f76e4c7 | 2011-11-19 10:22:44 +0100 | [diff] [blame] | 105 | return; |
| 106 | } |
| 107 | |
Marc-André Lureau | 41ac54b | 2016-10-21 23:44:44 +0300 | [diff] [blame] | 108 | qemu_chr_be_event(chr, |
Hans de Goede | f76e4c7 | 2011-11-19 10:22:44 +0100 | [diff] [blame] | 109 | connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED); |
| 110 | } |
| 111 | |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 112 | static SpiceCharDeviceInterface vmc_interface = { |
| 113 | .base.type = SPICE_INTERFACE_CHAR_DEVICE, |
| 114 | .base.description = "spice virtual channel char device", |
| 115 | .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR, |
| 116 | .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR, |
Hans de Goede | f76e4c7 | 2011-11-19 10:22:44 +0100 | [diff] [blame] | 117 | .state = vmc_state, |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 118 | .write = vmc_write, |
| 119 | .read = vmc_read, |
Marc-André Lureau | 5a49d3e | 2012-12-05 16:15:34 +0100 | [diff] [blame] | 120 | #if SPICE_SERVER_VERSION >= 0x000c02 |
| 121 | .event = vmc_event, |
| 122 | #endif |
Marc-André Lureau | e95e203 | 2015-05-05 16:58:56 +0200 | [diff] [blame] | 123 | #if SPICE_SERVER_VERSION >= 0x000c06 |
| 124 | .flags = SPICE_CHAR_DEVICE_NOTIFY_WRITABLE, |
| 125 | #endif |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 129 | static void vmc_register_interface(SpiceChardev *scd) |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 130 | { |
| 131 | if (scd->active) { |
| 132 | return; |
| 133 | } |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 134 | scd->sin.base.sif = &vmc_interface.base; |
| 135 | qemu_spice_add_interface(&scd->sin.base); |
| 136 | scd->active = true; |
| 137 | trace_spice_vmc_register_interface(scd); |
| 138 | } |
| 139 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 140 | static void vmc_unregister_interface(SpiceChardev *scd) |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 141 | { |
| 142 | if (!scd->active) { |
| 143 | return; |
| 144 | } |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 145 | spice_server_remove_interface(&scd->sin.base); |
| 146 | scd->active = false; |
| 147 | trace_spice_vmc_unregister_interface(scd); |
| 148 | } |
| 149 | |
Hans de Goede | ae893e5 | 2013-04-05 11:30:22 +0200 | [diff] [blame] | 150 | static gboolean spice_char_source_prepare(GSource *source, gint *timeout) |
| 151 | { |
| 152 | SpiceCharSource *src = (SpiceCharSource *)source; |
| 153 | |
| 154 | *timeout = -1; |
| 155 | |
| 156 | return !src->scd->blocked; |
| 157 | } |
| 158 | |
| 159 | static gboolean spice_char_source_check(GSource *source) |
| 160 | { |
| 161 | SpiceCharSource *src = (SpiceCharSource *)source; |
| 162 | |
| 163 | return !src->scd->blocked; |
| 164 | } |
| 165 | |
| 166 | static gboolean spice_char_source_dispatch(GSource *source, |
| 167 | GSourceFunc callback, gpointer user_data) |
| 168 | { |
| 169 | GIOFunc func = (GIOFunc)callback; |
| 170 | |
| 171 | return func(NULL, G_IO_OUT, user_data); |
| 172 | } |
| 173 | |
Stefan Weil | 51575c3 | 2015-02-06 22:43:14 +0100 | [diff] [blame] | 174 | static GSourceFuncs SpiceCharSourceFuncs = { |
Hans de Goede | ae893e5 | 2013-04-05 11:30:22 +0200 | [diff] [blame] | 175 | .prepare = spice_char_source_prepare, |
| 176 | .check = spice_char_source_check, |
| 177 | .dispatch = spice_char_source_dispatch, |
| 178 | }; |
| 179 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 180 | static GSource *spice_chr_add_watch(Chardev *chr, GIOCondition cond) |
Hans de Goede | ae893e5 | 2013-04-05 11:30:22 +0200 | [diff] [blame] | 181 | { |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 182 | SpiceChardev *scd = SPICE_CHARDEV(chr); |
Hans de Goede | ae893e5 | 2013-04-05 11:30:22 +0200 | [diff] [blame] | 183 | SpiceCharSource *src; |
| 184 | |
Marc-André Lureau | f7a8beb | 2015-05-28 15:04:58 +0200 | [diff] [blame] | 185 | assert(cond & G_IO_OUT); |
Hans de Goede | ae893e5 | 2013-04-05 11:30:22 +0200 | [diff] [blame] | 186 | |
| 187 | src = (SpiceCharSource *)g_source_new(&SpiceCharSourceFuncs, |
| 188 | sizeof(SpiceCharSource)); |
| 189 | src->scd = scd; |
| 190 | |
| 191 | return (GSource *)src; |
| 192 | } |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 193 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 194 | static int spice_chr_write(Chardev *chr, const uint8_t *buf, int len) |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 195 | { |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 196 | SpiceChardev *s = SPICE_CHARDEV(chr); |
Hans de Goede | ae893e5 | 2013-04-05 11:30:22 +0200 | [diff] [blame] | 197 | int read_bytes; |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 198 | |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 199 | assert(s->datalen == 0); |
Alon Levy | b010cec | 2013-04-05 11:30:23 +0200 | [diff] [blame] | 200 | s->datapos = buf; |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 201 | s->datalen = len; |
| 202 | spice_server_char_device_wakeup(&s->sin); |
Hans de Goede | ae893e5 | 2013-04-05 11:30:22 +0200 | [diff] [blame] | 203 | read_bytes = len - s->datalen; |
| 204 | if (read_bytes != len) { |
| 205 | /* We'll get passed in the unconsumed data with the next call */ |
| 206 | s->datalen = 0; |
| 207 | s->datapos = NULL; |
| 208 | s->blocked = true; |
| 209 | } |
| 210 | return read_bytes; |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 211 | } |
| 212 | |
Marc-André Lureau | 18c508a | 2016-12-08 16:34:16 +0300 | [diff] [blame] | 213 | static void char_spice_finalize(Object *obj) |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 214 | { |
Marc-André Lureau | 18c508a | 2016-12-08 16:34:16 +0300 | [diff] [blame] | 215 | SpiceChardev *s = SPICE_CHARDEV(obj); |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 216 | |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 217 | vmc_unregister_interface(s); |
Li Qiang | f20e6f8 | 2017-02-21 00:18:27 -0800 | [diff] [blame] | 218 | |
| 219 | if (s->next.le_prev) { |
| 220 | QLIST_REMOVE(s, next); |
| 221 | } |
Hans de Goede | 5e9b473 | 2013-03-13 10:41:31 +0100 | [diff] [blame] | 222 | |
| 223 | g_free((char *)s->sin.subtype); |
| 224 | #if SPICE_SERVER_VERSION >= 0x000c02 |
| 225 | g_free((char *)s->sin.portname); |
| 226 | #endif |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 229 | static void spice_vmc_set_fe_open(struct Chardev *chr, int fe_open) |
Hans de Goede | cd8f7df | 2011-03-24 11:12:04 +0100 | [diff] [blame] | 230 | { |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 231 | SpiceChardev *s = SPICE_CHARDEV(chr); |
Hans de Goede | 574b711 | 2013-03-26 11:07:58 +0100 | [diff] [blame] | 232 | if (fe_open) { |
| 233 | vmc_register_interface(s); |
| 234 | } else { |
| 235 | vmc_unregister_interface(s); |
| 236 | } |
Hans de Goede | cd8f7df | 2011-03-24 11:12:04 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 239 | static void spice_port_set_fe_open(struct Chardev *chr, int fe_open) |
Marc-André Lureau | 8909114 | 2014-01-11 00:20:24 +0100 | [diff] [blame] | 240 | { |
| 241 | #if SPICE_SERVER_VERSION >= 0x000c02 |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 242 | SpiceChardev *s = SPICE_CHARDEV(chr); |
Marc-André Lureau | 8909114 | 2014-01-11 00:20:24 +0100 | [diff] [blame] | 243 | |
| 244 | if (fe_open) { |
| 245 | spice_server_port_event(&s->sin, SPICE_PORT_EVENT_OPENED); |
| 246 | } else { |
| 247 | spice_server_port_event(&s->sin, SPICE_PORT_EVENT_CLOSED); |
| 248 | } |
| 249 | #endif |
| 250 | } |
| 251 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 252 | static void spice_chr_accept_input(struct Chardev *chr) |
Marc-André Lureau | e95e203 | 2015-05-05 16:58:56 +0200 | [diff] [blame] | 253 | { |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 254 | SpiceChardev *s = SPICE_CHARDEV(chr); |
Marc-André Lureau | e95e203 | 2015-05-05 16:58:56 +0200 | [diff] [blame] | 255 | |
| 256 | spice_server_char_device_wakeup(&s->sin); |
| 257 | } |
| 258 | |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 259 | static void chr_open(Chardev *chr, const char *subtype) |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 260 | { |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 261 | SpiceChardev *s = SPICE_CHARDEV(chr); |
Marc-André Lureau | 71b423f | 2012-12-05 16:15:33 +0100 | [diff] [blame] | 262 | |
Marc-André Lureau | 71b423f | 2012-12-05 16:15:33 +0100 | [diff] [blame] | 263 | s->active = false; |
Hans de Goede | 5e9b473 | 2013-03-13 10:41:31 +0100 | [diff] [blame] | 264 | s->sin.subtype = g_strdup(subtype); |
Marc-André Lureau | 71b423f | 2012-12-05 16:15:33 +0100 | [diff] [blame] | 265 | |
Marc-André Lureau | 7a5448c | 2012-12-05 16:15:35 +0100 | [diff] [blame] | 266 | QLIST_INSERT_HEAD(&spice_chars, s, next); |
Marc-André Lureau | 71b423f | 2012-12-05 16:15:33 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 269 | static void qemu_chr_open_spice_vmc(Chardev *chr, |
| 270 | ChardevBackend *backend, |
| 271 | bool *be_opened, |
| 272 | Error **errp) |
Marc-André Lureau | 71b423f | 2012-12-05 16:15:33 +0100 | [diff] [blame] | 273 | { |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 274 | ChardevSpiceChannel *spicevmc = backend->u.spicevmc.data; |
| 275 | const char *type = spicevmc->type; |
Marc-André Lureau | 71b423f | 2012-12-05 16:15:33 +0100 | [diff] [blame] | 276 | const char **psubtype = spice_server_char_device_recognized_subtypes(); |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 277 | |
Gerd Hoffmann | cd153e2 | 2013-02-25 12:39:06 +0100 | [diff] [blame] | 278 | for (; *psubtype != NULL; ++psubtype) { |
| 279 | if (strcmp(type, *psubtype) == 0) { |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 280 | break; |
| 281 | } |
| 282 | } |
Gerd Hoffmann | cd153e2 | 2013-02-25 12:39:06 +0100 | [diff] [blame] | 283 | if (*psubtype == NULL) { |
Marc-André Lureau | 096b489 | 2016-11-30 17:55:20 +0400 | [diff] [blame] | 284 | char *subtypes = g_strjoinv(", ", |
| 285 | (gchar **)spice_server_char_device_recognized_subtypes()); |
| 286 | |
| 287 | error_setg(errp, "unsupported type name: %s", type); |
| 288 | error_append_hint(errp, "allowed spice char type names: %s\n", |
| 289 | subtypes); |
| 290 | |
| 291 | g_free(subtypes); |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 292 | return; |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 293 | } |
| 294 | |
Marc-André Lureau | 82878da | 2016-10-22 13:09:43 +0300 | [diff] [blame] | 295 | *be_opened = false; |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 296 | chr_open(chr, type); |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 297 | } |
Marc-André Lureau | 5a49d3e | 2012-12-05 16:15:34 +0100 | [diff] [blame] | 298 | |
| 299 | #if SPICE_SERVER_VERSION >= 0x000c02 |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 300 | static void qemu_chr_open_spice_port(Chardev *chr, |
| 301 | ChardevBackend *backend, |
| 302 | bool *be_opened, |
| 303 | Error **errp) |
Marc-André Lureau | 5a49d3e | 2012-12-05 16:15:34 +0100 | [diff] [blame] | 304 | { |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 305 | ChardevSpicePort *spiceport = backend->u.spiceport.data; |
| 306 | const char *name = spiceport->fqdn; |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 307 | SpiceChardev *s; |
Marc-André Lureau | 5a49d3e | 2012-12-05 16:15:34 +0100 | [diff] [blame] | 308 | |
| 309 | if (name == NULL) { |
Marc-André Lureau | 096b489 | 2016-11-30 17:55:20 +0400 | [diff] [blame] | 310 | error_setg(errp, "missing name parameter"); |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 311 | return; |
Marc-André Lureau | 5a49d3e | 2012-12-05 16:15:34 +0100 | [diff] [blame] | 312 | } |
| 313 | |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 314 | chr_open(chr, "port"); |
| 315 | |
Marc-André Lureau | 82878da | 2016-10-22 13:09:43 +0300 | [diff] [blame] | 316 | *be_opened = false; |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 317 | s = SPICE_CHARDEV(chr); |
Hans de Goede | 5e9b473 | 2013-03-13 10:41:31 +0100 | [diff] [blame] | 318 | s->sin.portname = g_strdup(name); |
Marc-André Lureau | 5a49d3e | 2012-12-05 16:15:34 +0100 | [diff] [blame] | 319 | } |
Marc-André Lureau | afd0b40 | 2012-12-05 16:15:36 +0100 | [diff] [blame] | 320 | |
| 321 | void qemu_spice_register_ports(void) |
| 322 | { |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 323 | SpiceChardev *s; |
Marc-André Lureau | afd0b40 | 2012-12-05 16:15:36 +0100 | [diff] [blame] | 324 | |
| 325 | QLIST_FOREACH(s, &spice_chars, next) { |
| 326 | if (s->sin.portname == NULL) { |
| 327 | continue; |
| 328 | } |
| 329 | vmc_register_interface(s); |
| 330 | } |
| 331 | } |
Marc-André Lureau | 5a49d3e | 2012-12-05 16:15:34 +0100 | [diff] [blame] | 332 | #endif |
Anthony Liguori | 26c6061 | 2013-03-05 23:21:29 +0530 | [diff] [blame] | 333 | |
Gerd Hoffmann | cd153e2 | 2013-02-25 12:39:06 +0100 | [diff] [blame] | 334 | static void qemu_chr_parse_spice_vmc(QemuOpts *opts, ChardevBackend *backend, |
| 335 | Error **errp) |
| 336 | { |
| 337 | const char *name = qemu_opt_get(opts, "name"); |
Eric Blake | 21a933e | 2016-02-19 17:19:31 -0700 | [diff] [blame] | 338 | ChardevSpiceChannel *spicevmc; |
Gerd Hoffmann | cd153e2 | 2013-02-25 12:39:06 +0100 | [diff] [blame] | 339 | |
| 340 | if (name == NULL) { |
| 341 | error_setg(errp, "chardev: spice channel: no name given"); |
| 342 | return; |
| 343 | } |
Marc-André Lureau | 0b663b7 | 2016-12-09 11:04:51 +0300 | [diff] [blame] | 344 | backend->type = CHARDEV_BACKEND_KIND_SPICEVMC; |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 345 | spicevmc = backend->u.spicevmc.data = g_new0(ChardevSpiceChannel, 1); |
Eric Blake | 21a933e | 2016-02-19 17:19:31 -0700 | [diff] [blame] | 346 | qemu_chr_parse_common(opts, qapi_ChardevSpiceChannel_base(spicevmc)); |
| 347 | spicevmc->type = g_strdup(name); |
Gerd Hoffmann | cd153e2 | 2013-02-25 12:39:06 +0100 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static void qemu_chr_parse_spice_port(QemuOpts *opts, ChardevBackend *backend, |
| 351 | Error **errp) |
| 352 | { |
| 353 | const char *name = qemu_opt_get(opts, "name"); |
Eric Blake | 21a933e | 2016-02-19 17:19:31 -0700 | [diff] [blame] | 354 | ChardevSpicePort *spiceport; |
Gerd Hoffmann | cd153e2 | 2013-02-25 12:39:06 +0100 | [diff] [blame] | 355 | |
| 356 | if (name == NULL) { |
| 357 | error_setg(errp, "chardev: spice port: no name given"); |
| 358 | return; |
| 359 | } |
Marc-André Lureau | 0b663b7 | 2016-12-09 11:04:51 +0300 | [diff] [blame] | 360 | backend->type = CHARDEV_BACKEND_KIND_SPICEPORT; |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 361 | spiceport = backend->u.spiceport.data = g_new0(ChardevSpicePort, 1); |
Eric Blake | 21a933e | 2016-02-19 17:19:31 -0700 | [diff] [blame] | 362 | qemu_chr_parse_common(opts, qapi_ChardevSpicePort_base(spiceport)); |
| 363 | spiceport->fqdn = g_strdup(name); |
Gerd Hoffmann | cd153e2 | 2013-02-25 12:39:06 +0100 | [diff] [blame] | 364 | } |
| 365 | |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 366 | static void char_spice_class_init(ObjectClass *oc, void *data) |
| 367 | { |
| 368 | ChardevClass *cc = CHARDEV_CLASS(oc); |
| 369 | |
| 370 | cc->chr_write = spice_chr_write; |
| 371 | cc->chr_add_watch = spice_chr_add_watch; |
| 372 | cc->chr_accept_input = spice_chr_accept_input; |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static const TypeInfo char_spice_type_info = { |
| 376 | .name = TYPE_CHARDEV_SPICE, |
| 377 | .parent = TYPE_CHARDEV, |
| 378 | .instance_size = sizeof(SpiceChardev), |
Marc-André Lureau | 18c508a | 2016-12-08 16:34:16 +0300 | [diff] [blame] | 379 | .instance_finalize = char_spice_finalize, |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 380 | .class_init = char_spice_class_init, |
| 381 | .abstract = true, |
| 382 | }; |
| 383 | |
| 384 | static void char_spicevmc_class_init(ObjectClass *oc, void *data) |
| 385 | { |
| 386 | ChardevClass *cc = CHARDEV_CLASS(oc); |
| 387 | |
Marc-André Lureau | 88cace9 | 2016-12-09 00:50:12 +0300 | [diff] [blame] | 388 | cc->parse = qemu_chr_parse_spice_vmc; |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 389 | cc->open = qemu_chr_open_spice_vmc; |
| 390 | cc->chr_set_fe_open = spice_vmc_set_fe_open; |
| 391 | } |
| 392 | |
| 393 | static const TypeInfo char_spicevmc_type_info = { |
| 394 | .name = TYPE_CHARDEV_SPICEVMC, |
| 395 | .parent = TYPE_CHARDEV_SPICE, |
| 396 | .class_init = char_spicevmc_class_init, |
| 397 | }; |
| 398 | |
| 399 | static void char_spiceport_class_init(ObjectClass *oc, void *data) |
| 400 | { |
| 401 | ChardevClass *cc = CHARDEV_CLASS(oc); |
| 402 | |
Marc-André Lureau | 88cace9 | 2016-12-09 00:50:12 +0300 | [diff] [blame] | 403 | cc->parse = qemu_chr_parse_spice_port; |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 404 | cc->open = qemu_chr_open_spice_port; |
| 405 | cc->chr_set_fe_open = spice_port_set_fe_open; |
| 406 | } |
| 407 | |
| 408 | static const TypeInfo char_spiceport_type_info = { |
| 409 | .name = TYPE_CHARDEV_SPICEPORT, |
| 410 | .parent = TYPE_CHARDEV_SPICE, |
| 411 | .class_init = char_spiceport_class_init, |
| 412 | }; |
| 413 | |
Anthony Liguori | 26c6061 | 2013-03-05 23:21:29 +0530 | [diff] [blame] | 414 | static void register_types(void) |
| 415 | { |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 416 | type_register_static(&char_spice_type_info); |
| 417 | type_register_static(&char_spicevmc_type_info); |
| 418 | type_register_static(&char_spiceport_type_info); |
Anthony Liguori | 26c6061 | 2013-03-05 23:21:29 +0530 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | type_init(register_types); |