Browse Source

Merge pull request #500 from kanoi/klondike

Klondike
kanoi 12 years ago
parent
commit
2843670aff
11 changed files with 825 additions and 16 deletions
  1. 6 0
      01-cgminer.rules
  2. 11 5
      ASIC-README
  3. 4 0
      Makefile.am
  4. 8 6
      README
  5. 1 1
      api.c
  6. 24 0
      cgminer.c
  7. 20 3
      configure.ac
  8. 719 0
      driver-klondike.c
  9. 5 1
      miner.h
  10. 26 0
      usbutils.c
  11. 1 0
      usbutils.h

+ 6 - 0
01-cgminer.rules

@@ -16,8 +16,14 @@ ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", SUBSYSTEMS=="usb", ACTION=="a
 # Cairnsmore1
 ATTRS{idVendor}=="067b", ATTRS{idProduct}=="0230", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
 
+# Cairnsmore1-2
+ATTRS{idVendor}=="0403", ATTRS{idProduct}=="8350", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
+
 # Ztex
 ATTRS{idVendor}=="221a", ATTRS{idProduct}=="0100", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
 
 # BF1
 ATTRS{idVendor}=="03eb", ATTRS{idProduct}=="204b", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
+
+#Klondike
+ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="f60a", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"

+ 11 - 5
ASIC-README

@@ -1,10 +1,11 @@
 SUPPORTED DEVICES
 
-Currently supported devices include the Avalon (including BitBurner), the
-Butterfly Labs SC range of devices, the ASICMINER block erupters and the BPMC
-BF1 (bitfury) USB devices. 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 be disconnected when cgminer is running.
+Currently supported devices include the Avalon (including BitBurner and
+Klondike), the Butterfly Labs SC range of devices, the ASICMINER block
+erupters and the BPMC BF1 (bitfury) USB devices. 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 be disconnected when
+cgminer is running.
 
 
 The BFL devices should come up as one of the following:
@@ -20,6 +21,10 @@ Avalon will come up as AVA.
 
 Avalon devices need the --enable-avalon option when compiling cgminer.
 
+Klondike will come up as KLN.
+
+Klondike devices need the --enable-klondike option when compiling cgminer.
+
 ASICMINER block erupters will come up as AMU.
 
 ASICMINER devices need the --enable-icarus option when compiling cgminer.
@@ -98,6 +103,7 @@ ASIC SPECIFIC COMMANDS
 --avalon-temp <arg> Set avalon target temperature (default: 50)
 --bflsc-overheat <arg> Set overheat temperature where BFLSC devices throttle, 0 to disable (default: 90)
 --bitburner-voltage <arg> Set BitBurner core voltage, in millivolts
+--klondike-options <arg> Set klondike options clock:temp1:temp2:fan
 
 
 AVALON DEVICES

+ 4 - 0
Makefile.am

@@ -88,6 +88,10 @@ if HAS_AVALON
 cgminer_SOURCES += driver-avalon.c driver-avalon.h
 endif
 
+if HAS_KLONDIKE
+cgminer_SOURCES += driver-klondike.c
+endif
+
 if HAS_MODMINER
 cgminer_SOURCES += driver-modminer.c
 bitstreamsdir = $(bindir)/bitstreams

+ 8 - 6
README

@@ -114,6 +114,7 @@ CGMiner specific configuration options:
 	--enable-modminer       Compile support for ModMiner FPGAs(default disabled)
 	--enable-ztex           Compile support for Ztex Board(default disabled)
 	--enable-avalon         Compile support for Avalon (default disabled)
+	--enable-klondike       Compile support for Klondike (default disabled)
 	--enable-scrypt         Compile support for scrypt litecoin mining (default disabled)
 	--without-curses        Compile support for curses TUI (default enabled)
 
@@ -269,8 +270,8 @@ See SCRYPT-README for more information regarding litecoin mining.
 
 
 Cgminer should automatically find all of your Avalon ASIC, BFL ASIC, BitForce
-FPGAs, Icarus bitstream FPGAs, ASICMINER usb block erupters, ModMiner FPGAs,
-or Ztex FPGAs
+FPGAs, Icarus bitstream FPGAs, Klondike ASIC, ASICMINER usb block erupters,
+ModMiner FPGAs or Ztex FPGAs
 
 ---
 
@@ -314,11 +315,11 @@ just reboot.
 Advanced USB options:
 
 The --usb option can restrict how many Avalon, BFL ASIC, BitForce FPGAs,
-ModMiner FPGAs or Icarus bitstream FPGAs it finds:
+Klondike ASIC, ModMiner FPGAs or Icarus bitstream FPGAs it finds:
 
   --usb 1:2,1:3,1:4,1:*
 or
-  --usb BAS:1,BFL:1,MMQ:0,ICA:0
+  --usb BAS:1,BFL:1,MMQ:0,ICA:0,KLN:0
 or
   --usb :10
 
@@ -344,9 +345,10 @@ of details about each recognised USB device
 If you wish to see all USB devices, include the --usb-list-all option
 
 The second version
-  --usb BAS:1,BFL:1,MMQ:0,ICA:0
+  --usb BAS:1,BFL:1,MMQ:0,ICA:0,KLN:0
 allows you to specify how many devices to choose based on each device
-driver cgminer has - there are currently 4 USB drivers: BAS, BFL, MMQ & ICA
+driver cgminer has - there are currently 5 USB drivers: BAS, BFL, MMQ.
+ICA & KLN
 N.B. you can only specify which device driver to limit, not the type of
 each device, e.g. with BAS:n you can limit how many BFL ASIC devices will
 be checked, but you cannot limit the number of each type of BFL ASIC

+ 1 - 1
api.c

@@ -29,7 +29,7 @@
 #include "miner.h"
 #include "util.h"
 
-#if defined(USE_BFLSC) || defined(USE_AVALON) || defined(USE_BITFURY)
+#if defined(USE_BFLSC) || defined(USE_AVALON) || defined(USE_BITFURY) || defined(USE_KLONDIKE)
 #define HAVE_AN_ASIC 1
 #endif
 

+ 24 - 0
cgminer.c

@@ -166,6 +166,9 @@ bool opt_worktime;
 #ifdef USE_AVALON
 char *opt_avalon_options = NULL;
 #endif
+#ifdef USE_KLONDIKE
+char *opt_klondike_options = NULL;
+#endif
 #ifdef USE_USBUTILS
 char *opt_usb_select = NULL;
 int opt_usbdump = -1;
@@ -1026,6 +1029,15 @@ static char *set_avalon_options(const char *arg)
 }
 #endif
 
+#ifdef USE_KLONDIKE
+static char *set_klondike_options(const char *arg)
+{
+	opt_set_charp(arg, &opt_klondike_options);
+
+	return NULL;
+}
+#endif
+
 #ifdef USE_USBUTILS
 static char *set_usb_select(const char *arg)
 {
@@ -1231,6 +1243,11 @@ static struct opt_table opt_config_table[] = {
 	OPT_WITH_ARG("--bitburner-voltage",
 		     opt_set_intval, NULL, &opt_bitburner_core_voltage,
 		     "Set BitBurner core voltage, in millivolts"),
+#endif
+#ifdef USE_KLONDIKE
+	OPT_WITH_ARG("--klondike-options",
+		     set_klondike_options, NULL, NULL,
+		     "Set klondike options clock:temp1:temp2:fan"),
 #endif
 	OPT_WITHOUT_ARG("--load-balance",
 		     set_loadbalance, &pool_strategy,
@@ -1572,6 +1589,9 @@ static char *opt_verusage_and_exit(const char *extra)
 #ifdef USE_ICARUS
 		"icarus "
 #endif
+#ifdef USE_KLONDIKE
+		"klondike "
+#endif
 #ifdef USE_MODMINER
 		"modminer "
 #endif
@@ -4430,6 +4450,10 @@ void write_config(FILE *fcfg)
 		fprintf(fcfg, ",\n\"icarus-options\" : \"%s\"", json_escape(opt_icarus_options));
 	if (opt_icarus_timing)
 		fprintf(fcfg, ",\n\"icarus-timing\" : \"%s\"", json_escape(opt_icarus_timing));
+#ifdef USE_KLONDIKE
+	if (opt_klondike_options)
+		fprintf(fcfg, ",\n\"klondike-options\" : \"%s\"", json_escape(opt_icarus_options));
+#endif
 #ifdef USE_USBUTILS
 	if (opt_usb_select)
 		fprintf(fcfg, ",\n\"usb\" : \"%s\"", json_escape(opt_usb_select));

+ 20 - 3
configure.ac

@@ -275,6 +275,17 @@ if test "x$avalon" = xyes; then
 fi
 AM_CONDITIONAL([HAS_AVALON], [test x$avalon = xyes])
 
+klondike="no"
+
+AC_ARG_ENABLE([klondike],
+	[AC_HELP_STRING([--enable-klondike],[Compile support for Klondike (default disabled)])],
+	[klondike=$enableval]
+	)
+if test "x$klondike" = xyes; then
+	AC_DEFINE([USE_KLONDIKE], [1], [Defined to 1 if Klondike support is wanted])
+fi
+AM_CONDITIONAL([HAS_KLONDIKE], [test x$klondike = xyes])
+
 modminer="no"
 
 AC_ARG_ENABLE([modminer],
@@ -321,7 +332,7 @@ else
 	])
 fi
 
-if test x$avalon$bitforce$bitfury$modminer$bflsc$icarus != xnononononono; then
+if test x$avalon$bitforce$bitfury$modminer$bflsc$icarus$klondike != xnonononononono; then
 	want_usbutils=true
 else
 	want_usbutils=false
@@ -493,14 +504,14 @@ if test "x$opencl" != xno; then
 
 	else
 		echo "  OpenCL...............: NOT FOUND. GPU mining support DISABLED"
-		if test "x$avalon$bitforce$bitfury$icarus$ztex$modminer$bflsc" = xnonononononono; then
+		if test "x$avalon$bitforce$bitfury$icarus$ztex$modminer$bflsc$klondike" = xnononononononono; then
 			AC_MSG_ERROR([No mining configured in])
 		fi
 		echo "  scrypt...............: Disabled (needs OpenCL)"
 	fi
 else
 	echo "  OpenCL...............: Detection overrided. GPU mining support DISABLED"
-	if test "x$avalon$bitforce$bitfury$icarus$ztex$modminer$bflsc" = xnonononononono; then
+	if test "x$avalon$bitforce$bitfury$icarus$ztex$modminer$bflsc$klondike" = xnononononononono; then
 		AC_MSG_ERROR([No mining configured in])
 	fi
 	echo "  scrypt...............: Disabled (needs OpenCL)"
@@ -547,6 +558,12 @@ else
 	echo "  Icarus.FPGAs.........: Disabled"
 fi
 
+if test "x$klondike" = xyes; then
+	echo "  Klondike.ASICs.......: Enabled"
+else
+	echo "  Klondike.ASICs.......: Disabled"
+fi
+
 if test "x$modminer" = xyes; then
 	echo "  ModMiner.FPGAs.......: Enabled"
 else

+ 719 - 0
driver-klondike.c

@@ -0,0 +1,719 @@
+/*
+ * Copyright 2013 Andrew Smith
+ * Copyright 2013 Con Kolivas
+ * Copyright 2013 Chris Savery
+ *
+ * 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 <float.h>
+#include <limits.h>
+#include <pthread.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <strings.h>
+#include <sys/time.h>
+#include <unistd.h>
+#include <math.h>
+
+#include "config.h"
+
+#ifdef WIN32
+#include <windows.h>
+#endif
+
+#include "compat.h"
+#include "miner.h"
+#include "usbutils.h"
+
+#define K1 "K1"
+#define K16 "K16"
+#define K64 "K64"
+
+#define MIDSTATE_BYTES 32
+#define MERKLE_OFFSET 64
+#define MERKLE_BYTES 12
+
+#define REPLY_SIZE		15	// adequate for all types of replies
+#define REPLY_BUFSIZE 		16	// reply + 1 byte to mark used
+#define MAX_REPLY_COUNT		32	// more unhandled replies than this will result in data loss
+#define REPLY_WAIT_TIME		100 	// poll interval for a cmd waiting it's reply
+#define CMD_REPLY_RETRIES	8	// how many retries for cmds
+#define MAX_WORK_COUNT		4	// for now, must be binary multiple and match firmware
+#define TACH_FACTOR		87890	// fan rpm divisor
+
+struct device_drv klondike_drv;
+
+typedef struct klondike_id {
+	uint8_t version;
+	uint8_t product[7];
+	uint32_t serial;
+} IDENTITY;
+
+typedef struct klondike_status {
+	uint8_t state;
+	uint8_t chipcount;
+	uint8_t slavecount;
+	uint8_t workqc;
+	uint8_t workid;
+	uint8_t temp;
+	uint8_t fanspeed;
+	uint8_t errorcount;
+	uint16_t hashcount;
+	uint16_t maxcount;
+	uint8_t noise;
+} WORKSTATUS;
+
+typedef struct _worktask {
+	uint16_t pad1;
+	uint8_t pad2;
+	uint8_t workid;
+	uint32_t midstate[8];
+	uint32_t merkle[3];
+} WORKTASK;
+
+typedef struct _workresult {
+	uint16_t pad;
+	uint8_t device;
+	uint8_t workid;
+	uint32_t nonce;
+} WORKRESULT;
+
+typedef struct klondike_cfg {
+	uint16_t hashclock;
+	uint8_t temptarget;
+	uint8_t tempcritical;
+	uint8_t fantarget;
+	uint8_t pad;
+} WORKCFG;
+
+typedef struct device_info {
+	uint32_t noncecount;
+	uint32_t nextworkid;
+	uint16_t lasthashcount;
+	uint64_t totalhashcount;
+	uint32_t rangesize;
+	uint32_t *chipstats;
+} DEVINFO;
+
+struct klondike_info {
+	bool shutdown;
+	pthread_rwlock_t stat_lock;
+	struct thr_info replies_thr;
+	WORKSTATUS *status;
+	DEVINFO *devinfo;
+	WORKCFG *cfg;
+	char *replies;
+	int nextreply;
+	int noncecount;
+	uint64_t hashcount;
+	uint64_t errorcount;
+	uint64_t noisecount;
+};
+
+IDENTITY KlondikeID;
+
+static double cvtKlnToC(uint8_t temp)
+{
+	double Rt, stein, celsius;
+
+	Rt = 1000.0 * 255.0 / (double)temp - 1000.0;
+
+	stein = log(Rt / 2200.0) / 3987.0;
+
+	stein += 1.0 / (double)(25.0 + 273.15);
+
+	celsius = (1.0 / stein) - 273.15;
+
+	return celsius;
+}
+
+static int cvtCToKln(double deg)
+{
+	double R = exp((1/(deg+273.15)-1/(273.15+25))*3987)*2200;
+	return 256*R/(R+1000);
+}
+
+static char *SendCmdGetReply(struct cgpu_info *klncgpu, char Cmd, int device, int datalen, void *data)
+{
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	char outbuf[64];
+	int retries = CMD_REPLY_RETRIES;
+	int chkreply = klninfo->nextreply;
+	int sent, err;
+
+	if (klncgpu->usbinfo.nodev)
+		return NULL;
+
+	outbuf[0] = Cmd;
+	outbuf[1] = device;
+	memcpy(outbuf+2, data, datalen);
+	err = usb_write(klncgpu, outbuf, 2+datalen, &sent, C_REQUESTRESULTS);
+	if (err < 0 || sent != 2+datalen) {
+		applog(LOG_ERR, "%s (%s) Cmd:%c Dev:%d, write failed (%d:%d)", klncgpu->drv->dname, klncgpu->device_path, Cmd, device, sent, err);
+	}
+	while (retries-- > 0 && klninfo->shutdown == false) {
+		cgsleep_ms(REPLY_WAIT_TIME);
+		while (*(klninfo->replies + chkreply*REPLY_BUFSIZE) != Cmd || *(klninfo->replies + chkreply*REPLY_BUFSIZE + 2) != device) {
+			if (++chkreply == MAX_REPLY_COUNT)
+				chkreply = 0;
+			if (chkreply == klninfo->nextreply)
+				break;
+		}
+		if (chkreply == klninfo->nextreply)
+			continue;
+		*(klninfo->replies + chkreply*REPLY_BUFSIZE) = '!';  // mark to prevent re-use
+		return klninfo->replies + chkreply*REPLY_BUFSIZE + 1;
+	}
+	return NULL;
+}
+
+static bool klondike_get_stats(struct cgpu_info *klncgpu)
+{
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	int slaves, dev;
+
+	if (klncgpu->usbinfo.nodev || klninfo->status == NULL)
+		return false;
+
+	applog(LOG_DEBUG, "Klondike getting status");
+	slaves = klninfo->status[0].slavecount;
+
+	// loop thru devices and get status for each
+	wr_lock(&(klninfo->stat_lock));
+	for (dev = 0; dev <= slaves; dev++) {
+		char *reply = SendCmdGetReply(klncgpu, 'S', dev, 0, NULL);
+		if (reply != NULL)
+			memcpy((void *)(&(klninfo->status[dev])), reply+2, sizeof(klninfo->status[dev]));
+	}
+	wr_unlock(&(klninfo->stat_lock));
+
+	// todo: detect slavecount change and realloc space
+
+	return true;
+}
+
+static bool klondike_init(struct cgpu_info *klncgpu)
+{
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	int slaves, dev;
+
+	char *reply = SendCmdGetReply(klncgpu, 'S', 0, 0, NULL);
+	if (reply == NULL)
+		return false;
+
+	slaves = ((WORKSTATUS *)(reply+2))->slavecount;
+	if (klninfo->status == NULL) {
+		applog(LOG_DEBUG, "Klondike initializing data");
+
+		// alloc space for status, devinfo and cfg for master and slaves
+		klninfo->status = calloc(slaves+1, sizeof(WORKSTATUS));
+		if (unlikely(!klninfo->status))
+			quit(1, "Failed to calloc status array in klondke_get_stats");
+		klninfo->devinfo = calloc(slaves+1, sizeof(DEVINFO));
+		if (unlikely(!klninfo->devinfo))
+			quit(1, "Failed to calloc devinfo array in klondke_get_stats");
+		klninfo->cfg = calloc(slaves+1, sizeof(WORKCFG));
+		if (unlikely(!klninfo->cfg))
+			quit(1, "Failed to calloc cfg array in klondke_get_stats");
+	}
+
+	WORKCFG cfgset = { 0,0,0,0,0 }; // zero init triggers read back only
+	double temp1, temp2;
+	int size = 2;
+
+	if (opt_klondike_options != NULL) {  // boundaries are checked by device, with valid values returned
+		sscanf(opt_klondike_options, "%hu:%lf:%lf:%hhu", &cfgset.hashclock, &temp1, &temp2, &cfgset.fantarget);
+		cfgset.temptarget = cvtCToKln(temp1);
+		cfgset.tempcritical = cvtCToKln(temp2);
+		cfgset.fantarget = (int)255*cfgset.fantarget/100;
+		size = sizeof(cfgset);
+	}
+
+	for (dev = 0; dev <= slaves; dev++) {
+		char *reply = SendCmdGetReply(klncgpu, 'C', dev, size, &cfgset);
+		if (reply != NULL) {
+			klninfo->cfg[dev] = *(WORKCFG *)(reply+2);
+			applog(LOG_NOTICE, "Klondike config (%d: Clk: %d, T:%.0lf, C:%.0lf, F:%d)",
+				dev, klninfo->cfg[dev].hashclock,
+				cvtKlnToC(klninfo->cfg[dev].temptarget),
+				cvtKlnToC(klninfo->cfg[dev].tempcritical),
+				(int)100*klninfo->cfg[dev].fantarget/256);
+		}
+	}
+	klondike_get_stats(klncgpu);
+	for (dev = 0; dev <= slaves; dev++) {
+		klninfo->devinfo[dev].rangesize = ((uint64_t)1<<32) / klninfo->status[dev].chipcount;
+		klninfo->devinfo[dev].chipstats = calloc(klninfo->status[dev].chipcount*2 , sizeof(uint32_t));
+	}
+
+	SendCmdGetReply(klncgpu, 'E', 0, 1, "1");
+
+	return true;
+}
+
+static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
+{
+	struct cgpu_info *klncgpu = usb_alloc_cgpu(&klondike_drv, 1);
+	struct klondike_info *klninfo = NULL;
+
+	if (unlikely(!klncgpu))
+		quit(1, "Failed to calloc klncgpu in klondike_detect_one");
+
+	klninfo = calloc(1, sizeof(*klninfo));
+	if (unlikely(!klninfo))
+		quit(1, "Failed to calloc klninfo in klondke_detect_one");
+	klncgpu->device_data = (FILE *)klninfo;
+
+	klninfo->replies = calloc(MAX_REPLY_COUNT, REPLY_BUFSIZE);
+	if (unlikely(!klninfo->replies))
+		quit(1, "Failed to calloc replies buffer in klondke_detect_one");
+	klninfo->nextreply = 0;
+
+	if (usb_init(klncgpu, dev, found)) {
+		int attempts = 0;
+		while (attempts++ < 3) {
+			char devpath[20], reply[REPLY_SIZE];
+			int sent, recd, err;
+
+			sprintf(devpath, "%d:%d", (int)(klncgpu->usbinfo.bus_number), (int)(klncgpu->usbinfo.device_address));
+			err = usb_write(klncgpu, "I", 2, &sent, C_REQUESTRESULTS);
+			if (err < 0 || sent != 2) {
+				applog(LOG_ERR, "%s (%s) detect write failed (%d:%d)", klncgpu->drv->dname, devpath, sent, err);
+			}
+			cgsleep_ms(REPLY_WAIT_TIME*10);
+			err = usb_read(klncgpu, reply, REPLY_SIZE, &recd, C_GETRESULTS);
+			if (err < 0) {
+				applog(LOG_ERR, "%s (%s) detect read failed (%d:%d)", klncgpu->drv->dname, devpath, recd, err);
+			} else if (recd < 1) {
+				applog(LOG_ERR, "%s (%s) detect empty reply (%d)",	klncgpu->drv->dname, devpath, recd);
+			} else if (reply[0] == 'I' && reply[1] == 0) {
+
+				applog(LOG_DEBUG, "%s (%s) detect successful", klncgpu->drv->dname, devpath);
+				KlondikeID = *(IDENTITY *)(&reply[2]);
+				klncgpu->device_path = strdup(devpath);
+				update_usb_stats(klncgpu);
+				if (!add_cgpu(klncgpu))
+					break;
+				applog(LOG_DEBUG, "Klondike cgpu added");
+				return true;
+			}
+		}
+		usb_uninit(klncgpu);
+	}
+	free(klninfo->replies);
+	free(klncgpu);
+	return false;
+}
+
+static void klondike_detect(bool __maybe_unused hotplug)
+{
+	usb_detect(&klondike_drv, klondike_detect_one);
+}
+
+static void klondike_identify(__maybe_unused struct cgpu_info *klncgpu)
+{
+	//SendCmdGetReply(klncgpu, 'I', 0, 0, NULL);
+}
+
+static void klondike_check_nonce(struct cgpu_info *klncgpu, WORKRESULT *result)
+{
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	struct work *work, *tmp;
+
+	applog(LOG_DEBUG, "Klondike FOUND NONCE (%02x:%08x)", result->workid, result->nonce);
+
+	HASH_ITER(hh, klncgpu->queued_work, work, tmp) {
+		if (work->queued && (work->subid == (result->device*256 + result->workid))) {
+
+			wr_lock(&(klninfo->stat_lock));
+			klninfo->devinfo[result->device].noncecount++;
+			klninfo->noncecount++;
+			wr_unlock(&(klninfo->stat_lock));
+
+			result->nonce = le32toh(result->nonce - 0xC0);
+			applog(LOG_DEBUG, "Klondike SUBMIT NONCE (%02x:%08x)", result->workid, result->nonce);
+			bool ok = submit_nonce(klncgpu->thr[0], work, result->nonce);
+
+			applog(LOG_DEBUG, "Klondike chip stats %d, %08x, %d, %d", result->device, result->nonce, klninfo->devinfo[result->device].rangesize, klninfo->status[result->device].chipcount);
+			klninfo->devinfo[result->device].chipstats[(result->nonce / klninfo->devinfo[result->device].rangesize) + (ok ? 0 : klninfo->status[result->device].chipcount)]++;
+			return;
+		}
+	}
+
+	applog(LOG_ERR, "%s%i:%d unknown work (%02x:%08x) - ignored",
+		klncgpu->drv->name, klncgpu->device_id, result->device, result->workid, result->nonce);
+	//inc_hw_errors(klncgpu->thr[0]);
+}
+
+// Change this to LOG_WARNING if you wish to always see the replies
+#define READ_DEBUG LOG_DEBUG
+
+// thread to keep looking for replies
+static void *klondike_get_replies(void *userdata)
+{
+	struct cgpu_info *klncgpu = (struct cgpu_info *)userdata;
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	struct klondike_status *ks;
+	struct _workresult *wr;
+	struct klondike_cfg *kc;
+	struct klondike_id *ki;
+	char *replybuf;
+	int err, recd;
+
+	applog(LOG_DEBUG, "Klondike listening for replies");
+
+	while (klninfo->shutdown == false) {
+		if (klncgpu->usbinfo.nodev)
+			return NULL;
+
+		replybuf = klninfo->replies + klninfo->nextreply * REPLY_BUFSIZE;
+		replybuf[0] = 0;
+
+		err = usb_read(klncgpu, replybuf+1, REPLY_SIZE, &recd, C_GETRESULTS);
+		if (!err && recd == REPLY_SIZE) {
+			if (opt_log_level <= READ_DEBUG) {
+				char *hexdata = bin2hex((unsigned char *)(replybuf+1), recd);
+				applog(READ_DEBUG, "%s (%s) reply [%s:%s]", klncgpu->drv->dname, klncgpu->device_path, replybuf+1, hexdata);
+				free(hexdata);
+			}
+			if (++klninfo->nextreply == MAX_REPLY_COUNT)
+				klninfo->nextreply = 0;
+
+			replybuf[0] = replybuf[1];
+			switch (replybuf[0]) {
+				case '=':
+					wr = (struct _workresult *)(replybuf+1);
+					klondike_check_nonce(klncgpu, (WORKRESULT *)replybuf);
+					applog(READ_DEBUG,
+						"%s (%s) reply: work [%c] device=%d workid=%d"
+						" nonce=0x%08x",
+						klncgpu->drv->dname, klncgpu->device_path,
+						*(replybuf+1),
+						(int)(wr->device),
+						(int)(wr->workid),
+						(unsigned int)(wr->nonce));
+					break;
+				case 'S':
+				case 'W':
+				case 'A':
+				case 'E':
+					ks = (struct klondike_status *)(replybuf+1);
+					wr_lock(&(klninfo->stat_lock));
+					klninfo->errorcount += ks->errorcount;
+					klninfo->noisecount += ks->noise;
+					wr_unlock(&(klninfo->stat_lock));
+					applog(READ_DEBUG,
+						"%s (%s) reply: status [%c] chips=%d slaves=%d"
+						" workcq=%d workid=%d temp=%d fan=%d errors=%d"
+						" hashes=%d max=%d noise=%d",
+						klncgpu->drv->dname, klncgpu->device_path,
+						*(replybuf+1),
+						(int)(ks->chipcount),
+						(int)(ks->slavecount),
+						(int)(ks->workqc),
+						(int)(ks->workid),
+						(int)(ks->temp),
+						(int)(ks->fanspeed),
+						(int)(ks->errorcount),
+						(int)(ks->hashcount),
+						(int)(ks->maxcount),
+						(int)(ks->noise));
+					break;
+				case 'C':
+					kc = (struct klondike_cfg *)(replybuf+2);
+					applog(READ_DEBUG,
+						"%s (%s) reply: config [%c] clock=%d temptarget=%d"
+						" tempcrit=%d fan=%d",
+						klncgpu->drv->dname, klncgpu->device_path,
+						*(replybuf+1),
+						(int)(kc->hashclock),
+						(int)(kc->temptarget),
+						(int)(kc->tempcritical),
+						(int)(kc->fantarget));
+					break;
+				case 'I':
+					ki = (struct klondike_id *)(replybuf+2);
+					applog(READ_DEBUG,
+						"%s (%s) reply: info [%c] version=0x%02x prod=%.7s"
+						" serial=0x%08x",
+						klncgpu->drv->dname, klncgpu->device_path,
+						*(replybuf+1),
+						(int)(ki->version),
+						ki->product,
+						(unsigned int)(ki->serial));
+					break;
+				default:
+					break;
+			}
+		}
+	}
+	return NULL;
+}
+
+static void klondike_flush_work(struct cgpu_info *klncgpu)
+{
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	int dev;
+
+	applog(LOG_DEBUG, "Klondike flushing work");
+	for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
+		char *reply = SendCmdGetReply(klncgpu, 'A', dev, 0, NULL);
+		if (reply != NULL) {
+			wr_lock(&(klninfo->stat_lock));
+			klninfo->status[dev] = *(WORKSTATUS *)(reply+2);
+			wr_unlock(&(klninfo->stat_lock));
+		}
+	}
+}
+
+static bool klondike_thread_prepare(struct thr_info *thr)
+{
+	struct cgpu_info *klncgpu = thr->cgpu;
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+
+	if (thr_info_create(&(klninfo->replies_thr), NULL, klondike_get_replies, (void *)klncgpu)) {
+		applog(LOG_ERR, "%s%i: thread create failed", klncgpu->drv->name, klncgpu->device_id);
+		return false;
+	}
+	pthread_detach(klninfo->replies_thr.pth);
+
+	// let the listening get started
+	cgsleep_ms(100);
+
+	return klondike_init(klncgpu);
+}
+
+static bool klondike_thread_init(struct thr_info *thr)
+{
+	struct cgpu_info *klncgpu = thr->cgpu;
+
+	if (klncgpu->usbinfo.nodev)
+		return false;
+
+	klondike_flush_work(klncgpu);
+
+	return true;
+}
+
+static void klondike_shutdown(struct thr_info *thr)
+{
+	struct cgpu_info *klncgpu = thr->cgpu;
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	int dev;
+
+	applog(LOG_DEBUG, "Klondike shutting down work");
+	for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
+		SendCmdGetReply(klncgpu, 'E', dev, 1, "0");
+	}
+	klncgpu->shutdown = klninfo->shutdown = true;
+}
+
+static void klondike_thread_enable(struct thr_info *thr)
+{
+	struct cgpu_info *klncgpu = thr->cgpu;
+
+	if (klncgpu->usbinfo.nodev)
+		return;
+
+	//SendCmdGetReply(klncgpu, 'E', 0, 1, "0");
+
+}
+
+static bool klondike_send_work(struct cgpu_info *klncgpu, int dev, struct work *work)
+{
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	struct work *tmp;
+	WORKTASK data;
+
+	if (klncgpu->usbinfo.nodev)
+		return false;
+
+	memcpy(data.midstate, work->midstate, MIDSTATE_BYTES);
+	memcpy(data.merkle, work->data + MERKLE_OFFSET, MERKLE_BYTES);
+	data.workid = (uint8_t)(klninfo->devinfo[dev].nextworkid++ & 0xFF);
+	work->subid = dev*256 + data.workid;
+
+	if (opt_log_level <= LOG_DEBUG) {
+		char *hexdata = bin2hex(&data.workid, sizeof(data)-3);
+		applog(LOG_DEBUG, "WORKDATA: %s", hexdata);
+		free(hexdata);
+	}
+
+	applog(LOG_DEBUG, "Klondike sending work (%d:%02x)", dev, data.workid);
+	char *reply = SendCmdGetReply(klncgpu, 'W', dev, sizeof(data)-3, &data.workid);
+	if (reply != NULL) {
+		wr_lock(&(klninfo->stat_lock));
+		klninfo->status[dev] = *(WORKSTATUS *)(reply+2);
+		wr_unlock(&(klninfo->stat_lock));
+
+		// remove old work
+		HASH_ITER(hh, klncgpu->queued_work, work, tmp) {
+		if (work->queued && (work->subid == (int)(dev*256 + ((klninfo->devinfo[dev].nextworkid-2*MAX_WORK_COUNT) & 0xFF))))
+			work_completed(klncgpu, work);
+		}
+		return true;
+	}
+	return false;
+}
+
+static bool klondike_queue_full(struct cgpu_info *klncgpu)
+{
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	struct work *work = NULL;
+	int dev, queued;
+
+	for (queued = 0; queued < MAX_WORK_COUNT-1; queued++)
+		for (dev = 0; dev <= klninfo->status->slavecount; dev++)
+			if (klninfo->status[dev].workqc <= queued) {
+				if (!work)
+					work = get_queued(klncgpu);
+				if (unlikely(!work))
+					return false;
+				if (klondike_send_work(klncgpu, dev, work)) {
+					work = NULL;
+					break;
+				}
+			}
+
+	return true;
+}
+
+static int64_t klondike_scanwork(struct thr_info *thr)
+{
+	struct cgpu_info *klncgpu = thr->cgpu;
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	int64_t newhashcount = 0;
+	int dev;
+
+	if (klncgpu->usbinfo.nodev)
+		return -1;
+
+	restart_wait(thr, 200);
+	if (klninfo->status != NULL) {
+		rd_lock(&(klninfo->stat_lock));
+		for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
+			uint64_t newhashdev = 0;
+			if (klninfo->devinfo[dev].lasthashcount > klninfo->status[dev].hashcount) // todo: chg this to check workid for wrapped instead
+				newhashdev += klninfo->status[dev].maxcount; // hash counter wrapped
+			newhashdev += klninfo->status[dev].hashcount - klninfo->devinfo[dev].lasthashcount;
+			klninfo->devinfo[dev].lasthashcount = klninfo->status[dev].hashcount;
+			if (klninfo->status[dev].maxcount != 0)
+				klninfo->hashcount += (newhashdev << 32) / klninfo->status[dev].maxcount;
+
+			// todo: check stats for critical conditions
+		}
+		newhashcount += 0xffffffffull * (uint64_t)klninfo->noncecount;
+		klninfo->noncecount = 0;
+		rd_unlock(&(klninfo->stat_lock));
+	}
+	return newhashcount;
+}
+
+
+static void get_klondike_statline_before(char *buf, size_t siz, struct cgpu_info *klncgpu)
+{
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	uint8_t temp = 0xFF;
+	uint16_t fan = 0;
+	int dev;
+
+	if (klninfo->status == NULL)
+		return;
+
+	rd_lock(&(klninfo->stat_lock));
+	for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
+		if (klninfo->status[dev].temp < temp)
+			temp = klninfo->status[dev].temp;
+		fan += klninfo->cfg[dev].fantarget;
+	}
+	fan /= klninfo->status->slavecount+1;
+	rd_unlock(&(klninfo->stat_lock));
+
+	tailsprintf(buf, siz, "     %3.0fC %3d%% | ", cvtKlnToC(temp), fan*100/255);
+}
+
+static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
+{
+	struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
+	struct api_data *root = NULL;
+	char buf[32];
+	int dev;
+
+	if (klninfo->status == NULL)
+		return NULL;
+
+	rd_lock(&(klninfo->stat_lock));
+	for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
+
+		float fTemp = cvtKlnToC(klninfo->status[dev].temp);
+		sprintf(buf, "Temp %d", dev);
+		root = api_add_temp(root, buf, &fTemp, true);
+
+		double dClk = (double)klninfo->cfg[dev].hashclock;
+		sprintf(buf, "Clock %d", dev);
+		root = api_add_freq(root, buf, &dClk, true);
+
+		unsigned int iFan = (unsigned int)100 * klninfo->cfg[dev].fantarget / 255;
+		sprintf(buf, "Fan Percent %d", dev);
+		root = api_add_int(root, buf, (int *)(&iFan), true);
+
+		iFan = 0;
+		if (klninfo->status[dev].fanspeed > 0)
+			iFan = (unsigned int)TACH_FACTOR / klninfo->status[dev].fanspeed;
+		sprintf(buf, "Fan RPM %d", dev);
+		root = api_add_int(root, buf, (int *)(&iFan), true);
+
+		if (klninfo->devinfo[dev].chipstats != NULL) {
+			char data[2048];
+			char one[32];
+			int n;
+
+			sprintf(buf, "Nonces / Chip %d", dev);
+			data[0] = '\0';
+			for (n = 0; n < klninfo->status[dev].chipcount; n++) {
+				snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n]);
+				strcat(data, one);
+			}
+			root = api_add_string(root, buf, data, true);
+
+			sprintf(buf, "Errors / Chip %d", dev);
+			data[0] = '\0';
+			for (n = 0; n < klninfo->status[dev].chipcount; n++) {
+				snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n + klninfo->status[dev].chipcount]);
+				strcat(data, one);
+			}
+			root = api_add_string(root, buf, data, true);
+		}
+	}
+
+	root = api_add_uint64(root, "Hash Count", &(klninfo->hashcount), true);
+	root = api_add_uint64(root, "Error Count", &(klninfo->errorcount), true);
+	root = api_add_uint64(root, "Noise Count", &(klninfo->noisecount), true);
+
+	rd_unlock(&(klninfo->stat_lock));
+
+	return root;
+}
+
+struct device_drv klondike_drv = {
+	.drv_id = DRIVER_klondike,
+	.dname = "Klondike",
+	.name = "KLN",
+	.drv_detect = klondike_detect,
+	.get_api_stats = klondike_api_stats,
+	.get_statline_before = get_klondike_statline_before,
+	.get_stats = klondike_get_stats,
+	.identify_device = klondike_identify,
+	.thread_prepare = klondike_thread_prepare,
+	.thread_init = klondike_thread_init,
+	.hash_work = hash_queued_work,
+	.scanwork = klondike_scanwork,
+	.queue_full = klondike_queue_full,
+	.flush_work = klondike_flush_work,
+	.thread_shutdown = klondike_shutdown,
+	.thread_enable = klondike_thread_enable
+};

+ 5 - 1
miner.h

@@ -243,7 +243,8 @@ static inline int fsync (int fd)
 #define ASIC_PARSE_COMMANDS(DRIVER_ADD_COMMAND) \
 	DRIVER_ADD_COMMAND(bflsc) \
 	DRIVER_ADD_COMMAND(bitfury) \
-	DRIVER_ADD_COMMAND(avalon)
+	DRIVER_ADD_COMMAND(avalon) \
+	DRIVER_ADD_COMMAND(klondike)
 
 #define DRIVER_PARSE_COMMANDS(DRIVER_ADD_COMMAND) \
 	DRIVER_ADD_COMMAND(opencl) \
@@ -943,6 +944,9 @@ extern bool opt_worktime;
 #ifdef USE_AVALON
 extern char *opt_avalon_options;
 #endif
+#ifdef USE_KLONDIKE
+extern char *opt_klondike_options;
+#endif
 #ifdef USE_USBUTILS
 extern char *opt_usb_select;
 extern int opt_usbdump;

+ 26 - 0
usbutils.c

@@ -50,6 +50,7 @@
 #define BITFURY_TIMEOUT_MS 999
 #define MODMINER_TIMEOUT_MS 999
 #define AVALON_TIMEOUT_MS 999
+#define KLONDIKE_TIMEOUT_MS 999
 #define ICARUS_TIMEOUT_MS 999
 #else
 #define BFLSC_TIMEOUT_MS 300
@@ -57,6 +58,7 @@
 #define BITFURY_TIMEOUT_MS 100
 #define MODMINER_TIMEOUT_MS 100
 #define AVALON_TIMEOUT_MS 200
+#define KLONDIKE_TIMEOUT_MS 200
 #define ICARUS_TIMEOUT_MS 200
 #endif
 
@@ -139,6 +141,17 @@ static struct usb_intinfo ava_ints[] = {
 };
 #endif
 
+#ifdef USE_KLONDIKE
+static struct usb_epinfo kln_epinfos[] = {
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(1), 0, 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(1), 0, 0, 0 }
+};
+
+static struct usb_intinfo kln_ints[] = {
+	USB_EPS(0, kln_epinfos)
+};
+#endif
+
 #ifdef USE_ICARUS
 static struct usb_epinfo ica_epinfos[] = {
 	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(3), 0, 0, 0 },
@@ -292,6 +305,18 @@ static struct usb_find_devices find_dev[] = {
 		.latency = 10,
 		INTINFO(ava_ints) },
 #endif
+#ifdef USE_KLONDIKE
+	{
+		.drv = DRIVER_klondike,
+		.name = "KLN",
+		.ident = IDENT_KLN,
+		.idVendor = 0x04D8,
+		.idProduct = 0xF60A,
+		.config = 1,
+		.timeout = KLONDIKE_TIMEOUT_MS,
+		.latency = 10,
+		INTINFO(kln_ints) },
+#endif
 #ifdef USE_ICARUS
 	{
 		.drv = DRIVER_icarus,
@@ -3103,6 +3128,7 @@ void usb_cleanup()
 			case DRIVER_modminer:
 			case DRIVER_icarus:
 			case DRIVER_avalon:
+			case DRIVER_klondike:
 				mutex_lock(cgpu->usbinfo.devlock);
 				release_cgpu(cgpu);
 				mutex_unlock(cgpu->usbinfo.devlock);

+ 1 - 0
usbutils.h

@@ -144,6 +144,7 @@ enum sub_ident {
 	IDENT_MMQ,
 	IDENT_AVA,
 	IDENT_BTB,
+	IDENT_KLN,
 	IDENT_ICA,
 	IDENT_AMU,
 	IDENT_BLT,