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