libztex: Factor out local version of libusb_get_string_descriptor_ascii()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
diff --git a/libztex.c b/libztex.c
index 6136b04..9ae421e 100644
--- a/libztex.c
+++ b/libztex.c
@@ -48,6 +48,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,
@@ -527,7 +568,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;
err = libusb_open(dev, &newdev->hndl);
if (err != LIBUSB_SUCCESS) {
@@ -541,40 +581,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);