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