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
|
/* $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.
*/
#include <sys/queue.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "libusb20.h"
#include "libusb20_desc.h"
#include "libusb20_int.h"
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usb_ioctl.h>
static libusb20_init_backend_t ugen20_init_backend;
static libusb20_open_device_t ugen20_open_device;
static libusb20_close_device_t ugen20_close_device;
static libusb20_get_backend_name_t ugen20_get_backend_name;
static libusb20_exit_backend_t ugen20_exit_backend;
static libusb20_dev_get_iface_desc_t ugen20_dev_get_iface_desc;
static libusb20_dev_get_info_t ugen20_dev_get_info;
static libusb20_root_get_dev_quirk_t ugen20_root_get_dev_quirk;
static libusb20_root_get_quirk_name_t ugen20_root_get_quirk_name;
static libusb20_root_add_dev_quirk_t ugen20_root_add_dev_quirk;
static libusb20_root_remove_dev_quirk_t ugen20_root_remove_dev_quirk;
static libusb20_root_set_template_t ugen20_root_set_template;
static libusb20_root_get_template_t ugen20_root_get_template;
const struct libusb20_backend_methods libusb20_ugen20_backend = {
LIBUSB20_BACKEND(LIBUSB20_DECLARE, ugen20)
};
/* USB device specific */
static libusb20_get_config_desc_full_t ugen20_get_config_desc_full;
static libusb20_get_config_index_t ugen20_get_config_index;
static libusb20_set_config_index_t ugen20_set_config_index;
static libusb20_set_alt_index_t ugen20_set_alt_index;
static libusb20_reset_device_t ugen20_reset_device;
static libusb20_check_connected_t ugen20_check_connected;
static libusb20_set_power_mode_t ugen20_set_power_mode;
static libusb20_get_power_mode_t ugen20_get_power_mode;
static libusb20_kernel_driver_active_t ugen20_kernel_driver_active;
static libusb20_detach_kernel_driver_t ugen20_detach_kernel_driver;
static libusb20_do_request_sync_t ugen20_do_request_sync;
static libusb20_process_t ugen20_process;
/* USB transfer specific */
static libusb20_tr_open_t ugen20_tr_open;
static libusb20_tr_close_t ugen20_tr_close;
static libusb20_tr_clear_stall_sync_t ugen20_tr_clear_stall_sync;
static libusb20_tr_submit_t ugen20_tr_submit;
static libusb20_tr_cancel_async_t ugen20_tr_cancel_async;
static const struct libusb20_device_methods libusb20_ugen20_device_methods = {
LIBUSB20_DEVICE(LIBUSB20_DECLARE, ugen20)
};
static const char *
ugen20_get_backend_name(void)
{
return ("FreeBSD UGEN 2.0");
}
static uint32_t
ugen20_path_convert_one(const char **pp)
{
const char *ptr;
uint32_t temp = 0;
ptr = *pp;
while ((*ptr >= '0') && (*ptr <= '9')) {
temp *= 10;
temp += (*ptr - '0');
if (temp >= 1000000) {
/* catch overflow early */
return (0xFFFFFFFF);
}
ptr++;
}
if (*ptr == '.') {
/* skip dot */
ptr++;
}
*pp = ptr;
return (temp);
}
static int
ugen20_enumerate(struct libusb20_device *pdev, const char *id)
{
const char *tmp = id;
struct usb_device_descriptor ddesc;
struct usb_device_info devinfo;
uint32_t plugtime;
char buf[64];
int f;
int error;
pdev->bus_number = ugen20_path_convert_one(&tmp);
pdev->device_address = ugen20_path_convert_one(&tmp);
snprintf(buf, sizeof(buf), "/dev/" USB_GENERIC_NAME "%u.%u",
pdev->bus_number, pdev->device_address);
f = open(buf, O_RDWR);
if (f < 0) {
return (LIBUSB20_ERROR_OTHER);
}
if (ioctl(f, USB_GET_PLUGTIME, &plugtime)) {
error = LIBUSB20_ERROR_OTHER;
goto done;
}
/* store when the device was plugged */
pdev->session_data.plugtime = plugtime;
if (ioctl(f, USB_GET_DEVICE_DESC, &ddesc)) {
error = LIBUSB20_ERROR_OTHER;
goto done;
}
LIBUSB20_INIT(LIBUSB20_DEVICE_DESC, &(pdev->ddesc));
libusb20_me_decode(&ddesc, sizeof(ddesc), &(pdev->ddesc));
if (pdev->ddesc.bNumConfigurations == 0) {
error = LIBUSB20_ERROR_OTHER;
goto done;
} else if (pdev->ddesc.bNumConfigurations >= 8) {
error = LIBUSB20_ERROR_OTHER;
goto done;
}
if (ioctl(f, USB_GET_DEVICEINFO, &devinfo)) {
error = LIBUSB20_ERROR_OTHER;
goto done;
}
switch (devinfo.udi_mode) {
case USB_MODE_DEVICE:
pdev->usb_mode = LIBUSB20_MODE_DEVICE;
break;
default:
pdev->usb_mode = LIBUSB20_MODE_HOST;
break;
}
switch (devinfo.udi_speed) {
case USB_SPEED_LOW:
pdev->usb_speed = LIBUSB20_SPEED_LOW;
break;
case USB_SPEED_FULL:
pdev->usb_speed = LIBUSB20_SPEED_FULL;
break;
case USB_SPEED_HIGH:
pdev->usb_speed = LIBUSB20_SPEED_HIGH;
break;
case USB_SPEED_VARIABLE:
pdev->usb_speed = LIBUSB20_SPEED_VARIABLE;
break;
case USB_SPEED_SUPER:
pdev->usb_speed = LIBUSB20_SPEED_SUPER;
break;
default:
pdev->usb_speed = LIBUSB20_SPEED_UNKNOWN;
break;
}
/* get parent HUB index and port */
pdev->parent_address = devinfo.udi_hubindex;
pdev->parent_port = devinfo.udi_hubport;
/* generate a nice description for printout */
snprintf(pdev->usb_desc, sizeof(pdev->usb_desc),
USB_GENERIC_NAME "%u.%u: <%s %s> at usbus%u", pdev->bus_number,
pdev->device_address, devinfo.udi_product,
devinfo.udi_vendor, pdev->bus_number);
error = 0;
done:
close(f);
return (error);
}
struct ugen20_urd_state {
struct usb_read_dir urd;
uint32_t nparsed;
int f;
uint8_t *ptr;
const char *src;
const char *dst;
uint8_t buf[256];
uint8_t dummy_zero[1];
};
static int
ugen20_readdir(struct ugen20_urd_state *st)
{
; /* style fix */
repeat:
if (st->ptr == NULL) {
st->urd.urd_startentry += st->nparsed;
st->urd.urd_data = libusb20_pass_ptr(st->buf);
st->urd.urd_maxlen = sizeof(st->buf);
st->nparsed = 0;
if (ioctl(st->f, USB_READ_DIR, &st->urd)) {
return (EINVAL);
}
st->ptr = st->buf;
}
if (st->ptr[0] == 0) {
if (st->nparsed) {
st->ptr = NULL;
goto repeat;
} else {
return (ENXIO);
}
}
st->src = (void *)(st->ptr + 1);
st->dst = st->src + strlen(st->src) + 1;
st->ptr = st->ptr + st->ptr[0];
st->nparsed++;
if ((st->ptr < st->buf) ||
(st->ptr > st->dummy_zero)) {
/* invalid entry */
return (EINVAL);
}
return (0);
}
static int
ugen20_init_backend(struct libusb20_backend *pbe)
{
struct ugen20_urd_state state;
struct libusb20_device *pdev;
memset(&state, 0, sizeof(state));
state.f = open("/dev/" USB_DEVICE_NAME, O_RDONLY);
if (state.f < 0)
return (LIBUSB20_ERROR_OTHER);
while (ugen20_readdir(&state) == 0) {
if ((state.src[0] != 'u') ||
(state.src[1] != 'g') ||
(state.src[2] != 'e') ||
(state.src[3] != 'n')) {
continue;
}
pdev = libusb20_dev_alloc();
if (pdev == NULL) {
continue;
}
if (ugen20_enumerate(pdev, state.src + 4)) {
libusb20_dev_free(pdev);
continue;
}
/* put the device on the backend list */
libusb20_be_enqueue_device(pbe, pdev);
}
close(state.f);
return (0); /* success */
}
static void
ugen20_tr_release(struct libusb20_device *pdev)
{
struct usb_fs_uninit fs_uninit;
if (pdev->nTransfer == 0) {
return;
}
/* release all pending USB transfers */
if (pdev->privBeData != NULL) {
memset(&fs_uninit, 0, sizeof(fs_uninit));
if (ioctl(pdev->file, USB_FS_UNINIT, &fs_uninit)) {
/* ignore any errors of this kind */
}
}
return;
}
static int
ugen20_tr_renew(struct libusb20_device *pdev)
{
struct usb_fs_init fs_init;
struct usb_fs_endpoint *pfse;
int error;
uint32_t size;
uint16_t nMaxTransfer;
nMaxTransfer = pdev->nTransfer;
error = 0;
if (nMaxTransfer == 0) {
goto done;
}
size = nMaxTransfer * sizeof(*pfse);
if (pdev->privBeData == NULL) {
pfse = malloc(size);
if (pfse == NULL) {
error = LIBUSB20_ERROR_NO_MEM;
goto done;
}
pdev->privBeData = pfse;
}
/* reset endpoint data */
memset(pdev->privBeData, 0, size);
memset(&fs_init, 0, sizeof(fs_init));
fs_init.pEndpoints = libusb20_pass_ptr(pdev->privBeData);
fs_init.ep_index_max = nMaxTransfer;
if (ioctl(pdev->file, USB_FS_INIT, &fs_init)) {
error = LIBUSB20_ERROR_OTHER;
goto done;
}
done:
return (error);
}
static int
ugen20_open_device(struct libusb20_device *pdev, uint16_t nMaxTransfer)
{
uint32_t plugtime;
char buf[64];
int f;
int g;
int error;
snprintf(buf, sizeof(buf), "/dev/" USB_GENERIC_NAME "%u.%u",
pdev->bus_number, pdev->device_address);
/*
* We need two file handles, one for the control endpoint and one
* for BULK, INTERRUPT and ISOCHRONOUS transactions due to optimised
* kernel locking.
*/
g = open(buf, O_RDWR);
if (g < 0) {
return (LIBUSB20_ERROR_NO_DEVICE);
}
f = open(buf, O_RDWR);
if (f < 0) {
close(g);
return (LIBUSB20_ERROR_NO_DEVICE);
}
if (ioctl(f, USB_GET_PLUGTIME, &plugtime)) {
error = LIBUSB20_ERROR_OTHER;
goto done;
}
/* check that the correct device is still plugged */
if (pdev->session_data.plugtime != plugtime) {
error = LIBUSB20_ERROR_NO_DEVICE;
goto done;
}
/* need to set this before "tr_renew()" */
pdev->file = f;
pdev->file_ctrl = g;
/* renew all USB transfers */
error = ugen20_tr_renew(pdev);
if (error) {
goto done;
}
/* set methods */
pdev->methods = &libusb20_ugen20_device_methods;
done:
if (error) {
if (pdev->privBeData) {
/* cleanup after "tr_renew()" */
free(pdev->privBeData);
pdev->privBeData = NULL;
}
pdev->file = -1;
pdev->file_ctrl = -1;
close(f);
close(g);
}
return (error);
}
static int
ugen20_close_device(struct libusb20_device *pdev)
{
struct usb_fs_uninit fs_uninit;
if (pdev->privBeData) {
memset(&fs_uninit, 0, sizeof(fs_uninit));
if (ioctl(pdev->file, USB_FS_UNINIT, &fs_uninit)) {
/* ignore this error */
}
free(pdev->privBeData);
}
pdev->nTransfer = 0;
pdev->privBeData = NULL;
close(pdev->file);
close(pdev->file_ctrl);
pdev->file = -1;
pdev->file_ctrl = -1;
return (0); /* success */
}
static void
ugen20_exit_backend(struct libusb20_backend *pbe)
{
return; /* nothing to do */
}
static int
ugen20_get_config_desc_full(struct libusb20_device *pdev,
uint8_t **ppbuf, uint16_t *plen, uint8_t cfg_index)
{
struct usb_gen_descriptor gen_desc;
struct usb_config_descriptor cdesc;
uint8_t *ptr;
uint16_t len;
int error;
/* make sure memory is initialised */
memset(&cdesc, 0, sizeof(cdesc));
memset(&gen_desc, 0, sizeof(gen_desc));
gen_desc.ugd_data = libusb20_pass_ptr(&cdesc);
gen_desc.ugd_maxlen = sizeof(cdesc);
gen_desc.ugd_config_index = cfg_index;
error = ioctl(pdev->file_ctrl, USB_GET_FULL_DESC, &gen_desc);
if (error) {
return (LIBUSB20_ERROR_OTHER);
}
len = UGETW(cdesc.wTotalLength);
if (len < sizeof(cdesc)) {
/* corrupt descriptor */
return (LIBUSB20_ERROR_OTHER);
}
ptr = malloc(len);
if (!ptr) {
return (LIBUSB20_ERROR_NO_MEM);
}
/* make sure memory is initialised */
memset(ptr, 0, len);
gen_desc.ugd_data = libusb20_pass_ptr(ptr);
gen_desc.ugd_maxlen = len;
error = ioctl(pdev->file_ctrl, USB_GET_FULL_DESC, &gen_desc);
if (error) {
free(ptr);
return (LIBUSB20_ERROR_OTHER);
}
/* make sure that the device doesn't fool us */
memcpy(ptr, &cdesc, sizeof(cdesc));
*ppbuf = ptr;
*plen = len;
return (0); /* success */
}
static int
ugen20_get_config_index(struct libusb20_device *pdev, uint8_t *pindex)
{
int temp;
if (ioctl(pdev->file_ctrl, USB_GET_CONFIG, &temp)) {
return (LIBUSB20_ERROR_OTHER);
}
*pindex = temp;
return (0);
}
static int
ugen20_set_config_index(struct libusb20_device *pdev, uint8_t cfg_index)
{
int temp = cfg_index;
/* release all active USB transfers */
ugen20_tr_release(pdev);
if (ioctl(pdev->file_ctrl, USB_SET_CONFIG, &temp)) {
return (LIBUSB20_ERROR_OTHER);
}
return (ugen20_tr_renew(pdev));
}
static int
ugen20_set_alt_index(struct libusb20_device *pdev,
uint8_t iface_index, uint8_t alt_index)
{
struct usb_alt_interface alt_iface;
memset(&alt_iface, 0, sizeof(alt_iface));
alt_iface.uai_interface_index = iface_index;
alt_iface.uai_alt_index = alt_index;
/* release all active USB transfers */
ugen20_tr_release(pdev);
if (ioctl(pdev->file_ctrl, USB_SET_ALTINTERFACE, &alt_iface)) {
return (LIBUSB20_ERROR_OTHER);
}
return (ugen20_tr_renew(pdev));
}
static int
ugen20_reset_device(struct libusb20_device *pdev)
{
int temp = 0;
/* release all active USB transfers */
ugen20_tr_release(pdev);
if (ioctl(pdev->file_ctrl, USB_DEVICEENUMERATE, &temp)) {
return (LIBUSB20_ERROR_OTHER);
}
return (ugen20_tr_renew(pdev));
}
static int
ugen20_check_connected(struct libusb20_device *pdev)
{
uint32_t plugtime;
int error = 0;
if (ioctl(pdev->file_ctrl, USB_GET_PLUGTIME, &plugtime)) {
error = LIBUSB20_ERROR_NO_DEVICE;
goto done;
}
if (pdev->session_data.plugtime != plugtime) {
error = LIBUSB20_ERROR_NO_DEVICE;
goto done;
}
done:
return (error);
}
static int
ugen20_set_power_mode(struct libusb20_device *pdev, uint8_t power_mode)
{
int temp;
switch (power_mode) {
case LIBUSB20_POWER_OFF:
temp = USB_POWER_MODE_OFF;
break;
case LIBUSB20_POWER_ON:
temp = USB_POWER_MODE_ON;
break;
case LIBUSB20_POWER_SAVE:
temp = USB_POWER_MODE_SAVE;
break;
case LIBUSB20_POWER_SUSPEND:
temp = USB_POWER_MODE_SUSPEND;
break;
case LIBUSB20_POWER_RESUME:
temp = USB_POWER_MODE_RESUME;
break;
default:
return (LIBUSB20_ERROR_INVALID_PARAM);
}
if (ioctl(pdev->file_ctrl, USB_SET_POWER_MODE, &temp)) {
return (LIBUSB20_ERROR_OTHER);
}
return (0);
}
static int
ugen20_get_power_mode(struct libusb20_device *pdev, uint8_t *power_mode)
{
int temp;
if (ioctl(pdev->file_ctrl, USB_GET_POWER_MODE, &temp)) {
return (LIBUSB20_ERROR_OTHER);
}
switch (temp) {
case USB_POWER_MODE_OFF:
temp = LIBUSB20_POWER_OFF;
break;
case USB_POWER_MODE_ON:
temp = LIBUSB20_POWER_ON;
break;
case USB_POWER_MODE_SAVE:
temp = LIBUSB20_POWER_SAVE;
break;
case USB_POWER_MODE_SUSPEND:
temp = LIBUSB20_POWER_SUSPEND;
break;
case USB_POWER_MODE_RESUME:
temp = LIBUSB20_POWER_RESUME;
break;
default:
temp = LIBUSB20_POWER_ON;
break;
}
*power_mode = temp;
return (0); /* success */
}
static int
ugen20_kernel_driver_active(struct libusb20_device *pdev,
uint8_t iface_index)
{
int temp = iface_index;
if (ioctl(pdev->file_ctrl, USB_IFACE_DRIVER_ACTIVE, &temp)) {
return (LIBUSB20_ERROR_OTHER);
}
return (0); /* kernel driver is active */
}
static int
ugen20_detach_kernel_driver(struct libusb20_device *pdev,
uint8_t iface_index)
{
int temp = iface_index;
if (ioctl(pdev->file_ctrl, USB_IFACE_DRIVER_DETACH, &temp)) {
return (LIBUSB20_ERROR_OTHER);
}
return (0); /* kernel driver is active */
}
static int
ugen20_do_request_sync(struct libusb20_device *pdev,
struct LIBUSB20_CONTROL_SETUP_DECODED *setup,
void *data, uint16_t *pactlen, uint32_t timeout, uint8_t flags)
{
struct usb_ctl_request req;
memset(&req, 0, sizeof(req));
req.ucr_data = libusb20_pass_ptr(data);
if (!(flags & LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK)) {
req.ucr_flags |= USB_SHORT_XFER_OK;
}
if (libusb20_me_encode(&req.ucr_request,
sizeof(req.ucr_request), setup)) {
/* ignore */
}
if (ioctl(pdev->file_ctrl, USB_DO_REQUEST, &req)) {
return (LIBUSB20_ERROR_OTHER);
}
if (pactlen) {
/* get actual length */
*pactlen = req.ucr_actlen;
}
return (0); /* kernel driver is active */
}
static int
ugen20_process(struct libusb20_device *pdev)
{
struct usb_fs_complete temp;
struct usb_fs_endpoint *fsep;
struct libusb20_transfer *xfer;
while (1) {
if (ioctl(pdev->file, USB_FS_COMPLETE, &temp)) {
if (errno == EBUSY) {
break;
} else {
/* device detached */
return (LIBUSB20_ERROR_OTHER);
}
}
fsep = pdev->privBeData;
xfer = pdev->pTransfer;
fsep += temp.ep_index;
xfer += temp.ep_index;
/* update transfer status */
if (fsep->status == 0) {
xfer->aFrames = fsep->aFrames;
xfer->timeComplete = fsep->isoc_time_complete;
xfer->status = LIBUSB20_TRANSFER_COMPLETED;
} else if (fsep->status == USB_ERR_CANCELLED) {
xfer->aFrames = 0;
xfer->timeComplete = 0;
xfer->status = LIBUSB20_TRANSFER_CANCELLED;
} else if (fsep->status == USB_ERR_STALLED) {
xfer->aFrames = 0;
xfer->timeComplete = 0;
xfer->status = LIBUSB20_TRANSFER_STALL;
} else if (fsep->status == USB_ERR_TIMEOUT) {
xfer->aFrames = 0;
xfer->timeComplete = 0;
xfer->status = LIBUSB20_TRANSFER_TIMED_OUT;
} else {
xfer->aFrames = 0;
xfer->timeComplete = 0;
xfer->status = LIBUSB20_TRANSFER_ERROR;
}
libusb20_tr_callback_wrapper(xfer);
}
return (0); /* done */
}
static int
ugen20_tr_open(struct libusb20_transfer *xfer, uint32_t MaxBufSize,
uint32_t MaxFrameCount, uint8_t ep_no, uint16_t stream_id,
uint8_t pre_scale)
{
union {
struct usb_fs_open fs_open;
struct usb_fs_open_stream fs_open_stream;
} temp;
struct usb_fs_endpoint *fsep;
if (pre_scale)
MaxFrameCount |= USB_FS_MAX_FRAMES_PRE_SCALE;
memset(&temp, 0, sizeof(temp));
fsep = xfer->pdev->privBeData;
fsep += xfer->trIndex;
temp.fs_open.max_bufsize = MaxBufSize;
temp.fs_open.max_frames = MaxFrameCount;
temp.fs_open.ep_index = xfer->trIndex;
temp.fs_open.ep_no = ep_no;
if (stream_id != 0) {
temp.fs_open_stream.stream_id = stream_id;
if (ioctl(xfer->pdev->file, USB_FS_OPEN_STREAM, &temp.fs_open_stream))
return (LIBUSB20_ERROR_INVALID_PARAM);
} else {
if (ioctl(xfer->pdev->file, USB_FS_OPEN, &temp.fs_open))
return (LIBUSB20_ERROR_INVALID_PARAM);
}
/* maximums might have changed - update */
xfer->maxFrames = temp.fs_open.max_frames;
/* "max_bufsize" should be multiple of "max_packet_length" */
xfer->maxTotalLength = temp.fs_open.max_bufsize;
xfer->maxPacketLen = temp.fs_open.max_packet_length;
/* setup buffer and length lists using zero copy */
fsep->ppBuffer = libusb20_pass_ptr(xfer->ppBuffer);
fsep->pLength = libusb20_pass_ptr(xfer->pLength);
return (0); /* success */
}
static int
ugen20_tr_close(struct libusb20_transfer *xfer)
{
struct usb_fs_close temp;
memset(&temp, 0, sizeof(temp));
temp.ep_index = xfer->trIndex;
if (ioctl(xfer->pdev->file, USB_FS_CLOSE, &temp)) {
return (LIBUSB20_ERROR_INVALID_PARAM);
}
return (0); /* success */
}
static int
ugen20_tr_clear_stall_sync(struct libusb20_transfer *xfer)
{
struct usb_fs_clear_stall_sync temp;
memset(&temp, 0, sizeof(temp));
/* if the transfer is active, an error will be returned */
temp.ep_index = xfer->trIndex;
if (ioctl(xfer->pdev->file, USB_FS_CLEAR_STALL_SYNC, &temp)) {
return (LIBUSB20_ERROR_INVALID_PARAM);
}
return (0); /* success */
}
static void
ugen20_tr_submit(struct libusb20_transfer *xfer)
{
struct usb_fs_start temp;
struct usb_fs_endpoint *fsep;
memset(&temp, 0, sizeof(temp));
fsep = xfer->pdev->privBeData;
fsep += xfer->trIndex;
fsep->nFrames = xfer->nFrames;
fsep->flags = 0;
if (!(xfer->flags & LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK)) {
fsep->flags |= USB_FS_FLAG_SINGLE_SHORT_OK;
}
if (!(xfer->flags & LIBUSB20_TRANSFER_MULTI_SHORT_NOT_OK)) {
fsep->flags |= USB_FS_FLAG_MULTI_SHORT_OK;
}
if (xfer->flags & LIBUSB20_TRANSFER_FORCE_SHORT) {
fsep->flags |= USB_FS_FLAG_FORCE_SHORT;
}
if (xfer->flags & LIBUSB20_TRANSFER_DO_CLEAR_STALL) {
fsep->flags |= USB_FS_FLAG_CLEAR_STALL;
}
/* NOTE: The "fsep->timeout" variable is 16-bit. */
if (xfer->timeout > 65535)
fsep->timeout = 65535;
else
fsep->timeout = xfer->timeout;
temp.ep_index = xfer->trIndex;
if (ioctl(xfer->pdev->file, USB_FS_START, &temp)) {
/* ignore any errors - should never happen */
}
return; /* success */
}
static void
ugen20_tr_cancel_async(struct libusb20_transfer *xfer)
{
struct usb_fs_stop temp;
memset(&temp, 0, sizeof(temp));
temp.ep_index = xfer->trIndex;
if (ioctl(xfer->pdev->file, USB_FS_STOP, &temp)) {
/* ignore any errors - should never happen */
}
return;
}
static int
ugen20_be_ioctl(uint32_t cmd, void *data)
{
int f;
int error;
f = open("/dev/" USB_DEVICE_NAME, O_RDONLY);
if (f < 0)
return (LIBUSB20_ERROR_OTHER);
error = ioctl(f, cmd, data);
if (error == -1) {
if (errno == EPERM) {
error = LIBUSB20_ERROR_ACCESS;
} else {
error = LIBUSB20_ERROR_OTHER;
}
}
close(f);
return (error);
}
static int
ugen20_dev_get_iface_desc(struct libusb20_device *pdev,
uint8_t iface_index, char *buf, uint8_t len)
{
struct usb_gen_descriptor ugd;
memset(&ugd, 0, sizeof(ugd));
ugd.ugd_data = libusb20_pass_ptr(buf);
ugd.ugd_maxlen = len;
ugd.ugd_iface_index = iface_index;
if (ioctl(pdev->file, USB_GET_IFACE_DRIVER, &ugd)) {
return (LIBUSB20_ERROR_INVALID_PARAM);
}
return (0);
}
static int
ugen20_dev_get_info(struct libusb20_device *pdev,
struct usb_device_info *pinfo)
{
if (ioctl(pdev->file, USB_GET_DEVICEINFO, pinfo)) {
return (LIBUSB20_ERROR_INVALID_PARAM);
}
return (0);
}
static int
ugen20_root_get_dev_quirk(struct libusb20_backend *pbe,
uint16_t quirk_index, struct libusb20_quirk *pq)
{
struct usb_gen_quirk q;
int error;
memset(&q, 0, sizeof(q));
q.index = quirk_index;
error = ugen20_be_ioctl(USB_DEV_QUIRK_GET, &q);
if (error) {
if (errno == EINVAL) {
return (LIBUSB20_ERROR_NOT_FOUND);
}
} else {
pq->vid = q.vid;
pq->pid = q.pid;
pq->bcdDeviceLow = q.bcdDeviceLow;
pq->bcdDeviceHigh = q.bcdDeviceHigh;
strlcpy(pq->quirkname, q.quirkname, sizeof(pq->quirkname));
}
return (error);
}
static int
ugen20_root_get_quirk_name(struct libusb20_backend *pbe, uint16_t quirk_index,
struct libusb20_quirk *pq)
{
struct usb_gen_quirk q;
int error;
memset(&q, 0, sizeof(q));
q.index = quirk_index;
error = ugen20_be_ioctl(USB_QUIRK_NAME_GET, &q);
if (error) {
if (errno == EINVAL) {
return (LIBUSB20_ERROR_NOT_FOUND);
}
} else {
strlcpy(pq->quirkname, q.quirkname, sizeof(pq->quirkname));
}
return (error);
}
static int
ugen20_root_add_dev_quirk(struct libusb20_backend *pbe,
struct libusb20_quirk *pq)
{
struct usb_gen_quirk q;
int error;
memset(&q, 0, sizeof(q));
q.vid = pq->vid;
q.pid = pq->pid;
q.bcdDeviceLow = pq->bcdDeviceLow;
q.bcdDeviceHigh = pq->bcdDeviceHigh;
strlcpy(q.quirkname, pq->quirkname, sizeof(q.quirkname));
error = ugen20_be_ioctl(USB_DEV_QUIRK_ADD, &q);
if (error) {
if (errno == ENOMEM) {
return (LIBUSB20_ERROR_NO_MEM);
}
}
return (error);
}
static int
ugen20_root_remove_dev_quirk(struct libusb20_backend *pbe,
struct libusb20_quirk *pq)
{
struct usb_gen_quirk q;
int error;
memset(&q, 0, sizeof(q));
q.vid = pq->vid;
q.pid = pq->pid;
q.bcdDeviceLow = pq->bcdDeviceLow;
q.bcdDeviceHigh = pq->bcdDeviceHigh;
strlcpy(q.quirkname, pq->quirkname, sizeof(q.quirkname));
error = ugen20_be_ioctl(USB_DEV_QUIRK_REMOVE, &q);
if (error) {
if (errno == EINVAL) {
return (LIBUSB20_ERROR_NOT_FOUND);
}
}
return (error);
}
static int
ugen20_root_set_template(struct libusb20_backend *pbe, int temp)
{
return (ugen20_be_ioctl(USB_SET_TEMPLATE, &temp));
}
static int
ugen20_root_get_template(struct libusb20_backend *pbe, int *ptemp)
{
return (ugen20_be_ioctl(USB_GET_TEMPLATE, ptemp));
}
|