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 #if LIBUSBX_API_VERSION >= 0x01000103
163 # define HAVE_STREAMS 1
166 static const char *speed_name
[] = {
167 [LIBUSB_SPEED_UNKNOWN
] = "?",
168 [LIBUSB_SPEED_LOW
] = "1.5",
169 [LIBUSB_SPEED_FULL
] = "12",
170 [LIBUSB_SPEED_HIGH
] = "480",
171 [LIBUSB_SPEED_SUPER
] = "5000",
174 static const unsigned int speed_map
[] = {
175 [LIBUSB_SPEED_LOW
] = USB_SPEED_LOW
,
176 [LIBUSB_SPEED_FULL
] = USB_SPEED_FULL
,
177 [LIBUSB_SPEED_HIGH
] = USB_SPEED_HIGH
,
178 [LIBUSB_SPEED_SUPER
] = USB_SPEED_SUPER
,
181 static const unsigned int status_map
[] = {
182 [LIBUSB_TRANSFER_COMPLETED
] = USB_RET_SUCCESS
,
183 [LIBUSB_TRANSFER_ERROR
] = USB_RET_IOERROR
,
184 [LIBUSB_TRANSFER_TIMED_OUT
] = USB_RET_IOERROR
,
185 [LIBUSB_TRANSFER_CANCELLED
] = USB_RET_IOERROR
,
186 [LIBUSB_TRANSFER_STALL
] = USB_RET_STALL
,
187 [LIBUSB_TRANSFER_NO_DEVICE
] = USB_RET_NODEV
,
188 [LIBUSB_TRANSFER_OVERFLOW
] = USB_RET_BABBLE
,
191 static const char *err_names
[] = {
192 [-LIBUSB_ERROR_IO
] = "IO",
193 [-LIBUSB_ERROR_INVALID_PARAM
] = "INVALID_PARAM",
194 [-LIBUSB_ERROR_ACCESS
] = "ACCESS",
195 [-LIBUSB_ERROR_NO_DEVICE
] = "NO_DEVICE",
196 [-LIBUSB_ERROR_NOT_FOUND
] = "NOT_FOUND",
197 [-LIBUSB_ERROR_BUSY
] = "BUSY",
198 [-LIBUSB_ERROR_TIMEOUT
] = "TIMEOUT",
199 [-LIBUSB_ERROR_OVERFLOW
] = "OVERFLOW",
200 [-LIBUSB_ERROR_PIPE
] = "PIPE",
201 [-LIBUSB_ERROR_INTERRUPTED
] = "INTERRUPTED",
202 [-LIBUSB_ERROR_NO_MEM
] = "NO_MEM",
203 [-LIBUSB_ERROR_NOT_SUPPORTED
] = "NOT_SUPPORTED",
204 [-LIBUSB_ERROR_OTHER
] = "OTHER",
207 static libusb_context
*ctx
;
208 static uint32_t loglevel
;
212 static void usb_host_handle_fd(void *opaque
)
214 struct timeval tv
= { 0, 0 };
215 libusb_handle_events_timeout(ctx
, &tv
);
218 static void usb_host_add_fd(int fd
, short events
, void *user_data
)
220 qemu_set_fd_handler(fd
,
221 (events
& POLLIN
) ? usb_host_handle_fd
: NULL
,
222 (events
& POLLOUT
) ? usb_host_handle_fd
: NULL
,
226 static void usb_host_del_fd(int fd
, void *user_data
)
228 qemu_set_fd_handler(fd
, NULL
, NULL
, NULL
);
231 #endif /* !CONFIG_WIN32 */
233 static int usb_host_init(void)
236 const struct libusb_pollfd
**poll
;
243 rc
= libusb_init(&ctx
);
247 libusb_set_debug(ctx
, loglevel
);
249 /* FIXME: add support for Windows. */
251 libusb_set_pollfd_notifiers(ctx
, usb_host_add_fd
,
254 poll
= libusb_get_pollfds(ctx
);
257 for (i
= 0; poll
[i
] != NULL
; i
++) {
258 usb_host_add_fd(poll
[i
]->fd
, poll
[i
]->events
, ctx
);
266 static int usb_host_get_port(libusb_device
*dev
, char *port
, size_t len
)
272 #if LIBUSBX_API_VERSION >= 0x01000102
273 rc
= libusb_get_port_numbers(dev
, path
, 7);
275 rc
= libusb_get_port_path(ctx
, dev
, path
, 7);
280 off
= snprintf(port
, len
, "%d", path
[0]);
281 for (i
= 1; i
< rc
; i
++) {
282 off
+= snprintf(port
+off
, len
-off
, ".%d", path
[i
]);
287 static void usb_host_libusb_error(const char *func
, int rc
)
295 if (-rc
< ARRAY_SIZE(err_names
) && err_names
[-rc
]) {
296 errname
= err_names
[-rc
];
300 error_report("%s: %d [%s]", func
, rc
, errname
);
303 /* ------------------------------------------------------------------------ */
305 static bool usb_host_use_combining(USBEndpoint
*ep
)
312 if (ep
->pid
!= USB_TOKEN_IN
) {
315 type
= usb_ep_get_type(ep
->dev
, ep
->pid
, ep
->nr
);
316 if (type
!= USB_ENDPOINT_XFER_BULK
) {
322 /* ------------------------------------------------------------------------ */
324 static USBHostRequest
*usb_host_req_alloc(USBHostDevice
*s
, USBPacket
*p
,
325 bool in
, size_t bufsize
)
327 USBHostRequest
*r
= g_new0(USBHostRequest
, 1);
332 r
->xfer
= libusb_alloc_transfer(0);
334 r
->buffer
= g_malloc(bufsize
);
336 QTAILQ_INSERT_TAIL(&s
->requests
, r
, next
);
340 static void usb_host_req_free(USBHostRequest
*r
)
343 QTAILQ_REMOVE(&r
->host
->requests
, r
, next
);
345 libusb_free_transfer(r
->xfer
);
350 static USBHostRequest
*usb_host_req_find(USBHostDevice
*s
, USBPacket
*p
)
354 QTAILQ_FOREACH(r
, &s
->requests
, next
) {
362 static void LIBUSB_CALL
usb_host_req_complete_ctrl(struct libusb_transfer
*xfer
)
364 USBHostRequest
*r
= xfer
->user_data
;
365 USBHostDevice
*s
= r
->host
;
366 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
369 goto out
; /* request was canceled */
372 r
->p
->status
= status_map
[xfer
->status
];
373 r
->p
->actual_length
= xfer
->actual_length
;
374 if (r
->in
&& xfer
->actual_length
) {
375 memcpy(r
->cbuf
, r
->buffer
+ 8, xfer
->actual_length
);
377 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
378 * to work redirected to a not superspeed capable hcd */
379 if (r
->usb3ep0quirk
&& xfer
->actual_length
>= 18 &&
384 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
385 r
->p
->status
, r
->p
->actual_length
);
386 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
389 usb_host_req_free(r
);
395 static void LIBUSB_CALL
usb_host_req_complete_data(struct libusb_transfer
*xfer
)
397 USBHostRequest
*r
= xfer
->user_data
;
398 USBHostDevice
*s
= r
->host
;
399 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
402 goto out
; /* request was canceled */
405 r
->p
->status
= status_map
[xfer
->status
];
406 if (r
->in
&& xfer
->actual_length
) {
407 usb_packet_copy(r
->p
, r
->buffer
, xfer
->actual_length
);
409 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
410 r
->p
->status
, r
->p
->actual_length
);
411 if (usb_host_use_combining(r
->p
->ep
)) {
412 usb_combined_input_packet_complete(USB_DEVICE(s
), r
->p
);
414 usb_packet_complete(USB_DEVICE(s
), r
->p
);
418 usb_host_req_free(r
);
424 static void usb_host_req_abort(USBHostRequest
*r
)
426 USBHostDevice
*s
= r
->host
;
427 bool inflight
= (r
->p
&& r
->p
->state
== USB_PACKET_ASYNC
);
430 r
->p
->status
= USB_RET_NODEV
;
431 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
432 r
->p
->status
, r
->p
->actual_length
);
433 if (r
->p
->ep
->nr
== 0) {
434 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
436 usb_packet_complete(USB_DEVICE(s
), r
->p
);
441 QTAILQ_REMOVE(&r
->host
->requests
, r
, next
);
445 libusb_cancel_transfer(r
->xfer
);
449 /* ------------------------------------------------------------------------ */
451 static void LIBUSB_CALL
452 usb_host_req_complete_iso(struct libusb_transfer
*transfer
)
454 USBHostIsoXfer
*xfer
= transfer
->user_data
;
457 /* USBHostIsoXfer released while inflight */
458 g_free(transfer
->buffer
);
459 libusb_free_transfer(transfer
);
463 QTAILQ_REMOVE(&xfer
->ring
->inflight
, xfer
, next
);
464 if (QTAILQ_EMPTY(&xfer
->ring
->inflight
)) {
465 USBHostDevice
*s
= xfer
->ring
->host
;
466 trace_usb_host_iso_stop(s
->bus_num
, s
->addr
, xfer
->ring
->ep
->nr
);
468 if (xfer
->ring
->ep
->pid
== USB_TOKEN_IN
) {
469 QTAILQ_INSERT_TAIL(&xfer
->ring
->copy
, xfer
, next
);
470 usb_wakeup(xfer
->ring
->ep
, 0);
472 QTAILQ_INSERT_TAIL(&xfer
->ring
->unused
, xfer
, next
);
476 static USBHostIsoRing
*usb_host_iso_alloc(USBHostDevice
*s
, USBEndpoint
*ep
)
478 USBHostIsoRing
*ring
= g_new0(USBHostIsoRing
, 1);
479 USBHostIsoXfer
*xfer
;
480 /* FIXME: check interval (for now assume one xfer per frame) */
481 int packets
= s
->iso_urb_frames
;
486 QTAILQ_INIT(&ring
->unused
);
487 QTAILQ_INIT(&ring
->inflight
);
488 QTAILQ_INIT(&ring
->copy
);
489 QTAILQ_INSERT_TAIL(&s
->isorings
, ring
, next
);
491 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
492 xfer
= g_new0(USBHostIsoXfer
, 1);
494 xfer
->xfer
= libusb_alloc_transfer(packets
);
495 xfer
->xfer
->dev_handle
= s
->dh
;
496 xfer
->xfer
->type
= LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
;
498 xfer
->xfer
->endpoint
= ring
->ep
->nr
;
499 if (ring
->ep
->pid
== USB_TOKEN_IN
) {
500 xfer
->xfer
->endpoint
|= USB_DIR_IN
;
502 xfer
->xfer
->callback
= usb_host_req_complete_iso
;
503 xfer
->xfer
->user_data
= xfer
;
505 xfer
->xfer
->num_iso_packets
= packets
;
506 xfer
->xfer
->length
= ring
->ep
->max_packet_size
* packets
;
507 xfer
->xfer
->buffer
= g_malloc0(xfer
->xfer
->length
);
509 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
515 static USBHostIsoRing
*usb_host_iso_find(USBHostDevice
*s
, USBEndpoint
*ep
)
517 USBHostIsoRing
*ring
;
519 QTAILQ_FOREACH(ring
, &s
->isorings
, next
) {
520 if (ring
->ep
== ep
) {
527 static void usb_host_iso_reset_xfer(USBHostIsoXfer
*xfer
)
529 libusb_set_iso_packet_lengths(xfer
->xfer
,
530 xfer
->ring
->ep
->max_packet_size
);
532 xfer
->copy_complete
= false;
535 static void usb_host_iso_free_xfer(USBHostIsoXfer
*xfer
, bool inflight
)
538 xfer
->xfer
->user_data
= NULL
;
540 g_free(xfer
->xfer
->buffer
);
541 libusb_free_transfer(xfer
->xfer
);
546 static void usb_host_iso_free(USBHostIsoRing
*ring
)
548 USBHostIsoXfer
*xfer
;
550 while ((xfer
= QTAILQ_FIRST(&ring
->inflight
)) != NULL
) {
551 QTAILQ_REMOVE(&ring
->inflight
, xfer
, next
);
552 usb_host_iso_free_xfer(xfer
, true);
554 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
555 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
556 usb_host_iso_free_xfer(xfer
, false);
558 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
) {
559 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
560 usb_host_iso_free_xfer(xfer
, false);
563 QTAILQ_REMOVE(&ring
->host
->isorings
, ring
, next
);
567 static void usb_host_iso_free_all(USBHostDevice
*s
)
569 USBHostIsoRing
*ring
;
571 while ((ring
= QTAILQ_FIRST(&s
->isorings
)) != NULL
) {
572 usb_host_iso_free(ring
);
576 static bool usb_host_iso_data_copy(USBHostIsoXfer
*xfer
, USBPacket
*p
)
581 buf
= libusb_get_iso_packet_buffer_simple(xfer
->xfer
, xfer
->packet
);
582 if (p
->pid
== USB_TOKEN_OUT
) {
584 if (psize
> xfer
->ring
->ep
->max_packet_size
) {
585 /* should not happen (guest bug) */
586 psize
= xfer
->ring
->ep
->max_packet_size
;
588 xfer
->xfer
->iso_packet_desc
[xfer
->packet
].length
= psize
;
590 psize
= xfer
->xfer
->iso_packet_desc
[xfer
->packet
].actual_length
;
591 if (psize
> p
->iov
.size
) {
592 /* should not happen (guest bug) */
596 usb_packet_copy(p
, buf
, psize
);
598 xfer
->copy_complete
= (xfer
->packet
== xfer
->xfer
->num_iso_packets
);
599 return xfer
->copy_complete
;
602 static void usb_host_iso_data_in(USBHostDevice
*s
, USBPacket
*p
)
604 USBHostIsoRing
*ring
;
605 USBHostIsoXfer
*xfer
;
606 bool disconnect
= false;
609 ring
= usb_host_iso_find(s
, p
->ep
);
611 ring
= usb_host_iso_alloc(s
, p
->ep
);
614 /* copy data to guest */
615 xfer
= QTAILQ_FIRST(&ring
->copy
);
617 if (usb_host_iso_data_copy(xfer
, p
)) {
618 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
619 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
623 /* submit empty bufs to host */
624 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
625 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
626 usb_host_iso_reset_xfer(xfer
);
627 rc
= libusb_submit_transfer(xfer
->xfer
);
629 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
630 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
631 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
636 if (QTAILQ_EMPTY(&ring
->inflight
)) {
637 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
639 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
647 static void usb_host_iso_data_out(USBHostDevice
*s
, USBPacket
*p
)
649 USBHostIsoRing
*ring
;
650 USBHostIsoXfer
*xfer
;
651 bool disconnect
= false;
654 ring
= usb_host_iso_find(s
, p
->ep
);
656 ring
= usb_host_iso_alloc(s
, p
->ep
);
659 /* copy data from guest */
660 xfer
= QTAILQ_FIRST(&ring
->copy
);
661 while (xfer
!= NULL
&& xfer
->copy_complete
) {
663 xfer
= QTAILQ_NEXT(xfer
, next
);
666 xfer
= QTAILQ_FIRST(&ring
->unused
);
668 trace_usb_host_iso_out_of_bufs(s
->bus_num
, s
->addr
, p
->ep
->nr
);
671 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
672 usb_host_iso_reset_xfer(xfer
);
673 QTAILQ_INSERT_TAIL(&ring
->copy
, xfer
, next
);
675 usb_host_iso_data_copy(xfer
, p
);
677 if (QTAILQ_EMPTY(&ring
->inflight
)) {
678 /* wait until half of our buffers are filled
679 before kicking the iso out stream */
680 if (filled
*2 < s
->iso_urb_count
) {
685 /* submit filled bufs to host */
686 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
&&
687 xfer
->copy_complete
) {
688 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
689 rc
= libusb_submit_transfer(xfer
->xfer
);
691 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
692 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
693 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
698 if (QTAILQ_EMPTY(&ring
->inflight
)) {
699 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
701 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
709 /* ------------------------------------------------------------------------ */
711 static void usb_host_speed_compat(USBHostDevice
*s
)
713 USBDevice
*udev
= USB_DEVICE(s
);
714 struct libusb_config_descriptor
*conf
;
715 const struct libusb_interface_descriptor
*intf
;
716 const struct libusb_endpoint_descriptor
*endp
;
718 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
720 bool compat_high
= true;
721 bool compat_full
= true;
726 rc
= libusb_get_config_descriptor(s
->dev
, c
, &conf
);
730 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
731 for (a
= 0; a
< conf
->interface
[i
].num_altsetting
; a
++) {
732 intf
= &conf
->interface
[i
].altsetting
[a
];
733 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
734 endp
= &intf
->endpoint
[e
];
735 type
= endp
->bmAttributes
& 0x3;
741 case 0x02: /* BULK */
743 rc
= libusb_get_ss_endpoint_companion_descriptor
744 (ctx
, endp
, &endp_ss_comp
);
745 if (rc
== LIBUSB_SUCCESS
) {
746 libusb_free_ss_endpoint_companion_descriptor
753 case 0x03: /* INTERRUPT */
754 if (endp
->wMaxPacketSize
> 64) {
757 if (endp
->wMaxPacketSize
> 1024) {
765 libusb_free_config_descriptor(conf
);
768 udev
->speedmask
= (1 << udev
->speed
);
769 if (udev
->speed
== USB_SPEED_SUPER
&& compat_high
) {
770 udev
->speedmask
|= USB_SPEED_MASK_HIGH
;
772 if (udev
->speed
== USB_SPEED_SUPER
&& compat_full
) {
773 udev
->speedmask
|= USB_SPEED_MASK_FULL
;
775 if (udev
->speed
== USB_SPEED_HIGH
&& compat_full
) {
776 udev
->speedmask
|= USB_SPEED_MASK_FULL
;
780 static void usb_host_ep_update(USBHostDevice
*s
)
782 static const char *tname
[] = {
783 [USB_ENDPOINT_XFER_CONTROL
] = "control",
784 [USB_ENDPOINT_XFER_ISOC
] = "isoc",
785 [USB_ENDPOINT_XFER_BULK
] = "bulk",
786 [USB_ENDPOINT_XFER_INT
] = "int",
788 USBDevice
*udev
= USB_DEVICE(s
);
789 struct libusb_config_descriptor
*conf
;
790 const struct libusb_interface_descriptor
*intf
;
791 const struct libusb_endpoint_descriptor
*endp
;
793 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
800 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
804 trace_usb_host_parse_config(s
->bus_num
, s
->addr
,
805 conf
->bConfigurationValue
, true);
807 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
808 assert(udev
->altsetting
[i
] < conf
->interface
[i
].num_altsetting
);
809 intf
= &conf
->interface
[i
].altsetting
[udev
->altsetting
[i
]];
810 trace_usb_host_parse_interface(s
->bus_num
, s
->addr
,
811 intf
->bInterfaceNumber
,
812 intf
->bAlternateSetting
, true);
813 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
814 endp
= &intf
->endpoint
[e
];
816 devep
= endp
->bEndpointAddress
;
817 pid
= (devep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
819 type
= endp
->bmAttributes
& 0x3;
822 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
823 "invalid endpoint address");
826 if (usb_ep_get_type(udev
, pid
, ep
) != USB_ENDPOINT_XFER_INVALID
) {
827 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
828 "duplicate endpoint address");
832 trace_usb_host_parse_endpoint(s
->bus_num
, s
->addr
, ep
,
833 (devep
& USB_DIR_IN
) ? "in" : "out",
835 usb_ep_set_max_packet_size(udev
, pid
, ep
,
836 endp
->wMaxPacketSize
);
837 usb_ep_set_type(udev
, pid
, ep
, type
);
838 usb_ep_set_ifnum(udev
, pid
, ep
, i
);
839 usb_ep_set_halted(udev
, pid
, ep
, 0);
841 if (type
== LIBUSB_TRANSFER_TYPE_BULK
&&
842 libusb_get_ss_endpoint_companion_descriptor(ctx
, endp
,
843 &endp_ss_comp
) == LIBUSB_SUCCESS
) {
844 usb_ep_set_max_streams(udev
, pid
, ep
,
845 endp_ss_comp
->bmAttributes
);
846 libusb_free_ss_endpoint_companion_descriptor(endp_ss_comp
);
852 libusb_free_config_descriptor(conf
);
855 static int usb_host_open(USBHostDevice
*s
, libusb_device
*dev
)
857 USBDevice
*udev
= USB_DEVICE(s
);
858 int bus_num
= libusb_get_bus_number(dev
);
859 int addr
= libusb_get_device_address(dev
);
861 Error
*local_err
= NULL
;
863 trace_usb_host_open_started(bus_num
, addr
);
868 rc
= libusb_open(dev
, &s
->dh
);
874 s
->bus_num
= bus_num
;
877 usb_host_detach_kernel(s
);
879 libusb_get_device_descriptor(dev
, &s
->ddesc
);
880 usb_host_get_port(s
->dev
, s
->port
, sizeof(s
->port
));
883 usb_host_ep_update(s
);
885 udev
->speed
= speed_map
[libusb_get_device_speed(dev
)];
886 usb_host_speed_compat(s
);
888 if (s
->ddesc
.iProduct
) {
889 libusb_get_string_descriptor_ascii(s
->dh
, s
->ddesc
.iProduct
,
890 (unsigned char *)udev
->product_desc
,
891 sizeof(udev
->product_desc
));
893 snprintf(udev
->product_desc
, sizeof(udev
->product_desc
),
894 "host:%d.%d", bus_num
, addr
);
897 usb_device_attach(udev
, &local_err
);
899 error_report_err(local_err
);
903 trace_usb_host_open_success(bus_num
, addr
);
907 trace_usb_host_open_failure(bus_num
, addr
);
909 usb_host_release_interfaces(s
);
910 libusb_reset_device(s
->dh
);
911 usb_host_attach_kernel(s
);
919 static void usb_host_abort_xfers(USBHostDevice
*s
)
921 USBHostRequest
*r
, *rtmp
;
923 QTAILQ_FOREACH_SAFE(r
, &s
->requests
, next
, rtmp
) {
924 usb_host_req_abort(r
);
928 static int usb_host_close(USBHostDevice
*s
)
930 USBDevice
*udev
= USB_DEVICE(s
);
936 trace_usb_host_close(s
->bus_num
, s
->addr
);
938 usb_host_abort_xfers(s
);
939 usb_host_iso_free_all(s
);
941 if (udev
->attached
) {
942 usb_device_detach(udev
);
945 usb_host_release_interfaces(s
);
946 libusb_reset_device(s
->dh
);
947 usb_host_attach_kernel(s
);
952 usb_host_auto_check(NULL
);
956 static void usb_host_nodev_bh(void *opaque
)
958 USBHostDevice
*s
= opaque
;
962 static void usb_host_nodev(USBHostDevice
*s
)
965 s
->bh_nodev
= qemu_bh_new(usb_host_nodev_bh
, s
);
967 qemu_bh_schedule(s
->bh_nodev
);
970 static void usb_host_exit_notifier(struct Notifier
*n
, void *data
)
972 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
975 usb_host_release_interfaces(s
);
976 usb_host_attach_kernel(s
);
980 static libusb_device
*usb_host_find_ref(int bus
, int addr
)
982 libusb_device
**devs
= NULL
;
983 libusb_device
*ret
= NULL
;
986 if (usb_host_init() != 0) {
989 n
= libusb_get_device_list(ctx
, &devs
);
990 for (i
= 0; i
< n
; i
++) {
991 if (libusb_get_bus_number(devs
[i
]) == bus
&&
992 libusb_get_device_address(devs
[i
]) == addr
) {
993 ret
= libusb_ref_device(devs
[i
]);
997 libusb_free_device_list(devs
, 1);
1001 static void usb_host_realize(USBDevice
*udev
, Error
**errp
)
1003 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1004 libusb_device
*ldev
;
1007 if (s
->match
.vendor_id
> 0xffff) {
1008 error_setg(errp
, "vendorid out of range");
1011 if (s
->match
.product_id
> 0xffff) {
1012 error_setg(errp
, "productid out of range");
1015 if (s
->match
.addr
> 127) {
1016 error_setg(errp
, "hostaddr out of range");
1020 loglevel
= s
->loglevel
;
1021 udev
->flags
|= (1 << USB_DEV_FLAG_IS_HOST
);
1022 udev
->auto_attach
= 0;
1023 QTAILQ_INIT(&s
->requests
);
1024 QTAILQ_INIT(&s
->isorings
);
1026 if (s
->match
.addr
&& s
->match
.bus_num
&&
1027 !s
->match
.vendor_id
&&
1028 !s
->match
.product_id
&&
1030 s
->needs_autoscan
= false;
1031 ldev
= usb_host_find_ref(s
->match
.bus_num
,
1034 error_setg(errp
, "failed to find host usb device %d:%d",
1035 s
->match
.bus_num
, s
->match
.addr
);
1038 rc
= usb_host_open(s
, ldev
);
1039 libusb_unref_device(ldev
);
1041 error_setg(errp
, "failed to open host usb device %d:%d",
1042 s
->match
.bus_num
, s
->match
.addr
);
1046 s
->needs_autoscan
= true;
1047 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
1048 usb_host_auto_check(NULL
);
1051 s
->exit
.notify
= usb_host_exit_notifier
;
1052 qemu_add_exit_notifier(&s
->exit
);
1055 static void usb_host_instance_init(Object
*obj
)
1057 USBDevice
*udev
= USB_DEVICE(obj
);
1058 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1060 device_add_bootindex_property(obj
, &s
->bootindex
,
1065 static void usb_host_handle_destroy(USBDevice
*udev
)
1067 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1069 qemu_remove_exit_notifier(&s
->exit
);
1070 if (s
->needs_autoscan
) {
1071 QTAILQ_REMOVE(&hostdevs
, s
, next
);
1076 static void usb_host_cancel_packet(USBDevice
*udev
, USBPacket
*p
)
1078 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1082 usb_combined_packet_cancel(udev
, p
);
1086 trace_usb_host_req_canceled(s
->bus_num
, s
->addr
, p
);
1088 r
= usb_host_req_find(s
, p
);
1090 r
->p
= NULL
; /* mark as dead */
1091 libusb_cancel_transfer(r
->xfer
);
1095 static void usb_host_detach_kernel(USBHostDevice
*s
)
1097 struct libusb_config_descriptor
*conf
;
1100 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1104 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
1105 rc
= libusb_kernel_driver_active(s
->dh
, i
);
1106 usb_host_libusb_error("libusb_kernel_driver_active", rc
);
1110 trace_usb_host_detach_kernel(s
->bus_num
, s
->addr
, i
);
1111 rc
= libusb_detach_kernel_driver(s
->dh
, i
);
1112 usb_host_libusb_error("libusb_detach_kernel_driver", rc
);
1113 s
->ifs
[i
].detached
= true;
1115 libusb_free_config_descriptor(conf
);
1118 static void usb_host_attach_kernel(USBHostDevice
*s
)
1120 struct libusb_config_descriptor
*conf
;
1123 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1127 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
1128 if (!s
->ifs
[i
].detached
) {
1131 trace_usb_host_attach_kernel(s
->bus_num
, s
->addr
, i
);
1132 libusb_attach_kernel_driver(s
->dh
, i
);
1133 s
->ifs
[i
].detached
= false;
1135 libusb_free_config_descriptor(conf
);
1138 static int usb_host_claim_interfaces(USBHostDevice
*s
, int configuration
)
1140 USBDevice
*udev
= USB_DEVICE(s
);
1141 struct libusb_config_descriptor
*conf
;
1144 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1145 udev
->altsetting
[i
] = 0;
1147 udev
->ninterfaces
= 0;
1148 udev
->configuration
= 0;
1150 usb_host_detach_kernel(s
);
1152 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1154 if (rc
== LIBUSB_ERROR_NOT_FOUND
) {
1155 /* address state - ignore */
1156 return USB_RET_SUCCESS
;
1158 return USB_RET_STALL
;
1161 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
1162 trace_usb_host_claim_interface(s
->bus_num
, s
->addr
, configuration
, i
);
1163 rc
= libusb_claim_interface(s
->dh
, i
);
1164 usb_host_libusb_error("libusb_claim_interface", rc
);
1166 return USB_RET_STALL
;
1168 s
->ifs
[i
].claimed
= true;
1171 udev
->ninterfaces
= conf
->bNumInterfaces
;
1172 udev
->configuration
= configuration
;
1174 libusb_free_config_descriptor(conf
);
1175 return USB_RET_SUCCESS
;
1178 static void usb_host_release_interfaces(USBHostDevice
*s
)
1180 USBDevice
*udev
= USB_DEVICE(s
);
1183 for (i
= 0; i
< udev
->ninterfaces
; i
++) {
1184 if (!s
->ifs
[i
].claimed
) {
1187 trace_usb_host_release_interface(s
->bus_num
, s
->addr
, i
);
1188 rc
= libusb_release_interface(s
->dh
, i
);
1189 usb_host_libusb_error("libusb_release_interface", rc
);
1190 s
->ifs
[i
].claimed
= false;
1194 static void usb_host_set_address(USBHostDevice
*s
, int addr
)
1196 USBDevice
*udev
= USB_DEVICE(s
);
1198 trace_usb_host_set_address(s
->bus_num
, s
->addr
, addr
);
1202 static void usb_host_set_config(USBHostDevice
*s
, int config
, USBPacket
*p
)
1206 trace_usb_host_set_config(s
->bus_num
, s
->addr
, config
);
1208 usb_host_release_interfaces(s
);
1209 rc
= libusb_set_configuration(s
->dh
, config
);
1211 usb_host_libusb_error("libusb_set_configuration", rc
);
1212 p
->status
= USB_RET_STALL
;
1213 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1218 p
->status
= usb_host_claim_interfaces(s
, config
);
1219 if (p
->status
!= USB_RET_SUCCESS
) {
1222 usb_host_ep_update(s
);
1225 static void usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
,
1228 USBDevice
*udev
= USB_DEVICE(s
);
1231 trace_usb_host_set_interface(s
->bus_num
, s
->addr
, iface
, alt
);
1233 usb_host_iso_free_all(s
);
1235 if (iface
>= USB_MAX_INTERFACES
) {
1236 p
->status
= USB_RET_STALL
;
1240 rc
= libusb_set_interface_alt_setting(s
->dh
, iface
, alt
);
1242 usb_host_libusb_error("libusb_set_interface_alt_setting", rc
);
1243 p
->status
= USB_RET_STALL
;
1244 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1250 udev
->altsetting
[iface
] = alt
;
1251 usb_host_ep_update(s
);
1254 static void usb_host_handle_control(USBDevice
*udev
, USBPacket
*p
,
1255 int request
, int value
, int index
,
1256 int length
, uint8_t *data
)
1258 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1262 trace_usb_host_req_control(s
->bus_num
, s
->addr
, p
, request
, value
, index
);
1264 if (s
->dh
== NULL
) {
1265 p
->status
= USB_RET_NODEV
;
1266 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1271 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1272 usb_host_set_address(s
, value
);
1273 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1276 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1277 usb_host_set_config(s
, value
& 0xff, p
);
1278 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1281 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1282 usb_host_set_interface(s
, index
, value
, p
);
1283 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1286 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
1287 if (value
== 0) { /* clear halt */
1288 int pid
= (index
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1289 libusb_clear_halt(s
->dh
, index
);
1290 usb_ep_set_halted(udev
, pid
, index
& 0x0f, 0);
1291 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1296 r
= usb_host_req_alloc(s
, p
, (request
>> 8) & USB_DIR_IN
, length
+ 8);
1299 memcpy(r
->buffer
, udev
->setup_buf
, 8);
1301 memcpy(r
->buffer
+ 8, r
->cbuf
, r
->clen
);
1304 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
1305 * to work redirected to a not superspeed capable hcd */
1306 if ((udev
->speedmask
& USB_SPEED_MASK_SUPER
) &&
1307 !(udev
->port
->speedmask
& USB_SPEED_MASK_SUPER
) &&
1308 request
== 0x8006 && value
== 0x100 && index
== 0) {
1309 r
->usb3ep0quirk
= true;
1312 libusb_fill_control_transfer(r
->xfer
, s
->dh
, r
->buffer
,
1313 usb_host_req_complete_ctrl
, r
,
1315 rc
= libusb_submit_transfer(r
->xfer
);
1317 p
->status
= USB_RET_NODEV
;
1318 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1319 p
->status
, p
->actual_length
);
1320 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1326 p
->status
= USB_RET_ASYNC
;
1329 static void usb_host_handle_data(USBDevice
*udev
, USBPacket
*p
)
1331 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1336 if (usb_host_use_combining(p
->ep
) && p
->state
== USB_PACKET_SETUP
) {
1337 p
->status
= USB_RET_ADD_TO_QUEUE
;
1341 trace_usb_host_req_data(s
->bus_num
, s
->addr
, p
,
1342 p
->pid
== USB_TOKEN_IN
,
1343 p
->ep
->nr
, p
->iov
.size
);
1345 if (s
->dh
== NULL
) {
1346 p
->status
= USB_RET_NODEV
;
1347 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1350 if (p
->ep
->halted
) {
1351 p
->status
= USB_RET_STALL
;
1352 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1356 switch (usb_ep_get_type(udev
, p
->pid
, p
->ep
->nr
)) {
1357 case USB_ENDPOINT_XFER_BULK
:
1358 size
= usb_packet_size(p
);
1359 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, size
);
1361 usb_packet_copy(p
, r
->buffer
, size
);
1363 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1366 libusb_fill_bulk_stream_transfer(r
->xfer
, s
->dh
, ep
, p
->stream
,
1368 usb_host_req_complete_data
, r
,
1371 usb_host_req_free(r
);
1372 p
->status
= USB_RET_STALL
;
1376 libusb_fill_bulk_transfer(r
->xfer
, s
->dh
, ep
,
1378 usb_host_req_complete_data
, r
,
1382 case USB_ENDPOINT_XFER_INT
:
1383 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, p
->iov
.size
);
1385 usb_packet_copy(p
, r
->buffer
, p
->iov
.size
);
1387 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1388 libusb_fill_interrupt_transfer(r
->xfer
, s
->dh
, ep
,
1389 r
->buffer
, p
->iov
.size
,
1390 usb_host_req_complete_data
, r
,
1393 case USB_ENDPOINT_XFER_ISOC
:
1394 if (p
->pid
== USB_TOKEN_IN
) {
1395 usb_host_iso_data_in(s
, p
);
1397 usb_host_iso_data_out(s
, p
);
1399 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1400 p
->status
, p
->actual_length
);
1403 p
->status
= USB_RET_STALL
;
1404 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1405 p
->status
, p
->actual_length
);
1409 rc
= libusb_submit_transfer(r
->xfer
);
1411 p
->status
= USB_RET_NODEV
;
1412 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1413 p
->status
, p
->actual_length
);
1414 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1420 p
->status
= USB_RET_ASYNC
;
1423 static void usb_host_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
1425 if (usb_host_use_combining(ep
)) {
1426 usb_ep_combine_input_packets(ep
);
1430 static void usb_host_handle_reset(USBDevice
*udev
)
1432 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1435 trace_usb_host_reset(s
->bus_num
, s
->addr
);
1437 rc
= libusb_reset_device(s
->dh
);
1443 static int usb_host_alloc_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1444 int nr_eps
, int streams
)
1447 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1448 unsigned char endpoints
[30];
1451 for (i
= 0; i
< nr_eps
; i
++) {
1452 endpoints
[i
] = eps
[i
]->nr
;
1453 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1454 endpoints
[i
] |= 0x80;
1457 rc
= libusb_alloc_streams(s
->dh
, streams
, endpoints
, nr_eps
);
1459 usb_host_libusb_error("libusb_alloc_streams", rc
);
1460 } else if (rc
!= streams
) {
1461 error_report("libusb_alloc_streams: got less streams "
1462 "then requested %d < %d", rc
, streams
);
1465 return (rc
== streams
) ? 0 : -1;
1467 error_report("libusb_alloc_streams: error not implemented");
1472 static void usb_host_free_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1476 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1477 unsigned char endpoints
[30];
1480 for (i
= 0; i
< nr_eps
; i
++) {
1481 endpoints
[i
] = eps
[i
]->nr
;
1482 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1483 endpoints
[i
] |= 0x80;
1486 libusb_free_streams(s
->dh
, endpoints
, nr_eps
);
1491 * This is *NOT* about restoring state. We have absolutely no idea
1492 * what state the host device is in at the moment and whenever it is
1493 * still present in the first place. Attemping to contine where we
1494 * left off is impossible.
1496 * What we are going to do here is emulate a surprise removal of
1497 * the usb device passed through, then kick host scan so the device
1498 * will get re-attached (and re-initialized by the guest) in case it
1501 * As the device removal will change the state of other devices (usb
1502 * host controller, most likely interrupt controller too) we have to
1503 * wait with it until *all* vmstate is loaded. Thus post_load just
1504 * kicks a bottom half which then does the actual work.
1506 static void usb_host_post_load_bh(void *opaque
)
1508 USBHostDevice
*dev
= opaque
;
1509 USBDevice
*udev
= USB_DEVICE(dev
);
1511 if (dev
->dh
!= NULL
) {
1512 usb_host_close(dev
);
1514 if (udev
->attached
) {
1515 usb_device_detach(udev
);
1517 usb_host_auto_check(NULL
);
1520 static int usb_host_post_load(void *opaque
, int version_id
)
1522 USBHostDevice
*dev
= opaque
;
1524 if (!dev
->bh_postld
) {
1525 dev
->bh_postld
= qemu_bh_new(usb_host_post_load_bh
, dev
);
1527 qemu_bh_schedule(dev
->bh_postld
);
1531 static const VMStateDescription vmstate_usb_host
= {
1534 .minimum_version_id
= 1,
1535 .post_load
= usb_host_post_load
,
1536 .fields
= (VMStateField
[]) {
1537 VMSTATE_USB_DEVICE(parent_obj
, USBHostDevice
),
1538 VMSTATE_END_OF_LIST()
1542 static Property usb_host_dev_properties
[] = {
1543 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1544 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1545 DEFINE_PROP_STRING("hostport", USBHostDevice
, match
.port
),
1546 DEFINE_PROP_UINT32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1547 DEFINE_PROP_UINT32("productid", USBHostDevice
, match
.product_id
, 0),
1548 DEFINE_PROP_UINT32("isobufs", USBHostDevice
, iso_urb_count
, 4),
1549 DEFINE_PROP_UINT32("isobsize", USBHostDevice
, iso_urb_frames
, 32),
1550 DEFINE_PROP_UINT32("loglevel", USBHostDevice
, loglevel
,
1551 LIBUSB_LOG_LEVEL_WARNING
),
1552 DEFINE_PROP_BIT("pipeline", USBHostDevice
, options
,
1553 USB_HOST_OPT_PIPELINE
, true),
1554 DEFINE_PROP_END_OF_LIST(),
1557 static void usb_host_class_initfn(ObjectClass
*klass
, void *data
)
1559 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1560 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1562 uc
->realize
= usb_host_realize
;
1563 uc
->product_desc
= "USB Host Device";
1564 uc
->cancel_packet
= usb_host_cancel_packet
;
1565 uc
->handle_data
= usb_host_handle_data
;
1566 uc
->handle_control
= usb_host_handle_control
;
1567 uc
->handle_reset
= usb_host_handle_reset
;
1568 uc
->handle_destroy
= usb_host_handle_destroy
;
1569 uc
->flush_ep_queue
= usb_host_flush_ep_queue
;
1570 uc
->alloc_streams
= usb_host_alloc_streams
;
1571 uc
->free_streams
= usb_host_free_streams
;
1572 dc
->vmsd
= &vmstate_usb_host
;
1573 dc
->props
= usb_host_dev_properties
;
1574 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
1577 static TypeInfo usb_host_dev_info
= {
1578 .name
= TYPE_USB_HOST_DEVICE
,
1579 .parent
= TYPE_USB_DEVICE
,
1580 .instance_size
= sizeof(USBHostDevice
),
1581 .class_init
= usb_host_class_initfn
,
1582 .instance_init
= usb_host_instance_init
,
1585 static void usb_host_register_types(void)
1587 type_register_static(&usb_host_dev_info
);
1590 type_init(usb_host_register_types
)
1592 /* ------------------------------------------------------------------------ */
1594 static QEMUTimer
*usb_auto_timer
;
1595 static VMChangeStateEntry
*usb_vmstate
;
1597 static void usb_host_vm_state(void *unused
, int running
, RunState state
)
1600 usb_host_auto_check(unused
);
1604 static void usb_host_auto_check(void *unused
)
1606 struct USBHostDevice
*s
;
1607 struct USBAutoFilter
*f
;
1608 libusb_device
**devs
= NULL
;
1609 struct libusb_device_descriptor ddesc
;
1610 int unconnected
= 0;
1613 if (usb_host_init() != 0) {
1617 if (runstate_is_running()) {
1618 n
= libusb_get_device_list(ctx
, &devs
);
1619 for (i
= 0; i
< n
; i
++) {
1620 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1623 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1626 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1628 if (f
->bus_num
> 0 &&
1629 f
->bus_num
!= libusb_get_bus_number(devs
[i
])) {
1633 f
->addr
!= libusb_get_device_address(devs
[i
])) {
1636 if (f
->port
!= NULL
) {
1637 char port
[16] = "-";
1638 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1639 if (strcmp(f
->port
, port
) != 0) {
1643 if (f
->vendor_id
> 0 &&
1644 f
->vendor_id
!= ddesc
.idVendor
) {
1647 if (f
->product_id
> 0 &&
1648 f
->product_id
!= ddesc
.idProduct
) {
1652 /* We got a match */
1654 if (s
->errcount
>= 3) {
1657 if (s
->dh
!= NULL
) {
1660 if (usb_host_open(s
, devs
[i
]) < 0) {
1667 libusb_free_device_list(devs
, 1);
1669 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1670 if (s
->dh
== NULL
) {
1683 if (unconnected
== 0) {
1684 /* nothing to watch */
1685 if (usb_auto_timer
) {
1686 timer_del(usb_auto_timer
);
1687 trace_usb_host_auto_scan_disabled();
1695 usb_vmstate
= qemu_add_vm_change_state_handler(usb_host_vm_state
, NULL
);
1697 if (!usb_auto_timer
) {
1698 usb_auto_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, usb_host_auto_check
, NULL
);
1699 if (!usb_auto_timer
) {
1702 trace_usb_host_auto_scan_enabled();
1704 timer_mod(usb_auto_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 2000);
1707 void hmp_info_usbhost(Monitor
*mon
, const QDict
*qdict
)
1709 libusb_device
**devs
= NULL
;
1710 struct libusb_device_descriptor ddesc
;
1714 if (usb_host_init() != 0) {
1718 n
= libusb_get_device_list(ctx
, &devs
);
1719 for (i
= 0; i
< n
; i
++) {
1720 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1723 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1726 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1727 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1728 libusb_get_bus_number(devs
[i
]),
1729 libusb_get_device_address(devs
[i
]),
1731 speed_name
[libusb_get_device_speed(devs
[i
])]);
1732 monitor_printf(mon
, " Class %02x:", ddesc
.bDeviceClass
);
1733 monitor_printf(mon
, " USB device %04x:%04x",
1734 ddesc
.idVendor
, ddesc
.idProduct
);
1735 if (ddesc
.iProduct
) {
1736 libusb_device_handle
*handle
;
1737 if (libusb_open(devs
[i
], &handle
) == 0) {
1738 unsigned char name
[64] = "";
1739 libusb_get_string_descriptor_ascii(handle
,
1741 name
, sizeof(name
));
1742 libusb_close(handle
);
1743 monitor_printf(mon
, ", %s", name
);
1746 monitor_printf(mon
, "\n");
1748 libusb_free_device_list(devs
, 1);