/ driver-bitforce.c
driver-bitforce.c
  1  /*
  2   * Copyright 2012-2013 Andrew Smith
  3   * Copyright 2012 Luke Dashjr
  4   * Copyright 2012 Con Kolivas
  5   *
  6   * This program is free software; you can redistribute it and/or modify it
  7   * under the terms of the GNU General Public License as published by the Free
  8   * Software Foundation; either version 3 of the License, or (at your option)
  9   * any later version.  See COPYING for more details.
 10   */
 11  
 12  #include "config.h"
 13  
 14  #include <limits.h>
 15  #include <pthread.h>
 16  #include <stdint.h>
 17  #include <stdio.h>
 18  #include <strings.h>
 19  #include <sys/time.h>
 20  #include <unistd.h>
 21  
 22  #include "compat.h"
 23  #include "miner.h"
 24  #include "usbutils.h"
 25  #include "util.h"
 26  
 27  #define BITFORCE_IDENTIFY "ZGX"
 28  #define BITFORCE_IDENTIFY_LEN (sizeof(BITFORCE_IDENTIFY)-1)
 29  #define BITFORCE_FLASH "ZMX"
 30  #define BITFORCE_FLASH_LEN (sizeof(BITFORCE_FLASH)-1)
 31  #define BITFORCE_TEMPERATURE "ZLX"
 32  #define BITFORCE_TEMPERATURE_LEN (sizeof(BITFORCE_TEMPERATURE)-1)
 33  #define BITFORCE_SENDRANGE "ZPX"
 34  #define BITFORCE_SENDRANGE_LEN (sizeof(BITFORCE_SENDRANGE)-1)
 35  #define BITFORCE_SENDWORK "ZDX"
 36  #define BITFORCE_SENDWORK_LEN (sizeof(BITFORCE_SENDWORK)-1)
 37  #define BITFORCE_WORKSTATUS "ZFX"
 38  #define BITFORCE_WORKSTATUS_LEN (sizeof(BITFORCE_WORKSTATUS)-1)
 39  
 40  // Either of Nonce or No-nonce start with:
 41  #define BITFORCE_EITHER "N"
 42  #define BITFORCE_EITHER_LEN 1
 43  #define BITFORCE_NONCE "NONCE-FOUND"
 44  #define BITFORCE_NONCE_LEN (sizeof(BITFORCE_NONCE)-1)
 45  #define BITFORCE_NO_NONCE "NO-NONCE"
 46  #define BITFORCE_NO_NONCE_MATCH 3
 47  #define BITFORCE_IDLE "IDLE"
 48  #define BITFORCE_IDLE_MATCH 1
 49  
 50  #define BITFORCE_SLEEP_MS 500
 51  #define BITFORCE_TIMEOUT_S 7
 52  #define BITFORCE_TIMEOUT_MS (BITFORCE_TIMEOUT_S * 1000)
 53  #define BITFORCE_LONG_TIMEOUT_S 30
 54  #define BITFORCE_LONG_TIMEOUT_MS (BITFORCE_LONG_TIMEOUT_S * 1000)
 55  #define BITFORCE_CHECK_INTERVAL_MS 10
 56  #define WORK_CHECK_INTERVAL_MS 50
 57  #define MAX_START_DELAY_MS 100
 58  #define tv_to_ms(tval) (tval.tv_sec * 1000 + tval.tv_usec / 1000)
 59  #define TIME_AVG_CONSTANT 8
 60  
 61  #define KNAME_WORK  "full work"
 62  #define KNAME_RANGE "nonce range"
 63  
 64  #define BITFORCE_BUFSIZ (0x200)
 65  
 66  // If initialisation fails the first time,
 67  // sleep this amount (ms) and try again
 68  #define REINIT_TIME_FIRST_MS 100
 69  // Max ms per sleep
 70  #define REINIT_TIME_MAX_MS 800
 71  // Keep trying up to this many us
 72  #define REINIT_TIME_MAX 3000000
 73  
 74  static const char *blank = "";
 75  
 76  static void bitforce_initialise(struct cgpu_info *bitforce, bool lock)
 77  {
 78  	int err, interface;
 79  
 80  	if (lock)
 81  		mutex_lock(&bitforce->device_mutex);
 82  
 83  	if (bitforce->usbinfo.nodev)
 84  		goto failed;
 85  
 86  	interface = usb_interface(bitforce);
 87  	// Reset
 88  	err = usb_transfer(bitforce, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
 89  				FTDI_VALUE_RESET, interface, C_RESET);
 90  	if (opt_debug)
 91  		applog(LOG_DEBUG, "%s%i: reset got err %d",
 92  			bitforce->drv->name, bitforce->device_id, err);
 93  
 94  	if (bitforce->usbinfo.nodev)
 95  		goto failed;
 96  
 97  	// Set data control
 98  	err = usb_transfer(bitforce, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
 99  				FTDI_VALUE_DATA_BFL, interface, C_SETDATA);
100  	if (opt_debug)
101  		applog(LOG_DEBUG, "%s%i: setdata got err %d",
102  			bitforce->drv->name, bitforce->device_id, err);
103  
104  	if (bitforce->usbinfo.nodev)
105  		goto failed;
106  
107  	// Set the baud
108  	err = usb_transfer(bitforce, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_BFL,
109  				(FTDI_INDEX_BAUD_BFL & 0xff00) | interface,
110  				C_SETBAUD);
111  	if (opt_debug)
112  		applog(LOG_DEBUG, "%s%i: setbaud got err %d",
113  			bitforce->drv->name, bitforce->device_id, err);
114  
115  	if (bitforce->usbinfo.nodev)
116  		goto failed;
117  
118  	// Set Flow Control
119  	err = usb_transfer(bitforce, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
120  				FTDI_VALUE_FLOW, interface, C_SETFLOW);
121  	if (opt_debug)
122  		applog(LOG_DEBUG, "%s%i: setflowctrl got err %d",
123  			bitforce->drv->name, bitforce->device_id, err);
124  
125  	if (bitforce->usbinfo.nodev)
126  		goto failed;
127  
128  	// Set Modem Control
129  	err = usb_transfer(bitforce, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
130  				FTDI_VALUE_MODEM, interface, C_SETMODEM);
131  	if (opt_debug)
132  		applog(LOG_DEBUG, "%s%i: setmodemctrl got err %d",
133  			bitforce->drv->name, bitforce->device_id, err);
134  
135  	if (bitforce->usbinfo.nodev)
136  		goto failed;
137  
138  	// Clear any sent data
139  	err = usb_transfer(bitforce, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
140  				FTDI_VALUE_PURGE_TX, interface, C_PURGETX);
141  	if (opt_debug)
142  		applog(LOG_DEBUG, "%s%i: purgetx got err %d",
143  			bitforce->drv->name, bitforce->device_id, err);
144  
145  	if (bitforce->usbinfo.nodev)
146  		goto failed;
147  
148  	// Clear any received data
149  	err = usb_transfer(bitforce, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
150  				FTDI_VALUE_PURGE_RX, interface, C_PURGERX);
151  	if (opt_debug)
152  		applog(LOG_DEBUG, "%s%i: purgerx got err %d",
153  			bitforce->drv->name, bitforce->device_id, err);
154  
155  failed:
156  
157  	if (lock)
158  		mutex_unlock(&bitforce->device_mutex);
159  }
160  
161  static struct cgpu_info *bitforce_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
162  {
163  	char buf[BITFORCE_BUFSIZ+1];
164  	int err, amount;
165  	char *s;
166  	struct timeval init_start, init_now;
167  	int init_sleep, init_count;
168  	bool ident_first;
169  
170  	struct cgpu_info *bitforce = usb_alloc_cgpu(&bitforce_drv, 1);
171  
172  	if (!usb_init(bitforce, dev, found))
173  		goto shin;
174  
175  	// Allow 2 complete attempts if the 1st time returns an unrecognised reply
176  	ident_first = true;
177  retry:
178  	init_count = 0;
179  	init_sleep = REINIT_TIME_FIRST_MS;
180  	cgtime(&init_start);
181  reinit:
182  	bitforce_initialise(bitforce, false);
183  	if ((err = usb_write(bitforce, BITFORCE_IDENTIFY, BITFORCE_IDENTIFY_LEN, &amount, C_REQUESTIDENTIFY)) < 0 || amount != BITFORCE_IDENTIFY_LEN) {
184  		applog(LOG_ERR, "%s detect (%s) send identify request failed (%d:%d)",
185  			bitforce->drv->dname, bitforce->device_path, amount, err);
186  		goto unshin;
187  	}
188  
189  	if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY)) < 0 || amount < 1) {
190  		init_count++;
191  		cgtime(&init_now);
192  		if (us_tdiff(&init_now, &init_start) <= REINIT_TIME_MAX) {
193  			if (init_count == 2) {
194  				applog(LOG_WARNING, "%s detect (%s) 2nd init failed (%d:%d) - retrying",
195  					bitforce->drv->dname, bitforce->device_path, amount, err);
196  			}
197  			cgsleep_ms(init_sleep);
198  			if ((init_sleep * 2) <= REINIT_TIME_MAX_MS)
199  				init_sleep *= 2;
200  			goto reinit;
201  		}
202  
203  		if (init_count > 0)
204  			applog(LOG_WARNING, "%s detect (%s) init failed %d times %.2fs",
205  				bitforce->drv->dname, bitforce->device_path, init_count, tdiff(&init_now, &init_start));
206  
207  		if (err < 0) {
208  			applog(LOG_ERR, "%s detect (%s) error identify reply (%d:%d)",
209  				bitforce->drv->dname, bitforce->device_path, amount, err);
210  		} else {
211  			applog(LOG_ERR, "%s detect (%s) empty identify reply (%d)",
212  				bitforce->drv->dname, bitforce->device_path, amount);
213  		}
214  
215  		goto unshin;
216  	}
217  	buf[amount] = '\0';
218  
219  	if (unlikely(!strstr(buf, "SHA256"))) {
220  		if (ident_first) {
221  			applog(LOG_WARNING, "%s detect (%s) didn't recognise '%s' trying again ...",
222  				bitforce->drv->dname, bitforce->device_path, buf);
223  			ident_first = false;
224  			goto retry;
225  		}
226  		applog(LOG_ERR, "%s detect (%s) didn't recognise '%s' on 2nd attempt",
227  			bitforce->drv->dname, bitforce->device_path, buf);
228  		goto unshin;
229  	}
230  
231  	if (strstr(buf, "SHA256 SC")) {
232  #ifdef USE_BFLSC
233  		applog(LOG_DEBUG, "SC device detected, will defer to BFLSC driver");
234  #else
235  		applog(LOG_WARNING, "SC device detected but no BFLSC support compiled in!");
236  #endif
237  		goto unshin;
238  	}
239  
240  	if (likely((!memcmp(buf, ">>>ID: ", 7)) && (s = strstr(buf + 3, ">>>")))) {
241  		s[0] = '\0';
242  		bitforce->name = strdup(buf + 7);
243  	} else {
244  		bitforce->name = (char *)blank;
245  	}
246  
247  	// We have a real BitForce!
248  	applog(LOG_DEBUG, "%s (%s) identified as: '%s'",
249  		bitforce->drv->dname, bitforce->device_path, bitforce->name);
250  
251  	/* Initially enable support for nonce range and disable it later if it
252  	 * fails */
253  	if (opt_bfl_noncerange) {
254  		bitforce->nonce_range = true;
255  		bitforce->sleep_ms = BITFORCE_SLEEP_MS;
256  		bitforce->kname = KNAME_RANGE;
257  	} else {
258  		bitforce->sleep_ms = BITFORCE_SLEEP_MS * 5;
259  		bitforce->kname = KNAME_WORK;
260  	}
261  
262  	if (!add_cgpu(bitforce))
263  		goto unshin;
264  
265  	update_usb_stats(bitforce);
266  
267  	mutex_init(&bitforce->device_mutex);
268  
269  	return bitforce;
270  
271  unshin:
272  
273  	usb_uninit(bitforce);
274  
275  shin:
276  
277  	if (bitforce->name != blank) {
278  		free(bitforce->name);
279  		bitforce->name = NULL;
280  	}
281  
282  	bitforce = usb_free_cgpu(bitforce);
283  
284  	return NULL;
285  }
286  
287  static void bitforce_detect(bool __maybe_unused hotplug)
288  {
289  	usb_detect(&bitforce_drv, bitforce_detect_one);
290  }
291  
292  static void get_bitforce_statline_before(char *buf, size_t bufsiz, struct cgpu_info *bitforce)
293  {
294  	float gt = bitforce->temp;
295  
296  	if (gt > 0)
297  		tailsprintf(buf, bufsiz, "%5.1fC", gt);
298  }
299  
300  static bool bitforce_thread_prepare(__maybe_unused struct thr_info *thr)
301  {
302  //	struct cgpu_info *bitforce = thr->cgpu;
303  
304  	return true;
305  }
306  
307  static void bitforce_flash_led(struct cgpu_info *bitforce)
308  {
309  	int err, amount;
310  
311  	/* Do not try to flash the led if we're polling for a result to
312  	 * minimise the chance of interleaved results */
313  	if (bitforce->polling)
314  		return;
315  
316  	/* It is not critical flashing the led so don't get stuck if we
317  	 * can't grab the mutex now */
318  	if (mutex_trylock(&bitforce->device_mutex))
319  		return;
320  
321  	if ((err = usb_write(bitforce, BITFORCE_FLASH, BITFORCE_FLASH_LEN, &amount, C_REQUESTFLASH)) < 0 || amount != BITFORCE_FLASH_LEN) {
322  		applog(LOG_ERR, "%s%i: flash request failed (%d:%d)",
323  			bitforce->drv->name, bitforce->device_id, amount, err);
324  	} else {
325  		/* However, this stops anything else getting a reply
326  		 * So best to delay any other access to the BFL */
327  		cgsleep_ms(4000);
328  	}
329  
330  	/* Once we've tried - don't do it until told to again */
331  	bitforce->flash_led = false;
332  
333  	mutex_unlock(&bitforce->device_mutex);
334  
335  	return; // nothing is returned by the BFL
336  }
337  
338  static bool bitforce_get_temp(struct cgpu_info *bitforce)
339  {
340  	char buf[BITFORCE_BUFSIZ+1];
341  	int err, amount;
342  	char *s;
343  
344  	// Device is gone
345  	if (bitforce->usbinfo.nodev)
346  		return false;
347  
348  	/* Do not try to get the temperature if we're polling for a result to
349  	 * minimise the chance of interleaved results */
350  	if (bitforce->polling)
351  		return true;
352  
353  	// Flash instead of Temp - doing both can be too slow
354  	if (bitforce->flash_led) {
355  		bitforce_flash_led(bitforce);
356   		return true;
357  	}
358  
359  	/* It is not critical getting temperature so don't get stuck if we
360  	 * can't grab the mutex here */
361  	if (mutex_trylock(&bitforce->device_mutex))
362  		return false;
363  
364  	if ((err = usb_write(bitforce, BITFORCE_TEMPERATURE, BITFORCE_TEMPERATURE_LEN, &amount, C_REQUESTTEMPERATURE)) < 0 || amount != BITFORCE_TEMPERATURE_LEN) {
365  		mutex_unlock(&bitforce->device_mutex);
366  		applog(LOG_ERR, "%s%i: Error: Request temp invalid/timed out (%d:%d)",
367  				bitforce->drv->name, bitforce->device_id, amount, err);
368  		bitforce->hw_errors++;
369  		return false;
370  	}
371  
372  	if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETTEMPERATURE)) < 0 || amount < 1) {
373  		mutex_unlock(&bitforce->device_mutex);
374  		if (err < 0) {
375  			applog(LOG_ERR, "%s%i: Error: Get temp return invalid/timed out (%d:%d)",
376  					bitforce->drv->name, bitforce->device_id, amount, err);
377  		} else {
378  			applog(LOG_ERR, "%s%i: Error: Get temp returned nothing (%d:%d)",
379  					bitforce->drv->name, bitforce->device_id, amount, err);
380  		}
381  		bitforce->hw_errors++;
382  		return false;
383  	}
384  
385  	mutex_unlock(&bitforce->device_mutex);
386  	
387  	if ((!strncasecmp(buf, "TEMP", 4)) && (s = strchr(buf + 4, ':'))) {
388  		float temp = strtof(s + 1, NULL);
389  
390  		/* Cope with older software  that breaks and reads nonsense
391  		 * values */
392  		if (temp > 100)
393  			temp = strtod(s + 1, NULL);
394  
395  		if (temp > 0) {
396  			bitforce->temp = temp;
397  			if (unlikely(bitforce->cutofftemp > 0 && temp > bitforce->cutofftemp)) {
398  				applog(LOG_WARNING, "%s%i: Hit thermal cutoff limit, disabling!",
399  							bitforce->drv->name, bitforce->device_id);
400  				bitforce->deven = DEV_RECOVER;
401  				dev_error(bitforce, REASON_DEV_THERMAL_CUTOFF);
402  			}
403  		}
404  	} else {
405  		/* Use the temperature monitor as a kind of watchdog for when
406  		 * our responses are out of sync and flush the buffer to
407  		 * hopefully recover */
408  		applog(LOG_WARNING, "%s%i: Garbled response probably throttling, clearing buffer",
409  					bitforce->drv->name, bitforce->device_id);
410  		dev_error(bitforce, REASON_DEV_THROTTLE);
411  		/* Count throttling episodes as hardware errors */
412  		bitforce->hw_errors++;
413  		bitforce_initialise(bitforce, true);
414  		return false;
415  	}
416  
417  	return true;
418  }
419  
420  static bool bitforce_send_work(struct thr_info *thr, struct work *work)
421  {
422  	struct cgpu_info *bitforce = thr->cgpu;
423  	unsigned char ob[70];
424  	char buf[BITFORCE_BUFSIZ+1];
425  	int err, amount;
426  	char *s;
427  	char *cmd;
428  	int len;
429  
430  re_send:
431  	if (bitforce->nonce_range) {
432  		cmd = BITFORCE_SENDRANGE;
433  		len = BITFORCE_SENDRANGE_LEN;
434  	} else {
435  		cmd = BITFORCE_SENDWORK;
436  		len = BITFORCE_SENDWORK_LEN;
437  	}
438  
439  	mutex_lock(&bitforce->device_mutex);
440  	if ((err = usb_write(bitforce, cmd, len, &amount, C_REQUESTSENDWORK)) < 0 || amount != len) {
441  		mutex_unlock(&bitforce->device_mutex);
442  		applog(LOG_ERR, "%s%i: request send work failed (%d:%d)",
443  				bitforce->drv->name, bitforce->device_id, amount, err);
444  		return false;
445  	}
446  
447  	if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_REQUESTSENDWORKSTATUS)) < 0) {
448  		mutex_unlock(&bitforce->device_mutex);
449  		applog(LOG_ERR, "%s%d: read request send work status failed (%d:%d)",
450  				bitforce->drv->name, bitforce->device_id, amount, err);
451  		return false;
452  	}
453  
454  	if (amount == 0 || !buf[0] || !strncasecmp(buf, "B", 1)) {
455  		mutex_unlock(&bitforce->device_mutex);
456  		cgsleep_ms(WORK_CHECK_INTERVAL_MS);
457  		goto re_send;
458  	} else if (unlikely(strncasecmp(buf, "OK", 2))) {
459  		mutex_unlock(&bitforce->device_mutex);
460  		if (bitforce->nonce_range) {
461  			applog(LOG_WARNING, "%s%i: Does not support nonce range, disabling",
462  						bitforce->drv->name, bitforce->device_id);
463  			bitforce->nonce_range = false;
464  			bitforce->sleep_ms *= 5;
465  			bitforce->kname = KNAME_WORK;
466  			goto re_send;
467  		}
468  		applog(LOG_ERR, "%s%i: Error: Send work reports: %s",
469  				bitforce->drv->name, bitforce->device_id, buf);
470  		return false;
471  	}
472  
473  	sprintf((char *)ob, ">>>>>>>>");
474  	memcpy(ob + 8, work->midstate, 32);
475  	memcpy(ob + 8 + 32, work->data + 64, 12);
476  	if (!bitforce->nonce_range) {
477  		sprintf((char *)ob + 8 + 32 + 12, ">>>>>>>>");
478  		work->nonce = bitforce->nonces = 0xffffffff;
479  		len = 60;
480  	} else {
481  		uint32_t *nonce;
482  
483  		nonce = (uint32_t *)(ob + 8 + 32 + 12);
484  		*nonce = htobe32(work->nonce);
485  		nonce = (uint32_t *)(ob + 8 + 32 + 12 + 4);
486  		/* Split work up into 1/5th nonce ranges */
487  		bitforce->nonces = 0x33333332;
488  		*nonce = htobe32(work->nonce + bitforce->nonces);
489  		work->nonce += bitforce->nonces + 1;
490  		sprintf((char *)ob + 8 + 32 + 12 + 8, ">>>>>>>>");
491  		len = 68;
492  	}
493  
494  	if ((err = usb_write(bitforce, (char *)ob, len, &amount, C_SENDWORK)) < 0 || amount != len) {
495  		mutex_unlock(&bitforce->device_mutex);
496  		applog(LOG_ERR, "%s%i: send work failed (%d:%d)",
497  				bitforce->drv->name, bitforce->device_id, amount, err);
498  		return false;
499  	}
500  
501  	if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_SENDWORKSTATUS)) < 0) {
502  		mutex_unlock(&bitforce->device_mutex);
503  		applog(LOG_ERR, "%s%d: read send work status failed (%d:%d)",
504  				bitforce->drv->name, bitforce->device_id, amount, err);
505  		return false;
506  	}
507  
508  	mutex_unlock(&bitforce->device_mutex);
509  
510  	if (opt_debug) {
511  		s = bin2hex(ob + 8, 44);
512  		applog(LOG_DEBUG, "%s%i: block data: %s",
513  				bitforce->drv->name, bitforce->device_id, s);
514  		free(s);
515  	}
516  
517  	if (amount == 0 || !buf[0]) {
518  		applog(LOG_ERR, "%s%i: Error: Send block data returned empty string/timed out",
519  				bitforce->drv->name, bitforce->device_id);
520  		return false;
521  	}
522  
523  	if (unlikely(strncasecmp(buf, "OK", 2))) {
524  		applog(LOG_ERR, "%s%i: Error: Send block data reports: %s",
525  				bitforce->drv->name, bitforce->device_id, buf);
526  		return false;
527  	}
528  
529  	cgtime(&bitforce->work_start_tv);
530  	return true;
531  }
532  
533  static int64_t bitforce_get_result(struct thr_info *thr, struct work *work)
534  {
535  	struct cgpu_info *bitforce = thr->cgpu;
536  	unsigned int delay_time_ms;
537  	struct timeval elapsed;
538  	struct timeval now;
539  	char buf[BITFORCE_BUFSIZ+1];
540  	int amount;
541  	char *pnoncebuf;
542  	uint32_t nonce;
543  
544  	while (1) {
545  		if (unlikely(thr->work_restart))
546  			return 0;
547  
548  		mutex_lock(&bitforce->device_mutex);
549  		usb_write(bitforce, BITFORCE_WORKSTATUS, BITFORCE_WORKSTATUS_LEN, &amount, C_REQUESTWORKSTATUS);
550  		usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETWORKSTATUS);
551  		mutex_unlock(&bitforce->device_mutex);
552  
553  		cgtime(&now);
554  		timersub(&now, &bitforce->work_start_tv, &elapsed);
555  
556  		if (elapsed.tv_sec >= BITFORCE_LONG_TIMEOUT_S) {
557  			applog(LOG_ERR, "%s%i: took %ldms - longer than %dms",
558  				bitforce->drv->name, bitforce->device_id,
559  				tv_to_ms(elapsed), BITFORCE_LONG_TIMEOUT_MS);
560  			return 0;
561  		}
562  
563  		if (amount > 0 && buf[0] && strncasecmp(buf, "B", 1)) /* BFL does not respond during throttling */
564  			break;
565  
566  		/* if BFL is throttling, no point checking so quickly */
567  		delay_time_ms = (buf[0] ? BITFORCE_CHECK_INTERVAL_MS : 2 * WORK_CHECK_INTERVAL_MS);
568  		cgsleep_ms(delay_time_ms);
569  		bitforce->wait_ms += delay_time_ms;
570  	}
571  
572  	if (elapsed.tv_sec > BITFORCE_TIMEOUT_S) {
573  		applog(LOG_ERR, "%s%i: took %ldms - longer than %dms",
574  			bitforce->drv->name, bitforce->device_id,
575  			tv_to_ms(elapsed), BITFORCE_TIMEOUT_MS);
576  		dev_error(bitforce, REASON_DEV_OVER_HEAT);
577  
578  		/* Only return if we got nothing after timeout - there still may be results */
579  		if (amount == 0)
580  			return 0;
581  	} else if (!strncasecmp(buf, BITFORCE_EITHER, BITFORCE_EITHER_LEN)) {
582  		/* Simple timing adjustment. Allow a few polls to cope with
583  		 * OS timer delays being variably reliable. wait_ms will
584  		 * always equal sleep_ms when we've waited greater than or
585  		 * equal to the result return time.*/
586  		delay_time_ms = bitforce->sleep_ms;
587  
588  		if (bitforce->wait_ms > bitforce->sleep_ms + (WORK_CHECK_INTERVAL_MS * 2))
589  			bitforce->sleep_ms += (bitforce->wait_ms - bitforce->sleep_ms) / 2;
590  		else if (bitforce->wait_ms == bitforce->sleep_ms) {
591  			if (bitforce->sleep_ms > WORK_CHECK_INTERVAL_MS)
592  				bitforce->sleep_ms -= WORK_CHECK_INTERVAL_MS;
593  			else if (bitforce->sleep_ms > BITFORCE_CHECK_INTERVAL_MS)
594  				bitforce->sleep_ms -= BITFORCE_CHECK_INTERVAL_MS;
595  		}
596  
597  		if (delay_time_ms != bitforce->sleep_ms)
598  			  applog(LOG_DEBUG, "%s%i: Wait time changed to: %d, waited %u",
599  					bitforce->drv->name, bitforce->device_id,
600  					bitforce->sleep_ms, bitforce->wait_ms);
601  
602  		/* Work out the average time taken. Float for calculation, uint for display */
603  		bitforce->avg_wait_f += (tv_to_ms(elapsed) - bitforce->avg_wait_f) / TIME_AVG_CONSTANT;
604  		bitforce->avg_wait_d = (unsigned int) (bitforce->avg_wait_f + 0.5);
605  	}
606  
607  	applog(LOG_DEBUG, "%s%i: waited %dms until %s",
608  			bitforce->drv->name, bitforce->device_id,
609  			bitforce->wait_ms, buf);
610  	if (!strncasecmp(buf, BITFORCE_NO_NONCE, BITFORCE_NO_NONCE_MATCH))
611  		return bitforce->nonces;   /* No valid nonce found */
612  	else if (!strncasecmp(buf, BITFORCE_IDLE, BITFORCE_IDLE_MATCH))
613  		return 0;	/* Device idle */
614  	else if (strncasecmp(buf, BITFORCE_NONCE, BITFORCE_NONCE_LEN)) {
615  		bitforce->hw_errors++;
616  		applog(LOG_WARNING, "%s%i: Error: Get result reports: %s",
617  			bitforce->drv->name, bitforce->device_id, buf);
618  		bitforce_initialise(bitforce, true);
619  		return 0;
620  	}
621  
622  	pnoncebuf = &buf[12];
623  
624  	while (1) {
625  		hex2bin((void*)&nonce, pnoncebuf, 4);
626  #ifndef __BIG_ENDIAN__
627  		nonce = swab32(nonce);
628  #endif
629  		if (unlikely(bitforce->nonce_range && (nonce >= work->nonce ||
630  			(work->nonce > 0 && nonce < work->nonce - bitforce->nonces - 1)))) {
631  				applog(LOG_WARNING, "%s%i: Disabling broken nonce range support",
632  					bitforce->drv->name, bitforce->device_id);
633  				bitforce->nonce_range = false;
634  				work->nonce = 0xffffffff;
635  				bitforce->sleep_ms *= 5;
636  				bitforce->kname = KNAME_WORK;
637  		}
638  			
639  		submit_nonce(thr, work, nonce);
640  		if (strncmp(&pnoncebuf[8], ",", 1))
641  			break;
642  		pnoncebuf += 9;
643  	}
644  
645  	return bitforce->nonces;
646  }
647  
648  static void bitforce_shutdown(__maybe_unused struct thr_info *thr)
649  {
650  //	struct cgpu_info *bitforce = thr->cgpu;
651  }
652  
653  static void biforce_thread_enable(struct thr_info *thr)
654  {
655  	struct cgpu_info *bitforce = thr->cgpu;
656  
657  	bitforce_initialise(bitforce, true);
658  }
659  
660  static int64_t bitforce_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
661  {
662  	struct cgpu_info *bitforce = thr->cgpu;
663  	bool send_ret;
664  	int64_t ret;
665  
666  	// Device is gone
667  	if (bitforce->usbinfo.nodev)
668  		return -1;
669  
670  	send_ret = bitforce_send_work(thr, work);
671  
672  	if (!restart_wait(thr, bitforce->sleep_ms))
673  		return 0;
674  
675  	bitforce->wait_ms = bitforce->sleep_ms;
676  
677  	if (send_ret) {
678  		bitforce->polling = true;
679  		ret = bitforce_get_result(thr, work);
680  		bitforce->polling = false;
681  	} else
682  		ret = -1;
683  
684  	if (ret == -1) {
685  		ret = 0;
686  		applog(LOG_ERR, "%s%i: Comms error", bitforce->drv->name, bitforce->device_id);
687  		dev_error(bitforce, REASON_DEV_COMMS_ERROR);
688  		bitforce->hw_errors++;
689  		/* empty read buffer */
690  		bitforce_initialise(bitforce, true);
691  	}
692  	return ret;
693  }
694  
695  static bool bitforce_get_stats(struct cgpu_info *bitforce)
696  {
697  	return bitforce_get_temp(bitforce);
698  }
699  
700  static void bitforce_identify(struct cgpu_info *bitforce)
701  {
702  	bitforce->flash_led = true;
703  }
704  
705  static bool bitforce_thread_init(struct thr_info *thr)
706  {
707  	struct cgpu_info *bitforce = thr->cgpu;
708  	unsigned int wait;
709  
710  	/* Pause each new thread at least 100ms between initialising
711  	 * so the devices aren't making calls all at the same time. */
712  	wait = thr->id * MAX_START_DELAY_MS;
713  	applog(LOG_DEBUG, "%s%d: Delaying start by %dms",
714  			bitforce->drv->name, bitforce->device_id, wait / 1000);
715  	cgsleep_ms(wait);
716  
717  	return true;
718  }
719  
720  static struct api_data *bitforce_api_stats(struct cgpu_info *cgpu)
721  {
722  	struct api_data *root = NULL;
723  
724  	// Warning, access to these is not locked - but we don't really
725  	// care since hashing performance is way more important than
726  	// locking access to displaying API debug 'stats'
727  	// If locking becomes an issue for any of them, use copy_data=true also
728  	root = api_add_uint(root, "Sleep Time", &(cgpu->sleep_ms), false);
729  	root = api_add_uint(root, "Avg Wait", &(cgpu->avg_wait_d), false);
730  
731  	return root;
732  }
733  
734  struct device_drv bitforce_drv = {
735  	.drv_id = DRIVER_bitforce,
736  	.dname = "BitForce",
737  	.name = "BFL",
738  	.drv_detect = bitforce_detect,
739  	.get_api_stats = bitforce_api_stats,
740  	.get_statline_before = get_bitforce_statline_before,
741  	.get_stats = bitforce_get_stats,
742  	.identify_device = bitforce_identify,
743  	.thread_prepare = bitforce_thread_prepare,
744  	.thread_init = bitforce_thread_init,
745  	.scanhash = bitforce_scanhash,
746  	.thread_shutdown = bitforce_shutdown,
747  	.thread_enable = biforce_thread_enable
748  };