usb-host-libusb: Configuration 0 may be a valid configuration
[qemu.git] / hw / usb / host-libusb.c
blob35bae55e3a79cefaa14f68b8cf03ad0314659d7a
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_nodev;
98 QEMUBH *bh_postld;
99 Notifier exit;
101 /* request queues */
102 QTAILQ_HEAD(, USBHostRequest) requests;
103 QTAILQ_HEAD(, USBHostIsoRing) isorings;
106 struct USBHostRequest {
107 USBHostDevice *host;
108 USBPacket *p;
109 bool in;
110 struct libusb_transfer *xfer;
111 unsigned char *buffer;
112 unsigned char *cbuf;
113 unsigned int clen;
114 QTAILQ_ENTRY(USBHostRequest) next;
117 struct USBHostIsoXfer {
118 USBHostIsoRing *ring;
119 struct libusb_transfer *xfer;
120 bool copy_complete;
121 unsigned int packet;
122 QTAILQ_ENTRY(USBHostIsoXfer) next;
125 struct USBHostIsoRing {
126 USBHostDevice *host;
127 USBEndpoint *ep;
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,
203 ctx);
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;
214 int i, rc;
216 if (ctx) {
217 return 0;
219 rc = libusb_init(&ctx);
220 if (rc != 0) {
221 return -1;
223 libusb_set_debug(ctx, loglevel);
225 libusb_set_pollfd_notifiers(ctx, usb_host_add_fd,
226 usb_host_del_fd,
227 ctx);
228 poll = libusb_get_pollfds(ctx);
229 if (poll) {
230 for (i = 0; poll[i] != NULL; i++) {
231 usb_host_add_fd(poll[i]->fd, poll[i]->events, ctx);
234 free(poll);
235 return 0;
238 static int usb_host_get_port(libusb_device *dev, char *port, size_t len)
240 uint8_t path[7];
241 size_t off;
242 int rc, i;
244 #if LIBUSBX_API_VERSION >= 0x01000102
245 rc = libusb_get_port_numbers(dev, path, 7);
246 #else
247 rc = libusb_get_port_path(ctx, dev, path, 7);
248 #endif
249 if (rc < 0) {
250 return 0;
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]);
256 return off;
259 static void usb_host_libusb_error(const char *func, int rc)
261 const char *errname;
263 if (rc >= 0) {
264 return;
267 if (-rc < ARRAY_SIZE(err_names) && err_names[-rc]) {
268 errname = err_names[-rc];
269 } else {
270 errname = "?";
272 fprintf(stderr, "%s: %d [%s]\n", func, rc, errname);
275 /* ------------------------------------------------------------------------ */
277 static bool usb_host_use_combining(USBEndpoint *ep)
279 int type;
281 if (!ep->pipeline) {
282 return false;
284 if (ep->pid != USB_TOKEN_IN) {
285 return false;
287 type = usb_ep_get_type(ep->dev, ep->pid, ep->nr);
288 if (type != USB_ENDPOINT_XFER_BULK) {
289 return false;
291 return true;
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);
301 r->host = s;
302 r->p = p;
303 r->in = in;
304 r->xfer = libusb_alloc_transfer(0);
305 if (bufsize) {
306 r->buffer = g_malloc(bufsize);
308 QTAILQ_INSERT_TAIL(&s->requests, r, next);
309 return r;
312 static void usb_host_req_free(USBHostRequest *r)
314 if (r->host) {
315 QTAILQ_REMOVE(&r->host->requests, r, next);
317 libusb_free_transfer(r->xfer);
318 g_free(r->buffer);
319 g_free(r);
322 static USBHostRequest *usb_host_req_find(USBHostDevice *s, USBPacket *p)
324 USBHostRequest *r;
326 QTAILQ_FOREACH(r, &s->requests, next) {
327 if (r->p == p) {
328 return r;
331 return NULL;
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);
340 if (r->p == NULL) {
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);
353 out:
354 usb_host_req_free(r);
355 if (disconnect) {
356 usb_host_nodev(s);
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);
366 if (r->p == NULL) {
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);
378 } else {
379 usb_packet_complete(USB_DEVICE(s), r->p);
382 out:
383 usb_host_req_free(r);
384 if (disconnect) {
385 usb_host_nodev(s);
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);
394 if (inflight) {
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);
400 } else {
401 usb_packet_complete(USB_DEVICE(s), r->p);
403 r->p = NULL;
406 QTAILQ_REMOVE(&r->host->requests, r, next);
407 r->host = NULL;
409 if (inflight) {
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;
420 if (!xfer) {
421 /* USBHostIsoXfer released while inflight */
422 g_free(transfer->buffer);
423 libusb_free_transfer(transfer);
424 return;
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);
434 } else {
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;
445 int i;
447 ring->host = s;
448 ring->ep = ep;
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);
456 xfer->ring = ring;
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);
475 return ring;
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) {
484 return ring;
487 return NULL;
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);
494 xfer->packet = 0;
495 xfer->copy_complete = false;
498 static void usb_host_iso_free_xfer(USBHostIsoXfer *xfer, bool inflight)
500 if (inflight) {
501 xfer->xfer->user_data = NULL;
502 } else {
503 g_free(xfer->xfer->buffer);
504 libusb_free_transfer(xfer->xfer);
506 g_free(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);
527 g_free(ring);
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)
541 unsigned int psize;
542 unsigned char *buf;
544 buf = libusb_get_iso_packet_buffer_simple(xfer->xfer, xfer->packet);
545 if (p->pid == USB_TOKEN_OUT) {
546 psize = p->iov.size;
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;
552 } else {
553 psize = xfer->xfer->iso_packet_desc[xfer->packet].actual_length;
554 if (psize > p->iov.size) {
555 /* should not happen (guest bug) */
556 psize = p->iov.size;
559 usb_packet_copy(p, buf, psize);
560 xfer->packet++;
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;
570 int rc;
572 ring = usb_host_iso_find(s, p->ep);
573 if (ring == NULL) {
574 ring = usb_host_iso_alloc(s, p->ep);
577 /* copy data to guest */
578 xfer = QTAILQ_FIRST(&ring->copy);
579 if (xfer != NULL) {
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);
591 if (rc != 0) {
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) {
595 disconnect = true;
597 break;
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);
605 if (disconnect) {
606 usb_host_nodev(s);
610 static void usb_host_iso_data_out(USBHostDevice *s, USBPacket *p)
612 USBHostIsoRing *ring;
613 USBHostIsoXfer *xfer;
614 bool disconnect = false;
615 int rc, filled = 0;
617 ring = usb_host_iso_find(s, p->ep);
618 if (ring == NULL) {
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) {
625 filled++;
626 xfer = QTAILQ_NEXT(xfer, next);
628 if (xfer == NULL) {
629 xfer = QTAILQ_FIRST(&ring->unused);
630 if (xfer == NULL) {
631 trace_usb_host_iso_out_of_bufs(s->bus_num, s->addr, p->ep->nr);
632 return;
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) {
644 return;
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);
653 if (rc != 0) {
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) {
657 disconnect = true;
659 break;
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);
667 if (disconnect) {
668 usb_host_nodev(s);
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;
679 uint8_t type;
680 int rc, c, i, a, e;
682 for (c = 0;; c++) {
683 rc = libusb_get_config_descriptor(s->dev, c, &conf);
684 if (rc != 0) {
685 break;
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;
693 switch (type) {
694 case 0x01: /* ISO */
695 return false;
696 case 0x03: /* INTERRUPT */
697 if (endp->wMaxPacketSize > 64) {
698 return false;
700 break;
705 libusb_free_config_descriptor(conf);
707 return true;
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;
722 uint8_t devep, type;
723 int pid, ep;
724 int rc, i, e;
726 usb_ep_reset(udev);
727 rc = libusb_get_active_config_descriptor(s->dev, &conf);
728 if (rc != 0) {
729 return;
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;
745 ep = devep & 0xf;
746 type = endp->bmAttributes & 0x3;
748 if (ep == 0) {
749 trace_usb_host_parse_error(s->bus_num, s->addr,
750 "invalid endpoint address");
751 return;
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");
756 return;
759 trace_usb_host_parse_endpoint(s->bus_num, s->addr, ep,
760 (devep & USB_DIR_IN) ? "in" : "out",
761 tname[type], true);
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);
778 int rc;
780 trace_usb_host_open_started(bus_num, addr);
782 if (s->dh != NULL) {
783 goto fail;
785 rc = libusb_open(dev, &s->dh);
786 if (rc != 0) {
787 goto fail;
790 libusb_get_device_descriptor(dev, &s->ddesc);
791 s->dev = dev;
792 s->bus_num = bus_num;
793 s->addr = addr;
794 usb_host_get_port(s->dev, s->port, sizeof(s->port));
796 usb_ep_init(udev);
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));
809 } else {
810 snprintf(udev->product_desc, sizeof(udev->product_desc),
811 "host:%d.%d", bus_num, addr);
814 rc = usb_device_attach(udev);
815 if (rc) {
816 goto fail;
819 trace_usb_host_open_success(bus_num, addr);
820 return 0;
822 fail:
823 trace_usb_host_open_failure(bus_num, addr);
824 if (s->dh != NULL) {
825 libusb_close(s->dh);
826 s->dh = NULL;
827 s->dev = NULL;
829 return -1;
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);
845 if (s->dh == NULL) {
846 return -1;
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);
861 libusb_close(s->dh);
862 s->dh = NULL;
863 s->dev = NULL;
865 usb_host_auto_check(NULL);
866 return 0;
869 static void usb_host_nodev_bh(void *opaque)
871 USBHostDevice *s = opaque;
872 usb_host_close(s);
875 static void usb_host_nodev(USBHostDevice *s)
877 if (!s->bh_nodev) {
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);
887 if (s->dh) {
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);
909 return 0;
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);
918 usb_host_close(s);
921 static void usb_host_cancel_packet(USBDevice *udev, USBPacket *p)
923 USBHostDevice *s = USB_HOST_DEVICE(udev);
924 USBHostRequest *r;
926 if (p->combined) {
927 usb_combined_packet_cancel(udev, p);
928 return;
931 trace_usb_host_req_canceled(s->bus_num, s->addr, p);
933 r = usb_host_req_find(s, p);
934 if (r && r->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;
943 int rc, i;
945 rc = libusb_get_active_config_descriptor(s->dev, &conf);
946 if (rc != 0) {
947 return;
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);
952 if (rc != 1) {
953 continue;
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;
966 int rc, i;
968 rc = libusb_get_active_config_descriptor(s->dev, &conf);
969 if (rc != 0) {
970 return;
972 for (i = 0; i < conf->bNumInterfaces; i++) {
973 if (!s->ifs[i].detached) {
974 continue;
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;
987 int rc, i;
989 for (i = 0; i < USB_MAX_INTERFACES; i++) {
990 udev->altsetting[i] = 0;
992 udev->ninterfaces = 0;
993 udev->configuration = 0;
995 usb_host_detach_kernel(s);
997 rc = libusb_get_active_config_descriptor(s->dev, &conf);
998 if (rc != 0) {
999 if (rc == LIBUSB_ERROR_NOT_FOUND) {
1000 /* address state - ignore */
1001 return USB_RET_SUCCESS;
1003 return USB_RET_STALL;
1006 for (i = 0; i < conf->bNumInterfaces; i++) {
1007 trace_usb_host_claim_interface(s->bus_num, s->addr, configuration, i);
1008 rc = libusb_claim_interface(s->dh, i);
1009 usb_host_libusb_error("libusb_claim_interface", rc);
1010 if (rc != 0) {
1011 return USB_RET_STALL;
1013 s->ifs[i].claimed = true;
1016 udev->ninterfaces = conf->bNumInterfaces;
1017 udev->configuration = configuration;
1019 libusb_free_config_descriptor(conf);
1020 return USB_RET_SUCCESS;
1023 static void usb_host_release_interfaces(USBHostDevice *s)
1025 USBDevice *udev = USB_DEVICE(s);
1026 int i, rc;
1028 for (i = 0; i < udev->ninterfaces; i++) {
1029 if (!s->ifs[i].claimed) {
1030 continue;
1032 trace_usb_host_release_interface(s->bus_num, s->addr, i);
1033 rc = libusb_release_interface(s->dh, i);
1034 usb_host_libusb_error("libusb_release_interface", rc);
1035 s->ifs[i].claimed = false;
1039 static void usb_host_set_address(USBHostDevice *s, int addr)
1041 USBDevice *udev = USB_DEVICE(s);
1043 trace_usb_host_set_address(s->bus_num, s->addr, addr);
1044 udev->addr = addr;
1047 static void usb_host_set_config(USBHostDevice *s, int config, USBPacket *p)
1049 int rc;
1051 trace_usb_host_set_config(s->bus_num, s->addr, config);
1053 usb_host_release_interfaces(s);
1054 usb_host_detach_kernel(s);
1055 rc = libusb_set_configuration(s->dh, config);
1056 if (rc != 0) {
1057 usb_host_libusb_error("libusb_set_configuration", rc);
1058 p->status = USB_RET_STALL;
1059 if (rc == LIBUSB_ERROR_NO_DEVICE) {
1060 usb_host_nodev(s);
1062 return;
1064 p->status = usb_host_claim_interfaces(s, config);
1065 if (p->status != USB_RET_SUCCESS) {
1066 return;
1068 usb_host_ep_update(s);
1071 static void usb_host_set_interface(USBHostDevice *s, int iface, int alt,
1072 USBPacket *p)
1074 USBDevice *udev = USB_DEVICE(s);
1075 int rc;
1077 trace_usb_host_set_interface(s->bus_num, s->addr, iface, alt);
1079 usb_host_iso_free_all(s);
1081 if (iface >= USB_MAX_INTERFACES) {
1082 p->status = USB_RET_STALL;
1083 return;
1086 rc = libusb_set_interface_alt_setting(s->dh, iface, alt);
1087 if (rc != 0) {
1088 usb_host_libusb_error("libusb_set_interface_alt_setting", rc);
1089 p->status = USB_RET_STALL;
1090 if (rc == LIBUSB_ERROR_NO_DEVICE) {
1091 usb_host_nodev(s);
1093 return;
1096 udev->altsetting[iface] = alt;
1097 usb_host_ep_update(s);
1100 static void usb_host_handle_control(USBDevice *udev, USBPacket *p,
1101 int request, int value, int index,
1102 int length, uint8_t *data)
1104 USBHostDevice *s = USB_HOST_DEVICE(udev);
1105 USBHostRequest *r;
1106 int rc;
1108 trace_usb_host_req_control(s->bus_num, s->addr, p, request, value, index);
1110 if (s->dh == NULL) {
1111 p->status = USB_RET_NODEV;
1112 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1113 return;
1116 switch (request) {
1117 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
1118 usb_host_set_address(s, value);
1119 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1120 return;
1122 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
1123 usb_host_set_config(s, value & 0xff, p);
1124 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1125 return;
1127 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
1128 usb_host_set_interface(s, index, value, p);
1129 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1130 return;
1132 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
1133 if (value == 0) { /* clear halt */
1134 int pid = (index & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT;
1135 libusb_clear_halt(s->dh, index);
1136 usb_ep_set_halted(udev, pid, index & 0x0f, 0);
1137 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1138 return;
1142 r = usb_host_req_alloc(s, p, (request >> 8) & USB_DIR_IN, length + 8);
1143 r->cbuf = data;
1144 r->clen = length;
1145 memcpy(r->buffer, udev->setup_buf, 8);
1146 if (!r->in) {
1147 memcpy(r->buffer + 8, r->cbuf, r->clen);
1150 libusb_fill_control_transfer(r->xfer, s->dh, r->buffer,
1151 usb_host_req_complete_ctrl, r,
1152 CONTROL_TIMEOUT);
1153 rc = libusb_submit_transfer(r->xfer);
1154 if (rc != 0) {
1155 p->status = USB_RET_NODEV;
1156 trace_usb_host_req_complete(s->bus_num, s->addr, p,
1157 p->status, p->actual_length);
1158 if (rc == LIBUSB_ERROR_NO_DEVICE) {
1159 usb_host_nodev(s);
1161 return;
1164 p->status = USB_RET_ASYNC;
1167 static void usb_host_handle_data(USBDevice *udev, USBPacket *p)
1169 USBHostDevice *s = USB_HOST_DEVICE(udev);
1170 USBHostRequest *r;
1171 size_t size;
1172 int ep, rc;
1174 if (usb_host_use_combining(p->ep) && p->state == USB_PACKET_SETUP) {
1175 p->status = USB_RET_ADD_TO_QUEUE;
1176 return;
1179 trace_usb_host_req_data(s->bus_num, s->addr, p,
1180 p->pid == USB_TOKEN_IN,
1181 p->ep->nr, p->iov.size);
1183 if (s->dh == NULL) {
1184 p->status = USB_RET_NODEV;
1185 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1186 return;
1188 if (p->ep->halted) {
1189 p->status = USB_RET_STALL;
1190 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1191 return;
1194 switch (usb_ep_get_type(udev, p->pid, p->ep->nr)) {
1195 case USB_ENDPOINT_XFER_BULK:
1196 size = usb_packet_size(p);
1197 r = usb_host_req_alloc(s, p, p->pid == USB_TOKEN_IN, size);
1198 if (!r->in) {
1199 usb_packet_copy(p, r->buffer, size);
1201 ep = p->ep->nr | (r->in ? USB_DIR_IN : 0);
1202 libusb_fill_bulk_transfer(r->xfer, s->dh, ep,
1203 r->buffer, size,
1204 usb_host_req_complete_data, r,
1205 BULK_TIMEOUT);
1206 break;
1207 case USB_ENDPOINT_XFER_INT:
1208 r = usb_host_req_alloc(s, p, p->pid == USB_TOKEN_IN, p->iov.size);
1209 if (!r->in) {
1210 usb_packet_copy(p, r->buffer, p->iov.size);
1212 ep = p->ep->nr | (r->in ? USB_DIR_IN : 0);
1213 libusb_fill_interrupt_transfer(r->xfer, s->dh, ep,
1214 r->buffer, p->iov.size,
1215 usb_host_req_complete_data, r,
1216 INTR_TIMEOUT);
1217 break;
1218 case USB_ENDPOINT_XFER_ISOC:
1219 if (p->pid == USB_TOKEN_IN) {
1220 usb_host_iso_data_in(s, p);
1221 } else {
1222 usb_host_iso_data_out(s, p);
1224 trace_usb_host_req_complete(s->bus_num, s->addr, p,
1225 p->status, p->actual_length);
1226 return;
1227 default:
1228 p->status = USB_RET_STALL;
1229 trace_usb_host_req_complete(s->bus_num, s->addr, p,
1230 p->status, p->actual_length);
1231 return;
1234 rc = libusb_submit_transfer(r->xfer);
1235 if (rc != 0) {
1236 p->status = USB_RET_NODEV;
1237 trace_usb_host_req_complete(s->bus_num, s->addr, p,
1238 p->status, p->actual_length);
1239 if (rc == LIBUSB_ERROR_NO_DEVICE) {
1240 usb_host_nodev(s);
1242 return;
1245 p->status = USB_RET_ASYNC;
1248 static void usb_host_flush_ep_queue(USBDevice *dev, USBEndpoint *ep)
1250 if (usb_host_use_combining(ep)) {
1251 usb_ep_combine_input_packets(ep);
1255 static void usb_host_handle_reset(USBDevice *udev)
1257 USBHostDevice *s = USB_HOST_DEVICE(udev);
1258 int rc;
1260 trace_usb_host_reset(s->bus_num, s->addr);
1262 rc = libusb_reset_device(s->dh);
1263 if (rc != 0) {
1264 usb_host_nodev(s);
1269 * This is *NOT* about restoring state. We have absolutely no idea
1270 * what state the host device is in at the moment and whenever it is
1271 * still present in the first place. Attemping to contine where we
1272 * left off is impossible.
1274 * What we are going to to to here is emulate a surprise removal of
1275 * the usb device passed through, then kick host scan so the device
1276 * will get re-attached (and re-initialized by the guest) in case it
1277 * is still present.
1279 * As the device removal will change the state of other devices (usb
1280 * host controller, most likely interrupt controller too) we have to
1281 * wait with it until *all* vmstate is loaded. Thus post_load just
1282 * kicks a bottom half which then does the actual work.
1284 static void usb_host_post_load_bh(void *opaque)
1286 USBHostDevice *dev = opaque;
1287 USBDevice *udev = USB_DEVICE(dev);
1289 if (dev->dh != NULL) {
1290 usb_host_close(dev);
1292 if (udev->attached) {
1293 usb_device_detach(udev);
1295 usb_host_auto_check(NULL);
1298 static int usb_host_post_load(void *opaque, int version_id)
1300 USBHostDevice *dev = opaque;
1302 if (!dev->bh_postld) {
1303 dev->bh_postld = qemu_bh_new(usb_host_post_load_bh, dev);
1305 qemu_bh_schedule(dev->bh_postld);
1306 return 0;
1309 static const VMStateDescription vmstate_usb_host = {
1310 .name = "usb-host",
1311 .version_id = 1,
1312 .minimum_version_id = 1,
1313 .post_load = usb_host_post_load,
1314 .fields = (VMStateField[]) {
1315 VMSTATE_USB_DEVICE(parent_obj, USBHostDevice),
1316 VMSTATE_END_OF_LIST()
1320 static Property usb_host_dev_properties[] = {
1321 DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0),
1322 DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0),
1323 DEFINE_PROP_STRING("hostport", USBHostDevice, match.port),
1324 DEFINE_PROP_HEX32("vendorid", USBHostDevice, match.vendor_id, 0),
1325 DEFINE_PROP_HEX32("productid", USBHostDevice, match.product_id, 0),
1326 DEFINE_PROP_UINT32("isobufs", USBHostDevice, iso_urb_count, 4),
1327 DEFINE_PROP_UINT32("isobsize", USBHostDevice, iso_urb_frames, 32),
1328 DEFINE_PROP_INT32("bootindex", USBHostDevice, bootindex, -1),
1329 DEFINE_PROP_UINT32("loglevel", USBHostDevice, loglevel,
1330 LIBUSB_LOG_LEVEL_WARNING),
1331 DEFINE_PROP_BIT("pipeline", USBHostDevice, options,
1332 USB_HOST_OPT_PIPELINE, true),
1333 DEFINE_PROP_END_OF_LIST(),
1336 static void usb_host_class_initfn(ObjectClass *klass, void *data)
1338 DeviceClass *dc = DEVICE_CLASS(klass);
1339 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
1341 uc->init = usb_host_initfn;
1342 uc->product_desc = "USB Host Device";
1343 uc->cancel_packet = usb_host_cancel_packet;
1344 uc->handle_data = usb_host_handle_data;
1345 uc->handle_control = usb_host_handle_control;
1346 uc->handle_reset = usb_host_handle_reset;
1347 uc->handle_destroy = usb_host_handle_destroy;
1348 uc->flush_ep_queue = usb_host_flush_ep_queue;
1349 dc->vmsd = &vmstate_usb_host;
1350 dc->props = usb_host_dev_properties;
1351 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
1354 static TypeInfo usb_host_dev_info = {
1355 .name = TYPE_USB_HOST_DEVICE,
1356 .parent = TYPE_USB_DEVICE,
1357 .instance_size = sizeof(USBHostDevice),
1358 .class_init = usb_host_class_initfn,
1361 static void usb_host_register_types(void)
1363 type_register_static(&usb_host_dev_info);
1366 type_init(usb_host_register_types)
1368 /* ------------------------------------------------------------------------ */
1370 static QEMUTimer *usb_auto_timer;
1371 static VMChangeStateEntry *usb_vmstate;
1373 static void usb_host_vm_state(void *unused, int running, RunState state)
1375 if (running) {
1376 usb_host_auto_check(unused);
1380 static void usb_host_auto_check(void *unused)
1382 struct USBHostDevice *s;
1383 struct USBAutoFilter *f;
1384 libusb_device **devs;
1385 struct libusb_device_descriptor ddesc;
1386 int unconnected = 0;
1387 int i, n;
1389 if (usb_host_init() != 0) {
1390 return;
1393 if (runstate_is_running()) {
1394 n = libusb_get_device_list(ctx, &devs);
1395 for (i = 0; i < n; i++) {
1396 if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) {
1397 continue;
1399 if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) {
1400 continue;
1402 QTAILQ_FOREACH(s, &hostdevs, next) {
1403 f = &s->match;
1404 if (f->bus_num > 0 &&
1405 f->bus_num != libusb_get_bus_number(devs[i])) {
1406 continue;
1408 if (f->addr > 0 &&
1409 f->addr != libusb_get_device_address(devs[i])) {
1410 continue;
1412 if (f->port != NULL) {
1413 char port[16] = "-";
1414 usb_host_get_port(devs[i], port, sizeof(port));
1415 if (strcmp(f->port, port) != 0) {
1416 continue;
1419 if (f->vendor_id > 0 &&
1420 f->vendor_id != ddesc.idVendor) {
1421 continue;
1423 if (f->product_id > 0 &&
1424 f->product_id != ddesc.idProduct) {
1425 continue;
1428 /* We got a match */
1429 s->seen++;
1430 if (s->errcount >= 3) {
1431 continue;
1433 if (s->dh != NULL) {
1434 continue;
1436 if (usb_host_open(s, devs[i]) < 0) {
1437 s->errcount++;
1438 continue;
1440 break;
1443 libusb_free_device_list(devs, 1);
1445 QTAILQ_FOREACH(s, &hostdevs, next) {
1446 if (s->dh == NULL) {
1447 unconnected++;
1449 if (s->seen == 0) {
1450 if (s->dh) {
1451 usb_host_close(s);
1453 s->errcount = 0;
1455 s->seen = 0;
1458 #if 0
1459 if (unconnected == 0) {
1460 /* nothing to watch */
1461 if (usb_auto_timer) {
1462 timer_del(usb_auto_timer);
1463 trace_usb_host_auto_scan_disabled();
1465 return;
1467 #endif
1470 if (!usb_vmstate) {
1471 usb_vmstate = qemu_add_vm_change_state_handler(usb_host_vm_state, NULL);
1473 if (!usb_auto_timer) {
1474 usb_auto_timer = timer_new_ms(QEMU_CLOCK_REALTIME, usb_host_auto_check, NULL);
1475 if (!usb_auto_timer) {
1476 return;
1478 trace_usb_host_auto_scan_enabled();
1480 timer_mod(usb_auto_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 2000);
1483 void usb_host_info(Monitor *mon, const QDict *qdict)
1485 libusb_device **devs;
1486 struct libusb_device_descriptor ddesc;
1487 char port[16];
1488 int i, n;
1490 if (usb_host_init() != 0) {
1491 return;
1494 n = libusb_get_device_list(ctx, &devs);
1495 for (i = 0; i < n; i++) {
1496 if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) {
1497 continue;
1499 if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) {
1500 continue;
1502 usb_host_get_port(devs[i], port, sizeof(port));
1503 monitor_printf(mon, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1504 libusb_get_bus_number(devs[i]),
1505 libusb_get_device_address(devs[i]),
1506 port,
1507 speed_name[libusb_get_device_speed(devs[i])]);
1508 monitor_printf(mon, " Class %02x:", ddesc.bDeviceClass);
1509 monitor_printf(mon, " USB device %04x:%04x",
1510 ddesc.idVendor, ddesc.idProduct);
1511 if (ddesc.iProduct) {
1512 libusb_device_handle *handle;
1513 if (libusb_open(devs[i], &handle) == 0) {
1514 unsigned char name[64] = "";
1515 libusb_get_string_descriptor_ascii(handle,
1516 ddesc.iProduct,
1517 name, sizeof(name));
1518 libusb_close(handle);
1519 monitor_printf(mon, ", %s", name);
1522 monitor_printf(mon, "\n");
1524 libusb_free_device_list(devs, 1);