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
39 #include "qemu-common.h"
40 #include "monitor/monitor.h"
41 #include "qemu/error-report.h"
42 #include "sysemu/sysemu.h"
47 /* ------------------------------------------------------------------------ */
49 #define TYPE_USB_HOST_DEVICE "usb-host"
50 #define USB_HOST_DEVICE(obj) \
51 OBJECT_CHECK(USBHostDevice, (obj), TYPE_USB_HOST_DEVICE)
53 typedef struct USBHostDevice USBHostDevice
;
54 typedef struct USBHostRequest USBHostRequest
;
55 typedef struct USBHostIsoXfer USBHostIsoXfer
;
56 typedef struct USBHostIsoRing USBHostIsoRing
;
58 struct USBAutoFilter
{
66 enum USBHostDeviceOptions
{
67 USB_HOST_OPT_PIPELINE
,
70 struct USBHostDevice
{
74 struct USBAutoFilter match
;
76 uint32_t iso_urb_count
;
77 uint32_t iso_urb_frames
;
82 QTAILQ_ENTRY(USBHostDevice
) next
;
89 libusb_device_handle
*dh
;
90 struct libusb_device_descriptor ddesc
;
95 } ifs
[USB_MAX_INTERFACES
];
97 /* callbacks & friends */
103 QTAILQ_HEAD(, USBHostRequest
) requests
;
104 QTAILQ_HEAD(, USBHostIsoRing
) isorings
;
107 struct USBHostRequest
{
111 struct libusb_transfer
*xfer
;
112 unsigned char *buffer
;
116 QTAILQ_ENTRY(USBHostRequest
) next
;
119 struct USBHostIsoXfer
{
120 USBHostIsoRing
*ring
;
121 struct libusb_transfer
*xfer
;
124 QTAILQ_ENTRY(USBHostIsoXfer
) next
;
127 struct USBHostIsoRing
{
130 QTAILQ_HEAD(, USBHostIsoXfer
) unused
;
131 QTAILQ_HEAD(, USBHostIsoXfer
) inflight
;
132 QTAILQ_HEAD(, USBHostIsoXfer
) copy
;
133 QTAILQ_ENTRY(USBHostIsoRing
) next
;
136 static QTAILQ_HEAD(, USBHostDevice
) hostdevs
=
137 QTAILQ_HEAD_INITIALIZER(hostdevs
);
139 static void usb_host_auto_check(void *unused
);
140 static void usb_host_release_interfaces(USBHostDevice
*s
);
141 static void usb_host_nodev(USBHostDevice
*s
);
142 static void usb_host_detach_kernel(USBHostDevice
*s
);
143 static void usb_host_attach_kernel(USBHostDevice
*s
);
145 /* ------------------------------------------------------------------------ */
147 #ifndef LIBUSB_LOG_LEVEL_WARNING /* older libusb didn't define these */
148 #define LIBUSB_LOG_LEVEL_WARNING 2
151 /* ------------------------------------------------------------------------ */
153 #define CONTROL_TIMEOUT 10000 /* 10 sec */
154 #define BULK_TIMEOUT 0 /* unlimited */
155 #define INTR_TIMEOUT 0 /* unlimited */
157 #if LIBUSBX_API_VERSION >= 0x01000103
158 # define HAVE_STREAMS 1
161 static const char *speed_name
[] = {
162 [LIBUSB_SPEED_UNKNOWN
] = "?",
163 [LIBUSB_SPEED_LOW
] = "1.5",
164 [LIBUSB_SPEED_FULL
] = "12",
165 [LIBUSB_SPEED_HIGH
] = "480",
166 [LIBUSB_SPEED_SUPER
] = "5000",
169 static const unsigned int speed_map
[] = {
170 [LIBUSB_SPEED_LOW
] = USB_SPEED_LOW
,
171 [LIBUSB_SPEED_FULL
] = USB_SPEED_FULL
,
172 [LIBUSB_SPEED_HIGH
] = USB_SPEED_HIGH
,
173 [LIBUSB_SPEED_SUPER
] = USB_SPEED_SUPER
,
176 static const unsigned int status_map
[] = {
177 [LIBUSB_TRANSFER_COMPLETED
] = USB_RET_SUCCESS
,
178 [LIBUSB_TRANSFER_ERROR
] = USB_RET_IOERROR
,
179 [LIBUSB_TRANSFER_TIMED_OUT
] = USB_RET_IOERROR
,
180 [LIBUSB_TRANSFER_CANCELLED
] = USB_RET_IOERROR
,
181 [LIBUSB_TRANSFER_STALL
] = USB_RET_STALL
,
182 [LIBUSB_TRANSFER_NO_DEVICE
] = USB_RET_NODEV
,
183 [LIBUSB_TRANSFER_OVERFLOW
] = USB_RET_BABBLE
,
186 static const char *err_names
[] = {
187 [-LIBUSB_ERROR_IO
] = "IO",
188 [-LIBUSB_ERROR_INVALID_PARAM
] = "INVALID_PARAM",
189 [-LIBUSB_ERROR_ACCESS
] = "ACCESS",
190 [-LIBUSB_ERROR_NO_DEVICE
] = "NO_DEVICE",
191 [-LIBUSB_ERROR_NOT_FOUND
] = "NOT_FOUND",
192 [-LIBUSB_ERROR_BUSY
] = "BUSY",
193 [-LIBUSB_ERROR_TIMEOUT
] = "TIMEOUT",
194 [-LIBUSB_ERROR_OVERFLOW
] = "OVERFLOW",
195 [-LIBUSB_ERROR_PIPE
] = "PIPE",
196 [-LIBUSB_ERROR_INTERRUPTED
] = "INTERRUPTED",
197 [-LIBUSB_ERROR_NO_MEM
] = "NO_MEM",
198 [-LIBUSB_ERROR_NOT_SUPPORTED
] = "NOT_SUPPORTED",
199 [-LIBUSB_ERROR_OTHER
] = "OTHER",
202 static libusb_context
*ctx
;
203 static uint32_t loglevel
;
205 static void usb_host_handle_fd(void *opaque
)
207 struct timeval tv
= { 0, 0 };
208 libusb_handle_events_timeout(ctx
, &tv
);
211 static void usb_host_add_fd(int fd
, short events
, void *user_data
)
213 qemu_set_fd_handler(fd
,
214 (events
& POLLIN
) ? usb_host_handle_fd
: NULL
,
215 (events
& POLLOUT
) ? usb_host_handle_fd
: NULL
,
219 static void usb_host_del_fd(int fd
, void *user_data
)
221 qemu_set_fd_handler(fd
, NULL
, NULL
, NULL
);
224 static int usb_host_init(void)
226 const struct libusb_pollfd
**poll
;
232 rc
= libusb_init(&ctx
);
236 libusb_set_debug(ctx
, loglevel
);
238 libusb_set_pollfd_notifiers(ctx
, usb_host_add_fd
,
241 poll
= libusb_get_pollfds(ctx
);
243 for (i
= 0; poll
[i
] != NULL
; i
++) {
244 usb_host_add_fd(poll
[i
]->fd
, poll
[i
]->events
, ctx
);
251 static int usb_host_get_port(libusb_device
*dev
, char *port
, size_t len
)
257 #if LIBUSBX_API_VERSION >= 0x01000102
258 rc
= libusb_get_port_numbers(dev
, path
, 7);
260 rc
= libusb_get_port_path(ctx
, dev
, path
, 7);
265 off
= snprintf(port
, len
, "%d", path
[0]);
266 for (i
= 1; i
< rc
; i
++) {
267 off
+= snprintf(port
+off
, len
-off
, ".%d", path
[i
]);
272 static void usb_host_libusb_error(const char *func
, int rc
)
280 if (-rc
< ARRAY_SIZE(err_names
) && err_names
[-rc
]) {
281 errname
= err_names
[-rc
];
285 error_report("%s: %d [%s]", func
, rc
, errname
);
288 /* ------------------------------------------------------------------------ */
290 static bool usb_host_use_combining(USBEndpoint
*ep
)
297 if (ep
->pid
!= USB_TOKEN_IN
) {
300 type
= usb_ep_get_type(ep
->dev
, ep
->pid
, ep
->nr
);
301 if (type
!= USB_ENDPOINT_XFER_BULK
) {
307 /* ------------------------------------------------------------------------ */
309 static USBHostRequest
*usb_host_req_alloc(USBHostDevice
*s
, USBPacket
*p
,
310 bool in
, size_t bufsize
)
312 USBHostRequest
*r
= g_new0(USBHostRequest
, 1);
317 r
->xfer
= libusb_alloc_transfer(0);
319 r
->buffer
= g_malloc(bufsize
);
321 QTAILQ_INSERT_TAIL(&s
->requests
, r
, next
);
325 static void usb_host_req_free(USBHostRequest
*r
)
328 QTAILQ_REMOVE(&r
->host
->requests
, r
, next
);
330 libusb_free_transfer(r
->xfer
);
335 static USBHostRequest
*usb_host_req_find(USBHostDevice
*s
, USBPacket
*p
)
339 QTAILQ_FOREACH(r
, &s
->requests
, next
) {
347 static void usb_host_req_complete_ctrl(struct libusb_transfer
*xfer
)
349 USBHostRequest
*r
= xfer
->user_data
;
350 USBHostDevice
*s
= r
->host
;
351 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
354 goto out
; /* request was canceled */
357 r
->p
->status
= status_map
[xfer
->status
];
358 r
->p
->actual_length
= xfer
->actual_length
;
359 if (r
->in
&& xfer
->actual_length
) {
360 memcpy(r
->cbuf
, r
->buffer
+ 8, xfer
->actual_length
);
362 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
363 * to work redirected to a not superspeed capable hcd */
364 if (r
->usb3ep0quirk
&& xfer
->actual_length
>= 18 &&
369 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
370 r
->p
->status
, r
->p
->actual_length
);
371 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
374 usb_host_req_free(r
);
380 static void usb_host_req_complete_data(struct libusb_transfer
*xfer
)
382 USBHostRequest
*r
= xfer
->user_data
;
383 USBHostDevice
*s
= r
->host
;
384 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
387 goto out
; /* request was canceled */
390 r
->p
->status
= status_map
[xfer
->status
];
391 if (r
->in
&& xfer
->actual_length
) {
392 usb_packet_copy(r
->p
, r
->buffer
, xfer
->actual_length
);
394 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
395 r
->p
->status
, r
->p
->actual_length
);
396 if (usb_host_use_combining(r
->p
->ep
)) {
397 usb_combined_input_packet_complete(USB_DEVICE(s
), r
->p
);
399 usb_packet_complete(USB_DEVICE(s
), r
->p
);
403 usb_host_req_free(r
);
409 static void usb_host_req_abort(USBHostRequest
*r
)
411 USBHostDevice
*s
= r
->host
;
412 bool inflight
= (r
->p
&& r
->p
->state
== USB_PACKET_ASYNC
);
415 r
->p
->status
= USB_RET_NODEV
;
416 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
417 r
->p
->status
, r
->p
->actual_length
);
418 if (r
->p
->ep
->nr
== 0) {
419 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
421 usb_packet_complete(USB_DEVICE(s
), r
->p
);
426 QTAILQ_REMOVE(&r
->host
->requests
, r
, next
);
430 libusb_cancel_transfer(r
->xfer
);
434 /* ------------------------------------------------------------------------ */
436 static void usb_host_req_complete_iso(struct libusb_transfer
*transfer
)
438 USBHostIsoXfer
*xfer
= transfer
->user_data
;
441 /* USBHostIsoXfer released while inflight */
442 g_free(transfer
->buffer
);
443 libusb_free_transfer(transfer
);
447 QTAILQ_REMOVE(&xfer
->ring
->inflight
, xfer
, next
);
448 if (QTAILQ_EMPTY(&xfer
->ring
->inflight
)) {
449 USBHostDevice
*s
= xfer
->ring
->host
;
450 trace_usb_host_iso_stop(s
->bus_num
, s
->addr
, xfer
->ring
->ep
->nr
);
452 if (xfer
->ring
->ep
->pid
== USB_TOKEN_IN
) {
453 QTAILQ_INSERT_TAIL(&xfer
->ring
->copy
, xfer
, next
);
455 QTAILQ_INSERT_TAIL(&xfer
->ring
->unused
, xfer
, next
);
459 static USBHostIsoRing
*usb_host_iso_alloc(USBHostDevice
*s
, USBEndpoint
*ep
)
461 USBHostIsoRing
*ring
= g_new0(USBHostIsoRing
, 1);
462 USBHostIsoXfer
*xfer
;
463 /* FIXME: check interval (for now assume one xfer per frame) */
464 int packets
= s
->iso_urb_frames
;
469 QTAILQ_INIT(&ring
->unused
);
470 QTAILQ_INIT(&ring
->inflight
);
471 QTAILQ_INIT(&ring
->copy
);
472 QTAILQ_INSERT_TAIL(&s
->isorings
, ring
, next
);
474 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
475 xfer
= g_new0(USBHostIsoXfer
, 1);
477 xfer
->xfer
= libusb_alloc_transfer(packets
);
478 xfer
->xfer
->dev_handle
= s
->dh
;
479 xfer
->xfer
->type
= LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
;
481 xfer
->xfer
->endpoint
= ring
->ep
->nr
;
482 if (ring
->ep
->pid
== USB_TOKEN_IN
) {
483 xfer
->xfer
->endpoint
|= USB_DIR_IN
;
485 xfer
->xfer
->callback
= usb_host_req_complete_iso
;
486 xfer
->xfer
->user_data
= xfer
;
488 xfer
->xfer
->num_iso_packets
= packets
;
489 xfer
->xfer
->length
= ring
->ep
->max_packet_size
* packets
;
490 xfer
->xfer
->buffer
= g_malloc0(xfer
->xfer
->length
);
492 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
498 static USBHostIsoRing
*usb_host_iso_find(USBHostDevice
*s
, USBEndpoint
*ep
)
500 USBHostIsoRing
*ring
;
502 QTAILQ_FOREACH(ring
, &s
->isorings
, next
) {
503 if (ring
->ep
== ep
) {
510 static void usb_host_iso_reset_xfer(USBHostIsoXfer
*xfer
)
512 libusb_set_iso_packet_lengths(xfer
->xfer
,
513 xfer
->ring
->ep
->max_packet_size
);
515 xfer
->copy_complete
= false;
518 static void usb_host_iso_free_xfer(USBHostIsoXfer
*xfer
, bool inflight
)
521 xfer
->xfer
->user_data
= NULL
;
523 g_free(xfer
->xfer
->buffer
);
524 libusb_free_transfer(xfer
->xfer
);
529 static void usb_host_iso_free(USBHostIsoRing
*ring
)
531 USBHostIsoXfer
*xfer
;
533 while ((xfer
= QTAILQ_FIRST(&ring
->inflight
)) != NULL
) {
534 QTAILQ_REMOVE(&ring
->inflight
, xfer
, next
);
535 usb_host_iso_free_xfer(xfer
, true);
537 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
538 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
539 usb_host_iso_free_xfer(xfer
, false);
541 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
) {
542 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
543 usb_host_iso_free_xfer(xfer
, false);
546 QTAILQ_REMOVE(&ring
->host
->isorings
, ring
, next
);
550 static void usb_host_iso_free_all(USBHostDevice
*s
)
552 USBHostIsoRing
*ring
;
554 while ((ring
= QTAILQ_FIRST(&s
->isorings
)) != NULL
) {
555 usb_host_iso_free(ring
);
559 static bool usb_host_iso_data_copy(USBHostIsoXfer
*xfer
, USBPacket
*p
)
564 buf
= libusb_get_iso_packet_buffer_simple(xfer
->xfer
, xfer
->packet
);
565 if (p
->pid
== USB_TOKEN_OUT
) {
567 if (psize
> xfer
->ring
->ep
->max_packet_size
) {
568 /* should not happen (guest bug) */
569 psize
= xfer
->ring
->ep
->max_packet_size
;
571 xfer
->xfer
->iso_packet_desc
[xfer
->packet
].length
= psize
;
573 psize
= xfer
->xfer
->iso_packet_desc
[xfer
->packet
].actual_length
;
574 if (psize
> p
->iov
.size
) {
575 /* should not happen (guest bug) */
579 usb_packet_copy(p
, buf
, psize
);
581 xfer
->copy_complete
= (xfer
->packet
== xfer
->xfer
->num_iso_packets
);
582 return xfer
->copy_complete
;
585 static void usb_host_iso_data_in(USBHostDevice
*s
, USBPacket
*p
)
587 USBHostIsoRing
*ring
;
588 USBHostIsoXfer
*xfer
;
589 bool disconnect
= false;
592 ring
= usb_host_iso_find(s
, p
->ep
);
594 ring
= usb_host_iso_alloc(s
, p
->ep
);
597 /* copy data to guest */
598 xfer
= QTAILQ_FIRST(&ring
->copy
);
600 if (usb_host_iso_data_copy(xfer
, p
)) {
601 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
602 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
606 /* submit empty bufs to host */
607 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
608 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
609 usb_host_iso_reset_xfer(xfer
);
610 rc
= libusb_submit_transfer(xfer
->xfer
);
612 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
613 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
614 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
619 if (QTAILQ_EMPTY(&ring
->inflight
)) {
620 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
622 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
630 static void usb_host_iso_data_out(USBHostDevice
*s
, USBPacket
*p
)
632 USBHostIsoRing
*ring
;
633 USBHostIsoXfer
*xfer
;
634 bool disconnect
= false;
637 ring
= usb_host_iso_find(s
, p
->ep
);
639 ring
= usb_host_iso_alloc(s
, p
->ep
);
642 /* copy data from guest */
643 xfer
= QTAILQ_FIRST(&ring
->copy
);
644 while (xfer
!= NULL
&& xfer
->copy_complete
) {
646 xfer
= QTAILQ_NEXT(xfer
, next
);
649 xfer
= QTAILQ_FIRST(&ring
->unused
);
651 trace_usb_host_iso_out_of_bufs(s
->bus_num
, s
->addr
, p
->ep
->nr
);
654 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
655 usb_host_iso_reset_xfer(xfer
);
656 QTAILQ_INSERT_TAIL(&ring
->copy
, xfer
, next
);
658 usb_host_iso_data_copy(xfer
, p
);
660 if (QTAILQ_EMPTY(&ring
->inflight
)) {
661 /* wait until half of our buffers are filled
662 before kicking the iso out stream */
663 if (filled
*2 < s
->iso_urb_count
) {
668 /* submit filled bufs to host */
669 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
&&
670 xfer
->copy_complete
) {
671 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
672 rc
= libusb_submit_transfer(xfer
->xfer
);
674 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
675 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
676 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
681 if (QTAILQ_EMPTY(&ring
->inflight
)) {
682 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
684 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
692 /* ------------------------------------------------------------------------ */
694 static void usb_host_speed_compat(USBHostDevice
*s
)
696 USBDevice
*udev
= USB_DEVICE(s
);
697 struct libusb_config_descriptor
*conf
;
698 const struct libusb_interface_descriptor
*intf
;
699 const struct libusb_endpoint_descriptor
*endp
;
701 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
703 bool compat_high
= true;
704 bool compat_full
= true;
709 rc
= libusb_get_config_descriptor(s
->dev
, c
, &conf
);
713 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
714 for (a
= 0; a
< conf
->interface
[i
].num_altsetting
; a
++) {
715 intf
= &conf
->interface
[i
].altsetting
[a
];
716 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
717 endp
= &intf
->endpoint
[e
];
718 type
= endp
->bmAttributes
& 0x3;
724 case 0x02: /* BULK */
726 rc
= libusb_get_ss_endpoint_companion_descriptor
727 (ctx
, endp
, &endp_ss_comp
);
728 if (rc
== LIBUSB_SUCCESS
) {
729 libusb_free_ss_endpoint_companion_descriptor
736 case 0x03: /* INTERRUPT */
737 if (endp
->wMaxPacketSize
> 64) {
740 if (endp
->wMaxPacketSize
> 1024) {
748 libusb_free_config_descriptor(conf
);
751 udev
->speedmask
= (1 << udev
->speed
);
752 if (udev
->speed
== USB_SPEED_SUPER
&& compat_high
) {
753 udev
->speedmask
|= USB_SPEED_MASK_HIGH
;
755 if (udev
->speed
== USB_SPEED_SUPER
&& compat_full
) {
756 udev
->speedmask
|= USB_SPEED_MASK_FULL
;
758 if (udev
->speed
== USB_SPEED_HIGH
&& compat_full
) {
759 udev
->speedmask
|= USB_SPEED_MASK_FULL
;
763 static void usb_host_ep_update(USBHostDevice
*s
)
765 static const char *tname
[] = {
766 [USB_ENDPOINT_XFER_CONTROL
] = "control",
767 [USB_ENDPOINT_XFER_ISOC
] = "isoc",
768 [USB_ENDPOINT_XFER_BULK
] = "bulk",
769 [USB_ENDPOINT_XFER_INT
] = "int",
771 USBDevice
*udev
= USB_DEVICE(s
);
772 struct libusb_config_descriptor
*conf
;
773 const struct libusb_interface_descriptor
*intf
;
774 const struct libusb_endpoint_descriptor
*endp
;
776 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
783 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
787 trace_usb_host_parse_config(s
->bus_num
, s
->addr
,
788 conf
->bConfigurationValue
, true);
790 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
791 assert(udev
->altsetting
[i
] < conf
->interface
[i
].num_altsetting
);
792 intf
= &conf
->interface
[i
].altsetting
[udev
->altsetting
[i
]];
793 trace_usb_host_parse_interface(s
->bus_num
, s
->addr
,
794 intf
->bInterfaceNumber
,
795 intf
->bAlternateSetting
, true);
796 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
797 endp
= &intf
->endpoint
[e
];
799 devep
= endp
->bEndpointAddress
;
800 pid
= (devep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
802 type
= endp
->bmAttributes
& 0x3;
805 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
806 "invalid endpoint address");
809 if (usb_ep_get_type(udev
, pid
, ep
) != USB_ENDPOINT_XFER_INVALID
) {
810 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
811 "duplicate endpoint address");
815 trace_usb_host_parse_endpoint(s
->bus_num
, s
->addr
, ep
,
816 (devep
& USB_DIR_IN
) ? "in" : "out",
818 usb_ep_set_max_packet_size(udev
, pid
, ep
,
819 endp
->wMaxPacketSize
);
820 usb_ep_set_type(udev
, pid
, ep
, type
);
821 usb_ep_set_ifnum(udev
, pid
, ep
, i
);
822 usb_ep_set_halted(udev
, pid
, ep
, 0);
824 if (type
== LIBUSB_TRANSFER_TYPE_BULK
&&
825 libusb_get_ss_endpoint_companion_descriptor(ctx
, endp
,
826 &endp_ss_comp
) == LIBUSB_SUCCESS
) {
827 usb_ep_set_max_streams(udev
, pid
, ep
,
828 endp_ss_comp
->bmAttributes
);
829 libusb_free_ss_endpoint_companion_descriptor(endp_ss_comp
);
835 libusb_free_config_descriptor(conf
);
838 static int usb_host_open(USBHostDevice
*s
, libusb_device
*dev
)
840 USBDevice
*udev
= USB_DEVICE(s
);
841 int bus_num
= libusb_get_bus_number(dev
);
842 int addr
= libusb_get_device_address(dev
);
844 Error
*local_err
= NULL
;
846 trace_usb_host_open_started(bus_num
, addr
);
851 rc
= libusb_open(dev
, &s
->dh
);
857 s
->bus_num
= bus_num
;
860 usb_host_detach_kernel(s
);
862 libusb_get_device_descriptor(dev
, &s
->ddesc
);
863 usb_host_get_port(s
->dev
, s
->port
, sizeof(s
->port
));
866 usb_host_ep_update(s
);
868 udev
->speed
= speed_map
[libusb_get_device_speed(dev
)];
869 usb_host_speed_compat(s
);
871 if (s
->ddesc
.iProduct
) {
872 libusb_get_string_descriptor_ascii(s
->dh
, s
->ddesc
.iProduct
,
873 (unsigned char *)udev
->product_desc
,
874 sizeof(udev
->product_desc
));
876 snprintf(udev
->product_desc
, sizeof(udev
->product_desc
),
877 "host:%d.%d", bus_num
, addr
);
880 usb_device_attach(udev
, &local_err
);
882 error_report_err(local_err
);
886 trace_usb_host_open_success(bus_num
, addr
);
890 trace_usb_host_open_failure(bus_num
, addr
);
892 usb_host_release_interfaces(s
);
893 libusb_reset_device(s
->dh
);
894 usb_host_attach_kernel(s
);
902 static void usb_host_abort_xfers(USBHostDevice
*s
)
904 USBHostRequest
*r
, *rtmp
;
906 QTAILQ_FOREACH_SAFE(r
, &s
->requests
, next
, rtmp
) {
907 usb_host_req_abort(r
);
911 static int usb_host_close(USBHostDevice
*s
)
913 USBDevice
*udev
= USB_DEVICE(s
);
919 trace_usb_host_close(s
->bus_num
, s
->addr
);
921 usb_host_abort_xfers(s
);
922 usb_host_iso_free_all(s
);
924 if (udev
->attached
) {
925 usb_device_detach(udev
);
928 usb_host_release_interfaces(s
);
929 libusb_reset_device(s
->dh
);
930 usb_host_attach_kernel(s
);
935 usb_host_auto_check(NULL
);
939 static void usb_host_nodev_bh(void *opaque
)
941 USBHostDevice
*s
= opaque
;
945 static void usb_host_nodev(USBHostDevice
*s
)
948 s
->bh_nodev
= qemu_bh_new(usb_host_nodev_bh
, s
);
950 qemu_bh_schedule(s
->bh_nodev
);
953 static void usb_host_exit_notifier(struct Notifier
*n
, void *data
)
955 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
958 usb_host_release_interfaces(s
);
959 usb_host_attach_kernel(s
);
963 static void usb_host_realize(USBDevice
*udev
, Error
**errp
)
965 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
967 if (s
->match
.vendor_id
> 0xffff) {
968 error_setg(errp
, "vendorid out of range");
971 if (s
->match
.product_id
> 0xffff) {
972 error_setg(errp
, "productid out of range");
975 if (s
->match
.addr
> 127) {
976 error_setg(errp
, "hostaddr out of range");
980 loglevel
= s
->loglevel
;
981 udev
->flags
|= (1 << USB_DEV_FLAG_IS_HOST
);
982 udev
->auto_attach
= 0;
983 QTAILQ_INIT(&s
->requests
);
984 QTAILQ_INIT(&s
->isorings
);
986 s
->exit
.notify
= usb_host_exit_notifier
;
987 qemu_add_exit_notifier(&s
->exit
);
989 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
990 usb_host_auto_check(NULL
);
993 static void usb_host_instance_init(Object
*obj
)
995 USBDevice
*udev
= USB_DEVICE(obj
);
996 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
998 device_add_bootindex_property(obj
, &s
->bootindex
,
1003 static void usb_host_handle_destroy(USBDevice
*udev
)
1005 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1007 qemu_remove_exit_notifier(&s
->exit
);
1008 QTAILQ_REMOVE(&hostdevs
, s
, next
);
1012 static void usb_host_cancel_packet(USBDevice
*udev
, USBPacket
*p
)
1014 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1018 usb_combined_packet_cancel(udev
, p
);
1022 trace_usb_host_req_canceled(s
->bus_num
, s
->addr
, p
);
1024 r
= usb_host_req_find(s
, p
);
1026 r
->p
= NULL
; /* mark as dead */
1027 libusb_cancel_transfer(r
->xfer
);
1031 static void usb_host_detach_kernel(USBHostDevice
*s
)
1033 struct libusb_config_descriptor
*conf
;
1036 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1040 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
1041 rc
= libusb_kernel_driver_active(s
->dh
, i
);
1042 usb_host_libusb_error("libusb_kernel_driver_active", rc
);
1046 trace_usb_host_detach_kernel(s
->bus_num
, s
->addr
, i
);
1047 rc
= libusb_detach_kernel_driver(s
->dh
, i
);
1048 usb_host_libusb_error("libusb_detach_kernel_driver", rc
);
1049 s
->ifs
[i
].detached
= true;
1051 libusb_free_config_descriptor(conf
);
1054 static void usb_host_attach_kernel(USBHostDevice
*s
)
1056 struct libusb_config_descriptor
*conf
;
1059 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1063 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
1064 if (!s
->ifs
[i
].detached
) {
1067 trace_usb_host_attach_kernel(s
->bus_num
, s
->addr
, i
);
1068 libusb_attach_kernel_driver(s
->dh
, i
);
1069 s
->ifs
[i
].detached
= false;
1071 libusb_free_config_descriptor(conf
);
1074 static int usb_host_claim_interfaces(USBHostDevice
*s
, int configuration
)
1076 USBDevice
*udev
= USB_DEVICE(s
);
1077 struct libusb_config_descriptor
*conf
;
1080 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1081 udev
->altsetting
[i
] = 0;
1083 udev
->ninterfaces
= 0;
1084 udev
->configuration
= 0;
1086 usb_host_detach_kernel(s
);
1088 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1090 if (rc
== LIBUSB_ERROR_NOT_FOUND
) {
1091 /* address state - ignore */
1092 return USB_RET_SUCCESS
;
1094 return USB_RET_STALL
;
1097 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
1098 trace_usb_host_claim_interface(s
->bus_num
, s
->addr
, configuration
, i
);
1099 rc
= libusb_claim_interface(s
->dh
, i
);
1100 usb_host_libusb_error("libusb_claim_interface", rc
);
1102 return USB_RET_STALL
;
1104 s
->ifs
[i
].claimed
= true;
1107 udev
->ninterfaces
= conf
->bNumInterfaces
;
1108 udev
->configuration
= configuration
;
1110 libusb_free_config_descriptor(conf
);
1111 return USB_RET_SUCCESS
;
1114 static void usb_host_release_interfaces(USBHostDevice
*s
)
1116 USBDevice
*udev
= USB_DEVICE(s
);
1119 for (i
= 0; i
< udev
->ninterfaces
; i
++) {
1120 if (!s
->ifs
[i
].claimed
) {
1123 trace_usb_host_release_interface(s
->bus_num
, s
->addr
, i
);
1124 rc
= libusb_release_interface(s
->dh
, i
);
1125 usb_host_libusb_error("libusb_release_interface", rc
);
1126 s
->ifs
[i
].claimed
= false;
1130 static void usb_host_set_address(USBHostDevice
*s
, int addr
)
1132 USBDevice
*udev
= USB_DEVICE(s
);
1134 trace_usb_host_set_address(s
->bus_num
, s
->addr
, addr
);
1138 static void usb_host_set_config(USBHostDevice
*s
, int config
, USBPacket
*p
)
1142 trace_usb_host_set_config(s
->bus_num
, s
->addr
, config
);
1144 usb_host_release_interfaces(s
);
1145 rc
= libusb_set_configuration(s
->dh
, config
);
1147 usb_host_libusb_error("libusb_set_configuration", rc
);
1148 p
->status
= USB_RET_STALL
;
1149 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1154 p
->status
= usb_host_claim_interfaces(s
, config
);
1155 if (p
->status
!= USB_RET_SUCCESS
) {
1158 usb_host_ep_update(s
);
1161 static void usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
,
1164 USBDevice
*udev
= USB_DEVICE(s
);
1167 trace_usb_host_set_interface(s
->bus_num
, s
->addr
, iface
, alt
);
1169 usb_host_iso_free_all(s
);
1171 if (iface
>= USB_MAX_INTERFACES
) {
1172 p
->status
= USB_RET_STALL
;
1176 rc
= libusb_set_interface_alt_setting(s
->dh
, iface
, alt
);
1178 usb_host_libusb_error("libusb_set_interface_alt_setting", rc
);
1179 p
->status
= USB_RET_STALL
;
1180 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1186 udev
->altsetting
[iface
] = alt
;
1187 usb_host_ep_update(s
);
1190 static void usb_host_handle_control(USBDevice
*udev
, USBPacket
*p
,
1191 int request
, int value
, int index
,
1192 int length
, uint8_t *data
)
1194 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1198 trace_usb_host_req_control(s
->bus_num
, s
->addr
, p
, request
, value
, index
);
1200 if (s
->dh
== NULL
) {
1201 p
->status
= USB_RET_NODEV
;
1202 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1207 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1208 usb_host_set_address(s
, value
);
1209 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1212 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1213 usb_host_set_config(s
, value
& 0xff, p
);
1214 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1217 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1218 usb_host_set_interface(s
, index
, value
, p
);
1219 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1222 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
1223 if (value
== 0) { /* clear halt */
1224 int pid
= (index
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1225 libusb_clear_halt(s
->dh
, index
);
1226 usb_ep_set_halted(udev
, pid
, index
& 0x0f, 0);
1227 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1232 r
= usb_host_req_alloc(s
, p
, (request
>> 8) & USB_DIR_IN
, length
+ 8);
1235 memcpy(r
->buffer
, udev
->setup_buf
, 8);
1237 memcpy(r
->buffer
+ 8, r
->cbuf
, r
->clen
);
1240 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
1241 * to work redirected to a not superspeed capable hcd */
1242 if (udev
->speed
== USB_SPEED_SUPER
&&
1243 !(udev
->port
->speedmask
& USB_SPEED_MASK_SUPER
) &&
1244 request
== 0x8006 && value
== 0x100 && index
== 0) {
1245 r
->usb3ep0quirk
= true;
1248 libusb_fill_control_transfer(r
->xfer
, s
->dh
, r
->buffer
,
1249 usb_host_req_complete_ctrl
, r
,
1251 rc
= libusb_submit_transfer(r
->xfer
);
1253 p
->status
= USB_RET_NODEV
;
1254 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1255 p
->status
, p
->actual_length
);
1256 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1262 p
->status
= USB_RET_ASYNC
;
1265 static void usb_host_handle_data(USBDevice
*udev
, USBPacket
*p
)
1267 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1272 if (usb_host_use_combining(p
->ep
) && p
->state
== USB_PACKET_SETUP
) {
1273 p
->status
= USB_RET_ADD_TO_QUEUE
;
1277 trace_usb_host_req_data(s
->bus_num
, s
->addr
, p
,
1278 p
->pid
== USB_TOKEN_IN
,
1279 p
->ep
->nr
, p
->iov
.size
);
1281 if (s
->dh
== NULL
) {
1282 p
->status
= USB_RET_NODEV
;
1283 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1286 if (p
->ep
->halted
) {
1287 p
->status
= USB_RET_STALL
;
1288 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1292 switch (usb_ep_get_type(udev
, p
->pid
, p
->ep
->nr
)) {
1293 case USB_ENDPOINT_XFER_BULK
:
1294 size
= usb_packet_size(p
);
1295 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, size
);
1297 usb_packet_copy(p
, r
->buffer
, size
);
1299 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1302 libusb_fill_bulk_stream_transfer(r
->xfer
, s
->dh
, ep
, p
->stream
,
1304 usb_host_req_complete_data
, r
,
1307 usb_host_req_free(r
);
1308 p
->status
= USB_RET_STALL
;
1312 libusb_fill_bulk_transfer(r
->xfer
, s
->dh
, ep
,
1314 usb_host_req_complete_data
, r
,
1318 case USB_ENDPOINT_XFER_INT
:
1319 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, p
->iov
.size
);
1321 usb_packet_copy(p
, r
->buffer
, p
->iov
.size
);
1323 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1324 libusb_fill_interrupt_transfer(r
->xfer
, s
->dh
, ep
,
1325 r
->buffer
, p
->iov
.size
,
1326 usb_host_req_complete_data
, r
,
1329 case USB_ENDPOINT_XFER_ISOC
:
1330 if (p
->pid
== USB_TOKEN_IN
) {
1331 usb_host_iso_data_in(s
, p
);
1333 usb_host_iso_data_out(s
, p
);
1335 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1336 p
->status
, p
->actual_length
);
1339 p
->status
= USB_RET_STALL
;
1340 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1341 p
->status
, p
->actual_length
);
1345 rc
= libusb_submit_transfer(r
->xfer
);
1347 p
->status
= USB_RET_NODEV
;
1348 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1349 p
->status
, p
->actual_length
);
1350 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1356 p
->status
= USB_RET_ASYNC
;
1359 static void usb_host_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
1361 if (usb_host_use_combining(ep
)) {
1362 usb_ep_combine_input_packets(ep
);
1366 static void usb_host_handle_reset(USBDevice
*udev
)
1368 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1371 trace_usb_host_reset(s
->bus_num
, s
->addr
);
1373 rc
= libusb_reset_device(s
->dh
);
1379 static int usb_host_alloc_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1380 int nr_eps
, int streams
)
1383 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1384 unsigned char endpoints
[30];
1387 for (i
= 0; i
< nr_eps
; i
++) {
1388 endpoints
[i
] = eps
[i
]->nr
;
1389 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1390 endpoints
[i
] |= 0x80;
1393 rc
= libusb_alloc_streams(s
->dh
, streams
, endpoints
, nr_eps
);
1395 usb_host_libusb_error("libusb_alloc_streams", rc
);
1396 } else if (rc
!= streams
) {
1397 error_report("libusb_alloc_streams: got less streams "
1398 "then requested %d < %d", rc
, streams
);
1401 return (rc
== streams
) ? 0 : -1;
1403 error_report("libusb_alloc_streams: error not implemented");
1408 static void usb_host_free_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1412 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1413 unsigned char endpoints
[30];
1416 for (i
= 0; i
< nr_eps
; i
++) {
1417 endpoints
[i
] = eps
[i
]->nr
;
1418 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1419 endpoints
[i
] |= 0x80;
1422 libusb_free_streams(s
->dh
, endpoints
, nr_eps
);
1427 * This is *NOT* about restoring state. We have absolutely no idea
1428 * what state the host device is in at the moment and whenever it is
1429 * still present in the first place. Attemping to contine where we
1430 * left off is impossible.
1432 * What we are going to do here is emulate a surprise removal of
1433 * the usb device passed through, then kick host scan so the device
1434 * will get re-attached (and re-initialized by the guest) in case it
1437 * As the device removal will change the state of other devices (usb
1438 * host controller, most likely interrupt controller too) we have to
1439 * wait with it until *all* vmstate is loaded. Thus post_load just
1440 * kicks a bottom half which then does the actual work.
1442 static void usb_host_post_load_bh(void *opaque
)
1444 USBHostDevice
*dev
= opaque
;
1445 USBDevice
*udev
= USB_DEVICE(dev
);
1447 if (dev
->dh
!= NULL
) {
1448 usb_host_close(dev
);
1450 if (udev
->attached
) {
1451 usb_device_detach(udev
);
1453 usb_host_auto_check(NULL
);
1456 static int usb_host_post_load(void *opaque
, int version_id
)
1458 USBHostDevice
*dev
= opaque
;
1460 if (!dev
->bh_postld
) {
1461 dev
->bh_postld
= qemu_bh_new(usb_host_post_load_bh
, dev
);
1463 qemu_bh_schedule(dev
->bh_postld
);
1467 static const VMStateDescription vmstate_usb_host
= {
1470 .minimum_version_id
= 1,
1471 .post_load
= usb_host_post_load
,
1472 .fields
= (VMStateField
[]) {
1473 VMSTATE_USB_DEVICE(parent_obj
, USBHostDevice
),
1474 VMSTATE_END_OF_LIST()
1478 static Property usb_host_dev_properties
[] = {
1479 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1480 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1481 DEFINE_PROP_STRING("hostport", USBHostDevice
, match
.port
),
1482 DEFINE_PROP_UINT32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1483 DEFINE_PROP_UINT32("productid", USBHostDevice
, match
.product_id
, 0),
1484 DEFINE_PROP_UINT32("isobufs", USBHostDevice
, iso_urb_count
, 4),
1485 DEFINE_PROP_UINT32("isobsize", USBHostDevice
, iso_urb_frames
, 32),
1486 DEFINE_PROP_UINT32("loglevel", USBHostDevice
, loglevel
,
1487 LIBUSB_LOG_LEVEL_WARNING
),
1488 DEFINE_PROP_BIT("pipeline", USBHostDevice
, options
,
1489 USB_HOST_OPT_PIPELINE
, true),
1490 DEFINE_PROP_END_OF_LIST(),
1493 static void usb_host_class_initfn(ObjectClass
*klass
, void *data
)
1495 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1496 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1498 uc
->realize
= usb_host_realize
;
1499 uc
->product_desc
= "USB Host Device";
1500 uc
->cancel_packet
= usb_host_cancel_packet
;
1501 uc
->handle_data
= usb_host_handle_data
;
1502 uc
->handle_control
= usb_host_handle_control
;
1503 uc
->handle_reset
= usb_host_handle_reset
;
1504 uc
->handle_destroy
= usb_host_handle_destroy
;
1505 uc
->flush_ep_queue
= usb_host_flush_ep_queue
;
1506 uc
->alloc_streams
= usb_host_alloc_streams
;
1507 uc
->free_streams
= usb_host_free_streams
;
1508 dc
->vmsd
= &vmstate_usb_host
;
1509 dc
->props
= usb_host_dev_properties
;
1510 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
1513 static TypeInfo usb_host_dev_info
= {
1514 .name
= TYPE_USB_HOST_DEVICE
,
1515 .parent
= TYPE_USB_DEVICE
,
1516 .instance_size
= sizeof(USBHostDevice
),
1517 .class_init
= usb_host_class_initfn
,
1518 .instance_init
= usb_host_instance_init
,
1521 static void usb_host_register_types(void)
1523 type_register_static(&usb_host_dev_info
);
1526 type_init(usb_host_register_types
)
1528 /* ------------------------------------------------------------------------ */
1530 static QEMUTimer
*usb_auto_timer
;
1531 static VMChangeStateEntry
*usb_vmstate
;
1533 static void usb_host_vm_state(void *unused
, int running
, RunState state
)
1536 usb_host_auto_check(unused
);
1540 static void usb_host_auto_check(void *unused
)
1542 struct USBHostDevice
*s
;
1543 struct USBAutoFilter
*f
;
1544 libusb_device
**devs
= NULL
;
1545 struct libusb_device_descriptor ddesc
;
1546 int unconnected
= 0;
1549 if (usb_host_init() != 0) {
1553 if (runstate_is_running()) {
1554 n
= libusb_get_device_list(ctx
, &devs
);
1555 for (i
= 0; i
< n
; i
++) {
1556 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1559 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1562 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1564 if (f
->bus_num
> 0 &&
1565 f
->bus_num
!= libusb_get_bus_number(devs
[i
])) {
1569 f
->addr
!= libusb_get_device_address(devs
[i
])) {
1572 if (f
->port
!= NULL
) {
1573 char port
[16] = "-";
1574 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1575 if (strcmp(f
->port
, port
) != 0) {
1579 if (f
->vendor_id
> 0 &&
1580 f
->vendor_id
!= ddesc
.idVendor
) {
1583 if (f
->product_id
> 0 &&
1584 f
->product_id
!= ddesc
.idProduct
) {
1588 /* We got a match */
1590 if (s
->errcount
>= 3) {
1593 if (s
->dh
!= NULL
) {
1596 if (usb_host_open(s
, devs
[i
]) < 0) {
1603 libusb_free_device_list(devs
, 1);
1605 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1606 if (s
->dh
== NULL
) {
1619 if (unconnected
== 0) {
1620 /* nothing to watch */
1621 if (usb_auto_timer
) {
1622 timer_del(usb_auto_timer
);
1623 trace_usb_host_auto_scan_disabled();
1631 usb_vmstate
= qemu_add_vm_change_state_handler(usb_host_vm_state
, NULL
);
1633 if (!usb_auto_timer
) {
1634 usb_auto_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, usb_host_auto_check
, NULL
);
1635 if (!usb_auto_timer
) {
1638 trace_usb_host_auto_scan_enabled();
1640 timer_mod(usb_auto_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 2000);
1643 void hmp_info_usbhost(Monitor
*mon
, const QDict
*qdict
)
1645 libusb_device
**devs
= NULL
;
1646 struct libusb_device_descriptor ddesc
;
1650 if (usb_host_init() != 0) {
1654 n
= libusb_get_device_list(ctx
, &devs
);
1655 for (i
= 0; i
< n
; i
++) {
1656 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1659 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1662 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1663 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1664 libusb_get_bus_number(devs
[i
]),
1665 libusb_get_device_address(devs
[i
]),
1667 speed_name
[libusb_get_device_speed(devs
[i
])]);
1668 monitor_printf(mon
, " Class %02x:", ddesc
.bDeviceClass
);
1669 monitor_printf(mon
, " USB device %04x:%04x",
1670 ddesc
.idVendor
, ddesc
.idProduct
);
1671 if (ddesc
.iProduct
) {
1672 libusb_device_handle
*handle
;
1673 if (libusb_open(devs
[i
], &handle
) == 0) {
1674 unsigned char name
[64] = "";
1675 libusb_get_string_descriptor_ascii(handle
,
1677 name
, sizeof(name
));
1678 libusb_close(handle
);
1679 monitor_printf(mon
, ", %s", name
);
1682 monitor_printf(mon
, "\n");
1684 libusb_free_device_list(devs
, 1);