hw/arm/bcm2835: Use TYPE_PL011 instead of hardcoded string
[qemu/ar7.git] / chardev / spice.c
blob22c30ae833dd99093465f65bdc9939b701ccbccf
1 #include "qemu/osdep.h"
2 #include "trace.h"
3 #include "ui/qemu-spice.h"
4 #include "chardev/char.h"
5 #include "chardev/spice.h"
6 #include "qapi/error.h"
7 #include "qemu/error-report.h"
8 #include "qemu/option.h"
9 #include <spice/protocol.h>
11 typedef struct SpiceCharSource {
12 GSource source;
13 SpiceChardev *scd;
14 } SpiceCharSource;
16 static QLIST_HEAD(, SpiceChardev) spice_chars =
17 QLIST_HEAD_INITIALIZER(spice_chars);
19 static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
21 SpiceChardev *scd = container_of(sin, SpiceChardev, sin);
22 Chardev *chr = CHARDEV(scd);
23 ssize_t out = 0;
24 ssize_t last_out;
25 uint8_t* p = (uint8_t*)buf;
27 while (len > 0) {
28 int can_write = qemu_chr_be_can_write(chr);
29 last_out = MIN(len, can_write);
30 if (last_out <= 0) {
31 break;
33 qemu_chr_be_write(chr, p, last_out);
34 out += last_out;
35 len -= last_out;
36 p += last_out;
39 trace_spice_vmc_write(out, len + out);
40 return out;
43 static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
45 SpiceChardev *scd = container_of(sin, SpiceChardev, sin);
46 int bytes = MIN(len, scd->datalen);
48 if (bytes > 0) {
49 memcpy(buf, scd->datapos, bytes);
50 scd->datapos += bytes;
51 scd->datalen -= bytes;
52 assert(scd->datalen >= 0);
54 if (scd->datalen == 0) {
55 scd->datapos = 0;
56 scd->blocked = false;
58 trace_spice_vmc_read(bytes, len);
59 return bytes;
62 static void vmc_event(SpiceCharDeviceInstance *sin, uint8_t event)
64 SpiceChardev *scd = container_of(sin, SpiceChardev, sin);
65 Chardev *chr = CHARDEV(scd);
66 int chr_event;
68 switch (event) {
69 case SPICE_PORT_EVENT_BREAK:
70 chr_event = CHR_EVENT_BREAK;
71 break;
72 default:
73 return;
76 trace_spice_vmc_event(chr_event);
77 qemu_chr_be_event(chr, chr_event);
80 static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
82 SpiceChardev *scd = container_of(sin, SpiceChardev, sin);
83 Chardev *chr = CHARDEV(scd);
85 if ((chr->be_open && connected) ||
86 (!chr->be_open && !connected)) {
87 return;
90 qemu_chr_be_event(chr,
91 connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED);
94 static SpiceCharDeviceInterface vmc_interface = {
95 .base.type = SPICE_INTERFACE_CHAR_DEVICE,
96 .base.description = "spice virtual channel char device",
97 .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
98 .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
99 .state = vmc_state,
100 .write = vmc_write,
101 .read = vmc_read,
102 .event = vmc_event,
103 #if SPICE_SERVER_VERSION >= 0x000c06
104 .flags = SPICE_CHAR_DEVICE_NOTIFY_WRITABLE,
105 #endif
109 static void vmc_register_interface(SpiceChardev *scd)
111 if (scd->active) {
112 return;
114 scd->sin.base.sif = &vmc_interface.base;
115 qemu_spice_add_interface(&scd->sin.base);
116 scd->active = true;
117 trace_spice_vmc_register_interface(scd);
120 static void vmc_unregister_interface(SpiceChardev *scd)
122 if (!scd->active) {
123 return;
125 spice_server_remove_interface(&scd->sin.base);
126 scd->active = false;
127 trace_spice_vmc_unregister_interface(scd);
130 static gboolean spice_char_source_prepare(GSource *source, gint *timeout)
132 SpiceCharSource *src = (SpiceCharSource *)source;
133 Chardev *chr = CHARDEV(src->scd);
135 *timeout = -1;
137 if (!chr->be_open) {
138 return true;
141 return !src->scd->blocked;
144 static gboolean spice_char_source_check(GSource *source)
146 SpiceCharSource *src = (SpiceCharSource *)source;
147 Chardev *chr = CHARDEV(src->scd);
149 if (!chr->be_open) {
150 return true;
153 return !src->scd->blocked;
156 static gboolean spice_char_source_dispatch(GSource *source,
157 GSourceFunc callback, gpointer user_data)
159 SpiceCharSource *src = (SpiceCharSource *)source;
160 Chardev *chr = CHARDEV(src->scd);
161 GIOFunc func = (GIOFunc)callback;
162 GIOCondition cond = chr->be_open ? G_IO_OUT : G_IO_HUP;
164 return func(NULL, cond, user_data);
167 static GSourceFuncs SpiceCharSourceFuncs = {
168 .prepare = spice_char_source_prepare,
169 .check = spice_char_source_check,
170 .dispatch = spice_char_source_dispatch,
173 static GSource *spice_chr_add_watch(Chardev *chr, GIOCondition cond)
175 SpiceChardev *scd = SPICE_CHARDEV(chr);
176 SpiceCharSource *src;
178 assert(cond & G_IO_OUT);
180 src = (SpiceCharSource *)g_source_new(&SpiceCharSourceFuncs,
181 sizeof(SpiceCharSource));
182 src->scd = scd;
184 return (GSource *)src;
187 static int spice_chr_write(Chardev *chr, const uint8_t *buf, int len)
189 SpiceChardev *s = SPICE_CHARDEV(chr);
190 int read_bytes;
192 assert(s->datalen == 0);
194 if (!chr->be_open) {
195 trace_spice_chr_discard_write(len);
196 return len;
199 s->datapos = buf;
200 s->datalen = len;
201 spice_server_char_device_wakeup(&s->sin);
202 read_bytes = len - s->datalen;
203 if (read_bytes != len) {
204 /* We'll get passed in the unconsumed data with the next call */
205 s->datalen = 0;
206 s->datapos = NULL;
207 s->blocked = true;
209 return read_bytes;
212 static void char_spice_finalize(Object *obj)
214 SpiceChardev *s = SPICE_CHARDEV(obj);
216 vmc_unregister_interface(s);
218 if (s->next.le_prev) {
219 QLIST_REMOVE(s, next);
222 g_free((char *)s->sin.subtype);
223 g_free((char *)s->sin.portname);
226 static void spice_vmc_set_fe_open(struct Chardev *chr, int fe_open)
228 SpiceChardev *s = SPICE_CHARDEV(chr);
229 if (fe_open) {
230 vmc_register_interface(s);
231 } else {
232 vmc_unregister_interface(s);
236 static void spice_port_set_fe_open(struct Chardev *chr, int fe_open)
238 SpiceChardev *s = SPICE_CHARDEV(chr);
240 if (fe_open) {
241 spice_server_port_event(&s->sin, SPICE_PORT_EVENT_OPENED);
242 } else {
243 spice_server_port_event(&s->sin, SPICE_PORT_EVENT_CLOSED);
247 static void spice_chr_accept_input(struct Chardev *chr)
249 SpiceChardev *s = SPICE_CHARDEV(chr);
251 spice_server_char_device_wakeup(&s->sin);
254 static void chr_open(Chardev *chr, const char *subtype)
256 SpiceChardev *s = SPICE_CHARDEV(chr);
258 s->active = false;
259 s->sin.subtype = g_strdup(subtype);
261 QLIST_INSERT_HEAD(&spice_chars, s, next);
264 static void qemu_chr_open_spice_vmc(Chardev *chr,
265 ChardevBackend *backend,
266 bool *be_opened,
267 Error **errp)
269 ChardevSpiceChannel *spicevmc = backend->u.spicevmc.data;
270 const char *type = spicevmc->type;
271 const char **psubtype = spice_server_char_device_recognized_subtypes();
273 for (; *psubtype != NULL; ++psubtype) {
274 if (strcmp(type, *psubtype) == 0) {
275 break;
278 if (*psubtype == NULL) {
279 char *subtypes = g_strjoinv(", ",
280 (gchar **)spice_server_char_device_recognized_subtypes());
282 error_setg(errp, "unsupported type name: %s", type);
283 error_append_hint(errp, "allowed spice char type names: %s\n",
284 subtypes);
286 g_free(subtypes);
287 return;
290 *be_opened = false;
291 #if SPICE_SERVER_VERSION < 0x000e02
292 /* Spice < 0.14.2 doesn't explicitly open smartcard chardev */
293 if (strcmp(type, "smartcard") == 0) {
294 *be_opened = true;
296 #endif
297 chr_open(chr, type);
300 void qemu_chr_open_spice_port(Chardev *chr,
301 ChardevBackend *backend,
302 bool *be_opened,
303 Error **errp)
305 ChardevSpicePort *spiceport = backend->u.spiceport.data;
306 const char *name = spiceport->fqdn;
307 SpiceChardev *s;
309 if (name == NULL) {
310 error_setg(errp, "missing name parameter");
311 return;
314 chr_open(chr, "port");
316 *be_opened = false;
317 s = SPICE_CHARDEV(chr);
318 s->sin.portname = g_strdup(name);
320 if (using_spice) {
321 /* spice server already created */
322 vmc_register_interface(s);
326 void qemu_spice_register_ports(void)
328 SpiceChardev *s;
330 QLIST_FOREACH(s, &spice_chars, next) {
331 if (s->sin.portname == NULL) {
332 continue;
334 vmc_register_interface(s);
338 static void qemu_chr_parse_spice_vmc(QemuOpts *opts, ChardevBackend *backend,
339 Error **errp)
341 const char *name = qemu_opt_get(opts, "name");
342 ChardevSpiceChannel *spicevmc;
344 if (name == NULL) {
345 error_setg(errp, "chardev: spice channel: no name given");
346 return;
348 backend->type = CHARDEV_BACKEND_KIND_SPICEVMC;
349 spicevmc = backend->u.spicevmc.data = g_new0(ChardevSpiceChannel, 1);
350 qemu_chr_parse_common(opts, qapi_ChardevSpiceChannel_base(spicevmc));
351 spicevmc->type = g_strdup(name);
354 static void qemu_chr_parse_spice_port(QemuOpts *opts, ChardevBackend *backend,
355 Error **errp)
357 const char *name = qemu_opt_get(opts, "name");
358 ChardevSpicePort *spiceport;
360 if (name == NULL) {
361 error_setg(errp, "chardev: spice port: no name given");
362 return;
364 backend->type = CHARDEV_BACKEND_KIND_SPICEPORT;
365 spiceport = backend->u.spiceport.data = g_new0(ChardevSpicePort, 1);
366 qemu_chr_parse_common(opts, qapi_ChardevSpicePort_base(spiceport));
367 spiceport->fqdn = g_strdup(name);
370 static void char_spice_class_init(ObjectClass *oc, void *data)
372 ChardevClass *cc = CHARDEV_CLASS(oc);
374 cc->chr_write = spice_chr_write;
375 cc->chr_add_watch = spice_chr_add_watch;
376 cc->chr_accept_input = spice_chr_accept_input;
379 static const TypeInfo char_spice_type_info = {
380 .name = TYPE_CHARDEV_SPICE,
381 .parent = TYPE_CHARDEV,
382 .instance_size = sizeof(SpiceChardev),
383 .instance_finalize = char_spice_finalize,
384 .class_init = char_spice_class_init,
385 .abstract = true,
388 static void char_spicevmc_class_init(ObjectClass *oc, void *data)
390 ChardevClass *cc = CHARDEV_CLASS(oc);
392 cc->parse = qemu_chr_parse_spice_vmc;
393 cc->open = qemu_chr_open_spice_vmc;
394 cc->chr_set_fe_open = spice_vmc_set_fe_open;
397 static const TypeInfo char_spicevmc_type_info = {
398 .name = TYPE_CHARDEV_SPICEVMC,
399 .parent = TYPE_CHARDEV_SPICE,
400 .class_init = char_spicevmc_class_init,
403 static void char_spiceport_class_init(ObjectClass *oc, void *data)
405 ChardevClass *cc = CHARDEV_CLASS(oc);
407 cc->parse = qemu_chr_parse_spice_port;
408 cc->open = qemu_chr_open_spice_port;
409 cc->chr_set_fe_open = spice_port_set_fe_open;
412 static const TypeInfo char_spiceport_type_info = {
413 .name = TYPE_CHARDEV_SPICEPORT,
414 .parent = TYPE_CHARDEV_SPICE,
415 .class_init = char_spiceport_class_init,
418 static void register_types(void)
420 type_register_static(&char_spice_type_info);
421 type_register_static(&char_spicevmc_type_info);
422 type_register_static(&char_spiceport_type_info);
425 type_init(register_types);