hw: move executable format header files to hw/
[qemu/ar7.git] / spice-qemu-char.c
blob4b03143f68e7466f0a8e26beab8a3214ba99e3f0
1 #include "config-host.h"
2 #include "trace.h"
3 #include "ui/qemu-spice.h"
4 #include "char/char.h"
5 #include <spice.h>
6 #include <spice-experimental.h>
8 #include "qemu/osdep.h"
10 #define dprintf(_scd, _level, _fmt, ...) \
11 do { \
12 static unsigned __dprintf_counter = 0; \
13 if (_scd->debug >= _level) { \
14 fprintf(stderr, "scd: %3d: " _fmt, ++__dprintf_counter, ## __VA_ARGS__);\
15 } \
16 } while (0)
18 #define VMC_MAX_HOST_WRITE 2048
20 typedef struct SpiceCharDriver {
21 CharDriverState* chr;
22 SpiceCharDeviceInstance sin;
23 char *subtype;
24 bool active;
25 uint8_t *buffer;
26 uint8_t *datapos;
27 ssize_t bufsize, datalen;
28 uint32_t debug;
29 } SpiceCharDriver;
31 static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
33 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
34 ssize_t out = 0;
35 ssize_t last_out;
36 uint8_t* p = (uint8_t*)buf;
38 while (len > 0) {
39 last_out = MIN(len, VMC_MAX_HOST_WRITE);
40 if (qemu_chr_be_can_write(scd->chr) < last_out) {
41 break;
43 qemu_chr_be_write(scd->chr, p, last_out);
44 out += last_out;
45 len -= last_out;
46 p += last_out;
49 dprintf(scd, 3, "%s: %zu/%zd\n", __func__, out, len + out);
50 trace_spice_vmc_write(out, len + out);
51 return 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);
60 if (bytes > 0) {
61 memcpy(buf, scd->datapos, bytes);
62 scd->datapos += bytes;
63 scd->datalen -= bytes;
64 assert(scd->datalen >= 0);
65 if (scd->datalen == 0) {
66 scd->datapos = 0;
69 trace_spice_vmc_read(bytes, len);
70 return bytes;
73 static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
75 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
77 #if SPICE_SERVER_VERSION < 0x000901
79 * spice-server calls the state callback for the agent channel when the
80 * spice client connects / disconnects. Given that not the client but
81 * the server is doing the parsing of the messages this is wrong as the
82 * server is still listening. Worse, this causes the parser in the server
83 * to go out of sync, so we ignore state calls for subtype vdagent
84 * spicevmc chardevs. For the full story see:
85 * http://lists.freedesktop.org/archives/spice-devel/2011-July/004837.html
87 if (strcmp(sin->subtype, "vdagent") == 0) {
88 return;
90 #endif
92 if ((scd->chr->opened && connected) ||
93 (!scd->chr->opened && !connected)) {
94 return;
97 qemu_chr_be_event(scd->chr,
98 connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED);
101 static SpiceCharDeviceInterface vmc_interface = {
102 .base.type = SPICE_INTERFACE_CHAR_DEVICE,
103 .base.description = "spice virtual channel char device",
104 .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
105 .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
106 .state = vmc_state,
107 .write = vmc_write,
108 .read = vmc_read,
112 static void vmc_register_interface(SpiceCharDriver *scd)
114 if (scd->active) {
115 return;
117 dprintf(scd, 1, "%s\n", __func__);
118 scd->sin.base.sif = &vmc_interface.base;
119 qemu_spice_add_interface(&scd->sin.base);
120 scd->active = true;
121 trace_spice_vmc_register_interface(scd);
124 static void vmc_unregister_interface(SpiceCharDriver *scd)
126 if (!scd->active) {
127 return;
129 dprintf(scd, 1, "%s\n", __func__);
130 spice_server_remove_interface(&scd->sin.base);
131 scd->active = false;
132 trace_spice_vmc_unregister_interface(scd);
136 static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
138 SpiceCharDriver *s = chr->opaque;
140 dprintf(s, 2, "%s: %d\n", __func__, len);
141 vmc_register_interface(s);
142 assert(s->datalen == 0);
143 if (s->bufsize < len) {
144 s->bufsize = len;
145 s->buffer = g_realloc(s->buffer, s->bufsize);
147 memcpy(s->buffer, buf, len);
148 s->datapos = s->buffer;
149 s->datalen = len;
150 spice_server_char_device_wakeup(&s->sin);
151 return len;
154 static void spice_chr_close(struct CharDriverState *chr)
156 SpiceCharDriver *s = chr->opaque;
158 printf("%s\n", __func__);
159 vmc_unregister_interface(s);
160 g_free(s);
163 static void spice_chr_guest_open(struct CharDriverState *chr)
165 SpiceCharDriver *s = chr->opaque;
166 vmc_register_interface(s);
169 static void spice_chr_guest_close(struct CharDriverState *chr)
171 SpiceCharDriver *s = chr->opaque;
172 vmc_unregister_interface(s);
175 static void print_allowed_subtypes(void)
177 const char** psubtype;
178 int i;
180 fprintf(stderr, "allowed names: ");
181 for(i=0, psubtype = spice_server_char_device_recognized_subtypes();
182 *psubtype != NULL; ++psubtype, ++i) {
183 if (i == 0) {
184 fprintf(stderr, "%s", *psubtype);
185 } else {
186 fprintf(stderr, ", %s", *psubtype);
189 fprintf(stderr, "\n");
192 CharDriverState *qemu_chr_open_spice(QemuOpts *opts)
194 CharDriverState *chr;
195 SpiceCharDriver *s;
196 const char* name = qemu_opt_get(opts, "name");
197 uint32_t debug = qemu_opt_get_number(opts, "debug", 0);
198 const char** psubtype = spice_server_char_device_recognized_subtypes();
199 const char *subtype = NULL;
201 if (name == NULL) {
202 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
203 print_allowed_subtypes();
204 return NULL;
206 for(;*psubtype != NULL; ++psubtype) {
207 if (strcmp(name, *psubtype) == 0) {
208 subtype = *psubtype;
209 break;
212 if (subtype == NULL) {
213 fprintf(stderr, "spice-qemu-char: unsupported name: %s\n", name);
214 print_allowed_subtypes();
215 return NULL;
218 chr = g_malloc0(sizeof(CharDriverState));
219 s = g_malloc0(sizeof(SpiceCharDriver));
220 s->chr = chr;
221 s->debug = debug;
222 s->active = false;
223 s->sin.subtype = subtype;
224 chr->opaque = s;
225 chr->chr_write = spice_chr_write;
226 chr->chr_close = spice_chr_close;
227 chr->chr_guest_open = spice_chr_guest_open;
228 chr->chr_guest_close = spice_chr_guest_close;
230 #if SPICE_SERVER_VERSION < 0x000901
231 /* See comment in vmc_state() */
232 if (strcmp(subtype, "vdagent") == 0) {
233 qemu_chr_generic_open(chr);
235 #endif
237 return chr;