gus: use IsaDma interface instead of global DMA_* functions
[qemu/ar7.git] / spice-qemu-char.c
blob8951d7ca3751736bdb796b1d4b05fd3e6ba5bb6e
1 #include "config-host.h"
2 #include "trace.h"
3 #include "ui/qemu-spice.h"
4 #include "sysemu/char.h"
5 #include <spice.h>
6 #include <spice/protocol.h>
8 #include "qemu/osdep.h"
10 typedef struct SpiceCharDriver {
11 CharDriverState* chr;
12 SpiceCharDeviceInstance sin;
13 bool active;
14 bool blocked;
15 const uint8_t *datapos;
16 int datalen;
17 QLIST_ENTRY(SpiceCharDriver) next;
18 } SpiceCharDriver;
20 typedef struct SpiceCharSource {
21 GSource source;
22 SpiceCharDriver *scd;
23 } 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);
31 ssize_t out = 0;
32 ssize_t last_out;
33 uint8_t* p = (uint8_t*)buf;
35 while (len > 0) {
36 int can_write = qemu_chr_be_can_write(scd->chr);
37 last_out = MIN(len, can_write);
38 if (last_out <= 0) {
39 break;
41 qemu_chr_be_write(scd->chr, p, last_out);
42 out += last_out;
43 len -= last_out;
44 p += last_out;
47 trace_spice_vmc_write(out, len + out);
48 return 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);
56 if (bytes > 0) {
57 memcpy(buf, scd->datapos, bytes);
58 scd->datapos += bytes;
59 scd->datalen -= bytes;
60 assert(scd->datalen >= 0);
62 if (scd->datalen == 0) {
63 scd->datapos = 0;
64 scd->blocked = false;
66 trace_spice_vmc_read(bytes, len);
67 return bytes;
70 #if SPICE_SERVER_VERSION >= 0x000c02
71 static void vmc_event(SpiceCharDeviceInstance *sin, uint8_t event)
73 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
74 int chr_event;
76 switch (event) {
77 case SPICE_PORT_EVENT_BREAK:
78 chr_event = CHR_EVENT_BREAK;
79 break;
80 default:
81 return;
84 trace_spice_vmc_event(chr_event);
85 qemu_chr_be_event(scd->chr, chr_event);
87 #endif
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)) {
95 return;
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,
107 .state = vmc_state,
108 .write = vmc_write,
109 .read = vmc_read,
110 #if SPICE_SERVER_VERSION >= 0x000c02
111 .event = vmc_event,
112 #endif
113 #if SPICE_SERVER_VERSION >= 0x000c06
114 .flags = SPICE_CHAR_DEVICE_NOTIFY_WRITABLE,
115 #endif
119 static void vmc_register_interface(SpiceCharDriver *scd)
121 if (scd->active) {
122 return;
124 scd->sin.base.sif = &vmc_interface.base;
125 qemu_spice_add_interface(&scd->sin.base);
126 scd->active = true;
127 trace_spice_vmc_register_interface(scd);
130 static void vmc_unregister_interface(SpiceCharDriver *scd)
132 if (!scd->active) {
133 return;
135 spice_server_remove_interface(&scd->sin.base);
136 scd->active = false;
137 trace_spice_vmc_unregister_interface(scd);
140 static gboolean spice_char_source_prepare(GSource *source, gint *timeout)
142 SpiceCharSource *src = (SpiceCharSource *)source;
144 *timeout = -1;
146 return !src->scd->blocked;
149 static gboolean spice_char_source_check(GSource *source)
151 SpiceCharSource *src = (SpiceCharSource *)source;
153 return !src->scd->blocked;
156 static gboolean spice_char_source_dispatch(GSource *source,
157 GSourceFunc callback, gpointer user_data)
159 GIOFunc func = (GIOFunc)callback;
161 return func(NULL, G_IO_OUT, user_data);
164 static GSourceFuncs SpiceCharSourceFuncs = {
165 .prepare = spice_char_source_prepare,
166 .check = spice_char_source_check,
167 .dispatch = spice_char_source_dispatch,
170 static GSource *spice_chr_add_watch(CharDriverState *chr, GIOCondition cond)
172 SpiceCharDriver *scd = chr->opaque;
173 SpiceCharSource *src;
175 assert(cond & G_IO_OUT);
177 src = (SpiceCharSource *)g_source_new(&SpiceCharSourceFuncs,
178 sizeof(SpiceCharSource));
179 src->scd = scd;
181 return (GSource *)src;
184 static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
186 SpiceCharDriver *s = chr->opaque;
187 int read_bytes;
189 assert(s->datalen == 0);
190 s->datapos = buf;
191 s->datalen = len;
192 spice_server_char_device_wakeup(&s->sin);
193 read_bytes = len - s->datalen;
194 if (read_bytes != len) {
195 /* We'll get passed in the unconsumed data with the next call */
196 s->datalen = 0;
197 s->datapos = NULL;
198 s->blocked = true;
200 return read_bytes;
203 static void spice_chr_close(struct CharDriverState *chr)
205 SpiceCharDriver *s = chr->opaque;
207 vmc_unregister_interface(s);
208 QLIST_REMOVE(s, next);
210 g_free((char *)s->sin.subtype);
211 #if SPICE_SERVER_VERSION >= 0x000c02
212 g_free((char *)s->sin.portname);
213 #endif
214 g_free(s);
217 static void spice_vmc_set_fe_open(struct CharDriverState *chr, int fe_open)
219 SpiceCharDriver *s = chr->opaque;
220 if (fe_open) {
221 vmc_register_interface(s);
222 } else {
223 vmc_unregister_interface(s);
227 static void spice_port_set_fe_open(struct CharDriverState *chr, int fe_open)
229 #if SPICE_SERVER_VERSION >= 0x000c02
230 SpiceCharDriver *s = chr->opaque;
232 if (fe_open) {
233 spice_server_port_event(&s->sin, SPICE_PORT_EVENT_OPENED);
234 } else {
235 spice_server_port_event(&s->sin, SPICE_PORT_EVENT_CLOSED);
237 #endif
240 static void spice_chr_fe_event(struct CharDriverState *chr, int event)
242 #if SPICE_SERVER_VERSION >= 0x000c02
243 SpiceCharDriver *s = chr->opaque;
245 spice_server_port_event(&s->sin, event);
246 #endif
249 static void print_allowed_subtypes(void)
251 const char** psubtype;
252 int i;
254 fprintf(stderr, "allowed names: ");
255 for(i=0, psubtype = spice_server_char_device_recognized_subtypes();
256 *psubtype != NULL; ++psubtype, ++i) {
257 if (i == 0) {
258 fprintf(stderr, "%s", *psubtype);
259 } else {
260 fprintf(stderr, ", %s", *psubtype);
263 fprintf(stderr, "\n");
266 static void spice_chr_accept_input(struct CharDriverState *chr)
268 SpiceCharDriver *s = chr->opaque;
270 spice_server_char_device_wakeup(&s->sin);
273 static CharDriverState *chr_open(const char *subtype,
274 void (*set_fe_open)(struct CharDriverState *,
275 int),
276 ChardevCommon *backend,
277 Error **errp)
279 CharDriverState *chr;
280 SpiceCharDriver *s;
282 chr = qemu_chr_alloc(backend, errp);
283 if (!chr) {
284 return NULL;
286 s = g_malloc0(sizeof(SpiceCharDriver));
287 s->chr = chr;
288 s->active = false;
289 s->sin.subtype = g_strdup(subtype);
290 chr->opaque = s;
291 chr->chr_write = spice_chr_write;
292 chr->chr_add_watch = spice_chr_add_watch;
293 chr->chr_close = spice_chr_close;
294 chr->chr_set_fe_open = set_fe_open;
295 chr->explicit_be_open = true;
296 chr->chr_fe_event = spice_chr_fe_event;
297 chr->chr_accept_input = spice_chr_accept_input;
299 QLIST_INSERT_HEAD(&spice_chars, s, next);
301 return chr;
304 static CharDriverState *qemu_chr_open_spice_vmc(const char *id,
305 ChardevBackend *backend,
306 ChardevReturn *ret,
307 Error **errp)
309 const char *type = backend->u.spicevmc->type;
310 const char **psubtype = spice_server_char_device_recognized_subtypes();
311 ChardevCommon *common = qapi_ChardevSpiceChannel_base(backend->u.spicevmc);
313 for (; *psubtype != NULL; ++psubtype) {
314 if (strcmp(type, *psubtype) == 0) {
315 break;
318 if (*psubtype == NULL) {
319 fprintf(stderr, "spice-qemu-char: unsupported type: %s\n", type);
320 print_allowed_subtypes();
321 return NULL;
324 return chr_open(type, spice_vmc_set_fe_open, common, errp);
327 #if SPICE_SERVER_VERSION >= 0x000c02
328 static CharDriverState *qemu_chr_open_spice_port(const char *id,
329 ChardevBackend *backend,
330 ChardevReturn *ret,
331 Error **errp)
333 const char *name = backend->u.spiceport->fqdn;
334 ChardevCommon *common = qapi_ChardevSpicePort_base(backend->u.spiceport);
335 CharDriverState *chr;
336 SpiceCharDriver *s;
338 if (name == NULL) {
339 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
340 return NULL;
343 chr = chr_open("port", spice_port_set_fe_open, common, errp);
344 if (!chr) {
345 return NULL;
347 s = chr->opaque;
348 s->sin.portname = g_strdup(name);
350 return chr;
353 void qemu_spice_register_ports(void)
355 SpiceCharDriver *s;
357 QLIST_FOREACH(s, &spice_chars, next) {
358 if (s->sin.portname == NULL) {
359 continue;
361 vmc_register_interface(s);
364 #endif
366 static void qemu_chr_parse_spice_vmc(QemuOpts *opts, ChardevBackend *backend,
367 Error **errp)
369 const char *name = qemu_opt_get(opts, "name");
371 if (name == NULL) {
372 error_setg(errp, "chardev: spice channel: no name given");
373 return;
375 backend->u.spicevmc = g_new0(ChardevSpiceChannel, 1);
376 backend->u.spicevmc->type = g_strdup(name);
379 static void qemu_chr_parse_spice_port(QemuOpts *opts, ChardevBackend *backend,
380 Error **errp)
382 const char *name = qemu_opt_get(opts, "name");
384 if (name == NULL) {
385 error_setg(errp, "chardev: spice port: no name given");
386 return;
388 backend->u.spiceport = g_new0(ChardevSpicePort, 1);
389 backend->u.spiceport->fqdn = g_strdup(name);
392 static void register_types(void)
394 register_char_driver("spicevmc", CHARDEV_BACKEND_KIND_SPICEVMC,
395 qemu_chr_parse_spice_vmc, qemu_chr_open_spice_vmc);
396 register_char_driver("spiceport", CHARDEV_BACKEND_KIND_SPICEPORT,
397 qemu_chr_parse_spice_port, qemu_chr_open_spice_port);
400 type_init(register_types);