1 #include "config-host.h"
3 #include "ui/qemu-spice.h"
4 #include "sysemu/char.h"
6 #include <spice-experimental.h>
7 #include <spice/protocol.h>
9 #include "qemu/osdep.h"
11 typedef struct SpiceCharDriver
{
13 SpiceCharDeviceInstance sin
;
18 ssize_t bufsize
, datalen
;
19 QLIST_ENTRY(SpiceCharDriver
) next
;
22 static QLIST_HEAD(, SpiceCharDriver
) spice_chars
=
23 QLIST_HEAD_INITIALIZER(spice_chars
);
25 static int vmc_write(SpiceCharDeviceInstance
*sin
, const uint8_t *buf
, int len
)
27 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
30 uint8_t* p
= (uint8_t*)buf
;
33 last_out
= MIN(len
, qemu_chr_be_can_write(scd
->chr
));
37 qemu_chr_be_write(scd
->chr
, p
, last_out
);
43 trace_spice_vmc_write(out
, len
+ out
);
47 static int vmc_read(SpiceCharDeviceInstance
*sin
, uint8_t *buf
, int len
)
49 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
50 int bytes
= MIN(len
, scd
->datalen
);
53 memcpy(buf
, scd
->datapos
, bytes
);
54 scd
->datapos
+= bytes
;
55 scd
->datalen
-= bytes
;
56 assert(scd
->datalen
>= 0);
57 if (scd
->datalen
== 0) {
61 trace_spice_vmc_read(bytes
, len
);
65 #if SPICE_SERVER_VERSION >= 0x000c02
66 static void vmc_event(SpiceCharDeviceInstance
*sin
, uint8_t event
)
68 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
72 case SPICE_PORT_EVENT_BREAK
:
73 chr_event
= CHR_EVENT_BREAK
;
79 trace_spice_vmc_event(chr_event
);
80 qemu_chr_be_event(scd
->chr
, chr_event
);
84 static void vmc_state(SpiceCharDeviceInstance
*sin
, int connected
)
86 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
88 if ((scd
->chr
->be_open
&& connected
) ||
89 (!scd
->chr
->be_open
&& !connected
)) {
93 qemu_chr_be_event(scd
->chr
,
94 connected
? CHR_EVENT_OPENED
: CHR_EVENT_CLOSED
);
97 static SpiceCharDeviceInterface vmc_interface
= {
98 .base
.type
= SPICE_INTERFACE_CHAR_DEVICE
,
99 .base
.description
= "spice virtual channel char device",
100 .base
.major_version
= SPICE_INTERFACE_CHAR_DEVICE_MAJOR
,
101 .base
.minor_version
= SPICE_INTERFACE_CHAR_DEVICE_MINOR
,
105 #if SPICE_SERVER_VERSION >= 0x000c02
111 static void vmc_register_interface(SpiceCharDriver
*scd
)
116 scd
->sin
.base
.sif
= &vmc_interface
.base
;
117 qemu_spice_add_interface(&scd
->sin
.base
);
119 trace_spice_vmc_register_interface(scd
);
122 static void vmc_unregister_interface(SpiceCharDriver
*scd
)
127 spice_server_remove_interface(&scd
->sin
.base
);
129 trace_spice_vmc_unregister_interface(scd
);
133 static int spice_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
135 SpiceCharDriver
*s
= chr
->opaque
;
137 assert(s
->datalen
== 0);
138 if (s
->bufsize
< len
) {
140 s
->buffer
= g_realloc(s
->buffer
, s
->bufsize
);
142 memcpy(s
->buffer
, buf
, len
);
143 s
->datapos
= s
->buffer
;
145 spice_server_char_device_wakeup(&s
->sin
);
149 static void spice_chr_close(struct CharDriverState
*chr
)
151 SpiceCharDriver
*s
= chr
->opaque
;
153 vmc_unregister_interface(s
);
154 QLIST_REMOVE(s
, next
);
156 g_free((char *)s
->sin
.subtype
);
157 #if SPICE_SERVER_VERSION >= 0x000c02
158 g_free((char *)s
->sin
.portname
);
163 static void spice_chr_set_fe_open(struct CharDriverState
*chr
, int fe_open
)
165 SpiceCharDriver
*s
= chr
->opaque
;
167 vmc_register_interface(s
);
169 vmc_unregister_interface(s
);
173 static void print_allowed_subtypes(void)
175 const char** psubtype
;
178 fprintf(stderr
, "allowed names: ");
179 for(i
=0, psubtype
= spice_server_char_device_recognized_subtypes();
180 *psubtype
!= NULL
; ++psubtype
, ++i
) {
182 fprintf(stderr
, "%s", *psubtype
);
184 fprintf(stderr
, ", %s", *psubtype
);
187 fprintf(stderr
, "\n");
190 static CharDriverState
*chr_open(const char *subtype
)
192 CharDriverState
*chr
;
195 chr
= g_malloc0(sizeof(CharDriverState
));
196 s
= g_malloc0(sizeof(SpiceCharDriver
));
199 s
->sin
.subtype
= g_strdup(subtype
);
201 chr
->chr_write
= spice_chr_write
;
202 chr
->chr_close
= spice_chr_close
;
203 chr
->chr_set_fe_open
= spice_chr_set_fe_open
;
205 QLIST_INSERT_HEAD(&spice_chars
, s
, next
);
210 CharDriverState
*qemu_chr_open_spice_vmc(const char *type
)
212 const char **psubtype
= spice_server_char_device_recognized_subtypes();
215 fprintf(stderr
, "spice-qemu-char: missing name parameter\n");
216 print_allowed_subtypes();
219 for (; *psubtype
!= NULL
; ++psubtype
) {
220 if (strcmp(type
, *psubtype
) == 0) {
224 if (*psubtype
== NULL
) {
225 fprintf(stderr
, "spice-qemu-char: unsupported type: %s\n", type
);
226 print_allowed_subtypes();
230 return chr_open(type
);
233 #if SPICE_SERVER_VERSION >= 0x000c02
234 CharDriverState
*qemu_chr_open_spice_port(const char *name
)
236 CharDriverState
*chr
;
240 fprintf(stderr
, "spice-qemu-char: missing name parameter\n");
244 chr
= chr_open("port");
246 s
->sin
.portname
= g_strdup(name
);
251 void qemu_spice_register_ports(void)
255 QLIST_FOREACH(s
, &spice_chars
, next
) {
256 if (s
->sin
.portname
== NULL
) {
259 vmc_register_interface(s
);
264 static void qemu_chr_parse_spice_vmc(QemuOpts
*opts
, ChardevBackend
*backend
,
267 const char *name
= qemu_opt_get(opts
, "name");
270 error_setg(errp
, "chardev: spice channel: no name given");
273 backend
->spicevmc
= g_new0(ChardevSpiceChannel
, 1);
274 backend
->spicevmc
->type
= g_strdup(name
);
277 static void qemu_chr_parse_spice_port(QemuOpts
*opts
, ChardevBackend
*backend
,
280 const char *name
= qemu_opt_get(opts
, "name");
283 error_setg(errp
, "chardev: spice port: no name given");
286 backend
->spiceport
= g_new0(ChardevSpicePort
, 1);
287 backend
->spiceport
->fqdn
= g_strdup(name
);
290 static void register_types(void)
292 register_char_driver_qapi("spicevmc", CHARDEV_BACKEND_KIND_SPICEVMC
,
293 qemu_chr_parse_spice_vmc
);
294 register_char_driver_qapi("spiceport", CHARDEV_BACKEND_KIND_SPICEPORT
,
295 qemu_chr_parse_spice_port
);
298 type_init(register_types
);