1 #include "qemu/osdep.h"
3 #include "ui/qemu-spice.h"
4 #include "chardev/char.h"
5 #include "qapi/error.h"
6 #include "qemu/error-report.h"
7 #include "qemu/option.h"
9 #include <spice/protocol.h>
12 typedef struct SpiceChardev
{
15 SpiceCharDeviceInstance sin
;
18 const uint8_t *datapos
;
20 QLIST_ENTRY(SpiceChardev
) next
;
23 #define TYPE_CHARDEV_SPICE "chardev-spice"
24 #define TYPE_CHARDEV_SPICEVMC "chardev-spicevmc"
25 #define TYPE_CHARDEV_SPICEPORT "chardev-spiceport"
27 #define SPICE_CHARDEV(obj) OBJECT_CHECK(SpiceChardev, (obj), TYPE_CHARDEV_SPICE)
29 typedef struct SpiceCharSource
{
34 static QLIST_HEAD(, SpiceChardev
) spice_chars
=
35 QLIST_HEAD_INITIALIZER(spice_chars
);
37 static int vmc_write(SpiceCharDeviceInstance
*sin
, const uint8_t *buf
, int len
)
39 SpiceChardev
*scd
= container_of(sin
, SpiceChardev
, sin
);
40 Chardev
*chr
= CHARDEV(scd
);
43 uint8_t* p
= (uint8_t*)buf
;
46 int can_write
= qemu_chr_be_can_write(chr
);
47 last_out
= MIN(len
, can_write
);
51 qemu_chr_be_write(chr
, p
, last_out
);
57 trace_spice_vmc_write(out
, len
+ out
);
61 static int vmc_read(SpiceCharDeviceInstance
*sin
, uint8_t *buf
, int len
)
63 SpiceChardev
*scd
= container_of(sin
, SpiceChardev
, sin
);
64 int bytes
= MIN(len
, scd
->datalen
);
67 memcpy(buf
, scd
->datapos
, bytes
);
68 scd
->datapos
+= bytes
;
69 scd
->datalen
-= bytes
;
70 assert(scd
->datalen
>= 0);
72 if (scd
->datalen
== 0) {
76 trace_spice_vmc_read(bytes
, len
);
80 static void vmc_event(SpiceCharDeviceInstance
*sin
, uint8_t event
)
82 SpiceChardev
*scd
= container_of(sin
, SpiceChardev
, sin
);
83 Chardev
*chr
= CHARDEV(scd
);
87 case SPICE_PORT_EVENT_BREAK
:
88 chr_event
= CHR_EVENT_BREAK
;
94 trace_spice_vmc_event(chr_event
);
95 qemu_chr_be_event(chr
, chr_event
);
98 static void vmc_state(SpiceCharDeviceInstance
*sin
, int connected
)
100 SpiceChardev
*scd
= container_of(sin
, SpiceChardev
, sin
);
101 Chardev
*chr
= CHARDEV(scd
);
103 if ((chr
->be_open
&& connected
) ||
104 (!chr
->be_open
&& !connected
)) {
108 qemu_chr_be_event(chr
,
109 connected
? CHR_EVENT_OPENED
: CHR_EVENT_CLOSED
);
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
,
121 #if SPICE_SERVER_VERSION >= 0x000c06
122 .flags
= SPICE_CHAR_DEVICE_NOTIFY_WRITABLE
,
127 static void vmc_register_interface(SpiceChardev
*scd
)
132 scd
->sin
.base
.sif
= &vmc_interface
.base
;
133 qemu_spice_add_interface(&scd
->sin
.base
);
135 trace_spice_vmc_register_interface(scd
);
138 static void vmc_unregister_interface(SpiceChardev
*scd
)
143 spice_server_remove_interface(&scd
->sin
.base
);
145 trace_spice_vmc_unregister_interface(scd
);
148 static gboolean
spice_char_source_prepare(GSource
*source
, gint
*timeout
)
150 SpiceCharSource
*src
= (SpiceCharSource
*)source
;
154 return !src
->scd
->blocked
;
157 static gboolean
spice_char_source_check(GSource
*source
)
159 SpiceCharSource
*src
= (SpiceCharSource
*)source
;
161 return !src
->scd
->blocked
;
164 static gboolean
spice_char_source_dispatch(GSource
*source
,
165 GSourceFunc callback
, gpointer user_data
)
167 GIOFunc func
= (GIOFunc
)callback
;
169 return func(NULL
, G_IO_OUT
, user_data
);
172 static GSourceFuncs SpiceCharSourceFuncs
= {
173 .prepare
= spice_char_source_prepare
,
174 .check
= spice_char_source_check
,
175 .dispatch
= spice_char_source_dispatch
,
178 static GSource
*spice_chr_add_watch(Chardev
*chr
, GIOCondition cond
)
180 SpiceChardev
*scd
= SPICE_CHARDEV(chr
);
181 SpiceCharSource
*src
;
183 assert(cond
& G_IO_OUT
);
185 src
= (SpiceCharSource
*)g_source_new(&SpiceCharSourceFuncs
,
186 sizeof(SpiceCharSource
));
189 return (GSource
*)src
;
192 static int spice_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
194 SpiceChardev
*s
= SPICE_CHARDEV(chr
);
197 assert(s
->datalen
== 0);
200 spice_server_char_device_wakeup(&s
->sin
);
201 read_bytes
= len
- s
->datalen
;
202 if (read_bytes
!= len
) {
203 /* We'll get passed in the unconsumed data with the next call */
211 static void char_spice_finalize(Object
*obj
)
213 SpiceChardev
*s
= SPICE_CHARDEV(obj
);
215 vmc_unregister_interface(s
);
217 if (s
->next
.le_prev
) {
218 QLIST_REMOVE(s
, next
);
221 g_free((char *)s
->sin
.subtype
);
222 g_free((char *)s
->sin
.portname
);
225 static void spice_vmc_set_fe_open(struct Chardev
*chr
, int fe_open
)
227 SpiceChardev
*s
= SPICE_CHARDEV(chr
);
229 vmc_register_interface(s
);
231 vmc_unregister_interface(s
);
235 static void spice_port_set_fe_open(struct Chardev
*chr
, int fe_open
)
237 SpiceChardev
*s
= SPICE_CHARDEV(chr
);
240 spice_server_port_event(&s
->sin
, SPICE_PORT_EVENT_OPENED
);
242 spice_server_port_event(&s
->sin
, SPICE_PORT_EVENT_CLOSED
);
246 static void spice_chr_accept_input(struct Chardev
*chr
)
248 SpiceChardev
*s
= SPICE_CHARDEV(chr
);
250 spice_server_char_device_wakeup(&s
->sin
);
253 static void chr_open(Chardev
*chr
, const char *subtype
)
255 SpiceChardev
*s
= SPICE_CHARDEV(chr
);
258 s
->sin
.subtype
= g_strdup(subtype
);
260 QLIST_INSERT_HEAD(&spice_chars
, s
, next
);
263 static void qemu_chr_open_spice_vmc(Chardev
*chr
,
264 ChardevBackend
*backend
,
268 ChardevSpiceChannel
*spicevmc
= backend
->u
.spicevmc
.data
;
269 const char *type
= spicevmc
->type
;
270 const char **psubtype
= spice_server_char_device_recognized_subtypes();
272 for (; *psubtype
!= NULL
; ++psubtype
) {
273 if (strcmp(type
, *psubtype
) == 0) {
277 if (*psubtype
== NULL
) {
278 char *subtypes
= g_strjoinv(", ",
279 (gchar
**)spice_server_char_device_recognized_subtypes());
281 error_setg(errp
, "unsupported type name: %s", type
);
282 error_append_hint(errp
, "allowed spice char type names: %s\n",
293 static void qemu_chr_open_spice_port(Chardev
*chr
,
294 ChardevBackend
*backend
,
298 ChardevSpicePort
*spiceport
= backend
->u
.spiceport
.data
;
299 const char *name
= spiceport
->fqdn
;
303 error_setg(errp
, "missing name parameter");
307 chr_open(chr
, "port");
310 s
= SPICE_CHARDEV(chr
);
311 s
->sin
.portname
= g_strdup(name
);
314 void qemu_spice_register_ports(void)
318 QLIST_FOREACH(s
, &spice_chars
, next
) {
319 if (s
->sin
.portname
== NULL
) {
322 vmc_register_interface(s
);
326 static void qemu_chr_parse_spice_vmc(QemuOpts
*opts
, ChardevBackend
*backend
,
329 const char *name
= qemu_opt_get(opts
, "name");
330 ChardevSpiceChannel
*spicevmc
;
333 error_setg(errp
, "chardev: spice channel: no name given");
336 backend
->type
= CHARDEV_BACKEND_KIND_SPICEVMC
;
337 spicevmc
= backend
->u
.spicevmc
.data
= g_new0(ChardevSpiceChannel
, 1);
338 qemu_chr_parse_common(opts
, qapi_ChardevSpiceChannel_base(spicevmc
));
339 spicevmc
->type
= g_strdup(name
);
342 static void qemu_chr_parse_spice_port(QemuOpts
*opts
, ChardevBackend
*backend
,
345 const char *name
= qemu_opt_get(opts
, "name");
346 ChardevSpicePort
*spiceport
;
349 error_setg(errp
, "chardev: spice port: no name given");
352 backend
->type
= CHARDEV_BACKEND_KIND_SPICEPORT
;
353 spiceport
= backend
->u
.spiceport
.data
= g_new0(ChardevSpicePort
, 1);
354 qemu_chr_parse_common(opts
, qapi_ChardevSpicePort_base(spiceport
));
355 spiceport
->fqdn
= g_strdup(name
);
358 static void char_spice_class_init(ObjectClass
*oc
, void *data
)
360 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
362 cc
->chr_write
= spice_chr_write
;
363 cc
->chr_add_watch
= spice_chr_add_watch
;
364 cc
->chr_accept_input
= spice_chr_accept_input
;
367 static const TypeInfo char_spice_type_info
= {
368 .name
= TYPE_CHARDEV_SPICE
,
369 .parent
= TYPE_CHARDEV
,
370 .instance_size
= sizeof(SpiceChardev
),
371 .instance_finalize
= char_spice_finalize
,
372 .class_init
= char_spice_class_init
,
376 static void char_spicevmc_class_init(ObjectClass
*oc
, void *data
)
378 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
380 cc
->parse
= qemu_chr_parse_spice_vmc
;
381 cc
->open
= qemu_chr_open_spice_vmc
;
382 cc
->chr_set_fe_open
= spice_vmc_set_fe_open
;
385 static const TypeInfo char_spicevmc_type_info
= {
386 .name
= TYPE_CHARDEV_SPICEVMC
,
387 .parent
= TYPE_CHARDEV_SPICE
,
388 .class_init
= char_spicevmc_class_init
,
391 static void char_spiceport_class_init(ObjectClass
*oc
, void *data
)
393 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
395 cc
->parse
= qemu_chr_parse_spice_port
;
396 cc
->open
= qemu_chr_open_spice_port
;
397 cc
->chr_set_fe_open
= spice_port_set_fe_open
;
400 static const TypeInfo char_spiceport_type_info
= {
401 .name
= TYPE_CHARDEV_SPICEPORT
,
402 .parent
= TYPE_CHARDEV_SPICE
,
403 .class_init
= char_spiceport_class_init
,
406 static void register_types(void)
408 type_register_static(&char_spice_type_info
);
409 type_register_static(&char_spicevmc_type_info
);
410 type_register_static(&char_spiceport_type_info
);
413 type_init(register_types
);