1 #include "qemu/osdep.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
{
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
);
25 uint8_t* p
= (uint8_t*)buf
;
28 int can_write
= qemu_chr_be_can_write(chr
);
29 last_out
= MIN(len
, can_write
);
33 qemu_chr_be_write(chr
, p
, last_out
);
39 trace_spice_vmc_write(out
, len
+ 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
);
49 memcpy(buf
, scd
->datapos
, bytes
);
50 scd
->datapos
+= bytes
;
51 scd
->datalen
-= bytes
;
52 assert(scd
->datalen
>= 0);
54 if (scd
->datalen
== 0) {
58 trace_spice_vmc_read(bytes
, len
);
62 static void vmc_event(SpiceCharDeviceInstance
*sin
, uint8_t event
)
64 SpiceChardev
*scd
= container_of(sin
, SpiceChardev
, sin
);
65 Chardev
*chr
= CHARDEV(scd
);
69 case SPICE_PORT_EVENT_BREAK
:
70 chr_event
= CHR_EVENT_BREAK
;
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
)) {
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
,
103 #if SPICE_SERVER_VERSION >= 0x000c06
104 .flags
= SPICE_CHAR_DEVICE_NOTIFY_WRITABLE
,
109 static void vmc_register_interface(SpiceChardev
*scd
)
114 scd
->sin
.base
.sif
= &vmc_interface
.base
;
115 qemu_spice_add_interface(&scd
->sin
.base
);
117 trace_spice_vmc_register_interface(scd
);
120 static void vmc_unregister_interface(SpiceChardev
*scd
)
125 spice_server_remove_interface(&scd
->sin
.base
);
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
);
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
);
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
));
184 return (GSource
*)src
;
187 static int spice_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
189 SpiceChardev
*s
= SPICE_CHARDEV(chr
);
192 assert(s
->datalen
== 0);
195 trace_spice_chr_discard_write(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 */
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
);
230 vmc_register_interface(s
);
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
);
241 spice_server_port_event(&s
->sin
, SPICE_PORT_EVENT_OPENED
);
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
);
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
,
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) {
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",
291 #if SPICE_SERVER_VERSION < 0x000e02
292 /* Spice < 0.14.2 doesn't explicitly open smartcard chardev */
293 if (strcmp(type
, "smartcard") == 0) {
300 void qemu_chr_open_spice_port(Chardev
*chr
,
301 ChardevBackend
*backend
,
305 ChardevSpicePort
*spiceport
= backend
->u
.spiceport
.data
;
306 const char *name
= spiceport
->fqdn
;
310 error_setg(errp
, "missing name parameter");
314 chr_open(chr
, "port");
317 s
= SPICE_CHARDEV(chr
);
318 s
->sin
.portname
= g_strdup(name
);
321 /* spice server already created */
322 vmc_register_interface(s
);
326 void qemu_spice_register_ports(void)
330 QLIST_FOREACH(s
, &spice_chars
, next
) {
331 if (s
->sin
.portname
== NULL
) {
334 vmc_register_interface(s
);
338 static void qemu_chr_parse_spice_vmc(QemuOpts
*opts
, ChardevBackend
*backend
,
341 const char *name
= qemu_opt_get(opts
, "name");
342 ChardevSpiceChannel
*spicevmc
;
345 error_setg(errp
, "chardev: spice channel: no name given");
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
,
357 const char *name
= qemu_opt_get(opts
, "name");
358 ChardevSpicePort
*spiceport
;
361 error_setg(errp
, "chardev: spice port: no name given");
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
,
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
);