]> git.proxmox.com Git - mirror_qemu.git/blame - hw/usb/host-libusb.c
usb-host: live migration support for the libusb version
[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 */
95efb20c
GH
97 QEMUBH *bh_nodev;
98 QEMUBH *bh_postld;
2b2325ff
GH
99 Notifier exit;
100
101 /* request queues */
102 QTAILQ_HEAD(, USBHostRequest) requests;
103 QTAILQ_HEAD(, USBHostIsoRing) isorings;
104};
105
106struct USBHostRequest {
107 USBHostDevice *host;
108 USBPacket *p;
109 bool in;
110 struct libusb_transfer *xfer;
111 unsigned char *buffer;
112 unsigned char *cbuf;
113 unsigned int clen;
114 QTAILQ_ENTRY(USBHostRequest) next;
115};
116
117struct USBHostIsoXfer {
118 USBHostIsoRing *ring;
119 struct libusb_transfer *xfer;
120 bool copy_complete;
121 unsigned int packet;
122 QTAILQ_ENTRY(USBHostIsoXfer) next;
123};
124
125struct USBHostIsoRing {
126 USBHostDevice *host;
127 USBEndpoint *ep;
128 QTAILQ_HEAD(, USBHostIsoXfer) unused;
129 QTAILQ_HEAD(, USBHostIsoXfer) inflight;
130 QTAILQ_HEAD(, USBHostIsoXfer) copy;
131 QTAILQ_ENTRY(USBHostIsoRing) next;
132};
133
134static QTAILQ_HEAD(, USBHostDevice) hostdevs =
135 QTAILQ_HEAD_INITIALIZER(hostdevs);
136
137static void usb_host_auto_check(void *unused);
138static void usb_host_release_interfaces(USBHostDevice *s);
139static void usb_host_nodev(USBHostDevice *s);
140static void usb_host_attach_kernel(USBHostDevice *s);
141
142/* ------------------------------------------------------------------------ */
143
144#define CONTROL_TIMEOUT 10000 /* 10 sec */
145#define BULK_TIMEOUT 0 /* unlimited */
146#define INTR_TIMEOUT 0 /* unlimited */
147
148static const char *speed_name[] = {
149 [LIBUSB_SPEED_UNKNOWN] = "?",
150 [LIBUSB_SPEED_LOW] = "1.5",
151 [LIBUSB_SPEED_FULL] = "12",
152 [LIBUSB_SPEED_HIGH] = "480",
153 [LIBUSB_SPEED_SUPER] = "5000",
154};
155
156static const unsigned int speed_map[] = {
157 [LIBUSB_SPEED_LOW] = USB_SPEED_LOW,
158 [LIBUSB_SPEED_FULL] = USB_SPEED_FULL,
159 [LIBUSB_SPEED_HIGH] = USB_SPEED_HIGH,
160 [LIBUSB_SPEED_SUPER] = USB_SPEED_SUPER,
161};
162
163static const unsigned int status_map[] = {
164 [LIBUSB_TRANSFER_COMPLETED] = USB_RET_SUCCESS,
165 [LIBUSB_TRANSFER_ERROR] = USB_RET_IOERROR,
166 [LIBUSB_TRANSFER_TIMED_OUT] = USB_RET_IOERROR,
167 [LIBUSB_TRANSFER_CANCELLED] = USB_RET_IOERROR,
168 [LIBUSB_TRANSFER_STALL] = USB_RET_STALL,
169 [LIBUSB_TRANSFER_NO_DEVICE] = USB_RET_NODEV,
170 [LIBUSB_TRANSFER_OVERFLOW] = USB_RET_BABBLE,
171};
172
173static const char *err_names[] = {
174 [-LIBUSB_ERROR_IO] = "IO",
175 [-LIBUSB_ERROR_INVALID_PARAM] = "INVALID_PARAM",
176 [-LIBUSB_ERROR_ACCESS] = "ACCESS",
177 [-LIBUSB_ERROR_NO_DEVICE] = "NO_DEVICE",
178 [-LIBUSB_ERROR_NOT_FOUND] = "NOT_FOUND",
179 [-LIBUSB_ERROR_BUSY] = "BUSY",
180 [-LIBUSB_ERROR_TIMEOUT] = "TIMEOUT",
181 [-LIBUSB_ERROR_OVERFLOW] = "OVERFLOW",
182 [-LIBUSB_ERROR_PIPE] = "PIPE",
183 [-LIBUSB_ERROR_INTERRUPTED] = "INTERRUPTED",
184 [-LIBUSB_ERROR_NO_MEM] = "NO_MEM",
185 [-LIBUSB_ERROR_NOT_SUPPORTED] = "NOT_SUPPORTED",
186 [-LIBUSB_ERROR_OTHER] = "OTHER",
187};
188
189static libusb_context *ctx;
190static uint32_t loglevel;
191
192static void usb_host_handle_fd(void *opaque)
193{
194 struct timeval tv = { 0, 0 };
195 libusb_handle_events_timeout(ctx, &tv);
196}
197
198static void usb_host_add_fd(int fd, short events, void *user_data)
199{
200 qemu_set_fd_handler(fd,
201 (events & POLLIN) ? usb_host_handle_fd : NULL,
202 (events & POLLOUT) ? usb_host_handle_fd : NULL,
203 ctx);
204}
205
206static void usb_host_del_fd(int fd, void *user_data)
207{
208 qemu_set_fd_handler(fd, NULL, NULL, NULL);
209}
210
211static int usb_host_init(void)
212{
213 const struct libusb_pollfd **poll;
214 int i, rc;
215
216 if (ctx) {
217 return 0;
218 }
219 rc = libusb_init(&ctx);
220 if (rc != 0) {
221 return -1;
222 }
223 libusb_set_debug(ctx, loglevel);
224
225 libusb_set_pollfd_notifiers(ctx, usb_host_add_fd,
226 usb_host_del_fd,
227 ctx);
228 poll = libusb_get_pollfds(ctx);
229 if (poll) {
230 for (i = 0; poll[i] != NULL; i++) {
231 usb_host_add_fd(poll[i]->fd, poll[i]->events, ctx);
232 }
233 }
234 free(poll);
235 return 0;
236}
237
238static int usb_host_get_port(libusb_device *dev, char *port, size_t len)
239{
2b2325ff
GH
240 uint8_t path[7];
241 size_t off;
242 int rc, i;
243
244 rc = libusb_get_port_path(ctx, dev, path, 7);
245 if (rc < 0) {
246 return 0;
247 }
248 off = snprintf(port, len, "%d", path[0]);
249 for (i = 1; i < rc; i++) {
250 off += snprintf(port+off, len-off, ".%d", path[i]);
251 }
252 return off;
2b2325ff
GH
253}
254
255static void usb_host_libusb_error(const char *func, int rc)
256{
257 const char *errname;
258
259 if (rc >= 0) {
260 return;
261 }
262
263 if (-rc < ARRAY_SIZE(err_names) && err_names[-rc]) {
264 errname = err_names[-rc];
265 } else {
266 errname = "?";
267 }
268 fprintf(stderr, "%s: %d [%s]\n", func, rc, errname);
269}
270
271/* ------------------------------------------------------------------------ */
272
273static bool usb_host_use_combining(USBEndpoint *ep)
274{
275 int type;
276
277 if (!ep->pipeline) {
278 return false;
279 }
280 if (ep->pid != USB_TOKEN_IN) {
281 return false;
282 }
283 type = usb_ep_get_type(ep->dev, ep->pid, ep->nr);
284 if (type != USB_ENDPOINT_XFER_BULK) {
285 return false;
286 }
287 return true;
288}
289
290/* ------------------------------------------------------------------------ */
291
292static USBHostRequest *usb_host_req_alloc(USBHostDevice *s, USBPacket *p,
293 bool in, size_t bufsize)
294{
295 USBHostRequest *r = g_new0(USBHostRequest, 1);
296
297 r->host = s;
298 r->p = p;
299 r->in = in;
300 r->xfer = libusb_alloc_transfer(0);
301 if (bufsize) {
302 r->buffer = g_malloc(bufsize);
303 }
304 QTAILQ_INSERT_TAIL(&s->requests, r, next);
305 return r;
306}
307
308static void usb_host_req_free(USBHostRequest *r)
309{
310 if (r->host) {
311 QTAILQ_REMOVE(&r->host->requests, r, next);
312 }
313 libusb_free_transfer(r->xfer);
314 g_free(r->buffer);
315 g_free(r);
316}
317
318static USBHostRequest *usb_host_req_find(USBHostDevice *s, USBPacket *p)
319{
320 USBHostRequest *r;
321
322 QTAILQ_FOREACH(r, &s->requests, next) {
323 if (r->p == p) {
324 return r;
325 }
326 }
327 return NULL;
328}
329
330static void usb_host_req_complete_ctrl(struct libusb_transfer *xfer)
331{
332 USBHostRequest *r = xfer->user_data;
333 USBHostDevice *s = r->host;
334 bool disconnect = (xfer->status == LIBUSB_TRANSFER_NO_DEVICE);
335
336 if (r->p == NULL) {
337 goto out; /* request was canceled */
338 }
339
340 r->p->status = status_map[xfer->status];
341 r->p->actual_length = xfer->actual_length;
342 if (r->in && xfer->actual_length) {
343 memcpy(r->cbuf, r->buffer + 8, xfer->actual_length);
344 }
345 trace_usb_host_req_complete(s->bus_num, s->addr, r->p,
346 r->p->status, r->p->actual_length);
347 usb_generic_async_ctrl_complete(USB_DEVICE(s), r->p);
348
349out:
350 usb_host_req_free(r);
351 if (disconnect) {
352 usb_host_nodev(s);
353 }
354}
355
356static void usb_host_req_complete_data(struct libusb_transfer *xfer)
357{
358 USBHostRequest *r = xfer->user_data;
359 USBHostDevice *s = r->host;
360 bool disconnect = (xfer->status == LIBUSB_TRANSFER_NO_DEVICE);
361
362 if (r->p == NULL) {
363 goto out; /* request was canceled */
364 }
365
366 r->p->status = status_map[xfer->status];
367 if (r->in && xfer->actual_length) {
368 usb_packet_copy(r->p, r->buffer, xfer->actual_length);
369 }
370 trace_usb_host_req_complete(s->bus_num, s->addr, r->p,
371 r->p->status, r->p->actual_length);
372 if (usb_host_use_combining(r->p->ep)) {
373 usb_combined_input_packet_complete(USB_DEVICE(s), r->p);
374 } else {
375 usb_packet_complete(USB_DEVICE(s), r->p);
376 }
377
378out:
379 usb_host_req_free(r);
380 if (disconnect) {
381 usb_host_nodev(s);
382 }
383}
384
385static void usb_host_req_abort(USBHostRequest *r)
386{
387 USBHostDevice *s = r->host;
388 bool inflight = (r->p && r->p->state == USB_RET_ASYNC);
389
390 if (inflight) {
391 r->p->status = USB_RET_NODEV;
392 trace_usb_host_req_complete(s->bus_num, s->addr, r->p,
393 r->p->status, r->p->actual_length);
394 if (r->p->ep->nr == 0) {
395 usb_generic_async_ctrl_complete(USB_DEVICE(s), r->p);
396 } else {
397 usb_packet_complete(USB_DEVICE(s), r->p);
398 }
399 r->p = NULL;
400 }
401
402 QTAILQ_REMOVE(&r->host->requests, r, next);
403 r->host = NULL;
404
405 if (inflight) {
406 libusb_cancel_transfer(r->xfer);
407 }
408}
409
410/* ------------------------------------------------------------------------ */
411
412static void usb_host_req_complete_iso(struct libusb_transfer *transfer)
413{
414 USBHostIsoXfer *xfer = transfer->user_data;
415
416 if (!xfer) {
417 /* USBHostIsoXfer released while inflight */
418 g_free(transfer->buffer);
419 libusb_free_transfer(transfer);
420 return;
421 }
422
423 QTAILQ_REMOVE(&xfer->ring->inflight, xfer, next);
424 if (QTAILQ_EMPTY(&xfer->ring->inflight)) {
425 USBHostDevice *s = xfer->ring->host;
426 trace_usb_host_iso_stop(s->bus_num, s->addr, xfer->ring->ep->nr);
427 }
428 if (xfer->ring->ep->pid == USB_TOKEN_IN) {
429 QTAILQ_INSERT_TAIL(&xfer->ring->copy, xfer, next);
430 } else {
431 QTAILQ_INSERT_TAIL(&xfer->ring->unused, xfer, next);
432 }
433}
434
435static USBHostIsoRing *usb_host_iso_alloc(USBHostDevice *s, USBEndpoint *ep)
436{
437 USBHostIsoRing *ring = g_new0(USBHostIsoRing, 1);
438 USBHostIsoXfer *xfer;
439 /* FIXME: check interval (for now assume one xfer per frame) */
440 int packets = s->iso_urb_frames;
441 int i;
442
443 ring->host = s;
444 ring->ep = ep;
445 QTAILQ_INIT(&ring->unused);
446 QTAILQ_INIT(&ring->inflight);
447 QTAILQ_INIT(&ring->copy);
448 QTAILQ_INSERT_TAIL(&s->isorings, ring, next);
449
450 for (i = 0; i < s->iso_urb_count; i++) {
451 xfer = g_new0(USBHostIsoXfer, 1);
452 xfer->ring = ring;
453 xfer->xfer = libusb_alloc_transfer(packets);
454 xfer->xfer->dev_handle = s->dh;
455 xfer->xfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
456
457 xfer->xfer->endpoint = ring->ep->nr;
458 if (ring->ep->pid == USB_TOKEN_IN) {
459 xfer->xfer->endpoint |= USB_DIR_IN;
460 }
461 xfer->xfer->callback = usb_host_req_complete_iso;
462 xfer->xfer->user_data = xfer;
463
464 xfer->xfer->num_iso_packets = packets;
465 xfer->xfer->length = ring->ep->max_packet_size * packets;
466 xfer->xfer->buffer = g_malloc0(xfer->xfer->length);
467
468 QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
469 }
470
471 return ring;
472}
473
474static USBHostIsoRing *usb_host_iso_find(USBHostDevice *s, USBEndpoint *ep)
475{
476 USBHostIsoRing *ring;
477
478 QTAILQ_FOREACH(ring, &s->isorings, next) {
479 if (ring->ep == ep) {
480 return ring;
481 }
482 }
483 return NULL;
484}
485
486static void usb_host_iso_reset_xfer(USBHostIsoXfer *xfer)
487{
488 libusb_set_iso_packet_lengths(xfer->xfer,
489 xfer->ring->ep->max_packet_size);
490 xfer->packet = 0;
491 xfer->copy_complete = false;
492}
493
494static void usb_host_iso_free_xfer(USBHostIsoXfer *xfer, bool inflight)
495{
496 if (inflight) {
497 xfer->xfer->user_data = NULL;
498 } else {
499 g_free(xfer->xfer->buffer);
500 libusb_free_transfer(xfer->xfer);
501 }
502 g_free(xfer);
503}
504
505static void usb_host_iso_free(USBHostIsoRing *ring)
506{
507 USBHostIsoXfer *xfer;
508
509 while ((xfer = QTAILQ_FIRST(&ring->inflight)) != NULL) {
510 QTAILQ_REMOVE(&ring->inflight, xfer, next);
511 usb_host_iso_free_xfer(xfer, true);
512 }
513 while ((xfer = QTAILQ_FIRST(&ring->unused)) != NULL) {
514 QTAILQ_REMOVE(&ring->unused, xfer, next);
515 usb_host_iso_free_xfer(xfer, false);
516 }
517 while ((xfer = QTAILQ_FIRST(&ring->copy)) != NULL) {
518 QTAILQ_REMOVE(&ring->copy, xfer, next);
519 usb_host_iso_free_xfer(xfer, false);
520 }
521
522 QTAILQ_REMOVE(&ring->host->isorings, ring, next);
523 g_free(ring);
524}
525
526static void usb_host_iso_free_all(USBHostDevice *s)
527{
528 USBHostIsoRing *ring;
529
530 while ((ring = QTAILQ_FIRST(&s->isorings)) != NULL) {
531 usb_host_iso_free(ring);
532 }
533}
534
535static bool usb_host_iso_data_copy(USBHostIsoXfer *xfer, USBPacket *p)
536{
537 unsigned int psize;
538 unsigned char *buf;
539
540 buf = libusb_get_iso_packet_buffer_simple(xfer->xfer, xfer->packet);
541 if (p->pid == USB_TOKEN_OUT) {
542 psize = p->iov.size;
543 if (psize > xfer->ring->ep->max_packet_size) {
544 /* should not happen (guest bug) */
545 psize = xfer->ring->ep->max_packet_size;
546 }
547 xfer->xfer->iso_packet_desc[xfer->packet].length = psize;
548 } else {
549 psize = xfer->xfer->iso_packet_desc[xfer->packet].actual_length;
550 if (psize > p->iov.size) {
551 /* should not happen (guest bug) */
552 psize = p->iov.size;
553 }
554 }
555 usb_packet_copy(p, buf, psize);
556 xfer->packet++;
557 xfer->copy_complete = (xfer->packet == xfer->xfer->num_iso_packets);
558 return xfer->copy_complete;
559}
560
561static void usb_host_iso_data_in(USBHostDevice *s, USBPacket *p)
562{
563 USBHostIsoRing *ring;
564 USBHostIsoXfer *xfer;
565 bool disconnect = false;
566 int rc;
567
568 ring = usb_host_iso_find(s, p->ep);
569 if (ring == NULL) {
570 ring = usb_host_iso_alloc(s, p->ep);
571 }
572
573 /* copy data to guest */
574 xfer = QTAILQ_FIRST(&ring->copy);
575 if (xfer != NULL) {
576 if (usb_host_iso_data_copy(xfer, p)) {
577 QTAILQ_REMOVE(&ring->copy, xfer, next);
578 QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
579 }
580 }
581
582 /* submit empty bufs to host */
583 while ((xfer = QTAILQ_FIRST(&ring->unused)) != NULL) {
584 QTAILQ_REMOVE(&ring->unused, xfer, next);
585 usb_host_iso_reset_xfer(xfer);
586 rc = libusb_submit_transfer(xfer->xfer);
587 if (rc != 0) {
588 usb_host_libusb_error("libusb_submit_transfer [iso]", rc);
589 QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
590 if (rc == LIBUSB_ERROR_NO_DEVICE) {
591 disconnect = true;
592 }
593 break;
594 }
595 if (QTAILQ_EMPTY(&ring->inflight)) {
596 trace_usb_host_iso_start(s->bus_num, s->addr, p->ep->nr);
597 }
598 QTAILQ_INSERT_TAIL(&ring->inflight, xfer, next);
599 }
600
601 if (disconnect) {
602 usb_host_nodev(s);
603 }
604}
605
606static void usb_host_iso_data_out(USBHostDevice *s, USBPacket *p)
607{
608 USBHostIsoRing *ring;
609 USBHostIsoXfer *xfer;
610 bool disconnect = false;
611 int rc, filled = 0;
612
613 ring = usb_host_iso_find(s, p->ep);
614 if (ring == NULL) {
615 ring = usb_host_iso_alloc(s, p->ep);
616 }
617
618 /* copy data from guest */
619 xfer = QTAILQ_FIRST(&ring->copy);
620 while (xfer != NULL && xfer->copy_complete) {
621 filled++;
622 xfer = QTAILQ_NEXT(xfer, next);
623 }
624 if (xfer == NULL) {
625 xfer = QTAILQ_FIRST(&ring->unused);
626 if (xfer == NULL) {
627 trace_usb_host_iso_out_of_bufs(s->bus_num, s->addr, p->ep->nr);
628 return;
629 }
630 QTAILQ_REMOVE(&ring->unused, xfer, next);
631 usb_host_iso_reset_xfer(xfer);
632 QTAILQ_INSERT_TAIL(&ring->copy, xfer, next);
633 }
634 usb_host_iso_data_copy(xfer, p);
635
636 if (QTAILQ_EMPTY(&ring->inflight)) {
637 /* wait until half of our buffers are filled
638 before kicking the iso out stream */
639 if (filled*2 < s->iso_urb_count) {
640 return;
641 }
642 }
643
644 /* submit filled bufs to host */
645 while ((xfer = QTAILQ_FIRST(&ring->copy)) != NULL &&
646 xfer->copy_complete) {
647 QTAILQ_REMOVE(&ring->copy, xfer, next);
648 rc = libusb_submit_transfer(xfer->xfer);
649 if (rc != 0) {
650 usb_host_libusb_error("libusb_submit_transfer [iso]", rc);
651 QTAILQ_INSERT_TAIL(&ring->unused, xfer, next);
652 if (rc == LIBUSB_ERROR_NO_DEVICE) {
653 disconnect = true;
654 }
655 break;
656 }
657 if (QTAILQ_EMPTY(&ring->inflight)) {
658 trace_usb_host_iso_start(s->bus_num, s->addr, p->ep->nr);
659 }
660 QTAILQ_INSERT_TAIL(&ring->inflight, xfer, next);
661 }
662
663 if (disconnect) {
664 usb_host_nodev(s);
665 }
666}
667
668/* ------------------------------------------------------------------------ */
669
670static void usb_host_ep_update(USBHostDevice *s)
671{
672 static const char *tname[] = {
673 [USB_ENDPOINT_XFER_CONTROL] = "control",
674 [USB_ENDPOINT_XFER_ISOC] = "isoc",
675 [USB_ENDPOINT_XFER_BULK] = "bulk",
676 [USB_ENDPOINT_XFER_INT] = "int",
677 };
678 USBDevice *udev = USB_DEVICE(s);
679 struct libusb_config_descriptor *conf;
680 const struct libusb_interface_descriptor *intf;
681 const struct libusb_endpoint_descriptor *endp;
682 uint8_t devep, type;
683 int pid, ep;
684 int rc, i, e;
685
686 usb_ep_reset(udev);
687 rc = libusb_get_active_config_descriptor(s->dev, &conf);
688 if (rc != 0) {
689 return;
690 }
691 trace_usb_host_parse_config(s->bus_num, s->addr,
692 conf->bConfigurationValue, true);
693
694 for (i = 0; i < conf->bNumInterfaces; i++) {
695 assert(udev->altsetting[i] < conf->interface[i].num_altsetting);
696 intf = &conf->interface[i].altsetting[udev->altsetting[i]];
697 trace_usb_host_parse_interface(s->bus_num, s->addr,
698 intf->bInterfaceNumber,
699 intf->bAlternateSetting, true);
700 for (e = 0; e < intf->bNumEndpoints; e++) {
701 endp = &intf->endpoint[e];
702
703 devep = endp->bEndpointAddress;
704 pid = (devep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT;
705 ep = devep & 0xf;
706 type = endp->bmAttributes & 0x3;
707
708 if (ep == 0) {
709 trace_usb_host_parse_error(s->bus_num, s->addr,
710 "invalid endpoint address");
711 return;
712 }
713 if (usb_ep_get_type(udev, pid, ep) != USB_ENDPOINT_XFER_INVALID) {
714 trace_usb_host_parse_error(s->bus_num, s->addr,
715 "duplicate endpoint address");
716 return;
717 }
718
719 trace_usb_host_parse_endpoint(s->bus_num, s->addr, ep,
720 (devep & USB_DIR_IN) ? "in" : "out",
721 tname[type], true);
722 usb_ep_set_max_packet_size(udev, pid, ep,
723 endp->wMaxPacketSize);
724 usb_ep_set_type(udev, pid, ep, type);
725 usb_ep_set_ifnum(udev, pid, ep, i);
726 usb_ep_set_halted(udev, pid, ep, 0);
727 }
728 }
729
730 libusb_free_config_descriptor(conf);
731}
732
733static int usb_host_open(USBHostDevice *s, libusb_device *dev)
734{
735 USBDevice *udev = USB_DEVICE(s);
736 int bus_num = libusb_get_bus_number(dev);
737 int addr = libusb_get_device_address(dev);
738 int rc;
739
740 trace_usb_host_open_started(bus_num, addr);
741
742 if (s->dh != NULL) {
743 goto fail;
744 }
745 rc = libusb_open(dev, &s->dh);
746 if (rc != 0) {
747 goto fail;
748 }
749
750 libusb_get_device_descriptor(dev, &s->ddesc);
751 s->dev = dev;
752 s->bus_num = bus_num;
753 s->addr = addr;
754 usb_host_get_port(s->dev, s->port, sizeof(s->port));
755
756 usb_ep_init(udev);
757 usb_host_ep_update(s);
758
759 udev->speed = speed_map[libusb_get_device_speed(dev)];
760 udev->speedmask = (1 << udev->speed);
761#if 0
762 if (udev->speed == USB_SPEED_HIGH && usb_linux_full_speed_compat(dev)) {
763 udev->speedmask |= USB_SPEED_MASK_FULL;
764 }
765#endif
766
767 if (s->ddesc.iProduct) {
768 libusb_get_string_descriptor_ascii(s->dh, s->ddesc.iProduct,
769 (unsigned char *)udev->product_desc,
770 sizeof(udev->product_desc));
771 } else {
772 snprintf(udev->product_desc, sizeof(udev->product_desc),
773 "host:%d.%d", bus_num, addr);
774 }
775
776 rc = usb_device_attach(udev);
777 if (rc) {
778 goto fail;
779 }
780
781 trace_usb_host_open_success(bus_num, addr);
782 return 0;
783
784fail:
785 trace_usb_host_open_failure(bus_num, addr);
786 if (s->dh != NULL) {
787 libusb_close(s->dh);
788 s->dh = NULL;
789 s->dev = NULL;
790 }
791 return -1;
792}
793
794static void usb_host_abort_xfers(USBHostDevice *s)
795{
796 USBHostRequest *r, *rtmp;
797
798 QTAILQ_FOREACH_SAFE(r, &s->requests, next, rtmp) {
799 usb_host_req_abort(r);
800 }
801}
802
803static int usb_host_close(USBHostDevice *s)
804{
805 USBDevice *udev = USB_DEVICE(s);
806
807 if (s->dh == NULL) {
808 return -1;
809 }
810
811 trace_usb_host_close(s->bus_num, s->addr);
812
813 usb_host_abort_xfers(s);
814 usb_host_iso_free_all(s);
815
816 if (udev->attached) {
817 usb_device_detach(udev);
818 }
819
820 usb_host_release_interfaces(s);
821 libusb_reset_device(s->dh);
822 usb_host_attach_kernel(s);
823 libusb_close(s->dh);
824 s->dh = NULL;
825 s->dev = NULL;
826
827 usb_host_auto_check(NULL);
828 return 0;
829}
830
831static void usb_host_nodev_bh(void *opaque)
832{
833 USBHostDevice *s = opaque;
834 usb_host_close(s);
835}
836
837static void usb_host_nodev(USBHostDevice *s)
838{
95efb20c
GH
839 if (!s->bh_nodev) {
840 s->bh_nodev = qemu_bh_new(usb_host_nodev_bh, s);
2b2325ff 841 }
95efb20c 842 qemu_bh_schedule(s->bh_nodev);
2b2325ff
GH
843}
844
845static void usb_host_exit_notifier(struct Notifier *n, void *data)
846{
847 USBHostDevice *s = container_of(n, USBHostDevice, exit);
848
849 if (s->dh) {
850 usb_host_release_interfaces(s);
851 usb_host_attach_kernel(s);
852 }
853}
854
855static int usb_host_initfn(USBDevice *udev)
856{
857 USBHostDevice *s = USB_HOST_DEVICE(udev);
858
859 loglevel = s->loglevel;
860 udev->auto_attach = 0;
861 QTAILQ_INIT(&s->requests);
862 QTAILQ_INIT(&s->isorings);
863
864 s->exit.notify = usb_host_exit_notifier;
865 qemu_add_exit_notifier(&s->exit);
866
867 QTAILQ_INSERT_TAIL(&hostdevs, s, next);
868 add_boot_device_path(s->bootindex, &udev->qdev, NULL);
869 usb_host_auto_check(NULL);
870 return 0;
871}
872
873static void usb_host_handle_destroy(USBDevice *udev)
874{
875 USBHostDevice *s = USB_HOST_DEVICE(udev);
876
877 qemu_remove_exit_notifier(&s->exit);
878 QTAILQ_REMOVE(&hostdevs, s, next);
879 usb_host_close(s);
880}
881
882static void usb_host_cancel_packet(USBDevice *udev, USBPacket *p)
883{
884 USBHostDevice *s = USB_HOST_DEVICE(udev);
885 USBHostRequest *r;
886
887 if (p->combined) {
888 usb_combined_packet_cancel(udev, p);
889 return;
890 }
891
892 trace_usb_host_req_canceled(s->bus_num, s->addr, p);
893
894 r = usb_host_req_find(s, p);
895 if (r && r->p) {
896 r->p = NULL; /* mark as dead */
897 libusb_cancel_transfer(r->xfer);
898 }
899}
900
901static void usb_host_detach_kernel(USBHostDevice *s)
902{
903 struct libusb_config_descriptor *conf;
904 int rc, i;
905
906 rc = libusb_get_active_config_descriptor(s->dev, &conf);
907 if (rc != 0) {
908 return;
909 }
910 for (i = 0; i < conf->bNumInterfaces; i++) {
911 rc = libusb_kernel_driver_active(s->dh, i);
912 usb_host_libusb_error("libusb_kernel_driver_active", rc);
913 if (rc != 1) {
914 continue;
915 }
916 trace_usb_host_detach_kernel(s->bus_num, s->addr, i);
917 rc = libusb_detach_kernel_driver(s->dh, i);
918 usb_host_libusb_error("libusb_detach_kernel_driver", rc);
919 s->ifs[i].detached = true;
920 }
921 libusb_free_config_descriptor(conf);
922}
923
924static void usb_host_attach_kernel(USBHostDevice *s)
925{
926 struct libusb_config_descriptor *conf;
927 int rc, i;
928
929 rc = libusb_get_active_config_descriptor(s->dev, &conf);
930 if (rc != 0) {
931 return;
932 }
933 for (i = 0; i < conf->bNumInterfaces; i++) {
934 if (!s->ifs[i].detached) {
935 continue;
936 }
937 trace_usb_host_attach_kernel(s->bus_num, s->addr, i);
938 libusb_attach_kernel_driver(s->dh, i);
939 s->ifs[i].detached = false;
940 }
941 libusb_free_config_descriptor(conf);
942}
943
944static int usb_host_claim_interfaces(USBHostDevice *s, int configuration)
945{
946 USBDevice *udev = USB_DEVICE(s);
947 struct libusb_config_descriptor *conf;
948 int rc, i;
949
950 for (i = 0; i < USB_MAX_INTERFACES; i++) {
951 udev->altsetting[i] = 0;
952 }
953 udev->ninterfaces = 0;
954 udev->configuration = 0;
955
956 if (configuration == 0) {
957 /* address state - ignore */
958 return USB_RET_SUCCESS;
959 }
960
961 usb_host_detach_kernel(s);
962
963 rc = libusb_get_active_config_descriptor(s->dev, &conf);
964 if (rc != 0) {
965 return USB_RET_STALL;
966 }
967
968 for (i = 0; i < conf->bNumInterfaces; i++) {
969 trace_usb_host_claim_interface(s->bus_num, s->addr, configuration, i);
970 rc = libusb_claim_interface(s->dh, i);
971 usb_host_libusb_error("libusb_claim_interface", rc);
972 if (rc != 0) {
973 return USB_RET_STALL;
974 }
975 s->ifs[i].claimed = true;
976 }
977
978 udev->ninterfaces = conf->bNumInterfaces;
979 udev->configuration = configuration;
980
981 libusb_free_config_descriptor(conf);
982 return USB_RET_SUCCESS;
983}
984
985static void usb_host_release_interfaces(USBHostDevice *s)
986{
987 USBDevice *udev = USB_DEVICE(s);
988 int i, rc;
989
990 for (i = 0; i < udev->ninterfaces; i++) {
991 if (!s->ifs[i].claimed) {
992 continue;
993 }
994 trace_usb_host_release_interface(s->bus_num, s->addr, i);
995 rc = libusb_release_interface(s->dh, i);
996 usb_host_libusb_error("libusb_release_interface", rc);
997 s->ifs[i].claimed = false;
998 }
999}
1000
1001static void usb_host_set_address(USBHostDevice *s, int addr)
1002{
1003 USBDevice *udev = USB_DEVICE(s);
1004
1005 trace_usb_host_set_address(s->bus_num, s->addr, addr);
1006 udev->addr = addr;
1007}
1008
1009static void usb_host_set_config(USBHostDevice *s, int config, USBPacket *p)
1010{
1011 int rc;
1012
1013 trace_usb_host_set_config(s->bus_num, s->addr, config);
1014
1015 usb_host_release_interfaces(s);
1016 usb_host_detach_kernel(s);
1017 rc = libusb_set_configuration(s->dh, config);
1018 if (rc != 0) {
1019 usb_host_libusb_error("libusb_set_configuration", rc);
1020 p->status = USB_RET_STALL;
1021 if (rc == LIBUSB_ERROR_NO_DEVICE) {
1022 usb_host_nodev(s);
1023 }
1024 return;
1025 }
1026 p->status = usb_host_claim_interfaces(s, config);
1027 if (p->status != USB_RET_SUCCESS) {
1028 return;
1029 }
1030 usb_host_ep_update(s);
1031}
1032
1033static void usb_host_set_interface(USBHostDevice *s, int iface, int alt,
1034 USBPacket *p)
1035{
1036 USBDevice *udev = USB_DEVICE(s);
1037 int rc;
1038
1039 trace_usb_host_set_interface(s->bus_num, s->addr, iface, alt);
1040
1041 usb_host_iso_free_all(s);
1042
1043 if (iface >= USB_MAX_INTERFACES) {
1044 p->status = USB_RET_STALL;
1045 return;
1046 }
1047
1048 rc = libusb_set_interface_alt_setting(s->dh, iface, alt);
1049 if (rc != 0) {
1050 usb_host_libusb_error("libusb_set_interface_alt_setting", rc);
1051 p->status = USB_RET_STALL;
1052 if (rc == LIBUSB_ERROR_NO_DEVICE) {
1053 usb_host_nodev(s);
1054 }
1055 return;
1056 }
1057
1058 udev->altsetting[iface] = alt;
1059 usb_host_ep_update(s);
1060}
1061
1062static void usb_host_handle_control(USBDevice *udev, USBPacket *p,
1063 int request, int value, int index,
1064 int length, uint8_t *data)
1065{
1066 USBHostDevice *s = USB_HOST_DEVICE(udev);
1067 USBHostRequest *r;
1068 int rc;
1069
1070 trace_usb_host_req_control(s->bus_num, s->addr, p, request, value, index);
1071
1072 if (s->dh == NULL) {
1073 p->status = USB_RET_NODEV;
1074 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1075 return;
1076 }
1077
1078 switch (request) {
1079 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
1080 usb_host_set_address(s, value);
1081 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1082 return;
1083
1084 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
1085 usb_host_set_config(s, value & 0xff, p);
1086 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1087 return;
1088
1089 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
1090 usb_host_set_interface(s, index, value, p);
1091 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1092 return;
1093
1094 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
1095 if (value == 0) { /* clear halt */
1096 int pid = (index & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT;
1097 libusb_clear_halt(s->dh, index);
1098 usb_ep_set_halted(udev, pid, index & 0x0f, 0);
1099 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1100 return;
1101 }
1102 }
1103
1104 r = usb_host_req_alloc(s, p, (request >> 8) & USB_DIR_IN, length + 8);
1105 r->cbuf = data;
1106 r->clen = length;
1107 memcpy(r->buffer, udev->setup_buf, 8);
1108 if (!r->in) {
1109 memcpy(r->buffer + 8, r->cbuf, r->clen);
1110 }
1111
1112 libusb_fill_control_transfer(r->xfer, s->dh, r->buffer,
1113 usb_host_req_complete_ctrl, r,
1114 CONTROL_TIMEOUT);
1115 rc = libusb_submit_transfer(r->xfer);
1116 if (rc != 0) {
1117 p->status = USB_RET_NODEV;
1118 trace_usb_host_req_complete(s->bus_num, s->addr, p,
1119 p->status, p->actual_length);
1120 if (rc == LIBUSB_ERROR_NO_DEVICE) {
1121 usb_host_nodev(s);
1122 }
1123 return;
1124 }
1125
1126 p->status = USB_RET_ASYNC;
1127}
1128
1129static void usb_host_handle_data(USBDevice *udev, USBPacket *p)
1130{
1131 USBHostDevice *s = USB_HOST_DEVICE(udev);
1132 USBHostRequest *r;
1133 size_t size;
1134 int ep, rc;
1135
1136 if (usb_host_use_combining(p->ep) && p->state == USB_PACKET_SETUP) {
1137 p->status = USB_RET_ADD_TO_QUEUE;
1138 return;
1139 }
1140
1141 trace_usb_host_req_data(s->bus_num, s->addr, p,
1142 p->pid == USB_TOKEN_IN,
1143 p->ep->nr, p->iov.size);
1144
1145 if (s->dh == NULL) {
1146 p->status = USB_RET_NODEV;
1147 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1148 return;
1149 }
1150 if (p->ep->halted) {
1151 p->status = USB_RET_STALL;
1152 trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status);
1153 return;
1154 }
1155
1156 switch (usb_ep_get_type(udev, p->pid, p->ep->nr)) {
1157 case USB_ENDPOINT_XFER_BULK:
1158 size = usb_packet_size(p);
1159 r = usb_host_req_alloc(s, p, p->pid == USB_TOKEN_IN, size);
1160 if (!r->in) {
1161 usb_packet_copy(p, r->buffer, size);
1162 }
1163 ep = p->ep->nr | (r->in ? USB_DIR_IN : 0);
1164 libusb_fill_bulk_transfer(r->xfer, s->dh, ep,
1165 r->buffer, size,
1166 usb_host_req_complete_data, r,
1167 BULK_TIMEOUT);
1168 break;
1169 case USB_ENDPOINT_XFER_INT:
1170 r = usb_host_req_alloc(s, p, p->pid == USB_TOKEN_IN, p->iov.size);
1171 if (!r->in) {
1172 usb_packet_copy(p, r->buffer, p->iov.size);
1173 }
1174 ep = p->ep->nr | (r->in ? USB_DIR_IN : 0);
1175 libusb_fill_interrupt_transfer(r->xfer, s->dh, ep,
1176 r->buffer, p->iov.size,
1177 usb_host_req_complete_data, r,
1178 INTR_TIMEOUT);
1179 break;
1180 case USB_ENDPOINT_XFER_ISOC:
1181 if (p->pid == USB_TOKEN_IN) {
1182 usb_host_iso_data_in(s, p);
1183 } else {
1184 usb_host_iso_data_out(s, p);
1185 }
1186 trace_usb_host_req_complete(s->bus_num, s->addr, p,
1187 p->status, p->actual_length);
1188 return;
1189 default:
1190 p->status = USB_RET_STALL;
1191 trace_usb_host_req_complete(s->bus_num, s->addr, p,
1192 p->status, p->actual_length);
1193 return;
1194 }
1195
1196 rc = libusb_submit_transfer(r->xfer);
1197 if (rc != 0) {
1198 p->status = USB_RET_NODEV;
1199 trace_usb_host_req_complete(s->bus_num, s->addr, p,
1200 p->status, p->actual_length);
1201 if (rc == LIBUSB_ERROR_NO_DEVICE) {
1202 usb_host_nodev(s);
1203 }
1204 return;
1205 }
1206
1207 p->status = USB_RET_ASYNC;
1208}
1209
1210static void usb_host_flush_ep_queue(USBDevice *dev, USBEndpoint *ep)
1211{
1212 if (usb_host_use_combining(ep)) {
1213 usb_ep_combine_input_packets(ep);
1214 }
1215}
1216
1217static void usb_host_handle_reset(USBDevice *udev)
1218{
1219 USBHostDevice *s = USB_HOST_DEVICE(udev);
1220
1221 trace_usb_host_reset(s->bus_num, s->addr);
1222
1223 if (udev->configuration == 0) {
1224 return;
1225 }
1226 usb_host_release_interfaces(s);
1227 libusb_reset_device(s->dh);
1228 usb_host_claim_interfaces(s, 0);
1229 usb_host_ep_update(s);
1230}
1231
95efb20c
GH
1232/*
1233 * This is *NOT* about restoring state. We have absolutely no idea
1234 * what state the host device is in at the moment and whenever it is
1235 * still present in the first place. Attemping to contine where we
1236 * left off is impossible.
1237 *
1238 * What we are going to to to here is emulate a surprise removal of
1239 * the usb device passed through, then kick host scan so the device
1240 * will get re-attached (and re-initialized by the guest) in case it
1241 * is still present.
1242 *
1243 * As the device removal will change the state of other devices (usb
1244 * host controller, most likely interrupt controller too) we have to
1245 * wait with it until *all* vmstate is loaded. Thus post_load just
1246 * kicks a bottom half which then does the actual work.
1247 */
1248static void usb_host_post_load_bh(void *opaque)
1249{
1250 USBHostDevice *dev = opaque;
1251 USBDevice *udev = USB_DEVICE(dev);
1252
1253 if (dev->dh != NULL) {
1254 usb_host_close(dev);
1255 }
1256 if (udev->attached) {
1257 usb_device_detach(udev);
1258 }
1259 usb_host_auto_check(NULL);
1260}
1261
1262static int usb_host_post_load(void *opaque, int version_id)
1263{
1264 USBHostDevice *dev = opaque;
1265
1266 if (!dev->bh_postld) {
1267 dev->bh_postld = qemu_bh_new(usb_host_post_load_bh, dev);
1268 }
1269 qemu_bh_schedule(dev->bh_postld);
1270 return 0;
1271}
1272
2b2325ff
GH
1273static const VMStateDescription vmstate_usb_host = {
1274 .name = "usb-host",
95efb20c
GH
1275 .version_id = 1,
1276 .minimum_version_id = 1,
1277 .post_load = usb_host_post_load,
2b2325ff
GH
1278 .fields = (VMStateField[]) {
1279 VMSTATE_USB_DEVICE(parent_obj, USBHostDevice),
1280 VMSTATE_END_OF_LIST()
1281 }
1282};
1283
1284static Property usb_host_dev_properties[] = {
1285 DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0),
1286 DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0),
1287 DEFINE_PROP_STRING("hostport", USBHostDevice, match.port),
1288 DEFINE_PROP_HEX32("vendorid", USBHostDevice, match.vendor_id, 0),
1289 DEFINE_PROP_HEX32("productid", USBHostDevice, match.product_id, 0),
1290 DEFINE_PROP_UINT32("isobufs", USBHostDevice, iso_urb_count, 4),
1291 DEFINE_PROP_UINT32("isobsize", USBHostDevice, iso_urb_frames, 32),
1292 DEFINE_PROP_INT32("bootindex", USBHostDevice, bootindex, -1),
1293 DEFINE_PROP_UINT32("loglevel", USBHostDevice, loglevel,
1294 LIBUSB_LOG_LEVEL_WARNING),
1295 DEFINE_PROP_BIT("pipeline", USBHostDevice, options,
1296 USB_HOST_OPT_PIPELINE, true),
1297 DEFINE_PROP_END_OF_LIST(),
1298};
1299
1300static void usb_host_class_initfn(ObjectClass *klass, void *data)
1301{
1302 DeviceClass *dc = DEVICE_CLASS(klass);
1303 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
1304
1305 uc->init = usb_host_initfn;
1306 uc->product_desc = "USB Host Device";
1307 uc->cancel_packet = usb_host_cancel_packet;
1308 uc->handle_data = usb_host_handle_data;
1309 uc->handle_control = usb_host_handle_control;
1310 uc->handle_reset = usb_host_handle_reset;
1311 uc->handle_destroy = usb_host_handle_destroy;
1312 uc->flush_ep_queue = usb_host_flush_ep_queue;
1313 dc->vmsd = &vmstate_usb_host;
1314 dc->props = usb_host_dev_properties;
1315}
1316
1317static TypeInfo usb_host_dev_info = {
1318 .name = TYPE_USB_HOST_DEVICE,
1319 .parent = TYPE_USB_DEVICE,
1320 .instance_size = sizeof(USBHostDevice),
1321 .class_init = usb_host_class_initfn,
1322};
1323
1324static void usb_host_register_types(void)
1325{
1326 type_register_static(&usb_host_dev_info);
1327}
1328
1329type_init(usb_host_register_types)
1330
1331/* ------------------------------------------------------------------------ */
1332
1333static QEMUTimer *usb_auto_timer;
1334static VMChangeStateEntry *usb_vmstate;
1335
1336static void usb_host_vm_state(void *unused, int running, RunState state)
1337{
1338 if (running) {
1339 usb_host_auto_check(unused);
1340 }
1341}
1342
1343static void usb_host_auto_check(void *unused)
1344{
1345 struct USBHostDevice *s;
1346 struct USBAutoFilter *f;
1347 libusb_device **devs;
1348 struct libusb_device_descriptor ddesc;
1349 int unconnected = 0;
1350 int i, n;
1351
1352 if (usb_host_init() != 0) {
1353 return;
1354 }
1355
1356 if (runstate_is_running()) {
1357 n = libusb_get_device_list(ctx, &devs);
1358 for (i = 0; i < n; i++) {
1359 if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) {
1360 continue;
1361 }
1362 if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) {
1363 continue;
1364 }
1365 QTAILQ_FOREACH(s, &hostdevs, next) {
1366 f = &s->match;
1367 if (f->bus_num > 0 &&
1368 f->bus_num != libusb_get_bus_number(devs[i])) {
1369 continue;
1370 }
1371 if (f->addr > 0 &&
1372 f->addr != libusb_get_device_address(devs[i])) {
1373 continue;
1374 }
1375 if (f->port != NULL) {
1376 char port[16] = "-";
1377 usb_host_get_port(devs[i], port, sizeof(port));
1378 if (strcmp(f->port, port) != 0) {
1379 continue;
1380 }
1381 }
1382 if (f->vendor_id > 0 &&
1383 f->vendor_id != ddesc.idVendor) {
1384 continue;
1385 }
1386 if (f->product_id > 0 &&
1387 f->product_id != ddesc.idProduct) {
1388 continue;
1389 }
1390
1391 /* We got a match */
1392 s->seen++;
1393 if (s->errcount >= 3) {
1394 continue;
1395 }
1396 if (s->dh != NULL) {
1397 continue;
1398 }
1399 if (usb_host_open(s, devs[i]) < 0) {
1400 s->errcount++;
1401 continue;
1402 }
1403 break;
1404 }
1405 }
1406 libusb_free_device_list(devs, 1);
1407
1408 QTAILQ_FOREACH(s, &hostdevs, next) {
1409 if (s->dh == NULL) {
1410 unconnected++;
1411 }
1412 if (s->seen == 0) {
1413 if (s->dh) {
1414 usb_host_close(s);
1415 }
1416 s->errcount = 0;
1417 }
1418 s->seen = 0;
1419 }
1420
1421#if 0
1422 if (unconnected == 0) {
1423 /* nothing to watch */
1424 if (usb_auto_timer) {
1425 qemu_del_timer(usb_auto_timer);
1426 trace_usb_host_auto_scan_disabled();
1427 }
1428 return;
1429 }
1430#endif
1431 }
1432
1433 if (!usb_vmstate) {
1434 usb_vmstate = qemu_add_vm_change_state_handler(usb_host_vm_state, NULL);
1435 }
1436 if (!usb_auto_timer) {
1437 usb_auto_timer = qemu_new_timer_ms(rt_clock, usb_host_auto_check, NULL);
1438 if (!usb_auto_timer) {
1439 return;
1440 }
1441 trace_usb_host_auto_scan_enabled();
1442 }
1443 qemu_mod_timer(usb_auto_timer, qemu_get_clock_ms(rt_clock) + 2000);
1444}
1445
1446void usb_host_info(Monitor *mon, const QDict *qdict)
1447{
1448 libusb_device **devs;
1449 struct libusb_device_descriptor ddesc;
1450 char port[16];
1451 int i, n;
1452
1453 if (usb_host_init() != 0) {
1454 return;
1455 }
1456
1457 n = libusb_get_device_list(ctx, &devs);
1458 for (i = 0; i < n; i++) {
1459 if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) {
1460 continue;
1461 }
1462 if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) {
1463 continue;
1464 }
1465 usb_host_get_port(devs[i], port, sizeof(port));
1466 monitor_printf(mon, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1467 libusb_get_bus_number(devs[i]),
1468 libusb_get_device_address(devs[i]),
1469 port,
1470 speed_name[libusb_get_device_speed(devs[i])]);
1471 monitor_printf(mon, " Class %02x:", ddesc.bDeviceClass);
1472 monitor_printf(mon, " USB device %04x:%04x",
1473 ddesc.idVendor, ddesc.idProduct);
1474 if (ddesc.iProduct) {
1475 libusb_device_handle *handle;
1476 if (libusb_open(devs[i], &handle) == 0) {
1477 unsigned char name[64] = "";
1478 libusb_get_string_descriptor_ascii(handle,
1479 ddesc.iProduct,
1480 name, sizeof(name));
1481 libusb_close(handle);
1482 monitor_printf(mon, ", %s", name);
1483 }
1484 }
1485 monitor_printf(mon, "\n");
1486 }
1487 libusb_free_device_list(devs, 1);
1488}