Commit 8fb7a0d1be3e2d7767771bc25077ae636c4a88c3

Con Kolivas 2013-11-04T08:45:09

Always use a usb read buffer instead of having to explicitly enable it.

diff --git a/driver-avalon.c b/driver-avalon.c
index 0e6ab3b..796085a 100644
--- a/driver-avalon.c
+++ b/driver-avalon.c
@@ -780,7 +780,6 @@ static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found
 	/* Even though this is an FTDI type chip, we want to do the parsing
 	 * all ourselves so set it to std usb type */
 	avalon->usbdev->usb_type = USB_TYPE_STD;
-	usb_buffer_enable(avalon);
 
 	/* We have a real Avalon! */
 	avalon_initialise(avalon);
diff --git a/driver-bflsc.c b/driver-bflsc.c
index a64e552..f3e45c4 100644
--- a/driver-bflsc.c
+++ b/driver-bflsc.c
@@ -888,8 +888,6 @@ reinit:
 	mutex_init(&bflsc->device_mutex);
 	rwlock_init(&sc_info->stat_lock);
 
-	usb_buffer_enable(bflsc);
-
 	return true;
 
 unshin:
diff --git a/driver-bitfury.c b/driver-bitfury.c
index 3fdfd88..69f3aca 100644
--- a/driver-bitfury.c
+++ b/driver-bitfury.c
@@ -146,8 +146,6 @@ static bool bitfury_detect_one(struct libusb_device *dev, struct usb_find_device
 	 * hashrate to adapt quickly on starting. */
 	info->total_nonces = 1;
 
-	usb_buffer_enable(bitfury);
-
 	if (!bitfury_open(bitfury))
 		goto out_close;
 
diff --git a/driver-hashfast.c b/driver-hashfast.c
index c4ce1c1..7549a83 100644
--- a/driver-hashfast.c
+++ b/driver-hashfast.c
@@ -347,7 +347,6 @@ static bool hfa_initialise(struct cgpu_info *hashfast)
 	if (hashfast->usbinfo.nodev)
 		return false;
 
-	usb_buffer_enable(hashfast);
 	hfa_clear_readbuf(hashfast);
 
 	err = usb_transfer(hashfast, 0, 9, 1, 0, C_ATMEL_RESET);
@@ -916,7 +915,6 @@ static void hfa_shutdown(struct thr_info *thr)
 	pthread_join(info->read_thr, NULL);
 	hfa_free_all_work(info);
 	hfa_clear_readbuf(hashfast);
-	usb_buffer_disable(hashfast);
 	free(info->works);
 	free(info->die_statistics);
 	free(info->die_status);
diff --git a/driver-icarus.c b/driver-icarus.c
index 64c1fd1..bf53073 100644
--- a/driver-icarus.c
+++ b/driver-icarus.c
@@ -822,8 +822,6 @@ static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices
 	if (!usb_init(icarus, dev, found))
 		goto shin;
 
-	usb_buffer_enable(icarus);
-
 	get_options(this_option_offset, icarus, &baud, &work_division, &fpga_count);
 
 	hex2bin((void *)(&workdata), golden_ob, sizeof(workdata));
diff --git a/usbutils.c b/usbutils.c
index 227dad2..edd89bd 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -1309,9 +1309,6 @@ static struct cg_usb_device *free_cgusb(struct cg_usb_device *cgusb)
 
 	free(cgusb->found);
 
-	if (cgusb->buffer)
-		free(cgusb->buffer);
-
 	free(cgusb);
 
 	return NULL;
@@ -2503,6 +2500,7 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
 	double max, done;
 	int bufleft, err, got, tot, pstate;
 	bool first = true;
+	bool dobuffer;
 	char *search;
 	int endlen;
 	unsigned char *ptr, *usbbuf = cgpu->usbinfo.bulkbuf;
@@ -2531,17 +2529,12 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
 		timeout = usbdev->found->timeout;
 
 	if (end == NULL) {
-		if (usbdev->buffer && usbdev->bufamt) {
-			tot = usbdev->bufamt;
-			bufleft = bufsiz - tot;
+		tot = usbdev->bufamt;
+		bufleft = bufsiz - tot;
+		if (tot)
 			memcpy(usbbuf, usbdev->buffer, tot);
-			ptr = usbbuf + tot;
-			usbdev->bufamt = 0;
-		} else {
-			tot = 0;
-			bufleft = bufsiz;
-			ptr = usbbuf;
-		}
+		ptr = usbbuf + tot;
+		usbdev->bufamt = 0;
 
 		err = LIBUSB_SUCCESS;
 		initial_timeout = timeout;
@@ -2592,7 +2585,7 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
 		}
 
 		// N.B. usbdev->buffer was emptied before the while() loop
-		if (usbdev->buffer && tot > (int)bufsiz) {
+		if (tot > (int)bufsiz) {
 			usbdev->bufamt = tot - bufsiz;
 			memcpy(usbdev->buffer, usbbuf + bufsiz, usbdev->bufamt);
 			tot -= usbdev->bufamt;
@@ -2607,23 +2600,19 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
 		goto out_unlock;
 	}
 
-	if (usbdev->buffer && usbdev->bufamt) {
-		tot = usbdev->bufamt;
-		bufleft = bufsiz - tot;
+	tot = usbdev->bufamt;
+	bufleft = bufsiz - tot;
+	if (tot)
 		memcpy(usbbuf, usbdev->buffer, tot);
-		ptr = usbbuf + tot;
-		usbdev->bufamt = 0;
-	} else {
-		tot = 0;
-		bufleft = bufsiz;
-		ptr = usbbuf;
-	}
+	ptr = usbbuf + tot;
+	usbdev->bufamt = 0;
 
 	endlen = strlen(end);
 	err = LIBUSB_SUCCESS;
 	initial_timeout = timeout;
 	max = ((double)timeout) / 1000.0;
 	cgtime(&read_start);
+
 	while (bufleft > 0) {
 		got = 0;
 		err = usb_bulk_transfer(usbdev->handle, intinfo, epinfo,
@@ -2670,38 +2659,36 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
 			break;
 	}
 
-	if (usbdev->buffer) {
-		bool dobuffer = false;
+	dobuffer = false;
 
-		if ((search = find_end(usbbuf, usbbuf, tot, tot, (char *)end, endlen, true))) {
-			// end finishes after bufsiz
-			if ((search + endlen - (char *)usbbuf) > (int)bufsiz) {
-				usbdev->bufamt = tot - bufsiz;
-				dobuffer = true;
-			} else {
-				// extra data after end
-				if (*(search + endlen)) {
-					usbdev->bufamt = tot - (search + endlen - (char *)usbbuf);
-					dobuffer = true;
-				}
-			}
+	if ((search = find_end(usbbuf, usbbuf, tot, tot, (char *)end, endlen, true))) {
+		// end finishes after bufsiz
+		if ((search + endlen - (char *)usbbuf) > (int)bufsiz) {
+			usbdev->bufamt = tot - bufsiz;
+			dobuffer = true;
 		} else {
-			// no end, but still bigger than bufsiz
-			if (tot > (int)bufsiz) {
-				usbdev->bufamt = tot - bufsiz;
+			// extra data after end
+			if (*(search + endlen)) {
+				usbdev->bufamt = tot - (search + endlen - (char *)usbbuf);
 				dobuffer = true;
 			}
 		}
-
-		if (dobuffer) {
-			tot -= usbdev->bufamt;
-			memcpy(usbdev->buffer, usbbuf + tot, usbdev->bufamt);
-			usbbuf[tot] = '\0';
-			applog(LOG_DEBUG, "USB: %s%i read2 buffering %d extra bytes",
-			       cgpu->drv->name, cgpu->device_id, usbdev->bufamt);
+	} else {
+		// no end, but still bigger than bufsiz
+		if (tot > (int)bufsiz) {
+			usbdev->bufamt = tot - bufsiz;
+			dobuffer = true;
 		}
 	}
 
+	if (dobuffer) {
+		tot -= usbdev->bufamt;
+		memcpy(usbdev->buffer, usbbuf + tot, usbdev->bufamt);
+		usbbuf[tot] = '\0';
+		applog(LOG_DEBUG, "USB: %s%i read2 buffering %d extra bytes",
+			cgpu->drv->name, cgpu->device_id, usbdev->bufamt);
+	}
+
 	*processed = tot;
 	memcpy((char *)buf, (const char *)usbbuf, (tot < (int)bufsiz) ? tot + 1 : (int)bufsiz);
 
@@ -3031,44 +3018,6 @@ int _usb_ftdi_set_latency(struct cgpu_info *cgpu, int intinfo)
 	return err;
 }
 
-void usb_buffer_enable(struct cgpu_info *cgpu)
-{
-	struct cg_usb_device *cgusb;
-	int pstate;
-
-	DEVWLOCK(cgpu, pstate);
-
-	cgusb = cgpu->usbdev;
-	if (cgusb && !cgusb->buffer) {
-		cgusb->bufamt = 0;
-		cgusb->buffer = malloc(USB_MAX_READ+1);
-		if (!cgusb->buffer)
-			quit(1, "Failed to malloc buffer for USB %s%i",
-				cgpu->drv->name, cgpu->device_id);
-		cgusb->bufsiz = USB_MAX_READ;
-	}
-
-	DEVWUNLOCK(cgpu, pstate);
-}
-
-void usb_buffer_disable(struct cgpu_info *cgpu)
-{
-	struct cg_usb_device *cgusb;
-	int pstate;
-
-	DEVWLOCK(cgpu, pstate);
-
-	cgusb = cgpu->usbdev;
-	if (cgusb && cgusb->buffer) {
-		cgusb->bufamt = 0;
-		cgusb->bufsiz = 0;
-		free(cgusb->buffer);
-		cgusb->buffer = NULL;
-	}
-
-	DEVWUNLOCK(cgpu, pstate);
-}
-
 void usb_buffer_clear(struct cgpu_info *cgpu)
 {
 	int pstate;
diff --git a/usbutils.h b/usbutils.h
index b3a96e9..f8f9003 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -181,6 +181,8 @@ enum usb_types {
 	USB_TYPE_FTDI
 };
 
+#define USB_MAX_READ 8192
+
 struct cg_usb_device {
 	struct usb_find_devices *found;
 	libusb_device_handle *handle;
@@ -196,7 +198,7 @@ struct cg_usb_device {
 	char *serial_string;
 	unsigned char fwVersion;	// ??
 	unsigned char interfaceVersion;	// ??
-	char *buffer;
+	char buffer[USB_MAX_READ];
 	uint32_t bufsiz;
 	uint32_t bufamt;
 	cgtimer_t cgt_last_write;
@@ -205,8 +207,6 @@ struct cg_usb_device {
 
 #define USB_NOSTAT 0
 
-#define USB_MAX_READ 8192
-
 #define USB_TMO_0 50
 #define USB_TMO_1 100
 #define USB_TMO_2 500
@@ -394,8 +394,6 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe
 int usb_ftdi_cts(struct cgpu_info *cgpu);
 int _usb_ftdi_set_latency(struct cgpu_info *cgpu, int intinfo);
 #define usb_ftdi_set_latency(_cgpu) _usb_ftdi_set_latency(_cgpu, DEFAULT_INTINFO)
-void usb_buffer_enable(struct cgpu_info *cgpu);
-void usb_buffer_disable(struct cgpu_info *cgpu);
 void usb_buffer_clear(struct cgpu_info *cgpu);
 uint32_t usb_buffer_size(struct cgpu_info *cgpu);
 void usb_set_cps(struct cgpu_info *cgpu, int cps);