Commit d5b8d73fcc0426d1ea6fbce8d4b146476db55530

Kano 2014-03-02T00:13:59

Enable Bitmain Ant S1 code and make it conform to cgminer requirements

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
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
diff --git a/01-cgminer.rules b/01-cgminer.rules
index d2fa52a..feb432e 100644
--- a/01-cgminer.rules
+++ b/01-cgminer.rules
@@ -37,3 +37,6 @@ ATTRS{idVendor}=="198c", ATTRS{idProduct}=="b1f1", SUBSYSTEM=="usb", ACTION=="ad
 
 # NF1
 ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="00de", SUBSYSTEM=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
+
+# ANT_S1
+ATTRS{idVendor}=="4254", ATTRS{idProduct}=="4153", SUBSYSTEM=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
diff --git a/ASIC-README b/ASIC-README
index 25de102..058d8a6 100644
--- a/ASIC-README
+++ b/ASIC-README
@@ -12,6 +12,7 @@ Currently supported devices include:
 - Hashfast Babyjet and Sierra
 - Antminer U1 USB
 - Cointerra
+- Antminer S1
 
 No COM ports on windows or TTY devices will be used by cgminer as it
 communicates directly with them via USB so it is normal for them to not exist or
@@ -84,7 +85,7 @@ HFS: Hashfast Sierra
 HFA: Hashfast non standard (eg. a Babyjet with an added board)
 
 
-ANTMINER devices
+ANTMINER U1 devices
 
 Antminer devices need the --enable-icarus option when compiling cgminer.
 
@@ -93,6 +94,15 @@ are also set up as per the USB ASICs below. They need no options to work well
 but will accept all the icarus options.
 
 
+ANTMINER S1 devices
+
+Antminer S1 devices need the --enable-ants1 option when compiling cgminer.
+
+They are custom OpenWRT linux devices
+
+They are recognised with the name ANT
+
+
 BITMINE A1 devices
 
 Bitmine A1 devices need the --enable-bitmine_A1 compile option set.
@@ -192,6 +202,13 @@ ASIC SPECIFIC COMMANDS
 --bitburner-fury-options <arg> Override avalon-options for BitBurner Fury boards baud:miners:asic:timeout:freq
 --bitburner-fury-voltage <arg> Set BitBurner Fury core voltage, in millivolts
 --bitburner-voltage <arg> Set BitBurner (Avalon) core voltage, in millivolts
+--bitmain-auto      Adjust bitmain overclock frequency dynamically for best hashrate
+--bitmain-cutoff    Set bitmain overheat cut off temperature
+--bitmain-fan       Set fanspeed percentage for bitmain, single value or range (default: 20-100)
+--bitmain-freq      Set frequency range for bitmain-auto, single value or range
+--bitmain-hwerror   Set bitmain device detect hardware error
+--bitmain-options   Set bitmain options baud:miners:asic:timeout:freq
+--bitmain-temp      Set bitmain target temperature
 --bxf-temp-target <arg> Set target temperature for BXF devices (default: 82)
 --hfa-hash-clock <arg> Set hashfast clock speed (default: 550)
 --hfa-fail-drop <arg> Set how many MHz to drop clockspeed each failure on an overlocked hashfast device (default: 10)
@@ -203,6 +220,21 @@ ASIC SPECIFIC COMMANDS
 --klondike-options <arg> Set klondike options clock:temptarget
 
 
+ANTMINER S1 DEVICES
+
+--bitmain-auto      Adjust bitmain overclock frequency dynamically for best hashrate
+--bitmain-cutoff    Set bitmain overheat cut off temperature
+--bitmain-fan       Set fanspeed percentage for bitmain, single value or range (default: 20-100)
+--bitmain-freq      Set frequency range for bitmain-auto, single value or range
+--bitmain-hwerror   Set bitmain device detect hardware error
+--bitmain-options   Set bitmain options baud:miners:asic:timeout:freq
+--bitmain-temp      Set bitmain target temperature
+
+The Antminer S1 device comes with it's own operating system and a preinstalled
+version of cgminer as part of the flash firmware. No configuration should be
+necessary.
+
+
 ANTMINER U1 DEVICES
 
 --anu-freq <arg>    Set AntminerU1 frequency in MHz, range 150-500 (default: 200)
diff --git a/Makefile.am b/Makefile.am
index cc81a8f..658bfa2 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -116,6 +116,10 @@ if HAS_MINION
 cgminer_SOURCES += driver-minion.c
 endif
 
+if HAS_ANT_S1
+cgminer_SOURCES += driver-bitmain.c driver-bitmain.h
+endif
+
 if HAS_MODMINER
 cgminer_SOURCES += driver-modminer.c
 bitstreamsdir = $(bindir)/bitstreams
diff --git a/README b/README
index 56b6f3e..3cecbce 100644
--- a/README
+++ b/README
@@ -105,6 +105,8 @@ If building on Ubuntu:
 	sudo apt-get install build-essential autoconf automake libtool libcurl3-dev libudev-dev
 
 CGMiner specific configuration options:
+  --enable-ants1          Compile support for Antminer S1 Bitmain (default
+                          disabled)
   --enable-avalon         Compile support for Avalon (default disabled)
   --enable-bab            Compile support for BlackArrow Bitfury (default
                           disabled)
@@ -119,8 +121,6 @@ CGMiner specific configuration options:
   --enable-hashfast       Compile support for Hashfast (default disabled)
   --enable-icarus         Compile support for Icarus (default disabled)
   --enable-knc            Compile support for KnC miners (default disabled)
-  --enable-bab            Compile support for BlackArrow Bitfury (default disabled)
-  --enable-minion         Compile support for BlackArrow Minion ASIC (default disabled)
   --enable-klondike       Compile support for Klondike (default disabled)
   --enable-knc            Compile support for KnC miners (default disabled)
   --enable-minion         Compile support for Minion BlackArrow ASIC (default
@@ -180,6 +180,13 @@ Options for both config file and command line:
 --bitburner-fury-voltage <arg> Set BitBurner Fury core voltage, in millivolts
 --bitburner-fury-options <arg> Override avalon-options for BitBurner Fury boards baud:miners:asic:timeout:freq
 --bitburner-voltage <arg> Set BitBurner (Avalon) core voltage, in millivolts
+--bitmain-auto      Adjust bitmain overclock frequency dynamically for best hashrate
+--bitmain-cutoff    Set bitmain overheat cut off temperature
+--bitmain-fan       Set fanspeed percentage for bitmain, single value or range (default: 20-100)
+--bitmain-freq      Set frequency range for bitmain-auto, single value or range
+--bitmain-hwerror   Set bitmain device detect hardware error
+--bitmain-options   Set bitmain options baud:miners:asic:timeout:freq
+--bitmain-temp      Set bitmain target temperature
 --bxf-temp-target <arg> Set target temperature for BXF devices (default: 82)
 --compact           Use compact display without per device statistics
 --debug|-D          Enable debug output
diff --git a/api.c b/api.c
index e3dc72d..3ea0e3e 100644
--- a/api.c
+++ b/api.c
@@ -29,7 +29,8 @@
 #if defined(USE_BFLSC) || defined(USE_AVALON) || defined(USE_AVALON2) || \
 	defined(USE_HASHFAST) || defined(USE_BITFURY) || defined(USE_KLONDIKE) || \
 	defined(USE_KNC) || defined(USE_BAB) || defined(USE_DRILLBIT) || \
-	defined(USE_MINION) || defined(USE_COINTERRA) || defined(USE_BITMINE_A1)
+	defined(USE_MINION) || defined(USE_COINTERRA) || defined(USE_BITMINE_A1) || \
+	defined(USE_ANT_S1)
 #define HAVE_AN_ASIC 1
 #endif
 
@@ -153,6 +154,9 @@ static const char *FALSESTR = "false";
 static const char *SHA256STR = "sha256";
 
 static const char *DEVICECODE = ""
+#ifdef USE_ANT_S1
+			"ANT "
+#endif
 #ifdef USE_AVALON
 			"AVA "
 #endif
diff --git a/cgminer.c b/cgminer.c
index b9b8591..c2d56e7 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -88,6 +88,10 @@ char *curly = ":D";
 #include "driver-hashfast.h"
 #endif
 
+#ifdef USE_ANT_S1
+#include "driver-bitmain.h"
+#endif
+
 #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_AVALON) || defined(USE_AVALON2) || defined(USE_MODMINER)
 #	define USE_FPGA
 #endif
@@ -209,6 +213,9 @@ char *opt_bab_options = NULL;
 #ifdef USE_BITMINE_A1
 char *opt_bitmine_a1_options = NULL;
 #endif
+#ifdef USE_ANT_S1
+char *opt_bitmain_options = NULL;
+#endif
 #ifdef USE_USBUTILS
 char *opt_usb_select = NULL;
 int opt_usbdump = -1;
@@ -360,7 +367,7 @@ struct sigaction termhandler, inthandler;
 
 struct thread_q *getq;
 
-static int total_work;
+static uint32_t total_work;
 struct work *staged_work = NULL;
 
 struct schedtime {
@@ -1252,6 +1259,29 @@ static struct opt_table opt_config_table[] = {
 		     set_bitburner_fury_options, NULL, NULL,
 		     "Override avalon-options for BitBurner Fury boards baud:miners:asic:timeout:freq"),
 #endif
+#ifdef USE_ANT_S1
+	OPT_WITHOUT_ARG("--bitmain-auto",
+			opt_set_bool, &opt_bitmain_auto,
+			"Adjust bitmain overclock frequency dynamically for best hashrate"),
+	OPT_WITH_ARG("--bitmain-cutoff",
+		     set_int_0_to_100, opt_show_intval, &opt_bitmain_overheat,
+		     "Set bitmain overheat cut off temperature"),
+	OPT_WITH_ARG("--bitmain-fan",
+		     set_bitmain_fan, NULL, NULL,
+		     "Set fanspeed percentage for bitmain, single value or range (default: 20-100)"),
+	OPT_WITH_ARG("--bitmain-freq",
+		     set_bitmain_freq, NULL, NULL,
+		     "Set frequency range for bitmain-auto, single value or range"),
+	OPT_WITHOUT_ARG("--bitmain-hwerror",
+			opt_set_bool, &opt_bitmain_hwerror,
+			"Set bitmain device detect hardware error"),
+	OPT_WITH_ARG("--bitmain-options",
+		     opt_set_charp, NULL, &opt_bitmain_options,
+		     "Set bitmain options baud:miners:asic:timeout:freq"),
+	OPT_WITH_ARG("--bitmain-temp",
+		     set_int_0_to_100, opt_show_intval, &opt_bitmain_temp,
+		     "Set bitmain target temperature"),
+#endif
 #ifdef USE_BITMINE_A1
 	OPT_WITH_ARG("--bitmine-a1-options",
 		     set_bitmine_a1_options, NULL, NULL,
@@ -1644,6 +1674,9 @@ extern const char *opt_argv0;
 static char *opt_verusage_and_exit(const char *extra)
 {
 	printf("%s\nBuilt with "
+#ifdef USE_ANT_S1
+		"ant.S1 "
+#endif
 #ifdef USE_AVALON
 		"avalon "
 #endif
@@ -3829,7 +3862,7 @@ static char *offset_ntime(const char *ntime, int noffset)
  * prevent a copied work struct from freeing ram belonging to another struct */
 static void _copy_work(struct work *work, const struct work *base_work, int noffset)
 {
-	int id = work->id;
+	uint32_t id = work->id;
 
 	clean_work(work);
 	memcpy(work, base_work, sizeof(struct work));
@@ -7024,6 +7057,35 @@ struct work *clone_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate
 	return ret;
 }
 
+/* This function is for finding an already queued work item in the
+ * given que hashtable. Code using this function must be able
+ * to handle NULL as a return which implies there is no matching work.
+ * The calling function must lock access to the que if it is required. */
+struct work *__find_work_byid(struct work *que, uint32_t id)
+{
+	struct work *work, *tmp, *ret = NULL;
+
+	HASH_ITER(hh, que, work, tmp) {
+		if (work->id == id) {
+			ret = work;
+			break;
+		}
+	}
+
+	return ret;
+}
+
+struct work *find_queued_work_byid(struct cgpu_info *cgpu, uint32_t id)
+{
+	struct work *ret;
+
+	rd_lock(&cgpu->qlock);
+	ret = __find_work_byid(cgpu->queued_work, id);
+	rd_unlock(&cgpu->qlock);
+
+	return ret;
+}
+
 void __work_completed(struct cgpu_info *cgpu, struct work *work)
 {
 	cgpu->queued_count--;
diff --git a/configure.ac b/configure.ac
index 260bf41..77adfab 100644
--- a/configure.ac
+++ b/configure.ac
@@ -126,6 +126,17 @@ if test "x$has_winpthread" != xtrue; then
         PTHREAD_LIBS=-lpthread
 fi
 
+ants1="no"
+
+AC_ARG_ENABLE([ants1],
+	[AC_HELP_STRING([--enable-ants1],[Compile support for Antminer S1 Bitmain (default disabled)])],
+	[ants1=$enableval]
+	)
+if test "x$ants1" = xyes; then
+	AC_DEFINE([USE_ANT_S1], [1], [Defined to 1 if Antminer S1 Bitmain support is wanted])
+fi
+AM_CONDITIONAL([HAS_ANT_S1], [test x$ants1 = xyes])
+
 avalon="no"
 
 AC_ARG_ENABLE([avalon],
@@ -317,7 +328,7 @@ else
 	])
 fi
 
-if test x$avalon$bitforce$bitfury$modminer$bflsc$icarus$hashfast$klondike$drillbit$cointerra != xnononononononononono; then
+if test x$avalon$bitforce$bitfury$modminer$bflsc$icarus$hashfast$klondike$drillbit$cointerra$ants1 != xnonononononononononono; then
 	want_usbutils=true
 else
 	want_usbutils=false
@@ -497,6 +508,12 @@ echo "  curses.TUI...........: $cursesmsg"
 
 
 echo
+if test "x$ants1" = xyes; then
+	echo "  Antminer.S1.Bitmain..: Enabled"
+else
+	echo "  Antminer.S1.Bitmain..: Disabled"
+fi
+
 if test "x$avalon" = xyes; then
 	echo "  Avalon.ASICs.........: Enabled"
 else
@@ -587,7 +604,7 @@ else
 	echo "  ModMiner.FPGAs.......: Disabled"
 fi
 
-if test "x$avalon$avalon2$bab$bflsc$bitforce$bitfury$hashfast$icarus$klondike$knc$modminer$drillbit$minion$cointerra$bitmine_A1" = xnonononononononononononononono; then
+if test "x$avalon$avalon2$bab$bflsc$bitforce$bitfury$hashfast$icarus$klondike$knc$modminer$drillbit$minion$cointerra$bitmine_A1$ants1" = xnononononononononononononononono; then
 	AC_MSG_ERROR([No mining configured in])
 fi
 
diff --git a/driver-bitmain.c b/driver-bitmain.c
index 184d5f1..3a5d3ae 100644
--- a/driver-bitmain.c
+++ b/driver-bitmain.c
@@ -1,5 +1,6 @@
 /*
  * Copyright 2012-2013 Lingchao Xu <lingchao.xu@bitmaintech.com>
+ * Copyright 2014 Andrew Smith
  *
  * 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
@@ -8,7 +9,16 @@
  */
 
 #include "config.h"
+#include "compat.h"
+#include "miner.h"
 
+#ifndef LINUX
+static void ants1_detect(__maybe_unused bool hotplug)
+{
+}
+#else
+
+/*
 #include <limits.h>
 #include <pthread.h>
 #include <stdio.h>
@@ -16,30 +26,22 @@
 #include <sys/types.h>
 #include <dirent.h>
 #include <unistd.h>
-#ifndef WIN32
-  #include <sys/select.h>
-  #include <termios.h>
-  #include <sys/stat.h>
-  #include <fcntl.h>
-  #ifndef O_CLOEXEC
-    #define O_CLOEXEC 0
-  #endif
-#else
-  #include "compat.h"
-  #include <windows.h>
-  #include <io.h>
+#include <sys/select.h>
+#include <termios.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 0
 #endif
+*/
 
 #include "elist.h"
-#include "miner.h"
 #include "usbutils.h"
 #include "driver-bitmain.h"
 #include "hexdump.c"
 #include "util.h"
 
-char opt_bitmain_dev[256] = {0};
 bool opt_bitmain_hwerror = false;
-bool opt_bitmain_dev_usb = true;
 int opt_bitmain_temp = BITMAIN_TEMP_TARGET;
 int opt_bitmain_overheat = BITMAIN_TEMP_OVERHEAT;
 int opt_bitmain_fan_min = BITMAIN_DEFAULT_FAN_MIN_PWM;
@@ -50,10 +52,48 @@ bool opt_bitmain_auto;
 
 static int option_offset = -1;
 
+static unsigned char bit_swap_table[256] =
+{
+  0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
+  0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
+  0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
+  0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
+  0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
+  0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
+  0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
+  0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
+  0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
+  0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
+  0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
+  0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
+  0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
+  0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
+  0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
+  0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
+  0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
+  0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
+  0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
+  0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
+  0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
+  0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
+  0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
+  0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
+  0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
+  0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
+  0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
+  0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
+  0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
+  0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
+  0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
+  0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
+};
+
+#define bitswap(x) (bit_swap_table[x])
+
 // --------------------------------------------------------------
 //      CRC16 check table
 // --------------------------------------------------------------
-const uint8_t chCRCHTalbe[] =                                 // CRC high byte table
+const uint8_t chCRCHTalbe[] = // CRC high byte table
 {
  0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
@@ -79,7 +119,7 @@ const uint8_t chCRCHTalbe[] =                                 // CRC high byte t
  0x00, 0xC1, 0x81, 0x40
 };
 
-const uint8_t chCRCLTalbe[] =                                 // CRC low byte table
+const uint8_t chCRCLTalbe[] = // CRC low byte table
 {
  0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
  0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
@@ -119,42 +159,12 @@ static uint16_t CRC16(const uint8_t* p_data, uint16_t w_len)
 	return ((chCRCHi << 8) | chCRCLo);
 }
 
-static uint32_t num2bit(int num) {
-	switch(num) {
-	case 0:  return 0x80000000;
-	case 1:  return 0x40000000;
-	case 2:  return 0x20000000;
-	case 3:  return 0x10000000;
-	case 4:  return 0x08000000;
-	case 5:  return 0x04000000;
-	case 6:  return 0x02000000;
-	case 7:  return 0x01000000;
-	case 8:  return 0x00800000;
-	case 9:  return 0x00400000;
-	case 10: return 0x00200000;
-	case 11: return 0x00100000;
-	case 12: return 0x00080000;
-	case 13: return 0x00040000;
-	case 14: return 0x00020000;
-	case 15: return 0x00010000;
-	case 16: return 0x00008000;
-	case 17: return 0x00004000;
-	case 18: return 0x00002000;
-	case 19: return 0x00001000;
-	case 20: return 0x00000800;
-	case 21: return 0x00000400;
-	case 22: return 0x00000200;
-	case 23: return 0x00000100;
-	case 24: return 0x00000080;
-	case 25: return 0x00000040;
-	case 26: return 0x00000020;
-	case 27: return 0x00000010;
-	case 28: return 0x00000008;
-	case 29: return 0x00000004;
-	case 30: return 0x00000002;
-	case 31: return 0x00000001;
-	default: return 0x00000000;
-	}
+static uint32_t num2bit(int num)
+{
+	if (num < 0 || num > 31)
+		return 0;
+	else
+		return (((uint32_t)1) << (31 - num));
 }
 
 static bool get_options(int this_option_offset, int *baud, int *chain_num,
@@ -197,21 +207,21 @@ static bool get_options(int this_option_offset, int *baud, int *chain_num,
 
 	tmp = atoi(buf);
 	switch (tmp) {
-	case 115200:
-		*baud = 115200;
-		break;
-	case 57600:
-		*baud = 57600;
-		break;
-	case 38400:
-		*baud = 38400;
-		break;
-	case 19200:
-		*baud = 19200;
-		break;
-	default:
-		quit(1, "Invalid bitmain-options for baud (%s) "
-			"must be 115200, 57600, 38400 or 19200", buf);
+		case 115200:
+			*baud = 115200;
+			break;
+		case 57600:
+			*baud = 57600;
+			break;
+		case 38400:
+			*baud = 38400;
+			break;
+		case 19200:
+			*baud = 19200;
+				break;
+		default:
+			quit(1, "Invalid bitmain-options for baud (%s) "
+				"must be 115200, 57600, 38400 or 19200", buf);
 	}
 
 	if (colon && *colon) {
@@ -221,9 +231,9 @@ static bool get_options(int this_option_offset, int *baud, int *chain_num,
 
 		if (*colon) {
 			tmp = atoi(colon);
-			if (tmp > 0) {
+			if (tmp > 0)
 				*chain_num = tmp;
-			} else {
+			else {
 				quit(1, "Invalid bitmain-options for "
 					"chain_num (%s) must be 1 ~ %d",
 					colon, BITMAIN_DEFAULT_CHAIN_NUM);
@@ -259,25 +269,32 @@ static bool get_options(int this_option_offset, int *baud, int *chain_num,
 				}
 				if (colon4 && *colon4) {
 					colon5 = strchr(colon4, ':');
-					if(colon5)
+					if (colon5)
 						*(colon5++) = '\0';
 
 					tmp = atoi(colon4);
-					if (tmp < BITMAIN_MIN_FREQUENCY || tmp > BITMAIN_MAX_FREQUENCY) {
-						quit(1, "Invalid bitmain-options for frequency, must be %d <= frequency <= %d",
-						     BITMAIN_MIN_FREQUENCY, BITMAIN_MAX_FREQUENCY);
-					} else {
+					if (tmp < BITMAIN_MIN_FREQUENCY ||
+					    tmp > BITMAIN_MAX_FREQUENCY) {
+						quit(1, "Invalid bitmain-options for frequency,"
+							" must be %d <= frequency <= %d",
+							BITMAIN_MIN_FREQUENCY,
+							BITMAIN_MAX_FREQUENCY);
+					} else
 						*frequency = tmp;
-					}
+
 					if (colon5 && *colon5) {
-						if(strlen(colon5) > 8 || strlen(colon5)%2 != 0 || strlen(colon5)/2 == 0) {
-							quit(1, "Invalid bitmain-options for reg data, must be hex now: %s",
-									colon5);
+						if (strlen(colon5) > 8 ||
+						    strlen(colon5)%2 != 0 ||
+						    strlen(colon5)/2 == 0) {
+							quit(1, "Invalid bitmain-options for"
+								" reg data, must be hex now: %s",
+								colon5);
 						}
 						memset(reg_data, 0, 4);
-						if(!hex2bin(reg_data, colon5, strlen(colon5)/2)) {
-							quit(1, "Invalid bitmain-options for reg data, hex2bin error now: %s",
-									colon5);
+						if (!hex2bin(reg_data, colon5, strlen(colon5)/2)) {
+							quit(1, "Invalid bitmain-options for reg"
+								" data, hex2bin error now: %s",
+								colon5);
 						}
 					}
 				}
@@ -296,15 +313,16 @@ static int bitmain_set_txconfig(struct bitmain_txconfig_token *bm,
 {
 	uint16_t crc = 0;
 	int datalen = 0;
-	uint8_t * sendbuf = (uint8_t *)bm;
+	uint8_t *sendbuf = (uint8_t *)bm;
 	if (unlikely(!bm)) {
 		applog(LOG_WARNING, "bitmain_set_txconfig bitmain_txconfig_token is null");
 		return -1;
 	}
 
 	if (unlikely(timeout_data <= 0 || asic_num <= 0 || chain_num <= 0)) {
-		applog(LOG_WARNING, "bitmain_set_txconfig parameter invalid timeout_data(%d) asic_num(%d) chain_num(%d)",
-				timeout_data, asic_num, chain_num);
+		applog(LOG_WARNING, "bitmain_set_txconfig parameter invalid"
+				    " timeout_data(%d) asic_num(%d) chain_num(%d)",
+				    timeout_data, asic_num, chain_num);
 		return -1;
 	}
 
@@ -322,7 +340,7 @@ static int bitmain_set_txconfig(struct bitmain_txconfig_token *bm,
 	bm->chip_config_eft = chip_config_eft;
 	bm->hw_error_eft = hw_error_eft;
 
-	sendbuf[2] = htole8(sendbuf[2]);
+	sendbuf[2] = bitswap(sendbuf[2]);
 
 	bm->chain_num = chain_num;
 	bm->asic_num = asic_num;
@@ -340,37 +358,36 @@ static int bitmain_set_txconfig(struct bitmain_txconfig_token *bm,
 	crc = CRC16((uint8_t *)bm, datalen-2);
 	bm->crc = htole16(crc);
 
-	applog(LOG_ERR, "BTM TxConfigToken:reset(%d) faneft(%d) touteft(%d) freqeft(%d) volteft(%d) chainceft(%d) chipceft(%d) hweft(%d) mnum(%d) anum(%d) fanpwmdata(%d) toutdata(%d) freq(%d) volt(%d) chainctime(%d) regdata(%02x%02x%02x%02x) chipaddr(%02x) regaddr(%02x) crc(%04x)",
-					reset, fan_eft, timeout_eft, frequency_eft, voltage_eft,
-					chain_check_time_eft, chip_config_eft, hw_error_eft, chain_num, asic_num,
-					fan_pwm_data, timeout_data, frequency, voltage,
-					chain_check_time, reg_data[0], reg_data[1], reg_data[2], reg_data[3], chip_address, reg_address, crc);
+	applog(LOG_ERR, "BTM TxConfigToken:reset(%d) faneft(%d) touteft(%d) freqeft(%d)"
+			" volteft(%d) chainceft(%d) chipceft(%d) hweft(%d) mnum(%d)"
+			" anum(%d) fanpwmdata(%d) toutdata(%d) freq(%d) volt(%d)"
+			" chainctime(%d) regdata(%02x%02x%02x%02x) chipaddr(%02x)"
+			" regaddr(%02x) crc(%04x)",
+			reset, fan_eft, timeout_eft, frequency_eft, voltage_eft,
+			chain_check_time_eft, chip_config_eft, hw_error_eft,
+			chain_num, asic_num, fan_pwm_data, timeout_data, frequency,
+			voltage, chain_check_time, reg_data[0], reg_data[1],
+			reg_data[2], reg_data[3], chip_address, reg_address, crc);
 
 	return datalen;
 }
 
-#ifdef WIN32
-#define BITMAIN_TEST
-#endif
-
-#ifdef BITMAIN_TEST
-const char * btm_work_test_data = "00000002ddc1ce5579dbec17f17fbb8f31ae218a814b2a0c1900f0d90000000100000000b58aa6ca86546b07a5a46698f736c7ca9c0eedc756d8f28ac33c20cc24d792675276f879190afc85b6888022000000800000000000000000000000000000000000000000000000000000000000000000";
-const char * btm_work_test_midstate = "2d8738e7f5bcf76dcb8316fec772e20e240cd58c88d47f2d3f5a6a9547ed0a35";
-#endif
-
-static int bitmain_set_txtask(uint8_t * sendbuf,
-			    unsigned int * last_work_block, struct work **works, int work_array_size, int work_array, int sendworkcount, int * sendcount)
+static int bitmain_set_txtask(uint8_t *sendbuf, unsigned int *last_work_block,
+			      struct work **works, int work_array_size, int work_array,
+			      int sendworkcount, int *sendcount)
 {
 	uint16_t crc = 0;
 	uint32_t work_id = 0;
 	int datalen = 0;
 	int i = 0;
 	int index = work_array;
-	uint8_t new_block= 0;
-	char * ob_hex = NULL;
+	uint8_t new_block = 0;
+	//char *ob_hex = NULL;
 	struct bitmain_txtask_token *bm = (struct bitmain_txtask_token *)sendbuf;
-	*sendcount = 0;
 	int cursendcount = 0;
+
+	*sendcount = 0;
+
 	if (unlikely(!bm)) {
 		applog(LOG_WARNING, "bitmain_set_txtask bitmain_txtask_token is null");
 		return -1;
@@ -385,27 +402,28 @@ static int bitmain_set_txtask(uint8_t * sendbuf,
 
 	datalen = 10;
 	applog(LOG_DEBUG, "BTM send work count %d -----", sendworkcount);
-	for(i = 0; i < sendworkcount; i++) {
-		if(index > work_array_size) {
+	for (i = 0; i < sendworkcount; i++) {
+		if (index > work_array_size)
 			index = 0;
-		}
-		if(works[index]) {
-			if(works[index]->work_block > *last_work_block) {
-				applog(LOG_ERR, "BTM send task new block %d old(%d)", works[index]->work_block, *last_work_block);
+
+		if (works[index]) {
+			if (works[index]->work_block > *last_work_block) {
+				applog(LOG_ERR, "BTM send task new block %d old(%d)",
+						works[index]->work_block, *last_work_block);
 				new_block = 1;
 				*last_work_block = works[index]->work_block;
 			}
 #ifdef BITMAIN_TEST
-			if(!hex2bin(works[index]->data, btm_work_test_data, 128)) {
+			if (!hex2bin(works[index]->data, btm_work_test_data, 128))
 				applog(LOG_DEBUG, "BTM send task set test data error");
-			}
-			if(!hex2bin(works[index]->midstate, btm_work_test_midstate, 32)) {
+
+			if (!hex2bin(works[index]->midstate, btm_work_test_midstate, 32))
 				applog(LOG_DEBUG, "BTM send task set test midstate error");
-			}
 #endif
 			work_id = works[index]->id;
 			bm->works[cursendcount].work_id = htole32(work_id);
-			applog(LOG_DEBUG, "BTM send task work id:%d %d", bm->works[cursendcount].work_id, work_id);
+			applog(LOG_DEBUG, "BTM send task work id:%"PRIu32" %"PRIu32,
+					  bm->works[cursendcount].work_id, work_id);
 			memcpy(bm->works[cursendcount].midstate, works[index]->midstate, 32);
 			memcpy(bm->works[cursendcount].data2, works[index]->data + 64, 12);
 
@@ -417,7 +435,7 @@ static int bitmain_set_txtask(uint8_t * sendbuf,
 		}
 		index++;
 	}
-	if(cursendcount <= 0) {
+	if (cursendcount <= 0) {
 		applog(LOG_ERR, "BTM send work count %d", cursendcount);
 		return 0;
 	}
@@ -430,10 +448,12 @@ static int bitmain_set_txtask(uint8_t * sendbuf,
 	//memcpy(sendbuf+1, &len, 2);
 	bm->new_block = new_block;
 
-	sendbuf[4] = htole8(sendbuf[4]);
+	sendbuf[4] = bitswap(sendbuf[4]);
 
 	applog(LOG_DEBUG, "BitMain TxTask Token: %d %d %02x%02x%02x%02x%02x%02x",
-				datalen, bm->length, sendbuf[0],sendbuf[1],sendbuf[2],sendbuf[3],sendbuf[4],sendbuf[5]);
+			  datalen, bm->length,
+			  sendbuf[0], sendbuf[1], sendbuf[2],
+			  sendbuf[3], sendbuf[4], sendbuf[5]);
 
 	*sendcount = cursendcount;
 
@@ -442,9 +462,11 @@ static int bitmain_set_txtask(uint8_t * sendbuf,
 	memcpy(sendbuf+datalen-2, &crc, 2);
 
 	applog(LOG_DEBUG, "BitMain TxTask Token: new_block(%d) work_num(%d) crc(%04x)",
-						new_block, cursendcount, crc);
+			  new_block, cursendcount, crc);
 	applog(LOG_DEBUG, "BitMain TxTask Token: %d %d %02x%02x%02x%02x%02x%02x",
-			datalen, bm->length, sendbuf[0],sendbuf[1],sendbuf[2],sendbuf[3],sendbuf[4],sendbuf[5]);
+			  datalen, bm->length,
+			  sendbuf[0], sendbuf[1], sendbuf[2],
+			  sendbuf[3], sendbuf[4], sendbuf[5]);
 
 	return datalen;
 }
@@ -454,7 +476,7 @@ static int bitmain_set_rxstatus(struct bitmain_rxstatus_token *bm,
 {
 	uint16_t crc = 0;
 	int datalen = 0;
-	uint8_t * sendbuf = (uint8_t *)bm;
+	uint8_t *sendbuf = (uint8_t *)bm;
 
 	if (unlikely(!bm)) {
 		applog(LOG_WARNING, "bitmain_set_rxstatus bitmain_rxstatus_token is null");
@@ -470,7 +492,7 @@ static int bitmain_set_rxstatus(struct bitmain_rxstatus_token *bm,
 	bm->chip_status_eft = chip_status_eft;
 	bm->detect_get = detect_get;
 
-	sendbuf[2] = htole8(sendbuf[2]);
+	sendbuf[2] = bitswap(sendbuf[2]);
 
 	bm->chip_address = chip_address;
 	bm->reg_address = reg_address;
@@ -478,8 +500,9 @@ static int bitmain_set_rxstatus(struct bitmain_rxstatus_token *bm,
 	crc = CRC16((uint8_t *)bm, datalen-2);
 	bm->crc = htole16(crc);
 
-	applog(LOG_DEBUG, "BitMain RxStatus Token: chip_status_eft(%d) detect_get(%d) chip_address(%02x) reg_address(%02x) crc(%04x)",
-				chip_status_eft, detect_get, chip_address, reg_address, crc);
+	applog(LOG_DEBUG, "BitMain RxStatus Token: chip_status_eft(%d) detect_get(%d)"
+			  " chip_address(%02x) reg_address(%02x) crc(%04x)",
+			  chip_status_eft, detect_get, chip_address, reg_address, crc);
 
 	return datalen;
 }
@@ -493,7 +516,9 @@ static int bitmain_parse_rxstatus(const uint8_t * data, int datalen, struct bitm
 		return -1;
 	}
 	if (unlikely(!data || datalen <= 0)) {
-		applog(LOG_WARNING, "bitmain_parse_rxstatus parameter invalid data is null or datalen(%d) error", datalen);
+		applog(LOG_WARNING, "bitmain_parse_rxstatus parameter invalid data is null"
+				    " or datalen(%d) error",
+				    datalen);
 		return -1;
 	}
 	memcpy(bm, data, sizeof(struct bitmain_rxstatus_data));
@@ -508,47 +533,53 @@ static int bitmain_parse_rxstatus(const uint8_t * data, int datalen, struct bitm
 	crc = CRC16(data, datalen-2);
 	memcpy(&(bm->crc), data+datalen-2, 2);
 	bm->crc = htole16(bm->crc);
-	if(crc != bm->crc) {
-		applog(LOG_ERR, "bitmain_parse_rxstatus check crc(%d) != bm crc(%d) datalen(%d)", crc, bm->crc, datalen);
+	if (crc != bm->crc) {
+		applog(LOG_ERR, "bitmain_parse_rxstatus check crc(%d)"
+				" != bm crc(%d) datalen(%d)",
+				crc, bm->crc, datalen);
 		return -1;
 	}
 	bm->fifo_space = htole32(bm->fifo_space);
 	bm->nonce_error = htole32(bm->nonce_error);
-	if(bm->chain_num*5 + bm->temp_num + bm->fan_num + 22 != datalen) {
-		applog(LOG_ERR, "bitmain_parse_rxstatus chain_num(%d) temp_num(%d) fan_num(%d) not match datalen(%d)",
+	if (bm->chain_num*5 + bm->temp_num + bm->fan_num + 22 != datalen) {
+		applog(LOG_ERR, "bitmain_parse_rxstatus chain_num(%d) temp_num(%d)"
+				" fan_num(%d) not match datalen(%d)",
 				bm->chain_num, bm->temp_num, bm->fan_num, datalen);
 		return -1;
 	}
-	if(bm->chain_num > BITMAIN_MAX_CHAIN_NUM) {
+	if (bm->chain_num > BITMAIN_MAX_CHAIN_NUM) {
 		applog(LOG_ERR, "bitmain_parse_rxstatus chain_num=%d error", bm->chain_num);
 		return -1;
 	}
-	if(bm->chain_num > 0) {
+	if (bm->chain_num > 0) {
 		memcpy(bm->chain_asic_status, data+20, bm->chain_num*4);
 		memcpy(bm->chain_asic_num, data+20+bm->chain_num*4, bm->chain_num);
 	}
-	for(i = 0; i < bm->chain_num; i++) {
+	for (i = 0; i < bm->chain_num; i++) {
 		//bm->chain_asic_status[i] = swab32(bm->chain_asic_status[i]);
 		bm->chain_asic_status[i] = htole32(bm->chain_asic_status[i]);
 	}
-	if(bm->temp_num > 0) {
+	if (bm->temp_num > 0) {
 		memcpy(bm->temp, data+20+bm->chain_num*5, bm->temp_num);
 	}
-	if(bm->fan_num > 0) {
+	if (bm->fan_num > 0) {
 		memcpy(bm->fan, data+20+bm->chain_num*5+bm->temp_num, bm->fan_num);
 	}
-	applog(LOG_DEBUG, "BitMain RxStatusData: chipvalueeft(%d) version(%d) fifospace(%d) regvalue(%d) chainnum(%d) tempnum(%d) fannum(%d) crc(%04x)",
-			bm->chip_value_eft, bm->version, bm->fifo_space, bm->reg_value, bm->chain_num, bm->temp_num, bm->fan_num, bm->crc);
+	applog(LOG_DEBUG, "BitMain RxStatusData: chipvalueeft(%d) version(%d) fifospace(%d)"
+			  " regvalue(%d) chainnum(%d) tempnum(%d) fannum(%d) crc(%04x)",
+			  bm->chip_value_eft, bm->version, bm->fifo_space, bm->reg_value,
+			  bm->chain_num, bm->temp_num, bm->fan_num, bm->crc);
 	applog(LOG_DEBUG, "BitMain RxStatus Data chain info:");
-	for(i = 0; i < bm->chain_num; i++) {
-		applog(LOG_DEBUG, "BitMain RxStatus Data chain(%d) asic num=%d asic_status=%08x", i+1, bm->chain_asic_num[i], bm->chain_asic_status[i]);
+	for (i = 0; i < bm->chain_num; i++) {
+		applog(LOG_DEBUG, "BitMain RxStatus Data chain(%d) asic num=%d asic_status=%08x",
+				  i+1, bm->chain_asic_num[i], bm->chain_asic_status[i]);
 	}
 	applog(LOG_DEBUG, "BitMain RxStatus Data temp info:");
-	for(i = 0; i < bm->temp_num; i++) {
+	for (i = 0; i < bm->temp_num; i++) {
 		applog(LOG_DEBUG, "BitMain RxStatus Data temp(%d) temp=%d", i+1, bm->temp[i]);
 	}
 	applog(LOG_DEBUG, "BitMain RxStatus Data fan info:");
-	for(i = 0; i < bm->fan_num; i++) {
+	for (i = 0; i < bm->fan_num; i++) {
 		applog(LOG_DEBUG, "BitMain RxStatus Data fan(%d) fan=%d", i+1, bm->fan[i]);
 	}
 	return 0;
@@ -580,18 +611,22 @@ static int bitmain_parse_rxnonce(const uint8_t * data, int datalen, struct bitma
 	crc = CRC16(data, datalen-2);
 	memcpy(&(bm->crc), data+datalen-2, 2);
 	bm->crc = htole16(bm->crc);
-	if(crc != bm->crc) {
-		applog(LOG_ERR, "bitmain_parse_rxnonce check crc(%d) != bm crc(%d) datalen(%d)", crc, bm->crc, datalen);
+	if (crc != bm->crc) {
+		applog(LOG_ERR, "bitmain_parse_rxnonce check crc(%d)"
+				" != bm crc(%d) datalen(%d)",
+				crc, bm->crc, datalen);
 		return -1;
 	}
 	curnoncenum = (datalen-4)/8;
-	applog(LOG_DEBUG, "BitMain RxNonce Data: nonce_num(%d-%d) fifo_space(%d)", curnoncenum, bm->nonce_num, bm->fifo_space);
-	for(i = 0; i < curnoncenum; i++) {
+	applog(LOG_DEBUG, "BitMain RxNonce Data: nonce_num(%d-%d) fifo_space(%d)",
+			  curnoncenum, bm->nonce_num, bm->fifo_space);
+	for (i = 0; i < curnoncenum; i++) {
 		bm->nonces[i].work_id = htole32(bm->nonces[i].work_id);
 		bm->nonces[i].nonce = htole32(bm->nonces[i].nonce);
 
-		applog(LOG_DEBUG, "BitMain RxNonce Data %d: work_id(%d) nonce(%08x)(%d)",
-				i, bm->nonces[i].work_id, bm->nonces[i].nonce, bm->nonces[i].nonce);
+		applog(LOG_DEBUG, "BitMain RxNonce Data %d: work_id(%"PRIu32") nonce(%08x)(%d)",
+				  i, bm->nonces[i].work_id,
+				  bm->nonces[i].nonce, bm->nonces[i].nonce);
 	}
 	*nonce_num = curnoncenum;
 	return 0;
@@ -600,105 +635,68 @@ static int bitmain_parse_rxnonce(const uint8_t * data, int datalen, struct bitma
 static int bitmain_read(struct cgpu_info *bitmain, unsigned char *buf,
 		       size_t bufsize, int timeout, int ep)
 {
-	int err = 0, readlen = 0;
-	size_t total = 0;
+	int readlen = 0, err = 0;
 
-	if(bitmain == NULL || buf == NULL || bufsize <= 0) {
-		applog(LOG_WARNING, "bitmain_read parameter error bufsize(%d)", bufsize);
+	if (bitmain == NULL || buf == NULL || bufsize <= 0) {
+		applog(LOG_WARNING, "bitmain_read parameter error bufsize(%d)",
+				    (int)bufsize);
 		return -1;
 	}
-	if(opt_bitmain_dev_usb) {
-#ifdef WIN32
-		char readbuf[BITMAIN_READBUF_SIZE];
-		int ofs = 2, cp = 0;
-
-		err = usb_read_once_timeout(bitmain, readbuf, bufsize, &readlen, timeout, ep);
-		applog(LOG_DEBUG, "%s%i: Get bitmain read got readlen %d err %d",
-			bitmain->drv->name, bitmain->device_id, readlen, err);
-
-		if (readlen < 2)
-			goto out;
-
-		while (readlen > 2) {
-			cp = readlen - 2;
-			if (cp > 62)
-				cp = 62;
-			memcpy(&buf[total], &readbuf[ofs], cp);
-			total += cp;
-			readlen -= cp + 2;
-			ofs += 64;
-		}
-#else
-		err = usb_read_once_timeout(bitmain, buf, bufsize, &readlen, timeout, ep);
-		applog(LOG_DEBUG, "%s%i: Get bitmain read got readlen %d err %d",
-			bitmain->drv->name, bitmain->device_id, readlen, err);
-		total = readlen;
-#endif
-	} else {
-		err = btm_read(bitmain, buf, bufsize);
-		total = err;
-	}
-out:
-	return total;
+
+	err = usb_read_once_timeout(bitmain, (char *)buf, bufsize, &readlen, timeout, ep);
+	applog(LOG_DEBUG, "%s%i: Get bitmain read got readlen %d err %d",
+			  bitmain->drv->name, bitmain->device_id, readlen, err);
+
+	return readlen;
 }
 
 static int bitmain_write(struct cgpu_info *bitmain, char *buf, ssize_t len, int ep)
 {
 	int err, amount;
-	if(opt_bitmain_dev_usb) {
-		err = usb_write(bitmain, buf, len, &amount, ep);
-		applog(LOG_DEBUG, "%s%i: usb_write got err %d", bitmain->drv->name,
-				bitmain->device_id, err);
-
-		if (unlikely(err != 0)) {
-			applog(LOG_ERR, "usb_write error on bitmain_write err=%d", err);
-			return BTM_SEND_ERROR;
-		}
-		if (amount != len) {
-			applog(LOG_ERR, "usb_write length mismatch on bitmain_write amount=%d len=%d", amount, len);
-			return BTM_SEND_ERROR;
-		}
-	} else {
-		int havelen = 0;
-		while(havelen < len) {
-			err = btm_write(bitmain, buf+havelen, len-havelen);
-			if(err < 0) {
-				applog(LOG_DEBUG, "%s%i: btm_write got err %d", bitmain->drv->name,
-						bitmain->device_id, err);
-				applog(LOG_WARNING, "usb_write error on bitmain_write");
-				return BTM_SEND_ERROR;
-			} else {
-				havelen += err;
-			}
-		}
+
+	err = usb_write(bitmain, buf, len, &amount, ep);
+	applog(LOG_DEBUG, "%s%i: usb_write got err %d",
+			  bitmain->drv->name, bitmain->device_id, err);
+
+	if (unlikely(err != 0)) {
+		applog(LOG_ERR, "usb_write error on bitmain_write err=%d", err);
+		return BTM_SEND_ERROR;
+	}
+	if (amount != len) {
+		applog(LOG_ERR, "usb_write length mismatch on bitmain_write "
+				"amount=%d len=%d",
+				amount, (int)len);
+		return BTM_SEND_ERROR;
 	}
+
 	return BTM_SEND_OK;
 }
 
-static int bitmain_send_data(const uint8_t * data, int datalen, struct cgpu_info *bitmain)
+static int bitmain_send_data(const uint8_t *data, int datalen, __maybe_unused struct cgpu_info *bitmain)
 {
-	int delay, ret, ep = C_BITMAIN_SEND;
-	struct bitmain_info *info = NULL;
-	cgtimer_t ts_start;
+	int ret, ep = C_BITMAIN_SEND;
+	//int delay;
+	//struct bitmain_info *info = NULL;
+	//cgtimer_t ts_start;
 
-	if(datalen <= 0) {
+	if (datalen <= 0) {
 		return 0;
 	}
 
-	if(data[0] == BITMAIN_TOKEN_TYPE_TXCONFIG) {
+	if (data[0] == BITMAIN_TOKEN_TYPE_TXCONFIG) {
 		ep = C_BITMAIN_TOKEN_TXCONFIG;
-	} else if(data[0] == BITMAIN_TOKEN_TYPE_TXTASK) {
+	} else if (data[0] == BITMAIN_TOKEN_TYPE_TXTASK) {
 		ep = C_BITMAIN_TOKEN_TXTASK;
-	} else if(data[0] == BITMAIN_TOKEN_TYPE_RXSTATUS) {
+	} else if (data[0] == BITMAIN_TOKEN_TYPE_RXSTATUS) {
 		ep = C_BITMAIN_TOKEN_RXSTATUS;
 	}
 
-	info = bitmain->device_data;
+	//info = bitmain->device_data;
 	//delay = datalen * 10 * 1000000;
 	//delay = delay / info->baud;
 	//delay += 4000;
 
-	if(opt_debug) {
+	if (opt_debug) {
 		applog(LOG_DEBUG, "BitMain: Sent(%d):", datalen);
 		hexdump(data, datalen);
 	}
@@ -714,15 +712,6 @@ static int bitmain_send_data(const uint8_t * data, int datalen, struct cgpu_info
 	return ret;
 }
 
-static bool bitmain_decode_nonce(struct thr_info *thr, struct cgpu_info *bitmain,
-				struct bitmain_info *info, uint32_t nonce, struct work *work)
-{
-	info = bitmain->device_data;
-	//info->matching_work[work->subid]++;
-	applog(LOG_DEBUG, "BitMain: nonce = %08x", nonce);
-	return submit_nonce(thr, work, nonce);
-}
-
 static void bitmain_inc_nvw(struct bitmain_info *info, struct thr_info *thr)
 {
 	applog(LOG_INFO, "%s%d: No matching work - HW error",
@@ -732,31 +721,32 @@ static void bitmain_inc_nvw(struct bitmain_info *info, struct thr_info *thr)
 	info->no_matching_work++;
 }
 
-static inline void record_temp_fan(struct bitmain_info *info, struct bitmain_rxstatus_data *bm, float *temp_avg)
+static inline void record_temp_fan(struct bitmain_info *info, struct bitmain_rxstatus_data *bm, double *temp_avg)
 {
 	int i = 0;
-	*temp_avg = 0;
+
+	*temp_avg = 0.0;
 
 	info->fan_num = bm->fan_num;
-	for(i = 0; i < bm->fan_num; i++) {
+	for (i = 0; i < bm->fan_num; i++) {
 		info->fan[i] = bm->fan[i] * BITMAIN_FAN_FACTOR;
 	}
 	info->temp_num = bm->temp_num;
-	for(i = 0; i < bm->temp_num; i++) {
+	for (i = 0; i < bm->temp_num; i++) {
 		info->temp[i] = bm->temp[i];
 		/*
-		if(bm->temp[i] & 0x80) {
+		if (bm->temp[i] & 0x80) {
 			bm->temp[i] &= 0x7f;
 			info->temp[i] = 0 - ((~bm->temp[i] & 0x7f) + 1);
 		}*/
 		*temp_avg += info->temp[i];
 
-		if(info->temp[i] > info->temp_max) {
+		if (info->temp[i] > info->temp_max) {
 			info->temp_max = info->temp[i];
 		}
 	}
 
-	if(bm->temp_num > 0) {
+	if (bm->temp_num > 0) {
 		*temp_avg = *temp_avg / bm->temp_num;
 		info->temp_avg = *temp_avg;
 	}
@@ -771,16 +761,16 @@ static void bitmain_update_temps(struct cgpu_info *bitmain, struct bitmain_info 
 	record_temp_fan(info, bm, &(bitmain->temp));
 
 	strcpy(msg, "BitMain: ");
-	for(i = 0; i < bm->fan_num; i++) {
-		if(i != 0) {
+	for (i = 0; i < bm->fan_num; i++) {
+		if (i != 0) {
 			strcat(msg, ", ");
 		}
 		sprintf(tmp, "Fan%d: %d/m", i+1, info->fan[i]);
 		strcat(msg, tmp);
 	}
 	strcat(msg, "\t");
-	for(i = 0; i < bm->temp_num; i++) {
-		if(i != 0) {
+	for (i = 0; i < bm->temp_num; i++) {
+		if (i != 0) {
 			strcat(msg, ", ");
 		}
 		sprintf(tmp, "Temp%d: %dC", i+1, info->temp[i]);
@@ -807,62 +797,74 @@ static void bitmain_update_temps(struct cgpu_info *bitmain, struct bitmain_info 
 }
 
 static void bitmain_parse_results(struct cgpu_info *bitmain, struct bitmain_info *info,
-				 struct thr_info *thr, uint8_t *buf, int *offset)
+				  struct thr_info *thr, uint8_t *buf, int *offset)
 {
 	int i, j, n, m, errordiff, spare = BITMAIN_READ_SIZE;
 	uint32_t checkbit = 0x00000000;
 	bool found = false;
 	struct work *work = NULL;
-	char * ob_hex = NULL;
+	//char *ob_hex = NULL;
 
 	for (i = 0; i <= spare; i++) {
-		if(buf[i] == 0xa1) {
+		if (buf[i] == 0xa1) {
 			struct bitmain_rxstatus_data rxstatusdata;
 			applog(LOG_DEBUG, "bitmain_parse_results RxStatus Data");
-			if(*offset < 2) {
+			if (*offset < 2) {
 				return;
 			}
-			if(buf[i+1] > 124) {
-				applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxstatus datalen=%d error", buf[i+1]+2);
+			if (buf[i+1] > 124) {
+				applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxstatus"
+						" datalen=%d error",
+						buf[i+1]+2);
 				continue;
 			}
-			if(*offset < buf[i+1] + 2) {
+			if (*offset < buf[i+1] + 2) {
 				return;
 			}
-			if(bitmain_parse_rxstatus(buf+i, buf[i+1]+2, &rxstatusdata) != 0) {
-				applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxstatus error len=%d", buf[i+1]+2);
+			if (bitmain_parse_rxstatus(buf+i, buf[i+1]+2, &rxstatusdata) != 0) {
+				applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxstatus"
+						" error len=%d",
+						buf[i+1]+2);
 			} else {
 				mutex_lock(&info->qlock);
 				info->chain_num = rxstatusdata.chain_num;
 				info->fifo_space = rxstatusdata.fifo_space;
 				info->nonce_error = rxstatusdata.nonce_error;
 				errordiff = info->nonce_error-info->last_nonce_error;
-				applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxstatus version=%d chainnum=%d fifospace=%d nonceerror=%d-%d freq=%d chain info:",
-						rxstatusdata.version, info->chain_num, info->fifo_space, info->last_nonce_error, info->nonce_error, info->frequency);
-				for(n = 0; n < rxstatusdata.chain_num; n++) {
+				applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxstatus"
+						" version=%d chainnum=%d fifospace=%d"
+						" nonceerror=%d-%d freq=%d chain info:",
+						rxstatusdata.version, info->chain_num,
+						info->fifo_space, info->last_nonce_error,
+						info->nonce_error, info->frequency);
+				for (n = 0; n < rxstatusdata.chain_num; n++) {
 					info->chain_asic_num[n] = rxstatusdata.chain_asic_num[n];
 					info->chain_asic_status[n] = rxstatusdata.chain_asic_status[n];
 					memset(info->chain_asic_status_t[n], 0, 40);
 					j = 0;
-					for(m = 0; m < 32; m++) {
-						if(m%8 == 0 && m != 0) {
+					for (m = 0; m < 32; m++) {
+						if (m%8 == 0 && m != 0) {
 							info->chain_asic_status_t[n][j] = ' ';
 							j++;
 						}
 						checkbit = num2bit(m);
-						if(rxstatusdata.chain_asic_status[n] & checkbit) {
+						if (rxstatusdata.chain_asic_status[n] & checkbit)
 							info->chain_asic_status_t[n][j] = 'o';
-						} else {
+						else
 							info->chain_asic_status_t[n][j] = 'x';
-						}
+
 						j++;
 					}
-					applog(LOG_ERR, "bitmain_parse_rxstatus chain(%d) asic_num=%d asic_status=%08x-%s", n, info->chain_asic_num[n], info->chain_asic_status[n], info->chain_asic_status_t[n]);
+					applog(LOG_ERR, "bitmain_parse_rxstatus chain(%d)"
+							" asic_num=%d asic_status=%08x-%s",
+							n, info->chain_asic_num[n],
+							info->chain_asic_status[n],
+							info->chain_asic_status_t[n]);
 				}
 				mutex_unlock(&info->qlock);
 
-				if(errordiff > 0) {
-					for(j = 0; j < errordiff; j++) {
+				if (errordiff > 0) {
+					for (j = 0; j < errordiff; j++) {
 						bitmain_inc_nvw(info, thr);
 					}
 					mutex_lock(&info->qlock);
@@ -874,32 +876,41 @@ static void bitmain_parse_results(struct cgpu_info *bitmain, struct bitmain_info
 
 			found = true;
 			spare = buf[i+1] + 2 + i;
-			if(spare > *offset) {
-				applog(LOG_ERR, "bitmain_parse_rxresults space(%d) > offset(%d)", spare, *offset);
+			if (spare > *offset) {
+				applog(LOG_ERR, "bitmain_parse_rxresults space(%d) > offset(%d)",
+						spare, *offset);
 				spare = *offset;
 			}
 			break;
-		} else if(buf[i] == 0xa2) {
+		} else if (buf[i] == 0xa2) {
 			struct bitmain_rxnonce_data rxnoncedata;
 			int nonce_num = 0;
 			applog(LOG_DEBUG, "bitmain_parse_results RxNonce Data");
-			if(*offset < 2) {
+			if (*offset < 2) {
 				return;
 			}
-			if(buf[i+1] > 70) {
-				applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxnonce datalen=%d error", buf[i+1]+2);
+			if (buf[i+1] > 70) {
+				applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxnonce "
+						"datalen=%d error",
+						buf[i+1]+2);
 				continue;
 			}
-			if(*offset < buf[i+1] + 2) {
+			if (*offset < buf[i+1] + 2) {
 				return;
 			}
-			if(bitmain_parse_rxnonce(buf+i, buf[i+1]+2, &rxnoncedata, &nonce_num) != 0) {
-				applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxnonce error len=%d", buf[i+1]+2);
+			if (bitmain_parse_rxnonce(buf+i, buf[i+1]+2, &rxnoncedata, &nonce_num) != 0) {
+				applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxnonce "
+						"error len=%d",
+						buf[i+1]+2);
 			} else {
-				for(j = 0; j < nonce_num; j++) {
-					work = clone_queued_work_byid(bitmain, rxnoncedata.nonces[j].work_id);
-					if(work) {
-						applog(LOG_DEBUG, "bitmain_parse_results nonce find work(%d-%d)(%08x)", work->id, rxnoncedata.nonces[j].work_id, rxnoncedata.nonces[j].nonce);
+				for (j = 0; j < nonce_num; j++) {
+					work = find_queued_work_byid(bitmain, rxnoncedata.nonces[j].work_id);
+					if (work) {
+						applog(LOG_DEBUG, "bitmain_parse_results nonce find "
+								  "work(%"PRIu32"-%"PRIu32")(%08x)",
+								  work->id,
+								  rxnoncedata.nonces[j].work_id,
+								  rxnoncedata.nonces[j].nonce);
 
 						/*ob_hex = bin2hex(work->midstate, 32);
 						applog(LOG_ERR, "work %d midstate: %s", work->id, ob_hex);
@@ -909,37 +920,39 @@ static void bitmain_parse_results(struct cgpu_info *bitmain, struct bitmain_info
 						applog(LOG_ERR, "work %d data2: %s", work->id, ob_hex);
 						free(ob_hex);*/
 
-						if(work->work_block < info->last_work_block) {
-							applog(LOG_ERR, "BitMain: bitmain_parse_rxnonce work(%d) nonce stale", rxnoncedata.nonces[j].work_id);
-						} else {
-							if (bitmain_decode_nonce(thr, bitmain, info, rxnoncedata.nonces[j].nonce, work)) {
-						 		applog(LOG_DEBUG, "bitmain_decode_nonce info->qlock start");
-						 		mutex_lock(&info->qlock);
-						 		info->nonces++;
-								info->auto_nonces++;
-								mutex_unlock(&info->qlock);
-								applog(LOG_DEBUG, "bitmain_decode_nonce info->qlock stop");
-						 	} else {
-						 		//bitmain_inc_nvw(info, thr);
-						 		applog(LOG_ERR, "BitMain: bitmain_decode_nonce error work(%d)", rxnoncedata.nonces[j].work_id);
-						 	}
-						}
-					 	free_work(work);
+						//info->matching_work[work->subid]++;
+						applog(LOG_DEBUG, "BitMain: nonce = %08x",
+								  rxnoncedata.nonces[j].nonce);
+						if (submit_nonce(thr, work, rxnoncedata.nonces[j].nonce)) {
+					 		applog(LOG_DEBUG, "bitmain_decode_nonce ok");
+					 		mutex_lock(&info->qlock);
+					 		info->nonces++;
+							info->auto_nonces++;
+							mutex_unlock(&info->qlock);
+					 	} else {
+					 		//bitmain_inc_nvw(info, thr);
+					 		applog(LOG_ERR, "BitMain: bitmain_decode_nonce "
+									"error work(%"PRIu32")",
+									rxnoncedata.nonces[j].work_id);
+					 	}
 					} else {
 						//bitmain_inc_nvw(info, thr);
-						applog(LOG_ERR, "BitMain: Nonce not find work(%d)", rxnoncedata.nonces[j].work_id);
+						applog(LOG_ERR, "BitMain: Work not found for id (%"PRIu32")",
+								rxnoncedata.nonces[j].work_id);
 					}
 				}
 				mutex_lock(&info->qlock);
 				info->fifo_space = rxnoncedata.fifo_space;
-				applog(LOG_DEBUG, "bitmain_parse_rxnonce fifo space=%d", info->fifo_space);
+				applog(LOG_DEBUG, "bitmain_parse_rxnonce fifo space=%d",
+						  info->fifo_space);
 				mutex_unlock(&info->qlock);
 			}
 
  			found = true;
  			spare = buf[i+1] + 2 + i;
- 			if(spare > *offset) {
- 				applog(LOG_ERR, "bitmain_parse_rxnonce space(%d) > offset(%d)", spare, *offset);
+ 			if (spare > *offset) {
+ 				applog(LOG_ERR, "bitmain_parse_rxnonce space(%d) > offset(%d)",
+						spare, *offset);
  				spare = *offset;
  			}
  			break;
@@ -960,8 +973,7 @@ static void bitmain_parse_results(struct cgpu_info *bitmain, struct bitmain_info
 	memmove(buf, buf + spare, *offset);
 }
 
-static void bitmain_running_reset(struct cgpu_info *bitmain,
-				   struct bitmain_info *info)
+static void bitmain_running_reset(struct cgpu_info *bitmain, struct bitmain_info *info)
 {
 	bitmain->results = 0;
 	info->reset = false;
@@ -971,7 +983,7 @@ static void *bitmain_get_results(void *userdata)
 {
 	struct cgpu_info *bitmain = (struct cgpu_info *)userdata;
 	struct bitmain_info *info = bitmain->device_data;
-	int offset = 0, read_delay = 0, ret = 0;
+	int offset = 0, ret = 0;
 	const int rsize = BITMAIN_FTDI_READSIZE;
 	char readbuf[BITMAIN_READBUF_SIZE];
 	struct thr_info *thr = info->thr;
@@ -988,7 +1000,7 @@ static void *bitmain_get_results(void *userdata)
 
 		if (offset >= (int)BITMAIN_READ_SIZE) {
 			applog(LOG_DEBUG, "======start bitmain_get_results ");
-			bitmain_parse_results(bitmain, info, thr, readbuf, &offset);
+			bitmain_parse_results(bitmain, info, thr, (uint8_t *)readbuf, &offset);
 			applog(LOG_DEBUG, "======stop bitmain_get_results ");
 		}
 
@@ -1018,7 +1030,7 @@ static void *bitmain_get_results(void *userdata)
 
 		if (ret < 1) {
 			errorcount++;
-			if(errorcount > 100) {
+			if (errorcount > 100) {
 				applog(LOG_ERR, "bitmain_read errorcount ret=%d", ret);
 				cgsleep_ms(20);
 				errorcount = 0;
@@ -1037,15 +1049,12 @@ static void *bitmain_get_results(void *userdata)
 	return NULL;
 }
 
+/*
 static void bitmain_set_timeout(struct bitmain_info *info)
 {
 	info->timeout = BITMAIN_TIMEOUT_FACTOR / info->frequency;
 }
-
-static void *bitmain_send_tasks(void *userdata)
-{
-	return NULL;
-}
+*/
 
 static void bitmain_init(struct cgpu_info *bitmain)
 {
@@ -1073,9 +1082,6 @@ static bool bitmain_prepare(struct thr_info *thr)
 	if (pthread_create(&info->read_thr, NULL, bitmain_get_results, (void *)bitmain))
 		quit(1, "Failed to create bitmain read_thr");
 
-	//if (pthread_create(&info->write_thr, NULL, bitmain_send_tasks, (void *)bitmain))
-	//	quit(1, "Failed to create bitmain write_thr");
-
 	bitmain_init(bitmain);
 
 	return true;
@@ -1085,7 +1091,7 @@ static int bitmain_initialize(struct cgpu_info *bitmain)
 {
 	uint8_t data[BITMAIN_READBUF_SIZE];
 	struct bitmain_info *info = NULL;
-	int ret = 0, spare = 0;
+	int ret = 0;
 	uint8_t sendbuf[BITMAIN_SENDBUF_SIZE];
 	int readlen = 0;
 	int sendlen = 0;
@@ -1097,7 +1103,7 @@ static int bitmain_initialize(struct cgpu_info *bitmain)
 	int eft = 0;
 
 	/* Send reset, then check for result */
-	if(!bitmain) {
+	if (!bitmain) {
 		applog(LOG_WARNING, "bitmain_initialize cgpu_info is null");
 		return -1;
 	}
@@ -1106,7 +1112,7 @@ static int bitmain_initialize(struct cgpu_info *bitmain)
 	/* clear read buf */
 	ret = bitmain_read(bitmain, data, BITMAIN_READBUF_SIZE,
 				  BITMAIN_RESET_TIMEOUT, C_BITMAIN_READ);
-	if(ret > 0) {
+	if (ret > 0) {
 		if (opt_debug) {
 			applog(LOG_DEBUG, "BTM%d Clear Read(%d):", bitmain->device_id, ret);
 			hexdump(data, ret);
@@ -1114,7 +1120,7 @@ static int bitmain_initialize(struct cgpu_info *bitmain)
 	}
 
 	sendlen = bitmain_set_rxstatus((struct bitmain_rxstatus_token *)sendbuf, 0, 1, 0, 0);
-	if(sendlen <= 0) {
+	if (sendlen <= 0) {
 		applog(LOG_ERR, "bitmain_initialize bitmain_set_rxstatus error(%d)", sendlen);
 		return -1;
 	}
@@ -1124,64 +1130,81 @@ static int bitmain_initialize(struct cgpu_info *bitmain)
 		applog(LOG_ERR, "bitmain_initialize bitmain_send_data error");
 		return -1;
 	}
-	while(trycount >= 0) {
-		ret = bitmain_read(bitmain, data+readlen, BITMAIN_READBUF_SIZE, BITMAIN_RESET_TIMEOUT, C_BITMAIN_DATA_RXSTATUS);
-		if(ret > 0) {
+	while (trycount >= 0) {
+		ret = bitmain_read(bitmain, data+readlen, BITMAIN_READBUF_SIZE,
+				   BITMAIN_RESET_TIMEOUT, C_BITMAIN_DATA_RXSTATUS);
+		if (ret > 0) {
 			readlen += ret;
-			if(readlen > BITMAIN_READ_SIZE) {
-				for(i = 0; i < readlen; i++) {
-					if(data[i] == 0xa1) {
+			if (readlen > BITMAIN_READ_SIZE) {
+				for (i = 0; i < readlen; i++) {
+					if (data[i] == 0xa1) {
 						if (opt_debug) {
-							applog(LOG_DEBUG, "%s%d initset: get:", bitmain->drv->name, bitmain->device_id);
+							applog(LOG_DEBUG, "%s%d initset: get:",
+									  bitmain->drv->name,
+									  bitmain->device_id);
 							hexdump(data, readlen);
 						}
-						if(data[i+1] > 124) {
-							applog(LOG_ERR, "bitmain_initialize rxstatus datalen=%d error", data[i+1]+2);
+						if (data[i+1] > 124) {
+							applog(LOG_ERR, "bitmain_initialize rxstatus "
+									"datalen=%d error",
+									data[i+1]+2);
 							continue;
 						}
-						if(readlen-i < data[i+1]+2) {
-							applog(LOG_ERR, "bitmain_initialize rxstatus datalen=%d low", data[i+1]+2);
+						if (readlen-i < data[i+1]+2) {
+							applog(LOG_ERR, "bitmain_initialize rxstatus "
+									"datalen=%d low",
+									data[i+1]+2);
 							continue;
 						}
 						if (bitmain_parse_rxstatus(data+i, data[i+1]+2, &rxstatusdata) != 0) {
-							applog(LOG_ERR, "bitmain_initialize bitmain_parse_rxstatus error");
+							applog(LOG_ERR, "bitmain_initialize "
+									"bitmain_parse_rxstatus error");
 							continue;
 						}
 						info->chain_num = rxstatusdata.chain_num;
 						info->fifo_space = rxstatusdata.fifo_space;
 						info->nonce_error = 0;
 						info->last_nonce_error = 0;
-						applog(LOG_ERR, "bitmain_initialize bitmain_parse_rxstatus version(%d) chain_num(%d) fifo_space(%d) nonce_error(%d) freq=%d",
-								rxstatusdata.version, info->chain_num, info->fifo_space, rxstatusdata.nonce_error, info->frequency);
-						for(i = 0; i < rxstatusdata.chain_num; i++) {
+						applog(LOG_ERR, "bitmain_initialize bitmain_parse_rxstatus "
+								"version(%d) chain_num(%d) fifo_space(%d) "
+								"nonce_error(%d) freq=%d",
+								rxstatusdata.version,
+								info->chain_num,
+								info->fifo_space,
+								rxstatusdata.nonce_error,
+								info->frequency);
+						for (i = 0; i < rxstatusdata.chain_num; i++) {
 							info->chain_asic_num[i] = rxstatusdata.chain_asic_num[i];
 							info->chain_asic_status[i] = rxstatusdata.chain_asic_status[i];
 							memset(info->chain_asic_status_t[i], 0, 40);
 							j = 0;
-							for(m = 0; m < 32; m++) {
-								if(m%8 == 0 && m != 0) {
+							for (m = 0; m < 32; m++) {
+								if (m%8 == 0 && m != 0) {
 									info->chain_asic_status_t[i][j] = ' ';
 									j++;
 								}
 								checkbit = num2bit(m);
-								if(rxstatusdata.chain_asic_status[i] & checkbit) {
+								if (rxstatusdata.chain_asic_status[i] & checkbit)
 									info->chain_asic_status_t[i][j] = 'o';
-								} else {
+								else
 									info->chain_asic_status_t[i][j] = 'x';
-								}
+
 								j++;
 							}
-							applog(LOG_ERR, "bitmain_initialize bitmain_parse_rxstatus chain(%d) asic_num=%d asic_status=%08x-%s",
-									i, info->chain_asic_num[i], info->chain_asic_status[i], info->chain_asic_status_t[i]);
+							applog(LOG_ERR, "bitmain_initialize "
+									"bitmain_parse_rxstatus chain(%d) "
+									"asic_num=%d asic_status=%08x-%s",
+									i, info->chain_asic_num[i],
+									info->chain_asic_status[i],
+									info->chain_asic_status_t[i]);
 						}
 						bitmain_update_temps(bitmain, info, &rxstatusdata);
 						statusok = 1;
 						break;
 					}
 				}
-				if(statusok) {
+				if (statusok)
 					break;
-				}
 			}
 		}
 		trycount--;
@@ -1196,16 +1219,20 @@ static int bitmain_initialize(struct cgpu_info *bitmain)
 
 	cgtime(&info->last_status_time);
 
-	if(statusok) {
+	if (statusok) {
 		applog(LOG_ERR, "bitmain_initialize start send txconfig");
-		if(opt_bitmain_hwerror)
+		if (opt_bitmain_hwerror)
 			eft = 1;
 		else
 			eft = 0;
-		sendlen = bitmain_set_txconfig((struct bitmain_txconfig_token *)sendbuf, 1, 1, 1, 1, 1, 0, 1, eft,
-				info->chain_num, info->asic_num, BITMAIN_DEFAULT_FAN_MAX_PWM, info->timeout,
-				info->frequency, BITMAIN_DEFAULT_VOLTAGE, 0, 0, 0x04, info->reg_data);
-		if(sendlen <= 0) {
+
+		sendlen = bitmain_set_txconfig((struct bitmain_txconfig_token *)sendbuf,
+						1, 1, 1, 1, 1, 0, 1, eft,
+						info->chain_num, info->asic_num,
+						BITMAIN_DEFAULT_FAN_MAX_PWM, info->timeout,
+						info->frequency, BITMAIN_DEFAULT_VOLTAGE,
+						0, 0, 0x04, info->reg_data);
+		if (sendlen <= 0) {
 			applog(LOG_ERR, "bitmain_initialize bitmain_set_txconfig error(%d)", sendlen);
 			return -1;
 		}
@@ -1223,100 +1250,7 @@ static int bitmain_initialize(struct cgpu_info *bitmain)
 	return 0;
 }
 
-static void bitmain_usb_init(struct cgpu_info *bitmain)
-{
-	int err, interface;
-
-#ifndef WIN32
-	return;
-#endif
-
-	if (bitmain->usbinfo.nodev)
-		return;
-
-	interface = usb_interface(bitmain);
-
-	// Reset
-	err = usb_transfer(bitmain, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
-		FTDI_VALUE_RESET, interface, C_RESET);
-
-	applog(LOG_DEBUG, "%s%i: reset got err %d",
-		bitmain->drv->name, bitmain->device_id, err);
-
-	if (bitmain->usbinfo.nodev)
-		return;
-
-	// Set latency
-	err = usb_transfer(bitmain, FTDI_TYPE_OUT, FTDI_REQUEST_LATENCY,
-		BITMAIN_LATENCY, interface, C_LATENCY);
-
-	applog(LOG_DEBUG, "%s%i: latency got err %d",
-		bitmain->drv->name, bitmain->device_id, err);
-
-	if (bitmain->usbinfo.nodev)
-		return;
-
-	// Set data
-	err = usb_transfer(bitmain, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
-				FTDI_VALUE_DATA_BTM, interface, C_SETDATA);
-
-	applog(LOG_DEBUG, "%s%i: data got err %d",
-		bitmain->drv->name, bitmain->device_id, err);
-
-	if (bitmain->usbinfo.nodev)
-		return;
-
-	// Set the baud
-	err = usb_transfer(bitmain, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_BTM,
-				(FTDI_INDEX_BAUD_BTM & 0xff00) | interface,
-				C_SETBAUD);
-
-	applog(LOG_DEBUG, "%s%i: setbaud got err %d",
-		bitmain->drv->name, bitmain->device_id, err);
-
-	if (bitmain->usbinfo.nodev)
-		return;
-
-	// Set Modem Control
-	err = usb_transfer(bitmain, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
-		FTDI_VALUE_MODEM, interface, C_SETMODEM);
-
-	applog(LOG_DEBUG, "%s%i: setmodemctrl got err %d",
-		bitmain->drv->name, bitmain->device_id, err);
-
-	if (bitmain->usbinfo.nodev)
-		return;
-
-	// Set Flow Control
-	err = usb_transfer(bitmain, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
-		FTDI_VALUE_FLOW, interface, C_SETFLOW);
-
-	applog(LOG_DEBUG, "%s%i: setflowctrl got err %d",
-		bitmain->drv->name, bitmain->device_id, err);
-
-	if (bitmain->usbinfo.nodev)
-		return;
-
-	/* BitMain repeats the following */
-	// Set Modem Control
-	err = usb_transfer(bitmain, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
-		FTDI_VALUE_MODEM, interface, C_SETMODEM);
-
-	applog(LOG_DEBUG, "%s%i: setmodemctrl 2 got err %d",
-		bitmain->drv->name, bitmain->device_id, err);
-
-	if (bitmain->usbinfo.nodev)
-		return;
-
-	// Set Flow Control
-	err = usb_transfer(bitmain, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
-		FTDI_VALUE_FLOW, interface, C_SETFLOW);
-
-	applog(LOG_DEBUG, "%s%i: setflowctrl 2 got err %d",
-		bitmain->drv->name, bitmain->device_id, err);
-}
-
-static struct cgpu_info * bitmain_usb_detect_one(libusb_device *dev, struct usb_find_devices *found)
+static struct cgpu_info *bitmain_detect_one(libusb_device *dev, struct usb_find_devices *found)
 {
 	int baud, chain_num, asic_num, timeout, frequency = 0;
 	uint8_t reg_data[4] = {0};
@@ -1329,7 +1263,7 @@ static struct cgpu_info * bitmain_usb_detect_one(libusb_device *dev, struct usb_
 	if (opt_bitmain_options == NULL)
 		return NULL;
 
-	bitmain = usb_alloc_cgpu(&bitmain_drv, BITMAIN_MINER_THREADS);
+	bitmain = usb_alloc_cgpu(&ants1_drv, BITMAIN_MINER_THREADS);
 
 	baud = BITMAIN_IO_SPEED;
 	chain_num = BITMAIN_DEFAULT_CHAIN_NUM;
@@ -1347,9 +1281,6 @@ static struct cgpu_info * bitmain_usb_detect_one(libusb_device *dev, struct usb_
 	 * all ourselves so set it to std usb type */
 	bitmain->usbdev->usb_type = USB_TYPE_STD;
 
-	/* We have a real BitMain! */
-	bitmain_usb_init(bitmain);
-
 	bitmain->device_data = calloc(sizeof(struct bitmain_info), 1);
 	if (unlikely(!(bitmain->device_data)))
 		quit(1, "Failed to calloc bitmain_info data");
@@ -1416,103 +1347,9 @@ shin:
 	return NULL;
 }
 
-static bool bitmain_detect_one(const char * devpath)
+static void ants1_detect(bool __maybe_unused hotplug)
 {
-	int baud, chain_num, asic_num, timeout, frequency = 0;
-	uint8_t reg_data[4] = {0};
-	int this_option_offset = ++option_offset;
-	struct bitmain_info *info;
-	struct cgpu_info *bitmain;
-	bool configured;
-	int ret;
-
-	if (opt_bitmain_options == NULL)
-		return false;
-
-	bitmain = btm_alloc_cgpu(&bitmain_drv, BITMAIN_MINER_THREADS);
-
-	configured = get_options(this_option_offset, &baud, &chain_num,
-				 &asic_num, &timeout, &frequency, reg_data);
-
-	if (!btm_init(bitmain, opt_bitmain_dev))
-		goto shin;
-	applog(LOG_ERR, "bitmain_detect_one btm init ok");
-
-	bitmain->device_data = calloc(sizeof(struct bitmain_info), 1);
-	if (unlikely(!(bitmain->device_data)))
-		quit(1, "Failed to calloc bitmain_info data");
-	info = bitmain->device_data;
-
-	if (configured) {
-		info->baud = baud;
-		info->chain_num = chain_num;
-		info->asic_num = asic_num;
-		info->timeout = timeout;
-		info->frequency = frequency;
-		memcpy(info->reg_data, reg_data, 4);
-	} else {
-		info->baud = BITMAIN_IO_SPEED;
-		info->chain_num = BITMAIN_DEFAULT_CHAIN_NUM;
-		info->asic_num = BITMAIN_DEFAULT_ASIC_NUM;
-		info->timeout = BITMAIN_DEFAULT_TIMEOUT;
-		info->frequency = BITMAIN_DEFAULT_FREQUENCY;
-		memset(info->reg_data, 0, 4);
-	}
-	info->voltage = BITMAIN_DEFAULT_VOLTAGE;
-
-	info->fan_pwm = BITMAIN_DEFAULT_FAN_MIN_PWM;
-	info->temp_max = 0;
-	/* This is for check the temp/fan every 3~4s */
-	info->temp_history_count = (4 / (float)((float)info->timeout * ((float)1.67/0x32))) + 1;
-	if (info->temp_history_count <= 0)
-		info->temp_history_count = 1;
-
-	info->temp_history_index = 0;
-	info->temp_sum = 0;
-	info->temp_old = 0;
-
-	if (!add_cgpu(bitmain))
-		goto unshin;
-
-	ret = bitmain_initialize(bitmain);
-	applog(LOG_ERR, "bitmain_detect_one stop bitmain_initialize %d", ret);
-	if (ret && !configured)
-		goto unshin;
-
-	info->errorcount = 0;
-
-	applog(LOG_DEBUG, "BitMain Detected: %s "
-	       "(chain_num=%d asic_num=%d timeout=%d frequency=%d)",
-	       bitmain->device_path, info->chain_num, info->asic_num, info->timeout,
-	       info->frequency);
-
-	return true;
-
-unshin:
-	btm_uninit(bitmain);
-
-shin:
-	free(bitmain->device_data);
-	bitmain->device_data = NULL;
-
-	bitmain = usb_free_cgpu(bitmain);
-
-	return false;
-}
-
-static void bitmain_detect(bool __maybe_unused hotplug)
-{
-	applog(LOG_DEBUG, "BTM detect dev: %s", opt_bitmain_dev);
-	if(strlen(opt_bitmain_dev) <= 0) {
-		opt_bitmain_dev_usb = true;
-	} else {
-		opt_bitmain_dev_usb = false;
-	}
-	if(opt_bitmain_dev_usb) {
-		usb_detect(&bitmain_drv, bitmain_usb_detect_one);
-	} else {
-		btm_detect(&bitmain_drv, bitmain_detect_one);
-	}
+	usb_detect(&ants1_drv, bitmain_detect_one);
 }
 
 static void do_bitmain_close(struct thr_info *thr)
@@ -1521,7 +1358,6 @@ static void do_bitmain_close(struct thr_info *thr)
 	struct bitmain_info *info = bitmain->device_data;
 
 	pthread_join(info->read_thr, NULL);
-	pthread_join(info->write_thr, NULL);
 	bitmain_running_reset(bitmain, info);
 
 	info->no_matching_work = 0;
@@ -1536,12 +1372,12 @@ static void get_bitmain_statline_before(char *buf, size_t bufsiz, struct cgpu_in
 	int i = 0;
 
 	/* Find the lowest fan speed of the ASIC cooling fans. */
-	for(i = 0; i < info->fan_num; i++) {
+	for (i = 0; i < info->fan_num; i++) {
 		if (info->fan[i] >= 0 && info->fan[i] < lowfan)
 			lowfan = info->fan[i];
 	}
 
-	tailsprintf(buf, bufsiz, "%2d/%3dC %04dR | ", info->temp_avg, info->temp_max, lowfan);
+	tailsprintf(buf, bufsiz, "%2d/%3dC %04dR", info->temp_avg, info->temp_max, lowfan);
 }
 
 /* We use a replacement algorithm to only remove references to work done from
@@ -1561,7 +1397,7 @@ static bool bitmain_fill(struct cgpu_info *bitmain)
 
 	applog(LOG_DEBUG, "BTM bitmain_fill start--------");
 	mutex_lock(&info->qlock);
-	if(info->fifo_space <= 0) {
+	if (info->fifo_space <= 0) {
 		applog(LOG_DEBUG, "BTM bitmain_fill fifo space empty--------");
 		ret = true;
 		goto out_unlock;
@@ -1571,44 +1407,51 @@ static bool bitmain_fill(struct cgpu_info *bitmain)
 	} else {
 		ret = false;
 	}
-	while(info->fifo_space > 0) {
+	while (info->fifo_space > 0) {
 		neednum = info->fifo_space<8?info->fifo_space:8;
 		queuednum = bitmain->queued;
-		applog(LOG_DEBUG, "BTM: Work task queued(%d) fifo space(%d) needsend(%d)", queuednum, info->fifo_space, neednum);
-		if(queuednum < neednum) {
-			while(true) {
+		applog(LOG_DEBUG, "BTM: Work task queued(%d) fifo space(%d) needsend(%d)",
+				  queuednum, info->fifo_space, neednum);
+		if (queuednum < neednum) {
+			while (true) {
 				work = get_queued(bitmain);
 				if (unlikely(!work)) {
 					break;
 				} else {
-					applog(LOG_DEBUG, "BTM get work queued number:%d neednum:%d", queuednum, neednum);
+					applog(LOG_DEBUG, "BTM get work queued number:%d neednum:%d",
+							  queuednum, neednum);
 					subid = bitmain->queued++;
 					work->subid = subid;
 					slot = bitmain->work_array + subid;
 					if (slot > BITMAIN_ARRAY_SIZE) {
-						applog(LOG_DEBUG, "bitmain_fill array cyc %d", BITMAIN_ARRAY_SIZE);
+						applog(LOG_DEBUG, "bitmain_fill array cyc %d",
+								  BITMAIN_ARRAY_SIZE);
 						slot = 0;
 					}
 					if (likely(bitmain->works[slot])) {
-						applog(LOG_DEBUG, "bitmain_fill work_completed %d", slot);
+						applog(LOG_DEBUG, "bitmain_fill work_completed %d",
+								  slot);
 						work_completed(bitmain, bitmain->works[slot]);
 					}
 					bitmain->works[slot] = work;
 					queuednum++;
-					if(queuednum >= neednum) {
+					if (queuednum >= neednum) {
 						break;
 					}
 				}
 			}
 		}
-		if(queuednum < BITMAIN_MAX_DEAL_QUEUE_NUM) {
-			if(queuednum < neednum) {
-				applog(LOG_DEBUG, "BTM: No enough work to send, queue num=%d", queuednum);
+		if (queuednum < BITMAIN_MAX_DEAL_QUEUE_NUM) {
+			if (queuednum < neednum) {
+				applog(LOG_DEBUG, "BTM: No enough work to send, queue num=%d",
+						  queuednum);
 				break;
 			}
 		}
 		sendnum = queuednum < neednum ? queuednum : neednum;
-		sendlen = bitmain_set_txtask(sendbuf, &(info->last_work_block), bitmain->works, BITMAIN_ARRAY_SIZE, bitmain->work_array, sendnum, &sendcount);
+		sendlen = bitmain_set_txtask(sendbuf, &(info->last_work_block), bitmain->works,
+					     BITMAIN_ARRAY_SIZE, bitmain->work_array, sendnum,
+					     &sendcount);
 		bitmain->queued -= sendnum;
 		info->send_full_space += sendnum;
 		if (bitmain->queued < 0)
@@ -1632,7 +1475,9 @@ static bool bitmain_fill(struct cgpu_info *bitmain)
 				senderror = 1;
 				if (info->errorcount > 1000) {
 					info->errorcount = 0;
-					applog(LOG_ERR, "%s%d: Device disappeared, shutting down thread", bitmain->drv->name, bitmain->device_id);
+					applog(LOG_ERR, "%s%d: Device disappeared,"
+							" shutting down thread",
+							bitmain->drv->name, bitmain->device_id);
 					bitmain->shutdown = true;
 				}
 				break;
@@ -1649,7 +1494,7 @@ static bool bitmain_fill(struct cgpu_info *bitmain)
 out_unlock:
 	cgtime(&now);
 	timediff = now.tv_sec - info->last_status_time.tv_sec;
-	if(timediff < 0) timediff = -timediff;
+	if (timediff < 0) timediff = -timediff;
 
 	if (now.tv_sec - info->last_status_time.tv_sec > BITMAIN_SEND_STATUS_TIME) {
 		applog(LOG_DEBUG, "BTM: Send RX Status Token fifo_space(%d) timediff(%d)",
@@ -1667,7 +1512,9 @@ out_unlock:
 				senderror = 1;
 				if (info->errorcount > 1000) {
 					info->errorcount = 0;
-					applog(LOG_ERR, "%s%d: Device disappeared, shutting down thread", bitmain->drv->name, bitmain->device_id);
+					applog(LOG_ERR, "%s%d: Device disappeared,"
+							" shutting down thread",
+							bitmain->drv->name, bitmain->device_id);
 					bitmain->shutdown = true;
 				}
 			} else {
@@ -1678,12 +1525,12 @@ out_unlock:
 			}
 		}
 	}
-	if(info->send_full_space > BITMAIN_SEND_FULL_SPACE) {
+	if (info->send_full_space > BITMAIN_SEND_FULL_SPACE) {
 		info->send_full_space = 0;
 		ret = true;
 	}
 	mutex_unlock(&info->qlock);
-	if(senderror) {
+	if (senderror) {
 		ret = true;
 		cgsleep_prepare_r(&ts_start);
 		cgsleep_ms_r(&ts_start, 50);
@@ -1698,8 +1545,6 @@ static int64_t bitmain_scanhash(struct thr_info *thr)
 	const int chain_num = info->chain_num;
 	struct timeval now, then, tdiff;
 	int64_t hash_count, us_timeout;
-	struct timespec abstime;
-	int ret;
 
 	/* Half nonce range */
 	us_timeout = 0x80000000ll / info->asic_num / info->frequency;
@@ -1707,10 +1552,7 @@ static int64_t bitmain_scanhash(struct thr_info *thr)
 	tdiff.tv_usec = us_timeout - (tdiff.tv_sec * 1000000);
 	cgtime(&now);
 	timeradd(&now, &tdiff, &then);
-	abstime.tv_sec = then.tv_sec;
-	abstime.tv_nsec = then.tv_usec * 1000;
 
-	applog(LOG_DEBUG, "bitmain_scanhash info->qlock start");
 	mutex_lock(&info->qlock);
 	hash_count = 0xffffffffull * (uint64_t)info->nonces;
 	bitmain->results += info->nonces + info->idle;
@@ -1720,7 +1562,6 @@ static int64_t bitmain_scanhash(struct thr_info *thr)
 		bitmain->results--;
 	info->nonces = info->idle = 0;
 	mutex_unlock(&info->qlock);
-	applog(LOG_DEBUG, "bitmain_scanhash info->qlock stop");
 
 	/* Check for nothing but consecutive bad results or consistently less
 	 * results than we should be getting and reset the FPGA if necessary */
@@ -1743,12 +1584,12 @@ static int64_t bitmain_scanhash(struct thr_info *thr)
 static void bitmain_flush_work(struct cgpu_info *bitmain)
 {
 	struct bitmain_info *info = bitmain->device_data;
-	int i = 0;
+	//int i = 0;
 
 	mutex_lock(&info->qlock);
 	/* Will overwrite any work queued */
 	applog(LOG_ERR, "bitmain_flush_work queued=%d array=%d", bitmain->queued, bitmain->work_array);
-	if(bitmain->queued > 0) {
+	if (bitmain->queued > 0) {
 		if (bitmain->work_array + bitmain->queued > BITMAIN_ARRAY_SIZE) {
 			bitmain->work_array = bitmain->work_array + bitmain->queued-BITMAIN_ARRAY_SIZE;
 		} else {
@@ -1757,7 +1598,7 @@ static void bitmain_flush_work(struct cgpu_info *bitmain)
 	}
 	bitmain->queued = 0;
 	//bitmain->work_array = 0;
-	//for(i = 0; i < BITMAIN_ARRAY_SIZE; i++) {
+	//for (i = 0; i < BITMAIN_ARRAY_SIZE; i++) {
 	//	bitmain->works[i] = NULL;
 	//}
 	//pthread_cond_signal(&info->qcond);
@@ -1768,8 +1609,7 @@ static struct api_data *bitmain_api_stats(struct cgpu_info *cgpu)
 {
 	struct api_data *root = NULL;
 	struct bitmain_info *info = cgpu->device_data;
-	char buf[64];
-	int i = 0;
+	//int i = 0;
 	double hwp = (cgpu->hw_errors + cgpu->diff1) ?
 			(double)(cgpu->hw_errors) / (double)(cgpu->hw_errors + cgpu->diff1) : 0;
 
@@ -1827,17 +1667,6 @@ static void bitmain_shutdown(struct thr_info *thr)
 	do_bitmain_close(thr);
 }
 
-char *set_bitmain_dev(char *arg)
-{
-	if(arg == NULL || strlen(arg) <= 0) {
-		memcpy(opt_bitmain_dev, 0, 256);
-	} else {
-		strncpy(opt_bitmain_dev, arg, 256);
-	}
-	applog(LOG_DEBUG, "BTM set device: %s", opt_bitmain_dev);
-	return NULL;
-}
-
 char *set_bitmain_fan(char *arg)
 {
 	int val1, val2, ret;
@@ -1877,12 +1706,14 @@ char *set_bitmain_freq(char *arg)
 
 	return NULL;
 }
+#endif
 
-struct device_drv bitmain_drv = {
-	.drv_id = DRIVER_bitmain,
-	.dname = "Bitmain",
-	.name = "BTM",
-	.drv_detect = bitmain_detect,
+struct device_drv ants1_drv = {
+	.drv_id = DRIVER_ants1,
+	.dname = "BitmainAntS1",
+	.name = "ANT",
+	.drv_detect = ants1_detect,
+#ifdef LINUX
 	.thread_prepare = bitmain_prepare,
 	.hash_work = hash_queued_work,
 	.queue_full = bitmain_fill,
@@ -1892,4 +1723,5 @@ struct device_drv bitmain_drv = {
 	.get_statline_before = get_bitmain_statline_before,
 	.reinit_device = bitmain_init,
 	.thread_shutdown = bitmain_shutdown,
+#endif
 };
diff --git a/driver-bitmain.h b/driver-bitmain.h
index 33d9773..febbf4b 100644
--- a/driver-bitmain.h
+++ b/driver-bitmain.h
@@ -1,6 +1,7 @@
 /*
  * Copyright 2013 BitMain project
  * Copyright 2013 BitMain <xlc1985@126.com>
+ * Copyright 2014 Andrew Smith
  *
  * 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
@@ -11,7 +12,7 @@
 #ifndef BITMAIN_H
 #define BITMAIN_H
 
-#ifdef USE_BITMAIN
+#ifdef USE_ANT_S1
 
 #include "util.h"
 
@@ -197,7 +198,6 @@ struct bitmain_info {
 
 	struct thr_info *thr;
 	pthread_t read_thr;
-	pthread_t write_thr;
 	pthread_mutex_t lock;
 	pthread_mutex_t qlock;
 	pthread_cond_t qcond;
@@ -232,7 +232,6 @@ struct bitmain_info {
 ASSERT1(sizeof(uint32_t) == 4);
 
 extern struct bitmain_info **bitmain_info;
-extern char opt_bitmain_dev[256];
 extern int opt_bitmain_temp;
 extern int opt_bitmain_overheat;
 extern int opt_bitmain_fan_min;
@@ -240,9 +239,8 @@ extern int opt_bitmain_fan_max;
 extern int opt_bitmain_freq_min;
 extern int opt_bitmain_freq_max;
 extern bool opt_bitmain_auto;
-extern char *set_bitmain_dev(char *arg);
 extern char *set_bitmain_fan(char *arg);
 extern char *set_bitmain_freq(char *arg);
 
-#endif /* USE_BITMAIN */
+#endif /* USE_ANT_S1 */
 #endif	/* BITMAIN_H */
diff --git a/miner.h b/miner.h
index e0e7e52..90d3989 100644
--- a/miner.h
+++ b/miner.h
@@ -244,6 +244,7 @@ static inline int fsync (int fd)
 	DRIVER_ADD_COMMAND(drillbit) \
 	DRIVER_ADD_COMMAND(bab) \
 	DRIVER_ADD_COMMAND(minion) \
+	DRIVER_ADD_COMMAND(ants1) \
 	DRIVER_ADD_COMMAND(avalon2) \
 	DRIVER_ADD_COMMAND(avalon)
 
@@ -430,7 +431,7 @@ struct cgpu_info {
 	struct cg_usb_info usbinfo;
 	bool blacklisted;
 #endif
-#if defined(USE_AVALON) || defined(USE_AVALON2)
+#if defined(USE_AVALON) || defined(USE_AVALON2) || defined(USE_ANT_S1)
 	struct work **works;
 	int work_array;
 	int queued;
@@ -988,6 +989,10 @@ extern char *opt_bab_options;
 #ifdef USE_BITMINE_A1
 extern char *opt_bitmine_a1_options;
 #endif
+#ifdef USE_ANT_S1
+extern char *opt_bitmain_options;
+extern bool opt_bitmain_hwerror;
+#endif
 #ifdef USE_USBUTILS
 extern char *opt_usb_select;
 extern int opt_usbdump;
@@ -1305,7 +1310,7 @@ struct work {
 	int		gbt_txns;
 
 	unsigned int	work_block;
-	int		id;
+	uint32_t	id;
 	UT_hash_handle	hh;
 
 	double		work_difficulty;
@@ -1382,6 +1387,8 @@ extern struct work *get_queue_work(struct thr_info *thr, struct cgpu_info *cgpu,
 extern struct work *__find_work_bymidstate(struct work *que, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
 extern struct work *find_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
 extern struct work *clone_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
+extern struct work *__find_work_byid(struct work *que, uint32_t id);
+extern struct work *find_queued_work_byid(struct cgpu_info *cgpu, uint32_t id);
 extern void __work_completed(struct cgpu_info *cgpu, struct work *work);
 extern int age_queued_work(struct cgpu_info *cgpu, double secs);
 extern void work_completed(struct cgpu_info *cgpu, struct work *work);
diff --git a/usbutils.h b/usbutils.h
index c47e41e..4a40475 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -405,7 +405,14 @@ struct cg_usb_info {
 	USB_ADD_COMMAND(C_MCP_SETSPISETTING, "MCPSetSPISetting") \
 	USB_ADD_COMMAND(C_MCP_GETSPISETTING, "MCPGetSPISetting") \
 	USB_ADD_COMMAND(C_MCP_SPITRANSFER, "MCPSPITransfer") \
-	USB_ADD_COMMAND(C_MCP_SPICANCEL, "MCPSPICancel")
+	USB_ADD_COMMAND(C_MCP_SPICANCEL, "MCPSPICancel") \
+	USB_ADD_COMMAND(C_BITMAIN_SEND, "BitmainSend") \
+	USB_ADD_COMMAND(C_BITMAIN_READ, "BitmainRead") \
+	USB_ADD_COMMAND(C_BITMAIN_TOKEN_TXCONFIG, "BitmainTokenTxConfig") \
+	USB_ADD_COMMAND(C_BITMAIN_TOKEN_TXTASK, "BitmainTokenTxTask") \
+	USB_ADD_COMMAND(C_BITMAIN_TOKEN_RXSTATUS, "BitmainTokenRxStatus") \
+	USB_ADD_COMMAND(C_BITMAIN_DATA_RXSTATUS, "BitmainDataRxStatus") \
+	USB_ADD_COMMAND(C_BITMAIN_DATA_RXNONCE, "BitmainDataRxNonce")
 
 /* Create usb_cmds enum from USB_PARSE_COMMANDS macro */
 enum usb_cmds {