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"
43 #include <sys/ioctl.h>
44 #include <linux/usbdevice_fs.h>
47 #include "qapi/error.h"
48 #include "migration/vmstate.h"
49 #include "monitor/monitor.h"
50 #include "qemu/error-report.h"
51 #include "qemu/main-loop.h"
52 #include "qemu/module.h"
53 #include "sysemu/runstate.h"
54 #include "sysemu/sysemu.h"
57 #include "hw/qdev-properties.h"
60 /* ------------------------------------------------------------------------ */
62 #define TYPE_USB_HOST_DEVICE "usb-host"
63 #define USB_HOST_DEVICE(obj) \
64 OBJECT_CHECK(USBHostDevice, (obj), TYPE_USB_HOST_DEVICE)
66 typedef struct USBHostDevice USBHostDevice
;
67 typedef struct USBHostRequest USBHostRequest
;
68 typedef struct USBHostIsoXfer USBHostIsoXfer
;
69 typedef struct USBHostIsoRing USBHostIsoRing
;
71 struct USBAutoFilter
{
79 enum USBHostDeviceOptions
{
80 USB_HOST_OPT_PIPELINE
,
83 struct USBHostDevice
{
87 struct USBAutoFilter match
;
90 uint32_t iso_urb_count
;
91 uint32_t iso_urb_frames
;
95 bool allow_one_guest_reset
;
96 bool allow_all_guest_resets
;
97 bool suppress_remote_wake
;
100 QTAILQ_ENTRY(USBHostDevice
) next
;
108 libusb_device_handle
*dh
;
109 struct libusb_device_descriptor ddesc
;
114 } ifs
[USB_MAX_INTERFACES
];
116 /* callbacks & friends */
119 bool bh_postld_pending
;
123 QTAILQ_HEAD(, USBHostRequest
) requests
;
124 QTAILQ_HEAD(, USBHostIsoRing
) isorings
;
127 struct USBHostRequest
{
131 struct libusb_transfer
*xfer
;
132 unsigned char *buffer
;
136 QTAILQ_ENTRY(USBHostRequest
) next
;
139 struct USBHostIsoXfer
{
140 USBHostIsoRing
*ring
;
141 struct libusb_transfer
*xfer
;
144 QTAILQ_ENTRY(USBHostIsoXfer
) next
;
147 struct USBHostIsoRing
{
150 QTAILQ_HEAD(, USBHostIsoXfer
) unused
;
151 QTAILQ_HEAD(, USBHostIsoXfer
) inflight
;
152 QTAILQ_HEAD(, USBHostIsoXfer
) copy
;
153 QTAILQ_ENTRY(USBHostIsoRing
) next
;
156 static QTAILQ_HEAD(, USBHostDevice
) hostdevs
=
157 QTAILQ_HEAD_INITIALIZER(hostdevs
);
159 static void usb_host_auto_check(void *unused
);
160 static void usb_host_release_interfaces(USBHostDevice
*s
);
161 static void usb_host_nodev(USBHostDevice
*s
);
162 static void usb_host_detach_kernel(USBHostDevice
*s
);
163 static void usb_host_attach_kernel(USBHostDevice
*s
);
165 /* ------------------------------------------------------------------------ */
167 #ifndef LIBUSB_LOG_LEVEL_WARNING /* older libusb didn't define these */
168 #define LIBUSB_LOG_LEVEL_WARNING 2
171 /* ------------------------------------------------------------------------ */
173 #define CONTROL_TIMEOUT 10000 /* 10 sec */
174 #define BULK_TIMEOUT 0 /* unlimited */
175 #define INTR_TIMEOUT 0 /* unlimited */
177 #ifndef LIBUSB_API_VERSION
178 # define LIBUSB_API_VERSION LIBUSBX_API_VERSION
180 #if LIBUSB_API_VERSION >= 0x01000103
181 # define HAVE_STREAMS 1
184 static const char *speed_name
[] = {
185 [LIBUSB_SPEED_UNKNOWN
] = "?",
186 [LIBUSB_SPEED_LOW
] = "1.5",
187 [LIBUSB_SPEED_FULL
] = "12",
188 [LIBUSB_SPEED_HIGH
] = "480",
189 [LIBUSB_SPEED_SUPER
] = "5000",
192 static const unsigned int speed_map
[] = {
193 [LIBUSB_SPEED_LOW
] = USB_SPEED_LOW
,
194 [LIBUSB_SPEED_FULL
] = USB_SPEED_FULL
,
195 [LIBUSB_SPEED_HIGH
] = USB_SPEED_HIGH
,
196 [LIBUSB_SPEED_SUPER
] = USB_SPEED_SUPER
,
199 static const unsigned int status_map
[] = {
200 [LIBUSB_TRANSFER_COMPLETED
] = USB_RET_SUCCESS
,
201 [LIBUSB_TRANSFER_ERROR
] = USB_RET_IOERROR
,
202 [LIBUSB_TRANSFER_TIMED_OUT
] = USB_RET_IOERROR
,
203 [LIBUSB_TRANSFER_CANCELLED
] = USB_RET_IOERROR
,
204 [LIBUSB_TRANSFER_STALL
] = USB_RET_STALL
,
205 [LIBUSB_TRANSFER_NO_DEVICE
] = USB_RET_NODEV
,
206 [LIBUSB_TRANSFER_OVERFLOW
] = USB_RET_BABBLE
,
209 static const char *err_names
[] = {
210 [-LIBUSB_ERROR_IO
] = "IO",
211 [-LIBUSB_ERROR_INVALID_PARAM
] = "INVALID_PARAM",
212 [-LIBUSB_ERROR_ACCESS
] = "ACCESS",
213 [-LIBUSB_ERROR_NO_DEVICE
] = "NO_DEVICE",
214 [-LIBUSB_ERROR_NOT_FOUND
] = "NOT_FOUND",
215 [-LIBUSB_ERROR_BUSY
] = "BUSY",
216 [-LIBUSB_ERROR_TIMEOUT
] = "TIMEOUT",
217 [-LIBUSB_ERROR_OVERFLOW
] = "OVERFLOW",
218 [-LIBUSB_ERROR_PIPE
] = "PIPE",
219 [-LIBUSB_ERROR_INTERRUPTED
] = "INTERRUPTED",
220 [-LIBUSB_ERROR_NO_MEM
] = "NO_MEM",
221 [-LIBUSB_ERROR_NOT_SUPPORTED
] = "NOT_SUPPORTED",
222 [-LIBUSB_ERROR_OTHER
] = "OTHER",
225 static libusb_context
*ctx
;
226 static uint32_t loglevel
;
230 static void usb_host_handle_fd(void *opaque
)
232 struct timeval tv
= { 0, 0 };
233 libusb_handle_events_timeout(ctx
, &tv
);
236 static void usb_host_add_fd(int fd
, short events
, void *user_data
)
238 qemu_set_fd_handler(fd
,
239 (events
& POLLIN
) ? usb_host_handle_fd
: NULL
,
240 (events
& POLLOUT
) ? usb_host_handle_fd
: NULL
,
244 static void usb_host_del_fd(int fd
, void *user_data
)
246 qemu_set_fd_handler(fd
, NULL
, NULL
, NULL
);
249 #endif /* !CONFIG_WIN32 */
251 static int usb_host_init(void)
254 const struct libusb_pollfd
**poll
;
261 rc
= libusb_init(&ctx
);
265 #if LIBUSB_API_VERSION >= 0x01000106
266 libusb_set_option(ctx
, LIBUSB_OPTION_LOG_LEVEL
, loglevel
);
268 libusb_set_debug(ctx
, loglevel
);
271 /* FIXME: add support for Windows. */
273 libusb_set_pollfd_notifiers(ctx
, usb_host_add_fd
,
276 poll
= libusb_get_pollfds(ctx
);
279 for (i
= 0; poll
[i
] != NULL
; i
++) {
280 usb_host_add_fd(poll
[i
]->fd
, poll
[i
]->events
, ctx
);
288 static int usb_host_get_port(libusb_device
*dev
, char *port
, size_t len
)
294 #if LIBUSB_API_VERSION >= 0x01000102
295 rc
= libusb_get_port_numbers(dev
, path
, 7);
297 rc
= libusb_get_port_path(ctx
, dev
, path
, 7);
302 off
= snprintf(port
, len
, "%d", path
[0]);
303 for (i
= 1; i
< rc
; i
++) {
304 off
+= snprintf(port
+off
, len
-off
, ".%d", path
[i
]);
309 static void usb_host_libusb_error(const char *func
, int rc
)
317 if (-rc
< ARRAY_SIZE(err_names
) && err_names
[-rc
]) {
318 errname
= err_names
[-rc
];
322 error_report("%s: %d [%s]", func
, rc
, errname
);
325 /* ------------------------------------------------------------------------ */
327 static bool usb_host_use_combining(USBEndpoint
*ep
)
334 if (ep
->pid
!= USB_TOKEN_IN
) {
337 type
= usb_ep_get_type(ep
->dev
, ep
->pid
, ep
->nr
);
338 if (type
!= USB_ENDPOINT_XFER_BULK
) {
344 /* ------------------------------------------------------------------------ */
346 static USBHostRequest
*usb_host_req_alloc(USBHostDevice
*s
, USBPacket
*p
,
347 bool in
, size_t bufsize
)
349 USBHostRequest
*r
= g_new0(USBHostRequest
, 1);
354 r
->xfer
= libusb_alloc_transfer(0);
356 r
->buffer
= g_malloc(bufsize
);
358 QTAILQ_INSERT_TAIL(&s
->requests
, r
, next
);
362 static void usb_host_req_free(USBHostRequest
*r
)
364 QTAILQ_REMOVE(&r
->host
->requests
, r
, next
);
365 libusb_free_transfer(r
->xfer
);
370 static USBHostRequest
*usb_host_req_find(USBHostDevice
*s
, USBPacket
*p
)
374 QTAILQ_FOREACH(r
, &s
->requests
, next
) {
382 static void LIBUSB_CALL
usb_host_req_complete_ctrl(struct libusb_transfer
*xfer
)
384 USBHostRequest
*r
= xfer
->user_data
;
385 USBHostDevice
*s
= r
->host
;
386 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
389 goto out
; /* request was canceled */
392 r
->p
->status
= status_map
[xfer
->status
];
393 r
->p
->actual_length
= xfer
->actual_length
;
394 if (r
->in
&& xfer
->actual_length
) {
395 USBDevice
*udev
= USB_DEVICE(s
);
396 struct libusb_config_descriptor
*conf
= (void *)r
->cbuf
;
397 memcpy(r
->cbuf
, r
->buffer
+ 8, xfer
->actual_length
);
399 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
400 * to work redirected to a not superspeed capable hcd */
401 if (r
->usb3ep0quirk
&& xfer
->actual_length
>= 18 &&
406 *If this is GET_DESCRIPTOR request for configuration descriptor,
407 * remove 'remote wakeup' flag from it to prevent idle power down
410 if (s
->suppress_remote_wake
&&
411 udev
->setup_buf
[0] == USB_DIR_IN
&&
412 udev
->setup_buf
[1] == USB_REQ_GET_DESCRIPTOR
&&
413 udev
->setup_buf
[3] == USB_DT_CONFIG
&& udev
->setup_buf
[2] == 0 &&
414 xfer
->actual_length
>
415 offsetof(struct libusb_config_descriptor
, bmAttributes
) &&
416 (conf
->bmAttributes
& USB_CFG_ATT_WAKEUP
)) {
417 trace_usb_host_remote_wakeup_removed(s
->bus_num
, s
->addr
);
418 conf
->bmAttributes
&= ~USB_CFG_ATT_WAKEUP
;
421 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
422 r
->p
->status
, r
->p
->actual_length
);
423 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
426 usb_host_req_free(r
);
432 static void LIBUSB_CALL
usb_host_req_complete_data(struct libusb_transfer
*xfer
)
434 USBHostRequest
*r
= xfer
->user_data
;
435 USBHostDevice
*s
= r
->host
;
436 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
439 goto out
; /* request was canceled */
442 r
->p
->status
= status_map
[xfer
->status
];
443 if (r
->in
&& xfer
->actual_length
) {
444 usb_packet_copy(r
->p
, r
->buffer
, xfer
->actual_length
);
446 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
447 r
->p
->status
, r
->p
->actual_length
);
448 if (usb_host_use_combining(r
->p
->ep
)) {
449 usb_combined_input_packet_complete(USB_DEVICE(s
), r
->p
);
451 usb_packet_complete(USB_DEVICE(s
), r
->p
);
455 usb_host_req_free(r
);
461 static void usb_host_req_abort(USBHostRequest
*r
)
463 USBHostDevice
*s
= r
->host
;
464 bool inflight
= (r
->p
&& r
->p
->state
== USB_PACKET_ASYNC
);
467 r
->p
->status
= USB_RET_NODEV
;
468 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
469 r
->p
->status
, r
->p
->actual_length
);
470 if (r
->p
->ep
->nr
== 0) {
471 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
473 usb_packet_complete(USB_DEVICE(s
), r
->p
);
477 libusb_cancel_transfer(r
->xfer
);
481 /* ------------------------------------------------------------------------ */
483 static void LIBUSB_CALL
484 usb_host_req_complete_iso(struct libusb_transfer
*transfer
)
486 USBHostIsoXfer
*xfer
= transfer
->user_data
;
489 /* USBHostIsoXfer released while inflight */
490 g_free(transfer
->buffer
);
491 libusb_free_transfer(transfer
);
495 QTAILQ_REMOVE(&xfer
->ring
->inflight
, xfer
, next
);
496 if (QTAILQ_EMPTY(&xfer
->ring
->inflight
)) {
497 USBHostDevice
*s
= xfer
->ring
->host
;
498 trace_usb_host_iso_stop(s
->bus_num
, s
->addr
, xfer
->ring
->ep
->nr
);
500 if (xfer
->ring
->ep
->pid
== USB_TOKEN_IN
) {
501 QTAILQ_INSERT_TAIL(&xfer
->ring
->copy
, xfer
, next
);
502 usb_wakeup(xfer
->ring
->ep
, 0);
504 QTAILQ_INSERT_TAIL(&xfer
->ring
->unused
, xfer
, next
);
508 static USBHostIsoRing
*usb_host_iso_alloc(USBHostDevice
*s
, USBEndpoint
*ep
)
510 USBHostIsoRing
*ring
= g_new0(USBHostIsoRing
, 1);
511 USBHostIsoXfer
*xfer
;
512 /* FIXME: check interval (for now assume one xfer per frame) */
513 int packets
= s
->iso_urb_frames
;
518 QTAILQ_INIT(&ring
->unused
);
519 QTAILQ_INIT(&ring
->inflight
);
520 QTAILQ_INIT(&ring
->copy
);
521 QTAILQ_INSERT_TAIL(&s
->isorings
, ring
, next
);
523 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
524 xfer
= g_new0(USBHostIsoXfer
, 1);
526 xfer
->xfer
= libusb_alloc_transfer(packets
);
527 xfer
->xfer
->dev_handle
= s
->dh
;
528 xfer
->xfer
->type
= LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
;
530 xfer
->xfer
->endpoint
= ring
->ep
->nr
;
531 if (ring
->ep
->pid
== USB_TOKEN_IN
) {
532 xfer
->xfer
->endpoint
|= USB_DIR_IN
;
534 xfer
->xfer
->callback
= usb_host_req_complete_iso
;
535 xfer
->xfer
->user_data
= xfer
;
537 xfer
->xfer
->num_iso_packets
= packets
;
538 xfer
->xfer
->length
= ring
->ep
->max_packet_size
* packets
;
539 xfer
->xfer
->buffer
= g_malloc0(xfer
->xfer
->length
);
541 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
547 static USBHostIsoRing
*usb_host_iso_find(USBHostDevice
*s
, USBEndpoint
*ep
)
549 USBHostIsoRing
*ring
;
551 QTAILQ_FOREACH(ring
, &s
->isorings
, next
) {
552 if (ring
->ep
== ep
) {
559 static void usb_host_iso_reset_xfer(USBHostIsoXfer
*xfer
)
561 libusb_set_iso_packet_lengths(xfer
->xfer
,
562 xfer
->ring
->ep
->max_packet_size
);
564 xfer
->copy_complete
= false;
567 static void usb_host_iso_free_xfer(USBHostIsoXfer
*xfer
, bool inflight
)
570 xfer
->xfer
->user_data
= NULL
;
572 g_free(xfer
->xfer
->buffer
);
573 libusb_free_transfer(xfer
->xfer
);
578 static void usb_host_iso_free(USBHostIsoRing
*ring
)
580 USBHostIsoXfer
*xfer
;
582 while ((xfer
= QTAILQ_FIRST(&ring
->inflight
)) != NULL
) {
583 QTAILQ_REMOVE(&ring
->inflight
, xfer
, next
);
584 usb_host_iso_free_xfer(xfer
, true);
586 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
587 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
588 usb_host_iso_free_xfer(xfer
, false);
590 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
) {
591 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
592 usb_host_iso_free_xfer(xfer
, false);
595 QTAILQ_REMOVE(&ring
->host
->isorings
, ring
, next
);
599 static void usb_host_iso_free_all(USBHostDevice
*s
)
601 USBHostIsoRing
*ring
;
603 while ((ring
= QTAILQ_FIRST(&s
->isorings
)) != NULL
) {
604 usb_host_iso_free(ring
);
608 static bool usb_host_iso_data_copy(USBHostIsoXfer
*xfer
, USBPacket
*p
)
613 buf
= libusb_get_iso_packet_buffer_simple(xfer
->xfer
, xfer
->packet
);
614 if (p
->pid
== USB_TOKEN_OUT
) {
616 if (psize
> xfer
->ring
->ep
->max_packet_size
) {
617 /* should not happen (guest bug) */
618 psize
= xfer
->ring
->ep
->max_packet_size
;
620 xfer
->xfer
->iso_packet_desc
[xfer
->packet
].length
= psize
;
622 psize
= xfer
->xfer
->iso_packet_desc
[xfer
->packet
].actual_length
;
623 if (psize
> p
->iov
.size
) {
624 /* should not happen (guest bug) */
628 usb_packet_copy(p
, buf
, psize
);
630 xfer
->copy_complete
= (xfer
->packet
== xfer
->xfer
->num_iso_packets
);
631 return xfer
->copy_complete
;
634 static void usb_host_iso_data_in(USBHostDevice
*s
, USBPacket
*p
)
636 USBHostIsoRing
*ring
;
637 USBHostIsoXfer
*xfer
;
638 bool disconnect
= false;
641 ring
= usb_host_iso_find(s
, p
->ep
);
643 ring
= usb_host_iso_alloc(s
, p
->ep
);
646 /* copy data to guest */
647 xfer
= QTAILQ_FIRST(&ring
->copy
);
649 if (usb_host_iso_data_copy(xfer
, p
)) {
650 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
651 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
655 /* submit empty bufs to host */
656 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
657 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
658 usb_host_iso_reset_xfer(xfer
);
659 rc
= libusb_submit_transfer(xfer
->xfer
);
661 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
662 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
663 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
668 if (QTAILQ_EMPTY(&ring
->inflight
)) {
669 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
671 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
679 static void usb_host_iso_data_out(USBHostDevice
*s
, USBPacket
*p
)
681 USBHostIsoRing
*ring
;
682 USBHostIsoXfer
*xfer
;
683 bool disconnect
= false;
686 ring
= usb_host_iso_find(s
, p
->ep
);
688 ring
= usb_host_iso_alloc(s
, p
->ep
);
691 /* copy data from guest */
692 xfer
= QTAILQ_FIRST(&ring
->copy
);
693 while (xfer
!= NULL
&& xfer
->copy_complete
) {
695 xfer
= QTAILQ_NEXT(xfer
, next
);
698 xfer
= QTAILQ_FIRST(&ring
->unused
);
700 trace_usb_host_iso_out_of_bufs(s
->bus_num
, s
->addr
, p
->ep
->nr
);
703 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
704 usb_host_iso_reset_xfer(xfer
);
705 QTAILQ_INSERT_TAIL(&ring
->copy
, xfer
, next
);
707 usb_host_iso_data_copy(xfer
, p
);
709 if (QTAILQ_EMPTY(&ring
->inflight
)) {
710 /* wait until half of our buffers are filled
711 before kicking the iso out stream */
712 if (filled
*2 < s
->iso_urb_count
) {
717 /* submit filled bufs to host */
718 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
&&
719 xfer
->copy_complete
) {
720 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
721 rc
= libusb_submit_transfer(xfer
->xfer
);
723 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
724 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
725 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
730 if (QTAILQ_EMPTY(&ring
->inflight
)) {
731 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
733 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
741 /* ------------------------------------------------------------------------ */
743 static void usb_host_speed_compat(USBHostDevice
*s
)
745 USBDevice
*udev
= USB_DEVICE(s
);
746 struct libusb_config_descriptor
*conf
;
747 const struct libusb_interface_descriptor
*intf
;
748 const struct libusb_endpoint_descriptor
*endp
;
750 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
752 bool compat_high
= true;
753 bool compat_full
= true;
758 rc
= libusb_get_config_descriptor(s
->dev
, c
, &conf
);
762 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
763 for (a
= 0; a
< conf
->interface
[i
].num_altsetting
; a
++) {
764 intf
= &conf
->interface
[i
].altsetting
[a
];
765 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
766 endp
= &intf
->endpoint
[e
];
767 type
= endp
->bmAttributes
& 0x3;
773 case 0x02: /* BULK */
775 rc
= libusb_get_ss_endpoint_companion_descriptor
776 (ctx
, endp
, &endp_ss_comp
);
777 if (rc
== LIBUSB_SUCCESS
) {
778 int streams
= endp_ss_comp
->bmAttributes
& 0x1f;
783 libusb_free_ss_endpoint_companion_descriptor
788 case 0x03: /* INTERRUPT */
789 if (endp
->wMaxPacketSize
> 64) {
792 if (endp
->wMaxPacketSize
> 1024) {
800 libusb_free_config_descriptor(conf
);
803 udev
->speedmask
= (1 << udev
->speed
);
804 if (udev
->speed
== USB_SPEED_SUPER
&& compat_high
) {
805 udev
->speedmask
|= USB_SPEED_MASK_HIGH
;
807 if (udev
->speed
== USB_SPEED_SUPER
&& compat_full
) {
808 udev
->speedmask
|= USB_SPEED_MASK_FULL
;
810 if (udev
->speed
== USB_SPEED_HIGH
&& compat_full
) {
811 udev
->speedmask
|= USB_SPEED_MASK_FULL
;
815 static void usb_host_ep_update(USBHostDevice
*s
)
817 static const char *tname
[] = {
818 [USB_ENDPOINT_XFER_CONTROL
] = "control",
819 [USB_ENDPOINT_XFER_ISOC
] = "isoc",
820 [USB_ENDPOINT_XFER_BULK
] = "bulk",
821 [USB_ENDPOINT_XFER_INT
] = "int",
823 USBDevice
*udev
= USB_DEVICE(s
);
824 struct libusb_config_descriptor
*conf
;
825 const struct libusb_interface_descriptor
*intf
;
826 const struct libusb_endpoint_descriptor
*endp
;
828 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
835 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
839 trace_usb_host_parse_config(s
->bus_num
, s
->addr
,
840 conf
->bConfigurationValue
, true);
842 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
843 assert(udev
->altsetting
[i
] < conf
->interface
[i
].num_altsetting
);
844 intf
= &conf
->interface
[i
].altsetting
[udev
->altsetting
[i
]];
845 trace_usb_host_parse_interface(s
->bus_num
, s
->addr
,
846 intf
->bInterfaceNumber
,
847 intf
->bAlternateSetting
, true);
848 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
849 endp
= &intf
->endpoint
[e
];
851 devep
= endp
->bEndpointAddress
;
852 pid
= (devep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
854 type
= endp
->bmAttributes
& 0x3;
857 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
858 "invalid endpoint address");
861 if (usb_ep_get_type(udev
, pid
, ep
) != USB_ENDPOINT_XFER_INVALID
) {
862 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
863 "duplicate endpoint address");
867 trace_usb_host_parse_endpoint(s
->bus_num
, s
->addr
, ep
,
868 (devep
& USB_DIR_IN
) ? "in" : "out",
870 usb_ep_set_max_packet_size(udev
, pid
, ep
,
871 endp
->wMaxPacketSize
);
872 usb_ep_set_type(udev
, pid
, ep
, type
);
873 usb_ep_set_ifnum(udev
, pid
, ep
, i
);
874 usb_ep_set_halted(udev
, pid
, ep
, 0);
876 if (type
== LIBUSB_TRANSFER_TYPE_BULK
&&
877 libusb_get_ss_endpoint_companion_descriptor(ctx
, endp
,
878 &endp_ss_comp
) == LIBUSB_SUCCESS
) {
879 usb_ep_set_max_streams(udev
, pid
, ep
,
880 endp_ss_comp
->bmAttributes
);
881 libusb_free_ss_endpoint_companion_descriptor(endp_ss_comp
);
887 libusb_free_config_descriptor(conf
);
890 static int usb_host_open(USBHostDevice
*s
, libusb_device
*dev
, int hostfd
)
892 USBDevice
*udev
= USB_DEVICE(s
);
897 Error
*local_err
= NULL
;
899 if (s
->bh_postld_pending
) {
907 bus_num
= libusb_get_bus_number(dev
);
908 addr
= libusb_get_device_address(dev
);
909 trace_usb_host_open_started(bus_num
, addr
);
911 rc
= libusb_open(dev
, &s
->dh
);
916 #if LIBUSB_API_VERSION >= 0x01000107 && !defined(CONFIG_WIN32)
917 trace_usb_host_open_hostfd(hostfd
);
919 rc
= libusb_wrap_sys_device(ctx
, hostfd
, &s
->dh
);
924 dev
= libusb_get_device(s
->dh
);
925 bus_num
= libusb_get_bus_number(dev
);
926 addr
= libusb_get_device_address(dev
);
928 g_assert_not_reached();
933 s
->bus_num
= bus_num
;
936 usb_host_detach_kernel(s
);
938 libusb_get_device_descriptor(dev
, &s
->ddesc
);
939 usb_host_get_port(s
->dev
, s
->port
, sizeof(s
->port
));
942 usb_host_ep_update(s
);
944 libusb_speed
= libusb_get_device_speed(dev
);
946 if (hostfd
&& libusb_speed
== 0) {
948 * Workaround libusb bug: libusb_get_device_speed() does not
949 * work for libusb_wrap_sys_device() devices in v1.0.23.
951 * Speeds are defined in linux/usb/ch9.h, file not included
952 * due to name conflicts.
954 int rc
= ioctl(hostfd
, USBDEVFS_GET_SPEED
, NULL
);
957 libusb_speed
= LIBUSB_SPEED_LOW
;
960 libusb_speed
= LIBUSB_SPEED_FULL
;
963 case 4: /* wireless */
964 libusb_speed
= LIBUSB_SPEED_HIGH
;
967 case 6: /* super plus */
968 libusb_speed
= LIBUSB_SPEED_SUPER
;
973 udev
->speed
= speed_map
[libusb_speed
];
974 usb_host_speed_compat(s
);
976 if (s
->ddesc
.iProduct
) {
977 libusb_get_string_descriptor_ascii(s
->dh
, s
->ddesc
.iProduct
,
978 (unsigned char *)udev
->product_desc
,
979 sizeof(udev
->product_desc
));
981 snprintf(udev
->product_desc
, sizeof(udev
->product_desc
),
982 "host:%d.%d", bus_num
, addr
);
985 usb_device_attach(udev
, &local_err
);
987 error_report_err(local_err
);
991 trace_usb_host_open_success(bus_num
, addr
);
995 trace_usb_host_open_failure(bus_num
, addr
);
997 usb_host_release_interfaces(s
);
998 libusb_reset_device(s
->dh
);
999 usb_host_attach_kernel(s
);
1000 libusb_close(s
->dh
);
1007 static void usb_host_abort_xfers(USBHostDevice
*s
)
1009 USBHostRequest
*r
, *rtmp
;
1012 QTAILQ_FOREACH_SAFE(r
, &s
->requests
, next
, rtmp
) {
1013 usb_host_req_abort(r
);
1016 while (QTAILQ_FIRST(&s
->requests
) != NULL
) {
1018 memset(&tv
, 0, sizeof(tv
));
1020 libusb_handle_events_timeout(ctx
, &tv
);
1023 * Don't wait forever for libusb calling the complete
1024 * callback (which will unlink and free the request).
1026 * Leaking memory here, to make sure libusb will not
1027 * access memory which we have released already.
1029 QTAILQ_FOREACH_SAFE(r
, &s
->requests
, next
, rtmp
) {
1030 QTAILQ_REMOVE(&s
->requests
, r
, next
);
1037 static int usb_host_close(USBHostDevice
*s
)
1039 USBDevice
*udev
= USB_DEVICE(s
);
1041 if (s
->dh
== NULL
) {
1045 trace_usb_host_close(s
->bus_num
, s
->addr
);
1047 usb_host_abort_xfers(s
);
1048 usb_host_iso_free_all(s
);
1050 if (udev
->attached
) {
1051 usb_device_detach(udev
);
1054 usb_host_release_interfaces(s
);
1055 libusb_reset_device(s
->dh
);
1056 usb_host_attach_kernel(s
);
1057 libusb_close(s
->dh
);
1061 if (s
->hostfd
!= -1) {
1066 usb_host_auto_check(NULL
);
1070 static void usb_host_nodev_bh(void *opaque
)
1072 USBHostDevice
*s
= opaque
;
1076 static void usb_host_nodev(USBHostDevice
*s
)
1079 s
->bh_nodev
= qemu_bh_new(usb_host_nodev_bh
, s
);
1081 qemu_bh_schedule(s
->bh_nodev
);
1084 static void usb_host_exit_notifier(struct Notifier
*n
, void *data
)
1086 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
1089 usb_host_abort_xfers(s
);
1090 usb_host_release_interfaces(s
);
1091 libusb_reset_device(s
->dh
);
1092 usb_host_attach_kernel(s
);
1093 libusb_close(s
->dh
);
1097 static libusb_device
*usb_host_find_ref(int bus
, int addr
)
1099 libusb_device
**devs
= NULL
;
1100 libusb_device
*ret
= NULL
;
1103 n
= libusb_get_device_list(ctx
, &devs
);
1104 for (i
= 0; i
< n
; i
++) {
1105 if (libusb_get_bus_number(devs
[i
]) == bus
&&
1106 libusb_get_device_address(devs
[i
]) == addr
) {
1107 ret
= libusb_ref_device(devs
[i
]);
1111 libusb_free_device_list(devs
, 1);
1115 static void usb_host_realize(USBDevice
*udev
, Error
**errp
)
1117 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1118 libusb_device
*ldev
;
1121 if (usb_host_init() != 0) {
1122 error_setg(errp
, "failed to init libusb");
1125 if (s
->match
.vendor_id
> 0xffff) {
1126 error_setg(errp
, "vendorid out of range");
1129 if (s
->match
.product_id
> 0xffff) {
1130 error_setg(errp
, "productid out of range");
1133 if (s
->match
.addr
> 127) {
1134 error_setg(errp
, "hostaddr out of range");
1138 loglevel
= s
->loglevel
;
1139 udev
->flags
|= (1 << USB_DEV_FLAG_IS_HOST
);
1140 udev
->auto_attach
= 0;
1141 QTAILQ_INIT(&s
->requests
);
1142 QTAILQ_INIT(&s
->isorings
);
1145 #if LIBUSB_API_VERSION >= 0x01000107 && !defined(CONFIG_WIN32)
1146 if (s
->hostdevice
) {
1148 s
->needs_autoscan
= false;
1149 fd
= qemu_open(s
->hostdevice
, O_RDWR
);
1151 error_setg_errno(errp
, errno
, "failed to open %s", s
->hostdevice
);
1154 rc
= usb_host_open(s
, NULL
, fd
);
1156 error_setg(errp
, "failed to open host usb device %s", s
->hostdevice
);
1161 if (s
->match
.addr
&& s
->match
.bus_num
&&
1162 !s
->match
.vendor_id
&&
1163 !s
->match
.product_id
&&
1165 s
->needs_autoscan
= false;
1166 ldev
= usb_host_find_ref(s
->match
.bus_num
,
1169 error_setg(errp
, "failed to find host usb device %d:%d",
1170 s
->match
.bus_num
, s
->match
.addr
);
1173 rc
= usb_host_open(s
, ldev
, 0);
1174 libusb_unref_device(ldev
);
1176 error_setg(errp
, "failed to open host usb device %d:%d",
1177 s
->match
.bus_num
, s
->match
.addr
);
1181 s
->needs_autoscan
= true;
1182 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
1183 usb_host_auto_check(NULL
);
1186 s
->exit
.notify
= usb_host_exit_notifier
;
1187 qemu_add_exit_notifier(&s
->exit
);
1190 static void usb_host_instance_init(Object
*obj
)
1192 USBDevice
*udev
= USB_DEVICE(obj
);
1193 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1195 device_add_bootindex_property(obj
, &s
->bootindex
,
1200 static void usb_host_unrealize(USBDevice
*udev
)
1202 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1204 qemu_remove_exit_notifier(&s
->exit
);
1205 if (s
->needs_autoscan
) {
1206 QTAILQ_REMOVE(&hostdevs
, s
, next
);
1211 static void usb_host_cancel_packet(USBDevice
*udev
, USBPacket
*p
)
1213 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1217 usb_combined_packet_cancel(udev
, p
);
1221 trace_usb_host_req_canceled(s
->bus_num
, s
->addr
, p
);
1223 r
= usb_host_req_find(s
, p
);
1225 r
->p
= NULL
; /* mark as dead */
1226 libusb_cancel_transfer(r
->xfer
);
1230 static void usb_host_detach_kernel(USBHostDevice
*s
)
1232 struct libusb_config_descriptor
*conf
;
1235 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1239 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1240 rc
= libusb_kernel_driver_active(s
->dh
, i
);
1241 usb_host_libusb_error("libusb_kernel_driver_active", rc
);
1244 s
->ifs
[i
].detached
= true;
1248 trace_usb_host_detach_kernel(s
->bus_num
, s
->addr
, i
);
1249 rc
= libusb_detach_kernel_driver(s
->dh
, i
);
1250 usb_host_libusb_error("libusb_detach_kernel_driver", rc
);
1251 s
->ifs
[i
].detached
= true;
1253 libusb_free_config_descriptor(conf
);
1256 static void usb_host_attach_kernel(USBHostDevice
*s
)
1258 struct libusb_config_descriptor
*conf
;
1261 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1265 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1266 if (!s
->ifs
[i
].detached
) {
1269 trace_usb_host_attach_kernel(s
->bus_num
, s
->addr
, i
);
1270 libusb_attach_kernel_driver(s
->dh
, i
);
1271 s
->ifs
[i
].detached
= false;
1273 libusb_free_config_descriptor(conf
);
1276 static int usb_host_claim_interfaces(USBHostDevice
*s
, int configuration
)
1278 USBDevice
*udev
= USB_DEVICE(s
);
1279 struct libusb_config_descriptor
*conf
;
1282 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1283 udev
->altsetting
[i
] = 0;
1285 udev
->ninterfaces
= 0;
1286 udev
->configuration
= 0;
1288 usb_host_detach_kernel(s
);
1290 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1292 if (rc
== LIBUSB_ERROR_NOT_FOUND
) {
1293 /* address state - ignore */
1294 return USB_RET_SUCCESS
;
1296 return USB_RET_STALL
;
1300 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1301 trace_usb_host_claim_interface(s
->bus_num
, s
->addr
, configuration
, i
);
1302 rc
= libusb_claim_interface(s
->dh
, i
);
1304 s
->ifs
[i
].claimed
= true;
1305 if (++claimed
== conf
->bNumInterfaces
) {
1310 if (claimed
!= conf
->bNumInterfaces
) {
1311 return USB_RET_STALL
;
1314 udev
->ninterfaces
= conf
->bNumInterfaces
;
1315 udev
->configuration
= configuration
;
1317 libusb_free_config_descriptor(conf
);
1318 return USB_RET_SUCCESS
;
1321 static void usb_host_release_interfaces(USBHostDevice
*s
)
1325 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1326 if (!s
->ifs
[i
].claimed
) {
1329 trace_usb_host_release_interface(s
->bus_num
, s
->addr
, i
);
1330 rc
= libusb_release_interface(s
->dh
, i
);
1331 usb_host_libusb_error("libusb_release_interface", rc
);
1332 s
->ifs
[i
].claimed
= false;
1336 static void usb_host_set_address(USBHostDevice
*s
, int addr
)
1338 USBDevice
*udev
= USB_DEVICE(s
);
1340 trace_usb_host_set_address(s
->bus_num
, s
->addr
, addr
);
1344 static void usb_host_set_config(USBHostDevice
*s
, int config
, USBPacket
*p
)
1348 trace_usb_host_set_config(s
->bus_num
, s
->addr
, config
);
1350 usb_host_release_interfaces(s
);
1351 if (s
->ddesc
.bNumConfigurations
!= 1) {
1352 rc
= libusb_set_configuration(s
->dh
, config
);
1354 usb_host_libusb_error("libusb_set_configuration", rc
);
1355 p
->status
= USB_RET_STALL
;
1356 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1362 p
->status
= usb_host_claim_interfaces(s
, config
);
1363 if (p
->status
!= USB_RET_SUCCESS
) {
1366 usb_host_ep_update(s
);
1369 static void usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
,
1372 USBDevice
*udev
= USB_DEVICE(s
);
1375 trace_usb_host_set_interface(s
->bus_num
, s
->addr
, iface
, alt
);
1377 usb_host_iso_free_all(s
);
1379 if (iface
>= USB_MAX_INTERFACES
) {
1380 p
->status
= USB_RET_STALL
;
1384 rc
= libusb_set_interface_alt_setting(s
->dh
, iface
, alt
);
1386 usb_host_libusb_error("libusb_set_interface_alt_setting", rc
);
1387 p
->status
= USB_RET_STALL
;
1388 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1394 udev
->altsetting
[iface
] = alt
;
1395 usb_host_ep_update(s
);
1398 static void usb_host_handle_control(USBDevice
*udev
, USBPacket
*p
,
1399 int request
, int value
, int index
,
1400 int length
, uint8_t *data
)
1402 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1406 trace_usb_host_req_control(s
->bus_num
, s
->addr
, p
, request
, value
, index
);
1408 if (s
->dh
== NULL
) {
1409 p
->status
= USB_RET_NODEV
;
1410 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1415 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1416 usb_host_set_address(s
, value
);
1417 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1420 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1421 usb_host_set_config(s
, value
& 0xff, p
);
1422 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1425 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1426 usb_host_set_interface(s
, index
, value
, p
);
1427 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1430 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
1431 if (value
== 0) { /* clear halt */
1432 int pid
= (index
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1433 libusb_clear_halt(s
->dh
, index
);
1434 usb_ep_set_halted(udev
, pid
, index
& 0x0f, 0);
1435 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1440 r
= usb_host_req_alloc(s
, p
, (request
>> 8) & USB_DIR_IN
, length
+ 8);
1443 memcpy(r
->buffer
, udev
->setup_buf
, 8);
1445 memcpy(r
->buffer
+ 8, r
->cbuf
, r
->clen
);
1448 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
1449 * to work redirected to a not superspeed capable hcd */
1450 if ((udev
->speedmask
& USB_SPEED_MASK_SUPER
) &&
1451 !(udev
->port
->speedmask
& USB_SPEED_MASK_SUPER
) &&
1452 request
== 0x8006 && value
== 0x100 && index
== 0) {
1453 r
->usb3ep0quirk
= true;
1456 libusb_fill_control_transfer(r
->xfer
, s
->dh
, r
->buffer
,
1457 usb_host_req_complete_ctrl
, r
,
1459 rc
= libusb_submit_transfer(r
->xfer
);
1461 p
->status
= USB_RET_NODEV
;
1462 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1463 p
->status
, p
->actual_length
);
1464 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1470 p
->status
= USB_RET_ASYNC
;
1473 static void usb_host_handle_data(USBDevice
*udev
, USBPacket
*p
)
1475 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1480 if (usb_host_use_combining(p
->ep
) && p
->state
== USB_PACKET_SETUP
) {
1481 p
->status
= USB_RET_ADD_TO_QUEUE
;
1485 trace_usb_host_req_data(s
->bus_num
, s
->addr
, p
,
1486 p
->pid
== USB_TOKEN_IN
,
1487 p
->ep
->nr
, p
->iov
.size
);
1489 if (s
->dh
== NULL
) {
1490 p
->status
= USB_RET_NODEV
;
1491 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1494 if (p
->ep
->halted
) {
1495 p
->status
= USB_RET_STALL
;
1496 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1500 switch (usb_ep_get_type(udev
, p
->pid
, p
->ep
->nr
)) {
1501 case USB_ENDPOINT_XFER_BULK
:
1502 size
= usb_packet_size(p
);
1503 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, size
);
1505 usb_packet_copy(p
, r
->buffer
, size
);
1507 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1510 libusb_fill_bulk_stream_transfer(r
->xfer
, s
->dh
, ep
, p
->stream
,
1512 usb_host_req_complete_data
, r
,
1515 usb_host_req_free(r
);
1516 p
->status
= USB_RET_STALL
;
1520 libusb_fill_bulk_transfer(r
->xfer
, s
->dh
, ep
,
1522 usb_host_req_complete_data
, r
,
1526 case USB_ENDPOINT_XFER_INT
:
1527 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, p
->iov
.size
);
1529 usb_packet_copy(p
, r
->buffer
, p
->iov
.size
);
1531 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1532 libusb_fill_interrupt_transfer(r
->xfer
, s
->dh
, ep
,
1533 r
->buffer
, p
->iov
.size
,
1534 usb_host_req_complete_data
, r
,
1537 case USB_ENDPOINT_XFER_ISOC
:
1538 if (p
->pid
== USB_TOKEN_IN
) {
1539 usb_host_iso_data_in(s
, p
);
1541 usb_host_iso_data_out(s
, p
);
1543 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1544 p
->status
, p
->actual_length
);
1547 p
->status
= USB_RET_STALL
;
1548 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1549 p
->status
, p
->actual_length
);
1553 rc
= libusb_submit_transfer(r
->xfer
);
1555 p
->status
= USB_RET_NODEV
;
1556 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1557 p
->status
, p
->actual_length
);
1558 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1564 p
->status
= USB_RET_ASYNC
;
1567 static void usb_host_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
1569 if (usb_host_use_combining(ep
)) {
1570 usb_ep_combine_input_packets(ep
);
1574 static void usb_host_handle_reset(USBDevice
*udev
)
1576 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1579 if (!s
->allow_one_guest_reset
&& !s
->allow_all_guest_resets
) {
1582 if (!s
->allow_all_guest_resets
&& udev
->addr
== 0) {
1586 trace_usb_host_reset(s
->bus_num
, s
->addr
);
1588 rc
= libusb_reset_device(s
->dh
);
1594 static int usb_host_alloc_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1595 int nr_eps
, int streams
)
1598 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1599 unsigned char endpoints
[30];
1602 for (i
= 0; i
< nr_eps
; i
++) {
1603 endpoints
[i
] = eps
[i
]->nr
;
1604 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1605 endpoints
[i
] |= 0x80;
1608 rc
= libusb_alloc_streams(s
->dh
, streams
, endpoints
, nr_eps
);
1610 usb_host_libusb_error("libusb_alloc_streams", rc
);
1611 } else if (rc
!= streams
) {
1612 error_report("libusb_alloc_streams: got less streams "
1613 "then requested %d < %d", rc
, streams
);
1616 return (rc
== streams
) ? 0 : -1;
1618 error_report("libusb_alloc_streams: error not implemented");
1623 static void usb_host_free_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1627 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1628 unsigned char endpoints
[30];
1631 for (i
= 0; i
< nr_eps
; i
++) {
1632 endpoints
[i
] = eps
[i
]->nr
;
1633 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1634 endpoints
[i
] |= 0x80;
1637 libusb_free_streams(s
->dh
, endpoints
, nr_eps
);
1642 * This is *NOT* about restoring state. We have absolutely no idea
1643 * what state the host device is in at the moment and whenever it is
1644 * still present in the first place. Attemping to contine where we
1645 * left off is impossible.
1647 * What we are going to do here is emulate a surprise removal of
1648 * the usb device passed through, then kick host scan so the device
1649 * will get re-attached (and re-initialized by the guest) in case it
1652 * As the device removal will change the state of other devices (usb
1653 * host controller, most likely interrupt controller too) we have to
1654 * wait with it until *all* vmstate is loaded. Thus post_load just
1655 * kicks a bottom half which then does the actual work.
1657 static void usb_host_post_load_bh(void *opaque
)
1659 USBHostDevice
*dev
= opaque
;
1660 USBDevice
*udev
= USB_DEVICE(dev
);
1662 if (dev
->dh
!= NULL
) {
1663 usb_host_close(dev
);
1665 if (udev
->attached
) {
1666 usb_device_detach(udev
);
1668 dev
->bh_postld_pending
= false;
1669 usb_host_auto_check(NULL
);
1672 static int usb_host_post_load(void *opaque
, int version_id
)
1674 USBHostDevice
*dev
= opaque
;
1676 if (!dev
->bh_postld
) {
1677 dev
->bh_postld
= qemu_bh_new(usb_host_post_load_bh
, dev
);
1679 qemu_bh_schedule(dev
->bh_postld
);
1680 dev
->bh_postld_pending
= true;
1684 static const VMStateDescription vmstate_usb_host
= {
1687 .minimum_version_id
= 1,
1688 .post_load
= usb_host_post_load
,
1689 .fields
= (VMStateField
[]) {
1690 VMSTATE_USB_DEVICE(parent_obj
, USBHostDevice
),
1691 VMSTATE_END_OF_LIST()
1695 static Property usb_host_dev_properties
[] = {
1696 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1697 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1698 DEFINE_PROP_STRING("hostport", USBHostDevice
, match
.port
),
1699 DEFINE_PROP_UINT32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1700 DEFINE_PROP_UINT32("productid", USBHostDevice
, match
.product_id
, 0),
1701 #if LIBUSB_API_VERSION >= 0x01000107
1702 DEFINE_PROP_STRING("hostdevice", USBHostDevice
, hostdevice
),
1704 DEFINE_PROP_UINT32("isobufs", USBHostDevice
, iso_urb_count
, 4),
1705 DEFINE_PROP_UINT32("isobsize", USBHostDevice
, iso_urb_frames
, 32),
1706 DEFINE_PROP_BOOL("guest-reset", USBHostDevice
,
1707 allow_one_guest_reset
, true),
1708 DEFINE_PROP_BOOL("guest-resets-all", USBHostDevice
,
1709 allow_all_guest_resets
, false),
1710 DEFINE_PROP_UINT32("loglevel", USBHostDevice
, loglevel
,
1711 LIBUSB_LOG_LEVEL_WARNING
),
1712 DEFINE_PROP_BIT("pipeline", USBHostDevice
, options
,
1713 USB_HOST_OPT_PIPELINE
, true),
1714 DEFINE_PROP_BOOL("suppress-remote-wake", USBHostDevice
,
1715 suppress_remote_wake
, true),
1716 DEFINE_PROP_END_OF_LIST(),
1719 static void usb_host_class_initfn(ObjectClass
*klass
, void *data
)
1721 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1722 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1724 uc
->realize
= usb_host_realize
;
1725 uc
->product_desc
= "USB Host Device";
1726 uc
->cancel_packet
= usb_host_cancel_packet
;
1727 uc
->handle_data
= usb_host_handle_data
;
1728 uc
->handle_control
= usb_host_handle_control
;
1729 uc
->handle_reset
= usb_host_handle_reset
;
1730 uc
->unrealize
= usb_host_unrealize
;
1731 uc
->flush_ep_queue
= usb_host_flush_ep_queue
;
1732 uc
->alloc_streams
= usb_host_alloc_streams
;
1733 uc
->free_streams
= usb_host_free_streams
;
1734 dc
->vmsd
= &vmstate_usb_host
;
1735 device_class_set_props(dc
, usb_host_dev_properties
);
1736 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
1739 static TypeInfo usb_host_dev_info
= {
1740 .name
= TYPE_USB_HOST_DEVICE
,
1741 .parent
= TYPE_USB_DEVICE
,
1742 .instance_size
= sizeof(USBHostDevice
),
1743 .class_init
= usb_host_class_initfn
,
1744 .instance_init
= usb_host_instance_init
,
1747 static void usb_host_register_types(void)
1749 type_register_static(&usb_host_dev_info
);
1752 type_init(usb_host_register_types
)
1754 /* ------------------------------------------------------------------------ */
1756 static QEMUTimer
*usb_auto_timer
;
1757 static VMChangeStateEntry
*usb_vmstate
;
1759 static void usb_host_vm_state(void *unused
, int running
, RunState state
)
1762 usb_host_auto_check(unused
);
1766 static void usb_host_auto_check(void *unused
)
1768 struct USBHostDevice
*s
;
1769 struct USBAutoFilter
*f
;
1770 libusb_device
**devs
= NULL
;
1771 struct libusb_device_descriptor ddesc
;
1772 int unconnected
= 0;
1775 if (usb_host_init() != 0) {
1779 if (runstate_is_running()) {
1780 n
= libusb_get_device_list(ctx
, &devs
);
1781 for (i
= 0; i
< n
; i
++) {
1782 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1785 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1788 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1790 if (f
->bus_num
> 0 &&
1791 f
->bus_num
!= libusb_get_bus_number(devs
[i
])) {
1795 f
->addr
!= libusb_get_device_address(devs
[i
])) {
1798 if (f
->port
!= NULL
) {
1799 char port
[16] = "-";
1800 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1801 if (strcmp(f
->port
, port
) != 0) {
1805 if (f
->vendor_id
> 0 &&
1806 f
->vendor_id
!= ddesc
.idVendor
) {
1809 if (f
->product_id
> 0 &&
1810 f
->product_id
!= ddesc
.idProduct
) {
1814 /* We got a match */
1816 if (s
->errcount
>= 3) {
1819 if (s
->dh
!= NULL
) {
1822 if (usb_host_open(s
, devs
[i
], 0) < 0) {
1829 libusb_free_device_list(devs
, 1);
1831 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1832 if (s
->dh
== NULL
) {
1845 if (unconnected
== 0) {
1846 /* nothing to watch */
1847 if (usb_auto_timer
) {
1848 timer_del(usb_auto_timer
);
1849 trace_usb_host_auto_scan_disabled();
1857 usb_vmstate
= qemu_add_vm_change_state_handler(usb_host_vm_state
, NULL
);
1859 if (!usb_auto_timer
) {
1860 usb_auto_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, usb_host_auto_check
, NULL
);
1861 if (!usb_auto_timer
) {
1864 trace_usb_host_auto_scan_enabled();
1866 timer_mod(usb_auto_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 2000);
1870 * Check whether USB host device has a USB mass storage SCSI interface
1872 bool usb_host_dev_is_scsi_storage(USBDevice
*ud
)
1874 USBHostDevice
*uhd
= USB_HOST_DEVICE(ud
);
1875 struct libusb_config_descriptor
*conf
;
1876 const struct libusb_interface_descriptor
*intf
;
1877 bool is_scsi_storage
= false;
1880 if (!uhd
|| libusb_get_active_config_descriptor(uhd
->dev
, &conf
) != 0) {
1884 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
1885 intf
= &conf
->interface
[i
].altsetting
[ud
->altsetting
[i
]];
1886 if (intf
->bInterfaceClass
== LIBUSB_CLASS_MASS_STORAGE
&&
1887 intf
->bInterfaceSubClass
== 6) { /* 6 means SCSI */
1888 is_scsi_storage
= true;
1893 libusb_free_config_descriptor(conf
);
1895 return is_scsi_storage
;
1898 void hmp_info_usbhost(Monitor
*mon
, const QDict
*qdict
)
1900 libusb_device
**devs
= NULL
;
1901 struct libusb_device_descriptor ddesc
;
1905 if (usb_host_init() != 0) {
1909 n
= libusb_get_device_list(ctx
, &devs
);
1910 for (i
= 0; i
< n
; i
++) {
1911 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1914 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1917 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1918 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1919 libusb_get_bus_number(devs
[i
]),
1920 libusb_get_device_address(devs
[i
]),
1922 speed_name
[libusb_get_device_speed(devs
[i
])]);
1923 monitor_printf(mon
, " Class %02x:", ddesc
.bDeviceClass
);
1924 monitor_printf(mon
, " USB device %04x:%04x",
1925 ddesc
.idVendor
, ddesc
.idProduct
);
1926 if (ddesc
.iProduct
) {
1927 libusb_device_handle
*handle
;
1928 if (libusb_open(devs
[i
], &handle
) == 0) {
1929 unsigned char name
[64] = "";
1930 libusb_get_string_descriptor_ascii(handle
,
1932 name
, sizeof(name
));
1933 libusb_close(handle
);
1934 monitor_printf(mon
, ", %s", name
);
1937 monitor_printf(mon
, "\n");
1939 libusb_free_device_list(devs
, 1);