Commit 3e4425d98cdc087300f424e6998efe320aa0a03f

Con Kolivas 2014-01-09T16:20:18

Merge branch 'nanofury'

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
diff --git a/01-cgminer.rules b/01-cgminer.rules
index 733a774..e9a0bf1 100644
--- a/01-cgminer.rules
+++ b/01-cgminer.rules
@@ -1,35 +1,38 @@
 # Butterfly Labs FPGA and ASIC devices
-ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6014", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
+ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6014", SUBSYSTEM=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
 
 # ModMinerQuad
-ATTRS{idVendor}=="1fc9", ATTRS{idProduct}=="0003", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
+ATTRS{idVendor}=="1fc9", ATTRS{idProduct}=="0003", SUBSYSTEM=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
 
 # Lancelot and Avalon
-ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
+ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", SUBSYSTEM=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
 
 # Icarus
-ATTRS{idVendor}=="067b", ATTRS{idProduct}=="2303", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
+ATTRS{idVendor}=="067b", ATTRS{idProduct}=="2303", SUBSYSTEM=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
 
 # AsicminerUSB
-ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
+ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", SUBSYSTEM=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
 
 # Cairnsmore1
-ATTRS{idVendor}=="067b", ATTRS{idProduct}=="0230", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
+ATTRS{idVendor}=="067b", ATTRS{idProduct}=="0230", SUBSYSTEM=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
 
 # Cairnsmore1-2
-ATTRS{idVendor}=="0403", ATTRS{idProduct}=="8350", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
+ATTRS{idVendor}=="0403", ATTRS{idProduct}=="8350", SUBSYSTEM=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
 
 # Ztex
-ATTRS{idVendor}=="221a", ATTRS{idProduct}=="0100", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
+ATTRS{idVendor}=="221a", ATTRS{idProduct}=="0100", SUBSYSTEM=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
 
 # BF1
-ATTRS{idVendor}=="03eb", ATTRS{idProduct}=="204b", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
+ATTRS{idVendor}=="03eb", ATTRS{idProduct}=="204b", SUBSYSTEM=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
 
-#Klondike
-ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="f60a", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
+# Klondike
+ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="f60a", SUBSYSTEM=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
 
-#HashFast
-ATTRS{idVendor}=="297c", ATTRS{idProduct}=="0001", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev", ENV{ID_MM_DEVICE_IGNORE}="1"
+# HashFast
+ATTRS{idVendor}=="297c", ATTRS{idProduct}=="0001", SUBSYSTEM=="usb", ACTION=="add", MODE="0666", GROUP="plugdev", ENV{ID_MM_DEVICE_IGNORE}="1"
 
 # BXF
-ATTRS{idVendor}=="198c", ATTRS{idProduct}=="b1f1", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
+ATTRS{idVendor}=="198c", ATTRS{idProduct}=="b1f1", SUBSYSTEM=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
+
+# NF1
+ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="00de", SUBSYSTEM=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
diff --git a/ASIC-README b/ASIC-README
index af571a1..b206d43 100644
--- a/ASIC-README
+++ b/ASIC-README
@@ -3,8 +3,8 @@ SUPPORTED DEVICES
 Currently supported devices include the Avalon (including BitBurner and
 Klondike), the Butterfly Labs SC range of devices, the ASICMINER block
 erupters, the BF1 (bitfury) USB (red and blue) devices, KnCminer Mercury,
-Saturn and Jupiter devices, BlackArrow Bitfury devices, Bi*fury USB devices
-and upcoming Hashfast devices.
+Saturn and Jupiter devices, BlackArrow Bitfury devices, Bi*fury USB devices,
+Nanofury USB devices and Hashfast devices.
 
 No COM ports on windows or TTY devices will be used by cgminer as it
 communicates directly with them via USB so it is normal for them to not exist or
@@ -49,6 +49,8 @@ Bitfury devices need the --enable-bitfury option when compiling cgminer.
 
 Currently the BPMC/BGMC BF1 devices AKA redfury/bluefury are supported and
 come up as BF1, along with the Bi*fury USB devices which come up as BXF.
+Nanofury devices come up as NF1.
+
 Bitfury USB devices are also set up as per the USB ASICs below.
 
 
diff --git a/Makefile.am b/Makefile.am
index de7bf39..11ff5bc 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -51,6 +51,10 @@ if WANT_USBUTILS
 cgminer_SOURCES += usbutils.c usbutils.h
 endif
 
+if WANT_LIBBITFURY
+cgminer_SOURCES += libbitfury.c libbitfury.h
+endif
+
 # Device drivers
 if HAS_AVALON
 cgminer_SOURCES += driver-avalon.c driver-avalon.h
@@ -73,7 +77,7 @@ cgminer_SOURCES += driver-hashfast.c driver-hashfast.h hf_protocol.h hf_protocol
 endif
 
 if HAS_BITFURY
-cgminer_SOURCES += driver-bitfury.c driver-bitfury.h
+cgminer_SOURCES += driver-bitfury.c driver-bitfury.h mcp2210.c mcp2210.h
 endif
 
 if HAS_DRILLBIT
diff --git a/README b/README
index 40d6374..70ad78c 100644
--- a/README
+++ b/README
@@ -299,6 +299,12 @@ modem and the driver needs to be unloaded for cgminer to work:
 sudo kextunload -b com.apple.driver.AppleUSBCDC
 sudo kextunload -b com.apple.driver.AppleUSBCDCACMData
 
+Some devices need superuser access to mine on them so cgminer may need to
+be started with sudo
+i.e.:
+sudo cgminer <insert commands here>
+
+
 
 ---
 
diff --git a/configure.ac b/configure.ac
index 862d940..e0f6ffa 100644
--- a/configure.ac
+++ b/configure.ac
@@ -288,8 +288,15 @@ else
 	want_usbutils=false
 fi
 
+if test x$bab$bitfury != xnono; then
+	want_libbitfury = true
+else
+	want_libbitfury = false
+fi
+
 AM_CONDITIONAL([NEED_FPGAUTILS], [test x$modminer != xno])
 AM_CONDITIONAL([WANT_USBUTILS], [test x$want_usbutils != xfalse])
+AM_CONDITIONAL([WANT_LIBBITFURY], [test x$want_libbitfury != xfalse])
 AM_CONDITIONAL([HAVE_CURSES], [test x$curses = xyes])
 AM_CONDITIONAL([HAVE_WINDOWS], [test x$have_win32 = xtrue])
 AM_CONDITIONAL([HAVE_x86_64], [test x$have_x86_64 = xtrue])
diff --git a/driver-bab.c b/driver-bab.c
index 7db3df1..d40bf47 100644
--- a/driver-bab.c
+++ b/driver-bab.c
@@ -15,6 +15,7 @@
 #include "compat.h"
 #include "miner.h"
 #include "sha2.h"
+#include "libbitfury.h"
 
 /*
  * Tested on RPi running both Raspbian and Arch
@@ -1323,42 +1324,6 @@ static void bab_flush_work(struct cgpu_info *babcgpu)
 	mutex_unlock(&(babinfo->did_lock));
 }
 
-static void ms3steps(uint32_t *p)
-{
-	uint32_t a, b, c, d, e, f, g, h, new_e, new_a;
-	int i;
-
-	a = p[0];
-	b = p[1];
-	c = p[2];
-	d = p[3];
-	e = p[4];
-	f = p[5];
-	g = p[6];
-	h = p[7];
-	for (i = 0; i < 3; i++) {
-		new_e = p[i+16] + sha256_k[i] + h + CH(e,f,g) + SHA256_F2(e) + d;
-		new_a = p[i+16] + sha256_k[i] + h + CH(e,f,g) + SHA256_F2(e) +
-			SHA256_F1(a) + MAJ(a,b,c);
-		d = c;
-		c = b;
-		b = a;
-		a = new_a;
-		h = g;
-		g = f;
-		f = e;
-		e = new_e;
-	}
-	p[15] = a;
-	p[14] = b;
-	p[13] = c;
-	p[12] = d;
-	p[11] = e;
-	p[10] = f;
-	p[9] = g;
-	p[8] = h;
-}
-
 #define DATA_MERKLE7 16
 #define DATA_NTIME 17
 #define DATA_BITS 18
@@ -1369,31 +1334,6 @@ static void ms3steps(uint32_t *p)
 #define WORK_BITS (18*4)
 #define WORK_NONCE (19*4)
 
-static uint32_t decnonce(uint32_t in)
-{
-	uint32_t out;
-
-	/* First part load */
-	out = (in & 0xFF) << 24;
-	in >>= 8;
-
-	/* Byte reversal */
-	in = (((in & 0xaaaaaaaa) >> 1) | ((in & 0x55555555) << 1));
-	in = (((in & 0xcccccccc) >> 2) | ((in & 0x33333333) << 2));
-	in = (((in & 0xf0f0f0f0) >> 4) | ((in & 0x0f0f0f0f) << 4));
-
-	out |= (in >> 2) & 0x3FFFFF;
-
-	/* Extraction */
-	if (in & 1)
-		out |= (1 << 23);
-	if (in & 2)
-		out |= (1 << 22);
-
-	out -= 0x800004;
-	return out;
-}
-
 /*
  * Find the matching work item by checking the nonce against each work
  * item for the chip
diff --git a/driver-bitfury.c b/driver-bitfury.c
index bb39973..3bad374 100644
--- a/driver-bitfury.c
+++ b/driver-bitfury.c
@@ -12,6 +12,8 @@
 #include "miner.h"
 #include "driver-bitfury.h"
 #include "sha2.h"
+#include "mcp2210.h"
+#include "libbitfury.h"
 
 int opt_bxf_temp_target = BXF_TEMP_TARGET / 10;
 
@@ -276,6 +278,163 @@ out_close:
 	return false;
 }
 
+static void nf1_close(struct cgpu_info *bitfury)
+{
+	struct bitfury_info *info = bitfury->device_data;
+	struct mcp_settings *mcp = &info->mcp;
+	int i;
+
+	mcp2210_spi_cancel(bitfury);
+
+	/* Set all pins to input mode, ignoring return code */
+	for (i = 0; i < 9; i++) {
+		mcp->direction.pin[i] = MCP2210_GPIO_INPUT;
+		mcp->value.pin[i] = MCP2210_GPIO_PIN_LOW;
+	}
+	mcp2210_set_gpio_settings(bitfury, mcp);
+}
+
+static bool nf1_reinit(struct cgpu_info *bitfury, struct bitfury_info *info)
+{
+	spi_clear_buf(info);
+	spi_add_break(info);
+	spi_set_freq(info);
+	spi_send_conf(info);
+	spi_send_init(info);
+	spi_reset(bitfury, info);
+	return spi_txrx(bitfury, info);
+}
+
+static bool nf1_set_spi_settings(struct cgpu_info *bitfury, struct bitfury_info *info)
+{
+	struct mcp_settings *mcp = &info->mcp;
+
+	return mcp2210_set_spi_transfer_settings(bitfury, mcp->bitrate, mcp->icsv,
+		mcp->acsv, mcp->cstdd, mcp->ldbtcsd, mcp->sdbd, mcp->bpst, mcp->spimode);
+}
+
+static bool nf1_detect_one(struct cgpu_info *bitfury, struct bitfury_info *info)
+{
+	struct mcp_settings *mcp = &info->mcp;
+	char buf[MCP2210_BUFFER_LENGTH];
+	unsigned int length;
+	bool ret = false;
+	int i, val;
+
+	mcp2210_get_gpio_settings(bitfury, mcp);
+
+	for (i = 0; i < 9; i++) {
+		/* Set all pins to GPIO mode */
+		mcp->designation.pin[i] = MCP2210_PIN_GPIO;
+		/* Set all pins to input mode */
+		mcp->direction.pin[i] = MCP2210_GPIO_INPUT;
+		mcp->value.pin[i] = MCP2210_GPIO_PIN_LOW;
+	}
+
+	/* Set LED and PWR pins to output and high */
+	mcp->direction.pin[NF1_PIN_LED] = mcp->direction.pin[NF1_PIN_PWR_EN] = MCP2210_GPIO_OUTPUT;
+	mcp->value.pin[NF1_PIN_LED] = mcp->value.pin[NF1_PIN_PWR_EN] = MCP2210_GPIO_PIN_HIGH;
+
+	mcp->direction.pin[4] = MCP2210_GPIO_OUTPUT;
+	mcp->designation.pin[4] = MCP2210_PIN_CS;
+
+	if (!mcp2210_set_gpio_settings(bitfury, mcp))
+		goto out;
+
+	if (opt_debug) {
+		struct gpio_pin gp;
+
+		mcp2210_get_gpio_pindirs(bitfury, &gp);
+		for (i = 0; i < 9; i++) {
+			applog(LOG_DEBUG, "%s %d: Pin dir %d %d", bitfury->drv->name,
+			       bitfury->device_id, i, gp.pin[i]);
+		}
+		mcp2210_get_gpio_pinvals(bitfury, &gp);
+		for (i = 0; i < 9; i++) {
+			applog(LOG_DEBUG, "%s %d: Pin val %d %d", bitfury->drv->name,
+			       bitfury->device_id, i, gp.pin[i]);
+		}
+		mcp2210_get_gpio_pindes(bitfury, &gp);
+		for (i = 0; i < 9; i++) {
+			applog(LOG_DEBUG, "%s %d: Pin des %d %d", bitfury->drv->name,
+			       bitfury->device_id, i, gp.pin[i]);
+		}
+	}
+
+	/* Cancel any transfers in progress */
+	if (!mcp2210_spi_cancel(bitfury))
+		goto out;
+	if (!mcp2210_get_spi_transfer_settings(bitfury, &mcp->bitrate, &mcp->icsv,
+	    &mcp->acsv, &mcp->cstdd, &mcp->ldbtcsd, &mcp->sdbd, &mcp->bpst, &mcp->spimode))
+		goto out;
+	mcp->bitrate = 200000; // default to 200kHz
+	mcp->icsv = 0xffff;
+	mcp->acsv = 0xffef;
+	mcp->cstdd = mcp->ldbtcsd = mcp->sdbd = mcp->spimode = 0;
+	mcp->bpst = 1;
+	if (!nf1_set_spi_settings(bitfury, info))
+		goto out;
+
+	buf[0] = 0;
+	length = 1;
+	if (!mcp2210_spi_transfer(bitfury, mcp, buf, &length))
+		goto out;
+	/* after this command SCK_OVRRIDE should read the same as current SCK
+	 * value (which for mode 0 should be 0) */
+	if (!mcp2210_get_gpio_pinval(bitfury, NF1_PIN_SCK_OVR, &val))
+		goto out;
+	if (val != MCP2210_GPIO_PIN_LOW)
+		goto out;
+
+	/* switch SCK to polarity (default SCK=1 in mode 2) */
+	mcp->spimode = 2;
+	if (!nf1_set_spi_settings(bitfury, info))
+		goto out;
+	buf[0] = 0;
+	length = 1;
+	if (!mcp2210_spi_transfer(bitfury, mcp, buf, &length))
+		goto out;
+	/* after this command SCK_OVRRIDE should read the same as current SCK
+	 * value (which for mode 2 should be 1) */
+	if (!mcp2210_get_gpio_pinval(bitfury, NF1_PIN_SCK_OVR, &val))
+		goto out;
+	if (val != MCP2210_GPIO_PIN_HIGH)
+		goto out;
+
+	/* switch SCK to polarity (default SCK=0 in mode 0) */
+	mcp->spimode = 0;
+	if (!nf1_set_spi_settings(bitfury, info))
+		goto out;
+	buf[0] = 0;
+	length = 1;
+	if (!mcp2210_spi_transfer(bitfury, mcp, buf, &length))
+		goto out;
+	if (!mcp2210_get_gpio_pinval(bitfury, NF1_PIN_SCK_OVR, &val))
+		goto out;
+	if (val != MCP2210_GPIO_PIN_LOW)
+		goto out;
+
+	info->osc6_bits = 54;
+	if (!nf1_reinit(bitfury, info))
+		goto out;
+
+	ret = true;
+	if (!add_cgpu(bitfury))
+		quit(1, "Failed to add_cgpu in nf1_detect_one");
+
+	update_usb_stats(bitfury);
+	applog(LOG_INFO, "%s %d: Successfully initialised %s",
+	bitfury->drv->name, bitfury->device_id, bitfury->device_path);
+	spi_clear_buf(info);
+
+	info->total_nonces = 1;
+out:
+	if (!ret)
+		nf1_close(bitfury);
+
+	return ret;
+}
+
 static struct cgpu_info *bitfury_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
 {
 	struct cgpu_info *bitfury;
@@ -302,6 +461,9 @@ static struct cgpu_info *bitfury_detect_one(struct libusb_device *dev, struct us
 		case IDENT_BXF:
 			ret = bxf_detect_one(bitfury, info);
 			break;
+		case IDENT_NF1:
+			ret = nf1_detect_one(bitfury, info);
+			break;
 		default:
 			applog(LOG_INFO, "%s %d: Unrecognised bitfury device",
 			       bitfury->drv->name, bitfury->device_id);
@@ -586,46 +748,6 @@ static bool bitfury_prepare(struct thr_info *thr)
 	}
 }
 
-static uint32_t decnonce(uint32_t in)
-{
-	uint32_t out;
-
-	/* First part load */
-	out = (in & 0xFF) << 24; in >>= 8;
-
-	/* Byte reversal */
-	in = (((in & 0xaaaaaaaa) >> 1) | ((in & 0x55555555) << 1));
-	in = (((in & 0xcccccccc) >> 2) | ((in & 0x33333333) << 2));
-	in = (((in & 0xf0f0f0f0) >> 4) | ((in & 0x0f0f0f0f) << 4));
-
-	out |= (in >> 2)&0x3FFFFF;
-
-	/* Extraction */
-	if (in & 1) out |= (1 << 23);
-	if (in & 2) out |= (1 << 22);
-
-	out -= 0x800004;
-	return out;
-}
-
-#define BT_OFFSETS 3
-const uint32_t bf_offsets[] = {-0x800000, 0, -0x400000};
-
-static bool bitfury_checkresults(struct thr_info *thr, struct work *work, uint32_t nonce)
-{
-	int i;
-
-	for (i = 0; i < BT_OFFSETS; i++) {
-		uint32_t noffset = nonce + bf_offsets[i];
-
-		if (test_nonce(work, noffset)) {
-			submit_tested_work(thr, work);
-			return true;
-		}
-	}
-	return false;
-}
-
 static int64_t bitfury_rate(struct bitfury_info *info)
 {
 	double nonce_rate;
@@ -779,6 +901,40 @@ static int64_t bxf_scan(struct cgpu_info *bitfury, struct bitfury_info *info)
 	return ret;
 }
 
+static int64_t nf1_scan(struct thr_info *thr, struct cgpu_info *bitfury,
+			struct bitfury_info *info)
+{
+	int64_t ret = 0;
+
+	if (!info->work) {
+		info->work = get_work(thr, thr->id);
+		if (unlikely(thr->work_restart)) {
+			free_work(info->work);
+			return 0;
+		}
+		bitfury_work_to_payload(&info->payload, info->work);
+	}
+	if (!libbitfury_sendHashData(thr, bitfury, info))
+		return -1;
+
+	if (info->job_switched) {
+		if (likely(info->owork))
+			free_work(info->owork);
+		info->owork = info->work;
+		info->work = NULL;
+	}
+
+	ret = bitfury_rate(info);
+
+	if (unlikely(bitfury->usbinfo.nodev)) {
+		applog(LOG_WARNING, "%s %d: Device disappeared, disabling thread",
+		       bitfury->drv->name, bitfury->device_id);
+		ret = -1;
+	}
+
+	return ret;
+}
+
 static int64_t bitfury_scanwork(struct thr_info *thr)
 {
 	struct cgpu_info *bitfury = thr->cgpu;
@@ -787,10 +943,10 @@ static int64_t bitfury_scanwork(struct thr_info *thr)
 	switch(info->ident) {
 		case IDENT_BF1:
 			return bf1_scan(thr, bitfury, info);
-			break;
 		case IDENT_BXF:
 			return bxf_scan(bitfury, info);
-			break;
+		case IDENT_NF1:
+			return nf1_scan(thr, bitfury, info);
 		default:
 			return 0;
 	}
@@ -978,6 +1134,9 @@ static void bitfury_shutdown(struct thr_info *thr)
 		case IDENT_BXF:
 			bxf_close(info);
 			break;
+		case IDENT_NF1:
+			nf1_close(bitfury);
+			break;
 		default:
 			break;
 	}
diff --git a/driver-bitfury.h b/driver-bitfury.h
index 9412076..30d7e4d 100644
--- a/driver-bitfury.h
+++ b/driver-bitfury.h
@@ -12,6 +12,7 @@
 
 #include "miner.h"
 #include "usbutils.h"
+#include "mcp2210.h"
 
 #define BXF_CLOCK_DEFAULT 54
 #define BXF_CLOCK_OFF 0
@@ -24,6 +25,22 @@
 
 extern int opt_bxf_temp_target;
 
+#define NF1_PIN_LED 0
+#define NF1_PIN_SCK_OVR 5
+#define NF1_PIN_PWR_EN 6
+
+#define SPIBUF_SIZE 16384
+#define BITFURY_REFRESH_DELAY 100
+
+struct bitfury_payload {
+	unsigned char midstate[32];
+	unsigned int junk[8];
+	unsigned m7;
+	unsigned ntime;
+	unsigned nbits;
+	unsigned nnonce;
+};
+
 struct bitfury_info {
 	struct cgpu_info *base_cgpu;
 	struct thr_info *thr;
@@ -58,6 +75,20 @@ struct bitfury_info {
 	int filtered_hw[2]; // Hardware errors we're told about but are filtered
 	int job[2]; // Completed jobs we're told about
 	int submits[2]; // Submitted responses
+
+	/* NF1 specific data */
+	struct mcp_settings mcp;
+	char spibuf[SPIBUF_SIZE];
+	unsigned int spibufsz;
+	int osc6_bits;
+	struct bitfury_payload payload;
+	struct bitfury_payload opayload;
+	unsigned newbuf[17];
+	unsigned oldbuf[17];
+	int job_switched;
+	unsigned int results[16];
+	struct work *work;
+	struct work *owork;
 };
 
 #endif /* BITFURY_H */
diff --git a/libbitfury.c b/libbitfury.c
new file mode 100644
index 0000000..17f2cc1
--- /dev/null
+++ b/libbitfury.c
@@ -0,0 +1,338 @@
+/*
+ * Copyright 2014 Con Kolivas
+ * Copyright 2013 Andrew Smith
+ * Copyright 2013 bitfury
+ *
+ * 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 "miner.h"
+#include "driver-bitfury.h"
+#include "libbitfury.h"
+#include "sha2.h"
+
+void ms3steps(uint32_t *p)
+{
+	uint32_t a, b, c, d, e, f, g, h, new_e, new_a;
+	int i;
+
+	a = p[0];
+	b = p[1];
+	c = p[2];
+	d = p[3];
+	e = p[4];
+	f = p[5];
+	g = p[6];
+	h = p[7];
+	for (i = 0; i < 3; i++) {
+		new_e = p[i+16] + sha256_k[i] + h + CH(e,f,g) + SHA256_F2(e) + d;
+		new_a = p[i+16] + sha256_k[i] + h + CH(e,f,g) + SHA256_F2(e) +
+			SHA256_F1(a) + MAJ(a,b,c);
+		d = c;
+		c = b;
+		b = a;
+		a = new_a;
+		h = g;
+		g = f;
+		f = e;
+		e = new_e;
+	}
+	p[15] = a;
+	p[14] = b;
+	p[13] = c;
+	p[12] = d;
+	p[11] = e;
+	p[10] = f;
+	p[9] = g;
+	p[8] = h;
+}
+
+uint32_t decnonce(uint32_t in)
+{
+	uint32_t out;
+
+	/* First part load */
+	out = (in & 0xFF) << 24;
+	in >>= 8;
+
+	/* Byte reversal */
+	in = (((in & 0xaaaaaaaa) >> 1) | ((in & 0x55555555) << 1));
+	in = (((in & 0xcccccccc) >> 2) | ((in & 0x33333333) << 2));
+	in = (((in & 0xf0f0f0f0) >> 4) | ((in & 0x0f0f0f0f) << 4));
+
+	out |= (in >> 2) & 0x3FFFFF;
+
+	/* Extraction */
+	if (in & 1)
+		out |= (1 << 23);
+	if (in & 2)
+		out |= (1 << 22);
+
+	out -= 0x800004;
+	return out;
+}
+
+/* Test vectors to calculate (using address-translated loads) */
+static unsigned int atrvec[] = {
+	0xb0e72d8e, 0x1dc5b862, 0xe9e7c4a6, 0x3050f1f5, 0x8a1a6b7e, 0x7ec384e8, 0x42c1c3fc, 0x8ed158a1, /* MIDSTATE */
+	0,0,0,0,0,0,0,0,
+	0x8a0bb7b7, 0x33af304f, 0x0b290c1a, 0xf0c4e61f, /* WDATA: hashMerleRoot[7], nTime, nBits, nNonce */
+};
+static bool atrvec_set;
+
+void bitfury_work_to_payload(struct bitfury_payload *p, struct work *work)
+{
+	memcpy(p->midstate, work->midstate, 32);
+	p->m7 = *(unsigned int *)(work->data + 64);
+	p->ntime = *(unsigned int *)(work->data + 68);
+	p->nbits = *(unsigned int *)(work->data + 72);
+	applog(LOG_INFO, "INFO nonc: %08x bitfury_scanHash MS0: %08x, ", p->nnonce,
+	       ((unsigned int *)work->midstate)[0]);
+	applog(LOG_INFO, "INFO merkle[7]: %08x, ntime: %08x, nbits: %08x", p->m7,
+	       p->ntime, p->nbits);
+}
+
+/* Configuration registers - control oscillators and such stuff. PROGRAMMED when
+ * magic number matches, UNPROGRAMMED (default) otherwise */
+void spi_config_reg(struct bitfury_info *info, int cfgreg, int ena)
+{
+	static const uint8_t enaconf[4] = { 0xc1, 0x6a, 0x59, 0xe3 };
+	static const uint8_t disconf[4] = { 0, 0, 0, 0 };
+
+	if (ena)
+		spi_add_data(info, 0x7000 + cfgreg * 32, enaconf, 4);
+	else
+		spi_add_data(info, 0x7000 + cfgreg * 32, disconf, 4);
+}
+
+void spi_set_freq(struct bitfury_info *info)
+{
+	uint64_t freq;
+	const uint8_t *osc6 = (unsigned char *)&freq;
+
+	freq = (1ULL << info->osc6_bits) - 1ULL;
+	spi_add_data(info, 0x6000, osc6, 8); /* Program internal on-die slow oscillator frequency */
+}
+
+#define FIRST_BASE 61
+#define SECOND_BASE 4
+
+void spi_send_conf(struct bitfury_info *info)
+{
+	const int8_t nf1_counters[16] = { 64, 64, SECOND_BASE, SECOND_BASE+4, SECOND_BASE+2,
+		SECOND_BASE+2+16, SECOND_BASE, SECOND_BASE+1, (FIRST_BASE)%65, (FIRST_BASE+1)%65,
+		(FIRST_BASE+3)%65, (FIRST_BASE+3+16)%65, (FIRST_BASE+4)%65, (FIRST_BASE+4+4)%65,
+		(FIRST_BASE+3+3)%65, (FIRST_BASE+3+1+3)%65 };
+	int i;
+
+	for (i = 7; i <= 11; i++)
+		spi_config_reg(info, i, 0);
+	spi_config_reg(info, 6, 1); /* disable OUTSLK */
+	spi_config_reg(info, 4, 1); /* Enable slow oscillator */
+	for (i = 1; i <= 3; ++i)
+		spi_config_reg(info, i, 0);
+	/* Program counters correctly for rounds processing, here it should
+	 * start consuming power */
+	spi_add_data(info, 0x0100, nf1_counters, 16);
+}
+
+void spi_send_init(struct bitfury_info *info)
+{
+	/* Prepare internal buffers */
+	/* PREPARE BUFFERS (INITIAL PROGRAMMING) */
+	unsigned int w[16];
+
+	if (!atrvec_set) {
+		atrvec_set = true;
+		ms3steps(atrvec);
+	}
+	memset(w, 0, sizeof(w));
+	w[3] = 0xffffffff;
+	w[4] = 0x80000000;
+	w[15] = 0x00000280;
+	spi_add_data(info, 0x1000, w, 16 * 4);
+	spi_add_data(info, 0x1400, w, 8 * 4);
+	memset(w, 0, sizeof(w));
+	w[0] = 0x80000000;
+	w[7] = 0x100;
+	spi_add_data(info, 0x1900, w, 8 * 4); /* Prepare MS and W buffers! */
+	spi_add_data(info, 0x3000, atrvec, 19 * 4);
+}
+void spi_clear_buf(struct bitfury_info *info)
+{
+	info->spibufsz = 0;
+}
+
+void spi_add_buf(struct bitfury_info *info, const void *buf, const int sz)
+{
+	if (unlikely(info->spibufsz + sz > SPIBUF_SIZE)) {
+		applog(LOG_WARNING, "SPI bufsize overflow!");
+		return;
+	}
+	memcpy(&info->spibuf[info->spibufsz], buf, sz);
+	info->spibufsz += sz;
+}
+
+void spi_add_break(struct bitfury_info *info)
+{
+	spi_add_buf(info, "\x4", 1);
+}
+
+static void spi_add_buf_reverse(struct bitfury_info *info, const char *buf, const int sz)
+{
+	int i;
+
+	for (i = 0; i < sz; i++) { // Reverse bit order in each byte!
+		unsigned char p = buf[i];
+
+		p = ((p & 0xaa) >> 1) | ((p & 0x55) << 1);
+		p = ((p & 0xcc) >> 2) | ((p & 0x33) << 2);
+		p = ((p & 0xf0) >> 4) | ((p & 0x0f) << 4);
+		info->spibuf[info->spibufsz + i] = p;
+	}
+	info->spibufsz += sz;
+}
+
+void spi_add_data(struct bitfury_info *info, uint16_t addr, const void *buf, int len)
+{
+	unsigned char otmp[3];
+
+	if (len < 4 || len > 128) {
+		applog(LOG_WARNING, "Can't add SPI data size %d", len);
+		return;
+	}
+	len /= 4; /* Strip */
+	otmp[0] = (len - 1) | 0xE0;
+	otmp[1] = (addr >> 8) & 0xFF;
+	otmp[2] = addr & 0xFF;
+	spi_add_buf(info, otmp, 3);
+	len *= 4;
+	spi_add_buf_reverse(info, buf, len);
+}
+
+// Bit-banging reset... Each 3 reset cycles reset first chip in chain
+bool spi_reset(struct cgpu_info *bitfury, struct bitfury_info *info)
+{
+	struct mcp_settings *mcp = &info->mcp;
+	int r;
+
+	// SCK_OVRRIDE
+	mcp->value.pin[NF1_PIN_SCK_OVR] = MCP2210_GPIO_PIN_HIGH;
+	mcp->direction.pin[NF1_PIN_SCK_OVR] = MCP2210_GPIO_OUTPUT;
+	mcp->designation.pin[NF1_PIN_SCK_OVR] = MCP2210_PIN_GPIO;
+	if (!mcp2210_set_gpio_settings(bitfury, mcp))
+		return false;
+
+	for (r = 0; r < 16; ++r) {
+		char buf[1] = {0x81}; // will send this waveform: - _ _ _ _ _ _ -
+		unsigned int length = 1;
+
+		if (!mcp2210_spi_transfer(bitfury, &info->mcp, buf, &length))
+			return false;
+	}
+
+	// Deactivate override
+	mcp->direction.pin[NF1_PIN_SCK_OVR] = MCP2210_GPIO_INPUT;
+	if (!mcp2210_set_gpio_settings(bitfury, mcp))
+		return false;
+
+	return true;
+}
+
+bool spi_txrx(struct cgpu_info *bitfury, struct bitfury_info *info)
+{
+	unsigned int length, sendrcv;
+	int offset = 0;
+
+	length = info->spibufsz;
+	applog(LOG_DEBUG, "%s %d: SPI sending %u bytes total", bitfury->drv->name,
+	       bitfury->device_id, length);
+	while (length > MCP2210_TRANSFER_MAX) {
+		sendrcv = MCP2210_TRANSFER_MAX;
+		if (!mcp2210_spi_transfer(bitfury, &info->mcp, info->spibuf + offset, &sendrcv))
+			return false;
+		if (sendrcv != MCP2210_TRANSFER_MAX) {
+			applog(LOG_DEBUG, "%s %d: Send/Receive size mismatch sent %d received %d",
+			       bitfury->drv->name, bitfury->device_id, MCP2210_TRANSFER_MAX, sendrcv);
+		}
+		length -= MCP2210_TRANSFER_MAX;
+		offset += MCP2210_TRANSFER_MAX;
+	}
+	sendrcv = length;
+	if (!mcp2210_spi_transfer(bitfury, &info->mcp, info->spibuf + offset, &sendrcv))
+		return false;
+	if (sendrcv != length) {
+		applog(LOG_WARNING, "%s %d: Send/Receive size mismatch sent %d received %d",
+		       bitfury->drv->name, bitfury->device_id, length, sendrcv);
+		return false;
+	}
+	return true;
+}
+
+#define BT_OFFSETS 3
+
+bool bitfury_checkresults(struct thr_info *thr, struct work *work, uint32_t nonce)
+{
+	const uint32_t bf_offsets[] = {-0x800000, 0, -0x400000};
+	int i;
+
+	for (i = 0; i < BT_OFFSETS; i++) {
+		uint32_t noffset = nonce + bf_offsets[i];
+
+		if (test_nonce(work, noffset)) {
+			submit_tested_work(thr, work);
+			return true;
+		}
+	}
+	return false;
+}
+
+bool libbitfury_sendHashData(struct thr_info *thr, struct cgpu_info *bitfury,
+			     struct bitfury_info *info)
+{
+	static unsigned second_run;
+	unsigned *newbuf = info->newbuf;
+	unsigned *oldbuf = info->oldbuf;
+	struct bitfury_payload *p = &(info->payload);
+	struct bitfury_payload *op = &(info->opayload);
+	unsigned int localvec[20];
+
+	/* Programming next value */
+	memcpy(localvec, p, 20 * 4);
+	ms3steps(localvec);
+
+	spi_clear_buf(info);
+	spi_add_break(info);
+	spi_add_data(info, 0x3000, (void*)localvec, 19 * 4);
+	if (!spi_txrx(bitfury, info))
+		return false;
+
+	memcpy(newbuf, info->spibuf + 4, 17 * 4);
+
+	info->job_switched = newbuf[16] != oldbuf[16];
+
+	if (second_run && info->job_switched) {
+		int i;
+
+		for (i = 0; i < 16; i++) {
+			if (oldbuf[i] != newbuf[i] && info->owork) {
+				uint32_t nonce; //possible nonce
+
+				nonce = decnonce(newbuf[i]);
+				if (bitfury_checkresults(thr, info->owork, nonce))
+					info->nonces++;
+			}
+		}
+
+		memcpy(op, p, sizeof(struct bitfury_payload));
+		memcpy(oldbuf, newbuf, 17 * 4);
+	}
+
+	cgsleep_ms(BITFURY_REFRESH_DELAY);
+	second_run = 1;
+
+	return true;
+}
diff --git a/libbitfury.h b/libbitfury.h
new file mode 100644
index 0000000..79ed564
--- /dev/null
+++ b/libbitfury.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2014 Con Kolivas
+ *
+ * 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.
+ */
+
+#ifndef LIBBITFURY_H
+#define LIBBITFURY_H
+#include "miner.h"
+#include "driver-bitfury.h"
+
+void ms3steps(uint32_t *p);
+uint32_t decnonce(uint32_t in);
+void bitfury_work_to_payload(struct bitfury_payload *p, struct work *work);
+void spi_set_freq(struct bitfury_info *info);
+void spi_send_conf(struct bitfury_info *info);
+void spi_send_init(struct bitfury_info *info);
+void spi_clear_buf(struct bitfury_info *info);
+void spi_add_buf(struct bitfury_info *info, const void *buf, const int sz);
+void spi_add_break(struct bitfury_info *info);
+void spi_add_data(struct bitfury_info *info, uint16_t addr, const void *buf, int len);
+bool spi_reset(struct cgpu_info *bitfury, struct bitfury_info *info);
+bool spi_txrx(struct cgpu_info *bitfury, struct bitfury_info *info);
+bool bitfury_checkresults(struct thr_info *thr, struct work *work, uint32_t nonce);
+bool libbitfury_sendHashData(struct thr_info *thr, struct cgpu_info *bitfury,
+			     struct bitfury_info *info);
+
+#endif /* LIBBITFURY_H */
diff --git a/mcp2210.c b/mcp2210.c
new file mode 100644
index 0000000..83ff95f
--- /dev/null
+++ b/mcp2210.c
@@ -0,0 +1,366 @@
+/*
+ * Copyright 2014 Con Kolivas
+ *
+ * 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 "miner.h"
+#include "usbutils.h"
+#include "mcp2210.h"
+
+static bool mcp2210_send(struct cgpu_info *cgpu, char *buf, enum usb_cmds cmd)
+{
+	int amount, err;
+
+	if (unlikely(cgpu->usbinfo.nodev))
+		return false;
+
+	err = usb_write(cgpu, buf, MCP2210_BUFFER_LENGTH, &amount, cmd);
+	if (err || amount != MCP2210_BUFFER_LENGTH) {
+		applog(LOG_WARNING, "%s %d: Error %d sending %s sent %d of %d",
+		       cgpu->drv->name, cgpu->device_id, err, usb_cmdname(cmd),
+		       amount, MCP2210_BUFFER_LENGTH);
+		return false;
+	}
+	return true;
+}
+
+static bool mcp2210_recv(struct cgpu_info *cgpu, char *buf, enum usb_cmds cmd)
+{
+	int amount, err;
+
+	if (unlikely(cgpu->usbinfo.nodev))
+		return false;
+
+	err = usb_read(cgpu, buf, MCP2210_BUFFER_LENGTH, &amount, cmd);
+	if (err || amount != MCP2210_BUFFER_LENGTH) {
+		applog(LOG_WARNING, "%s %d: Error %d receiving %s received %d of %d",
+		       cgpu->drv->name, cgpu->device_id, err, usb_cmdname(cmd),
+		       amount, MCP2210_BUFFER_LENGTH);
+		return false;
+	}
+	return true;
+}
+
+bool mcp2210_send_recv(struct cgpu_info *cgpu, char *buf, enum usb_cmds cmd)
+{
+	uint8_t mcp_cmd = buf[0];
+
+	if (!mcp2210_send(cgpu, buf, cmd))
+		return false;
+
+	if (!mcp2210_recv(cgpu, buf, cmd))
+		return false;
+
+	/* Return code should always echo original command */
+	if (buf[0] != mcp_cmd) {
+		applog(LOG_WARNING, "%s %d: Response code mismatch, asked for %u got %u",
+		       cgpu->drv->name, cgpu->device_id, mcp_cmd, buf[0]);
+		return false;
+	}
+	return true;
+}
+
+bool mcp2210_get_gpio_settings(struct cgpu_info *cgpu, struct mcp_settings *mcp)
+{
+	char buf[MCP2210_BUFFER_LENGTH];
+	int i;
+
+	memset(buf, 0, MCP2210_BUFFER_LENGTH);
+	buf[0] = MCP2210_GET_GPIO_SETTING;
+	if (!mcp2210_send_recv(cgpu, buf, C_MCP_GETGPIOSETTING))
+		return false;
+
+	for (i = 0; i < 8; i++) {
+		mcp->designation.pin[i] = buf[4 + i];
+		mcp->value.pin[i] = !!(buf[13] & (0x01u << i));
+		mcp->direction.pin[i] = !!(buf[15] & (0x01u << i));
+	}
+	mcp->designation.pin[8] = buf[12];
+	mcp->value.pin[8] = buf[14] & 0x01u;
+	mcp->direction.pin[8] = buf[16] & 0x01u;
+
+	return true;
+}
+
+bool mcp2210_set_gpio_settings(struct cgpu_info *cgpu, struct mcp_settings *mcp)
+{
+	char buf[MCP2210_BUFFER_LENGTH];
+	uint8_t buf17;
+	int i;
+
+	memset(buf, 0, MCP2210_BUFFER_LENGTH);
+	buf[0] = MCP2210_GET_GPIO_SETTING;
+	if (!mcp2210_send_recv(cgpu, buf, C_MCP_GETGPIOSETTING))
+		return false;
+	buf17 = buf[17];
+
+	memset(buf, 0, MCP2210_BUFFER_LENGTH);
+	buf[0] = MCP2210_SET_GPIO_SETTING;
+	buf[17] = buf17;
+	for (i = 0; i < 8; i++) {
+		buf[4 + i] = mcp->designation.pin[i];
+		buf[13] |= mcp->value.pin[i] << i;
+		buf[15] |= mcp->direction.pin[i] << i;
+	}
+	buf[12] = mcp->designation.pin[8];
+	buf[14] = mcp->value.pin[8];
+	buf[16] = mcp->direction.pin[8];
+	return mcp2210_send_recv(cgpu, buf, C_MCP_SETGPIOSETTING);
+}
+
+/* Get all the pin designations and store them in a gpio_pin struct */
+bool mcp2210_get_gpio_pindes(struct cgpu_info *cgpu, struct gpio_pin *gp)
+{
+	char buf[MCP2210_BUFFER_LENGTH];
+	int i;
+
+	memset(buf, 0, MCP2210_BUFFER_LENGTH);
+	buf[0] = MCP2210_GET_GPIO_SETTING;
+	if (!mcp2210_send_recv(cgpu, buf, C_MCP_GETGPIOSETTING))
+		return false;
+
+	for (i = 0; i < 9; i++)
+		gp->pin[i] = buf[4 + i];
+	return true;
+}
+
+
+/* Get all the pin vals and store them in a gpio_pin struct */
+bool mcp2210_get_gpio_pinvals(struct cgpu_info *cgpu, struct gpio_pin *gp)
+{
+	char buf[MCP2210_BUFFER_LENGTH];
+	int i;
+
+	memset(buf, 0, MCP2210_BUFFER_LENGTH);
+	buf[0] = MCP2210_GET_GPIO_PIN_VAL;
+	if (!mcp2210_send_recv(cgpu, buf, C_MCP_GETGPIOPINVAL))
+		return false;
+
+	for (i = 0; i < 8; i++)
+		gp->pin[i] = !!(buf[4] & (0x01u << i));
+	gp->pin[8] = buf[5] & 0x01u;
+
+	return true;
+}
+
+/* Get all the pindirs */
+bool mcp2210_get_gpio_pindirs(struct cgpu_info *cgpu, struct gpio_pin *gp)
+{
+	char buf[MCP2210_BUFFER_LENGTH];
+	int i;
+
+	memset(buf, 0, MCP2210_BUFFER_LENGTH);
+	buf[0] = MCP2210_GET_GPIO_PIN_DIR;
+	if (!mcp2210_send_recv(cgpu, buf, C_MCP_GETGPIOPINDIR))
+		return false;
+
+	for (i = 0; i < 8; i++)
+		gp->pin[i] = !!(buf[4] & (0x01u << i));
+	gp->pin[8] = buf[5] & 0x01u;
+
+	return true;
+}
+
+/* Get the designation of one pin */
+bool mcp2210_get_gpio_pin(struct cgpu_info *cgpu, int pin, int *des)
+{
+	struct gpio_pin gp;
+
+	if (!mcp2210_get_gpio_pindes(cgpu, &gp))
+		return false;
+
+	*des = gp.pin[pin];
+	return true;
+}
+
+/* Get one pinval */
+bool mcp2210_get_gpio_pinval(struct cgpu_info *cgpu, int pin, int *val)
+{
+	char buf[MCP2210_BUFFER_LENGTH];
+
+	memset(buf, 0, MCP2210_BUFFER_LENGTH);
+	buf[0] = MCP2210_GET_GPIO_PIN_VAL;
+	if (!mcp2210_send_recv(cgpu, buf, C_MCP_GETGPIOPINVAL))
+		return false;
+
+	buf[0] = MCP2210_GET_GPIO_PIN_VAL;
+
+	if (pin < 8)
+		*val = !!(buf[4] & (0x01u << pin));
+	else
+		*val = !!(buf[5] & 0x01u);
+
+	return true;
+}
+
+/* Get one pindir */
+bool mcp2210_get_gpio_pindir(struct cgpu_info *cgpu, int pin, int *dir)
+{
+	char buf[MCP2210_BUFFER_LENGTH];
+
+	memset(buf, 0, MCP2210_BUFFER_LENGTH);
+	buf[0] = MCP2210_GET_GPIO_PIN_DIR;
+	if (!mcp2210_send_recv(cgpu, buf, C_MCP_GETGPIOPINDIR))
+		return false;
+
+	buf[0] = MCP2210_GET_GPIO_PIN_DIR;
+
+	if (pin < 8)
+		*dir = !!(buf[4] & (0x01u << pin));
+	else
+		*dir = !!(buf[5] & 0x01u);
+
+	return true;
+}
+
+bool mcp2210_spi_cancel(struct cgpu_info *cgpu)
+{
+	char buf[MCP2210_BUFFER_LENGTH];
+
+	memset(buf, 0, MCP2210_BUFFER_LENGTH);
+	buf[0] = MCP2210_SPI_CANCEL;
+	return mcp2210_send_recv(cgpu, buf, C_MCP_SPICANCEL);
+}
+
+/* Abbreviations correspond to:
+ * IdleChipSelectValue, ActiveChipSelectValue, CSToDataDelay, LastDataByteToCSDelay,
+ * SubsequentDataByteDelay, BytesPerSPITransfer
+ */
+bool
+mcp2210_get_spi_transfer_settings(struct cgpu_info *cgpu, unsigned int *bitrate, unsigned int *icsv,
+				  unsigned int *acsv, unsigned int *cstdd, unsigned int *ldbtcsd,
+				  unsigned int *sdbd, unsigned int *bpst, unsigned int *spimode)
+{
+	char buf[MCP2210_BUFFER_LENGTH];
+
+	memset(buf, 0, MCP2210_BUFFER_LENGTH);
+	buf[0] = MCP2210_GET_SPI_SETTING;
+
+	if (!mcp2210_send_recv(cgpu, buf, C_MCP_GETSPISETTING))
+		return false;
+	*bitrate = buf[7] << 24 | buf[6] << 16 | buf[5] << 8 | buf[4];
+	*icsv = (buf[9] & 0x1) << 8 | buf[8];
+	*acsv = (buf[11] & 0x1) << 8 | buf[10];
+	*cstdd = buf[13] << 8 | buf[12];
+	*ldbtcsd = buf[15] << 8 | buf[14];
+	*sdbd = buf[17] << 8 | buf[16];
+	*bpst = buf[19] << 8 | buf[18];
+	*spimode = buf[20];
+	return true;
+}
+
+bool
+mcp2210_set_spi_transfer_settings(struct cgpu_info *cgpu, unsigned int bitrate, unsigned int icsv,
+				  unsigned int acsv, unsigned int cstdd, unsigned int ldbtcsd,
+				  unsigned int sdbd, unsigned int bpst, unsigned int spimode)
+{
+	char buf[MCP2210_BUFFER_LENGTH];
+	bool ret;
+
+	memset(buf, 0, MCP2210_BUFFER_LENGTH);
+	buf[0] = MCP2210_SET_SPI_SETTING;
+
+	buf[4] = bitrate & 0xfful;
+	buf[5] = (bitrate & 0xff00ul) >> 8;
+	buf[6] = (bitrate & 0xff0000ul) >> 16;
+	buf[7] = (bitrate & 0xff000000ul) >> 24;
+
+	buf[8] = icsv & 0xff;
+	buf[9] = (icsv & 0x100) >> 8;
+
+	buf[10] = acsv & 0xff;
+	buf[11] = (acsv & 0x100) >> 8;
+
+	buf[12] = cstdd & 0xff;
+	buf[13] = (cstdd & 0xff00) >> 8;
+
+	buf[14] = ldbtcsd & 0xff;
+	buf[15] = (ldbtcsd & 0xff00) >> 8;
+
+	buf[16] = sdbd & 0xff;
+	buf[17] = (sdbd & 0xff00) >> 8;
+
+	buf[18] = bpst & 0xff;
+	buf[19] = (bpst & 0xff00) >> 8;
+
+	buf[20] = spimode;
+	ret = mcp2210_send_recv(cgpu, buf, C_MCP_SETSPISETTING);
+	if (!ret)
+		return ret;
+	if (buf[1] != 0) {
+		applog(LOG_DEBUG, "Failed to set spi settings");
+		return false;
+	}
+	return true;
+}
+
+/* Perform an spi transfer of *length bytes and return the amount of data
+ * returned in the same buffer in *length */
+bool mcp2210_spi_transfer(struct cgpu_info *cgpu, struct mcp_settings *mcp,
+			  char *data, unsigned int *length)
+{
+	uint8_t res, status, orig_len, offset = 0;
+	char buf[MCP2210_BUFFER_LENGTH];
+
+	if (unlikely(*length > MCP2210_TRANSFER_MAX || !*length)) {
+		applog(LOG_ERR, "%s %d: Unable to spi transfer %u bytes", cgpu->drv->name,
+		       cgpu->device_id, *length);
+		return false;
+	}
+	if (mcp->bpst != *length) {
+		/* Set the transfer setting only when it changes. */
+		mcp->bpst = *length;
+		if (!mcp2210_set_spi_transfer_settings(cgpu, mcp->bitrate, mcp->icsv,
+		    mcp->acsv, mcp->cstdd, mcp->ldbtcsd, mcp->sdbd, mcp->bpst, mcp->spimode))
+			return false;
+	}
+	orig_len = *length;
+retry:
+	applog(LOG_DEBUG, "%s %d: SPI sending %u bytes", cgpu->drv->name, cgpu->device_id,
+	       *length);
+	memset(buf, 0, MCP2210_BUFFER_LENGTH);
+	buf[0] = MCP2210_SPI_TRANSFER;
+	buf[1] = *length;
+
+	if (*length)
+		memcpy(buf + 4, data + offset, *length);
+	if (!mcp2210_send_recv(cgpu, buf, C_MCP_SPITRANSFER))
+		return false;
+
+	res = (uint8_t)buf[1];
+	switch(res) {
+		case MCP2210_SPI_TRANSFER_SUCCESS:
+			*length = buf[2];
+			status = buf[3];
+			applog(LOG_DEBUG, "%s %d: SPI transfer success, received %u bytes status 0x%x",
+			       cgpu->drv->name, cgpu->device_id, *length, status);
+			if (*length) {
+				memcpy(data + offset, buf + 4, *length);
+				offset += *length;
+			}
+			if (status == 0x30) {
+				/* This shouldn't happen */
+				applog(LOG_DEBUG, "%s %d: SPI expecting more data inappropriately",
+				       cgpu->drv->name, cgpu->device_id);
+				return false;
+			}
+			if (offset < orig_len) {
+				*length = 0;
+				goto retry;
+			}
+			*length = orig_len;
+			return true;
+		case MCP2210_SPI_TRANSFER_ERROR_IP:
+			applog(LOG_DEBUG, "%s %d: SPI transfer error in progress",
+			       cgpu->drv->name, cgpu->device_id);
+			goto retry;
+		case MCP2210_SPI_TRANSFER_ERROR_NA:
+			applog(LOG_WARNING, "%s %d: External owner error on mcp2210 spi transfer",
+			       cgpu->drv->name, cgpu->device_id);
+		default:
+			return false;
+	}
+}
diff --git a/mcp2210.h b/mcp2210.h
new file mode 100644
index 0000000..cb75443
--- /dev/null
+++ b/mcp2210.h
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2014 Con Kolivas
+ *
+ * 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.
+ */
+
+#ifndef MCP2210_H
+#define MCP2210_H
+
+#define MCP2210_BUFFER_LENGTH		64
+#define MCP2210_TRANSFER_MAX		60
+
+#define MCP2210_PIN_GPIO		0x0
+#define MCP2210_PIN_CS			0x1
+#define MCP2210_PIN_DEDICATED		0x2
+
+#define MCP2210_GPIO_PIN_LOW		0
+#define MCP2210_GPIO_PIN_HIGH		1
+
+#define MCP2210_GPIO_OUTPUT		0
+#define MCP2210_GPIO_INPUT		1
+
+#define MCP2210_SPI_CANCEL		0x11
+#define MCP2210_GET_GPIO_SETTING	0x20
+#define MCP2210_SET_GPIO_SETTING	0x21
+#define MCP2210_SET_GPIO_PIN_VAL	0x30
+#define MCP2210_GET_GPIO_PIN_VAL	0x31
+#define MCP2210_SET_GPIO_PIN_DIR	0x32
+#define MCP2210_GET_GPIO_PIN_DIR	0x33
+#define MCP2210_SET_SPI_SETTING		0X40
+#define MCP2210_GET_SPI_SETTING		0X41
+#define MCP2210_SPI_TRANSFER		0x42
+
+#define MCP2210_SPI_TRANSFER_SUCCESS	0x00
+#define MCP2210_SPI_TRANSFER_ERROR_NA	0xF7	// SPI not available due to external owner
+#define MCP2210_SPI_TRANSFER_ERROR_IP	0xF8	// SPI not available due to transfer in progress
+
+struct gpio_pin {
+	uint8_t pin[9];
+};
+
+struct mcp_settings {
+	struct gpio_pin designation;
+	struct gpio_pin value;
+	struct gpio_pin direction;
+	unsigned int bitrate, icsv, acsv, cstdd, ldbtcsd, sdbd, bpst, spimode;
+};
+
+bool mcp2210_send_recv(struct cgpu_info *cgpu, char *buf, enum usb_cmds cmd);
+bool mcp2210_get_gpio_settings(struct cgpu_info *cgpu, struct mcp_settings *mcp);
+bool mcp2210_set_gpio_settings(struct cgpu_info *cgpu, struct mcp_settings *mcp);
+bool mcp2210_get_gpio_pindes(struct cgpu_info *cgpu, struct gpio_pin *gp);
+bool mcp2210_get_gpio_pinvals(struct cgpu_info *cgpu, struct gpio_pin *gp);
+bool mcp2210_get_gpio_pindirs(struct cgpu_info *cgpu, struct gpio_pin *gp);
+bool mcp2210_get_gpio_pin(struct cgpu_info *cgpu, int pin, int *des);
+bool mcp2210_get_gpio_pinval(struct cgpu_info *cgpu, int pin, int *val);
+bool mcp2210_get_gpio_pindir(struct cgpu_info *cgpu, int pin, int *dir);
+bool mcp2210_spi_cancel(struct cgpu_info *cgpu);
+bool
+mcp2210_get_spi_transfer_settings(struct cgpu_info *cgpu, unsigned int *bitrate, unsigned int *icsv,
+				  unsigned int *acsv, unsigned int *cstdd, unsigned int *ldbtcsd,
+				  unsigned int *sdbd, unsigned int *bpst, unsigned int *spimode);
+bool
+mcp2210_set_spi_transfer_settings(struct cgpu_info *cgpu, unsigned int bitrate, unsigned int icsv,
+				  unsigned int acsv, unsigned int cstdd, unsigned int ldbtcsd,
+				  unsigned int sdbd, unsigned int bpst, unsigned int spimode);
+bool mcp2210_spi_transfer(struct cgpu_info *cgpu, struct mcp_settings *mcp,
+			  char *data, unsigned int *length);
+
+#endif /* MCP2210_H */
diff --git a/usbutils.c b/usbutils.c
index 3556ed9..a56fcf9 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -153,6 +153,15 @@ static struct usb_intinfo bxf_ints[] = {
 	USB_EPS(1,  bxf1_epinfos),
 	USB_EPS(0,  bxf0_epinfos)
 };
+
+static struct usb_epinfo nf1_epinfos[] = {
+	{ LIBUSB_TRANSFER_TYPE_INTERRUPT,	64,	EPI(1), 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_INTERRUPT,	64,	EPO(1), 0, 0 },
+};
+
+static struct usb_intinfo nf1_ints[] = {
+	USB_EPS(0, nf1_epinfos)
+};
 #endif
 
 #ifdef USE_DRILLBIT
@@ -368,6 +377,17 @@ static struct usb_find_devices find_dev[] = {
 		.iProduct = "bi?fury",
 		INTINFO(bxf_ints)
 	},
+	{
+		.drv = DRIVER_bitfury,
+		.name = "NF1",
+		.ident = IDENT_NF1,
+		.idVendor = 0x04d8,
+		.idProduct = 0x00de,
+		.config = 1,
+		.timeout = BITFURY_TIMEOUT_MS,
+		.latency = LATENCY_UNUSED,
+		INTINFO(nf1_ints)
+	},
 #endif
 #ifdef USE_DRILLBIT
 	{
@@ -2468,7 +2488,7 @@ static int usb_submit_transfer(struct usb_transfer *ut, struct libusb_transfer *
 }
 
 static int
-usb_bulk_transfer(struct cgpu_info *cgpu, struct cg_usb_device *usbdev, int intinfo,
+usb_perform_transfer(struct cgpu_info *cgpu, struct cg_usb_device *usbdev, int intinfo,
 		  int epinfo, unsigned char *data, int length, int *transferred,
 		  unsigned int timeout, __maybe_unused int mode, enum usb_cmds cmd,
 		  __maybe_unused int seq, bool cancellable, bool tt)
@@ -2478,6 +2498,7 @@ usb_bulk_transfer(struct cgpu_info *cgpu, struct cg_usb_device *usbdev, int inti
 	struct usb_epinfo *usb_epinfo;
 	struct usb_transfer ut;
 	unsigned char endpoint;
+	bool interrupt;
 	int err, errn;
 #if DO_USB_STATS
 	struct timeval tv_start, tv_finish;
@@ -2494,6 +2515,7 @@ usb_bulk_transfer(struct cgpu_info *cgpu, struct cg_usb_device *usbdev, int inti
 #endif
 
 	usb_epinfo = &(usbdev->found->intinfos[intinfo].epinfos[epinfo]);
+	interrupt = usb_epinfo->att == LIBUSB_TRANSFER_TYPE_INTERRUPT;
 	endpoint = usb_epinfo->ep;
 
 	/* Avoid any async transfers during shutdown to allow the polling
@@ -2507,8 +2529,10 @@ pipe_retry:
 		memcpy(buf, data, length);
 #ifndef HAVE_LIBUSB
 		/* Older versions may not have this feature so only enable it
-		 * when we know we're compiling with included static libusb */
-		ut.transfer->flags |= LIBUSB_TRANSFER_ADD_ZERO_PACKET;
+		 * when we know we're compiling with included static libusb. We
+		 * only do this for bulk transfer, not interrupt. */
+		if (!interrupt)
+			ut.transfer->flags |= LIBUSB_TRANSFER_ADD_ZERO_PACKET;
 #endif
 #ifdef WIN32
 		/* Writes on windows really don't like to be cancelled, but
@@ -2519,10 +2543,16 @@ pipe_retry:
 #endif
 	}
 
-	USBDEBUG("USB debug: @usb_bulk_transfer(%s (nodev=%s),intinfo=%d,epinfo=%d,data=%p,length=%d,timeout=%u,mode=%d,cmd=%s,seq=%d) endpoint=%d", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), intinfo, epinfo, data, length, timeout, mode, usb_cmdname(cmd), seq, (int)endpoint);
+	USBDEBUG("USB debug: @usb_perform_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);
 
-	libusb_fill_bulk_transfer(ut.transfer, dev_handle, endpoint, buf, length,
-				  transfer_callback, &ut, bulk_timeout);
+	if (interrupt) {
+		libusb_fill_interrupt_transfer(ut.transfer, dev_handle, endpoint,
+					       buf, length, transfer_callback, &ut,
+				 bulk_timeout);
+	} else {
+		libusb_fill_bulk_transfer(ut.transfer, dev_handle, endpoint, buf,
+					  length, transfer_callback, &ut, bulk_timeout);
+	}
 	STATS_TIMEVAL(&tv_start);
 	err = usb_submit_transfer(&ut, ut.transfer, cancellable, tt);
 	errn = errno;
@@ -2565,15 +2595,16 @@ pipe_retry:
 	return err;
 }
 
-int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz, int *processed, int timeout, const char *end, enum usb_cmds cmd, bool readonce, bool cancellable)
+int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz,
+	      int *processed, int timeout, const char *end, enum usb_cmds cmd, bool readonce, bool cancellable)
 {
 	unsigned char *ptr, usbbuf[USB_READ_BUFSIZE];
 	struct timeval read_start, tv_finish;
 	int bufleft, err, got, tot, pstate;
-	const size_t usbbufread = 512; /* Always read full size */
 	struct cg_usb_device *usbdev;
 	unsigned int initial_timeout;
 	bool first = true;
+	size_t usbbufread;
 	int endlen = 0;
 	char *eom = NULL;
 	double done;
@@ -2586,6 +2617,12 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
 		endlen = strlen(end);
 
 	DEVRLOCK(cgpu, pstate);
+	usbdev = cgpu->usbdev;
+	/* Interrupt transfers are guaranteed to be of an expected size (we hope) */
+	if (usbdev->found->intinfos[intinfo].epinfos[epinfo].att == LIBUSB_TRANSFER_TYPE_INTERRUPT)
+		usbbufread = bufsiz;
+	else
+		usbbufread = 512;
 
 	if (cgpu->usbinfo.nodev) {
 		*processed = 0;
@@ -2595,7 +2632,6 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
 		goto out_noerrmsg;
 	}
 
-	usbdev = cgpu->usbdev;
 	ftdi = (usbdev->usb_type == USB_TYPE_FTDI);
 
 	USBDEBUG("USB debug: _usb_read(%s (nodev=%s),intinfo=%d,epinfo=%d,buf=%p,bufsiz=%d,proc=%p,timeout=%u,end=%s,cmd=%s,ftdi=%s,readonce=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), intinfo, epinfo, buf, (int)bufsiz, processed, timeout, end ? (char *)str_text((char *)end) : "NULL", usb_cmdname(cmd), bool_str(ftdi), bool_str(readonce));
@@ -2620,7 +2656,7 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
 	initial_timeout = timeout;
 	cgtime(&read_start);
 	while (bufleft > 0 && !eom) {
-		err = usb_bulk_transfer(cgpu, usbdev, intinfo, epinfo, ptr, usbbufread,
+		err = usb_perform_transfer(cgpu, usbdev, intinfo, epinfo, ptr, usbbufread,
 					&got, timeout, MODE_BULK_READ, cmd,
 					first ? SEQ0 : SEQ1, cancellable, false);
 		cgtime(&tv_finish);
@@ -2657,11 +2693,14 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
 				       cgpu->drv->name, cgpu->device_id, err, libusb_error_name(err));
 			}
 		}
+		ptr += got;
+		bufleft -= got;
+		if (bufleft < 1)
+			err = LIBUSB_SUCCESS;
+
 		if (err || readonce)
 			break;
 
-		ptr += got;
-		bufleft -= got;
 
 		first = false;
 
@@ -2754,7 +2793,7 @@ int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_
 					tosend = 1;
 			}
 		}
-		err = usb_bulk_transfer(cgpu, usbdev, intinfo, epinfo, (unsigned char *)buf,
+		err = usb_perform_transfer(cgpu, usbdev, intinfo, epinfo, (unsigned char *)buf,
 					tosend, &sent, timeout, MODE_BULK_WRITE,
 					cmd, first ? SEQ0 : SEQ1, false, usb11);
 		cgtime(&tv_finish);
diff --git a/usbutils.h b/usbutils.h
index 91d15ee..e308565 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -148,12 +148,13 @@ enum sub_ident {
 	IDENT_BXF,
 	IDENT_CMR1,
 	IDENT_CMR2,
+	IDENT_DRB,
 	IDENT_HFA,
 	IDENT_ICA,
 	IDENT_KLN,
 	IDENT_LLT,
 	IDENT_MMQ,
-	IDENT_DRB
+	IDENT_NF1
 };
 
 struct usb_find_devices {
@@ -385,7 +386,17 @@ struct cg_usb_info {
 	USB_ADD_COMMAND(C_HF_NOTICE, "HFNotice") \
 	USB_ADD_COMMAND(C_HF_GETHEADER, "HFGetHeader") \
 	USB_ADD_COMMAND(C_HF_GETDATA, "HFGetData") \
-	USB_ADD_COMMAND(C_HF_CLEAR_READ, "HFClearRead")
+	USB_ADD_COMMAND(C_HF_CLEAR_READ, "HFClearRead") \
+	USB_ADD_COMMAND(C_MCP_GETGPIOSETTING, "MCPGetGPIOSetting") \
+	USB_ADD_COMMAND(C_MCP_SETGPIOSETTING, "MCPSetGPIOSetting") \
+	USB_ADD_COMMAND(C_MCP_GETGPIOPINVAL, "MCPGetGPIOPinVal") \
+	USB_ADD_COMMAND(C_MCP_SETGPIOPINVAL, "MCPSetGPIOPinVal") \
+	USB_ADD_COMMAND(C_MCP_GETGPIOPINDIR, "MCPGetGPIOPinDir") \
+	USB_ADD_COMMAND(C_MCP_SETGPIOPINDIR, "MCPSetGPIOPinDir") \
+	USB_ADD_COMMAND(C_MCP_SETSPISETTING, "MCPSetSPISetting") \
+	USB_ADD_COMMAND(C_MCP_GETSPISETTING, "MCPGetSPISetting") \
+	USB_ADD_COMMAND(C_MCP_SPITRANSFER, "MCPSPITransfer") \
+	USB_ADD_COMMAND(C_MCP_SPICANCEL, "MCPSPICancel")
 
 /* Create usb_cmds enum from USB_PARSE_COMMANDS macro */
 enum usb_cmds {