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
;
111 static void usbredir_hello(void *priv
, struct usb_redir_hello_header
*h
);
112 static void usbredir_device_connect(void *priv
,
113 struct usb_redir_device_connect_header
*device_connect
);
114 static void usbredir_device_disconnect(void *priv
);
115 static void usbredir_interface_info(void *priv
,
116 struct usb_redir_interface_info_header
*interface_info
);
117 static void usbredir_ep_info(void *priv
,
118 struct usb_redir_ep_info_header
*ep_info
);
119 static void usbredir_configuration_status(void *priv
, uint64_t id
,
120 struct usb_redir_configuration_status_header
*configuration_status
);
121 static void usbredir_alt_setting_status(void *priv
, uint64_t id
,
122 struct usb_redir_alt_setting_status_header
*alt_setting_status
);
123 static void usbredir_iso_stream_status(void *priv
, uint64_t id
,
124 struct usb_redir_iso_stream_status_header
*iso_stream_status
);
125 static void usbredir_interrupt_receiving_status(void *priv
, uint64_t id
,
126 struct usb_redir_interrupt_receiving_status_header
127 *interrupt_receiving_status
);
128 static void usbredir_bulk_streams_status(void *priv
, uint64_t id
,
129 struct usb_redir_bulk_streams_status_header
*bulk_streams_status
);
130 static void usbredir_control_packet(void *priv
, uint64_t id
,
131 struct usb_redir_control_packet_header
*control_packet
,
132 uint8_t *data
, int data_len
);
133 static void usbredir_bulk_packet(void *priv
, uint64_t id
,
134 struct usb_redir_bulk_packet_header
*bulk_packet
,
135 uint8_t *data
, int data_len
);
136 static void usbredir_iso_packet(void *priv
, uint64_t id
,
137 struct usb_redir_iso_packet_header
*iso_packet
,
138 uint8_t *data
, int data_len
);
139 static void usbredir_interrupt_packet(void *priv
, uint64_t id
,
140 struct usb_redir_interrupt_packet_header
*interrupt_header
,
141 uint8_t *data
, int data_len
);
143 static int usbredir_handle_status(USBRedirDevice
*dev
,
144 int status
, int actual_len
);
146 #define VERSION "qemu usb-redir guest " QEMU_VERSION
154 if (dev->debug >= usbredirparser_error) { \
155 error_report("usb-redir error: " __VA_ARGS__); \
158 #define WARNING(...) \
160 if (dev->debug >= usbredirparser_warning) { \
161 error_report("usb-redir warning: " __VA_ARGS__); \
166 if (dev->debug >= usbredirparser_info) { \
167 error_report("usb-redir: " __VA_ARGS__); \
170 #define DPRINTF(...) \
172 if (dev->debug >= usbredirparser_debug) { \
173 error_report("usb-redir: " __VA_ARGS__); \
176 #define DPRINTF2(...) \
178 if (dev->debug >= usbredirparser_debug_data) { \
179 error_report("usb-redir: " __VA_ARGS__); \
183 static void usbredir_log(void *priv
, int level
, const char *msg
)
185 USBRedirDevice
*dev
= priv
;
187 if (dev
->debug
< level
) {
191 error_report("%s", msg
);
194 static void usbredir_log_data(USBRedirDevice
*dev
, const char *desc
,
195 const uint8_t *data
, int len
)
199 if (dev
->debug
< usbredirparser_debug_data
) {
203 for (i
= 0; i
< len
; i
+= j
) {
206 n
= sprintf(buf
, "%s", desc
);
207 for (j
= 0; j
< 8 && i
+ j
< len
; j
++) {
208 n
+= sprintf(buf
+ n
, " %02X", data
[i
+ j
]);
210 error_report("%s", buf
);
215 * usbredirparser io functions
218 static int usbredir_read(void *priv
, uint8_t *data
, int count
)
220 USBRedirDevice
*dev
= priv
;
222 if (dev
->read_buf_size
< count
) {
223 count
= dev
->read_buf_size
;
226 memcpy(data
, dev
->read_buf
, count
);
228 dev
->read_buf_size
-= count
;
229 if (dev
->read_buf_size
) {
230 dev
->read_buf
+= count
;
232 dev
->read_buf
= NULL
;
238 static int usbredir_write(void *priv
, uint8_t *data
, int count
)
240 USBRedirDevice
*dev
= priv
;
242 if (!dev
->cs
->opened
) {
246 /* Don't send new data to the chardev until our state is fully synced */
247 if (!runstate_check(RUN_STATE_RUNNING
)) {
251 return qemu_chr_fe_write(dev
->cs
, data
, count
);
255 * Cancelled and buffered packets helpers
258 static void packet_id_queue_init(struct PacketIdQueue
*q
,
259 USBRedirDevice
*dev
, const char *name
)
263 QTAILQ_INIT(&q
->head
);
267 static void packet_id_queue_add(struct PacketIdQueue
*q
, uint64_t id
)
269 USBRedirDevice
*dev
= q
->dev
;
270 struct PacketIdQueueEntry
*e
;
272 DPRINTF("adding packet id %"PRIu64
" to %s queue\n", id
, q
->name
);
274 e
= g_malloc0(sizeof(struct PacketIdQueueEntry
));
276 QTAILQ_INSERT_TAIL(&q
->head
, e
, next
);
280 static int packet_id_queue_remove(struct PacketIdQueue
*q
, uint64_t id
)
282 USBRedirDevice
*dev
= q
->dev
;
283 struct PacketIdQueueEntry
*e
;
285 QTAILQ_FOREACH(e
, &q
->head
, next
) {
287 DPRINTF("removing packet id %"PRIu64
" from %s queue\n",
289 QTAILQ_REMOVE(&q
->head
, e
, next
);
298 static void packet_id_queue_empty(struct PacketIdQueue
*q
)
300 USBRedirDevice
*dev
= q
->dev
;
301 struct PacketIdQueueEntry
*e
, *next_e
;
303 DPRINTF("removing %d packet-ids from %s queue\n", q
->size
, q
->name
);
305 QTAILQ_FOREACH_SAFE(e
, &q
->head
, next
, next_e
) {
306 QTAILQ_REMOVE(&q
->head
, e
, next
);
312 static void usbredir_cancel_packet(USBDevice
*udev
, USBPacket
*p
)
314 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
317 usb_combined_packet_cancel(udev
, p
);
321 packet_id_queue_add(&dev
->cancelled
, p
->id
);
322 usbredirparser_send_cancel_data_packet(dev
->parser
, p
->id
);
323 usbredirparser_do_write(dev
->parser
);
326 static int usbredir_is_cancelled(USBRedirDevice
*dev
, uint64_t id
)
328 if (!dev
->dev
.attached
) {
329 return 1; /* Treat everything as cancelled after a disconnect */
331 return packet_id_queue_remove(&dev
->cancelled
, id
);
334 static void usbredir_fill_already_in_flight_from_ep(USBRedirDevice
*dev
,
335 struct USBEndpoint
*ep
)
339 QTAILQ_FOREACH(p
, &ep
->queue
, queue
) {
340 /* Skip combined packets, except for the first */
341 if (p
->combined
&& p
!= p
->combined
->first
) {
344 packet_id_queue_add(&dev
->already_in_flight
, p
->id
);
348 static void usbredir_fill_already_in_flight(USBRedirDevice
*dev
)
351 struct USBDevice
*udev
= &dev
->dev
;
353 usbredir_fill_already_in_flight_from_ep(dev
, &udev
->ep_ctl
);
355 for (ep
= 0; ep
< USB_MAX_ENDPOINTS
; ep
++) {
356 usbredir_fill_already_in_flight_from_ep(dev
, &udev
->ep_in
[ep
]);
357 usbredir_fill_already_in_flight_from_ep(dev
, &udev
->ep_out
[ep
]);
361 static int usbredir_already_in_flight(USBRedirDevice
*dev
, uint64_t id
)
363 return packet_id_queue_remove(&dev
->already_in_flight
, id
);
366 static USBPacket
*usbredir_find_packet_by_id(USBRedirDevice
*dev
,
367 uint8_t ep
, uint64_t id
)
371 if (usbredir_is_cancelled(dev
, id
)) {
375 p
= usb_ep_find_packet_by_id(&dev
->dev
,
376 (ep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
,
379 ERROR("could not find packet with id %"PRIu64
"\n", id
);
384 static void bufp_alloc(USBRedirDevice
*dev
,
385 uint8_t *data
, int len
, int status
, uint8_t ep
)
387 struct buf_packet
*bufp
;
389 if (!dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
&&
390 dev
->endpoint
[EP2I(ep
)].bufpq_size
>
391 2 * dev
->endpoint
[EP2I(ep
)].bufpq_target_size
) {
392 DPRINTF("bufpq overflow, dropping packets ep %02X\n", ep
);
393 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 1;
395 /* Since we're interupting the stream anyways, drop enough packets to get
396 back to our target buffer size */
397 if (dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
) {
398 if (dev
->endpoint
[EP2I(ep
)].bufpq_size
>
399 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
) {
403 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 0;
406 bufp
= g_malloc(sizeof(struct buf_packet
));
409 bufp
->status
= status
;
410 QTAILQ_INSERT_TAIL(&dev
->endpoint
[EP2I(ep
)].bufpq
, bufp
, next
);
411 dev
->endpoint
[EP2I(ep
)].bufpq_size
++;
414 static void bufp_free(USBRedirDevice
*dev
, struct buf_packet
*bufp
,
417 QTAILQ_REMOVE(&dev
->endpoint
[EP2I(ep
)].bufpq
, bufp
, next
);
418 dev
->endpoint
[EP2I(ep
)].bufpq_size
--;
423 static void usbredir_free_bufpq(USBRedirDevice
*dev
, uint8_t ep
)
425 struct buf_packet
*buf
, *buf_next
;
427 QTAILQ_FOREACH_SAFE(buf
, &dev
->endpoint
[EP2I(ep
)].bufpq
, next
, buf_next
) {
428 bufp_free(dev
, buf
, ep
);
433 * USBDevice callbacks
436 static void usbredir_handle_reset(USBDevice
*udev
)
438 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
440 DPRINTF("reset device\n");
441 usbredirparser_send_reset(dev
->parser
);
442 usbredirparser_do_write(dev
->parser
);
445 static int usbredir_handle_iso_data(USBRedirDevice
*dev
, USBPacket
*p
,
449 if (!dev
->endpoint
[EP2I(ep
)].iso_started
&&
450 !dev
->endpoint
[EP2I(ep
)].iso_error
) {
451 struct usb_redir_start_iso_stream_header start_iso
= {
456 if (dev
->dev
.speed
== USB_SPEED_HIGH
) {
457 pkts_per_sec
= 8000 / dev
->endpoint
[EP2I(ep
)].interval
;
459 pkts_per_sec
= 1000 / dev
->endpoint
[EP2I(ep
)].interval
;
461 /* Testing has shown that we need circa 60 ms buffer */
462 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
= (pkts_per_sec
* 60) / 1000;
464 /* Aim for approx 100 interrupts / second on the client to
465 balance latency and interrupt load */
466 start_iso
.pkts_per_urb
= pkts_per_sec
/ 100;
467 if (start_iso
.pkts_per_urb
< 1) {
468 start_iso
.pkts_per_urb
= 1;
469 } else if (start_iso
.pkts_per_urb
> 32) {
470 start_iso
.pkts_per_urb
= 32;
473 start_iso
.no_urbs
= (dev
->endpoint
[EP2I(ep
)].bufpq_target_size
+
474 start_iso
.pkts_per_urb
- 1) /
475 start_iso
.pkts_per_urb
;
476 /* Output endpoints pre-fill only 1/2 of the packets, keeping the rest
477 as overflow buffer. Also see the usbredir protocol documentation */
478 if (!(ep
& USB_DIR_IN
)) {
479 start_iso
.no_urbs
*= 2;
481 if (start_iso
.no_urbs
> 16) {
482 start_iso
.no_urbs
= 16;
485 /* No id, we look at the ep when receiving a status back */
486 usbredirparser_send_start_iso_stream(dev
->parser
, 0, &start_iso
);
487 usbredirparser_do_write(dev
->parser
);
488 DPRINTF("iso stream started pkts/sec %d pkts/urb %d urbs %d ep %02X\n",
489 pkts_per_sec
, start_iso
.pkts_per_urb
, start_iso
.no_urbs
, ep
);
490 dev
->endpoint
[EP2I(ep
)].iso_started
= 1;
491 dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
= 0;
492 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 0;
495 if (ep
& USB_DIR_IN
) {
496 struct buf_packet
*isop
;
498 if (dev
->endpoint
[EP2I(ep
)].iso_started
&&
499 !dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
) {
500 if (dev
->endpoint
[EP2I(ep
)].bufpq_size
<
501 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
) {
502 return usbredir_handle_status(dev
, 0, 0);
504 dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
= 1;
507 isop
= QTAILQ_FIRST(&dev
->endpoint
[EP2I(ep
)].bufpq
);
509 DPRINTF("iso-token-in ep %02X, no isop, iso_error: %d\n",
510 ep
, dev
->endpoint
[EP2I(ep
)].iso_error
);
511 /* Re-fill the buffer */
512 dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
= 0;
513 /* Check iso_error for stream errors, otherwise its an underrun */
514 status
= dev
->endpoint
[EP2I(ep
)].iso_error
;
515 dev
->endpoint
[EP2I(ep
)].iso_error
= 0;
516 return status
? USB_RET_IOERROR
: 0;
518 DPRINTF2("iso-token-in ep %02X status %d len %d queue-size: %d\n", ep
,
519 isop
->status
, isop
->len
, dev
->endpoint
[EP2I(ep
)].bufpq_size
);
521 status
= isop
->status
;
522 if (status
!= usb_redir_success
) {
523 bufp_free(dev
, isop
, ep
);
524 return USB_RET_IOERROR
;
528 if (len
> p
->iov
.size
) {
529 ERROR("received iso data is larger then packet ep %02X (%d > %d)\n",
530 ep
, len
, (int)p
->iov
.size
);
531 bufp_free(dev
, isop
, ep
);
532 return USB_RET_BABBLE
;
534 usb_packet_copy(p
, isop
->data
, len
);
535 bufp_free(dev
, isop
, ep
);
538 /* If the stream was not started because of a pending error don't
539 send the packet to the usb-host */
540 if (dev
->endpoint
[EP2I(ep
)].iso_started
) {
541 struct usb_redir_iso_packet_header iso_packet
= {
543 .length
= p
->iov
.size
545 uint8_t buf
[p
->iov
.size
];
546 /* No id, we look at the ep when receiving a status back */
547 usb_packet_copy(p
, buf
, p
->iov
.size
);
548 usbredirparser_send_iso_packet(dev
->parser
, 0, &iso_packet
,
550 usbredirparser_do_write(dev
->parser
);
552 status
= dev
->endpoint
[EP2I(ep
)].iso_error
;
553 dev
->endpoint
[EP2I(ep
)].iso_error
= 0;
554 DPRINTF2("iso-token-out ep %02X status %d len %zd\n", ep
, status
,
556 return usbredir_handle_status(dev
, status
, p
->iov
.size
);
560 static void usbredir_stop_iso_stream(USBRedirDevice
*dev
, uint8_t ep
)
562 struct usb_redir_stop_iso_stream_header stop_iso_stream
= {
565 if (dev
->endpoint
[EP2I(ep
)].iso_started
) {
566 usbredirparser_send_stop_iso_stream(dev
->parser
, 0, &stop_iso_stream
);
567 DPRINTF("iso stream stopped ep %02X\n", ep
);
568 dev
->endpoint
[EP2I(ep
)].iso_started
= 0;
570 dev
->endpoint
[EP2I(ep
)].iso_error
= 0;
571 usbredir_free_bufpq(dev
, ep
);
574 static int usbredir_handle_bulk_data(USBRedirDevice
*dev
, USBPacket
*p
,
577 struct usb_redir_bulk_packet_header bulk_packet
;
578 size_t size
= (p
->combined
) ? p
->combined
->iov
.size
: p
->iov
.size
;
580 DPRINTF("bulk-out ep %02X len %zd id %"PRIu64
"\n", ep
, size
, p
->id
);
582 if (usbredir_already_in_flight(dev
, p
->id
)) {
583 return 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 return USB_RET_ASYNC
;
613 static int 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 return usbredir_handle_status(dev
, status
, 0);
649 DPRINTF("interrupt-token-in ep %02X status %d len %d\n", ep
,
650 intp
->status
, intp
->len
);
652 status
= intp
->status
;
653 if (status
!= usb_redir_success
) {
654 bufp_free(dev
, intp
, ep
);
655 return usbredir_handle_status(dev
, status
, 0);
659 if (len
> p
->iov
.size
) {
660 ERROR("received int data is larger then packet ep %02X\n", ep
);
661 bufp_free(dev
, intp
, ep
);
662 return USB_RET_BABBLE
;
664 usb_packet_copy(p
, intp
->data
, len
);
665 bufp_free(dev
, intp
, ep
);
668 /* Output interrupt endpoint, normal async operation */
669 struct usb_redir_interrupt_packet_header interrupt_packet
;
670 uint8_t buf
[p
->iov
.size
];
672 DPRINTF("interrupt-out ep %02X len %zd id %"PRIu64
"\n", ep
,
675 if (usbredir_already_in_flight(dev
, p
->id
)) {
676 return USB_RET_ASYNC
;
679 interrupt_packet
.endpoint
= ep
;
680 interrupt_packet
.length
= p
->iov
.size
;
682 usb_packet_copy(p
, buf
, p
->iov
.size
);
683 usbredir_log_data(dev
, "interrupt data out:", buf
, p
->iov
.size
);
684 usbredirparser_send_interrupt_packet(dev
->parser
, p
->id
,
685 &interrupt_packet
, buf
, p
->iov
.size
);
686 usbredirparser_do_write(dev
->parser
);
687 return USB_RET_ASYNC
;
691 static void usbredir_stop_interrupt_receiving(USBRedirDevice
*dev
,
694 struct usb_redir_stop_interrupt_receiving_header stop_interrupt_recv
= {
697 if (dev
->endpoint
[EP2I(ep
)].interrupt_started
) {
698 usbredirparser_send_stop_interrupt_receiving(dev
->parser
, 0,
699 &stop_interrupt_recv
);
700 DPRINTF("interrupt recv stopped ep %02X\n", ep
);
701 dev
->endpoint
[EP2I(ep
)].interrupt_started
= 0;
703 dev
->endpoint
[EP2I(ep
)].interrupt_error
= 0;
704 usbredir_free_bufpq(dev
, ep
);
707 static int usbredir_handle_data(USBDevice
*udev
, USBPacket
*p
)
709 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
713 if (p
->pid
== USB_TOKEN_IN
) {
717 switch (dev
->endpoint
[EP2I(ep
)].type
) {
718 case USB_ENDPOINT_XFER_CONTROL
:
719 ERROR("handle_data called for control transfer on ep %02X\n", ep
);
721 case USB_ENDPOINT_XFER_ISOC
:
722 return usbredir_handle_iso_data(dev
, p
, ep
);
723 case USB_ENDPOINT_XFER_BULK
:
724 if (p
->state
== USB_PACKET_SETUP
&& p
->pid
== USB_TOKEN_IN
&&
726 return USB_RET_ADD_TO_QUEUE
;
728 return usbredir_handle_bulk_data(dev
, p
, ep
);
729 case USB_ENDPOINT_XFER_INT
:
730 return usbredir_handle_interrupt_data(dev
, p
, ep
);
732 ERROR("handle_data ep %02X has unknown type %d\n", ep
,
733 dev
->endpoint
[EP2I(ep
)].type
);
738 static void usbredir_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
740 if (ep
->pid
== USB_TOKEN_IN
&& ep
->pipeline
) {
741 usb_ep_combine_input_packets(ep
);
745 static int usbredir_set_config(USBRedirDevice
*dev
, USBPacket
*p
,
748 struct usb_redir_set_configuration_header set_config
;
751 DPRINTF("set config %d id %"PRIu64
"\n", config
, p
->id
);
753 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
754 switch (dev
->endpoint
[i
].type
) {
755 case USB_ENDPOINT_XFER_ISOC
:
756 usbredir_stop_iso_stream(dev
, I2EP(i
));
758 case USB_ENDPOINT_XFER_INT
:
760 usbredir_stop_interrupt_receiving(dev
, I2EP(i
));
764 usbredir_free_bufpq(dev
, I2EP(i
));
767 set_config
.configuration
= config
;
768 usbredirparser_send_set_configuration(dev
->parser
, p
->id
, &set_config
);
769 usbredirparser_do_write(dev
->parser
);
770 return USB_RET_ASYNC
;
773 static int usbredir_get_config(USBRedirDevice
*dev
, USBPacket
*p
)
775 DPRINTF("get config id %"PRIu64
"\n", p
->id
);
777 usbredirparser_send_get_configuration(dev
->parser
, p
->id
);
778 usbredirparser_do_write(dev
->parser
);
779 return USB_RET_ASYNC
;
782 static int usbredir_set_interface(USBRedirDevice
*dev
, USBPacket
*p
,
783 int interface
, int alt
)
785 struct usb_redir_set_alt_setting_header set_alt
;
788 DPRINTF("set interface %d alt %d id %"PRIu64
"\n", interface
, alt
, p
->id
);
790 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
791 if (dev
->endpoint
[i
].interface
== interface
) {
792 switch (dev
->endpoint
[i
].type
) {
793 case USB_ENDPOINT_XFER_ISOC
:
794 usbredir_stop_iso_stream(dev
, I2EP(i
));
796 case USB_ENDPOINT_XFER_INT
:
798 usbredir_stop_interrupt_receiving(dev
, I2EP(i
));
802 usbredir_free_bufpq(dev
, I2EP(i
));
806 set_alt
.interface
= interface
;
808 usbredirparser_send_set_alt_setting(dev
->parser
, p
->id
, &set_alt
);
809 usbredirparser_do_write(dev
->parser
);
810 return USB_RET_ASYNC
;
813 static int usbredir_get_interface(USBRedirDevice
*dev
, USBPacket
*p
,
816 struct usb_redir_get_alt_setting_header get_alt
;
818 DPRINTF("get interface %d id %"PRIu64
"\n", interface
, p
->id
);
820 get_alt
.interface
= interface
;
821 usbredirparser_send_get_alt_setting(dev
->parser
, p
->id
, &get_alt
);
822 usbredirparser_do_write(dev
->parser
);
823 return USB_RET_ASYNC
;
826 static int usbredir_handle_control(USBDevice
*udev
, USBPacket
*p
,
827 int request
, int value
, int index
, int length
, uint8_t *data
)
829 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
830 struct usb_redir_control_packet_header control_packet
;
832 if (usbredir_already_in_flight(dev
, p
->id
)) {
833 return USB_RET_ASYNC
;
836 /* Special cases for certain standard device requests */
838 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
839 DPRINTF("set address %d\n", value
);
840 dev
->dev
.addr
= value
;
842 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
843 return usbredir_set_config(dev
, p
, value
& 0xff);
844 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
845 return usbredir_get_config(dev
, p
);
846 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
847 return usbredir_set_interface(dev
, p
, index
, value
);
848 case InterfaceRequest
| USB_REQ_GET_INTERFACE
:
849 return usbredir_get_interface(dev
, p
, index
);
852 /* Normal ctrl requests, note request is (bRequestType << 8) | bRequest */
854 "ctrl-out type 0x%x req 0x%x val 0x%x index %d len %d id %"PRIu64
"\n",
855 request
>> 8, request
& 0xff, value
, index
, length
, p
->id
);
857 control_packet
.request
= request
& 0xFF;
858 control_packet
.requesttype
= request
>> 8;
859 control_packet
.endpoint
= control_packet
.requesttype
& USB_DIR_IN
;
860 control_packet
.value
= value
;
861 control_packet
.index
= index
;
862 control_packet
.length
= length
;
864 if (control_packet
.requesttype
& USB_DIR_IN
) {
865 usbredirparser_send_control_packet(dev
->parser
, p
->id
,
866 &control_packet
, NULL
, 0);
868 usbredir_log_data(dev
, "ctrl data out:", data
, length
);
869 usbredirparser_send_control_packet(dev
->parser
, p
->id
,
870 &control_packet
, data
, length
);
872 usbredirparser_do_write(dev
->parser
);
873 return USB_RET_ASYNC
;
877 * Close events can be triggered by usbredirparser_do_write which gets called
878 * from within the USBDevice data / control packet callbacks and doing a
879 * usb_detach from within these callbacks is not a good idea.
881 * So we use a bh handler to take care of close events.
883 static void usbredir_chardev_close_bh(void *opaque
)
885 USBRedirDevice
*dev
= opaque
;
887 usbredir_device_disconnect(dev
);
890 DPRINTF("destroying usbredirparser\n");
891 usbredirparser_destroy(dev
->parser
);
896 static void usbredir_create_parser(USBRedirDevice
*dev
)
898 uint32_t caps
[USB_REDIR_CAPS_SIZE
] = { 0, };
901 DPRINTF("creating usbredirparser\n");
903 dev
->parser
= qemu_oom_check(usbredirparser_create());
904 dev
->parser
->priv
= dev
;
905 dev
->parser
->log_func
= usbredir_log
;
906 dev
->parser
->read_func
= usbredir_read
;
907 dev
->parser
->write_func
= usbredir_write
;
908 dev
->parser
->hello_func
= usbredir_hello
;
909 dev
->parser
->device_connect_func
= usbredir_device_connect
;
910 dev
->parser
->device_disconnect_func
= usbredir_device_disconnect
;
911 dev
->parser
->interface_info_func
= usbredir_interface_info
;
912 dev
->parser
->ep_info_func
= usbredir_ep_info
;
913 dev
->parser
->configuration_status_func
= usbredir_configuration_status
;
914 dev
->parser
->alt_setting_status_func
= usbredir_alt_setting_status
;
915 dev
->parser
->iso_stream_status_func
= usbredir_iso_stream_status
;
916 dev
->parser
->interrupt_receiving_status_func
=
917 usbredir_interrupt_receiving_status
;
918 dev
->parser
->bulk_streams_status_func
= usbredir_bulk_streams_status
;
919 dev
->parser
->control_packet_func
= usbredir_control_packet
;
920 dev
->parser
->bulk_packet_func
= usbredir_bulk_packet
;
921 dev
->parser
->iso_packet_func
= usbredir_iso_packet
;
922 dev
->parser
->interrupt_packet_func
= usbredir_interrupt_packet
;
923 dev
->read_buf
= NULL
;
924 dev
->read_buf_size
= 0;
926 usbredirparser_caps_set_cap(caps
, usb_redir_cap_connect_device_version
);
927 usbredirparser_caps_set_cap(caps
, usb_redir_cap_filter
);
928 usbredirparser_caps_set_cap(caps
, usb_redir_cap_ep_info_max_packet_size
);
929 usbredirparser_caps_set_cap(caps
, usb_redir_cap_64bits_ids
);
930 usbredirparser_caps_set_cap(caps
, usb_redir_cap_32bits_bulk_length
);
932 if (runstate_check(RUN_STATE_INMIGRATE
)) {
933 flags
|= usbredirparser_fl_no_hello
;
935 usbredirparser_init(dev
->parser
, VERSION
, caps
, USB_REDIR_CAPS_SIZE
,
937 usbredirparser_do_write(dev
->parser
);
940 static void usbredir_reject_device(USBRedirDevice
*dev
)
942 usbredir_device_disconnect(dev
);
943 if (usbredirparser_peer_has_cap(dev
->parser
, usb_redir_cap_filter
)) {
944 usbredirparser_send_filter_reject(dev
->parser
);
945 usbredirparser_do_write(dev
->parser
);
949 static void usbredir_do_attach(void *opaque
)
951 USBRedirDevice
*dev
= opaque
;
953 /* In order to work properly with XHCI controllers we need these caps */
954 if ((dev
->dev
.port
->speedmask
& USB_SPEED_MASK_SUPER
) && !(
955 usbredirparser_peer_has_cap(dev
->parser
,
956 usb_redir_cap_ep_info_max_packet_size
) &&
957 usbredirparser_peer_has_cap(dev
->parser
,
958 usb_redir_cap_64bits_ids
))) {
959 ERROR("usb-redir-host lacks capabilities needed for use with XHCI\n");
960 usbredir_reject_device(dev
);
964 if (usb_device_attach(&dev
->dev
) != 0) {
965 usbredir_reject_device(dev
);
973 static int usbredir_chardev_can_read(void *opaque
)
975 USBRedirDevice
*dev
= opaque
;
978 WARNING("chardev_can_read called on non open chardev!\n");
982 /* Don't read new data from the chardev until our state is fully synced */
983 if (!runstate_check(RUN_STATE_RUNNING
)) {
987 /* usbredir_parser_do_read will consume *all* data we give it */
991 static void usbredir_chardev_read(void *opaque
, const uint8_t *buf
, int size
)
993 USBRedirDevice
*dev
= opaque
;
995 /* No recursion allowed! */
996 assert(dev
->read_buf
== NULL
);
999 dev
->read_buf_size
= size
;
1001 usbredirparser_do_read(dev
->parser
);
1002 /* Send any acks, etc. which may be queued now */
1003 usbredirparser_do_write(dev
->parser
);
1006 static void usbredir_chardev_event(void *opaque
, int event
)
1008 USBRedirDevice
*dev
= opaque
;
1011 case CHR_EVENT_OPENED
:
1012 DPRINTF("chardev open\n");
1013 /* Make sure any pending closes are handled (no-op if none pending) */
1014 usbredir_chardev_close_bh(dev
);
1015 qemu_bh_cancel(dev
->chardev_close_bh
);
1016 usbredir_create_parser(dev
);
1018 case CHR_EVENT_CLOSED
:
1019 DPRINTF("chardev close\n");
1020 qemu_bh_schedule(dev
->chardev_close_bh
);
1029 static void usbredir_vm_state_change(void *priv
, int running
, RunState state
)
1031 USBRedirDevice
*dev
= priv
;
1033 if (state
== RUN_STATE_RUNNING
&& dev
->parser
!= NULL
) {
1034 usbredirparser_do_write(dev
->parser
); /* Flush any pending writes */
1038 static int usbredir_initfn(USBDevice
*udev
)
1040 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
1043 if (dev
->cs
== NULL
) {
1044 qerror_report(QERR_MISSING_PARAMETER
, "chardev");
1048 if (dev
->filter_str
) {
1049 i
= usbredirfilter_string_to_rules(dev
->filter_str
, ":", "|",
1051 &dev
->filter_rules_count
);
1053 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "filter",
1054 "a usb device filter string");
1059 dev
->chardev_close_bh
= qemu_bh_new(usbredir_chardev_close_bh
, dev
);
1060 dev
->attach_timer
= qemu_new_timer_ms(vm_clock
, usbredir_do_attach
, dev
);
1062 packet_id_queue_init(&dev
->cancelled
, dev
, "cancelled");
1063 packet_id_queue_init(&dev
->already_in_flight
, dev
, "already-in-flight");
1064 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1065 QTAILQ_INIT(&dev
->endpoint
[i
].bufpq
);
1068 /* We'll do the attach once we receive the speed from the usb-host */
1069 udev
->auto_attach
= 0;
1071 /* Let the backend know we are ready */
1072 qemu_chr_fe_open(dev
->cs
);
1073 qemu_chr_add_handlers(dev
->cs
, usbredir_chardev_can_read
,
1074 usbredir_chardev_read
, usbredir_chardev_event
, dev
);
1076 qemu_add_vm_change_state_handler(usbredir_vm_state_change
, dev
);
1077 add_boot_device_path(dev
->bootindex
, &udev
->qdev
, NULL
);
1081 static void usbredir_cleanup_device_queues(USBRedirDevice
*dev
)
1085 packet_id_queue_empty(&dev
->cancelled
);
1086 packet_id_queue_empty(&dev
->already_in_flight
);
1087 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1088 usbredir_free_bufpq(dev
, I2EP(i
));
1092 static void usbredir_handle_destroy(USBDevice
*udev
)
1094 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
1096 qemu_chr_fe_close(dev
->cs
);
1097 qemu_chr_delete(dev
->cs
);
1098 /* Note must be done after qemu_chr_close, as that causes a close event */
1099 qemu_bh_delete(dev
->chardev_close_bh
);
1101 qemu_del_timer(dev
->attach_timer
);
1102 qemu_free_timer(dev
->attach_timer
);
1104 usbredir_cleanup_device_queues(dev
);
1107 usbredirparser_destroy(dev
->parser
);
1110 free(dev
->filter_rules
);
1113 static int usbredir_check_filter(USBRedirDevice
*dev
)
1115 if (dev
->interface_info
.interface_count
== NO_INTERFACE_INFO
) {
1116 ERROR("No interface info for device\n");
1120 if (dev
->filter_rules
) {
1121 if (!usbredirparser_peer_has_cap(dev
->parser
,
1122 usb_redir_cap_connect_device_version
)) {
1123 ERROR("Device filter specified and peer does not have the "
1124 "connect_device_version capability\n");
1128 if (usbredirfilter_check(
1130 dev
->filter_rules_count
,
1131 dev
->device_info
.device_class
,
1132 dev
->device_info
.device_subclass
,
1133 dev
->device_info
.device_protocol
,
1134 dev
->interface_info
.interface_class
,
1135 dev
->interface_info
.interface_subclass
,
1136 dev
->interface_info
.interface_protocol
,
1137 dev
->interface_info
.interface_count
,
1138 dev
->device_info
.vendor_id
,
1139 dev
->device_info
.product_id
,
1140 dev
->device_info
.device_version_bcd
,
1149 usbredir_reject_device(dev
);
1154 * usbredirparser packet complete callbacks
1157 static int usbredir_handle_status(USBRedirDevice
*dev
,
1158 int status
, int actual_len
)
1161 case usb_redir_success
:
1163 case usb_redir_stall
:
1164 return USB_RET_STALL
;
1165 case usb_redir_cancelled
:
1167 * When the usbredir-host unredirects a device, it will report a status
1168 * of cancelled for all pending packets, followed by a disconnect msg.
1170 return USB_RET_IOERROR
;
1171 case usb_redir_inval
:
1172 WARNING("got invalid param error from usb-host?\n");
1173 return USB_RET_IOERROR
;
1174 case usb_redir_babble
:
1175 return USB_RET_BABBLE
;
1176 case usb_redir_ioerror
:
1177 case usb_redir_timeout
:
1179 return USB_RET_IOERROR
;
1183 static void usbredir_hello(void *priv
, struct usb_redir_hello_header
*h
)
1185 USBRedirDevice
*dev
= priv
;
1187 /* Try to send the filter info now that we've the usb-host's caps */
1188 if (usbredirparser_peer_has_cap(dev
->parser
, usb_redir_cap_filter
) &&
1189 dev
->filter_rules
) {
1190 usbredirparser_send_filter_filter(dev
->parser
, dev
->filter_rules
,
1191 dev
->filter_rules_count
);
1192 usbredirparser_do_write(dev
->parser
);
1196 static void usbredir_device_connect(void *priv
,
1197 struct usb_redir_device_connect_header
*device_connect
)
1199 USBRedirDevice
*dev
= priv
;
1202 if (qemu_timer_pending(dev
->attach_timer
) || dev
->dev
.attached
) {
1203 ERROR("Received device connect while already connected\n");
1207 switch (device_connect
->speed
) {
1208 case usb_redir_speed_low
:
1209 speed
= "low speed";
1210 dev
->dev
.speed
= USB_SPEED_LOW
;
1212 case usb_redir_speed_full
:
1213 speed
= "full speed";
1214 dev
->dev
.speed
= USB_SPEED_FULL
;
1216 case usb_redir_speed_high
:
1217 speed
= "high speed";
1218 dev
->dev
.speed
= USB_SPEED_HIGH
;
1220 case usb_redir_speed_super
:
1221 speed
= "super speed";
1222 dev
->dev
.speed
= USB_SPEED_SUPER
;
1225 speed
= "unknown speed";
1226 dev
->dev
.speed
= USB_SPEED_FULL
;
1229 if (usbredirparser_peer_has_cap(dev
->parser
,
1230 usb_redir_cap_connect_device_version
)) {
1231 INFO("attaching %s device %04x:%04x version %d.%d class %02x\n",
1232 speed
, device_connect
->vendor_id
, device_connect
->product_id
,
1233 ((device_connect
->device_version_bcd
& 0xf000) >> 12) * 10 +
1234 ((device_connect
->device_version_bcd
& 0x0f00) >> 8),
1235 ((device_connect
->device_version_bcd
& 0x00f0) >> 4) * 10 +
1236 ((device_connect
->device_version_bcd
& 0x000f) >> 0),
1237 device_connect
->device_class
);
1239 INFO("attaching %s device %04x:%04x class %02x\n", speed
,
1240 device_connect
->vendor_id
, device_connect
->product_id
,
1241 device_connect
->device_class
);
1244 dev
->dev
.speedmask
= (1 << dev
->dev
.speed
);
1245 dev
->device_info
= *device_connect
;
1247 if (usbredir_check_filter(dev
)) {
1248 WARNING("Device %04x:%04x rejected by device filter, not attaching\n",
1249 device_connect
->vendor_id
, device_connect
->product_id
);
1253 qemu_mod_timer(dev
->attach_timer
, dev
->next_attach_time
);
1256 static void usbredir_device_disconnect(void *priv
)
1258 USBRedirDevice
*dev
= priv
;
1261 /* Stop any pending attaches */
1262 qemu_del_timer(dev
->attach_timer
);
1264 if (dev
->dev
.attached
) {
1265 DPRINTF("detaching device\n");
1266 usb_device_detach(&dev
->dev
);
1268 * Delay next usb device attach to give the guest a chance to see
1269 * see the detach / attach in case of quick close / open succession
1271 dev
->next_attach_time
= qemu_get_clock_ms(vm_clock
) + 200;
1274 /* Reset state so that the next dev connected starts with a clean slate */
1275 usbredir_cleanup_device_queues(dev
);
1276 memset(dev
->endpoint
, 0, sizeof(dev
->endpoint
));
1277 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1278 QTAILQ_INIT(&dev
->endpoint
[i
].bufpq
);
1280 usb_ep_init(&dev
->dev
);
1281 dev
->interface_info
.interface_count
= NO_INTERFACE_INFO
;
1286 static void usbredir_interface_info(void *priv
,
1287 struct usb_redir_interface_info_header
*interface_info
)
1289 USBRedirDevice
*dev
= priv
;
1291 dev
->interface_info
= *interface_info
;
1294 * If we receive interface info after the device has already been
1295 * connected (ie on a set_config), re-check the filter.
1297 if (qemu_timer_pending(dev
->attach_timer
) || dev
->dev
.attached
) {
1298 if (usbredir_check_filter(dev
)) {
1299 ERROR("Device no longer matches filter after interface info "
1300 "change, disconnecting!\n");
1305 static void usbredir_set_pipeline(USBRedirDevice
*dev
, struct USBEndpoint
*uep
)
1307 if (uep
->type
!= USB_ENDPOINT_XFER_BULK
) {
1310 if (uep
->pid
== USB_TOKEN_OUT
) {
1311 uep
->pipeline
= true;
1313 if (uep
->pid
== USB_TOKEN_IN
&& uep
->max_packet_size
!= 0 &&
1314 usbredirparser_peer_has_cap(dev
->parser
,
1315 usb_redir_cap_32bits_bulk_length
)) {
1316 uep
->pipeline
= true;
1320 static void usbredir_setup_usb_eps(USBRedirDevice
*dev
)
1322 struct USBEndpoint
*usb_ep
;
1325 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1326 pid
= (i
& 0x10) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1327 usb_ep
= usb_ep_get(&dev
->dev
, pid
, i
& 0x0f);
1328 usb_ep
->type
= dev
->endpoint
[i
].type
;
1329 usb_ep
->ifnum
= dev
->endpoint
[i
].interface
;
1330 usb_ep
->max_packet_size
= dev
->endpoint
[i
].max_packet_size
;
1331 usbredir_set_pipeline(dev
, usb_ep
);
1335 static void usbredir_ep_info(void *priv
,
1336 struct usb_redir_ep_info_header
*ep_info
)
1338 USBRedirDevice
*dev
= priv
;
1341 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1342 dev
->endpoint
[i
].type
= ep_info
->type
[i
];
1343 dev
->endpoint
[i
].interval
= ep_info
->interval
[i
];
1344 dev
->endpoint
[i
].interface
= ep_info
->interface
[i
];
1345 if (usbredirparser_peer_has_cap(dev
->parser
,
1346 usb_redir_cap_ep_info_max_packet_size
)) {
1347 dev
->endpoint
[i
].max_packet_size
= ep_info
->max_packet_size
[i
];
1349 switch (dev
->endpoint
[i
].type
) {
1350 case usb_redir_type_invalid
:
1352 case usb_redir_type_iso
:
1353 case usb_redir_type_interrupt
:
1354 if (dev
->endpoint
[i
].interval
== 0) {
1355 ERROR("Received 0 interval for isoc or irq endpoint\n");
1356 usbredir_device_disconnect(dev
);
1359 case usb_redir_type_control
:
1360 case usb_redir_type_bulk
:
1361 DPRINTF("ep: %02X type: %d interface: %d\n", I2EP(i
),
1362 dev
->endpoint
[i
].type
, dev
->endpoint
[i
].interface
);
1365 ERROR("Received invalid endpoint type\n");
1366 usbredir_device_disconnect(dev
);
1370 usbredir_setup_usb_eps(dev
);
1373 static void usbredir_configuration_status(void *priv
, uint64_t id
,
1374 struct usb_redir_configuration_status_header
*config_status
)
1376 USBRedirDevice
*dev
= priv
;
1380 DPRINTF("set config status %d config %d id %"PRIu64
"\n",
1381 config_status
->status
, config_status
->configuration
, id
);
1383 p
= usbredir_find_packet_by_id(dev
, 0, id
);
1385 if (dev
->dev
.setup_buf
[0] & USB_DIR_IN
) {
1386 dev
->dev
.data_buf
[0] = config_status
->configuration
;
1389 p
->result
= usbredir_handle_status(dev
, config_status
->status
, len
);
1390 usb_generic_async_ctrl_complete(&dev
->dev
, p
);
1394 static void usbredir_alt_setting_status(void *priv
, uint64_t id
,
1395 struct usb_redir_alt_setting_status_header
*alt_setting_status
)
1397 USBRedirDevice
*dev
= priv
;
1401 DPRINTF("alt status %d intf %d alt %d id: %"PRIu64
"\n",
1402 alt_setting_status
->status
, alt_setting_status
->interface
,
1403 alt_setting_status
->alt
, id
);
1405 p
= usbredir_find_packet_by_id(dev
, 0, id
);
1407 if (dev
->dev
.setup_buf
[0] & USB_DIR_IN
) {
1408 dev
->dev
.data_buf
[0] = alt_setting_status
->alt
;
1412 usbredir_handle_status(dev
, alt_setting_status
->status
, len
);
1413 usb_generic_async_ctrl_complete(&dev
->dev
, p
);
1417 static void usbredir_iso_stream_status(void *priv
, uint64_t id
,
1418 struct usb_redir_iso_stream_status_header
*iso_stream_status
)
1420 USBRedirDevice
*dev
= priv
;
1421 uint8_t ep
= iso_stream_status
->endpoint
;
1423 DPRINTF("iso status %d ep %02X id %"PRIu64
"\n", iso_stream_status
->status
,
1426 if (!dev
->dev
.attached
|| !dev
->endpoint
[EP2I(ep
)].iso_started
) {
1430 dev
->endpoint
[EP2I(ep
)].iso_error
= iso_stream_status
->status
;
1431 if (iso_stream_status
->status
== usb_redir_stall
) {
1432 DPRINTF("iso stream stopped by peer ep %02X\n", ep
);
1433 dev
->endpoint
[EP2I(ep
)].iso_started
= 0;
1437 static void usbredir_interrupt_receiving_status(void *priv
, uint64_t id
,
1438 struct usb_redir_interrupt_receiving_status_header
1439 *interrupt_receiving_status
)
1441 USBRedirDevice
*dev
= priv
;
1442 uint8_t ep
= interrupt_receiving_status
->endpoint
;
1444 DPRINTF("interrupt recv status %d ep %02X id %"PRIu64
"\n",
1445 interrupt_receiving_status
->status
, ep
, id
);
1447 if (!dev
->dev
.attached
|| !dev
->endpoint
[EP2I(ep
)].interrupt_started
) {
1451 dev
->endpoint
[EP2I(ep
)].interrupt_error
=
1452 interrupt_receiving_status
->status
;
1453 if (interrupt_receiving_status
->status
== usb_redir_stall
) {
1454 DPRINTF("interrupt receiving stopped by peer ep %02X\n", ep
);
1455 dev
->endpoint
[EP2I(ep
)].interrupt_started
= 0;
1459 static void usbredir_bulk_streams_status(void *priv
, uint64_t id
,
1460 struct usb_redir_bulk_streams_status_header
*bulk_streams_status
)
1464 static void usbredir_control_packet(void *priv
, uint64_t id
,
1465 struct usb_redir_control_packet_header
*control_packet
,
1466 uint8_t *data
, int data_len
)
1468 USBRedirDevice
*dev
= priv
;
1470 int len
= control_packet
->length
;
1472 DPRINTF("ctrl-in status %d len %d id %"PRIu64
"\n", control_packet
->status
,
1475 p
= usbredir_find_packet_by_id(dev
, 0, id
);
1477 len
= usbredir_handle_status(dev
, control_packet
->status
, len
);
1479 usbredir_log_data(dev
, "ctrl data in:", data
, data_len
);
1480 if (data_len
<= sizeof(dev
->dev
.data_buf
)) {
1481 memcpy(dev
->dev
.data_buf
, data
, data_len
);
1483 ERROR("ctrl buffer too small (%d > %zu)\n",
1484 data_len
, sizeof(dev
->dev
.data_buf
));
1485 len
= USB_RET_STALL
;
1489 usb_generic_async_ctrl_complete(&dev
->dev
, p
);
1494 static void usbredir_bulk_packet(void *priv
, uint64_t id
,
1495 struct usb_redir_bulk_packet_header
*bulk_packet
,
1496 uint8_t *data
, int data_len
)
1498 USBRedirDevice
*dev
= priv
;
1499 uint8_t ep
= bulk_packet
->endpoint
;
1500 int len
= (bulk_packet
->length_high
<< 16) | bulk_packet
->length
;
1503 DPRINTF("bulk-in status %d ep %02X len %d id %"PRIu64
"\n",
1504 bulk_packet
->status
, ep
, len
, id
);
1506 p
= usbredir_find_packet_by_id(dev
, ep
, id
);
1508 size_t size
= (p
->combined
) ? p
->combined
->iov
.size
: p
->iov
.size
;
1509 len
= usbredir_handle_status(dev
, bulk_packet
->status
, len
);
1511 usbredir_log_data(dev
, "bulk data in:", data
, data_len
);
1512 if (data_len
<= size
) {
1514 iov_from_buf(p
->combined
->iov
.iov
, p
->combined
->iov
.niov
,
1517 usb_packet_copy(p
, data
, data_len
);
1520 ERROR("bulk got more data then requested (%d > %zd)\n",
1521 data_len
, p
->iov
.size
);
1522 len
= USB_RET_BABBLE
;
1526 if (p
->pid
== USB_TOKEN_IN
&& p
->ep
->pipeline
) {
1527 usb_combined_input_packet_complete(&dev
->dev
, p
);
1529 usb_packet_complete(&dev
->dev
, p
);
1535 static void usbredir_iso_packet(void *priv
, uint64_t id
,
1536 struct usb_redir_iso_packet_header
*iso_packet
,
1537 uint8_t *data
, int data_len
)
1539 USBRedirDevice
*dev
= priv
;
1540 uint8_t ep
= iso_packet
->endpoint
;
1542 DPRINTF2("iso-in status %d ep %02X len %d id %"PRIu64
"\n",
1543 iso_packet
->status
, ep
, data_len
, id
);
1545 if (dev
->endpoint
[EP2I(ep
)].type
!= USB_ENDPOINT_XFER_ISOC
) {
1546 ERROR("received iso packet for non iso endpoint %02X\n", ep
);
1551 if (dev
->endpoint
[EP2I(ep
)].iso_started
== 0) {
1552 DPRINTF("received iso packet for non started stream ep %02X\n", ep
);
1557 /* bufp_alloc also adds the packet to the ep queue */
1558 bufp_alloc(dev
, data
, data_len
, iso_packet
->status
, ep
);
1561 static void usbredir_interrupt_packet(void *priv
, uint64_t id
,
1562 struct usb_redir_interrupt_packet_header
*interrupt_packet
,
1563 uint8_t *data
, int data_len
)
1565 USBRedirDevice
*dev
= priv
;
1566 uint8_t ep
= interrupt_packet
->endpoint
;
1568 DPRINTF("interrupt-in status %d ep %02X len %d id %"PRIu64
"\n",
1569 interrupt_packet
->status
, ep
, data_len
, id
);
1571 if (dev
->endpoint
[EP2I(ep
)].type
!= USB_ENDPOINT_XFER_INT
) {
1572 ERROR("received int packet for non interrupt endpoint %02X\n", ep
);
1577 if (ep
& USB_DIR_IN
) {
1578 if (dev
->endpoint
[EP2I(ep
)].interrupt_started
== 0) {
1579 DPRINTF("received int packet while not started ep %02X\n", ep
);
1584 /* bufp_alloc also adds the packet to the ep queue */
1585 bufp_alloc(dev
, data
, data_len
, interrupt_packet
->status
, ep
);
1587 int len
= interrupt_packet
->length
;
1589 USBPacket
*p
= usbredir_find_packet_by_id(dev
, ep
, id
);
1591 p
->result
= usbredir_handle_status(dev
,
1592 interrupt_packet
->status
, len
);
1593 usb_packet_complete(&dev
->dev
, p
);
1602 static void usbredir_pre_save(void *priv
)
1604 USBRedirDevice
*dev
= priv
;
1606 usbredir_fill_already_in_flight(dev
);
1609 static int usbredir_post_load(void *priv
, int version_id
)
1611 USBRedirDevice
*dev
= priv
;
1613 switch (dev
->device_info
.speed
) {
1614 case usb_redir_speed_low
:
1615 dev
->dev
.speed
= USB_SPEED_LOW
;
1617 case usb_redir_speed_full
:
1618 dev
->dev
.speed
= USB_SPEED_FULL
;
1620 case usb_redir_speed_high
:
1621 dev
->dev
.speed
= USB_SPEED_HIGH
;
1623 case usb_redir_speed_super
:
1624 dev
->dev
.speed
= USB_SPEED_SUPER
;
1627 dev
->dev
.speed
= USB_SPEED_FULL
;
1629 dev
->dev
.speedmask
= (1 << dev
->dev
.speed
);
1631 usbredir_setup_usb_eps(dev
);
1636 /* For usbredirparser migration */
1637 static void usbredir_put_parser(QEMUFile
*f
, void *priv
, size_t unused
)
1639 USBRedirDevice
*dev
= priv
;
1643 if (dev
->parser
== NULL
) {
1644 qemu_put_be32(f
, 0);
1648 usbredirparser_serialize(dev
->parser
, &data
, &len
);
1649 qemu_oom_check(data
);
1651 qemu_put_be32(f
, len
);
1652 qemu_put_buffer(f
, data
, len
);
1657 static int usbredir_get_parser(QEMUFile
*f
, void *priv
, size_t unused
)
1659 USBRedirDevice
*dev
= priv
;
1663 len
= qemu_get_be32(f
);
1669 * If our chardev is not open already at this point the usbredir connection
1670 * has been broken (non seamless migration, or restore from disk).
1672 * In this case create a temporary parser to receive the migration data,
1673 * and schedule the close_bh to report the device as disconnected to the
1674 * guest and to destroy the parser again.
1676 if (dev
->parser
== NULL
) {
1677 WARNING("usb-redir connection broken during migration\n");
1678 usbredir_create_parser(dev
);
1679 qemu_bh_schedule(dev
->chardev_close_bh
);
1682 data
= g_malloc(len
);
1683 qemu_get_buffer(f
, data
, len
);
1685 ret
= usbredirparser_unserialize(dev
->parser
, data
, len
);
1692 static const VMStateInfo usbredir_parser_vmstate_info
= {
1693 .name
= "usb-redir-parser",
1694 .put
= usbredir_put_parser
,
1695 .get
= usbredir_get_parser
,
1699 /* For buffered packets (iso/irq) queue migration */
1700 static void usbredir_put_bufpq(QEMUFile
*f
, void *priv
, size_t unused
)
1702 struct endp_data
*endp
= priv
;
1703 struct buf_packet
*bufp
;
1704 int remain
= endp
->bufpq_size
;
1706 qemu_put_be32(f
, endp
->bufpq_size
);
1707 QTAILQ_FOREACH(bufp
, &endp
->bufpq
, next
) {
1708 qemu_put_be32(f
, bufp
->len
);
1709 qemu_put_be32(f
, bufp
->status
);
1710 qemu_put_buffer(f
, bufp
->data
, bufp
->len
);
1713 assert(remain
== 0);
1716 static int usbredir_get_bufpq(QEMUFile
*f
, void *priv
, size_t unused
)
1718 struct endp_data
*endp
= priv
;
1719 struct buf_packet
*bufp
;
1722 endp
->bufpq_size
= qemu_get_be32(f
);
1723 for (i
= 0; i
< endp
->bufpq_size
; i
++) {
1724 bufp
= g_malloc(sizeof(struct buf_packet
));
1725 bufp
->len
= qemu_get_be32(f
);
1726 bufp
->status
= qemu_get_be32(f
);
1727 bufp
->data
= qemu_oom_check(malloc(bufp
->len
)); /* regular malloc! */
1728 qemu_get_buffer(f
, bufp
->data
, bufp
->len
);
1729 QTAILQ_INSERT_TAIL(&endp
->bufpq
, bufp
, next
);
1734 static const VMStateInfo usbredir_ep_bufpq_vmstate_info
= {
1735 .name
= "usb-redir-bufpq",
1736 .put
= usbredir_put_bufpq
,
1737 .get
= usbredir_get_bufpq
,
1741 /* For endp_data migration */
1742 static const VMStateDescription usbredir_ep_vmstate
= {
1743 .name
= "usb-redir-ep",
1745 .minimum_version_id
= 1,
1746 .fields
= (VMStateField
[]) {
1747 VMSTATE_UINT8(type
, struct endp_data
),
1748 VMSTATE_UINT8(interval
, struct endp_data
),
1749 VMSTATE_UINT8(interface
, struct endp_data
),
1750 VMSTATE_UINT16(max_packet_size
, struct endp_data
),
1751 VMSTATE_UINT8(iso_started
, struct endp_data
),
1752 VMSTATE_UINT8(iso_error
, struct endp_data
),
1753 VMSTATE_UINT8(interrupt_started
, struct endp_data
),
1754 VMSTATE_UINT8(interrupt_error
, struct endp_data
),
1755 VMSTATE_UINT8(bufpq_prefilled
, struct endp_data
),
1756 VMSTATE_UINT8(bufpq_dropping_packets
, struct endp_data
),
1760 .field_exists
= NULL
,
1762 .info
= &usbredir_ep_bufpq_vmstate_info
,
1763 .flags
= VMS_SINGLE
,
1766 VMSTATE_INT32(bufpq_target_size
, struct endp_data
),
1767 VMSTATE_END_OF_LIST()
1772 /* For PacketIdQueue migration */
1773 static void usbredir_put_packet_id_q(QEMUFile
*f
, void *priv
, size_t unused
)
1775 struct PacketIdQueue
*q
= priv
;
1776 USBRedirDevice
*dev
= q
->dev
;
1777 struct PacketIdQueueEntry
*e
;
1778 int remain
= q
->size
;
1780 DPRINTF("put_packet_id_q %s size %d\n", q
->name
, q
->size
);
1781 qemu_put_be32(f
, q
->size
);
1782 QTAILQ_FOREACH(e
, &q
->head
, next
) {
1783 qemu_put_be64(f
, e
->id
);
1786 assert(remain
== 0);
1789 static int usbredir_get_packet_id_q(QEMUFile
*f
, void *priv
, size_t unused
)
1791 struct PacketIdQueue
*q
= priv
;
1792 USBRedirDevice
*dev
= q
->dev
;
1796 size
= qemu_get_be32(f
);
1797 DPRINTF("get_packet_id_q %s size %d\n", q
->name
, size
);
1798 for (i
= 0; i
< size
; i
++) {
1799 id
= qemu_get_be64(f
);
1800 packet_id_queue_add(q
, id
);
1802 assert(q
->size
== size
);
1806 static const VMStateInfo usbredir_ep_packet_id_q_vmstate_info
= {
1807 .name
= "usb-redir-packet-id-q",
1808 .put
= usbredir_put_packet_id_q
,
1809 .get
= usbredir_get_packet_id_q
,
1812 static const VMStateDescription usbredir_ep_packet_id_queue_vmstate
= {
1813 .name
= "usb-redir-packet-id-queue",
1815 .minimum_version_id
= 1,
1816 .fields
= (VMStateField
[]) {
1820 .field_exists
= NULL
,
1822 .info
= &usbredir_ep_packet_id_q_vmstate_info
,
1823 .flags
= VMS_SINGLE
,
1826 VMSTATE_END_OF_LIST()
1831 /* For usb_redir_device_connect_header migration */
1832 static const VMStateDescription usbredir_device_info_vmstate
= {
1833 .name
= "usb-redir-device-info",
1835 .minimum_version_id
= 1,
1836 .fields
= (VMStateField
[]) {
1837 VMSTATE_UINT8(speed
, struct usb_redir_device_connect_header
),
1838 VMSTATE_UINT8(device_class
, struct usb_redir_device_connect_header
),
1839 VMSTATE_UINT8(device_subclass
, struct usb_redir_device_connect_header
),
1840 VMSTATE_UINT8(device_protocol
, struct usb_redir_device_connect_header
),
1841 VMSTATE_UINT16(vendor_id
, struct usb_redir_device_connect_header
),
1842 VMSTATE_UINT16(product_id
, struct usb_redir_device_connect_header
),
1843 VMSTATE_UINT16(device_version_bcd
,
1844 struct usb_redir_device_connect_header
),
1845 VMSTATE_END_OF_LIST()
1850 /* For usb_redir_interface_info_header migration */
1851 static const VMStateDescription usbredir_interface_info_vmstate
= {
1852 .name
= "usb-redir-interface-info",
1854 .minimum_version_id
= 1,
1855 .fields
= (VMStateField
[]) {
1856 VMSTATE_UINT32(interface_count
,
1857 struct usb_redir_interface_info_header
),
1858 VMSTATE_UINT8_ARRAY(interface
,
1859 struct usb_redir_interface_info_header
, 32),
1860 VMSTATE_UINT8_ARRAY(interface_class
,
1861 struct usb_redir_interface_info_header
, 32),
1862 VMSTATE_UINT8_ARRAY(interface_subclass
,
1863 struct usb_redir_interface_info_header
, 32),
1864 VMSTATE_UINT8_ARRAY(interface_protocol
,
1865 struct usb_redir_interface_info_header
, 32),
1866 VMSTATE_END_OF_LIST()
1871 /* And finally the USBRedirDevice vmstate itself */
1872 static const VMStateDescription usbredir_vmstate
= {
1873 .name
= "usb-redir",
1875 .minimum_version_id
= 1,
1876 .pre_save
= usbredir_pre_save
,
1877 .post_load
= usbredir_post_load
,
1878 .fields
= (VMStateField
[]) {
1879 VMSTATE_USB_DEVICE(dev
, USBRedirDevice
),
1880 VMSTATE_TIMER(attach_timer
, USBRedirDevice
),
1884 .field_exists
= NULL
,
1886 .info
= &usbredir_parser_vmstate_info
,
1887 .flags
= VMS_SINGLE
,
1890 VMSTATE_STRUCT_ARRAY(endpoint
, USBRedirDevice
, MAX_ENDPOINTS
, 1,
1891 usbredir_ep_vmstate
, struct endp_data
),
1892 VMSTATE_STRUCT(cancelled
, USBRedirDevice
, 1,
1893 usbredir_ep_packet_id_queue_vmstate
,
1894 struct PacketIdQueue
),
1895 VMSTATE_STRUCT(already_in_flight
, USBRedirDevice
, 1,
1896 usbredir_ep_packet_id_queue_vmstate
,
1897 struct PacketIdQueue
),
1898 VMSTATE_STRUCT(device_info
, USBRedirDevice
, 1,
1899 usbredir_device_info_vmstate
,
1900 struct usb_redir_device_connect_header
),
1901 VMSTATE_STRUCT(interface_info
, USBRedirDevice
, 1,
1902 usbredir_interface_info_vmstate
,
1903 struct usb_redir_interface_info_header
),
1904 VMSTATE_END_OF_LIST()
1908 static Property usbredir_properties
[] = {
1909 DEFINE_PROP_CHR("chardev", USBRedirDevice
, cs
),
1910 DEFINE_PROP_UINT8("debug", USBRedirDevice
, debug
, 0),
1911 DEFINE_PROP_STRING("filter", USBRedirDevice
, filter_str
),
1912 DEFINE_PROP_INT32("bootindex", USBRedirDevice
, bootindex
, -1),
1913 DEFINE_PROP_END_OF_LIST(),
1916 static void usbredir_class_initfn(ObjectClass
*klass
, void *data
)
1918 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1919 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1921 uc
->init
= usbredir_initfn
;
1922 uc
->product_desc
= "USB Redirection Device";
1923 uc
->handle_destroy
= usbredir_handle_destroy
;
1924 uc
->cancel_packet
= usbredir_cancel_packet
;
1925 uc
->handle_reset
= usbredir_handle_reset
;
1926 uc
->handle_data
= usbredir_handle_data
;
1927 uc
->handle_control
= usbredir_handle_control
;
1928 uc
->flush_ep_queue
= usbredir_flush_ep_queue
;
1929 dc
->vmsd
= &usbredir_vmstate
;
1930 dc
->props
= usbredir_properties
;
1933 static TypeInfo usbredir_dev_info
= {
1934 .name
= "usb-redir",
1935 .parent
= TYPE_USB_DEVICE
,
1936 .instance_size
= sizeof(USBRedirDevice
),
1937 .class_init
= usbredir_class_initfn
,
1940 static void usbredir_register_types(void)
1942 type_register_static(&usbredir_dev_info
);
1945 type_init(usbredir_register_types
)