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