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"
37 #include "qom/object.h"
44 #include <sys/ioctl.h>
45 #include <linux/usbdevice_fs.h>
48 #include "qapi/error.h"
49 #include "migration/vmstate.h"
50 #include "monitor/monitor.h"
51 #include "qemu/error-report.h"
52 #include "qemu/main-loop.h"
53 #include "qemu/module.h"
54 #include "sysemu/runstate.h"
55 #include "sysemu/sysemu.h"
58 #include "hw/qdev-properties.h"
61 /* ------------------------------------------------------------------------ */
63 #define TYPE_USB_HOST_DEVICE "usb-host"
64 OBJECT_DECLARE_SIMPLE_TYPE(USBHostDevice
, USB_HOST_DEVICE
)
66 typedef struct USBHostRequest USBHostRequest
;
67 typedef struct USBHostIsoXfer USBHostIsoXfer
;
68 typedef struct USBHostIsoRing USBHostIsoRing
;
70 struct USBAutoFilter
{
78 enum USBHostDeviceOptions
{
79 USB_HOST_OPT_PIPELINE
,
82 struct USBHostDevice
{
86 struct USBAutoFilter match
;
89 uint32_t iso_urb_count
;
90 uint32_t iso_urb_frames
;
94 bool allow_one_guest_reset
;
95 bool allow_all_guest_resets
;
96 bool suppress_remote_wake
;
99 QTAILQ_ENTRY(USBHostDevice
) next
;
107 libusb_device_handle
*dh
;
108 struct libusb_device_descriptor ddesc
;
113 } ifs
[USB_MAX_INTERFACES
];
115 /* callbacks & friends */
118 bool bh_postld_pending
;
122 QTAILQ_HEAD(, USBHostRequest
) requests
;
123 QTAILQ_HEAD(, USBHostIsoRing
) isorings
;
126 struct USBHostRequest
{
130 struct libusb_transfer
*xfer
;
131 unsigned char *buffer
;
135 QTAILQ_ENTRY(USBHostRequest
) next
;
138 struct USBHostIsoXfer
{
139 USBHostIsoRing
*ring
;
140 struct libusb_transfer
*xfer
;
143 QTAILQ_ENTRY(USBHostIsoXfer
) next
;
146 struct USBHostIsoRing
{
149 QTAILQ_HEAD(, USBHostIsoXfer
) unused
;
150 QTAILQ_HEAD(, USBHostIsoXfer
) inflight
;
151 QTAILQ_HEAD(, USBHostIsoXfer
) copy
;
152 QTAILQ_ENTRY(USBHostIsoRing
) next
;
155 static QTAILQ_HEAD(, USBHostDevice
) hostdevs
=
156 QTAILQ_HEAD_INITIALIZER(hostdevs
);
158 static void usb_host_auto_check(void *unused
);
159 static void usb_host_release_interfaces(USBHostDevice
*s
);
160 static void usb_host_nodev(USBHostDevice
*s
);
161 static void usb_host_detach_kernel(USBHostDevice
*s
);
162 static void usb_host_attach_kernel(USBHostDevice
*s
);
164 /* ------------------------------------------------------------------------ */
166 #ifndef LIBUSB_LOG_LEVEL_WARNING /* older libusb didn't define these */
167 #define LIBUSB_LOG_LEVEL_WARNING 2
170 /* ------------------------------------------------------------------------ */
172 #define CONTROL_TIMEOUT 10000 /* 10 sec */
173 #define BULK_TIMEOUT 0 /* unlimited */
174 #define INTR_TIMEOUT 0 /* unlimited */
176 #ifndef LIBUSB_API_VERSION
177 # define LIBUSB_API_VERSION LIBUSBX_API_VERSION
179 #if LIBUSB_API_VERSION >= 0x01000103
180 # define HAVE_STREAMS 1
182 #if LIBUSB_API_VERSION >= 0x01000106
183 # define HAVE_SUPER_PLUS 1
186 static const char *speed_name
[] = {
187 [LIBUSB_SPEED_UNKNOWN
] = "?",
188 [LIBUSB_SPEED_LOW
] = "1.5",
189 [LIBUSB_SPEED_FULL
] = "12",
190 [LIBUSB_SPEED_HIGH
] = "480",
191 [LIBUSB_SPEED_SUPER
] = "5000",
192 #ifdef HAVE_SUPER_PLUS
193 [LIBUSB_SPEED_SUPER_PLUS
] = "5000+",
197 static const unsigned int speed_map
[] = {
198 [LIBUSB_SPEED_LOW
] = USB_SPEED_LOW
,
199 [LIBUSB_SPEED_FULL
] = USB_SPEED_FULL
,
200 [LIBUSB_SPEED_HIGH
] = USB_SPEED_HIGH
,
201 [LIBUSB_SPEED_SUPER
] = USB_SPEED_SUPER
,
202 #ifdef HAVE_SUPER_PLUS
203 [LIBUSB_SPEED_SUPER_PLUS
] = USB_SPEED_SUPER
,
207 static const unsigned int status_map
[] = {
208 [LIBUSB_TRANSFER_COMPLETED
] = USB_RET_SUCCESS
,
209 [LIBUSB_TRANSFER_ERROR
] = USB_RET_IOERROR
,
210 [LIBUSB_TRANSFER_TIMED_OUT
] = USB_RET_IOERROR
,
211 [LIBUSB_TRANSFER_CANCELLED
] = USB_RET_IOERROR
,
212 [LIBUSB_TRANSFER_STALL
] = USB_RET_STALL
,
213 [LIBUSB_TRANSFER_NO_DEVICE
] = USB_RET_NODEV
,
214 [LIBUSB_TRANSFER_OVERFLOW
] = USB_RET_BABBLE
,
217 static const char *err_names
[] = {
218 [-LIBUSB_ERROR_IO
] = "IO",
219 [-LIBUSB_ERROR_INVALID_PARAM
] = "INVALID_PARAM",
220 [-LIBUSB_ERROR_ACCESS
] = "ACCESS",
221 [-LIBUSB_ERROR_NO_DEVICE
] = "NO_DEVICE",
222 [-LIBUSB_ERROR_NOT_FOUND
] = "NOT_FOUND",
223 [-LIBUSB_ERROR_BUSY
] = "BUSY",
224 [-LIBUSB_ERROR_TIMEOUT
] = "TIMEOUT",
225 [-LIBUSB_ERROR_OVERFLOW
] = "OVERFLOW",
226 [-LIBUSB_ERROR_PIPE
] = "PIPE",
227 [-LIBUSB_ERROR_INTERRUPTED
] = "INTERRUPTED",
228 [-LIBUSB_ERROR_NO_MEM
] = "NO_MEM",
229 [-LIBUSB_ERROR_NOT_SUPPORTED
] = "NOT_SUPPORTED",
230 [-LIBUSB_ERROR_OTHER
] = "OTHER",
233 static libusb_context
*ctx
;
234 static uint32_t loglevel
;
238 static void usb_host_handle_fd(void *opaque
)
240 struct timeval tv
= { 0, 0 };
241 libusb_handle_events_timeout(ctx
, &tv
);
244 static void usb_host_add_fd(int fd
, short events
, void *user_data
)
246 qemu_set_fd_handler(fd
,
247 (events
& POLLIN
) ? usb_host_handle_fd
: NULL
,
248 (events
& POLLOUT
) ? usb_host_handle_fd
: NULL
,
252 static void usb_host_del_fd(int fd
, void *user_data
)
254 qemu_set_fd_handler(fd
, NULL
, NULL
, NULL
);
257 #endif /* !CONFIG_WIN32 */
259 static int usb_host_init(void)
262 const struct libusb_pollfd
**poll
;
269 rc
= libusb_init(&ctx
);
273 #if LIBUSB_API_VERSION >= 0x01000106
274 libusb_set_option(ctx
, LIBUSB_OPTION_LOG_LEVEL
, loglevel
);
276 libusb_set_debug(ctx
, loglevel
);
279 /* FIXME: add support for Windows. */
281 libusb_set_pollfd_notifiers(ctx
, usb_host_add_fd
,
284 poll
= libusb_get_pollfds(ctx
);
287 for (i
= 0; poll
[i
] != NULL
; i
++) {
288 usb_host_add_fd(poll
[i
]->fd
, poll
[i
]->events
, ctx
);
296 static int usb_host_get_port(libusb_device
*dev
, char *port
, size_t len
)
302 #if LIBUSB_API_VERSION >= 0x01000102
303 rc
= libusb_get_port_numbers(dev
, path
, 7);
305 rc
= libusb_get_port_path(ctx
, dev
, path
, 7);
310 off
= snprintf(port
, len
, "%d", path
[0]);
311 for (i
= 1; i
< rc
; i
++) {
312 off
+= snprintf(port
+off
, len
-off
, ".%d", path
[i
]);
317 static void usb_host_libusb_error(const char *func
, int rc
)
325 if (-rc
< ARRAY_SIZE(err_names
) && err_names
[-rc
]) {
326 errname
= err_names
[-rc
];
330 error_report("%s: %d [%s]", func
, rc
, errname
);
333 /* ------------------------------------------------------------------------ */
335 static bool usb_host_use_combining(USBEndpoint
*ep
)
342 if (ep
->pid
!= USB_TOKEN_IN
) {
345 type
= usb_ep_get_type(ep
->dev
, ep
->pid
, ep
->nr
);
346 if (type
!= USB_ENDPOINT_XFER_BULK
) {
352 /* ------------------------------------------------------------------------ */
354 static USBHostRequest
*usb_host_req_alloc(USBHostDevice
*s
, USBPacket
*p
,
355 bool in
, size_t bufsize
)
357 USBHostRequest
*r
= g_new0(USBHostRequest
, 1);
362 r
->xfer
= libusb_alloc_transfer(0);
364 r
->buffer
= g_malloc(bufsize
);
366 QTAILQ_INSERT_TAIL(&s
->requests
, r
, next
);
370 static void usb_host_req_free(USBHostRequest
*r
)
372 QTAILQ_REMOVE(&r
->host
->requests
, r
, next
);
373 libusb_free_transfer(r
->xfer
);
378 static USBHostRequest
*usb_host_req_find(USBHostDevice
*s
, USBPacket
*p
)
382 QTAILQ_FOREACH(r
, &s
->requests
, next
) {
390 static void LIBUSB_CALL
usb_host_req_complete_ctrl(struct libusb_transfer
*xfer
)
392 USBHostRequest
*r
= xfer
->user_data
;
393 USBHostDevice
*s
= r
->host
;
394 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
397 goto out
; /* request was canceled */
400 r
->p
->status
= status_map
[xfer
->status
];
401 r
->p
->actual_length
= xfer
->actual_length
;
402 if (r
->in
&& xfer
->actual_length
) {
403 USBDevice
*udev
= USB_DEVICE(s
);
404 struct libusb_config_descriptor
*conf
= (void *)r
->cbuf
;
405 memcpy(r
->cbuf
, r
->buffer
+ 8, xfer
->actual_length
);
407 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
408 * to work redirected to a not superspeed capable hcd */
409 if (r
->usb3ep0quirk
&& xfer
->actual_length
>= 18 &&
414 *If this is GET_DESCRIPTOR request for configuration descriptor,
415 * remove 'remote wakeup' flag from it to prevent idle power down
418 if (s
->suppress_remote_wake
&&
419 udev
->setup_buf
[0] == USB_DIR_IN
&&
420 udev
->setup_buf
[1] == USB_REQ_GET_DESCRIPTOR
&&
421 udev
->setup_buf
[3] == USB_DT_CONFIG
&& udev
->setup_buf
[2] == 0 &&
422 xfer
->actual_length
>
423 offsetof(struct libusb_config_descriptor
, bmAttributes
) &&
424 (conf
->bmAttributes
& USB_CFG_ATT_WAKEUP
)) {
425 trace_usb_host_remote_wakeup_removed(s
->bus_num
, s
->addr
);
426 conf
->bmAttributes
&= ~USB_CFG_ATT_WAKEUP
;
429 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
430 r
->p
->status
, r
->p
->actual_length
);
431 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
434 usb_host_req_free(r
);
440 static void LIBUSB_CALL
usb_host_req_complete_data(struct libusb_transfer
*xfer
)
442 USBHostRequest
*r
= xfer
->user_data
;
443 USBHostDevice
*s
= r
->host
;
444 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
447 goto out
; /* request was canceled */
450 r
->p
->status
= status_map
[xfer
->status
];
451 if (r
->in
&& xfer
->actual_length
) {
452 usb_packet_copy(r
->p
, r
->buffer
, xfer
->actual_length
);
454 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
455 r
->p
->status
, r
->p
->actual_length
);
456 if (usb_host_use_combining(r
->p
->ep
)) {
457 usb_combined_input_packet_complete(USB_DEVICE(s
), r
->p
);
459 usb_packet_complete(USB_DEVICE(s
), r
->p
);
463 usb_host_req_free(r
);
469 static void usb_host_req_abort(USBHostRequest
*r
)
471 USBHostDevice
*s
= r
->host
;
472 bool inflight
= (r
->p
&& r
->p
->state
== USB_PACKET_ASYNC
);
475 r
->p
->status
= USB_RET_NODEV
;
476 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
477 r
->p
->status
, r
->p
->actual_length
);
478 if (r
->p
->ep
->nr
== 0) {
479 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
481 usb_packet_complete(USB_DEVICE(s
), r
->p
);
485 libusb_cancel_transfer(r
->xfer
);
489 /* ------------------------------------------------------------------------ */
491 static void LIBUSB_CALL
492 usb_host_req_complete_iso(struct libusb_transfer
*transfer
)
494 USBHostIsoXfer
*xfer
= transfer
->user_data
;
497 /* USBHostIsoXfer released while inflight */
498 g_free(transfer
->buffer
);
499 libusb_free_transfer(transfer
);
503 QTAILQ_REMOVE(&xfer
->ring
->inflight
, xfer
, next
);
504 if (QTAILQ_EMPTY(&xfer
->ring
->inflight
)) {
505 USBHostDevice
*s
= xfer
->ring
->host
;
506 trace_usb_host_iso_stop(s
->bus_num
, s
->addr
, xfer
->ring
->ep
->nr
);
508 if (xfer
->ring
->ep
->pid
== USB_TOKEN_IN
) {
509 QTAILQ_INSERT_TAIL(&xfer
->ring
->copy
, xfer
, next
);
510 usb_wakeup(xfer
->ring
->ep
, 0);
512 QTAILQ_INSERT_TAIL(&xfer
->ring
->unused
, xfer
, next
);
516 static USBHostIsoRing
*usb_host_iso_alloc(USBHostDevice
*s
, USBEndpoint
*ep
)
518 USBHostIsoRing
*ring
= g_new0(USBHostIsoRing
, 1);
519 USBHostIsoXfer
*xfer
;
520 /* FIXME: check interval (for now assume one xfer per frame) */
521 int packets
= s
->iso_urb_frames
;
526 QTAILQ_INIT(&ring
->unused
);
527 QTAILQ_INIT(&ring
->inflight
);
528 QTAILQ_INIT(&ring
->copy
);
529 QTAILQ_INSERT_TAIL(&s
->isorings
, ring
, next
);
531 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
532 xfer
= g_new0(USBHostIsoXfer
, 1);
534 xfer
->xfer
= libusb_alloc_transfer(packets
);
535 xfer
->xfer
->dev_handle
= s
->dh
;
536 xfer
->xfer
->type
= LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
;
538 xfer
->xfer
->endpoint
= ring
->ep
->nr
;
539 if (ring
->ep
->pid
== USB_TOKEN_IN
) {
540 xfer
->xfer
->endpoint
|= USB_DIR_IN
;
542 xfer
->xfer
->callback
= usb_host_req_complete_iso
;
543 xfer
->xfer
->user_data
= xfer
;
545 xfer
->xfer
->num_iso_packets
= packets
;
546 xfer
->xfer
->length
= ring
->ep
->max_packet_size
* packets
;
547 xfer
->xfer
->buffer
= g_malloc0(xfer
->xfer
->length
);
549 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
555 static USBHostIsoRing
*usb_host_iso_find(USBHostDevice
*s
, USBEndpoint
*ep
)
557 USBHostIsoRing
*ring
;
559 QTAILQ_FOREACH(ring
, &s
->isorings
, next
) {
560 if (ring
->ep
== ep
) {
567 static void usb_host_iso_reset_xfer(USBHostIsoXfer
*xfer
)
569 libusb_set_iso_packet_lengths(xfer
->xfer
,
570 xfer
->ring
->ep
->max_packet_size
);
572 xfer
->copy_complete
= false;
575 static void usb_host_iso_free_xfer(USBHostIsoXfer
*xfer
, bool inflight
)
578 xfer
->xfer
->user_data
= NULL
;
580 g_free(xfer
->xfer
->buffer
);
581 libusb_free_transfer(xfer
->xfer
);
586 static void usb_host_iso_free(USBHostIsoRing
*ring
)
588 USBHostIsoXfer
*xfer
;
590 while ((xfer
= QTAILQ_FIRST(&ring
->inflight
)) != NULL
) {
591 QTAILQ_REMOVE(&ring
->inflight
, xfer
, next
);
592 usb_host_iso_free_xfer(xfer
, true);
594 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
595 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
596 usb_host_iso_free_xfer(xfer
, false);
598 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
) {
599 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
600 usb_host_iso_free_xfer(xfer
, false);
603 QTAILQ_REMOVE(&ring
->host
->isorings
, ring
, next
);
607 static void usb_host_iso_free_all(USBHostDevice
*s
)
609 USBHostIsoRing
*ring
;
611 while ((ring
= QTAILQ_FIRST(&s
->isorings
)) != NULL
) {
612 usb_host_iso_free(ring
);
616 static bool usb_host_iso_data_copy(USBHostIsoXfer
*xfer
, USBPacket
*p
)
621 buf
= libusb_get_iso_packet_buffer_simple(xfer
->xfer
, xfer
->packet
);
622 if (p
->pid
== USB_TOKEN_OUT
) {
624 if (psize
> xfer
->ring
->ep
->max_packet_size
) {
625 /* should not happen (guest bug) */
626 psize
= xfer
->ring
->ep
->max_packet_size
;
628 xfer
->xfer
->iso_packet_desc
[xfer
->packet
].length
= psize
;
630 psize
= xfer
->xfer
->iso_packet_desc
[xfer
->packet
].actual_length
;
631 if (psize
> p
->iov
.size
) {
632 /* should not happen (guest bug) */
636 usb_packet_copy(p
, buf
, psize
);
638 xfer
->copy_complete
= (xfer
->packet
== xfer
->xfer
->num_iso_packets
);
639 return xfer
->copy_complete
;
642 static void usb_host_iso_data_in(USBHostDevice
*s
, USBPacket
*p
)
644 USBHostIsoRing
*ring
;
645 USBHostIsoXfer
*xfer
;
646 bool disconnect
= false;
649 ring
= usb_host_iso_find(s
, p
->ep
);
651 ring
= usb_host_iso_alloc(s
, p
->ep
);
654 /* copy data to guest */
655 xfer
= QTAILQ_FIRST(&ring
->copy
);
657 if (usb_host_iso_data_copy(xfer
, p
)) {
658 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
659 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
663 /* submit empty bufs to host */
664 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
665 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
666 usb_host_iso_reset_xfer(xfer
);
667 rc
= libusb_submit_transfer(xfer
->xfer
);
669 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
670 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
671 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
676 if (QTAILQ_EMPTY(&ring
->inflight
)) {
677 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
679 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
687 static void usb_host_iso_data_out(USBHostDevice
*s
, USBPacket
*p
)
689 USBHostIsoRing
*ring
;
690 USBHostIsoXfer
*xfer
;
691 bool disconnect
= false;
694 ring
= usb_host_iso_find(s
, p
->ep
);
696 ring
= usb_host_iso_alloc(s
, p
->ep
);
699 /* copy data from guest */
700 xfer
= QTAILQ_FIRST(&ring
->copy
);
701 while (xfer
!= NULL
&& xfer
->copy_complete
) {
703 xfer
= QTAILQ_NEXT(xfer
, next
);
706 xfer
= QTAILQ_FIRST(&ring
->unused
);
708 trace_usb_host_iso_out_of_bufs(s
->bus_num
, s
->addr
, p
->ep
->nr
);
711 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
712 usb_host_iso_reset_xfer(xfer
);
713 QTAILQ_INSERT_TAIL(&ring
->copy
, xfer
, next
);
715 usb_host_iso_data_copy(xfer
, p
);
717 if (QTAILQ_EMPTY(&ring
->inflight
)) {
718 /* wait until half of our buffers are filled
719 before kicking the iso out stream */
720 if (filled
*2 < s
->iso_urb_count
) {
725 /* submit filled bufs to host */
726 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
&&
727 xfer
->copy_complete
) {
728 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
729 rc
= libusb_submit_transfer(xfer
->xfer
);
731 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
732 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
733 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
738 if (QTAILQ_EMPTY(&ring
->inflight
)) {
739 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
741 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
749 /* ------------------------------------------------------------------------ */
751 static void usb_host_speed_compat(USBHostDevice
*s
)
753 USBDevice
*udev
= USB_DEVICE(s
);
754 struct libusb_config_descriptor
*conf
;
755 const struct libusb_interface_descriptor
*intf
;
756 const struct libusb_endpoint_descriptor
*endp
;
758 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
760 bool compat_high
= true;
761 bool compat_full
= true;
766 rc
= libusb_get_config_descriptor(s
->dev
, c
, &conf
);
770 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
771 for (a
= 0; a
< conf
->interface
[i
].num_altsetting
; a
++) {
772 intf
= &conf
->interface
[i
].altsetting
[a
];
773 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
774 endp
= &intf
->endpoint
[e
];
775 type
= endp
->bmAttributes
& 0x3;
781 case 0x02: /* BULK */
783 rc
= libusb_get_ss_endpoint_companion_descriptor
784 (ctx
, endp
, &endp_ss_comp
);
785 if (rc
== LIBUSB_SUCCESS
) {
786 int streams
= endp_ss_comp
->bmAttributes
& 0x1f;
791 libusb_free_ss_endpoint_companion_descriptor
796 case 0x03: /* INTERRUPT */
797 if (endp
->wMaxPacketSize
> 64) {
800 if (endp
->wMaxPacketSize
> 1024) {
808 libusb_free_config_descriptor(conf
);
811 udev
->speedmask
= (1 << udev
->speed
);
812 if (udev
->speed
== USB_SPEED_SUPER
&& compat_high
) {
813 udev
->speedmask
|= USB_SPEED_MASK_HIGH
;
815 if (udev
->speed
== USB_SPEED_SUPER
&& compat_full
) {
816 udev
->speedmask
|= USB_SPEED_MASK_FULL
;
818 if (udev
->speed
== USB_SPEED_HIGH
&& compat_full
) {
819 udev
->speedmask
|= USB_SPEED_MASK_FULL
;
823 static void usb_host_ep_update(USBHostDevice
*s
)
825 static const char *tname
[] = {
826 [USB_ENDPOINT_XFER_CONTROL
] = "control",
827 [USB_ENDPOINT_XFER_ISOC
] = "isoc",
828 [USB_ENDPOINT_XFER_BULK
] = "bulk",
829 [USB_ENDPOINT_XFER_INT
] = "int",
831 USBDevice
*udev
= USB_DEVICE(s
);
832 struct libusb_config_descriptor
*conf
;
833 const struct libusb_interface_descriptor
*intf
;
834 const struct libusb_endpoint_descriptor
*endp
;
836 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
843 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
847 trace_usb_host_parse_config(s
->bus_num
, s
->addr
,
848 conf
->bConfigurationValue
, true);
850 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
851 assert(udev
->altsetting
[i
] < conf
->interface
[i
].num_altsetting
);
852 intf
= &conf
->interface
[i
].altsetting
[udev
->altsetting
[i
]];
853 trace_usb_host_parse_interface(s
->bus_num
, s
->addr
,
854 intf
->bInterfaceNumber
,
855 intf
->bAlternateSetting
, true);
856 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
857 endp
= &intf
->endpoint
[e
];
859 devep
= endp
->bEndpointAddress
;
860 pid
= (devep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
862 type
= endp
->bmAttributes
& 0x3;
865 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
866 "invalid endpoint address");
869 if (usb_ep_get_type(udev
, pid
, ep
) != USB_ENDPOINT_XFER_INVALID
) {
870 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
871 "duplicate endpoint address");
875 trace_usb_host_parse_endpoint(s
->bus_num
, s
->addr
, ep
,
876 (devep
& USB_DIR_IN
) ? "in" : "out",
878 usb_ep_set_max_packet_size(udev
, pid
, ep
,
879 endp
->wMaxPacketSize
);
880 usb_ep_set_type(udev
, pid
, ep
, type
);
881 usb_ep_set_ifnum(udev
, pid
, ep
, i
);
882 usb_ep_set_halted(udev
, pid
, ep
, 0);
884 if (type
== LIBUSB_TRANSFER_TYPE_BULK
&&
885 libusb_get_ss_endpoint_companion_descriptor(ctx
, endp
,
886 &endp_ss_comp
) == LIBUSB_SUCCESS
) {
887 usb_ep_set_max_streams(udev
, pid
, ep
,
888 endp_ss_comp
->bmAttributes
);
889 libusb_free_ss_endpoint_companion_descriptor(endp_ss_comp
);
895 libusb_free_config_descriptor(conf
);
898 static int usb_host_open(USBHostDevice
*s
, libusb_device
*dev
, int hostfd
)
900 USBDevice
*udev
= USB_DEVICE(s
);
905 Error
*local_err
= NULL
;
907 if (s
->bh_postld_pending
) {
915 bus_num
= libusb_get_bus_number(dev
);
916 addr
= libusb_get_device_address(dev
);
917 trace_usb_host_open_started(bus_num
, addr
);
919 rc
= libusb_open(dev
, &s
->dh
);
924 #if LIBUSB_API_VERSION >= 0x01000107 && !defined(CONFIG_WIN32)
925 trace_usb_host_open_hostfd(hostfd
);
927 rc
= libusb_wrap_sys_device(ctx
, hostfd
, &s
->dh
);
932 dev
= libusb_get_device(s
->dh
);
933 bus_num
= libusb_get_bus_number(dev
);
934 addr
= libusb_get_device_address(dev
);
936 g_assert_not_reached();
941 s
->bus_num
= bus_num
;
944 usb_host_detach_kernel(s
);
946 libusb_get_device_descriptor(dev
, &s
->ddesc
);
947 usb_host_get_port(s
->dev
, s
->port
, sizeof(s
->port
));
950 usb_host_ep_update(s
);
952 libusb_speed
= libusb_get_device_speed(dev
);
953 #if LIBUSB_API_VERSION >= 0x01000107 && defined(CONFIG_LINUX) && \
954 defined(USBDEVFS_GET_SPEED)
955 if (hostfd
&& libusb_speed
== 0) {
957 * Workaround libusb bug: libusb_get_device_speed() does not
958 * work for libusb_wrap_sys_device() devices in v1.0.23.
960 * Speeds are defined in linux/usb/ch9.h, file not included
961 * due to name conflicts.
963 int rc
= ioctl(hostfd
, USBDEVFS_GET_SPEED
, NULL
);
966 libusb_speed
= LIBUSB_SPEED_LOW
;
969 libusb_speed
= LIBUSB_SPEED_FULL
;
972 case 4: /* wireless */
973 libusb_speed
= LIBUSB_SPEED_HIGH
;
976 libusb_speed
= LIBUSB_SPEED_SUPER
;
978 case 6: /* super plus */
979 #ifdef HAVE_SUPER_PLUS
980 libusb_speed
= LIBUSB_SPEED_SUPER_PLUS
;
982 libusb_speed
= LIBUSB_SPEED_SUPER
;
988 udev
->speed
= speed_map
[libusb_speed
];
989 usb_host_speed_compat(s
);
991 if (s
->ddesc
.iProduct
) {
992 libusb_get_string_descriptor_ascii(s
->dh
, s
->ddesc
.iProduct
,
993 (unsigned char *)udev
->product_desc
,
994 sizeof(udev
->product_desc
));
996 snprintf(udev
->product_desc
, sizeof(udev
->product_desc
),
997 "host:%d.%d", bus_num
, addr
);
1000 usb_device_attach(udev
, &local_err
);
1002 error_report_err(local_err
);
1006 trace_usb_host_open_success(bus_num
, addr
);
1010 trace_usb_host_open_failure(bus_num
, addr
);
1011 if (s
->dh
!= NULL
) {
1012 usb_host_release_interfaces(s
);
1013 libusb_reset_device(s
->dh
);
1014 usb_host_attach_kernel(s
);
1015 libusb_close(s
->dh
);
1022 static void usb_host_abort_xfers(USBHostDevice
*s
)
1024 USBHostRequest
*r
, *rtmp
;
1027 QTAILQ_FOREACH_SAFE(r
, &s
->requests
, next
, rtmp
) {
1028 usb_host_req_abort(r
);
1031 while (QTAILQ_FIRST(&s
->requests
) != NULL
) {
1033 memset(&tv
, 0, sizeof(tv
));
1035 libusb_handle_events_timeout(ctx
, &tv
);
1038 * Don't wait forever for libusb calling the complete
1039 * callback (which will unlink and free the request).
1041 * Leaking memory here, to make sure libusb will not
1042 * access memory which we have released already.
1044 QTAILQ_FOREACH_SAFE(r
, &s
->requests
, next
, rtmp
) {
1045 QTAILQ_REMOVE(&s
->requests
, r
, next
);
1052 static int usb_host_close(USBHostDevice
*s
)
1054 USBDevice
*udev
= USB_DEVICE(s
);
1056 if (s
->dh
== NULL
) {
1060 trace_usb_host_close(s
->bus_num
, s
->addr
);
1062 usb_host_abort_xfers(s
);
1063 usb_host_iso_free_all(s
);
1065 if (udev
->attached
) {
1066 usb_device_detach(udev
);
1069 usb_host_release_interfaces(s
);
1070 libusb_reset_device(s
->dh
);
1071 usb_host_attach_kernel(s
);
1072 libusb_close(s
->dh
);
1076 if (s
->hostfd
!= -1) {
1081 usb_host_auto_check(NULL
);
1085 static void usb_host_nodev_bh(void *opaque
)
1087 USBHostDevice
*s
= opaque
;
1091 static void usb_host_nodev(USBHostDevice
*s
)
1094 s
->bh_nodev
= qemu_bh_new(usb_host_nodev_bh
, s
);
1096 qemu_bh_schedule(s
->bh_nodev
);
1099 static void usb_host_exit_notifier(struct Notifier
*n
, void *data
)
1101 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
1104 usb_host_abort_xfers(s
);
1105 usb_host_release_interfaces(s
);
1106 libusb_reset_device(s
->dh
);
1107 usb_host_attach_kernel(s
);
1108 libusb_close(s
->dh
);
1112 static libusb_device
*usb_host_find_ref(int bus
, int addr
)
1114 libusb_device
**devs
= NULL
;
1115 libusb_device
*ret
= NULL
;
1118 n
= libusb_get_device_list(ctx
, &devs
);
1119 for (i
= 0; i
< n
; i
++) {
1120 if (libusb_get_bus_number(devs
[i
]) == bus
&&
1121 libusb_get_device_address(devs
[i
]) == addr
) {
1122 ret
= libusb_ref_device(devs
[i
]);
1126 libusb_free_device_list(devs
, 1);
1130 static void usb_host_realize(USBDevice
*udev
, Error
**errp
)
1132 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1133 libusb_device
*ldev
;
1136 if (usb_host_init() != 0) {
1137 error_setg(errp
, "failed to init libusb");
1140 if (s
->match
.vendor_id
> 0xffff) {
1141 error_setg(errp
, "vendorid out of range");
1144 if (s
->match
.product_id
> 0xffff) {
1145 error_setg(errp
, "productid out of range");
1148 if (s
->match
.addr
> 127) {
1149 error_setg(errp
, "hostaddr out of range");
1153 loglevel
= s
->loglevel
;
1154 udev
->flags
|= (1 << USB_DEV_FLAG_IS_HOST
);
1155 udev
->auto_attach
= 0;
1156 QTAILQ_INIT(&s
->requests
);
1157 QTAILQ_INIT(&s
->isorings
);
1160 #if LIBUSB_API_VERSION >= 0x01000107 && !defined(CONFIG_WIN32)
1161 if (s
->hostdevice
) {
1163 s
->needs_autoscan
= false;
1164 fd
= qemu_open_old(s
->hostdevice
, O_RDWR
);
1166 error_setg_errno(errp
, errno
, "failed to open %s", s
->hostdevice
);
1169 rc
= usb_host_open(s
, NULL
, fd
);
1171 error_setg(errp
, "failed to open host usb device %s", s
->hostdevice
);
1176 if (s
->match
.addr
&& s
->match
.bus_num
&&
1177 !s
->match
.vendor_id
&&
1178 !s
->match
.product_id
&&
1180 s
->needs_autoscan
= false;
1181 ldev
= usb_host_find_ref(s
->match
.bus_num
,
1184 error_setg(errp
, "failed to find host usb device %d:%d",
1185 s
->match
.bus_num
, s
->match
.addr
);
1188 rc
= usb_host_open(s
, ldev
, 0);
1189 libusb_unref_device(ldev
);
1191 error_setg(errp
, "failed to open host usb device %d:%d",
1192 s
->match
.bus_num
, s
->match
.addr
);
1196 s
->needs_autoscan
= true;
1197 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
1198 usb_host_auto_check(NULL
);
1201 s
->exit
.notify
= usb_host_exit_notifier
;
1202 qemu_add_exit_notifier(&s
->exit
);
1205 static void usb_host_instance_init(Object
*obj
)
1207 USBDevice
*udev
= USB_DEVICE(obj
);
1208 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1210 device_add_bootindex_property(obj
, &s
->bootindex
,
1215 static void usb_host_unrealize(USBDevice
*udev
)
1217 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1219 qemu_remove_exit_notifier(&s
->exit
);
1220 if (s
->needs_autoscan
) {
1221 QTAILQ_REMOVE(&hostdevs
, s
, next
);
1226 static void usb_host_cancel_packet(USBDevice
*udev
, USBPacket
*p
)
1228 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1232 usb_combined_packet_cancel(udev
, p
);
1236 trace_usb_host_req_canceled(s
->bus_num
, s
->addr
, p
);
1238 r
= usb_host_req_find(s
, p
);
1240 r
->p
= NULL
; /* mark as dead */
1241 libusb_cancel_transfer(r
->xfer
);
1245 static void usb_host_detach_kernel(USBHostDevice
*s
)
1247 struct libusb_config_descriptor
*conf
;
1250 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1254 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1255 rc
= libusb_kernel_driver_active(s
->dh
, i
);
1256 usb_host_libusb_error("libusb_kernel_driver_active", rc
);
1259 s
->ifs
[i
].detached
= true;
1263 trace_usb_host_detach_kernel(s
->bus_num
, s
->addr
, i
);
1264 rc
= libusb_detach_kernel_driver(s
->dh
, i
);
1265 usb_host_libusb_error("libusb_detach_kernel_driver", rc
);
1266 s
->ifs
[i
].detached
= true;
1268 libusb_free_config_descriptor(conf
);
1271 static void usb_host_attach_kernel(USBHostDevice
*s
)
1273 struct libusb_config_descriptor
*conf
;
1276 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1280 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1281 if (!s
->ifs
[i
].detached
) {
1284 trace_usb_host_attach_kernel(s
->bus_num
, s
->addr
, i
);
1285 libusb_attach_kernel_driver(s
->dh
, i
);
1286 s
->ifs
[i
].detached
= false;
1288 libusb_free_config_descriptor(conf
);
1291 static int usb_host_claim_interfaces(USBHostDevice
*s
, int configuration
)
1293 USBDevice
*udev
= USB_DEVICE(s
);
1294 struct libusb_config_descriptor
*conf
;
1297 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1298 udev
->altsetting
[i
] = 0;
1300 udev
->ninterfaces
= 0;
1301 udev
->configuration
= 0;
1303 usb_host_detach_kernel(s
);
1305 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1307 if (rc
== LIBUSB_ERROR_NOT_FOUND
) {
1308 /* address state - ignore */
1309 return USB_RET_SUCCESS
;
1311 return USB_RET_STALL
;
1315 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1316 trace_usb_host_claim_interface(s
->bus_num
, s
->addr
, configuration
, i
);
1317 rc
= libusb_claim_interface(s
->dh
, i
);
1319 s
->ifs
[i
].claimed
= true;
1320 if (++claimed
== conf
->bNumInterfaces
) {
1325 if (claimed
!= conf
->bNumInterfaces
) {
1326 return USB_RET_STALL
;
1329 udev
->ninterfaces
= conf
->bNumInterfaces
;
1330 udev
->configuration
= configuration
;
1332 libusb_free_config_descriptor(conf
);
1333 return USB_RET_SUCCESS
;
1336 static void usb_host_release_interfaces(USBHostDevice
*s
)
1340 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1341 if (!s
->ifs
[i
].claimed
) {
1344 trace_usb_host_release_interface(s
->bus_num
, s
->addr
, i
);
1345 rc
= libusb_release_interface(s
->dh
, i
);
1346 usb_host_libusb_error("libusb_release_interface", rc
);
1347 s
->ifs
[i
].claimed
= false;
1351 static void usb_host_set_address(USBHostDevice
*s
, int addr
)
1353 USBDevice
*udev
= USB_DEVICE(s
);
1355 trace_usb_host_set_address(s
->bus_num
, s
->addr
, addr
);
1359 static void usb_host_set_config(USBHostDevice
*s
, int config
, USBPacket
*p
)
1363 trace_usb_host_set_config(s
->bus_num
, s
->addr
, config
);
1365 usb_host_release_interfaces(s
);
1366 if (s
->ddesc
.bNumConfigurations
!= 1) {
1367 rc
= libusb_set_configuration(s
->dh
, config
);
1369 usb_host_libusb_error("libusb_set_configuration", rc
);
1370 p
->status
= USB_RET_STALL
;
1371 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1377 p
->status
= usb_host_claim_interfaces(s
, config
);
1378 if (p
->status
!= USB_RET_SUCCESS
) {
1381 usb_host_ep_update(s
);
1384 static void usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
,
1387 USBDevice
*udev
= USB_DEVICE(s
);
1390 trace_usb_host_set_interface(s
->bus_num
, s
->addr
, iface
, alt
);
1392 usb_host_iso_free_all(s
);
1394 if (iface
>= USB_MAX_INTERFACES
) {
1395 p
->status
= USB_RET_STALL
;
1399 rc
= libusb_set_interface_alt_setting(s
->dh
, iface
, alt
);
1401 usb_host_libusb_error("libusb_set_interface_alt_setting", rc
);
1402 p
->status
= USB_RET_STALL
;
1403 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1409 udev
->altsetting
[iface
] = alt
;
1410 usb_host_ep_update(s
);
1413 static void usb_host_handle_control(USBDevice
*udev
, USBPacket
*p
,
1414 int request
, int value
, int index
,
1415 int length
, uint8_t *data
)
1417 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1421 trace_usb_host_req_control(s
->bus_num
, s
->addr
, p
, request
, value
, index
);
1423 if (s
->dh
== NULL
) {
1424 p
->status
= USB_RET_NODEV
;
1425 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1430 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1431 usb_host_set_address(s
, value
);
1432 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1435 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1436 usb_host_set_config(s
, value
& 0xff, p
);
1437 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1440 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1441 usb_host_set_interface(s
, index
, value
, p
);
1442 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1445 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
1446 if (value
== 0) { /* clear halt */
1447 int pid
= (index
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1448 libusb_clear_halt(s
->dh
, index
);
1449 usb_ep_set_halted(udev
, pid
, index
& 0x0f, 0);
1450 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1455 r
= usb_host_req_alloc(s
, p
, (request
>> 8) & USB_DIR_IN
, length
+ 8);
1458 memcpy(r
->buffer
, udev
->setup_buf
, 8);
1460 memcpy(r
->buffer
+ 8, r
->cbuf
, r
->clen
);
1463 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
1464 * to work redirected to a not superspeed capable hcd */
1465 if ((udev
->speedmask
& USB_SPEED_MASK_SUPER
) &&
1466 !(udev
->port
->speedmask
& USB_SPEED_MASK_SUPER
) &&
1467 request
== 0x8006 && value
== 0x100 && index
== 0) {
1468 r
->usb3ep0quirk
= true;
1471 libusb_fill_control_transfer(r
->xfer
, s
->dh
, r
->buffer
,
1472 usb_host_req_complete_ctrl
, r
,
1474 rc
= libusb_submit_transfer(r
->xfer
);
1476 p
->status
= USB_RET_NODEV
;
1477 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1478 p
->status
, p
->actual_length
);
1479 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1485 p
->status
= USB_RET_ASYNC
;
1488 static void usb_host_handle_data(USBDevice
*udev
, USBPacket
*p
)
1490 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1495 if (usb_host_use_combining(p
->ep
) && p
->state
== USB_PACKET_SETUP
) {
1496 p
->status
= USB_RET_ADD_TO_QUEUE
;
1500 trace_usb_host_req_data(s
->bus_num
, s
->addr
, p
,
1501 p
->pid
== USB_TOKEN_IN
,
1502 p
->ep
->nr
, p
->iov
.size
);
1504 if (s
->dh
== NULL
) {
1505 p
->status
= USB_RET_NODEV
;
1506 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1509 if (p
->ep
->halted
) {
1510 p
->status
= USB_RET_STALL
;
1511 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1515 switch (usb_ep_get_type(udev
, p
->pid
, p
->ep
->nr
)) {
1516 case USB_ENDPOINT_XFER_BULK
:
1517 size
= usb_packet_size(p
);
1518 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, size
);
1520 usb_packet_copy(p
, r
->buffer
, size
);
1522 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1525 libusb_fill_bulk_stream_transfer(r
->xfer
, s
->dh
, ep
, p
->stream
,
1527 usb_host_req_complete_data
, r
,
1530 usb_host_req_free(r
);
1531 p
->status
= USB_RET_STALL
;
1535 libusb_fill_bulk_transfer(r
->xfer
, s
->dh
, ep
,
1537 usb_host_req_complete_data
, r
,
1541 case USB_ENDPOINT_XFER_INT
:
1542 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, p
->iov
.size
);
1544 usb_packet_copy(p
, r
->buffer
, p
->iov
.size
);
1546 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1547 libusb_fill_interrupt_transfer(r
->xfer
, s
->dh
, ep
,
1548 r
->buffer
, p
->iov
.size
,
1549 usb_host_req_complete_data
, r
,
1552 case USB_ENDPOINT_XFER_ISOC
:
1553 if (p
->pid
== USB_TOKEN_IN
) {
1554 usb_host_iso_data_in(s
, p
);
1556 usb_host_iso_data_out(s
, p
);
1558 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1559 p
->status
, p
->actual_length
);
1562 p
->status
= USB_RET_STALL
;
1563 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1564 p
->status
, p
->actual_length
);
1568 rc
= libusb_submit_transfer(r
->xfer
);
1570 p
->status
= USB_RET_NODEV
;
1571 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1572 p
->status
, p
->actual_length
);
1573 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1579 p
->status
= USB_RET_ASYNC
;
1582 static void usb_host_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
1584 if (usb_host_use_combining(ep
)) {
1585 usb_ep_combine_input_packets(ep
);
1589 static void usb_host_handle_reset(USBDevice
*udev
)
1591 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1594 if (!s
->allow_one_guest_reset
&& !s
->allow_all_guest_resets
) {
1597 if (!s
->allow_all_guest_resets
&& udev
->addr
== 0) {
1601 trace_usb_host_reset(s
->bus_num
, s
->addr
);
1603 rc
= libusb_reset_device(s
->dh
);
1609 static int usb_host_alloc_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1610 int nr_eps
, int streams
)
1613 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1614 unsigned char endpoints
[30];
1617 for (i
= 0; i
< nr_eps
; i
++) {
1618 endpoints
[i
] = eps
[i
]->nr
;
1619 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1620 endpoints
[i
] |= 0x80;
1623 rc
= libusb_alloc_streams(s
->dh
, streams
, endpoints
, nr_eps
);
1625 usb_host_libusb_error("libusb_alloc_streams", rc
);
1626 } else if (rc
!= streams
) {
1627 error_report("libusb_alloc_streams: got less streams "
1628 "then requested %d < %d", rc
, streams
);
1631 return (rc
== streams
) ? 0 : -1;
1633 error_report("libusb_alloc_streams: error not implemented");
1638 static void usb_host_free_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1642 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1643 unsigned char endpoints
[30];
1646 for (i
= 0; i
< nr_eps
; i
++) {
1647 endpoints
[i
] = eps
[i
]->nr
;
1648 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1649 endpoints
[i
] |= 0x80;
1652 libusb_free_streams(s
->dh
, endpoints
, nr_eps
);
1657 * This is *NOT* about restoring state. We have absolutely no idea
1658 * what state the host device is in at the moment and whenever it is
1659 * still present in the first place. Attemping to contine where we
1660 * left off is impossible.
1662 * What we are going to do here is emulate a surprise removal of
1663 * the usb device passed through, then kick host scan so the device
1664 * will get re-attached (and re-initialized by the guest) in case it
1667 * As the device removal will change the state of other devices (usb
1668 * host controller, most likely interrupt controller too) we have to
1669 * wait with it until *all* vmstate is loaded. Thus post_load just
1670 * kicks a bottom half which then does the actual work.
1672 static void usb_host_post_load_bh(void *opaque
)
1674 USBHostDevice
*dev
= opaque
;
1675 USBDevice
*udev
= USB_DEVICE(dev
);
1677 if (dev
->dh
!= NULL
) {
1678 usb_host_close(dev
);
1680 if (udev
->attached
) {
1681 usb_device_detach(udev
);
1683 dev
->bh_postld_pending
= false;
1684 usb_host_auto_check(NULL
);
1687 static int usb_host_post_load(void *opaque
, int version_id
)
1689 USBHostDevice
*dev
= opaque
;
1691 if (!dev
->bh_postld
) {
1692 dev
->bh_postld
= qemu_bh_new(usb_host_post_load_bh
, dev
);
1694 qemu_bh_schedule(dev
->bh_postld
);
1695 dev
->bh_postld_pending
= true;
1699 static const VMStateDescription vmstate_usb_host
= {
1702 .minimum_version_id
= 1,
1703 .post_load
= usb_host_post_load
,
1704 .fields
= (VMStateField
[]) {
1705 VMSTATE_USB_DEVICE(parent_obj
, USBHostDevice
),
1706 VMSTATE_END_OF_LIST()
1710 static Property usb_host_dev_properties
[] = {
1711 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1712 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1713 DEFINE_PROP_STRING("hostport", USBHostDevice
, match
.port
),
1714 DEFINE_PROP_UINT32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1715 DEFINE_PROP_UINT32("productid", USBHostDevice
, match
.product_id
, 0),
1716 #if LIBUSB_API_VERSION >= 0x01000107
1717 DEFINE_PROP_STRING("hostdevice", USBHostDevice
, hostdevice
),
1719 DEFINE_PROP_UINT32("isobufs", USBHostDevice
, iso_urb_count
, 4),
1720 DEFINE_PROP_UINT32("isobsize", USBHostDevice
, iso_urb_frames
, 32),
1721 DEFINE_PROP_BOOL("guest-reset", USBHostDevice
,
1722 allow_one_guest_reset
, true),
1723 DEFINE_PROP_BOOL("guest-resets-all", USBHostDevice
,
1724 allow_all_guest_resets
, false),
1725 DEFINE_PROP_UINT32("loglevel", USBHostDevice
, loglevel
,
1726 LIBUSB_LOG_LEVEL_WARNING
),
1727 DEFINE_PROP_BIT("pipeline", USBHostDevice
, options
,
1728 USB_HOST_OPT_PIPELINE
, true),
1729 DEFINE_PROP_BOOL("suppress-remote-wake", USBHostDevice
,
1730 suppress_remote_wake
, true),
1731 DEFINE_PROP_END_OF_LIST(),
1734 static void usb_host_class_initfn(ObjectClass
*klass
, void *data
)
1736 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1737 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1739 uc
->realize
= usb_host_realize
;
1740 uc
->product_desc
= "USB Host Device";
1741 uc
->cancel_packet
= usb_host_cancel_packet
;
1742 uc
->handle_data
= usb_host_handle_data
;
1743 uc
->handle_control
= usb_host_handle_control
;
1744 uc
->handle_reset
= usb_host_handle_reset
;
1745 uc
->unrealize
= usb_host_unrealize
;
1746 uc
->flush_ep_queue
= usb_host_flush_ep_queue
;
1747 uc
->alloc_streams
= usb_host_alloc_streams
;
1748 uc
->free_streams
= usb_host_free_streams
;
1749 dc
->vmsd
= &vmstate_usb_host
;
1750 device_class_set_props(dc
, usb_host_dev_properties
);
1751 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
1754 static TypeInfo usb_host_dev_info
= {
1755 .name
= TYPE_USB_HOST_DEVICE
,
1756 .parent
= TYPE_USB_DEVICE
,
1757 .instance_size
= sizeof(USBHostDevice
),
1758 .class_init
= usb_host_class_initfn
,
1759 .instance_init
= usb_host_instance_init
,
1762 static void usb_host_register_types(void)
1764 type_register_static(&usb_host_dev_info
);
1767 type_init(usb_host_register_types
)
1769 /* ------------------------------------------------------------------------ */
1771 static QEMUTimer
*usb_auto_timer
;
1772 static VMChangeStateEntry
*usb_vmstate
;
1774 static void usb_host_vm_state(void *unused
, int running
, RunState state
)
1777 usb_host_auto_check(unused
);
1781 static void usb_host_auto_check(void *unused
)
1783 struct USBHostDevice
*s
;
1784 struct USBAutoFilter
*f
;
1785 libusb_device
**devs
= NULL
;
1786 struct libusb_device_descriptor ddesc
;
1787 int unconnected
= 0;
1790 if (usb_host_init() != 0) {
1794 if (runstate_is_running()) {
1795 n
= libusb_get_device_list(ctx
, &devs
);
1796 for (i
= 0; i
< n
; i
++) {
1797 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1800 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1803 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1805 if (f
->bus_num
> 0 &&
1806 f
->bus_num
!= libusb_get_bus_number(devs
[i
])) {
1810 f
->addr
!= libusb_get_device_address(devs
[i
])) {
1813 if (f
->port
!= NULL
) {
1814 char port
[16] = "-";
1815 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1816 if (strcmp(f
->port
, port
) != 0) {
1820 if (f
->vendor_id
> 0 &&
1821 f
->vendor_id
!= ddesc
.idVendor
) {
1824 if (f
->product_id
> 0 &&
1825 f
->product_id
!= ddesc
.idProduct
) {
1829 /* We got a match */
1831 if (s
->errcount
>= 3) {
1834 if (s
->dh
!= NULL
) {
1837 if (usb_host_open(s
, devs
[i
], 0) < 0) {
1844 libusb_free_device_list(devs
, 1);
1846 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1847 if (s
->dh
== NULL
) {
1860 if (unconnected
== 0) {
1861 /* nothing to watch */
1862 if (usb_auto_timer
) {
1863 timer_del(usb_auto_timer
);
1864 trace_usb_host_auto_scan_disabled();
1872 usb_vmstate
= qemu_add_vm_change_state_handler(usb_host_vm_state
, NULL
);
1874 if (!usb_auto_timer
) {
1875 usb_auto_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, usb_host_auto_check
, NULL
);
1876 if (!usb_auto_timer
) {
1879 trace_usb_host_auto_scan_enabled();
1881 timer_mod(usb_auto_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 2000);
1885 * Check whether USB host device has a USB mass storage SCSI interface
1887 bool usb_host_dev_is_scsi_storage(USBDevice
*ud
)
1889 USBHostDevice
*uhd
= USB_HOST_DEVICE(ud
);
1890 struct libusb_config_descriptor
*conf
;
1891 const struct libusb_interface_descriptor
*intf
;
1892 bool is_scsi_storage
= false;
1895 if (!uhd
|| libusb_get_active_config_descriptor(uhd
->dev
, &conf
) != 0) {
1899 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
1900 intf
= &conf
->interface
[i
].altsetting
[ud
->altsetting
[i
]];
1901 if (intf
->bInterfaceClass
== LIBUSB_CLASS_MASS_STORAGE
&&
1902 intf
->bInterfaceSubClass
== 6) { /* 6 means SCSI */
1903 is_scsi_storage
= true;
1908 libusb_free_config_descriptor(conf
);
1910 return is_scsi_storage
;
1913 void hmp_info_usbhost(Monitor
*mon
, const QDict
*qdict
)
1915 libusb_device
**devs
= NULL
;
1916 struct libusb_device_descriptor ddesc
;
1920 if (usb_host_init() != 0) {
1924 n
= libusb_get_device_list(ctx
, &devs
);
1925 for (i
= 0; i
< n
; i
++) {
1926 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1929 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1932 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1933 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1934 libusb_get_bus_number(devs
[i
]),
1935 libusb_get_device_address(devs
[i
]),
1937 speed_name
[libusb_get_device_speed(devs
[i
])]);
1938 monitor_printf(mon
, " Class %02x:", ddesc
.bDeviceClass
);
1939 monitor_printf(mon
, " USB device %04x:%04x",
1940 ddesc
.idVendor
, ddesc
.idProduct
);
1941 if (ddesc
.iProduct
) {
1942 libusb_device_handle
*handle
;
1943 if (libusb_open(devs
[i
], &handle
) == 0) {
1944 unsigned char name
[64] = "";
1945 libusb_get_string_descriptor_ascii(handle
,
1947 name
, sizeof(name
));
1948 libusb_close(handle
);
1949 monitor_printf(mon
, ", %s", name
);
1952 monitor_printf(mon
, "\n");
1954 libusb_free_device_list(devs
, 1);