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