1 #include "config-host.h"
3 #include "ui/qemu-spice.h"
5 #include <spice-experimental.h>
9 #define dprintf(_scd, _level, _fmt, ...) \
11 static unsigned __dprintf_counter = 0; \
12 if (_scd->debug >= _level) { \
13 fprintf(stderr, "scd: %3d: " _fmt, ++__dprintf_counter, ## __VA_ARGS__);\
17 #define VMC_MAX_HOST_WRITE 2048
19 typedef struct SpiceCharDriver
{
21 SpiceCharDeviceInstance sin
;
26 ssize_t bufsize
, datalen
;
30 static int vmc_write(SpiceCharDeviceInstance
*sin
, const uint8_t *buf
, int len
)
32 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
35 uint8_t* p
= (uint8_t*)buf
;
38 last_out
= MIN(len
, VMC_MAX_HOST_WRITE
);
39 qemu_chr_read(scd
->chr
, p
, last_out
);
49 dprintf(scd
, 3, "%s: %lu/%zd\n", __func__
, out
, len
+ out
);
50 trace_spice_vmc_write(out
, len
+ out
);
54 static int vmc_read(SpiceCharDeviceInstance
*sin
, uint8_t *buf
, int len
)
56 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
57 int bytes
= MIN(len
, scd
->datalen
);
59 dprintf(scd
, 2, "%s: %p %d/%d/%zd\n", __func__
, scd
->datapos
, len
, bytes
, scd
->datalen
);
61 memcpy(buf
, scd
->datapos
, bytes
);
62 scd
->datapos
+= bytes
;
63 scd
->datalen
-= bytes
;
64 assert(scd
->datalen
>= 0);
65 if (scd
->datalen
== 0) {
69 trace_spice_vmc_read(bytes
, len
);
73 static SpiceCharDeviceInterface vmc_interface
= {
74 .base
.type
= SPICE_INTERFACE_CHAR_DEVICE
,
75 .base
.description
= "spice virtual channel char device",
76 .base
.major_version
= SPICE_INTERFACE_CHAR_DEVICE_MAJOR
,
77 .base
.minor_version
= SPICE_INTERFACE_CHAR_DEVICE_MINOR
,
83 static void vmc_register_interface(SpiceCharDriver
*scd
)
88 dprintf(scd
, 1, "%s\n", __func__
);
89 scd
->sin
.base
.sif
= &vmc_interface
.base
;
90 qemu_spice_add_interface(&scd
->sin
.base
);
92 trace_spice_vmc_register_interface(scd
);
95 static void vmc_unregister_interface(SpiceCharDriver
*scd
)
100 dprintf(scd
, 1, "%s\n", __func__
);
101 spice_server_remove_interface(&scd
->sin
.base
);
103 trace_spice_vmc_unregister_interface(scd
);
107 static int spice_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
109 SpiceCharDriver
*s
= chr
->opaque
;
111 dprintf(s
, 2, "%s: %d\n", __func__
, len
);
112 vmc_register_interface(s
);
113 assert(s
->datalen
== 0);
114 if (s
->bufsize
< len
) {
116 s
->buffer
= qemu_realloc(s
->buffer
, s
->bufsize
);
118 memcpy(s
->buffer
, buf
, len
);
119 s
->datapos
= s
->buffer
;
121 spice_server_char_device_wakeup(&s
->sin
);
125 static void spice_chr_close(struct CharDriverState
*chr
)
127 SpiceCharDriver
*s
= chr
->opaque
;
129 printf("%s\n", __func__
);
130 vmc_unregister_interface(s
);
134 static void print_allowed_subtypes(void)
136 const char** psubtype
;
139 fprintf(stderr
, "allowed names: ");
140 for(i
=0, psubtype
= spice_server_char_device_recognized_subtypes();
141 *psubtype
!= NULL
; ++psubtype
, ++i
) {
143 fprintf(stderr
, "%s", *psubtype
);
145 fprintf(stderr
, ", %s", *psubtype
);
148 fprintf(stderr
, "\n");
151 CharDriverState
*qemu_chr_open_spice(QemuOpts
*opts
)
153 CharDriverState
*chr
;
155 const char* name
= qemu_opt_get(opts
, "name");
156 uint32_t debug
= qemu_opt_get_number(opts
, "debug", 0);
157 const char** psubtype
= spice_server_char_device_recognized_subtypes();
158 const char *subtype
= NULL
;
161 fprintf(stderr
, "spice-qemu-char: missing name parameter\n");
162 print_allowed_subtypes();
165 for(;*psubtype
!= NULL
; ++psubtype
) {
166 if (strcmp(name
, *psubtype
) == 0) {
171 if (subtype
== NULL
) {
172 fprintf(stderr
, "spice-qemu-char: unsupported name\n");
173 print_allowed_subtypes();
177 chr
= qemu_mallocz(sizeof(CharDriverState
));
178 s
= qemu_mallocz(sizeof(SpiceCharDriver
));
182 s
->sin
.subtype
= subtype
;
184 chr
->chr_write
= spice_chr_write
;
185 chr
->chr_close
= spice_chr_close
;
187 qemu_chr_generic_open(chr
);