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