]> git.proxmox.com Git - mirror_qemu.git/blame - hw/usb/host-libusb.c
Merge remote-tracking branch 'qemu-kvm/uq/master' into staging
[mirror_qemu.git] / hw / usb / host-libusb.c
CommitLineData
2b2325ff
GH
1/*
2 * Linux host USB redirector
3 *
4 * Copyright (c) 2005 Fabrice Bellard
5 *
6 * Copyright (c) 2008 Max Krasnyansky
7 * Support for host device auto connect & disconnect
8 * Major rewrite to support fully async operation
9 *
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
13 *
14 * (c) 2012 Gerd Hoffmann <kraxel@redhat.com>
15 * Completely rewritten to use libusb instead of usbfs ioctls.
16 *
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:
23 *
24 * The above copyright notice and this permission notice shall be included in
25 * all copies or substantial portions of the Software.
26 *
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.
34 */
35
36#include <poll.h>
37#include <libusb.h>
38
39#include "qemu-common.h"
40#include "monitor/monitor.h"
41#include "sysemu/sysemu.h"
42#include "trace.h"
43
44#include "hw/usb.h"
45
46/* ------------------------------------------------------------------------ */
47
48#define TYPE_USB_HOST_DEVICE "usb-host"
49#define USB_HOST_DEVICE(obj) \
50 OBJECT_CHECK(USBHostDevice, (obj), TYPE_USB_HOST_DEVICE)
51
52typedef struct USBHostDevice USBHostDevice;
53typedef struct USBHostRequest USBHostRequest;
54typedef struct USBHostIsoXfer USBHostIsoXfer;
55typedef struct USBHostIsoRing USBHostIsoRing;
56
57struct USBAutoFilter {
58 uint32_t bus_num;
59 uint32_t addr;
60 char *port;
61 uint32_t vendor_id;
62 uint32_t product_id;
63};
64
65enum USBHostDeviceOptions {
66 USB_HOST_OPT_PIPELINE,
67};
68
69struct USBHostDevice {
70 USBDevice parent_obj;
71
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;
79
80 /* state */
81 QTAILQ_ENTRY(USBHostDevice) next;
82 int seen, errcount;
83 int bus_num;
84 int addr;
85 char port[16];
86
87 libusb_device *dev;
88 libusb_device_handle *dh;
89 struct libusb_device_descriptor ddesc;
90
91 struct {
92 bool detached;
93 bool claimed;
94 } ifs[USB_MAX_INTERFACES];
95
96 /* callbacks & friends */
97 QEMUBH *bh;
98 Notifier exit;
99
100 /* request queues */
101 QTAILQ_HEAD(, USBHostRequest) requests;
102 QTAILQ_HEAD(, USBHostIsoRing) isorings;
103};
104
105struct 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;
114};
115
116struct USBHostIsoXfer {
117 USBHostIsoRing *ring;
118 struct libusb_transfer *xfer;
119 bool copy_complete;
120 unsigned int packet;
121 QTAILQ_ENTRY(USBHostIsoXfer) next;
122};
123
124struct 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;
131};
132
133static QTAILQ_HEAD(, USBHostDevice) hostdevs =
134 QTAILQ_HEAD_INITIALIZER(hostdevs);
135
136static void usb_host_auto_check(void *unused);
137static void usb_host_release_interfaces(USBHostDevice *s);
138static void usb_host_nodev(USBHostDevice *s);
139static void usb_host_attach_kernel(USBHostDevice *s);
140
141/* ------------------------------------------------------------------------ */
142
143#define CONTROL_TIMEOUT 10000 /* 10 sec */
144#define BULK_TIMEOUT 0 /* unlimited */
145#define INTR_TIMEOUT 0 /* unlimited */
146
147static 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",
153};
154
155static 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,
160};
161
162static 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,
170};
171
172static 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",
186};
187
188static libusb_context *ctx;
189static uint32_t loglevel;
190
191static void usb_host_handle_fd(void *opaque)
192{
193 struct timeval tv = { 0, 0 };
194 libusb_handle_events_timeout(ctx, &tv);
195}
196
197static void usb_host_add_fd(int fd, short events, void *user_data)
198{
199 qemu_set_fd_handler(fd,
200 (events & POLLIN) ? usb_host_handle_fd : NULL,
201 (events & POLLOUT) ? usb_host_handle_fd : NULL,
202 ctx);
203}
204
205static void usb_host_del_fd(int fd, void *user_data)
206{
207 qemu_set_fd_handler(fd, NULL, NULL, NULL);
208}
209
210static int usb_host_init(void)
211{
212 const struct libusb_pollfd **poll;
213 int i, rc;
214
215 if (ctx) {
216 return 0;
217 }
218 rc = libusb_init(&ctx);
219 if (rc != 0) {
220 return -1;
221 }
222 libusb_set_debug(ctx, loglevel);
223
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);
231 }
232 }
233 free(poll);
234 return 0;
235}
236
237static int usb_host_get_port(libusb_device *dev, char *port, size_t len)
238{
2b2325ff
GH
239 uint8_t path[7];
240 size_t off;
241 int rc, i;
242
243 rc = libusb_get_port_path(ctx, dev, path, 7);
244 if (rc < 0) {
245 return 0;
246 }
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]);
250 }
251 return off;
2b2325ff
GH
252}
253
254static void usb_host_libusb_error(const char *func, int rc)
255{
256 const char *errname;
257
258 if (rc >= 0) {
259 return;
260 }
261
262 if (-rc < ARRAY_SIZE(err_names) && err_names[-rc]) {
263 errname = err_names[-rc];
264 } else {
265 errname = "?";
266 }
267 fprintf(stderr, "%s: %d [%s]\n", func, rc, errname);
268}
269
270/* ------------------------------------------------------------------------ */
271
272static bool usb_host_use_combining(USBEndpoint *ep)
273{
274 int type;
275
276 if (!ep->pipeline) {
277 return false;
278 }
279 if (ep->pid != USB_TOKEN_IN) {
280 return false;
281 }
282 type = usb_ep_get_type(ep->dev, ep->pid, ep->nr);
283 if (type != USB_ENDPOINT_XFER_BULK) {
284 return false;
285 }
286 return true;
287}
288
289/* ------------------------------------------------------------------------ */
290
291static USBHostRequest *usb_host_req_alloc(USBHostDevice *s, USBPacket *p,
292 bool in, size_t bufsize)
293{
294 USBHostRequest *r = g_new0(USBHostRequest, 1);
295
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);
302 }
303 QTAILQ_INSERT_TAIL(&s->requests, r, next);
304 return r;
305}
306
307static void usb_host_req_free(USBHostRequest *r)
308{
309 if (r->host) {
310 QTAILQ_REMOVE(&r->host->requests, r, next);
311 }
312 libusb_free_transfer(r->xfer);
313 g_free(r->buffer);
314 g_free(r);
315}
316
317static USBHostRequest *usb_host_req_find(USBHostDevice *s, USBPacket *p)
318{
319 USBHostRequest *r;
320
321 QTAILQ_FOREACH(r, &s->requests, next) {
322 if (r->p == p) {
323 return r;
324 }
325 }
326 return NULL;
327}
328
329static void usb_host_req_complete_ctrl(struct libusb_transfer *xfer)
330{
331 USBHostRequest *r = xfer->user_data;
332 USBHostDevice *s = r->host;
333 bool disconnect = (xfer->status == LIBUSB_TRANSFER_NO_DEVICE);
334
335 if (r->p == NULL) {
336 goto out; /* request was canceled */
337 }
338
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);
343 }
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);
347
348out:
349 usb_host_req_free(r);
350 if (disconnect) {
351 usb_host_nodev(s);
352 }
353}
354
355static void usb_host_req_complete_data(struct libusb_transfer *xfer)
356{
357 USBHostRequest *r = xfer->user_data;
358 USBHostDevice *s = r->host;
359 bool disconnect = (xfer->status == LIBUSB_TRANSFER_NO_DEVICE);
360
361 if (r->p == NULL) {
362 goto out; /* request was canceled */
363 }
364
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);
368 }
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);
375 }
376
377out:
378 usb_host_req_free(r);
379 if (disconnect) {
380 usb_host_nodev(s);
381 }
382}
383
384static void usb_host_req_abort(USBHostRequest *r)
385{
386 USBHostDevice *s = r->host;
387 bool inflight = (r->p && r->p->state == USB_RET_ASYNC);
388
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);
397 }
398 r->p = NULL;
399 }
400
401 QTAILQ_REMOVE(&r->host->requests, r, next);
402 r->host = NULL;
403
404 if (inflight) {
405 libusb_cancel_transfer(r->xfer);
406 }
407}
408
409/* ------------------------------------------------------------------------ */
410
411static void usb_host_req_complete_iso(struct libusb_transfer *transfer)
412{
413 USBHostIsoXfer *xfer = transfer->user_data;
414
415 if (!xfer) {
416 /* USBHostIsoXfer released while inflight */
417 g_free(transfer->buffer);
418 libusb_free_transfer(transfer);
419 return;
420 }
421
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);
426 }
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);
431 }
432}
433
434static USBHostIsoRing *usb_host_iso_alloc(USBHostDevice *s, USBEndpoint *ep)
435{
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;
441
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);
448
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;
455
456 xfer->xfer->endpoint = ring->ep->nr;
457 if (ring->ep->pid == USB_TOKEN_IN) {
458 xfer->xfer->endpoint |= USB_DIR_IN;
459 }
460 xfer->xfer->callback = usb_host_req_complete_iso;
461 xfer->xfer->user_data = xfer;
462
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);
466
467 QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
468 }
469
470 return ring;
471}
472
473static USBHostIsoRing *usb_host_iso_find(USBHostDevice *s, USBEndpoint *ep)
474{
475 USBHostIsoRing *ring;
476
477 QTAILQ_FOREACH(ring, &s->isorings, next) {
478 if (ring->ep == ep) {
479 return ring;
480 }
481 }
482 return NULL;
483}
484
485static void usb_host_iso_reset_xfer(USBHostIsoXfer *xfer)
486{
487 libusb_set_iso_packet_lengths(xfer->xfer,
488 xfer->ring->ep->max_packet_size);
489 xfer->packet = 0;
490 xfer->copy_complete = false;
491}
492
493static void usb_host_iso_free_xfer(USBHostIsoXfer *xfer, bool inflight)
494{
495 if (inflight) {
496 xfer->xfer->user_data = NULL;
497 } else {
498 g_free(xfer->xfer->buffer);
499 libusb_free_transfer(xfer->xfer);
500 }
501 g_free(xfer);
502}
503
504static void usb_host_iso_free(USBHostIsoRing *ring)
505{
506 USBHostIsoXfer *xfer;
507
508 while ((xfer = QTAILQ_FIRST(&ring->inflight)) != NULL) {
509 QTAILQ_REMOVE(&ring->inflight, xfer, next);
510 usb_host_iso_free_xfer(xfer, true);
511 }
512 while ((xfer = QTAILQ_FIRST(&ring->unused)) != NULL) {
513 QTAILQ_REMOVE(&ring->unused, xfer, next);
514 usb_host_iso_free_xfer(xfer, false);
515 }
516 while ((xfer = QTAILQ_FIRST(&ring->copy)) != NULL) {
517 QTAILQ_REMOVE(&ring->copy, xfer, next);
518 usb_host_iso_free_xfer(xfer, false);
519 }
520
521 QTAILQ_REMOVE(&ring->host->isorings, ring, next);
522 g_free(ring);
523}
524
525static void usb_host_iso_free_all(USBHostDevice *s)
526{
527 USBHostIsoRing *ring;
528
529 while ((ring = QTAILQ_FIRST(&s->isorings)) != NULL) {
530 usb_host_iso_free(ring);
531 }
532}
533
534static bool usb_host_iso_data_copy(USBHostIsoXfer *xfer, USBPacket *p)
535{
536 unsigned int psize;
537 unsigned char *buf;
538
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;
545 }
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;
552 }
553 }
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;
558}
559
560static void usb_host_iso_data_in(USBHostDevice *s, USBPacket *p)
561{
562 USBHostIsoRing *ring;
563 USBHostIsoXfer *xfer;
564 bool disconnect = false;
565 int rc;
566
567 ring = usb_host_iso_find(s, p->ep);
568 if (ring == NULL) {
569 ring = usb_host_iso_alloc(s, p->ep);
570 }
571
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);
578 }
579 }
580
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;
591 }
592 break;
593 }
594 if (QTAILQ_EMPTY(&ring->inflight)) {
595 trace_usb_host_iso_start(s->bus_num, s->addr, p->ep->nr);
596 }
597 QTAILQ_INSERT_TAIL(&ring->inflight, xfer, next);
598 }
599
600 if (disconnect) {
601 usb_host_nodev(s);
602 }
603}
604
605static void usb_host_iso_data_out(USBHostDevice *s, USBPacket *p)
606{
607 USBHostIsoRing *ring;
608 USBHostIsoXfer *xfer;
609 bool disconnect = false;
610 int rc, filled = 0;
611
612 ring = usb_host_iso_find(s, p->ep);
613 if (ring == NULL) {
614 ring = usb_host_iso_alloc(s, p->ep);
615 }
616
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);
622 }
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;
628 }
629 QTAILQ_REMOVE(&ring->unused, xfer, next);
630 usb_host_iso_reset_xfer(xfer);
631 QTAILQ_INSERT_TAIL(&ring->copy, xfer, next);
632 }
633 usb_host_iso_data_copy(xfer, p);
634
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;
640 }
641 }
642
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;
653 }
654 break;
655 }
656 if (QTAILQ_EMPTY(&ring->inflight)) {
657 trace_usb_host_iso_start(s->bus_num, s->addr, p->ep->nr);
658 }
659 QTAILQ_INSERT_TAIL(&ring->inflight, xfer, next);
660 }
661
662 if (disconnect) {
663 usb_host_nodev(s);
664 }
665}
666
667/* ------------------------------------------------------------------------ */
668
669static void usb_host_ep_update(USBHostDevice *s)
670{
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",
676 };
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;
684
685 usb_ep_reset(udev);
686 rc = libusb_get_active_config_descriptor(s->dev, &conf);
687 if (rc != 0) {
688 return;
689 }
690 trace_usb_host_parse_config(s->bus_num, s->addr,
691 conf->bConfigurationValue, true);
692
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];
701
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;
706
707 if (ep == 0) {
708 trace_usb_host_parse_error(s->bus_num, s->addr,
709 "invalid endpoint address");
710 return;
711 }
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;
716 }
717
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);
726 }
727 }
728
729 libusb_free_config_descriptor(conf);
730}
731
732static int usb_host_open(USBHostDevice *s, libusb_device *dev)
733{
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;
738
739 trace_usb_host_open_started(bus_num, addr);
740
741 if (s->dh != NULL) {
742 goto fail;
743 }
744 rc = libusb_open(dev, &s->dh);
745 if (rc != 0) {
746 goto fail;
747 }
748
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));
754
755 usb_ep_init(udev);
756 usb_host_ep_update(s);
757
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;
763 }
764#endif
765
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);
773 }
774
775 rc = usb_device_attach(udev);
776 if (rc) {
777 goto fail;
778 }
779
780 trace_usb_host_open_success(bus_num, addr);
781 return 0;
782
783fail:
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;
789 }
790 return -1;
791}
792
793static void usb_host_abort_xfers(USBHostDevice *s)
794{
795 USBHostRequest *r, *rtmp;
796
797 QTAILQ_FOREACH_SAFE(r, &s->requests, next, rtmp) {
798 usb_host_req_abort(r);
799 }
800}
801
802static int usb_host_close(USBHostDevice *s)
803{
804 USBDevice *udev = USB_DEVICE(s);
805
806 if (s->dh == NULL) {
807 return -1;
808 }
809
810 trace_usb_host_close(s->bus_num, s->addr);
811
812 usb_host_abort_xfers(s);
813 usb_host_iso_free_all(s);
814
815 if (udev->attached) {
816 usb_device_detach(udev);
817 }
818
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;
825
826 usb_host_auto_check(NULL);
827 return 0;
828}
829
830static void usb_host_nodev_bh(void *opaque)
831{
832 USBHostDevice *s = opaque;
833 usb_host_close(s);
834}
835
836static void usb_host_nodev(USBHostDevice *s)
837{
838 if (!s->bh) {
839 s->bh = qemu_bh_new(usb_host_nodev_bh, s);
840 }
841 qemu_bh_schedule(s->bh);
842}
843
844static void usb_host_exit_notifier(struct Notifier *n, void *data)
845{
846 USBHostDevice *s = container_of(n, USBHostDevice, exit);
847
848 if (s->dh) {
849 usb_host_release_interfaces(s);
850 usb_host_attach_kernel(s);
851 }
852}
853
854static int usb_host_initfn(USBDevice *udev)
855{
856 USBHostDevice *s = USB_HOST_DEVICE(udev);
857
858 loglevel = s->loglevel;
859 udev->auto_attach = 0;
860 QTAILQ_INIT(&s->requests);
861 QTAILQ_INIT(&s->isorings);
862
863 s->exit.notify = usb_host_exit_notifier;
864 qemu_add_exit_notifier(&s->exit);
865
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;
870}
871
872static void usb_host_handle_destroy(USBDevice *udev)
873{
874 USBHostDevice *s = USB_HOST_DEVICE(udev);
875
876 qemu_remove_exit_notifier(&s->exit);
877 QTAILQ_REMOVE(&hostdevs, s, next);
878 usb_host_close(s);
879}
880
881static void usb_host_cancel_packet(USBDevice *udev, USBPacket *p)
882{
883 USBHostDevice *s = USB_HOST_DEVICE(udev);
884 USBHostRequest *r;
885
886 if (p->combined) {
887 usb_combined_packet_cancel(udev, p);
888 return;
889 }
890
891 trace_usb_host_req_canceled(s->bus_num, s->addr, p);
892
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);
897 }
898}
899
900static void usb_host_detach_kernel(USBHostDevice *s)
901{
902 struct libusb_config_descriptor *conf;
903 int rc, i;
904
905 rc = libusb_get_active_config_descriptor(s->dev, &conf);
906 if (rc != 0) {
907 return;
908 }
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;
914 }
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;
919 }
920 libusb_free_config_descriptor(conf);
921}
922
923static void usb_host_attach_kernel(USBHostDevice *s)
924{
925 struct libusb_config_descriptor *conf;
926 int rc, i;
927
928 rc = libusb_get_active_config_descriptor(s->dev, &conf);
929 if (rc != 0) {
930 return;
931 }
932 for (i = 0; i < conf->bNumInterfaces; i++) {
933 if (!s->ifs[i].detached) {
934 continue;
935 }
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;
939 }
940 libusb_free_config_descriptor(conf);
941}
942
943static int usb_host_claim_interfaces(USBHostDevice *s, int configuration)
944{
945 USBDevice *udev = USB_DEVICE(s);
946 struct libusb_config_descriptor *conf;
947 int rc, i;
948
949 for (i = 0; i < USB_MAX_INTERFACES; i++) {
950 udev->altsetting[i] = 0;
951 }
952 udev->ninterfaces = 0;
953 udev->configuration = 0;
954
955 if (configuration == 0) {
956 /* address state - ignore */
957 return USB_RET_SUCCESS;
958 }
959
960 usb_host_detach_kernel(s);
961
962 rc = libusb_get_active_config_descriptor(s->dev, &conf);
963 if (rc != 0) {
964 return USB_RET_STALL;
965 }
966
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;
973 }
974 s->ifs[i].claimed = true;
975 }
976
977 udev->ninterfaces = conf->bNumInterfaces;
978 udev->configuration = configuration;
979
980 libusb_free_config_descriptor(conf);
981 return USB_RET_SUCCESS;
982}
983
984static void usb_host_release_interfaces(USBHostDevice *s)
985{
986 USBDevice *udev = USB_DEVICE(s);
987 int i, rc;
988
989 for (i = 0; i < udev->ninterfaces; i++) {
990 if (!s->ifs[i].claimed) {
991 continue;
992 }
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;
997 }
998}
999
1000static void usb_host_set_address(USBHostDevice *s, int addr)
1001{
1002 USBDevice *udev = USB_DEVICE(s);
1003
1004 trace_usb_host_set_address(s->bus_num, s->addr, addr);
1005 udev->addr = addr;
1006}
1007
1008static void usb_host_set_config(USBHostDevice *s, int config, USBPacket *p)
1009{
1010 int rc;
1011
1012 trace_usb_host_set_config(s->bus_num, s->addr, config);
1013
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);
1022 }
1023 return;
1024 }
1025 p->status = usb_host_claim_interfaces(s, config);
1026 if (p->status != USB_RET_SUCCESS) {
1027 return;
1028 }
1029 usb_host_ep_update(s);
1030}
1031
1032static void usb_host_set_interface(USBHostDevice *s, int iface, int alt,
1033 USBPacket *p)
1034{
1035 USBDevice *udev = USB_DEVICE(s);
1036 int rc;
1037
1038 trace_usb_host_set_interface(s->bus_num, s->addr, iface, alt);
1039
1040 usb_host_iso_free_all(s);
1041
1042 if (iface >= USB_MAX_INTERFACES) {
1043 p->status = USB_RET_STALL;
1044 return;
1045 }
1046
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);
1053 }
1054 return;
1055 }
1056
1057 udev->altsetting[iface] = alt;
1058 usb_host_ep_update(s);
1059}
1060
1061static void usb_host_handle_control(USBDevice *udev, USBPacket *p,
1062 int request, int value, int index,
1063 int length, uint8_t *data)
1064{
1065 USBHostDevice *s = USB_HOST_DEVICE(udev);
1066 USBHostRequest *r;
1067 int rc;
1068
1069 trace_usb_host_req_control(s->bus_num, s->addr, p, request, value, index);
1070
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;
1075 }
1076
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;
1082
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;
1087
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;
1092
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;
1100 }
1101 }
1102
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);
1109 }
1110
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);
1121 }
1122 return;
1123 }
1124
1125 p->status = USB_RET_ASYNC;
1126}
1127
1128static void usb_host_handle_data(USBDevice *udev, USBPacket *p)
1129{
1130 USBHostDevice *s = USB_HOST_DEVICE(udev);
1131 USBHostRequest *r;
1132 size_t size;
1133 int ep, rc;
1134
1135 if (usb_host_use_combining(p->ep) && p->state == USB_PACKET_SETUP) {
1136 p->status = USB_RET_ADD_TO_QUEUE;
1137 return;
1138 }
1139
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);
1143
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;
1148 }
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;
1153 }
1154
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);
1161 }
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);
1172 }
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);
1184 }
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;
1193 }
1194
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);
1202 }
1203 return;
1204 }
1205
1206 p->status = USB_RET_ASYNC;
1207}
1208
1209static void usb_host_flush_ep_queue(USBDevice *dev, USBEndpoint *ep)
1210{
1211 if (usb_host_use_combining(ep)) {
1212 usb_ep_combine_input_packets(ep);
1213 }
1214}
1215
1216static void usb_host_handle_reset(USBDevice *udev)
1217{
1218 USBHostDevice *s = USB_HOST_DEVICE(udev);
1219
1220 trace_usb_host_reset(s->bus_num, s->addr);
1221
1222 if (udev->configuration == 0) {
1223 return;
1224 }
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);
1229}
1230
1231static 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()
1237 }
1238};
1239
1240static 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(),
1254};
1255
1256static void usb_host_class_initfn(ObjectClass *klass, void *data)
1257{
1258 DeviceClass *dc = DEVICE_CLASS(klass);
1259 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
1260
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;
1271}
1272
1273static 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,
1278};
1279
1280static void usb_host_register_types(void)
1281{
1282 type_register_static(&usb_host_dev_info);
1283}
1284
1285type_init(usb_host_register_types)
1286
1287/* ------------------------------------------------------------------------ */
1288
1289static QEMUTimer *usb_auto_timer;
1290static VMChangeStateEntry *usb_vmstate;
1291
1292static void usb_host_vm_state(void *unused, int running, RunState state)
1293{
1294 if (running) {
1295 usb_host_auto_check(unused);
1296 }
1297}
1298
1299static void usb_host_auto_check(void *unused)
1300{
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;
1307
1308 if (usb_host_init() != 0) {
1309 return;
1310 }
1311
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;
1317 }
1318 if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) {
1319 continue;
1320 }
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;
1326 }
1327 if (f->addr > 0 &&
1328 f->addr != libusb_get_device_address(devs[i])) {
1329 continue;
1330 }
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;
1336 }
1337 }
1338 if (f->vendor_id > 0 &&
1339 f->vendor_id != ddesc.idVendor) {
1340 continue;
1341 }
1342 if (f->product_id > 0 &&
1343 f->product_id != ddesc.idProduct) {
1344 continue;
1345 }
1346
1347 /* We got a match */
1348 s->seen++;
1349 if (s->errcount >= 3) {
1350 continue;
1351 }
1352 if (s->dh != NULL) {
1353 continue;
1354 }
1355 if (usb_host_open(s, devs[i]) < 0) {
1356 s->errcount++;
1357 continue;
1358 }
1359 break;
1360 }
1361 }
1362 libusb_free_device_list(devs, 1);
1363
1364 QTAILQ_FOREACH(s, &hostdevs, next) {
1365 if (s->dh == NULL) {
1366 unconnected++;
1367 }
1368 if (s->seen == 0) {
1369 if (s->dh) {
1370 usb_host_close(s);
1371 }
1372 s->errcount = 0;
1373 }
1374 s->seen = 0;
1375 }
1376
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();
1383 }
1384 return;
1385 }
1386#endif
1387 }
1388
1389 if (!usb_vmstate) {
1390 usb_vmstate = qemu_add_vm_change_state_handler(usb_host_vm_state, NULL);
1391 }
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;
1396 }
1397 trace_usb_host_auto_scan_enabled();
1398 }
1399 qemu_mod_timer(usb_auto_timer, qemu_get_clock_ms(rt_clock) + 2000);
1400}
1401
1402void usb_host_info(Monitor *mon, const QDict *qdict)
1403{
1404 libusb_device **devs;
1405 struct libusb_device_descriptor ddesc;
1406 char port[16];
1407 int i, n;
1408
1409 if (usb_host_init() != 0) {
1410 return;
1411 }
1412
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;
1417 }
1418 if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) {
1419 continue;
1420 }
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);
1439 }
1440 }
1441 monitor_printf(mon, "\n");
1442 }
1443 libusb_free_device_list(devs, 1);
1444}