1 /* $FreeBSD: head/lib/libusb/libusb10.c 264344 2014-04-11 14:11:55Z hselasky $ */ 2 /*- 3 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved. 4 * Copyright (c) 2009 Hans Petter Selasky. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #ifdef LIBUSB_GLOBAL_INCLUDE_FILE 29 #include LIBUSB_GLOBAL_INCLUDE_FILE 30 #else 31 #include <assert.h> 32 #include <errno.h> 33 #include <poll.h> 34 #include <pthread.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include <unistd.h> 39 #include <time.h> 40 #include <sys/fcntl.h> 41 #include <sys/ioctl.h> 42 #include <sys/queue.h> 43 #include <sys/endian.h> 44 #endif 45 46 #define libusb_device_handle libusb20_device 47 48 #include "libusb20.h" 49 #include "libusb20_desc.h" 50 #include "libusb20_int.h" 51 #include "libusb.h" 52 #include "libusb10.h" 53 54 static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER; 55 struct libusb_context *usbi_default_context = NULL; 56 57 /* Prototypes */ 58 59 static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t); 60 static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *); 61 static int libusb10_convert_error(uint8_t status); 62 static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int); 63 static void libusb10_isoc_proxy(struct libusb20_transfer *); 64 static void libusb10_bulk_intr_proxy(struct libusb20_transfer *); 65 static void libusb10_ctrl_proxy(struct libusb20_transfer *); 66 static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t); 67 68 /* Library initialisation / deinitialisation */ 69 70 void 71 libusb_set_debug(libusb_context *ctx, int level) 72 { 73 ctx = GET_CONTEXT(ctx); 74 if (ctx) 75 ctx->debug = level; 76 } 77 78 static void 79 libusb_set_nonblocking(int f) 80 { 81 int flags; 82 83 /* 84 * We ignore any failures in this function, hence the 85 * non-blocking flag is not critical to the operation of 86 * libUSB. We use F_GETFL and F_SETFL to be compatible with 87 * Linux. 88 */ 89 90 flags = fcntl(f, F_GETFL, NULL); 91 if (flags == -1) 92 return; 93 flags |= O_NONBLOCK; 94 fcntl(f, F_SETFL, flags); 95 } 96 97 int 98 libusb_init(libusb_context **context) 99 { 100 struct libusb_context *ctx; 101 pthread_condattr_t attr; 102 char *debug; 103 int ret; 104 105 ctx = malloc(sizeof(*ctx)); 106 if (!ctx) 107 return (LIBUSB_ERROR_INVALID_PARAM); 108 109 memset(ctx, 0, sizeof(*ctx)); 110 111 debug = getenv("LIBUSB_DEBUG"); 112 if (debug != NULL) { 113 ctx->debug = atoi(debug); 114 if (ctx->debug != 0) 115 ctx->debug_fixed = 1; 116 } 117 TAILQ_INIT(&ctx->pollfds); 118 TAILQ_INIT(&ctx->tr_done); 119 TAILQ_INIT(&ctx->hotplug_cbh); 120 TAILQ_INIT(&ctx->hotplug_devs); 121 122 if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) { 123 free(ctx); 124 return (LIBUSB_ERROR_NO_MEM); 125 } 126 if (pthread_mutex_init(&ctx->hotplug_lock, NULL) != 0) { 127 pthread_mutex_destroy(&ctx->ctx_lock); 128 free(ctx); 129 return (LIBUSB_ERROR_NO_MEM); 130 } 131 if (pthread_condattr_init(&attr) != 0) { 132 pthread_mutex_destroy(&ctx->ctx_lock); 133 pthread_mutex_destroy(&ctx->hotplug_lock); 134 free(ctx); 135 return (LIBUSB_ERROR_NO_MEM); 136 } 137 if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) { 138 pthread_mutex_destroy(&ctx->ctx_lock); 139 pthread_mutex_destroy(&ctx->hotplug_lock); 140 pthread_condattr_destroy(&attr); 141 free(ctx); 142 return (LIBUSB_ERROR_OTHER); 143 } 144 if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) { 145 pthread_mutex_destroy(&ctx->ctx_lock); 146 pthread_mutex_destroy(&ctx->hotplug_lock); 147 pthread_condattr_destroy(&attr); 148 free(ctx); 149 return (LIBUSB_ERROR_NO_MEM); 150 } 151 pthread_condattr_destroy(&attr); 152 153 ctx->ctx_handler = NO_THREAD; 154 ctx->hotplug_handler = NO_THREAD; 155 156 ret = pipe(ctx->ctrl_pipe); 157 if (ret < 0) { 158 pthread_mutex_destroy(&ctx->ctx_lock); 159 pthread_mutex_destroy(&ctx->hotplug_lock); 160 pthread_cond_destroy(&ctx->ctx_cond); 161 free(ctx); 162 return (LIBUSB_ERROR_OTHER); 163 } 164 /* set non-blocking mode on the control pipe to avoid deadlock */ 165 libusb_set_nonblocking(ctx->ctrl_pipe[0]); 166 libusb_set_nonblocking(ctx->ctrl_pipe[1]); 167 168 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN); 169 170 pthread_mutex_lock(&default_context_lock); 171 if (usbi_default_context == NULL) { 172 usbi_default_context = ctx; 173 } 174 pthread_mutex_unlock(&default_context_lock); 175 176 if (context) 177 *context = ctx; 178 179 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete"); 180 181 return (0); 182 } 183 184 void 185 libusb_exit(libusb_context *ctx) 186 { 187 ctx = GET_CONTEXT(ctx); 188 189 if (ctx == NULL) 190 return; 191 192 /* stop hotplug thread, if any */ 193 194 if (ctx->hotplug_handler != NO_THREAD) { 195 pthread_t td; 196 void *ptr; 197 198 HOTPLUG_LOCK(ctx); 199 td = ctx->hotplug_handler; 200 ctx->hotplug_handler = NO_THREAD; 201 HOTPLUG_UNLOCK(ctx); 202 203 pthread_join(td, &ptr); 204 } 205 206 /* XXX cleanup devices */ 207 208 libusb10_remove_pollfd(ctx, &ctx->ctx_poll); 209 close(ctx->ctrl_pipe[0]); 210 close(ctx->ctrl_pipe[1]); 211 pthread_mutex_destroy(&ctx->ctx_lock); 212 pthread_mutex_destroy(&ctx->hotplug_lock); 213 pthread_cond_destroy(&ctx->ctx_cond); 214 215 pthread_mutex_lock(&default_context_lock); 216 if (ctx == usbi_default_context) { 217 usbi_default_context = NULL; 218 } 219 pthread_mutex_unlock(&default_context_lock); 220 221 free(ctx); 222 } 223 224 /* Device handling and initialisation. */ 225 226 ssize_t 227 libusb_get_device_list(libusb_context *ctx, libusb_device ***list) 228 { 229 struct libusb20_backend *usb_backend; 230 struct libusb20_device *pdev; 231 struct libusb_device *dev; 232 int i; 233 234 ctx = GET_CONTEXT(ctx); 235 236 if (ctx == NULL) 237 return (LIBUSB_ERROR_INVALID_PARAM); 238 239 if (list == NULL) 240 return (LIBUSB_ERROR_INVALID_PARAM); 241 242 usb_backend = libusb20_be_alloc_default(); 243 if (usb_backend == NULL) 244 return (LIBUSB_ERROR_NO_MEM); 245 246 /* figure out how many USB devices are present */ 247 pdev = NULL; 248 i = 0; 249 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev))) 250 i++; 251 252 /* allocate device pointer list */ 253 *list = malloc((i + 1) * sizeof(void *)); 254 if (*list == NULL) { 255 libusb20_be_free(usb_backend); 256 return (LIBUSB_ERROR_NO_MEM); 257 } 258 /* create libusb v1.0 compliant devices */ 259 i = 0; 260 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) { 261 262 dev = malloc(sizeof(*dev)); 263 if (dev == NULL) { 264 while (i != 0) { 265 libusb_unref_device((*list)[i - 1]); 266 i--; 267 } 268 free(*list); 269 *list = NULL; 270 libusb20_be_free(usb_backend); 271 return (LIBUSB_ERROR_NO_MEM); 272 } 273 /* get device into libUSB v1.0 list */ 274 libusb20_be_dequeue_device(usb_backend, pdev); 275 276 memset(dev, 0, sizeof(*dev)); 277 278 /* init transfer queues */ 279 TAILQ_INIT(&dev->tr_head); 280 281 /* set context we belong to */ 282 dev->ctx = ctx; 283 284 /* link together the two structures */ 285 dev->os_priv = pdev; 286 pdev->privLuData = dev; 287 288 (*list)[i] = libusb_ref_device(dev); 289 i++; 290 } 291 (*list)[i] = NULL; 292 293 libusb20_be_free(usb_backend); 294 return (i); 295 } 296 297 void 298 libusb_free_device_list(libusb_device **list, int unref_devices) 299 { 300 int i; 301 302 if (list == NULL) 303 return; /* be NULL safe */ 304 305 if (unref_devices) { 306 for (i = 0; list[i] != NULL; i++) 307 libusb_unref_device(list[i]); 308 } 309 free(list); 310 } 311 312 uint8_t 313 libusb_get_bus_number(libusb_device *dev) 314 { 315 if (dev == NULL) 316 return (0); /* should not happen */ 317 return (libusb20_dev_get_bus_number(dev->os_priv)); 318 } 319 320 int 321 libusb_get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize) 322 { 323 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize)); 324 } 325 326 int 327 libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *buf, 328 uint8_t bufsize) 329 { 330 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize)); 331 } 332 333 uint8_t 334 libusb_get_device_address(libusb_device *dev) 335 { 336 if (dev == NULL) 337 return (0); /* should not happen */ 338 return (libusb20_dev_get_address(dev->os_priv)); 339 } 340 341 enum libusb_speed 342 libusb_get_device_speed(libusb_device *dev) 343 { 344 if (dev == NULL) 345 return (LIBUSB_SPEED_UNKNOWN); /* should not happen */ 346 347 switch (libusb20_dev_get_speed(dev->os_priv)) { 348 case LIBUSB20_SPEED_LOW: 349 return (LIBUSB_SPEED_LOW); 350 case LIBUSB20_SPEED_FULL: 351 return (LIBUSB_SPEED_FULL); 352 case LIBUSB20_SPEED_HIGH: 353 return (LIBUSB_SPEED_HIGH); 354 case LIBUSB20_SPEED_SUPER: 355 return (LIBUSB_SPEED_SUPER); 356 default: 357 break; 358 } 359 return (LIBUSB_SPEED_UNKNOWN); 360 } 361 362 int 363 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint) 364 { 365 struct libusb_config_descriptor *pdconf; 366 struct libusb_interface *pinf; 367 struct libusb_interface_descriptor *pdinf; 368 struct libusb_endpoint_descriptor *pdend; 369 int i; 370 int j; 371 int k; 372 int ret; 373 374 if (dev == NULL) 375 return (LIBUSB_ERROR_NO_DEVICE); 376 377 ret = libusb_get_active_config_descriptor(dev, &pdconf); 378 if (ret < 0) 379 return (ret); 380 381 ret = LIBUSB_ERROR_NOT_FOUND; 382 for (i = 0; i < pdconf->bNumInterfaces; i++) { 383 pinf = &pdconf->interface[i]; 384 for (j = 0; j < pinf->num_altsetting; j++) { 385 pdinf = &pinf->altsetting[j]; 386 for (k = 0; k < pdinf->bNumEndpoints; k++) { 387 pdend = &pdinf->endpoint[k]; 388 if (pdend->bEndpointAddress == endpoint) { 389 ret = pdend->wMaxPacketSize; 390 goto out; 391 } 392 } 393 } 394 } 395 396 out: 397 libusb_free_config_descriptor(pdconf); 398 return (ret); 399 } 400 401 int 402 libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint) 403 { 404 int multiplier; 405 int ret; 406 407 ret = libusb_get_max_packet_size(dev, endpoint); 408 409 switch (libusb20_dev_get_speed(dev->os_priv)) { 410 case LIBUSB20_SPEED_LOW: 411 case LIBUSB20_SPEED_FULL: 412 break; 413 default: 414 if (ret > -1) { 415 multiplier = (1 + ((ret >> 11) & 3)); 416 if (multiplier > 3) 417 multiplier = 3; 418 ret = (ret & 0x7FF) * multiplier; 419 } 420 break; 421 } 422 return (ret); 423 } 424 425 libusb_device * 426 libusb_ref_device(libusb_device *dev) 427 { 428 if (dev == NULL) 429 return (NULL); /* be NULL safe */ 430 431 CTX_LOCK(dev->ctx); 432 dev->refcnt++; 433 CTX_UNLOCK(dev->ctx); 434 435 return (dev); 436 } 437 438 void 439 libusb_unref_device(libusb_device *dev) 440 { 441 if (dev == NULL) 442 return; /* be NULL safe */ 443 444 CTX_LOCK(dev->ctx); 445 dev->refcnt--; 446 CTX_UNLOCK(dev->ctx); 447 448 if (dev->refcnt == 0) { 449 libusb20_dev_free(dev->os_priv); 450 free(dev); 451 } 452 } 453 454 int 455 libusb_open(libusb_device *dev, libusb_device_handle **devh) 456 { 457 libusb_context *ctx = dev->ctx; 458 struct libusb20_device *pdev = dev->os_priv; 459 uint8_t dummy; 460 int err; 461 462 if (devh == NULL) 463 return (LIBUSB_ERROR_INVALID_PARAM); 464 465 /* set default device handle value */ 466 *devh = NULL; 467 468 dev = libusb_ref_device(dev); 469 if (dev == NULL) 470 return (LIBUSB_ERROR_INVALID_PARAM); 471 472 err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ ); 473 if (err) { 474 libusb_unref_device(dev); 475 return (LIBUSB_ERROR_NO_MEM); 476 } 477 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN | 478 POLLOUT | POLLRDNORM | POLLWRNORM); 479 480 /* make sure our event loop detects the new device */ 481 dummy = 0; 482 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy)); 483 if (err < (int)sizeof(dummy)) { 484 /* ignore error, if any */ 485 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!"); 486 } 487 *devh = pdev; 488 489 return (0); 490 } 491 492 libusb_device_handle * 493 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id, 494 uint16_t product_id) 495 { 496 struct libusb_device **devs; 497 struct libusb20_device *pdev; 498 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc; 499 int i; 500 int j; 501 502 ctx = GET_CONTEXT(ctx); 503 if (ctx == NULL) 504 return (NULL); /* be NULL safe */ 505 506 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter"); 507 508 if ((i = libusb_get_device_list(ctx, &devs)) < 0) 509 return (NULL); 510 511 pdev = NULL; 512 for (j = 0; j < i; j++) { 513 struct libusb20_device *tdev; 514 515 tdev = devs[j]->os_priv; 516 pdesc = libusb20_dev_get_device_desc(tdev); 517 /* 518 * NOTE: The USB library will automatically swap the 519 * fields in the device descriptor to be of host 520 * endian type! 521 */ 522 if (pdesc->idVendor == vendor_id && 523 pdesc->idProduct == product_id) { 524 libusb_open(devs[j], &pdev); 525 break; 526 } 527 } 528 529 libusb_free_device_list(devs, 1); 530 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave"); 531 return (pdev); 532 } 533 534 void 535 libusb_close(struct libusb20_device *pdev) 536 { 537 libusb_context *ctx; 538 struct libusb_device *dev; 539 uint8_t dummy; 540 int err; 541 542 if (pdev == NULL) 543 return; /* be NULL safe */ 544 545 dev = libusb_get_device(pdev); 546 ctx = dev->ctx; 547 548 libusb10_remove_pollfd(ctx, &dev->dev_poll); 549 550 libusb20_dev_close(pdev); 551 552 /* unref will free the "pdev" when the refcount reaches zero */ 553 libusb_unref_device(dev); 554 555 /* make sure our event loop detects the closed device */ 556 dummy = 0; 557 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy)); 558 if (err < (int)sizeof(dummy)) { 559 /* ignore error, if any */ 560 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!"); 561 } 562 } 563 564 libusb_device * 565 libusb_get_device(struct libusb20_device *pdev) 566 { 567 if (pdev == NULL) 568 return (NULL); 569 return ((libusb_device *)pdev->privLuData); 570 } 571 572 int 573 libusb_get_configuration(struct libusb20_device *pdev, int *config) 574 { 575 struct libusb20_config *pconf; 576 577 if (pdev == NULL || config == NULL) 578 return (LIBUSB_ERROR_INVALID_PARAM); 579 580 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev)); 581 if (pconf == NULL) 582 return (LIBUSB_ERROR_NO_MEM); 583 584 *config = pconf->desc.bConfigurationValue; 585 586 free(pconf); 587 588 return (0); 589 } 590 591 int 592 libusb_set_configuration(struct libusb20_device *pdev, int configuration) 593 { 594 struct libusb20_config *pconf; 595 struct libusb_device *dev; 596 int err; 597 uint8_t i; 598 599 dev = libusb_get_device(pdev); 600 if (dev == NULL) 601 return (LIBUSB_ERROR_INVALID_PARAM); 602 603 if (configuration < 1) { 604 /* unconfigure */ 605 i = 255; 606 } else { 607 for (i = 0; i != 255; i++) { 608 uint8_t found; 609 610 pconf = libusb20_dev_alloc_config(pdev, i); 611 if (pconf == NULL) 612 return (LIBUSB_ERROR_INVALID_PARAM); 613 found = (pconf->desc.bConfigurationValue 614 == configuration); 615 free(pconf); 616 617 if (found) 618 goto set_config; 619 } 620 return (LIBUSB_ERROR_INVALID_PARAM); 621 } 622 623 set_config: 624 625 libusb10_cancel_all_transfer(dev); 626 627 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll); 628 629 err = libusb20_dev_set_config_index(pdev, i); 630 631 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN | 632 POLLOUT | POLLRDNORM | POLLWRNORM); 633 634 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0); 635 } 636 637 int 638 libusb_claim_interface(struct libusb20_device *pdev, int interface_number) 639 { 640 libusb_device *dev; 641 642 dev = libusb_get_device(pdev); 643 if (dev == NULL) 644 return (LIBUSB_ERROR_INVALID_PARAM); 645 646 if (interface_number < 0 || interface_number > 31) 647 return (LIBUSB_ERROR_INVALID_PARAM); 648 649 CTX_LOCK(dev->ctx); 650 dev->claimed_interfaces |= (1 << interface_number); 651 CTX_UNLOCK(dev->ctx); 652 653 return (0); 654 } 655 656 int 657 libusb_release_interface(struct libusb20_device *pdev, int interface_number) 658 { 659 libusb_device *dev; 660 int err = 0; 661 662 dev = libusb_get_device(pdev); 663 if (dev == NULL) 664 return (LIBUSB_ERROR_INVALID_PARAM); 665 666 if (interface_number < 0 || interface_number > 31) 667 return (LIBUSB_ERROR_INVALID_PARAM); 668 669 CTX_LOCK(dev->ctx); 670 if (!(dev->claimed_interfaces & (1 << interface_number))) 671 err = LIBUSB_ERROR_NOT_FOUND; 672 673 if (!err) 674 dev->claimed_interfaces &= ~(1 << interface_number); 675 CTX_UNLOCK(dev->ctx); 676 return (err); 677 } 678 679 int 680 libusb_set_interface_alt_setting(struct libusb20_device *pdev, 681 int interface_number, int alternate_setting) 682 { 683 libusb_device *dev; 684 int err = 0; 685 686 dev = libusb_get_device(pdev); 687 if (dev == NULL) 688 return (LIBUSB_ERROR_INVALID_PARAM); 689 690 if (interface_number < 0 || interface_number > 31) 691 return (LIBUSB_ERROR_INVALID_PARAM); 692 693 CTX_LOCK(dev->ctx); 694 if (!(dev->claimed_interfaces & (1 << interface_number))) 695 err = LIBUSB_ERROR_NOT_FOUND; 696 CTX_UNLOCK(dev->ctx); 697 698 if (err) 699 return (err); 700 701 libusb10_cancel_all_transfer(dev); 702 703 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll); 704 705 err = libusb20_dev_set_alt_index(pdev, 706 interface_number, alternate_setting); 707 708 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, 709 pdev, libusb20_dev_get_fd(pdev), 710 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); 711 712 return (err ? LIBUSB_ERROR_OTHER : 0); 713 } 714 715 static struct libusb20_transfer * 716 libusb10_get_transfer(struct libusb20_device *pdev, 717 uint8_t endpoint, uint8_t xfer_index) 718 { 719 xfer_index &= 1; /* double buffering */ 720 721 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4; 722 723 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) { 724 /* this is an IN endpoint */ 725 xfer_index |= 2; 726 } 727 return (libusb20_tr_get_pointer(pdev, xfer_index)); 728 } 729 730 int 731 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint) 732 { 733 struct libusb20_transfer *xfer; 734 struct libusb_device *dev; 735 int err; 736 737 xfer = libusb10_get_transfer(pdev, endpoint, 0); 738 if (xfer == NULL) 739 return (LIBUSB_ERROR_INVALID_PARAM); 740 741 dev = libusb_get_device(pdev); 742 if (dev == NULL) 743 return (LIBUSB_ERROR_INVALID_PARAM); 744 745 CTX_LOCK(dev->ctx); 746 err = libusb20_tr_open(xfer, 0, 1, endpoint); 747 CTX_UNLOCK(dev->ctx); 748 749 if (err != 0 && err != LIBUSB20_ERROR_BUSY) 750 return (LIBUSB_ERROR_OTHER); 751 752 libusb20_tr_clear_stall_sync(xfer); 753 754 /* check if we opened the transfer */ 755 if (err == 0) { 756 CTX_LOCK(dev->ctx); 757 libusb20_tr_close(xfer); 758 CTX_UNLOCK(dev->ctx); 759 } 760 return (0); /* success */ 761 } 762 763 int 764 libusb_reset_device(struct libusb20_device *pdev) 765 { 766 libusb_device *dev; 767 int err; 768 769 dev = libusb_get_device(pdev); 770 if (dev == NULL) 771 return (LIBUSB_ERROR_INVALID_PARAM); 772 773 libusb10_cancel_all_transfer(dev); 774 775 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll); 776 777 err = libusb20_dev_reset(pdev); 778 779 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, 780 pdev, libusb20_dev_get_fd(pdev), 781 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); 782 783 return (err ? LIBUSB_ERROR_OTHER : 0); 784 } 785 786 int 787 libusb_check_connected(struct libusb20_device *pdev) 788 { 789 libusb_device *dev; 790 int err; 791 792 dev = libusb_get_device(pdev); 793 if (dev == NULL) 794 return (LIBUSB_ERROR_INVALID_PARAM); 795 796 err = libusb20_dev_check_connected(pdev); 797 798 return (err ? LIBUSB_ERROR_NO_DEVICE : 0); 799 } 800 801 int 802 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface) 803 { 804 if (pdev == NULL) 805 return (LIBUSB_ERROR_INVALID_PARAM); 806 807 if (libusb20_dev_kernel_driver_active(pdev, interface)) 808 return (0); /* no kernel driver is active */ 809 else 810 return (1); /* kernel driver is active */ 811 } 812 813 int 814 libusb_get_driver_np(struct libusb20_device *pdev, int interface, 815 char *name, int namelen) 816 { 817 return (libusb_get_driver(pdev, interface, name, namelen)); 818 } 819 820 int 821 libusb_get_driver(struct libusb20_device *pdev, int interface, 822 char *name, int namelen) 823 { 824 char *ptr; 825 int err; 826 827 if (pdev == NULL) 828 return (LIBUSB_ERROR_INVALID_PARAM); 829 if (namelen < 1) 830 return (LIBUSB_ERROR_INVALID_PARAM); 831 if (namelen > 255) 832 namelen = 255; 833 834 err = libusb20_dev_get_iface_desc( 835 pdev, interface, name, namelen); 836 837 if (err != 0) 838 return (LIBUSB_ERROR_OTHER); 839 840 /* we only want the driver name */ 841 ptr = strstr(name, ":"); 842 if (ptr != NULL) 843 *ptr = 0; 844 845 return (0); 846 } 847 848 int 849 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface) 850 { 851 return (libusb_detach_kernel_driver(pdev, interface)); 852 } 853 854 int 855 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface) 856 { 857 int err; 858 859 if (pdev == NULL) 860 return (LIBUSB_ERROR_INVALID_PARAM); 861 862 err = libusb20_dev_detach_kernel_driver( 863 pdev, interface); 864 865 return (err ? LIBUSB_ERROR_OTHER : 0); 866 } 867 868 int 869 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface) 870 { 871 if (pdev == NULL) 872 return (LIBUSB_ERROR_INVALID_PARAM); 873 /* stub - currently not supported by libusb20 */ 874 return (0); 875 } 876 877 /* Asynchronous device I/O */ 878 879 struct libusb_transfer * 880 libusb_alloc_transfer(int iso_packets) 881 { 882 struct libusb_transfer *uxfer; 883 struct libusb_super_transfer *sxfer; 884 int len; 885 886 len = sizeof(struct libusb_transfer) + 887 sizeof(struct libusb_super_transfer) + 888 (iso_packets * sizeof(libusb_iso_packet_descriptor)); 889 890 sxfer = malloc(len); 891 if (sxfer == NULL) 892 return (NULL); 893 894 memset(sxfer, 0, len); 895 896 uxfer = (struct libusb_transfer *)( 897 ((uint8_t *)sxfer) + sizeof(*sxfer)); 898 899 /* set default value */ 900 uxfer->num_iso_packets = iso_packets; 901 902 return (uxfer); 903 } 904 905 void 906 libusb_free_transfer(struct libusb_transfer *uxfer) 907 { 908 struct libusb_super_transfer *sxfer; 909 910 if (uxfer == NULL) 911 return; /* be NULL safe */ 912 913 /* check if we should free the transfer buffer */ 914 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER) 915 free(uxfer->buffer); 916 917 sxfer = (struct libusb_super_transfer *)( 918 (uint8_t *)uxfer - sizeof(*sxfer)); 919 920 free(sxfer); 921 } 922 923 static uint32_t 924 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer) 925 { 926 uint32_t ret; 927 928 switch (xfer->type) { 929 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: 930 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */ 931 break; 932 case LIBUSB_TRANSFER_TYPE_CONTROL: 933 ret = 2; 934 break; 935 default: 936 ret = 1; 937 break; 938 } 939 return (ret); 940 } 941 942 static int 943 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer) 944 { 945 int ret; 946 int usb_speed; 947 948 usb_speed = libusb20_dev_get_speed(pdev); 949 950 switch (xfer->type) { 951 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: 952 ret = 0; /* kernel will auto-select */ 953 break; 954 case LIBUSB_TRANSFER_TYPE_CONTROL: 955 ret = 1024; 956 break; 957 default: 958 switch (usb_speed) { 959 case LIBUSB20_SPEED_LOW: 960 ret = 256; 961 break; 962 case LIBUSB20_SPEED_FULL: 963 ret = 4096; 964 break; 965 case LIBUSB20_SPEED_SUPER: 966 ret = 65536; 967 break; 968 default: 969 ret = 16384; 970 break; 971 } 972 break; 973 } 974 return (ret); 975 } 976 977 static int 978 libusb10_convert_error(uint8_t status) 979 { 980 ; /* indent fix */ 981 982 switch (status) { 983 case LIBUSB20_TRANSFER_START: 984 case LIBUSB20_TRANSFER_COMPLETED: 985 return (LIBUSB_TRANSFER_COMPLETED); 986 case LIBUSB20_TRANSFER_OVERFLOW: 987 return (LIBUSB_TRANSFER_OVERFLOW); 988 case LIBUSB20_TRANSFER_NO_DEVICE: 989 return (LIBUSB_TRANSFER_NO_DEVICE); 990 case LIBUSB20_TRANSFER_STALL: 991 return (LIBUSB_TRANSFER_STALL); 992 case LIBUSB20_TRANSFER_CANCELLED: 993 return (LIBUSB_TRANSFER_CANCELLED); 994 case LIBUSB20_TRANSFER_TIMED_OUT: 995 return (LIBUSB_TRANSFER_TIMED_OUT); 996 default: 997 return (LIBUSB_TRANSFER_ERROR); 998 } 999 } 1000 1001 /* This function must be called locked */ 1002 1003 static void 1004 libusb10_complete_transfer(struct libusb20_transfer *pxfer, 1005 struct libusb_super_transfer *sxfer, int status) 1006 { 1007 struct libusb_transfer *uxfer; 1008 struct libusb_device *dev; 1009 1010 uxfer = (struct libusb_transfer *)( 1011 ((uint8_t *)sxfer) + sizeof(*sxfer)); 1012 1013 if (pxfer != NULL) 1014 libusb20_tr_set_priv_sc1(pxfer, NULL); 1015 1016 /* set transfer status */ 1017 uxfer->status = status; 1018 1019 /* update super transfer state */ 1020 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE; 1021 1022 dev = libusb_get_device(uxfer->dev_handle); 1023 1024 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry); 1025 } 1026 1027 /* This function must be called locked */ 1028 1029 static void 1030 libusb10_isoc_proxy(struct libusb20_transfer *pxfer) 1031 { 1032 struct libusb_super_transfer *sxfer; 1033 struct libusb_transfer *uxfer; 1034 uint32_t actlen; 1035 uint16_t iso_packets; 1036 uint16_t i; 1037 uint8_t status; 1038 uint8_t flags; 1039 1040 status = libusb20_tr_get_status(pxfer); 1041 sxfer = libusb20_tr_get_priv_sc1(pxfer); 1042 actlen = libusb20_tr_get_actual_length(pxfer); 1043 iso_packets = libusb20_tr_get_max_frames(pxfer); 1044 1045 if (sxfer == NULL) 1046 return; /* cancelled - nothing to do */ 1047 1048 uxfer = (struct libusb_transfer *)( 1049 ((uint8_t *)sxfer) + sizeof(*sxfer)); 1050 1051 if (iso_packets > uxfer->num_iso_packets) 1052 iso_packets = uxfer->num_iso_packets; 1053 1054 if (iso_packets == 0) 1055 return; /* nothing to do */ 1056 1057 /* make sure that the number of ISOCHRONOUS packets is valid */ 1058 uxfer->num_iso_packets = iso_packets; 1059 1060 flags = uxfer->flags; 1061 1062 switch (status) { 1063 case LIBUSB20_TRANSFER_COMPLETED: 1064 1065 /* update actual length */ 1066 uxfer->actual_length = actlen; 1067 for (i = 0; i != iso_packets; i++) { 1068 uxfer->iso_packet_desc[i].actual_length = 1069 libusb20_tr_get_length(pxfer, i); 1070 } 1071 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); 1072 break; 1073 1074 case LIBUSB20_TRANSFER_START: 1075 1076 /* setup length(s) */ 1077 actlen = 0; 1078 for (i = 0; i != iso_packets; i++) { 1079 libusb20_tr_setup_isoc(pxfer, 1080 &uxfer->buffer[actlen], 1081 uxfer->iso_packet_desc[i].length, i); 1082 actlen += uxfer->iso_packet_desc[i].length; 1083 } 1084 1085 /* no remainder */ 1086 sxfer->rem_len = 0; 1087 1088 libusb20_tr_set_total_frames(pxfer, iso_packets); 1089 libusb20_tr_submit(pxfer); 1090 1091 /* fork another USB transfer, if any */ 1092 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint); 1093 break; 1094 1095 default: 1096 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status)); 1097 break; 1098 } 1099 } 1100 1101 /* This function must be called locked */ 1102 1103 static void 1104 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer) 1105 { 1106 struct libusb_super_transfer *sxfer; 1107 struct libusb_transfer *uxfer; 1108 uint32_t max_bulk; 1109 uint32_t actlen; 1110 uint8_t status; 1111 uint8_t flags; 1112 1113 status = libusb20_tr_get_status(pxfer); 1114 sxfer = libusb20_tr_get_priv_sc1(pxfer); 1115 max_bulk = libusb20_tr_get_max_total_length(pxfer); 1116 actlen = libusb20_tr_get_actual_length(pxfer); 1117 1118 if (sxfer == NULL) 1119 return; /* cancelled - nothing to do */ 1120 1121 uxfer = (struct libusb_transfer *)( 1122 ((uint8_t *)sxfer) + sizeof(*sxfer)); 1123 1124 flags = uxfer->flags; 1125 1126 switch (status) { 1127 case LIBUSB20_TRANSFER_COMPLETED: 1128 1129 uxfer->actual_length += actlen; 1130 1131 /* check for short packet */ 1132 if (sxfer->last_len != actlen) { 1133 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) { 1134 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR); 1135 } else { 1136 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); 1137 } 1138 break; 1139 } 1140 /* check for end of data */ 1141 if (sxfer->rem_len == 0) { 1142 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); 1143 break; 1144 } 1145 /* FALLTHROUGH */ 1146 1147 case LIBUSB20_TRANSFER_START: 1148 if (max_bulk > sxfer->rem_len) { 1149 max_bulk = sxfer->rem_len; 1150 } 1151 /* setup new BULK or INTERRUPT transaction */ 1152 libusb20_tr_setup_bulk(pxfer, 1153 sxfer->curr_data, max_bulk, uxfer->timeout); 1154 1155 /* update counters */ 1156 sxfer->last_len = max_bulk; 1157 sxfer->curr_data += max_bulk; 1158 sxfer->rem_len -= max_bulk; 1159 1160 libusb20_tr_submit(pxfer); 1161 1162 /* check if we can fork another USB transfer */ 1163 if (sxfer->rem_len == 0) 1164 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint); 1165 break; 1166 1167 default: 1168 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status)); 1169 break; 1170 } 1171 } 1172 1173 /* This function must be called locked */ 1174 1175 static void 1176 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer) 1177 { 1178 struct libusb_super_transfer *sxfer; 1179 struct libusb_transfer *uxfer; 1180 uint32_t max_bulk; 1181 uint32_t actlen; 1182 uint8_t status; 1183 uint8_t flags; 1184 1185 status = libusb20_tr_get_status(pxfer); 1186 sxfer = libusb20_tr_get_priv_sc1(pxfer); 1187 max_bulk = libusb20_tr_get_max_total_length(pxfer); 1188 actlen = libusb20_tr_get_actual_length(pxfer); 1189 1190 if (sxfer == NULL) 1191 return; /* cancelled - nothing to do */ 1192 1193 uxfer = (struct libusb_transfer *)( 1194 ((uint8_t *)sxfer) + sizeof(*sxfer)); 1195 1196 flags = uxfer->flags; 1197 1198 switch (status) { 1199 case LIBUSB20_TRANSFER_COMPLETED: 1200 1201 uxfer->actual_length += actlen; 1202 1203 /* subtract length of SETUP packet, if any */ 1204 actlen -= libusb20_tr_get_length(pxfer, 0); 1205 1206 /* check for short packet */ 1207 if (sxfer->last_len != actlen) { 1208 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) { 1209 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR); 1210 } else { 1211 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); 1212 } 1213 break; 1214 } 1215 /* check for end of data */ 1216 if (sxfer->rem_len == 0) { 1217 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); 1218 break; 1219 } 1220 /* FALLTHROUGH */ 1221 1222 case LIBUSB20_TRANSFER_START: 1223 if (max_bulk > sxfer->rem_len) { 1224 max_bulk = sxfer->rem_len; 1225 } 1226 /* setup new CONTROL transaction */ 1227 if (status == LIBUSB20_TRANSFER_COMPLETED) { 1228 /* next fragment - don't send SETUP packet */ 1229 libusb20_tr_set_length(pxfer, 0, 0); 1230 } else { 1231 /* first fragment - send SETUP packet */ 1232 libusb20_tr_set_length(pxfer, 8, 0); 1233 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0); 1234 } 1235 1236 if (max_bulk != 0) { 1237 libusb20_tr_set_length(pxfer, max_bulk, 1); 1238 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1); 1239 libusb20_tr_set_total_frames(pxfer, 2); 1240 } else { 1241 libusb20_tr_set_total_frames(pxfer, 1); 1242 } 1243 1244 /* update counters */ 1245 sxfer->last_len = max_bulk; 1246 sxfer->curr_data += max_bulk; 1247 sxfer->rem_len -= max_bulk; 1248 1249 libusb20_tr_submit(pxfer); 1250 1251 /* check if we can fork another USB transfer */ 1252 if (sxfer->rem_len == 0) 1253 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint); 1254 break; 1255 1256 default: 1257 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status)); 1258 break; 1259 } 1260 } 1261 1262 /* The following function must be called locked */ 1263 1264 static void 1265 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint) 1266 { 1267 struct libusb20_transfer *pxfer0; 1268 struct libusb20_transfer *pxfer1; 1269 struct libusb_super_transfer *sxfer; 1270 struct libusb_transfer *uxfer; 1271 struct libusb_device *dev; 1272 int err; 1273 int buffsize; 1274 int maxframe; 1275 int temp; 1276 uint8_t dummy; 1277 1278 dev = libusb_get_device(pdev); 1279 1280 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0); 1281 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1); 1282 1283 if (pxfer0 == NULL || pxfer1 == NULL) 1284 return; /* shouldn't happen */ 1285 1286 temp = 0; 1287 if (libusb20_tr_pending(pxfer0)) 1288 temp |= 1; 1289 if (libusb20_tr_pending(pxfer1)) 1290 temp |= 2; 1291 1292 switch (temp) { 1293 case 3: 1294 /* wait till one of the transfers complete */ 1295 return; 1296 case 2: 1297 sxfer = libusb20_tr_get_priv_sc1(pxfer1); 1298 if (sxfer == NULL) 1299 return; /* cancelling */ 1300 if (sxfer->rem_len) 1301 return; /* cannot queue another one */ 1302 /* swap transfers */ 1303 pxfer1 = pxfer0; 1304 break; 1305 case 1: 1306 sxfer = libusb20_tr_get_priv_sc1(pxfer0); 1307 if (sxfer == NULL) 1308 return; /* cancelling */ 1309 if (sxfer->rem_len) 1310 return; /* cannot queue another one */ 1311 /* swap transfers */ 1312 pxfer0 = pxfer1; 1313 break; 1314 default: 1315 break; 1316 } 1317 1318 /* find next transfer on same endpoint */ 1319 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) { 1320 1321 uxfer = (struct libusb_transfer *)( 1322 ((uint8_t *)sxfer) + sizeof(*sxfer)); 1323 1324 if (uxfer->endpoint == endpoint) { 1325 TAILQ_REMOVE(&dev->tr_head, sxfer, entry); 1326 sxfer->entry.tqe_prev = NULL; 1327 goto found; 1328 } 1329 } 1330 return; /* success */ 1331 1332 found: 1333 1334 libusb20_tr_set_priv_sc0(pxfer0, pdev); 1335 libusb20_tr_set_priv_sc1(pxfer0, sxfer); 1336 1337 /* reset super transfer state */ 1338 sxfer->rem_len = uxfer->length; 1339 sxfer->curr_data = uxfer->buffer; 1340 uxfer->actual_length = 0; 1341 1342 switch (uxfer->type) { 1343 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: 1344 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy); 1345 break; 1346 case LIBUSB_TRANSFER_TYPE_BULK: 1347 case LIBUSB_TRANSFER_TYPE_INTERRUPT: 1348 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy); 1349 break; 1350 case LIBUSB_TRANSFER_TYPE_CONTROL: 1351 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy); 1352 if (sxfer->rem_len < 8) 1353 goto failure; 1354 1355 /* remove SETUP packet from data */ 1356 sxfer->rem_len -= 8; 1357 sxfer->curr_data += 8; 1358 break; 1359 default: 1360 goto failure; 1361 } 1362 1363 buffsize = libusb10_get_buffsize(pdev, uxfer); 1364 maxframe = libusb10_get_maxframe(pdev, uxfer); 1365 1366 /* make sure the transfer is opened */ 1367 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint); 1368 if (err && (err != LIBUSB20_ERROR_BUSY)) { 1369 goto failure; 1370 } 1371 libusb20_tr_start(pxfer0); 1372 return; 1373 1374 failure: 1375 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR); 1376 1377 /* make sure our event loop spins the done handler */ 1378 dummy = 0; 1379 err = write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy)); 1380 } 1381 1382 /* The following function must be called unlocked */ 1383 1384 int 1385 libusb_submit_transfer(struct libusb_transfer *uxfer) 1386 { 1387 struct libusb20_transfer *pxfer0; 1388 struct libusb20_transfer *pxfer1; 1389 struct libusb_super_transfer *sxfer; 1390 struct libusb_device *dev; 1391 uint8_t endpoint; 1392 int err; 1393 1394 if (uxfer == NULL) 1395 return (LIBUSB_ERROR_INVALID_PARAM); 1396 1397 if (uxfer->dev_handle == NULL) 1398 return (LIBUSB_ERROR_INVALID_PARAM); 1399 1400 endpoint = uxfer->endpoint; 1401 1402 dev = libusb_get_device(uxfer->dev_handle); 1403 1404 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter"); 1405 1406 sxfer = (struct libusb_super_transfer *)( 1407 (uint8_t *)uxfer - sizeof(*sxfer)); 1408 1409 CTX_LOCK(dev->ctx); 1410 1411 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0); 1412 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1); 1413 1414 if (pxfer0 == NULL || pxfer1 == NULL) { 1415 err = LIBUSB_ERROR_OTHER; 1416 } else if ((sxfer->entry.tqe_prev != NULL) || 1417 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) || 1418 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) { 1419 err = LIBUSB_ERROR_BUSY; 1420 } else { 1421 1422 /* set pending state */ 1423 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND; 1424 1425 /* insert transfer into transfer head list */ 1426 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry); 1427 1428 /* start work transfers */ 1429 libusb10_submit_transfer_sub( 1430 uxfer->dev_handle, endpoint); 1431 1432 err = 0; /* success */ 1433 } 1434 1435 CTX_UNLOCK(dev->ctx); 1436 1437 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err); 1438 1439 return (err); 1440 } 1441 1442 /* Asynchronous transfer cancel */ 1443 1444 int 1445 libusb_cancel_transfer(struct libusb_transfer *uxfer) 1446 { 1447 struct libusb20_transfer *pxfer0; 1448 struct libusb20_transfer *pxfer1; 1449 struct libusb_super_transfer *sxfer; 1450 struct libusb_device *dev; 1451 uint8_t endpoint; 1452 int retval; 1453 1454 if (uxfer == NULL) 1455 return (LIBUSB_ERROR_INVALID_PARAM); 1456 1457 /* check if not initialised */ 1458 if (uxfer->dev_handle == NULL) 1459 return (LIBUSB_ERROR_NOT_FOUND); 1460 1461 endpoint = uxfer->endpoint; 1462 1463 dev = libusb_get_device(uxfer->dev_handle); 1464 1465 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter"); 1466 1467 sxfer = (struct libusb_super_transfer *)( 1468 (uint8_t *)uxfer - sizeof(*sxfer)); 1469 1470 retval = 0; 1471 1472 CTX_LOCK(dev->ctx); 1473 1474 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0); 1475 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1); 1476 1477 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) { 1478 /* only update the transfer status */ 1479 uxfer->status = LIBUSB_TRANSFER_CANCELLED; 1480 retval = LIBUSB_ERROR_NOT_FOUND; 1481 } else if (sxfer->entry.tqe_prev != NULL) { 1482 /* we are lucky - transfer is on a queue */ 1483 TAILQ_REMOVE(&dev->tr_head, sxfer, entry); 1484 sxfer->entry.tqe_prev = NULL; 1485 libusb10_complete_transfer(NULL, 1486 sxfer, LIBUSB_TRANSFER_CANCELLED); 1487 } else if (pxfer0 == NULL || pxfer1 == NULL) { 1488 /* not started */ 1489 retval = LIBUSB_ERROR_NOT_FOUND; 1490 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) { 1491 libusb10_complete_transfer(pxfer0, 1492 sxfer, LIBUSB_TRANSFER_CANCELLED); 1493 libusb20_tr_stop(pxfer0); 1494 /* make sure the queue doesn't stall */ 1495 libusb10_submit_transfer_sub( 1496 uxfer->dev_handle, endpoint); 1497 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) { 1498 libusb10_complete_transfer(pxfer1, 1499 sxfer, LIBUSB_TRANSFER_CANCELLED); 1500 libusb20_tr_stop(pxfer1); 1501 /* make sure the queue doesn't stall */ 1502 libusb10_submit_transfer_sub( 1503 uxfer->dev_handle, endpoint); 1504 } else { 1505 /* not started */ 1506 retval = LIBUSB_ERROR_NOT_FOUND; 1507 } 1508 1509 CTX_UNLOCK(dev->ctx); 1510 1511 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave"); 1512 1513 return (retval); 1514 } 1515 1516 UNEXPORTED void 1517 libusb10_cancel_all_transfer(libusb_device *dev) 1518 { 1519 /* TODO */ 1520 } 1521 1522 uint16_t 1523 libusb_cpu_to_le16(uint16_t x) 1524 { 1525 return (htole16(x)); 1526 } 1527 1528 uint16_t 1529 libusb_le16_to_cpu(uint16_t x) 1530 { 1531 return (le16toh(x)); 1532 } 1533 1534 const char * 1535 libusb_strerror(int code) 1536 { 1537 switch (code) { 1538 case LIBUSB_SUCCESS: 1539 return ("Success"); 1540 case LIBUSB_ERROR_IO: 1541 return ("I/O error"); 1542 case LIBUSB_ERROR_INVALID_PARAM: 1543 return ("Invalid parameter"); 1544 case LIBUSB_ERROR_ACCESS: 1545 return ("Permissions error"); 1546 case LIBUSB_ERROR_NO_DEVICE: 1547 return ("No device"); 1548 case LIBUSB_ERROR_NOT_FOUND: 1549 return ("Not found"); 1550 case LIBUSB_ERROR_BUSY: 1551 return ("Device busy"); 1552 case LIBUSB_ERROR_TIMEOUT: 1553 return ("Timeout"); 1554 case LIBUSB_ERROR_OVERFLOW: 1555 return ("Overflow"); 1556 case LIBUSB_ERROR_PIPE: 1557 return ("Pipe error"); 1558 case LIBUSB_ERROR_INTERRUPTED: 1559 return ("Interrupted"); 1560 case LIBUSB_ERROR_NO_MEM: 1561 return ("Out of memory"); 1562 case LIBUSB_ERROR_NOT_SUPPORTED: 1563 return ("Not supported"); 1564 case LIBUSB_ERROR_OTHER: 1565 return ("Other error"); 1566 default: 1567 return ("Unknown error"); 1568 } 1569 } 1570 1571 const char * 1572 libusb_error_name(int code) 1573 { 1574 switch (code) { 1575 case LIBUSB_SUCCESS: 1576 return ("LIBUSB_SUCCESS"); 1577 case LIBUSB_ERROR_IO: 1578 return ("LIBUSB_ERROR_IO"); 1579 case LIBUSB_ERROR_INVALID_PARAM: 1580 return ("LIBUSB_ERROR_INVALID_PARAM"); 1581 case LIBUSB_ERROR_ACCESS: 1582 return ("LIBUSB_ERROR_ACCESS"); 1583 case LIBUSB_ERROR_NO_DEVICE: 1584 return ("LIBUSB_ERROR_NO_DEVICE"); 1585 case LIBUSB_ERROR_NOT_FOUND: 1586 return ("LIBUSB_ERROR_NOT_FOUND"); 1587 case LIBUSB_ERROR_BUSY: 1588 return ("LIBUSB_ERROR_BUSY"); 1589 case LIBUSB_ERROR_TIMEOUT: 1590 return ("LIBUSB_ERROR_TIMEOUT"); 1591 case LIBUSB_ERROR_OVERFLOW: 1592 return ("LIBUSB_ERROR_OVERFLOW"); 1593 case LIBUSB_ERROR_PIPE: 1594 return ("LIBUSB_ERROR_PIPE"); 1595 case LIBUSB_ERROR_INTERRUPTED: 1596 return ("LIBUSB_ERROR_INTERRUPTED"); 1597 case LIBUSB_ERROR_NO_MEM: 1598 return ("LIBUSB_ERROR_NO_MEM"); 1599 case LIBUSB_ERROR_NOT_SUPPORTED: 1600 return ("LIBUSB_ERROR_NOT_SUPPORTED"); 1601 case LIBUSB_ERROR_OTHER: 1602 return ("LIBUSB_ERROR_OTHER"); 1603 default: 1604 return ("LIBUSB_ERROR_UNKNOWN"); 1605 } 1606 } 1607