2 * Linux host USB redirector
4 * Copyright (c) 2005 Fabrice Bellard
6 * Copyright (c) 2008 Max Krasnyansky
7 * Support for host device auto connect & disconnect
8 * Major rewrite to support fully async operation
10 * Copyright 2008 TJ <linux@tjworld.net>
11 * Added flexible support for /dev/bus/usb /sys/bus/usb/devices in addition
12 * to the legacy /proc/bus/usb USB device discovery and handling
14 * (c) 2012 Gerd Hoffmann <kraxel@redhat.com>
15 * Completely rewritten to use libusb instead of usbfs ioctls.
17 * Permission is hereby granted, free of charge, to any person obtaining a copy
18 * of this software and associated documentation files (the "Software"), to deal
19 * in the Software without restriction, including without limitation the rights
20 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21 * copies of the Software, and to permit persons to whom the Software is
22 * furnished to do so, subject to the following conditions:
24 * The above copyright notice and this permission notice shall be included in
25 * all copies or substantial portions of the Software.
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
36 #include "qemu/osdep.h"
42 #include "qapi/error.h"
43 #include "migration/vmstate.h"
44 #include "monitor/monitor.h"
45 #include "qemu/error-report.h"
46 #include "qemu/main-loop.h"
47 #include "qemu/module.h"
48 #include "sysemu/runstate.h"
49 #include "sysemu/sysemu.h"
52 #include "hw/qdev-properties.h"
55 /* ------------------------------------------------------------------------ */
57 #define TYPE_USB_HOST_DEVICE "usb-host"
58 #define USB_HOST_DEVICE(obj) \
59 OBJECT_CHECK(USBHostDevice, (obj), TYPE_USB_HOST_DEVICE)
61 typedef struct USBHostDevice USBHostDevice
;
62 typedef struct USBHostRequest USBHostRequest
;
63 typedef struct USBHostIsoXfer USBHostIsoXfer
;
64 typedef struct USBHostIsoRing USBHostIsoRing
;
66 struct USBAutoFilter
{
74 enum USBHostDeviceOptions
{
75 USB_HOST_OPT_PIPELINE
,
78 struct USBHostDevice
{
82 struct USBAutoFilter match
;
84 uint32_t iso_urb_count
;
85 uint32_t iso_urb_frames
;
89 bool allow_one_guest_reset
;
90 bool allow_all_guest_resets
;
91 bool suppress_remote_wake
;
94 QTAILQ_ENTRY(USBHostDevice
) next
;
101 libusb_device_handle
*dh
;
102 struct libusb_device_descriptor ddesc
;
107 } ifs
[USB_MAX_INTERFACES
];
109 /* callbacks & friends */
112 bool bh_postld_pending
;
116 QTAILQ_HEAD(, USBHostRequest
) requests
;
117 QTAILQ_HEAD(, USBHostIsoRing
) isorings
;
120 struct USBHostRequest
{
124 struct libusb_transfer
*xfer
;
125 unsigned char *buffer
;
129 QTAILQ_ENTRY(USBHostRequest
) next
;
132 struct USBHostIsoXfer
{
133 USBHostIsoRing
*ring
;
134 struct libusb_transfer
*xfer
;
137 QTAILQ_ENTRY(USBHostIsoXfer
) next
;
140 struct USBHostIsoRing
{
143 QTAILQ_HEAD(, USBHostIsoXfer
) unused
;
144 QTAILQ_HEAD(, USBHostIsoXfer
) inflight
;
145 QTAILQ_HEAD(, USBHostIsoXfer
) copy
;
146 QTAILQ_ENTRY(USBHostIsoRing
) next
;
149 static QTAILQ_HEAD(, USBHostDevice
) hostdevs
=
150 QTAILQ_HEAD_INITIALIZER(hostdevs
);
152 static void usb_host_auto_check(void *unused
);
153 static void usb_host_release_interfaces(USBHostDevice
*s
);
154 static void usb_host_nodev(USBHostDevice
*s
);
155 static void usb_host_detach_kernel(USBHostDevice
*s
);
156 static void usb_host_attach_kernel(USBHostDevice
*s
);
158 /* ------------------------------------------------------------------------ */
160 #ifndef LIBUSB_LOG_LEVEL_WARNING /* older libusb didn't define these */
161 #define LIBUSB_LOG_LEVEL_WARNING 2
164 /* ------------------------------------------------------------------------ */
166 #define CONTROL_TIMEOUT 10000 /* 10 sec */
167 #define BULK_TIMEOUT 0 /* unlimited */
168 #define INTR_TIMEOUT 0 /* unlimited */
170 #ifndef LIBUSB_API_VERSION
171 # define LIBUSB_API_VERSION LIBUSBX_API_VERSION
173 #if LIBUSB_API_VERSION >= 0x01000103
174 # define HAVE_STREAMS 1
177 static const char *speed_name
[] = {
178 [LIBUSB_SPEED_UNKNOWN
] = "?",
179 [LIBUSB_SPEED_LOW
] = "1.5",
180 [LIBUSB_SPEED_FULL
] = "12",
181 [LIBUSB_SPEED_HIGH
] = "480",
182 [LIBUSB_SPEED_SUPER
] = "5000",
185 static const unsigned int speed_map
[] = {
186 [LIBUSB_SPEED_LOW
] = USB_SPEED_LOW
,
187 [LIBUSB_SPEED_FULL
] = USB_SPEED_FULL
,
188 [LIBUSB_SPEED_HIGH
] = USB_SPEED_HIGH
,
189 [LIBUSB_SPEED_SUPER
] = USB_SPEED_SUPER
,
192 static const unsigned int status_map
[] = {
193 [LIBUSB_TRANSFER_COMPLETED
] = USB_RET_SUCCESS
,
194 [LIBUSB_TRANSFER_ERROR
] = USB_RET_IOERROR
,
195 [LIBUSB_TRANSFER_TIMED_OUT
] = USB_RET_IOERROR
,
196 [LIBUSB_TRANSFER_CANCELLED
] = USB_RET_IOERROR
,
197 [LIBUSB_TRANSFER_STALL
] = USB_RET_STALL
,
198 [LIBUSB_TRANSFER_NO_DEVICE
] = USB_RET_NODEV
,
199 [LIBUSB_TRANSFER_OVERFLOW
] = USB_RET_BABBLE
,
202 static const char *err_names
[] = {
203 [-LIBUSB_ERROR_IO
] = "IO",
204 [-LIBUSB_ERROR_INVALID_PARAM
] = "INVALID_PARAM",
205 [-LIBUSB_ERROR_ACCESS
] = "ACCESS",
206 [-LIBUSB_ERROR_NO_DEVICE
] = "NO_DEVICE",
207 [-LIBUSB_ERROR_NOT_FOUND
] = "NOT_FOUND",
208 [-LIBUSB_ERROR_BUSY
] = "BUSY",
209 [-LIBUSB_ERROR_TIMEOUT
] = "TIMEOUT",
210 [-LIBUSB_ERROR_OVERFLOW
] = "OVERFLOW",
211 [-LIBUSB_ERROR_PIPE
] = "PIPE",
212 [-LIBUSB_ERROR_INTERRUPTED
] = "INTERRUPTED",
213 [-LIBUSB_ERROR_NO_MEM
] = "NO_MEM",
214 [-LIBUSB_ERROR_NOT_SUPPORTED
] = "NOT_SUPPORTED",
215 [-LIBUSB_ERROR_OTHER
] = "OTHER",
218 static libusb_context
*ctx
;
219 static uint32_t loglevel
;
223 static void usb_host_handle_fd(void *opaque
)
225 struct timeval tv
= { 0, 0 };
226 libusb_handle_events_timeout(ctx
, &tv
);
229 static void usb_host_add_fd(int fd
, short events
, void *user_data
)
231 qemu_set_fd_handler(fd
,
232 (events
& POLLIN
) ? usb_host_handle_fd
: NULL
,
233 (events
& POLLOUT
) ? usb_host_handle_fd
: NULL
,
237 static void usb_host_del_fd(int fd
, void *user_data
)
239 qemu_set_fd_handler(fd
, NULL
, NULL
, NULL
);
242 #endif /* !CONFIG_WIN32 */
244 static int usb_host_init(void)
247 const struct libusb_pollfd
**poll
;
254 rc
= libusb_init(&ctx
);
258 #if LIBUSB_API_VERSION >= 0x01000106
259 libusb_set_option(ctx
, LIBUSB_OPTION_LOG_LEVEL
, loglevel
);
261 libusb_set_debug(ctx
, loglevel
);
264 /* FIXME: add support for Windows. */
266 libusb_set_pollfd_notifiers(ctx
, usb_host_add_fd
,
269 poll
= libusb_get_pollfds(ctx
);
272 for (i
= 0; poll
[i
] != NULL
; i
++) {
273 usb_host_add_fd(poll
[i
]->fd
, poll
[i
]->events
, ctx
);
281 static int usb_host_get_port(libusb_device
*dev
, char *port
, size_t len
)
287 #if LIBUSB_API_VERSION >= 0x01000102
288 rc
= libusb_get_port_numbers(dev
, path
, 7);
290 rc
= libusb_get_port_path(ctx
, dev
, path
, 7);
295 off
= snprintf(port
, len
, "%d", path
[0]);
296 for (i
= 1; i
< rc
; i
++) {
297 off
+= snprintf(port
+off
, len
-off
, ".%d", path
[i
]);
302 static void usb_host_libusb_error(const char *func
, int rc
)
310 if (-rc
< ARRAY_SIZE(err_names
) && err_names
[-rc
]) {
311 errname
= err_names
[-rc
];
315 error_report("%s: %d [%s]", func
, rc
, errname
);
318 /* ------------------------------------------------------------------------ */
320 static bool usb_host_use_combining(USBEndpoint
*ep
)
327 if (ep
->pid
!= USB_TOKEN_IN
) {
330 type
= usb_ep_get_type(ep
->dev
, ep
->pid
, ep
->nr
);
331 if (type
!= USB_ENDPOINT_XFER_BULK
) {
337 /* ------------------------------------------------------------------------ */
339 static USBHostRequest
*usb_host_req_alloc(USBHostDevice
*s
, USBPacket
*p
,
340 bool in
, size_t bufsize
)
342 USBHostRequest
*r
= g_new0(USBHostRequest
, 1);
347 r
->xfer
= libusb_alloc_transfer(0);
349 r
->buffer
= g_malloc(bufsize
);
351 QTAILQ_INSERT_TAIL(&s
->requests
, r
, next
);
355 static void usb_host_req_free(USBHostRequest
*r
)
357 QTAILQ_REMOVE(&r
->host
->requests
, r
, next
);
358 libusb_free_transfer(r
->xfer
);
363 static USBHostRequest
*usb_host_req_find(USBHostDevice
*s
, USBPacket
*p
)
367 QTAILQ_FOREACH(r
, &s
->requests
, next
) {
375 static void LIBUSB_CALL
usb_host_req_complete_ctrl(struct libusb_transfer
*xfer
)
377 USBHostRequest
*r
= xfer
->user_data
;
378 USBHostDevice
*s
= r
->host
;
379 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
382 goto out
; /* request was canceled */
385 r
->p
->status
= status_map
[xfer
->status
];
386 r
->p
->actual_length
= xfer
->actual_length
;
387 if (r
->in
&& xfer
->actual_length
) {
388 USBDevice
*udev
= USB_DEVICE(s
);
389 struct libusb_config_descriptor
*conf
= (void *)r
->cbuf
;
390 memcpy(r
->cbuf
, r
->buffer
+ 8, xfer
->actual_length
);
392 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
393 * to work redirected to a not superspeed capable hcd */
394 if (r
->usb3ep0quirk
&& xfer
->actual_length
>= 18 &&
399 *If this is GET_DESCRIPTOR request for configuration descriptor,
400 * remove 'remote wakeup' flag from it to prevent idle power down
403 if (s
->suppress_remote_wake
&&
404 udev
->setup_buf
[0] == USB_DIR_IN
&&
405 udev
->setup_buf
[1] == USB_REQ_GET_DESCRIPTOR
&&
406 udev
->setup_buf
[3] == USB_DT_CONFIG
&& udev
->setup_buf
[2] == 0 &&
407 xfer
->actual_length
>
408 offsetof(struct libusb_config_descriptor
, bmAttributes
) &&
409 (conf
->bmAttributes
& USB_CFG_ATT_WAKEUP
)) {
410 trace_usb_host_remote_wakeup_removed(s
->bus_num
, s
->addr
);
411 conf
->bmAttributes
&= ~USB_CFG_ATT_WAKEUP
;
414 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
415 r
->p
->status
, r
->p
->actual_length
);
416 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
419 usb_host_req_free(r
);
425 static void LIBUSB_CALL
usb_host_req_complete_data(struct libusb_transfer
*xfer
)
427 USBHostRequest
*r
= xfer
->user_data
;
428 USBHostDevice
*s
= r
->host
;
429 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
432 goto out
; /* request was canceled */
435 r
->p
->status
= status_map
[xfer
->status
];
436 if (r
->in
&& xfer
->actual_length
) {
437 usb_packet_copy(r
->p
, r
->buffer
, xfer
->actual_length
);
439 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
440 r
->p
->status
, r
->p
->actual_length
);
441 if (usb_host_use_combining(r
->p
->ep
)) {
442 usb_combined_input_packet_complete(USB_DEVICE(s
), r
->p
);
444 usb_packet_complete(USB_DEVICE(s
), r
->p
);
448 usb_host_req_free(r
);
454 static void usb_host_req_abort(USBHostRequest
*r
)
456 USBHostDevice
*s
= r
->host
;
457 bool inflight
= (r
->p
&& r
->p
->state
== USB_PACKET_ASYNC
);
460 r
->p
->status
= USB_RET_NODEV
;
461 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
462 r
->p
->status
, r
->p
->actual_length
);
463 if (r
->p
->ep
->nr
== 0) {
464 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
466 usb_packet_complete(USB_DEVICE(s
), r
->p
);
470 libusb_cancel_transfer(r
->xfer
);
474 /* ------------------------------------------------------------------------ */
476 static void LIBUSB_CALL
477 usb_host_req_complete_iso(struct libusb_transfer
*transfer
)
479 USBHostIsoXfer
*xfer
= transfer
->user_data
;
482 /* USBHostIsoXfer released while inflight */
483 g_free(transfer
->buffer
);
484 libusb_free_transfer(transfer
);
488 QTAILQ_REMOVE(&xfer
->ring
->inflight
, xfer
, next
);
489 if (QTAILQ_EMPTY(&xfer
->ring
->inflight
)) {
490 USBHostDevice
*s
= xfer
->ring
->host
;
491 trace_usb_host_iso_stop(s
->bus_num
, s
->addr
, xfer
->ring
->ep
->nr
);
493 if (xfer
->ring
->ep
->pid
== USB_TOKEN_IN
) {
494 QTAILQ_INSERT_TAIL(&xfer
->ring
->copy
, xfer
, next
);
495 usb_wakeup(xfer
->ring
->ep
, 0);
497 QTAILQ_INSERT_TAIL(&xfer
->ring
->unused
, xfer
, next
);
501 static USBHostIsoRing
*usb_host_iso_alloc(USBHostDevice
*s
, USBEndpoint
*ep
)
503 USBHostIsoRing
*ring
= g_new0(USBHostIsoRing
, 1);
504 USBHostIsoXfer
*xfer
;
505 /* FIXME: check interval (for now assume one xfer per frame) */
506 int packets
= s
->iso_urb_frames
;
511 QTAILQ_INIT(&ring
->unused
);
512 QTAILQ_INIT(&ring
->inflight
);
513 QTAILQ_INIT(&ring
->copy
);
514 QTAILQ_INSERT_TAIL(&s
->isorings
, ring
, next
);
516 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
517 xfer
= g_new0(USBHostIsoXfer
, 1);
519 xfer
->xfer
= libusb_alloc_transfer(packets
);
520 xfer
->xfer
->dev_handle
= s
->dh
;
521 xfer
->xfer
->type
= LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
;
523 xfer
->xfer
->endpoint
= ring
->ep
->nr
;
524 if (ring
->ep
->pid
== USB_TOKEN_IN
) {
525 xfer
->xfer
->endpoint
|= USB_DIR_IN
;
527 xfer
->xfer
->callback
= usb_host_req_complete_iso
;
528 xfer
->xfer
->user_data
= xfer
;
530 xfer
->xfer
->num_iso_packets
= packets
;
531 xfer
->xfer
->length
= ring
->ep
->max_packet_size
* packets
;
532 xfer
->xfer
->buffer
= g_malloc0(xfer
->xfer
->length
);
534 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
540 static USBHostIsoRing
*usb_host_iso_find(USBHostDevice
*s
, USBEndpoint
*ep
)
542 USBHostIsoRing
*ring
;
544 QTAILQ_FOREACH(ring
, &s
->isorings
, next
) {
545 if (ring
->ep
== ep
) {
552 static void usb_host_iso_reset_xfer(USBHostIsoXfer
*xfer
)
554 libusb_set_iso_packet_lengths(xfer
->xfer
,
555 xfer
->ring
->ep
->max_packet_size
);
557 xfer
->copy_complete
= false;
560 static void usb_host_iso_free_xfer(USBHostIsoXfer
*xfer
, bool inflight
)
563 xfer
->xfer
->user_data
= NULL
;
565 g_free(xfer
->xfer
->buffer
);
566 libusb_free_transfer(xfer
->xfer
);
571 static void usb_host_iso_free(USBHostIsoRing
*ring
)
573 USBHostIsoXfer
*xfer
;
575 while ((xfer
= QTAILQ_FIRST(&ring
->inflight
)) != NULL
) {
576 QTAILQ_REMOVE(&ring
->inflight
, xfer
, next
);
577 usb_host_iso_free_xfer(xfer
, true);
579 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
580 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
581 usb_host_iso_free_xfer(xfer
, false);
583 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
) {
584 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
585 usb_host_iso_free_xfer(xfer
, false);
588 QTAILQ_REMOVE(&ring
->host
->isorings
, ring
, next
);
592 static void usb_host_iso_free_all(USBHostDevice
*s
)
594 USBHostIsoRing
*ring
;
596 while ((ring
= QTAILQ_FIRST(&s
->isorings
)) != NULL
) {
597 usb_host_iso_free(ring
);
601 static bool usb_host_iso_data_copy(USBHostIsoXfer
*xfer
, USBPacket
*p
)
606 buf
= libusb_get_iso_packet_buffer_simple(xfer
->xfer
, xfer
->packet
);
607 if (p
->pid
== USB_TOKEN_OUT
) {
609 if (psize
> xfer
->ring
->ep
->max_packet_size
) {
610 /* should not happen (guest bug) */
611 psize
= xfer
->ring
->ep
->max_packet_size
;
613 xfer
->xfer
->iso_packet_desc
[xfer
->packet
].length
= psize
;
615 psize
= xfer
->xfer
->iso_packet_desc
[xfer
->packet
].actual_length
;
616 if (psize
> p
->iov
.size
) {
617 /* should not happen (guest bug) */
621 usb_packet_copy(p
, buf
, psize
);
623 xfer
->copy_complete
= (xfer
->packet
== xfer
->xfer
->num_iso_packets
);
624 return xfer
->copy_complete
;
627 static void usb_host_iso_data_in(USBHostDevice
*s
, USBPacket
*p
)
629 USBHostIsoRing
*ring
;
630 USBHostIsoXfer
*xfer
;
631 bool disconnect
= false;
634 ring
= usb_host_iso_find(s
, p
->ep
);
636 ring
= usb_host_iso_alloc(s
, p
->ep
);
639 /* copy data to guest */
640 xfer
= QTAILQ_FIRST(&ring
->copy
);
642 if (usb_host_iso_data_copy(xfer
, p
)) {
643 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
644 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
648 /* submit empty bufs to host */
649 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
650 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
651 usb_host_iso_reset_xfer(xfer
);
652 rc
= libusb_submit_transfer(xfer
->xfer
);
654 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
655 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
656 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
661 if (QTAILQ_EMPTY(&ring
->inflight
)) {
662 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
664 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
672 static void usb_host_iso_data_out(USBHostDevice
*s
, USBPacket
*p
)
674 USBHostIsoRing
*ring
;
675 USBHostIsoXfer
*xfer
;
676 bool disconnect
= false;
679 ring
= usb_host_iso_find(s
, p
->ep
);
681 ring
= usb_host_iso_alloc(s
, p
->ep
);
684 /* copy data from guest */
685 xfer
= QTAILQ_FIRST(&ring
->copy
);
686 while (xfer
!= NULL
&& xfer
->copy_complete
) {
688 xfer
= QTAILQ_NEXT(xfer
, next
);
691 xfer
= QTAILQ_FIRST(&ring
->unused
);
693 trace_usb_host_iso_out_of_bufs(s
->bus_num
, s
->addr
, p
->ep
->nr
);
696 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
697 usb_host_iso_reset_xfer(xfer
);
698 QTAILQ_INSERT_TAIL(&ring
->copy
, xfer
, next
);
700 usb_host_iso_data_copy(xfer
, p
);
702 if (QTAILQ_EMPTY(&ring
->inflight
)) {
703 /* wait until half of our buffers are filled
704 before kicking the iso out stream */
705 if (filled
*2 < s
->iso_urb_count
) {
710 /* submit filled bufs to host */
711 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
&&
712 xfer
->copy_complete
) {
713 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
714 rc
= libusb_submit_transfer(xfer
->xfer
);
716 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
717 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
718 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
723 if (QTAILQ_EMPTY(&ring
->inflight
)) {
724 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
726 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
734 /* ------------------------------------------------------------------------ */
736 static void usb_host_speed_compat(USBHostDevice
*s
)
738 USBDevice
*udev
= USB_DEVICE(s
);
739 struct libusb_config_descriptor
*conf
;
740 const struct libusb_interface_descriptor
*intf
;
741 const struct libusb_endpoint_descriptor
*endp
;
743 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
745 bool compat_high
= true;
746 bool compat_full
= true;
751 rc
= libusb_get_config_descriptor(s
->dev
, c
, &conf
);
755 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
756 for (a
= 0; a
< conf
->interface
[i
].num_altsetting
; a
++) {
757 intf
= &conf
->interface
[i
].altsetting
[a
];
758 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
759 endp
= &intf
->endpoint
[e
];
760 type
= endp
->bmAttributes
& 0x3;
766 case 0x02: /* BULK */
768 rc
= libusb_get_ss_endpoint_companion_descriptor
769 (ctx
, endp
, &endp_ss_comp
);
770 if (rc
== LIBUSB_SUCCESS
) {
771 int streams
= endp_ss_comp
->bmAttributes
& 0x1f;
776 libusb_free_ss_endpoint_companion_descriptor
781 case 0x03: /* INTERRUPT */
782 if (endp
->wMaxPacketSize
> 64) {
785 if (endp
->wMaxPacketSize
> 1024) {
793 libusb_free_config_descriptor(conf
);
796 udev
->speedmask
= (1 << udev
->speed
);
797 if (udev
->speed
== USB_SPEED_SUPER
&& compat_high
) {
798 udev
->speedmask
|= USB_SPEED_MASK_HIGH
;
800 if (udev
->speed
== USB_SPEED_SUPER
&& compat_full
) {
801 udev
->speedmask
|= USB_SPEED_MASK_FULL
;
803 if (udev
->speed
== USB_SPEED_HIGH
&& compat_full
) {
804 udev
->speedmask
|= USB_SPEED_MASK_FULL
;
808 static void usb_host_ep_update(USBHostDevice
*s
)
810 static const char *tname
[] = {
811 [USB_ENDPOINT_XFER_CONTROL
] = "control",
812 [USB_ENDPOINT_XFER_ISOC
] = "isoc",
813 [USB_ENDPOINT_XFER_BULK
] = "bulk",
814 [USB_ENDPOINT_XFER_INT
] = "int",
816 USBDevice
*udev
= USB_DEVICE(s
);
817 struct libusb_config_descriptor
*conf
;
818 const struct libusb_interface_descriptor
*intf
;
819 const struct libusb_endpoint_descriptor
*endp
;
821 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
828 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
832 trace_usb_host_parse_config(s
->bus_num
, s
->addr
,
833 conf
->bConfigurationValue
, true);
835 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
836 assert(udev
->altsetting
[i
] < conf
->interface
[i
].num_altsetting
);
837 intf
= &conf
->interface
[i
].altsetting
[udev
->altsetting
[i
]];
838 trace_usb_host_parse_interface(s
->bus_num
, s
->addr
,
839 intf
->bInterfaceNumber
,
840 intf
->bAlternateSetting
, true);
841 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
842 endp
= &intf
->endpoint
[e
];
844 devep
= endp
->bEndpointAddress
;
845 pid
= (devep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
847 type
= endp
->bmAttributes
& 0x3;
850 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
851 "invalid endpoint address");
854 if (usb_ep_get_type(udev
, pid
, ep
) != USB_ENDPOINT_XFER_INVALID
) {
855 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
856 "duplicate endpoint address");
860 trace_usb_host_parse_endpoint(s
->bus_num
, s
->addr
, ep
,
861 (devep
& USB_DIR_IN
) ? "in" : "out",
863 usb_ep_set_max_packet_size(udev
, pid
, ep
,
864 endp
->wMaxPacketSize
);
865 usb_ep_set_type(udev
, pid
, ep
, type
);
866 usb_ep_set_ifnum(udev
, pid
, ep
, i
);
867 usb_ep_set_halted(udev
, pid
, ep
, 0);
869 if (type
== LIBUSB_TRANSFER_TYPE_BULK
&&
870 libusb_get_ss_endpoint_companion_descriptor(ctx
, endp
,
871 &endp_ss_comp
) == LIBUSB_SUCCESS
) {
872 usb_ep_set_max_streams(udev
, pid
, ep
,
873 endp_ss_comp
->bmAttributes
);
874 libusb_free_ss_endpoint_companion_descriptor(endp_ss_comp
);
880 libusb_free_config_descriptor(conf
);
883 static int usb_host_open(USBHostDevice
*s
, libusb_device
*dev
)
885 USBDevice
*udev
= USB_DEVICE(s
);
886 int bus_num
= libusb_get_bus_number(dev
);
887 int addr
= libusb_get_device_address(dev
);
889 Error
*local_err
= NULL
;
891 if (s
->bh_postld_pending
) {
895 trace_usb_host_open_started(bus_num
, addr
);
900 rc
= libusb_open(dev
, &s
->dh
);
906 s
->bus_num
= bus_num
;
909 usb_host_detach_kernel(s
);
911 libusb_get_device_descriptor(dev
, &s
->ddesc
);
912 usb_host_get_port(s
->dev
, s
->port
, sizeof(s
->port
));
915 usb_host_ep_update(s
);
917 udev
->speed
= speed_map
[libusb_get_device_speed(dev
)];
918 usb_host_speed_compat(s
);
920 if (s
->ddesc
.iProduct
) {
921 libusb_get_string_descriptor_ascii(s
->dh
, s
->ddesc
.iProduct
,
922 (unsigned char *)udev
->product_desc
,
923 sizeof(udev
->product_desc
));
925 snprintf(udev
->product_desc
, sizeof(udev
->product_desc
),
926 "host:%d.%d", bus_num
, addr
);
929 usb_device_attach(udev
, &local_err
);
931 error_report_err(local_err
);
935 trace_usb_host_open_success(bus_num
, addr
);
939 trace_usb_host_open_failure(bus_num
, addr
);
941 usb_host_release_interfaces(s
);
942 libusb_reset_device(s
->dh
);
943 usb_host_attach_kernel(s
);
951 static void usb_host_abort_xfers(USBHostDevice
*s
)
953 USBHostRequest
*r
, *rtmp
;
955 QTAILQ_FOREACH_SAFE(r
, &s
->requests
, next
, rtmp
) {
956 usb_host_req_abort(r
);
959 while (QTAILQ_FIRST(&s
->requests
) != NULL
) {
961 memset(&tv
, 0, sizeof(tv
));
963 libusb_handle_events_timeout(ctx
, &tv
);
967 static int usb_host_close(USBHostDevice
*s
)
969 USBDevice
*udev
= USB_DEVICE(s
);
975 trace_usb_host_close(s
->bus_num
, s
->addr
);
977 usb_host_abort_xfers(s
);
978 usb_host_iso_free_all(s
);
980 if (udev
->attached
) {
981 usb_device_detach(udev
);
984 usb_host_release_interfaces(s
);
985 libusb_reset_device(s
->dh
);
986 usb_host_attach_kernel(s
);
991 usb_host_auto_check(NULL
);
995 static void usb_host_nodev_bh(void *opaque
)
997 USBHostDevice
*s
= opaque
;
1001 static void usb_host_nodev(USBHostDevice
*s
)
1004 s
->bh_nodev
= qemu_bh_new(usb_host_nodev_bh
, s
);
1006 qemu_bh_schedule(s
->bh_nodev
);
1009 static void usb_host_exit_notifier(struct Notifier
*n
, void *data
)
1011 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
1014 usb_host_abort_xfers(s
);
1015 usb_host_release_interfaces(s
);
1016 libusb_reset_device(s
->dh
);
1017 usb_host_attach_kernel(s
);
1018 libusb_close(s
->dh
);
1022 static libusb_device
*usb_host_find_ref(int bus
, int addr
)
1024 libusb_device
**devs
= NULL
;
1025 libusb_device
*ret
= NULL
;
1028 if (usb_host_init() != 0) {
1031 n
= libusb_get_device_list(ctx
, &devs
);
1032 for (i
= 0; i
< n
; i
++) {
1033 if (libusb_get_bus_number(devs
[i
]) == bus
&&
1034 libusb_get_device_address(devs
[i
]) == addr
) {
1035 ret
= libusb_ref_device(devs
[i
]);
1039 libusb_free_device_list(devs
, 1);
1043 static void usb_host_realize(USBDevice
*udev
, Error
**errp
)
1045 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1046 libusb_device
*ldev
;
1049 if (s
->match
.vendor_id
> 0xffff) {
1050 error_setg(errp
, "vendorid out of range");
1053 if (s
->match
.product_id
> 0xffff) {
1054 error_setg(errp
, "productid out of range");
1057 if (s
->match
.addr
> 127) {
1058 error_setg(errp
, "hostaddr out of range");
1062 loglevel
= s
->loglevel
;
1063 udev
->flags
|= (1 << USB_DEV_FLAG_IS_HOST
);
1064 udev
->auto_attach
= 0;
1065 QTAILQ_INIT(&s
->requests
);
1066 QTAILQ_INIT(&s
->isorings
);
1068 if (s
->match
.addr
&& s
->match
.bus_num
&&
1069 !s
->match
.vendor_id
&&
1070 !s
->match
.product_id
&&
1072 s
->needs_autoscan
= false;
1073 ldev
= usb_host_find_ref(s
->match
.bus_num
,
1076 error_setg(errp
, "failed to find host usb device %d:%d",
1077 s
->match
.bus_num
, s
->match
.addr
);
1080 rc
= usb_host_open(s
, ldev
);
1081 libusb_unref_device(ldev
);
1083 error_setg(errp
, "failed to open host usb device %d:%d",
1084 s
->match
.bus_num
, s
->match
.addr
);
1088 s
->needs_autoscan
= true;
1089 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
1090 usb_host_auto_check(NULL
);
1093 s
->exit
.notify
= usb_host_exit_notifier
;
1094 qemu_add_exit_notifier(&s
->exit
);
1097 static void usb_host_instance_init(Object
*obj
)
1099 USBDevice
*udev
= USB_DEVICE(obj
);
1100 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1102 device_add_bootindex_property(obj
, &s
->bootindex
,
1107 static void usb_host_unrealize(USBDevice
*udev
, Error
**errp
)
1109 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1111 qemu_remove_exit_notifier(&s
->exit
);
1112 if (s
->needs_autoscan
) {
1113 QTAILQ_REMOVE(&hostdevs
, s
, next
);
1118 static void usb_host_cancel_packet(USBDevice
*udev
, USBPacket
*p
)
1120 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1124 usb_combined_packet_cancel(udev
, p
);
1128 trace_usb_host_req_canceled(s
->bus_num
, s
->addr
, p
);
1130 r
= usb_host_req_find(s
, p
);
1132 r
->p
= NULL
; /* mark as dead */
1133 libusb_cancel_transfer(r
->xfer
);
1137 static void usb_host_detach_kernel(USBHostDevice
*s
)
1139 struct libusb_config_descriptor
*conf
;
1142 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1146 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1147 rc
= libusb_kernel_driver_active(s
->dh
, i
);
1148 usb_host_libusb_error("libusb_kernel_driver_active", rc
);
1151 s
->ifs
[i
].detached
= true;
1155 trace_usb_host_detach_kernel(s
->bus_num
, s
->addr
, i
);
1156 rc
= libusb_detach_kernel_driver(s
->dh
, i
);
1157 usb_host_libusb_error("libusb_detach_kernel_driver", rc
);
1158 s
->ifs
[i
].detached
= true;
1160 libusb_free_config_descriptor(conf
);
1163 static void usb_host_attach_kernel(USBHostDevice
*s
)
1165 struct libusb_config_descriptor
*conf
;
1168 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1172 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1173 if (!s
->ifs
[i
].detached
) {
1176 trace_usb_host_attach_kernel(s
->bus_num
, s
->addr
, i
);
1177 libusb_attach_kernel_driver(s
->dh
, i
);
1178 s
->ifs
[i
].detached
= false;
1180 libusb_free_config_descriptor(conf
);
1183 static int usb_host_claim_interfaces(USBHostDevice
*s
, int configuration
)
1185 USBDevice
*udev
= USB_DEVICE(s
);
1186 struct libusb_config_descriptor
*conf
;
1189 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1190 udev
->altsetting
[i
] = 0;
1192 udev
->ninterfaces
= 0;
1193 udev
->configuration
= 0;
1195 usb_host_detach_kernel(s
);
1197 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1199 if (rc
== LIBUSB_ERROR_NOT_FOUND
) {
1200 /* address state - ignore */
1201 return USB_RET_SUCCESS
;
1203 return USB_RET_STALL
;
1207 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1208 trace_usb_host_claim_interface(s
->bus_num
, s
->addr
, configuration
, i
);
1209 rc
= libusb_claim_interface(s
->dh
, i
);
1211 s
->ifs
[i
].claimed
= true;
1212 if (++claimed
== conf
->bNumInterfaces
) {
1217 if (claimed
!= conf
->bNumInterfaces
) {
1218 return USB_RET_STALL
;
1221 udev
->ninterfaces
= conf
->bNumInterfaces
;
1222 udev
->configuration
= configuration
;
1224 libusb_free_config_descriptor(conf
);
1225 return USB_RET_SUCCESS
;
1228 static void usb_host_release_interfaces(USBHostDevice
*s
)
1232 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1233 if (!s
->ifs
[i
].claimed
) {
1236 trace_usb_host_release_interface(s
->bus_num
, s
->addr
, i
);
1237 rc
= libusb_release_interface(s
->dh
, i
);
1238 usb_host_libusb_error("libusb_release_interface", rc
);
1239 s
->ifs
[i
].claimed
= false;
1243 static void usb_host_set_address(USBHostDevice
*s
, int addr
)
1245 USBDevice
*udev
= USB_DEVICE(s
);
1247 trace_usb_host_set_address(s
->bus_num
, s
->addr
, addr
);
1251 static void usb_host_set_config(USBHostDevice
*s
, int config
, USBPacket
*p
)
1255 trace_usb_host_set_config(s
->bus_num
, s
->addr
, config
);
1257 usb_host_release_interfaces(s
);
1258 if (s
->ddesc
.bNumConfigurations
!= 1) {
1259 rc
= libusb_set_configuration(s
->dh
, config
);
1261 usb_host_libusb_error("libusb_set_configuration", rc
);
1262 p
->status
= USB_RET_STALL
;
1263 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1269 p
->status
= usb_host_claim_interfaces(s
, config
);
1270 if (p
->status
!= USB_RET_SUCCESS
) {
1273 usb_host_ep_update(s
);
1276 static void usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
,
1279 USBDevice
*udev
= USB_DEVICE(s
);
1282 trace_usb_host_set_interface(s
->bus_num
, s
->addr
, iface
, alt
);
1284 usb_host_iso_free_all(s
);
1286 if (iface
>= USB_MAX_INTERFACES
) {
1287 p
->status
= USB_RET_STALL
;
1291 rc
= libusb_set_interface_alt_setting(s
->dh
, iface
, alt
);
1293 usb_host_libusb_error("libusb_set_interface_alt_setting", rc
);
1294 p
->status
= USB_RET_STALL
;
1295 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1301 udev
->altsetting
[iface
] = alt
;
1302 usb_host_ep_update(s
);
1305 static void usb_host_handle_control(USBDevice
*udev
, USBPacket
*p
,
1306 int request
, int value
, int index
,
1307 int length
, uint8_t *data
)
1309 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1313 trace_usb_host_req_control(s
->bus_num
, s
->addr
, p
, request
, value
, index
);
1315 if (s
->dh
== NULL
) {
1316 p
->status
= USB_RET_NODEV
;
1317 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1322 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1323 usb_host_set_address(s
, value
);
1324 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1327 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1328 usb_host_set_config(s
, value
& 0xff, p
);
1329 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1332 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1333 usb_host_set_interface(s
, index
, value
, p
);
1334 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1337 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
1338 if (value
== 0) { /* clear halt */
1339 int pid
= (index
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1340 libusb_clear_halt(s
->dh
, index
);
1341 usb_ep_set_halted(udev
, pid
, index
& 0x0f, 0);
1342 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1347 r
= usb_host_req_alloc(s
, p
, (request
>> 8) & USB_DIR_IN
, length
+ 8);
1350 memcpy(r
->buffer
, udev
->setup_buf
, 8);
1352 memcpy(r
->buffer
+ 8, r
->cbuf
, r
->clen
);
1355 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
1356 * to work redirected to a not superspeed capable hcd */
1357 if ((udev
->speedmask
& USB_SPEED_MASK_SUPER
) &&
1358 !(udev
->port
->speedmask
& USB_SPEED_MASK_SUPER
) &&
1359 request
== 0x8006 && value
== 0x100 && index
== 0) {
1360 r
->usb3ep0quirk
= true;
1363 libusb_fill_control_transfer(r
->xfer
, s
->dh
, r
->buffer
,
1364 usb_host_req_complete_ctrl
, r
,
1366 rc
= libusb_submit_transfer(r
->xfer
);
1368 p
->status
= USB_RET_NODEV
;
1369 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1370 p
->status
, p
->actual_length
);
1371 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1377 p
->status
= USB_RET_ASYNC
;
1380 static void usb_host_handle_data(USBDevice
*udev
, USBPacket
*p
)
1382 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1387 if (usb_host_use_combining(p
->ep
) && p
->state
== USB_PACKET_SETUP
) {
1388 p
->status
= USB_RET_ADD_TO_QUEUE
;
1392 trace_usb_host_req_data(s
->bus_num
, s
->addr
, p
,
1393 p
->pid
== USB_TOKEN_IN
,
1394 p
->ep
->nr
, p
->iov
.size
);
1396 if (s
->dh
== NULL
) {
1397 p
->status
= USB_RET_NODEV
;
1398 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1401 if (p
->ep
->halted
) {
1402 p
->status
= USB_RET_STALL
;
1403 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1407 switch (usb_ep_get_type(udev
, p
->pid
, p
->ep
->nr
)) {
1408 case USB_ENDPOINT_XFER_BULK
:
1409 size
= usb_packet_size(p
);
1410 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, size
);
1412 usb_packet_copy(p
, r
->buffer
, size
);
1414 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1417 libusb_fill_bulk_stream_transfer(r
->xfer
, s
->dh
, ep
, p
->stream
,
1419 usb_host_req_complete_data
, r
,
1422 usb_host_req_free(r
);
1423 p
->status
= USB_RET_STALL
;
1427 libusb_fill_bulk_transfer(r
->xfer
, s
->dh
, ep
,
1429 usb_host_req_complete_data
, r
,
1433 case USB_ENDPOINT_XFER_INT
:
1434 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, p
->iov
.size
);
1436 usb_packet_copy(p
, r
->buffer
, p
->iov
.size
);
1438 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1439 libusb_fill_interrupt_transfer(r
->xfer
, s
->dh
, ep
,
1440 r
->buffer
, p
->iov
.size
,
1441 usb_host_req_complete_data
, r
,
1444 case USB_ENDPOINT_XFER_ISOC
:
1445 if (p
->pid
== USB_TOKEN_IN
) {
1446 usb_host_iso_data_in(s
, p
);
1448 usb_host_iso_data_out(s
, p
);
1450 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1451 p
->status
, p
->actual_length
);
1454 p
->status
= USB_RET_STALL
;
1455 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1456 p
->status
, p
->actual_length
);
1460 rc
= libusb_submit_transfer(r
->xfer
);
1462 p
->status
= USB_RET_NODEV
;
1463 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1464 p
->status
, p
->actual_length
);
1465 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1471 p
->status
= USB_RET_ASYNC
;
1474 static void usb_host_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
1476 if (usb_host_use_combining(ep
)) {
1477 usb_ep_combine_input_packets(ep
);
1481 static void usb_host_handle_reset(USBDevice
*udev
)
1483 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1486 if (!s
->allow_one_guest_reset
&& !s
->allow_all_guest_resets
) {
1489 if (!s
->allow_all_guest_resets
&& udev
->addr
== 0) {
1493 trace_usb_host_reset(s
->bus_num
, s
->addr
);
1495 rc
= libusb_reset_device(s
->dh
);
1501 static int usb_host_alloc_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1502 int nr_eps
, int streams
)
1505 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1506 unsigned char endpoints
[30];
1509 for (i
= 0; i
< nr_eps
; i
++) {
1510 endpoints
[i
] = eps
[i
]->nr
;
1511 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1512 endpoints
[i
] |= 0x80;
1515 rc
= libusb_alloc_streams(s
->dh
, streams
, endpoints
, nr_eps
);
1517 usb_host_libusb_error("libusb_alloc_streams", rc
);
1518 } else if (rc
!= streams
) {
1519 error_report("libusb_alloc_streams: got less streams "
1520 "then requested %d < %d", rc
, streams
);
1523 return (rc
== streams
) ? 0 : -1;
1525 error_report("libusb_alloc_streams: error not implemented");
1530 static void usb_host_free_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1534 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1535 unsigned char endpoints
[30];
1538 for (i
= 0; i
< nr_eps
; i
++) {
1539 endpoints
[i
] = eps
[i
]->nr
;
1540 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1541 endpoints
[i
] |= 0x80;
1544 libusb_free_streams(s
->dh
, endpoints
, nr_eps
);
1549 * This is *NOT* about restoring state. We have absolutely no idea
1550 * what state the host device is in at the moment and whenever it is
1551 * still present in the first place. Attemping to contine where we
1552 * left off is impossible.
1554 * What we are going to do here is emulate a surprise removal of
1555 * the usb device passed through, then kick host scan so the device
1556 * will get re-attached (and re-initialized by the guest) in case it
1559 * As the device removal will change the state of other devices (usb
1560 * host controller, most likely interrupt controller too) we have to
1561 * wait with it until *all* vmstate is loaded. Thus post_load just
1562 * kicks a bottom half which then does the actual work.
1564 static void usb_host_post_load_bh(void *opaque
)
1566 USBHostDevice
*dev
= opaque
;
1567 USBDevice
*udev
= USB_DEVICE(dev
);
1569 if (dev
->dh
!= NULL
) {
1570 usb_host_close(dev
);
1572 if (udev
->attached
) {
1573 usb_device_detach(udev
);
1575 dev
->bh_postld_pending
= false;
1576 usb_host_auto_check(NULL
);
1579 static int usb_host_post_load(void *opaque
, int version_id
)
1581 USBHostDevice
*dev
= opaque
;
1583 if (!dev
->bh_postld
) {
1584 dev
->bh_postld
= qemu_bh_new(usb_host_post_load_bh
, dev
);
1586 qemu_bh_schedule(dev
->bh_postld
);
1587 dev
->bh_postld_pending
= true;
1591 static const VMStateDescription vmstate_usb_host
= {
1594 .minimum_version_id
= 1,
1595 .post_load
= usb_host_post_load
,
1596 .fields
= (VMStateField
[]) {
1597 VMSTATE_USB_DEVICE(parent_obj
, USBHostDevice
),
1598 VMSTATE_END_OF_LIST()
1602 static Property usb_host_dev_properties
[] = {
1603 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1604 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1605 DEFINE_PROP_STRING("hostport", USBHostDevice
, match
.port
),
1606 DEFINE_PROP_UINT32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1607 DEFINE_PROP_UINT32("productid", USBHostDevice
, match
.product_id
, 0),
1608 DEFINE_PROP_UINT32("isobufs", USBHostDevice
, iso_urb_count
, 4),
1609 DEFINE_PROP_UINT32("isobsize", USBHostDevice
, iso_urb_frames
, 32),
1610 DEFINE_PROP_BOOL("guest-reset", USBHostDevice
,
1611 allow_one_guest_reset
, true),
1612 DEFINE_PROP_BOOL("guest-resets-all", USBHostDevice
,
1613 allow_all_guest_resets
, false),
1614 DEFINE_PROP_UINT32("loglevel", USBHostDevice
, loglevel
,
1615 LIBUSB_LOG_LEVEL_WARNING
),
1616 DEFINE_PROP_BIT("pipeline", USBHostDevice
, options
,
1617 USB_HOST_OPT_PIPELINE
, true),
1618 DEFINE_PROP_BOOL("suppress-remote-wake", USBHostDevice
,
1619 suppress_remote_wake
, true),
1620 DEFINE_PROP_END_OF_LIST(),
1623 static void usb_host_class_initfn(ObjectClass
*klass
, void *data
)
1625 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1626 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1628 uc
->realize
= usb_host_realize
;
1629 uc
->product_desc
= "USB Host Device";
1630 uc
->cancel_packet
= usb_host_cancel_packet
;
1631 uc
->handle_data
= usb_host_handle_data
;
1632 uc
->handle_control
= usb_host_handle_control
;
1633 uc
->handle_reset
= usb_host_handle_reset
;
1634 uc
->unrealize
= usb_host_unrealize
;
1635 uc
->flush_ep_queue
= usb_host_flush_ep_queue
;
1636 uc
->alloc_streams
= usb_host_alloc_streams
;
1637 uc
->free_streams
= usb_host_free_streams
;
1638 dc
->vmsd
= &vmstate_usb_host
;
1639 device_class_set_props(dc
, usb_host_dev_properties
);
1640 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
1643 static TypeInfo usb_host_dev_info
= {
1644 .name
= TYPE_USB_HOST_DEVICE
,
1645 .parent
= TYPE_USB_DEVICE
,
1646 .instance_size
= sizeof(USBHostDevice
),
1647 .class_init
= usb_host_class_initfn
,
1648 .instance_init
= usb_host_instance_init
,
1651 static void usb_host_register_types(void)
1653 type_register_static(&usb_host_dev_info
);
1656 type_init(usb_host_register_types
)
1658 /* ------------------------------------------------------------------------ */
1660 static QEMUTimer
*usb_auto_timer
;
1661 static VMChangeStateEntry
*usb_vmstate
;
1663 static void usb_host_vm_state(void *unused
, int running
, RunState state
)
1666 usb_host_auto_check(unused
);
1670 static void usb_host_auto_check(void *unused
)
1672 struct USBHostDevice
*s
;
1673 struct USBAutoFilter
*f
;
1674 libusb_device
**devs
= NULL
;
1675 struct libusb_device_descriptor ddesc
;
1676 int unconnected
= 0;
1679 if (usb_host_init() != 0) {
1683 if (runstate_is_running()) {
1684 n
= libusb_get_device_list(ctx
, &devs
);
1685 for (i
= 0; i
< n
; i
++) {
1686 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1689 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1692 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1694 if (f
->bus_num
> 0 &&
1695 f
->bus_num
!= libusb_get_bus_number(devs
[i
])) {
1699 f
->addr
!= libusb_get_device_address(devs
[i
])) {
1702 if (f
->port
!= NULL
) {
1703 char port
[16] = "-";
1704 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1705 if (strcmp(f
->port
, port
) != 0) {
1709 if (f
->vendor_id
> 0 &&
1710 f
->vendor_id
!= ddesc
.idVendor
) {
1713 if (f
->product_id
> 0 &&
1714 f
->product_id
!= ddesc
.idProduct
) {
1718 /* We got a match */
1720 if (s
->errcount
>= 3) {
1723 if (s
->dh
!= NULL
) {
1726 if (usb_host_open(s
, devs
[i
]) < 0) {
1733 libusb_free_device_list(devs
, 1);
1735 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1736 if (s
->dh
== NULL
) {
1749 if (unconnected
== 0) {
1750 /* nothing to watch */
1751 if (usb_auto_timer
) {
1752 timer_del(usb_auto_timer
);
1753 trace_usb_host_auto_scan_disabled();
1761 usb_vmstate
= qemu_add_vm_change_state_handler(usb_host_vm_state
, NULL
);
1763 if (!usb_auto_timer
) {
1764 usb_auto_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, usb_host_auto_check
, NULL
);
1765 if (!usb_auto_timer
) {
1768 trace_usb_host_auto_scan_enabled();
1770 timer_mod(usb_auto_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 2000);
1774 * Check whether USB host device has a USB mass storage SCSI interface
1776 bool usb_host_dev_is_scsi_storage(USBDevice
*ud
)
1778 USBHostDevice
*uhd
= USB_HOST_DEVICE(ud
);
1779 struct libusb_config_descriptor
*conf
;
1780 const struct libusb_interface_descriptor
*intf
;
1781 bool is_scsi_storage
= false;
1784 if (!uhd
|| libusb_get_active_config_descriptor(uhd
->dev
, &conf
) != 0) {
1788 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
1789 intf
= &conf
->interface
[i
].altsetting
[ud
->altsetting
[i
]];
1790 if (intf
->bInterfaceClass
== LIBUSB_CLASS_MASS_STORAGE
&&
1791 intf
->bInterfaceSubClass
== 6) { /* 6 means SCSI */
1792 is_scsi_storage
= true;
1797 libusb_free_config_descriptor(conf
);
1799 return is_scsi_storage
;
1802 void hmp_info_usbhost(Monitor
*mon
, const QDict
*qdict
)
1804 libusb_device
**devs
= NULL
;
1805 struct libusb_device_descriptor ddesc
;
1809 if (usb_host_init() != 0) {
1813 n
= libusb_get_device_list(ctx
, &devs
);
1814 for (i
= 0; i
< n
; i
++) {
1815 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1818 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1821 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1822 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1823 libusb_get_bus_number(devs
[i
]),
1824 libusb_get_device_address(devs
[i
]),
1826 speed_name
[libusb_get_device_speed(devs
[i
])]);
1827 monitor_printf(mon
, " Class %02x:", ddesc
.bDeviceClass
);
1828 monitor_printf(mon
, " USB device %04x:%04x",
1829 ddesc
.idVendor
, ddesc
.idProduct
);
1830 if (ddesc
.iProduct
) {
1831 libusb_device_handle
*handle
;
1832 if (libusb_open(devs
[i
], &handle
) == 0) {
1833 unsigned char name
[64] = "";
1834 libusb_get_string_descriptor_ascii(handle
,
1836 name
, sizeof(name
));
1837 libusb_close(handle
);
1838 monitor_printf(mon
, ", %s", name
);
1841 monitor_printf(mon
, "\n");
1843 libusb_free_device_list(devs
, 1);