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
45UNEXPORTED void
46libusb10_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
70UNEXPORTED void
71libusb10_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
90static int
91libusb10_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
185do_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
226int
227libusb_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
248void
249libusb_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
257void
258libusb_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
268int
269libusb_event_handling_ok(libusb_context *ctx)
270{
271	ctx = GET_CONTEXT(ctx);
272	return (ctx->ctx_handler == pthread_self());
273}
274
275int
276libusb_event_handler_active(libusb_context *ctx)
277{
278	ctx = GET_CONTEXT(ctx);
279	return (ctx->ctx_handler != NO_THREAD);
280}
281
282void
283libusb_lock_event_waiters(libusb_context *ctx)
284{
285	ctx = GET_CONTEXT(ctx);
286	CTX_LOCK(ctx);
287}
288
289void
290libusb_unlock_event_waiters(libusb_context *ctx)
291{
292	ctx = GET_CONTEXT(ctx);
293	CTX_UNLOCK(ctx);
294}
295
296int
297libusb_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_MONOTONIC, &ts);
311	if (err < 0)
312		return (LIBUSB_ERROR_OTHER);
313
314	/*
315	 * The "tv" arguments points to a relative time structure and
316	 * not an absolute time structure.
317	 */
318	ts.tv_sec += tv->tv_sec;
319	ts.tv_nsec += tv->tv_usec * 1000;
320	if (ts.tv_nsec >= 1000000000) {
321		ts.tv_nsec -= 1000000000;
322		ts.tv_sec++;
323	}
324	err = pthread_cond_timedwait(&ctx->ctx_cond,
325	    &ctx->ctx_lock, &ts);
326
327	if (err == ETIMEDOUT)
328		return (1);
329
330	return (0);
331}
332
333int
334libusb_handle_events_timeout_completed(libusb_context *ctx,
335    struct timeval *tv, int *completed)
336{
337	int err = 0;
338
339	ctx = GET_CONTEXT(ctx);
340
341	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed enter");
342
343	libusb_lock_events(ctx);
344
345	while (1) {
346		if (completed != NULL) {
347			if (*completed != 0 || err != 0)
348				break;
349		}
350		err = libusb_handle_events_locked(ctx, tv);
351		if (completed == NULL)
352			break;
353	}
354
355	libusb_unlock_events(ctx);
356
357	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed exit");
358
359	return (err);
360}
361
362int
363libusb_handle_events_completed(libusb_context *ctx, int *completed)
364{
365	return (libusb_handle_events_timeout_completed(ctx, NULL, completed));
366}
367
368int
369libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv)
370{
371	return (libusb_handle_events_timeout_completed(ctx, tv, NULL));
372}
373
374int
375libusb_handle_events(libusb_context *ctx)
376{
377	return (libusb_handle_events_timeout_completed(ctx, NULL, NULL));
378}
379
380int
381libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv)
382{
383	int err;
384
385	ctx = GET_CONTEXT(ctx);
386
387	if (libusb_event_handling_ok(ctx)) {
388		err = libusb10_handle_events_sub(ctx, tv);
389	} else {
390		err = libusb_wait_for_event(ctx, tv);
391		if (err != 0)
392			err = LIBUSB_ERROR_TIMEOUT;
393	}
394	return (err);
395}
396
397int
398libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv)
399{
400	/* all timeouts are currently being done by the kernel */
401	timerclear(tv);
402	return (0);
403}
404
405void
406libusb_set_pollfd_notifiers(libusb_context *ctx,
407    libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
408    void *user_data)
409{
410	ctx = GET_CONTEXT(ctx);
411
412	ctx->fd_added_cb = added_cb;
413	ctx->fd_removed_cb = removed_cb;
414	ctx->fd_cb_user_data = user_data;
415}
416
417const struct libusb_pollfd **
418libusb_get_pollfds(libusb_context *ctx)
419{
420	struct libusb_super_pollfd *pollfd;
421	libusb_pollfd **ret;
422	int i;
423
424	ctx = GET_CONTEXT(ctx);
425
426	CTX_LOCK(ctx);
427
428	i = 0;
429	TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
430	    i++;
431
432	ret = calloc(i + 1, sizeof(struct libusb_pollfd *));
433	if (ret == NULL)
434		goto done;
435
436	i = 0;
437	TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
438	    ret[i++] = &pollfd->pollfd;
439	ret[i] = NULL;
440
441done:
442	CTX_UNLOCK(ctx);
443	return ((const struct libusb_pollfd **)ret);
444}
445
446
447/* Synchronous device I/O */
448
449int
450libusb_control_transfer(libusb_device_handle *devh,
451    uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
452    uint8_t *data, uint16_t wLength, unsigned int timeout)
453{
454	struct LIBUSB20_CONTROL_SETUP_DECODED req;
455	int err;
456	uint16_t actlen;
457
458	if (devh == NULL)
459		return (LIBUSB_ERROR_INVALID_PARAM);
460
461	if ((wLength != 0) && (data == NULL))
462		return (LIBUSB_ERROR_INVALID_PARAM);
463
464	LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
465
466	req.bmRequestType = bmRequestType;
467	req.bRequest = bRequest;
468	req.wValue = wValue;
469	req.wIndex = wIndex;
470	req.wLength = wLength;
471
472	err = libusb20_dev_request_sync(devh, &req, data,
473	    &actlen, timeout, 0);
474
475	if (err == LIBUSB20_ERROR_PIPE)
476		return (LIBUSB_ERROR_PIPE);
477	else if (err == LIBUSB20_ERROR_TIMEOUT)
478		return (LIBUSB_ERROR_TIMEOUT);
479	else if (err)
480		return (LIBUSB_ERROR_NO_DEVICE);
481
482	return (actlen);
483}
484
485static void
486libusb10_do_transfer_cb(struct libusb_transfer *transfer)
487{
488	libusb_context *ctx;
489	int *pdone;
490
491	ctx = GET_CONTEXT(NULL);
492
493	DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "sync I/O done");
494
495	pdone = transfer->user_data;
496	*pdone = 1;
497}
498
499/*
500 * TODO: Replace the following function. Allocating and freeing on a
501 * per-transfer basis is slow.  --HPS
502 */
503static int
504libusb10_do_transfer(libusb_device_handle *devh,
505    uint8_t endpoint, uint8_t *data, int length,
506    int *transferred, unsigned int timeout, int type)
507{
508	libusb_context *ctx;
509	struct libusb_transfer *xfer;
510	int done;
511	int ret;
512
513	if (devh == NULL)
514		return (LIBUSB_ERROR_INVALID_PARAM);
515
516	if ((length != 0) && (data == NULL))
517		return (LIBUSB_ERROR_INVALID_PARAM);
518
519	xfer = libusb_alloc_transfer(0);
520	if (xfer == NULL)
521		return (LIBUSB_ERROR_NO_MEM);
522
523	ctx = libusb_get_device(devh)->ctx;
524
525	xfer->dev_handle = devh;
526	xfer->endpoint = endpoint;
527	xfer->type = type;
528	xfer->timeout = timeout;
529	xfer->buffer = data;
530	xfer->length = length;
531	xfer->user_data = (void *)&done;
532	xfer->callback = libusb10_do_transfer_cb;
533	done = 0;
534
535	if ((ret = libusb_submit_transfer(xfer)) < 0) {
536		libusb_free_transfer(xfer);
537		return (ret);
538	}
539	while (done == 0) {
540		if ((ret = libusb_handle_events(ctx)) < 0) {
541			libusb_cancel_transfer(xfer);
542			usleep(1000);	/* nice it */
543		}
544	}
545
546	*transferred = xfer->actual_length;
547
548	switch (xfer->status) {
549	case LIBUSB_TRANSFER_COMPLETED:
550		ret = 0;
551		break;
552	case LIBUSB_TRANSFER_TIMED_OUT:
553		ret = LIBUSB_ERROR_TIMEOUT;
554		break;
555	case LIBUSB_TRANSFER_OVERFLOW:
556		ret = LIBUSB_ERROR_OVERFLOW;
557		break;
558	case LIBUSB_TRANSFER_STALL:
559		ret = LIBUSB_ERROR_PIPE;
560		break;
561	case LIBUSB_TRANSFER_NO_DEVICE:
562		ret = LIBUSB_ERROR_NO_DEVICE;
563		break;
564	default:
565		ret = LIBUSB_ERROR_OTHER;
566		break;
567	}
568
569	libusb_free_transfer(xfer);
570	return (ret);
571}
572
573int
574libusb_bulk_transfer(libusb_device_handle *devh,
575    uint8_t endpoint, uint8_t *data, int length,
576    int *transferred, unsigned int timeout)
577{
578	libusb_context *ctx;
579	int ret;
580
581	ctx = GET_CONTEXT(NULL);
582	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter");
583
584	ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
585	    timeout, LIBUSB_TRANSFER_TYPE_BULK);
586
587	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave");
588	return (ret);
589}
590
591int
592libusb_interrupt_transfer(libusb_device_handle *devh,
593    uint8_t endpoint, uint8_t *data, int length,
594    int *transferred, unsigned int timeout)
595{
596	libusb_context *ctx;
597	int ret;
598
599	ctx = GET_CONTEXT(NULL);
600	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter");
601
602	ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
603	    timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
604
605	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave");
606	return (ret);
607}
608
609uint8_t *
610libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t off)
611{
612	uint8_t *ptr;
613	uint32_t n;
614
615	if (transfer->num_iso_packets < 0)
616		return (NULL);
617
618	if (off >= (uint32_t)transfer->num_iso_packets)
619		return (NULL);
620
621	ptr = transfer->buffer;
622	if (ptr == NULL)
623		return (NULL);
624
625	for (n = 0; n != off; n++) {
626		ptr += transfer->iso_packet_desc[n].length;
627	}
628	return (ptr);
629}
630
631uint8_t *
632libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t off)
633{
634	uint8_t *ptr;
635
636	if (transfer->num_iso_packets < 0)
637		return (NULL);
638
639	if (off >= (uint32_t)transfer->num_iso_packets)
640		return (NULL);
641
642	ptr = transfer->buffer;
643	if (ptr == NULL)
644		return (NULL);
645
646	ptr += transfer->iso_packet_desc[0].length * off;
647
648	return (ptr);
649}
650
651void
652libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length)
653{
654	int n;
655
656	if (transfer->num_iso_packets < 0)
657		return;
658
659	for (n = 0; n != transfer->num_iso_packets; n++)
660		transfer->iso_packet_desc[n].length = length;
661}
662
663uint8_t *
664libusb_control_transfer_get_data(struct libusb_transfer *transfer)
665{
666	if (transfer->buffer == NULL)
667		return (NULL);
668
669	return (transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE);
670}
671
672struct libusb_control_setup *
673libusb_control_transfer_get_setup(struct libusb_transfer *transfer)
674{
675	return ((struct libusb_control_setup *)transfer->buffer);
676}
677
678void
679libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType,
680    uint8_t bRequest, uint16_t wValue,
681    uint16_t wIndex, uint16_t wLength)
682{
683	struct libusb_control_setup *req = (struct libusb_control_setup *)buf;
684
685	/* The alignment is OK for all fields below. */
686	req->bmRequestType = bmRequestType;
687	req->bRequest = bRequest;
688	req->wValue = htole16(wValue);
689	req->wIndex = htole16(wIndex);
690	req->wLength = htole16(wLength);
691}
692
693void
694libusb_fill_control_transfer(struct libusb_transfer *transfer,
695    libusb_device_handle *devh, uint8_t *buf,
696    libusb_transfer_cb_fn callback, void *user_data,
697    uint32_t timeout)
698{
699	struct libusb_control_setup *setup = (struct libusb_control_setup *)buf;
700
701	transfer->dev_handle = devh;
702	transfer->endpoint = 0;
703	transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
704	transfer->timeout = timeout;
705	transfer->buffer = buf;
706	if (setup != NULL)
707		transfer->length = LIBUSB_CONTROL_SETUP_SIZE
708			+ le16toh(setup->wLength);
709	else
710		transfer->length = 0;
711	transfer->user_data = user_data;
712	transfer->callback = callback;
713
714}
715
716void
717libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
718    libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
719    int length, libusb_transfer_cb_fn callback, void *user_data,
720    uint32_t timeout)
721{
722	transfer->dev_handle = devh;
723	transfer->endpoint = endpoint;
724	transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
725	transfer->timeout = timeout;
726	transfer->buffer = buf;
727	transfer->length = length;
728	transfer->user_data = user_data;
729	transfer->callback = callback;
730}
731
732void
733libusb_fill_interrupt_transfer(struct libusb_transfer *transfer,
734    libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
735    int length, libusb_transfer_cb_fn callback, void *user_data,
736    uint32_t timeout)
737{
738	transfer->dev_handle = devh;
739	transfer->endpoint = endpoint;
740	transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
741	transfer->timeout = timeout;
742	transfer->buffer = buf;
743	transfer->length = length;
744	transfer->user_data = user_data;
745	transfer->callback = callback;
746}
747
748void
749libusb_fill_iso_transfer(struct libusb_transfer *transfer,
750    libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
751    int length, int npacket, libusb_transfer_cb_fn callback,
752    void *user_data, uint32_t timeout)
753{
754	transfer->dev_handle = devh;
755	transfer->endpoint = endpoint;
756	transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
757	transfer->timeout = timeout;
758	transfer->buffer = buf;
759	transfer->length = length;
760	transfer->num_iso_packets = npacket;
761	transfer->user_data = user_data;
762	transfer->callback = callback;
763}
764
765