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 */
105 bool bh_postld_pending
;
109 QTAILQ_HEAD(, USBHostRequest
) requests
;
110 QTAILQ_HEAD(, USBHostIsoRing
) isorings
;
113 struct USBHostRequest
{
117 struct libusb_transfer
*xfer
;
118 unsigned char *buffer
;
122 QTAILQ_ENTRY(USBHostRequest
) next
;
125 struct USBHostIsoXfer
{
126 USBHostIsoRing
*ring
;
127 struct libusb_transfer
*xfer
;
130 QTAILQ_ENTRY(USBHostIsoXfer
) next
;
133 struct USBHostIsoRing
{
136 QTAILQ_HEAD(, USBHostIsoXfer
) unused
;
137 QTAILQ_HEAD(, USBHostIsoXfer
) inflight
;
138 QTAILQ_HEAD(, USBHostIsoXfer
) copy
;
139 QTAILQ_ENTRY(USBHostIsoRing
) next
;
142 static QTAILQ_HEAD(, USBHostDevice
) hostdevs
=
143 QTAILQ_HEAD_INITIALIZER(hostdevs
);
145 static void usb_host_auto_check(void *unused
);
146 static void usb_host_release_interfaces(USBHostDevice
*s
);
147 static void usb_host_nodev(USBHostDevice
*s
);
148 static void usb_host_detach_kernel(USBHostDevice
*s
);
149 static void usb_host_attach_kernel(USBHostDevice
*s
);
151 /* ------------------------------------------------------------------------ */
153 #ifndef LIBUSB_LOG_LEVEL_WARNING /* older libusb didn't define these */
154 #define LIBUSB_LOG_LEVEL_WARNING 2
157 /* ------------------------------------------------------------------------ */
159 #define CONTROL_TIMEOUT 10000 /* 10 sec */
160 #define BULK_TIMEOUT 0 /* unlimited */
161 #define INTR_TIMEOUT 0 /* unlimited */
163 #ifndef LIBUSB_API_VERSION
164 # define LIBUSB_API_VERSION LIBUSBX_API_VERSION
166 #if LIBUSB_API_VERSION >= 0x01000103
167 # define HAVE_STREAMS 1
170 static const char *speed_name
[] = {
171 [LIBUSB_SPEED_UNKNOWN
] = "?",
172 [LIBUSB_SPEED_LOW
] = "1.5",
173 [LIBUSB_SPEED_FULL
] = "12",
174 [LIBUSB_SPEED_HIGH
] = "480",
175 [LIBUSB_SPEED_SUPER
] = "5000",
178 static const unsigned int speed_map
[] = {
179 [LIBUSB_SPEED_LOW
] = USB_SPEED_LOW
,
180 [LIBUSB_SPEED_FULL
] = USB_SPEED_FULL
,
181 [LIBUSB_SPEED_HIGH
] = USB_SPEED_HIGH
,
182 [LIBUSB_SPEED_SUPER
] = USB_SPEED_SUPER
,
185 static const unsigned int status_map
[] = {
186 [LIBUSB_TRANSFER_COMPLETED
] = USB_RET_SUCCESS
,
187 [LIBUSB_TRANSFER_ERROR
] = USB_RET_IOERROR
,
188 [LIBUSB_TRANSFER_TIMED_OUT
] = USB_RET_IOERROR
,
189 [LIBUSB_TRANSFER_CANCELLED
] = USB_RET_IOERROR
,
190 [LIBUSB_TRANSFER_STALL
] = USB_RET_STALL
,
191 [LIBUSB_TRANSFER_NO_DEVICE
] = USB_RET_NODEV
,
192 [LIBUSB_TRANSFER_OVERFLOW
] = USB_RET_BABBLE
,
195 static const char *err_names
[] = {
196 [-LIBUSB_ERROR_IO
] = "IO",
197 [-LIBUSB_ERROR_INVALID_PARAM
] = "INVALID_PARAM",
198 [-LIBUSB_ERROR_ACCESS
] = "ACCESS",
199 [-LIBUSB_ERROR_NO_DEVICE
] = "NO_DEVICE",
200 [-LIBUSB_ERROR_NOT_FOUND
] = "NOT_FOUND",
201 [-LIBUSB_ERROR_BUSY
] = "BUSY",
202 [-LIBUSB_ERROR_TIMEOUT
] = "TIMEOUT",
203 [-LIBUSB_ERROR_OVERFLOW
] = "OVERFLOW",
204 [-LIBUSB_ERROR_PIPE
] = "PIPE",
205 [-LIBUSB_ERROR_INTERRUPTED
] = "INTERRUPTED",
206 [-LIBUSB_ERROR_NO_MEM
] = "NO_MEM",
207 [-LIBUSB_ERROR_NOT_SUPPORTED
] = "NOT_SUPPORTED",
208 [-LIBUSB_ERROR_OTHER
] = "OTHER",
211 static libusb_context
*ctx
;
212 static uint32_t loglevel
;
216 static void usb_host_handle_fd(void *opaque
)
218 struct timeval tv
= { 0, 0 };
219 libusb_handle_events_timeout(ctx
, &tv
);
222 static void usb_host_add_fd(int fd
, short events
, void *user_data
)
224 qemu_set_fd_handler(fd
,
225 (events
& POLLIN
) ? usb_host_handle_fd
: NULL
,
226 (events
& POLLOUT
) ? usb_host_handle_fd
: NULL
,
230 static void usb_host_del_fd(int fd
, void *user_data
)
232 qemu_set_fd_handler(fd
, NULL
, NULL
, NULL
);
235 #endif /* !CONFIG_WIN32 */
237 static int usb_host_init(void)
240 const struct libusb_pollfd
**poll
;
247 rc
= libusb_init(&ctx
);
251 #if LIBUSB_API_VERSION >= 0x01000106
252 libusb_set_option(ctx
, LIBUSB_OPTION_LOG_LEVEL
, loglevel
);
254 libusb_set_debug(ctx
, loglevel
);
257 /* FIXME: add support for Windows. */
259 libusb_set_pollfd_notifiers(ctx
, usb_host_add_fd
,
262 poll
= libusb_get_pollfds(ctx
);
265 for (i
= 0; poll
[i
] != NULL
; i
++) {
266 usb_host_add_fd(poll
[i
]->fd
, poll
[i
]->events
, ctx
);
274 static int usb_host_get_port(libusb_device
*dev
, char *port
, size_t len
)
280 #if LIBUSB_API_VERSION >= 0x01000102
281 rc
= libusb_get_port_numbers(dev
, path
, 7);
283 rc
= libusb_get_port_path(ctx
, dev
, path
, 7);
288 off
= snprintf(port
, len
, "%d", path
[0]);
289 for (i
= 1; i
< rc
; i
++) {
290 off
+= snprintf(port
+off
, len
-off
, ".%d", path
[i
]);
295 static void usb_host_libusb_error(const char *func
, int rc
)
303 if (-rc
< ARRAY_SIZE(err_names
) && err_names
[-rc
]) {
304 errname
= err_names
[-rc
];
308 error_report("%s: %d [%s]", func
, rc
, errname
);
311 /* ------------------------------------------------------------------------ */
313 static bool usb_host_use_combining(USBEndpoint
*ep
)
320 if (ep
->pid
!= USB_TOKEN_IN
) {
323 type
= usb_ep_get_type(ep
->dev
, ep
->pid
, ep
->nr
);
324 if (type
!= USB_ENDPOINT_XFER_BULK
) {
330 /* ------------------------------------------------------------------------ */
332 static USBHostRequest
*usb_host_req_alloc(USBHostDevice
*s
, USBPacket
*p
,
333 bool in
, size_t bufsize
)
335 USBHostRequest
*r
= g_new0(USBHostRequest
, 1);
340 r
->xfer
= libusb_alloc_transfer(0);
342 r
->buffer
= g_malloc(bufsize
);
344 QTAILQ_INSERT_TAIL(&s
->requests
, r
, next
);
348 static void usb_host_req_free(USBHostRequest
*r
)
351 QTAILQ_REMOVE(&r
->host
->requests
, r
, next
);
353 libusb_free_transfer(r
->xfer
);
358 static USBHostRequest
*usb_host_req_find(USBHostDevice
*s
, USBPacket
*p
)
362 QTAILQ_FOREACH(r
, &s
->requests
, next
) {
370 static void LIBUSB_CALL
usb_host_req_complete_ctrl(struct libusb_transfer
*xfer
)
372 USBHostRequest
*r
= xfer
->user_data
;
373 USBHostDevice
*s
= r
->host
;
374 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
377 goto out
; /* request was canceled */
380 r
->p
->status
= status_map
[xfer
->status
];
381 r
->p
->actual_length
= xfer
->actual_length
;
382 if (r
->in
&& xfer
->actual_length
) {
383 memcpy(r
->cbuf
, r
->buffer
+ 8, xfer
->actual_length
);
385 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
386 * to work redirected to a not superspeed capable hcd */
387 if (r
->usb3ep0quirk
&& xfer
->actual_length
>= 18 &&
392 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
393 r
->p
->status
, r
->p
->actual_length
);
394 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
397 usb_host_req_free(r
);
403 static void LIBUSB_CALL
usb_host_req_complete_data(struct libusb_transfer
*xfer
)
405 USBHostRequest
*r
= xfer
->user_data
;
406 USBHostDevice
*s
= r
->host
;
407 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
410 goto out
; /* request was canceled */
413 r
->p
->status
= status_map
[xfer
->status
];
414 if (r
->in
&& xfer
->actual_length
) {
415 usb_packet_copy(r
->p
, r
->buffer
, xfer
->actual_length
);
417 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
418 r
->p
->status
, r
->p
->actual_length
);
419 if (usb_host_use_combining(r
->p
->ep
)) {
420 usb_combined_input_packet_complete(USB_DEVICE(s
), r
->p
);
422 usb_packet_complete(USB_DEVICE(s
), r
->p
);
426 usb_host_req_free(r
);
432 static void usb_host_req_abort(USBHostRequest
*r
)
434 USBHostDevice
*s
= r
->host
;
435 bool inflight
= (r
->p
&& r
->p
->state
== USB_PACKET_ASYNC
);
438 r
->p
->status
= USB_RET_NODEV
;
439 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
440 r
->p
->status
, r
->p
->actual_length
);
441 if (r
->p
->ep
->nr
== 0) {
442 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
444 usb_packet_complete(USB_DEVICE(s
), r
->p
);
449 QTAILQ_REMOVE(&r
->host
->requests
, r
, next
);
453 libusb_cancel_transfer(r
->xfer
);
457 /* ------------------------------------------------------------------------ */
459 static void LIBUSB_CALL
460 usb_host_req_complete_iso(struct libusb_transfer
*transfer
)
462 USBHostIsoXfer
*xfer
= transfer
->user_data
;
465 /* USBHostIsoXfer released while inflight */
466 g_free(transfer
->buffer
);
467 libusb_free_transfer(transfer
);
471 QTAILQ_REMOVE(&xfer
->ring
->inflight
, xfer
, next
);
472 if (QTAILQ_EMPTY(&xfer
->ring
->inflight
)) {
473 USBHostDevice
*s
= xfer
->ring
->host
;
474 trace_usb_host_iso_stop(s
->bus_num
, s
->addr
, xfer
->ring
->ep
->nr
);
476 if (xfer
->ring
->ep
->pid
== USB_TOKEN_IN
) {
477 QTAILQ_INSERT_TAIL(&xfer
->ring
->copy
, xfer
, next
);
478 usb_wakeup(xfer
->ring
->ep
, 0);
480 QTAILQ_INSERT_TAIL(&xfer
->ring
->unused
, xfer
, next
);
484 static USBHostIsoRing
*usb_host_iso_alloc(USBHostDevice
*s
, USBEndpoint
*ep
)
486 USBHostIsoRing
*ring
= g_new0(USBHostIsoRing
, 1);
487 USBHostIsoXfer
*xfer
;
488 /* FIXME: check interval (for now assume one xfer per frame) */
489 int packets
= s
->iso_urb_frames
;
494 QTAILQ_INIT(&ring
->unused
);
495 QTAILQ_INIT(&ring
->inflight
);
496 QTAILQ_INIT(&ring
->copy
);
497 QTAILQ_INSERT_TAIL(&s
->isorings
, ring
, next
);
499 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
500 xfer
= g_new0(USBHostIsoXfer
, 1);
502 xfer
->xfer
= libusb_alloc_transfer(packets
);
503 xfer
->xfer
->dev_handle
= s
->dh
;
504 xfer
->xfer
->type
= LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
;
506 xfer
->xfer
->endpoint
= ring
->ep
->nr
;
507 if (ring
->ep
->pid
== USB_TOKEN_IN
) {
508 xfer
->xfer
->endpoint
|= USB_DIR_IN
;
510 xfer
->xfer
->callback
= usb_host_req_complete_iso
;
511 xfer
->xfer
->user_data
= xfer
;
513 xfer
->xfer
->num_iso_packets
= packets
;
514 xfer
->xfer
->length
= ring
->ep
->max_packet_size
* packets
;
515 xfer
->xfer
->buffer
= g_malloc0(xfer
->xfer
->length
);
517 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
523 static USBHostIsoRing
*usb_host_iso_find(USBHostDevice
*s
, USBEndpoint
*ep
)
525 USBHostIsoRing
*ring
;
527 QTAILQ_FOREACH(ring
, &s
->isorings
, next
) {
528 if (ring
->ep
== ep
) {
535 static void usb_host_iso_reset_xfer(USBHostIsoXfer
*xfer
)
537 libusb_set_iso_packet_lengths(xfer
->xfer
,
538 xfer
->ring
->ep
->max_packet_size
);
540 xfer
->copy_complete
= false;
543 static void usb_host_iso_free_xfer(USBHostIsoXfer
*xfer
, bool inflight
)
546 xfer
->xfer
->user_data
= NULL
;
548 g_free(xfer
->xfer
->buffer
);
549 libusb_free_transfer(xfer
->xfer
);
554 static void usb_host_iso_free(USBHostIsoRing
*ring
)
556 USBHostIsoXfer
*xfer
;
558 while ((xfer
= QTAILQ_FIRST(&ring
->inflight
)) != NULL
) {
559 QTAILQ_REMOVE(&ring
->inflight
, xfer
, next
);
560 usb_host_iso_free_xfer(xfer
, true);
562 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
563 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
564 usb_host_iso_free_xfer(xfer
, false);
566 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
) {
567 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
568 usb_host_iso_free_xfer(xfer
, false);
571 QTAILQ_REMOVE(&ring
->host
->isorings
, ring
, next
);
575 static void usb_host_iso_free_all(USBHostDevice
*s
)
577 USBHostIsoRing
*ring
;
579 while ((ring
= QTAILQ_FIRST(&s
->isorings
)) != NULL
) {
580 usb_host_iso_free(ring
);
584 static bool usb_host_iso_data_copy(USBHostIsoXfer
*xfer
, USBPacket
*p
)
589 buf
= libusb_get_iso_packet_buffer_simple(xfer
->xfer
, xfer
->packet
);
590 if (p
->pid
== USB_TOKEN_OUT
) {
592 if (psize
> xfer
->ring
->ep
->max_packet_size
) {
593 /* should not happen (guest bug) */
594 psize
= xfer
->ring
->ep
->max_packet_size
;
596 xfer
->xfer
->iso_packet_desc
[xfer
->packet
].length
= psize
;
598 psize
= xfer
->xfer
->iso_packet_desc
[xfer
->packet
].actual_length
;
599 if (psize
> p
->iov
.size
) {
600 /* should not happen (guest bug) */
604 usb_packet_copy(p
, buf
, psize
);
606 xfer
->copy_complete
= (xfer
->packet
== xfer
->xfer
->num_iso_packets
);
607 return xfer
->copy_complete
;
610 static void usb_host_iso_data_in(USBHostDevice
*s
, USBPacket
*p
)
612 USBHostIsoRing
*ring
;
613 USBHostIsoXfer
*xfer
;
614 bool disconnect
= false;
617 ring
= usb_host_iso_find(s
, p
->ep
);
619 ring
= usb_host_iso_alloc(s
, p
->ep
);
622 /* copy data to guest */
623 xfer
= QTAILQ_FIRST(&ring
->copy
);
625 if (usb_host_iso_data_copy(xfer
, p
)) {
626 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
627 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
631 /* submit empty bufs to host */
632 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
633 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
634 usb_host_iso_reset_xfer(xfer
);
635 rc
= libusb_submit_transfer(xfer
->xfer
);
637 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
638 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
639 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
644 if (QTAILQ_EMPTY(&ring
->inflight
)) {
645 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
647 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
655 static void usb_host_iso_data_out(USBHostDevice
*s
, USBPacket
*p
)
657 USBHostIsoRing
*ring
;
658 USBHostIsoXfer
*xfer
;
659 bool disconnect
= false;
662 ring
= usb_host_iso_find(s
, p
->ep
);
664 ring
= usb_host_iso_alloc(s
, p
->ep
);
667 /* copy data from guest */
668 xfer
= QTAILQ_FIRST(&ring
->copy
);
669 while (xfer
!= NULL
&& xfer
->copy_complete
) {
671 xfer
= QTAILQ_NEXT(xfer
, next
);
674 xfer
= QTAILQ_FIRST(&ring
->unused
);
676 trace_usb_host_iso_out_of_bufs(s
->bus_num
, s
->addr
, p
->ep
->nr
);
679 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
680 usb_host_iso_reset_xfer(xfer
);
681 QTAILQ_INSERT_TAIL(&ring
->copy
, xfer
, next
);
683 usb_host_iso_data_copy(xfer
, p
);
685 if (QTAILQ_EMPTY(&ring
->inflight
)) {
686 /* wait until half of our buffers are filled
687 before kicking the iso out stream */
688 if (filled
*2 < s
->iso_urb_count
) {
693 /* submit filled bufs to host */
694 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
&&
695 xfer
->copy_complete
) {
696 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
697 rc
= libusb_submit_transfer(xfer
->xfer
);
699 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
700 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
701 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
706 if (QTAILQ_EMPTY(&ring
->inflight
)) {
707 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
709 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
717 /* ------------------------------------------------------------------------ */
719 static void usb_host_speed_compat(USBHostDevice
*s
)
721 USBDevice
*udev
= USB_DEVICE(s
);
722 struct libusb_config_descriptor
*conf
;
723 const struct libusb_interface_descriptor
*intf
;
724 const struct libusb_endpoint_descriptor
*endp
;
726 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
728 bool compat_high
= true;
729 bool compat_full
= true;
734 rc
= libusb_get_config_descriptor(s
->dev
, c
, &conf
);
738 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
739 for (a
= 0; a
< conf
->interface
[i
].num_altsetting
; a
++) {
740 intf
= &conf
->interface
[i
].altsetting
[a
];
741 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
742 endp
= &intf
->endpoint
[e
];
743 type
= endp
->bmAttributes
& 0x3;
749 case 0x02: /* BULK */
751 rc
= libusb_get_ss_endpoint_companion_descriptor
752 (ctx
, endp
, &endp_ss_comp
);
753 if (rc
== LIBUSB_SUCCESS
) {
754 int streams
= endp_ss_comp
->bmAttributes
& 0x1f;
759 libusb_free_ss_endpoint_companion_descriptor
764 case 0x03: /* INTERRUPT */
765 if (endp
->wMaxPacketSize
> 64) {
768 if (endp
->wMaxPacketSize
> 1024) {
776 libusb_free_config_descriptor(conf
);
779 udev
->speedmask
= (1 << udev
->speed
);
780 if (udev
->speed
== USB_SPEED_SUPER
&& compat_high
) {
781 udev
->speedmask
|= USB_SPEED_MASK_HIGH
;
783 if (udev
->speed
== USB_SPEED_SUPER
&& compat_full
) {
784 udev
->speedmask
|= USB_SPEED_MASK_FULL
;
786 if (udev
->speed
== USB_SPEED_HIGH
&& compat_full
) {
787 udev
->speedmask
|= USB_SPEED_MASK_FULL
;
791 static void usb_host_ep_update(USBHostDevice
*s
)
793 static const char *tname
[] = {
794 [USB_ENDPOINT_XFER_CONTROL
] = "control",
795 [USB_ENDPOINT_XFER_ISOC
] = "isoc",
796 [USB_ENDPOINT_XFER_BULK
] = "bulk",
797 [USB_ENDPOINT_XFER_INT
] = "int",
799 USBDevice
*udev
= USB_DEVICE(s
);
800 struct libusb_config_descriptor
*conf
;
801 const struct libusb_interface_descriptor
*intf
;
802 const struct libusb_endpoint_descriptor
*endp
;
804 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
811 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
815 trace_usb_host_parse_config(s
->bus_num
, s
->addr
,
816 conf
->bConfigurationValue
, true);
818 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
819 assert(udev
->altsetting
[i
] < conf
->interface
[i
].num_altsetting
);
820 intf
= &conf
->interface
[i
].altsetting
[udev
->altsetting
[i
]];
821 trace_usb_host_parse_interface(s
->bus_num
, s
->addr
,
822 intf
->bInterfaceNumber
,
823 intf
->bAlternateSetting
, true);
824 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
825 endp
= &intf
->endpoint
[e
];
827 devep
= endp
->bEndpointAddress
;
828 pid
= (devep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
830 type
= endp
->bmAttributes
& 0x3;
833 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
834 "invalid endpoint address");
837 if (usb_ep_get_type(udev
, pid
, ep
) != USB_ENDPOINT_XFER_INVALID
) {
838 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
839 "duplicate endpoint address");
843 trace_usb_host_parse_endpoint(s
->bus_num
, s
->addr
, ep
,
844 (devep
& USB_DIR_IN
) ? "in" : "out",
846 usb_ep_set_max_packet_size(udev
, pid
, ep
,
847 endp
->wMaxPacketSize
);
848 usb_ep_set_type(udev
, pid
, ep
, type
);
849 usb_ep_set_ifnum(udev
, pid
, ep
, i
);
850 usb_ep_set_halted(udev
, pid
, ep
, 0);
852 if (type
== LIBUSB_TRANSFER_TYPE_BULK
&&
853 libusb_get_ss_endpoint_companion_descriptor(ctx
, endp
,
854 &endp_ss_comp
) == LIBUSB_SUCCESS
) {
855 usb_ep_set_max_streams(udev
, pid
, ep
,
856 endp_ss_comp
->bmAttributes
);
857 libusb_free_ss_endpoint_companion_descriptor(endp_ss_comp
);
863 libusb_free_config_descriptor(conf
);
866 static int usb_host_open(USBHostDevice
*s
, libusb_device
*dev
)
868 USBDevice
*udev
= USB_DEVICE(s
);
869 int bus_num
= libusb_get_bus_number(dev
);
870 int addr
= libusb_get_device_address(dev
);
872 Error
*local_err
= NULL
;
874 if (s
->bh_postld_pending
) {
878 trace_usb_host_open_started(bus_num
, addr
);
883 rc
= libusb_open(dev
, &s
->dh
);
889 s
->bus_num
= bus_num
;
892 usb_host_detach_kernel(s
);
894 libusb_get_device_descriptor(dev
, &s
->ddesc
);
895 usb_host_get_port(s
->dev
, s
->port
, sizeof(s
->port
));
898 usb_host_ep_update(s
);
900 udev
->speed
= speed_map
[libusb_get_device_speed(dev
)];
901 usb_host_speed_compat(s
);
903 if (s
->ddesc
.iProduct
) {
904 libusb_get_string_descriptor_ascii(s
->dh
, s
->ddesc
.iProduct
,
905 (unsigned char *)udev
->product_desc
,
906 sizeof(udev
->product_desc
));
908 snprintf(udev
->product_desc
, sizeof(udev
->product_desc
),
909 "host:%d.%d", bus_num
, addr
);
912 usb_device_attach(udev
, &local_err
);
914 error_report_err(local_err
);
918 trace_usb_host_open_success(bus_num
, addr
);
922 trace_usb_host_open_failure(bus_num
, addr
);
924 usb_host_release_interfaces(s
);
925 libusb_reset_device(s
->dh
);
926 usb_host_attach_kernel(s
);
934 static void usb_host_abort_xfers(USBHostDevice
*s
)
936 USBHostRequest
*r
, *rtmp
;
938 QTAILQ_FOREACH_SAFE(r
, &s
->requests
, next
, rtmp
) {
939 usb_host_req_abort(r
);
943 static int usb_host_close(USBHostDevice
*s
)
945 USBDevice
*udev
= USB_DEVICE(s
);
951 trace_usb_host_close(s
->bus_num
, s
->addr
);
953 usb_host_abort_xfers(s
);
954 usb_host_iso_free_all(s
);
956 if (udev
->attached
) {
957 usb_device_detach(udev
);
960 usb_host_release_interfaces(s
);
961 libusb_reset_device(s
->dh
);
962 usb_host_attach_kernel(s
);
967 usb_host_auto_check(NULL
);
971 static void usb_host_nodev_bh(void *opaque
)
973 USBHostDevice
*s
= opaque
;
977 static void usb_host_nodev(USBHostDevice
*s
)
980 s
->bh_nodev
= qemu_bh_new(usb_host_nodev_bh
, s
);
982 qemu_bh_schedule(s
->bh_nodev
);
985 static void usb_host_exit_notifier(struct Notifier
*n
, void *data
)
987 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
990 usb_host_release_interfaces(s
);
991 usb_host_attach_kernel(s
);
995 static libusb_device
*usb_host_find_ref(int bus
, int addr
)
997 libusb_device
**devs
= NULL
;
998 libusb_device
*ret
= NULL
;
1001 if (usb_host_init() != 0) {
1004 n
= libusb_get_device_list(ctx
, &devs
);
1005 for (i
= 0; i
< n
; i
++) {
1006 if (libusb_get_bus_number(devs
[i
]) == bus
&&
1007 libusb_get_device_address(devs
[i
]) == addr
) {
1008 ret
= libusb_ref_device(devs
[i
]);
1012 libusb_free_device_list(devs
, 1);
1016 static void usb_host_realize(USBDevice
*udev
, Error
**errp
)
1018 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1019 libusb_device
*ldev
;
1022 if (s
->match
.vendor_id
> 0xffff) {
1023 error_setg(errp
, "vendorid out of range");
1026 if (s
->match
.product_id
> 0xffff) {
1027 error_setg(errp
, "productid out of range");
1030 if (s
->match
.addr
> 127) {
1031 error_setg(errp
, "hostaddr out of range");
1035 loglevel
= s
->loglevel
;
1036 udev
->flags
|= (1 << USB_DEV_FLAG_IS_HOST
);
1037 udev
->auto_attach
= 0;
1038 QTAILQ_INIT(&s
->requests
);
1039 QTAILQ_INIT(&s
->isorings
);
1041 if (s
->match
.addr
&& s
->match
.bus_num
&&
1042 !s
->match
.vendor_id
&&
1043 !s
->match
.product_id
&&
1045 s
->needs_autoscan
= false;
1046 ldev
= usb_host_find_ref(s
->match
.bus_num
,
1049 error_setg(errp
, "failed to find host usb device %d:%d",
1050 s
->match
.bus_num
, s
->match
.addr
);
1053 rc
= usb_host_open(s
, ldev
);
1054 libusb_unref_device(ldev
);
1056 error_setg(errp
, "failed to open host usb device %d:%d",
1057 s
->match
.bus_num
, s
->match
.addr
);
1061 s
->needs_autoscan
= true;
1062 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
1063 usb_host_auto_check(NULL
);
1066 s
->exit
.notify
= usb_host_exit_notifier
;
1067 qemu_add_exit_notifier(&s
->exit
);
1070 static void usb_host_instance_init(Object
*obj
)
1072 USBDevice
*udev
= USB_DEVICE(obj
);
1073 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1075 device_add_bootindex_property(obj
, &s
->bootindex
,
1080 static void usb_host_unrealize(USBDevice
*udev
, Error
**errp
)
1082 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1084 qemu_remove_exit_notifier(&s
->exit
);
1085 if (s
->needs_autoscan
) {
1086 QTAILQ_REMOVE(&hostdevs
, s
, next
);
1091 static void usb_host_cancel_packet(USBDevice
*udev
, USBPacket
*p
)
1093 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1097 usb_combined_packet_cancel(udev
, p
);
1101 trace_usb_host_req_canceled(s
->bus_num
, s
->addr
, p
);
1103 r
= usb_host_req_find(s
, p
);
1105 r
->p
= NULL
; /* mark as dead */
1106 libusb_cancel_transfer(r
->xfer
);
1110 static void usb_host_detach_kernel(USBHostDevice
*s
)
1112 struct libusb_config_descriptor
*conf
;
1115 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1119 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1120 rc
= libusb_kernel_driver_active(s
->dh
, i
);
1121 usb_host_libusb_error("libusb_kernel_driver_active", rc
);
1124 s
->ifs
[i
].detached
= true;
1128 trace_usb_host_detach_kernel(s
->bus_num
, s
->addr
, i
);
1129 rc
= libusb_detach_kernel_driver(s
->dh
, i
);
1130 usb_host_libusb_error("libusb_detach_kernel_driver", rc
);
1131 s
->ifs
[i
].detached
= true;
1133 libusb_free_config_descriptor(conf
);
1136 static void usb_host_attach_kernel(USBHostDevice
*s
)
1138 struct libusb_config_descriptor
*conf
;
1141 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1145 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1146 if (!s
->ifs
[i
].detached
) {
1149 trace_usb_host_attach_kernel(s
->bus_num
, s
->addr
, i
);
1150 libusb_attach_kernel_driver(s
->dh
, i
);
1151 s
->ifs
[i
].detached
= false;
1153 libusb_free_config_descriptor(conf
);
1156 static int usb_host_claim_interfaces(USBHostDevice
*s
, int configuration
)
1158 USBDevice
*udev
= USB_DEVICE(s
);
1159 struct libusb_config_descriptor
*conf
;
1162 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1163 udev
->altsetting
[i
] = 0;
1165 udev
->ninterfaces
= 0;
1166 udev
->configuration
= 0;
1168 usb_host_detach_kernel(s
);
1170 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1172 if (rc
== LIBUSB_ERROR_NOT_FOUND
) {
1173 /* address state - ignore */
1174 return USB_RET_SUCCESS
;
1176 return USB_RET_STALL
;
1180 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1181 trace_usb_host_claim_interface(s
->bus_num
, s
->addr
, configuration
, i
);
1182 rc
= libusb_claim_interface(s
->dh
, i
);
1184 s
->ifs
[i
].claimed
= true;
1185 if (++claimed
== conf
->bNumInterfaces
) {
1190 if (claimed
!= conf
->bNumInterfaces
) {
1191 return USB_RET_STALL
;
1194 udev
->ninterfaces
= conf
->bNumInterfaces
;
1195 udev
->configuration
= configuration
;
1197 libusb_free_config_descriptor(conf
);
1198 return USB_RET_SUCCESS
;
1201 static void usb_host_release_interfaces(USBHostDevice
*s
)
1205 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1206 if (!s
->ifs
[i
].claimed
) {
1209 trace_usb_host_release_interface(s
->bus_num
, s
->addr
, i
);
1210 rc
= libusb_release_interface(s
->dh
, i
);
1211 usb_host_libusb_error("libusb_release_interface", rc
);
1212 s
->ifs
[i
].claimed
= false;
1216 static void usb_host_set_address(USBHostDevice
*s
, int addr
)
1218 USBDevice
*udev
= USB_DEVICE(s
);
1220 trace_usb_host_set_address(s
->bus_num
, s
->addr
, addr
);
1224 static void usb_host_set_config(USBHostDevice
*s
, int config
, USBPacket
*p
)
1228 trace_usb_host_set_config(s
->bus_num
, s
->addr
, config
);
1230 usb_host_release_interfaces(s
);
1231 rc
= libusb_set_configuration(s
->dh
, config
);
1233 usb_host_libusb_error("libusb_set_configuration", rc
);
1234 p
->status
= USB_RET_STALL
;
1235 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1240 p
->status
= usb_host_claim_interfaces(s
, config
);
1241 if (p
->status
!= USB_RET_SUCCESS
) {
1244 usb_host_ep_update(s
);
1247 static void usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
,
1250 USBDevice
*udev
= USB_DEVICE(s
);
1253 trace_usb_host_set_interface(s
->bus_num
, s
->addr
, iface
, alt
);
1255 usb_host_iso_free_all(s
);
1257 if (iface
>= USB_MAX_INTERFACES
) {
1258 p
->status
= USB_RET_STALL
;
1262 rc
= libusb_set_interface_alt_setting(s
->dh
, iface
, alt
);
1264 usb_host_libusb_error("libusb_set_interface_alt_setting", rc
);
1265 p
->status
= USB_RET_STALL
;
1266 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1272 udev
->altsetting
[iface
] = alt
;
1273 usb_host_ep_update(s
);
1276 static void usb_host_handle_control(USBDevice
*udev
, USBPacket
*p
,
1277 int request
, int value
, int index
,
1278 int length
, uint8_t *data
)
1280 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1284 trace_usb_host_req_control(s
->bus_num
, s
->addr
, p
, request
, value
, index
);
1286 if (s
->dh
== NULL
) {
1287 p
->status
= USB_RET_NODEV
;
1288 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1293 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1294 usb_host_set_address(s
, value
);
1295 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1298 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1299 usb_host_set_config(s
, value
& 0xff, p
);
1300 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1303 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1304 usb_host_set_interface(s
, index
, value
, p
);
1305 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1308 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
1309 if (value
== 0) { /* clear halt */
1310 int pid
= (index
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1311 libusb_clear_halt(s
->dh
, index
);
1312 usb_ep_set_halted(udev
, pid
, index
& 0x0f, 0);
1313 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1318 r
= usb_host_req_alloc(s
, p
, (request
>> 8) & USB_DIR_IN
, length
+ 8);
1321 memcpy(r
->buffer
, udev
->setup_buf
, 8);
1323 memcpy(r
->buffer
+ 8, r
->cbuf
, r
->clen
);
1326 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
1327 * to work redirected to a not superspeed capable hcd */
1328 if ((udev
->speedmask
& USB_SPEED_MASK_SUPER
) &&
1329 !(udev
->port
->speedmask
& USB_SPEED_MASK_SUPER
) &&
1330 request
== 0x8006 && value
== 0x100 && index
== 0) {
1331 r
->usb3ep0quirk
= true;
1334 libusb_fill_control_transfer(r
->xfer
, s
->dh
, r
->buffer
,
1335 usb_host_req_complete_ctrl
, r
,
1337 rc
= libusb_submit_transfer(r
->xfer
);
1339 p
->status
= USB_RET_NODEV
;
1340 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1341 p
->status
, p
->actual_length
);
1342 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1348 p
->status
= USB_RET_ASYNC
;
1351 static void usb_host_handle_data(USBDevice
*udev
, USBPacket
*p
)
1353 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1358 if (usb_host_use_combining(p
->ep
) && p
->state
== USB_PACKET_SETUP
) {
1359 p
->status
= USB_RET_ADD_TO_QUEUE
;
1363 trace_usb_host_req_data(s
->bus_num
, s
->addr
, p
,
1364 p
->pid
== USB_TOKEN_IN
,
1365 p
->ep
->nr
, p
->iov
.size
);
1367 if (s
->dh
== NULL
) {
1368 p
->status
= USB_RET_NODEV
;
1369 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1372 if (p
->ep
->halted
) {
1373 p
->status
= USB_RET_STALL
;
1374 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1378 switch (usb_ep_get_type(udev
, p
->pid
, p
->ep
->nr
)) {
1379 case USB_ENDPOINT_XFER_BULK
:
1380 size
= usb_packet_size(p
);
1381 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, size
);
1383 usb_packet_copy(p
, r
->buffer
, size
);
1385 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1388 libusb_fill_bulk_stream_transfer(r
->xfer
, s
->dh
, ep
, p
->stream
,
1390 usb_host_req_complete_data
, r
,
1393 usb_host_req_free(r
);
1394 p
->status
= USB_RET_STALL
;
1398 libusb_fill_bulk_transfer(r
->xfer
, s
->dh
, ep
,
1400 usb_host_req_complete_data
, r
,
1404 case USB_ENDPOINT_XFER_INT
:
1405 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, p
->iov
.size
);
1407 usb_packet_copy(p
, r
->buffer
, p
->iov
.size
);
1409 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1410 libusb_fill_interrupt_transfer(r
->xfer
, s
->dh
, ep
,
1411 r
->buffer
, p
->iov
.size
,
1412 usb_host_req_complete_data
, r
,
1415 case USB_ENDPOINT_XFER_ISOC
:
1416 if (p
->pid
== USB_TOKEN_IN
) {
1417 usb_host_iso_data_in(s
, p
);
1419 usb_host_iso_data_out(s
, p
);
1421 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1422 p
->status
, p
->actual_length
);
1425 p
->status
= USB_RET_STALL
;
1426 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1427 p
->status
, p
->actual_length
);
1431 rc
= libusb_submit_transfer(r
->xfer
);
1433 p
->status
= USB_RET_NODEV
;
1434 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1435 p
->status
, p
->actual_length
);
1436 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1442 p
->status
= USB_RET_ASYNC
;
1445 static void usb_host_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
1447 if (usb_host_use_combining(ep
)) {
1448 usb_ep_combine_input_packets(ep
);
1452 static void usb_host_handle_reset(USBDevice
*udev
)
1454 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1457 trace_usb_host_reset(s
->bus_num
, s
->addr
);
1459 rc
= libusb_reset_device(s
->dh
);
1465 static int usb_host_alloc_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1466 int nr_eps
, int streams
)
1469 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1470 unsigned char endpoints
[30];
1473 for (i
= 0; i
< nr_eps
; i
++) {
1474 endpoints
[i
] = eps
[i
]->nr
;
1475 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1476 endpoints
[i
] |= 0x80;
1479 rc
= libusb_alloc_streams(s
->dh
, streams
, endpoints
, nr_eps
);
1481 usb_host_libusb_error("libusb_alloc_streams", rc
);
1482 } else if (rc
!= streams
) {
1483 error_report("libusb_alloc_streams: got less streams "
1484 "then requested %d < %d", rc
, streams
);
1487 return (rc
== streams
) ? 0 : -1;
1489 error_report("libusb_alloc_streams: error not implemented");
1494 static void usb_host_free_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1498 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1499 unsigned char endpoints
[30];
1502 for (i
= 0; i
< nr_eps
; i
++) {
1503 endpoints
[i
] = eps
[i
]->nr
;
1504 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1505 endpoints
[i
] |= 0x80;
1508 libusb_free_streams(s
->dh
, endpoints
, nr_eps
);
1513 * This is *NOT* about restoring state. We have absolutely no idea
1514 * what state the host device is in at the moment and whenever it is
1515 * still present in the first place. Attemping to contine where we
1516 * left off is impossible.
1518 * What we are going to do here is emulate a surprise removal of
1519 * the usb device passed through, then kick host scan so the device
1520 * will get re-attached (and re-initialized by the guest) in case it
1523 * As the device removal will change the state of other devices (usb
1524 * host controller, most likely interrupt controller too) we have to
1525 * wait with it until *all* vmstate is loaded. Thus post_load just
1526 * kicks a bottom half which then does the actual work.
1528 static void usb_host_post_load_bh(void *opaque
)
1530 USBHostDevice
*dev
= opaque
;
1531 USBDevice
*udev
= USB_DEVICE(dev
);
1533 if (dev
->dh
!= NULL
) {
1534 usb_host_close(dev
);
1536 if (udev
->attached
) {
1537 usb_device_detach(udev
);
1539 dev
->bh_postld_pending
= false;
1540 usb_host_auto_check(NULL
);
1543 static int usb_host_post_load(void *opaque
, int version_id
)
1545 USBHostDevice
*dev
= opaque
;
1547 if (!dev
->bh_postld
) {
1548 dev
->bh_postld
= qemu_bh_new(usb_host_post_load_bh
, dev
);
1550 qemu_bh_schedule(dev
->bh_postld
);
1551 dev
->bh_postld_pending
= true;
1555 static const VMStateDescription vmstate_usb_host
= {
1558 .minimum_version_id
= 1,
1559 .post_load
= usb_host_post_load
,
1560 .fields
= (VMStateField
[]) {
1561 VMSTATE_USB_DEVICE(parent_obj
, USBHostDevice
),
1562 VMSTATE_END_OF_LIST()
1566 static Property usb_host_dev_properties
[] = {
1567 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1568 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1569 DEFINE_PROP_STRING("hostport", USBHostDevice
, match
.port
),
1570 DEFINE_PROP_UINT32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1571 DEFINE_PROP_UINT32("productid", USBHostDevice
, match
.product_id
, 0),
1572 DEFINE_PROP_UINT32("isobufs", USBHostDevice
, iso_urb_count
, 4),
1573 DEFINE_PROP_UINT32("isobsize", USBHostDevice
, iso_urb_frames
, 32),
1574 DEFINE_PROP_UINT32("loglevel", USBHostDevice
, loglevel
,
1575 LIBUSB_LOG_LEVEL_WARNING
),
1576 DEFINE_PROP_BIT("pipeline", USBHostDevice
, options
,
1577 USB_HOST_OPT_PIPELINE
, true),
1578 DEFINE_PROP_END_OF_LIST(),
1581 static void usb_host_class_initfn(ObjectClass
*klass
, void *data
)
1583 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1584 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1586 uc
->realize
= usb_host_realize
;
1587 uc
->product_desc
= "USB Host Device";
1588 uc
->cancel_packet
= usb_host_cancel_packet
;
1589 uc
->handle_data
= usb_host_handle_data
;
1590 uc
->handle_control
= usb_host_handle_control
;
1591 uc
->handle_reset
= usb_host_handle_reset
;
1592 uc
->unrealize
= usb_host_unrealize
;
1593 uc
->flush_ep_queue
= usb_host_flush_ep_queue
;
1594 uc
->alloc_streams
= usb_host_alloc_streams
;
1595 uc
->free_streams
= usb_host_free_streams
;
1596 dc
->vmsd
= &vmstate_usb_host
;
1597 dc
->props
= usb_host_dev_properties
;
1598 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
1601 static TypeInfo usb_host_dev_info
= {
1602 .name
= TYPE_USB_HOST_DEVICE
,
1603 .parent
= TYPE_USB_DEVICE
,
1604 .instance_size
= sizeof(USBHostDevice
),
1605 .class_init
= usb_host_class_initfn
,
1606 .instance_init
= usb_host_instance_init
,
1609 static void usb_host_register_types(void)
1611 type_register_static(&usb_host_dev_info
);
1614 type_init(usb_host_register_types
)
1616 /* ------------------------------------------------------------------------ */
1618 static QEMUTimer
*usb_auto_timer
;
1619 static VMChangeStateEntry
*usb_vmstate
;
1621 static void usb_host_vm_state(void *unused
, int running
, RunState state
)
1624 usb_host_auto_check(unused
);
1628 static void usb_host_auto_check(void *unused
)
1630 struct USBHostDevice
*s
;
1631 struct USBAutoFilter
*f
;
1632 libusb_device
**devs
= NULL
;
1633 struct libusb_device_descriptor ddesc
;
1634 int unconnected
= 0;
1637 if (usb_host_init() != 0) {
1641 if (runstate_is_running()) {
1642 n
= libusb_get_device_list(ctx
, &devs
);
1643 for (i
= 0; i
< n
; i
++) {
1644 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1647 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1650 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1652 if (f
->bus_num
> 0 &&
1653 f
->bus_num
!= libusb_get_bus_number(devs
[i
])) {
1657 f
->addr
!= libusb_get_device_address(devs
[i
])) {
1660 if (f
->port
!= NULL
) {
1661 char port
[16] = "-";
1662 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1663 if (strcmp(f
->port
, port
) != 0) {
1667 if (f
->vendor_id
> 0 &&
1668 f
->vendor_id
!= ddesc
.idVendor
) {
1671 if (f
->product_id
> 0 &&
1672 f
->product_id
!= ddesc
.idProduct
) {
1676 /* We got a match */
1678 if (s
->errcount
>= 3) {
1681 if (s
->dh
!= NULL
) {
1684 if (usb_host_open(s
, devs
[i
]) < 0) {
1691 libusb_free_device_list(devs
, 1);
1693 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1694 if (s
->dh
== NULL
) {
1707 if (unconnected
== 0) {
1708 /* nothing to watch */
1709 if (usb_auto_timer
) {
1710 timer_del(usb_auto_timer
);
1711 trace_usb_host_auto_scan_disabled();
1719 usb_vmstate
= qemu_add_vm_change_state_handler(usb_host_vm_state
, NULL
);
1721 if (!usb_auto_timer
) {
1722 usb_auto_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, usb_host_auto_check
, NULL
);
1723 if (!usb_auto_timer
) {
1726 trace_usb_host_auto_scan_enabled();
1728 timer_mod(usb_auto_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 2000);
1732 * Check whether USB host device has a USB mass storage SCSI interface
1734 bool usb_host_dev_is_scsi_storage(USBDevice
*ud
)
1736 USBHostDevice
*uhd
= USB_HOST_DEVICE(ud
);
1737 struct libusb_config_descriptor
*conf
;
1738 const struct libusb_interface_descriptor
*intf
;
1739 bool is_scsi_storage
= false;
1742 if (!uhd
|| libusb_get_active_config_descriptor(uhd
->dev
, &conf
) != 0) {
1746 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
1747 intf
= &conf
->interface
[i
].altsetting
[ud
->altsetting
[i
]];
1748 if (intf
->bInterfaceClass
== LIBUSB_CLASS_MASS_STORAGE
&&
1749 intf
->bInterfaceSubClass
== 6) { /* 6 means SCSI */
1750 is_scsi_storage
= true;
1755 libusb_free_config_descriptor(conf
);
1757 return is_scsi_storage
;
1760 void hmp_info_usbhost(Monitor
*mon
, const QDict
*qdict
)
1762 libusb_device
**devs
= NULL
;
1763 struct libusb_device_descriptor ddesc
;
1767 if (usb_host_init() != 0) {
1771 n
= libusb_get_device_list(ctx
, &devs
);
1772 for (i
= 0; i
< n
; i
++) {
1773 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1776 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1779 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1780 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1781 libusb_get_bus_number(devs
[i
]),
1782 libusb_get_device_address(devs
[i
]),
1784 speed_name
[libusb_get_device_speed(devs
[i
])]);
1785 monitor_printf(mon
, " Class %02x:", ddesc
.bDeviceClass
);
1786 monitor_printf(mon
, " USB device %04x:%04x",
1787 ddesc
.idVendor
, ddesc
.idProduct
);
1788 if (ddesc
.iProduct
) {
1789 libusb_device_handle
*handle
;
1790 if (libusb_open(devs
[i
], &handle
) == 0) {
1791 unsigned char name
[64] = "";
1792 libusb_get_string_descriptor_ascii(handle
,
1794 name
, sizeof(name
));
1795 libusb_close(handle
);
1796 monitor_printf(mon
, ", %s", name
);
1799 monitor_printf(mon
, "\n");
1801 libusb_free_device_list(devs
, 1);