usbutils - make FTDI handling automatic
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
diff --git a/driver-bflsc.c b/driver-bflsc.c
index 0e8c57d..a532e0d 100644
--- a/driver-bflsc.c
+++ b/driver-bflsc.c
@@ -487,7 +487,7 @@ static bool getok(struct cgpu_info *bflsc, enum usb_cmds cmd, int *err, int *amo
{
char buf[BFLSC_BUFSIZ+1];
- *err = usb_ftdi_read_nl(bflsc, buf, sizeof(buf)-1, amount, cmd);
+ *err = usb_read_nl(bflsc, buf, sizeof(buf)-1, amount, cmd);
if (*err < 0 || *amount < (int)BFLSC_OK_LEN)
return false;
else
@@ -496,7 +496,7 @@ static bool getok(struct cgpu_info *bflsc, enum usb_cmds cmd, int *err, int *amo
static bool getokerr(struct cgpu_info *bflsc, enum usb_cmds cmd, int *err, int *amount, char *buf, size_t bufsiz)
{
- *err = usb_ftdi_read_nl(bflsc, buf, bufsiz-1, amount, cmd);
+ *err = usb_read_nl(bflsc, buf, bufsiz-1, amount, cmd);
if (*err < 0 || *amount < (int)BFLSC_OK_LEN)
return false;
else {
@@ -529,7 +529,7 @@ static void bflsc_send_flush_work(struct cgpu_info *bflsc, int dev)
}
}
-/* return True = attempted usb_ftdi_read_ok()
+/* return True = attempted usb_read_ok()
* set ignore to true means no applog/ignore errors */
static bool bflsc_qres(struct cgpu_info *bflsc, char *buf, size_t bufsiz, int dev, int *err, int *amount, bool ignore)
{
@@ -550,7 +550,7 @@ static bool bflsc_qres(struct cgpu_info *bflsc, char *buf, size_t bufsiz, int de
// of course all other I/O must also be failing ...
} else {
readok = true;
- *err = usb_ftdi_read_ok(bflsc, buf, bufsiz-1, amount, C_GETRESULTS);
+ *err = usb_read_ok(bflsc, buf, bufsiz-1, amount, C_GETRESULTS);
mutex_unlock(&(bflsc->device_mutex));
if (*err < 0 || *amount < 1) {
@@ -695,7 +695,7 @@ static bool getinfo(struct cgpu_info *bflsc, int dev)
return false;
}
- err = usb_ftdi_read_ok(bflsc, buf, sizeof(buf)-1, &amount, C_GETDETAILS);
+ err = usb_read_ok(bflsc, buf, sizeof(buf)-1, &amount, C_GETDETAILS);
if (err < 0 || amount < 1) {
if (err < 0) {
applog(LOG_ERR, "%s detect (%s) get details return invalid/timed out (%d:%d)",
@@ -830,7 +830,7 @@ reinit:
goto unshin;
}
- err = usb_ftdi_read_nl(bflsc, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY);
+ err = usb_read_nl(bflsc, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY);
if (err < 0 || amount < 1) {
init_count++;
cgtime(&init_now);
@@ -1095,7 +1095,7 @@ static bool bflsc_get_temp(struct cgpu_info *bflsc, int dev)
return false;
}
- err = usb_ftdi_read_nl(bflsc, temp_buf, sizeof(temp_buf)-1, &amount, C_GETTEMPERATURE);
+ err = usb_read_nl(bflsc, temp_buf, sizeof(temp_buf)-1, &amount, C_GETTEMPERATURE);
if (err < 0 || amount < 1) {
mutex_unlock(&(bflsc->device_mutex));
if (err < 0) {
@@ -1117,7 +1117,7 @@ static bool bflsc_get_temp(struct cgpu_info *bflsc, int dev)
return false;
}
- err = usb_ftdi_read_nl(bflsc, volt_buf, sizeof(volt_buf)-1, &amount, C_GETTEMPERATURE);
+ err = usb_read_nl(bflsc, volt_buf, sizeof(volt_buf)-1, &amount, C_GETTEMPERATURE);
if (err < 0 || amount < 1) {
mutex_unlock(&(bflsc->device_mutex));
if (err < 0) {
diff --git a/driver-bitforce.c b/driver-bitforce.c
index f703edc..7b4361b 100644
--- a/driver-bitforce.c
+++ b/driver-bitforce.c
@@ -200,7 +200,7 @@ reinit:
goto unshin;
}
- if ((err = usb_ftdi_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY)) < 0 || amount < 1) {
+ if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY)) < 0 || amount < 1) {
init_count++;
cgtime(&init_now);
if (us_tdiff(&init_now, &init_start) <= REINIT_TIME_MAX) {
@@ -395,7 +395,7 @@ static bool bitforce_get_temp(struct cgpu_info *bitforce)
return false;
}
- if ((err = usb_ftdi_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETTEMPERATURE)) < 0 || amount < 1) {
+ if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETTEMPERATURE)) < 0 || amount < 1) {
mutex_unlock(&bitforce->device_mutex);
if (err < 0) {
applog(LOG_ERR, "%s%i: Error: Get temp return invalid/timed out (%d:%d)",
@@ -470,7 +470,7 @@ re_send:
return false;
}
- if ((err = usb_ftdi_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_REQUESTSENDWORKSTATUS)) < 0) {
+ if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_REQUESTSENDWORKSTATUS)) < 0) {
mutex_unlock(&bitforce->device_mutex);
applog(LOG_ERR, "%s%d: read request send work status failed (%d:%d)",
bitforce->drv->name, bitforce->device_id, amount, err);
@@ -524,7 +524,7 @@ re_send:
return false;
}
- if ((err = usb_ftdi_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_SENDWORKSTATUS)) < 0) {
+ if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_SENDWORKSTATUS)) < 0) {
mutex_unlock(&bitforce->device_mutex);
applog(LOG_ERR, "%s%d: read send work status failed (%d:%d)",
bitforce->drv->name, bitforce->device_id, amount, err);
@@ -573,7 +573,7 @@ static int64_t bitforce_get_result(struct thr_info *thr, struct work *work)
mutex_lock(&bitforce->device_mutex);
usb_write(bitforce, BITFORCE_WORKSTATUS, BITFORCE_WORKSTATUS_LEN, &amount, C_REQUESTWORKSTATUS);
- usb_ftdi_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETWORKSTATUS);
+ usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETWORKSTATUS);
mutex_unlock(&bitforce->device_mutex);
cgtime(&now);
diff --git a/usbutils.c b/usbutils.c
index f010960..2d286d1 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -115,13 +115,15 @@ static struct usb_endpoints cmr_eps[] = {
};
#endif
+#define IDVENDOR_FTDI 0x0403
+
// TODO: Add support for (at least) Isochronous endpoints
static struct usb_find_devices find_dev[] = {
#ifdef USE_BFLSC
{
.drv = DRV_BFLSC,
.name = "BAS",
- .idVendor = 0x0403,
+ .idVendor = IDVENDOR_FTDI,
.idProduct = 0x6014,
.iManufacturer = "Butterfly Labs",
.iProduct = "BitFORCE SHA256 SC",
@@ -136,7 +138,7 @@ static struct usb_find_devices find_dev[] = {
{
.drv = DRV_BITFORCE,
.name = "BFL",
- .idVendor = 0x0403,
+ .idVendor = IDVENDOR_FTDI,
.idProduct = 0x6014,
.iManufacturer = "Butterfly Labs Inc.",
.iProduct = "BitFORCE SHA256",
@@ -164,7 +166,7 @@ static struct usb_find_devices find_dev[] = {
{
.drv = DRV_AVALON,
.name = "AVA",
- .idVendor = 0x0403,
+ .idVendor = IDVENDOR_FTDI,
.idProduct = 0x6001,
.kernel = 0,
.config = 1,
@@ -199,7 +201,7 @@ static struct usb_find_devices find_dev[] = {
{
.drv = DRV_ICARUS,
.name = "BLT",
- .idVendor = 0x0403,
+ .idVendor = IDVENDOR_FTDI,
.idProduct = 0x6001,
.iProduct = "FT232R USB UART",
.kernel = 0,
@@ -212,7 +214,7 @@ static struct usb_find_devices find_dev[] = {
{
.drv = DRV_ICARUS,
.name = "LLT",
- .idVendor = 0x0403,
+ .idVendor = IDVENDOR_FTDI,
.idProduct = 0x6001,
.kernel = 0,
.config = 1,
@@ -1330,6 +1332,9 @@ static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct u
cgusb = calloc(1, sizeof(*cgusb));
cgusb->found = found;
+ if (found->idVendor == IDVENDOR_FTDI)
+ cgusb->usb_type = USB_TYPE_FTDI;
+
cgusb->descriptor = calloc(1, sizeof(*(cgusb->descriptor)));
err = libusb_get_device_descriptor(dev, cgusb->descriptor);
@@ -1911,9 +1916,10 @@ static void rejected_inc(struct cgpu_info *cgpu)
#define USB_MAX_READ 8192
-int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool ftdi, bool readonce)
+int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool readonce)
{
struct cg_usb_device *usbdev = cgpu->usbdev;
+ bool ftdi = (usbdev->usb_type == USB_TYPE_FTDI);
#if DO_USB_STATS
struct timeval tv_start;
#endif
diff --git a/usbutils.h b/usbutils.h
index 2c4f642..64a4546 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -65,12 +65,18 @@ struct usb_find_devices {
struct usb_endpoints *eps;
};
+enum usb_types {
+ USB_TYPE_STD = 0,
+ USB_TYPE_FTDI
+};
+
struct cg_usb_device {
struct usb_find_devices *found;
libusb_device_handle *handle;
pthread_mutex_t *mutex;
struct libusb_device_descriptor *descriptor;
uint16_t usbver;
+ enum usb_types usb_type;
int speed;
char *prod_string;
char *manuf_string;
@@ -150,29 +156,32 @@ bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find
void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *));
struct api_data *api_usb_stats(int *count);
void update_usb_stats(struct cgpu_info *cgpu);
-int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool ftdi, bool readonce);
+int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool readonce);
int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds);
int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned int timeout, enum usb_cmds cmd);
void usb_cleanup();
void usb_initialise();
#define usb_read(cgpu, buf, bufsiz, read, cmd) \
- _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, false)
+ _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false)
#define usb_read_once(cgpu, buf, bufsiz, read, cmd) \
- _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, true)
+ _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, true)
#define usb_read_nl(cgpu, buf, bufsiz, read, cmd) \
- _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, false, false)
+ _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, false)
+
+#define usb_read_ok(cgpu, buf, bufsiz, read, cmd) \
+ _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "OK\n", cmd, false)
#define usb_read_ep(cgpu, ep, buf, bufsiz, read, cmd) \
- _usb_read(cgpu, ep, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, false)
+ _usb_read(cgpu, ep, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false)
#define usb_read_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
- _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, false, false)
+ _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, false)
#define usb_read_ep_timeout(cgpu, ep, buf, bufsiz, read, timeout, cmd) \
- _usb_read(cgpu, ep, buf, bufsiz, read, timeout, NULL, cmd, false, false)
+ _usb_read(cgpu, ep, buf, bufsiz, read, timeout, NULL, cmd, false)
#define usb_write(cgpu, buf, bufsiz, wrote, cmd) \
_usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
@@ -186,15 +195,6 @@ void usb_initialise();
#define usb_write_ep_timeout(cgpu, ep, buf, bufsiz, wrote, timeout, cmd) \
_usb_write(cgpu, ep, buf, bufsiz, wrote, timeout, cmd)
-#define usb_ftdi_read(cgpu, buf, bufsiz, read, cmd) \
- _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, true, false)
-
-#define usb_ftdi_read_nl(cgpu, buf, bufsiz, read, cmd) \
- _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, true, false)
-
-#define usb_ftdi_read_ok(cgpu, buf, bufsiz, read, cmd) \
- _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "OK\n", cmd, true, false)
-
#define usb_transfer(cgpu, typ, req, val, idx, cmd) \
_usb_transfer(cgpu, typ, req, val, idx, DEVTIMEOUT, cmd)