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