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