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/osdep.h"
29 #include "qapi/error.h"
30 #include "qemu-common.h"
31 #include "qemu/timer.h"
32 #include "sysemu/sysemu.h"
33 #include "qapi/qmp/qerror.h"
34 #include "qemu/error-report.h"
36 #include "sysemu/char.h"
38 #include <usbredirparser.h>
39 #include <usbredirfilter.h>
43 /* ERROR is defined below. Remove any previous definition. */
46 #define MAX_ENDPOINTS 32
47 #define NO_INTERFACE_INFO 255 /* Valid interface_count always <= 32 */
48 #define EP2I(ep_address) (((ep_address & 0x80) >> 3) | (ep_address & 0x0f))
49 #define I2EP(i) (((i & 0x10) << 3) | (i & 0x0f))
50 #define USBEP2I(usb_ep) (((usb_ep)->pid == USB_TOKEN_IN) ? \
51 ((usb_ep)->nr | 0x10) : ((usb_ep)->nr))
52 #define I2USBEP(d, i) (usb_ep_get(&(d)->dev, \
53 ((i) & 0x10) ? USB_TOKEN_IN : USB_TOKEN_OUT, \
56 #ifndef USBREDIR_VERSION /* This is not defined in older usbredir versions */
57 #define USBREDIR_VERSION 0
60 typedef struct USBRedirDevice USBRedirDevice
;
62 /* Struct to hold buffered packets */
65 void *free_on_destroy
;
69 QTAILQ_ENTRY(buf_packet
)next
;
76 uint8_t interface
; /* bInterfaceNumber this ep belongs to */
77 uint16_t max_packet_size
; /* In bytes, not wMaxPacketSize format !! */
80 uint8_t iso_error
; /* For reporting iso errors to the HC */
81 uint8_t interrupt_started
;
82 uint8_t interrupt_error
;
83 uint8_t bulk_receiving_enabled
;
84 uint8_t bulk_receiving_started
;
85 uint8_t bufpq_prefilled
;
86 uint8_t bufpq_dropping_packets
;
87 QTAILQ_HEAD(, buf_packet
) bufpq
;
89 int32_t bufpq_target_size
;
90 USBPacket
*pending_async_packet
;
93 struct PacketIdQueueEntry
{
95 QTAILQ_ENTRY(PacketIdQueueEntry
)next
;
98 struct PacketIdQueue
{
101 QTAILQ_HEAD(, PacketIdQueueEntry
) head
;
105 struct USBRedirDevice
{
112 /* Data passed from chardev the fd_read cb to the usbredirparser read cb */
113 const uint8_t *read_buf
;
115 /* Active chardev-watch-tag */
117 /* For async handling of close / reject */
118 QEMUBH
*chardev_close_bh
;
119 QEMUBH
*device_reject_bh
;
120 /* To delay the usb attach in case of quick chardev close + open */
121 QEMUTimer
*attach_timer
;
122 int64_t next_attach_time
;
123 struct usbredirparser
*parser
;
124 struct endp_data endpoint
[MAX_ENDPOINTS
];
125 struct PacketIdQueue cancelled
;
126 struct PacketIdQueue already_in_flight
;
127 void (*buffered_bulk_in_complete
)(USBRedirDevice
*, USBPacket
*, uint8_t);
128 /* Data for device filtering */
129 struct usb_redir_device_connect_header device_info
;
130 struct usb_redir_interface_info_header interface_info
;
131 struct usbredirfilter_rule
*filter_rules
;
132 int filter_rules_count
;
133 int compatible_speedmask
;
136 #define TYPE_USB_REDIR "usb-redir"
137 #define USB_REDIRECT(obj) OBJECT_CHECK(USBRedirDevice, (obj), TYPE_USB_REDIR)
139 static void usbredir_hello(void *priv
, struct usb_redir_hello_header
*h
);
140 static void usbredir_device_connect(void *priv
,
141 struct usb_redir_device_connect_header
*device_connect
);
142 static void usbredir_device_disconnect(void *priv
);
143 static void usbredir_interface_info(void *priv
,
144 struct usb_redir_interface_info_header
*interface_info
);
145 static void usbredir_ep_info(void *priv
,
146 struct usb_redir_ep_info_header
*ep_info
);
147 static void usbredir_configuration_status(void *priv
, uint64_t id
,
148 struct usb_redir_configuration_status_header
*configuration_status
);
149 static void usbredir_alt_setting_status(void *priv
, uint64_t id
,
150 struct usb_redir_alt_setting_status_header
*alt_setting_status
);
151 static void usbredir_iso_stream_status(void *priv
, uint64_t id
,
152 struct usb_redir_iso_stream_status_header
*iso_stream_status
);
153 static void usbredir_interrupt_receiving_status(void *priv
, uint64_t id
,
154 struct usb_redir_interrupt_receiving_status_header
155 *interrupt_receiving_status
);
156 static void usbredir_bulk_streams_status(void *priv
, uint64_t id
,
157 struct usb_redir_bulk_streams_status_header
*bulk_streams_status
);
158 static void usbredir_bulk_receiving_status(void *priv
, uint64_t id
,
159 struct usb_redir_bulk_receiving_status_header
*bulk_receiving_status
);
160 static void usbredir_control_packet(void *priv
, uint64_t id
,
161 struct usb_redir_control_packet_header
*control_packet
,
162 uint8_t *data
, int data_len
);
163 static void usbredir_bulk_packet(void *priv
, uint64_t id
,
164 struct usb_redir_bulk_packet_header
*bulk_packet
,
165 uint8_t *data
, int data_len
);
166 static void usbredir_iso_packet(void *priv
, uint64_t id
,
167 struct usb_redir_iso_packet_header
*iso_packet
,
168 uint8_t *data
, int data_len
);
169 static void usbredir_interrupt_packet(void *priv
, uint64_t id
,
170 struct usb_redir_interrupt_packet_header
*interrupt_header
,
171 uint8_t *data
, int data_len
);
172 static void usbredir_buffered_bulk_packet(void *priv
, uint64_t id
,
173 struct usb_redir_buffered_bulk_packet_header
*buffered_bulk_packet
,
174 uint8_t *data
, int data_len
);
176 static void usbredir_handle_status(USBRedirDevice
*dev
, USBPacket
*p
,
179 #define VERSION "qemu usb-redir guest " QEMU_VERSION
187 if (dev->debug >= usbredirparser_error) { \
188 error_report("usb-redir error: " __VA_ARGS__); \
191 #define WARNING(...) \
193 if (dev->debug >= usbredirparser_warning) { \
194 error_report("usb-redir warning: " __VA_ARGS__); \
199 if (dev->debug >= usbredirparser_info) { \
200 error_report("usb-redir: " __VA_ARGS__); \
203 #define DPRINTF(...) \
205 if (dev->debug >= usbredirparser_debug) { \
206 error_report("usb-redir: " __VA_ARGS__); \
209 #define DPRINTF2(...) \
211 if (dev->debug >= usbredirparser_debug_data) { \
212 error_report("usb-redir: " __VA_ARGS__); \
216 static void usbredir_log(void *priv
, int level
, const char *msg
)
218 USBRedirDevice
*dev
= priv
;
220 if (dev
->debug
< level
) {
224 error_report("%s", msg
);
227 static void usbredir_log_data(USBRedirDevice
*dev
, const char *desc
,
228 const uint8_t *data
, int len
)
232 if (dev
->debug
< usbredirparser_debug_data
) {
236 for (i
= 0; i
< len
; i
+= j
) {
239 n
= sprintf(buf
, "%s", desc
);
240 for (j
= 0; j
< 8 && i
+ j
< len
; j
++) {
241 n
+= sprintf(buf
+ n
, " %02X", data
[i
+ j
]);
243 error_report("%s", buf
);
248 * usbredirparser io functions
251 static int usbredir_read(void *priv
, uint8_t *data
, int count
)
253 USBRedirDevice
*dev
= priv
;
255 if (dev
->read_buf_size
< count
) {
256 count
= dev
->read_buf_size
;
259 memcpy(data
, dev
->read_buf
, count
);
261 dev
->read_buf_size
-= count
;
262 if (dev
->read_buf_size
) {
263 dev
->read_buf
+= count
;
265 dev
->read_buf
= NULL
;
271 static gboolean
usbredir_write_unblocked(GIOChannel
*chan
, GIOCondition cond
,
274 USBRedirDevice
*dev
= opaque
;
277 usbredirparser_do_write(dev
->parser
);
282 static int usbredir_write(void *priv
, uint8_t *data
, int count
)
284 USBRedirDevice
*dev
= priv
;
287 if (!dev
->cs
->be_open
) {
291 /* Don't send new data to the chardev until our state is fully synced */
292 if (!runstate_check(RUN_STATE_RUNNING
)) {
296 r
= qemu_chr_fe_write(dev
->cs
, data
, count
);
299 dev
->watch
= qemu_chr_fe_add_watch(dev
->cs
, G_IO_OUT
|G_IO_HUP
,
300 usbredir_write_unblocked
, dev
);
310 * Cancelled and buffered packets helpers
313 static void packet_id_queue_init(struct PacketIdQueue
*q
,
314 USBRedirDevice
*dev
, const char *name
)
318 QTAILQ_INIT(&q
->head
);
322 static void packet_id_queue_add(struct PacketIdQueue
*q
, uint64_t id
)
324 USBRedirDevice
*dev
= q
->dev
;
325 struct PacketIdQueueEntry
*e
;
327 DPRINTF("adding packet id %"PRIu64
" to %s queue\n", id
, q
->name
);
329 e
= g_new0(struct PacketIdQueueEntry
, 1);
331 QTAILQ_INSERT_TAIL(&q
->head
, e
, next
);
335 static int packet_id_queue_remove(struct PacketIdQueue
*q
, uint64_t id
)
337 USBRedirDevice
*dev
= q
->dev
;
338 struct PacketIdQueueEntry
*e
;
340 QTAILQ_FOREACH(e
, &q
->head
, next
) {
342 DPRINTF("removing packet id %"PRIu64
" from %s queue\n",
344 QTAILQ_REMOVE(&q
->head
, e
, next
);
353 static void packet_id_queue_empty(struct PacketIdQueue
*q
)
355 USBRedirDevice
*dev
= q
->dev
;
356 struct PacketIdQueueEntry
*e
, *next_e
;
358 DPRINTF("removing %d packet-ids from %s queue\n", q
->size
, q
->name
);
360 QTAILQ_FOREACH_SAFE(e
, &q
->head
, next
, next_e
) {
361 QTAILQ_REMOVE(&q
->head
, e
, next
);
367 static void usbredir_cancel_packet(USBDevice
*udev
, USBPacket
*p
)
369 USBRedirDevice
*dev
= USB_REDIRECT(udev
);
370 int i
= USBEP2I(p
->ep
);
373 usb_combined_packet_cancel(udev
, p
);
377 if (dev
->endpoint
[i
].pending_async_packet
) {
378 assert(dev
->endpoint
[i
].pending_async_packet
== p
);
379 dev
->endpoint
[i
].pending_async_packet
= NULL
;
383 packet_id_queue_add(&dev
->cancelled
, p
->id
);
384 usbredirparser_send_cancel_data_packet(dev
->parser
, p
->id
);
385 usbredirparser_do_write(dev
->parser
);
388 static int usbredir_is_cancelled(USBRedirDevice
*dev
, uint64_t id
)
390 if (!dev
->dev
.attached
) {
391 return 1; /* Treat everything as cancelled after a disconnect */
393 return packet_id_queue_remove(&dev
->cancelled
, id
);
396 static void usbredir_fill_already_in_flight_from_ep(USBRedirDevice
*dev
,
397 struct USBEndpoint
*ep
)
401 /* async handled packets for bulk receiving eps do not count as inflight */
402 if (dev
->endpoint
[USBEP2I(ep
)].bulk_receiving_started
) {
406 QTAILQ_FOREACH(p
, &ep
->queue
, queue
) {
407 /* Skip combined packets, except for the first */
408 if (p
->combined
&& p
!= p
->combined
->first
) {
411 if (p
->state
== USB_PACKET_ASYNC
) {
412 packet_id_queue_add(&dev
->already_in_flight
, p
->id
);
417 static void usbredir_fill_already_in_flight(USBRedirDevice
*dev
)
420 struct USBDevice
*udev
= &dev
->dev
;
422 usbredir_fill_already_in_flight_from_ep(dev
, &udev
->ep_ctl
);
424 for (ep
= 0; ep
< USB_MAX_ENDPOINTS
; ep
++) {
425 usbredir_fill_already_in_flight_from_ep(dev
, &udev
->ep_in
[ep
]);
426 usbredir_fill_already_in_flight_from_ep(dev
, &udev
->ep_out
[ep
]);
430 static int usbredir_already_in_flight(USBRedirDevice
*dev
, uint64_t id
)
432 return packet_id_queue_remove(&dev
->already_in_flight
, id
);
435 static USBPacket
*usbredir_find_packet_by_id(USBRedirDevice
*dev
,
436 uint8_t ep
, uint64_t id
)
440 if (usbredir_is_cancelled(dev
, id
)) {
444 p
= usb_ep_find_packet_by_id(&dev
->dev
,
445 (ep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
,
448 ERROR("could not find packet with id %"PRIu64
"\n", id
);
453 static int bufp_alloc(USBRedirDevice
*dev
, uint8_t *data
, uint16_t len
,
454 uint8_t status
, uint8_t ep
, void *free_on_destroy
)
456 struct buf_packet
*bufp
;
458 if (!dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
&&
459 dev
->endpoint
[EP2I(ep
)].bufpq_size
>
460 2 * dev
->endpoint
[EP2I(ep
)].bufpq_target_size
) {
461 DPRINTF("bufpq overflow, dropping packets ep %02X\n", ep
);
462 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 1;
464 /* Since we're interupting the stream anyways, drop enough packets to get
465 back to our target buffer size */
466 if (dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
) {
467 if (dev
->endpoint
[EP2I(ep
)].bufpq_size
>
468 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
) {
472 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 0;
475 bufp
= g_new(struct buf_packet
, 1);
479 bufp
->status
= status
;
480 bufp
->free_on_destroy
= free_on_destroy
;
481 QTAILQ_INSERT_TAIL(&dev
->endpoint
[EP2I(ep
)].bufpq
, bufp
, next
);
482 dev
->endpoint
[EP2I(ep
)].bufpq_size
++;
486 static void bufp_free(USBRedirDevice
*dev
, struct buf_packet
*bufp
,
489 QTAILQ_REMOVE(&dev
->endpoint
[EP2I(ep
)].bufpq
, bufp
, next
);
490 dev
->endpoint
[EP2I(ep
)].bufpq_size
--;
491 free(bufp
->free_on_destroy
);
495 static void usbredir_free_bufpq(USBRedirDevice
*dev
, uint8_t ep
)
497 struct buf_packet
*buf
, *buf_next
;
499 QTAILQ_FOREACH_SAFE(buf
, &dev
->endpoint
[EP2I(ep
)].bufpq
, next
, buf_next
) {
500 bufp_free(dev
, buf
, ep
);
505 * USBDevice callbacks
508 static void usbredir_handle_reset(USBDevice
*udev
)
510 USBRedirDevice
*dev
= USB_REDIRECT(udev
);
512 DPRINTF("reset device\n");
513 usbredirparser_send_reset(dev
->parser
);
514 usbredirparser_do_write(dev
->parser
);
517 static void usbredir_handle_iso_data(USBRedirDevice
*dev
, USBPacket
*p
,
521 if (!dev
->endpoint
[EP2I(ep
)].iso_started
&&
522 !dev
->endpoint
[EP2I(ep
)].iso_error
) {
523 struct usb_redir_start_iso_stream_header start_iso
= {
528 if (dev
->dev
.speed
== USB_SPEED_HIGH
) {
529 pkts_per_sec
= 8000 / dev
->endpoint
[EP2I(ep
)].interval
;
531 pkts_per_sec
= 1000 / dev
->endpoint
[EP2I(ep
)].interval
;
533 /* Testing has shown that we need circa 60 ms buffer */
534 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
= (pkts_per_sec
* 60) / 1000;
536 /* Aim for approx 100 interrupts / second on the client to
537 balance latency and interrupt load */
538 start_iso
.pkts_per_urb
= pkts_per_sec
/ 100;
539 if (start_iso
.pkts_per_urb
< 1) {
540 start_iso
.pkts_per_urb
= 1;
541 } else if (start_iso
.pkts_per_urb
> 32) {
542 start_iso
.pkts_per_urb
= 32;
545 start_iso
.no_urbs
= (dev
->endpoint
[EP2I(ep
)].bufpq_target_size
+
546 start_iso
.pkts_per_urb
- 1) /
547 start_iso
.pkts_per_urb
;
548 /* Output endpoints pre-fill only 1/2 of the packets, keeping the rest
549 as overflow buffer. Also see the usbredir protocol documentation */
550 if (!(ep
& USB_DIR_IN
)) {
551 start_iso
.no_urbs
*= 2;
553 if (start_iso
.no_urbs
> 16) {
554 start_iso
.no_urbs
= 16;
557 /* No id, we look at the ep when receiving a status back */
558 usbredirparser_send_start_iso_stream(dev
->parser
, 0, &start_iso
);
559 usbredirparser_do_write(dev
->parser
);
560 DPRINTF("iso stream started pkts/sec %d pkts/urb %d urbs %d ep %02X\n",
561 pkts_per_sec
, start_iso
.pkts_per_urb
, start_iso
.no_urbs
, ep
);
562 dev
->endpoint
[EP2I(ep
)].iso_started
= 1;
563 dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
= 0;
564 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 0;
567 if (ep
& USB_DIR_IN
) {
568 struct buf_packet
*isop
;
570 if (dev
->endpoint
[EP2I(ep
)].iso_started
&&
571 !dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
) {
572 if (dev
->endpoint
[EP2I(ep
)].bufpq_size
<
573 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
) {
576 dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
= 1;
579 isop
= QTAILQ_FIRST(&dev
->endpoint
[EP2I(ep
)].bufpq
);
581 DPRINTF("iso-token-in ep %02X, no isop, iso_error: %d\n",
582 ep
, dev
->endpoint
[EP2I(ep
)].iso_error
);
583 /* Re-fill the buffer */
584 dev
->endpoint
[EP2I(ep
)].bufpq_prefilled
= 0;
585 /* Check iso_error for stream errors, otherwise its an underrun */
586 status
= dev
->endpoint
[EP2I(ep
)].iso_error
;
587 dev
->endpoint
[EP2I(ep
)].iso_error
= 0;
588 p
->status
= status
? USB_RET_IOERROR
: USB_RET_SUCCESS
;
591 DPRINTF2("iso-token-in ep %02X status %d len %d queue-size: %d\n", ep
,
592 isop
->status
, isop
->len
, dev
->endpoint
[EP2I(ep
)].bufpq_size
);
594 status
= isop
->status
;
596 if (len
> p
->iov
.size
) {
597 ERROR("received iso data is larger then packet ep %02X (%d > %d)\n",
598 ep
, len
, (int)p
->iov
.size
);
600 status
= usb_redir_babble
;
602 usb_packet_copy(p
, isop
->data
, len
);
603 bufp_free(dev
, isop
, ep
);
604 usbredir_handle_status(dev
, p
, status
);
606 /* If the stream was not started because of a pending error don't
607 send the packet to the usb-host */
608 if (dev
->endpoint
[EP2I(ep
)].iso_started
) {
609 struct usb_redir_iso_packet_header iso_packet
= {
611 .length
= p
->iov
.size
613 uint8_t buf
[p
->iov
.size
];
614 /* No id, we look at the ep when receiving a status back */
615 usb_packet_copy(p
, buf
, p
->iov
.size
);
616 usbredirparser_send_iso_packet(dev
->parser
, 0, &iso_packet
,
618 usbredirparser_do_write(dev
->parser
);
620 status
= dev
->endpoint
[EP2I(ep
)].iso_error
;
621 dev
->endpoint
[EP2I(ep
)].iso_error
= 0;
622 DPRINTF2("iso-token-out ep %02X status %d len %zd\n", ep
, status
,
624 usbredir_handle_status(dev
, p
, status
);
628 static void usbredir_stop_iso_stream(USBRedirDevice
*dev
, uint8_t ep
)
630 struct usb_redir_stop_iso_stream_header stop_iso_stream
= {
633 if (dev
->endpoint
[EP2I(ep
)].iso_started
) {
634 usbredirparser_send_stop_iso_stream(dev
->parser
, 0, &stop_iso_stream
);
635 DPRINTF("iso stream stopped ep %02X\n", ep
);
636 dev
->endpoint
[EP2I(ep
)].iso_started
= 0;
638 dev
->endpoint
[EP2I(ep
)].iso_error
= 0;
639 usbredir_free_bufpq(dev
, ep
);
643 * The usb-host may poll the endpoint faster then our guest, resulting in lots
644 * of smaller bulkp-s. The below buffered_bulk_in_complete* functions combine
645 * data from multiple bulkp-s into a single packet, avoiding bufpq overflows.
647 static void usbredir_buffered_bulk_add_data_to_packet(USBRedirDevice
*dev
,
648 struct buf_packet
*bulkp
, int count
, USBPacket
*p
, uint8_t ep
)
650 usb_packet_copy(p
, bulkp
->data
+ bulkp
->offset
, count
);
651 bulkp
->offset
+= count
;
652 if (bulkp
->offset
== bulkp
->len
) {
653 /* Store status in the last packet with data from this bulkp */
654 usbredir_handle_status(dev
, p
, bulkp
->status
);
655 bufp_free(dev
, bulkp
, ep
);
659 static void usbredir_buffered_bulk_in_complete_raw(USBRedirDevice
*dev
,
660 USBPacket
*p
, uint8_t ep
)
662 struct buf_packet
*bulkp
;
665 while ((bulkp
= QTAILQ_FIRST(&dev
->endpoint
[EP2I(ep
)].bufpq
)) &&
666 p
->actual_length
< p
->iov
.size
&& p
->status
== USB_RET_SUCCESS
) {
667 count
= bulkp
->len
- bulkp
->offset
;
668 if (count
> (p
->iov
.size
- p
->actual_length
)) {
669 count
= p
->iov
.size
- p
->actual_length
;
671 usbredir_buffered_bulk_add_data_to_packet(dev
, bulkp
, count
, p
, ep
);
675 static void usbredir_buffered_bulk_in_complete_ftdi(USBRedirDevice
*dev
,
676 USBPacket
*p
, uint8_t ep
)
678 const int maxp
= dev
->endpoint
[EP2I(ep
)].max_packet_size
;
679 uint8_t header
[2] = { 0, 0 };
680 struct buf_packet
*bulkp
;
683 while ((bulkp
= QTAILQ_FIRST(&dev
->endpoint
[EP2I(ep
)].bufpq
)) &&
684 p
->actual_length
< p
->iov
.size
&& p
->status
== USB_RET_SUCCESS
) {
685 if (bulkp
->len
< 2) {
686 WARNING("malformed ftdi bulk in packet\n");
687 bufp_free(dev
, bulkp
, ep
);
691 if ((p
->actual_length
% maxp
) == 0) {
692 usb_packet_copy(p
, bulkp
->data
, 2);
693 memcpy(header
, bulkp
->data
, 2);
695 if (bulkp
->data
[0] != header
[0] || bulkp
->data
[1] != header
[1]) {
696 break; /* Different header, add to next packet */
700 if (bulkp
->offset
== 0) {
701 bulkp
->offset
= 2; /* Skip header */
703 count
= bulkp
->len
- bulkp
->offset
;
704 /* Must repeat the header at maxp interval */
705 if (count
> (maxp
- (p
->actual_length
% maxp
))) {
706 count
= maxp
- (p
->actual_length
% maxp
);
708 usbredir_buffered_bulk_add_data_to_packet(dev
, bulkp
, count
, p
, ep
);
712 static void usbredir_buffered_bulk_in_complete(USBRedirDevice
*dev
,
713 USBPacket
*p
, uint8_t ep
)
715 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
716 dev
->buffered_bulk_in_complete(dev
, p
, ep
);
717 DPRINTF("bulk-token-in ep %02X status %d len %d id %"PRIu64
"\n",
718 ep
, p
->status
, p
->actual_length
, p
->id
);
721 static void usbredir_handle_buffered_bulk_in_data(USBRedirDevice
*dev
,
722 USBPacket
*p
, uint8_t ep
)
724 /* Input bulk endpoint, buffered packet input */
725 if (!dev
->endpoint
[EP2I(ep
)].bulk_receiving_started
) {
727 struct usb_redir_start_bulk_receiving_header start
= {
732 /* Round bytes_per_transfer up to a multiple of max_packet_size */
733 bpt
= 512 + dev
->endpoint
[EP2I(ep
)].max_packet_size
- 1;
734 bpt
/= dev
->endpoint
[EP2I(ep
)].max_packet_size
;
735 bpt
*= dev
->endpoint
[EP2I(ep
)].max_packet_size
;
736 start
.bytes_per_transfer
= bpt
;
737 /* No id, we look at the ep when receiving a status back */
738 usbredirparser_send_start_bulk_receiving(dev
->parser
, 0, &start
);
739 usbredirparser_do_write(dev
->parser
);
740 DPRINTF("bulk receiving started bytes/transfer %u count %d ep %02X\n",
741 start
.bytes_per_transfer
, start
.no_transfers
, ep
);
742 dev
->endpoint
[EP2I(ep
)].bulk_receiving_started
= 1;
743 /* We don't really want to drop bulk packets ever, but
744 having some upper limit to how much we buffer is good. */
745 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
= 5000;
746 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 0;
749 if (QTAILQ_EMPTY(&dev
->endpoint
[EP2I(ep
)].bufpq
)) {
750 DPRINTF("bulk-token-in ep %02X, no bulkp\n", ep
);
751 assert(dev
->endpoint
[EP2I(ep
)].pending_async_packet
== NULL
);
752 dev
->endpoint
[EP2I(ep
)].pending_async_packet
= p
;
753 p
->status
= USB_RET_ASYNC
;
756 usbredir_buffered_bulk_in_complete(dev
, p
, ep
);
759 static void usbredir_stop_bulk_receiving(USBRedirDevice
*dev
, uint8_t ep
)
761 struct usb_redir_stop_bulk_receiving_header stop_bulk
= {
765 if (dev
->endpoint
[EP2I(ep
)].bulk_receiving_started
) {
766 usbredirparser_send_stop_bulk_receiving(dev
->parser
, 0, &stop_bulk
);
767 DPRINTF("bulk receiving stopped ep %02X\n", ep
);
768 dev
->endpoint
[EP2I(ep
)].bulk_receiving_started
= 0;
770 usbredir_free_bufpq(dev
, ep
);
773 static void usbredir_handle_bulk_data(USBRedirDevice
*dev
, USBPacket
*p
,
776 struct usb_redir_bulk_packet_header bulk_packet
;
777 size_t size
= usb_packet_size(p
);
778 const int maxp
= dev
->endpoint
[EP2I(ep
)].max_packet_size
;
780 if (usbredir_already_in_flight(dev
, p
->id
)) {
781 p
->status
= USB_RET_ASYNC
;
785 if (dev
->endpoint
[EP2I(ep
)].bulk_receiving_enabled
) {
786 if (size
!= 0 && (size
% maxp
) == 0) {
787 usbredir_handle_buffered_bulk_in_data(dev
, p
, ep
);
790 WARNING("bulk recv invalid size %zd ep %02x, disabling\n", size
, ep
);
791 assert(dev
->endpoint
[EP2I(ep
)].pending_async_packet
== NULL
);
792 usbredir_stop_bulk_receiving(dev
, ep
);
793 dev
->endpoint
[EP2I(ep
)].bulk_receiving_enabled
= 0;
796 DPRINTF("bulk-out ep %02X stream %u len %zd id %"PRIu64
"\n",
797 ep
, p
->stream
, size
, p
->id
);
799 bulk_packet
.endpoint
= ep
;
800 bulk_packet
.length
= size
;
801 bulk_packet
.stream_id
= p
->stream
;
802 bulk_packet
.length_high
= size
>> 16;
803 assert(bulk_packet
.length_high
== 0 ||
804 usbredirparser_peer_has_cap(dev
->parser
,
805 usb_redir_cap_32bits_bulk_length
));
807 if (ep
& USB_DIR_IN
) {
808 usbredirparser_send_bulk_packet(dev
->parser
, p
->id
,
809 &bulk_packet
, NULL
, 0);
812 usb_packet_copy(p
, buf
, size
);
813 usbredir_log_data(dev
, "bulk data out:", buf
, size
);
814 usbredirparser_send_bulk_packet(dev
->parser
, p
->id
,
815 &bulk_packet
, buf
, size
);
817 usbredirparser_do_write(dev
->parser
);
818 p
->status
= USB_RET_ASYNC
;
821 static void usbredir_handle_interrupt_in_data(USBRedirDevice
*dev
,
822 USBPacket
*p
, uint8_t ep
)
824 /* Input interrupt endpoint, buffered packet input */
825 struct buf_packet
*intp
;
828 if (!dev
->endpoint
[EP2I(ep
)].interrupt_started
&&
829 !dev
->endpoint
[EP2I(ep
)].interrupt_error
) {
830 struct usb_redir_start_interrupt_receiving_header start_int
= {
833 /* No id, we look at the ep when receiving a status back */
834 usbredirparser_send_start_interrupt_receiving(dev
->parser
, 0,
836 usbredirparser_do_write(dev
->parser
);
837 DPRINTF("interrupt recv started ep %02X\n", ep
);
838 dev
->endpoint
[EP2I(ep
)].interrupt_started
= 1;
839 /* We don't really want to drop interrupt packets ever, but
840 having some upper limit to how much we buffer is good. */
841 dev
->endpoint
[EP2I(ep
)].bufpq_target_size
= 1000;
842 dev
->endpoint
[EP2I(ep
)].bufpq_dropping_packets
= 0;
845 intp
= QTAILQ_FIRST(&dev
->endpoint
[EP2I(ep
)].bufpq
);
847 DPRINTF2("interrupt-token-in ep %02X, no intp\n", ep
);
848 /* Check interrupt_error for stream errors */
849 status
= dev
->endpoint
[EP2I(ep
)].interrupt_error
;
850 dev
->endpoint
[EP2I(ep
)].interrupt_error
= 0;
852 usbredir_handle_status(dev
, p
, status
);
854 p
->status
= USB_RET_NAK
;
858 DPRINTF("interrupt-token-in ep %02X status %d len %d\n", ep
,
859 intp
->status
, intp
->len
);
861 status
= intp
->status
;
863 if (len
> p
->iov
.size
) {
864 ERROR("received int data is larger then packet ep %02X\n", ep
);
866 status
= usb_redir_babble
;
868 usb_packet_copy(p
, intp
->data
, len
);
869 bufp_free(dev
, intp
, ep
);
870 usbredir_handle_status(dev
, p
, status
);
874 * Handle interrupt out data, the usbredir protocol expects us to do this
875 * async, so that it can report back a completion status. But guests will
876 * expect immediate completion for an interrupt endpoint, and handling this
877 * async causes migration issues. So we report success directly, counting
878 * on the fact that output interrupt packets normally always succeed.
880 static void usbredir_handle_interrupt_out_data(USBRedirDevice
*dev
,
881 USBPacket
*p
, uint8_t ep
)
883 struct usb_redir_interrupt_packet_header interrupt_packet
;
884 uint8_t buf
[p
->iov
.size
];
886 DPRINTF("interrupt-out ep %02X len %zd id %"PRIu64
"\n", ep
,
889 interrupt_packet
.endpoint
= ep
;
890 interrupt_packet
.length
= p
->iov
.size
;
892 usb_packet_copy(p
, buf
, p
->iov
.size
);
893 usbredir_log_data(dev
, "interrupt data out:", buf
, p
->iov
.size
);
894 usbredirparser_send_interrupt_packet(dev
->parser
, p
->id
,
895 &interrupt_packet
, buf
, p
->iov
.size
);
896 usbredirparser_do_write(dev
->parser
);
899 static void usbredir_stop_interrupt_receiving(USBRedirDevice
*dev
,
902 struct usb_redir_stop_interrupt_receiving_header stop_interrupt_recv
= {
905 if (dev
->endpoint
[EP2I(ep
)].interrupt_started
) {
906 usbredirparser_send_stop_interrupt_receiving(dev
->parser
, 0,
907 &stop_interrupt_recv
);
908 DPRINTF("interrupt recv stopped ep %02X\n", ep
);
909 dev
->endpoint
[EP2I(ep
)].interrupt_started
= 0;
911 dev
->endpoint
[EP2I(ep
)].interrupt_error
= 0;
912 usbredir_free_bufpq(dev
, ep
);
915 static void usbredir_handle_data(USBDevice
*udev
, USBPacket
*p
)
917 USBRedirDevice
*dev
= USB_REDIRECT(udev
);
921 if (p
->pid
== USB_TOKEN_IN
) {
925 switch (dev
->endpoint
[EP2I(ep
)].type
) {
926 case USB_ENDPOINT_XFER_CONTROL
:
927 ERROR("handle_data called for control transfer on ep %02X\n", ep
);
928 p
->status
= USB_RET_NAK
;
930 case USB_ENDPOINT_XFER_BULK
:
931 if (p
->state
== USB_PACKET_SETUP
&& p
->pid
== USB_TOKEN_IN
&&
933 p
->status
= USB_RET_ADD_TO_QUEUE
;
936 usbredir_handle_bulk_data(dev
, p
, ep
);
938 case USB_ENDPOINT_XFER_ISOC
:
939 usbredir_handle_iso_data(dev
, p
, ep
);
941 case USB_ENDPOINT_XFER_INT
:
942 if (ep
& USB_DIR_IN
) {
943 usbredir_handle_interrupt_in_data(dev
, p
, ep
);
945 usbredir_handle_interrupt_out_data(dev
, p
, ep
);
949 ERROR("handle_data ep %02X has unknown type %d\n", ep
,
950 dev
->endpoint
[EP2I(ep
)].type
);
951 p
->status
= USB_RET_NAK
;
955 static void usbredir_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
957 if (ep
->pid
== USB_TOKEN_IN
&& ep
->pipeline
) {
958 usb_ep_combine_input_packets(ep
);
962 static void usbredir_stop_ep(USBRedirDevice
*dev
, int i
)
964 uint8_t ep
= I2EP(i
);
966 switch (dev
->endpoint
[i
].type
) {
967 case USB_ENDPOINT_XFER_BULK
:
968 if (ep
& USB_DIR_IN
) {
969 usbredir_stop_bulk_receiving(dev
, ep
);
972 case USB_ENDPOINT_XFER_ISOC
:
973 usbredir_stop_iso_stream(dev
, ep
);
975 case USB_ENDPOINT_XFER_INT
:
976 if (ep
& USB_DIR_IN
) {
977 usbredir_stop_interrupt_receiving(dev
, ep
);
981 usbredir_free_bufpq(dev
, ep
);
984 static void usbredir_ep_stopped(USBDevice
*udev
, USBEndpoint
*uep
)
986 USBRedirDevice
*dev
= USB_REDIRECT(udev
);
988 usbredir_stop_ep(dev
, USBEP2I(uep
));
989 usbredirparser_do_write(dev
->parser
);
992 static void usbredir_set_config(USBRedirDevice
*dev
, USBPacket
*p
,
995 struct usb_redir_set_configuration_header set_config
;
998 DPRINTF("set config %d id %"PRIu64
"\n", config
, p
->id
);
1000 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1001 usbredir_stop_ep(dev
, i
);
1004 set_config
.configuration
= config
;
1005 usbredirparser_send_set_configuration(dev
->parser
, p
->id
, &set_config
);
1006 usbredirparser_do_write(dev
->parser
);
1007 p
->status
= USB_RET_ASYNC
;
1010 static void usbredir_get_config(USBRedirDevice
*dev
, USBPacket
*p
)
1012 DPRINTF("get config id %"PRIu64
"\n", p
->id
);
1014 usbredirparser_send_get_configuration(dev
->parser
, p
->id
);
1015 usbredirparser_do_write(dev
->parser
);
1016 p
->status
= USB_RET_ASYNC
;
1019 static void usbredir_set_interface(USBRedirDevice
*dev
, USBPacket
*p
,
1020 int interface
, int alt
)
1022 struct usb_redir_set_alt_setting_header set_alt
;
1025 DPRINTF("set interface %d alt %d id %"PRIu64
"\n", interface
, alt
, p
->id
);
1027 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1028 if (dev
->endpoint
[i
].interface
== interface
) {
1029 usbredir_stop_ep(dev
, i
);
1033 set_alt
.interface
= interface
;
1035 usbredirparser_send_set_alt_setting(dev
->parser
, p
->id
, &set_alt
);
1036 usbredirparser_do_write(dev
->parser
);
1037 p
->status
= USB_RET_ASYNC
;
1040 static void usbredir_get_interface(USBRedirDevice
*dev
, USBPacket
*p
,
1043 struct usb_redir_get_alt_setting_header get_alt
;
1045 DPRINTF("get interface %d id %"PRIu64
"\n", interface
, p
->id
);
1047 get_alt
.interface
= interface
;
1048 usbredirparser_send_get_alt_setting(dev
->parser
, p
->id
, &get_alt
);
1049 usbredirparser_do_write(dev
->parser
);
1050 p
->status
= USB_RET_ASYNC
;
1053 static void usbredir_handle_control(USBDevice
*udev
, USBPacket
*p
,
1054 int request
, int value
, int index
, int length
, uint8_t *data
)
1056 USBRedirDevice
*dev
= USB_REDIRECT(udev
);
1057 struct usb_redir_control_packet_header control_packet
;
1059 if (usbredir_already_in_flight(dev
, p
->id
)) {
1060 p
->status
= USB_RET_ASYNC
;
1064 /* Special cases for certain standard device requests */
1066 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1067 DPRINTF("set address %d\n", value
);
1068 dev
->dev
.addr
= value
;
1070 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1071 usbredir_set_config(dev
, p
, value
& 0xff);
1073 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
1074 usbredir_get_config(dev
, p
);
1076 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1077 usbredir_set_interface(dev
, p
, index
, value
);
1079 case InterfaceRequest
| USB_REQ_GET_INTERFACE
:
1080 usbredir_get_interface(dev
, p
, index
);
1084 /* Normal ctrl requests, note request is (bRequestType << 8) | bRequest */
1086 "ctrl-out type 0x%x req 0x%x val 0x%x index %d len %d id %"PRIu64
"\n",
1087 request
>> 8, request
& 0xff, value
, index
, length
, p
->id
);
1089 control_packet
.request
= request
& 0xFF;
1090 control_packet
.requesttype
= request
>> 8;
1091 control_packet
.endpoint
= control_packet
.requesttype
& USB_DIR_IN
;
1092 control_packet
.value
= value
;
1093 control_packet
.index
= index
;
1094 control_packet
.length
= length
;
1096 if (control_packet
.requesttype
& USB_DIR_IN
) {
1097 usbredirparser_send_control_packet(dev
->parser
, p
->id
,
1098 &control_packet
, NULL
, 0);
1100 usbredir_log_data(dev
, "ctrl data out:", data
, length
);
1101 usbredirparser_send_control_packet(dev
->parser
, p
->id
,
1102 &control_packet
, data
, length
);
1104 usbredirparser_do_write(dev
->parser
);
1105 p
->status
= USB_RET_ASYNC
;
1108 static int usbredir_alloc_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1109 int nr_eps
, int streams
)
1111 USBRedirDevice
*dev
= USB_REDIRECT(udev
);
1112 #if USBREDIR_VERSION >= 0x000700
1113 struct usb_redir_alloc_bulk_streams_header alloc_streams
;
1116 if (!usbredirparser_peer_has_cap(dev
->parser
,
1117 usb_redir_cap_bulk_streams
)) {
1118 ERROR("peer does not support streams\n");
1123 ERROR("request to allocate 0 streams\n");
1127 alloc_streams
.no_streams
= streams
;
1128 alloc_streams
.endpoints
= 0;
1129 for (i
= 0; i
< nr_eps
; i
++) {
1130 alloc_streams
.endpoints
|= 1 << USBEP2I(eps
[i
]);
1132 usbredirparser_send_alloc_bulk_streams(dev
->parser
, 0, &alloc_streams
);
1133 usbredirparser_do_write(dev
->parser
);
1137 ERROR("usbredir_alloc_streams not implemented\n");
1141 ERROR("streams are not available, disconnecting\n");
1142 qemu_bh_schedule(dev
->device_reject_bh
);
1146 static void usbredir_free_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1149 #if USBREDIR_VERSION >= 0x000700
1150 USBRedirDevice
*dev
= USB_REDIRECT(udev
);
1151 struct usb_redir_free_bulk_streams_header free_streams
;
1154 if (!usbredirparser_peer_has_cap(dev
->parser
,
1155 usb_redir_cap_bulk_streams
)) {
1159 free_streams
.endpoints
= 0;
1160 for (i
= 0; i
< nr_eps
; i
++) {
1161 free_streams
.endpoints
|= 1 << USBEP2I(eps
[i
]);
1163 usbredirparser_send_free_bulk_streams(dev
->parser
, 0, &free_streams
);
1164 usbredirparser_do_write(dev
->parser
);
1169 * Close events can be triggered by usbredirparser_do_write which gets called
1170 * from within the USBDevice data / control packet callbacks and doing a
1171 * usb_detach from within these callbacks is not a good idea.
1173 * So we use a bh handler to take care of close events.
1175 static void usbredir_chardev_close_bh(void *opaque
)
1177 USBRedirDevice
*dev
= opaque
;
1179 qemu_bh_cancel(dev
->device_reject_bh
);
1180 usbredir_device_disconnect(dev
);
1183 DPRINTF("destroying usbredirparser\n");
1184 usbredirparser_destroy(dev
->parser
);
1188 g_source_remove(dev
->watch
);
1193 static void usbredir_create_parser(USBRedirDevice
*dev
)
1195 uint32_t caps
[USB_REDIR_CAPS_SIZE
] = { 0, };
1198 DPRINTF("creating usbredirparser\n");
1200 dev
->parser
= qemu_oom_check(usbredirparser_create());
1201 dev
->parser
->priv
= dev
;
1202 dev
->parser
->log_func
= usbredir_log
;
1203 dev
->parser
->read_func
= usbredir_read
;
1204 dev
->parser
->write_func
= usbredir_write
;
1205 dev
->parser
->hello_func
= usbredir_hello
;
1206 dev
->parser
->device_connect_func
= usbredir_device_connect
;
1207 dev
->parser
->device_disconnect_func
= usbredir_device_disconnect
;
1208 dev
->parser
->interface_info_func
= usbredir_interface_info
;
1209 dev
->parser
->ep_info_func
= usbredir_ep_info
;
1210 dev
->parser
->configuration_status_func
= usbredir_configuration_status
;
1211 dev
->parser
->alt_setting_status_func
= usbredir_alt_setting_status
;
1212 dev
->parser
->iso_stream_status_func
= usbredir_iso_stream_status
;
1213 dev
->parser
->interrupt_receiving_status_func
=
1214 usbredir_interrupt_receiving_status
;
1215 dev
->parser
->bulk_streams_status_func
= usbredir_bulk_streams_status
;
1216 dev
->parser
->bulk_receiving_status_func
= usbredir_bulk_receiving_status
;
1217 dev
->parser
->control_packet_func
= usbredir_control_packet
;
1218 dev
->parser
->bulk_packet_func
= usbredir_bulk_packet
;
1219 dev
->parser
->iso_packet_func
= usbredir_iso_packet
;
1220 dev
->parser
->interrupt_packet_func
= usbredir_interrupt_packet
;
1221 dev
->parser
->buffered_bulk_packet_func
= usbredir_buffered_bulk_packet
;
1222 dev
->read_buf
= NULL
;
1223 dev
->read_buf_size
= 0;
1225 usbredirparser_caps_set_cap(caps
, usb_redir_cap_connect_device_version
);
1226 usbredirparser_caps_set_cap(caps
, usb_redir_cap_filter
);
1227 usbredirparser_caps_set_cap(caps
, usb_redir_cap_ep_info_max_packet_size
);
1228 usbredirparser_caps_set_cap(caps
, usb_redir_cap_64bits_ids
);
1229 usbredirparser_caps_set_cap(caps
, usb_redir_cap_32bits_bulk_length
);
1230 usbredirparser_caps_set_cap(caps
, usb_redir_cap_bulk_receiving
);
1231 #if USBREDIR_VERSION >= 0x000700
1232 usbredirparser_caps_set_cap(caps
, usb_redir_cap_bulk_streams
);
1235 if (runstate_check(RUN_STATE_INMIGRATE
)) {
1236 flags
|= usbredirparser_fl_no_hello
;
1238 usbredirparser_init(dev
->parser
, VERSION
, caps
, USB_REDIR_CAPS_SIZE
,
1240 usbredirparser_do_write(dev
->parser
);
1243 static void usbredir_reject_device(USBRedirDevice
*dev
)
1245 usbredir_device_disconnect(dev
);
1246 if (usbredirparser_peer_has_cap(dev
->parser
, usb_redir_cap_filter
)) {
1247 usbredirparser_send_filter_reject(dev
->parser
);
1248 usbredirparser_do_write(dev
->parser
);
1253 * We may need to reject the device when the hcd calls alloc_streams, doing
1254 * an usb_detach from within a hcd call is not a good idea, hence this bh.
1256 static void usbredir_device_reject_bh(void *opaque
)
1258 USBRedirDevice
*dev
= opaque
;
1260 usbredir_reject_device(dev
);
1263 static void usbredir_do_attach(void *opaque
)
1265 USBRedirDevice
*dev
= opaque
;
1266 Error
*local_err
= NULL
;
1268 /* In order to work properly with XHCI controllers we need these caps */
1269 if ((dev
->dev
.port
->speedmask
& USB_SPEED_MASK_SUPER
) && !(
1270 usbredirparser_peer_has_cap(dev
->parser
,
1271 usb_redir_cap_ep_info_max_packet_size
) &&
1272 usbredirparser_peer_has_cap(dev
->parser
,
1273 usb_redir_cap_32bits_bulk_length
) &&
1274 usbredirparser_peer_has_cap(dev
->parser
,
1275 usb_redir_cap_64bits_ids
))) {
1276 ERROR("usb-redir-host lacks capabilities needed for use with XHCI\n");
1277 usbredir_reject_device(dev
);
1281 usb_device_attach(&dev
->dev
, &local_err
);
1283 error_report_err(local_err
);
1284 WARNING("rejecting device due to speed mismatch\n");
1285 usbredir_reject_device(dev
);
1293 static int usbredir_chardev_can_read(void *opaque
)
1295 USBRedirDevice
*dev
= opaque
;
1298 WARNING("chardev_can_read called on non open chardev!\n");
1302 /* Don't read new data from the chardev until our state is fully synced */
1303 if (!runstate_check(RUN_STATE_RUNNING
)) {
1307 /* usbredir_parser_do_read will consume *all* data we give it */
1311 static void usbredir_chardev_read(void *opaque
, const uint8_t *buf
, int size
)
1313 USBRedirDevice
*dev
= opaque
;
1315 /* No recursion allowed! */
1316 assert(dev
->read_buf
== NULL
);
1318 dev
->read_buf
= buf
;
1319 dev
->read_buf_size
= size
;
1321 usbredirparser_do_read(dev
->parser
);
1322 /* Send any acks, etc. which may be queued now */
1323 usbredirparser_do_write(dev
->parser
);
1326 static void usbredir_chardev_event(void *opaque
, int event
)
1328 USBRedirDevice
*dev
= opaque
;
1331 case CHR_EVENT_OPENED
:
1332 DPRINTF("chardev open\n");
1333 /* Make sure any pending closes are handled (no-op if none pending) */
1334 usbredir_chardev_close_bh(dev
);
1335 qemu_bh_cancel(dev
->chardev_close_bh
);
1336 usbredir_create_parser(dev
);
1338 case CHR_EVENT_CLOSED
:
1339 DPRINTF("chardev close\n");
1340 qemu_bh_schedule(dev
->chardev_close_bh
);
1349 static void usbredir_vm_state_change(void *priv
, int running
, RunState state
)
1351 USBRedirDevice
*dev
= priv
;
1353 if (state
== RUN_STATE_RUNNING
&& dev
->parser
!= NULL
) {
1354 usbredirparser_do_write(dev
->parser
); /* Flush any pending writes */
1358 static void usbredir_init_endpoints(USBRedirDevice
*dev
)
1362 usb_ep_init(&dev
->dev
);
1363 memset(dev
->endpoint
, 0, sizeof(dev
->endpoint
));
1364 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1365 dev
->endpoint
[i
].dev
= dev
;
1366 QTAILQ_INIT(&dev
->endpoint
[i
].bufpq
);
1370 static void usbredir_realize(USBDevice
*udev
, Error
**errp
)
1372 USBRedirDevice
*dev
= USB_REDIRECT(udev
);
1375 if (dev
->cs
== NULL
) {
1376 error_setg(errp
, QERR_MISSING_PARAMETER
, "chardev");
1380 if (dev
->filter_str
) {
1381 i
= usbredirfilter_string_to_rules(dev
->filter_str
, ":", "|",
1383 &dev
->filter_rules_count
);
1385 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "filter",
1386 "a usb device filter string");
1391 dev
->chardev_close_bh
= qemu_bh_new(usbredir_chardev_close_bh
, dev
);
1392 dev
->device_reject_bh
= qemu_bh_new(usbredir_device_reject_bh
, dev
);
1393 dev
->attach_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, usbredir_do_attach
, dev
);
1395 packet_id_queue_init(&dev
->cancelled
, dev
, "cancelled");
1396 packet_id_queue_init(&dev
->already_in_flight
, dev
, "already-in-flight");
1397 usbredir_init_endpoints(dev
);
1399 /* We'll do the attach once we receive the speed from the usb-host */
1400 udev
->auto_attach
= 0;
1402 /* Will be cleared during setup when we find conflicts */
1403 dev
->compatible_speedmask
= USB_SPEED_MASK_FULL
| USB_SPEED_MASK_HIGH
;
1405 /* Let the backend know we are ready */
1406 qemu_chr_add_handlers(dev
->cs
, usbredir_chardev_can_read
,
1407 usbredir_chardev_read
, usbredir_chardev_event
, dev
);
1409 qemu_add_vm_change_state_handler(usbredir_vm_state_change
, dev
);
1412 static void usbredir_cleanup_device_queues(USBRedirDevice
*dev
)
1416 packet_id_queue_empty(&dev
->cancelled
);
1417 packet_id_queue_empty(&dev
->already_in_flight
);
1418 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1419 usbredir_free_bufpq(dev
, I2EP(i
));
1423 static void usbredir_handle_destroy(USBDevice
*udev
)
1425 USBRedirDevice
*dev
= USB_REDIRECT(udev
);
1427 qemu_chr_delete(dev
->cs
);
1429 /* Note must be done after qemu_chr_close, as that causes a close event */
1430 qemu_bh_delete(dev
->chardev_close_bh
);
1431 qemu_bh_delete(dev
->device_reject_bh
);
1433 timer_del(dev
->attach_timer
);
1434 timer_free(dev
->attach_timer
);
1436 usbredir_cleanup_device_queues(dev
);
1439 usbredirparser_destroy(dev
->parser
);
1442 g_source_remove(dev
->watch
);
1445 free(dev
->filter_rules
);
1448 static int usbredir_check_filter(USBRedirDevice
*dev
)
1450 if (dev
->interface_info
.interface_count
== NO_INTERFACE_INFO
) {
1451 ERROR("No interface info for device\n");
1455 if (dev
->filter_rules
) {
1456 if (!usbredirparser_peer_has_cap(dev
->parser
,
1457 usb_redir_cap_connect_device_version
)) {
1458 ERROR("Device filter specified and peer does not have the "
1459 "connect_device_version capability\n");
1463 if (usbredirfilter_check(
1465 dev
->filter_rules_count
,
1466 dev
->device_info
.device_class
,
1467 dev
->device_info
.device_subclass
,
1468 dev
->device_info
.device_protocol
,
1469 dev
->interface_info
.interface_class
,
1470 dev
->interface_info
.interface_subclass
,
1471 dev
->interface_info
.interface_protocol
,
1472 dev
->interface_info
.interface_count
,
1473 dev
->device_info
.vendor_id
,
1474 dev
->device_info
.product_id
,
1475 dev
->device_info
.device_version_bcd
,
1484 usbredir_reject_device(dev
);
1488 static void usbredir_check_bulk_receiving(USBRedirDevice
*dev
)
1492 if (!usbredirparser_peer_has_cap(dev
->parser
,
1493 usb_redir_cap_bulk_receiving
)) {
1497 for (i
= EP2I(USB_DIR_IN
); i
< MAX_ENDPOINTS
; i
++) {
1498 dev
->endpoint
[i
].bulk_receiving_enabled
= 0;
1500 for (i
= 0; i
< dev
->interface_info
.interface_count
; i
++) {
1501 quirks
= usb_get_quirks(dev
->device_info
.vendor_id
,
1502 dev
->device_info
.product_id
,
1503 dev
->interface_info
.interface_class
[i
],
1504 dev
->interface_info
.interface_subclass
[i
],
1505 dev
->interface_info
.interface_protocol
[i
]);
1506 if (!(quirks
& USB_QUIRK_BUFFER_BULK_IN
)) {
1509 if (quirks
& USB_QUIRK_IS_FTDI
) {
1510 dev
->buffered_bulk_in_complete
=
1511 usbredir_buffered_bulk_in_complete_ftdi
;
1513 dev
->buffered_bulk_in_complete
=
1514 usbredir_buffered_bulk_in_complete_raw
;
1517 for (j
= EP2I(USB_DIR_IN
); j
< MAX_ENDPOINTS
; j
++) {
1518 if (dev
->endpoint
[j
].interface
==
1519 dev
->interface_info
.interface
[i
] &&
1520 dev
->endpoint
[j
].type
== USB_ENDPOINT_XFER_BULK
&&
1521 dev
->endpoint
[j
].max_packet_size
!= 0) {
1522 dev
->endpoint
[j
].bulk_receiving_enabled
= 1;
1524 * With buffering pipelining is not necessary. Also packet
1525 * combining and bulk in buffering don't play nice together!
1527 I2USBEP(dev
, j
)->pipeline
= false;
1528 break; /* Only buffer for the first ep of each intf */
1535 * usbredirparser packet complete callbacks
1538 static void usbredir_handle_status(USBRedirDevice
*dev
, USBPacket
*p
,
1542 case usb_redir_success
:
1543 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
1545 case usb_redir_stall
:
1546 p
->status
= USB_RET_STALL
;
1548 case usb_redir_cancelled
:
1550 * When the usbredir-host unredirects a device, it will report a status
1551 * of cancelled for all pending packets, followed by a disconnect msg.
1553 p
->status
= USB_RET_IOERROR
;
1555 case usb_redir_inval
:
1556 WARNING("got invalid param error from usb-host?\n");
1557 p
->status
= USB_RET_IOERROR
;
1559 case usb_redir_babble
:
1560 p
->status
= USB_RET_BABBLE
;
1562 case usb_redir_ioerror
:
1563 case usb_redir_timeout
:
1565 p
->status
= USB_RET_IOERROR
;
1569 static void usbredir_hello(void *priv
, struct usb_redir_hello_header
*h
)
1571 USBRedirDevice
*dev
= priv
;
1573 /* Try to send the filter info now that we've the usb-host's caps */
1574 if (usbredirparser_peer_has_cap(dev
->parser
, usb_redir_cap_filter
) &&
1575 dev
->filter_rules
) {
1576 usbredirparser_send_filter_filter(dev
->parser
, dev
->filter_rules
,
1577 dev
->filter_rules_count
);
1578 usbredirparser_do_write(dev
->parser
);
1582 static void usbredir_device_connect(void *priv
,
1583 struct usb_redir_device_connect_header
*device_connect
)
1585 USBRedirDevice
*dev
= priv
;
1588 if (timer_pending(dev
->attach_timer
) || dev
->dev
.attached
) {
1589 ERROR("Received device connect while already connected\n");
1593 switch (device_connect
->speed
) {
1594 case usb_redir_speed_low
:
1595 speed
= "low speed";
1596 dev
->dev
.speed
= USB_SPEED_LOW
;
1597 dev
->compatible_speedmask
&= ~USB_SPEED_MASK_FULL
;
1598 dev
->compatible_speedmask
&= ~USB_SPEED_MASK_HIGH
;
1600 case usb_redir_speed_full
:
1601 speed
= "full speed";
1602 dev
->dev
.speed
= USB_SPEED_FULL
;
1603 dev
->compatible_speedmask
&= ~USB_SPEED_MASK_HIGH
;
1605 case usb_redir_speed_high
:
1606 speed
= "high speed";
1607 dev
->dev
.speed
= USB_SPEED_HIGH
;
1609 case usb_redir_speed_super
:
1610 speed
= "super speed";
1611 dev
->dev
.speed
= USB_SPEED_SUPER
;
1614 speed
= "unknown speed";
1615 dev
->dev
.speed
= USB_SPEED_FULL
;
1618 if (usbredirparser_peer_has_cap(dev
->parser
,
1619 usb_redir_cap_connect_device_version
)) {
1620 INFO("attaching %s device %04x:%04x version %d.%d class %02x\n",
1621 speed
, device_connect
->vendor_id
, device_connect
->product_id
,
1622 ((device_connect
->device_version_bcd
& 0xf000) >> 12) * 10 +
1623 ((device_connect
->device_version_bcd
& 0x0f00) >> 8),
1624 ((device_connect
->device_version_bcd
& 0x00f0) >> 4) * 10 +
1625 ((device_connect
->device_version_bcd
& 0x000f) >> 0),
1626 device_connect
->device_class
);
1628 INFO("attaching %s device %04x:%04x class %02x\n", speed
,
1629 device_connect
->vendor_id
, device_connect
->product_id
,
1630 device_connect
->device_class
);
1633 dev
->dev
.speedmask
= (1 << dev
->dev
.speed
) | dev
->compatible_speedmask
;
1634 dev
->device_info
= *device_connect
;
1636 if (usbredir_check_filter(dev
)) {
1637 WARNING("Device %04x:%04x rejected by device filter, not attaching\n",
1638 device_connect
->vendor_id
, device_connect
->product_id
);
1642 usbredir_check_bulk_receiving(dev
);
1643 timer_mod(dev
->attach_timer
, dev
->next_attach_time
);
1646 static void usbredir_device_disconnect(void *priv
)
1648 USBRedirDevice
*dev
= priv
;
1650 /* Stop any pending attaches */
1651 timer_del(dev
->attach_timer
);
1653 if (dev
->dev
.attached
) {
1654 DPRINTF("detaching device\n");
1655 usb_device_detach(&dev
->dev
);
1657 * Delay next usb device attach to give the guest a chance to see
1658 * see the detach / attach in case of quick close / open succession
1660 dev
->next_attach_time
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + 200;
1663 /* Reset state so that the next dev connected starts with a clean slate */
1664 usbredir_cleanup_device_queues(dev
);
1665 usbredir_init_endpoints(dev
);
1666 dev
->interface_info
.interface_count
= NO_INTERFACE_INFO
;
1669 dev
->compatible_speedmask
= USB_SPEED_MASK_FULL
| USB_SPEED_MASK_HIGH
;
1672 static void usbredir_interface_info(void *priv
,
1673 struct usb_redir_interface_info_header
*interface_info
)
1675 USBRedirDevice
*dev
= priv
;
1677 dev
->interface_info
= *interface_info
;
1680 * If we receive interface info after the device has already been
1681 * connected (ie on a set_config), re-check interface dependent things.
1683 if (timer_pending(dev
->attach_timer
) || dev
->dev
.attached
) {
1684 usbredir_check_bulk_receiving(dev
);
1685 if (usbredir_check_filter(dev
)) {
1686 ERROR("Device no longer matches filter after interface info "
1687 "change, disconnecting!\n");
1692 static void usbredir_mark_speed_incompatible(USBRedirDevice
*dev
, int speed
)
1694 dev
->compatible_speedmask
&= ~(1 << speed
);
1695 dev
->dev
.speedmask
= (1 << dev
->dev
.speed
) | dev
->compatible_speedmask
;
1698 static void usbredir_set_pipeline(USBRedirDevice
*dev
, struct USBEndpoint
*uep
)
1700 if (uep
->type
!= USB_ENDPOINT_XFER_BULK
) {
1703 if (uep
->pid
== USB_TOKEN_OUT
) {
1704 uep
->pipeline
= true;
1706 if (uep
->pid
== USB_TOKEN_IN
&& uep
->max_packet_size
!= 0 &&
1707 usbredirparser_peer_has_cap(dev
->parser
,
1708 usb_redir_cap_32bits_bulk_length
)) {
1709 uep
->pipeline
= true;
1713 static void usbredir_setup_usb_eps(USBRedirDevice
*dev
)
1715 struct USBEndpoint
*usb_ep
;
1718 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1719 usb_ep
= I2USBEP(dev
, i
);
1720 usb_ep
->type
= dev
->endpoint
[i
].type
;
1721 usb_ep
->ifnum
= dev
->endpoint
[i
].interface
;
1722 usb_ep
->max_packet_size
= dev
->endpoint
[i
].max_packet_size
;
1723 usb_ep
->max_streams
= dev
->endpoint
[i
].max_streams
;
1724 usbredir_set_pipeline(dev
, usb_ep
);
1728 static void usbredir_ep_info(void *priv
,
1729 struct usb_redir_ep_info_header
*ep_info
)
1731 USBRedirDevice
*dev
= priv
;
1734 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1735 dev
->endpoint
[i
].type
= ep_info
->type
[i
];
1736 dev
->endpoint
[i
].interval
= ep_info
->interval
[i
];
1737 dev
->endpoint
[i
].interface
= ep_info
->interface
[i
];
1738 if (usbredirparser_peer_has_cap(dev
->parser
,
1739 usb_redir_cap_ep_info_max_packet_size
)) {
1740 dev
->endpoint
[i
].max_packet_size
= ep_info
->max_packet_size
[i
];
1742 #if USBREDIR_VERSION >= 0x000700
1743 if (usbredirparser_peer_has_cap(dev
->parser
,
1744 usb_redir_cap_bulk_streams
)) {
1745 dev
->endpoint
[i
].max_streams
= ep_info
->max_streams
[i
];
1748 switch (dev
->endpoint
[i
].type
) {
1749 case usb_redir_type_invalid
:
1751 case usb_redir_type_iso
:
1752 usbredir_mark_speed_incompatible(dev
, USB_SPEED_FULL
);
1753 usbredir_mark_speed_incompatible(dev
, USB_SPEED_HIGH
);
1755 case usb_redir_type_interrupt
:
1756 if (!usbredirparser_peer_has_cap(dev
->parser
,
1757 usb_redir_cap_ep_info_max_packet_size
) ||
1758 ep_info
->max_packet_size
[i
] > 64) {
1759 usbredir_mark_speed_incompatible(dev
, USB_SPEED_FULL
);
1761 if (!usbredirparser_peer_has_cap(dev
->parser
,
1762 usb_redir_cap_ep_info_max_packet_size
) ||
1763 ep_info
->max_packet_size
[i
] > 1024) {
1764 usbredir_mark_speed_incompatible(dev
, USB_SPEED_HIGH
);
1766 if (dev
->endpoint
[i
].interval
== 0) {
1767 ERROR("Received 0 interval for isoc or irq endpoint\n");
1768 usbredir_reject_device(dev
);
1772 case usb_redir_type_control
:
1773 case usb_redir_type_bulk
:
1774 DPRINTF("ep: %02X type: %d interface: %d\n", I2EP(i
),
1775 dev
->endpoint
[i
].type
, dev
->endpoint
[i
].interface
);
1778 ERROR("Received invalid endpoint type\n");
1779 usbredir_reject_device(dev
);
1783 /* The new ep info may have caused a speed incompatibility, recheck */
1784 if (dev
->dev
.attached
&&
1785 !(dev
->dev
.port
->speedmask
& dev
->dev
.speedmask
)) {
1786 ERROR("Device no longer matches speed after endpoint info change, "
1787 "disconnecting!\n");
1788 usbredir_reject_device(dev
);
1791 usbredir_setup_usb_eps(dev
);
1792 usbredir_check_bulk_receiving(dev
);
1795 static void usbredir_configuration_status(void *priv
, uint64_t id
,
1796 struct usb_redir_configuration_status_header
*config_status
)
1798 USBRedirDevice
*dev
= priv
;
1801 DPRINTF("set config status %d config %d id %"PRIu64
"\n",
1802 config_status
->status
, config_status
->configuration
, id
);
1804 p
= usbredir_find_packet_by_id(dev
, 0, id
);
1806 if (dev
->dev
.setup_buf
[0] & USB_DIR_IN
) {
1807 dev
->dev
.data_buf
[0] = config_status
->configuration
;
1808 p
->actual_length
= 1;
1810 usbredir_handle_status(dev
, p
, config_status
->status
);
1811 usb_generic_async_ctrl_complete(&dev
->dev
, p
);
1815 static void usbredir_alt_setting_status(void *priv
, uint64_t id
,
1816 struct usb_redir_alt_setting_status_header
*alt_setting_status
)
1818 USBRedirDevice
*dev
= priv
;
1821 DPRINTF("alt status %d intf %d alt %d id: %"PRIu64
"\n",
1822 alt_setting_status
->status
, alt_setting_status
->interface
,
1823 alt_setting_status
->alt
, id
);
1825 p
= usbredir_find_packet_by_id(dev
, 0, id
);
1827 if (dev
->dev
.setup_buf
[0] & USB_DIR_IN
) {
1828 dev
->dev
.data_buf
[0] = alt_setting_status
->alt
;
1829 p
->actual_length
= 1;
1831 usbredir_handle_status(dev
, p
, alt_setting_status
->status
);
1832 usb_generic_async_ctrl_complete(&dev
->dev
, p
);
1836 static void usbredir_iso_stream_status(void *priv
, uint64_t id
,
1837 struct usb_redir_iso_stream_status_header
*iso_stream_status
)
1839 USBRedirDevice
*dev
= priv
;
1840 uint8_t ep
= iso_stream_status
->endpoint
;
1842 DPRINTF("iso status %d ep %02X id %"PRIu64
"\n", iso_stream_status
->status
,
1845 if (!dev
->dev
.attached
|| !dev
->endpoint
[EP2I(ep
)].iso_started
) {
1849 dev
->endpoint
[EP2I(ep
)].iso_error
= iso_stream_status
->status
;
1850 if (iso_stream_status
->status
== usb_redir_stall
) {
1851 DPRINTF("iso stream stopped by peer ep %02X\n", ep
);
1852 dev
->endpoint
[EP2I(ep
)].iso_started
= 0;
1856 static void usbredir_interrupt_receiving_status(void *priv
, uint64_t id
,
1857 struct usb_redir_interrupt_receiving_status_header
1858 *interrupt_receiving_status
)
1860 USBRedirDevice
*dev
= priv
;
1861 uint8_t ep
= interrupt_receiving_status
->endpoint
;
1863 DPRINTF("interrupt recv status %d ep %02X id %"PRIu64
"\n",
1864 interrupt_receiving_status
->status
, ep
, id
);
1866 if (!dev
->dev
.attached
|| !dev
->endpoint
[EP2I(ep
)].interrupt_started
) {
1870 dev
->endpoint
[EP2I(ep
)].interrupt_error
=
1871 interrupt_receiving_status
->status
;
1872 if (interrupt_receiving_status
->status
== usb_redir_stall
) {
1873 DPRINTF("interrupt receiving stopped by peer ep %02X\n", ep
);
1874 dev
->endpoint
[EP2I(ep
)].interrupt_started
= 0;
1878 static void usbredir_bulk_streams_status(void *priv
, uint64_t id
,
1879 struct usb_redir_bulk_streams_status_header
*bulk_streams_status
)
1881 #if USBREDIR_VERSION >= 0x000700
1882 USBRedirDevice
*dev
= priv
;
1884 if (bulk_streams_status
->status
== usb_redir_success
) {
1885 DPRINTF("bulk streams status %d eps %08x\n",
1886 bulk_streams_status
->status
, bulk_streams_status
->endpoints
);
1888 ERROR("bulk streams %s failed status %d eps %08x\n",
1889 (bulk_streams_status
->no_streams
== 0) ? "free" : "alloc",
1890 bulk_streams_status
->status
, bulk_streams_status
->endpoints
);
1891 ERROR("usb-redir-host does not provide streams, disconnecting\n");
1892 usbredir_reject_device(dev
);
1897 static void usbredir_bulk_receiving_status(void *priv
, uint64_t id
,
1898 struct usb_redir_bulk_receiving_status_header
*bulk_receiving_status
)
1900 USBRedirDevice
*dev
= priv
;
1901 uint8_t ep
= bulk_receiving_status
->endpoint
;
1903 DPRINTF("bulk recv status %d ep %02X id %"PRIu64
"\n",
1904 bulk_receiving_status
->status
, ep
, id
);
1906 if (!dev
->dev
.attached
|| !dev
->endpoint
[EP2I(ep
)].bulk_receiving_started
) {
1910 if (bulk_receiving_status
->status
== usb_redir_stall
) {
1911 DPRINTF("bulk receiving stopped by peer ep %02X\n", ep
);
1912 dev
->endpoint
[EP2I(ep
)].bulk_receiving_started
= 0;
1916 static void usbredir_control_packet(void *priv
, uint64_t id
,
1917 struct usb_redir_control_packet_header
*control_packet
,
1918 uint8_t *data
, int data_len
)
1920 USBRedirDevice
*dev
= priv
;
1922 int len
= control_packet
->length
;
1924 DPRINTF("ctrl-in status %d len %d id %"PRIu64
"\n", control_packet
->status
,
1927 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
1928 * to work redirected to a not superspeed capable hcd */
1929 if (dev
->dev
.speed
== USB_SPEED_SUPER
&&
1930 !((dev
->dev
.port
->speedmask
& USB_SPEED_MASK_SUPER
)) &&
1931 control_packet
->requesttype
== 0x80 &&
1932 control_packet
->request
== 6 &&
1933 control_packet
->value
== 0x100 && control_packet
->index
== 0 &&
1934 data_len
>= 18 && data
[7] == 9) {
1938 p
= usbredir_find_packet_by_id(dev
, 0, id
);
1940 usbredir_handle_status(dev
, p
, control_packet
->status
);
1942 usbredir_log_data(dev
, "ctrl data in:", data
, data_len
);
1943 if (data_len
> sizeof(dev
->dev
.data_buf
)) {
1944 ERROR("ctrl buffer too small (%d > %zu)\n",
1945 data_len
, sizeof(dev
->dev
.data_buf
));
1946 p
->status
= USB_RET_STALL
;
1947 data_len
= len
= sizeof(dev
->dev
.data_buf
);
1949 memcpy(dev
->dev
.data_buf
, data
, data_len
);
1951 p
->actual_length
= len
;
1952 usb_generic_async_ctrl_complete(&dev
->dev
, p
);
1957 static void usbredir_bulk_packet(void *priv
, uint64_t id
,
1958 struct usb_redir_bulk_packet_header
*bulk_packet
,
1959 uint8_t *data
, int data_len
)
1961 USBRedirDevice
*dev
= priv
;
1962 uint8_t ep
= bulk_packet
->endpoint
;
1963 int len
= (bulk_packet
->length_high
<< 16) | bulk_packet
->length
;
1966 DPRINTF("bulk-in status %d ep %02X stream %u len %d id %"PRIu64
"\n",
1967 bulk_packet
->status
, ep
, bulk_packet
->stream_id
, len
, id
);
1969 p
= usbredir_find_packet_by_id(dev
, ep
, id
);
1971 size_t size
= usb_packet_size(p
);
1972 usbredir_handle_status(dev
, p
, bulk_packet
->status
);
1974 usbredir_log_data(dev
, "bulk data in:", data
, data_len
);
1975 if (data_len
> size
) {
1976 ERROR("bulk got more data then requested (%d > %zd)\n",
1977 data_len
, p
->iov
.size
);
1978 p
->status
= USB_RET_BABBLE
;
1979 data_len
= len
= size
;
1981 usb_packet_copy(p
, data
, data_len
);
1983 p
->actual_length
= len
;
1984 if (p
->pid
== USB_TOKEN_IN
&& p
->ep
->pipeline
) {
1985 usb_combined_input_packet_complete(&dev
->dev
, p
);
1987 usb_packet_complete(&dev
->dev
, p
);
1993 static void usbredir_iso_packet(void *priv
, uint64_t id
,
1994 struct usb_redir_iso_packet_header
*iso_packet
,
1995 uint8_t *data
, int data_len
)
1997 USBRedirDevice
*dev
= priv
;
1998 uint8_t ep
= iso_packet
->endpoint
;
2000 DPRINTF2("iso-in status %d ep %02X len %d id %"PRIu64
"\n",
2001 iso_packet
->status
, ep
, data_len
, id
);
2003 if (dev
->endpoint
[EP2I(ep
)].type
!= USB_ENDPOINT_XFER_ISOC
) {
2004 ERROR("received iso packet for non iso endpoint %02X\n", ep
);
2009 if (dev
->endpoint
[EP2I(ep
)].iso_started
== 0) {
2010 DPRINTF("received iso packet for non started stream ep %02X\n", ep
);
2015 /* bufp_alloc also adds the packet to the ep queue */
2016 bufp_alloc(dev
, data
, data_len
, iso_packet
->status
, ep
, data
);
2019 static void usbredir_interrupt_packet(void *priv
, uint64_t id
,
2020 struct usb_redir_interrupt_packet_header
*interrupt_packet
,
2021 uint8_t *data
, int data_len
)
2023 USBRedirDevice
*dev
= priv
;
2024 uint8_t ep
= interrupt_packet
->endpoint
;
2026 DPRINTF("interrupt-in status %d ep %02X len %d id %"PRIu64
"\n",
2027 interrupt_packet
->status
, ep
, data_len
, id
);
2029 if (dev
->endpoint
[EP2I(ep
)].type
!= USB_ENDPOINT_XFER_INT
) {
2030 ERROR("received int packet for non interrupt endpoint %02X\n", ep
);
2035 if (ep
& USB_DIR_IN
) {
2036 if (dev
->endpoint
[EP2I(ep
)].interrupt_started
== 0) {
2037 DPRINTF("received int packet while not started ep %02X\n", ep
);
2042 if (QTAILQ_EMPTY(&dev
->endpoint
[EP2I(ep
)].bufpq
)) {
2043 usb_wakeup(usb_ep_get(&dev
->dev
, USB_TOKEN_IN
, ep
& 0x0f), 0);
2046 /* bufp_alloc also adds the packet to the ep queue */
2047 bufp_alloc(dev
, data
, data_len
, interrupt_packet
->status
, ep
, data
);
2050 * We report output interrupt packets as completed directly upon
2051 * submission, so all we can do here if one failed is warn.
2053 if (interrupt_packet
->status
) {
2054 WARNING("interrupt output failed status %d ep %02X id %"PRIu64
"\n",
2055 interrupt_packet
->status
, ep
, id
);
2060 static void usbredir_buffered_bulk_packet(void *priv
, uint64_t id
,
2061 struct usb_redir_buffered_bulk_packet_header
*buffered_bulk_packet
,
2062 uint8_t *data
, int data_len
)
2064 USBRedirDevice
*dev
= priv
;
2065 uint8_t status
, ep
= buffered_bulk_packet
->endpoint
;
2066 void *free_on_destroy
;
2069 DPRINTF("buffered-bulk-in status %d ep %02X len %d id %"PRIu64
"\n",
2070 buffered_bulk_packet
->status
, ep
, data_len
, id
);
2072 if (dev
->endpoint
[EP2I(ep
)].type
!= USB_ENDPOINT_XFER_BULK
) {
2073 ERROR("received buffered-bulk packet for non bulk ep %02X\n", ep
);
2078 if (dev
->endpoint
[EP2I(ep
)].bulk_receiving_started
== 0) {
2079 DPRINTF("received buffered-bulk packet on not started ep %02X\n", ep
);
2084 /* Data must be in maxp chunks for buffered_bulk_add_*_data_to_packet */
2085 len
= dev
->endpoint
[EP2I(ep
)].max_packet_size
;
2086 status
= usb_redir_success
;
2087 free_on_destroy
= NULL
;
2088 for (i
= 0; i
< data_len
; i
+= len
) {
2090 if (len
>= (data_len
- i
)) {
2092 status
= buffered_bulk_packet
->status
;
2093 free_on_destroy
= data
;
2095 /* bufp_alloc also adds the packet to the ep queue */
2096 r
= bufp_alloc(dev
, data
+ i
, len
, status
, ep
, free_on_destroy
);
2102 if (dev
->endpoint
[EP2I(ep
)].pending_async_packet
) {
2103 USBPacket
*p
= dev
->endpoint
[EP2I(ep
)].pending_async_packet
;
2104 dev
->endpoint
[EP2I(ep
)].pending_async_packet
= NULL
;
2105 usbredir_buffered_bulk_in_complete(dev
, p
, ep
);
2106 usb_packet_complete(&dev
->dev
, p
);
2114 static void usbredir_pre_save(void *priv
)
2116 USBRedirDevice
*dev
= priv
;
2118 usbredir_fill_already_in_flight(dev
);
2121 static int usbredir_post_load(void *priv
, int version_id
)
2123 USBRedirDevice
*dev
= priv
;
2125 if (dev
->parser
== NULL
) {
2129 switch (dev
->device_info
.speed
) {
2130 case usb_redir_speed_low
:
2131 dev
->dev
.speed
= USB_SPEED_LOW
;
2133 case usb_redir_speed_full
:
2134 dev
->dev
.speed
= USB_SPEED_FULL
;
2136 case usb_redir_speed_high
:
2137 dev
->dev
.speed
= USB_SPEED_HIGH
;
2139 case usb_redir_speed_super
:
2140 dev
->dev
.speed
= USB_SPEED_SUPER
;
2143 dev
->dev
.speed
= USB_SPEED_FULL
;
2145 dev
->dev
.speedmask
= (1 << dev
->dev
.speed
);
2147 usbredir_setup_usb_eps(dev
);
2148 usbredir_check_bulk_receiving(dev
);
2153 /* For usbredirparser migration */
2154 static void usbredir_put_parser(QEMUFile
*f
, void *priv
, size_t unused
)
2156 USBRedirDevice
*dev
= priv
;
2160 if (dev
->parser
== NULL
) {
2161 qemu_put_be32(f
, 0);
2165 usbredirparser_serialize(dev
->parser
, &data
, &len
);
2166 qemu_oom_check(data
);
2168 qemu_put_be32(f
, len
);
2169 qemu_put_buffer(f
, data
, len
);
2174 static int usbredir_get_parser(QEMUFile
*f
, void *priv
, size_t unused
)
2176 USBRedirDevice
*dev
= priv
;
2180 len
= qemu_get_be32(f
);
2186 * If our chardev is not open already at this point the usbredir connection
2187 * has been broken (non seamless migration, or restore from disk).
2189 * In this case create a temporary parser to receive the migration data,
2190 * and schedule the close_bh to report the device as disconnected to the
2191 * guest and to destroy the parser again.
2193 if (dev
->parser
== NULL
) {
2194 WARNING("usb-redir connection broken during migration\n");
2195 usbredir_create_parser(dev
);
2196 qemu_bh_schedule(dev
->chardev_close_bh
);
2199 data
= g_malloc(len
);
2200 qemu_get_buffer(f
, data
, len
);
2202 ret
= usbredirparser_unserialize(dev
->parser
, data
, len
);
2209 static const VMStateInfo usbredir_parser_vmstate_info
= {
2210 .name
= "usb-redir-parser",
2211 .put
= usbredir_put_parser
,
2212 .get
= usbredir_get_parser
,
2216 /* For buffered packets (iso/irq) queue migration */
2217 static void usbredir_put_bufpq(QEMUFile
*f
, void *priv
, size_t unused
)
2219 struct endp_data
*endp
= priv
;
2220 USBRedirDevice
*dev
= endp
->dev
;
2221 struct buf_packet
*bufp
;
2224 qemu_put_be32(f
, endp
->bufpq_size
);
2225 QTAILQ_FOREACH(bufp
, &endp
->bufpq
, next
) {
2226 len
= bufp
->len
- bufp
->offset
;
2227 DPRINTF("put_bufpq %d/%d len %d status %d\n", i
+ 1, endp
->bufpq_size
,
2229 qemu_put_be32(f
, len
);
2230 qemu_put_be32(f
, bufp
->status
);
2231 qemu_put_buffer(f
, bufp
->data
+ bufp
->offset
, len
);
2234 assert(i
== endp
->bufpq_size
);
2237 static int usbredir_get_bufpq(QEMUFile
*f
, void *priv
, size_t unused
)
2239 struct endp_data
*endp
= priv
;
2240 USBRedirDevice
*dev
= endp
->dev
;
2241 struct buf_packet
*bufp
;
2244 endp
->bufpq_size
= qemu_get_be32(f
);
2245 for (i
= 0; i
< endp
->bufpq_size
; i
++) {
2246 bufp
= g_new(struct buf_packet
, 1);
2247 bufp
->len
= qemu_get_be32(f
);
2248 bufp
->status
= qemu_get_be32(f
);
2250 bufp
->data
= qemu_oom_check(malloc(bufp
->len
)); /* regular malloc! */
2251 bufp
->free_on_destroy
= bufp
->data
;
2252 qemu_get_buffer(f
, bufp
->data
, bufp
->len
);
2253 QTAILQ_INSERT_TAIL(&endp
->bufpq
, bufp
, next
);
2254 DPRINTF("get_bufpq %d/%d len %d status %d\n", i
+ 1, endp
->bufpq_size
,
2255 bufp
->len
, bufp
->status
);
2260 static const VMStateInfo usbredir_ep_bufpq_vmstate_info
= {
2261 .name
= "usb-redir-bufpq",
2262 .put
= usbredir_put_bufpq
,
2263 .get
= usbredir_get_bufpq
,
2267 /* For endp_data migration */
2268 static bool usbredir_bulk_receiving_needed(void *priv
)
2270 struct endp_data
*endp
= priv
;
2272 return endp
->bulk_receiving_started
;
2275 static const VMStateDescription usbredir_bulk_receiving_vmstate
= {
2276 .name
= "usb-redir-ep/bulk-receiving",
2278 .minimum_version_id
= 1,
2279 .needed
= usbredir_bulk_receiving_needed
,
2280 .fields
= (VMStateField
[]) {
2281 VMSTATE_UINT8(bulk_receiving_started
, struct endp_data
),
2282 VMSTATE_END_OF_LIST()
2286 static bool usbredir_stream_needed(void *priv
)
2288 struct endp_data
*endp
= priv
;
2290 return endp
->max_streams
;
2293 static const VMStateDescription usbredir_stream_vmstate
= {
2294 .name
= "usb-redir-ep/stream-state",
2296 .minimum_version_id
= 1,
2297 .needed
= usbredir_stream_needed
,
2298 .fields
= (VMStateField
[]) {
2299 VMSTATE_UINT32(max_streams
, struct endp_data
),
2300 VMSTATE_END_OF_LIST()
2304 static const VMStateDescription usbredir_ep_vmstate
= {
2305 .name
= "usb-redir-ep",
2307 .minimum_version_id
= 1,
2308 .fields
= (VMStateField
[]) {
2309 VMSTATE_UINT8(type
, struct endp_data
),
2310 VMSTATE_UINT8(interval
, struct endp_data
),
2311 VMSTATE_UINT8(interface
, struct endp_data
),
2312 VMSTATE_UINT16(max_packet_size
, struct endp_data
),
2313 VMSTATE_UINT8(iso_started
, struct endp_data
),
2314 VMSTATE_UINT8(iso_error
, struct endp_data
),
2315 VMSTATE_UINT8(interrupt_started
, struct endp_data
),
2316 VMSTATE_UINT8(interrupt_error
, struct endp_data
),
2317 VMSTATE_UINT8(bufpq_prefilled
, struct endp_data
),
2318 VMSTATE_UINT8(bufpq_dropping_packets
, struct endp_data
),
2322 .field_exists
= NULL
,
2324 .info
= &usbredir_ep_bufpq_vmstate_info
,
2325 .flags
= VMS_SINGLE
,
2328 VMSTATE_INT32(bufpq_target_size
, struct endp_data
),
2329 VMSTATE_END_OF_LIST()
2331 .subsections
= (const VMStateDescription
*[]) {
2332 &usbredir_bulk_receiving_vmstate
,
2333 &usbredir_stream_vmstate
,
2339 /* For PacketIdQueue migration */
2340 static void usbredir_put_packet_id_q(QEMUFile
*f
, void *priv
, size_t unused
)
2342 struct PacketIdQueue
*q
= priv
;
2343 USBRedirDevice
*dev
= q
->dev
;
2344 struct PacketIdQueueEntry
*e
;
2345 int remain
= q
->size
;
2347 DPRINTF("put_packet_id_q %s size %d\n", q
->name
, q
->size
);
2348 qemu_put_be32(f
, q
->size
);
2349 QTAILQ_FOREACH(e
, &q
->head
, next
) {
2350 qemu_put_be64(f
, e
->id
);
2353 assert(remain
== 0);
2356 static int usbredir_get_packet_id_q(QEMUFile
*f
, void *priv
, size_t unused
)
2358 struct PacketIdQueue
*q
= priv
;
2359 USBRedirDevice
*dev
= q
->dev
;
2363 size
= qemu_get_be32(f
);
2364 DPRINTF("get_packet_id_q %s size %d\n", q
->name
, size
);
2365 for (i
= 0; i
< size
; i
++) {
2366 id
= qemu_get_be64(f
);
2367 packet_id_queue_add(q
, id
);
2369 assert(q
->size
== size
);
2373 static const VMStateInfo usbredir_ep_packet_id_q_vmstate_info
= {
2374 .name
= "usb-redir-packet-id-q",
2375 .put
= usbredir_put_packet_id_q
,
2376 .get
= usbredir_get_packet_id_q
,
2379 static const VMStateDescription usbredir_ep_packet_id_queue_vmstate
= {
2380 .name
= "usb-redir-packet-id-queue",
2382 .minimum_version_id
= 1,
2383 .fields
= (VMStateField
[]) {
2387 .field_exists
= NULL
,
2389 .info
= &usbredir_ep_packet_id_q_vmstate_info
,
2390 .flags
= VMS_SINGLE
,
2393 VMSTATE_END_OF_LIST()
2398 /* For usb_redir_device_connect_header migration */
2399 static const VMStateDescription usbredir_device_info_vmstate
= {
2400 .name
= "usb-redir-device-info",
2402 .minimum_version_id
= 1,
2403 .fields
= (VMStateField
[]) {
2404 VMSTATE_UINT8(speed
, struct usb_redir_device_connect_header
),
2405 VMSTATE_UINT8(device_class
, struct usb_redir_device_connect_header
),
2406 VMSTATE_UINT8(device_subclass
, struct usb_redir_device_connect_header
),
2407 VMSTATE_UINT8(device_protocol
, struct usb_redir_device_connect_header
),
2408 VMSTATE_UINT16(vendor_id
, struct usb_redir_device_connect_header
),
2409 VMSTATE_UINT16(product_id
, struct usb_redir_device_connect_header
),
2410 VMSTATE_UINT16(device_version_bcd
,
2411 struct usb_redir_device_connect_header
),
2412 VMSTATE_END_OF_LIST()
2417 /* For usb_redir_interface_info_header migration */
2418 static const VMStateDescription usbredir_interface_info_vmstate
= {
2419 .name
= "usb-redir-interface-info",
2421 .minimum_version_id
= 1,
2422 .fields
= (VMStateField
[]) {
2423 VMSTATE_UINT32(interface_count
,
2424 struct usb_redir_interface_info_header
),
2425 VMSTATE_UINT8_ARRAY(interface
,
2426 struct usb_redir_interface_info_header
, 32),
2427 VMSTATE_UINT8_ARRAY(interface_class
,
2428 struct usb_redir_interface_info_header
, 32),
2429 VMSTATE_UINT8_ARRAY(interface_subclass
,
2430 struct usb_redir_interface_info_header
, 32),
2431 VMSTATE_UINT8_ARRAY(interface_protocol
,
2432 struct usb_redir_interface_info_header
, 32),
2433 VMSTATE_END_OF_LIST()
2438 /* And finally the USBRedirDevice vmstate itself */
2439 static const VMStateDescription usbredir_vmstate
= {
2440 .name
= "usb-redir",
2442 .minimum_version_id
= 1,
2443 .pre_save
= usbredir_pre_save
,
2444 .post_load
= usbredir_post_load
,
2445 .fields
= (VMStateField
[]) {
2446 VMSTATE_USB_DEVICE(dev
, USBRedirDevice
),
2447 VMSTATE_TIMER_PTR(attach_timer
, USBRedirDevice
),
2451 .field_exists
= NULL
,
2453 .info
= &usbredir_parser_vmstate_info
,
2454 .flags
= VMS_SINGLE
,
2457 VMSTATE_STRUCT_ARRAY(endpoint
, USBRedirDevice
, MAX_ENDPOINTS
, 1,
2458 usbredir_ep_vmstate
, struct endp_data
),
2459 VMSTATE_STRUCT(cancelled
, USBRedirDevice
, 1,
2460 usbredir_ep_packet_id_queue_vmstate
,
2461 struct PacketIdQueue
),
2462 VMSTATE_STRUCT(already_in_flight
, USBRedirDevice
, 1,
2463 usbredir_ep_packet_id_queue_vmstate
,
2464 struct PacketIdQueue
),
2465 VMSTATE_STRUCT(device_info
, USBRedirDevice
, 1,
2466 usbredir_device_info_vmstate
,
2467 struct usb_redir_device_connect_header
),
2468 VMSTATE_STRUCT(interface_info
, USBRedirDevice
, 1,
2469 usbredir_interface_info_vmstate
,
2470 struct usb_redir_interface_info_header
),
2471 VMSTATE_END_OF_LIST()
2475 static Property usbredir_properties
[] = {
2476 DEFINE_PROP_CHR("chardev", USBRedirDevice
, cs
),
2477 DEFINE_PROP_UINT8("debug", USBRedirDevice
, debug
, usbredirparser_warning
),
2478 DEFINE_PROP_STRING("filter", USBRedirDevice
, filter_str
),
2479 DEFINE_PROP_END_OF_LIST(),
2482 static void usbredir_class_initfn(ObjectClass
*klass
, void *data
)
2484 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
2485 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2487 uc
->realize
= usbredir_realize
;
2488 uc
->product_desc
= "USB Redirection Device";
2489 uc
->handle_destroy
= usbredir_handle_destroy
;
2490 uc
->cancel_packet
= usbredir_cancel_packet
;
2491 uc
->handle_reset
= usbredir_handle_reset
;
2492 uc
->handle_data
= usbredir_handle_data
;
2493 uc
->handle_control
= usbredir_handle_control
;
2494 uc
->flush_ep_queue
= usbredir_flush_ep_queue
;
2495 uc
->ep_stopped
= usbredir_ep_stopped
;
2496 uc
->alloc_streams
= usbredir_alloc_streams
;
2497 uc
->free_streams
= usbredir_free_streams
;
2498 dc
->vmsd
= &usbredir_vmstate
;
2499 dc
->props
= usbredir_properties
;
2500 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
2503 static void usbredir_instance_init(Object
*obj
)
2505 USBDevice
*udev
= USB_DEVICE(obj
);
2506 USBRedirDevice
*dev
= USB_REDIRECT(udev
);
2508 device_add_bootindex_property(obj
, &dev
->bootindex
,
2513 static const TypeInfo usbredir_dev_info
= {
2514 .name
= TYPE_USB_REDIR
,
2515 .parent
= TYPE_USB_DEVICE
,
2516 .instance_size
= sizeof(USBRedirDevice
),
2517 .class_init
= usbredir_class_initfn
,
2518 .instance_init
= usbredir_instance_init
,
2521 static void usbredir_register_types(void)
2523 type_register_static(&usbredir_dev_info
);
2526 type_init(usbredir_register_types
)