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