Commit 19050c6a29dd72dd51cf7d74dc46d3b8c7e50b8e

Angus Gratton 2013-12-03T19:23:17

Tab-indent drillbit driver, to match rest of cgminer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
diff --git a/driver-drillbit.c b/driver-drillbit.c
index 6de1ee7..ef120cd 100644
--- a/driver-drillbit.c
+++ b/driver-drillbit.c
@@ -36,9 +36,9 @@
 
 typedef struct
 {
-  uint16_t chip_id;
-  uint8_t midstate[32];
-  uint8_t data[12];
+        uint16_t chip_id;
+        uint8_t midstate[32];
+        uint8_t data[12];
 } WorkRequest;
 
 #define SZ_SERIALISED_WORKREQUEST 46
@@ -46,10 +46,10 @@ static void serialise_work_request(uint8_t *buf, uint16_t chip_id, const struct 
 
 typedef struct
 {
-  uint16_t chip_id;
-  uint8_t num_nonces;
-  uint8_t is_idle;
-  uint32_t nonce[MAX_RESULTS];
+        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)
@@ -66,11 +66,11 @@ static void deserialise_work_result(WorkResult *work_result, const uint8_t *buf)
 
 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;
+        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;
 } BoardConfig;
 
 #define SZ_SERIALISED_BOARDCONFIG 6
@@ -78,11 +78,11 @@ static void serialise_board_config(uint8_t *buf, const BoardConfig *boardconfig)
 
 typedef struct
 {
-    uint8_t protocol_version;
-    uint8_t product[8];
-    uint32_t serial;
-    uint8_t num_chips;
-    uint16_t capabilities;
+        uint8_t protocol_version;
+        uint8_t product[8];
+        uint32_t serial;
+        uint8_t num_chips;
+        uint16_t capabilities;
 } Identity;
 
 #define SZ_SERIALISED_IDENTITY 16
@@ -90,62 +90,62 @@ static void deserialise_identity(Identity *identity, const uint8_t *buf);
 
 // Hashable structure of per-device config settings
 typedef struct {
-  uint8_t key[9];
-  BoardConfig config;
-  UT_hash_handle hh;
+        uint8_t 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,
-  use_ext_clock: 0,
-  int_clock_level: 40,
-  clock_div2: 0,
-  ext_clock_freq: 200
- },
+key: { 0 },
+config: {
+        core_voltage: CONFIG_CORE_085V,
+        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;
+        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) {
-  uint8_t *res = (uint8_t *)result;
-  char *hex;
-  int count, ms_left;
-  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 %d bytes / %lu (timeout %d)", count, result_size, timeout);
-  hex = bin2hex(res, count);
-  drvlog(LOG_DEBUG, "%s", hex);
-  free(hex);
-  return false;
+        uint8_t *res = (uint8_t *)result;
+        char *hex;
+        int count, ms_left;
+        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 %d bytes / %lu (timeout %d)", count, result_size, timeout);
+        hex = bin2hex(res, count);
+        drvlog(LOG_DEBUG, "%s", hex);
+        free(hex);
+        return false;
 }
 
 static bool usb_read_simple_response(struct cgpu_info *drillbit, char command, enum usb_cmds command_name);
@@ -154,13 +154,13 @@ static bool usb_read_simple_response(struct cgpu_info *drillbit, char command, e
    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(drillbit, &command, 1, &amount, 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);
+        int amount;
+        usb_write(drillbit, &command, 1, &amount, 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);
 }
 
 
@@ -168,281 +168,281 @@ static bool usb_send_simple_command(struct cgpu_info *drillbit, char command, en
    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, C_BF_GETRES);
-  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;
+        int amount;
+        char response;
+        /* Expect a single byte, matching the command, as acknowledgement */
+        usb_read_timeout(drillbit, &response, 1, &amount, TIMEOUT, C_BF_GETRES);
+        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;
+        char buf[512];
+        int amount;
 
-	do {
-          usb_read_timeout(drillbit, buf, 512, &amount, 5, C_BF_FLUSH);
-	} while (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);
+        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);
+        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)
 {
-	int amount;
+        int amount;
 
         usb_send_simple_command(drillbit, 'L', C_BF_IDENTIFY);
 }
 
 static bool drillbit_getinfo(struct cgpu_info *drillbit, struct drillbit_info *info)
 {
-	int amount, err;
+        int amount, err;
         uint8_t buf[SZ_SERIALISED_IDENTITY];
         Identity identity;
 
-	drillbit_empty_buffer(drillbit);
-	err = usb_write(drillbit, "I", 1, &amount, C_BF_REQINFO);
-	if (err) {
+        drillbit_empty_buffer(drillbit);
+        err = usb_write(drillbit, "I", 1, &amount, 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 %lu",
+        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 %lu",
                         amount, sizeof(Identity));
-		return false;
-	}
+                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;
+                drvlog(LOG_ERR, "Got invalid contents for GETINFO identity response");
+                return false;
         }
 
         const int MIN_VERSION = 2;
         const int MAX_VERSION = 2;
         if(identity.protocol_version < MIN_VERSION) {
-          drvlog(LOG_ERR, "Unknown device protocol version %d.", identity.protocol_version);
-          return false;
+                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;
+                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;
         }
 
         // load identity data into device info structure
-	info->version = identity.protocol_version;
+        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");
+                // 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));
+                memcpy(info->product, identity.product, sizeof(identity.product));
         }
         info->serial = identity.serial;
         info->num_chips = identity.num_chips;
 
-	drvlog(LOG_INFO, "Getinfo returned version %d, product %s serial %08x num_chips %d",
+        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;
+        drillbit_empty_buffer(drillbit);
+        return true;
 }
 
 static bool drillbit_reset(struct cgpu_info *drillbit)
 {
-	int amount, err;
+        int amount, err;
         if(!usb_send_simple_command(drillbit, 'R', C_BF_REQRESET))
-          return false;
-        
-	drillbit_empty_buffer(drillbit);
-	return true;
+                return false;
+
+        drillbit_empty_buffer(drillbit);
+        return true;
 }
 
 static void drillbit_send_config(struct cgpu_info *drillbit)
 {
-  struct drillbit_info *info = drillbit->device_data;
-  char cmd;
-  int amount;
-  uint8_t search_key[9];
-  uint8_t buf[SZ_SERIALISED_BOARDCONFIG];
-  config_setting *setting;
-
-  // Find the relevant board config
-
-  // 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);
-  } 
-  else {
-    // 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);
-    }
-    else {
-      // 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);
-    }
-  }
-
-  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);
-  cmd = 'C';
-  usb_write(drillbit, &cmd, 1, &amount, C_BF_REQWORK);
-
-  serialise_board_config(buf, &setting->config);
-  usb_write(drillbit, buf, SZ_SERIALISED_BOARDCONFIG, &amount, C_BF_CONFIG);
-
-  /* Expect a single 'C' byte as acknowledgement */
-  usb_read_simple_response(drillbit, 'C', C_BF_CONFIG); // TODO: verify response
+        struct drillbit_info *info = drillbit->device_data;
+        char cmd;
+        int amount;
+        uint8_t search_key[9];
+        uint8_t buf[SZ_SERIALISED_BOARDCONFIG];
+        config_setting *setting;
+
+        // Find the relevant board config
+
+        // 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);
+        } 
+        else {
+                // 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);
+                }
+                else {
+                        // 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);
+                }
+        }
+
+        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);
+        cmd = 'C';
+        usb_write(drillbit, &cmd, 1, &amount, C_BF_REQWORK);
+
+        serialise_board_config(buf, &setting->config);
+        usb_write(drillbit, buf, SZ_SERIALISED_BOARDCONFIG, &amount, C_BF_CONFIG);
+
+        /* Expect a single 'C' byte as acknowledgement */
+        usb_read_simple_response(drillbit, 'C', C_BF_CONFIG); // TODO: verify response
 }
 
 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;
-    uint8_t 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;
-    }
-
-    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;
+        /* 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;
+                uint8_t 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;
+                }
+
+                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;
+        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
+                return false; // Bit of a hack doing this here, should do it somewhere else
 
-	drillbit = usb_alloc_cgpu(&drillbit_drv, 1);
+        drillbit = usb_alloc_cgpu(&drillbit_drv, 1);
 
-	if (!usb_init(drillbit, dev, found))
+        if (!usb_init(drillbit, dev, found))
 		goto out;
 	drvlog(LOG_INFO, "Found at %s", drillbit->device_path);
 
@@ -468,7 +468,7 @@ static struct cgpu_info *drillbit_detect_one(struct libusb_device *dev, struct u
            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;
+                info->chips[i].chip_id = i;
         }
         cgtime(&info->tv_lastchipinfo);
 
@@ -480,7 +480,7 @@ static struct cgpu_info *drillbit_detect_one(struct libusb_device *dev, struct u
         drillbit_send_config(drillbit);
 
 	drvlog(LOG_INFO, "Successfully initialised %s",
-	       drillbit->device_path);
+                drillbit->device_path);
 
 	return drillbit;
 out_close:
@@ -539,74 +539,74 @@ static bool drillbit_checkresults(struct thr_info *thr, struct work *work, uint3
 // 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, j, k;
-  int successful_results, found;
-  uint32_t result_count;
-  uint8_t buf[SZ_SERIALISED_WORKRESULT];
-  WorkResult response;
-
-  successful_results = 0;
-
-  // Send request for completed work
-  cmd = 'E';
-  usb_write(drillbit, &cmd, 1, &amount, 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");
-    return false;
-  }
-  if(result_count)
-    drvlog(LOG_DEBUG, "Result count %d",result_count);
-
-  // Receive work results (0 or more)
-  for(j = 0; j < result_count; j++) {
-
-    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);
-      return 0;
-    }
-    deserialise_work_result(&response, buf);
-
-    if (unlikely(thr->work_restart))
-      goto cleanup;
-    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);
-    }
-    for(i = 0; i < response.num_nonces; i++) {
-      found = false;
-      for(k = 0; k < WORK_HISTORY_LEN; k++) {
-        if (chip->current_work[k] && drillbit_checkresults(thr, chip->current_work[k], response.nonce[i])) {
-          chip->success_count++;
-          successful_results++;
-          found = true;
-          break;
+        struct cgpu_info *drillbit = thr->cgpu;
+        struct drillbit_info *info = drillbit->device_data;
+        struct drillbit_chip_info *chip;
+        char cmd;
+        int amount, i, j, k;
+        int successful_results, found;
+        uint32_t result_count;
+        uint8_t buf[SZ_SERIALISED_WORKRESULT];
+        WorkResult response;
+
+        successful_results = 0;
+
+        // Send request for completed work
+        cmd = 'E';
+        usb_write(drillbit, &cmd, 1, &amount, 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");
+                return false;
         }
-      }
-      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:
-  return successful_results;
+        if(result_count)
+                drvlog(LOG_DEBUG, "Result count %d",result_count);
+
+        // Receive work results (0 or more)
+        for(j = 0; j < result_count; j++) {
+
+                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);
+                        return 0;
+                }
+                deserialise_work_result(&response, buf);
+
+                if (unlikely(thr->work_restart))
+                        goto cleanup;
+                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);
+                }
+                for(i = 0; i < response.num_nonces; i++) {
+                        found = false;
+                        for(k = 0; k < WORK_HISTORY_LEN; k++) {
+                                if (chip->current_work[k] && drillbit_checkresults(thr, chip->current_work[k], response.nonce[i])) {
+                                        chip->success_count++;
+                                        successful_results++;
+                                        found = true;
+                                        break;
+                                }
+                        }
+                        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:
+        return successful_results;
 }
 
 static int64_t drillbit_scanwork(struct thr_info *thr)
@@ -635,19 +635,19 @@ static int64_t drillbit_scanwork(struct thr_info *thr)
         // 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) {
-            drvlog(LOG_ERR, "Timing out unresponsive ASIC %d", info->chips[i].chip_id);
-            info->chips[i].state = IDLE;
-            info->chips[i].timeout_count++;
-            chip = &info->chips[i];
-          }
+                if(info->chips[i].state == IDLE)
+                        continue;
+                ms_diff = ms_tdiff(&tv_now, &info->chips[i].tv_start);
+                if(ms_diff > TIMEOUT) {
+                        drvlog(LOG_ERR, "Timing out unresponsive ASIC %d", info->chips[i].chip_id);
+                        info->chips[i].state = IDLE;
+                        info->chips[i].timeout_count++;
+                        chip = &info->chips[i];
+                }
         }
 
         if(chip == NULL) { // nothing available to send work to!
-          goto cascade;
+                goto cascade;
         }
 
         /* Get some new work for the chip */
@@ -671,38 +671,38 @@ static int64_t drillbit_scanwork(struct thr_info *thr)
         /* 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;
+                chip->state = WORKING_QUEUED;
         else
-          chip->state = WORKING_NOQUEUED;
+                chip->state = WORKING_NOQUEUED;
 
         // Read into work history
         if(chip->current_work[0])
-          work_completed(drillbit, 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[i] = chip->current_work[i+1];
         chip->current_work[WORK_HISTORY_LEN-1] = copy_work(work);
         cgtime(&chip->tv_start);
 
         /* 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);
-          for(i = 0; i < info->num_chips; i++) {
-            chip= &info->chips[i];
-            j = snprintf(buf+amount, sizeof(buf)-amount, " %d:%d/%d/%d",
-                         chip->chip_id, chip->success_count, chip->error_count,
-                         chip->timeout_count);
-            if(j < 0)
-              break;
-            amount += j;
-            if(amount >= sizeof(buf))
-               break;
-          }
-          drvlog(LOG_INFO, "%s", buf);
-          cgtime(&info->tv_lastchipinfo);
+                /* 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);
+                for(i = 0; i < info->num_chips; i++) {
+                        chip= &info->chips[i];
+                        j = snprintf(buf+amount, sizeof(buf)-amount, " %d:%d/%d/%d",
+                                chip->chip_id, chip->success_count, chip->error_count,
+                                chip->timeout_count);
+                        if(j < 0)
+                                break;
+                        amount += j;
+                        if(amount >= sizeof(buf))
+                                break;
+                }
+                drvlog(LOG_INFO, "%s", buf);
+                cgtime(&info->tv_lastchipinfo);
         }
 
 cascade:
@@ -710,7 +710,7 @@ cascade:
 
 	if (unlikely(drillbit->usbinfo.nodev)) {
 		drvlog(LOG_WARNING, "%s %d: Device disappeared, disabling thread",
-		       drillbit->drv->name, drillbit->device_id);
+                        drillbit->drv->name, drillbit->device_id);
 		return -1;
 	}
 	return 0xffffffffULL * result_count;
@@ -763,53 +763,53 @@ struct device_drv drillbit_drv = {
 
 /* Structure serialisation/deserialisation */
 
-#define SERIALISE(FIELD) do {                    \
-    memcpy(&buf[offset], &FIELD, sizeof(FIELD)); \
-    offset += sizeof(FIELD);                     \
-  } while(0)
+#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)
+#define DESERIALISE(FIELD) do {                                 \
+                memcpy(&FIELD, &buf[offset], sizeof(FIELD));    \
+                offset += sizeof(FIELD);                        \
+        } while(0)
 
 static void serialise_work_request(uint8_t *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;
+        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 uint8_t *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]);
+        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(uint8_t *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);
+        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 uint8_t *buf)
 {
-  size_t offset = 0;
-  DESERIALISE(id->protocol_version);
-  DESERIALISE(id->product);
-  DESERIALISE(id->serial);
-  DESERIALISE(id->num_chips);
-  DESERIALISE(id->capabilities);
+        size_t offset = 0;
+        DESERIALISE(id->protocol_version);
+        DESERIALISE(id->product);
+        DESERIALISE(id->serial);
+        DESERIALISE(id->num_chips);
+        DESERIALISE(id->capabilities);
 }