Commit 2a222ecffa7e14dc3f55b55d38d5e61a68fa1eb1

Con Kolivas 2013-05-27T20:04:12

Use the avalon read timeout to completion if no data has been read.

diff --git a/driver-avalon.c b/driver-avalon.c
index f300b3e..de0ade2 100644
--- a/driver-avalon.c
+++ b/driver-avalon.c
@@ -242,13 +242,14 @@ static void wait_avalon_ready(struct cgpu_info *avalon)
 	}
 }
 
-static int avalon_read(struct cgpu_info *avalon, unsigned char *buf, size_t bufsize)
+static int avalon_read(struct cgpu_info *avalon, unsigned char *buf,
+		       size_t bufsize, int timeout)
 {
 	struct cg_usb_device *usbdev = avalon->usbdev;
 	int err, amount;
 
 	err = libusb_bulk_transfer(usbdev->handle, usbdev->found->eps[DEFAULT_EP_IN].ep,
-				   buf, bufsize, &amount, AVALON_READ_TIMEOUT);
+				   buf, bufsize, &amount, timeout);
 	applog(LOG_DEBUG, "%s%i: Get avalon read got err %d",
 	       avalon->drv->name, avalon->device_id, err);
 	return amount;
@@ -281,7 +282,8 @@ static int avalon_reset(struct cgpu_info *avalon, bool initial)
 		return 0;
 	}
 
-	ret = avalon_read(avalon, (unsigned char *)&ar, AVALON_READ_SIZE);
+	ret = avalon_read(avalon, (unsigned char *)&ar, AVALON_READ_SIZE,
+			  AVALON_RESET_TIMEOUT);
 
 	/* What do these sleeps do?? */
 	p.tv_sec = 0;
@@ -742,6 +744,7 @@ static void *avalon_get_results(void *userdata)
 	RenameThread(threadname);
 
 	while (42) {
+		struct timeval tv_start, now, tdiff;
 		unsigned char buf[rsize];
 		int amount, ofs, cp;
 
@@ -754,10 +757,19 @@ static void *avalon_get_results(void *userdata)
 			offset = 0;
 		}
 
-		amount = avalon_read(avalon, buf, rsize);
+		cgtime(&tv_start);
+		amount = avalon_read(avalon, buf, rsize, AVALON_READ_TIMEOUT);
 
-		if (amount < 3)
+		if (amount < 3) {
+			int ms_delay;
+
+			cgtime(&now);
+			timersub(&now, &tv_start, &tdiff);
+			ms_delay = AVALON_READ_TIMEOUT - (tdiff.tv_usec / 1000);
+			if (ms_delay > 0)
+				nmsleep(ms_delay);
 			continue;
+		}
 
 		if (opt_debug) {
 			applog(LOG_DEBUG, "Avalon: get:");
diff --git a/driver-avalon.h b/driver-avalon.h
index 03c89d7..d7dc870 100644
--- a/driver-avalon.h
+++ b/driver-avalon.h
@@ -30,6 +30,7 @@
 
 #define AVALON_FTDI_READSIZE 512
 #define AVALON_READBUF_SIZE 8192
+#define AVALON_RESET_TIMEOUT 100
 #define AVALON_READ_TIMEOUT 10
 
 struct avalon_task {