Commit 05dd48ace1801f4bc9351b44b09bf5d15a7d9e1a

Kano 2013-05-05T22:04:46

usbutils/mmq fixed size usb_read default to wait for all data

diff --git a/driver-modminer.c b/driver-modminer.c
index fff0225..8d609df 100644
--- a/driver-modminer.c
+++ b/driver-modminer.c
@@ -103,7 +103,7 @@ static void do_ping(struct cgpu_info *modminer)
 		modminer->drv->name, modminer->fpgaid, amount, err);
 
 	// Clear any outstanding data
-	while ((err = usb_read(modminer, buf, sizeof(buf)-1, &amount, C_CLEAR)) == 0 && amount > 0)
+	while ((err = usb_read_once(modminer, buf, sizeof(buf)-1, &amount, C_CLEAR)) == 0 && amount > 0)
 		applog(LOG_DEBUG, "%s%u: clear got %d",
 			modminer->drv->name, modminer->fpgaid, amount);
 
@@ -141,7 +141,7 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic
 		goto unshin;
 	}
 
-	if ((err = usb_read(modminer, buf, sizeof(buf)-1, &amount, C_GETVERSION)) < 0 || amount < 1) {
+	if ((err = usb_read_once(modminer, buf, sizeof(buf)-1, &amount, C_GETVERSION)) < 0 || amount < 1) {
 		if (err < 0)
 			applog(LOG_ERR, "%s detect (%s) no version reply (%d)",
 				modminer->drv->dname, devpath, err);
diff --git a/usbutils.c b/usbutils.c
index a03566a..f4efe61 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -1837,7 +1837,7 @@ static void rejected_inc(struct cgpu_info *cgpu)
 
 #define USB_MAX_READ 8192
 
-int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool ftdi)
+int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool ftdi, bool readonce)
 {
 	struct cg_usb_device *usbdev = cgpu->usbdev;
 #if DO_USB_STATS
@@ -1846,7 +1846,7 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
 	struct timeval read_start, tv_finish;
 	unsigned int initial_timeout;
 	double max, done;
-	int err, got, tot;
+	int bufleft, err, got, tot;
 	bool first = true;
 	unsigned char *search;
 	int endlen;
@@ -1855,7 +1855,7 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
 	unsigned char usbbuf[USB_MAX_READ+4], *ptr;
 	size_t usbbufread;
 
-	USBDEBUG("USB debug: _usb_read(%s (nodev=%s),ep=%d,buf=%p,bufsiz=%zu,proc=%p,timeout=%u,end=%s,cmd=%s,ftdi=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), ep, buf, bufsiz, processed, timeout, end ? (char *)str_text((char *)end) : "NULL", usb_cmdname(cmd), bool_str(ftdi));
+	USBDEBUG("USB debug: _usb_read(%s (nodev=%s),ep=%d,buf=%p,bufsiz=%zu,proc=%p,timeout=%u,end=%s,cmd=%s,ftdi=%s,readonce=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), ep, buf, bufsiz, processed, timeout, end ? (char *)str_text((char *)end) : "NULL", usb_cmdname(cmd), bool_str(ftdi), bool_str(readonce));
 
 	if (bufsiz > USB_MAX_READ)
 		quit(1, "%s USB read request %d too large (max=%d)", cgpu->drv->name, bufsiz, USB_MAX_READ);
@@ -1873,34 +1873,60 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
 		timeout = usbdev->found->timeout;
 
 	if (end == NULL) {
-		if (ftdi)
-			usbbufread = bufsiz + 2;
-		else
-			usbbufread = bufsiz;
-		got = 0;
-		STATS_TIMEVAL(&tv_start);
-		err = libusb_bulk_transfer(usbdev->handle,
-				usbdev->found->eps[ep].ep,
-				usbbuf, usbbufread, &got, timeout);
-		STATS_TIMEVAL(&tv_finish);
-		USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, SEQ0);
-		usbbuf[got] = '\0';
+		tot = 0;
+		ptr = usbbuf;
+		bufleft = bufsiz;
+		err = LIBUSB_SUCCESS;
+		initial_timeout = timeout;
+		max = ((double)timeout) / 1000.0;
+		cgtime(&read_start);
+		while (bufleft > 0) {
+			if (ftdi)
+				usbbufread = bufleft + 2;
+			else
+				usbbufread = bufleft;
+			got = 0;
+			STATS_TIMEVAL(&tv_start);
+			err = libusb_bulk_transfer(usbdev->handle,
+					usbdev->found->eps[ep].ep,
+					ptr, usbbufread, &got, timeout);
+			STATS_TIMEVAL(&tv_finish);
+			USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, first ? SEQ0 : SEQ1);
+			ptr[got] = '\0';
+
+			USBDEBUG("USB debug: @_usb_read(%s (nodev=%s)) first=%s err=%d%s got=%d ptr='%s' usbbufread=%zu", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), bool_str(first), err, isnodev(err), got, (char *)str_text((char *)ptr), usbbufread);
+
+			if (ftdi) {
+				// first 2 bytes returned are an FTDI status
+				if (got > 2) {
+					got -= 2;
+					memmove(ptr, ptr+2, got+1);
+				} else {
+					got = 0;
+					*ptr = '\0';
+				}
+			}
 
-		USBDEBUG("USB debug: @_usb_read(%s (nodev=%s)) err=%d%s got=%d usbbuf='%s' usbbufread=%zu", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), err, isnodev(err), got, (char *)str_text((char *)usbbuf), usbbufread);
+			tot += got;
 
-		if (ftdi) {
-			// first 2 bytes returned are an FTDI status
-			if (got > 2) {
-				got -= 2;
-				memmove(usbbuf, usbbuf+2, got+1);
-			} else {
-				got = 0;
-				usbbuf[0] = '\0';
-			}
+			if (err || readonce)
+				break;
+
+			ptr += got;
+			bufleft -= got;
+
+			first = false;
+
+			done = tdiff(&tv_finish, &read_start);
+			// N.B. this is return LIBUSB_SUCCESS with whatever size has already been read
+			if (unlikely(done >= max))
+				break;
+
+			timeout = initial_timeout - (done * 1000);
 		}
 
-		*processed = got;
-		memcpy((char *)buf, (const char *)usbbuf, (got < (int)bufsiz) ? got + 1 : (int)bufsiz);
+		*processed = tot;
+		memcpy((char *)buf, (const char *)usbbuf, (tot < (int)bufsiz) ? tot + 1 : (int)bufsiz);
 
 		if (NODEV(err))
 			release_cgpu(cgpu);
@@ -1910,16 +1936,17 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
 
 	tot = 0;
 	ptr = usbbuf;
+	bufleft = bufsiz;
 	endlen = strlen(end);
 	err = LIBUSB_SUCCESS;
 	initial_timeout = timeout;
 	max = ((double)timeout) / 1000.0;
 	cgtime(&read_start);
-	while (bufsiz) {
+	while (bufleft > 0) {
 		if (ftdi)
-			usbbufread = bufsiz + 2;
+			usbbufread = bufleft + 2;
 		else
-			usbbufread = bufsiz;
+			usbbufread = bufleft;
 		got = 0;
 		STATS_TIMEVAL(&tv_start);
 		err = libusb_bulk_transfer(usbdev->handle,
@@ -1944,7 +1971,7 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
 
 		tot += got;
 
-		if (err)
+		if (err || readonce)
 			break;
 
 		// WARNING - this will return data past END ('if' there is extra data)
@@ -1966,7 +1993,7 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
 		}
 
 		ptr += got;
-		bufsiz -= got;
+		bufleft -= got;
 
 		first = false;
 
diff --git a/usbutils.h b/usbutils.h
index 7fd6cb3..2c4f642 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -150,26 +150,29 @@ bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find
 void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *));
 struct api_data *api_usb_stats(int *count);
 void update_usb_stats(struct cgpu_info *cgpu);
-int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool ftdi);
+int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool ftdi, bool readonce);
 int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds);
 int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned int timeout, enum usb_cmds cmd);
 void usb_cleanup();
 void usb_initialise();
 
 #define usb_read(cgpu, buf, bufsiz, read, cmd) \
-	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false)
+	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, false)
+
+#define usb_read_once(cgpu, buf, bufsiz, read, cmd) \
+	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, true)
 
 #define usb_read_nl(cgpu, buf, bufsiz, read, cmd) \
-	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, false)
+	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, false, false)
 
 #define usb_read_ep(cgpu, ep, buf, bufsiz, read, cmd) \
-	_usb_read(cgpu, ep, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false)
+	_usb_read(cgpu, ep, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, false)
 
 #define usb_read_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
-	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, false)
+	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, false, false)
 
 #define usb_read_ep_timeout(cgpu, ep, buf, bufsiz, read, timeout, cmd) \
-	_usb_read(cgpu, ep, buf, bufsiz, read, timeout, NULL, cmd, false)
+	_usb_read(cgpu, ep, buf, bufsiz, read, timeout, NULL, cmd, false, false)
 
 #define usb_write(cgpu, buf, bufsiz, wrote, cmd) \
 	_usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
@@ -184,13 +187,13 @@ void usb_initialise();
 	_usb_write(cgpu, ep, buf, bufsiz, wrote, timeout, cmd)
 
 #define usb_ftdi_read(cgpu, buf, bufsiz, read, cmd) \
-	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, true)
+	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, true, false)
 
 #define usb_ftdi_read_nl(cgpu, buf, bufsiz, read, cmd) \
-	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, true)
+	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, true, false)
 
 #define usb_ftdi_read_ok(cgpu, buf, bufsiz, read, cmd) \
-	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "OK\n", cmd, true)
+	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "OK\n", cmd, true, false)
 
 #define usb_transfer(cgpu, typ, req, val, idx, cmd) \
 	_usb_transfer(cgpu, typ, req, val, idx, DEVTIMEOUT, cmd)