2 * Linux host USB redirector
4 * Copyright (c) 2005 Fabrice Bellard
6 * Copyright (c) 2008 Max Krasnyansky
7 * Support for host device auto connect & disconnect
8 * Major rewrite to support fully async operation
10 * Copyright 2008 TJ <linux@tjworld.net>
11 * Added flexible support for /dev/bus/usb /sys/bus/usb/devices in addition
12 * to the legacy /proc/bus/usb USB device discovery and handling
14 * (c) 2012 Gerd Hoffmann <kraxel@redhat.com>
15 * Completely rewritten to use libusb instead of usbfs ioctls.
17 * Permission is hereby granted, free of charge, to any person obtaining a copy
18 * of this software and associated documentation files (the "Software"), to deal
19 * in the Software without restriction, including without limitation the rights
20 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21 * copies of the Software, and to permit persons to whom the Software is
22 * furnished to do so, subject to the following conditions:
24 * The above copyright notice and this permission notice shall be included in
25 * all copies or substantial portions of the Software.
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
36 #include "qemu/osdep.h"
42 #include "qapi/error.h"
43 #include "migration/vmstate.h"
44 #include "monitor/monitor.h"
45 #include "qemu/error-report.h"
46 #include "qemu/main-loop.h"
47 #include "qemu/module.h"
48 #include "sysemu/runstate.h"
49 #include "sysemu/sysemu.h"
52 #include "hw/qdev-properties.h"
55 /* ------------------------------------------------------------------------ */
57 #define TYPE_USB_HOST_DEVICE "usb-host"
58 #define USB_HOST_DEVICE(obj) \
59 OBJECT_CHECK(USBHostDevice, (obj), TYPE_USB_HOST_DEVICE)
61 typedef struct USBHostDevice USBHostDevice
;
62 typedef struct USBHostRequest USBHostRequest
;
63 typedef struct USBHostIsoXfer USBHostIsoXfer
;
64 typedef struct USBHostIsoRing USBHostIsoRing
;
66 struct USBAutoFilter
{
74 enum USBHostDeviceOptions
{
75 USB_HOST_OPT_PIPELINE
,
78 struct USBHostDevice
{
82 struct USBAutoFilter match
;
85 uint32_t iso_urb_count
;
86 uint32_t iso_urb_frames
;
90 bool allow_one_guest_reset
;
91 bool allow_all_guest_resets
;
92 bool suppress_remote_wake
;
95 QTAILQ_ENTRY(USBHostDevice
) next
;
103 libusb_device_handle
*dh
;
104 struct libusb_device_descriptor ddesc
;
109 } ifs
[USB_MAX_INTERFACES
];
111 /* callbacks & friends */
114 bool bh_postld_pending
;
118 QTAILQ_HEAD(, USBHostRequest
) requests
;
119 QTAILQ_HEAD(, USBHostIsoRing
) isorings
;
122 struct USBHostRequest
{
126 struct libusb_transfer
*xfer
;
127 unsigned char *buffer
;
131 QTAILQ_ENTRY(USBHostRequest
) next
;
134 struct USBHostIsoXfer
{
135 USBHostIsoRing
*ring
;
136 struct libusb_transfer
*xfer
;
139 QTAILQ_ENTRY(USBHostIsoXfer
) next
;
142 struct USBHostIsoRing
{
145 QTAILQ_HEAD(, USBHostIsoXfer
) unused
;
146 QTAILQ_HEAD(, USBHostIsoXfer
) inflight
;
147 QTAILQ_HEAD(, USBHostIsoXfer
) copy
;
148 QTAILQ_ENTRY(USBHostIsoRing
) next
;
151 static QTAILQ_HEAD(, USBHostDevice
) hostdevs
=
152 QTAILQ_HEAD_INITIALIZER(hostdevs
);
154 static void usb_host_auto_check(void *unused
);
155 static void usb_host_release_interfaces(USBHostDevice
*s
);
156 static void usb_host_nodev(USBHostDevice
*s
);
157 static void usb_host_detach_kernel(USBHostDevice
*s
);
158 static void usb_host_attach_kernel(USBHostDevice
*s
);
160 /* ------------------------------------------------------------------------ */
162 #ifndef LIBUSB_LOG_LEVEL_WARNING /* older libusb didn't define these */
163 #define LIBUSB_LOG_LEVEL_WARNING 2
166 /* ------------------------------------------------------------------------ */
168 #define CONTROL_TIMEOUT 10000 /* 10 sec */
169 #define BULK_TIMEOUT 0 /* unlimited */
170 #define INTR_TIMEOUT 0 /* unlimited */
172 #ifndef LIBUSB_API_VERSION
173 # define LIBUSB_API_VERSION LIBUSBX_API_VERSION
175 #if LIBUSB_API_VERSION >= 0x01000103
176 # define HAVE_STREAMS 1
179 static const char *speed_name
[] = {
180 [LIBUSB_SPEED_UNKNOWN
] = "?",
181 [LIBUSB_SPEED_LOW
] = "1.5",
182 [LIBUSB_SPEED_FULL
] = "12",
183 [LIBUSB_SPEED_HIGH
] = "480",
184 [LIBUSB_SPEED_SUPER
] = "5000",
187 static const unsigned int speed_map
[] = {
188 [LIBUSB_SPEED_LOW
] = USB_SPEED_LOW
,
189 [LIBUSB_SPEED_FULL
] = USB_SPEED_FULL
,
190 [LIBUSB_SPEED_HIGH
] = USB_SPEED_HIGH
,
191 [LIBUSB_SPEED_SUPER
] = USB_SPEED_SUPER
,
194 static const unsigned int status_map
[] = {
195 [LIBUSB_TRANSFER_COMPLETED
] = USB_RET_SUCCESS
,
196 [LIBUSB_TRANSFER_ERROR
] = USB_RET_IOERROR
,
197 [LIBUSB_TRANSFER_TIMED_OUT
] = USB_RET_IOERROR
,
198 [LIBUSB_TRANSFER_CANCELLED
] = USB_RET_IOERROR
,
199 [LIBUSB_TRANSFER_STALL
] = USB_RET_STALL
,
200 [LIBUSB_TRANSFER_NO_DEVICE
] = USB_RET_NODEV
,
201 [LIBUSB_TRANSFER_OVERFLOW
] = USB_RET_BABBLE
,
204 static const char *err_names
[] = {
205 [-LIBUSB_ERROR_IO
] = "IO",
206 [-LIBUSB_ERROR_INVALID_PARAM
] = "INVALID_PARAM",
207 [-LIBUSB_ERROR_ACCESS
] = "ACCESS",
208 [-LIBUSB_ERROR_NO_DEVICE
] = "NO_DEVICE",
209 [-LIBUSB_ERROR_NOT_FOUND
] = "NOT_FOUND",
210 [-LIBUSB_ERROR_BUSY
] = "BUSY",
211 [-LIBUSB_ERROR_TIMEOUT
] = "TIMEOUT",
212 [-LIBUSB_ERROR_OVERFLOW
] = "OVERFLOW",
213 [-LIBUSB_ERROR_PIPE
] = "PIPE",
214 [-LIBUSB_ERROR_INTERRUPTED
] = "INTERRUPTED",
215 [-LIBUSB_ERROR_NO_MEM
] = "NO_MEM",
216 [-LIBUSB_ERROR_NOT_SUPPORTED
] = "NOT_SUPPORTED",
217 [-LIBUSB_ERROR_OTHER
] = "OTHER",
220 static libusb_context
*ctx
;
221 static uint32_t loglevel
;
225 static void usb_host_handle_fd(void *opaque
)
227 struct timeval tv
= { 0, 0 };
228 libusb_handle_events_timeout(ctx
, &tv
);
231 static void usb_host_add_fd(int fd
, short events
, void *user_data
)
233 qemu_set_fd_handler(fd
,
234 (events
& POLLIN
) ? usb_host_handle_fd
: NULL
,
235 (events
& POLLOUT
) ? usb_host_handle_fd
: NULL
,
239 static void usb_host_del_fd(int fd
, void *user_data
)
241 qemu_set_fd_handler(fd
, NULL
, NULL
, NULL
);
244 #endif /* !CONFIG_WIN32 */
246 static int usb_host_init(void)
249 const struct libusb_pollfd
**poll
;
256 rc
= libusb_init(&ctx
);
260 #if LIBUSB_API_VERSION >= 0x01000106
261 libusb_set_option(ctx
, LIBUSB_OPTION_LOG_LEVEL
, loglevel
);
263 libusb_set_debug(ctx
, loglevel
);
266 /* FIXME: add support for Windows. */
268 libusb_set_pollfd_notifiers(ctx
, usb_host_add_fd
,
271 poll
= libusb_get_pollfds(ctx
);
274 for (i
= 0; poll
[i
] != NULL
; i
++) {
275 usb_host_add_fd(poll
[i
]->fd
, poll
[i
]->events
, ctx
);
283 static int usb_host_get_port(libusb_device
*dev
, char *port
, size_t len
)
289 #if LIBUSB_API_VERSION >= 0x01000102
290 rc
= libusb_get_port_numbers(dev
, path
, 7);
292 rc
= libusb_get_port_path(ctx
, dev
, path
, 7);
297 off
= snprintf(port
, len
, "%d", path
[0]);
298 for (i
= 1; i
< rc
; i
++) {
299 off
+= snprintf(port
+off
, len
-off
, ".%d", path
[i
]);
304 static void usb_host_libusb_error(const char *func
, int rc
)
312 if (-rc
< ARRAY_SIZE(err_names
) && err_names
[-rc
]) {
313 errname
= err_names
[-rc
];
317 error_report("%s: %d [%s]", func
, rc
, errname
);
320 /* ------------------------------------------------------------------------ */
322 static bool usb_host_use_combining(USBEndpoint
*ep
)
329 if (ep
->pid
!= USB_TOKEN_IN
) {
332 type
= usb_ep_get_type(ep
->dev
, ep
->pid
, ep
->nr
);
333 if (type
!= USB_ENDPOINT_XFER_BULK
) {
339 /* ------------------------------------------------------------------------ */
341 static USBHostRequest
*usb_host_req_alloc(USBHostDevice
*s
, USBPacket
*p
,
342 bool in
, size_t bufsize
)
344 USBHostRequest
*r
= g_new0(USBHostRequest
, 1);
349 r
->xfer
= libusb_alloc_transfer(0);
351 r
->buffer
= g_malloc(bufsize
);
353 QTAILQ_INSERT_TAIL(&s
->requests
, r
, next
);
357 static void usb_host_req_free(USBHostRequest
*r
)
359 QTAILQ_REMOVE(&r
->host
->requests
, r
, next
);
360 libusb_free_transfer(r
->xfer
);
365 static USBHostRequest
*usb_host_req_find(USBHostDevice
*s
, USBPacket
*p
)
369 QTAILQ_FOREACH(r
, &s
->requests
, next
) {
377 static void LIBUSB_CALL
usb_host_req_complete_ctrl(struct libusb_transfer
*xfer
)
379 USBHostRequest
*r
= xfer
->user_data
;
380 USBHostDevice
*s
= r
->host
;
381 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
384 goto out
; /* request was canceled */
387 r
->p
->status
= status_map
[xfer
->status
];
388 r
->p
->actual_length
= xfer
->actual_length
;
389 if (r
->in
&& xfer
->actual_length
) {
390 USBDevice
*udev
= USB_DEVICE(s
);
391 struct libusb_config_descriptor
*conf
= (void *)r
->cbuf
;
392 memcpy(r
->cbuf
, r
->buffer
+ 8, xfer
->actual_length
);
394 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
395 * to work redirected to a not superspeed capable hcd */
396 if (r
->usb3ep0quirk
&& xfer
->actual_length
>= 18 &&
401 *If this is GET_DESCRIPTOR request for configuration descriptor,
402 * remove 'remote wakeup' flag from it to prevent idle power down
405 if (s
->suppress_remote_wake
&&
406 udev
->setup_buf
[0] == USB_DIR_IN
&&
407 udev
->setup_buf
[1] == USB_REQ_GET_DESCRIPTOR
&&
408 udev
->setup_buf
[3] == USB_DT_CONFIG
&& udev
->setup_buf
[2] == 0 &&
409 xfer
->actual_length
>
410 offsetof(struct libusb_config_descriptor
, bmAttributes
) &&
411 (conf
->bmAttributes
& USB_CFG_ATT_WAKEUP
)) {
412 trace_usb_host_remote_wakeup_removed(s
->bus_num
, s
->addr
);
413 conf
->bmAttributes
&= ~USB_CFG_ATT_WAKEUP
;
416 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
417 r
->p
->status
, r
->p
->actual_length
);
418 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
421 usb_host_req_free(r
);
427 static void LIBUSB_CALL
usb_host_req_complete_data(struct libusb_transfer
*xfer
)
429 USBHostRequest
*r
= xfer
->user_data
;
430 USBHostDevice
*s
= r
->host
;
431 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
434 goto out
; /* request was canceled */
437 r
->p
->status
= status_map
[xfer
->status
];
438 if (r
->in
&& xfer
->actual_length
) {
439 usb_packet_copy(r
->p
, r
->buffer
, xfer
->actual_length
);
441 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
442 r
->p
->status
, r
->p
->actual_length
);
443 if (usb_host_use_combining(r
->p
->ep
)) {
444 usb_combined_input_packet_complete(USB_DEVICE(s
), r
->p
);
446 usb_packet_complete(USB_DEVICE(s
), r
->p
);
450 usb_host_req_free(r
);
456 static void usb_host_req_abort(USBHostRequest
*r
)
458 USBHostDevice
*s
= r
->host
;
459 bool inflight
= (r
->p
&& r
->p
->state
== USB_PACKET_ASYNC
);
462 r
->p
->status
= USB_RET_NODEV
;
463 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
464 r
->p
->status
, r
->p
->actual_length
);
465 if (r
->p
->ep
->nr
== 0) {
466 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
468 usb_packet_complete(USB_DEVICE(s
), r
->p
);
472 libusb_cancel_transfer(r
->xfer
);
476 /* ------------------------------------------------------------------------ */
478 static void LIBUSB_CALL
479 usb_host_req_complete_iso(struct libusb_transfer
*transfer
)
481 USBHostIsoXfer
*xfer
= transfer
->user_data
;
484 /* USBHostIsoXfer released while inflight */
485 g_free(transfer
->buffer
);
486 libusb_free_transfer(transfer
);
490 QTAILQ_REMOVE(&xfer
->ring
->inflight
, xfer
, next
);
491 if (QTAILQ_EMPTY(&xfer
->ring
->inflight
)) {
492 USBHostDevice
*s
= xfer
->ring
->host
;
493 trace_usb_host_iso_stop(s
->bus_num
, s
->addr
, xfer
->ring
->ep
->nr
);
495 if (xfer
->ring
->ep
->pid
== USB_TOKEN_IN
) {
496 QTAILQ_INSERT_TAIL(&xfer
->ring
->copy
, xfer
, next
);
497 usb_wakeup(xfer
->ring
->ep
, 0);
499 QTAILQ_INSERT_TAIL(&xfer
->ring
->unused
, xfer
, next
);
503 static USBHostIsoRing
*usb_host_iso_alloc(USBHostDevice
*s
, USBEndpoint
*ep
)
505 USBHostIsoRing
*ring
= g_new0(USBHostIsoRing
, 1);
506 USBHostIsoXfer
*xfer
;
507 /* FIXME: check interval (for now assume one xfer per frame) */
508 int packets
= s
->iso_urb_frames
;
513 QTAILQ_INIT(&ring
->unused
);
514 QTAILQ_INIT(&ring
->inflight
);
515 QTAILQ_INIT(&ring
->copy
);
516 QTAILQ_INSERT_TAIL(&s
->isorings
, ring
, next
);
518 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
519 xfer
= g_new0(USBHostIsoXfer
, 1);
521 xfer
->xfer
= libusb_alloc_transfer(packets
);
522 xfer
->xfer
->dev_handle
= s
->dh
;
523 xfer
->xfer
->type
= LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
;
525 xfer
->xfer
->endpoint
= ring
->ep
->nr
;
526 if (ring
->ep
->pid
== USB_TOKEN_IN
) {
527 xfer
->xfer
->endpoint
|= USB_DIR_IN
;
529 xfer
->xfer
->callback
= usb_host_req_complete_iso
;
530 xfer
->xfer
->user_data
= xfer
;
532 xfer
->xfer
->num_iso_packets
= packets
;
533 xfer
->xfer
->length
= ring
->ep
->max_packet_size
* packets
;
534 xfer
->xfer
->buffer
= g_malloc0(xfer
->xfer
->length
);
536 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
542 static USBHostIsoRing
*usb_host_iso_find(USBHostDevice
*s
, USBEndpoint
*ep
)
544 USBHostIsoRing
*ring
;
546 QTAILQ_FOREACH(ring
, &s
->isorings
, next
) {
547 if (ring
->ep
== ep
) {
554 static void usb_host_iso_reset_xfer(USBHostIsoXfer
*xfer
)
556 libusb_set_iso_packet_lengths(xfer
->xfer
,
557 xfer
->ring
->ep
->max_packet_size
);
559 xfer
->copy_complete
= false;
562 static void usb_host_iso_free_xfer(USBHostIsoXfer
*xfer
, bool inflight
)
565 xfer
->xfer
->user_data
= NULL
;
567 g_free(xfer
->xfer
->buffer
);
568 libusb_free_transfer(xfer
->xfer
);
573 static void usb_host_iso_free(USBHostIsoRing
*ring
)
575 USBHostIsoXfer
*xfer
;
577 while ((xfer
= QTAILQ_FIRST(&ring
->inflight
)) != NULL
) {
578 QTAILQ_REMOVE(&ring
->inflight
, xfer
, next
);
579 usb_host_iso_free_xfer(xfer
, true);
581 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
582 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
583 usb_host_iso_free_xfer(xfer
, false);
585 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
) {
586 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
587 usb_host_iso_free_xfer(xfer
, false);
590 QTAILQ_REMOVE(&ring
->host
->isorings
, ring
, next
);
594 static void usb_host_iso_free_all(USBHostDevice
*s
)
596 USBHostIsoRing
*ring
;
598 while ((ring
= QTAILQ_FIRST(&s
->isorings
)) != NULL
) {
599 usb_host_iso_free(ring
);
603 static bool usb_host_iso_data_copy(USBHostIsoXfer
*xfer
, USBPacket
*p
)
608 buf
= libusb_get_iso_packet_buffer_simple(xfer
->xfer
, xfer
->packet
);
609 if (p
->pid
== USB_TOKEN_OUT
) {
611 if (psize
> xfer
->ring
->ep
->max_packet_size
) {
612 /* should not happen (guest bug) */
613 psize
= xfer
->ring
->ep
->max_packet_size
;
615 xfer
->xfer
->iso_packet_desc
[xfer
->packet
].length
= psize
;
617 psize
= xfer
->xfer
->iso_packet_desc
[xfer
->packet
].actual_length
;
618 if (psize
> p
->iov
.size
) {
619 /* should not happen (guest bug) */
623 usb_packet_copy(p
, buf
, psize
);
625 xfer
->copy_complete
= (xfer
->packet
== xfer
->xfer
->num_iso_packets
);
626 return xfer
->copy_complete
;
629 static void usb_host_iso_data_in(USBHostDevice
*s
, USBPacket
*p
)
631 USBHostIsoRing
*ring
;
632 USBHostIsoXfer
*xfer
;
633 bool disconnect
= false;
636 ring
= usb_host_iso_find(s
, p
->ep
);
638 ring
= usb_host_iso_alloc(s
, p
->ep
);
641 /* copy data to guest */
642 xfer
= QTAILQ_FIRST(&ring
->copy
);
644 if (usb_host_iso_data_copy(xfer
, p
)) {
645 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
646 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
650 /* submit empty bufs to host */
651 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
652 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
653 usb_host_iso_reset_xfer(xfer
);
654 rc
= libusb_submit_transfer(xfer
->xfer
);
656 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
657 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
658 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
663 if (QTAILQ_EMPTY(&ring
->inflight
)) {
664 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
666 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
674 static void usb_host_iso_data_out(USBHostDevice
*s
, USBPacket
*p
)
676 USBHostIsoRing
*ring
;
677 USBHostIsoXfer
*xfer
;
678 bool disconnect
= false;
681 ring
= usb_host_iso_find(s
, p
->ep
);
683 ring
= usb_host_iso_alloc(s
, p
->ep
);
686 /* copy data from guest */
687 xfer
= QTAILQ_FIRST(&ring
->copy
);
688 while (xfer
!= NULL
&& xfer
->copy_complete
) {
690 xfer
= QTAILQ_NEXT(xfer
, next
);
693 xfer
= QTAILQ_FIRST(&ring
->unused
);
695 trace_usb_host_iso_out_of_bufs(s
->bus_num
, s
->addr
, p
->ep
->nr
);
698 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
699 usb_host_iso_reset_xfer(xfer
);
700 QTAILQ_INSERT_TAIL(&ring
->copy
, xfer
, next
);
702 usb_host_iso_data_copy(xfer
, p
);
704 if (QTAILQ_EMPTY(&ring
->inflight
)) {
705 /* wait until half of our buffers are filled
706 before kicking the iso out stream */
707 if (filled
*2 < s
->iso_urb_count
) {
712 /* submit filled bufs to host */
713 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
&&
714 xfer
->copy_complete
) {
715 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
716 rc
= libusb_submit_transfer(xfer
->xfer
);
718 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
719 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
720 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
725 if (QTAILQ_EMPTY(&ring
->inflight
)) {
726 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
728 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
736 /* ------------------------------------------------------------------------ */
738 static void usb_host_speed_compat(USBHostDevice
*s
)
740 USBDevice
*udev
= USB_DEVICE(s
);
741 struct libusb_config_descriptor
*conf
;
742 const struct libusb_interface_descriptor
*intf
;
743 const struct libusb_endpoint_descriptor
*endp
;
745 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
747 bool compat_high
= true;
748 bool compat_full
= true;
753 rc
= libusb_get_config_descriptor(s
->dev
, c
, &conf
);
757 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
758 for (a
= 0; a
< conf
->interface
[i
].num_altsetting
; a
++) {
759 intf
= &conf
->interface
[i
].altsetting
[a
];
760 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
761 endp
= &intf
->endpoint
[e
];
762 type
= endp
->bmAttributes
& 0x3;
768 case 0x02: /* BULK */
770 rc
= libusb_get_ss_endpoint_companion_descriptor
771 (ctx
, endp
, &endp_ss_comp
);
772 if (rc
== LIBUSB_SUCCESS
) {
773 int streams
= endp_ss_comp
->bmAttributes
& 0x1f;
778 libusb_free_ss_endpoint_companion_descriptor
783 case 0x03: /* INTERRUPT */
784 if (endp
->wMaxPacketSize
> 64) {
787 if (endp
->wMaxPacketSize
> 1024) {
795 libusb_free_config_descriptor(conf
);
798 udev
->speedmask
= (1 << udev
->speed
);
799 if (udev
->speed
== USB_SPEED_SUPER
&& compat_high
) {
800 udev
->speedmask
|= USB_SPEED_MASK_HIGH
;
802 if (udev
->speed
== USB_SPEED_SUPER
&& compat_full
) {
803 udev
->speedmask
|= USB_SPEED_MASK_FULL
;
805 if (udev
->speed
== USB_SPEED_HIGH
&& compat_full
) {
806 udev
->speedmask
|= USB_SPEED_MASK_FULL
;
810 static void usb_host_ep_update(USBHostDevice
*s
)
812 static const char *tname
[] = {
813 [USB_ENDPOINT_XFER_CONTROL
] = "control",
814 [USB_ENDPOINT_XFER_ISOC
] = "isoc",
815 [USB_ENDPOINT_XFER_BULK
] = "bulk",
816 [USB_ENDPOINT_XFER_INT
] = "int",
818 USBDevice
*udev
= USB_DEVICE(s
);
819 struct libusb_config_descriptor
*conf
;
820 const struct libusb_interface_descriptor
*intf
;
821 const struct libusb_endpoint_descriptor
*endp
;
823 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
830 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
834 trace_usb_host_parse_config(s
->bus_num
, s
->addr
,
835 conf
->bConfigurationValue
, true);
837 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
838 assert(udev
->altsetting
[i
] < conf
->interface
[i
].num_altsetting
);
839 intf
= &conf
->interface
[i
].altsetting
[udev
->altsetting
[i
]];
840 trace_usb_host_parse_interface(s
->bus_num
, s
->addr
,
841 intf
->bInterfaceNumber
,
842 intf
->bAlternateSetting
, true);
843 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
844 endp
= &intf
->endpoint
[e
];
846 devep
= endp
->bEndpointAddress
;
847 pid
= (devep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
849 type
= endp
->bmAttributes
& 0x3;
852 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
853 "invalid endpoint address");
856 if (usb_ep_get_type(udev
, pid
, ep
) != USB_ENDPOINT_XFER_INVALID
) {
857 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
858 "duplicate endpoint address");
862 trace_usb_host_parse_endpoint(s
->bus_num
, s
->addr
, ep
,
863 (devep
& USB_DIR_IN
) ? "in" : "out",
865 usb_ep_set_max_packet_size(udev
, pid
, ep
,
866 endp
->wMaxPacketSize
);
867 usb_ep_set_type(udev
, pid
, ep
, type
);
868 usb_ep_set_ifnum(udev
, pid
, ep
, i
);
869 usb_ep_set_halted(udev
, pid
, ep
, 0);
871 if (type
== LIBUSB_TRANSFER_TYPE_BULK
&&
872 libusb_get_ss_endpoint_companion_descriptor(ctx
, endp
,
873 &endp_ss_comp
) == LIBUSB_SUCCESS
) {
874 usb_ep_set_max_streams(udev
, pid
, ep
,
875 endp_ss_comp
->bmAttributes
);
876 libusb_free_ss_endpoint_companion_descriptor(endp_ss_comp
);
882 libusb_free_config_descriptor(conf
);
885 static int usb_host_open(USBHostDevice
*s
, libusb_device
*dev
, int hostfd
)
887 USBDevice
*udev
= USB_DEVICE(s
);
891 Error
*local_err
= NULL
;
893 if (s
->bh_postld_pending
) {
901 bus_num
= libusb_get_bus_number(dev
);
902 addr
= libusb_get_device_address(dev
);
903 trace_usb_host_open_started(bus_num
, addr
);
905 rc
= libusb_open(dev
, &s
->dh
);
910 #if LIBUSB_API_VERSION >= 0x01000107 && !defined(CONFIG_WIN32)
911 trace_usb_host_open_hostfd(hostfd
);
913 rc
= libusb_wrap_sys_device(ctx
, hostfd
, &s
->dh
);
918 dev
= libusb_get_device(s
->dh
);
919 bus_num
= libusb_get_bus_number(dev
);
920 addr
= libusb_get_device_address(dev
);
922 g_assert_not_reached();
927 s
->bus_num
= bus_num
;
930 usb_host_detach_kernel(s
);
932 libusb_get_device_descriptor(dev
, &s
->ddesc
);
933 usb_host_get_port(s
->dev
, s
->port
, sizeof(s
->port
));
936 usb_host_ep_update(s
);
938 udev
->speed
= speed_map
[libusb_get_device_speed(dev
)];
939 usb_host_speed_compat(s
);
941 if (s
->ddesc
.iProduct
) {
942 libusb_get_string_descriptor_ascii(s
->dh
, s
->ddesc
.iProduct
,
943 (unsigned char *)udev
->product_desc
,
944 sizeof(udev
->product_desc
));
946 snprintf(udev
->product_desc
, sizeof(udev
->product_desc
),
947 "host:%d.%d", bus_num
, addr
);
950 usb_device_attach(udev
, &local_err
);
952 error_report_err(local_err
);
956 trace_usb_host_open_success(bus_num
, addr
);
960 trace_usb_host_open_failure(bus_num
, addr
);
962 usb_host_release_interfaces(s
);
963 libusb_reset_device(s
->dh
);
964 usb_host_attach_kernel(s
);
972 static void usb_host_abort_xfers(USBHostDevice
*s
)
974 USBHostRequest
*r
, *rtmp
;
977 QTAILQ_FOREACH_SAFE(r
, &s
->requests
, next
, rtmp
) {
978 usb_host_req_abort(r
);
981 while (QTAILQ_FIRST(&s
->requests
) != NULL
) {
983 memset(&tv
, 0, sizeof(tv
));
985 libusb_handle_events_timeout(ctx
, &tv
);
988 * Don't wait forever for libusb calling the complete
989 * callback (which will unlink and free the request).
991 * Leaking memory here, to make sure libusb will not
992 * access memory which we have released already.
994 QTAILQ_FOREACH_SAFE(r
, &s
->requests
, next
, rtmp
) {
995 QTAILQ_REMOVE(&s
->requests
, r
, next
);
1002 static int usb_host_close(USBHostDevice
*s
)
1004 USBDevice
*udev
= USB_DEVICE(s
);
1006 if (s
->dh
== NULL
) {
1010 trace_usb_host_close(s
->bus_num
, s
->addr
);
1012 usb_host_abort_xfers(s
);
1013 usb_host_iso_free_all(s
);
1015 if (udev
->attached
) {
1016 usb_device_detach(udev
);
1019 usb_host_release_interfaces(s
);
1020 libusb_reset_device(s
->dh
);
1021 usb_host_attach_kernel(s
);
1022 libusb_close(s
->dh
);
1026 if (s
->hostfd
!= -1) {
1031 usb_host_auto_check(NULL
);
1035 static void usb_host_nodev_bh(void *opaque
)
1037 USBHostDevice
*s
= opaque
;
1041 static void usb_host_nodev(USBHostDevice
*s
)
1044 s
->bh_nodev
= qemu_bh_new(usb_host_nodev_bh
, s
);
1046 qemu_bh_schedule(s
->bh_nodev
);
1049 static void usb_host_exit_notifier(struct Notifier
*n
, void *data
)
1051 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
1054 usb_host_abort_xfers(s
);
1055 usb_host_release_interfaces(s
);
1056 libusb_reset_device(s
->dh
);
1057 usb_host_attach_kernel(s
);
1058 libusb_close(s
->dh
);
1062 static libusb_device
*usb_host_find_ref(int bus
, int addr
)
1064 libusb_device
**devs
= NULL
;
1065 libusb_device
*ret
= NULL
;
1068 n
= libusb_get_device_list(ctx
, &devs
);
1069 for (i
= 0; i
< n
; i
++) {
1070 if (libusb_get_bus_number(devs
[i
]) == bus
&&
1071 libusb_get_device_address(devs
[i
]) == addr
) {
1072 ret
= libusb_ref_device(devs
[i
]);
1076 libusb_free_device_list(devs
, 1);
1080 static void usb_host_realize(USBDevice
*udev
, Error
**errp
)
1082 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1083 libusb_device
*ldev
;
1086 if (usb_host_init() != 0) {
1087 error_setg(errp
, "failed to init libusb");
1090 if (s
->match
.vendor_id
> 0xffff) {
1091 error_setg(errp
, "vendorid out of range");
1094 if (s
->match
.product_id
> 0xffff) {
1095 error_setg(errp
, "productid out of range");
1098 if (s
->match
.addr
> 127) {
1099 error_setg(errp
, "hostaddr out of range");
1103 loglevel
= s
->loglevel
;
1104 udev
->flags
|= (1 << USB_DEV_FLAG_IS_HOST
);
1105 udev
->auto_attach
= 0;
1106 QTAILQ_INIT(&s
->requests
);
1107 QTAILQ_INIT(&s
->isorings
);
1110 #if LIBUSB_API_VERSION >= 0x01000107 && !defined(CONFIG_WIN32)
1111 if (s
->hostdevice
) {
1113 s
->needs_autoscan
= false;
1114 fd
= qemu_open(s
->hostdevice
, O_RDWR
);
1116 error_setg_errno(errp
, errno
, "failed to open %s", s
->hostdevice
);
1119 rc
= usb_host_open(s
, NULL
, fd
);
1121 error_setg(errp
, "failed to open host usb device %s", s
->hostdevice
);
1126 if (s
->match
.addr
&& s
->match
.bus_num
&&
1127 !s
->match
.vendor_id
&&
1128 !s
->match
.product_id
&&
1130 s
->needs_autoscan
= false;
1131 ldev
= usb_host_find_ref(s
->match
.bus_num
,
1134 error_setg(errp
, "failed to find host usb device %d:%d",
1135 s
->match
.bus_num
, s
->match
.addr
);
1138 rc
= usb_host_open(s
, ldev
, 0);
1139 libusb_unref_device(ldev
);
1141 error_setg(errp
, "failed to open host usb device %d:%d",
1142 s
->match
.bus_num
, s
->match
.addr
);
1146 s
->needs_autoscan
= true;
1147 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
1148 usb_host_auto_check(NULL
);
1151 s
->exit
.notify
= usb_host_exit_notifier
;
1152 qemu_add_exit_notifier(&s
->exit
);
1155 static void usb_host_instance_init(Object
*obj
)
1157 USBDevice
*udev
= USB_DEVICE(obj
);
1158 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1160 device_add_bootindex_property(obj
, &s
->bootindex
,
1165 static void usb_host_unrealize(USBDevice
*udev
)
1167 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1169 qemu_remove_exit_notifier(&s
->exit
);
1170 if (s
->needs_autoscan
) {
1171 QTAILQ_REMOVE(&hostdevs
, s
, next
);
1176 static void usb_host_cancel_packet(USBDevice
*udev
, USBPacket
*p
)
1178 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1182 usb_combined_packet_cancel(udev
, p
);
1186 trace_usb_host_req_canceled(s
->bus_num
, s
->addr
, p
);
1188 r
= usb_host_req_find(s
, p
);
1190 r
->p
= NULL
; /* mark as dead */
1191 libusb_cancel_transfer(r
->xfer
);
1195 static void usb_host_detach_kernel(USBHostDevice
*s
)
1197 struct libusb_config_descriptor
*conf
;
1200 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1204 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1205 rc
= libusb_kernel_driver_active(s
->dh
, i
);
1206 usb_host_libusb_error("libusb_kernel_driver_active", rc
);
1209 s
->ifs
[i
].detached
= true;
1213 trace_usb_host_detach_kernel(s
->bus_num
, s
->addr
, i
);
1214 rc
= libusb_detach_kernel_driver(s
->dh
, i
);
1215 usb_host_libusb_error("libusb_detach_kernel_driver", rc
);
1216 s
->ifs
[i
].detached
= true;
1218 libusb_free_config_descriptor(conf
);
1221 static void usb_host_attach_kernel(USBHostDevice
*s
)
1223 struct libusb_config_descriptor
*conf
;
1226 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1230 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1231 if (!s
->ifs
[i
].detached
) {
1234 trace_usb_host_attach_kernel(s
->bus_num
, s
->addr
, i
);
1235 libusb_attach_kernel_driver(s
->dh
, i
);
1236 s
->ifs
[i
].detached
= false;
1238 libusb_free_config_descriptor(conf
);
1241 static int usb_host_claim_interfaces(USBHostDevice
*s
, int configuration
)
1243 USBDevice
*udev
= USB_DEVICE(s
);
1244 struct libusb_config_descriptor
*conf
;
1247 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1248 udev
->altsetting
[i
] = 0;
1250 udev
->ninterfaces
= 0;
1251 udev
->configuration
= 0;
1253 usb_host_detach_kernel(s
);
1255 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1257 if (rc
== LIBUSB_ERROR_NOT_FOUND
) {
1258 /* address state - ignore */
1259 return USB_RET_SUCCESS
;
1261 return USB_RET_STALL
;
1265 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1266 trace_usb_host_claim_interface(s
->bus_num
, s
->addr
, configuration
, i
);
1267 rc
= libusb_claim_interface(s
->dh
, i
);
1269 s
->ifs
[i
].claimed
= true;
1270 if (++claimed
== conf
->bNumInterfaces
) {
1275 if (claimed
!= conf
->bNumInterfaces
) {
1276 return USB_RET_STALL
;
1279 udev
->ninterfaces
= conf
->bNumInterfaces
;
1280 udev
->configuration
= configuration
;
1282 libusb_free_config_descriptor(conf
);
1283 return USB_RET_SUCCESS
;
1286 static void usb_host_release_interfaces(USBHostDevice
*s
)
1290 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1291 if (!s
->ifs
[i
].claimed
) {
1294 trace_usb_host_release_interface(s
->bus_num
, s
->addr
, i
);
1295 rc
= libusb_release_interface(s
->dh
, i
);
1296 usb_host_libusb_error("libusb_release_interface", rc
);
1297 s
->ifs
[i
].claimed
= false;
1301 static void usb_host_set_address(USBHostDevice
*s
, int addr
)
1303 USBDevice
*udev
= USB_DEVICE(s
);
1305 trace_usb_host_set_address(s
->bus_num
, s
->addr
, addr
);
1309 static void usb_host_set_config(USBHostDevice
*s
, int config
, USBPacket
*p
)
1313 trace_usb_host_set_config(s
->bus_num
, s
->addr
, config
);
1315 usb_host_release_interfaces(s
);
1316 if (s
->ddesc
.bNumConfigurations
!= 1) {
1317 rc
= libusb_set_configuration(s
->dh
, config
);
1319 usb_host_libusb_error("libusb_set_configuration", rc
);
1320 p
->status
= USB_RET_STALL
;
1321 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1327 p
->status
= usb_host_claim_interfaces(s
, config
);
1328 if (p
->status
!= USB_RET_SUCCESS
) {
1331 usb_host_ep_update(s
);
1334 static void usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
,
1337 USBDevice
*udev
= USB_DEVICE(s
);
1340 trace_usb_host_set_interface(s
->bus_num
, s
->addr
, iface
, alt
);
1342 usb_host_iso_free_all(s
);
1344 if (iface
>= USB_MAX_INTERFACES
) {
1345 p
->status
= USB_RET_STALL
;
1349 rc
= libusb_set_interface_alt_setting(s
->dh
, iface
, alt
);
1351 usb_host_libusb_error("libusb_set_interface_alt_setting", rc
);
1352 p
->status
= USB_RET_STALL
;
1353 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1359 udev
->altsetting
[iface
] = alt
;
1360 usb_host_ep_update(s
);
1363 static void usb_host_handle_control(USBDevice
*udev
, USBPacket
*p
,
1364 int request
, int value
, int index
,
1365 int length
, uint8_t *data
)
1367 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1371 trace_usb_host_req_control(s
->bus_num
, s
->addr
, p
, request
, value
, index
);
1373 if (s
->dh
== NULL
) {
1374 p
->status
= USB_RET_NODEV
;
1375 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1380 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1381 usb_host_set_address(s
, value
);
1382 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1385 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1386 usb_host_set_config(s
, value
& 0xff, p
);
1387 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1390 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1391 usb_host_set_interface(s
, index
, value
, p
);
1392 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1395 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
1396 if (value
== 0) { /* clear halt */
1397 int pid
= (index
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1398 libusb_clear_halt(s
->dh
, index
);
1399 usb_ep_set_halted(udev
, pid
, index
& 0x0f, 0);
1400 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1405 r
= usb_host_req_alloc(s
, p
, (request
>> 8) & USB_DIR_IN
, length
+ 8);
1408 memcpy(r
->buffer
, udev
->setup_buf
, 8);
1410 memcpy(r
->buffer
+ 8, r
->cbuf
, r
->clen
);
1413 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
1414 * to work redirected to a not superspeed capable hcd */
1415 if ((udev
->speedmask
& USB_SPEED_MASK_SUPER
) &&
1416 !(udev
->port
->speedmask
& USB_SPEED_MASK_SUPER
) &&
1417 request
== 0x8006 && value
== 0x100 && index
== 0) {
1418 r
->usb3ep0quirk
= true;
1421 libusb_fill_control_transfer(r
->xfer
, s
->dh
, r
->buffer
,
1422 usb_host_req_complete_ctrl
, r
,
1424 rc
= libusb_submit_transfer(r
->xfer
);
1426 p
->status
= USB_RET_NODEV
;
1427 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1428 p
->status
, p
->actual_length
);
1429 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1435 p
->status
= USB_RET_ASYNC
;
1438 static void usb_host_handle_data(USBDevice
*udev
, USBPacket
*p
)
1440 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1445 if (usb_host_use_combining(p
->ep
) && p
->state
== USB_PACKET_SETUP
) {
1446 p
->status
= USB_RET_ADD_TO_QUEUE
;
1450 trace_usb_host_req_data(s
->bus_num
, s
->addr
, p
,
1451 p
->pid
== USB_TOKEN_IN
,
1452 p
->ep
->nr
, p
->iov
.size
);
1454 if (s
->dh
== NULL
) {
1455 p
->status
= USB_RET_NODEV
;
1456 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1459 if (p
->ep
->halted
) {
1460 p
->status
= USB_RET_STALL
;
1461 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1465 switch (usb_ep_get_type(udev
, p
->pid
, p
->ep
->nr
)) {
1466 case USB_ENDPOINT_XFER_BULK
:
1467 size
= usb_packet_size(p
);
1468 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, size
);
1470 usb_packet_copy(p
, r
->buffer
, size
);
1472 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1475 libusb_fill_bulk_stream_transfer(r
->xfer
, s
->dh
, ep
, p
->stream
,
1477 usb_host_req_complete_data
, r
,
1480 usb_host_req_free(r
);
1481 p
->status
= USB_RET_STALL
;
1485 libusb_fill_bulk_transfer(r
->xfer
, s
->dh
, ep
,
1487 usb_host_req_complete_data
, r
,
1491 case USB_ENDPOINT_XFER_INT
:
1492 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, p
->iov
.size
);
1494 usb_packet_copy(p
, r
->buffer
, p
->iov
.size
);
1496 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1497 libusb_fill_interrupt_transfer(r
->xfer
, s
->dh
, ep
,
1498 r
->buffer
, p
->iov
.size
,
1499 usb_host_req_complete_data
, r
,
1502 case USB_ENDPOINT_XFER_ISOC
:
1503 if (p
->pid
== USB_TOKEN_IN
) {
1504 usb_host_iso_data_in(s
, p
);
1506 usb_host_iso_data_out(s
, p
);
1508 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1509 p
->status
, p
->actual_length
);
1512 p
->status
= USB_RET_STALL
;
1513 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1514 p
->status
, p
->actual_length
);
1518 rc
= libusb_submit_transfer(r
->xfer
);
1520 p
->status
= USB_RET_NODEV
;
1521 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1522 p
->status
, p
->actual_length
);
1523 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1529 p
->status
= USB_RET_ASYNC
;
1532 static void usb_host_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
1534 if (usb_host_use_combining(ep
)) {
1535 usb_ep_combine_input_packets(ep
);
1539 static void usb_host_handle_reset(USBDevice
*udev
)
1541 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1544 if (!s
->allow_one_guest_reset
&& !s
->allow_all_guest_resets
) {
1547 if (!s
->allow_all_guest_resets
&& udev
->addr
== 0) {
1551 trace_usb_host_reset(s
->bus_num
, s
->addr
);
1553 rc
= libusb_reset_device(s
->dh
);
1559 static int usb_host_alloc_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1560 int nr_eps
, int streams
)
1563 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1564 unsigned char endpoints
[30];
1567 for (i
= 0; i
< nr_eps
; i
++) {
1568 endpoints
[i
] = eps
[i
]->nr
;
1569 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1570 endpoints
[i
] |= 0x80;
1573 rc
= libusb_alloc_streams(s
->dh
, streams
, endpoints
, nr_eps
);
1575 usb_host_libusb_error("libusb_alloc_streams", rc
);
1576 } else if (rc
!= streams
) {
1577 error_report("libusb_alloc_streams: got less streams "
1578 "then requested %d < %d", rc
, streams
);
1581 return (rc
== streams
) ? 0 : -1;
1583 error_report("libusb_alloc_streams: error not implemented");
1588 static void usb_host_free_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1592 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1593 unsigned char endpoints
[30];
1596 for (i
= 0; i
< nr_eps
; i
++) {
1597 endpoints
[i
] = eps
[i
]->nr
;
1598 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1599 endpoints
[i
] |= 0x80;
1602 libusb_free_streams(s
->dh
, endpoints
, nr_eps
);
1607 * This is *NOT* about restoring state. We have absolutely no idea
1608 * what state the host device is in at the moment and whenever it is
1609 * still present in the first place. Attemping to contine where we
1610 * left off is impossible.
1612 * What we are going to do here is emulate a surprise removal of
1613 * the usb device passed through, then kick host scan so the device
1614 * will get re-attached (and re-initialized by the guest) in case it
1617 * As the device removal will change the state of other devices (usb
1618 * host controller, most likely interrupt controller too) we have to
1619 * wait with it until *all* vmstate is loaded. Thus post_load just
1620 * kicks a bottom half which then does the actual work.
1622 static void usb_host_post_load_bh(void *opaque
)
1624 USBHostDevice
*dev
= opaque
;
1625 USBDevice
*udev
= USB_DEVICE(dev
);
1627 if (dev
->dh
!= NULL
) {
1628 usb_host_close(dev
);
1630 if (udev
->attached
) {
1631 usb_device_detach(udev
);
1633 dev
->bh_postld_pending
= false;
1634 usb_host_auto_check(NULL
);
1637 static int usb_host_post_load(void *opaque
, int version_id
)
1639 USBHostDevice
*dev
= opaque
;
1641 if (!dev
->bh_postld
) {
1642 dev
->bh_postld
= qemu_bh_new(usb_host_post_load_bh
, dev
);
1644 qemu_bh_schedule(dev
->bh_postld
);
1645 dev
->bh_postld_pending
= true;
1649 static const VMStateDescription vmstate_usb_host
= {
1652 .minimum_version_id
= 1,
1653 .post_load
= usb_host_post_load
,
1654 .fields
= (VMStateField
[]) {
1655 VMSTATE_USB_DEVICE(parent_obj
, USBHostDevice
),
1656 VMSTATE_END_OF_LIST()
1660 static Property usb_host_dev_properties
[] = {
1661 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1662 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1663 DEFINE_PROP_STRING("hostport", USBHostDevice
, match
.port
),
1664 DEFINE_PROP_UINT32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1665 DEFINE_PROP_UINT32("productid", USBHostDevice
, match
.product_id
, 0),
1666 #if LIBUSB_API_VERSION >= 0x01000107
1667 DEFINE_PROP_STRING("hostdevice", USBHostDevice
, hostdevice
),
1669 DEFINE_PROP_UINT32("isobufs", USBHostDevice
, iso_urb_count
, 4),
1670 DEFINE_PROP_UINT32("isobsize", USBHostDevice
, iso_urb_frames
, 32),
1671 DEFINE_PROP_BOOL("guest-reset", USBHostDevice
,
1672 allow_one_guest_reset
, true),
1673 DEFINE_PROP_BOOL("guest-resets-all", USBHostDevice
,
1674 allow_all_guest_resets
, false),
1675 DEFINE_PROP_UINT32("loglevel", USBHostDevice
, loglevel
,
1676 LIBUSB_LOG_LEVEL_WARNING
),
1677 DEFINE_PROP_BIT("pipeline", USBHostDevice
, options
,
1678 USB_HOST_OPT_PIPELINE
, true),
1679 DEFINE_PROP_BOOL("suppress-remote-wake", USBHostDevice
,
1680 suppress_remote_wake
, true),
1681 DEFINE_PROP_END_OF_LIST(),
1684 static void usb_host_class_initfn(ObjectClass
*klass
, void *data
)
1686 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1687 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1689 uc
->realize
= usb_host_realize
;
1690 uc
->product_desc
= "USB Host Device";
1691 uc
->cancel_packet
= usb_host_cancel_packet
;
1692 uc
->handle_data
= usb_host_handle_data
;
1693 uc
->handle_control
= usb_host_handle_control
;
1694 uc
->handle_reset
= usb_host_handle_reset
;
1695 uc
->unrealize
= usb_host_unrealize
;
1696 uc
->flush_ep_queue
= usb_host_flush_ep_queue
;
1697 uc
->alloc_streams
= usb_host_alloc_streams
;
1698 uc
->free_streams
= usb_host_free_streams
;
1699 dc
->vmsd
= &vmstate_usb_host
;
1700 device_class_set_props(dc
, usb_host_dev_properties
);
1701 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
1704 static TypeInfo usb_host_dev_info
= {
1705 .name
= TYPE_USB_HOST_DEVICE
,
1706 .parent
= TYPE_USB_DEVICE
,
1707 .instance_size
= sizeof(USBHostDevice
),
1708 .class_init
= usb_host_class_initfn
,
1709 .instance_init
= usb_host_instance_init
,
1712 static void usb_host_register_types(void)
1714 type_register_static(&usb_host_dev_info
);
1717 type_init(usb_host_register_types
)
1719 /* ------------------------------------------------------------------------ */
1721 static QEMUTimer
*usb_auto_timer
;
1722 static VMChangeStateEntry
*usb_vmstate
;
1724 static void usb_host_vm_state(void *unused
, int running
, RunState state
)
1727 usb_host_auto_check(unused
);
1731 static void usb_host_auto_check(void *unused
)
1733 struct USBHostDevice
*s
;
1734 struct USBAutoFilter
*f
;
1735 libusb_device
**devs
= NULL
;
1736 struct libusb_device_descriptor ddesc
;
1737 int unconnected
= 0;
1740 if (usb_host_init() != 0) {
1744 if (runstate_is_running()) {
1745 n
= libusb_get_device_list(ctx
, &devs
);
1746 for (i
= 0; i
< n
; i
++) {
1747 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1750 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1753 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1755 if (f
->bus_num
> 0 &&
1756 f
->bus_num
!= libusb_get_bus_number(devs
[i
])) {
1760 f
->addr
!= libusb_get_device_address(devs
[i
])) {
1763 if (f
->port
!= NULL
) {
1764 char port
[16] = "-";
1765 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1766 if (strcmp(f
->port
, port
) != 0) {
1770 if (f
->vendor_id
> 0 &&
1771 f
->vendor_id
!= ddesc
.idVendor
) {
1774 if (f
->product_id
> 0 &&
1775 f
->product_id
!= ddesc
.idProduct
) {
1779 /* We got a match */
1781 if (s
->errcount
>= 3) {
1784 if (s
->dh
!= NULL
) {
1787 if (usb_host_open(s
, devs
[i
], 0) < 0) {
1794 libusb_free_device_list(devs
, 1);
1796 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1797 if (s
->dh
== NULL
) {
1810 if (unconnected
== 0) {
1811 /* nothing to watch */
1812 if (usb_auto_timer
) {
1813 timer_del(usb_auto_timer
);
1814 trace_usb_host_auto_scan_disabled();
1822 usb_vmstate
= qemu_add_vm_change_state_handler(usb_host_vm_state
, NULL
);
1824 if (!usb_auto_timer
) {
1825 usb_auto_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, usb_host_auto_check
, NULL
);
1826 if (!usb_auto_timer
) {
1829 trace_usb_host_auto_scan_enabled();
1831 timer_mod(usb_auto_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 2000);
1835 * Check whether USB host device has a USB mass storage SCSI interface
1837 bool usb_host_dev_is_scsi_storage(USBDevice
*ud
)
1839 USBHostDevice
*uhd
= USB_HOST_DEVICE(ud
);
1840 struct libusb_config_descriptor
*conf
;
1841 const struct libusb_interface_descriptor
*intf
;
1842 bool is_scsi_storage
= false;
1845 if (!uhd
|| libusb_get_active_config_descriptor(uhd
->dev
, &conf
) != 0) {
1849 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
1850 intf
= &conf
->interface
[i
].altsetting
[ud
->altsetting
[i
]];
1851 if (intf
->bInterfaceClass
== LIBUSB_CLASS_MASS_STORAGE
&&
1852 intf
->bInterfaceSubClass
== 6) { /* 6 means SCSI */
1853 is_scsi_storage
= true;
1858 libusb_free_config_descriptor(conf
);
1860 return is_scsi_storage
;
1863 void hmp_info_usbhost(Monitor
*mon
, const QDict
*qdict
)
1865 libusb_device
**devs
= NULL
;
1866 struct libusb_device_descriptor ddesc
;
1870 if (usb_host_init() != 0) {
1874 n
= libusb_get_device_list(ctx
, &devs
);
1875 for (i
= 0; i
< n
; i
++) {
1876 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1879 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1882 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1883 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1884 libusb_get_bus_number(devs
[i
]),
1885 libusb_get_device_address(devs
[i
]),
1887 speed_name
[libusb_get_device_speed(devs
[i
])]);
1888 monitor_printf(mon
, " Class %02x:", ddesc
.bDeviceClass
);
1889 monitor_printf(mon
, " USB device %04x:%04x",
1890 ddesc
.idVendor
, ddesc
.idProduct
);
1891 if (ddesc
.iProduct
) {
1892 libusb_device_handle
*handle
;
1893 if (libusb_open(devs
[i
], &handle
) == 0) {
1894 unsigned char name
[64] = "";
1895 libusb_get_string_descriptor_ascii(handle
,
1897 name
, sizeof(name
));
1898 libusb_close(handle
);
1899 monitor_printf(mon
, ", %s", name
);
1902 monitor_printf(mon
, "\n");
1904 libusb_free_device_list(devs
, 1);