Commit 488b2ba5e4a7ceb46bc62c304d96e8c4e042bf61

Con Kolivas 2013-11-14T21:25:52

Merge branch 'master' into bxf

diff --git a/api.c b/api.c
index 07f6c39..302196d 100644
--- a/api.c
+++ b/api.c
@@ -1625,10 +1625,14 @@ uint64_t api_trylock(void *lock, const char *file, const char *func, const int l
 	LOCKINFO *info;
 	uint64_t id;
 
+	locklock();
+
 	info = findlock(lock, CGLOCK_UNKNOWN, file, func, linenum);
 	id = lock_id++;
 	addgettry(info, id, file, func, linenum, false);
 
+	lockunlock();
+
 	return id;
 }
 
diff --git a/compat/libusb-1.0/libusb/os/windows_usb.c b/compat/libusb-1.0/libusb/os/windows_usb.c
index afc4dfc..ac359c4 100644
--- a/compat/libusb-1.0/libusb/os/windows_usb.c
+++ b/compat/libusb-1.0/libusb/os/windows_usb.c
@@ -1822,9 +1822,6 @@ static int windows_submit_transfer(struct usbi_transfer *itransfer)
 		return submit_control_transfer(itransfer);
 	case LIBUSB_TRANSFER_TYPE_BULK:
 	case LIBUSB_TRANSFER_TYPE_INTERRUPT:
-		if (IS_XFEROUT(transfer) &&
-		    transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET)
-			return LIBUSB_ERROR_NOT_SUPPORTED;
 		return submit_bulk_transfer(itransfer);
 	case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
 		return submit_iso_transfer(itransfer);
@@ -2391,20 +2388,8 @@ static int winusb_configure_endpoints(struct libusb_device_handle *dev_handle, i
 			PIPE_TRANSFER_TIMEOUT, sizeof(ULONG), &timeout)) {
 			usbi_dbg("failed to set PIPE_TRANSFER_TIMEOUT for control endpoint %02X", endpoint_address);
 		}
-		if (i == -1) continue;	// Other policies don't apply to control endpoint
-		policy = false;
-		if (!WinUsb_SetPipePolicy(winusb_handle, endpoint_address,
-			SHORT_PACKET_TERMINATE, sizeof(UCHAR), &policy)) {
-			usbi_dbg("failed to disable SHORT_PACKET_TERMINATE for endpoint %02X", endpoint_address);
-		}
-		if (!WinUsb_SetPipePolicy(winusb_handle, endpoint_address,
-			IGNORE_SHORT_PACKETS, sizeof(UCHAR), &policy)) {
-			usbi_dbg("failed to disable IGNORE_SHORT_PACKETS for endpoint %02X", endpoint_address);
-		}
-		if (!WinUsb_SetPipePolicy(winusb_handle, endpoint_address,
-			ALLOW_PARTIAL_READS, sizeof(UCHAR), &policy)) {
-			usbi_dbg("failed to disable ALLOW_PARTIAL_READS for endpoint %02X", endpoint_address);
-		}
+		if (i == -1)
+			continue;	// Other policies don't apply to control endpoint
 		policy = true;
 		if (!WinUsb_SetPipePolicy(winusb_handle, endpoint_address,
 			AUTO_CLEAR_STALL, sizeof(UCHAR), &policy)) {
@@ -2649,6 +2634,8 @@ static int winusb_submit_bulk_transfer(struct usbi_transfer *itransfer)
 	bool ret;
 	int current_interface;
 	struct winfd wfd;
+	ULONG ppolicy = sizeof(UCHAR);
+	UCHAR policy;
 
 	CHECK_WINUSB_AVAILABLE;
 
@@ -2671,9 +2658,20 @@ static int winusb_submit_bulk_transfer(struct usbi_transfer *itransfer)
 	}
 
 	if (IS_XFERIN(transfer)) {
-		usbi_dbg("reading %d bytes", transfer->length);
+		WinUsb_GetPipePolicy(wfd.handle, transfer->endpoint, AUTO_CLEAR_STALL, &ppolicy, &policy);
+		if (!policy) {
+			policy = TRUE;
+			WinUsb_SetPipePolicy(wfd.handle, transfer->endpoint, AUTO_CLEAR_STALL, ppolicy, &policy);
+		}
 		ret = WinUsb_ReadPipe(wfd.handle, transfer->endpoint, transfer->buffer, transfer->length, NULL, wfd.overlapped);
 	} else {
+		if (transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
+			WinUsb_GetPipePolicy(wfd.handle, transfer->endpoint, SHORT_PACKET_TERMINATE, &ppolicy, &policy);
+			if (!policy) {
+				policy = TRUE;
+				WinUsb_SetPipePolicy(wfd.handle, transfer->endpoint, SHORT_PACKET_TERMINATE, ppolicy, &policy);
+			}
+		}
 		usbi_dbg("writing %d bytes", transfer->length);
 		ret = WinUsb_WritePipe(wfd.handle, transfer->endpoint, transfer->buffer, transfer->length, NULL, wfd.overlapped);
 	}
diff --git a/driver-bab.c b/driver-bab.c
index 545e8bb..7db3df1 100644
--- a/driver-bab.c
+++ b/driver-bab.c
@@ -17,7 +17,8 @@
 #include "sha2.h"
 
 /*
- * Tested on RPi running Raspbian with BlackArrow BitFury V1 16 chip GPIO board
+ * Tested on RPi running both Raspbian and Arch
+ *  with BlackArrow BitFury V1 16 chip GPIO board
  */
 
 #ifndef LINUX
diff --git a/driver-klondike.c b/driver-klondike.c
index 2446202..3a740ec 100644
--- a/driver-klondike.c
+++ b/driver-klondike.c
@@ -1129,26 +1129,28 @@ static void klondike_flush_work(struct cgpu_info *klncgpu)
 	KLINE kline;
 	int slaves, dev;
 
-	wr_lock(&(klninfo->stat_lock));
-	klninfo->block_seq++;
-	slaves = klninfo->status[0].kline.ws.slavecount;
-	wr_unlock(&(klninfo->stat_lock));
+	if (klninfo->initialised) {
+		wr_lock(&(klninfo->stat_lock));
+		klninfo->block_seq++;
+		slaves = klninfo->status[0].kline.ws.slavecount;
+		wr_unlock(&(klninfo->stat_lock));
 
-	applog(LOG_DEBUG, "%s%i: flushing work",
-			  klncgpu->drv->name, klncgpu->device_id);
-	zero_kline(&kline);
-	kline.hd.cmd = KLN_CMD_ABORT;
-	for (dev = 0; dev <= slaves; dev++) {
-		kline.hd.dev = dev;
-		kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
-		if (kitem != NULL) {
-			wr_lock(&(klninfo->stat_lock));
-			memcpy((void *)&(klninfo->status[dev]),
-				kitem,
-				sizeof(klninfo->status[dev]));
-			klninfo->jobque[dev].flushed = true;
-			wr_unlock(&(klninfo->stat_lock));
-			kitem = release_kitem(klncgpu, kitem);
+		applog(LOG_DEBUG, "%s%i: flushing work",
+				  klncgpu->drv->name, klncgpu->device_id);
+		zero_kline(&kline);
+		kline.hd.cmd = KLN_CMD_ABORT;
+		for (dev = 0; dev <= slaves; dev++) {
+			kline.hd.dev = dev;
+			kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
+			if (kitem != NULL) {
+				wr_lock(&(klninfo->stat_lock));
+				memcpy((void *)&(klninfo->status[dev]),
+					kitem,
+					sizeof(klninfo->status[dev]));
+				klninfo->jobque[dev].flushed = true;
+				wr_unlock(&(klninfo->stat_lock));
+				kitem = release_kitem(klncgpu, kitem);
+			}
 		}
 	}
 }
diff --git a/usbutils.c b/usbutils.c
index 16e383f..1984c2e 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -2456,15 +2456,11 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, int intinfo,
 	struct usb_transfer ut;
 	unsigned char endpoint;
 	int err, errn;
-	/* Zero length packet required */
-	bool zlp = false;
 #if DO_USB_STATS
 	struct timeval tv_start, tv_finish;
 #endif
 	unsigned char buf[512];
 #ifdef WIN32
-	int dummy;
-
 	/* On windows the callback_timeout is a safety mechanism only. */
 	bulk_timeout = timeout;
 	callback_timeout += WIN_CALLBACK_EXTRA;
@@ -2486,17 +2482,9 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, int intinfo,
 
 	if ((endpoint & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT) {
 		memcpy(buf, data, length);
-		/* If this is the last packet in a transfer and is the length
-		 * of the wMaxPacketSize then we need to send a zero length
-		 * packet to let the device know it's the end of the message.*/
 		if (length > usb_epinfo->wMaxPacketSize)
 			length = usb_epinfo->wMaxPacketSize;
-		else if (length == usb_epinfo->wMaxPacketSize)
-			zlp = true;
-#ifndef WIN32
-		/* Windows doesn't support this flag so we emulate it below */
 		ut.transfer->flags |= LIBUSB_TRANSFER_ADD_ZERO_PACKET;
-#endif
 	}
 
 	USBDEBUG("USB debug: @usb_bulk_transfer(%s (nodev=%s),intinfo=%d,epinfo=%d,data=%p,length=%d,timeout=%u,mode=%d,cmd=%s,seq=%d) endpoint=%d", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), intinfo, epinfo, data, length, timeout, mode, usb_cmdname(cmd), seq, (int)endpoint);
@@ -2539,12 +2527,6 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, int intinfo,
 	}
 	if ((endpoint & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN)
 		memcpy(data, buf, *transferred);
-	else if (zlp) {
-#ifdef WIN32
-		/* Send a zero length packet */
-		libusb_bulk_transfer(dev_handle, endpoint, NULL, 0, &dummy, 100);
-#endif
-	}
 
 	return err;
 }