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