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 int err = 0; 641 642 dev = libusb_get_device(pdev); 643 if (dev == NULL) 644 return (LIBUSB_ERROR_INVALID_PARAM); 645 646 if (interface_number < 0 || interface_number > 31) 647 return (LIBUSB_ERROR_INVALID_PARAM); 648 649 if (pdev->auto_detach != 0) { 650 err = libusb_detach_kernel_driver(pdev, interface_number); 651 if (err != 0) 652 goto done; 653 } 654 655 CTX_LOCK(dev->ctx); 656 dev->claimed_interfaces |= (1 << interface_number); 657 CTX_UNLOCK(dev->ctx); 658done: 659 return (err); 660} 661 662int 663libusb_release_interface(struct libusb20_device *pdev, int interface_number) 664{ 665 libusb_device *dev; 666 int err = 0; 667 668 dev = libusb_get_device(pdev); 669 if (dev == NULL) 670 return (LIBUSB_ERROR_INVALID_PARAM); 671 672 if (interface_number < 0 || interface_number > 31) 673 return (LIBUSB_ERROR_INVALID_PARAM); 674 675 if (pdev->auto_detach != 0) { 676 err = libusb_attach_kernel_driver(pdev, interface_number); 677 if (err != 0) 678 goto done; 679 } 680 681 CTX_LOCK(dev->ctx); 682 if (!(dev->claimed_interfaces & (1 << interface_number))) 683 err = LIBUSB_ERROR_NOT_FOUND; 684 else 685 dev->claimed_interfaces &= ~(1 << interface_number); 686 CTX_UNLOCK(dev->ctx); 687done: 688 return (err); 689} 690 691int 692libusb_set_interface_alt_setting(struct libusb20_device *pdev, 693 int interface_number, int alternate_setting) 694{ 695 libusb_device *dev; 696 int err = 0; 697 698 dev = libusb_get_device(pdev); 699 if (dev == NULL) 700 return (LIBUSB_ERROR_INVALID_PARAM); 701 702 if (interface_number < 0 || interface_number > 31) 703 return (LIBUSB_ERROR_INVALID_PARAM); 704 705 CTX_LOCK(dev->ctx); 706 if (!(dev->claimed_interfaces & (1 << interface_number))) 707 err = LIBUSB_ERROR_NOT_FOUND; 708 CTX_UNLOCK(dev->ctx); 709 710 if (err) 711 return (err); 712 713 libusb10_cancel_all_transfer(dev); 714 715 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll); 716 717 err = libusb20_dev_set_alt_index(pdev, 718 interface_number, alternate_setting); 719 720 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, 721 pdev, libusb20_dev_get_fd(pdev), 722 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); 723 724 return (err ? LIBUSB_ERROR_OTHER : 0); 725} 726 727static struct libusb20_transfer * 728libusb10_get_transfer(struct libusb20_device *pdev, 729 uint8_t endpoint, uint8_t xfer_index) 730{ 731 xfer_index &= 1; /* double buffering */ 732 733 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4; 734 735 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) { 736 /* this is an IN endpoint */ 737 xfer_index |= 2; 738 } 739 return (libusb20_tr_get_pointer(pdev, xfer_index)); 740} 741 742int 743libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint) 744{ 745 struct libusb20_transfer *xfer; 746 struct libusb_device *dev; 747 int err; 748 749 xfer = libusb10_get_transfer(pdev, endpoint, 0); 750 if (xfer == NULL) 751 return (LIBUSB_ERROR_INVALID_PARAM); 752 753 dev = libusb_get_device(pdev); 754 if (dev == NULL) 755 return (LIBUSB_ERROR_INVALID_PARAM); 756 757 CTX_LOCK(dev->ctx); 758 err = libusb20_tr_open(xfer, 0, 1, endpoint); 759 CTX_UNLOCK(dev->ctx); 760 761 if (err != 0 && err != LIBUSB20_ERROR_BUSY) 762 return (LIBUSB_ERROR_OTHER); 763 764 libusb20_tr_clear_stall_sync(xfer); 765 766 /* check if we opened the transfer */ 767 if (err == 0) { 768 CTX_LOCK(dev->ctx); 769 libusb20_tr_close(xfer); 770 CTX_UNLOCK(dev->ctx); 771 } 772 return (0); /* success */ 773} 774 775int 776libusb_reset_device(struct libusb20_device *pdev) 777{ 778 libusb_device *dev; 779 int err; 780 781 dev = libusb_get_device(pdev); 782 if (dev == NULL) 783 return (LIBUSB_ERROR_INVALID_PARAM); 784 785 libusb10_cancel_all_transfer(dev); 786 787 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll); 788 789 err = libusb20_dev_reset(pdev); 790 791 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, 792 pdev, libusb20_dev_get_fd(pdev), 793 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); 794 795 return (err ? LIBUSB_ERROR_OTHER : 0); 796} 797 798int 799libusb_check_connected(struct libusb20_device *pdev) 800{ 801 libusb_device *dev; 802 int err; 803 804 dev = libusb_get_device(pdev); 805 if (dev == NULL) 806 return (LIBUSB_ERROR_INVALID_PARAM); 807 808 err = libusb20_dev_check_connected(pdev); 809 810 return (err ? LIBUSB_ERROR_NO_DEVICE : 0); 811} 812 813int 814libusb_kernel_driver_active(struct libusb20_device *pdev, int interface) 815{ 816 if (pdev == NULL) 817 return (LIBUSB_ERROR_INVALID_PARAM); 818 819 if (libusb20_dev_kernel_driver_active(pdev, interface)) 820 return (0); /* no kernel driver is active */ 821 else 822 return (1); /* kernel driver is active */ 823} 824 825int 826libusb_get_driver_np(struct libusb20_device *pdev, int interface, 827 char *name, int namelen) 828{ 829 return (libusb_get_driver(pdev, interface, name, namelen)); 830} 831 832int 833libusb_get_driver(struct libusb20_device *pdev, int interface, 834 char *name, int namelen) 835{ 836 char *ptr; 837 int err; 838 839 if (pdev == NULL) 840 return (LIBUSB_ERROR_INVALID_PARAM); 841 if (namelen < 1) 842 return (LIBUSB_ERROR_INVALID_PARAM); 843 if (namelen > 255) 844 namelen = 255; 845 846 err = libusb20_dev_get_iface_desc( 847 pdev, interface, name, namelen); 848 849 if (err != 0) 850 return (LIBUSB_ERROR_OTHER); 851 852 /* we only want the driver name */ 853 ptr = strstr(name, ":"); 854 if (ptr != NULL) 855 *ptr = 0; 856 857 return (0); 858} 859 860int 861libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface) 862{ 863 return (libusb_detach_kernel_driver(pdev, interface)); 864} 865 866int 867libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface) 868{ 869 int err; 870 871 if (pdev == NULL) 872 return (LIBUSB_ERROR_INVALID_PARAM); 873 874 err = libusb20_dev_detach_kernel_driver( 875 pdev, interface); 876 877 return (err ? LIBUSB_ERROR_OTHER : 0); 878} 879 880int 881libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface) 882{ 883 if (pdev == NULL) 884 return (LIBUSB_ERROR_INVALID_PARAM); 885 /* stub - currently not supported by libusb20 */ 886 return (0); 887} 888 889int 890libusb_set_auto_detach_kernel_driver(libusb_device_handle *dev, int enable) 891{ 892 dev->auto_detach = (enable ? 1 : 0);
| 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 int err = 0; 641 642 dev = libusb_get_device(pdev); 643 if (dev == NULL) 644 return (LIBUSB_ERROR_INVALID_PARAM); 645 646 if (interface_number < 0 || interface_number > 31) 647 return (LIBUSB_ERROR_INVALID_PARAM); 648 649 if (pdev->auto_detach != 0) { 650 err = libusb_detach_kernel_driver(pdev, interface_number); 651 if (err != 0) 652 goto done; 653 } 654 655 CTX_LOCK(dev->ctx); 656 dev->claimed_interfaces |= (1 << interface_number); 657 CTX_UNLOCK(dev->ctx); 658done: 659 return (err); 660} 661 662int 663libusb_release_interface(struct libusb20_device *pdev, int interface_number) 664{ 665 libusb_device *dev; 666 int err = 0; 667 668 dev = libusb_get_device(pdev); 669 if (dev == NULL) 670 return (LIBUSB_ERROR_INVALID_PARAM); 671 672 if (interface_number < 0 || interface_number > 31) 673 return (LIBUSB_ERROR_INVALID_PARAM); 674 675 if (pdev->auto_detach != 0) { 676 err = libusb_attach_kernel_driver(pdev, interface_number); 677 if (err != 0) 678 goto done; 679 } 680 681 CTX_LOCK(dev->ctx); 682 if (!(dev->claimed_interfaces & (1 << interface_number))) 683 err = LIBUSB_ERROR_NOT_FOUND; 684 else 685 dev->claimed_interfaces &= ~(1 << interface_number); 686 CTX_UNLOCK(dev->ctx); 687done: 688 return (err); 689} 690 691int 692libusb_set_interface_alt_setting(struct libusb20_device *pdev, 693 int interface_number, int alternate_setting) 694{ 695 libusb_device *dev; 696 int err = 0; 697 698 dev = libusb_get_device(pdev); 699 if (dev == NULL) 700 return (LIBUSB_ERROR_INVALID_PARAM); 701 702 if (interface_number < 0 || interface_number > 31) 703 return (LIBUSB_ERROR_INVALID_PARAM); 704 705 CTX_LOCK(dev->ctx); 706 if (!(dev->claimed_interfaces & (1 << interface_number))) 707 err = LIBUSB_ERROR_NOT_FOUND; 708 CTX_UNLOCK(dev->ctx); 709 710 if (err) 711 return (err); 712 713 libusb10_cancel_all_transfer(dev); 714 715 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll); 716 717 err = libusb20_dev_set_alt_index(pdev, 718 interface_number, alternate_setting); 719 720 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, 721 pdev, libusb20_dev_get_fd(pdev), 722 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); 723 724 return (err ? LIBUSB_ERROR_OTHER : 0); 725} 726 727static struct libusb20_transfer * 728libusb10_get_transfer(struct libusb20_device *pdev, 729 uint8_t endpoint, uint8_t xfer_index) 730{ 731 xfer_index &= 1; /* double buffering */ 732 733 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4; 734 735 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) { 736 /* this is an IN endpoint */ 737 xfer_index |= 2; 738 } 739 return (libusb20_tr_get_pointer(pdev, xfer_index)); 740} 741 742int 743libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint) 744{ 745 struct libusb20_transfer *xfer; 746 struct libusb_device *dev; 747 int err; 748 749 xfer = libusb10_get_transfer(pdev, endpoint, 0); 750 if (xfer == NULL) 751 return (LIBUSB_ERROR_INVALID_PARAM); 752 753 dev = libusb_get_device(pdev); 754 if (dev == NULL) 755 return (LIBUSB_ERROR_INVALID_PARAM); 756 757 CTX_LOCK(dev->ctx); 758 err = libusb20_tr_open(xfer, 0, 1, endpoint); 759 CTX_UNLOCK(dev->ctx); 760 761 if (err != 0 && err != LIBUSB20_ERROR_BUSY) 762 return (LIBUSB_ERROR_OTHER); 763 764 libusb20_tr_clear_stall_sync(xfer); 765 766 /* check if we opened the transfer */ 767 if (err == 0) { 768 CTX_LOCK(dev->ctx); 769 libusb20_tr_close(xfer); 770 CTX_UNLOCK(dev->ctx); 771 } 772 return (0); /* success */ 773} 774 775int 776libusb_reset_device(struct libusb20_device *pdev) 777{ 778 libusb_device *dev; 779 int err; 780 781 dev = libusb_get_device(pdev); 782 if (dev == NULL) 783 return (LIBUSB_ERROR_INVALID_PARAM); 784 785 libusb10_cancel_all_transfer(dev); 786 787 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll); 788 789 err = libusb20_dev_reset(pdev); 790 791 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, 792 pdev, libusb20_dev_get_fd(pdev), 793 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); 794 795 return (err ? LIBUSB_ERROR_OTHER : 0); 796} 797 798int 799libusb_check_connected(struct libusb20_device *pdev) 800{ 801 libusb_device *dev; 802 int err; 803 804 dev = libusb_get_device(pdev); 805 if (dev == NULL) 806 return (LIBUSB_ERROR_INVALID_PARAM); 807 808 err = libusb20_dev_check_connected(pdev); 809 810 return (err ? LIBUSB_ERROR_NO_DEVICE : 0); 811} 812 813int 814libusb_kernel_driver_active(struct libusb20_device *pdev, int interface) 815{ 816 if (pdev == NULL) 817 return (LIBUSB_ERROR_INVALID_PARAM); 818 819 if (libusb20_dev_kernel_driver_active(pdev, interface)) 820 return (0); /* no kernel driver is active */ 821 else 822 return (1); /* kernel driver is active */ 823} 824 825int 826libusb_get_driver_np(struct libusb20_device *pdev, int interface, 827 char *name, int namelen) 828{ 829 return (libusb_get_driver(pdev, interface, name, namelen)); 830} 831 832int 833libusb_get_driver(struct libusb20_device *pdev, int interface, 834 char *name, int namelen) 835{ 836 char *ptr; 837 int err; 838 839 if (pdev == NULL) 840 return (LIBUSB_ERROR_INVALID_PARAM); 841 if (namelen < 1) 842 return (LIBUSB_ERROR_INVALID_PARAM); 843 if (namelen > 255) 844 namelen = 255; 845 846 err = libusb20_dev_get_iface_desc( 847 pdev, interface, name, namelen); 848 849 if (err != 0) 850 return (LIBUSB_ERROR_OTHER); 851 852 /* we only want the driver name */ 853 ptr = strstr(name, ":"); 854 if (ptr != NULL) 855 *ptr = 0; 856 857 return (0); 858} 859 860int 861libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface) 862{ 863 return (libusb_detach_kernel_driver(pdev, interface)); 864} 865 866int 867libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface) 868{ 869 int err; 870 871 if (pdev == NULL) 872 return (LIBUSB_ERROR_INVALID_PARAM); 873 874 err = libusb20_dev_detach_kernel_driver( 875 pdev, interface); 876 877 return (err ? LIBUSB_ERROR_OTHER : 0); 878} 879 880int 881libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface) 882{ 883 if (pdev == NULL) 884 return (LIBUSB_ERROR_INVALID_PARAM); 885 /* stub - currently not supported by libusb20 */ 886 return (0); 887} 888 889int 890libusb_set_auto_detach_kernel_driver(libusb_device_handle *dev, int enable) 891{ 892 dev->auto_detach = (enable ? 1 : 0);
|
893} 894 895/* Asynchronous device I/O */ 896 897struct libusb_transfer * 898libusb_alloc_transfer(int iso_packets) 899{ 900 struct libusb_transfer *uxfer; 901 struct libusb_super_transfer *sxfer; 902 int len; 903 904 len = sizeof(struct libusb_transfer) + 905 sizeof(struct libusb_super_transfer) + 906 (iso_packets * sizeof(libusb_iso_packet_descriptor)); 907 908 sxfer = malloc(len); 909 if (sxfer == NULL) 910 return (NULL); 911 912 memset(sxfer, 0, len); 913 914 uxfer = (struct libusb_transfer *)( 915 ((uint8_t *)sxfer) + sizeof(*sxfer)); 916 917 /* set default value */ 918 uxfer->num_iso_packets = iso_packets; 919 920 return (uxfer); 921} 922 923void 924libusb_free_transfer(struct libusb_transfer *uxfer) 925{ 926 struct libusb_super_transfer *sxfer; 927 928 if (uxfer == NULL) 929 return; /* be NULL safe */ 930 931 /* check if we should free the transfer buffer */ 932 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER) 933 free(uxfer->buffer); 934 935 sxfer = (struct libusb_super_transfer *)( 936 (uint8_t *)uxfer - sizeof(*sxfer)); 937 938 free(sxfer); 939} 940 941static uint32_t 942libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer) 943{ 944 uint32_t ret; 945 946 switch (xfer->type) { 947 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: 948 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */ 949 break; 950 case LIBUSB_TRANSFER_TYPE_CONTROL: 951 ret = 2; 952 break; 953 default: 954 ret = 1; 955 break; 956 } 957 return (ret); 958} 959 960static int 961libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer) 962{ 963 int ret; 964 int usb_speed; 965 966 usb_speed = libusb20_dev_get_speed(pdev); 967 968 switch (xfer->type) { 969 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: 970 ret = 0; /* kernel will auto-select */ 971 break; 972 case LIBUSB_TRANSFER_TYPE_CONTROL: 973 ret = 1024; 974 break; 975 default: 976 switch (usb_speed) { 977 case LIBUSB20_SPEED_LOW: 978 ret = 256; 979 break; 980 case LIBUSB20_SPEED_FULL: 981 ret = 4096; 982 break; 983 case LIBUSB20_SPEED_SUPER: 984 ret = 65536; 985 break; 986 default: 987 ret = 16384; 988 break; 989 } 990 break; 991 } 992 return (ret); 993} 994 995static int 996libusb10_convert_error(uint8_t status) 997{ 998 ; /* indent fix */ 999 1000 switch (status) { 1001 case LIBUSB20_TRANSFER_START: 1002 case LIBUSB20_TRANSFER_COMPLETED: 1003 return (LIBUSB_TRANSFER_COMPLETED); 1004 case LIBUSB20_TRANSFER_OVERFLOW: 1005 return (LIBUSB_TRANSFER_OVERFLOW); 1006 case LIBUSB20_TRANSFER_NO_DEVICE: 1007 return (LIBUSB_TRANSFER_NO_DEVICE); 1008 case LIBUSB20_TRANSFER_STALL: 1009 return (LIBUSB_TRANSFER_STALL); 1010 case LIBUSB20_TRANSFER_CANCELLED: 1011 return (LIBUSB_TRANSFER_CANCELLED); 1012 case LIBUSB20_TRANSFER_TIMED_OUT: 1013 return (LIBUSB_TRANSFER_TIMED_OUT); 1014 default: 1015 return (LIBUSB_TRANSFER_ERROR); 1016 } 1017} 1018 1019/* This function must be called locked */ 1020 1021static void 1022libusb10_complete_transfer(struct libusb20_transfer *pxfer, 1023 struct libusb_super_transfer *sxfer, int status) 1024{ 1025 struct libusb_transfer *uxfer; 1026 struct libusb_device *dev; 1027 1028 uxfer = (struct libusb_transfer *)( 1029 ((uint8_t *)sxfer) + sizeof(*sxfer)); 1030 1031 if (pxfer != NULL) 1032 libusb20_tr_set_priv_sc1(pxfer, NULL); 1033 1034 /* set transfer status */ 1035 uxfer->status = status; 1036 1037 /* update super transfer state */ 1038 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE; 1039 1040 dev = libusb_get_device(uxfer->dev_handle); 1041 1042 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry); 1043} 1044 1045/* This function must be called locked */ 1046 1047static void 1048libusb10_isoc_proxy(struct libusb20_transfer *pxfer) 1049{ 1050 struct libusb_super_transfer *sxfer; 1051 struct libusb_transfer *uxfer; 1052 uint32_t actlen; 1053 uint16_t iso_packets; 1054 uint16_t i; 1055 uint8_t status; 1056 1057 status = libusb20_tr_get_status(pxfer); 1058 sxfer = libusb20_tr_get_priv_sc1(pxfer); 1059 actlen = libusb20_tr_get_actual_length(pxfer); 1060 iso_packets = libusb20_tr_get_max_frames(pxfer); 1061 1062 if (sxfer == NULL) 1063 return; /* cancelled - nothing to do */ 1064 1065 uxfer = (struct libusb_transfer *)( 1066 ((uint8_t *)sxfer) + sizeof(*sxfer)); 1067 1068 if (iso_packets > uxfer->num_iso_packets) 1069 iso_packets = uxfer->num_iso_packets; 1070 1071 if (iso_packets == 0) 1072 return; /* nothing to do */ 1073 1074 /* make sure that the number of ISOCHRONOUS packets is valid */ 1075 uxfer->num_iso_packets = iso_packets; 1076 1077 switch (status) { 1078 case LIBUSB20_TRANSFER_COMPLETED: 1079 /* update actual length */ 1080 uxfer->actual_length = actlen; 1081 for (i = 0; i != iso_packets; i++) { 1082 uxfer->iso_packet_desc[i].actual_length = 1083 libusb20_tr_get_length(pxfer, i); 1084 } 1085 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); 1086 break; 1087 case LIBUSB20_TRANSFER_START: 1088 /* setup length(s) */ 1089 actlen = 0; 1090 for (i = 0; i != iso_packets; i++) { 1091 libusb20_tr_setup_isoc(pxfer, 1092 &uxfer->buffer[actlen], 1093 uxfer->iso_packet_desc[i].length, i); 1094 actlen += uxfer->iso_packet_desc[i].length; 1095 } 1096 1097 /* no remainder */ 1098 sxfer->rem_len = 0; 1099 1100 libusb20_tr_set_total_frames(pxfer, iso_packets); 1101 libusb20_tr_submit(pxfer); 1102 1103 /* fork another USB transfer, if any */ 1104 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint); 1105 break; 1106 default: 1107 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status)); 1108 break; 1109 } 1110} 1111 1112/* This function must be called locked */ 1113 1114static void 1115libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer) 1116{ 1117 struct libusb_super_transfer *sxfer; 1118 struct libusb_transfer *uxfer; 1119 uint32_t max_bulk; 1120 uint32_t actlen; 1121 uint8_t status; 1122 uint8_t flags; 1123 1124 status = libusb20_tr_get_status(pxfer); 1125 sxfer = libusb20_tr_get_priv_sc1(pxfer); 1126 max_bulk = libusb20_tr_get_max_total_length(pxfer); 1127 actlen = libusb20_tr_get_actual_length(pxfer); 1128 1129 if (sxfer == NULL) 1130 return; /* cancelled - nothing to do */ 1131 1132 uxfer = (struct libusb_transfer *)( 1133 ((uint8_t *)sxfer) + sizeof(*sxfer)); 1134 1135 flags = uxfer->flags; 1136 1137 switch (status) { 1138 case LIBUSB20_TRANSFER_COMPLETED: 1139 1140 uxfer->actual_length += actlen; 1141 1142 /* check for short packet */ 1143 if (sxfer->last_len != actlen) { 1144 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) { 1145 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR); 1146 } else { 1147 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); 1148 } 1149 break; 1150 } 1151 /* check for end of data */ 1152 if (sxfer->rem_len == 0) { 1153 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); 1154 break; 1155 } 1156 /* FALLTHROUGH */ 1157 1158 case LIBUSB20_TRANSFER_START: 1159 if (max_bulk > sxfer->rem_len) { 1160 max_bulk = sxfer->rem_len; 1161 } 1162 /* setup new BULK or INTERRUPT transaction */ 1163 libusb20_tr_setup_bulk(pxfer, 1164 sxfer->curr_data, max_bulk, uxfer->timeout); 1165 1166 /* update counters */ 1167 sxfer->last_len = max_bulk; 1168 sxfer->curr_data += max_bulk; 1169 sxfer->rem_len -= max_bulk; 1170 1171 libusb20_tr_submit(pxfer); 1172 1173 /* check if we can fork another USB transfer */ 1174 if (sxfer->rem_len == 0) 1175 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint); 1176 break; 1177 1178 default: 1179 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status)); 1180 break; 1181 } 1182} 1183 1184/* This function must be called locked */ 1185 1186static void 1187libusb10_ctrl_proxy(struct libusb20_transfer *pxfer) 1188{ 1189 struct libusb_super_transfer *sxfer; 1190 struct libusb_transfer *uxfer; 1191 uint32_t max_bulk; 1192 uint32_t actlen; 1193 uint8_t status; 1194 uint8_t flags; 1195 1196 status = libusb20_tr_get_status(pxfer); 1197 sxfer = libusb20_tr_get_priv_sc1(pxfer); 1198 max_bulk = libusb20_tr_get_max_total_length(pxfer); 1199 actlen = libusb20_tr_get_actual_length(pxfer); 1200 1201 if (sxfer == NULL) 1202 return; /* cancelled - nothing to do */ 1203 1204 uxfer = (struct libusb_transfer *)( 1205 ((uint8_t *)sxfer) + sizeof(*sxfer)); 1206 1207 flags = uxfer->flags; 1208 1209 switch (status) { 1210 case LIBUSB20_TRANSFER_COMPLETED: 1211 1212 uxfer->actual_length += actlen; 1213 1214 /* subtract length of SETUP packet, if any */ 1215 actlen -= libusb20_tr_get_length(pxfer, 0); 1216 1217 /* check for short packet */ 1218 if (sxfer->last_len != actlen) { 1219 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) { 1220 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR); 1221 } else { 1222 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); 1223 } 1224 break; 1225 } 1226 /* check for end of data */ 1227 if (sxfer->rem_len == 0) { 1228 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); 1229 break; 1230 } 1231 /* FALLTHROUGH */ 1232 1233 case LIBUSB20_TRANSFER_START: 1234 if (max_bulk > sxfer->rem_len) { 1235 max_bulk = sxfer->rem_len; 1236 } 1237 /* setup new CONTROL transaction */ 1238 if (status == LIBUSB20_TRANSFER_COMPLETED) { 1239 /* next fragment - don't send SETUP packet */ 1240 libusb20_tr_set_length(pxfer, 0, 0); 1241 } else { 1242 /* first fragment - send SETUP packet */ 1243 libusb20_tr_set_length(pxfer, 8, 0); 1244 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0); 1245 } 1246 1247 if (max_bulk != 0) { 1248 libusb20_tr_set_length(pxfer, max_bulk, 1); 1249 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1); 1250 libusb20_tr_set_total_frames(pxfer, 2); 1251 } else { 1252 libusb20_tr_set_total_frames(pxfer, 1); 1253 } 1254 1255 /* update counters */ 1256 sxfer->last_len = max_bulk; 1257 sxfer->curr_data += max_bulk; 1258 sxfer->rem_len -= max_bulk; 1259 1260 libusb20_tr_submit(pxfer); 1261 1262 /* check if we can fork another USB transfer */ 1263 if (sxfer->rem_len == 0) 1264 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint); 1265 break; 1266 1267 default: 1268 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status)); 1269 break; 1270 } 1271} 1272 1273/* The following function must be called locked */ 1274 1275static void 1276libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint) 1277{ 1278 struct libusb20_transfer *pxfer0; 1279 struct libusb20_transfer *pxfer1; 1280 struct libusb_super_transfer *sxfer; 1281 struct libusb_transfer *uxfer; 1282 struct libusb_device *dev; 1283 int err; 1284 int buffsize; 1285 int maxframe; 1286 int temp; 1287 uint8_t dummy; 1288 1289 dev = libusb_get_device(pdev); 1290 1291 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0); 1292 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1); 1293 1294 if (pxfer0 == NULL || pxfer1 == NULL) 1295 return; /* shouldn't happen */ 1296 1297 temp = 0; 1298 if (libusb20_tr_pending(pxfer0)) 1299 temp |= 1; 1300 if (libusb20_tr_pending(pxfer1)) 1301 temp |= 2; 1302 1303 switch (temp) { 1304 case 3: 1305 /* wait till one of the transfers complete */ 1306 return; 1307 case 2: 1308 sxfer = libusb20_tr_get_priv_sc1(pxfer1); 1309 if (sxfer == NULL) 1310 return; /* cancelling */ 1311 if (sxfer->rem_len) 1312 return; /* cannot queue another one */ 1313 /* swap transfers */ 1314 pxfer1 = pxfer0; 1315 break; 1316 case 1: 1317 sxfer = libusb20_tr_get_priv_sc1(pxfer0); 1318 if (sxfer == NULL) 1319 return; /* cancelling */ 1320 if (sxfer->rem_len) 1321 return; /* cannot queue another one */ 1322 /* swap transfers */ 1323 pxfer0 = pxfer1; 1324 break; 1325 default: 1326 break; 1327 } 1328 1329 /* find next transfer on same endpoint */ 1330 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) { 1331 1332 uxfer = (struct libusb_transfer *)( 1333 ((uint8_t *)sxfer) + sizeof(*sxfer)); 1334 1335 if (uxfer->endpoint == endpoint) { 1336 TAILQ_REMOVE(&dev->tr_head, sxfer, entry); 1337 sxfer->entry.tqe_prev = NULL; 1338 goto found; 1339 } 1340 } 1341 return; /* success */ 1342 1343found: 1344 1345 libusb20_tr_set_priv_sc0(pxfer0, pdev); 1346 libusb20_tr_set_priv_sc1(pxfer0, sxfer); 1347 1348 /* reset super transfer state */ 1349 sxfer->rem_len = uxfer->length; 1350 sxfer->curr_data = uxfer->buffer; 1351 uxfer->actual_length = 0; 1352 1353 switch (uxfer->type) { 1354 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: 1355 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy); 1356 break; 1357 case LIBUSB_TRANSFER_TYPE_BULK: 1358 case LIBUSB_TRANSFER_TYPE_INTERRUPT: 1359 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy); 1360 break; 1361 case LIBUSB_TRANSFER_TYPE_CONTROL: 1362 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy); 1363 if (sxfer->rem_len < 8) 1364 goto failure; 1365 1366 /* remove SETUP packet from data */ 1367 sxfer->rem_len -= 8; 1368 sxfer->curr_data += 8; 1369 break; 1370 default: 1371 goto failure; 1372 } 1373 1374 buffsize = libusb10_get_buffsize(pdev, uxfer); 1375 maxframe = libusb10_get_maxframe(pdev, uxfer); 1376 1377 /* make sure the transfer is opened */ 1378 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint); 1379 if (err && (err != LIBUSB20_ERROR_BUSY)) { 1380 goto failure; 1381 } 1382 libusb20_tr_start(pxfer0); 1383 return; 1384 1385failure: 1386 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR); 1387 1388 /* make sure our event loop spins the done handler */ 1389 dummy = 0; 1390 err = write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy)); 1391} 1392 1393/* The following function must be called unlocked */ 1394 1395int 1396libusb_submit_transfer(struct libusb_transfer *uxfer) 1397{ 1398 struct libusb20_transfer *pxfer0; 1399 struct libusb20_transfer *pxfer1; 1400 struct libusb_super_transfer *sxfer; 1401 struct libusb_device *dev; 1402 uint8_t endpoint; 1403 int err; 1404 1405 if (uxfer == NULL) 1406 return (LIBUSB_ERROR_INVALID_PARAM); 1407 1408 if (uxfer->dev_handle == NULL) 1409 return (LIBUSB_ERROR_INVALID_PARAM); 1410 1411 endpoint = uxfer->endpoint; 1412 1413 dev = libusb_get_device(uxfer->dev_handle); 1414 1415 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter"); 1416 1417 sxfer = (struct libusb_super_transfer *)( 1418 (uint8_t *)uxfer - sizeof(*sxfer)); 1419 1420 CTX_LOCK(dev->ctx); 1421 1422 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0); 1423 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1); 1424 1425 if (pxfer0 == NULL || pxfer1 == NULL) { 1426 err = LIBUSB_ERROR_OTHER; 1427 } else if ((sxfer->entry.tqe_prev != NULL) || 1428 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) || 1429 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) { 1430 err = LIBUSB_ERROR_BUSY; 1431 } else { 1432 1433 /* set pending state */ 1434 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND; 1435 1436 /* insert transfer into transfer head list */ 1437 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry); 1438 1439 /* start work transfers */ 1440 libusb10_submit_transfer_sub( 1441 uxfer->dev_handle, endpoint); 1442 1443 err = 0; /* success */ 1444 } 1445 1446 CTX_UNLOCK(dev->ctx); 1447 1448 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err); 1449 1450 return (err); 1451} 1452 1453/* Asynchronous transfer cancel */ 1454 1455int 1456libusb_cancel_transfer(struct libusb_transfer *uxfer) 1457{ 1458 struct libusb20_transfer *pxfer0; 1459 struct libusb20_transfer *pxfer1; 1460 struct libusb_super_transfer *sxfer; 1461 struct libusb_device *dev; 1462 uint8_t endpoint; 1463 int retval; 1464 1465 if (uxfer == NULL) 1466 return (LIBUSB_ERROR_INVALID_PARAM); 1467 1468 /* check if not initialised */ 1469 if (uxfer->dev_handle == NULL) 1470 return (LIBUSB_ERROR_NOT_FOUND); 1471 1472 endpoint = uxfer->endpoint; 1473 1474 dev = libusb_get_device(uxfer->dev_handle); 1475 1476 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter"); 1477 1478 sxfer = (struct libusb_super_transfer *)( 1479 (uint8_t *)uxfer - sizeof(*sxfer)); 1480 1481 retval = 0; 1482 1483 CTX_LOCK(dev->ctx); 1484 1485 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0); 1486 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1); 1487 1488 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) { 1489 /* only update the transfer status */ 1490 uxfer->status = LIBUSB_TRANSFER_CANCELLED; 1491 retval = LIBUSB_ERROR_NOT_FOUND; 1492 } else if (sxfer->entry.tqe_prev != NULL) { 1493 /* we are lucky - transfer is on a queue */ 1494 TAILQ_REMOVE(&dev->tr_head, sxfer, entry); 1495 sxfer->entry.tqe_prev = NULL; 1496 libusb10_complete_transfer(NULL, 1497 sxfer, LIBUSB_TRANSFER_CANCELLED); 1498 } else if (pxfer0 == NULL || pxfer1 == NULL) { 1499 /* not started */ 1500 retval = LIBUSB_ERROR_NOT_FOUND; 1501 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) { 1502 libusb10_complete_transfer(pxfer0, 1503 sxfer, LIBUSB_TRANSFER_CANCELLED); 1504 libusb20_tr_stop(pxfer0); 1505 /* make sure the queue doesn't stall */ 1506 libusb10_submit_transfer_sub( 1507 uxfer->dev_handle, endpoint); 1508 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) { 1509 libusb10_complete_transfer(pxfer1, 1510 sxfer, LIBUSB_TRANSFER_CANCELLED); 1511 libusb20_tr_stop(pxfer1); 1512 /* make sure the queue doesn't stall */ 1513 libusb10_submit_transfer_sub( 1514 uxfer->dev_handle, endpoint); 1515 } else { 1516 /* not started */ 1517 retval = LIBUSB_ERROR_NOT_FOUND; 1518 } 1519 1520 CTX_UNLOCK(dev->ctx); 1521 1522 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave"); 1523 1524 return (retval); 1525} 1526 1527UNEXPORTED void 1528libusb10_cancel_all_transfer(libusb_device *dev) 1529{ 1530 struct libusb20_device *pdev = dev->os_priv; 1531 unsigned x; 1532 1533 for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) { 1534 struct libusb20_transfer *xfer; 1535 1536 xfer = libusb20_tr_get_pointer(pdev, x); 1537 if (xfer == NULL) 1538 continue; 1539 libusb20_tr_close(xfer); 1540 } 1541} 1542 1543uint16_t 1544libusb_cpu_to_le16(uint16_t x) 1545{ 1546 return (htole16(x)); 1547} 1548 1549uint16_t 1550libusb_le16_to_cpu(uint16_t x) 1551{ 1552 return (le16toh(x)); 1553} 1554 1555const char * 1556libusb_strerror(int code) 1557{ 1558 switch (code) { 1559 case LIBUSB_SUCCESS: 1560 return ("Success"); 1561 case LIBUSB_ERROR_IO: 1562 return ("I/O error"); 1563 case LIBUSB_ERROR_INVALID_PARAM: 1564 return ("Invalid parameter"); 1565 case LIBUSB_ERROR_ACCESS: 1566 return ("Permissions error"); 1567 case LIBUSB_ERROR_NO_DEVICE: 1568 return ("No device"); 1569 case LIBUSB_ERROR_NOT_FOUND: 1570 return ("Not found"); 1571 case LIBUSB_ERROR_BUSY: 1572 return ("Device busy"); 1573 case LIBUSB_ERROR_TIMEOUT: 1574 return ("Timeout"); 1575 case LIBUSB_ERROR_OVERFLOW: 1576 return ("Overflow"); 1577 case LIBUSB_ERROR_PIPE: 1578 return ("Pipe error"); 1579 case LIBUSB_ERROR_INTERRUPTED: 1580 return ("Interrupted"); 1581 case LIBUSB_ERROR_NO_MEM: 1582 return ("Out of memory"); 1583 case LIBUSB_ERROR_NOT_SUPPORTED: 1584 return ("Not supported"); 1585 case LIBUSB_ERROR_OTHER: 1586 return ("Other error"); 1587 default: 1588 return ("Unknown error"); 1589 } 1590} 1591 1592const char * 1593libusb_error_name(int code) 1594{ 1595 switch (code) { 1596 case LIBUSB_SUCCESS: 1597 return ("LIBUSB_SUCCESS"); 1598 case LIBUSB_ERROR_IO: 1599 return ("LIBUSB_ERROR_IO"); 1600 case LIBUSB_ERROR_INVALID_PARAM: 1601 return ("LIBUSB_ERROR_INVALID_PARAM"); 1602 case LIBUSB_ERROR_ACCESS: 1603 return ("LIBUSB_ERROR_ACCESS"); 1604 case LIBUSB_ERROR_NO_DEVICE: 1605 return ("LIBUSB_ERROR_NO_DEVICE"); 1606 case LIBUSB_ERROR_NOT_FOUND: 1607 return ("LIBUSB_ERROR_NOT_FOUND"); 1608 case LIBUSB_ERROR_BUSY: 1609 return ("LIBUSB_ERROR_BUSY"); 1610 case LIBUSB_ERROR_TIMEOUT: 1611 return ("LIBUSB_ERROR_TIMEOUT"); 1612 case LIBUSB_ERROR_OVERFLOW: 1613 return ("LIBUSB_ERROR_OVERFLOW"); 1614 case LIBUSB_ERROR_PIPE: 1615 return ("LIBUSB_ERROR_PIPE"); 1616 case LIBUSB_ERROR_INTERRUPTED: 1617 return ("LIBUSB_ERROR_INTERRUPTED"); 1618 case LIBUSB_ERROR_NO_MEM: 1619 return ("LIBUSB_ERROR_NO_MEM"); 1620 case LIBUSB_ERROR_NOT_SUPPORTED: 1621 return ("LIBUSB_ERROR_NOT_SUPPORTED"); 1622 case LIBUSB_ERROR_OTHER: 1623 return ("LIBUSB_ERROR_OTHER"); 1624 default: 1625 return ("LIBUSB_ERROR_UNKNOWN"); 1626 } 1627}
| 894} 895 896/* Asynchronous device I/O */ 897 898struct libusb_transfer * 899libusb_alloc_transfer(int iso_packets) 900{ 901 struct libusb_transfer *uxfer; 902 struct libusb_super_transfer *sxfer; 903 int len; 904 905 len = sizeof(struct libusb_transfer) + 906 sizeof(struct libusb_super_transfer) + 907 (iso_packets * sizeof(libusb_iso_packet_descriptor)); 908 909 sxfer = malloc(len); 910 if (sxfer == NULL) 911 return (NULL); 912 913 memset(sxfer, 0, len); 914 915 uxfer = (struct libusb_transfer *)( 916 ((uint8_t *)sxfer) + sizeof(*sxfer)); 917 918 /* set default value */ 919 uxfer->num_iso_packets = iso_packets; 920 921 return (uxfer); 922} 923 924void 925libusb_free_transfer(struct libusb_transfer *uxfer) 926{ 927 struct libusb_super_transfer *sxfer; 928 929 if (uxfer == NULL) 930 return; /* be NULL safe */ 931 932 /* check if we should free the transfer buffer */ 933 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER) 934 free(uxfer->buffer); 935 936 sxfer = (struct libusb_super_transfer *)( 937 (uint8_t *)uxfer - sizeof(*sxfer)); 938 939 free(sxfer); 940} 941 942static uint32_t 943libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer) 944{ 945 uint32_t ret; 946 947 switch (xfer->type) { 948 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: 949 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */ 950 break; 951 case LIBUSB_TRANSFER_TYPE_CONTROL: 952 ret = 2; 953 break; 954 default: 955 ret = 1; 956 break; 957 } 958 return (ret); 959} 960 961static int 962libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer) 963{ 964 int ret; 965 int usb_speed; 966 967 usb_speed = libusb20_dev_get_speed(pdev); 968 969 switch (xfer->type) { 970 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: 971 ret = 0; /* kernel will auto-select */ 972 break; 973 case LIBUSB_TRANSFER_TYPE_CONTROL: 974 ret = 1024; 975 break; 976 default: 977 switch (usb_speed) { 978 case LIBUSB20_SPEED_LOW: 979 ret = 256; 980 break; 981 case LIBUSB20_SPEED_FULL: 982 ret = 4096; 983 break; 984 case LIBUSB20_SPEED_SUPER: 985 ret = 65536; 986 break; 987 default: 988 ret = 16384; 989 break; 990 } 991 break; 992 } 993 return (ret); 994} 995 996static int 997libusb10_convert_error(uint8_t status) 998{ 999 ; /* indent fix */ 1000 1001 switch (status) { 1002 case LIBUSB20_TRANSFER_START: 1003 case LIBUSB20_TRANSFER_COMPLETED: 1004 return (LIBUSB_TRANSFER_COMPLETED); 1005 case LIBUSB20_TRANSFER_OVERFLOW: 1006 return (LIBUSB_TRANSFER_OVERFLOW); 1007 case LIBUSB20_TRANSFER_NO_DEVICE: 1008 return (LIBUSB_TRANSFER_NO_DEVICE); 1009 case LIBUSB20_TRANSFER_STALL: 1010 return (LIBUSB_TRANSFER_STALL); 1011 case LIBUSB20_TRANSFER_CANCELLED: 1012 return (LIBUSB_TRANSFER_CANCELLED); 1013 case LIBUSB20_TRANSFER_TIMED_OUT: 1014 return (LIBUSB_TRANSFER_TIMED_OUT); 1015 default: 1016 return (LIBUSB_TRANSFER_ERROR); 1017 } 1018} 1019 1020/* This function must be called locked */ 1021 1022static void 1023libusb10_complete_transfer(struct libusb20_transfer *pxfer, 1024 struct libusb_super_transfer *sxfer, int status) 1025{ 1026 struct libusb_transfer *uxfer; 1027 struct libusb_device *dev; 1028 1029 uxfer = (struct libusb_transfer *)( 1030 ((uint8_t *)sxfer) + sizeof(*sxfer)); 1031 1032 if (pxfer != NULL) 1033 libusb20_tr_set_priv_sc1(pxfer, NULL); 1034 1035 /* set transfer status */ 1036 uxfer->status = status; 1037 1038 /* update super transfer state */ 1039 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE; 1040 1041 dev = libusb_get_device(uxfer->dev_handle); 1042 1043 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry); 1044} 1045 1046/* This function must be called locked */ 1047 1048static void 1049libusb10_isoc_proxy(struct libusb20_transfer *pxfer) 1050{ 1051 struct libusb_super_transfer *sxfer; 1052 struct libusb_transfer *uxfer; 1053 uint32_t actlen; 1054 uint16_t iso_packets; 1055 uint16_t i; 1056 uint8_t status; 1057 1058 status = libusb20_tr_get_status(pxfer); 1059 sxfer = libusb20_tr_get_priv_sc1(pxfer); 1060 actlen = libusb20_tr_get_actual_length(pxfer); 1061 iso_packets = libusb20_tr_get_max_frames(pxfer); 1062 1063 if (sxfer == NULL) 1064 return; /* cancelled - nothing to do */ 1065 1066 uxfer = (struct libusb_transfer *)( 1067 ((uint8_t *)sxfer) + sizeof(*sxfer)); 1068 1069 if (iso_packets > uxfer->num_iso_packets) 1070 iso_packets = uxfer->num_iso_packets; 1071 1072 if (iso_packets == 0) 1073 return; /* nothing to do */ 1074 1075 /* make sure that the number of ISOCHRONOUS packets is valid */ 1076 uxfer->num_iso_packets = iso_packets; 1077 1078 switch (status) { 1079 case LIBUSB20_TRANSFER_COMPLETED: 1080 /* update actual length */ 1081 uxfer->actual_length = actlen; 1082 for (i = 0; i != iso_packets; i++) { 1083 uxfer->iso_packet_desc[i].actual_length = 1084 libusb20_tr_get_length(pxfer, i); 1085 } 1086 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); 1087 break; 1088 case LIBUSB20_TRANSFER_START: 1089 /* setup length(s) */ 1090 actlen = 0; 1091 for (i = 0; i != iso_packets; i++) { 1092 libusb20_tr_setup_isoc(pxfer, 1093 &uxfer->buffer[actlen], 1094 uxfer->iso_packet_desc[i].length, i); 1095 actlen += uxfer->iso_packet_desc[i].length; 1096 } 1097 1098 /* no remainder */ 1099 sxfer->rem_len = 0; 1100 1101 libusb20_tr_set_total_frames(pxfer, iso_packets); 1102 libusb20_tr_submit(pxfer); 1103 1104 /* fork another USB transfer, if any */ 1105 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint); 1106 break; 1107 default: 1108 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status)); 1109 break; 1110 } 1111} 1112 1113/* This function must be called locked */ 1114 1115static void 1116libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer) 1117{ 1118 struct libusb_super_transfer *sxfer; 1119 struct libusb_transfer *uxfer; 1120 uint32_t max_bulk; 1121 uint32_t actlen; 1122 uint8_t status; 1123 uint8_t flags; 1124 1125 status = libusb20_tr_get_status(pxfer); 1126 sxfer = libusb20_tr_get_priv_sc1(pxfer); 1127 max_bulk = libusb20_tr_get_max_total_length(pxfer); 1128 actlen = libusb20_tr_get_actual_length(pxfer); 1129 1130 if (sxfer == NULL) 1131 return; /* cancelled - nothing to do */ 1132 1133 uxfer = (struct libusb_transfer *)( 1134 ((uint8_t *)sxfer) + sizeof(*sxfer)); 1135 1136 flags = uxfer->flags; 1137 1138 switch (status) { 1139 case LIBUSB20_TRANSFER_COMPLETED: 1140 1141 uxfer->actual_length += actlen; 1142 1143 /* check for short packet */ 1144 if (sxfer->last_len != actlen) { 1145 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) { 1146 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR); 1147 } else { 1148 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); 1149 } 1150 break; 1151 } 1152 /* check for end of data */ 1153 if (sxfer->rem_len == 0) { 1154 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); 1155 break; 1156 } 1157 /* FALLTHROUGH */ 1158 1159 case LIBUSB20_TRANSFER_START: 1160 if (max_bulk > sxfer->rem_len) { 1161 max_bulk = sxfer->rem_len; 1162 } 1163 /* setup new BULK or INTERRUPT transaction */ 1164 libusb20_tr_setup_bulk(pxfer, 1165 sxfer->curr_data, max_bulk, uxfer->timeout); 1166 1167 /* update counters */ 1168 sxfer->last_len = max_bulk; 1169 sxfer->curr_data += max_bulk; 1170 sxfer->rem_len -= max_bulk; 1171 1172 libusb20_tr_submit(pxfer); 1173 1174 /* check if we can fork another USB transfer */ 1175 if (sxfer->rem_len == 0) 1176 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint); 1177 break; 1178 1179 default: 1180 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status)); 1181 break; 1182 } 1183} 1184 1185/* This function must be called locked */ 1186 1187static void 1188libusb10_ctrl_proxy(struct libusb20_transfer *pxfer) 1189{ 1190 struct libusb_super_transfer *sxfer; 1191 struct libusb_transfer *uxfer; 1192 uint32_t max_bulk; 1193 uint32_t actlen; 1194 uint8_t status; 1195 uint8_t flags; 1196 1197 status = libusb20_tr_get_status(pxfer); 1198 sxfer = libusb20_tr_get_priv_sc1(pxfer); 1199 max_bulk = libusb20_tr_get_max_total_length(pxfer); 1200 actlen = libusb20_tr_get_actual_length(pxfer); 1201 1202 if (sxfer == NULL) 1203 return; /* cancelled - nothing to do */ 1204 1205 uxfer = (struct libusb_transfer *)( 1206 ((uint8_t *)sxfer) + sizeof(*sxfer)); 1207 1208 flags = uxfer->flags; 1209 1210 switch (status) { 1211 case LIBUSB20_TRANSFER_COMPLETED: 1212 1213 uxfer->actual_length += actlen; 1214 1215 /* subtract length of SETUP packet, if any */ 1216 actlen -= libusb20_tr_get_length(pxfer, 0); 1217 1218 /* check for short packet */ 1219 if (sxfer->last_len != actlen) { 1220 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) { 1221 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR); 1222 } else { 1223 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); 1224 } 1225 break; 1226 } 1227 /* check for end of data */ 1228 if (sxfer->rem_len == 0) { 1229 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); 1230 break; 1231 } 1232 /* FALLTHROUGH */ 1233 1234 case LIBUSB20_TRANSFER_START: 1235 if (max_bulk > sxfer->rem_len) { 1236 max_bulk = sxfer->rem_len; 1237 } 1238 /* setup new CONTROL transaction */ 1239 if (status == LIBUSB20_TRANSFER_COMPLETED) { 1240 /* next fragment - don't send SETUP packet */ 1241 libusb20_tr_set_length(pxfer, 0, 0); 1242 } else { 1243 /* first fragment - send SETUP packet */ 1244 libusb20_tr_set_length(pxfer, 8, 0); 1245 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0); 1246 } 1247 1248 if (max_bulk != 0) { 1249 libusb20_tr_set_length(pxfer, max_bulk, 1); 1250 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1); 1251 libusb20_tr_set_total_frames(pxfer, 2); 1252 } else { 1253 libusb20_tr_set_total_frames(pxfer, 1); 1254 } 1255 1256 /* update counters */ 1257 sxfer->last_len = max_bulk; 1258 sxfer->curr_data += max_bulk; 1259 sxfer->rem_len -= max_bulk; 1260 1261 libusb20_tr_submit(pxfer); 1262 1263 /* check if we can fork another USB transfer */ 1264 if (sxfer->rem_len == 0) 1265 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint); 1266 break; 1267 1268 default: 1269 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status)); 1270 break; 1271 } 1272} 1273 1274/* The following function must be called locked */ 1275 1276static void 1277libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint) 1278{ 1279 struct libusb20_transfer *pxfer0; 1280 struct libusb20_transfer *pxfer1; 1281 struct libusb_super_transfer *sxfer; 1282 struct libusb_transfer *uxfer; 1283 struct libusb_device *dev; 1284 int err; 1285 int buffsize; 1286 int maxframe; 1287 int temp; 1288 uint8_t dummy; 1289 1290 dev = libusb_get_device(pdev); 1291 1292 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0); 1293 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1); 1294 1295 if (pxfer0 == NULL || pxfer1 == NULL) 1296 return; /* shouldn't happen */ 1297 1298 temp = 0; 1299 if (libusb20_tr_pending(pxfer0)) 1300 temp |= 1; 1301 if (libusb20_tr_pending(pxfer1)) 1302 temp |= 2; 1303 1304 switch (temp) { 1305 case 3: 1306 /* wait till one of the transfers complete */ 1307 return; 1308 case 2: 1309 sxfer = libusb20_tr_get_priv_sc1(pxfer1); 1310 if (sxfer == NULL) 1311 return; /* cancelling */ 1312 if (sxfer->rem_len) 1313 return; /* cannot queue another one */ 1314 /* swap transfers */ 1315 pxfer1 = pxfer0; 1316 break; 1317 case 1: 1318 sxfer = libusb20_tr_get_priv_sc1(pxfer0); 1319 if (sxfer == NULL) 1320 return; /* cancelling */ 1321 if (sxfer->rem_len) 1322 return; /* cannot queue another one */ 1323 /* swap transfers */ 1324 pxfer0 = pxfer1; 1325 break; 1326 default: 1327 break; 1328 } 1329 1330 /* find next transfer on same endpoint */ 1331 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) { 1332 1333 uxfer = (struct libusb_transfer *)( 1334 ((uint8_t *)sxfer) + sizeof(*sxfer)); 1335 1336 if (uxfer->endpoint == endpoint) { 1337 TAILQ_REMOVE(&dev->tr_head, sxfer, entry); 1338 sxfer->entry.tqe_prev = NULL; 1339 goto found; 1340 } 1341 } 1342 return; /* success */ 1343 1344found: 1345 1346 libusb20_tr_set_priv_sc0(pxfer0, pdev); 1347 libusb20_tr_set_priv_sc1(pxfer0, sxfer); 1348 1349 /* reset super transfer state */ 1350 sxfer->rem_len = uxfer->length; 1351 sxfer->curr_data = uxfer->buffer; 1352 uxfer->actual_length = 0; 1353 1354 switch (uxfer->type) { 1355 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: 1356 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy); 1357 break; 1358 case LIBUSB_TRANSFER_TYPE_BULK: 1359 case LIBUSB_TRANSFER_TYPE_INTERRUPT: 1360 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy); 1361 break; 1362 case LIBUSB_TRANSFER_TYPE_CONTROL: 1363 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy); 1364 if (sxfer->rem_len < 8) 1365 goto failure; 1366 1367 /* remove SETUP packet from data */ 1368 sxfer->rem_len -= 8; 1369 sxfer->curr_data += 8; 1370 break; 1371 default: 1372 goto failure; 1373 } 1374 1375 buffsize = libusb10_get_buffsize(pdev, uxfer); 1376 maxframe = libusb10_get_maxframe(pdev, uxfer); 1377 1378 /* make sure the transfer is opened */ 1379 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint); 1380 if (err && (err != LIBUSB20_ERROR_BUSY)) { 1381 goto failure; 1382 } 1383 libusb20_tr_start(pxfer0); 1384 return; 1385 1386failure: 1387 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR); 1388 1389 /* make sure our event loop spins the done handler */ 1390 dummy = 0; 1391 err = write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy)); 1392} 1393 1394/* The following function must be called unlocked */ 1395 1396int 1397libusb_submit_transfer(struct libusb_transfer *uxfer) 1398{ 1399 struct libusb20_transfer *pxfer0; 1400 struct libusb20_transfer *pxfer1; 1401 struct libusb_super_transfer *sxfer; 1402 struct libusb_device *dev; 1403 uint8_t endpoint; 1404 int err; 1405 1406 if (uxfer == NULL) 1407 return (LIBUSB_ERROR_INVALID_PARAM); 1408 1409 if (uxfer->dev_handle == NULL) 1410 return (LIBUSB_ERROR_INVALID_PARAM); 1411 1412 endpoint = uxfer->endpoint; 1413 1414 dev = libusb_get_device(uxfer->dev_handle); 1415 1416 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter"); 1417 1418 sxfer = (struct libusb_super_transfer *)( 1419 (uint8_t *)uxfer - sizeof(*sxfer)); 1420 1421 CTX_LOCK(dev->ctx); 1422 1423 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0); 1424 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1); 1425 1426 if (pxfer0 == NULL || pxfer1 == NULL) { 1427 err = LIBUSB_ERROR_OTHER; 1428 } else if ((sxfer->entry.tqe_prev != NULL) || 1429 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) || 1430 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) { 1431 err = LIBUSB_ERROR_BUSY; 1432 } else { 1433 1434 /* set pending state */ 1435 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND; 1436 1437 /* insert transfer into transfer head list */ 1438 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry); 1439 1440 /* start work transfers */ 1441 libusb10_submit_transfer_sub( 1442 uxfer->dev_handle, endpoint); 1443 1444 err = 0; /* success */ 1445 } 1446 1447 CTX_UNLOCK(dev->ctx); 1448 1449 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err); 1450 1451 return (err); 1452} 1453 1454/* Asynchronous transfer cancel */ 1455 1456int 1457libusb_cancel_transfer(struct libusb_transfer *uxfer) 1458{ 1459 struct libusb20_transfer *pxfer0; 1460 struct libusb20_transfer *pxfer1; 1461 struct libusb_super_transfer *sxfer; 1462 struct libusb_device *dev; 1463 uint8_t endpoint; 1464 int retval; 1465 1466 if (uxfer == NULL) 1467 return (LIBUSB_ERROR_INVALID_PARAM); 1468 1469 /* check if not initialised */ 1470 if (uxfer->dev_handle == NULL) 1471 return (LIBUSB_ERROR_NOT_FOUND); 1472 1473 endpoint = uxfer->endpoint; 1474 1475 dev = libusb_get_device(uxfer->dev_handle); 1476 1477 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter"); 1478 1479 sxfer = (struct libusb_super_transfer *)( 1480 (uint8_t *)uxfer - sizeof(*sxfer)); 1481 1482 retval = 0; 1483 1484 CTX_LOCK(dev->ctx); 1485 1486 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0); 1487 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1); 1488 1489 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) { 1490 /* only update the transfer status */ 1491 uxfer->status = LIBUSB_TRANSFER_CANCELLED; 1492 retval = LIBUSB_ERROR_NOT_FOUND; 1493 } else if (sxfer->entry.tqe_prev != NULL) { 1494 /* we are lucky - transfer is on a queue */ 1495 TAILQ_REMOVE(&dev->tr_head, sxfer, entry); 1496 sxfer->entry.tqe_prev = NULL; 1497 libusb10_complete_transfer(NULL, 1498 sxfer, LIBUSB_TRANSFER_CANCELLED); 1499 } else if (pxfer0 == NULL || pxfer1 == NULL) { 1500 /* not started */ 1501 retval = LIBUSB_ERROR_NOT_FOUND; 1502 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) { 1503 libusb10_complete_transfer(pxfer0, 1504 sxfer, LIBUSB_TRANSFER_CANCELLED); 1505 libusb20_tr_stop(pxfer0); 1506 /* make sure the queue doesn't stall */ 1507 libusb10_submit_transfer_sub( 1508 uxfer->dev_handle, endpoint); 1509 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) { 1510 libusb10_complete_transfer(pxfer1, 1511 sxfer, LIBUSB_TRANSFER_CANCELLED); 1512 libusb20_tr_stop(pxfer1); 1513 /* make sure the queue doesn't stall */ 1514 libusb10_submit_transfer_sub( 1515 uxfer->dev_handle, endpoint); 1516 } else { 1517 /* not started */ 1518 retval = LIBUSB_ERROR_NOT_FOUND; 1519 } 1520 1521 CTX_UNLOCK(dev->ctx); 1522 1523 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave"); 1524 1525 return (retval); 1526} 1527 1528UNEXPORTED void 1529libusb10_cancel_all_transfer(libusb_device *dev) 1530{ 1531 struct libusb20_device *pdev = dev->os_priv; 1532 unsigned x; 1533 1534 for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) { 1535 struct libusb20_transfer *xfer; 1536 1537 xfer = libusb20_tr_get_pointer(pdev, x); 1538 if (xfer == NULL) 1539 continue; 1540 libusb20_tr_close(xfer); 1541 } 1542} 1543 1544uint16_t 1545libusb_cpu_to_le16(uint16_t x) 1546{ 1547 return (htole16(x)); 1548} 1549 1550uint16_t 1551libusb_le16_to_cpu(uint16_t x) 1552{ 1553 return (le16toh(x)); 1554} 1555 1556const char * 1557libusb_strerror(int code) 1558{ 1559 switch (code) { 1560 case LIBUSB_SUCCESS: 1561 return ("Success"); 1562 case LIBUSB_ERROR_IO: 1563 return ("I/O error"); 1564 case LIBUSB_ERROR_INVALID_PARAM: 1565 return ("Invalid parameter"); 1566 case LIBUSB_ERROR_ACCESS: 1567 return ("Permissions error"); 1568 case LIBUSB_ERROR_NO_DEVICE: 1569 return ("No device"); 1570 case LIBUSB_ERROR_NOT_FOUND: 1571 return ("Not found"); 1572 case LIBUSB_ERROR_BUSY: 1573 return ("Device busy"); 1574 case LIBUSB_ERROR_TIMEOUT: 1575 return ("Timeout"); 1576 case LIBUSB_ERROR_OVERFLOW: 1577 return ("Overflow"); 1578 case LIBUSB_ERROR_PIPE: 1579 return ("Pipe error"); 1580 case LIBUSB_ERROR_INTERRUPTED: 1581 return ("Interrupted"); 1582 case LIBUSB_ERROR_NO_MEM: 1583 return ("Out of memory"); 1584 case LIBUSB_ERROR_NOT_SUPPORTED: 1585 return ("Not supported"); 1586 case LIBUSB_ERROR_OTHER: 1587 return ("Other error"); 1588 default: 1589 return ("Unknown error"); 1590 } 1591} 1592 1593const char * 1594libusb_error_name(int code) 1595{ 1596 switch (code) { 1597 case LIBUSB_SUCCESS: 1598 return ("LIBUSB_SUCCESS"); 1599 case LIBUSB_ERROR_IO: 1600 return ("LIBUSB_ERROR_IO"); 1601 case LIBUSB_ERROR_INVALID_PARAM: 1602 return ("LIBUSB_ERROR_INVALID_PARAM"); 1603 case LIBUSB_ERROR_ACCESS: 1604 return ("LIBUSB_ERROR_ACCESS"); 1605 case LIBUSB_ERROR_NO_DEVICE: 1606 return ("LIBUSB_ERROR_NO_DEVICE"); 1607 case LIBUSB_ERROR_NOT_FOUND: 1608 return ("LIBUSB_ERROR_NOT_FOUND"); 1609 case LIBUSB_ERROR_BUSY: 1610 return ("LIBUSB_ERROR_BUSY"); 1611 case LIBUSB_ERROR_TIMEOUT: 1612 return ("LIBUSB_ERROR_TIMEOUT"); 1613 case LIBUSB_ERROR_OVERFLOW: 1614 return ("LIBUSB_ERROR_OVERFLOW"); 1615 case LIBUSB_ERROR_PIPE: 1616 return ("LIBUSB_ERROR_PIPE"); 1617 case LIBUSB_ERROR_INTERRUPTED: 1618 return ("LIBUSB_ERROR_INTERRUPTED"); 1619 case LIBUSB_ERROR_NO_MEM: 1620 return ("LIBUSB_ERROR_NO_MEM"); 1621 case LIBUSB_ERROR_NOT_SUPPORTED: 1622 return ("LIBUSB_ERROR_NOT_SUPPORTED"); 1623 case LIBUSB_ERROR_OTHER: 1624 return ("LIBUSB_ERROR_OTHER"); 1625 default: 1626 return ("LIBUSB_ERROR_UNKNOWN"); 1627 } 1628}
|