xref: /freebsd/lib/libusb/libusb20.c (revision eb69d1f144a6fcc765d1b9d44a5ae8082353e70b)
1 /* $FreeBSD$ */
2 /*-
3  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4  *
5  * Copyright (c) 2008-2009 Hans Petter Selasky. 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 <ctype.h>
33 #include <poll.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <time.h>
38 #include <sys/queue.h>
39 #endif
40 
41 #include "libusb20.h"
42 #include "libusb20_desc.h"
43 #include "libusb20_int.h"
44 
45 static int
46 dummy_int(void)
47 {
48 	return (LIBUSB20_ERROR_NOT_SUPPORTED);
49 }
50 
51 static void
52 dummy_void(void)
53 {
54 	return;
55 }
56 
57 static void
58 dummy_callback(struct libusb20_transfer *xfer)
59 {
60 	;				/* style fix */
61 	switch (libusb20_tr_get_status(xfer)) {
62 	case LIBUSB20_TRANSFER_START:
63 		libusb20_tr_submit(xfer);
64 		break;
65 	default:
66 		/* complete or error */
67 		break;
68 	}
69 	return;
70 }
71 
72 #define	dummy_get_config_desc_full (void *)dummy_int
73 #define	dummy_get_config_index (void *)dummy_int
74 #define	dummy_set_config_index (void *)dummy_int
75 #define	dummy_set_alt_index (void *)dummy_int
76 #define	dummy_reset_device (void *)dummy_int
77 #define	dummy_check_connected (void *)dummy_int
78 #define	dummy_set_power_mode (void *)dummy_int
79 #define	dummy_get_power_mode (void *)dummy_int
80 #define	dummy_get_port_path (void *)dummy_int
81 #define	dummy_get_power_usage (void *)dummy_int
82 #define	dummy_kernel_driver_active (void *)dummy_int
83 #define	dummy_detach_kernel_driver (void *)dummy_int
84 #define	dummy_do_request_sync (void *)dummy_int
85 #define	dummy_tr_open (void *)dummy_int
86 #define	dummy_tr_close (void *)dummy_int
87 #define	dummy_tr_clear_stall_sync (void *)dummy_int
88 #define	dummy_process (void *)dummy_int
89 #define	dummy_dev_info (void *)dummy_int
90 #define	dummy_dev_get_iface_driver (void *)dummy_int
91 
92 #define	dummy_tr_submit (void *)dummy_void
93 #define	dummy_tr_cancel_async (void *)dummy_void
94 
95 static const struct libusb20_device_methods libusb20_dummy_methods = {
96 	LIBUSB20_DEVICE(LIBUSB20_DECLARE, dummy)
97 };
98 
99 void
100 libusb20_tr_callback_wrapper(struct libusb20_transfer *xfer)
101 {
102 	;				/* style fix */
103 
104 repeat:
105 
106 	if (!xfer->is_pending) {
107 		xfer->status = LIBUSB20_TRANSFER_START;
108 	} else {
109 		xfer->is_pending = 0;
110 	}
111 
112 	xfer->callback(xfer);
113 
114 	if (xfer->is_restart) {
115 		xfer->is_restart = 0;
116 		goto repeat;
117 	}
118 	if (xfer->is_draining &&
119 	    (!xfer->is_pending)) {
120 		xfer->is_draining = 0;
121 		xfer->status = LIBUSB20_TRANSFER_DRAINED;
122 		xfer->callback(xfer);
123 	}
124 	return;
125 }
126 
127 int
128 libusb20_tr_close(struct libusb20_transfer *xfer)
129 {
130 	int error;
131 
132 	if (!xfer->is_opened) {
133 		return (LIBUSB20_ERROR_OTHER);
134 	}
135 	error = xfer->pdev->methods->tr_close(xfer);
136 
137 	if (xfer->pLength) {
138 		free(xfer->pLength);
139 	}
140 	if (xfer->ppBuffer) {
141 		free(xfer->ppBuffer);
142 	}
143 	/* reset variable fields in case the transfer is opened again */
144 	xfer->priv_sc0 = NULL;
145 	xfer->priv_sc1 = NULL;
146 	xfer->is_opened = 0;
147 	xfer->is_pending = 0;
148 	xfer->is_cancel = 0;
149 	xfer->is_draining = 0;
150 	xfer->is_restart = 0;
151 	xfer->status = 0;
152 	xfer->flags = 0;
153 	xfer->nFrames = 0;
154 	xfer->aFrames = 0;
155 	xfer->timeout = 0;
156 	xfer->maxFrames = 0;
157 	xfer->maxTotalLength = 0;
158 	xfer->maxPacketLen = 0;
159 	return (error);
160 }
161 
162 int
163 libusb20_tr_open(struct libusb20_transfer *xfer, uint32_t MaxBufSize,
164     uint32_t MaxFrameCount, uint8_t ep_no)
165 {
166 	return (libusb20_tr_open_stream(xfer, MaxBufSize, MaxFrameCount, ep_no, 0));
167 }
168 
169 int
170 libusb20_tr_open_stream(struct libusb20_transfer *xfer, uint32_t MaxBufSize,
171     uint32_t MaxFrameCount, uint8_t ep_no, uint16_t stream_id)
172 {
173 	uint32_t size;
174 	uint8_t pre_scale;
175 	int error;
176 
177 	if (xfer->is_opened)
178 		return (LIBUSB20_ERROR_BUSY);
179 	if (MaxFrameCount & LIBUSB20_MAX_FRAME_PRE_SCALE) {
180 		MaxFrameCount &= ~LIBUSB20_MAX_FRAME_PRE_SCALE;
181 		/*
182 		 * The kernel can setup 8 times more frames when
183 		 * pre-scaling ISOCHRONOUS transfers. Make sure the
184 		 * length and pointer buffers are big enough:
185 		 */
186 		MaxFrameCount *= 8;
187 		pre_scale = 1;
188 	} else {
189 		pre_scale = 0;
190 	}
191 	if (MaxFrameCount == 0)
192 		return (LIBUSB20_ERROR_INVALID_PARAM);
193 
194 	xfer->maxFrames = MaxFrameCount;
195 
196 	size = MaxFrameCount * sizeof(xfer->pLength[0]);
197 	xfer->pLength = malloc(size);
198 	if (xfer->pLength == NULL) {
199 		return (LIBUSB20_ERROR_NO_MEM);
200 	}
201 	memset(xfer->pLength, 0, size);
202 
203 	size = MaxFrameCount * sizeof(xfer->ppBuffer[0]);
204 	xfer->ppBuffer = malloc(size);
205 	if (xfer->ppBuffer == NULL) {
206 		free(xfer->pLength);
207 		return (LIBUSB20_ERROR_NO_MEM);
208 	}
209 	memset(xfer->ppBuffer, 0, size);
210 
211 	if (pre_scale) {
212 		error = xfer->pdev->methods->tr_open(xfer, MaxBufSize,
213 		    MaxFrameCount / 8, ep_no, stream_id, 1);
214 	} else {
215 		error = xfer->pdev->methods->tr_open(xfer, MaxBufSize,
216 		    MaxFrameCount, ep_no, stream_id, 0);
217 	}
218 
219 	if (error) {
220 		free(xfer->ppBuffer);
221 		free(xfer->pLength);
222 	} else {
223 		xfer->is_opened = 1;
224 	}
225 	return (error);
226 }
227 
228 struct libusb20_transfer *
229 libusb20_tr_get_pointer(struct libusb20_device *pdev, uint16_t trIndex)
230 {
231 	if (trIndex >= pdev->nTransfer) {
232 		return (NULL);
233 	}
234 	return (pdev->pTransfer + trIndex);
235 }
236 
237 uint32_t
238 libusb20_tr_get_actual_frames(struct libusb20_transfer *xfer)
239 {
240 	return (xfer->aFrames);
241 }
242 
243 uint16_t
244 libusb20_tr_get_time_complete(struct libusb20_transfer *xfer)
245 {
246 	return (xfer->timeComplete);
247 }
248 
249 uint32_t
250 libusb20_tr_get_actual_length(struct libusb20_transfer *xfer)
251 {
252 	uint32_t x;
253 	uint32_t actlen = 0;
254 
255 	for (x = 0; x != xfer->aFrames; x++) {
256 		actlen += xfer->pLength[x];
257 	}
258 	return (actlen);
259 }
260 
261 uint32_t
262 libusb20_tr_get_max_frames(struct libusb20_transfer *xfer)
263 {
264 	return (xfer->maxFrames);
265 }
266 
267 uint32_t
268 libusb20_tr_get_max_packet_length(struct libusb20_transfer *xfer)
269 {
270 	/*
271 	 * Special Case NOTE: If the packet multiplier is non-zero for
272 	 * High Speed USB, the value returned is equal to
273 	 * "wMaxPacketSize * multiplier" !
274 	 */
275 	return (xfer->maxPacketLen);
276 }
277 
278 uint32_t
279 libusb20_tr_get_max_total_length(struct libusb20_transfer *xfer)
280 {
281 	return (xfer->maxTotalLength);
282 }
283 
284 uint8_t
285 libusb20_tr_get_status(struct libusb20_transfer *xfer)
286 {
287 	return (xfer->status);
288 }
289 
290 uint8_t
291 libusb20_tr_pending(struct libusb20_transfer *xfer)
292 {
293 	return (xfer->is_pending);
294 }
295 
296 void   *
297 libusb20_tr_get_priv_sc0(struct libusb20_transfer *xfer)
298 {
299 	return (xfer->priv_sc0);
300 }
301 
302 void   *
303 libusb20_tr_get_priv_sc1(struct libusb20_transfer *xfer)
304 {
305 	return (xfer->priv_sc1);
306 }
307 
308 void
309 libusb20_tr_stop(struct libusb20_transfer *xfer)
310 {
311 	if (!xfer->is_opened) {
312 		/* transfer is not opened */
313 		return;
314 	}
315 	if (!xfer->is_pending) {
316 		/* transfer not pending */
317 		return;
318 	}
319 	if (xfer->is_cancel) {
320 		/* already cancelling */
321 		return;
322 	}
323 	xfer->is_cancel = 1;		/* we are cancelling */
324 
325 	xfer->pdev->methods->tr_cancel_async(xfer);
326 	return;
327 }
328 
329 void
330 libusb20_tr_drain(struct libusb20_transfer *xfer)
331 {
332 	if (!xfer->is_opened) {
333 		/* transfer is not opened */
334 		return;
335 	}
336 	/* make sure that we are cancelling */
337 	libusb20_tr_stop(xfer);
338 
339 	if (xfer->is_pending) {
340 		xfer->is_draining = 1;
341 	}
342 	return;
343 }
344 
345 void
346 libusb20_tr_clear_stall_sync(struct libusb20_transfer *xfer)
347 {
348 	xfer->pdev->methods->tr_clear_stall_sync(xfer);
349 	return;
350 }
351 
352 void
353 libusb20_tr_set_buffer(struct libusb20_transfer *xfer, void *buffer, uint16_t frIndex)
354 {
355 	xfer->ppBuffer[frIndex] = libusb20_pass_ptr(buffer);
356 	return;
357 }
358 
359 void
360 libusb20_tr_set_callback(struct libusb20_transfer *xfer, libusb20_tr_callback_t *cb)
361 {
362 	xfer->callback = cb;
363 	return;
364 }
365 
366 void
367 libusb20_tr_set_flags(struct libusb20_transfer *xfer, uint8_t flags)
368 {
369 	xfer->flags = flags;
370 	return;
371 }
372 
373 uint32_t
374 libusb20_tr_get_length(struct libusb20_transfer *xfer, uint16_t frIndex)
375 {
376 	return (xfer->pLength[frIndex]);
377 }
378 
379 void
380 libusb20_tr_set_length(struct libusb20_transfer *xfer, uint32_t length, uint16_t frIndex)
381 {
382 	xfer->pLength[frIndex] = length;
383 	return;
384 }
385 
386 void
387 libusb20_tr_set_priv_sc0(struct libusb20_transfer *xfer, void *sc0)
388 {
389 	xfer->priv_sc0 = sc0;
390 	return;
391 }
392 
393 void
394 libusb20_tr_set_priv_sc1(struct libusb20_transfer *xfer, void *sc1)
395 {
396 	xfer->priv_sc1 = sc1;
397 	return;
398 }
399 
400 void
401 libusb20_tr_set_timeout(struct libusb20_transfer *xfer, uint32_t timeout)
402 {
403 	xfer->timeout = timeout;
404 	return;
405 }
406 
407 void
408 libusb20_tr_set_total_frames(struct libusb20_transfer *xfer, uint32_t nFrames)
409 {
410 	if (nFrames > xfer->maxFrames) {
411 		/* should not happen */
412 		nFrames = xfer->maxFrames;
413 	}
414 	xfer->nFrames = nFrames;
415 	return;
416 }
417 
418 void
419 libusb20_tr_setup_bulk(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
420 {
421 	xfer->ppBuffer[0] = libusb20_pass_ptr(pBuf);
422 	xfer->pLength[0] = length;
423 	xfer->timeout = timeout;
424 	xfer->nFrames = 1;
425 	return;
426 }
427 
428 void
429 libusb20_tr_setup_control(struct libusb20_transfer *xfer, void *psetup, void *pBuf, uint32_t timeout)
430 {
431 	uint16_t len;
432 
433 	xfer->ppBuffer[0] = libusb20_pass_ptr(psetup);
434 	xfer->pLength[0] = 8;		/* fixed */
435 	xfer->timeout = timeout;
436 
437 	len = ((uint8_t *)psetup)[6] | (((uint8_t *)psetup)[7] << 8);
438 
439 	if (len != 0) {
440 		xfer->nFrames = 2;
441 		xfer->ppBuffer[1] = libusb20_pass_ptr(pBuf);
442 		xfer->pLength[1] = len;
443 	} else {
444 		xfer->nFrames = 1;
445 	}
446 	return;
447 }
448 
449 void
450 libusb20_tr_setup_intr(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
451 {
452 	xfer->ppBuffer[0] = libusb20_pass_ptr(pBuf);
453 	xfer->pLength[0] = length;
454 	xfer->timeout = timeout;
455 	xfer->nFrames = 1;
456 	return;
457 }
458 
459 void
460 libusb20_tr_setup_isoc(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint16_t frIndex)
461 {
462 	if (frIndex >= xfer->maxFrames) {
463 		/* should not happen */
464 		return;
465 	}
466 	xfer->ppBuffer[frIndex] = libusb20_pass_ptr(pBuf);
467 	xfer->pLength[frIndex] = length;
468 	return;
469 }
470 
471 uint8_t
472 libusb20_tr_bulk_intr_sync(struct libusb20_transfer *xfer,
473     void *pbuf, uint32_t length, uint32_t *pactlen,
474     uint32_t timeout)
475 {
476 	struct libusb20_device *pdev = xfer->pdev;
477 	uint32_t transfer_max;
478 	uint32_t transfer_act;
479 	uint8_t retval;
480 
481 	/* set some sensible default value */
482 	if (pactlen != NULL)
483 		*pactlen = 0;
484 
485 	/* check for error condition */
486 	if (libusb20_tr_pending(xfer))
487 		return (LIBUSB20_ERROR_OTHER);
488 
489 	do {
490 		/* compute maximum transfer length */
491 		transfer_max =
492 		    libusb20_tr_get_max_total_length(xfer);
493 
494 		if (transfer_max > length)
495 			transfer_max = length;
496 
497 		/* setup bulk or interrupt transfer */
498 		libusb20_tr_setup_bulk(xfer, pbuf,
499 		    transfer_max, timeout);
500 
501 		/* start the transfer */
502 		libusb20_tr_start(xfer);
503 
504 		/* wait for transfer completion */
505 		while (libusb20_dev_process(pdev) == 0) {
506 
507 			if (libusb20_tr_pending(xfer) == 0)
508 				break;
509 
510 			libusb20_dev_wait_process(pdev, -1);
511 		}
512 
513 		transfer_act = libusb20_tr_get_actual_length(xfer);
514 
515 		/* update actual length, if any */
516 		if (pactlen != NULL)
517 			pactlen[0] += transfer_act;
518 
519 		/* check transfer status */
520 		retval = libusb20_tr_get_status(xfer);
521 		if (retval)
522 			break;
523 
524 		/* check for short transfer */
525 		if (transfer_act != transfer_max)
526 			break;
527 
528 		/* update buffer pointer and length */
529 		pbuf = ((uint8_t *)pbuf) + transfer_max;
530 		length = length - transfer_max;
531 
532 	} while (length != 0);
533 
534 	return (retval);
535 }
536 
537 void
538 libusb20_tr_submit(struct libusb20_transfer *xfer)
539 {
540 	if (!xfer->is_opened) {
541 		/* transfer is not opened */
542 		return;
543 	}
544 	if (xfer->is_pending) {
545 		/* should not happen */
546 		return;
547 	}
548 	xfer->is_pending = 1;		/* we are pending */
549 	xfer->is_cancel = 0;		/* not cancelling */
550 	xfer->is_restart = 0;		/* not restarting */
551 
552 	xfer->pdev->methods->tr_submit(xfer);
553 	return;
554 }
555 
556 void
557 libusb20_tr_start(struct libusb20_transfer *xfer)
558 {
559 	if (!xfer->is_opened) {
560 		/* transfer is not opened */
561 		return;
562 	}
563 	if (xfer->is_pending) {
564 		if (xfer->is_cancel) {
565 			/* cancelling - restart */
566 			xfer->is_restart = 1;
567 		}
568 		/* transfer not pending */
569 		return;
570 	}
571 	/* get into the callback */
572 	libusb20_tr_callback_wrapper(xfer);
573 	return;
574 }
575 
576 /* USB device operations */
577 
578 int
579 libusb20_dev_close(struct libusb20_device *pdev)
580 {
581 	struct libusb20_transfer *xfer;
582 	uint16_t x;
583 	int error = 0;
584 
585 	if (!pdev->is_opened) {
586 		return (LIBUSB20_ERROR_OTHER);
587 	}
588 	for (x = 0; x != pdev->nTransfer; x++) {
589 		xfer = pdev->pTransfer + x;
590 
591 		if (!xfer->is_opened) {
592 			/* transfer is not opened */
593 			continue;
594 		}
595 
596 		libusb20_tr_drain(xfer);
597 
598 		libusb20_tr_close(xfer);
599 	}
600 
601 	if (pdev->pTransfer != NULL) {
602 		free(pdev->pTransfer);
603 		pdev->pTransfer = NULL;
604 	}
605 	error = pdev->beMethods->close_device(pdev);
606 
607 	pdev->methods = &libusb20_dummy_methods;
608 
609 	pdev->is_opened = 0;
610 
611 	/*
612 	 * The following variable is only used by the libusb v0.1
613 	 * compat layer:
614 	 */
615 	pdev->claimed_interface = 0;
616 
617 	/*
618 	 * The following variable is only used by the libusb v1.0
619 	 * compat layer:
620 	 */
621 	pdev->auto_detach = 0;
622 
623 	return (error);
624 }
625 
626 int
627 libusb20_dev_detach_kernel_driver(struct libusb20_device *pdev, uint8_t ifaceIndex)
628 {
629 	int error;
630 
631 	error = pdev->methods->detach_kernel_driver(pdev, ifaceIndex);
632 	return (error);
633 }
634 
635 struct LIBUSB20_DEVICE_DESC_DECODED *
636 libusb20_dev_get_device_desc(struct libusb20_device *pdev)
637 {
638 	return (&(pdev->ddesc));
639 }
640 
641 int
642 libusb20_dev_get_fd(struct libusb20_device *pdev)
643 {
644 	return (pdev->file);
645 }
646 
647 int
648 libusb20_dev_kernel_driver_active(struct libusb20_device *pdev, uint8_t ifaceIndex)
649 {
650 	int error;
651 
652 	error = pdev->methods->kernel_driver_active(pdev, ifaceIndex);
653 	return (error);
654 }
655 
656 int
657 libusb20_dev_open(struct libusb20_device *pdev, uint16_t nTransferMax)
658 {
659 	struct libusb20_transfer *xfer;
660 	uint32_t size;
661 	uint16_t x;
662 	int error;
663 
664 	if (pdev->is_opened) {
665 		return (LIBUSB20_ERROR_BUSY);
666 	}
667 	if (nTransferMax >= 256) {
668 		return (LIBUSB20_ERROR_INVALID_PARAM);
669 	} else if (nTransferMax != 0) {
670 		size = sizeof(pdev->pTransfer[0]) * nTransferMax;
671 		pdev->pTransfer = malloc(size);
672 		if (pdev->pTransfer == NULL) {
673 			return (LIBUSB20_ERROR_NO_MEM);
674 		}
675 		memset(pdev->pTransfer, 0, size);
676 	}
677 	/* initialise all transfers */
678 	for (x = 0; x != nTransferMax; x++) {
679 
680 		xfer = pdev->pTransfer + x;
681 
682 		xfer->pdev = pdev;
683 		xfer->trIndex = x;
684 		xfer->callback = &dummy_callback;
685 	}
686 
687 	/* set "nTransfer" early */
688 	pdev->nTransfer = nTransferMax;
689 
690 	error = pdev->beMethods->open_device(pdev, nTransferMax);
691 
692 	if (error) {
693 		if (pdev->pTransfer != NULL) {
694 			free(pdev->pTransfer);
695 			pdev->pTransfer = NULL;
696 		}
697 		pdev->file = -1;
698 		pdev->file_ctrl = -1;
699 		pdev->nTransfer = 0;
700 	} else {
701 		pdev->is_opened = 1;
702 	}
703 	return (error);
704 }
705 
706 int
707 libusb20_dev_reset(struct libusb20_device *pdev)
708 {
709 	int error;
710 
711 	error = pdev->methods->reset_device(pdev);
712 	return (error);
713 }
714 
715 int
716 libusb20_dev_check_connected(struct libusb20_device *pdev)
717 {
718 	int error;
719 
720 	error = pdev->methods->check_connected(pdev);
721 	return (error);
722 }
723 
724 int
725 libusb20_dev_set_power_mode(struct libusb20_device *pdev, uint8_t power_mode)
726 {
727 	int error;
728 
729 	error = pdev->methods->set_power_mode(pdev, power_mode);
730 	return (error);
731 }
732 
733 uint8_t
734 libusb20_dev_get_power_mode(struct libusb20_device *pdev)
735 {
736 	int error;
737 	uint8_t power_mode;
738 
739 	error = pdev->methods->get_power_mode(pdev, &power_mode);
740 	if (error)
741 		power_mode = LIBUSB20_POWER_ON;	/* fake power mode */
742 	return (power_mode);
743 }
744 
745 int
746 libusb20_dev_get_port_path(struct libusb20_device *pdev, uint8_t *buf, uint8_t bufsize)
747 {
748 	return (pdev->methods->get_port_path(pdev, buf, bufsize));
749 }
750 
751 uint16_t
752 libusb20_dev_get_power_usage(struct libusb20_device *pdev)
753 {
754 	int error;
755 	uint16_t power_usage;
756 
757 	error = pdev->methods->get_power_usage(pdev, &power_usage);
758 	if (error)
759 		power_usage = 0;
760 	return (power_usage);
761 }
762 
763 int
764 libusb20_dev_set_alt_index(struct libusb20_device *pdev, uint8_t ifaceIndex, uint8_t altIndex)
765 {
766 	int error;
767 
768 	error = pdev->methods->set_alt_index(pdev, ifaceIndex, altIndex);
769 	return (error);
770 }
771 
772 int
773 libusb20_dev_set_config_index(struct libusb20_device *pdev, uint8_t configIndex)
774 {
775 	int error;
776 
777 	error = pdev->methods->set_config_index(pdev, configIndex);
778 	return (error);
779 }
780 
781 int
782 libusb20_dev_request_sync(struct libusb20_device *pdev,
783     struct LIBUSB20_CONTROL_SETUP_DECODED *setup, void *data,
784     uint16_t *pactlen, uint32_t timeout, uint8_t flags)
785 {
786 	int error;
787 
788 	error = pdev->methods->do_request_sync(pdev,
789 	    setup, data, pactlen, timeout, flags);
790 	return (error);
791 }
792 
793 int
794 libusb20_dev_req_string_sync(struct libusb20_device *pdev,
795     uint8_t str_index, uint16_t langid, void *ptr, uint16_t len)
796 {
797 	struct LIBUSB20_CONTROL_SETUP_DECODED req;
798 	int error;
799 
800 	/* make sure memory is initialised */
801 	memset(ptr, 0, len);
802 
803 	if (len < 4) {
804 		/* invalid length */
805 		return (LIBUSB20_ERROR_INVALID_PARAM);
806 	}
807 	LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
808 
809 	/*
810 	 * We need to read the USB string in two steps else some USB
811 	 * devices will complain.
812 	 */
813 	req.bmRequestType =
814 	    LIBUSB20_REQUEST_TYPE_STANDARD |
815 	    LIBUSB20_RECIPIENT_DEVICE |
816 	    LIBUSB20_ENDPOINT_IN;
817 	req.bRequest = LIBUSB20_REQUEST_GET_DESCRIPTOR;
818 	req.wValue = (LIBUSB20_DT_STRING << 8) | str_index;
819 	req.wIndex = langid;
820 	req.wLength = 4;		/* bytes */
821 
822 	error = libusb20_dev_request_sync(pdev, &req,
823 	    ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);
824 	if (error) {
825 		return (error);
826 	}
827 	req.wLength = *(uint8_t *)ptr;	/* bytes */
828 	if (req.wLength > len) {
829 		/* partial string read */
830 		req.wLength = len;
831 	}
832 	error = libusb20_dev_request_sync(pdev, &req,
833 	    ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);
834 
835 	if (error) {
836 		return (error);
837 	}
838 	if (((uint8_t *)ptr)[1] != LIBUSB20_DT_STRING) {
839 		return (LIBUSB20_ERROR_OTHER);
840 	}
841 	return (0);			/* success */
842 }
843 
844 int
845 libusb20_dev_req_string_simple_sync(struct libusb20_device *pdev,
846     uint8_t str_index, void *ptr, uint16_t len)
847 {
848 	char *buf;
849 	int error;
850 	uint16_t langid;
851 	uint16_t n;
852 	uint16_t i;
853 	uint16_t c;
854 	uint8_t temp[255];
855 	uint8_t swap;
856 
857 	/* the following code derives from the FreeBSD USB kernel */
858 
859 	if ((len < 1) || (ptr == NULL)) {
860 		/* too short buffer */
861 		return (LIBUSB20_ERROR_INVALID_PARAM);
862 	}
863 	error = libusb20_dev_req_string_sync(pdev,
864 	    0, 0, temp, sizeof(temp));
865 	if (error < 0) {
866 		*(uint8_t *)ptr = 0;	/* zero terminate */
867 		return (error);
868 	}
869 	langid = temp[2] | (temp[3] << 8);
870 
871 	error = libusb20_dev_req_string_sync(pdev, str_index,
872 	    langid, temp, sizeof(temp));
873 	if (error < 0) {
874 		*(uint8_t *)ptr = 0;	/* zero terminate */
875 		return (error);
876 	}
877 	if (temp[0] < 2) {
878 		/* string length is too short */
879 		*(uint8_t *)ptr = 0;	/* zero terminate */
880 		return (LIBUSB20_ERROR_OTHER);
881 	}
882 	/* reserve one byte for terminating zero */
883 	len--;
884 
885 	/* find maximum length */
886 	n = (temp[0] / 2) - 1;
887 	if (n > len) {
888 		n = len;
889 	}
890 	/* reset swap state */
891 	swap = 3;
892 
893 	/* setup output buffer pointer */
894 	buf = ptr;
895 
896 	/* convert and filter */
897 	for (i = 0; (i != n); i++) {
898 		c = temp[(2 * i) + 2] | (temp[(2 * i) + 3] << 8);
899 
900 		/* convert from Unicode, handle buggy strings */
901 		if (((c & 0xff00) == 0) && (swap & 1)) {
902 			/* Little Endian, default */
903 			*buf = c;
904 			swap = 1;
905 		} else if (((c & 0x00ff) == 0) && (swap & 2)) {
906 			/* Big Endian */
907 			*buf = c >> 8;
908 			swap = 2;
909 		} else {
910 			/* skip invalid character */
911 			continue;
912 		}
913 		/*
914 		 * Filter by default - we don't allow greater and less than
915 		 * signs because they might confuse the dmesg printouts!
916 		 */
917 		if ((*buf == '<') || (*buf == '>') || (!isprint(*buf))) {
918 			/* skip invalid character */
919 			continue;
920 		}
921 		buf++;
922 	}
923 	*buf = 0;			/* zero terminate string */
924 
925 	return (0);
926 }
927 
928 struct libusb20_config *
929 libusb20_dev_alloc_config(struct libusb20_device *pdev, uint8_t configIndex)
930 {
931 	struct libusb20_config *retval = NULL;
932 	uint8_t *ptr;
933 	uint16_t len;
934 	uint8_t do_close;
935 	int error;
936 
937 	if (!pdev->is_opened) {
938 		error = libusb20_dev_open(pdev, 0);
939 		if (error) {
940 			return (NULL);
941 		}
942 		do_close = 1;
943 	} else {
944 		do_close = 0;
945 	}
946 	error = pdev->methods->get_config_desc_full(pdev,
947 	    &ptr, &len, configIndex);
948 
949 	if (error) {
950 		goto done;
951 	}
952 	/* parse new config descriptor */
953 	retval = libusb20_parse_config_desc(ptr);
954 
955 	/* free config descriptor */
956 	free(ptr);
957 
958 done:
959 	if (do_close) {
960 		error = libusb20_dev_close(pdev);
961 	}
962 	return (retval);
963 }
964 
965 struct libusb20_device *
966 libusb20_dev_alloc(void)
967 {
968 	struct libusb20_device *pdev;
969 
970 	pdev = malloc(sizeof(*pdev));
971 	if (pdev == NULL) {
972 		return (NULL);
973 	}
974 	memset(pdev, 0, sizeof(*pdev));
975 
976 	pdev->file = -1;
977 	pdev->file_ctrl = -1;
978 	pdev->methods = &libusb20_dummy_methods;
979 	return (pdev);
980 }
981 
982 uint8_t
983 libusb20_dev_get_config_index(struct libusb20_device *pdev)
984 {
985 	int error;
986 	uint8_t cfg_index;
987 	uint8_t do_close;
988 
989 	if (!pdev->is_opened) {
990 		error = libusb20_dev_open(pdev, 0);
991 		if (error == 0) {
992 			do_close = 1;
993 		} else {
994 			do_close = 0;
995 		}
996 	} else {
997 		do_close = 0;
998 	}
999 
1000 	error = pdev->methods->get_config_index(pdev, &cfg_index);
1001 	if (error)
1002 		cfg_index = 0xFF;	/* current config index */
1003 	if (do_close) {
1004 		if (libusb20_dev_close(pdev)) {
1005 			/* ignore */
1006 		}
1007 	}
1008 	return (cfg_index);
1009 }
1010 
1011 uint8_t
1012 libusb20_dev_get_mode(struct libusb20_device *pdev)
1013 {
1014 	return (pdev->usb_mode);
1015 }
1016 
1017 uint8_t
1018 libusb20_dev_get_speed(struct libusb20_device *pdev)
1019 {
1020 	return (pdev->usb_speed);
1021 }
1022 
1023 /* if this function returns an error, the device is gone */
1024 int
1025 libusb20_dev_process(struct libusb20_device *pdev)
1026 {
1027 	int error;
1028 
1029 	error = pdev->methods->process(pdev);
1030 	return (error);
1031 }
1032 
1033 void
1034 libusb20_dev_wait_process(struct libusb20_device *pdev, int timeout)
1035 {
1036 	struct pollfd pfd[1];
1037 
1038 	if (!pdev->is_opened) {
1039 		return;
1040 	}
1041 	pfd[0].fd = pdev->file;
1042 	pfd[0].events = (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
1043 	pfd[0].revents = 0;
1044 
1045 	if (poll(pfd, 1, timeout)) {
1046 		/* ignore any error */
1047 	}
1048 	return;
1049 }
1050 
1051 void
1052 libusb20_dev_free(struct libusb20_device *pdev)
1053 {
1054 	if (pdev == NULL) {
1055 		/* be NULL safe */
1056 		return;
1057 	}
1058 	if (pdev->is_opened) {
1059 		if (libusb20_dev_close(pdev)) {
1060 			/* ignore any errors */
1061 		}
1062 	}
1063 	free(pdev);
1064 	return;
1065 }
1066 
1067 int
1068 libusb20_dev_get_info(struct libusb20_device *pdev,
1069     struct usb_device_info *pinfo)
1070 {
1071 	if (pinfo == NULL)
1072 		return (LIBUSB20_ERROR_INVALID_PARAM);
1073 
1074 	return (pdev->beMethods->dev_get_info(pdev, pinfo));
1075 }
1076 
1077 const char *
1078 libusb20_dev_get_backend_name(struct libusb20_device *pdev)
1079 {
1080 	return (pdev->beMethods->get_backend_name());
1081 }
1082 
1083 const char *
1084 libusb20_dev_get_desc(struct libusb20_device *pdev)
1085 {
1086 	return (pdev->usb_desc);
1087 }
1088 
1089 void
1090 libusb20_dev_set_debug(struct libusb20_device *pdev, int debug)
1091 {
1092 	pdev->debug = debug;
1093 	return;
1094 }
1095 
1096 int
1097 libusb20_dev_get_debug(struct libusb20_device *pdev)
1098 {
1099 	return (pdev->debug);
1100 }
1101 
1102 uint8_t
1103 libusb20_dev_get_address(struct libusb20_device *pdev)
1104 {
1105 	return (pdev->device_address);
1106 }
1107 
1108 uint8_t
1109 libusb20_dev_get_parent_address(struct libusb20_device *pdev)
1110 {
1111 	return (pdev->parent_address);
1112 }
1113 
1114 uint8_t
1115 libusb20_dev_get_parent_port(struct libusb20_device *pdev)
1116 {
1117 	return (pdev->parent_port);
1118 }
1119 
1120 uint8_t
1121 libusb20_dev_get_bus_number(struct libusb20_device *pdev)
1122 {
1123 	return (pdev->bus_number);
1124 }
1125 
1126 int
1127 libusb20_dev_get_iface_desc(struct libusb20_device *pdev,
1128     uint8_t iface_index, char *buf, uint8_t len)
1129 {
1130 	if ((buf == NULL) || (len == 0))
1131 		return (LIBUSB20_ERROR_INVALID_PARAM);
1132 
1133 	buf[0] = 0;		/* set default string value */
1134 
1135 	return (pdev->beMethods->dev_get_iface_desc(
1136 	    pdev, iface_index, buf, len));
1137 }
1138 
1139 /* USB backend operations */
1140 
1141 int
1142 libusb20_be_get_dev_quirk(struct libusb20_backend *pbe,
1143     uint16_t quirk_index, struct libusb20_quirk *pq)
1144 {
1145 	return (pbe->methods->root_get_dev_quirk(pbe, quirk_index, pq));
1146 }
1147 
1148 int
1149 libusb20_be_get_quirk_name(struct libusb20_backend *pbe,
1150     uint16_t quirk_index, struct libusb20_quirk *pq)
1151 {
1152 	return (pbe->methods->root_get_quirk_name(pbe, quirk_index, pq));
1153 }
1154 
1155 int
1156 libusb20_be_add_dev_quirk(struct libusb20_backend *pbe,
1157     struct libusb20_quirk *pq)
1158 {
1159 	return (pbe->methods->root_add_dev_quirk(pbe, pq));
1160 }
1161 
1162 int
1163 libusb20_be_remove_dev_quirk(struct libusb20_backend *pbe,
1164     struct libusb20_quirk *pq)
1165 {
1166 	return (pbe->methods->root_remove_dev_quirk(pbe, pq));
1167 }
1168 
1169 int
1170 libusb20_be_set_template(struct libusb20_backend *pbe, int temp)
1171 {
1172 	return (pbe->methods->root_set_template(pbe, temp));
1173 }
1174 
1175 int
1176 libusb20_be_get_template(struct libusb20_backend *pbe, int *ptemp)
1177 {
1178 	int temp;
1179 
1180 	if (ptemp == NULL)
1181 		ptemp = &temp;
1182 
1183 	return (pbe->methods->root_get_template(pbe, ptemp));
1184 }
1185 
1186 struct libusb20_device *
1187 libusb20_be_device_foreach(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1188 {
1189 	if (pbe == NULL) {
1190 		pdev = NULL;
1191 	} else if (pdev == NULL) {
1192 		pdev = TAILQ_FIRST(&(pbe->usb_devs));
1193 	} else {
1194 		pdev = TAILQ_NEXT(pdev, dev_entry);
1195 	}
1196 	return (pdev);
1197 }
1198 
1199 struct libusb20_backend *
1200 libusb20_be_alloc(const struct libusb20_backend_methods *methods)
1201 {
1202 	struct libusb20_backend *pbe;
1203 
1204 	pbe = malloc(sizeof(*pbe));
1205 	if (pbe == NULL) {
1206 		return (NULL);
1207 	}
1208 	memset(pbe, 0, sizeof(*pbe));
1209 
1210 	TAILQ_INIT(&(pbe->usb_devs));
1211 
1212 	pbe->methods = methods;		/* set backend methods */
1213 
1214 	/* do the initial device scan */
1215 	if (pbe->methods->init_backend) {
1216 		pbe->methods->init_backend(pbe);
1217 	}
1218 	return (pbe);
1219 }
1220 
1221 struct libusb20_backend *
1222 libusb20_be_alloc_linux(void)
1223 {
1224 	return (NULL);
1225 }
1226 
1227 struct libusb20_backend *
1228 libusb20_be_alloc_ugen20(void)
1229 {
1230 	return (libusb20_be_alloc(&libusb20_ugen20_backend));
1231 }
1232 
1233 struct libusb20_backend *
1234 libusb20_be_alloc_default(void)
1235 {
1236 	struct libusb20_backend *pbe;
1237 
1238 #ifdef __linux__
1239 	pbe = libusb20_be_alloc_linux();
1240 	if (pbe) {
1241 		return (pbe);
1242 	}
1243 #endif
1244 	pbe = libusb20_be_alloc_ugen20();
1245 	if (pbe) {
1246 		return (pbe);
1247 	}
1248 	return (NULL);			/* no backend found */
1249 }
1250 
1251 void
1252 libusb20_be_free(struct libusb20_backend *pbe)
1253 {
1254 	struct libusb20_device *pdev;
1255 
1256 	if (pbe == NULL) {
1257 		/* be NULL safe */
1258 		return;
1259 	}
1260 	while ((pdev = libusb20_be_device_foreach(pbe, NULL))) {
1261 		libusb20_be_dequeue_device(pbe, pdev);
1262 		libusb20_dev_free(pdev);
1263 	}
1264 	if (pbe->methods->exit_backend) {
1265 		pbe->methods->exit_backend(pbe);
1266 	}
1267 	/* free backend */
1268 	free(pbe);
1269 }
1270 
1271 void
1272 libusb20_be_enqueue_device(struct libusb20_backend *pbe, struct libusb20_device *pdev)
1273 {
1274 	pdev->beMethods = pbe->methods;	/* copy backend methods */
1275 	TAILQ_INSERT_TAIL(&(pbe->usb_devs), pdev, dev_entry);
1276 }
1277 
1278 void
1279 libusb20_be_dequeue_device(struct libusb20_backend *pbe,
1280     struct libusb20_device *pdev)
1281 {
1282 	TAILQ_REMOVE(&(pbe->usb_devs), pdev, dev_entry);
1283 }
1284 
1285 const char *
1286 libusb20_strerror(int code)
1287 {
1288 	switch (code) {
1289 	case LIBUSB20_SUCCESS:
1290 		return ("Success");
1291 	case LIBUSB20_ERROR_IO:
1292 		return ("I/O error");
1293 	case LIBUSB20_ERROR_INVALID_PARAM:
1294 		return ("Invalid parameter");
1295 	case LIBUSB20_ERROR_ACCESS:
1296 		return ("Permissions error");
1297 	case LIBUSB20_ERROR_NO_DEVICE:
1298 		return ("No device");
1299 	case LIBUSB20_ERROR_NOT_FOUND:
1300 		return ("Not found");
1301 	case LIBUSB20_ERROR_BUSY:
1302 		return ("Device busy");
1303 	case LIBUSB20_ERROR_TIMEOUT:
1304 		return ("Timeout");
1305 	case LIBUSB20_ERROR_OVERFLOW:
1306 		return ("Overflow");
1307 	case LIBUSB20_ERROR_PIPE:
1308 		return ("Pipe error");
1309 	case LIBUSB20_ERROR_INTERRUPTED:
1310 		return ("Interrupted");
1311 	case LIBUSB20_ERROR_NO_MEM:
1312 		return ("Out of memory");
1313 	case LIBUSB20_ERROR_NOT_SUPPORTED:
1314 		return ("Not supported");
1315 	case LIBUSB20_ERROR_OTHER:
1316 		return ("Other error");
1317 	default:
1318 		return ("Unknown error");
1319 	}
1320 }
1321 
1322 const char *
1323 libusb20_error_name(int code)
1324 {
1325 	switch (code) {
1326 	case LIBUSB20_SUCCESS:
1327 		return ("LIBUSB20_SUCCESS");
1328 	case LIBUSB20_ERROR_IO:
1329 		return ("LIBUSB20_ERROR_IO");
1330 	case LIBUSB20_ERROR_INVALID_PARAM:
1331 		return ("LIBUSB20_ERROR_INVALID_PARAM");
1332 	case LIBUSB20_ERROR_ACCESS:
1333 		return ("LIBUSB20_ERROR_ACCESS");
1334 	case LIBUSB20_ERROR_NO_DEVICE:
1335 		return ("LIBUSB20_ERROR_NO_DEVICE");
1336 	case LIBUSB20_ERROR_NOT_FOUND:
1337 		return ("LIBUSB20_ERROR_NOT_FOUND");
1338 	case LIBUSB20_ERROR_BUSY:
1339 		return ("LIBUSB20_ERROR_BUSY");
1340 	case LIBUSB20_ERROR_TIMEOUT:
1341 		return ("LIBUSB20_ERROR_TIMEOUT");
1342 	case LIBUSB20_ERROR_OVERFLOW:
1343 		return ("LIBUSB20_ERROR_OVERFLOW");
1344 	case LIBUSB20_ERROR_PIPE:
1345 		return ("LIBUSB20_ERROR_PIPE");
1346 	case LIBUSB20_ERROR_INTERRUPTED:
1347 		return ("LIBUSB20_ERROR_INTERRUPTED");
1348 	case LIBUSB20_ERROR_NO_MEM:
1349 		return ("LIBUSB20_ERROR_NO_MEM");
1350 	case LIBUSB20_ERROR_NOT_SUPPORTED:
1351 		return ("LIBUSB20_ERROR_NOT_SUPPORTED");
1352 	case LIBUSB20_ERROR_OTHER:
1353 		return ("LIBUSB20_ERROR_OTHER");
1354 	default:
1355 		return ("LIBUSB20_ERROR_UNKNOWN");
1356 	}
1357 }
1358