]> git.proxmox.com Git - qemu.git/blob - hw/usb/redirect.c
566737d8f508214d2c9aa50e5ef72456b87d767b
[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 int usbredir_initfn(USBDevice *udev)
1067 {
1068 USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev);
1069 int i;
1070
1071 if (dev->cs == NULL) {
1072 qerror_report(QERR_MISSING_PARAMETER, "chardev");
1073 return -1;
1074 }
1075
1076 if (dev->filter_str) {
1077 i = usbredirfilter_string_to_rules(dev->filter_str, ":", "|",
1078 &dev->filter_rules,
1079 &dev->filter_rules_count);
1080 if (i) {
1081 qerror_report(QERR_INVALID_PARAMETER_VALUE, "filter",
1082 "a usb device filter string");
1083 return -1;
1084 }
1085 }
1086
1087 dev->chardev_close_bh = qemu_bh_new(usbredir_chardev_close_bh, dev);
1088 dev->attach_timer = qemu_new_timer_ms(vm_clock, usbredir_do_attach, dev);
1089
1090 packet_id_queue_init(&dev->cancelled, dev, "cancelled");
1091 packet_id_queue_init(&dev->already_in_flight, dev, "already-in-flight");
1092 for (i = 0; i < MAX_ENDPOINTS; i++) {
1093 QTAILQ_INIT(&dev->endpoint[i].bufpq);
1094 }
1095
1096 /* We'll do the attach once we receive the speed from the usb-host */
1097 udev->auto_attach = 0;
1098
1099 /* Will be cleared during setup when we find conflicts */
1100 dev->compatible_speedmask = USB_SPEED_MASK_FULL | USB_SPEED_MASK_HIGH;
1101
1102 /* Let the backend know we are ready */
1103 qemu_chr_fe_open(dev->cs);
1104 qemu_chr_add_handlers(dev->cs, usbredir_chardev_can_read,
1105 usbredir_chardev_read, usbredir_chardev_event, dev);
1106
1107 qemu_add_vm_change_state_handler(usbredir_vm_state_change, dev);
1108 add_boot_device_path(dev->bootindex, &udev->qdev, NULL);
1109 return 0;
1110 }
1111
1112 static void usbredir_cleanup_device_queues(USBRedirDevice *dev)
1113 {
1114 int i;
1115
1116 packet_id_queue_empty(&dev->cancelled);
1117 packet_id_queue_empty(&dev->already_in_flight);
1118 for (i = 0; i < MAX_ENDPOINTS; i++) {
1119 usbredir_free_bufpq(dev, I2EP(i));
1120 }
1121 }
1122
1123 static void usbredir_handle_destroy(USBDevice *udev)
1124 {
1125 USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev);
1126
1127 qemu_chr_fe_close(dev->cs);
1128 qemu_chr_delete(dev->cs);
1129 /* Note must be done after qemu_chr_close, as that causes a close event */
1130 qemu_bh_delete(dev->chardev_close_bh);
1131
1132 qemu_del_timer(dev->attach_timer);
1133 qemu_free_timer(dev->attach_timer);
1134
1135 usbredir_cleanup_device_queues(dev);
1136
1137 if (dev->parser) {
1138 usbredirparser_destroy(dev->parser);
1139 }
1140
1141 free(dev->filter_rules);
1142 }
1143
1144 static int usbredir_check_filter(USBRedirDevice *dev)
1145 {
1146 if (dev->interface_info.interface_count == NO_INTERFACE_INFO) {
1147 ERROR("No interface info for device\n");
1148 goto error;
1149 }
1150
1151 if (dev->filter_rules) {
1152 if (!usbredirparser_peer_has_cap(dev->parser,
1153 usb_redir_cap_connect_device_version)) {
1154 ERROR("Device filter specified and peer does not have the "
1155 "connect_device_version capability\n");
1156 goto error;
1157 }
1158
1159 if (usbredirfilter_check(
1160 dev->filter_rules,
1161 dev->filter_rules_count,
1162 dev->device_info.device_class,
1163 dev->device_info.device_subclass,
1164 dev->device_info.device_protocol,
1165 dev->interface_info.interface_class,
1166 dev->interface_info.interface_subclass,
1167 dev->interface_info.interface_protocol,
1168 dev->interface_info.interface_count,
1169 dev->device_info.vendor_id,
1170 dev->device_info.product_id,
1171 dev->device_info.device_version_bcd,
1172 0) != 0) {
1173 goto error;
1174 }
1175 }
1176
1177 return 0;
1178
1179 error:
1180 usbredir_reject_device(dev);
1181 return -1;
1182 }
1183
1184 /*
1185 * usbredirparser packet complete callbacks
1186 */
1187
1188 static void usbredir_handle_status(USBRedirDevice *dev, USBPacket *p,
1189 int status)
1190 {
1191 switch (status) {
1192 case usb_redir_success:
1193 p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */
1194 break;
1195 case usb_redir_stall:
1196 p->status = USB_RET_STALL;
1197 break;
1198 case usb_redir_cancelled:
1199 /*
1200 * When the usbredir-host unredirects a device, it will report a status
1201 * of cancelled for all pending packets, followed by a disconnect msg.
1202 */
1203 p->status = USB_RET_IOERROR;
1204 break;
1205 case usb_redir_inval:
1206 WARNING("got invalid param error from usb-host?\n");
1207 p->status = USB_RET_IOERROR;
1208 break;
1209 case usb_redir_babble:
1210 p->status = USB_RET_BABBLE;
1211 break;
1212 case usb_redir_ioerror:
1213 case usb_redir_timeout:
1214 default:
1215 p->status = USB_RET_IOERROR;
1216 }
1217 }
1218
1219 static void usbredir_hello(void *priv, struct usb_redir_hello_header *h)
1220 {
1221 USBRedirDevice *dev = priv;
1222
1223 /* Try to send the filter info now that we've the usb-host's caps */
1224 if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter) &&
1225 dev->filter_rules) {
1226 usbredirparser_send_filter_filter(dev->parser, dev->filter_rules,
1227 dev->filter_rules_count);
1228 usbredirparser_do_write(dev->parser);
1229 }
1230 }
1231
1232 static void usbredir_device_connect(void *priv,
1233 struct usb_redir_device_connect_header *device_connect)
1234 {
1235 USBRedirDevice *dev = priv;
1236 const char *speed;
1237
1238 if (qemu_timer_pending(dev->attach_timer) || dev->dev.attached) {
1239 ERROR("Received device connect while already connected\n");
1240 return;
1241 }
1242
1243 switch (device_connect->speed) {
1244 case usb_redir_speed_low:
1245 speed = "low speed";
1246 dev->dev.speed = USB_SPEED_LOW;
1247 dev->compatible_speedmask &= ~USB_SPEED_MASK_FULL;
1248 dev->compatible_speedmask &= ~USB_SPEED_MASK_HIGH;
1249 break;
1250 case usb_redir_speed_full:
1251 speed = "full speed";
1252 dev->dev.speed = USB_SPEED_FULL;
1253 dev->compatible_speedmask &= ~USB_SPEED_MASK_HIGH;
1254 break;
1255 case usb_redir_speed_high:
1256 speed = "high speed";
1257 dev->dev.speed = USB_SPEED_HIGH;
1258 break;
1259 case usb_redir_speed_super:
1260 speed = "super speed";
1261 dev->dev.speed = USB_SPEED_SUPER;
1262 break;
1263 default:
1264 speed = "unknown speed";
1265 dev->dev.speed = USB_SPEED_FULL;
1266 }
1267
1268 if (usbredirparser_peer_has_cap(dev->parser,
1269 usb_redir_cap_connect_device_version)) {
1270 INFO("attaching %s device %04x:%04x version %d.%d class %02x\n",
1271 speed, device_connect->vendor_id, device_connect->product_id,
1272 ((device_connect->device_version_bcd & 0xf000) >> 12) * 10 +
1273 ((device_connect->device_version_bcd & 0x0f00) >> 8),
1274 ((device_connect->device_version_bcd & 0x00f0) >> 4) * 10 +
1275 ((device_connect->device_version_bcd & 0x000f) >> 0),
1276 device_connect->device_class);
1277 } else {
1278 INFO("attaching %s device %04x:%04x class %02x\n", speed,
1279 device_connect->vendor_id, device_connect->product_id,
1280 device_connect->device_class);
1281 }
1282
1283 dev->dev.speedmask = (1 << dev->dev.speed) | dev->compatible_speedmask;
1284 dev->device_info = *device_connect;
1285
1286 if (usbredir_check_filter(dev)) {
1287 WARNING("Device %04x:%04x rejected by device filter, not attaching\n",
1288 device_connect->vendor_id, device_connect->product_id);
1289 return;
1290 }
1291
1292 qemu_mod_timer(dev->attach_timer, dev->next_attach_time);
1293 }
1294
1295 static void usbredir_device_disconnect(void *priv)
1296 {
1297 USBRedirDevice *dev = priv;
1298 int i;
1299
1300 /* Stop any pending attaches */
1301 qemu_del_timer(dev->attach_timer);
1302
1303 if (dev->dev.attached) {
1304 DPRINTF("detaching device\n");
1305 usb_device_detach(&dev->dev);
1306 /*
1307 * Delay next usb device attach to give the guest a chance to see
1308 * see the detach / attach in case of quick close / open succession
1309 */
1310 dev->next_attach_time = qemu_get_clock_ms(vm_clock) + 200;
1311 }
1312
1313 /* Reset state so that the next dev connected starts with a clean slate */
1314 usbredir_cleanup_device_queues(dev);
1315 memset(dev->endpoint, 0, sizeof(dev->endpoint));
1316 for (i = 0; i < MAX_ENDPOINTS; i++) {
1317 QTAILQ_INIT(&dev->endpoint[i].bufpq);
1318 }
1319 usb_ep_init(&dev->dev);
1320 dev->interface_info.interface_count = NO_INTERFACE_INFO;
1321 dev->dev.addr = 0;
1322 dev->dev.speed = 0;
1323 dev->compatible_speedmask = USB_SPEED_MASK_FULL | USB_SPEED_MASK_HIGH;
1324 }
1325
1326 static void usbredir_interface_info(void *priv,
1327 struct usb_redir_interface_info_header *interface_info)
1328 {
1329 USBRedirDevice *dev = priv;
1330
1331 dev->interface_info = *interface_info;
1332
1333 /*
1334 * If we receive interface info after the device has already been
1335 * connected (ie on a set_config), re-check the filter.
1336 */
1337 if (qemu_timer_pending(dev->attach_timer) || dev->dev.attached) {
1338 if (usbredir_check_filter(dev)) {
1339 ERROR("Device no longer matches filter after interface info "
1340 "change, disconnecting!\n");
1341 }
1342 }
1343 }
1344
1345 static void usbredir_mark_speed_incompatible(USBRedirDevice *dev, int speed)
1346 {
1347 dev->compatible_speedmask &= ~(1 << speed);
1348 dev->dev.speedmask = (1 << dev->dev.speed) | dev->compatible_speedmask;
1349 }
1350
1351 static void usbredir_set_pipeline(USBRedirDevice *dev, struct USBEndpoint *uep)
1352 {
1353 if (uep->type != USB_ENDPOINT_XFER_BULK) {
1354 return;
1355 }
1356 if (uep->pid == USB_TOKEN_OUT) {
1357 uep->pipeline = true;
1358 }
1359 if (uep->pid == USB_TOKEN_IN && uep->max_packet_size != 0 &&
1360 usbredirparser_peer_has_cap(dev->parser,
1361 usb_redir_cap_32bits_bulk_length)) {
1362 uep->pipeline = true;
1363 }
1364 }
1365
1366 static void usbredir_setup_usb_eps(USBRedirDevice *dev)
1367 {
1368 struct USBEndpoint *usb_ep;
1369 int i;
1370
1371 for (i = 0; i < MAX_ENDPOINTS; i++) {
1372 usb_ep = I2USBEP(dev, i);
1373 usb_ep->type = dev->endpoint[i].type;
1374 usb_ep->ifnum = dev->endpoint[i].interface;
1375 usb_ep->max_packet_size = dev->endpoint[i].max_packet_size;
1376 usbredir_set_pipeline(dev, usb_ep);
1377 }
1378 }
1379
1380 static void usbredir_ep_info(void *priv,
1381 struct usb_redir_ep_info_header *ep_info)
1382 {
1383 USBRedirDevice *dev = priv;
1384 int i;
1385
1386 for (i = 0; i < MAX_ENDPOINTS; i++) {
1387 dev->endpoint[i].type = ep_info->type[i];
1388 dev->endpoint[i].interval = ep_info->interval[i];
1389 dev->endpoint[i].interface = ep_info->interface[i];
1390 if (usbredirparser_peer_has_cap(dev->parser,
1391 usb_redir_cap_ep_info_max_packet_size)) {
1392 dev->endpoint[i].max_packet_size = ep_info->max_packet_size[i];
1393 }
1394 switch (dev->endpoint[i].type) {
1395 case usb_redir_type_invalid:
1396 break;
1397 case usb_redir_type_iso:
1398 usbredir_mark_speed_incompatible(dev, USB_SPEED_FULL);
1399 usbredir_mark_speed_incompatible(dev, USB_SPEED_HIGH);
1400 /* Fall through */
1401 case usb_redir_type_interrupt:
1402 if (!usbredirparser_peer_has_cap(dev->parser,
1403 usb_redir_cap_ep_info_max_packet_size) ||
1404 ep_info->max_packet_size[i] > 64) {
1405 usbredir_mark_speed_incompatible(dev, USB_SPEED_FULL);
1406 }
1407 if (!usbredirparser_peer_has_cap(dev->parser,
1408 usb_redir_cap_ep_info_max_packet_size) ||
1409 ep_info->max_packet_size[i] > 1024) {
1410 usbredir_mark_speed_incompatible(dev, USB_SPEED_HIGH);
1411 }
1412 if (dev->endpoint[i].interval == 0) {
1413 ERROR("Received 0 interval for isoc or irq endpoint\n");
1414 usbredir_reject_device(dev);
1415 return;
1416 }
1417 /* Fall through */
1418 case usb_redir_type_control:
1419 case usb_redir_type_bulk:
1420 DPRINTF("ep: %02X type: %d interface: %d\n", I2EP(i),
1421 dev->endpoint[i].type, dev->endpoint[i].interface);
1422 break;
1423 default:
1424 ERROR("Received invalid endpoint type\n");
1425 usbredir_reject_device(dev);
1426 return;
1427 }
1428 }
1429 /* The new ep info may have caused a speed incompatibility, recheck */
1430 if (dev->dev.attached &&
1431 !(dev->dev.port->speedmask & dev->dev.speedmask)) {
1432 ERROR("Device no longer matches speed after endpoint info change, "
1433 "disconnecting!\n");
1434 usbredir_reject_device(dev);
1435 return;
1436 }
1437 usbredir_setup_usb_eps(dev);
1438 }
1439
1440 static void usbredir_configuration_status(void *priv, uint64_t id,
1441 struct usb_redir_configuration_status_header *config_status)
1442 {
1443 USBRedirDevice *dev = priv;
1444 USBPacket *p;
1445
1446 DPRINTF("set config status %d config %d id %"PRIu64"\n",
1447 config_status->status, config_status->configuration, id);
1448
1449 p = usbredir_find_packet_by_id(dev, 0, id);
1450 if (p) {
1451 if (dev->dev.setup_buf[0] & USB_DIR_IN) {
1452 dev->dev.data_buf[0] = config_status->configuration;
1453 p->actual_length = 1;
1454 }
1455 usbredir_handle_status(dev, p, config_status->status);
1456 usb_generic_async_ctrl_complete(&dev->dev, p);
1457 }
1458 }
1459
1460 static void usbredir_alt_setting_status(void *priv, uint64_t id,
1461 struct usb_redir_alt_setting_status_header *alt_setting_status)
1462 {
1463 USBRedirDevice *dev = priv;
1464 USBPacket *p;
1465
1466 DPRINTF("alt status %d intf %d alt %d id: %"PRIu64"\n",
1467 alt_setting_status->status, alt_setting_status->interface,
1468 alt_setting_status->alt, id);
1469
1470 p = usbredir_find_packet_by_id(dev, 0, id);
1471 if (p) {
1472 if (dev->dev.setup_buf[0] & USB_DIR_IN) {
1473 dev->dev.data_buf[0] = alt_setting_status->alt;
1474 p->actual_length = 1;
1475 }
1476 usbredir_handle_status(dev, p, alt_setting_status->status);
1477 usb_generic_async_ctrl_complete(&dev->dev, p);
1478 }
1479 }
1480
1481 static void usbredir_iso_stream_status(void *priv, uint64_t id,
1482 struct usb_redir_iso_stream_status_header *iso_stream_status)
1483 {
1484 USBRedirDevice *dev = priv;
1485 uint8_t ep = iso_stream_status->endpoint;
1486
1487 DPRINTF("iso status %d ep %02X id %"PRIu64"\n", iso_stream_status->status,
1488 ep, id);
1489
1490 if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].iso_started) {
1491 return;
1492 }
1493
1494 dev->endpoint[EP2I(ep)].iso_error = iso_stream_status->status;
1495 if (iso_stream_status->status == usb_redir_stall) {
1496 DPRINTF("iso stream stopped by peer ep %02X\n", ep);
1497 dev->endpoint[EP2I(ep)].iso_started = 0;
1498 }
1499 }
1500
1501 static void usbredir_interrupt_receiving_status(void *priv, uint64_t id,
1502 struct usb_redir_interrupt_receiving_status_header
1503 *interrupt_receiving_status)
1504 {
1505 USBRedirDevice *dev = priv;
1506 uint8_t ep = interrupt_receiving_status->endpoint;
1507
1508 DPRINTF("interrupt recv status %d ep %02X id %"PRIu64"\n",
1509 interrupt_receiving_status->status, ep, id);
1510
1511 if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].interrupt_started) {
1512 return;
1513 }
1514
1515 dev->endpoint[EP2I(ep)].interrupt_error =
1516 interrupt_receiving_status->status;
1517 if (interrupt_receiving_status->status == usb_redir_stall) {
1518 DPRINTF("interrupt receiving stopped by peer ep %02X\n", ep);
1519 dev->endpoint[EP2I(ep)].interrupt_started = 0;
1520 }
1521 }
1522
1523 static void usbredir_bulk_streams_status(void *priv, uint64_t id,
1524 struct usb_redir_bulk_streams_status_header *bulk_streams_status)
1525 {
1526 }
1527
1528 static void usbredir_control_packet(void *priv, uint64_t id,
1529 struct usb_redir_control_packet_header *control_packet,
1530 uint8_t *data, int data_len)
1531 {
1532 USBRedirDevice *dev = priv;
1533 USBPacket *p;
1534 int len = control_packet->length;
1535
1536 DPRINTF("ctrl-in status %d len %d id %"PRIu64"\n", control_packet->status,
1537 len, id);
1538
1539 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
1540 * to work redirected to a not superspeed capable hcd */
1541 if (dev->dev.speed == USB_SPEED_SUPER &&
1542 !((dev->dev.port->speedmask & USB_SPEED_MASK_SUPER)) &&
1543 control_packet->requesttype == 0x80 &&
1544 control_packet->request == 6 &&
1545 control_packet->value == 0x100 && control_packet->index == 0 &&
1546 data_len >= 18 && data[7] == 9) {
1547 data[7] = 64;
1548 }
1549
1550 p = usbredir_find_packet_by_id(dev, 0, id);
1551 if (p) {
1552 usbredir_handle_status(dev, p, control_packet->status);
1553 if (data_len > 0) {
1554 usbredir_log_data(dev, "ctrl data in:", data, data_len);
1555 if (data_len > sizeof(dev->dev.data_buf)) {
1556 ERROR("ctrl buffer too small (%d > %zu)\n",
1557 data_len, sizeof(dev->dev.data_buf));
1558 p->status = USB_RET_STALL;
1559 data_len = len = sizeof(dev->dev.data_buf);
1560 }
1561 memcpy(dev->dev.data_buf, data, data_len);
1562 }
1563 p->actual_length = len;
1564 usb_generic_async_ctrl_complete(&dev->dev, p);
1565 }
1566 free(data);
1567 }
1568
1569 static void usbredir_bulk_packet(void *priv, uint64_t id,
1570 struct usb_redir_bulk_packet_header *bulk_packet,
1571 uint8_t *data, int data_len)
1572 {
1573 USBRedirDevice *dev = priv;
1574 uint8_t ep = bulk_packet->endpoint;
1575 int len = (bulk_packet->length_high << 16) | bulk_packet->length;
1576 USBPacket *p;
1577
1578 DPRINTF("bulk-in status %d ep %02X len %d id %"PRIu64"\n",
1579 bulk_packet->status, ep, len, id);
1580
1581 p = usbredir_find_packet_by_id(dev, ep, id);
1582 if (p) {
1583 size_t size = (p->combined) ? p->combined->iov.size : p->iov.size;
1584 usbredir_handle_status(dev, p, bulk_packet->status);
1585 if (data_len > 0) {
1586 usbredir_log_data(dev, "bulk data in:", data, data_len);
1587 if (data_len > size) {
1588 ERROR("bulk got more data then requested (%d > %zd)\n",
1589 data_len, p->iov.size);
1590 p->status = USB_RET_BABBLE;
1591 data_len = len = size;
1592 }
1593 if (p->combined) {
1594 iov_from_buf(p->combined->iov.iov, p->combined->iov.niov,
1595 0, data, data_len);
1596 } else {
1597 usb_packet_copy(p, data, data_len);
1598 }
1599 }
1600 p->actual_length = len;
1601 if (p->pid == USB_TOKEN_IN && p->ep->pipeline) {
1602 usb_combined_input_packet_complete(&dev->dev, p);
1603 } else {
1604 usb_packet_complete(&dev->dev, p);
1605 }
1606 }
1607 free(data);
1608 }
1609
1610 static void usbredir_iso_packet(void *priv, uint64_t id,
1611 struct usb_redir_iso_packet_header *iso_packet,
1612 uint8_t *data, int data_len)
1613 {
1614 USBRedirDevice *dev = priv;
1615 uint8_t ep = iso_packet->endpoint;
1616
1617 DPRINTF2("iso-in status %d ep %02X len %d id %"PRIu64"\n",
1618 iso_packet->status, ep, data_len, id);
1619
1620 if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_ISOC) {
1621 ERROR("received iso packet for non iso endpoint %02X\n", ep);
1622 free(data);
1623 return;
1624 }
1625
1626 if (dev->endpoint[EP2I(ep)].iso_started == 0) {
1627 DPRINTF("received iso packet for non started stream ep %02X\n", ep);
1628 free(data);
1629 return;
1630 }
1631
1632 /* bufp_alloc also adds the packet to the ep queue */
1633 bufp_alloc(dev, data, data_len, iso_packet->status, ep);
1634 }
1635
1636 static void usbredir_interrupt_packet(void *priv, uint64_t id,
1637 struct usb_redir_interrupt_packet_header *interrupt_packet,
1638 uint8_t *data, int data_len)
1639 {
1640 USBRedirDevice *dev = priv;
1641 uint8_t ep = interrupt_packet->endpoint;
1642
1643 DPRINTF("interrupt-in status %d ep %02X len %d id %"PRIu64"\n",
1644 interrupt_packet->status, ep, data_len, id);
1645
1646 if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_INT) {
1647 ERROR("received int packet for non interrupt endpoint %02X\n", ep);
1648 free(data);
1649 return;
1650 }
1651
1652 if (ep & USB_DIR_IN) {
1653 if (dev->endpoint[EP2I(ep)].interrupt_started == 0) {
1654 DPRINTF("received int packet while not started ep %02X\n", ep);
1655 free(data);
1656 return;
1657 }
1658
1659 if (QTAILQ_EMPTY(&dev->endpoint[EP2I(ep)].bufpq)) {
1660 usb_wakeup(usb_ep_get(&dev->dev, USB_TOKEN_IN, ep & 0x0f));
1661 }
1662
1663 /* bufp_alloc also adds the packet to the ep queue */
1664 bufp_alloc(dev, data, data_len, interrupt_packet->status, ep);
1665 } else {
1666 /*
1667 * We report output interrupt packets as completed directly upon
1668 * submission, so all we can do here if one failed is warn.
1669 */
1670 if (interrupt_packet->status) {
1671 WARNING("interrupt output failed status %d ep %02X id %"PRIu64"\n",
1672 interrupt_packet->status, ep, id);
1673 }
1674 }
1675 }
1676
1677 /*
1678 * Migration code
1679 */
1680
1681 static void usbredir_pre_save(void *priv)
1682 {
1683 USBRedirDevice *dev = priv;
1684
1685 usbredir_fill_already_in_flight(dev);
1686 }
1687
1688 static int usbredir_post_load(void *priv, int version_id)
1689 {
1690 USBRedirDevice *dev = priv;
1691
1692 switch (dev->device_info.speed) {
1693 case usb_redir_speed_low:
1694 dev->dev.speed = USB_SPEED_LOW;
1695 break;
1696 case usb_redir_speed_full:
1697 dev->dev.speed = USB_SPEED_FULL;
1698 break;
1699 case usb_redir_speed_high:
1700 dev->dev.speed = USB_SPEED_HIGH;
1701 break;
1702 case usb_redir_speed_super:
1703 dev->dev.speed = USB_SPEED_SUPER;
1704 break;
1705 default:
1706 dev->dev.speed = USB_SPEED_FULL;
1707 }
1708 dev->dev.speedmask = (1 << dev->dev.speed);
1709
1710 usbredir_setup_usb_eps(dev);
1711
1712 return 0;
1713 }
1714
1715 /* For usbredirparser migration */
1716 static void usbredir_put_parser(QEMUFile *f, void *priv, size_t unused)
1717 {
1718 USBRedirDevice *dev = priv;
1719 uint8_t *data;
1720 int len;
1721
1722 if (dev->parser == NULL) {
1723 qemu_put_be32(f, 0);
1724 return;
1725 }
1726
1727 usbredirparser_serialize(dev->parser, &data, &len);
1728 qemu_oom_check(data);
1729
1730 qemu_put_be32(f, len);
1731 qemu_put_buffer(f, data, len);
1732
1733 free(data);
1734 }
1735
1736 static int usbredir_get_parser(QEMUFile *f, void *priv, size_t unused)
1737 {
1738 USBRedirDevice *dev = priv;
1739 uint8_t *data;
1740 int len, ret;
1741
1742 len = qemu_get_be32(f);
1743 if (len == 0) {
1744 return 0;
1745 }
1746
1747 /*
1748 * If our chardev is not open already at this point the usbredir connection
1749 * has been broken (non seamless migration, or restore from disk).
1750 *
1751 * In this case create a temporary parser to receive the migration data,
1752 * and schedule the close_bh to report the device as disconnected to the
1753 * guest and to destroy the parser again.
1754 */
1755 if (dev->parser == NULL) {
1756 WARNING("usb-redir connection broken during migration\n");
1757 usbredir_create_parser(dev);
1758 qemu_bh_schedule(dev->chardev_close_bh);
1759 }
1760
1761 data = g_malloc(len);
1762 qemu_get_buffer(f, data, len);
1763
1764 ret = usbredirparser_unserialize(dev->parser, data, len);
1765
1766 g_free(data);
1767
1768 return ret;
1769 }
1770
1771 static const VMStateInfo usbredir_parser_vmstate_info = {
1772 .name = "usb-redir-parser",
1773 .put = usbredir_put_parser,
1774 .get = usbredir_get_parser,
1775 };
1776
1777
1778 /* For buffered packets (iso/irq) queue migration */
1779 static void usbredir_put_bufpq(QEMUFile *f, void *priv, size_t unused)
1780 {
1781 struct endp_data *endp = priv;
1782 struct buf_packet *bufp;
1783 int remain = endp->bufpq_size;
1784
1785 qemu_put_be32(f, endp->bufpq_size);
1786 QTAILQ_FOREACH(bufp, &endp->bufpq, next) {
1787 qemu_put_be32(f, bufp->len);
1788 qemu_put_be32(f, bufp->status);
1789 qemu_put_buffer(f, bufp->data, bufp->len);
1790 remain--;
1791 }
1792 assert(remain == 0);
1793 }
1794
1795 static int usbredir_get_bufpq(QEMUFile *f, void *priv, size_t unused)
1796 {
1797 struct endp_data *endp = priv;
1798 struct buf_packet *bufp;
1799 int i;
1800
1801 endp->bufpq_size = qemu_get_be32(f);
1802 for (i = 0; i < endp->bufpq_size; i++) {
1803 bufp = g_malloc(sizeof(struct buf_packet));
1804 bufp->len = qemu_get_be32(f);
1805 bufp->status = qemu_get_be32(f);
1806 bufp->data = qemu_oom_check(malloc(bufp->len)); /* regular malloc! */
1807 qemu_get_buffer(f, bufp->data, bufp->len);
1808 QTAILQ_INSERT_TAIL(&endp->bufpq, bufp, next);
1809 }
1810 return 0;
1811 }
1812
1813 static const VMStateInfo usbredir_ep_bufpq_vmstate_info = {
1814 .name = "usb-redir-bufpq",
1815 .put = usbredir_put_bufpq,
1816 .get = usbredir_get_bufpq,
1817 };
1818
1819
1820 /* For endp_data migration */
1821 static const VMStateDescription usbredir_ep_vmstate = {
1822 .name = "usb-redir-ep",
1823 .version_id = 1,
1824 .minimum_version_id = 1,
1825 .fields = (VMStateField[]) {
1826 VMSTATE_UINT8(type, struct endp_data),
1827 VMSTATE_UINT8(interval, struct endp_data),
1828 VMSTATE_UINT8(interface, struct endp_data),
1829 VMSTATE_UINT16(max_packet_size, struct endp_data),
1830 VMSTATE_UINT8(iso_started, struct endp_data),
1831 VMSTATE_UINT8(iso_error, struct endp_data),
1832 VMSTATE_UINT8(interrupt_started, struct endp_data),
1833 VMSTATE_UINT8(interrupt_error, struct endp_data),
1834 VMSTATE_UINT8(bufpq_prefilled, struct endp_data),
1835 VMSTATE_UINT8(bufpq_dropping_packets, struct endp_data),
1836 {
1837 .name = "bufpq",
1838 .version_id = 0,
1839 .field_exists = NULL,
1840 .size = 0,
1841 .info = &usbredir_ep_bufpq_vmstate_info,
1842 .flags = VMS_SINGLE,
1843 .offset = 0,
1844 },
1845 VMSTATE_INT32(bufpq_target_size, struct endp_data),
1846 VMSTATE_END_OF_LIST()
1847 }
1848 };
1849
1850
1851 /* For PacketIdQueue migration */
1852 static void usbredir_put_packet_id_q(QEMUFile *f, void *priv, size_t unused)
1853 {
1854 struct PacketIdQueue *q = priv;
1855 USBRedirDevice *dev = q->dev;
1856 struct PacketIdQueueEntry *e;
1857 int remain = q->size;
1858
1859 DPRINTF("put_packet_id_q %s size %d\n", q->name, q->size);
1860 qemu_put_be32(f, q->size);
1861 QTAILQ_FOREACH(e, &q->head, next) {
1862 qemu_put_be64(f, e->id);
1863 remain--;
1864 }
1865 assert(remain == 0);
1866 }
1867
1868 static int usbredir_get_packet_id_q(QEMUFile *f, void *priv, size_t unused)
1869 {
1870 struct PacketIdQueue *q = priv;
1871 USBRedirDevice *dev = q->dev;
1872 int i, size;
1873 uint64_t id;
1874
1875 size = qemu_get_be32(f);
1876 DPRINTF("get_packet_id_q %s size %d\n", q->name, size);
1877 for (i = 0; i < size; i++) {
1878 id = qemu_get_be64(f);
1879 packet_id_queue_add(q, id);
1880 }
1881 assert(q->size == size);
1882 return 0;
1883 }
1884
1885 static const VMStateInfo usbredir_ep_packet_id_q_vmstate_info = {
1886 .name = "usb-redir-packet-id-q",
1887 .put = usbredir_put_packet_id_q,
1888 .get = usbredir_get_packet_id_q,
1889 };
1890
1891 static const VMStateDescription usbredir_ep_packet_id_queue_vmstate = {
1892 .name = "usb-redir-packet-id-queue",
1893 .version_id = 1,
1894 .minimum_version_id = 1,
1895 .fields = (VMStateField[]) {
1896 {
1897 .name = "queue",
1898 .version_id = 0,
1899 .field_exists = NULL,
1900 .size = 0,
1901 .info = &usbredir_ep_packet_id_q_vmstate_info,
1902 .flags = VMS_SINGLE,
1903 .offset = 0,
1904 },
1905 VMSTATE_END_OF_LIST()
1906 }
1907 };
1908
1909
1910 /* For usb_redir_device_connect_header migration */
1911 static const VMStateDescription usbredir_device_info_vmstate = {
1912 .name = "usb-redir-device-info",
1913 .version_id = 1,
1914 .minimum_version_id = 1,
1915 .fields = (VMStateField[]) {
1916 VMSTATE_UINT8(speed, struct usb_redir_device_connect_header),
1917 VMSTATE_UINT8(device_class, struct usb_redir_device_connect_header),
1918 VMSTATE_UINT8(device_subclass, struct usb_redir_device_connect_header),
1919 VMSTATE_UINT8(device_protocol, struct usb_redir_device_connect_header),
1920 VMSTATE_UINT16(vendor_id, struct usb_redir_device_connect_header),
1921 VMSTATE_UINT16(product_id, struct usb_redir_device_connect_header),
1922 VMSTATE_UINT16(device_version_bcd,
1923 struct usb_redir_device_connect_header),
1924 VMSTATE_END_OF_LIST()
1925 }
1926 };
1927
1928
1929 /* For usb_redir_interface_info_header migration */
1930 static const VMStateDescription usbredir_interface_info_vmstate = {
1931 .name = "usb-redir-interface-info",
1932 .version_id = 1,
1933 .minimum_version_id = 1,
1934 .fields = (VMStateField[]) {
1935 VMSTATE_UINT32(interface_count,
1936 struct usb_redir_interface_info_header),
1937 VMSTATE_UINT8_ARRAY(interface,
1938 struct usb_redir_interface_info_header, 32),
1939 VMSTATE_UINT8_ARRAY(interface_class,
1940 struct usb_redir_interface_info_header, 32),
1941 VMSTATE_UINT8_ARRAY(interface_subclass,
1942 struct usb_redir_interface_info_header, 32),
1943 VMSTATE_UINT8_ARRAY(interface_protocol,
1944 struct usb_redir_interface_info_header, 32),
1945 VMSTATE_END_OF_LIST()
1946 }
1947 };
1948
1949
1950 /* And finally the USBRedirDevice vmstate itself */
1951 static const VMStateDescription usbredir_vmstate = {
1952 .name = "usb-redir",
1953 .version_id = 1,
1954 .minimum_version_id = 1,
1955 .pre_save = usbredir_pre_save,
1956 .post_load = usbredir_post_load,
1957 .fields = (VMStateField[]) {
1958 VMSTATE_USB_DEVICE(dev, USBRedirDevice),
1959 VMSTATE_TIMER(attach_timer, USBRedirDevice),
1960 {
1961 .name = "parser",
1962 .version_id = 0,
1963 .field_exists = NULL,
1964 .size = 0,
1965 .info = &usbredir_parser_vmstate_info,
1966 .flags = VMS_SINGLE,
1967 .offset = 0,
1968 },
1969 VMSTATE_STRUCT_ARRAY(endpoint, USBRedirDevice, MAX_ENDPOINTS, 1,
1970 usbredir_ep_vmstate, struct endp_data),
1971 VMSTATE_STRUCT(cancelled, USBRedirDevice, 1,
1972 usbredir_ep_packet_id_queue_vmstate,
1973 struct PacketIdQueue),
1974 VMSTATE_STRUCT(already_in_flight, USBRedirDevice, 1,
1975 usbredir_ep_packet_id_queue_vmstate,
1976 struct PacketIdQueue),
1977 VMSTATE_STRUCT(device_info, USBRedirDevice, 1,
1978 usbredir_device_info_vmstate,
1979 struct usb_redir_device_connect_header),
1980 VMSTATE_STRUCT(interface_info, USBRedirDevice, 1,
1981 usbredir_interface_info_vmstate,
1982 struct usb_redir_interface_info_header),
1983 VMSTATE_END_OF_LIST()
1984 }
1985 };
1986
1987 static Property usbredir_properties[] = {
1988 DEFINE_PROP_CHR("chardev", USBRedirDevice, cs),
1989 DEFINE_PROP_UINT8("debug", USBRedirDevice, debug, usbredirparser_warning),
1990 DEFINE_PROP_STRING("filter", USBRedirDevice, filter_str),
1991 DEFINE_PROP_INT32("bootindex", USBRedirDevice, bootindex, -1),
1992 DEFINE_PROP_END_OF_LIST(),
1993 };
1994
1995 static void usbredir_class_initfn(ObjectClass *klass, void *data)
1996 {
1997 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
1998 DeviceClass *dc = DEVICE_CLASS(klass);
1999
2000 uc->init = usbredir_initfn;
2001 uc->product_desc = "USB Redirection Device";
2002 uc->handle_destroy = usbredir_handle_destroy;
2003 uc->cancel_packet = usbredir_cancel_packet;
2004 uc->handle_reset = usbredir_handle_reset;
2005 uc->handle_data = usbredir_handle_data;
2006 uc->handle_control = usbredir_handle_control;
2007 uc->flush_ep_queue = usbredir_flush_ep_queue;
2008 uc->ep_stopped = usbredir_ep_stopped;
2009 dc->vmsd = &usbredir_vmstate;
2010 dc->props = usbredir_properties;
2011 }
2012
2013 static TypeInfo usbredir_dev_info = {
2014 .name = "usb-redir",
2015 .parent = TYPE_USB_DEVICE,
2016 .instance_size = sizeof(USBRedirDevice),
2017 .class_init = usbredir_class_initfn,
2018 };
2019
2020 static void usbredir_register_types(void)
2021 {
2022 type_register_static(&usbredir_dev_info);
2023 }
2024
2025 type_init(usbredir_register_types)