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