1 /* $FreeBSD: stable/9/lib/libusb/libusb10_io.c 339191 2018-10-05 07:52:28Z hselasky $ */
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
libusb10_add_pollfd(libusb_context * ctx,struct libusb_super_pollfd * pollfd,struct libusb20_device * pdev,int fd,short events)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
libusb10_remove_pollfd(libusb_context * ctx,struct libusb_super_pollfd * pollfd)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
libusb10_handle_events_sub(struct libusb_context * ctx,struct timeval * tv)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
libusb_try_lock_events(libusb_context * ctx)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
libusb_lock_events(libusb_context * ctx)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
libusb_unlock_events(libusb_context * ctx)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
libusb_event_handling_ok(libusb_context * ctx)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
libusb_event_handler_active(libusb_context * ctx)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
libusb_lock_event_waiters(libusb_context * ctx)283 libusb_lock_event_waiters(libusb_context *ctx)
284 {
285 	ctx = GET_CONTEXT(ctx);
286 	CTX_LOCK(ctx);
287 }
288 
289 void
libusb_unlock_event_waiters(libusb_context * ctx)290 libusb_unlock_event_waiters(libusb_context *ctx)
291 {
292 	ctx = GET_CONTEXT(ctx);
293 	CTX_UNLOCK(ctx);
294 }
295 
296 int
libusb_wait_for_event(libusb_context * ctx,struct timeval * tv)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 		/* try to grab polling of actual events, if any */
309 		if (ctx->ctx_handler == NO_THREAD)
310 			ctx->ctx_handler = pthread_self();
311 		return (0);
312 	}
313 	err = clock_gettime(CLOCK_MONOTONIC, &ts);
314 	if (err < 0)
315 		return (LIBUSB_ERROR_OTHER);
316 
317 	/*
318 	 * The "tv" arguments points to a relative time structure and
319 	 * not an absolute time structure.
320 	 */
321 	ts.tv_sec += tv->tv_sec;
322 	ts.tv_nsec += tv->tv_usec * 1000;
323 	if (ts.tv_nsec >= 1000000000) {
324 		ts.tv_nsec -= 1000000000;
325 		ts.tv_sec++;
326 	}
327 	err = pthread_cond_timedwait(&ctx->ctx_cond,
328 	    &ctx->ctx_lock, &ts);
329 	/* try to grab polling of actual events, if any */
330 	if (ctx->ctx_handler == NO_THREAD)
331 		ctx->ctx_handler = pthread_self();
332 
333 	if (err == ETIMEDOUT)
334 		return (1);
335 
336 	return (0);
337 }
338 
339 int
libusb_handle_events_timeout_completed(libusb_context * ctx,struct timeval * tv,int * completed)340 libusb_handle_events_timeout_completed(libusb_context *ctx,
341     struct timeval *tv, int *completed)
342 {
343 	int err = 0;
344 
345 	ctx = GET_CONTEXT(ctx);
346 
347 	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed enter");
348 
349 	libusb_lock_events(ctx);
350 
351 	while (1) {
352 		if (completed != NULL) {
353 			if (*completed != 0 || err != 0)
354 				break;
355 		}
356 		err = libusb_handle_events_locked(ctx, tv);
357 		if (completed == NULL)
358 			break;
359 	}
360 
361 	libusb_unlock_events(ctx);
362 
363 	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed exit");
364 
365 	return (err);
366 }
367 
368 int
libusb_handle_events_completed(libusb_context * ctx,int * completed)369 libusb_handle_events_completed(libusb_context *ctx, int *completed)
370 {
371 	return (libusb_handle_events_timeout_completed(ctx, NULL, completed));
372 }
373 
374 int
libusb_handle_events_timeout(libusb_context * ctx,struct timeval * tv)375 libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv)
376 {
377 	return (libusb_handle_events_timeout_completed(ctx, tv, NULL));
378 }
379 
380 int
libusb_handle_events(libusb_context * ctx)381 libusb_handle_events(libusb_context *ctx)
382 {
383 	return (libusb_handle_events_timeout_completed(ctx, NULL, NULL));
384 }
385 
386 int
libusb_handle_events_locked(libusb_context * ctx,struct timeval * tv)387 libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv)
388 {
389 	int err;
390 
391 	ctx = GET_CONTEXT(ctx);
392 
393 	if (libusb_event_handling_ok(ctx)) {
394 		err = libusb10_handle_events_sub(ctx, tv);
395 	} else {
396 		err = libusb_wait_for_event(ctx, tv);
397 		if (err != 0)
398 			err = LIBUSB_ERROR_TIMEOUT;
399 	}
400 	return (err);
401 }
402 
403 int
libusb_get_next_timeout(libusb_context * ctx,struct timeval * tv)404 libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv)
405 {
406 	/* all timeouts are currently being done by the kernel */
407 	timerclear(tv);
408 	return (0);
409 }
410 
411 void
libusb_set_pollfd_notifiers(libusb_context * ctx,libusb_pollfd_added_cb added_cb,libusb_pollfd_removed_cb removed_cb,void * user_data)412 libusb_set_pollfd_notifiers(libusb_context *ctx,
413     libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
414     void *user_data)
415 {
416 	ctx = GET_CONTEXT(ctx);
417 
418 	ctx->fd_added_cb = added_cb;
419 	ctx->fd_removed_cb = removed_cb;
420 	ctx->fd_cb_user_data = user_data;
421 }
422 
423 const struct libusb_pollfd **
libusb_get_pollfds(libusb_context * ctx)424 libusb_get_pollfds(libusb_context *ctx)
425 {
426 	struct libusb_super_pollfd *pollfd;
427 	libusb_pollfd **ret;
428 	int i;
429 
430 	ctx = GET_CONTEXT(ctx);
431 
432 	CTX_LOCK(ctx);
433 
434 	i = 0;
435 	TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
436 	    i++;
437 
438 	ret = calloc(i + 1, sizeof(struct libusb_pollfd *));
439 	if (ret == NULL)
440 		goto done;
441 
442 	i = 0;
443 	TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
444 	    ret[i++] = &pollfd->pollfd;
445 	ret[i] = NULL;
446 
447 done:
448 	CTX_UNLOCK(ctx);
449 	return ((const struct libusb_pollfd **)ret);
450 }
451 
452 
453 /* Synchronous device I/O */
454 
455 int
libusb_control_transfer(libusb_device_handle * devh,uint8_t bmRequestType,uint8_t bRequest,uint16_t wValue,uint16_t wIndex,uint8_t * data,uint16_t wLength,unsigned int timeout)456 libusb_control_transfer(libusb_device_handle *devh,
457     uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
458     uint8_t *data, uint16_t wLength, unsigned int timeout)
459 {
460 	struct LIBUSB20_CONTROL_SETUP_DECODED req;
461 	int err;
462 	uint16_t actlen;
463 
464 	if (devh == NULL)
465 		return (LIBUSB_ERROR_INVALID_PARAM);
466 
467 	if ((wLength != 0) && (data == NULL))
468 		return (LIBUSB_ERROR_INVALID_PARAM);
469 
470 	LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
471 
472 	req.bmRequestType = bmRequestType;
473 	req.bRequest = bRequest;
474 	req.wValue = wValue;
475 	req.wIndex = wIndex;
476 	req.wLength = wLength;
477 
478 	err = libusb20_dev_request_sync(devh, &req, data,
479 	    &actlen, timeout, 0);
480 
481 	if (err == LIBUSB20_ERROR_PIPE)
482 		return (LIBUSB_ERROR_PIPE);
483 	else if (err == LIBUSB20_ERROR_TIMEOUT)
484 		return (LIBUSB_ERROR_TIMEOUT);
485 	else if (err)
486 		return (LIBUSB_ERROR_NO_DEVICE);
487 
488 	return (actlen);
489 }
490 
491 static libusb_context *
libusb10_get_context_by_device_handle(libusb_device_handle * devh)492 libusb10_get_context_by_device_handle(libusb_device_handle *devh)
493 {
494 	libusb_context *ctx;
495 
496 	if (devh != NULL)
497 		ctx = libusb_get_device(devh)->ctx;
498 	else
499 		ctx = NULL;
500 
501 	return (GET_CONTEXT(ctx));
502 }
503 
504 static void
libusb10_do_transfer_cb(struct libusb_transfer * transfer)505 libusb10_do_transfer_cb(struct libusb_transfer *transfer)
506 {
507 	libusb_context *ctx;
508 	int *pdone;
509 
510 	ctx = libusb10_get_context_by_device_handle(transfer->dev_handle);
511 
512 	DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "sync I/O done");
513 
514 	pdone = transfer->user_data;
515 	*pdone = 1;
516 }
517 
518 /*
519  * TODO: Replace the following function. Allocating and freeing on a
520  * per-transfer basis is slow.  --HPS
521  */
522 static int
libusb10_do_transfer(libusb_device_handle * devh,uint8_t endpoint,uint8_t * data,int length,int * transferred,unsigned int timeout,int type)523 libusb10_do_transfer(libusb_device_handle *devh,
524     uint8_t endpoint, uint8_t *data, int length,
525     int *transferred, unsigned int timeout, int type)
526 {
527 	libusb_context *ctx;
528 	struct libusb_transfer *xfer;
529 	int done;
530 	int ret;
531 
532 	if (devh == NULL)
533 		return (LIBUSB_ERROR_INVALID_PARAM);
534 
535 	if ((length != 0) && (data == NULL))
536 		return (LIBUSB_ERROR_INVALID_PARAM);
537 
538 	xfer = libusb_alloc_transfer(0);
539 	if (xfer == NULL)
540 		return (LIBUSB_ERROR_NO_MEM);
541 
542 	ctx = libusb_get_device(devh)->ctx;
543 
544 	xfer->dev_handle = devh;
545 	xfer->endpoint = endpoint;
546 	xfer->type = type;
547 	xfer->timeout = timeout;
548 	xfer->buffer = data;
549 	xfer->length = length;
550 	xfer->user_data = (void *)&done;
551 	xfer->callback = libusb10_do_transfer_cb;
552 	done = 0;
553 
554 	if ((ret = libusb_submit_transfer(xfer)) < 0) {
555 		libusb_free_transfer(xfer);
556 		return (ret);
557 	}
558 	while (done == 0) {
559 		if ((ret = libusb_handle_events(ctx)) < 0) {
560 			libusb_cancel_transfer(xfer);
561 			usleep(1000);	/* nice it */
562 		}
563 	}
564 
565 	*transferred = xfer->actual_length;
566 
567 	switch (xfer->status) {
568 	case LIBUSB_TRANSFER_COMPLETED:
569 		ret = 0;
570 		break;
571 	case LIBUSB_TRANSFER_TIMED_OUT:
572 		ret = LIBUSB_ERROR_TIMEOUT;
573 		break;
574 	case LIBUSB_TRANSFER_OVERFLOW:
575 		ret = LIBUSB_ERROR_OVERFLOW;
576 		break;
577 	case LIBUSB_TRANSFER_STALL:
578 		ret = LIBUSB_ERROR_PIPE;
579 		break;
580 	case LIBUSB_TRANSFER_NO_DEVICE:
581 		ret = LIBUSB_ERROR_NO_DEVICE;
582 		break;
583 	default:
584 		ret = LIBUSB_ERROR_OTHER;
585 		break;
586 	}
587 
588 	libusb_free_transfer(xfer);
589 	return (ret);
590 }
591 
592 int
libusb_bulk_transfer(libusb_device_handle * devh,uint8_t endpoint,uint8_t * data,int length,int * transferred,unsigned int timeout)593 libusb_bulk_transfer(libusb_device_handle *devh,
594     uint8_t endpoint, uint8_t *data, int length,
595     int *transferred, unsigned int timeout)
596 {
597 	libusb_context *ctx;
598 	int ret;
599 
600 	ctx = libusb10_get_context_by_device_handle(devh);
601 
602 	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter");
603 
604 	ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
605 	    timeout, LIBUSB_TRANSFER_TYPE_BULK);
606 
607 	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave");
608 	return (ret);
609 }
610 
611 int
libusb_interrupt_transfer(libusb_device_handle * devh,uint8_t endpoint,uint8_t * data,int length,int * transferred,unsigned int timeout)612 libusb_interrupt_transfer(libusb_device_handle *devh,
613     uint8_t endpoint, uint8_t *data, int length,
614     int *transferred, unsigned int timeout)
615 {
616 	libusb_context *ctx;
617 	int ret;
618 
619 	ctx = libusb10_get_context_by_device_handle(devh);
620 
621 	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter");
622 
623 	ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
624 	    timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
625 
626 	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave");
627 	return (ret);
628 }
629 
630 uint8_t *
libusb_get_iso_packet_buffer(struct libusb_transfer * transfer,uint32_t off)631 libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t off)
632 {
633 	uint8_t *ptr;
634 	uint32_t n;
635 
636 	if (transfer->num_iso_packets < 0)
637 		return (NULL);
638 
639 	if (off >= (uint32_t)transfer->num_iso_packets)
640 		return (NULL);
641 
642 	ptr = transfer->buffer;
643 	if (ptr == NULL)
644 		return (NULL);
645 
646 	for (n = 0; n != off; n++) {
647 		ptr += transfer->iso_packet_desc[n].length;
648 	}
649 	return (ptr);
650 }
651 
652 uint8_t *
libusb_get_iso_packet_buffer_simple(struct libusb_transfer * transfer,uint32_t off)653 libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t off)
654 {
655 	uint8_t *ptr;
656 
657 	if (transfer->num_iso_packets < 0)
658 		return (NULL);
659 
660 	if (off >= (uint32_t)transfer->num_iso_packets)
661 		return (NULL);
662 
663 	ptr = transfer->buffer;
664 	if (ptr == NULL)
665 		return (NULL);
666 
667 	ptr += transfer->iso_packet_desc[0].length * off;
668 
669 	return (ptr);
670 }
671 
672 void
libusb_set_iso_packet_lengths(struct libusb_transfer * transfer,uint32_t length)673 libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length)
674 {
675 	int n;
676 
677 	if (transfer->num_iso_packets < 0)
678 		return;
679 
680 	for (n = 0; n != transfer->num_iso_packets; n++)
681 		transfer->iso_packet_desc[n].length = length;
682 }
683 
684 uint8_t *
libusb_control_transfer_get_data(struct libusb_transfer * transfer)685 libusb_control_transfer_get_data(struct libusb_transfer *transfer)
686 {
687 	if (transfer->buffer == NULL)
688 		return (NULL);
689 
690 	return (transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE);
691 }
692 
693 struct libusb_control_setup *
libusb_control_transfer_get_setup(struct libusb_transfer * transfer)694 libusb_control_transfer_get_setup(struct libusb_transfer *transfer)
695 {
696 	return ((struct libusb_control_setup *)transfer->buffer);
697 }
698 
699 void
libusb_fill_control_setup(uint8_t * buf,uint8_t bmRequestType,uint8_t bRequest,uint16_t wValue,uint16_t wIndex,uint16_t wLength)700 libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType,
701     uint8_t bRequest, uint16_t wValue,
702     uint16_t wIndex, uint16_t wLength)
703 {
704 	struct libusb_control_setup *req = (struct libusb_control_setup *)buf;
705 
706 	/* The alignment is OK for all fields below. */
707 	req->bmRequestType = bmRequestType;
708 	req->bRequest = bRequest;
709 	req->wValue = htole16(wValue);
710 	req->wIndex = htole16(wIndex);
711 	req->wLength = htole16(wLength);
712 }
713 
714 void
libusb_fill_control_transfer(struct libusb_transfer * transfer,libusb_device_handle * devh,uint8_t * buf,libusb_transfer_cb_fn callback,void * user_data,uint32_t timeout)715 libusb_fill_control_transfer(struct libusb_transfer *transfer,
716     libusb_device_handle *devh, uint8_t *buf,
717     libusb_transfer_cb_fn callback, void *user_data,
718     uint32_t timeout)
719 {
720 	struct libusb_control_setup *setup = (struct libusb_control_setup *)buf;
721 
722 	transfer->dev_handle = devh;
723 	transfer->endpoint = 0;
724 	transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
725 	transfer->timeout = timeout;
726 	transfer->buffer = buf;
727 	if (setup != NULL)
728 		transfer->length = LIBUSB_CONTROL_SETUP_SIZE
729 			+ le16toh(setup->wLength);
730 	else
731 		transfer->length = 0;
732 	transfer->user_data = user_data;
733 	transfer->callback = callback;
734 
735 }
736 
737 void
libusb_fill_bulk_transfer(struct libusb_transfer * transfer,libusb_device_handle * devh,uint8_t endpoint,uint8_t * buf,int length,libusb_transfer_cb_fn callback,void * user_data,uint32_t timeout)738 libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
739     libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
740     int length, libusb_transfer_cb_fn callback, void *user_data,
741     uint32_t timeout)
742 {
743 	transfer->dev_handle = devh;
744 	transfer->endpoint = endpoint;
745 	transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
746 	transfer->timeout = timeout;
747 	transfer->buffer = buf;
748 	transfer->length = length;
749 	transfer->user_data = user_data;
750 	transfer->callback = callback;
751 }
752 
753 void
libusb_fill_interrupt_transfer(struct libusb_transfer * transfer,libusb_device_handle * devh,uint8_t endpoint,uint8_t * buf,int length,libusb_transfer_cb_fn callback,void * user_data,uint32_t timeout)754 libusb_fill_interrupt_transfer(struct libusb_transfer *transfer,
755     libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
756     int length, libusb_transfer_cb_fn callback, void *user_data,
757     uint32_t timeout)
758 {
759 	transfer->dev_handle = devh;
760 	transfer->endpoint = endpoint;
761 	transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
762 	transfer->timeout = timeout;
763 	transfer->buffer = buf;
764 	transfer->length = length;
765 	transfer->user_data = user_data;
766 	transfer->callback = callback;
767 }
768 
769 void
libusb_fill_iso_transfer(struct libusb_transfer * transfer,libusb_device_handle * devh,uint8_t endpoint,uint8_t * buf,int length,int npacket,libusb_transfer_cb_fn callback,void * user_data,uint32_t timeout)770 libusb_fill_iso_transfer(struct libusb_transfer *transfer,
771     libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
772     int length, int npacket, libusb_transfer_cb_fn callback,
773     void *user_data, uint32_t timeout)
774 {
775 	transfer->dev_handle = devh;
776 	transfer->endpoint = endpoint;
777 	transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
778 	transfer->timeout = timeout;
779 	transfer->buffer = buf;
780 	transfer->length = length;
781 	transfer->num_iso_packets = npacket;
782 	transfer->user_data = user_data;
783 	transfer->callback = callback;
784 }
785 
786 int
libusb_alloc_streams(libusb_device_handle * dev,uint32_t num_streams,unsigned char * endpoints,int num_endpoints)787 libusb_alloc_streams(libusb_device_handle *dev, uint32_t num_streams,
788     unsigned char *endpoints, int num_endpoints)
789 {
790 
791 	return (LIBUSB_ERROR_NOT_SUPPORTED);
792 }
793 
794 int
libusb_free_streams(libusb_device_handle * dev,unsigned char * endpoints,int num_endpoints)795 libusb_free_streams(libusb_device_handle *dev, unsigned char *endpoints, int num_endpoints)
796 {
797 
798 	return (0);
799 }
800 
801 void
libusb_transfer_set_stream_id(struct libusb_transfer * transfer,uint32_t stream_id)802 libusb_transfer_set_stream_id(struct libusb_transfer *transfer, uint32_t stream_id)
803 {
804 	struct libusb_super_transfer *sxfer;
805 
806 	if (transfer == NULL)
807 		return;
808 
809 	sxfer = (struct libusb_super_transfer *)(
810 	    ((uint8_t *)transfer) - sizeof(*sxfer));
811 
812 	/* set stream ID */
813 	sxfer->stream_id = stream_id;
814 }
815 
816 uint32_t
libusb_transfer_get_stream_id(struct libusb_transfer * transfer)817 libusb_transfer_get_stream_id(struct libusb_transfer *transfer)
818 {
819 	struct libusb_super_transfer *sxfer;
820 
821 	if (transfer == NULL)
822 		return (0);
823 
824 	sxfer = (struct libusb_super_transfer *)(
825 	    ((uint8_t *)transfer) - sizeof(*sxfer));
826 
827 	/* get stream ID */
828 	return (sxfer->stream_id);
829 }
830