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 "sysemu/sysemu.h"
46 /* ------------------------------------------------------------------------ */
48 #define TYPE_USB_HOST_DEVICE "usb-host"
49 #define USB_HOST_DEVICE(obj) \
50 OBJECT_CHECK(USBHostDevice, (obj), TYPE_USB_HOST_DEVICE)
52 typedef struct USBHostDevice USBHostDevice
;
53 typedef struct USBHostRequest USBHostRequest
;
54 typedef struct USBHostIsoXfer USBHostIsoXfer
;
55 typedef struct USBHostIsoRing USBHostIsoRing
;
57 struct USBAutoFilter
{
65 enum USBHostDeviceOptions
{
66 USB_HOST_OPT_PIPELINE
,
69 struct USBHostDevice
{
73 struct USBAutoFilter match
;
75 uint32_t iso_urb_count
;
76 uint32_t iso_urb_frames
;
81 QTAILQ_ENTRY(USBHostDevice
) next
;
88 libusb_device_handle
*dh
;
89 struct libusb_device_descriptor ddesc
;
94 } ifs
[USB_MAX_INTERFACES
];
96 /* callbacks & friends */
102 QTAILQ_HEAD(, USBHostRequest
) requests
;
103 QTAILQ_HEAD(, USBHostIsoRing
) isorings
;
106 struct USBHostRequest
{
110 struct libusb_transfer
*xfer
;
111 unsigned char *buffer
;
115 QTAILQ_ENTRY(USBHostRequest
) next
;
118 struct USBHostIsoXfer
{
119 USBHostIsoRing
*ring
;
120 struct libusb_transfer
*xfer
;
123 QTAILQ_ENTRY(USBHostIsoXfer
) next
;
126 struct USBHostIsoRing
{
129 QTAILQ_HEAD(, USBHostIsoXfer
) unused
;
130 QTAILQ_HEAD(, USBHostIsoXfer
) inflight
;
131 QTAILQ_HEAD(, USBHostIsoXfer
) copy
;
132 QTAILQ_ENTRY(USBHostIsoRing
) next
;
135 static QTAILQ_HEAD(, USBHostDevice
) hostdevs
=
136 QTAILQ_HEAD_INITIALIZER(hostdevs
);
138 static void usb_host_auto_check(void *unused
);
139 static void usb_host_release_interfaces(USBHostDevice
*s
);
140 static void usb_host_nodev(USBHostDevice
*s
);
141 static void usb_host_detach_kernel(USBHostDevice
*s
);
142 static void usb_host_attach_kernel(USBHostDevice
*s
);
144 /* ------------------------------------------------------------------------ */
146 #define CONTROL_TIMEOUT 10000 /* 10 sec */
147 #define BULK_TIMEOUT 0 /* unlimited */
148 #define INTR_TIMEOUT 0 /* unlimited */
150 #if LIBUSBX_API_VERSION >= 0x01000103
151 # define HAVE_STREAMS 1
154 static const char *speed_name
[] = {
155 [LIBUSB_SPEED_UNKNOWN
] = "?",
156 [LIBUSB_SPEED_LOW
] = "1.5",
157 [LIBUSB_SPEED_FULL
] = "12",
158 [LIBUSB_SPEED_HIGH
] = "480",
159 [LIBUSB_SPEED_SUPER
] = "5000",
162 static const unsigned int speed_map
[] = {
163 [LIBUSB_SPEED_LOW
] = USB_SPEED_LOW
,
164 [LIBUSB_SPEED_FULL
] = USB_SPEED_FULL
,
165 [LIBUSB_SPEED_HIGH
] = USB_SPEED_HIGH
,
166 [LIBUSB_SPEED_SUPER
] = USB_SPEED_SUPER
,
169 static const unsigned int status_map
[] = {
170 [LIBUSB_TRANSFER_COMPLETED
] = USB_RET_SUCCESS
,
171 [LIBUSB_TRANSFER_ERROR
] = USB_RET_IOERROR
,
172 [LIBUSB_TRANSFER_TIMED_OUT
] = USB_RET_IOERROR
,
173 [LIBUSB_TRANSFER_CANCELLED
] = USB_RET_IOERROR
,
174 [LIBUSB_TRANSFER_STALL
] = USB_RET_STALL
,
175 [LIBUSB_TRANSFER_NO_DEVICE
] = USB_RET_NODEV
,
176 [LIBUSB_TRANSFER_OVERFLOW
] = USB_RET_BABBLE
,
179 static const char *err_names
[] = {
180 [-LIBUSB_ERROR_IO
] = "IO",
181 [-LIBUSB_ERROR_INVALID_PARAM
] = "INVALID_PARAM",
182 [-LIBUSB_ERROR_ACCESS
] = "ACCESS",
183 [-LIBUSB_ERROR_NO_DEVICE
] = "NO_DEVICE",
184 [-LIBUSB_ERROR_NOT_FOUND
] = "NOT_FOUND",
185 [-LIBUSB_ERROR_BUSY
] = "BUSY",
186 [-LIBUSB_ERROR_TIMEOUT
] = "TIMEOUT",
187 [-LIBUSB_ERROR_OVERFLOW
] = "OVERFLOW",
188 [-LIBUSB_ERROR_PIPE
] = "PIPE",
189 [-LIBUSB_ERROR_INTERRUPTED
] = "INTERRUPTED",
190 [-LIBUSB_ERROR_NO_MEM
] = "NO_MEM",
191 [-LIBUSB_ERROR_NOT_SUPPORTED
] = "NOT_SUPPORTED",
192 [-LIBUSB_ERROR_OTHER
] = "OTHER",
195 static libusb_context
*ctx
;
196 static uint32_t loglevel
;
198 static void usb_host_handle_fd(void *opaque
)
200 struct timeval tv
= { 0, 0 };
201 libusb_handle_events_timeout(ctx
, &tv
);
204 static void usb_host_add_fd(int fd
, short events
, void *user_data
)
206 qemu_set_fd_handler(fd
,
207 (events
& POLLIN
) ? usb_host_handle_fd
: NULL
,
208 (events
& POLLOUT
) ? usb_host_handle_fd
: NULL
,
212 static void usb_host_del_fd(int fd
, void *user_data
)
214 qemu_set_fd_handler(fd
, NULL
, NULL
, NULL
);
217 static int usb_host_init(void)
219 const struct libusb_pollfd
**poll
;
225 rc
= libusb_init(&ctx
);
229 libusb_set_debug(ctx
, loglevel
);
231 libusb_set_pollfd_notifiers(ctx
, usb_host_add_fd
,
234 poll
= libusb_get_pollfds(ctx
);
236 for (i
= 0; poll
[i
] != NULL
; i
++) {
237 usb_host_add_fd(poll
[i
]->fd
, poll
[i
]->events
, ctx
);
244 static int usb_host_get_port(libusb_device
*dev
, char *port
, size_t len
)
250 #if LIBUSBX_API_VERSION >= 0x01000102
251 rc
= libusb_get_port_numbers(dev
, path
, 7);
253 rc
= libusb_get_port_path(ctx
, dev
, path
, 7);
258 off
= snprintf(port
, len
, "%d", path
[0]);
259 for (i
= 1; i
< rc
; i
++) {
260 off
+= snprintf(port
+off
, len
-off
, ".%d", path
[i
]);
265 static void usb_host_libusb_error(const char *func
, int rc
)
273 if (-rc
< ARRAY_SIZE(err_names
) && err_names
[-rc
]) {
274 errname
= err_names
[-rc
];
278 error_report("%s: %d [%s]", func
, rc
, errname
);
281 /* ------------------------------------------------------------------------ */
283 static bool usb_host_use_combining(USBEndpoint
*ep
)
290 if (ep
->pid
!= USB_TOKEN_IN
) {
293 type
= usb_ep_get_type(ep
->dev
, ep
->pid
, ep
->nr
);
294 if (type
!= USB_ENDPOINT_XFER_BULK
) {
300 /* ------------------------------------------------------------------------ */
302 static USBHostRequest
*usb_host_req_alloc(USBHostDevice
*s
, USBPacket
*p
,
303 bool in
, size_t bufsize
)
305 USBHostRequest
*r
= g_new0(USBHostRequest
, 1);
310 r
->xfer
= libusb_alloc_transfer(0);
312 r
->buffer
= g_malloc(bufsize
);
314 QTAILQ_INSERT_TAIL(&s
->requests
, r
, next
);
318 static void usb_host_req_free(USBHostRequest
*r
)
321 QTAILQ_REMOVE(&r
->host
->requests
, r
, next
);
323 libusb_free_transfer(r
->xfer
);
328 static USBHostRequest
*usb_host_req_find(USBHostDevice
*s
, USBPacket
*p
)
332 QTAILQ_FOREACH(r
, &s
->requests
, next
) {
340 static void usb_host_req_complete_ctrl(struct libusb_transfer
*xfer
)
342 USBHostRequest
*r
= xfer
->user_data
;
343 USBHostDevice
*s
= r
->host
;
344 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
347 goto out
; /* request was canceled */
350 r
->p
->status
= status_map
[xfer
->status
];
351 r
->p
->actual_length
= xfer
->actual_length
;
352 if (r
->in
&& xfer
->actual_length
) {
353 memcpy(r
->cbuf
, r
->buffer
+ 8, xfer
->actual_length
);
355 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
356 * to work redirected to a not superspeed capable hcd */
357 if (r
->usb3ep0quirk
&& xfer
->actual_length
>= 18 &&
362 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
363 r
->p
->status
, r
->p
->actual_length
);
364 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
367 usb_host_req_free(r
);
373 static void usb_host_req_complete_data(struct libusb_transfer
*xfer
)
375 USBHostRequest
*r
= xfer
->user_data
;
376 USBHostDevice
*s
= r
->host
;
377 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
380 goto out
; /* request was canceled */
383 r
->p
->status
= status_map
[xfer
->status
];
384 if (r
->in
&& xfer
->actual_length
) {
385 usb_packet_copy(r
->p
, r
->buffer
, xfer
->actual_length
);
387 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
388 r
->p
->status
, r
->p
->actual_length
);
389 if (usb_host_use_combining(r
->p
->ep
)) {
390 usb_combined_input_packet_complete(USB_DEVICE(s
), r
->p
);
392 usb_packet_complete(USB_DEVICE(s
), r
->p
);
396 usb_host_req_free(r
);
402 static void usb_host_req_abort(USBHostRequest
*r
)
404 USBHostDevice
*s
= r
->host
;
405 bool inflight
= (r
->p
&& r
->p
->state
== USB_PACKET_ASYNC
);
408 r
->p
->status
= USB_RET_NODEV
;
409 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
410 r
->p
->status
, r
->p
->actual_length
);
411 if (r
->p
->ep
->nr
== 0) {
412 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
414 usb_packet_complete(USB_DEVICE(s
), r
->p
);
419 QTAILQ_REMOVE(&r
->host
->requests
, r
, next
);
423 libusb_cancel_transfer(r
->xfer
);
427 /* ------------------------------------------------------------------------ */
429 static void usb_host_req_complete_iso(struct libusb_transfer
*transfer
)
431 USBHostIsoXfer
*xfer
= transfer
->user_data
;
434 /* USBHostIsoXfer released while inflight */
435 g_free(transfer
->buffer
);
436 libusb_free_transfer(transfer
);
440 QTAILQ_REMOVE(&xfer
->ring
->inflight
, xfer
, next
);
441 if (QTAILQ_EMPTY(&xfer
->ring
->inflight
)) {
442 USBHostDevice
*s
= xfer
->ring
->host
;
443 trace_usb_host_iso_stop(s
->bus_num
, s
->addr
, xfer
->ring
->ep
->nr
);
445 if (xfer
->ring
->ep
->pid
== USB_TOKEN_IN
) {
446 QTAILQ_INSERT_TAIL(&xfer
->ring
->copy
, xfer
, next
);
448 QTAILQ_INSERT_TAIL(&xfer
->ring
->unused
, xfer
, next
);
452 static USBHostIsoRing
*usb_host_iso_alloc(USBHostDevice
*s
, USBEndpoint
*ep
)
454 USBHostIsoRing
*ring
= g_new0(USBHostIsoRing
, 1);
455 USBHostIsoXfer
*xfer
;
456 /* FIXME: check interval (for now assume one xfer per frame) */
457 int packets
= s
->iso_urb_frames
;
462 QTAILQ_INIT(&ring
->unused
);
463 QTAILQ_INIT(&ring
->inflight
);
464 QTAILQ_INIT(&ring
->copy
);
465 QTAILQ_INSERT_TAIL(&s
->isorings
, ring
, next
);
467 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
468 xfer
= g_new0(USBHostIsoXfer
, 1);
470 xfer
->xfer
= libusb_alloc_transfer(packets
);
471 xfer
->xfer
->dev_handle
= s
->dh
;
472 xfer
->xfer
->type
= LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
;
474 xfer
->xfer
->endpoint
= ring
->ep
->nr
;
475 if (ring
->ep
->pid
== USB_TOKEN_IN
) {
476 xfer
->xfer
->endpoint
|= USB_DIR_IN
;
478 xfer
->xfer
->callback
= usb_host_req_complete_iso
;
479 xfer
->xfer
->user_data
= xfer
;
481 xfer
->xfer
->num_iso_packets
= packets
;
482 xfer
->xfer
->length
= ring
->ep
->max_packet_size
* packets
;
483 xfer
->xfer
->buffer
= g_malloc0(xfer
->xfer
->length
);
485 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
491 static USBHostIsoRing
*usb_host_iso_find(USBHostDevice
*s
, USBEndpoint
*ep
)
493 USBHostIsoRing
*ring
;
495 QTAILQ_FOREACH(ring
, &s
->isorings
, next
) {
496 if (ring
->ep
== ep
) {
503 static void usb_host_iso_reset_xfer(USBHostIsoXfer
*xfer
)
505 libusb_set_iso_packet_lengths(xfer
->xfer
,
506 xfer
->ring
->ep
->max_packet_size
);
508 xfer
->copy_complete
= false;
511 static void usb_host_iso_free_xfer(USBHostIsoXfer
*xfer
, bool inflight
)
514 xfer
->xfer
->user_data
= NULL
;
516 g_free(xfer
->xfer
->buffer
);
517 libusb_free_transfer(xfer
->xfer
);
522 static void usb_host_iso_free(USBHostIsoRing
*ring
)
524 USBHostIsoXfer
*xfer
;
526 while ((xfer
= QTAILQ_FIRST(&ring
->inflight
)) != NULL
) {
527 QTAILQ_REMOVE(&ring
->inflight
, xfer
, next
);
528 usb_host_iso_free_xfer(xfer
, true);
530 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
531 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
532 usb_host_iso_free_xfer(xfer
, false);
534 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
) {
535 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
536 usb_host_iso_free_xfer(xfer
, false);
539 QTAILQ_REMOVE(&ring
->host
->isorings
, ring
, next
);
543 static void usb_host_iso_free_all(USBHostDevice
*s
)
545 USBHostIsoRing
*ring
;
547 while ((ring
= QTAILQ_FIRST(&s
->isorings
)) != NULL
) {
548 usb_host_iso_free(ring
);
552 static bool usb_host_iso_data_copy(USBHostIsoXfer
*xfer
, USBPacket
*p
)
557 buf
= libusb_get_iso_packet_buffer_simple(xfer
->xfer
, xfer
->packet
);
558 if (p
->pid
== USB_TOKEN_OUT
) {
560 if (psize
> xfer
->ring
->ep
->max_packet_size
) {
561 /* should not happen (guest bug) */
562 psize
= xfer
->ring
->ep
->max_packet_size
;
564 xfer
->xfer
->iso_packet_desc
[xfer
->packet
].length
= psize
;
566 psize
= xfer
->xfer
->iso_packet_desc
[xfer
->packet
].actual_length
;
567 if (psize
> p
->iov
.size
) {
568 /* should not happen (guest bug) */
572 usb_packet_copy(p
, buf
, psize
);
574 xfer
->copy_complete
= (xfer
->packet
== xfer
->xfer
->num_iso_packets
);
575 return xfer
->copy_complete
;
578 static void usb_host_iso_data_in(USBHostDevice
*s
, USBPacket
*p
)
580 USBHostIsoRing
*ring
;
581 USBHostIsoXfer
*xfer
;
582 bool disconnect
= false;
585 ring
= usb_host_iso_find(s
, p
->ep
);
587 ring
= usb_host_iso_alloc(s
, p
->ep
);
590 /* copy data to guest */
591 xfer
= QTAILQ_FIRST(&ring
->copy
);
593 if (usb_host_iso_data_copy(xfer
, p
)) {
594 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
595 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
599 /* submit empty bufs to host */
600 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
601 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
602 usb_host_iso_reset_xfer(xfer
);
603 rc
= libusb_submit_transfer(xfer
->xfer
);
605 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
606 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
607 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
612 if (QTAILQ_EMPTY(&ring
->inflight
)) {
613 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
615 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
623 static void usb_host_iso_data_out(USBHostDevice
*s
, USBPacket
*p
)
625 USBHostIsoRing
*ring
;
626 USBHostIsoXfer
*xfer
;
627 bool disconnect
= false;
630 ring
= usb_host_iso_find(s
, p
->ep
);
632 ring
= usb_host_iso_alloc(s
, p
->ep
);
635 /* copy data from guest */
636 xfer
= QTAILQ_FIRST(&ring
->copy
);
637 while (xfer
!= NULL
&& xfer
->copy_complete
) {
639 xfer
= QTAILQ_NEXT(xfer
, next
);
642 xfer
= QTAILQ_FIRST(&ring
->unused
);
644 trace_usb_host_iso_out_of_bufs(s
->bus_num
, s
->addr
, p
->ep
->nr
);
647 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
648 usb_host_iso_reset_xfer(xfer
);
649 QTAILQ_INSERT_TAIL(&ring
->copy
, xfer
, next
);
651 usb_host_iso_data_copy(xfer
, p
);
653 if (QTAILQ_EMPTY(&ring
->inflight
)) {
654 /* wait until half of our buffers are filled
655 before kicking the iso out stream */
656 if (filled
*2 < s
->iso_urb_count
) {
661 /* submit filled bufs to host */
662 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
&&
663 xfer
->copy_complete
) {
664 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
665 rc
= libusb_submit_transfer(xfer
->xfer
);
667 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
668 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
669 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
674 if (QTAILQ_EMPTY(&ring
->inflight
)) {
675 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
677 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
685 /* ------------------------------------------------------------------------ */
687 static void usb_host_speed_compat(USBHostDevice
*s
)
689 USBDevice
*udev
= USB_DEVICE(s
);
690 struct libusb_config_descriptor
*conf
;
691 const struct libusb_interface_descriptor
*intf
;
692 const struct libusb_endpoint_descriptor
*endp
;
694 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
696 bool compat_high
= true;
697 bool compat_full
= true;
702 rc
= libusb_get_config_descriptor(s
->dev
, c
, &conf
);
706 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
707 for (a
= 0; a
< conf
->interface
[i
].num_altsetting
; a
++) {
708 intf
= &conf
->interface
[i
].altsetting
[a
];
709 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
710 endp
= &intf
->endpoint
[e
];
711 type
= endp
->bmAttributes
& 0x3;
717 case 0x02: /* BULK */
719 rc
= libusb_get_ss_endpoint_companion_descriptor
720 (ctx
, endp
, &endp_ss_comp
);
721 if (rc
== LIBUSB_SUCCESS
) {
722 libusb_free_ss_endpoint_companion_descriptor
729 case 0x03: /* INTERRUPT */
730 if (endp
->wMaxPacketSize
> 64) {
733 if (endp
->wMaxPacketSize
> 1024) {
741 libusb_free_config_descriptor(conf
);
744 udev
->speedmask
= (1 << udev
->speed
);
745 if (udev
->speed
== USB_SPEED_SUPER
&& compat_high
) {
746 udev
->speedmask
|= USB_SPEED_HIGH
;
748 if (udev
->speed
== USB_SPEED_SUPER
&& compat_full
) {
749 udev
->speedmask
|= USB_SPEED_FULL
;
751 if (udev
->speed
== USB_SPEED_HIGH
&& compat_full
) {
752 udev
->speedmask
|= USB_SPEED_FULL
;
756 static void usb_host_ep_update(USBHostDevice
*s
)
758 static const char *tname
[] = {
759 [USB_ENDPOINT_XFER_CONTROL
] = "control",
760 [USB_ENDPOINT_XFER_ISOC
] = "isoc",
761 [USB_ENDPOINT_XFER_BULK
] = "bulk",
762 [USB_ENDPOINT_XFER_INT
] = "int",
764 USBDevice
*udev
= USB_DEVICE(s
);
765 struct libusb_config_descriptor
*conf
;
766 const struct libusb_interface_descriptor
*intf
;
767 const struct libusb_endpoint_descriptor
*endp
;
769 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
776 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
780 trace_usb_host_parse_config(s
->bus_num
, s
->addr
,
781 conf
->bConfigurationValue
, true);
783 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
784 assert(udev
->altsetting
[i
] < conf
->interface
[i
].num_altsetting
);
785 intf
= &conf
->interface
[i
].altsetting
[udev
->altsetting
[i
]];
786 trace_usb_host_parse_interface(s
->bus_num
, s
->addr
,
787 intf
->bInterfaceNumber
,
788 intf
->bAlternateSetting
, true);
789 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
790 endp
= &intf
->endpoint
[e
];
792 devep
= endp
->bEndpointAddress
;
793 pid
= (devep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
795 type
= endp
->bmAttributes
& 0x3;
798 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
799 "invalid endpoint address");
802 if (usb_ep_get_type(udev
, pid
, ep
) != USB_ENDPOINT_XFER_INVALID
) {
803 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
804 "duplicate endpoint address");
808 trace_usb_host_parse_endpoint(s
->bus_num
, s
->addr
, ep
,
809 (devep
& USB_DIR_IN
) ? "in" : "out",
811 usb_ep_set_max_packet_size(udev
, pid
, ep
,
812 endp
->wMaxPacketSize
);
813 usb_ep_set_type(udev
, pid
, ep
, type
);
814 usb_ep_set_ifnum(udev
, pid
, ep
, i
);
815 usb_ep_set_halted(udev
, pid
, ep
, 0);
817 if (type
== LIBUSB_TRANSFER_TYPE_BULK
&&
818 libusb_get_ss_endpoint_companion_descriptor(ctx
, endp
,
819 &endp_ss_comp
) == LIBUSB_SUCCESS
) {
820 usb_ep_set_max_streams(udev
, pid
, ep
,
821 endp_ss_comp
->bmAttributes
);
822 libusb_free_ss_endpoint_companion_descriptor(endp_ss_comp
);
828 libusb_free_config_descriptor(conf
);
831 static int usb_host_open(USBHostDevice
*s
, libusb_device
*dev
)
833 USBDevice
*udev
= USB_DEVICE(s
);
834 int bus_num
= libusb_get_bus_number(dev
);
835 int addr
= libusb_get_device_address(dev
);
837 Error
*local_err
= NULL
;
839 trace_usb_host_open_started(bus_num
, addr
);
844 rc
= libusb_open(dev
, &s
->dh
);
850 s
->bus_num
= bus_num
;
853 usb_host_detach_kernel(s
);
855 libusb_get_device_descriptor(dev
, &s
->ddesc
);
856 usb_host_get_port(s
->dev
, s
->port
, sizeof(s
->port
));
859 usb_host_ep_update(s
);
861 udev
->speed
= speed_map
[libusb_get_device_speed(dev
)];
862 usb_host_speed_compat(s
);
864 if (s
->ddesc
.iProduct
) {
865 libusb_get_string_descriptor_ascii(s
->dh
, s
->ddesc
.iProduct
,
866 (unsigned char *)udev
->product_desc
,
867 sizeof(udev
->product_desc
));
869 snprintf(udev
->product_desc
, sizeof(udev
->product_desc
),
870 "host:%d.%d", bus_num
, addr
);
873 usb_device_attach(udev
, &local_err
);
875 error_report("%s", error_get_pretty(local_err
));
876 error_free(local_err
);
880 trace_usb_host_open_success(bus_num
, addr
);
884 trace_usb_host_open_failure(bus_num
, addr
);
893 static void usb_host_abort_xfers(USBHostDevice
*s
)
895 USBHostRequest
*r
, *rtmp
;
897 QTAILQ_FOREACH_SAFE(r
, &s
->requests
, next
, rtmp
) {
898 usb_host_req_abort(r
);
902 static int usb_host_close(USBHostDevice
*s
)
904 USBDevice
*udev
= USB_DEVICE(s
);
910 trace_usb_host_close(s
->bus_num
, s
->addr
);
912 usb_host_abort_xfers(s
);
913 usb_host_iso_free_all(s
);
915 if (udev
->attached
) {
916 usb_device_detach(udev
);
919 usb_host_release_interfaces(s
);
920 libusb_reset_device(s
->dh
);
921 usb_host_attach_kernel(s
);
926 usb_host_auto_check(NULL
);
930 static void usb_host_nodev_bh(void *opaque
)
932 USBHostDevice
*s
= opaque
;
936 static void usb_host_nodev(USBHostDevice
*s
)
939 s
->bh_nodev
= qemu_bh_new(usb_host_nodev_bh
, s
);
941 qemu_bh_schedule(s
->bh_nodev
);
944 static void usb_host_exit_notifier(struct Notifier
*n
, void *data
)
946 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
949 usb_host_release_interfaces(s
);
950 usb_host_attach_kernel(s
);
954 static void usb_host_realize(USBDevice
*udev
, Error
**errp
)
956 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
958 if (s
->match
.vendor_id
> 0xffff) {
959 error_setg(errp
, "vendorid out of range");
962 if (s
->match
.product_id
> 0xffff) {
963 error_setg(errp
, "productid out of range");
966 if (s
->match
.addr
> 127) {
967 error_setg(errp
, "hostaddr out of range");
971 loglevel
= s
->loglevel
;
972 udev
->flags
|= (1 << USB_DEV_FLAG_IS_HOST
);
973 udev
->auto_attach
= 0;
974 QTAILQ_INIT(&s
->requests
);
975 QTAILQ_INIT(&s
->isorings
);
977 s
->exit
.notify
= usb_host_exit_notifier
;
978 qemu_add_exit_notifier(&s
->exit
);
980 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
981 add_boot_device_path(s
->bootindex
, &udev
->qdev
, NULL
);
982 usb_host_auto_check(NULL
);
985 static void usb_host_handle_destroy(USBDevice
*udev
)
987 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
989 qemu_remove_exit_notifier(&s
->exit
);
990 QTAILQ_REMOVE(&hostdevs
, s
, next
);
994 static void usb_host_cancel_packet(USBDevice
*udev
, USBPacket
*p
)
996 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1000 usb_combined_packet_cancel(udev
, p
);
1004 trace_usb_host_req_canceled(s
->bus_num
, s
->addr
, p
);
1006 r
= usb_host_req_find(s
, p
);
1008 r
->p
= NULL
; /* mark as dead */
1009 libusb_cancel_transfer(r
->xfer
);
1013 static void usb_host_detach_kernel(USBHostDevice
*s
)
1015 struct libusb_config_descriptor
*conf
;
1018 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1022 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
1023 rc
= libusb_kernel_driver_active(s
->dh
, i
);
1024 usb_host_libusb_error("libusb_kernel_driver_active", rc
);
1028 trace_usb_host_detach_kernel(s
->bus_num
, s
->addr
, i
);
1029 rc
= libusb_detach_kernel_driver(s
->dh
, i
);
1030 usb_host_libusb_error("libusb_detach_kernel_driver", rc
);
1031 s
->ifs
[i
].detached
= true;
1033 libusb_free_config_descriptor(conf
);
1036 static void usb_host_attach_kernel(USBHostDevice
*s
)
1038 struct libusb_config_descriptor
*conf
;
1041 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1045 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
1046 if (!s
->ifs
[i
].detached
) {
1049 trace_usb_host_attach_kernel(s
->bus_num
, s
->addr
, i
);
1050 libusb_attach_kernel_driver(s
->dh
, i
);
1051 s
->ifs
[i
].detached
= false;
1053 libusb_free_config_descriptor(conf
);
1056 static int usb_host_claim_interfaces(USBHostDevice
*s
, int configuration
)
1058 USBDevice
*udev
= USB_DEVICE(s
);
1059 struct libusb_config_descriptor
*conf
;
1062 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1063 udev
->altsetting
[i
] = 0;
1065 udev
->ninterfaces
= 0;
1066 udev
->configuration
= 0;
1068 usb_host_detach_kernel(s
);
1070 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1072 if (rc
== LIBUSB_ERROR_NOT_FOUND
) {
1073 /* address state - ignore */
1074 return USB_RET_SUCCESS
;
1076 return USB_RET_STALL
;
1079 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
1080 trace_usb_host_claim_interface(s
->bus_num
, s
->addr
, configuration
, i
);
1081 rc
= libusb_claim_interface(s
->dh
, i
);
1082 usb_host_libusb_error("libusb_claim_interface", rc
);
1084 return USB_RET_STALL
;
1086 s
->ifs
[i
].claimed
= true;
1089 udev
->ninterfaces
= conf
->bNumInterfaces
;
1090 udev
->configuration
= configuration
;
1092 libusb_free_config_descriptor(conf
);
1093 return USB_RET_SUCCESS
;
1096 static void usb_host_release_interfaces(USBHostDevice
*s
)
1098 USBDevice
*udev
= USB_DEVICE(s
);
1101 for (i
= 0; i
< udev
->ninterfaces
; i
++) {
1102 if (!s
->ifs
[i
].claimed
) {
1105 trace_usb_host_release_interface(s
->bus_num
, s
->addr
, i
);
1106 rc
= libusb_release_interface(s
->dh
, i
);
1107 usb_host_libusb_error("libusb_release_interface", rc
);
1108 s
->ifs
[i
].claimed
= false;
1112 static void usb_host_set_address(USBHostDevice
*s
, int addr
)
1114 USBDevice
*udev
= USB_DEVICE(s
);
1116 trace_usb_host_set_address(s
->bus_num
, s
->addr
, addr
);
1120 static void usb_host_set_config(USBHostDevice
*s
, int config
, USBPacket
*p
)
1124 trace_usb_host_set_config(s
->bus_num
, s
->addr
, config
);
1126 usb_host_release_interfaces(s
);
1127 rc
= libusb_set_configuration(s
->dh
, config
);
1129 usb_host_libusb_error("libusb_set_configuration", rc
);
1130 p
->status
= USB_RET_STALL
;
1131 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1136 p
->status
= usb_host_claim_interfaces(s
, config
);
1137 if (p
->status
!= USB_RET_SUCCESS
) {
1140 usb_host_ep_update(s
);
1143 static void usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
,
1146 USBDevice
*udev
= USB_DEVICE(s
);
1149 trace_usb_host_set_interface(s
->bus_num
, s
->addr
, iface
, alt
);
1151 usb_host_iso_free_all(s
);
1153 if (iface
>= USB_MAX_INTERFACES
) {
1154 p
->status
= USB_RET_STALL
;
1158 rc
= libusb_set_interface_alt_setting(s
->dh
, iface
, alt
);
1160 usb_host_libusb_error("libusb_set_interface_alt_setting", rc
);
1161 p
->status
= USB_RET_STALL
;
1162 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1168 udev
->altsetting
[iface
] = alt
;
1169 usb_host_ep_update(s
);
1172 static void usb_host_handle_control(USBDevice
*udev
, USBPacket
*p
,
1173 int request
, int value
, int index
,
1174 int length
, uint8_t *data
)
1176 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1180 trace_usb_host_req_control(s
->bus_num
, s
->addr
, p
, request
, value
, index
);
1182 if (s
->dh
== NULL
) {
1183 p
->status
= USB_RET_NODEV
;
1184 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1189 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1190 usb_host_set_address(s
, value
);
1191 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1194 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1195 usb_host_set_config(s
, value
& 0xff, p
);
1196 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1199 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1200 usb_host_set_interface(s
, index
, value
, p
);
1201 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1204 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
1205 if (value
== 0) { /* clear halt */
1206 int pid
= (index
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1207 libusb_clear_halt(s
->dh
, index
);
1208 usb_ep_set_halted(udev
, pid
, index
& 0x0f, 0);
1209 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1214 r
= usb_host_req_alloc(s
, p
, (request
>> 8) & USB_DIR_IN
, length
+ 8);
1217 memcpy(r
->buffer
, udev
->setup_buf
, 8);
1219 memcpy(r
->buffer
+ 8, r
->cbuf
, r
->clen
);
1222 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
1223 * to work redirected to a not superspeed capable hcd */
1224 if (udev
->speed
== USB_SPEED_SUPER
&&
1225 !((udev
->port
->speedmask
& USB_SPEED_MASK_SUPER
)) &&
1226 request
== 0x8006 && value
== 0x100 && index
== 0) {
1227 r
->usb3ep0quirk
= true;
1230 libusb_fill_control_transfer(r
->xfer
, s
->dh
, r
->buffer
,
1231 usb_host_req_complete_ctrl
, r
,
1233 rc
= libusb_submit_transfer(r
->xfer
);
1235 p
->status
= USB_RET_NODEV
;
1236 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1237 p
->status
, p
->actual_length
);
1238 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1244 p
->status
= USB_RET_ASYNC
;
1247 static void usb_host_handle_data(USBDevice
*udev
, USBPacket
*p
)
1249 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1254 if (usb_host_use_combining(p
->ep
) && p
->state
== USB_PACKET_SETUP
) {
1255 p
->status
= USB_RET_ADD_TO_QUEUE
;
1259 trace_usb_host_req_data(s
->bus_num
, s
->addr
, p
,
1260 p
->pid
== USB_TOKEN_IN
,
1261 p
->ep
->nr
, p
->iov
.size
);
1263 if (s
->dh
== NULL
) {
1264 p
->status
= USB_RET_NODEV
;
1265 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1268 if (p
->ep
->halted
) {
1269 p
->status
= USB_RET_STALL
;
1270 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1274 switch (usb_ep_get_type(udev
, p
->pid
, p
->ep
->nr
)) {
1275 case USB_ENDPOINT_XFER_BULK
:
1276 size
= usb_packet_size(p
);
1277 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, size
);
1279 usb_packet_copy(p
, r
->buffer
, size
);
1281 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1284 libusb_fill_bulk_stream_transfer(r
->xfer
, s
->dh
, ep
, p
->stream
,
1286 usb_host_req_complete_data
, r
,
1289 usb_host_req_free(r
);
1290 p
->status
= USB_RET_STALL
;
1294 libusb_fill_bulk_transfer(r
->xfer
, s
->dh
, ep
,
1296 usb_host_req_complete_data
, r
,
1300 case USB_ENDPOINT_XFER_INT
:
1301 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, p
->iov
.size
);
1303 usb_packet_copy(p
, r
->buffer
, p
->iov
.size
);
1305 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1306 libusb_fill_interrupt_transfer(r
->xfer
, s
->dh
, ep
,
1307 r
->buffer
, p
->iov
.size
,
1308 usb_host_req_complete_data
, r
,
1311 case USB_ENDPOINT_XFER_ISOC
:
1312 if (p
->pid
== USB_TOKEN_IN
) {
1313 usb_host_iso_data_in(s
, p
);
1315 usb_host_iso_data_out(s
, p
);
1317 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1318 p
->status
, p
->actual_length
);
1321 p
->status
= USB_RET_STALL
;
1322 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1323 p
->status
, p
->actual_length
);
1327 rc
= libusb_submit_transfer(r
->xfer
);
1329 p
->status
= USB_RET_NODEV
;
1330 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1331 p
->status
, p
->actual_length
);
1332 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1338 p
->status
= USB_RET_ASYNC
;
1341 static void usb_host_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
1343 if (usb_host_use_combining(ep
)) {
1344 usb_ep_combine_input_packets(ep
);
1348 static void usb_host_handle_reset(USBDevice
*udev
)
1350 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1353 trace_usb_host_reset(s
->bus_num
, s
->addr
);
1355 rc
= libusb_reset_device(s
->dh
);
1361 static int usb_host_alloc_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1362 int nr_eps
, int streams
)
1365 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1366 unsigned char endpoints
[30];
1369 for (i
= 0; i
< nr_eps
; i
++) {
1370 endpoints
[i
] = eps
[i
]->nr
;
1371 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1372 endpoints
[i
] |= 0x80;
1375 rc
= libusb_alloc_streams(s
->dh
, streams
, endpoints
, nr_eps
);
1377 usb_host_libusb_error("libusb_alloc_streams", rc
);
1378 } else if (rc
!= streams
) {
1379 error_report("libusb_alloc_streams: got less streams "
1380 "then requested %d < %d", rc
, streams
);
1383 return (rc
== streams
) ? 0 : -1;
1385 error_report("libusb_alloc_streams: error not implemented");
1390 static void usb_host_free_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1394 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1395 unsigned char endpoints
[30];
1398 for (i
= 0; i
< nr_eps
; i
++) {
1399 endpoints
[i
] = eps
[i
]->nr
;
1400 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1401 endpoints
[i
] |= 0x80;
1404 libusb_free_streams(s
->dh
, endpoints
, nr_eps
);
1409 * This is *NOT* about restoring state. We have absolutely no idea
1410 * what state the host device is in at the moment and whenever it is
1411 * still present in the first place. Attemping to contine where we
1412 * left off is impossible.
1414 * What we are going to to to here is emulate a surprise removal of
1415 * the usb device passed through, then kick host scan so the device
1416 * will get re-attached (and re-initialized by the guest) in case it
1419 * As the device removal will change the state of other devices (usb
1420 * host controller, most likely interrupt controller too) we have to
1421 * wait with it until *all* vmstate is loaded. Thus post_load just
1422 * kicks a bottom half which then does the actual work.
1424 static void usb_host_post_load_bh(void *opaque
)
1426 USBHostDevice
*dev
= opaque
;
1427 USBDevice
*udev
= USB_DEVICE(dev
);
1429 if (dev
->dh
!= NULL
) {
1430 usb_host_close(dev
);
1432 if (udev
->attached
) {
1433 usb_device_detach(udev
);
1435 usb_host_auto_check(NULL
);
1438 static int usb_host_post_load(void *opaque
, int version_id
)
1440 USBHostDevice
*dev
= opaque
;
1442 if (!dev
->bh_postld
) {
1443 dev
->bh_postld
= qemu_bh_new(usb_host_post_load_bh
, dev
);
1445 qemu_bh_schedule(dev
->bh_postld
);
1449 static const VMStateDescription vmstate_usb_host
= {
1452 .minimum_version_id
= 1,
1453 .post_load
= usb_host_post_load
,
1454 .fields
= (VMStateField
[]) {
1455 VMSTATE_USB_DEVICE(parent_obj
, USBHostDevice
),
1456 VMSTATE_END_OF_LIST()
1460 static Property usb_host_dev_properties
[] = {
1461 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1462 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1463 DEFINE_PROP_STRING("hostport", USBHostDevice
, match
.port
),
1464 DEFINE_PROP_UINT32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1465 DEFINE_PROP_UINT32("productid", USBHostDevice
, match
.product_id
, 0),
1466 DEFINE_PROP_UINT32("isobufs", USBHostDevice
, iso_urb_count
, 4),
1467 DEFINE_PROP_UINT32("isobsize", USBHostDevice
, iso_urb_frames
, 32),
1468 DEFINE_PROP_INT32("bootindex", USBHostDevice
, bootindex
, -1),
1469 DEFINE_PROP_UINT32("loglevel", USBHostDevice
, loglevel
,
1470 LIBUSB_LOG_LEVEL_WARNING
),
1471 DEFINE_PROP_BIT("pipeline", USBHostDevice
, options
,
1472 USB_HOST_OPT_PIPELINE
, true),
1473 DEFINE_PROP_END_OF_LIST(),
1476 static void usb_host_class_initfn(ObjectClass
*klass
, void *data
)
1478 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1479 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1481 uc
->realize
= usb_host_realize
;
1482 uc
->product_desc
= "USB Host Device";
1483 uc
->cancel_packet
= usb_host_cancel_packet
;
1484 uc
->handle_data
= usb_host_handle_data
;
1485 uc
->handle_control
= usb_host_handle_control
;
1486 uc
->handle_reset
= usb_host_handle_reset
;
1487 uc
->handle_destroy
= usb_host_handle_destroy
;
1488 uc
->flush_ep_queue
= usb_host_flush_ep_queue
;
1489 uc
->alloc_streams
= usb_host_alloc_streams
;
1490 uc
->free_streams
= usb_host_free_streams
;
1491 dc
->vmsd
= &vmstate_usb_host
;
1492 dc
->props
= usb_host_dev_properties
;
1493 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
1496 static TypeInfo usb_host_dev_info
= {
1497 .name
= TYPE_USB_HOST_DEVICE
,
1498 .parent
= TYPE_USB_DEVICE
,
1499 .instance_size
= sizeof(USBHostDevice
),
1500 .class_init
= usb_host_class_initfn
,
1503 static void usb_host_register_types(void)
1505 type_register_static(&usb_host_dev_info
);
1508 type_init(usb_host_register_types
)
1510 /* ------------------------------------------------------------------------ */
1512 static QEMUTimer
*usb_auto_timer
;
1513 static VMChangeStateEntry
*usb_vmstate
;
1515 static void usb_host_vm_state(void *unused
, int running
, RunState state
)
1518 usb_host_auto_check(unused
);
1522 static void usb_host_auto_check(void *unused
)
1524 struct USBHostDevice
*s
;
1525 struct USBAutoFilter
*f
;
1526 libusb_device
**devs
= NULL
;
1527 struct libusb_device_descriptor ddesc
;
1528 int unconnected
= 0;
1531 if (usb_host_init() != 0) {
1535 if (runstate_is_running()) {
1536 n
= libusb_get_device_list(ctx
, &devs
);
1537 for (i
= 0; i
< n
; i
++) {
1538 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1541 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1544 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1546 if (f
->bus_num
> 0 &&
1547 f
->bus_num
!= libusb_get_bus_number(devs
[i
])) {
1551 f
->addr
!= libusb_get_device_address(devs
[i
])) {
1554 if (f
->port
!= NULL
) {
1555 char port
[16] = "-";
1556 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1557 if (strcmp(f
->port
, port
) != 0) {
1561 if (f
->vendor_id
> 0 &&
1562 f
->vendor_id
!= ddesc
.idVendor
) {
1565 if (f
->product_id
> 0 &&
1566 f
->product_id
!= ddesc
.idProduct
) {
1570 /* We got a match */
1572 if (s
->errcount
>= 3) {
1575 if (s
->dh
!= NULL
) {
1578 if (usb_host_open(s
, devs
[i
]) < 0) {
1585 libusb_free_device_list(devs
, 1);
1587 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1588 if (s
->dh
== NULL
) {
1601 if (unconnected
== 0) {
1602 /* nothing to watch */
1603 if (usb_auto_timer
) {
1604 timer_del(usb_auto_timer
);
1605 trace_usb_host_auto_scan_disabled();
1613 usb_vmstate
= qemu_add_vm_change_state_handler(usb_host_vm_state
, NULL
);
1615 if (!usb_auto_timer
) {
1616 usb_auto_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, usb_host_auto_check
, NULL
);
1617 if (!usb_auto_timer
) {
1620 trace_usb_host_auto_scan_enabled();
1622 timer_mod(usb_auto_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 2000);
1625 void usb_host_info(Monitor
*mon
, const QDict
*qdict
)
1627 libusb_device
**devs
= NULL
;
1628 struct libusb_device_descriptor ddesc
;
1632 if (usb_host_init() != 0) {
1636 n
= libusb_get_device_list(ctx
, &devs
);
1637 for (i
= 0; i
< n
; i
++) {
1638 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1641 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1644 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1645 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1646 libusb_get_bus_number(devs
[i
]),
1647 libusb_get_device_address(devs
[i
]),
1649 speed_name
[libusb_get_device_speed(devs
[i
])]);
1650 monitor_printf(mon
, " Class %02x:", ddesc
.bDeviceClass
);
1651 monitor_printf(mon
, " USB device %04x:%04x",
1652 ddesc
.idVendor
, ddesc
.idProduct
);
1653 if (ddesc
.iProduct
) {
1654 libusb_device_handle
*handle
;
1655 if (libusb_open(devs
[i
], &handle
) == 0) {
1656 unsigned char name
[64] = "";
1657 libusb_get_string_descriptor_ascii(handle
,
1659 name
, sizeof(name
));
1660 libusb_close(handle
);
1661 monitor_printf(mon
, ", %s", name
);
1664 monitor_printf(mon
, "\n");
1666 libusb_free_device_list(devs
, 1);