]> git.proxmox.com Git - mirror_qemu.git/blame - hw/usb/host-libusb.c
monitor/usb: register 'info usbhost' dynamically
[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
e532b2e0 36#include "qemu/osdep.h"
db1015e9 37#include "qom/object.h"
a277c3e0 38#ifndef CONFIG_WIN32
2b2325ff 39#include <poll.h>
a277c3e0 40#endif
2b2325ff
GH
41#include <libusb.h>
42
202d69a7
GH
43#ifdef CONFIG_LINUX
44#include <sys/ioctl.h>
45#include <linux/usbdevice_fs.h>
46#endif
47
da34e65c 48#include "qapi/error.h"
d6454270 49#include "migration/vmstate.h"
2b2325ff 50#include "monitor/monitor.h"
d49b6836 51#include "qemu/error-report.h"
db725815 52#include "qemu/main-loop.h"
0b8fa32f 53#include "qemu/module.h"
54d31236 54#include "sysemu/runstate.h"
2b2325ff
GH
55#include "sysemu/sysemu.h"
56#include "trace.h"
57
a27bd6c7 58#include "hw/qdev-properties.h"
2b2325ff
GH
59#include "hw/usb.h"
60
61/* ------------------------------------------------------------------------ */
62
63#define TYPE_USB_HOST_DEVICE "usb-host"
8063396b 64OBJECT_DECLARE_SIMPLE_TYPE(USBHostDevice, USB_HOST_DEVICE)
2b2325ff 65
2b2325ff
GH
66typedef struct USBHostRequest USBHostRequest;
67typedef struct USBHostIsoXfer USBHostIsoXfer;
68typedef struct USBHostIsoRing USBHostIsoRing;
69
70struct USBAutoFilter {
71 uint32_t bus_num;
72 uint32_t addr;
73 char *port;
74 uint32_t vendor_id;
75 uint32_t product_id;
76};
77
78enum USBHostDeviceOptions {
79 USB_HOST_OPT_PIPELINE,
80};
81
82struct USBHostDevice {
83 USBDevice parent_obj;
84
85 /* properties */
86 struct USBAutoFilter match;
9f815e83 87 char *hostdevice;
2b2325ff
GH
88 int32_t bootindex;
89 uint32_t iso_urb_count;
90 uint32_t iso_urb_frames;
91 uint32_t options;
92 uint32_t loglevel;
e058fa2d 93 bool needs_autoscan;
1dfe2b91
GH
94 bool allow_one_guest_reset;
95 bool allow_all_guest_resets;
7bacaf5f 96 bool suppress_remote_wake;
1dfe2b91 97
2b2325ff
GH
98 /* state */
99 QTAILQ_ENTRY(USBHostDevice) next;
100 int seen, errcount;
101 int bus_num;
102 int addr;
103 char port[16];
104
9f815e83 105 int hostfd;
2b2325ff
GH
106 libusb_device *dev;
107 libusb_device_handle *dh;
108 struct libusb_device_descriptor ddesc;
109
110 struct {
111 bool detached;
112 bool claimed;
113 } ifs[USB_MAX_INTERFACES];
114
115 /* callbacks & friends */
95efb20c
GH
116 QEMUBH *bh_nodev;
117 QEMUBH *bh_postld;
3280ea8e 118 bool bh_postld_pending;
2b2325ff
GH
119 Notifier exit;
120
121 /* request queues */
122 QTAILQ_HEAD(, USBHostRequest) requests;
123 QTAILQ_HEAD(, USBHostIsoRing) isorings;
124};
125
126struct USBHostRequest {
127 USBHostDevice *host;
128 USBPacket *p;
129 bool in;
130 struct libusb_transfer *xfer;
131 unsigned char *buffer;
132 unsigned char *cbuf;
133 unsigned int clen;
b88a3e01 134 bool usb3ep0quirk;
2b2325ff
GH
135 QTAILQ_ENTRY(USBHostRequest) next;
136};
137
138struct USBHostIsoXfer {
139 USBHostIsoRing *ring;
140 struct libusb_transfer *xfer;
141 bool copy_complete;
142 unsigned int packet;
143 QTAILQ_ENTRY(USBHostIsoXfer) next;
144};
145
146struct USBHostIsoRing {
147 USBHostDevice *host;
148 USBEndpoint *ep;
149 QTAILQ_HEAD(, USBHostIsoXfer) unused;
150 QTAILQ_HEAD(, USBHostIsoXfer) inflight;
151 QTAILQ_HEAD(, USBHostIsoXfer) copy;
152 QTAILQ_ENTRY(USBHostIsoRing) next;
153};
154
155static QTAILQ_HEAD(, USBHostDevice) hostdevs =
156 QTAILQ_HEAD_INITIALIZER(hostdevs);
157
158static void usb_host_auto_check(void *unused);
159static void usb_host_release_interfaces(USBHostDevice *s);
160static void usb_host_nodev(USBHostDevice *s);
f34d5c75 161static void usb_host_detach_kernel(USBHostDevice *s);
2b2325ff
GH
162static void usb_host_attach_kernel(USBHostDevice *s);
163
164/* ------------------------------------------------------------------------ */
165
1e03e407
CJ
166#ifndef LIBUSB_LOG_LEVEL_WARNING /* older libusb didn't define these */
167#define LIBUSB_LOG_LEVEL_WARNING 2
168#endif
169
170/* ------------------------------------------------------------------------ */
171
2b2325ff
GH
172#define CONTROL_TIMEOUT 10000 /* 10 sec */
173#define BULK_TIMEOUT 0 /* unlimited */
174#define INTR_TIMEOUT 0 /* unlimited */
175
102a3d84
GH
176#ifndef LIBUSB_API_VERSION
177# define LIBUSB_API_VERSION LIBUSBX_API_VERSION
178#endif
179#if LIBUSB_API_VERSION >= 0x01000103
322fd1f4
GH
180# define HAVE_STREAMS 1
181#endif
2980a316
GH
182#if LIBUSB_API_VERSION >= 0x01000106
183# define HAVE_SUPER_PLUS 1
184#endif
322fd1f4 185
2b2325ff
GH
186static const char *speed_name[] = {
187 [LIBUSB_SPEED_UNKNOWN] = "?",
188 [LIBUSB_SPEED_LOW] = "1.5",
189 [LIBUSB_SPEED_FULL] = "12",
190 [LIBUSB_SPEED_HIGH] = "480",
191 [LIBUSB_SPEED_SUPER] = "5000",
2980a316
GH
192#ifdef HAVE_SUPER_PLUS
193 [LIBUSB_SPEED_SUPER_PLUS] = "5000+",
194#endif
2b2325ff
GH
195};
196
197static const unsigned int speed_map[] = {
198 [LIBUSB_SPEED_LOW] = USB_SPEED_LOW,
199 [LIBUSB_SPEED_FULL] = USB_SPEED_FULL,
200 [LIBUSB_SPEED_HIGH] = USB_SPEED_HIGH,
201 [LIBUSB_SPEED_SUPER] = USB_SPEED_SUPER,
2980a316
GH
202#ifdef HAVE_SUPER_PLUS
203 [LIBUSB_SPEED_SUPER_PLUS] = USB_SPEED_SUPER,
204#endif
2b2325ff
GH
205};
206
207static const unsigned int status_map[] = {
208 [LIBUSB_TRANSFER_COMPLETED] = USB_RET_SUCCESS,
209 [LIBUSB_TRANSFER_ERROR] = USB_RET_IOERROR,
210 [LIBUSB_TRANSFER_TIMED_OUT] = USB_RET_IOERROR,
211 [LIBUSB_TRANSFER_CANCELLED] = USB_RET_IOERROR,
212 [LIBUSB_TRANSFER_STALL] = USB_RET_STALL,
213 [LIBUSB_TRANSFER_NO_DEVICE] = USB_RET_NODEV,
214 [LIBUSB_TRANSFER_OVERFLOW] = USB_RET_BABBLE,
215};
216
217static const char *err_names[] = {
218 [-LIBUSB_ERROR_IO] = "IO",
219 [-LIBUSB_ERROR_INVALID_PARAM] = "INVALID_PARAM",
220 [-LIBUSB_ERROR_ACCESS] = "ACCESS",
221 [-LIBUSB_ERROR_NO_DEVICE] = "NO_DEVICE",
222 [-LIBUSB_ERROR_NOT_FOUND] = "NOT_FOUND",
223 [-LIBUSB_ERROR_BUSY] = "BUSY",
224 [-LIBUSB_ERROR_TIMEOUT] = "TIMEOUT",
225 [-LIBUSB_ERROR_OVERFLOW] = "OVERFLOW",
226 [-LIBUSB_ERROR_PIPE] = "PIPE",
227 [-LIBUSB_ERROR_INTERRUPTED] = "INTERRUPTED",
228 [-LIBUSB_ERROR_NO_MEM] = "NO_MEM",
229 [-LIBUSB_ERROR_NOT_SUPPORTED] = "NOT_SUPPORTED",
230 [-LIBUSB_ERROR_OTHER] = "OTHER",
231};
232
233static libusb_context *ctx;
234static uint32_t loglevel;
235
a277c3e0
SW
236#ifndef CONFIG_WIN32
237
2b2325ff
GH
238static void usb_host_handle_fd(void *opaque)
239{
240 struct timeval tv = { 0, 0 };
241 libusb_handle_events_timeout(ctx, &tv);
242}
243
244static void usb_host_add_fd(int fd, short events, void *user_data)
245{
246 qemu_set_fd_handler(fd,
247 (events & POLLIN) ? usb_host_handle_fd : NULL,
248 (events & POLLOUT) ? usb_host_handle_fd : NULL,
249 ctx);
250}
251
252static void usb_host_del_fd(int fd, void *user_data)
253{
254 qemu_set_fd_handler(fd, NULL, NULL, NULL);
255}
256
a277c3e0
SW
257#endif /* !CONFIG_WIN32 */
258
2b2325ff
GH
259static int usb_host_init(void)
260{
a277c3e0 261#ifndef CONFIG_WIN32
2b2325ff 262 const struct libusb_pollfd **poll;
a277c3e0 263#endif
3bf2b3a1 264 int rc;
2b2325ff
GH
265
266 if (ctx) {
267 return 0;
268 }
269 rc = libusb_init(&ctx);
270 if (rc != 0) {
271 return -1;
272 }
9d8fa0df
JT
273#if LIBUSB_API_VERSION >= 0x01000106
274 libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, loglevel);
275#else
2b2325ff 276 libusb_set_debug(ctx, loglevel);
9d8fa0df 277#endif
a277c3e0
SW
278#ifdef CONFIG_WIN32
279 /* FIXME: add support for Windows. */
280#else
2b2325ff
GH
281 libusb_set_pollfd_notifiers(ctx, usb_host_add_fd,
282 usb_host_del_fd,
283 ctx);
284 poll = libusb_get_pollfds(ctx);
285 if (poll) {
3bf2b3a1 286 int i;
2b2325ff
GH
287 for (i = 0; poll[i] != NULL; i++) {
288 usb_host_add_fd(poll[i]->fd, poll[i]->events, ctx);
289 }
290 }
291 free(poll);
a277c3e0 292#endif
2b2325ff
GH
293 return 0;
294}
295
296static int usb_host_get_port(libusb_device *dev, char *port, size_t len)
297{
2b2325ff
GH
298 uint8_t path[7];
299 size_t off;
300 int rc, i;
301
102a3d84 302#if LIBUSB_API_VERSION >= 0x01000102
bc45de8c
HG
303 rc = libusb_get_port_numbers(dev, path, 7);
304#else
2b2325ff 305 rc = libusb_get_port_path(ctx, dev, path, 7);
bc45de8c 306#endif
2b2325ff
GH
307 if (rc < 0) {
308 return 0;
309 }
310 off = snprintf(port, len, "%d", path[0]);
311 for (i = 1; i < rc; i++) {
312 off += snprintf(port+off, len-off, ".%d", path[i]);
313 }
314 return off;
2b2325ff
GH
315}
316
317static void usb_host_libusb_error(const char *func, int rc)
318{
319 const char *errname;
320
321 if (rc >= 0) {
322 return;
323 }
324
325 if (-rc < ARRAY_SIZE(err_names) && err_names[-rc]) {
326 errname = err_names[-rc];
327 } else {
328 errname = "?";
329 }
2e6a0dd1 330 error_report("%s: %d [%s]", func, rc, errname);
2b2325ff
GH
331}
332
333/* ------------------------------------------------------------------------ */
334
335static bool usb_host_use_combining(USBEndpoint *ep)
336{
337 int type;
338
339 if (!ep->pipeline) {
340 return false;
341 }
342 if (ep->pid != USB_TOKEN_IN) {
343 return false;
344 }
345 type = usb_ep_get_type(ep->dev, ep->pid, ep->nr);
346 if (type != USB_ENDPOINT_XFER_BULK) {
347 return false;
348 }
349 return true;
350}
351
352/* ------------------------------------------------------------------------ */
353
354static USBHostRequest *usb_host_req_alloc(USBHostDevice *s, USBPacket *p,
355 bool in, size_t bufsize)
356{
357 USBHostRequest *r = g_new0(USBHostRequest, 1);
358
359 r->host = s;
360 r->p = p;
361 r->in = in;
362 r->xfer = libusb_alloc_transfer(0);
363 if (bufsize) {
364 r->buffer = g_malloc(bufsize);
365 }
366 QTAILQ_INSERT_TAIL(&s->requests, r, next);
367 return r;
368}
369
370static void usb_host_req_free(USBHostRequest *r)
371{
76d0a936 372 QTAILQ_REMOVE(&r->host->requests, r, next);
2b2325ff
GH
373 libusb_free_transfer(r->xfer);
374 g_free(r->buffer);
375 g_free(r);
376}
377
378static USBHostRequest *usb_host_req_find(USBHostDevice *s, USBPacket *p)
379{
380 USBHostRequest *r;
381
382 QTAILQ_FOREACH(r, &s->requests, next) {
383 if (r->p == p) {
384 return r;
385 }
386 }
387 return NULL;
388}
389
c16e3664 390static void LIBUSB_CALL usb_host_req_complete_ctrl(struct libusb_transfer *xfer)
2b2325ff
GH
391{
392 USBHostRequest *r = xfer->user_data;
393 USBHostDevice *s = r->host;
394 bool disconnect = (xfer->status == LIBUSB_TRANSFER_NO_DEVICE);
395
396 if (r->p == NULL) {
397 goto out; /* request was canceled */
398 }
399
400 r->p->status = status_map[xfer->status];
401 r->p->actual_length = xfer->actual_length;
402 if (r->in && xfer->actual_length) {
7bacaf5f
YB
403 USBDevice *udev = USB_DEVICE(s);
404 struct libusb_config_descriptor *conf = (void *)r->cbuf;
2b2325ff 405 memcpy(r->cbuf, r->buffer + 8, xfer->actual_length);
b88a3e01
GH
406
407 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
408 * to work redirected to a not superspeed capable hcd */
409 if (r->usb3ep0quirk && xfer->actual_length >= 18 &&
410 r->cbuf[7] == 9) {
411 r->cbuf[7] = 64;
412 }
7bacaf5f
YB
413 /*
414 *If this is GET_DESCRIPTOR request for configuration descriptor,
415 * remove 'remote wakeup' flag from it to prevent idle power down
416 * in Windows guest
417 */
418 if (s->suppress_remote_wake &&
419 udev->setup_buf[0] == USB_DIR_IN &&
420 udev->setup_buf[1] == USB_REQ_GET_DESCRIPTOR &&
421 udev->setup_buf[3] == USB_DT_CONFIG && udev->setup_buf[2] == 0 &&
422 xfer->actual_length >
423 offsetof(struct libusb_config_descriptor, bmAttributes) &&
424 (conf->bmAttributes & USB_CFG_ATT_WAKEUP)) {
425 trace_usb_host_remote_wakeup_removed(s->bus_num, s->addr);
426 conf->bmAttributes &= ~USB_CFG_ATT_WAKEUP;
427 }
2b2325ff
GH
428 }
429 trace_usb_host_req_complete(s->bus_num, s->addr, r->p,
430 r->p->status, r->p->actual_length);
431 usb_generic_async_ctrl_complete(USB_DEVICE(s), r->p);
432
433out:
434 usb_host_req_free(r);
435 if (disconnect) {
436 usb_host_nodev(s);
437 }
438}
439
c16e3664 440static void LIBUSB_CALL usb_host_req_complete_data(struct libusb_transfer *xfer)
2b2325ff
GH
441{
442 USBHostRequest *r = xfer->user_data;
443 USBHostDevice *s = r->host;
444 bool disconnect = (xfer->status == LIBUSB_TRANSFER_NO_DEVICE);
445
446 if (r->p == NULL) {
447 goto out; /* request was canceled */
448 }
449
450 r->p->status = status_map[xfer->status];
451 if (r->in && xfer->actual_length) {
452 usb_packet_copy(r->p, r->buffer, xfer->actual_length);
453 }
454 trace_usb_host_req_complete(s->bus_num, s->addr, r->p,
455 r->p->status, r->p->actual_length);
456 if (usb_host_use_combining(r->p->ep)) {
457 usb_combined_input_packet_complete(USB_DEVICE(s), r->p);
458 } else {
459 usb_packet_complete(USB_DEVICE(s), r->p);
460 }
461
462out:
463 usb_host_req_free(r);
464 if (disconnect) {
465 usb_host_nodev(s);
466 }
467}
468
469static void usb_host_req_abort(USBHostRequest *r)
470{
471 USBHostDevice *s = r->host;
45ec2671 472 bool inflight = (r->p && r->p->state == USB_PACKET_ASYNC);
2b2325ff
GH
473
474 if (inflight) {
475 r->p->status = USB_RET_NODEV;
476 trace_usb_host_req_complete(s->bus_num, s->addr, r->p,
477 r->p->status, r->p->actual_length);
478 if (r->p->ep->nr == 0) {
479 usb_generic_async_ctrl_complete(USB_DEVICE(s), r->p);
480 } else {
481 usb_packet_complete(USB_DEVICE(s), r->p);
482 }
483 r->p = NULL;
2b2325ff 484
2b2325ff
GH
485 libusb_cancel_transfer(r->xfer);
486 }
487}
488
489/* ------------------------------------------------------------------------ */
490
c16e3664
SW
491static void LIBUSB_CALL
492usb_host_req_complete_iso(struct libusb_transfer *transfer)
2b2325ff
GH
493{
494 USBHostIsoXfer *xfer = transfer->user_data;
495
496 if (!xfer) {
497 /* USBHostIsoXfer released while inflight */
498 g_free(transfer->buffer);
499 libusb_free_transfer(transfer);
500 return;
501 }
502
503 QTAILQ_REMOVE(&xfer->ring->inflight, xfer, next);
504 if (QTAILQ_EMPTY(&xfer->ring->inflight)) {
505 USBHostDevice *s = xfer->ring->host;
506 trace_usb_host_iso_stop(s->bus_num, s->addr, xfer->ring->ep->nr);
507 }
508 if (xfer->ring->ep->pid == USB_TOKEN_IN) {
509 QTAILQ_INSERT_TAIL(&xfer->ring->copy, xfer, next);
e206ddfb 510 usb_wakeup(xfer->ring->ep, 0);
2b2325ff
GH
511 } else {
512 QTAILQ_INSERT_TAIL(&xfer->ring->unused, xfer, next);
513 }
514}
515
516static USBHostIsoRing *usb_host_iso_alloc(USBHostDevice *s, USBEndpoint *ep)
517{
518 USBHostIsoRing *ring = g_new0(USBHostIsoRing, 1);
519 USBHostIsoXfer *xfer;
520 /* FIXME: check interval (for now assume one xfer per frame) */
521 int packets = s->iso_urb_frames;
522 int i;
523
524 ring->host = s;
525 ring->ep = ep;
526 QTAILQ_INIT(&ring->unused);
527 QTAILQ_INIT(&ring->inflight);
528 QTAILQ_INIT(&ring->copy);
529 QTAILQ_INSERT_TAIL(&s->isorings, ring, next);
530
531 for (i = 0; i < s->iso_urb_count; i++) {
532 xfer = g_new0(USBHostIsoXfer, 1);
533 xfer->ring = ring;
534 xfer->xfer = libusb_alloc_transfer(packets);
535 xfer->xfer->dev_handle = s->dh;
536 xfer->xfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
537
538 xfer->xfer->endpoint = ring->ep->nr;
539 if (ring->ep->pid == USB_TOKEN_IN) {
540 xfer->xfer->endpoint |= USB_DIR_IN;
541 }
542 xfer->xfer->callback = usb_host_req_complete_iso;
543 xfer->xfer->user_data = xfer;
544
545 xfer->xfer->num_iso_packets = packets;
546 xfer->xfer->length = ring->ep->max_packet_size * packets;
547 xfer->xfer->buffer = g_malloc0(xfer->xfer->length);
548
549 QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
550 }
551
552 return ring;
553}
554
555static USBHostIsoRing *usb_host_iso_find(USBHostDevice *s, USBEndpoint *ep)
556{
557 USBHostIsoRing *ring;
558
559 QTAILQ_FOREACH(ring, &s->isorings, next) {
560 if (ring->ep == ep) {
561 return ring;
562 }
563 }
564 return NULL;
565}
566
567static void usb_host_iso_reset_xfer(USBHostIsoXfer *xfer)
568{
569 libusb_set_iso_packet_lengths(xfer->xfer,
570 xfer->ring->ep->max_packet_size);
571 xfer->packet = 0;
572 xfer->copy_complete = false;
573}
574
575static void usb_host_iso_free_xfer(USBHostIsoXfer *xfer, bool inflight)
576{
577 if (inflight) {
578 xfer->xfer->user_data = NULL;
579 } else {
580 g_free(xfer->xfer->buffer);
581 libusb_free_transfer(xfer->xfer);
582 }
583 g_free(xfer);
584}
585
586static void usb_host_iso_free(USBHostIsoRing *ring)
587{
588 USBHostIsoXfer *xfer;
589
590 while ((xfer = QTAILQ_FIRST(&ring->inflight)) != NULL) {
591 QTAILQ_REMOVE(&ring->inflight, xfer, next);
592 usb_host_iso_free_xfer(xfer, true);
593 }
594 while ((xfer = QTAILQ_FIRST(&ring->unused)) != NULL) {
595 QTAILQ_REMOVE(&ring->unused, xfer, next);
596 usb_host_iso_free_xfer(xfer, false);
597 }
598 while ((xfer = QTAILQ_FIRST(&ring->copy)) != NULL) {
599 QTAILQ_REMOVE(&ring->copy, xfer, next);
600 usb_host_iso_free_xfer(xfer, false);
601 }
602
603 QTAILQ_REMOVE(&ring->host->isorings, ring, next);
604 g_free(ring);
605}
606
607static void usb_host_iso_free_all(USBHostDevice *s)
608{
609 USBHostIsoRing *ring;
610
611 while ((ring = QTAILQ_FIRST(&s->isorings)) != NULL) {
612 usb_host_iso_free(ring);
613 }
614}
615
616static bool usb_host_iso_data_copy(USBHostIsoXfer *xfer, USBPacket *p)
617{
618 unsigned int psize;
619 unsigned char *buf;
620
621 buf = libusb_get_iso_packet_buffer_simple(xfer->xfer, xfer->packet);
622 if (p->pid == USB_TOKEN_OUT) {
623 psize = p->iov.size;
624 if (psize > xfer->ring->ep->max_packet_size) {
625 /* should not happen (guest bug) */
626 psize = xfer->ring->ep->max_packet_size;
627 }
628 xfer->xfer->iso_packet_desc[xfer->packet].length = psize;
629 } else {
630 psize = xfer->xfer->iso_packet_desc[xfer->packet].actual_length;
631 if (psize > p->iov.size) {
632 /* should not happen (guest bug) */
633 psize = p->iov.size;
634 }
635 }
636 usb_packet_copy(p, buf, psize);
637 xfer->packet++;
638 xfer->copy_complete = (xfer->packet == xfer->xfer->num_iso_packets);
639 return xfer->copy_complete;
640}
641
642static void usb_host_iso_data_in(USBHostDevice *s, USBPacket *p)
643{
644 USBHostIsoRing *ring;
645 USBHostIsoXfer *xfer;
646 bool disconnect = false;
647 int rc;
648
649 ring = usb_host_iso_find(s, p->ep);
650 if (ring == NULL) {
651 ring = usb_host_iso_alloc(s, p->ep);
652 }
653
654 /* copy data to guest */
655 xfer = QTAILQ_FIRST(&ring->copy);
656 if (xfer != NULL) {
657 if (usb_host_iso_data_copy(xfer, p)) {
658 QTAILQ_REMOVE(&ring->copy, xfer, next);
659 QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
660 }
661 }
662
663 /* submit empty bufs to host */
664 while ((xfer = QTAILQ_FIRST(&ring->unused)) != NULL) {
665 QTAILQ_REMOVE(&ring->unused, xfer, next);
666 usb_host_iso_reset_xfer(xfer);
667 rc = libusb_submit_transfer(xfer->xfer);
668 if (rc != 0) {
669 usb_host_libusb_error("libusb_submit_transfer [iso]", rc);
670 QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
671 if (rc == LIBUSB_ERROR_NO_DEVICE) {
672 disconnect = true;
673 }
674 break;
675 }
676 if (QTAILQ_EMPTY(&ring->inflight)) {
677 trace_usb_host_iso_start(s->bus_num, s->addr, p->ep->nr);
678 }
679 QTAILQ_INSERT_TAIL(&ring->inflight, xfer, next);
680 }
681
682 if (disconnect) {
683 usb_host_nodev(s);
684 }
685}
686
687static void usb_host_iso_data_out(USBHostDevice *s, USBPacket *p)
688{
689 USBHostIsoRing *ring;
690 USBHostIsoXfer *xfer;
691 bool disconnect = false;
692 int rc, filled = 0;
693
694 ring = usb_host_iso_find(s, p->ep);
695 if (ring == NULL) {
696 ring = usb_host_iso_alloc(s, p->ep);
697 }
698
699 /* copy data from guest */
700 xfer = QTAILQ_FIRST(&ring->copy);
701 while (xfer != NULL && xfer->copy_complete) {
702 filled++;
703 xfer = QTAILQ_NEXT(xfer, next);
704 }
705 if (xfer == NULL) {
706 xfer = QTAILQ_FIRST(&ring->unused);
707 if (xfer == NULL) {
708 trace_usb_host_iso_out_of_bufs(s->bus_num, s->addr, p->ep->nr);
709 return;
710 }
711 QTAILQ_REMOVE(&ring->unused, xfer, next);
712 usb_host_iso_reset_xfer(xfer);
713 QTAILQ_INSERT_TAIL(&ring->copy, xfer, next);
714 }
715 usb_host_iso_data_copy(xfer, p);
716
717 if (QTAILQ_EMPTY(&ring->inflight)) {
718 /* wait until half of our buffers are filled
719 before kicking the iso out stream */
720 if (filled*2 < s->iso_urb_count) {
721 return;
722 }
723 }
724
725 /* submit filled bufs to host */
726 while ((xfer = QTAILQ_FIRST(&ring->copy)) != NULL &&
727 xfer->copy_complete) {
728 QTAILQ_REMOVE(&ring->copy, xfer, next);
729 rc = libusb_submit_transfer(xfer->xfer);
730 if (rc != 0) {
731 usb_host_libusb_error("libusb_submit_transfer [iso]", rc);
732 QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
733 if (rc == LIBUSB_ERROR_NO_DEVICE) {
734 disconnect = true;
735 }
736 break;
737 }
738 if (QTAILQ_EMPTY(&ring->inflight)) {
739 trace_usb_host_iso_start(s->bus_num, s->addr, p->ep->nr);
740 }
741 QTAILQ_INSERT_TAIL(&ring->inflight, xfer, next);
742 }
743
744 if (disconnect) {
745 usb_host_nodev(s);
746 }
747}
748
749/* ------------------------------------------------------------------------ */
750
b88a3e01 751static void usb_host_speed_compat(USBHostDevice *s)
c3268cc1 752{
b88a3e01 753 USBDevice *udev = USB_DEVICE(s);
c3268cc1
GH
754 struct libusb_config_descriptor *conf;
755 const struct libusb_interface_descriptor *intf;
756 const struct libusb_endpoint_descriptor *endp;
322fd1f4 757#ifdef HAVE_STREAMS
b88a3e01
GH
758 struct libusb_ss_endpoint_companion_descriptor *endp_ss_comp;
759#endif
760 bool compat_high = true;
761 bool compat_full = true;
c3268cc1
GH
762 uint8_t type;
763 int rc, c, i, a, e;
764
765 for (c = 0;; c++) {
766 rc = libusb_get_config_descriptor(s->dev, c, &conf);
767 if (rc != 0) {
768 break;
769 }
770 for (i = 0; i < conf->bNumInterfaces; i++) {
771 for (a = 0; a < conf->interface[i].num_altsetting; a++) {
772 intf = &conf->interface[i].altsetting[a];
b7b2a60b
GH
773
774 if (intf->bInterfaceClass == LIBUSB_CLASS_MASS_STORAGE &&
775 intf->bInterfaceSubClass == 6) { /* SCSI */
776 udev->flags |= (1 << USB_DEV_FLAG_IS_SCSI_STORAGE);
777 break;
778 }
779
c3268cc1
GH
780 for (e = 0; e < intf->bNumEndpoints; e++) {
781 endp = &intf->endpoint[e];
782 type = endp->bmAttributes & 0x3;
783 switch (type) {
784 case 0x01: /* ISO */
b88a3e01
GH
785 compat_full = false;
786 compat_high = false;
787 break;
788 case 0x02: /* BULK */
322fd1f4 789#ifdef HAVE_STREAMS
b88a3e01
GH
790 rc = libusb_get_ss_endpoint_companion_descriptor
791 (ctx, endp, &endp_ss_comp);
792 if (rc == LIBUSB_SUCCESS) {
6a711234
GH
793 int streams = endp_ss_comp->bmAttributes & 0x1f;
794 if (streams) {
795 compat_full = false;
796 compat_high = false;
797 }
b88a3e01
GH
798 libusb_free_ss_endpoint_companion_descriptor
799 (endp_ss_comp);
b88a3e01
GH
800 }
801#endif
802 break;
c3268cc1
GH
803 case 0x03: /* INTERRUPT */
804 if (endp->wMaxPacketSize > 64) {
b88a3e01
GH
805 compat_full = false;
806 }
807 if (endp->wMaxPacketSize > 1024) {
808 compat_high = false;
c3268cc1
GH
809 }
810 break;
811 }
812 }
813 }
814 }
815 libusb_free_config_descriptor(conf);
816 }
b88a3e01
GH
817
818 udev->speedmask = (1 << udev->speed);
819 if (udev->speed == USB_SPEED_SUPER && compat_high) {
79ae25af 820 udev->speedmask |= USB_SPEED_MASK_HIGH;
b88a3e01
GH
821 }
822 if (udev->speed == USB_SPEED_SUPER && compat_full) {
79ae25af 823 udev->speedmask |= USB_SPEED_MASK_FULL;
b88a3e01
GH
824 }
825 if (udev->speed == USB_SPEED_HIGH && compat_full) {
79ae25af 826 udev->speedmask |= USB_SPEED_MASK_FULL;
b88a3e01 827 }
c3268cc1
GH
828}
829
2b2325ff
GH
830static void usb_host_ep_update(USBHostDevice *s)
831{
832 static const char *tname[] = {
833 [USB_ENDPOINT_XFER_CONTROL] = "control",
834 [USB_ENDPOINT_XFER_ISOC] = "isoc",
835 [USB_ENDPOINT_XFER_BULK] = "bulk",
836 [USB_ENDPOINT_XFER_INT] = "int",
837 };
838 USBDevice *udev = USB_DEVICE(s);
839 struct libusb_config_descriptor *conf;
840 const struct libusb_interface_descriptor *intf;
841 const struct libusb_endpoint_descriptor *endp;
322fd1f4 842#ifdef HAVE_STREAMS
b664b80f
HG
843 struct libusb_ss_endpoint_companion_descriptor *endp_ss_comp;
844#endif
2b2325ff 845 uint8_t devep, type;
0dbe4768 846 int pid, ep, alt;
2b2325ff
GH
847 int rc, i, e;
848
849 usb_ep_reset(udev);
850 rc = libusb_get_active_config_descriptor(s->dev, &conf);
851 if (rc != 0) {
852 return;
853 }
854 trace_usb_host_parse_config(s->bus_num, s->addr,
855 conf->bConfigurationValue, true);
856
857 for (i = 0; i < conf->bNumInterfaces; i++) {
0dbe4768
NR
858 /*
859 * The udev->altsetting array indexes alternate settings
860 * by the interface number. Get the 0th alternate setting
861 * first so that we can grab the interface number, and
862 * then correct the alternate setting value if necessary.
863 */
864 intf = &conf->interface[i].altsetting[0];
865 alt = udev->altsetting[intf->bInterfaceNumber];
866
867 if (alt != 0) {
868 assert(alt < conf->interface[i].num_altsetting);
869 intf = &conf->interface[i].altsetting[alt];
870 }
871
2b2325ff
GH
872 trace_usb_host_parse_interface(s->bus_num, s->addr,
873 intf->bInterfaceNumber,
874 intf->bAlternateSetting, true);
875 for (e = 0; e < intf->bNumEndpoints; e++) {
876 endp = &intf->endpoint[e];
877
878 devep = endp->bEndpointAddress;
879 pid = (devep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT;
880 ep = devep & 0xf;
881 type = endp->bmAttributes & 0x3;
882
883 if (ep == 0) {
884 trace_usb_host_parse_error(s->bus_num, s->addr,
885 "invalid endpoint address");
886 return;
887 }
888 if (usb_ep_get_type(udev, pid, ep) != USB_ENDPOINT_XFER_INVALID) {
889 trace_usb_host_parse_error(s->bus_num, s->addr,
890 "duplicate endpoint address");
891 return;
892 }
893
894 trace_usb_host_parse_endpoint(s->bus_num, s->addr, ep,
895 (devep & USB_DIR_IN) ? "in" : "out",
896 tname[type], true);
897 usb_ep_set_max_packet_size(udev, pid, ep,
898 endp->wMaxPacketSize);
899 usb_ep_set_type(udev, pid, ep, type);
900 usb_ep_set_ifnum(udev, pid, ep, i);
901 usb_ep_set_halted(udev, pid, ep, 0);
322fd1f4 902#ifdef HAVE_STREAMS
b664b80f
HG
903 if (type == LIBUSB_TRANSFER_TYPE_BULK &&
904 libusb_get_ss_endpoint_companion_descriptor(ctx, endp,
905 &endp_ss_comp) == LIBUSB_SUCCESS) {
906 usb_ep_set_max_streams(udev, pid, ep,
907 endp_ss_comp->bmAttributes);
908 libusb_free_ss_endpoint_companion_descriptor(endp_ss_comp);
909 }
910#endif
2b2325ff
GH
911 }
912 }
913
914 libusb_free_config_descriptor(conf);
915}
916
9f815e83 917static int usb_host_open(USBHostDevice *s, libusb_device *dev, int hostfd)
2b2325ff
GH
918{
919 USBDevice *udev = USB_DEVICE(s);
202d69a7 920 int libusb_speed;
9f815e83
GH
921 int bus_num = 0;
922 int addr = 0;
2b2325ff 923 int rc;
7d553f27 924 Error *local_err = NULL;
2b2325ff 925
3280ea8e
GH
926 if (s->bh_postld_pending) {
927 return -1;
928 }
2b2325ff
GH
929 if (s->dh != NULL) {
930 goto fail;
931 }
9f815e83
GH
932
933 if (dev) {
934 bus_num = libusb_get_bus_number(dev);
935 addr = libusb_get_device_address(dev);
936 trace_usb_host_open_started(bus_num, addr);
937
938 rc = libusb_open(dev, &s->dh);
939 if (rc != 0) {
940 goto fail;
941 }
942 } else {
631009e7 943#if LIBUSB_API_VERSION >= 0x01000107 && !defined(CONFIG_WIN32)
9f815e83
GH
944 trace_usb_host_open_hostfd(hostfd);
945
946 rc = libusb_wrap_sys_device(ctx, hostfd, &s->dh);
947 if (rc != 0) {
948 goto fail;
949 }
950 s->hostfd = hostfd;
951 dev = libusb_get_device(s->dh);
952 bus_num = libusb_get_bus_number(dev);
953 addr = libusb_get_device_address(dev);
954#else
955 g_assert_not_reached();
956#endif
2b2325ff
GH
957 }
958
2b2325ff
GH
959 s->dev = dev;
960 s->bus_num = bus_num;
961 s->addr = addr;
f34d5c75
HG
962
963 usb_host_detach_kernel(s);
964
965 libusb_get_device_descriptor(dev, &s->ddesc);
2b2325ff
GH
966 usb_host_get_port(s->dev, s->port, sizeof(s->port));
967
968 usb_ep_init(udev);
969 usb_host_ep_update(s);
970
202d69a7 971 libusb_speed = libusb_get_device_speed(dev);
a3c27ea0
FF
972#if LIBUSB_API_VERSION >= 0x01000107 && defined(CONFIG_LINUX) && \
973 defined(USBDEVFS_GET_SPEED)
202d69a7
GH
974 if (hostfd && libusb_speed == 0) {
975 /*
976 * Workaround libusb bug: libusb_get_device_speed() does not
977 * work for libusb_wrap_sys_device() devices in v1.0.23.
978 *
979 * Speeds are defined in linux/usb/ch9.h, file not included
980 * due to name conflicts.
981 */
982 int rc = ioctl(hostfd, USBDEVFS_GET_SPEED, NULL);
983 switch (rc) {
984 case 1: /* low */
985 libusb_speed = LIBUSB_SPEED_LOW;
986 break;
987 case 2: /* full */
988 libusb_speed = LIBUSB_SPEED_FULL;
989 break;
990 case 3: /* high */
991 case 4: /* wireless */
992 libusb_speed = LIBUSB_SPEED_HIGH;
993 break;
994 case 5: /* super */
2980a316
GH
995 libusb_speed = LIBUSB_SPEED_SUPER;
996 break;
202d69a7 997 case 6: /* super plus */
2980a316
GH
998#ifdef HAVE_SUPER_PLUS
999 libusb_speed = LIBUSB_SPEED_SUPER_PLUS;
1000#else
202d69a7 1001 libusb_speed = LIBUSB_SPEED_SUPER;
2980a316 1002#endif
202d69a7
GH
1003 break;
1004 }
1005 }
1006#endif
1007 udev->speed = speed_map[libusb_speed];
b88a3e01 1008 usb_host_speed_compat(s);
2b2325ff
GH
1009
1010 if (s->ddesc.iProduct) {
1011 libusb_get_string_descriptor_ascii(s->dh, s->ddesc.iProduct,
1012 (unsigned char *)udev->product_desc,
1013 sizeof(udev->product_desc));
1014 } else {
1015 snprintf(udev->product_desc, sizeof(udev->product_desc),
1016 "host:%d.%d", bus_num, addr);
1017 }
1018
7d553f27
GA
1019 usb_device_attach(udev, &local_err);
1020 if (local_err) {
565f65d2 1021 error_report_err(local_err);
2b2325ff
GH
1022 goto fail;
1023 }
1024
1025 trace_usb_host_open_success(bus_num, addr);
1026 return 0;
1027
1028fail:
1029 trace_usb_host_open_failure(bus_num, addr);
1030 if (s->dh != NULL) {
6110ce59
LM
1031 usb_host_release_interfaces(s);
1032 libusb_reset_device(s->dh);
1033 usb_host_attach_kernel(s);
2b2325ff
GH
1034 libusb_close(s->dh);
1035 s->dh = NULL;
1036 s->dev = NULL;
1037 }
1038 return -1;
1039}
1040
1041static void usb_host_abort_xfers(USBHostDevice *s)
1042{
1043 USBHostRequest *r, *rtmp;
54cdfe51 1044 int limit = 100;
2b2325ff
GH
1045
1046 QTAILQ_FOREACH_SAFE(r, &s->requests, next, rtmp) {
1047 usb_host_req_abort(r);
1048 }
76d0a936
GH
1049
1050 while (QTAILQ_FIRST(&s->requests) != NULL) {
1051 struct timeval tv;
1052 memset(&tv, 0, sizeof(tv));
1053 tv.tv_usec = 2500;
1054 libusb_handle_events_timeout(ctx, &tv);
54cdfe51
GH
1055 if (--limit == 0) {
1056 /*
1057 * Don't wait forever for libusb calling the complete
1058 * callback (which will unlink and free the request).
1059 *
1060 * Leaking memory here, to make sure libusb will not
1061 * access memory which we have released already.
1062 */
1063 QTAILQ_FOREACH_SAFE(r, &s->requests, next, rtmp) {
1064 QTAILQ_REMOVE(&s->requests, r, next);
1065 }
1066 return;
1067 }
76d0a936 1068 }
2b2325ff
GH
1069}
1070
1071static int usb_host_close(USBHostDevice *s)
1072{
1073 USBDevice *udev = USB_DEVICE(s);
1074
1075 if (s->dh == NULL) {
1076 return -1;
1077 }
1078
1079 trace_usb_host_close(s->bus_num, s->addr);
1080
1081 usb_host_abort_xfers(s);
1082 usb_host_iso_free_all(s);
1083
1084 if (udev->attached) {
1085 usb_device_detach(udev);
1086 }
1087
1088 usb_host_release_interfaces(s);
1089 libusb_reset_device(s->dh);
1090 usb_host_attach_kernel(s);
1091 libusb_close(s->dh);
1092 s->dh = NULL;
1093 s->dev = NULL;
1094
9f815e83
GH
1095 if (s->hostfd != -1) {
1096 close(s->hostfd);
1097 s->hostfd = -1;
1098 }
1099
2b2325ff
GH
1100 usb_host_auto_check(NULL);
1101 return 0;
1102}
1103
1104static void usb_host_nodev_bh(void *opaque)
1105{
1106 USBHostDevice *s = opaque;
1107 usb_host_close(s);
1108}
1109
1110static void usb_host_nodev(USBHostDevice *s)
1111{
95efb20c
GH
1112 if (!s->bh_nodev) {
1113 s->bh_nodev = qemu_bh_new(usb_host_nodev_bh, s);
2b2325ff 1114 }
95efb20c 1115 qemu_bh_schedule(s->bh_nodev);
2b2325ff
GH
1116}
1117
1118static void usb_host_exit_notifier(struct Notifier *n, void *data)
1119{
1120 USBHostDevice *s = container_of(n, USBHostDevice, exit);
1121
1122 if (s->dh) {
76d0a936 1123 usb_host_abort_xfers(s);
2b2325ff 1124 usb_host_release_interfaces(s);
5621d045 1125 libusb_reset_device(s->dh);
2b2325ff 1126 usb_host_attach_kernel(s);
5621d045 1127 libusb_close(s->dh);
2b2325ff
GH
1128 }
1129}
1130
e058fa2d
GH
1131static libusb_device *usb_host_find_ref(int bus, int addr)
1132{
1133 libusb_device **devs = NULL;
1134 libusb_device *ret = NULL;
1135 int i, n;
1136
e058fa2d
GH
1137 n = libusb_get_device_list(ctx, &devs);
1138 for (i = 0; i < n; i++) {
1139 if (libusb_get_bus_number(devs[i]) == bus &&
1140 libusb_get_device_address(devs[i]) == addr) {
1141 ret = libusb_ref_device(devs[i]);
1142 break;
1143 }
1144 }
1145 libusb_free_device_list(devs, 1);
1146 return ret;
1147}
1148
2aa76dc1 1149static void usb_host_realize(USBDevice *udev, Error **errp)
2b2325ff
GH
1150{
1151 USBHostDevice *s = USB_HOST_DEVICE(udev);
e058fa2d
GH
1152 libusb_device *ldev;
1153 int rc;
2b2325ff 1154
9f815e83
GH
1155 if (usb_host_init() != 0) {
1156 error_setg(errp, "failed to init libusb");
1157 return;
1158 }
f3cda6e0 1159 if (s->match.vendor_id > 0xffff) {
2aa76dc1
GA
1160 error_setg(errp, "vendorid out of range");
1161 return;
f3cda6e0
GH
1162 }
1163 if (s->match.product_id > 0xffff) {
2aa76dc1
GA
1164 error_setg(errp, "productid out of range");
1165 return;
f3cda6e0
GH
1166 }
1167 if (s->match.addr > 127) {
2aa76dc1
GA
1168 error_setg(errp, "hostaddr out of range");
1169 return;
f3cda6e0
GH
1170 }
1171
2b2325ff 1172 loglevel = s->loglevel;
628e5485 1173 udev->flags |= (1 << USB_DEV_FLAG_IS_HOST);
2b2325ff
GH
1174 udev->auto_attach = 0;
1175 QTAILQ_INIT(&s->requests);
1176 QTAILQ_INIT(&s->isorings);
9f815e83 1177 s->hostfd = -1;
2b2325ff 1178
631009e7 1179#if LIBUSB_API_VERSION >= 0x01000107 && !defined(CONFIG_WIN32)
9f815e83
GH
1180 if (s->hostdevice) {
1181 int fd;
1182 s->needs_autoscan = false;
448058aa 1183 fd = qemu_open_old(s->hostdevice, O_RDWR);
9f815e83
GH
1184 if (fd < 0) {
1185 error_setg_errno(errp, errno, "failed to open %s", s->hostdevice);
1186 return;
1187 }
1188 rc = usb_host_open(s, NULL, fd);
1189 if (rc < 0) {
1190 error_setg(errp, "failed to open host usb device %s", s->hostdevice);
1191 return;
1192 }
1193 } else
1194#endif
e058fa2d
GH
1195 if (s->match.addr && s->match.bus_num &&
1196 !s->match.vendor_id &&
1197 !s->match.product_id &&
1198 !s->match.port) {
1199 s->needs_autoscan = false;
1200 ldev = usb_host_find_ref(s->match.bus_num,
1201 s->match.addr);
1202 if (!ldev) {
1203 error_setg(errp, "failed to find host usb device %d:%d",
1204 s->match.bus_num, s->match.addr);
1205 return;
1206 }
9f815e83 1207 rc = usb_host_open(s, ldev, 0);
e058fa2d
GH
1208 libusb_unref_device(ldev);
1209 if (rc < 0) {
1210 error_setg(errp, "failed to open host usb device %d:%d",
1211 s->match.bus_num, s->match.addr);
1212 return;
1213 }
1214 } else {
1215 s->needs_autoscan = true;
1216 QTAILQ_INSERT_TAIL(&hostdevs, s, next);
1217 usb_host_auto_check(NULL);
1218 }
1219
2b2325ff
GH
1220 s->exit.notify = usb_host_exit_notifier;
1221 qemu_add_exit_notifier(&s->exit);
2b2325ff
GH
1222}
1223
e6adae52
GA
1224static void usb_host_instance_init(Object *obj)
1225{
1226 USBDevice *udev = USB_DEVICE(obj);
1227 USBHostDevice *s = USB_HOST_DEVICE(udev);
1228
1229 device_add_bootindex_property(obj, &s->bootindex,
1230 "bootindex", NULL,
40c2281c 1231 &udev->qdev);
e6adae52
GA
1232}
1233
b69c3c21 1234static void usb_host_unrealize(USBDevice *udev)
2b2325ff
GH
1235{
1236 USBHostDevice *s = USB_HOST_DEVICE(udev);
1237
1238 qemu_remove_exit_notifier(&s->exit);
e058fa2d
GH
1239 if (s->needs_autoscan) {
1240 QTAILQ_REMOVE(&hostdevs, s, next);
1241 }
2b2325ff
GH
1242 usb_host_close(s);
1243}
1244
1245static void usb_host_cancel_packet(USBDevice *udev, USBPacket *p)
1246{
1247 USBHostDevice *s = USB_HOST_DEVICE(udev);
1248 USBHostRequest *r;
1249
1250 if (p->combined) {
1251 usb_combined_packet_cancel(udev, p);
1252 return;
1253 }
1254
1255 trace_usb_host_req_canceled(s->bus_num, s->addr, p);
1256
1257 r = usb_host_req_find(s, p);
1258 if (r && r->p) {
1259 r->p = NULL; /* mark as dead */
1260 libusb_cancel_transfer(r->xfer);
1261 }
1262}
1263
1264static void usb_host_detach_kernel(USBHostDevice *s)
1265{
1266 struct libusb_config_descriptor *conf;
1267 int rc, i;
1268
1269 rc = libusb_get_active_config_descriptor(s->dev, &conf);
1270 if (rc != 0) {
1271 return;
1272 }
896b6757 1273 for (i = 0; i < USB_MAX_INTERFACES; i++) {
2b2325ff
GH
1274 rc = libusb_kernel_driver_active(s->dh, i);
1275 usb_host_libusb_error("libusb_kernel_driver_active", rc);
1276 if (rc != 1) {
933d2d4b 1277 if (rc == 0) {
1278 s->ifs[i].detached = true;
1279 }
2b2325ff
GH
1280 continue;
1281 }
1282 trace_usb_host_detach_kernel(s->bus_num, s->addr, i);
1283 rc = libusb_detach_kernel_driver(s->dh, i);
1284 usb_host_libusb_error("libusb_detach_kernel_driver", rc);
1285 s->ifs[i].detached = true;
1286 }
1287 libusb_free_config_descriptor(conf);
1288}
1289
1290static void usb_host_attach_kernel(USBHostDevice *s)
1291{
1292 struct libusb_config_descriptor *conf;
1293 int rc, i;
1294
1295 rc = libusb_get_active_config_descriptor(s->dev, &conf);
1296 if (rc != 0) {
1297 return;
1298 }
896b6757 1299 for (i = 0; i < USB_MAX_INTERFACES; i++) {
2b2325ff
GH
1300 if (!s->ifs[i].detached) {
1301 continue;
1302 }
1303 trace_usb_host_attach_kernel(s->bus_num, s->addr, i);
1304 libusb_attach_kernel_driver(s->dh, i);
1305 s->ifs[i].detached = false;
1306 }
1307 libusb_free_config_descriptor(conf);
1308}
1309
1310static int usb_host_claim_interfaces(USBHostDevice *s, int configuration)
1311{
1312 USBDevice *udev = USB_DEVICE(s);
1313 struct libusb_config_descriptor *conf;
896b6757 1314 int rc, i, claimed;
2b2325ff
GH
1315
1316 for (i = 0; i < USB_MAX_INTERFACES; i++) {
1317 udev->altsetting[i] = 0;
1318 }
1319 udev->ninterfaces = 0;
1320 udev->configuration = 0;
1321
2b2325ff
GH
1322 usb_host_detach_kernel(s);
1323
1324 rc = libusb_get_active_config_descriptor(s->dev, &conf);
1325 if (rc != 0) {
1294ca79
HG
1326 if (rc == LIBUSB_ERROR_NOT_FOUND) {
1327 /* address state - ignore */
1328 return USB_RET_SUCCESS;
1329 }
2b2325ff
GH
1330 return USB_RET_STALL;
1331 }
1332
896b6757
SB
1333 claimed = 0;
1334 for (i = 0; i < USB_MAX_INTERFACES; i++) {
2b2325ff
GH
1335 trace_usb_host_claim_interface(s->bus_num, s->addr, configuration, i);
1336 rc = libusb_claim_interface(s->dh, i);
896b6757
SB
1337 if (rc == 0) {
1338 s->ifs[i].claimed = true;
1339 if (++claimed == conf->bNumInterfaces) {
1340 break;
1341 }
2b2325ff 1342 }
896b6757
SB
1343 }
1344 if (claimed != conf->bNumInterfaces) {
1345 return USB_RET_STALL;
2b2325ff
GH
1346 }
1347
1348 udev->ninterfaces = conf->bNumInterfaces;
1349 udev->configuration = configuration;
1350
1351 libusb_free_config_descriptor(conf);
1352 return USB_RET_SUCCESS;
1353}
1354
1355static void usb_host_release_interfaces(USBHostDevice *s)
1356{
2b2325ff
GH
1357 int i, rc;
1358
896b6757 1359 for (i = 0; i < USB_MAX_INTERFACES; i++) {
2b2325ff
GH
1360 if (!s->ifs[i].claimed) {
1361 continue;
1362 }
1363 trace_usb_host_release_interface(s->bus_num, s->addr, i);
1364 rc = libusb_release_interface(s->dh, i);
1365 usb_host_libusb_error("libusb_release_interface", rc);
1366 s->ifs[i].claimed = false;
1367 }
1368}
1369
1370static void usb_host_set_address(USBHostDevice *s, int addr)
1371{
1372 USBDevice *udev = USB_DEVICE(s);
1373
1374 trace_usb_host_set_address(s->bus_num, s->addr, addr);
1375 udev->addr = addr;
1376}
1377
1378static void usb_host_set_config(USBHostDevice *s, int config, USBPacket *p)
1379{
bfe44898 1380 int rc = 0;
2b2325ff
GH
1381
1382 trace_usb_host_set_config(s->bus_num, s->addr, config);
1383
1384 usb_host_release_interfaces(s);
bfe44898
GH
1385 if (s->ddesc.bNumConfigurations != 1) {
1386 rc = libusb_set_configuration(s->dh, config);
1387 if (rc != 0) {
1388 usb_host_libusb_error("libusb_set_configuration", rc);
1389 p->status = USB_RET_STALL;
1390 if (rc == LIBUSB_ERROR_NO_DEVICE) {
1391 usb_host_nodev(s);
1392 }
1393 return;
2b2325ff 1394 }
2b2325ff
GH
1395 }
1396 p->status = usb_host_claim_interfaces(s, config);
1397 if (p->status != USB_RET_SUCCESS) {
1398 return;
1399 }
1400 usb_host_ep_update(s);
1401}
1402
1403static void usb_host_set_interface(USBHostDevice *s, int iface, int alt,
1404 USBPacket *p)
1405{
1406 USBDevice *udev = USB_DEVICE(s);
1407 int rc;
1408
1409 trace_usb_host_set_interface(s->bus_num, s->addr, iface, alt);
1410
1411 usb_host_iso_free_all(s);
1412
1413 if (iface >= USB_MAX_INTERFACES) {
1414 p->status = USB_RET_STALL;
1415 return;
1416 }
1417
1418 rc = libusb_set_interface_alt_setting(s->dh, iface, alt);
1419 if (rc != 0) {
1420 usb_host_libusb_error("libusb_set_interface_alt_setting", rc);
1421 p->status = USB_RET_STALL;
1422 if (rc == LIBUSB_ERROR_NO_DEVICE) {
1423 usb_host_nodev(s);
1424 }
1425 return;
1426 }
1427
1428 udev->altsetting[iface] = alt;
1429 usb_host_ep_update(s);
1430}
1431
1432static void usb_host_handle_control(USBDevice *udev, USBPacket *p,
1433 int request, int value, int index,
1434 int length, uint8_t *data)
1435{
1436 USBHostDevice *s = USB_HOST_DEVICE(udev);
1437 USBHostRequest *r;
1438 int rc;
1439
1440 trace_usb_host_req_control(s->bus_num, s->addr, p, request, value, index);
1441
1442 if (s->dh == NULL) {
1443 p->status = USB_RET_NODEV;
1444 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1445 return;
1446 }
1447
1448 switch (request) {
1449 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
1450 usb_host_set_address(s, value);
1451 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1452 return;
1453
1454 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
1455 usb_host_set_config(s, value & 0xff, p);
1456 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1457 return;
1458
1459 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
1460 usb_host_set_interface(s, index, value, p);
1461 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1462 return;
1463
1464 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
1465 if (value == 0) { /* clear halt */
1466 int pid = (index & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT;
1467 libusb_clear_halt(s->dh, index);
1468 usb_ep_set_halted(udev, pid, index & 0x0f, 0);
1469 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1470 return;
1471 }
1472 }
1473
1474 r = usb_host_req_alloc(s, p, (request >> 8) & USB_DIR_IN, length + 8);
1475 r->cbuf = data;
1476 r->clen = length;
1477 memcpy(r->buffer, udev->setup_buf, 8);
1478 if (!r->in) {
1479 memcpy(r->buffer + 8, r->cbuf, r->clen);
1480 }
1481
b88a3e01
GH
1482 /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
1483 * to work redirected to a not superspeed capable hcd */
a9be4e7c 1484 if ((udev->speedmask & USB_SPEED_MASK_SUPER) &&
72517114 1485 !(udev->port->speedmask & USB_SPEED_MASK_SUPER) &&
b88a3e01
GH
1486 request == 0x8006 && value == 0x100 && index == 0) {
1487 r->usb3ep0quirk = true;
1488 }
1489
2b2325ff
GH
1490 libusb_fill_control_transfer(r->xfer, s->dh, r->buffer,
1491 usb_host_req_complete_ctrl, r,
1492 CONTROL_TIMEOUT);
1493 rc = libusb_submit_transfer(r->xfer);
1494 if (rc != 0) {
1495 p->status = USB_RET_NODEV;
1496 trace_usb_host_req_complete(s->bus_num, s->addr, p,
1497 p->status, p->actual_length);
1498 if (rc == LIBUSB_ERROR_NO_DEVICE) {
1499 usb_host_nodev(s);
1500 }
1501 return;
1502 }
1503
1504 p->status = USB_RET_ASYNC;
1505}
1506
1507static void usb_host_handle_data(USBDevice *udev, USBPacket *p)
1508{
1509 USBHostDevice *s = USB_HOST_DEVICE(udev);
1510 USBHostRequest *r;
1511 size_t size;
1512 int ep, rc;
1513
1514 if (usb_host_use_combining(p->ep) && p->state == USB_PACKET_SETUP) {
1515 p->status = USB_RET_ADD_TO_QUEUE;
1516 return;
1517 }
1518
1519 trace_usb_host_req_data(s->bus_num, s->addr, p,
1520 p->pid == USB_TOKEN_IN,
1521 p->ep->nr, p->iov.size);
1522
1523 if (s->dh == NULL) {
1524 p->status = USB_RET_NODEV;
1525 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1526 return;
1527 }
1528 if (p->ep->halted) {
1529 p->status = USB_RET_STALL;
1530 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1531 return;
1532 }
1533
1534 switch (usb_ep_get_type(udev, p->pid, p->ep->nr)) {
1535 case USB_ENDPOINT_XFER_BULK:
1536 size = usb_packet_size(p);
1537 r = usb_host_req_alloc(s, p, p->pid == USB_TOKEN_IN, size);
1538 if (!r->in) {
1539 usb_packet_copy(p, r->buffer, size);
1540 }
1541 ep = p->ep->nr | (r->in ? USB_DIR_IN : 0);
8d1bd3c9 1542 if (p->stream) {
322fd1f4 1543#ifdef HAVE_STREAMS
8d1bd3c9
HG
1544 libusb_fill_bulk_stream_transfer(r->xfer, s->dh, ep, p->stream,
1545 r->buffer, size,
1546 usb_host_req_complete_data, r,
1547 BULK_TIMEOUT);
1548#else
1549 usb_host_req_free(r);
1550 p->status = USB_RET_STALL;
1551 return;
1552#endif
1553 } else {
1554 libusb_fill_bulk_transfer(r->xfer, s->dh, ep,
1555 r->buffer, size,
1556 usb_host_req_complete_data, r,
1557 BULK_TIMEOUT);
1558 }
2b2325ff
GH
1559 break;
1560 case USB_ENDPOINT_XFER_INT:
1561 r = usb_host_req_alloc(s, p, p->pid == USB_TOKEN_IN, p->iov.size);
1562 if (!r->in) {
1563 usb_packet_copy(p, r->buffer, p->iov.size);
1564 }
1565 ep = p->ep->nr | (r->in ? USB_DIR_IN : 0);
1566 libusb_fill_interrupt_transfer(r->xfer, s->dh, ep,
1567 r->buffer, p->iov.size,
1568 usb_host_req_complete_data, r,
1569 INTR_TIMEOUT);
1570 break;
1571 case USB_ENDPOINT_XFER_ISOC:
1572 if (p->pid == USB_TOKEN_IN) {
1573 usb_host_iso_data_in(s, p);
1574 } else {
1575 usb_host_iso_data_out(s, p);
1576 }
1577 trace_usb_host_req_complete(s->bus_num, s->addr, p,
1578 p->status, p->actual_length);
1579 return;
1580 default:
1581 p->status = USB_RET_STALL;
1582 trace_usb_host_req_complete(s->bus_num, s->addr, p,
1583 p->status, p->actual_length);
1584 return;
1585 }
1586
1587 rc = libusb_submit_transfer(r->xfer);
1588 if (rc != 0) {
1589 p->status = USB_RET_NODEV;
1590 trace_usb_host_req_complete(s->bus_num, s->addr, p,
1591 p->status, p->actual_length);
1592 if (rc == LIBUSB_ERROR_NO_DEVICE) {
1593 usb_host_nodev(s);
1594 }
1595 return;
1596 }
1597
1598 p->status = USB_RET_ASYNC;
1599}
1600
1601static void usb_host_flush_ep_queue(USBDevice *dev, USBEndpoint *ep)
1602{
1603 if (usb_host_use_combining(ep)) {
1604 usb_ep_combine_input_packets(ep);
1605 }
1606}
1607
1608static void usb_host_handle_reset(USBDevice *udev)
1609{
1610 USBHostDevice *s = USB_HOST_DEVICE(udev);
5af35d7f 1611 int rc;
2b2325ff 1612
1dfe2b91 1613 if (!s->allow_one_guest_reset && !s->allow_all_guest_resets) {
ba4c735b
AK
1614 return;
1615 }
1dfe2b91 1616 if (!s->allow_all_guest_resets && udev->addr == 0) {
65f14ab9
GH
1617 return;
1618 }
ba4c735b 1619
2b2325ff
GH
1620 trace_usb_host_reset(s->bus_num, s->addr);
1621
5af35d7f
HG
1622 rc = libusb_reset_device(s->dh);
1623 if (rc != 0) {
1624 usb_host_nodev(s);
2b2325ff 1625 }
2b2325ff
GH
1626}
1627
56a9f180
HG
1628static int usb_host_alloc_streams(USBDevice *udev, USBEndpoint **eps,
1629 int nr_eps, int streams)
1630{
322fd1f4 1631#ifdef HAVE_STREAMS
56a9f180
HG
1632 USBHostDevice *s = USB_HOST_DEVICE(udev);
1633 unsigned char endpoints[30];
1634 int i, rc;
1635
1636 for (i = 0; i < nr_eps; i++) {
1637 endpoints[i] = eps[i]->nr;
1638 if (eps[i]->pid == USB_TOKEN_IN) {
1639 endpoints[i] |= 0x80;
1640 }
1641 }
1642 rc = libusb_alloc_streams(s->dh, streams, endpoints, nr_eps);
1643 if (rc < 0) {
1644 usb_host_libusb_error("libusb_alloc_streams", rc);
1645 } else if (rc != streams) {
2e6a0dd1
GA
1646 error_report("libusb_alloc_streams: got less streams "
1647 "then requested %d < %d", rc, streams);
56a9f180
HG
1648 }
1649
1650 return (rc == streams) ? 0 : -1;
1651#else
2e6a0dd1 1652 error_report("libusb_alloc_streams: error not implemented");
56a9f180
HG
1653 return -1;
1654#endif
1655}
1656
1657static void usb_host_free_streams(USBDevice *udev, USBEndpoint **eps,
1658 int nr_eps)
1659{
322fd1f4 1660#ifdef HAVE_STREAMS
56a9f180
HG
1661 USBHostDevice *s = USB_HOST_DEVICE(udev);
1662 unsigned char endpoints[30];
1663 int i;
1664
1665 for (i = 0; i < nr_eps; i++) {
1666 endpoints[i] = eps[i]->nr;
1667 if (eps[i]->pid == USB_TOKEN_IN) {
1668 endpoints[i] |= 0x80;
1669 }
1670 }
1671 libusb_free_streams(s->dh, endpoints, nr_eps);
1672#endif
1673}
1674
95efb20c
GH
1675/*
1676 * This is *NOT* about restoring state. We have absolutely no idea
1677 * what state the host device is in at the moment and whenever it is
1678 * still present in the first place. Attemping to contine where we
1679 * left off is impossible.
1680 *
b6af0975 1681 * What we are going to do here is emulate a surprise removal of
95efb20c
GH
1682 * the usb device passed through, then kick host scan so the device
1683 * will get re-attached (and re-initialized by the guest) in case it
1684 * is still present.
1685 *
1686 * As the device removal will change the state of other devices (usb
1687 * host controller, most likely interrupt controller too) we have to
1688 * wait with it until *all* vmstate is loaded. Thus post_load just
1689 * kicks a bottom half which then does the actual work.
1690 */
1691static void usb_host_post_load_bh(void *opaque)
1692{
1693 USBHostDevice *dev = opaque;
1694 USBDevice *udev = USB_DEVICE(dev);
1695
1696 if (dev->dh != NULL) {
1697 usb_host_close(dev);
1698 }
1699 if (udev->attached) {
1700 usb_device_detach(udev);
1701 }
3280ea8e 1702 dev->bh_postld_pending = false;
95efb20c
GH
1703 usb_host_auto_check(NULL);
1704}
1705
1706static int usb_host_post_load(void *opaque, int version_id)
1707{
1708 USBHostDevice *dev = opaque;
1709
1710 if (!dev->bh_postld) {
1711 dev->bh_postld = qemu_bh_new(usb_host_post_load_bh, dev);
1712 }
1713 qemu_bh_schedule(dev->bh_postld);
3280ea8e 1714 dev->bh_postld_pending = true;
95efb20c
GH
1715 return 0;
1716}
1717
2b2325ff
GH
1718static const VMStateDescription vmstate_usb_host = {
1719 .name = "usb-host",
95efb20c
GH
1720 .version_id = 1,
1721 .minimum_version_id = 1,
1722 .post_load = usb_host_post_load,
2b2325ff
GH
1723 .fields = (VMStateField[]) {
1724 VMSTATE_USB_DEVICE(parent_obj, USBHostDevice),
1725 VMSTATE_END_OF_LIST()
1726 }
1727};
1728
1729static Property usb_host_dev_properties[] = {
1730 DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0),
1731 DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0),
1732 DEFINE_PROP_STRING("hostport", USBHostDevice, match.port),
c7bcc85d
PB
1733 DEFINE_PROP_UINT32("vendorid", USBHostDevice, match.vendor_id, 0),
1734 DEFINE_PROP_UINT32("productid", USBHostDevice, match.product_id, 0),
9f815e83
GH
1735#if LIBUSB_API_VERSION >= 0x01000107
1736 DEFINE_PROP_STRING("hostdevice", USBHostDevice, hostdevice),
1737#endif
2b2325ff
GH
1738 DEFINE_PROP_UINT32("isobufs", USBHostDevice, iso_urb_count, 4),
1739 DEFINE_PROP_UINT32("isobsize", USBHostDevice, iso_urb_frames, 32),
1dfe2b91
GH
1740 DEFINE_PROP_BOOL("guest-reset", USBHostDevice,
1741 allow_one_guest_reset, true),
1742 DEFINE_PROP_BOOL("guest-resets-all", USBHostDevice,
1743 allow_all_guest_resets, false),
2b2325ff
GH
1744 DEFINE_PROP_UINT32("loglevel", USBHostDevice, loglevel,
1745 LIBUSB_LOG_LEVEL_WARNING),
1746 DEFINE_PROP_BIT("pipeline", USBHostDevice, options,
1747 USB_HOST_OPT_PIPELINE, true),
7bacaf5f
YB
1748 DEFINE_PROP_BOOL("suppress-remote-wake", USBHostDevice,
1749 suppress_remote_wake, true),
2b2325ff
GH
1750 DEFINE_PROP_END_OF_LIST(),
1751};
1752
1753static void usb_host_class_initfn(ObjectClass *klass, void *data)
1754{
1755 DeviceClass *dc = DEVICE_CLASS(klass);
1756 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
1757
2aa76dc1 1758 uc->realize = usb_host_realize;
2b2325ff
GH
1759 uc->product_desc = "USB Host Device";
1760 uc->cancel_packet = usb_host_cancel_packet;
1761 uc->handle_data = usb_host_handle_data;
1762 uc->handle_control = usb_host_handle_control;
1763 uc->handle_reset = usb_host_handle_reset;
c4fe9700 1764 uc->unrealize = usb_host_unrealize;
2b2325ff 1765 uc->flush_ep_queue = usb_host_flush_ep_queue;
56a9f180
HG
1766 uc->alloc_streams = usb_host_alloc_streams;
1767 uc->free_streams = usb_host_free_streams;
2b2325ff 1768 dc->vmsd = &vmstate_usb_host;
4f67d30b 1769 device_class_set_props(dc, usb_host_dev_properties);
125ee0ed 1770 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
2b2325ff
GH
1771}
1772
1773static TypeInfo usb_host_dev_info = {
1774 .name = TYPE_USB_HOST_DEVICE,
1775 .parent = TYPE_USB_DEVICE,
1776 .instance_size = sizeof(USBHostDevice),
1777 .class_init = usb_host_class_initfn,
e6adae52 1778 .instance_init = usb_host_instance_init,
2b2325ff
GH
1779};
1780
1781static void usb_host_register_types(void)
1782{
1783 type_register_static(&usb_host_dev_info);
f1a74bf9 1784 monitor_register_hmp("usbhost", true, hmp_info_usbhost);
2b2325ff
GH
1785}
1786
1787type_init(usb_host_register_types)
1788
1789/* ------------------------------------------------------------------------ */
1790
1791static QEMUTimer *usb_auto_timer;
1792static VMChangeStateEntry *usb_vmstate;
1793
538f0497 1794static void usb_host_vm_state(void *unused, bool running, RunState state)
2b2325ff
GH
1795{
1796 if (running) {
1797 usb_host_auto_check(unused);
1798 }
1799}
1800
1801static void usb_host_auto_check(void *unused)
1802{
1803 struct USBHostDevice *s;
1804 struct USBAutoFilter *f;
3ce21445 1805 libusb_device **devs = NULL;
2b2325ff
GH
1806 struct libusb_device_descriptor ddesc;
1807 int unconnected = 0;
1808 int i, n;
1809
1810 if (usb_host_init() != 0) {
1811 return;
1812 }
1813
1814 if (runstate_is_running()) {
1815 n = libusb_get_device_list(ctx, &devs);
1816 for (i = 0; i < n; i++) {
1817 if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) {
1818 continue;
1819 }
1820 if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) {
1821 continue;
1822 }
1823 QTAILQ_FOREACH(s, &hostdevs, next) {
1824 f = &s->match;
1825 if (f->bus_num > 0 &&
1826 f->bus_num != libusb_get_bus_number(devs[i])) {
1827 continue;
1828 }
1829 if (f->addr > 0 &&
1830 f->addr != libusb_get_device_address(devs[i])) {
1831 continue;
1832 }
1833 if (f->port != NULL) {
1834 char port[16] = "-";
1835 usb_host_get_port(devs[i], port, sizeof(port));
1836 if (strcmp(f->port, port) != 0) {
1837 continue;
1838 }
1839 }
1840 if (f->vendor_id > 0 &&
1841 f->vendor_id != ddesc.idVendor) {
1842 continue;
1843 }
1844 if (f->product_id > 0 &&
1845 f->product_id != ddesc.idProduct) {
1846 continue;
1847 }
1848
1849 /* We got a match */
1850 s->seen++;
1851 if (s->errcount >= 3) {
1852 continue;
1853 }
1854 if (s->dh != NULL) {
1855 continue;
1856 }
9f815e83 1857 if (usb_host_open(s, devs[i], 0) < 0) {
2b2325ff
GH
1858 s->errcount++;
1859 continue;
1860 }
1861 break;
1862 }
1863 }
1864 libusb_free_device_list(devs, 1);
1865
1866 QTAILQ_FOREACH(s, &hostdevs, next) {
1867 if (s->dh == NULL) {
1868 unconnected++;
1869 }
1870 if (s->seen == 0) {
1871 if (s->dh) {
1872 usb_host_close(s);
1873 }
1874 s->errcount = 0;
1875 }
1876 s->seen = 0;
1877 }
1878
1879#if 0
1880 if (unconnected == 0) {
1881 /* nothing to watch */
1882 if (usb_auto_timer) {
bc72ad67 1883 timer_del(usb_auto_timer);
2b2325ff
GH
1884 trace_usb_host_auto_scan_disabled();
1885 }
1886 return;
1887 }
1888#endif
1889 }
1890
1891 if (!usb_vmstate) {
1892 usb_vmstate = qemu_add_vm_change_state_handler(usb_host_vm_state, NULL);
1893 }
1894 if (!usb_auto_timer) {
bc72ad67 1895 usb_auto_timer = timer_new_ms(QEMU_CLOCK_REALTIME, usb_host_auto_check, NULL);
2b2325ff
GH
1896 if (!usb_auto_timer) {
1897 return;
1898 }
1899 trace_usb_host_auto_scan_enabled();
1900 }
bc72ad67 1901 timer_mod(usb_auto_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 2000);
2b2325ff
GH
1902}
1903
1ce6be24 1904void hmp_info_usbhost(Monitor *mon, const QDict *qdict)
2b2325ff 1905{
3ce21445 1906 libusb_device **devs = NULL;
2b2325ff
GH
1907 struct libusb_device_descriptor ddesc;
1908 char port[16];
1909 int i, n;
1910
1911 if (usb_host_init() != 0) {
1912 return;
1913 }
1914
1915 n = libusb_get_device_list(ctx, &devs);
1916 for (i = 0; i < n; i++) {
1917 if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) {
1918 continue;
1919 }
1920 if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) {
1921 continue;
1922 }
1923 usb_host_get_port(devs[i], port, sizeof(port));
1924 monitor_printf(mon, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1925 libusb_get_bus_number(devs[i]),
1926 libusb_get_device_address(devs[i]),
1927 port,
1928 speed_name[libusb_get_device_speed(devs[i])]);
1929 monitor_printf(mon, " Class %02x:", ddesc.bDeviceClass);
1930 monitor_printf(mon, " USB device %04x:%04x",
1931 ddesc.idVendor, ddesc.idProduct);
1932 if (ddesc.iProduct) {
1933 libusb_device_handle *handle;
1934 if (libusb_open(devs[i], &handle) == 0) {
1935 unsigned char name[64] = "";
1936 libusb_get_string_descriptor_ascii(handle,
1937 ddesc.iProduct,
1938 name, sizeof(name));
1939 libusb_close(handle);
1940 monitor_printf(mon, ", %s", name);
1941 }
1942 }
1943 monitor_printf(mon, "\n");
1944 }
1945 libusb_free_device_list(devs, 1);
1946}