libusb10_io.c revision 338789
1139743Simp/* $FreeBSD: stable/10/lib/libusb/libusb10_io.c 338789 2018-09-19 07:57:30Z hselasky $ */ 243412Snewton/*- 343412Snewton * Copyright (c) 2009 Sylvestre Gallon. All rights reserved. 443412Snewton * 543412Snewton * Redistribution and use in source and binary forms, with or without 643412Snewton * modification, are permitted provided that the following conditions 743412Snewton * are met: 843412Snewton * 1. Redistributions of source code must retain the above copyright 943412Snewton * notice, this list of conditions and the following disclaimer. 1043412Snewton * 2. Redistributions in binary form must reproduce the above copyright 1143412Snewton * notice, this list of conditions and the following disclaimer in the 1243412Snewton * documentation and/or other materials provided with the distribution. 1343412Snewton * 1443412Snewton * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1543412Snewton * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1643412Snewton * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1743412Snewton * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1843412Snewton * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1943412Snewton * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2043412Snewton * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2143412Snewton * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2243412Snewton * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2343412Snewton * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2443412Snewton * SUCH DAMAGE. 2543412Snewton */ 2643412Snewton 2749267Snewton#ifdef LIBUSB_GLOBAL_INCLUDE_FILE 2850477Speter#include LIBUSB_GLOBAL_INCLUDE_FILE 2943412Snewton#else 3043412Snewton#include <errno.h> 3143412Snewton#include <poll.h> 3243412Snewton#include <pthread.h> 3343412Snewton#include <stdio.h> 3443412Snewton#include <stdlib.h> 3565302Sobrien#include <string.h> 3643412Snewton#include <time.h> 3743412Snewton#include <unistd.h> 3843412Snewton#include <sys/queue.h> 3943412Snewton#include <sys/endian.h> 4043412Snewton#endif 4143412Snewton 4243412Snewton#define libusb_device_handle libusb20_device 4343412Snewton 4443412Snewton#include "libusb20.h" 4543412Snewton#include "libusb20_desc.h" 4643412Snewton#include "libusb20_int.h" 4743412Snewton#include "libusb.h" 4843412Snewton#include "libusb10.h" 4943412Snewton 5043412SnewtonUNEXPORTED void 5143412Snewtonlibusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd, 5243412Snewton struct libusb20_device *pdev, int fd, short events) 5343412Snewton{ 5443412Snewton if (ctx == NULL) 5543412Snewton return; /* invalid */ 5643412Snewton 5743412Snewton if (pollfd->entry.tqe_prev != NULL) 5843412Snewton return; /* already queued */ 5943412Snewton 6043412Snewton if (fd < 0) 6143412Snewton return; /* invalid */ 6243412Snewton 6343412Snewton pollfd->pdev = pdev; 6443412Snewton pollfd->pollfd.fd = fd; 6543412Snewton pollfd->pollfd.events = events; 6643412Snewton 6743412Snewton CTX_LOCK(ctx); 6843412Snewton TAILQ_INSERT_TAIL(&ctx->pollfds, pollfd, entry); 6943412Snewton CTX_UNLOCK(ctx); 7043412Snewton 7156046Snewton if (ctx->fd_added_cb) 7243412Snewton ctx->fd_added_cb(fd, events, ctx->fd_cb_user_data); 7343412Snewton} 7443412Snewton 7543412SnewtonUNEXPORTED void 7643412Snewtonlibusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd) 7743412Snewton{ 7843412Snewton if (ctx == NULL) 7943412Snewton return; /* invalid */ 8043412Snewton 8192761Salfred if (pollfd->entry.tqe_prev == NULL) 8243412Snewton return; /* already dequeued */ 8343412Snewton 8443412Snewton CTX_LOCK(ctx); 8543412Snewton TAILQ_REMOVE(&ctx->pollfds, pollfd, entry); 8643412Snewton pollfd->entry.tqe_prev = NULL; 8743412Snewton CTX_UNLOCK(ctx); 8843412Snewton 8943412Snewton if (ctx->fd_removed_cb) 9043412Snewton ctx->fd_removed_cb(pollfd->pollfd.fd, ctx->fd_cb_user_data); 9143412Snewton} 9243412Snewton 9343412Snewton/* This function must be called locked */ 94142500Ssam 95142500Ssamstatic int 9651793Smarcellibusb10_handle_events_sub(struct libusb_context *ctx, struct timeval *tv) 9751793Smarcel{ 98142500Ssam struct libusb_device *dev; 9951793Smarcel struct libusb20_device **ppdev; 100142500Ssam struct libusb_super_pollfd *pfd; 10151793Smarcel struct pollfd *fds; 10243412Snewton struct libusb_super_transfer *sxfer; 10343412Snewton struct libusb_transfer *uxfer; 10443412Snewton nfds_t nfds; 10543412Snewton int timeout; 10643412Snewton int i; 10748620Scracauer int err; 10848620Scracauer 10948620Scracauer DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb10_handle_events_sub enter"); 11048620Scracauer 11143412Snewton nfds = 0; 11243412Snewton i = 0; 11343412Snewton TAILQ_FOREACH(pfd, &ctx->pollfds, entry) 11443412Snewton nfds++; 11543412Snewton 11643412Snewton fds = alloca(sizeof(*fds) * nfds); 11743412Snewton if (fds == NULL) 11843412Snewton return (LIBUSB_ERROR_NO_MEM); 11943412Snewton 12043412Snewton ppdev = alloca(sizeof(*ppdev) * nfds); 12143412Snewton if (ppdev == NULL) 12243412Snewton return (LIBUSB_ERROR_NO_MEM); 12343412Snewton 12443412Snewton TAILQ_FOREACH(pfd, &ctx->pollfds, entry) { 12543412Snewton fds[i].fd = pfd->pollfd.fd; 12643412Snewton fds[i].events = pfd->pollfd.events; 12743412Snewton fds[i].revents = 0; 12843412Snewton ppdev[i] = pfd->pdev; 12943412Snewton if (pfd->pdev != NULL) 13043412Snewton libusb_get_device(pfd->pdev)->refcnt++; 13143412Snewton i++; 13243412Snewton } 13343412Snewton 13468520Smarcel if (tv == NULL) 13568520Smarcel timeout = -1; 136151316Sdavidxu else 137151316Sdavidxu timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 999) / 1000); 13892761Salfred 13992761Salfred CTX_UNLOCK(ctx); 14092761Salfred err = poll(fds, nfds, timeout); 14192761Salfred CTX_LOCK(ctx); 142151316Sdavidxu 14343412Snewton if ((err == -1) && (errno == EINTR)) 14443412Snewton err = LIBUSB_ERROR_INTERRUPTED; 145 else if (err < 0) 146 err = LIBUSB_ERROR_IO; 147 148 if (err < 1) { 149 for (i = 0; i != (int)nfds; i++) { 150 if (ppdev[i] != NULL) { 151 CTX_UNLOCK(ctx); 152 libusb_unref_device(libusb_get_device(ppdev[i])); 153 CTX_LOCK(ctx); 154 } 155 } 156 goto do_done; 157 } 158 for (i = 0; i != (int)nfds; i++) { 159 if (ppdev[i] != NULL) { 160 dev = libusb_get_device(ppdev[i]); 161 162 if (fds[i].revents != 0) { 163 err = libusb20_dev_process(ppdev[i]); 164 165 if (err) { 166 /* set device is gone */ 167 dev->device_is_gone = 1; 168 169 /* remove USB device from polling loop */ 170 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll); 171 172 /* cancel all pending transfers */ 173 libusb10_cancel_all_transfer_locked(ppdev[i], dev); 174 } 175 } 176 CTX_UNLOCK(ctx); 177 libusb_unref_device(dev); 178 CTX_LOCK(ctx); 179 180 } else { 181 uint8_t dummy; 182 183 while (read(fds[i].fd, &dummy, 1) == 1) 184 ; 185 } 186 } 187 188 err = 0; 189 190do_done: 191 192 /* Do all done callbacks */ 193 194 while ((sxfer = TAILQ_FIRST(&ctx->tr_done))) { 195 uint8_t flags; 196 197 TAILQ_REMOVE(&ctx->tr_done, sxfer, entry); 198 sxfer->entry.tqe_prev = NULL; 199 200 ctx->tr_done_ref++; 201 202 CTX_UNLOCK(ctx); 203 204 uxfer = (struct libusb_transfer *)( 205 ((uint8_t *)sxfer) + sizeof(*sxfer)); 206 207 /* Allow the callback to free the transfer itself. */ 208 flags = uxfer->flags; 209 210 if (uxfer->callback != NULL) 211 (uxfer->callback) (uxfer); 212 213 /* Check if the USB transfer should be automatically freed. */ 214 if (flags & LIBUSB_TRANSFER_FREE_TRANSFER) 215 libusb_free_transfer(uxfer); 216 217 CTX_LOCK(ctx); 218 219 ctx->tr_done_ref--; 220 ctx->tr_done_gen++; 221 } 222 223 /* Wakeup other waiters */ 224 pthread_cond_broadcast(&ctx->ctx_cond); 225 226 return (err); 227} 228 229/* Polling and timing */ 230 231int 232libusb_try_lock_events(libusb_context *ctx) 233{ 234 int err; 235 236 ctx = GET_CONTEXT(ctx); 237 if (ctx == NULL) 238 return (1); 239 240 err = CTX_TRYLOCK(ctx); 241 if (err) 242 return (1); 243 244 err = (ctx->ctx_handler != NO_THREAD); 245 if (err) 246 CTX_UNLOCK(ctx); 247 else 248 ctx->ctx_handler = pthread_self(); 249 250 return (err); 251} 252 253void 254libusb_lock_events(libusb_context *ctx) 255{ 256 ctx = GET_CONTEXT(ctx); 257 CTX_LOCK(ctx); 258 if (ctx->ctx_handler == NO_THREAD) 259 ctx->ctx_handler = pthread_self(); 260} 261 262void 263libusb_unlock_events(libusb_context *ctx) 264{ 265 ctx = GET_CONTEXT(ctx); 266 if (ctx->ctx_handler == pthread_self()) { 267 ctx->ctx_handler = NO_THREAD; 268 pthread_cond_broadcast(&ctx->ctx_cond); 269 } 270 CTX_UNLOCK(ctx); 271} 272 273int 274libusb_event_handling_ok(libusb_context *ctx) 275{ 276 ctx = GET_CONTEXT(ctx); 277 return (ctx->ctx_handler == pthread_self()); 278} 279 280int 281libusb_event_handler_active(libusb_context *ctx) 282{ 283 ctx = GET_CONTEXT(ctx); 284 return (ctx->ctx_handler != NO_THREAD); 285} 286 287void 288libusb_lock_event_waiters(libusb_context *ctx) 289{ 290 ctx = GET_CONTEXT(ctx); 291 CTX_LOCK(ctx); 292} 293 294void 295libusb_unlock_event_waiters(libusb_context *ctx) 296{ 297 ctx = GET_CONTEXT(ctx); 298 CTX_UNLOCK(ctx); 299} 300 301int 302libusb_wait_for_event(libusb_context *ctx, struct timeval *tv) 303{ 304 struct timespec ts; 305 int err; 306 307 ctx = GET_CONTEXT(ctx); 308 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_wait_for_event enter"); 309 310 if (tv == NULL) { 311 pthread_cond_wait(&ctx->ctx_cond, 312 &ctx->ctx_lock); 313 return (0); 314 } 315 err = clock_gettime(CLOCK_MONOTONIC, &ts); 316 if (err < 0) 317 return (LIBUSB_ERROR_OTHER); 318 319 /* 320 * The "tv" arguments points to a relative time structure and 321 * not an absolute time structure. 322 */ 323 ts.tv_sec += tv->tv_sec; 324 ts.tv_nsec += tv->tv_usec * 1000; 325 if (ts.tv_nsec >= 1000000000) { 326 ts.tv_nsec -= 1000000000; 327 ts.tv_sec++; 328 } 329 err = pthread_cond_timedwait(&ctx->ctx_cond, 330 &ctx->ctx_lock, &ts); 331 332 if (err == ETIMEDOUT) 333 return (1); 334 335 return (0); 336} 337 338int 339libusb_handle_events_timeout_completed(libusb_context *ctx, 340 struct timeval *tv, int *completed) 341{ 342 int err = 0; 343 344 ctx = GET_CONTEXT(ctx); 345 346 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed enter"); 347 348 libusb_lock_events(ctx); 349 350 while (1) { 351 if (completed != NULL) { 352 if (*completed != 0 || err != 0) 353 break; 354 } 355 err = libusb_handle_events_locked(ctx, tv); 356 if (completed == NULL) 357 break; 358 } 359 360 libusb_unlock_events(ctx); 361 362 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed exit"); 363 364 return (err); 365} 366 367int 368libusb_handle_events_completed(libusb_context *ctx, int *completed) 369{ 370 return (libusb_handle_events_timeout_completed(ctx, NULL, completed)); 371} 372 373int 374libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv) 375{ 376 return (libusb_handle_events_timeout_completed(ctx, tv, NULL)); 377} 378 379int 380libusb_handle_events(libusb_context *ctx) 381{ 382 return (libusb_handle_events_timeout_completed(ctx, NULL, NULL)); 383} 384 385int 386libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv) 387{ 388 int err; 389 390 ctx = GET_CONTEXT(ctx); 391 392 if (libusb_event_handling_ok(ctx)) { 393 err = libusb10_handle_events_sub(ctx, tv); 394 } else { 395 err = libusb_wait_for_event(ctx, tv); 396 if (err != 0) 397 err = LIBUSB_ERROR_TIMEOUT; 398 } 399 return (err); 400} 401 402int 403libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv) 404{ 405 /* all timeouts are currently being done by the kernel */ 406 timerclear(tv); 407 return (0); 408} 409 410void 411libusb_set_pollfd_notifiers(libusb_context *ctx, 412 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb, 413 void *user_data) 414{ 415 ctx = GET_CONTEXT(ctx); 416 417 ctx->fd_added_cb = added_cb; 418 ctx->fd_removed_cb = removed_cb; 419 ctx->fd_cb_user_data = user_data; 420} 421 422const struct libusb_pollfd ** 423libusb_get_pollfds(libusb_context *ctx) 424{ 425 struct libusb_super_pollfd *pollfd; 426 libusb_pollfd **ret; 427 int i; 428 429 ctx = GET_CONTEXT(ctx); 430 431 CTX_LOCK(ctx); 432 433 i = 0; 434 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry) 435 i++; 436 437 ret = calloc(i + 1, sizeof(struct libusb_pollfd *)); 438 if (ret == NULL) 439 goto done; 440 441 i = 0; 442 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry) 443 ret[i++] = &pollfd->pollfd; 444 ret[i] = NULL; 445 446done: 447 CTX_UNLOCK(ctx); 448 return ((const struct libusb_pollfd **)ret); 449} 450 451 452/* Synchronous device I/O */ 453 454int 455libusb_control_transfer(libusb_device_handle *devh, 456 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, 457 uint8_t *data, uint16_t wLength, unsigned int timeout) 458{ 459 struct LIBUSB20_CONTROL_SETUP_DECODED req; 460 int err; 461 uint16_t actlen; 462 463 if (devh == NULL) 464 return (LIBUSB_ERROR_INVALID_PARAM); 465 466 if ((wLength != 0) && (data == NULL)) 467 return (LIBUSB_ERROR_INVALID_PARAM); 468 469 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req); 470 471 req.bmRequestType = bmRequestType; 472 req.bRequest = bRequest; 473 req.wValue = wValue; 474 req.wIndex = wIndex; 475 req.wLength = wLength; 476 477 err = libusb20_dev_request_sync(devh, &req, data, 478 &actlen, timeout, 0); 479 480 if (err == LIBUSB20_ERROR_PIPE) 481 return (LIBUSB_ERROR_PIPE); 482 else if (err == LIBUSB20_ERROR_TIMEOUT) 483 return (LIBUSB_ERROR_TIMEOUT); 484 else if (err) 485 return (LIBUSB_ERROR_NO_DEVICE); 486 487 return (actlen); 488} 489 490static void 491libusb10_do_transfer_cb(struct libusb_transfer *transfer) 492{ 493 libusb_context *ctx; 494 int *pdone; 495 496 ctx = GET_CONTEXT(NULL); 497 498 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "sync I/O done"); 499 500 pdone = transfer->user_data; 501 *pdone = 1; 502} 503 504/* 505 * TODO: Replace the following function. Allocating and freeing on a 506 * per-transfer basis is slow. --HPS 507 */ 508static int 509libusb10_do_transfer(libusb_device_handle *devh, 510 uint8_t endpoint, uint8_t *data, int length, 511 int *transferred, unsigned int timeout, int type) 512{ 513 libusb_context *ctx; 514 struct libusb_transfer *xfer; 515 int done; 516 int ret; 517 518 if (devh == NULL) 519 return (LIBUSB_ERROR_INVALID_PARAM); 520 521 if ((length != 0) && (data == NULL)) 522 return (LIBUSB_ERROR_INVALID_PARAM); 523 524 xfer = libusb_alloc_transfer(0); 525 if (xfer == NULL) 526 return (LIBUSB_ERROR_NO_MEM); 527 528 ctx = libusb_get_device(devh)->ctx; 529 530 xfer->dev_handle = devh; 531 xfer->endpoint = endpoint; 532 xfer->type = type; 533 xfer->timeout = timeout; 534 xfer->buffer = data; 535 xfer->length = length; 536 xfer->user_data = (void *)&done; 537 xfer->callback = libusb10_do_transfer_cb; 538 done = 0; 539 540 if ((ret = libusb_submit_transfer(xfer)) < 0) { 541 libusb_free_transfer(xfer); 542 return (ret); 543 } 544 while (done == 0) { 545 if ((ret = libusb_handle_events(ctx)) < 0) { 546 libusb_cancel_transfer(xfer); 547 usleep(1000); /* nice it */ 548 } 549 } 550 551 *transferred = xfer->actual_length; 552 553 switch (xfer->status) { 554 case LIBUSB_TRANSFER_COMPLETED: 555 ret = 0; 556 break; 557 case LIBUSB_TRANSFER_TIMED_OUT: 558 ret = LIBUSB_ERROR_TIMEOUT; 559 break; 560 case LIBUSB_TRANSFER_OVERFLOW: 561 ret = LIBUSB_ERROR_OVERFLOW; 562 break; 563 case LIBUSB_TRANSFER_STALL: 564 ret = LIBUSB_ERROR_PIPE; 565 break; 566 case LIBUSB_TRANSFER_NO_DEVICE: 567 ret = LIBUSB_ERROR_NO_DEVICE; 568 break; 569 default: 570 ret = LIBUSB_ERROR_OTHER; 571 break; 572 } 573 574 libusb_free_transfer(xfer); 575 return (ret); 576} 577 578int 579libusb_bulk_transfer(libusb_device_handle *devh, 580 uint8_t endpoint, uint8_t *data, int length, 581 int *transferred, unsigned int timeout) 582{ 583 libusb_context *ctx; 584 int ret; 585 586 ctx = GET_CONTEXT(NULL); 587 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter"); 588 589 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred, 590 timeout, LIBUSB_TRANSFER_TYPE_BULK); 591 592 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave"); 593 return (ret); 594} 595 596int 597libusb_interrupt_transfer(libusb_device_handle *devh, 598 uint8_t endpoint, uint8_t *data, int length, 599 int *transferred, unsigned int timeout) 600{ 601 libusb_context *ctx; 602 int ret; 603 604 ctx = GET_CONTEXT(NULL); 605 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter"); 606 607 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred, 608 timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT); 609 610 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave"); 611 return (ret); 612} 613 614uint8_t * 615libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t off) 616{ 617 uint8_t *ptr; 618 uint32_t n; 619 620 if (transfer->num_iso_packets < 0) 621 return (NULL); 622 623 if (off >= (uint32_t)transfer->num_iso_packets) 624 return (NULL); 625 626 ptr = transfer->buffer; 627 if (ptr == NULL) 628 return (NULL); 629 630 for (n = 0; n != off; n++) { 631 ptr += transfer->iso_packet_desc[n].length; 632 } 633 return (ptr); 634} 635 636uint8_t * 637libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t off) 638{ 639 uint8_t *ptr; 640 641 if (transfer->num_iso_packets < 0) 642 return (NULL); 643 644 if (off >= (uint32_t)transfer->num_iso_packets) 645 return (NULL); 646 647 ptr = transfer->buffer; 648 if (ptr == NULL) 649 return (NULL); 650 651 ptr += transfer->iso_packet_desc[0].length * off; 652 653 return (ptr); 654} 655 656void 657libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length) 658{ 659 int n; 660 661 if (transfer->num_iso_packets < 0) 662 return; 663 664 for (n = 0; n != transfer->num_iso_packets; n++) 665 transfer->iso_packet_desc[n].length = length; 666} 667 668uint8_t * 669libusb_control_transfer_get_data(struct libusb_transfer *transfer) 670{ 671 if (transfer->buffer == NULL) 672 return (NULL); 673 674 return (transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE); 675} 676 677struct libusb_control_setup * 678libusb_control_transfer_get_setup(struct libusb_transfer *transfer) 679{ 680 return ((struct libusb_control_setup *)transfer->buffer); 681} 682 683void 684libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType, 685 uint8_t bRequest, uint16_t wValue, 686 uint16_t wIndex, uint16_t wLength) 687{ 688 struct libusb_control_setup *req = (struct libusb_control_setup *)buf; 689 690 /* The alignment is OK for all fields below. */ 691 req->bmRequestType = bmRequestType; 692 req->bRequest = bRequest; 693 req->wValue = htole16(wValue); 694 req->wIndex = htole16(wIndex); 695 req->wLength = htole16(wLength); 696} 697 698void 699libusb_fill_control_transfer(struct libusb_transfer *transfer, 700 libusb_device_handle *devh, uint8_t *buf, 701 libusb_transfer_cb_fn callback, void *user_data, 702 uint32_t timeout) 703{ 704 struct libusb_control_setup *setup = (struct libusb_control_setup *)buf; 705 706 transfer->dev_handle = devh; 707 transfer->endpoint = 0; 708 transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL; 709 transfer->timeout = timeout; 710 transfer->buffer = buf; 711 if (setup != NULL) 712 transfer->length = LIBUSB_CONTROL_SETUP_SIZE 713 + le16toh(setup->wLength); 714 else 715 transfer->length = 0; 716 transfer->user_data = user_data; 717 transfer->callback = callback; 718 719} 720 721void 722libusb_fill_bulk_transfer(struct libusb_transfer *transfer, 723 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf, 724 int length, libusb_transfer_cb_fn callback, void *user_data, 725 uint32_t timeout) 726{ 727 transfer->dev_handle = devh; 728 transfer->endpoint = endpoint; 729 transfer->type = LIBUSB_TRANSFER_TYPE_BULK; 730 transfer->timeout = timeout; 731 transfer->buffer = buf; 732 transfer->length = length; 733 transfer->user_data = user_data; 734 transfer->callback = callback; 735} 736 737void 738libusb_fill_interrupt_transfer(struct libusb_transfer *transfer, 739 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf, 740 int length, libusb_transfer_cb_fn callback, void *user_data, 741 uint32_t timeout) 742{ 743 transfer->dev_handle = devh; 744 transfer->endpoint = endpoint; 745 transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT; 746 transfer->timeout = timeout; 747 transfer->buffer = buf; 748 transfer->length = length; 749 transfer->user_data = user_data; 750 transfer->callback = callback; 751} 752 753void 754libusb_fill_iso_transfer(struct libusb_transfer *transfer, 755 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf, 756 int length, int npacket, libusb_transfer_cb_fn callback, 757 void *user_data, uint32_t timeout) 758{ 759 transfer->dev_handle = devh; 760 transfer->endpoint = endpoint; 761 transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS; 762 transfer->timeout = timeout; 763 transfer->buffer = buf; 764 transfer->length = length; 765 transfer->num_iso_packets = npacket; 766 transfer->user_data = user_data; 767 transfer->callback = callback; 768} 769 770int 771libusb_alloc_streams(libusb_device_handle *dev, uint32_t num_streams, 772 unsigned char *endpoints, int num_endpoints) 773{ 774 if (num_streams > 1) 775 return (LIBUSB_ERROR_INVALID_PARAM); 776 return (0); 777} 778 779int 780libusb_free_streams(libusb_device_handle *dev, unsigned char *endpoints, int num_endpoints) 781{ 782 783 return (0); 784} 785 786void 787libusb_transfer_set_stream_id(struct libusb_transfer *transfer, uint32_t stream_id) 788{ 789 struct libusb_super_transfer *sxfer; 790 791 if (transfer == NULL) 792 return; 793 794 sxfer = (struct libusb_super_transfer *)( 795 ((uint8_t *)transfer) - sizeof(*sxfer)); 796 797 /* set stream ID */ 798 sxfer->stream_id = stream_id; 799} 800 801uint32_t 802libusb_transfer_get_stream_id(struct libusb_transfer *transfer) 803{ 804 struct libusb_super_transfer *sxfer; 805 806 if (transfer == NULL) 807 return (0); 808 809 sxfer = (struct libusb_super_transfer *)( 810 ((uint8_t *)transfer) - sizeof(*sxfer)); 811 812 /* get stream ID */ 813 return (sxfer->stream_id); 814} 815