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