]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - lib/libusb/libusb10_io.c
MFC r338616:
[FreeBSD/stable/9.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 = libusb20_dev_process(ppdev[i]);
159
160                                 if (err) {
161                                         /* set device is gone */
162                                         dev->device_is_gone = 1;
163
164                                         /* remove USB device from polling loop */
165                                         libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
166
167                                         /* cancel all pending transfers */
168                                         libusb10_cancel_all_transfer_locked(ppdev[i], dev);
169                                 }
170                         }
171                         CTX_UNLOCK(ctx);
172                         libusb_unref_device(dev);
173                         CTX_LOCK(ctx);
174
175                 } else {
176                         uint8_t dummy;
177
178                         while (read(fds[i].fd, &dummy, 1) == 1)
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_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
333 int
334 libusb_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
362 int
363 libusb_handle_events_completed(libusb_context *ctx, int *completed)
364 {
365         return (libusb_handle_events_timeout_completed(ctx, NULL, completed));
366 }
367
368 int
369 libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv)
370 {
371         return (libusb_handle_events_timeout_completed(ctx, tv, NULL));
372 }
373
374 int
375 libusb_handle_events(libusb_context *ctx)
376 {
377         return (libusb_handle_events_timeout_completed(ctx, NULL, NULL));
378 }
379
380 int
381 libusb_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
397 int
398 libusb_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
405 void
406 libusb_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
417 const struct libusb_pollfd **
418 libusb_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
441 done:
442         CTX_UNLOCK(ctx);
443         return ((const struct libusb_pollfd **)ret);
444 }
445
446
447 /* Synchronous device I/O */
448
449 int
450 libusb_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
485 static void
486 libusb10_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  */
503 static int
504 libusb10_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
573 int
574 libusb_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
591 int
592 libusb_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
609 uint8_t *
610 libusb_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
631 uint8_t *
632 libusb_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
651 void
652 libusb_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
663 uint8_t *
664 libusb_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
672 struct libusb_control_setup *
673 libusb_control_transfer_get_setup(struct libusb_transfer *transfer)
674 {
675         return ((struct libusb_control_setup *)transfer->buffer);
676 }
677
678 void
679 libusb_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
693 void
694 libusb_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
716 void
717 libusb_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
732 void
733 libusb_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
748 void
749 libusb_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 int
766 libusb_alloc_streams(libusb_device_handle *dev, uint32_t num_streams,
767     unsigned char *endpoints, int num_endpoints)
768 {
769
770         return (LIBUSB_ERROR_NOT_SUPPORTED);
771 }
772
773 int
774 libusb_free_streams(libusb_device_handle *dev, unsigned char *endpoints, int num_endpoints)
775 {
776
777         return (0);
778 }
779
780 void
781 libusb_transfer_set_stream_id(struct libusb_transfer *transfer, uint32_t stream_id)
782 {
783         struct libusb_super_transfer *sxfer;
784
785         if (transfer == NULL)
786                 return;
787
788         sxfer = (struct libusb_super_transfer *)(
789             ((uint8_t *)transfer) - sizeof(*sxfer));
790
791         /* set stream ID */
792         sxfer->stream_id = stream_id;
793 }
794
795 uint32_t
796 libusb_transfer_get_stream_id(struct libusb_transfer *transfer)
797 {
798         struct libusb_super_transfer *sxfer;
799
800         if (transfer == NULL)
801                 return (0);
802
803         sxfer = (struct libusb_super_transfer *)(
804             ((uint8_t *)transfer) - sizeof(*sxfer));
805
806         /* get stream ID */
807         return (sxfer->stream_id);
808 }