1 #include "config-host.h"
3 #include "ui/qemu-spice.h"
4 #include "sysemu/char.h"
6 #include <spice/protocol.h>
8 #include "qemu/osdep.h"
10 typedef struct SpiceCharDriver
{
12 SpiceCharDeviceInstance sin
;
15 const uint8_t *datapos
;
17 QLIST_ENTRY(SpiceCharDriver
) next
;
20 typedef struct SpiceCharSource
{
25 static QLIST_HEAD(, SpiceCharDriver
) spice_chars
=
26 QLIST_HEAD_INITIALIZER(spice_chars
);
28 static int vmc_write(SpiceCharDeviceInstance
*sin
, const uint8_t *buf
, int len
)
30 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
33 uint8_t* p
= (uint8_t*)buf
;
36 int can_write
= qemu_chr_be_can_write(scd
->chr
);
37 last_out
= MIN(len
, can_write
);
41 qemu_chr_be_write(scd
->chr
, p
, last_out
);
47 trace_spice_vmc_write(out
, len
+ out
);
51 static int vmc_read(SpiceCharDeviceInstance
*sin
, uint8_t *buf
, int len
)
53 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
54 int bytes
= MIN(len
, scd
->datalen
);
57 memcpy(buf
, scd
->datapos
, bytes
);
58 scd
->datapos
+= bytes
;
59 scd
->datalen
-= bytes
;
60 assert(scd
->datalen
>= 0);
62 if (scd
->datalen
== 0) {
66 trace_spice_vmc_read(bytes
, len
);
70 #if SPICE_SERVER_VERSION >= 0x000c02
71 static void vmc_event(SpiceCharDeviceInstance
*sin
, uint8_t event
)
73 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
77 case SPICE_PORT_EVENT_BREAK
:
78 chr_event
= CHR_EVENT_BREAK
;
84 trace_spice_vmc_event(chr_event
);
85 qemu_chr_be_event(scd
->chr
, chr_event
);
89 static void vmc_state(SpiceCharDeviceInstance
*sin
, int connected
)
91 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
93 if ((scd
->chr
->be_open
&& connected
) ||
94 (!scd
->chr
->be_open
&& !connected
)) {
98 qemu_chr_be_event(scd
->chr
,
99 connected
? CHR_EVENT_OPENED
: CHR_EVENT_CLOSED
);
102 static SpiceCharDeviceInterface vmc_interface
= {
103 .base
.type
= SPICE_INTERFACE_CHAR_DEVICE
,
104 .base
.description
= "spice virtual channel char device",
105 .base
.major_version
= SPICE_INTERFACE_CHAR_DEVICE_MAJOR
,
106 .base
.minor_version
= SPICE_INTERFACE_CHAR_DEVICE_MINOR
,
110 #if SPICE_SERVER_VERSION >= 0x000c02
116 static void vmc_register_interface(SpiceCharDriver
*scd
)
121 scd
->sin
.base
.sif
= &vmc_interface
.base
;
122 qemu_spice_add_interface(&scd
->sin
.base
);
124 trace_spice_vmc_register_interface(scd
);
127 static void vmc_unregister_interface(SpiceCharDriver
*scd
)
132 spice_server_remove_interface(&scd
->sin
.base
);
134 trace_spice_vmc_unregister_interface(scd
);
137 static gboolean
spice_char_source_prepare(GSource
*source
, gint
*timeout
)
139 SpiceCharSource
*src
= (SpiceCharSource
*)source
;
143 return !src
->scd
->blocked
;
146 static gboolean
spice_char_source_check(GSource
*source
)
148 SpiceCharSource
*src
= (SpiceCharSource
*)source
;
150 return !src
->scd
->blocked
;
153 static gboolean
spice_char_source_dispatch(GSource
*source
,
154 GSourceFunc callback
, gpointer user_data
)
156 GIOFunc func
= (GIOFunc
)callback
;
158 return func(NULL
, G_IO_OUT
, user_data
);
161 static GSourceFuncs SpiceCharSourceFuncs
= {
162 .prepare
= spice_char_source_prepare
,
163 .check
= spice_char_source_check
,
164 .dispatch
= spice_char_source_dispatch
,
167 static GSource
*spice_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
169 SpiceCharDriver
*scd
= chr
->opaque
;
170 SpiceCharSource
*src
;
172 assert(cond
== G_IO_OUT
);
174 src
= (SpiceCharSource
*)g_source_new(&SpiceCharSourceFuncs
,
175 sizeof(SpiceCharSource
));
178 return (GSource
*)src
;
181 static int spice_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
183 SpiceCharDriver
*s
= chr
->opaque
;
186 assert(s
->datalen
== 0);
189 spice_server_char_device_wakeup(&s
->sin
);
190 read_bytes
= len
- s
->datalen
;
191 if (read_bytes
!= len
) {
192 /* We'll get passed in the unconsumed data with the next call */
200 static void spice_chr_close(struct CharDriverState
*chr
)
202 SpiceCharDriver
*s
= chr
->opaque
;
204 vmc_unregister_interface(s
);
205 QLIST_REMOVE(s
, next
);
207 g_free((char *)s
->sin
.subtype
);
208 #if SPICE_SERVER_VERSION >= 0x000c02
209 g_free((char *)s
->sin
.portname
);
214 static void spice_vmc_set_fe_open(struct CharDriverState
*chr
, int fe_open
)
216 SpiceCharDriver
*s
= chr
->opaque
;
218 vmc_register_interface(s
);
220 vmc_unregister_interface(s
);
224 static void spice_port_set_fe_open(struct CharDriverState
*chr
, int fe_open
)
226 #if SPICE_SERVER_VERSION >= 0x000c02
227 SpiceCharDriver
*s
= chr
->opaque
;
230 spice_server_port_event(&s
->sin
, SPICE_PORT_EVENT_OPENED
);
232 spice_server_port_event(&s
->sin
, SPICE_PORT_EVENT_CLOSED
);
237 static void spice_chr_fe_event(struct CharDriverState
*chr
, int event
)
239 #if SPICE_SERVER_VERSION >= 0x000c02
240 SpiceCharDriver
*s
= chr
->opaque
;
242 spice_server_port_event(&s
->sin
, event
);
246 static void print_allowed_subtypes(void)
248 const char** psubtype
;
251 fprintf(stderr
, "allowed names: ");
252 for(i
=0, psubtype
= spice_server_char_device_recognized_subtypes();
253 *psubtype
!= NULL
; ++psubtype
, ++i
) {
255 fprintf(stderr
, "%s", *psubtype
);
257 fprintf(stderr
, ", %s", *psubtype
);
260 fprintf(stderr
, "\n");
263 static CharDriverState
*chr_open(const char *subtype
,
264 void (*set_fe_open
)(struct CharDriverState
*, int))
267 CharDriverState
*chr
;
270 chr
= qemu_chr_alloc();
271 s
= g_malloc0(sizeof(SpiceCharDriver
));
274 s
->sin
.subtype
= g_strdup(subtype
);
276 chr
->chr_write
= spice_chr_write
;
277 chr
->chr_add_watch
= spice_chr_add_watch
;
278 chr
->chr_close
= spice_chr_close
;
279 chr
->chr_set_fe_open
= set_fe_open
;
280 chr
->explicit_be_open
= true;
281 chr
->chr_fe_event
= spice_chr_fe_event
;
283 QLIST_INSERT_HEAD(&spice_chars
, s
, next
);
288 CharDriverState
*qemu_chr_open_spice_vmc(const char *type
)
290 const char **psubtype
= spice_server_char_device_recognized_subtypes();
293 fprintf(stderr
, "spice-qemu-char: missing name parameter\n");
294 print_allowed_subtypes();
297 for (; *psubtype
!= NULL
; ++psubtype
) {
298 if (strcmp(type
, *psubtype
) == 0) {
302 if (*psubtype
== NULL
) {
303 fprintf(stderr
, "spice-qemu-char: unsupported type: %s\n", type
);
304 print_allowed_subtypes();
308 return chr_open(type
, spice_vmc_set_fe_open
);
311 #if SPICE_SERVER_VERSION >= 0x000c02
312 CharDriverState
*qemu_chr_open_spice_port(const char *name
)
314 CharDriverState
*chr
;
318 fprintf(stderr
, "spice-qemu-char: missing name parameter\n");
322 chr
= chr_open("port", spice_port_set_fe_open
);
324 s
->sin
.portname
= g_strdup(name
);
329 void qemu_spice_register_ports(void)
333 QLIST_FOREACH(s
, &spice_chars
, next
) {
334 if (s
->sin
.portname
== NULL
) {
337 vmc_register_interface(s
);
342 static void qemu_chr_parse_spice_vmc(QemuOpts
*opts
, ChardevBackend
*backend
,
345 const char *name
= qemu_opt_get(opts
, "name");
348 error_setg(errp
, "chardev: spice channel: no name given");
351 backend
->spicevmc
= g_new0(ChardevSpiceChannel
, 1);
352 backend
->spicevmc
->type
= g_strdup(name
);
355 static void qemu_chr_parse_spice_port(QemuOpts
*opts
, ChardevBackend
*backend
,
358 const char *name
= qemu_opt_get(opts
, "name");
361 error_setg(errp
, "chardev: spice port: no name given");
364 backend
->spiceport
= g_new0(ChardevSpicePort
, 1);
365 backend
->spiceport
->fqdn
= g_strdup(name
);
368 static void register_types(void)
370 register_char_driver("spicevmc", CHARDEV_BACKEND_KIND_SPICEVMC
,
371 qemu_chr_parse_spice_vmc
);
372 register_char_driver("spiceport", CHARDEV_BACKEND_KIND_SPICEPORT
,
373 qemu_chr_parse_spice_port
);
376 type_init(register_types
);