forked from girnyau/cgminer-gc3355
-
Notifications
You must be signed in to change notification settings - Fork 9
/
usbutils.c
3797 lines (3166 loc) · 97.4 KB
/
usbutils.c
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
/*
* Copyright 2012-2013 Andrew Smith
* Copyright 2013 Con Kolivas <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version. See COPYING for more details.
*/
#include "config.h"
#include <ctype.h>
#include <stdint.h>
#include <stdbool.h>
#include "logging.h"
#include "miner.h"
#include "usbutils.h"
#define NODEV(err) ((err) == LIBUSB_ERROR_NO_DEVICE || \
(err) == LIBUSB_ERROR_PIPE || \
(err) == LIBUSB_ERROR_OTHER)
/* Timeout errors on writes are basically unrecoverable */
#define WRITENODEV(err) ((err) == LIBUSB_ERROR_TIMEOUT || NODEV(err))
#define NOCONTROLDEV(err) ((err) == LIBUSB_ERROR_NO_DEVICE || \
(err) == LIBUSB_ERROR_OTHER)
/*
* WARNING - these assume DEVLOCK(cgpu, pstate) is called first and
* DEVUNLOCK(cgpu, pstate) in called in the same function with the same pstate
* given to DEVLOCK.
* You must call DEVUNLOCK(cgpu, pstate) before exiting the function or it will leave
* the thread Cancelability unrestored
*/
#define DEVWLOCK(cgpu, _pth_state) do { \
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &_pth_state); \
cg_wlock(&cgpu->usbinfo.devlock); \
} while (0)
#define DEVWUNLOCK(cgpu, _pth_state) do { \
cg_wunlock(&cgpu->usbinfo.devlock); \
pthread_setcancelstate(_pth_state, NULL); \
} while (0)
#define DEVRLOCK(cgpu, _pth_state) do { \
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &_pth_state); \
cg_rlock(&cgpu->usbinfo.devlock); \
} while (0)
#define DEVRUNLOCK(cgpu, _pth_state) do { \
cg_runlock(&cgpu->usbinfo.devlock); \
pthread_setcancelstate(_pth_state, NULL); \
} while (0)
#define USB_CONFIG 1
#ifdef WIN32
#define GRIDSEED_TIMEOUT_MS 999
#define BFLSC_TIMEOUT_MS 999
#define BITFORCE_TIMEOUT_MS 999
#define BITFURY_TIMEOUT_MS 999
#define MODMINER_TIMEOUT_MS 999
#define AVALON_TIMEOUT_MS 999
#define KLONDIKE_TIMEOUT_MS 999
#define ICARUS_TIMEOUT_MS 999
#define HASHFAST_TIMEOUT_MS 999
/* The safety timeout we use, cancelling async transfers on windows that fail
* to timeout on their own. */
#define WIN_CALLBACK_EXTRA 40
#else
#define GRIDSEED_TIMEOUT_MS 100
#define BFLSC_TIMEOUT_MS 300
#define BITFORCE_TIMEOUT_MS 200
#define BITFURY_TIMEOUT_MS 100
#define MODMINER_TIMEOUT_MS 100
#define AVALON_TIMEOUT_MS 200
#define KLONDIKE_TIMEOUT_MS 200
#define ICARUS_TIMEOUT_MS 200
#define HASHFAST_TIMEOUT_MS 200
#endif
#define USB_EPS(_intx, _epinfosx) { \
.interface = _intx, \
.ctrl_transfer = _intx, \
.epinfo_count = ARRAY_SIZE(_epinfosx), \
.epinfos = _epinfosx \
}
#define USB_EPS_CTRL(_inty, _ctrlinty, _epinfosy) { \
.interface = _inty, \
.ctrl_transfer = _ctrlinty, \
.epinfo_count = ARRAY_SIZE(_epinfosy), \
.epinfos = _epinfosy \
}
/* Linked list of all async transfers in progress. Protected by cgusb_fd_lock.
* This allows us to not stop the usb polling thread till all are complete, and
* to find cancellable transfers. */
static struct list_head ut_list;
#ifdef USE_BFLSC
// N.B. transfer size is 512 with USB2.0, but only 64 with USB1.1
static struct usb_epinfo bas_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
};
static struct usb_intinfo bas_ints[] = {
USB_EPS(0, bas_epinfos)
};
#endif
#ifdef USE_BITFORCE
// N.B. transfer size is 512 with USB2.0, but only 64 with USB1.1
static struct usb_epinfo bfl_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
};
static struct usb_intinfo bfl_ints[] = {
USB_EPS(0, bfl_epinfos)
};
#endif
#ifdef USE_BITFURY
static struct usb_epinfo bfu0_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_INTERRUPT, 8, EPI(2), 0, 0 }
};
static struct usb_epinfo bfu1_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 16, EPI(3), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 16, EPO(4), 0, 0 }
};
/* Default to interface 1 */
static struct usb_intinfo bfu_ints[] = {
USB_EPS(1, bfu1_epinfos),
USB_EPS(0, bfu0_epinfos)
};
#endif
#ifdef USE_HASHFAST
#include "driver-hashfast.h"
static struct usb_epinfo hfa0_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_INTERRUPT, 8, EPI(3), 0, 0 }
};
static struct usb_epinfo hfa1_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
};
/* Default to interface 1 */
static struct usb_intinfo hfa_ints[] = {
USB_EPS(1, hfa1_epinfos),
USB_EPS(0, hfa0_epinfos)
};
#endif
#ifdef USE_GRIDSEED
static struct usb_epinfo gsd_epinfos[] = {
// { LIBUSB_TRANSFER_TYPE_INTERRUPT, 8, EPI(2), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(3), 0, 0 }
};
static struct usb_intinfo gsd_ints[] = {
USB_EPS(1, gsd_epinfos)
};
static struct usb_epinfo gsd1_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0, 0 }
};
static struct usb_intinfo gsd1_ints[] = {
USB_EPS(0, gsd1_epinfos)
};
static struct usb_epinfo gsd2_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 512, EPI(1), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 512, EPO(2), 0, 0 }
};
static struct usb_intinfo gsd2_ints[] = {
USB_EPS(0, gsd2_epinfos)
};
static struct usb_epinfo gsd3_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
};
static struct usb_intinfo gsd3_ints[] = {
USB_EPS(0, gsd3_epinfos)
};
#endif
#ifdef USE_MODMINER
static struct usb_epinfo mmq_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(3), 0, 0 }
};
static struct usb_intinfo mmq_ints[] = {
USB_EPS(1, mmq_epinfos)
};
#endif
#ifdef USE_AVALON
static struct usb_epinfo ava_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
};
static struct usb_intinfo ava_ints[] = {
USB_EPS(0, ava_epinfos)
};
#endif
#ifdef USE_KLONDIKE
static struct usb_epinfo kln_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0, 0 }
};
static struct usb_intinfo kln_ints[] = {
USB_EPS(0, kln_epinfos)
};
#endif
#ifdef USE_ICARUS
static struct usb_epinfo ica_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
};
static struct usb_intinfo ica_ints[] = {
USB_EPS(0, ica_epinfos)
};
static struct usb_epinfo amu_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0, 0 }
};
static struct usb_intinfo amu_ints[] = {
USB_EPS(0, amu_epinfos)
};
static struct usb_epinfo llt_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
};
static struct usb_intinfo llt_ints[] = {
USB_EPS(0, llt_epinfos)
};
static struct usb_epinfo cmr1_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
};
static struct usb_intinfo cmr1_ints[] = {
USB_EPS(0, cmr1_epinfos)
};
static struct usb_epinfo cmr2_epinfos0[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
};
static struct usb_epinfo cmr2_epinfos1[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(4), 0, 0 },
};
static struct usb_epinfo cmr2_epinfos2[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(5), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(6), 0, 0 },
};
static struct usb_epinfo cmr2_epinfos3[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(7), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(8), 0, 0 }
};
static struct usb_intinfo cmr2_ints[] = {
USB_EPS_CTRL(0, 1, cmr2_epinfos0),
USB_EPS_CTRL(1, 2, cmr2_epinfos1),
USB_EPS_CTRL(2, 3, cmr2_epinfos2),
USB_EPS_CTRL(3, 4, cmr2_epinfos3)
};
#endif
#define IDVENDOR_FTDI 0x0403
#define INTINFO(_ints) \
.intinfo_count = ARRAY_SIZE(_ints), \
.intinfos = _ints
#define USBEP(_usbdev, _intinfo, _epinfo) (_usbdev->found->intinfos[_intinfo].epinfos[_epinfo].ep)
#define THISIF(_found, _this) (_found->intinfos[_this].interface)
#define USBIF(_usbdev, _this) THISIF(_usbdev->found, _this)
// TODO: Add support for (at least) Isochronous endpoints
static struct usb_find_devices find_dev[] = {
#ifdef USE_BFLSC
{
.drv = DRIVER_bflsc,
.name = "BAS",
.ident = IDENT_BAS,
.idVendor = IDVENDOR_FTDI,
.idProduct = 0x6014,
//.iManufacturer = "Butterfly Labs",
.iProduct = "BitFORCE SHA256 SC",
.config = 1,
.timeout = BFLSC_TIMEOUT_MS,
.latency = LATENCY_STD,
INTINFO(bas_ints) },
#endif
#ifdef USE_BITFORCE
{
.drv = DRIVER_bitforce,
.name = "BFL",
.ident = IDENT_BFL,
.idVendor = IDVENDOR_FTDI,
.idProduct = 0x6014,
.iManufacturer = "Butterfly Labs Inc.",
.iProduct = "BitFORCE SHA256",
.config = 1,
.timeout = BITFORCE_TIMEOUT_MS,
.latency = LATENCY_STD,
INTINFO(bfl_ints) },
#endif
#ifdef USE_BITFURY
{
.drv = DRIVER_bitfury,
.name = "BF1",
.ident = IDENT_BFU,
.idVendor = 0x03eb,
.idProduct = 0x204b,
.config = 1,
.timeout = BITFURY_TIMEOUT_MS,
.latency = LATENCY_UNUSED,
//.iManufacturer = "BPMC",
.iProduct = "Bitfury BF1",
INTINFO(bfu_ints)
},
#endif
#ifdef USE_MODMINER
{
.drv = DRIVER_modminer,
.name = "MMQ",
.ident = IDENT_MMQ,
.idVendor = 0x1fc9,
.idProduct = 0x0003,
.config = 1,
.timeout = MODMINER_TIMEOUT_MS,
.latency = LATENCY_UNUSED,
INTINFO(mmq_ints) },
#endif
#ifdef USE_AVALON
{
.drv = DRIVER_avalon,
.name = "BTB",
.ident = IDENT_BTB,
.idVendor = IDVENDOR_FTDI,
.idProduct = 0x6001,
.iManufacturer = "Burnin Electronics",
.iProduct = "BitBurner",
.config = 1,
.timeout = AVALON_TIMEOUT_MS,
.latency = 10,
INTINFO(ava_ints) },
{
.drv = DRIVER_avalon,
.name = "BBF",
.ident = IDENT_BBF,
.idVendor = IDVENDOR_FTDI,
.idProduct = 0x6001,
.iManufacturer = "Burnin Electronics",
.iProduct = "BitBurner Fury",
.config = 1,
.timeout = AVALON_TIMEOUT_MS,
.latency = 10,
INTINFO(ava_ints) },
{
.drv = DRIVER_avalon,
.name = "AVA",
.ident = IDENT_AVA,
.idVendor = IDVENDOR_FTDI,
.idProduct = 0x6001,
.config = 1,
.timeout = AVALON_TIMEOUT_MS,
.latency = 10,
INTINFO(ava_ints) },
#endif
#ifdef USE_HASHFAST
{
.drv = DRIVER_hashfast,
.name = "HFA",
.ident = IDENT_HFA,
.idVendor = HF_USB_VENDOR_ID,
.idProduct = HF_USB_PRODUCT_ID_G1,
.iManufacturer = "HashFast LLC",
.iProduct = "M1 Module",
.config = 1,
.timeout = HASHFAST_TIMEOUT_MS,
.latency = LATENCY_UNUSED,
INTINFO(hfa_ints) },
#endif
#ifdef USE_KLONDIKE
{
.drv = DRIVER_klondike,
.name = "KLN",
.ident = IDENT_KLN,
.idVendor = 0x04D8,
.idProduct = 0xF60A,
.config = 1,
.timeout = KLONDIKE_TIMEOUT_MS,
.latency = 10,
INTINFO(kln_ints) },
#endif
#ifdef USE_GRIDSEED
{
.drv = DRIVER_gridseed,
.name = "GSD",
.ident = IDENT_GSD,
.idVendor = 0x0483,
.idProduct = 0x5740,
.iManufacturer = "STMicroelectronics",
.iProduct = "STM32 Virtual COM Port ",
.config = 1,
.timeout = GRIDSEED_TIMEOUT_MS,
.latency = LATENCY_STD,
INTINFO(gsd_ints) },
{
.drv = DRIVER_gridseed,
.name = "GSD",
.ident = IDENT_GSD1,
.idVendor = 0x10c4,
.idProduct = 0xea60,
.iProduct = "CP2102 USB to UART Bridge Controller",
.config = 1,
.timeout = GRIDSEED_TIMEOUT_MS,
.latency = LATENCY_STD,
INTINFO(gsd1_ints) },
{
.drv = DRIVER_gridseed,
.name = "GSD",
.ident = IDENT_GSD2,
.idVendor = IDVENDOR_FTDI,
.idProduct = 0x6010,
.iProduct = "Dual RS232-HS",
.config = 1,
.timeout = GRIDSEED_TIMEOUT_MS,
.latency = LATENCY_STD,
INTINFO(gsd2_ints) },
{
.drv = DRIVER_gridseed,
.name = "GSD",
.ident = IDENT_GSD3,
.idVendor = 0x067b,
.idProduct = 0x2303,
.iProduct = "USB-Serial Controller",
.config = 1,
.timeout = GRIDSEED_TIMEOUT_MS,
.latency = LATENCY_STD,
INTINFO(gsd3_ints) },
#endif
#ifdef USE_ICARUS
{
.drv = DRIVER_icarus,
.name = "ICA",
.ident = IDENT_ICA,
.idVendor = 0x067b,
.idProduct = 0x2303,
.config = 1,
.timeout = ICARUS_TIMEOUT_MS,
.latency = LATENCY_UNUSED,
INTINFO(ica_ints) },
{
.drv = DRIVER_icarus,
.name = "AMU",
.ident = IDENT_AMU,
.idVendor = 0x10c4,
.idProduct = 0xea60,
.config = 1,
.timeout = ICARUS_TIMEOUT_MS,
.latency = LATENCY_UNUSED,
INTINFO(amu_ints) },
{
.drv = DRIVER_icarus,
.name = "BLT",
.ident = IDENT_BLT,
.idVendor = IDVENDOR_FTDI,
.idProduct = 0x6001,
.iProduct = "FT232R USB UART",
.config = 1,
.timeout = ICARUS_TIMEOUT_MS,
.latency = LATENCY_STD,
INTINFO(llt_ints) },
// For any that don't match the above "BLT"
{
.drv = DRIVER_icarus,
.name = "LLT",
.ident = IDENT_LLT,
.idVendor = IDVENDOR_FTDI,
.idProduct = 0x6001,
.config = 1,
.timeout = ICARUS_TIMEOUT_MS,
.latency = LATENCY_STD,
INTINFO(llt_ints) },
{
.drv = DRIVER_icarus,
.name = "CMR",
.ident = IDENT_CMR1,
.idVendor = IDVENDOR_FTDI,
.idProduct = 0x6014,
.iProduct = "Cairnsmore1",
.config = 1,
.timeout = ICARUS_TIMEOUT_MS,
.latency = LATENCY_STD,
INTINFO(cmr1_ints) },
{
.drv = DRIVER_icarus,
.name = "CMR",
.ident = IDENT_CMR2,
.idVendor = IDVENDOR_FTDI,
.idProduct = 0x8350,
.iProduct = "Cairnsmore1",
.config = 1,
.timeout = ICARUS_TIMEOUT_MS,
.latency = LATENCY_STD,
INTINFO(cmr2_ints) },
#endif
{ DRIVER_MAX, NULL, 0, 0, 0, NULL, NULL, 0, 0, 0, 0, NULL }
};
#define STRBUFLEN 256
static const char *BLANK = "";
static const char *space = " ";
static const char *nodatareturned = "no data returned ";
#define IOERR_CHECK(cgpu, err) \
if (err == LIBUSB_ERROR_IO) { \
cgpu->usbinfo.ioerr_count++; \
cgpu->usbinfo.continuous_ioerr_count++; \
} else { \
cgpu->usbinfo.continuous_ioerr_count = 0; \
}
/* Timeout errors on writes are unusual and should be treated as IO errors. */
#define WRITEIOERR_CHECK(cgpu, err) \
if (err == LIBUSB_ERROR_IO || err == LIBUSB_ERROR_TIMEOUT) { \
cgpu->usbinfo.ioerr_count++; \
cgpu->usbinfo.continuous_ioerr_count++; \
} else { \
cgpu->usbinfo.continuous_ioerr_count = 0; \
}
#if 0 // enable USBDEBUG - only during development testing
static const char *debug_true_str = "true";
static const char *debug_false_str = "false";
static const char *nodevstr = "=NODEV";
#define bool_str(boo) ((boo) ? debug_true_str : debug_false_str)
#define isnodev(err) (NODEV(err) ? nodevstr : BLANK)
#define USBDEBUG(fmt, ...) applog(LOG_WARNING, fmt, ##__VA_ARGS__)
#else
#define USBDEBUG(fmt, ...)
#endif
// For device limits by driver
static struct driver_count {
uint32_t count;
uint32_t limit;
} drv_count[DRIVER_MAX];
// For device limits by list of bus/dev
static struct usb_busdev {
int bus_number;
int device_address;
void *resource1;
void *resource2;
} *busdev;
static int busdev_count = 0;
// Total device limit
static int total_count = 0;
static int total_limit = 999999;
struct usb_in_use_list {
struct usb_busdev in_use;
struct usb_in_use_list *prev;
struct usb_in_use_list *next;
};
// List of in use devices
static struct usb_in_use_list *in_use_head = NULL;
struct resource_work {
bool lock;
const char *dname;
uint8_t bus_number;
uint8_t device_address;
struct resource_work *next;
};
// Pending work for the reslock thread
struct resource_work *res_work_head = NULL;
struct resource_reply {
uint8_t bus_number;
uint8_t device_address;
bool got;
struct resource_reply *next;
};
// Replies to lock requests
struct resource_reply *res_reply_head = NULL;
// Some stats need to always be defined
#define SEQ0 0
#define SEQ1 1
// NONE must be 0 - calloced
#define MODE_NONE 0
#define MODE_CTRL_READ (1 << 0)
#define MODE_CTRL_WRITE (1 << 1)
#define MODE_BULK_READ (1 << 2)
#define MODE_BULK_WRITE (1 << 3)
// Set this to 0 to remove stats processing
#define DO_USB_STATS 1
static bool stats_initialised = false;
#if DO_USB_STATS
#define MODE_SEP_STR "+"
#define MODE_NONE_STR "X"
#define MODE_CTRL_READ_STR "cr"
#define MODE_CTRL_WRITE_STR "cw"
#define MODE_BULK_READ_STR "br"
#define MODE_BULK_WRITE_STR "bw"
// One for each CMD, TIMEOUT, ERROR
struct cg_usb_stats_item {
uint64_t count;
double total_delay;
double min_delay;
double max_delay;
struct timeval first;
struct timeval last;
};
#define CMD_CMD 0
#define CMD_TIMEOUT 1
#define CMD_ERROR 2
// One for each C_CMD
struct cg_usb_stats_details {
int seq;
uint32_t modes;
struct cg_usb_stats_item item[CMD_ERROR+1];
};
// One for each device
struct cg_usb_stats {
char *name;
int device_id;
struct cg_usb_stats_details *details;
};
static struct cg_usb_stats *usb_stats = NULL;
static int next_stat = USB_NOSTAT;
#define SECTOMS(s) ((int)((s) * 1000))
#define USB_STATS(sgpu_, sta_, fin_, err_, mode_, cmd_, seq_, tmo_) \
stats(sgpu_, sta_, fin_, err_, mode_, cmd_, seq_, tmo_)
#define STATS_TIMEVAL(tv_) cgtime(tv_)
#define USB_REJECT(sgpu_, mode_) rejected_inc(sgpu_, mode_)
#else
#define USB_STATS(sgpu_, sta_, fin_, err_, mode_, cmd_, seq_, tmo_)
#define STATS_TIMEVAL(tv_)
#define USB_REJECT(sgpu_, mode_)
#endif // DO_USB_STATS
/* Create usb_commands array from USB_PARSE_COMMANDS macro in usbutils.h */
char *usb_commands[] = {
USB_PARSE_COMMANDS(JUMPTABLE)
"Null"
};
#ifdef EOL
#undef EOL
#endif
#define EOL "\n"
static const char *DESDEV = "Device";
static const char *DESCON = "Config";
static const char *DESSTR = "String";
static const char *DESINT = "Interface";
static const char *DESEP = "Endpoint";
static const char *DESHID = "HID";
static const char *DESRPT = "Report";
static const char *DESPHY = "Physical";
static const char *DESHUB = "Hub";
static const char *EPIN = "In: ";
static const char *EPOUT = "Out: ";
static const char *EPX = "?: ";
static const char *CONTROL = "Control";
static const char *ISOCHRONOUS_X = "Isochronous+?";
static const char *ISOCHRONOUS_N_X = "Isochronous+None+?";
static const char *ISOCHRONOUS_N_D = "Isochronous+None+Data";
static const char *ISOCHRONOUS_N_F = "Isochronous+None+Feedback";
static const char *ISOCHRONOUS_N_I = "Isochronous+None+Implicit";
static const char *ISOCHRONOUS_A_X = "Isochronous+Async+?";
static const char *ISOCHRONOUS_A_D = "Isochronous+Async+Data";
static const char *ISOCHRONOUS_A_F = "Isochronous+Async+Feedback";
static const char *ISOCHRONOUS_A_I = "Isochronous+Async+Implicit";
static const char *ISOCHRONOUS_D_X = "Isochronous+Adaptive+?";
static const char *ISOCHRONOUS_D_D = "Isochronous+Adaptive+Data";
static const char *ISOCHRONOUS_D_F = "Isochronous+Adaptive+Feedback";
static const char *ISOCHRONOUS_D_I = "Isochronous+Adaptive+Implicit";
static const char *ISOCHRONOUS_S_X = "Isochronous+Sync+?";
static const char *ISOCHRONOUS_S_D = "Isochronous+Sync+Data";
static const char *ISOCHRONOUS_S_F = "Isochronous+Sync+Feedback";
static const char *ISOCHRONOUS_S_I = "Isochronous+Sync+Implicit";
static const char *BULK = "Bulk";
static const char *INTERRUPT = "Interrupt";
static const char *UNKNOWN = "Unknown";
static const char *destype(uint8_t bDescriptorType)
{
switch (bDescriptorType) {
case LIBUSB_DT_DEVICE:
return DESDEV;
case LIBUSB_DT_CONFIG:
return DESCON;
case LIBUSB_DT_STRING:
return DESSTR;
case LIBUSB_DT_INTERFACE:
return DESINT;
case LIBUSB_DT_ENDPOINT:
return DESEP;
case LIBUSB_DT_HID:
return DESHID;
case LIBUSB_DT_REPORT:
return DESRPT;
case LIBUSB_DT_PHYSICAL:
return DESPHY;
case LIBUSB_DT_HUB:
return DESHUB;
}
return UNKNOWN;
}
static const char *epdir(uint8_t bEndpointAddress)
{
switch (bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) {
case LIBUSB_ENDPOINT_IN:
return EPIN;
case LIBUSB_ENDPOINT_OUT:
return EPOUT;
}
return EPX;
}
static const char *epatt(uint8_t bmAttributes)
{
switch(bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) {
case LIBUSB_TRANSFER_TYPE_CONTROL:
return CONTROL;
case LIBUSB_TRANSFER_TYPE_BULK:
return BULK;
case LIBUSB_TRANSFER_TYPE_INTERRUPT:
return INTERRUPT;
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
switch(bmAttributes & LIBUSB_ISO_SYNC_TYPE_MASK) {
case LIBUSB_ISO_SYNC_TYPE_NONE:
switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
case LIBUSB_ISO_USAGE_TYPE_DATA:
return ISOCHRONOUS_N_D;
case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
return ISOCHRONOUS_N_F;
case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
return ISOCHRONOUS_N_I;
}
return ISOCHRONOUS_N_X;
case LIBUSB_ISO_SYNC_TYPE_ASYNC:
switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
case LIBUSB_ISO_USAGE_TYPE_DATA:
return ISOCHRONOUS_A_D;
case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
return ISOCHRONOUS_A_F;
case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
return ISOCHRONOUS_A_I;
}
return ISOCHRONOUS_A_X;
case LIBUSB_ISO_SYNC_TYPE_ADAPTIVE:
switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
case LIBUSB_ISO_USAGE_TYPE_DATA:
return ISOCHRONOUS_D_D;
case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
return ISOCHRONOUS_D_F;
case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
return ISOCHRONOUS_D_I;
}
return ISOCHRONOUS_D_X;
case LIBUSB_ISO_SYNC_TYPE_SYNC:
switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
case LIBUSB_ISO_USAGE_TYPE_DATA:
return ISOCHRONOUS_S_D;
case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
return ISOCHRONOUS_S_F;
case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
return ISOCHRONOUS_S_I;
}
return ISOCHRONOUS_S_X;
}
return ISOCHRONOUS_X;
}
return UNKNOWN;
}
static void append(char **buf, char *append, size_t *off, size_t *len)
{
int new = strlen(append);
if ((new + *off) >= *len)
{
*len *= 2;
*buf = realloc(*buf, *len);
if (unlikely(!*buf))
quit(1, "USB failed to realloc append");
}
strcpy(*buf + *off, append);
*off += new;
}
static bool setgetdes(ssize_t count, libusb_device *dev, struct libusb_device_handle *handle, struct libusb_config_descriptor **config, int cd, char **buf, size_t *off, size_t *len)
{
char tmp[512];
int err;
err = libusb_set_configuration(handle, cd);
if (err) {
snprintf(tmp, sizeof(tmp), EOL " ** dev %d: Failed to set config descriptor to %d, err %d",
(int)count, cd, err);
append(buf, tmp, off, len);
return false;
}
err = libusb_get_active_config_descriptor(dev, config);
if (err) {
snprintf(tmp, sizeof(tmp), EOL " ** dev %d: Failed to get active config descriptor set to %d, err %d",
(int)count, cd, err);
append(buf, tmp, off, len);
return false;
}
snprintf(tmp, sizeof(tmp), EOL " ** dev %d: Set & Got active config descriptor to %d, err %d",
(int)count, cd, err);
append(buf, tmp, off, len);
return true;
}
static void usb_full(ssize_t *count, libusb_device *dev, char **buf, size_t *off, size_t *len, int level)
{
struct libusb_device_descriptor desc;
uint8_t bus_number;
uint8_t device_address;
struct libusb_device_handle *handle;
struct libusb_config_descriptor *config;
const struct libusb_interface_descriptor *idesc;
const struct libusb_endpoint_descriptor *epdesc;
unsigned char man[STRBUFLEN+1];
unsigned char prod[STRBUFLEN+1];
unsigned char ser[STRBUFLEN+1];
char tmp[512];
int err, i, j, k;
err = libusb_get_device_descriptor(dev, &desc);
if (opt_usb_list_all && err) {
snprintf(tmp, sizeof(tmp), EOL ".USB dev %d: Failed to get descriptor, err %d",
(int)(++(*count)), err);
append(buf, tmp, off, len);
return;
}
bus_number = libusb_get_bus_number(dev);
device_address = libusb_get_device_address(dev);
if (!opt_usb_list_all) {
bool known = false;
for (i = 0; find_dev[i].drv != DRIVER_MAX; i++)
if ((find_dev[i].idVendor == desc.idVendor) &&
(find_dev[i].idProduct == desc.idProduct)) {
known = true;
break;
}
if (!known)
return;
}
(*count)++;
if (level == 0) {
snprintf(tmp, sizeof(tmp), EOL ".USB dev %d: Bus %d Device %d ID: %04x:%04x",
(int)(*count), (int)bus_number, (int)device_address,
desc.idVendor, desc.idProduct);
} else {
snprintf(tmp, sizeof(tmp), EOL ".USB dev %d: Bus %d Device %d Device Descriptor:" EOL "\tLength: %d" EOL
"\tDescriptor Type: %s" EOL "\tUSB: %04x" EOL "\tDeviceClass: %d" EOL
"\tDeviceSubClass: %d" EOL "\tDeviceProtocol: %d" EOL "\tMaxPacketSize0: %d" EOL
"\tidVendor: %04x" EOL "\tidProduct: %04x" EOL "\tDeviceRelease: %x" EOL
"\tNumConfigurations: %d",
(int)(*count), (int)bus_number, (int)device_address,
(int)(desc.bLength), destype(desc.bDescriptorType),
desc.bcdUSB, (int)(desc.bDeviceClass), (int)(desc.bDeviceSubClass),
(int)(desc.bDeviceProtocol), (int)(desc.bMaxPacketSize0),
desc.idVendor, desc.idProduct, desc.bcdDevice,
(int)(desc.bNumConfigurations));
}
append(buf, tmp, off, len);
err = libusb_open(dev, &handle);
if (err) {
snprintf(tmp, sizeof(tmp), EOL " ** dev %d: Failed to open, err %d", (int)(*count), err);
append(buf, tmp, off, len);
return;
}
err = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, man, STRBUFLEN);
if (err < 0)
snprintf((char *)man, sizeof(man), "** err:(%d) %s", err, libusb_error_name(err));
err = libusb_get_string_descriptor_ascii(handle, desc.iProduct, prod, STRBUFLEN);
if (err < 0)
snprintf((char *)prod, sizeof(prod), "** err:(%d) %s", err, libusb_error_name(err));
if (level == 0) {
libusb_close(handle);
snprintf(tmp, sizeof(tmp), EOL " Manufacturer: '%s'" EOL " Product: '%s'", man, prod);
append(buf, tmp, off, len);
return;
}
if (libusb_kernel_driver_active(handle, 0) == 1) {
snprintf(tmp, sizeof(tmp), EOL " * dev %d: kernel attached", (int)(*count));
append(buf, tmp, off, len);
}
err = libusb_get_active_config_descriptor(dev, &config);
if (err) {
if (!setgetdes(*count, dev, handle, &config, 1, buf, off, len)
&& !setgetdes(*count, dev, handle, &config, 0, buf, off, len)) {
libusb_close(handle);
snprintf(tmp, sizeof(tmp), EOL " ** dev %d: Failed to set config descriptor to %d or %d",
(int)(*count), 1, 0);
append(buf, tmp, off, len);
return;
}
}
snprintf(tmp, sizeof(tmp), EOL " dev %d: Active Config:" EOL "\tDescriptorType: %s" EOL
"\tNumInterfaces: %d" EOL "\tConfigurationValue: %d" EOL
"\tAttributes: %d" EOL "\tMaxPower: %d",
(int)(*count), destype(config->bDescriptorType),
(int)(config->bNumInterfaces), (int)(config->iConfiguration),
(int)(config->bmAttributes), (int)(config->MaxPower));
append(buf, tmp, off, len);
for (i = 0; i < (int)(config->bNumInterfaces); i++) {
for (j = 0; j < config->interface[i].num_altsetting; j++) {
idesc = &(config->interface[i].altsetting[j]);
snprintf(tmp, sizeof(tmp), EOL " _dev %d: Interface Descriptor %d:" EOL
"\tDescriptorType: %s" EOL "\tInterfaceNumber: %d" EOL
"\tNumEndpoints: %d" EOL "\tInterfaceClass: %d" EOL
"\tInterfaceSubClass: %d" EOL "\tInterfaceProtocol: %d",
(int)(*count), j, destype(idesc->bDescriptorType),
(int)(idesc->bInterfaceNumber),
(int)(idesc->bNumEndpoints),
(int)(idesc->bInterfaceClass),
(int)(idesc->bInterfaceSubClass),