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