Commit 97a13df46b051c65561b5a7081d157c0f937e436

Kano 2013-12-23T06:58:26

Merge remote-tracking branch 'drill/master' into drill

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
diff --git a/Makefile.am b/Makefile.am
index 6316cde..2311ead 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -76,6 +76,10 @@ if HAS_BITFURY
 cgminer_SOURCES += driver-bitfury.c driver-bitfury.h
 endif
 
+if HAS_DRILLBIT
+cgminer_SOURCES += driver-drillbit.c driver-drillbit.h
+endif
+
 if HAS_ICARUS
 cgminer_SOURCES += driver-icarus.c
 endif
diff --git a/cgminer.c b/cgminer.c
index 6c4ea1a..5bf1949 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -171,6 +171,9 @@ char *opt_bitburner_fury_options = NULL;
 #ifdef USE_KLONDIKE
 char *opt_klondike_options = NULL;
 #endif
+#ifdef USE_DRILLBIT
+char *opt_drillbit_options = NULL;
+#endif
 #ifdef USE_USBUTILS
 char *opt_usb_select = NULL;
 int opt_usbdump = -1;
@@ -1051,6 +1054,15 @@ static char *set_klondike_options(const char *arg)
 }
 #endif
 
+#ifdef USE_DRILLBIT
+static char *set_drillbit_options(const char *arg)
+{
+	opt_set_charp(arg, &opt_drillbit_options);
+
+	return NULL;
+}
+#endif
+
 #ifdef USE_USBUTILS
 static char *set_usb_select(const char *arg)
 {
@@ -1213,6 +1225,11 @@ static struct opt_table opt_config_table[] = {
 		     set_klondike_options, NULL, NULL,
 		     "Set klondike options clock:temptarget"),
 #endif
+#ifdef USE_DRILLBIT
+        OPT_WITH_ARG("--drillbit-options",
+                     set_drillbit_options, NULL, NULL,
+                     "Set drillbit options <int|ext>:clock[:clock_divider][:voltage]"),
+#endif
 	OPT_WITHOUT_ARG("--load-balance",
 		     set_loadbalance, &pool_strategy,
 		     "Change multipool strategy from failover to quota based balance"),
@@ -1497,6 +1514,9 @@ static char *opt_verusage_and_exit(const char *extra)
 #ifdef USE_BITFURY
 		"bitfury "
 #endif
+#ifdef USE_DRILLBIT
+                "drillbit "
+#endif
 #ifdef USE_HASHFAST
 		"hashfast "
 #endif
@@ -4372,6 +4392,10 @@ void write_config(FILE *fcfg)
 	if (opt_klondike_options)
 		fprintf(fcfg, ",\n\"klondike-options\" : \"%s\"", json_escape(opt_icarus_options));
 #endif
+#ifdef USE_DRILLBIT
+        if (opt_drillbit_options)
+                fprintf(fcfg, ",\n\"drillbit-options\" : \"%s\"", json_escape(opt_drillbit_options));
+#endif
 #ifdef USE_USBUTILS
 	if (opt_usb_select)
 		fprintf(fcfg, ",\n\"usb\" : \"%s\"", json_escape(opt_usb_select));
@@ -7590,7 +7614,7 @@ bool add_cgpu(struct cgpu_info *cgpu)
 {
 	static struct _cgpu_devid_counter *devids = NULL;
 	struct _cgpu_devid_counter *d;
-	
+
 	HASH_FIND_STR(devids, cgpu->drv->name, d);
 	if (d)
 		cgpu->device_id = ++d->lastid;
diff --git a/configure.ac b/configure.ac
index 3d5f1c1..644f946 100644
--- a/configure.ac
+++ b/configure.ac
@@ -173,14 +173,25 @@ AM_CONDITIONAL([HAS_BITFORCE], [test x$bitforce = xyes])
 bitfury="no"
 
 AC_ARG_ENABLE([bitfury],
-	[AC_HELP_STRING([--enable-bitfury],[Compile support for BitFury ASICs (default disabled)])],
+	[AC_HELP_STRING([--enable-bitfury],[Compile support for BitFury BF1 (default disabled)])],
 	[bitfury=$enableval]
 	)
 if test "x$bitfury" = xyes; then
-	AC_DEFINE([USE_BITFURY], [1], [Defined to 1 if BitFury support is wanted])
+	AC_DEFINE([USE_BITFURY], [1], [Defined to 1 if BitFury BF1 support is wanted])
 fi
 AM_CONDITIONAL([HAS_BITFURY], [test x$bitfury = xyes])
 
+drillbit="no"
+
+AC_ARG_ENABLE([drillbit],
+	[AC_HELP_STRING([--enable-drillbit],[Compile support for Drillbit BitFury ASICs (default disabled)])],
+	[drillbit=$enableval]
+	)
+if test "x$drillbit" = xyes; then
+	AC_DEFINE([USE_DRILLBIT], [1], [Defined to 1 if Drillbit BitFury support is wanted])
+fi
+AM_CONDITIONAL([HAS_DRILLBIT], [test x$drillbit = xyes])
+
 hashfast="no"
 
 AC_ARG_ENABLE([hashfast],
@@ -260,7 +271,7 @@ else
 	])
 fi
 
-if test x$avalon$bitforce$bitfury$modminer$bflsc$icarus$hashfast$klondike != xnononononononono; then
+if test x$avalon$bitforce$bitfury$modminer$bflsc$icarus$hashfast$klondike$drillbit != xnonononononononono; then
 	want_usbutils=true
 else
 	want_usbutils=false
@@ -458,9 +469,15 @@ else
 fi
 
 if test "x$bitfury" = xyes; then
-	echo "  BitFury.ASICs........: Enabled"
+	echo "  BitFury.BF1........: Enabled"
+else
+	echo "  BitFury.BF1........: Disabled"
+fi
+
+if test "x$drillbit" = xyes; then
+	echo "  Drillbit.BitFury........: Enabled"
 else
-	echo "  BitFury.ASICs........: Disabled"
+	echo "  Drillbit.BitFury........: Disabled"
 fi
 
 if test "x$hashfast" = xyes; then
@@ -493,7 +510,7 @@ else
 	echo "  ModMiner.FPGAs.......: Disabled"
 fi
 
-if test "x$avalon$bab$bflsc$bitforce$bitfury$hashfast$icarus$klondike$knc$modminer" = xnononononononononono; then
+if test "x$avalon$bab$bflsc$bitforce$bitfury$hashfast$icarus$klondike$knc$modminer$drillbit" = xnonononononononononono; then
 	AC_MSG_ERROR([No mining configured in])
 fi
 
diff --git a/driver-drillbit.c b/driver-drillbit.c
new file mode 100644
index 0000000..5596916
--- /dev/null
+++ b/driver-drillbit.c
@@ -0,0 +1,993 @@
+
+/*
+ * Copyright 2013 Con Kolivas
+ * Copyright 2013 Angus Gratton
+ * Copyright 2013 James Nichols
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.  See COPYING for more details.
+ */
+
+#include "config.h"
+
+#include "miner.h"
+#include "driver-drillbit.h"
+#include "sha2.h"
+
+#define TIMEOUT 3000
+#define MAX_RESULTS 16 // max results from a single chip
+
+#define drvlog(prio, fmt, ...) do {					\
+		if (opt_debug || prio != LOG_DEBUG) {			\
+			if (use_syslog || opt_log_output || prio <= opt_log_level) { \
+				char tmp42[LOGBUFSIZ];			\
+				snprintf(tmp42, sizeof(tmp42), "%s%d: "fmt, \
+					 drillbit->drv->name, drillbit->device_id, ##__VA_ARGS__); \
+				_applog(prio, tmp42, false);		\
+			}						\
+		}							\
+	} while (0)
+
+
+/* Request and response structsfor firmware */
+
+typedef struct
+{
+	uint16_t chip_id;
+	uint8_t midstate[32];
+	uint8_t data[12];
+} WorkRequest;
+
+#define SZ_SERIALISED_WORKREQUEST 46
+static void serialise_work_request(char *buf, uint16_t chip_id, const struct work *wr);
+
+typedef struct
+{
+	uint16_t chip_id;
+	uint8_t num_nonces;
+	uint8_t is_idle;
+	uint32_t nonce[MAX_RESULTS];
+} WorkResult;
+
+#define SZ_SERIALISED_WORKRESULT (4+4*MAX_RESULTS)
+static void deserialise_work_result(WorkResult *work_result, const char *buf);
+
+#define CONFIG_PW1 (1<<0)
+#define CONFIG_PW2 (1<<1)
+
+// Possible core voltage settings on PW1 & PW2
+#define CONFIG_CORE_065V 0
+#define CONFIG_CORE_075V CONFIG_PW2
+#define CONFIG_CORE_085V CONFIG_PW1
+#define CONFIG_CORE_095V (CONFIG_PW1|CONFIG_PW2)
+
+typedef struct
+{
+	uint8_t core_voltage; // Set to flags defined above
+	uint8_t int_clock_level; // Clock level (30-48 without divider), see asic.c for details
+	uint8_t clock_div2;	 // Apply the /2 clock divider (both internal and external)
+	uint8_t use_ext_clock; // Ignored on boards without external clocks
+	uint16_t ext_clock_freq;
+	uint16_t core_voltage_mv; // set to a plain human-readable integer value (not serialised atm)
+} BoardConfig;
+
+#define SZ_SERIALISED_BOARDCONFIG 6
+static void serialise_board_config(char *buf, const BoardConfig *boardconfig);
+
+typedef struct
+{
+	uint8_t protocol_version;
+	char product[8];
+	uint32_t serial;
+	uint8_t num_chips;
+	uint16_t capabilities;
+} Identity;
+
+/* Capabilities flags known to cgminer */
+#define CAP_TEMP 1
+#define CAP_EXT_CLOCK 2
+
+#define SZ_SERIALISED_IDENTITY 16
+static void deserialise_identity(Identity *identity, const char *buf);
+
+// Hashable structure of per-device config settings
+typedef struct {
+	char key[9];
+	BoardConfig config;
+	UT_hash_handle hh;
+} config_setting;
+
+/* Comparatively modest default settings */
+static config_setting default_settings = {
+key: { 0 },
+config: {
+	core_voltage: CONFIG_CORE_085V,
+	core_voltage_mv: 850,
+	use_ext_clock: 0,
+	int_clock_level: 40,
+	clock_div2: 0,
+	ext_clock_freq: 200
+},
+};
+
+static config_setting *settings;
+
+/* Return a pointer to the chip_info structure for a given chip id, or NULL otherwise */
+static struct drillbit_chip_info *find_chip(struct drillbit_info *info, uint16_t chip_id) {
+	int i;
+	for(i = 0; i < info->num_chips; i++) {
+		if(info->chips[i].chip_id == chip_id)
+			return &info->chips[i];
+	}
+	return NULL;
+}
+
+/* Read a fixed size buffer back from USB, returns true on success */
+static bool usb_read_fixed_size(struct cgpu_info *drillbit, void *result, size_t result_size, int timeout, enum usb_cmds command_name) {
+	char *res = (char *)result;
+	int ms_left;
+	size_t count;
+	struct timeval tv_now, tv_start;
+	int amount;
+
+	cgtime(&tv_start);
+	ms_left = timeout;
+
+	amount = 1;
+	count = 0;
+	while(count < result_size && ms_left > 0) {
+		usb_read_timeout(drillbit, &res[count], result_size-count, &amount, ms_left, command_name);
+		count += amount;
+		cgtime(&tv_now);
+		ms_left = timeout - ms_tdiff(&tv_now, &tv_start);
+	}
+	if(count == result_size) {
+		return true;
+	}
+	drvlog(LOG_ERR, "Read incomplete fixed size packet - got %zu bytes / %zu (timeout %d)", count, result_size, timeout);
+	return false;
+}
+
+static bool usb_read_simple_response(struct cgpu_info *drillbit, char command, enum usb_cmds command_name);
+
+/* Write a simple one-byte command and expect a simple one-byte response
+   Returns true on success
+*/
+static bool usb_send_simple_command(struct cgpu_info *drillbit, char command, enum usb_cmds command_name) {
+	int amount;
+	usb_write_timeout(drillbit, &command, 1, &amount, TIMEOUT, C_BF_REQWORK);
+	if(amount != 1) {
+		drvlog(LOG_ERR, "Failed to write command %c",command);
+		return false;
+	}
+	return usb_read_simple_response(drillbit, command, command_name);
+}
+
+
+/* Read a simple single-byte response and check it matches the correct command character
+   Return true on success
+*/
+static bool usb_read_simple_response(struct cgpu_info *drillbit, char command, enum usb_cmds command_name) {
+	int amount;
+	char response;
+	/* Expect a single byte, matching the command, as acknowledgement */
+	usb_read_timeout(drillbit, &response, 1, &amount, TIMEOUT, command_name);
+	if(amount != 1) {
+		drvlog(LOG_ERR, "Got no response to command %c",command);
+		return false;
+	}
+	if(response != command) {
+		drvlog(LOG_ERR, "Got unexpected response %c to command %c", response, command);	   
+		return false;
+	}
+	return true;
+}
+
+static void drillbit_empty_buffer(struct cgpu_info *drillbit)
+{
+	char buf[512];
+	int amount;
+
+	do {
+		usb_read_timeout(drillbit, buf, 512, &amount, 5, C_BF_FLUSH);
+	} while (amount);
+}
+
+static void drillbit_open(struct cgpu_info *drillbit)
+{
+	drillbit_empty_buffer(drillbit);
+}
+
+static void drillbit_close(struct cgpu_info *drillbit)
+{
+	struct drillbit_info *info = drillbit->device_data;
+	drillbit_empty_buffer(drillbit);
+	if(info->chips)
+		free(info->chips);
+}
+
+static void drillbit_identify(struct cgpu_info *drillbit)
+{
+	usb_send_simple_command(drillbit, 'L', C_BF_IDENTIFY);
+}
+
+static bool drillbit_getinfo(struct cgpu_info *drillbit, struct drillbit_info *info)
+{
+	int err;
+	int amount;
+	char buf[SZ_SERIALISED_IDENTITY];
+	Identity identity;
+
+	drillbit_empty_buffer(drillbit);
+	err = usb_write_timeout(drillbit, "I", 1, &amount, TIMEOUT, C_BF_REQINFO);
+	if (err) {
+		drvlog(LOG_INFO, "Failed to write REQINFO");
+		return false;
+	}
+	// can't call usb_read_fixed_size here as stats not initialised
+	err = usb_read_timeout(drillbit, buf, SZ_SERIALISED_IDENTITY, &amount, 1000, C_BF_GETINFO);
+	if (err) {
+		drvlog(LOG_ERR, "Failed to read GETINFO");
+		return false;
+	}
+	if (amount != SZ_SERIALISED_IDENTITY) {
+		drvlog(LOG_ERR, "Getinfo received %d bytes instead of %zu",
+		       amount, sizeof(Identity));
+		return false;
+	}
+	deserialise_identity(&identity, buf);
+
+	// sanity checks on the identity buffer we get back
+	if(strlen(identity.product) == 0 || identity.serial == 0 || identity.num_chips == 0) {
+		drvlog(LOG_ERR, "Got invalid contents for GETINFO identity response");
+		return false;
+	}
+
+	const int MIN_VERSION = 2;
+	const int MAX_VERSION = 3;
+	if(identity.protocol_version < MIN_VERSION) {
+		drvlog(LOG_ERR, "Unknown device protocol version %d.", identity.protocol_version);
+		return false;
+	}
+	if(identity.protocol_version > MAX_VERSION) {
+		drvlog(LOG_ERR, "Device firmware uses newer Drillbit protocol %d. We only support up to %d. Find a newer cgminer!", identity.protocol_version, MAX_VERSION);
+		return false;
+	}
+
+	if(identity.protocol_version == 2 && identity.num_chips == 1) {
+		// Production firmware Thumbs don't set any capability bits, so fill in the EXT_CLOCK one
+		identity.capabilities = CAP_EXT_CLOCK;
+	}
+
+	// load identity data into device info structure
+	info->version = identity.protocol_version;
+	if(strncmp(identity.product, "DRILLBIT", sizeof(identity.product)) == 0) {
+		// Hack: first production firmwares all described themselves as DRILLBIT, so fill in the gaps
+		if(identity.num_chips == 1)
+			strcpy(info->product, "Thumb");
+		else
+			strcpy(info->product, "Eight");
+	}
+	else {
+		memcpy(info->product, identity.product, sizeof(identity.product));
+	}
+	info->serial = identity.serial;
+	info->num_chips = identity.num_chips;
+	info->capabilities = identity.capabilities;
+
+	drvlog(LOG_INFO, "Getinfo returned version %d, product %s serial %08x num_chips %d",
+	       info->version, info->product, info->serial, info->num_chips);
+
+	drillbit_empty_buffer(drillbit);
+	return true;
+}
+
+static bool drillbit_reset(struct cgpu_info *drillbit)
+{
+	struct drillbit_info *info = drillbit->device_data;
+	struct drillbit_chip_info *chip;
+	int i, k, res;
+
+	res = usb_send_simple_command(drillbit, 'R', C_BF_REQRESET);
+
+	for(i = 0; i < info->num_chips; i++) {
+		chip = &info->chips[i];
+		chip->state = IDLE;
+		chip->work_sent_count = 0;
+		for(k = 0; k < WORK_HISTORY_LEN-1; k++) {
+			if(chip->current_work[k]) {
+				work_completed(drillbit, chip->current_work[k]);
+				chip->current_work[k] = NULL;
+			}
+		}
+	}
+
+	drillbit_empty_buffer(drillbit);
+	return res;
+}
+
+static config_setting *find_settings(struct cgpu_info *drillbit)
+{
+	struct drillbit_info *info = drillbit->device_data;
+	config_setting *setting;
+	char search_key[9];
+
+	// Search by serial (8 character hex string)
+	sprintf(search_key, "%08x", info->serial);
+	HASH_FIND_STR(settings, search_key, setting);
+	if(setting)  {
+		drvlog(LOG_INFO, "Using unit-specific settings for serial %s", search_key);
+		return setting;
+	}
+
+	// Search by DRBxxx
+	snprintf(search_key, 9, "DRB%d", drillbit->device_id);
+	HASH_FIND_STR(settings, search_key, setting);
+	if(setting) {
+		drvlog(LOG_INFO, "Using device_id specific settings for device");
+		return setting;
+	}
+
+	// Failing that, search by product name
+	HASH_FIND_STR(settings, info->product, setting);
+	if(setting) {
+		drvlog(LOG_INFO, "Using product-specific settings for device %s", info->product);
+		return setting;
+	}
+
+	// Search by "short" product name
+	snprintf(search_key, 9, "%c%d", info->product[0], info->num_chips);
+	HASH_FIND_STR(settings, search_key, setting);
+	if(setting) {
+		drvlog(LOG_INFO, "Using product-specific settings for device %s", info->product);
+		return setting;
+	}
+
+	// Failing that, return default/generic config (null key)
+	search_key[0] = 0;
+	HASH_FIND_STR(settings, search_key, setting);
+	drvlog(LOG_INFO, "Using non-specific settings for device %s (serial %08x)", info->product,
+	       info->serial);
+	return setting;
+}
+
+static void drillbit_send_config(struct cgpu_info *drillbit)
+{
+	struct drillbit_info *info = drillbit->device_data;
+	char cmd;
+	int amount;
+	char buf[SZ_SERIALISED_BOARDCONFIG];
+	config_setting *setting;
+
+	// Find the relevant board config
+	setting = find_settings(drillbit);
+	drvlog(LOG_NOTICE, "Config: %s:%d:%d:%d Serial: %08x",
+	       setting->config.use_ext_clock ? "ext":"int",
+	       setting->config.use_ext_clock ? setting->config.ext_clock_freq : setting->config.int_clock_level,
+	       setting->config.clock_div2 ? 2 : 1,
+	       setting->config.core_voltage_mv,
+	       info->serial);
+
+	drvlog(LOG_INFO, "Sending board configuration voltage=%d use_ext_clock=%d int_clock_level=%d clock_div2=%d ext_clock_freq=%d",
+	       setting->config.core_voltage, setting->config.use_ext_clock,
+	       setting->config.int_clock_level,
+	       setting->config.clock_div2, setting->config.ext_clock_freq);
+
+	if(setting->config.use_ext_clock && !(info->capabilities & CAP_EXT_CLOCK)) {
+		drvlog(LOG_WARNING, "Chosen configuration specifies external clock but this device (serial %08x) has no external clock!", info->serial);
+	}
+
+	cmd = 'C';
+	usb_write_timeout(drillbit, &cmd, 1, &amount, TIMEOUT, C_BF_REQWORK);
+
+	serialise_board_config(buf, &setting->config);
+	usb_write_timeout(drillbit, buf, SZ_SERIALISED_BOARDCONFIG, &amount, TIMEOUT, C_BF_CONFIG);
+
+	/* Expect a single 'C' byte as acknowledgement */
+	usb_read_simple_response(drillbit, 'C', C_BF_CONFIG); // TODO: verify response
+}
+
+static void drillbit_updatetemps(struct thr_info *thr)
+{
+	struct cgpu_info *drillbit = thr->cgpu;
+	struct drillbit_info *info = drillbit->device_data;
+	char cmd;
+	int amount;
+	uint16_t temp;
+	struct timeval tv_now;
+
+	if(!(info->capabilities & CAP_TEMP))
+		return;
+
+	cgtime(&tv_now);
+	if(ms_tdiff(&tv_now, &info->tv_lasttemp) < 1000)
+		return; // Only update temps once a second
+	info->tv_lasttemp = tv_now;
+
+	cmd = 'T';
+	usb_write_timeout(drillbit, &cmd, 1, &amount, TIMEOUT, C_BF_GETTEMP);
+
+	if(!usb_read_fixed_size(drillbit, &temp, sizeof(temp), TIMEOUT, C_BF_GETTEMP)) {
+		drvlog(LOG_ERR, "Got no response to request for current temperature");
+		return;
+	}
+
+	drvlog(LOG_INFO, "Got temperature reading %d.%dC", temp/10, temp%10);
+	info->temp = temp;
+	if(temp > info->max_temp)
+		info->max_temp = temp;
+}
+
+static void drillbit_get_statline_before(char *buf, size_t bufsiz, struct cgpu_info *drillbit)
+{
+	struct drillbit_info *info = drillbit->device_data;
+
+	tailsprintf(buf, bufsiz, "%c%-2d", info->product[0], info->num_chips);
+
+	if((info->capabilities & CAP_TEMP) && info->temp != 0) {
+		tailsprintf(buf, bufsiz, " %d.%dC (%d.%dC)", info->temp/10, info->temp%10,
+			    info->max_temp/10, info->max_temp%10);
+	} else {
+		// Space out to the same width as if there was a temp field in place
+		tailsprintf(buf, bufsiz, "		  ");
+	}
+
+	tailsprintf(buf, bufsiz, " | ");
+}
+
+
+static bool drillbit_parse_options()
+{
+	/* Read configuration options (currently global not per-ASIC or per-board) */
+	if(settings != NULL)
+		return true; // Already initialised
+
+	// Start with the system-wide defaults
+	HASH_ADD_STR( settings, key, (&default_settings) );
+
+	char *next_opt = opt_drillbit_options;
+	while (next_opt && strlen(next_opt)) {
+		BoardConfig parsed_config;
+		config_setting *new_setting;
+		char key[9];
+		int count, freq, clockdiv, voltage;
+		char clksrc[4];
+
+		// Try looking for an option tagged with a key, first
+		count = sscanf(next_opt, "%8[^:]:%3s:%d:%d:%d", key,
+			       clksrc, &freq, &clockdiv, &voltage);
+		if(count < 5) {
+			key[0] = 0;
+			count = sscanf(next_opt, "%3s:%d:%d:%d",
+				       clksrc, &freq, &clockdiv, &voltage);
+			if(count < 4) {
+				applog(LOG_ERR, "Failed to parse drillbit-options. Invalid options string: '%s'", next_opt);
+				settings = NULL;
+				return false;
+			}
+		}
+
+		if(clockdiv != 1 && clockdiv != 2) {
+			applog(LOG_ERR, "drillbit-options: Invalid clock divider value %d. Valid values are 1 & 2.", clockdiv);
+			settings = NULL;
+			return false;
+		}
+		parsed_config.clock_div2 = count > 2 && clockdiv == 2;
+
+		if(!strcmp("int",clksrc)) {
+			parsed_config.use_ext_clock = 0;
+			if(freq < 0 || freq > 63) {
+				applog(LOG_ERR, "drillbit-options: Invalid internal oscillator level %d. Recommended range is %s for this clock divider (possible is 0-63)", freq, parsed_config.clock_div2 ? "48-57":"30-48");
+				settings = NULL;
+				return false;
+			}
+			if(parsed_config.clock_div2 && (freq < 48 || freq > 57)) {
+				applog(LOG_WARNING, "drillbit-options: Internal oscillator level %d outside recommended range 48-57.", freq);
+			}
+			if(!parsed_config.clock_div2 && (freq < 30 || freq > 48)) {
+				applog(LOG_WARNING, "drillbit-options: Internal oscillator level %d outside recommended range 30-48.", freq);
+			}
+			parsed_config.int_clock_level = freq;
+		}
+		else if (!strcmp("ext", clksrc)) {
+			parsed_config.use_ext_clock = 1;
+			parsed_config.ext_clock_freq = freq;
+			if(freq < 80 || freq > 230) {
+				applog(LOG_WARNING, "drillbit-options: Warning: recommended external clock frequencies are 80-230MHz. Value %d may produce unexpected results.", freq);
+			}
+		}
+		else {
+			applog(LOG_ERR, "drillbit-options: Invalid clock source. Valid choices are int, ext.");
+			return false;
+		}
+
+		parsed_config.core_voltage_mv = voltage;
+		switch(voltage) {
+		case 650:
+			voltage = CONFIG_CORE_065V;
+			break;
+		case 750:
+			voltage = CONFIG_CORE_075V;
+			break;
+		case 850:
+			voltage = CONFIG_CORE_085V;
+			break;
+		case 950:
+			voltage = CONFIG_CORE_095V;
+			break;
+		default:
+			applog(LOG_ERR, "drillbit-options: Invalid core voltage %d. Valid values 650,750,850,950mV)", voltage);
+			return false;
+		}
+		parsed_config.core_voltage = voltage;
+
+		// Add the new set of settings to the configuration choices hash table
+		new_setting = (config_setting *)calloc(sizeof(config_setting), 1);
+		memcpy(&new_setting->config, &parsed_config, sizeof(BoardConfig));
+		memcpy(&new_setting->key, key, 8);
+		config_setting *ignore;
+		HASH_REPLACE_STR(settings, key, new_setting, ignore);
+
+		// Look for next comma-delimited Drillbit option
+		next_opt = strstr(next_opt, ",");
+		if(next_opt)
+			next_opt++;
+	}
+	return true;
+}
+
+static struct cgpu_info *drillbit_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
+{
+	struct cgpu_info *drillbit;
+	struct drillbit_info *info;
+	int i;
+
+	if (!drillbit_parse_options())
+		return false; // Bit of a hack doing this here, should do it somewhere else
+
+	drillbit = usb_alloc_cgpu(&drillbit_drv, 1);
+	drillbit->device_id = -1; // temporary so drvlog() prints a non-valid device_id
+
+	if (!usb_init(drillbit, dev, found))
+		goto out;
+	applog(LOG_INFO, "DRB: Device found at %s", drillbit->device_path);
+
+	info = calloc(sizeof(struct drillbit_info), 1);
+	if (!info)
+		quit(1, "Failed to calloc info in drillbit_detect_one");
+	drillbit->device_data = info;
+
+	drillbit_open(drillbit);
+
+	/* Send getinfo request */
+	if (!drillbit_getinfo(drillbit, info))
+		goto out_close;
+
+	/* TODO: Add detection for actual chip ids based on command/response,
+	   not prefill assumption about chip layout based on info structure */
+	info->chips = calloc(sizeof(struct drillbit_chip_info), info->num_chips);
+	for(i = 0; i < info->num_chips; i++) {
+		info->chips[i].chip_id = i;
+	}
+
+	/* Send reset request */
+	if (!drillbit_reset(drillbit))
+		goto out_close;
+
+	drillbit_identify(drillbit);
+	drillbit_empty_buffer(drillbit);
+
+	cgtime(&info->tv_lastchipinfo);
+
+	if (!add_cgpu(drillbit))
+		goto out_close;
+
+	update_usb_stats(drillbit);
+
+	drillbit_send_config(drillbit);
+
+	drvlog(LOG_INFO, "Successfully initialised %s",
+	       drillbit->device_path);
+
+	return drillbit;
+out_close:
+	drillbit_close(drillbit);
+	usb_uninit(drillbit);
+out:
+	drillbit = usb_free_cgpu(drillbit);
+	return drillbit;
+}
+
+static void drillbit_detect(bool __maybe_unused hotplug)
+{
+	usb_detect(&drillbit_drv, drillbit_detect_one);
+}
+
+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 BF_OFFSETS 3
+static const uint32_t bf_offsets[] = {-0x800000, 0, -0x400000};
+
+static bool drillbit_checkresults(struct thr_info *thr, struct work *work, uint32_t nonce)
+{
+	int i;
+
+	nonce = decnonce(nonce);
+	for (i = 0; i < BF_OFFSETS; i++) {
+		if (test_nonce(work, nonce + bf_offsets[i])) {
+			submit_tested_work(thr, work);
+			return true;
+		}
+	}
+	return false;
+}
+
+// Check and submit back any pending work results from firmware,
+// returns number of successful results found
+static int check_for_results(struct thr_info *thr)
+{
+	struct cgpu_info *drillbit = thr->cgpu;
+	struct drillbit_info *info = drillbit->device_data;
+	struct drillbit_chip_info *chip;
+	char cmd;
+	int amount, i, k, found;
+	uint8_t j;
+	int successful_results = 0;
+	uint32_t result_count;
+	char buf[SZ_SERIALISED_WORKRESULT];
+	WorkResult *responses = NULL;
+	WorkResult *response;
+
+	if (unlikely(thr->work_restart))
+		goto cleanup;
+
+	// Send request for completed work
+	cmd = 'E';
+	usb_write_timeout(drillbit, &cmd, 1, &amount, TIMEOUT, C_BF_GETRES);
+
+	// Receive count for work results
+	if(!usb_read_fixed_size(drillbit, &result_count, sizeof(result_count), TIMEOUT, C_BF_GETRES)) {
+		drvlog(LOG_ERR, "Got no response to request for work results");
+		goto cleanup;
+	}
+	if(unlikely(drillbit->usbinfo.nodev))
+		goto cleanup;
+	if(result_count)
+		drvlog(LOG_DEBUG, "Result count %d",result_count);
+
+	if(result_count > 1024) {
+		drvlog(LOG_ERR, "Got implausible result count %d - treating as error!", result_count);
+		goto cleanup;
+	}
+
+	if(result_count == 0) {
+		// Short circuit reading any work results
+		return 0;
+	}
+
+	responses = calloc(result_count, sizeof(WorkResult));
+
+	// Receive work results (0 or more) into buffer
+	for(j = 0; j < result_count; j++) {
+		if(unlikely(drillbit->usbinfo.nodev))
+			goto cleanup;
+		if(!usb_read_fixed_size(drillbit, buf, SZ_SERIALISED_WORKRESULT, TIMEOUT, C_BF_GETRES)) {
+			drvlog(LOG_ERR, "Failed to read response data packet idx %d count 0x%x", j, result_count);
+			goto cleanup;
+		}
+		deserialise_work_result(&responses[j], buf);
+	}
+
+	for(j = 0; j < result_count; j++) {
+		if (unlikely(thr->work_restart))
+			goto cleanup;
+
+		response = &responses[j];
+		drvlog(LOG_DEBUG, "Got response packet chip_id %d nonces %d is_idle %d", response->chip_id, response->num_nonces, response->is_idle);
+		chip = find_chip(info, response->chip_id);
+		if(!chip) {
+			drvlog(LOG_ERR, "Got work result for unknown chip id %d", response->chip_id);
+			continue;
+		}
+		if(chip->state == IDLE) {
+			drvlog(LOG_WARNING, "Got spurious work results for idle ASIC %d", response->chip_id);
+		}
+		if(response->num_nonces > MAX_RESULTS) {
+			drvlog(LOG_ERR, "Got invalid number of result nonces (%d) for chip id %d", response->num_nonces, response->chip_id);
+			goto cleanup;
+		}
+		for(i = 0; i < response->num_nonces; i++) {
+			if (unlikely(thr->work_restart))
+				goto cleanup;
+			found = false;
+			for(k = 0; k < WORK_HISTORY_LEN; k++) {
+				/* NB we deliberately check all results against all work because sometimes ASICs seem to give multiple "valid" nonces,
+				   and this seems to avoid some result that would otherwise be rejected by the pool.
+
+				   However we only count one success per result set to avoid artificially inflating the hashrate.
+				   A smarter thing to do here might be to look at the full set of nonces in the response and start from the "best" one first.
+				*/
+				if (chip->current_work[k] && drillbit_checkresults(thr, chip->current_work[k], response->nonce[i])) {
+					if(!found) {
+						chip->success_count++;
+						successful_results++;
+						found = true;
+					}
+				}
+			}
+			if(!found && chip->state != IDLE) {
+				/* all nonces we got back from this chip were invalid */
+				inc_hw_errors(thr);
+				chip->error_count++;
+			}
+		}
+		if(chip->state == WORKING_QUEUED && !response->is_idle)
+			chip->state = WORKING_NOQUEUED; // Time to queue up another piece of "next work"
+		else
+			chip->state = IDLE; // Uh-oh, we're totally out of work for this ASIC!
+	}
+
+cleanup:
+	drillbit_empty_buffer(drillbit);
+	if(responses)
+		free(responses);
+	return successful_results;
+}
+
+static void drillbit_send_work_to_chip(struct thr_info *thr, struct drillbit_chip_info *chip)
+{
+	struct cgpu_info *drillbit = thr->cgpu;
+	struct work *work;
+	char cmd;
+	char buf[SZ_SERIALISED_WORKREQUEST];
+	int amount, i;
+
+	/* Get some new work for the chip */
+	work = get_queue_work(thr, drillbit, thr->id);
+	if (unlikely(thr->work_restart)) {
+		work_completed(drillbit, work);
+		return;
+	}
+
+	drvlog(LOG_DEBUG, "Sending work to chip_id %d", chip->chip_id);
+	serialise_work_request(buf, chip->chip_id, work);
+
+	/* Send work to cgminer */
+	cmd = 'W';
+	usb_write_timeout(drillbit, &cmd, 1, &amount, TIMEOUT, C_BF_REQWORK);
+	usb_write_timeout(drillbit, buf, SZ_SERIALISED_WORKREQUEST, &amount, TIMEOUT, C_BF_REQWORK);
+
+	/* Expect a single 'W' byte as acknowledgement */
+	usb_read_simple_response(drillbit, 'W', C_BF_REQWORK);
+	if(chip->state == WORKING_NOQUEUED)
+		chip->state = WORKING_QUEUED;
+	else
+		chip->state = WORKING_NOQUEUED;
+
+	if (unlikely(thr->work_restart)) {
+		work_completed(drillbit, work);
+		return;
+	}
+
+	// Read into work history
+	if(chip->current_work[0])
+		work_completed(drillbit, chip->current_work[0]);
+	for(i = 0; i < WORK_HISTORY_LEN-1; i++)
+		chip->current_work[i] = chip->current_work[i+1];
+	chip->current_work[WORK_HISTORY_LEN-1] = work;
+	cgtime(&chip->tv_start);
+
+	chip->work_sent_count++;
+}
+
+static int64_t drillbit_scanwork(struct thr_info *thr)
+{
+	struct cgpu_info *drillbit = thr->cgpu;
+	struct drillbit_info *info = drillbit->device_data;
+	struct drillbit_chip_info *chip;
+	struct timeval tv_now;
+	int amount, i, j, ms_diff, result_count = 0;;
+	char buf[200];
+
+	/* send work to an any chip without queued work */
+	for(i = 0; i < info->num_chips; i++) {
+		if(info->chips[i].state != WORKING_QUEUED) {
+			drillbit_send_work_to_chip(thr, &info->chips[i]);
+		}
+		if (unlikely(thr->work_restart) || unlikely(drillbit->usbinfo.nodev))
+			goto cascade;
+	}
+
+	/* check for any chips that have timed out on sending results */
+	cgtime(&tv_now);
+	for(i = 0; i < info->num_chips; i++) {
+		if(info->chips[i].state == IDLE)
+			continue;
+		ms_diff = ms_tdiff(&tv_now, &info->chips[i].tv_start);
+		if(ms_diff > TIMEOUT) {
+			if(info->chips[i].work_sent_count > 4) {
+				/* Only count ASIC timeouts after the pool has started to send work in earnest,
+				   some pools can create unusual delays early on */
+				drvlog(LOG_ERR, "Timing out unresponsive ASIC %d", info->chips[i].chip_id);
+				info->chips[i].timeout_count++;
+			}
+			info->chips[i].state = IDLE;
+			drillbit_send_work_to_chip(thr, &info->chips[i]);
+		}
+		if (unlikely(thr->work_restart) || unlikely(drillbit->usbinfo.nodev))
+			goto cascade;
+	}
+
+	/* Check for results */
+	result_count = check_for_results(thr);
+
+	/* Print a per-chip info line every 30 seconds */
+	cgtime(&tv_now);
+	if(opt_log_level <= LOG_INFO && ms_tdiff(&tv_now, &info->tv_lastchipinfo) > 30000) {
+		/* TODO: this output line may get truncated (max debug is 256 bytes) once we get more
+		   chips in a single device
+		*/
+		amount = sprintf(buf, "%s %d: S/E/T", drillbit->drv->name, drillbit->device_id);
+		if(amount > 0) {
+			for(i = 0; i < info->num_chips; i++) {
+				chip= &info->chips[i];
+				j = snprintf(&buf[amount], sizeof(buf)-(size_t)amount, "%u:%u/%u/%u",
+					     chip->chip_id, chip->success_count, chip->error_count,
+					     chip->timeout_count);
+				if(j < 0)
+					break;
+				amount += j;
+				if((size_t)amount >= sizeof(buf))
+					break;
+			}
+			drvlog(LOG_INFO, "%s", buf);
+			cgtime(&info->tv_lastchipinfo);
+		}
+	}
+
+	drillbit_updatetemps(thr);
+
+cascade:
+	drillbit_empty_buffer(drillbit);
+
+	if (unlikely(drillbit->usbinfo.nodev)) {
+		drvlog(LOG_WARNING, "Device disappeared, disabling thread");
+		return -1;
+	}
+
+	if (unlikely(thr->work_restart)) {
+		/* Issue an ASIC reset as we won't be coming back for any of these results */
+		drvlog(LOG_DEBUG, "Received work restart, resetting ASIC");
+		drillbit_reset(drillbit);
+	}
+
+	return 0xffffffffULL * result_count;
+}
+
+static struct api_data *drillbit_api_stats(struct cgpu_info *cgpu)
+{
+	struct drillbit_info *info = cgpu->device_data;
+	struct api_data *root = NULL;
+	char serial[16];
+	int version;
+
+	version = info->version;
+	root = api_add_int(root, "Protocol Version", &version, true);
+	root = api_add_string(root, "Product", info->product, false);
+	sprintf(serial, "%08x", info->serial);
+	root = api_add_string(root, "Serial", serial, true);
+	root = api_add_uint8(root, "ASIC Count", &info->num_chips, true);
+	if(info->capabilities & CAP_TEMP) {
+		float temp = (float)info->temp/10;
+		root = api_add_temp(root, "Temp", &temp, true);
+		temp = (float)info->max_temp/10;
+		root = api_add_temp(root, "Temp Max", &temp, true);
+	}
+
+	return root;
+}
+
+static void drillbit_reinit(struct cgpu_info  *drillbit)
+{
+	drillbit_close(drillbit);
+	drillbit_open(drillbit);
+	drillbit_reset(drillbit);
+}
+
+static void drillbit_shutdown(struct thr_info *thr)
+{
+	struct cgpu_info *drillbit = thr->cgpu;
+
+	drillbit_close(drillbit);
+}
+
+/* Currently hardcoded to BF1 devices */
+struct device_drv drillbit_drv = {
+	.drv_id = DRIVER_drillbit,
+	.dname = "Drillbit",
+	.name = "DRB",
+	.drv_detect = drillbit_detect,
+	.hash_work = &hash_driver_work,
+	.scanwork = drillbit_scanwork,
+	.get_api_stats = drillbit_api_stats,
+	.get_statline_before = drillbit_get_statline_before,
+	.reinit_device = drillbit_reinit,
+	.thread_shutdown = drillbit_shutdown,
+	.identify_device = drillbit_identify,
+};
+
+
+/* Structure serialisation/deserialisation */
+
+#define SERIALISE(FIELD) do {					\
+		memcpy(&buf[offset], &FIELD, sizeof(FIELD));	\
+		offset += sizeof(FIELD);			\
+	} while(0)
+
+#define DESERIALISE(FIELD) do {					\
+		memcpy(&FIELD, &buf[offset], sizeof(FIELD));	\
+		offset += sizeof(FIELD);			\
+	} while(0)
+
+static void serialise_work_request(char *buf, uint16_t chip_id, const struct work *work)
+{
+	size_t offset = 0;
+	SERIALISE(chip_id);
+	memcpy(&buf[offset], work->midstate, 32);
+	offset += 32;
+	memcpy(&buf[offset], work->data + 64, 12);
+	//offset += 12;
+}
+
+static void deserialise_work_result(WorkResult *wr, const char *buf)
+{
+	int i;
+	size_t offset = 0;
+	DESERIALISE(wr->chip_id);
+	DESERIALISE(wr->num_nonces);
+	DESERIALISE(wr->is_idle);
+	for(i = 0; i < MAX_RESULTS; i++)
+		DESERIALISE(wr->nonce[i]);
+}
+
+static void serialise_board_config(char *buf, const BoardConfig *bc)
+{
+	size_t offset = 0;
+	SERIALISE(bc->core_voltage);
+	SERIALISE(bc->int_clock_level);
+	SERIALISE(bc->clock_div2);
+	SERIALISE(bc->use_ext_clock);
+	SERIALISE(bc->ext_clock_freq);
+}
+
+static void deserialise_identity(Identity *id, const char *buf)
+{
+	size_t offset = 0;
+	DESERIALISE(id->protocol_version);
+	DESERIALISE(id->product);
+	DESERIALISE(id->serial);
+	DESERIALISE(id->num_chips);
+	DESERIALISE(id->capabilities);
+}
diff --git a/driver-drillbit.h b/driver-drillbit.h
new file mode 100644
index 0000000..7377352
--- /dev/null
+++ b/driver-drillbit.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2013 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 BITFURY_H
+#define BITFURY_H
+
+#include "miner.h"
+#include "usbutils.h"
+
+#define WORK_HISTORY_LEN 4
+
+struct drillbit_chip_info;
+
+/* drillbit_info structure applies to entire device */
+struct drillbit_info {
+  struct cgpu_info *base_cgpu;
+  uint8_t version;
+  uint8_t num_chips;
+  uint16_t capabilities;
+  char product[8];
+  uint32_t serial;
+  struct drillbit_chip_info *chips;
+  struct timeval tv_lastchipinfo;
+  struct timeval tv_lasttemp;
+  uint16_t temp;
+  uint16_t max_temp;
+};
+
+enum drillbit_chip_state {
+  IDLE,            /* Has no work */
+  WORKING_NOQUEUED, /* Has current work but nothing queued as "next work" */
+  WORKING_QUEUED   /* Has current work and a piece of work queued for after that */
+};
+
+struct drillbit_chip_info {
+  uint16_t chip_id;
+  struct work *current_work[WORK_HISTORY_LEN];
+  enum drillbit_chip_state state;
+  struct timeval tv_start;
+  uint32_t success_count;
+  uint32_t error_count;
+  uint32_t timeout_count;
+  uint32_t work_sent_count;
+};
+
+#endif /* BITFURY_H */
diff --git a/miner.h b/miner.h
index 042f8cb..6a1c1fb 100644
--- a/miner.h
+++ b/miner.h
@@ -237,6 +237,7 @@ static inline int fsync (int fd)
 	DRIVER_ADD_COMMAND(hashfast) \
 	DRIVER_ADD_COMMAND(klondike) \
 	DRIVER_ADD_COMMAND(knc) \
+	DRIVER_ADD_COMMAND(drillbit) \
 	DRIVER_ADD_COMMAND(bab) \
 	DRIVER_ADD_COMMAND(avalon)
 
@@ -954,6 +955,9 @@ extern char *opt_bitburner_fury_options;
 #ifdef USE_KLONDIKE
 extern char *opt_klondike_options;
 #endif
+#ifdef USE_DRILLBIT
+extern char *opt_drillbit_options;
+#endif
 #ifdef USE_USBUTILS
 extern char *opt_usb_select;
 extern int opt_usbdump;
diff --git a/usbutils.c b/usbutils.c
index 28dc6a2..6b7a0e1 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -57,6 +57,7 @@ static cgtimer_t usb11_cgt;
 #define USB_CONFIG 1
 
 #define BITFURY_TIMEOUT_MS 999
+#define DRILLBIT_TIMEOUT_MS 999
 #define ICARUS_TIMEOUT_MS 999
 
 #ifdef WIN32
@@ -138,6 +139,24 @@ static struct usb_intinfo bfu_ints[] = {
 	USB_EPS(1,  bfu1_epinfos),
 	USB_EPS(0,  bfu0_epinfos)
 };
+#endif
+
+#ifdef USE_DRILLBIT
+// Drillbit Bitfury devices
+static struct usb_epinfo drillbit_int_epinfos[] = {
+	{ LIBUSB_TRANSFER_TYPE_INTERRUPT,	8,	EPI(3), 0, 0 }
+};
+
+static struct usb_epinfo drillbit_bulk_epinfos[] = {
+	{ LIBUSB_TRANSFER_TYPE_BULK,	16,	EPI(1), 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	16,	EPO(2), 0, 0 },
+};
+
+/* Default to interface 1 */
+static struct usb_intinfo drillbit_ints[] = {
+	USB_EPS(1,  drillbit_bulk_epinfos),
+	USB_EPS(0,  drillbit_int_epinfos)
+};
 
 static struct usb_epinfo bxf0_epinfos[] = {
 	{ LIBUSB_TRANSFER_TYPE_INTERRUPT,	8,	EPI(1), 0, 0 }
@@ -336,6 +355,21 @@ static struct usb_find_devices find_dev[] = {
 		.iProduct = "Bitfury BF1",
 		INTINFO(bfu_ints)
 	},
+#endif
+#ifdef USE_DRILLBIT
+	{
+		.drv = DRIVER_drillbit,
+		.name = "DRB",
+		.ident = IDENT_DRB,
+		.idVendor = 0x03eb,
+		.idProduct = 0x2404,
+		.config = 1,
+		.timeout = DRILLBIT_TIMEOUT_MS,
+		.latency = LATENCY_UNUSED,
+		.iManufacturer = "Drillbit Systems",
+		.iProduct = NULL, /* Can be Thumb or Eight, same driver */
+		INTINFO(drillbit_ints)
+	},
 	{
 		.drv = DRIVER_bitfury,
 		.name = "BXF",
@@ -3104,6 +3138,7 @@ void usb_cleanup(void)
 			case DRIVER_bflsc:
 			case DRIVER_bitforce:
 			case DRIVER_bitfury:
+			case DRIVER_drillbit:
 			case DRIVER_modminer:
 			case DRIVER_icarus:
 			case DRIVER_avalon:
diff --git a/usbutils.h b/usbutils.h
index 7ab77ac..b55bb13 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -153,7 +153,8 @@ enum sub_ident {
 	IDENT_KLN,
 	IDENT_LLT,
 	IDENT_MMQ,
-	IDENT_ZTX
+	IDENT_ZTX,
+        IDENT_DRB
 };
 
 struct usb_find_devices {
@@ -327,6 +328,23 @@ struct cg_usb_info {
 	USB_ADD_COMMAND(C_ENABLE_UART, "EnableUART") \
 	USB_ADD_COMMAND(C_BB_SET_VOLTAGE, "SetCoreVoltage") \
 	USB_ADD_COMMAND(C_BB_GET_VOLTAGE, "GetCoreVoltage") \
+	USB_ADD_COMMAND(C_BF_RESET, "BFReset") \
+	USB_ADD_COMMAND(C_BF_OPEN, "BFOpen") \
+	USB_ADD_COMMAND(C_BF_INIT, "BFInit") \
+	USB_ADD_COMMAND(C_BF_CLOSE, "BFClose") \
+	USB_ADD_COMMAND(C_BF_REQINFO, "BFRequestInfo") \
+	USB_ADD_COMMAND(C_BF_GETINFO, "BFGetInfo") \
+	USB_ADD_COMMAND(C_BF_REQRESET, "BFRequestReset") \
+	USB_ADD_COMMAND(C_BF_GETRESET, "BFGetReset") \
+	USB_ADD_COMMAND(C_BF_REQWORK, "BFRequestWork") \
+	USB_ADD_COMMAND(C_BF_GETWORK, "BFGetWork") \
+	USB_ADD_COMMAND(C_BF_GETRES, "BFGetResults") \
+	USB_ADD_COMMAND(C_BF_FLUSH, "BFFlush") \
+	USB_ADD_COMMAND(C_BF_IFLUSH, "BFInterruptFlush") \
+	USB_ADD_COMMAND(C_BF_IDENTIFY, "BFIdentify") \
+	USB_ADD_COMMAND(C_BF_DETECTCHIPS, "BFDetectChips") \
+	USB_ADD_COMMAND(C_BF_CONFIG, "BFConfig") \
+        USB_ADD_COMMAND(C_BF_GETTEMP, "BFGetTemp") \
 	USB_ADD_COMMAND(C_ATMEL_RESET, "AtmelReset") \
 	USB_ADD_COMMAND(C_ATMEL_OPEN, "AtmelOpen") \
 	USB_ADD_COMMAND(C_ATMEL_INIT, "AtmelInit") \
diff --git a/windows-build.txt b/windows-build.txt
index 2da205d..2259ea8 100644
--- a/windows-build.txt
+++ b/windows-build.txt
@@ -4,6 +4,8 @@
 #                                                                                    #
 ######################################################################################
 
+(See bottom of file for steps to cross-build for Win32 from Linux.)
+
 **************************************************************************************
 * Introduction                                                                       *
 **************************************************************************************
@@ -251,8 +253,94 @@ You will have to copy "libusb-1.0.dll" to your working cgminer binary directory.
 --without-curses        Compile support for curses TUI (default enabled)
 --without-libudev       Autodetect FPGAs using libudev (default enabled)
 
+
+
 ######################################################################################
 #                                                                                    #
-#       Native WIN32 setup and build instructions (on mingw32/Windows) complete      #
+#       Cross-compiling for Windows from Linux                                       #
 #                                                                                    #
 ######################################################################################
+
+It is possible to cross-compile Windows binaries from Linux. The
+process is a bit different to the native steps shown above (it is also
+possible to use wine and the native steps, but this is more messing
+around, very slow, and not advisable.)
+
+** Install mingw cross compiler
+
+On Ubuntu/Debian:
+
+sudo apt-get install mingw32
+
+** create a directory to hold our cross-library dependencies
+
+We'll create a directory outside the source tree to hold non-system
+libraries we depend on. We could put these in
+/usr/i586-mingw32msvc/lib or anywhere else, instead (though keeping it
+outside /usr means we can set it up without root privileges.)
+
+IMPORTANT: If you put this directory inside your cgminer directory,
+remember 'make distclean' may delete it!
+
+mkdir -p ../cgminer-win32-deps/lib
+cd ../cgminer-win32-deps
+mkdir include
+mkdir bin
+
+NB: All following steps assume you are in the "cgminer-win32-deps" directory. Adjust as necessary.
+
+** pdcurses
+
+wget http://internode.dl.sourceforge.net/project/pdcurses/pdcurses/3.4/pdc34dllw.zip
+unzip /home/gus/Downloads/pdc34dllw.zip
+mv *.h include/
+mv pdcurses.lib lib/
+mv pdcurses.dll bin/
+
+** pthreads-w32
+
+(NB: I found pthreads-w32 2.9.1 doesn't seem to work properly, transfers time out early due to sem_timedwait exiting immediately(?))
+
+wget -O lib/libpthread.a ftp://sourceware.org/pub/pthreads-win32/prebuilt-dll-2-8-0-release/lib/libpthreadGC2.a
+wget -O include/pthread.h ftp://sourceware.org/pub/pthreads-win32/prebuilt-dll-2-8-0-release/include/pthread.h
+wget -O include/sched.h ftp://sourceware.org/pub/pthreads-win32/prebuilt-dll-2-8-0-release/include/sched.h
+wget -O include/semaphore.h ftp://sourceware.org/pub/pthreads-win32/prebuilt-dll-2-8-0-release/include/semaphore.h
+wget -O lib/libpthread.a ftp://sourceware.org/pub/pthreads-win32/prebuilt-dll-2-8-0-release/lib/libpthreadGC2.a
+wget -O bin/pthreadGC2.dll ftp://sourceware.org/pub/pthreads-win32/prebuilt-dll-2-8-0-release/lib/pthreadGC2.dll
+
+** libcurl
+
+wget http://curl.haxx.se/gknw.net/7.33.0/dist-w32/curl-7.33.0-devel-mingw32.zip
+unzip curl-7.33.0-devel-mingw32.zip
+mv curl-7.33.0-devel-mingw32/include/* include/
+mv curl-7.33.0-devel-mingw32/lib/* lib/
+mv curl-7.33.0-devel-mingw32/bin/* bin/
+rm -rf curl-7.33.0-devel-mingw32
+
+
+** clean up
+
+rm *.zip
+
+
+** Building cgminer
+
+Below assumes you're building in a "build-win32" or similar directory
+inside the cgminer directory. Fix up the -I and -L paths appropriately
+if you're building in-tree or someplace else.
+
+Configure command:
+
+CPPFLAGS="-I`pwd`/../../cgminer-win32-deps/include" LDFLAGS="-L`pwd`/../../cgminer-win32-deps/lib -lcurldll" ../autogen.sh --prefix=/usr/local/i586-mingw32 --host=i586-mingw32msvc --build=i686-linux 
+
+^^^ Plus whatever configure arguments you want to add. Note the paths
+    to cgminer-win32-deps that you may need to change.
+
+And make:
+
+make
+
+After cgminer builds, the next steps are the same as for native
+building as given under "Copy files to a build directory/folder"
+(DLLs can all be found in the cgminer-win32-deps/bin directory.)
+