aboutsummaryrefslogtreecommitdiff
path: root/lib/libusb/libusb20.c
blob: 3efa9c704841ccd5f3679ce42514d55d94edb1ee (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
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
/* $FreeBSD$ */
/*-
 * Copyright (c) 2008-2009 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.
 */

#include <sys/queue.h>

#include <ctype.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

static int
dummy_int(void)
{
	return (LIBUSB20_ERROR_NOT_SUPPORTED);
}

static void
dummy_void(void)
{
	return;
}

static void
dummy_callback(struct libusb20_transfer *xfer)
{
	;				/* style fix */
	switch (libusb20_tr_get_status(xfer)) {
	case LIBUSB20_TRANSFER_START:
		libusb20_tr_submit(xfer);
		break;
	default:
		/* complete or error */
		break;
	}
	return;
}

#define	dummy_get_config_desc_full (void *)dummy_int
#define	dummy_get_config_index (void *)dummy_int
#define	dummy_set_config_index (void *)dummy_int
#define	dummy_set_alt_index (void *)dummy_int
#define	dummy_reset_device (void *)dummy_int
#define	dummy_check_connected (void *)dummy_int
#define	dummy_set_power_mode (void *)dummy_int
#define	dummy_get_power_mode (void *)dummy_int
#define	dummy_kernel_driver_active (void *)dummy_int
#define	dummy_detach_kernel_driver (void *)dummy_int
#define	dummy_do_request_sync (void *)dummy_int
#define	dummy_tr_open (void *)dummy_int
#define	dummy_tr_close (void *)dummy_int
#define	dummy_tr_clear_stall_sync (void *)dummy_int
#define	dummy_process (void *)dummy_int
#define	dummy_dev_info (void *)dummy_int
#define	dummy_dev_get_iface_driver (void *)dummy_int

#define	dummy_tr_submit (void *)dummy_void
#define	dummy_tr_cancel_async (void *)dummy_void

static const struct libusb20_device_methods libusb20_dummy_methods = {
	LIBUSB20_DEVICE(LIBUSB20_DECLARE, dummy)
};

void
libusb20_tr_callback_wrapper(struct libusb20_transfer *xfer)
{
	;				/* style fix */

repeat:

	if (!xfer->is_pending) {
		xfer->status = LIBUSB20_TRANSFER_START;
	} else {
		xfer->is_pending = 0;
	}

	xfer->callback(xfer);

	if (xfer->is_restart) {
		xfer->is_restart = 0;
		goto repeat;
	}
	if (xfer->is_draining &&
	    (!xfer->is_pending)) {
		xfer->is_draining = 0;
		xfer->status = LIBUSB20_TRANSFER_DRAINED;
		xfer->callback(xfer);
	}
	return;
}

int
libusb20_tr_close(struct libusb20_transfer *xfer)
{
	int error;

	if (!xfer->is_opened) {
		return (LIBUSB20_ERROR_OTHER);
	}
	error = xfer->pdev->methods->tr_close(xfer);

	if (xfer->pLength) {
		free(xfer->pLength);
	}
	if (xfer->ppBuffer) {
		free(xfer->ppBuffer);
	}
	/* reset variable fields in case the transfer is opened again */
	xfer->priv_sc0 = 0;
	xfer->priv_sc1 = 0;
	xfer->is_opened = 0;
	xfer->is_pending = 0;
	xfer->is_cancel = 0;
	xfer->is_draining = 0;
	xfer->is_restart = 0;
	xfer->status = 0;
	xfer->flags = 0;
	xfer->nFrames = 0;
	xfer->aFrames = 0;
	xfer->timeout = 0;
	xfer->maxFrames = 0;
	xfer->maxTotalLength = 0;
	xfer->maxPacketLen = 0;
	return (error);
}

int
libusb20_tr_open(struct libusb20_transfer *xfer, uint32_t MaxBufSize,
    uint32_t MaxFrameCount, uint8_t ep_no)
{
	uint32_t size;
	int error;

	if (xfer->is_opened) {
		return (LIBUSB20_ERROR_BUSY);
	}
	if (MaxFrameCount == 0) {
		return (LIBUSB20_ERROR_INVALID_PARAM);
	}
	xfer->maxFrames = MaxFrameCount;

	size = MaxFrameCount * sizeof(xfer->pLength[0]);
	xfer->pLength = malloc(size);
	if (xfer->pLength == NULL) {
		return (LIBUSB20_ERROR_NO_MEM);
	}
	memset(xfer->pLength, 0, size);

	size = MaxFrameCount * sizeof(xfer->ppBuffer[0]);
	xfer->ppBuffer = malloc(size);
	if (xfer->ppBuffer == NULL) {
		free(xfer->pLength);
		return (LIBUSB20_ERROR_NO_MEM);
	}
	memset(xfer->ppBuffer, 0, size);

	error = xfer->pdev->methods->tr_open(xfer, MaxBufSize,
	    MaxFrameCount, ep_no);

	if (error) {
		free(xfer->ppBuffer);
		free(xfer->pLength);
	} else {
		xfer->is_opened = 1;
	}
	return (error);
}

struct libusb20_transfer *
libusb20_tr_get_pointer(struct libusb20_device *pdev, uint16_t trIndex)
{
	if (trIndex >= pdev->nTransfer) {
		return (NULL);
	}
	return (pdev->pTransfer + trIndex);
}

uint32_t
libusb20_tr_get_actual_frames(struct libusb20_transfer *xfer)
{
	return (xfer->aFrames);
}

uint16_t
libusb20_tr_get_time_complete(struct libusb20_transfer *xfer)
{
	return (xfer->timeComplete);
}

uint32_t
libusb20_tr_get_actual_length(struct libusb20_transfer *xfer)
{
	uint32_t x;
	uint32_t actlen = 0;

	for (x = 0; x != xfer->aFrames; x++) {
		actlen += xfer->pLength[x];
	}
	return (actlen);
}

uint32_t
libusb20_tr_get_max_frames(struct libusb20_transfer *xfer)
{
	return (xfer->maxFrames);
}

uint32_t
libusb20_tr_get_max_packet_length(struct libusb20_transfer *xfer)
{
	/*
	 * Special Case NOTE: If the packet multiplier is non-zero for
	 * High Speed USB, the value returned is equal to
	 * "wMaxPacketSize * multiplier" !
	 */
	return (xfer->maxPacketLen);
}

uint32_t
libusb20_tr_get_max_total_length(struct libusb20_transfer *xfer)
{
	return (xfer->maxTotalLength);
}

uint8_t
libusb20_tr_get_status(struct libusb20_transfer *xfer)
{
	return (xfer->status);
}

uint8_t
libusb20_tr_pending(struct libusb20_transfer *xfer)
{
	return (xfer->is_pending);
}

void   *
libusb20_tr_get_priv_sc0(struct libusb20_transfer *xfer)
{
	return (xfer->priv_sc0);
}

void   *
libusb20_tr_get_priv_sc1(struct libusb20_transfer *xfer)
{
	return (xfer->priv_sc1);
}

void
libusb20_tr_stop(struct libusb20_transfer *xfer)
{
	if (!xfer->is_opened) {
		/* transfer is not opened */
		return;
	}
	if (!xfer->is_pending) {
		/* transfer not pending */
		return;
	}
	if (xfer->is_cancel) {
		/* already cancelling */
		return;
	}
	xfer->is_cancel = 1;		/* we are cancelling */

	xfer->pdev->methods->tr_cancel_async(xfer);
	return;
}

void
libusb20_tr_drain(struct libusb20_transfer *xfer)
{
	if (!xfer->is_opened) {
		/* transfer is not opened */
		return;
	}
	/* make sure that we are cancelling */
	libusb20_tr_stop(xfer);

	if (xfer->is_pending) {
		xfer->is_draining = 1;
	}
	return;
}

void
libusb20_tr_clear_stall_sync(struct libusb20_transfer *xfer)
{
	xfer->pdev->methods->tr_clear_stall_sync(xfer);
	return;
}

void
libusb20_tr_set_buffer(struct libusb20_transfer *xfer, void *buffer, uint16_t frIndex)
{
	xfer->ppBuffer[frIndex] = buffer;
	return;
}

void
libusb20_tr_set_callback(struct libusb20_transfer *xfer, libusb20_tr_callback_t *cb)
{
	xfer->callback = cb;
	return;
}

void
libusb20_tr_set_flags(struct libusb20_transfer *xfer, uint8_t flags)
{
	xfer->flags = flags;
	return;
}

uint32_t
libusb20_tr_get_length(struct libusb20_transfer *xfer, uint16_t frIndex)
{
	return (xfer->pLength[frIndex]);
}

void
libusb20_tr_set_length(struct libusb20_transfer *xfer, uint32_t length, uint16_t frIndex)
{
	xfer->pLength[frIndex] = length;
	return;
}

void
libusb20_tr_set_priv_sc0(struct libusb20_transfer *xfer, void *sc0)
{
	xfer->priv_sc0 = sc0;
	return;
}

void
libusb20_tr_set_priv_sc1(struct libusb20_transfer *xfer, void *sc1)
{
	xfer->priv_sc1 = sc1;
	return;
}

void
libusb20_tr_set_timeout(struct libusb20_transfer *xfer, uint32_t timeout)
{
	xfer->timeout = timeout;
	return;
}

void
libusb20_tr_set_total_frames(struct libusb20_transfer *xfer, uint32_t nFrames)
{
	if (nFrames > xfer->maxFrames) {
		/* should not happen */
		nFrames = xfer->maxFrames;
	}
	xfer->nFrames = nFrames;
	return;
}

void
libusb20_tr_setup_bulk(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
{
	xfer->ppBuffer[0] = pBuf;
	xfer->pLength[0] = length;
	xfer->timeout = timeout;
	xfer->nFrames = 1;
	return;
}

void
libusb20_tr_setup_control(struct libusb20_transfer *xfer, void *psetup, void *pBuf, uint32_t timeout)
{
	uint16_t len;

	xfer->ppBuffer[0] = psetup;
	xfer->pLength[0] = 8;		/* fixed */
	xfer->timeout = timeout;

	len = ((uint8_t *)psetup)[6] | (((uint8_t *)psetup)[7] << 8);

	if (len != 0) {
		xfer->nFrames = 2;
		xfer->ppBuffer[1] = pBuf;
		xfer->pLength[1] = len;
	} else {
		xfer->nFrames = 1;
	}
	return;
}

void
libusb20_tr_setup_intr(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint32_t timeout)
{
	xfer->ppBuffer[0] = pBuf;
	xfer->pLength[0] = length;
	xfer->timeout = timeout;
	xfer->nFrames = 1;
	return;
}

void
libusb20_tr_setup_isoc(struct libusb20_transfer *xfer, void *pBuf, uint32_t length, uint16_t frIndex)
{
	if (frIndex >= xfer->maxFrames) {
		/* should not happen */
		return;
	}
	xfer->ppBuffer[frIndex] = pBuf;
	xfer->pLength[frIndex] = length;
	return;
}

uint8_t
libusb20_tr_bulk_intr_sync(struct libusb20_transfer *xfer,
    void *pbuf, uint32_t length, uint32_t *pactlen,
    uint32_t timeout)
{
	struct libusb20_device *pdev = xfer->pdev;
	uint32_t transfer_max;
	uint32_t transfer_act;
	uint8_t retval;

	/* set some sensible default value */
	if (pactlen != NULL)
		*pactlen = 0;

	/* check for error condition */
	if (libusb20_tr_pending(xfer))
		return (LIBUSB20_ERROR_OTHER);

	do {
		/* compute maximum transfer length */
		transfer_max = 
		    libusb20_tr_get_max_total_length(xfer);

		if (transfer_max > length)
			transfer_max = length;

		/* setup bulk or interrupt transfer */
		libusb20_tr_setup_bulk(xfer, pbuf, 
		    transfer_max, timeout);

		/* start the transfer */
		libusb20_tr_start(xfer);

		/* wait for transfer completion */
		while (libusb20_dev_process(pdev) == 0) {

			if (libusb20_tr_pending(xfer) == 0)
				break;

			libusb20_dev_wait_process(pdev, -1);
		}

		transfer_act = libusb20_tr_get_actual_length(xfer);

		/* update actual length, if any */
		if (pactlen != NULL)
			pactlen[0] += transfer_act;

		/* check transfer status */
		retval = libusb20_tr_get_status(xfer);
		if (retval)
			break;

		/* check for short transfer */
		if (transfer_act != transfer_max)
			break;

		/* update buffer pointer and length */
		pbuf = ((uint8_t *)pbuf) + transfer_max;
		length = length - transfer_max;

	} while (length != 0);

	return (retval);
}

void
libusb20_tr_submit(struct libusb20_transfer *xfer)
{
	if (!xfer->is_opened) {
		/* transfer is not opened */
		return;
	}
	if (xfer->is_pending) {
		/* should not happen */
		return;
	}
	xfer->is_pending = 1;		/* we are pending */
	xfer->is_cancel = 0;		/* not cancelling */
	xfer->is_restart = 0;		/* not restarting */

	xfer->pdev->methods->tr_submit(xfer);
	return;
}

void
libusb20_tr_start(struct libusb20_transfer *xfer)
{
	if (!xfer->is_opened) {
		/* transfer is not opened */
		return;
	}
	if (xfer->is_pending) {
		if (xfer->is_cancel) {
			/* cancelling - restart */
			xfer->is_restart = 1;
		}
		/* transfer not pending */
		return;
	}
	/* get into the callback */
	libusb20_tr_callback_wrapper(xfer);
	return;
}

/* USB device operations */

int
libusb20_dev_close(struct libusb20_device *pdev)
{
	struct libusb20_transfer *xfer;
	uint16_t x;
	int error = 0;

	if (!pdev->is_opened) {
		return (LIBUSB20_ERROR_OTHER);
	}
	for (x = 0; x != pdev->nTransfer; x++) {
		xfer = pdev->pTransfer + x;

		if (!xfer->is_opened) {
			/* transfer is not opened */
			continue;
		}

		libusb20_tr_drain(xfer);

		libusb20_tr_close(xfer);
	}

	if (pdev->pTransfer != NULL) {
		free(pdev->pTransfer);
		pdev->pTransfer = NULL;
	}
	error = pdev->beMethods->close_device(pdev);

	pdev->methods = &libusb20_dummy_methods;

	pdev->is_opened = 0;

	/* 
	 * The following variable is only used by the libusb v0.1
	 * compat layer:
	 */
	pdev->claimed_interface = 0;

	return (error);
}

int
libusb20_dev_detach_kernel_driver(struct libusb20_device *pdev, uint8_t ifaceIndex)
{
	int error;

	error = pdev->methods->detach_kernel_driver(pdev, ifaceIndex);
	return (error);
}

struct LIBUSB20_DEVICE_DESC_DECODED *
libusb20_dev_get_device_desc(struct libusb20_device *pdev)
{
	return (&(pdev->ddesc));
}

int
libusb20_dev_get_fd(struct libusb20_device *pdev)
{
	return (pdev->file);
}

int
libusb20_dev_kernel_driver_active(struct libusb20_device *pdev, uint8_t ifaceIndex)
{
	int error;

	error = pdev->methods->kernel_driver_active(pdev, ifaceIndex);
	return (error);
}

int
libusb20_dev_open(struct libusb20_device *pdev, uint16_t nTransferMax)
{
	struct libusb20_transfer *xfer;
	uint32_t size;
	uint16_t x;
	int error;

	if (pdev->is_opened) {
		return (LIBUSB20_ERROR_BUSY);
	}
	if (nTransferMax >= 256) {
		return (LIBUSB20_ERROR_INVALID_PARAM);
	} else if (nTransferMax != 0) {
		size = sizeof(pdev->pTransfer[0]) * nTransferMax;
		pdev->pTransfer = malloc(size);
		if (pdev->pTransfer == NULL) {
			return (LIBUSB20_ERROR_NO_MEM);
		}
		memset(pdev->pTransfer, 0, size);
	}
	/* initialise all transfers */
	for (x = 0; x != nTransferMax; x++) {

		xfer = pdev->pTransfer + x;

		xfer->pdev = pdev;
		xfer->trIndex = x;
		xfer->callback = &dummy_callback;
	}

	/* set "nTransfer" early */
	pdev->nTransfer = nTransferMax;

	error = pdev->beMethods->open_device(pdev, nTransferMax);

	if (error) {
		if (pdev->pTransfer != NULL) {
			free(pdev->pTransfer);
			pdev->pTransfer = NULL;
		}
		pdev->file = -1;
		pdev->file_ctrl = -1;
		pdev->nTransfer = 0;
	} else {
		pdev->is_opened = 1;
	}
	return (error);
}

int
libusb20_dev_reset(struct libusb20_device *pdev)
{
	int error;

	error = pdev->methods->reset_device(pdev);
	return (error);
}

int
libusb20_dev_check_connected(struct libusb20_device *pdev)
{
	int error;

	error = pdev->methods->check_connected(pdev);
	return (error);
}

int
libusb20_dev_set_power_mode(struct libusb20_device *pdev, uint8_t power_mode)
{
	int error;

	error = pdev->methods->set_power_mode(pdev, power_mode);
	return (error);
}

uint8_t
libusb20_dev_get_power_mode(struct libusb20_device *pdev)
{
	int error;
	uint8_t power_mode;

	error = pdev->methods->get_power_mode(pdev, &power_mode);
	if (error)
		power_mode = LIBUSB20_POWER_ON;	/* fake power mode */
	return (power_mode);
}

int
libusb20_dev_set_alt_index(struct libusb20_device *pdev, uint8_t ifaceIndex, uint8_t altIndex)
{
	int error;

	error = pdev->methods->set_alt_index(pdev, ifaceIndex, altIndex);
	return (error);
}

int
libusb20_dev_set_config_index(struct libusb20_device *pdev, uint8_t configIndex)
{
	int error;

	error = pdev->methods->set_config_index(pdev, configIndex);
	return (error);
}

int
libusb20_dev_request_sync(struct libusb20_device *pdev,
    struct LIBUSB20_CONTROL_SETUP_DECODED *setup, void *data,
    uint16_t *pactlen, uint32_t timeout, uint8_t flags)
{
	int error;

	error = pdev->methods->do_request_sync(pdev,
	    setup, data, pactlen, timeout, flags);
	return (error);
}

int
libusb20_dev_req_string_sync(struct libusb20_device *pdev,
    uint8_t str_index, uint16_t langid, void *ptr, uint16_t len)
{
	struct LIBUSB20_CONTROL_SETUP_DECODED req;
	int error;

	/* make sure memory is initialised */
	memset(ptr, 0, len);

	if (len < 4) {
		/* invalid length */
		return (LIBUSB20_ERROR_INVALID_PARAM);
	}
	LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);

	/*
	 * We need to read the USB string in two steps else some USB
	 * devices will complain.
	 */
	req.bmRequestType =
	    LIBUSB20_REQUEST_TYPE_STANDARD |
	    LIBUSB20_RECIPIENT_DEVICE |
	    LIBUSB20_ENDPOINT_IN;
	req.bRequest = LIBUSB20_REQUEST_GET_DESCRIPTOR;
	req.wValue = (LIBUSB20_DT_STRING << 8) | str_index;
	req.wIndex = langid;
	req.wLength = 4;		/* bytes */

	error = libusb20_dev_request_sync(pdev, &req,
	    ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);
	if (error) {
		return (error);
	}
	req.wLength = *(uint8_t *)ptr;	/* bytes */
	if (req.wLength > len) {
		/* partial string read */
		req.wLength = len;
	}
	error = libusb20_dev_request_sync(pdev, &req,
	    ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK);

	if (error) {
		return (error);
	}
	if (((uint8_t *)ptr)[1] != LIBUSB20_DT_STRING) {
		return (LIBUSB20_ERROR_OTHER);
	}
	return (0);			/* success */
}

int
libusb20_dev_req_string_simple_sync(struct libusb20_device *pdev,
    uint8_t str_index, void *ptr, uint16_t len)
{
	char *buf;
	int error;
	uint16_t langid;
	uint16_t n;
	uint16_t i;
	uint16_t c;
	uint8_t temp[255];
	uint8_t swap;

	/* the following code derives from the FreeBSD USB kernel */

	if ((len < 1) || (ptr == NULL)) {
		/* too short buffer */
		return (LIBUSB20_ERROR_INVALID_PARAM);
	}
	error = libusb20_dev_req_string_sync(pdev,
	    0, 0, temp, sizeof(temp));
	if (error < 0) {
		*(uint8_t *)ptr = 0;	/* zero terminate */
		return (error);
	}
	langid = temp[2] | (temp[3] << 8);

	error = libusb20_dev_req_string_sync(pdev, str_index,
	    langid, temp, sizeof(temp));
	if (error < 0) {
		*(uint8_t *)ptr = 0;	/* zero terminate */
		return (error);
	}
	if (temp[0] < 2) {
		/* string length is too short */
		*(uint8_t *)ptr = 0;	/* zero terminate */
		return (LIBUSB20_ERROR_OTHER);
	}
	/* reserve one byte for terminating zero */
	len--;

	/* find maximum length */
	n = (temp[0] / 2) - 1;
	if (n > len) {
		n = len;
	}
	/* reset swap state */
	swap = 3;

	/* setup output buffer pointer */
	buf = ptr;

	/* convert and filter */
	for (i = 0; (i != n); i++) {
		c = temp[(2 * i) + 2] | (temp[(2 * i) + 3] << 8);

		/* convert from Unicode, handle buggy strings */
		if (((c & 0xff00) == 0) && (swap & 1)) {
			/* Little Endian, default */
			*buf = c;
			swap = 1;
		} else if (((c & 0x00ff) == 0) && (swap & 2)) {
			/* Big Endian */
			*buf = c >> 8;
			swap = 2;
		} else {
			/* skip invalid character */
			continue;
		}
		/*
		 * Filter by default - we don't allow greater and less than
		 * signs because they might confuse the dmesg printouts!
		 */
		if ((*buf == '<') || (*buf == '>') || (!isprint(*buf))) {
			/* skip invalid character */
			continue;
		}
		buf++;
	}
	*buf = 0;			/* zero terminate string */

	return (0);
}

struct libusb20_config *
libusb20_dev_alloc_config(struct libusb20_device *pdev, uint8_t configIndex)
{
	struct libusb20_config *retval = NULL;
	uint8_t *ptr;
	uint16_t len;
	uint8_t do_close;
	int error;

	if (!pdev->is_opened) {
		error = libusb20_dev_open(pdev, 0);
		if (error) {
			return (NULL);
		}
		do_close = 1;
	} else {
		do_close = 0;
	}
	error = pdev->methods->get_config_desc_full(pdev,
	    &ptr, &len, configIndex);

	if (error) {
		goto done;
	}
	/* parse new config descriptor */
	retval = libusb20_parse_config_desc(ptr);

	/* free config descriptor */
	free(ptr);

done:
	if (do_close) {
		error = libusb20_dev_close(pdev);
	}
	return (retval);
}

struct libusb20_device *
libusb20_dev_alloc(void)
{
	struct libusb20_device *pdev;

	pdev = malloc(sizeof(*pdev));
	if (pdev == NULL) {
		return (NULL);
	}
	memset(pdev, 0, sizeof(*pdev));

	pdev->file = -1;
	pdev->file_ctrl = -1;
	pdev->methods = &libusb20_dummy_methods;
	return (pdev);
}

uint8_t
libusb20_dev_get_config_index(struct libusb20_device *pdev)
{
	int error;
	uint8_t cfg_index;
	uint8_t do_close;

	if (!pdev->is_opened) {
		error = libusb20_dev_open(pdev, 0);
		if (error == 0) {
			do_close = 1;
		} else {
			do_close = 0;
		}
	} else {
		do_close = 0;
	}

	error = pdev->methods->get_config_index(pdev, &cfg_index);
	if (error) {
		cfg_index = 0 - 1;	/* current config index */
	}
	if (do_close) {
		if (libusb20_dev_close(pdev)) {
			/* ignore */
		}
	}
	return (cfg_index);
}

uint8_t
libusb20_dev_get_mode(struct libusb20_device *pdev)
{
	return (pdev->usb_mode);
}

uint8_t
libusb20_dev_get_speed(struct libusb20_device *pdev)
{
	return (pdev->usb_speed);
}

/* if this function returns an error, the device is gone */
int
libusb20_dev_process(struct libusb20_device *pdev)
{
	int error;

	error = pdev->methods->process(pdev);
	return (error);
}

void
libusb20_dev_wait_process(struct libusb20_device *pdev, int timeout)
{
	struct pollfd pfd[1];

	if (!pdev->is_opened) {
		return;
	}
	pfd[0].fd = pdev->file;
	pfd[0].events = (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
	pfd[0].revents = 0;

	if (poll(pfd, 1, timeout)) {
		/* ignore any error */
	}
	return;
}

void
libusb20_dev_free(struct libusb20_device *pdev)
{
	if (pdev == NULL) {
		/* be NULL safe */
		return;
	}
	if (pdev->is_opened) {
		if (libusb20_dev_close(pdev)) {
			/* ignore any errors */
		}
	}
	free(pdev);
	return;
}

int
libusb20_dev_get_info(struct libusb20_device *pdev,
    struct usb_device_info *pinfo)
{
	if (pinfo == NULL)
		return (LIBUSB20_ERROR_INVALID_PARAM);

	return (pdev->beMethods->dev_get_info(pdev, pinfo));
}

const char *
libusb20_dev_get_backend_name(struct libusb20_device *pdev)
{
	return (pdev->beMethods->get_backend_name());
}

const char *
libusb20_dev_get_desc(struct libusb20_device *pdev)
{
	return (pdev->usb_desc);
}

void
libusb20_dev_set_debug(struct libusb20_device *pdev, int debug)
{
	pdev->debug = debug;
	return;
}

int
libusb20_dev_get_debug(struct libusb20_device *pdev)
{
	return (pdev->debug);
}

uint8_t
libusb20_dev_get_address(struct libusb20_device *pdev)
{
	return (pdev->device_address);
}

uint8_t
libusb20_dev_get_bus_number(struct libusb20_device *pdev)
{
	return (pdev->bus_number);
}

int
libusb20_dev_get_iface_desc(struct libusb20_device *pdev, 
    uint8_t iface_index, char *buf, uint8_t len)
{
	if ((buf == NULL) || (len == 0))
		return (LIBUSB20_ERROR_INVALID_PARAM);

	return (pdev->beMethods->dev_get_iface_desc(
	    pdev, iface_index, buf, len));
}

/* USB backend operations */

int
libusb20_be_get_dev_quirk(struct libusb20_backend *pbe,
    uint16_t quirk_index, struct libusb20_quirk *pq)
{
	return (pbe->methods->root_get_dev_quirk(pbe, quirk_index, pq));
}

int
libusb20_be_get_quirk_name(struct libusb20_backend *pbe,
    uint16_t quirk_index, struct libusb20_quirk *pq)
{
	return (pbe->methods->root_get_quirk_name(pbe, quirk_index, pq));
}

int
libusb20_be_add_dev_quirk(struct libusb20_backend *pbe,
    struct libusb20_quirk *pq)
{
	return (pbe->methods->root_add_dev_quirk(pbe, pq));
}

int
libusb20_be_remove_dev_quirk(struct libusb20_backend *pbe,
    struct libusb20_quirk *pq)
{
	return (pbe->methods->root_remove_dev_quirk(pbe, pq));
}

int
libusb20_be_set_template(struct libusb20_backend *pbe, int temp)
{
	return (pbe->methods->root_set_template(pbe, temp));
}

int
libusb20_be_get_template(struct libusb20_backend *pbe, int *ptemp)
{
	int temp;

	if (ptemp == NULL)
		ptemp = &temp;

	return (pbe->methods->root_get_template(pbe, ptemp));
}

struct libusb20_device *
libusb20_be_device_foreach(struct libusb20_backend *pbe, struct libusb20_device *pdev)
{
	if (pbe == NULL) {
		pdev = NULL;
	} else if (pdev == NULL) {
		pdev = TAILQ_FIRST(&(pbe->usb_devs));
	} else {
		pdev = TAILQ_NEXT(pdev, dev_entry);
	}
	return (pdev);
}

struct libusb20_backend *
libusb20_be_alloc(const struct libusb20_backend_methods *methods)
{
	struct libusb20_backend *pbe;

	pbe = malloc(sizeof(*pbe));
	if (pbe == NULL) {
		return (NULL);
	}
	memset(pbe, 0, sizeof(*pbe));

	TAILQ_INIT(&(pbe->usb_devs));

	pbe->methods = methods;		/* set backend methods */

	/* do the initial device scan */
	if (pbe->methods->init_backend) {
		pbe->methods->init_backend(pbe);
	}
	return (pbe);
}

struct libusb20_backend *
libusb20_be_alloc_linux(void)
{
	struct libusb20_backend *pbe;

#ifdef __linux__
	pbe = libusb20_be_alloc(&libusb20_linux_backend);
#else
	pbe = NULL;
#endif
	return (pbe);
}

struct libusb20_backend *
libusb20_be_alloc_ugen20(void)
{
	struct libusb20_backend *pbe;

#ifdef __FreeBSD__
	pbe = libusb20_be_alloc(&libusb20_ugen20_backend);
#else
	pbe = NULL;
#endif
	return (pbe);
}

struct libusb20_backend *
libusb20_be_alloc_default(void)
{
	struct libusb20_backend *pbe;

	pbe = libusb20_be_alloc_linux();
	if (pbe) {
		return (pbe);
	}
	pbe = libusb20_be_alloc_ugen20();
	if (pbe) {
		return (pbe);
	}
	return (NULL);			/* no backend found */
}

void
libusb20_be_free(struct libusb20_backend *pbe)
{
	struct libusb20_device *pdev;

	if (pbe == NULL) {
		/* be NULL safe */
		return;
	}
	while ((pdev = libusb20_be_device_foreach(pbe, NULL))) {
		libusb20_be_dequeue_device(pbe, pdev);
		libusb20_dev_free(pdev);
	}
	if (pbe->methods->exit_backend) {
		pbe->methods->exit_backend(pbe);
	}
	/* free backend */
	free(pbe);
}

void
libusb20_be_enqueue_device(struct libusb20_backend *pbe, struct libusb20_device *pdev)
{
	pdev->beMethods = pbe->methods;	/* copy backend methods */
	TAILQ_INSERT_TAIL(&(pbe->usb_devs), pdev, dev_entry);
}

void
libusb20_be_dequeue_device(struct libusb20_backend *pbe,
    struct libusb20_device *pdev)
{
	TAILQ_REMOVE(&(pbe->usb_devs), pdev, dev_entry);
}