2 * USB redirector usb-guest
4 * Copyright (c) 2011-2012 Red Hat, Inc.
7 * Hans de Goede <hdegoede@redhat.com>
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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
28 #include "qemu-common.h"
29 #include "qemu-timer.h"
35 #include <sys/ioctl.h>
37 #include <usbredirparser.h>
38 #include <usbredirfilter.h>
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))
47 typedef struct USBRedirDevice USBRedirDevice
;
49 /* Struct to hold buffered packets (iso or int input packets) */
54 QTAILQ_ENTRY(buf_packet
)next
;
60 uint8_t interface
; /* bInterfaceNumber this ep belongs to */
61 uint16_t max_packet_size
; /* In bytes, not wMaxPacketSize format !! */
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
;
70 int32_t bufpq_target_size
;
73 struct PacketIdQueueEntry
{
75 QTAILQ_ENTRY(PacketIdQueueEntry
)next
;
78 struct PacketIdQueue
{
81 QTAILQ_HEAD(, PacketIdQueueEntry
) head
;
85 struct USBRedirDevice
{
92 /* Data passed from chardev the fd_read cb to the usbredirparser read cb */
93 const uint8_t *read_buf
;
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
;
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
);
144 static void usbredir_handle_status(USBRedirDevice
*dev
, USBPacket
*p
,
147 #define VERSION "qemu usb-redir guest " QEMU_VERSION
155 if (dev->debug >= usbredirparser_error) { \
156 error_report("usb-redir error: " __VA_ARGS__); \
159 #define WARNING(...) \
161 if (dev->debug >= usbredirparser_warning) { \
162 error_report("usb-redir warning: " __VA_ARGS__); \
167 if (dev->debug >= usbredirparser_info) { \
168 error_report("usb-redir: " __VA_ARGS__); \
171 #define DPRINTF(...) \
173 if (dev->debug >= usbredirparser_debug) { \
174 error_report("usb-redir: " __VA_ARGS__); \
177 #define DPRINTF2(...) \
179 if (dev->debug >= usbredirparser_debug_data) { \
180 error_report("usb-redir: " __VA_ARGS__); \
184 static void usbredir_log(void *priv
, int level
, const char *msg
)
186 USBRedirDevice
*dev
= priv
;
188 if (dev
->debug
< level
) {
192 error_report("%s", msg
);
195 static void usbredir_log_data(USBRedirDevice
*dev
, const char *desc
,
196 const uint8_t *data
, int len
)
200 if (dev
->debug
< usbredirparser_debug_data
) {
204 for (i
= 0; i
< len
; i
+= j
) {
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
]);
211 error_report("%s", buf
);
216 * usbredirparser io functions
219 static int usbredir_read(void *priv
, uint8_t *data
, int count
)
221 USBRedirDevice
*dev
= priv
;
223 if (dev
->read_buf_size
< count
) {
224 count
= dev
->read_buf_size
;
227 memcpy(data
, dev
->read_buf
, count
);
229 dev
->read_buf_size
-= count
;
230 if (dev
->read_buf_size
) {
231 dev
->read_buf
+= count
;
233 dev
->read_buf
= NULL
;
239 static int usbredir_write(void *priv
, uint8_t *data
, int count
)
241 USBRedirDevice
*dev
= priv
;
243 if (!dev
->cs
->opened
) {
247 /* Don't send new data to the chardev until our state is fully synced */
248 if (!runstate_check(RUN_STATE_RUNNING
)) {
252 return qemu_chr_fe_write(dev
->cs
, data
, count
);
256 * Cancelled and buffered packets helpers
259 static void packet_id_queue_init(struct PacketIdQueue
*q
,
260 USBRedirDevice
*dev
, const char *name
)
264 QTAILQ_INIT(&q
->head
);
268 static void packet_id_queue_add(struct PacketIdQueue
*q
, uint64_t id
)
270 USBRedirDevice
*dev
= q
->dev
;
271 struct PacketIdQueueEntry
*e
;
273 DPRINTF("adding packet id %"PRIu64
" to %s queue\n", id
, q
->name
);
275 e
= g_malloc0(sizeof(struct PacketIdQueueEntry
));
277 QTAILQ_INSERT_TAIL(&q
->head
, e
, next
);
281 static int packet_id_queue_remove(struct PacketIdQueue
*q
, uint64_t id
)
283 USBRedirDevice
*dev
= q
->dev
;
284 struct PacketIdQueueEntry
*e
;
286 QTAILQ_FOREACH(e
, &q
->head
, next
) {
288 DPRINTF("removing packet id %"PRIu64
" from %s queue\n",
290 QTAILQ_REMOVE(&q
->head
, e
, next
);
299 static void packet_id_queue_empty(struct PacketIdQueue
*q
)
301 USBRedirDevice
*dev
= q
->dev
;
302 struct PacketIdQueueEntry
*e
, *next_e
;
304 DPRINTF("removing %d packet-ids from %s queue\n", q
->size
, q
->name
);
306 QTAILQ_FOREACH_SAFE(e
, &q
->head
, next
, next_e
) {
307 QTAILQ_REMOVE(&q
->head
, e
, next
);
313 static void usbredir_cancel_packet(USBDevice
*udev
, USBPacket
*p
)
315 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
318 usb_combined_packet_cancel(udev
, p
);
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
);
327 static int usbredir_is_cancelled(USBRedirDevice
*dev
, uint64_t id
)
329 if (!dev
->dev
.attached
) {
330 return 1; /* Treat everything as cancelled after a disconnect */
332 return packet_id_queue_remove(&dev
->cancelled
, id
);
335 static void usbredir_fill_already_in_flight_from_ep(USBRedirDevice
*dev
,
336 struct USBEndpoint
*ep
)
340 QTAILQ_FOREACH(p
, &ep
->queue
, queue
) {
341 /* Skip combined packets, except for the first */
342 if (p
->combined
&& p
!= p
->combined
->first
) {
345 if (p
->state
== USB_PACKET_ASYNC
) {
346 packet_id_queue_add(&dev
->already_in_flight
, p
->id
);
351 static void usbredir_fill_already_in_flight(USBRedirDevice
*dev
)
354 struct USBDevice
*udev
= &dev
->dev
;
356 usbredir_fill_already_in_flight_from_ep(dev
, &udev
->ep_ctl
);
358 for (ep
= 0; ep
< USB_MAX_ENDPOINTS
; ep
++) {
359 usbredir_fill_already_in_flight_from_ep(dev
, &udev
->ep_in
[ep
]);
360 usbredir_fill_already_in_flight_from_ep(dev
, &udev
->ep_out
[ep
]);
364 static int usbredir_already_in_flight(USBRedirDevice
*dev
, uint64_t id
)
366 return packet_id_queue_remove(&dev
->already_in_flight
, id
);
369 static USBPacket
*usbredir_find_packet_by_id(USBRedirDevice
*dev
,
370 uint8_t ep
, uint64_t id
)
374 if (usbredir_is_cancelled(dev
, id
)) {
378 p
= usb_ep_find_packet_by_id(&dev
->dev
,
379 (ep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
,
382 ERROR("could not find packet with id %"PRIu64
"\n", id
);
387 static void bufp_alloc(USBRedirDevice
*dev
,
388 uint8_t *data
, int len
, int status
, uint8_t ep
)
390 struct buf_packet
*bufp
;
392 if (!dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
&&
393 dev
->endpoint
[EP2I(ep
)].bufpq_size
>
394 2 * dev
->endpoint
[EP2I(ep
)].bufpq_target_size
) {
395 DPRINTF("bufpq overflow, dropping packets ep %02X\n", ep
);
396 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 1;
398 /* Since we're interupting the stream anyways, drop enough packets to get
399 back to our target buffer size */
400 if (dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
) {
401 if (dev
->endpoint
[EP2I(ep
)].bufpq_size
>
402 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
) {
406 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 0;
409 bufp
= g_malloc(sizeof(struct buf_packet
));
412 bufp
->status
= status
;
413 QTAILQ_INSERT_TAIL(&dev
->endpoint
[EP2I(ep
)].bufpq
, bufp
, next
);
414 dev
->endpoint
[EP2I(ep
)].bufpq_size
++;
417 static void bufp_free(USBRedirDevice
*dev
, struct buf_packet
*bufp
,
420 QTAILQ_REMOVE(&dev
->endpoint
[EP2I(ep
)].bufpq
, bufp
, next
);
421 dev
->endpoint
[EP2I(ep
)].bufpq_size
--;
426 static void usbredir_free_bufpq(USBRedirDevice
*dev
, uint8_t ep
)
428 struct buf_packet
*buf
, *buf_next
;
430 QTAILQ_FOREACH_SAFE(buf
, &dev
->endpoint
[EP2I(ep
)].bufpq
, next
, buf_next
) {
431 bufp_free(dev
, buf
, ep
);
436 * USBDevice callbacks
439 static void usbredir_handle_reset(USBDevice
*udev
)
441 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
443 DPRINTF("reset device\n");
444 usbredirparser_send_reset(dev
->parser
);
445 usbredirparser_do_write(dev
->parser
);
448 static void usbredir_handle_iso_data(USBRedirDevice
*dev
, USBPacket
*p
,
452 if (!dev
->endpoint
[EP2I(ep
)].iso_started
&&
453 !dev
->endpoint
[EP2I(ep
)].iso_error
) {
454 struct usb_redir_start_iso_stream_header start_iso
= {
459 if (dev
->dev
.speed
== USB_SPEED_HIGH
) {
460 pkts_per_sec
= 8000 / dev
->endpoint
[EP2I(ep
)].interval
;
462 pkts_per_sec
= 1000 / dev
->endpoint
[EP2I(ep
)].interval
;
464 /* Testing has shown that we need circa 60 ms buffer */
465 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
= (pkts_per_sec
* 60) / 1000;
467 /* Aim for approx 100 interrupts / second on the client to
468 balance latency and interrupt load */
469 start_iso
.pkts_per_urb
= pkts_per_sec
/ 100;
470 if (start_iso
.pkts_per_urb
< 1) {
471 start_iso
.pkts_per_urb
= 1;
472 } else if (start_iso
.pkts_per_urb
> 32) {
473 start_iso
.pkts_per_urb
= 32;
476 start_iso
.no_urbs
= (dev
->endpoint
[EP2I(ep
)].bufpq_target_size
+
477 start_iso
.pkts_per_urb
- 1) /
478 start_iso
.pkts_per_urb
;
479 /* Output endpoints pre-fill only 1/2 of the packets, keeping the rest
480 as overflow buffer. Also see the usbredir protocol documentation */
481 if (!(ep
& USB_DIR_IN
)) {
482 start_iso
.no_urbs
*= 2;
484 if (start_iso
.no_urbs
> 16) {
485 start_iso
.no_urbs
= 16;
488 /* No id, we look at the ep when receiving a status back */
489 usbredirparser_send_start_iso_stream(dev
->parser
, 0, &start_iso
);
490 usbredirparser_do_write(dev
->parser
);
491 DPRINTF("iso stream started pkts/sec %d pkts/urb %d urbs %d ep %02X\n",
492 pkts_per_sec
, start_iso
.pkts_per_urb
, start_iso
.no_urbs
, ep
);
493 dev
->endpoint
[EP2I(ep
)].iso_started
= 1;
494 dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
= 0;
495 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 0;
498 if (ep
& USB_DIR_IN
) {
499 struct buf_packet
*isop
;
501 if (dev
->endpoint
[EP2I(ep
)].iso_started
&&
502 !dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
) {
503 if (dev
->endpoint
[EP2I(ep
)].bufpq_size
<
504 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
) {
507 dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
= 1;
510 isop
= QTAILQ_FIRST(&dev
->endpoint
[EP2I(ep
)].bufpq
);
512 DPRINTF("iso-token-in ep %02X, no isop, iso_error: %d\n",
513 ep
, dev
->endpoint
[EP2I(ep
)].iso_error
);
514 /* Re-fill the buffer */
515 dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
= 0;
516 /* Check iso_error for stream errors, otherwise its an underrun */
517 status
= dev
->endpoint
[EP2I(ep
)].iso_error
;
518 dev
->endpoint
[EP2I(ep
)].iso_error
= 0;
519 p
->status
= status
? USB_RET_IOERROR
: USB_RET_SUCCESS
;
522 DPRINTF2("iso-token-in ep %02X status %d len %d queue-size: %d\n", ep
,
523 isop
->status
, isop
->len
, dev
->endpoint
[EP2I(ep
)].bufpq_size
);
525 status
= isop
->status
;
527 if (len
> p
->iov
.size
) {
528 ERROR("received iso data is larger then packet ep %02X (%d > %d)\n",
529 ep
, len
, (int)p
->iov
.size
);
531 status
= usb_redir_babble
;
533 usb_packet_copy(p
, isop
->data
, len
);
534 bufp_free(dev
, isop
, ep
);
535 usbredir_handle_status(dev
, p
, status
);
537 /* If the stream was not started because of a pending error don't
538 send the packet to the usb-host */
539 if (dev
->endpoint
[EP2I(ep
)].iso_started
) {
540 struct usb_redir_iso_packet_header iso_packet
= {
542 .length
= p
->iov
.size
544 uint8_t buf
[p
->iov
.size
];
545 /* No id, we look at the ep when receiving a status back */
546 usb_packet_copy(p
, buf
, p
->iov
.size
);
547 usbredirparser_send_iso_packet(dev
->parser
, 0, &iso_packet
,
549 usbredirparser_do_write(dev
->parser
);
551 status
= dev
->endpoint
[EP2I(ep
)].iso_error
;
552 dev
->endpoint
[EP2I(ep
)].iso_error
= 0;
553 DPRINTF2("iso-token-out ep %02X status %d len %zd\n", ep
, status
,
555 usbredir_handle_status(dev
, p
, status
);
559 static void usbredir_stop_iso_stream(USBRedirDevice
*dev
, uint8_t ep
)
561 struct usb_redir_stop_iso_stream_header stop_iso_stream
= {
564 if (dev
->endpoint
[EP2I(ep
)].iso_started
) {
565 usbredirparser_send_stop_iso_stream(dev
->parser
, 0, &stop_iso_stream
);
566 DPRINTF("iso stream stopped ep %02X\n", ep
);
567 dev
->endpoint
[EP2I(ep
)].iso_started
= 0;
569 dev
->endpoint
[EP2I(ep
)].iso_error
= 0;
570 usbredir_free_bufpq(dev
, ep
);
573 static void usbredir_handle_bulk_data(USBRedirDevice
*dev
, USBPacket
*p
,
576 struct usb_redir_bulk_packet_header bulk_packet
;
577 size_t size
= (p
->combined
) ? p
->combined
->iov
.size
: p
->iov
.size
;
579 DPRINTF("bulk-out ep %02X len %zd id %"PRIu64
"\n", ep
, size
, p
->id
);
581 if (usbredir_already_in_flight(dev
, p
->id
)) {
582 p
->status
= USB_RET_ASYNC
;
586 bulk_packet
.endpoint
= ep
;
587 bulk_packet
.length
= size
;
588 bulk_packet
.stream_id
= 0;
589 bulk_packet
.length_high
= size
>> 16;
590 assert(bulk_packet
.length_high
== 0 ||
591 usbredirparser_peer_has_cap(dev
->parser
,
592 usb_redir_cap_32bits_bulk_length
));
594 if (ep
& USB_DIR_IN
) {
595 usbredirparser_send_bulk_packet(dev
->parser
, p
->id
,
596 &bulk_packet
, NULL
, 0);
600 iov_to_buf(p
->combined
->iov
.iov
, p
->combined
->iov
.niov
,
603 usb_packet_copy(p
, buf
, size
);
605 usbredir_log_data(dev
, "bulk data out:", buf
, size
);
606 usbredirparser_send_bulk_packet(dev
->parser
, p
->id
,
607 &bulk_packet
, buf
, size
);
609 usbredirparser_do_write(dev
->parser
);
610 p
->status
= USB_RET_ASYNC
;
613 static void usbredir_handle_interrupt_data(USBRedirDevice
*dev
,
614 USBPacket
*p
, uint8_t ep
)
616 if (ep
& USB_DIR_IN
) {
617 /* Input interrupt endpoint, buffered packet input */
618 struct buf_packet
*intp
;
621 if (!dev
->endpoint
[EP2I(ep
)].interrupt_started
&&
622 !dev
->endpoint
[EP2I(ep
)].interrupt_error
) {
623 struct usb_redir_start_interrupt_receiving_header start_int
= {
626 /* No id, we look at the ep when receiving a status back */
627 usbredirparser_send_start_interrupt_receiving(dev
->parser
, 0,
629 usbredirparser_do_write(dev
->parser
);
630 DPRINTF("interrupt recv started ep %02X\n", ep
);
631 dev
->endpoint
[EP2I(ep
)].interrupt_started
= 1;
632 /* We don't really want to drop interrupt packets ever, but
633 having some upper limit to how much we buffer is good. */
634 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
= 1000;
635 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 0;
638 intp
= QTAILQ_FIRST(&dev
->endpoint
[EP2I(ep
)].bufpq
);
640 DPRINTF2("interrupt-token-in ep %02X, no intp\n", ep
);
641 /* Check interrupt_error for stream errors */
642 status
= dev
->endpoint
[EP2I(ep
)].interrupt_error
;
643 dev
->endpoint
[EP2I(ep
)].interrupt_error
= 0;
645 usbredir_handle_status(dev
, p
, status
);
647 p
->status
= USB_RET_NAK
;
651 DPRINTF("interrupt-token-in ep %02X status %d len %d\n", ep
,
652 intp
->status
, intp
->len
);
654 status
= intp
->status
;
656 if (len
> p
->iov
.size
) {
657 ERROR("received int data is larger then packet ep %02X\n", ep
);
659 status
= usb_redir_babble
;
661 usb_packet_copy(p
, intp
->data
, len
);
662 bufp_free(dev
, intp
, ep
);
663 usbredir_handle_status(dev
, p
, status
);
665 /* Output interrupt endpoint, normal async operation */
666 struct usb_redir_interrupt_packet_header interrupt_packet
;
667 uint8_t buf
[p
->iov
.size
];
669 DPRINTF("interrupt-out ep %02X len %zd id %"PRIu64
"\n", ep
,
672 if (usbredir_already_in_flight(dev
, p
->id
)) {
673 p
->status
= USB_RET_ASYNC
;
677 interrupt_packet
.endpoint
= ep
;
678 interrupt_packet
.length
= p
->iov
.size
;
680 usb_packet_copy(p
, buf
, p
->iov
.size
);
681 usbredir_log_data(dev
, "interrupt data out:", buf
, p
->iov
.size
);
682 usbredirparser_send_interrupt_packet(dev
->parser
, p
->id
,
683 &interrupt_packet
, buf
, p
->iov
.size
);
684 usbredirparser_do_write(dev
->parser
);
685 p
->status
= USB_RET_ASYNC
;
689 static void usbredir_stop_interrupt_receiving(USBRedirDevice
*dev
,
692 struct usb_redir_stop_interrupt_receiving_header stop_interrupt_recv
= {
695 if (dev
->endpoint
[EP2I(ep
)].interrupt_started
) {
696 usbredirparser_send_stop_interrupt_receiving(dev
->parser
, 0,
697 &stop_interrupt_recv
);
698 DPRINTF("interrupt recv stopped ep %02X\n", ep
);
699 dev
->endpoint
[EP2I(ep
)].interrupt_started
= 0;
701 dev
->endpoint
[EP2I(ep
)].interrupt_error
= 0;
702 usbredir_free_bufpq(dev
, ep
);
705 static void usbredir_handle_data(USBDevice
*udev
, USBPacket
*p
)
707 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
711 if (p
->pid
== USB_TOKEN_IN
) {
715 switch (dev
->endpoint
[EP2I(ep
)].type
) {
716 case USB_ENDPOINT_XFER_CONTROL
:
717 ERROR("handle_data called for control transfer on ep %02X\n", ep
);
718 p
->status
= USB_RET_NAK
;
720 case USB_ENDPOINT_XFER_ISOC
:
721 usbredir_handle_iso_data(dev
, p
, ep
);
723 case USB_ENDPOINT_XFER_BULK
:
724 if (p
->state
== USB_PACKET_SETUP
&& p
->pid
== USB_TOKEN_IN
&&
726 p
->status
= USB_RET_ADD_TO_QUEUE
;
729 usbredir_handle_bulk_data(dev
, p
, ep
);
731 case USB_ENDPOINT_XFER_INT
:
732 usbredir_handle_interrupt_data(dev
, p
, ep
);
735 ERROR("handle_data ep %02X has unknown type %d\n", ep
,
736 dev
->endpoint
[EP2I(ep
)].type
);
737 p
->status
= USB_RET_NAK
;
741 static void usbredir_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
743 if (ep
->pid
== USB_TOKEN_IN
&& ep
->pipeline
) {
744 usb_ep_combine_input_packets(ep
);
748 static void usbredir_set_config(USBRedirDevice
*dev
, USBPacket
*p
,
751 struct usb_redir_set_configuration_header set_config
;
754 DPRINTF("set config %d id %"PRIu64
"\n", config
, p
->id
);
756 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
757 switch (dev
->endpoint
[i
].type
) {
758 case USB_ENDPOINT_XFER_ISOC
:
759 usbredir_stop_iso_stream(dev
, I2EP(i
));
761 case USB_ENDPOINT_XFER_INT
:
763 usbredir_stop_interrupt_receiving(dev
, I2EP(i
));
767 usbredir_free_bufpq(dev
, I2EP(i
));
770 set_config
.configuration
= config
;
771 usbredirparser_send_set_configuration(dev
->parser
, p
->id
, &set_config
);
772 usbredirparser_do_write(dev
->parser
);
773 p
->status
= USB_RET_ASYNC
;
776 static void usbredir_get_config(USBRedirDevice
*dev
, USBPacket
*p
)
778 DPRINTF("get config id %"PRIu64
"\n", p
->id
);
780 usbredirparser_send_get_configuration(dev
->parser
, p
->id
);
781 usbredirparser_do_write(dev
->parser
);
782 p
->status
= USB_RET_ASYNC
;
785 static void usbredir_set_interface(USBRedirDevice
*dev
, USBPacket
*p
,
786 int interface
, int alt
)
788 struct usb_redir_set_alt_setting_header set_alt
;
791 DPRINTF("set interface %d alt %d id %"PRIu64
"\n", interface
, alt
, p
->id
);
793 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
794 if (dev
->endpoint
[i
].interface
== interface
) {
795 switch (dev
->endpoint
[i
].type
) {
796 case USB_ENDPOINT_XFER_ISOC
:
797 usbredir_stop_iso_stream(dev
, I2EP(i
));
799 case USB_ENDPOINT_XFER_INT
:
801 usbredir_stop_interrupt_receiving(dev
, I2EP(i
));
805 usbredir_free_bufpq(dev
, I2EP(i
));
809 set_alt
.interface
= interface
;
811 usbredirparser_send_set_alt_setting(dev
->parser
, p
->id
, &set_alt
);
812 usbredirparser_do_write(dev
->parser
);
813 p
->status
= USB_RET_ASYNC
;
816 static void usbredir_get_interface(USBRedirDevice
*dev
, USBPacket
*p
,
819 struct usb_redir_get_alt_setting_header get_alt
;
821 DPRINTF("get interface %d id %"PRIu64
"\n", interface
, p
->id
);
823 get_alt
.interface
= interface
;
824 usbredirparser_send_get_alt_setting(dev
->parser
, p
->id
, &get_alt
);
825 usbredirparser_do_write(dev
->parser
);
826 p
->status
= USB_RET_ASYNC
;
829 static void usbredir_handle_control(USBDevice
*udev
, USBPacket
*p
,
830 int request
, int value
, int index
, int length
, uint8_t *data
)
832 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
833 struct usb_redir_control_packet_header control_packet
;
835 if (usbredir_already_in_flight(dev
, p
->id
)) {
836 p
->status
= USB_RET_ASYNC
;
840 /* Special cases for certain standard device requests */
842 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
843 DPRINTF("set address %d\n", value
);
844 dev
->dev
.addr
= value
;
846 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
847 usbredir_set_config(dev
, p
, value
& 0xff);
849 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
850 usbredir_get_config(dev
, p
);
852 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
853 usbredir_set_interface(dev
, p
, index
, value
);
855 case InterfaceRequest
| USB_REQ_GET_INTERFACE
:
856 usbredir_get_interface(dev
, p
, index
);
860 /* Normal ctrl requests, note request is (bRequestType << 8) | bRequest */
862 "ctrl-out type 0x%x req 0x%x val 0x%x index %d len %d id %"PRIu64
"\n",
863 request
>> 8, request
& 0xff, value
, index
, length
, p
->id
);
865 control_packet
.request
= request
& 0xFF;
866 control_packet
.requesttype
= request
>> 8;
867 control_packet
.endpoint
= control_packet
.requesttype
& USB_DIR_IN
;
868 control_packet
.value
= value
;
869 control_packet
.index
= index
;
870 control_packet
.length
= length
;
872 if (control_packet
.requesttype
& USB_DIR_IN
) {
873 usbredirparser_send_control_packet(dev
->parser
, p
->id
,
874 &control_packet
, NULL
, 0);
876 usbredir_log_data(dev
, "ctrl data out:", data
, length
);
877 usbredirparser_send_control_packet(dev
->parser
, p
->id
,
878 &control_packet
, data
, length
);
880 usbredirparser_do_write(dev
->parser
);
881 p
->status
= USB_RET_ASYNC
;
885 * Close events can be triggered by usbredirparser_do_write which gets called
886 * from within the USBDevice data / control packet callbacks and doing a
887 * usb_detach from within these callbacks is not a good idea.
889 * So we use a bh handler to take care of close events.
891 static void usbredir_chardev_close_bh(void *opaque
)
893 USBRedirDevice
*dev
= opaque
;
895 usbredir_device_disconnect(dev
);
898 DPRINTF("destroying usbredirparser\n");
899 usbredirparser_destroy(dev
->parser
);
904 static void usbredir_create_parser(USBRedirDevice
*dev
)
906 uint32_t caps
[USB_REDIR_CAPS_SIZE
] = { 0, };
909 DPRINTF("creating usbredirparser\n");
911 dev
->parser
= qemu_oom_check(usbredirparser_create());
912 dev
->parser
->priv
= dev
;
913 dev
->parser
->log_func
= usbredir_log
;
914 dev
->parser
->read_func
= usbredir_read
;
915 dev
->parser
->write_func
= usbredir_write
;
916 dev
->parser
->hello_func
= usbredir_hello
;
917 dev
->parser
->device_connect_func
= usbredir_device_connect
;
918 dev
->parser
->device_disconnect_func
= usbredir_device_disconnect
;
919 dev
->parser
->interface_info_func
= usbredir_interface_info
;
920 dev
->parser
->ep_info_func
= usbredir_ep_info
;
921 dev
->parser
->configuration_status_func
= usbredir_configuration_status
;
922 dev
->parser
->alt_setting_status_func
= usbredir_alt_setting_status
;
923 dev
->parser
->iso_stream_status_func
= usbredir_iso_stream_status
;
924 dev
->parser
->interrupt_receiving_status_func
=
925 usbredir_interrupt_receiving_status
;
926 dev
->parser
->bulk_streams_status_func
= usbredir_bulk_streams_status
;
927 dev
->parser
->control_packet_func
= usbredir_control_packet
;
928 dev
->parser
->bulk_packet_func
= usbredir_bulk_packet
;
929 dev
->parser
->iso_packet_func
= usbredir_iso_packet
;
930 dev
->parser
->interrupt_packet_func
= usbredir_interrupt_packet
;
931 dev
->read_buf
= NULL
;
932 dev
->read_buf_size
= 0;
934 usbredirparser_caps_set_cap(caps
, usb_redir_cap_connect_device_version
);
935 usbredirparser_caps_set_cap(caps
, usb_redir_cap_filter
);
936 usbredirparser_caps_set_cap(caps
, usb_redir_cap_ep_info_max_packet_size
);
937 usbredirparser_caps_set_cap(caps
, usb_redir_cap_64bits_ids
);
938 usbredirparser_caps_set_cap(caps
, usb_redir_cap_32bits_bulk_length
);
940 if (runstate_check(RUN_STATE_INMIGRATE
)) {
941 flags
|= usbredirparser_fl_no_hello
;
943 usbredirparser_init(dev
->parser
, VERSION
, caps
, USB_REDIR_CAPS_SIZE
,
945 usbredirparser_do_write(dev
->parser
);
948 static void usbredir_reject_device(USBRedirDevice
*dev
)
950 usbredir_device_disconnect(dev
);
951 if (usbredirparser_peer_has_cap(dev
->parser
, usb_redir_cap_filter
)) {
952 usbredirparser_send_filter_reject(dev
->parser
);
953 usbredirparser_do_write(dev
->parser
);
957 static void usbredir_do_attach(void *opaque
)
959 USBRedirDevice
*dev
= opaque
;
961 /* In order to work properly with XHCI controllers we need these caps */
962 if ((dev
->dev
.port
->speedmask
& USB_SPEED_MASK_SUPER
) && !(
963 usbredirparser_peer_has_cap(dev
->parser
,
964 usb_redir_cap_ep_info_max_packet_size
) &&
965 usbredirparser_peer_has_cap(dev
->parser
,
966 usb_redir_cap_64bits_ids
))) {
967 ERROR("usb-redir-host lacks capabilities needed for use with XHCI\n");
968 usbredir_reject_device(dev
);
972 if (usb_device_attach(&dev
->dev
) != 0) {
973 WARNING("rejecting device due to speed mismatch\n");
974 usbredir_reject_device(dev
);
982 static int usbredir_chardev_can_read(void *opaque
)
984 USBRedirDevice
*dev
= opaque
;
987 WARNING("chardev_can_read called on non open chardev!\n");
991 /* Don't read new data from the chardev until our state is fully synced */
992 if (!runstate_check(RUN_STATE_RUNNING
)) {
996 /* usbredir_parser_do_read will consume *all* data we give it */
1000 static void usbredir_chardev_read(void *opaque
, const uint8_t *buf
, int size
)
1002 USBRedirDevice
*dev
= opaque
;
1004 /* No recursion allowed! */
1005 assert(dev
->read_buf
== NULL
);
1007 dev
->read_buf
= buf
;
1008 dev
->read_buf_size
= size
;
1010 usbredirparser_do_read(dev
->parser
);
1011 /* Send any acks, etc. which may be queued now */
1012 usbredirparser_do_write(dev
->parser
);
1015 static void usbredir_chardev_event(void *opaque
, int event
)
1017 USBRedirDevice
*dev
= opaque
;
1020 case CHR_EVENT_OPENED
:
1021 DPRINTF("chardev open\n");
1022 /* Make sure any pending closes are handled (no-op if none pending) */
1023 usbredir_chardev_close_bh(dev
);
1024 qemu_bh_cancel(dev
->chardev_close_bh
);
1025 usbredir_create_parser(dev
);
1027 case CHR_EVENT_CLOSED
:
1028 DPRINTF("chardev close\n");
1029 qemu_bh_schedule(dev
->chardev_close_bh
);
1038 static void usbredir_vm_state_change(void *priv
, int running
, RunState state
)
1040 USBRedirDevice
*dev
= priv
;
1042 if (state
== RUN_STATE_RUNNING
&& dev
->parser
!= NULL
) {
1043 usbredirparser_do_write(dev
->parser
); /* Flush any pending writes */
1047 static int usbredir_initfn(USBDevice
*udev
)
1049 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
1052 if (dev
->cs
== NULL
) {
1053 qerror_report(QERR_MISSING_PARAMETER
, "chardev");
1057 if (dev
->filter_str
) {
1058 i
= usbredirfilter_string_to_rules(dev
->filter_str
, ":", "|",
1060 &dev
->filter_rules_count
);
1062 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "filter",
1063 "a usb device filter string");
1068 dev
->chardev_close_bh
= qemu_bh_new(usbredir_chardev_close_bh
, dev
);
1069 dev
->attach_timer
= qemu_new_timer_ms(vm_clock
, usbredir_do_attach
, dev
);
1071 packet_id_queue_init(&dev
->cancelled
, dev
, "cancelled");
1072 packet_id_queue_init(&dev
->already_in_flight
, dev
, "already-in-flight");
1073 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1074 QTAILQ_INIT(&dev
->endpoint
[i
].bufpq
);
1077 /* We'll do the attach once we receive the speed from the usb-host */
1078 udev
->auto_attach
= 0;
1080 /* Will be cleared during setup when we find conflicts */
1081 dev
->compatible_speedmask
= USB_SPEED_MASK_FULL
| USB_SPEED_MASK_HIGH
;
1083 /* Let the backend know we are ready */
1084 qemu_chr_fe_open(dev
->cs
);
1085 qemu_chr_add_handlers(dev
->cs
, usbredir_chardev_can_read
,
1086 usbredir_chardev_read
, usbredir_chardev_event
, dev
);
1088 qemu_add_vm_change_state_handler(usbredir_vm_state_change
, dev
);
1089 add_boot_device_path(dev
->bootindex
, &udev
->qdev
, NULL
);
1093 static void usbredir_cleanup_device_queues(USBRedirDevice
*dev
)
1097 packet_id_queue_empty(&dev
->cancelled
);
1098 packet_id_queue_empty(&dev
->already_in_flight
);
1099 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1100 usbredir_free_bufpq(dev
, I2EP(i
));
1104 static void usbredir_handle_destroy(USBDevice
*udev
)
1106 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
1108 qemu_chr_fe_close(dev
->cs
);
1109 qemu_chr_delete(dev
->cs
);
1110 /* Note must be done after qemu_chr_close, as that causes a close event */
1111 qemu_bh_delete(dev
->chardev_close_bh
);
1113 qemu_del_timer(dev
->attach_timer
);
1114 qemu_free_timer(dev
->attach_timer
);
1116 usbredir_cleanup_device_queues(dev
);
1119 usbredirparser_destroy(dev
->parser
);
1122 free(dev
->filter_rules
);
1125 static int usbredir_check_filter(USBRedirDevice
*dev
)
1127 if (dev
->interface_info
.interface_count
== NO_INTERFACE_INFO
) {
1128 ERROR("No interface info for device\n");
1132 if (dev
->filter_rules
) {
1133 if (!usbredirparser_peer_has_cap(dev
->parser
,
1134 usb_redir_cap_connect_device_version
)) {
1135 ERROR("Device filter specified and peer does not have the "
1136 "connect_device_version capability\n");
1140 if (usbredirfilter_check(
1142 dev
->filter_rules_count
,
1143 dev
->device_info
.device_class
,
1144 dev
->device_info
.device_subclass
,
1145 dev
->device_info
.device_protocol
,
1146 dev
->interface_info
.interface_class
,
1147 dev
->interface_info
.interface_subclass
,
1148 dev
->interface_info
.interface_protocol
,
1149 dev
->interface_info
.interface_count
,
1150 dev
->device_info
.vendor_id
,
1151 dev
->device_info
.product_id
,
1152 dev
->device_info
.device_version_bcd
,
1161 usbredir_reject_device(dev
);
1166 * usbredirparser packet complete callbacks
1169 static void usbredir_handle_status(USBRedirDevice
*dev
, USBPacket
*p
,
1173 case usb_redir_success
:
1174 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
1176 case usb_redir_stall
:
1177 p
->status
= USB_RET_STALL
;
1179 case usb_redir_cancelled
:
1181 * When the usbredir-host unredirects a device, it will report a status
1182 * of cancelled for all pending packets, followed by a disconnect msg.
1184 p
->status
= USB_RET_IOERROR
;
1186 case usb_redir_inval
:
1187 WARNING("got invalid param error from usb-host?\n");
1188 p
->status
= USB_RET_IOERROR
;
1190 case usb_redir_babble
:
1191 p
->status
= USB_RET_BABBLE
;
1193 case usb_redir_ioerror
:
1194 case usb_redir_timeout
:
1196 p
->status
= USB_RET_IOERROR
;
1200 static void usbredir_hello(void *priv
, struct usb_redir_hello_header
*h
)
1202 USBRedirDevice
*dev
= priv
;
1204 /* Try to send the filter info now that we've the usb-host's caps */
1205 if (usbredirparser_peer_has_cap(dev
->parser
, usb_redir_cap_filter
) &&
1206 dev
->filter_rules
) {
1207 usbredirparser_send_filter_filter(dev
->parser
, dev
->filter_rules
,
1208 dev
->filter_rules_count
);
1209 usbredirparser_do_write(dev
->parser
);
1213 static void usbredir_device_connect(void *priv
,
1214 struct usb_redir_device_connect_header
*device_connect
)
1216 USBRedirDevice
*dev
= priv
;
1219 if (qemu_timer_pending(dev
->attach_timer
) || dev
->dev
.attached
) {
1220 ERROR("Received device connect while already connected\n");
1224 switch (device_connect
->speed
) {
1225 case usb_redir_speed_low
:
1226 speed
= "low speed";
1227 dev
->dev
.speed
= USB_SPEED_LOW
;
1228 dev
->compatible_speedmask
&= ~USB_SPEED_MASK_FULL
;
1229 dev
->compatible_speedmask
&= ~USB_SPEED_MASK_HIGH
;
1231 case usb_redir_speed_full
:
1232 speed
= "full speed";
1233 dev
->dev
.speed
= USB_SPEED_FULL
;
1234 dev
->compatible_speedmask
&= ~USB_SPEED_MASK_HIGH
;
1236 case usb_redir_speed_high
:
1237 speed
= "high speed";
1238 dev
->dev
.speed
= USB_SPEED_HIGH
;
1240 case usb_redir_speed_super
:
1241 speed
= "super speed";
1242 dev
->dev
.speed
= USB_SPEED_SUPER
;
1245 speed
= "unknown speed";
1246 dev
->dev
.speed
= USB_SPEED_FULL
;
1249 if (usbredirparser_peer_has_cap(dev
->parser
,
1250 usb_redir_cap_connect_device_version
)) {
1251 INFO("attaching %s device %04x:%04x version %d.%d class %02x\n",
1252 speed
, device_connect
->vendor_id
, device_connect
->product_id
,
1253 ((device_connect
->device_version_bcd
& 0xf000) >> 12) * 10 +
1254 ((device_connect
->device_version_bcd
& 0x0f00) >> 8),
1255 ((device_connect
->device_version_bcd
& 0x00f0) >> 4) * 10 +
1256 ((device_connect
->device_version_bcd
& 0x000f) >> 0),
1257 device_connect
->device_class
);
1259 INFO("attaching %s device %04x:%04x class %02x\n", speed
,
1260 device_connect
->vendor_id
, device_connect
->product_id
,
1261 device_connect
->device_class
);
1264 dev
->dev
.speedmask
= (1 << dev
->dev
.speed
) | dev
->compatible_speedmask
;
1265 dev
->device_info
= *device_connect
;
1267 if (usbredir_check_filter(dev
)) {
1268 WARNING("Device %04x:%04x rejected by device filter, not attaching\n",
1269 device_connect
->vendor_id
, device_connect
->product_id
);
1273 qemu_mod_timer(dev
->attach_timer
, dev
->next_attach_time
);
1276 static void usbredir_device_disconnect(void *priv
)
1278 USBRedirDevice
*dev
= priv
;
1281 /* Stop any pending attaches */
1282 qemu_del_timer(dev
->attach_timer
);
1284 if (dev
->dev
.attached
) {
1285 DPRINTF("detaching device\n");
1286 usb_device_detach(&dev
->dev
);
1288 * Delay next usb device attach to give the guest a chance to see
1289 * see the detach / attach in case of quick close / open succession
1291 dev
->next_attach_time
= qemu_get_clock_ms(vm_clock
) + 200;
1294 /* Reset state so that the next dev connected starts with a clean slate */
1295 usbredir_cleanup_device_queues(dev
);
1296 memset(dev
->endpoint
, 0, sizeof(dev
->endpoint
));
1297 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1298 QTAILQ_INIT(&dev
->endpoint
[i
].bufpq
);
1300 usb_ep_init(&dev
->dev
);
1301 dev
->interface_info
.interface_count
= NO_INTERFACE_INFO
;
1304 dev
->compatible_speedmask
= USB_SPEED_MASK_FULL
| USB_SPEED_MASK_HIGH
;
1307 static void usbredir_interface_info(void *priv
,
1308 struct usb_redir_interface_info_header
*interface_info
)
1310 USBRedirDevice
*dev
= priv
;
1312 dev
->interface_info
= *interface_info
;
1315 * If we receive interface info after the device has already been
1316 * connected (ie on a set_config), re-check the filter.
1318 if (qemu_timer_pending(dev
->attach_timer
) || dev
->dev
.attached
) {
1319 if (usbredir_check_filter(dev
)) {
1320 ERROR("Device no longer matches filter after interface info "
1321 "change, disconnecting!\n");
1326 static void usbredir_mark_speed_incompatible(USBRedirDevice
*dev
, int speed
)
1328 dev
->compatible_speedmask
&= ~(1 << speed
);
1329 dev
->dev
.speedmask
= (1 << dev
->dev
.speed
) | dev
->compatible_speedmask
;
1332 static void usbredir_set_pipeline(USBRedirDevice
*dev
, struct USBEndpoint
*uep
)
1334 if (uep
->type
!= USB_ENDPOINT_XFER_BULK
) {
1337 if (uep
->pid
== USB_TOKEN_OUT
) {
1338 uep
->pipeline
= true;
1340 if (uep
->pid
== USB_TOKEN_IN
&& uep
->max_packet_size
!= 0 &&
1341 usbredirparser_peer_has_cap(dev
->parser
,
1342 usb_redir_cap_32bits_bulk_length
)) {
1343 uep
->pipeline
= true;
1347 static void usbredir_setup_usb_eps(USBRedirDevice
*dev
)
1349 struct USBEndpoint
*usb_ep
;
1352 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1353 pid
= (i
& 0x10) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1354 usb_ep
= usb_ep_get(&dev
->dev
, pid
, i
& 0x0f);
1355 usb_ep
->type
= dev
->endpoint
[i
].type
;
1356 usb_ep
->ifnum
= dev
->endpoint
[i
].interface
;
1357 usb_ep
->max_packet_size
= dev
->endpoint
[i
].max_packet_size
;
1358 usbredir_set_pipeline(dev
, usb_ep
);
1362 static void usbredir_ep_info(void *priv
,
1363 struct usb_redir_ep_info_header
*ep_info
)
1365 USBRedirDevice
*dev
= priv
;
1368 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1369 dev
->endpoint
[i
].type
= ep_info
->type
[i
];
1370 dev
->endpoint
[i
].interval
= ep_info
->interval
[i
];
1371 dev
->endpoint
[i
].interface
= ep_info
->interface
[i
];
1372 if (usbredirparser_peer_has_cap(dev
->parser
,
1373 usb_redir_cap_ep_info_max_packet_size
)) {
1374 dev
->endpoint
[i
].max_packet_size
= ep_info
->max_packet_size
[i
];
1376 switch (dev
->endpoint
[i
].type
) {
1377 case usb_redir_type_invalid
:
1379 case usb_redir_type_iso
:
1380 usbredir_mark_speed_incompatible(dev
, USB_SPEED_FULL
);
1381 usbredir_mark_speed_incompatible(dev
, USB_SPEED_HIGH
);
1383 case usb_redir_type_interrupt
:
1384 if (!usbredirparser_peer_has_cap(dev
->parser
,
1385 usb_redir_cap_ep_info_max_packet_size
) ||
1386 ep_info
->max_packet_size
[i
] > 64) {
1387 usbredir_mark_speed_incompatible(dev
, USB_SPEED_FULL
);
1389 if (!usbredirparser_peer_has_cap(dev
->parser
,
1390 usb_redir_cap_ep_info_max_packet_size
) ||
1391 ep_info
->max_packet_size
[i
] > 1024) {
1392 usbredir_mark_speed_incompatible(dev
, USB_SPEED_HIGH
);
1394 if (dev
->endpoint
[i
].interval
== 0) {
1395 ERROR("Received 0 interval for isoc or irq endpoint\n");
1396 usbredir_reject_device(dev
);
1400 case usb_redir_type_control
:
1401 case usb_redir_type_bulk
:
1402 DPRINTF("ep: %02X type: %d interface: %d\n", I2EP(i
),
1403 dev
->endpoint
[i
].type
, dev
->endpoint
[i
].interface
);
1406 ERROR("Received invalid endpoint type\n");
1407 usbredir_reject_device(dev
);
1411 /* The new ep info may have caused a speed incompatibility, recheck */
1412 if (dev
->dev
.attached
&&
1413 !(dev
->dev
.port
->speedmask
& dev
->dev
.speedmask
)) {
1414 ERROR("Device no longer matches speed after endpoint info change, "
1415 "disconnecting!\n");
1416 usbredir_reject_device(dev
);
1419 usbredir_setup_usb_eps(dev
);
1422 static void usbredir_configuration_status(void *priv
, uint64_t id
,
1423 struct usb_redir_configuration_status_header
*config_status
)
1425 USBRedirDevice
*dev
= priv
;
1428 DPRINTF("set config status %d config %d id %"PRIu64
"\n",
1429 config_status
->status
, config_status
->configuration
, id
);
1431 p
= usbredir_find_packet_by_id(dev
, 0, id
);
1433 if (dev
->dev
.setup_buf
[0] & USB_DIR_IN
) {
1434 dev
->dev
.data_buf
[0] = config_status
->configuration
;
1435 p
->actual_length
= 1;
1437 usbredir_handle_status(dev
, p
, config_status
->status
);
1438 usb_generic_async_ctrl_complete(&dev
->dev
, p
);
1442 static void usbredir_alt_setting_status(void *priv
, uint64_t id
,
1443 struct usb_redir_alt_setting_status_header
*alt_setting_status
)
1445 USBRedirDevice
*dev
= priv
;
1448 DPRINTF("alt status %d intf %d alt %d id: %"PRIu64
"\n",
1449 alt_setting_status
->status
, alt_setting_status
->interface
,
1450 alt_setting_status
->alt
, id
);
1452 p
= usbredir_find_packet_by_id(dev
, 0, id
);
1454 if (dev
->dev
.setup_buf
[0] & USB_DIR_IN
) {
1455 dev
->dev
.data_buf
[0] = alt_setting_status
->alt
;
1456 p
->actual_length
= 1;
1458 usbredir_handle_status(dev
, p
, alt_setting_status
->status
);
1459 usb_generic_async_ctrl_complete(&dev
->dev
, p
);
1463 static void usbredir_iso_stream_status(void *priv
, uint64_t id
,
1464 struct usb_redir_iso_stream_status_header
*iso_stream_status
)
1466 USBRedirDevice
*dev
= priv
;
1467 uint8_t ep
= iso_stream_status
->endpoint
;
1469 DPRINTF("iso status %d ep %02X id %"PRIu64
"\n", iso_stream_status
->status
,
1472 if (!dev
->dev
.attached
|| !dev
->endpoint
[EP2I(ep
)].iso_started
) {
1476 dev
->endpoint
[EP2I(ep
)].iso_error
= iso_stream_status
->status
;
1477 if (iso_stream_status
->status
== usb_redir_stall
) {
1478 DPRINTF("iso stream stopped by peer ep %02X\n", ep
);
1479 dev
->endpoint
[EP2I(ep
)].iso_started
= 0;
1483 static void usbredir_interrupt_receiving_status(void *priv
, uint64_t id
,
1484 struct usb_redir_interrupt_receiving_status_header
1485 *interrupt_receiving_status
)
1487 USBRedirDevice
*dev
= priv
;
1488 uint8_t ep
= interrupt_receiving_status
->endpoint
;
1490 DPRINTF("interrupt recv status %d ep %02X id %"PRIu64
"\n",
1491 interrupt_receiving_status
->status
, ep
, id
);
1493 if (!dev
->dev
.attached
|| !dev
->endpoint
[EP2I(ep
)].interrupt_started
) {
1497 dev
->endpoint
[EP2I(ep
)].interrupt_error
=
1498 interrupt_receiving_status
->status
;
1499 if (interrupt_receiving_status
->status
== usb_redir_stall
) {
1500 DPRINTF("interrupt receiving stopped by peer ep %02X\n", ep
);
1501 dev
->endpoint
[EP2I(ep
)].interrupt_started
= 0;
1505 static void usbredir_bulk_streams_status(void *priv
, uint64_t id
,
1506 struct usb_redir_bulk_streams_status_header
*bulk_streams_status
)
1510 static void usbredir_control_packet(void *priv
, uint64_t id
,
1511 struct usb_redir_control_packet_header
*control_packet
,
1512 uint8_t *data
, int data_len
)
1514 USBRedirDevice
*dev
= priv
;
1516 int len
= control_packet
->length
;
1518 DPRINTF("ctrl-in status %d len %d id %"PRIu64
"\n", control_packet
->status
,
1521 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
1522 * to work redirected to a not superspeed capable hcd */
1523 if (dev
->dev
.speed
== USB_SPEED_SUPER
&&
1524 !((dev
->dev
.port
->speedmask
& USB_SPEED_MASK_SUPER
)) &&
1525 control_packet
->requesttype
== 0x80 &&
1526 control_packet
->request
== 6 &&
1527 control_packet
->value
== 0x100 && control_packet
->index
== 0 &&
1528 data_len
>= 18 && data
[7] == 9) {
1532 p
= usbredir_find_packet_by_id(dev
, 0, id
);
1534 usbredir_handle_status(dev
, p
, control_packet
->status
);
1536 usbredir_log_data(dev
, "ctrl data in:", data
, data_len
);
1537 if (data_len
> sizeof(dev
->dev
.data_buf
)) {
1538 ERROR("ctrl buffer too small (%d > %zu)\n",
1539 data_len
, sizeof(dev
->dev
.data_buf
));
1540 p
->status
= USB_RET_STALL
;
1541 data_len
= len
= sizeof(dev
->dev
.data_buf
);
1543 memcpy(dev
->dev
.data_buf
, data
, data_len
);
1545 p
->actual_length
= len
;
1546 usb_generic_async_ctrl_complete(&dev
->dev
, p
);
1551 static void usbredir_bulk_packet(void *priv
, uint64_t id
,
1552 struct usb_redir_bulk_packet_header
*bulk_packet
,
1553 uint8_t *data
, int data_len
)
1555 USBRedirDevice
*dev
= priv
;
1556 uint8_t ep
= bulk_packet
->endpoint
;
1557 int len
= (bulk_packet
->length_high
<< 16) | bulk_packet
->length
;
1560 DPRINTF("bulk-in status %d ep %02X len %d id %"PRIu64
"\n",
1561 bulk_packet
->status
, ep
, len
, id
);
1563 p
= usbredir_find_packet_by_id(dev
, ep
, id
);
1565 size_t size
= (p
->combined
) ? p
->combined
->iov
.size
: p
->iov
.size
;
1566 usbredir_handle_status(dev
, p
, bulk_packet
->status
);
1568 usbredir_log_data(dev
, "bulk data in:", data
, data_len
);
1569 if (data_len
> size
) {
1570 ERROR("bulk got more data then requested (%d > %zd)\n",
1571 data_len
, p
->iov
.size
);
1572 p
->status
= USB_RET_BABBLE
;
1573 data_len
= len
= size
;
1576 iov_from_buf(p
->combined
->iov
.iov
, p
->combined
->iov
.niov
,
1579 usb_packet_copy(p
, data
, data_len
);
1582 p
->actual_length
= len
;
1583 if (p
->pid
== USB_TOKEN_IN
&& p
->ep
->pipeline
) {
1584 usb_combined_input_packet_complete(&dev
->dev
, p
);
1586 usb_packet_complete(&dev
->dev
, p
);
1592 static void usbredir_iso_packet(void *priv
, uint64_t id
,
1593 struct usb_redir_iso_packet_header
*iso_packet
,
1594 uint8_t *data
, int data_len
)
1596 USBRedirDevice
*dev
= priv
;
1597 uint8_t ep
= iso_packet
->endpoint
;
1599 DPRINTF2("iso-in status %d ep %02X len %d id %"PRIu64
"\n",
1600 iso_packet
->status
, ep
, data_len
, id
);
1602 if (dev
->endpoint
[EP2I(ep
)].type
!= USB_ENDPOINT_XFER_ISOC
) {
1603 ERROR("received iso packet for non iso endpoint %02X\n", ep
);
1608 if (dev
->endpoint
[EP2I(ep
)].iso_started
== 0) {
1609 DPRINTF("received iso packet for non started stream ep %02X\n", ep
);
1614 /* bufp_alloc also adds the packet to the ep queue */
1615 bufp_alloc(dev
, data
, data_len
, iso_packet
->status
, ep
);
1618 static void usbredir_interrupt_packet(void *priv
, uint64_t id
,
1619 struct usb_redir_interrupt_packet_header
*interrupt_packet
,
1620 uint8_t *data
, int data_len
)
1622 USBRedirDevice
*dev
= priv
;
1623 uint8_t ep
= interrupt_packet
->endpoint
;
1625 DPRINTF("interrupt-in status %d ep %02X len %d id %"PRIu64
"\n",
1626 interrupt_packet
->status
, ep
, data_len
, id
);
1628 if (dev
->endpoint
[EP2I(ep
)].type
!= USB_ENDPOINT_XFER_INT
) {
1629 ERROR("received int packet for non interrupt endpoint %02X\n", ep
);
1634 if (ep
& USB_DIR_IN
) {
1635 if (dev
->endpoint
[EP2I(ep
)].interrupt_started
== 0) {
1636 DPRINTF("received int packet while not started ep %02X\n", ep
);
1641 /* bufp_alloc also adds the packet to the ep queue */
1642 bufp_alloc(dev
, data
, data_len
, interrupt_packet
->status
, ep
);
1644 USBPacket
*p
= usbredir_find_packet_by_id(dev
, ep
, id
);
1646 usbredir_handle_status(dev
, p
, interrupt_packet
->status
);
1647 p
->actual_length
= interrupt_packet
->length
;
1648 usb_packet_complete(&dev
->dev
, p
);
1657 static void usbredir_pre_save(void *priv
)
1659 USBRedirDevice
*dev
= priv
;
1661 usbredir_fill_already_in_flight(dev
);
1664 static int usbredir_post_load(void *priv
, int version_id
)
1666 USBRedirDevice
*dev
= priv
;
1668 switch (dev
->device_info
.speed
) {
1669 case usb_redir_speed_low
:
1670 dev
->dev
.speed
= USB_SPEED_LOW
;
1672 case usb_redir_speed_full
:
1673 dev
->dev
.speed
= USB_SPEED_FULL
;
1675 case usb_redir_speed_high
:
1676 dev
->dev
.speed
= USB_SPEED_HIGH
;
1678 case usb_redir_speed_super
:
1679 dev
->dev
.speed
= USB_SPEED_SUPER
;
1682 dev
->dev
.speed
= USB_SPEED_FULL
;
1684 dev
->dev
.speedmask
= (1 << dev
->dev
.speed
);
1686 usbredir_setup_usb_eps(dev
);
1691 /* For usbredirparser migration */
1692 static void usbredir_put_parser(QEMUFile
*f
, void *priv
, size_t unused
)
1694 USBRedirDevice
*dev
= priv
;
1698 if (dev
->parser
== NULL
) {
1699 qemu_put_be32(f
, 0);
1703 usbredirparser_serialize(dev
->parser
, &data
, &len
);
1704 qemu_oom_check(data
);
1706 qemu_put_be32(f
, len
);
1707 qemu_put_buffer(f
, data
, len
);
1712 static int usbredir_get_parser(QEMUFile
*f
, void *priv
, size_t unused
)
1714 USBRedirDevice
*dev
= priv
;
1718 len
= qemu_get_be32(f
);
1724 * If our chardev is not open already at this point the usbredir connection
1725 * has been broken (non seamless migration, or restore from disk).
1727 * In this case create a temporary parser to receive the migration data,
1728 * and schedule the close_bh to report the device as disconnected to the
1729 * guest and to destroy the parser again.
1731 if (dev
->parser
== NULL
) {
1732 WARNING("usb-redir connection broken during migration\n");
1733 usbredir_create_parser(dev
);
1734 qemu_bh_schedule(dev
->chardev_close_bh
);
1737 data
= g_malloc(len
);
1738 qemu_get_buffer(f
, data
, len
);
1740 ret
= usbredirparser_unserialize(dev
->parser
, data
, len
);
1747 static const VMStateInfo usbredir_parser_vmstate_info
= {
1748 .name
= "usb-redir-parser",
1749 .put
= usbredir_put_parser
,
1750 .get
= usbredir_get_parser
,
1754 /* For buffered packets (iso/irq) queue migration */
1755 static void usbredir_put_bufpq(QEMUFile
*f
, void *priv
, size_t unused
)
1757 struct endp_data
*endp
= priv
;
1758 struct buf_packet
*bufp
;
1759 int remain
= endp
->bufpq_size
;
1761 qemu_put_be32(f
, endp
->bufpq_size
);
1762 QTAILQ_FOREACH(bufp
, &endp
->bufpq
, next
) {
1763 qemu_put_be32(f
, bufp
->len
);
1764 qemu_put_be32(f
, bufp
->status
);
1765 qemu_put_buffer(f
, bufp
->data
, bufp
->len
);
1768 assert(remain
== 0);
1771 static int usbredir_get_bufpq(QEMUFile
*f
, void *priv
, size_t unused
)
1773 struct endp_data
*endp
= priv
;
1774 struct buf_packet
*bufp
;
1777 endp
->bufpq_size
= qemu_get_be32(f
);
1778 for (i
= 0; i
< endp
->bufpq_size
; i
++) {
1779 bufp
= g_malloc(sizeof(struct buf_packet
));
1780 bufp
->len
= qemu_get_be32(f
);
1781 bufp
->status
= qemu_get_be32(f
);
1782 bufp
->data
= qemu_oom_check(malloc(bufp
->len
)); /* regular malloc! */
1783 qemu_get_buffer(f
, bufp
->data
, bufp
->len
);
1784 QTAILQ_INSERT_TAIL(&endp
->bufpq
, bufp
, next
);
1789 static const VMStateInfo usbredir_ep_bufpq_vmstate_info
= {
1790 .name
= "usb-redir-bufpq",
1791 .put
= usbredir_put_bufpq
,
1792 .get
= usbredir_get_bufpq
,
1796 /* For endp_data migration */
1797 static const VMStateDescription usbredir_ep_vmstate
= {
1798 .name
= "usb-redir-ep",
1800 .minimum_version_id
= 1,
1801 .fields
= (VMStateField
[]) {
1802 VMSTATE_UINT8(type
, struct endp_data
),
1803 VMSTATE_UINT8(interval
, struct endp_data
),
1804 VMSTATE_UINT8(interface
, struct endp_data
),
1805 VMSTATE_UINT16(max_packet_size
, struct endp_data
),
1806 VMSTATE_UINT8(iso_started
, struct endp_data
),
1807 VMSTATE_UINT8(iso_error
, struct endp_data
),
1808 VMSTATE_UINT8(interrupt_started
, struct endp_data
),
1809 VMSTATE_UINT8(interrupt_error
, struct endp_data
),
1810 VMSTATE_UINT8(bufpq_prefilled
, struct endp_data
),
1811 VMSTATE_UINT8(bufpq_dropping_packets
, struct endp_data
),
1815 .field_exists
= NULL
,
1817 .info
= &usbredir_ep_bufpq_vmstate_info
,
1818 .flags
= VMS_SINGLE
,
1821 VMSTATE_INT32(bufpq_target_size
, struct endp_data
),
1822 VMSTATE_END_OF_LIST()
1827 /* For PacketIdQueue migration */
1828 static void usbredir_put_packet_id_q(QEMUFile
*f
, void *priv
, size_t unused
)
1830 struct PacketIdQueue
*q
= priv
;
1831 USBRedirDevice
*dev
= q
->dev
;
1832 struct PacketIdQueueEntry
*e
;
1833 int remain
= q
->size
;
1835 DPRINTF("put_packet_id_q %s size %d\n", q
->name
, q
->size
);
1836 qemu_put_be32(f
, q
->size
);
1837 QTAILQ_FOREACH(e
, &q
->head
, next
) {
1838 qemu_put_be64(f
, e
->id
);
1841 assert(remain
== 0);
1844 static int usbredir_get_packet_id_q(QEMUFile
*f
, void *priv
, size_t unused
)
1846 struct PacketIdQueue
*q
= priv
;
1847 USBRedirDevice
*dev
= q
->dev
;
1851 size
= qemu_get_be32(f
);
1852 DPRINTF("get_packet_id_q %s size %d\n", q
->name
, size
);
1853 for (i
= 0; i
< size
; i
++) {
1854 id
= qemu_get_be64(f
);
1855 packet_id_queue_add(q
, id
);
1857 assert(q
->size
== size
);
1861 static const VMStateInfo usbredir_ep_packet_id_q_vmstate_info
= {
1862 .name
= "usb-redir-packet-id-q",
1863 .put
= usbredir_put_packet_id_q
,
1864 .get
= usbredir_get_packet_id_q
,
1867 static const VMStateDescription usbredir_ep_packet_id_queue_vmstate
= {
1868 .name
= "usb-redir-packet-id-queue",
1870 .minimum_version_id
= 1,
1871 .fields
= (VMStateField
[]) {
1875 .field_exists
= NULL
,
1877 .info
= &usbredir_ep_packet_id_q_vmstate_info
,
1878 .flags
= VMS_SINGLE
,
1881 VMSTATE_END_OF_LIST()
1886 /* For usb_redir_device_connect_header migration */
1887 static const VMStateDescription usbredir_device_info_vmstate
= {
1888 .name
= "usb-redir-device-info",
1890 .minimum_version_id
= 1,
1891 .fields
= (VMStateField
[]) {
1892 VMSTATE_UINT8(speed
, struct usb_redir_device_connect_header
),
1893 VMSTATE_UINT8(device_class
, struct usb_redir_device_connect_header
),
1894 VMSTATE_UINT8(device_subclass
, struct usb_redir_device_connect_header
),
1895 VMSTATE_UINT8(device_protocol
, struct usb_redir_device_connect_header
),
1896 VMSTATE_UINT16(vendor_id
, struct usb_redir_device_connect_header
),
1897 VMSTATE_UINT16(product_id
, struct usb_redir_device_connect_header
),
1898 VMSTATE_UINT16(device_version_bcd
,
1899 struct usb_redir_device_connect_header
),
1900 VMSTATE_END_OF_LIST()
1905 /* For usb_redir_interface_info_header migration */
1906 static const VMStateDescription usbredir_interface_info_vmstate
= {
1907 .name
= "usb-redir-interface-info",
1909 .minimum_version_id
= 1,
1910 .fields
= (VMStateField
[]) {
1911 VMSTATE_UINT32(interface_count
,
1912 struct usb_redir_interface_info_header
),
1913 VMSTATE_UINT8_ARRAY(interface
,
1914 struct usb_redir_interface_info_header
, 32),
1915 VMSTATE_UINT8_ARRAY(interface_class
,
1916 struct usb_redir_interface_info_header
, 32),
1917 VMSTATE_UINT8_ARRAY(interface_subclass
,
1918 struct usb_redir_interface_info_header
, 32),
1919 VMSTATE_UINT8_ARRAY(interface_protocol
,
1920 struct usb_redir_interface_info_header
, 32),
1921 VMSTATE_END_OF_LIST()
1926 /* And finally the USBRedirDevice vmstate itself */
1927 static const VMStateDescription usbredir_vmstate
= {
1928 .name
= "usb-redir",
1930 .minimum_version_id
= 1,
1931 .pre_save
= usbredir_pre_save
,
1932 .post_load
= usbredir_post_load
,
1933 .fields
= (VMStateField
[]) {
1934 VMSTATE_USB_DEVICE(dev
, USBRedirDevice
),
1935 VMSTATE_TIMER(attach_timer
, USBRedirDevice
),
1939 .field_exists
= NULL
,
1941 .info
= &usbredir_parser_vmstate_info
,
1942 .flags
= VMS_SINGLE
,
1945 VMSTATE_STRUCT_ARRAY(endpoint
, USBRedirDevice
, MAX_ENDPOINTS
, 1,
1946 usbredir_ep_vmstate
, struct endp_data
),
1947 VMSTATE_STRUCT(cancelled
, USBRedirDevice
, 1,
1948 usbredir_ep_packet_id_queue_vmstate
,
1949 struct PacketIdQueue
),
1950 VMSTATE_STRUCT(already_in_flight
, USBRedirDevice
, 1,
1951 usbredir_ep_packet_id_queue_vmstate
,
1952 struct PacketIdQueue
),
1953 VMSTATE_STRUCT(device_info
, USBRedirDevice
, 1,
1954 usbredir_device_info_vmstate
,
1955 struct usb_redir_device_connect_header
),
1956 VMSTATE_STRUCT(interface_info
, USBRedirDevice
, 1,
1957 usbredir_interface_info_vmstate
,
1958 struct usb_redir_interface_info_header
),
1959 VMSTATE_END_OF_LIST()
1963 static Property usbredir_properties
[] = {
1964 DEFINE_PROP_CHR("chardev", USBRedirDevice
, cs
),
1965 DEFINE_PROP_UINT8("debug", USBRedirDevice
, debug
, 0),
1966 DEFINE_PROP_STRING("filter", USBRedirDevice
, filter_str
),
1967 DEFINE_PROP_INT32("bootindex", USBRedirDevice
, bootindex
, -1),
1968 DEFINE_PROP_END_OF_LIST(),
1971 static void usbredir_class_initfn(ObjectClass
*klass
, void *data
)
1973 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1974 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1976 uc
->init
= usbredir_initfn
;
1977 uc
->product_desc
= "USB Redirection Device";
1978 uc
->handle_destroy
= usbredir_handle_destroy
;
1979 uc
->cancel_packet
= usbredir_cancel_packet
;
1980 uc
->handle_reset
= usbredir_handle_reset
;
1981 uc
->handle_data
= usbredir_handle_data
;
1982 uc
->handle_control
= usbredir_handle_control
;
1983 uc
->flush_ep_queue
= usbredir_flush_ep_queue
;
1984 dc
->vmsd
= &usbredir_vmstate
;
1985 dc
->props
= usbredir_properties
;
1988 static TypeInfo usbredir_dev_info
= {
1989 .name
= "usb-redir",
1990 .parent
= TYPE_USB_DEVICE
,
1991 .instance_size
= sizeof(USBRedirDevice
),
1992 .class_init
= usbredir_class_initfn
,
1995 static void usbredir_register_types(void)
1997 type_register_static(&usbredir_dev_info
);
2000 type_init(usbredir_register_types
)