Commit 424c9565346227b9281b7e0ff047fc817f8d0612

Henrik Nordstrom 2013-11-23T20:07:37

Merge branch 'master' of https://github.com/ckolivas/cgminer

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
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
diff --git a/01-cgminer.rules b/01-cgminer.rules
index 972468c..733a774 100644
--- a/01-cgminer.rules
+++ b/01-cgminer.rules
@@ -30,3 +30,6 @@ ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="f60a", SUBSYSTEMS=="usb", ACTION=="a
 
 #HashFast
 ATTRS{idVendor}=="297c", ATTRS{idProduct}=="0001", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev", ENV{ID_MM_DEVICE_IGNORE}="1"
+
+# BXF
+ATTRS{idVendor}=="198c", ATTRS{idProduct}=="b1f1", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
diff --git a/ASIC-README b/ASIC-README
index bf83045..a535c2b 100644
--- a/ASIC-README
+++ b/ASIC-README
@@ -3,8 +3,8 @@ SUPPORTED DEVICES
 Currently supported devices include the Avalon (including BitBurner and
 Klondike), the Butterfly Labs SC range of devices, the ASICMINER block
 erupters, the BF1 (bitfury) USB (red and blue) devices, KnCminer Mercury,
-Saturn and Jupiter devices, BlackArrow Bitfury devices and upcoming
-Hashfast devices.
+Saturn and Jupiter devices, BlackArrow Bitfury devices, Bi*fury USB devices
+and upcoming Hashfast devices.
 
 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
@@ -32,7 +32,7 @@ ASICMINER block erupters will come up as AMU.
 ASICMINER devices need the --enable-icarus option when compiling cgminer.
 Also note that the AMU is managed by the Icarus driver which is detailed
 in the FPGA-README. Configuring them uses the same mechanism as outlined
-below for getting started with butterfly labs ASICs.
+below for getting started with USB ASICs.
 
 
 BlackArrow Bitfury devices
@@ -47,13 +47,14 @@ BITFURY devices
 
 Bitfury devices need the --enable-bitfury option when compiling cgminer.
 
-Currently only the BPMC BF1 devices AKA redfury/bluefury are supported and
-come up as BF1. There are no options available for them. Bitfury device are
-also set up as per the butterfly labs ASICs below.
+Currently the BPMC/BGMC BF1 devices AKA redfury/bluefury are supported and
+come up as BF1, along with the Bi*fury USB devices which come up as BXF.
+There are no options available for them. Bitfury USB devices are also set up
+as per the USB ASICs below.
 
 
-
-GETTING STARTED WITH BUTTERFLY LABS ASICS
+---
+GETTING STARTED WITH USB ASICS
 
 Unlike other software, cgminer uses direct USB communication instead of the
 ancient serial USB communication to be much faster, more reliable and use a
@@ -64,9 +65,13 @@ devices requires different drivers.
 WINDOWS:
 
 On windows, the direct USB support requires the installation of a WinUSB
-driver (NOT the ftdi_sio driver), and attach it to the Butterfly labs device.
-The easiest way to do this is to use the zadig utility which will install the
-drivers for you and then once you plug in your device you can choose the
+driver (NOT the ftdi_sio driver), and attach it to the chosen USB device.
+When configuring your device, plug it in and wait for windows to attempt to
+install a driver on its own. It may think it has succeeded or failed but wait
+for it to finish regardless. This is NOT the driver you want installed. At this
+point you need to associate your device with the WinUSB driver. The easiest
+way to do this is to use the zadig utility which you must right click on and
+run as administrator. Then once you plug in your device you can choose the
 "list all devices" from the "option" menu and you should be able to see the
 device as something like: "BitFORCE SHA256 SC". Choose the install or replace
 driver option and select WinUSB. You can either google for zadig or download
@@ -103,6 +108,18 @@ After this you can either manually restart udev and re-login, or more easily
 just reboot.
 
 
+OSX:
+
+On OSX, like Linux, no drivers need to be installed. However some devices
+like the bitfury USB sticks automatically load a driver thinking they're a
+modem and the driver needs to be unloaded for cgminer to work:
+
+sudo kextunload -b com.apple.driver.AppleUSBCDC
+sudo kextunload -b com.apple.driver.AppleUSBCDCACMData
+
+
+---
+
 ASIC SPECIFIC COMMANDS
 
 --avalon-auto       Adjust avalon overclock frequency dynamically for best hashrate
diff --git a/Makefile.am b/Makefile.am
index 05002bc..b791eba 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -69,7 +69,7 @@ cgminer_SOURCES += driver-bitforce.c
 endif
 
 if HAS_HASHFAST
-cgminer_SOURCES += driver-hashfast.c driver-hashfast.h hf_protocol.h
+cgminer_SOURCES += driver-hashfast.c driver-hashfast.h hf_protocol.h hf_protocol_be.h
 endif
 
 if HAS_BITFURY
diff --git a/NEWS b/NEWS
index a1209b7..f851e42 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,129 @@
+Version 3.8.3 - 23rd November 2013
+
+- Set the bitfury device start times from when we first get valid work.
+- Fix stack corruption of zeroing too much in bf1 driver.
+- Make usb_detect return the cgpu associated with it to check if it succeeds to
+decide on whether to increment the device count or not.
+- Set tv work start time for bxf driver.
+- Age the bxf work items over 90 seconds, not the bf1 work items.
+- Zero the read buffer in _usb_read to avoid stale data and only use stack
+memory instead of using the bulkbuf since it is only used in _usb_read.
+- Leave room for temperatures above 100 degrees and pad consistently for bxf
+statline.
+- Drop json stratum auth failed message log level to verbose.
+- Change the processed value not the bufsiz in response to an end of message
+marker.
+- Don't lose data beyond the end of message in a usb read.
+- Silence irrelevant warning.
+- Only check strlen on end if end exists.
+- Simplify the end of message detection in _usb_read and allow it to return
+without doing another read if the message is already in the buffer.
+- Increase work ageing time to 90 seconds for bxf driver to account for firmware
+changes.
+- Use the age_queued_work function in the bitfury driver.
+- Provide a function to discard queued work based on age.
+- The json_val in api.c is a borrowed reference, not a new one so don't decref
+it.
+- Decrement json references in api.c to not leak memory.
+- line 2913 added urlencode
+- With reliable writes to the avalon there is no need for the sleep delays
+between writes.
+- There is no need to limit usb write transfers to maxpacketsize and it's
+harmful for large transfers on slow devices such as wrt routers.
+- Disable USB stats which were not meant to be enabled by default and add extra
+memory for a memory error when stats are enabled.
+- Set limit and count to integers to not overflow during failed hotplug attempts
+and then not trying again.
+- Update api example compilation instructions.
+
+
+Version 3.8.2 - 16th November 2013
+
+- Add more verbose documentation to the readme files for windows users.
+- Add more information on libusb failure to init telling users to check README
+file.
+- Add information on unloading cdc drivers on osx to README
+- Prevent a deadlock with use of restart_threads by spawning a thread to send
+the driver flush work messages.
+- Set priority of various threads if possible.
+- Add bxf data to api output.
+- Do not hold the mining thread lock in restart_threads when calling the driver
+flush work commands.
+- Send extra work regularly to the bxf device and parse the needwork command by
+sending the amount of work it requests.
+- Allow messages to have arbitrary offsets in the bxf parser in case we have
+lingering buffered data.
+- Send the maxroll command to the bxf driver and store the value to see if we
+need to update it.
+- Add sending of flush command to bxf on flush_work
+- Add flush and version commands to bxf start up, flush buffer and try to parse
+version response string.
+- Abstract out bxf recv message.
+- Add extra bxf commands to usbutils
+- Abstract out bxf send message to allow us to easily add extra commands.
+- Don't run device restart code if the device is not enabled.
+- Expand size of bitfury statline
+- Various driver fixes for bitfury devices, including a flag from when first
+valid work appears.
+- Look up work results in bxf driver from correct variable.
+- Correct incorrect error code in bxf driver for usb writes and add debugging.
+- Add bxf details to usbutils.
+- Implement a statline showing temperature for bxf
+- Add api data for bxf device, sharing the hashrate function with bf1.
+- Count no matching work as a hw error on bxf
+- Add BXF to udev rules.
+- Work id should be hexadecimal in bxf messages.
+- Add unrecognised string debugging to bxf driver.
+- Implement the main scanloop for bxf, trying to prevent it from ntime rolling
+work if the work protocol does not allow it.
+- Parse bxf work submits fully, submitting the results.
+- Provide a function for setting the work ntime.
+- Implement a skeleton parse bxf submit function.
+- Use the bxf read thread to set the device target and send its first work item.
+- Implement a bxf send work function and set update and restart functions to
+sending new work since that's the equivalent for that device.
+- Add temperature parsing to bxf driver
+- Create and destroy a basic bxf read thread.
+- Remove the buffer from bitfury info since it is only used on one pass in the
+bf1 device.
+- Add a rudimentary bxf detect one function.
+- Rename all bf1 specific functions in the bitfury driver, using a switch to
+choose correct function.
+- Rename bitfury_getinfo to bf1_getinfo since it's unique to bf1 devices.
+- Separate out the bf1 reset from bitfury reset.
+- Store the bitfury identity in the info struct.
+- BaB - updated tested OS comment
+- Uniquely identify the BF1 and BXF bitfury devices.
+- Remove the default libusb WinUsb pipe policies that don't suit us.
+- Only set the winusb pipe policy if it doesn't match our requirements instead
+of every transfer.
+- klondike - dont try to flush if not initialised
+- api.c trylock() add missing locklock
+- Use our new zero length packet support directly in windows.
+- Enable support for zero length packet on windows and auto clear pipe stalls.
+- util.c: Decreasing reference count on allocated JSON obects to prevent memory
+leak
+- api.c: Release apisock on error in api()
+- api.c: Release io_data->ptr when releasing io_data in io_free()
+- We can't connect to a GBT pool at all with fix protocol enabled.
+- Initialise the stgd lock mutex earlier to prevent dereferences when pool
+testing occurs before it.
+- Klondike support I2C USB layout also - as KLI
+- Return error codes in avalon_read() if they're not timeouts.
+- Break out of the avalon idle loop if we get a send error.
+- Set avalon ftdi latency to just less than the time it would take to fill the
+ftdi buffer at 115200 baud
+- Update example.conf
+- Only limit packetsize on usb out writes.
+- We must chop up every 64 bytes returned on an ftdi chip, not just the first 2
+bytes so revert to parsing the data internally in the avalon instead of using
+usbutils' simple ftdi parser.
+- Only retry 3 times in hfa_reset.
+- Only add_cgpu in hashfast driver once we have a real driver set up.
+- Clean up properly if hfa_detect_common fails in the hashfast driver.
+- --shares should be scaled to diff1 not absolute number of shares
+
+
 Version 3.8.1 - 11th November 2013
 
 - Revert "Send a zero length packet at the end of every usb transfer on windows
diff --git a/README b/README
index 3e19f78..cf2420e 100644
--- a/README
+++ b/README
@@ -235,7 +235,8 @@ See FGPA-README for more information regarding this.
 
 Cgminer should automatically find all of your Avalon ASIC, BFL ASIC, BitForce
 FPGAs, Icarus bitstream FPGAs, Klondike ASIC, ASICMINER usb block erupters,
-KnC ASICs, BaB ASICs, Hashfast ASICs and ModMiner FPGAs.
+KnC ASICs, BaB ASICs, Hashfast ASICs, ModMiner FPGAs, BPMC/BGMC BF1 USB ASICs
+and Bi*fury USB ASICs.
 
 ---
 
@@ -244,14 +245,24 @@ SETTING UP USB DEVICES
 WINDOWS:
 
 On windows, the direct USB support requires the installation of a WinUSB
-driver (NOT the ftdi_sio driver), and attach it to your devices.
-The easiest way to do this is to use the zadig utility which will install the
-drivers for you and then once you plug in your device you can choose the
+driver (NOT the ftdi_sio driver), and attach it to the chosen USB device.
+When configuring your device, plug it in and wait for windows to attempt to
+install a driver on its own. It may think it has succeeded or failed but wait
+for it to finish regardless. This is NOT the driver you want installed. At this
+point you need to associate your device with the WinUSB driver. The easiest
+way to do this is to use the zadig utility which you must right click on and
+run as administrator. Then once you plug in your device you can choose the
 "list all devices" from the "option" menu and you should be able to see the
 device as something like: "BitFORCE SHA256 SC". Choose the install or replace
 driver option and select WinUSB. You can either google for zadig or download
 it from the cgminer directory in the DOWNLOADS link above.
 
+When you first switch a device over to WinUSB with zadig and it shows that
+correctly on the left of the zadig window, but it still gives permission
+errors, you may need to unplug the USB miner and then plug it back in. Some
+users may need to reboot at this point.
+
+
 LINUX:
 
 On linux, the direct USB support requires no drivers at all. However due to
@@ -276,6 +287,19 @@ directory with the following command:
 After this you can either manually restart udev and re-login, or more easily
 just reboot.
 
+
+OSX:
+
+On OSX, like Linux, no drivers need to be installed. However some devices
+like the bitfury USB sticks automatically load a driver thinking they're a
+modem and the driver needs to be unloaded for cgminer to work:
+
+sudo kextunload -b com.apple.driver.AppleUSBCDC
+sudo kextunload -b com.apple.driver.AppleUSBCDCACMData
+
+
+---
+
 Advanced USB options:
 
 The --usb option can restrict how many Avalon, BFL ASIC, BitForce FPGAs,
diff --git a/api-example.c b/api-example.c
index d14b6aa..26d5a0e 100644
--- a/api-example.c
+++ b/api-example.c
@@ -8,7 +8,7 @@
  */
 
 /* Compile:
- *   gcc api-example.c -Icompat/jansson -Icompat/libusb-1.0/libusb -o cgminer-api
+ *   gcc api-example.c -Icompat/jansson-2.5 -Icompat/libusb-1.0/libusb -o cgminer-api
  */
 
 #include "config.h"
diff --git a/api.c b/api.c
index c811c61..bbc8e17 100644
--- a/api.c
+++ b/api.c
@@ -1626,10 +1626,14 @@ uint64_t api_trylock(void *lock, const char *file, const char *func, const int l
 	LOCKINFO *info;
 	uint64_t id;
 
+	locklock();
+
 	info = findlock(lock, CGLOCK_UNKNOWN, file, func, linenum);
 	id = lock_id++;
 	addgettry(info, id, file, func, linenum, false);
 
+	lockunlock();
+
 	return id;
 }
 
@@ -4378,7 +4382,7 @@ void api(int api_thr_id)
 	struct sockaddr_in cli;
 	socklen_t clisiz;
 	char cmdbuf[100];
-	char *cmd;
+	char *cmd = NULL;
 	char *param;
 	bool addrok;
 	char group;
@@ -4586,6 +4590,7 @@ void api(int api_thr_id)
 								}
 							}
 						}
+						json_decref(json_config);
 					}
 				}
 
diff --git a/cgminer.c b/cgminer.c
index 3634234..65ca941 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -2484,7 +2484,7 @@ share_result(json_t *val, json_t *res, json_t *err, const struct work *work,
 				       hashshow, cgpu->drv->name, cgpu->device_id, resubmit ? "(resubmit)" : "", worktime);
 		}
 		sharelog("accept", work);
-		if (opt_shares && total_accepted >= opt_shares) {
+		if (opt_shares && total_diff_accepted >= opt_shares) {
 			applog(LOG_WARNING, "Successfully mined %d accepted shares as requested and exiting.", opt_shares);
 			kill_work();
 			return;
@@ -3529,6 +3529,17 @@ static void _copy_work(struct work *work, const struct work *base_work, int noff
 		work->coinbase = strdup(base_work->coinbase);
 }
 
+void set_work_ntime(struct work *work, int ntime)
+{
+	uint32_t *work_ntime = (uint32_t *)(work->data + 68);
+
+	*work_ntime = htobe32(ntime);
+	if (work->ntime) {
+		free(work->ntime);
+		work->ntime = bin2hex((unsigned char *)work_ntime, 4);
+	}
+}
+
 /* Generates a copy of an existing work struct, creating fresh heap allocations
  * for all dynamically allocated arrays within the struct. noffset is used for
  * when a driver has internally rolled the ntime, noffset is a relative value.
@@ -3851,11 +3862,13 @@ int restart_wait(struct thr_info *thr, unsigned int mstime)
 	
 static void flush_queue(struct cgpu_info *cgpu);
 
-static void restart_threads(void)
+static void *restart_thread(void __maybe_unused *arg)
 {
 	struct pool *cp = current_pool();
 	struct cgpu_info *cgpu;
-	int i;
+	int i, mt;
+
+	pthread_detach(pthread_self());
 
 	/* Artificially set the lagging flag to avoid pool not providing work
 	 * fast enough  messages after every long poll */
@@ -3865,15 +3878,19 @@ static void restart_threads(void)
 	discard_stale();
 
 	rd_lock(&mining_thr_lock);
-	for (i = 0; i < mining_threads; i++) {
+	mt = mining_threads;
+	rd_unlock(&mining_thr_lock);
+
+	for (i = 0; i < mt; i++) {
 		cgpu = mining_thr[i]->cgpu;
 		if (unlikely(!cgpu))
 			continue;
+		if (cgpu->deven != DEV_ENABLED)
+			continue;
 		mining_thr[i]->work_restart = true;
 		flush_queue(cgpu);
 		cgpu->drv->flush_work(cgpu);
 	}
-	rd_unlock(&mining_thr_lock);
 
 	mutex_lock(&restart_lock);
 	pthread_cond_broadcast(&restart_cond);
@@ -3885,6 +3902,17 @@ static void restart_threads(void)
 	 * early. */
 	cancel_usb_transfers();
 #endif
+	return NULL;
+}
+
+/* In order to prevent a deadlock via the various drv->flush_work
+ * implementations we send the restart messages via a separate thread. */
+static void restart_threads(void)
+{
+	pthread_t rthread;
+
+	if (unlikely(pthread_create(&rthread, NULL, restart_thread, NULL)))
+		quit(1, "Failed to create restart thread");
 }
 
 static void signal_work_update(void)
@@ -4436,6 +4464,30 @@ void zero_stats(void)
 	}
 }
 
+static void set_highprio(void)
+{
+#ifndef WIN32
+	int ret = nice(-10);
+
+	if (!ret)
+		applog(LOG_DEBUG, "Unable to set thread to high priority");
+#else
+	SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
+#endif
+}
+
+static void set_lowprio(void)
+{
+#ifndef WIN32
+	int ret = nice(10);
+
+	if (!ret)
+		applog(LOG_INFO, "Unable to set thread to low priority");
+#else
+	SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST);
+#endif
+}
+
 #ifdef HAVE_CURSES
 static void display_pools(void)
 {
@@ -4841,6 +4893,7 @@ static void *api_thread(void *userdata)
 
 	RenameThread("api");
 
+	set_lowprio();
 	api(api_thr_id);
 
 	PTH(mythr) = 0L;
@@ -5503,7 +5556,7 @@ retry_stratum:
 	}
 
 	/* Probe for GBT support on first pass */
-	if (!pool->probed && !opt_fix_protocol) {
+	if (!pool->probed) {
 		applog(LOG_DEBUG, "Probing for GBT support");
 		val = json_rpc_call(curl, pool->rpc_url, pool->rpc_userpass,
 				    gbt_req, true, false, &rolltime, pool, false);
@@ -6330,6 +6383,30 @@ void __work_completed(struct cgpu_info *cgpu, struct work *work)
 	cgpu->queued_count--;
 	HASH_DEL(cgpu->queued_work, work);
 }
+
+/* This iterates over a queued hashlist finding work started more than secs
+ * seconds ago and discards the work as completed. The driver must set the
+ * work->tv_work_start value appropriately. Returns the number of items aged. */
+int age_queued_work(struct cgpu_info *cgpu, double secs)
+{
+	struct work *work, *tmp;
+	struct timeval tv_now;
+	int aged = 0;
+
+	cgtime(&tv_now);
+
+	wr_lock(&cgpu->qlock);
+	HASH_ITER(hh, cgpu->queued_work, work, tmp) {
+		if (tdiff(&tv_now, &work->tv_work_start) > secs) {
+			__work_completed(cgpu, work);
+			aged++;
+		}
+	}
+	wr_unlock(&cgpu->qlock);
+
+	return aged;
+}
+
 /* This function should be used by queued device drivers when they're sure
  * the work struct is no longer in use. */
 void work_completed(struct cgpu_info *cgpu, struct work *work)
@@ -6501,6 +6578,7 @@ void *miner_thread(void *userdata)
 	applog(LOG_DEBUG, "Waiting on sem in miner thread");
 	cgsem_wait(&mythr->sem);
 
+	set_highprio();
 	drv->hash_work(mythr);
 out:
 	drv->thread_shutdown(mythr);
@@ -6779,6 +6857,8 @@ static void *watchpool_thread(void __maybe_unused *userdata)
 
 	RenameThread("watchpool");
 
+	set_lowprio();
+
 	while (42) {
 		struct timeval now;
 		int i;
@@ -6862,6 +6942,7 @@ static void *watchdog_thread(void __maybe_unused *userdata)
 
 	RenameThread("watchdog");
 
+	set_lowprio();
 	memset(&zero_tv, 0, sizeof(struct timeval));
 	cgtime(&rotate_tv);
 
@@ -7112,9 +7193,9 @@ void print_summary(void)
 	}
 
 	if (opt_shares) {
-		applog(LOG_WARNING, "Mined %d accepted shares of %d requested\n", total_accepted, opt_shares);
-		if (opt_shares > total_accepted)
-			applog(LOG_WARNING, "WARNING - Mined only %d shares of %d requested.", total_accepted, opt_shares);
+		applog(LOG_WARNING, "Mined %.0f accepted shares of %d requested\n", total_diff_accepted, opt_shares);
+		if (opt_shares > total_diff_accepted)
+			applog(LOG_WARNING, "WARNING - Mined only %.0f shares of %d requested.", total_diff_accepted, opt_shares);
 	}
 	applog(LOG_WARNING, " ");
 
@@ -7649,6 +7730,8 @@ static void *hotplug_thread(void __maybe_unused *userdata)
 
 	RenameThread("hotplug");
 
+	set_lowprio();
+
 	hotplug_mode = true;
 
 	cgsleep_ms(5000);
@@ -7780,6 +7863,13 @@ int main(int argc, char *argv[])
 	if (unlikely(pthread_cond_init(&gws_cond, NULL)))
 		quit(1, "Failed to pthread_cond_init gws_cond");
 
+	/* Create a unique get work queue */
+	getq = tq_new();
+	if (!getq)
+		quit(1, "Failed to create getq");
+	/* We use the getq mutex as the staged lock */
+	stgd_lock = &getq->mutex;
+
 	initialise_usb();
 
 	snprintf(packagename, sizeof(packagename), "%s %s", PACKAGE, VERSION);
@@ -8006,13 +8096,6 @@ int main(int argc, char *argv[])
 			quit(1, "Failed to calloc mining_thr[%d]", i);
 	}
 
-	/* Create a unique get work queue */
-	getq = tq_new();
-	if (!getq)
-		quit(1, "Failed to create getq");
-	/* We use the getq mutex as the staged lock */
-	stgd_lock = &getq->mutex;
-
 	if (opt_benchmark)
 		goto begin_bench;
 
diff --git a/compat/libusb-1.0/libusb/os/windows_usb.c b/compat/libusb-1.0/libusb/os/windows_usb.c
index afc4dfc..ac359c4 100644
--- a/compat/libusb-1.0/libusb/os/windows_usb.c
+++ b/compat/libusb-1.0/libusb/os/windows_usb.c
@@ -1822,9 +1822,6 @@ static int windows_submit_transfer(struct usbi_transfer *itransfer)
 		return submit_control_transfer(itransfer);
 	case LIBUSB_TRANSFER_TYPE_BULK:
 	case LIBUSB_TRANSFER_TYPE_INTERRUPT:
-		if (IS_XFEROUT(transfer) &&
-		    transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET)
-			return LIBUSB_ERROR_NOT_SUPPORTED;
 		return submit_bulk_transfer(itransfer);
 	case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
 		return submit_iso_transfer(itransfer);
@@ -2391,20 +2388,8 @@ static int winusb_configure_endpoints(struct libusb_device_handle *dev_handle, i
 			PIPE_TRANSFER_TIMEOUT, sizeof(ULONG), &timeout)) {
 			usbi_dbg("failed to set PIPE_TRANSFER_TIMEOUT for control endpoint %02X", endpoint_address);
 		}
-		if (i == -1) continue;	// Other policies don't apply to control endpoint
-		policy = false;
-		if (!WinUsb_SetPipePolicy(winusb_handle, endpoint_address,
-			SHORT_PACKET_TERMINATE, sizeof(UCHAR), &policy)) {
-			usbi_dbg("failed to disable SHORT_PACKET_TERMINATE for endpoint %02X", endpoint_address);
-		}
-		if (!WinUsb_SetPipePolicy(winusb_handle, endpoint_address,
-			IGNORE_SHORT_PACKETS, sizeof(UCHAR), &policy)) {
-			usbi_dbg("failed to disable IGNORE_SHORT_PACKETS for endpoint %02X", endpoint_address);
-		}
-		if (!WinUsb_SetPipePolicy(winusb_handle, endpoint_address,
-			ALLOW_PARTIAL_READS, sizeof(UCHAR), &policy)) {
-			usbi_dbg("failed to disable ALLOW_PARTIAL_READS for endpoint %02X", endpoint_address);
-		}
+		if (i == -1)
+			continue;	// Other policies don't apply to control endpoint
 		policy = true;
 		if (!WinUsb_SetPipePolicy(winusb_handle, endpoint_address,
 			AUTO_CLEAR_STALL, sizeof(UCHAR), &policy)) {
@@ -2649,6 +2634,8 @@ static int winusb_submit_bulk_transfer(struct usbi_transfer *itransfer)
 	bool ret;
 	int current_interface;
 	struct winfd wfd;
+	ULONG ppolicy = sizeof(UCHAR);
+	UCHAR policy;
 
 	CHECK_WINUSB_AVAILABLE;
 
@@ -2671,9 +2658,20 @@ static int winusb_submit_bulk_transfer(struct usbi_transfer *itransfer)
 	}
 
 	if (IS_XFERIN(transfer)) {
-		usbi_dbg("reading %d bytes", transfer->length);
+		WinUsb_GetPipePolicy(wfd.handle, transfer->endpoint, AUTO_CLEAR_STALL, &ppolicy, &policy);
+		if (!policy) {
+			policy = TRUE;
+			WinUsb_SetPipePolicy(wfd.handle, transfer->endpoint, AUTO_CLEAR_STALL, ppolicy, &policy);
+		}
 		ret = WinUsb_ReadPipe(wfd.handle, transfer->endpoint, transfer->buffer, transfer->length, NULL, wfd.overlapped);
 	} else {
+		if (transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
+			WinUsb_GetPipePolicy(wfd.handle, transfer->endpoint, SHORT_PACKET_TERMINATE, &ppolicy, &policy);
+			if (!policy) {
+				policy = TRUE;
+				WinUsb_SetPipePolicy(wfd.handle, transfer->endpoint, SHORT_PACKET_TERMINATE, ppolicy, &policy);
+			}
+		}
 		usbi_dbg("writing %d bytes", transfer->length);
 		ret = WinUsb_WritePipe(wfd.handle, transfer->endpoint, transfer->buffer, transfer->length, NULL, wfd.overlapped);
 	}
diff --git a/configure.ac b/configure.ac
index 4caa4e7..10b0eea 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2,7 +2,7 @@
 ##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##
 m4_define([v_maj], [3])
 m4_define([v_min], [8])
-m4_define([v_mic], [1])
+m4_define([v_mic], [3])
 ##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##
 m4_define([v_ver], [v_maj.v_min.v_mic])
 m4_define([lt_rev], m4_eval(v_maj + v_min))
diff --git a/driver-avalon.c b/driver-avalon.c
index 8a892d8..aaabe55 100644
--- a/driver-avalon.c
+++ b/driver-avalon.c
@@ -142,12 +142,10 @@ static int avalon_write(struct cgpu_info *avalon, char *buf, ssize_t len, int ep
 	return AVA_SEND_OK;
 }
 
-static int avalon_send_task(const struct avalon_task *at, struct cgpu_info *avalon,
-			    struct avalon_info *info)
-
+static int avalon_send_task(const struct avalon_task *at, struct cgpu_info *avalon)
 {
 	uint8_t buf[AVALON_WRITE_SIZE + 4 * AVALON_DEFAULT_ASIC_NUM];
-	int delay, ret, i, ep = C_AVALON_TASK;
+	int ret, i, ep = C_AVALON_TASK;
 	uint32_t nonce_range;
 	size_t nr_len;
 
@@ -188,10 +186,6 @@ static int avalon_send_task(const struct avalon_task *at, struct cgpu_info *aval
 	tt |= ((buf[4] & 0x80) ? (1 << 0) : 0);
 	buf[4] = tt;
 #endif
-	delay = nr_len * 10 * 1000000;
-	delay = delay / info->baud;
-	delay += 4000;
-
 	if (at->reset) {
 		ep = C_AVALON_RESET;
 		nr_len = 1;
@@ -200,15 +194,8 @@ static int avalon_send_task(const struct avalon_task *at, struct cgpu_info *aval
 		applog(LOG_DEBUG, "Avalon: Sent(%u):", (unsigned int)nr_len);
 		hexdump(buf, nr_len);
 	}
-	/* Sleep from the last time we sent data */
-	cgsleep_us_r(&info->cgsent, info->send_delay);
-
-	cgsleep_prepare_r(&info->cgsent);
 	ret = avalon_write(avalon, (char *)buf, nr_len, ep);
 
-	applog(LOG_DEBUG, "Avalon: Sent: Buffer delay: %dus", info->send_delay);
-	info->send_delay = delay;
-
 	return ret;
 }
 
@@ -291,14 +278,31 @@ static inline bool avalon_cts(char c)
 
 static int avalon_read(struct cgpu_info *avalon, char *buf, size_t bufsize, int ep)
 {
-	int err, amount;
+	size_t total = 0, readsize = bufsize + 2;
+	char readbuf[AVALON_READBUF_SIZE];
+	int err, amount, ofs = 2, cp;
 
-	err = usb_read_once(avalon, buf, bufsize, &amount, ep);
+	err = usb_read_once(avalon, readbuf, readsize, &amount, ep);
 	applog(LOG_DEBUG, "%s%i: Get avalon read got err %d",
 	       avalon->drv->name, avalon->device_id, err);
-	if (unlikely(err && err != LIBUSB_ERROR_TIMEOUT))
-		amount = -1;
-	return amount;
+	if (err && err != LIBUSB_ERROR_TIMEOUT)
+		return err;
+
+	if (amount < 2)
+		goto out;
+
+	/* The first 2 of every 64 bytes are status on FTDIRL */
+	while (amount > 2) {
+		cp = amount - 2;
+		if (cp > 62)
+			cp = 62;
+		memcpy(&buf[total], &readbuf[ofs], cp);
+		total += cp;
+		amount -= cp + 2;
+		ofs += 64;
+	}
+out:
+	return total;
 }
 
 static int avalon_reset(struct cgpu_info *avalon, bool initial)
@@ -320,7 +324,7 @@ static int avalon_reset(struct cgpu_info *avalon, bool initial)
 			 AVALON_DEFAULT_FREQUENCY);
 
 	wait_avalon_ready(avalon);
-	ret = avalon_send_task(&at, avalon, info);
+	ret = avalon_send_task(&at, avalon);
 	if (unlikely(ret == AVA_SEND_ERROR))
 		return -1;
 
@@ -561,7 +565,8 @@ static void avalon_idle(struct cgpu_info *avalon, struct avalon_info *info)
 		avalon_init_task(&at, 0, 0, info->fan_pwm, info->timeout,
 				 info->asic_count, info->miner_count, 1, 1,
 				 info->frequency);
-		avalon_send_task(&at, avalon, info);
+		if (avalon_send_task(&at, avalon) == AVA_SEND_ERROR)
+			break;
 	}
 	applog(LOG_WARNING, "%s%i: Idling %d miners", avalon->drv->name, avalon->device_id, i);
 	wait_avalon_ready(avalon);
@@ -736,7 +741,7 @@ static void bitburner_get_version(struct cgpu_info *avalon)
 	}
 }
 
-static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found)
+static struct cgpu_info *avalon_detect_one(libusb_device *dev, struct usb_find_devices *found)
 {
 	int baud, miner_count, asic_count, timeout, frequency;
 	int this_option_offset;
@@ -761,7 +766,9 @@ static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found
 				 &asic_count, &timeout, &frequency,
 				 (usb_ident(avalon) == IDENT_BBF && opt_bitburner_fury_options != NULL) ? opt_bitburner_fury_options : opt_avalon_options);
 
-	avalon->usbdev->usb_type = USB_TYPE_FTDI;
+	/* Even though this is an FTDI type chip, we want to do the parsing
+	 * all ourselves so set it to std usb type */
+	avalon->usbdev->usb_type = USB_TYPE_STD;
 
 	/* We have a real Avalon! */
 	avalon_initialise(avalon);
@@ -844,7 +851,7 @@ static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found
 		bitburner_get_version(avalon);
 	}
 
-	return true;
+	return avalon;
 
 unshin:
 
@@ -857,7 +864,7 @@ shin:
 
 	avalon = usb_free_cgpu(avalon);
 
-	return false;
+	return NULL;
 }
 
 static void avalon_detect(bool __maybe_unused hotplug)
@@ -1152,7 +1159,7 @@ static void *avalon_send_tasks(void *userdata)
 			}
 			mutex_unlock(&info->qlock);
 
-			ret = avalon_send_task(&at, avalon, info);
+			ret = avalon_send_task(&at, avalon);
 
 			if (unlikely(ret == AVA_SEND_ERROR)) {
 				/* Send errors are fatal */
diff --git a/driver-avalon.h b/driver-avalon.h
index 0a11161..5b6cf83 100644
--- a/driver-avalon.h
+++ b/driver-avalon.h
@@ -65,7 +65,8 @@
 
 #define AVALON_FTDI_READSIZE 510
 #define AVALON_READBUF_SIZE 8192
-#define AVALON_LATENCY 16 /* This is basically the ftdi default anyway */
+/* Set latency to just less than full 64 byte packet size at 115200 baud */
+#define AVALON_LATENCY 4
 
 struct avalon_task {
 	uint8_t reset		:1;
@@ -144,8 +145,6 @@ struct avalon_info {
 	pthread_mutex_t lock;
 	pthread_mutex_t qlock;
 	cgsem_t qsem;
-	cgtimer_t cgsent;
-	int send_delay;
 
 	int nonces;
 	int auto_queued;
diff --git a/driver-bab.c b/driver-bab.c
index 545e8bb..7db3df1 100644
--- a/driver-bab.c
+++ b/driver-bab.c
@@ -17,7 +17,8 @@
 #include "sha2.h"
 
 /*
- * Tested on RPi running Raspbian with BlackArrow BitFury V1 16 chip GPIO board
+ * Tested on RPi running both Raspbian and Arch
+ *  with BlackArrow BitFury V1 16 chip GPIO board
  */
 
 #ifndef LINUX
diff --git a/driver-bflsc.c b/driver-bflsc.c
index f3e45c4..5dafa9c 100644
--- a/driver-bflsc.c
+++ b/driver-bflsc.c
@@ -688,7 +688,7 @@ ne:
 	return ok;
 }
 
-static bool bflsc_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
+static struct cgpu_info *bflsc_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
 {
 	struct bflsc_info *sc_info = NULL;
 	char buf[BFLSC_BUFSIZ+1];
@@ -888,7 +888,7 @@ reinit:
 	mutex_init(&bflsc->device_mutex);
 	rwlock_init(&sc_info->stat_lock);
 
-	return true;
+	return bflsc;
 
 unshin:
 
@@ -906,7 +906,7 @@ shin:
 
 	bflsc = usb_free_cgpu(bflsc);
 
-	return false;
+	return NULL;
 }
 
 static void bflsc_detect(bool __maybe_unused hotplug)
diff --git a/driver-bitforce.c b/driver-bitforce.c
index 901ba08..d4f51c1 100644
--- a/driver-bitforce.c
+++ b/driver-bitforce.c
@@ -162,7 +162,7 @@ failed:
 		mutex_unlock(&bitforce->device_mutex);
 }
 
-static bool bitforce_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
+static struct cgpu_info *bitforce_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
 {
 	char buf[BITFORCE_BUFSIZ+1];
 	int err, amount;
@@ -270,7 +270,7 @@ reinit:
 
 	mutex_init(&bitforce->device_mutex);
 
-	return true;
+	return bitforce;
 
 unshin:
 
@@ -285,7 +285,7 @@ shin:
 
 	bitforce = usb_free_cgpu(bitforce);
 
-	return false;
+	return NULL;
 }
 
 static void bitforce_detect(bool __maybe_unused hotplug)
diff --git a/driver-bitfury.c b/driver-bitfury.c
index 3331d82..7181488 100644
--- a/driver-bitfury.c
+++ b/driver-bitfury.c
@@ -18,7 +18,7 @@
 #define BF1MSGSIZE 7
 #define BF1INFOSIZE 14
 
-static void bitfury_empty_buffer(struct cgpu_info *bitfury)
+static void bf1_empty_buffer(struct cgpu_info *bitfury)
 {
 	char buf[512];
 	int amount;
@@ -28,12 +28,12 @@ static void bitfury_empty_buffer(struct cgpu_info *bitfury)
 	} while (amount);
 }
 
-static int bitfury_open(struct cgpu_info *bitfury)
+static bool bf1_open(struct cgpu_info *bitfury)
 {
 	uint32_t buf[2];
 	int err;
 
-	bitfury_empty_buffer(bitfury);
+	bf1_empty_buffer(bitfury);
 	/* Magic sequence to reset device only really needed for windows but
 	 * harmless on linux. */
 	buf[0] = 0x80250000;
@@ -53,19 +53,33 @@ static int bitfury_open(struct cgpu_info *bitfury)
 	return (err == BF1MSGSIZE);
 }
 
-static void bitfury_close(struct cgpu_info *bitfury)
+static void bf1_close(struct cgpu_info *bitfury)
 {
-	bitfury_empty_buffer(bitfury);
+	bf1_empty_buffer(bitfury);
 }
 
-static void bitfury_identify(struct cgpu_info *bitfury)
+static void bf1_identify(struct cgpu_info *bitfury)
 {
 	int amount;
 
 	usb_write(bitfury, "L", 1, &amount, C_BF1_IDENTIFY);
 }
 
-static bool bitfury_getinfo(struct cgpu_info *bitfury, struct bitfury_info *info)
+static void bitfury_identify(struct cgpu_info *bitfury)
+{
+	struct bitfury_info *info = bitfury->device_data;
+
+	switch(info->ident) {
+		case IDENT_BF1:
+			bf1_identify(bitfury);
+			break;
+		case IDENT_BXF:
+		default:
+			break;
+	}
+}
+
+static bool bf1_getinfo(struct cgpu_info *bitfury, struct bitfury_info *info)
 {
 	int amount, err;
 	char buf[16];
@@ -93,11 +107,11 @@ static bool bitfury_getinfo(struct cgpu_info *bitfury, struct bitfury_info *info
 
 	applog(LOG_INFO, "%s %d: Getinfo returned version %d, product %s serial %08x", bitfury->drv->name,
 	       bitfury->device_id, info->version, info->product, info->serial);
-	bitfury_empty_buffer(bitfury);
+	bf1_empty_buffer(bitfury);
 	return true;
 }
 
-static bool bitfury_reset(struct cgpu_info *bitfury)
+static bool bf1_reset(struct cgpu_info *bitfury)
 {
 	int amount, err;
 	char buf[16];
@@ -122,64 +136,352 @@ static bool bitfury_reset(struct cgpu_info *bitfury)
 	}
 	applog(LOG_DEBUG, "%s %d: Getreset returned %s", bitfury->drv->name,
 	       bitfury->device_id, buf);
-	bitfury_empty_buffer(bitfury);
+	bf1_empty_buffer(bitfury);
 	return true;
 }
 
-static bool bitfury_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
+static bool bxf_send_msg(struct cgpu_info *bitfury, char *buf, enum usb_cmds cmd)
 {
-	struct cgpu_info *bitfury;
-	struct bitfury_info *info;
+	int err, amount, len;
+
+	len = strlen(buf);
+	applog(LOG_DEBUG, "%s %d: Sending %s", bitfury->drv->name, bitfury->device_id, buf);
+	err = usb_write(bitfury, buf, len, &amount, cmd);
+	if (err || amount != len) {
+		applog(LOG_WARNING, "%s %d: Error %d sending %s sent %d of %d", bitfury->drv->name,
+		       bitfury->device_id, err, usb_cmdname(cmd), amount, len);
+		return false;
+	}
+	return true;
+}
 
-	bitfury = usb_alloc_cgpu(&bitfury_drv, 1);
+/* Returns the amount received only if we receive a full message, otherwise
+ * it returns the err value. */
+static int bxf_recv_msg(struct cgpu_info *bitfury, char *buf)
+{
+	int err, amount;
 
-	if (!usb_init(bitfury, dev, found))
-		goto out;
-	applog(LOG_INFO, "%s %d: Found at %s", bitfury->drv->name,
-	       bitfury->device_id, bitfury->device_path);
+	err = usb_read_nl(bitfury, buf, 512, &amount, C_BXF_READ);
+	if (amount)
+		applog(LOG_DEBUG, "%s %d: Received %s", bitfury->drv->name, bitfury->device_id, buf);
+	if (!err)
+		return amount;
+	return err;
+}
+
+/* Keep reading till the first timeout or error */
+static void bxf_clear_buffer(struct cgpu_info *bitfury)
+{
+	int err, retries = 0;
+	char buf[512];
+
+	do {
+		err = bxf_recv_msg(bitfury, buf);
+		usb_buffer_clear(bitfury);
+		if (err < 0)
+			break;
+	} while (retries++ < 10);
+}
+
+static bool bxf_send_flush(struct cgpu_info *bitfury)
+{
+	char buf[8];
+
+	sprintf(buf, "flush\n");
+	return bxf_send_msg(bitfury, buf, C_BXF_FLUSH);
+}
+
+static bool bxf_detect_one(struct cgpu_info *bitfury, struct bitfury_info *info)
+{
+	int err, retries = 0;
+	char buf[512];
+
+	if (!bxf_send_flush(bitfury))
+		return false;
+
+	bxf_clear_buffer(bitfury);
+
+	sprintf(buf, "version\n");
+	if (!bxf_send_msg(bitfury, buf, C_BXF_VERSION))
+		return false;
+
+	do {
+		err = bxf_recv_msg(bitfury, buf);
+		if (err < 0 && err != LIBUSB_ERROR_TIMEOUT)
+			return false;
+		if (err > 0 && !strncmp(buf, "version", 7)) {
+			sscanf(&buf[8], "%d.%d rev %d chips %d", &info->ver_major,
+			       &info->ver_minor, &info->hw_rev, &info->chips);
+			applog(LOG_INFO, "%s %d: Version %d.%d rev %d chips %d",
+			       bitfury->drv->name, bitfury->device_id, info->ver_major,
+			       info->ver_minor, info->hw_rev, info->chips);
+			break;
+		}
+		/* Keep parsing if the buffer is full without counting it as
+		 * a retry. */
+		if (usb_buffer_size(bitfury))
+			continue;
+	} while (retries++ < 10);
+
+	if (!add_cgpu(bitfury))
+		quit(1, "Failed to add_cgpu in bxf_detect_one");
+
+	update_usb_stats(bitfury);
+	applog(LOG_INFO, "%s %d: Successfully initialised %s",
+	       bitfury->drv->name, bitfury->device_id, bitfury->device_path);
 
-	info = calloc(sizeof(struct bitfury_info), 1);
-	if (!info)
-		quit(1, "Failed to calloc info in bitfury_detect_one");
-	bitfury->device_data = info;
-	/* This does not artificially raise hashrate, it simply allows the
-	 * hashrate to adapt quickly on starting. */
 	info->total_nonces = 1;
+	/* This unsets it to make sure it gets set on the first pass */
+	info->maxroll = -1;
 
-	if (!bitfury_open(bitfury))
+	return true;
+}
+
+static bool bf1_detect_one(struct cgpu_info *bitfury, struct bitfury_info *info)
+{
+	if (!bf1_open(bitfury))
 		goto out_close;
 
 	/* Send getinfo request */
-	if (!bitfury_getinfo(bitfury, info))
+	if (!bf1_getinfo(bitfury, info))
 		goto out_close;
 
 	/* Send reset request */
-	if (!bitfury_reset(bitfury))
+	if (!bf1_reset(bitfury))
 		goto out_close;
 
-	bitfury_identify(bitfury);
-	bitfury_empty_buffer(bitfury);
+	bf1_identify(bitfury);
+	bf1_empty_buffer(bitfury);
 
 	if (!add_cgpu(bitfury))
-		quit(1, "Failed to add_cgpu in bitfury_detect_one");
+		quit(1, "Failed to add_cgpu in bf1_detect_one");
 
 	update_usb_stats(bitfury);
 	applog(LOG_INFO, "%s %d: Successfully initialised %s",
 	       bitfury->drv->name, bitfury->device_id, bitfury->device_path);
+
+	/* This does not artificially raise hashrate, it simply allows the
+	 * hashrate to adapt quickly on starting. */
+	info->total_nonces = 1;
+
 	return true;
 out_close:
-	bitfury_close(bitfury);
-	usb_uninit(bitfury);
-out:
-	bitfury = usb_free_cgpu(bitfury);
+	bf1_close(bitfury);
 	return false;
 }
 
+static struct cgpu_info *bitfury_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
+{
+	struct cgpu_info *bitfury;
+	struct bitfury_info *info;
+	enum sub_ident ident;
+	bool ret = false;
+
+	bitfury = usb_alloc_cgpu(&bitfury_drv, 1);
+
+	if (!usb_init(bitfury, dev, found))
+		goto out;
+	applog(LOG_INFO, "%s %d: Found at %s", bitfury->drv->name,
+	       bitfury->device_id, bitfury->device_path);
+
+	info = calloc(sizeof(struct bitfury_info), 1);
+	if (!info)
+		quit(1, "Failed to calloc info in bitfury_detect_one");
+	bitfury->device_data = info;
+	info->ident = ident = usb_ident(bitfury);
+	switch (ident) {
+		case IDENT_BF1:
+			ret = bf1_detect_one(bitfury, info);
+			break;
+		case IDENT_BXF:
+			ret = bxf_detect_one(bitfury, info);
+			break;
+		default:
+			applog(LOG_INFO, "%s %d: Unrecognised bitfury device",
+			       bitfury->drv->name, bitfury->device_id);
+			break;
+	}
+
+	if (!ret) {
+		free(info);
+		usb_uninit(bitfury);
+out:
+		bitfury = usb_free_cgpu(bitfury);
+	}
+	return bitfury;
+}
+
 static void bitfury_detect(bool __maybe_unused hotplug)
 {
 	usb_detect(&bitfury_drv, bitfury_detect_one);
 }
 
+static void parse_bxf_submit(struct cgpu_info *bitfury, struct bitfury_info *info, char *buf)
+{
+	struct work *match_work, *tmp, *work = NULL;
+	struct thr_info *thr = info->thr;
+	uint32_t nonce, timestamp;
+	int workid;
+
+	if (!sscanf(&buf[7], "%x %x %x", &nonce, &workid, &timestamp)) {
+		applog(LOG_WARNING, "%s %d: Failed to parse submit response",
+		       bitfury->drv->name, bitfury->device_id);
+		return;
+	}
+
+	applog(LOG_DEBUG, "%s %d: Parsed nonce %u workid %d timestamp %u",
+	       bitfury->drv->name, bitfury->device_id, nonce, workid, timestamp);
+
+	rd_lock(&bitfury->qlock);
+	HASH_ITER(hh, bitfury->queued_work, match_work, tmp) {
+		if (match_work->subid == workid) {
+			work = copy_work(match_work);
+			break;
+		}
+	}
+	rd_unlock(&bitfury->qlock);
+
+	if (!work) {
+		/* Discard first results from any previous run */
+		if (unlikely(!info->valid))
+			return;
+
+		applog(LOG_INFO, "%s %d: No matching work", bitfury->drv->name, bitfury->device_id);
+
+		mutex_lock(&info->lock);
+		info->no_matching_work++;
+		mutex_unlock(&info->lock);
+
+		inc_hw_errors(thr);
+		return;
+	}
+	/* Set the device start time from when we first get valid results */
+	if (unlikely(!info->valid)) {
+		info->valid = true;
+		cgtime(&bitfury->dev_start_tv);
+	}
+	set_work_ntime(work, timestamp);
+	if (submit_nonce(thr, work, nonce)) {
+		mutex_lock(&info->lock);
+		info->nonces++;
+		mutex_unlock(&info->lock);
+	}
+	free_work(work);
+}
+
+static void parse_bxf_temp(struct cgpu_info *bitfury, struct bitfury_info *info, char *buf)
+{
+	unsigned int temp;
+
+	if (!sscanf(&buf[5], "%u", &temp)) {
+		applog(LOG_INFO, "%s %d: Failed to parse temperature",
+		       bitfury->drv->name, bitfury->device_id);
+		return;
+	}
+	mutex_lock(&info->lock);
+	info->temperature = (double)temp / 10;
+	mutex_unlock(&info->lock);
+}
+
+static void bxf_update_work(struct cgpu_info *bitfury, struct bitfury_info *info);
+
+static void parse_bxf_needwork(struct cgpu_info *bitfury, struct bitfury_info *info,
+			       char *buf)
+{
+	int needed;
+
+	if (!sscanf(&buf[9], "%d", &needed)) {
+		applog(LOG_INFO, "%s %d: Failed to parse needwork",
+		       bitfury->drv->name, bitfury->device_id);
+		return;
+	}
+	while (needed-- > 0)
+		bxf_update_work(bitfury, info);
+}
+
+static void *bxf_get_results(void *userdata)
+{
+	struct cgpu_info *bitfury = userdata;
+	struct bitfury_info *info = bitfury->device_data;
+	char threadname[24], buf[512];
+
+	snprintf(threadname, 24, "bxf_recv/%d", bitfury->device_id);
+
+	/* We operate the device at lowest diff since it's not a lot of results
+	 * to process and gives us a better indicator of the nonce return rate
+	 * and hardware errors. */
+	sprintf(buf, "target ffffffff\n");
+	if (!bxf_send_msg(bitfury, buf, C_BXF_TARGET))
+		goto out;
+
+	/* Read thread sends the first work item to get the device started
+	 * since it will roll ntime and make work itself from there on. */
+	bxf_update_work(bitfury, info);
+	bxf_update_work(bitfury, info);
+
+	while (likely(!bitfury->shutdown)) {
+		char *msg;
+		int err;
+
+		if (unlikely(bitfury->usbinfo.nodev))
+			break;
+
+		err = bxf_recv_msg(bitfury, buf);
+		if (err < 0) {
+			if (err != LIBUSB_ERROR_TIMEOUT)
+				break;
+			continue;
+		}
+		if (!err)
+			continue;
+
+		msg = strstr(buf, "submit");
+		if (msg) {
+			parse_bxf_submit(bitfury, info, msg);
+			continue;
+		}
+		msg = strstr(buf, "temp");
+		if (msg) {
+			parse_bxf_temp(bitfury, info, msg);
+			continue;
+		}
+		msg = strstr(buf, "needwork");
+		if (msg) {
+			parse_bxf_needwork(bitfury, info, msg);
+			continue;
+		}
+		applog(LOG_DEBUG, "%s %d: Unrecognised string %s",
+		       bitfury->drv->name, bitfury->device_id, buf);
+	}
+out:
+	return NULL;
+}
+
+static bool bxf_prepare(struct cgpu_info *bitfury, struct bitfury_info *info)
+{
+	mutex_init(&info->lock);
+	if (pthread_create(&info->read_thr, NULL, bxf_get_results, (void *)bitfury))
+		quit(1, "Failed to create bxf read_thr");
+	return true;
+}
+
+static bool bitfury_prepare(struct thr_info *thr)
+{
+	struct cgpu_info *bitfury = thr->cgpu;
+	struct bitfury_info *info = bitfury->device_data;
+
+	info->thr = thr;
+
+	switch(info->ident) {
+		case IDENT_BXF:
+			return bxf_prepare(bitfury, info);
+			break;
+		case IDENT_BF1:
+		default:
+			return true;
+	}
+}
+
 static uint32_t decnonce(uint32_t in)
 {
 	uint32_t out;
@@ -220,16 +522,31 @@ static bool bitfury_checkresults(struct thr_info *thr, struct work *work, uint32
 	return false;
 }
 
-static int64_t bitfury_scanwork(struct thr_info *thr)
+static int64_t bitfury_rate(struct bitfury_info *info)
 {
-	struct cgpu_info *bitfury = thr->cgpu;
-	struct bitfury_info *info = bitfury->device_data;
-	int amount, i, aged = 0, total = 0, ms_diff;
+	double nonce_rate;
+	int64_t ret = 0;
+
+	info->cycles++;
+	info->total_nonces += info->nonces;
+	info->saved_nonces += info->nonces;
+	info->nonces = 0;
+	nonce_rate = (double)info->total_nonces / (double)info->cycles;
+	if (info->saved_nonces >= nonce_rate) {
+		info->saved_nonces -= nonce_rate;
+		ret = (double)0xffffffff * nonce_rate;
+	}
+	return ret;
+}
+
+static int64_t bf1_scan(struct thr_info *thr, struct cgpu_info *bitfury,
+			struct bitfury_info *info)
+{
+	int amount, i, aged, total = 0, ms_diff;
+	char readbuf[512], buf[45];
 	struct work *work, *tmp;
 	struct timeval tv_now;
-	double nonce_rate;
 	int64_t ret = 0;
-	char buf[45];
 
 	work = get_queue_work(thr, bitfury, thr->id);
 	if (unlikely(thr->work_restart)) {
@@ -247,7 +564,7 @@ static int64_t bitfury_scanwork(struct thr_info *thr)
 	cgtime(&tv_now);
 	ms_diff = 600 - ms_tdiff(&tv_now, &info->tv_start);
 	if (ms_diff > 0) {
-		usb_read_timeout_cancellable(bitfury, info->buf, 512, &amount, ms_diff,
+		usb_read_timeout_cancellable(bitfury, readbuf, 512, &amount, ms_diff,
 					     C_BF1_GETRES);
 		total += amount;
 	}
@@ -259,11 +576,11 @@ static int64_t bitfury_scanwork(struct thr_info *thr)
 	/* If a work restart was sent, just empty the buffer. */
 	if (unlikely(ms_diff < 10 || thr->work_restart))
 		ms_diff = 10;
-	usb_read_once_timeout_cancellable(bitfury, info->buf + total, BF1MSGSIZE,
+	usb_read_once_timeout_cancellable(bitfury, readbuf + total, BF1MSGSIZE,
 					  &amount, ms_diff, C_BF1_GETRES);
 	total += amount;
 	while (amount) {
-		usb_read_once_timeout(bitfury, info->buf + total, 512, &amount, 10,
+		usb_read_once_timeout(bitfury, readbuf + total, 512 - total, &amount, 10,
 				      C_BF1_GETRES);
 		total += amount;
 	};
@@ -288,7 +605,7 @@ out:
 		uint32_t nonce;
 
 		/* Ignore state & switched data in results for now. */
-		memcpy(&nonce, info->buf + i + 3, 4);
+		memcpy(&nonce, readbuf + i + 3, 4);
 		nonce = decnonce(nonce);
 
 		rd_lock(&bitfury->qlock);
@@ -301,37 +618,53 @@ out:
 		}
 		rd_unlock(&bitfury->qlock);
 
-		if (!found)
-			inc_hw_errors(thr);
+		if (!found) {
+			if (likely(info->valid))
+				inc_hw_errors(thr);
+		} else if (unlikely(!info->valid)) {
+			info->valid = true;
+			cgtime(&bitfury->dev_start_tv);
+		}
 	}
 
 	cgtime(&tv_now);
 
 	/* This iterates over the hashlist finding work started more than 6
-	 * seconds ago which equates to leaving 5 past work items in the array
-	 * to look for results. */
-	wr_lock(&bitfury->qlock);
-	HASH_ITER(hh, bitfury->queued_work, work, tmp) {
-		if (tdiff(&tv_now, &work->tv_work_start) > 6.0) {
-			__work_completed(bitfury, work);
-			aged++;
-		}
-	}
-	wr_unlock(&bitfury->qlock);
-
+	 * seconds ago. */
+	aged = age_queued_work(bitfury, 6.0);
 	if (aged) {
 		applog(LOG_DEBUG, "%s %d: Aged %d work items", bitfury->drv->name,
 		       bitfury->device_id, aged);
 	}
 
-	info->cycles++;
-	info->total_nonces += info->nonces;
-	info->saved_nonces += info->nonces;
-	info->nonces = 0;
-	nonce_rate = (double)info->total_nonces / (double)info->cycles;
-	if (info->saved_nonces >= nonce_rate) {
-		info->saved_nonces -= nonce_rate;
-		ret = (double)0xffffffff * nonce_rate;
+	ret = bitfury_rate(info);
+
+	if (unlikely(bitfury->usbinfo.nodev)) {
+		applog(LOG_WARNING, "%s %d: Device disappeared, disabling thread",
+		       bitfury->drv->name, bitfury->device_id);
+		ret = -1;
+	}
+	return ret;
+}
+
+static int64_t bxf_scan(struct cgpu_info *bitfury, struct bitfury_info *info)
+{
+	int64_t ret;
+	int aged;
+
+	bxf_update_work(bitfury, info);
+	cgsleep_ms(600);
+
+	mutex_lock(&info->lock);
+	ret = bitfury_rate(info);
+	mutex_unlock(&info->lock);
+
+	/* Keep no more than the last 90 seconds worth of work items in the
+	 * hashlist */
+	aged = age_queued_work(bitfury, 90.0);
+	if (aged) {
+		applog(LOG_DEBUG, "%s %d: Aged %d work items", bitfury->drv->name,
+		       bitfury->device_id, aged);
 	}
 
 	if (unlikely(bitfury->usbinfo.nodev)) {
@@ -342,9 +675,89 @@ out:
 	return ret;
 }
 
-static struct api_data *bitfury_api_stats(struct cgpu_info *cgpu)
+static int64_t bitfury_scanwork(struct thr_info *thr)
+{
+	struct cgpu_info *bitfury = thr->cgpu;
+	struct bitfury_info *info = bitfury->device_data;
+
+	switch(info->ident) {
+		case IDENT_BF1:
+			return bf1_scan(thr, bitfury, info);
+			break;
+		case IDENT_BXF:
+			return bxf_scan(bitfury, info);
+			break;
+		default:
+			return 0;
+	}
+}
+
+static void bxf_send_maxroll(struct cgpu_info *bitfury, int maxroll)
+{
+	char buf[20];
+
+	sprintf(buf, "maxroll %d\n", maxroll);
+	bxf_send_msg(bitfury, buf, C_BXF_MAXROLL);
+}
+
+static bool bxf_send_work(struct cgpu_info *bitfury, struct work *work)
+{
+	char buf[512], hexwork[156];
+
+	__bin2hex(hexwork, work->data, 76);
+	sprintf(buf, "work %s %x\n", hexwork, work->subid);
+	return bxf_send_msg(bitfury, buf, C_BXF_WORK);
+}
+
+static void bxf_update_work(struct cgpu_info *bitfury, struct bitfury_info *info)
+{
+	struct thr_info *thr = info->thr;
+	struct work *work;
+
+	work = get_queue_work(thr, bitfury, thr->id);
+	if (work->drv_rolllimit != info->maxroll) {
+		info->maxroll = work->drv_rolllimit;
+		bxf_send_maxroll(bitfury, info->maxroll);
+	}
+
+	mutex_lock(&info->lock);
+	work->subid = ++info->work_id;
+	mutex_unlock(&info->lock);
+
+	cgtime(&work->tv_work_start);
+	bxf_send_work(bitfury, work);
+}
+
+static void bitfury_flush_work(struct cgpu_info *bitfury)
+{
+	struct bitfury_info *info = bitfury->device_data;
+
+	switch(info->ident) {
+		case IDENT_BXF:
+			bxf_send_flush(bitfury);
+			bxf_update_work(bitfury, info);
+			bxf_update_work(bitfury, info);
+		case IDENT_BF1:
+		default:
+			break;
+	}
+}
+
+static void bitfury_update_work(struct cgpu_info *bitfury)
+{
+	struct bitfury_info *info = bitfury->device_data;
+
+	switch(info->ident) {
+		case IDENT_BXF:
+			bxf_update_work(bitfury, info);
+		case IDENT_BF1:
+		default:
+			break;
+	}
+}
+
+static struct api_data *bf1_api_stats(struct bitfury_info *info)
 {
-	struct bitfury_info *info = cgpu->device_data;
 	struct api_data *root = NULL;
 	double nonce_rate;
 	char serial[16];
@@ -361,18 +774,97 @@ static struct api_data *bitfury_api_stats(struct cgpu_info *cgpu)
 	return root;
 }
 
-static void bitfury_init(struct cgpu_info  *bitfury)
+static struct api_data *bxf_api_stats(struct bitfury_info *info)
 {
-	bitfury_close(bitfury);
-	bitfury_open(bitfury);
-	bitfury_reset(bitfury);
+	struct api_data *root = NULL;
+	double nonce_rate;
+	char buf[32];
+
+	sprintf(buf, "%d.%d", info->ver_major, info->ver_minor);
+	root = api_add_string(root, "Version", buf, true);
+	root = api_add_int(root, "Revision", &info->hw_rev,  false);
+	root = api_add_int(root, "Chips", &info->chips, false);
+	nonce_rate = (double)info->total_nonces / (double)info->cycles;
+	root = api_add_double(root, "NonceRate", &nonce_rate, true);
+	root = api_add_int(root, "NoMatchingWork", &info->no_matching_work, false);
+
+	return root;
+}
+
+static struct api_data *bitfury_api_stats(struct cgpu_info *cgpu)
+{
+	struct bitfury_info *info = cgpu->device_data;
+
+	switch(info->ident) {
+		case IDENT_BF1:
+			return bf1_api_stats(info);
+			break;
+		case IDENT_BXF:
+			return bxf_api_stats(info);
+			break;
+		default:
+			break;
+	}
+	return NULL;
+}
+
+static void bitfury_get_statline_before(char *buf, size_t bufsiz, struct cgpu_info *cgpu)
+{
+	struct bitfury_info *info = cgpu->device_data;
+
+	switch(info->ident) {
+		case IDENT_BXF:
+			tailsprintf(buf, bufsiz, "%03.1fC         | ", info->temperature);
+			break;
+		case IDENT_BF1:
+		default:
+			tailsprintf(buf, bufsiz, "               | ");
+			break;
+	}
+}
+
+static void bf1_init(struct cgpu_info *bitfury)
+{
+	bf1_close(bitfury);
+	bf1_open(bitfury);
+	bf1_reset(bitfury);
+}
+
+static void bitfury_init(struct cgpu_info *bitfury)
+{
+	struct bitfury_info *info = bitfury->device_data;
+
+	switch(info->ident) {
+		case IDENT_BF1:
+			bf1_init(bitfury);
+			break;
+		case IDENT_BXF:
+		default:
+			break;
+	}
+}
+
+static void bxf_close(struct bitfury_info *info)
+{
+	pthread_join(info->read_thr, NULL);
+	mutex_destroy(&info->lock);
 }
 
 static void bitfury_shutdown(struct thr_info *thr)
 {
 	struct cgpu_info *bitfury = thr->cgpu;
+	struct bitfury_info *info = bitfury->device_data;
 
-	bitfury_close(bitfury);
+	switch(info->ident) {
+		case IDENT_BF1:
+			bf1_close(bitfury);
+			break;
+		case IDENT_BXF:
+			bxf_close(info);
+			break;
+		default:
+			break;
+	}
 }
 
 /* Currently hardcoded to BF1 devices */
@@ -381,9 +873,13 @@ struct device_drv bitfury_drv = {
 	.dname = "bitfury",
 	.name = "BF1",
 	.drv_detect = bitfury_detect,
+	.thread_prepare = bitfury_prepare,
 	.hash_work = &hash_driver_work,
 	.scanwork = bitfury_scanwork,
+	.flush_work = bitfury_flush_work,
+	.update_work = bitfury_update_work,
 	.get_api_stats = bitfury_api_stats,
+	.get_statline_before = bitfury_get_statline_before,
 	.reinit_device = bitfury_init,
 	.thread_shutdown = bitfury_shutdown,
 	.identify_device = bitfury_identify
diff --git a/driver-bitfury.h b/driver-bitfury.h
index 79921b8..851d3cc 100644
--- a/driver-bitfury.h
+++ b/driver-bitfury.h
@@ -15,15 +15,31 @@
 
 struct bitfury_info {
 	struct cgpu_info *base_cgpu;
-	uint8_t version;
-	char product[8];
-	uint32_t serial;
-	char buf[512];
+	struct thr_info *thr;
+	enum sub_ident ident;
 	int nonces;
 	int total_nonces;
 	double saved_nonces;
 	int cycles;
+	bool valid; /* Set on first valid data being found */
+
+	/* BF1 specific data */
+	uint8_t version;
+	char product[8];
+	uint32_t serial;
 	struct timeval tv_start;
+
+	/* BXF specific data */
+	pthread_mutex_t lock;
+	pthread_t read_thr;
+	double temperature;
+	int work_id; // Current work->subid
+	int no_matching_work;
+	int maxroll; // Last maxroll sent to device
+	int ver_major;
+	int ver_minor;
+	int hw_rev;
+	int chips;
 };
 
 #endif /* BITFURY_H */
diff --git a/driver-hashfast.c b/driver-hashfast.c
index 281d2ea..4f4a661 100644
--- a/driver-hashfast.c
+++ b/driver-hashfast.c
@@ -147,20 +147,26 @@ static bool hfa_send_packet(struct cgpu_info *hashfast, struct hf_header *h, int
 
 static bool hfa_get_header(struct cgpu_info *hashfast, struct hf_header *h, uint8_t *computed_crc)
 {
-	int amount, ret, orig_len, len, ofs = 0, reads = 0;
+	int amount, ret, orig_len, len, ofs = 0;
+	cgtimer_t ts_start;
 	char buf[512];
 	char *header;
 
+	orig_len = len = sizeof(*h);
+
 	/* Read for up to 200ms till we find the first occurrence of HF_PREAMBLE
 	 * though it should be the first byte unless we get woefully out of
 	 * sync. */
-	orig_len = len = sizeof(*h);
+	cgtimer_time(&ts_start);
 	do {
+		cgtimer_t ts_now, ts_diff;
 
-		if (++reads > 20)
+		cgtimer_time(&ts_now);
+		cgtimer_sub(&ts_now, &ts_start, &ts_diff);
+		if (cgtimer_to_ms(&ts_diff) > 200)
 			return false;
 
-		ret = usb_read_timeout(hashfast, buf + ofs, len, &amount, 10, C_HF_GETHEADER);
+		ret = usb_read(hashfast, buf + ofs, len, &amount, C_HF_GETHEADER);
 		if (unlikely(ret && ret != LIBUSB_ERROR_TIMEOUT))
 			return false;
 		ofs += amount;
@@ -206,11 +212,11 @@ static bool hfa_reset(struct cgpu_info *hashfast, struct hashfast_info *info)
 	struct hf_usb_init_header usb_init, *hu = &usb_init;
 	struct hf_usb_init_base *db;
         struct hf_usb_init_options *ho;
+	int retries = 0, i;
 	char buf[1024];
 	struct hf_header *h = (struct hf_header *)buf;
 	uint8_t hcrc;
 	bool ret;
-	int i;
 
         // XXX Following items need to be defaults with command-line overrides
 	info->hash_clock_rate = 550;	// Hash clock rate in Mhz
@@ -260,7 +266,9 @@ tryagain:
 		applog(LOG_WARNING, "HFA %d: OP_USB_INIT: Tossing packet, valid but unexpected type %d",
                         hashfast->device_id, h->operation_code);
 		hfa_get_data(hashfast, buf, h->data_length);
-		goto tryagain;
+		if (retries++ < 3)
+			goto tryagain;
+		return false;
 	}
 
 	applog(LOG_DEBUG, "HFA %d: Good reply to OP_USB_INIT", hashfast->device_id);
@@ -409,7 +417,7 @@ static bool hfa_initialise(struct cgpu_info *hashfast)
 	return (err == 7);
 }
 
-static bool hfa_detect_one_usb(libusb_device *dev, struct usb_find_devices *found)
+static struct cgpu_info *hfa_detect_one(libusb_device *dev, struct usb_find_devices *found)
 {
 	struct cgpu_info *hashfast;
 
@@ -419,19 +427,25 @@ static bool hfa_detect_one_usb(libusb_device *dev, struct usb_find_devices *foun
 
 	if (!usb_init(hashfast, dev, found)) {
 		hashfast = usb_free_cgpu(hashfast);
-		return false;
+		return NULL;
 	}
 
 	hashfast->usbdev->usb_type = USB_TYPE_STD;
 
 	if (!hfa_initialise(hashfast)) {
 		hashfast = usb_free_cgpu(hashfast);
-		return false;
+		return NULL;
 	}
 
-	add_cgpu(hashfast);
+	if (!hfa_detect_common(hashfast)) {
+		usb_uninit(hashfast);
+		hashfast = usb_free_cgpu(hashfast);
+		return NULL;
+	}
+	if (!add_cgpu(hashfast))
+		return NULL;
 
-	return hfa_detect_common(hashfast);
+	return hashfast;
 }
 
 static void hfa_detect(bool hotplug)
@@ -439,7 +453,7 @@ static void hfa_detect(bool hotplug)
 	/* Set up the CRC tables only once. */
 	if (!hotplug)
 		hfa_init_crc8();
-	usb_detect(&hashfast_drv, hfa_detect_one_usb);
+	usb_detect(&hashfast_drv, hfa_detect_one);
 }
 
 static bool hfa_get_packet(struct cgpu_info *hashfast, struct hf_header *h)
diff --git a/driver-icarus.c b/driver-icarus.c
index 7da4a99..af35124 100644
--- a/driver-icarus.c
+++ b/driver-icarus.c
@@ -786,7 +786,7 @@ static void get_options(int this_option_offset, struct cgpu_info *icarus, int *b
 	}
 }
 
-static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
+static struct cgpu_info *icarus_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
 {
 	int this_option_offset = ++option_offset;
 	struct ICARUS_INFO *info;
@@ -1010,7 +1010,7 @@ cmr2_retry:
 		}
 	}
 
-	return true;
+	return icarus;
 
 unshin:
 
@@ -1022,7 +1022,7 @@ shin:
 
 	icarus = usb_free_cgpu(icarus);
 
-	return false;
+	return NULL;
 }
 
 static void icarus_detect(bool __maybe_unused hotplug)
diff --git a/driver-klondike.c b/driver-klondike.c
index 2446202..5da915c 100644
--- a/driver-klondike.c
+++ b/driver-klondike.c
@@ -765,7 +765,7 @@ static void control_init(struct cgpu_info *klncgpu)
 			  klncgpu->drv->name, klncgpu->device_id, err);
 }
 
-static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
+static struct cgpu_info *klondike_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
 {
 	struct cgpu_info *klncgpu = usb_alloc_cgpu(&klondike_drv, 1);
 	struct klondike_info *klninfo = NULL;
@@ -823,7 +823,7 @@ static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devic
 				applog(LOG_DEBUG, "Klondike cgpu added");
 				rwlock_init(&klninfo->stat_lock);
 				cglock_init(&klninfo->klist_lock);
-				return true;
+				return klncgpu;
 			}
 		}
 		usb_uninit(klncgpu);
@@ -831,7 +831,7 @@ static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devic
 	free(klninfo->free);
 	free(klninfo);
 	free(klncgpu);
-	return false;
+	return NULL;
 }
 
 static void klondike_detect(bool __maybe_unused hotplug)
@@ -1129,26 +1129,28 @@ static void klondike_flush_work(struct cgpu_info *klncgpu)
 	KLINE kline;
 	int slaves, dev;
 
-	wr_lock(&(klninfo->stat_lock));
-	klninfo->block_seq++;
-	slaves = klninfo->status[0].kline.ws.slavecount;
-	wr_unlock(&(klninfo->stat_lock));
+	if (klninfo->initialised) {
+		wr_lock(&(klninfo->stat_lock));
+		klninfo->block_seq++;
+		slaves = klninfo->status[0].kline.ws.slavecount;
+		wr_unlock(&(klninfo->stat_lock));
 
-	applog(LOG_DEBUG, "%s%i: flushing work",
-			  klncgpu->drv->name, klncgpu->device_id);
-	zero_kline(&kline);
-	kline.hd.cmd = KLN_CMD_ABORT;
-	for (dev = 0; dev <= slaves; dev++) {
-		kline.hd.dev = dev;
-		kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
-		if (kitem != NULL) {
-			wr_lock(&(klninfo->stat_lock));
-			memcpy((void *)&(klninfo->status[dev]),
-				kitem,
-				sizeof(klninfo->status[dev]));
-			klninfo->jobque[dev].flushed = true;
-			wr_unlock(&(klninfo->stat_lock));
-			kitem = release_kitem(klncgpu, kitem);
+		applog(LOG_DEBUG, "%s%i: flushing work",
+				  klncgpu->drv->name, klncgpu->device_id);
+		zero_kline(&kline);
+		kline.hd.cmd = KLN_CMD_ABORT;
+		for (dev = 0; dev <= slaves; dev++) {
+			kline.hd.dev = dev;
+			kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
+			if (kitem != NULL) {
+				wr_lock(&(klninfo->stat_lock));
+				memcpy((void *)&(klninfo->status[dev]),
+					kitem,
+					sizeof(klninfo->status[dev]));
+				klninfo->jobque[dev].flushed = true;
+				wr_unlock(&(klninfo->stat_lock));
+				kitem = release_kitem(klncgpu, kitem);
+			}
 		}
 	}
 }
diff --git a/driver-modminer.c b/driver-modminer.c
index e10cbba..642d13d 100644
--- a/driver-modminer.c
+++ b/driver-modminer.c
@@ -109,7 +109,7 @@ static void do_ping(struct cgpu_info *modminer)
 		modminer->drv->name, modminer->fpgaid, amount, err);
 }
 
-static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
+static struct cgpu_info *modminer_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
 {
 	char buf[0x100+1];
 	char *devname = NULL;
@@ -217,7 +217,7 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic
 
 	modminer = usb_free_cgpu(modminer);
 
-	return true;
+	return modminer;
 
 unshin:
 	if (!added)
@@ -232,9 +232,9 @@ shin:
 	modminer = usb_free_cgpu(modminer);
 
 	if (added)
-		return true;
+		return modminer;
 	else
-		return false;
+		return NULL;
 }
 
 static void modminer_detect(bool __maybe_unused hotplug)
diff --git a/example.conf b/example.conf
index ae3366e..2448ed5 100644
--- a/example.conf
+++ b/example.conf
@@ -17,24 +17,9 @@
 	}
 ],
 
-"intensity" : "d,9,9,9",
-"gpu-engine" : "0-985,0-950,0-960,0-1000",
-"gpu-fan" : "0-85,0-85,0-85,0-85",
-"gpu-memclock" : "860,825,835,875",
-"gpu-powertune" : "20,20,20,20",
-"temp-cutoff" : "95,95,95,95",
-"temp-overheat" : "85,85,85,85",
-"temp-target" : "75,75,75,75",
-
-"auto-fan" : true,
-"auto-gpu" : true,
-"expiry" : "120",
 "failover-only" : true,
-"gpu-threads" : "2",
-"log" : "5",
-"queue" : "1",
-"scan-time" : "60",
-"temp-hysteresis" : "3",
-
-"kernel-path" : "/usr/local/bin"
+"no-submit-stale" : true,
+"api-listen" : true,
+"api-port" : "4028",
+"api-allow" : "W:192.168.1.0/24,W:127.0.0.1"
 }
diff --git a/hf_protocol.h b/hf_protocol.h
index 387c284..28cc13b 100644
--- a/hf_protocol.h
+++ b/hf_protocol.h
@@ -98,7 +98,7 @@
 
 // Structure definitions, LE platforms
 
-#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+#if __BYTE_ORDER == __BIG_ENDIAN
 #include "hf_protocol_be.h"
 #else
 // Generic header
diff --git a/miner.h b/miner.h
index ef05dad..685a4fb 100644
--- a/miner.h
+++ b/miner.h
@@ -1444,6 +1444,7 @@ extern struct work *__find_work_bymidstate(struct work *que, char *midstate, siz
 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 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);
 extern struct work *take_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
 extern void hash_driver_work(struct thr_info *mythr);
@@ -1475,6 +1476,7 @@ extern void adl(void);
 extern void app_restart(void);
 extern void clean_work(struct work *work);
 extern void free_work(struct work *work);
+extern void set_work_ntime(struct work *work, int ntime);
 extern struct work *copy_work_noffset(struct work *base_work, int noffset);
 #define copy_work(work_in) copy_work_noffset(work_in, 0)
 extern struct thr_info *get_thread(int thr_id);
diff --git a/miner.php b/miner.php
index fd42271..ff2e457 100644
--- a/miner.php
+++ b/miner.php
@@ -2910,7 +2910,7 @@ function display()
 
  if ($allowcustompages === true)
  {
-	$pg = trim(getparam('pg', true));
+	$pg = urlencode(trim(getparam('pg', true)));
 	if ($pagesonly === true)
 	{
 		if ($pg !== null && $pg !== '')
diff --git a/usbutils.c b/usbutils.c
index 3b62545..b3459d2 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -139,6 +139,20 @@ static struct usb_intinfo bfu_ints[] = {
 	USB_EPS(1,  bfu1_epinfos),
 	USB_EPS(0,  bfu0_epinfos)
 };
+
+static struct usb_epinfo bxf0_epinfos[] = {
+	{ LIBUSB_TRANSFER_TYPE_INTERRUPT,	8,	EPI(1), 0, 0 }
+};
+
+static struct usb_epinfo bxf1_epinfos[] = {
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(2), 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(2), 0, 0 }
+};
+
+static struct usb_intinfo bxf_ints[] = {
+	USB_EPS(1,  bxf1_epinfos),
+	USB_EPS(0,  bxf0_epinfos)
+};
 #endif
 
 #ifdef USE_HASHFAST
@@ -191,6 +205,20 @@ static struct usb_epinfo kln_epinfos[] = {
 static struct usb_intinfo kln_ints[] = {
 	USB_EPS(0, kln_epinfos)
 };
+
+static struct usb_epinfo kli0_epinfos[] = {
+	{ LIBUSB_TRANSFER_TYPE_INTERRUPT, 8,	EPI(1), 0, 0 }
+};
+
+static struct usb_epinfo kli1_epinfos[] = {
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(2), 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(2), 0, 0 }
+};
+
+static struct usb_intinfo kli_ints[] = {
+	USB_EPS(1, kli1_epinfos),
+	USB_EPS(0, kli0_epinfos)
+};
 #endif
 
 #ifdef USE_ICARUS
@@ -299,7 +327,7 @@ static struct usb_find_devices find_dev[] = {
 	{
 		.drv = DRIVER_bitfury,
 		.name = "BF1",
-		.ident = IDENT_BFU,
+		.ident = IDENT_BF1,
 		.idVendor = 0x03eb,
 		.idProduct = 0x204b,
 		.config = 1,
@@ -309,6 +337,19 @@ static struct usb_find_devices find_dev[] = {
 		.iProduct = "Bitfury BF1",
 		INTINFO(bfu_ints)
 	},
+	{
+		.drv = DRIVER_bitfury,
+		.name = "BXF",
+		.ident = IDENT_BXF,
+		.idVendor = 0x198c,
+		.idProduct = 0xb1f1,
+		.config = 1,
+		.timeout = BITFURY_TIMEOUT_MS,
+		.latency = LATENCY_UNUSED,
+		.iManufacturer = "c-scape",
+		.iProduct = "bi?fury",
+		INTINFO(bxf_ints)
+	},
 #endif
 #ifdef USE_MODMINER
 	{
@@ -383,6 +424,16 @@ static struct usb_find_devices find_dev[] = {
 		.timeout = KLONDIKE_TIMEOUT_MS,
 		.latency = 10,
 		INTINFO(kln_ints) },
+	{
+		.drv = DRIVER_klondike,
+		.name = "KLI",
+		.ident = IDENT_KLN,
+		.idVendor = 0x04D8,
+		.idProduct = 0xF60A,
+		.config = 1,
+		.timeout = KLONDIKE_TIMEOUT_MS,
+		.latency = 10,
+		INTINFO(kli_ints) },
 #endif
 #ifdef USE_ICARUS
 	{
@@ -488,8 +539,8 @@ static const char *nodatareturned = "no data returned ";
 
 // For device limits by driver
 static struct driver_count {
-	uint32_t count;
-	uint32_t limit;
+	int count;
+	int limit;
 } drv_count[DRIVER_MAX];
 
 // For device limits by list of bus/dev
@@ -548,7 +599,7 @@ struct resource_reply *res_reply_head = NULL;
 #define MODE_BULK_WRITE (1 << 3)
 
 // Set this to 0 to remove stats processing
-#define DO_USB_STATS 1
+#define DO_USB_STATS 0
 
 static bool stats_initialised = false;
 
@@ -1359,6 +1410,7 @@ void usb_uninit(struct cgpu_info *cgpu)
 static void release_cgpu(struct cgpu_info *cgpu)
 {
 	struct cg_usb_device *cgusb = cgpu->usbdev;
+	bool initted = cgpu->usbinfo.initialised;
 	struct cgpu_info *lookcgpu;
 	int i;
 
@@ -1369,9 +1421,11 @@ static void release_cgpu(struct cgpu_info *cgpu)
 	applog(LOG_DEBUG, "USB release %s%i",
 			cgpu->drv->name, cgpu->device_id);
 
-	zombie_devs++;
-	total_count--;
-	drv_count[cgpu->drv->drv_id].count--;
+	if (initted) {
+		zombie_devs++;
+		total_count--;
+		drv_count[cgpu->drv->drv_id].count--;
+	}
 
 	cgpu->usbinfo.nodev = true;
 	cgpu->usbinfo.nodev_count++;
@@ -1381,8 +1435,10 @@ static void release_cgpu(struct cgpu_info *cgpu)
 	for (i = 0; i < total_devices; i++) {
 		lookcgpu = get_devices(i);
 		if (lookcgpu != cgpu && lookcgpu->usbdev == cgusb) {
-			total_count--;
-			drv_count[lookcgpu->drv->drv_id].count--;
+			if (initted) {
+				total_count--;
+				drv_count[lookcgpu->drv->drv_id].count--;
+			}
 
 			lookcgpu->usbinfo.nodev = true;
 			lookcgpu->usbinfo.nodev_count++;
@@ -1539,6 +1595,7 @@ static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct u
 					"USB init, open device failed, err %d, "
 					"you don't have privilege to access %s",
 					err, devstr);
+				applog(LOG_ERR, "See README file included for help");
 				break;
 #ifdef WIN32
 			// Windows specific message
@@ -1547,6 +1604,7 @@ static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct u
 					"USB init, open device failed, err %d, "
 					"you need to install a WinUSB driver for %s",
 					err, devstr);
+				applog(LOG_ERR, "See README.txt file included for help");
 				break;
 #endif
 			default:
@@ -1925,11 +1983,12 @@ static struct usb_find_devices *usb_check(__maybe_unused struct device_drv *drv,
 	return NULL;
 }
 
-void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *))
+void usb_detect(struct device_drv *drv, struct cgpu_info *(*device_detect)(struct libusb_device *, struct usb_find_devices *))
 {
 	libusb_device **list;
 	ssize_t count, i;
 	struct usb_find_devices *found;
+	struct cgpu_info *cgpu;
 
 	applog(LOG_DEBUG, "USB scan devices: checking for %s devices", drv->name);
 
@@ -1974,9 +2033,11 @@ void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_devi
 			if (is_in_use(list[i]) || cgminer_usb_lock(drv, list[i]) == false)
 				free(found);
 			else {
-				if (!device_detect(list[i], found))
+				cgpu = device_detect(list[i], found);
+				if (!cgpu)
 					cgminer_usb_unlock(drv, list[i]);
 				else {
+					cgpu->usbinfo.initialised = true;
 					total_count++;
 					drv_count[drv->drv_id].count++;
 				}
@@ -2125,7 +2186,7 @@ static void newstats(struct cgpu_info *cgpu)
 
 	usb_stats[next_stat].name = cgpu->drv->name;
 	usb_stats[next_stat].device_id = -1;
-	usb_stats[next_stat].details = calloc(1, sizeof(struct cg_usb_stats_details) * C_MAX * 2);
+	usb_stats[next_stat].details = calloc(1, sizeof(struct cg_usb_stats_details) * C_MAX * 2 + 8);
 	if (unlikely(!usb_stats[next_stat].details))
 		quit(1, "USB failed to calloc details for %d", next_stat+1);
 
@@ -2234,37 +2295,6 @@ static void rejected_inc(struct cgpu_info *cgpu, uint32_t mode)
 }
 #endif
 
-static char *find_end(unsigned char *buf, unsigned char *ptr, int ptrlen, int tot, char *end, int endlen, bool first)
-{
-	unsigned char *search;
-
-	if (endlen > tot)
-		return NULL;
-
-	// If end is only 1 char - do a faster search
-	if (endlen == 1) {
-		if (first)
-			search = buf;
-		else
-			search = ptr;
-
-		return strchr((char *)search, *end);
-	} else {
-		if (first)
-			search = buf;
-		else {
-			// must allow end to have been chopped in 2
-			if ((tot - ptrlen) >= (endlen - 1))
-				search = ptr - (endlen - 1);
-			else
-				search = ptr - (tot - ptrlen);
-		}
-
-		return strstr((char *)search, end);
-	}
-}
-
-#define USB_MAX_READ 8192
 #define USB_RETRY_MAX 5
 
 struct usb_transfer {
@@ -2419,15 +2449,11 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, int intinfo,
 	struct usb_transfer ut;
 	unsigned char endpoint;
 	int err, errn;
-	/* End of transfer and zero length packet required */
-	bool eot = false, zlp = false;
 #if DO_USB_STATS
 	struct timeval tv_start, tv_finish;
 #endif
 	unsigned char buf[512];
 #ifdef WIN32
-	int dummy;
-
 	/* On windows the callback_timeout is a safety mechanism only. */
 	bulk_timeout = timeout;
 	callback_timeout += WIN_CALLBACK_EXTRA;
@@ -2440,11 +2466,6 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, int intinfo,
 	usb_epinfo = &(cgpu->usbdev->found->intinfos[intinfo].epinfos[epinfo]);
 	endpoint = usb_epinfo->ep;
 
-	if (length > usb_epinfo->wMaxPacketSize)
-		length = usb_epinfo->wMaxPacketSize;
-	else if (length == usb_epinfo->wMaxPacketSize)
-		eot = true;
-
 	/* Avoid any async transfers during shutdown to allow the polling
 	 * thread to be shut down after all existing transfers are complete */
 	if (unlikely(cgpu->shutdown))
@@ -2454,15 +2475,7 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, int intinfo,
 
 	if ((endpoint & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT) {
 		memcpy(buf, data, length);
-		/* If this is the last packet in a transfer and is the length
-		 * of the wMaxPacketSize then we need to send a zero length
-		 * packet to let the device know it's the end of the message.*/
-		if (eot)
-			zlp = true;
-#ifndef WIN32
-		/* Windows doesn't support this flag so we emulate it below */
 		ut.transfer->flags |= LIBUSB_TRANSFER_ADD_ZERO_PACKET;
-#endif
 	}
 
 	USBDEBUG("USB debug: @usb_bulk_transfer(%s (nodev=%s),intinfo=%d,epinfo=%d,data=%p,length=%d,timeout=%u,mode=%d,cmd=%s,seq=%d) endpoint=%d", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), intinfo, epinfo, data, length, timeout, mode, usb_cmdname(cmd), seq, (int)endpoint);
@@ -2505,35 +2518,33 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, int intinfo,
 	}
 	if ((endpoint & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN)
 		memcpy(data, buf, *transferred);
-	else if (zlp) {
-#ifdef WIN32
-		/* Send a zero length packet */
-		libusb_bulk_transfer(dev_handle, endpoint, NULL, 0, &dummy, 100);
-#endif
-	}
 
 	return err;
 }
 
 int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz, int *processed, int timeout, const char *end, enum usb_cmds cmd, bool readonce, bool cancellable)
 {
-	struct cg_usb_device *usbdev;
-	bool ftdi;
+	unsigned char *ptr, usbbuf[USB_READ_BUFSIZE];
 	struct timeval read_start, tv_finish;
-	unsigned int initial_timeout;
 	int bufleft, err, got, tot, pstate;
-	bool first = true;
-	bool dobuffer;
-	char *search;
-	int endlen;
-	unsigned char *ptr, *usbbuf = cgpu->usbinfo.bulkbuf;
 	const size_t usbbufread = 512; /* Always read full size */
+	struct cg_usb_device *usbdev;
+	unsigned int initial_timeout;
+	bool first = true;
+	int endlen = 0;
+	char *eom = NULL;
 	double done;
+	bool ftdi;
+
+	memset(usbbuf, 0, USB_READ_BUFSIZE);
+	memset(buf, 0, bufsiz);
+
+	if (end)
+		endlen = strlen(end);
 
 	DEVRLOCK(cgpu, pstate);
 
 	if (cgpu->usbinfo.nodev) {
-		*buf = '\0';
 		*processed = 0;
 		USB_REJECT(cgpu, MODE_BULK_READ);
 
@@ -2552,75 +2563,6 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
 	if (timeout == DEVTIMEOUT)
 		timeout = usbdev->found->timeout;
 
-	if (end == NULL) {
-		tot = usbdev->bufamt;
-		bufleft = bufsiz - tot;
-		if (tot)
-			memcpy(usbbuf, usbdev->buffer, tot);
-		ptr = usbbuf + tot;
-		usbdev->bufamt = 0;
-
-		err = LIBUSB_SUCCESS;
-		initial_timeout = timeout;
-		cgtime(&read_start);
-		while (bufleft > 0) {
-			got = 0;
-
-			err = usb_bulk_transfer(usbdev->handle, intinfo, epinfo,
-						ptr, usbbufread, &got, timeout,
-						cgpu, MODE_BULK_READ, cmd, first ? SEQ0 : SEQ1,
-						cancellable);
-			cgtime(&tv_finish);
-			ptr[got] = '\0';
-
-			USBDEBUG("USB debug: @_usb_read(%s (nodev=%s)) first=%s err=%d%s got=%d ptr='%s' usbbufread=%d", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), bool_str(first), err, isnodev(err), got, (char *)str_text((char *)ptr), (int)usbbufread);
-
-			IOERR_CHECK(cgpu, err);
-
-			if (ftdi) {
-				// first 2 bytes returned are an FTDI status
-				if (got > 2) {
-					got -= 2;
-					memmove(ptr, ptr+2, got+1);
-				} else {
-					got = 0;
-					*ptr = '\0';
-				}
-			}
-
-			tot += got;
-
-			if (err || readonce)
-				break;
-
-			ptr += got;
-			bufleft -= got;
-
-			first = false;
-
-			done = tdiff(&tv_finish, &read_start);
-			// N.B. this is: return last err with whatever size has already been read
-			timeout = initial_timeout - (done * 1000);
-			if (timeout <= 0)
-				break;
-		}
-
-		// N.B. usbdev->buffer was emptied before the while() loop
-		if (tot > (int)bufsiz) {
-			usbdev->bufamt = tot - bufsiz;
-			memcpy(usbdev->buffer, usbbuf + bufsiz, usbdev->bufamt);
-			tot -= usbdev->bufamt;
-			usbbuf[tot] = '\0';
-			applog(LOG_DEBUG, "USB: %s%i read1 buffering %d extra bytes",
-			       cgpu->drv->name, cgpu->device_id, usbdev->bufamt);
-		}
-
-		*processed = tot;
-		memcpy((char *)buf, (const char *)usbbuf, (tot < (int)bufsiz) ? tot + 1 : (int)bufsiz);
-
-		goto out_unlock;
-	}
-
 	tot = usbdev->bufamt;
 	bufleft = bufsiz - tot;
 	if (tot)
@@ -2628,13 +2570,13 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
 	ptr = usbbuf + tot;
 	usbdev->bufamt = 0;
 
-	endlen = strlen(end);
 	err = LIBUSB_SUCCESS;
+	if (end != NULL)
+		eom = strstr((const char *)usbbuf, end);
+
 	initial_timeout = timeout;
 	cgtime(&read_start);
-
-	while (bufleft > 0) {
-		got = 0;
+	while (bufleft > 0 && !eom) {
 		err = usb_bulk_transfer(usbdev->handle, intinfo, epinfo,
 					ptr, usbbufread, &got, timeout,
 					cgpu, MODE_BULK_READ, cmd, first ? SEQ0 : SEQ1,
@@ -2658,59 +2600,48 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
 		}
 
 		tot += got;
+		if (end != NULL)
+			eom = strstr((const char *)usbbuf, end);
 
 		if (err || readonce)
 			break;
 
-		if (find_end(usbbuf, ptr, got, tot, (char *)end, endlen, first))
-			break;
-
 		ptr += got;
 		bufleft -= got;
 
 		first = false;
 
 		done = tdiff(&tv_finish, &read_start);
-		// N.B. this is: return LIBUSB_SUCCESS with whatever size has already been read
+		// N.B. this is: return last err with whatever size has already been read
 		timeout = initial_timeout - (done * 1000);
 		if (timeout <= 0)
 			break;
 	}
 
-	dobuffer = false;
+	/* If we found the end of message marker, just use that data and
+	 * return success. */
+	if (eom) {
+		size_t eomlen = (void *)eom - (void *)usbbuf + endlen;
 
-	if ((search = find_end(usbbuf, usbbuf, tot, tot, (char *)end, endlen, true))) {
-		// end finishes after bufsiz
-		if ((search + endlen - (char *)usbbuf) > (int)bufsiz) {
-			usbdev->bufamt = tot - bufsiz;
-			dobuffer = true;
-		} else {
-			// extra data after end
-			if (*(search + endlen)) {
-				usbdev->bufamt = tot - (search + endlen - (char *)usbbuf);
-				dobuffer = true;
-			}
-		}
-	} else {
-		// no end, but still bigger than bufsiz
-		if (tot > (int)bufsiz) {
-			usbdev->bufamt = tot - bufsiz;
-			dobuffer = true;
+		if (eomlen < bufsiz) {
+			bufsiz = eomlen;
+			err = LIBUSB_SUCCESS;
 		}
 	}
 
-	if (dobuffer) {
+	// N.B. usbdev->buffer was emptied before the while() loop
+	if (tot > (int)bufsiz) {
+		usbdev->bufamt = tot - bufsiz;
+		memcpy(usbdev->buffer, usbbuf + bufsiz, usbdev->bufamt);
 		tot -= usbdev->bufamt;
-		memcpy(usbdev->buffer, usbbuf + tot, usbdev->bufamt);
 		usbbuf[tot] = '\0';
-		applog(LOG_DEBUG, "USB: %s%i read2 buffering %d extra bytes",
+		applog(LOG_DEBUG, "USB: %s%i read1 buffering %d extra bytes",
 			cgpu->drv->name, cgpu->device_id, usbdev->bufamt);
 	}
 
 	*processed = tot;
 	memcpy((char *)buf, (const char *)usbbuf, (tot < (int)bufsiz) ? tot + 1 : (int)bufsiz);
 
-out_unlock:
 	if (err && err != LIBUSB_ERROR_TIMEOUT) {
 		applog(LOG_WARNING, "%s %i %s usb read err:(%d) %s", cgpu->drv->name, cgpu->device_id, usb_cmdname(cmd),
 		       err, libusb_error_name(err));
diff --git a/usbutils.h b/usbutils.h
index 9a22247..035f83e 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -134,24 +134,25 @@ struct usb_intinfo {
 
 enum sub_ident {
 	IDENT_UNK = 0,
+	IDENT_AMU,
+	IDENT_AVA,
 	IDENT_BAJ,
 	IDENT_BAL,
-	IDENT_BAS,
 	IDENT_BAM,
+	IDENT_BAS,
+	IDENT_BBF,
+	IDENT_BF1,
 	IDENT_BFL,
-	IDENT_BFU,
-	IDENT_MMQ,
-	IDENT_AVA,
+	IDENT_BLT,
 	IDENT_BTB,
+	IDENT_BXF,
+	IDENT_CMR1,
+	IDENT_CMR2,
 	IDENT_HFA,
-	IDENT_BBF,
-	IDENT_KLN,
 	IDENT_ICA,
-	IDENT_AMU,
-	IDENT_BLT,
+	IDENT_KLN,
 	IDENT_LLT,
-	IDENT_CMR1,
-	IDENT_CMR2,
+	IDENT_MMQ,
 	IDENT_ZTX
 };
 
@@ -182,6 +183,12 @@ enum usb_types {
 };
 
 #define USB_MAX_READ 8192
+/*
+ * We add 4: 1 for null, 2 for FTDI status and 1 to round to 4 bytes
+ * If a single device ever has multiple end points then it will need
+ * multiple of these
+ */
+#define USB_READ_BUFSIZE (USB_MAX_READ + 4)
 
 struct cg_usb_device {
 	struct usb_find_devices *found;
@@ -223,6 +230,7 @@ struct cg_usb_info {
 	uint8_t device_address;
 	int usbstat;
 	bool nodev;
+	bool initialised;
 	int nodev_count;
 	struct timeval last_nodev;
 	uint32_t ioerr_count;
@@ -250,13 +258,6 @@ struct cg_usb_info {
 	uint64_t write_delay_count;
 	double total_write_delay;
 
-	/*
-	 * We add 4: 1 for null, 2 for FTDI status and 1 to round to 4 bytes
-	 * If a single device ever has multiple end points then it will need
-	 * multiple of these
-	 */
-	unsigned char bulkbuf[USB_MAX_READ+4];
-
 	uint64_t tmo_count;
 	struct cg_usb_tmo usb_tmo[USB_TMOS];
 };
@@ -342,6 +343,12 @@ struct cg_usb_info {
 	USB_ADD_COMMAND(C_BF1_FLUSH, "BF1Flush") \
 	USB_ADD_COMMAND(C_BF1_IFLUSH, "BF1InterruptFlush") \
 	USB_ADD_COMMAND(C_BF1_IDENTIFY, "BF1Identify") \
+	USB_ADD_COMMAND(C_BXF_READ, "BXFRead") \
+	USB_ADD_COMMAND(C_BXF_WORK, "BXFWork") \
+	USB_ADD_COMMAND(C_BXF_TARGET, "BXFTarget") \
+	USB_ADD_COMMAND(C_BXF_VERSION, "BXFVersion") \
+	USB_ADD_COMMAND(C_BXF_MAXROLL, "BXFMaxRoll") \
+	USB_ADD_COMMAND(C_BXF_FLUSH, "BXFFlush") \
 	USB_ADD_COMMAND(C_HF_RESET, "HFReset") \
 	USB_ADD_COMMAND(C_HF_PLL_CONFIG, "HFPLLConfig") \
 	USB_ADD_COMMAND(C_HF_ADDRESS, "HFAddress") \
@@ -382,7 +389,7 @@ struct cgpu_info *usb_alloc_cgpu(struct device_drv *drv, int threads);
 struct cgpu_info *usb_free_cgpu(struct cgpu_info *cgpu);
 void usb_uninit(struct cgpu_info *cgpu);
 bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found);
-void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *));
+void usb_detect(struct device_drv *drv, struct cgpu_info *(*device_detect)(struct libusb_device *, struct usb_find_devices *));
 struct api_data *api_usb_stats(int *count);
 void update_usb_stats(struct cgpu_info *cgpu);
 int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz, int *processed, int timeout, const char *end, enum usb_cmds cmd, bool readonce, bool cancellable);
diff --git a/util.c b/util.c
index 4e3d502..0e9670c 100644
--- a/util.c
+++ b/util.c
@@ -1839,7 +1839,7 @@ bool auth_stratum(struct pool *pool)
 			ss = json_dumps(err_val, JSON_INDENT(3));
 		else
 			ss = strdup("(unknown reason)");
-		applog(LOG_WARNING, "pool %d JSON stratum auth failed: %s", pool->pool_no, ss);
+		applog(LOG_INFO, "pool %d JSON stratum auth failed: %s", pool->pool_no, ss);
 		free(ss);
 
 		goto out;