Commit 11109ceef4663f2ca667bd62fcbfefb96301220a

kanoi 2014-07-03T05:23:06

icarus - tidy up rbox code, remove statics, and add rocketbox

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
diff --git a/driver-icarus.c b/driver-icarus.c
index 3acf961..8a22221 100644
--- a/driver-icarus.c
+++ b/driver-icarus.c
@@ -50,8 +50,6 @@
 #include "miner.h"
 #include "usbutils.h"
 
-#define ROCKMINER
-#define ROCKMINER_PRINT_DEBUG 0
 // The serial I/O speed - Linux uses a define 'B115200' in bits/termios.h
 #define ICARUS_IO_SPEED 115200
 
@@ -188,6 +186,70 @@ static const char *MODE_LONG_STREQ = "long=";
 static const char *MODE_VALUE_STR = "value";
 static const char *MODE_UNKNOWN_STR = "unknown";
 
+#define MAX_DEVICE_NUM 100
+#define MAX_WORK_BUFFER_SIZE 2
+#define MAX_CHIP_NUM 24
+// Set it to 3 or 9
+#define	NONCE_CORRECTION_TIMES	3
+#define MAX_TRIES	4
+#define	RM_CMD_MASK		0x0F
+#define	RM_STATUS_MASK		0xF0
+#define	RM_CHIP_MASK		0x3F
+#define	RM_PRODUCT_MASK		0xC0
+#define	RM_PRODUCT_RBOX		0x00
+#define	RM_PRODUCT_T1		0x40
+#define	RM_PRODUCT_T2		0x80
+#define	RM_PRODUCT_TEST		0xC0
+
+typedef enum {
+	NONCE_DATA1_OFFSET= 0,
+	NONCE_DATA2_OFFSET,
+	NONCE_DATA3_OFFSET,
+	NONCE_DATA4_OFFSET,
+	NONCE_TASK_CMD_OFFSET,
+	NONCE_CHIP_NO_OFFSET,
+	NONCE_TASK_NO_OFFSET,
+	NONCE_COMMAND_OFFSET,
+	NONCE_MAX_OFFSET
+} NONCE_OFFSET;
+
+typedef enum {
+	NONCE_DATA_CMD= 0,
+	NONCE_TASK_COMPLETE_CMD,
+	NONCE_GET_TASK_CMD,
+} NONCE_COMMAND;
+
+typedef struct NONCE_DATA {
+	int chip_no;
+	unsigned int task_no ;
+	unsigned char work_state;
+	int cmd_value;
+} NONCE_DATA;
+
+typedef enum {
+	ROCKMINER_RBOX = 0,
+	ROCKMINER_T1,
+	ROCKMINER_T2,
+	ROCKMINER_MAX
+} ROCKMINER_PRODUCT_T;
+
+typedef struct rockminer_chip_info {
+	unsigned char freq;
+	int error_cnt;
+	time_t last_received_task_complete_time;
+} ROCKMINER_CHIP_INFO;
+
+typedef struct rockminer_device_info {
+	unsigned char detect_chip_no;
+	unsigned char chip_max;
+	unsigned char product_id;
+	float min_frq;
+	float def_frq;
+	float max_frq;
+	ROCKMINER_CHIP_INFO chip[MAX_CHIP_NUM];
+	time_t dev_detect_time;
+} ROCKMINER_DEVICE_INFO;
+
 struct ICARUS_INFO {
 	enum sub_ident ident;
 	int intinfo;
@@ -237,6 +299,9 @@ struct ICARUS_INFO {
 	int nonce_size;
 
 	bool failing;
+
+	ROCKMINER_DEVICE_INFO rmdev;
+	struct work *g_work[MAX_CHIP_NUM][MAX_WORK_BUFFER_SIZE];
 };
 
 #define ICARUS_MIDSTATE_SIZE 32
@@ -275,84 +340,6 @@ struct ICARUS_WORK {
 	uint8_t work[ICARUS_WORK_SIZE];
 };
 
-#define MAX_DEVICE_NUM 100
-#define MAX_WORK_BUFFER_SIZE 2
-#define MAX_CHIP_NUM 24
-#define	NONCE_CORRECTION_TIMES	3
-#define MAX_TRIES	4
-//#define	NONCE_TEST
-#define	RM_CMD_MASK		0x0F
-#define	RM_STATUS_MASK		0xF0
-#define	RM_CHIP_MASK		0x3F
-#define	RM_PRODUCT_MASK		0xC0
-#define	RM_PRODUCT_RBOX		0x00
-#define	RM_PRODUCT_T1		0x10
-
-#ifdef NONCE_TEST
-int device_nonce_conunts[MAX_DEVICE_NUM];
-int chip_nonce_conunts[MAX_CHIP_NUM];
-int chip_work_conunts[MAX_CHIP_NUM];
-int device_nonce_corr_ok_1[MAX_DEVICE_NUM];
-int device_nonce_corr_ok[MAX_DEVICE_NUM];
-int device_nonce_corr_fail[MAX_DEVICE_NUM];
-int device_nonce_corr_fail_2[MAX_DEVICE_NUM];
-#endif
-
-typedef enum {
-	NONCE_DATA1_OFFSET= 0,
-	NONCE_DATA2_OFFSET,
-	NONCE_DATA3_OFFSET,
-	NONCE_DATA4_OFFSET,
-	NONCE_TASK_CMD_OFFSET,
-	NONCE_CHIP_NO_OFFSET,
-	NONCE_TASK_NO_OFFSET,
-	NONCE_COMMAND_OFFSET,
-	NONCE_MAX_OFFSET
-} NONCE_OFFSET;
-
-typedef enum {
-	NONCE_DATA_CMD= 0,
-	NONCE_TASK_COMPLETE_CMD,
-	NONCE_GET_TASK_CMD,
-} NONCE_COMMAND;
-
-typedef struct NONCE_DATA
-{
-int chip_no ;
-unsigned int task_no ;
-unsigned char work_state;
-int cmd_value;
-}NONCE_DATA;
-
-
-
-typedef enum {
-	ROCKMINER_RBOX= 0,
-	ROCKMINER_T1,
-	ROCKMINER_MAX
-} ROCKMINER_PRODUCT_T;
-
-typedef struct ROCKMINER_CHIP_INFO
-{
-unsigned char freq;
-int error_cnt;
-time_t last_received_task_complete_time;
-}ROCKMINER_CHIP_INFO;
-
-typedef struct ROCKMINER_DEVICE_INFO
-{
-unsigned char detect_chip_no;
-unsigned char chip_max;
-unsigned char product_id;
-float min_frq;
-float def_frq;
-float max_frq;
-ROCKMINER_CHIP_INFO chip[MAX_CHIP_NUM];
-time_t dev_detect_time;
-}ROCKMINER_DEVICE_INFO;
-
-ROCKMINER_DEVICE_INFO rmdev[MAX_DEVICE_NUM];
-
 #define END_CONDITION 0x0000ffff
 
 // Looking for options in --icarus-timing and --icarus-options:
@@ -513,6 +500,7 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud)
 			break;
 		case IDENT_AMU:
 		case IDENT_ANU:
+		case IDENT_LIN:
 			// Enable the UART
 			transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_IFC_ENABLE,
 				 CP210X_VALUE_UART_ENABLE,
@@ -930,15 +918,15 @@ unsigned char crc5(unsigned char *ptr, unsigned char len)
 		memcpy(crcin, crcout, 5);
 	}
 	crc = 0;
-	if(crcin[4])
+	if (crcin[4])
 		crc |= 0x10;
-	if(crcin[3])
+	if (crcin[3])
 		crc |= 0x08;
-	if(crcin[2])
+	if (crcin[2])
 		crc |= 0x04;
-	if(crcin[1])
+	if (crcin[1])
 		crc |= 0x02;
-	if(crcin[0])
+	if (crcin[0])
 		crc |= 0x01;
 	return crc;
 }
@@ -1036,45 +1024,23 @@ static bool set_anu_freq(struct cgpu_info *icarus, struct ICARUS_INFO *info)
 	return true;
 }
 
-static void rock_init_last_received_task_complete_time(uint32_t dev_id)
+static void rock_init_last_received_task_complete_time(struct ICARUS_INFO *info)
 {
 	int i;
-	if(opt_rock_freq<rmdev[dev_id].min_frq||(opt_rock_freq>rmdev[dev_id].max_frq))
-			opt_rock_freq =rmdev[dev_id].def_frq;
-	for (i = 0; i < MAX_CHIP_NUM; ++i)
-	{
-		rmdev[dev_id].chip[i].last_received_task_complete_time = time(NULL);
-		rmdev[dev_id].chip[i].freq = opt_rock_freq/10 - 1;
-		rmdev[dev_id].chip[i].error_cnt= 0;
-	}
-	rmdev[dev_id].dev_detect_time = time(NULL);
-}
 
-struct work *g_work[MAX_DEVICE_NUM][MAX_CHIP_NUM][MAX_WORK_BUFFER_SIZE];
-
-void clear_chip_busy(int device_id)
-{
-	memset(&(g_work[device_id][0]), 0, (sizeof(g_work)/MAX_DEVICE_NUM));
-}
+	if (opt_rock_freq < info->rmdev.min_frq ||
+	    opt_rock_freq > info->rmdev.max_frq)
+		opt_rock_freq =info->rmdev.def_frq;
 
-void rock_print_work_data(struct ICARUS_WORK *p_workdata)
-{
-	char *ob_hex = NULL;
-	opt_debug = ROCKMINER_PRINT_DEBUG;
-	if (opt_debug) {
-		ob_hex = bin2hex((void *)(p_workdata), sizeof(*p_workdata));
-		applog(LOG_WARNING, "sent %s", ob_hex);
-		free(ob_hex);
-
-        rev((void *)(&(p_workdata->midstate)), ICARUS_MIDSTATE_SIZE);
-    	rev((void *)(&(p_workdata->work)), ICARUS_WORK_SIZE);
-    	ob_hex = bin2hex((void *)(p_workdata), sizeof(*p_workdata));
-    	applog(LOG_WARNING, "revert sent %s", ob_hex);
-    	free(ob_hex);
+	for (i = 0; i < MAX_CHIP_NUM; ++i) {
+		info->rmdev.chip[i].last_received_task_complete_time = time(NULL);
+		info->rmdev.chip[i].freq = opt_rock_freq/10 - 1;
+		info->rmdev.chip[i].error_cnt = 0;
 	}
+
+	info->rmdev.dev_detect_time = time(NULL);
 }
 
-bool icarus_get_device_id(struct cgpu_info *cgpu);
 
 static void icarus_clear(struct cgpu_info *icarus, struct ICARUS_INFO *info)
 {
@@ -1379,7 +1345,6 @@ static struct cgpu_info *rock_detect_one(struct libusb_device *dev, struct usb_f
 {
 	struct ICARUS_INFO *info;
 	struct timeval tv_start, tv_finish;
-	int opt_debug = ROCKMINER_PRINT_DEBUG;
 	char *ob_hex = NULL;
 
 	// Block 171874 nonce = (0xa2870100) = 0x000187a2
@@ -1408,10 +1373,7 @@ static struct cgpu_info *rock_detect_one(struct libusb_device *dev, struct usb_f
 	int32_t correction_value[] = {0, 1, -1};
 #endif
 	NONCE_DATA nonce_data;
-
 	uint32_t nonce;
-	//int cmd_value = 0;
-	int dev_id;
 
 	if ((sizeof(workdata) << 1) != (sizeof(golden_ob) - 1))
 		quithere(1, "Data and golden_ob sizes don't match");
@@ -1421,16 +1383,9 @@ static struct cgpu_info *rock_detect_one(struct libusb_device *dev, struct usb_f
 	if (!usb_init(icarus, dev, found))
 		goto shin;
 
-#ifdef WIN32
-	dev_id = icarus_get_device_id(icarus);
-#else
-	dev_id = icarus->device_id;
-#endif
-
 	hex2bin((void *)(&workdata), golden_ob, sizeof(workdata));
 	rev((void *)(&(workdata.midstate)), ICARUS_MIDSTATE_SIZE);
 	rev((void *)(&(workdata.work)), ICARUS_WORK_SIZE);
-	opt_debug = ROCKMINER_PRINT_DEBUG;
 	if (opt_debug) {
 		ob_hex = bin2hex((void *)(&workdata), sizeof(workdata));
 		applog(LOG_WARNING, "%s%d: send_gold_nonce %s",
@@ -1452,100 +1407,93 @@ static struct cgpu_info *rock_detect_one(struct libusb_device *dev, struct usb_f
 	tries = MAX_TRIES;
 	ok = false;
 	while (!ok && tries-- > 0) {
-#if ROCKMINER_PRINT_DEBUG
-		applog(LOG_WARNING, "tries: %d", tries);
-#endif
+		icarus_initialise(icarus, info->baud);
+
+		applog(LOG_DEBUG, "tries: %d", tries);
 		workdata.unused[ICARUS_UNUSED_SIZE - 3] = opt_rock_freq/10 - 1;
 		workdata.unused[ICARUS_UNUSED_SIZE - 2] = (MAX_TRIES-1-tries);
-		rmdev[dev_id].detect_chip_no ++;
-			if(rmdev[dev_id].detect_chip_no >=MAX_TRIES )
-				rmdev[dev_id].detect_chip_no = 0;
-		//g_detect_chip_no[dev_id] = (g_detect_chip_no[dev_id] + 1) & MAX_CHIP_NUM;
+		info->rmdev.detect_chip_no ++;
+		if (info->rmdev.detect_chip_no >= MAX_TRIES)
+			info->rmdev.detect_chip_no = 0;
+		//g_detect_chip_no = (g_detect_chip_no + 1) & MAX_CHIP_NUM;
 
 		err = usb_write_ii(icarus, info->intinfo,
 				   (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
-		rock_print_work_data(&workdata);
 		if (err != LIBUSB_SUCCESS || amount != sizeof(workdata))
-		continue;
+			continue;
 
 		memset(nonce_bin, 0, sizeof(nonce_bin));
 		ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, NULL, 100);
-#if ROCKMINER_PRINT_DEBUG
-		applog(LOG_WARNING, "Rockminer nonce_bin: %02X, %02X, %02X, %02X, %02X, %02X, %02X, %02X", nonce_bin[0], nonce_bin[1], nonce_bin[2], nonce_bin[3], nonce_bin[4], nonce_bin[5], nonce_bin[6], nonce_bin[7]);
-#endif
-		if (ret != ICA_NONCE_OK)
-		{
-#if ROCKMINER_PRINT_DEBUG
-		applog(LOG_WARNING, "detect_one get_gold_nonce error, tries = %d.\r\n", tries);
-#endif
+		applog(LOG_ERR, "Rockminer nonce_bin: %02x %02x %02x %02x %02x %02x %02x %02x",
+				  nonce_bin[0], nonce_bin[1], nonce_bin[2], nonce_bin[3],
+				  nonce_bin[4], nonce_bin[5], nonce_bin[6], nonce_bin[7]);
+		if (ret != ICA_NONCE_OK) {
+			applog(LOG_DEBUG, "detect_one get_gold_nonce error, tries = %d", tries);
 			continue;
 		}
 
-		//device max chip
-		if((nonce_bin[NONCE_CHIP_NO_OFFSET]&RM_PRODUCT_MASK)==RM_PRODUCT_T1)
-		{
-			rmdev[dev_id].product_id = ROCKMINER_T1;
-			rmdev[dev_id].chip_max = 24;
-			rmdev[dev_id].min_frq = 200;
-			rmdev[dev_id].def_frq = 280;
-			rmdev[dev_id].max_frq = 320;
-		}
-		else
-		{
-			rmdev[dev_id].product_id= ROCKMINER_RBOX;
-			rmdev[dev_id].chip_max = 4;
-			rmdev[dev_id].min_frq = 200;
-			rmdev[dev_id].def_frq = 270;
-			rmdev[dev_id].max_frq = 290;
+		switch (nonce_bin[NONCE_CHIP_NO_OFFSET] & RM_PRODUCT_MASK) {
+			case RM_PRODUCT_T1:
+				info->rmdev.product_id = ROCKMINER_T1;
+				info->rmdev.chip_max = 24;
+				info->rmdev.min_frq = 200;
+				info->rmdev.def_frq = 280;
+				info->rmdev.max_frq = 320;
+				break;
+			case RM_PRODUCT_T2: // what's this?
+				info->rmdev.product_id = ROCKMINER_T2;
+				info->rmdev.chip_max = 16;
+				info->rmdev.min_frq = 200;
+				info->rmdev.def_frq = 300;
+				info->rmdev.max_frq = 400;
+				break;
+			case RM_PRODUCT_RBOX:
+				info->rmdev.product_id = ROCKMINER_RBOX;
+				info->rmdev.chip_max = 4;
+				info->rmdev.min_frq = 200;
+				info->rmdev.def_frq = 270;
+				info->rmdev.max_frq = 290;
+				break;
+			default:
+				continue;
 		}
+
 		nonce_data.chip_no = nonce_bin[NONCE_CHIP_NO_OFFSET]&RM_CHIP_MASK;
-		if (nonce_data.chip_no >= rmdev[dev_id].chip_max)
-		{
+		if (nonce_data.chip_no >= info->rmdev.chip_max)
 			nonce_data.chip_no = 0;
-		}
 
-		nonce_data.cmd_value = nonce_bin[NONCE_TASK_CMD_OFFSET]&RM_CMD_MASK;
-		if (nonce_data.cmd_value == NONCE_TASK_COMPLETE_CMD)
-		{
-#if ROCKMINER_PRINT_DEBUG
-			applog(LOG_WARNING, "complete g_detect_chip_no: %d", rmdev[dev_id].detect_chip_no);
-#endif
-		workdata.unused[ICARUS_UNUSED_SIZE - 3] = opt_rock_freq/10 - 1;
-			workdata.unused[ICARUS_UNUSED_SIZE - 2] =  rmdev[dev_id].detect_chip_no;
-			rmdev[dev_id].detect_chip_no ++;
-			if(rmdev[dev_id].detect_chip_no>=MAX_TRIES)
-				rmdev[dev_id].detect_chip_no = 0;
+		nonce_data.cmd_value = nonce_bin[NONCE_TASK_CMD_OFFSET] & RM_CMD_MASK;
+		if (nonce_data.cmd_value == NONCE_TASK_COMPLETE_CMD) {
+			applog(LOG_DEBUG, "complete g_detect_chip_no: %d", info->rmdev.detect_chip_no);
+			workdata.unused[ICARUS_UNUSED_SIZE - 3] = opt_rock_freq/10 - 1;
+			workdata.unused[ICARUS_UNUSED_SIZE - 2] =  info->rmdev.detect_chip_no;
+			info->rmdev.detect_chip_no ++;
+			if (info->rmdev.detect_chip_no >= MAX_TRIES)
+				info->rmdev.detect_chip_no = 0;
 
 			err = usb_write_ii(icarus, info->intinfo,
 				   (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
-			rock_print_work_data(&workdata);
 			if (err != LIBUSB_SUCCESS || amount != sizeof(workdata))
 				continue;
-#if ROCKMINER_PRINT_DEBUG
-			applog(LOG_WARNING, "send_gold_nonce usb_write_ii");
-#endif
+			applog(LOG_DEBUG, "send_gold_nonce usb_write_ii");
 			continue;
 		}
 
 		memcpy((char *)&nonce, nonce_bin, ICARUS_READ_SIZE);
 		nonce = htobe32(nonce);
-#if ROCKMINER_PRINT_DEBUG
-		applog(LOG_WARNING, "Rockminer nonce: %08X", nonce);
-#endif
+		applog(LOG_DEBUG, "Rockminer nonce: %08X", nonce);
 		correction_times = 0;
-		while(correction_times<NONCE_CORRECTION_TIMES)
-		{
+		while (correction_times < NONCE_CORRECTION_TIMES) {
 			nonce_hex = bin2hex(nonce_bin, 4);
 			if (golden_nonce_val == nonce + correction_value[correction_times]) {
-				clear_chip_busy(dev_id);
-				rock_init_last_received_task_complete_time(dev_id);
+				memset(&(info->g_work[0]), 0, sizeof(info->g_work));
+				rock_init_last_received_task_complete_time(info);
 
 				ok = true;
 				break;
 			} else {
-#if ROCKMINER_PRINT_DEBUG
-				applog(LOG_WARNING, "detect_one gold_nonce compare error times = %d\r\n", correction_times);
-#endif
+				applog(LOG_DEBUG, "detect_one gold_nonce compare error times = %d",
+						  correction_times);
 				if (tries < 0 && info->ident != IDENT_CMR2) {
 					applog(LOG_WARNING,
 						"Icarus Detect: "
@@ -1553,31 +1501,19 @@ static struct cgpu_info *rock_detect_one(struct libusb_device *dev, struct usb_f
 						icarus->device_path, nonce_hex, golden_nonce);
 				}
 
-				if(nonce == 0)
-				{
+				if (nonce == 0)
 					break;
-				}
 			}
 			free(nonce_hex);
 			correction_times++;
 		}
-
-#if ROCKMINER_PRINT_DEBUG
-			if(correction_times > 0 )
-			{
-				if (correction_times < NONCE_CORRECTION_TIMES)
-				applog(LOG_WARNING, "Rockminer correction the Golden nonce success %s%d[%d]=%d",icarus->drv->name, icarus->device_id,chip_no,correction_times);
-				else
-				applog(LOG_WARNING, "Rockminer correction the Golden nonce fail %s%d[%d]=%d",icarus->drv->name, icarus->device_id,chip_no,correction_times);
-			}
-#endif
-
 	}
 
 	if (!ok)
 		goto unshin;
+
 	applog(LOG_DEBUG, "Icarus Detect: Test succeeded at %s: got %s",
-	       icarus->device_path, golden_nonce);
+		          icarus->device_path, golden_nonce);
 
 	/* We have a real Rockminer! */
 	if (!add_cgpu(icarus))
@@ -1589,7 +1525,8 @@ static struct cgpu_info *rock_detect_one(struct libusb_device *dev, struct usb_f
 	icarus->drv->get_statline_before = &rock_statline_before;
 
 	applog(LOG_INFO, "%s%d: Found at %s",
-		icarus->drv->name, icarus->device_id, icarus->device_path);
+			  icarus->drv->name, icarus->device_id,
+			  icarus->device_path);
 
 	timersub(&tv_finish, &tv_start, &(info->golden_tv));
 
@@ -1668,47 +1605,29 @@ void rock_send_task(unsigned char chip_no, unsigned int current_task_id, struct 
 	struct ICARUS_WORK workdata;
 	char *ob_hex;
 	struct work *work = NULL;
-	int dev_id = icarus->device_id;
 
-#if ROCKMINER_PRINT_DEBUG
-	applog(LOG_WARNING, "thr_info id:%d, device_thread:%d, icarus cgminer_id: %d, device_id: %d.", thr->id, thr->device_thread, icarus->cgminer_id, icarus->device_id);
-	applog(LOG_WARNING, "::::::::::::::g_work[%d][%d][%d] = 0x%x", dev_id, chip_no, current_task_id, (int)g_work[dev_id][chip_no][current_task_id]);
-#endif
-	if (g_work[dev_id][chip_no][current_task_id] == NULL)
-	{
+	if (info->g_work[chip_no][current_task_id] == NULL) {
 		work = get_work(thr, thr->id);
 		if (work == NULL)
-		{
 			return;
-		}
-		g_work[dev_id][chip_no][current_task_id] = work;
-	}
-	else
-	{
-		work = g_work[dev_id][chip_no][current_task_id];
-#if ROCKMINER_PRINT_DEBUG
-		applog(LOG_WARNING, "::resend work");
-#endif
+		info->g_work[chip_no][current_task_id] = work;
+	} else {
+		work = info->g_work[chip_no][current_task_id];
+		applog(LOG_DEBUG, "::resend work");
 	}
 
-#if ROCKMINER_PRINT_DEBUG
-	//if(chip_no == 7)
-	applog(LOG_WARNING, "::::::::::::::g_work[%d][%d][%d] = 0x%x", dev_id, chip_no, current_task_id, (int)g_work[dev_id][chip_no][current_task_id]);
-#endif
-
 	memset((void *)(&workdata), 0, sizeof(workdata));
 	memcpy(&(workdata.midstate), work->midstate, ICARUS_MIDSTATE_SIZE);
 	memcpy(&(workdata.work), work->data + ICARUS_WORK_DATA_OFFSET, ICARUS_WORK_SIZE);
 	workdata.unused[ICARUS_UNUSED_SIZE - 4] = 0xaa;
-	if((rmdev[dev_id].chip[chip_no].freq>(rmdev[dev_id].max_frq/10-1))||(rmdev[dev_id].chip[chip_no].freq<(rmdev[dev_id].min_frq/10-1)))
-	{
-		rock_init_last_received_task_complete_time(dev_id);
-	}
-	workdata.unused[ICARUS_UNUSED_SIZE - 3] = rmdev[dev_id].chip[chip_no].freq; //icarus->freq/10 - 1; ;
+	if (info->rmdev.chip[chip_no].freq > (info->rmdev.max_frq / 10 -1) || 
+	    info->rmdev.chip[chip_no].freq < (info->rmdev.min_frq / 10 - 1))
+		rock_init_last_received_task_complete_time(info);
+
+	workdata.unused[ICARUS_UNUSED_SIZE - 3] = info->rmdev.chip[chip_no].freq; //icarus->freq/10 - 1; ;
 	workdata.unused[ICARUS_UNUSED_SIZE - 2] = chip_no ;
 	workdata.unused[ICARUS_UNUSED_SIZE - 1] = 0x55;
 
-	opt_debug = ROCKMINER_PRINT_DEBUG;
 	if (opt_debug) {
 		ob_hex = bin2hex((void *)(work->data), 128);
 		applog(LOG_WARNING, "%s%d: work->data %s",
@@ -1716,18 +1635,9 @@ void rock_send_task(unsigned char chip_no, unsigned int current_task_id, struct 
 		free(ob_hex);
 	}
 
-#ifndef ROCKMINER
-	rev((void *)(&(workdata.midstate)), ICARUS_MIDSTATE_SIZE);
-	rev((void *)(&(workdata.work)), ICARUS_WORK_SIZE);
-
-	if (info->speed_next_work || info->flash_next_work)
-		//cmr2_commands(icarus);
-#endif
-
 	// We only want results for the work we are about to send
 	usb_buffer_clear(icarus);
 
-
 	err = usb_write_ii(icarus, info->intinfo, (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
 
 	if (err < 0 || amount != sizeof(workdata)) {
@@ -1736,30 +1646,15 @@ void rock_send_task(unsigned char chip_no, unsigned int current_task_id, struct 
 		dev_error(icarus, REASON_DEV_COMMS_ERROR);
 		icarus_initialise(icarus, info->baud);
 
-		if(g_work[dev_id][chip_no][current_task_id])
+		if (info->g_work[chip_no][current_task_id])
 		{
-			free_work(g_work[dev_id][chip_no][current_task_id]);
-			g_work[dev_id][chip_no][current_task_id] = NULL;
+			free_work(info->g_work[chip_no][current_task_id]);
+			info->g_work[chip_no][current_task_id] = NULL;
 		}
 
 		return;
 	}
 
-	opt_debug = ROCKMINER_PRINT_DEBUG;
-	if (opt_debug) {
-		ob_hex = bin2hex((void *)(&workdata), sizeof(workdata));
-		applog(LOG_WARNING, "%s%d: sent %s",
-			icarus->drv->name, icarus->device_id, ob_hex);
-		free(ob_hex);
-
-        rev((void *)(&(workdata.midstate)), ICARUS_MIDSTATE_SIZE);
-    	rev((void *)(&(workdata.work)), ICARUS_WORK_SIZE);
-    	ob_hex = bin2hex((void *)(&workdata), sizeof(workdata));
-    	applog(LOG_WARNING, "%s%d: revert sent %s",
-    		icarus->drv->name, icarus->device_id, ob_hex);
-    	free(ob_hex);
-	}
-
 	return;
 }
 
@@ -2027,7 +1922,6 @@ static int64_t rock_scanwork(struct thr_info *thr)
 	int64_t hash_count = 0;
 	struct timeval tv_start, tv_finish, elapsed;
 	struct work *work = NULL;
-
 	int64_t estimate_hashes;
 	int correction_times = 0;
 #if (NONCE_CORRECTION_TIMES == 9)
@@ -2040,10 +1934,6 @@ static int64_t rock_scanwork(struct thr_info *thr)
 
 	int chip_no = 0;
 	time_t recv_time = 0;
-	//unsigned int task_no = 0;
-	//	int cmd_value = 0;
-	int dev_id = icarus->device_id;
-	//unsigned char work_state;
 
 	if (unlikely(share_work_tdiff(icarus) > info->fail_time)) {
 		if (info->failing) {
@@ -2067,97 +1957,49 @@ static int64_t rock_scanwork(struct thr_info *thr)
 
 	elapsed.tv_sec = elapsed.tv_usec = 0;
 
-
-	for (chip_no = 0; chip_no < rmdev[dev_id].chip_max; chip_no++)
-	{
+	for (chip_no = 0; chip_no < info->rmdev.chip_max; chip_no++) {
 		recv_time = time(NULL);
-		if (recv_time > rmdev[dev_id].chip[chip_no].last_received_task_complete_time + 1)
-		{
-#if NONCE_TEST
-			applog(LOG_WARNING, ":::chip_no error = %d nonce timeout", chip_no);
-#endif
-			rmdev[dev_id].chip[chip_no].last_received_task_complete_time = recv_time;
+		if (recv_time > info->rmdev.chip[chip_no].last_received_task_complete_time + 1) {
+			info->rmdev.chip[chip_no].last_received_task_complete_time = recv_time;
 			rock_send_task(chip_no, 0,thr);
 			break;
 		}
 	}
 
-	/* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */
 	memset(nonce_bin, 0, sizeof(nonce_bin));
 	ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, thr, 3000);//info->read_time);
-#if ROCKMINER_PRINT_DEBUG
-	applog(LOG_WARNING, "Rockminer nonce_bin: ret = %d. %02X, %02X, %02X, %02X, %02X, %02X, %02X, %02X", ret, nonce_bin[0], nonce_bin[1], nonce_bin[2], nonce_bin[3], nonce_bin[4], nonce_bin[5], nonce_bin[6], nonce_bin[7]);
-#endif
-	if((nonce_bin[NONCE_CHIP_NO_OFFSET]&RM_PRODUCT_MASK)==RM_PRODUCT_T1)
-		{
-			rmdev[dev_id].product_id = ROCKMINER_T1;
-			rmdev[dev_id].chip_max = 24;
-			rmdev[dev_id].min_frq = 200;
-			rmdev[dev_id].def_frq = 280;
-			rmdev[dev_id].max_frq = 320;
-		}
-		else
-		{
-			rmdev[dev_id].product_id= ROCKMINER_RBOX;
-			rmdev[dev_id].chip_max = 4;
-			rmdev[dev_id].min_frq = 200;
-			rmdev[dev_id].def_frq = 270;
-			rmdev[dev_id].max_frq = 290;
-		}
-	nonce_data.chip_no = nonce_bin[NONCE_CHIP_NO_OFFSET]&RM_CHIP_MASK;
-	if(nonce_data.chip_no>=rmdev[dev_id].chip_max)
-		nonce_data.chip_no=0;
-	nonce_data.task_no = nonce_bin[NONCE_TASK_NO_OFFSET]>=2?0:nonce_bin[NONCE_TASK_NO_OFFSET];
-	nonce_data.cmd_value = nonce_bin[NONCE_TASK_CMD_OFFSET]&RM_CMD_MASK;
-	nonce_data.work_state = nonce_bin[NONCE_TASK_CMD_OFFSET]&RM_STATUS_MASK;
-#if ROCKMINER_PRINT_DEBUG
-	if(nonce_data.work_state == 2)
-		applog(LOG_WARNING, "Temperature of Device is too hign to work!");
-#endif
+
+	nonce_data.chip_no = nonce_bin[NONCE_CHIP_NO_OFFSET] & RM_CHIP_MASK;
+	if (nonce_data.chip_no >= info->rmdev.chip_max)
+		nonce_data.chip_no = 0;
+	nonce_data.task_no = (nonce_bin[NONCE_TASK_NO_OFFSET] >= 2) ? 0 : nonce_bin[NONCE_TASK_NO_OFFSET];
+	nonce_data.cmd_value = nonce_bin[NONCE_TASK_CMD_OFFSET] & RM_CMD_MASK;
+	nonce_data.work_state = nonce_bin[NONCE_TASK_CMD_OFFSET] & RM_STATUS_MASK;
 
 	icarus->temp = (double)nonce_bin[NONCE_COMMAND_OFFSET];
 	if (icarus->temp == 128)
 		icarus->temp = 0;
 
-	if (nonce_data.cmd_value == NONCE_TASK_COMPLETE_CMD)
-	{
-		rmdev[dev_id].chip[nonce_data.chip_no].last_received_task_complete_time = time(NULL);
-
-		if (g_work[dev_id][nonce_data.chip_no][nonce_data.task_no])
-		{
-			free_work(g_work[dev_id][nonce_data.chip_no][nonce_data.task_no]);
-			g_work[dev_id][nonce_data.chip_no][nonce_data.task_no] = NULL;
+	if (nonce_data.cmd_value == NONCE_TASK_COMPLETE_CMD) {
+		info->rmdev.chip[nonce_data.chip_no].last_received_task_complete_time = time(NULL);
+		if (info->g_work[nonce_data.chip_no][nonce_data.task_no]) {
+			free_work(info->g_work[nonce_data.chip_no][nonce_data.task_no]);
+			info->g_work[nonce_data.chip_no][nonce_data.task_no] = NULL;
 		}
-#ifdef NONCE_TEST// ROCKMINER_PRINT_DEBUG
-			chip_work_conunts[nonce_data.chip_no] ++;
-#endif
-
 		goto out;
 	}
 
-	if (nonce_data.cmd_value == NONCE_GET_TASK_CMD)
-	{
+	if (nonce_data.cmd_value == NONCE_GET_TASK_CMD) {
 		rock_send_task(nonce_data.chip_no, nonce_data.task_no, thr);
 		goto out;
 	}
 
 	if (ret == ICA_NONCE_TIMEOUT)
-	{
-#if ROCKMINER_PRINT_DEBUG
-		applog(LOG_WARNING, "chip_no = %d nonce timeout, ret = %d", nonce_data.chip_no, ret);
-#endif
 		rock_send_task(nonce_data.chip_no, nonce_data.task_no, thr);
-	}
-
-#if ROCKMINER_PRINT_DEBUG
-	applog(LOG_WARNING, "Rockminer chip_no: %d", nonce_data.chip_no);
-#endif
 
-	work = g_work[dev_id][nonce_data.chip_no][nonce_data.task_no];
+	work = info->g_work[nonce_data.chip_no][nonce_data.task_no];
 	if (work == NULL)
-	{
 		goto out;
-	}
 
 	if (ret == ICA_NONCE_ERROR)
 		goto out;
@@ -2186,109 +2028,37 @@ static int64_t rock_scanwork(struct thr_info *thr)
 
 	memcpy((char *)&nonce, nonce_bin, ICARUS_READ_SIZE);
 	nonce = htobe32(nonce);
-#if  ROCKMINER_PRINT_DEBUG
-	applog(LOG_WARNING, "Rockminer nonce %s%d[%d/24]-%d: %08X",icarus->drv->name, icarus->device_id,nonce_data.chip_no,nonce_data.task_no, nonce);
-#endif
-
-#ifdef NONCE_TEST// ROCKMINER_PRINT_DEBUG
-	device_nonce_conunts[dev_id] ++;
-	chip_nonce_conunts[nonce_data.chip_no] ++;
-#endif
-
 	recv_time = time(NULL);
-	if((recv_time-rmdev[dev_id].dev_detect_time )>=60)//device_chip_max[device_id]
-		{
+	if ((recv_time-info->rmdev.dev_detect_time) >= 60) {
 		unsigned char i;
-		rmdev[dev_id].dev_detect_time  = recv_time;
-#ifdef NONCE_TEST// ROCKMINER_PRINT_DEBUG
-		applog(LOG_WARNING, "AMU%d Freq: %d0MHz/ %d0MHz/ %d0MHz/ %d0MHz ",dev_id,rmdev[dev_id].chip[0].freq+1,rmdev[dev_id].chip[1].freq+1,rmdev[dev_id].chip[2].freq+1,rmdev[dev_id].chip[3].freq+1);
-#endif
-			for (i = 0; i  < rmdev[dev_id].chip_max; i ++)
-			{
-#ifdef NONCE_TEST// ROCKMINER_PRINT_DEBUG
-			applog(LOG_WARNING, "AMU%d Chip[%d] nonce=%d/%d/%d,freq:%d0MHz, ",dev_id,i,rmdev[dev_id].chip[i].error_cnt,chip_nonce_conunts[i],chip_work_conunts[i],rmdev[dev_id].chip[i].freq+1);
-			chip_nonce_conunts[i] = 0;
-			chip_work_conunts[i] = 0;
-#endif
-			if(rmdev[dev_id].chip[i].error_cnt>=12)
-				{
-				if(rmdev[dev_id].chip[i].freq>rmdev[dev_id].min_frq)	//200Mhz
-					{
-					rmdev[dev_id].chip[i].freq --;
-#ifdef  NONCE_TEST
-				applog(LOG_WARNING, "AMU%d Chip[%d]=%d SUB FREQ TO %d0MHz,DEF(%fMHz) ",dev_id,i,rmdev[dev_id].chip[i].error_cnt,rmdev[dev_id].chip[i].freq+1,opt_rock_freq);
-#endif
-					}
-				}
-			else if(rmdev[dev_id].chip[i].error_cnt<=1)
-				{
-				if(rmdev[dev_id].chip[i].freq<(rmdev[dev_id].def_frq/10-1))
-					{
-					rmdev[dev_id].chip[i].freq ++;
-#ifdef  NONCE_TEST
-				applog(LOG_WARNING, "AMU%d Chip[%d]=%d ADD FREQ TO %d0MHz,DEF(%fMHz) ",dev_id,i,rmdev[dev_id].chip[i].error_cnt,rmdev[dev_id].chip[i].freq+1,opt_rock_freq);
-#endif
-					}
-				}
-			rmdev[dev_id].chip[i].error_cnt = 0;
+		info->rmdev.dev_detect_time  = recv_time;
+		for (i = 0; i < info->rmdev.chip_max; i ++) {
+			if (info->rmdev.chip[i].error_cnt >= 12) {
+				if (info->rmdev.chip[i].freq > info->rmdev.min_frq)
+					info->rmdev.chip[i].freq--;
+			} else if (info->rmdev.chip[i].error_cnt <= 1) {
+				if (info->rmdev.chip[i].freq < (info->rmdev.def_frq / 10 - 1))
+					info->rmdev.chip[i].freq++;
 			}
-#ifdef NONCE_TEST// ROCKMINER_PRINT_DEBUG
-						device_nonce_corr_ok[dev_id] = 0;
-						device_nonce_corr_fail[dev_id] = 0;
-						device_nonce_corr_fail_2[dev_id] = 0;
-						device_nonce_conunts[dev_id] = 0;
-#endif
-
+			info->rmdev.chip[i].error_cnt = 0;
 		}
+	}
 
 	correction_times = 0;
-	while(correction_times<NONCE_CORRECTION_TIMES)
-	{
-		if (submit_nonce(thr, work, nonce + correction_value[correction_times]))
-		{
+	while (correction_times < NONCE_CORRECTION_TIMES) {
+		if (submit_nonce(thr, work, nonce + correction_value[correction_times])) {
 			hash_count++;
 			info->failing = false;
-#if ROCKMINER_PRINT_DEBUG
-			applog(LOG_WARNING, "Rockminer nonce :::OK:::\r\n");
-#endif
+			applog(LOG_DEBUG, "Rockminer nonce :::OK:::");
 			break;
-		}
-		else
-		{
-#if ROCKMINER_PRINT_DEBUG
-			applog(LOG_WARNING, "Rockminer nonce error times = %d\r\n", correction_times);
-#endif
-			if(nonce == 0)
-			{
+		} else {
+			applog(LOG_DEBUG, "Rockminer nonce error times = %d", correction_times);
+			if (nonce == 0)
 				break;
-			}
 		}
 		correction_times++;
 	}
 
-	if(correction_times > 0 )
-	{
-
-	/* FIXME: Where is this i supposed to come from? Comment out instead */
-	//rmdev[dev_id].chip[i].error_cnt ++;
-	    if (correction_times < NONCE_CORRECTION_TIMES)
-	    	{
-
-#ifdef NONCE_TEST// ROCKMINER_PRINT_DEBUG
-	    		device_nonce_corr_ok[dev_id] ++;
-		//applog(LOG_WARNING, "Rockminer correction the nonce success %s%d[%d]=%d",icarus->drv->name, icarus->dev_id,chip_no,correction_times);
-#endif
-	    	}
-		else
-			{
-#ifdef NONCE_TEST// ROCKMINER_PRINT_DEBUG
-		device_nonce_corr_fail[dev_id] ++;
-#endif
-		//applog(LOG_WARNING, "Rockminer correction the nonce fail ---------%s%d[%d]=%d",icarus->drv->name, icarus->dev_id,chip_no,correction_times);
-			}
-
-	}
-
 	hash_count = (hash_count * info->nonce_mask);
 
 	if (opt_debug || info->do_icarus_timing)