Commit 977a7b753f0a7cc5e24d57d5366aae02ab8b8415

Con Kolivas 2013-09-24T22:58:10

Reliably extract BF1 information at startup and reset the device.

diff --git a/driver-bitfury.c b/driver-bitfury.c
index 66f35ff..81c3ef0 100644
--- a/driver-bitfury.c
+++ b/driver-bitfury.c
@@ -26,9 +26,20 @@ static void bitfury_close(struct cgpu_info *bitfury)
 	usb_transfer(bitfury, 0x21, 0x22, 0, 0, C_BFO_CLOSE);
 }
 
+static void bitfury_empty_buffer(struct cgpu_info *bitfury)
+{
+	char buf[512];
+	int amount;
+
+	do {
+		usb_read(bitfury, buf, 512, &amount, C_PING);
+	} while (amount);
+}
+
 static bool bitfury_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
 {
 	struct cgpu_info *bitfury;
+	struct bitfury_info *info;
 	char buf[512];
 	int amount;
 
@@ -41,17 +52,44 @@ static bool bitfury_detect_one(struct libusb_device *dev, struct usb_find_device
 	applog(LOG_INFO, "%s%d: Found at %s", bitfury->drv->name,
 	       bitfury->device_id, bitfury->device_path);
 
+	info = calloc(sizeof(struct bitfury_info), 1);
+	if (!info)
+		quit(1, "Failed to calloc info in bitfury_detect_one");
+	bitfury->device_data = info;
+
+	bitfury_empty_buffer(bitfury);
 	usb_buffer_enable(bitfury);
+
 	bitfury_open(bitfury);
+
+	/* Send getinfo request */
 	usb_write(bitfury, "I", 1, &amount, C_BFO_REQINFO);
 	usb_read(bitfury, buf, 14, &amount, C_BFO_GETINFO);
 	if (amount != 14) {
-		applog(LOG_WARNING, "%s%d: Getinfo received %d",
+		applog(LOG_WARNING, "%s%d: Getinfo received %d bytes",
+		       bitfury->drv->name, bitfury->device_id, amount);
+		goto out_close;
+	}
+	info->version = buf[1];
+	memcpy(&info->product, buf + 2, 8);
+	memcpy(&info->serial, buf + 10, 4);
+
+	applog(LOG_INFO, "%s%d: Getinfo returned version %d, product %s serial %08x", bitfury->drv->name,
+	       bitfury->device_id, info->version, info->product, info->serial);
+	bitfury_empty_buffer(bitfury);
+
+	/* Send reset request */
+	usb_write(bitfury, "R", 1, &amount, C_BFO_REQRESET);
+	usb_read_timeout(bitfury, buf, 7, &amount, 1000, C_BFO_GETRESET);
+
+	if (amount != 7) {
+		applog(LOG_WARNING, "%s%d: Getreset received %d bytes",
 		       bitfury->drv->name, bitfury->device_id, amount);
 		goto out_close;
 	}
-	applog(LOG_INFO, "%s%d: Getinfo returned %s", bitfury->drv->name,
+	applog(LOG_INFO, "%s%d: Getreset returned %s", bitfury->drv->name,
 	       bitfury->device_id, buf);
+	bitfury_empty_buffer(bitfury);
 
 	//return true;
 out_close:
diff --git a/driver-bitfury.h b/driver-bitfury.h
index 52592bb..c427fdc 100644
--- a/driver-bitfury.h
+++ b/driver-bitfury.h
@@ -12,4 +12,10 @@
 
 #include "usbutils.h"
 
+struct bitfury_info {
+	uint8_t version;
+	char product[8];
+	uint32_t serial;
+};
+
 #endif /* BITFURY_H */