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/module.h"
9 #include "qemu/option.h"
10 #include <spice/protocol.h>
12 typedef struct SpiceCharSource
{
17 static int vmc_write(SpiceCharDeviceInstance
*sin
, const uint8_t *buf
, int len
)
19 SpiceChardev
*scd
= container_of(sin
, SpiceChardev
, sin
);
20 Chardev
*chr
= CHARDEV(scd
);
23 uint8_t* p
= (uint8_t*)buf
;
26 int can_write
= qemu_chr_be_can_write(chr
);
27 last_out
= MIN(len
, can_write
);
31 qemu_chr_be_write(chr
, p
, last_out
);
37 trace_spice_vmc_write(out
, len
+ out
);
41 static int vmc_read(SpiceCharDeviceInstance
*sin
, uint8_t *buf
, int len
)
43 SpiceChardev
*scd
= container_of(sin
, SpiceChardev
, sin
);
44 int bytes
= MIN(len
, scd
->datalen
);
47 memcpy(buf
, scd
->datapos
, bytes
);
48 scd
->datapos
+= bytes
;
49 scd
->datalen
-= bytes
;
50 assert(scd
->datalen
>= 0);
52 if (scd
->datalen
== 0) {
56 trace_spice_vmc_read(bytes
, len
);
60 static void vmc_event(SpiceCharDeviceInstance
*sin
, uint8_t event
)
62 SpiceChardev
*scd
= container_of(sin
, SpiceChardev
, sin
);
63 Chardev
*chr
= CHARDEV(scd
);
67 case SPICE_PORT_EVENT_BREAK
:
68 chr_event
= CHR_EVENT_BREAK
;
74 trace_spice_vmc_event(chr_event
);
75 qemu_chr_be_event(chr
, chr_event
);
78 static void vmc_state(SpiceCharDeviceInstance
*sin
, int connected
)
80 SpiceChardev
*scd
= container_of(sin
, SpiceChardev
, sin
);
81 Chardev
*chr
= CHARDEV(scd
);
83 if ((chr
->be_open
&& connected
) ||
84 (!chr
->be_open
&& !connected
)) {
88 qemu_chr_be_event(chr
,
89 connected
? CHR_EVENT_OPENED
: CHR_EVENT_CLOSED
);
92 static SpiceCharDeviceInterface vmc_interface
= {
93 .base
.type
= SPICE_INTERFACE_CHAR_DEVICE
,
94 .base
.description
= "spice virtual channel char device",
95 .base
.major_version
= SPICE_INTERFACE_CHAR_DEVICE_MAJOR
,
96 .base
.minor_version
= SPICE_INTERFACE_CHAR_DEVICE_MINOR
,
101 .flags
= SPICE_CHAR_DEVICE_NOTIFY_WRITABLE
,
105 static void vmc_register_interface(SpiceChardev
*scd
)
110 scd
->sin
.base
.sif
= &vmc_interface
.base
;
111 qemu_spice
.add_interface(&scd
->sin
.base
);
113 trace_spice_vmc_register_interface(scd
);
116 static void vmc_unregister_interface(SpiceChardev
*scd
)
121 spice_server_remove_interface(&scd
->sin
.base
);
123 trace_spice_vmc_unregister_interface(scd
);
126 static gboolean
spice_char_source_prepare(GSource
*source
, gint
*timeout
)
128 SpiceCharSource
*src
= (SpiceCharSource
*)source
;
129 Chardev
*chr
= CHARDEV(src
->scd
);
137 return !src
->scd
->blocked
;
140 static gboolean
spice_char_source_check(GSource
*source
)
142 SpiceCharSource
*src
= (SpiceCharSource
*)source
;
143 Chardev
*chr
= CHARDEV(src
->scd
);
149 return !src
->scd
->blocked
;
152 static gboolean
spice_char_source_dispatch(GSource
*source
,
153 GSourceFunc callback
, gpointer user_data
)
155 SpiceCharSource
*src
= (SpiceCharSource
*)source
;
156 Chardev
*chr
= CHARDEV(src
->scd
);
157 GIOFunc func
= (GIOFunc
)callback
;
158 GIOCondition cond
= chr
->be_open
? G_IO_OUT
: G_IO_HUP
;
160 return func(NULL
, cond
, user_data
);
163 static GSourceFuncs SpiceCharSourceFuncs
= {
164 .prepare
= spice_char_source_prepare
,
165 .check
= spice_char_source_check
,
166 .dispatch
= spice_char_source_dispatch
,
169 static GSource
*spice_chr_add_watch(Chardev
*chr
, GIOCondition cond
)
171 SpiceChardev
*scd
= SPICE_CHARDEV(chr
);
172 SpiceCharSource
*src
;
174 assert(cond
& G_IO_OUT
);
176 src
= (SpiceCharSource
*)g_source_new(&SpiceCharSourceFuncs
,
177 sizeof(SpiceCharSource
));
180 return (GSource
*)src
;
183 static int spice_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
185 SpiceChardev
*s
= SPICE_CHARDEV(chr
);
188 assert(s
->datalen
== 0);
191 trace_spice_chr_discard_write(len
);
197 spice_server_char_device_wakeup(&s
->sin
);
198 read_bytes
= len
- s
->datalen
;
199 if (read_bytes
!= len
) {
200 /* We'll get passed in the unconsumed data with the next call */
208 static void char_spice_finalize(Object
*obj
)
210 SpiceChardev
*s
= SPICE_CHARDEV(obj
);
212 vmc_unregister_interface(s
);
214 g_free((char *)s
->sin
.subtype
);
215 g_free((char *)s
->sin
.portname
);
218 static void spice_vmc_set_fe_open(struct Chardev
*chr
, int fe_open
)
220 SpiceChardev
*s
= SPICE_CHARDEV(chr
);
222 vmc_register_interface(s
);
224 vmc_unregister_interface(s
);
228 static void spice_port_set_fe_open(struct Chardev
*chr
, int fe_open
)
230 SpiceChardev
*s
= SPICE_CHARDEV(chr
);
233 spice_server_port_event(&s
->sin
, SPICE_PORT_EVENT_OPENED
);
235 spice_server_port_event(&s
->sin
, SPICE_PORT_EVENT_CLOSED
);
239 static void spice_chr_accept_input(struct Chardev
*chr
)
241 SpiceChardev
*s
= SPICE_CHARDEV(chr
);
243 spice_server_char_device_wakeup(&s
->sin
);
246 static void chr_open(Chardev
*chr
, const char *subtype
)
248 SpiceChardev
*s
= SPICE_CHARDEV(chr
);
251 s
->sin
.subtype
= g_strdup(subtype
);
254 static void qemu_chr_open_spice_vmc(Chardev
*chr
,
255 ChardevBackend
*backend
,
259 ChardevSpiceChannel
*spicevmc
= backend
->u
.spicevmc
.data
;
260 const char *type
= spicevmc
->type
;
261 const char **psubtype
= spice_server_char_device_recognized_subtypes();
263 for (; *psubtype
!= NULL
; ++psubtype
) {
264 if (strcmp(type
, *psubtype
) == 0) {
268 if (*psubtype
== NULL
) {
269 char *subtypes
= g_strjoinv(", ",
270 (gchar
**)spice_server_char_device_recognized_subtypes());
272 error_setg(errp
, "unsupported type name: %s", type
);
273 error_append_hint(errp
, "allowed spice char type names: %s\n",
281 #if SPICE_SERVER_VERSION < 0x000e02
282 /* Spice < 0.14.2 doesn't explicitly open smartcard chardev */
283 if (strcmp(type
, "smartcard") == 0) {
290 static void qemu_chr_open_spice_port(Chardev
*chr
,
291 ChardevBackend
*backend
,
295 ChardevSpicePort
*spiceport
= backend
->u
.spiceport
.data
;
296 const char *name
= spiceport
->fqdn
;
300 error_setg(errp
, "missing name parameter");
305 error_setg(errp
, "spice not enabled");
309 chr_open(chr
, "port");
312 s
= SPICE_CHARDEV(chr
);
313 s
->sin
.portname
= g_strdup(name
);
315 vmc_register_interface(s
);
318 static void qemu_chr_parse_spice_vmc(QemuOpts
*opts
, ChardevBackend
*backend
,
321 const char *name
= qemu_opt_get(opts
, "name");
322 ChardevSpiceChannel
*spicevmc
;
325 error_setg(errp
, "chardev: spice channel: no name given");
328 backend
->type
= CHARDEV_BACKEND_KIND_SPICEVMC
;
329 spicevmc
= backend
->u
.spicevmc
.data
= g_new0(ChardevSpiceChannel
, 1);
330 qemu_chr_parse_common(opts
, qapi_ChardevSpiceChannel_base(spicevmc
));
331 spicevmc
->type
= g_strdup(name
);
334 static void qemu_chr_parse_spice_port(QemuOpts
*opts
, ChardevBackend
*backend
,
337 const char *name
= qemu_opt_get(opts
, "name");
338 ChardevSpicePort
*spiceport
;
341 error_setg(errp
, "chardev: spice port: no name given");
344 backend
->type
= CHARDEV_BACKEND_KIND_SPICEPORT
;
345 spiceport
= backend
->u
.spiceport
.data
= g_new0(ChardevSpicePort
, 1);
346 qemu_chr_parse_common(opts
, qapi_ChardevSpicePort_base(spiceport
));
347 spiceport
->fqdn
= g_strdup(name
);
350 static void char_spice_class_init(ObjectClass
*oc
, void *data
)
352 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
354 cc
->chr_write
= spice_chr_write
;
355 cc
->chr_add_watch
= spice_chr_add_watch
;
356 cc
->chr_accept_input
= spice_chr_accept_input
;
359 static const TypeInfo char_spice_type_info
= {
360 .name
= TYPE_CHARDEV_SPICE
,
361 .parent
= TYPE_CHARDEV
,
362 .instance_size
= sizeof(SpiceChardev
),
363 .instance_finalize
= char_spice_finalize
,
364 .class_init
= char_spice_class_init
,
367 module_obj(TYPE_CHARDEV_SPICE
);
369 static void char_spicevmc_class_init(ObjectClass
*oc
, void *data
)
371 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
373 cc
->parse
= qemu_chr_parse_spice_vmc
;
374 cc
->open
= qemu_chr_open_spice_vmc
;
375 cc
->chr_set_fe_open
= spice_vmc_set_fe_open
;
378 static const TypeInfo char_spicevmc_type_info
= {
379 .name
= TYPE_CHARDEV_SPICEVMC
,
380 .parent
= TYPE_CHARDEV_SPICE
,
381 .class_init
= char_spicevmc_class_init
,
383 module_obj(TYPE_CHARDEV_SPICEVMC
);
385 static void char_spiceport_class_init(ObjectClass
*oc
, void *data
)
387 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
389 cc
->parse
= qemu_chr_parse_spice_port
;
390 cc
->open
= qemu_chr_open_spice_port
;
391 cc
->chr_set_fe_open
= spice_port_set_fe_open
;
394 static const TypeInfo char_spiceport_type_info
= {
395 .name
= TYPE_CHARDEV_SPICEPORT
,
396 .parent
= TYPE_CHARDEV_SPICE
,
397 .class_init
= char_spiceport_class_init
,
399 module_obj(TYPE_CHARDEV_SPICEPORT
);
401 static void register_types(void)
403 type_register_static(&char_spice_type_info
);
404 type_register_static(&char_spicevmc_type_info
);
405 type_register_static(&char_spiceport_type_info
);
408 type_init(register_types
);
410 module_dep("ui-spice-core");