]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - lib/libusb/libusb10_io.c
MFC r338679:
[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 libusb_context *
486 libusb10_get_context_by_device_handle(libusb_device_handle *devh)
487 {
488         libusb_context *ctx;
489
490         if (devh != NULL)
491                 ctx = libusb_get_device(devh)->ctx;
492         else
493                 ctx = NULL;
494
495         return (GET_CONTEXT(ctx));
496 }
497
498 static void
499 libusb10_do_transfer_cb(struct libusb_transfer *transfer)
500 {
501         libusb_context *ctx;
502         int *pdone;
503
504         ctx = libusb10_get_context_by_device_handle(transfer->dev_handle);
505
506         DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "sync I/O done");
507
508         pdone = transfer->user_data;
509         *pdone = 1;
510 }
511
512 /*
513  * TODO: Replace the following function. Allocating and freeing on a
514  * per-transfer basis is slow.  --HPS
515  */
516 static int
517 libusb10_do_transfer(libusb_device_handle *devh,
518     uint8_t endpoint, uint8_t *data, int length,
519     int *transferred, unsigned int timeout, int type)
520 {
521         libusb_context *ctx;
522         struct libusb_transfer *xfer;
523         int done;
524         int ret;
525
526         if (devh == NULL)
527                 return (LIBUSB_ERROR_INVALID_PARAM);
528
529         if ((length != 0) && (data == NULL))
530                 return (LIBUSB_ERROR_INVALID_PARAM);
531
532         xfer = libusb_alloc_transfer(0);
533         if (xfer == NULL)
534                 return (LIBUSB_ERROR_NO_MEM);
535
536         ctx = libusb_get_device(devh)->ctx;
537
538         xfer->dev_handle = devh;
539         xfer->endpoint = endpoint;
540         xfer->type = type;
541         xfer->timeout = timeout;
542         xfer->buffer = data;
543         xfer->length = length;
544         xfer->user_data = (void *)&done;
545         xfer->callback = libusb10_do_transfer_cb;
546         done = 0;
547
548         if ((ret = libusb_submit_transfer(xfer)) < 0) {
549                 libusb_free_transfer(xfer);
550                 return (ret);
551         }
552         while (done == 0) {
553                 if ((ret = libusb_handle_events(ctx)) < 0) {
554                         libusb_cancel_transfer(xfer);
555                         usleep(1000);   /* nice it */
556                 }
557         }
558
559         *transferred = xfer->actual_length;
560
561         switch (xfer->status) {
562         case LIBUSB_TRANSFER_COMPLETED:
563                 ret = 0;
564                 break;
565         case LIBUSB_TRANSFER_TIMED_OUT:
566                 ret = LIBUSB_ERROR_TIMEOUT;
567                 break;
568         case LIBUSB_TRANSFER_OVERFLOW:
569                 ret = LIBUSB_ERROR_OVERFLOW;
570                 break;
571         case LIBUSB_TRANSFER_STALL:
572                 ret = LIBUSB_ERROR_PIPE;
573                 break;
574         case LIBUSB_TRANSFER_NO_DEVICE:
575                 ret = LIBUSB_ERROR_NO_DEVICE;
576                 break;
577         default:
578                 ret = LIBUSB_ERROR_OTHER;
579                 break;
580         }
581
582         libusb_free_transfer(xfer);
583         return (ret);
584 }
585
586 int
587 libusb_bulk_transfer(libusb_device_handle *devh,
588     uint8_t endpoint, uint8_t *data, int length,
589     int *transferred, unsigned int timeout)
590 {
591         libusb_context *ctx;
592         int ret;
593
594         ctx = libusb10_get_context_by_device_handle(devh);
595
596         DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter");
597
598         ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
599             timeout, LIBUSB_TRANSFER_TYPE_BULK);
600
601         DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave");
602         return (ret);
603 }
604
605 int
606 libusb_interrupt_transfer(libusb_device_handle *devh,
607     uint8_t endpoint, uint8_t *data, int length,
608     int *transferred, unsigned int timeout)
609 {
610         libusb_context *ctx;
611         int ret;
612
613         ctx = libusb10_get_context_by_device_handle(devh);
614
615         DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter");
616
617         ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
618             timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
619
620         DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave");
621         return (ret);
622 }
623
624 uint8_t *
625 libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t off)
626 {
627         uint8_t *ptr;
628         uint32_t n;
629
630         if (transfer->num_iso_packets < 0)
631                 return (NULL);
632
633         if (off >= (uint32_t)transfer->num_iso_packets)
634                 return (NULL);
635
636         ptr = transfer->buffer;
637         if (ptr == NULL)
638                 return (NULL);
639
640         for (n = 0; n != off; n++) {
641                 ptr += transfer->iso_packet_desc[n].length;
642         }
643         return (ptr);
644 }
645
646 uint8_t *
647 libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t off)
648 {
649         uint8_t *ptr;
650
651         if (transfer->num_iso_packets < 0)
652                 return (NULL);
653
654         if (off >= (uint32_t)transfer->num_iso_packets)
655                 return (NULL);
656
657         ptr = transfer->buffer;
658         if (ptr == NULL)
659                 return (NULL);
660
661         ptr += transfer->iso_packet_desc[0].length * off;
662
663         return (ptr);
664 }
665
666 void
667 libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length)
668 {
669         int n;
670
671         if (transfer->num_iso_packets < 0)
672                 return;
673
674         for (n = 0; n != transfer->num_iso_packets; n++)
675                 transfer->iso_packet_desc[n].length = length;
676 }
677
678 uint8_t *
679 libusb_control_transfer_get_data(struct libusb_transfer *transfer)
680 {
681         if (transfer->buffer == NULL)
682                 return (NULL);
683
684         return (transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE);
685 }
686
687 struct libusb_control_setup *
688 libusb_control_transfer_get_setup(struct libusb_transfer *transfer)
689 {
690         return ((struct libusb_control_setup *)transfer->buffer);
691 }
692
693 void
694 libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType,
695     uint8_t bRequest, uint16_t wValue,
696     uint16_t wIndex, uint16_t wLength)
697 {
698         struct libusb_control_setup *req = (struct libusb_control_setup *)buf;
699
700         /* The alignment is OK for all fields below. */
701         req->bmRequestType = bmRequestType;
702         req->bRequest = bRequest;
703         req->wValue = htole16(wValue);
704         req->wIndex = htole16(wIndex);
705         req->wLength = htole16(wLength);
706 }
707
708 void
709 libusb_fill_control_transfer(struct libusb_transfer *transfer, 
710     libusb_device_handle *devh, uint8_t *buf,
711     libusb_transfer_cb_fn callback, void *user_data,
712     uint32_t timeout)
713 {
714         struct libusb_control_setup *setup = (struct libusb_control_setup *)buf;
715
716         transfer->dev_handle = devh;
717         transfer->endpoint = 0;
718         transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
719         transfer->timeout = timeout;
720         transfer->buffer = buf;
721         if (setup != NULL)
722                 transfer->length = LIBUSB_CONTROL_SETUP_SIZE
723                         + le16toh(setup->wLength);
724         else
725                 transfer->length = 0;
726         transfer->user_data = user_data;
727         transfer->callback = callback;
728
729 }
730
731 void
732 libusb_fill_bulk_transfer(struct libusb_transfer *transfer, 
733     libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf, 
734     int length, libusb_transfer_cb_fn callback, void *user_data,
735     uint32_t timeout)
736 {
737         transfer->dev_handle = devh;
738         transfer->endpoint = endpoint;
739         transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
740         transfer->timeout = timeout;
741         transfer->buffer = buf;
742         transfer->length = length;
743         transfer->user_data = user_data;
744         transfer->callback = callback;
745 }
746
747 void
748 libusb_fill_interrupt_transfer(struct libusb_transfer *transfer,
749     libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
750     int length, libusb_transfer_cb_fn callback, void *user_data,
751     uint32_t timeout)
752 {
753         transfer->dev_handle = devh;
754         transfer->endpoint = endpoint;
755         transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
756         transfer->timeout = timeout;
757         transfer->buffer = buf;
758         transfer->length = length;
759         transfer->user_data = user_data;
760         transfer->callback = callback;
761 }
762
763 void
764 libusb_fill_iso_transfer(struct libusb_transfer *transfer, 
765     libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
766     int length, int npacket, libusb_transfer_cb_fn callback,
767     void *user_data, uint32_t timeout)
768 {
769         transfer->dev_handle = devh;
770         transfer->endpoint = endpoint;
771         transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
772         transfer->timeout = timeout;
773         transfer->buffer = buf;
774         transfer->length = length;
775         transfer->num_iso_packets = npacket;
776         transfer->user_data = user_data;
777         transfer->callback = callback;
778 }
779
780 int
781 libusb_alloc_streams(libusb_device_handle *dev, uint32_t num_streams,
782     unsigned char *endpoints, int num_endpoints)
783 {
784
785         return (LIBUSB_ERROR_NOT_SUPPORTED);
786 }
787
788 int
789 libusb_free_streams(libusb_device_handle *dev, unsigned char *endpoints, int num_endpoints)
790 {
791
792         return (0);
793 }
794
795 void
796 libusb_transfer_set_stream_id(struct libusb_transfer *transfer, uint32_t stream_id)
797 {
798         struct libusb_super_transfer *sxfer;
799
800         if (transfer == NULL)
801                 return;
802
803         sxfer = (struct libusb_super_transfer *)(
804             ((uint8_t *)transfer) - sizeof(*sxfer));
805
806         /* set stream ID */
807         sxfer->stream_id = stream_id;
808 }
809
810 uint32_t
811 libusb_transfer_get_stream_id(struct libusb_transfer *transfer)
812 {
813         struct libusb_super_transfer *sxfer;
814
815         if (transfer == NULL)
816                 return (0);
817
818         sxfer = (struct libusb_super_transfer *)(
819             ((uint8_t *)transfer) - sizeof(*sxfer));
820
821         /* get stream ID */
822         return (sxfer->stream_id);
823 }