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
);
454 usb_wakeup(xfer
->ring
->ep
, 0);
456 QTAILQ_INSERT_TAIL(&xfer
->ring
->unused
, xfer
, next
);
460 static USBHostIsoRing
*usb_host_iso_alloc(USBHostDevice
*s
, USBEndpoint
*ep
)
462 USBHostIsoRing
*ring
= g_new0(USBHostIsoRing
, 1);
463 USBHostIsoXfer
*xfer
;
464 /* FIXME: check interval (for now assume one xfer per frame) */
465 int packets
= s
->iso_urb_frames
;
470 QTAILQ_INIT(&ring
->unused
);
471 QTAILQ_INIT(&ring
->inflight
);
472 QTAILQ_INIT(&ring
->copy
);
473 QTAILQ_INSERT_TAIL(&s
->isorings
, ring
, next
);
475 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
476 xfer
= g_new0(USBHostIsoXfer
, 1);
478 xfer
->xfer
= libusb_alloc_transfer(packets
);
479 xfer
->xfer
->dev_handle
= s
->dh
;
480 xfer
->xfer
->type
= LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
;
482 xfer
->xfer
->endpoint
= ring
->ep
->nr
;
483 if (ring
->ep
->pid
== USB_TOKEN_IN
) {
484 xfer
->xfer
->endpoint
|= USB_DIR_IN
;
486 xfer
->xfer
->callback
= usb_host_req_complete_iso
;
487 xfer
->xfer
->user_data
= xfer
;
489 xfer
->xfer
->num_iso_packets
= packets
;
490 xfer
->xfer
->length
= ring
->ep
->max_packet_size
* packets
;
491 xfer
->xfer
->buffer
= g_malloc0(xfer
->xfer
->length
);
493 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
499 static USBHostIsoRing
*usb_host_iso_find(USBHostDevice
*s
, USBEndpoint
*ep
)
501 USBHostIsoRing
*ring
;
503 QTAILQ_FOREACH(ring
, &s
->isorings
, next
) {
504 if (ring
->ep
== ep
) {
511 static void usb_host_iso_reset_xfer(USBHostIsoXfer
*xfer
)
513 libusb_set_iso_packet_lengths(xfer
->xfer
,
514 xfer
->ring
->ep
->max_packet_size
);
516 xfer
->copy_complete
= false;
519 static void usb_host_iso_free_xfer(USBHostIsoXfer
*xfer
, bool inflight
)
522 xfer
->xfer
->user_data
= NULL
;
524 g_free(xfer
->xfer
->buffer
);
525 libusb_free_transfer(xfer
->xfer
);
530 static void usb_host_iso_free(USBHostIsoRing
*ring
)
532 USBHostIsoXfer
*xfer
;
534 while ((xfer
= QTAILQ_FIRST(&ring
->inflight
)) != NULL
) {
535 QTAILQ_REMOVE(&ring
->inflight
, xfer
, next
);
536 usb_host_iso_free_xfer(xfer
, true);
538 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
539 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
540 usb_host_iso_free_xfer(xfer
, false);
542 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
) {
543 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
544 usb_host_iso_free_xfer(xfer
, false);
547 QTAILQ_REMOVE(&ring
->host
->isorings
, ring
, next
);
551 static void usb_host_iso_free_all(USBHostDevice
*s
)
553 USBHostIsoRing
*ring
;
555 while ((ring
= QTAILQ_FIRST(&s
->isorings
)) != NULL
) {
556 usb_host_iso_free(ring
);
560 static bool usb_host_iso_data_copy(USBHostIsoXfer
*xfer
, USBPacket
*p
)
565 buf
= libusb_get_iso_packet_buffer_simple(xfer
->xfer
, xfer
->packet
);
566 if (p
->pid
== USB_TOKEN_OUT
) {
568 if (psize
> xfer
->ring
->ep
->max_packet_size
) {
569 /* should not happen (guest bug) */
570 psize
= xfer
->ring
->ep
->max_packet_size
;
572 xfer
->xfer
->iso_packet_desc
[xfer
->packet
].length
= psize
;
574 psize
= xfer
->xfer
->iso_packet_desc
[xfer
->packet
].actual_length
;
575 if (psize
> p
->iov
.size
) {
576 /* should not happen (guest bug) */
580 usb_packet_copy(p
, buf
, psize
);
582 xfer
->copy_complete
= (xfer
->packet
== xfer
->xfer
->num_iso_packets
);
583 return xfer
->copy_complete
;
586 static void usb_host_iso_data_in(USBHostDevice
*s
, USBPacket
*p
)
588 USBHostIsoRing
*ring
;
589 USBHostIsoXfer
*xfer
;
590 bool disconnect
= false;
593 ring
= usb_host_iso_find(s
, p
->ep
);
595 ring
= usb_host_iso_alloc(s
, p
->ep
);
598 /* copy data to guest */
599 xfer
= QTAILQ_FIRST(&ring
->copy
);
601 if (usb_host_iso_data_copy(xfer
, p
)) {
602 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
603 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
607 /* submit empty bufs to host */
608 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
609 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
610 usb_host_iso_reset_xfer(xfer
);
611 rc
= libusb_submit_transfer(xfer
->xfer
);
613 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
614 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
615 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
620 if (QTAILQ_EMPTY(&ring
->inflight
)) {
621 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
623 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
631 static void usb_host_iso_data_out(USBHostDevice
*s
, USBPacket
*p
)
633 USBHostIsoRing
*ring
;
634 USBHostIsoXfer
*xfer
;
635 bool disconnect
= false;
638 ring
= usb_host_iso_find(s
, p
->ep
);
640 ring
= usb_host_iso_alloc(s
, p
->ep
);
643 /* copy data from guest */
644 xfer
= QTAILQ_FIRST(&ring
->copy
);
645 while (xfer
!= NULL
&& xfer
->copy_complete
) {
647 xfer
= QTAILQ_NEXT(xfer
, next
);
650 xfer
= QTAILQ_FIRST(&ring
->unused
);
652 trace_usb_host_iso_out_of_bufs(s
->bus_num
, s
->addr
, p
->ep
->nr
);
655 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
656 usb_host_iso_reset_xfer(xfer
);
657 QTAILQ_INSERT_TAIL(&ring
->copy
, xfer
, next
);
659 usb_host_iso_data_copy(xfer
, p
);
661 if (QTAILQ_EMPTY(&ring
->inflight
)) {
662 /* wait until half of our buffers are filled
663 before kicking the iso out stream */
664 if (filled
*2 < s
->iso_urb_count
) {
669 /* submit filled bufs to host */
670 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
&&
671 xfer
->copy_complete
) {
672 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
673 rc
= libusb_submit_transfer(xfer
->xfer
);
675 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
676 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
677 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
682 if (QTAILQ_EMPTY(&ring
->inflight
)) {
683 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
685 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
693 /* ------------------------------------------------------------------------ */
695 static void usb_host_speed_compat(USBHostDevice
*s
)
697 USBDevice
*udev
= USB_DEVICE(s
);
698 struct libusb_config_descriptor
*conf
;
699 const struct libusb_interface_descriptor
*intf
;
700 const struct libusb_endpoint_descriptor
*endp
;
702 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
704 bool compat_high
= true;
705 bool compat_full
= true;
710 rc
= libusb_get_config_descriptor(s
->dev
, c
, &conf
);
714 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
715 for (a
= 0; a
< conf
->interface
[i
].num_altsetting
; a
++) {
716 intf
= &conf
->interface
[i
].altsetting
[a
];
717 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
718 endp
= &intf
->endpoint
[e
];
719 type
= endp
->bmAttributes
& 0x3;
725 case 0x02: /* BULK */
727 rc
= libusb_get_ss_endpoint_companion_descriptor
728 (ctx
, endp
, &endp_ss_comp
);
729 if (rc
== LIBUSB_SUCCESS
) {
730 libusb_free_ss_endpoint_companion_descriptor
737 case 0x03: /* INTERRUPT */
738 if (endp
->wMaxPacketSize
> 64) {
741 if (endp
->wMaxPacketSize
> 1024) {
749 libusb_free_config_descriptor(conf
);
752 udev
->speedmask
= (1 << udev
->speed
);
753 if (udev
->speed
== USB_SPEED_SUPER
&& compat_high
) {
754 udev
->speedmask
|= USB_SPEED_MASK_HIGH
;
756 if (udev
->speed
== USB_SPEED_SUPER
&& compat_full
) {
757 udev
->speedmask
|= USB_SPEED_MASK_FULL
;
759 if (udev
->speed
== USB_SPEED_HIGH
&& compat_full
) {
760 udev
->speedmask
|= USB_SPEED_MASK_FULL
;
764 static void usb_host_ep_update(USBHostDevice
*s
)
766 static const char *tname
[] = {
767 [USB_ENDPOINT_XFER_CONTROL
] = "control",
768 [USB_ENDPOINT_XFER_ISOC
] = "isoc",
769 [USB_ENDPOINT_XFER_BULK
] = "bulk",
770 [USB_ENDPOINT_XFER_INT
] = "int",
772 USBDevice
*udev
= USB_DEVICE(s
);
773 struct libusb_config_descriptor
*conf
;
774 const struct libusb_interface_descriptor
*intf
;
775 const struct libusb_endpoint_descriptor
*endp
;
777 struct libusb_ss_endpoint_companion_descriptor
*endp_ss_comp
;
784 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
788 trace_usb_host_parse_config(s
->bus_num
, s
->addr
,
789 conf
->bConfigurationValue
, true);
791 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
792 assert(udev
->altsetting
[i
] < conf
->interface
[i
].num_altsetting
);
793 intf
= &conf
->interface
[i
].altsetting
[udev
->altsetting
[i
]];
794 trace_usb_host_parse_interface(s
->bus_num
, s
->addr
,
795 intf
->bInterfaceNumber
,
796 intf
->bAlternateSetting
, true);
797 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
798 endp
= &intf
->endpoint
[e
];
800 devep
= endp
->bEndpointAddress
;
801 pid
= (devep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
803 type
= endp
->bmAttributes
& 0x3;
806 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
807 "invalid endpoint address");
810 if (usb_ep_get_type(udev
, pid
, ep
) != USB_ENDPOINT_XFER_INVALID
) {
811 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
812 "duplicate endpoint address");
816 trace_usb_host_parse_endpoint(s
->bus_num
, s
->addr
, ep
,
817 (devep
& USB_DIR_IN
) ? "in" : "out",
819 usb_ep_set_max_packet_size(udev
, pid
, ep
,
820 endp
->wMaxPacketSize
);
821 usb_ep_set_type(udev
, pid
, ep
, type
);
822 usb_ep_set_ifnum(udev
, pid
, ep
, i
);
823 usb_ep_set_halted(udev
, pid
, ep
, 0);
825 if (type
== LIBUSB_TRANSFER_TYPE_BULK
&&
826 libusb_get_ss_endpoint_companion_descriptor(ctx
, endp
,
827 &endp_ss_comp
) == LIBUSB_SUCCESS
) {
828 usb_ep_set_max_streams(udev
, pid
, ep
,
829 endp_ss_comp
->bmAttributes
);
830 libusb_free_ss_endpoint_companion_descriptor(endp_ss_comp
);
836 libusb_free_config_descriptor(conf
);
839 static int usb_host_open(USBHostDevice
*s
, libusb_device
*dev
)
841 USBDevice
*udev
= USB_DEVICE(s
);
842 int bus_num
= libusb_get_bus_number(dev
);
843 int addr
= libusb_get_device_address(dev
);
845 Error
*local_err
= NULL
;
847 trace_usb_host_open_started(bus_num
, addr
);
852 rc
= libusb_open(dev
, &s
->dh
);
858 s
->bus_num
= bus_num
;
861 usb_host_detach_kernel(s
);
863 libusb_get_device_descriptor(dev
, &s
->ddesc
);
864 usb_host_get_port(s
->dev
, s
->port
, sizeof(s
->port
));
867 usb_host_ep_update(s
);
869 udev
->speed
= speed_map
[libusb_get_device_speed(dev
)];
870 usb_host_speed_compat(s
);
872 if (s
->ddesc
.iProduct
) {
873 libusb_get_string_descriptor_ascii(s
->dh
, s
->ddesc
.iProduct
,
874 (unsigned char *)udev
->product_desc
,
875 sizeof(udev
->product_desc
));
877 snprintf(udev
->product_desc
, sizeof(udev
->product_desc
),
878 "host:%d.%d", bus_num
, addr
);
881 usb_device_attach(udev
, &local_err
);
883 error_report_err(local_err
);
887 trace_usb_host_open_success(bus_num
, addr
);
891 trace_usb_host_open_failure(bus_num
, addr
);
893 usb_host_release_interfaces(s
);
894 libusb_reset_device(s
->dh
);
895 usb_host_attach_kernel(s
);
903 static void usb_host_abort_xfers(USBHostDevice
*s
)
905 USBHostRequest
*r
, *rtmp
;
907 QTAILQ_FOREACH_SAFE(r
, &s
->requests
, next
, rtmp
) {
908 usb_host_req_abort(r
);
912 static int usb_host_close(USBHostDevice
*s
)
914 USBDevice
*udev
= USB_DEVICE(s
);
920 trace_usb_host_close(s
->bus_num
, s
->addr
);
922 usb_host_abort_xfers(s
);
923 usb_host_iso_free_all(s
);
925 if (udev
->attached
) {
926 usb_device_detach(udev
);
929 usb_host_release_interfaces(s
);
930 libusb_reset_device(s
->dh
);
931 usb_host_attach_kernel(s
);
936 usb_host_auto_check(NULL
);
940 static void usb_host_nodev_bh(void *opaque
)
942 USBHostDevice
*s
= opaque
;
946 static void usb_host_nodev(USBHostDevice
*s
)
949 s
->bh_nodev
= qemu_bh_new(usb_host_nodev_bh
, s
);
951 qemu_bh_schedule(s
->bh_nodev
);
954 static void usb_host_exit_notifier(struct Notifier
*n
, void *data
)
956 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
959 usb_host_release_interfaces(s
);
960 usb_host_attach_kernel(s
);
964 static void usb_host_realize(USBDevice
*udev
, Error
**errp
)
966 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
968 if (s
->match
.vendor_id
> 0xffff) {
969 error_setg(errp
, "vendorid out of range");
972 if (s
->match
.product_id
> 0xffff) {
973 error_setg(errp
, "productid out of range");
976 if (s
->match
.addr
> 127) {
977 error_setg(errp
, "hostaddr out of range");
981 loglevel
= s
->loglevel
;
982 udev
->flags
|= (1 << USB_DEV_FLAG_IS_HOST
);
983 udev
->auto_attach
= 0;
984 QTAILQ_INIT(&s
->requests
);
985 QTAILQ_INIT(&s
->isorings
);
987 s
->exit
.notify
= usb_host_exit_notifier
;
988 qemu_add_exit_notifier(&s
->exit
);
990 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
991 usb_host_auto_check(NULL
);
994 static void usb_host_instance_init(Object
*obj
)
996 USBDevice
*udev
= USB_DEVICE(obj
);
997 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
999 device_add_bootindex_property(obj
, &s
->bootindex
,
1004 static void usb_host_handle_destroy(USBDevice
*udev
)
1006 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1008 qemu_remove_exit_notifier(&s
->exit
);
1009 QTAILQ_REMOVE(&hostdevs
, s
, next
);
1013 static void usb_host_cancel_packet(USBDevice
*udev
, USBPacket
*p
)
1015 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1019 usb_combined_packet_cancel(udev
, p
);
1023 trace_usb_host_req_canceled(s
->bus_num
, s
->addr
, p
);
1025 r
= usb_host_req_find(s
, p
);
1027 r
->p
= NULL
; /* mark as dead */
1028 libusb_cancel_transfer(r
->xfer
);
1032 static void usb_host_detach_kernel(USBHostDevice
*s
)
1034 struct libusb_config_descriptor
*conf
;
1037 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1041 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
1042 rc
= libusb_kernel_driver_active(s
->dh
, i
);
1043 usb_host_libusb_error("libusb_kernel_driver_active", rc
);
1047 trace_usb_host_detach_kernel(s
->bus_num
, s
->addr
, i
);
1048 rc
= libusb_detach_kernel_driver(s
->dh
, i
);
1049 usb_host_libusb_error("libusb_detach_kernel_driver", rc
);
1050 s
->ifs
[i
].detached
= true;
1052 libusb_free_config_descriptor(conf
);
1055 static void usb_host_attach_kernel(USBHostDevice
*s
)
1057 struct libusb_config_descriptor
*conf
;
1060 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1064 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
1065 if (!s
->ifs
[i
].detached
) {
1068 trace_usb_host_attach_kernel(s
->bus_num
, s
->addr
, i
);
1069 libusb_attach_kernel_driver(s
->dh
, i
);
1070 s
->ifs
[i
].detached
= false;
1072 libusb_free_config_descriptor(conf
);
1075 static int usb_host_claim_interfaces(USBHostDevice
*s
, int configuration
)
1077 USBDevice
*udev
= USB_DEVICE(s
);
1078 struct libusb_config_descriptor
*conf
;
1081 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
1082 udev
->altsetting
[i
] = 0;
1084 udev
->ninterfaces
= 0;
1085 udev
->configuration
= 0;
1087 usb_host_detach_kernel(s
);
1089 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1091 if (rc
== LIBUSB_ERROR_NOT_FOUND
) {
1092 /* address state - ignore */
1093 return USB_RET_SUCCESS
;
1095 return USB_RET_STALL
;
1098 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
1099 trace_usb_host_claim_interface(s
->bus_num
, s
->addr
, configuration
, i
);
1100 rc
= libusb_claim_interface(s
->dh
, i
);
1101 usb_host_libusb_error("libusb_claim_interface", rc
);
1103 return USB_RET_STALL
;
1105 s
->ifs
[i
].claimed
= true;
1108 udev
->ninterfaces
= conf
->bNumInterfaces
;
1109 udev
->configuration
= configuration
;
1111 libusb_free_config_descriptor(conf
);
1112 return USB_RET_SUCCESS
;
1115 static void usb_host_release_interfaces(USBHostDevice
*s
)
1117 USBDevice
*udev
= USB_DEVICE(s
);
1120 for (i
= 0; i
< udev
->ninterfaces
; i
++) {
1121 if (!s
->ifs
[i
].claimed
) {
1124 trace_usb_host_release_interface(s
->bus_num
, s
->addr
, i
);
1125 rc
= libusb_release_interface(s
->dh
, i
);
1126 usb_host_libusb_error("libusb_release_interface", rc
);
1127 s
->ifs
[i
].claimed
= false;
1131 static void usb_host_set_address(USBHostDevice
*s
, int addr
)
1133 USBDevice
*udev
= USB_DEVICE(s
);
1135 trace_usb_host_set_address(s
->bus_num
, s
->addr
, addr
);
1139 static void usb_host_set_config(USBHostDevice
*s
, int config
, USBPacket
*p
)
1143 trace_usb_host_set_config(s
->bus_num
, s
->addr
, config
);
1145 usb_host_release_interfaces(s
);
1146 rc
= libusb_set_configuration(s
->dh
, config
);
1148 usb_host_libusb_error("libusb_set_configuration", rc
);
1149 p
->status
= USB_RET_STALL
;
1150 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1155 p
->status
= usb_host_claim_interfaces(s
, config
);
1156 if (p
->status
!= USB_RET_SUCCESS
) {
1159 usb_host_ep_update(s
);
1162 static void usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
,
1165 USBDevice
*udev
= USB_DEVICE(s
);
1168 trace_usb_host_set_interface(s
->bus_num
, s
->addr
, iface
, alt
);
1170 usb_host_iso_free_all(s
);
1172 if (iface
>= USB_MAX_INTERFACES
) {
1173 p
->status
= USB_RET_STALL
;
1177 rc
= libusb_set_interface_alt_setting(s
->dh
, iface
, alt
);
1179 usb_host_libusb_error("libusb_set_interface_alt_setting", rc
);
1180 p
->status
= USB_RET_STALL
;
1181 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1187 udev
->altsetting
[iface
] = alt
;
1188 usb_host_ep_update(s
);
1191 static void usb_host_handle_control(USBDevice
*udev
, USBPacket
*p
,
1192 int request
, int value
, int index
,
1193 int length
, uint8_t *data
)
1195 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1199 trace_usb_host_req_control(s
->bus_num
, s
->addr
, p
, request
, value
, index
);
1201 if (s
->dh
== NULL
) {
1202 p
->status
= USB_RET_NODEV
;
1203 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1208 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1209 usb_host_set_address(s
, value
);
1210 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1213 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1214 usb_host_set_config(s
, value
& 0xff, p
);
1215 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1218 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1219 usb_host_set_interface(s
, index
, value
, p
);
1220 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1223 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
1224 if (value
== 0) { /* clear halt */
1225 int pid
= (index
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1226 libusb_clear_halt(s
->dh
, index
);
1227 usb_ep_set_halted(udev
, pid
, index
& 0x0f, 0);
1228 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1233 r
= usb_host_req_alloc(s
, p
, (request
>> 8) & USB_DIR_IN
, length
+ 8);
1236 memcpy(r
->buffer
, udev
->setup_buf
, 8);
1238 memcpy(r
->buffer
+ 8, r
->cbuf
, r
->clen
);
1241 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
1242 * to work redirected to a not superspeed capable hcd */
1243 if ((udev
->speedmask
& USB_SPEED_MASK_SUPER
) &&
1244 !(udev
->port
->speedmask
& USB_SPEED_MASK_SUPER
) &&
1245 request
== 0x8006 && value
== 0x100 && index
== 0) {
1246 r
->usb3ep0quirk
= true;
1249 libusb_fill_control_transfer(r
->xfer
, s
->dh
, r
->buffer
,
1250 usb_host_req_complete_ctrl
, r
,
1252 rc
= libusb_submit_transfer(r
->xfer
);
1254 p
->status
= USB_RET_NODEV
;
1255 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1256 p
->status
, p
->actual_length
);
1257 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1263 p
->status
= USB_RET_ASYNC
;
1266 static void usb_host_handle_data(USBDevice
*udev
, USBPacket
*p
)
1268 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1273 if (usb_host_use_combining(p
->ep
) && p
->state
== USB_PACKET_SETUP
) {
1274 p
->status
= USB_RET_ADD_TO_QUEUE
;
1278 trace_usb_host_req_data(s
->bus_num
, s
->addr
, p
,
1279 p
->pid
== USB_TOKEN_IN
,
1280 p
->ep
->nr
, p
->iov
.size
);
1282 if (s
->dh
== NULL
) {
1283 p
->status
= USB_RET_NODEV
;
1284 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1287 if (p
->ep
->halted
) {
1288 p
->status
= USB_RET_STALL
;
1289 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1293 switch (usb_ep_get_type(udev
, p
->pid
, p
->ep
->nr
)) {
1294 case USB_ENDPOINT_XFER_BULK
:
1295 size
= usb_packet_size(p
);
1296 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, size
);
1298 usb_packet_copy(p
, r
->buffer
, size
);
1300 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1303 libusb_fill_bulk_stream_transfer(r
->xfer
, s
->dh
, ep
, p
->stream
,
1305 usb_host_req_complete_data
, r
,
1308 usb_host_req_free(r
);
1309 p
->status
= USB_RET_STALL
;
1313 libusb_fill_bulk_transfer(r
->xfer
, s
->dh
, ep
,
1315 usb_host_req_complete_data
, r
,
1319 case USB_ENDPOINT_XFER_INT
:
1320 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, p
->iov
.size
);
1322 usb_packet_copy(p
, r
->buffer
, p
->iov
.size
);
1324 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1325 libusb_fill_interrupt_transfer(r
->xfer
, s
->dh
, ep
,
1326 r
->buffer
, p
->iov
.size
,
1327 usb_host_req_complete_data
, r
,
1330 case USB_ENDPOINT_XFER_ISOC
:
1331 if (p
->pid
== USB_TOKEN_IN
) {
1332 usb_host_iso_data_in(s
, p
);
1334 usb_host_iso_data_out(s
, p
);
1336 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1337 p
->status
, p
->actual_length
);
1340 p
->status
= USB_RET_STALL
;
1341 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1342 p
->status
, p
->actual_length
);
1346 rc
= libusb_submit_transfer(r
->xfer
);
1348 p
->status
= USB_RET_NODEV
;
1349 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1350 p
->status
, p
->actual_length
);
1351 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1357 p
->status
= USB_RET_ASYNC
;
1360 static void usb_host_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
1362 if (usb_host_use_combining(ep
)) {
1363 usb_ep_combine_input_packets(ep
);
1367 static void usb_host_handle_reset(USBDevice
*udev
)
1369 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1372 trace_usb_host_reset(s
->bus_num
, s
->addr
);
1374 rc
= libusb_reset_device(s
->dh
);
1380 static int usb_host_alloc_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1381 int nr_eps
, int streams
)
1384 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1385 unsigned char endpoints
[30];
1388 for (i
= 0; i
< nr_eps
; i
++) {
1389 endpoints
[i
] = eps
[i
]->nr
;
1390 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1391 endpoints
[i
] |= 0x80;
1394 rc
= libusb_alloc_streams(s
->dh
, streams
, endpoints
, nr_eps
);
1396 usb_host_libusb_error("libusb_alloc_streams", rc
);
1397 } else if (rc
!= streams
) {
1398 error_report("libusb_alloc_streams: got less streams "
1399 "then requested %d < %d", rc
, streams
);
1402 return (rc
== streams
) ? 0 : -1;
1404 error_report("libusb_alloc_streams: error not implemented");
1409 static void usb_host_free_streams(USBDevice
*udev
, USBEndpoint
**eps
,
1413 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1414 unsigned char endpoints
[30];
1417 for (i
= 0; i
< nr_eps
; i
++) {
1418 endpoints
[i
] = eps
[i
]->nr
;
1419 if (eps
[i
]->pid
== USB_TOKEN_IN
) {
1420 endpoints
[i
] |= 0x80;
1423 libusb_free_streams(s
->dh
, endpoints
, nr_eps
);
1428 * This is *NOT* about restoring state. We have absolutely no idea
1429 * what state the host device is in at the moment and whenever it is
1430 * still present in the first place. Attemping to contine where we
1431 * left off is impossible.
1433 * What we are going to do here is emulate a surprise removal of
1434 * the usb device passed through, then kick host scan so the device
1435 * will get re-attached (and re-initialized by the guest) in case it
1438 * As the device removal will change the state of other devices (usb
1439 * host controller, most likely interrupt controller too) we have to
1440 * wait with it until *all* vmstate is loaded. Thus post_load just
1441 * kicks a bottom half which then does the actual work.
1443 static void usb_host_post_load_bh(void *opaque
)
1445 USBHostDevice
*dev
= opaque
;
1446 USBDevice
*udev
= USB_DEVICE(dev
);
1448 if (dev
->dh
!= NULL
) {
1449 usb_host_close(dev
);
1451 if (udev
->attached
) {
1452 usb_device_detach(udev
);
1454 usb_host_auto_check(NULL
);
1457 static int usb_host_post_load(void *opaque
, int version_id
)
1459 USBHostDevice
*dev
= opaque
;
1461 if (!dev
->bh_postld
) {
1462 dev
->bh_postld
= qemu_bh_new(usb_host_post_load_bh
, dev
);
1464 qemu_bh_schedule(dev
->bh_postld
);
1468 static const VMStateDescription vmstate_usb_host
= {
1471 .minimum_version_id
= 1,
1472 .post_load
= usb_host_post_load
,
1473 .fields
= (VMStateField
[]) {
1474 VMSTATE_USB_DEVICE(parent_obj
, USBHostDevice
),
1475 VMSTATE_END_OF_LIST()
1479 static Property usb_host_dev_properties
[] = {
1480 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1481 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1482 DEFINE_PROP_STRING("hostport", USBHostDevice
, match
.port
),
1483 DEFINE_PROP_UINT32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1484 DEFINE_PROP_UINT32("productid", USBHostDevice
, match
.product_id
, 0),
1485 DEFINE_PROP_UINT32("isobufs", USBHostDevice
, iso_urb_count
, 4),
1486 DEFINE_PROP_UINT32("isobsize", USBHostDevice
, iso_urb_frames
, 32),
1487 DEFINE_PROP_UINT32("loglevel", USBHostDevice
, loglevel
,
1488 LIBUSB_LOG_LEVEL_WARNING
),
1489 DEFINE_PROP_BIT("pipeline", USBHostDevice
, options
,
1490 USB_HOST_OPT_PIPELINE
, true),
1491 DEFINE_PROP_END_OF_LIST(),
1494 static void usb_host_class_initfn(ObjectClass
*klass
, void *data
)
1496 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1497 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1499 uc
->realize
= usb_host_realize
;
1500 uc
->product_desc
= "USB Host Device";
1501 uc
->cancel_packet
= usb_host_cancel_packet
;
1502 uc
->handle_data
= usb_host_handle_data
;
1503 uc
->handle_control
= usb_host_handle_control
;
1504 uc
->handle_reset
= usb_host_handle_reset
;
1505 uc
->handle_destroy
= usb_host_handle_destroy
;
1506 uc
->flush_ep_queue
= usb_host_flush_ep_queue
;
1507 uc
->alloc_streams
= usb_host_alloc_streams
;
1508 uc
->free_streams
= usb_host_free_streams
;
1509 dc
->vmsd
= &vmstate_usb_host
;
1510 dc
->props
= usb_host_dev_properties
;
1511 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
1514 static TypeInfo usb_host_dev_info
= {
1515 .name
= TYPE_USB_HOST_DEVICE
,
1516 .parent
= TYPE_USB_DEVICE
,
1517 .instance_size
= sizeof(USBHostDevice
),
1518 .class_init
= usb_host_class_initfn
,
1519 .instance_init
= usb_host_instance_init
,
1522 static void usb_host_register_types(void)
1524 type_register_static(&usb_host_dev_info
);
1527 type_init(usb_host_register_types
)
1529 /* ------------------------------------------------------------------------ */
1531 static QEMUTimer
*usb_auto_timer
;
1532 static VMChangeStateEntry
*usb_vmstate
;
1534 static void usb_host_vm_state(void *unused
, int running
, RunState state
)
1537 usb_host_auto_check(unused
);
1541 static void usb_host_auto_check(void *unused
)
1543 struct USBHostDevice
*s
;
1544 struct USBAutoFilter
*f
;
1545 libusb_device
**devs
= NULL
;
1546 struct libusb_device_descriptor ddesc
;
1547 int unconnected
= 0;
1550 if (usb_host_init() != 0) {
1554 if (runstate_is_running()) {
1555 n
= libusb_get_device_list(ctx
, &devs
);
1556 for (i
= 0; i
< n
; i
++) {
1557 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1560 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1563 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1565 if (f
->bus_num
> 0 &&
1566 f
->bus_num
!= libusb_get_bus_number(devs
[i
])) {
1570 f
->addr
!= libusb_get_device_address(devs
[i
])) {
1573 if (f
->port
!= NULL
) {
1574 char port
[16] = "-";
1575 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1576 if (strcmp(f
->port
, port
) != 0) {
1580 if (f
->vendor_id
> 0 &&
1581 f
->vendor_id
!= ddesc
.idVendor
) {
1584 if (f
->product_id
> 0 &&
1585 f
->product_id
!= ddesc
.idProduct
) {
1589 /* We got a match */
1591 if (s
->errcount
>= 3) {
1594 if (s
->dh
!= NULL
) {
1597 if (usb_host_open(s
, devs
[i
]) < 0) {
1604 libusb_free_device_list(devs
, 1);
1606 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1607 if (s
->dh
== NULL
) {
1620 if (unconnected
== 0) {
1621 /* nothing to watch */
1622 if (usb_auto_timer
) {
1623 timer_del(usb_auto_timer
);
1624 trace_usb_host_auto_scan_disabled();
1632 usb_vmstate
= qemu_add_vm_change_state_handler(usb_host_vm_state
, NULL
);
1634 if (!usb_auto_timer
) {
1635 usb_auto_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, usb_host_auto_check
, NULL
);
1636 if (!usb_auto_timer
) {
1639 trace_usb_host_auto_scan_enabled();
1641 timer_mod(usb_auto_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 2000);
1644 void hmp_info_usbhost(Monitor
*mon
, const QDict
*qdict
)
1646 libusb_device
**devs
= NULL
;
1647 struct libusb_device_descriptor ddesc
;
1651 if (usb_host_init() != 0) {
1655 n
= libusb_get_device_list(ctx
, &devs
);
1656 for (i
= 0; i
< n
; i
++) {
1657 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1660 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1663 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1664 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1665 libusb_get_bus_number(devs
[i
]),
1666 libusb_get_device_address(devs
[i
]),
1668 speed_name
[libusb_get_device_speed(devs
[i
])]);
1669 monitor_printf(mon
, " Class %02x:", ddesc
.bDeviceClass
);
1670 monitor_printf(mon
, " USB device %04x:%04x",
1671 ddesc
.idVendor
, ddesc
.idProduct
);
1672 if (ddesc
.iProduct
) {
1673 libusb_device_handle
*handle
;
1674 if (libusb_open(devs
[i
], &handle
) == 0) {
1675 unsigned char name
[64] = "";
1676 libusb_get_string_descriptor_ascii(handle
,
1678 name
, sizeof(name
));
1679 libusb_close(handle
);
1680 monitor_printf(mon
, ", %s", name
);
1683 monitor_printf(mon
, "\n");
1685 libusb_free_device_list(devs
, 1);