xref: /freebsd/lib/libusb/libusb10_io.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
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 					/*
169 					 * When the device is opened
170 					 * set the "device_is_gone"
171 					 * flag. This prevents the
172 					 * client from submitting new
173 					 * USB transfers to a detached
174 					 * device.
175 					 */
176 					if (ppdev[i]->is_opened)
177 						dev->device_is_gone = 1;
178 
179 					/* remove USB device from polling loop */
180 					libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
181 
182 					/* cancel all pending transfers */
183 					libusb10_cancel_all_transfer_locked(ppdev[i], dev);
184 				}
185 			}
186 			CTX_UNLOCK(ctx);
187 			libusb_unref_device(dev);
188 			CTX_LOCK(ctx);
189 
190 		} else {
191 			uint8_t dummy;
192 
193 			while (read(fds[i].fd, &dummy, 1) == 1)
194 				;
195 		}
196 	}
197 
198 	err = 0;
199 
200 do_done:
201 
202 	/* Do all done callbacks */
203 
204 	while ((sxfer = TAILQ_FIRST(&ctx->tr_done))) {
205 		uint8_t flags;
206 
207 		TAILQ_REMOVE(&ctx->tr_done, sxfer, entry);
208 		sxfer->entry.tqe_prev = NULL;
209 
210 		ctx->tr_done_ref++;
211 
212 		CTX_UNLOCK(ctx);
213 
214 		uxfer = (struct libusb_transfer *)(
215 		    ((uint8_t *)sxfer) + sizeof(*sxfer));
216 
217 		/* Allow the callback to free the transfer itself. */
218 		flags = uxfer->flags;
219 
220 		if (uxfer->callback != NULL)
221 			(uxfer->callback) (uxfer);
222 
223 		/* Check if the USB transfer should be automatically freed. */
224 		if (flags & LIBUSB_TRANSFER_FREE_TRANSFER)
225 			libusb_free_transfer(uxfer);
226 
227 		CTX_LOCK(ctx);
228 
229 		ctx->tr_done_ref--;
230 		ctx->tr_done_gen++;
231 	}
232 
233 	/* Wakeup other waiters */
234 	pthread_cond_broadcast(&ctx->ctx_cond);
235 
236 	return (err);
237 }
238 
239 /* Polling and timing */
240 
241 int
242 libusb_try_lock_events(libusb_context *ctx)
243 {
244 	int err;
245 
246 	ctx = GET_CONTEXT(ctx);
247 	if (ctx == NULL)
248 		return (1);
249 
250 	err = CTX_TRYLOCK(ctx);
251 	if (err)
252 		return (1);
253 
254 	err = (ctx->ctx_handler != NO_THREAD);
255 	if (err)
256 		CTX_UNLOCK(ctx);
257 	else
258 		ctx->ctx_handler = pthread_self();
259 
260 	return (err);
261 }
262 
263 void
264 libusb_lock_events(libusb_context *ctx)
265 {
266 	ctx = GET_CONTEXT(ctx);
267 	CTX_LOCK(ctx);
268 	if (ctx->ctx_handler == NO_THREAD)
269 		ctx->ctx_handler = pthread_self();
270 }
271 
272 void
273 libusb_unlock_events(libusb_context *ctx)
274 {
275 	ctx = GET_CONTEXT(ctx);
276 	if (ctx->ctx_handler == pthread_self()) {
277 		ctx->ctx_handler = NO_THREAD;
278 		pthread_cond_broadcast(&ctx->ctx_cond);
279 	}
280 	CTX_UNLOCK(ctx);
281 }
282 
283 int
284 libusb_event_handling_ok(libusb_context *ctx)
285 {
286 	ctx = GET_CONTEXT(ctx);
287 	return (ctx->ctx_handler == pthread_self());
288 }
289 
290 int
291 libusb_event_handler_active(libusb_context *ctx)
292 {
293 	ctx = GET_CONTEXT(ctx);
294 	return (ctx->ctx_handler != NO_THREAD);
295 }
296 
297 void
298 libusb_lock_event_waiters(libusb_context *ctx)
299 {
300 	ctx = GET_CONTEXT(ctx);
301 	CTX_LOCK(ctx);
302 }
303 
304 void
305 libusb_unlock_event_waiters(libusb_context *ctx)
306 {
307 	ctx = GET_CONTEXT(ctx);
308 	CTX_UNLOCK(ctx);
309 }
310 
311 int
312 libusb_wait_for_event(libusb_context *ctx, struct timeval *tv)
313 {
314 	struct timespec ts;
315 	int err;
316 
317 	ctx = GET_CONTEXT(ctx);
318 	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_wait_for_event enter");
319 
320 	if (tv == NULL) {
321 		pthread_cond_wait(&ctx->ctx_cond,
322 		    &ctx->ctx_lock);
323 		/* try to grab polling of actual events, if any */
324 		if (ctx->ctx_handler == NO_THREAD)
325 			ctx->ctx_handler = pthread_self();
326 		return (0);
327 	}
328 	err = clock_gettime(CLOCK_MONOTONIC, &ts);
329 	if (err < 0)
330 		return (LIBUSB_ERROR_OTHER);
331 
332 	/*
333 	 * The "tv" arguments points to a relative time structure and
334 	 * not an absolute time structure.
335 	 */
336 	ts.tv_sec += tv->tv_sec;
337 	ts.tv_nsec += tv->tv_usec * 1000;
338 	if (ts.tv_nsec >= 1000000000) {
339 		ts.tv_nsec -= 1000000000;
340 		ts.tv_sec++;
341 	}
342 	err = pthread_cond_timedwait(&ctx->ctx_cond,
343 	    &ctx->ctx_lock, &ts);
344 	/* try to grab polling of actual events, if any */
345 	if (ctx->ctx_handler == NO_THREAD)
346 		ctx->ctx_handler = pthread_self();
347 
348 	if (err == ETIMEDOUT)
349 		return (1);
350 
351 	return (0);
352 }
353 
354 int
355 libusb_handle_events_timeout_completed(libusb_context *ctx,
356     struct timeval *tv, int *completed)
357 {
358 	int err = 0;
359 
360 	ctx = GET_CONTEXT(ctx);
361 
362 	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed enter");
363 
364 	libusb_lock_events(ctx);
365 
366 	while (1) {
367 		if (completed != NULL) {
368 			if (*completed != 0 || err != 0)
369 				break;
370 		}
371 		err = libusb_handle_events_locked(ctx, tv);
372 		if (completed == NULL)
373 			break;
374 	}
375 
376 	libusb_unlock_events(ctx);
377 
378 	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed exit");
379 
380 	return (err);
381 }
382 
383 int
384 libusb_handle_events_completed(libusb_context *ctx, int *completed)
385 {
386 	return (libusb_handle_events_timeout_completed(ctx, NULL, completed));
387 }
388 
389 int
390 libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv)
391 {
392 	return (libusb_handle_events_timeout_completed(ctx, tv, NULL));
393 }
394 
395 int
396 libusb_handle_events(libusb_context *ctx)
397 {
398 	return (libusb_handle_events_timeout_completed(ctx, NULL, NULL));
399 }
400 
401 int
402 libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv)
403 {
404 	int err;
405 
406 	ctx = GET_CONTEXT(ctx);
407 
408 	if (libusb_event_handling_ok(ctx)) {
409 		err = libusb10_handle_events_sub(ctx, tv);
410 	} else {
411 		err = libusb_wait_for_event(ctx, tv);
412 		if (err != 0)
413 			err = LIBUSB_ERROR_TIMEOUT;
414 	}
415 	return (err);
416 }
417 
418 int
419 libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv)
420 {
421 	/* all timeouts are currently being done by the kernel */
422 	timerclear(tv);
423 	return (0);
424 }
425 
426 void
427 libusb_set_pollfd_notifiers(libusb_context *ctx,
428     libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
429     void *user_data)
430 {
431 	ctx = GET_CONTEXT(ctx);
432 
433 	ctx->fd_added_cb = added_cb;
434 	ctx->fd_removed_cb = removed_cb;
435 	ctx->fd_cb_user_data = user_data;
436 }
437 
438 const struct libusb_pollfd **
439 libusb_get_pollfds(libusb_context *ctx)
440 {
441 	struct libusb_super_pollfd *pollfd;
442 	libusb_pollfd **ret;
443 	int i;
444 
445 	ctx = GET_CONTEXT(ctx);
446 
447 	CTX_LOCK(ctx);
448 
449 	i = 0;
450 	TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
451 	    i++;
452 
453 	ret = calloc(i + 1, sizeof(struct libusb_pollfd *));
454 	if (ret == NULL)
455 		goto done;
456 
457 	i = 0;
458 	TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
459 	    ret[i++] = &pollfd->pollfd;
460 	ret[i] = NULL;
461 
462 done:
463 	CTX_UNLOCK(ctx);
464 	return ((const struct libusb_pollfd **)ret);
465 }
466 
467 
468 /* Synchronous device I/O */
469 
470 int
471 libusb_control_transfer(libusb_device_handle *devh,
472     uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
473     uint8_t *data, uint16_t wLength, unsigned int timeout)
474 {
475 	struct LIBUSB20_CONTROL_SETUP_DECODED req;
476 	int err;
477 	uint16_t actlen;
478 
479 	if (devh == NULL)
480 		return (LIBUSB_ERROR_INVALID_PARAM);
481 
482 	if ((wLength != 0) && (data == NULL))
483 		return (LIBUSB_ERROR_INVALID_PARAM);
484 
485 	LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
486 
487 	req.bmRequestType = bmRequestType;
488 	req.bRequest = bRequest;
489 	req.wValue = wValue;
490 	req.wIndex = wIndex;
491 	req.wLength = wLength;
492 
493 	err = libusb20_dev_request_sync(devh, &req, data,
494 	    &actlen, timeout, 0);
495 
496 	if (err == LIBUSB20_ERROR_PIPE)
497 		return (LIBUSB_ERROR_PIPE);
498 	else if (err == LIBUSB20_ERROR_TIMEOUT)
499 		return (LIBUSB_ERROR_TIMEOUT);
500 	else if (err)
501 		return (LIBUSB_ERROR_NO_DEVICE);
502 
503 	return (actlen);
504 }
505 
506 static libusb_context *
507 libusb10_get_context_by_device_handle(libusb_device_handle *devh)
508 {
509 	libusb_context *ctx;
510 
511 	if (devh != NULL)
512 		ctx = libusb_get_device(devh)->ctx;
513 	else
514 		ctx = NULL;
515 
516 	return (GET_CONTEXT(ctx));
517 }
518 
519 static void
520 libusb10_do_transfer_cb(struct libusb_transfer *transfer)
521 {
522 	libusb_context *ctx;
523 	int *pdone;
524 
525 	ctx = libusb10_get_context_by_device_handle(transfer->dev_handle);
526 
527 	DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "sync I/O done");
528 
529 	pdone = transfer->user_data;
530 	*pdone = 1;
531 }
532 
533 /*
534  * TODO: Replace the following function. Allocating and freeing on a
535  * per-transfer basis is slow.  --HPS
536  */
537 static int
538 libusb10_do_transfer(libusb_device_handle *devh,
539     uint8_t endpoint, uint8_t *data, int length,
540     int *transferred, unsigned int timeout, int type)
541 {
542 	libusb_context *ctx;
543 	struct libusb_transfer *xfer;
544 	int done;
545 	int ret;
546 
547 	if (devh == NULL)
548 		return (LIBUSB_ERROR_INVALID_PARAM);
549 
550 	if ((length != 0) && (data == NULL))
551 		return (LIBUSB_ERROR_INVALID_PARAM);
552 
553 	xfer = libusb_alloc_transfer(0);
554 	if (xfer == NULL)
555 		return (LIBUSB_ERROR_NO_MEM);
556 
557 	ctx = libusb_get_device(devh)->ctx;
558 
559 	xfer->dev_handle = devh;
560 	xfer->endpoint = endpoint;
561 	xfer->type = type;
562 	xfer->timeout = timeout;
563 	xfer->buffer = data;
564 	xfer->length = length;
565 	xfer->user_data = (void *)&done;
566 	xfer->callback = libusb10_do_transfer_cb;
567 	done = 0;
568 
569 	if ((ret = libusb_submit_transfer(xfer)) < 0) {
570 		libusb_free_transfer(xfer);
571 		return (ret);
572 	}
573 	while (done == 0) {
574 		if ((ret = libusb_handle_events(ctx)) < 0) {
575 			libusb_cancel_transfer(xfer);
576 			usleep(1000);	/* nice it */
577 		}
578 	}
579 
580 	*transferred = xfer->actual_length;
581 
582 	switch (xfer->status) {
583 	case LIBUSB_TRANSFER_COMPLETED:
584 		ret = 0;
585 		break;
586 	case LIBUSB_TRANSFER_TIMED_OUT:
587 		ret = LIBUSB_ERROR_TIMEOUT;
588 		break;
589 	case LIBUSB_TRANSFER_OVERFLOW:
590 		ret = LIBUSB_ERROR_OVERFLOW;
591 		break;
592 	case LIBUSB_TRANSFER_STALL:
593 		ret = LIBUSB_ERROR_PIPE;
594 		break;
595 	case LIBUSB_TRANSFER_NO_DEVICE:
596 		ret = LIBUSB_ERROR_NO_DEVICE;
597 		break;
598 	default:
599 		ret = LIBUSB_ERROR_OTHER;
600 		break;
601 	}
602 
603 	libusb_free_transfer(xfer);
604 	return (ret);
605 }
606 
607 int
608 libusb_bulk_transfer(libusb_device_handle *devh,
609     uint8_t endpoint, uint8_t *data, int length,
610     int *transferred, unsigned int timeout)
611 {
612 	libusb_context *ctx;
613 	int ret;
614 
615 	ctx = libusb10_get_context_by_device_handle(devh);
616 
617 	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter");
618 
619 	ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
620 	    timeout, LIBUSB_TRANSFER_TYPE_BULK);
621 
622 	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave");
623 	return (ret);
624 }
625 
626 int
627 libusb_interrupt_transfer(libusb_device_handle *devh,
628     uint8_t endpoint, uint8_t *data, int length,
629     int *transferred, unsigned int timeout)
630 {
631 	libusb_context *ctx;
632 	int ret;
633 
634 	ctx = libusb10_get_context_by_device_handle(devh);
635 
636 	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter");
637 
638 	ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
639 	    timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
640 
641 	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave");
642 	return (ret);
643 }
644 
645 uint8_t *
646 libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t off)
647 {
648 	uint8_t *ptr;
649 	uint32_t n;
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 	for (n = 0; n != off; n++) {
662 		ptr += transfer->iso_packet_desc[n].length;
663 	}
664 	return (ptr);
665 }
666 
667 uint8_t *
668 libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t off)
669 {
670 	uint8_t *ptr;
671 
672 	if (transfer->num_iso_packets < 0)
673 		return (NULL);
674 
675 	if (off >= (uint32_t)transfer->num_iso_packets)
676 		return (NULL);
677 
678 	ptr = transfer->buffer;
679 	if (ptr == NULL)
680 		return (NULL);
681 
682 	ptr += transfer->iso_packet_desc[0].length * off;
683 
684 	return (ptr);
685 }
686 
687 void
688 libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length)
689 {
690 	int n;
691 
692 	if (transfer->num_iso_packets < 0)
693 		return;
694 
695 	for (n = 0; n != transfer->num_iso_packets; n++)
696 		transfer->iso_packet_desc[n].length = length;
697 }
698 
699 uint8_t *
700 libusb_control_transfer_get_data(struct libusb_transfer *transfer)
701 {
702 	if (transfer->buffer == NULL)
703 		return (NULL);
704 
705 	return (transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE);
706 }
707 
708 struct libusb_control_setup *
709 libusb_control_transfer_get_setup(struct libusb_transfer *transfer)
710 {
711 	return ((struct libusb_control_setup *)transfer->buffer);
712 }
713 
714 void
715 libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType,
716     uint8_t bRequest, uint16_t wValue,
717     uint16_t wIndex, uint16_t wLength)
718 {
719 	struct libusb_control_setup *req = (struct libusb_control_setup *)buf;
720 
721 	/* The alignment is OK for all fields below. */
722 	req->bmRequestType = bmRequestType;
723 	req->bRequest = bRequest;
724 	req->wValue = htole16(wValue);
725 	req->wIndex = htole16(wIndex);
726 	req->wLength = htole16(wLength);
727 }
728 
729 void
730 libusb_fill_control_transfer(struct libusb_transfer *transfer,
731     libusb_device_handle *devh, uint8_t *buf,
732     libusb_transfer_cb_fn callback, void *user_data,
733     uint32_t timeout)
734 {
735 	struct libusb_control_setup *setup = (struct libusb_control_setup *)buf;
736 
737 	transfer->dev_handle = devh;
738 	transfer->endpoint = 0;
739 	transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
740 	transfer->timeout = timeout;
741 	transfer->buffer = buf;
742 	if (setup != NULL)
743 		transfer->length = LIBUSB_CONTROL_SETUP_SIZE
744 			+ le16toh(setup->wLength);
745 	else
746 		transfer->length = 0;
747 	transfer->user_data = user_data;
748 	transfer->callback = callback;
749 
750 }
751 
752 void
753 libusb_fill_bulk_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_BULK;
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_interrupt_transfer(struct libusb_transfer *transfer,
770     libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
771     int length, libusb_transfer_cb_fn callback, void *user_data,
772     uint32_t timeout)
773 {
774 	transfer->dev_handle = devh;
775 	transfer->endpoint = endpoint;
776 	transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
777 	transfer->timeout = timeout;
778 	transfer->buffer = buf;
779 	transfer->length = length;
780 	transfer->user_data = user_data;
781 	transfer->callback = callback;
782 }
783 
784 void
785 libusb_fill_iso_transfer(struct libusb_transfer *transfer,
786     libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
787     int length, int npacket, libusb_transfer_cb_fn callback,
788     void *user_data, uint32_t timeout)
789 {
790 	transfer->dev_handle = devh;
791 	transfer->endpoint = endpoint;
792 	transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
793 	transfer->timeout = timeout;
794 	transfer->buffer = buf;
795 	transfer->length = length;
796 	transfer->num_iso_packets = npacket;
797 	transfer->user_data = user_data;
798 	transfer->callback = callback;
799 }
800 
801 int
802 libusb_alloc_streams(libusb_device_handle *dev, uint32_t num_streams,
803     unsigned char *endpoints, int num_endpoints)
804 {
805 	if (num_streams > 1)
806 		return (LIBUSB_ERROR_INVALID_PARAM);
807 	return (0);
808 }
809 
810 int
811 libusb_free_streams(libusb_device_handle *dev, unsigned char *endpoints, int num_endpoints)
812 {
813 
814 	return (0);
815 }
816 
817 void
818 libusb_transfer_set_stream_id(struct libusb_transfer *transfer, uint32_t stream_id)
819 {
820 	struct libusb_super_transfer *sxfer;
821 
822 	if (transfer == NULL)
823 		return;
824 
825 	sxfer = (struct libusb_super_transfer *)(
826 	    ((uint8_t *)transfer) - sizeof(*sxfer));
827 
828 	/* set stream ID */
829 	sxfer->stream_id = stream_id;
830 }
831 
832 uint32_t
833 libusb_transfer_get_stream_id(struct libusb_transfer *transfer)
834 {
835 	struct libusb_super_transfer *sxfer;
836 
837 	if (transfer == NULL)
838 		return (0);
839 
840 	sxfer = (struct libusb_super_transfer *)(
841 	    ((uint8_t *)transfer) - sizeof(*sxfer));
842 
843 	/* get stream ID */
844 	return (sxfer->stream_id);
845 }
846