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