xref: /freebsd/lib/libusb/libusb10_io.c (revision f5dc2263ab1be8a35a7e27e82103f9ccd41ae584)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifdef LIBUSB_GLOBAL_INCLUDE_FILE
29 #include LIBUSB_GLOBAL_INCLUDE_FILE
30 #else
31 #include <assert.h>
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/eventfd.h>
41 #include <sys/queue.h>
42 #include <sys/endian.h>
43 #endif
44 
45 #define	libusb_device_handle libusb20_device
46 
47 #include "libusb20.h"
48 #include "libusb20_desc.h"
49 #include "libusb20_int.h"
50 #include "libusb.h"
51 #include "libusb10.h"
52 
53 UNEXPORTED void
54 libusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd,
55     struct libusb20_device *pdev, int fd, short events)
56 {
57 	if (ctx == NULL)
58 		return;			/* invalid */
59 
60 	if (pollfd->entry.tqe_prev != NULL)
61 		return;			/* already queued */
62 
63 	if (fd < 0)
64 		return;			/* invalid */
65 
66 	pollfd->pdev = pdev;
67 	pollfd->pollfd.fd = fd;
68 	pollfd->pollfd.events = events;
69 
70 	CTX_LOCK(ctx);
71 	TAILQ_INSERT_TAIL(&ctx->pollfds, pollfd, entry);
72 	CTX_UNLOCK(ctx);
73 
74 	if (ctx->fd_added_cb)
75 		ctx->fd_added_cb(fd, events, ctx->fd_cb_user_data);
76 }
77 
78 UNEXPORTED void
79 libusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd)
80 {
81 	if (ctx == NULL)
82 		return;			/* invalid */
83 
84 	if (pollfd->entry.tqe_prev == NULL)
85 		return;			/* already dequeued */
86 
87 	CTX_LOCK(ctx);
88 	TAILQ_REMOVE(&ctx->pollfds, pollfd, entry);
89 	pollfd->entry.tqe_prev = NULL;
90 	CTX_UNLOCK(ctx);
91 
92 	if (ctx->fd_removed_cb)
93 		ctx->fd_removed_cb(pollfd->pollfd.fd, ctx->fd_cb_user_data);
94 }
95 
96 /* This function must be called locked */
97 
98 static int
99 libusb10_handle_events_sub(struct libusb_context *ctx, struct timeval *tv)
100 {
101 	struct libusb_device *dev;
102 	struct libusb20_device **ppdev;
103 	struct libusb_super_pollfd *pfd;
104 	struct pollfd *fds;
105 	struct libusb_super_transfer *sxfer;
106 	struct libusb_transfer *uxfer;
107 	nfds_t nfds;
108 	int timeout;
109 	int i;
110 	int err;
111 
112 	DPRINTF(ctx, LIBUSB_LOG_LEVEL_DEBUG, "libusb10_handle_events_sub enter");
113 
114 	nfds = 0;
115 	i = 0;
116 	TAILQ_FOREACH(pfd, &ctx->pollfds, entry)
117 	    nfds++;
118 
119 	fds = alloca(sizeof(*fds) * nfds);
120 	if (fds == NULL)
121 		return (LIBUSB_ERROR_NO_MEM);
122 
123 	ppdev = alloca(sizeof(*ppdev) * nfds);
124 	if (ppdev == NULL)
125 		return (LIBUSB_ERROR_NO_MEM);
126 
127 	TAILQ_FOREACH(pfd, &ctx->pollfds, entry) {
128 		fds[i].fd = pfd->pollfd.fd;
129 		fds[i].events = pfd->pollfd.events;
130 		fds[i].revents = 0;
131 		ppdev[i] = pfd->pdev;
132 		if (pfd->pdev != NULL)
133 			libusb_get_device(pfd->pdev)->refcnt++;
134 		i++;
135 	}
136 
137 	if (tv == NULL)
138 		timeout = -1;
139 	else
140 		timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 999) / 1000);
141 
142 	CTX_UNLOCK(ctx);
143 	err = poll(fds, nfds, timeout);
144 	CTX_LOCK(ctx);
145 
146 	if ((err == -1) && (errno == EINTR))
147 		err = LIBUSB_ERROR_INTERRUPTED;
148 	else if (err < 0)
149 		err = LIBUSB_ERROR_IO;
150 
151 	if (err < 1) {
152 		for (i = 0; i != (int)nfds; i++) {
153 			if (ppdev[i] != NULL) {
154 				CTX_UNLOCK(ctx);
155 				libusb_unref_device(libusb_get_device(ppdev[i]));
156 				CTX_LOCK(ctx);
157 			}
158 		}
159 		goto do_done;
160 	}
161 	for (i = 0; i != (int)nfds; i++) {
162 		if (ppdev[i] != NULL) {
163 			dev = libusb_get_device(ppdev[i]);
164 
165 			if (fds[i].revents != 0) {
166 				err = libusb20_dev_process(ppdev[i]);
167 
168 				if (err) {
169 					/*
170 					 * When the device is opened
171 					 * set the "device_is_gone"
172 					 * flag. This prevents the
173 					 * client from submitting new
174 					 * USB transfers to a detached
175 					 * device.
176 					 */
177 					if (ppdev[i]->is_opened)
178 						dev->device_is_gone = 1;
179 
180 					/* remove USB device from polling loop */
181 					libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
182 
183 					/* cancel all pending transfers */
184 					libusb10_cancel_all_transfer_locked(ppdev[i], dev);
185 				}
186 			}
187 			CTX_UNLOCK(ctx);
188 			libusb_unref_device(dev);
189 			CTX_LOCK(ctx);
190 
191 		} else {
192 			eventfd_read(fds[i].fd, &(eventfd_t){0});
193 
194 		}
195 	}
196 
197 	err = 0;
198 
199 do_done:
200 
201 	/* Do all done callbacks */
202 
203 	while ((sxfer = TAILQ_FIRST(&ctx->tr_done))) {
204 		uint8_t flags;
205 
206 		TAILQ_REMOVE(&ctx->tr_done, sxfer, entry);
207 		sxfer->entry.tqe_prev = NULL;
208 
209 		ctx->tr_done_ref++;
210 
211 		CTX_UNLOCK(ctx);
212 
213 		uxfer = (struct libusb_transfer *)(
214 		    ((uint8_t *)sxfer) + sizeof(*sxfer));
215 
216 		/* Allow the callback to free the transfer itself. */
217 		flags = uxfer->flags;
218 
219 		if (uxfer->callback != NULL)
220 			(uxfer->callback) (uxfer);
221 
222 		/* Check if the USB transfer should be automatically freed. */
223 		if (flags & LIBUSB_TRANSFER_FREE_TRANSFER)
224 			libusb_free_transfer(uxfer);
225 
226 		CTX_LOCK(ctx);
227 
228 		ctx->tr_done_ref--;
229 		ctx->tr_done_gen++;
230 	}
231 
232 	/* Wakeup other waiters */
233 	pthread_cond_broadcast(&ctx->ctx_cond);
234 	DPRINTF(ctx, LIBUSB_LOG_LEVEL_DEBUG, "libusb10_handle_events_sub complete");
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_LOG_LEVEL_DEBUG, "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_LOG_LEVEL_DEBUG, "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_LOG_LEVEL_DEBUG, "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 int
427 libusb_pollfds_handle_timeouts(libusb_context *ctx)
428 {
429 	return (1);
430 }
431 
432 void
433 libusb_set_pollfd_notifiers(libusb_context *ctx,
434     libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
435     void *user_data)
436 {
437 	ctx = GET_CONTEXT(ctx);
438 
439 	ctx->fd_added_cb = added_cb;
440 	ctx->fd_removed_cb = removed_cb;
441 	ctx->fd_cb_user_data = user_data;
442 }
443 
444 const struct libusb_pollfd **
445 libusb_get_pollfds(libusb_context *ctx)
446 {
447 	struct libusb_super_pollfd *pollfd;
448 	libusb_pollfd **ret;
449 	int i;
450 
451 	ctx = GET_CONTEXT(ctx);
452 
453 	CTX_LOCK(ctx);
454 
455 	i = 0;
456 	TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
457 	    i++;
458 
459 	ret = calloc(i + 1, sizeof(struct libusb_pollfd *));
460 	if (ret == NULL)
461 		goto done;
462 
463 	i = 0;
464 	TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
465 	    ret[i++] = &pollfd->pollfd;
466 	ret[i] = NULL;
467 
468 done:
469 	CTX_UNLOCK(ctx);
470 	return ((const struct libusb_pollfd **)ret);
471 }
472 
473 
474 /* Synchronous device I/O */
475 
476 static int
477 libusb10_convert_libusb20_error(int err)
478 {
479 	/* LIBUSB20_ERROR_* values are always non-positive */
480 	assert(err <= 0);
481 
482 	switch (err) {
483 	case LIBUSB20_SUCCESS:
484 		return (0);
485 	case LIBUSB20_ERROR_IO:
486 		return (LIBUSB_ERROR_IO);
487 	case LIBUSB20_ERROR_INVALID_PARAM:
488 		return (LIBUSB_ERROR_INVALID_PARAM);
489 	case LIBUSB20_ERROR_ACCESS:
490 		return (LIBUSB_ERROR_ACCESS);
491 	case LIBUSB20_ERROR_NO_DEVICE:
492 		return (LIBUSB_ERROR_NO_DEVICE);
493 	case LIBUSB20_ERROR_NOT_FOUND:
494 		return (LIBUSB_ERROR_NOT_FOUND);
495 	case LIBUSB20_ERROR_BUSY:
496 		return (LIBUSB_ERROR_BUSY);
497 	case LIBUSB20_ERROR_TIMEOUT:
498 		return (LIBUSB_ERROR_TIMEOUT);
499 	case LIBUSB20_ERROR_OVERFLOW:
500 		return (LIBUSB_ERROR_OVERFLOW);
501 	case LIBUSB20_ERROR_PIPE:
502 		return (LIBUSB_ERROR_PIPE);
503 	case LIBUSB20_ERROR_INTERRUPTED:
504 		return (LIBUSB_ERROR_INTERRUPTED);
505 	case LIBUSB20_ERROR_NO_MEM:
506 		return (LIBUSB_ERROR_NO_MEM);
507 	case LIBUSB20_ERROR_NOT_SUPPORTED:
508 		return (LIBUSB_ERROR_NOT_SUPPORTED);
509 	default:
510 		return (LIBUSB_ERROR_OTHER);
511 	}
512 }
513 
514 int
515 libusb_control_transfer(libusb_device_handle *devh,
516     uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
517     uint8_t *data, uint16_t wLength, unsigned int timeout)
518 {
519 	struct LIBUSB20_CONTROL_SETUP_DECODED req;
520 	int err;
521 	uint16_t actlen;
522 
523 	if (devh == NULL)
524 		return (LIBUSB_ERROR_INVALID_PARAM);
525 
526 	if ((wLength != 0) && (data == NULL))
527 		return (LIBUSB_ERROR_INVALID_PARAM);
528 
529 	LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
530 
531 	req.bmRequestType = bmRequestType;
532 	req.bRequest = bRequest;
533 	req.wValue = wValue;
534 	req.wIndex = wIndex;
535 	req.wLength = wLength;
536 
537 	err = libusb20_dev_request_sync(devh, &req, data,
538 	    &actlen, timeout, 0);
539 
540 	if (err)
541 		return (libusb10_convert_libusb20_error(err));
542 
543 	return (actlen);
544 }
545 
546 static libusb_context *
547 libusb10_get_context_by_device_handle(libusb_device_handle *devh)
548 {
549 	libusb_context *ctx;
550 
551 	if (devh != NULL)
552 		ctx = libusb_get_device(devh)->ctx;
553 	else
554 		ctx = NULL;
555 
556 	return (GET_CONTEXT(ctx));
557 }
558 
559 static void
560 libusb10_do_transfer_cb(struct libusb_transfer *transfer)
561 {
562 	libusb_context *ctx;
563 	int *pdone;
564 
565 	ctx = libusb10_get_context_by_device_handle(transfer->dev_handle);
566 
567 	DPRINTF(ctx, LIBUSB_LOG_LEVEL_DEBUG, "sync I/O done");
568 
569 	pdone = transfer->user_data;
570 	*pdone = 1;
571 }
572 
573 /*
574  * TODO: Replace the following function. Allocating and freeing on a
575  * per-transfer basis is slow.  --HPS
576  */
577 static int
578 libusb10_do_transfer(libusb_device_handle *devh,
579     uint8_t endpoint, uint8_t *data, int length,
580     int *transferred, unsigned int timeout, int type)
581 {
582 	libusb_context *ctx;
583 	struct libusb_transfer *xfer;
584 	int done;
585 	int ret;
586 
587 	if (devh == NULL)
588 		return (LIBUSB_ERROR_INVALID_PARAM);
589 
590 	if ((length != 0) && (data == NULL))
591 		return (LIBUSB_ERROR_INVALID_PARAM);
592 
593 	xfer = libusb_alloc_transfer(0);
594 	if (xfer == NULL)
595 		return (LIBUSB_ERROR_NO_MEM);
596 
597 	ctx = libusb_get_device(devh)->ctx;
598 
599 	xfer->dev_handle = devh;
600 	xfer->endpoint = endpoint;
601 	xfer->type = type;
602 	xfer->timeout = timeout;
603 	xfer->buffer = data;
604 	xfer->length = length;
605 	xfer->user_data = (void *)&done;
606 	xfer->callback = libusb10_do_transfer_cb;
607 	done = 0;
608 
609 	if ((ret = libusb_submit_transfer(xfer)) < 0) {
610 		libusb_free_transfer(xfer);
611 		return (ret);
612 	}
613 	while (done == 0) {
614 		if ((ret = libusb_handle_events(ctx)) < 0) {
615 			libusb_cancel_transfer(xfer);
616 			usleep(1000);	/* nice it */
617 		}
618 	}
619 
620 	*transferred = xfer->actual_length;
621 
622 	switch (xfer->status) {
623 	case LIBUSB_TRANSFER_COMPLETED:
624 		ret = 0;
625 		break;
626 	case LIBUSB_TRANSFER_TIMED_OUT:
627 		ret = LIBUSB_ERROR_TIMEOUT;
628 		break;
629 	case LIBUSB_TRANSFER_OVERFLOW:
630 		ret = LIBUSB_ERROR_OVERFLOW;
631 		break;
632 	case LIBUSB_TRANSFER_STALL:
633 		ret = LIBUSB_ERROR_PIPE;
634 		break;
635 	case LIBUSB_TRANSFER_NO_DEVICE:
636 		ret = LIBUSB_ERROR_NO_DEVICE;
637 		break;
638 	default:
639 		ret = LIBUSB_ERROR_OTHER;
640 		break;
641 	}
642 
643 	libusb_free_transfer(xfer);
644 	return (ret);
645 }
646 
647 int
648 libusb_bulk_transfer(libusb_device_handle *devh,
649     uint8_t endpoint, uint8_t *data, int length,
650     int *transferred, unsigned int timeout)
651 {
652 	libusb_context *ctx;
653 	int ret;
654 
655 	ctx = libusb10_get_context_by_device_handle(devh);
656 
657 	DPRINTF(ctx, LIBUSB_LOG_LEVEL_DEBUG, "libusb_bulk_transfer enter");
658 
659 	ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
660 	    timeout, LIBUSB_TRANSFER_TYPE_BULK);
661 
662 	DPRINTF(ctx, LIBUSB_LOG_LEVEL_DEBUG, "libusb_bulk_transfer leave");
663 	return (ret);
664 }
665 
666 int
667 libusb_interrupt_transfer(libusb_device_handle *devh,
668     uint8_t endpoint, uint8_t *data, int length,
669     int *transferred, unsigned int timeout)
670 {
671 	libusb_context *ctx;
672 	int ret;
673 
674 	ctx = libusb10_get_context_by_device_handle(devh);
675 
676 	DPRINTF(ctx, LIBUSB_LOG_LEVEL_DEBUG, "libusb_interrupt_transfer enter");
677 
678 	ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
679 	    timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
680 
681 	DPRINTF(ctx, LIBUSB_LOG_LEVEL_DEBUG, "libusb_interrupt_transfer leave");
682 	return (ret);
683 }
684 
685 uint8_t *
686 libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t off)
687 {
688 	uint8_t *ptr;
689 	uint32_t n;
690 
691 	if (transfer->num_iso_packets < 0)
692 		return (NULL);
693 
694 	if (off >= (uint32_t)transfer->num_iso_packets)
695 		return (NULL);
696 
697 	ptr = transfer->buffer;
698 	if (ptr == NULL)
699 		return (NULL);
700 
701 	for (n = 0; n != off; n++) {
702 		ptr += transfer->iso_packet_desc[n].length;
703 	}
704 	return (ptr);
705 }
706 
707 uint8_t *
708 libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t off)
709 {
710 	uint8_t *ptr;
711 
712 	if (transfer->num_iso_packets < 0)
713 		return (NULL);
714 
715 	if (off >= (uint32_t)transfer->num_iso_packets)
716 		return (NULL);
717 
718 	ptr = transfer->buffer;
719 	if (ptr == NULL)
720 		return (NULL);
721 
722 	ptr += transfer->iso_packet_desc[0].length * off;
723 
724 	return (ptr);
725 }
726 
727 void
728 libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length)
729 {
730 	int n;
731 
732 	if (transfer->num_iso_packets < 0)
733 		return;
734 
735 	for (n = 0; n != transfer->num_iso_packets; n++)
736 		transfer->iso_packet_desc[n].length = length;
737 }
738 
739 uint8_t *
740 libusb_control_transfer_get_data(struct libusb_transfer *transfer)
741 {
742 	if (transfer->buffer == NULL)
743 		return (NULL);
744 
745 	return (transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE);
746 }
747 
748 struct libusb_control_setup *
749 libusb_control_transfer_get_setup(struct libusb_transfer *transfer)
750 {
751 	return ((struct libusb_control_setup *)transfer->buffer);
752 }
753 
754 void
755 libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType,
756     uint8_t bRequest, uint16_t wValue,
757     uint16_t wIndex, uint16_t wLength)
758 {
759 	struct libusb_control_setup *req = (struct libusb_control_setup *)buf;
760 
761 	/* The alignment is OK for all fields below. */
762 	req->bmRequestType = bmRequestType;
763 	req->bRequest = bRequest;
764 	req->wValue = htole16(wValue);
765 	req->wIndex = htole16(wIndex);
766 	req->wLength = htole16(wLength);
767 }
768 
769 void
770 libusb_fill_control_transfer(struct libusb_transfer *transfer,
771     libusb_device_handle *devh, uint8_t *buf,
772     libusb_transfer_cb_fn callback, void *user_data,
773     uint32_t timeout)
774 {
775 	struct libusb_control_setup *setup = (struct libusb_control_setup *)buf;
776 
777 	transfer->dev_handle = devh;
778 	transfer->endpoint = 0;
779 	transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
780 	transfer->timeout = timeout;
781 	transfer->buffer = buf;
782 	if (setup != NULL)
783 		transfer->length = LIBUSB_CONTROL_SETUP_SIZE
784 			+ le16toh(setup->wLength);
785 	else
786 		transfer->length = 0;
787 	transfer->user_data = user_data;
788 	transfer->callback = callback;
789 
790 }
791 
792 void
793 libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
794     libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
795     int length, libusb_transfer_cb_fn callback, void *user_data,
796     uint32_t timeout)
797 {
798 	transfer->dev_handle = devh;
799 	transfer->endpoint = endpoint;
800 	transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
801 	transfer->timeout = timeout;
802 	transfer->buffer = buf;
803 	transfer->length = length;
804 	transfer->user_data = user_data;
805 	transfer->callback = callback;
806 }
807 
808 void
809 libusb_fill_interrupt_transfer(struct libusb_transfer *transfer,
810     libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
811     int length, libusb_transfer_cb_fn callback, void *user_data,
812     uint32_t timeout)
813 {
814 	transfer->dev_handle = devh;
815 	transfer->endpoint = endpoint;
816 	transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
817 	transfer->timeout = timeout;
818 	transfer->buffer = buf;
819 	transfer->length = length;
820 	transfer->user_data = user_data;
821 	transfer->callback = callback;
822 }
823 
824 void
825 libusb_fill_bulk_stream_transfer(struct libusb_transfer *transfer,
826     libusb_device_handle *dev_handle, unsigned char endpoint,
827     uint32_t stream_id, unsigned char *buffer, int length,
828     libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
829 {
830 	libusb_fill_bulk_transfer(transfer, dev_handle, endpoint, buffer,
831 	    length, callback, user_data, timeout);
832 	transfer->type = LIBUSB_TRANSFER_TYPE_BULK_STREAM;
833 
834 	libusb_transfer_set_stream_id(transfer, stream_id);
835 }
836 
837 void
838 libusb_fill_iso_transfer(struct libusb_transfer *transfer,
839     libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
840     int length, int npacket, libusb_transfer_cb_fn callback,
841     void *user_data, uint32_t timeout)
842 {
843 	transfer->dev_handle = devh;
844 	transfer->endpoint = endpoint;
845 	transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
846 	transfer->timeout = timeout;
847 	transfer->buffer = buf;
848 	transfer->length = length;
849 	transfer->num_iso_packets = npacket;
850 	transfer->user_data = user_data;
851 	transfer->callback = callback;
852 }
853 
854 int
855 libusb_alloc_streams(libusb_device_handle *dev, uint32_t num_streams,
856     unsigned char *endpoints, int num_endpoints)
857 {
858 	if (num_streams > 1)
859 		return (LIBUSB_ERROR_INVALID_PARAM);
860 	return (0);
861 }
862 
863 int
864 libusb_free_streams(libusb_device_handle *dev, unsigned char *endpoints, int num_endpoints)
865 {
866 
867 	return (0);
868 }
869 
870 void
871 libusb_transfer_set_stream_id(struct libusb_transfer *transfer, uint32_t stream_id)
872 {
873 	struct libusb_super_transfer *sxfer;
874 
875 	if (transfer == NULL)
876 		return;
877 
878 	sxfer = (struct libusb_super_transfer *)(
879 	    ((uint8_t *)transfer) - sizeof(*sxfer));
880 
881 	/* set stream ID */
882 	sxfer->stream_id = stream_id;
883 }
884 
885 uint32_t
886 libusb_transfer_get_stream_id(struct libusb_transfer *transfer)
887 {
888 	struct libusb_super_transfer *sxfer;
889 
890 	if (transfer == NULL)
891 		return (0);
892 
893 	sxfer = (struct libusb_super_transfer *)(
894 	    ((uint8_t *)transfer) - sizeof(*sxfer));
895 
896 	/* get stream ID */
897 	return (sxfer->stream_id);
898 }
899 
900 void
901 libusb_free_pollfds(const struct libusb_pollfd **pollfds)
902 {
903 	if (pollfds == NULL)
904 		return;
905 
906 	free(pollfds);
907 }
908