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