Commit 69d67af10445ee64bc851eccb00f079956389b0e

Con Kolivas 2014-02-12T20:46:44

Add driver for cointerra devices.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
diff --git a/ASIC-README b/ASIC-README
index df5f178..e41a983 100644
--- a/ASIC-README
+++ b/ASIC-README
@@ -1,10 +1,17 @@
 SUPPORTED DEVICES
 
-Currently supported devices include the Avalon (including BitBurner and
-Klondike), the Butterfly Labs SC range of devices, the ASICMINER block
-erupters, the BF1 (bitfury) USB (red and blue) devices, KnCminer Mercury,
-Saturn and Jupiter devices, BlackArrow Bitfury devices, Bi*fury USB devices,
-Nanofury USB devices, Hashfast devices and Antminer U1 USB devices.
+Currently supported devices include:
+- Avalon (including BitBurner and Klondike)
+- Butterfly Labs SC range
+- ASICMINER block erupters
+- BF1 (bitfury) USB (red and blue)
+- KnCminer Mercury, Saturn and Jupiter
+- BlackArrow Bitfury
+- Bi*fury USB
+- Nanofury USB
+- Hashfast Babyjet and Sierra
+- Antminer U1 USB
+- Cointerra
 
 No COM ports on windows or TTY devices will be used by cgminer as it
 communicates directly with them via USB so it is normal for them to not exist or
@@ -54,6 +61,16 @@ Nanofury devices come up as NF1.
 Bitfury USB devices are also set up as per the USB ASICs below.
 
 
+COINTERRA devices
+
+Cointerra devices need the --enable-cointerra option when compiling cgminer.
+
+Cointerra devices come up as CTA devices and currently take no command line
+arguments.
+
+Cointerra USB devices are set up as per the USB ASIC instructions below.
+
+
 HASHFAST devices
 
 Hashfast devices need the --enable-hashfast option when compiling cgminer.
diff --git a/Makefile.am b/Makefile.am
index 3a20646..252ca9f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -94,6 +94,10 @@ if HAS_KLONDIKE
 cgminer_SOURCES += driver-klondike.c
 endif
 
+if HAS_COINTERRA
+cgminer_SOURCES += driver-cointerra.c driver-cointerra.h
+endif
+
 if HAS_BAB
 cgminer_SOURCES += driver-bab.c
 endif
diff --git a/README b/README
index 6063a79..f793c63 100644
--- a/README
+++ b/README
@@ -103,17 +103,28 @@ If building on Red Hat:
 
 CGMiner specific configuration options:
   --enable-avalon         Compile support for Avalon (default disabled)
+  --enable-bab            Compile support for BlackArrow Bitfury (default
+                          disabled)
   --enable-bflsc          Compile support for BFL ASICs (default disabled)
   --enable-bitforce       Compile support for BitForce FPGAs (default
                           disabled)
   --enable-bitfury        Compile support for BitFury ASICs (default disabled)
+  --enable-cointerra      Compile support for Cointerra ASICs (default
+                          disabled)
+  --enable-drillbit       Compile support for Drillbit BitFury ASICs (default
+                          disabled)
   --enable-hashfast       Compile support for Hashfast (default disabled)
   --enable-icarus         Compile support for Icarus (default disabled)
   --enable-knc            Compile support for KnC miners (default disabled)
   --enable-bab            Compile support for BlackArrow Bitfury (default disabled)
   --enable-minion         Compile support for BlackArrow Minion ASIC (default disabled)
   --enable-klondike       Compile support for Klondike (default disabled)
+  --enable-knc            Compile support for KnC miners (default disabled)
+  --enable-minion         Compile support for Minion BlackArrow ASIC (default
+                          disabled)
   --enable-modminer       Compile support for ModMiner FPGAs(default disabled)
+  --disable-libcurl       Disable building with libcurl for getwork and GBT
+                          support
   --without-curses        Compile support for curses TUI (default enabled)
   --with-system-libusb    Compile against dynamic system libusb (default use
                           included static libusb)
@@ -260,7 +271,8 @@ See FGPA-README for more information regarding this.
 Cgminer should automatically find all of your Avalon ASIC, BFL ASIC, BitForce
 FPGAs, Icarus bitstream FPGAs, Klondike ASIC, ASICMINER usb block erupters,
 KnC ASICs, BaB ASICs, Hashfast ASICs, ModMiner FPGAs, BPMC/BGMC BF1 USB ASICs,
-Bi*fury USB ASICs, Nanofury USB ASICs and Antminer U1 USB ASICs.
+Bi*fury USB ASICs, Nanofury USB ASICs, Antminer U1 USB ASICs and Cointerra
+devices.
 
 ---
 
@@ -301,7 +313,7 @@ If your distribution does not have the plugdev group you can create it with:
 
  sudo groupadd plugdev
 
-In order for the BFL devices to instantly be owned by the plugdev group and
+In order for the USB devices to instantly be owned by the plugdev group and
 accessible by anyone from the plugdev group you can copy the file
 "01-cgminer.rules" from the cgminer archive into the /etc/udev/rules.d
 directory with the following command:
diff --git a/api.c b/api.c
index f59790e..692d0b3 100644
--- a/api.c
+++ b/api.c
@@ -26,7 +26,7 @@
 #include "util.h"
 #include "klist.h"
 
-#if defined(USE_BFLSC) || defined(USE_AVALON) || defined(USE_HASHFAST) || defined(USE_BITFURY) || defined(USE_KLONDIKE) || defined(USE_KNC) || defined(USE_BAB) || defined(USE_DRILLBIT) || defined(USE_MINION)
+#if defined(USE_BFLSC) || defined(USE_AVALON) || defined(USE_HASHFAST) || defined(USE_BITFURY) || defined(USE_KLONDIKE) || defined(USE_KNC) || defined(USE_BAB) || defined(USE_DRILLBIT) || defined(USE_MINION) || defined(USE_COINTERRA)
 #define HAVE_AN_ASIC 1
 #endif
 
@@ -183,6 +183,9 @@ static const char *DEVICECODE = ""
 #ifdef USE_MODMINER
 			"MMQ "
 #endif
+#ifdef USE_COINTERRA
+			"CTA "
+#endif
 			"";
 
 static const char *OSINFO =
diff --git a/cgminer.c b/cgminer.c
index 9073284..0d4037b 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -76,6 +76,10 @@ char *curly = ":D";
 #include "driver-bitfury.h"
 #endif
 
+#ifdef USE_COINTERRA
+#include "driver-cointerra.h"
+#endif
+
 #ifdef USE_HASHFAST
 #include "driver-hashfast.h"
 #endif
@@ -650,6 +654,13 @@ static char *set_int_0_to_100(const char *arg, int *i)
 }
 #endif
 
+#ifdef USE_COINTERRA
+static char *set_int_0_to_255(const char *arg, int *i)
+{
+        return set_int_range(arg, i, 0, 255);
+}
+#endif
+
 #if defined(USE_BFLSC) || defined(USE_BITFURY) || defined(USE_HASHFAST)
 static char *set_int_0_to_200(const char *arg, int *i)
 {
@@ -1211,6 +1222,11 @@ static struct opt_table opt_config_table[] = {
 			opt_set_bool, &opt_compact,
 			"Use compact display without per device statistics"),
 #endif
+#ifdef USE_COINTERRA
+	OPT_WITH_ARG("--cta-load",
+		set_int_0_to_255, NULL, &opt_cta_load,
+		opt_hidden),
+#endif
 	OPT_WITHOUT_ARG("--debug|-D",
 		     enable_debug, &opt_debug,
 		     "Enable debug output"),
@@ -1572,6 +1588,9 @@ static char *opt_verusage_and_exit(const char *extra)
 #ifdef USE_BITFURY
 		"bitfury "
 #endif
+#ifdef USE_COINTERRA
+		"cointerra "
+#endif
 #ifdef USE_DRILLBIT
                 "drillbit "
 #endif
@@ -3714,7 +3733,7 @@ static bool stale_work(struct work *work, bool share)
 	return false;
 }
 
-static uint64_t share_diff(const struct work *work)
+uint64_t share_diff(const struct work *work)
 {
 	bool new_best = false;
 	double d64, s64;
diff --git a/configure.ac b/configure.ac
index 00157ec..12a900c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -181,6 +181,17 @@ if test "x$bitfury" = xyes; then
 fi
 AM_CONDITIONAL([HAS_BITFURY], [test x$bitfury = xyes])
 
+cointerra="no"
+
+AC_ARG_ENABLE([cointerra],
+	[AC_HELP_STRING([--enable-cointerra],[Compile support for Cointerra ASICs (default disabled)])],
+	[cointerra=$enableval]
+	)
+if test "x$cointerra" = xyes; then
+	AC_DEFINE([USE_COINTERRA], [1], [Defined to 1 if Cointerra support is wanted])
+fi
+AM_CONDITIONAL([HAS_COINTERRA], [test x$cointerra = xyes])
+
 drillbit="no"
 
 AC_ARG_ENABLE([drillbit],
@@ -282,7 +293,7 @@ else
 	])
 fi
 
-if test x$avalon$bitforce$bitfury$modminer$bflsc$icarus$hashfast$klondike$drillbit != xnonononononononono; then
+if test x$avalon$bitforce$bitfury$modminer$bflsc$icarus$hashfast$klondike$drillbit$cointerra != xnononononononononono; then
 	want_usbutils=true
 else
 	want_usbutils=false
@@ -498,6 +509,12 @@ else
 	echo "  BitFury.ASICs........: Disabled"
 fi
 
+if test "x$cointerra" = xyes; then
+	echo "  Cointerra.ASICs......: Enabled"
+else
+	echo "  Cointerra.ASICs......: Disabled"
+fi
+
 if test "x$drillbit" = xyes; then
 	echo "  Drillbit.BitFury.....: Enabled"
 else
@@ -534,7 +551,7 @@ else
 	echo "  ModMiner.FPGAs.......: Disabled"
 fi
 
-if test "x$avalon$bab$bflsc$bitforce$bitfury$hashfast$icarus$klondike$knc$modminer$drillbit$minion" = xnononononononononononono; then
+if test "x$avalon$bab$bflsc$bitforce$bitfury$hashfast$icarus$klondike$knc$modminer$drillbit$minion$cointerra" = xnonononononononononononono; then
 	AC_MSG_ERROR([No mining configured in])
 fi
 
diff --git a/driver-cointerra.c b/driver-cointerra.c
new file mode 100644
index 0000000..27993d8
--- /dev/null
+++ b/driver-cointerra.c
@@ -0,0 +1,1110 @@
+/*
+ * Copyright 2013 Con Kolivas <kernel@kolivas.org>
+ *
+ * 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-cointerra.h"
+
+static const char *cointerra_hdr = "ZZ";
+
+static void cta_gen_message(char *msg, char type)
+{
+	memset(msg, 0, CTA_MSG_SIZE);
+	memcpy(msg, cointerra_hdr, 2);
+	msg[CTA_MSG_TYPE] = type;
+}
+
+/* Find the number of leading zero bits in diff */
+static uint8_t diff_to_bits(double diff)
+{
+	uint64_t diff64;
+	uint8_t i;
+
+	diff /= 0.9999847412109375;
+	diff *= (double)2147483648.0;
+	if (diff > 0x8000000000000000ULL)
+		diff = 0x8000000000000000ULL;
+	/* Convert it to an integer */
+	diff64 = diff;
+	for (i = 0; diff64; i++, diff64 >>= 1);
+
+	return i;
+}
+
+static bool cta_reset_init(char *buf)
+{
+	return ((buf[CTA_MSG_TYPE] == CTA_RECV_RDONE) && ((buf[CTA_RESET_TYPE]&0x3) == CTA_RESET_INIT));
+}
+
+static bool cta_open(struct cgpu_info *cointerra)
+{
+	int err, amount, offset = 0;
+	char buf[CTA_MSG_SIZE];
+	cgtimer_t ts_start;
+	bool ret = false;
+
+	if (cointerra->usbinfo.nodev)
+		return false;
+
+	applog(LOG_INFO, "CTA_OPEN");
+
+	cta_gen_message(buf, CTA_SEND_RESET);
+	// set the initial difficulty
+	buf[CTA_RESET_TYPE] = CTA_RESET_INIT | CTA_RESET_DIFF;
+	buf[CTA_RESET_DIFF] = diff_to_bits(CTA_INIT_DIFF);
+	buf[CTA_RESET_LOAD] = opt_cta_load ? opt_cta_load : 255;
+
+	if (cointerra->usbinfo.nodev)
+		return ret;
+
+	err = usb_write(cointerra, buf, CTA_MSG_SIZE, &amount, C_CTA_WRITE);
+	if (err) {
+		applog(LOG_INFO, "Write error %d, wrote %d of %d", err, amount, CTA_MSG_SIZE);
+		return ret;
+	}
+
+	cgtimer_time(&ts_start);
+
+	/* Read from the device for up to 2 seconds discarding any data that
+	 * doesn't match a reset complete acknowledgement. */
+	while (42) {
+		cgtimer_t ts_now, ts_diff;
+		char *msg;
+
+		cgtimer_time(&ts_now);
+		cgtimer_sub(&ts_now, &ts_start, &ts_diff);
+		if (cgtimer_to_ms(&ts_diff) > 2000) {
+			applog(LOG_DEBUG, "%s %d: Timed out waiting for response to reset init",
+			       cointerra->drv->name, cointerra->device_id);
+			break;
+		}
+
+		if (cointerra->usbinfo.nodev)
+			break;
+
+		err = usb_read(cointerra, buf + offset, CTA_MSG_SIZE - offset, &amount, C_CTA_READ);
+		if (err && err != LIBUSB_ERROR_TIMEOUT) {
+			applog(LOG_INFO, "%s %d: Read error %d, read %d", cointerra->drv->name,
+			       cointerra->device_id, err, amount);
+			break;
+		}
+		if (!amount)
+			continue;
+
+		msg = strstr(buf, cointerra_hdr);
+		if (!msg) {
+			/* Keep the last byte in case it's the first byte of
+			 * the 2 byte header. */
+			offset = 1;
+			memmove(buf, buf + amount - 1, offset);
+			continue;
+		}
+
+		if (msg > buf) {
+			/* length of message = offset for next usb_read after moving */
+			offset = CTA_MSG_SIZE - (msg - buf);
+			memmove(buf, msg, offset);
+			continue;
+		}
+
+		/* We have a full sized message starting with the header now */
+		if (cta_reset_init(buf)) {
+			/* We can't store any other data returned with this
+			 * reset since we have not allocated any memory for
+			 * a cointerra_info structure yet. */
+			applog(LOG_INFO, "%s %d: Successful reset init received",
+			       cointerra->drv->name, cointerra->device_id);
+			ret = true;
+			break;
+		}
+	}
+
+	return ret;
+}
+
+static void cta_clear_work(struct cgpu_info *cgpu)
+{
+	struct work *work, *tmp;
+
+	wr_lock(&cgpu->qlock);
+	HASH_ITER(hh, cgpu->queued_work, work, tmp) {
+		__work_completed(cgpu, work);
+		free_work(work);
+	}
+	wr_unlock(&cgpu->qlock);
+}
+
+static void cta_close(struct cgpu_info *cointerra)
+{
+	struct cointerra_info *info = cointerra->device_data;
+
+	/* Wait for read thread to die */
+	pthread_join(info->read_thr, NULL);
+
+	/* Open does the same reset init followed by response as is required to
+	 * close the device. */
+	if (!cta_open(cointerra)) {
+		applog(LOG_INFO, "%s %d: Reset on close failed", cointerra->drv->name,
+			cointerra->device_id);
+	}
+
+	mutex_destroy(&info->lock);
+	mutex_destroy(&info->sendlock);
+	/* Don't free info here to avoid trying to access dereferenced members
+	 * once a device is unplugged. */
+	cta_clear_work(cointerra);
+}
+
+static struct cgpu_info *cta_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
+{
+	struct cgpu_info *cointerra = usb_alloc_cgpu(&cointerra_drv, 1);
+	int tries = 0;
+
+	if (!usb_init(cointerra, dev, found))
+		goto fail;
+	applog(LOG_INFO, "%s %d: Found at %s", cointerra->drv->name,
+	       cointerra->device_id, cointerra->device_path);
+
+	while (!cta_open(cointerra) && !cointerra->usbinfo.nodev) {
+		if (tries++ > 3)
+			goto failed_open;
+		applog(LOG_INFO, "%s %d: Failed to open %d times, retrying", cointerra->drv->name,
+		       cointerra->device_id, tries);
+	}
+
+	if (!add_cgpu(cointerra))
+		goto fail_close;
+
+	update_usb_stats(cointerra);
+	applog(LOG_INFO, "%s %d: Successfully set up %s", cointerra->drv->name,
+	       cointerra->device_id, cointerra->device_path);
+	return cointerra;
+
+fail_close:
+	cta_close(cointerra);
+failed_open:
+	applog(LOG_INFO, "%s %d: Failed to initialise %s", cointerra->drv->name,
+	       cointerra->device_id, cointerra->device_path);
+fail:
+	usb_free_cgpu(cointerra);
+	return NULL;
+}
+
+static void cta_detect(bool __maybe_unused hotplug)
+{
+	usb_detect(&cointerra_drv, cta_detect_one);
+}
+
+/* This function will remove a work item from the hashtable if it matches the
+ * id in work->subid and return a pointer to the work but it will not free the
+ * work. It may return NULL if it cannot find matching work. */
+static struct work *take_work_by_id(struct cgpu_info *cgpu, uint16_t id)
+{
+	struct work *work, *tmp, *ret = NULL;
+
+	wr_lock(&cgpu->qlock);
+	HASH_ITER(hh, cgpu->queued_work, work, tmp) {
+		if (work->subid == id) {
+			ret = work;
+			break;
+		}
+	}
+	if (ret)
+		__work_completed(cgpu, ret);
+	wr_unlock(&cgpu->qlock);
+
+	return ret;
+}
+
+/* This function will look up a work item in the hashtable if it matches the
+ * id in work->subid and return a cloned work item if it matches. It may return
+ * NULL if it cannot find matching work. */
+static struct work *clone_work_by_id(struct cgpu_info *cgpu, uint16_t id)
+{
+	struct work *work, *tmp, *ret = NULL;
+
+	rd_lock(&cgpu->qlock);
+	HASH_ITER(hh, cgpu->queued_work, work, tmp) {
+		if (work->subid == id) {
+			ret = work;
+			break;
+		}
+	}
+	if (ret)
+		ret = copy_work(ret);
+	rd_unlock(&cgpu->qlock);
+
+	return ret;
+}
+
+static bool cta_send_msg(struct cgpu_info *cointerra, char *buf);
+
+static uint16_t hu16_from_msg(char *buf, int msg)
+{
+	return le16toh(*(uint16_t *)&buf[msg]);
+}
+
+static uint32_t hu32_from_msg(char *buf, int msg)
+{
+	return le32toh(*(uint32_t *)&buf[msg]);
+}
+
+static uint64_t hu64_from_msg(char *buf, int msg)
+{
+	return le64toh(*(uint64_t *)&buf[msg]);
+}
+
+static uint8_t u8_from_msg(char *buf, int msg)
+{
+	return *(uint8_t *)&buf[msg];
+}
+
+static void msg_from_hu16(char *buf, int msg, uint16_t val)
+{
+	*(uint16_t *)&buf[msg] = htole16(val);
+}
+
+static void cta_parse_reqwork(struct cgpu_info *cointerra, struct cointerra_info *info,
+			      char *buf)
+{
+	uint16_t retwork;
+
+	retwork = hu16_from_msg(buf, CTA_REQWORK_REQUESTS);
+	applog(LOG_DEBUG, "%s %d: Request work message for %u items received",
+	       cointerra->drv->name, cointerra->device_id, retwork);
+
+	mutex_lock(&info->lock);
+	info->requested = retwork;
+	/* Wake up the main scanwork loop since we need more
+		* work. */
+	pthread_cond_signal(&info->wake_cond);
+	mutex_unlock(&info->lock);
+}
+
+static void cta_parse_recvmatch(struct thr_info *thr, struct cgpu_info *cointerra,
+				struct cointerra_info *info, char *buf)
+{
+	uint32_t timestamp_offset, mcu_tag;
+	uint16_t retwork;
+	struct work *work;
+
+	/* No endian switch needs doing here since it's sent and returned as
+	 * the same 4 bytes */
+	retwork = *(uint16_t *)(&buf[CTA_DRIVER_TAG]);
+	mcu_tag = hu32_from_msg(buf, CTA_MCU_TAG);
+	applog(LOG_DEBUG, "%s %d: Match message for id 0x%04x MCU id 0x%08x received",
+	       cointerra->drv->name, cointerra->device_id, retwork, mcu_tag);
+
+	work = clone_work_by_id(cointerra, retwork);
+	if (likely(work)) {
+		uint32_t wdiff = hu32_from_msg(buf, CTA_WORK_DIFFBITS);
+		uint32_t nonce = hu32_from_msg(buf, CTA_MATCH_NONCE);
+		unsigned char rhash[32];
+		char outhash[16];
+		bool ret;
+
+		timestamp_offset = hu32_from_msg(buf, CTA_MATCH_NOFFSET);
+		if (timestamp_offset) {
+			struct work *base_work = work;
+
+			work = copy_work_noffset(base_work, timestamp_offset);
+			free_work(base_work);
+		}
+
+		/* Test against the difficulty we asked for along with the work */
+		ret = test_nonce_diff(work, nonce, wdiff);
+
+		if (opt_debug) {
+			/* Debugging, remove me */
+			swab256(rhash, work->hash);
+			__bin2hex(outhash, rhash, 8);
+			applog(LOG_DEBUG, "submit work %s 0x%04x 0x%08x %d 0x%08x",
+			       outhash, retwork, mcu_tag, timestamp_offset, nonce);
+		}
+
+		if (likely(ret)) {
+			uint8_t asic, core, pipe;
+			int pipeno, bitchar, bitbit;
+
+			asic = u8_from_msg(buf, CTA_MCU_ASIC);
+			core = u8_from_msg(buf, CTA_MCU_CORE);
+			pipe = u8_from_msg(buf, CTA_MCU_PIPE);
+			pipeno = asic * 512 + core * 128 + pipe;
+			if (unlikely(asic > 1 || core > 3 || pipe > 127 || pipeno > 1023)) {
+				applog(LOG_WARNING, "%s %d: MCU invalid pipe asic %d core %d pipe %d",
+				       cointerra->drv->name, cointerra->device_id, asic, core, pipe);
+			} else {
+				info->last_pipe_nonce[pipeno] = time(NULL);
+				bitchar = pipeno / 8;
+				bitbit = pipeno % 8;
+				info->pipe_bitmap[bitchar] |= 0x80 >> bitbit;
+			}
+
+			applog(LOG_DEBUG, "%s %d: Submitting tested work job_id %s work_id %u",
+			       cointerra->drv->name, cointerra->device_id, work->job_id, work->subid);
+			ret = submit_tested_work(thr, work);
+
+			mutex_lock(&info->lock);
+			if (ret)
+				info->share_hashes += (uint64_t)work->work_difficulty * 0x100000000ull;
+			info->hashes += nonce;
+			mutex_unlock(&info->lock);
+		} else {
+			char sendbuf[CTA_MSG_SIZE];
+
+			applog(LOG_DEBUG, "%s %d: Notify bad match work",
+			       cointerra->drv->name, cointerra->device_id);
+			if (opt_debug) {
+				uint64_t sdiff = share_diff(work);
+				unsigned char midstate[32], wdata[12];
+				char hexmidstate[68], hexwdata[28];
+				uint16_t wid;
+
+				memcpy(&wid, &info->work_id, 2);
+				flip32(midstate, work->midstate);
+				__bin2hex(hexmidstate, midstate, 32);
+				flip12(wdata, &work->data[64]);
+				__bin2hex(hexwdata, wdata, 12);
+				applog(LOG_DEBUG, "False match sent: work id %u midstate %s  blkhdr %s",
+				       wid, hexmidstate, hexwdata);
+				applog(LOG_DEBUG, "False match reports: work id 0x%04x MCU id 0x%08x work diff %u",
+				       retwork, mcu_tag, wdiff);
+				applog(LOG_DEBUG, "False match tested: nonce 0x%08x noffset %d %s",
+				       nonce, timestamp_offset, outhash);
+				applog(LOG_DEBUG, "False match devdiff set to %.1f share diff calc %"PRIu64,
+				       work->device_diff, sdiff);
+			}
+
+			/* Tell the device we got a false match */
+			cta_gen_message(sendbuf, CTA_SEND_FMATCH);
+			memcpy(sendbuf + 3, buf, CTA_MSG_SIZE - 3);
+			cta_send_msg(cointerra, sendbuf);
+		}
+		free_work(work);
+	} else {
+		applog(LOG_INFO, "%s %d: Matching work id 0x%X %d not found", cointerra->drv->name,
+		       cointerra->device_id, retwork, __LINE__);
+		inc_hw_errors(thr);
+
+		mutex_lock(&info->lock);
+		info->no_matching_work++;
+		mutex_unlock(&info->lock);
+	}
+}
+
+static void cta_parse_wdone(struct thr_info *thr, struct cgpu_info *cointerra,
+			    struct cointerra_info *info, char *buf)
+{
+	uint16_t retwork = *(uint16_t *)(&buf[CTA_DRIVER_TAG]);
+	struct work *work = take_work_by_id(cointerra, retwork);
+	uint64_t hashes;
+
+	if (likely(work))
+		free_work(work);
+	else {
+		applog(LOG_INFO, "%s %d: Done work not found id 0x%X %d",
+		       cointerra->drv->name, cointerra->device_id, retwork, __LINE__);
+		inc_hw_errors(thr);
+	}
+
+	/* Removing hashes from work done message */
+	hashes = hu64_from_msg(buf, CTA_WDONE_NONCES);
+	if (unlikely(hashes > (61 * 0x100000000ull))) {
+		applog(LOG_INFO, "%s Invalid hash returned %"PRIu64"x %"PRIu64"x %"PRIu64"X",
+		       __func__, info->hashes, hashes, hashes);
+		hashes = 0;
+	}
+
+	mutex_lock(&info->lock);
+	info->hashes += hashes;
+	mutex_unlock(&info->lock);
+}
+
+static void u16array_from_msg(uint16_t *u16, int entries, int var, char *buf)
+{
+	int i, j;
+
+	for (i = 0, j = 0; i < entries; i++, j += sizeof(uint16_t))
+		u16[i] = hu16_from_msg(buf, var + j);
+}
+
+static void cta_parse_statread(struct cointerra_info *info, char *buf)
+{
+	mutex_lock(&info->lock);
+	u16array_from_msg(info->coretemp, CTA_CORES, CTA_STAT_CORETEMPS, buf);
+	info->ambtemp_low = hu16_from_msg(buf, CTA_STAT_AMBTEMP_LOW);
+	info->ambtemp_avg = hu16_from_msg(buf, CTA_STAT_AMBTEMP_AVG);
+	info->ambtemp_high = hu16_from_msg(buf, CTA_STAT_AMBTEMP_HIGH);
+	u16array_from_msg(info->pump_tachs, CTA_PUMPS, CTA_STAT_PUMP_TACHS, buf);
+	u16array_from_msg(info->fan_tachs, CTA_FANS, CTA_STAT_FAN_TACHS, buf);
+	u16array_from_msg(info->corevolts, CTA_CORES, CTA_STAT_CORE_VOLTS, buf);
+	info->volts33 = hu16_from_msg(buf, CTA_STAT_VOLTS33);
+	info->volts12 = hu16_from_msg(buf, CTA_STAT_VOLTS12);
+	info->inactive = hu16_from_msg(buf, CTA_STAT_INACTIVE);
+	info->active = hu16_from_msg(buf, CTA_STAT_ACTIVE);
+	mutex_unlock(&info->lock);
+}
+
+static void u8array_from_msg(uint8_t *u8, int entries, int var, char *buf)
+{
+	int i;
+
+	for (i = 0; i < entries; i++)
+		u8[i] = u8_from_msg(buf, var + i);
+}
+
+static void cta_parse_statset(struct cointerra_info *info, char *buf)
+{
+	mutex_lock(&info->lock);
+	u8array_from_msg(info->coreperf, CTA_CORES, CTA_STAT_PERFMODE, buf);
+	u8array_from_msg(info->fanspeed, CTA_FANS, CTA_STAT_FANSPEEDS, buf);
+	info->dies_active = u8_from_msg(buf, CTA_STAT_DIES_ACTIVE);
+	u8array_from_msg(info->pipes_enabled, CTA_CORES, CTA_STAT_PIPES_ENABLED, buf);
+	u16array_from_msg(info->corefreqs, CTA_CORES, CTA_STAT_CORE_FREQS, buf);
+	info->uptime = hu32_from_msg(buf,CTA_STAT_UPTIME);
+	mutex_unlock(&info->lock);
+}
+
+static void cta_parse_info(struct cointerra_info *info, char *buf)
+{
+	mutex_lock(&info->lock);
+	info->hwrev = hu64_from_msg(buf, CTA_INFO_HWREV);
+	info->serial = hu32_from_msg(buf, CTA_INFO_SERNO);
+	info->asics = u8_from_msg(buf, CTA_INFO_NUMASICS);
+	info->dies = u8_from_msg(buf, CTA_INFO_NUMDIES);
+	info->cores = hu16_from_msg(buf, CTA_INFO_NUMCORES);
+	info->board_number = u8_from_msg(buf, CTA_INFO_BOARDNUMBER);
+	info->fwrev[0] = u8_from_msg(buf, CTA_INFO_FWREV_MAJ);
+	info->fwrev[1] = u8_from_msg(buf, CTA_INFO_FWREV_MIN);
+	info->fwrev[2] = u8_from_msg(buf, CTA_INFO_FWREV_MIC);
+	info->fw_year = hu16_from_msg(buf, CTA_INFO_FWDATE_YEAR);
+	info->fw_month = u8_from_msg(buf, CTA_INFO_FWDATE_MONTH);
+	info->fw_day = u8_from_msg(buf, CTA_INFO_FWDATE_DAY);
+	info->init_diffbits = u8_from_msg(buf, CTA_INFO_INITDIFFBITS);
+	info->min_diffbits = u8_from_msg(buf, CTA_INFO_MINDIFFBITS);
+	info->max_diffbits = u8_from_msg(buf, CTA_INFO_MAXDIFFBITS);
+	mutex_unlock(&info->lock);
+}
+
+static void cta_parse_rdone(struct cgpu_info *cointerra, struct cointerra_info *info,
+			    char *buf)
+{
+	uint8_t reset_type, diffbits;
+	uint64_t wdone;
+
+	reset_type = buf[CTA_RESET_TYPE];
+	diffbits = buf[CTA_RESET_DIFF];
+	wdone = hu64_from_msg(buf, CTA_WDONE_NONCES);
+
+	applog(LOG_INFO, "%s %d: Reset done type %u message %u diffbits %"PRIu64" done received",
+	       cointerra->drv->name, cointerra->device_id, reset_type, diffbits, wdone);
+	if (wdone) {
+		applog(LOG_INFO, "%s %d: Reset done type %u message %u diffbits %"PRIu64" done received",
+			cointerra->drv->name, cointerra->device_id, reset_type, diffbits, wdone);
+
+		mutex_lock(&info->lock);
+		info->hashes += wdone;
+		mutex_unlock(&info->lock);
+	}
+
+	/* Note that the cgsem that is posted here must not be waited on while
+	 * holding the info->lock to not get into a livelock since this
+	 * function also grabs the lock first and it's always best to not sleep
+	 * while holding a lock. */
+	if (reset_type == CTA_RESET_NEW) {
+		cta_clear_work(cointerra);
+		/* Tell reset sender that the reset is complete
+			* and it may resume. */
+		cgsem_post(&info->reset_sem);
+	}
+}
+
+static void cta_parse_debug(struct cointerra_info *info, char *buf)
+{
+	mutex_lock(&info->lock);
+
+	info->tot_underruns = hu16_from_msg(buf, CTA_STAT_UNDERRUNS);
+	u16array_from_msg(info->tot_hw_errors, CTA_CORES, CTA_STAT_HW_ERRORS, buf);
+	info->tot_hashes = hu64_from_msg(buf, CTA_STAT_HASHES);
+	info->tot_flushed_hashes = hu64_from_msg(buf, CTA_STAT_FLUSHED_HASHES);
+
+	mutex_unlock(&info->lock);
+}
+
+static void cta_parse_msg(struct thr_info *thr, struct cgpu_info *cointerra,
+			  struct cointerra_info *info, char *buf)
+{
+	switch (buf[CTA_MSG_TYPE]) {
+		default:
+		case CTA_RECV_UNUSED:
+			applog(LOG_INFO, "%s %d: Unidentified message type %u",
+			       cointerra->drv->name, cointerra->device_id, buf[CTA_MSG_TYPE]);
+			break;
+		case CTA_RECV_REQWORK:
+			cta_parse_reqwork(cointerra, info, buf);
+			break;
+		case CTA_RECV_MATCH:
+			cta_parse_recvmatch(thr, cointerra, info, buf);
+			break;
+		case CTA_RECV_WDONE:
+			applog(LOG_DEBUG, "%s %d: Work done message received",
+			       cointerra->drv->name, cointerra->device_id);
+			cta_parse_wdone(thr, cointerra, info, buf);
+			break;
+		case CTA_RECV_STATREAD:
+			applog(LOG_DEBUG, "%s %d: Status readings message received",
+			       cointerra->drv->name, cointerra->device_id);
+			cta_parse_statread(info, buf);
+			break;
+		case CTA_RECV_STATSET:
+			applog(LOG_DEBUG, "%s %d: Status settings message received",
+			       cointerra->drv->name, cointerra->device_id);
+			cta_parse_statset(info, buf);
+			break;
+		case CTA_RECV_INFO:
+			applog(LOG_DEBUG, "%s %d: Info message received",
+			       cointerra->drv->name, cointerra->device_id);
+			cta_parse_info(info, buf);
+			break;
+		case CTA_RECV_MSG:
+			applog(LOG_NOTICE, "%s %d: MSG: %s",
+			       cointerra->drv->name, cointerra->device_id, &buf[CTA_MSG_RECVD]);
+			break;
+		case CTA_RECV_RDONE:
+			cta_parse_rdone(cointerra, info, buf);
+			break;
+		case CTA_RECV_STATDEBUG:
+			cta_parse_debug(info, buf);
+			break;
+	}
+}
+
+static void *cta_recv_thread(void *arg)
+{
+	struct thr_info *thr = (struct thr_info *)arg;
+	struct cgpu_info *cointerra = thr->cgpu;
+	struct cointerra_info *info = cointerra->device_data;
+	char threadname[24];
+	int offset = 0;
+
+	snprintf(threadname, 24, "cta_recv/%d", cointerra->device_id);
+	RenameThread(threadname);
+
+	while (likely(!cointerra->shutdown)) {
+		char buf[CTA_READBUF_SIZE];
+		int amount, err;
+
+		if (unlikely(cointerra->usbinfo.nodev)) {
+			applog(LOG_DEBUG, "%s %d: Device disappeared, disabling recv thread",
+			       cointerra->drv->name, cointerra->device_id);
+			break;
+		}
+
+		err = usb_read(cointerra, buf + offset, CTA_MSG_SIZE, &amount, C_CTA_READ);
+		if (err && err != LIBUSB_ERROR_TIMEOUT) {
+			applog(LOG_ERR, "%s %d: Read error %d, read %d", cointerra->drv->name,
+			       cointerra->device_id, err, amount);
+			break;
+		}
+		offset += amount;
+
+		while (offset >= CTA_MSG_SIZE) {
+			char *msg = strstr(buf, cointerra_hdr);
+			int begin;
+
+			if (unlikely(!msg)) {
+				applog(LOG_WARNING, "%s %d: No message header found, discarding buffer",
+				       cointerra->drv->name, cointerra->device_id);
+				inc_hw_errors(thr);
+				/* Save the last byte in case it's the fist
+				 * byte of a header. */
+				begin = CTA_MSG_SIZE - 1;
+				offset -= begin;
+				memmove(buf, buf + begin, offset);
+				continue;
+			}
+
+			if (unlikely(msg != buf)) {
+				begin = msg - buf;
+				applog(LOG_WARNING, "%s %d: Reads out of sync, discarding %d bytes",
+				       cointerra->drv->name, cointerra->device_id, begin);
+				inc_hw_errors(thr);
+				offset -= begin;
+				memmove(buf, msg, offset);
+				if (offset < CTA_MSG_SIZE)
+					break;
+			}
+
+			/* We have enough buffer for a full message, parse now */
+			cta_parse_msg(thr, cointerra, info, msg);
+			offset -= CTA_MSG_SIZE;
+			if (offset > 0)
+				memmove(buf, buf + CTA_MSG_SIZE, offset);
+		}
+	}
+
+	return NULL;
+}
+
+static bool cta_send_msg(struct cgpu_info *cointerra, char *buf)
+{
+	struct cointerra_info *info = cointerra->device_data;
+	int amount, err;
+
+	if (unlikely(cointerra->usbinfo.nodev))
+		return false;
+
+	/* Serialise usb writes to prevent overlap in case multiple threads
+	 * send messages */
+	mutex_lock(&info->sendlock);
+	err = usb_write(cointerra, buf, CTA_MSG_SIZE, &amount, C_CTA_WRITE);
+	mutex_unlock(&info->sendlock);
+
+	if (unlikely(err || amount != CTA_MSG_SIZE)) {
+		applog(LOG_ERR, "%s %d: Write error %d, wrote %d of %d", cointerra->drv->name,
+		       cointerra->device_id, err, amount, CTA_MSG_SIZE);
+		return false;
+	}
+	return true;
+}
+
+static bool cta_prepare(struct thr_info *thr)
+{
+	struct cgpu_info *cointerra = thr->cgpu;
+	struct cointerra_info *info = calloc(sizeof(struct cointerra_info), 1);
+	char buf[CTA_MSG_SIZE];
+	time_t now_t;
+	int i;
+
+	if (unlikely(cointerra->usbinfo.nodev))
+		return false;
+
+	if (unlikely(!info))
+		quit(1, "Failed to calloc info in cta_detect_one");
+	cointerra->device_data = info;
+	/* Nominally set a requested value when starting, preempting the need
+	 * for a req-work message. */
+	info->requested = CTA_MAX_QUEUE;
+
+	info->thr = thr;
+	mutex_init(&info->lock);
+	mutex_init(&info->sendlock);
+	if (unlikely(pthread_cond_init(&info->wake_cond, NULL)))
+		quit(1, "Failed to create cta pthread cond");
+	cgsem_init(&info->reset_sem);
+	if (pthread_create(&info->read_thr, NULL, cta_recv_thread, (void *)thr))
+		quit(1, "Failed to create cta_recv_thread");
+
+	/* Request a single status setting message */
+	cta_gen_message(buf, CTA_SEND_REQUEST);
+	msg_from_hu16(buf, CTA_REQ_MSGTYPE, CTA_RECV_STATSET);
+	msg_from_hu16(buf, CTA_REQ_INTERVAL, 0);
+	if (!cta_send_msg(cointerra, buf))
+		return false;
+
+	/* Request status debug messages every 60 seconds */
+	cta_gen_message(buf, CTA_SEND_REQUEST);
+	msg_from_hu16(buf, CTA_REQ_MSGTYPE, CTA_RECV_STATDEBUG);
+	msg_from_hu16(buf, CTA_REQ_INTERVAL, 6000);
+	if (!cta_send_msg(cointerra, buf))
+		return false;
+
+	/* Start with a full bitmap of valid nonce flags for every pipe */
+	now_t = time(NULL);
+	for (i = 0; i < 1024; i++)
+		info->last_pipe_nonce[i] = now_t;
+	for (i = 0; i < 128; i++)
+		info->pipe_bitmap[i] = 0x00u;
+
+	return true;
+}
+
+static void cta_send_reset(struct cgpu_info *cointerra, struct cointerra_info *info,
+			   uint8_t reset_type, uint8_t diffbits);
+static void cta_flush_work(struct cgpu_info *cointerra);
+
+/* *_fill and *_scanwork are serialised wrt to each other */
+static bool cta_fill(struct cgpu_info *cointerra)
+{
+	struct cointerra_info *info = cointerra->device_data;
+	bool ret = true;
+	char buf[CTA_MSG_SIZE];
+	struct work *work = NULL;
+	unsigned short nroll_limit;
+	uint32_t swab[8];
+	uint8_t diffbits;
+
+	//applog(LOG_WARNING, "%s %d: cta_fill %d", cointerra->drv->name, cointerra->device_id,__LINE__);
+
+	if (unlikely(info->thr->work_restart))
+		cta_flush_work(cointerra);
+
+	mutex_lock(&info->lock);
+	if (!info->requested)
+		goto out_unlock;
+	work = get_queued(cointerra);
+	if (unlikely(!work)) {
+		ret = false;
+		goto out_unlock;
+	}
+	if (--info->requested > 0)
+		ret = false;
+
+	/* It does not matter what endian this uint16_t is since it will be
+	 * the same value on sending to the MC as returning in match/done. This
+	 * will automatically wrap as a uint16_t. It cannot be zero for the MCU
+	 * though. */
+	if (unlikely(++info->work_id == 0))
+		info->work_id = 1;
+	work->subid = info->work_id;
+
+	diffbits = diff_to_bits(work->device_diff);
+
+	cta_gen_message(buf, CTA_SEND_WORK);
+
+ 	memcpy(buf + CTA_DRIVER_TAG, &info->work_id, 2);
+
+	flip32(swab, work->midstate);
+	memcpy(buf + CTA_WORK_MIDSTATE, swab, 32);
+
+	flip12(swab, &work->data[64]);
+	memcpy(buf + CTA_WORK_DATA, swab, 12);
+
+	nroll_limit = htole16(work->drv_rolllimit);
+	memcpy(buf + CTA_WORK_NROLL, &nroll_limit, 2);
+
+	memcpy(buf + CTA_WORK_DIFFBITS, &diffbits, 1);
+
+out_unlock:
+	mutex_unlock(&info->lock);
+
+	if (work) {
+		cgtime(&work->tv_work_start);
+		applog(LOG_DEBUG, "%s %d: Sending work job_id %s work_id %u", cointerra->drv->name,
+		       cointerra->device_id, work->job_id, work->subid);
+		if (unlikely(!cta_send_msg(cointerra, buf))) {
+			work_completed(cointerra, work);
+			applog(LOG_INFO, "%s %d: Failed to send work",
+			       cointerra->drv->name, cointerra->device_id);
+			/* The device will fail after this */
+		}
+	}
+
+	return ret;
+}
+
+static void cta_send_reset(struct cgpu_info *cointerra, struct cointerra_info *info,
+			   uint8_t reset_type, uint8_t diffbits)
+{
+	char buf[CTA_MSG_SIZE];
+	int ret, retries = 0;
+
+	/* Clear any accumulated messages in case we've gotten out of sync. */
+	cgsem_reset(&info->reset_sem);
+resend:
+	cta_gen_message(buf, CTA_SEND_RESET);
+
+	buf[CTA_RESET_TYPE] = reset_type;
+	buf[CTA_RESET_LOAD] = opt_cta_load ? opt_cta_load : 255;
+
+	applog(LOG_INFO, "%s %d: Sending Reset type %u with diffbits %u", cointerra->drv->name,
+	       cointerra->device_id, reset_type, diffbits);
+	cta_send_msg(cointerra, buf);
+
+	/* Wait for read thread to parse a reset message and signal us we may
+	 * return to submitting other messages. Use a timeout in case we have
+	 * a problem and the reset done message never returns. */
+	if (reset_type == CTA_RESET_NEW) {
+		ret = cgsem_mswait(&info->reset_sem, CTA_RESET_TIMEOUT);
+		if (ret) {
+			if (++retries < 3) {
+				applog(LOG_INFO, "%s %d: Timed out waiting for reset done msg, retrying",
+				       cointerra->drv->name, cointerra->device_id);
+				goto resend;
+			}
+			applog(LOG_WARNING, "%s %d: Timed out waiting for reset done msg",
+			       cointerra->drv->name, cointerra->device_id);
+		}
+		/* Good place to flush any work we have */
+		flush_queue(cointerra);
+	}
+}
+
+static void cta_flush_work(struct cgpu_info *cointerra)
+{
+	struct cointerra_info *info = cointerra->device_data;
+
+	applog(LOG_INFO, "%s %d: cta_flush_work %d", cointerra->drv->name, cointerra->device_id,
+	       __LINE__);
+	cta_send_reset(cointerra, info, CTA_RESET_NEW, 0);
+	/* TODO revisit */
+	info->thr->work_restart = false;
+}
+
+static void cta_update_work(struct cgpu_info *cointerra)
+{
+	struct cointerra_info *info = cointerra->device_data;
+
+	applog(LOG_INFO, "%s %d: Update work", cointerra->drv->name, cointerra->device_id);
+	cta_send_reset(cointerra, info, CTA_RESET_UPDATE, 0);
+}
+
+static int64_t cta_scanwork(struct thr_info *thr)
+{
+	struct cgpu_info *cointerra = thr->cgpu;
+	struct cointerra_info *info = cointerra->device_data;
+	int64_t hashes;
+
+	applog(LOG_DEBUG, "%s %d: cta_scanwork %d", cointerra->drv->name, cointerra->device_id,__LINE__);
+
+	if (unlikely(cointerra->usbinfo.nodev)) {
+		hashes = -1;
+		goto out;
+	}
+
+	if (unlikely(thr->work_restart)) {
+		applog(LOG_INFO, "%s %d: Flush work line %d",
+		     cointerra->drv->name, cointerra->device_id,__LINE__);
+		cta_flush_work(cointerra);
+	} else {
+		struct timespec abstime, tsdiff = {0, 500000000};
+		struct timeval now;
+		time_t now_t;
+		int i;
+
+		cgtime(&now);
+		timeval_to_spec(&abstime, &now);
+		timeraddspec(&abstime, &tsdiff);
+
+		/* Discard work that was started more than 5 minutes ago as
+		 * a safety precaution backup in case the hardware failed to
+		 * return a work done message for some work items. */
+		age_queued_work(cointerra, 300.0);
+
+		/* Use this opportunity to unset the bits in any pipes that
+		 * have not returned a valid nonce for over an hour. */
+		now_t = time(NULL);
+		for (i = 0; i < 1024; i++) {
+			if (unlikely(now_t > info->last_pipe_nonce[i] + 3600)) {
+				int bitchar = i / 8, bitbit = i % 8;
+
+				info->pipe_bitmap[bitchar] &= ~(0x80 >> bitbit);
+			}
+		}
+
+		/* Sleep for up to 0.5 seconds, waking if we need work or
+		 * have received a restart message. */
+		mutex_lock(&info->lock);
+		pthread_cond_timedwait(&info->wake_cond, &info->lock, &abstime);
+		mutex_unlock(&info->lock);
+
+		if (thr->work_restart) {
+			applog(LOG_INFO, "%s %d: Flush work line %d",
+			       cointerra->drv->name, cointerra->device_id,__LINE__);
+			cta_flush_work(cointerra);
+		}
+	}
+
+	mutex_lock(&info->lock);
+	hashes = info->hashes;
+	info->tot_calc_hashes += hashes;
+	info->hashes = 0;
+	mutex_unlock(&info->lock);
+
+	if (unlikely(cointerra->usbinfo.nodev))
+		hashes = -1;
+out:
+	return hashes;
+}
+
+/* This is used for a work restart. We don't actually perform the work restart
+ * here but wake up the scanwork loop if it's waiting on the conditional so
+ * that it can test for the restart message. */
+static void cta_wake(struct cgpu_info *cointerra)
+{
+	struct cointerra_info *info = cointerra->device_data;
+
+	mutex_lock(&info->lock);
+	pthread_cond_signal(&info->wake_cond);
+	mutex_unlock(&info->lock);
+}
+
+static void cta_shutdown(struct thr_info *thr)
+{
+	struct cgpu_info *cointerra = thr->cgpu;
+
+	cta_close(cointerra);
+}
+
+static void cta_zero_stats(struct cgpu_info *cointerra)
+{
+	struct cointerra_info *info = cointerra->device_data;
+
+	info->tot_calc_hashes = 0;
+	info->tot_reset_hashes = info->tot_hashes;
+	info->share_hashes = 0;
+}
+
+static struct api_data *cta_api_stats(struct cgpu_info *cgpu)
+{
+	struct api_data *root = NULL;
+	struct cointerra_info *info = cgpu->device_data;
+	double dev_runtime = cgpu_runtime(cgpu);
+	char bitmaphex[36];
+	uint64_t ghs, val;
+	char buf[64];
+	int i;
+
+	/* Info data */
+	root = api_add_uint16(root, "HW Revision", &info->hwrev, false);
+	root = api_add_uint32(root, "Serial", &info->serial, false);
+	root = api_add_uint8(root, "Asics", &info->asics, false);
+	root = api_add_uint8(root, "Dies", &info->dies, false);
+	root = api_add_uint16(root, "Cores", &info->cores, false);
+	root = api_add_uint8(root, "Board number", &info->board_number, false);
+	sprintf(buf, "%u.%u.%u", info->fwrev[0], info->fwrev[1], info->fwrev[2]);
+	root = api_add_string(root, "FW Revision", buf, true);
+	sprintf(buf, "%04u-%02u-%02u", info->fw_year, info->fw_month, info->fw_day);
+	root = api_add_string(root, "FW Date", buf, true);
+	root = api_add_uint8(root, "Init diffbits", &info->init_diffbits, false);
+	root = api_add_uint8(root, "Min diffbits", &info->min_diffbits, false);
+	root = api_add_uint8(root, "Max diffbits", &info->max_diffbits, false);
+
+	/* Status readings */
+	for (i = 0; i < CTA_CORES; i++) {
+		sprintf(buf, "CoreTemp%d", i);
+		root = api_add_uint16(root, buf, &info->coretemp[i], false);
+	}
+	root = api_add_uint16(root, "Ambient Low", &info->ambtemp_low, false);
+	root = api_add_uint16(root, "Ambient Avg", &info->ambtemp_avg, false);
+	root = api_add_uint16(root, "Ambient High", &info->ambtemp_high, false);
+	for (i = 0; i < CTA_PUMPS; i++) {
+		sprintf(buf, "PumpRPM%d", i);
+		root = api_add_uint16(root, buf, &info->pump_tachs[i], false);
+	}
+	for (i = 0; i < CTA_FANS; i++) {
+		sprintf(buf, "FanRPM%d", i);
+		root = api_add_uint16(root, buf, &info->fan_tachs[i], false);
+	}
+	for (i = 0; i < CTA_CORES; i++) {
+		sprintf(buf, "CoreFreqs%d", i);
+		root = api_add_uint16(root, buf, &info->corefreqs[i], false);
+	}
+
+	for (i = 0; i < CTA_CORES; i++) {
+		sprintf(buf, "CoreVolts%d", i);
+		root = api_add_uint16(root, buf, &info->corevolts[i], false);
+	}
+	root = api_add_uint16(root, "Volts3.3", &info->volts33, false);
+	root = api_add_uint16(root, "Volts12", &info->volts12, false);
+	root = api_add_uint16(root, "Inactive", &info->inactive, false);
+	root = api_add_uint16(root, "Active", &info->active, false);
+
+	/* Status settings */
+	for (i = 0; i < CTA_CORES; i++) {
+		sprintf(buf, "CorePerfMode%d", i);
+		root = api_add_uint8(root, buf, &info->coreperf[i], false);
+	}
+	for (i = 0; i < CTA_FANS; i++) {
+		sprintf(buf, "FanSpeed%d", i);
+		root = api_add_uint8(root, buf, &info->fanspeed[i], false);
+	}
+	root = api_add_uint8(root, "DiesActive", &info->dies_active, false);
+	for (i = 0; i < CTA_CORES; i++) {
+		sprintf(buf, "PipesEnabled%d", i);
+		root = api_add_uint8(root, buf, &info->pipes_enabled[i], false);
+	}
+
+	/* Status debug */
+	root = api_add_int(root, "Underruns", &info->tot_underruns, false);
+	for (i = 0; i < CTA_CORES; i++) {
+		sprintf(buf, "HWErrors%d", i);
+		root = api_add_uint16(root, buf, &info->tot_hw_errors[i], false);
+	}
+	ghs = info->tot_calc_hashes / dev_runtime;
+	root = api_add_uint64(root, "Calc hashrate", &ghs, true);
+	ghs = (info->tot_hashes - info->tot_reset_hashes) / dev_runtime;
+	root = api_add_uint64(root, "Hashrate", &ghs, true);
+	ghs = info->tot_hashes / dev_runtime;
+	root = api_add_uint64(root, "Raw hashrate", &ghs, true);
+	ghs = info->share_hashes / dev_runtime;
+	root = api_add_uint64(root, "Share hashrate", &ghs, true);
+	root = api_add_uint64(root, "Total calc hashes", &info->tot_calc_hashes, false);
+	ghs = info->tot_hashes - info->tot_reset_hashes;
+	root = api_add_uint64(root, "Total hashes", &ghs, true);
+	root = api_add_uint64(root, "Total raw hashes", &info->tot_hashes, false);
+	root = api_add_uint64(root, "Total share hashes", &info->share_hashes, false);
+	root = api_add_uint64(root, "Total flushed hashes", &info->tot_flushed_hashes, false);
+	val = cgpu->diff_accepted * 0x100000000ull;
+	root = api_add_uint64(root, "Accepted hashes", &val, true);
+	ghs = val / dev_runtime;
+	root = api_add_uint64(root, "Accepted hashrate", &ghs, true);
+	val = cgpu->diff_rejected * 0x100000000ull;
+	root = api_add_uint64(root, "Rejected hashes", &val, true);
+	ghs = val / dev_runtime;
+	root = api_add_uint64(root, "Rejected hashrate", &ghs, true);
+	root = api_add_uint32(root, "Uptime",&info->uptime,false);
+	__bin2hex(bitmaphex, info->pipe_bitmap, 16);
+	root = api_add_string(root, "Asic0Core0", bitmaphex, true);
+	__bin2hex(bitmaphex, &info->pipe_bitmap[16], 16);
+	root = api_add_string(root, "Asic0Core1", bitmaphex, true);
+	__bin2hex(bitmaphex, &info->pipe_bitmap[32], 16);
+	root = api_add_string(root, "Asic0Core2", bitmaphex, true);
+	__bin2hex(bitmaphex, &info->pipe_bitmap[48], 16);
+	root = api_add_string(root, "Asic0Core3", bitmaphex, true);
+	__bin2hex(bitmaphex, &info->pipe_bitmap[64], 16);
+	root = api_add_string(root, "Asic1Core0", bitmaphex, true);
+	__bin2hex(bitmaphex, &info->pipe_bitmap[80], 16);
+	root = api_add_string(root, "Asic1Core1", bitmaphex, true);
+	__bin2hex(bitmaphex, &info->pipe_bitmap[96], 16);
+	root = api_add_string(root, "Asic1Core2", bitmaphex, true);
+	__bin2hex(bitmaphex, &info->pipe_bitmap[112], 16);
+	root = api_add_string(root, "Asic1Core3", bitmaphex, true);
+
+	return root;
+}
+
+static void cta_statline_before(char *buf, size_t bufsiz, struct cgpu_info *cointerra)
+{
+	struct cointerra_info *info = cointerra->device_data;
+	double max_temp = 0, max_volt = 0;
+	int freq = 0, i;
+
+	for (i = 0; i < CTA_CORES; i++) {
+		if (info->coretemp[i] > max_temp)
+			max_temp = info->coretemp[i];
+		if (info->corevolts[i] > max_volt)
+			max_volt = info->corevolts[i];
+		if (info->corefreqs[i] > freq)
+			freq = info->corefreqs[i];
+	}
+	max_temp /= 100;
+	max_volt /= 100;
+
+	tailsprintf(buf, bufsiz, "%3d %3.1fC %2.1fV | ", freq, max_temp, max_volt);
+}
+
+struct device_drv cointerra_drv = {
+	.drv_id = DRIVER_cointerra,
+	.dname = "cointerra",
+	.name = "CTA",
+	.drv_detect = cta_detect,
+	.thread_prepare = cta_prepare,
+	.hash_work = hash_queued_work,
+	.queue_full = cta_fill,
+	.update_work = cta_update_work,
+	.scanwork = cta_scanwork,
+	.flush_work = cta_wake,
+	.get_api_stats = cta_api_stats,
+	.get_statline_before = cta_statline_before,
+	.thread_shutdown = cta_shutdown,
+	.zero_stats = cta_zero_stats,
+	.max_diff = 256, // Set it below the actual limit to check nonces
+};
diff --git a/driver-cointerra.h b/driver-cointerra.h
new file mode 100644
index 0000000..5205f7c
--- /dev/null
+++ b/driver-cointerra.h
@@ -0,0 +1,199 @@
+/*
+ * Copyright 2013 Con Kolivas <kernel@kolivas.org>
+ *
+ * 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 COINTERRA_H
+#define COINTERRA_H
+
+#define CTA_READBUF_SIZE 8192
+#define CTA_MSG_SIZE 64
+#define CTA_READ_TIMEOUT 1
+#define CTA_READ_INTERVAL 100
+#define CTA_SCAN_INTERVAL 500
+#define CTA_RESET_TIMEOUT 200
+
+#define CTA_INIT_DIFF		32*0.9999847412109375
+
+#if 0
+/* FIXME: how big should this be? */
+#define CTA_MAX_QUEUE 2300
+#else
+#define CTA_MAX_QUEUE (32 / CTA_NROLL_TIME)
+#endif
+
+#define CTA_NROLL_TIME 2
+
+/* Offsets into buffer */
+#define CTA_MSG_TYPE		2
+#define CTA_RESET_TYPE		3
+#define CTA_RESET_DIFF		4
+#define CTA_RESET_LOAD		5
+#define CTA_DRIVER_TAG		3
+#define CTA_MCU_TAG		5
+#define CTA_MCU_CORE		5
+#define CTA_MCU_ASIC		6
+#define CTA_MCU_PIPE		8
+#define CTA_MATCH_NOFFSET	45
+#define CTA_MATCH_NONCE		60
+#define CTA_WDONE_NONCES	11
+#define CTA_MSG_RECVD		3
+#define CTA_WORK_MIDSTATE	9
+#define CTA_WORK_DATA		41
+#define CTA_WORK_NROLL		53
+#define CTA_WORK_DIFFBITS	55
+#define CTA_REQWORK_REQUESTS	3
+
+/* Received message types */
+#define CTA_RECV_UNUSED		0
+#define CTA_RECV_REQWORK	1
+#define CTA_RECV_MATCH		2
+#define CTA_RECV_WDONE		3
+#define CTA_RECV_STATREAD	4
+#define CTA_RECV_STATSET	5
+#define CTA_RECV_INFO		6
+#define CTA_RECV_MSG            7
+#define CTA_RECV_RDONE		8
+#define CTA_RECV_STATDEBUG	10
+
+/* Sent message types */
+#define CTA_SEND_UNUSED		0
+#define CTA_SEND_RESET		1
+#define CTA_SEND_WORK		2
+#define CTA_SEND_SETPERF	3
+#define CTA_SEND_REQUEST	4
+#define CTA_SEND_FMATCH		5
+#define CTA_SEND_IDENTIFY	6
+
+/* Types of reset in CTA_RESET_TYPE */
+#define CTA_RESET_NONE		0
+#define CTA_RESET_UPDATE	1
+#define CTA_RESET_NEW		2
+#define CTA_RESET_INIT		3
+
+#define CTA_INFO_HWREV		3
+#define CTA_INFO_SERNO		5
+#define CTA_INFO_NUMASICS	9
+#define CTA_INFO_NUMDIES	10
+#define CTA_INFO_NUMCORES	11
+#define CTA_INFO_BOARDNUMBER	13
+#define CTA_INFO_FWREV_MAJ	19
+#define CTA_INFO_FWREV_MIN	20
+#define CTA_INFO_FWREV_MIC	21
+#define CTA_INFO_FWDATE_YEAR	23
+#define CTA_INFO_FWDATE_MONTH	25
+#define CTA_INFO_FWDATE_DAY	26
+#define CTA_INFO_INITDIFFBITS	27
+#define CTA_INFO_MINDIFFBITS	28
+#define CTA_INFO_MAXDIFFBITS	29
+
+#define CTA_STAT_CORETEMPS	3
+#define CTA_STAT_AMBTEMP_LOW	19
+#define CTA_STAT_AMBTEMP_AVG	21
+#define CTA_STAT_AMBTEMP_HIGH	23
+#define CTA_STAT_PUMP_TACHS	25
+#define CTA_STAT_FAN_TACHS	29
+#define CTA_STAT_CORE_VOLTS	37
+#define CTA_STAT_VOLTS33	53
+#define CTA_STAT_VOLTS12	55
+#define CTA_STAT_INACTIVE	57
+#define CTA_STAT_ACTIVE		59
+
+#define CTA_STAT_PERFMODE	3
+#define CTA_STAT_FANSPEEDS	11
+#define CTA_STAT_DIES_ACTIVE	15
+#define CTA_STAT_PIPES_ENABLED	16
+#define CTA_STAT_MIN_FAN_SPEED 	24
+#define CTA_STAT_UPTIME		25
+#define CTA_STAT_HEARTBEATS	29
+#define CTA_STAT_CORE_FREQS	45
+
+#define CTA_STAT_UNDERRUNS	3
+#define CTA_STAT_HW_ERRORS	5
+#define CTA_STAT_UPTIME_MS	21
+#define CTA_STAT_HASHES		25
+#define CTA_STAT_FLUSHED_HASHES	33
+
+#define CTA_CORES		8
+#define CTA_PUMPS		2
+#define CTA_FANS		4
+
+#define CTA_REQ_MSGTYPE		3
+#define CTA_REQ_INTERVAL	5
+
+
+int opt_cta_load;
+
+struct cointerra_info {
+	/* Info data */
+	uint16_t hwrev;
+	uint32_t serial;
+	uint8_t asics;
+	uint8_t dies;
+	uint16_t cores;
+	uint8_t board_number;
+	uint8_t fwrev[3];
+	uint16_t fw_year;
+	uint8_t fw_month;
+	uint8_t fw_day;
+	uint8_t init_diffbits;
+	uint8_t min_diffbits;
+	uint8_t max_diffbits;
+
+	/* Status readings data */
+	uint16_t coretemp[CTA_CORES];
+	uint16_t ambtemp_low;
+	uint16_t ambtemp_avg;
+	uint16_t ambtemp_high;
+	uint16_t pump_tachs[CTA_PUMPS];
+	uint16_t fan_tachs[CTA_FANS];
+	uint16_t corevolts[CTA_CORES];
+	uint16_t volts33;
+	uint16_t volts12;
+	uint16_t inactive;
+	uint16_t active;
+	uint16_t corefreqs[CTA_CORES];
+	uint32_t uptime;
+
+	/* Status settings data */
+	uint8_t coreperf[CTA_CORES];
+	uint8_t fanspeed[CTA_FANS];
+	uint8_t dies_active;
+	uint8_t pipes_enabled[CTA_CORES];
+
+	/* Status debug data */
+	uint16_t underruns;
+	uint16_t hw_errors[CTA_CORES];
+
+	/* Running total from debug messages */
+	int tot_underruns;
+	uint16_t tot_hw_errors[CTA_CORES];
+	uint64_t tot_hashes;
+	uint64_t tot_reset_hashes;
+	uint64_t tot_flushed_hashes;
+
+	/* Calculated totals based on work done and nonces found */
+	uint64_t hashes;
+	uint64_t tot_calc_hashes;
+
+	/* Calculated totals based on shares returned */
+	uint64_t share_hashes;
+	int requested;
+	uint16_t work_id;
+	int no_matching_work;
+	time_t last_pipe_nonce[1024];
+	unsigned char pipe_bitmap[128];
+
+	struct thr_info *thr;
+	pthread_mutex_t lock;
+	pthread_mutex_t sendlock;
+	pthread_cond_t wake_cond;
+	pthread_t read_thr;
+	cgsem_t reset_sem;
+};
+
+#endif /* COINTERRA_H */
diff --git a/miner.h b/miner.h
index 9d89f9a..cfbf836 100644
--- a/miner.h
+++ b/miner.h
@@ -234,6 +234,7 @@ static inline int fsync (int fd)
 #define ASIC_PARSE_COMMANDS(DRIVER_ADD_COMMAND) \
 	DRIVER_ADD_COMMAND(bflsc) \
 	DRIVER_ADD_COMMAND(bitfury) \
+	DRIVER_ADD_COMMAND(cointerra) \
 	DRIVER_ADD_COMMAND(hashfast) \
 	DRIVER_ADD_COMMAND(klondike) \
 	DRIVER_ADD_COMMAND(knc) \
@@ -600,6 +601,16 @@ static inline void swab256(void *dest_p, const void *src_p)
 	dest[7] = swab32(src[0]);
 }
 
+static inline void flip12(void *dest_p, const void *src_p)
+{
+	uint32_t *dest = dest_p;
+	const uint32_t *src = src_p;
+	int i;
+
+	for (i = 0; i < 3; i++)
+		dest[i] = swab32(src[i]);
+}
+
 static inline void flip32(void *dest_p, const void *src_p)
 {
 	uint32_t *dest = dest_p;
@@ -1400,6 +1411,7 @@ extern void free_work(struct work *work);
 extern void set_work_ntime(struct work *work, int ntime);
 extern struct work *copy_work_noffset(struct work *base_work, int noffset);
 #define copy_work(work_in) copy_work_noffset(work_in, 0)
+extern uint64_t share_diff(const struct work *work);
 extern struct thr_info *get_thread(int thr_id);
 extern struct cgpu_info *get_devices(int id);
 
diff --git a/usbutils.c b/usbutils.c
index 3ba89c7..a940f1b 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -66,6 +66,7 @@ static cgtimer_t usb11_cgt;
 #define MODMINER_TIMEOUT_MS 999
 #define AVALON_TIMEOUT_MS 999
 #define KLONDIKE_TIMEOUT_MS 999
+#define COINTERRA_TIMEOUT_MS 999
 #define HASHFAST_TIMEOUT_MS 999
 
 /* The safety timeout we use, cancelling async transfers on windows that fail
@@ -78,6 +79,7 @@ static cgtimer_t usb11_cgt;
 #define MODMINER_TIMEOUT_MS 100
 #define AVALON_TIMEOUT_MS 200
 #define KLONDIKE_TIMEOUT_MS 200
+#define COINTERRA_TIMEOUT_MS 200
 #define HASHFAST_TIMEOUT_MS 500
 #endif
 
@@ -310,6 +312,17 @@ static struct usb_intinfo cmr2_ints[] = {
 };
 #endif
 
+#ifdef USE_COINTERRA
+static struct usb_epinfo cointerra_epinfos[] = {
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(1), 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(1), 0, 0 }
+};
+
+static struct usb_intinfo cointerra_ints[] = {
+	USB_EPS(0, cointerra_epinfos)
+};
+#endif
+
 #define IDVENDOR_FTDI 0x0403
 
 #define INTINFO(_ints) \
@@ -564,6 +577,18 @@ static struct usb_find_devices find_dev[] = {
 		.latency = LATENCY_STD,
 		INTINFO(cmr2_ints) },
 #endif
+#ifdef USE_COINTERRA
+	{
+		.drv = DRIVER_cointerra,
+		.name = "CTA",
+		.ident = IDENT_CTA,
+		.idVendor = 0x1cbe,
+		.idProduct = 0x0003,
+		.config = 1,
+		.timeout = COINTERRA_TIMEOUT_MS,
+		.latency = LATENCY_STD,
+		INTINFO(cointerra_ints) },
+#endif
 	{ DRIVER_MAX, NULL, 0, 0, 0, NULL, NULL, 0, 0, 0, 0, NULL }
 };
 
@@ -3172,6 +3197,7 @@ void usb_cleanup(void)
 			case DRIVER_bflsc:
 			case DRIVER_bitforce:
 			case DRIVER_bitfury:
+			case DRIVER_cointerra:
 			case DRIVER_drillbit:
 			case DRIVER_modminer:
 			case DRIVER_icarus:
diff --git a/usbutils.h b/usbutils.h
index a9ee127..d53d98e 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -149,6 +149,7 @@ enum sub_ident {
 	IDENT_BXF,
 	IDENT_CMR1,
 	IDENT_CMR2,
+	IDENT_CTA,
 	IDENT_DRB,
 	IDENT_HFA,
 	IDENT_ICA,
@@ -392,6 +393,8 @@ struct cg_usb_info {
 	USB_ADD_COMMAND(C_HF_GETHEADER, "HFGetHeader") \
 	USB_ADD_COMMAND(C_HF_GETDATA, "HFGetData") \
 	USB_ADD_COMMAND(C_HF_CLEAR_READ, "HFClearRead") \
+	USB_ADD_COMMAND(C_CTA_READ, "CTARead") \
+	USB_ADD_COMMAND(C_CTA_WRITE, "CTAWrite") \
 	USB_ADD_COMMAND(C_MCP_GETGPIOSETTING, "MCPGetGPIOSetting") \
 	USB_ADD_COMMAND(C_MCP_SETGPIOSETTING, "MCPSetGPIOSetting") \
 	USB_ADD_COMMAND(C_MCP_GETGPIOPINVAL, "MCPGetGPIOPinVal") \