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
;
114 QTAILQ_ENTRY(USBHostRequest
) next
;
117 struct USBHostIsoXfer
{
118 USBHostIsoRing
*ring
;
119 struct libusb_transfer
*xfer
;
122 QTAILQ_ENTRY(USBHostIsoXfer
) next
;
125 struct USBHostIsoRing
{
128 QTAILQ_HEAD(, USBHostIsoXfer
) unused
;
129 QTAILQ_HEAD(, USBHostIsoXfer
) inflight
;
130 QTAILQ_HEAD(, USBHostIsoXfer
) copy
;
131 QTAILQ_ENTRY(USBHostIsoRing
) next
;
134 static QTAILQ_HEAD(, USBHostDevice
) hostdevs
=
135 QTAILQ_HEAD_INITIALIZER(hostdevs
);
137 static void usb_host_auto_check(void *unused
);
138 static void usb_host_release_interfaces(USBHostDevice
*s
);
139 static void usb_host_nodev(USBHostDevice
*s
);
140 static void usb_host_attach_kernel(USBHostDevice
*s
);
142 /* ------------------------------------------------------------------------ */
144 #define CONTROL_TIMEOUT 10000 /* 10 sec */
145 #define BULK_TIMEOUT 0 /* unlimited */
146 #define INTR_TIMEOUT 0 /* unlimited */
148 static const char *speed_name
[] = {
149 [LIBUSB_SPEED_UNKNOWN
] = "?",
150 [LIBUSB_SPEED_LOW
] = "1.5",
151 [LIBUSB_SPEED_FULL
] = "12",
152 [LIBUSB_SPEED_HIGH
] = "480",
153 [LIBUSB_SPEED_SUPER
] = "5000",
156 static const unsigned int speed_map
[] = {
157 [LIBUSB_SPEED_LOW
] = USB_SPEED_LOW
,
158 [LIBUSB_SPEED_FULL
] = USB_SPEED_FULL
,
159 [LIBUSB_SPEED_HIGH
] = USB_SPEED_HIGH
,
160 [LIBUSB_SPEED_SUPER
] = USB_SPEED_SUPER
,
163 static const unsigned int status_map
[] = {
164 [LIBUSB_TRANSFER_COMPLETED
] = USB_RET_SUCCESS
,
165 [LIBUSB_TRANSFER_ERROR
] = USB_RET_IOERROR
,
166 [LIBUSB_TRANSFER_TIMED_OUT
] = USB_RET_IOERROR
,
167 [LIBUSB_TRANSFER_CANCELLED
] = USB_RET_IOERROR
,
168 [LIBUSB_TRANSFER_STALL
] = USB_RET_STALL
,
169 [LIBUSB_TRANSFER_NO_DEVICE
] = USB_RET_NODEV
,
170 [LIBUSB_TRANSFER_OVERFLOW
] = USB_RET_BABBLE
,
173 static const char *err_names
[] = {
174 [-LIBUSB_ERROR_IO
] = "IO",
175 [-LIBUSB_ERROR_INVALID_PARAM
] = "INVALID_PARAM",
176 [-LIBUSB_ERROR_ACCESS
] = "ACCESS",
177 [-LIBUSB_ERROR_NO_DEVICE
] = "NO_DEVICE",
178 [-LIBUSB_ERROR_NOT_FOUND
] = "NOT_FOUND",
179 [-LIBUSB_ERROR_BUSY
] = "BUSY",
180 [-LIBUSB_ERROR_TIMEOUT
] = "TIMEOUT",
181 [-LIBUSB_ERROR_OVERFLOW
] = "OVERFLOW",
182 [-LIBUSB_ERROR_PIPE
] = "PIPE",
183 [-LIBUSB_ERROR_INTERRUPTED
] = "INTERRUPTED",
184 [-LIBUSB_ERROR_NO_MEM
] = "NO_MEM",
185 [-LIBUSB_ERROR_NOT_SUPPORTED
] = "NOT_SUPPORTED",
186 [-LIBUSB_ERROR_OTHER
] = "OTHER",
189 static libusb_context
*ctx
;
190 static uint32_t loglevel
;
192 static void usb_host_handle_fd(void *opaque
)
194 struct timeval tv
= { 0, 0 };
195 libusb_handle_events_timeout(ctx
, &tv
);
198 static void usb_host_add_fd(int fd
, short events
, void *user_data
)
200 qemu_set_fd_handler(fd
,
201 (events
& POLLIN
) ? usb_host_handle_fd
: NULL
,
202 (events
& POLLOUT
) ? usb_host_handle_fd
: NULL
,
206 static void usb_host_del_fd(int fd
, void *user_data
)
208 qemu_set_fd_handler(fd
, NULL
, NULL
, NULL
);
211 static int usb_host_init(void)
213 const struct libusb_pollfd
**poll
;
219 rc
= libusb_init(&ctx
);
223 libusb_set_debug(ctx
, loglevel
);
225 libusb_set_pollfd_notifiers(ctx
, usb_host_add_fd
,
228 poll
= libusb_get_pollfds(ctx
);
230 for (i
= 0; poll
[i
] != NULL
; i
++) {
231 usb_host_add_fd(poll
[i
]->fd
, poll
[i
]->events
, ctx
);
238 static int usb_host_get_port(libusb_device
*dev
, char *port
, size_t len
)
244 #if LIBUSBX_API_VERSION >= 0x01000102
245 rc
= libusb_get_port_numbers(dev
, path
, 7);
247 rc
= libusb_get_port_path(ctx
, dev
, path
, 7);
252 off
= snprintf(port
, len
, "%d", path
[0]);
253 for (i
= 1; i
< rc
; i
++) {
254 off
+= snprintf(port
+off
, len
-off
, ".%d", path
[i
]);
259 static void usb_host_libusb_error(const char *func
, int rc
)
267 if (-rc
< ARRAY_SIZE(err_names
) && err_names
[-rc
]) {
268 errname
= err_names
[-rc
];
272 fprintf(stderr
, "%s: %d [%s]\n", func
, rc
, errname
);
275 /* ------------------------------------------------------------------------ */
277 static bool usb_host_use_combining(USBEndpoint
*ep
)
284 if (ep
->pid
!= USB_TOKEN_IN
) {
287 type
= usb_ep_get_type(ep
->dev
, ep
->pid
, ep
->nr
);
288 if (type
!= USB_ENDPOINT_XFER_BULK
) {
294 /* ------------------------------------------------------------------------ */
296 static USBHostRequest
*usb_host_req_alloc(USBHostDevice
*s
, USBPacket
*p
,
297 bool in
, size_t bufsize
)
299 USBHostRequest
*r
= g_new0(USBHostRequest
, 1);
304 r
->xfer
= libusb_alloc_transfer(0);
306 r
->buffer
= g_malloc(bufsize
);
308 QTAILQ_INSERT_TAIL(&s
->requests
, r
, next
);
312 static void usb_host_req_free(USBHostRequest
*r
)
315 QTAILQ_REMOVE(&r
->host
->requests
, r
, next
);
317 libusb_free_transfer(r
->xfer
);
322 static USBHostRequest
*usb_host_req_find(USBHostDevice
*s
, USBPacket
*p
)
326 QTAILQ_FOREACH(r
, &s
->requests
, next
) {
334 static void usb_host_req_complete_ctrl(struct libusb_transfer
*xfer
)
336 USBHostRequest
*r
= xfer
->user_data
;
337 USBHostDevice
*s
= r
->host
;
338 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
341 goto out
; /* request was canceled */
344 r
->p
->status
= status_map
[xfer
->status
];
345 r
->p
->actual_length
= xfer
->actual_length
;
346 if (r
->in
&& xfer
->actual_length
) {
347 memcpy(r
->cbuf
, r
->buffer
+ 8, xfer
->actual_length
);
349 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
350 r
->p
->status
, r
->p
->actual_length
);
351 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
354 usb_host_req_free(r
);
360 static void usb_host_req_complete_data(struct libusb_transfer
*xfer
)
362 USBHostRequest
*r
= xfer
->user_data
;
363 USBHostDevice
*s
= r
->host
;
364 bool disconnect
= (xfer
->status
== LIBUSB_TRANSFER_NO_DEVICE
);
367 goto out
; /* request was canceled */
370 r
->p
->status
= status_map
[xfer
->status
];
371 if (r
->in
&& xfer
->actual_length
) {
372 usb_packet_copy(r
->p
, r
->buffer
, xfer
->actual_length
);
374 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
375 r
->p
->status
, r
->p
->actual_length
);
376 if (usb_host_use_combining(r
->p
->ep
)) {
377 usb_combined_input_packet_complete(USB_DEVICE(s
), r
->p
);
379 usb_packet_complete(USB_DEVICE(s
), r
->p
);
383 usb_host_req_free(r
);
389 static void usb_host_req_abort(USBHostRequest
*r
)
391 USBHostDevice
*s
= r
->host
;
392 bool inflight
= (r
->p
&& r
->p
->state
== USB_PACKET_ASYNC
);
395 r
->p
->status
= USB_RET_NODEV
;
396 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, r
->p
,
397 r
->p
->status
, r
->p
->actual_length
);
398 if (r
->p
->ep
->nr
== 0) {
399 usb_generic_async_ctrl_complete(USB_DEVICE(s
), r
->p
);
401 usb_packet_complete(USB_DEVICE(s
), r
->p
);
406 QTAILQ_REMOVE(&r
->host
->requests
, r
, next
);
410 libusb_cancel_transfer(r
->xfer
);
414 /* ------------------------------------------------------------------------ */
416 static void usb_host_req_complete_iso(struct libusb_transfer
*transfer
)
418 USBHostIsoXfer
*xfer
= transfer
->user_data
;
421 /* USBHostIsoXfer released while inflight */
422 g_free(transfer
->buffer
);
423 libusb_free_transfer(transfer
);
427 QTAILQ_REMOVE(&xfer
->ring
->inflight
, xfer
, next
);
428 if (QTAILQ_EMPTY(&xfer
->ring
->inflight
)) {
429 USBHostDevice
*s
= xfer
->ring
->host
;
430 trace_usb_host_iso_stop(s
->bus_num
, s
->addr
, xfer
->ring
->ep
->nr
);
432 if (xfer
->ring
->ep
->pid
== USB_TOKEN_IN
) {
433 QTAILQ_INSERT_TAIL(&xfer
->ring
->copy
, xfer
, next
);
435 QTAILQ_INSERT_TAIL(&xfer
->ring
->unused
, xfer
, next
);
439 static USBHostIsoRing
*usb_host_iso_alloc(USBHostDevice
*s
, USBEndpoint
*ep
)
441 USBHostIsoRing
*ring
= g_new0(USBHostIsoRing
, 1);
442 USBHostIsoXfer
*xfer
;
443 /* FIXME: check interval (for now assume one xfer per frame) */
444 int packets
= s
->iso_urb_frames
;
449 QTAILQ_INIT(&ring
->unused
);
450 QTAILQ_INIT(&ring
->inflight
);
451 QTAILQ_INIT(&ring
->copy
);
452 QTAILQ_INSERT_TAIL(&s
->isorings
, ring
, next
);
454 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
455 xfer
= g_new0(USBHostIsoXfer
, 1);
457 xfer
->xfer
= libusb_alloc_transfer(packets
);
458 xfer
->xfer
->dev_handle
= s
->dh
;
459 xfer
->xfer
->type
= LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
;
461 xfer
->xfer
->endpoint
= ring
->ep
->nr
;
462 if (ring
->ep
->pid
== USB_TOKEN_IN
) {
463 xfer
->xfer
->endpoint
|= USB_DIR_IN
;
465 xfer
->xfer
->callback
= usb_host_req_complete_iso
;
466 xfer
->xfer
->user_data
= xfer
;
468 xfer
->xfer
->num_iso_packets
= packets
;
469 xfer
->xfer
->length
= ring
->ep
->max_packet_size
* packets
;
470 xfer
->xfer
->buffer
= g_malloc0(xfer
->xfer
->length
);
472 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
478 static USBHostIsoRing
*usb_host_iso_find(USBHostDevice
*s
, USBEndpoint
*ep
)
480 USBHostIsoRing
*ring
;
482 QTAILQ_FOREACH(ring
, &s
->isorings
, next
) {
483 if (ring
->ep
== ep
) {
490 static void usb_host_iso_reset_xfer(USBHostIsoXfer
*xfer
)
492 libusb_set_iso_packet_lengths(xfer
->xfer
,
493 xfer
->ring
->ep
->max_packet_size
);
495 xfer
->copy_complete
= false;
498 static void usb_host_iso_free_xfer(USBHostIsoXfer
*xfer
, bool inflight
)
501 xfer
->xfer
->user_data
= NULL
;
503 g_free(xfer
->xfer
->buffer
);
504 libusb_free_transfer(xfer
->xfer
);
509 static void usb_host_iso_free(USBHostIsoRing
*ring
)
511 USBHostIsoXfer
*xfer
;
513 while ((xfer
= QTAILQ_FIRST(&ring
->inflight
)) != NULL
) {
514 QTAILQ_REMOVE(&ring
->inflight
, xfer
, next
);
515 usb_host_iso_free_xfer(xfer
, true);
517 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
518 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
519 usb_host_iso_free_xfer(xfer
, false);
521 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
) {
522 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
523 usb_host_iso_free_xfer(xfer
, false);
526 QTAILQ_REMOVE(&ring
->host
->isorings
, ring
, next
);
530 static void usb_host_iso_free_all(USBHostDevice
*s
)
532 USBHostIsoRing
*ring
;
534 while ((ring
= QTAILQ_FIRST(&s
->isorings
)) != NULL
) {
535 usb_host_iso_free(ring
);
539 static bool usb_host_iso_data_copy(USBHostIsoXfer
*xfer
, USBPacket
*p
)
544 buf
= libusb_get_iso_packet_buffer_simple(xfer
->xfer
, xfer
->packet
);
545 if (p
->pid
== USB_TOKEN_OUT
) {
547 if (psize
> xfer
->ring
->ep
->max_packet_size
) {
548 /* should not happen (guest bug) */
549 psize
= xfer
->ring
->ep
->max_packet_size
;
551 xfer
->xfer
->iso_packet_desc
[xfer
->packet
].length
= psize
;
553 psize
= xfer
->xfer
->iso_packet_desc
[xfer
->packet
].actual_length
;
554 if (psize
> p
->iov
.size
) {
555 /* should not happen (guest bug) */
559 usb_packet_copy(p
, buf
, psize
);
561 xfer
->copy_complete
= (xfer
->packet
== xfer
->xfer
->num_iso_packets
);
562 return xfer
->copy_complete
;
565 static void usb_host_iso_data_in(USBHostDevice
*s
, USBPacket
*p
)
567 USBHostIsoRing
*ring
;
568 USBHostIsoXfer
*xfer
;
569 bool disconnect
= false;
572 ring
= usb_host_iso_find(s
, p
->ep
);
574 ring
= usb_host_iso_alloc(s
, p
->ep
);
577 /* copy data to guest */
578 xfer
= QTAILQ_FIRST(&ring
->copy
);
580 if (usb_host_iso_data_copy(xfer
, p
)) {
581 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
582 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
586 /* submit empty bufs to host */
587 while ((xfer
= QTAILQ_FIRST(&ring
->unused
)) != NULL
) {
588 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
589 usb_host_iso_reset_xfer(xfer
);
590 rc
= libusb_submit_transfer(xfer
->xfer
);
592 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
593 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
594 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
599 if (QTAILQ_EMPTY(&ring
->inflight
)) {
600 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
602 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
610 static void usb_host_iso_data_out(USBHostDevice
*s
, USBPacket
*p
)
612 USBHostIsoRing
*ring
;
613 USBHostIsoXfer
*xfer
;
614 bool disconnect
= false;
617 ring
= usb_host_iso_find(s
, p
->ep
);
619 ring
= usb_host_iso_alloc(s
, p
->ep
);
622 /* copy data from guest */
623 xfer
= QTAILQ_FIRST(&ring
->copy
);
624 while (xfer
!= NULL
&& xfer
->copy_complete
) {
626 xfer
= QTAILQ_NEXT(xfer
, next
);
629 xfer
= QTAILQ_FIRST(&ring
->unused
);
631 trace_usb_host_iso_out_of_bufs(s
->bus_num
, s
->addr
, p
->ep
->nr
);
634 QTAILQ_REMOVE(&ring
->unused
, xfer
, next
);
635 usb_host_iso_reset_xfer(xfer
);
636 QTAILQ_INSERT_TAIL(&ring
->copy
, xfer
, next
);
638 usb_host_iso_data_copy(xfer
, p
);
640 if (QTAILQ_EMPTY(&ring
->inflight
)) {
641 /* wait until half of our buffers are filled
642 before kicking the iso out stream */
643 if (filled
*2 < s
->iso_urb_count
) {
648 /* submit filled bufs to host */
649 while ((xfer
= QTAILQ_FIRST(&ring
->copy
)) != NULL
&&
650 xfer
->copy_complete
) {
651 QTAILQ_REMOVE(&ring
->copy
, xfer
, next
);
652 rc
= libusb_submit_transfer(xfer
->xfer
);
654 usb_host_libusb_error("libusb_submit_transfer [iso]", rc
);
655 QTAILQ_INSERT_TAIL(&ring
->unused
, xfer
, next
);
656 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
661 if (QTAILQ_EMPTY(&ring
->inflight
)) {
662 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, p
->ep
->nr
);
664 QTAILQ_INSERT_TAIL(&ring
->inflight
, xfer
, next
);
672 /* ------------------------------------------------------------------------ */
674 static bool usb_host_full_speed_compat(USBHostDevice
*s
)
676 struct libusb_config_descriptor
*conf
;
677 const struct libusb_interface_descriptor
*intf
;
678 const struct libusb_endpoint_descriptor
*endp
;
683 rc
= libusb_get_config_descriptor(s
->dev
, c
, &conf
);
687 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
688 for (a
= 0; a
< conf
->interface
[i
].num_altsetting
; a
++) {
689 intf
= &conf
->interface
[i
].altsetting
[a
];
690 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
691 endp
= &intf
->endpoint
[e
];
692 type
= endp
->bmAttributes
& 0x3;
696 case 0x03: /* INTERRUPT */
697 if (endp
->wMaxPacketSize
> 64) {
705 libusb_free_config_descriptor(conf
);
710 static void usb_host_ep_update(USBHostDevice
*s
)
712 static const char *tname
[] = {
713 [USB_ENDPOINT_XFER_CONTROL
] = "control",
714 [USB_ENDPOINT_XFER_ISOC
] = "isoc",
715 [USB_ENDPOINT_XFER_BULK
] = "bulk",
716 [USB_ENDPOINT_XFER_INT
] = "int",
718 USBDevice
*udev
= USB_DEVICE(s
);
719 struct libusb_config_descriptor
*conf
;
720 const struct libusb_interface_descriptor
*intf
;
721 const struct libusb_endpoint_descriptor
*endp
;
727 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
731 trace_usb_host_parse_config(s
->bus_num
, s
->addr
,
732 conf
->bConfigurationValue
, true);
734 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
735 assert(udev
->altsetting
[i
] < conf
->interface
[i
].num_altsetting
);
736 intf
= &conf
->interface
[i
].altsetting
[udev
->altsetting
[i
]];
737 trace_usb_host_parse_interface(s
->bus_num
, s
->addr
,
738 intf
->bInterfaceNumber
,
739 intf
->bAlternateSetting
, true);
740 for (e
= 0; e
< intf
->bNumEndpoints
; e
++) {
741 endp
= &intf
->endpoint
[e
];
743 devep
= endp
->bEndpointAddress
;
744 pid
= (devep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
746 type
= endp
->bmAttributes
& 0x3;
749 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
750 "invalid endpoint address");
753 if (usb_ep_get_type(udev
, pid
, ep
) != USB_ENDPOINT_XFER_INVALID
) {
754 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
755 "duplicate endpoint address");
759 trace_usb_host_parse_endpoint(s
->bus_num
, s
->addr
, ep
,
760 (devep
& USB_DIR_IN
) ? "in" : "out",
762 usb_ep_set_max_packet_size(udev
, pid
, ep
,
763 endp
->wMaxPacketSize
);
764 usb_ep_set_type(udev
, pid
, ep
, type
);
765 usb_ep_set_ifnum(udev
, pid
, ep
, i
);
766 usb_ep_set_halted(udev
, pid
, ep
, 0);
770 libusb_free_config_descriptor(conf
);
773 static int usb_host_open(USBHostDevice
*s
, libusb_device
*dev
)
775 USBDevice
*udev
= USB_DEVICE(s
);
776 int bus_num
= libusb_get_bus_number(dev
);
777 int addr
= libusb_get_device_address(dev
);
780 trace_usb_host_open_started(bus_num
, addr
);
785 rc
= libusb_open(dev
, &s
->dh
);
790 libusb_get_device_descriptor(dev
, &s
->ddesc
);
792 s
->bus_num
= bus_num
;
794 usb_host_get_port(s
->dev
, s
->port
, sizeof(s
->port
));
797 usb_host_ep_update(s
);
799 udev
->speed
= speed_map
[libusb_get_device_speed(dev
)];
800 udev
->speedmask
= (1 << udev
->speed
);
801 if (udev
->speed
== USB_SPEED_HIGH
&& usb_host_full_speed_compat(s
)) {
802 udev
->speedmask
|= USB_SPEED_MASK_FULL
;
805 if (s
->ddesc
.iProduct
) {
806 libusb_get_string_descriptor_ascii(s
->dh
, s
->ddesc
.iProduct
,
807 (unsigned char *)udev
->product_desc
,
808 sizeof(udev
->product_desc
));
810 snprintf(udev
->product_desc
, sizeof(udev
->product_desc
),
811 "host:%d.%d", bus_num
, addr
);
814 rc
= usb_device_attach(udev
);
819 trace_usb_host_open_success(bus_num
, addr
);
823 trace_usb_host_open_failure(bus_num
, addr
);
832 static void usb_host_abort_xfers(USBHostDevice
*s
)
834 USBHostRequest
*r
, *rtmp
;
836 QTAILQ_FOREACH_SAFE(r
, &s
->requests
, next
, rtmp
) {
837 usb_host_req_abort(r
);
841 static int usb_host_close(USBHostDevice
*s
)
843 USBDevice
*udev
= USB_DEVICE(s
);
849 trace_usb_host_close(s
->bus_num
, s
->addr
);
851 usb_host_abort_xfers(s
);
852 usb_host_iso_free_all(s
);
854 if (udev
->attached
) {
855 usb_device_detach(udev
);
858 usb_host_release_interfaces(s
);
859 libusb_reset_device(s
->dh
);
860 usb_host_attach_kernel(s
);
865 usb_host_auto_check(NULL
);
869 static void usb_host_nodev_bh(void *opaque
)
871 USBHostDevice
*s
= opaque
;
875 static void usb_host_nodev(USBHostDevice
*s
)
878 s
->bh_nodev
= qemu_bh_new(usb_host_nodev_bh
, s
);
880 qemu_bh_schedule(s
->bh_nodev
);
883 static void usb_host_exit_notifier(struct Notifier
*n
, void *data
)
885 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
888 usb_host_release_interfaces(s
);
889 usb_host_attach_kernel(s
);
893 static int usb_host_initfn(USBDevice
*udev
)
895 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
897 loglevel
= s
->loglevel
;
898 udev
->flags
|= (1 << USB_DEV_FLAG_IS_HOST
);
899 udev
->auto_attach
= 0;
900 QTAILQ_INIT(&s
->requests
);
901 QTAILQ_INIT(&s
->isorings
);
903 s
->exit
.notify
= usb_host_exit_notifier
;
904 qemu_add_exit_notifier(&s
->exit
);
906 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
907 add_boot_device_path(s
->bootindex
, &udev
->qdev
, NULL
);
908 usb_host_auto_check(NULL
);
912 static void usb_host_handle_destroy(USBDevice
*udev
)
914 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
916 qemu_remove_exit_notifier(&s
->exit
);
917 QTAILQ_REMOVE(&hostdevs
, s
, next
);
921 static void usb_host_cancel_packet(USBDevice
*udev
, USBPacket
*p
)
923 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
927 usb_combined_packet_cancel(udev
, p
);
931 trace_usb_host_req_canceled(s
->bus_num
, s
->addr
, p
);
933 r
= usb_host_req_find(s
, p
);
935 r
->p
= NULL
; /* mark as dead */
936 libusb_cancel_transfer(r
->xfer
);
940 static void usb_host_detach_kernel(USBHostDevice
*s
)
942 struct libusb_config_descriptor
*conf
;
945 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
949 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
950 rc
= libusb_kernel_driver_active(s
->dh
, i
);
951 usb_host_libusb_error("libusb_kernel_driver_active", rc
);
955 trace_usb_host_detach_kernel(s
->bus_num
, s
->addr
, i
);
956 rc
= libusb_detach_kernel_driver(s
->dh
, i
);
957 usb_host_libusb_error("libusb_detach_kernel_driver", rc
);
958 s
->ifs
[i
].detached
= true;
960 libusb_free_config_descriptor(conf
);
963 static void usb_host_attach_kernel(USBHostDevice
*s
)
965 struct libusb_config_descriptor
*conf
;
968 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
972 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
973 if (!s
->ifs
[i
].detached
) {
976 trace_usb_host_attach_kernel(s
->bus_num
, s
->addr
, i
);
977 libusb_attach_kernel_driver(s
->dh
, i
);
978 s
->ifs
[i
].detached
= false;
980 libusb_free_config_descriptor(conf
);
983 static int usb_host_claim_interfaces(USBHostDevice
*s
, int configuration
)
985 USBDevice
*udev
= USB_DEVICE(s
);
986 struct libusb_config_descriptor
*conf
;
989 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
990 udev
->altsetting
[i
] = 0;
992 udev
->ninterfaces
= 0;
993 udev
->configuration
= 0;
995 if (configuration
== 0) {
996 /* address state - ignore */
997 return USB_RET_SUCCESS
;
1000 usb_host_detach_kernel(s
);
1002 rc
= libusb_get_active_config_descriptor(s
->dev
, &conf
);
1004 return USB_RET_STALL
;
1007 for (i
= 0; i
< conf
->bNumInterfaces
; i
++) {
1008 trace_usb_host_claim_interface(s
->bus_num
, s
->addr
, configuration
, i
);
1009 rc
= libusb_claim_interface(s
->dh
, i
);
1010 usb_host_libusb_error("libusb_claim_interface", rc
);
1012 return USB_RET_STALL
;
1014 s
->ifs
[i
].claimed
= true;
1017 udev
->ninterfaces
= conf
->bNumInterfaces
;
1018 udev
->configuration
= configuration
;
1020 libusb_free_config_descriptor(conf
);
1021 return USB_RET_SUCCESS
;
1024 static void usb_host_release_interfaces(USBHostDevice
*s
)
1026 USBDevice
*udev
= USB_DEVICE(s
);
1029 for (i
= 0; i
< udev
->ninterfaces
; i
++) {
1030 if (!s
->ifs
[i
].claimed
) {
1033 trace_usb_host_release_interface(s
->bus_num
, s
->addr
, i
);
1034 rc
= libusb_release_interface(s
->dh
, i
);
1035 usb_host_libusb_error("libusb_release_interface", rc
);
1036 s
->ifs
[i
].claimed
= false;
1040 static void usb_host_set_address(USBHostDevice
*s
, int addr
)
1042 USBDevice
*udev
= USB_DEVICE(s
);
1044 trace_usb_host_set_address(s
->bus_num
, s
->addr
, addr
);
1048 static void usb_host_set_config(USBHostDevice
*s
, int config
, USBPacket
*p
)
1052 trace_usb_host_set_config(s
->bus_num
, s
->addr
, config
);
1054 usb_host_release_interfaces(s
);
1055 usb_host_detach_kernel(s
);
1056 rc
= libusb_set_configuration(s
->dh
, config
);
1058 usb_host_libusb_error("libusb_set_configuration", rc
);
1059 p
->status
= USB_RET_STALL
;
1060 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1065 p
->status
= usb_host_claim_interfaces(s
, config
);
1066 if (p
->status
!= USB_RET_SUCCESS
) {
1069 usb_host_ep_update(s
);
1072 static void usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
,
1075 USBDevice
*udev
= USB_DEVICE(s
);
1078 trace_usb_host_set_interface(s
->bus_num
, s
->addr
, iface
, alt
);
1080 usb_host_iso_free_all(s
);
1082 if (iface
>= USB_MAX_INTERFACES
) {
1083 p
->status
= USB_RET_STALL
;
1087 rc
= libusb_set_interface_alt_setting(s
->dh
, iface
, alt
);
1089 usb_host_libusb_error("libusb_set_interface_alt_setting", rc
);
1090 p
->status
= USB_RET_STALL
;
1091 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1097 udev
->altsetting
[iface
] = alt
;
1098 usb_host_ep_update(s
);
1101 static void usb_host_handle_control(USBDevice
*udev
, USBPacket
*p
,
1102 int request
, int value
, int index
,
1103 int length
, uint8_t *data
)
1105 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1109 trace_usb_host_req_control(s
->bus_num
, s
->addr
, p
, request
, value
, index
);
1111 if (s
->dh
== NULL
) {
1112 p
->status
= USB_RET_NODEV
;
1113 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1118 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1119 usb_host_set_address(s
, value
);
1120 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1123 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1124 usb_host_set_config(s
, value
& 0xff, p
);
1125 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1128 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1129 usb_host_set_interface(s
, index
, value
, p
);
1130 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1133 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
1134 if (value
== 0) { /* clear halt */
1135 int pid
= (index
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1136 libusb_clear_halt(s
->dh
, index
);
1137 usb_ep_set_halted(udev
, pid
, index
& 0x0f, 0);
1138 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1143 r
= usb_host_req_alloc(s
, p
, (request
>> 8) & USB_DIR_IN
, length
+ 8);
1146 memcpy(r
->buffer
, udev
->setup_buf
, 8);
1148 memcpy(r
->buffer
+ 8, r
->cbuf
, r
->clen
);
1151 libusb_fill_control_transfer(r
->xfer
, s
->dh
, r
->buffer
,
1152 usb_host_req_complete_ctrl
, r
,
1154 rc
= libusb_submit_transfer(r
->xfer
);
1156 p
->status
= USB_RET_NODEV
;
1157 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1158 p
->status
, p
->actual_length
);
1159 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1165 p
->status
= USB_RET_ASYNC
;
1168 static void usb_host_handle_data(USBDevice
*udev
, USBPacket
*p
)
1170 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1175 if (usb_host_use_combining(p
->ep
) && p
->state
== USB_PACKET_SETUP
) {
1176 p
->status
= USB_RET_ADD_TO_QUEUE
;
1180 trace_usb_host_req_data(s
->bus_num
, s
->addr
, p
,
1181 p
->pid
== USB_TOKEN_IN
,
1182 p
->ep
->nr
, p
->iov
.size
);
1184 if (s
->dh
== NULL
) {
1185 p
->status
= USB_RET_NODEV
;
1186 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1189 if (p
->ep
->halted
) {
1190 p
->status
= USB_RET_STALL
;
1191 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1195 switch (usb_ep_get_type(udev
, p
->pid
, p
->ep
->nr
)) {
1196 case USB_ENDPOINT_XFER_BULK
:
1197 size
= usb_packet_size(p
);
1198 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, size
);
1200 usb_packet_copy(p
, r
->buffer
, size
);
1202 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1203 libusb_fill_bulk_transfer(r
->xfer
, s
->dh
, ep
,
1205 usb_host_req_complete_data
, r
,
1208 case USB_ENDPOINT_XFER_INT
:
1209 r
= usb_host_req_alloc(s
, p
, p
->pid
== USB_TOKEN_IN
, p
->iov
.size
);
1211 usb_packet_copy(p
, r
->buffer
, p
->iov
.size
);
1213 ep
= p
->ep
->nr
| (r
->in
? USB_DIR_IN
: 0);
1214 libusb_fill_interrupt_transfer(r
->xfer
, s
->dh
, ep
,
1215 r
->buffer
, p
->iov
.size
,
1216 usb_host_req_complete_data
, r
,
1219 case USB_ENDPOINT_XFER_ISOC
:
1220 if (p
->pid
== USB_TOKEN_IN
) {
1221 usb_host_iso_data_in(s
, p
);
1223 usb_host_iso_data_out(s
, p
);
1225 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1226 p
->status
, p
->actual_length
);
1229 p
->status
= USB_RET_STALL
;
1230 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1231 p
->status
, p
->actual_length
);
1235 rc
= libusb_submit_transfer(r
->xfer
);
1237 p
->status
= USB_RET_NODEV
;
1238 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
1239 p
->status
, p
->actual_length
);
1240 if (rc
== LIBUSB_ERROR_NO_DEVICE
) {
1246 p
->status
= USB_RET_ASYNC
;
1249 static void usb_host_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
1251 if (usb_host_use_combining(ep
)) {
1252 usb_ep_combine_input_packets(ep
);
1256 static void usb_host_handle_reset(USBDevice
*udev
)
1258 USBHostDevice
*s
= USB_HOST_DEVICE(udev
);
1260 trace_usb_host_reset(s
->bus_num
, s
->addr
);
1262 if (udev
->configuration
== 0) {
1265 usb_host_release_interfaces(s
);
1266 libusb_reset_device(s
->dh
);
1267 usb_host_claim_interfaces(s
, 0);
1268 usb_host_ep_update(s
);
1272 * This is *NOT* about restoring state. We have absolutely no idea
1273 * what state the host device is in at the moment and whenever it is
1274 * still present in the first place. Attemping to contine where we
1275 * left off is impossible.
1277 * What we are going to to to here is emulate a surprise removal of
1278 * the usb device passed through, then kick host scan so the device
1279 * will get re-attached (and re-initialized by the guest) in case it
1282 * As the device removal will change the state of other devices (usb
1283 * host controller, most likely interrupt controller too) we have to
1284 * wait with it until *all* vmstate is loaded. Thus post_load just
1285 * kicks a bottom half which then does the actual work.
1287 static void usb_host_post_load_bh(void *opaque
)
1289 USBHostDevice
*dev
= opaque
;
1290 USBDevice
*udev
= USB_DEVICE(dev
);
1292 if (dev
->dh
!= NULL
) {
1293 usb_host_close(dev
);
1295 if (udev
->attached
) {
1296 usb_device_detach(udev
);
1298 usb_host_auto_check(NULL
);
1301 static int usb_host_post_load(void *opaque
, int version_id
)
1303 USBHostDevice
*dev
= opaque
;
1305 if (!dev
->bh_postld
) {
1306 dev
->bh_postld
= qemu_bh_new(usb_host_post_load_bh
, dev
);
1308 qemu_bh_schedule(dev
->bh_postld
);
1312 static const VMStateDescription vmstate_usb_host
= {
1315 .minimum_version_id
= 1,
1316 .post_load
= usb_host_post_load
,
1317 .fields
= (VMStateField
[]) {
1318 VMSTATE_USB_DEVICE(parent_obj
, USBHostDevice
),
1319 VMSTATE_END_OF_LIST()
1323 static Property usb_host_dev_properties
[] = {
1324 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1325 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1326 DEFINE_PROP_STRING("hostport", USBHostDevice
, match
.port
),
1327 DEFINE_PROP_HEX32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1328 DEFINE_PROP_HEX32("productid", USBHostDevice
, match
.product_id
, 0),
1329 DEFINE_PROP_UINT32("isobufs", USBHostDevice
, iso_urb_count
, 4),
1330 DEFINE_PROP_UINT32("isobsize", USBHostDevice
, iso_urb_frames
, 32),
1331 DEFINE_PROP_INT32("bootindex", USBHostDevice
, bootindex
, -1),
1332 DEFINE_PROP_UINT32("loglevel", USBHostDevice
, loglevel
,
1333 LIBUSB_LOG_LEVEL_WARNING
),
1334 DEFINE_PROP_BIT("pipeline", USBHostDevice
, options
,
1335 USB_HOST_OPT_PIPELINE
, true),
1336 DEFINE_PROP_END_OF_LIST(),
1339 static void usb_host_class_initfn(ObjectClass
*klass
, void *data
)
1341 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1342 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1344 uc
->init
= usb_host_initfn
;
1345 uc
->product_desc
= "USB Host Device";
1346 uc
->cancel_packet
= usb_host_cancel_packet
;
1347 uc
->handle_data
= usb_host_handle_data
;
1348 uc
->handle_control
= usb_host_handle_control
;
1349 uc
->handle_reset
= usb_host_handle_reset
;
1350 uc
->handle_destroy
= usb_host_handle_destroy
;
1351 uc
->flush_ep_queue
= usb_host_flush_ep_queue
;
1352 dc
->vmsd
= &vmstate_usb_host
;
1353 dc
->props
= usb_host_dev_properties
;
1354 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
1357 static TypeInfo usb_host_dev_info
= {
1358 .name
= TYPE_USB_HOST_DEVICE
,
1359 .parent
= TYPE_USB_DEVICE
,
1360 .instance_size
= sizeof(USBHostDevice
),
1361 .class_init
= usb_host_class_initfn
,
1364 static void usb_host_register_types(void)
1366 type_register_static(&usb_host_dev_info
);
1369 type_init(usb_host_register_types
)
1371 /* ------------------------------------------------------------------------ */
1373 static QEMUTimer
*usb_auto_timer
;
1374 static VMChangeStateEntry
*usb_vmstate
;
1376 static void usb_host_vm_state(void *unused
, int running
, RunState state
)
1379 usb_host_auto_check(unused
);
1383 static void usb_host_auto_check(void *unused
)
1385 struct USBHostDevice
*s
;
1386 struct USBAutoFilter
*f
;
1387 libusb_device
**devs
;
1388 struct libusb_device_descriptor ddesc
;
1389 int unconnected
= 0;
1392 if (usb_host_init() != 0) {
1396 if (runstate_is_running()) {
1397 n
= libusb_get_device_list(ctx
, &devs
);
1398 for (i
= 0; i
< n
; i
++) {
1399 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1402 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1405 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1407 if (f
->bus_num
> 0 &&
1408 f
->bus_num
!= libusb_get_bus_number(devs
[i
])) {
1412 f
->addr
!= libusb_get_device_address(devs
[i
])) {
1415 if (f
->port
!= NULL
) {
1416 char port
[16] = "-";
1417 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1418 if (strcmp(f
->port
, port
) != 0) {
1422 if (f
->vendor_id
> 0 &&
1423 f
->vendor_id
!= ddesc
.idVendor
) {
1426 if (f
->product_id
> 0 &&
1427 f
->product_id
!= ddesc
.idProduct
) {
1431 /* We got a match */
1433 if (s
->errcount
>= 3) {
1436 if (s
->dh
!= NULL
) {
1439 if (usb_host_open(s
, devs
[i
]) < 0) {
1446 libusb_free_device_list(devs
, 1);
1448 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1449 if (s
->dh
== NULL
) {
1462 if (unconnected
== 0) {
1463 /* nothing to watch */
1464 if (usb_auto_timer
) {
1465 qemu_del_timer(usb_auto_timer
);
1466 trace_usb_host_auto_scan_disabled();
1474 usb_vmstate
= qemu_add_vm_change_state_handler(usb_host_vm_state
, NULL
);
1476 if (!usb_auto_timer
) {
1477 usb_auto_timer
= qemu_new_timer_ms(rt_clock
, usb_host_auto_check
, NULL
);
1478 if (!usb_auto_timer
) {
1481 trace_usb_host_auto_scan_enabled();
1483 qemu_mod_timer(usb_auto_timer
, qemu_get_clock_ms(rt_clock
) + 2000);
1486 void usb_host_info(Monitor
*mon
, const QDict
*qdict
)
1488 libusb_device
**devs
;
1489 struct libusb_device_descriptor ddesc
;
1493 if (usb_host_init() != 0) {
1497 n
= libusb_get_device_list(ctx
, &devs
);
1498 for (i
= 0; i
< n
; i
++) {
1499 if (libusb_get_device_descriptor(devs
[i
], &ddesc
) != 0) {
1502 if (ddesc
.bDeviceClass
== LIBUSB_CLASS_HUB
) {
1505 usb_host_get_port(devs
[i
], port
, sizeof(port
));
1506 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1507 libusb_get_bus_number(devs
[i
]),
1508 libusb_get_device_address(devs
[i
]),
1510 speed_name
[libusb_get_device_speed(devs
[i
])]);
1511 monitor_printf(mon
, " Class %02x:", ddesc
.bDeviceClass
);
1512 monitor_printf(mon
, " USB device %04x:%04x",
1513 ddesc
.idVendor
, ddesc
.idProduct
);
1514 if (ddesc
.iProduct
) {
1515 libusb_device_handle
*handle
;
1516 if (libusb_open(devs
[i
], &handle
) == 0) {
1517 unsigned char name
[64] = "";
1518 libusb_get_string_descriptor_ascii(handle
,
1520 name
, sizeof(name
));
1521 libusb_close(handle
);
1522 monitor_printf(mon
, ", %s", name
);
1525 monitor_printf(mon
, "\n");
1527 libusb_free_device_list(devs
, 1);