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