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