]>
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; | |
114 | QTAILQ_ENTRY(USBHostRequest) next; | |
115 | }; | |
116 | ||
117 | struct USBHostIsoXfer { | |
118 | USBHostIsoRing *ring; | |
119 | struct libusb_transfer *xfer; | |
120 | bool copy_complete; | |
121 | unsigned int packet; | |
122 | QTAILQ_ENTRY(USBHostIsoXfer) next; | |
123 | }; | |
124 | ||
125 | struct 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 | ||
134 | static QTAILQ_HEAD(, USBHostDevice) hostdevs = | |
135 | QTAILQ_HEAD_INITIALIZER(hostdevs); | |
136 | ||
137 | static void usb_host_auto_check(void *unused); | |
138 | static void usb_host_release_interfaces(USBHostDevice *s); | |
139 | static void usb_host_nodev(USBHostDevice *s); | |
f34d5c75 | 140 | static void usb_host_detach_kernel(USBHostDevice *s); |
2b2325ff GH |
141 | static 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 | ||
149 | static 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 | ||
157 | static 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 | ||
164 | static 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 | ||
174 | static 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 | ||
190 | static libusb_context *ctx; | |
191 | static uint32_t loglevel; | |
192 | ||
193 | static void usb_host_handle_fd(void *opaque) | |
194 | { | |
195 | struct timeval tv = { 0, 0 }; | |
196 | libusb_handle_events_timeout(ctx, &tv); | |
197 | } | |
198 | ||
199 | static 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 | ||
207 | static void usb_host_del_fd(int fd, void *user_data) | |
208 | { | |
209 | qemu_set_fd_handler(fd, NULL, NULL, NULL); | |
210 | } | |
211 | ||
212 | static 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 | ||
239 | static 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 | ||
260 | static 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 | ||
278 | static 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 | ||
297 | static 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 | ||
313 | static 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 | ||
323 | static 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 | ||
335 | static 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 | ||
354 | out: | |
355 | usb_host_req_free(r); | |
356 | if (disconnect) { | |
357 | usb_host_nodev(s); | |
358 | } | |
359 | } | |
360 | ||
361 | static 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 | ||
383 | out: | |
384 | usb_host_req_free(r); | |
385 | if (disconnect) { | |
386 | usb_host_nodev(s); | |
387 | } | |
388 | } | |
389 | ||
390 | static 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 | ||
417 | static 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 | ||
440 | static 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 | ||
479 | static 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 | ||
491 | static 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 | ||
499 | static 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 | ||
510 | static 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 | ||
531 | static 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 | ||
540 | static 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 | ||
566 | static 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 | ||
611 | static 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 |
675 | static 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 |
711 | static 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 | ||
786 | static 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 | ||
838 | fail: | |
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 | ||
848 | static 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 | ||
857 | static 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 | ||
885 | static void usb_host_nodev_bh(void *opaque) | |
886 | { | |
887 | USBHostDevice *s = opaque; | |
888 | usb_host_close(s); | |
889 | } | |
890 | ||
891 | static 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 | ||
899 | static 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 | ||
909 | static 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 | ||
928 | static 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 | ||
937 | static 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 | ||
956 | static 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 | ||
979 | static 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 | ||
999 | static 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 | ||
1039 | static 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 | ||
1055 | static 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 | ||
1063 | static 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 | ||
1086 | static 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 | ||
1115 | static 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 | ||
1182 | static 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); | |
1217 | libusb_fill_bulk_transfer(r->xfer, s->dh, ep, | |
1218 | r->buffer, size, | |
1219 | usb_host_req_complete_data, r, | |
1220 | BULK_TIMEOUT); | |
1221 | break; | |
1222 | case USB_ENDPOINT_XFER_INT: | |
1223 | r = usb_host_req_alloc(s, p, p->pid == USB_TOKEN_IN, p->iov.size); | |
1224 | if (!r->in) { | |
1225 | usb_packet_copy(p, r->buffer, p->iov.size); | |
1226 | } | |
1227 | ep = p->ep->nr | (r->in ? USB_DIR_IN : 0); | |
1228 | libusb_fill_interrupt_transfer(r->xfer, s->dh, ep, | |
1229 | r->buffer, p->iov.size, | |
1230 | usb_host_req_complete_data, r, | |
1231 | INTR_TIMEOUT); | |
1232 | break; | |
1233 | case USB_ENDPOINT_XFER_ISOC: | |
1234 | if (p->pid == USB_TOKEN_IN) { | |
1235 | usb_host_iso_data_in(s, p); | |
1236 | } else { | |
1237 | usb_host_iso_data_out(s, p); | |
1238 | } | |
1239 | trace_usb_host_req_complete(s->bus_num, s->addr, p, | |
1240 | p->status, p->actual_length); | |
1241 | return; | |
1242 | default: | |
1243 | p->status = USB_RET_STALL; | |
1244 | trace_usb_host_req_complete(s->bus_num, s->addr, p, | |
1245 | p->status, p->actual_length); | |
1246 | return; | |
1247 | } | |
1248 | ||
1249 | rc = libusb_submit_transfer(r->xfer); | |
1250 | if (rc != 0) { | |
1251 | p->status = USB_RET_NODEV; | |
1252 | trace_usb_host_req_complete(s->bus_num, s->addr, p, | |
1253 | p->status, p->actual_length); | |
1254 | if (rc == LIBUSB_ERROR_NO_DEVICE) { | |
1255 | usb_host_nodev(s); | |
1256 | } | |
1257 | return; | |
1258 | } | |
1259 | ||
1260 | p->status = USB_RET_ASYNC; | |
1261 | } | |
1262 | ||
1263 | static void usb_host_flush_ep_queue(USBDevice *dev, USBEndpoint *ep) | |
1264 | { | |
1265 | if (usb_host_use_combining(ep)) { | |
1266 | usb_ep_combine_input_packets(ep); | |
1267 | } | |
1268 | } | |
1269 | ||
1270 | static void usb_host_handle_reset(USBDevice *udev) | |
1271 | { | |
1272 | USBHostDevice *s = USB_HOST_DEVICE(udev); | |
5af35d7f | 1273 | int rc; |
2b2325ff GH |
1274 | |
1275 | trace_usb_host_reset(s->bus_num, s->addr); | |
1276 | ||
5af35d7f HG |
1277 | rc = libusb_reset_device(s->dh); |
1278 | if (rc != 0) { | |
1279 | usb_host_nodev(s); | |
2b2325ff | 1280 | } |
2b2325ff GH |
1281 | } |
1282 | ||
56a9f180 HG |
1283 | static int usb_host_alloc_streams(USBDevice *udev, USBEndpoint **eps, |
1284 | int nr_eps, int streams) | |
1285 | { | |
1286 | #if LIBUSBX_API_VERSION >= 0x01000103 | |
1287 | USBHostDevice *s = USB_HOST_DEVICE(udev); | |
1288 | unsigned char endpoints[30]; | |
1289 | int i, rc; | |
1290 | ||
1291 | for (i = 0; i < nr_eps; i++) { | |
1292 | endpoints[i] = eps[i]->nr; | |
1293 | if (eps[i]->pid == USB_TOKEN_IN) { | |
1294 | endpoints[i] |= 0x80; | |
1295 | } | |
1296 | } | |
1297 | rc = libusb_alloc_streams(s->dh, streams, endpoints, nr_eps); | |
1298 | if (rc < 0) { | |
1299 | usb_host_libusb_error("libusb_alloc_streams", rc); | |
1300 | } else if (rc != streams) { | |
1301 | fprintf(stderr, | |
1302 | "libusb_alloc_streams: got less streams then requested %d < %d\n", | |
1303 | rc, streams); | |
1304 | } | |
1305 | ||
1306 | return (rc == streams) ? 0 : -1; | |
1307 | #else | |
1308 | fprintf(stderr, "libusb_alloc_streams: error not implemented\n"); | |
1309 | return -1; | |
1310 | #endif | |
1311 | } | |
1312 | ||
1313 | static void usb_host_free_streams(USBDevice *udev, USBEndpoint **eps, | |
1314 | int nr_eps) | |
1315 | { | |
1316 | #if LIBUSBX_API_VERSION >= 0x01000103 | |
1317 | USBHostDevice *s = USB_HOST_DEVICE(udev); | |
1318 | unsigned char endpoints[30]; | |
1319 | int i; | |
1320 | ||
1321 | for (i = 0; i < nr_eps; i++) { | |
1322 | endpoints[i] = eps[i]->nr; | |
1323 | if (eps[i]->pid == USB_TOKEN_IN) { | |
1324 | endpoints[i] |= 0x80; | |
1325 | } | |
1326 | } | |
1327 | libusb_free_streams(s->dh, endpoints, nr_eps); | |
1328 | #endif | |
1329 | } | |
1330 | ||
95efb20c GH |
1331 | /* |
1332 | * This is *NOT* about restoring state. We have absolutely no idea | |
1333 | * what state the host device is in at the moment and whenever it is | |
1334 | * still present in the first place. Attemping to contine where we | |
1335 | * left off is impossible. | |
1336 | * | |
1337 | * What we are going to to to here is emulate a surprise removal of | |
1338 | * the usb device passed through, then kick host scan so the device | |
1339 | * will get re-attached (and re-initialized by the guest) in case it | |
1340 | * is still present. | |
1341 | * | |
1342 | * As the device removal will change the state of other devices (usb | |
1343 | * host controller, most likely interrupt controller too) we have to | |
1344 | * wait with it until *all* vmstate is loaded. Thus post_load just | |
1345 | * kicks a bottom half which then does the actual work. | |
1346 | */ | |
1347 | static void usb_host_post_load_bh(void *opaque) | |
1348 | { | |
1349 | USBHostDevice *dev = opaque; | |
1350 | USBDevice *udev = USB_DEVICE(dev); | |
1351 | ||
1352 | if (dev->dh != NULL) { | |
1353 | usb_host_close(dev); | |
1354 | } | |
1355 | if (udev->attached) { | |
1356 | usb_device_detach(udev); | |
1357 | } | |
1358 | usb_host_auto_check(NULL); | |
1359 | } | |
1360 | ||
1361 | static int usb_host_post_load(void *opaque, int version_id) | |
1362 | { | |
1363 | USBHostDevice *dev = opaque; | |
1364 | ||
1365 | if (!dev->bh_postld) { | |
1366 | dev->bh_postld = qemu_bh_new(usb_host_post_load_bh, dev); | |
1367 | } | |
1368 | qemu_bh_schedule(dev->bh_postld); | |
1369 | return 0; | |
1370 | } | |
1371 | ||
2b2325ff GH |
1372 | static const VMStateDescription vmstate_usb_host = { |
1373 | .name = "usb-host", | |
95efb20c GH |
1374 | .version_id = 1, |
1375 | .minimum_version_id = 1, | |
1376 | .post_load = usb_host_post_load, | |
2b2325ff GH |
1377 | .fields = (VMStateField[]) { |
1378 | VMSTATE_USB_DEVICE(parent_obj, USBHostDevice), | |
1379 | VMSTATE_END_OF_LIST() | |
1380 | } | |
1381 | }; | |
1382 | ||
1383 | static Property usb_host_dev_properties[] = { | |
1384 | DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0), | |
1385 | DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0), | |
1386 | DEFINE_PROP_STRING("hostport", USBHostDevice, match.port), | |
c7bcc85d PB |
1387 | DEFINE_PROP_UINT32("vendorid", USBHostDevice, match.vendor_id, 0), |
1388 | DEFINE_PROP_UINT32("productid", USBHostDevice, match.product_id, 0), | |
2b2325ff GH |
1389 | DEFINE_PROP_UINT32("isobufs", USBHostDevice, iso_urb_count, 4), |
1390 | DEFINE_PROP_UINT32("isobsize", USBHostDevice, iso_urb_frames, 32), | |
1391 | DEFINE_PROP_INT32("bootindex", USBHostDevice, bootindex, -1), | |
1392 | DEFINE_PROP_UINT32("loglevel", USBHostDevice, loglevel, | |
1393 | LIBUSB_LOG_LEVEL_WARNING), | |
1394 | DEFINE_PROP_BIT("pipeline", USBHostDevice, options, | |
1395 | USB_HOST_OPT_PIPELINE, true), | |
1396 | DEFINE_PROP_END_OF_LIST(), | |
1397 | }; | |
1398 | ||
1399 | static void usb_host_class_initfn(ObjectClass *klass, void *data) | |
1400 | { | |
1401 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1402 | USBDeviceClass *uc = USB_DEVICE_CLASS(klass); | |
1403 | ||
1404 | uc->init = usb_host_initfn; | |
1405 | uc->product_desc = "USB Host Device"; | |
1406 | uc->cancel_packet = usb_host_cancel_packet; | |
1407 | uc->handle_data = usb_host_handle_data; | |
1408 | uc->handle_control = usb_host_handle_control; | |
1409 | uc->handle_reset = usb_host_handle_reset; | |
1410 | uc->handle_destroy = usb_host_handle_destroy; | |
1411 | uc->flush_ep_queue = usb_host_flush_ep_queue; | |
56a9f180 HG |
1412 | uc->alloc_streams = usb_host_alloc_streams; |
1413 | uc->free_streams = usb_host_free_streams; | |
2b2325ff GH |
1414 | dc->vmsd = &vmstate_usb_host; |
1415 | dc->props = usb_host_dev_properties; | |
125ee0ed | 1416 | set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); |
2b2325ff GH |
1417 | } |
1418 | ||
1419 | static TypeInfo usb_host_dev_info = { | |
1420 | .name = TYPE_USB_HOST_DEVICE, | |
1421 | .parent = TYPE_USB_DEVICE, | |
1422 | .instance_size = sizeof(USBHostDevice), | |
1423 | .class_init = usb_host_class_initfn, | |
1424 | }; | |
1425 | ||
1426 | static void usb_host_register_types(void) | |
1427 | { | |
1428 | type_register_static(&usb_host_dev_info); | |
1429 | } | |
1430 | ||
1431 | type_init(usb_host_register_types) | |
1432 | ||
1433 | /* ------------------------------------------------------------------------ */ | |
1434 | ||
1435 | static QEMUTimer *usb_auto_timer; | |
1436 | static VMChangeStateEntry *usb_vmstate; | |
1437 | ||
1438 | static void usb_host_vm_state(void *unused, int running, RunState state) | |
1439 | { | |
1440 | if (running) { | |
1441 | usb_host_auto_check(unused); | |
1442 | } | |
1443 | } | |
1444 | ||
1445 | static void usb_host_auto_check(void *unused) | |
1446 | { | |
1447 | struct USBHostDevice *s; | |
1448 | struct USBAutoFilter *f; | |
1449 | libusb_device **devs; | |
1450 | struct libusb_device_descriptor ddesc; | |
1451 | int unconnected = 0; | |
1452 | int i, n; | |
1453 | ||
1454 | if (usb_host_init() != 0) { | |
1455 | return; | |
1456 | } | |
1457 | ||
1458 | if (runstate_is_running()) { | |
1459 | n = libusb_get_device_list(ctx, &devs); | |
1460 | for (i = 0; i < n; i++) { | |
1461 | if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) { | |
1462 | continue; | |
1463 | } | |
1464 | if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) { | |
1465 | continue; | |
1466 | } | |
1467 | QTAILQ_FOREACH(s, &hostdevs, next) { | |
1468 | f = &s->match; | |
1469 | if (f->bus_num > 0 && | |
1470 | f->bus_num != libusb_get_bus_number(devs[i])) { | |
1471 | continue; | |
1472 | } | |
1473 | if (f->addr > 0 && | |
1474 | f->addr != libusb_get_device_address(devs[i])) { | |
1475 | continue; | |
1476 | } | |
1477 | if (f->port != NULL) { | |
1478 | char port[16] = "-"; | |
1479 | usb_host_get_port(devs[i], port, sizeof(port)); | |
1480 | if (strcmp(f->port, port) != 0) { | |
1481 | continue; | |
1482 | } | |
1483 | } | |
1484 | if (f->vendor_id > 0 && | |
1485 | f->vendor_id != ddesc.idVendor) { | |
1486 | continue; | |
1487 | } | |
1488 | if (f->product_id > 0 && | |
1489 | f->product_id != ddesc.idProduct) { | |
1490 | continue; | |
1491 | } | |
1492 | ||
1493 | /* We got a match */ | |
1494 | s->seen++; | |
1495 | if (s->errcount >= 3) { | |
1496 | continue; | |
1497 | } | |
1498 | if (s->dh != NULL) { | |
1499 | continue; | |
1500 | } | |
1501 | if (usb_host_open(s, devs[i]) < 0) { | |
1502 | s->errcount++; | |
1503 | continue; | |
1504 | } | |
1505 | break; | |
1506 | } | |
1507 | } | |
1508 | libusb_free_device_list(devs, 1); | |
1509 | ||
1510 | QTAILQ_FOREACH(s, &hostdevs, next) { | |
1511 | if (s->dh == NULL) { | |
1512 | unconnected++; | |
1513 | } | |
1514 | if (s->seen == 0) { | |
1515 | if (s->dh) { | |
1516 | usb_host_close(s); | |
1517 | } | |
1518 | s->errcount = 0; | |
1519 | } | |
1520 | s->seen = 0; | |
1521 | } | |
1522 | ||
1523 | #if 0 | |
1524 | if (unconnected == 0) { | |
1525 | /* nothing to watch */ | |
1526 | if (usb_auto_timer) { | |
bc72ad67 | 1527 | timer_del(usb_auto_timer); |
2b2325ff GH |
1528 | trace_usb_host_auto_scan_disabled(); |
1529 | } | |
1530 | return; | |
1531 | } | |
1532 | #endif | |
1533 | } | |
1534 | ||
1535 | if (!usb_vmstate) { | |
1536 | usb_vmstate = qemu_add_vm_change_state_handler(usb_host_vm_state, NULL); | |
1537 | } | |
1538 | if (!usb_auto_timer) { | |
bc72ad67 | 1539 | usb_auto_timer = timer_new_ms(QEMU_CLOCK_REALTIME, usb_host_auto_check, NULL); |
2b2325ff GH |
1540 | if (!usb_auto_timer) { |
1541 | return; | |
1542 | } | |
1543 | trace_usb_host_auto_scan_enabled(); | |
1544 | } | |
bc72ad67 | 1545 | timer_mod(usb_auto_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 2000); |
2b2325ff GH |
1546 | } |
1547 | ||
1548 | void usb_host_info(Monitor *mon, const QDict *qdict) | |
1549 | { | |
1550 | libusb_device **devs; | |
1551 | struct libusb_device_descriptor ddesc; | |
1552 | char port[16]; | |
1553 | int i, n; | |
1554 | ||
1555 | if (usb_host_init() != 0) { | |
1556 | return; | |
1557 | } | |
1558 | ||
1559 | n = libusb_get_device_list(ctx, &devs); | |
1560 | for (i = 0; i < n; i++) { | |
1561 | if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) { | |
1562 | continue; | |
1563 | } | |
1564 | if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) { | |
1565 | continue; | |
1566 | } | |
1567 | usb_host_get_port(devs[i], port, sizeof(port)); | |
1568 | monitor_printf(mon, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n", | |
1569 | libusb_get_bus_number(devs[i]), | |
1570 | libusb_get_device_address(devs[i]), | |
1571 | port, | |
1572 | speed_name[libusb_get_device_speed(devs[i])]); | |
1573 | monitor_printf(mon, " Class %02x:", ddesc.bDeviceClass); | |
1574 | monitor_printf(mon, " USB device %04x:%04x", | |
1575 | ddesc.idVendor, ddesc.idProduct); | |
1576 | if (ddesc.iProduct) { | |
1577 | libusb_device_handle *handle; | |
1578 | if (libusb_open(devs[i], &handle) == 0) { | |
1579 | unsigned char name[64] = ""; | |
1580 | libusb_get_string_descriptor_ascii(handle, | |
1581 | ddesc.iProduct, | |
1582 | name, sizeof(name)); | |
1583 | libusb_close(handle); | |
1584 | monitor_printf(mon, ", %s", name); | |
1585 | } | |
1586 | } | |
1587 | monitor_printf(mon, "\n"); | |
1588 | } | |
1589 | libusb_free_device_list(devs, 1); | |
1590 | } |