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