| 1 | /* |
| 2 | * Linux host USB redirector |
| 3 | * |
| 4 | * Copyright (c) 2005 Fabrice Bellard |
| 5 | * |
| 6 | * Copyright (c) 2008 Max Krasnyansky |
| 7 | * Support for host device auto connect & disconnect |
| 8 | * Major rewrite to support fully async operation |
| 9 | * |
| 10 | * Copyright 2008 TJ <linux@tjworld.net> |
| 11 | * Added flexible support for /dev/bus/usb /sys/bus/usb/devices in addition |
| 12 | * to the legacy /proc/bus/usb USB device discovery and handling |
| 13 | * |
| 14 | * (c) 2012 Gerd Hoffmann <kraxel@redhat.com> |
| 15 | * Completely rewritten to use libusb instead of usbfs ioctls. |
| 16 | * |
| 17 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 18 | * of this software and associated documentation files (the "Software"), to deal |
| 19 | * in the Software without restriction, including without limitation the rights |
| 20 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 21 | * copies of the Software, and to permit persons to whom the Software is |
| 22 | * furnished to do so, subject to the following conditions: |
| 23 | * |
| 24 | * The above copyright notice and this permission notice shall be included in |
| 25 | * all copies or substantial portions of the Software. |
| 26 | * |
| 27 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 28 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 29 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 30 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 31 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 32 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 33 | * THE SOFTWARE. |
| 34 | */ |
| 35 | |
| 36 | #include "qemu/osdep.h" |
| 37 | #include "qom/object.h" |
| 38 | #ifndef CONFIG_WIN32 |
| 39 | #include <poll.h> |
| 40 | #endif |
| 41 | #include <libusb.h> |
| 42 | |
| 43 | #ifdef CONFIG_LINUX |
| 44 | #include <sys/ioctl.h> |
| 45 | #include <linux/usbdevice_fs.h> |
| 46 | #endif |
| 47 | |
| 48 | #include "qapi/error.h" |
| 49 | #include "migration/vmstate.h" |
| 50 | #include "monitor/monitor.h" |
| 51 | #include "monitor/hmp.h" |
| 52 | #include "qemu/error-report.h" |
| 53 | #include "qemu/main-loop.h" |
| 54 | #include "qemu/module.h" |
| 55 | #include "system/runstate.h" |
| 56 | #include "system/system.h" |
| 57 | #include "trace.h" |
| 58 | |
| 59 | #include "hw/core/qdev-properties.h" |
| 60 | #include "hw/usb/usb.h" |
| 61 | |
| 62 | /* ------------------------------------------------------------------------ */ |
| 63 | |
| 64 | #define TYPE_USB_HOST_DEVICE "usb-host" |
| 65 | OBJECT_DECLARE_SIMPLE_TYPE(USBHostDevice, USB_HOST_DEVICE) |
| 66 | |
| 67 | typedef struct USBHostRequest USBHostRequest; |
| 68 | typedef struct USBHostIsoXfer USBHostIsoXfer; |
| 69 | typedef struct USBHostIsoRing USBHostIsoRing; |
| 70 | |
| 71 | struct USBAutoFilter { |
| 72 | uint32_t bus_num; |
| 73 | uint32_t addr; |
| 74 | char *port; |
| 75 | uint32_t vendor_id; |
| 76 | uint32_t product_id; |
| 77 | }; |
| 78 | |
| 79 | enum USBHostDeviceOptions { |
| 80 | USB_HOST_OPT_PIPELINE, |
| 81 | }; |
| 82 | |
| 83 | struct USBHostDevice { |
| 84 | USBDevice parent_obj; |
| 85 | |
| 86 | /* properties */ |
| 87 | struct USBAutoFilter match; |
| 88 | char *hostdevice; |
| 89 | int32_t bootindex; |
| 90 | uint32_t iso_urb_count; |
| 91 | uint32_t iso_urb_frames; |
| 92 | uint32_t options; |
| 93 | uint32_t loglevel; |
| 94 | bool needs_autoscan; |
| 95 | bool allow_one_guest_reset; |
| 96 | bool allow_all_guest_resets; |
| 97 | bool suppress_remote_wake; |
| 98 | |
| 99 | /* state */ |
| 100 | QTAILQ_ENTRY(USBHostDevice) next; |
| 101 | int seen, errcount; |
| 102 | int bus_num; |
| 103 | int addr; |
| 104 | char port[16]; |
| 105 | |
| 106 | int hostfd; |
| 107 | libusb_device *dev; |
| 108 | libusb_device_handle *dh; |
| 109 | struct libusb_device_descriptor ddesc; |
| 110 | |
| 111 | struct { |
| 112 | bool detached; |
| 113 | bool claimed; |
| 114 | } ifs[USB_MAX_INTERFACES]; |
| 115 | |
| 116 | /* callbacks & friends */ |
| 117 | QEMUBH *bh_nodev; |
| 118 | QEMUBH *bh_postld; |
| 119 | bool bh_postld_pending; |
| 120 | Notifier exit; |
| 121 | |
| 122 | /* request queues */ |
| 123 | QTAILQ_HEAD(, USBHostRequest) requests; |
| 124 | QTAILQ_HEAD(, USBHostIsoRing) isorings; |
| 125 | }; |
| 126 | |
| 127 | struct USBHostRequest { |
| 128 | USBHostDevice *host; |
| 129 | USBPacket *p; |
| 130 | bool in; |
| 131 | struct libusb_transfer *xfer; |
| 132 | unsigned char *buffer; |
| 133 | unsigned char *cbuf; |
| 134 | unsigned int clen; |
| 135 | bool usb3ep0quirk; |
| 136 | QTAILQ_ENTRY(USBHostRequest) next; |
| 137 | }; |
| 138 | |
| 139 | struct USBHostIsoXfer { |
| 140 | USBHostIsoRing *ring; |
| 141 | struct libusb_transfer *xfer; |
| 142 | bool copy_complete; |
| 143 | unsigned int packet; |
| 144 | QTAILQ_ENTRY(USBHostIsoXfer) next; |
| 145 | }; |
| 146 | |
| 147 | struct USBHostIsoRing { |
| 148 | USBHostDevice *host; |
| 149 | USBEndpoint *ep; |
| 150 | QTAILQ_HEAD(, USBHostIsoXfer) unused; |
| 151 | QTAILQ_HEAD(, USBHostIsoXfer) inflight; |
| 152 | QTAILQ_HEAD(, USBHostIsoXfer) copy; |
| 153 | QTAILQ_ENTRY(USBHostIsoRing) next; |
| 154 | }; |
| 155 | |
| 156 | static QTAILQ_HEAD(, USBHostDevice) hostdevs = |
| 157 | QTAILQ_HEAD_INITIALIZER(hostdevs); |
| 158 | |
| 159 | static void usb_host_auto_check(void *unused); |
| 160 | static void usb_host_release_interfaces(USBHostDevice *s); |
| 161 | static void usb_host_nodev(USBHostDevice *s); |
| 162 | static void usb_host_detach_kernel(USBHostDevice *s); |
| 163 | static void usb_host_attach_kernel(USBHostDevice *s); |
| 164 | |
| 165 | /* ------------------------------------------------------------------------ */ |
| 166 | |
| 167 | #ifndef LIBUSB_LOG_LEVEL_WARNING /* older libusb didn't define these */ |
| 168 | #define LIBUSB_LOG_LEVEL_WARNING 2 |
| 169 | #endif |
| 170 | |
| 171 | /* ------------------------------------------------------------------------ */ |
| 172 | |
| 173 | #define CONTROL_TIMEOUT 10000 /* 10 sec */ |
| 174 | #define BULK_TIMEOUT 0 /* unlimited */ |
| 175 | #define INTR_TIMEOUT 0 /* unlimited */ |
| 176 | |
| 177 | #ifndef LIBUSB_API_VERSION |
| 178 | # define LIBUSB_API_VERSION LIBUSBX_API_VERSION |
| 179 | #endif |
| 180 | #if LIBUSB_API_VERSION >= 0x01000103 |
| 181 | # define HAVE_STREAMS 1 |
| 182 | #endif |
| 183 | #if LIBUSB_API_VERSION >= 0x01000106 |
| 184 | # define HAVE_SUPER_PLUS 1 |
| 185 | #endif |
| 186 | |
| 187 | #ifdef CONFIG_HMP |
| 188 | static const char *speed_name[] = { |
| 189 | [LIBUSB_SPEED_UNKNOWN] = "?", |
| 190 | [LIBUSB_SPEED_LOW] = "1.5", |
| 191 | [LIBUSB_SPEED_FULL] = "12", |
| 192 | [LIBUSB_SPEED_HIGH] = "480", |
| 193 | [LIBUSB_SPEED_SUPER] = "5000", |
| 194 | #ifdef HAVE_SUPER_PLUS |
| 195 | [LIBUSB_SPEED_SUPER_PLUS] = "5000+", |
| 196 | #endif |
| 197 | }; |
| 198 | #endif |
| 199 | |
| 200 | static const unsigned int speed_map[] = { |
| 201 | [LIBUSB_SPEED_LOW] = USB_SPEED_LOW, |
| 202 | [LIBUSB_SPEED_FULL] = USB_SPEED_FULL, |
| 203 | [LIBUSB_SPEED_HIGH] = USB_SPEED_HIGH, |
| 204 | [LIBUSB_SPEED_SUPER] = USB_SPEED_SUPER, |
| 205 | #ifdef HAVE_SUPER_PLUS |
| 206 | [LIBUSB_SPEED_SUPER_PLUS] = USB_SPEED_SUPER, |
| 207 | #endif |
| 208 | }; |
| 209 | |
| 210 | static const unsigned int status_map[] = { |
| 211 | [LIBUSB_TRANSFER_COMPLETED] = USB_RET_SUCCESS, |
| 212 | [LIBUSB_TRANSFER_ERROR] = USB_RET_IOERROR, |
| 213 | [LIBUSB_TRANSFER_TIMED_OUT] = USB_RET_IOERROR, |
| 214 | [LIBUSB_TRANSFER_CANCELLED] = USB_RET_IOERROR, |
| 215 | [LIBUSB_TRANSFER_STALL] = USB_RET_STALL, |
| 216 | [LIBUSB_TRANSFER_NO_DEVICE] = USB_RET_NODEV, |
| 217 | [LIBUSB_TRANSFER_OVERFLOW] = USB_RET_BABBLE, |
| 218 | }; |
| 219 | |
| 220 | static const char *err_names[] = { |
| 221 | [-LIBUSB_ERROR_IO] = "IO", |
| 222 | [-LIBUSB_ERROR_INVALID_PARAM] = "INVALID_PARAM", |
| 223 | [-LIBUSB_ERROR_ACCESS] = "ACCESS", |
| 224 | [-LIBUSB_ERROR_NO_DEVICE] = "NO_DEVICE", |
| 225 | [-LIBUSB_ERROR_NOT_FOUND] = "NOT_FOUND", |
| 226 | [-LIBUSB_ERROR_BUSY] = "BUSY", |
| 227 | [-LIBUSB_ERROR_TIMEOUT] = "TIMEOUT", |
| 228 | [-LIBUSB_ERROR_OVERFLOW] = "OVERFLOW", |
| 229 | [-LIBUSB_ERROR_PIPE] = "PIPE", |
| 230 | [-LIBUSB_ERROR_INTERRUPTED] = "INTERRUPTED", |
| 231 | [-LIBUSB_ERROR_NO_MEM] = "NO_MEM", |
| 232 | [-LIBUSB_ERROR_NOT_SUPPORTED] = "NOT_SUPPORTED", |
| 233 | [-LIBUSB_ERROR_OTHER] = "OTHER", |
| 234 | }; |
| 235 | |
| 236 | static libusb_context *ctx; |
| 237 | static uint32_t loglevel; |
| 238 | |
| 239 | #ifndef CONFIG_WIN32 |
| 240 | |
| 241 | static void usb_host_handle_fd(void *opaque) |
| 242 | { |
| 243 | struct timeval tv = { 0, 0 }; |
| 244 | libusb_handle_events_timeout(ctx, &tv); |
| 245 | } |
| 246 | |
| 247 | static void usb_host_add_fd(int fd, short events, void *user_data) |
| 248 | { |
| 249 | qemu_set_fd_handler(fd, |
| 250 | (events & POLLIN) ? usb_host_handle_fd : NULL, |
| 251 | (events & POLLOUT) ? usb_host_handle_fd : NULL, |
| 252 | ctx); |
| 253 | } |
| 254 | |
| 255 | static void usb_host_del_fd(int fd, void *user_data) |
| 256 | { |
| 257 | qemu_set_fd_handler(fd, NULL, NULL, NULL); |
| 258 | } |
| 259 | |
| 260 | #else |
| 261 | |
| 262 | static QEMUTimer *poll_timer; |
| 263 | static uint32_t request_count; |
| 264 | |
| 265 | static void usb_host_timer_kick(void) |
| 266 | { |
| 267 | int64_t delay_ns; |
| 268 | |
| 269 | delay_ns = request_count |
| 270 | ? (NANOSECONDS_PER_SECOND / 100) /* 10 ms interval with active req */ |
| 271 | : (NANOSECONDS_PER_SECOND); /* 1 sec interval otherwise */ |
| 272 | timer_mod(poll_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + delay_ns); |
| 273 | } |
| 274 | |
| 275 | static void usb_host_timer(void *opaque) |
| 276 | { |
| 277 | struct timeval tv = { 0, 0 }; |
| 278 | |
| 279 | libusb_handle_events_timeout(ctx, &tv); |
| 280 | usb_host_timer_kick(); |
| 281 | } |
| 282 | |
| 283 | #endif /* !CONFIG_WIN32 */ |
| 284 | |
| 285 | static int usb_host_init(void) |
| 286 | { |
| 287 | #ifndef CONFIG_WIN32 |
| 288 | const struct libusb_pollfd **poll; |
| 289 | #endif |
| 290 | int rc; |
| 291 | |
| 292 | if (ctx) { |
| 293 | return 0; |
| 294 | } |
| 295 | rc = libusb_init(&ctx); |
| 296 | if (rc != 0) { |
| 297 | return -1; |
| 298 | } |
| 299 | #if LIBUSB_API_VERSION >= 0x01000106 |
| 300 | libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, loglevel); |
| 301 | #else |
| 302 | libusb_set_debug(ctx, loglevel); |
| 303 | #endif |
| 304 | #ifdef CONFIG_WIN32 |
| 305 | poll_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, usb_host_timer, NULL); |
| 306 | usb_host_timer_kick(); |
| 307 | #else |
| 308 | libusb_set_pollfd_notifiers(ctx, usb_host_add_fd, |
| 309 | usb_host_del_fd, |
| 310 | ctx); |
| 311 | poll = libusb_get_pollfds(ctx); |
| 312 | if (poll) { |
| 313 | int i; |
| 314 | for (i = 0; poll[i] != NULL; i++) { |
| 315 | usb_host_add_fd(poll[i]->fd, poll[i]->events, ctx); |
| 316 | } |
| 317 | } |
| 318 | free(poll); |
| 319 | #endif |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | static int usb_host_get_port(libusb_device *dev, char *port, size_t len) |
| 324 | { |
| 325 | uint8_t path[7]; |
| 326 | size_t off; |
| 327 | int rc, i; |
| 328 | |
| 329 | #if LIBUSB_API_VERSION >= 0x01000102 |
| 330 | rc = libusb_get_port_numbers(dev, path, 7); |
| 331 | #else |
| 332 | rc = libusb_get_port_path(ctx, dev, path, 7); |
| 333 | #endif |
| 334 | if (rc < 0) { |
| 335 | return 0; |
| 336 | } |
| 337 | off = snprintf(port, len, "%d", path[0]); |
| 338 | for (i = 1; i < rc; i++) { |
| 339 | off += snprintf(port+off, len-off, ".%d", path[i]); |
| 340 | } |
| 341 | return off; |
| 342 | } |
| 343 | |
| 344 | static void usb_host_libusb_error(const char *func, int rc) |
| 345 | { |
| 346 | const char *errname; |
| 347 | |
| 348 | if (rc >= 0) { |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | if (-rc < ARRAY_SIZE(err_names) && err_names[-rc]) { |
| 353 | errname = err_names[-rc]; |
| 354 | } else { |
| 355 | errname = "?"; |
| 356 | } |
| 357 | error_report("%s: %d [%s]", func, rc, errname); |
| 358 | } |
| 359 | |
| 360 | /* ------------------------------------------------------------------------ */ |
| 361 | |
| 362 | static bool usb_host_use_combining(USBEndpoint *ep) |
| 363 | { |
| 364 | int type; |
| 365 | |
| 366 | if (!ep->pipeline) { |
| 367 | return false; |
| 368 | } |
| 369 | if (ep->pid != USB_TOKEN_IN) { |
| 370 | return false; |
| 371 | } |
| 372 | type = usb_ep_get_type(ep->dev, ep->pid, ep->nr); |
| 373 | if (type != USB_ENDPOINT_XFER_BULK) { |
| 374 | return false; |
| 375 | } |
| 376 | return true; |
| 377 | } |
| 378 | |
| 379 | /* ------------------------------------------------------------------------ */ |
| 380 | |
| 381 | static USBHostRequest *usb_host_req_alloc(USBHostDevice *s, USBPacket *p, |
| 382 | bool in, size_t bufsize) |
| 383 | { |
| 384 | USBHostRequest *r = g_new0(USBHostRequest, 1); |
| 385 | |
| 386 | r->host = s; |
| 387 | r->p = p; |
| 388 | r->in = in; |
| 389 | r->xfer = libusb_alloc_transfer(0); |
| 390 | if (bufsize) { |
| 391 | r->buffer = g_malloc(bufsize); |
| 392 | } |
| 393 | QTAILQ_INSERT_TAIL(&s->requests, r, next); |
| 394 | #ifdef CONFIG_WIN32 |
| 395 | request_count++; |
| 396 | usb_host_timer_kick(); |
| 397 | #endif |
| 398 | return r; |
| 399 | } |
| 400 | |
| 401 | static void usb_host_req_free(USBHostRequest *r) |
| 402 | { |
| 403 | #ifdef CONFIG_WIN32 |
| 404 | request_count--; |
| 405 | #endif |
| 406 | QTAILQ_REMOVE(&r->host->requests, r, next); |
| 407 | libusb_free_transfer(r->xfer); |
| 408 | g_free(r->buffer); |
| 409 | g_free(r); |
| 410 | } |
| 411 | |
| 412 | static USBHostRequest *usb_host_req_find(USBHostDevice *s, USBPacket *p) |
| 413 | { |
| 414 | USBHostRequest *r; |
| 415 | |
| 416 | QTAILQ_FOREACH(r, &s->requests, next) { |
| 417 | if (r->p == p) { |
| 418 | return r; |
| 419 | } |
| 420 | } |
| 421 | return NULL; |
| 422 | } |
| 423 | |
| 424 | static void LIBUSB_CALL usb_host_req_complete_ctrl(struct libusb_transfer *xfer) |
| 425 | { |
| 426 | USBHostRequest *r = xfer->user_data; |
| 427 | USBHostDevice *s = r->host; |
| 428 | bool disconnect = (xfer->status == LIBUSB_TRANSFER_NO_DEVICE); |
| 429 | |
| 430 | if (r->p == NULL) { |
| 431 | goto out; /* request was canceled */ |
| 432 | } |
| 433 | |
| 434 | r->p->status = status_map[xfer->status]; |
| 435 | r->p->actual_length = xfer->actual_length; |
| 436 | if (r->in && xfer->actual_length) { |
| 437 | USBDevice *udev = USB_DEVICE(s); |
| 438 | struct libusb_config_descriptor *conf = (void *)r->cbuf; |
| 439 | memcpy(r->cbuf, r->buffer + 8, xfer->actual_length); |
| 440 | |
| 441 | /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices |
| 442 | * to work redirected to a not superspeed capable hcd */ |
| 443 | if (r->usb3ep0quirk && xfer->actual_length >= 18 && |
| 444 | r->cbuf[7] == 9) { |
| 445 | r->cbuf[7] = 64; |
| 446 | } |
| 447 | /* |
| 448 | *If this is GET_DESCRIPTOR request for configuration descriptor, |
| 449 | * remove 'remote wakeup' flag from it to prevent idle power down |
| 450 | * in Windows guest |
| 451 | */ |
| 452 | if (s->suppress_remote_wake && |
| 453 | udev->setup_buf[0] == USB_DIR_IN && |
| 454 | udev->setup_buf[1] == USB_REQ_GET_DESCRIPTOR && |
| 455 | udev->setup_buf[3] == USB_DT_CONFIG && udev->setup_buf[2] == 0 && |
| 456 | xfer->actual_length > |
| 457 | offsetof(struct libusb_config_descriptor, bmAttributes) && |
| 458 | (conf->bmAttributes & USB_CFG_ATT_WAKEUP)) { |
| 459 | trace_usb_host_remote_wakeup_removed(s->bus_num, s->addr); |
| 460 | conf->bmAttributes &= ~USB_CFG_ATT_WAKEUP; |
| 461 | } |
| 462 | } |
| 463 | trace_usb_host_req_complete(s->bus_num, s->addr, r->p, |
| 464 | r->p->status, r->p->actual_length); |
| 465 | usb_generic_async_ctrl_complete(USB_DEVICE(s), r->p); |
| 466 | |
| 467 | out: |
| 468 | usb_host_req_free(r); |
| 469 | if (disconnect) { |
| 470 | usb_host_nodev(s); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | static void LIBUSB_CALL usb_host_req_complete_data(struct libusb_transfer *xfer) |
| 475 | { |
| 476 | USBHostRequest *r = xfer->user_data; |
| 477 | USBHostDevice *s = r->host; |
| 478 | bool disconnect = (xfer->status == LIBUSB_TRANSFER_NO_DEVICE); |
| 479 | |
| 480 | if (r->p == NULL) { |
| 481 | goto out; /* request was canceled */ |
| 482 | } |
| 483 | |
| 484 | r->p->status = status_map[xfer->status]; |
| 485 | if (r->in && xfer->actual_length) { |
| 486 | usb_packet_copy(r->p, r->buffer, xfer->actual_length); |
| 487 | } |
| 488 | trace_usb_host_req_complete(s->bus_num, s->addr, r->p, |
| 489 | r->p->status, r->p->actual_length); |
| 490 | if (usb_host_use_combining(r->p->ep)) { |
| 491 | usb_combined_input_packet_complete(USB_DEVICE(s), r->p); |
| 492 | } else { |
| 493 | usb_packet_complete(USB_DEVICE(s), r->p); |
| 494 | } |
| 495 | |
| 496 | out: |
| 497 | usb_host_req_free(r); |
| 498 | if (disconnect) { |
| 499 | usb_host_nodev(s); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | static void usb_host_req_abort(USBHostRequest *r) |
| 504 | { |
| 505 | USBHostDevice *s = r->host; |
| 506 | bool inflight = (r->p && r->p->state == USB_PACKET_ASYNC); |
| 507 | |
| 508 | if (inflight) { |
| 509 | r->p->status = USB_RET_NODEV; |
| 510 | trace_usb_host_req_complete(s->bus_num, s->addr, r->p, |
| 511 | r->p->status, r->p->actual_length); |
| 512 | if (r->p->ep->nr == 0) { |
| 513 | usb_generic_async_ctrl_complete(USB_DEVICE(s), r->p); |
| 514 | } else { |
| 515 | usb_packet_complete(USB_DEVICE(s), r->p); |
| 516 | } |
| 517 | r->p = NULL; |
| 518 | |
| 519 | libusb_cancel_transfer(r->xfer); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | /* ------------------------------------------------------------------------ */ |
| 524 | |
| 525 | static void LIBUSB_CALL |
| 526 | usb_host_req_complete_iso(struct libusb_transfer *transfer) |
| 527 | { |
| 528 | USBHostIsoXfer *xfer = transfer->user_data; |
| 529 | |
| 530 | if (!xfer) { |
| 531 | /* USBHostIsoXfer released while inflight */ |
| 532 | g_free(transfer->buffer); |
| 533 | libusb_free_transfer(transfer); |
| 534 | return; |
| 535 | } |
| 536 | |
| 537 | QTAILQ_REMOVE(&xfer->ring->inflight, xfer, next); |
| 538 | if (QTAILQ_EMPTY(&xfer->ring->inflight)) { |
| 539 | USBHostDevice *s = xfer->ring->host; |
| 540 | trace_usb_host_iso_stop(s->bus_num, s->addr, xfer->ring->ep->nr); |
| 541 | } |
| 542 | if (xfer->ring->ep->pid == USB_TOKEN_IN) { |
| 543 | QTAILQ_INSERT_TAIL(&xfer->ring->copy, xfer, next); |
| 544 | usb_wakeup(xfer->ring->ep, 0); |
| 545 | } else { |
| 546 | QTAILQ_INSERT_TAIL(&xfer->ring->unused, xfer, next); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | static USBHostIsoRing *usb_host_iso_alloc(USBHostDevice *s, USBEndpoint *ep) |
| 551 | { |
| 552 | USBHostIsoRing *ring = g_new0(USBHostIsoRing, 1); |
| 553 | USBHostIsoXfer *xfer; |
| 554 | /* FIXME: check interval (for now assume one xfer per frame) */ |
| 555 | int packets = s->iso_urb_frames; |
| 556 | int i; |
| 557 | |
| 558 | ring->host = s; |
| 559 | ring->ep = ep; |
| 560 | QTAILQ_INIT(&ring->unused); |
| 561 | QTAILQ_INIT(&ring->inflight); |
| 562 | QTAILQ_INIT(&ring->copy); |
| 563 | QTAILQ_INSERT_TAIL(&s->isorings, ring, next); |
| 564 | |
| 565 | for (i = 0; i < s->iso_urb_count; i++) { |
| 566 | xfer = g_new0(USBHostIsoXfer, 1); |
| 567 | xfer->ring = ring; |
| 568 | xfer->xfer = libusb_alloc_transfer(packets); |
| 569 | xfer->xfer->dev_handle = s->dh; |
| 570 | xfer->xfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS; |
| 571 | |
| 572 | xfer->xfer->endpoint = ring->ep->nr; |
| 573 | if (ring->ep->pid == USB_TOKEN_IN) { |
| 574 | xfer->xfer->endpoint |= USB_DIR_IN; |
| 575 | } |
| 576 | xfer->xfer->callback = usb_host_req_complete_iso; |
| 577 | xfer->xfer->user_data = xfer; |
| 578 | |
| 579 | xfer->xfer->num_iso_packets = packets; |
| 580 | xfer->xfer->length = ring->ep->max_packet_size * packets; |
| 581 | xfer->xfer->buffer = g_malloc0(xfer->xfer->length); |
| 582 | |
| 583 | QTAILQ_INSERT_TAIL(&ring->unused, xfer, next); |
| 584 | } |
| 585 | |
| 586 | return ring; |
| 587 | } |
| 588 | |
| 589 | static USBHostIsoRing *usb_host_iso_find(USBHostDevice *s, USBEndpoint *ep) |
| 590 | { |
| 591 | USBHostIsoRing *ring; |
| 592 | |
| 593 | QTAILQ_FOREACH(ring, &s->isorings, next) { |
| 594 | if (ring->ep == ep) { |
| 595 | return ring; |
| 596 | } |
| 597 | } |
| 598 | return NULL; |
| 599 | } |
| 600 | |
| 601 | static void usb_host_iso_reset_xfer(USBHostIsoXfer *xfer) |
| 602 | { |
| 603 | libusb_set_iso_packet_lengths(xfer->xfer, |
| 604 | xfer->ring->ep->max_packet_size); |
| 605 | xfer->packet = 0; |
| 606 | xfer->copy_complete = false; |
| 607 | } |
| 608 | |
| 609 | static void usb_host_iso_free_xfer(USBHostIsoXfer *xfer, bool inflight) |
| 610 | { |
| 611 | if (inflight) { |
| 612 | xfer->xfer->user_data = NULL; |
| 613 | } else { |
| 614 | g_free(xfer->xfer->buffer); |
| 615 | libusb_free_transfer(xfer->xfer); |
| 616 | } |
| 617 | g_free(xfer); |
| 618 | } |
| 619 | |
| 620 | static void usb_host_iso_free(USBHostIsoRing *ring) |
| 621 | { |
| 622 | USBHostIsoXfer *xfer; |
| 623 | |
| 624 | while ((xfer = QTAILQ_FIRST(&ring->inflight)) != NULL) { |
| 625 | QTAILQ_REMOVE(&ring->inflight, xfer, next); |
| 626 | usb_host_iso_free_xfer(xfer, true); |
| 627 | } |
| 628 | while ((xfer = QTAILQ_FIRST(&ring->unused)) != NULL) { |
| 629 | QTAILQ_REMOVE(&ring->unused, xfer, next); |
| 630 | usb_host_iso_free_xfer(xfer, false); |
| 631 | } |
| 632 | while ((xfer = QTAILQ_FIRST(&ring->copy)) != NULL) { |
| 633 | QTAILQ_REMOVE(&ring->copy, xfer, next); |
| 634 | usb_host_iso_free_xfer(xfer, false); |
| 635 | } |
| 636 | |
| 637 | QTAILQ_REMOVE(&ring->host->isorings, ring, next); |
| 638 | g_free(ring); |
| 639 | } |
| 640 | |
| 641 | static void usb_host_iso_free_all(USBHostDevice *s) |
| 642 | { |
| 643 | USBHostIsoRing *ring; |
| 644 | |
| 645 | while ((ring = QTAILQ_FIRST(&s->isorings)) != NULL) { |
| 646 | usb_host_iso_free(ring); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | static bool usb_host_iso_data_copy(USBHostIsoXfer *xfer, USBPacket *p) |
| 651 | { |
| 652 | unsigned int psize; |
| 653 | unsigned char *buf; |
| 654 | |
| 655 | buf = libusb_get_iso_packet_buffer_simple(xfer->xfer, xfer->packet); |
| 656 | if (p->pid == USB_TOKEN_OUT) { |
| 657 | psize = p->iov.size; |
| 658 | if (psize > xfer->ring->ep->max_packet_size) { |
| 659 | /* should not happen (guest bug) */ |
| 660 | psize = xfer->ring->ep->max_packet_size; |
| 661 | } |
| 662 | xfer->xfer->iso_packet_desc[xfer->packet].length = psize; |
| 663 | } else { |
| 664 | psize = xfer->xfer->iso_packet_desc[xfer->packet].actual_length; |
| 665 | if (psize > p->iov.size) { |
| 666 | /* should not happen (guest bug) */ |
| 667 | psize = p->iov.size; |
| 668 | } |
| 669 | } |
| 670 | usb_packet_copy(p, buf, psize); |
| 671 | xfer->packet++; |
| 672 | xfer->copy_complete = (xfer->packet == xfer->xfer->num_iso_packets); |
| 673 | return xfer->copy_complete; |
| 674 | } |
| 675 | |
| 676 | static void usb_host_iso_data_in(USBHostDevice *s, USBPacket *p) |
| 677 | { |
| 678 | USBHostIsoRing *ring; |
| 679 | USBHostIsoXfer *xfer; |
| 680 | bool disconnect = false; |
| 681 | int rc; |
| 682 | |
| 683 | ring = usb_host_iso_find(s, p->ep); |
| 684 | if (ring == NULL) { |
| 685 | ring = usb_host_iso_alloc(s, p->ep); |
| 686 | } |
| 687 | |
| 688 | /* copy data to guest */ |
| 689 | xfer = QTAILQ_FIRST(&ring->copy); |
| 690 | if (xfer != NULL) { |
| 691 | if (usb_host_iso_data_copy(xfer, p)) { |
| 692 | QTAILQ_REMOVE(&ring->copy, xfer, next); |
| 693 | QTAILQ_INSERT_TAIL(&ring->unused, xfer, next); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | /* submit empty bufs to host */ |
| 698 | while ((xfer = QTAILQ_FIRST(&ring->unused)) != NULL) { |
| 699 | QTAILQ_REMOVE(&ring->unused, xfer, next); |
| 700 | usb_host_iso_reset_xfer(xfer); |
| 701 | rc = libusb_submit_transfer(xfer->xfer); |
| 702 | if (rc != 0) { |
| 703 | usb_host_libusb_error("libusb_submit_transfer [iso]", rc); |
| 704 | QTAILQ_INSERT_TAIL(&ring->unused, xfer, next); |
| 705 | if (rc == LIBUSB_ERROR_NO_DEVICE) { |
| 706 | disconnect = true; |
| 707 | } |
| 708 | break; |
| 709 | } |
| 710 | if (QTAILQ_EMPTY(&ring->inflight)) { |
| 711 | trace_usb_host_iso_start(s->bus_num, s->addr, p->ep->nr); |
| 712 | } |
| 713 | QTAILQ_INSERT_TAIL(&ring->inflight, xfer, next); |
| 714 | } |
| 715 | |
| 716 | if (disconnect) { |
| 717 | usb_host_nodev(s); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | static void usb_host_iso_data_out(USBHostDevice *s, USBPacket *p) |
| 722 | { |
| 723 | USBHostIsoRing *ring; |
| 724 | USBHostIsoXfer *xfer; |
| 725 | bool disconnect = false; |
| 726 | int rc, filled = 0; |
| 727 | |
| 728 | ring = usb_host_iso_find(s, p->ep); |
| 729 | if (ring == NULL) { |
| 730 | ring = usb_host_iso_alloc(s, p->ep); |
| 731 | } |
| 732 | |
| 733 | /* copy data from guest */ |
| 734 | xfer = QTAILQ_FIRST(&ring->copy); |
| 735 | while (xfer != NULL && xfer->copy_complete) { |
| 736 | filled++; |
| 737 | xfer = QTAILQ_NEXT(xfer, next); |
| 738 | } |
| 739 | if (xfer == NULL) { |
| 740 | xfer = QTAILQ_FIRST(&ring->unused); |
| 741 | if (xfer == NULL) { |
| 742 | trace_usb_host_iso_out_of_bufs(s->bus_num, s->addr, p->ep->nr); |
| 743 | return; |
| 744 | } |
| 745 | QTAILQ_REMOVE(&ring->unused, xfer, next); |
| 746 | usb_host_iso_reset_xfer(xfer); |
| 747 | QTAILQ_INSERT_TAIL(&ring->copy, xfer, next); |
| 748 | } |
| 749 | usb_host_iso_data_copy(xfer, p); |
| 750 | |
| 751 | if (QTAILQ_EMPTY(&ring->inflight)) { |
| 752 | /* wait until half of our buffers are filled |
| 753 | before kicking the iso out stream */ |
| 754 | if (filled*2 < s->iso_urb_count) { |
| 755 | return; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | /* submit filled bufs to host */ |
| 760 | while ((xfer = QTAILQ_FIRST(&ring->copy)) != NULL && |
| 761 | xfer->copy_complete) { |
| 762 | QTAILQ_REMOVE(&ring->copy, xfer, next); |
| 763 | rc = libusb_submit_transfer(xfer->xfer); |
| 764 | if (rc != 0) { |
| 765 | usb_host_libusb_error("libusb_submit_transfer [iso]", rc); |
| 766 | QTAILQ_INSERT_TAIL(&ring->unused, xfer, next); |
| 767 | if (rc == LIBUSB_ERROR_NO_DEVICE) { |
| 768 | disconnect = true; |
| 769 | } |
| 770 | break; |
| 771 | } |
| 772 | if (QTAILQ_EMPTY(&ring->inflight)) { |
| 773 | trace_usb_host_iso_start(s->bus_num, s->addr, p->ep->nr); |
| 774 | } |
| 775 | QTAILQ_INSERT_TAIL(&ring->inflight, xfer, next); |
| 776 | } |
| 777 | |
| 778 | if (disconnect) { |
| 779 | usb_host_nodev(s); |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | /* ------------------------------------------------------------------------ */ |
| 784 | |
| 785 | static void usb_host_speed_compat(USBHostDevice *s) |
| 786 | { |
| 787 | USBDevice *udev = USB_DEVICE(s); |
| 788 | struct libusb_config_descriptor *conf; |
| 789 | const struct libusb_interface_descriptor *intf; |
| 790 | const struct libusb_endpoint_descriptor *endp; |
| 791 | #ifdef HAVE_STREAMS |
| 792 | struct libusb_ss_endpoint_companion_descriptor *endp_ss_comp; |
| 793 | #endif |
| 794 | bool compat_high = true; |
| 795 | bool compat_full = true; |
| 796 | uint8_t type; |
| 797 | int rc, c, i, a, e; |
| 798 | |
| 799 | for (c = 0;; c++) { |
| 800 | rc = libusb_get_config_descriptor(s->dev, c, &conf); |
| 801 | if (rc != 0) { |
| 802 | break; |
| 803 | } |
| 804 | for (i = 0; i < conf->bNumInterfaces; i++) { |
| 805 | for (a = 0; a < conf->interface[i].num_altsetting; a++) { |
| 806 | intf = &conf->interface[i].altsetting[a]; |
| 807 | |
| 808 | if (intf->bInterfaceClass == LIBUSB_CLASS_MASS_STORAGE && |
| 809 | intf->bInterfaceSubClass == 6) { /* SCSI */ |
| 810 | udev->flags |= (1 << USB_DEV_FLAG_IS_SCSI_STORAGE); |
| 811 | break; |
| 812 | } |
| 813 | |
| 814 | for (e = 0; e < intf->bNumEndpoints; e++) { |
| 815 | endp = &intf->endpoint[e]; |
| 816 | type = endp->bmAttributes & 0x3; |
| 817 | switch (type) { |
| 818 | case 0x01: /* ISO */ |
| 819 | compat_full = false; |
| 820 | compat_high = false; |
| 821 | break; |
| 822 | case 0x02: /* BULK */ |
| 823 | #ifdef HAVE_STREAMS |
| 824 | rc = libusb_get_ss_endpoint_companion_descriptor |
| 825 | (ctx, endp, &endp_ss_comp); |
| 826 | if (rc == LIBUSB_SUCCESS) { |
| 827 | int streams = endp_ss_comp->bmAttributes & 0x1f; |
| 828 | if (streams) { |
| 829 | compat_full = false; |
| 830 | compat_high = false; |
| 831 | } |
| 832 | libusb_free_ss_endpoint_companion_descriptor |
| 833 | (endp_ss_comp); |
| 834 | } |
| 835 | #endif |
| 836 | break; |
| 837 | case 0x03: /* INTERRUPT */ |
| 838 | if (endp->wMaxPacketSize > 64) { |
| 839 | compat_full = false; |
| 840 | } |
| 841 | if (endp->wMaxPacketSize > 1024) { |
| 842 | compat_high = false; |
| 843 | } |
| 844 | break; |
| 845 | } |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | libusb_free_config_descriptor(conf); |
| 850 | } |
| 851 | |
| 852 | udev->speedmask = (1 << udev->speed); |
| 853 | if (udev->speed == USB_SPEED_SUPER && compat_high) { |
| 854 | udev->speedmask |= USB_SPEED_MASK_HIGH; |
| 855 | } |
| 856 | if (udev->speed == USB_SPEED_SUPER && compat_full) { |
| 857 | udev->speedmask |= USB_SPEED_MASK_FULL; |
| 858 | } |
| 859 | if (udev->speed == USB_SPEED_HIGH && compat_full) { |
| 860 | udev->speedmask |= USB_SPEED_MASK_FULL; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | static void usb_host_ep_update(USBHostDevice *s) |
| 865 | { |
| 866 | static const char *tname[] = { |
| 867 | [USB_ENDPOINT_XFER_CONTROL] = "control", |
| 868 | [USB_ENDPOINT_XFER_ISOC] = "isoc", |
| 869 | [USB_ENDPOINT_XFER_BULK] = "bulk", |
| 870 | [USB_ENDPOINT_XFER_INT] = "int", |
| 871 | }; |
| 872 | USBDevice *udev = USB_DEVICE(s); |
| 873 | struct libusb_config_descriptor *conf; |
| 874 | const struct libusb_interface_descriptor *intf; |
| 875 | const struct libusb_endpoint_descriptor *endp; |
| 876 | #ifdef HAVE_STREAMS |
| 877 | struct libusb_ss_endpoint_companion_descriptor *endp_ss_comp; |
| 878 | #endif |
| 879 | uint8_t devep, type; |
| 880 | int pid, ep, alt; |
| 881 | int rc, i, e; |
| 882 | |
| 883 | usb_ep_reset(udev); |
| 884 | rc = libusb_get_active_config_descriptor(s->dev, &conf); |
| 885 | if (rc != 0) { |
| 886 | return; |
| 887 | } |
| 888 | trace_usb_host_parse_config(s->bus_num, s->addr, |
| 889 | conf->bConfigurationValue, true); |
| 890 | |
| 891 | for (i = 0; i < conf->bNumInterfaces; i++) { |
| 892 | /* |
| 893 | * The udev->altsetting array indexes alternate settings |
| 894 | * by the interface number. Get the 0th alternate setting |
| 895 | * first so that we can grab the interface number, and |
| 896 | * then correct the alternate setting value if necessary. |
| 897 | */ |
| 898 | intf = &conf->interface[i].altsetting[0]; |
| 899 | alt = udev->altsetting[intf->bInterfaceNumber]; |
| 900 | |
| 901 | if (alt != 0) { |
| 902 | assert(alt < conf->interface[i].num_altsetting); |
| 903 | intf = &conf->interface[i].altsetting[alt]; |
| 904 | } |
| 905 | |
| 906 | trace_usb_host_parse_interface(s->bus_num, s->addr, |
| 907 | intf->bInterfaceNumber, |
| 908 | intf->bAlternateSetting, true); |
| 909 | for (e = 0; e < intf->bNumEndpoints; e++) { |
| 910 | endp = &intf->endpoint[e]; |
| 911 | |
| 912 | devep = endp->bEndpointAddress; |
| 913 | pid = (devep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT; |
| 914 | ep = devep & 0xf; |
| 915 | type = endp->bmAttributes & 0x3; |
| 916 | |
| 917 | if (ep == 0) { |
| 918 | trace_usb_host_parse_error(s->bus_num, s->addr, |
| 919 | "invalid endpoint address"); |
| 920 | return; |
| 921 | } |
| 922 | if (usb_ep_get_type(udev, pid, ep) != USB_ENDPOINT_XFER_INVALID) { |
| 923 | trace_usb_host_parse_error(s->bus_num, s->addr, |
| 924 | "duplicate endpoint address"); |
| 925 | return; |
| 926 | } |
| 927 | |
| 928 | trace_usb_host_parse_endpoint(s->bus_num, s->addr, ep, |
| 929 | (devep & USB_DIR_IN) ? "in" : "out", |
| 930 | tname[type], true); |
| 931 | usb_ep_set_max_packet_size(udev, pid, ep, |
| 932 | endp->wMaxPacketSize); |
| 933 | usb_ep_set_type(udev, pid, ep, type); |
| 934 | usb_ep_set_ifnum(udev, pid, ep, i); |
| 935 | usb_ep_set_halted(udev, pid, ep, 0); |
| 936 | #ifdef HAVE_STREAMS |
| 937 | if (type == LIBUSB_TRANSFER_TYPE_BULK && |
| 938 | libusb_get_ss_endpoint_companion_descriptor(ctx, endp, |
| 939 | &endp_ss_comp) == LIBUSB_SUCCESS) { |
| 940 | usb_ep_set_max_streams(udev, pid, ep, |
| 941 | endp_ss_comp->bmAttributes); |
| 942 | libusb_free_ss_endpoint_companion_descriptor(endp_ss_comp); |
| 943 | } |
| 944 | #endif |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | libusb_free_config_descriptor(conf); |
| 949 | } |
| 950 | |
| 951 | static int usb_host_open(USBHostDevice *s, libusb_device *dev, int hostfd) |
| 952 | { |
| 953 | USBDevice *udev = USB_DEVICE(s); |
| 954 | int libusb_speed; |
| 955 | int bus_num = 0; |
| 956 | int addr = 0; |
| 957 | int rc; |
| 958 | Error *local_err = NULL; |
| 959 | |
| 960 | if (s->bh_postld_pending) { |
| 961 | return -1; |
| 962 | } |
| 963 | if (s->dh != NULL) { |
| 964 | goto fail; |
| 965 | } |
| 966 | |
| 967 | if (dev) { |
| 968 | bus_num = libusb_get_bus_number(dev); |
| 969 | addr = libusb_get_device_address(dev); |
| 970 | trace_usb_host_open_started(bus_num, addr); |
| 971 | |
| 972 | rc = libusb_open(dev, &s->dh); |
| 973 | if (rc != 0) { |
| 974 | goto fail; |
| 975 | } |
| 976 | } else { |
| 977 | #if LIBUSB_API_VERSION >= 0x01000107 && !defined(CONFIG_WIN32) |
| 978 | trace_usb_host_open_hostfd(hostfd); |
| 979 | |
| 980 | rc = libusb_wrap_sys_device(ctx, hostfd, &s->dh); |
| 981 | if (rc != 0) { |
| 982 | goto fail; |
| 983 | } |
| 984 | s->hostfd = hostfd; |
| 985 | dev = libusb_get_device(s->dh); |
| 986 | bus_num = libusb_get_bus_number(dev); |
| 987 | addr = libusb_get_device_address(dev); |
| 988 | #else |
| 989 | g_assert_not_reached(); |
| 990 | #endif |
| 991 | } |
| 992 | |
| 993 | s->dev = dev; |
| 994 | s->bus_num = bus_num; |
| 995 | s->addr = addr; |
| 996 | |
| 997 | usb_host_detach_kernel(s); |
| 998 | |
| 999 | libusb_get_device_descriptor(dev, &s->ddesc); |
| 1000 | usb_host_get_port(s->dev, s->port, sizeof(s->port)); |
| 1001 | |
| 1002 | usb_ep_init(udev); |
| 1003 | usb_host_ep_update(s); |
| 1004 | |
| 1005 | libusb_speed = libusb_get_device_speed(dev); |
| 1006 | #if LIBUSB_API_VERSION >= 0x01000107 && defined(CONFIG_LINUX) && \ |
| 1007 | defined(USBDEVFS_GET_SPEED) |
| 1008 | if (hostfd && libusb_speed == 0) { |
| 1009 | /* |
| 1010 | * Workaround libusb bug: libusb_get_device_speed() does not |
| 1011 | * work for libusb_wrap_sys_device() devices in v1.0.23. |
| 1012 | * |
| 1013 | * Speeds are defined in linux/usb/ch9.h, file not included |
| 1014 | * due to name conflicts. |
| 1015 | */ |
| 1016 | rc = ioctl(hostfd, USBDEVFS_GET_SPEED, NULL); |
| 1017 | switch (rc) { |
| 1018 | case 1: /* low */ |
| 1019 | libusb_speed = LIBUSB_SPEED_LOW; |
| 1020 | break; |
| 1021 | case 2: /* full */ |
| 1022 | libusb_speed = LIBUSB_SPEED_FULL; |
| 1023 | break; |
| 1024 | case 3: /* high */ |
| 1025 | case 4: /* wireless */ |
| 1026 | libusb_speed = LIBUSB_SPEED_HIGH; |
| 1027 | break; |
| 1028 | case 5: /* super */ |
| 1029 | libusb_speed = LIBUSB_SPEED_SUPER; |
| 1030 | break; |
| 1031 | case 6: /* super plus */ |
| 1032 | #ifdef HAVE_SUPER_PLUS |
| 1033 | libusb_speed = LIBUSB_SPEED_SUPER_PLUS; |
| 1034 | #else |
| 1035 | libusb_speed = LIBUSB_SPEED_SUPER; |
| 1036 | #endif |
| 1037 | break; |
| 1038 | } |
| 1039 | } |
| 1040 | #endif |
| 1041 | udev->speed = speed_map[libusb_speed]; |
| 1042 | usb_host_speed_compat(s); |
| 1043 | |
| 1044 | if (s->ddesc.iProduct) { |
| 1045 | libusb_get_string_descriptor_ascii(s->dh, s->ddesc.iProduct, |
| 1046 | (unsigned char *)udev->product_desc, |
| 1047 | sizeof(udev->product_desc)); |
| 1048 | } else { |
| 1049 | snprintf(udev->product_desc, sizeof(udev->product_desc), |
| 1050 | "host:%d.%d", bus_num, addr); |
| 1051 | } |
| 1052 | |
| 1053 | usb_device_attach(udev, &local_err); |
| 1054 | if (local_err) { |
| 1055 | error_report_err(local_err); |
| 1056 | goto fail; |
| 1057 | } |
| 1058 | |
| 1059 | trace_usb_host_open_success(bus_num, addr); |
| 1060 | return 0; |
| 1061 | |
| 1062 | fail: |
| 1063 | trace_usb_host_open_failure(bus_num, addr); |
| 1064 | if (s->dh != NULL) { |
| 1065 | usb_host_release_interfaces(s); |
| 1066 | libusb_reset_device(s->dh); |
| 1067 | usb_host_attach_kernel(s); |
| 1068 | libusb_close(s->dh); |
| 1069 | s->dh = NULL; |
| 1070 | s->dev = NULL; |
| 1071 | } |
| 1072 | return -1; |
| 1073 | } |
| 1074 | |
| 1075 | static void usb_host_abort_xfers(USBHostDevice *s) |
| 1076 | { |
| 1077 | USBHostRequest *r, *rtmp; |
| 1078 | int limit = 100; |
| 1079 | |
| 1080 | QTAILQ_FOREACH_SAFE(r, &s->requests, next, rtmp) { |
| 1081 | usb_host_req_abort(r); |
| 1082 | } |
| 1083 | |
| 1084 | while (QTAILQ_FIRST(&s->requests) != NULL) { |
| 1085 | struct timeval tv; |
| 1086 | memset(&tv, 0, sizeof(tv)); |
| 1087 | tv.tv_usec = 2500; |
| 1088 | libusb_handle_events_timeout(ctx, &tv); |
| 1089 | if (--limit == 0) { |
| 1090 | /* |
| 1091 | * Don't wait forever for libusb calling the complete |
| 1092 | * callback (which will unlink and free the request). |
| 1093 | * |
| 1094 | * Leaking memory here, to make sure libusb will not |
| 1095 | * access memory which we have released already. |
| 1096 | */ |
| 1097 | QTAILQ_FOREACH_SAFE(r, &s->requests, next, rtmp) { |
| 1098 | QTAILQ_REMOVE(&s->requests, r, next); |
| 1099 | } |
| 1100 | return; |
| 1101 | } |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | static int usb_host_close(USBHostDevice *s) |
| 1106 | { |
| 1107 | USBDevice *udev = USB_DEVICE(s); |
| 1108 | |
| 1109 | if (s->dh == NULL) { |
| 1110 | return -1; |
| 1111 | } |
| 1112 | |
| 1113 | trace_usb_host_close(s->bus_num, s->addr); |
| 1114 | |
| 1115 | usb_host_abort_xfers(s); |
| 1116 | usb_host_iso_free_all(s); |
| 1117 | |
| 1118 | if (udev->attached) { |
| 1119 | usb_device_detach(udev); |
| 1120 | } |
| 1121 | |
| 1122 | usb_host_release_interfaces(s); |
| 1123 | libusb_reset_device(s->dh); |
| 1124 | usb_host_attach_kernel(s); |
| 1125 | libusb_close(s->dh); |
| 1126 | s->dh = NULL; |
| 1127 | s->dev = NULL; |
| 1128 | |
| 1129 | if (s->hostfd != -1) { |
| 1130 | close(s->hostfd); |
| 1131 | s->hostfd = -1; |
| 1132 | } |
| 1133 | |
| 1134 | usb_host_auto_check(NULL); |
| 1135 | return 0; |
| 1136 | } |
| 1137 | |
| 1138 | static void usb_host_nodev_bh(void *opaque) |
| 1139 | { |
| 1140 | USBHostDevice *s = opaque; |
| 1141 | usb_host_close(s); |
| 1142 | } |
| 1143 | |
| 1144 | static void usb_host_nodev(USBHostDevice *s) |
| 1145 | { |
| 1146 | if (!s->bh_nodev) { |
| 1147 | s->bh_nodev = qemu_bh_new_guarded(usb_host_nodev_bh, s, |
| 1148 | &DEVICE(s)->mem_reentrancy_guard); |
| 1149 | } |
| 1150 | qemu_bh_schedule(s->bh_nodev); |
| 1151 | } |
| 1152 | |
| 1153 | static void usb_host_exit_notifier(struct Notifier *n, void *data) |
| 1154 | { |
| 1155 | USBHostDevice *s = container_of(n, USBHostDevice, exit); |
| 1156 | |
| 1157 | if (s->dh) { |
| 1158 | usb_host_abort_xfers(s); |
| 1159 | usb_host_release_interfaces(s); |
| 1160 | libusb_reset_device(s->dh); |
| 1161 | usb_host_attach_kernel(s); |
| 1162 | libusb_close(s->dh); |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | static libusb_device *usb_host_find_ref(int bus, int addr) |
| 1167 | { |
| 1168 | libusb_device **devs = NULL; |
| 1169 | libusb_device *ret = NULL; |
| 1170 | int i, n; |
| 1171 | |
| 1172 | n = libusb_get_device_list(ctx, &devs); |
| 1173 | for (i = 0; i < n; i++) { |
| 1174 | if (libusb_get_bus_number(devs[i]) == bus && |
| 1175 | libusb_get_device_address(devs[i]) == addr) { |
| 1176 | ret = libusb_ref_device(devs[i]); |
| 1177 | break; |
| 1178 | } |
| 1179 | } |
| 1180 | libusb_free_device_list(devs, 1); |
| 1181 | return ret; |
| 1182 | } |
| 1183 | |
| 1184 | static void usb_host_realize(USBDevice *udev, Error **errp) |
| 1185 | { |
| 1186 | USBHostDevice *s = USB_HOST_DEVICE(udev); |
| 1187 | libusb_device *ldev; |
| 1188 | int rc; |
| 1189 | |
| 1190 | if (usb_host_init() != 0) { |
| 1191 | error_setg(errp, "failed to init libusb"); |
| 1192 | return; |
| 1193 | } |
| 1194 | if (s->match.vendor_id > 0xffff) { |
| 1195 | error_setg(errp, "vendorid out of range"); |
| 1196 | return; |
| 1197 | } |
| 1198 | if (s->match.product_id > 0xffff) { |
| 1199 | error_setg(errp, "productid out of range"); |
| 1200 | return; |
| 1201 | } |
| 1202 | if (s->match.addr > 127) { |
| 1203 | error_setg(errp, "hostaddr out of range"); |
| 1204 | return; |
| 1205 | } |
| 1206 | |
| 1207 | loglevel = s->loglevel; |
| 1208 | udev->flags |= (1 << USB_DEV_FLAG_IS_HOST); |
| 1209 | udev->auto_attach = 0; |
| 1210 | QTAILQ_INIT(&s->requests); |
| 1211 | QTAILQ_INIT(&s->isorings); |
| 1212 | s->hostfd = -1; |
| 1213 | |
| 1214 | #if LIBUSB_API_VERSION >= 0x01000107 && !defined(CONFIG_WIN32) |
| 1215 | if (s->hostdevice) { |
| 1216 | int fd; |
| 1217 | s->needs_autoscan = false; |
| 1218 | fd = qemu_open(s->hostdevice, O_RDWR, errp); |
| 1219 | if (fd < 0) { |
| 1220 | return; |
| 1221 | } |
| 1222 | rc = usb_host_open(s, NULL, fd); |
| 1223 | if (rc < 0) { |
| 1224 | error_setg(errp, "failed to open host usb device %s", s->hostdevice); |
| 1225 | return; |
| 1226 | } |
| 1227 | } else |
| 1228 | #endif |
| 1229 | if (s->match.addr && s->match.bus_num && |
| 1230 | !s->match.vendor_id && |
| 1231 | !s->match.product_id && |
| 1232 | !s->match.port) { |
| 1233 | s->needs_autoscan = false; |
| 1234 | ldev = usb_host_find_ref(s->match.bus_num, |
| 1235 | s->match.addr); |
| 1236 | if (!ldev) { |
| 1237 | error_setg(errp, "failed to find host usb device %d:%d", |
| 1238 | s->match.bus_num, s->match.addr); |
| 1239 | return; |
| 1240 | } |
| 1241 | rc = usb_host_open(s, ldev, 0); |
| 1242 | libusb_unref_device(ldev); |
| 1243 | if (rc < 0) { |
| 1244 | error_setg(errp, "failed to open host usb device %d:%d", |
| 1245 | s->match.bus_num, s->match.addr); |
| 1246 | return; |
| 1247 | } |
| 1248 | } else { |
| 1249 | s->needs_autoscan = true; |
| 1250 | QTAILQ_INSERT_TAIL(&hostdevs, s, next); |
| 1251 | usb_host_auto_check(NULL); |
| 1252 | } |
| 1253 | |
| 1254 | s->exit.notify = usb_host_exit_notifier; |
| 1255 | qemu_add_exit_notifier(&s->exit); |
| 1256 | } |
| 1257 | |
| 1258 | static void usb_host_instance_init(Object *obj) |
| 1259 | { |
| 1260 | USBDevice *udev = USB_DEVICE(obj); |
| 1261 | USBHostDevice *s = USB_HOST_DEVICE(udev); |
| 1262 | |
| 1263 | device_add_bootindex_property(obj, &s->bootindex, |
| 1264 | "bootindex", NULL, |
| 1265 | &udev->qdev); |
| 1266 | } |
| 1267 | |
| 1268 | static void usb_host_unrealize(USBDevice *udev) |
| 1269 | { |
| 1270 | USBHostDevice *s = USB_HOST_DEVICE(udev); |
| 1271 | |
| 1272 | qemu_remove_exit_notifier(&s->exit); |
| 1273 | if (s->needs_autoscan) { |
| 1274 | QTAILQ_REMOVE(&hostdevs, s, next); |
| 1275 | } |
| 1276 | usb_host_close(s); |
| 1277 | } |
| 1278 | |
| 1279 | static void usb_host_cancel_packet(USBDevice *udev, USBPacket *p) |
| 1280 | { |
| 1281 | USBHostDevice *s = USB_HOST_DEVICE(udev); |
| 1282 | USBHostRequest *r; |
| 1283 | |
| 1284 | if (p->combined) { |
| 1285 | usb_combined_packet_cancel(udev, p); |
| 1286 | return; |
| 1287 | } |
| 1288 | |
| 1289 | trace_usb_host_req_canceled(s->bus_num, s->addr, p); |
| 1290 | |
| 1291 | r = usb_host_req_find(s, p); |
| 1292 | if (r && r->p) { |
| 1293 | r->p = NULL; /* mark as dead */ |
| 1294 | libusb_cancel_transfer(r->xfer); |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | static void usb_host_detach_kernel(USBHostDevice *s) |
| 1299 | { |
| 1300 | struct libusb_config_descriptor *conf; |
| 1301 | int rc, i; |
| 1302 | |
| 1303 | rc = libusb_get_active_config_descriptor(s->dev, &conf); |
| 1304 | if (rc != 0) { |
| 1305 | return; |
| 1306 | } |
| 1307 | for (i = 0; i < USB_MAX_INTERFACES; i++) { |
| 1308 | rc = libusb_kernel_driver_active(s->dh, i); |
| 1309 | usb_host_libusb_error("libusb_kernel_driver_active", rc); |
| 1310 | if (rc != 1) { |
| 1311 | if (rc == 0) { |
| 1312 | s->ifs[i].detached = true; |
| 1313 | } |
| 1314 | continue; |
| 1315 | } |
| 1316 | trace_usb_host_detach_kernel(s->bus_num, s->addr, i); |
| 1317 | rc = libusb_detach_kernel_driver(s->dh, i); |
| 1318 | usb_host_libusb_error("libusb_detach_kernel_driver", rc); |
| 1319 | s->ifs[i].detached = true; |
| 1320 | } |
| 1321 | libusb_free_config_descriptor(conf); |
| 1322 | } |
| 1323 | |
| 1324 | static void usb_host_attach_kernel(USBHostDevice *s) |
| 1325 | { |
| 1326 | struct libusb_config_descriptor *conf; |
| 1327 | int rc, i; |
| 1328 | |
| 1329 | rc = libusb_get_active_config_descriptor(s->dev, &conf); |
| 1330 | if (rc != 0) { |
| 1331 | return; |
| 1332 | } |
| 1333 | for (i = 0; i < USB_MAX_INTERFACES; i++) { |
| 1334 | if (!s->ifs[i].detached) { |
| 1335 | continue; |
| 1336 | } |
| 1337 | trace_usb_host_attach_kernel(s->bus_num, s->addr, i); |
| 1338 | libusb_attach_kernel_driver(s->dh, i); |
| 1339 | s->ifs[i].detached = false; |
| 1340 | } |
| 1341 | libusb_free_config_descriptor(conf); |
| 1342 | } |
| 1343 | |
| 1344 | static int usb_host_claim_interfaces(USBHostDevice *s, int configuration) |
| 1345 | { |
| 1346 | USBDevice *udev = USB_DEVICE(s); |
| 1347 | struct libusb_config_descriptor *conf; |
| 1348 | int rc, i, claimed; |
| 1349 | |
| 1350 | for (i = 0; i < USB_MAX_INTERFACES; i++) { |
| 1351 | udev->altsetting[i] = 0; |
| 1352 | } |
| 1353 | udev->ninterfaces = 0; |
| 1354 | udev->configuration = 0; |
| 1355 | |
| 1356 | usb_host_detach_kernel(s); |
| 1357 | |
| 1358 | rc = libusb_get_active_config_descriptor(s->dev, &conf); |
| 1359 | if (rc != 0) { |
| 1360 | if (rc == LIBUSB_ERROR_NOT_FOUND) { |
| 1361 | /* address state - ignore */ |
| 1362 | return USB_RET_SUCCESS; |
| 1363 | } |
| 1364 | return USB_RET_STALL; |
| 1365 | } |
| 1366 | |
| 1367 | claimed = 0; |
| 1368 | for (i = 0; i < USB_MAX_INTERFACES; i++) { |
| 1369 | trace_usb_host_claim_interface(s->bus_num, s->addr, configuration, i); |
| 1370 | rc = libusb_claim_interface(s->dh, i); |
| 1371 | if (rc == 0) { |
| 1372 | s->ifs[i].claimed = true; |
| 1373 | if (++claimed == conf->bNumInterfaces) { |
| 1374 | break; |
| 1375 | } |
| 1376 | } |
| 1377 | } |
| 1378 | if (claimed != conf->bNumInterfaces) { |
| 1379 | return USB_RET_STALL; |
| 1380 | } |
| 1381 | |
| 1382 | udev->ninterfaces = conf->bNumInterfaces; |
| 1383 | udev->configuration = configuration; |
| 1384 | |
| 1385 | libusb_free_config_descriptor(conf); |
| 1386 | return USB_RET_SUCCESS; |
| 1387 | } |
| 1388 | |
| 1389 | static void usb_host_release_interfaces(USBHostDevice *s) |
| 1390 | { |
| 1391 | int i, rc; |
| 1392 | |
| 1393 | for (i = 0; i < USB_MAX_INTERFACES; i++) { |
| 1394 | if (!s->ifs[i].claimed) { |
| 1395 | continue; |
| 1396 | } |
| 1397 | trace_usb_host_release_interface(s->bus_num, s->addr, i); |
| 1398 | rc = libusb_release_interface(s->dh, i); |
| 1399 | usb_host_libusb_error("libusb_release_interface", rc); |
| 1400 | s->ifs[i].claimed = false; |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | static void usb_host_set_address(USBHostDevice *s, int addr) |
| 1405 | { |
| 1406 | USBDevice *udev = USB_DEVICE(s); |
| 1407 | |
| 1408 | trace_usb_host_set_address(s->bus_num, s->addr, addr); |
| 1409 | udev->addr = addr; |
| 1410 | } |
| 1411 | |
| 1412 | static void usb_host_set_config(USBHostDevice *s, int config, USBPacket *p) |
| 1413 | { |
| 1414 | int rc = 0; |
| 1415 | |
| 1416 | trace_usb_host_set_config(s->bus_num, s->addr, config); |
| 1417 | |
| 1418 | usb_host_release_interfaces(s); |
| 1419 | if (s->ddesc.bNumConfigurations != 1) { |
| 1420 | rc = libusb_set_configuration(s->dh, config); |
| 1421 | if (rc != 0) { |
| 1422 | usb_host_libusb_error("libusb_set_configuration", rc); |
| 1423 | p->status = USB_RET_STALL; |
| 1424 | if (rc == LIBUSB_ERROR_NO_DEVICE) { |
| 1425 | usb_host_nodev(s); |
| 1426 | } |
| 1427 | return; |
| 1428 | } |
| 1429 | } |
| 1430 | p->status = usb_host_claim_interfaces(s, config); |
| 1431 | if (p->status != USB_RET_SUCCESS) { |
| 1432 | return; |
| 1433 | } |
| 1434 | usb_host_ep_update(s); |
| 1435 | } |
| 1436 | |
| 1437 | static void usb_host_set_interface(USBHostDevice *s, int iface, int alt, |
| 1438 | USBPacket *p) |
| 1439 | { |
| 1440 | USBDevice *udev = USB_DEVICE(s); |
| 1441 | int rc; |
| 1442 | |
| 1443 | trace_usb_host_set_interface(s->bus_num, s->addr, iface, alt); |
| 1444 | |
| 1445 | usb_host_iso_free_all(s); |
| 1446 | |
| 1447 | if (iface >= USB_MAX_INTERFACES) { |
| 1448 | p->status = USB_RET_STALL; |
| 1449 | return; |
| 1450 | } |
| 1451 | |
| 1452 | rc = libusb_set_interface_alt_setting(s->dh, iface, alt); |
| 1453 | if (rc != 0) { |
| 1454 | usb_host_libusb_error("libusb_set_interface_alt_setting", rc); |
| 1455 | p->status = USB_RET_STALL; |
| 1456 | if (rc == LIBUSB_ERROR_NO_DEVICE) { |
| 1457 | usb_host_nodev(s); |
| 1458 | } |
| 1459 | return; |
| 1460 | } |
| 1461 | |
| 1462 | udev->altsetting[iface] = alt; |
| 1463 | usb_host_ep_update(s); |
| 1464 | } |
| 1465 | |
| 1466 | static void usb_host_handle_control(USBDevice *udev, USBPacket *p, |
| 1467 | int request, int value, int index, |
| 1468 | int length, uint8_t *data) |
| 1469 | { |
| 1470 | USBHostDevice *s = USB_HOST_DEVICE(udev); |
| 1471 | USBHostRequest *r; |
| 1472 | int rc; |
| 1473 | |
| 1474 | trace_usb_host_req_control(s->bus_num, s->addr, p, request, value, index); |
| 1475 | |
| 1476 | if (s->dh == NULL) { |
| 1477 | p->status = USB_RET_NODEV; |
| 1478 | trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status); |
| 1479 | return; |
| 1480 | } |
| 1481 | |
| 1482 | switch (request) { |
| 1483 | case DeviceOutRequest | USB_REQ_SET_ADDRESS: |
| 1484 | usb_host_set_address(s, value); |
| 1485 | trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status); |
| 1486 | return; |
| 1487 | |
| 1488 | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: |
| 1489 | usb_host_set_config(s, value & 0xff, p); |
| 1490 | trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status); |
| 1491 | return; |
| 1492 | |
| 1493 | case InterfaceOutRequest | USB_REQ_SET_INTERFACE: |
| 1494 | usb_host_set_interface(s, index, value, p); |
| 1495 | trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status); |
| 1496 | return; |
| 1497 | |
| 1498 | case EndpointOutRequest | USB_REQ_CLEAR_FEATURE: |
| 1499 | if (value == 0) { /* clear halt */ |
| 1500 | int pid = (index & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT; |
| 1501 | libusb_clear_halt(s->dh, index); |
| 1502 | usb_ep_set_halted(udev, pid, index & 0x0f, 0); |
| 1503 | trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status); |
| 1504 | return; |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | r = usb_host_req_alloc(s, p, (request >> 8) & USB_DIR_IN, length + 8); |
| 1509 | r->cbuf = data; |
| 1510 | r->clen = length; |
| 1511 | memcpy(r->buffer, udev->setup_buf, 8); |
| 1512 | if (!r->in) { |
| 1513 | memcpy(r->buffer + 8, r->cbuf, r->clen); |
| 1514 | } |
| 1515 | |
| 1516 | /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices |
| 1517 | * to work redirected to a not superspeed capable hcd */ |
| 1518 | if ((udev->speedmask & USB_SPEED_MASK_SUPER) && |
| 1519 | !(udev->port->speedmask & USB_SPEED_MASK_SUPER) && |
| 1520 | request == 0x8006 && value == 0x100 && index == 0) { |
| 1521 | r->usb3ep0quirk = true; |
| 1522 | } |
| 1523 | |
| 1524 | libusb_fill_control_transfer(r->xfer, s->dh, r->buffer, |
| 1525 | usb_host_req_complete_ctrl, r, |
| 1526 | CONTROL_TIMEOUT); |
| 1527 | rc = libusb_submit_transfer(r->xfer); |
| 1528 | if (rc != 0) { |
| 1529 | p->status = USB_RET_NODEV; |
| 1530 | trace_usb_host_req_complete(s->bus_num, s->addr, p, |
| 1531 | p->status, p->actual_length); |
| 1532 | if (rc == LIBUSB_ERROR_NO_DEVICE) { |
| 1533 | usb_host_nodev(s); |
| 1534 | } |
| 1535 | return; |
| 1536 | } |
| 1537 | |
| 1538 | p->status = USB_RET_ASYNC; |
| 1539 | } |
| 1540 | |
| 1541 | static void usb_host_handle_data(USBDevice *udev, USBPacket *p) |
| 1542 | { |
| 1543 | USBHostDevice *s = USB_HOST_DEVICE(udev); |
| 1544 | USBHostRequest *r; |
| 1545 | size_t size; |
| 1546 | int ep, rc; |
| 1547 | |
| 1548 | if (usb_host_use_combining(p->ep) && p->state == USB_PACKET_SETUP) { |
| 1549 | p->status = USB_RET_ADD_TO_QUEUE; |
| 1550 | return; |
| 1551 | } |
| 1552 | |
| 1553 | trace_usb_host_req_data(s->bus_num, s->addr, p, |
| 1554 | p->pid == USB_TOKEN_IN, |
| 1555 | p->ep->nr, p->iov.size); |
| 1556 | |
| 1557 | if (s->dh == NULL) { |
| 1558 | p->status = USB_RET_NODEV; |
| 1559 | trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status); |
| 1560 | return; |
| 1561 | } |
| 1562 | if (p->ep->halted) { |
| 1563 | p->status = USB_RET_STALL; |
| 1564 | trace_usb_host_req_emulated(s->bus_num, s->addr, p, p->status); |
| 1565 | return; |
| 1566 | } |
| 1567 | |
| 1568 | switch (usb_ep_get_type(udev, p->pid, p->ep->nr)) { |
| 1569 | case USB_ENDPOINT_XFER_BULK: |
| 1570 | size = usb_packet_size(p); |
| 1571 | r = usb_host_req_alloc(s, p, p->pid == USB_TOKEN_IN, size); |
| 1572 | if (!r->in) { |
| 1573 | usb_packet_copy(p, r->buffer, size); |
| 1574 | } |
| 1575 | ep = p->ep->nr | (r->in ? USB_DIR_IN : 0); |
| 1576 | if (p->stream) { |
| 1577 | #ifdef HAVE_STREAMS |
| 1578 | libusb_fill_bulk_stream_transfer(r->xfer, s->dh, ep, p->stream, |
| 1579 | r->buffer, size, |
| 1580 | usb_host_req_complete_data, r, |
| 1581 | BULK_TIMEOUT); |
| 1582 | #else |
| 1583 | usb_host_req_free(r); |
| 1584 | p->status = USB_RET_STALL; |
| 1585 | return; |
| 1586 | #endif |
| 1587 | } else { |
| 1588 | libusb_fill_bulk_transfer(r->xfer, s->dh, ep, |
| 1589 | r->buffer, size, |
| 1590 | usb_host_req_complete_data, r, |
| 1591 | BULK_TIMEOUT); |
| 1592 | } |
| 1593 | break; |
| 1594 | case USB_ENDPOINT_XFER_INT: |
| 1595 | r = usb_host_req_alloc(s, p, p->pid == USB_TOKEN_IN, p->iov.size); |
| 1596 | if (!r->in) { |
| 1597 | usb_packet_copy(p, r->buffer, p->iov.size); |
| 1598 | } |
| 1599 | ep = p->ep->nr | (r->in ? USB_DIR_IN : 0); |
| 1600 | libusb_fill_interrupt_transfer(r->xfer, s->dh, ep, |
| 1601 | r->buffer, p->iov.size, |
| 1602 | usb_host_req_complete_data, r, |
| 1603 | INTR_TIMEOUT); |
| 1604 | break; |
| 1605 | case USB_ENDPOINT_XFER_ISOC: |
| 1606 | if (p->pid == USB_TOKEN_IN) { |
| 1607 | usb_host_iso_data_in(s, p); |
| 1608 | } else { |
| 1609 | usb_host_iso_data_out(s, p); |
| 1610 | } |
| 1611 | trace_usb_host_req_complete(s->bus_num, s->addr, p, |
| 1612 | p->status, p->actual_length); |
| 1613 | return; |
| 1614 | default: |
| 1615 | p->status = USB_RET_STALL; |
| 1616 | trace_usb_host_req_complete(s->bus_num, s->addr, p, |
| 1617 | p->status, p->actual_length); |
| 1618 | return; |
| 1619 | } |
| 1620 | |
| 1621 | rc = libusb_submit_transfer(r->xfer); |
| 1622 | if (rc != 0) { |
| 1623 | p->status = USB_RET_NODEV; |
| 1624 | trace_usb_host_req_complete(s->bus_num, s->addr, p, |
| 1625 | p->status, p->actual_length); |
| 1626 | if (rc == LIBUSB_ERROR_NO_DEVICE) { |
| 1627 | usb_host_nodev(s); |
| 1628 | } |
| 1629 | return; |
| 1630 | } |
| 1631 | |
| 1632 | p->status = USB_RET_ASYNC; |
| 1633 | } |
| 1634 | |
| 1635 | static void usb_host_flush_ep_queue(USBDevice *dev, USBEndpoint *ep) |
| 1636 | { |
| 1637 | if (usb_host_use_combining(ep)) { |
| 1638 | usb_ep_combine_input_packets(ep); |
| 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | static void usb_host_handle_reset(USBDevice *udev) |
| 1643 | { |
| 1644 | USBHostDevice *s = USB_HOST_DEVICE(udev); |
| 1645 | int rc; |
| 1646 | |
| 1647 | if (!s->allow_one_guest_reset && !s->allow_all_guest_resets) { |
| 1648 | return; |
| 1649 | } |
| 1650 | if (!s->allow_all_guest_resets && udev->addr == 0) { |
| 1651 | return; |
| 1652 | } |
| 1653 | |
| 1654 | trace_usb_host_reset(s->bus_num, s->addr); |
| 1655 | |
| 1656 | rc = libusb_reset_device(s->dh); |
| 1657 | if (rc != 0) { |
| 1658 | usb_host_nodev(s); |
| 1659 | } |
| 1660 | } |
| 1661 | |
| 1662 | static int usb_host_alloc_streams(USBDevice *udev, USBEndpoint **eps, |
| 1663 | int nr_eps, int streams) |
| 1664 | { |
| 1665 | #ifdef HAVE_STREAMS |
| 1666 | USBHostDevice *s = USB_HOST_DEVICE(udev); |
| 1667 | unsigned char endpoints[30]; |
| 1668 | int i, rc; |
| 1669 | |
| 1670 | for (i = 0; i < nr_eps; i++) { |
| 1671 | endpoints[i] = eps[i]->nr; |
| 1672 | if (eps[i]->pid == USB_TOKEN_IN) { |
| 1673 | endpoints[i] |= 0x80; |
| 1674 | } |
| 1675 | } |
| 1676 | rc = libusb_alloc_streams(s->dh, streams, endpoints, nr_eps); |
| 1677 | if (rc < 0) { |
| 1678 | usb_host_libusb_error("libusb_alloc_streams", rc); |
| 1679 | } else if (rc != streams) { |
| 1680 | error_report("libusb_alloc_streams: got less streams " |
| 1681 | "then requested %d < %d", rc, streams); |
| 1682 | } |
| 1683 | |
| 1684 | return (rc == streams) ? 0 : -1; |
| 1685 | #else |
| 1686 | error_report("libusb_alloc_streams: error not implemented"); |
| 1687 | return -1; |
| 1688 | #endif |
| 1689 | } |
| 1690 | |
| 1691 | static void usb_host_free_streams(USBDevice *udev, USBEndpoint **eps, |
| 1692 | int nr_eps) |
| 1693 | { |
| 1694 | #ifdef HAVE_STREAMS |
| 1695 | USBHostDevice *s = USB_HOST_DEVICE(udev); |
| 1696 | unsigned char endpoints[30]; |
| 1697 | int i; |
| 1698 | |
| 1699 | for (i = 0; i < nr_eps; i++) { |
| 1700 | endpoints[i] = eps[i]->nr; |
| 1701 | if (eps[i]->pid == USB_TOKEN_IN) { |
| 1702 | endpoints[i] |= 0x80; |
| 1703 | } |
| 1704 | } |
| 1705 | libusb_free_streams(s->dh, endpoints, nr_eps); |
| 1706 | #endif |
| 1707 | } |
| 1708 | |
| 1709 | /* |
| 1710 | * This is *NOT* about restoring state. We have absolutely no idea |
| 1711 | * what state the host device is in at the moment and whenever it is |
| 1712 | * still present in the first place. Attempting to continue where we |
| 1713 | * left off is impossible. |
| 1714 | * |
| 1715 | * What we are going to do here is emulate a surprise removal of |
| 1716 | * the usb device passed through, then kick host scan so the device |
| 1717 | * will get re-attached (and re-initialized by the guest) in case it |
| 1718 | * is still present. |
| 1719 | * |
| 1720 | * As the device removal will change the state of other devices (usb |
| 1721 | * host controller, most likely interrupt controller too) we have to |
| 1722 | * wait with it until *all* vmstate is loaded. Thus post_load just |
| 1723 | * kicks a bottom half which then does the actual work. |
| 1724 | */ |
| 1725 | static void usb_host_post_load_bh(void *opaque) |
| 1726 | { |
| 1727 | USBHostDevice *dev = opaque; |
| 1728 | USBDevice *udev = USB_DEVICE(dev); |
| 1729 | |
| 1730 | if (dev->dh != NULL) { |
| 1731 | usb_host_close(dev); |
| 1732 | } |
| 1733 | if (udev->attached) { |
| 1734 | usb_device_detach(udev); |
| 1735 | } |
| 1736 | dev->bh_postld_pending = false; |
| 1737 | usb_host_auto_check(NULL); |
| 1738 | } |
| 1739 | |
| 1740 | static int usb_host_post_load(void *opaque, int version_id) |
| 1741 | { |
| 1742 | USBHostDevice *dev = opaque; |
| 1743 | |
| 1744 | if (!dev->bh_postld) { |
| 1745 | dev->bh_postld = qemu_bh_new_guarded(usb_host_post_load_bh, dev, |
| 1746 | &DEVICE(dev)->mem_reentrancy_guard); |
| 1747 | } |
| 1748 | qemu_bh_schedule(dev->bh_postld); |
| 1749 | dev->bh_postld_pending = true; |
| 1750 | return 0; |
| 1751 | } |
| 1752 | |
| 1753 | static const VMStateDescription vmstate_usb_host = { |
| 1754 | .name = "usb-host", |
| 1755 | .version_id = 1, |
| 1756 | .minimum_version_id = 1, |
| 1757 | .post_load = usb_host_post_load, |
| 1758 | .fields = (const VMStateField[]) { |
| 1759 | VMSTATE_USB_DEVICE(parent_obj, USBHostDevice), |
| 1760 | VMSTATE_END_OF_LIST() |
| 1761 | } |
| 1762 | }; |
| 1763 | |
| 1764 | static const Property usb_host_dev_properties[] = { |
| 1765 | DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0), |
| 1766 | DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0), |
| 1767 | DEFINE_PROP_STRING("hostport", USBHostDevice, match.port), |
| 1768 | DEFINE_PROP_UINT32("vendorid", USBHostDevice, match.vendor_id, 0), |
| 1769 | DEFINE_PROP_UINT32("productid", USBHostDevice, match.product_id, 0), |
| 1770 | #if LIBUSB_API_VERSION >= 0x01000107 |
| 1771 | DEFINE_PROP_STRING("hostdevice", USBHostDevice, hostdevice), |
| 1772 | #endif |
| 1773 | DEFINE_PROP_UINT32("isobufs", USBHostDevice, iso_urb_count, 4), |
| 1774 | DEFINE_PROP_UINT32("isobsize", USBHostDevice, iso_urb_frames, 32), |
| 1775 | DEFINE_PROP_BOOL("guest-reset", USBHostDevice, |
| 1776 | allow_one_guest_reset, true), |
| 1777 | DEFINE_PROP_BOOL("guest-resets-all", USBHostDevice, |
| 1778 | allow_all_guest_resets, false), |
| 1779 | DEFINE_PROP_UINT32("loglevel", USBHostDevice, loglevel, |
| 1780 | LIBUSB_LOG_LEVEL_WARNING), |
| 1781 | DEFINE_PROP_BIT("pipeline", USBHostDevice, options, |
| 1782 | USB_HOST_OPT_PIPELINE, true), |
| 1783 | DEFINE_PROP_BOOL("suppress-remote-wake", USBHostDevice, |
| 1784 | suppress_remote_wake, true), |
| 1785 | }; |
| 1786 | |
| 1787 | static void usb_host_class_initfn(ObjectClass *klass, const void *data) |
| 1788 | { |
| 1789 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 1790 | USBDeviceClass *uc = USB_DEVICE_CLASS(klass); |
| 1791 | |
| 1792 | uc->realize = usb_host_realize; |
| 1793 | uc->product_desc = "USB Host Device"; |
| 1794 | uc->cancel_packet = usb_host_cancel_packet; |
| 1795 | uc->handle_data = usb_host_handle_data; |
| 1796 | uc->handle_control = usb_host_handle_control; |
| 1797 | uc->handle_reset = usb_host_handle_reset; |
| 1798 | uc->unrealize = usb_host_unrealize; |
| 1799 | uc->flush_ep_queue = usb_host_flush_ep_queue; |
| 1800 | uc->alloc_streams = usb_host_alloc_streams; |
| 1801 | uc->free_streams = usb_host_free_streams; |
| 1802 | dc->vmsd = &vmstate_usb_host; |
| 1803 | device_class_set_props(dc, usb_host_dev_properties); |
| 1804 | set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); |
| 1805 | } |
| 1806 | |
| 1807 | static const TypeInfo usb_host_dev_info = { |
| 1808 | .name = TYPE_USB_HOST_DEVICE, |
| 1809 | .parent = TYPE_USB_DEVICE, |
| 1810 | .instance_size = sizeof(USBHostDevice), |
| 1811 | .class_init = usb_host_class_initfn, |
| 1812 | .instance_init = usb_host_instance_init, |
| 1813 | }; |
| 1814 | module_obj(TYPE_USB_HOST_DEVICE); |
| 1815 | module_kconfig(USB); |
| 1816 | |
| 1817 | static void usb_host_register_types(void) |
| 1818 | { |
| 1819 | type_register_static(&usb_host_dev_info); |
| 1820 | #ifdef CONFIG_HMP |
| 1821 | monitor_register_hmp("usbhost", true, hmp_info_usbhost); |
| 1822 | #endif |
| 1823 | } |
| 1824 | |
| 1825 | type_init(usb_host_register_types) |
| 1826 | |
| 1827 | /* ------------------------------------------------------------------------ */ |
| 1828 | |
| 1829 | static QEMUTimer *usb_auto_timer; |
| 1830 | static VMChangeStateEntry *usb_vmstate; |
| 1831 | |
| 1832 | static void usb_host_vm_state(void *unused, bool running, RunState state) |
| 1833 | { |
| 1834 | if (running) { |
| 1835 | usb_host_auto_check(unused); |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | static void usb_host_auto_check(void *unused) |
| 1840 | { |
| 1841 | struct USBHostDevice *s; |
| 1842 | struct USBAutoFilter *f; |
| 1843 | libusb_device **devs = NULL; |
| 1844 | struct libusb_device_descriptor ddesc; |
| 1845 | int i, n; |
| 1846 | |
| 1847 | if (usb_host_init() != 0) { |
| 1848 | return; |
| 1849 | } |
| 1850 | |
| 1851 | if (runstate_is_running()) { |
| 1852 | n = libusb_get_device_list(ctx, &devs); |
| 1853 | for (i = 0; i < n; i++) { |
| 1854 | if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) { |
| 1855 | continue; |
| 1856 | } |
| 1857 | if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) { |
| 1858 | continue; |
| 1859 | } |
| 1860 | QTAILQ_FOREACH(s, &hostdevs, next) { |
| 1861 | f = &s->match; |
| 1862 | if (f->bus_num > 0 && |
| 1863 | f->bus_num != libusb_get_bus_number(devs[i])) { |
| 1864 | continue; |
| 1865 | } |
| 1866 | if (f->addr > 0 && |
| 1867 | f->addr != libusb_get_device_address(devs[i])) { |
| 1868 | continue; |
| 1869 | } |
| 1870 | if (f->port != NULL) { |
| 1871 | char port[16] = "-"; |
| 1872 | usb_host_get_port(devs[i], port, sizeof(port)); |
| 1873 | if (strcmp(f->port, port) != 0) { |
| 1874 | continue; |
| 1875 | } |
| 1876 | } |
| 1877 | if (f->vendor_id > 0 && |
| 1878 | f->vendor_id != ddesc.idVendor) { |
| 1879 | continue; |
| 1880 | } |
| 1881 | if (f->product_id > 0 && |
| 1882 | f->product_id != ddesc.idProduct) { |
| 1883 | continue; |
| 1884 | } |
| 1885 | |
| 1886 | /* We got a match */ |
| 1887 | s->seen++; |
| 1888 | if (s->errcount >= 3) { |
| 1889 | continue; |
| 1890 | } |
| 1891 | if (s->dh != NULL) { |
| 1892 | continue; |
| 1893 | } |
| 1894 | if (usb_host_open(s, devs[i], 0) < 0) { |
| 1895 | s->errcount++; |
| 1896 | continue; |
| 1897 | } |
| 1898 | break; |
| 1899 | } |
| 1900 | } |
| 1901 | libusb_free_device_list(devs, 1); |
| 1902 | |
| 1903 | QTAILQ_FOREACH(s, &hostdevs, next) { |
| 1904 | if (s->seen == 0) { |
| 1905 | if (s->dh) { |
| 1906 | usb_host_close(s); |
| 1907 | } |
| 1908 | s->errcount = 0; |
| 1909 | } |
| 1910 | s->seen = 0; |
| 1911 | } |
| 1912 | } |
| 1913 | |
| 1914 | if (!usb_vmstate) { |
| 1915 | usb_vmstate = qemu_add_vm_change_state_handler(usb_host_vm_state, NULL); |
| 1916 | } |
| 1917 | if (!usb_auto_timer) { |
| 1918 | usb_auto_timer = timer_new_ms(QEMU_CLOCK_REALTIME, usb_host_auto_check, NULL); |
| 1919 | if (!usb_auto_timer) { |
| 1920 | return; |
| 1921 | } |
| 1922 | trace_usb_host_auto_scan_enabled(); |
| 1923 | } |
| 1924 | timer_mod(usb_auto_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 2000); |
| 1925 | } |
| 1926 | |
| 1927 | #ifdef CONFIG_HMP |
| 1928 | void hmp_info_usbhost(MonitorHMP *hmp, const QDict *qdict) |
| 1929 | { |
| 1930 | libusb_device **devs = NULL; |
| 1931 | struct libusb_device_descriptor ddesc; |
| 1932 | char port[16]; |
| 1933 | int i, n; |
| 1934 | |
| 1935 | if (usb_host_init() != 0) { |
| 1936 | return; |
| 1937 | } |
| 1938 | |
| 1939 | n = libusb_get_device_list(ctx, &devs); |
| 1940 | for (i = 0; i < n; i++) { |
| 1941 | if (libusb_get_device_descriptor(devs[i], &ddesc) != 0) { |
| 1942 | continue; |
| 1943 | } |
| 1944 | if (ddesc.bDeviceClass == LIBUSB_CLASS_HUB) { |
| 1945 | continue; |
| 1946 | } |
| 1947 | usb_host_get_port(devs[i], port, sizeof(port)); |
| 1948 | monitor_hmp_printf(hmp, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n", |
| 1949 | libusb_get_bus_number(devs[i]), |
| 1950 | libusb_get_device_address(devs[i]), |
| 1951 | port, |
| 1952 | speed_name[libusb_get_device_speed(devs[i])]); |
| 1953 | monitor_hmp_printf(hmp, " Class %02x:", ddesc.bDeviceClass); |
| 1954 | monitor_hmp_printf(hmp, " USB device %04x:%04x", |
| 1955 | ddesc.idVendor, ddesc.idProduct); |
| 1956 | if (ddesc.iProduct) { |
| 1957 | libusb_device_handle *handle; |
| 1958 | if (libusb_open(devs[i], &handle) == 0) { |
| 1959 | unsigned char name[64] = ""; |
| 1960 | libusb_get_string_descriptor_ascii(handle, |
| 1961 | ddesc.iProduct, |
| 1962 | name, sizeof(name)); |
| 1963 | libusb_close(handle); |
| 1964 | monitor_hmp_printf(hmp, ", %s", name); |
| 1965 | } |
| 1966 | } |
| 1967 | monitor_hmp_printf(hmp, "\n"); |
| 1968 | } |
| 1969 | libusb_free_device_list(devs, 1); |
| 1970 | } |
| 1971 | #endif |