1 /* $FreeBSD$ */ 2 /*- 3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 4 * 5 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #ifdef LIBUSB_GLOBAL_INCLUDE_FILE 30 #include LIBUSB_GLOBAL_INCLUDE_FILE 31 #else 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 <time.h> 39 #include <unistd.h> 40 #include <sys/queue.h> 41 #include <sys/endian.h> 42 #endif 43 44 #define libusb_device_handle libusb20_device 45 46 #include "libusb20.h" 47 #include "libusb20_desc.h" 48 #include "libusb20_int.h" 49 #include "libusb.h" 50 #include "libusb10.h" 51 52 UNEXPORTED void 53 libusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd, 54 struct libusb20_device *pdev, int fd, short events) 55 { 56 if (ctx == NULL) 57 return; /* invalid */ 58 59 if (pollfd->entry.tqe_prev != NULL) 60 return; /* already queued */ 61 62 if (fd < 0) 63 return; /* invalid */ 64 65 pollfd->pdev = pdev; 66 pollfd->pollfd.fd = fd; 67 pollfd->pollfd.events = events; 68 69 CTX_LOCK(ctx); 70 TAILQ_INSERT_TAIL(&ctx->pollfds, pollfd, entry); 71 CTX_UNLOCK(ctx); 72 73 if (ctx->fd_added_cb) 74 ctx->fd_added_cb(fd, events, ctx->fd_cb_user_data); 75 } 76 77 UNEXPORTED void 78 libusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd) 79 { 80 if (ctx == NULL) 81 return; /* invalid */ 82 83 if (pollfd->entry.tqe_prev == NULL) 84 return; /* already dequeued */ 85 86 CTX_LOCK(ctx); 87 TAILQ_REMOVE(&ctx->pollfds, pollfd, entry); 88 pollfd->entry.tqe_prev = NULL; 89 CTX_UNLOCK(ctx); 90 91 if (ctx->fd_removed_cb) 92 ctx->fd_removed_cb(pollfd->pollfd.fd, ctx->fd_cb_user_data); 93 } 94 95 /* This function must be called locked */ 96 97 static int 98 libusb10_handle_events_sub(struct libusb_context *ctx, struct timeval *tv) 99 { 100 struct libusb_device *dev; 101 struct libusb20_device **ppdev; 102 struct libusb_super_pollfd *pfd; 103 struct pollfd *fds; 104 struct libusb_super_transfer *sxfer; 105 struct libusb_transfer *uxfer; 106 nfds_t nfds; 107 int timeout; 108 int i; 109 int err; 110 111 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb10_handle_events_sub enter"); 112 113 nfds = 0; 114 i = 0; 115 TAILQ_FOREACH(pfd, &ctx->pollfds, entry) 116 nfds++; 117 118 fds = alloca(sizeof(*fds) * nfds); 119 if (fds == NULL) 120 return (LIBUSB_ERROR_NO_MEM); 121 122 ppdev = alloca(sizeof(*ppdev) * nfds); 123 if (ppdev == NULL) 124 return (LIBUSB_ERROR_NO_MEM); 125 126 TAILQ_FOREACH(pfd, &ctx->pollfds, entry) { 127 fds[i].fd = pfd->pollfd.fd; 128 fds[i].events = pfd->pollfd.events; 129 fds[i].revents = 0; 130 ppdev[i] = pfd->pdev; 131 if (pfd->pdev != NULL) 132 libusb_get_device(pfd->pdev)->refcnt++; 133 i++; 134 } 135 136 if (tv == NULL) 137 timeout = -1; 138 else 139 timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 999) / 1000); 140 141 CTX_UNLOCK(ctx); 142 err = poll(fds, nfds, timeout); 143 CTX_LOCK(ctx); 144 145 if ((err == -1) && (errno == EINTR)) 146 err = LIBUSB_ERROR_INTERRUPTED; 147 else if (err < 0) 148 err = LIBUSB_ERROR_IO; 149 150 if (err < 1) { 151 for (i = 0; i != (int)nfds; i++) { 152 if (ppdev[i] != NULL) { 153 CTX_UNLOCK(ctx); 154 libusb_unref_device(libusb_get_device(ppdev[i])); 155 CTX_LOCK(ctx); 156 } 157 } 158 goto do_done; 159 } 160 for (i = 0; i != (int)nfds; i++) { 161 if (ppdev[i] != NULL) { 162 dev = libusb_get_device(ppdev[i]); 163 164 if (fds[i].revents != 0) { 165 err = libusb20_dev_process(ppdev[i]); 166 167 if (err) { 168 /* set device is gone */ 169 dev->device_is_gone = 1; 170 171 /* remove USB device from polling loop */ 172 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll); 173 174 /* cancel all pending transfers */ 175 libusb10_cancel_all_transfer_locked(ppdev[i], dev); 176 } 177 } 178 CTX_UNLOCK(ctx); 179 libusb_unref_device(dev); 180 CTX_LOCK(ctx); 181 182 } else { 183 uint8_t dummy; 184 185 while (read(fds[i].fd, &dummy, 1) == 1) 186 ; 187 } 188 } 189 190 err = 0; 191 192 do_done: 193 194 /* Do all done callbacks */ 195 196 while ((sxfer = TAILQ_FIRST(&ctx->tr_done))) { 197 uint8_t flags; 198 199 TAILQ_REMOVE(&ctx->tr_done, sxfer, entry); 200 sxfer->entry.tqe_prev = NULL; 201 202 ctx->tr_done_ref++; 203 204 CTX_UNLOCK(ctx); 205 206 uxfer = (struct libusb_transfer *)( 207 ((uint8_t *)sxfer) + sizeof(*sxfer)); 208 209 /* Allow the callback to free the transfer itself. */ 210 flags = uxfer->flags; 211 212 if (uxfer->callback != NULL) 213 (uxfer->callback) (uxfer); 214 215 /* Check if the USB transfer should be automatically freed. */ 216 if (flags & LIBUSB_TRANSFER_FREE_TRANSFER) 217 libusb_free_transfer(uxfer); 218 219 CTX_LOCK(ctx); 220 221 ctx->tr_done_ref--; 222 ctx->tr_done_gen++; 223 } 224 225 /* Wakeup other waiters */ 226 pthread_cond_broadcast(&ctx->ctx_cond); 227 228 return (err); 229 } 230 231 /* Polling and timing */ 232 233 int 234 libusb_try_lock_events(libusb_context *ctx) 235 { 236 int err; 237 238 ctx = GET_CONTEXT(ctx); 239 if (ctx == NULL) 240 return (1); 241 242 err = CTX_TRYLOCK(ctx); 243 if (err) 244 return (1); 245 246 err = (ctx->ctx_handler != NO_THREAD); 247 if (err) 248 CTX_UNLOCK(ctx); 249 else 250 ctx->ctx_handler = pthread_self(); 251 252 return (err); 253 } 254 255 void 256 libusb_lock_events(libusb_context *ctx) 257 { 258 ctx = GET_CONTEXT(ctx); 259 CTX_LOCK(ctx); 260 if (ctx->ctx_handler == NO_THREAD) 261 ctx->ctx_handler = pthread_self(); 262 } 263 264 void 265 libusb_unlock_events(libusb_context *ctx) 266 { 267 ctx = GET_CONTEXT(ctx); 268 if (ctx->ctx_handler == pthread_self()) { 269 ctx->ctx_handler = NO_THREAD; 270 pthread_cond_broadcast(&ctx->ctx_cond); 271 } 272 CTX_UNLOCK(ctx); 273 } 274 275 int 276 libusb_event_handling_ok(libusb_context *ctx) 277 { 278 ctx = GET_CONTEXT(ctx); 279 return (ctx->ctx_handler == pthread_self()); 280 } 281 282 int 283 libusb_event_handler_active(libusb_context *ctx) 284 { 285 ctx = GET_CONTEXT(ctx); 286 return (ctx->ctx_handler != NO_THREAD); 287 } 288 289 void 290 libusb_lock_event_waiters(libusb_context *ctx) 291 { 292 ctx = GET_CONTEXT(ctx); 293 CTX_LOCK(ctx); 294 } 295 296 void 297 libusb_unlock_event_waiters(libusb_context *ctx) 298 { 299 ctx = GET_CONTEXT(ctx); 300 CTX_UNLOCK(ctx); 301 } 302 303 int 304 libusb_wait_for_event(libusb_context *ctx, struct timeval *tv) 305 { 306 struct timespec ts; 307 int err; 308 309 ctx = GET_CONTEXT(ctx); 310 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_wait_for_event enter"); 311 312 if (tv == NULL) { 313 pthread_cond_wait(&ctx->ctx_cond, 314 &ctx->ctx_lock); 315 /* try to grab polling of actual events, if any */ 316 if (ctx->ctx_handler == NO_THREAD) 317 ctx->ctx_handler = pthread_self(); 318 return (0); 319 } 320 err = clock_gettime(CLOCK_MONOTONIC, &ts); 321 if (err < 0) 322 return (LIBUSB_ERROR_OTHER); 323 324 /* 325 * The "tv" arguments points to a relative time structure and 326 * not an absolute time structure. 327 */ 328 ts.tv_sec += tv->tv_sec; 329 ts.tv_nsec += tv->tv_usec * 1000; 330 if (ts.tv_nsec >= 1000000000) { 331 ts.tv_nsec -= 1000000000; 332 ts.tv_sec++; 333 } 334 err = pthread_cond_timedwait(&ctx->ctx_cond, 335 &ctx->ctx_lock, &ts); 336 /* try to grab polling of actual events, if any */ 337 if (ctx->ctx_handler == NO_THREAD) 338 ctx->ctx_handler = pthread_self(); 339 340 if (err == ETIMEDOUT) 341 return (1); 342 343 return (0); 344 } 345 346 int 347 libusb_handle_events_timeout_completed(libusb_context *ctx, 348 struct timeval *tv, int *completed) 349 { 350 int err = 0; 351 352 ctx = GET_CONTEXT(ctx); 353 354 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed enter"); 355 356 libusb_lock_events(ctx); 357 358 while (1) { 359 if (completed != NULL) { 360 if (*completed != 0 || err != 0) 361 break; 362 } 363 err = libusb_handle_events_locked(ctx, tv); 364 if (completed == NULL) 365 break; 366 } 367 368 libusb_unlock_events(ctx); 369 370 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed exit"); 371 372 return (err); 373 } 374 375 int 376 libusb_handle_events_completed(libusb_context *ctx, int *completed) 377 { 378 return (libusb_handle_events_timeout_completed(ctx, NULL, completed)); 379 } 380 381 int 382 libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv) 383 { 384 return (libusb_handle_events_timeout_completed(ctx, tv, NULL)); 385 } 386 387 int 388 libusb_handle_events(libusb_context *ctx) 389 { 390 return (libusb_handle_events_timeout_completed(ctx, NULL, NULL)); 391 } 392 393 int 394 libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv) 395 { 396 int err; 397 398 ctx = GET_CONTEXT(ctx); 399 400 if (libusb_event_handling_ok(ctx)) { 401 err = libusb10_handle_events_sub(ctx, tv); 402 } else { 403 err = libusb_wait_for_event(ctx, tv); 404 if (err != 0) 405 err = LIBUSB_ERROR_TIMEOUT; 406 } 407 return (err); 408 } 409 410 int 411 libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv) 412 { 413 /* all timeouts are currently being done by the kernel */ 414 timerclear(tv); 415 return (0); 416 } 417 418 void 419 libusb_set_pollfd_notifiers(libusb_context *ctx, 420 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb, 421 void *user_data) 422 { 423 ctx = GET_CONTEXT(ctx); 424 425 ctx->fd_added_cb = added_cb; 426 ctx->fd_removed_cb = removed_cb; 427 ctx->fd_cb_user_data = user_data; 428 } 429 430 const struct libusb_pollfd ** 431 libusb_get_pollfds(libusb_context *ctx) 432 { 433 struct libusb_super_pollfd *pollfd; 434 libusb_pollfd **ret; 435 int i; 436 437 ctx = GET_CONTEXT(ctx); 438 439 CTX_LOCK(ctx); 440 441 i = 0; 442 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry) 443 i++; 444 445 ret = calloc(i + 1, sizeof(struct libusb_pollfd *)); 446 if (ret == NULL) 447 goto done; 448 449 i = 0; 450 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry) 451 ret[i++] = &pollfd->pollfd; 452 ret[i] = NULL; 453 454 done: 455 CTX_UNLOCK(ctx); 456 return ((const struct libusb_pollfd **)ret); 457 } 458 459 460 /* Synchronous device I/O */ 461 462 int 463 libusb_control_transfer(libusb_device_handle *devh, 464 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, 465 uint8_t *data, uint16_t wLength, unsigned int timeout) 466 { 467 struct LIBUSB20_CONTROL_SETUP_DECODED req; 468 int err; 469 uint16_t actlen; 470 471 if (devh == NULL) 472 return (LIBUSB_ERROR_INVALID_PARAM); 473 474 if ((wLength != 0) && (data == NULL)) 475 return (LIBUSB_ERROR_INVALID_PARAM); 476 477 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req); 478 479 req.bmRequestType = bmRequestType; 480 req.bRequest = bRequest; 481 req.wValue = wValue; 482 req.wIndex = wIndex; 483 req.wLength = wLength; 484 485 err = libusb20_dev_request_sync(devh, &req, data, 486 &actlen, timeout, 0); 487 488 if (err == LIBUSB20_ERROR_PIPE) 489 return (LIBUSB_ERROR_PIPE); 490 else if (err == LIBUSB20_ERROR_TIMEOUT) 491 return (LIBUSB_ERROR_TIMEOUT); 492 else if (err) 493 return (LIBUSB_ERROR_NO_DEVICE); 494 495 return (actlen); 496 } 497 498 static libusb_context * 499 libusb10_get_context_by_device_handle(libusb_device_handle *devh) 500 { 501 libusb_context *ctx; 502 503 if (devh != NULL) 504 ctx = libusb_get_device(devh)->ctx; 505 else 506 ctx = NULL; 507 508 return (GET_CONTEXT(ctx)); 509 } 510 511 static void 512 libusb10_do_transfer_cb(struct libusb_transfer *transfer) 513 { 514 libusb_context *ctx; 515 int *pdone; 516 517 ctx = libusb10_get_context_by_device_handle(transfer->dev_handle); 518 519 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "sync I/O done"); 520 521 pdone = transfer->user_data; 522 *pdone = 1; 523 } 524 525 /* 526 * TODO: Replace the following function. Allocating and freeing on a 527 * per-transfer basis is slow. --HPS 528 */ 529 static int 530 libusb10_do_transfer(libusb_device_handle *devh, 531 uint8_t endpoint, uint8_t *data, int length, 532 int *transferred, unsigned int timeout, int type) 533 { 534 libusb_context *ctx; 535 struct libusb_transfer *xfer; 536 int done; 537 int ret; 538 539 if (devh == NULL) 540 return (LIBUSB_ERROR_INVALID_PARAM); 541 542 if ((length != 0) && (data == NULL)) 543 return (LIBUSB_ERROR_INVALID_PARAM); 544 545 xfer = libusb_alloc_transfer(0); 546 if (xfer == NULL) 547 return (LIBUSB_ERROR_NO_MEM); 548 549 ctx = libusb_get_device(devh)->ctx; 550 551 xfer->dev_handle = devh; 552 xfer->endpoint = endpoint; 553 xfer->type = type; 554 xfer->timeout = timeout; 555 xfer->buffer = data; 556 xfer->length = length; 557 xfer->user_data = (void *)&done; 558 xfer->callback = libusb10_do_transfer_cb; 559 done = 0; 560 561 if ((ret = libusb_submit_transfer(xfer)) < 0) { 562 libusb_free_transfer(xfer); 563 return (ret); 564 } 565 while (done == 0) { 566 if ((ret = libusb_handle_events(ctx)) < 0) { 567 libusb_cancel_transfer(xfer); 568 usleep(1000); /* nice it */ 569 } 570 } 571 572 *transferred = xfer->actual_length; 573 574 switch (xfer->status) { 575 case LIBUSB_TRANSFER_COMPLETED: 576 ret = 0; 577 break; 578 case LIBUSB_TRANSFER_TIMED_OUT: 579 ret = LIBUSB_ERROR_TIMEOUT; 580 break; 581 case LIBUSB_TRANSFER_OVERFLOW: 582 ret = LIBUSB_ERROR_OVERFLOW; 583 break; 584 case LIBUSB_TRANSFER_STALL: 585 ret = LIBUSB_ERROR_PIPE; 586 break; 587 case LIBUSB_TRANSFER_NO_DEVICE: 588 ret = LIBUSB_ERROR_NO_DEVICE; 589 break; 590 default: 591 ret = LIBUSB_ERROR_OTHER; 592 break; 593 } 594 595 libusb_free_transfer(xfer); 596 return (ret); 597 } 598 599 int 600 libusb_bulk_transfer(libusb_device_handle *devh, 601 uint8_t endpoint, uint8_t *data, int length, 602 int *transferred, unsigned int timeout) 603 { 604 libusb_context *ctx; 605 int ret; 606 607 ctx = libusb10_get_context_by_device_handle(devh); 608 609 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter"); 610 611 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred, 612 timeout, LIBUSB_TRANSFER_TYPE_BULK); 613 614 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave"); 615 return (ret); 616 } 617 618 int 619 libusb_interrupt_transfer(libusb_device_handle *devh, 620 uint8_t endpoint, uint8_t *data, int length, 621 int *transferred, unsigned int timeout) 622 { 623 libusb_context *ctx; 624 int ret; 625 626 ctx = libusb10_get_context_by_device_handle(devh); 627 628 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter"); 629 630 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred, 631 timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT); 632 633 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave"); 634 return (ret); 635 } 636 637 uint8_t * 638 libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t off) 639 { 640 uint8_t *ptr; 641 uint32_t n; 642 643 if (transfer->num_iso_packets < 0) 644 return (NULL); 645 646 if (off >= (uint32_t)transfer->num_iso_packets) 647 return (NULL); 648 649 ptr = transfer->buffer; 650 if (ptr == NULL) 651 return (NULL); 652 653 for (n = 0; n != off; n++) { 654 ptr += transfer->iso_packet_desc[n].length; 655 } 656 return (ptr); 657 } 658 659 uint8_t * 660 libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t off) 661 { 662 uint8_t *ptr; 663 664 if (transfer->num_iso_packets < 0) 665 return (NULL); 666 667 if (off >= (uint32_t)transfer->num_iso_packets) 668 return (NULL); 669 670 ptr = transfer->buffer; 671 if (ptr == NULL) 672 return (NULL); 673 674 ptr += transfer->iso_packet_desc[0].length * off; 675 676 return (ptr); 677 } 678 679 void 680 libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length) 681 { 682 int n; 683 684 if (transfer->num_iso_packets < 0) 685 return; 686 687 for (n = 0; n != transfer->num_iso_packets; n++) 688 transfer->iso_packet_desc[n].length = length; 689 } 690 691 uint8_t * 692 libusb_control_transfer_get_data(struct libusb_transfer *transfer) 693 { 694 if (transfer->buffer == NULL) 695 return (NULL); 696 697 return (transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE); 698 } 699 700 struct libusb_control_setup * 701 libusb_control_transfer_get_setup(struct libusb_transfer *transfer) 702 { 703 return ((struct libusb_control_setup *)transfer->buffer); 704 } 705 706 void 707 libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType, 708 uint8_t bRequest, uint16_t wValue, 709 uint16_t wIndex, uint16_t wLength) 710 { 711 struct libusb_control_setup *req = (struct libusb_control_setup *)buf; 712 713 /* The alignment is OK for all fields below. */ 714 req->bmRequestType = bmRequestType; 715 req->bRequest = bRequest; 716 req->wValue = htole16(wValue); 717 req->wIndex = htole16(wIndex); 718 req->wLength = htole16(wLength); 719 } 720 721 void 722 libusb_fill_control_transfer(struct libusb_transfer *transfer, 723 libusb_device_handle *devh, uint8_t *buf, 724 libusb_transfer_cb_fn callback, void *user_data, 725 uint32_t timeout) 726 { 727 struct libusb_control_setup *setup = (struct libusb_control_setup *)buf; 728 729 transfer->dev_handle = devh; 730 transfer->endpoint = 0; 731 transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL; 732 transfer->timeout = timeout; 733 transfer->buffer = buf; 734 if (setup != NULL) 735 transfer->length = LIBUSB_CONTROL_SETUP_SIZE 736 + le16toh(setup->wLength); 737 else 738 transfer->length = 0; 739 transfer->user_data = user_data; 740 transfer->callback = callback; 741 742 } 743 744 void 745 libusb_fill_bulk_transfer(struct libusb_transfer *transfer, 746 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf, 747 int length, libusb_transfer_cb_fn callback, void *user_data, 748 uint32_t timeout) 749 { 750 transfer->dev_handle = devh; 751 transfer->endpoint = endpoint; 752 transfer->type = LIBUSB_TRANSFER_TYPE_BULK; 753 transfer->timeout = timeout; 754 transfer->buffer = buf; 755 transfer->length = length; 756 transfer->user_data = user_data; 757 transfer->callback = callback; 758 } 759 760 void 761 libusb_fill_interrupt_transfer(struct libusb_transfer *transfer, 762 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf, 763 int length, libusb_transfer_cb_fn callback, void *user_data, 764 uint32_t timeout) 765 { 766 transfer->dev_handle = devh; 767 transfer->endpoint = endpoint; 768 transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT; 769 transfer->timeout = timeout; 770 transfer->buffer = buf; 771 transfer->length = length; 772 transfer->user_data = user_data; 773 transfer->callback = callback; 774 } 775 776 void 777 libusb_fill_iso_transfer(struct libusb_transfer *transfer, 778 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf, 779 int length, int npacket, libusb_transfer_cb_fn callback, 780 void *user_data, uint32_t timeout) 781 { 782 transfer->dev_handle = devh; 783 transfer->endpoint = endpoint; 784 transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS; 785 transfer->timeout = timeout; 786 transfer->buffer = buf; 787 transfer->length = length; 788 transfer->num_iso_packets = npacket; 789 transfer->user_data = user_data; 790 transfer->callback = callback; 791 } 792 793 int 794 libusb_alloc_streams(libusb_device_handle *dev, uint32_t num_streams, 795 unsigned char *endpoints, int num_endpoints) 796 { 797 if (num_streams > 1) 798 return (LIBUSB_ERROR_INVALID_PARAM); 799 return (0); 800 } 801 802 int 803 libusb_free_streams(libusb_device_handle *dev, unsigned char *endpoints, int num_endpoints) 804 { 805 806 return (0); 807 } 808 809 void 810 libusb_transfer_set_stream_id(struct libusb_transfer *transfer, uint32_t stream_id) 811 { 812 struct libusb_super_transfer *sxfer; 813 814 if (transfer == NULL) 815 return; 816 817 sxfer = (struct libusb_super_transfer *)( 818 ((uint8_t *)transfer) - sizeof(*sxfer)); 819 820 /* set stream ID */ 821 sxfer->stream_id = stream_id; 822 } 823 824 uint32_t 825 libusb_transfer_get_stream_id(struct libusb_transfer *transfer) 826 { 827 struct libusb_super_transfer *sxfer; 828 829 if (transfer == NULL) 830 return (0); 831 832 sxfer = (struct libusb_super_transfer *)( 833 ((uint8_t *)transfer) - sizeof(*sxfer)); 834 835 /* get stream ID */ 836 return (sxfer->stream_id); 837 } 838