Merge branch 'ztex-master' of https://github.com/nelisky/cgminer into ztex
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
diff --git a/Makefile.am b/Makefile.am
index 02ee6f0..f94eae3 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -13,7 +13,7 @@ EXTRA_DIST = example.conf m4/gnulib-cache.m4 linux-usb-cgminer \
SUBDIRS = lib compat ccan
-INCLUDES = $(PTHREAD_FLAGS) -fno-strict-aliasing $(JANSSON_INCLUDES)
+INCLUDES = $(PTHREAD_FLAGS) -fno-strict-aliasing $(JANSSON_INCLUDES) $(USB_FLAGS)
bin_PROGRAMS = cgminer
@@ -22,7 +22,7 @@ bin_SCRIPTS = *.cl
cgminer_LDFLAGS = $(PTHREAD_FLAGS)
cgminer_LDADD = $(DLOPEN_FLAGS) @LIBCURL_LIBS@ @JANSSON_LIBS@ @PTHREAD_LIBS@ \
@OPENCL_LIBS@ @NCURSES_LIBS@ @PDCURSES_LIBS@ @WS2_LIBS@ \
- @UDEV_LIBS@ \
+ @UDEV_LIBS@ @USB_LIBS@ \
@MATH_LIBS@ lib/libgnu.a ccan/libccan.a
cgminer_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib @OPENCL_FLAGS@
@@ -74,3 +74,7 @@ endif
if HAS_ICARUS
cgminer_SOURCES += icarus.c
endif
+
+if HAS_ZTEX
+cgminer_SOURCES += ztex.c libztex.c
+endif
diff --git a/bitstreams/LICENSE.txt b/bitstreams/LICENSE.txt
new file mode 100644
index 0000000..99cd2ed
--- /dev/null
+++ b/bitstreams/LICENSE.txt
@@ -0,0 +1,24 @@
+All the bitstream files included in this directory that follow the name pattern ztex_*.bit are:
+
+----
+
+Copyright (C) 2009-2011 ZTEX GmbH.
+http://www.ztex.de
+
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License version 3 as
+published by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, see http://www.gnu.org/licenses/.
+
+----
+
+You can find the original sources at the BTCMiner project home page: http://www.ztex.de/btcminer/
+
diff --git a/bitstreams/ztex_ufm1_15b1.bit b/bitstreams/ztex_ufm1_15b1.bit
new file mode 100644
index 0000000..4ed6f4c
Binary files /dev/null and b/bitstreams/ztex_ufm1_15b1.bit differ
diff --git a/bitstreams/ztex_ufm1_15d1.bit b/bitstreams/ztex_ufm1_15d1.bit
new file mode 100644
index 0000000..b87eaf4
Binary files /dev/null and b/bitstreams/ztex_ufm1_15d1.bit differ
diff --git a/bitstreams/ztex_ufm1_15d3.bit b/bitstreams/ztex_ufm1_15d3.bit
new file mode 100644
index 0000000..78157a5
Binary files /dev/null and b/bitstreams/ztex_ufm1_15d3.bit differ
diff --git a/cgminer.c b/cgminer.c
index 33203dc..1577dec 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -54,7 +54,6 @@
#include <sys/wait.h>
#endif
-
enum workio_commands {
WC_GET_WORK,
WC_SUBMIT_WORK,
@@ -1017,6 +1016,9 @@ static char *opt_verusage_and_exit(const char *extra)
#ifdef USE_ICARUS
"icarus "
#endif
+#ifdef USE_ZTEX
+ "ztex "
+#endif
"mining support.\n"
, packagename);
printf("%s", opt_usage(opt_argv0, extra));
@@ -1526,6 +1528,8 @@ static bool submit_upstream_work(const struct work *work)
res = json_object_get(val, "result");
if (!QUIET) {
+#ifndef MIPSEB
+// This one segfaults on my router for some reason
isblock = regeneratehash(work);
if (isblock)
found_blocks++;
@@ -1533,6 +1537,7 @@ static bool submit_upstream_work(const struct work *work)
sprintf(hashshow, "%08lx.%08lx.%08lx%s",
(unsigned long)(hash32[7]), (unsigned long)(hash32[6]), (unsigned long)(hash32[5]),
isblock ? " BLOCK!" : "");
+#endif
}
/* Theoretically threads could race when modifying accepted and
@@ -4130,6 +4135,9 @@ static void clean_up(void)
#ifdef HAVE_OPENCL
clear_adl(nDevs);
#endif
+#ifdef HAVE_LIBUSB
+ libusb_exit(NULL);
+#endif
gettimeofday(&total_tv_end, NULL);
#ifdef HAVE_CURSES
@@ -4385,6 +4393,10 @@ extern struct device_api bitforce_api;
extern struct device_api icarus_api;
#endif
+#ifdef USE_ZTEX
+extern struct device_api ztex_api;
+#endif
+
static int cgminer_id_count = 0;
@@ -4419,6 +4431,9 @@ int main(int argc, char *argv[])
for (i = 0; i < argc; i++)
initial_args[i] = strdup(argv[i]);
initial_args[argc] = NULL;
+#ifdef HAVE_LIBUSB
+ libusb_init(NULL);
+#endif
mutex_init(&hash_lock);
mutex_init(&qd_lock);
@@ -4572,6 +4587,10 @@ int main(int argc, char *argv[])
icarus_api.api_detect();
#endif
+#ifdef USE_ZTEX
+ ztex_api.api_detect();
+#endif
+
#ifdef WANT_CPUMINE
cpu_api.api_detect();
#endif
diff --git a/configure.ac b/configure.ac
index 4f7ed9a..16dc55a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -62,6 +62,8 @@ AC_FUNC_ALLOCA
have_win32=false
PTHREAD_FLAGS="-lpthread"
+USB_LIBS=""
+USB_FLAGS=""
DLOPEN_FLAGS="-ldl"
OPENCL_LIBS="-lOpenCL"
WS2_LIBS=""
@@ -207,6 +209,16 @@ if test "x$icarus" = xyes; then
fi
AM_CONDITIONAL([HAS_ICARUS], [test x$icarus = xyes])
+ztex="no"
+
+AC_ARG_ENABLE([ztex],
+ [AC_HELP_STRING([--enable-ztex],[Compile support for Ztex (default disabled)])],
+ [ztex=$enableval]
+ )
+if test "x$ztex" = xyes; then
+ AC_DEFINE([USE_ZTEX], [1], [Defined to 1 if Ztex support is wanted])
+fi
+AM_CONDITIONAL([HAS_ZTEX], [test x$ztex = xyes])
curses="auto"
@@ -301,6 +313,14 @@ if test "x$bitforce" != xno; then
fi
AM_CONDITIONAL([HAVE_LIBUDEV], [test x$libudev != xno])
+if test "x$ztex" != xno; then
+ AC_CHECK_LIB(usb-1.0, libusb_init, ,
+ AC_MSG_ERROR([Could not find usb library - please install libusb]))
+ AC_DEFINE([HAVE_LIBUSB], [1], [Defined to 1 if libusb is wanted])
+ USB_LIBS="-lusb-1.0"
+ USB_FLAGS=""
+fi
+
PKG_PROG_PKG_CONFIG()
PKG_CHECK_MODULES([LIBCURL], [libcurl >= 7.15.6], [AC_DEFINE([CURL_HAS_SOCKOPT], [1], [Defined if version of curl supports sockopts.])],
@@ -362,6 +382,8 @@ AC_SUBST(PDCURSES_LIBS)
AC_SUBST(WS2_LIBS)
AC_SUBST(MATH_LIBS)
AC_SUBST(UDEV_LIBS)
+AC_SUBST(USB_LIBS)
+AC_SUBST(USB_FLAGS)
AC_CONFIG_FILES([
Makefile
@@ -393,13 +415,13 @@ if test "x$opencl" != xno; then
echo " OpenCL...............: FOUND. GPU mining support enabled"
else
echo " OpenCL...............: NOT FOUND. GPU mining support DISABLED"
- if test "x$cpumining$bitforce$icarus" = xnonono; then
+ if test "x$cpumining$bitforce$icarus$ztex" = xnononono; then
AC_MSG_ERROR([No mining configured in])
fi
fi
else
echo " OpenCL...............: Detection overrided. GPU mining support DISABLED"
- if test "x$cpumining$bitforce$icarus" = xnonono; then
+ if test "x$cpumining$bitforce$icarus$ztex" = xnononono; then
AC_MSG_ERROR([No mining configured in])
fi
fi
@@ -427,6 +449,12 @@ else
echo " Icarus.FPGAs.........: Disabled"
fi
+if test "x$ztex" = xyes; then
+ echo " Ztex.FPGAs...........: Enabled"
+else
+ echo " Ztex.FPGAs...........: Disabled"
+fi
+
if test "x$bitforce" != xno; then
echo " libudev.detection....: $libudev"
fi
@@ -441,8 +469,8 @@ echo
echo "Compilation............: make (or gmake)"
echo " CPPFLAGS.............: $CPPFLAGS"
echo " CFLAGS...............: $CFLAGS"
-echo " LDFLAGS..............: $LDFLAGS $PTHREAD_FLAGS"
-echo " LDADD................: $DLOPEN_FLAGS $LIBCURL_LIBS $JANSSON_LIBS $PTHREAD_LIBS $OPENCL_LIBS $NCURSES_LIBS $PDCURSES_LIBS $WS2_LIBS $MATH_LIBS $UDEV_LIBS"
+echo " LDFLAGS..............: $LDFLAGS $PTHREAD_FLAGS $USB_FLAGS"
+echo " LDADD................: $DLOPEN_FLAGS $LIBCURL_LIBS $JANSSON_LIBS $PTHREAD_LIBS $OPENCL_LIBS $NCURSES_LIBS $PDCURSES_LIBS $WS2_LIBS $MATH_LIBS $UDEV_LIBS $USB_LIBS"
echo
echo "Installation...........: make install (as root if needed, with 'su' or 'sudo')"
echo " prefix...............: $prefix"
diff --git a/libztex.c b/libztex.c
new file mode 100644
index 0000000..28716d6
--- /dev/null
+++ b/libztex.c
@@ -0,0 +1,459 @@
+/**
+ * libztex.c - Ztex 1.15x fpga board support library
+ *
+ * Copyright (c) 2012 nelisky.btc@gmail.com
+ *
+ * This work is based upon the Java SDK provided by ztex which is
+ * Copyright (C) 2009-2011 ZTEX GmbH.
+ * http://www.ztex.de
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see http://www.gnu.org/licenses/.
+**/
+
+#include <stdio.h>
+#include <unistd.h>
+#include "miner.h"
+#include "libztex.h"
+
+#define BUFSIZE 256
+
+//* Capability index for EEPROM support.
+#define CAPABILITY_EEPROM 0,0
+//* Capability index for FPGA configuration support.
+#define CAPABILITY_FPGA 0,1
+//* Capability index for FLASH memory support.
+#define CAPABILITY_FLASH 0,2
+//* Capability index for DEBUG helper support.
+#define CAPABILITY_DEBUG 0,3
+//* Capability index for AVR XMEGA support.
+#define CAPABILITY_XMEGA 0,4
+//* Capability index for AVR XMEGA support.
+#define CAPABILITY_HS_FPGA 0,5
+//* Capability index for AVR XMEGA support.
+#define CAPABILITY_MAC_EEPROM 0,6
+
+
+
+static bool libztex_checkDevice (struct libusb_device *dev) {
+ int err;
+
+ struct libusb_device_descriptor desc;
+ err = libusb_get_device_descriptor(dev, &desc);
+ if (unlikely(err != 0)) {
+ applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
+ return false;
+ }
+ if (!(desc.idVendor == LIBZTEX_IDVENDOR && desc.idProduct == LIBZTEX_IDPRODUCT)) {
+ applog(LOG_DEBUG, "Not a ZTEX device %0.4x:%0.4x", desc.idVendor, desc.idProduct);
+ return false;
+ }
+ return true;
+}
+
+static bool libztex_checkCapability (struct libztex_device *ztex, int i, int j) {
+ if (!((i>=0) && (i<=5) && (j>=0) && (j<8) &&
+ (((ztex->interfaceCapabilities[i] & 255) & (1 << j)) != 0)))
+ applog(LOG_ERR, "%s: capability missing: %d %d", ztex->repr, i, i);
+ return true;
+}
+
+static int libztex_detectBitstreamBitOrder (const unsigned char *buf, int size) {
+ int i;
+ size -= 4;
+ for (i=0; i<size; i++) {
+ if ( ((buf[i] & 255)==0xaa) && ((buf[i+1] & 255)==0x99) && ((buf[i+2] & 255)==0x55) && ((buf[i+3] & 255)==0x66) )
+ return 1;
+ if ( ((buf[i] & 255)==0x55) && ((buf[i+1] & 255)==0x99) && ((buf[i+2] & 255)==0xaa) && ((buf[i+3] & 255)==0x66) )
+ return 0;
+ }
+ applog(LOG_WARNING, "Unable to determine bitstream bit order: no signature found");
+ return 0;
+}
+
+static void libztex_swapBits (unsigned char *buf, int size) {
+ int i;
+ unsigned char c;
+ for (i=0; i<size; i++) {
+ c = buf[i];
+ buf[i] = ((c & 128) >> 7) |
+ ((c & 64) >> 5) |
+ ((c & 32) >> 3) |
+ ((c & 16) >> 1) |
+ ((c & 8) << 1) |
+ ((c & 4) << 3) |
+ ((c & 2) << 5) |
+ ((c & 1) << 7);
+ }
+}
+
+static int libztex_getFpgaState (struct libztex_device *ztex, struct libztex_fpgastate *state) {
+ int cnt;
+ unsigned char buf[9];
+ if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
+ return -1;
+ cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x30, 0, 0, buf, 9, 1000);
+ if (unlikely(cnt < 0)) {
+ applog(LOG_ERR, "%s: Failed getFpgaState with err %d", ztex->repr, cnt);
+ return cnt;
+ }
+ state->fpgaConfigured = (buf[0] == 0);
+ state->fpgaChecksum = buf[1] & 0xff;
+ state->fpgaBytes = ((buf[5] & 0xff)<<24) | ((buf[4] & 0xff)<<16) | ((buf[3] & 0xff)<<8) | (buf[2] & 0xff);
+ state->fpgaInitB = buf[6] & 0xff;
+ state->fpgaFlashResult = buf[7];
+ state->fpgaFlashBitSwap = (buf[8] != 0);
+ return 0;
+}
+
+static int libztex_configureFpgaLS (struct libztex_device *ztex, const char* firmware, bool force, char bs) {
+ struct libztex_fpgastate state;
+ ssize_t pos=0;
+ int transactionBytes = 2048;
+ unsigned char buf[transactionBytes], cs;
+ int tries, cnt, buf_p, i;
+ FILE *fp;
+
+ if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
+ return -1;
+
+ libztex_getFpgaState(ztex, &state);
+ if (!force && state.fpgaConfigured) {
+ applog(LOG_DEBUG, "Bitstream already configured");
+ return 1;
+ }
+
+ for (tries=10; tries>0; tries--) {
+
+ fp = fopen(firmware, "rb");
+ if (!fp) {
+ applog(LOG_ERR, "%s: failed to read firmware '%s'", ztex->repr, firmware);
+ return -2;
+ }
+
+ cs = 0;
+ while (pos < transactionBytes && !feof(fp)) {
+ buf[pos] = getc(fp);
+ cs += buf[pos++];
+ };
+ if (feof(fp))
+ pos--;
+
+ if ( bs != 0 && bs != 1 )
+ bs = libztex_detectBitstreamBitOrder(buf, transactionBytes<pos ? transactionBytes : pos);
+
+ //* Reset fpga
+ cnt = libztex_resetFpga(ztex);
+ if (unlikely(cnt < 0)) {
+ applog(LOG_ERR, "%s: Failed reset fpga with err %d", ztex->repr, cnt);
+ continue;
+ }
+
+ if ( bs == 1 )
+ libztex_swapBits(buf, pos);
+
+ buf_p = pos;
+ while (1) {
+ i = 0;
+ while (i < buf_p) {
+ cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x32, 0, 0, &buf[i], buf_p-i, 5000);
+ if (unlikely(cnt < 0)) {
+ applog(LOG_ERR, "%s: Failed send fpga data with err %d", ztex->repr, cnt);
+ break;
+ }
+ i += cnt;
+ }
+ if (i < buf_p || buf_p < transactionBytes)
+ break;
+ buf_p = 0;
+ while (buf_p < transactionBytes && !feof(fp)) {
+ buf[buf_p] = getc(fp);
+ cs += buf[buf_p++];
+ };
+ if (feof(fp))
+ buf_p--;
+ pos += buf_p;
+ if (buf_p == 0)
+ break;
+ if ( bs == 1 )
+ libztex_swapBits(buf, buf_p);
+ }
+ if (cnt >= 0)
+ tries = 0;
+
+ fclose(fp);
+ }
+ libztex_getFpgaState(ztex, &state);
+ if (!state.fpgaConfigured) {
+ applog(LOG_ERR, "%s: FPGA configuration failed: DONE pin does not go high", ztex->repr);
+ return 3;
+ }
+ usleep(200000);
+ applog(LOG_INFO, "%s: FPGA configuration done", ztex->repr);
+ return 0;
+}
+
+int libztex_configureFpga (struct libztex_device *ztex) {
+ int rv;
+ char buf[256] = "bitstreams/";
+ memset(&buf[11], 0, 245);
+ strcpy(&buf[11], ztex->bitFileName);
+ strcpy(&buf[strlen(buf)], ".bit");
+
+ rv = libztex_configureFpgaLS(ztex, buf, true, 2);
+ return rv;
+}
+
+int libztex_setFreq (struct libztex_device *ztex, uint16_t freq) {
+ int cnt;
+ if (freq > ztex->freqMaxM)
+ freq = ztex->freqMaxM;
+
+ cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x83, freq, 0, NULL, 0, 500);
+ if (unlikely(cnt < 0)) {
+ applog(LOG_ERR, "Ztex check device: Failed to set frequency with err %d", cnt);
+ return cnt;
+ }
+ ztex->freqM = freq;
+ applog(LOG_WARNING, "%s: Frequency change to %0.2f Mhz", ztex->repr, ztex->freqM1 * (ztex->freqM + 1));
+
+ return 0;
+}
+
+int libztex_resetFpga (struct libztex_device *ztex) {
+ return libusb_control_transfer(ztex->hndl, 0x40, 0x31, 0, 0, NULL, 0, 1000);
+}
+
+int libztex_prepare_device (struct libusb_device *dev, struct libztex_device** ztex) {
+ struct libztex_device *newdev;
+ int cnt, err;
+ unsigned char buf[64];
+
+ newdev = malloc(sizeof(struct libztex_device));
+ newdev->valid = false;
+ newdev->hndl = NULL;
+ newdev->bitFileName = NULL;
+ *ztex = newdev;
+
+ err = libusb_get_device_descriptor(dev, &newdev->descriptor);
+ if (unlikely(err != 0)) {
+ applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
+ return err;
+ }
+
+ // Check vendorId and productId
+ if (!(newdev->descriptor.idVendor == LIBZTEX_IDVENDOR &&
+ newdev->descriptor.idProduct == LIBZTEX_IDPRODUCT)) {
+ applog(LOG_ERR, "Not a ztex device? %0.4X, %0.4X", newdev->descriptor.idVendor, newdev->descriptor.idProduct);
+ return 1;
+ }
+
+ err = libusb_open(dev, &newdev->hndl);
+ if (unlikely(err != 0)) {
+ applog(LOG_ERR, "Ztex check device: Failed to open handle with error %d", err);
+ return err;
+ }
+
+ cnt = libusb_get_string_descriptor_ascii (newdev->hndl, newdev->descriptor.iSerialNumber, newdev->snString,
+ LIBZTEX_SNSTRING_LEN+1);
+ if (unlikely(cnt < 0)) {
+ applog(LOG_ERR, "Ztex check device: Failed to read device snString with err %d", cnt);
+ return cnt;
+ }
+
+ cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x22, 0, 0, buf, 40, 500);
+ if (unlikely(cnt < 0)) {
+ applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
+ return cnt;
+ }
+
+ if ( buf[0]!=40 || buf[1]!=1 || buf[2]!='Z' || buf[3]!='T' || buf[4]!='E' || buf[5]!='X' ) {
+ applog(LOG_ERR, "Ztex check device: Error reading ztex descriptor");
+ return 2;
+ }
+
+ newdev->productId[0] = buf[6];
+ newdev->productId[1] = buf[7];
+ newdev->productId[2] = buf[8];
+ newdev->productId[3] = buf[9];
+ newdev->fwVersion = buf[10];
+ newdev->interfaceVersion = buf[11];
+ newdev->interfaceCapabilities[0] = buf[12];
+ newdev->interfaceCapabilities[1] = buf[13];
+ newdev->interfaceCapabilities[2] = buf[14];
+ newdev->interfaceCapabilities[3] = buf[15];
+ newdev->interfaceCapabilities[4] = buf[16];
+ newdev->interfaceCapabilities[5] = buf[17];
+ newdev->moduleReserved[0] = buf[18];
+ newdev->moduleReserved[1] = buf[19];
+ newdev->moduleReserved[2] = buf[20];
+ newdev->moduleReserved[3] = buf[21];
+ newdev->moduleReserved[4] = buf[22];
+ newdev->moduleReserved[5] = buf[23];
+ newdev->moduleReserved[6] = buf[24];
+ newdev->moduleReserved[7] = buf[25];
+ newdev->moduleReserved[8] = buf[26];
+ newdev->moduleReserved[9] = buf[27];
+ newdev->moduleReserved[10] = buf[28];
+ newdev->moduleReserved[11] = buf[29];
+
+
+ cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x82, 0, 0, buf, 64, 500);
+ if (unlikely(cnt < 0)) {
+ applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
+ return cnt;
+ }
+
+ if (unlikely(buf[0] != 4)) {
+ if (unlikely(buf[0] != 2)) {
+ applog(LOG_ERR, "Invalid BTCMiner descriptor version. Firmware must be updated (%d).", buf[0]);
+ return 3;
+ }
+ applog(LOG_WARNING, "Firmware out of date");
+ }
+
+ newdev->numNonces = buf[1] + 1;
+ newdev->offsNonces = ((buf[2] & 255) | ((buf[3] & 255) << 8)) - 10000;
+ newdev->freqM1 = ( (buf[4] & 255) | ((buf[5] & 255) << 8) ) * 0.01;
+ newdev->freqMaxM = (buf[7] & 255);
+ newdev->freqM = (buf[6] & 255);
+ newdev->freqMDefault = newdev->freqM;
+
+ for (cnt=0; cnt<255; cnt++) {
+ newdev->errorCount[cnt] = 0;
+ newdev->errorWeight[cnt] = 0;
+ newdev->errorRate[cnt] = 0;
+ newdev->maxErrorRate[cnt] = 0;
+ }
+
+ cnt = strlen((char *)&buf[buf[0]==4?10:8]);
+ newdev->bitFileName = malloc(sizeof(char)*(cnt+1));
+ memcpy(newdev->bitFileName, &buf[buf[0]==4?10:8], cnt+1);
+
+ newdev->usbbus = libusb_get_bus_number(dev);
+ newdev->usbaddress = libusb_get_device_address(dev);
+ sprintf(newdev->repr, "ZTEX %.3d:%.3d-%s", newdev->usbbus, newdev->usbaddress, newdev->snString);
+ newdev->valid = true;
+ return 0;
+}
+
+void libztex_destroy_device (struct libztex_device* ztex) {
+ if (ztex->hndl != NULL) {
+ libusb_close(ztex->hndl);
+ ztex->hndl = NULL;
+ }
+ if (ztex->bitFileName != NULL) {
+ free(ztex->bitFileName);
+ ztex->bitFileName = NULL;
+ }
+ free(ztex);
+}
+
+int libztex_scanDevices (struct libztex_dev_list*** devs_p) {
+ libusb_device **list;
+ struct libztex_device *ztex;
+ ssize_t cnt = libusb_get_device_list(NULL, &list);
+ ssize_t i = 0;
+ int found = 0, pos = 0, err;
+
+ if (unlikely(cnt < 0)) {
+ applog(LOG_ERR, "Ztex scan devices: Failed to list usb devices with err %d", cnt);
+ return 0;
+ }
+
+ int usbdevices[LIBZTEX_MAX_DESCRIPTORS];
+
+ for (i = 0; i < cnt; i++) {
+ if (libztex_checkDevice(list[i])) {
+ // Got one!
+ usbdevices[found] = i;
+ found++;
+ }
+ }
+
+ struct libztex_dev_list **devs;
+ devs = malloc(sizeof(struct libztex_dev_list *) * found);
+ if (devs == NULL) {
+ applog(LOG_ERR, "Ztex scan devices: Failed to allocate memory");
+ return 0;
+ }
+
+ for (i = 0; i < found; i++) {
+ err = libztex_prepare_device(list[usbdevices[i]], &ztex);
+ if (unlikely(err != 0))
+ applog(LOG_ERR, "prepare device: %d", err);
+ // check if valid
+ if (!ztex->valid) {
+ libztex_destroy_device(ztex);
+ continue;
+ }
+ devs[pos] = malloc(sizeof(struct libztex_dev_list));
+ devs[pos]->dev = ztex;
+ devs[pos]->next = NULL;
+ if (pos > 0)
+ devs[pos-1]->next = devs[pos];
+ pos++;
+ }
+
+ libusb_free_device_list(list, 1);
+ *devs_p = devs;
+ return pos;
+}
+
+int libztex_sendHashData (struct libztex_device *ztex, unsigned char *sendbuf) {
+ int cnt;
+ if (ztex == NULL || ztex->hndl == NULL)
+ return 0;
+ cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x80, 0, 0, sendbuf, 44, 1000);
+ if (unlikely(cnt < 0))
+ applog(LOG_ERR, "%s: Failed sendHashData with err %d", ztex->repr, cnt);
+
+ return cnt;
+}
+
+int libztex_readHashData (struct libztex_device *ztex, struct libztex_hash_data nonces[]) {
+ // length of buf must be 8 * (numNonces + 1)
+ unsigned char rbuf[12*8];
+ int cnt, i;
+
+ if (ztex->hndl == NULL)
+ return 0;
+
+ cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x81, 0, 0, rbuf, 12*ztex->numNonces, 1000);
+ if (unlikely(cnt < 0)) {
+ applog(LOG_ERR, "%s: Failed readHashData with err %d", ztex->repr, cnt);
+ return cnt;
+ }
+
+ for (i=0; i<ztex->numNonces; i++) {
+ memcpy((char*)&nonces[i].goldenNonce, &rbuf[i*12], 4);
+ nonces[i].goldenNonce -= ztex->offsNonces;
+ memcpy((char*)&nonces[i].nonce, &rbuf[(i*12)+4], 4);
+ nonces[i].nonce -= ztex->offsNonces;
+ memcpy((char*)&nonces[i].hash7, &rbuf[(i*12)+8], 4);
+ }
+
+ return cnt;
+}
+
+void libztex_freeDevList (struct libztex_dev_list **devs) {
+ ssize_t cnt = 0;
+ bool done = false;
+ while (!done) {
+ if (devs[cnt]->next == NULL)
+ done = true;
+ free(devs[cnt++]);
+ }
+ free(devs);
+}
+
+
diff --git a/libztex.h b/libztex.h
new file mode 100644
index 0000000..5f88af9
--- /dev/null
+++ b/libztex.h
@@ -0,0 +1,95 @@
+/**
+ * libztex.h - headers for Ztex 1.15x fpga board support library
+ *
+ * Copyright (c) 2012 nelisky.btc@gmail.com
+ *
+ * This work is based upon the Java SDK provided by ztex which is
+ * Copyright (C) 2009-2011 ZTEX GmbH.
+ * http://www.ztex.de
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see http://www.gnu.org/licenses/.
+**/
+#ifndef __LIBZTEX_H__
+#define __LIBZTEX_H__
+
+#include <libusb-1.0/libusb.h>
+
+#define LIBZTEX_MAX_DESCRIPTORS 512
+#define LIBZTEX_SNSTRING_LEN 10
+
+#define LIBZTEX_IDVENDOR 0x221A
+#define LIBZTEX_IDPRODUCT 0x0100
+
+#define LIBZTEX_MAXMAXERRORRATE 0.05
+#define LIBZTEX_ERRORHYSTERESIS 0.1
+#define LIBZTEX_OVERHEATTHRESHOLD 0.5
+
+struct libztex_fpgastate {
+ bool fpgaConfigured;
+ unsigned char fpgaChecksum;
+ uint16_t fpgaBytes;
+ unsigned char fpgaInitB;
+ unsigned char fpgaFlashResult;
+ bool fpgaFlashBitSwap;
+};
+
+struct libztex_device {
+ bool valid;
+ struct libusb_device_descriptor descriptor;
+ libusb_device_handle *hndl;
+ unsigned char usbbus;
+ unsigned char usbaddress;
+ unsigned char snString[LIBZTEX_SNSTRING_LEN+1];
+ unsigned char productId[4];
+ unsigned char fwVersion;
+ unsigned char interfaceVersion;
+ unsigned char interfaceCapabilities[6];
+ unsigned char moduleReserved[12];
+ uint8_t numNonces;
+ uint16_t offsNonces;
+ double freqM1;
+ uint8_t freqM;
+ uint8_t freqMaxM;
+ uint8_t freqMDefault;
+ char* bitFileName;
+
+ double errorCount[256];
+ double errorWeight[256];
+ double errorRate[256];
+ double maxErrorRate[256];
+
+ char repr[64];
+};
+
+struct libztex_dev_list {
+ struct libztex_device *dev;
+ struct libztex_dev_list *next;
+};
+
+struct libztex_hash_data {
+ uint32_t goldenNonce;
+ uint32_t nonce;
+ uint32_t hash7;
+};
+
+extern int libztex_scanDevices (struct libztex_dev_list ***devs);
+extern void libztex_freeDevList (struct libztex_dev_list **devs);
+extern int libztex_prepare_device (struct libusb_device *dev, struct libztex_device** ztex);
+extern void libztex_destroy_device (struct libztex_device* ztex);
+extern int libztex_configureFpga (struct libztex_device *dev);
+extern int libztex_setFreq (struct libztex_device *ztex, uint16_t freq);
+extern int libztex_sendHashData (struct libztex_device *ztex, unsigned char *sendbuf);
+extern int libztex_readHashData (struct libztex_device *ztex, struct libztex_hash_data nonces[]);
+extern int libztex_resetFpga (struct libztex_device *ztex);
+
+#endif /* __LIBZTEX_H__ */
diff --git a/miner.h b/miner.h
index 24a6ef3..69abfe7 100644
--- a/miner.h
+++ b/miner.h
@@ -61,6 +61,14 @@ void *alloca (size_t);
#include "ADL_SDK/adl_sdk.h"
#endif
+#ifdef HAVE_LIBUSB
+ #include <libusb-1.0/libusb.h>
+#endif
+
+#ifdef USE_ZTEX
+ #include "libztex.h"
+#endif
+
#if !defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
#define bswap_16 __builtin_bswap16
#define bswap_32 __builtin_bswap32
@@ -249,6 +257,9 @@ struct cgpu_info {
int device_id;
char *device_path;
FILE *device_file;
+#ifdef USE_ZTEX
+ struct libztex_device *device;
+#endif
int device_fd;
enum dev_enable deven;
diff --git a/mkinstalldirs b/mkinstalldirs
old mode 100644
new mode 100755
diff --git a/todo_ztex.txt b/todo_ztex.txt
new file mode 100644
index 0000000..4014b04
--- /dev/null
+++ b/todo_ztex.txt
@@ -0,0 +1,5 @@
+- verify setting cgpu.status=DEAD does in fact stop the thread
+- allow configuring bitstream directory
+- HS fpga config
+- allow configuring LIBZTEX_OVERHEATTHRESHOLD
+- hotplug support?
diff --git a/ztex.c b/ztex.c
new file mode 100644
index 0000000..e5ada7b
--- /dev/null
+++ b/ztex.c
@@ -0,0 +1,323 @@
+/**
+ * ztex.c - cgminer worker for Ztex 1.15x fpga board
+ *
+ * Copyright (c) 2012 nelisky.btc@gmail.com
+ *
+ * This work is based upon the Java SDK provided by ztex which is
+ * Copyright (C) 2009-2011 ZTEX GmbH.
+ * http://www.ztex.de
+ *
+ * This work is based upon the icarus.c worker which is
+ * Copyright 2012 Luke Dashjr
+ * Copyright 2012 Xiangfu <xiangfu@openmobilefree.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see http://www.gnu.org/licenses/.
+**/
+#include <unistd.h>
+#include <sha2.h>
+#include "miner.h"
+#include "libztex.h"
+
+#define GOLDEN_BACKLOG 5
+
+struct device_api ztex_api, ztex_hotplug_api;
+
+// Forward declarations
+static void ztex_disable (struct thr_info* thr);
+static bool ztex_prepare(struct thr_info *thr);
+
+static void ztex_detect()
+{
+ int cnt;
+ int i;
+ struct libztex_dev_list **ztex_devices;
+
+ cnt = libztex_scanDevices(&ztex_devices);
+ applog(LOG_WARNING, "Found %d ztex board(s)", cnt);
+
+ for (i=0; i<cnt; i++) {
+ if (total_devices == MAX_DEVICES)
+ break;
+ struct cgpu_info *ztex;
+ ztex = calloc(1, sizeof(struct cgpu_info));
+ ztex->api = &ztex_api;
+ ztex->device_id = total_devices;
+ ztex->device = ztex_devices[i]->dev;
+ ztex->threads = 1;
+ devices[total_devices++] = ztex;
+
+ applog(LOG_WARNING,"%s: Found Ztex, mark as %d", ztex->device->repr, ztex->device_id);
+ }
+
+ if (cnt > 0)
+ libztex_freeDevList(ztex_devices);
+}
+
+static bool ztex_updateFreq (struct libztex_device* ztex)
+{
+ int i, maxM, bestM;
+ double bestR, r;
+
+ for (i=0; i<ztex->freqMaxM; i++)
+ if (ztex->maxErrorRate[i+1]*i < ztex->maxErrorRate[i]*(i+20))
+ ztex->maxErrorRate[i+1] = ztex->maxErrorRate[i]*(1.0+20.0/i);
+
+ maxM = 0;
+ while (maxM<ztex->freqMDefault && ztex->maxErrorRate[maxM+1]<LIBZTEX_MAXMAXERRORRATE)
+ maxM++;
+ while (maxM<ztex->freqMaxM && ztex->errorWeight[maxM]>150 && ztex->maxErrorRate[maxM+1]<LIBZTEX_MAXMAXERRORRATE)
+ maxM++;
+
+ bestM = 0;
+ bestR = 0;
+ for (i=0; i<=maxM; i++) {
+ r = (i + 1 + ( i == ztex->freqM ? LIBZTEX_ERRORHYSTERESIS : 0))*(1-ztex->maxErrorRate[i]);
+ if (r > bestR) {
+ bestM = i;
+ bestR = r;
+ }
+ }
+
+ if (bestM != ztex->freqM)
+ libztex_setFreq(ztex, bestM);
+
+ maxM = ztex->freqMDefault;
+ while (maxM<ztex->freqMaxM && ztex->errorWeight[maxM+1] > 100)
+ maxM++;
+ if ((bestM < (1.0-LIBZTEX_OVERHEATTHRESHOLD) * maxM) && bestM < maxM - 1) {
+ libztex_resetFpga(ztex);
+ applog(LOG_ERR, "%s: frequency drop of %.1f%% detect. This may be caused by overheating. FPGA is shut down to prevent damage.", ztex->repr, (1.0-1.0*bestM/maxM)*100);
+ return false;
+ }
+ return true;
+}
+
+
+static bool ztex_checkNonce (struct libztex_device *ztex,
+ struct work *work,
+ struct libztex_hash_data *hdata)
+{
+ uint32_t *data32 = (uint32_t *)(work->data);
+ unsigned char swap[128];
+ uint32_t *swap32 = (uint32_t *)swap;
+ unsigned char hash1[32];
+ unsigned char hash2[32];
+ uint32_t *hash2_32 = (uint32_t *)hash2;
+ int i;
+
+#if defined(__BIGENDIAN__) || defined(MIPSEB)
+ hdata->nonce = swab32(hdata->nonce);
+ hdata->hash7 = swab32(hdata->hash7);
+#endif
+
+ work->data[64 + 12 + 0] = (hdata->nonce >> 0) & 0xff;
+ work->data[64 + 12 + 1] = (hdata->nonce >> 8) & 0xff;
+ work->data[64 + 12 + 2] = (hdata->nonce >> 16) & 0xff;
+ work->data[64 + 12 + 3] = (hdata->nonce >> 24) & 0xff;
+
+ for (i = 0; i < 80 / 4; i++)
+ swap32[i] = swab32(data32[i]);
+
+ sha2(swap, 80, hash1, false);
+ sha2(hash1, 32, hash2, false);
+#if defined(__BIGENDIAN__) || defined(MIPSEB)
+ if (hash2_32[7] != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
+#else
+ if (swab32(hash2_32[7]) != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
+#endif
+ ztex->errorCount[ztex->freqM] += 1.0/ztex->numNonces;
+ applog(LOG_DEBUG, "%s: checkNonce failed for %0.8X", ztex->repr, hdata->nonce);
+ return false;
+ }
+ return true;
+}
+
+static uint64_t ztex_scanhash(struct thr_info *thr, struct work *work,
+ __maybe_unused uint64_t max_nonce)
+{
+ struct libztex_device *ztex;
+ unsigned char sendbuf[44];
+ int i, j;
+ uint32_t backlog[GOLDEN_BACKLOG];
+ int backlog_p = 0;
+ uint32_t lastnonce[GOLDEN_BACKLOG], nonce, noncecnt = 0;
+ bool overflow, found, rv;
+ struct libztex_hash_data hdata[GOLDEN_BACKLOG];
+
+ ztex = thr->cgpu->device;
+
+ memcpy(sendbuf, work->data + 64, 12);
+ memcpy(sendbuf+12, work->midstate, 32);
+ memset(backlog, 0, sizeof(backlog));
+ i = libztex_sendHashData(ztex, sendbuf);
+ if (i < 0) {
+ // Something wrong happened in send
+ applog(LOG_ERR, "%s: Failed to send hash data with err %d, retrying", ztex->repr, i);
+ usleep(500000);
+ i = libztex_sendHashData(ztex, sendbuf);
+ if (i < 0) {
+ // And there's nothing we can do about it
+ ztex_disable(thr);
+ applog(LOG_ERR, "%s: Failed to send hash data with err %d, giving up", ztex->repr, i);
+ return 0;
+ }
+ }
+
+ applog(LOG_DEBUG, "sent hashdata");
+
+ for (i=0; i<ztex->numNonces; i++)
+ lastnonce[i] = 0;
+
+ overflow = false;
+
+ while (!(overflow || work_restart[thr->id].restart)) {
+ usleep(250000);
+ if (work_restart[thr->id].restart) {
+ applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
+ break;
+ }
+ i = libztex_readHashData(ztex, &hdata[0]);
+ if (i < 0) {
+ // Something wrong happened in read
+ applog(LOG_ERR, "%s: Failed to read hash data with err %d, retrying", ztex->repr, i);
+ usleep(500000);
+ i = libztex_readHashData(ztex, &hdata[0]);
+ if (i < 0) {
+ // And there's nothing we can do about it
+ ztex_disable(thr);
+ applog(LOG_ERR, "%s: Failed to read hash data with err %d, giving up", ztex->repr, i);
+ return 0;
+ }
+ }
+
+ if (work_restart[thr->id].restart) {
+ applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
+ break;
+ }
+
+ ztex->errorCount[ztex->freqM] *= 0.995;
+ ztex->errorWeight[ztex->freqM] = ztex->errorWeight[ztex->freqM] * 0.995 + 1.0;
+
+ for (i=0; i<ztex->numNonces; i++) {
+ nonce = hdata[i].nonce;
+#if defined(__BIGENDIAN__) || defined(MIPSEB)
+ nonce = swab32(nonce);
+#endif
+ if (nonce > noncecnt)
+ noncecnt = nonce;
+ if (((nonce & 0x7fffffff) >> 4) < ((lastnonce[i] & 0x7fffffff) >> 4)) {
+ applog(LOG_DEBUG, "%s: overflow nonce=%0.8x lastnonce=%0.8x", ztex->repr, nonce, lastnonce[i]);
+ overflow = true;
+ } else {
+ lastnonce[i] = nonce;
+ }
+#if !(defined(__BIGENDIAN__) || defined(MIPSEB))
+ nonce = swab32(nonce);
+#endif
+ if (!ztex_checkNonce(ztex, work, &hdata[i])) {
+ thr->cgpu->hw_errors++;
+ continue;
+ }
+ nonce = hdata[i].goldenNonce;
+ if (nonce > 0) {
+ found = false;
+ for (j=0; j<GOLDEN_BACKLOG; j++) {
+ if (backlog[j] == nonce) {
+ found = true;
+ break;
+ }
+ }
+ if (!found) {
+ applog(LOG_DEBUG, "%s: Share found", ztex->repr);
+ backlog[backlog_p++] = nonce;
+ if (backlog_p >= GOLDEN_BACKLOG)
+ backlog_p = 0;
+#if defined(__BIGENDIAN__) || defined(MIPSEB)
+ nonce = swab32(nonce);
+#endif
+ work->blk.nonce = 0xffffffff;
+ rv = submit_nonce(thr, work, nonce);
+ applog(LOG_DEBUG, "%s: submitted %0.8x %d", ztex->repr, nonce, rv);
+ }
+ }
+
+ }
+
+ }
+
+ ztex->errorRate[ztex->freqM] = ztex->errorCount[ztex->freqM] / ztex->errorWeight[ztex->freqM] * (ztex->errorWeight[ztex->freqM]<100 ? ztex->errorWeight[ztex->freqM]*0.01 : 1.0);
+ if (ztex->errorRate[ztex->freqM] > ztex->maxErrorRate[ztex->freqM])
+ ztex->maxErrorRate[ztex->freqM] = ztex->errorRate[ztex->freqM];
+
+ if (!ztex_updateFreq(ztex))
+ // Something really serious happened, so mark this thread as dead!
+ return 0;
+
+ applog(LOG_DEBUG, "%s: exit %1.8X", ztex->repr, noncecnt);
+
+ work->blk.nonce = 0xffffffff;
+
+ return noncecnt > 0 ? noncecnt : 1;
+}
+
+static void ztex_statline_before(char *buf, struct cgpu_info *cgpu)
+{
+ if (cgpu->deven == DEV_ENABLED) {
+ tailsprintf(buf, "%s | ", cgpu->device->snString);
+ tailsprintf(buf, "%0.2fMhz | ", cgpu->device->freqM1 * (cgpu->device->freqM + 1));
+ }
+}
+
+static bool ztex_prepare(struct thr_info *thr)
+{
+ struct timeval now;
+ struct cgpu_info *ztex = thr->cgpu;
+
+ gettimeofday(&now, NULL);
+ get_datestamp(ztex->init, &now);
+
+ if (libztex_configureFpga(ztex->device) != 0)
+ return false;
+
+ ztex->device->freqM = -1;
+ ztex_updateFreq(ztex->device);
+
+ applog(LOG_DEBUG, "%s: prepare", ztex->device->repr);
+ return true;
+}
+
+static void ztex_shutdown(struct thr_info *thr)
+{
+ if (thr->cgpu->device != NULL) {
+ applog(LOG_DEBUG, "%s: shutdown", thr->cgpu->device->repr);
+ libztex_destroy_device(thr->cgpu->device);
+ thr->cgpu->device = NULL;
+ }
+}
+
+static void ztex_disable (struct thr_info *thr)
+{
+ applog(LOG_ERR, "%s: Disabling!", thr->cgpu->device->repr);
+ devices[thr->cgpu->device_id]->deven = DEV_DISABLED;
+ ztex_shutdown(thr);
+}
+
+struct device_api ztex_api = {
+ .name = "ZTX",
+ .api_detect = ztex_detect,
+ .get_statline_before = ztex_statline_before,
+ .thread_prepare = ztex_prepare,
+ .scanhash = ztex_scanhash,
+ .thread_shutdown = ztex_shutdown,
+};
+