s390-ccw.img: replace while loop with a disabled wait on s390 bios
[qemu/agraf.git] / hw / usb / host-libusb.c
blobd1186b8a9fcbc2b98b2822d953a3acd2e31db4e5
1 /*
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
33 * THE SOFTWARE.
36 #include <poll.h>
37 #include <libusb.h>
39 #include "qemu-common.h"
40 #include "monitor/monitor.h"
41 #include "sysemu/sysemu.h"
42 #include "trace.h"
44 #include "hw/usb.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 {
58 uint32_t bus_num;
59 uint32_t addr;
60 char *port;
61 uint32_t vendor_id;
62 uint32_t product_id;
65 enum USBHostDeviceOptions {
66 USB_HOST_OPT_PIPELINE,
69 struct USBHostDevice {
70 USBDevice parent_obj;
72 /* properties */
73 struct USBAutoFilter match;
74 int32_t bootindex;
75 uint32_t iso_urb_count;
76 uint32_t iso_urb_frames;
77 uint32_t options;
78 uint32_t loglevel;
80 /* state */
81 QTAILQ_ENTRY(USBHostDevice) next;
82 int seen, errcount;
83 int bus_num;
84 int addr;
85 char port[16];
87 libusb_device *dev;
88 libusb_device_handle *dh;
89 struct libusb_device_descriptor ddesc;
91 struct {
92 bool detached;
93 bool claimed;
94 } ifs[USB_MAX_INTERFACES];
96 /* callbacks & friends */
97 QEMUBH *bh;
98 Notifier exit;
100 /* request queues */
101 QTAILQ_HEAD(, USBHostRequest) requests;
102 QTAILQ_HEAD(, USBHostIsoRing) isorings;
105 struct USBHostRequest {
106 USBHostDevice *host;
107 USBPacket *p;
108 bool in;
109 struct libusb_transfer *xfer;
110 unsigned char *buffer;
111 unsigned char *cbuf;
112 unsigned int clen;
113 QTAILQ_ENTRY(USBHostRequest) next;
116 struct USBHostIsoXfer {
117 USBHostIsoRing *ring;
118 struct libusb_transfer *xfer;
119 bool copy_complete;
120 unsigned int packet;
121 QTAILQ_ENTRY(USBHostIsoXfer) next;
124 struct USBHostIsoRing {
125 USBHostDevice *host;
126 USBEndpoint *ep;
127 QTAILQ_HEAD(, USBHostIsoXfer) unused;
128 QTAILQ_HEAD(, USBHostIsoXfer) inflight;
129 QTAILQ_HEAD(, USBHostIsoXfer) copy;
130 QTAILQ_ENTRY(USBHostIsoRing) next;
133 static QTAILQ_HEAD(, USBHostDevice) hostdevs =
134 QTAILQ_HEAD_INITIALIZER(hostdevs);
136 static void usb_host_auto_check(void *unused);
137 static void usb_host_release_interfaces(USBHostDevice *s);
138 static void usb_host_nodev(USBHostDevice *s);
139 static void usb_host_attach_kernel(USBHostDevice *s);
141 /* ------------------------------------------------------------------------ */
143 #define CONTROL_TIMEOUT 10000 /* 10 sec */
144 #define BULK_TIMEOUT 0 /* unlimited */
145 #define INTR_TIMEOUT 0 /* unlimited */
147 static const char *speed_name[] = {
148 [LIBUSB_SPEED_UNKNOWN] = "?",
149 [LIBUSB_SPEED_LOW] = "1.5",
150 [LIBUSB_SPEED_FULL] = "12",
151 [LIBUSB_SPEED_HIGH] = "480",
152 [LIBUSB_SPEED_SUPER] = "5000",
155 static const unsigned int speed_map[] = {
156 [LIBUSB_SPEED_LOW] = USB_SPEED_LOW,
157 [LIBUSB_SPEED_FULL] = USB_SPEED_FULL,
158 [LIBUSB_SPEED_HIGH] = USB_SPEED_HIGH,
159 [LIBUSB_SPEED_SUPER] = USB_SPEED_SUPER,
162 static const unsigned int status_map[] = {
163 [LIBUSB_TRANSFER_COMPLETED] = USB_RET_SUCCESS,
164 [LIBUSB_TRANSFER_ERROR] = USB_RET_IOERROR,
165 [LIBUSB_TRANSFER_TIMED_OUT] = USB_RET_IOERROR,
166 [LIBUSB_TRANSFER_CANCELLED] = USB_RET_IOERROR,
167 [LIBUSB_TRANSFER_STALL] = USB_RET_STALL,
168 [LIBUSB_TRANSFER_NO_DEVICE] = USB_RET_NODEV,
169 [LIBUSB_TRANSFER_OVERFLOW] = USB_RET_BABBLE,
172 static const char *err_names[] = {
173 [-LIBUSB_ERROR_IO] = "IO",
174 [-LIBUSB_ERROR_INVALID_PARAM] = "INVALID_PARAM",
175 [-LIBUSB_ERROR_ACCESS] = "ACCESS",
176 [-LIBUSB_ERROR_NO_DEVICE] = "NO_DEVICE",
177 [-LIBUSB_ERROR_NOT_FOUND] = "NOT_FOUND",
178 [-LIBUSB_ERROR_BUSY] = "BUSY",
179 [-LIBUSB_ERROR_TIMEOUT] = "TIMEOUT",
180 [-LIBUSB_ERROR_OVERFLOW] = "OVERFLOW",
181 [-LIBUSB_ERROR_PIPE] = "PIPE",
182 [-LIBUSB_ERROR_INTERRUPTED] = "INTERRUPTED",
183 [-LIBUSB_ERROR_NO_MEM] = "NO_MEM",
184 [-LIBUSB_ERROR_NOT_SUPPORTED] = "NOT_SUPPORTED",
185 [-LIBUSB_ERROR_OTHER] = "OTHER",
188 static libusb_context *ctx;
189 static uint32_t loglevel;
191 static void usb_host_handle_fd(void *opaque)
193 struct timeval tv = { 0, 0 };
194 libusb_handle_events_timeout(ctx, &tv);
197 static void usb_host_add_fd(int fd, short events, void *user_data)
199 qemu_set_fd_handler(fd,
200 (events & POLLIN) ? usb_host_handle_fd : NULL,
201 (events & POLLOUT) ? usb_host_handle_fd : NULL,
202 ctx);
205 static void usb_host_del_fd(int fd, void *user_data)
207 qemu_set_fd_handler(fd, NULL, NULL, NULL);
210 static int usb_host_init(void)
212 const struct libusb_pollfd **poll;
213 int i, rc;
215 if (ctx) {
216 return 0;
218 rc = libusb_init(&ctx);
219 if (rc != 0) {
220 return -1;
222 libusb_set_debug(ctx, loglevel);
224 libusb_set_pollfd_notifiers(ctx, usb_host_add_fd,
225 usb_host_del_fd,
226 ctx);
227 poll = libusb_get_pollfds(ctx);
228 if (poll) {
229 for (i = 0; poll[i] != NULL; i++) {
230 usb_host_add_fd(poll[i]->fd, poll[i]->events, ctx);
233 free(poll);
234 return 0;
237 static int usb_host_get_port(libusb_device *dev, char *port, size_t len)
239 uint8_t path[7];
240 size_t off;
241 int rc, i;
243 rc = libusb_get_port_path(ctx, dev, path, 7);
244 if (rc < 0) {
245 return 0;
247 off = snprintf(port, len, "%d", path[0]);
248 for (i = 1; i < rc; i++) {
249 off += snprintf(port+off, len-off, ".%d", path[i]);
251 return off;
254 static void usb_host_libusb_error(const char *func, int rc)
256 const char *errname;
258 if (rc >= 0) {
259 return;
262 if (-rc < ARRAY_SIZE(err_names) && err_names[-rc]) {
263 errname = err_names[-rc];
264 } else {
265 errname = "?";
267 fprintf(stderr, "%s: %d [%s]\n", func, rc, errname);
270 /* ------------------------------------------------------------------------ */
272 static bool usb_host_use_combining(USBEndpoint *ep)
274 int type;
276 if (!ep->pipeline) {
277 return false;
279 if (ep->pid != USB_TOKEN_IN) {
280 return false;
282 type = usb_ep_get_type(ep->dev, ep->pid, ep->nr);
283 if (type != USB_ENDPOINT_XFER_BULK) {
284 return false;
286 return true;
289 /* ------------------------------------------------------------------------ */
291 static USBHostRequest *usb_host_req_alloc(USBHostDevice *s, USBPacket *p,
292 bool in, size_t bufsize)
294 USBHostRequest *r = g_new0(USBHostRequest, 1);
296 r->host = s;
297 r->p = p;
298 r->in = in;
299 r->xfer = libusb_alloc_transfer(0);
300 if (bufsize) {
301 r->buffer = g_malloc(bufsize);
303 QTAILQ_INSERT_TAIL(&s->requests, r, next);
304 return r;
307 static void usb_host_req_free(USBHostRequest *r)
309 if (r->host) {
310 QTAILQ_REMOVE(&r->host->requests, r, next);
312 libusb_free_transfer(r->xfer);
313 g_free(r->buffer);
314 g_free(r);
317 static USBHostRequest *usb_host_req_find(USBHostDevice *s, USBPacket *p)
319 USBHostRequest *r;
321 QTAILQ_FOREACH(r, &s->requests, next) {
322 if (r->p == p) {
323 return r;
326 return NULL;
329 static void usb_host_req_complete_ctrl(struct libusb_transfer *xfer)
331 USBHostRequest *r = xfer->user_data;
332 USBHostDevice *s = r->host;
333 bool disconnect = (xfer->status == LIBUSB_TRANSFER_NO_DEVICE);
335 if (r->p == NULL) {
336 goto out; /* request was canceled */
339 r->p->status = status_map[xfer->status];
340 r->p->actual_length = xfer->actual_length;
341 if (r->in && xfer->actual_length) {
342 memcpy(r->cbuf, r->buffer + 8, xfer->actual_length);
344 trace_usb_host_req_complete(s->bus_num, s->addr, r->p,
345 r->p->status, r->p->actual_length);
346 usb_generic_async_ctrl_complete(USB_DEVICE(s), r->p);
348 out:
349 usb_host_req_free(r);
350 if (disconnect) {
351 usb_host_nodev(s);
355 static void usb_host_req_complete_data(struct libusb_transfer *xfer)
357 USBHostRequest *r = xfer->user_data;
358 USBHostDevice *s = r->host;
359 bool disconnect = (xfer->status == LIBUSB_TRANSFER_NO_DEVICE);
361 if (r->p == NULL) {
362 goto out; /* request was canceled */
365 r->p->status = status_map[xfer->status];
366 if (r->in && xfer->actual_length) {
367 usb_packet_copy(r->p, r->buffer, xfer->actual_length);
369 trace_usb_host_req_complete(s->bus_num, s->addr, r->p,
370 r->p->status, r->p->actual_length);
371 if (usb_host_use_combining(r->p->ep)) {
372 usb_combined_input_packet_complete(USB_DEVICE(s), r->p);
373 } else {
374 usb_packet_complete(USB_DEVICE(s), r->p);
377 out:
378 usb_host_req_free(r);
379 if (disconnect) {
380 usb_host_nodev(s);
384 static void usb_host_req_abort(USBHostRequest *r)
386 USBHostDevice *s = r->host;
387 bool inflight = (r->p && r->p->state == USB_RET_ASYNC);
389 if (inflight) {
390 r->p->status = USB_RET_NODEV;
391 trace_usb_host_req_complete(s->bus_num, s->addr, r->p,
392 r->p->status, r->p->actual_length);
393 if (r->p->ep->nr == 0) {
394 usb_generic_async_ctrl_complete(USB_DEVICE(s), r->p);
395 } else {
396 usb_packet_complete(USB_DEVICE(s), r->p);
398 r->p = NULL;
401 QTAILQ_REMOVE(&r->host->requests, r, next);
402 r->host = NULL;
404 if (inflight) {
405 libusb_cancel_transfer(r->xfer);
409 /* ------------------------------------------------------------------------ */
411 static void usb_host_req_complete_iso(struct libusb_transfer *transfer)
413 USBHostIsoXfer *xfer = transfer->user_data;
415 if (!xfer) {
416 /* USBHostIsoXfer released while inflight */
417 g_free(transfer->buffer);
418 libusb_free_transfer(transfer);
419 return;
422 QTAILQ_REMOVE(&xfer->ring->inflight, xfer, next);
423 if (QTAILQ_EMPTY(&xfer->ring->inflight)) {
424 USBHostDevice *s = xfer->ring->host;
425 trace_usb_host_iso_stop(s->bus_num, s->addr, xfer->ring->ep->nr);
427 if (xfer->ring->ep->pid == USB_TOKEN_IN) {
428 QTAILQ_INSERT_TAIL(&xfer->ring->copy, xfer, next);
429 } else {
430 QTAILQ_INSERT_TAIL(&xfer->ring->unused, xfer, next);
434 static USBHostIsoRing *usb_host_iso_alloc(USBHostDevice *s, USBEndpoint *ep)
436 USBHostIsoRing *ring = g_new0(USBHostIsoRing, 1);
437 USBHostIsoXfer *xfer;
438 /* FIXME: check interval (for now assume one xfer per frame) */
439 int packets = s->iso_urb_frames;
440 int i;
442 ring->host = s;
443 ring->ep = ep;
444 QTAILQ_INIT(&ring->unused);
445 QTAILQ_INIT(&ring->inflight);
446 QTAILQ_INIT(&ring->copy);
447 QTAILQ_INSERT_TAIL(&s->isorings, ring, next);
449 for (i = 0; i < s->iso_urb_count; i++) {
450 xfer = g_new0(USBHostIsoXfer, 1);
451 xfer->ring = ring;
452 xfer->xfer = libusb_alloc_transfer(packets);
453 xfer->xfer->dev_handle = s->dh;
454 xfer->xfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
456 xfer->xfer->endpoint = ring->ep->nr;
457 if (ring->ep->pid == USB_TOKEN_IN) {
458 xfer->xfer->endpoint |= USB_DIR_IN;
460 xfer->xfer->callback = usb_host_req_complete_iso;
461 xfer->xfer->user_data = xfer;
463 xfer->xfer->num_iso_packets = packets;
464 xfer->xfer->length = ring->ep->max_packet_size * packets;
465 xfer->xfer->buffer = g_malloc0(xfer->xfer->length);
467 QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
470 return ring;
473 static USBHostIsoRing *usb_host_iso_find(USBHostDevice *s, USBEndpoint *ep)
475 USBHostIsoRing *ring;
477 QTAILQ_FOREACH(ring, &s->isorings, next) {
478 if (ring->ep == ep) {
479 return ring;
482 return NULL;
485 static void usb_host_iso_reset_xfer(USBHostIsoXfer *xfer)
487 libusb_set_iso_packet_lengths(xfer->xfer,
488 xfer->ring->ep->max_packet_size);
489 xfer->packet = 0;
490 xfer->copy_complete = false;
493 static void usb_host_iso_free_xfer(USBHostIsoXfer *xfer, bool inflight)
495 if (inflight) {
496 xfer->xfer->user_data = NULL;
497 } else {
498 g_free(xfer->xfer->buffer);
499 libusb_free_transfer(xfer->xfer);
501 g_free(xfer);
504 static void usb_host_iso_free(USBHostIsoRing *ring)
506 USBHostIsoXfer *xfer;
508 while ((xfer = QTAILQ_FIRST(&ring->inflight)) != NULL) {
509 QTAILQ_REMOVE(&ring->inflight, xfer, next);
510 usb_host_iso_free_xfer(xfer, true);
512 while ((xfer = QTAILQ_FIRST(&ring->unused)) != NULL) {
513 QTAILQ_REMOVE(&ring->unused, xfer, next);
514 usb_host_iso_free_xfer(xfer, false);
516 while ((xfer = QTAILQ_FIRST(&ring->copy)) != NULL) {
517 QTAILQ_REMOVE(&ring->copy, xfer, next);
518 usb_host_iso_free_xfer(xfer, false);
521 QTAILQ_REMOVE(&ring->host->isorings, ring, next);
522 g_free(ring);
525 static void usb_host_iso_free_all(USBHostDevice *s)
527 USBHostIsoRing *ring;
529 while ((ring = QTAILQ_FIRST(&s->isorings)) != NULL) {
530 usb_host_iso_free(ring);
534 static bool usb_host_iso_data_copy(USBHostIsoXfer *xfer, USBPacket *p)
536 unsigned int psize;
537 unsigned char *buf;
539 buf = libusb_get_iso_packet_buffer_simple(xfer->xfer, xfer->packet);
540 if (p->pid == USB_TOKEN_OUT) {
541 psize = p->iov.size;
542 if (psize > xfer->ring->ep->max_packet_size) {
543 /* should not happen (guest bug) */
544 psize = xfer->ring->ep->max_packet_size;
546 xfer->xfer->iso_packet_desc[xfer->packet].length = psize;
547 } else {
548 psize = xfer->xfer->iso_packet_desc[xfer->packet].actual_length;
549 if (psize > p->iov.size) {
550 /* should not happen (guest bug) */
551 psize = p->iov.size;
554 usb_packet_copy(p, buf, psize);
555 xfer->packet++;
556 xfer->copy_complete = (xfer->packet == xfer->xfer->num_iso_packets);
557 return xfer->copy_complete;
560 static void usb_host_iso_data_in(USBHostDevice *s, USBPacket *p)
562 USBHostIsoRing *ring;
563 USBHostIsoXfer *xfer;
564 bool disconnect = false;
565 int rc;
567 ring = usb_host_iso_find(s, p->ep);
568 if (ring == NULL) {
569 ring = usb_host_iso_alloc(s, p->ep);
572 /* copy data to guest */
573 xfer = QTAILQ_FIRST(&ring->copy);
574 if (xfer != NULL) {
575 if (usb_host_iso_data_copy(xfer, p)) {
576 QTAILQ_REMOVE(&ring->copy, xfer, next);
577 QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
581 /* submit empty bufs to host */
582 while ((xfer = QTAILQ_FIRST(&ring->unused)) != NULL) {
583 QTAILQ_REMOVE(&ring->unused, xfer, next);
584 usb_host_iso_reset_xfer(xfer);
585 rc = libusb_submit_transfer(xfer->xfer);
586 if (rc != 0) {
587 usb_host_libusb_error("libusb_submit_transfer [iso]", rc);
588 QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
589 if (rc == LIBUSB_ERROR_NO_DEVICE) {
590 disconnect = true;
592 break;
594 if (QTAILQ_EMPTY(&ring->inflight)) {
595 trace_usb_host_iso_start(s->bus_num, s->addr, p->ep->nr);
597 QTAILQ_INSERT_TAIL(&ring->inflight, xfer, next);
600 if (disconnect) {
601 usb_host_nodev(s);
605 static void usb_host_iso_data_out(USBHostDevice *s, USBPacket *p)
607 USBHostIsoRing *ring;
608 USBHostIsoXfer *xfer;
609 bool disconnect = false;
610 int rc, filled = 0;
612 ring = usb_host_iso_find(s, p->ep);
613 if (ring == NULL) {
614 ring = usb_host_iso_alloc(s, p->ep);
617 /* copy data from guest */
618 xfer = QTAILQ_FIRST(&ring->copy);
619 while (xfer != NULL && xfer->copy_complete) {
620 filled++;
621 xfer = QTAILQ_NEXT(xfer, next);
623 if (xfer == NULL) {
624 xfer = QTAILQ_FIRST(&ring->unused);
625 if (xfer == NULL) {
626 trace_usb_host_iso_out_of_bufs(s->bus_num, s->addr, p->ep->nr);
627 return;
629 QTAILQ_REMOVE(&ring->unused, xfer, next);
630 usb_host_iso_reset_xfer(xfer);
631 QTAILQ_INSERT_TAIL(&ring->copy, xfer, next);
633 usb_host_iso_data_copy(xfer, p);
635 if (QTAILQ_EMPTY(&ring->inflight)) {
636 /* wait until half of our buffers are filled
637 before kicking the iso out stream */
638 if (filled*2 < s->iso_urb_count) {
639 return;
643 /* submit filled bufs to host */
644 while ((xfer = QTAILQ_FIRST(&ring->copy)) != NULL &&
645 xfer->copy_complete) {
646 QTAILQ_REMOVE(&ring->copy, xfer, next);
647 rc = libusb_submit_transfer(xfer->xfer);
648 if (rc != 0) {
649 usb_host_libusb_error("libusb_submit_transfer [iso]", rc);
650 QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
651 if (rc == LIBUSB_ERROR_NO_DEVICE) {
652 disconnect = true;
654 break;
656 if (QTAILQ_EMPTY(&ring->inflight)) {
657 trace_usb_host_iso_start(s->bus_num, s->addr, p->ep->nr);
659 QTAILQ_INSERT_TAIL(&ring->inflight, xfer, next);
662 if (disconnect) {
663 usb_host_nodev(s);
667 /* ------------------------------------------------------------------------ */
669 static void usb_host_ep_update(USBHostDevice *s)
671 static const char *tname[] = {
672 [USB_ENDPOINT_XFER_CONTROL] = "control",
673 [USB_ENDPOINT_XFER_ISOC] = "isoc",
674 [USB_ENDPOINT_XFER_BULK] = "bulk",
675 [USB_ENDPOINT_XFER_INT] = "int",
677 USBDevice *udev = USB_DEVICE(s);
678 struct libusb_config_descriptor *conf;
679 const struct libusb_interface_descriptor *intf;
680 const struct libusb_endpoint_descriptor *endp;
681 uint8_t devep, type;
682 int pid, ep;
683 int rc, i, e;
685 usb_ep_reset(udev);
686 rc = libusb_get_active_config_descriptor(s->dev, &conf);
687 if (rc != 0) {
688 return;
690 trace_usb_host_parse_config(s->bus_num, s->addr,
691 conf->bConfigurationValue, true);
693 for (i = 0; i < conf->bNumInterfaces; i++) {
694 assert(udev->altsetting[i] < conf->interface[i].num_altsetting);
695 intf = &conf->interface[i].altsetting[udev->altsetting[i]];
696 trace_usb_host_parse_interface(s->bus_num, s->addr,
697 intf->bInterfaceNumber,
698 intf->bAlternateSetting, true);
699 for (e = 0; e < intf->bNumEndpoints; e++) {
700 endp = &intf->endpoint[e];
702 devep = endp->bEndpointAddress;
703 pid = (devep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT;
704 ep = devep & 0xf;
705 type = endp->bmAttributes & 0x3;
707 if (ep == 0) {
708 trace_usb_host_parse_error(s->bus_num, s->addr,
709 "invalid endpoint address");
710 return;
712 if (usb_ep_get_type(udev, pid, ep) != USB_ENDPOINT_XFER_INVALID) {
713 trace_usb_host_parse_error(s->bus_num, s->addr,
714 "duplicate endpoint address");
715 return;
718 trace_usb_host_parse_endpoint(s->bus_num, s->addr, ep,
719 (devep & USB_DIR_IN) ? "in" : "out",
720 tname[type], true);
721 usb_ep_set_max_packet_size(udev, pid, ep,
722 endp->wMaxPacketSize);
723 usb_ep_set_type(udev, pid, ep, type);
724 usb_ep_set_ifnum(udev, pid, ep, i);
725 usb_ep_set_halted(udev, pid, ep, 0);
729 libusb_free_config_descriptor(conf);
732 static int usb_host_open(USBHostDevice *s, libusb_device *dev)
734 USBDevice *udev = USB_DEVICE(s);
735 int bus_num = libusb_get_bus_number(dev);
736 int addr = libusb_get_device_address(dev);
737 int rc;
739 trace_usb_host_open_started(bus_num, addr);
741 if (s->dh != NULL) {
742 goto fail;
744 rc = libusb_open(dev, &s->dh);
745 if (rc != 0) {
746 goto fail;
749 libusb_get_device_descriptor(dev, &s->ddesc);
750 s->dev = dev;
751 s->bus_num = bus_num;
752 s->addr = addr;
753 usb_host_get_port(s->dev, s->port, sizeof(s->port));
755 usb_ep_init(udev);
756 usb_host_ep_update(s);
758 udev->speed = speed_map[libusb_get_device_speed(dev)];
759 udev->speedmask = (1 << udev->speed);
760 #if 0
761 if (udev->speed == USB_SPEED_HIGH && usb_linux_full_speed_compat(dev)) {
762 udev->speedmask |= USB_SPEED_MASK_FULL;
764 #endif
766 if (s->ddesc.iProduct) {
767 libusb_get_string_descriptor_ascii(s->dh, s->ddesc.iProduct,
768 (unsigned char *)udev->product_desc,
769 sizeof(udev->product_desc));
770 } else {
771 snprintf(udev->product_desc, sizeof(udev->product_desc),
772 "host:%d.%d", bus_num, addr);
775 rc = usb_device_attach(udev);
776 if (rc) {
777 goto fail;
780 trace_usb_host_open_success(bus_num, addr);
781 return 0;
783 fail:
784 trace_usb_host_open_failure(bus_num, addr);
785 if (s->dh != NULL) {
786 libusb_close(s->dh);
787 s->dh = NULL;
788 s->dev = NULL;
790 return -1;
793 static void usb_host_abort_xfers(USBHostDevice *s)
795 USBHostRequest *r, *rtmp;
797 QTAILQ_FOREACH_SAFE(r, &s->requests, next, rtmp) {
798 usb_host_req_abort(r);
802 static int usb_host_close(USBHostDevice *s)
804 USBDevice *udev = USB_DEVICE(s);
806 if (s->dh == NULL) {
807 return -1;
810 trace_usb_host_close(s->bus_num, s->addr);
812 usb_host_abort_xfers(s);
813 usb_host_iso_free_all(s);
815 if (udev->attached) {
816 usb_device_detach(udev);
819 usb_host_release_interfaces(s);
820 libusb_reset_device(s->dh);
821 usb_host_attach_kernel(s);
822 libusb_close(s->dh);
823 s->dh = NULL;
824 s->dev = NULL;
826 usb_host_auto_check(NULL);
827 return 0;
830 static void usb_host_nodev_bh(void *opaque)
832 USBHostDevice *s = opaque;
833 usb_host_close(s);
836 static void usb_host_nodev(USBHostDevice *s)
838 if (!s->bh) {
839 s->bh = qemu_bh_new(usb_host_nodev_bh, s);
841 qemu_bh_schedule(s->bh);
844 static void usb_host_exit_notifier(struct Notifier *n, void *data)
846 USBHostDevice *s = container_of(n, USBHostDevice, exit);
848 if (s->dh) {
849 usb_host_release_interfaces(s);
850 usb_host_attach_kernel(s);
854 static int usb_host_initfn(USBDevice *udev)
856 USBHostDevice *s = USB_HOST_DEVICE(udev);
858 loglevel = s->loglevel;
859 udev->auto_attach = 0;
860 QTAILQ_INIT(&s->requests);
861 QTAILQ_INIT(&s->isorings);
863 s->exit.notify = usb_host_exit_notifier;
864 qemu_add_exit_notifier(&s->exit);
866 QTAILQ_INSERT_TAIL(&hostdevs, s, next);
867 add_boot_device_path(s->bootindex, &udev->qdev, NULL);
868 usb_host_auto_check(NULL);
869 return 0;
872 static void usb_host_handle_destroy(USBDevice *udev)
874 USBHostDevice *s = USB_HOST_DEVICE(udev);
876 qemu_remove_exit_notifier(&s->exit);
877 QTAILQ_REMOVE(&hostdevs, s, next);
878 usb_host_close(s);
881 static void usb_host_cancel_packet(USBDevice *udev, USBPacket *p)
883 USBHostDevice *s = USB_HOST_DEVICE(udev);
884 USBHostRequest *r;
886 if (p->combined) {
887 usb_combined_packet_cancel(udev, p);
888 return;
891 trace_usb_host_req_canceled(s->bus_num, s->addr, p);
893 r = usb_host_req_find(s, p);
894 if (r && r->p) {
895 r->p = NULL; /* mark as dead */
896 libusb_cancel_transfer(r->xfer);
900 static void usb_host_detach_kernel(USBHostDevice *s)
902 struct libusb_config_descriptor *conf;
903 int rc, i;
905 rc = libusb_get_active_config_descriptor(s->dev, &conf);
906 if (rc != 0) {
907 return;
909 for (i = 0; i < conf->bNumInterfaces; i++) {
910 rc = libusb_kernel_driver_active(s->dh, i);
911 usb_host_libusb_error("libusb_kernel_driver_active", rc);
912 if (rc != 1) {
913 continue;
915 trace_usb_host_detach_kernel(s->bus_num, s->addr, i);
916 rc = libusb_detach_kernel_driver(s->dh, i);
917 usb_host_libusb_error("libusb_detach_kernel_driver", rc);
918 s->ifs[i].detached = true;
920 libusb_free_config_descriptor(conf);
923 static void usb_host_attach_kernel(USBHostDevice *s)
925 struct libusb_config_descriptor *conf;
926 int rc, i;
928 rc = libusb_get_active_config_descriptor(s->dev, &conf);
929 if (rc != 0) {
930 return;
932 for (i = 0; i < conf->bNumInterfaces; i++) {
933 if (!s->ifs[i].detached) {
934 continue;
936 trace_usb_host_attach_kernel(s->bus_num, s->addr, i);
937 libusb_attach_kernel_driver(s->dh, i);
938 s->ifs[i].detached = false;
940 libusb_free_config_descriptor(conf);
943 static int usb_host_claim_interfaces(USBHostDevice *s, int configuration)
945 USBDevice *udev = USB_DEVICE(s);
946 struct libusb_config_descriptor *conf;
947 int rc, i;
949 for (i = 0; i < USB_MAX_INTERFACES; i++) {
950 udev->altsetting[i] = 0;
952 udev->ninterfaces = 0;
953 udev->configuration = 0;
955 if (configuration == 0) {
956 /* address state - ignore */
957 return USB_RET_SUCCESS;
960 usb_host_detach_kernel(s);
962 rc = libusb_get_active_config_descriptor(s->dev, &conf);
963 if (rc != 0) {
964 return USB_RET_STALL;
967 for (i = 0; i < conf->bNumInterfaces; i++) {
968 trace_usb_host_claim_interface(s->bus_num, s->addr, configuration, i);
969 rc = libusb_claim_interface(s->dh, i);
970 usb_host_libusb_error("libusb_claim_interface", rc);
971 if (rc != 0) {
972 return USB_RET_STALL;
974 s->ifs[i].claimed = true;
977 udev->ninterfaces = conf->bNumInterfaces;
978 udev->configuration = configuration;
980 libusb_free_config_descriptor(conf);
981 return USB_RET_SUCCESS;
984 static void usb_host_release_interfaces(USBHostDevice *s)
986 USBDevice *udev = USB_DEVICE(s);
987 int i, rc;
989 for (i = 0; i < udev->ninterfaces; i++) {
990 if (!s->ifs[i].claimed) {
991 continue;
993 trace_usb_host_release_interface(s->bus_num, s->addr, i);
994 rc = libusb_release_interface(s->dh, i);
995 usb_host_libusb_error("libusb_release_interface", rc);
996 s->ifs[i].claimed = false;
1000 static void usb_host_set_address(USBHostDevice *s, int addr)
1002 USBDevice *udev = USB_DEVICE(s);
1004 trace_usb_host_set_address(s->bus_num, s->addr, addr);
1005 udev->addr = addr;
1008 static void usb_host_set_config(USBHostDevice *s, int config, USBPacket *p)
1010 int rc;
1012 trace_usb_host_set_config(s->bus_num, s->addr, config);
1014 usb_host_release_interfaces(s);
1015 usb_host_detach_kernel(s);
1016 rc = libusb_set_configuration(s->dh, config);
1017 if (rc != 0) {
1018 usb_host_libusb_error("libusb_set_configuration", rc);
1019 p->status = USB_RET_STALL;
1020 if (rc == LIBUSB_ERROR_NO_DEVICE) {
1021 usb_host_nodev(s);
1023 return;
1025 p->status = usb_host_claim_interfaces(s, config);
1026 if (p->status != USB_RET_SUCCESS) {
1027 return;
1029 usb_host_ep_update(s);
1032 static void usb_host_set_interface(USBHostDevice *s, int iface, int alt,
1033 USBPacket *p)
1035 USBDevice *udev = USB_DEVICE(s);
1036 int rc;
1038 trace_usb_host_set_interface(s->bus_num, s->addr, iface, alt);
1040 usb_host_iso_free_all(s);
1042 if (iface >= USB_MAX_INTERFACES) {
1043 p->status = USB_RET_STALL;
1044 return;
1047 rc = libusb_set_interface_alt_setting(s->dh, iface, alt);
1048 if (rc != 0) {
1049 usb_host_libusb_error("libusb_set_interface_alt_setting", rc);
1050 p->status = USB_RET_STALL;
1051 if (rc == LIBUSB_ERROR_NO_DEVICE) {
1052 usb_host_nodev(s);
1054 return;
1057 udev->altsetting[iface] = alt;
1058 usb_host_ep_update(s);
1061 static void usb_host_handle_control(USBDevice *udev, USBPacket *p,
1062 int request, int value, int index,
1063 int length, uint8_t *data)
1065 USBHostDevice *s = USB_HOST_DEVICE(udev);
1066 USBHostRequest *r;
1067 int rc;
1069 trace_usb_host_req_control(s->bus_num, s->addr, p, request, value, index);
1071 if (s->dh == NULL) {
1072 p->status = USB_RET_NODEV;
1073 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1074 return;
1077 switch (request) {
1078 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
1079 usb_host_set_address(s, value);
1080 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1081 return;
1083 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
1084 usb_host_set_config(s, value & 0xff, p);
1085 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1086 return;
1088 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
1089 usb_host_set_interface(s, index, value, p);
1090 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1091 return;
1093 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
1094 if (value == 0) { /* clear halt */
1095 int pid = (index & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT;
1096 libusb_clear_halt(s->dh, index);
1097 usb_ep_set_halted(udev, pid, index & 0x0f, 0);
1098 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1099 return;
1103 r = usb_host_req_alloc(s, p, (request >> 8) & USB_DIR_IN, length + 8);
1104 r->cbuf = data;
1105 r->clen = length;
1106 memcpy(r->buffer, udev->setup_buf, 8);
1107 if (!r->in) {
1108 memcpy(r->buffer + 8, r->cbuf, r->clen);
1111 libusb_fill_control_transfer(r->xfer, s->dh, r->buffer,
1112 usb_host_req_complete_ctrl, r,
1113 CONTROL_TIMEOUT);
1114 rc = libusb_submit_transfer(r->xfer);
1115 if (rc != 0) {
1116 p->status = USB_RET_NODEV;
1117 trace_usb_host_req_complete(s->bus_num, s->addr, p,
1118 p->status, p->actual_length);
1119 if (rc == LIBUSB_ERROR_NO_DEVICE) {
1120 usb_host_nodev(s);
1122 return;
1125 p->status = USB_RET_ASYNC;
1128 static void usb_host_handle_data(USBDevice *udev, USBPacket *p)
1130 USBHostDevice *s = USB_HOST_DEVICE(udev);
1131 USBHostRequest *r;
1132 size_t size;
1133 int ep, rc;
1135 if (usb_host_use_combining(p->ep) && p->state == USB_PACKET_SETUP) {
1136 p->status = USB_RET_ADD_TO_QUEUE;
1137 return;
1140 trace_usb_host_req_data(s->bus_num, s->addr, p,
1141 p->pid == USB_TOKEN_IN,
1142 p->ep->nr, p->iov.size);
1144 if (s->dh == NULL) {
1145 p->status = USB_RET_NODEV;
1146 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1147 return;
1149 if (p->ep->halted) {
1150 p->status = USB_RET_STALL;
1151 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1152 return;
1155 switch (usb_ep_get_type(udev, p->pid, p->ep->nr)) {
1156 case USB_ENDPOINT_XFER_BULK:
1157 size = usb_packet_size(p);
1158 r = usb_host_req_alloc(s, p, p->pid == USB_TOKEN_IN, size);
1159 if (!r->in) {
1160 usb_packet_copy(p, r->buffer, size);
1162 ep = p->ep->nr | (r->in ? USB_DIR_IN : 0);
1163 libusb_fill_bulk_transfer(r->xfer, s->dh, ep,
1164 r->buffer, size,
1165 usb_host_req_complete_data, r,
1166 BULK_TIMEOUT);
1167 break;
1168 case USB_ENDPOINT_XFER_INT:
1169 r = usb_host_req_alloc(s, p, p->pid == USB_TOKEN_IN, p->iov.size);
1170 if (!r->in) {
1171 usb_packet_copy(p, r->buffer, p->iov.size);
1173 ep = p->ep->nr | (r->in ? USB_DIR_IN : 0);
1174 libusb_fill_interrupt_transfer(r->xfer, s->dh, ep,
1175 r->buffer, p->iov.size,
1176 usb_host_req_complete_data, r,
1177 INTR_TIMEOUT);
1178 break;
1179 case USB_ENDPOINT_XFER_ISOC:
1180 if (p->pid == USB_TOKEN_IN) {
1181 usb_host_iso_data_in(s, p);
1182 } else {
1183 usb_host_iso_data_out(s, p);
1185 trace_usb_host_req_complete(s->bus_num, s->addr, p,
1186 p->status, p->actual_length);
1187 return;
1188 default:
1189 p->status = USB_RET_STALL;
1190 trace_usb_host_req_complete(s->bus_num, s->addr, p,
1191 p->status, p->actual_length);
1192 return;
1195 rc = libusb_submit_transfer(r->xfer);
1196 if (rc != 0) {
1197 p->status = USB_RET_NODEV;
1198 trace_usb_host_req_complete(s->bus_num, s->addr, p,
1199 p->status, p->actual_length);
1200 if (rc == LIBUSB_ERROR_NO_DEVICE) {
1201 usb_host_nodev(s);
1203 return;
1206 p->status = USB_RET_ASYNC;
1209 static void usb_host_flush_ep_queue(USBDevice *dev, USBEndpoint *ep)
1211 if (usb_host_use_combining(ep)) {
1212 usb_ep_combine_input_packets(ep);
1216 static void usb_host_handle_reset(USBDevice *udev)
1218 USBHostDevice *s = USB_HOST_DEVICE(udev);
1220 trace_usb_host_reset(s->bus_num, s->addr);
1222 if (udev->configuration == 0) {
1223 return;
1225 usb_host_release_interfaces(s);
1226 libusb_reset_device(s->dh);
1227 usb_host_claim_interfaces(s, 0);
1228 usb_host_ep_update(s);
1231 static const VMStateDescription vmstate_usb_host = {
1232 .name = "usb-host",
1233 .unmigratable = 1,
1234 .fields = (VMStateField[]) {
1235 VMSTATE_USB_DEVICE(parent_obj, USBHostDevice),
1236 VMSTATE_END_OF_LIST()
1240 static Property usb_host_dev_properties[] = {
1241 DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0),
1242 DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0),
1243 DEFINE_PROP_STRING("hostport", USBHostDevice, match.port),
1244 DEFINE_PROP_HEX32("vendorid", USBHostDevice, match.vendor_id, 0),
1245 DEFINE_PROP_HEX32("productid", USBHostDevice, match.product_id, 0),
1246 DEFINE_PROP_UINT32("isobufs", USBHostDevice, iso_urb_count, 4),
1247 DEFINE_PROP_UINT32("isobsize", USBHostDevice, iso_urb_frames, 32),
1248 DEFINE_PROP_INT32("bootindex", USBHostDevice, bootindex, -1),
1249 DEFINE_PROP_UINT32("loglevel", USBHostDevice, loglevel,
1250 LIBUSB_LOG_LEVEL_WARNING),
1251 DEFINE_PROP_BIT("pipeline", USBHostDevice, options,
1252 USB_HOST_OPT_PIPELINE, true),
1253 DEFINE_PROP_END_OF_LIST(),
1256 static void usb_host_class_initfn(ObjectClass *klass, void *data)
1258 DeviceClass *dc = DEVICE_CLASS(klass);
1259 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
1261 uc->init = usb_host_initfn;
1262 uc->product_desc = "USB Host Device";
1263 uc->cancel_packet = usb_host_cancel_packet;
1264 uc->handle_data = usb_host_handle_data;
1265 uc->handle_control = usb_host_handle_control;
1266 uc->handle_reset = usb_host_handle_reset;
1267 uc->handle_destroy = usb_host_handle_destroy;
1268 uc->flush_ep_queue = usb_host_flush_ep_queue;
1269 dc->vmsd = &vmstate_usb_host;
1270 dc->props = usb_host_dev_properties;
1273 static TypeInfo usb_host_dev_info = {
1274 .name = TYPE_USB_HOST_DEVICE,
1275 .parent = TYPE_USB_DEVICE,
1276 .instance_size = sizeof(USBHostDevice),
1277 .class_init = usb_host_class_initfn,
1280 static void usb_host_register_types(void)
1282 type_register_static(&usb_host_dev_info);
1285 type_init(usb_host_register_types)
1287 /* ------------------------------------------------------------------------ */
1289 static QEMUTimer *usb_auto_timer;
1290 static VMChangeStateEntry *usb_vmstate;
1292 static void usb_host_vm_state(void *unused, int running, RunState state)
1294 if (running) {
1295 usb_host_auto_check(unused);
1299 static void usb_host_auto_check(void *unused)
1301 struct USBHostDevice *s;
1302 struct USBAutoFilter *f;
1303 libusb_device **devs;
1304 struct libusb_device_descriptor ddesc;
1305 int unconnected = 0;
1306 int i, n;
1308 if (usb_host_init() != 0) {
1309 return;
1312 if (runstate_is_running()) {
1313 n = libusb_get_device_list(ctx, &devs);
1314 for (i = 0; i < n; i++) {
1315 if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) {
1316 continue;
1318 if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) {
1319 continue;
1321 QTAILQ_FOREACH(s, &hostdevs, next) {
1322 f = &s->match;
1323 if (f->bus_num > 0 &&
1324 f->bus_num != libusb_get_bus_number(devs[i])) {
1325 continue;
1327 if (f->addr > 0 &&
1328 f->addr != libusb_get_device_address(devs[i])) {
1329 continue;
1331 if (f->port != NULL) {
1332 char port[16] = "-";
1333 usb_host_get_port(devs[i], port, sizeof(port));
1334 if (strcmp(f->port, port) != 0) {
1335 continue;
1338 if (f->vendor_id > 0 &&
1339 f->vendor_id != ddesc.idVendor) {
1340 continue;
1342 if (f->product_id > 0 &&
1343 f->product_id != ddesc.idProduct) {
1344 continue;
1347 /* We got a match */
1348 s->seen++;
1349 if (s->errcount >= 3) {
1350 continue;
1352 if (s->dh != NULL) {
1353 continue;
1355 if (usb_host_open(s, devs[i]) < 0) {
1356 s->errcount++;
1357 continue;
1359 break;
1362 libusb_free_device_list(devs, 1);
1364 QTAILQ_FOREACH(s, &hostdevs, next) {
1365 if (s->dh == NULL) {
1366 unconnected++;
1368 if (s->seen == 0) {
1369 if (s->dh) {
1370 usb_host_close(s);
1372 s->errcount = 0;
1374 s->seen = 0;
1377 #if 0
1378 if (unconnected == 0) {
1379 /* nothing to watch */
1380 if (usb_auto_timer) {
1381 qemu_del_timer(usb_auto_timer);
1382 trace_usb_host_auto_scan_disabled();
1384 return;
1386 #endif
1389 if (!usb_vmstate) {
1390 usb_vmstate = qemu_add_vm_change_state_handler(usb_host_vm_state, NULL);
1392 if (!usb_auto_timer) {
1393 usb_auto_timer = qemu_new_timer_ms(rt_clock, usb_host_auto_check, NULL);
1394 if (!usb_auto_timer) {
1395 return;
1397 trace_usb_host_auto_scan_enabled();
1399 qemu_mod_timer(usb_auto_timer, qemu_get_clock_ms(rt_clock) + 2000);
1402 void usb_host_info(Monitor *mon, const QDict *qdict)
1404 libusb_device **devs;
1405 struct libusb_device_descriptor ddesc;
1406 char port[16];
1407 int i, n;
1409 if (usb_host_init() != 0) {
1410 return;
1413 n = libusb_get_device_list(ctx, &devs);
1414 for (i = 0; i < n; i++) {
1415 if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) {
1416 continue;
1418 if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) {
1419 continue;
1421 usb_host_get_port(devs[i], port, sizeof(port));
1422 monitor_printf(mon, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1423 libusb_get_bus_number(devs[i]),
1424 libusb_get_device_address(devs[i]),
1425 port,
1426 speed_name[libusb_get_device_speed(devs[i])]);
1427 monitor_printf(mon, " Class %02x:", ddesc.bDeviceClass);
1428 monitor_printf(mon, " USB device %04x:%04x",
1429 ddesc.idVendor, ddesc.idProduct);
1430 if (ddesc.iProduct) {
1431 libusb_device_handle *handle;
1432 if (libusb_open(devs[i], &handle) == 0) {
1433 unsigned char name[64] = "";
1434 libusb_get_string_descriptor_ascii(handle,
1435 ddesc.iProduct,
1436 name, sizeof(name));
1437 libusb_close(handle);
1438 monitor_printf(mon, ", %s", name);
1441 monitor_printf(mon, "\n");
1443 libusb_free_device_list(devs, 1);