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 "qemu-common.h"
44 #include "monitor/monitor.h"
45 #include "qemu/error-report.h"
46 #include "sysemu/sysemu.h"
51 /* ------------------------------------------------------------------------ */
53 #define TYPE_USB_HOST_DEVICE "usb-host"
54 #define USB_HOST_DEVICE(obj) \
55 OBJECT_CHECK(USBHostDevice, (obj), TYPE_USB_HOST_DEVICE)
57 typedef struct USBHostDevice USBHostDevice
;
58 typedef struct USBHostRequest USBHostRequest
;
59 typedef struct USBHostIsoXfer USBHostIsoXfer
;
60 typedef struct USBHostIsoRing USBHostIsoRing
;
62 struct USBAutoFilter
{
70 enum USBHostDeviceOptions
{
71 USB_HOST_OPT_PIPELINE
,
74 struct USBHostDevice
{
78 struct USBAutoFilter match
;
80 uint32_t iso_urb_count
;
81 uint32_t iso_urb_frames
;
87 QTAILQ_ENTRY(USBHostDevice
) next
;
94 libusb_device_handle
*dh
;
95 struct libusb_device_descriptor ddesc
;
100 } ifs
[USB_MAX_INTERFACES
];
102 /* callbacks & friends */
108 QTAILQ_HEAD(, USBHostRequest
) requests
;
109 QTAILQ_HEAD(, USBHostIsoRing
) isorings
;
112 struct USBHostRequest
{
116 struct libusb_transfer
*xfer
;
117 unsigned char *buffer
;
121 QTAILQ_ENTRY(USBHostRequest
) next
;
124 struct USBHostIsoXfer
{
125 USBHostIsoRing
*ring
;
126 struct libusb_transfer
*xfer
;
129 QTAILQ_ENTRY(USBHostIsoXfer
) next
;
132 struct USBHostIsoRing
{
135 QTAILQ_HEAD(, USBHostIsoXfer
) unused
;
136 QTAILQ_HEAD(, USBHostIsoXfer
) inflight
;
137 QTAILQ_HEAD(, USBHostIsoXfer
) copy
;
138 QTAILQ_ENTRY(USBHostIsoRing
) next
;
141 static QTAILQ_HEAD(, USBHostDevice
) hostdevs
=
142 QTAILQ_HEAD_INITIALIZER(hostdevs
);
144 static void usb_host_auto_check(void *unused
);
145 static void usb_host_release_interfaces(USBHostDevice
*s
);
146 static void usb_host_nodev(USBHostDevice
*s
);
147 static void usb_host_detach_kernel(USBHostDevice
*s
);
148 static void usb_host_attach_kernel(USBHostDevice
*s
);
150 /* ------------------------------------------------------------------------ */
152 #ifndef LIBUSB_LOG_LEVEL_WARNING /* older libusb didn't define these */
153 #define LIBUSB_LOG_LEVEL_WARNING 2
156 /* ------------------------------------------------------------------------ */
158 #define CONTROL_TIMEOUT 10000 /* 10 sec */
159 #define BULK_TIMEOUT 0 /* unlimited */
160 #define INTR_TIMEOUT 0 /* unlimited */
162 #ifndef LIBUSB_API_VERSION
163 # define LIBUSB_API_VERSION LIBUSBX_API_VERSION
165 #if LIBUSB_API_VERSION >= 0x01000103
166 # define HAVE_STREAMS 1
169 static const char *speed_name
[] = {
170 [LIBUSB_SPEED_UNKNOWN
] = "?",
171 [LIBUSB_SPEED_LOW
] = "1.5",
172 [LIBUSB_SPEED_FULL
] = "12",
173 [LIBUSB_SPEED_HIGH
] = "480",
174 [LIBUSB_SPEED_SUPER
] = "5000",
177 static const unsigned int speed_map
[] = {
178 [LIBUSB_SPEED_LOW
] = USB_SPEED_LOW
,
179 [LIBUSB_SPEED_FULL
] = USB_SPEED_FULL
,
180 [LIBUSB_SPEED_HIGH
] = USB_SPEED_HIGH
,
181 [LIBUSB_SPEED_SUPER
] = USB_SPEED_SUPER
,
184 static const unsigned int status_map
[] = {
185 [LIBUSB_TRANSFER_COMPLETED
] = USB_RET_SUCCESS
,
186 [LIBUSB_TRANSFER_ERROR
] = USB_RET_IOERROR
,
187 [LIBUSB_TRANSFER_TIMED_OUT
] = USB_RET_IOERROR
,
188 [LIBUSB_TRANSFER_CANCELLED
] = USB_RET_IOERROR
,
189 [LIBUSB_TRANSFER_STALL
] = USB_RET_STALL
,
190 [LIBUSB_TRANSFER_NO_DEVICE
] = USB_RET_NODEV
,
191 [LIBUSB_TRANSFER_OVERFLOW
] = USB_RET_BABBLE
,
194 static const char *err_names
[] = {
195 [-LIBUSB_ERROR_IO
] = "IO",
196 [-LIBUSB_ERROR_INVALID_PARAM
] = "INVALID_PARAM",
197 [-LIBUSB_ERROR_ACCESS
] = "ACCESS",
198 [-LIBUSB_ERROR_NO_DEVICE
] = "NO_DEVICE",
199 [-LIBUSB_ERROR_NOT_FOUND
] = "NOT_FOUND",
200 [-LIBUSB_ERROR_BUSY
] = "BUSY",
201 [-LIBUSB_ERROR_TIMEOUT
] = "TIMEOUT",
202 [-LIBUSB_ERROR_OVERFLOW
] = "OVERFLOW",
203 [-LIBUSB_ERROR_PIPE
] = "PIPE",
204 [-LIBUSB_ERROR_INTERRUPTED
] = "INTERRUPTED",
205 [-LIBUSB_ERROR_NO_MEM
] = "NO_MEM",
206 [-LIBUSB_ERROR_NOT_SUPPORTED
] = "NOT_SUPPORTED",
207 [-LIBUSB_ERROR_OTHER
] = "OTHER",
210 static libusb_context
*ctx
;
211 static uint32_t loglevel
;
215 static void usb_host_handle_fd(void *opaque
)
217 struct timeval tv
= { 0, 0 };
218 libusb_handle_events_timeout(ctx
, &tv
);
221 static void usb_host_add_fd(int fd
, short events
, void *user_data
)
223 qemu_set_fd_handler(fd
,
224 (events
& POLLIN
) ? usb_host_handle_fd
: NULL
,
225 (events
& POLLOUT
) ? usb_host_handle_fd
: NULL
,
229 static void usb_host_del_fd(int fd
, void *user_data
)
231 qemu_set_fd_handler(fd
, NULL
, NULL
, NULL
);
234 #endif /* !CONFIG_WIN32 */
236 static int usb_host_init(void)
239 const struct libusb_pollfd
**poll
;
246 rc
= libusb_init(&ctx
);
250 libusb_set_debug(ctx
, loglevel
);
252 /* FIXME: add support for Windows. */
254 libusb_set_pollfd_notifiers(ctx
, usb_host_add_fd
,
257 poll
= libusb_get_pollfds(ctx
);
260 for (i
= 0; poll
[i
] != NULL
; i
++) {
261 usb_host_add_fd(poll
[i
]->fd
, poll
[i
]->events
, ctx
);
269 static int usb_host_get_port(libusb_device
*dev
, char *port
, size_t len
)
275 #if LIBUSB_API_VERSION >= 0x01000102
276 rc
= libusb_get_port_numbers(dev
, path
, 7);
278 rc
= libusb_get_port_path(ctx
, dev
, path
, 7);
283 off
= snprintf(port
, len
, "%d", path
[0]);
284 for (i
= 1; i
< rc
; i
++) {
285 off
+= snprintf(port
+off
, len
-off
, ".%d", path
[i
]);
290 static void usb_host_libusb_error(const char *func
, int rc
)
298 if (-rc
< ARRAY_SIZE(err_names
) && err_names
[-rc
]) {
299 errname
= err_names
[-rc
];
303 error_report("%s: %d [%s]", func
, rc
, errname
);
306 /* ------------------------------------------------------------------------ */
308 static bool usb_host_use_combining(USBEndpoint
*ep
)
315 if (ep
->pid
!= USB_TOKEN_IN
) {
318 type
= usb_ep_get_type(ep
->dev
, ep
->pid
, ep
->nr
);
319 if (type
!= USB_ENDPOINT_XFER_BULK
) {
325 /* ------------------------------------------------------------------------ */
327 static USBHostRequest
*usb_host_req_alloc(USBHostDevice
*s
, USBPacket
*p
,
328 bool in
, size_t bufsize
)
330 USBHostRequest
*r
= g_new0(USBHostRequest
, 1);
335 r
->xfer
= libusb_alloc_transfer(0);
337 r
->buffer
= g_malloc(bufsize
);
339 QTAILQ_INSERT_TAIL(&s
->requests
, r
, next
);
343 static void usb_host_req_free(USBHostRequest
*r
)
346 QTAILQ_REMOVE(&r
->host
->requests
, r
, next
);
348 libusb_free_transfer(r
->xfer
);
353 static USBHostRequest
*usb_host_req_find(USBHostDevice
*s
, USBPacket
*p
)
357 QTAILQ_FOREACH(r
, &s
->requests
, next
) {
365 static void LIBUSB_CALL
usb_host_req_complete_ctrl(struct libusb_transfer
*xfer
)
367 USBHostRequest
*r
= xfer
->user_data
;
368 USBHostDevice
*s
= r
->host
;
369 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
372 goto out
; /* request was canceled */
375 r
->p
->status
= status_map
[xfer
->status
];
376 r
->p
->actual_length
= xfer
->actual_length
;
377 if (r
->in
&& xfer
->actual_length
) {
378 memcpy(r
->cbuf
, r
->buffer
+ 8, xfer
->actual_length
);
380 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
381 * to work redirected to a not superspeed capable hcd */
382 if (r
->usb3ep0quirk
&& xfer
->actual_length
>= 18 &&
387 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
388 r
->p
->status
, r
->p
->actual_length
);
389 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
392 usb_host_req_free(r
);
398 static void LIBUSB_CALL
usb_host_req_complete_data(struct libusb_transfer
*xfer
)
400 USBHostRequest
*r
= xfer
->user_data
;
401 USBHostDevice
*s
= r
->host
;
402 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
405 goto out
; /* request was canceled */
408 r
->p
->status
= status_map
[xfer
->status
];
409 if (r
->in
&& xfer
->actual_length
) {
410 usb_packet_copy(r
->p
, r
->buffer
, xfer
->actual_length
);
412 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
413 r
->p
->status
, r
->p
->actual_length
);
414 if (usb_host_use_combining(r
->p
->ep
)) {
415 usb_combined_input_packet_complete(USB_DEVICE(s
), r
->p
);
417 usb_packet_complete(USB_DEVICE(s
), r
->p
);
421 usb_host_req_free(r
);
427 static void usb_host_req_abort(USBHostRequest
*r
)
429 USBHostDevice
*s
= r
->host
;
430 bool inflight
= (r
->p
&& r
->p
->state
== USB_PACKET_ASYNC
);
433 r
->p
->status
= USB_RET_NODEV
;
434 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
435 r
->p
->status
, r
->p
->actual_length
);
436 if (r
->p
->ep
->nr
== 0) {
437 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
439 usb_packet_complete(USB_DEVICE(s
), r
->p
);
444 QTAILQ_REMOVE(&r
->host
->requests
, r
, next
);
448 libusb_cancel_transfer(r
->xfer
);
452 /* ------------------------------------------------------------------------ */
454 static void LIBUSB_CALL
455 usb_host_req_complete_iso(struct libusb_transfer
*transfer
)
457 USBHostIsoXfer
*xfer
= transfer
->user_data
;
460 /* USBHostIsoXfer released while inflight */
461 g_free(transfer
->buffer
);
462 libusb_free_transfer(transfer
);
466 QTAILQ_REMOVE(&xfer
->ring
->inflight
, xfer
, next
);
467 if (QTAILQ_EMPTY(&xfer
->ring
->inflight
)) {
468 USBHostDevice
*s
= xfer
->ring
->host
;
469 trace_usb_host_iso_stop(s
->bus_num
, s
->addr
, xfer
->ring
->ep
->nr
);
471 if (xfer
->ring
->ep
->pid
== USB_TOKEN_IN
) {
472 QTAILQ_INSERT_TAIL(&xfer
->ring
->copy
, xfer
, next
);
473 usb_wakeup(xfer
->ring
->ep
, 0);
475 QTAILQ_INSERT_TAIL(&xfer
->ring
->unused
, xfer
, next
);
479 static USBHostIsoRing
*usb_host_iso_alloc(USBHostDevice
*s
, USBEndpoint
*ep
)
481 USBHostIsoRing
*ring
= g_new0(USBHostIsoRing
, 1);
482 USBHostIsoXfer
*xfer
;
483 /* FIXME: check interval (for now assume one xfer per frame) */
484 int packets
= s
->iso_urb_frames
;
489 QTAILQ_INIT(&ring
->unused
);
490 QTAILQ_INIT(&ring
->inflight
);
491 QTAILQ_INIT(&ring
->copy
);
492 QTAILQ_INSERT_TAIL(&s
->isorings
, ring
, next
);
494 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
495 xfer
= g_new0(USBHostIsoXfer
, 1);
497 xfer
->xfer
= libusb_alloc_transfer(packets
);
498 xfer
->xfer
->dev_handle
= s
->dh
;
499 xfer
->xfer
->type
= LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
;
501 xfer
->xfer
->endpoint
= ring
->ep
->nr
;
502 if (ring
->ep
->pid
== USB_TOKEN_IN
) {
503 xfer
->xfer
->endpoint
|= USB_DIR_IN
;
505 xfer
->xfer
->callback
= usb_host_req_complete_iso
;
506 xfer
->xfer
->user_data
= xfer
;
508 xfer
->xfer
->num_iso_packets
= packets
;
509 xfer
->xfer
->length
= ring
->ep
->max_packet_size
* packets
;
510 xfer
->xfer
->buffer
= g_malloc0(xfer
->xfer
->length
);
512 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
518 static USBHostIsoRing
*usb_host_iso_find(USBHostDevice
*s
, USBEndpoint
*ep
)
520 USBHostIsoRing
*ring
;
522 QTAILQ_FOREACH(ring
, &s
->isorings
, next
) {
523 if (ring
->ep
== ep
) {
530 static void usb_host_iso_reset_xfer(USBHostIsoXfer
*xfer
)
532 libusb_set_iso_packet_lengths(xfer
->xfer
,
533 xfer
->ring
->ep
->max_packet_size
);
535 xfer
->copy_complete
= false;
538 static void usb_host_iso_free_xfer(USBHostIsoXfer
*xfer
, bool inflight
)
541 xfer
->xfer
->user_data
= NULL
;
543 g_free(xfer
->xfer
->buffer
);
544 libusb_free_transfer(xfer
->xfer
);
549 static void usb_host_iso_free(USBHostIsoRing
*ring
)
551 USBHostIsoXfer
*xfer
;
553 while ((xfer
= QTAILQ_FIRST(&ring
->inflight
)) != NULL
) {
554 QTAILQ_REMOVE(&ring
->inflight
, xfer
, next
);
555 usb_host_iso_free_xfer(xfer
, true);
557 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
558 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
559 usb_host_iso_free_xfer(xfer
, false);
561 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
) {
562 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
563 usb_host_iso_free_xfer(xfer
, false);
566 QTAILQ_REMOVE(&ring
->host
->isorings
, ring
, next
);
570 static void usb_host_iso_free_all(USBHostDevice
*s
)
572 USBHostIsoRing
*ring
;
574 while ((ring
= QTAILQ_FIRST(&s
->isorings
)) != NULL
) {
575 usb_host_iso_free(ring
);
579 static bool usb_host_iso_data_copy(USBHostIsoXfer
*xfer
, USBPacket
*p
)
584 buf
= libusb_get_iso_packet_buffer_simple(xfer
->xfer
, xfer
->packet
);
585 if (p
->pid
== USB_TOKEN_OUT
) {
587 if (psize
> xfer
->ring
->ep
->max_packet_size
) {
588 /* should not happen (guest bug) */
589 psize
= xfer
->ring
->ep
->max_packet_size
;
591 xfer
->xfer
->iso_packet_desc
[xfer
->packet
].length
= psize
;
593 psize
= xfer
->xfer
->iso_packet_desc
[xfer
->packet
].actual_length
;
594 if (psize
> p
->iov
.size
) {
595 /* should not happen (guest bug) */
599 usb_packet_copy(p
, buf
, psize
);
601 xfer
->copy_complete
= (xfer
->packet
== xfer
->xfer
->num_iso_packets
);
602 return xfer
->copy_complete
;
605 static void usb_host_iso_data_in(USBHostDevice
*s
, USBPacket
*p
)
607 USBHostIsoRing
*ring
;
608 USBHostIsoXfer
*xfer
;
609 bool disconnect
= false;
612 ring
= usb_host_iso_find(s
, p
->ep
);
614 ring
= usb_host_iso_alloc(s
, p
->ep
);
617 /* copy data to guest */
618 xfer
= QTAILQ_FIRST(&ring
->copy
);
620 if (usb_host_iso_data_copy(xfer
, p
)) {
621 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
622 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
626 /* submit empty bufs to host */
627 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
628 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
629 usb_host_iso_reset_xfer(xfer
);
630 rc
= libusb_submit_transfer(xfer
->xfer
);
632 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
633 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
634 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
639 if (QTAILQ_EMPTY(&ring
->inflight
)) {
640 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
642 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
650 static void usb_host_iso_data_out(USBHostDevice
*s
, USBPacket
*p
)
652 USBHostIsoRing
*ring
;
653 USBHostIsoXfer
*xfer
;
654 bool disconnect
= false;
657 ring
= usb_host_iso_find(s
, p
->ep
);
659 ring
= usb_host_iso_alloc(s
, p
->ep
);
662 /* copy data from guest */
663 xfer
= QTAILQ_FIRST(&ring
->copy
);
664 while (xfer
!= NULL
&& xfer
->copy_complete
) {
666 xfer
= QTAILQ_NEXT(xfer
, next
);
669 xfer
= QTAILQ_FIRST(&ring
->unused
);
671 trace_usb_host_iso_out_of_bufs(s
->bus_num
, s
->addr
, p
->ep
->nr
);
674 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
675 usb_host_iso_reset_xfer(xfer
);
676 QTAILQ_INSERT_TAIL(&ring
->copy
, xfer
, next
);
678 usb_host_iso_data_copy(xfer
, p
);
680 if (QTAILQ_EMPTY(&ring
->inflight
)) {
681 /* wait until half of our buffers are filled
682 before kicking the iso out stream */
683 if (filled
*2 < s
->iso_urb_count
) {
688 /* submit filled bufs to host */
689 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
&&
690 xfer
->copy_complete
) {
691 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
692 rc
= libusb_submit_transfer(xfer
->xfer
);
694 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
695 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
696 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
701 if (QTAILQ_EMPTY(&ring
->inflight
)) {
702 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
704 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
712 /* ------------------------------------------------------------------------ */
714 static void usb_host_speed_compat(USBHostDevice
*s
)
716 USBDevice
*udev
= USB_DEVICE(s
);
717 struct libusb_config_descriptor
*conf
;
718 const struct libusb_interface_descriptor
*intf
;
719 const struct libusb_endpoint_descriptor
*endp
;
721 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
723 bool compat_high
= true;
724 bool compat_full
= true;
729 rc
= libusb_get_config_descriptor(s
->dev
, c
, &conf
);
733 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
734 for (a
= 0; a
< conf
->interface
[i
].num_altsetting
; a
++) {
735 intf
= &conf
->interface
[i
].altsetting
[a
];
736 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
737 endp
= &intf
->endpoint
[e
];
738 type
= endp
->bmAttributes
& 0x3;
744 case 0x02: /* BULK */
746 rc
= libusb_get_ss_endpoint_companion_descriptor
747 (ctx
, endp
, &endp_ss_comp
);
748 if (rc
== LIBUSB_SUCCESS
) {
749 int streams
= endp_ss_comp
->bmAttributes
& 0x1f;
754 libusb_free_ss_endpoint_companion_descriptor
759 case 0x03: /* INTERRUPT */
760 if (endp
->wMaxPacketSize
> 64) {
763 if (endp
->wMaxPacketSize
> 1024) {
771 libusb_free_config_descriptor(conf
);
774 udev
->speedmask
= (1 << udev
->speed
);
775 if (udev
->speed
== USB_SPEED_SUPER
&& compat_high
) {
776 udev
->speedmask
|= USB_SPEED_MASK_HIGH
;
778 if (udev
->speed
== USB_SPEED_SUPER
&& compat_full
) {
779 udev
->speedmask
|= USB_SPEED_MASK_FULL
;
781 if (udev
->speed
== USB_SPEED_HIGH
&& compat_full
) {
782 udev
->speedmask
|= USB_SPEED_MASK_FULL
;
786 static void usb_host_ep_update(USBHostDevice
*s
)
788 static const char *tname
[] = {
789 [USB_ENDPOINT_XFER_CONTROL
] = "control",
790 [USB_ENDPOINT_XFER_ISOC
] = "isoc",
791 [USB_ENDPOINT_XFER_BULK
] = "bulk",
792 [USB_ENDPOINT_XFER_INT
] = "int",
794 USBDevice
*udev
= USB_DEVICE(s
);
795 struct libusb_config_descriptor
*conf
;
796 const struct libusb_interface_descriptor
*intf
;
797 const struct libusb_endpoint_descriptor
*endp
;
799 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
806 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
810 trace_usb_host_parse_config(s
->bus_num
, s
->addr
,
811 conf
->bConfigurationValue
, true);
813 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
814 assert(udev
->altsetting
[i
] < conf
->interface
[i
].num_altsetting
);
815 intf
= &conf
->interface
[i
].altsetting
[udev
->altsetting
[i
]];
816 trace_usb_host_parse_interface(s
->bus_num
, s
->addr
,
817 intf
->bInterfaceNumber
,
818 intf
->bAlternateSetting
, true);
819 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
820 endp
= &intf
->endpoint
[e
];
822 devep
= endp
->bEndpointAddress
;
823 pid
= (devep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
825 type
= endp
->bmAttributes
& 0x3;
828 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
829 "invalid endpoint address");
832 if (usb_ep_get_type(udev
, pid
, ep
) != USB_ENDPOINT_XFER_INVALID
) {
833 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
834 "duplicate endpoint address");
838 trace_usb_host_parse_endpoint(s
->bus_num
, s
->addr
, ep
,
839 (devep
& USB_DIR_IN
) ? "in" : "out",
841 usb_ep_set_max_packet_size(udev
, pid
, ep
,
842 endp
->wMaxPacketSize
);
843 usb_ep_set_type(udev
, pid
, ep
, type
);
844 usb_ep_set_ifnum(udev
, pid
, ep
, i
);
845 usb_ep_set_halted(udev
, pid
, ep
, 0);
847 if (type
== LIBUSB_TRANSFER_TYPE_BULK
&&
848 libusb_get_ss_endpoint_companion_descriptor(ctx
, endp
,
849 &endp_ss_comp
) == LIBUSB_SUCCESS
) {
850 usb_ep_set_max_streams(udev
, pid
, ep
,
851 endp_ss_comp
->bmAttributes
);
852 libusb_free_ss_endpoint_companion_descriptor(endp_ss_comp
);
858 libusb_free_config_descriptor(conf
);
861 static int usb_host_open(USBHostDevice
*s
, libusb_device
*dev
)
863 USBDevice
*udev
= USB_DEVICE(s
);
864 int bus_num
= libusb_get_bus_number(dev
);
865 int addr
= libusb_get_device_address(dev
);
867 Error
*local_err
= NULL
;
869 trace_usb_host_open_started(bus_num
, addr
);
874 rc
= libusb_open(dev
, &s
->dh
);
880 s
->bus_num
= bus_num
;
883 usb_host_detach_kernel(s
);
885 libusb_get_device_descriptor(dev
, &s
->ddesc
);
886 usb_host_get_port(s
->dev
, s
->port
, sizeof(s
->port
));
889 usb_host_ep_update(s
);
891 udev
->speed
= speed_map
[libusb_get_device_speed(dev
)];
892 usb_host_speed_compat(s
);
894 if (s
->ddesc
.iProduct
) {
895 libusb_get_string_descriptor_ascii(s
->dh
, s
->ddesc
.iProduct
,
896 (unsigned char *)udev
->product_desc
,
897 sizeof(udev
->product_desc
));
899 snprintf(udev
->product_desc
, sizeof(udev
->product_desc
),
900 "host:%d.%d", bus_num
, addr
);
903 usb_device_attach(udev
, &local_err
);
905 error_report_err(local_err
);
909 trace_usb_host_open_success(bus_num
, addr
);
913 trace_usb_host_open_failure(bus_num
, addr
);
915 usb_host_release_interfaces(s
);
916 libusb_reset_device(s
->dh
);
917 usb_host_attach_kernel(s
);
925 static void usb_host_abort_xfers(USBHostDevice
*s
)
927 USBHostRequest
*r
, *rtmp
;
929 QTAILQ_FOREACH_SAFE(r
, &s
->requests
, next
, rtmp
) {
930 usb_host_req_abort(r
);
934 static int usb_host_close(USBHostDevice
*s
)
936 USBDevice
*udev
= USB_DEVICE(s
);
942 trace_usb_host_close(s
->bus_num
, s
->addr
);
944 usb_host_abort_xfers(s
);
945 usb_host_iso_free_all(s
);
947 if (udev
->attached
) {
948 usb_device_detach(udev
);
951 usb_host_release_interfaces(s
);
952 libusb_reset_device(s
->dh
);
953 usb_host_attach_kernel(s
);
958 usb_host_auto_check(NULL
);
962 static void usb_host_nodev_bh(void *opaque
)
964 USBHostDevice
*s
= opaque
;
968 static void usb_host_nodev(USBHostDevice
*s
)
971 s
->bh_nodev
= qemu_bh_new(usb_host_nodev_bh
, s
);
973 qemu_bh_schedule(s
->bh_nodev
);
976 static void usb_host_exit_notifier(struct Notifier
*n
, void *data
)
978 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
981 usb_host_release_interfaces(s
);
982 usb_host_attach_kernel(s
);
986 static libusb_device
*usb_host_find_ref(int bus
, int addr
)
988 libusb_device
**devs
= NULL
;
989 libusb_device
*ret
= NULL
;
992 if (usb_host_init() != 0) {
995 n
= libusb_get_device_list(ctx
, &devs
);
996 for (i
= 0; i
< n
; i
++) {
997 if (libusb_get_bus_number(devs
[i
]) == bus
&&
998 libusb_get_device_address(devs
[i
]) == addr
) {
999 ret
= libusb_ref_device(devs
[i
]);
1003 libusb_free_device_list(devs
, 1);
1007 static void usb_host_realize(USBDevice
*udev
, Error
**errp
)
1009 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1010 libusb_device
*ldev
;
1013 if (s
->match
.vendor_id
> 0xffff) {
1014 error_setg(errp
, "vendorid out of range");
1017 if (s
->match
.product_id
> 0xffff) {
1018 error_setg(errp
, "productid out of range");
1021 if (s
->match
.addr
> 127) {
1022 error_setg(errp
, "hostaddr out of range");
1026 loglevel
= s
->loglevel
;
1027 udev
->flags
|= (1 << USB_DEV_FLAG_IS_HOST
);
1028 udev
->auto_attach
= 0;
1029 QTAILQ_INIT(&s
->requests
);
1030 QTAILQ_INIT(&s
->isorings
);
1032 if (s
->match
.addr
&& s
->match
.bus_num
&&
1033 !s
->match
.vendor_id
&&
1034 !s
->match
.product_id
&&
1036 s
->needs_autoscan
= false;
1037 ldev
= usb_host_find_ref(s
->match
.bus_num
,
1040 error_setg(errp
, "failed to find host usb device %d:%d",
1041 s
->match
.bus_num
, s
->match
.addr
);
1044 rc
= usb_host_open(s
, ldev
);
1045 libusb_unref_device(ldev
);
1047 error_setg(errp
, "failed to open host usb device %d:%d",
1048 s
->match
.bus_num
, s
->match
.addr
);
1052 s
->needs_autoscan
= true;
1053 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
1054 usb_host_auto_check(NULL
);
1057 s
->exit
.notify
= usb_host_exit_notifier
;
1058 qemu_add_exit_notifier(&s
->exit
);
1061 static void usb_host_instance_init(Object
*obj
)
1063 USBDevice
*udev
= USB_DEVICE(obj
);
1064 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1066 device_add_bootindex_property(obj
, &s
->bootindex
,
1071 static void usb_host_unrealize(USBDevice
*udev
, Error
**errp
)
1073 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1075 qemu_remove_exit_notifier(&s
->exit
);
1076 if (s
->needs_autoscan
) {
1077 QTAILQ_REMOVE(&hostdevs
, s
, next
);
1082 static void usb_host_cancel_packet(USBDevice
*udev
, USBPacket
*p
)
1084 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1088 usb_combined_packet_cancel(udev
, p
);
1092 trace_usb_host_req_canceled(s
->bus_num
, s
->addr
, p
);
1094 r
= usb_host_req_find(s
, p
);
1096 r
->p
= NULL
; /* mark as dead */
1097 libusb_cancel_transfer(r
->xfer
);
1101 static void usb_host_detach_kernel(USBHostDevice
*s
)
1103 struct libusb_config_descriptor
*conf
;
1106 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1110 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1111 rc
= libusb_kernel_driver_active(s
->dh
, i
);
1112 usb_host_libusb_error("libusb_kernel_driver_active", rc
);
1116 trace_usb_host_detach_kernel(s
->bus_num
, s
->addr
, i
);
1117 rc
= libusb_detach_kernel_driver(s
->dh
, i
);
1118 usb_host_libusb_error("libusb_detach_kernel_driver", rc
);
1119 s
->ifs
[i
].detached
= true;
1121 libusb_free_config_descriptor(conf
);
1124 static void usb_host_attach_kernel(USBHostDevice
*s
)
1126 struct libusb_config_descriptor
*conf
;
1129 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1133 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1134 if (!s
->ifs
[i
].detached
) {
1137 trace_usb_host_attach_kernel(s
->bus_num
, s
->addr
, i
);
1138 libusb_attach_kernel_driver(s
->dh
, i
);
1139 s
->ifs
[i
].detached
= false;
1141 libusb_free_config_descriptor(conf
);
1144 static int usb_host_claim_interfaces(USBHostDevice
*s
, int configuration
)
1146 USBDevice
*udev
= USB_DEVICE(s
);
1147 struct libusb_config_descriptor
*conf
;
1150 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1151 udev
->altsetting
[i
] = 0;
1153 udev
->ninterfaces
= 0;
1154 udev
->configuration
= 0;
1156 usb_host_detach_kernel(s
);
1158 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1160 if (rc
== LIBUSB_ERROR_NOT_FOUND
) {
1161 /* address state - ignore */
1162 return USB_RET_SUCCESS
;
1164 return USB_RET_STALL
;
1168 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1169 trace_usb_host_claim_interface(s
->bus_num
, s
->addr
, configuration
, i
);
1170 rc
= libusb_claim_interface(s
->dh
, i
);
1172 s
->ifs
[i
].claimed
= true;
1173 if (++claimed
== conf
->bNumInterfaces
) {
1178 if (claimed
!= conf
->bNumInterfaces
) {
1179 return USB_RET_STALL
;
1182 udev
->ninterfaces
= conf
->bNumInterfaces
;
1183 udev
->configuration
= configuration
;
1185 libusb_free_config_descriptor(conf
);
1186 return USB_RET_SUCCESS
;
1189 static void usb_host_release_interfaces(USBHostDevice
*s
)
1193 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1194 if (!s
->ifs
[i
].claimed
) {
1197 trace_usb_host_release_interface(s
->bus_num
, s
->addr
, i
);
1198 rc
= libusb_release_interface(s
->dh
, i
);
1199 usb_host_libusb_error("libusb_release_interface", rc
);
1200 s
->ifs
[i
].claimed
= false;
1204 static void usb_host_set_address(USBHostDevice
*s
, int addr
)
1206 USBDevice
*udev
= USB_DEVICE(s
);
1208 trace_usb_host_set_address(s
->bus_num
, s
->addr
, addr
);
1212 static void usb_host_set_config(USBHostDevice
*s
, int config
, USBPacket
*p
)
1216 trace_usb_host_set_config(s
->bus_num
, s
->addr
, config
);
1218 usb_host_release_interfaces(s
);
1219 rc
= libusb_set_configuration(s
->dh
, config
);
1221 usb_host_libusb_error("libusb_set_configuration", rc
);
1222 p
->status
= USB_RET_STALL
;
1223 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1228 p
->status
= usb_host_claim_interfaces(s
, config
);
1229 if (p
->status
!= USB_RET_SUCCESS
) {
1232 usb_host_ep_update(s
);
1235 static void usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
,
1238 USBDevice
*udev
= USB_DEVICE(s
);
1241 trace_usb_host_set_interface(s
->bus_num
, s
->addr
, iface
, alt
);
1243 usb_host_iso_free_all(s
);
1245 if (iface
>= USB_MAX_INTERFACES
) {
1246 p
->status
= USB_RET_STALL
;
1250 rc
= libusb_set_interface_alt_setting(s
->dh
, iface
, alt
);
1252 usb_host_libusb_error("libusb_set_interface_alt_setting", rc
);
1253 p
->status
= USB_RET_STALL
;
1254 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1260 udev
->altsetting
[iface
] = alt
;
1261 usb_host_ep_update(s
);
1264 static void usb_host_handle_control(USBDevice
*udev
, USBPacket
*p
,
1265 int request
, int value
, int index
,
1266 int length
, uint8_t *data
)
1268 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1272 trace_usb_host_req_control(s
->bus_num
, s
->addr
, p
, request
, value
, index
);
1274 if (s
->dh
== NULL
) {
1275 p
->status
= USB_RET_NODEV
;
1276 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1281 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1282 usb_host_set_address(s
, value
);
1283 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1286 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1287 usb_host_set_config(s
, value
& 0xff, p
);
1288 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1291 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1292 usb_host_set_interface(s
, index
, value
, p
);
1293 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1296 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
1297 if (value
== 0) { /* clear halt */
1298 int pid
= (index
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1299 libusb_clear_halt(s
->dh
, index
);
1300 usb_ep_set_halted(udev
, pid
, index
& 0x0f, 0);
1301 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1306 r
= usb_host_req_alloc(s
, p
, (request
>> 8) & USB_DIR_IN
, length
+ 8);
1309 memcpy(r
->buffer
, udev
->setup_buf
, 8);
1311 memcpy(r
->buffer
+ 8, r
->cbuf
, r
->clen
);
1314 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
1315 * to work redirected to a not superspeed capable hcd */
1316 if ((udev
->speedmask
& USB_SPEED_MASK_SUPER
) &&
1317 !(udev
->port
->speedmask
& USB_SPEED_MASK_SUPER
) &&
1318 request
== 0x8006 && value
== 0x100 && index
== 0) {
1319 r
->usb3ep0quirk
= true;
1322 libusb_fill_control_transfer(r
->xfer
, s
->dh
, r
->buffer
,
1323 usb_host_req_complete_ctrl
, r
,
1325 rc
= libusb_submit_transfer(r
->xfer
);
1327 p
->status
= USB_RET_NODEV
;
1328 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1329 p
->status
, p
->actual_length
);
1330 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1336 p
->status
= USB_RET_ASYNC
;
1339 static void usb_host_handle_data(USBDevice
*udev
, USBPacket
*p
)
1341 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1346 if (usb_host_use_combining(p
->ep
) && p
->state
== USB_PACKET_SETUP
) {
1347 p
->status
= USB_RET_ADD_TO_QUEUE
;
1351 trace_usb_host_req_data(s
->bus_num
, s
->addr
, p
,
1352 p
->pid
== USB_TOKEN_IN
,
1353 p
->ep
->nr
, p
->iov
.size
);
1355 if (s
->dh
== NULL
) {
1356 p
->status
= USB_RET_NODEV
;
1357 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1360 if (p
->ep
->halted
) {
1361 p
->status
= USB_RET_STALL
;
1362 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1366 switch (usb_ep_get_type(udev
, p
->pid
, p
->ep
->nr
)) {
1367 case USB_ENDPOINT_XFER_BULK
:
1368 size
= usb_packet_size(p
);
1369 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, size
);
1371 usb_packet_copy(p
, r
->buffer
, size
);
1373 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1376 libusb_fill_bulk_stream_transfer(r
->xfer
, s
->dh
, ep
, p
->stream
,
1378 usb_host_req_complete_data
, r
,
1381 usb_host_req_free(r
);
1382 p
->status
= USB_RET_STALL
;
1386 libusb_fill_bulk_transfer(r
->xfer
, s
->dh
, ep
,
1388 usb_host_req_complete_data
, r
,
1392 case USB_ENDPOINT_XFER_INT
:
1393 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, p
->iov
.size
);
1395 usb_packet_copy(p
, r
->buffer
, p
->iov
.size
);
1397 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1398 libusb_fill_interrupt_transfer(r
->xfer
, s
->dh
, ep
,
1399 r
->buffer
, p
->iov
.size
,
1400 usb_host_req_complete_data
, r
,
1403 case USB_ENDPOINT_XFER_ISOC
:
1404 if (p
->pid
== USB_TOKEN_IN
) {
1405 usb_host_iso_data_in(s
, p
);
1407 usb_host_iso_data_out(s
, p
);
1409 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1410 p
->status
, p
->actual_length
);
1413 p
->status
= USB_RET_STALL
;
1414 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1415 p
->status
, p
->actual_length
);
1419 rc
= libusb_submit_transfer(r
->xfer
);
1421 p
->status
= USB_RET_NODEV
;
1422 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1423 p
->status
, p
->actual_length
);
1424 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1430 p
->status
= USB_RET_ASYNC
;
1433 static void usb_host_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
1435 if (usb_host_use_combining(ep
)) {
1436 usb_ep_combine_input_packets(ep
);
1440 static void usb_host_handle_reset(USBDevice
*udev
)
1442 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1445 trace_usb_host_reset(s
->bus_num
, s
->addr
);
1447 rc
= libusb_reset_device(s
->dh
);
1453 static int usb_host_alloc_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1454 int nr_eps
, int streams
)
1457 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1458 unsigned char endpoints
[30];
1461 for (i
= 0; i
< nr_eps
; i
++) {
1462 endpoints
[i
] = eps
[i
]->nr
;
1463 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1464 endpoints
[i
] |= 0x80;
1467 rc
= libusb_alloc_streams(s
->dh
, streams
, endpoints
, nr_eps
);
1469 usb_host_libusb_error("libusb_alloc_streams", rc
);
1470 } else if (rc
!= streams
) {
1471 error_report("libusb_alloc_streams: got less streams "
1472 "then requested %d < %d", rc
, streams
);
1475 return (rc
== streams
) ? 0 : -1;
1477 error_report("libusb_alloc_streams: error not implemented");
1482 static void usb_host_free_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1486 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1487 unsigned char endpoints
[30];
1490 for (i
= 0; i
< nr_eps
; i
++) {
1491 endpoints
[i
] = eps
[i
]->nr
;
1492 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1493 endpoints
[i
] |= 0x80;
1496 libusb_free_streams(s
->dh
, endpoints
, nr_eps
);
1501 * This is *NOT* about restoring state. We have absolutely no idea
1502 * what state the host device is in at the moment and whenever it is
1503 * still present in the first place. Attemping to contine where we
1504 * left off is impossible.
1506 * What we are going to do here is emulate a surprise removal of
1507 * the usb device passed through, then kick host scan so the device
1508 * will get re-attached (and re-initialized by the guest) in case it
1511 * As the device removal will change the state of other devices (usb
1512 * host controller, most likely interrupt controller too) we have to
1513 * wait with it until *all* vmstate is loaded. Thus post_load just
1514 * kicks a bottom half which then does the actual work.
1516 static void usb_host_post_load_bh(void *opaque
)
1518 USBHostDevice
*dev
= opaque
;
1519 USBDevice
*udev
= USB_DEVICE(dev
);
1521 if (dev
->dh
!= NULL
) {
1522 usb_host_close(dev
);
1524 if (udev
->attached
) {
1525 usb_device_detach(udev
);
1527 usb_host_auto_check(NULL
);
1530 static int usb_host_post_load(void *opaque
, int version_id
)
1532 USBHostDevice
*dev
= opaque
;
1534 if (!dev
->bh_postld
) {
1535 dev
->bh_postld
= qemu_bh_new(usb_host_post_load_bh
, dev
);
1537 qemu_bh_schedule(dev
->bh_postld
);
1541 static const VMStateDescription vmstate_usb_host
= {
1544 .minimum_version_id
= 1,
1545 .post_load
= usb_host_post_load
,
1546 .fields
= (VMStateField
[]) {
1547 VMSTATE_USB_DEVICE(parent_obj
, USBHostDevice
),
1548 VMSTATE_END_OF_LIST()
1552 static Property usb_host_dev_properties
[] = {
1553 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1554 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1555 DEFINE_PROP_STRING("hostport", USBHostDevice
, match
.port
),
1556 DEFINE_PROP_UINT32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1557 DEFINE_PROP_UINT32("productid", USBHostDevice
, match
.product_id
, 0),
1558 DEFINE_PROP_UINT32("isobufs", USBHostDevice
, iso_urb_count
, 4),
1559 DEFINE_PROP_UINT32("isobsize", USBHostDevice
, iso_urb_frames
, 32),
1560 DEFINE_PROP_UINT32("loglevel", USBHostDevice
, loglevel
,
1561 LIBUSB_LOG_LEVEL_WARNING
),
1562 DEFINE_PROP_BIT("pipeline", USBHostDevice
, options
,
1563 USB_HOST_OPT_PIPELINE
, true),
1564 DEFINE_PROP_END_OF_LIST(),
1567 static void usb_host_class_initfn(ObjectClass
*klass
, void *data
)
1569 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1570 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1572 uc
->realize
= usb_host_realize
;
1573 uc
->product_desc
= "USB Host Device";
1574 uc
->cancel_packet
= usb_host_cancel_packet
;
1575 uc
->handle_data
= usb_host_handle_data
;
1576 uc
->handle_control
= usb_host_handle_control
;
1577 uc
->handle_reset
= usb_host_handle_reset
;
1578 uc
->unrealize
= usb_host_unrealize
;
1579 uc
->flush_ep_queue
= usb_host_flush_ep_queue
;
1580 uc
->alloc_streams
= usb_host_alloc_streams
;
1581 uc
->free_streams
= usb_host_free_streams
;
1582 dc
->vmsd
= &vmstate_usb_host
;
1583 dc
->props
= usb_host_dev_properties
;
1584 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
1587 static TypeInfo usb_host_dev_info
= {
1588 .name
= TYPE_USB_HOST_DEVICE
,
1589 .parent
= TYPE_USB_DEVICE
,
1590 .instance_size
= sizeof(USBHostDevice
),
1591 .class_init
= usb_host_class_initfn
,
1592 .instance_init
= usb_host_instance_init
,
1595 static void usb_host_register_types(void)
1597 type_register_static(&usb_host_dev_info
);
1600 type_init(usb_host_register_types
)
1602 /* ------------------------------------------------------------------------ */
1604 static QEMUTimer
*usb_auto_timer
;
1605 static VMChangeStateEntry
*usb_vmstate
;
1607 static void usb_host_vm_state(void *unused
, int running
, RunState state
)
1610 usb_host_auto_check(unused
);
1614 static void usb_host_auto_check(void *unused
)
1616 struct USBHostDevice
*s
;
1617 struct USBAutoFilter
*f
;
1618 libusb_device
**devs
= NULL
;
1619 struct libusb_device_descriptor ddesc
;
1620 int unconnected
= 0;
1623 if (usb_host_init() != 0) {
1627 if (runstate_is_running()) {
1628 n
= libusb_get_device_list(ctx
, &devs
);
1629 for (i
= 0; i
< n
; i
++) {
1630 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1633 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1636 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1638 if (f
->bus_num
> 0 &&
1639 f
->bus_num
!= libusb_get_bus_number(devs
[i
])) {
1643 f
->addr
!= libusb_get_device_address(devs
[i
])) {
1646 if (f
->port
!= NULL
) {
1647 char port
[16] = "-";
1648 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1649 if (strcmp(f
->port
, port
) != 0) {
1653 if (f
->vendor_id
> 0 &&
1654 f
->vendor_id
!= ddesc
.idVendor
) {
1657 if (f
->product_id
> 0 &&
1658 f
->product_id
!= ddesc
.idProduct
) {
1662 /* We got a match */
1664 if (s
->errcount
>= 3) {
1667 if (s
->dh
!= NULL
) {
1670 if (usb_host_open(s
, devs
[i
]) < 0) {
1677 libusb_free_device_list(devs
, 1);
1679 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1680 if (s
->dh
== NULL
) {
1693 if (unconnected
== 0) {
1694 /* nothing to watch */
1695 if (usb_auto_timer
) {
1696 timer_del(usb_auto_timer
);
1697 trace_usb_host_auto_scan_disabled();
1705 usb_vmstate
= qemu_add_vm_change_state_handler(usb_host_vm_state
, NULL
);
1707 if (!usb_auto_timer
) {
1708 usb_auto_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, usb_host_auto_check
, NULL
);
1709 if (!usb_auto_timer
) {
1712 trace_usb_host_auto_scan_enabled();
1714 timer_mod(usb_auto_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 2000);
1718 * Check whether USB host device has a USB mass storage SCSI interface
1720 bool usb_host_dev_is_scsi_storage(USBDevice
*ud
)
1722 USBHostDevice
*uhd
= USB_HOST_DEVICE(ud
);
1723 struct libusb_config_descriptor
*conf
;
1724 const struct libusb_interface_descriptor
*intf
;
1725 bool is_scsi_storage
= false;
1728 if (!uhd
|| libusb_get_active_config_descriptor(uhd
->dev
, &conf
) != 0) {
1732 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
1733 intf
= &conf
->interface
[i
].altsetting
[ud
->altsetting
[i
]];
1734 if (intf
->bInterfaceClass
== LIBUSB_CLASS_MASS_STORAGE
&&
1735 intf
->bInterfaceSubClass
== 6) { /* 6 means SCSI */
1736 is_scsi_storage
= true;
1741 libusb_free_config_descriptor(conf
);
1743 return is_scsi_storage
;
1746 void hmp_info_usbhost(Monitor
*mon
, const QDict
*qdict
)
1748 libusb_device
**devs
= NULL
;
1749 struct libusb_device_descriptor ddesc
;
1753 if (usb_host_init() != 0) {
1757 n
= libusb_get_device_list(ctx
, &devs
);
1758 for (i
= 0; i
< n
; i
++) {
1759 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1762 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1765 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1766 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1767 libusb_get_bus_number(devs
[i
]),
1768 libusb_get_device_address(devs
[i
]),
1770 speed_name
[libusb_get_device_speed(devs
[i
])]);
1771 monitor_printf(mon
, " Class %02x:", ddesc
.bDeviceClass
);
1772 monitor_printf(mon
, " USB device %04x:%04x",
1773 ddesc
.idVendor
, ddesc
.idProduct
);
1774 if (ddesc
.iProduct
) {
1775 libusb_device_handle
*handle
;
1776 if (libusb_open(devs
[i
], &handle
) == 0) {
1777 unsigned char name
[64] = "";
1778 libusb_get_string_descriptor_ascii(handle
,
1780 name
, sizeof(name
));
1781 libusb_close(handle
);
1782 monitor_printf(mon
, ", %s", name
);
1785 monitor_printf(mon
, "\n");
1787 libusb_free_device_list(devs
, 1);