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 Cancelled Cancelled
;
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 */
62 uint8_t iso_error
; /* For reporting iso errors to the HC */
63 uint8_t interrupt_started
;
64 uint8_t interrupt_error
;
65 uint8_t bufpq_prefilled
;
66 uint8_t bufpq_dropping_packets
;
67 QTAILQ_HEAD(, buf_packet
) bufpq
;
69 int bufpq_target_size
;
72 struct USBRedirDevice
{
79 /* Data passed from chardev the fd_read cb to the usbredirparser read cb */
80 const uint8_t *read_buf
;
82 /* For async handling of close */
83 QEMUBH
*chardev_close_bh
;
84 /* To delay the usb attach in case of quick chardev close + open */
85 QEMUTimer
*attach_timer
;
86 int64_t next_attach_time
;
87 struct usbredirparser
*parser
;
88 struct endp_data endpoint
[MAX_ENDPOINTS
];
89 QTAILQ_HEAD(, Cancelled
) cancelled
;
90 /* Data for device filtering */
91 struct usb_redir_device_connect_header device_info
;
92 struct usb_redir_interface_info_header interface_info
;
93 struct usbredirfilter_rule
*filter_rules
;
94 int filter_rules_count
;
99 QTAILQ_ENTRY(Cancelled
)next
;
102 static void usbredir_hello(void *priv
, struct usb_redir_hello_header
*h
);
103 static void usbredir_device_connect(void *priv
,
104 struct usb_redir_device_connect_header
*device_connect
);
105 static void usbredir_device_disconnect(void *priv
);
106 static void usbredir_interface_info(void *priv
,
107 struct usb_redir_interface_info_header
*interface_info
);
108 static void usbredir_ep_info(void *priv
,
109 struct usb_redir_ep_info_header
*ep_info
);
110 static void usbredir_configuration_status(void *priv
, uint64_t id
,
111 struct usb_redir_configuration_status_header
*configuration_status
);
112 static void usbredir_alt_setting_status(void *priv
, uint64_t id
,
113 struct usb_redir_alt_setting_status_header
*alt_setting_status
);
114 static void usbredir_iso_stream_status(void *priv
, uint64_t id
,
115 struct usb_redir_iso_stream_status_header
*iso_stream_status
);
116 static void usbredir_interrupt_receiving_status(void *priv
, uint64_t id
,
117 struct usb_redir_interrupt_receiving_status_header
118 *interrupt_receiving_status
);
119 static void usbredir_bulk_streams_status(void *priv
, uint64_t id
,
120 struct usb_redir_bulk_streams_status_header
*bulk_streams_status
);
121 static void usbredir_control_packet(void *priv
, uint64_t id
,
122 struct usb_redir_control_packet_header
*control_packet
,
123 uint8_t *data
, int data_len
);
124 static void usbredir_bulk_packet(void *priv
, uint64_t id
,
125 struct usb_redir_bulk_packet_header
*bulk_packet
,
126 uint8_t *data
, int data_len
);
127 static void usbredir_iso_packet(void *priv
, uint64_t id
,
128 struct usb_redir_iso_packet_header
*iso_packet
,
129 uint8_t *data
, int data_len
);
130 static void usbredir_interrupt_packet(void *priv
, uint64_t id
,
131 struct usb_redir_interrupt_packet_header
*interrupt_header
,
132 uint8_t *data
, int data_len
);
134 static int usbredir_handle_status(USBRedirDevice
*dev
,
135 int status
, int actual_len
);
143 if (dev->debug >= usbredirparser_error) { \
144 error_report("usb-redir error: " __VA_ARGS__); \
147 #define WARNING(...) \
149 if (dev->debug >= usbredirparser_warning) { \
150 error_report("usb-redir warning: " __VA_ARGS__); \
155 if (dev->debug >= usbredirparser_info) { \
156 error_report("usb-redir: " __VA_ARGS__); \
159 #define DPRINTF(...) \
161 if (dev->debug >= usbredirparser_debug) { \
162 error_report("usb-redir: " __VA_ARGS__); \
165 #define DPRINTF2(...) \
167 if (dev->debug >= usbredirparser_debug_data) { \
168 error_report("usb-redir: " __VA_ARGS__); \
172 static void usbredir_log(void *priv
, int level
, const char *msg
)
174 USBRedirDevice
*dev
= priv
;
176 if (dev
->debug
< level
) {
180 error_report("%s", msg
);
183 static void usbredir_log_data(USBRedirDevice
*dev
, const char *desc
,
184 const uint8_t *data
, int len
)
188 if (dev
->debug
< usbredirparser_debug_data
) {
192 for (i
= 0; i
< len
; i
+= j
) {
195 n
= sprintf(buf
, "%s", desc
);
196 for (j
= 0; j
< 8 && i
+ j
< len
; j
++) {
197 n
+= sprintf(buf
+ n
, " %02X", data
[i
+ j
]);
199 error_report("%s", buf
);
204 * usbredirparser io functions
207 static int usbredir_read(void *priv
, uint8_t *data
, int count
)
209 USBRedirDevice
*dev
= priv
;
211 if (dev
->read_buf_size
< count
) {
212 count
= dev
->read_buf_size
;
215 memcpy(data
, dev
->read_buf
, count
);
217 dev
->read_buf_size
-= count
;
218 if (dev
->read_buf_size
) {
219 dev
->read_buf
+= count
;
221 dev
->read_buf
= NULL
;
227 static int usbredir_write(void *priv
, uint8_t *data
, int count
)
229 USBRedirDevice
*dev
= priv
;
231 if (!dev
->cs
->opened
) {
235 return qemu_chr_fe_write(dev
->cs
, data
, count
);
239 * Cancelled and buffered packets helpers
242 static void usbredir_cancel_packet(USBDevice
*udev
, USBPacket
*p
)
244 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
247 DPRINTF("cancel packet id %"PRIu64
"\n", p
->id
);
249 c
= g_malloc0(sizeof(Cancelled
));
251 QTAILQ_INSERT_TAIL(&dev
->cancelled
, c
, next
);
253 usbredirparser_send_cancel_data_packet(dev
->parser
, p
->id
);
254 usbredirparser_do_write(dev
->parser
);
257 static int usbredir_is_cancelled(USBRedirDevice
*dev
, uint64_t id
)
261 if (!dev
->dev
.attached
) {
262 return 1; /* Treat everything as cancelled after a disconnect */
265 QTAILQ_FOREACH(c
, &dev
->cancelled
, next
) {
267 QTAILQ_REMOVE(&dev
->cancelled
, c
, next
);
275 static USBPacket
*usbredir_find_packet_by_id(USBRedirDevice
*dev
,
276 uint8_t ep
, uint64_t id
)
280 if (usbredir_is_cancelled(dev
, id
)) {
284 p
= usb_ep_find_packet_by_id(&dev
->dev
,
285 (ep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
,
288 ERROR("could not find packet with id %"PRIu64
"\n", id
);
293 static void bufp_alloc(USBRedirDevice
*dev
,
294 uint8_t *data
, int len
, int status
, uint8_t ep
)
296 struct buf_packet
*bufp
;
298 if (!dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
&&
299 dev
->endpoint
[EP2I(ep
)].bufpq_size
>
300 2 * dev
->endpoint
[EP2I(ep
)].bufpq_target_size
) {
301 DPRINTF("bufpq overflow, dropping packets ep %02X\n", ep
);
302 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 1;
304 /* Since we're interupting the stream anyways, drop enough packets to get
305 back to our target buffer size */
306 if (dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
) {
307 if (dev
->endpoint
[EP2I(ep
)].bufpq_size
>
308 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
) {
312 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 0;
315 bufp
= g_malloc(sizeof(struct buf_packet
));
318 bufp
->status
= status
;
319 QTAILQ_INSERT_TAIL(&dev
->endpoint
[EP2I(ep
)].bufpq
, bufp
, next
);
320 dev
->endpoint
[EP2I(ep
)].bufpq_size
++;
323 static void bufp_free(USBRedirDevice
*dev
, struct buf_packet
*bufp
,
326 QTAILQ_REMOVE(&dev
->endpoint
[EP2I(ep
)].bufpq
, bufp
, next
);
327 dev
->endpoint
[EP2I(ep
)].bufpq_size
--;
332 static void usbredir_free_bufpq(USBRedirDevice
*dev
, uint8_t ep
)
334 struct buf_packet
*buf
, *buf_next
;
336 QTAILQ_FOREACH_SAFE(buf
, &dev
->endpoint
[EP2I(ep
)].bufpq
, next
, buf_next
) {
337 bufp_free(dev
, buf
, ep
);
342 * USBDevice callbacks
345 static void usbredir_handle_reset(USBDevice
*udev
)
347 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
349 DPRINTF("reset device\n");
350 usbredirparser_send_reset(dev
->parser
);
351 usbredirparser_do_write(dev
->parser
);
354 static int usbredir_handle_iso_data(USBRedirDevice
*dev
, USBPacket
*p
,
358 if (!dev
->endpoint
[EP2I(ep
)].iso_started
&&
359 !dev
->endpoint
[EP2I(ep
)].iso_error
) {
360 struct usb_redir_start_iso_stream_header start_iso
= {
365 if (dev
->dev
.speed
== USB_SPEED_HIGH
) {
366 pkts_per_sec
= 8000 / dev
->endpoint
[EP2I(ep
)].interval
;
368 pkts_per_sec
= 1000 / dev
->endpoint
[EP2I(ep
)].interval
;
370 /* Testing has shown that we need circa 60 ms buffer */
371 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
= (pkts_per_sec
* 60) / 1000;
373 /* Aim for approx 100 interrupts / second on the client to
374 balance latency and interrupt load */
375 start_iso
.pkts_per_urb
= pkts_per_sec
/ 100;
376 if (start_iso
.pkts_per_urb
< 1) {
377 start_iso
.pkts_per_urb
= 1;
378 } else if (start_iso
.pkts_per_urb
> 32) {
379 start_iso
.pkts_per_urb
= 32;
382 start_iso
.no_urbs
= (dev
->endpoint
[EP2I(ep
)].bufpq_target_size
+
383 start_iso
.pkts_per_urb
- 1) /
384 start_iso
.pkts_per_urb
;
385 /* Output endpoints pre-fill only 1/2 of the packets, keeping the rest
386 as overflow buffer. Also see the usbredir protocol documentation */
387 if (!(ep
& USB_DIR_IN
)) {
388 start_iso
.no_urbs
*= 2;
390 if (start_iso
.no_urbs
> 16) {
391 start_iso
.no_urbs
= 16;
394 /* No id, we look at the ep when receiving a status back */
395 usbredirparser_send_start_iso_stream(dev
->parser
, 0, &start_iso
);
396 usbredirparser_do_write(dev
->parser
);
397 DPRINTF("iso stream started pkts/sec %d pkts/urb %d urbs %d ep %02X\n",
398 pkts_per_sec
, start_iso
.pkts_per_urb
, start_iso
.no_urbs
, ep
);
399 dev
->endpoint
[EP2I(ep
)].iso_started
= 1;
400 dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
= 0;
401 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 0;
404 if (ep
& USB_DIR_IN
) {
405 struct buf_packet
*isop
;
407 if (dev
->endpoint
[EP2I(ep
)].iso_started
&&
408 !dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
) {
409 if (dev
->endpoint
[EP2I(ep
)].bufpq_size
<
410 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
) {
411 return usbredir_handle_status(dev
, 0, 0);
413 dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
= 1;
416 isop
= QTAILQ_FIRST(&dev
->endpoint
[EP2I(ep
)].bufpq
);
418 DPRINTF("iso-token-in ep %02X, no isop, iso_error: %d\n",
419 ep
, dev
->endpoint
[EP2I(ep
)].iso_error
);
420 /* Re-fill the buffer */
421 dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
= 0;
422 /* Check iso_error for stream errors, otherwise its an underrun */
423 status
= dev
->endpoint
[EP2I(ep
)].iso_error
;
424 dev
->endpoint
[EP2I(ep
)].iso_error
= 0;
425 return status
? USB_RET_IOERROR
: 0;
427 DPRINTF2("iso-token-in ep %02X status %d len %d queue-size: %d\n", ep
,
428 isop
->status
, isop
->len
, dev
->endpoint
[EP2I(ep
)].bufpq_size
);
430 status
= isop
->status
;
431 if (status
!= usb_redir_success
) {
432 bufp_free(dev
, isop
, ep
);
433 return USB_RET_IOERROR
;
437 if (len
> p
->iov
.size
) {
438 ERROR("received iso data is larger then packet ep %02X (%d > %d)\n",
439 ep
, len
, (int)p
->iov
.size
);
440 bufp_free(dev
, isop
, ep
);
441 return USB_RET_BABBLE
;
443 usb_packet_copy(p
, isop
->data
, len
);
444 bufp_free(dev
, isop
, ep
);
447 /* If the stream was not started because of a pending error don't
448 send the packet to the usb-host */
449 if (dev
->endpoint
[EP2I(ep
)].iso_started
) {
450 struct usb_redir_iso_packet_header iso_packet
= {
452 .length
= p
->iov
.size
454 uint8_t buf
[p
->iov
.size
];
455 /* No id, we look at the ep when receiving a status back */
456 usb_packet_copy(p
, buf
, p
->iov
.size
);
457 usbredirparser_send_iso_packet(dev
->parser
, 0, &iso_packet
,
459 usbredirparser_do_write(dev
->parser
);
461 status
= dev
->endpoint
[EP2I(ep
)].iso_error
;
462 dev
->endpoint
[EP2I(ep
)].iso_error
= 0;
463 DPRINTF2("iso-token-out ep %02X status %d len %zd\n", ep
, status
,
465 return usbredir_handle_status(dev
, status
, p
->iov
.size
);
469 static void usbredir_stop_iso_stream(USBRedirDevice
*dev
, uint8_t ep
)
471 struct usb_redir_stop_iso_stream_header stop_iso_stream
= {
474 if (dev
->endpoint
[EP2I(ep
)].iso_started
) {
475 usbredirparser_send_stop_iso_stream(dev
->parser
, 0, &stop_iso_stream
);
476 DPRINTF("iso stream stopped ep %02X\n", ep
);
477 dev
->endpoint
[EP2I(ep
)].iso_started
= 0;
479 dev
->endpoint
[EP2I(ep
)].iso_error
= 0;
480 usbredir_free_bufpq(dev
, ep
);
483 static int usbredir_handle_bulk_data(USBRedirDevice
*dev
, USBPacket
*p
,
486 struct usb_redir_bulk_packet_header bulk_packet
;
488 DPRINTF("bulk-out ep %02X len %zd id %"PRIu64
"\n", ep
, p
->iov
.size
, p
->id
);
490 bulk_packet
.endpoint
= ep
;
491 bulk_packet
.length
= p
->iov
.size
;
492 bulk_packet
.stream_id
= 0;
494 if (ep
& USB_DIR_IN
) {
495 usbredirparser_send_bulk_packet(dev
->parser
, p
->id
,
496 &bulk_packet
, NULL
, 0);
498 uint8_t buf
[p
->iov
.size
];
499 usb_packet_copy(p
, buf
, p
->iov
.size
);
500 usbredir_log_data(dev
, "bulk data out:", buf
, p
->iov
.size
);
501 usbredirparser_send_bulk_packet(dev
->parser
, p
->id
,
502 &bulk_packet
, buf
, p
->iov
.size
);
504 usbredirparser_do_write(dev
->parser
);
505 return USB_RET_ASYNC
;
508 static int usbredir_handle_interrupt_data(USBRedirDevice
*dev
,
509 USBPacket
*p
, uint8_t ep
)
511 if (ep
& USB_DIR_IN
) {
512 /* Input interrupt endpoint, buffered packet input */
513 struct buf_packet
*intp
;
516 if (!dev
->endpoint
[EP2I(ep
)].interrupt_started
&&
517 !dev
->endpoint
[EP2I(ep
)].interrupt_error
) {
518 struct usb_redir_start_interrupt_receiving_header start_int
= {
521 /* No id, we look at the ep when receiving a status back */
522 usbredirparser_send_start_interrupt_receiving(dev
->parser
, 0,
524 usbredirparser_do_write(dev
->parser
);
525 DPRINTF("interrupt recv started ep %02X\n", ep
);
526 dev
->endpoint
[EP2I(ep
)].interrupt_started
= 1;
527 /* We don't really want to drop interrupt packets ever, but
528 having some upper limit to how much we buffer is good. */
529 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
= 1000;
530 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 0;
533 intp
= QTAILQ_FIRST(&dev
->endpoint
[EP2I(ep
)].bufpq
);
535 DPRINTF2("interrupt-token-in ep %02X, no intp\n", ep
);
536 /* Check interrupt_error for stream errors */
537 status
= dev
->endpoint
[EP2I(ep
)].interrupt_error
;
538 dev
->endpoint
[EP2I(ep
)].interrupt_error
= 0;
540 return usbredir_handle_status(dev
, status
, 0);
544 DPRINTF("interrupt-token-in ep %02X status %d len %d\n", ep
,
545 intp
->status
, intp
->len
);
547 status
= intp
->status
;
548 if (status
!= usb_redir_success
) {
549 bufp_free(dev
, intp
, ep
);
550 return usbredir_handle_status(dev
, status
, 0);
554 if (len
> p
->iov
.size
) {
555 ERROR("received int data is larger then packet ep %02X\n", ep
);
556 bufp_free(dev
, intp
, ep
);
557 return USB_RET_BABBLE
;
559 usb_packet_copy(p
, intp
->data
, len
);
560 bufp_free(dev
, intp
, ep
);
563 /* Output interrupt endpoint, normal async operation */
564 struct usb_redir_interrupt_packet_header interrupt_packet
;
565 uint8_t buf
[p
->iov
.size
];
567 DPRINTF("interrupt-out ep %02X len %zd id %"PRIu64
"\n", ep
,
570 interrupt_packet
.endpoint
= ep
;
571 interrupt_packet
.length
= p
->iov
.size
;
573 usb_packet_copy(p
, buf
, p
->iov
.size
);
574 usbredir_log_data(dev
, "interrupt data out:", buf
, p
->iov
.size
);
575 usbredirparser_send_interrupt_packet(dev
->parser
, p
->id
,
576 &interrupt_packet
, buf
, p
->iov
.size
);
577 usbredirparser_do_write(dev
->parser
);
578 return USB_RET_ASYNC
;
582 static void usbredir_stop_interrupt_receiving(USBRedirDevice
*dev
,
585 struct usb_redir_stop_interrupt_receiving_header stop_interrupt_recv
= {
588 if (dev
->endpoint
[EP2I(ep
)].interrupt_started
) {
589 usbredirparser_send_stop_interrupt_receiving(dev
->parser
, 0,
590 &stop_interrupt_recv
);
591 DPRINTF("interrupt recv stopped ep %02X\n", ep
);
592 dev
->endpoint
[EP2I(ep
)].interrupt_started
= 0;
594 dev
->endpoint
[EP2I(ep
)].interrupt_error
= 0;
595 usbredir_free_bufpq(dev
, ep
);
598 static int usbredir_handle_data(USBDevice
*udev
, USBPacket
*p
)
600 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
604 if (p
->pid
== USB_TOKEN_IN
) {
608 switch (dev
->endpoint
[EP2I(ep
)].type
) {
609 case USB_ENDPOINT_XFER_CONTROL
:
610 ERROR("handle_data called for control transfer on ep %02X\n", ep
);
612 case USB_ENDPOINT_XFER_ISOC
:
613 return usbredir_handle_iso_data(dev
, p
, ep
);
614 case USB_ENDPOINT_XFER_BULK
:
615 return usbredir_handle_bulk_data(dev
, p
, ep
);
616 case USB_ENDPOINT_XFER_INT
:
617 return usbredir_handle_interrupt_data(dev
, p
, ep
);
619 ERROR("handle_data ep %02X has unknown type %d\n", ep
,
620 dev
->endpoint
[EP2I(ep
)].type
);
625 static int usbredir_set_config(USBRedirDevice
*dev
, USBPacket
*p
,
628 struct usb_redir_set_configuration_header set_config
;
631 DPRINTF("set config %d id %"PRIu64
"\n", config
, p
->id
);
633 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
634 switch (dev
->endpoint
[i
].type
) {
635 case USB_ENDPOINT_XFER_ISOC
:
636 usbredir_stop_iso_stream(dev
, I2EP(i
));
638 case USB_ENDPOINT_XFER_INT
:
640 usbredir_stop_interrupt_receiving(dev
, I2EP(i
));
644 usbredir_free_bufpq(dev
, I2EP(i
));
647 set_config
.configuration
= config
;
648 usbredirparser_send_set_configuration(dev
->parser
, p
->id
, &set_config
);
649 usbredirparser_do_write(dev
->parser
);
650 return USB_RET_ASYNC
;
653 static int usbredir_get_config(USBRedirDevice
*dev
, USBPacket
*p
)
655 DPRINTF("get config id %"PRIu64
"\n", p
->id
);
657 usbredirparser_send_get_configuration(dev
->parser
, p
->id
);
658 usbredirparser_do_write(dev
->parser
);
659 return USB_RET_ASYNC
;
662 static int usbredir_set_interface(USBRedirDevice
*dev
, USBPacket
*p
,
663 int interface
, int alt
)
665 struct usb_redir_set_alt_setting_header set_alt
;
668 DPRINTF("set interface %d alt %d id %"PRIu64
"\n", interface
, alt
, p
->id
);
670 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
671 if (dev
->endpoint
[i
].interface
== interface
) {
672 switch (dev
->endpoint
[i
].type
) {
673 case USB_ENDPOINT_XFER_ISOC
:
674 usbredir_stop_iso_stream(dev
, I2EP(i
));
676 case USB_ENDPOINT_XFER_INT
:
678 usbredir_stop_interrupt_receiving(dev
, I2EP(i
));
682 usbredir_free_bufpq(dev
, I2EP(i
));
686 set_alt
.interface
= interface
;
688 usbredirparser_send_set_alt_setting(dev
->parser
, p
->id
, &set_alt
);
689 usbredirparser_do_write(dev
->parser
);
690 return USB_RET_ASYNC
;
693 static int usbredir_get_interface(USBRedirDevice
*dev
, USBPacket
*p
,
696 struct usb_redir_get_alt_setting_header get_alt
;
698 DPRINTF("get interface %d id %"PRIu64
"\n", interface
, p
->id
);
700 get_alt
.interface
= interface
;
701 usbredirparser_send_get_alt_setting(dev
->parser
, p
->id
, &get_alt
);
702 usbredirparser_do_write(dev
->parser
);
703 return USB_RET_ASYNC
;
706 static int usbredir_handle_control(USBDevice
*udev
, USBPacket
*p
,
707 int request
, int value
, int index
, int length
, uint8_t *data
)
709 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
710 struct usb_redir_control_packet_header control_packet
;
712 /* Special cases for certain standard device requests */
714 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
715 DPRINTF("set address %d\n", value
);
716 dev
->dev
.addr
= value
;
718 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
719 return usbredir_set_config(dev
, p
, value
& 0xff);
720 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
721 return usbredir_get_config(dev
, p
);
722 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
723 return usbredir_set_interface(dev
, p
, index
, value
);
724 case InterfaceRequest
| USB_REQ_GET_INTERFACE
:
725 return usbredir_get_interface(dev
, p
, index
);
728 /* Normal ctrl requests, note request is (bRequestType << 8) | bRequest */
730 "ctrl-out type 0x%x req 0x%x val 0x%x index %d len %d id %"PRIu64
"\n",
731 request
>> 8, request
& 0xff, value
, index
, length
, p
->id
);
733 control_packet
.request
= request
& 0xFF;
734 control_packet
.requesttype
= request
>> 8;
735 control_packet
.endpoint
= control_packet
.requesttype
& USB_DIR_IN
;
736 control_packet
.value
= value
;
737 control_packet
.index
= index
;
738 control_packet
.length
= length
;
740 if (control_packet
.requesttype
& USB_DIR_IN
) {
741 usbredirparser_send_control_packet(dev
->parser
, p
->id
,
742 &control_packet
, NULL
, 0);
744 usbredir_log_data(dev
, "ctrl data out:", data
, length
);
745 usbredirparser_send_control_packet(dev
->parser
, p
->id
,
746 &control_packet
, data
, length
);
748 usbredirparser_do_write(dev
->parser
);
749 return USB_RET_ASYNC
;
753 * Close events can be triggered by usbredirparser_do_write which gets called
754 * from within the USBDevice data / control packet callbacks and doing a
755 * usb_detach from within these callbacks is not a good idea.
757 * So we use a bh handler to take care of close events.
759 static void usbredir_chardev_close_bh(void *opaque
)
761 USBRedirDevice
*dev
= opaque
;
763 usbredir_device_disconnect(dev
);
766 usbredirparser_destroy(dev
->parser
);
771 static void usbredir_chardev_open(USBRedirDevice
*dev
)
773 uint32_t caps
[USB_REDIR_CAPS_SIZE
] = { 0, };
776 /* Make sure any pending closes are handled (no-op if none pending) */
777 usbredir_chardev_close_bh(dev
);
778 qemu_bh_cancel(dev
->chardev_close_bh
);
780 strcpy(version
, "qemu usb-redir guest ");
781 pstrcat(version
, sizeof(version
), qemu_get_version());
783 dev
->parser
= qemu_oom_check(usbredirparser_create());
784 dev
->parser
->priv
= dev
;
785 dev
->parser
->log_func
= usbredir_log
;
786 dev
->parser
->read_func
= usbredir_read
;
787 dev
->parser
->write_func
= usbredir_write
;
788 dev
->parser
->hello_func
= usbredir_hello
;
789 dev
->parser
->device_connect_func
= usbredir_device_connect
;
790 dev
->parser
->device_disconnect_func
= usbredir_device_disconnect
;
791 dev
->parser
->interface_info_func
= usbredir_interface_info
;
792 dev
->parser
->ep_info_func
= usbredir_ep_info
;
793 dev
->parser
->configuration_status_func
= usbredir_configuration_status
;
794 dev
->parser
->alt_setting_status_func
= usbredir_alt_setting_status
;
795 dev
->parser
->iso_stream_status_func
= usbredir_iso_stream_status
;
796 dev
->parser
->interrupt_receiving_status_func
=
797 usbredir_interrupt_receiving_status
;
798 dev
->parser
->bulk_streams_status_func
= usbredir_bulk_streams_status
;
799 dev
->parser
->control_packet_func
= usbredir_control_packet
;
800 dev
->parser
->bulk_packet_func
= usbredir_bulk_packet
;
801 dev
->parser
->iso_packet_func
= usbredir_iso_packet
;
802 dev
->parser
->interrupt_packet_func
= usbredir_interrupt_packet
;
803 dev
->read_buf
= NULL
;
804 dev
->read_buf_size
= 0;
806 usbredirparser_caps_set_cap(caps
, usb_redir_cap_connect_device_version
);
807 usbredirparser_caps_set_cap(caps
, usb_redir_cap_filter
);
808 usbredirparser_caps_set_cap(caps
, usb_redir_cap_ep_info_max_packet_size
);
809 usbredirparser_caps_set_cap(caps
, usb_redir_cap_64bits_ids
);
810 usbredirparser_init(dev
->parser
, version
, caps
, USB_REDIR_CAPS_SIZE
, 0);
811 usbredirparser_do_write(dev
->parser
);
814 static void usbredir_reject_device(USBRedirDevice
*dev
)
816 usbredir_device_disconnect(dev
);
817 if (usbredirparser_peer_has_cap(dev
->parser
, usb_redir_cap_filter
)) {
818 usbredirparser_send_filter_reject(dev
->parser
);
819 usbredirparser_do_write(dev
->parser
);
823 static void usbredir_do_attach(void *opaque
)
825 USBRedirDevice
*dev
= opaque
;
827 /* In order to work properly with XHCI controllers we need these caps */
828 if ((dev
->dev
.port
->speedmask
& USB_SPEED_MASK_SUPER
) && !(
829 usbredirparser_peer_has_cap(dev
->parser
,
830 usb_redir_cap_ep_info_max_packet_size
) &&
831 usbredirparser_peer_has_cap(dev
->parser
,
832 usb_redir_cap_64bits_ids
))) {
833 ERROR("usb-redir-host lacks capabilities needed for use with XHCI\n");
834 usbredir_reject_device(dev
);
838 if (usb_device_attach(&dev
->dev
) != 0) {
839 usbredir_reject_device(dev
);
847 static int usbredir_chardev_can_read(void *opaque
)
849 USBRedirDevice
*dev
= opaque
;
852 WARNING("chardev_can_read called on non open chardev!\n");
856 /* usbredir_parser_do_read will consume *all* data we give it */
860 static void usbredir_chardev_read(void *opaque
, const uint8_t *buf
, int size
)
862 USBRedirDevice
*dev
= opaque
;
864 /* No recursion allowed! */
865 assert(dev
->read_buf
== NULL
);
868 dev
->read_buf_size
= size
;
870 usbredirparser_do_read(dev
->parser
);
871 /* Send any acks, etc. which may be queued now */
872 usbredirparser_do_write(dev
->parser
);
875 static void usbredir_chardev_event(void *opaque
, int event
)
877 USBRedirDevice
*dev
= opaque
;
880 case CHR_EVENT_OPENED
:
881 usbredir_chardev_open(dev
);
883 case CHR_EVENT_CLOSED
:
884 qemu_bh_schedule(dev
->chardev_close_bh
);
893 static int usbredir_initfn(USBDevice
*udev
)
895 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
898 if (dev
->cs
== NULL
) {
899 qerror_report(QERR_MISSING_PARAMETER
, "chardev");
903 if (dev
->filter_str
) {
904 i
= usbredirfilter_string_to_rules(dev
->filter_str
, ":", "|",
906 &dev
->filter_rules_count
);
908 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "filter",
909 "a usb device filter string");
914 dev
->chardev_close_bh
= qemu_bh_new(usbredir_chardev_close_bh
, dev
);
915 dev
->attach_timer
= qemu_new_timer_ms(vm_clock
, usbredir_do_attach
, dev
);
917 QTAILQ_INIT(&dev
->cancelled
);
918 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
919 QTAILQ_INIT(&dev
->endpoint
[i
].bufpq
);
922 /* We'll do the attach once we receive the speed from the usb-host */
923 udev
->auto_attach
= 0;
925 /* Let the backend know we are ready */
926 qemu_chr_fe_open(dev
->cs
);
927 qemu_chr_add_handlers(dev
->cs
, usbredir_chardev_can_read
,
928 usbredir_chardev_read
, usbredir_chardev_event
, dev
);
930 add_boot_device_path(dev
->bootindex
, &udev
->qdev
, NULL
);
934 static void usbredir_cleanup_device_queues(USBRedirDevice
*dev
)
936 Cancelled
*c
, *next_c
;
939 QTAILQ_FOREACH_SAFE(c
, &dev
->cancelled
, next
, next_c
) {
940 QTAILQ_REMOVE(&dev
->cancelled
, c
, next
);
943 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
944 usbredir_free_bufpq(dev
, I2EP(i
));
948 static void usbredir_handle_destroy(USBDevice
*udev
)
950 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
952 qemu_chr_fe_close(dev
->cs
);
953 qemu_chr_delete(dev
->cs
);
954 /* Note must be done after qemu_chr_close, as that causes a close event */
955 qemu_bh_delete(dev
->chardev_close_bh
);
957 qemu_del_timer(dev
->attach_timer
);
958 qemu_free_timer(dev
->attach_timer
);
960 usbredir_cleanup_device_queues(dev
);
963 usbredirparser_destroy(dev
->parser
);
966 free(dev
->filter_rules
);
969 static int usbredir_check_filter(USBRedirDevice
*dev
)
971 if (dev
->interface_info
.interface_count
== NO_INTERFACE_INFO
) {
972 ERROR("No interface info for device\n");
976 if (dev
->filter_rules
) {
977 if (!usbredirparser_peer_has_cap(dev
->parser
,
978 usb_redir_cap_connect_device_version
)) {
979 ERROR("Device filter specified and peer does not have the "
980 "connect_device_version capability\n");
984 if (usbredirfilter_check(
986 dev
->filter_rules_count
,
987 dev
->device_info
.device_class
,
988 dev
->device_info
.device_subclass
,
989 dev
->device_info
.device_protocol
,
990 dev
->interface_info
.interface_class
,
991 dev
->interface_info
.interface_subclass
,
992 dev
->interface_info
.interface_protocol
,
993 dev
->interface_info
.interface_count
,
994 dev
->device_info
.vendor_id
,
995 dev
->device_info
.product_id
,
996 dev
->device_info
.device_version_bcd
,
1005 usbredir_reject_device(dev
);
1010 * usbredirparser packet complete callbacks
1013 static int usbredir_handle_status(USBRedirDevice
*dev
,
1014 int status
, int actual_len
)
1017 case usb_redir_success
:
1019 case usb_redir_stall
:
1020 return USB_RET_STALL
;
1021 case usb_redir_cancelled
:
1023 * When the usbredir-host unredirects a device, it will report a status
1024 * of cancelled for all pending packets, followed by a disconnect msg.
1026 return USB_RET_IOERROR
;
1027 case usb_redir_inval
:
1028 WARNING("got invalid param error from usb-host?\n");
1029 return USB_RET_IOERROR
;
1030 case usb_redir_babble
:
1031 return USB_RET_BABBLE
;
1032 case usb_redir_ioerror
:
1033 case usb_redir_timeout
:
1035 return USB_RET_IOERROR
;
1039 static void usbredir_hello(void *priv
, struct usb_redir_hello_header
*h
)
1041 USBRedirDevice
*dev
= priv
;
1043 /* Try to send the filter info now that we've the usb-host's caps */
1044 if (usbredirparser_peer_has_cap(dev
->parser
, usb_redir_cap_filter
) &&
1045 dev
->filter_rules
) {
1046 usbredirparser_send_filter_filter(dev
->parser
, dev
->filter_rules
,
1047 dev
->filter_rules_count
);
1048 usbredirparser_do_write(dev
->parser
);
1052 static void usbredir_device_connect(void *priv
,
1053 struct usb_redir_device_connect_header
*device_connect
)
1055 USBRedirDevice
*dev
= priv
;
1058 if (qemu_timer_pending(dev
->attach_timer
) || dev
->dev
.attached
) {
1059 ERROR("Received device connect while already connected\n");
1063 switch (device_connect
->speed
) {
1064 case usb_redir_speed_low
:
1065 speed
= "low speed";
1066 dev
->dev
.speed
= USB_SPEED_LOW
;
1068 case usb_redir_speed_full
:
1069 speed
= "full speed";
1070 dev
->dev
.speed
= USB_SPEED_FULL
;
1072 case usb_redir_speed_high
:
1073 speed
= "high speed";
1074 dev
->dev
.speed
= USB_SPEED_HIGH
;
1076 case usb_redir_speed_super
:
1077 speed
= "super speed";
1078 dev
->dev
.speed
= USB_SPEED_SUPER
;
1081 speed
= "unknown speed";
1082 dev
->dev
.speed
= USB_SPEED_FULL
;
1085 if (usbredirparser_peer_has_cap(dev
->parser
,
1086 usb_redir_cap_connect_device_version
)) {
1087 INFO("attaching %s device %04x:%04x version %d.%d class %02x\n",
1088 speed
, device_connect
->vendor_id
, device_connect
->product_id
,
1089 ((device_connect
->device_version_bcd
& 0xf000) >> 12) * 10 +
1090 ((device_connect
->device_version_bcd
& 0x0f00) >> 8),
1091 ((device_connect
->device_version_bcd
& 0x00f0) >> 4) * 10 +
1092 ((device_connect
->device_version_bcd
& 0x000f) >> 0),
1093 device_connect
->device_class
);
1095 INFO("attaching %s device %04x:%04x class %02x\n", speed
,
1096 device_connect
->vendor_id
, device_connect
->product_id
,
1097 device_connect
->device_class
);
1100 dev
->dev
.speedmask
= (1 << dev
->dev
.speed
);
1101 dev
->device_info
= *device_connect
;
1103 if (usbredir_check_filter(dev
)) {
1104 WARNING("Device %04x:%04x rejected by device filter, not attaching\n",
1105 device_connect
->vendor_id
, device_connect
->product_id
);
1109 qemu_mod_timer(dev
->attach_timer
, dev
->next_attach_time
);
1112 static void usbredir_device_disconnect(void *priv
)
1114 USBRedirDevice
*dev
= priv
;
1117 /* Stop any pending attaches */
1118 qemu_del_timer(dev
->attach_timer
);
1120 if (dev
->dev
.attached
) {
1121 usb_device_detach(&dev
->dev
);
1123 * Delay next usb device attach to give the guest a chance to see
1124 * see the detach / attach in case of quick close / open succession
1126 dev
->next_attach_time
= qemu_get_clock_ms(vm_clock
) + 200;
1129 /* Reset state so that the next dev connected starts with a clean slate */
1130 usbredir_cleanup_device_queues(dev
);
1131 memset(dev
->endpoint
, 0, sizeof(dev
->endpoint
));
1132 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1133 QTAILQ_INIT(&dev
->endpoint
[i
].bufpq
);
1135 usb_ep_init(&dev
->dev
);
1136 dev
->interface_info
.interface_count
= NO_INTERFACE_INFO
;
1141 static void usbredir_interface_info(void *priv
,
1142 struct usb_redir_interface_info_header
*interface_info
)
1144 USBRedirDevice
*dev
= priv
;
1146 dev
->interface_info
= *interface_info
;
1149 * If we receive interface info after the device has already been
1150 * connected (ie on a set_config), re-check the filter.
1152 if (qemu_timer_pending(dev
->attach_timer
) || dev
->dev
.attached
) {
1153 if (usbredir_check_filter(dev
)) {
1154 ERROR("Device no longer matches filter after interface info "
1155 "change, disconnecting!\n");
1160 static void usbredir_ep_info(void *priv
,
1161 struct usb_redir_ep_info_header
*ep_info
)
1163 USBRedirDevice
*dev
= priv
;
1164 struct USBEndpoint
*usb_ep
;
1167 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1168 dev
->endpoint
[i
].type
= ep_info
->type
[i
];
1169 dev
->endpoint
[i
].interval
= ep_info
->interval
[i
];
1170 dev
->endpoint
[i
].interface
= ep_info
->interface
[i
];
1171 switch (dev
->endpoint
[i
].type
) {
1172 case usb_redir_type_invalid
:
1174 case usb_redir_type_iso
:
1175 case usb_redir_type_interrupt
:
1176 if (dev
->endpoint
[i
].interval
== 0) {
1177 ERROR("Received 0 interval for isoc or irq endpoint\n");
1178 usbredir_device_disconnect(dev
);
1181 case usb_redir_type_control
:
1182 case usb_redir_type_bulk
:
1183 DPRINTF("ep: %02X type: %d interface: %d\n", I2EP(i
),
1184 dev
->endpoint
[i
].type
, dev
->endpoint
[i
].interface
);
1187 ERROR("Received invalid endpoint type\n");
1188 usbredir_device_disconnect(dev
);
1191 usb_ep
= usb_ep_get(&dev
->dev
,
1192 (i
& 0x10) ? USB_TOKEN_IN
: USB_TOKEN_OUT
,
1194 usb_ep
->type
= dev
->endpoint
[i
].type
;
1195 usb_ep
->ifnum
= dev
->endpoint
[i
].interface
;
1196 if (usbredirparser_peer_has_cap(dev
->parser
,
1197 usb_redir_cap_ep_info_max_packet_size
)) {
1198 usb_ep
->max_packet_size
= ep_info
->max_packet_size
[i
];
1200 if (ep_info
->type
[i
] == usb_redir_type_bulk
) {
1201 usb_ep
->pipeline
= true;
1206 static void usbredir_configuration_status(void *priv
, uint64_t id
,
1207 struct usb_redir_configuration_status_header
*config_status
)
1209 USBRedirDevice
*dev
= priv
;
1213 DPRINTF("set config status %d config %d id %"PRIu64
"\n",
1214 config_status
->status
, config_status
->configuration
, id
);
1216 p
= usbredir_find_packet_by_id(dev
, 0, id
);
1218 if (dev
->dev
.setup_buf
[0] & USB_DIR_IN
) {
1219 dev
->dev
.data_buf
[0] = config_status
->configuration
;
1222 p
->result
= usbredir_handle_status(dev
, config_status
->status
, len
);
1223 usb_generic_async_ctrl_complete(&dev
->dev
, p
);
1227 static void usbredir_alt_setting_status(void *priv
, uint64_t id
,
1228 struct usb_redir_alt_setting_status_header
*alt_setting_status
)
1230 USBRedirDevice
*dev
= priv
;
1234 DPRINTF("alt status %d intf %d alt %d id: %"PRIu64
"\n",
1235 alt_setting_status
->status
, alt_setting_status
->interface
,
1236 alt_setting_status
->alt
, id
);
1238 p
= usbredir_find_packet_by_id(dev
, 0, id
);
1240 if (dev
->dev
.setup_buf
[0] & USB_DIR_IN
) {
1241 dev
->dev
.data_buf
[0] = alt_setting_status
->alt
;
1245 usbredir_handle_status(dev
, alt_setting_status
->status
, len
);
1246 usb_generic_async_ctrl_complete(&dev
->dev
, p
);
1250 static void usbredir_iso_stream_status(void *priv
, uint64_t id
,
1251 struct usb_redir_iso_stream_status_header
*iso_stream_status
)
1253 USBRedirDevice
*dev
= priv
;
1254 uint8_t ep
= iso_stream_status
->endpoint
;
1256 DPRINTF("iso status %d ep %02X id %"PRIu64
"\n", iso_stream_status
->status
,
1259 if (!dev
->dev
.attached
|| !dev
->endpoint
[EP2I(ep
)].iso_started
) {
1263 dev
->endpoint
[EP2I(ep
)].iso_error
= iso_stream_status
->status
;
1264 if (iso_stream_status
->status
== usb_redir_stall
) {
1265 DPRINTF("iso stream stopped by peer ep %02X\n", ep
);
1266 dev
->endpoint
[EP2I(ep
)].iso_started
= 0;
1270 static void usbredir_interrupt_receiving_status(void *priv
, uint64_t id
,
1271 struct usb_redir_interrupt_receiving_status_header
1272 *interrupt_receiving_status
)
1274 USBRedirDevice
*dev
= priv
;
1275 uint8_t ep
= interrupt_receiving_status
->endpoint
;
1277 DPRINTF("interrupt recv status %d ep %02X id %"PRIu64
"\n",
1278 interrupt_receiving_status
->status
, ep
, id
);
1280 if (!dev
->dev
.attached
|| !dev
->endpoint
[EP2I(ep
)].interrupt_started
) {
1284 dev
->endpoint
[EP2I(ep
)].interrupt_error
=
1285 interrupt_receiving_status
->status
;
1286 if (interrupt_receiving_status
->status
== usb_redir_stall
) {
1287 DPRINTF("interrupt receiving stopped by peer ep %02X\n", ep
);
1288 dev
->endpoint
[EP2I(ep
)].interrupt_started
= 0;
1292 static void usbredir_bulk_streams_status(void *priv
, uint64_t id
,
1293 struct usb_redir_bulk_streams_status_header
*bulk_streams_status
)
1297 static void usbredir_control_packet(void *priv
, uint64_t id
,
1298 struct usb_redir_control_packet_header
*control_packet
,
1299 uint8_t *data
, int data_len
)
1301 USBRedirDevice
*dev
= priv
;
1303 int len
= control_packet
->length
;
1305 DPRINTF("ctrl-in status %d len %d id %"PRIu64
"\n", control_packet
->status
,
1308 p
= usbredir_find_packet_by_id(dev
, 0, id
);
1310 len
= usbredir_handle_status(dev
, control_packet
->status
, len
);
1312 usbredir_log_data(dev
, "ctrl data in:", data
, data_len
);
1313 if (data_len
<= sizeof(dev
->dev
.data_buf
)) {
1314 memcpy(dev
->dev
.data_buf
, data
, data_len
);
1316 ERROR("ctrl buffer too small (%d > %zu)\n",
1317 data_len
, sizeof(dev
->dev
.data_buf
));
1318 len
= USB_RET_STALL
;
1322 usb_generic_async_ctrl_complete(&dev
->dev
, p
);
1327 static void usbredir_bulk_packet(void *priv
, uint64_t id
,
1328 struct usb_redir_bulk_packet_header
*bulk_packet
,
1329 uint8_t *data
, int data_len
)
1331 USBRedirDevice
*dev
= priv
;
1332 uint8_t ep
= bulk_packet
->endpoint
;
1333 int len
= bulk_packet
->length
;
1336 DPRINTF("bulk-in status %d ep %02X len %d id %"PRIu64
"\n",
1337 bulk_packet
->status
, ep
, len
, id
);
1339 p
= usbredir_find_packet_by_id(dev
, ep
, id
);
1341 len
= usbredir_handle_status(dev
, bulk_packet
->status
, len
);
1343 usbredir_log_data(dev
, "bulk data in:", data
, data_len
);
1344 if (data_len
<= p
->iov
.size
) {
1345 usb_packet_copy(p
, data
, data_len
);
1347 ERROR("bulk got more data then requested (%d > %zd)\n",
1348 data_len
, p
->iov
.size
);
1349 len
= USB_RET_BABBLE
;
1353 usb_packet_complete(&dev
->dev
, p
);
1358 static void usbredir_iso_packet(void *priv
, uint64_t id
,
1359 struct usb_redir_iso_packet_header
*iso_packet
,
1360 uint8_t *data
, int data_len
)
1362 USBRedirDevice
*dev
= priv
;
1363 uint8_t ep
= iso_packet
->endpoint
;
1365 DPRINTF2("iso-in status %d ep %02X len %d id %"PRIu64
"\n",
1366 iso_packet
->status
, ep
, data_len
, id
);
1368 if (dev
->endpoint
[EP2I(ep
)].type
!= USB_ENDPOINT_XFER_ISOC
) {
1369 ERROR("received iso packet for non iso endpoint %02X\n", ep
);
1374 if (dev
->endpoint
[EP2I(ep
)].iso_started
== 0) {
1375 DPRINTF("received iso packet for non started stream ep %02X\n", ep
);
1380 /* bufp_alloc also adds the packet to the ep queue */
1381 bufp_alloc(dev
, data
, data_len
, iso_packet
->status
, ep
);
1384 static void usbredir_interrupt_packet(void *priv
, uint64_t id
,
1385 struct usb_redir_interrupt_packet_header
*interrupt_packet
,
1386 uint8_t *data
, int data_len
)
1388 USBRedirDevice
*dev
= priv
;
1389 uint8_t ep
= interrupt_packet
->endpoint
;
1391 DPRINTF("interrupt-in status %d ep %02X len %d id %"PRIu64
"\n",
1392 interrupt_packet
->status
, ep
, data_len
, id
);
1394 if (dev
->endpoint
[EP2I(ep
)].type
!= USB_ENDPOINT_XFER_INT
) {
1395 ERROR("received int packet for non interrupt endpoint %02X\n", ep
);
1400 if (ep
& USB_DIR_IN
) {
1401 if (dev
->endpoint
[EP2I(ep
)].interrupt_started
== 0) {
1402 DPRINTF("received int packet while not started ep %02X\n", ep
);
1407 /* bufp_alloc also adds the packet to the ep queue */
1408 bufp_alloc(dev
, data
, data_len
, interrupt_packet
->status
, ep
);
1410 int len
= interrupt_packet
->length
;
1412 USBPacket
*p
= usbredir_find_packet_by_id(dev
, ep
, id
);
1414 p
->result
= usbredir_handle_status(dev
,
1415 interrupt_packet
->status
, len
);
1416 usb_packet_complete(&dev
->dev
, p
);
1421 static Property usbredir_properties
[] = {
1422 DEFINE_PROP_CHR("chardev", USBRedirDevice
, cs
),
1423 DEFINE_PROP_UINT8("debug", USBRedirDevice
, debug
, 0),
1424 DEFINE_PROP_STRING("filter", USBRedirDevice
, filter_str
),
1425 DEFINE_PROP_INT32("bootindex", USBRedirDevice
, bootindex
, -1),
1426 DEFINE_PROP_END_OF_LIST(),
1429 static void usbredir_class_initfn(ObjectClass
*klass
, void *data
)
1431 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1432 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1434 uc
->init
= usbredir_initfn
;
1435 uc
->product_desc
= "USB Redirection Device";
1436 uc
->handle_destroy
= usbredir_handle_destroy
;
1437 uc
->cancel_packet
= usbredir_cancel_packet
;
1438 uc
->handle_reset
= usbredir_handle_reset
;
1439 uc
->handle_data
= usbredir_handle_data
;
1440 uc
->handle_control
= usbredir_handle_control
;
1441 dc
->props
= usbredir_properties
;
1444 static TypeInfo usbredir_dev_info
= {
1445 .name
= "usb-redir",
1446 .parent
= TYPE_USB_DEVICE
,
1447 .instance_size
= sizeof(USBRedirDevice
),
1448 .class_init
= usbredir_class_initfn
,
1451 static void usbredir_register_types(void)
1453 type_register_static(&usbredir_dev_info
);
1456 type_init(usbredir_register_types
)