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