Commit 9b5c534911a52dc64b2dd58014470650efbea7cd

Con Kolivas 2013-10-13T16:41:53

Merge branch 'master' into hashfast Conflicts: api.c configure.ac miner.h usbutils.c usbutils.h

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
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
diff --git a/01-cgminer.rules b/01-cgminer.rules
index 24211b4..89443ad 100644
--- a/01-cgminer.rules
+++ b/01-cgminer.rules
@@ -16,8 +16,14 @@ ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", SUBSYSTEMS=="usb", ACTION=="a
 # Cairnsmore1
 ATTRS{idVendor}=="067b", ATTRS{idProduct}=="0230", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
 
+# Cairnsmore1-2
+ATTRS{idVendor}=="0403", ATTRS{idProduct}=="8350", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
+
 # Ztex
 ATTRS{idVendor}=="221a", ATTRS{idProduct}=="0100", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
 
 # BF1
 ATTRS{idVendor}=="03eb", ATTRS{idProduct}=="204b", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
+
+#Klondike
+ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="f60a", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
diff --git a/API-README b/API-README
index 0898da0..bf6e520 100644
--- a/API-README
+++ b/API-README
@@ -277,13 +277,13 @@ The list of requests - a (*) means it requires privileged access - and replies:
                none           There is no reply section just the STATUS section
                               stating the results of the identify request
                               This is only available if PGA mining is enabled
-                              and currently only BFL singles support this
-                              command
+                              and currently only BFL singles and Cairnsmore1's
+                              with the appropriate firmware support this command
                               On a BFL single it will flash the led on the front
                               of the device for appoximately 4s
-                              All other non BFL PGA devices will return a
+                              All other non BFL,ICA PGA devices will return a
                               warning status message stating that they dont
-                              support it
+                              support it. Non-CMR ICAs will ignore the command.
                               This adds a 4s delay to the BFL share being
                               processed so you may get a message stating that
                               procssing took longer than 7000ms if the request
@@ -364,6 +364,7 @@ The list of requests - a (*) means it requires privileged access - and replies:
 
                               The current options are:
                                MMQ opt=clock val=160 to 230 (a multiple of 2)
+                               CMR opt=clock val=100 to 220
 
  zero|Which,true/false (*)
                none           There is no reply section just the STATUS section
@@ -433,8 +434,8 @@ The list of requests - a (*) means it requires privileged access - and replies:
                               help message about the options available
 
                               The current options are:
-                               AVA+BTB opt=freq val=256 to 450 - chip frequency
-                               BTB opt=millivolts val=1000 to 1310 - corevoltage
+                               AVA+BTB opt=freq val=256 to 1024 - chip frequency
+                               BTB opt=millivolts val=1000 to 1400 - corevoltage
 
 When you enable, disable or restart a GPU, PGA or ASC, you will also get
 Thread messages in the cgminer status window
diff --git a/ASIC-README b/ASIC-README
index 4da05ab..55b76d8 100644
--- a/ASIC-README
+++ b/ASIC-README
@@ -1,10 +1,11 @@
 SUPPORTED DEVICES
 
-Currently supported devices include the Avalon (including BitBurner), the
-Butterfly Labs SC range of devices, the ASICMINER block erupters and the BPMC
-BF1 (bitfury) USB 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 be disconnected when cgminer is running.
+Currently supported devices include the Avalon (including BitBurner and
+Klondike), the Butterfly Labs SC range of devices, the ASICMINER block
+erupters and the BPMC BF1 (bitfury) USB 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 be disconnected when
+cgminer is running.
 
 
 The BFL devices should come up as one of the following:
@@ -20,6 +21,10 @@ Avalon will come up as AVA.
 
 Avalon devices need the --enable-avalon option when compiling cgminer.
 
+Klondike will come up as KLN.
+
+Klondike devices need the --enable-klondike option when compiling cgminer.
+
 ASICMINER block erupters will come up as AMU.
 
 ASICMINER devices need the --enable-icarus option when compiling cgminer.
@@ -98,6 +103,7 @@ ASIC SPECIFIC COMMANDS
 --avalon-temp <arg> Set avalon target temperature (default: 50)
 --bflsc-overheat <arg> Set overheat temperature where BFLSC devices throttle, 0 to disable (default: 90)
 --bitburner-voltage <arg> Set BitBurner core voltage, in millivolts
+--klondike-options <arg> Set klondike options clock:temp1:temp2:fan
 
 
 AVALON DEVICES
diff --git a/Makefile.am b/Makefile.am
index 01ba36f..d6e8e14 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -98,14 +98,12 @@ if HAS_ICARUS
 cgminer_SOURCES += driver-icarus.c
 endif
 
-if HAS_MODMINER
-cgminer_SOURCES += driver-modminer.c
-bitstreamsdir = $(bindir)/bitstreams
-dist_bitstreams_DATA = $(top_srcdir)/bitstreams/*
+if HAS_KLONDIKE
+cgminer_SOURCES += driver-klondike.c
 endif
 
-if HAS_ZTEX
-cgminer_SOURCES += driver-ztex.c libztex.c libztex.h
+if HAS_MODMINER
+cgminer_SOURCES += driver-modminer.c
 bitstreamsdir = $(bindir)/bitstreams
 dist_bitstreams_DATA = $(top_srcdir)/bitstreams/*
 endif
diff --git a/NEWS b/NEWS
index ad19011..80afdaa 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,96 @@
+
+- We are always dependent on libusb handling events so use the blocking
+libusb_handle_events in the polling thread and use a bool to know if we should
+continue polling.
+- Use fractional hashrate return values in bitfury_scanhash to minimise the
+number of times we return 0 based on hashrate so far to further damp out
+displayed hashrate.
+- Check for presence of driver name in DRIVER_COUNT_FOUND to prevent strcmp on a
+null pointer when a driver is not built in.
+- CMR allow sending flash and clock commands
+- Kill off threads that have failed using hash_sole_work instead of just
+disabling them.
+- Make the bf1 getinfo size a macro
+- Failing to add_cgpu in bitfury should be a terminal failure.
+- Check return values when attempting to open a BF1 device and set the msg size
+as a macro.
+- Display errors on failed usb read and write and consider sequential IO errors
+a permanent failure.
+- Use libusb's own error name function instead of hand coding the error names.
+- Limit ms_tdiff to 1 hour as a sanity check.
+- Enable the usb buffer in avalon driver.
+- Check for async transfer variants of error messages.
+- Remove unused variables.
+- Try switching pools if for some reason we end up with only idle pools and have
+ended up current_pool set to an idle one.
+- Check a pool is stable for >5 mins before switching back to it.
+- Minimise the time between dropping the read devlock and grabbing the write
+devlock to avoid tons of logging spam in the interim.
+- Check for libusb transfer stall error to be consistent with async IO errors
+returned for a halt condition.
+- Check for continuous IO errors on USB and consider the device inactive if more
+than retry max.
+- Make the devlock a cglock in usbutils and only grab the write lock for
+fundamental changes allowing us to send and receive transfers concurrently
+without lock contention.
+- Prevent overflows in us_tdiff and ms_tdiff.
+- Change second initialise message on bitfury verbose mode.
+- Submitting an ntime offset nonce needs to be done on a copy of the work
+instead of the original so abstract out shared components as much as possible,
+minimising strdups in copy_work and make submit_work_async work take copied
+work, cleaning up code in the process.
+- Provide a way for drivers to submit work that it has internally rolled the
+ntime value by returning the amount it has ntime rolled to be added.
+- Typo in configure.ac
+- Remove unmaintained broken ztex driver.
+- Icarus - use a data structure for I/O rather than magic numbers
+- delete old tracked ccan/opt/*.o files
+- klondike correct cvtKlnToC() temperature calculation
+- klondike - correct 1st reply debug based on define
+- klondike - debug dump structured replies
+- klondike - avoid division by zero if maxcount is unexpectedly zero
+- klondike store and report errorcount and noise
+- klondike - fix chipstats api stats buffer overrun with 16 chips
+- klondike add new nonecount only once
+- klondike - report mh/s based on nonces found + put old estimate into API stats
+- klondike use a memcpy
+- klondike fix bracket tabs indenting
+- api.c missing Klondike from ASIC list
+- Klondike update code to current git
+- Add 2nd CMR to 01-cgminer.rules
+- Add Klondike to 01-cgminer.rules
+- Klondike to main directory
+- Klondike consistent code spacing
+- Klondike update driver code to current git
+- update firmware for 16 chips, add dist files
+- beta final 0.3.0 release
+- updated firmware, IOC method
+- prevent nonces when not state W
+- added driver config option support
+- fixes for 300 MHz, fix K1 parts list
+- update driver, docs
+- update firmware & utils
+- updated cgminer driver for 3.3.1
+- update firmware and driver, create new cgminer fork
+- update klondike driver
+- add cgminer driver file as-is
+- Add API output displaying USB cancellations.
+- Store statistics on how often we have to cancel async bulk transfers and add a
+debug message whenever we do.
+- Treat any unexpected timeouts waiting for async transfers as though there may
+be a usb halt condition and attempt to clear the halt before cancelling the
+tranfer.
+- Remove zero packet flag on usb as it's unsupported outside linux and
+unnecessary.
+- Fake the libusb transfer timed out message if we force cancel it with our own
+async functions.
+- Use asynchronous transfers for all bulk transfers, allowing us to use our own
+timers and cancelling transfers that take too long.
+- Add libusb error warning message when significant error occurs.
+- Icarus CMR2 detect FPGA setup
+- Disable bitfury device thread on it disappearing.
+
+
 Version 3.5.0 - 29th September 2013
 
 - Add magic init sequence required on BF1 devices to get them mining on windows.
diff --git a/README b/README
index 3d98de5..5c73991 100644
--- a/README
+++ b/README
@@ -114,6 +114,7 @@ CGMiner specific configuration options:
 	--enable-modminer       Compile support for ModMiner FPGAs(default disabled)
 	--enable-ztex           Compile support for Ztex Board(default disabled)
 	--enable-avalon         Compile support for Avalon (default disabled)
+	--enable-klondike       Compile support for Klondike (default disabled)
 	--enable-scrypt         Compile support for scrypt litecoin mining (default disabled)
 	--without-curses        Compile support for curses TUI (default enabled)
 
@@ -269,8 +270,8 @@ See SCRYPT-README for more information regarding litecoin mining.
 
 
 Cgminer should automatically find all of your Avalon ASIC, BFL ASIC, BitForce
-FPGAs, Icarus bitstream FPGAs, ASICMINER usb block erupters, ModMiner FPGAs,
-or Ztex FPGAs
+FPGAs, Icarus bitstream FPGAs, Klondike ASIC, ASICMINER usb block erupters,
+ModMiner FPGAs or Ztex FPGAs
 
 ---
 
@@ -314,11 +315,11 @@ just reboot.
 Advanced USB options:
 
 The --usb option can restrict how many Avalon, BFL ASIC, BitForce FPGAs,
-ModMiner FPGAs or Icarus bitstream FPGAs it finds:
+Klondike ASIC, ModMiner FPGAs or Icarus bitstream FPGAs it finds:
 
   --usb 1:2,1:3,1:4,1:*
 or
-  --usb BAS:1,BFL:1,MMQ:0,ICA:0
+  --usb BAS:1,BFL:1,MMQ:0,ICA:0,KLN:0
 or
   --usb :10
 
@@ -344,9 +345,10 @@ of details about each recognised USB device
 If you wish to see all USB devices, include the --usb-list-all option
 
 The second version
-  --usb BAS:1,BFL:1,MMQ:0,ICA:0
+  --usb BAS:1,BFL:1,MMQ:0,ICA:0,KLN:0
 allows you to specify how many devices to choose based on each device
-driver cgminer has - there are currently 4 USB drivers: BAS, BFL, MMQ & ICA
+driver cgminer has - there are currently 5 USB drivers: BAS, BFL, MMQ.
+ICA & KLN
 N.B. you can only specify which device driver to limit, not the type of
 each device, e.g. with BAS:n you can limit how many BFL ASIC devices will
 be checked, but you cannot limit the number of each type of BFL ASIC
diff --git a/api.c b/api.c
index f5bfd5b..b573767 100644
--- a/api.c
+++ b/api.c
@@ -29,11 +29,11 @@
 #include "miner.h"
 #include "util.h"
 
-#if defined(USE_BFLSC) || defined(USE_AVALON) || defined(USE_HASHFAST) || defined(USE_BITFURY)
+#if defined(USE_BFLSC) || defined(USE_AVALON) || defined(USE_HASHFAST) || defined(USE_BITFURY) || defined(USE_KLONDIKE)
 #define HAVE_AN_ASIC 1
 #endif
 
-#if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX) || defined(USE_MODMINER)
+#if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_MODMINER)
 #define HAVE_AN_FPGA 1
 #endif
 
@@ -191,8 +191,8 @@ static const char *DEVICECODE = ""
 #ifdef USE_MODMINER
 			"MMQ "
 #endif
-#ifdef USE_ZTEX
-			"ZTX "
+#ifdef USE_MODMINER
+			"MMQ "
 #endif
 			"";
 
@@ -1723,10 +1723,6 @@ static void pgastatus(struct io_data *io_data, int pga, bool isjson, bool precom
 		if (dev_runtime < 1.0)
 			dev_runtime = 1.0;
 
-#ifdef USE_ZTEX
-		if (cgpu->drv->drv_id == DRIVER_ztex && cgpu->device_ztex)
-			frequency = cgpu->device_ztex->freqM1 * (cgpu->device_ztex->freqM + 1);
-#endif
 #ifdef USE_MODMINER
 		if (cgpu->drv->drv_id == DRIVER_modminer)
 			frequency = cgpu->clock;
diff --git a/bitstreams/COPYING_ztex b/bitstreams/COPYING_ztex
deleted file mode 100644
index 99cd2ed..0000000
--- a/bitstreams/COPYING_ztex
+++ /dev/null
@@ -1,24 +0,0 @@
-All the bitstream files included in this directory that follow the name pattern ztex_*.bit are:
-
-----
-
-Copyright (C) 2009-2011 ZTEX GmbH.
-http://www.ztex.de
-
-
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License version 3 as
-published by the Free Software Foundation.
-
-This program is distributed in the hope that it will be useful, but
-WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, see http://www.gnu.org/licenses/.
-
-----
-
-You can find the original sources at the BTCMiner project home page: http://www.ztex.de/btcminer/
-
diff --git a/bitstreams/ztex_ufm1_15b1.bit b/bitstreams/ztex_ufm1_15b1.bit
deleted file mode 100644
index 4ed6f4c..0000000
Binary files a/bitstreams/ztex_ufm1_15b1.bit and /dev/null differ
diff --git a/bitstreams/ztex_ufm1_15d1.bit b/bitstreams/ztex_ufm1_15d1.bit
deleted file mode 100644
index b87eaf4..0000000
Binary files a/bitstreams/ztex_ufm1_15d1.bit and /dev/null differ
diff --git a/bitstreams/ztex_ufm1_15d3.bit b/bitstreams/ztex_ufm1_15d3.bit
deleted file mode 100644
index 78157a5..0000000
Binary files a/bitstreams/ztex_ufm1_15d3.bit and /dev/null differ
diff --git a/bitstreams/ztex_ufm1_15d4.bin b/bitstreams/ztex_ufm1_15d4.bin
deleted file mode 100644
index 411c172..0000000
Binary files a/bitstreams/ztex_ufm1_15d4.bin and /dev/null differ
diff --git a/bitstreams/ztex_ufm1_15d4.bit b/bitstreams/ztex_ufm1_15d4.bit
deleted file mode 100644
index 0951976..0000000
Binary files a/bitstreams/ztex_ufm1_15d4.bit and /dev/null differ
diff --git a/bitstreams/ztex_ufm1_15y1.bin b/bitstreams/ztex_ufm1_15y1.bin
deleted file mode 100644
index f1532f0..0000000
Binary files a/bitstreams/ztex_ufm1_15y1.bin and /dev/null differ
diff --git a/bitstreams/ztex_ufm1_15y1.bit b/bitstreams/ztex_ufm1_15y1.bit
deleted file mode 100644
index c9b38ae..0000000
Binary files a/bitstreams/ztex_ufm1_15y1.bit and /dev/null differ
diff --git a/ccan/opt/helpers.o b/ccan/opt/helpers.o
deleted file mode 100644
index 5862126..0000000
Binary files a/ccan/opt/helpers.o and /dev/null differ
diff --git a/ccan/opt/opt.o b/ccan/opt/opt.o
deleted file mode 100644
index f57d591..0000000
Binary files a/ccan/opt/opt.o and /dev/null differ
diff --git a/ccan/opt/parse.o b/ccan/opt/parse.o
deleted file mode 100644
index ef7ac29..0000000
Binary files a/ccan/opt/parse.o and /dev/null differ
diff --git a/ccan/opt/usage.o b/ccan/opt/usage.o
deleted file mode 100644
index 1e72da3..0000000
Binary files a/ccan/opt/usage.o and /dev/null differ
diff --git a/cgminer.c b/cgminer.c
index 5f2afa6..89baa2f 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -58,6 +58,9 @@ char *curly = ":D";
 #include "driver-opencl.h"
 #include "bench_block.h"
 #include "scrypt.h"
+#ifdef USE_USBUTILS
+#include "usbutils.h"
+#endif
 
 #if defined(unix) || defined(__APPLE__)
 	#include <errno.h>
@@ -79,8 +82,6 @@ char *curly = ":D";
 
 #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_AVALON) || defined(USE_MODMINER)
 #	define USE_FPGA
-#elif defined(USE_ZTEX)
-#	define USE_FPGA
 #endif
 
 struct strategies strategies[] = {
@@ -170,11 +171,16 @@ bool opt_worktime;
 #ifdef USE_AVALON
 char *opt_avalon_options = NULL;
 #endif
+#ifdef USE_KLONDIKE
+char *opt_klondike_options = NULL;
+#endif
 #ifdef USE_USBUTILS
 char *opt_usb_select = NULL;
 int opt_usbdump = -1;
 bool opt_usb_list_all;
 cgsem_t usb_resource_sem;
+static pthread_t usb_poll_thread;
+static bool usb_polling;
 #endif
 
 char *opt_kernel_path;
@@ -1030,6 +1036,15 @@ static char *set_avalon_options(const char *arg)
 }
 #endif
 
+#ifdef USE_KLONDIKE
+static char *set_klondike_options(const char *arg)
+{
+	opt_set_charp(arg, &opt_klondike_options);
+
+	return NULL;
+}
+#endif
+
 #ifdef USE_USBUTILS
 static char *set_usb_select(const char *arg)
 {
@@ -1236,6 +1251,11 @@ static struct opt_table opt_config_table[] = {
 		     opt_set_intval, NULL, &opt_bitburner_core_voltage,
 		     "Set BitBurner core voltage, in millivolts"),
 #endif
+#ifdef USE_KLONDIKE
+	OPT_WITH_ARG("--klondike-options",
+		     set_klondike_options, NULL, NULL,
+		     "Set klondike options clock:temp1:temp2:fan"),
+#endif
 	OPT_WITHOUT_ARG("--load-balance",
 		     set_loadbalance, &pool_strategy,
 		     "Change multipool strategy from failover to quota based balance"),
@@ -1579,15 +1599,15 @@ static char *opt_verusage_and_exit(const char *extra)
 #ifdef USE_ICARUS
 		"icarus "
 #endif
+#ifdef USE_KLONDIKE
+		"klondike "
+#endif
 #ifdef USE_MODMINER
 		"modminer "
 #endif
 #ifdef USE_SCRYPT
 		"scrypt "
 #endif
-#ifdef USE_ZTEX
-		"ztex "
-#endif
 		"mining support.\n"
 		, packagename);
 	printf("%s", opt_usage(opt_argv0, extra));
@@ -3503,9 +3523,23 @@ static void *submit_work_thread(void __maybe_unused *userdata)
 }
 #endif /* HAVE_LIBCURL */
 
+/* Return an adjusted ntime if we're submitting work that a device has
+ * internally offset the ntime. */
+static char *offset_ntime(const char *ntime, int noffset)
+{
+	unsigned char bin[4];
+	uint32_t h32, *be32 = (uint32_t *)bin;
+
+	hex2bin(bin, ntime, 4);
+	h32 = be32toh(*be32) + noffset;
+	*be32 = htobe32(h32);
+
+	return bin2hex(bin, 4);
+}
+
 /* Duplicates any dynamically allocated arrays within the work struct to
  * prevent a copied work struct from freeing ram belonging to another struct */
-void __copy_work(struct work *work, struct work *base_work)
+static void _copy_work(struct work *work, const struct work *base_work, int noffset)
 {
 	int id = work->id;
 
@@ -3518,8 +3552,12 @@ void __copy_work(struct work *work, struct work *base_work)
 		work->job_id = strdup(base_work->job_id);
 	if (base_work->nonce1)
 		work->nonce1 = strdup(base_work->nonce1);
-	if (base_work->ntime)
-		work->ntime = strdup(base_work->ntime);
+	if (base_work->ntime) {
+		if (noffset)
+			work->ntime = offset_ntime(base_work->ntime, noffset);
+		else
+			work->ntime = strdup(base_work->ntime);
+	}
 	if (base_work->coinbase)
 		work->coinbase = strdup(base_work->coinbase);
 }
@@ -3530,7 +3568,7 @@ struct work *copy_work(struct work *base_work)
 {
 	struct work *work = make_work();
 
-	__copy_work(work, base_work);
+	_copy_work(work, base_work, 0);
 
 	return work;
 }
@@ -3973,15 +4011,15 @@ static void set_blockdiff(const struct work *work)
 static bool test_work_current(struct work *work)
 {
 	bool ret = true;
-	char *hexstr;
+	char hexstr[20];
 
 	if (work->mandatory)
 		return ret;
 
 	/* Hack to work around dud work sneaking into test */
-	hexstr = bin2hex(work->data + 8, 18);
+	__bin2hex(hexstr, work->data + 8, 18);
 	if (!strncmp(hexstr, "000000000000000000000000000000000000", 36))
-		goto out_free;
+		return ret;
 
 	/* Search to see if this block exists yet and if not, consider it a
 	 * new block and set the current block details to this one */
@@ -4016,7 +4054,7 @@ static bool test_work_current(struct work *work)
 			applog(LOG_DEBUG, "Deleted block %d from database", deleted_block);
 		set_curblock(hexstr, work->data);
 		if (unlikely(new_blocks == 1))
-			goto out_free;
+			return ret;
 
 		work->work_block = ++work_block;
 
@@ -4039,8 +4077,6 @@ static bool test_work_current(struct work *work)
 		}
 	}
 	work->longpoll = false;
-out_free:
-	free(hexstr);
 	return ret;
 }
 
@@ -4437,6 +4473,10 @@ void write_config(FILE *fcfg)
 		fprintf(fcfg, ",\n\"icarus-options\" : \"%s\"", json_escape(opt_icarus_options));
 	if (opt_icarus_timing)
 		fprintf(fcfg, ",\n\"icarus-timing\" : \"%s\"", json_escape(opt_icarus_timing));
+#ifdef USE_KLONDIKE
+	if (opt_klondike_options)
+		fprintf(fcfg, ",\n\"klondike-options\" : \"%s\"", json_escape(opt_icarus_options));
+#endif
 #ifdef USE_USBUTILS
 	if (opt_usb_select)
 		fprintf(fcfg, ",\n\"usb\" : \"%s\"", json_escape(opt_usb_select));
@@ -5430,8 +5470,8 @@ static void *stratum_sthread(void *userdata)
 		quit(1, "Failed to create stratum_q in stratum_sthread");
 
 	while (42) {
+		char noncehex[12], nonce2hex[20];
 		struct stratum_share *sshare;
-		char *noncehex, *nonce2hex;
 		uint32_t *hash32, nonce;
 		char s[1024], nonce2[8];
 		struct work *work;
@@ -5460,7 +5500,7 @@ static void *stratum_sthread(void *userdata)
 		/* This work item is freed in parse_stratum_response */
 		sshare->work = work;
 		nonce = *((uint32_t *)(work->data + 76));
-		noncehex = bin2hex((const unsigned char *)&nonce, 4);
+		__bin2hex(noncehex, (const unsigned char *)&nonce, 4);
 		memset(s, 0, 1024);
 
 		mutex_lock(&sshare_lock);
@@ -5471,15 +5511,11 @@ static void *stratum_sthread(void *userdata)
 		memset(nonce2, 0, 8);
 		/* We only use uint32_t sized nonce2 increments internally */
 		memcpy(nonce2, &work->nonce2, sizeof(uint32_t));
-		nonce2hex = bin2hex((const unsigned char *)nonce2, work->nonce2_len);
-		if (unlikely(!nonce2hex))
-			quit(1, "Failed to bin2hex nonce2 in stratum_thread");
+		__bin2hex(nonce2hex, (const unsigned char *)nonce2, work->nonce2_len);
 
 		snprintf(s, sizeof(s),
 			"{\"params\": [\"%s\", \"%s\", \"%s\", \"%s\", \"%s\"], \"id\": %d, \"method\": \"mining.submit\"}",
 			pool->rpc_user, work->job_id, nonce2hex, work->ntime, noncehex, sshare->id);
-		free(noncehex);
-		free(nonce2hex);
 
 		applog(LOG_INFO, "Submitting share %08lx to pool %d",
 					(long unsigned int)htole32(hash32[6]), pool->pool_no);
@@ -5753,10 +5789,9 @@ out:
 
 static void pool_resus(struct pool *pool)
 {
-	if (pool_strategy == POOL_FAILOVER && pool->prio < cp_prio()) {
-		applog(LOG_WARNING, "Pool %d %s alive", pool->pool_no, pool->rpc_url);
-		switch_pools(NULL);
-	} else
+	if (pool_strategy == POOL_FAILOVER && pool->prio < cp_prio())
+		applog(LOG_WARNING, "Pool %d %s alive, testing stability", pool->pool_no, pool->rpc_url);
+	else
 		applog(LOG_INFO, "Pool %d %s alive", pool->pool_no, pool->rpc_url);
 }
 
@@ -5938,7 +5973,7 @@ static void gen_stratum_work(struct pool *pool, struct work *work)
 	cgtime(&work->tv_staged);
 }
 
-static struct work *get_work(struct thr_info *thr, const int thr_id)
+struct work *get_work(struct thr_info *thr, const int thr_id)
 {
 	struct work *work = NULL;
 
@@ -5960,14 +5995,13 @@ static struct work *get_work(struct thr_info *thr, const int thr_id)
 	return work;
 }
 
-static void submit_work_async(struct work *work_in, struct timeval *tv_work_found)
+/* Submit a copy of the tested, statistic recorded work item asynchronously */
+static void submit_work_async(struct work *work)
 {
-	struct work *work = copy_work(work_in);
 	struct pool *pool = work->pool;
 	pthread_t submit_thread;
 
-	if (tv_work_found)
-		copy_time(&work->tv_work_found, tv_work_found);
+	cgtime(&work->tv_work_found);
 
 	if (stale_work(work, true)) {
 		if (opt_submit_stale)
@@ -6006,6 +6040,9 @@ static void submit_work_async(struct work *work_in, struct timeval *tv_work_foun
 
 void inc_hw_errors(struct thr_info *thr)
 {
+	applog(LOG_INFO, "%s%d: invalid nonce - HW error", thr->cgpu->drv->name,
+	       thr->cgpu->device_id);
+
 	mutex_lock(&stats_lock);
 	hw_errors++;
 	thr->cgpu->hw_errors++;
@@ -6030,12 +6067,8 @@ bool test_nonce(struct work *work, uint32_t nonce)
 	return (be32toh(hash2_32[7]) <= diff1targ);
 }
 
-/* To be used once the work has been tested to be meet diff1 and has had its
- * nonce adjusted. */
-void submit_tested_work(struct thr_info *thr, struct work *work)
+static void update_work_stats(struct thr_info *thr, struct work *work)
 {
-	struct timeval tv_work_found;
-
 	work->share_diff = share_diff(work);
 
 	mutex_lock(&stats_lock);
@@ -6044,31 +6077,62 @@ void submit_tested_work(struct thr_info *thr, struct work *work)
 	work->pool->diff1 += work->device_diff;
 	thr->cgpu->last_device_valid_work = time(NULL);
 	mutex_unlock(&stats_lock);
+}
+
+/* To be used once the work has been tested to be meet diff1 and has had its
+ * nonce adjusted. */
+void submit_tested_work(struct thr_info *thr, struct work *work)
+{
+	struct work *work_out;
+	update_work_stats(thr, work);
 
 	if (!fulltest(work->hash2, work->target)) {
 		applog(LOG_INFO, "Share below target");
 		return;
 	}
-
-	cgtime(&tv_work_found);
-	submit_work_async(work, &tv_work_found);
+	work_out = copy_work(work);
+	submit_work_async(work_out);
 }
 
 /* Returns true if nonce for work was a valid share */
 bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
 {
-	bool ret = true;
-
 	if (test_nonce(work, nonce))
 		submit_tested_work(thr, work);
 	else {
-		applog(LOG_INFO, "%s%d: invalid nonce - HW error",
-		       thr->cgpu->drv->name, thr->cgpu->device_id);
+		inc_hw_errors(thr);
+		return false;
+	}
 
+	return true;
+}
+
+/* Allows drivers to submit work items where the driver has changed the ntime
+ * value by noffset. Must be only used with a work protocol that does not ntime
+ * roll itself intrinsically to generate work (eg stratum). We do not touch
+ * the original work struct, but the copy of it only. */
+bool submit_noffset_nonce(struct thr_info *thr, struct work *work_in, uint32_t nonce,
+			  int noffset)
+{
+	struct work *work = make_work();
+	bool ret = false;
+
+	_copy_work(work, work_in, noffset);
+	if (!test_nonce(work, nonce)) {
 		inc_hw_errors(thr);
-		ret = false;
+		goto out;
 	}
+	ret = true;
+	update_work_stats(thr, work);
+	if (!fulltest(work->hash2, work->target)) {
+		applog(LOG_INFO, "Share below target");
+		goto  out;
+	}
+	submit_work_async(work);
 
+out:
+	if (!ret)
+		free_work(work);
 	return ret;
 }
 
@@ -6193,7 +6257,8 @@ static void hash_sole_work(struct thr_info *mythr)
 				applog(LOG_ERR, "%s %d failure, disabling!", drv->name, cgpu->device_id);
 				cgpu->deven = DEV_DISABLED;
 				dev_error(cgpu, REASON_THREAD_ZERO_HASH);
-				mt_disable(mythr, thr_id, drv);
+				cgpu->shutdown = true;
+				break;
 			}
 
 			hashes_done += hashes;
@@ -6466,6 +6531,53 @@ void hash_queued_work(struct thr_info *mythr)
 	cgpu->deven = DEV_DISABLED;
 }
 
+/* This version of hash_work is for devices drivers that want to do their own
+ * work management entirely, usually by using get_work(). Note that get_work
+ * is a blocking function and will wait indefinitely if no work is available
+ * so this must be taken into consideration in the driver. */
+void hash_driver_work(struct thr_info *mythr)
+{
+	struct timeval tv_start = {0, 0}, tv_end;
+	struct cgpu_info *cgpu = mythr->cgpu;
+	struct device_drv *drv = cgpu->drv;
+	const int thr_id = mythr->id;
+	int64_t hashes_done = 0;
+
+	while (likely(!cgpu->shutdown)) {
+		struct timeval diff;
+		int64_t hashes;
+
+		mythr->work_restart = false;
+
+		hashes = drv->scanwork(mythr);
+
+		if (unlikely(hashes == -1 )) {
+			applog(LOG_ERR, "%s %d failure, disabling!", drv->name, cgpu->device_id);
+			cgpu->deven = DEV_DISABLED;
+			dev_error(cgpu, REASON_THREAD_ZERO_HASH);
+			mt_disable(mythr, thr_id, drv);
+		}
+
+		hashes_done += hashes;
+		cgtime(&tv_end);
+		timersub(&tv_end, &tv_start, &diff);
+		/* Update the hashmeter at most 5 times per second */
+		if ((hashes_done && (diff.tv_sec > 0 || diff.tv_usec > 200000)) ||
+		    diff.tv_sec >= opt_log_interval) {
+			hashmeter(thr_id, &diff, hashes_done);
+			hashes_done = 0;
+			copy_time(&tv_start, &tv_end);
+		}
+
+		if (unlikely(mythr->pause || cgpu->deven != DEV_ENABLED))
+			mt_disable(mythr, thr_id, drv);
+
+		if (unlikely(mythr->work_restart))
+			drv->flush_work(cgpu);
+	}
+	cgpu->deven = DEV_DISABLED;
+}
+
 void *miner_thread(void *userdata)
 {
 	struct thr_info *mythr = userdata;
@@ -6805,8 +6917,21 @@ static void *watchpool_thread(void __maybe_unused *userdata)
 				if (pool_active(pool, true) && pool_tclear(pool, &pool->idle))
 					pool_resus(pool);
 			}
+
+			/* Only switch pools if the failback pool has been
+			 * alive for more than 5 minutes to prevent
+			 * intermittently failing pools from being used. */
+			if (!pool->idle && pool_strategy == POOL_FAILOVER && pool->prio < cp_prio() &&
+			    now.tv_sec - pool->tv_idle.tv_sec > 300) {
+				applog(LOG_WARNING, "Pool %d %s stable for 5 mins",
+				       pool->pool_no, pool->rpc_url);
+				switch_pools(NULL);
+			}
 		}
 
+		if (current_pool()->idle)
+			switch_pools(NULL);
+
 		if (pool_strategy == POOL_ROTATE && now.tv_sec - rotate_tv.tv_sec > 60 * opt_rotate_period) {
 			cgtime(&rotate_tv);
 			switch_pools(NULL);
@@ -7005,6 +7130,9 @@ static void log_print_status(struct cgpu_info *cgpu)
 	applog(LOG_WARNING, "%s", logline);
 }
 
+static void noop_get_statline(char __maybe_unused *buf, size_t __maybe_unused bufsiz, struct cgpu_info __maybe_unused *cgpu);
+void blank_get_statline_before(char *buf, size_t bufsiz, struct cgpu_info __maybe_unused *cgpu);
+
 void print_summary(void)
 {
 	struct timeval diff;
@@ -7076,6 +7204,8 @@ void print_summary(void)
 	for (i = 0; i < total_devices; ++i) {
 		struct cgpu_info *cgpu = get_devices(i);
 
+		cgpu->drv->get_statline_before = &blank_get_statline_before;
+		cgpu->drv->get_statline = &noop_get_statline;
 		log_print_status(cgpu);
 	}
 
@@ -7096,6 +7226,8 @@ static void clean_up(void)
 	clear_adl(nDevs);
 #endif
 #ifdef USE_USBUTILS
+	usb_polling = false;
+	pthread_join(usb_poll_thread, NULL);
         libusb_exit(NULL);
 #endif
 
@@ -7170,6 +7302,7 @@ static void *test_pool_thread(void *arg)
 			applog(LOG_NOTICE, "Switching to pool %d %s - first alive pool", pool->pool_no, pool->rpc_url);
 
 		pool_resus(pool);
+		switch_pools(NULL);
 	} else
 		pool_died(pool);
 
@@ -7663,6 +7796,36 @@ static void probe_pools(void)
 #define DRIVER_FILL_DEVICE_DRV(X) fill_device_drv(&X##_drv);
 #define DRIVER_DRV_DETECT_ALL(X) X##_drv.drv_detect(false);
 
+#ifdef USE_USBUTILS
+static void *libusb_poll_thread(void __maybe_unused *arg)
+{
+	struct timeval tv_end = {0, 200000};
+
+	RenameThread("usbpoll");
+
+	while (usb_polling)
+		libusb_handle_events_timeout_completed(NULL, &tv_end, NULL);
+
+	return NULL;
+}
+
+static void initialise_usb(void) {
+	int err = libusb_init(NULL);
+	if (err) {
+		fprintf(stderr, "libusb_init() failed err %d", err);
+		fflush(stderr);
+		quit(1, "libusb_init() failed");
+	}
+	mutex_init(&cgusb_lock);
+	mutex_init(&cgusbres_lock);
+	cglock_init(&cgusb_fd_lock);
+	usb_polling = true;
+	pthread_create(&usb_poll_thread, NULL, libusb_poll_thread, NULL);
+}
+#else
+#define initialise_usb() {}
+#endif
+
 int main(int argc, char *argv[])
 {
 	struct sigaction handler;
@@ -7682,17 +7845,7 @@ int main(int argc, char *argv[])
 		initial_args[i] = strdup(argv[i]);
 	initial_args[argc] = NULL;
 
-#ifdef USE_USBUTILS
-	int err = libusb_init(NULL);
-	if (err) {
-		fprintf(stderr, "libusb_init() failed err %d", err);
-		fflush(stderr);
-		quit(1, "libusb_init() failed");
-	}
-	mutex_init(&cgusb_lock);
-	mutex_init(&cgusbres_lock);
-	cglock_init(&cgusb_fd_lock);
-#endif
+	initialise_usb();
 
 	mutex_init(&hash_lock);
 	mutex_init(&console_lock);
diff --git a/configure.ac b/configure.ac
index ab0c06d..f737b8b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -260,7 +260,7 @@ AC_ARG_ENABLE([bitfury],
 	[bitfury=$enableval]
 	)
 if test "x$bitfury" = xyes; then
-	AC_DEFINE([USE_BITFURY], [1], [Defined to 1 if BitForce support is wanted])
+	AC_DEFINE([USE_BITFURY], [1], [Defined to 1 if BitFury support is wanted])
 fi
 AM_CONDITIONAL([HAS_BITFURY], [test x$bitfury = xyes])
 
@@ -286,6 +286,17 @@ if test "x$icarus" = xyes; then
 fi
 AM_CONDITIONAL([HAS_ICARUS], [test x$icarus = xyes])
 
+klondike="no"
+
+AC_ARG_ENABLE([klondike],
+	[AC_HELP_STRING([--enable-klondike],[Compile support for Klondike (default disabled)])],
+	[klondike=$enableval]
+	)
+if test "x$klondike" = xyes; then
+	AC_DEFINE([USE_KLONDIKE], [1], [Defined to 1 if Klondike support is wanted])
+fi
+AM_CONDITIONAL([HAS_KLONDIKE], [test x$klondike = xyes])
+
 modminer="no"
 
 AC_ARG_ENABLE([modminer],
@@ -297,17 +308,6 @@ if test "x$modminer" = xyes; then
 fi
 AM_CONDITIONAL([HAS_MODMINER], [test x$modminer = xyes])
 
-ztex="no"
-
-AC_ARG_ENABLE([ztex],
-	[AC_HELP_STRING([--enable-ztex],[Compile support for Ztex (default disabled)])],
-	[ztex=$enableval]
-	)
-if test "x$ztex" = xyes; then
-	AC_DEFINE([USE_ZTEX], [1], [Defined to 1 if Ztex support is wanted])
-fi
-AM_CONDITIONAL([HAS_ZTEX], [test x$ztex = xyes])
-
 curses="auto"
 
 AC_ARG_WITH([curses],
@@ -332,13 +332,13 @@ else
 	])
 fi
 
-if test x$avalon$bitforce$bitfury$modminer$bflsc$icarus$hashfast != xnonononononono; then
+if test x$avalon$bitforce$bitfury$modminer$bflsc$icarus$hashfast$klondike != xnononononononono; then
 	want_usbutils=true
 else
 	want_usbutils=false
 fi
 
-AM_CONDITIONAL([NEED_FPGAUTILS], [test x$modminer$ztex != xnono])
+AM_CONDITIONAL([NEED_FPGAUTILS], [test x$modminer != xno])
 AM_CONDITIONAL([WANT_USBUTILS], [test x$want_usbutils != xfalse])
 AM_CONDITIONAL([HAVE_CURSES], [test x$curses = xyes])
 AM_CONDITIONAL([HAVE_WINDOWS], [test x$have_win32 = xtrue])
@@ -504,14 +504,14 @@ if test "x$opencl" != xno; then
 
 	else
 		echo "  OpenCL...............: NOT FOUND. GPU mining support DISABLED"
-		if test "x$avalon$bitforce$bitfury$icarus$ztex$modminer$bflsc$hashfast" = xnononononononono; then
+		if test "x$avalon$bitforce$bitfury$icarus$ztex$modminer$bflsc$hashfast$klondike" = xnonononononononono; then
 			AC_MSG_ERROR([No mining configured in])
 		fi
 		echo "  scrypt...............: Disabled (needs OpenCL)"
 	fi
 else
 	echo "  OpenCL...............: Detection overrided. GPU mining support DISABLED"
-	if test "x$avalon$bitforce$bitfury$icarus$ztex$modminer$bflsc$hashfast" = xnononononononono; then
+	if test "x$avalon$bitforce$bitfury$icarus$ztex$modminer$bflsc$hashfast$klondike" = xnonononononononono; then
 		AC_MSG_ERROR([No mining configured in])
 	fi
 	echo "  scrypt...............: Disabled (needs OpenCL)"
@@ -564,16 +564,16 @@ else
 	echo "  Icarus.ASICs/FPGAs...: Disabled"
 fi
 
-if test "x$modminer" = xyes; then
-	echo "  ModMiner.FPGAs.......: Enabled"
+if test "x$klondike" = xyes; then
+	echo "  Klondike.ASICs.......: Enabled"
 else
-	echo "  ModMiner.FPGAs.......: Disabled"
+	echo "  Klondike.ASICs.......: Disabled"
 fi
 
-if test "x$ztex" = xyes; then
-	echo "  Ztex.FPGAs...........: Enabled"
+if test "x$modminer" = xyes; then
+	echo "  ModMiner.FPGAs.......: Enabled"
 else
-	echo "  Ztex.FPGAs...........: Disabled"
+	echo "  ModMiner.FPGAs.......: Disabled"
 fi
 
 echo
@@ -586,4 +586,3 @@ echo
 echo "Installation...........: make install (as root if needed, with 'su' or 'sudo')"
 echo "  prefix...............: $prefix"
 echo
-
diff --git a/driver-avalon.c b/driver-avalon.c
index 6abdaeb..341a635 100644
--- a/driver-avalon.c
+++ b/driver-avalon.c
@@ -720,6 +720,7 @@ static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found
 	 * all ourselves so set it to std usb type */
 	avalon->usbdev->usb_type = USB_TYPE_STD;
 	usb_set_pps(avalon, AVALON_USB_PACKETSIZE);
+	usb_buffer_enable(avalon);
 
 	/* We have a real Avalon! */
 	avalon_initialise(avalon);
diff --git a/driver-bitfury.c b/driver-bitfury.c
index 8069a1d..e5757f5 100644
--- a/driver-bitfury.c
+++ b/driver-bitfury.c
@@ -15,6 +15,8 @@
 
 /* Wait longer 1/3 longer than it would take for a full nonce range */
 #define BF1WAIT 1600
+#define BF1MSGSIZE 7
+#define BF1INFOSIZE 14
 
 static void bitfury_empty_buffer(struct cgpu_info *bitfury)
 {
@@ -26,18 +28,29 @@ static void bitfury_empty_buffer(struct cgpu_info *bitfury)
 	} while (amount);
 }
 
-static void bitfury_open(struct cgpu_info *bitfury)
+static int bitfury_open(struct cgpu_info *bitfury)
 {
 	uint32_t buf[2];
+	int err;
 
 	bitfury_empty_buffer(bitfury);
 	/* Magic sequence to reset device only really needed for windows but
 	 * harmless on linux. */
 	buf[0] = 0x80250000;
 	buf[1] = 0x00000800;
-	usb_transfer(bitfury, 0, 9, 1, 0, C_BF1_RESET);
-	usb_transfer(bitfury, 0x21, 0x22, 0, 0, C_BF1_OPEN);
-	usb_transfer_data(bitfury, 0x21, 0x20, 0x0000, 0, buf, 7, C_BF1_INIT);
+	err = usb_transfer(bitfury, 0, 9, 1, 0, C_BF1_RESET);
+	if (!err)
+		err = usb_transfer(bitfury, 0x21, 0x22, 0, 0, C_BF1_OPEN);
+	if (!err) {
+		err = usb_transfer_data(bitfury, 0x21, 0x20, 0x0000, 0, buf,
+					BF1MSGSIZE, C_BF1_INIT);
+	}
+
+	if (err < 0) {
+		applog(LOG_INFO, "%s %d: Failed to open with error %s", bitfury->drv->name,
+		       bitfury->device_id, libusb_error_name(err));
+	}
+	return (err == BF1MSGSIZE);
 }
 
 static void bitfury_close(struct cgpu_info *bitfury)
@@ -63,15 +76,15 @@ static bool bitfury_getinfo(struct cgpu_info *bitfury, struct bitfury_info *info
 		       bitfury->drv->name, bitfury->device_id);
 		return false;
 	}
-	err = usb_read(bitfury, buf, 14, &amount, C_BF1_GETINFO);
+	err = usb_read(bitfury, buf, BF1INFOSIZE, &amount, C_BF1_GETINFO);
 	if (err) {
 		applog(LOG_INFO, "%s %d: Failed to read GETINFO",
 		       bitfury->drv->name, bitfury->device_id);
 		return false;
 	}
-	if (amount != 14) {
-		applog(LOG_INFO, "%s %d: Getinfo received %d bytes instead of 14",
-		       bitfury->drv->name, bitfury->device_id, amount);
+	if (amount != BF1INFOSIZE) {
+		applog(LOG_INFO, "%s %d: Getinfo received %d bytes instead of %d",
+		       bitfury->drv->name, bitfury->device_id, amount, BF1INFOSIZE);
 		return false;
 	}
 	info->version = buf[1];
@@ -95,15 +108,16 @@ static bool bitfury_reset(struct cgpu_info *bitfury)
 		       bitfury->drv->name, bitfury->device_id);
 		return false;
 	}
-	err = usb_read_timeout(bitfury, buf, 7, &amount, BF1WAIT, C_BF1_GETRESET);
+	err = usb_read_timeout(bitfury, buf, BF1MSGSIZE, &amount, BF1WAIT,
+			       C_BF1_GETRESET);
 	if (err) {
 		applog(LOG_INFO, "%s %d: Failed to read GETRESET",
 		       bitfury->drv->name, bitfury->device_id);
 		return false;
 	}
-	if (amount != 7) {
-		applog(LOG_INFO, "%s %d: Getreset received %d bytes instead of 7",
-		       bitfury->drv->name, bitfury->device_id, amount);
+	if (amount != BF1MSGSIZE) {
+		applog(LOG_INFO, "%s %d: Getreset received %d bytes instead of %d",
+		       bitfury->drv->name, bitfury->device_id, amount, BF1MSGSIZE);
 		return false;
 	}
 	applog(LOG_DEBUG, "%s %d: Getreset returned %s", bitfury->drv->name,
@@ -128,10 +142,14 @@ static bool bitfury_detect_one(struct libusb_device *dev, struct usb_find_device
 	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;
 
 	usb_buffer_enable(bitfury);
 
-	bitfury_open(bitfury);
+	if (!bitfury_open(bitfury))
+		goto out_close;
 
 	/* Send getinfo request */
 	if (!bitfury_getinfo(bitfury, info))
@@ -145,10 +163,10 @@ static bool bitfury_detect_one(struct libusb_device *dev, struct usb_find_device
 	bitfury_empty_buffer(bitfury);
 
 	if (!add_cgpu(bitfury))
-		goto out_close;
+		quit(1, "Failed to add_cgpu in bitfury_detect_one");
 
 	update_usb_stats(bitfury);
-	applog(LOG_INFO, "%s %d: Found at %s",
+	applog(LOG_INFO, "%s %d: Successfully initialised %s",
 	       bitfury->drv->name, bitfury->device_id, bitfury->device_path);
 	return true;
 out_close:
@@ -202,16 +220,24 @@ static bool bitfury_checkresults(struct thr_info *thr, struct work *work, uint32
 	return false;
 }
 
-static int64_t bitfury_scanhash(struct thr_info *thr, struct work *work,
-				int64_t __maybe_unused max_nonce)
+static int64_t bitfury_scanwork(struct thr_info *thr)
 {
 	struct cgpu_info *bitfury = thr->cgpu;
 	struct bitfury_info *info = bitfury->device_data;
 	struct timeval tv_now;
+	struct work *work;
+	double nonce_rate;
+	int64_t ret = 0;
 	int amount, i;
 	char buf[45];
 	int ms_diff;
 
+	work = get_work(thr, thr->id);
+	if (unlikely(thr->work_restart)) {
+		free_work(work);
+		return 0;
+	}
+
 	buf[0] = 'W';
 	memcpy(buf + 1, work->midstate, 32);
 	memcpy(buf + 33, work->data + 64, 12);
@@ -235,7 +261,8 @@ static int64_t bitfury_scanhash(struct thr_info *thr, struct work *work,
 	ms_diff = BF1WAIT - ms_tdiff(&tv_now, &info->tv_start);
 	if (unlikely(ms_diff < 10))
 		ms_diff = 10;
-	usb_read_once_timeout(bitfury, info->buf + info->tot, 7, &amount, ms_diff, C_BF1_GETRES);
+	usb_read_once_timeout(bitfury, info->buf + info->tot, BF1MSGSIZE,
+			      &amount, ms_diff, C_BF1_GETRES);
 	info->tot += amount;
 	while (amount) {
 		usb_read_once_timeout(bitfury, info->buf + info->tot, 512, &amount, 10, C_BF1_GETRES);
@@ -249,7 +276,7 @@ static int64_t bitfury_scanhash(struct thr_info *thr, struct work *work,
 	usb_write(bitfury, buf, 45, &amount, C_BF1_REQWORK);
 	cgtime(&info->tv_start);
 	/* Get response acknowledging work */
-	usb_read(bitfury, buf, 7, &amount, C_BF1_GETWORK);
+	usb_read(bitfury, buf, BF1MSGSIZE, &amount, C_BF1_GETWORK);
 
 	/* Only happens on startup */
 	if (unlikely(!info->prevwork[BF1ARRAY_SIZE]))
@@ -257,7 +284,7 @@ static int64_t bitfury_scanhash(struct thr_info *thr, struct work *work,
 
 	/* Search for what work the nonce matches in order of likelihood. Last
 	 * entry is end of result marker. */
-	for (i = 0; i < info->tot - 7; i += 7) {
+	for (i = 0; i < info->tot - BF1MSGSIZE; i += BF1MSGSIZE) {
 		uint32_t nonce;
 		int j;
 
@@ -277,19 +304,31 @@ static int64_t bitfury_scanhash(struct thr_info *thr, struct work *work,
 cascade:
 	for (i = BF1ARRAY_SIZE; i > 0; i--)
 		info->prevwork[i] = info->prevwork[i - 1];
-	info->prevwork[0] = copy_work(work);
-	work->blk.nonce = 0xffffffff;
-	if (info->nonces) {
-		info->nonces--;
-		return (int64_t)0xffffffff;
+	info->prevwork[0] = work;
+
+	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;
+	}
+
+	if (unlikely(bitfury->usbinfo.nodev)) {
+		applog(LOG_WARNING, "%s %d: Device disappeared, disabling thread",
+		       bitfury->drv->name, bitfury->device_id);
+		ret = -1;
 	}
-	return 0;
+	return ret;
 }
 
 static struct api_data *bitfury_api_stats(struct cgpu_info *cgpu)
 {
 	struct bitfury_info *info = cgpu->device_data;
 	struct api_data *root = NULL;
+	double nonce_rate;
 	char serial[16];
 	int version;
 
@@ -298,6 +337,8 @@ static struct api_data *bitfury_api_stats(struct cgpu_info *cgpu)
 	root = api_add_string(root, "Product", info->product, false);
 	sprintf(serial, "%08x", info->serial);
 	root = api_add_string(root, "Serial", serial, true);
+	nonce_rate = (double)info->total_nonces / (double)info->cycles;
+	root = api_add_double(root, "NonceRate", &nonce_rate, true);
 
 	return root;
 }
@@ -322,7 +363,8 @@ struct device_drv bitfury_drv = {
 	.dname = "bitfury",
 	.name = "BF1",
 	.drv_detect = bitfury_detect,
-	.scanhash = bitfury_scanhash,
+	.hash_work = &hash_driver_work,
+	.scanwork = bitfury_scanwork,
 	.get_api_stats = bitfury_api_stats,
 	.reinit_device = bitfury_init,
 	.thread_shutdown = bitfury_shutdown,
diff --git a/driver-bitfury.h b/driver-bitfury.h
index 9cbe420..07e795f 100644
--- a/driver-bitfury.h
+++ b/driver-bitfury.h
@@ -24,6 +24,9 @@ struct bitfury_info {
 	char buf[512];
 	int tot;
 	int nonces;
+	int total_nonces;
+	double saved_nonces;
+	int cycles;
 	struct timeval tv_start;
 };
 
diff --git a/driver-icarus.c b/driver-icarus.c
index 6ac1a0b..2655c14 100644
--- a/driver-icarus.c
+++ b/driver-icarus.c
@@ -108,6 +108,8 @@ ASSERT1(sizeof(uint32_t) == 4);
 #define CAIRNSMORE2_HASH_TIME 0.0000000066600
 #define NANOSEC 1000000000.0
 
+#define CAIRNSMORE2_INTS 4
+
 // Icarus Rev3 doesn't send a completion message when it finishes
 // the full nonce range, so to avoid being idle we must abort the
 // work (by starting a new work item) shortly before it finishes
@@ -178,6 +180,7 @@ static const char *MODE_VALUE_STR = "value";
 static const char *MODE_UNKNOWN_STR = "unknown";
 
 struct ICARUS_INFO {
+	enum sub_ident ident;
 	int intinfo;
 
 	// time to calculate the golden_ob
@@ -216,7 +219,45 @@ struct ICARUS_INFO {
 	int fpga_count;
 	uint32_t nonce_mask;
 
-	bool initialised;
+	uint8_t cmr2_speed;
+	bool speed_next_work;
+	bool flash_next_work;
+};
+
+#define ICARUS_MIDSTATE_SIZE 32
+#define ICARUS_UNUSED_SIZE 16
+#define ICARUS_WORK_SIZE 12
+
+#define ICARUS_WORK_DATA_OFFSET 64
+
+#define ICARUS_CMR2_SPEED_FACTOR 2.5
+#define ICARUS_CMR2_SPEED_MIN_INT 100
+#define ICARUS_CMR2_SPEED_DEF_INT 180
+#define ICARUS_CMR2_SPEED_MAX_INT 220
+#define CMR2_INT_TO_SPEED(_speed) ((uint8_t)((float)_speed / ICARUS_CMR2_SPEED_FACTOR))
+#define ICARUS_CMR2_SPEED_MIN CMR2_INT_TO_SPEED(ICARUS_CMR2_SPEED_MIN_INT)
+#define ICARUS_CMR2_SPEED_DEF CMR2_INT_TO_SPEED(ICARUS_CMR2_SPEED_DEF_INT)
+#define ICARUS_CMR2_SPEED_MAX CMR2_INT_TO_SPEED(ICARUS_CMR2_SPEED_MAX_INT)
+#define ICARUS_CMR2_SPEED_INC 1
+#define ICARUS_CMR2_SPEED_DEC -1
+#define ICARUS_CMR2_SPEED_FAIL -10
+
+#define ICARUS_CMR2_PREFIX ((uint8_t)0xB7)
+#define ICARUS_CMR2_CMD_SPEED ((uint8_t)0)
+#define ICARUS_CMR2_CMD_FLASH ((uint8_t)1)
+#define ICARUS_CMR2_DATA_FLASH_OFF ((uint8_t)0)
+#define ICARUS_CMR2_DATA_FLASH_ON ((uint8_t)1)
+#define ICARUS_CMR2_CHECK ((uint8_t)0x6D)
+
+struct ICARUS_WORK {
+	uint8_t midstate[ICARUS_MIDSTATE_SIZE];
+	// These 4 bytes are for CMR2 bitstreams that handle MHz adjustment
+	uint8_t check;
+	uint8_t data;
+	uint8_t cmd;
+	uint8_t prefix;
+	uint8_t unused[ICARUS_UNUSED_SIZE];
+	uint8_t work[ICARUS_WORK_SIZE];
 };
 
 #define END_CONDITION 0x0000ffff
@@ -411,8 +452,6 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud)
 			quit(1, "icarus_intialise() called with invalid %s cgid %i ident=%d",
 				icarus->drv->name, icarus->cgminer_id, ident);
 	}
-
-	info->initialised = true;
 }
 
 static void rev(unsigned char *s, size_t l)
@@ -803,13 +842,18 @@ static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices
 
 	const char golden_nonce[] = "000187a2";
 	const uint32_t golden_nonce_val = 0x000187a2;
-	unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
+	unsigned char nonce_bin[ICARUS_READ_SIZE];
+	struct ICARUS_WORK workdata;
 	char *nonce_hex;
 	int baud, uninitialised_var(work_division), uninitialised_var(fpga_count);
 	struct cgpu_info *icarus;
-	int ret, err, amount, tries;
-	enum sub_ident ident;
+	int ret, err, amount, tries, i;
 	bool ok;
+	bool cmr2_ok[CAIRNSMORE2_INTS];
+	int cmr2_count;
+
+	if ((sizeof(workdata) << 1) != (sizeof(golden_ob) - 1))
+		quithere(1, "Data and golden_ob sizes don't match");
 
 	icarus = usb_alloc_cgpu(&icarus_drv, 1);
 
@@ -820,15 +864,15 @@ static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices
 
 	get_options(this_option_offset, icarus, &baud, &work_division, &fpga_count);
 
-	hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
+	hex2bin((void *)(&workdata), golden_ob, sizeof(workdata));
 
 	info = (struct ICARUS_INFO *)calloc(1, sizeof(struct ICARUS_INFO));
 	if (unlikely(!info))
 		quit(1, "Failed to malloc ICARUS_INFO");
 	icarus->device_data = (void *)info;
 
-	ident = usb_ident(icarus);
-	switch (ident) {
+	info->ident = usb_ident(icarus);
+	switch (info->ident) {
 		case IDENT_ICA:
 		case IDENT_BLT:
 		case IDENT_LLT:
@@ -837,21 +881,34 @@ static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices
 			info->timeout = ICARUS_WAIT_TIMEOUT;
 			break;
 		case IDENT_CMR2:
+			if (found->intinfo_count != CAIRNSMORE2_INTS) {
+				quithere(1, "CMR2 Interface count (%d) isn't expected: %d",
+						found->intinfo_count,
+						CAIRNSMORE2_INTS);
+			}
 			info->timeout = ICARUS_CMR2_TIMEOUT;
+			cmr2_count = 0;
+			for (i = 0; i < CAIRNSMORE2_INTS; i++)
+				cmr2_ok[i] = false;
 			break;
 		default:
 			quit(1, "%s icarus_detect_one() invalid %s ident=%d",
-				icarus->drv->dname, icarus->drv->dname, ident);
+				icarus->drv->dname, icarus->drv->dname, info->ident);
 	}
 
+// For CMR2 test each USB Interface
+
+cmr2_retry:
+
 	tries = 2;
 	ok = false;
 	while (!ok && tries-- > 0) {
 		icarus_initialise(icarus, baud);
 
-		err = usb_write(icarus, (char *)ob_bin, sizeof(ob_bin), &amount, C_SENDTESTWORK);
+		err = usb_write_ii(icarus, info->intinfo,
+				   (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
 
-		if (err != LIBUSB_SUCCESS || amount != sizeof(ob_bin))
+		if (err != LIBUSB_SUCCESS || amount != sizeof(workdata))
 			continue;
 
 		memset(nonce_bin, 0, sizeof(nonce_bin));
@@ -863,7 +920,7 @@ static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices
 		if (strncmp(nonce_hex, golden_nonce, 8) == 0)
 			ok = true;
 		else {
-			if (tries < 0) {
+			if (tries < 0 && info->ident != IDENT_CMR2) {
 				applog(LOG_ERR,
 					"Icarus Detect: "
 					"Test failed at %s: get %s, should: %s",
@@ -873,13 +930,50 @@ static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices
 		free(nonce_hex);
 	}
 
-	if (!ok)
-		goto unshin;
+	if (!ok) {
+		if (info->ident != IDENT_CMR2)
+			goto unshin;
+
+		if (info->intinfo < CAIRNSMORE2_INTS-1) {
+			info->intinfo++;
+			goto cmr2_retry;
+		}
+	} else {
+		if (info->ident == IDENT_CMR2) {
+			applog(LOG_DEBUG,
+				"Icarus Detect: "
+				"Test succeeded at %s i%d: got %s",
+					icarus->device_path, info->intinfo, golden_nonce);
+
+			cmr2_ok[info->intinfo] = true;
+			cmr2_count++;
+			if (info->intinfo < CAIRNSMORE2_INTS-1) {
+				info->intinfo++;
+				goto cmr2_retry;
+			}
+		}
+	}
+
+	if (info->ident == IDENT_CMR2) {
+		if (cmr2_count == 0) {
+			applog(LOG_ERR,
+				"Icarus Detect: Test failed at %s: for all %d CMR2 Interfaces",
+				icarus->device_path, CAIRNSMORE2_INTS);
+			goto unshin;
+		}
 
-	applog(LOG_DEBUG,
-		"Icarus Detect: "
-		"Test succeeded at %s: got %s",
-			icarus->device_path, golden_nonce);
+		// set the interface to the first one that succeeded
+		for (i = 0; i < CAIRNSMORE2_INTS; i++)
+			if (cmr2_ok[i]) {
+				info->intinfo = i;
+				break;
+			}
+	} else {
+		applog(LOG_DEBUG,
+			"Icarus Detect: "
+			"Test succeeded at %s: got %s",
+				icarus->device_path, golden_nonce);
+	}
 
 	/* We have a real Icarus! */
 	if (!add_cgpu(icarus))
@@ -890,6 +984,18 @@ static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices
 	applog(LOG_INFO, "%s%d: Found at %s",
 		icarus->drv->name, icarus->device_id, icarus->device_path);
 
+	if (info->ident == IDENT_CMR2) {
+		applog(LOG_INFO, "%s%d: with %d Interface%s",
+				icarus->drv->name, icarus->device_id,
+				cmr2_count, cmr2_count > 1 ? "s" : "");
+
+		// Assume 1 or 2 are running FPGA pairs
+		if (cmr2_count < 3) {
+			work_division = fpga_count = 2;
+			info->Hs /= 2;
+		}
+	}
+
 	applog(LOG_DEBUG, "%s%d: Init baud=%d work_division=%d fpga_count=%d",
 		icarus->drv->name, icarus->device_id, baud, work_division, fpga_count);
 
@@ -903,12 +1009,15 @@ static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices
 
 	set_timing_mode(this_option_offset, icarus);
 	
-	if (usb_ident(icarus) == IDENT_CMR2) {
+	if (info->ident == IDENT_CMR2) {
 		int i;
-		for (i = 1; i < icarus->usbdev->found->intinfo_count; i++) {
+		for (i = info->intinfo + 1; i < icarus->usbdev->found->intinfo_count; i++) {
 			struct cgpu_info *cgtmp;
 			struct ICARUS_INFO *intmp;
 
+			if (!cmr2_ok[i])
+				continue;
+
 			cgtmp = usb_copy_cgpu(icarus);
 			if (!cgtmp) {
 				applog(LOG_ERR, "%s%d: Init failed initinfo %d",
@@ -968,13 +1077,53 @@ static bool icarus_prepare(__maybe_unused struct thr_info *thr)
 	return true;
 }
 
+static void cmr2_command(struct cgpu_info *icarus, uint8_t cmd, uint8_t data)
+{
+	struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
+	struct ICARUS_WORK workdata;
+	int amount;
+
+	memset((void *)(&workdata), 0, sizeof(workdata));
+
+	workdata.prefix = ICARUS_CMR2_PREFIX;
+	workdata.cmd = cmd;
+	workdata.data = data;
+	workdata.check = workdata.data ^ workdata.cmd ^ workdata.prefix ^ ICARUS_CMR2_CHECK;
+
+	usb_write_ii(icarus, info->intinfo, (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
+}
+
+static void cmr2_commands(struct cgpu_info *icarus)
+{
+	struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
+
+	if (info->speed_next_work) {
+		info->speed_next_work = false;
+		cmr2_command(icarus, ICARUS_CMR2_CMD_SPEED, info->cmr2_speed);
+		return;
+	}
+
+	if (info->flash_next_work) {
+		info->flash_next_work = false;
+		cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_ON);
+		cgsleep_ms(250);
+		cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_OFF);
+		cgsleep_ms(250);
+		cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_ON);
+		cgsleep_ms(250);
+		cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_OFF);
+		return;
+	}
+}
+
 static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
 				__maybe_unused int64_t max_nonce)
 {
 	struct cgpu_info *icarus = thr->cgpu;
 	struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
 	int ret, err, amount;
-	unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
+	unsigned char nonce_bin[ICARUS_READ_SIZE];
+	struct ICARUS_WORK workdata;
 	char *ob_hex;
 	uint32_t nonce;
 	int64_t hash_count;
@@ -997,22 +1146,22 @@ static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
 	if (icarus->usbinfo.nodev)
 		return -1;
 
-	if (!info->initialised)
-		icarus_initialise(icarus, info->baud);
-
 	elapsed.tv_sec = elapsed.tv_usec = 0;
 
-	memset(ob_bin, 0, sizeof(ob_bin));
-	memcpy(ob_bin, work->midstate, 32);
-	memcpy(ob_bin + 52, work->data + 64, 12);
-	rev(ob_bin, 32);
-	rev(ob_bin + 52, 12);
+	memset((void *)(&workdata), 0, sizeof(workdata));
+	memcpy(&(workdata.midstate), work->midstate, ICARUS_MIDSTATE_SIZE);
+	memcpy(&(workdata.work), work->data + ICARUS_WORK_DATA_OFFSET, ICARUS_WORK_SIZE);
+	rev((void *)(&(workdata.midstate)), ICARUS_MIDSTATE_SIZE);
+	rev((void *)(&(workdata.work)), ICARUS_WORK_SIZE);
+
+	if (info->speed_next_work || info->flash_next_work)
+		cmr2_commands(icarus);
 
 	// We only want results for the work we are about to send
 	usb_buffer_clear(icarus);
 
-	err = usb_write_ii(icarus, info->intinfo, (char *)ob_bin, sizeof(ob_bin), &amount, C_SENDWORK);
-	if (err < 0 || amount != sizeof(ob_bin)) {
+	err = usb_write_ii(icarus, info->intinfo, (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
+	if (err < 0 || amount != sizeof(workdata)) {
 		applog(LOG_ERR, "%s%i: Comms error (werr=%d amt=%d)",
 				icarus->drv->name, icarus->device_id, err, amount);
 		dev_error(icarus, REASON_DEV_COMMS_ERROR);
@@ -1021,7 +1170,7 @@ static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
 	}
 
 	if (opt_debug) {
-		ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
+		ob_hex = bin2hex((void *)(&workdata), sizeof(workdata));
 		applog(LOG_DEBUG, "%s%d: sent %s",
 			icarus->drv->name, icarus->device_id, ob_hex);
 		free(ob_hex);
@@ -1228,17 +1377,80 @@ static struct api_data *icarus_api_stats(struct cgpu_info *cgpu)
 	return root;
 }
 
+static void icarus_statline_before(char *buf, size_t bufsiz, struct cgpu_info *cgpu)
+{
+	struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data);
+
+	if (info->ident == IDENT_CMR2 && info->cmr2_speed > 0)
+		tailsprintf(buf, bufsiz, "%5.1fMhz", (float)(info->cmr2_speed) * ICARUS_CMR2_SPEED_FACTOR);
+	else
+		tailsprintf(buf, bufsiz, "        ");
+
+	tailsprintf(buf, bufsiz, "        | ");
+}
+
 static void icarus_shutdown(__maybe_unused struct thr_info *thr)
 {
 	// TODO: ?
 }
 
+static void icarus_identify(struct cgpu_info *cgpu)
+{
+	struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data);
+
+	if (info->ident == IDENT_CMR2)
+		info->flash_next_work = true;
+}
+
+static char *icarus_set(struct cgpu_info *cgpu, char *option, char *setting, char *replybuf)
+{
+	struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data);
+	int val;
+
+	if (info->ident != IDENT_CMR2) {
+		strcpy(replybuf, "no set options available");
+		return replybuf;
+	}
+
+	if (strcasecmp(option, "help") == 0) {
+		sprintf(replybuf, "clock: range %d-%d",
+				  ICARUS_CMR2_SPEED_MIN_INT, ICARUS_CMR2_SPEED_MAX_INT);
+		return replybuf;
+	}
+
+	if (strcasecmp(option, "clock") == 0) {
+		if (!setting || !*setting) {
+			sprintf(replybuf, "missing clock setting");
+			return replybuf;
+		}
+
+		val = atoi(setting);
+		if (val < ICARUS_CMR2_SPEED_MIN_INT || val > ICARUS_CMR2_SPEED_MAX_INT) {
+			sprintf(replybuf, "invalid clock: '%s' valid range %d-%d",
+					  setting,
+					  ICARUS_CMR2_SPEED_MIN_INT,
+					  ICARUS_CMR2_SPEED_MAX_INT);
+		}
+
+		info->cmr2_speed = CMR2_INT_TO_SPEED(val);
+		info->speed_next_work = true;
+
+		return NULL;
+	}
+
+	sprintf(replybuf, "Unknown option: %s", option);
+	return replybuf;
+}
+
 struct device_drv icarus_drv = {
 	.drv_id = DRIVER_icarus,
 	.dname = "Icarus",
 	.name = "ICA",
 	.drv_detect = icarus_detect,
 	.get_api_stats = icarus_api_stats,
+	.get_statline_before = icarus_statline_before,
+	.set_device = icarus_set,
+	.identify_device = icarus_identify,
 	.thread_prepare = icarus_prepare,
 	.scanhash = icarus_scanhash,
 	.thread_shutdown = icarus_shutdown,
diff --git a/driver-klondike.c b/driver-klondike.c
new file mode 100644
index 0000000..7dafc1d
--- /dev/null
+++ b/driver-klondike.c
@@ -0,0 +1,756 @@
+/*
+ * Copyright 2013 Andrew Smith
+ * Copyright 2013 Con Kolivas
+ * Copyright 2013 Chris Savery
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.  See COPYING for more details.
+ */
+
+#include <float.h>
+#include <limits.h>
+#include <pthread.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <strings.h>
+#include <sys/time.h>
+#include <unistd.h>
+#include <math.h>
+
+#include "config.h"
+
+#ifdef WIN32
+#include <windows.h>
+#endif
+
+#include "compat.h"
+#include "miner.h"
+#include "usbutils.h"
+
+#define K1 "K1"
+#define K16 "K16"
+#define K64 "K64"
+
+#define MIDSTATE_BYTES 32
+#define MERKLE_OFFSET 64
+#define MERKLE_BYTES 12
+
+#define REPLY_SIZE		15	// adequate for all types of replies
+#define REPLY_BUFSIZE 		16	// reply + 1 byte to mark used
+#define MAX_REPLY_COUNT		4096	// more unhandled replies than this will result in data loss
+#define REPLY_WAIT_TIME		100 	// poll interval for a cmd waiting it's reply
+#define CMD_REPLY_RETRIES	8	// how many retries for cmds
+#define MAX_WORK_COUNT		4	// for now, must be binary multiple and match firmware
+#define TACH_FACTOR		87890	// fan rpm divisor
+
+struct device_drv klondike_drv;
+
+typedef struct klondike_id {
+	uint8_t version;
+	uint8_t product[7];
+	uint32_t serial;
+} IDENTITY;
+
+typedef struct klondike_status {
+	uint8_t state;
+	uint8_t chipcount;
+	uint8_t slavecount;
+	uint8_t workqc;
+	uint8_t workid;
+	uint8_t temp;
+	uint8_t fanspeed;
+	uint8_t errorcount;
+	uint16_t hashcount;
+	uint16_t maxcount;
+	uint8_t noise;
+} WORKSTATUS;
+
+typedef struct _worktask {
+	uint16_t pad1;
+	uint8_t pad2;
+	uint8_t workid;
+	uint32_t midstate[8];
+	uint32_t merkle[3];
+} WORKTASK;
+
+typedef struct _workresult {
+	uint16_t pad;
+	uint8_t device;
+	uint8_t workid;
+	uint32_t nonce;
+} WORKRESULT;
+
+typedef struct klondike_cfg {
+	uint16_t hashclock;
+	uint8_t temptarget;
+	uint8_t tempcritical;
+	uint8_t fantarget;
+	uint8_t pad;
+} WORKCFG;
+
+typedef struct device_info {
+	uint32_t noncecount;
+	uint32_t nextworkid;
+	uint16_t lasthashcount;
+	uint64_t totalhashcount;
+	uint32_t rangesize;
+	uint32_t *chipstats;
+} DEVINFO;
+
+struct klondike_info {
+	bool shutdown;
+	pthread_rwlock_t stat_lock;
+	struct thr_info replies_thr;
+	WORKSTATUS *status;
+	DEVINFO *devinfo;
+	WORKCFG *cfg;
+	char *replies;
+	int nextreply;
+	int noncecount;
+	uint64_t hashcount;
+	uint64_t errorcount;
+	uint64_t noisecount;
+};
+
+IDENTITY KlondikeID;
+
+static double cvtKlnToC(uint8_t temp)
+{
+	double Rt, stein, celsius;
+
+	if (temp == 0)
+		return 0.0;
+
+	Rt = 1000.0 * 255.0 / (double)temp - 1000.0;
+
+	stein = log(Rt / 2200.0) / 3987.0;
+
+	stein += 1.0 / (double)(25.0 + 273.15);
+
+	celsius = (1.0 / stein) - 273.15;
+
+	// For display of bad data
+	if (celsius < 0.0)
+		celsius = 0.0;
+	if (celsius > 200.0)
+		celsius = 200.0;
+
+	return celsius;
+}
+
+static int cvtCToKln(double deg)
+{
+	double Rt, stein, temp;
+
+	if (deg < 0.0)
+		deg = 0.0;
+
+	stein = 1.0 / (deg + 273.15);
+
+	stein -= 1.0 / (double)(25.0 + 273.15);
+
+	Rt = exp(stein * 3987.0) * 2200.0;
+
+	if (Rt == -1000.0)
+		Rt++;
+
+	temp = 1000.0 * 256.0 / (Rt + 1000.0);
+
+	if (temp > 255)
+		temp = 255;
+	if (temp < 0)
+		temp = 0;
+
+	return (int)temp;
+}
+
+static char *SendCmdGetReply(struct cgpu_info *klncgpu, char Cmd, int device, int datalen, void *data)
+{
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	char outbuf[64];
+	int retries = CMD_REPLY_RETRIES;
+	int chkreply = klninfo->nextreply;
+	int sent, err;
+
+	if (klncgpu->usbinfo.nodev)
+		return NULL;
+
+	outbuf[0] = Cmd;
+	outbuf[1] = device;
+	memcpy(outbuf+2, data, datalen);
+	err = usb_write(klncgpu, outbuf, 2+datalen, &sent, C_REQUESTRESULTS);
+	if (err < 0 || sent != 2+datalen) {
+		applog(LOG_ERR, "%s (%s) Cmd:%c Dev:%d, write failed (%d:%d)", klncgpu->drv->dname, klncgpu->device_path, Cmd, device, sent, err);
+	}
+	while (retries-- > 0 && klninfo->shutdown == false) {
+		cgsleep_ms(REPLY_WAIT_TIME);
+		while (*(klninfo->replies + chkreply*REPLY_BUFSIZE) != Cmd || *(klninfo->replies + chkreply*REPLY_BUFSIZE + 2) != device) {
+			if (++chkreply == MAX_REPLY_COUNT)
+				chkreply = 0;
+			if (chkreply == klninfo->nextreply)
+				break;
+		}
+		if (chkreply == klninfo->nextreply)
+			continue;
+		*(klninfo->replies + chkreply*REPLY_BUFSIZE) = '!';  // mark to prevent re-use
+		return klninfo->replies + chkreply*REPLY_BUFSIZE + 1;
+	}
+	return NULL;
+}
+
+static bool klondike_get_stats(struct cgpu_info *klncgpu)
+{
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	int slaves, dev;
+
+	if (klncgpu->usbinfo.nodev || klninfo->status == NULL)
+		return false;
+
+	applog(LOG_DEBUG, "Klondike getting status");
+	slaves = klninfo->status[0].slavecount;
+
+	// loop thru devices and get status for each
+	wr_lock(&(klninfo->stat_lock));
+	for (dev = 0; dev <= slaves; dev++) {
+		char *reply = SendCmdGetReply(klncgpu, 'S', dev, 0, NULL);
+		if (reply != NULL)
+			memcpy((void *)(&(klninfo->status[dev])), reply+2, sizeof(klninfo->status[dev]));
+	}
+	wr_unlock(&(klninfo->stat_lock));
+
+	// todo: detect slavecount change and realloc space
+
+	return true;
+}
+
+static bool klondike_init(struct cgpu_info *klncgpu)
+{
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	int slaves, dev;
+
+	char *reply = SendCmdGetReply(klncgpu, 'S', 0, 0, NULL);
+	if (reply == NULL)
+		return false;
+
+	slaves = ((WORKSTATUS *)(reply+2))->slavecount;
+	if (klninfo->status == NULL) {
+		applog(LOG_DEBUG, "Klondike initializing data");
+
+		// alloc space for status, devinfo and cfg for master and slaves
+		klninfo->status = calloc(slaves+1, sizeof(WORKSTATUS));
+		if (unlikely(!klninfo->status))
+			quit(1, "Failed to calloc status array in klondke_get_stats");
+		klninfo->devinfo = calloc(slaves+1, sizeof(DEVINFO));
+		if (unlikely(!klninfo->devinfo))
+			quit(1, "Failed to calloc devinfo array in klondke_get_stats");
+		klninfo->cfg = calloc(slaves+1, sizeof(WORKCFG));
+		if (unlikely(!klninfo->cfg))
+			quit(1, "Failed to calloc cfg array in klondke_get_stats");
+	}
+
+	WORKCFG cfgset = { 0,0,0,0,0 }; // zero init triggers read back only
+	double temp1, temp2;
+	int size = 2;
+
+	if (opt_klondike_options != NULL) {  // boundaries are checked by device, with valid values returned
+		sscanf(opt_klondike_options, "%hu:%lf:%lf:%hhu", &cfgset.hashclock, &temp1, &temp2, &cfgset.fantarget);
+		cfgset.temptarget = cvtCToKln(temp1);
+		cfgset.tempcritical = cvtCToKln(temp2);
+		cfgset.fantarget = (int)255*cfgset.fantarget/100;
+		size = sizeof(cfgset);
+	}
+
+	for (dev = 0; dev <= slaves; dev++) {
+		char *reply = SendCmdGetReply(klncgpu, 'C', dev, size, &cfgset);
+		if (reply != NULL) {
+			klninfo->cfg[dev] = *(WORKCFG *)(reply+2);
+			applog(LOG_NOTICE, "Klondike config (%d: Clk: %d, T:%.0lf, C:%.0lf, F:%d)",
+				dev, klninfo->cfg[dev].hashclock,
+				cvtKlnToC(klninfo->cfg[dev].temptarget),
+				cvtKlnToC(klninfo->cfg[dev].tempcritical),
+				(int)100*klninfo->cfg[dev].fantarget/256);
+		}
+	}
+	klondike_get_stats(klncgpu);
+	for (dev = 0; dev <= slaves; dev++) {
+		klninfo->devinfo[dev].rangesize = ((uint64_t)1<<32) / klninfo->status[dev].chipcount;
+		klninfo->devinfo[dev].chipstats = calloc(klninfo->status[dev].chipcount*2 , sizeof(uint32_t));
+	}
+
+	SendCmdGetReply(klncgpu, 'E', 0, 1, "1");
+
+	return true;
+}
+
+static bool 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;
+
+	if (unlikely(!klncgpu))
+		quit(1, "Failed to calloc klncgpu in klondike_detect_one");
+
+	klninfo = calloc(1, sizeof(*klninfo));
+	if (unlikely(!klninfo))
+		quit(1, "Failed to calloc klninfo in klondke_detect_one");
+	klncgpu->device_data = (FILE *)klninfo;
+
+	klninfo->replies = calloc(MAX_REPLY_COUNT, REPLY_BUFSIZE);
+	if (unlikely(!klninfo->replies))
+		quit(1, "Failed to calloc replies buffer in klondke_detect_one");
+	klninfo->nextreply = 0;
+
+	if (usb_init(klncgpu, dev, found)) {
+		int attempts = 0;
+		while (attempts++ < 3) {
+			char devpath[20], reply[REPLY_SIZE];
+			int sent, recd, err;
+
+			sprintf(devpath, "%d:%d", (int)(klncgpu->usbinfo.bus_number), (int)(klncgpu->usbinfo.device_address));
+			err = usb_write(klncgpu, "I", 2, &sent, C_REQUESTRESULTS);
+			if (err < 0 || sent != 2) {
+				applog(LOG_ERR, "%s (%s) detect write failed (%d:%d)", klncgpu->drv->dname, devpath, sent, err);
+			}
+			cgsleep_ms(REPLY_WAIT_TIME*10);
+			err = usb_read(klncgpu, reply, REPLY_SIZE, &recd, C_GETRESULTS);
+			if (err < 0) {
+				applog(LOG_ERR, "%s (%s) detect read failed (%d:%d)", klncgpu->drv->dname, devpath, recd, err);
+			} else if (recd < 1) {
+				applog(LOG_ERR, "%s (%s) detect empty reply (%d)",	klncgpu->drv->dname, devpath, recd);
+			} else if (reply[0] == 'I' && reply[1] == 0) {
+
+				applog(LOG_DEBUG, "%s (%s) detect successful", klncgpu->drv->dname, devpath);
+				KlondikeID = *(IDENTITY *)(&reply[2]);
+				klncgpu->device_path = strdup(devpath);
+				update_usb_stats(klncgpu);
+				if (!add_cgpu(klncgpu))
+					break;
+				applog(LOG_DEBUG, "Klondike cgpu added");
+				return true;
+			}
+		}
+		usb_uninit(klncgpu);
+	}
+	free(klninfo->replies);
+	free(klncgpu);
+	return false;
+}
+
+static void klondike_detect(bool __maybe_unused hotplug)
+{
+	usb_detect(&klondike_drv, klondike_detect_one);
+}
+
+static void klondike_identify(__maybe_unused struct cgpu_info *klncgpu)
+{
+	//SendCmdGetReply(klncgpu, 'I', 0, 0, NULL);
+}
+
+static void klondike_check_nonce(struct cgpu_info *klncgpu, WORKRESULT *result)
+{
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	struct work *work, *tmp;
+
+	applog(LOG_DEBUG, "Klondike FOUND NONCE (%02x:%08x)", result->workid, result->nonce);
+
+	HASH_ITER(hh, klncgpu->queued_work, work, tmp) {
+		if (work->queued && (work->subid == (result->device*256 + result->workid))) {
+
+			wr_lock(&(klninfo->stat_lock));
+			klninfo->devinfo[result->device].noncecount++;
+			klninfo->noncecount++;
+			wr_unlock(&(klninfo->stat_lock));
+
+			result->nonce = le32toh(result->nonce - 0xC0);
+			applog(LOG_DEBUG, "Klondike SUBMIT NONCE (%02x:%08x)", result->workid, result->nonce);
+			bool ok = submit_nonce(klncgpu->thr[0], work, result->nonce);
+
+			applog(LOG_DEBUG, "Klondike chip stats %d, %08x, %d, %d", result->device, result->nonce, klninfo->devinfo[result->device].rangesize, klninfo->status[result->device].chipcount);
+			klninfo->devinfo[result->device].chipstats[(result->nonce / klninfo->devinfo[result->device].rangesize) + (ok ? 0 : klninfo->status[result->device].chipcount)]++;
+			return;
+		}
+	}
+
+	applog(LOG_ERR, "%s%i:%d unknown work (%02x:%08x) - ignored",
+		klncgpu->drv->name, klncgpu->device_id, result->device, result->workid, result->nonce);
+	//inc_hw_errors(klncgpu->thr[0]);
+}
+
+// Change this to LOG_WARNING if you wish to always see the replies
+#define READ_DEBUG LOG_DEBUG
+
+// thread to keep looking for replies
+static void *klondike_get_replies(void *userdata)
+{
+	struct cgpu_info *klncgpu = (struct cgpu_info *)userdata;
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	struct klondike_status *ks;
+	struct _workresult *wr;
+	struct klondike_cfg *kc;
+	struct klondike_id *ki;
+	char *replybuf;
+	int err, recd;
+
+	applog(LOG_DEBUG, "Klondike listening for replies");
+
+	while (klninfo->shutdown == false) {
+		if (klncgpu->usbinfo.nodev)
+			return NULL;
+
+		replybuf = klninfo->replies + klninfo->nextreply * REPLY_BUFSIZE;
+		replybuf[0] = 0;
+
+		err = usb_read(klncgpu, replybuf+1, REPLY_SIZE, &recd, C_GETRESULTS);
+		if (!err && recd == REPLY_SIZE) {
+			if (opt_log_level <= READ_DEBUG) {
+				char *hexdata = bin2hex((unsigned char *)(replybuf+1), recd);
+				applog(READ_DEBUG, "%s (%s) reply [%s:%s]", klncgpu->drv->dname, klncgpu->device_path, replybuf+1, hexdata);
+				free(hexdata);
+			}
+			if (++klninfo->nextreply == MAX_REPLY_COUNT)
+				klninfo->nextreply = 0;
+
+			replybuf[0] = replybuf[1];
+			switch (replybuf[0]) {
+				case '=':
+					wr = (struct _workresult *)(replybuf+1);
+					klondike_check_nonce(klncgpu, (WORKRESULT *)replybuf);
+					applog(READ_DEBUG,
+						"%s (%s) reply: work [%c] device=%d workid=%d"
+						" nonce=0x%08x",
+						klncgpu->drv->dname, klncgpu->device_path,
+						*(replybuf+1),
+						(int)(wr->device),
+						(int)(wr->workid),
+						(unsigned int)(wr->nonce));
+					break;
+				case 'S':
+				case 'W':
+				case 'A':
+				case 'E':
+					ks = (struct klondike_status *)(replybuf+1);
+					wr_lock(&(klninfo->stat_lock));
+					klninfo->errorcount += ks->errorcount;
+					klninfo->noisecount += ks->noise;
+					wr_unlock(&(klninfo->stat_lock));
+					applog(READ_DEBUG,
+						"%s (%s) reply: status [%c] chips=%d slaves=%d"
+						" workcq=%d workid=%d temp=%d fan=%d errors=%d"
+						" hashes=%d max=%d noise=%d",
+						klncgpu->drv->dname, klncgpu->device_path,
+						*(replybuf+1),
+						(int)(ks->chipcount),
+						(int)(ks->slavecount),
+						(int)(ks->workqc),
+						(int)(ks->workid),
+						(int)(ks->temp),
+						(int)(ks->fanspeed),
+						(int)(ks->errorcount),
+						(int)(ks->hashcount),
+						(int)(ks->maxcount),
+						(int)(ks->noise));
+					break;
+				case 'C':
+					kc = (struct klondike_cfg *)(replybuf+2);
+					applog(READ_DEBUG,
+						"%s (%s) reply: config [%c] clock=%d temptarget=%d"
+						" tempcrit=%d fan=%d",
+						klncgpu->drv->dname, klncgpu->device_path,
+						*(replybuf+1),
+						(int)(kc->hashclock),
+						(int)(kc->temptarget),
+						(int)(kc->tempcritical),
+						(int)(kc->fantarget));
+					break;
+				case 'I':
+					ki = (struct klondike_id *)(replybuf+2);
+					applog(READ_DEBUG,
+						"%s (%s) reply: info [%c] version=0x%02x prod=%.7s"
+						" serial=0x%08x",
+						klncgpu->drv->dname, klncgpu->device_path,
+						*(replybuf+1),
+						(int)(ki->version),
+						ki->product,
+						(unsigned int)(ki->serial));
+					break;
+				default:
+					break;
+			}
+		}
+	}
+	return NULL;
+}
+
+static void klondike_flush_work(struct cgpu_info *klncgpu)
+{
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	int dev;
+
+	applog(LOG_DEBUG, "Klondike flushing work");
+	for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
+		char *reply = SendCmdGetReply(klncgpu, 'A', dev, 0, NULL);
+		if (reply != NULL) {
+			wr_lock(&(klninfo->stat_lock));
+			klninfo->status[dev] = *(WORKSTATUS *)(reply+2);
+			wr_unlock(&(klninfo->stat_lock));
+		}
+	}
+}
+
+static bool klondike_thread_prepare(struct thr_info *thr)
+{
+	struct cgpu_info *klncgpu = thr->cgpu;
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+
+	if (thr_info_create(&(klninfo->replies_thr), NULL, klondike_get_replies, (void *)klncgpu)) {
+		applog(LOG_ERR, "%s%i: thread create failed", klncgpu->drv->name, klncgpu->device_id);
+		return false;
+	}
+	pthread_detach(klninfo->replies_thr.pth);
+
+	// let the listening get started
+	cgsleep_ms(100);
+
+	return klondike_init(klncgpu);
+}
+
+static bool klondike_thread_init(struct thr_info *thr)
+{
+	struct cgpu_info *klncgpu = thr->cgpu;
+
+	if (klncgpu->usbinfo.nodev)
+		return false;
+
+	klondike_flush_work(klncgpu);
+
+	return true;
+}
+
+static void klondike_shutdown(struct thr_info *thr)
+{
+	struct cgpu_info *klncgpu = thr->cgpu;
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	int dev;
+
+	applog(LOG_DEBUG, "Klondike shutting down work");
+	for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
+		SendCmdGetReply(klncgpu, 'E', dev, 1, "0");
+	}
+	klncgpu->shutdown = klninfo->shutdown = true;
+}
+
+static void klondike_thread_enable(struct thr_info *thr)
+{
+	struct cgpu_info *klncgpu = thr->cgpu;
+
+	if (klncgpu->usbinfo.nodev)
+		return;
+
+	//SendCmdGetReply(klncgpu, 'E', 0, 1, "0");
+
+}
+
+static bool klondike_send_work(struct cgpu_info *klncgpu, int dev, struct work *work)
+{
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	struct work *tmp;
+	WORKTASK data;
+
+	if (klncgpu->usbinfo.nodev)
+		return false;
+
+	memcpy(data.midstate, work->midstate, MIDSTATE_BYTES);
+	memcpy(data.merkle, work->data + MERKLE_OFFSET, MERKLE_BYTES);
+	data.workid = (uint8_t)(klninfo->devinfo[dev].nextworkid++ & 0xFF);
+	work->subid = dev*256 + data.workid;
+
+	if (opt_log_level <= LOG_DEBUG) {
+		char *hexdata = bin2hex(&data.workid, sizeof(data)-3);
+		applog(LOG_DEBUG, "WORKDATA: %s", hexdata);
+		free(hexdata);
+	}
+
+	applog(LOG_DEBUG, "Klondike sending work (%d:%02x)", dev, data.workid);
+	char *reply = SendCmdGetReply(klncgpu, 'W', dev, sizeof(data)-3, &data.workid);
+	if (reply != NULL) {
+		wr_lock(&(klninfo->stat_lock));
+		klninfo->status[dev] = *(WORKSTATUS *)(reply+2);
+		wr_unlock(&(klninfo->stat_lock));
+
+		// remove old work
+		HASH_ITER(hh, klncgpu->queued_work, work, tmp) {
+		if (work->queued && (work->subid == (int)(dev*256 + ((klninfo->devinfo[dev].nextworkid-2*MAX_WORK_COUNT) & 0xFF))))
+			work_completed(klncgpu, work);
+		}
+		return true;
+	}
+	return false;
+}
+
+static bool klondike_queue_full(struct cgpu_info *klncgpu)
+{
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	struct work *work = NULL;
+	int dev, queued;
+
+	for (queued = 0; queued < MAX_WORK_COUNT-1; queued++)
+		for (dev = 0; dev <= klninfo->status->slavecount; dev++)
+			if (klninfo->status[dev].workqc <= queued) {
+				if (!work)
+					work = get_queued(klncgpu);
+				if (unlikely(!work))
+					return false;
+				if (klondike_send_work(klncgpu, dev, work)) {
+					work = NULL;
+					break;
+				}
+			}
+
+	return true;
+}
+
+static int64_t klondike_scanwork(struct thr_info *thr)
+{
+	struct cgpu_info *klncgpu = thr->cgpu;
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	int64_t newhashcount = 0;
+	int dev;
+
+	if (klncgpu->usbinfo.nodev)
+		return -1;
+
+	restart_wait(thr, 200);
+	if (klninfo->status != NULL) {
+		rd_lock(&(klninfo->stat_lock));
+		for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
+			uint64_t newhashdev = 0;
+			if (klninfo->devinfo[dev].lasthashcount > klninfo->status[dev].hashcount) // todo: chg this to check workid for wrapped instead
+				newhashdev += klninfo->status[dev].maxcount; // hash counter wrapped
+			newhashdev += klninfo->status[dev].hashcount - klninfo->devinfo[dev].lasthashcount;
+			klninfo->devinfo[dev].lasthashcount = klninfo->status[dev].hashcount;
+			if (klninfo->status[dev].maxcount != 0)
+				klninfo->hashcount += (newhashdev << 32) / klninfo->status[dev].maxcount;
+
+			// todo: check stats for critical conditions
+		}
+		newhashcount += 0xffffffffull * (uint64_t)klninfo->noncecount;
+		klninfo->noncecount = 0;
+		rd_unlock(&(klninfo->stat_lock));
+	}
+	return newhashcount;
+}
+
+
+static void get_klondike_statline_before(char *buf, size_t siz, struct cgpu_info *klncgpu)
+{
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	uint8_t temp = 0xFF;
+	uint16_t fan = 0;
+	uint16_t clock = 0;
+	int dev;
+	char tmp[16];
+
+	if (klninfo->status == NULL)
+		return;
+
+	rd_lock(&(klninfo->stat_lock));
+	for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
+		if (klninfo->status[dev].temp < temp)
+			temp = klninfo->status[dev].temp;
+		fan += klninfo->cfg[dev].fantarget;
+		clock += klninfo->cfg[dev].hashclock;
+	}
+	fan /= klninfo->status->slavecount+1;
+	clock /= klninfo->status->slavecount+1;
+	rd_unlock(&(klninfo->stat_lock));
+
+	snprintf(tmp, sizeof(tmp), "%2.0fC", cvtKlnToC(temp));
+	if (strlen(tmp) < 4)
+		strcat(tmp, " ");
+
+	tailsprintf(buf, siz, "%3dMHz %3d%% %s| ", (int)clock, fan*100/255, tmp);
+}
+
+static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
+{
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	struct api_data *root = NULL;
+	char buf[32];
+	int dev;
+
+	if (klninfo->status == NULL)
+		return NULL;
+
+	rd_lock(&(klninfo->stat_lock));
+	for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
+
+		float fTemp = cvtKlnToC(klninfo->status[dev].temp);
+		sprintf(buf, "Temp %d", dev);
+		root = api_add_temp(root, buf, &fTemp, true);
+
+		double dClk = (double)klninfo->cfg[dev].hashclock;
+		sprintf(buf, "Clock %d", dev);
+		root = api_add_freq(root, buf, &dClk, true);
+
+		unsigned int iFan = (unsigned int)100 * klninfo->cfg[dev].fantarget / 255;
+		sprintf(buf, "Fan Percent %d", dev);
+		root = api_add_int(root, buf, (int *)(&iFan), true);
+
+		iFan = 0;
+		if (klninfo->status[dev].fanspeed > 0)
+			iFan = (unsigned int)TACH_FACTOR / klninfo->status[dev].fanspeed;
+		sprintf(buf, "Fan RPM %d", dev);
+		root = api_add_int(root, buf, (int *)(&iFan), true);
+
+		if (klninfo->devinfo[dev].chipstats != NULL) {
+			char data[2048];
+			char one[32];
+			int n;
+
+			sprintf(buf, "Nonces / Chip %d", dev);
+			data[0] = '\0';
+			for (n = 0; n < klninfo->status[dev].chipcount; n++) {
+				snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n]);
+				strcat(data, one);
+			}
+			root = api_add_string(root, buf, data, true);
+
+			sprintf(buf, "Errors / Chip %d", dev);
+			data[0] = '\0';
+			for (n = 0; n < klninfo->status[dev].chipcount; n++) {
+				snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n + klninfo->status[dev].chipcount]);
+				strcat(data, one);
+			}
+			root = api_add_string(root, buf, data, true);
+		}
+	}
+
+	root = api_add_uint64(root, "Hash Count", &(klninfo->hashcount), true);
+	root = api_add_uint64(root, "Error Count", &(klninfo->errorcount), true);
+	root = api_add_uint64(root, "Noise Count", &(klninfo->noisecount), true);
+
+	rd_unlock(&(klninfo->stat_lock));
+
+	return root;
+}
+
+struct device_drv klondike_drv = {
+	.drv_id = DRIVER_klondike,
+	.dname = "Klondike",
+	.name = "KLN",
+	.drv_detect = klondike_detect,
+	.get_api_stats = klondike_api_stats,
+	.get_statline_before = get_klondike_statline_before,
+	.get_stats = klondike_get_stats,
+	.identify_device = klondike_identify,
+	.thread_prepare = klondike_thread_prepare,
+	.thread_init = klondike_thread_init,
+	.hash_work = hash_queued_work,
+	.scanwork = klondike_scanwork,
+	.queue_full = klondike_queue_full,
+	.flush_work = klondike_flush_work,
+	.thread_shutdown = klondike_shutdown,
+	.thread_enable = klondike_thread_enable
+};
diff --git a/driver-modminer.c b/driver-modminer.c
index 0672c99..44ee90c 100644
--- a/driver-modminer.c
+++ b/driver-modminer.c
@@ -206,7 +206,7 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic
 		tmp->modminer_mutex = modminer->modminer_mutex;
 
 		if (!add_cgpu(tmp)) {
-			tmp = usb_free_cgpu_devlock(tmp, !added);
+			tmp = usb_free_cgpu(tmp);
 			goto unshin;
 		}
 
@@ -215,7 +215,7 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic
 		added = true;
 	}
 
-	modminer = usb_free_cgpu_devlock(modminer, !added);
+	modminer = usb_free_cgpu(modminer);
 
 	return true;
 
@@ -229,7 +229,7 @@ shin:
 		modminer->modminer_mutex = NULL;
 	}
 
-	modminer = usb_free_cgpu_devlock(modminer, !added);
+	modminer = usb_free_cgpu(modminer);
 
 	if (added)
 		return true;
diff --git a/driver-ztex.c b/driver-ztex.c
deleted file mode 100644
index ee0c0a9..0000000
--- a/driver-ztex.c
+++ /dev/null
@@ -1,423 +0,0 @@
-/**
- *   ztex.c - cgminer worker for Ztex 1.15x fpga board
- *
- *   Copyright (c) 2012 nelisky.btc@gmail.com
- *
- *   This work is based upon the Java SDK provided by ztex which is
- *   Copyright (C) 2009-2011 ZTEX GmbH.
- *   http://www.ztex.de
- *
- *   This work is based upon the icarus.c worker which was
- *   Copyright 2012 Luke Dashjr
- *   Copyright 2012 Xiangfu <xiangfu@openmobilefree.com>
- *
- *   This program is free software; you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License version 2 as
- *   published by the Free Software Foundation.
- *
- *   This program is distributed in the hope that it will be useful, but
- *   WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- *   General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program; if not, see http://www.gnu.org/licenses/.
-**/
-#include "miner.h"
-#include <unistd.h>
-#include <sha2.h>
-#include "libztex.h"
-#include "util.h"
-
-#define GOLDEN_BACKLOG 5
-
-// Forward declarations
-static void ztex_disable(struct thr_info* thr);
-static bool ztex_prepare(struct thr_info *thr);
-
-static void ztex_selectFpga(struct libztex_device* ztex)
-{
-	if (ztex->root->numberOfFpgas > 1) {
-		if (ztex->root->selectedFpga != ztex->fpgaNum)
-			mutex_lock(&ztex->root->mutex);
-		libztex_selectFpga(ztex);
-	}
-}
-
-static void ztex_releaseFpga(struct libztex_device* ztex)
-{
-	if (ztex->root->numberOfFpgas > 1) {
-		ztex->root->selectedFpga = -1;
-		mutex_unlock(&ztex->root->mutex);
-	}
-}
-
-static void ztex_detect(bool __maybe_unused hotplug)
-{
-	int cnt;
-	int i,j;
-	int fpgacount;
-	struct libztex_dev_list **ztex_devices;
-	struct libztex_device *ztex_slave;
-	struct cgpu_info *ztex;
-
-	cnt = libztex_scanDevices(&ztex_devices);
-	if (cnt > 0)
-		applog(LOG_WARNING, "Found %d ztex board%s", cnt, cnt > 1 ? "s" : "");
-
-	for (i = 0; i < cnt; i++) {
-		ztex = calloc(1, sizeof(struct cgpu_info));
-		ztex->drv = &ztex_drv;
-		ztex->device_ztex = ztex_devices[i]->dev;
-		ztex->threads = 1;
-		ztex->device_ztex->fpgaNum = 0;
-		ztex->device_ztex->root = ztex->device_ztex;
-		add_cgpu(ztex);
-
-		fpgacount = libztex_numberOfFpgas(ztex->device_ztex);
-
-		if (fpgacount > 1)
-			pthread_mutex_init(&ztex->device_ztex->mutex, NULL);
-
-		for (j = 1; j < fpgacount; j++) {
-			ztex = calloc(1, sizeof(struct cgpu_info));
-			ztex->drv = &ztex_drv;
-			ztex_slave = calloc(1, sizeof(struct libztex_device));
-			memcpy(ztex_slave, ztex_devices[i]->dev, sizeof(struct libztex_device));
-			ztex->device_ztex = ztex_slave;
-			ztex->threads = 1;
-			ztex_slave->fpgaNum = j;
-			ztex_slave->root = ztex_devices[i]->dev;
-			ztex_slave->repr[strlen(ztex_slave->repr) - 1] = ('1' + j);
-			add_cgpu(ztex);
-		}
-
-		applog(LOG_WARNING,"%s: Found Ztex (fpga count = %d) , mark as %d", ztex->device_ztex->repr, fpgacount, ztex->device_id);
-	}
-
-	if (cnt > 0)
-		libztex_freeDevList(ztex_devices);
-}
-
-static bool ztex_updateFreq(struct libztex_device* ztex)
-{
-	int i, maxM, bestM;
-	double bestR, r;
-
-	for (i = 0; i < ztex->freqMaxM; i++)
-		if (ztex->maxErrorRate[i + 1] * i < ztex->maxErrorRate[i] * (i + 20))
-			ztex->maxErrorRate[i + 1] = ztex->maxErrorRate[i] * (1.0 + 20.0 / i);
-
-	maxM = 0;
-	while (maxM < ztex->freqMDefault && ztex->maxErrorRate[maxM + 1] < LIBZTEX_MAXMAXERRORRATE)
-		maxM++;
-	while (maxM < ztex->freqMaxM && ztex->errorWeight[maxM] > 150 && ztex->maxErrorRate[maxM + 1] < LIBZTEX_MAXMAXERRORRATE)
-		maxM++;
-
-	bestM = 0;
-	bestR = 0;
-	for (i = 0; i <= maxM; i++) {
-		r = (i + 1 + (i == ztex->freqM? LIBZTEX_ERRORHYSTERESIS: 0)) * (1 - ztex->maxErrorRate[i]);
-		if (r > bestR) {
-			bestM = i;
-			bestR = r;
-		}
-	}
-
-	if (bestM != ztex->freqM) {
-		ztex_selectFpga(ztex);
-		libztex_setFreq(ztex, bestM);
-		ztex_releaseFpga(ztex);
-	}
-
-	maxM = ztex->freqMDefault;
-	while (maxM < ztex->freqMaxM && ztex->errorWeight[maxM + 1] > 100)
-		maxM++;
-	if ((bestM < (1.0 - LIBZTEX_OVERHEATTHRESHOLD) * maxM) && bestM < maxM - 1) {
-		ztex_selectFpga(ztex);
-		libztex_resetFpga(ztex);
-		ztex_releaseFpga(ztex);
-		applog(LOG_ERR, "%s: frequency drop of %.1f%% detect. This may be caused by overheating. FPGA is shut down to prevent damage.",
-		       ztex->repr, (1.0 - 1.0 * bestM / maxM) * 100);
-		return false;
-	}
-	return true;
-}
-
-
-static uint32_t ztex_checkNonce(struct work *work, uint32_t nonce)
-{
-	uint32_t *data32 = (uint32_t *)(work->data);
-	unsigned char swap[80];
-	uint32_t *swap32 = (uint32_t *)swap;
-	unsigned char hash1[32];
-	unsigned char hash2[32];
-	uint32_t *hash2_32 = (uint32_t *)hash2;
-	int i;
-
-	swap32[76/4] = htonl(nonce);
-
-	for (i = 0; i < 76 / 4; i++)
-		swap32[i] = swab32(data32[i]);
-
-	sha256(swap, 80, hash1);
-	sha256(hash1, 32, hash2);
-
-	return htonl(hash2_32[7]);
-}
-
-static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
-                              __maybe_unused int64_t max_nonce)
-{
-	struct libztex_device *ztex;
-	unsigned char sendbuf[44];
-	int i, j, k;
-	uint32_t *backlog;
-	int backlog_p = 0, backlog_max;
-	uint32_t *lastnonce;
-	uint32_t nonce, noncecnt = 0;
-	bool overflow, found;
-	struct libztex_hash_data hdata[GOLDEN_BACKLOG];
-
-	if (thr->cgpu->deven == DEV_DISABLED)
-		return -1;
-
-	ztex = thr->cgpu->device_ztex;
-
-	memcpy(sendbuf, work->data + 64, 12);
-	memcpy(sendbuf + 12, work->midstate, 32);
-
-	ztex_selectFpga(ztex);
-	i = libztex_sendHashData(ztex, sendbuf);
-	if (i < 0) {
-		// Something wrong happened in send
-		applog(LOG_ERR, "%s: Failed to send hash data with err %d, retrying", ztex->repr, i);
-		cgsleep_ms(500);
-		i = libztex_sendHashData(ztex, sendbuf);
-		if (i < 0) {
-			// And there's nothing we can do about it
-			ztex_disable(thr);
-			applog(LOG_ERR, "%s: Failed to send hash data with err %d, giving up", ztex->repr, i);
-			ztex_releaseFpga(ztex);
-			return -1;
-		}
-	}
-	ztex_releaseFpga(ztex);
-
-	applog(LOG_DEBUG, "%s: sent hashdata", ztex->repr);
-
-	lastnonce = calloc(1, sizeof(uint32_t)*ztex->numNonces);
-	if (lastnonce == NULL) {
-		applog(LOG_ERR, "%s: failed to allocate lastnonce[%d]", ztex->repr, ztex->numNonces);
-		return -1;
-	}
-
-	/* Add an extra slot for detecting dupes that lie around */
-	backlog_max = ztex->numNonces * (2 + ztex->extraSolutions);
-	backlog = calloc(1, sizeof(uint32_t) * backlog_max);
-	if (backlog == NULL) {
-		applog(LOG_ERR, "%s: failed to allocate backlog[%d]", ztex->repr, backlog_max);
-		return -1;
-	}
-
-	overflow = false;
-	int count = 0;
-	int validNonces = 0;
-	double errorCount = 0;
-
-	applog(LOG_DEBUG, "%s: entering poll loop", ztex->repr);
-	while (!(overflow || thr->work_restart)) {
-		count++;
-
-		int sleepcount = 0;
-		while (thr->work_restart == 0 && sleepcount < 25) {
-			cgsleep_ms(10);
-			sleepcount += 1;
-		}
-
-		if (thr->work_restart) {
-			applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
-			break;
-		}
-
-		ztex_selectFpga(ztex);
-		i = libztex_readHashData(ztex, &hdata[0]);
-		if (i < 0) {
-			// Something wrong happened in read
-			applog(LOG_ERR, "%s: Failed to read hash data with err %d, retrying", ztex->repr, i);
-			cgsleep_ms(500);
-			i = libztex_readHashData(ztex, &hdata[0]);
-			if (i < 0) {
-				// And there's nothing we can do about it
-				ztex_disable(thr);
-				applog(LOG_ERR, "%s: Failed to read hash data with err %d, giving up", ztex->repr, i);
-				free(lastnonce);
-				free(backlog);
-				ztex_releaseFpga(ztex);
-				return -1;
-			}
-		}
-		ztex_releaseFpga(ztex);
-
-		if (thr->work_restart) {
-			applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
-			break;
-		}
-
-		ztex->errorCount[ztex->freqM] *= 0.995;
-		ztex->errorWeight[ztex->freqM] = ztex->errorWeight[ztex->freqM] * 0.995 + 1.0;
-
-		for (i = 0; i < ztex->numNonces; i++) {
-			nonce = hdata[i].nonce;
-			if (nonce > noncecnt)
-				noncecnt = nonce;
-			if (((0xffffffff - nonce) < (nonce - lastnonce[i])) || nonce < lastnonce[i]) {
-				applog(LOG_DEBUG, "%s: overflow nonce=%08x lastnonce=%08x", ztex->repr, nonce, lastnonce[i]);
-				overflow = true;
-			} else
-				lastnonce[i] = nonce;
-
-			if (ztex_checkNonce(work, nonce) != (hdata->hash7 + 0x5be0cd19)) {
-				applog(LOG_DEBUG, "%s: checkNonce failed for %08X", ztex->repr, nonce);
-
-				// do not count errors in the first 500ms after sendHashData (2x250 wait time)
-				if (count > 2) {
-					thr->cgpu->hw_errors++;
-					errorCount += (1.0 / ztex->numNonces);
-				}
-			}
-			else
-				validNonces++;
-
-
-			for (j=0; j<=ztex->extraSolutions; j++) {
-				nonce = hdata[i].goldenNonce[j];
-
-				if (nonce == ztex->offsNonces) {
-					continue;
-				}
-
-				// precheck the extraSolutions since they often fail
-				if (j > 0 && ztex_checkNonce(work, nonce) != 0) {
-					continue;
-				}
-
-				found = false;
-				for (k = 0; k < backlog_max; k++) {
-					if (backlog[k] == nonce) {
-						found = true;
-						break;
-					}
-				}
-				if (!found) {
-					applog(LOG_DEBUG, "%s: Share found N%dE%d", ztex->repr, i, j);
-					backlog[backlog_p++] = nonce;
-
-					if (backlog_p >= backlog_max)
-						backlog_p = 0;
-
-					work->blk.nonce = 0xffffffff;
-					submit_nonce(thr, work, nonce);
-					applog(LOG_DEBUG, "%s: submitted %08x", ztex->repr, nonce);
-				}
-			}
-		}
-	}
-
-	// only add the errorCount if we had at least some valid nonces or
-	// had no valid nonces in the last round
-	if (errorCount > 0.0) {
-		if (ztex->nonceCheckValid > 0 && validNonces == 0) {
-			applog(LOG_ERR, "%s: resetting %.1f errors", ztex->repr, errorCount);
-		}
-		else {
-			ztex->errorCount[ztex->freqM] += errorCount;
-		}
-	}
-
-	// remember the number of valid nonces for the check in the next round
-	ztex->nonceCheckValid = validNonces;
-
-	ztex->errorRate[ztex->freqM] = ztex->errorCount[ztex->freqM] /	ztex->errorWeight[ztex->freqM] * (ztex->errorWeight[ztex->freqM] < 100? ztex->errorWeight[ztex->freqM] * 0.01: 1.0);
-	if (ztex->errorRate[ztex->freqM] > ztex->maxErrorRate[ztex->freqM])
-		ztex->maxErrorRate[ztex->freqM] = ztex->errorRate[ztex->freqM];
-
-	if (!ztex_updateFreq(ztex)) {
-		// Something really serious happened, so mark this thread as dead!
-		free(lastnonce);
-		free(backlog);
-		
-		return -1;
-	}
-
-	applog(LOG_DEBUG, "%s: exit %1.8X", ztex->repr, noncecnt);
-
-	work->blk.nonce = 0xffffffff;
-
-	free(lastnonce);
-	free(backlog);
-
-	return noncecnt;
-}
-
-static void ztex_statline_before(char *buf, size_t bufsiz, struct cgpu_info *cgpu)
-{
-	if (cgpu->deven == DEV_ENABLED) {
-		tailsprintf(buf, bufsiz, "%s-%d | ", cgpu->device_ztex->snString, cgpu->device_ztex->fpgaNum+1);
-		tailsprintf(buf, bufsiz, "%0.1fMHz | ", cgpu->device_ztex->freqM1 * (cgpu->device_ztex->freqM + 1));
-	}
-}
-
-static bool ztex_prepare(struct thr_info *thr)
-{
-	struct cgpu_info *cgpu = thr->cgpu;
-	struct libztex_device *ztex = cgpu->device_ztex;
-
-	ztex_selectFpga(ztex);
-	if (libztex_configureFpga(ztex) != 0) {
-		libztex_resetFpga(ztex);
-		ztex_releaseFpga(ztex);
-		applog(LOG_ERR, "%s: Disabling!", thr->cgpu->device_ztex->repr);
-		thr->cgpu->deven = DEV_DISABLED;
-		return true;
-	}
-	ztex->freqM = ztex->freqMaxM+1;;
-	//ztex_updateFreq(ztex);
-	libztex_setFreq(ztex, ztex->freqMDefault);
-	ztex_releaseFpga(ztex);
-	applog(LOG_DEBUG, "%s: prepare", ztex->repr);
-	return true;
-}
-
-static void ztex_shutdown(struct thr_info *thr)
-{
-	if (thr->cgpu->device_ztex != NULL) {
-		if (thr->cgpu->device_ztex->fpgaNum == 0)
-			pthread_mutex_destroy(&thr->cgpu->device_ztex->mutex);  
-		applog(LOG_DEBUG, "%s: shutdown", thr->cgpu->device_ztex->repr);
-		libztex_destroy_device(thr->cgpu->device_ztex);
-		thr->cgpu->device_ztex = NULL;
-	}
-}
-
-static void ztex_disable(struct thr_info *thr)
-{
-	struct cgpu_info *cgpu;
-
-	applog(LOG_ERR, "%s: Disabling!", thr->cgpu->device_ztex->repr);
-	cgpu = get_devices(thr->cgpu->device_id);
-	cgpu->deven = DEV_DISABLED;
-	ztex_shutdown(thr);
-}
-
-struct device_drv ztex_drv = {
-	.drv_id = DRIVER_ztex,
-	.dname = "ztex",
-	.name = "ZTX",
-	.drv_detect = ztex_detect,
-	.get_statline_before = ztex_statline_before,
-	.thread_prepare = ztex_prepare,
-	.scanhash = ztex_scanhash,
-	.thread_shutdown = ztex_shutdown,
-};
-
diff --git a/libztex.c b/libztex.c
deleted file mode 100644
index 860a842..0000000
--- a/libztex.c
+++ /dev/null
@@ -1,915 +0,0 @@
-/**
- *   libztex.c - Ztex 1.15x/1.15y fpga board support library
- *
- *   Copyright (c) 2012 nelisky.btc@gmail.com
- *   Copyright (c) 2012 Denis Ahrens <denis@h3q.com>
- *   Copyright (c) 2012 Peter Stuge <peter@stuge.se>
- *
- *   This work is based upon the Java SDK provided by ztex which is
- *   Copyright (C) 2009-2011 ZTEX GmbH.
- *   http://www.ztex.de
- *
- *   This program is free software; you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License version 2 as
- *   published by the Free Software Foundation.
- *
- *   This program is distributed in the hope that it will be useful, but
- *   WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- *   General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program; if not, see http://www.gnu.org/licenses/.
-**/
-
-#include "config.h"
-
-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
-
-#include "miner.h"
-#include "fpgautils.h"
-#include "libztex.h"
-
-//* Capability index for EEPROM support.
-#define CAPABILITY_EEPROM 0,0
-//* Capability index for FPGA configuration support. 
-#define CAPABILITY_FPGA 0,1
-//* Capability index for FLASH memory support.
-#define CAPABILITY_FLASH 0,2
-//* Capability index for DEBUG helper support.
-#define CAPABILITY_DEBUG 0,3
-//* Capability index for AVR XMEGA support.
-#define CAPABILITY_XMEGA 0,4
-//* Capability index for AVR XMEGA support.
-#define CAPABILITY_HS_FPGA 0,5
-//* Capability index for AVR XMEGA support.
-#define CAPABILITY_MAC_EEPROM 0,6
-//* Capability index for multi FPGA support.
-#define CAPABILITY_MULTI_FPGA 0,7
-
-static int libztex_get_string_descriptor_ascii(libusb_device_handle *dev, uint8_t desc_index,
-		unsigned char *data, int length)
-{
-	int i, cnt;
-	uint16_t langid;
-	unsigned char buf[260];
-
-	/* We open code string descriptor retrieval and ASCII decoding here
-	 * in order to work around that libusb_get_string_descriptor_ascii()
-	 * in the FreeBSD libusb implementation hits a bug in ZTEX firmware,
-	 * where the device returns more bytes than requested, causing babble,
-	 * which makes FreeBSD return an error to us.
-	 *
-	 * Avoid the mess by doing it manually the same way as libusb-1.0.
-	 */
-
-	cnt = libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
-	    LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | 0,
-	    0x0000, buf, sizeof(buf), 1000);
-	if (cnt < 0) {
-		applog(LOG_ERR, "%s: Failed to read LANGIDs: %d", __func__, cnt);
-		return cnt;
-	}
-
-	langid = libusb_le16_to_cpu(((uint16_t *)buf)[1]);
-
-	cnt = libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
-	    LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | desc_index,
-	    langid, buf, sizeof(buf), 1000);
-	if (cnt < 0) {
-		applog(LOG_ERR, "%s: Failed to read string descriptor: %d", __func__, cnt);
-		return cnt;
-	}
-
-	/* num chars = (all bytes except bLength and bDescriptorType) / 2 */
-	for (i = 0; i <= (cnt - 2) / 2 && i < length-1; i++)
-		data[i] = buf[2 + i*2];
-
-	data[i] = 0;
-
-	return LIBUSB_SUCCESS;
-}
-
-enum check_result
-{
-	CHECK_ERROR,
-	CHECK_IS_NOT_ZTEX,
-	CHECK_OK,
-	CHECK_RESCAN,
-};
-
-static bool libztex_firmwareReset(struct libusb_device_handle *hndl, bool enable)
-{
-	uint8_t reset = enable;
-	int cnt = libusb_control_transfer(hndl, 0x40, 0xA0, 0xE600, 0, &reset, 1, 1000);
-	if (cnt < 0)
-	{
-		applog(LOG_ERR, "Ztex reset %d failed: %d", enable, cnt);
-		return 1;
-	}
-
-	return 0;
-}
-
-static enum check_result libztex_checkDevice(struct libusb_device *dev)
-{
-	FILE *fp = NULL;
-	libusb_device_handle *hndl = NULL;
-	struct libusb_device_descriptor desc;
-	int ret = CHECK_ERROR, err, cnt;
-	size_t got_bytes, length;
-	unsigned char buf[64], *fw_buf;
-	unsigned int i;
-
-	err = libusb_get_device_descriptor(dev, &desc);
-	if (unlikely(err != 0)) {
-		applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
-		return CHECK_ERROR;
-	}
-
-	if (desc.idVendor != LIBZTEX_IDVENDOR || desc.idProduct != LIBZTEX_IDPRODUCT) {
-		applog(LOG_DEBUG, "Not a ZTEX device %04x:%04x", desc.idVendor, desc.idProduct);
-		return CHECK_IS_NOT_ZTEX;
-	}
-
-	err = libusb_open(dev, &hndl);
-	if (err != LIBUSB_SUCCESS) {
-		applog(LOG_ERR, "%s: Can not open ZTEX device: %d", __func__, err);
-		goto done;
-	}
-
-	cnt = libusb_control_transfer(hndl, 0xc0, 0x22, 0, 0, buf, 40, 500);
-	if (unlikely(cnt < 0)) {
-		applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
-		goto done;
-	}
-
-	if (buf[0] != 40 || buf[1] != 1 || buf[2] != 'Z' || buf[3] != 'T' || buf[4] != 'E' || buf[5] != 'X') {
-		applog(LOG_ERR, "Ztex check device: Error reading ztex descriptor");
-		goto done;
-	}
-
-	if (buf[6] != 10)
-	{
-		ret = CHECK_IS_NOT_ZTEX;
-		goto done;
-	}
-
-	// 15 = 1.15y   13 = 1.15d or 1.15x
-	switch(buf[7])
-	{
-		case 13:
-			applog(LOG_ERR, "Found ztex board 1.15d or 1.15x");
-			break;
-		case 15:
-			applog(LOG_ERR, "Found ztex board 1.15y");
-			break;
-		default:
-			applog(LOG_ERR, "Found unknown ztex board");
-			ret = CHECK_IS_NOT_ZTEX;
-			goto done;
-	}
-
-	// testing for dummy firmware
-	if (buf[8] != 0) {
-		ret = CHECK_OK;
-		goto done;
-	}
-
-	applog(LOG_ERR, "Found dummy firmware, trying to send mining firmware");
-
-	char productString[32];
-
-	cnt = libztex_get_string_descriptor_ascii(hndl, desc.iProduct, (unsigned char*)productString, sizeof(productString));
-	if (unlikely(cnt < 0)) {
-		applog(LOG_ERR, "Ztex check device: Failed to read device productString with err %d", cnt);
-		return cnt;
-	}
-
-	applog(LOG_ERR, "productString: %s", productString);
-
-	unsigned char productID2 = buf[7];
-	char *firmware = NULL;
-
-	if (strcmp("USB-FPGA Module 1.15d (default)", productString) == 0 && productID2 == 13)
-	{
-		firmware = "ztex_ufm1_15d4.bin";
-	}
-	else if (strcmp("USB-FPGA Module 1.15x (default)", productString) == 0 && productID2 == 13)
-	{
-		firmware = "ztex_ufm1_15d4.bin";
-	}
-	else if (strcmp("USB-FPGA Module 1.15y (default)", productString) == 0 && productID2 == 15)
-	{
-		firmware = "ztex_ufm1_15y1.bin";
-	}
-
-	if (firmware == NULL)
-	{
-		applog(LOG_ERR, "could not figure out which firmware to use");
-		goto done;
-	}
-
-	applog(LOG_ERR, "Mining firmware filename: %s", firmware);
-
-	fp = open_bitstream("ztex", firmware);
-	if (!fp) {
-		applog(LOG_ERR, "failed to open firmware file '%s'", firmware);
-		goto done;
-	}
-
-	if (0 != fseek(fp, 0, SEEK_END)) {
-		applog(LOG_ERR, "Ztex firmware fseek: %s", strerror(errno));
-		goto done;
-	}
-
-	length = ftell(fp);
-	rewind(fp);
-	fw_buf = malloc(length);
-	if (!fw_buf) {
-		applog(LOG_ERR, "%s: Can not allocate memory: %s", __func__, strerror(errno));
-		goto done;
-	}
-
-	got_bytes = fread(fw_buf, 1, length, fp);
-	fclose(fp);
-	fp = NULL;
-
-	if (got_bytes < length) {
-		applog(LOG_ERR, "%s: Incomplete firmware read: %d/%d", __func__, (int)got_bytes, (int)length);
-		goto done;
-	}
-
-	// in buf[] is still the identifier of the dummy firmware
-	// use it to compare it with the new firmware
-	char *rv = memmem(fw_buf, got_bytes, buf, 8);
-	if (rv == NULL)
-	{
-		applog(LOG_ERR, "%s: found firmware is not ZTEX", __func__);
-		goto done;
-	}
-
-	// check for dummy firmware
-	if (rv[8] == 0)
-	{
-		applog(LOG_ERR, "%s: found a ZTEX dummy firmware", __func__);
-		goto done;
-	}
-
-	if (libztex_firmwareReset(hndl, true))
-		goto done;
-
-	for (i = 0; i < length; i+= 256) {
-		// firmware wants data in small chunks like 256 bytes
-		int numbytes = (length - i) < 256 ? (length - i) : 256;
-		int k = libusb_control_transfer(hndl, 0x40, 0xA0, i, 0, fw_buf + i, numbytes, 1000);
-		if (k < numbytes)
-		{
-			applog(LOG_ERR, "Ztex device: Failed to write firmware at %d with err: %d", i, k);
-			goto done;
-		}
-	}
-
-	if (libztex_firmwareReset(hndl, false))
-		goto done;
-
-	applog(LOG_ERR, "Ztex device: succesfully wrote firmware");
-	ret = CHECK_RESCAN;
-
-done:
-	if (fp)
-		fclose(fp);
-	if (hndl)
-		libusb_close(hndl);
-	return ret;
-}
-
-static bool libztex_checkCapability(struct libztex_device *ztex, int i, int j)
-{
-	if (!((i >= 0) && (i <= 5) && (j >= 0) && (j < 8) &&
-	     (((ztex->interfaceCapabilities[i] & 255) & (1 << j)) != 0))) {
-		applog(LOG_ERR, "%s: capability missing: %d %d", ztex->repr, i, j);
-		return false;
-	}
-	return true;
-}
-
-static char libztex_detectBitstreamBitOrder(const unsigned char *buf, int size)
-{
-	int i;
-
-	for (i = 0; i < size - 4; i++) {
-		if (((buf[i] & 255) == 0xaa) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0x55) && ((buf[i + 3] & 255) == 0x66))
-			return 1;
-		if (((buf[i] & 255) == 0x55) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0xaa) && ((buf[i + 3] & 255) == 0x66))
-			return 0;
-	} 
-	applog(LOG_WARNING, "Unable to determine bitstream bit order: no signature found");
-	return 0;
-}
-
-static void libztex_swapBits(unsigned char *buf, int size)
-{
-	unsigned char c;
-	int i;
-
-	for (i = 0; i < size; i++) {
-		c = buf[i];
-		buf[i] = ((c & 128) >> 7) |
-		         ((c & 64) >> 5) |
-		         ((c & 32) >> 3) |
-		         ((c & 16) >> 1) |
-		         ((c & 8) << 1) |
-		         ((c & 4) << 3) |
-		         ((c & 2) << 5) |
-		         ((c & 1) << 7);
-	}
-}
-
-static int libztex_getFpgaState(struct libztex_device *ztex, struct libztex_fpgastate *state)
-{
-	unsigned char buf[9];
-	int cnt;
-
-	if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
-		return -1;
-	cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x30, 0, 0, buf, 9, 1000);
-	if (unlikely(cnt < 0)) {
-		applog(LOG_ERR, "%s: Failed getFpgaState with err %d", ztex->repr, cnt);
-		return cnt;
-	}
-	state->fpgaConfigured = (buf[0] == 0);
-	state->fpgaChecksum = buf[1] & 0xff;
-	state->fpgaBytes = ((buf[5] & 0xff) << 24) | ((buf[4] & 0xff) << 16) | ((buf[3] & 0xff) << 8) | (buf[2] & 0xff);
-	state->fpgaInitB = buf[6] & 0xff;
-	state->fpgaFlashResult = buf[7];
-	state->fpgaFlashBitSwap = (buf[8] != 0);
-	return 0;
-}
-
-static int libztex_configureFpgaHS(struct libztex_device *ztex, const char* firmware, bool force, char bs)
-{
-	struct libztex_fpgastate state;
-	const int transactionBytes = 65536;
-	unsigned char buf[transactionBytes], settings[2];
-	int tries, cnt, err;
-	FILE *fp;
-
-	if (!libztex_checkCapability(ztex, CAPABILITY_HS_FPGA))
-		return -1;
-	libztex_getFpgaState(ztex, &state);
-	if (!force && state.fpgaConfigured) {
-		applog(LOG_INFO, "Bitstream already configured");
-		return 0;
-	}
-	cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x33, 0, 0, settings, 2, 1000);
-	if (unlikely(cnt < 0)) {
-		applog(LOG_ERR, "%s: Failed getHSFpgaSettings with err %d", ztex->repr, cnt);
-		return cnt;
-	}
-
-	err = libusb_claim_interface(ztex->hndl, settings[1]);
-	if (err != LIBUSB_SUCCESS) {
-		applog(LOG_ERR, "%s: failed to claim interface for hs transfer", ztex->repr);
-		return -4;
-	}
-
-	for (tries = 3; tries > 0; tries--) {
-		fp = open_bitstream("ztex", firmware);
-		if (!fp) {
-			applog(LOG_ERR, "%s: failed to read bitstream '%s'", ztex->repr, firmware);
-			libusb_release_interface(ztex->hndl, settings[1]);
-			return -2;
-		}
-
-		libusb_control_transfer(ztex->hndl, 0x40, 0x34, 0, 0, NULL, 0, 1000);
-		// 0x34 - initHSFPGAConfiguration
-
-		do
-		{
-			int length = fread(buf,1,transactionBytes,fp);
-
-			if (bs != 0 && bs != 1)
-				bs = libztex_detectBitstreamBitOrder(buf, length);
-			if (bs == 1)
-				libztex_swapBits(buf, length);
-
-			err = libusb_bulk_transfer(ztex->hndl, settings[0], buf, length, &cnt, 1000);
-			if (cnt != length)
-				applog(LOG_ERR, "%s: cnt != length", ztex->repr);
-			if (err != 0)
-				applog(LOG_ERR, "%s: Failed send hs fpga data", ztex->repr);
-		}
-		while (!feof(fp));
-
-		libusb_control_transfer(ztex->hndl, 0x40, 0x35, 0, 0, NULL, 0, 1000);
-		// 0x35 - finishHSFPGAConfiguration
-		if (cnt >= 0)
-			tries = 0;
-
-		fclose(fp);
-
-		libztex_getFpgaState(ztex, &state);
-		if (!state.fpgaConfigured) {
-			applog(LOG_ERR, "%s: HS FPGA configuration failed: DONE pin does not go high", ztex->repr);
-			libusb_release_interface(ztex->hndl, settings[1]);
-			return -3;
-		}
-	}
-
-	libusb_release_interface(ztex->hndl, settings[1]);
-
-	cgsleep_ms(200);
-	applog(LOG_INFO, "%s: HS FPGA configuration done", ztex->repr);
-	return 0;
-}
-
-static int libztex_configureFpgaLS(struct libztex_device *ztex, const char* firmware, bool force, char bs)
-{
-	struct libztex_fpgastate state;
-	const int transactionBytes = 2048;
-	unsigned char buf[transactionBytes];
-	int tries, cnt;
-	FILE *fp;
-
-	if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
-		return -1;
-
-	libztex_getFpgaState(ztex, &state);
-	if (!force && state.fpgaConfigured) {
-		applog(LOG_DEBUG, "Bitstream already configured");
-		return 0;
-	}
-
-	for (tries = 10; tries > 0; tries--) {
-		fp = open_bitstream("ztex", firmware);
-		if (!fp) {
-			applog(LOG_ERR, "%s: failed to read bitstream '%s'", ztex->repr, firmware);
-			return -2;
-		}
-
-		//* Reset fpga
-		cnt = libztex_resetFpga(ztex);
-		if (unlikely(cnt < 0)) {
-			applog(LOG_ERR, "%s: Failed reset fpga with err %d", ztex->repr, cnt);
-			continue;
-		}
-
-		do
-		{
-			int length = fread(buf, 1, transactionBytes, fp);
-
-			if (bs != 0 && bs != 1)
-				bs = libztex_detectBitstreamBitOrder(buf, length);
-			if (bs == 1)
-				libztex_swapBits(buf, length);
-			cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x32, 0, 0, buf, length, 5000);
-			if (cnt != length)
-			{
-				applog(LOG_ERR, "%s: Failed send ls fpga data", ztex->repr);
-				break;
-			}
-		}
-		while (!feof(fp));
-
-		if (cnt > 0)
-			tries = 0;
-
-		fclose(fp);
-	}
-
-	libztex_getFpgaState(ztex, &state);
-	if (!state.fpgaConfigured) {
-		applog(LOG_ERR, "%s: LS FPGA configuration failed: DONE pin does not go high", ztex->repr);
-		return -3;
-	}
-
-	cgsleep_ms(200);
-	applog(LOG_INFO, "%s: FPGA configuration done", ztex->repr);
-	return 0;
-}
-
-int libztex_configureFpga(struct libztex_device *ztex)
-{
-	char buf[256];
-	int rv;
-
-	strcpy(buf, ztex->bitFileName);
-	strcat(buf, ".bit");
-	rv = libztex_configureFpgaHS(ztex, buf, true, 2);
-	if (rv != 0)
-		rv = libztex_configureFpgaLS(ztex, buf, true, 2);
-	return rv;
-}
-
-int libztex_numberOfFpgas(struct libztex_device *ztex)
-{
-	int cnt;
-	unsigned char buf[3];
-
-	if (ztex->numberOfFpgas < 0) {
-		if (libztex_checkCapability(ztex, CAPABILITY_MULTI_FPGA)) {
-			cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x50, 0, 0, buf, 3, 1000);
-			if (unlikely(cnt < 0)) {
-				applog(LOG_ERR, "%s: Failed getMultiFpgaInfo with err %d", ztex->repr, cnt);
-				return cnt;
-			}
-			ztex->numberOfFpgas = buf[0] + 1;
-			ztex->selectedFpga = -1;//buf[1];
-			ztex->parallelConfigSupport = (buf[2] == 1);
-		} else {
-			ztex->numberOfFpgas = 1;
-			ztex->selectedFpga = -1;//0;
-			ztex->parallelConfigSupport = false;
-		}
-	}
-	return ztex->numberOfFpgas;
-}
-
-int libztex_selectFpga(struct libztex_device *ztex)
-{
-	int cnt, fpgacnt = libztex_numberOfFpgas(ztex->root);
-	int16_t number = ztex->fpgaNum;
-
-	if (number < 0 || number >= fpgacnt) {
-		applog(LOG_WARNING, "%s: Trying to select wrong fpga (%d in %d)", ztex->repr, number, fpgacnt);
-		return 1;
-	}
-	if (ztex->root->selectedFpga != number && libztex_checkCapability(ztex->root, CAPABILITY_MULTI_FPGA)) {
-		cnt = libusb_control_transfer(ztex->root->hndl, 0x40, 0x51, (uint16_t)number, 0, NULL, 0, 500);
-		if (unlikely(cnt < 0)) {
-			applog(LOG_ERR, "Ztex check device: Failed to set fpga with err %d", cnt);
-			ztex->root->selectedFpga = -1;
-			return cnt;
-		}
-		ztex->root->selectedFpga = number;
-	}
-	return 0;
-}
-
-int libztex_setFreq(struct libztex_device *ztex, uint16_t freq)
-{
-	int cnt;
-	uint16_t oldfreq = ztex->freqM;
-
-	if (freq > ztex->freqMaxM)
-		freq = ztex->freqMaxM;
-
-	cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x83, freq, 0, NULL, 0, 500);
-	if (unlikely(cnt < 0)) {
-		applog(LOG_ERR, "Ztex check device: Failed to set frequency with err %d", cnt);
-		return cnt;
-	}
-	ztex->freqM = freq;
-	if (oldfreq > ztex->freqMaxM) 
-		applog(LOG_WARNING, "%s: Frequency set to %0.1f MHz",
-		       ztex->repr, ztex->freqM1 * (ztex->freqM + 1));
-	else
-		applog(LOG_WARNING, "%s: Frequency change from %0.1f to %0.1f MHz",
-		       ztex->repr, ztex->freqM1 * (oldfreq + 1), ztex->freqM1 * (ztex->freqM + 1));
-
-	return 0;
-}
-
-int libztex_resetFpga(struct libztex_device *ztex)
-{
-	return libusb_control_transfer(ztex->hndl, 0x40, 0x31, 0, 0, NULL, 0, 1000);
-}
-
-int libztex_suspend(struct libztex_device *ztex)
-{
-	if (ztex->suspendSupported) {
-		return libusb_control_transfer(ztex->hndl, 0x40, 0x84, 0, 0, NULL, 0, 1000);
-	} else {
-		return 0;
-	}
-}
-
-int libztex_prepare_device(struct libusb_device *dev, struct libztex_device** ztex)
-{
-	struct libztex_device *newdev = *ztex;
-	int i, cnt, err;
-	unsigned char buf[64];
-
-	err = libusb_open(dev, &newdev->hndl);
-	if (err != LIBUSB_SUCCESS) {
-		applog(LOG_ERR, "%s: Can not open ZTEX device: %d", __func__, err);
-		return CHECK_ERROR;
-	}
-
-	err = libusb_get_device_descriptor(dev, &newdev->descriptor);
-	if (unlikely(err != 0)) {
-		applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
-		return CHECK_ERROR;
-	}
-
-	cnt = libztex_get_string_descriptor_ascii(newdev->hndl, newdev->descriptor.iSerialNumber, newdev->snString, sizeof(newdev->snString));
-	if (unlikely(cnt < 0)) {
-		applog(LOG_ERR, "Ztex check device: Failed to read device snString with err %d", cnt);
-		return cnt;
-	}
-
-	cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x22, 0, 0, buf, 40, 500);
-	if (unlikely(cnt < 0)) {
-		applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
-		return cnt;
-	}
-
-	if (buf[0] != 40 || buf[1] != 1 || buf[2] != 'Z' || buf[3] != 'T' || buf[4] != 'E' || buf[5] != 'X') {
-		applog(LOG_ERR, "Ztex check device: Error reading ztex descriptor");
-		return 2;
-	}
-
-	newdev->productId[0] = buf[6];
-	newdev->productId[1] = buf[7];
-	newdev->productId[2] = buf[8];
-	newdev->productId[3] = buf[9];
-	newdev->fwVersion = buf[10];
-	newdev->interfaceVersion = buf[11];
-	newdev->interfaceCapabilities[0] = buf[12];
-	newdev->interfaceCapabilities[1] = buf[13];
-	newdev->interfaceCapabilities[2] = buf[14];
-	newdev->interfaceCapabilities[3] = buf[15];
-	newdev->interfaceCapabilities[4] = buf[16];
-	newdev->interfaceCapabilities[5] = buf[17];
-	newdev->moduleReserved[0] = buf[18];
-	newdev->moduleReserved[1] = buf[19];
-	newdev->moduleReserved[2] = buf[20];
-	newdev->moduleReserved[3] = buf[21];
-	newdev->moduleReserved[4] = buf[22];
-	newdev->moduleReserved[5] = buf[23];
-	newdev->moduleReserved[6] = buf[24];
-	newdev->moduleReserved[7] = buf[25];
-	newdev->moduleReserved[8] = buf[26];
-	newdev->moduleReserved[9] = buf[27];
-	newdev->moduleReserved[10] = buf[28];
-	newdev->moduleReserved[11] = buf[29];
-
-	cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x82, 0, 0, buf, 64, 500);
-	if (unlikely(cnt < 0)) {
-		applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
-		return cnt;
-	}
-
-	if (unlikely(buf[0] != 5)) {
-		if (unlikely(buf[0] != 2 && buf[0] != 4)) {
-			applog(LOG_ERR, "Invalid BTCMiner descriptor version. Firmware must be updated (%d).", buf[0]);
-			return 3;
-		}
-		applog(LOG_WARNING, "Firmware out of date (%d).", buf[0]);
-	}
-
-	i = buf[0] > 4? 11: (buf[0] > 2? 10: 8);
-
-	while (cnt < 64 && buf[cnt] != 0)
-		cnt++;
-	if (cnt < i + 1) {
-		applog(LOG_ERR, "Invalid bitstream file name .");
-		return 4;
-	}
-
-	newdev->bitFileName = malloc(sizeof(char) * (cnt + 1));
-	memcpy(newdev->bitFileName, &buf[i], cnt);
-	newdev->bitFileName[cnt] = 0;	
-
-	newdev->numNonces = buf[1] + 1;
-	newdev->offsNonces = ((buf[2] & 255) | ((buf[3] & 255) << 8)) - 10000;
-	newdev->freqM1 = ((buf[4] & 255) | ((buf[5] & 255) << 8) ) * 0.01;
-	newdev->freqMaxM = (buf[7] & 255);
-	newdev->freqM = (buf[6] & 255);
-	newdev->freqMDefault = newdev->freqM;
-	newdev->suspendSupported = (buf[0] == 5);
-	newdev->hashesPerClock = buf[0] > 2? (((buf[8] & 255) | ((buf[9] & 255) << 8)) + 1) / 128.0: 1.0;
-	newdev->extraSolutions = buf[0] > 4? buf[10]: 0;
-
-	applog(LOG_DEBUG, "PID: %d numNonces: %d offsNonces: %d freqM1: %f freqMaxM: %d freqM: %d suspendSupported: %s hashesPerClock: %f extraSolutions: %d",
-	                 buf[0], newdev->numNonces, newdev->offsNonces, newdev->freqM1, newdev->freqMaxM, newdev->freqM, newdev->suspendSupported ? "T": "F", 
-	                 newdev->hashesPerClock, newdev->extraSolutions);
-
-	if (buf[0] < 4) {
-		if (strncmp(newdev->bitFileName, "ztex_ufm1_15b", 13) != 0)
-			newdev->hashesPerClock = 0.5;
-		applog(LOG_WARNING, "HASHES_PER_CLOCK not defined, assuming %0.2f", newdev->hashesPerClock);
-	}
-
-	for (cnt=0; cnt < 255; cnt++) {
-		newdev->errorCount[cnt] = 0;
-		newdev->errorWeight[cnt] = 0;
-		newdev->errorRate[cnt] = 0;
-		newdev->maxErrorRate[cnt] = 0;
-	}
-
-	// fake that the last round found something valid
-	newdev->nonceCheckValid = 1;
-
-	newdev->usbbus = libusb_get_bus_number(dev);
-	newdev->usbaddress = libusb_get_device_address(dev);
-	sprintf(newdev->repr, "ZTEX %s-1", newdev->snString);
-	return 0;
-}
-
-void libztex_destroy_device(struct libztex_device* ztex)
-{
-	if (ztex->hndl != NULL) {
-		libusb_close(ztex->hndl);
-		ztex->hndl = NULL;
-	}
-	if (ztex->bitFileName != NULL) {
-		free(ztex->bitFileName);
-		ztex->bitFileName = NULL;
-	}
-	free(ztex);
-}
-
-int libztex_scanDevices(struct libztex_dev_list*** devs_p)
-{
-	int usbdevices[LIBZTEX_MAX_DESCRIPTORS];
-	struct libztex_dev_list **devs = NULL;
-	struct libztex_device *ztex = NULL;
-	int found, max_found = 0, pos = 0, err, rescan, ret = 0;
-	libusb_device **list = NULL;
-	ssize_t cnt, i;
-
-	do {
-		cnt = libusb_get_device_list(NULL, &list);
-		if (unlikely(cnt < 0)) {
-			applog(LOG_ERR, "Ztex scan devices: Failed to list usb devices with err %d", (int)cnt);
-			goto done;
-		}
-
-		for (found = rescan = i = 0; i < cnt; i++) {
-			err = libztex_checkDevice(list[i]);
-			switch (err) {
-			case CHECK_ERROR:
-				applog(LOG_ERR, "Ztex: Can not check device: %d", err);
-				continue;
-			case CHECK_IS_NOT_ZTEX:
-				continue;
-			case CHECK_OK:
-				// Got one!
-				usbdevices[found++] = i;
-				break;
-			case CHECK_RESCAN:
-				rescan = 1;
-				found++;
-				break;
-			}
-		}
-
-		if (found < max_found)
-			rescan = 1;
-		else if (found > max_found)
-			max_found = found;
-
-		if (rescan)
-			libusb_free_device_list(list, 1);
-	} while (rescan);
-
-	if (0 == found)
-		goto done;
-
-	devs = malloc(sizeof(struct libztex_dev_list *) * found);
-	if (devs == NULL) {
-		applog(LOG_ERR, "Ztex scan devices: Failed to allocate memory");
-		goto done;
-	}
-
-	for (i = 0; i < found; i++) {
-		if (!ztex) {
-			ztex = malloc(sizeof(*ztex));
-			if (!ztex) {
-				applog(LOG_ERR, "%s: Can not allocate memory for device struct: %s", __func__, strerror(errno));
-				goto done;
-			}
-		}
-
-		ztex->bitFileName = NULL;
-		ztex->numberOfFpgas = -1;
-
-		err = libztex_prepare_device(list[usbdevices[i]], &ztex);
-		if (unlikely(err != 0)) {
-			applog(LOG_ERR, "prepare device: %d", err);
-			libztex_destroy_device(ztex);
-			ztex = NULL;
-			continue;
-		}
-
-		devs[pos] = malloc(sizeof(struct libztex_dev_list));
-		if (NULL == devs[pos]) {
-			applog(LOG_ERR, "%s: Can not allocate memory for device: %s", __func__, strerror(errno));
-			libztex_destroy_device(ztex);
-			ztex = NULL;
-			continue;
-		}
-
-		devs[pos]->dev = ztex;
-		ztex = NULL;
-		devs[pos]->next = NULL;
-		if (pos > 0)
-			devs[pos - 1]->next = devs[pos];
-		pos++;
-	}
-
-	ret = pos;
-
-done:
-	if (ret > 0)
-		*devs_p = devs;
-	else if (devs)
-		free(devs);
-	if (list)
-		libusb_free_device_list(list, 1);
-	return ret;
-}
-
-int libztex_sendHashData(struct libztex_device *ztex, unsigned char *sendbuf)
-{
-	int cnt = 0, ret, len;
-
-	if (ztex == NULL || ztex->hndl == NULL)
-		return 0;
-	ret = 44; len = 0;
-	while (ret > 0) {
-		cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x80, 0, 0, sendbuf + len, ret, 1000);
-		if (cnt >= 0) {
-			ret -= cnt;
-			len += cnt;
-		} else
-			break;
-	}
-	if (unlikely(cnt < 0))
-		applog(LOG_ERR, "%s: Failed sendHashData with err %d", ztex->repr, cnt);
-
-	return cnt;
-}
-
-int libztex_readHashData(struct libztex_device *ztex, struct libztex_hash_data nonces[])
-{
-	int bufsize = 12 + ztex->extraSolutions * 4;
-	int cnt = 0, i, j, ret, len;
-	unsigned char *rbuf;
-
-	if (ztex->hndl == NULL)
-		return 0;
-
-	rbuf = malloc(sizeof(unsigned char) * (ztex->numNonces * bufsize));
-	if (rbuf == NULL) {
-		applog(LOG_ERR, "%s: Failed to allocate memory for reading nonces", ztex->repr);
-		return 0;
-	}
-	ret = bufsize * ztex->numNonces; len = 0;
-	while (ret > 0) {
-		cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x81, 0, 0, rbuf + len, ret, 1000);
-		if (cnt >= 0) {
-			ret -= cnt;
-			len += cnt;
-		} else
-			break;
-	}
-
-	if (unlikely(cnt < 0)) {
-		applog(LOG_ERR, "%s: Failed readHashData with err %d", ztex->repr, cnt);
-		free(rbuf);
-		return cnt;
-	}
-
-	for (i=0; i<ztex->numNonces; i++) {
-		memcpy((char*)&nonces[i].goldenNonce[0], &rbuf[i*bufsize], 4);
-		nonces[i].goldenNonce[0] -= ztex->offsNonces;
-		//applog(LOG_DEBUG, "W %d:0 %0.8x", i, nonces[i].goldenNonce[0]);
-
-		memcpy((char*)&nonces[i].nonce, &rbuf[(i*bufsize)+4], 4);
-		memcpy((char*)&nonces[i].hash7, &rbuf[(i*bufsize)+8], 4);
-
-		nonces[i].nonce = htole32(nonces[i].nonce);
-		nonces[i].hash7 = htole32(nonces[i].hash7);
-
-		nonces[i].nonce -= ztex->offsNonces;
-
-		for (j=0; j<ztex->extraSolutions; j++) {
-			memcpy((char*)&nonces[i].goldenNonce[j+1], &rbuf[(i*bufsize)+12+(j*4)], 4);
-			nonces[i].goldenNonce[j+1] = htole32(nonces[i].goldenNonce[j+1]);
-			nonces[i].goldenNonce[j+1] -= ztex->offsNonces;
-			//applog(LOG_DEBUG, "W %d:%d %0.8x", i, j+1, nonces[i].goldenNonce[j+1]);
-		}
-	}
-
-	free(rbuf);
-	return cnt;
-}
-
-void libztex_freeDevList(struct libztex_dev_list **devs)
-{
-	bool done = false;
-	ssize_t cnt = 0;
-
-	while (!done) {
-		if (devs[cnt]->next == NULL)
-			done = true;
-		free(devs[cnt++]);
-	}
-	free(devs);
-}
-
diff --git a/libztex.h b/libztex.h
deleted file mode 100644
index c1d09b6..0000000
--- a/libztex.h
+++ /dev/null
@@ -1,108 +0,0 @@
-/**
- *   libztex.h - headers for Ztex 1.15x fpga board support library
- *
- *   Copyright (c) 2012 nelisky.btc@gmail.com
- *
- *   This work is based upon the Java SDK provided by ztex which is
- *   Copyright (C) 2009-2011 ZTEX GmbH.
- *   http://www.ztex.de
- *
- *   This program is free software; you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License version 2 as
- *   published by the Free Software Foundation.
- *
- *   This program is distributed in the hope that it will be useful, but
- *   WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- *   General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program; if not, see http://www.gnu.org/licenses/.
-**/
-#ifndef __LIBZTEX_H__
-#define __LIBZTEX_H__
-
-#include <libusb.h>
-
-#define LIBZTEX_MAX_DESCRIPTORS 512
-#define LIBZTEX_SNSTRING_LEN 10
-
-#define LIBZTEX_IDVENDOR 0x221A
-#define LIBZTEX_IDPRODUCT 0x0100
-
-#define LIBZTEX_MAXMAXERRORRATE 0.05
-#define LIBZTEX_ERRORHYSTERESIS 0.1
-#define LIBZTEX_OVERHEATTHRESHOLD 0.4
-
-struct libztex_fpgastate {
-	bool fpgaConfigured;
-	unsigned char fpgaChecksum;
-	uint16_t fpgaBytes;
-	unsigned char fpgaInitB;
-	unsigned char fpgaFlashResult;
-	bool fpgaFlashBitSwap;
-};
-
-struct libztex_device {
-	pthread_mutex_t	mutex;
-	struct libztex_device *root;
-	int16_t fpgaNum;
-	struct libusb_device_descriptor descriptor;
-	libusb_device_handle *hndl; 
-	unsigned char usbbus;
-	unsigned char usbaddress;
-	unsigned char snString[LIBZTEX_SNSTRING_LEN+1];
-	unsigned char productId[4];
-	unsigned char fwVersion;
-	unsigned char interfaceVersion;
-	unsigned char interfaceCapabilities[6];
-	unsigned char moduleReserved[12];
-	uint8_t numNonces;
-	uint16_t offsNonces;
-	double freqM1;	
-	uint8_t freqM;
-	uint8_t freqMaxM;
-	uint8_t freqMDefault;
-	char* bitFileName;
-	bool suspendSupported;
-	double hashesPerClock;
-	uint8_t extraSolutions;
-
-	double errorCount[256];
-	double errorWeight[256];
-	double errorRate[256];
-	double maxErrorRate[256];
-
-	int16_t nonceCheckValid;
-
-	int16_t numberOfFpgas;
-	int selectedFpga;
-	bool parallelConfigSupport;
-	
-	char repr[20];
-};
-
-struct libztex_dev_list { 
-	struct libztex_device *dev;
-	struct libztex_dev_list *next;
-};
-
-struct libztex_hash_data {
-	uint32_t goldenNonce[2];
-	uint32_t nonce;
-	uint32_t hash7;
-};
-
-extern int libztex_scanDevices (struct libztex_dev_list ***devs);
-extern void libztex_freeDevList (struct libztex_dev_list **devs);
-extern int libztex_prepare_device (struct libusb_device *dev, struct libztex_device** ztex);
-extern void libztex_destroy_device (struct libztex_device* ztex);
-extern int libztex_configureFpga (struct libztex_device *dev);
-extern int libztex_setFreq (struct libztex_device *ztex, uint16_t freq);
-extern int libztex_sendHashData (struct libztex_device *ztex, unsigned char *sendbuf);
-extern int libztex_readHashData (struct libztex_device *ztex, struct libztex_hash_data nonces[]);
-extern int libztex_resetFpga (struct libztex_device *ztex);
-extern int libztex_selectFpga(struct libztex_device *ztex);
-extern int libztex_numberOfFpgas(struct libztex_device *ztex);
-
-#endif /* __LIBZTEX_H__ */
diff --git a/miner.h b/miner.h
index 4bda1bd..6764fbf 100644
--- a/miner.h
+++ b/miner.h
@@ -131,10 +131,6 @@ static inline int fsync (int fd)
   #include <libusb.h>
 #endif
 
-#ifdef USE_ZTEX
-  #include "libztex.h"
-#endif
-
 #ifdef USE_USBUTILS
   #include "usbutils.h"
 #endif
@@ -237,13 +233,13 @@ static inline int fsync (int fd)
 #define FPGA_PARSE_COMMANDS(DRIVER_ADD_COMMAND) \
 	DRIVER_ADD_COMMAND(bitforce) \
 	DRIVER_ADD_COMMAND(icarus) \
-	DRIVER_ADD_COMMAND(modminer) \
-	DRIVER_ADD_COMMAND(ztex)
+	DRIVER_ADD_COMMAND(modminer)
 
 #define ASIC_PARSE_COMMANDS(DRIVER_ADD_COMMAND) \
 	DRIVER_ADD_COMMAND(bflsc) \
 	DRIVER_ADD_COMMAND(bitfury) \
 	DRIVER_ADD_COMMAND(hashfast) \
+	DRIVER_ADD_COMMAND(klondike) \
 	DRIVER_ADD_COMMAND(avalon)
 
 #define DRIVER_PARSE_COMMANDS(DRIVER_ADD_COMMAND) \
@@ -466,14 +462,9 @@ struct cgpu_info {
 	char *name;
 	char *device_path;
 	void *device_data;
-	union {
-#ifdef USE_ZTEX
-		struct libztex_device *device_ztex;
-#endif
 #ifdef USE_USBUTILS
-		struct cg_usb_device *usbdev;
+	struct cg_usb_device *usbdev;
 #endif
-	};
 #ifdef USE_AVALON
 	struct work **works;
 	int work_array;
@@ -830,14 +821,6 @@ static inline void _rwlock_init(pthread_rwlock_t *lock, const char *file, const 
 		quitfrom(1, file, func, line, "Failed to pthread_rwlock_init errno=%d", errno);
 }
 
-/* cgminer locks, a write biased variant of rwlocks */
-struct cglock {
-	pthread_mutex_t mutex;
-	pthread_rwlock_t rwlock;
-};
-
-typedef struct cglock cglock_t;
-
 static inline void cglock_init(cglock_t *lock)
 {
 	mutex_init(&lock->mutex);
@@ -898,6 +881,14 @@ static inline void cg_runlock(cglock_t *lock)
 	rd_unlock(&lock->rwlock);
 }
 
+/* This drops the read lock and grabs a write lock. It does NOT protect data
+ * between the two locks! */
+static inline void cg_ruwlock(cglock_t *lock)
+{
+	rd_unlock_noyield(&lock->rwlock);
+	cg_wlock(lock);
+}
+
 static inline void cg_wunlock(cglock_t *lock)
 {
 	wr_unlock_noyield(&lock->rwlock);
@@ -938,6 +929,9 @@ extern bool opt_worktime;
 #ifdef USE_AVALON
 extern char *opt_avalon_options;
 #endif
+#ifdef USE_KLONDIKE
+extern char *opt_klondike_options;
+#endif
 #ifdef USE_USBUTILS
 extern char *opt_usb_select;
 extern int opt_usbdump;
@@ -959,6 +953,7 @@ extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
 #endif
 extern const char *proxytype(proxytypes_t proxytype);
 extern char *get_proxy(char *url, struct pool *pool);
+extern void __bin2hex(char *s, const unsigned char *p, size_t len);
 extern char *bin2hex(const unsigned char *p, size_t len);
 extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
 
@@ -1389,12 +1384,16 @@ extern void inc_hw_errors(struct thr_info *thr);
 extern bool test_nonce(struct work *work, uint32_t nonce);
 extern void submit_tested_work(struct thr_info *thr, struct work *work);
 extern bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
+extern bool submit_noffset_nonce(struct thr_info *thr, struct work *work, uint32_t nonce,
+			  int noffset);
+extern struct work *get_work(struct thr_info *thr, const int thr_id);
 extern struct work *get_queued(struct cgpu_info *cgpu);
 extern struct work *__find_work_bymidstate(struct work *que, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
 extern struct work *find_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
 extern struct work *clone_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
 extern 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);
 extern void hash_queued_work(struct thr_info *mythr);
 extern void _wlog(const char *str);
 extern void _wlogprint(const char *str);
@@ -1423,7 +1422,6 @@ 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 __copy_work(struct work *work, struct work *base_work);
 extern struct work *copy_work(struct work *base_work);
 extern struct thr_info *get_thread(int thr_id);
 extern struct cgpu_info *get_devices(int id);
diff --git a/ocl.c b/ocl.c
index f7184db..977eee8 100644
--- a/ocl.c
+++ b/ocl.c
@@ -342,6 +342,7 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize)
 	/* Check for OpenCL >= 1.0 support, needed for global offset parameter usage. */
 	char * devoclver = malloc(1024);
 	const char * ocl10 = "OpenCL 1.0";
+	const char * ocl11 = "OpenCL 1.1";
 
 	status = clGetDeviceInfo(devices[gpu], CL_DEVICE_VERSION, 1024, (void *)devoclver, NULL);
 	if (status != CL_SUCCESS) {
@@ -349,8 +350,12 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize)
 		return NULL;
 	}
 	find = strstr(devoclver, ocl10);
-	if (!find)
+	if (!find) {
 		clState->hasOpenCL11plus = true;
+		find = strstr(devoclver, ocl11);
+		if (!find)
+			clState->hasOpenCL12plus = true;
+	}
 
 	status = clGetDeviceInfo(devices[gpu], CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, sizeof(cl_uint), (void *)&preferred_vwidth, NULL);
 	if (status != CL_SUCCESS) {
@@ -618,19 +623,20 @@ build:
 	if (clState->hasBitAlign) {
 		strcat(CompilerOptions, " -D BITALIGN");
 		applog(LOG_DEBUG, "cl_amd_media_ops found, setting BITALIGN");
-		if (strstr(name, "Cedar") ||
-		    strstr(name, "Redwood") ||
-		    strstr(name, "Juniper") ||
-		    strstr(name, "Cypress" ) ||
-		    strstr(name, "Hemlock" ) ||
-		    strstr(name, "Caicos" ) ||
-		    strstr(name, "Turks" ) ||
-		    strstr(name, "Barts" ) ||
-		    strstr(name, "Cayman" ) ||
-		    strstr(name, "Antilles" ) ||
-		    strstr(name, "Wrestler" ) ||
-		    strstr(name, "Zacate" ) ||
-		    strstr(name, "WinterPark" ))
+		if (!clState->hasOpenCL12plus &&
+		    (strstr(name, "Cedar") ||
+		     strstr(name, "Redwood") ||
+		     strstr(name, "Juniper") ||
+		     strstr(name, "Cypress" ) ||
+		     strstr(name, "Hemlock" ) ||
+		     strstr(name, "Caicos" ) ||
+		     strstr(name, "Turks" ) ||
+		     strstr(name, "Barts" ) ||
+		     strstr(name, "Cayman" ) ||
+		     strstr(name, "Antilles" ) ||
+		     strstr(name, "Wrestler" ) ||
+		     strstr(name, "Zacate" ) ||
+		     strstr(name, "WinterPark" )))
 			patchbfi = true;
 	} else
 		applog(LOG_DEBUG, "cl_amd_media_ops not found, will not set BITALIGN");
diff --git a/ocl.h b/ocl.h
index 984e7d6..ed8bfde 100644
--- a/ocl.h
+++ b/ocl.h
@@ -27,6 +27,7 @@ typedef struct {
 #endif
 	bool hasBitAlign;
 	bool hasOpenCL11plus;
+	bool hasOpenCL12plus;
 	bool goffset;
 	cl_uint vwidth;
 	size_t max_work_size;
diff --git a/todo_ztex.txt b/todo_ztex.txt
deleted file mode 100644
index 4014b04..0000000
--- a/todo_ztex.txt
+++ /dev/null
@@ -1,5 +0,0 @@
-- verify setting cgpu.status=DEAD does in fact stop the thread
-- allow configuring bitstream directory
-- HS fpga config
-- allow configuring LIBZTEX_OVERHEATTHRESHOLD
-- hotplug support?
diff --git a/usbutils.c b/usbutils.c
index 9fa8ef5..2db31a2 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -20,10 +20,15 @@
 
 #define NODEV(err) ((err) == LIBUSB_ERROR_NO_DEVICE || \
 			(err) == LIBUSB_ERROR_PIPE || \
-			(err) == LIBUSB_ERROR_OTHER)
+			(err) == LIBUSB_ERROR_OTHER || \
+			(err) == LIBUSB_TRANSFER_NO_DEVICE || \
+			(err) == LIBUSB_TRANSFER_STALL || \
+			(err) == LIBUSB_TRANSFER_ERROR)
 
 #define NOCONTROLDEV(err) ((err) == LIBUSB_ERROR_NO_DEVICE || \
-			(err) == LIBUSB_ERROR_OTHER)
+			(err) == LIBUSB_ERROR_OTHER || \
+			(err) == LIBUSB_TRANSFER_NO_DEVICE || \
+			(err) == LIBUSB_TRANSFER_ERROR)
 
 /*
  * WARNING - these assume DEVLOCK(cgpu, pstate) is called first and
@@ -32,13 +37,23 @@
  *  You must call DEVUNLOCK(cgpu, pstate) before exiting the function or it will leave
  *  the thread Cancelability unrestored
  */
-#define DEVLOCK(cgpu, _pth_state) do { \
+#define DEVWLOCK(cgpu, _pth_state) do { \
 			pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &_pth_state); \
-			mutex_lock(cgpu->usbinfo.devlock); \
+			cg_wlock(&cgpu->usbinfo.devlock); \
 			} while (0)
 
-#define DEVUNLOCK(cgpu, _pth_state) do { \
-			mutex_unlock(cgpu->usbinfo.devlock); \
+#define DEVWUNLOCK(cgpu, _pth_state) do { \
+			cg_wunlock(&cgpu->usbinfo.devlock); \
+			pthread_setcancelstate(_pth_state, NULL); \
+			} while (0)
+
+#define DEVRLOCK(cgpu, _pth_state) do { \
+			pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &_pth_state); \
+			cg_rlock(&cgpu->usbinfo.devlock); \
+			} while (0)
+
+#define DEVRUNLOCK(cgpu, _pth_state) do { \
+			cg_runlock(&cgpu->usbinfo.devlock); \
 			pthread_setcancelstate(_pth_state, NULL); \
 			} while (0)
 
@@ -50,6 +65,7 @@
 #define BITFURY_TIMEOUT_MS 999
 #define MODMINER_TIMEOUT_MS 999
 #define AVALON_TIMEOUT_MS 999
+#define KLONDIKE_TIMEOUT_MS 999
 #define ICARUS_TIMEOUT_MS 999
 #else
 #define BFLSC_TIMEOUT_MS 300
@@ -57,6 +73,7 @@
 #define BITFURY_TIMEOUT_MS 100
 #define MODMINER_TIMEOUT_MS 100
 #define AVALON_TIMEOUT_MS 200
+#define KLONDIKE_TIMEOUT_MS 200
 #define ICARUS_TIMEOUT_MS 200
 #endif
 
@@ -139,6 +156,17 @@ static struct usb_intinfo ava_ints[] = {
 };
 #endif
 
+#ifdef USE_KLONDIKE
+static struct usb_epinfo kln_epinfos[] = {
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(1), 0, 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(1), 0, 0, 0 }
+};
+
+static struct usb_intinfo kln_ints[] = {
+	USB_EPS(0, kln_epinfos)
+};
+#endif
+
 #ifdef USE_ICARUS
 static struct usb_epinfo ica_epinfos[] = {
 	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(3), 0, 0, 0 },
@@ -299,6 +327,18 @@ static struct usb_find_devices find_dev[] = {
 		.ident = IDENT_HFA,
 	},
 #endif
+#ifdef USE_KLONDIKE
+	{
+		.drv = DRIVER_klondike,
+		.name = "KLN",
+		.ident = IDENT_KLN,
+		.idVendor = 0x04D8,
+		.idProduct = 0xF60A,
+		.config = 1,
+		.timeout = KLONDIKE_TIMEOUT_MS,
+		.latency = 10,
+		INTINFO(kln_ints) },
+#endif
 #ifdef USE_ICARUS
 	{
 		.drv = DRIVER_icarus,
@@ -365,21 +405,6 @@ static struct usb_find_devices find_dev[] = {
 		.latency = LATENCY_STD,
 		INTINFO(cmr2_ints) },
 #endif
-#ifdef USE_ZTEX
-// This is here so cgminer -n shows them
-// the ztex driver (as at 201303) doesn't use usbutils
-	{
-		.drv = DRIVER_ztex,
-		.name = "ZTX",
-		.ident = IDENT_ZTX,
-		.idVendor = 0x221a,
-		.idProduct = 0x0100,
-		.config = 1,
-		.timeout = 100,
-		.latency = LATENCY_UNUSED,
-		.intinfo_count = 0,
-		.intinfos = NULL },
-#endif
 	{ DRIVER_MAX, NULL, 0, 0, 0, NULL, NULL, 0, 0, 0, 0, NULL }
 };
 
@@ -574,29 +599,6 @@ static const char *BULK = "Bulk";
 static const char *INTERRUPT = "Interrupt";
 static const char *UNKNOWN = "Unknown";
 
-static const char *err_io_str = " IO Error";
-static const char *err_access_str = " Access Denied-a";
-static const char *err_timeout_str = " Reply Timeout";
-static const char *err_pipe_str = " Access denied-p";
-static const char *err_other_str = " Access denied-o";
-
-static const char *usberrstr(int err)
-{
-	switch (err) {
-		case LIBUSB_ERROR_IO:
-			return err_io_str;
-		case LIBUSB_ERROR_ACCESS:
-			return err_access_str;
-		case LIBUSB_ERROR_TIMEOUT:
-			return err_timeout_str;
-		case LIBUSB_ERROR_PIPE:
-			return err_pipe_str;
-		case LIBUSB_ERROR_OTHER:
-			return err_other_str;
-	}
-	return BLANK;
-}
-
 static const char *destype(uint8_t bDescriptorType)
 {
 	switch (bDescriptorType) {
@@ -803,11 +805,11 @@ static void usb_full(ssize_t *count, libusb_device *dev, char **buf, size_t *off
 
 	err = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, man, STRBUFLEN);
 	if (err < 0)
-		snprintf((char *)man, sizeof(man), "** err(%d)%s", err, usberrstr(err));
+		snprintf((char *)man, sizeof(man), "** err(%d)%s", err, libusb_error_name(err));
 
 	err = libusb_get_string_descriptor_ascii(handle, desc.iProduct, prod, STRBUFLEN);
 	if (err < 0)
-		snprintf((char *)prod, sizeof(prod), "** err(%d)%s", err, usberrstr(err));
+		snprintf((char *)prod, sizeof(prod), "** err(%d)%s", err, libusb_error_name(err));
 
 	if (level == 0) {
 		libusb_close(handle);
@@ -883,7 +885,7 @@ static void usb_full(ssize_t *count, libusb_device *dev, char **buf, size_t *off
 
 	err = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, ser, STRBUFLEN);
 	if (err < 0)
-		snprintf((char *)ser, sizeof(ser), "** err(%d)%s", err, usberrstr(err));
+		snprintf((char *)ser, sizeof(ser), "** err(%d)%s", err, libusb_error_name(err));
 
 	snprintf(tmp, sizeof(tmp), EOL "     dev %d: More Info:" EOL "\tManufacturer: '%s'" EOL
 			"\tProduct: '%s'" EOL "\tSerial '%s'",
@@ -903,7 +905,7 @@ void usb_all(int level)
 
 	count = libusb_get_device_list(NULL, &list);
 	if (count < 0) {
-		applog(LOG_ERR, "USB all: failed, err %d%s", (int)count, usberrstr((int)count));
+		applog(LOG_ERR, "USB all: failed, err %d%s", (int)count, libusb_error_name((int)count));
 		return;
 	}
 
@@ -1265,14 +1267,14 @@ static void _usb_uninit(struct cgpu_info *cgpu)
 {
 	int ifinfo;
 
-	applog(LOG_DEBUG, "USB uninit %s%i",
-			cgpu->drv->name, cgpu->device_id);
-
 	// May have happened already during a failed initialisation
 	//  if release_cgpu() was called due to a USB NODEV(err)
 	if (!cgpu->usbdev)
 		return;
 
+	applog(LOG_DEBUG, "USB uninit %s%i",
+			cgpu->drv->name, cgpu->device_id);
+
 	if (cgpu->usbdev->handle) {
 		for (ifinfo = cgpu->usbdev->found->intinfo_count - 1; ifinfo >= 0; ifinfo--) {
 			libusb_release_interface(cgpu->usbdev->handle,
@@ -1293,30 +1295,29 @@ void usb_uninit(struct cgpu_info *cgpu)
 {
 	int pstate;
 
-	DEVLOCK(cgpu, pstate);
+	DEVWLOCK(cgpu, pstate);
 
 	_usb_uninit(cgpu);
 
-	DEVUNLOCK(cgpu, pstate);
+	DEVWUNLOCK(cgpu, pstate);
 }
 
-/*
- * N.B. this is always called inside
- *	DEVLOCK(cgpu, pstate);
- */
+/* We have dropped the read devlock before entering this function but we pick
+ * up the write lock to prevent any attempts to work on dereferenced code once
+ * the nodev flag has been set. */
 static void release_cgpu(struct cgpu_info *cgpu)
 {
 	struct cg_usb_device *cgusb = cgpu->usbdev;
 	struct cgpu_info *lookcgpu;
 	int i;
 
-	applog(LOG_DEBUG, "USB release %s%i",
-			cgpu->drv->name, cgpu->device_id);
-
 	// It has already been done
 	if (cgpu->usbinfo.nodev)
 		return;
 
+	applog(LOG_DEBUG, "USB release %s%i",
+			cgpu->drv->name, cgpu->device_id);
+
 	zombie_devs++;
 	total_count--;
 	drv_count[cgpu->drv->drv_id].count--;
@@ -1352,7 +1353,7 @@ struct cgpu_info *usb_copy_cgpu(struct cgpu_info *orig)
 	struct cgpu_info *copy;
 	int pstate;
 
-	DEVLOCK(orig, pstate);
+	DEVWLOCK(orig, pstate);
 
 	copy = calloc(1, sizeof(*copy));
 	if (unlikely(!copy))
@@ -1369,7 +1370,7 @@ struct cgpu_info *usb_copy_cgpu(struct cgpu_info *orig)
 
 	copy->usbinfo.nodev = (copy->usbdev == NULL);
 
-	DEVUNLOCK(orig, pstate);
+	DEVWUNLOCK(orig, pstate);
 
 	return copy;
 }
@@ -1387,25 +1388,18 @@ struct cgpu_info *usb_alloc_cgpu(struct device_drv *drv, int threads)
 
 	cgpu->usbinfo.nodev = true;
 
-	cgpu->usbinfo.devlock = calloc(1, sizeof(*(cgpu->usbinfo.devlock)));
-	if (unlikely(!cgpu->usbinfo.devlock))
-		quit(1, "Failed to calloc devlock for %s in usb_alloc_cgpu", drv->dname);
-
-	mutex_init(cgpu->usbinfo.devlock);
+	cglock_init(&cgpu->usbinfo.devlock);
 
 	return cgpu;
 }
 
-struct cgpu_info *usb_free_cgpu_devlock(struct cgpu_info *cgpu, bool free_devlock)
+struct cgpu_info *usb_free_cgpu(struct cgpu_info *cgpu)
 {
 	if (cgpu->drv->copy)
 		free(cgpu->drv);
 
 	free(cgpu->device_path);
 
-	if (free_devlock)
-		free(cgpu->usbinfo.devlock);
-
 	free(cgpu);
 
 	return NULL;
@@ -1428,7 +1422,7 @@ static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct u
 	int bad = USB_INIT_FAIL;
 	int cfg, claimed = 0;
 
-	DEVLOCK(cgpu, pstate);
+	DEVWLOCK(cgpu, pstate);
 
 	cgpu->usbinfo.bus_number = libusb_get_bus_number(dev);
 	cgpu->usbinfo.device_address = libusb_get_device_address(dev);
@@ -1745,7 +1739,7 @@ dame:
 	cgusb = free_cgusb(cgusb);
 
 out_unlock:
-	DEVUNLOCK(cgpu, pstate);
+	DEVWUNLOCK(cgpu, pstate);
 
 	return bad;
 }
@@ -2206,6 +2200,62 @@ static char *find_end(unsigned char *buf, unsigned char *ptr, int ptrlen, int to
 #define USB_MAX_READ 8192
 #define USB_RETRY_MAX 5
 
+struct usb_transfer {
+	pthread_mutex_t mutex;
+	pthread_cond_t cond;
+	struct libusb_transfer *transfer;
+};
+
+static void init_usb_transfer(struct usb_transfer *ut)
+{
+	mutex_init(&ut->mutex);
+	pthread_cond_init(&ut->cond, NULL);
+	ut->transfer = libusb_alloc_transfer(0);
+	if (unlikely(!ut->transfer))
+		quit(1, "Failed to libusb_alloc_transfer");
+	ut->transfer->user_data = ut;
+}
+
+static void LIBUSB_CALL transfer_callback(struct libusb_transfer *transfer)
+{
+	struct usb_transfer *ut = transfer->user_data;
+
+	mutex_lock(&ut->mutex);
+	pthread_cond_signal(&ut->cond);
+	mutex_unlock(&ut->mutex);
+}
+
+/* Wait for callback function to tell us it has finished the USB transfer, but
+ * use our own timer to cancel the request if we go beyond the timeout. */
+static int callback_wait(struct usb_transfer *ut, int *transferred, unsigned int timeout)
+{
+	struct libusb_transfer *transfer= ut->transfer;
+	struct timespec ts_now, ts_end;
+	struct timeval tv_now;
+	int ret;
+
+	cgtime(&tv_now);
+	ms_to_timespec(&ts_end, timeout);
+	timeval_to_spec(&ts_now, &tv_now);
+	timeraddspec(&ts_end, &ts_now);
+	ret = pthread_cond_timedwait(&ut->cond, &ut->mutex, &ts_end);
+	if (ret) {
+		/* We are emulating a timeout ourself here */
+		libusb_cancel_transfer(transfer);
+
+		/* Now wait for the callback function to be invoked. */
+		pthread_cond_wait(&ut->cond, &ut->mutex);
+	}
+	ret = transfer->status;
+	if (ret == LIBUSB_TRANSFER_CANCELLED)
+		ret = LIBUSB_TRANSFER_TIMED_OUT;
+
+	/* No need to sort out mutexes here since they won't be reused */
+	*transferred = transfer->actual_length;
+
+	return ret;
+}
+
 static int
 usb_bulk_transfer(struct libusb_device_handle *dev_handle, int intinfo,
 		  int epinfo, unsigned char *data, int length,
@@ -2214,13 +2264,14 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, int intinfo,
 		  enum usb_cmds cmd, __maybe_unused int seq)
 {
 	struct usb_epinfo *usb_epinfo;
+	struct usb_transfer ut;
 	unsigned char endpoint;
 	uint16_t MaxPacketSize;
-	int err, errn, tries = 0;
+	int err, errn;
 #if DO_USB_STATS
 	struct timeval tv_start, tv_finish;
 #endif
-	unsigned char *buf;
+	unsigned char buf[512];
 
 	usb_epinfo = &(cgpu->usbdev->found->intinfos[intinfo].epinfos[epinfo]);
 	endpoint = usb_epinfo->ep;
@@ -2233,18 +2284,26 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, int intinfo,
 		MaxPacketSize = usb_epinfo->wMaxPacketSize;
 	if (length > MaxPacketSize)
 		length = MaxPacketSize;
-	buf = alloca(MaxPacketSize);
 	if ((endpoint & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT)
 		memcpy(buf, data, length);
 
 	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);
 
+	init_usb_transfer(&ut);
+	mutex_lock(&ut.mutex);
+	/* We give the transfer no timeout since we manage timeouts ourself */
+	libusb_fill_bulk_transfer(ut.transfer, dev_handle, endpoint, buf, length,
+				  transfer_callback, &ut, 0);
+
 	STATS_TIMEVAL(&tv_start);
 	cg_rlock(&cgusb_fd_lock);
-	err = libusb_bulk_transfer(dev_handle, endpoint, buf, length,
-				   transferred, timeout);
-	errn = errno;
+	err = libusb_submit_transfer(ut.transfer);
 	cg_runlock(&cgusb_fd_lock);
+	errn = errno;
+	if (!err)
+		err = callback_wait(&ut, transferred, timeout);
+	libusb_free_transfer(ut.transfer);
+
 	STATS_TIMEVAL(&tv_finish);
 	USB_STATS(cgpu, &tv_start, &tv_finish, err, mode, cmd, seq, timeout);
 
@@ -2253,41 +2312,21 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, int intinfo,
 				cgpu->drv->name, cgpu->device_id,
 				usb_cmdname(cmd), *transferred, err, errn);
 
-	if (err == LIBUSB_ERROR_PIPE) {
-		cgpu->usbinfo.last_pipe = time(NULL);
-		cgpu->usbinfo.pipe_count++;
-		applog(LOG_INFO, "%s%i: libusb pipe error, trying to clear",
-			cgpu->drv->name, cgpu->device_id);
+	if (err == LIBUSB_ERROR_PIPE || err == LIBUSB_TRANSFER_STALL) {
+		int retries = 0;
+
 		do {
+			cgpu->usbinfo.last_pipe = time(NULL);
+			cgpu->usbinfo.pipe_count++;
+			applog(LOG_INFO, "%s%i: libusb pipe error, trying to clear",
+				cgpu->drv->name, cgpu->device_id);
 			err = libusb_clear_halt(dev_handle, endpoint);
-			if (unlikely(err == LIBUSB_ERROR_NOT_FOUND ||
-				     err == LIBUSB_ERROR_NO_DEVICE)) {
-					cgpu->usbinfo.clear_err_count++;
-					break;
-			}
-
-			STATS_TIMEVAL(&tv_start);
-			cg_rlock(&cgusb_fd_lock);
-			err = libusb_bulk_transfer(dev_handle, endpoint, buf,
-						   length, transferred, timeout);
-			errn = errno;
-			cg_runlock(&cgusb_fd_lock);
-			STATS_TIMEVAL(&tv_finish);
-			USB_STATS(cgpu, &tv_start, &tv_finish, err, mode, cmd, seq, timeout);
-
-			if (err < 0)
-				applog(LOG_DEBUG, "%s%i: %s (amt=%d err=%d ern=%d)",
-						cgpu->drv->name, cgpu->device_id,
-						usb_cmdname(cmd), *transferred, err, errn);
+			applog(LOG_DEBUG, "%s%i: libusb pipe error%scleared",
+				cgpu->drv->name, cgpu->device_id, err ? " not " : " ");
 
 			if (err)
-				cgpu->usbinfo.retry_err_count++;
-		} while (err == LIBUSB_ERROR_PIPE && tries++ < USB_RETRY_MAX);
-		applog(LOG_DEBUG, "%s%i: libusb pipe error%scleared",
-			cgpu->drv->name, cgpu->device_id, err ? " not " : " ");
-
-		if (err)
-			cgpu->usbinfo.clear_fail_count++;
+				cgpu->usbinfo.clear_fail_count++;
+		} while (err && ++retries < USB_RETRY_MAX);
 	}
 	if ((endpoint & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN)
 		memcpy(data, buf, length);
@@ -2309,7 +2348,7 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
 	unsigned char *ptr, *usbbuf = cgpu->usbinfo.bulkbuf;
 	size_t usbbufread;
 
-	DEVLOCK(cgpu, pstate);
+	DEVRLOCK(cgpu, pstate);
 
 	if (cgpu->usbinfo.nodev) {
 		*buf = '\0';
@@ -2317,7 +2356,7 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
 		USB_REJECT(cgpu, MODE_BULK_READ);
 
 		err = LIBUSB_ERROR_NO_DEVICE;
-		goto out_unlock;
+		goto out_noerrmsg;
 	}
 
 	usbdev = cgpu->usbdev;
@@ -2429,9 +2468,6 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
 		*processed = tot;
 		memcpy((char *)buf, (const char *)usbbuf, (tot < (int)bufsiz) ? tot + 1 : (int)bufsiz);
 
-		if (NODEV(err))
-			release_cgpu(cgpu);
-
 		goto out_unlock;
 	}
 
@@ -2557,11 +2593,20 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
 	*processed = tot;
 	memcpy((char *)buf, (const char *)usbbuf, (tot < (int)bufsiz) ? tot + 1 : (int)bufsiz);
 
-	if (NODEV(err))
-		release_cgpu(cgpu);
-
 out_unlock:
-	DEVUNLOCK(cgpu, pstate);
+	if (err && err != LIBUSB_ERROR_TIMEOUT && err != LIBUSB_TRANSFER_TIMED_OUT) {
+		applog(LOG_WARNING, "%s %i usb read error: %s", cgpu->drv->name, cgpu->device_id,
+		       libusb_error_name(err));
+		if (cgpu->usbinfo.continuous_ioerr_count > USB_RETRY_MAX)
+			err = LIBUSB_ERROR_OTHER;
+	}
+out_noerrmsg:
+	if (NODEV(err)) {
+		cg_ruwlock(&cgpu->usbinfo.devlock);
+		release_cgpu(cgpu);
+		DEVWUNLOCK(cgpu, pstate);
+	} else
+		DEVRUNLOCK(cgpu, pstate);
 
 	return err;
 }
@@ -2575,7 +2620,7 @@ int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_
 	__maybe_unused bool first = true;
 	int err, sent, tot, pstate;
 
-	DEVLOCK(cgpu, pstate);
+	DEVRLOCK(cgpu, pstate);
 
 	USBDEBUG("USB debug: _usb_write(%s (nodev=%s),intinfo=%d,epinfo=%d,buf='%s',bufsiz=%d,proc=%p,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), intinfo, epinfo, (char *)str_text(buf), (int)bufsiz, processed, timeout, usb_cmdname(cmd));
 
@@ -2585,7 +2630,7 @@ int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_
 		USB_REJECT(cgpu, MODE_BULK_WRITE);
 
 		err = LIBUSB_ERROR_NO_DEVICE;
-		goto out_unlock;
+		goto out_noerrmsg;
 	}
 
 	usbdev = cgpu->usbdev;
@@ -2649,12 +2694,54 @@ int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_
 
 	*processed = tot;
 
-	if (NODEV(err))
+	if (err) {
+		applog(LOG_WARNING, "%s %i usb write error: %s", cgpu->drv->name, cgpu->device_id,
+		       libusb_error_name(err));
+		if (cgpu->usbinfo.continuous_ioerr_count > USB_RETRY_MAX)
+			err = LIBUSB_ERROR_OTHER;
+	}
+out_noerrmsg:
+	if (NODEV(err)) {
+		cg_ruwlock(&cgpu->usbinfo.devlock);
 		release_cgpu(cgpu);
+		DEVWUNLOCK(cgpu, pstate);
+	} else
+		DEVRUNLOCK(cgpu, pstate);
 
-out_unlock:
-	DEVUNLOCK(cgpu, pstate);
+	return err;
+}
 
+/* As we do for bulk reads, emulate a sync function for control transfers using
+ * our own timeouts that takes the same parameters as libusb_control_transfer.
+ */
+static int usb_control_transfer(libusb_device_handle *dev_handle, uint8_t bmRequestType,
+				uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
+				unsigned char *buffer, uint16_t wLength, unsigned int timeout)
+{
+	struct usb_transfer ut;
+	unsigned char buf[70];
+	int err, transferred;
+
+	init_usb_transfer(&ut);
+	mutex_lock(&ut.mutex);
+	libusb_fill_control_setup(buf, bmRequestType, bRequest, wValue,
+				  wIndex, wLength);
+	libusb_fill_control_transfer(ut.transfer, dev_handle, buf, transfer_callback,
+				     &ut, 0);
+	err = libusb_submit_transfer(ut.transfer);
+	if (!err)
+		err = callback_wait(&ut, &transferred, timeout);
+	if (!err && transferred) {
+		unsigned char *ofbuf = libusb_control_transfer_get_data(ut.transfer);
+
+		memcpy(buffer, ofbuf, transferred);
+		err = transferred;
+		goto out;
+	}
+	if ((err) == LIBUSB_TRANSFER_CANCELLED)
+		err = LIBUSB_ERROR_TIMEOUT;
+out:
+	libusb_free_transfer(ut.transfer);
 	return err;
 }
 
@@ -2714,7 +2801,7 @@ int __usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bReques
 	}
 	STATS_TIMEVAL(&tv_start);
 	cg_rlock(&cgusb_fd_lock);
-	err = libusb_control_transfer(usbdev->handle, request_type,
+	err = usb_control_transfer(usbdev->handle, request_type,
 		bRequest, wValue, wIndex, buf, (uint16_t)siz, timeout);
 	cg_runlock(&cgusb_fd_lock);
 	STATS_TIMEVAL(&tv_finish);
@@ -2724,22 +2811,30 @@ int __usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bReques
 
 	IOERR_CHECK(cgpu, err);
 
-	if (NOCONTROLDEV(err))
-		release_cgpu(cgpu);
-
+	if (err < 0 && err != LIBUSB_ERROR_TIMEOUT) {
+		applog(LOG_WARNING, "%s %i usb transfer error(%d): %s", cgpu->drv->name, cgpu->device_id,
+		       err, libusb_error_name(err));
+	}
 out_:
 	return err;
 }
 
+/* We use the write devlock for control transfers since some control transfers
+ * are rare but may be changing settings within the device causing problems
+ * if concurrent transfers are happening. Using the write lock serialises
+ * any transfers. */
 int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint32_t *data, int siz, unsigned int timeout, enum usb_cmds cmd)
 {
 	int pstate, err;
 
-	DEVLOCK(cgpu, pstate);
+	DEVWLOCK(cgpu, pstate);
 
 	err = __usb_transfer(cgpu, request_type, bRequest, wValue, wIndex, data, siz, timeout, cmd);
 
-	DEVUNLOCK(cgpu, pstate);
+	if (NOCONTROLDEV(err))
+		release_cgpu(cgpu);
+
+	DEVWUNLOCK(cgpu, pstate);
 
 	return err;
 }
@@ -2753,7 +2848,7 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe
 	unsigned char tbuf[64];
 	int err, pstate;
 
-	DEVLOCK(cgpu, pstate);
+	DEVWLOCK(cgpu, pstate);
 
 	USBDEBUG("USB debug: _usb_transfer_read(%s (nodev=%s),type=%"PRIu8",req=%"PRIu8",value=%"PRIu16",index=%"PRIu16",bufsiz=%d,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), request_type, bRequest, wValue, wIndex, bufsiz, timeout, usb_cmdname(cmd));
 
@@ -2761,7 +2856,7 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe
 		USB_REJECT(cgpu, MODE_CTRL_READ);
 
 		err = LIBUSB_ERROR_NO_DEVICE;
-		goto out_unlock;
+		goto out_noerrmsg;
 	}
 	usbdev = cgpu->usbdev;
 	if (timeout == DEVTIMEOUT)
@@ -2788,7 +2883,7 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe
 	memset(tbuf, 0, 64);
 	STATS_TIMEVAL(&tv_start);
 	cg_rlock(&cgusb_fd_lock);
-	err = libusb_control_transfer(usbdev->handle, request_type,
+	err = usb_control_transfer(usbdev->handle, request_type,
 		bRequest, wValue, wIndex,
 		tbuf, (uint16_t)bufsiz, timeout);
 	cg_runlock(&cgusb_fd_lock);
@@ -2803,11 +2898,16 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe
 	if (err > 0) {
 		*amount = err;
 		err = 0;
-	} else if (NOCONTROLDEV(err))
+	}
+	if (err < 0 && err != LIBUSB_ERROR_TIMEOUT) {
+		applog(LOG_WARNING, "%s %i usb transfer read error(%d): %s", cgpu->drv->name, cgpu->device_id,
+		       err, libusb_error_name(err));
+	}
+out_noerrmsg:
+	if (NOCONTROLDEV(err))
 		release_cgpu(cgpu);
 
-out_unlock:
-	DEVUNLOCK(cgpu, pstate);
+	DEVWUNLOCK(cgpu, pstate);
 
 	return err;
 }
@@ -2841,7 +2941,7 @@ int _usb_ftdi_set_latency(struct cgpu_info *cgpu, int intinfo)
 	int err = 0;
 	int pstate;
 
-	DEVLOCK(cgpu, pstate);
+	DEVWLOCK(cgpu, pstate);
 
 	if (cgpu->usbdev) {
 		if (cgpu->usbdev->usb_type != USB_TYPE_FTDI) {
@@ -2861,7 +2961,7 @@ int _usb_ftdi_set_latency(struct cgpu_info *cgpu, int intinfo)
 						NULL, 0, DEVTIMEOUT, C_LATENCY);
 	}
 
-	DEVUNLOCK(cgpu, pstate);
+	DEVWUNLOCK(cgpu, pstate);
 
 	applog(LOG_DEBUG, "%s: cgid %d %s got err %d",
 				cgpu->drv->name, cgpu->cgminer_id,
@@ -2875,7 +2975,7 @@ void usb_buffer_enable(struct cgpu_info *cgpu)
 	struct cg_usb_device *cgusb;
 	int pstate;
 
-	DEVLOCK(cgpu, pstate);
+	DEVWLOCK(cgpu, pstate);
 
 	cgusb = cgpu->usbdev;
 	if (cgusb && !cgusb->buffer) {
@@ -2887,7 +2987,7 @@ void usb_buffer_enable(struct cgpu_info *cgpu)
 		cgusb->bufsiz = USB_MAX_READ;
 	}
 
-	DEVUNLOCK(cgpu, pstate);
+	DEVWUNLOCK(cgpu, pstate);
 }
 
 void usb_buffer_disable(struct cgpu_info *cgpu)
@@ -2895,7 +2995,7 @@ void usb_buffer_disable(struct cgpu_info *cgpu)
 	struct cg_usb_device *cgusb;
 	int pstate;
 
-	DEVLOCK(cgpu, pstate);
+	DEVWLOCK(cgpu, pstate);
 
 	cgusb = cgpu->usbdev;
 	if (cgusb && cgusb->buffer) {
@@ -2905,19 +3005,19 @@ void usb_buffer_disable(struct cgpu_info *cgpu)
 		cgusb->buffer = NULL;
 	}
 
-	DEVUNLOCK(cgpu, pstate);
+	DEVWUNLOCK(cgpu, pstate);
 }
 
 void usb_buffer_clear(struct cgpu_info *cgpu)
 {
 	int pstate;
 
-	DEVLOCK(cgpu, pstate);
+	DEVWLOCK(cgpu, pstate);
 
 	if (cgpu->usbdev)
 		cgpu->usbdev->bufamt = 0;
 
-	DEVUNLOCK(cgpu, pstate);
+	DEVWUNLOCK(cgpu, pstate);
 }
 
 uint32_t usb_buffer_size(struct cgpu_info *cgpu)
@@ -2925,12 +3025,12 @@ uint32_t usb_buffer_size(struct cgpu_info *cgpu)
 	uint32_t ret = 0;
 	int pstate;
 
-	DEVLOCK(cgpu, pstate);
+	DEVRLOCK(cgpu, pstate);
 
 	if (cgpu->usbdev)
 		ret = cgpu->usbdev->bufamt;
 
-	DEVUNLOCK(cgpu, pstate);
+	DEVRUNLOCK(cgpu, pstate);
 
 	return ret;
 }
@@ -2939,36 +3039,36 @@ void usb_set_cps(struct cgpu_info *cgpu, int cps)
 {
 	int pstate;
 
-	DEVLOCK(cgpu, pstate);
+	DEVWLOCK(cgpu, pstate);
 
 	if (cgpu->usbdev)
 		cgpu->usbdev->cps = cps;
 
-	DEVUNLOCK(cgpu, pstate);
+	DEVWUNLOCK(cgpu, pstate);
 }
 
 void usb_enable_cps(struct cgpu_info *cgpu)
 {
 	int pstate;
 
-	DEVLOCK(cgpu, pstate);
+	DEVWLOCK(cgpu, pstate);
 
 	if (cgpu->usbdev)
 		cgpu->usbdev->usecps = true;
 
-	DEVUNLOCK(cgpu, pstate);
+	DEVWUNLOCK(cgpu, pstate);
 }
 
 void usb_disable_cps(struct cgpu_info *cgpu)
 {
 	int pstate;
 
-	DEVLOCK(cgpu, pstate);
+	DEVWLOCK(cgpu, pstate);
 
 	if (cgpu->usbdev)
 		cgpu->usbdev->usecps = false;
 
-	DEVUNLOCK(cgpu, pstate);
+	DEVWUNLOCK(cgpu, pstate);
 }
 
 /*
@@ -2983,12 +3083,12 @@ int _usb_interface(struct cgpu_info *cgpu, int intinfo)
 	int interface = 0;
 	int pstate;
 
-	DEVLOCK(cgpu, pstate);
+	DEVRLOCK(cgpu, pstate);
 
 	if (cgpu->usbdev)
 		interface = cgpu->usbdev->found->intinfos[intinfo].ctrl_transfer;
 
-	DEVUNLOCK(cgpu, pstate);
+	DEVRUNLOCK(cgpu, pstate);
 
 	return interface;
 }
@@ -2998,12 +3098,12 @@ enum sub_ident usb_ident(struct cgpu_info *cgpu)
 	enum sub_ident ident = IDENT_UNK;
 	int pstate;
 
-	DEVLOCK(cgpu, pstate);
+	DEVRLOCK(cgpu, pstate);
 
 	if (cgpu->usbdev)
 		ident = cgpu->usbdev->ident;
 
-	DEVUNLOCK(cgpu, pstate);
+	DEVRUNLOCK(cgpu, pstate);
 
 	return ident;
 }
@@ -3021,7 +3121,7 @@ void _usb_set_pps(struct cgpu_info *cgpu, int intinfo, int epinfo, uint16_t Pref
 	struct usb_find_devices *found;
 	int pstate;
 
-	DEVLOCK(cgpu, pstate);
+	DEVWLOCK(cgpu, pstate);
 
 	if (cgpu->usbdev) {
 		found = cgpu->usbdev->found;
@@ -3039,7 +3139,7 @@ void _usb_set_pps(struct cgpu_info *cgpu, int intinfo, int epinfo, uint16_t Pref
 		}
 	}
 
-	DEVUNLOCK(cgpu, pstate);
+	DEVWUNLOCK(cgpu, pstate);
 }
 
 // Need to set all devices with matching usbdev
@@ -3050,7 +3150,7 @@ void usb_set_dev_start(struct cgpu_info *cgpu)
 	struct timeval now;
 	int pstate;
 
-	DEVLOCK(cgpu, pstate);
+	DEVWLOCK(cgpu, pstate);
 
 	cgusb = cgpu->usbdev;
 
@@ -3067,13 +3167,13 @@ void usb_set_dev_start(struct cgpu_info *cgpu)
 		}
 	}
 
-	DEVUNLOCK(cgpu, pstate);
+	DEVWUNLOCK(cgpu, pstate);
 }
 
-void usb_cleanup()
+void usb_cleanup(void)
 {
 	struct cgpu_info *cgpu;
-	int count;
+	int count, pstate;
 	int i;
 
 	hotplug_time = 0;
@@ -3090,9 +3190,10 @@ void usb_cleanup()
 			case DRIVER_modminer:
 			case DRIVER_icarus:
 			case DRIVER_avalon:
-				mutex_lock(cgpu->usbinfo.devlock);
+			case DRIVER_klondike:
+				DEVWLOCK(cgpu, pstate);
 				release_cgpu(cgpu);
-				mutex_unlock(cgpu->usbinfo.devlock);
+				DEVWUNLOCK(cgpu, pstate);
 				count++;
 				break;
 			default:
@@ -3131,7 +3232,7 @@ void usb_cleanup()
 	cgsem_destroy(&usb_resource_sem);
 }
 
-#define DRIVER_COUNT_FOUND(X) if (strcasecmp(ptr, X##_drv.name) == 0) { \
+#define DRIVER_COUNT_FOUND(X) if (X##_drv.name && strcasecmp(ptr, X##_drv.name) == 0) { \
 	drv_count[X##_drv.drv_id].limit = lim; \
 	found = true; \
 	}
diff --git a/usbutils.h b/usbutils.h
index bd823c7..7dbe4eb 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -145,6 +145,7 @@ enum sub_ident {
 	IDENT_AVA,
 	IDENT_BTB,
 	IDENT_HFA,
+	IDENT_KLN,
 	IDENT_ICA,
 	IDENT_AMU,
 	IDENT_BLT,
@@ -238,7 +239,7 @@ struct cg_usb_info {
 	 * that uses the lock - however, all usbutils code MUST use it
 	 * to avoid devices disappearing while in use by multiple threads
 	 */
-	pthread_mutex_t *devlock;
+	cglock_t devlock;
 
 	time_t last_pipe;
 	uint64_t pipe_count;
@@ -369,8 +370,7 @@ const char *usb_cmdname(enum usb_cmds cmd);
 void usb_applog(struct cgpu_info *bflsc, enum usb_cmds cmd, char *msg, int amount, int err);
 struct cgpu_info *usb_copy_cgpu(struct cgpu_info *orig);
 struct cgpu_info *usb_alloc_cgpu(struct device_drv *drv, int threads);
-struct cgpu_info *usb_free_cgpu_devlock(struct cgpu_info *cgpu, bool free_devlock);
-#define usb_free_cgpu(cgpu) usb_free_cgpu_devlock(cgpu, true)
+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 *));
diff --git a/util.c b/util.c
index ecece48..8e03281 100644
--- a/util.c
+++ b/util.c
@@ -584,6 +584,16 @@ char *get_proxy(char *url, struct pool *pool)
 	return url;
 }
 
+/* Adequate size s==len*2 + 1 must be alloced to use this variant */
+void __bin2hex(char *s, const unsigned char *p, size_t len)
+{
+	int i;
+
+	for (i = 0; i < (int)len; i++)
+		sprintf(s + (i * 2), "%02x", (unsigned int)p[i]);
+
+}
+
 /* Returns a malloced array string of a binary value of arbitrary length. The
  * array is rounded up to a 4 byte size to appease architectures that need
  * aligned array  sizes */
@@ -591,7 +601,6 @@ char *bin2hex(const unsigned char *p, size_t len)
 {
 	ssize_t slen;
 	char *s;
-	int i;
 
 	slen = len * 2 + 1;
 	if (slen % 4)
@@ -600,8 +609,7 @@ char *bin2hex(const unsigned char *p, size_t len)
 	if (unlikely(!s))
 		quithere(1, "Failed to calloc");
 
-	for (i = 0; i < (int)len; i++)
-		sprintf(s + (i * 2), "%02x", (unsigned int)p[i]);
+	__bin2hex(s, p, len);
 
 	return s;
 }
@@ -1071,13 +1079,20 @@ void cgsleep_us(int64_t us)
 /* Returns the microseconds difference between end and start times as a double */
 double us_tdiff(struct timeval *end, struct timeval *start)
 {
-	return end->tv_sec * 1000000 + end->tv_usec - start->tv_sec * 1000000 - start->tv_usec;
+	/* Sanity check. We should only be using this for small differences so
+	 * limit the max to 60 seconds. */
+	if (unlikely(end->tv_sec - start->tv_sec > 60))
+		return 60000000;
+	return (end->tv_sec - start->tv_sec) * 1000000 + (end->tv_usec - start->tv_usec);
 }
 
 /* Returns the milliseconds difference between end and start times */
 int ms_tdiff(struct timeval *end, struct timeval *start)
 {
-	return end->tv_sec * 1000 + end->tv_usec / 1000 - start->tv_sec * 1000 - start->tv_usec / 1000;
+	/* Like us_tdiff, limit to 1 hour. */
+	if (unlikely(end->tv_sec - start->tv_sec > 3600))
+		return 3600000;
+	return (end->tv_sec - start->tv_sec) * 1000 + (end->tv_usec - start->tv_usec) / 1000;
 }
 
 /* Returns the seconds difference between end and start times as a double */
@@ -1999,7 +2014,7 @@ static bool setup_stratum_socket(struct pool *pool)
 		break;
 	}
 	if (p == NULL) {
-		applog(LOG_NOTICE, "Failed to connect to stratum on %s:%s",
+		applog(LOG_INFO, "Failed to connect to stratum on %s:%s",
 		       sockaddr_url, sockaddr_port);
 		freeaddrinfo(servinfo);
 		return false;
diff --git a/util.h b/util.h
index 2370e87..f0ee041 100644
--- a/util.h
+++ b/util.h
@@ -59,6 +59,14 @@ typedef curl_proxytype proxytypes_t;
 typedef int proxytypes_t;
 #endif /* HAVE_LIBCURL */
 
+/* cgminer locks, a write biased variant of rwlocks */
+struct cglock {
+	pthread_mutex_t mutex;
+	pthread_rwlock_t rwlock;
+};
+
+typedef struct cglock cglock_t;
+
 /* cgminer specific unnamed semaphore implementations to cope with osx not
  * implementing them. */
 #ifdef __APPLE__