2 * USB redirector usb-guest
4 * Copyright (c) 2011-2012 Red Hat, Inc.
7 * Hans de Goede <hdegoede@redhat.com>
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "qemu-common.h"
29 #include "qemu-timer.h"
35 #include <sys/ioctl.h>
37 #include <usbredirparser.h>
38 #include <usbredirfilter.h>
42 #define MAX_ENDPOINTS 32
43 #define NO_INTERFACE_INFO 255 /* Valid interface_count always <= 32 */
44 #define EP2I(ep_address) (((ep_address & 0x80) >> 3) | (ep_address & 0x0f))
45 #define I2EP(i) (((i & 0x10) << 3) | (i & 0x0f))
47 typedef struct USBRedirDevice USBRedirDevice
;
49 /* Struct to hold buffered packets (iso or int input packets) */
54 QTAILQ_ENTRY(buf_packet
)next
;
60 uint8_t interface
; /* bInterfaceNumber this ep belongs to */
61 uint16_t max_packet_size
; /* In bytes, not wMaxPacketSize format !! */
63 uint8_t iso_error
; /* For reporting iso errors to the HC */
64 uint8_t interrupt_started
;
65 uint8_t interrupt_error
;
66 uint8_t bufpq_prefilled
;
67 uint8_t bufpq_dropping_packets
;
68 QTAILQ_HEAD(, buf_packet
) bufpq
;
70 int32_t bufpq_target_size
;
73 struct PacketIdQueueEntry
{
75 QTAILQ_ENTRY(PacketIdQueueEntry
)next
;
78 struct PacketIdQueue
{
81 QTAILQ_HEAD(, PacketIdQueueEntry
) head
;
85 struct USBRedirDevice
{
92 /* Data passed from chardev the fd_read cb to the usbredirparser read cb */
93 const uint8_t *read_buf
;
95 /* For async handling of close */
96 QEMUBH
*chardev_close_bh
;
97 /* To delay the usb attach in case of quick chardev close + open */
98 QEMUTimer
*attach_timer
;
99 int64_t next_attach_time
;
100 struct usbredirparser
*parser
;
101 struct endp_data endpoint
[MAX_ENDPOINTS
];
102 struct PacketIdQueue cancelled
;
103 struct PacketIdQueue already_in_flight
;
104 /* Data for device filtering */
105 struct usb_redir_device_connect_header device_info
;
106 struct usb_redir_interface_info_header interface_info
;
107 struct usbredirfilter_rule
*filter_rules
;
108 int filter_rules_count
;
109 int compatible_speedmask
;
112 static void usbredir_hello(void *priv
, struct usb_redir_hello_header
*h
);
113 static void usbredir_device_connect(void *priv
,
114 struct usb_redir_device_connect_header
*device_connect
);
115 static void usbredir_device_disconnect(void *priv
);
116 static void usbredir_interface_info(void *priv
,
117 struct usb_redir_interface_info_header
*interface_info
);
118 static void usbredir_ep_info(void *priv
,
119 struct usb_redir_ep_info_header
*ep_info
);
120 static void usbredir_configuration_status(void *priv
, uint64_t id
,
121 struct usb_redir_configuration_status_header
*configuration_status
);
122 static void usbredir_alt_setting_status(void *priv
, uint64_t id
,
123 struct usb_redir_alt_setting_status_header
*alt_setting_status
);
124 static void usbredir_iso_stream_status(void *priv
, uint64_t id
,
125 struct usb_redir_iso_stream_status_header
*iso_stream_status
);
126 static void usbredir_interrupt_receiving_status(void *priv
, uint64_t id
,
127 struct usb_redir_interrupt_receiving_status_header
128 *interrupt_receiving_status
);
129 static void usbredir_bulk_streams_status(void *priv
, uint64_t id
,
130 struct usb_redir_bulk_streams_status_header
*bulk_streams_status
);
131 static void usbredir_control_packet(void *priv
, uint64_t id
,
132 struct usb_redir_control_packet_header
*control_packet
,
133 uint8_t *data
, int data_len
);
134 static void usbredir_bulk_packet(void *priv
, uint64_t id
,
135 struct usb_redir_bulk_packet_header
*bulk_packet
,
136 uint8_t *data
, int data_len
);
137 static void usbredir_iso_packet(void *priv
, uint64_t id
,
138 struct usb_redir_iso_packet_header
*iso_packet
,
139 uint8_t *data
, int data_len
);
140 static void usbredir_interrupt_packet(void *priv
, uint64_t id
,
141 struct usb_redir_interrupt_packet_header
*interrupt_header
,
142 uint8_t *data
, int data_len
);
144 static void usbredir_handle_status(USBRedirDevice
*dev
, USBPacket
*p
,
147 #define VERSION "qemu usb-redir guest " QEMU_VERSION
155 if (dev->debug >= usbredirparser_error) { \
156 error_report("usb-redir error: " __VA_ARGS__); \
159 #define WARNING(...) \
161 if (dev->debug >= usbredirparser_warning) { \
162 error_report("usb-redir warning: " __VA_ARGS__); \
167 if (dev->debug >= usbredirparser_info) { \
168 error_report("usb-redir: " __VA_ARGS__); \
171 #define DPRINTF(...) \
173 if (dev->debug >= usbredirparser_debug) { \
174 error_report("usb-redir: " __VA_ARGS__); \
177 #define DPRINTF2(...) \
179 if (dev->debug >= usbredirparser_debug_data) { \
180 error_report("usb-redir: " __VA_ARGS__); \
184 static void usbredir_log(void *priv
, int level
, const char *msg
)
186 USBRedirDevice
*dev
= priv
;
188 if (dev
->debug
< level
) {
192 error_report("%s", msg
);
195 static void usbredir_log_data(USBRedirDevice
*dev
, const char *desc
,
196 const uint8_t *data
, int len
)
200 if (dev
->debug
< usbredirparser_debug_data
) {
204 for (i
= 0; i
< len
; i
+= j
) {
207 n
= sprintf(buf
, "%s", desc
);
208 for (j
= 0; j
< 8 && i
+ j
< len
; j
++) {
209 n
+= sprintf(buf
+ n
, " %02X", data
[i
+ j
]);
211 error_report("%s", buf
);
216 * usbredirparser io functions
219 static int usbredir_read(void *priv
, uint8_t *data
, int count
)
221 USBRedirDevice
*dev
= priv
;
223 if (dev
->read_buf_size
< count
) {
224 count
= dev
->read_buf_size
;
227 memcpy(data
, dev
->read_buf
, count
);
229 dev
->read_buf_size
-= count
;
230 if (dev
->read_buf_size
) {
231 dev
->read_buf
+= count
;
233 dev
->read_buf
= NULL
;
239 static int usbredir_write(void *priv
, uint8_t *data
, int count
)
241 USBRedirDevice
*dev
= priv
;
243 if (!dev
->cs
->opened
) {
247 /* Don't send new data to the chardev until our state is fully synced */
248 if (!runstate_check(RUN_STATE_RUNNING
)) {
252 return qemu_chr_fe_write(dev
->cs
, data
, count
);
256 * Cancelled and buffered packets helpers
259 static void packet_id_queue_init(struct PacketIdQueue
*q
,
260 USBRedirDevice
*dev
, const char *name
)
264 QTAILQ_INIT(&q
->head
);
268 static void packet_id_queue_add(struct PacketIdQueue
*q
, uint64_t id
)
270 USBRedirDevice
*dev
= q
->dev
;
271 struct PacketIdQueueEntry
*e
;
273 DPRINTF("adding packet id %"PRIu64
" to %s queue\n", id
, q
->name
);
275 e
= g_malloc0(sizeof(struct PacketIdQueueEntry
));
277 QTAILQ_INSERT_TAIL(&q
->head
, e
, next
);
281 static int packet_id_queue_remove(struct PacketIdQueue
*q
, uint64_t id
)
283 USBRedirDevice
*dev
= q
->dev
;
284 struct PacketIdQueueEntry
*e
;
286 QTAILQ_FOREACH(e
, &q
->head
, next
) {
288 DPRINTF("removing packet id %"PRIu64
" from %s queue\n",
290 QTAILQ_REMOVE(&q
->head
, e
, next
);
299 static void packet_id_queue_empty(struct PacketIdQueue
*q
)
301 USBRedirDevice
*dev
= q
->dev
;
302 struct PacketIdQueueEntry
*e
, *next_e
;
304 DPRINTF("removing %d packet-ids from %s queue\n", q
->size
, q
->name
);
306 QTAILQ_FOREACH_SAFE(e
, &q
->head
, next
, next_e
) {
307 QTAILQ_REMOVE(&q
->head
, e
, next
);
313 static void usbredir_cancel_packet(USBDevice
*udev
, USBPacket
*p
)
315 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
318 usb_combined_packet_cancel(udev
, p
);
322 packet_id_queue_add(&dev
->cancelled
, p
->id
);
323 usbredirparser_send_cancel_data_packet(dev
->parser
, p
->id
);
324 usbredirparser_do_write(dev
->parser
);
327 static int usbredir_is_cancelled(USBRedirDevice
*dev
, uint64_t id
)
329 if (!dev
->dev
.attached
) {
330 return 1; /* Treat everything as cancelled after a disconnect */
332 return packet_id_queue_remove(&dev
->cancelled
, id
);
335 static void usbredir_fill_already_in_flight_from_ep(USBRedirDevice
*dev
,
336 struct USBEndpoint
*ep
)
340 QTAILQ_FOREACH(p
, &ep
->queue
, queue
) {
341 /* Skip combined packets, except for the first */
342 if (p
->combined
&& p
!= p
->combined
->first
) {
345 packet_id_queue_add(&dev
->already_in_flight
, p
->id
);
349 static void usbredir_fill_already_in_flight(USBRedirDevice
*dev
)
352 struct USBDevice
*udev
= &dev
->dev
;
354 usbredir_fill_already_in_flight_from_ep(dev
, &udev
->ep_ctl
);
356 for (ep
= 0; ep
< USB_MAX_ENDPOINTS
; ep
++) {
357 usbredir_fill_already_in_flight_from_ep(dev
, &udev
->ep_in
[ep
]);
358 usbredir_fill_already_in_flight_from_ep(dev
, &udev
->ep_out
[ep
]);
362 static int usbredir_already_in_flight(USBRedirDevice
*dev
, uint64_t id
)
364 return packet_id_queue_remove(&dev
->already_in_flight
, id
);
367 static USBPacket
*usbredir_find_packet_by_id(USBRedirDevice
*dev
,
368 uint8_t ep
, uint64_t id
)
372 if (usbredir_is_cancelled(dev
, id
)) {
376 p
= usb_ep_find_packet_by_id(&dev
->dev
,
377 (ep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
,
380 ERROR("could not find packet with id %"PRIu64
"\n", id
);
385 static void bufp_alloc(USBRedirDevice
*dev
,
386 uint8_t *data
, int len
, int status
, uint8_t ep
)
388 struct buf_packet
*bufp
;
390 if (!dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
&&
391 dev
->endpoint
[EP2I(ep
)].bufpq_size
>
392 2 * dev
->endpoint
[EP2I(ep
)].bufpq_target_size
) {
393 DPRINTF("bufpq overflow, dropping packets ep %02X\n", ep
);
394 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 1;
396 /* Since we're interupting the stream anyways, drop enough packets to get
397 back to our target buffer size */
398 if (dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
) {
399 if (dev
->endpoint
[EP2I(ep
)].bufpq_size
>
400 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
) {
404 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 0;
407 bufp
= g_malloc(sizeof(struct buf_packet
));
410 bufp
->status
= status
;
411 QTAILQ_INSERT_TAIL(&dev
->endpoint
[EP2I(ep
)].bufpq
, bufp
, next
);
412 dev
->endpoint
[EP2I(ep
)].bufpq_size
++;
415 static void bufp_free(USBRedirDevice
*dev
, struct buf_packet
*bufp
,
418 QTAILQ_REMOVE(&dev
->endpoint
[EP2I(ep
)].bufpq
, bufp
, next
);
419 dev
->endpoint
[EP2I(ep
)].bufpq_size
--;
424 static void usbredir_free_bufpq(USBRedirDevice
*dev
, uint8_t ep
)
426 struct buf_packet
*buf
, *buf_next
;
428 QTAILQ_FOREACH_SAFE(buf
, &dev
->endpoint
[EP2I(ep
)].bufpq
, next
, buf_next
) {
429 bufp_free(dev
, buf
, ep
);
434 * USBDevice callbacks
437 static void usbredir_handle_reset(USBDevice
*udev
)
439 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
441 DPRINTF("reset device\n");
442 usbredirparser_send_reset(dev
->parser
);
443 usbredirparser_do_write(dev
->parser
);
446 static void usbredir_handle_iso_data(USBRedirDevice
*dev
, USBPacket
*p
,
450 if (!dev
->endpoint
[EP2I(ep
)].iso_started
&&
451 !dev
->endpoint
[EP2I(ep
)].iso_error
) {
452 struct usb_redir_start_iso_stream_header start_iso
= {
457 if (dev
->dev
.speed
== USB_SPEED_HIGH
) {
458 pkts_per_sec
= 8000 / dev
->endpoint
[EP2I(ep
)].interval
;
460 pkts_per_sec
= 1000 / dev
->endpoint
[EP2I(ep
)].interval
;
462 /* Testing has shown that we need circa 60 ms buffer */
463 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
= (pkts_per_sec
* 60) / 1000;
465 /* Aim for approx 100 interrupts / second on the client to
466 balance latency and interrupt load */
467 start_iso
.pkts_per_urb
= pkts_per_sec
/ 100;
468 if (start_iso
.pkts_per_urb
< 1) {
469 start_iso
.pkts_per_urb
= 1;
470 } else if (start_iso
.pkts_per_urb
> 32) {
471 start_iso
.pkts_per_urb
= 32;
474 start_iso
.no_urbs
= (dev
->endpoint
[EP2I(ep
)].bufpq_target_size
+
475 start_iso
.pkts_per_urb
- 1) /
476 start_iso
.pkts_per_urb
;
477 /* Output endpoints pre-fill only 1/2 of the packets, keeping the rest
478 as overflow buffer. Also see the usbredir protocol documentation */
479 if (!(ep
& USB_DIR_IN
)) {
480 start_iso
.no_urbs
*= 2;
482 if (start_iso
.no_urbs
> 16) {
483 start_iso
.no_urbs
= 16;
486 /* No id, we look at the ep when receiving a status back */
487 usbredirparser_send_start_iso_stream(dev
->parser
, 0, &start_iso
);
488 usbredirparser_do_write(dev
->parser
);
489 DPRINTF("iso stream started pkts/sec %d pkts/urb %d urbs %d ep %02X\n",
490 pkts_per_sec
, start_iso
.pkts_per_urb
, start_iso
.no_urbs
, ep
);
491 dev
->endpoint
[EP2I(ep
)].iso_started
= 1;
492 dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
= 0;
493 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 0;
496 if (ep
& USB_DIR_IN
) {
497 struct buf_packet
*isop
;
499 if (dev
->endpoint
[EP2I(ep
)].iso_started
&&
500 !dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
) {
501 if (dev
->endpoint
[EP2I(ep
)].bufpq_size
<
502 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
) {
505 dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
= 1;
508 isop
= QTAILQ_FIRST(&dev
->endpoint
[EP2I(ep
)].bufpq
);
510 DPRINTF("iso-token-in ep %02X, no isop, iso_error: %d\n",
511 ep
, dev
->endpoint
[EP2I(ep
)].iso_error
);
512 /* Re-fill the buffer */
513 dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
= 0;
514 /* Check iso_error for stream errors, otherwise its an underrun */
515 status
= dev
->endpoint
[EP2I(ep
)].iso_error
;
516 dev
->endpoint
[EP2I(ep
)].iso_error
= 0;
517 p
->status
= status
? USB_RET_IOERROR
: USB_RET_SUCCESS
;
520 DPRINTF2("iso-token-in ep %02X status %d len %d queue-size: %d\n", ep
,
521 isop
->status
, isop
->len
, dev
->endpoint
[EP2I(ep
)].bufpq_size
);
523 status
= isop
->status
;
524 if (status
!= usb_redir_success
) {
525 bufp_free(dev
, isop
, ep
);
526 p
->status
= USB_RET_IOERROR
;
531 if (len
> p
->iov
.size
) {
532 ERROR("received iso data is larger then packet ep %02X (%d > %d)\n",
533 ep
, len
, (int)p
->iov
.size
);
534 bufp_free(dev
, isop
, ep
);
535 p
->status
= USB_RET_BABBLE
;
538 usb_packet_copy(p
, isop
->data
, len
);
539 bufp_free(dev
, isop
, ep
);
541 /* If the stream was not started because of a pending error don't
542 send the packet to the usb-host */
543 if (dev
->endpoint
[EP2I(ep
)].iso_started
) {
544 struct usb_redir_iso_packet_header iso_packet
= {
546 .length
= p
->iov
.size
548 uint8_t buf
[p
->iov
.size
];
549 /* No id, we look at the ep when receiving a status back */
550 usb_packet_copy(p
, buf
, p
->iov
.size
);
551 usbredirparser_send_iso_packet(dev
->parser
, 0, &iso_packet
,
553 usbredirparser_do_write(dev
->parser
);
555 status
= dev
->endpoint
[EP2I(ep
)].iso_error
;
556 dev
->endpoint
[EP2I(ep
)].iso_error
= 0;
557 DPRINTF2("iso-token-out ep %02X status %d len %zd\n", ep
, status
,
559 usbredir_handle_status(dev
, p
, status
);
563 static void usbredir_stop_iso_stream(USBRedirDevice
*dev
, uint8_t ep
)
565 struct usb_redir_stop_iso_stream_header stop_iso_stream
= {
568 if (dev
->endpoint
[EP2I(ep
)].iso_started
) {
569 usbredirparser_send_stop_iso_stream(dev
->parser
, 0, &stop_iso_stream
);
570 DPRINTF("iso stream stopped ep %02X\n", ep
);
571 dev
->endpoint
[EP2I(ep
)].iso_started
= 0;
573 dev
->endpoint
[EP2I(ep
)].iso_error
= 0;
574 usbredir_free_bufpq(dev
, ep
);
577 static void usbredir_handle_bulk_data(USBRedirDevice
*dev
, USBPacket
*p
,
580 struct usb_redir_bulk_packet_header bulk_packet
;
581 size_t size
= (p
->combined
) ? p
->combined
->iov
.size
: p
->iov
.size
;
583 DPRINTF("bulk-out ep %02X len %zd id %"PRIu64
"\n", ep
, size
, p
->id
);
585 if (usbredir_already_in_flight(dev
, p
->id
)) {
586 p
->status
= USB_RET_ASYNC
;
590 bulk_packet
.endpoint
= ep
;
591 bulk_packet
.length
= size
;
592 bulk_packet
.stream_id
= 0;
593 bulk_packet
.length_high
= size
>> 16;
594 assert(bulk_packet
.length_high
== 0 ||
595 usbredirparser_peer_has_cap(dev
->parser
,
596 usb_redir_cap_32bits_bulk_length
));
598 if (ep
& USB_DIR_IN
) {
599 usbredirparser_send_bulk_packet(dev
->parser
, p
->id
,
600 &bulk_packet
, NULL
, 0);
604 iov_to_buf(p
->combined
->iov
.iov
, p
->combined
->iov
.niov
,
607 usb_packet_copy(p
, buf
, size
);
609 usbredir_log_data(dev
, "bulk data out:", buf
, size
);
610 usbredirparser_send_bulk_packet(dev
->parser
, p
->id
,
611 &bulk_packet
, buf
, size
);
613 usbredirparser_do_write(dev
->parser
);
614 p
->status
= USB_RET_ASYNC
;
617 static void usbredir_handle_interrupt_data(USBRedirDevice
*dev
,
618 USBPacket
*p
, uint8_t ep
)
620 if (ep
& USB_DIR_IN
) {
621 /* Input interrupt endpoint, buffered packet input */
622 struct buf_packet
*intp
;
625 if (!dev
->endpoint
[EP2I(ep
)].interrupt_started
&&
626 !dev
->endpoint
[EP2I(ep
)].interrupt_error
) {
627 struct usb_redir_start_interrupt_receiving_header start_int
= {
630 /* No id, we look at the ep when receiving a status back */
631 usbredirparser_send_start_interrupt_receiving(dev
->parser
, 0,
633 usbredirparser_do_write(dev
->parser
);
634 DPRINTF("interrupt recv started ep %02X\n", ep
);
635 dev
->endpoint
[EP2I(ep
)].interrupt_started
= 1;
636 /* We don't really want to drop interrupt packets ever, but
637 having some upper limit to how much we buffer is good. */
638 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
= 1000;
639 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 0;
642 intp
= QTAILQ_FIRST(&dev
->endpoint
[EP2I(ep
)].bufpq
);
644 DPRINTF2("interrupt-token-in ep %02X, no intp\n", ep
);
645 /* Check interrupt_error for stream errors */
646 status
= dev
->endpoint
[EP2I(ep
)].interrupt_error
;
647 dev
->endpoint
[EP2I(ep
)].interrupt_error
= 0;
649 usbredir_handle_status(dev
, p
, status
);
651 p
->status
= USB_RET_NAK
;
655 DPRINTF("interrupt-token-in ep %02X status %d len %d\n", ep
,
656 intp
->status
, intp
->len
);
658 status
= intp
->status
;
659 if (status
!= usb_redir_success
) {
660 bufp_free(dev
, intp
, ep
);
661 usbredir_handle_status(dev
, p
, status
);
666 if (len
> p
->iov
.size
) {
667 ERROR("received int data is larger then packet ep %02X\n", ep
);
668 bufp_free(dev
, intp
, ep
);
669 p
->status
= USB_RET_BABBLE
;
672 usb_packet_copy(p
, intp
->data
, len
);
673 bufp_free(dev
, intp
, ep
);
675 /* Output interrupt endpoint, normal async operation */
676 struct usb_redir_interrupt_packet_header interrupt_packet
;
677 uint8_t buf
[p
->iov
.size
];
679 DPRINTF("interrupt-out ep %02X len %zd id %"PRIu64
"\n", ep
,
682 if (usbredir_already_in_flight(dev
, p
->id
)) {
683 p
->status
= USB_RET_ASYNC
;
687 interrupt_packet
.endpoint
= ep
;
688 interrupt_packet
.length
= p
->iov
.size
;
690 usb_packet_copy(p
, buf
, p
->iov
.size
);
691 usbredir_log_data(dev
, "interrupt data out:", buf
, p
->iov
.size
);
692 usbredirparser_send_interrupt_packet(dev
->parser
, p
->id
,
693 &interrupt_packet
, buf
, p
->iov
.size
);
694 usbredirparser_do_write(dev
->parser
);
695 p
->status
= USB_RET_ASYNC
;
699 static void usbredir_stop_interrupt_receiving(USBRedirDevice
*dev
,
702 struct usb_redir_stop_interrupt_receiving_header stop_interrupt_recv
= {
705 if (dev
->endpoint
[EP2I(ep
)].interrupt_started
) {
706 usbredirparser_send_stop_interrupt_receiving(dev
->parser
, 0,
707 &stop_interrupt_recv
);
708 DPRINTF("interrupt recv stopped ep %02X\n", ep
);
709 dev
->endpoint
[EP2I(ep
)].interrupt_started
= 0;
711 dev
->endpoint
[EP2I(ep
)].interrupt_error
= 0;
712 usbredir_free_bufpq(dev
, ep
);
715 static void usbredir_handle_data(USBDevice
*udev
, USBPacket
*p
)
717 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
721 if (p
->pid
== USB_TOKEN_IN
) {
725 switch (dev
->endpoint
[EP2I(ep
)].type
) {
726 case USB_ENDPOINT_XFER_CONTROL
:
727 ERROR("handle_data called for control transfer on ep %02X\n", ep
);
728 p
->status
= USB_RET_NAK
;
730 case USB_ENDPOINT_XFER_ISOC
:
731 usbredir_handle_iso_data(dev
, p
, ep
);
733 case USB_ENDPOINT_XFER_BULK
:
734 if (p
->state
== USB_PACKET_SETUP
&& p
->pid
== USB_TOKEN_IN
&&
736 p
->status
= USB_RET_ADD_TO_QUEUE
;
739 usbredir_handle_bulk_data(dev
, p
, ep
);
741 case USB_ENDPOINT_XFER_INT
:
742 usbredir_handle_interrupt_data(dev
, p
, ep
);
745 ERROR("handle_data ep %02X has unknown type %d\n", ep
,
746 dev
->endpoint
[EP2I(ep
)].type
);
747 p
->status
= USB_RET_NAK
;
751 static void usbredir_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
753 if (ep
->pid
== USB_TOKEN_IN
&& ep
->pipeline
) {
754 usb_ep_combine_input_packets(ep
);
758 static void usbredir_set_config(USBRedirDevice
*dev
, USBPacket
*p
,
761 struct usb_redir_set_configuration_header set_config
;
764 DPRINTF("set config %d id %"PRIu64
"\n", config
, p
->id
);
766 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
767 switch (dev
->endpoint
[i
].type
) {
768 case USB_ENDPOINT_XFER_ISOC
:
769 usbredir_stop_iso_stream(dev
, I2EP(i
));
771 case USB_ENDPOINT_XFER_INT
:
773 usbredir_stop_interrupt_receiving(dev
, I2EP(i
));
777 usbredir_free_bufpq(dev
, I2EP(i
));
780 set_config
.configuration
= config
;
781 usbredirparser_send_set_configuration(dev
->parser
, p
->id
, &set_config
);
782 usbredirparser_do_write(dev
->parser
);
783 p
->status
= USB_RET_ASYNC
;
786 static void usbredir_get_config(USBRedirDevice
*dev
, USBPacket
*p
)
788 DPRINTF("get config id %"PRIu64
"\n", p
->id
);
790 usbredirparser_send_get_configuration(dev
->parser
, p
->id
);
791 usbredirparser_do_write(dev
->parser
);
792 p
->status
= USB_RET_ASYNC
;
795 static void usbredir_set_interface(USBRedirDevice
*dev
, USBPacket
*p
,
796 int interface
, int alt
)
798 struct usb_redir_set_alt_setting_header set_alt
;
801 DPRINTF("set interface %d alt %d id %"PRIu64
"\n", interface
, alt
, p
->id
);
803 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
804 if (dev
->endpoint
[i
].interface
== interface
) {
805 switch (dev
->endpoint
[i
].type
) {
806 case USB_ENDPOINT_XFER_ISOC
:
807 usbredir_stop_iso_stream(dev
, I2EP(i
));
809 case USB_ENDPOINT_XFER_INT
:
811 usbredir_stop_interrupt_receiving(dev
, I2EP(i
));
815 usbredir_free_bufpq(dev
, I2EP(i
));
819 set_alt
.interface
= interface
;
821 usbredirparser_send_set_alt_setting(dev
->parser
, p
->id
, &set_alt
);
822 usbredirparser_do_write(dev
->parser
);
823 p
->status
= USB_RET_ASYNC
;
826 static void usbredir_get_interface(USBRedirDevice
*dev
, USBPacket
*p
,
829 struct usb_redir_get_alt_setting_header get_alt
;
831 DPRINTF("get interface %d id %"PRIu64
"\n", interface
, p
->id
);
833 get_alt
.interface
= interface
;
834 usbredirparser_send_get_alt_setting(dev
->parser
, p
->id
, &get_alt
);
835 usbredirparser_do_write(dev
->parser
);
836 p
->status
= USB_RET_ASYNC
;
839 static void usbredir_handle_control(USBDevice
*udev
, USBPacket
*p
,
840 int request
, int value
, int index
, int length
, uint8_t *data
)
842 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
843 struct usb_redir_control_packet_header control_packet
;
845 if (usbredir_already_in_flight(dev
, p
->id
)) {
846 p
->status
= USB_RET_ASYNC
;
850 /* Special cases for certain standard device requests */
852 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
853 DPRINTF("set address %d\n", value
);
854 dev
->dev
.addr
= value
;
856 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
857 usbredir_set_config(dev
, p
, value
& 0xff);
859 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
860 usbredir_get_config(dev
, p
);
862 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
863 usbredir_set_interface(dev
, p
, index
, value
);
865 case InterfaceRequest
| USB_REQ_GET_INTERFACE
:
866 usbredir_get_interface(dev
, p
, index
);
870 /* Normal ctrl requests, note request is (bRequestType << 8) | bRequest */
872 "ctrl-out type 0x%x req 0x%x val 0x%x index %d len %d id %"PRIu64
"\n",
873 request
>> 8, request
& 0xff, value
, index
, length
, p
->id
);
875 control_packet
.request
= request
& 0xFF;
876 control_packet
.requesttype
= request
>> 8;
877 control_packet
.endpoint
= control_packet
.requesttype
& USB_DIR_IN
;
878 control_packet
.value
= value
;
879 control_packet
.index
= index
;
880 control_packet
.length
= length
;
882 if (control_packet
.requesttype
& USB_DIR_IN
) {
883 usbredirparser_send_control_packet(dev
->parser
, p
->id
,
884 &control_packet
, NULL
, 0);
886 usbredir_log_data(dev
, "ctrl data out:", data
, length
);
887 usbredirparser_send_control_packet(dev
->parser
, p
->id
,
888 &control_packet
, data
, length
);
890 usbredirparser_do_write(dev
->parser
);
891 p
->status
= USB_RET_ASYNC
;
895 * Close events can be triggered by usbredirparser_do_write which gets called
896 * from within the USBDevice data / control packet callbacks and doing a
897 * usb_detach from within these callbacks is not a good idea.
899 * So we use a bh handler to take care of close events.
901 static void usbredir_chardev_close_bh(void *opaque
)
903 USBRedirDevice
*dev
= opaque
;
905 usbredir_device_disconnect(dev
);
908 DPRINTF("destroying usbredirparser\n");
909 usbredirparser_destroy(dev
->parser
);
914 static void usbredir_create_parser(USBRedirDevice
*dev
)
916 uint32_t caps
[USB_REDIR_CAPS_SIZE
] = { 0, };
919 DPRINTF("creating usbredirparser\n");
921 dev
->parser
= qemu_oom_check(usbredirparser_create());
922 dev
->parser
->priv
= dev
;
923 dev
->parser
->log_func
= usbredir_log
;
924 dev
->parser
->read_func
= usbredir_read
;
925 dev
->parser
->write_func
= usbredir_write
;
926 dev
->parser
->hello_func
= usbredir_hello
;
927 dev
->parser
->device_connect_func
= usbredir_device_connect
;
928 dev
->parser
->device_disconnect_func
= usbredir_device_disconnect
;
929 dev
->parser
->interface_info_func
= usbredir_interface_info
;
930 dev
->parser
->ep_info_func
= usbredir_ep_info
;
931 dev
->parser
->configuration_status_func
= usbredir_configuration_status
;
932 dev
->parser
->alt_setting_status_func
= usbredir_alt_setting_status
;
933 dev
->parser
->iso_stream_status_func
= usbredir_iso_stream_status
;
934 dev
->parser
->interrupt_receiving_status_func
=
935 usbredir_interrupt_receiving_status
;
936 dev
->parser
->bulk_streams_status_func
= usbredir_bulk_streams_status
;
937 dev
->parser
->control_packet_func
= usbredir_control_packet
;
938 dev
->parser
->bulk_packet_func
= usbredir_bulk_packet
;
939 dev
->parser
->iso_packet_func
= usbredir_iso_packet
;
940 dev
->parser
->interrupt_packet_func
= usbredir_interrupt_packet
;
941 dev
->read_buf
= NULL
;
942 dev
->read_buf_size
= 0;
944 usbredirparser_caps_set_cap(caps
, usb_redir_cap_connect_device_version
);
945 usbredirparser_caps_set_cap(caps
, usb_redir_cap_filter
);
946 usbredirparser_caps_set_cap(caps
, usb_redir_cap_ep_info_max_packet_size
);
947 usbredirparser_caps_set_cap(caps
, usb_redir_cap_64bits_ids
);
948 usbredirparser_caps_set_cap(caps
, usb_redir_cap_32bits_bulk_length
);
950 if (runstate_check(RUN_STATE_INMIGRATE
)) {
951 flags
|= usbredirparser_fl_no_hello
;
953 usbredirparser_init(dev
->parser
, VERSION
, caps
, USB_REDIR_CAPS_SIZE
,
955 usbredirparser_do_write(dev
->parser
);
958 static void usbredir_reject_device(USBRedirDevice
*dev
)
960 usbredir_device_disconnect(dev
);
961 if (usbredirparser_peer_has_cap(dev
->parser
, usb_redir_cap_filter
)) {
962 usbredirparser_send_filter_reject(dev
->parser
);
963 usbredirparser_do_write(dev
->parser
);
967 static void usbredir_do_attach(void *opaque
)
969 USBRedirDevice
*dev
= opaque
;
971 /* In order to work properly with XHCI controllers we need these caps */
972 if ((dev
->dev
.port
->speedmask
& USB_SPEED_MASK_SUPER
) && !(
973 usbredirparser_peer_has_cap(dev
->parser
,
974 usb_redir_cap_ep_info_max_packet_size
) &&
975 usbredirparser_peer_has_cap(dev
->parser
,
976 usb_redir_cap_64bits_ids
))) {
977 ERROR("usb-redir-host lacks capabilities needed for use with XHCI\n");
978 usbredir_reject_device(dev
);
982 if (usb_device_attach(&dev
->dev
) != 0) {
983 WARNING("rejecting device due to speed mismatch\n");
984 usbredir_reject_device(dev
);
992 static int usbredir_chardev_can_read(void *opaque
)
994 USBRedirDevice
*dev
= opaque
;
997 WARNING("chardev_can_read called on non open chardev!\n");
1001 /* Don't read new data from the chardev until our state is fully synced */
1002 if (!runstate_check(RUN_STATE_RUNNING
)) {
1006 /* usbredir_parser_do_read will consume *all* data we give it */
1010 static void usbredir_chardev_read(void *opaque
, const uint8_t *buf
, int size
)
1012 USBRedirDevice
*dev
= opaque
;
1014 /* No recursion allowed! */
1015 assert(dev
->read_buf
== NULL
);
1017 dev
->read_buf
= buf
;
1018 dev
->read_buf_size
= size
;
1020 usbredirparser_do_read(dev
->parser
);
1021 /* Send any acks, etc. which may be queued now */
1022 usbredirparser_do_write(dev
->parser
);
1025 static void usbredir_chardev_event(void *opaque
, int event
)
1027 USBRedirDevice
*dev
= opaque
;
1030 case CHR_EVENT_OPENED
:
1031 DPRINTF("chardev open\n");
1032 /* Make sure any pending closes are handled (no-op if none pending) */
1033 usbredir_chardev_close_bh(dev
);
1034 qemu_bh_cancel(dev
->chardev_close_bh
);
1035 usbredir_create_parser(dev
);
1037 case CHR_EVENT_CLOSED
:
1038 DPRINTF("chardev close\n");
1039 qemu_bh_schedule(dev
->chardev_close_bh
);
1048 static void usbredir_vm_state_change(void *priv
, int running
, RunState state
)
1050 USBRedirDevice
*dev
= priv
;
1052 if (state
== RUN_STATE_RUNNING
&& dev
->parser
!= NULL
) {
1053 usbredirparser_do_write(dev
->parser
); /* Flush any pending writes */
1057 static int usbredir_initfn(USBDevice
*udev
)
1059 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
1062 if (dev
->cs
== NULL
) {
1063 qerror_report(QERR_MISSING_PARAMETER
, "chardev");
1067 if (dev
->filter_str
) {
1068 i
= usbredirfilter_string_to_rules(dev
->filter_str
, ":", "|",
1070 &dev
->filter_rules_count
);
1072 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "filter",
1073 "a usb device filter string");
1078 dev
->chardev_close_bh
= qemu_bh_new(usbredir_chardev_close_bh
, dev
);
1079 dev
->attach_timer
= qemu_new_timer_ms(vm_clock
, usbredir_do_attach
, dev
);
1081 packet_id_queue_init(&dev
->cancelled
, dev
, "cancelled");
1082 packet_id_queue_init(&dev
->already_in_flight
, dev
, "already-in-flight");
1083 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1084 QTAILQ_INIT(&dev
->endpoint
[i
].bufpq
);
1087 /* We'll do the attach once we receive the speed from the usb-host */
1088 udev
->auto_attach
= 0;
1090 /* Will be cleared during setup when we find conflicts */
1091 dev
->compatible_speedmask
= USB_SPEED_MASK_FULL
| USB_SPEED_MASK_HIGH
;
1093 /* Let the backend know we are ready */
1094 qemu_chr_fe_open(dev
->cs
);
1095 qemu_chr_add_handlers(dev
->cs
, usbredir_chardev_can_read
,
1096 usbredir_chardev_read
, usbredir_chardev_event
, dev
);
1098 qemu_add_vm_change_state_handler(usbredir_vm_state_change
, dev
);
1099 add_boot_device_path(dev
->bootindex
, &udev
->qdev
, NULL
);
1103 static void usbredir_cleanup_device_queues(USBRedirDevice
*dev
)
1107 packet_id_queue_empty(&dev
->cancelled
);
1108 packet_id_queue_empty(&dev
->already_in_flight
);
1109 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1110 usbredir_free_bufpq(dev
, I2EP(i
));
1114 static void usbredir_handle_destroy(USBDevice
*udev
)
1116 USBRedirDevice
*dev
= DO_UPCAST(USBRedirDevice
, dev
, udev
);
1118 qemu_chr_fe_close(dev
->cs
);
1119 qemu_chr_delete(dev
->cs
);
1120 /* Note must be done after qemu_chr_close, as that causes a close event */
1121 qemu_bh_delete(dev
->chardev_close_bh
);
1123 qemu_del_timer(dev
->attach_timer
);
1124 qemu_free_timer(dev
->attach_timer
);
1126 usbredir_cleanup_device_queues(dev
);
1129 usbredirparser_destroy(dev
->parser
);
1132 free(dev
->filter_rules
);
1135 static int usbredir_check_filter(USBRedirDevice
*dev
)
1137 if (dev
->interface_info
.interface_count
== NO_INTERFACE_INFO
) {
1138 ERROR("No interface info for device\n");
1142 if (dev
->filter_rules
) {
1143 if (!usbredirparser_peer_has_cap(dev
->parser
,
1144 usb_redir_cap_connect_device_version
)) {
1145 ERROR("Device filter specified and peer does not have the "
1146 "connect_device_version capability\n");
1150 if (usbredirfilter_check(
1152 dev
->filter_rules_count
,
1153 dev
->device_info
.device_class
,
1154 dev
->device_info
.device_subclass
,
1155 dev
->device_info
.device_protocol
,
1156 dev
->interface_info
.interface_class
,
1157 dev
->interface_info
.interface_subclass
,
1158 dev
->interface_info
.interface_protocol
,
1159 dev
->interface_info
.interface_count
,
1160 dev
->device_info
.vendor_id
,
1161 dev
->device_info
.product_id
,
1162 dev
->device_info
.device_version_bcd
,
1171 usbredir_reject_device(dev
);
1176 * usbredirparser packet complete callbacks
1179 static void usbredir_handle_status(USBRedirDevice
*dev
, USBPacket
*p
,
1183 case usb_redir_success
:
1184 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
1186 case usb_redir_stall
:
1187 p
->status
= USB_RET_STALL
;
1189 case usb_redir_cancelled
:
1191 * When the usbredir-host unredirects a device, it will report a status
1192 * of cancelled for all pending packets, followed by a disconnect msg.
1194 p
->status
= USB_RET_IOERROR
;
1196 case usb_redir_inval
:
1197 WARNING("got invalid param error from usb-host?\n");
1198 p
->status
= USB_RET_IOERROR
;
1200 case usb_redir_babble
:
1201 p
->status
= USB_RET_BABBLE
;
1203 case usb_redir_ioerror
:
1204 case usb_redir_timeout
:
1206 p
->status
= USB_RET_IOERROR
;
1210 static void usbredir_hello(void *priv
, struct usb_redir_hello_header
*h
)
1212 USBRedirDevice
*dev
= priv
;
1214 /* Try to send the filter info now that we've the usb-host's caps */
1215 if (usbredirparser_peer_has_cap(dev
->parser
, usb_redir_cap_filter
) &&
1216 dev
->filter_rules
) {
1217 usbredirparser_send_filter_filter(dev
->parser
, dev
->filter_rules
,
1218 dev
->filter_rules_count
);
1219 usbredirparser_do_write(dev
->parser
);
1223 static void usbredir_device_connect(void *priv
,
1224 struct usb_redir_device_connect_header
*device_connect
)
1226 USBRedirDevice
*dev
= priv
;
1229 if (qemu_timer_pending(dev
->attach_timer
) || dev
->dev
.attached
) {
1230 ERROR("Received device connect while already connected\n");
1234 switch (device_connect
->speed
) {
1235 case usb_redir_speed_low
:
1236 speed
= "low speed";
1237 dev
->dev
.speed
= USB_SPEED_LOW
;
1238 dev
->compatible_speedmask
&= ~USB_SPEED_MASK_FULL
;
1239 dev
->compatible_speedmask
&= ~USB_SPEED_MASK_HIGH
;
1241 case usb_redir_speed_full
:
1242 speed
= "full speed";
1243 dev
->dev
.speed
= USB_SPEED_FULL
;
1244 dev
->compatible_speedmask
&= ~USB_SPEED_MASK_HIGH
;
1246 case usb_redir_speed_high
:
1247 speed
= "high speed";
1248 dev
->dev
.speed
= USB_SPEED_HIGH
;
1250 case usb_redir_speed_super
:
1251 speed
= "super speed";
1252 dev
->dev
.speed
= USB_SPEED_SUPER
;
1255 speed
= "unknown speed";
1256 dev
->dev
.speed
= USB_SPEED_FULL
;
1259 if (usbredirparser_peer_has_cap(dev
->parser
,
1260 usb_redir_cap_connect_device_version
)) {
1261 INFO("attaching %s device %04x:%04x version %d.%d class %02x\n",
1262 speed
, device_connect
->vendor_id
, device_connect
->product_id
,
1263 ((device_connect
->device_version_bcd
& 0xf000) >> 12) * 10 +
1264 ((device_connect
->device_version_bcd
& 0x0f00) >> 8),
1265 ((device_connect
->device_version_bcd
& 0x00f0) >> 4) * 10 +
1266 ((device_connect
->device_version_bcd
& 0x000f) >> 0),
1267 device_connect
->device_class
);
1269 INFO("attaching %s device %04x:%04x class %02x\n", speed
,
1270 device_connect
->vendor_id
, device_connect
->product_id
,
1271 device_connect
->device_class
);
1274 dev
->dev
.speedmask
= (1 << dev
->dev
.speed
) | dev
->compatible_speedmask
;
1275 dev
->device_info
= *device_connect
;
1277 if (usbredir_check_filter(dev
)) {
1278 WARNING("Device %04x:%04x rejected by device filter, not attaching\n",
1279 device_connect
->vendor_id
, device_connect
->product_id
);
1283 qemu_mod_timer(dev
->attach_timer
, dev
->next_attach_time
);
1286 static void usbredir_device_disconnect(void *priv
)
1288 USBRedirDevice
*dev
= priv
;
1291 /* Stop any pending attaches */
1292 qemu_del_timer(dev
->attach_timer
);
1294 if (dev
->dev
.attached
) {
1295 DPRINTF("detaching device\n");
1296 usb_device_detach(&dev
->dev
);
1298 * Delay next usb device attach to give the guest a chance to see
1299 * see the detach / attach in case of quick close / open succession
1301 dev
->next_attach_time
= qemu_get_clock_ms(vm_clock
) + 200;
1304 /* Reset state so that the next dev connected starts with a clean slate */
1305 usbredir_cleanup_device_queues(dev
);
1306 memset(dev
->endpoint
, 0, sizeof(dev
->endpoint
));
1307 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1308 QTAILQ_INIT(&dev
->endpoint
[i
].bufpq
);
1310 usb_ep_init(&dev
->dev
);
1311 dev
->interface_info
.interface_count
= NO_INTERFACE_INFO
;
1314 dev
->compatible_speedmask
= USB_SPEED_MASK_FULL
| USB_SPEED_MASK_HIGH
;
1317 static void usbredir_interface_info(void *priv
,
1318 struct usb_redir_interface_info_header
*interface_info
)
1320 USBRedirDevice
*dev
= priv
;
1322 dev
->interface_info
= *interface_info
;
1325 * If we receive interface info after the device has already been
1326 * connected (ie on a set_config), re-check the filter.
1328 if (qemu_timer_pending(dev
->attach_timer
) || dev
->dev
.attached
) {
1329 if (usbredir_check_filter(dev
)) {
1330 ERROR("Device no longer matches filter after interface info "
1331 "change, disconnecting!\n");
1336 static void usbredir_mark_speed_incompatible(USBRedirDevice
*dev
, int speed
)
1338 dev
->compatible_speedmask
&= ~(1 << speed
);
1339 dev
->dev
.speedmask
= (1 << dev
->dev
.speed
) | dev
->compatible_speedmask
;
1342 static void usbredir_set_pipeline(USBRedirDevice
*dev
, struct USBEndpoint
*uep
)
1344 if (uep
->type
!= USB_ENDPOINT_XFER_BULK
) {
1347 if (uep
->pid
== USB_TOKEN_OUT
) {
1348 uep
->pipeline
= true;
1350 if (uep
->pid
== USB_TOKEN_IN
&& uep
->max_packet_size
!= 0 &&
1351 usbredirparser_peer_has_cap(dev
->parser
,
1352 usb_redir_cap_32bits_bulk_length
)) {
1353 uep
->pipeline
= true;
1357 static void usbredir_setup_usb_eps(USBRedirDevice
*dev
)
1359 struct USBEndpoint
*usb_ep
;
1362 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1363 pid
= (i
& 0x10) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1364 usb_ep
= usb_ep_get(&dev
->dev
, pid
, i
& 0x0f);
1365 usb_ep
->type
= dev
->endpoint
[i
].type
;
1366 usb_ep
->ifnum
= dev
->endpoint
[i
].interface
;
1367 usb_ep
->max_packet_size
= dev
->endpoint
[i
].max_packet_size
;
1368 usbredir_set_pipeline(dev
, usb_ep
);
1372 static void usbredir_ep_info(void *priv
,
1373 struct usb_redir_ep_info_header
*ep_info
)
1375 USBRedirDevice
*dev
= priv
;
1378 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1379 dev
->endpoint
[i
].type
= ep_info
->type
[i
];
1380 dev
->endpoint
[i
].interval
= ep_info
->interval
[i
];
1381 dev
->endpoint
[i
].interface
= ep_info
->interface
[i
];
1382 if (usbredirparser_peer_has_cap(dev
->parser
,
1383 usb_redir_cap_ep_info_max_packet_size
)) {
1384 dev
->endpoint
[i
].max_packet_size
= ep_info
->max_packet_size
[i
];
1386 switch (dev
->endpoint
[i
].type
) {
1387 case usb_redir_type_invalid
:
1389 case usb_redir_type_iso
:
1390 usbredir_mark_speed_incompatible(dev
, USB_SPEED_FULL
);
1391 usbredir_mark_speed_incompatible(dev
, USB_SPEED_HIGH
);
1393 case usb_redir_type_interrupt
:
1394 if (!usbredirparser_peer_has_cap(dev
->parser
,
1395 usb_redir_cap_ep_info_max_packet_size
) ||
1396 ep_info
->max_packet_size
[i
] > 64) {
1397 usbredir_mark_speed_incompatible(dev
, USB_SPEED_FULL
);
1399 if (!usbredirparser_peer_has_cap(dev
->parser
,
1400 usb_redir_cap_ep_info_max_packet_size
) ||
1401 ep_info
->max_packet_size
[i
] > 1024) {
1402 usbredir_mark_speed_incompatible(dev
, USB_SPEED_HIGH
);
1404 if (dev
->endpoint
[i
].interval
== 0) {
1405 ERROR("Received 0 interval for isoc or irq endpoint\n");
1406 usbredir_reject_device(dev
);
1410 case usb_redir_type_control
:
1411 case usb_redir_type_bulk
:
1412 DPRINTF("ep: %02X type: %d interface: %d\n", I2EP(i
),
1413 dev
->endpoint
[i
].type
, dev
->endpoint
[i
].interface
);
1416 ERROR("Received invalid endpoint type\n");
1417 usbredir_reject_device(dev
);
1421 /* The new ep info may have caused a speed incompatibility, recheck */
1422 if (dev
->dev
.attached
&&
1423 !(dev
->dev
.port
->speedmask
& dev
->dev
.speedmask
)) {
1424 ERROR("Device no longer matches speed after endpoint info change, "
1425 "disconnecting!\n");
1426 usbredir_reject_device(dev
);
1429 usbredir_setup_usb_eps(dev
);
1432 static void usbredir_configuration_status(void *priv
, uint64_t id
,
1433 struct usb_redir_configuration_status_header
*config_status
)
1435 USBRedirDevice
*dev
= priv
;
1438 DPRINTF("set config status %d config %d id %"PRIu64
"\n",
1439 config_status
->status
, config_status
->configuration
, id
);
1441 p
= usbredir_find_packet_by_id(dev
, 0, id
);
1443 if (dev
->dev
.setup_buf
[0] & USB_DIR_IN
) {
1444 dev
->dev
.data_buf
[0] = config_status
->configuration
;
1445 p
->actual_length
= 1;
1447 usbredir_handle_status(dev
, p
, config_status
->status
);
1448 usb_generic_async_ctrl_complete(&dev
->dev
, p
);
1452 static void usbredir_alt_setting_status(void *priv
, uint64_t id
,
1453 struct usb_redir_alt_setting_status_header
*alt_setting_status
)
1455 USBRedirDevice
*dev
= priv
;
1458 DPRINTF("alt status %d intf %d alt %d id: %"PRIu64
"\n",
1459 alt_setting_status
->status
, alt_setting_status
->interface
,
1460 alt_setting_status
->alt
, id
);
1462 p
= usbredir_find_packet_by_id(dev
, 0, id
);
1464 if (dev
->dev
.setup_buf
[0] & USB_DIR_IN
) {
1465 dev
->dev
.data_buf
[0] = alt_setting_status
->alt
;
1466 p
->actual_length
= 1;
1468 usbredir_handle_status(dev
, p
, alt_setting_status
->status
);
1469 usb_generic_async_ctrl_complete(&dev
->dev
, p
);
1473 static void usbredir_iso_stream_status(void *priv
, uint64_t id
,
1474 struct usb_redir_iso_stream_status_header
*iso_stream_status
)
1476 USBRedirDevice
*dev
= priv
;
1477 uint8_t ep
= iso_stream_status
->endpoint
;
1479 DPRINTF("iso status %d ep %02X id %"PRIu64
"\n", iso_stream_status
->status
,
1482 if (!dev
->dev
.attached
|| !dev
->endpoint
[EP2I(ep
)].iso_started
) {
1486 dev
->endpoint
[EP2I(ep
)].iso_error
= iso_stream_status
->status
;
1487 if (iso_stream_status
->status
== usb_redir_stall
) {
1488 DPRINTF("iso stream stopped by peer ep %02X\n", ep
);
1489 dev
->endpoint
[EP2I(ep
)].iso_started
= 0;
1493 static void usbredir_interrupt_receiving_status(void *priv
, uint64_t id
,
1494 struct usb_redir_interrupt_receiving_status_header
1495 *interrupt_receiving_status
)
1497 USBRedirDevice
*dev
= priv
;
1498 uint8_t ep
= interrupt_receiving_status
->endpoint
;
1500 DPRINTF("interrupt recv status %d ep %02X id %"PRIu64
"\n",
1501 interrupt_receiving_status
->status
, ep
, id
);
1503 if (!dev
->dev
.attached
|| !dev
->endpoint
[EP2I(ep
)].interrupt_started
) {
1507 dev
->endpoint
[EP2I(ep
)].interrupt_error
=
1508 interrupt_receiving_status
->status
;
1509 if (interrupt_receiving_status
->status
== usb_redir_stall
) {
1510 DPRINTF("interrupt receiving stopped by peer ep %02X\n", ep
);
1511 dev
->endpoint
[EP2I(ep
)].interrupt_started
= 0;
1515 static void usbredir_bulk_streams_status(void *priv
, uint64_t id
,
1516 struct usb_redir_bulk_streams_status_header
*bulk_streams_status
)
1520 static void usbredir_control_packet(void *priv
, uint64_t id
,
1521 struct usb_redir_control_packet_header
*control_packet
,
1522 uint8_t *data
, int data_len
)
1524 USBRedirDevice
*dev
= priv
;
1526 int len
= control_packet
->length
;
1528 DPRINTF("ctrl-in status %d len %d id %"PRIu64
"\n", control_packet
->status
,
1531 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
1532 * to work redirected to a not superspeed capable hcd */
1533 if (dev
->dev
.speed
== USB_SPEED_SUPER
&&
1534 !((dev
->dev
.port
->speedmask
& USB_SPEED_MASK_SUPER
)) &&
1535 control_packet
->requesttype
== 0x80 &&
1536 control_packet
->request
== 6 &&
1537 control_packet
->value
== 0x100 && control_packet
->index
== 0 &&
1538 data_len
>= 18 && data
[7] == 9) {
1542 p
= usbredir_find_packet_by_id(dev
, 0, id
);
1544 usbredir_handle_status(dev
, p
, control_packet
->status
);
1545 if (p
->status
== USB_RET_SUCCESS
) {
1546 usbredir_log_data(dev
, "ctrl data in:", data
, data_len
);
1547 if (data_len
<= sizeof(dev
->dev
.data_buf
)) {
1548 memcpy(dev
->dev
.data_buf
, data
, data_len
);
1550 ERROR("ctrl buffer too small (%d > %zu)\n",
1551 data_len
, sizeof(dev
->dev
.data_buf
));
1552 p
->status
= USB_RET_STALL
;
1556 p
->actual_length
= len
;
1557 usb_generic_async_ctrl_complete(&dev
->dev
, p
);
1562 static void usbredir_bulk_packet(void *priv
, uint64_t id
,
1563 struct usb_redir_bulk_packet_header
*bulk_packet
,
1564 uint8_t *data
, int data_len
)
1566 USBRedirDevice
*dev
= priv
;
1567 uint8_t ep
= bulk_packet
->endpoint
;
1568 int len
= (bulk_packet
->length_high
<< 16) | bulk_packet
->length
;
1571 DPRINTF("bulk-in status %d ep %02X len %d id %"PRIu64
"\n",
1572 bulk_packet
->status
, ep
, len
, id
);
1574 p
= usbredir_find_packet_by_id(dev
, ep
, id
);
1576 size_t size
= (p
->combined
) ? p
->combined
->iov
.size
: p
->iov
.size
;
1577 usbredir_handle_status(dev
, p
, bulk_packet
->status
);
1578 if (p
->status
== USB_RET_SUCCESS
) {
1579 usbredir_log_data(dev
, "bulk data in:", data
, data_len
);
1580 if (data_len
<= size
) {
1582 iov_from_buf(p
->combined
->iov
.iov
, p
->combined
->iov
.niov
,
1585 usb_packet_copy(p
, data
, data_len
);
1588 ERROR("bulk got more data then requested (%d > %zd)\n",
1589 data_len
, p
->iov
.size
);
1590 p
->status
= USB_RET_BABBLE
;
1594 p
->actual_length
= len
;
1595 if (p
->pid
== USB_TOKEN_IN
&& p
->ep
->pipeline
) {
1596 usb_combined_input_packet_complete(&dev
->dev
, p
);
1598 usb_packet_complete(&dev
->dev
, p
);
1604 static void usbredir_iso_packet(void *priv
, uint64_t id
,
1605 struct usb_redir_iso_packet_header
*iso_packet
,
1606 uint8_t *data
, int data_len
)
1608 USBRedirDevice
*dev
= priv
;
1609 uint8_t ep
= iso_packet
->endpoint
;
1611 DPRINTF2("iso-in status %d ep %02X len %d id %"PRIu64
"\n",
1612 iso_packet
->status
, ep
, data_len
, id
);
1614 if (dev
->endpoint
[EP2I(ep
)].type
!= USB_ENDPOINT_XFER_ISOC
) {
1615 ERROR("received iso packet for non iso endpoint %02X\n", ep
);
1620 if (dev
->endpoint
[EP2I(ep
)].iso_started
== 0) {
1621 DPRINTF("received iso packet for non started stream ep %02X\n", ep
);
1626 /* bufp_alloc also adds the packet to the ep queue */
1627 bufp_alloc(dev
, data
, data_len
, iso_packet
->status
, ep
);
1630 static void usbredir_interrupt_packet(void *priv
, uint64_t id
,
1631 struct usb_redir_interrupt_packet_header
*interrupt_packet
,
1632 uint8_t *data
, int data_len
)
1634 USBRedirDevice
*dev
= priv
;
1635 uint8_t ep
= interrupt_packet
->endpoint
;
1637 DPRINTF("interrupt-in status %d ep %02X len %d id %"PRIu64
"\n",
1638 interrupt_packet
->status
, ep
, data_len
, id
);
1640 if (dev
->endpoint
[EP2I(ep
)].type
!= USB_ENDPOINT_XFER_INT
) {
1641 ERROR("received int packet for non interrupt endpoint %02X\n", ep
);
1646 if (ep
& USB_DIR_IN
) {
1647 if (dev
->endpoint
[EP2I(ep
)].interrupt_started
== 0) {
1648 DPRINTF("received int packet while not started ep %02X\n", ep
);
1653 /* bufp_alloc also adds the packet to the ep queue */
1654 bufp_alloc(dev
, data
, data_len
, interrupt_packet
->status
, ep
);
1656 int len
= interrupt_packet
->length
;
1658 USBPacket
*p
= usbredir_find_packet_by_id(dev
, ep
, id
);
1660 usbredir_handle_status(dev
, p
, interrupt_packet
->status
);
1661 p
->actual_length
= len
;
1662 usb_packet_complete(&dev
->dev
, p
);
1671 static void usbredir_pre_save(void *priv
)
1673 USBRedirDevice
*dev
= priv
;
1675 usbredir_fill_already_in_flight(dev
);
1678 static int usbredir_post_load(void *priv
, int version_id
)
1680 USBRedirDevice
*dev
= priv
;
1682 switch (dev
->device_info
.speed
) {
1683 case usb_redir_speed_low
:
1684 dev
->dev
.speed
= USB_SPEED_LOW
;
1686 case usb_redir_speed_full
:
1687 dev
->dev
.speed
= USB_SPEED_FULL
;
1689 case usb_redir_speed_high
:
1690 dev
->dev
.speed
= USB_SPEED_HIGH
;
1692 case usb_redir_speed_super
:
1693 dev
->dev
.speed
= USB_SPEED_SUPER
;
1696 dev
->dev
.speed
= USB_SPEED_FULL
;
1698 dev
->dev
.speedmask
= (1 << dev
->dev
.speed
);
1700 usbredir_setup_usb_eps(dev
);
1705 /* For usbredirparser migration */
1706 static void usbredir_put_parser(QEMUFile
*f
, void *priv
, size_t unused
)
1708 USBRedirDevice
*dev
= priv
;
1712 if (dev
->parser
== NULL
) {
1713 qemu_put_be32(f
, 0);
1717 usbredirparser_serialize(dev
->parser
, &data
, &len
);
1718 qemu_oom_check(data
);
1720 qemu_put_be32(f
, len
);
1721 qemu_put_buffer(f
, data
, len
);
1726 static int usbredir_get_parser(QEMUFile
*f
, void *priv
, size_t unused
)
1728 USBRedirDevice
*dev
= priv
;
1732 len
= qemu_get_be32(f
);
1738 * If our chardev is not open already at this point the usbredir connection
1739 * has been broken (non seamless migration, or restore from disk).
1741 * In this case create a temporary parser to receive the migration data,
1742 * and schedule the close_bh to report the device as disconnected to the
1743 * guest and to destroy the parser again.
1745 if (dev
->parser
== NULL
) {
1746 WARNING("usb-redir connection broken during migration\n");
1747 usbredir_create_parser(dev
);
1748 qemu_bh_schedule(dev
->chardev_close_bh
);
1751 data
= g_malloc(len
);
1752 qemu_get_buffer(f
, data
, len
);
1754 ret
= usbredirparser_unserialize(dev
->parser
, data
, len
);
1761 static const VMStateInfo usbredir_parser_vmstate_info
= {
1762 .name
= "usb-redir-parser",
1763 .put
= usbredir_put_parser
,
1764 .get
= usbredir_get_parser
,
1768 /* For buffered packets (iso/irq) queue migration */
1769 static void usbredir_put_bufpq(QEMUFile
*f
, void *priv
, size_t unused
)
1771 struct endp_data
*endp
= priv
;
1772 struct buf_packet
*bufp
;
1773 int remain
= endp
->bufpq_size
;
1775 qemu_put_be32(f
, endp
->bufpq_size
);
1776 QTAILQ_FOREACH(bufp
, &endp
->bufpq
, next
) {
1777 qemu_put_be32(f
, bufp
->len
);
1778 qemu_put_be32(f
, bufp
->status
);
1779 qemu_put_buffer(f
, bufp
->data
, bufp
->len
);
1782 assert(remain
== 0);
1785 static int usbredir_get_bufpq(QEMUFile
*f
, void *priv
, size_t unused
)
1787 struct endp_data
*endp
= priv
;
1788 struct buf_packet
*bufp
;
1791 endp
->bufpq_size
= qemu_get_be32(f
);
1792 for (i
= 0; i
< endp
->bufpq_size
; i
++) {
1793 bufp
= g_malloc(sizeof(struct buf_packet
));
1794 bufp
->len
= qemu_get_be32(f
);
1795 bufp
->status
= qemu_get_be32(f
);
1796 bufp
->data
= qemu_oom_check(malloc(bufp
->len
)); /* regular malloc! */
1797 qemu_get_buffer(f
, bufp
->data
, bufp
->len
);
1798 QTAILQ_INSERT_TAIL(&endp
->bufpq
, bufp
, next
);
1803 static const VMStateInfo usbredir_ep_bufpq_vmstate_info
= {
1804 .name
= "usb-redir-bufpq",
1805 .put
= usbredir_put_bufpq
,
1806 .get
= usbredir_get_bufpq
,
1810 /* For endp_data migration */
1811 static const VMStateDescription usbredir_ep_vmstate
= {
1812 .name
= "usb-redir-ep",
1814 .minimum_version_id
= 1,
1815 .fields
= (VMStateField
[]) {
1816 VMSTATE_UINT8(type
, struct endp_data
),
1817 VMSTATE_UINT8(interval
, struct endp_data
),
1818 VMSTATE_UINT8(interface
, struct endp_data
),
1819 VMSTATE_UINT16(max_packet_size
, struct endp_data
),
1820 VMSTATE_UINT8(iso_started
, struct endp_data
),
1821 VMSTATE_UINT8(iso_error
, struct endp_data
),
1822 VMSTATE_UINT8(interrupt_started
, struct endp_data
),
1823 VMSTATE_UINT8(interrupt_error
, struct endp_data
),
1824 VMSTATE_UINT8(bufpq_prefilled
, struct endp_data
),
1825 VMSTATE_UINT8(bufpq_dropping_packets
, struct endp_data
),
1829 .field_exists
= NULL
,
1831 .info
= &usbredir_ep_bufpq_vmstate_info
,
1832 .flags
= VMS_SINGLE
,
1835 VMSTATE_INT32(bufpq_target_size
, struct endp_data
),
1836 VMSTATE_END_OF_LIST()
1841 /* For PacketIdQueue migration */
1842 static void usbredir_put_packet_id_q(QEMUFile
*f
, void *priv
, size_t unused
)
1844 struct PacketIdQueue
*q
= priv
;
1845 USBRedirDevice
*dev
= q
->dev
;
1846 struct PacketIdQueueEntry
*e
;
1847 int remain
= q
->size
;
1849 DPRINTF("put_packet_id_q %s size %d\n", q
->name
, q
->size
);
1850 qemu_put_be32(f
, q
->size
);
1851 QTAILQ_FOREACH(e
, &q
->head
, next
) {
1852 qemu_put_be64(f
, e
->id
);
1855 assert(remain
== 0);
1858 static int usbredir_get_packet_id_q(QEMUFile
*f
, void *priv
, size_t unused
)
1860 struct PacketIdQueue
*q
= priv
;
1861 USBRedirDevice
*dev
= q
->dev
;
1865 size
= qemu_get_be32(f
);
1866 DPRINTF("get_packet_id_q %s size %d\n", q
->name
, size
);
1867 for (i
= 0; i
< size
; i
++) {
1868 id
= qemu_get_be64(f
);
1869 packet_id_queue_add(q
, id
);
1871 assert(q
->size
== size
);
1875 static const VMStateInfo usbredir_ep_packet_id_q_vmstate_info
= {
1876 .name
= "usb-redir-packet-id-q",
1877 .put
= usbredir_put_packet_id_q
,
1878 .get
= usbredir_get_packet_id_q
,
1881 static const VMStateDescription usbredir_ep_packet_id_queue_vmstate
= {
1882 .name
= "usb-redir-packet-id-queue",
1884 .minimum_version_id
= 1,
1885 .fields
= (VMStateField
[]) {
1889 .field_exists
= NULL
,
1891 .info
= &usbredir_ep_packet_id_q_vmstate_info
,
1892 .flags
= VMS_SINGLE
,
1895 VMSTATE_END_OF_LIST()
1900 /* For usb_redir_device_connect_header migration */
1901 static const VMStateDescription usbredir_device_info_vmstate
= {
1902 .name
= "usb-redir-device-info",
1904 .minimum_version_id
= 1,
1905 .fields
= (VMStateField
[]) {
1906 VMSTATE_UINT8(speed
, struct usb_redir_device_connect_header
),
1907 VMSTATE_UINT8(device_class
, struct usb_redir_device_connect_header
),
1908 VMSTATE_UINT8(device_subclass
, struct usb_redir_device_connect_header
),
1909 VMSTATE_UINT8(device_protocol
, struct usb_redir_device_connect_header
),
1910 VMSTATE_UINT16(vendor_id
, struct usb_redir_device_connect_header
),
1911 VMSTATE_UINT16(product_id
, struct usb_redir_device_connect_header
),
1912 VMSTATE_UINT16(device_version_bcd
,
1913 struct usb_redir_device_connect_header
),
1914 VMSTATE_END_OF_LIST()
1919 /* For usb_redir_interface_info_header migration */
1920 static const VMStateDescription usbredir_interface_info_vmstate
= {
1921 .name
= "usb-redir-interface-info",
1923 .minimum_version_id
= 1,
1924 .fields
= (VMStateField
[]) {
1925 VMSTATE_UINT32(interface_count
,
1926 struct usb_redir_interface_info_header
),
1927 VMSTATE_UINT8_ARRAY(interface
,
1928 struct usb_redir_interface_info_header
, 32),
1929 VMSTATE_UINT8_ARRAY(interface_class
,
1930 struct usb_redir_interface_info_header
, 32),
1931 VMSTATE_UINT8_ARRAY(interface_subclass
,
1932 struct usb_redir_interface_info_header
, 32),
1933 VMSTATE_UINT8_ARRAY(interface_protocol
,
1934 struct usb_redir_interface_info_header
, 32),
1935 VMSTATE_END_OF_LIST()
1940 /* And finally the USBRedirDevice vmstate itself */
1941 static const VMStateDescription usbredir_vmstate
= {
1942 .name
= "usb-redir",
1944 .minimum_version_id
= 1,
1945 .pre_save
= usbredir_pre_save
,
1946 .post_load
= usbredir_post_load
,
1947 .fields
= (VMStateField
[]) {
1948 VMSTATE_USB_DEVICE(dev
, USBRedirDevice
),
1949 VMSTATE_TIMER(attach_timer
, USBRedirDevice
),
1953 .field_exists
= NULL
,
1955 .info
= &usbredir_parser_vmstate_info
,
1956 .flags
= VMS_SINGLE
,
1959 VMSTATE_STRUCT_ARRAY(endpoint
, USBRedirDevice
, MAX_ENDPOINTS
, 1,
1960 usbredir_ep_vmstate
, struct endp_data
),
1961 VMSTATE_STRUCT(cancelled
, USBRedirDevice
, 1,
1962 usbredir_ep_packet_id_queue_vmstate
,
1963 struct PacketIdQueue
),
1964 VMSTATE_STRUCT(already_in_flight
, USBRedirDevice
, 1,
1965 usbredir_ep_packet_id_queue_vmstate
,
1966 struct PacketIdQueue
),
1967 VMSTATE_STRUCT(device_info
, USBRedirDevice
, 1,
1968 usbredir_device_info_vmstate
,
1969 struct usb_redir_device_connect_header
),
1970 VMSTATE_STRUCT(interface_info
, USBRedirDevice
, 1,
1971 usbredir_interface_info_vmstate
,
1972 struct usb_redir_interface_info_header
),
1973 VMSTATE_END_OF_LIST()
1977 static Property usbredir_properties
[] = {
1978 DEFINE_PROP_CHR("chardev", USBRedirDevice
, cs
),
1979 DEFINE_PROP_UINT8("debug", USBRedirDevice
, debug
, 0),
1980 DEFINE_PROP_STRING("filter", USBRedirDevice
, filter_str
),
1981 DEFINE_PROP_INT32("bootindex", USBRedirDevice
, bootindex
, -1),
1982 DEFINE_PROP_END_OF_LIST(),
1985 static void usbredir_class_initfn(ObjectClass
*klass
, void *data
)
1987 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1988 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1990 uc
->init
= usbredir_initfn
;
1991 uc
->product_desc
= "USB Redirection Device";
1992 uc
->handle_destroy
= usbredir_handle_destroy
;
1993 uc
->cancel_packet
= usbredir_cancel_packet
;
1994 uc
->handle_reset
= usbredir_handle_reset
;
1995 uc
->handle_data
= usbredir_handle_data
;
1996 uc
->handle_control
= usbredir_handle_control
;
1997 uc
->flush_ep_queue
= usbredir_flush_ep_queue
;
1998 dc
->vmsd
= &usbredir_vmstate
;
1999 dc
->props
= usbredir_properties
;
2002 static TypeInfo usbredir_dev_info
= {
2003 .name
= "usb-redir",
2004 .parent
= TYPE_USB_DEVICE
,
2005 .instance_size
= sizeof(USBRedirDevice
),
2006 .class_init
= usbredir_class_initfn
,
2009 static void usbredir_register_types(void)
2011 type_register_static(&usbredir_dev_info
);
2014 type_init(usbredir_register_types
)