aboutsummaryrefslogtreecommitdiff
path: root/lib/libusb/libusb10_io.c
blob: 47a1b6833e8e95a5ed1886e48733b0785a72f942 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/* $FreeBSD$ */
/*-
 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/queue.h>

#include <errno.h>
#include <poll.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#include "libusb20.h"
#include "libusb20_desc.h"
#include "libusb20_int.h"
#include "libusb.h"
#include "libusb10.h"

UNEXPORTED void
libusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd,
    struct libusb20_device *pdev, int fd, short events)
{
	if (ctx == NULL)
		return;			/* invalid */

	if (pollfd->entry.tqe_prev != NULL)
		return;			/* already queued */

	if (fd < 0)
		return;			/* invalid */

	pollfd->pdev = pdev;
	pollfd->pollfd.fd = fd;
	pollfd->pollfd.events = events;

	CTX_LOCK(ctx);
	TAILQ_INSERT_TAIL(&ctx->pollfds, pollfd, entry);
	CTX_UNLOCK(ctx);

	if (ctx->fd_added_cb)
		ctx->fd_added_cb(fd, events, ctx->fd_cb_user_data);
}

UNEXPORTED void
libusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd)
{
	if (ctx == NULL)
		return;			/* invalid */

	if (pollfd->entry.tqe_prev == NULL)
		return;			/* already dequeued */

	CTX_LOCK(ctx);
	TAILQ_REMOVE(&ctx->pollfds, pollfd, entry);
	pollfd->entry.tqe_prev = NULL;
	CTX_UNLOCK(ctx);

	if (ctx->fd_removed_cb)
		ctx->fd_removed_cb(pollfd->pollfd.fd, ctx->fd_cb_user_data);
}

/* This function must be called locked */

static int
libusb10_handle_events_sub(struct libusb_context *ctx, struct timeval *tv)
{
	struct libusb_device *dev;
	struct libusb20_device **ppdev;
	struct libusb_super_pollfd *pfd;
	struct pollfd *fds;
	struct libusb_super_transfer *sxfer;
	struct libusb_transfer *uxfer;
	nfds_t nfds;
	int timeout;
	int i;
	int err;

	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb10_handle_events_sub enter");

	nfds = 0;
	i = 0;
	TAILQ_FOREACH(pfd, &ctx->pollfds, entry)
	    nfds++;

	fds = alloca(sizeof(*fds) * nfds);
	if (fds == NULL)
		return (LIBUSB_ERROR_NO_MEM);

	ppdev = alloca(sizeof(*ppdev) * nfds);
	if (ppdev == NULL)
		return (LIBUSB_ERROR_NO_MEM);

	TAILQ_FOREACH(pfd, &ctx->pollfds, entry) {
		fds[i].fd = pfd->pollfd.fd;
		fds[i].events = pfd->pollfd.events;
		fds[i].revents = 0;
		ppdev[i] = pfd->pdev;
		if (pfd->pdev != NULL)
			libusb_get_device(pfd->pdev)->refcnt++;
		i++;
	}

	if (tv == NULL)
		timeout = -1;
	else
		timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 999) / 1000);

	CTX_UNLOCK(ctx);
	err = poll(fds, nfds, timeout);
	CTX_LOCK(ctx);

	if ((err == -1) && (errno == EINTR))
		err = LIBUSB_ERROR_INTERRUPTED;
	else if (err < 0)
		err = LIBUSB_ERROR_IO;

	if (err < 1) {
		for (i = 0; i != nfds; i++) {
			if (ppdev[i] != NULL) {
				CTX_UNLOCK(ctx);
				libusb_unref_device(libusb_get_device(ppdev[i]));
				CTX_LOCK(ctx);
			}
		}
		goto do_done;
	}
	for (i = 0; i != nfds; i++) {
		if (ppdev[i] != NULL) {
			dev = libusb_get_device(ppdev[i]);

			if (fds[i].revents == 0)
				err = 0;	/* nothing to do */
			else
				err = libusb20_dev_process(ppdev[i]);

			if (err) {
				/* cancel all transfers - device is gone */
				libusb10_cancel_all_transfer(dev);

				/* remove USB device from polling loop */
				libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
			}
			CTX_UNLOCK(ctx);
			libusb_unref_device(dev);
			CTX_LOCK(ctx);

		} else {
			uint8_t dummy;

			while (1) {
				if (read(fds[i].fd, &dummy, 1) != 1)
					break;
			}
		}
	}

	err = 0;

do_done:

	/* Do all done callbacks */

	while ((sxfer = TAILQ_FIRST(&ctx->tr_done))) {
		TAILQ_REMOVE(&ctx->tr_done, sxfer, entry);
		sxfer->entry.tqe_prev = NULL;

		ctx->tr_done_ref++;

		CTX_UNLOCK(ctx);

		uxfer = (struct libusb_transfer *)(
		    ((uint8_t *)sxfer) + sizeof(*sxfer));

		if (uxfer->callback != NULL)
			(uxfer->callback) (uxfer);

		if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
			free(uxfer->buffer);

		if (uxfer->flags & LIBUSB_TRANSFER_FREE_TRANSFER)
			libusb_free_transfer(uxfer);

		CTX_LOCK(ctx);

		ctx->tr_done_ref--;
		ctx->tr_done_gen++;
	}

	/* Wakeup other waiters */
	pthread_cond_broadcast(&ctx->ctx_cond);

	return (err);
}

/* Polling and timing */

int
libusb_try_lock_events(libusb_context *ctx)
{
	int err;

	ctx = GET_CONTEXT(ctx);
	if (ctx == NULL)
		return (1);

	err = CTX_TRYLOCK(ctx);
	if (err)
		return (1);

	err = (ctx->ctx_handler != NO_THREAD);
	if (err)
		CTX_UNLOCK(ctx);
	else
		ctx->ctx_handler = pthread_self();

	return (err);
}

void
libusb_lock_events(libusb_context *ctx)
{
	ctx = GET_CONTEXT(ctx);
	CTX_LOCK(ctx);
	if (ctx->ctx_handler == NO_THREAD)
		ctx->ctx_handler = pthread_self();
}

void
libusb_unlock_events(libusb_context *ctx)
{
	ctx = GET_CONTEXT(ctx);
	if (ctx->ctx_handler == pthread_self()) {
		ctx->ctx_handler = NO_THREAD;
		pthread_cond_broadcast(&ctx->ctx_cond);
	}
	CTX_UNLOCK(ctx);
}

int
libusb_event_handling_ok(libusb_context *ctx)
{
	ctx = GET_CONTEXT(ctx);
	return (ctx->ctx_handler == pthread_self());
}

int
libusb_event_handler_active(libusb_context *ctx)
{
	ctx = GET_CONTEXT(ctx);
	return (ctx->ctx_handler != NO_THREAD);
}

void
libusb_lock_event_waiters(libusb_context *ctx)
{
	ctx = GET_CONTEXT(ctx);
	CTX_LOCK(ctx);
}

void
libusb_unlock_event_waiters(libusb_context *ctx)
{
	ctx = GET_CONTEXT(ctx);
	CTX_UNLOCK(ctx);
}

int
libusb_wait_for_event(libusb_context *ctx, struct timeval *tv)
{
	struct timespec ts;
	int err;

	ctx = GET_CONTEXT(ctx);
	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_wait_for_event enter");

	if (tv == NULL) {
		pthread_cond_wait(&ctx->ctx_cond,
		    &ctx->ctx_lock);
		return (0);
	}
	err = clock_gettime(CLOCK_REALTIME, &ts);
	if (err < 0)
		return (LIBUSB_ERROR_OTHER);

	ts.tv_sec = tv->tv_sec;
	ts.tv_nsec = tv->tv_usec * 1000;
	if (ts.tv_nsec >= 1000000000) {
		ts.tv_nsec -= 1000000000;
		ts.tv_sec++;
	}
	err = pthread_cond_timedwait(&ctx->ctx_cond,
	    &ctx->ctx_lock, &ts);

	if (err == ETIMEDOUT)
		return (1);

	return (0);
}

int
libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv)
{
	int err;

	ctx = GET_CONTEXT(ctx);

	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout enter");

	libusb_lock_events(ctx);

	err = libusb_handle_events_locked(ctx, tv);

	libusb_unlock_events(ctx);

	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout leave");

	return (err);
}

int
libusb_handle_events(libusb_context *ctx)
{
	return (libusb_handle_events_timeout(ctx, NULL));
}

int
libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv)
{
	int err;

	ctx = GET_CONTEXT(ctx);

	if (libusb_event_handling_ok(ctx)) {
		err = libusb10_handle_events_sub(ctx, tv);
	} else {
		libusb_wait_for_event(ctx, tv);
		err = 0;
	}
	return (err);
}

int
libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv)
{
	/* all timeouts are currently being done by the kernel */
	timerclear(tv);
	return (0);
}

void
libusb_set_pollfd_notifiers(libusb_context *ctx,
    libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
    void *user_data)
{
	ctx = GET_CONTEXT(ctx);

	ctx->fd_added_cb = added_cb;
	ctx->fd_removed_cb = removed_cb;
	ctx->fd_cb_user_data = user_data;
}

struct libusb_pollfd **
libusb_get_pollfds(libusb_context *ctx)
{
	struct libusb_super_pollfd *pollfd;
	libusb_pollfd **ret;
	int i;

	ctx = GET_CONTEXT(ctx);

	CTX_LOCK(ctx);

	i = 0;
	TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
	    i++;

	ret = calloc(i + 1, sizeof(struct libusb_pollfd *));
	if (ret == NULL)
		goto done;

	i = 0;
	TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
	    ret[i++] = &pollfd->pollfd;
	ret[i] = NULL;

done:
	CTX_UNLOCK(ctx);
	return (ret);
}


/* Synchronous device I/O */

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)
{
	struct LIBUSB20_CONTROL_SETUP_DECODED req;
	int err;
	uint16_t actlen;

	if (devh == NULL)
		return (LIBUSB_ERROR_INVALID_PARAM);

	if ((wLength != 0) && (data == NULL))
		return (LIBUSB_ERROR_INVALID_PARAM);

	LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);

	req.bmRequestType = bmRequestType;
	req.bRequest = bRequest;
	req.wValue = wValue;
	req.wIndex = wIndex;
	req.wLength = wLength;

	err = libusb20_dev_request_sync(devh, &req, data,
	    &actlen, timeout, 0);

	if (err == LIBUSB20_ERROR_PIPE)
		return (LIBUSB_ERROR_PIPE);
	else if (err == LIBUSB20_ERROR_TIMEOUT)
		return (LIBUSB_ERROR_TIMEOUT);
	else if (err)
		return (LIBUSB_ERROR_NO_DEVICE);

	return (actlen);
}

static void
libusb10_do_transfer_cb(struct libusb_transfer *transfer)
{
	libusb_context *ctx;
	int *pdone;

	ctx = GET_CONTEXT(NULL);

	DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "sync I/O done");

	pdone = transfer->user_data;
	*pdone = 1;
}

/*
 * TODO: Replace the following function. Allocating and freeing on a
 * per-transfer basis is slow.  --HPS
 */
static int
libusb10_do_transfer(libusb_device_handle *devh,
    uint8_t endpoint, uint8_t *data, int length,
    int *transferred, unsigned int timeout, int type)
{
	libusb_context *ctx;
	struct libusb_transfer *xfer;
	volatile int complet;
	int ret;

	if (devh == NULL)
		return (LIBUSB_ERROR_INVALID_PARAM);

	if ((length != 0) && (data == NULL))
		return (LIBUSB_ERROR_INVALID_PARAM);

	xfer = libusb_alloc_transfer(0);
	if (xfer == NULL)
		return (LIBUSB_ERROR_NO_MEM);

	ctx = libusb_get_device(devh)->ctx;

	xfer->dev_handle = devh;
	xfer->endpoint = endpoint;
	xfer->type = type;
	xfer->timeout = timeout;
	xfer->buffer = data;
	xfer->length = length;
	xfer->user_data = (void *)&complet;
	xfer->callback = libusb10_do_transfer_cb;
	complet = 0;

	if ((ret = libusb_submit_transfer(xfer)) < 0) {
		libusb_free_transfer(xfer);
		return (ret);
	}
	while (complet == 0) {
		if ((ret = libusb_handle_events(ctx)) < 0) {
			libusb_cancel_transfer(xfer);
			usleep(1000);	/* nice it */
		}
	}

	*transferred = xfer->actual_length;

	switch (xfer->status) {
	case LIBUSB_TRANSFER_COMPLETED:
		ret = 0;
		break;
	case LIBUSB_TRANSFER_TIMED_OUT:
		ret = LIBUSB_ERROR_TIMEOUT;
		break;
	case LIBUSB_TRANSFER_OVERFLOW:
		ret = LIBUSB_ERROR_OVERFLOW;
		break;
	case LIBUSB_TRANSFER_STALL:
		ret = LIBUSB_ERROR_PIPE;
		break;
	case LIBUSB_TRANSFER_NO_DEVICE:
		ret = LIBUSB_ERROR_NO_DEVICE;
		break;
	default:
		ret = LIBUSB_ERROR_OTHER;
		break;
	}

	libusb_free_transfer(xfer);
	return (ret);
}

int
libusb_bulk_transfer(libusb_device_handle *devh,
    uint8_t endpoint, uint8_t *data, int length,
    int *transferred, unsigned int timeout)
{
	libusb_context *ctx;
	int ret;

	ctx = GET_CONTEXT(NULL);
	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter");

	ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
	    timeout, LIBUSB_TRANSFER_TYPE_BULK);

	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave");
	return (ret);
}

int
libusb_interrupt_transfer(libusb_device_handle *devh,
    uint8_t endpoint, uint8_t *data, int length,
    int *transferred, unsigned int timeout)
{
	libusb_context *ctx;
	int ret;

	ctx = GET_CONTEXT(NULL);
	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter");

	ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
	    timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);

	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave");
	return (ret);
}

uint8_t *
libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t index)
{
	uint8_t *ptr;
	uint32_t n;

	if (transfer->num_iso_packets < 0)
		return (NULL);

	if (index >= (uint32_t)transfer->num_iso_packets)
		return (NULL);

	ptr = transfer->buffer;
	if (ptr == NULL)
		return (NULL);

	for (n = 0; n != index; n++) {
		ptr += transfer->iso_packet_desc[n].length;
	}
	return (ptr);
}

uint8_t *
libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t index)
{
	uint8_t *ptr;

	if (transfer->num_iso_packets < 0)
		return (NULL);

	if (index >= (uint32_t)transfer->num_iso_packets)
		return (NULL);

	ptr = transfer->buffer;
	if (ptr == NULL)
		return (NULL);

	ptr += transfer->iso_packet_desc[0].length * index;

	return (ptr);
}

void
libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length)
{
	int n;

	if (transfer->num_iso_packets < 0)
		return;

	for (n = 0; n != transfer->num_iso_packets; n++)
		transfer->iso_packet_desc[n].length = length;
}

uint8_t *
libusb_control_transfer_get_data(struct libusb_transfer *transfer)
{
	if (transfer->buffer == NULL)
		return (NULL);

	return (transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE);
}

struct libusb_control_setup *
libusb_control_transfer_get_setup(struct libusb_transfer *transfer)
{
	return ((struct libusb_control_setup *)transfer->buffer);
}

void
libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType,
    uint8_t bRequest, uint16_t wValue,
    uint16_t wIndex, uint16_t wLength)
{
	struct libusb_control_setup *req = (struct libusb_control_setup *)buf;

	/* The alignment is OK for all fields below. */
	req->bmRequestType = bmRequestType;
	req->bRequest = bRequest;
	req->wValue = htole16(wValue);
	req->wIndex = htole16(wIndex);
	req->wLength = htole16(wLength);
}

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)
{
	struct libusb_control_setup *setup = (struct libusb_control_setup *)buf;

	transfer->dev_handle = devh;
	transfer->endpoint = 0;
	transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
	transfer->timeout = timeout;
	transfer->buffer = buf;
	if (setup != NULL)
		transfer->length = LIBUSB_CONTROL_SETUP_SIZE
			+ le16toh(setup->wLength);
	else
		transfer->length = 0;
	transfer->user_data = user_data;
	transfer->callback = callback;

}

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)
{
	transfer->dev_handle = devh;
	transfer->endpoint = endpoint;
	transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
	transfer->timeout = timeout;
	transfer->buffer = buf;
	transfer->length = length;
	transfer->user_data = user_data;
	transfer->callback = callback;
}

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)
{
	transfer->dev_handle = devh;
	transfer->endpoint = endpoint;
	transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
	transfer->timeout = timeout;
	transfer->buffer = buf;
	transfer->length = length;
	transfer->user_data = user_data;
	transfer->callback = callback;
}

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)
{
	transfer->dev_handle = devh;
	transfer->endpoint = endpoint;
	transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
	transfer->timeout = timeout;
	transfer->buffer = buf;
	transfer->length = length;
	transfer->num_iso_packets = npacket;
	transfer->user_data = user_data;
	transfer->callback = callback;
}