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"
34 #include <sys/ioctl.h>
36 #include <usbredirparser.h>
37 #include <usbredirfilter.h>
41 #define MAX_ENDPOINTS 32
42 #define NO_INTERFACE_INFO 255 /* Valid interface_count always <= 32 */
43 #define EP2I(ep_address) (((ep_address & 0x80) >> 3) | (ep_address & 0x0f))
44 #define I2EP(i) (((i & 0x10) << 3) | (i & 0x0f))
46 typedef struct USBRedirDevice USBRedirDevice
;
48 /* Struct to hold buffered packets (iso or int input packets) */
53 QTAILQ_ENTRY(buf_packet
)next
;
59 uint8_t interface
; /* bInterfaceNumber this ep belongs to */
61 uint8_t iso_error
; /* For reporting iso errors to the HC */
62 uint8_t interrupt_started
;
63 uint8_t interrupt_error
;
64 uint8_t bufpq_prefilled
;
65 uint8_t bufpq_dropping_packets
;
66 QTAILQ_HEAD(, buf_packet
) bufpq
;
68 int bufpq_target_size
;
71 struct PacketIdQueueEntry
{
73 QTAILQ_ENTRY(PacketIdQueueEntry
)next
;
76 struct PacketIdQueue
{
79 QTAILQ_HEAD(, PacketIdQueueEntry
) head
;
83 struct USBRedirDevice
{
90 /* Data passed from chardev the fd_read cb to the usbredirparser read cb */
91 const uint8_t *read_buf
;
93 /* For async handling of close */
94 QEMUBH
*chardev_close_bh
;
95 /* To delay the usb attach in case of quick chardev close + open */
96 QEMUTimer
*attach_timer
;
97 int64_t next_attach_time
;
98 struct usbredirparser
*parser
;
99 struct endp_data endpoint
[MAX_ENDPOINTS
];
100 struct PacketIdQueue cancelled
;
101 struct PacketIdQueue already_in_flight
;
102 /* Data for device filtering */
103 struct usb_redir_device_connect_header device_info
;
104 struct usb_redir_interface_info_header interface_info
;
105 struct usbredirfilter_rule
*filter_rules
;
106 int filter_rules_count
;
109 static void usbredir_hello(void *priv
, struct usb_redir_hello_header
*h
);
110 static void usbredir_device_connect(void *priv
,
111 struct usb_redir_device_connect_header
*device_connect
);
112 static void usbredir_device_disconnect(void *priv
);
113 static void usbredir_interface_info(void *priv
,
114 struct usb_redir_interface_info_header
*interface_info
);
115 static void usbredir_ep_info(void *priv
,
116 struct usb_redir_ep_info_header
*ep_info
);
117 static void usbredir_configuration_status(void *priv
, uint64_t id
,
118 struct usb_redir_configuration_status_header
*configuration_status
);
119 static void usbredir_alt_setting_status(void *priv
, uint64_t id
,
120 struct usb_redir_alt_setting_status_header
*alt_setting_status
);
121 static void usbredir_iso_stream_status(void *priv
, uint64_t id
,
122 struct usb_redir_iso_stream_status_header
*iso_stream_status
);
123 static void usbredir_interrupt_receiving_status(void *priv
, uint64_t id
,
124 struct usb_redir_interrupt_receiving_status_header
125 *interrupt_receiving_status
);
126 static void usbredir_bulk_streams_status(void *priv
, uint64_t id
,
127 struct usb_redir_bulk_streams_status_header
*bulk_streams_status
);
128 static void usbredir_control_packet(void *priv
, uint64_t id
,
129 struct usb_redir_control_packet_header
*control_packet
,
130 uint8_t *data
, int data_len
);
131 static void usbredir_bulk_packet(void *priv
, uint64_t id
,
132 struct usb_redir_bulk_packet_header
*bulk_packet
,
133 uint8_t *data
, int data_len
);
134 static void usbredir_iso_packet(void *priv
, uint64_t id
,
135 struct usb_redir_iso_packet_header
*iso_packet
,
136 uint8_t *data
, int data_len
);
137 static void usbredir_interrupt_packet(void *priv
, uint64_t id
,
138 struct usb_redir_interrupt_packet_header
*interrupt_header
,
139 uint8_t *data
, int data_len
);
141 static int usbredir_handle_status(USBRedirDevice
*dev
,
142 int status
, int actual_len
);
150 if (dev->debug >= usbredirparser_error) { \
151 error_report("usb-redir error: " __VA_ARGS__); \
154 #define WARNING(...) \
156 if (dev->debug >= usbredirparser_warning) { \
157 error_report("usb-redir warning: " __VA_ARGS__); \
162 if (dev->debug >= usbredirparser_info) { \
163 error_report("usb-redir: " __VA_ARGS__); \
166 #define DPRINTF(...) \
168 if (dev->debug >= usbredirparser_debug) { \
169 error_report("usb-redir: " __VA_ARGS__); \
172 #define DPRINTF2(...) \
174 if (dev->debug >= usbredirparser_debug_data) { \
175 error_report("usb-redir: " __VA_ARGS__); \
179 static void usbredir_log(void *priv
, int level
, const char *msg
)
181 USBRedirDevice
*dev
= priv
;
183 if (dev
->debug
< level
) {
187 error_report("%s", msg
);
190 static void usbredir_log_data(USBRedirDevice
*dev
, const char *desc
,
191 const uint8_t *data
, int len
)
195 if (dev
->debug
< usbredirparser_debug_data
) {
199 for (i
= 0; i
< len
; i
+= j
) {
202 n
= sprintf(buf
, "%s", desc
);
203 for (j
= 0; j
< 8 && i
+ j
< len
; j
++) {
204 n
+= sprintf(buf
+ n
, " %02X", data
[i
+ j
]);
206 error_report("%s", buf
);
211 * usbredirparser io functions
214 static int usbredir_read(void *priv
, uint8_t *data
, int count
)
216 USBRedirDevice
*dev
= priv
;
218 if (dev
->read_buf_size
< count
) {
219 count
= dev
->read_buf_size
;
222 memcpy(data
, dev
->read_buf
, count
);
224 dev
->read_buf_size
-= count
;
225 if (dev
->read_buf_size
) {
226 dev
->read_buf
+= count
;
228 dev
->read_buf
= NULL
;
234 static int usbredir_write(void *priv
, uint8_t *data
, int count
)
236 USBRedirDevice
*dev
= priv
;
238 if (!dev
->cs
->opened
) {
242 return qemu_chr_fe_write(dev
->cs
, data
, count
);
246 * Cancelled and buffered packets helpers
249 static void packet_id_queue_init(struct PacketIdQueue
*q
,
250 USBRedirDevice
*dev
, const char *name
)
254 QTAILQ_INIT(&q
->head
);
258 static void packet_id_queue_add(struct PacketIdQueue
*q
, uint64_t id
)
260 USBRedirDevice
*dev
= q
->dev
;
261 struct PacketIdQueueEntry
*e
;
263 DPRINTF("adding packet id %"PRIu64
" to %s queue\n", id
, q
->name
);
265 e
= g_malloc0(sizeof(struct PacketIdQueueEntry
));
267 QTAILQ_INSERT_TAIL(&q
->head
, e
, next
);
271 static int packet_id_queue_remove(struct PacketIdQueue
*q
, uint64_t id
)
273 USBRedirDevice
*dev
= q
->dev
;
274 struct PacketIdQueueEntry
*e
;
276 QTAILQ_FOREACH(e
, &q
->head
, next
) {
278 DPRINTF("removing packet id %"PRIu64
" from %s queue\n",
280 QTAILQ_REMOVE(&q
->head
, e
, next
);
289 static void packet_id_queue_empty(struct PacketIdQueue
*q
)
291 USBRedirDevice
*dev
= q
->dev
;
292 struct PacketIdQueueEntry
*e
, *next_e
;
294 DPRINTF("removing %d packet-ids from %s queue\n", q
->size
, q
->name
);
296 QTAILQ_FOREACH_SAFE(e
, &q
->head
, next
, next_e
) {
297 QTAILQ_REMOVE(&q
->head
, e
, next
);
303 static void usbredir_cancel_packet(USBDevice
*udev
, USBPacket
*p
)
305 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
307 packet_id_queue_add(&dev
->cancelled
, p
->id
);
308 usbredirparser_send_cancel_data_packet(dev
->parser
, p
->id
);
309 usbredirparser_do_write(dev
->parser
);
312 static int usbredir_is_cancelled(USBRedirDevice
*dev
, uint64_t id
)
314 if (!dev
->dev
.attached
) {
315 return 1; /* Treat everything as cancelled after a disconnect */
317 return packet_id_queue_remove(&dev
->cancelled
, id
);
320 static void usbredir_fill_already_in_flight_from_ep(USBRedirDevice
*dev
,
321 struct USBEndpoint
*ep
)
325 QTAILQ_FOREACH(p
, &ep
->queue
, queue
) {
326 packet_id_queue_add(&dev
->already_in_flight
, p
->id
);
330 static void usbredir_fill_already_in_flight(USBRedirDevice
*dev
)
333 struct USBDevice
*udev
= &dev
->dev
;
335 usbredir_fill_already_in_flight_from_ep(dev
, &udev
->ep_ctl
);
337 for (ep
= 0; ep
< USB_MAX_ENDPOINTS
; ep
++) {
338 usbredir_fill_already_in_flight_from_ep(dev
, &udev
->ep_in
[ep
]);
339 usbredir_fill_already_in_flight_from_ep(dev
, &udev
->ep_out
[ep
]);
343 static int usbredir_already_in_flight(USBRedirDevice
*dev
, uint64_t id
)
345 return packet_id_queue_remove(&dev
->already_in_flight
, id
);
348 static USBPacket
*usbredir_find_packet_by_id(USBRedirDevice
*dev
,
349 uint8_t ep
, uint64_t id
)
353 if (usbredir_is_cancelled(dev
, id
)) {
357 p
= usb_ep_find_packet_by_id(&dev
->dev
,
358 (ep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
,
361 ERROR("could not find packet with id %"PRIu64
"\n", id
);
366 static void bufp_alloc(USBRedirDevice
*dev
,
367 uint8_t *data
, int len
, int status
, uint8_t ep
)
369 struct buf_packet
*bufp
;
371 if (!dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
&&
372 dev
->endpoint
[EP2I(ep
)].bufpq_size
>
373 2 * dev
->endpoint
[EP2I(ep
)].bufpq_target_size
) {
374 DPRINTF("bufpq overflow, dropping packets ep %02X\n", ep
);
375 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 1;
377 /* Since we're interupting the stream anyways, drop enough packets to get
378 back to our target buffer size */
379 if (dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
) {
380 if (dev
->endpoint
[EP2I(ep
)].bufpq_size
>
381 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
) {
385 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 0;
388 bufp
= g_malloc(sizeof(struct buf_packet
));
391 bufp
->status
= status
;
392 QTAILQ_INSERT_TAIL(&dev
->endpoint
[EP2I(ep
)].bufpq
, bufp
, next
);
393 dev
->endpoint
[EP2I(ep
)].bufpq_size
++;
396 static void bufp_free(USBRedirDevice
*dev
, struct buf_packet
*bufp
,
399 QTAILQ_REMOVE(&dev
->endpoint
[EP2I(ep
)].bufpq
, bufp
, next
);
400 dev
->endpoint
[EP2I(ep
)].bufpq_size
--;
405 static void usbredir_free_bufpq(USBRedirDevice
*dev
, uint8_t ep
)
407 struct buf_packet
*buf
, *buf_next
;
409 QTAILQ_FOREACH_SAFE(buf
, &dev
->endpoint
[EP2I(ep
)].bufpq
, next
, buf_next
) {
410 bufp_free(dev
, buf
, ep
);
415 * USBDevice callbacks
418 static void usbredir_handle_reset(USBDevice
*udev
)
420 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
422 DPRINTF("reset device\n");
423 usbredirparser_send_reset(dev
->parser
);
424 usbredirparser_do_write(dev
->parser
);
427 static int usbredir_handle_iso_data(USBRedirDevice
*dev
, USBPacket
*p
,
431 if (!dev
->endpoint
[EP2I(ep
)].iso_started
&&
432 !dev
->endpoint
[EP2I(ep
)].iso_error
) {
433 struct usb_redir_start_iso_stream_header start_iso
= {
438 if (dev
->dev
.speed
== USB_SPEED_HIGH
) {
439 pkts_per_sec
= 8000 / dev
->endpoint
[EP2I(ep
)].interval
;
441 pkts_per_sec
= 1000 / dev
->endpoint
[EP2I(ep
)].interval
;
443 /* Testing has shown that we need circa 60 ms buffer */
444 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
= (pkts_per_sec
* 60) / 1000;
446 /* Aim for approx 100 interrupts / second on the client to
447 balance latency and interrupt load */
448 start_iso
.pkts_per_urb
= pkts_per_sec
/ 100;
449 if (start_iso
.pkts_per_urb
< 1) {
450 start_iso
.pkts_per_urb
= 1;
451 } else if (start_iso
.pkts_per_urb
> 32) {
452 start_iso
.pkts_per_urb
= 32;
455 start_iso
.no_urbs
= (dev
->endpoint
[EP2I(ep
)].bufpq_target_size
+
456 start_iso
.pkts_per_urb
- 1) /
457 start_iso
.pkts_per_urb
;
458 /* Output endpoints pre-fill only 1/2 of the packets, keeping the rest
459 as overflow buffer. Also see the usbredir protocol documentation */
460 if (!(ep
& USB_DIR_IN
)) {
461 start_iso
.no_urbs
*= 2;
463 if (start_iso
.no_urbs
> 16) {
464 start_iso
.no_urbs
= 16;
467 /* No id, we look at the ep when receiving a status back */
468 usbredirparser_send_start_iso_stream(dev
->parser
, 0, &start_iso
);
469 usbredirparser_do_write(dev
->parser
);
470 DPRINTF("iso stream started pkts/sec %d pkts/urb %d urbs %d ep %02X\n",
471 pkts_per_sec
, start_iso
.pkts_per_urb
, start_iso
.no_urbs
, ep
);
472 dev
->endpoint
[EP2I(ep
)].iso_started
= 1;
473 dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
= 0;
474 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 0;
477 if (ep
& USB_DIR_IN
) {
478 struct buf_packet
*isop
;
480 if (dev
->endpoint
[EP2I(ep
)].iso_started
&&
481 !dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
) {
482 if (dev
->endpoint
[EP2I(ep
)].bufpq_size
<
483 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
) {
484 return usbredir_handle_status(dev
, 0, 0);
486 dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
= 1;
489 isop
= QTAILQ_FIRST(&dev
->endpoint
[EP2I(ep
)].bufpq
);
491 DPRINTF("iso-token-in ep %02X, no isop, iso_error: %d\n",
492 ep
, dev
->endpoint
[EP2I(ep
)].iso_error
);
493 /* Re-fill the buffer */
494 dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
= 0;
495 /* Check iso_error for stream errors, otherwise its an underrun */
496 status
= dev
->endpoint
[EP2I(ep
)].iso_error
;
497 dev
->endpoint
[EP2I(ep
)].iso_error
= 0;
498 return status
? USB_RET_IOERROR
: 0;
500 DPRINTF2("iso-token-in ep %02X status %d len %d queue-size: %d\n", ep
,
501 isop
->status
, isop
->len
, dev
->endpoint
[EP2I(ep
)].bufpq_size
);
503 status
= isop
->status
;
504 if (status
!= usb_redir_success
) {
505 bufp_free(dev
, isop
, ep
);
506 return USB_RET_IOERROR
;
510 if (len
> p
->iov
.size
) {
511 ERROR("received iso data is larger then packet ep %02X (%d > %d)\n",
512 ep
, len
, (int)p
->iov
.size
);
513 bufp_free(dev
, isop
, ep
);
514 return USB_RET_BABBLE
;
516 usb_packet_copy(p
, isop
->data
, len
);
517 bufp_free(dev
, isop
, ep
);
520 /* If the stream was not started because of a pending error don't
521 send the packet to the usb-host */
522 if (dev
->endpoint
[EP2I(ep
)].iso_started
) {
523 struct usb_redir_iso_packet_header iso_packet
= {
525 .length
= p
->iov
.size
527 uint8_t buf
[p
->iov
.size
];
528 /* No id, we look at the ep when receiving a status back */
529 usb_packet_copy(p
, buf
, p
->iov
.size
);
530 usbredirparser_send_iso_packet(dev
->parser
, 0, &iso_packet
,
532 usbredirparser_do_write(dev
->parser
);
534 status
= dev
->endpoint
[EP2I(ep
)].iso_error
;
535 dev
->endpoint
[EP2I(ep
)].iso_error
= 0;
536 DPRINTF2("iso-token-out ep %02X status %d len %zd\n", ep
, status
,
538 return usbredir_handle_status(dev
, status
, p
->iov
.size
);
542 static void usbredir_stop_iso_stream(USBRedirDevice
*dev
, uint8_t ep
)
544 struct usb_redir_stop_iso_stream_header stop_iso_stream
= {
547 if (dev
->endpoint
[EP2I(ep
)].iso_started
) {
548 usbredirparser_send_stop_iso_stream(dev
->parser
, 0, &stop_iso_stream
);
549 DPRINTF("iso stream stopped ep %02X\n", ep
);
550 dev
->endpoint
[EP2I(ep
)].iso_started
= 0;
552 dev
->endpoint
[EP2I(ep
)].iso_error
= 0;
553 usbredir_free_bufpq(dev
, ep
);
556 static int usbredir_handle_bulk_data(USBRedirDevice
*dev
, USBPacket
*p
,
559 struct usb_redir_bulk_packet_header bulk_packet
;
561 DPRINTF("bulk-out ep %02X len %zd id %"PRIu64
"\n", ep
, p
->iov
.size
, p
->id
);
563 if (usbredir_already_in_flight(dev
, p
->id
)) {
564 return USB_RET_ASYNC
;
567 bulk_packet
.endpoint
= ep
;
568 bulk_packet
.length
= p
->iov
.size
;
569 bulk_packet
.stream_id
= 0;
571 if (ep
& USB_DIR_IN
) {
572 usbredirparser_send_bulk_packet(dev
->parser
, p
->id
,
573 &bulk_packet
, NULL
, 0);
575 uint8_t buf
[p
->iov
.size
];
576 usb_packet_copy(p
, buf
, p
->iov
.size
);
577 usbredir_log_data(dev
, "bulk data out:", buf
, p
->iov
.size
);
578 usbredirparser_send_bulk_packet(dev
->parser
, p
->id
,
579 &bulk_packet
, buf
, p
->iov
.size
);
581 usbredirparser_do_write(dev
->parser
);
582 return USB_RET_ASYNC
;
585 static int usbredir_handle_interrupt_data(USBRedirDevice
*dev
,
586 USBPacket
*p
, uint8_t ep
)
588 if (ep
& USB_DIR_IN
) {
589 /* Input interrupt endpoint, buffered packet input */
590 struct buf_packet
*intp
;
593 if (!dev
->endpoint
[EP2I(ep
)].interrupt_started
&&
594 !dev
->endpoint
[EP2I(ep
)].interrupt_error
) {
595 struct usb_redir_start_interrupt_receiving_header start_int
= {
598 /* No id, we look at the ep when receiving a status back */
599 usbredirparser_send_start_interrupt_receiving(dev
->parser
, 0,
601 usbredirparser_do_write(dev
->parser
);
602 DPRINTF("interrupt recv started ep %02X\n", ep
);
603 dev
->endpoint
[EP2I(ep
)].interrupt_started
= 1;
604 /* We don't really want to drop interrupt packets ever, but
605 having some upper limit to how much we buffer is good. */
606 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
= 1000;
607 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 0;
610 intp
= QTAILQ_FIRST(&dev
->endpoint
[EP2I(ep
)].bufpq
);
612 DPRINTF2("interrupt-token-in ep %02X, no intp\n", ep
);
613 /* Check interrupt_error for stream errors */
614 status
= dev
->endpoint
[EP2I(ep
)].interrupt_error
;
615 dev
->endpoint
[EP2I(ep
)].interrupt_error
= 0;
617 return usbredir_handle_status(dev
, status
, 0);
621 DPRINTF("interrupt-token-in ep %02X status %d len %d\n", ep
,
622 intp
->status
, intp
->len
);
624 status
= intp
->status
;
625 if (status
!= usb_redir_success
) {
626 bufp_free(dev
, intp
, ep
);
627 return usbredir_handle_status(dev
, status
, 0);
631 if (len
> p
->iov
.size
) {
632 ERROR("received int data is larger then packet ep %02X\n", ep
);
633 bufp_free(dev
, intp
, ep
);
634 return USB_RET_BABBLE
;
636 usb_packet_copy(p
, intp
->data
, len
);
637 bufp_free(dev
, intp
, ep
);
640 /* Output interrupt endpoint, normal async operation */
641 struct usb_redir_interrupt_packet_header interrupt_packet
;
642 uint8_t buf
[p
->iov
.size
];
644 DPRINTF("interrupt-out ep %02X len %zd id %"PRIu64
"\n", ep
,
647 if (usbredir_already_in_flight(dev
, p
->id
)) {
648 return USB_RET_ASYNC
;
651 interrupt_packet
.endpoint
= ep
;
652 interrupt_packet
.length
= p
->iov
.size
;
654 usb_packet_copy(p
, buf
, p
->iov
.size
);
655 usbredir_log_data(dev
, "interrupt data out:", buf
, p
->iov
.size
);
656 usbredirparser_send_interrupt_packet(dev
->parser
, p
->id
,
657 &interrupt_packet
, buf
, p
->iov
.size
);
658 usbredirparser_do_write(dev
->parser
);
659 return USB_RET_ASYNC
;
663 static void usbredir_stop_interrupt_receiving(USBRedirDevice
*dev
,
666 struct usb_redir_stop_interrupt_receiving_header stop_interrupt_recv
= {
669 if (dev
->endpoint
[EP2I(ep
)].interrupt_started
) {
670 usbredirparser_send_stop_interrupt_receiving(dev
->parser
, 0,
671 &stop_interrupt_recv
);
672 DPRINTF("interrupt recv stopped ep %02X\n", ep
);
673 dev
->endpoint
[EP2I(ep
)].interrupt_started
= 0;
675 dev
->endpoint
[EP2I(ep
)].interrupt_error
= 0;
676 usbredir_free_bufpq(dev
, ep
);
679 static int usbredir_handle_data(USBDevice
*udev
, USBPacket
*p
)
681 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
685 if (p
->pid
== USB_TOKEN_IN
) {
689 switch (dev
->endpoint
[EP2I(ep
)].type
) {
690 case USB_ENDPOINT_XFER_CONTROL
:
691 ERROR("handle_data called for control transfer on ep %02X\n", ep
);
693 case USB_ENDPOINT_XFER_ISOC
:
694 return usbredir_handle_iso_data(dev
, p
, ep
);
695 case USB_ENDPOINT_XFER_BULK
:
696 return usbredir_handle_bulk_data(dev
, p
, ep
);
697 case USB_ENDPOINT_XFER_INT
:
698 return usbredir_handle_interrupt_data(dev
, p
, ep
);
700 ERROR("handle_data ep %02X has unknown type %d\n", ep
,
701 dev
->endpoint
[EP2I(ep
)].type
);
706 static int usbredir_set_config(USBRedirDevice
*dev
, USBPacket
*p
,
709 struct usb_redir_set_configuration_header set_config
;
712 DPRINTF("set config %d id %"PRIu64
"\n", config
, p
->id
);
714 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
715 switch (dev
->endpoint
[i
].type
) {
716 case USB_ENDPOINT_XFER_ISOC
:
717 usbredir_stop_iso_stream(dev
, I2EP(i
));
719 case USB_ENDPOINT_XFER_INT
:
721 usbredir_stop_interrupt_receiving(dev
, I2EP(i
));
725 usbredir_free_bufpq(dev
, I2EP(i
));
728 set_config
.configuration
= config
;
729 usbredirparser_send_set_configuration(dev
->parser
, p
->id
, &set_config
);
730 usbredirparser_do_write(dev
->parser
);
731 return USB_RET_ASYNC
;
734 static int usbredir_get_config(USBRedirDevice
*dev
, USBPacket
*p
)
736 DPRINTF("get config id %"PRIu64
"\n", p
->id
);
738 usbredirparser_send_get_configuration(dev
->parser
, p
->id
);
739 usbredirparser_do_write(dev
->parser
);
740 return USB_RET_ASYNC
;
743 static int usbredir_set_interface(USBRedirDevice
*dev
, USBPacket
*p
,
744 int interface
, int alt
)
746 struct usb_redir_set_alt_setting_header set_alt
;
749 DPRINTF("set interface %d alt %d id %"PRIu64
"\n", interface
, alt
, p
->id
);
751 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
752 if (dev
->endpoint
[i
].interface
== interface
) {
753 switch (dev
->endpoint
[i
].type
) {
754 case USB_ENDPOINT_XFER_ISOC
:
755 usbredir_stop_iso_stream(dev
, I2EP(i
));
757 case USB_ENDPOINT_XFER_INT
:
759 usbredir_stop_interrupt_receiving(dev
, I2EP(i
));
763 usbredir_free_bufpq(dev
, I2EP(i
));
767 set_alt
.interface
= interface
;
769 usbredirparser_send_set_alt_setting(dev
->parser
, p
->id
, &set_alt
);
770 usbredirparser_do_write(dev
->parser
);
771 return USB_RET_ASYNC
;
774 static int usbredir_get_interface(USBRedirDevice
*dev
, USBPacket
*p
,
777 struct usb_redir_get_alt_setting_header get_alt
;
779 DPRINTF("get interface %d id %"PRIu64
"\n", interface
, p
->id
);
781 get_alt
.interface
= interface
;
782 usbredirparser_send_get_alt_setting(dev
->parser
, p
->id
, &get_alt
);
783 usbredirparser_do_write(dev
->parser
);
784 return USB_RET_ASYNC
;
787 static int usbredir_handle_control(USBDevice
*udev
, USBPacket
*p
,
788 int request
, int value
, int index
, int length
, uint8_t *data
)
790 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
791 struct usb_redir_control_packet_header control_packet
;
793 if (usbredir_already_in_flight(dev
, p
->id
)) {
794 return USB_RET_ASYNC
;
797 /* Special cases for certain standard device requests */
799 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
800 DPRINTF("set address %d\n", value
);
801 dev
->dev
.addr
= value
;
803 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
804 return usbredir_set_config(dev
, p
, value
& 0xff);
805 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
806 return usbredir_get_config(dev
, p
);
807 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
808 return usbredir_set_interface(dev
, p
, index
, value
);
809 case InterfaceRequest
| USB_REQ_GET_INTERFACE
:
810 return usbredir_get_interface(dev
, p
, index
);
813 /* Normal ctrl requests, note request is (bRequestType << 8) | bRequest */
815 "ctrl-out type 0x%x req 0x%x val 0x%x index %d len %d id %"PRIu64
"\n",
816 request
>> 8, request
& 0xff, value
, index
, length
, p
->id
);
818 control_packet
.request
= request
& 0xFF;
819 control_packet
.requesttype
= request
>> 8;
820 control_packet
.endpoint
= control_packet
.requesttype
& USB_DIR_IN
;
821 control_packet
.value
= value
;
822 control_packet
.index
= index
;
823 control_packet
.length
= length
;
825 if (control_packet
.requesttype
& USB_DIR_IN
) {
826 usbredirparser_send_control_packet(dev
->parser
, p
->id
,
827 &control_packet
, NULL
, 0);
829 usbredir_log_data(dev
, "ctrl data out:", data
, length
);
830 usbredirparser_send_control_packet(dev
->parser
, p
->id
,
831 &control_packet
, data
, length
);
833 usbredirparser_do_write(dev
->parser
);
834 return USB_RET_ASYNC
;
838 * Close events can be triggered by usbredirparser_do_write which gets called
839 * from within the USBDevice data / control packet callbacks and doing a
840 * usb_detach from within these callbacks is not a good idea.
842 * So we use a bh handler to take care of close events.
844 static void usbredir_chardev_close_bh(void *opaque
)
846 USBRedirDevice
*dev
= opaque
;
848 usbredir_device_disconnect(dev
);
851 usbredirparser_destroy(dev
->parser
);
856 static void usbredir_chardev_open(USBRedirDevice
*dev
)
858 uint32_t caps
[USB_REDIR_CAPS_SIZE
] = { 0, };
861 /* Make sure any pending closes are handled (no-op if none pending) */
862 usbredir_chardev_close_bh(dev
);
863 qemu_bh_cancel(dev
->chardev_close_bh
);
865 strcpy(version
, "qemu usb-redir guest ");
866 pstrcat(version
, sizeof(version
), qemu_get_version());
868 dev
->parser
= qemu_oom_check(usbredirparser_create());
869 dev
->parser
->priv
= dev
;
870 dev
->parser
->log_func
= usbredir_log
;
871 dev
->parser
->read_func
= usbredir_read
;
872 dev
->parser
->write_func
= usbredir_write
;
873 dev
->parser
->hello_func
= usbredir_hello
;
874 dev
->parser
->device_connect_func
= usbredir_device_connect
;
875 dev
->parser
->device_disconnect_func
= usbredir_device_disconnect
;
876 dev
->parser
->interface_info_func
= usbredir_interface_info
;
877 dev
->parser
->ep_info_func
= usbredir_ep_info
;
878 dev
->parser
->configuration_status_func
= usbredir_configuration_status
;
879 dev
->parser
->alt_setting_status_func
= usbredir_alt_setting_status
;
880 dev
->parser
->iso_stream_status_func
= usbredir_iso_stream_status
;
881 dev
->parser
->interrupt_receiving_status_func
=
882 usbredir_interrupt_receiving_status
;
883 dev
->parser
->bulk_streams_status_func
= usbredir_bulk_streams_status
;
884 dev
->parser
->control_packet_func
= usbredir_control_packet
;
885 dev
->parser
->bulk_packet_func
= usbredir_bulk_packet
;
886 dev
->parser
->iso_packet_func
= usbredir_iso_packet
;
887 dev
->parser
->interrupt_packet_func
= usbredir_interrupt_packet
;
888 dev
->read_buf
= NULL
;
889 dev
->read_buf_size
= 0;
891 usbredirparser_caps_set_cap(caps
, usb_redir_cap_connect_device_version
);
892 usbredirparser_caps_set_cap(caps
, usb_redir_cap_filter
);
893 usbredirparser_caps_set_cap(caps
, usb_redir_cap_ep_info_max_packet_size
);
894 usbredirparser_caps_set_cap(caps
, usb_redir_cap_64bits_ids
);
895 usbredirparser_init(dev
->parser
, version
, caps
, USB_REDIR_CAPS_SIZE
, 0);
896 usbredirparser_do_write(dev
->parser
);
899 static void usbredir_reject_device(USBRedirDevice
*dev
)
901 usbredir_device_disconnect(dev
);
902 if (usbredirparser_peer_has_cap(dev
->parser
, usb_redir_cap_filter
)) {
903 usbredirparser_send_filter_reject(dev
->parser
);
904 usbredirparser_do_write(dev
->parser
);
908 static void usbredir_do_attach(void *opaque
)
910 USBRedirDevice
*dev
= opaque
;
912 /* In order to work properly with XHCI controllers we need these caps */
913 if ((dev
->dev
.port
->speedmask
& USB_SPEED_MASK_SUPER
) && !(
914 usbredirparser_peer_has_cap(dev
->parser
,
915 usb_redir_cap_ep_info_max_packet_size
) &&
916 usbredirparser_peer_has_cap(dev
->parser
,
917 usb_redir_cap_64bits_ids
))) {
918 ERROR("usb-redir-host lacks capabilities needed for use with XHCI\n");
919 usbredir_reject_device(dev
);
923 if (usb_device_attach(&dev
->dev
) != 0) {
924 usbredir_reject_device(dev
);
932 static int usbredir_chardev_can_read(void *opaque
)
934 USBRedirDevice
*dev
= opaque
;
937 WARNING("chardev_can_read called on non open chardev!\n");
941 /* usbredir_parser_do_read will consume *all* data we give it */
945 static void usbredir_chardev_read(void *opaque
, const uint8_t *buf
, int size
)
947 USBRedirDevice
*dev
= opaque
;
949 /* No recursion allowed! */
950 assert(dev
->read_buf
== NULL
);
953 dev
->read_buf_size
= size
;
955 usbredirparser_do_read(dev
->parser
);
956 /* Send any acks, etc. which may be queued now */
957 usbredirparser_do_write(dev
->parser
);
960 static void usbredir_chardev_event(void *opaque
, int event
)
962 USBRedirDevice
*dev
= opaque
;
965 case CHR_EVENT_OPENED
:
966 usbredir_chardev_open(dev
);
968 case CHR_EVENT_CLOSED
:
969 qemu_bh_schedule(dev
->chardev_close_bh
);
978 static int usbredir_initfn(USBDevice
*udev
)
980 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
983 if (dev
->cs
== NULL
) {
984 qerror_report(QERR_MISSING_PARAMETER
, "chardev");
988 if (dev
->filter_str
) {
989 i
= usbredirfilter_string_to_rules(dev
->filter_str
, ":", "|",
991 &dev
->filter_rules_count
);
993 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "filter",
994 "a usb device filter string");
999 dev
->chardev_close_bh
= qemu_bh_new(usbredir_chardev_close_bh
, dev
);
1000 dev
->attach_timer
= qemu_new_timer_ms(vm_clock
, usbredir_do_attach
, dev
);
1002 packet_id_queue_init(&dev
->cancelled
, dev
, "cancelled");
1003 packet_id_queue_init(&dev
->already_in_flight
, dev
, "already-in-flight");
1004 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1005 QTAILQ_INIT(&dev
->endpoint
[i
].bufpq
);
1008 /* We'll do the attach once we receive the speed from the usb-host */
1009 udev
->auto_attach
= 0;
1011 /* Let the backend know we are ready */
1012 qemu_chr_fe_open(dev
->cs
);
1013 qemu_chr_add_handlers(dev
->cs
, usbredir_chardev_can_read
,
1014 usbredir_chardev_read
, usbredir_chardev_event
, dev
);
1016 add_boot_device_path(dev
->bootindex
, &udev
->qdev
, NULL
);
1020 static void usbredir_cleanup_device_queues(USBRedirDevice
*dev
)
1024 packet_id_queue_empty(&dev
->cancelled
);
1025 packet_id_queue_empty(&dev
->already_in_flight
);
1026 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1027 usbredir_free_bufpq(dev
, I2EP(i
));
1031 static void usbredir_handle_destroy(USBDevice
*udev
)
1033 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
1035 qemu_chr_fe_close(dev
->cs
);
1036 qemu_chr_delete(dev
->cs
);
1037 /* Note must be done after qemu_chr_close, as that causes a close event */
1038 qemu_bh_delete(dev
->chardev_close_bh
);
1040 qemu_del_timer(dev
->attach_timer
);
1041 qemu_free_timer(dev
->attach_timer
);
1043 usbredir_cleanup_device_queues(dev
);
1046 usbredirparser_destroy(dev
->parser
);
1049 free(dev
->filter_rules
);
1052 static int usbredir_check_filter(USBRedirDevice
*dev
)
1054 if (dev
->interface_info
.interface_count
== NO_INTERFACE_INFO
) {
1055 ERROR("No interface info for device\n");
1059 if (dev
->filter_rules
) {
1060 if (!usbredirparser_peer_has_cap(dev
->parser
,
1061 usb_redir_cap_connect_device_version
)) {
1062 ERROR("Device filter specified and peer does not have the "
1063 "connect_device_version capability\n");
1067 if (usbredirfilter_check(
1069 dev
->filter_rules_count
,
1070 dev
->device_info
.device_class
,
1071 dev
->device_info
.device_subclass
,
1072 dev
->device_info
.device_protocol
,
1073 dev
->interface_info
.interface_class
,
1074 dev
->interface_info
.interface_subclass
,
1075 dev
->interface_info
.interface_protocol
,
1076 dev
->interface_info
.interface_count
,
1077 dev
->device_info
.vendor_id
,
1078 dev
->device_info
.product_id
,
1079 dev
->device_info
.device_version_bcd
,
1088 usbredir_reject_device(dev
);
1093 * usbredirparser packet complete callbacks
1096 static int usbredir_handle_status(USBRedirDevice
*dev
,
1097 int status
, int actual_len
)
1100 case usb_redir_success
:
1102 case usb_redir_stall
:
1103 return USB_RET_STALL
;
1104 case usb_redir_cancelled
:
1106 * When the usbredir-host unredirects a device, it will report a status
1107 * of cancelled for all pending packets, followed by a disconnect msg.
1109 return USB_RET_IOERROR
;
1110 case usb_redir_inval
:
1111 WARNING("got invalid param error from usb-host?\n");
1112 return USB_RET_IOERROR
;
1113 case usb_redir_babble
:
1114 return USB_RET_BABBLE
;
1115 case usb_redir_ioerror
:
1116 case usb_redir_timeout
:
1118 return USB_RET_IOERROR
;
1122 static void usbredir_hello(void *priv
, struct usb_redir_hello_header
*h
)
1124 USBRedirDevice
*dev
= priv
;
1126 /* Try to send the filter info now that we've the usb-host's caps */
1127 if (usbredirparser_peer_has_cap(dev
->parser
, usb_redir_cap_filter
) &&
1128 dev
->filter_rules
) {
1129 usbredirparser_send_filter_filter(dev
->parser
, dev
->filter_rules
,
1130 dev
->filter_rules_count
);
1131 usbredirparser_do_write(dev
->parser
);
1135 static void usbredir_device_connect(void *priv
,
1136 struct usb_redir_device_connect_header
*device_connect
)
1138 USBRedirDevice
*dev
= priv
;
1141 if (qemu_timer_pending(dev
->attach_timer
) || dev
->dev
.attached
) {
1142 ERROR("Received device connect while already connected\n");
1146 switch (device_connect
->speed
) {
1147 case usb_redir_speed_low
:
1148 speed
= "low speed";
1149 dev
->dev
.speed
= USB_SPEED_LOW
;
1151 case usb_redir_speed_full
:
1152 speed
= "full speed";
1153 dev
->dev
.speed
= USB_SPEED_FULL
;
1155 case usb_redir_speed_high
:
1156 speed
= "high speed";
1157 dev
->dev
.speed
= USB_SPEED_HIGH
;
1159 case usb_redir_speed_super
:
1160 speed
= "super speed";
1161 dev
->dev
.speed
= USB_SPEED_SUPER
;
1164 speed
= "unknown speed";
1165 dev
->dev
.speed
= USB_SPEED_FULL
;
1168 if (usbredirparser_peer_has_cap(dev
->parser
,
1169 usb_redir_cap_connect_device_version
)) {
1170 INFO("attaching %s device %04x:%04x version %d.%d class %02x\n",
1171 speed
, device_connect
->vendor_id
, device_connect
->product_id
,
1172 ((device_connect
->device_version_bcd
& 0xf000) >> 12) * 10 +
1173 ((device_connect
->device_version_bcd
& 0x0f00) >> 8),
1174 ((device_connect
->device_version_bcd
& 0x00f0) >> 4) * 10 +
1175 ((device_connect
->device_version_bcd
& 0x000f) >> 0),
1176 device_connect
->device_class
);
1178 INFO("attaching %s device %04x:%04x class %02x\n", speed
,
1179 device_connect
->vendor_id
, device_connect
->product_id
,
1180 device_connect
->device_class
);
1183 dev
->dev
.speedmask
= (1 << dev
->dev
.speed
);
1184 dev
->device_info
= *device_connect
;
1186 if (usbredir_check_filter(dev
)) {
1187 WARNING("Device %04x:%04x rejected by device filter, not attaching\n",
1188 device_connect
->vendor_id
, device_connect
->product_id
);
1192 qemu_mod_timer(dev
->attach_timer
, dev
->next_attach_time
);
1195 static void usbredir_device_disconnect(void *priv
)
1197 USBRedirDevice
*dev
= priv
;
1200 /* Stop any pending attaches */
1201 qemu_del_timer(dev
->attach_timer
);
1203 if (dev
->dev
.attached
) {
1204 usb_device_detach(&dev
->dev
);
1206 * Delay next usb device attach to give the guest a chance to see
1207 * see the detach / attach in case of quick close / open succession
1209 dev
->next_attach_time
= qemu_get_clock_ms(vm_clock
) + 200;
1212 /* Reset state so that the next dev connected starts with a clean slate */
1213 usbredir_cleanup_device_queues(dev
);
1214 memset(dev
->endpoint
, 0, sizeof(dev
->endpoint
));
1215 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1216 QTAILQ_INIT(&dev
->endpoint
[i
].bufpq
);
1218 usb_ep_init(&dev
->dev
);
1219 dev
->interface_info
.interface_count
= NO_INTERFACE_INFO
;
1224 static void usbredir_interface_info(void *priv
,
1225 struct usb_redir_interface_info_header
*interface_info
)
1227 USBRedirDevice
*dev
= priv
;
1229 dev
->interface_info
= *interface_info
;
1232 * If we receive interface info after the device has already been
1233 * connected (ie on a set_config), re-check the filter.
1235 if (qemu_timer_pending(dev
->attach_timer
) || dev
->dev
.attached
) {
1236 if (usbredir_check_filter(dev
)) {
1237 ERROR("Device no longer matches filter after interface info "
1238 "change, disconnecting!\n");
1243 static void usbredir_ep_info(void *priv
,
1244 struct usb_redir_ep_info_header
*ep_info
)
1246 USBRedirDevice
*dev
= priv
;
1247 struct USBEndpoint
*usb_ep
;
1250 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1251 dev
->endpoint
[i
].type
= ep_info
->type
[i
];
1252 dev
->endpoint
[i
].interval
= ep_info
->interval
[i
];
1253 dev
->endpoint
[i
].interface
= ep_info
->interface
[i
];
1254 switch (dev
->endpoint
[i
].type
) {
1255 case usb_redir_type_invalid
:
1257 case usb_redir_type_iso
:
1258 case usb_redir_type_interrupt
:
1259 if (dev
->endpoint
[i
].interval
== 0) {
1260 ERROR("Received 0 interval for isoc or irq endpoint\n");
1261 usbredir_device_disconnect(dev
);
1264 case usb_redir_type_control
:
1265 case usb_redir_type_bulk
:
1266 DPRINTF("ep: %02X type: %d interface: %d\n", I2EP(i
),
1267 dev
->endpoint
[i
].type
, dev
->endpoint
[i
].interface
);
1270 ERROR("Received invalid endpoint type\n");
1271 usbredir_device_disconnect(dev
);
1274 usb_ep
= usb_ep_get(&dev
->dev
,
1275 (i
& 0x10) ? USB_TOKEN_IN
: USB_TOKEN_OUT
,
1277 usb_ep
->type
= dev
->endpoint
[i
].type
;
1278 usb_ep
->ifnum
= dev
->endpoint
[i
].interface
;
1279 if (usbredirparser_peer_has_cap(dev
->parser
,
1280 usb_redir_cap_ep_info_max_packet_size
)) {
1281 usb_ep
->max_packet_size
= ep_info
->max_packet_size
[i
];
1283 if (ep_info
->type
[i
] == usb_redir_type_bulk
) {
1284 usb_ep
->pipeline
= true;
1289 static void usbredir_configuration_status(void *priv
, uint64_t id
,
1290 struct usb_redir_configuration_status_header
*config_status
)
1292 USBRedirDevice
*dev
= priv
;
1296 DPRINTF("set config status %d config %d id %"PRIu64
"\n",
1297 config_status
->status
, config_status
->configuration
, id
);
1299 p
= usbredir_find_packet_by_id(dev
, 0, id
);
1301 if (dev
->dev
.setup_buf
[0] & USB_DIR_IN
) {
1302 dev
->dev
.data_buf
[0] = config_status
->configuration
;
1305 p
->result
= usbredir_handle_status(dev
, config_status
->status
, len
);
1306 usb_generic_async_ctrl_complete(&dev
->dev
, p
);
1310 static void usbredir_alt_setting_status(void *priv
, uint64_t id
,
1311 struct usb_redir_alt_setting_status_header
*alt_setting_status
)
1313 USBRedirDevice
*dev
= priv
;
1317 DPRINTF("alt status %d intf %d alt %d id: %"PRIu64
"\n",
1318 alt_setting_status
->status
, alt_setting_status
->interface
,
1319 alt_setting_status
->alt
, id
);
1321 p
= usbredir_find_packet_by_id(dev
, 0, id
);
1323 if (dev
->dev
.setup_buf
[0] & USB_DIR_IN
) {
1324 dev
->dev
.data_buf
[0] = alt_setting_status
->alt
;
1328 usbredir_handle_status(dev
, alt_setting_status
->status
, len
);
1329 usb_generic_async_ctrl_complete(&dev
->dev
, p
);
1333 static void usbredir_iso_stream_status(void *priv
, uint64_t id
,
1334 struct usb_redir_iso_stream_status_header
*iso_stream_status
)
1336 USBRedirDevice
*dev
= priv
;
1337 uint8_t ep
= iso_stream_status
->endpoint
;
1339 DPRINTF("iso status %d ep %02X id %"PRIu64
"\n", iso_stream_status
->status
,
1342 if (!dev
->dev
.attached
|| !dev
->endpoint
[EP2I(ep
)].iso_started
) {
1346 dev
->endpoint
[EP2I(ep
)].iso_error
= iso_stream_status
->status
;
1347 if (iso_stream_status
->status
== usb_redir_stall
) {
1348 DPRINTF("iso stream stopped by peer ep %02X\n", ep
);
1349 dev
->endpoint
[EP2I(ep
)].iso_started
= 0;
1353 static void usbredir_interrupt_receiving_status(void *priv
, uint64_t id
,
1354 struct usb_redir_interrupt_receiving_status_header
1355 *interrupt_receiving_status
)
1357 USBRedirDevice
*dev
= priv
;
1358 uint8_t ep
= interrupt_receiving_status
->endpoint
;
1360 DPRINTF("interrupt recv status %d ep %02X id %"PRIu64
"\n",
1361 interrupt_receiving_status
->status
, ep
, id
);
1363 if (!dev
->dev
.attached
|| !dev
->endpoint
[EP2I(ep
)].interrupt_started
) {
1367 dev
->endpoint
[EP2I(ep
)].interrupt_error
=
1368 interrupt_receiving_status
->status
;
1369 if (interrupt_receiving_status
->status
== usb_redir_stall
) {
1370 DPRINTF("interrupt receiving stopped by peer ep %02X\n", ep
);
1371 dev
->endpoint
[EP2I(ep
)].interrupt_started
= 0;
1375 static void usbredir_bulk_streams_status(void *priv
, uint64_t id
,
1376 struct usb_redir_bulk_streams_status_header
*bulk_streams_status
)
1380 static void usbredir_control_packet(void *priv
, uint64_t id
,
1381 struct usb_redir_control_packet_header
*control_packet
,
1382 uint8_t *data
, int data_len
)
1384 USBRedirDevice
*dev
= priv
;
1386 int len
= control_packet
->length
;
1388 DPRINTF("ctrl-in status %d len %d id %"PRIu64
"\n", control_packet
->status
,
1391 p
= usbredir_find_packet_by_id(dev
, 0, id
);
1393 len
= usbredir_handle_status(dev
, control_packet
->status
, len
);
1395 usbredir_log_data(dev
, "ctrl data in:", data
, data_len
);
1396 if (data_len
<= sizeof(dev
->dev
.data_buf
)) {
1397 memcpy(dev
->dev
.data_buf
, data
, data_len
);
1399 ERROR("ctrl buffer too small (%d > %zu)\n",
1400 data_len
, sizeof(dev
->dev
.data_buf
));
1401 len
= USB_RET_STALL
;
1405 usb_generic_async_ctrl_complete(&dev
->dev
, p
);
1410 static void usbredir_bulk_packet(void *priv
, uint64_t id
,
1411 struct usb_redir_bulk_packet_header
*bulk_packet
,
1412 uint8_t *data
, int data_len
)
1414 USBRedirDevice
*dev
= priv
;
1415 uint8_t ep
= bulk_packet
->endpoint
;
1416 int len
= bulk_packet
->length
;
1419 DPRINTF("bulk-in status %d ep %02X len %d id %"PRIu64
"\n",
1420 bulk_packet
->status
, ep
, len
, id
);
1422 p
= usbredir_find_packet_by_id(dev
, ep
, id
);
1424 len
= usbredir_handle_status(dev
, bulk_packet
->status
, len
);
1426 usbredir_log_data(dev
, "bulk data in:", data
, data_len
);
1427 if (data_len
<= p
->iov
.size
) {
1428 usb_packet_copy(p
, data
, data_len
);
1430 ERROR("bulk got more data then requested (%d > %zd)\n",
1431 data_len
, p
->iov
.size
);
1432 len
= USB_RET_BABBLE
;
1436 usb_packet_complete(&dev
->dev
, p
);
1441 static void usbredir_iso_packet(void *priv
, uint64_t id
,
1442 struct usb_redir_iso_packet_header
*iso_packet
,
1443 uint8_t *data
, int data_len
)
1445 USBRedirDevice
*dev
= priv
;
1446 uint8_t ep
= iso_packet
->endpoint
;
1448 DPRINTF2("iso-in status %d ep %02X len %d id %"PRIu64
"\n",
1449 iso_packet
->status
, ep
, data_len
, id
);
1451 if (dev
->endpoint
[EP2I(ep
)].type
!= USB_ENDPOINT_XFER_ISOC
) {
1452 ERROR("received iso packet for non iso endpoint %02X\n", ep
);
1457 if (dev
->endpoint
[EP2I(ep
)].iso_started
== 0) {
1458 DPRINTF("received iso packet for non started stream ep %02X\n", ep
);
1463 /* bufp_alloc also adds the packet to the ep queue */
1464 bufp_alloc(dev
, data
, data_len
, iso_packet
->status
, ep
);
1467 static void usbredir_interrupt_packet(void *priv
, uint64_t id
,
1468 struct usb_redir_interrupt_packet_header
*interrupt_packet
,
1469 uint8_t *data
, int data_len
)
1471 USBRedirDevice
*dev
= priv
;
1472 uint8_t ep
= interrupt_packet
->endpoint
;
1474 DPRINTF("interrupt-in status %d ep %02X len %d id %"PRIu64
"\n",
1475 interrupt_packet
->status
, ep
, data_len
, id
);
1477 if (dev
->endpoint
[EP2I(ep
)].type
!= USB_ENDPOINT_XFER_INT
) {
1478 ERROR("received int packet for non interrupt endpoint %02X\n", ep
);
1483 if (ep
& USB_DIR_IN
) {
1484 if (dev
->endpoint
[EP2I(ep
)].interrupt_started
== 0) {
1485 DPRINTF("received int packet while not started ep %02X\n", ep
);
1490 /* bufp_alloc also adds the packet to the ep queue */
1491 bufp_alloc(dev
, data
, data_len
, interrupt_packet
->status
, ep
);
1493 int len
= interrupt_packet
->length
;
1495 USBPacket
*p
= usbredir_find_packet_by_id(dev
, ep
, id
);
1497 p
->result
= usbredir_handle_status(dev
,
1498 interrupt_packet
->status
, len
);
1499 usb_packet_complete(&dev
->dev
, p
);
1504 static Property usbredir_properties
[] = {
1505 DEFINE_PROP_CHR("chardev", USBRedirDevice
, cs
),
1506 DEFINE_PROP_UINT8("debug", USBRedirDevice
, debug
, 0),
1507 DEFINE_PROP_STRING("filter", USBRedirDevice
, filter_str
),
1508 DEFINE_PROP_INT32("bootindex", USBRedirDevice
, bootindex
, -1),
1509 DEFINE_PROP_END_OF_LIST(),
1512 static void usbredir_class_initfn(ObjectClass
*klass
, void *data
)
1514 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1515 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1517 uc
->init
= usbredir_initfn
;
1518 uc
->product_desc
= "USB Redirection Device";
1519 uc
->handle_destroy
= usbredir_handle_destroy
;
1520 uc
->cancel_packet
= usbredir_cancel_packet
;
1521 uc
->handle_reset
= usbredir_handle_reset
;
1522 uc
->handle_data
= usbredir_handle_data
;
1523 uc
->handle_control
= usbredir_handle_control
;
1524 dc
->props
= usbredir_properties
;
1527 static TypeInfo usbredir_dev_info
= {
1528 .name
= "usb-redir",
1529 .parent
= TYPE_USB_DEVICE
,
1530 .instance_size
= sizeof(USBRedirDevice
),
1531 .class_init
= usbredir_class_initfn
,
1534 static void usbredir_register_types(void)
1536 type_register_static(&usbredir_dev_info
);
1539 type_init(usbredir_register_types
)