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