Browse Source

Reliably extract BF1 information at startup and reset the device.

Con Kolivas 12 years ago
parent
commit
977a7b753f
2 changed files with 46 additions and 2 deletions
  1. 40 2
      driver-bitfury.c
  2. 6 0
      driver-bitfury.h

+ 40 - 2
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);
 	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)
 static bool bitfury_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
 {
 {
 	struct cgpu_info *bitfury;
 	struct cgpu_info *bitfury;
+	struct bitfury_info *info;
 	char buf[512];
 	char buf[512];
 	int amount;
 	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,
 	applog(LOG_INFO, "%s%d: Found at %s", bitfury->drv->name,
 	       bitfury->device_id, bitfury->device_path);
 	       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);
 	usb_buffer_enable(bitfury);
+
 	bitfury_open(bitfury);
 	bitfury_open(bitfury);
+
+	/* Send getinfo request */
 	usb_write(bitfury, "I", 1, &amount, C_BFO_REQINFO);
 	usb_write(bitfury, "I", 1, &amount, C_BFO_REQINFO);
 	usb_read(bitfury, buf, 14, &amount, C_BFO_GETINFO);
 	usb_read(bitfury, buf, 14, &amount, C_BFO_GETINFO);
 	if (amount != 14) {
 	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);
 		       bitfury->drv->name, bitfury->device_id, amount);
 		goto out_close;
 		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->device_id, buf);
+	bitfury_empty_buffer(bitfury);
 
 
 	//return true;
 	//return true;
 out_close:
 out_close:

+ 6 - 0
driver-bitfury.h

@@ -12,4 +12,10 @@
 
 
 #include "usbutils.h"
 #include "usbutils.h"
 
 
+struct bitfury_info {
+	uint8_t version;
+	char product[8];
+	uint32_t serial;
+};
+
 #endif /* BITFURY_H */
 #endif /* BITFURY_H */