]> git.proxmox.com Git - qemu.git/blame - hw/usb/redirect.c
usb-redir: Add support for migration
[qemu.git] / hw / usb / redirect.c
CommitLineData
69354a83
HG
1/*
2 * USB redirector usb-guest
3 *
cb897117 4 * Copyright (c) 2011-2012 Red Hat, Inc.
69354a83
HG
5 *
6 * Red Hat Authors:
7 * Hans de Goede <hdegoede@redhat.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
28#include "qemu-common.h"
29#include "qemu-timer.h"
30#include "monitor.h"
31#include "sysemu.h"
32
33#include <dirent.h>
34#include <sys/ioctl.h>
35#include <signal.h>
36#include <usbredirparser.h>
6af16589 37#include <usbredirfilter.h>
69354a83
HG
38
39#include "hw/usb.h"
40
41#define MAX_ENDPOINTS 32
1510168e 42#define NO_INTERFACE_INFO 255 /* Valid interface_count always <= 32 */
69354a83
HG
43#define EP2I(ep_address) (((ep_address & 0x80) >> 3) | (ep_address & 0x0f))
44#define I2EP(i) (((i & 0x10) << 3) | (i & 0x0f))
45
69354a83
HG
46typedef struct USBRedirDevice USBRedirDevice;
47
48/* Struct to hold buffered packets (iso or int input packets) */
49struct buf_packet {
50 uint8_t *data;
51 int len;
52 int status;
53 QTAILQ_ENTRY(buf_packet)next;
54};
55
56struct endp_data {
57 uint8_t type;
58 uint8_t interval;
59 uint8_t interface; /* bInterfaceNumber this ep belongs to */
3f4be328 60 uint16_t max_packet_size; /* In bytes, not wMaxPacketSize format !! */
69354a83
HG
61 uint8_t iso_started;
62 uint8_t iso_error; /* For reporting iso errors to the HC */
63 uint8_t interrupt_started;
64 uint8_t interrupt_error;
e1537884 65 uint8_t bufpq_prefilled;
81fd7b74 66 uint8_t bufpq_dropping_packets;
69354a83 67 QTAILQ_HEAD(, buf_packet) bufpq;
fc3f6e1b
HG
68 int32_t bufpq_size;
69 int32_t bufpq_target_size;
69354a83
HG
70};
71
8e60452a
HG
72struct PacketIdQueueEntry {
73 uint64_t id;
74 QTAILQ_ENTRY(PacketIdQueueEntry)next;
75};
76
77struct PacketIdQueue {
78 USBRedirDevice *dev;
79 const char *name;
80 QTAILQ_HEAD(, PacketIdQueueEntry) head;
81 int size;
82};
83
69354a83
HG
84struct USBRedirDevice {
85 USBDevice dev;
86 /* Properties */
87 CharDriverState *cs;
88 uint8_t debug;
6af16589 89 char *filter_str;
65bb3a5c 90 int32_t bootindex;
69354a83
HG
91 /* Data passed from chardev the fd_read cb to the usbredirparser read cb */
92 const uint8_t *read_buf;
93 int read_buf_size;
ed9873bf
HG
94 /* For async handling of close */
95 QEMUBH *chardev_close_bh;
69354a83
HG
96 /* To delay the usb attach in case of quick chardev close + open */
97 QEMUTimer *attach_timer;
98 int64_t next_attach_time;
99 struct usbredirparser *parser;
100 struct endp_data endpoint[MAX_ENDPOINTS];
8e60452a 101 struct PacketIdQueue cancelled;
9a8d4067 102 struct PacketIdQueue already_in_flight;
6af16589
HG
103 /* Data for device filtering */
104 struct usb_redir_device_connect_header device_info;
105 struct usb_redir_interface_info_header interface_info;
106 struct usbredirfilter_rule *filter_rules;
107 int filter_rules_count;
69354a83
HG
108};
109
097a66ef 110static void usbredir_hello(void *priv, struct usb_redir_hello_header *h);
69354a83
HG
111static void usbredir_device_connect(void *priv,
112 struct usb_redir_device_connect_header *device_connect);
113static void usbredir_device_disconnect(void *priv);
114static void usbredir_interface_info(void *priv,
115 struct usb_redir_interface_info_header *interface_info);
116static void usbredir_ep_info(void *priv,
117 struct usb_redir_ep_info_header *ep_info);
be4a8928 118static void usbredir_configuration_status(void *priv, uint64_t id,
69354a83 119 struct usb_redir_configuration_status_header *configuration_status);
be4a8928 120static void usbredir_alt_setting_status(void *priv, uint64_t id,
69354a83 121 struct usb_redir_alt_setting_status_header *alt_setting_status);
be4a8928 122static void usbredir_iso_stream_status(void *priv, uint64_t id,
69354a83 123 struct usb_redir_iso_stream_status_header *iso_stream_status);
be4a8928 124static void usbredir_interrupt_receiving_status(void *priv, uint64_t id,
69354a83
HG
125 struct usb_redir_interrupt_receiving_status_header
126 *interrupt_receiving_status);
be4a8928 127static void usbredir_bulk_streams_status(void *priv, uint64_t id,
69354a83 128 struct usb_redir_bulk_streams_status_header *bulk_streams_status);
be4a8928 129static void usbredir_control_packet(void *priv, uint64_t id,
69354a83
HG
130 struct usb_redir_control_packet_header *control_packet,
131 uint8_t *data, int data_len);
be4a8928 132static void usbredir_bulk_packet(void *priv, uint64_t id,
69354a83
HG
133 struct usb_redir_bulk_packet_header *bulk_packet,
134 uint8_t *data, int data_len);
be4a8928 135static void usbredir_iso_packet(void *priv, uint64_t id,
69354a83
HG
136 struct usb_redir_iso_packet_header *iso_packet,
137 uint8_t *data, int data_len);
be4a8928 138static void usbredir_interrupt_packet(void *priv, uint64_t id,
69354a83
HG
139 struct usb_redir_interrupt_packet_header *interrupt_header,
140 uint8_t *data, int data_len);
141
142static int usbredir_handle_status(USBRedirDevice *dev,
143 int status, int actual_len);
144
69354a83
HG
145/*
146 * Logging stuff
147 */
148
149#define ERROR(...) \
150 do { \
151 if (dev->debug >= usbredirparser_error) { \
152 error_report("usb-redir error: " __VA_ARGS__); \
153 } \
154 } while (0)
155#define WARNING(...) \
156 do { \
157 if (dev->debug >= usbredirparser_warning) { \
158 error_report("usb-redir warning: " __VA_ARGS__); \
159 } \
160 } while (0)
161#define INFO(...) \
162 do { \
163 if (dev->debug >= usbredirparser_info) { \
164 error_report("usb-redir: " __VA_ARGS__); \
165 } \
166 } while (0)
167#define DPRINTF(...) \
168 do { \
169 if (dev->debug >= usbredirparser_debug) { \
170 error_report("usb-redir: " __VA_ARGS__); \
171 } \
172 } while (0)
173#define DPRINTF2(...) \
174 do { \
175 if (dev->debug >= usbredirparser_debug_data) { \
176 error_report("usb-redir: " __VA_ARGS__); \
177 } \
178 } while (0)
179
180static void usbredir_log(void *priv, int level, const char *msg)
181{
182 USBRedirDevice *dev = priv;
183
184 if (dev->debug < level) {
185 return;
186 }
187
be62a2eb 188 error_report("%s", msg);
69354a83
HG
189}
190
191static void usbredir_log_data(USBRedirDevice *dev, const char *desc,
192 const uint8_t *data, int len)
193{
194 int i, j, n;
195
196 if (dev->debug < usbredirparser_debug_data) {
197 return;
198 }
199
200 for (i = 0; i < len; i += j) {
201 char buf[128];
202
203 n = sprintf(buf, "%s", desc);
204 for (j = 0; j < 8 && i + j < len; j++) {
205 n += sprintf(buf + n, " %02X", data[i + j]);
206 }
be62a2eb 207 error_report("%s", buf);
69354a83
HG
208 }
209}
210
211/*
212 * usbredirparser io functions
213 */
214
215static int usbredir_read(void *priv, uint8_t *data, int count)
216{
217 USBRedirDevice *dev = priv;
218
219 if (dev->read_buf_size < count) {
220 count = dev->read_buf_size;
221 }
222
223 memcpy(data, dev->read_buf, count);
224
225 dev->read_buf_size -= count;
226 if (dev->read_buf_size) {
227 dev->read_buf += count;
228 } else {
229 dev->read_buf = NULL;
230 }
231
232 return count;
233}
234
235static int usbredir_write(void *priv, uint8_t *data, int count)
236{
237 USBRedirDevice *dev = priv;
238
c1b71a1d
HG
239 if (!dev->cs->opened) {
240 return 0;
241 }
242
fc3f6e1b
HG
243 /* Don't send new data to the chardev until our state is fully synced */
244 if (!runstate_check(RUN_STATE_RUNNING)) {
245 return 0;
246 }
247
2cc6e0a1 248 return qemu_chr_fe_write(dev->cs, data, count);
69354a83
HG
249}
250
251/*
de550a6a 252 * Cancelled and buffered packets helpers
69354a83
HG
253 */
254
8e60452a
HG
255static void packet_id_queue_init(struct PacketIdQueue *q,
256 USBRedirDevice *dev, const char *name)
69354a83 257{
8e60452a
HG
258 q->dev = dev;
259 q->name = name;
260 QTAILQ_INIT(&q->head);
261 q->size = 0;
262}
263
264static void packet_id_queue_add(struct PacketIdQueue *q, uint64_t id)
265{
266 USBRedirDevice *dev = q->dev;
267 struct PacketIdQueueEntry *e;
69354a83 268
8e60452a
HG
269 DPRINTF("adding packet id %"PRIu64" to %s queue\n", id, q->name);
270
271 e = g_malloc0(sizeof(struct PacketIdQueueEntry));
272 e->id = id;
273 QTAILQ_INSERT_TAIL(&q->head, e, next);
274 q->size++;
275}
69354a83 276
8e60452a
HG
277static int packet_id_queue_remove(struct PacketIdQueue *q, uint64_t id)
278{
279 USBRedirDevice *dev = q->dev;
280 struct PacketIdQueueEntry *e;
de550a6a 281
8e60452a
HG
282 QTAILQ_FOREACH(e, &q->head, next) {
283 if (e->id == id) {
284 DPRINTF("removing packet id %"PRIu64" from %s queue\n",
285 id, q->name);
286 QTAILQ_REMOVE(&q->head, e, next);
287 q->size--;
288 g_free(e);
289 return 1;
290 }
291 }
292 return 0;
293}
294
295static void packet_id_queue_empty(struct PacketIdQueue *q)
296{
297 USBRedirDevice *dev = q->dev;
298 struct PacketIdQueueEntry *e, *next_e;
299
300 DPRINTF("removing %d packet-ids from %s queue\n", q->size, q->name);
301
302 QTAILQ_FOREACH_SAFE(e, &q->head, next, next_e) {
303 QTAILQ_REMOVE(&q->head, e, next);
304 g_free(e);
305 }
306 q->size = 0;
307}
308
309static void usbredir_cancel_packet(USBDevice *udev, USBPacket *p)
310{
311 USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev);
312
313 packet_id_queue_add(&dev->cancelled, p->id);
de550a6a
HG
314 usbredirparser_send_cancel_data_packet(dev->parser, p->id);
315 usbredirparser_do_write(dev->parser);
69354a83
HG
316}
317
de550a6a 318static int usbredir_is_cancelled(USBRedirDevice *dev, uint64_t id)
69354a83 319{
de550a6a
HG
320 if (!dev->dev.attached) {
321 return 1; /* Treat everything as cancelled after a disconnect */
322 }
8e60452a 323 return packet_id_queue_remove(&dev->cancelled, id);
69354a83
HG
324}
325
9a8d4067
HG
326static void usbredir_fill_already_in_flight_from_ep(USBRedirDevice *dev,
327 struct USBEndpoint *ep)
328{
329 static USBPacket *p;
330
331 QTAILQ_FOREACH(p, &ep->queue, queue) {
332 packet_id_queue_add(&dev->already_in_flight, p->id);
333 }
334}
335
336static void usbredir_fill_already_in_flight(USBRedirDevice *dev)
337{
338 int ep;
339 struct USBDevice *udev = &dev->dev;
340
341 usbredir_fill_already_in_flight_from_ep(dev, &udev->ep_ctl);
342
343 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
344 usbredir_fill_already_in_flight_from_ep(dev, &udev->ep_in[ep]);
345 usbredir_fill_already_in_flight_from_ep(dev, &udev->ep_out[ep]);
346 }
347}
348
349static int usbredir_already_in_flight(USBRedirDevice *dev, uint64_t id)
350{
351 return packet_id_queue_remove(&dev->already_in_flight, id);
352}
353
de550a6a
HG
354static USBPacket *usbredir_find_packet_by_id(USBRedirDevice *dev,
355 uint8_t ep, uint64_t id)
69354a83 356{
de550a6a 357 USBPacket *p;
69354a83 358
de550a6a
HG
359 if (usbredir_is_cancelled(dev, id)) {
360 return NULL;
361 }
69354a83 362
de550a6a
HG
363 p = usb_ep_find_packet_by_id(&dev->dev,
364 (ep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT,
365 ep & 0x0f, id);
366 if (p == NULL) {
367 ERROR("could not find packet with id %"PRIu64"\n", id);
69354a83 368 }
de550a6a 369 return p;
69354a83
HG
370}
371
81fd7b74 372static void bufp_alloc(USBRedirDevice *dev,
69354a83
HG
373 uint8_t *data, int len, int status, uint8_t ep)
374{
81fd7b74
HG
375 struct buf_packet *bufp;
376
377 if (!dev->endpoint[EP2I(ep)].bufpq_dropping_packets &&
378 dev->endpoint[EP2I(ep)].bufpq_size >
379 2 * dev->endpoint[EP2I(ep)].bufpq_target_size) {
380 DPRINTF("bufpq overflow, dropping packets ep %02X\n", ep);
381 dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 1;
382 }
383 /* Since we're interupting the stream anyways, drop enough packets to get
384 back to our target buffer size */
385 if (dev->endpoint[EP2I(ep)].bufpq_dropping_packets) {
386 if (dev->endpoint[EP2I(ep)].bufpq_size >
387 dev->endpoint[EP2I(ep)].bufpq_target_size) {
388 free(data);
389 return;
390 }
391 dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0;
392 }
393
394 bufp = g_malloc(sizeof(struct buf_packet));
69354a83
HG
395 bufp->data = data;
396 bufp->len = len;
397 bufp->status = status;
398 QTAILQ_INSERT_TAIL(&dev->endpoint[EP2I(ep)].bufpq, bufp, next);
e1537884 399 dev->endpoint[EP2I(ep)].bufpq_size++;
69354a83
HG
400}
401
402static void bufp_free(USBRedirDevice *dev, struct buf_packet *bufp,
403 uint8_t ep)
404{
405 QTAILQ_REMOVE(&dev->endpoint[EP2I(ep)].bufpq, bufp, next);
e1537884 406 dev->endpoint[EP2I(ep)].bufpq_size--;
69354a83 407 free(bufp->data);
7267c094 408 g_free(bufp);
69354a83
HG
409}
410
411static void usbredir_free_bufpq(USBRedirDevice *dev, uint8_t ep)
412{
413 struct buf_packet *buf, *buf_next;
414
415 QTAILQ_FOREACH_SAFE(buf, &dev->endpoint[EP2I(ep)].bufpq, next, buf_next) {
416 bufp_free(dev, buf, ep);
417 }
418}
419
420/*
421 * USBDevice callbacks
422 */
423
424static void usbredir_handle_reset(USBDevice *udev)
425{
426 USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev);
427
428 DPRINTF("reset device\n");
429 usbredirparser_send_reset(dev->parser);
430 usbredirparser_do_write(dev->parser);
431}
432
433static int usbredir_handle_iso_data(USBRedirDevice *dev, USBPacket *p,
434 uint8_t ep)
435{
436 int status, len;
69354a83
HG
437 if (!dev->endpoint[EP2I(ep)].iso_started &&
438 !dev->endpoint[EP2I(ep)].iso_error) {
439 struct usb_redir_start_iso_stream_header start_iso = {
440 .endpoint = ep,
69354a83 441 };
e8a7dd29
HG
442 int pkts_per_sec;
443
444 if (dev->dev.speed == USB_SPEED_HIGH) {
445 pkts_per_sec = 8000 / dev->endpoint[EP2I(ep)].interval;
446 } else {
447 pkts_per_sec = 1000 / dev->endpoint[EP2I(ep)].interval;
448 }
449 /* Testing has shown that we need circa 60 ms buffer */
450 dev->endpoint[EP2I(ep)].bufpq_target_size = (pkts_per_sec * 60) / 1000;
451
452 /* Aim for approx 100 interrupts / second on the client to
453 balance latency and interrupt load */
454 start_iso.pkts_per_urb = pkts_per_sec / 100;
455 if (start_iso.pkts_per_urb < 1) {
456 start_iso.pkts_per_urb = 1;
457 } else if (start_iso.pkts_per_urb > 32) {
458 start_iso.pkts_per_urb = 32;
459 }
460
461 start_iso.no_urbs = (dev->endpoint[EP2I(ep)].bufpq_target_size +
462 start_iso.pkts_per_urb - 1) /
463 start_iso.pkts_per_urb;
464 /* Output endpoints pre-fill only 1/2 of the packets, keeping the rest
465 as overflow buffer. Also see the usbredir protocol documentation */
466 if (!(ep & USB_DIR_IN)) {
467 start_iso.no_urbs *= 2;
468 }
469 if (start_iso.no_urbs > 16) {
470 start_iso.no_urbs = 16;
471 }
472
69354a83
HG
473 /* No id, we look at the ep when receiving a status back */
474 usbredirparser_send_start_iso_stream(dev->parser, 0, &start_iso);
475 usbredirparser_do_write(dev->parser);
32213543
HG
476 DPRINTF("iso stream started pkts/sec %d pkts/urb %d urbs %d ep %02X\n",
477 pkts_per_sec, start_iso.pkts_per_urb, start_iso.no_urbs, ep);
69354a83 478 dev->endpoint[EP2I(ep)].iso_started = 1;
e1537884 479 dev->endpoint[EP2I(ep)].bufpq_prefilled = 0;
81fd7b74 480 dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0;
69354a83
HG
481 }
482
483 if (ep & USB_DIR_IN) {
484 struct buf_packet *isop;
485
e1537884
HG
486 if (dev->endpoint[EP2I(ep)].iso_started &&
487 !dev->endpoint[EP2I(ep)].bufpq_prefilled) {
488 if (dev->endpoint[EP2I(ep)].bufpq_size <
489 dev->endpoint[EP2I(ep)].bufpq_target_size) {
490 return usbredir_handle_status(dev, 0, 0);
491 }
492 dev->endpoint[EP2I(ep)].bufpq_prefilled = 1;
493 }
494
69354a83
HG
495 isop = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq);
496 if (isop == NULL) {
32213543
HG
497 DPRINTF("iso-token-in ep %02X, no isop, iso_error: %d\n",
498 ep, dev->endpoint[EP2I(ep)].iso_error);
e1537884
HG
499 /* Re-fill the buffer */
500 dev->endpoint[EP2I(ep)].bufpq_prefilled = 0;
69354a83
HG
501 /* Check iso_error for stream errors, otherwise its an underrun */
502 status = dev->endpoint[EP2I(ep)].iso_error;
503 dev->endpoint[EP2I(ep)].iso_error = 0;
d61000a8 504 return status ? USB_RET_IOERROR : 0;
69354a83 505 }
32213543
HG
506 DPRINTF2("iso-token-in ep %02X status %d len %d queue-size: %d\n", ep,
507 isop->status, isop->len, dev->endpoint[EP2I(ep)].bufpq_size);
69354a83
HG
508
509 status = isop->status;
510 if (status != usb_redir_success) {
511 bufp_free(dev, isop, ep);
d61000a8 512 return USB_RET_IOERROR;
69354a83
HG
513 }
514
515 len = isop->len;
4f4321c1 516 if (len > p->iov.size) {
32213543
HG
517 ERROR("received iso data is larger then packet ep %02X (%d > %d)\n",
518 ep, len, (int)p->iov.size);
69354a83 519 bufp_free(dev, isop, ep);
4d819a9b 520 return USB_RET_BABBLE;
69354a83 521 }
4f4321c1 522 usb_packet_copy(p, isop->data, len);
69354a83
HG
523 bufp_free(dev, isop, ep);
524 return len;
525 } else {
526 /* If the stream was not started because of a pending error don't
527 send the packet to the usb-host */
528 if (dev->endpoint[EP2I(ep)].iso_started) {
529 struct usb_redir_iso_packet_header iso_packet = {
530 .endpoint = ep,
4f4321c1 531 .length = p->iov.size
69354a83 532 };
4f4321c1 533 uint8_t buf[p->iov.size];
69354a83 534 /* No id, we look at the ep when receiving a status back */
4f4321c1 535 usb_packet_copy(p, buf, p->iov.size);
69354a83 536 usbredirparser_send_iso_packet(dev->parser, 0, &iso_packet,
4f4321c1 537 buf, p->iov.size);
69354a83
HG
538 usbredirparser_do_write(dev->parser);
539 }
540 status = dev->endpoint[EP2I(ep)].iso_error;
541 dev->endpoint[EP2I(ep)].iso_error = 0;
4f4321c1
GH
542 DPRINTF2("iso-token-out ep %02X status %d len %zd\n", ep, status,
543 p->iov.size);
544 return usbredir_handle_status(dev, status, p->iov.size);
69354a83
HG
545 }
546}
547
548static void usbredir_stop_iso_stream(USBRedirDevice *dev, uint8_t ep)
549{
550 struct usb_redir_stop_iso_stream_header stop_iso_stream = {
551 .endpoint = ep
552 };
553 if (dev->endpoint[EP2I(ep)].iso_started) {
554 usbredirparser_send_stop_iso_stream(dev->parser, 0, &stop_iso_stream);
555 DPRINTF("iso stream stopped ep %02X\n", ep);
556 dev->endpoint[EP2I(ep)].iso_started = 0;
557 }
2bd836e5 558 dev->endpoint[EP2I(ep)].iso_error = 0;
69354a83
HG
559 usbredir_free_bufpq(dev, ep);
560}
561
562static int usbredir_handle_bulk_data(USBRedirDevice *dev, USBPacket *p,
563 uint8_t ep)
564{
69354a83
HG
565 struct usb_redir_bulk_packet_header bulk_packet;
566
de550a6a 567 DPRINTF("bulk-out ep %02X len %zd id %"PRIu64"\n", ep, p->iov.size, p->id);
69354a83 568
9a8d4067
HG
569 if (usbredir_already_in_flight(dev, p->id)) {
570 return USB_RET_ASYNC;
571 }
572
69354a83 573 bulk_packet.endpoint = ep;
4f4321c1 574 bulk_packet.length = p->iov.size;
69354a83 575 bulk_packet.stream_id = 0;
69354a83
HG
576
577 if (ep & USB_DIR_IN) {
de550a6a 578 usbredirparser_send_bulk_packet(dev->parser, p->id,
69354a83
HG
579 &bulk_packet, NULL, 0);
580 } else {
4f4321c1
GH
581 uint8_t buf[p->iov.size];
582 usb_packet_copy(p, buf, p->iov.size);
583 usbredir_log_data(dev, "bulk data out:", buf, p->iov.size);
de550a6a 584 usbredirparser_send_bulk_packet(dev->parser, p->id,
4f4321c1 585 &bulk_packet, buf, p->iov.size);
69354a83
HG
586 }
587 usbredirparser_do_write(dev->parser);
588 return USB_RET_ASYNC;
589}
590
591static int usbredir_handle_interrupt_data(USBRedirDevice *dev,
592 USBPacket *p, uint8_t ep)
593{
594 if (ep & USB_DIR_IN) {
595 /* Input interrupt endpoint, buffered packet input */
596 struct buf_packet *intp;
597 int status, len;
598
599 if (!dev->endpoint[EP2I(ep)].interrupt_started &&
600 !dev->endpoint[EP2I(ep)].interrupt_error) {
601 struct usb_redir_start_interrupt_receiving_header start_int = {
602 .endpoint = ep,
603 };
604 /* No id, we look at the ep when receiving a status back */
605 usbredirparser_send_start_interrupt_receiving(dev->parser, 0,
606 &start_int);
607 usbredirparser_do_write(dev->parser);
608 DPRINTF("interrupt recv started ep %02X\n", ep);
609 dev->endpoint[EP2I(ep)].interrupt_started = 1;
81fd7b74
HG
610 /* We don't really want to drop interrupt packets ever, but
611 having some upper limit to how much we buffer is good. */
612 dev->endpoint[EP2I(ep)].bufpq_target_size = 1000;
613 dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0;
69354a83
HG
614 }
615
616 intp = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq);
617 if (intp == NULL) {
618 DPRINTF2("interrupt-token-in ep %02X, no intp\n", ep);
619 /* Check interrupt_error for stream errors */
620 status = dev->endpoint[EP2I(ep)].interrupt_error;
621 dev->endpoint[EP2I(ep)].interrupt_error = 0;
e6472210
HG
622 if (status) {
623 return usbredir_handle_status(dev, status, 0);
624 }
625 return USB_RET_NAK;
69354a83
HG
626 }
627 DPRINTF("interrupt-token-in ep %02X status %d len %d\n", ep,
628 intp->status, intp->len);
629
630 status = intp->status;
631 if (status != usb_redir_success) {
632 bufp_free(dev, intp, ep);
633 return usbredir_handle_status(dev, status, 0);
634 }
635
636 len = intp->len;
4f4321c1 637 if (len > p->iov.size) {
69354a83
HG
638 ERROR("received int data is larger then packet ep %02X\n", ep);
639 bufp_free(dev, intp, ep);
4d819a9b 640 return USB_RET_BABBLE;
69354a83 641 }
4f4321c1 642 usb_packet_copy(p, intp->data, len);
69354a83
HG
643 bufp_free(dev, intp, ep);
644 return len;
645 } else {
646 /* Output interrupt endpoint, normal async operation */
69354a83 647 struct usb_redir_interrupt_packet_header interrupt_packet;
4f4321c1 648 uint8_t buf[p->iov.size];
69354a83 649
de550a6a
HG
650 DPRINTF("interrupt-out ep %02X len %zd id %"PRIu64"\n", ep,
651 p->iov.size, p->id);
69354a83 652
9a8d4067
HG
653 if (usbredir_already_in_flight(dev, p->id)) {
654 return USB_RET_ASYNC;
655 }
656
69354a83 657 interrupt_packet.endpoint = ep;
4f4321c1 658 interrupt_packet.length = p->iov.size;
69354a83 659
4f4321c1
GH
660 usb_packet_copy(p, buf, p->iov.size);
661 usbredir_log_data(dev, "interrupt data out:", buf, p->iov.size);
de550a6a 662 usbredirparser_send_interrupt_packet(dev->parser, p->id,
4f4321c1 663 &interrupt_packet, buf, p->iov.size);
69354a83
HG
664 usbredirparser_do_write(dev->parser);
665 return USB_RET_ASYNC;
666 }
667}
668
669static void usbredir_stop_interrupt_receiving(USBRedirDevice *dev,
670 uint8_t ep)
671{
672 struct usb_redir_stop_interrupt_receiving_header stop_interrupt_recv = {
673 .endpoint = ep
674 };
675 if (dev->endpoint[EP2I(ep)].interrupt_started) {
676 usbredirparser_send_stop_interrupt_receiving(dev->parser, 0,
677 &stop_interrupt_recv);
678 DPRINTF("interrupt recv stopped ep %02X\n", ep);
679 dev->endpoint[EP2I(ep)].interrupt_started = 0;
680 }
2bd836e5 681 dev->endpoint[EP2I(ep)].interrupt_error = 0;
69354a83
HG
682 usbredir_free_bufpq(dev, ep);
683}
684
685static int usbredir_handle_data(USBDevice *udev, USBPacket *p)
686{
687 USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev);
688 uint8_t ep;
689
079d0b7f 690 ep = p->ep->nr;
69354a83
HG
691 if (p->pid == USB_TOKEN_IN) {
692 ep |= USB_DIR_IN;
693 }
694
695 switch (dev->endpoint[EP2I(ep)].type) {
696 case USB_ENDPOINT_XFER_CONTROL:
697 ERROR("handle_data called for control transfer on ep %02X\n", ep);
698 return USB_RET_NAK;
699 case USB_ENDPOINT_XFER_ISOC:
700 return usbredir_handle_iso_data(dev, p, ep);
701 case USB_ENDPOINT_XFER_BULK:
3a93113a 702 return usbredir_handle_bulk_data(dev, p, ep);
69354a83 703 case USB_ENDPOINT_XFER_INT:
3a93113a 704 return usbredir_handle_interrupt_data(dev, p, ep);
69354a83
HG
705 default:
706 ERROR("handle_data ep %02X has unknown type %d\n", ep,
707 dev->endpoint[EP2I(ep)].type);
708 return USB_RET_NAK;
709 }
710}
711
712static int usbredir_set_config(USBRedirDevice *dev, USBPacket *p,
713 int config)
714{
715 struct usb_redir_set_configuration_header set_config;
69354a83
HG
716 int i;
717
de550a6a 718 DPRINTF("set config %d id %"PRIu64"\n", config, p->id);
69354a83
HG
719
720 for (i = 0; i < MAX_ENDPOINTS; i++) {
721 switch (dev->endpoint[i].type) {
722 case USB_ENDPOINT_XFER_ISOC:
723 usbredir_stop_iso_stream(dev, I2EP(i));
724 break;
725 case USB_ENDPOINT_XFER_INT:
726 if (i & 0x10) {
727 usbredir_stop_interrupt_receiving(dev, I2EP(i));
728 }
729 break;
730 }
731 usbredir_free_bufpq(dev, I2EP(i));
732 }
733
734 set_config.configuration = config;
de550a6a 735 usbredirparser_send_set_configuration(dev->parser, p->id, &set_config);
69354a83
HG
736 usbredirparser_do_write(dev->parser);
737 return USB_RET_ASYNC;
738}
739
740static int usbredir_get_config(USBRedirDevice *dev, USBPacket *p)
741{
de550a6a 742 DPRINTF("get config id %"PRIu64"\n", p->id);
69354a83 743
de550a6a 744 usbredirparser_send_get_configuration(dev->parser, p->id);
69354a83
HG
745 usbredirparser_do_write(dev->parser);
746 return USB_RET_ASYNC;
747}
748
749static int usbredir_set_interface(USBRedirDevice *dev, USBPacket *p,
750 int interface, int alt)
751{
752 struct usb_redir_set_alt_setting_header set_alt;
69354a83
HG
753 int i;
754
de550a6a 755 DPRINTF("set interface %d alt %d id %"PRIu64"\n", interface, alt, p->id);
69354a83
HG
756
757 for (i = 0; i < MAX_ENDPOINTS; i++) {
758 if (dev->endpoint[i].interface == interface) {
759 switch (dev->endpoint[i].type) {
760 case USB_ENDPOINT_XFER_ISOC:
761 usbredir_stop_iso_stream(dev, I2EP(i));
762 break;
763 case USB_ENDPOINT_XFER_INT:
764 if (i & 0x10) {
765 usbredir_stop_interrupt_receiving(dev, I2EP(i));
766 }
767 break;
768 }
769 usbredir_free_bufpq(dev, I2EP(i));
770 }
771 }
772
773 set_alt.interface = interface;
774 set_alt.alt = alt;
de550a6a 775 usbredirparser_send_set_alt_setting(dev->parser, p->id, &set_alt);
69354a83
HG
776 usbredirparser_do_write(dev->parser);
777 return USB_RET_ASYNC;
778}
779
780static int usbredir_get_interface(USBRedirDevice *dev, USBPacket *p,
781 int interface)
782{
783 struct usb_redir_get_alt_setting_header get_alt;
69354a83 784
de550a6a 785 DPRINTF("get interface %d id %"PRIu64"\n", interface, p->id);
69354a83
HG
786
787 get_alt.interface = interface;
de550a6a 788 usbredirparser_send_get_alt_setting(dev->parser, p->id, &get_alt);
69354a83
HG
789 usbredirparser_do_write(dev->parser);
790 return USB_RET_ASYNC;
791}
792
793static int usbredir_handle_control(USBDevice *udev, USBPacket *p,
794 int request, int value, int index, int length, uint8_t *data)
795{
796 USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev);
797 struct usb_redir_control_packet_header control_packet;
69354a83 798
9a8d4067
HG
799 if (usbredir_already_in_flight(dev, p->id)) {
800 return USB_RET_ASYNC;
801 }
802
69354a83
HG
803 /* Special cases for certain standard device requests */
804 switch (request) {
805 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
806 DPRINTF("set address %d\n", value);
807 dev->dev.addr = value;
808 return 0;
809 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
810 return usbredir_set_config(dev, p, value & 0xff);
811 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
812 return usbredir_get_config(dev, p);
813 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
814 return usbredir_set_interface(dev, p, index, value);
815 case InterfaceRequest | USB_REQ_GET_INTERFACE:
816 return usbredir_get_interface(dev, p, index);
817 }
818
de550a6a
HG
819 /* Normal ctrl requests, note request is (bRequestType << 8) | bRequest */
820 DPRINTF(
821 "ctrl-out type 0x%x req 0x%x val 0x%x index %d len %d id %"PRIu64"\n",
822 request >> 8, request & 0xff, value, index, length, p->id);
69354a83
HG
823
824 control_packet.request = request & 0xFF;
825 control_packet.requesttype = request >> 8;
826 control_packet.endpoint = control_packet.requesttype & USB_DIR_IN;
827 control_packet.value = value;
828 control_packet.index = index;
829 control_packet.length = length;
69354a83
HG
830
831 if (control_packet.requesttype & USB_DIR_IN) {
de550a6a 832 usbredirparser_send_control_packet(dev->parser, p->id,
69354a83
HG
833 &control_packet, NULL, 0);
834 } else {
835 usbredir_log_data(dev, "ctrl data out:", data, length);
de550a6a 836 usbredirparser_send_control_packet(dev->parser, p->id,
69354a83
HG
837 &control_packet, data, length);
838 }
839 usbredirparser_do_write(dev->parser);
840 return USB_RET_ASYNC;
841}
842
843/*
844 * Close events can be triggered by usbredirparser_do_write which gets called
845 * from within the USBDevice data / control packet callbacks and doing a
846 * usb_detach from within these callbacks is not a good idea.
847 *
ed9873bf 848 * So we use a bh handler to take care of close events.
69354a83 849 */
ed9873bf 850static void usbredir_chardev_close_bh(void *opaque)
69354a83
HG
851{
852 USBRedirDevice *dev = opaque;
853
854 usbredir_device_disconnect(dev);
855
856 if (dev->parser) {
857 usbredirparser_destroy(dev->parser);
858 dev->parser = NULL;
859 }
ed9873bf 860}
69354a83 861
ed9873bf
HG
862static void usbredir_chardev_open(USBRedirDevice *dev)
863{
864 uint32_t caps[USB_REDIR_CAPS_SIZE] = { 0, };
865 char version[32];
fc3f6e1b 866 int flags = 0;
6af16589 867
ed9873bf
HG
868 /* Make sure any pending closes are handled (no-op if none pending) */
869 usbredir_chardev_close_bh(dev);
870 qemu_bh_cancel(dev->chardev_close_bh);
871
872 strcpy(version, "qemu usb-redir guest ");
873 pstrcat(version, sizeof(version), qemu_get_version());
874
875 dev->parser = qemu_oom_check(usbredirparser_create());
876 dev->parser->priv = dev;
877 dev->parser->log_func = usbredir_log;
878 dev->parser->read_func = usbredir_read;
879 dev->parser->write_func = usbredir_write;
880 dev->parser->hello_func = usbredir_hello;
881 dev->parser->device_connect_func = usbredir_device_connect;
882 dev->parser->device_disconnect_func = usbredir_device_disconnect;
883 dev->parser->interface_info_func = usbredir_interface_info;
884 dev->parser->ep_info_func = usbredir_ep_info;
885 dev->parser->configuration_status_func = usbredir_configuration_status;
886 dev->parser->alt_setting_status_func = usbredir_alt_setting_status;
887 dev->parser->iso_stream_status_func = usbredir_iso_stream_status;
888 dev->parser->interrupt_receiving_status_func =
889 usbredir_interrupt_receiving_status;
890 dev->parser->bulk_streams_status_func = usbredir_bulk_streams_status;
891 dev->parser->control_packet_func = usbredir_control_packet;
892 dev->parser->bulk_packet_func = usbredir_bulk_packet;
893 dev->parser->iso_packet_func = usbredir_iso_packet;
894 dev->parser->interrupt_packet_func = usbredir_interrupt_packet;
895 dev->read_buf = NULL;
896 dev->read_buf_size = 0;
897
898 usbredirparser_caps_set_cap(caps, usb_redir_cap_connect_device_version);
899 usbredirparser_caps_set_cap(caps, usb_redir_cap_filter);
0fde3b7a 900 usbredirparser_caps_set_cap(caps, usb_redir_cap_ep_info_max_packet_size);
be4a8928 901 usbredirparser_caps_set_cap(caps, usb_redir_cap_64bits_ids);
fc3f6e1b
HG
902
903 if (runstate_check(RUN_STATE_INMIGRATE)) {
904 flags |= usbredirparser_fl_no_hello;
905 }
906 usbredirparser_init(dev->parser, version, caps, USB_REDIR_CAPS_SIZE,
907 flags);
ed9873bf 908 usbredirparser_do_write(dev->parser);
69354a83
HG
909}
910
910c1e6b
HG
911static void usbredir_reject_device(USBRedirDevice *dev)
912{
913 usbredir_device_disconnect(dev);
914 if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter)) {
915 usbredirparser_send_filter_reject(dev->parser);
916 usbredirparser_do_write(dev->parser);
917 }
918}
919
69354a83
HG
920static void usbredir_do_attach(void *opaque)
921{
922 USBRedirDevice *dev = opaque;
923
a508cc42
HG
924 /* In order to work properly with XHCI controllers we need these caps */
925 if ((dev->dev.port->speedmask & USB_SPEED_MASK_SUPER) && !(
926 usbredirparser_peer_has_cap(dev->parser,
927 usb_redir_cap_ep_info_max_packet_size) &&
928 usbredirparser_peer_has_cap(dev->parser,
929 usb_redir_cap_64bits_ids))) {
930 ERROR("usb-redir-host lacks capabilities needed for use with XHCI\n");
931 usbredir_reject_device(dev);
932 return;
933 }
934
714f9db0 935 if (usb_device_attach(&dev->dev) != 0) {
910c1e6b 936 usbredir_reject_device(dev);
714f9db0 937 }
69354a83
HG
938}
939
940/*
941 * chardev callbacks
942 */
943
944static int usbredir_chardev_can_read(void *opaque)
945{
946 USBRedirDevice *dev = opaque;
947
ed9873bf
HG
948 if (!dev->parser) {
949 WARNING("chardev_can_read called on non open chardev!\n");
69354a83
HG
950 return 0;
951 }
ed9873bf 952
fc3f6e1b
HG
953 /* Don't read new data from the chardev until our state is fully synced */
954 if (!runstate_check(RUN_STATE_RUNNING)) {
955 return 0;
956 }
957
ed9873bf
HG
958 /* usbredir_parser_do_read will consume *all* data we give it */
959 return 1024 * 1024;
69354a83
HG
960}
961
962static void usbredir_chardev_read(void *opaque, const uint8_t *buf, int size)
963{
964 USBRedirDevice *dev = opaque;
965
966 /* No recursion allowed! */
967 assert(dev->read_buf == NULL);
968
969 dev->read_buf = buf;
970 dev->read_buf_size = size;
971
972 usbredirparser_do_read(dev->parser);
973 /* Send any acks, etc. which may be queued now */
974 usbredirparser_do_write(dev->parser);
975}
976
977static void usbredir_chardev_event(void *opaque, int event)
978{
979 USBRedirDevice *dev = opaque;
980
981 switch (event) {
982 case CHR_EVENT_OPENED:
ed9873bf
HG
983 usbredir_chardev_open(dev);
984 break;
69354a83 985 case CHR_EVENT_CLOSED:
ed9873bf 986 qemu_bh_schedule(dev->chardev_close_bh);
69354a83
HG
987 break;
988 }
989}
990
991/*
992 * init + destroy
993 */
994
fc3f6e1b
HG
995static void usbredir_vm_state_change(void *priv, int running, RunState state)
996{
997 USBRedirDevice *dev = priv;
998
999 if (state == RUN_STATE_RUNNING && dev->parser != NULL) {
1000 usbredirparser_do_write(dev->parser); /* Flush any pending writes */
1001 }
1002}
1003
69354a83
HG
1004static int usbredir_initfn(USBDevice *udev)
1005{
1006 USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev);
1007 int i;
1008
1009 if (dev->cs == NULL) {
1010 qerror_report(QERR_MISSING_PARAMETER, "chardev");
1011 return -1;
1012 }
1013
6af16589
HG
1014 if (dev->filter_str) {
1015 i = usbredirfilter_string_to_rules(dev->filter_str, ":", "|",
1016 &dev->filter_rules,
1017 &dev->filter_rules_count);
1018 if (i) {
1019 qerror_report(QERR_INVALID_PARAMETER_VALUE, "filter",
1020 "a usb device filter string");
1021 return -1;
1022 }
1023 }
1024
ed9873bf 1025 dev->chardev_close_bh = qemu_bh_new(usbredir_chardev_close_bh, dev);
69354a83
HG
1026 dev->attach_timer = qemu_new_timer_ms(vm_clock, usbredir_do_attach, dev);
1027
8e60452a 1028 packet_id_queue_init(&dev->cancelled, dev, "cancelled");
9a8d4067 1029 packet_id_queue_init(&dev->already_in_flight, dev, "already-in-flight");
69354a83
HG
1030 for (i = 0; i < MAX_ENDPOINTS; i++) {
1031 QTAILQ_INIT(&dev->endpoint[i].bufpq);
1032 }
1033
1034 /* We'll do the attach once we receive the speed from the usb-host */
1035 udev->auto_attach = 0;
1036
65f9d986
HG
1037 /* Let the backend know we are ready */
1038 qemu_chr_fe_open(dev->cs);
69354a83
HG
1039 qemu_chr_add_handlers(dev->cs, usbredir_chardev_can_read,
1040 usbredir_chardev_read, usbredir_chardev_event, dev);
1041
fc3f6e1b 1042 qemu_add_vm_change_state_handler(usbredir_vm_state_change, dev);
65bb3a5c 1043 add_boot_device_path(dev->bootindex, &udev->qdev, NULL);
69354a83
HG
1044 return 0;
1045}
1046
1047static void usbredir_cleanup_device_queues(USBRedirDevice *dev)
1048{
69354a83
HG
1049 int i;
1050
8e60452a 1051 packet_id_queue_empty(&dev->cancelled);
9a8d4067 1052 packet_id_queue_empty(&dev->already_in_flight);
69354a83
HG
1053 for (i = 0; i < MAX_ENDPOINTS; i++) {
1054 usbredir_free_bufpq(dev, I2EP(i));
1055 }
1056}
1057
1058static void usbredir_handle_destroy(USBDevice *udev)
1059{
1060 USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev);
1061
65f9d986 1062 qemu_chr_fe_close(dev->cs);
70f24fb6 1063 qemu_chr_delete(dev->cs);
69354a83 1064 /* Note must be done after qemu_chr_close, as that causes a close event */
ed9873bf 1065 qemu_bh_delete(dev->chardev_close_bh);
69354a83
HG
1066
1067 qemu_del_timer(dev->attach_timer);
1068 qemu_free_timer(dev->attach_timer);
1069
1070 usbredir_cleanup_device_queues(dev);
1071
1072 if (dev->parser) {
1073 usbredirparser_destroy(dev->parser);
1074 }
6af16589
HG
1075
1076 free(dev->filter_rules);
1077}
1078
1079static int usbredir_check_filter(USBRedirDevice *dev)
1080{
1510168e 1081 if (dev->interface_info.interface_count == NO_INTERFACE_INFO) {
6af16589 1082 ERROR("No interface info for device\n");
5b3bd682 1083 goto error;
6af16589
HG
1084 }
1085
1086 if (dev->filter_rules) {
1087 if (!usbredirparser_peer_has_cap(dev->parser,
1088 usb_redir_cap_connect_device_version)) {
1089 ERROR("Device filter specified and peer does not have the "
1090 "connect_device_version capability\n");
5b3bd682 1091 goto error;
6af16589
HG
1092 }
1093
1094 if (usbredirfilter_check(
1095 dev->filter_rules,
1096 dev->filter_rules_count,
1097 dev->device_info.device_class,
1098 dev->device_info.device_subclass,
1099 dev->device_info.device_protocol,
1100 dev->interface_info.interface_class,
1101 dev->interface_info.interface_subclass,
1102 dev->interface_info.interface_protocol,
1103 dev->interface_info.interface_count,
1104 dev->device_info.vendor_id,
1105 dev->device_info.product_id,
1106 dev->device_info.device_version_bcd,
1107 0) != 0) {
5b3bd682 1108 goto error;
6af16589
HG
1109 }
1110 }
1111
1112 return 0;
5b3bd682
HG
1113
1114error:
910c1e6b 1115 usbredir_reject_device(dev);
5b3bd682 1116 return -1;
69354a83
HG
1117}
1118
1119/*
1120 * usbredirparser packet complete callbacks
1121 */
1122
1123static int usbredir_handle_status(USBRedirDevice *dev,
1124 int status, int actual_len)
1125{
1126 switch (status) {
1127 case usb_redir_success:
1128 return actual_len;
1129 case usb_redir_stall:
1130 return USB_RET_STALL;
1131 case usb_redir_cancelled:
18113340
HG
1132 /*
1133 * When the usbredir-host unredirects a device, it will report a status
1134 * of cancelled for all pending packets, followed by a disconnect msg.
1135 */
1136 return USB_RET_IOERROR;
69354a83 1137 case usb_redir_inval:
d61000a8 1138 WARNING("got invalid param error from usb-host?\n");
18113340 1139 return USB_RET_IOERROR;
adae502c
HG
1140 case usb_redir_babble:
1141 return USB_RET_BABBLE;
69354a83
HG
1142 case usb_redir_ioerror:
1143 case usb_redir_timeout:
1144 default:
d61000a8 1145 return USB_RET_IOERROR;
69354a83
HG
1146 }
1147}
1148
097a66ef
HG
1149static void usbredir_hello(void *priv, struct usb_redir_hello_header *h)
1150{
1151 USBRedirDevice *dev = priv;
1152
1153 /* Try to send the filter info now that we've the usb-host's caps */
1154 if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter) &&
1155 dev->filter_rules) {
1156 usbredirparser_send_filter_filter(dev->parser, dev->filter_rules,
1157 dev->filter_rules_count);
1158 usbredirparser_do_write(dev->parser);
1159 }
1160}
1161
69354a83
HG
1162static void usbredir_device_connect(void *priv,
1163 struct usb_redir_device_connect_header *device_connect)
1164{
1165 USBRedirDevice *dev = priv;
6af16589 1166 const char *speed;
69354a83 1167
99f08100
HG
1168 if (qemu_timer_pending(dev->attach_timer) || dev->dev.attached) {
1169 ERROR("Received device connect while already connected\n");
1170 return;
1171 }
1172
69354a83
HG
1173 switch (device_connect->speed) {
1174 case usb_redir_speed_low:
6af16589 1175 speed = "low speed";
69354a83
HG
1176 dev->dev.speed = USB_SPEED_LOW;
1177 break;
1178 case usb_redir_speed_full:
6af16589 1179 speed = "full speed";
69354a83
HG
1180 dev->dev.speed = USB_SPEED_FULL;
1181 break;
1182 case usb_redir_speed_high:
6af16589 1183 speed = "high speed";
69354a83
HG
1184 dev->dev.speed = USB_SPEED_HIGH;
1185 break;
1186 case usb_redir_speed_super:
6af16589 1187 speed = "super speed";
69354a83
HG
1188 dev->dev.speed = USB_SPEED_SUPER;
1189 break;
1190 default:
6af16589 1191 speed = "unknown speed";
69354a83
HG
1192 dev->dev.speed = USB_SPEED_FULL;
1193 }
6af16589
HG
1194
1195 if (usbredirparser_peer_has_cap(dev->parser,
1196 usb_redir_cap_connect_device_version)) {
1197 INFO("attaching %s device %04x:%04x version %d.%d class %02x\n",
1198 speed, device_connect->vendor_id, device_connect->product_id,
52234bc0
HG
1199 ((device_connect->device_version_bcd & 0xf000) >> 12) * 10 +
1200 ((device_connect->device_version_bcd & 0x0f00) >> 8),
1201 ((device_connect->device_version_bcd & 0x00f0) >> 4) * 10 +
1202 ((device_connect->device_version_bcd & 0x000f) >> 0),
6af16589
HG
1203 device_connect->device_class);
1204 } else {
1205 INFO("attaching %s device %04x:%04x class %02x\n", speed,
1206 device_connect->vendor_id, device_connect->product_id,
1207 device_connect->device_class);
1208 }
1209
69354a83 1210 dev->dev.speedmask = (1 << dev->dev.speed);
6af16589
HG
1211 dev->device_info = *device_connect;
1212
1213 if (usbredir_check_filter(dev)) {
1214 WARNING("Device %04x:%04x rejected by device filter, not attaching\n",
1215 device_connect->vendor_id, device_connect->product_id);
1216 return;
1217 }
1218
69354a83
HG
1219 qemu_mod_timer(dev->attach_timer, dev->next_attach_time);
1220}
1221
1222static void usbredir_device_disconnect(void *priv)
1223{
1224 USBRedirDevice *dev = priv;
99f08100 1225 int i;
69354a83
HG
1226
1227 /* Stop any pending attaches */
1228 qemu_del_timer(dev->attach_timer);
1229
1230 if (dev->dev.attached) {
1231 usb_device_detach(&dev->dev);
69354a83
HG
1232 /*
1233 * Delay next usb device attach to give the guest a chance to see
1234 * see the detach / attach in case of quick close / open succession
1235 */
1236 dev->next_attach_time = qemu_get_clock_ms(vm_clock) + 200;
1237 }
99f08100
HG
1238
1239 /* Reset state so that the next dev connected starts with a clean slate */
1240 usbredir_cleanup_device_queues(dev);
1241 memset(dev->endpoint, 0, sizeof(dev->endpoint));
1242 for (i = 0; i < MAX_ENDPOINTS; i++) {
1243 QTAILQ_INIT(&dev->endpoint[i].bufpq);
1244 }
0454b611 1245 usb_ep_init(&dev->dev);
1510168e 1246 dev->interface_info.interface_count = NO_INTERFACE_INFO;
a0625c56
HG
1247 dev->dev.addr = 0;
1248 dev->dev.speed = 0;
69354a83
HG
1249}
1250
1251static void usbredir_interface_info(void *priv,
1252 struct usb_redir_interface_info_header *interface_info)
1253{
6af16589
HG
1254 USBRedirDevice *dev = priv;
1255
1256 dev->interface_info = *interface_info;
1257
1258 /*
1259 * If we receive interface info after the device has already been
1260 * connected (ie on a set_config), re-check the filter.
1261 */
1262 if (qemu_timer_pending(dev->attach_timer) || dev->dev.attached) {
1263 if (usbredir_check_filter(dev)) {
1264 ERROR("Device no longer matches filter after interface info "
1265 "change, disconnecting!\n");
6af16589
HG
1266 }
1267 }
69354a83
HG
1268}
1269
1270static void usbredir_ep_info(void *priv,
1271 struct usb_redir_ep_info_header *ep_info)
1272{
1273 USBRedirDevice *dev = priv;
0454b611 1274 struct USBEndpoint *usb_ep;
69354a83
HG
1275 int i;
1276
1277 for (i = 0; i < MAX_ENDPOINTS; i++) {
1278 dev->endpoint[i].type = ep_info->type[i];
1279 dev->endpoint[i].interval = ep_info->interval[i];
1280 dev->endpoint[i].interface = ep_info->interface[i];
e8a7dd29
HG
1281 switch (dev->endpoint[i].type) {
1282 case usb_redir_type_invalid:
1283 break;
1284 case usb_redir_type_iso:
1285 case usb_redir_type_interrupt:
1286 if (dev->endpoint[i].interval == 0) {
1287 ERROR("Received 0 interval for isoc or irq endpoint\n");
1288 usbredir_device_disconnect(dev);
1289 }
1290 /* Fall through */
1291 case usb_redir_type_control:
1292 case usb_redir_type_bulk:
69354a83
HG
1293 DPRINTF("ep: %02X type: %d interface: %d\n", I2EP(i),
1294 dev->endpoint[i].type, dev->endpoint[i].interface);
e8a7dd29
HG
1295 break;
1296 default:
1297 ERROR("Received invalid endpoint type\n");
1298 usbredir_device_disconnect(dev);
0454b611 1299 return;
69354a83 1300 }
0454b611
HG
1301 usb_ep = usb_ep_get(&dev->dev,
1302 (i & 0x10) ? USB_TOKEN_IN : USB_TOKEN_OUT,
1303 i & 0x0f);
1304 usb_ep->type = dev->endpoint[i].type;
1305 usb_ep->ifnum = dev->endpoint[i].interface;
0fde3b7a
HG
1306 if (usbredirparser_peer_has_cap(dev->parser,
1307 usb_redir_cap_ep_info_max_packet_size)) {
3f4be328
HG
1308 dev->endpoint[i].max_packet_size =
1309 usb_ep->max_packet_size = ep_info->max_packet_size[i];
0fde3b7a 1310 }
6c67446a
HG
1311 if (ep_info->type[i] == usb_redir_type_bulk) {
1312 usb_ep->pipeline = true;
1313 }
69354a83
HG
1314 }
1315}
1316
be4a8928 1317static void usbredir_configuration_status(void *priv, uint64_t id,
69354a83
HG
1318 struct usb_redir_configuration_status_header *config_status)
1319{
1320 USBRedirDevice *dev = priv;
de550a6a 1321 USBPacket *p;
69354a83
HG
1322 int len = 0;
1323
be4a8928
HG
1324 DPRINTF("set config status %d config %d id %"PRIu64"\n",
1325 config_status->status, config_status->configuration, id);
69354a83 1326
de550a6a
HG
1327 p = usbredir_find_packet_by_id(dev, 0, id);
1328 if (p) {
cb897117 1329 if (dev->dev.setup_buf[0] & USB_DIR_IN) {
69354a83
HG
1330 dev->dev.data_buf[0] = config_status->configuration;
1331 len = 1;
1332 }
de550a6a
HG
1333 p->result = usbredir_handle_status(dev, config_status->status, len);
1334 usb_generic_async_ctrl_complete(&dev->dev, p);
69354a83 1335 }
69354a83
HG
1336}
1337
be4a8928 1338static void usbredir_alt_setting_status(void *priv, uint64_t id,
69354a83
HG
1339 struct usb_redir_alt_setting_status_header *alt_setting_status)
1340{
1341 USBRedirDevice *dev = priv;
de550a6a 1342 USBPacket *p;
69354a83
HG
1343 int len = 0;
1344
be4a8928
HG
1345 DPRINTF("alt status %d intf %d alt %d id: %"PRIu64"\n",
1346 alt_setting_status->status, alt_setting_status->interface,
69354a83
HG
1347 alt_setting_status->alt, id);
1348
de550a6a
HG
1349 p = usbredir_find_packet_by_id(dev, 0, id);
1350 if (p) {
cb897117 1351 if (dev->dev.setup_buf[0] & USB_DIR_IN) {
69354a83
HG
1352 dev->dev.data_buf[0] = alt_setting_status->alt;
1353 len = 1;
1354 }
de550a6a 1355 p->result =
69354a83 1356 usbredir_handle_status(dev, alt_setting_status->status, len);
de550a6a 1357 usb_generic_async_ctrl_complete(&dev->dev, p);
69354a83 1358 }
69354a83
HG
1359}
1360
be4a8928 1361static void usbredir_iso_stream_status(void *priv, uint64_t id,
69354a83
HG
1362 struct usb_redir_iso_stream_status_header *iso_stream_status)
1363{
1364 USBRedirDevice *dev = priv;
1365 uint8_t ep = iso_stream_status->endpoint;
1366
be4a8928 1367 DPRINTF("iso status %d ep %02X id %"PRIu64"\n", iso_stream_status->status,
69354a83
HG
1368 ep, id);
1369
2bd836e5 1370 if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].iso_started) {
99f08100
HG
1371 return;
1372 }
1373
69354a83
HG
1374 dev->endpoint[EP2I(ep)].iso_error = iso_stream_status->status;
1375 if (iso_stream_status->status == usb_redir_stall) {
1376 DPRINTF("iso stream stopped by peer ep %02X\n", ep);
1377 dev->endpoint[EP2I(ep)].iso_started = 0;
1378 }
1379}
1380
be4a8928 1381static void usbredir_interrupt_receiving_status(void *priv, uint64_t id,
69354a83
HG
1382 struct usb_redir_interrupt_receiving_status_header
1383 *interrupt_receiving_status)
1384{
1385 USBRedirDevice *dev = priv;
1386 uint8_t ep = interrupt_receiving_status->endpoint;
1387
be4a8928 1388 DPRINTF("interrupt recv status %d ep %02X id %"PRIu64"\n",
69354a83
HG
1389 interrupt_receiving_status->status, ep, id);
1390
2bd836e5 1391 if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].interrupt_started) {
99f08100
HG
1392 return;
1393 }
1394
69354a83
HG
1395 dev->endpoint[EP2I(ep)].interrupt_error =
1396 interrupt_receiving_status->status;
1397 if (interrupt_receiving_status->status == usb_redir_stall) {
1398 DPRINTF("interrupt receiving stopped by peer ep %02X\n", ep);
1399 dev->endpoint[EP2I(ep)].interrupt_started = 0;
1400 }
1401}
1402
be4a8928 1403static void usbredir_bulk_streams_status(void *priv, uint64_t id,
69354a83
HG
1404 struct usb_redir_bulk_streams_status_header *bulk_streams_status)
1405{
1406}
1407
be4a8928 1408static void usbredir_control_packet(void *priv, uint64_t id,
69354a83
HG
1409 struct usb_redir_control_packet_header *control_packet,
1410 uint8_t *data, int data_len)
1411{
1412 USBRedirDevice *dev = priv;
de550a6a 1413 USBPacket *p;
69354a83 1414 int len = control_packet->length;
69354a83 1415
be4a8928 1416 DPRINTF("ctrl-in status %d len %d id %"PRIu64"\n", control_packet->status,
69354a83
HG
1417 len, id);
1418
de550a6a
HG
1419 p = usbredir_find_packet_by_id(dev, 0, id);
1420 if (p) {
69354a83
HG
1421 len = usbredir_handle_status(dev, control_packet->status, len);
1422 if (len > 0) {
1423 usbredir_log_data(dev, "ctrl data in:", data, data_len);
1424 if (data_len <= sizeof(dev->dev.data_buf)) {
1425 memcpy(dev->dev.data_buf, data, data_len);
1426 } else {
1427 ERROR("ctrl buffer too small (%d > %zu)\n",
1428 data_len, sizeof(dev->dev.data_buf));
1429 len = USB_RET_STALL;
1430 }
1431 }
de550a6a
HG
1432 p->result = len;
1433 usb_generic_async_ctrl_complete(&dev->dev, p);
69354a83 1434 }
69354a83
HG
1435 free(data);
1436}
1437
be4a8928 1438static void usbredir_bulk_packet(void *priv, uint64_t id,
69354a83
HG
1439 struct usb_redir_bulk_packet_header *bulk_packet,
1440 uint8_t *data, int data_len)
1441{
1442 USBRedirDevice *dev = priv;
1443 uint8_t ep = bulk_packet->endpoint;
1444 int len = bulk_packet->length;
de550a6a 1445 USBPacket *p;
69354a83 1446
be4a8928
HG
1447 DPRINTF("bulk-in status %d ep %02X len %d id %"PRIu64"\n",
1448 bulk_packet->status, ep, len, id);
69354a83 1449
de550a6a
HG
1450 p = usbredir_find_packet_by_id(dev, ep, id);
1451 if (p) {
69354a83
HG
1452 len = usbredir_handle_status(dev, bulk_packet->status, len);
1453 if (len > 0) {
1454 usbredir_log_data(dev, "bulk data in:", data, data_len);
de550a6a
HG
1455 if (data_len <= p->iov.size) {
1456 usb_packet_copy(p, data, data_len);
69354a83 1457 } else {
2979a361
HG
1458 ERROR("bulk got more data then requested (%d > %zd)\n",
1459 data_len, p->iov.size);
1460 len = USB_RET_BABBLE;
69354a83
HG
1461 }
1462 }
de550a6a
HG
1463 p->result = len;
1464 usb_packet_complete(&dev->dev, p);
69354a83 1465 }
69354a83
HG
1466 free(data);
1467}
1468
be4a8928 1469static void usbredir_iso_packet(void *priv, uint64_t id,
69354a83
HG
1470 struct usb_redir_iso_packet_header *iso_packet,
1471 uint8_t *data, int data_len)
1472{
1473 USBRedirDevice *dev = priv;
1474 uint8_t ep = iso_packet->endpoint;
1475
be4a8928
HG
1476 DPRINTF2("iso-in status %d ep %02X len %d id %"PRIu64"\n",
1477 iso_packet->status, ep, data_len, id);
69354a83
HG
1478
1479 if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_ISOC) {
1480 ERROR("received iso packet for non iso endpoint %02X\n", ep);
1481 free(data);
1482 return;
1483 }
1484
1485 if (dev->endpoint[EP2I(ep)].iso_started == 0) {
1486 DPRINTF("received iso packet for non started stream ep %02X\n", ep);
1487 free(data);
1488 return;
1489 }
1490
1491 /* bufp_alloc also adds the packet to the ep queue */
1492 bufp_alloc(dev, data, data_len, iso_packet->status, ep);
1493}
1494
be4a8928 1495static void usbredir_interrupt_packet(void *priv, uint64_t id,
69354a83
HG
1496 struct usb_redir_interrupt_packet_header *interrupt_packet,
1497 uint8_t *data, int data_len)
1498{
1499 USBRedirDevice *dev = priv;
1500 uint8_t ep = interrupt_packet->endpoint;
1501
be4a8928 1502 DPRINTF("interrupt-in status %d ep %02X len %d id %"PRIu64"\n",
69354a83
HG
1503 interrupt_packet->status, ep, data_len, id);
1504
1505 if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_INT) {
1506 ERROR("received int packet for non interrupt endpoint %02X\n", ep);
1507 free(data);
1508 return;
1509 }
1510
1511 if (ep & USB_DIR_IN) {
1512 if (dev->endpoint[EP2I(ep)].interrupt_started == 0) {
1513 DPRINTF("received int packet while not started ep %02X\n", ep);
1514 free(data);
1515 return;
1516 }
1517
1518 /* bufp_alloc also adds the packet to the ep queue */
1519 bufp_alloc(dev, data, data_len, interrupt_packet->status, ep);
1520 } else {
1521 int len = interrupt_packet->length;
1522
de550a6a
HG
1523 USBPacket *p = usbredir_find_packet_by_id(dev, ep, id);
1524 if (p) {
1525 p->result = usbredir_handle_status(dev,
69354a83 1526 interrupt_packet->status, len);
de550a6a 1527 usb_packet_complete(&dev->dev, p);
69354a83 1528 }
69354a83
HG
1529 }
1530}
1531
fc3f6e1b
HG
1532/*
1533 * Migration code
1534 */
1535
1536static void usbredir_pre_save(void *priv)
1537{
1538 USBRedirDevice *dev = priv;
1539
1540 usbredir_fill_already_in_flight(dev);
1541}
1542
1543static int usbredir_post_load(void *priv, int version_id)
1544{
1545 USBRedirDevice *dev = priv;
1546 struct USBEndpoint *usb_ep;
1547 int i;
1548
1549 switch (dev->device_info.speed) {
1550 case usb_redir_speed_low:
1551 dev->dev.speed = USB_SPEED_LOW;
1552 break;
1553 case usb_redir_speed_full:
1554 dev->dev.speed = USB_SPEED_FULL;
1555 break;
1556 case usb_redir_speed_high:
1557 dev->dev.speed = USB_SPEED_HIGH;
1558 break;
1559 case usb_redir_speed_super:
1560 dev->dev.speed = USB_SPEED_SUPER;
1561 break;
1562 default:
1563 dev->dev.speed = USB_SPEED_FULL;
1564 }
1565 dev->dev.speedmask = (1 << dev->dev.speed);
1566
1567 for (i = 0; i < MAX_ENDPOINTS; i++) {
1568 usb_ep = usb_ep_get(&dev->dev,
1569 (i & 0x10) ? USB_TOKEN_IN : USB_TOKEN_OUT,
1570 i & 0x0f);
1571 usb_ep->type = dev->endpoint[i].type;
1572 usb_ep->ifnum = dev->endpoint[i].interface;
1573 usb_ep->max_packet_size = dev->endpoint[i].max_packet_size;
1574 if (dev->endpoint[i].type == usb_redir_type_bulk) {
1575 usb_ep->pipeline = true;
1576 }
1577 }
1578 return 0;
1579}
1580
1581/* For usbredirparser migration */
1582static void usbredir_put_parser(QEMUFile *f, void *priv, size_t unused)
1583{
1584 USBRedirDevice *dev = priv;
1585 uint8_t *data;
1586 int len;
1587
1588 if (dev->parser == NULL) {
1589 qemu_put_be32(f, 0);
1590 return;
1591 }
1592
1593 usbredirparser_serialize(dev->parser, &data, &len);
1594 qemu_oom_check(data);
1595
1596 qemu_put_be32(f, len);
1597 qemu_put_buffer(f, data, len);
1598
1599 free(data);
1600}
1601
1602static int usbredir_get_parser(QEMUFile *f, void *priv, size_t unused)
1603{
1604 USBRedirDevice *dev = priv;
1605 uint8_t *data;
1606 int len, ret;
1607
1608 len = qemu_get_be32(f);
1609 if (len == 0) {
1610 return 0;
1611 }
1612
1613 /*
1614 * Our chardev should be open already at this point, otherwise
1615 * the usbredir channel will be broken (ie spice without seamless)
1616 */
1617 if (dev->parser == NULL) {
1618 ERROR("get_parser called with closed chardev, failing migration\n");
1619 return -1;
1620 }
1621
1622 data = g_malloc(len);
1623 qemu_get_buffer(f, data, len);
1624
1625 ret = usbredirparser_unserialize(dev->parser, data, len);
1626
1627 g_free(data);
1628
1629 return ret;
1630}
1631
1632static const VMStateInfo usbredir_parser_vmstate_info = {
1633 .name = "usb-redir-parser",
1634 .put = usbredir_put_parser,
1635 .get = usbredir_get_parser,
1636};
1637
1638
1639/* For buffered packets (iso/irq) queue migration */
1640static void usbredir_put_bufpq(QEMUFile *f, void *priv, size_t unused)
1641{
1642 struct endp_data *endp = priv;
1643 struct buf_packet *bufp;
1644 int remain = endp->bufpq_size;
1645
1646 qemu_put_be32(f, endp->bufpq_size);
1647 QTAILQ_FOREACH(bufp, &endp->bufpq, next) {
1648 qemu_put_be32(f, bufp->len);
1649 qemu_put_be32(f, bufp->status);
1650 qemu_put_buffer(f, bufp->data, bufp->len);
1651 remain--;
1652 }
1653 assert(remain == 0);
1654}
1655
1656static int usbredir_get_bufpq(QEMUFile *f, void *priv, size_t unused)
1657{
1658 struct endp_data *endp = priv;
1659 struct buf_packet *bufp;
1660 int i;
1661
1662 endp->bufpq_size = qemu_get_be32(f);
1663 for (i = 0; i < endp->bufpq_size; i++) {
1664 bufp = g_malloc(sizeof(struct buf_packet));
1665 bufp->len = qemu_get_be32(f);
1666 bufp->status = qemu_get_be32(f);
1667 bufp->data = qemu_oom_check(malloc(bufp->len)); /* regular malloc! */
1668 qemu_get_buffer(f, bufp->data, bufp->len);
1669 QTAILQ_INSERT_TAIL(&endp->bufpq, bufp, next);
1670 }
1671 return 0;
1672}
1673
1674static const VMStateInfo usbredir_ep_bufpq_vmstate_info = {
1675 .name = "usb-redir-bufpq",
1676 .put = usbredir_put_bufpq,
1677 .get = usbredir_get_bufpq,
1678};
1679
1680
1681/* For endp_data migration */
1682static const VMStateDescription usbredir_ep_vmstate = {
1683 .name = "usb-redir-ep",
1684 .version_id = 1,
1685 .minimum_version_id = 1,
1686 .fields = (VMStateField[]) {
1687 VMSTATE_UINT8(type, struct endp_data),
1688 VMSTATE_UINT8(interval, struct endp_data),
1689 VMSTATE_UINT8(interface, struct endp_data),
1690 VMSTATE_UINT16(max_packet_size, struct endp_data),
1691 VMSTATE_UINT8(iso_started, struct endp_data),
1692 VMSTATE_UINT8(iso_error, struct endp_data),
1693 VMSTATE_UINT8(interrupt_started, struct endp_data),
1694 VMSTATE_UINT8(interrupt_error, struct endp_data),
1695 VMSTATE_UINT8(bufpq_prefilled, struct endp_data),
1696 VMSTATE_UINT8(bufpq_dropping_packets, struct endp_data),
1697 {
1698 .name = "bufpq",
1699 .version_id = 0,
1700 .field_exists = NULL,
1701 .size = 0,
1702 .info = &usbredir_ep_bufpq_vmstate_info,
1703 .flags = VMS_SINGLE,
1704 .offset = 0,
1705 },
1706 VMSTATE_INT32(bufpq_target_size, struct endp_data),
1707 VMSTATE_END_OF_LIST()
1708 }
1709};
1710
1711
1712/* For PacketIdQueue migration */
1713static void usbredir_put_packet_id_q(QEMUFile *f, void *priv, size_t unused)
1714{
1715 struct PacketIdQueue *q = priv;
1716 USBRedirDevice *dev = q->dev;
1717 struct PacketIdQueueEntry *e;
1718 int remain = q->size;
1719
1720 DPRINTF("put_packet_id_q %s size %d\n", q->name, q->size);
1721 qemu_put_be32(f, q->size);
1722 QTAILQ_FOREACH(e, &q->head, next) {
1723 qemu_put_be64(f, e->id);
1724 remain--;
1725 }
1726 assert(remain == 0);
1727}
1728
1729static int usbredir_get_packet_id_q(QEMUFile *f, void *priv, size_t unused)
1730{
1731 struct PacketIdQueue *q = priv;
1732 USBRedirDevice *dev = q->dev;
1733 int i, size;
1734 uint64_t id;
1735
1736 size = qemu_get_be32(f);
1737 DPRINTF("get_packet_id_q %s size %d\n", q->name, size);
1738 for (i = 0; i < size; i++) {
1739 id = qemu_get_be64(f);
1740 packet_id_queue_add(q, id);
1741 }
1742 assert(q->size == size);
1743 return 0;
1744}
1745
1746static const VMStateInfo usbredir_ep_packet_id_q_vmstate_info = {
1747 .name = "usb-redir-packet-id-q",
1748 .put = usbredir_put_packet_id_q,
1749 .get = usbredir_get_packet_id_q,
1750};
1751
1752static const VMStateDescription usbredir_ep_packet_id_queue_vmstate = {
1753 .name = "usb-redir-packet-id-queue",
1754 .version_id = 1,
1755 .minimum_version_id = 1,
1756 .fields = (VMStateField[]) {
1757 {
1758 .name = "queue",
1759 .version_id = 0,
1760 .field_exists = NULL,
1761 .size = 0,
1762 .info = &usbredir_ep_packet_id_q_vmstate_info,
1763 .flags = VMS_SINGLE,
1764 .offset = 0,
1765 },
1766 VMSTATE_END_OF_LIST()
1767 }
1768};
1769
1770
1771/* For usb_redir_device_connect_header migration */
1772static const VMStateDescription usbredir_device_info_vmstate = {
1773 .name = "usb-redir-device-info",
1774 .version_id = 1,
1775 .minimum_version_id = 1,
1776 .fields = (VMStateField[]) {
1777 VMSTATE_UINT8(speed, struct usb_redir_device_connect_header),
1778 VMSTATE_UINT8(device_class, struct usb_redir_device_connect_header),
1779 VMSTATE_UINT8(device_subclass, struct usb_redir_device_connect_header),
1780 VMSTATE_UINT8(device_protocol, struct usb_redir_device_connect_header),
1781 VMSTATE_UINT16(vendor_id, struct usb_redir_device_connect_header),
1782 VMSTATE_UINT16(product_id, struct usb_redir_device_connect_header),
1783 VMSTATE_UINT16(device_version_bcd,
1784 struct usb_redir_device_connect_header),
1785 VMSTATE_END_OF_LIST()
1786 }
1787};
1788
1789
1790/* For usb_redir_interface_info_header migration */
1791static const VMStateDescription usbredir_interface_info_vmstate = {
1792 .name = "usb-redir-interface-info",
1793 .version_id = 1,
1794 .minimum_version_id = 1,
1795 .fields = (VMStateField[]) {
1796 VMSTATE_UINT32(interface_count,
1797 struct usb_redir_interface_info_header),
1798 VMSTATE_UINT8_ARRAY(interface,
1799 struct usb_redir_interface_info_header, 32),
1800 VMSTATE_UINT8_ARRAY(interface_class,
1801 struct usb_redir_interface_info_header, 32),
1802 VMSTATE_UINT8_ARRAY(interface_subclass,
1803 struct usb_redir_interface_info_header, 32),
1804 VMSTATE_UINT8_ARRAY(interface_protocol,
1805 struct usb_redir_interface_info_header, 32),
1806 VMSTATE_END_OF_LIST()
1807 }
1808};
1809
1810
1811/* And finally the USBRedirDevice vmstate itself */
1812static const VMStateDescription usbredir_vmstate = {
1813 .name = "usb-redir",
1814 .version_id = 1,
1815 .minimum_version_id = 1,
1816 .pre_save = usbredir_pre_save,
1817 .post_load = usbredir_post_load,
1818 .fields = (VMStateField[]) {
1819 VMSTATE_USB_DEVICE(dev, USBRedirDevice),
1820 VMSTATE_TIMER(attach_timer, USBRedirDevice),
1821 {
1822 .name = "parser",
1823 .version_id = 0,
1824 .field_exists = NULL,
1825 .size = 0,
1826 .info = &usbredir_parser_vmstate_info,
1827 .flags = VMS_SINGLE,
1828 .offset = 0,
1829 },
1830 VMSTATE_STRUCT_ARRAY(endpoint, USBRedirDevice, MAX_ENDPOINTS, 1,
1831 usbredir_ep_vmstate, struct endp_data),
1832 VMSTATE_STRUCT(cancelled, USBRedirDevice, 1,
1833 usbredir_ep_packet_id_queue_vmstate,
1834 struct PacketIdQueue),
1835 VMSTATE_STRUCT(already_in_flight, USBRedirDevice, 1,
1836 usbredir_ep_packet_id_queue_vmstate,
1837 struct PacketIdQueue),
1838 VMSTATE_STRUCT(device_info, USBRedirDevice, 1,
1839 usbredir_device_info_vmstate,
1840 struct usb_redir_device_connect_header),
1841 VMSTATE_STRUCT(interface_info, USBRedirDevice, 1,
1842 usbredir_interface_info_vmstate,
1843 struct usb_redir_interface_info_header),
1844 VMSTATE_END_OF_LIST()
1845 }
1846};
1847
3bc36349
AL
1848static Property usbredir_properties[] = {
1849 DEFINE_PROP_CHR("chardev", USBRedirDevice, cs),
1850 DEFINE_PROP_UINT8("debug", USBRedirDevice, debug, 0),
6af16589 1851 DEFINE_PROP_STRING("filter", USBRedirDevice, filter_str),
65bb3a5c 1852 DEFINE_PROP_INT32("bootindex", USBRedirDevice, bootindex, -1),
3bc36349
AL
1853 DEFINE_PROP_END_OF_LIST(),
1854};
1855
62aed765
AL
1856static void usbredir_class_initfn(ObjectClass *klass, void *data)
1857{
1858 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
3bc36349 1859 DeviceClass *dc = DEVICE_CLASS(klass);
62aed765
AL
1860
1861 uc->init = usbredir_initfn;
1862 uc->product_desc = "USB Redirection Device";
1863 uc->handle_destroy = usbredir_handle_destroy;
62aed765
AL
1864 uc->cancel_packet = usbredir_cancel_packet;
1865 uc->handle_reset = usbredir_handle_reset;
1866 uc->handle_data = usbredir_handle_data;
1867 uc->handle_control = usbredir_handle_control;
fc3f6e1b 1868 dc->vmsd = &usbredir_vmstate;
3bc36349 1869 dc->props = usbredir_properties;
62aed765
AL
1870}
1871
3bc36349
AL
1872static TypeInfo usbredir_dev_info = {
1873 .name = "usb-redir",
1874 .parent = TYPE_USB_DEVICE,
1875 .instance_size = sizeof(USBRedirDevice),
1876 .class_init = usbredir_class_initfn,
69354a83
HG
1877};
1878
83f7d43a 1879static void usbredir_register_types(void)
69354a83 1880{
3bc36349 1881 type_register_static(&usbredir_dev_info);
69354a83 1882}
83f7d43a
AF
1883
1884type_init(usbredir_register_types)