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