aboutsummaryrefslogtreecommitdiff
path: root/lib/libusb/libusb20_compat01.c
blob: aae819c0802647090ef6b985109556a5f8e5b4ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
/* $FreeBSD$ */
/*-
 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * This file contains the emulation layer for LibUSB v0.1 from sourceforge.
 */

#include <sys/queue.h>

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#include "libusb20.h"
#include "libusb20_desc.h"
#include "libusb20_int.h"
#include "usb.h"

/*
 * The two following macros were taken from the original LibUSB v0.1
 * for sake of compatibility:
 */
#define	LIST_ADD(begin, ent)	   \
  do {				   \
    if (begin) {		   \
      ent->next = begin;	   \
      ent->next->prev = ent;	   \
    } else {			   \
      ent->next = NULL;		   \
    }				   \
    ent->prev = NULL;		   \
    begin = ent;		   \
  } while(0)

#define	LIST_DEL(begin, ent)		 \
  do {					 \
    if (ent->prev) {			 \
      ent->prev->next = ent->next;	 \
    } else {				 \
      begin = ent->next;		 \
    }					 \
    if (ent->next) {			 \
      ent->next->prev = ent->prev;	 \
    }					 \
    ent->prev = NULL;			 \
    ent->next = NULL;			 \
  } while (0)

struct usb_bus *usb_busses = NULL;

static struct usb_bus usb_global_bus = {
	.dirname = {"/dev/usb"},
	.root_dev = NULL,
	.devices = NULL,
};

static struct libusb20_backend *usb_backend = NULL;

struct usb_parse_state {

	struct {
		struct libusb20_endpoint *currep;
		struct libusb20_interface *currifc;
		struct libusb20_config *currcfg;
		struct libusb20_me_struct *currextra;
	}	a;

	struct {
		struct usb_config_descriptor *currcfg;
		struct usb_interface_descriptor *currifc;
		struct usb_endpoint_descriptor *currep;
		struct usb_interface *currifcw;
		uint8_t *currextra;
	}	b;

	uint8_t	preparse;
};

static uint8_t
usb_get_first_claimed_interface(usb_dev_handle * dev)
{
	struct libusb20_device *pdev = (void *)dev;
	uint32_t x;
	uint8_t y;

	x = pdev->claimed_interfaces;

	for (y = 0; y != 32; y++) {
		if (x & (1 << y))
			break;
	}

	if (y == 32)
		y = 0xFF;		/* dummy */

	return (y);
}

static struct libusb20_transfer *
usb_get_transfer_by_ep_no(usb_dev_handle * dev, uint8_t ep_no)
{
	struct libusb20_device *pdev = (void *)dev;
	struct libusb20_transfer *xfer;
	int err;
	uint32_t bufsize;
	uint8_t x;
	uint8_t speed;

	x = (ep_no & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 2;

	if (ep_no & LIBUSB20_ENDPOINT_DIR_MASK) {
		/* this is an IN endpoint */
		x |= 1;
	}
	speed = libusb20_dev_get_speed(pdev);

	/* select a sensible buffer size */
	if (speed == LIBUSB20_SPEED_LOW) {
		bufsize = 256;
	} else if (speed == LIBUSB20_SPEED_FULL) {
		bufsize = 4096;
	} else {
		bufsize = 16384;
	}

	xfer = libusb20_tr_get_pointer(pdev, x);

	if (xfer == NULL)
		return (xfer);

	err = libusb20_tr_open(xfer, bufsize, 1, ep_no);
	if (err == LIBUSB20_ERROR_BUSY) {
		/* already opened */
		return (xfer);
	} else if (err) {
		return (NULL);
	}
	/* success */
	return (xfer);
}

usb_dev_handle *
usb_open(struct usb_device *dev)
{
	int err;

	err = libusb20_dev_open(dev->dev, 16 * 2);
	if (err == LIBUSB20_ERROR_BUSY) {
		/*
		 * Workaround buggy USB applications which open the USB
		 * device multiple times:
		 */
		return (dev->dev);
	}
	if (err)
		return (NULL);

	/*
	 * Dequeue USB device from backend queue so that it does not get
	 * freed when the backend is re-scanned:
	 */
	libusb20_be_dequeue_device(usb_backend, dev->dev);

	return (dev->dev);
}

int
usb_close(usb_dev_handle * udev)
{
	struct usb_device *dev;
	int err;

	err = libusb20_dev_close((void *)udev);

	if (err)
		return (-1);

	if (usb_backend != NULL) {
		/*
		 * Enqueue USB device to backend queue so that it gets freed
		 * when the backend is re-scanned:
		 */
		libusb20_be_enqueue_device(usb_backend, (void *)udev);
	} else {
		/*
		 * The backend is gone. Free device data so that we
		 * don't start leaking memory!
		 */
		dev = usb_device(udev);
		libusb20_dev_free((void *)udev);
		LIST_DEL(usb_global_bus.devices, dev);
		free(dev);
	}
	return (0);
}

int
usb_get_string(usb_dev_handle * dev, int strindex,
    int langid, char *buf, size_t buflen)
{
	int err;

	err = libusb20_dev_req_string_sync((void *)dev,
	    strindex, langid, buf, buflen);

	if (err)
		return (-1);

	return (0);
}

int
usb_get_string_simple(usb_dev_handle * dev, int strindex,
    char *buf, size_t buflen)
{
	int err;

	err = libusb20_dev_req_string_simple_sync((void *)dev,
	    strindex, buf, buflen);

	if (err)
		return (-1);

	return (strlen(buf));
}

int
usb_get_descriptor_by_endpoint(usb_dev_handle * udev, int ep, uint8_t type,
    uint8_t ep_index, void *buf, int size)
{
	memset(buf, 0, size);

	return (usb_control_msg(udev, ep | USB_ENDPOINT_IN,
	    USB_REQ_GET_DESCRIPTOR, (type << 8) + ep_index, 0,
	    buf, size, 1000));
}

int
usb_get_descriptor(usb_dev_handle * udev, uint8_t type, uint8_t desc_index,
    void *buf, int size)
{
	memset(buf, 0, size);

	return (usb_control_msg(udev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR,
	    (type << 8) + desc_index, 0, buf, size, 1000));
}

int
usb_parse_descriptor(uint8_t *source, char *description, void *dest)
{
	uint8_t *sp = source;
	uint8_t *dp = dest;
	uint16_t w;
	uint32_t d;
	char *cp;

	for (cp = description; *cp; cp++) {
		switch (*cp) {
		case 'b':		/* 8-bit byte */
			*dp++ = *sp++;
			break;
			/*
			 * 16-bit word, convert from little endian to CPU
			 */
		case 'w':
			w = (sp[1] << 8) | sp[0];
			sp += 2;
			/* Align to word boundary */
			dp += ((dp - (uint8_t *)0) & 1);
			*((uint16_t *)dp) = w;
			dp += 2;
			break;
			/*
			 * 32-bit dword, convert from little endian to CPU
			 */
		case 'd':
			d = (sp[3] << 24) | (sp[2] << 16) |
			    (sp[1] << 8) | sp[0];
			sp += 4;
			/* Align to word boundary */
			dp += ((dp - (uint8_t *)0) & 1);
			/* Align to double word boundary */
			dp += ((dp - (uint8_t *)0) & 2);
			*((uint32_t *)dp) = d;
			dp += 4;
			break;
		}
	}
	return (sp - source);
}

static void
usb_parse_extra(struct usb_parse_state *ps, uint8_t **pptr, int *plen)
{
	void *ptr;
	uint16_t len;

	ptr = ps->a.currextra->ptr;
	len = ps->a.currextra->len;

	if (ps->preparse == 0) {
		memcpy(ps->b.currextra, ptr, len);
		*pptr = ps->b.currextra;
		*plen = len;
	}
	ps->b.currextra += len;
	return;
}

static void
usb_parse_endpoint(struct usb_parse_state *ps)
{
	struct usb_endpoint_descriptor *bep;
	struct libusb20_endpoint *aep;

	aep = ps->a.currep;
	bep = ps->b.currep++;

	if (ps->preparse == 0) {
		/* copy descriptor fields */
		bep->bLength = aep->desc.bLength;
		bep->bDescriptorType = aep->desc.bDescriptorType;
		bep->bEndpointAddress = aep->desc.bEndpointAddress;
		bep->bmAttributes = aep->desc.bmAttributes;
		bep->wMaxPacketSize = aep->desc.wMaxPacketSize;
		bep->bInterval = aep->desc.bInterval;
		bep->bRefresh = aep->desc.bRefresh;
		bep->bSynchAddress = aep->desc.bSynchAddress;
	}
	ps->a.currextra = &aep->extra;
	usb_parse_extra(ps, &bep->extra, &bep->extralen);
	return;
}

static void
usb_parse_iface_sub(struct usb_parse_state *ps)
{
	struct libusb20_interface *aifc;
	struct usb_interface_descriptor *bifc;
	uint8_t x;

	aifc = ps->a.currifc;
	bifc = ps->b.currifc++;

	if (ps->preparse == 0) {
		/* copy descriptor fields */
		bifc->bLength = aifc->desc.bLength;
		bifc->bDescriptorType = aifc->desc.bDescriptorType;
		bifc->bInterfaceNumber = aifc->desc.bInterfaceNumber;
		bifc->bAlternateSetting = aifc->desc.bAlternateSetting;
		bifc->bNumEndpoints = aifc->num_endpoints;
		bifc->bInterfaceClass = aifc->desc.bInterfaceClass;
		bifc->bInterfaceSubClass = aifc->desc.bInterfaceSubClass;
		bifc->bInterfaceProtocol = aifc->desc.bInterfaceProtocol;
		bifc->iInterface = aifc->desc.iInterface;
		bifc->endpoint = ps->b.currep;
	}
	for (x = 0; x != aifc->num_endpoints; x++) {
		ps->a.currep = aifc->endpoints + x;
		usb_parse_endpoint(ps);
	}

	ps->a.currextra = &aifc->extra;
	usb_parse_extra(ps, &bifc->extra, &bifc->extralen);
	return;
}

static void
usb_parse_iface(struct usb_parse_state *ps)
{
	struct libusb20_interface *aifc;
	struct usb_interface *bifc;
	uint8_t x;

	aifc = ps->a.currifc;
	bifc = ps->b.currifcw++;

	if (ps->preparse == 0) {
		/* initialise interface wrapper */
		bifc->altsetting = ps->b.currifc;
		bifc->num_altsetting = aifc->num_altsetting + 1;
	}
	usb_parse_iface_sub(ps);

	for (x = 0; x != aifc->num_altsetting; x++) {
		ps->a.currifc = aifc->altsetting + x;
		usb_parse_iface_sub(ps);
	}
	return;
}

static void
usb_parse_config(struct usb_parse_state *ps)
{
	struct libusb20_config *acfg;
	struct usb_config_descriptor *bcfg;
	uint8_t x;

	acfg = ps->a.currcfg;
	bcfg = ps->b.currcfg;

	if (ps->preparse == 0) {
		/* initialise config wrapper */
		bcfg->bLength = acfg->desc.bLength;
		bcfg->bDescriptorType = acfg->desc.bDescriptorType;
		bcfg->wTotalLength = acfg->desc.wTotalLength;
		bcfg->bNumInterfaces = acfg->num_interface;
		bcfg->bConfigurationValue = acfg->desc.bConfigurationValue;
		bcfg->iConfiguration = acfg->desc.iConfiguration;
		bcfg->bmAttributes = acfg->desc.bmAttributes;
		bcfg->MaxPower = acfg->desc.bMaxPower;
		bcfg->interface = ps->b.currifcw;
	}
	for (x = 0; x != acfg->num_interface; x++) {
		ps->a.currifc = acfg->interface + x;
		usb_parse_iface(ps);
	}

	ps->a.currextra = &acfg->extra;
	usb_parse_extra(ps, &bcfg->extra, &bcfg->extralen);
	return;
}

int
usb_parse_configuration(struct usb_config_descriptor *config,
    uint8_t *buffer)
{
	struct usb_parse_state ps;
	uint8_t *ptr;
	uint32_t a;
	uint32_t b;
	uint32_t c;
	uint32_t d;

	if ((buffer == NULL) || (config == NULL)) {
		return (-1);
	}
	memset(&ps, 0, sizeof(ps));

	ps.a.currcfg = libusb20_parse_config_desc(buffer);
	ps.b.currcfg = config;
	if (ps.a.currcfg == NULL) {
		/* could not parse config or out of memory */
		return (-1);
	}
	/* do the pre-parse */
	ps.preparse = 1;
	usb_parse_config(&ps);

	a = ((uint8_t *)(ps.b.currifcw) - ((uint8_t *)0));
	b = ((uint8_t *)(ps.b.currifc) - ((uint8_t *)0));
	c = ((uint8_t *)(ps.b.currep) - ((uint8_t *)0));
	d = ((uint8_t *)(ps.b.currextra) - ((uint8_t *)0));

	/* allocate memory for our configuration */
	ptr = malloc(a + b + c + d);

	/* "currifcw" must be first, hence this pointer is freed */
	ps.b.currifcw = (void *)(ptr);
	ps.b.currifc = (void *)(ptr + a);
	ps.b.currep = (void *)(ptr + a + b);
	ps.b.currextra = (void *)(ptr + a + b + c);

	/* generate a libusb v0.1 compatible structure */
	ps.preparse = 0;
	usb_parse_config(&ps);

	/* free config structure */
	free(ps.a.currcfg);

	return (0);			/* success */
}

void
usb_destroy_configuration(struct usb_device *dev)
{
	uint8_t c;

	if (dev->config == NULL) {
		return;
	}
	for (c = 0; c != dev->descriptor.bNumConfigurations; c++) {
		struct usb_config_descriptor *cf = &dev->config[c];

		if (cf->interface != NULL) {
			free(cf->interface);
			cf->interface = NULL;
		}
	}

	free(dev->config);
	dev->config = NULL;
	return;
}

void
usb_fetch_and_parse_descriptors(usb_dev_handle * udev)
{
	struct usb_device *dev;
	struct libusb20_device *pdev;
	uint8_t *ptr;
	int error;
	uint32_t size;
	uint16_t len;
	uint8_t x;

	if (udev == NULL) {
		/* be NULL safe */
		return;
	}
	dev = usb_device(udev);
	pdev = (void *)udev;

	if (dev->descriptor.bNumConfigurations == 0) {
		/* invalid device */
		return;
	}
	size = dev->descriptor.bNumConfigurations *
	    sizeof(struct usb_config_descriptor);

	dev->config = malloc(size);
	if (dev->config == NULL) {
		/* out of memory */
		return;
	}
	memset(dev->config, 0, size);

	for (x = 0; x != dev->descriptor.bNumConfigurations; x++) {

		error = (pdev->methods->get_config_desc_full) (
		    pdev, &ptr, &len, x);

		if (error) {
			usb_destroy_configuration(dev);
			return;
		}
		usb_parse_configuration(dev->config + x, ptr);

		/* free config buffer */
		free(ptr);
	}
	return;
}

static int
usb_std_io(usb_dev_handle * dev, int ep, char *bytes, int size,
    int timeout, int is_intr)
{
	struct libusb20_transfer *xfer;
	uint32_t temp;
	uint32_t maxsize;
	uint32_t actlen;
	char *oldbytes;

	xfer = usb_get_transfer_by_ep_no(dev, ep);
	if (xfer == NULL)
		return (-1);

	if (libusb20_tr_pending(xfer)) {
		/* there is already a transfer ongoing */
		return (-1);
	}
	maxsize = libusb20_tr_get_max_total_length(xfer);
	oldbytes = bytes;

	/*
	 * We allow transferring zero bytes which is the same
	 * equivalent to a zero length USB packet.
	 */
	do {

		temp = size;
		if (temp > maxsize) {
			/* find maximum possible length */
			temp = maxsize;
		}
		if (is_intr)
			libusb20_tr_setup_intr(xfer, bytes, temp, timeout);
		else
			libusb20_tr_setup_bulk(xfer, bytes, temp, timeout);

		libusb20_tr_start(xfer);

		while (1) {

			if (libusb20_dev_process((void *)dev) != 0) {
				/* device detached */
				return (-1);
			}
			if (libusb20_tr_pending(xfer) == 0) {
				/* transfer complete */
				break;
			}
			/* wait for USB event from kernel */
			libusb20_dev_wait_process((void *)dev, -1);
		}

		switch (libusb20_tr_get_status(xfer)) {
		case 0:
			/* success */
			break;
		case LIBUSB20_TRANSFER_TIMED_OUT:
			/* transfer timeout */
			return (-ETIMEDOUT);
		default:
			/* other transfer error */
			return (-ENXIO);
		}
		actlen = libusb20_tr_get_actual_length(xfer);

		bytes += actlen;
		size -= actlen;

		if (actlen != temp) {
			/* short transfer */
			break;
		}
	} while (size > 0);

	return (bytes - oldbytes);
}

int
usb_bulk_write(usb_dev_handle * dev, int ep, char *bytes,
    int size, int timeout)
{
	return (usb_std_io(dev, ep & ~USB_ENDPOINT_DIR_MASK,
	    bytes, size, timeout, 0));
}

int
usb_bulk_read(usb_dev_handle * dev, int ep, char *bytes,
    int size, int timeout)
{
	return (usb_std_io(dev, ep | USB_ENDPOINT_DIR_MASK,
	    bytes, size, timeout, 0));
}

int
usb_interrupt_write(usb_dev_handle * dev, int ep, char *bytes,
    int size, int timeout)
{
	return (usb_std_io(dev, ep & ~USB_ENDPOINT_DIR_MASK,
	    bytes, size, timeout, 1));
}

int
usb_interrupt_read(usb_dev_handle * dev, int ep, char *bytes,
    int size, int timeout)
{
	return (usb_std_io(dev, ep | USB_ENDPOINT_DIR_MASK,
	    bytes, size, timeout, 1));
}

int
usb_control_msg(usb_dev_handle * dev, int requesttype, int request,
    int value, int wIndex, char *bytes, int size, int timeout)
{
	struct LIBUSB20_CONTROL_SETUP_DECODED req;
	int err;
	uint16_t actlen;

	LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);

	req.bmRequestType = requesttype;
	req.bRequest = request;
	req.wValue = value;
	req.wIndex = wIndex;
	req.wLength = size;

	err = libusb20_dev_request_sync((void *)dev, &req, bytes,
	    &actlen, timeout, 0);

	if (err)
		return (-1);

	return (actlen);
}

int
usb_set_configuration(usb_dev_handle * udev, int bConfigurationValue)
{
	struct usb_device *dev;
	int err;
	uint8_t i;

	/*
	 * Need to translate from "bConfigurationValue" to
	 * configuration index:
	 */

	if (bConfigurationValue == 0) {
		/* unconfigure */
		i = 255;
	} else {
		/* lookup configuration index */
		dev = usb_device(udev);

		/* check if the configuration array is not there */
		if (dev->config == NULL) {
			return (-1);
		}
		for (i = 0;; i++) {
			if (i == dev->descriptor.bNumConfigurations) {
				/* "bConfigurationValue" not found */
				return (-1);
			}
			if ((dev->config + i)->bConfigurationValue ==
			    bConfigurationValue) {
				break;
			}
		}
	}

	err = libusb20_dev_set_config_index((void *)udev, i);

	if (err)
		return (-1);

	return (0);
}

int
usb_claim_interface(usb_dev_handle * dev, int interface)
{
	int err;

	err = libusb20_dev_claim_interface((void *)dev, interface);

	if (err)
		return (-1);

	return (0);
}

int
usb_release_interface(usb_dev_handle * dev, int interface)
{
	int err;

	err = libusb20_dev_release_interface((void *)dev, interface);

	if (err)
		return (-1);

	return (0);
}

int
usb_set_altinterface(usb_dev_handle * dev, int alternate)
{
	int err;
	uint8_t iface;

	iface = usb_get_first_claimed_interface(dev);

	err = libusb20_dev_set_alt_index((void *)dev, iface, alternate);

	if (err)
		return (-1);

	return (0);
}

int
usb_resetep(usb_dev_handle * dev, unsigned int ep)
{
	/* emulate an endpoint reset through clear-STALL */
	return (usb_clear_halt(dev, ep));
}

int
usb_clear_halt(usb_dev_handle * dev, unsigned int ep)
{
	struct libusb20_transfer *xfer;

	xfer = usb_get_transfer_by_ep_no(dev, ep);
	if (xfer == NULL)
		return (-1);

	libusb20_tr_clear_stall_sync(xfer);

	return (0);
}

int
usb_reset(usb_dev_handle * dev)
{
	int err;

	err = libusb20_dev_reset((void *)dev);

	if (err)
		return (-1);

	/*
	 * Be compatible with LibUSB from sourceforge and close the
	 * handle after reset!
	 */
	return (usb_close(dev));
}

const char *
usb_strerror(void)
{
	/* TODO */
	return ("Unknown error");
}

void
usb_init(void)
{
	/* nothing to do */
	return;
}

void
usb_set_debug(int level)
{
	/* use kernel UGEN debugging if you need to see what is going on */
	return;
}

int
usb_find_busses(void)
{
	usb_busses = &usb_global_bus;
	return (0);
}

int
usb_find_devices(void)
{
	struct libusb20_device *pdev;
	struct usb_device *udev;
	struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
	int err;

	/* cleanup after last device search */
	/* close all opened devices, if any */

	while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
		udev = pdev->priv01Data;
		libusb20_be_dequeue_device(usb_backend, pdev);
		libusb20_dev_free(pdev);
		if (udev != NULL) {
			LIST_DEL(usb_global_bus.devices, udev);
			free(udev);
		}
	}

	/* free old USB backend, if any */

	libusb20_be_free(usb_backend);

	/* do a new backend device search */
	usb_backend = libusb20_be_alloc_default();
	if (usb_backend == NULL) {
		return (-1);
	}
	/* iterate all devices */

	pdev = NULL;
	while ((pdev = libusb20_be_device_foreach(usb_backend, pdev))) {
		udev = malloc(sizeof(*udev));
		if (udev == NULL)
			break;

		memset(udev, 0, sizeof(*udev));

		udev->bus = &usb_global_bus;

		snprintf(udev->filename, sizeof(udev->filename),
		    "/dev/ugen%u.%u",
		    libusb20_dev_get_bus_number(pdev),
		    libusb20_dev_get_address(pdev));

		ddesc = libusb20_dev_get_device_desc(pdev);

		udev->descriptor.bLength = sizeof(udev->descriptor);
		udev->descriptor.bDescriptorType = ddesc->bDescriptorType;
		udev->descriptor.bcdUSB = ddesc->bcdUSB;
		udev->descriptor.bDeviceClass = ddesc->bDeviceClass;
		udev->descriptor.bDeviceSubClass = ddesc->bDeviceSubClass;
		udev->descriptor.bDeviceProtocol = ddesc->bDeviceProtocol;
		udev->descriptor.bMaxPacketSize0 = ddesc->bMaxPacketSize0;
		udev->descriptor.idVendor = ddesc->idVendor;
		udev->descriptor.idProduct = ddesc->idProduct;
		udev->descriptor.bcdDevice = ddesc->bcdDevice;
		udev->descriptor.iManufacturer = ddesc->iManufacturer;
		udev->descriptor.iProduct = ddesc->iProduct;
		udev->descriptor.iSerialNumber = ddesc->iSerialNumber;
		udev->descriptor.bNumConfigurations =
		    ddesc->bNumConfigurations;
		if (udev->descriptor.bNumConfigurations > USB_MAXCONFIG) {
			/* truncate number of configurations */
			udev->descriptor.bNumConfigurations = USB_MAXCONFIG;
		}
		/* link together the two structures */
		udev->dev = pdev;
		pdev->priv01Data = udev;

		err = libusb20_dev_open(pdev, 0);
		if (err == 0) {
			/* XXX get all config descriptors by default */
			usb_fetch_and_parse_descriptors((void *)pdev);
			libusb20_dev_close(pdev);
		}
		LIST_ADD(usb_global_bus.devices, udev);
	}

	return (0);			/* success */
}

struct usb_device *
usb_device(usb_dev_handle * dev)
{
	struct libusb20_device *pdev;

	pdev = (void *)dev;

	return (pdev->priv01Data);
}

struct usb_bus *
usb_get_busses(void)
{
	return (usb_busses);
}