Commit c1c834a697cdeb403bf0c8b59c563222f95d1c82

Con Kolivas 2013-11-14T11:58:21

Create and destroy a basic bxf read thread.

diff --git a/driver-bitfury.c b/driver-bitfury.c
index 993ec7d..eec35dc 100644
--- a/driver-bitfury.c
+++ b/driver-bitfury.c
@@ -232,6 +232,54 @@ static void bitfury_detect(bool __maybe_unused hotplug)
 	usb_detect(&bitfury_drv, bitfury_detect_one);
 }
 
+static void *bxf_get_results(void *userdata)
+{
+	struct cgpu_info *bitfury = userdata;
+	struct bitfury_info *info = bitfury->device_data;
+	char threadname[24];
+
+	snprintf(threadname, 24, "bxf_recv/%d", bitfury->device_id);
+
+	while (likely(!bitfury->shutdown)) {
+		int err, amount;
+		char buf[512];
+
+		if (unlikely(bitfury->usbinfo.nodev))
+			break;
+
+		err = usb_read_nl(bitfury, buf, 512, &amount, C_BXF_READ);
+		if (err) {
+			if (err != LIBUSB_ERROR_TIMEOUT)
+				break;
+			continue;
+		}
+	}
+	return NULL;
+}
+
+static bool bxf_prepare(struct cgpu_info *bitfury, struct bitfury_info *info)
+{
+	mutex_init(&info->lock);
+	if (pthread_create(&info->read_thr, NULL, bxf_get_results, (void *)bitfury))
+		quit(1, "Failed to create bxf read_thr");
+	return true;
+}
+
+static bool bitfury_prepare(struct thr_info *thr)
+{
+	struct cgpu_info *bitfury = thr->cgpu;
+	struct bitfury_info *info = bitfury->device_data;
+
+	switch(info->ident) {
+		case IDENT_BXF:
+			return bxf_prepare(bitfury, info);
+			break;
+		case IDENT_BF1:
+		default:
+			return true;
+	}
+}
+
 static uint32_t decnonce(uint32_t in)
 {
 	uint32_t out;
@@ -448,6 +496,12 @@ static void bitfury_init(struct cgpu_info *bitfury)
 	}
 }
 
+static void bxf_close(struct bitfury_info *info)
+{
+	pthread_join(info->read_thr, NULL);
+	mutex_destroy(&info->lock);
+}
+
 static void bitfury_shutdown(struct thr_info *thr)
 {
 	struct cgpu_info *bitfury = thr->cgpu;
@@ -458,6 +512,8 @@ static void bitfury_shutdown(struct thr_info *thr)
 			bf1_close(bitfury);
 			break;
 		case IDENT_BXF:
+			bxf_close(info);
+			break;
 		default:
 			break;
 	}
@@ -469,6 +525,7 @@ struct device_drv bitfury_drv = {
 	.dname = "bitfury",
 	.name = "BF1",
 	.drv_detect = bitfury_detect,
+	.thread_prepare = bitfury_prepare,
 	.hash_work = &hash_driver_work,
 	.scanwork = bitfury_scanwork,
 	.get_api_stats = bitfury_api_stats,
diff --git a/driver-bitfury.h b/driver-bitfury.h
index dc16e50..cb75341 100644
--- a/driver-bitfury.h
+++ b/driver-bitfury.h
@@ -26,6 +26,10 @@ struct bitfury_info {
 	char product[8];
 	uint32_t serial;
 	struct timeval tv_start;
+
+	/* BXF specific data */
+	pthread_mutex_t lock;
+	pthread_t read_thr;
 };
 
 #endif /* BITFURY_H */
diff --git a/usbutils.h b/usbutils.h
index 3fbc9cb..2ab4733 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -343,6 +343,7 @@ struct cg_usb_info {
 	USB_ADD_COMMAND(C_BF1_FLUSH, "BF1Flush") \
 	USB_ADD_COMMAND(C_BF1_IFLUSH, "BF1InterruptFlush") \
 	USB_ADD_COMMAND(C_BF1_IDENTIFY, "BF1Identify") \
+	USB_ADD_COMMAND(C_BXF_READ, "BXFRead") \
 	USB_ADD_COMMAND(C_HF_RESET, "HFReset") \
 	USB_ADD_COMMAND(C_HF_PLL_CONFIG, "HFPLLConfig") \
 	USB_ADD_COMMAND(C_HF_ADDRESS, "HFAddress") \