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
183 static const char *speed_name
[] = {
184 [LIBUSB_SPEED_UNKNOWN
] = "?",
185 [LIBUSB_SPEED_LOW
] = "1.5",
186 [LIBUSB_SPEED_FULL
] = "12",
187 [LIBUSB_SPEED_HIGH
] = "480",
188 [LIBUSB_SPEED_SUPER
] = "5000",
191 static const unsigned int speed_map
[] = {
192 [LIBUSB_SPEED_LOW
] = USB_SPEED_LOW
,
193 [LIBUSB_SPEED_FULL
] = USB_SPEED_FULL
,
194 [LIBUSB_SPEED_HIGH
] = USB_SPEED_HIGH
,
195 [LIBUSB_SPEED_SUPER
] = USB_SPEED_SUPER
,
198 static const unsigned int status_map
[] = {
199 [LIBUSB_TRANSFER_COMPLETED
] = USB_RET_SUCCESS
,
200 [LIBUSB_TRANSFER_ERROR
] = USB_RET_IOERROR
,
201 [LIBUSB_TRANSFER_TIMED_OUT
] = USB_RET_IOERROR
,
202 [LIBUSB_TRANSFER_CANCELLED
] = USB_RET_IOERROR
,
203 [LIBUSB_TRANSFER_STALL
] = USB_RET_STALL
,
204 [LIBUSB_TRANSFER_NO_DEVICE
] = USB_RET_NODEV
,
205 [LIBUSB_TRANSFER_OVERFLOW
] = USB_RET_BABBLE
,
208 static const char *err_names
[] = {
209 [-LIBUSB_ERROR_IO
] = "IO",
210 [-LIBUSB_ERROR_INVALID_PARAM
] = "INVALID_PARAM",
211 [-LIBUSB_ERROR_ACCESS
] = "ACCESS",
212 [-LIBUSB_ERROR_NO_DEVICE
] = "NO_DEVICE",
213 [-LIBUSB_ERROR_NOT_FOUND
] = "NOT_FOUND",
214 [-LIBUSB_ERROR_BUSY
] = "BUSY",
215 [-LIBUSB_ERROR_TIMEOUT
] = "TIMEOUT",
216 [-LIBUSB_ERROR_OVERFLOW
] = "OVERFLOW",
217 [-LIBUSB_ERROR_PIPE
] = "PIPE",
218 [-LIBUSB_ERROR_INTERRUPTED
] = "INTERRUPTED",
219 [-LIBUSB_ERROR_NO_MEM
] = "NO_MEM",
220 [-LIBUSB_ERROR_NOT_SUPPORTED
] = "NOT_SUPPORTED",
221 [-LIBUSB_ERROR_OTHER
] = "OTHER",
224 static libusb_context
*ctx
;
225 static uint32_t loglevel
;
229 static void usb_host_handle_fd(void *opaque
)
231 struct timeval tv
= { 0, 0 };
232 libusb_handle_events_timeout(ctx
, &tv
);
235 static void usb_host_add_fd(int fd
, short events
, void *user_data
)
237 qemu_set_fd_handler(fd
,
238 (events
& POLLIN
) ? usb_host_handle_fd
: NULL
,
239 (events
& POLLOUT
) ? usb_host_handle_fd
: NULL
,
243 static void usb_host_del_fd(int fd
, void *user_data
)
245 qemu_set_fd_handler(fd
, NULL
, NULL
, NULL
);
248 #endif /* !CONFIG_WIN32 */
250 static int usb_host_init(void)
253 const struct libusb_pollfd
**poll
;
260 rc
= libusb_init(&ctx
);
264 #if LIBUSB_API_VERSION >= 0x01000106
265 libusb_set_option(ctx
, LIBUSB_OPTION_LOG_LEVEL
, loglevel
);
267 libusb_set_debug(ctx
, loglevel
);
270 /* FIXME: add support for Windows. */
272 libusb_set_pollfd_notifiers(ctx
, usb_host_add_fd
,
275 poll
= libusb_get_pollfds(ctx
);
278 for (i
= 0; poll
[i
] != NULL
; i
++) {
279 usb_host_add_fd(poll
[i
]->fd
, poll
[i
]->events
, ctx
);
287 static int usb_host_get_port(libusb_device
*dev
, char *port
, size_t len
)
293 #if LIBUSB_API_VERSION >= 0x01000102
294 rc
= libusb_get_port_numbers(dev
, path
, 7);
296 rc
= libusb_get_port_path(ctx
, dev
, path
, 7);
301 off
= snprintf(port
, len
, "%d", path
[0]);
302 for (i
= 1; i
< rc
; i
++) {
303 off
+= snprintf(port
+off
, len
-off
, ".%d", path
[i
]);
308 static void usb_host_libusb_error(const char *func
, int rc
)
316 if (-rc
< ARRAY_SIZE(err_names
) && err_names
[-rc
]) {
317 errname
= err_names
[-rc
];
321 error_report("%s: %d [%s]", func
, rc
, errname
);
324 /* ------------------------------------------------------------------------ */
326 static bool usb_host_use_combining(USBEndpoint
*ep
)
333 if (ep
->pid
!= USB_TOKEN_IN
) {
336 type
= usb_ep_get_type(ep
->dev
, ep
->pid
, ep
->nr
);
337 if (type
!= USB_ENDPOINT_XFER_BULK
) {
343 /* ------------------------------------------------------------------------ */
345 static USBHostRequest
*usb_host_req_alloc(USBHostDevice
*s
, USBPacket
*p
,
346 bool in
, size_t bufsize
)
348 USBHostRequest
*r
= g_new0(USBHostRequest
, 1);
353 r
->xfer
= libusb_alloc_transfer(0);
355 r
->buffer
= g_malloc(bufsize
);
357 QTAILQ_INSERT_TAIL(&s
->requests
, r
, next
);
361 static void usb_host_req_free(USBHostRequest
*r
)
363 QTAILQ_REMOVE(&r
->host
->requests
, r
, next
);
364 libusb_free_transfer(r
->xfer
);
369 static USBHostRequest
*usb_host_req_find(USBHostDevice
*s
, USBPacket
*p
)
373 QTAILQ_FOREACH(r
, &s
->requests
, next
) {
381 static void LIBUSB_CALL
usb_host_req_complete_ctrl(struct libusb_transfer
*xfer
)
383 USBHostRequest
*r
= xfer
->user_data
;
384 USBHostDevice
*s
= r
->host
;
385 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
388 goto out
; /* request was canceled */
391 r
->p
->status
= status_map
[xfer
->status
];
392 r
->p
->actual_length
= xfer
->actual_length
;
393 if (r
->in
&& xfer
->actual_length
) {
394 USBDevice
*udev
= USB_DEVICE(s
);
395 struct libusb_config_descriptor
*conf
= (void *)r
->cbuf
;
396 memcpy(r
->cbuf
, r
->buffer
+ 8, xfer
->actual_length
);
398 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
399 * to work redirected to a not superspeed capable hcd */
400 if (r
->usb3ep0quirk
&& xfer
->actual_length
>= 18 &&
405 *If this is GET_DESCRIPTOR request for configuration descriptor,
406 * remove 'remote wakeup' flag from it to prevent idle power down
409 if (s
->suppress_remote_wake
&&
410 udev
->setup_buf
[0] == USB_DIR_IN
&&
411 udev
->setup_buf
[1] == USB_REQ_GET_DESCRIPTOR
&&
412 udev
->setup_buf
[3] == USB_DT_CONFIG
&& udev
->setup_buf
[2] == 0 &&
413 xfer
->actual_length
>
414 offsetof(struct libusb_config_descriptor
, bmAttributes
) &&
415 (conf
->bmAttributes
& USB_CFG_ATT_WAKEUP
)) {
416 trace_usb_host_remote_wakeup_removed(s
->bus_num
, s
->addr
);
417 conf
->bmAttributes
&= ~USB_CFG_ATT_WAKEUP
;
420 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
421 r
->p
->status
, r
->p
->actual_length
);
422 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
425 usb_host_req_free(r
);
431 static void LIBUSB_CALL
usb_host_req_complete_data(struct libusb_transfer
*xfer
)
433 USBHostRequest
*r
= xfer
->user_data
;
434 USBHostDevice
*s
= r
->host
;
435 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
438 goto out
; /* request was canceled */
441 r
->p
->status
= status_map
[xfer
->status
];
442 if (r
->in
&& xfer
->actual_length
) {
443 usb_packet_copy(r
->p
, r
->buffer
, xfer
->actual_length
);
445 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
446 r
->p
->status
, r
->p
->actual_length
);
447 if (usb_host_use_combining(r
->p
->ep
)) {
448 usb_combined_input_packet_complete(USB_DEVICE(s
), r
->p
);
450 usb_packet_complete(USB_DEVICE(s
), r
->p
);
454 usb_host_req_free(r
);
460 static void usb_host_req_abort(USBHostRequest
*r
)
462 USBHostDevice
*s
= r
->host
;
463 bool inflight
= (r
->p
&& r
->p
->state
== USB_PACKET_ASYNC
);
466 r
->p
->status
= USB_RET_NODEV
;
467 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
468 r
->p
->status
, r
->p
->actual_length
);
469 if (r
->p
->ep
->nr
== 0) {
470 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
472 usb_packet_complete(USB_DEVICE(s
), r
->p
);
476 libusb_cancel_transfer(r
->xfer
);
480 /* ------------------------------------------------------------------------ */
482 static void LIBUSB_CALL
483 usb_host_req_complete_iso(struct libusb_transfer
*transfer
)
485 USBHostIsoXfer
*xfer
= transfer
->user_data
;
488 /* USBHostIsoXfer released while inflight */
489 g_free(transfer
->buffer
);
490 libusb_free_transfer(transfer
);
494 QTAILQ_REMOVE(&xfer
->ring
->inflight
, xfer
, next
);
495 if (QTAILQ_EMPTY(&xfer
->ring
->inflight
)) {
496 USBHostDevice
*s
= xfer
->ring
->host
;
497 trace_usb_host_iso_stop(s
->bus_num
, s
->addr
, xfer
->ring
->ep
->nr
);
499 if (xfer
->ring
->ep
->pid
== USB_TOKEN_IN
) {
500 QTAILQ_INSERT_TAIL(&xfer
->ring
->copy
, xfer
, next
);
501 usb_wakeup(xfer
->ring
->ep
, 0);
503 QTAILQ_INSERT_TAIL(&xfer
->ring
->unused
, xfer
, next
);
507 static USBHostIsoRing
*usb_host_iso_alloc(USBHostDevice
*s
, USBEndpoint
*ep
)
509 USBHostIsoRing
*ring
= g_new0(USBHostIsoRing
, 1);
510 USBHostIsoXfer
*xfer
;
511 /* FIXME: check interval (for now assume one xfer per frame) */
512 int packets
= s
->iso_urb_frames
;
517 QTAILQ_INIT(&ring
->unused
);
518 QTAILQ_INIT(&ring
->inflight
);
519 QTAILQ_INIT(&ring
->copy
);
520 QTAILQ_INSERT_TAIL(&s
->isorings
, ring
, next
);
522 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
523 xfer
= g_new0(USBHostIsoXfer
, 1);
525 xfer
->xfer
= libusb_alloc_transfer(packets
);
526 xfer
->xfer
->dev_handle
= s
->dh
;
527 xfer
->xfer
->type
= LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
;
529 xfer
->xfer
->endpoint
= ring
->ep
->nr
;
530 if (ring
->ep
->pid
== USB_TOKEN_IN
) {
531 xfer
->xfer
->endpoint
|= USB_DIR_IN
;
533 xfer
->xfer
->callback
= usb_host_req_complete_iso
;
534 xfer
->xfer
->user_data
= xfer
;
536 xfer
->xfer
->num_iso_packets
= packets
;
537 xfer
->xfer
->length
= ring
->ep
->max_packet_size
* packets
;
538 xfer
->xfer
->buffer
= g_malloc0(xfer
->xfer
->length
);
540 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
546 static USBHostIsoRing
*usb_host_iso_find(USBHostDevice
*s
, USBEndpoint
*ep
)
548 USBHostIsoRing
*ring
;
550 QTAILQ_FOREACH(ring
, &s
->isorings
, next
) {
551 if (ring
->ep
== ep
) {
558 static void usb_host_iso_reset_xfer(USBHostIsoXfer
*xfer
)
560 libusb_set_iso_packet_lengths(xfer
->xfer
,
561 xfer
->ring
->ep
->max_packet_size
);
563 xfer
->copy_complete
= false;
566 static void usb_host_iso_free_xfer(USBHostIsoXfer
*xfer
, bool inflight
)
569 xfer
->xfer
->user_data
= NULL
;
571 g_free(xfer
->xfer
->buffer
);
572 libusb_free_transfer(xfer
->xfer
);
577 static void usb_host_iso_free(USBHostIsoRing
*ring
)
579 USBHostIsoXfer
*xfer
;
581 while ((xfer
= QTAILQ_FIRST(&ring
->inflight
)) != NULL
) {
582 QTAILQ_REMOVE(&ring
->inflight
, xfer
, next
);
583 usb_host_iso_free_xfer(xfer
, true);
585 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
586 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
587 usb_host_iso_free_xfer(xfer
, false);
589 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
) {
590 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
591 usb_host_iso_free_xfer(xfer
, false);
594 QTAILQ_REMOVE(&ring
->host
->isorings
, ring
, next
);
598 static void usb_host_iso_free_all(USBHostDevice
*s
)
600 USBHostIsoRing
*ring
;
602 while ((ring
= QTAILQ_FIRST(&s
->isorings
)) != NULL
) {
603 usb_host_iso_free(ring
);
607 static bool usb_host_iso_data_copy(USBHostIsoXfer
*xfer
, USBPacket
*p
)
612 buf
= libusb_get_iso_packet_buffer_simple(xfer
->xfer
, xfer
->packet
);
613 if (p
->pid
== USB_TOKEN_OUT
) {
615 if (psize
> xfer
->ring
->ep
->max_packet_size
) {
616 /* should not happen (guest bug) */
617 psize
= xfer
->ring
->ep
->max_packet_size
;
619 xfer
->xfer
->iso_packet_desc
[xfer
->packet
].length
= psize
;
621 psize
= xfer
->xfer
->iso_packet_desc
[xfer
->packet
].actual_length
;
622 if (psize
> p
->iov
.size
) {
623 /* should not happen (guest bug) */
627 usb_packet_copy(p
, buf
, psize
);
629 xfer
->copy_complete
= (xfer
->packet
== xfer
->xfer
->num_iso_packets
);
630 return xfer
->copy_complete
;
633 static void usb_host_iso_data_in(USBHostDevice
*s
, USBPacket
*p
)
635 USBHostIsoRing
*ring
;
636 USBHostIsoXfer
*xfer
;
637 bool disconnect
= false;
640 ring
= usb_host_iso_find(s
, p
->ep
);
642 ring
= usb_host_iso_alloc(s
, p
->ep
);
645 /* copy data to guest */
646 xfer
= QTAILQ_FIRST(&ring
->copy
);
648 if (usb_host_iso_data_copy(xfer
, p
)) {
649 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
650 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
654 /* submit empty bufs to host */
655 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
656 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
657 usb_host_iso_reset_xfer(xfer
);
658 rc
= libusb_submit_transfer(xfer
->xfer
);
660 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
661 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
662 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
667 if (QTAILQ_EMPTY(&ring
->inflight
)) {
668 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
670 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
678 static void usb_host_iso_data_out(USBHostDevice
*s
, USBPacket
*p
)
680 USBHostIsoRing
*ring
;
681 USBHostIsoXfer
*xfer
;
682 bool disconnect
= false;
685 ring
= usb_host_iso_find(s
, p
->ep
);
687 ring
= usb_host_iso_alloc(s
, p
->ep
);
690 /* copy data from guest */
691 xfer
= QTAILQ_FIRST(&ring
->copy
);
692 while (xfer
!= NULL
&& xfer
->copy_complete
) {
694 xfer
= QTAILQ_NEXT(xfer
, next
);
697 xfer
= QTAILQ_FIRST(&ring
->unused
);
699 trace_usb_host_iso_out_of_bufs(s
->bus_num
, s
->addr
, p
->ep
->nr
);
702 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
703 usb_host_iso_reset_xfer(xfer
);
704 QTAILQ_INSERT_TAIL(&ring
->copy
, xfer
, next
);
706 usb_host_iso_data_copy(xfer
, p
);
708 if (QTAILQ_EMPTY(&ring
->inflight
)) {
709 /* wait until half of our buffers are filled
710 before kicking the iso out stream */
711 if (filled
*2 < s
->iso_urb_count
) {
716 /* submit filled bufs to host */
717 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
&&
718 xfer
->copy_complete
) {
719 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
720 rc
= libusb_submit_transfer(xfer
->xfer
);
722 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
723 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
724 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
729 if (QTAILQ_EMPTY(&ring
->inflight
)) {
730 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
732 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
740 /* ------------------------------------------------------------------------ */
742 static void usb_host_speed_compat(USBHostDevice
*s
)
744 USBDevice
*udev
= USB_DEVICE(s
);
745 struct libusb_config_descriptor
*conf
;
746 const struct libusb_interface_descriptor
*intf
;
747 const struct libusb_endpoint_descriptor
*endp
;
749 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
751 bool compat_high
= true;
752 bool compat_full
= true;
757 rc
= libusb_get_config_descriptor(s
->dev
, c
, &conf
);
761 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
762 for (a
= 0; a
< conf
->interface
[i
].num_altsetting
; a
++) {
763 intf
= &conf
->interface
[i
].altsetting
[a
];
764 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
765 endp
= &intf
->endpoint
[e
];
766 type
= endp
->bmAttributes
& 0x3;
772 case 0x02: /* BULK */
774 rc
= libusb_get_ss_endpoint_companion_descriptor
775 (ctx
, endp
, &endp_ss_comp
);
776 if (rc
== LIBUSB_SUCCESS
) {
777 int streams
= endp_ss_comp
->bmAttributes
& 0x1f;
782 libusb_free_ss_endpoint_companion_descriptor
787 case 0x03: /* INTERRUPT */
788 if (endp
->wMaxPacketSize
> 64) {
791 if (endp
->wMaxPacketSize
> 1024) {
799 libusb_free_config_descriptor(conf
);
802 udev
->speedmask
= (1 << udev
->speed
);
803 if (udev
->speed
== USB_SPEED_SUPER
&& compat_high
) {
804 udev
->speedmask
|= USB_SPEED_MASK_HIGH
;
806 if (udev
->speed
== USB_SPEED_SUPER
&& compat_full
) {
807 udev
->speedmask
|= USB_SPEED_MASK_FULL
;
809 if (udev
->speed
== USB_SPEED_HIGH
&& compat_full
) {
810 udev
->speedmask
|= USB_SPEED_MASK_FULL
;
814 static void usb_host_ep_update(USBHostDevice
*s
)
816 static const char *tname
[] = {
817 [USB_ENDPOINT_XFER_CONTROL
] = "control",
818 [USB_ENDPOINT_XFER_ISOC
] = "isoc",
819 [USB_ENDPOINT_XFER_BULK
] = "bulk",
820 [USB_ENDPOINT_XFER_INT
] = "int",
822 USBDevice
*udev
= USB_DEVICE(s
);
823 struct libusb_config_descriptor
*conf
;
824 const struct libusb_interface_descriptor
*intf
;
825 const struct libusb_endpoint_descriptor
*endp
;
827 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
834 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
838 trace_usb_host_parse_config(s
->bus_num
, s
->addr
,
839 conf
->bConfigurationValue
, true);
841 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
842 assert(udev
->altsetting
[i
] < conf
->interface
[i
].num_altsetting
);
843 intf
= &conf
->interface
[i
].altsetting
[udev
->altsetting
[i
]];
844 trace_usb_host_parse_interface(s
->bus_num
, s
->addr
,
845 intf
->bInterfaceNumber
,
846 intf
->bAlternateSetting
, true);
847 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
848 endp
= &intf
->endpoint
[e
];
850 devep
= endp
->bEndpointAddress
;
851 pid
= (devep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
853 type
= endp
->bmAttributes
& 0x3;
856 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
857 "invalid endpoint address");
860 if (usb_ep_get_type(udev
, pid
, ep
) != USB_ENDPOINT_XFER_INVALID
) {
861 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
862 "duplicate endpoint address");
866 trace_usb_host_parse_endpoint(s
->bus_num
, s
->addr
, ep
,
867 (devep
& USB_DIR_IN
) ? "in" : "out",
869 usb_ep_set_max_packet_size(udev
, pid
, ep
,
870 endp
->wMaxPacketSize
);
871 usb_ep_set_type(udev
, pid
, ep
, type
);
872 usb_ep_set_ifnum(udev
, pid
, ep
, i
);
873 usb_ep_set_halted(udev
, pid
, ep
, 0);
875 if (type
== LIBUSB_TRANSFER_TYPE_BULK
&&
876 libusb_get_ss_endpoint_companion_descriptor(ctx
, endp
,
877 &endp_ss_comp
) == LIBUSB_SUCCESS
) {
878 usb_ep_set_max_streams(udev
, pid
, ep
,
879 endp_ss_comp
->bmAttributes
);
880 libusb_free_ss_endpoint_companion_descriptor(endp_ss_comp
);
886 libusb_free_config_descriptor(conf
);
889 static int usb_host_open(USBHostDevice
*s
, libusb_device
*dev
, int hostfd
)
891 USBDevice
*udev
= USB_DEVICE(s
);
896 Error
*local_err
= NULL
;
898 if (s
->bh_postld_pending
) {
906 bus_num
= libusb_get_bus_number(dev
);
907 addr
= libusb_get_device_address(dev
);
908 trace_usb_host_open_started(bus_num
, addr
);
910 rc
= libusb_open(dev
, &s
->dh
);
915 #if LIBUSB_API_VERSION >= 0x01000107 && !defined(CONFIG_WIN32)
916 trace_usb_host_open_hostfd(hostfd
);
918 rc
= libusb_wrap_sys_device(ctx
, hostfd
, &s
->dh
);
923 dev
= libusb_get_device(s
->dh
);
924 bus_num
= libusb_get_bus_number(dev
);
925 addr
= libusb_get_device_address(dev
);
927 g_assert_not_reached();
932 s
->bus_num
= bus_num
;
935 usb_host_detach_kernel(s
);
937 libusb_get_device_descriptor(dev
, &s
->ddesc
);
938 usb_host_get_port(s
->dev
, s
->port
, sizeof(s
->port
));
941 usb_host_ep_update(s
);
943 libusb_speed
= libusb_get_device_speed(dev
);
944 #if LIBUSB_API_VERSION >= 0x01000107 && defined(CONFIG_LINUX)
945 if (hostfd
&& libusb_speed
== 0) {
947 * Workaround libusb bug: libusb_get_device_speed() does not
948 * work for libusb_wrap_sys_device() devices in v1.0.23.
950 * Speeds are defined in linux/usb/ch9.h, file not included
951 * due to name conflicts.
953 int rc
= ioctl(hostfd
, USBDEVFS_GET_SPEED
, NULL
);
956 libusb_speed
= LIBUSB_SPEED_LOW
;
959 libusb_speed
= LIBUSB_SPEED_FULL
;
962 case 4: /* wireless */
963 libusb_speed
= LIBUSB_SPEED_HIGH
;
966 case 6: /* super plus */
967 libusb_speed
= LIBUSB_SPEED_SUPER
;
972 udev
->speed
= speed_map
[libusb_speed
];
973 usb_host_speed_compat(s
);
975 if (s
->ddesc
.iProduct
) {
976 libusb_get_string_descriptor_ascii(s
->dh
, s
->ddesc
.iProduct
,
977 (unsigned char *)udev
->product_desc
,
978 sizeof(udev
->product_desc
));
980 snprintf(udev
->product_desc
, sizeof(udev
->product_desc
),
981 "host:%d.%d", bus_num
, addr
);
984 usb_device_attach(udev
, &local_err
);
986 error_report_err(local_err
);
990 trace_usb_host_open_success(bus_num
, addr
);
994 trace_usb_host_open_failure(bus_num
, addr
);
996 usb_host_release_interfaces(s
);
997 libusb_reset_device(s
->dh
);
998 usb_host_attach_kernel(s
);
1006 static void usb_host_abort_xfers(USBHostDevice
*s
)
1008 USBHostRequest
*r
, *rtmp
;
1011 QTAILQ_FOREACH_SAFE(r
, &s
->requests
, next
, rtmp
) {
1012 usb_host_req_abort(r
);
1015 while (QTAILQ_FIRST(&s
->requests
) != NULL
) {
1017 memset(&tv
, 0, sizeof(tv
));
1019 libusb_handle_events_timeout(ctx
, &tv
);
1022 * Don't wait forever for libusb calling the complete
1023 * callback (which will unlink and free the request).
1025 * Leaking memory here, to make sure libusb will not
1026 * access memory which we have released already.
1028 QTAILQ_FOREACH_SAFE(r
, &s
->requests
, next
, rtmp
) {
1029 QTAILQ_REMOVE(&s
->requests
, r
, next
);
1036 static int usb_host_close(USBHostDevice
*s
)
1038 USBDevice
*udev
= USB_DEVICE(s
);
1040 if (s
->dh
== NULL
) {
1044 trace_usb_host_close(s
->bus_num
, s
->addr
);
1046 usb_host_abort_xfers(s
);
1047 usb_host_iso_free_all(s
);
1049 if (udev
->attached
) {
1050 usb_device_detach(udev
);
1053 usb_host_release_interfaces(s
);
1054 libusb_reset_device(s
->dh
);
1055 usb_host_attach_kernel(s
);
1056 libusb_close(s
->dh
);
1060 if (s
->hostfd
!= -1) {
1065 usb_host_auto_check(NULL
);
1069 static void usb_host_nodev_bh(void *opaque
)
1071 USBHostDevice
*s
= opaque
;
1075 static void usb_host_nodev(USBHostDevice
*s
)
1078 s
->bh_nodev
= qemu_bh_new(usb_host_nodev_bh
, s
);
1080 qemu_bh_schedule(s
->bh_nodev
);
1083 static void usb_host_exit_notifier(struct Notifier
*n
, void *data
)
1085 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
1088 usb_host_abort_xfers(s
);
1089 usb_host_release_interfaces(s
);
1090 libusb_reset_device(s
->dh
);
1091 usb_host_attach_kernel(s
);
1092 libusb_close(s
->dh
);
1096 static libusb_device
*usb_host_find_ref(int bus
, int addr
)
1098 libusb_device
**devs
= NULL
;
1099 libusb_device
*ret
= NULL
;
1102 n
= libusb_get_device_list(ctx
, &devs
);
1103 for (i
= 0; i
< n
; i
++) {
1104 if (libusb_get_bus_number(devs
[i
]) == bus
&&
1105 libusb_get_device_address(devs
[i
]) == addr
) {
1106 ret
= libusb_ref_device(devs
[i
]);
1110 libusb_free_device_list(devs
, 1);
1114 static void usb_host_realize(USBDevice
*udev
, Error
**errp
)
1116 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1117 libusb_device
*ldev
;
1120 if (usb_host_init() != 0) {
1121 error_setg(errp
, "failed to init libusb");
1124 if (s
->match
.vendor_id
> 0xffff) {
1125 error_setg(errp
, "vendorid out of range");
1128 if (s
->match
.product_id
> 0xffff) {
1129 error_setg(errp
, "productid out of range");
1132 if (s
->match
.addr
> 127) {
1133 error_setg(errp
, "hostaddr out of range");
1137 loglevel
= s
->loglevel
;
1138 udev
->flags
|= (1 << USB_DEV_FLAG_IS_HOST
);
1139 udev
->auto_attach
= 0;
1140 QTAILQ_INIT(&s
->requests
);
1141 QTAILQ_INIT(&s
->isorings
);
1144 #if LIBUSB_API_VERSION >= 0x01000107 && !defined(CONFIG_WIN32)
1145 if (s
->hostdevice
) {
1147 s
->needs_autoscan
= false;
1148 fd
= qemu_open_old(s
->hostdevice
, O_RDWR
);
1150 error_setg_errno(errp
, errno
, "failed to open %s", s
->hostdevice
);
1153 rc
= usb_host_open(s
, NULL
, fd
);
1155 error_setg(errp
, "failed to open host usb device %s", s
->hostdevice
);
1160 if (s
->match
.addr
&& s
->match
.bus_num
&&
1161 !s
->match
.vendor_id
&&
1162 !s
->match
.product_id
&&
1164 s
->needs_autoscan
= false;
1165 ldev
= usb_host_find_ref(s
->match
.bus_num
,
1168 error_setg(errp
, "failed to find host usb device %d:%d",
1169 s
->match
.bus_num
, s
->match
.addr
);
1172 rc
= usb_host_open(s
, ldev
, 0);
1173 libusb_unref_device(ldev
);
1175 error_setg(errp
, "failed to open host usb device %d:%d",
1176 s
->match
.bus_num
, s
->match
.addr
);
1180 s
->needs_autoscan
= true;
1181 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
1182 usb_host_auto_check(NULL
);
1185 s
->exit
.notify
= usb_host_exit_notifier
;
1186 qemu_add_exit_notifier(&s
->exit
);
1189 static void usb_host_instance_init(Object
*obj
)
1191 USBDevice
*udev
= USB_DEVICE(obj
);
1192 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1194 device_add_bootindex_property(obj
, &s
->bootindex
,
1199 static void usb_host_unrealize(USBDevice
*udev
)
1201 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1203 qemu_remove_exit_notifier(&s
->exit
);
1204 if (s
->needs_autoscan
) {
1205 QTAILQ_REMOVE(&hostdevs
, s
, next
);
1210 static void usb_host_cancel_packet(USBDevice
*udev
, USBPacket
*p
)
1212 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1216 usb_combined_packet_cancel(udev
, p
);
1220 trace_usb_host_req_canceled(s
->bus_num
, s
->addr
, p
);
1222 r
= usb_host_req_find(s
, p
);
1224 r
->p
= NULL
; /* mark as dead */
1225 libusb_cancel_transfer(r
->xfer
);
1229 static void usb_host_detach_kernel(USBHostDevice
*s
)
1231 struct libusb_config_descriptor
*conf
;
1234 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1238 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1239 rc
= libusb_kernel_driver_active(s
->dh
, i
);
1240 usb_host_libusb_error("libusb_kernel_driver_active", rc
);
1243 s
->ifs
[i
].detached
= true;
1247 trace_usb_host_detach_kernel(s
->bus_num
, s
->addr
, i
);
1248 rc
= libusb_detach_kernel_driver(s
->dh
, i
);
1249 usb_host_libusb_error("libusb_detach_kernel_driver", rc
);
1250 s
->ifs
[i
].detached
= true;
1252 libusb_free_config_descriptor(conf
);
1255 static void usb_host_attach_kernel(USBHostDevice
*s
)
1257 struct libusb_config_descriptor
*conf
;
1260 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1264 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1265 if (!s
->ifs
[i
].detached
) {
1268 trace_usb_host_attach_kernel(s
->bus_num
, s
->addr
, i
);
1269 libusb_attach_kernel_driver(s
->dh
, i
);
1270 s
->ifs
[i
].detached
= false;
1272 libusb_free_config_descriptor(conf
);
1275 static int usb_host_claim_interfaces(USBHostDevice
*s
, int configuration
)
1277 USBDevice
*udev
= USB_DEVICE(s
);
1278 struct libusb_config_descriptor
*conf
;
1281 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1282 udev
->altsetting
[i
] = 0;
1284 udev
->ninterfaces
= 0;
1285 udev
->configuration
= 0;
1287 usb_host_detach_kernel(s
);
1289 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1291 if (rc
== LIBUSB_ERROR_NOT_FOUND
) {
1292 /* address state - ignore */
1293 return USB_RET_SUCCESS
;
1295 return USB_RET_STALL
;
1299 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1300 trace_usb_host_claim_interface(s
->bus_num
, s
->addr
, configuration
, i
);
1301 rc
= libusb_claim_interface(s
->dh
, i
);
1303 s
->ifs
[i
].claimed
= true;
1304 if (++claimed
== conf
->bNumInterfaces
) {
1309 if (claimed
!= conf
->bNumInterfaces
) {
1310 return USB_RET_STALL
;
1313 udev
->ninterfaces
= conf
->bNumInterfaces
;
1314 udev
->configuration
= configuration
;
1316 libusb_free_config_descriptor(conf
);
1317 return USB_RET_SUCCESS
;
1320 static void usb_host_release_interfaces(USBHostDevice
*s
)
1324 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1325 if (!s
->ifs
[i
].claimed
) {
1328 trace_usb_host_release_interface(s
->bus_num
, s
->addr
, i
);
1329 rc
= libusb_release_interface(s
->dh
, i
);
1330 usb_host_libusb_error("libusb_release_interface", rc
);
1331 s
->ifs
[i
].claimed
= false;
1335 static void usb_host_set_address(USBHostDevice
*s
, int addr
)
1337 USBDevice
*udev
= USB_DEVICE(s
);
1339 trace_usb_host_set_address(s
->bus_num
, s
->addr
, addr
);
1343 static void usb_host_set_config(USBHostDevice
*s
, int config
, USBPacket
*p
)
1347 trace_usb_host_set_config(s
->bus_num
, s
->addr
, config
);
1349 usb_host_release_interfaces(s
);
1350 if (s
->ddesc
.bNumConfigurations
!= 1) {
1351 rc
= libusb_set_configuration(s
->dh
, config
);
1353 usb_host_libusb_error("libusb_set_configuration", rc
);
1354 p
->status
= USB_RET_STALL
;
1355 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1361 p
->status
= usb_host_claim_interfaces(s
, config
);
1362 if (p
->status
!= USB_RET_SUCCESS
) {
1365 usb_host_ep_update(s
);
1368 static void usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
,
1371 USBDevice
*udev
= USB_DEVICE(s
);
1374 trace_usb_host_set_interface(s
->bus_num
, s
->addr
, iface
, alt
);
1376 usb_host_iso_free_all(s
);
1378 if (iface
>= USB_MAX_INTERFACES
) {
1379 p
->status
= USB_RET_STALL
;
1383 rc
= libusb_set_interface_alt_setting(s
->dh
, iface
, alt
);
1385 usb_host_libusb_error("libusb_set_interface_alt_setting", rc
);
1386 p
->status
= USB_RET_STALL
;
1387 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1393 udev
->altsetting
[iface
] = alt
;
1394 usb_host_ep_update(s
);
1397 static void usb_host_handle_control(USBDevice
*udev
, USBPacket
*p
,
1398 int request
, int value
, int index
,
1399 int length
, uint8_t *data
)
1401 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1405 trace_usb_host_req_control(s
->bus_num
, s
->addr
, p
, request
, value
, index
);
1407 if (s
->dh
== NULL
) {
1408 p
->status
= USB_RET_NODEV
;
1409 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1414 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1415 usb_host_set_address(s
, value
);
1416 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1419 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1420 usb_host_set_config(s
, value
& 0xff, p
);
1421 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1424 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1425 usb_host_set_interface(s
, index
, value
, p
);
1426 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1429 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
1430 if (value
== 0) { /* clear halt */
1431 int pid
= (index
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1432 libusb_clear_halt(s
->dh
, index
);
1433 usb_ep_set_halted(udev
, pid
, index
& 0x0f, 0);
1434 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1439 r
= usb_host_req_alloc(s
, p
, (request
>> 8) & USB_DIR_IN
, length
+ 8);
1442 memcpy(r
->buffer
, udev
->setup_buf
, 8);
1444 memcpy(r
->buffer
+ 8, r
->cbuf
, r
->clen
);
1447 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
1448 * to work redirected to a not superspeed capable hcd */
1449 if ((udev
->speedmask
& USB_SPEED_MASK_SUPER
) &&
1450 !(udev
->port
->speedmask
& USB_SPEED_MASK_SUPER
) &&
1451 request
== 0x8006 && value
== 0x100 && index
== 0) {
1452 r
->usb3ep0quirk
= true;
1455 libusb_fill_control_transfer(r
->xfer
, s
->dh
, r
->buffer
,
1456 usb_host_req_complete_ctrl
, r
,
1458 rc
= libusb_submit_transfer(r
->xfer
);
1460 p
->status
= USB_RET_NODEV
;
1461 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1462 p
->status
, p
->actual_length
);
1463 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1469 p
->status
= USB_RET_ASYNC
;
1472 static void usb_host_handle_data(USBDevice
*udev
, USBPacket
*p
)
1474 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1479 if (usb_host_use_combining(p
->ep
) && p
->state
== USB_PACKET_SETUP
) {
1480 p
->status
= USB_RET_ADD_TO_QUEUE
;
1484 trace_usb_host_req_data(s
->bus_num
, s
->addr
, p
,
1485 p
->pid
== USB_TOKEN_IN
,
1486 p
->ep
->nr
, p
->iov
.size
);
1488 if (s
->dh
== NULL
) {
1489 p
->status
= USB_RET_NODEV
;
1490 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1493 if (p
->ep
->halted
) {
1494 p
->status
= USB_RET_STALL
;
1495 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1499 switch (usb_ep_get_type(udev
, p
->pid
, p
->ep
->nr
)) {
1500 case USB_ENDPOINT_XFER_BULK
:
1501 size
= usb_packet_size(p
);
1502 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, size
);
1504 usb_packet_copy(p
, r
->buffer
, size
);
1506 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1509 libusb_fill_bulk_stream_transfer(r
->xfer
, s
->dh
, ep
, p
->stream
,
1511 usb_host_req_complete_data
, r
,
1514 usb_host_req_free(r
);
1515 p
->status
= USB_RET_STALL
;
1519 libusb_fill_bulk_transfer(r
->xfer
, s
->dh
, ep
,
1521 usb_host_req_complete_data
, r
,
1525 case USB_ENDPOINT_XFER_INT
:
1526 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, p
->iov
.size
);
1528 usb_packet_copy(p
, r
->buffer
, p
->iov
.size
);
1530 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1531 libusb_fill_interrupt_transfer(r
->xfer
, s
->dh
, ep
,
1532 r
->buffer
, p
->iov
.size
,
1533 usb_host_req_complete_data
, r
,
1536 case USB_ENDPOINT_XFER_ISOC
:
1537 if (p
->pid
== USB_TOKEN_IN
) {
1538 usb_host_iso_data_in(s
, p
);
1540 usb_host_iso_data_out(s
, p
);
1542 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1543 p
->status
, p
->actual_length
);
1546 p
->status
= USB_RET_STALL
;
1547 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1548 p
->status
, p
->actual_length
);
1552 rc
= libusb_submit_transfer(r
->xfer
);
1554 p
->status
= USB_RET_NODEV
;
1555 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1556 p
->status
, p
->actual_length
);
1557 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1563 p
->status
= USB_RET_ASYNC
;
1566 static void usb_host_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
1568 if (usb_host_use_combining(ep
)) {
1569 usb_ep_combine_input_packets(ep
);
1573 static void usb_host_handle_reset(USBDevice
*udev
)
1575 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1578 if (!s
->allow_one_guest_reset
&& !s
->allow_all_guest_resets
) {
1581 if (!s
->allow_all_guest_resets
&& udev
->addr
== 0) {
1585 trace_usb_host_reset(s
->bus_num
, s
->addr
);
1587 rc
= libusb_reset_device(s
->dh
);
1593 static int usb_host_alloc_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1594 int nr_eps
, int streams
)
1597 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1598 unsigned char endpoints
[30];
1601 for (i
= 0; i
< nr_eps
; i
++) {
1602 endpoints
[i
] = eps
[i
]->nr
;
1603 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1604 endpoints
[i
] |= 0x80;
1607 rc
= libusb_alloc_streams(s
->dh
, streams
, endpoints
, nr_eps
);
1609 usb_host_libusb_error("libusb_alloc_streams", rc
);
1610 } else if (rc
!= streams
) {
1611 error_report("libusb_alloc_streams: got less streams "
1612 "then requested %d < %d", rc
, streams
);
1615 return (rc
== streams
) ? 0 : -1;
1617 error_report("libusb_alloc_streams: error not implemented");
1622 static void usb_host_free_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1626 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1627 unsigned char endpoints
[30];
1630 for (i
= 0; i
< nr_eps
; i
++) {
1631 endpoints
[i
] = eps
[i
]->nr
;
1632 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1633 endpoints
[i
] |= 0x80;
1636 libusb_free_streams(s
->dh
, endpoints
, nr_eps
);
1641 * This is *NOT* about restoring state. We have absolutely no idea
1642 * what state the host device is in at the moment and whenever it is
1643 * still present in the first place. Attemping to contine where we
1644 * left off is impossible.
1646 * What we are going to do here is emulate a surprise removal of
1647 * the usb device passed through, then kick host scan so the device
1648 * will get re-attached (and re-initialized by the guest) in case it
1651 * As the device removal will change the state of other devices (usb
1652 * host controller, most likely interrupt controller too) we have to
1653 * wait with it until *all* vmstate is loaded. Thus post_load just
1654 * kicks a bottom half which then does the actual work.
1656 static void usb_host_post_load_bh(void *opaque
)
1658 USBHostDevice
*dev
= opaque
;
1659 USBDevice
*udev
= USB_DEVICE(dev
);
1661 if (dev
->dh
!= NULL
) {
1662 usb_host_close(dev
);
1664 if (udev
->attached
) {
1665 usb_device_detach(udev
);
1667 dev
->bh_postld_pending
= false;
1668 usb_host_auto_check(NULL
);
1671 static int usb_host_post_load(void *opaque
, int version_id
)
1673 USBHostDevice
*dev
= opaque
;
1675 if (!dev
->bh_postld
) {
1676 dev
->bh_postld
= qemu_bh_new(usb_host_post_load_bh
, dev
);
1678 qemu_bh_schedule(dev
->bh_postld
);
1679 dev
->bh_postld_pending
= true;
1683 static const VMStateDescription vmstate_usb_host
= {
1686 .minimum_version_id
= 1,
1687 .post_load
= usb_host_post_load
,
1688 .fields
= (VMStateField
[]) {
1689 VMSTATE_USB_DEVICE(parent_obj
, USBHostDevice
),
1690 VMSTATE_END_OF_LIST()
1694 static Property usb_host_dev_properties
[] = {
1695 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1696 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1697 DEFINE_PROP_STRING("hostport", USBHostDevice
, match
.port
),
1698 DEFINE_PROP_UINT32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1699 DEFINE_PROP_UINT32("productid", USBHostDevice
, match
.product_id
, 0),
1700 #if LIBUSB_API_VERSION >= 0x01000107
1701 DEFINE_PROP_STRING("hostdevice", USBHostDevice
, hostdevice
),
1703 DEFINE_PROP_UINT32("isobufs", USBHostDevice
, iso_urb_count
, 4),
1704 DEFINE_PROP_UINT32("isobsize", USBHostDevice
, iso_urb_frames
, 32),
1705 DEFINE_PROP_BOOL("guest-reset", USBHostDevice
,
1706 allow_one_guest_reset
, true),
1707 DEFINE_PROP_BOOL("guest-resets-all", USBHostDevice
,
1708 allow_all_guest_resets
, false),
1709 DEFINE_PROP_UINT32("loglevel", USBHostDevice
, loglevel
,
1710 LIBUSB_LOG_LEVEL_WARNING
),
1711 DEFINE_PROP_BIT("pipeline", USBHostDevice
, options
,
1712 USB_HOST_OPT_PIPELINE
, true),
1713 DEFINE_PROP_BOOL("suppress-remote-wake", USBHostDevice
,
1714 suppress_remote_wake
, true),
1715 DEFINE_PROP_END_OF_LIST(),
1718 static void usb_host_class_initfn(ObjectClass
*klass
, void *data
)
1720 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1721 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1723 uc
->realize
= usb_host_realize
;
1724 uc
->product_desc
= "USB Host Device";
1725 uc
->cancel_packet
= usb_host_cancel_packet
;
1726 uc
->handle_data
= usb_host_handle_data
;
1727 uc
->handle_control
= usb_host_handle_control
;
1728 uc
->handle_reset
= usb_host_handle_reset
;
1729 uc
->unrealize
= usb_host_unrealize
;
1730 uc
->flush_ep_queue
= usb_host_flush_ep_queue
;
1731 uc
->alloc_streams
= usb_host_alloc_streams
;
1732 uc
->free_streams
= usb_host_free_streams
;
1733 dc
->vmsd
= &vmstate_usb_host
;
1734 device_class_set_props(dc
, usb_host_dev_properties
);
1735 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
1738 static TypeInfo usb_host_dev_info
= {
1739 .name
= TYPE_USB_HOST_DEVICE
,
1740 .parent
= TYPE_USB_DEVICE
,
1741 .instance_size
= sizeof(USBHostDevice
),
1742 .class_init
= usb_host_class_initfn
,
1743 .instance_init
= usb_host_instance_init
,
1746 static void usb_host_register_types(void)
1748 type_register_static(&usb_host_dev_info
);
1751 type_init(usb_host_register_types
)
1753 /* ------------------------------------------------------------------------ */
1755 static QEMUTimer
*usb_auto_timer
;
1756 static VMChangeStateEntry
*usb_vmstate
;
1758 static void usb_host_vm_state(void *unused
, int running
, RunState state
)
1761 usb_host_auto_check(unused
);
1765 static void usb_host_auto_check(void *unused
)
1767 struct USBHostDevice
*s
;
1768 struct USBAutoFilter
*f
;
1769 libusb_device
**devs
= NULL
;
1770 struct libusb_device_descriptor ddesc
;
1771 int unconnected
= 0;
1774 if (usb_host_init() != 0) {
1778 if (runstate_is_running()) {
1779 n
= libusb_get_device_list(ctx
, &devs
);
1780 for (i
= 0; i
< n
; i
++) {
1781 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1784 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1787 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1789 if (f
->bus_num
> 0 &&
1790 f
->bus_num
!= libusb_get_bus_number(devs
[i
])) {
1794 f
->addr
!= libusb_get_device_address(devs
[i
])) {
1797 if (f
->port
!= NULL
) {
1798 char port
[16] = "-";
1799 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1800 if (strcmp(f
->port
, port
) != 0) {
1804 if (f
->vendor_id
> 0 &&
1805 f
->vendor_id
!= ddesc
.idVendor
) {
1808 if (f
->product_id
> 0 &&
1809 f
->product_id
!= ddesc
.idProduct
) {
1813 /* We got a match */
1815 if (s
->errcount
>= 3) {
1818 if (s
->dh
!= NULL
) {
1821 if (usb_host_open(s
, devs
[i
], 0) < 0) {
1828 libusb_free_device_list(devs
, 1);
1830 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1831 if (s
->dh
== NULL
) {
1844 if (unconnected
== 0) {
1845 /* nothing to watch */
1846 if (usb_auto_timer
) {
1847 timer_del(usb_auto_timer
);
1848 trace_usb_host_auto_scan_disabled();
1856 usb_vmstate
= qemu_add_vm_change_state_handler(usb_host_vm_state
, NULL
);
1858 if (!usb_auto_timer
) {
1859 usb_auto_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, usb_host_auto_check
, NULL
);
1860 if (!usb_auto_timer
) {
1863 trace_usb_host_auto_scan_enabled();
1865 timer_mod(usb_auto_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 2000);
1869 * Check whether USB host device has a USB mass storage SCSI interface
1871 bool usb_host_dev_is_scsi_storage(USBDevice
*ud
)
1873 USBHostDevice
*uhd
= USB_HOST_DEVICE(ud
);
1874 struct libusb_config_descriptor
*conf
;
1875 const struct libusb_interface_descriptor
*intf
;
1876 bool is_scsi_storage
= false;
1879 if (!uhd
|| libusb_get_active_config_descriptor(uhd
->dev
, &conf
) != 0) {
1883 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
1884 intf
= &conf
->interface
[i
].altsetting
[ud
->altsetting
[i
]];
1885 if (intf
->bInterfaceClass
== LIBUSB_CLASS_MASS_STORAGE
&&
1886 intf
->bInterfaceSubClass
== 6) { /* 6 means SCSI */
1887 is_scsi_storage
= true;
1892 libusb_free_config_descriptor(conf
);
1894 return is_scsi_storage
;
1897 void hmp_info_usbhost(Monitor
*mon
, const QDict
*qdict
)
1899 libusb_device
**devs
= NULL
;
1900 struct libusb_device_descriptor ddesc
;
1904 if (usb_host_init() != 0) {
1908 n
= libusb_get_device_list(ctx
, &devs
);
1909 for (i
= 0; i
< n
; i
++) {
1910 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1913 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1916 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1917 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1918 libusb_get_bus_number(devs
[i
]),
1919 libusb_get_device_address(devs
[i
]),
1921 speed_name
[libusb_get_device_speed(devs
[i
])]);
1922 monitor_printf(mon
, " Class %02x:", ddesc
.bDeviceClass
);
1923 monitor_printf(mon
, " USB device %04x:%04x",
1924 ddesc
.idVendor
, ddesc
.idProduct
);
1925 if (ddesc
.iProduct
) {
1926 libusb_device_handle
*handle
;
1927 if (libusb_open(devs
[i
], &handle
) == 0) {
1928 unsigned char name
[64] = "";
1929 libusb_get_string_descriptor_ascii(handle
,
1931 name
, sizeof(name
));
1932 libusb_close(handle
);
1933 monitor_printf(mon
, ", %s", name
);
1936 monitor_printf(mon
, "\n");
1938 libusb_free_device_list(devs
, 1);