|
|
@@ -1,5 +1,5 @@
|
|
|
/**
|
|
|
- * libztex.c - Ztex 1.15x fpga board support library
|
|
|
+ * libztex.c - Ztex 1.15x/1.15y fpga board support library
|
|
|
*
|
|
|
* Copyright (c) 2012 nelisky.btc@gmail.com
|
|
|
* Copyright (c) 2012 Denis Ahrens <denis@h3q.com>
|
|
|
@@ -49,6 +49,47 @@
|
|
|
//* Capability index for multi FPGA support.
|
|
|
#define CAPABILITY_MULTI_FPGA 0,7
|
|
|
|
|
|
+static int libztex_get_string_descriptor_ascii(libusb_device_handle *dev, uint8_t desc_index,
|
|
|
+ unsigned char *data, int length) {
|
|
|
+ int i, cnt;
|
|
|
+ uint16_t langid;
|
|
|
+ unsigned char buf[260];
|
|
|
+
|
|
|
+ /* We open code string descriptor retrieval and ASCII decoding here
|
|
|
+ * in order to work around that libusb_get_string_descriptor_ascii()
|
|
|
+ * in the FreeBSD libusb implementation hits a bug in ZTEX firmware,
|
|
|
+ * where the device returns more bytes than requested, causing babble,
|
|
|
+ * which makes FreeBSD return an error to us.
|
|
|
+ *
|
|
|
+ * Avoid the mess by doing it manually the same way as libusb-1.0.
|
|
|
+ */
|
|
|
+
|
|
|
+ cnt = libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
|
|
|
+ LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | 0,
|
|
|
+ 0x0000, buf, sizeof(buf), 1000);
|
|
|
+ if (cnt < 0) {
|
|
|
+ applog(LOG_ERR, "%s: Failed to read LANGIDs: %s", __func__, libusb_error_name(cnt));
|
|
|
+ return cnt;
|
|
|
+ }
|
|
|
+
|
|
|
+ langid = libusb_le16_to_cpu(((uint16_t *)buf)[1]);
|
|
|
+
|
|
|
+ cnt = libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
|
|
|
+ LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | desc_index,
|
|
|
+ langid, buf, sizeof(buf), 1000);
|
|
|
+ if (cnt < 0) {
|
|
|
+ applog(LOG_ERR, "%s: Failed to read string descriptor: %s", __func__, libusb_error_name(cnt));
|
|
|
+ return cnt;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* num chars = (all bytes except bLength and bDescriptorType) / 2 */
|
|
|
+ for (i = 0; i <= (cnt - 2) / 2 && i < length-1; i++)
|
|
|
+ data[i] = buf[2 + i*2];
|
|
|
+
|
|
|
+ data[i] = 0;
|
|
|
+
|
|
|
+ return LIBUSB_SUCCESS;
|
|
|
+}
|
|
|
|
|
|
enum check_result {
|
|
|
CHECK_ERROR,
|
|
|
@@ -65,7 +106,6 @@ static enum check_result libztex_checkDevice(struct libusb_device *dev)
|
|
|
int i, ret = CHECK_ERROR, err, cnt;
|
|
|
size_t got_bytes, length;
|
|
|
unsigned char buf[64], *fw_buf;
|
|
|
- char *firmware = "ztex_ufm1_15y1.bin";
|
|
|
|
|
|
err = libusb_get_device_descriptor(dev, &desc);
|
|
|
if (unlikely(err != 0)) {
|
|
|
@@ -105,9 +145,8 @@ static enum check_result libztex_checkDevice(struct libusb_device *dev)
|
|
|
switch(buf[7])
|
|
|
{
|
|
|
case 13:
|
|
|
- applog(LOG_ERR, "Found ztex board 1.15d or 1.15x but currently unsupported!");
|
|
|
- ret = CHECK_IS_NOT_ZTEX;
|
|
|
- goto done;
|
|
|
+ applog(LOG_ERR, "Found ztex board 1.15d or 1.15x");
|
|
|
+ break;
|
|
|
case 15:
|
|
|
applog(LOG_ERR, "Found ztex board 1.15y");
|
|
|
break;
|
|
|
@@ -123,17 +162,42 @@ static enum check_result libztex_checkDevice(struct libusb_device *dev)
|
|
|
goto done;
|
|
|
}
|
|
|
|
|
|
- applog(LOG_ERR, "Found dummy firmware, trying to send mining firmware: %s", firmware);
|
|
|
+ applog(LOG_ERR, "Found dummy firmware, trying to send mining firmware");
|
|
|
|
|
|
- // reset 1
|
|
|
- buf[0] = 1;
|
|
|
- cnt = libusb_control_transfer(hndl, 0x40, 0xA0, 0xE600, 0, buf, 1,1000);
|
|
|
- if (cnt < 0)
|
|
|
+ char productString[32];
|
|
|
+
|
|
|
+ cnt = libztex_get_string_descriptor_ascii(hndl, desc.iProduct, productString, sizeof(productString));
|
|
|
+ if (unlikely(cnt < 0)) {
|
|
|
+ applog(LOG_ERR, "Ztex check device: Failed to read device productString with err %d", cnt);
|
|
|
+ return cnt;
|
|
|
+ }
|
|
|
+
|
|
|
+ applog(LOG_ERR, "productString: %s", productString);
|
|
|
+
|
|
|
+ unsigned char productID2 = buf[7];
|
|
|
+ char *firmware = NULL;
|
|
|
+
|
|
|
+ if (strcmp("USB-FPGA Module 1.15d (default)", productString) == 0 && productID2 == 13)
|
|
|
{
|
|
|
- applog(LOG_ERR, "Ztex reset 1 failed: %s", libusb_error_name(cnt));
|
|
|
+ firmware = "ztex_ufm1_15d4.bin";
|
|
|
+ }
|
|
|
+ else if (strcmp("USB-FPGA Module 1.15x (default)", productString) == 0 && productID2 == 13)
|
|
|
+ {
|
|
|
+ firmware = "ztex_ufm1_15d4.bin";
|
|
|
+ }
|
|
|
+ else if (strcmp("USB-FPGA Module 1.15y (default)", productString) == 0 && productID2 == 15)
|
|
|
+ {
|
|
|
+ firmware = "ztex_ufm1_15y1.bin";
|
|
|
+ }
|
|
|
+
|
|
|
+ if (firmware == NULL)
|
|
|
+ {
|
|
|
+ applog(LOG_ERR, "could not figure out which firmware to use");
|
|
|
goto done;
|
|
|
}
|
|
|
|
|
|
+ applog(LOG_ERR, "Mining firmware filename: %s", firmware);
|
|
|
+
|
|
|
fp = open_bitstream("ztex", firmware);
|
|
|
if (!fp) {
|
|
|
applog(LOG_ERR, "failed to open firmware file '%s'", firmware);
|
|
|
@@ -162,6 +226,31 @@ static enum check_result libztex_checkDevice(struct libusb_device *dev)
|
|
|
goto done;
|
|
|
}
|
|
|
|
|
|
+ // in buf[] is still the identifier of the dummy firmware
|
|
|
+ // use it to compare it with the new firmware
|
|
|
+ char *rv = memmem(fw_buf, got_bytes, buf, 8);
|
|
|
+ if (rv == NULL)
|
|
|
+ {
|
|
|
+ applog(LOG_ERR, "%s: found firmware is not ZTEX", __func__);
|
|
|
+ goto done;
|
|
|
+ }
|
|
|
+
|
|
|
+ // check for dummy firmware
|
|
|
+ if (rv[8] == 0)
|
|
|
+ {
|
|
|
+ applog(LOG_ERR, "%s: found a ZTEX dummy firmware", __func__);
|
|
|
+ goto done;
|
|
|
+ }
|
|
|
+
|
|
|
+ // reset 1
|
|
|
+ buf[0] = 1;
|
|
|
+ cnt = libusb_control_transfer(hndl, 0x40, 0xA0, 0xE600, 0, buf, 1,1000);
|
|
|
+ if (cnt < 0)
|
|
|
+ {
|
|
|
+ applog(LOG_ERR, "Ztex reset 1 failed: %s", libusb_error_name(cnt));
|
|
|
+ goto done;
|
|
|
+ }
|
|
|
+
|
|
|
for (i = 0; i < length; i+= 256) {
|
|
|
// firmware wants data in small chunks like 256 bytes
|
|
|
int numbytes = (length - i) < 256 ? (length - i) : 256;
|
|
|
@@ -261,8 +350,7 @@ static int libztex_configureFpgaHS(struct libztex_device *ztex, const char* firm
|
|
|
struct libztex_fpgastate state;
|
|
|
const int transactionBytes = 65536;
|
|
|
unsigned char buf[transactionBytes], settings[2];
|
|
|
- int tries, cnt, buf_p, i;
|
|
|
- ssize_t pos = 0;
|
|
|
+ int tries, cnt, err, i;
|
|
|
FILE *fp;
|
|
|
|
|
|
if (!libztex_checkCapability(ztex, CAPABILITY_HS_FPGA))
|
|
|
@@ -270,7 +358,7 @@ static int libztex_configureFpgaHS(struct libztex_device *ztex, const char* firm
|
|
|
libztex_getFpgaState(ztex, &state);
|
|
|
if (!force && state.fpgaConfigured) {
|
|
|
applog(LOG_INFO, "Bitstream already configured");
|
|
|
- return 1;
|
|
|
+ return 0;
|
|
|
}
|
|
|
cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x33, 0, 0, settings, 2, 1000);
|
|
|
if (unlikely(cnt < 0)) {
|
|
|
@@ -283,56 +371,29 @@ static int libztex_configureFpgaHS(struct libztex_device *ztex, const char* firm
|
|
|
for (tries = 3; tries > 0; tries--) {
|
|
|
fp = open_bitstream("ztex", firmware);
|
|
|
if (!fp) {
|
|
|
- applog(LOG_ERR, "%s: failed to read firmware '%s'", ztex->repr, firmware);
|
|
|
+ applog(LOG_ERR, "%s: failed to read bitstream '%s'", ztex->repr, firmware);
|
|
|
return -2;
|
|
|
}
|
|
|
|
|
|
- while (pos < transactionBytes && !feof(fp)) {
|
|
|
- buf[pos++] = getc(fp);
|
|
|
- }
|
|
|
-
|
|
|
- if (feof(fp))
|
|
|
- pos--;
|
|
|
-
|
|
|
- if (bs != 0 && bs != 1)
|
|
|
- bs = libztex_detectBitstreamBitOrder(buf, transactionBytes < pos? transactionBytes: pos);
|
|
|
-
|
|
|
-
|
|
|
- if (bs == 1)
|
|
|
- libztex_swapBits(buf, pos);
|
|
|
-
|
|
|
libusb_control_transfer(ztex->hndl, 0x40, 0x34, 0, 0, NULL, 0, 1000);
|
|
|
// 0x34 - initHSFPGAConfiguration
|
|
|
|
|
|
- buf_p = pos;
|
|
|
- while (1) {
|
|
|
- i = 0;
|
|
|
- while (i < buf_p) {
|
|
|
- if (libusb_bulk_transfer(ztex->hndl,
|
|
|
- settings[0],
|
|
|
- &buf[i],
|
|
|
- buf_p - i,
|
|
|
- &cnt, 1000) != 0) {
|
|
|
- applog(LOG_ERR, "%s: Failed send hs fpga data", ztex->repr);
|
|
|
- break;
|
|
|
- }
|
|
|
- usleep(500);
|
|
|
- i += cnt;
|
|
|
- }
|
|
|
- if (i < buf_p || buf_p < transactionBytes)
|
|
|
- break;
|
|
|
- buf_p = 0;
|
|
|
- while (buf_p < transactionBytes && !feof(fp)) {
|
|
|
- buf[buf_p++] = getc(fp);
|
|
|
- }
|
|
|
- if (feof(fp))
|
|
|
- buf_p--;
|
|
|
- pos += buf_p;
|
|
|
- if (buf_p == 0)
|
|
|
- break;
|
|
|
+ do
|
|
|
+ {
|
|
|
+ int length = fread(buf,1,transactionBytes,fp);
|
|
|
+
|
|
|
+ if (bs != 0 && bs != 1)
|
|
|
+ bs = libztex_detectBitstreamBitOrder(buf, length);
|
|
|
if (bs == 1)
|
|
|
- libztex_swapBits(buf, buf_p);
|
|
|
+ libztex_swapBits(buf, length);
|
|
|
+
|
|
|
+ err = libusb_bulk_transfer(ztex->hndl, settings[0], buf, length, &cnt, 1000);
|
|
|
+ if (cnt != length)
|
|
|
+ applog(LOG_ERR, "%s: cnt != length", ztex->repr);
|
|
|
+ if (err != 0)
|
|
|
+ applog(LOG_ERR, "%s: Failed send hs fpga data", ztex->repr);
|
|
|
}
|
|
|
+ while (!feof(fp));
|
|
|
|
|
|
libusb_control_transfer(ztex->hndl, 0x40, 0x35, 0, 0, NULL, 0, 1000);
|
|
|
// 0x35 - finishHSFPGAConfiguration
|
|
|
@@ -372,13 +433,13 @@ static int libztex_configureFpgaLS(struct libztex_device *ztex, const char* firm
|
|
|
libztex_getFpgaState(ztex, &state);
|
|
|
if (!force && state.fpgaConfigured) {
|
|
|
applog(LOG_DEBUG, "Bitstream already configured");
|
|
|
- return 1;
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
for (tries = 10; tries > 0; tries--) {
|
|
|
fp = open_bitstream("ztex", firmware);
|
|
|
if (!fp) {
|
|
|
- applog(LOG_ERR, "%s: failed to read firmware '%s'", ztex->repr, firmware);
|
|
|
+ applog(LOG_ERR, "%s: failed to read bitstream '%s'", ztex->repr, firmware);
|
|
|
return -2;
|
|
|
}
|
|
|
|
|
|
@@ -546,7 +607,6 @@ int libztex_prepare_device(struct libusb_device *dev, struct libztex_device** zt
|
|
|
struct libztex_device *newdev = *ztex;
|
|
|
int i, cnt, err;
|
|
|
unsigned char buf[64];
|
|
|
- uint16_t langid;
|
|
|
|
|
|
dclk_prepare(&newdev->dclk);
|
|
|
err = libusb_open(dev, &newdev->hndl);
|
|
|
@@ -561,40 +621,12 @@ int libztex_prepare_device(struct libusb_device *dev, struct libztex_device** zt
|
|
|
return CHECK_ERROR;
|
|
|
}
|
|
|
|
|
|
- /* We open code string descriptor retrieval and ASCII decoding here
|
|
|
- * in order to work around that libusb_get_string_descriptor_ascii()
|
|
|
- * in the FreeBSD libusb implementation hits a bug in ZTEX firmware,
|
|
|
- * where the device returns more bytes than requested, causing babble,
|
|
|
- * which makes FreeBSD return an error to us.
|
|
|
- *
|
|
|
- * Avoid the mess by doing it manually the same way as libusb-1.0.
|
|
|
- */
|
|
|
-
|
|
|
- cnt = libusb_control_transfer(newdev->hndl, LIBUSB_ENDPOINT_IN,
|
|
|
- LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | 0,
|
|
|
- 0x0000, buf, sizeof(buf), 1000);
|
|
|
- if (unlikely(cnt < 0)) {
|
|
|
- applog(LOG_ERR, "Ztex check device: Failed to read device LANGIDs with err %d", cnt);
|
|
|
- return cnt;
|
|
|
- }
|
|
|
-
|
|
|
- langid = libusb_le16_to_cpu(((uint16_t *)buf)[1]);
|
|
|
-
|
|
|
- cnt = libusb_control_transfer(newdev->hndl, LIBUSB_ENDPOINT_IN,
|
|
|
- LIBUSB_REQUEST_GET_DESCRIPTOR,
|
|
|
- (LIBUSB_DT_STRING << 8) | newdev->descriptor.iSerialNumber,
|
|
|
- langid, buf, sizeof(buf), 1000);
|
|
|
+ cnt = libztex_get_string_descriptor_ascii(newdev->hndl, newdev->descriptor.iSerialNumber, newdev->snString, sizeof(newdev->snString));
|
|
|
if (unlikely(cnt < 0)) {
|
|
|
applog(LOG_ERR, "Ztex check device: Failed to read device snString with err %d", cnt);
|
|
|
return cnt;
|
|
|
}
|
|
|
|
|
|
- /* num chars = (all bytes except bLength and bDescriptorType) / 2 */
|
|
|
- for (i = 0; i <= (cnt - 2) / 2 && i < (int)sizeof(newdev->snString)-1; i++)
|
|
|
- newdev->snString[i] = buf[2 + i*2];
|
|
|
-
|
|
|
- newdev->snString[i] = 0;
|
|
|
-
|
|
|
cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x22, 0, 0, buf, 40, 500);
|
|
|
if (unlikely(cnt < 0)) {
|
|
|
applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
|