Commit bbf8a00c09bfc7a63fc5e222f3054da1340be299

Angus Gratton 2013-10-25T10:50:03

Hashing with new protocol

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
diff --git a/Makefile.am b/Makefile.am
index bc60361..392dbef 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -80,6 +80,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 bc673e1..d8c165f 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -7561,7 +7561,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 d1270f2..89891a4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -245,15 +245,27 @@ 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])
 
-icarus="no"
+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])
+
+
+Icarus="no"
 
 AC_ARG_ENABLE([icarus],
 	[AC_HELP_STRING([--enable-icarus],[Compile support for Icarus (default disabled)])],
@@ -321,7 +333,7 @@ else
 	])
 fi
 
-if test x$avalon$bitforce$bitfury$modminer$bflsc$icarus$klondike != xnonononononono; then
+if test x$avalon$bitforce$bitfury$drillbit$modminer$bflsc$icarus$klondike != xnononononononono; then
 	want_usbutils=true
 else
 	want_usbutils=false
@@ -493,14 +505,14 @@ if test "x$opencl" != xno; then
 
 	else
 		echo "  OpenCL...............: NOT FOUND. GPU mining support DISABLED"
-		if test "x$avalon$bitforce$bitfury$icarus$modminer$bflsc$klondike" = xnonononononono; then
+		if test "x$avalon$bitforce$bitfury$drillbit$icarus$modminer$bflsc$klondike" = xnononononononono; then
 			AC_MSG_ERROR([No mining configured in])
 		fi
 		echo "  scrypt...............: Disabled (needs OpenCL)"
 	fi
 else
 	echo "  OpenCL...............: Detection overrided. GPU mining support DISABLED"
-	if test "x$avalon$bitforce$bitfury$icarus$modminer$bflsc$klondike" = xnonononononono; then
+	if test "x$avalon$bitforce$bitfury$drillbit$icarus$modminer$bflsc$klondike" = xnononononononono; then
 		AC_MSG_ERROR([No mining configured in])
 	fi
 	echo "  scrypt...............: Disabled (needs OpenCL)"
@@ -536,11 +548,18 @@ else
 fi
 
 if test "x$bitfury" = xyes; then
-	echo "  BitFury.ASICs........: Enabled"
+	echo "  BitFury.BF1........: Enabled"
 else
-	echo "  BitFury.ASICs........: Disabled"
+	echo "  BitFury.BF1........: Disabled"
 fi
 
+if test "x$drillbit" = xyes; then
+	echo "  Drillbit.BitFury........: Enabled"
+else
+	echo "  Drillbit.BitFury........: Disabled"
+fi
+
+
 if test "x$icarus" = xyes; then
 	echo "  Icarus.FPGAs.........: Enabled"
 else
diff --git a/driver-drillbit.c b/driver-drillbit.c
new file mode 100644
index 0000000..cac0432
--- /dev/null
+++ b/driver-drillbit.c
@@ -0,0 +1,497 @@
+/*
+ * 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.
+ */
+
+#include "config.h"
+
+#include "miner.h"
+#include "driver-drillbit.h"
+#include "sha2.h"
+
+/* Wait longer 1/3 longer than it would take for a full nonce range */
+#define TIMEOUT 2000
+#define MAX_RESULTS 16 // max results from a single chip
+
+/* Request and response structs */
+
+typedef struct __attribute__((packed))
+{
+  uint16_t chip_id;
+  uint8_t midstate[32];
+  uint8_t data[12];
+} WorkRequest;
+
+typedef struct __attribute__((packed))
+{
+  uint16_t chip_id;
+  uint8_t num_nonces;
+  uint32_t nonce[MAX_RESULTS];
+} WorkResult;
+
+typedef struct __attribute__((packed)) Identity
+{
+    uint8_t version;
+    uint8_t product[8];
+    uint32_t serial;
+    uint8_t num_chips;
+} Identity;
+
+/* 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 *bitfury, void *result, size_t result_size, int timeout, enum usb_cmds command_name) {
+  uint8_t *buf[result_size];
+  char *hex;
+  int count;
+  int amount;
+
+  amount = 1;
+  count = 0;
+  while(amount > 0 && count < result_size) { // TODO: decrement timeout appropriately
+    usb_read_once_timeout(bitfury, (char *)&buf[count], result_size-count, &amount, timeout, command_name);
+    count += amount;
+  }
+  if(amount > 0) {
+    memcpy(result, buf, result_size);
+    return true;
+  }
+  if(count > 0) {
+    applog(LOG_ERR, "Read incomplete fixed size packet - got %d bytes / %lu", count, result_size);
+    hex = bin2hex(result, count);
+    applog(LOG_DEBUG, "%s", hex);
+    free(hex);
+  }
+  return false;
+}
+
+static bool usb_read_simple_response(struct cgpu_info *bitfury, 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 *bitfury, char command, enum usb_cmds command_name) {
+  int amount;
+  usb_write(bitfury, &command, 1, &amount, C_BF_REQWORK);
+  if(amount != 1) {
+    applog(LOG_ERR, "Failed to write command %c",command);
+    return false;
+  }
+  return usb_read_simple_response(bitfury, 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 *bitfury, char command, enum usb_cmds command_name) {
+  int amount;
+  char response;
+  /* Expect a single byte, matching the command, as acknowledgement */
+  usb_read_once_timeout(bitfury, &response, 1, &amount, TIMEOUT, C_BF_GETRES);
+  if(amount != 1) {
+    applog(LOG_ERR, "Got no response to command %c",command);
+    return false;
+  }
+  if(response != command) {
+    applog(LOG_ERR, "Got unexpected response %c to command %c", response, command);    
+    return false;
+  }
+  return true;
+}
+
+static void bitfury_empty_buffer(struct cgpu_info *bitfury)
+{
+	char buf[512];
+	int amount;
+
+	do {
+		usb_read_once(bitfury, buf, 512, &amount, C_BF_FLUSH);
+	} while (amount);
+}
+
+static void bitfury_open(struct cgpu_info *bitfury)
+{
+	uint32_t buf[2];
+
+	bitfury_empty_buffer(bitfury);
+	/* Magic sequence to reset device only really needed for windows but
+	 * harmless on linux. */
+        /*
+	buf[0] = 0x80250000;
+	buf[1] = 0x00000800;
+	usb_transfer(bitfury, 0, 9, 1, 0, C_BF_RESET);
+	usb_transfer(bitfury, 0x21, 0x22, 0, 0, C_BF_OPEN);
+	usb_transfer_data(bitfury, 0x21, 0x20, 0x0000, 0, buf, 7, C_BF_INIT);
+        */
+}
+
+static void bitfury_close(struct cgpu_info *bitfury)
+{
+  struct drillbit_info *info = bitfury->device_data;
+  bitfury_empty_buffer(bitfury);
+  if(info->chips)
+    free(info->chips);
+}
+
+static void bitfury_identify(struct cgpu_info *bitfury)
+{
+	int amount;
+
+        usb_send_simple_command(bitfury, 'L', C_BF_IDENTIFY);
+}
+
+static bool bitfury_getinfo(struct cgpu_info *bitfury, struct drillbit_info *info)
+{
+	int amount, err;
+        Identity identity;
+
+	err = usb_write(bitfury, "I", 1, &amount, C_BF_REQINFO);
+	if (err) {
+		applog(LOG_INFO, "%s %d: Failed to write REQINFO",
+		       bitfury->drv->name, bitfury->device_id);
+		return false;
+	}
+        // can't call usb_read_fixed_size here as stats not initialised
+	err = usb_read(bitfury, (void *)&identity, sizeof(Identity), &amount, C_BF_GETINFO);
+	if (err) {
+		applog(LOG_INFO, "%s %d: Failed to read GETINFO",
+		       bitfury->drv->name, bitfury->device_id);
+		return false;
+	}
+	if (amount != sizeof(Identity)) {
+		applog(LOG_INFO, "%s %d: Getinfo received %d bytes instead of %lu",
+		       bitfury->drv->name, bitfury->device_id, amount, sizeof(Identity));
+		return false;
+	}
+	info->version = identity.version;
+	memcpy(&info->product, identity.product, sizeof(identity.product));
+        info->serial = identity.serial;
+        info->num_chips = identity.num_chips;
+
+	applog(LOG_INFO, "%s %d: Getinfo returned version %d, product %s serial %08x num_chips %d", bitfury->drv->name,
+	       bitfury->device_id, info->version, info->product, info->serial, info->num_chips);
+	bitfury_empty_buffer(bitfury);
+	return true;
+}
+
+static bool bitfury_reset(struct cgpu_info *bitfury)
+{
+	int amount, err;
+        if(!usb_send_simple_command(bitfury, 'R', C_BF_REQRESET))
+          return false;
+        
+	bitfury_empty_buffer(bitfury);
+	return true;
+}
+
+static bool bitfury_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
+{
+	struct cgpu_info *bitfury;
+	struct drillbit_info *info;
+        int i;
+
+	bitfury = usb_alloc_cgpu(&bitfury_drv, 1);
+
+	if (!usb_init(bitfury, dev, found))
+		goto out;
+	applog(LOG_INFO, "%s %d: Found at %s", bitfury->drv->name,
+	       bitfury->device_id, bitfury->device_path);
+
+	info = calloc(sizeof(struct drillbit_info), 1);
+	if (!info)
+		quit(1, "Failed to calloc info in bitfury_detect_one");
+	bitfury->device_data = info;
+
+	usb_buffer_enable(bitfury);
+
+	bitfury_open(bitfury);
+
+	/* Send getinfo request */
+	if (!bitfury_getinfo(bitfury, info))
+		goto out_close;
+
+	/* Send reset request */
+	if (!bitfury_reset(bitfury))
+		goto out_close;
+
+	bitfury_identify(bitfury);
+	bitfury_empty_buffer(bitfury);
+
+        /* 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;
+        }
+
+	if (!add_cgpu(bitfury))
+		goto out_close;
+
+	update_usb_stats(bitfury);
+	applog(LOG_INFO, "%s %d: Successfully initialised %s",
+	       bitfury->drv->name, bitfury->device_id, bitfury->device_path);
+
+	return true;
+out_close:
+	bitfury_close(bitfury);
+	usb_uninit(bitfury);
+out:
+	bitfury = usb_free_cgpu(bitfury);
+	return false;
+}
+
+static void bitfury_detect(bool __maybe_unused hotplug)
+{
+	usb_detect(&bitfury_drv, bitfury_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 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;
+
+        nonce = decnonce(nonce);
+	for (i = 0; i < BT_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 bool check_for_results(struct thr_info *thr)
+{
+  struct cgpu_info *bitfury = thr->cgpu;
+  struct drillbit_info *info = bitfury->device_data;
+  struct drillbit_chip_info *chip;
+  char cmd;
+  int amount, i, j, k;
+  int successful_results;
+  uint32_t result_count;
+  WorkResult response;
+
+  successful_results = 0;
+
+  // Send request for completed work
+  cmd = 'E';
+  usb_write(bitfury, &cmd, 1, &amount, C_BF_GETRES);
+
+  if(!usb_read_fixed_size(bitfury, &result_count, sizeof(result_count), TIMEOUT, C_BF_GETRES)) {
+    applog(LOG_ERR, "Got no response to request for work results");
+    return false;
+  }
+  if(result_count)
+    applog(LOG_DEBUG, "Result count %d",result_count);
+
+  for(j = 0; j < result_count; j++) {
+
+    if(!usb_read_fixed_size(bitfury, &response, sizeof(WorkResult), TIMEOUT, C_BF_GETRES)) {
+      applog(LOG_ERR, "Failed to read response data packet idx %d count %d", j, result_count);
+      return false;
+    }
+
+    if (unlikely(thr->work_restart))
+      goto cleanup;
+    // Receive work results (0 or more, with a terminating final dummy result)
+    applog(LOG_DEBUG, "Got response packet chip_id %d nonces %d", response.chip_id, response.num_nonces);
+    chip = find_chip(info, response.chip_id);
+    if(!chip) {
+      applog(LOG_ERR, "Got work result for unknown chip id %d", response.chip_id);
+      continue;
+    }
+    if(!chip->has_work) {
+      applog(LOG_WARNING, "Got spurious work results for idle ASIC %d", response.chip_id);
+    }
+    for(i = 0; i < response.num_nonces; i++) {
+      applog(LOG_DEBUG, "Checking nonce 0x%x",response.nonce[i]);
+      for(k = 0; k < WORK_HISTORY_LEN; k++) {
+        applog(LOG_DEBUG, "k=%d current_work=%p", k, chip->current_work[k]);
+        if (chip->current_work[k] && bitfury_checkresults(thr, chip->current_work[k], response.nonce[i])) {
+          successful_results++;
+          applog(LOG_INFO, "Found nonce!");
+          break;
+        }
+      }
+    }
+    chip->has_work = false;
+  }
+
+
+ cleanup:
+  bitfury_empty_buffer(bitfury);
+  return successful_results;
+}
+
+static int64_t bitfury_scanhash(struct thr_info *thr, struct work *work,
+				int64_t __maybe_unused max_nonce)
+{
+	struct cgpu_info *bitfury = thr->cgpu;
+	struct drillbit_info *info = bitfury->device_data;
+        struct drillbit_chip_info *chip;
+	struct timeval tv_now;
+	int amount, i, j;
+	int ms_diff;
+        uint8_t cmd;
+        int result_count;
+        WorkRequest request;
+        char *tmp;
+
+        result_count = 0;
+
+        chip = NULL;
+	cgtime(&info->tv_start);
+        ms_diff = TIMEOUT;
+        while(chip == NULL) { // wait for a free chip to open up
+          result_count += check_for_results(thr);
+          for(i = 0; i < info->num_chips; i++) {
+            if(!info->chips[i].has_work) {
+              chip = &info->chips[i];
+              break;
+            }
+          }
+
+          cgtime(&tv_now);
+          ms_diff = ms_tdiff(&tv_now, &info->tv_start);
+          if(ms_diff > TIMEOUT) {
+            applog(LOG_ERR, "Timed out waiting for any results to come back from ASICs.");
+            goto cascade;
+          }
+        }
+
+        if(!chip) {
+          return 0;
+        }
+
+        applog(LOG_DEBUG, "Sending work to chip_id %d", chip->chip_id);
+        request.chip_id = chip->chip_id;
+	memcpy(&request.midstate, work->midstate, 32);
+	memcpy(&request.data, work->data + 64, 12);
+
+        /*
+        tmp = bin2hex(work->midstate, 32);
+        applog(LOG_DEBUG, "Midstate %s", tmp);
+        free(tmp);
+        tmp = bin2hex(work->data+64, 12);
+        applog(LOG_DEBUG, "Data %s", tmp);
+        free(tmp);*/
+
+        applog(LOG_DEBUG, "Data %x %x %x", *((uint32_t*)(work->data+64)),
+               *((uint32_t*)(work->data+64+4)), *((uint32_t*)(work->data+64+8)));
+
+        uint32_t *data = request.data;
+        applog(LOG_DEBUG, "Data in request %x %x %x", data[0],data[1],data[2]);
+
+	/* Send work to cgminer */
+        cmd = 'W';
+	usb_write(bitfury, &cmd, 1, &amount, C_BF_REQWORK);
+	usb_write(bitfury, (void *)&request, sizeof(request), &amount, C_BF_REQWORK);
+
+	if (unlikely(thr->work_restart))
+		goto cascade;
+
+        /* Expect a single 'W' byte as acknowledgement */
+        usb_read_simple_response(bitfury, 'W', C_BF_REQWORK); // TODO: verify response
+        chip->has_work = true;
+
+        // Read into work history
+        if(chip->current_work[0])
+          free_work(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] = copy_work(work);
+        applog(LOG_DEBUG, "assigned new current_work=%p", chip->current_work[WORK_HISTORY_LEN-1]);
+
+        bitfury_empty_buffer(bitfury);
+cascade:
+	work->blk.nonce = 0xffffffff;
+
+	if (unlikely(bitfury->usbinfo.nodev)) {
+		applog(LOG_WARNING, "%s %d: Device disappeared, disabling thread",
+		       bitfury->drv->name, bitfury->device_id);
+		return -1;
+	}
+	return result_count * 0xffffffffULL;
+}
+
+static struct api_data *bitfury_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, "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);
+
+	return root;
+}
+
+static void bitfury_init(struct cgpu_info  *bitfury)
+{
+	bitfury_close(bitfury);
+	bitfury_open(bitfury);
+	bitfury_reset(bitfury);
+}
+
+static void bitfury_shutdown(struct thr_info *thr)
+{
+	struct cgpu_info *bitfury = thr->cgpu;
+
+	bitfury_close(bitfury);
+}
+
+/* Currently hardcoded to BF1 devices */
+struct device_drv bitfury_drv = {
+	.drv_id = DRIVER_drillbit,
+	.dname = "Drillbit",
+	.name = "Drillbit",
+	.drv_detect = bitfury_detect,
+	.scanhash = bitfury_scanhash,
+	.get_api_stats = bitfury_api_stats,
+	.reinit_device = bitfury_init,
+	.thread_shutdown = bitfury_shutdown,
+	.identify_device = bitfury_identify
+};
diff --git a/driver-drillbit.h b/driver-drillbit.h
new file mode 100644
index 0000000..7ab294b
--- /dev/null
+++ b/driver-drillbit.h
@@ -0,0 +1,37 @@
+/*
+ * 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 8
+
+struct drillbit_chip_info;
+
+/* drillbit_info structure applies to entire device */
+struct drillbit_info {
+  struct cgpu_info *base_cgpu;
+  uint8_t version;
+  char product[8];
+  uint32_t serial;
+  uint8_t num_chips;
+  struct drillbit_chip_info *chips;
+  struct timeval tv_start;
+};
+
+struct drillbit_chip_info {
+  uint16_t chip_id;
+  struct work *current_work[WORK_HISTORY_LEN];
+  bool has_work;
+};
+
+#endif /* BITFURY_H */
diff --git a/miner.h b/miner.h
index 2138519..bd2ce6b 100644
--- a/miner.h
+++ b/miner.h
@@ -239,7 +239,8 @@ static inline int fsync (int fd)
 	DRIVER_ADD_COMMAND(bflsc) \
 	DRIVER_ADD_COMMAND(bitfury) \
 	DRIVER_ADD_COMMAND(avalon) \
-	DRIVER_ADD_COMMAND(klondike)
+	DRIVER_ADD_COMMAND(klondike) \
+        DRIVER_ADD_COMMAND(drillbit)
 
 #define DRIVER_PARSE_COMMANDS(DRIVER_ADD_COMMAND) \
 	DRIVER_ADD_COMMAND(opencl) \
diff --git a/usbutils.c b/usbutils.c
index d1bfef5..a139ea7 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -48,6 +48,7 @@
 #define BFLSC_TIMEOUT_MS 999
 #define BITFORCE_TIMEOUT_MS 999
 #define BITFURY_TIMEOUT_MS 999
+#define DRILLBIT_TIMEOUT_MS 999
 #define MODMINER_TIMEOUT_MS 999
 #define AVALON_TIMEOUT_MS 999
 #define KLONDIKE_TIMEOUT_MS 999
@@ -56,6 +57,7 @@
 #define BFLSC_TIMEOUT_MS 300
 #define BITFORCE_TIMEOUT_MS 200
 #define BITFURY_TIMEOUT_MS 100
+#define DRILLBIT_TIMEOUT_MS 200
 #define MODMINER_TIMEOUT_MS 100
 #define AVALON_TIMEOUT_MS 200
 #define KLONDIKE_TIMEOUT_MS 200
@@ -103,19 +105,38 @@ static struct usb_intinfo bfl_ints[] = {
 #endif
 
 #ifdef USE_BITFURY
-static struct usb_epinfo bfu0_epinfos[] = {
+// Bitfury BF1
+static struct usb_epinfo bf1_int_epinfos[] = {
 	{ LIBUSB_TRANSFER_TYPE_INTERRUPT,	8,	EPI(2), 0, 0, 0 }
 };
 
-static struct usb_epinfo bfu1_epinfos[] = {
+static struct usb_epinfo bf1_bulk_epinfos[] = {
 	{ LIBUSB_TRANSFER_TYPE_BULK,	16,	EPI(3), 0, 0, 0 },
 	{ LIBUSB_TRANSFER_TYPE_BULK,	16,	EPO(4), 0, 0, 0 }
 };
 
 /* Default to interface 1 */
-static struct usb_intinfo bfu_ints[] = {
-	USB_EPS(1,  bfu1_epinfos),
-	USB_EPS(0,  bfu0_epinfos)
+static struct usb_intinfo bf1_ints[] = {
+	USB_EPS(1,  bf1_bulk_epinfos),
+	USB_EPS(0,  bf1_int_epinfos)
+};
+#endif
+
+#ifdef USE_DRILLBIT
+// Drillbit Bitfury devices
+static struct usb_epinfo drillbit_int_epinfos[] = {
+	{ LIBUSB_TRANSFER_TYPE_INTERRUPT,	8,	EPI(3), 0, 0, 0 }
+};
+
+static struct usb_epinfo drillbit_bulk_epinfos[] = {
+	{ LIBUSB_TRANSFER_TYPE_BULK,	16,	EPI(1), 0, 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	16,	EPO(2), 0, 0, 0 }
+};
+
+/* Default to interface 1 */
+static struct usb_intinfo drillbit_ints[] = {
+	USB_EPS(1,  drillbit_bulk_epinfos),
+	USB_EPS(0,  drillbit_int_epinfos)
 };
 #endif
 
@@ -266,7 +287,22 @@ static struct usb_find_devices find_dev[] = {
 		.latency = LATENCY_UNUSED,
 		.iManufacturer = "BPMC",
 		.iProduct = "Bitfury BF1",
-		INTINFO(bfu_ints)
+		INTINFO(bf1_ints)
+	},
+#endif
+#ifdef USE_DRILLBIT
+	{
+		.drv = DRIVER_drillbit,
+		.name = "Drillbit",
+		.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)
 	},
 #endif
 #ifdef USE_MODMINER
@@ -3110,6 +3146,7 @@ void usb_cleanup()
 			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 7b499ee..6f228bc 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -151,7 +151,8 @@ enum sub_ident {
 	IDENT_LLT,
 	IDENT_CMR1,
 	IDENT_CMR2,
-	IDENT_ZTX
+	IDENT_ZTX,
+        IDENT_DRB
 };
 
 struct usb_find_devices {
@@ -329,20 +330,21 @@ 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_BF1_RESET, "BF1Reset") \
-	USB_ADD_COMMAND(C_BF1_OPEN, "BF1Open") \
-	USB_ADD_COMMAND(C_BF1_INIT, "BF1Init") \
-	USB_ADD_COMMAND(C_BF1_CLOSE, "BF1Close") \
-	USB_ADD_COMMAND(C_BF1_REQINFO, "BF1RequestInfo") \
-	USB_ADD_COMMAND(C_BF1_GETINFO, "BF1GetInfo") \
-	USB_ADD_COMMAND(C_BF1_REQRESET, "BF1RequestReset") \
-	USB_ADD_COMMAND(C_BF1_GETRESET, "BF1GetReset") \
-	USB_ADD_COMMAND(C_BF1_REQWORK, "BF1RequestWork") \
-	USB_ADD_COMMAND(C_BF1_GETWORK, "BF1GetWork") \
-	USB_ADD_COMMAND(C_BF1_GETRES, "BF1GetResults") \
-	USB_ADD_COMMAND(C_BF1_FLUSH, "BF1Flush") \
-	USB_ADD_COMMAND(C_BF1_IFLUSH, "BF1InterruptFlush") \
-	USB_ADD_COMMAND(C_BF1_IDENTIFY, "BF1Identify")
+	USB_ADD_COMMAND(C_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")
 
 /* Create usb_cmds enum from USB_PARSE_COMMANDS macro */
 enum usb_cmds {