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