Merge pull request #456 from kanoi/master cps and usbdev locking
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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
diff --git a/FPGA-README b/FPGA-README
index de12f5c..9f9c4ab 100644
--- a/FPGA-README
+++ b/FPGA-README
@@ -9,7 +9,7 @@ When mining on windows, the driver being used will determine if mining will work
If the driver doesn't allow mining, you will get a "USB init," error message
i.e. one of:
- open device failed, err %d, you need to install a Windows USB driver for the device
+ open device failed, err %d, you need to install a WinUSB driver for the device
or
claim interface %d failed, err %d
diff --git a/api.c b/api.c
index 31240dd..8c29bf9 100644
--- a/api.c
+++ b/api.c
@@ -3023,10 +3023,10 @@ static int itemstats(struct io_data *io_data, int i, char *id, struct cgminer_st
if (cgpu) {
#ifdef USE_USBUTILS
- char pipe_details[128];
+ char details[128];
if (cgpu->usbinfo.pipe_count)
- snprintf(pipe_details, sizeof(pipe_details),
+ snprintf(details, sizeof(details),
"%"PRIu64" %"PRIu64"/%"PRIu64"/%"PRIu64" %lu",
cgpu->usbinfo.pipe_count,
cgpu->usbinfo.clear_err_count,
@@ -3034,9 +3034,18 @@ static int itemstats(struct io_data *io_data, int i, char *id, struct cgminer_st
cgpu->usbinfo.clear_fail_count,
(unsigned long)(cgpu->usbinfo.last_pipe));
else
- strcpy(pipe_details, "0");
+ strcpy(details, "0");
- root = api_add_string(root, "USB Pipe", pipe_details, true);
+ root = api_add_string(root, "USB Pipe", details, true);
+
+ snprintf(details, sizeof(details),
+ "r%"PRIu64" %.6f w%"PRIu64" %.6f",
+ cgpu->usbinfo.read_delay_count,
+ cgpu->usbinfo.total_read_delay,
+ cgpu->usbinfo.write_delay_count,
+ cgpu->usbinfo.total_write_delay);
+
+ root = api_add_string(root, "USB Delay", details, true);
#endif
}
diff --git a/driver-bflsc.c b/driver-bflsc.c
index 21b8215..67023f0 100644
--- a/driver-bflsc.c
+++ b/driver-bflsc.c
@@ -157,8 +157,8 @@ struct bflsc_info {
struct DataForwardToChain {
uint8_t header;
- uint8_t deviceAddress;
uint8_t payloadSize;
+ uint8_t deviceAddress;
uint8_t payloadData[BFLSC_MAXPAYLOAD];
};
diff --git a/driver-icarus.c b/driver-icarus.c
index bfa78c0..b6e2817 100644
--- a/driver-icarus.c
+++ b/driver-icarus.c
@@ -251,18 +251,28 @@ static void _transfer(struct cgpu_info *icarus, uint8_t request_type, uint8_t bR
static void icarus_initialise(struct cgpu_info *icarus, int baud)
{
uint16_t wValue, wIndex;
+ enum sub_ident ident;
+ int interface;
if (icarus->usbinfo.nodev)
return;
- switch (icarus->usbdev->ident) {
+ usb_set_cps(icarus, baud / 10);
+ usb_enable_cps(icarus);
+
+ interface = usb_interface(icarus);
+ ident = usb_ident(icarus);
+
+ switch (ident) {
case IDENT_BLT:
case IDENT_LLT:
case IDENT_CMR1:
case IDENT_CMR2:
+ usb_set_pps(icarus, BLT_PREF_PACKET);
+
// Reset
transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_RESET,
- icarus->usbdev->found->interface, C_RESET);
+ interface, C_RESET);
if (icarus->usbinfo.nodev)
return;
@@ -275,7 +285,7 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud)
// Set data control
transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_DATA, FTDI_VALUE_DATA_BLT,
- icarus->usbdev->found->interface, C_SETDATA);
+ interface, C_SETDATA);
if (icarus->usbinfo.nodev)
return;
@@ -284,8 +294,7 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud)
wValue = FTDI_VALUE_BAUD_BLT;
wIndex = FTDI_INDEX_BAUD_BLT;
- if (icarus->usbdev->ident == IDENT_CMR1 ||
- icarus->usbdev->ident == IDENT_CMR2) {
+ if (ident == IDENT_CMR1 || ident == IDENT_CMR2) {
switch (baud) {
case 115200:
wValue = FTDI_VALUE_BAUD_CMR_115;
@@ -303,43 +312,42 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud)
// Set the baud
transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, wValue,
- (wIndex & 0xff00) | icarus->usbdev->found->interface,
- C_SETBAUD);
+ (wIndex & 0xff00) | interface, C_SETBAUD);
if (icarus->usbinfo.nodev)
return;
// Set Modem Control
transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM, FTDI_VALUE_MODEM,
- icarus->usbdev->found->interface, C_SETMODEM);
+ interface, C_SETMODEM);
if (icarus->usbinfo.nodev)
return;
// Set Flow Control
transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW, FTDI_VALUE_FLOW,
- icarus->usbdev->found->interface, C_SETFLOW);
+ interface, C_SETFLOW);
if (icarus->usbinfo.nodev)
return;
// Clear any sent data
transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_PURGE_TX,
- icarus->usbdev->found->interface, C_PURGETX);
+ interface, C_PURGETX);
if (icarus->usbinfo.nodev)
return;
// Clear any received data
transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_PURGE_RX,
- icarus->usbdev->found->interface, C_PURGERX);
-
- icarus->usbdev->PrefPacketSize = BLT_PREF_PACKET;
+ interface, C_PURGERX);
break;
case IDENT_ICA:
+ usb_set_pps(icarus, ICA_PREF_PACKET);
+
// Set Data Control
transfer(icarus, PL2303_CTRL_OUT, PL2303_REQUEST_CTRL, PL2303_VALUE_CTRL,
- icarus->usbdev->found->interface, C_SETDATA);
+ interface, C_SETDATA);
if (icarus->usbinfo.nodev)
return;
@@ -347,30 +355,29 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud)
// Set Line Control
uint32_t ica_data[2] = { PL2303_VALUE_LINE0, PL2303_VALUE_LINE1 };
_transfer(icarus, PL2303_CTRL_OUT, PL2303_REQUEST_LINE, PL2303_VALUE_LINE,
- icarus->usbdev->found->interface,
- &ica_data[0], PL2303_VALUE_LINE_SIZE, C_SETLINE);
+ interface, &ica_data[0], PL2303_VALUE_LINE_SIZE, C_SETLINE);
if (icarus->usbinfo.nodev)
return;
// Vendor
transfer(icarus, PL2303_VENDOR_OUT, PL2303_REQUEST_VENDOR, PL2303_VALUE_VENDOR,
- icarus->usbdev->found->interface, C_VENDOR);
-
- icarus->usbdev->PrefPacketSize = ICA_PREF_PACKET;
+ interface, C_VENDOR);
break;
case IDENT_AMU:
+ usb_set_pps(icarus, AMU_PREF_PACKET);
+
// Enable the UART
transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_IFC_ENABLE,
CP210X_VALUE_UART_ENABLE,
- icarus->usbdev->found->interface, C_ENABLE_UART);
+ interface, C_ENABLE_UART);
if (icarus->usbinfo.nodev)
return;
// Set data control
transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_DATA, CP210X_VALUE_DATA,
- icarus->usbdev->found->interface, C_SETDATA);
+ interface, C_SETDATA);
if (icarus->usbinfo.nodev)
return;
@@ -378,15 +385,11 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud)
// Set the baud
uint32_t data = CP210X_DATA_BAUD;
_transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_BAUD, 0,
- icarus->usbdev->found->interface,
- &data, sizeof(data), C_SETBAUD);
-
- icarus->usbdev->PrefPacketSize = AMU_PREF_PACKET;
+ interface, &data, sizeof(data), C_SETBAUD);
break;
default:
quit(1, "icarus_intialise() called with invalid %s cgid %i ident=%d",
- icarus->drv->name, icarus->cgminer_id,
- icarus->usbdev->ident);
+ icarus->drv->name, icarus->cgminer_id, ident);
}
}
@@ -480,6 +483,7 @@ static const char *timing_mode_str(enum timing_mode timing_mode)
static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
{
struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
+ enum sub_ident ident;
double Hs;
char buf[BUFSIZ+1];
char *ptr, *comma, *eq;
@@ -509,7 +513,8 @@ static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
buf[max] = '\0';
}
- switch (icarus->usbdev->ident) {
+ ident = usb_ident(icarus);
+ switch (ident) {
case IDENT_ICA:
info->Hs = ICARUS_REV3_HASH_TIME;
break;
@@ -527,7 +532,7 @@ static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
break;
default:
quit(1, "Icarus get_options() called with invalid %s ident=%d",
- icarus->drv->name, icarus->usbdev->ident);
+ icarus->drv->name, ident);
}
info->read_time = 0;
@@ -613,6 +618,7 @@ static void get_options(int this_option_offset, struct cgpu_info *icarus, int *b
{
char buf[BUFSIZ+1];
char *ptr, *comma, *colon, *colon2;
+ enum sub_ident ident;
size_t max;
int i, tmp;
@@ -639,7 +645,8 @@ static void get_options(int this_option_offset, struct cgpu_info *icarus, int *b
buf[max] = '\0';
}
- switch (icarus->usbdev->ident) {
+ ident = usb_ident(icarus);
+ switch (ident) {
case IDENT_ICA:
case IDENT_BLT:
case IDENT_LLT:
@@ -661,7 +668,7 @@ static void get_options(int this_option_offset, struct cgpu_info *icarus, int *b
break;
default:
quit(1, "Icarus get_options() called with invalid %s ident=%d",
- icarus->drv->name, icarus->usbdev->ident);
+ icarus->drv->name, ident);
}
if (*buf) {
diff --git a/driver-modminer.c b/driver-modminer.c
index 4baeeff..50488e8 100644
--- a/driver-modminer.c
+++ b/driver-modminer.c
@@ -128,6 +128,9 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic
if (!usb_init(modminer, dev, found))
goto shin;
+ usb_set_cps(modminer, 11520);
+ usb_enable_cps(modminer);
+
do_ping(modminer);
if ((err = usb_write(modminer, MODMINER_GET_VERSION, 1, &amount, C_REQUESTVERSION)) < 0 || amount != 1) {
diff --git a/usbutils.c b/usbutils.c
index 620a108..2f861c9 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -2312,6 +2312,24 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
}
got = 0;
+ if (first && usbdev->usecps) {
+ if (usbdev->last_write_tv.tv_sec && usbdev->last_write_siz) {
+ struct timeval now;
+ double need;
+
+ cgtime(&now);
+ need = (double)(usbdev->last_write_siz) /
+ (double)(usbdev->cps) -
+ tdiff(&now, &(usbdev->last_write_tv));
+
+ // Simple error condition check/avoidance '< 1.0'
+ if (need > 0.0 && need < 1.0) {
+ cgpu->usbinfo.read_delay_count++;
+ cgpu->usbinfo.total_read_delay += need;
+ nmsleep((unsigned int)(need * 1000.0));
+ }
+ }
+ }
STATS_TIMEVAL(&tv_start);
err = usb_bulk_transfer(usbdev->handle,
usbdev->found->eps[ep].ep,
@@ -2403,6 +2421,24 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
usbbufread = bufleft;
}
got = 0;
+ if (first && usbdev->usecps) {
+ if (usbdev->last_write_tv.tv_sec && usbdev->last_write_siz) {
+ struct timeval now;
+ double need;
+
+ cgtime(&now);
+ need = (double)(usbdev->last_write_siz) /
+ (double)(usbdev->cps) -
+ tdiff(&now, &(usbdev->last_write_tv));
+
+ // Simple error condition check/avoidance '< 1.0'
+ if (need > 0.0 && need < 1.0) {
+ cgpu->usbinfo.read_delay_count++;
+ cgpu->usbinfo.total_read_delay += need;
+ nmsleep((unsigned int)(need * 1000.0));
+ }
+ }
+ }
STATS_TIMEVAL(&tv_start);
err = usb_bulk_transfer(usbdev->handle,
usbdev->found->eps[ep].ep, ptr,
@@ -2531,6 +2567,26 @@ int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pr
cgtime(&read_start);
while (bufsiz > 0) {
sent = 0;
+ if (usbdev->usecps) {
+ if (usbdev->last_write_tv.tv_sec && usbdev->last_write_siz) {
+ struct timeval now;
+ double need;
+
+ cgtime(&now);
+ need = (double)(usbdev->last_write_siz) /
+ (double)(usbdev->cps) -
+ tdiff(&now, &(usbdev->last_write_tv));
+
+ // Simple error condition check/avoidance '< 1.0'
+ if (need > 0.0 && need < 1.0) {
+ cgpu->usbinfo.write_delay_count++;
+ cgpu->usbinfo.total_write_delay += need;
+ nmsleep((unsigned int)(need * 1000.0));
+ }
+ }
+ cgtime(&(usbdev->last_write_tv));
+ usbdev->last_write_siz = bufsiz;
+ }
STATS_TIMEVAL(&tv_start);
err = usb_bulk_transfer(usbdev->handle,
usbdev->found->eps[ep].ep,
@@ -2608,6 +2664,26 @@ int __usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bReques
USBDEBUG("USB debug: @_usb_transfer() buf=%s", bin2hex((unsigned char *)buf, (size_t)siz));
+ if (usbdev->usecps) {
+ if (usbdev->last_write_tv.tv_sec && usbdev->last_write_siz) {
+ struct timeval now;
+ double need;
+
+ cgtime(&now);
+ need = (double)(usbdev->last_write_siz) /
+ (double)(usbdev->cps) -
+ tdiff(&now, &(usbdev->last_write_tv));
+
+ // Simple error condition check/avoidance '< 1.0'
+ if (need > 0.0 && need < 1.0) {
+ cgpu->usbinfo.write_delay_count++;
+ cgpu->usbinfo.total_write_delay += need;
+ nmsleep((unsigned int)(need * 1000.0));
+ }
+ }
+ cgtime(&(usbdev->last_write_tv));
+ usbdev->last_write_siz = siz;
+ }
STATS_TIMEVAL(&tv_start);
cg_rlock(&cgusb_fd_lock);
err = libusb_control_transfer(usbdev->handle, request_type,
@@ -2665,6 +2741,24 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe
*amount = 0;
+ if (usbdev->usecps) {
+ if (usbdev->last_write_tv.tv_sec && usbdev->last_write_siz) {
+ struct timeval now;
+ double need;
+
+ cgtime(&now);
+ need = (double)(usbdev->last_write_siz) /
+ (double)(usbdev->cps) -
+ tdiff(&now, &(usbdev->last_write_tv));
+
+ // Simple error condition check/avoidance '< 1.0'
+ if (need > 0.0 && need < 1.0) {
+ cgpu->usbinfo.read_delay_count++;
+ cgpu->usbinfo.total_read_delay += need;
+ nmsleep((unsigned int)(need * 1000.0));
+ }
+ }
+ }
STATS_TIMEVAL(&tv_start);
cg_rlock(&cgusb_fd_lock);
err = libusb_control_transfer(usbdev->handle, request_type,
@@ -2814,6 +2908,89 @@ uint32_t usb_buffer_size(struct cgpu_info *cgpu)
return ret;
}
+void usb_set_cps(struct cgpu_info *cgpu, int cps)
+{
+ int pstate;
+
+ DEVLOCK(cgpu, pstate);
+
+ if (cgpu->usbdev)
+ cgpu->usbdev->cps = cps;
+
+ DEVUNLOCK(cgpu, pstate);
+}
+
+void usb_enable_cps(struct cgpu_info *cgpu)
+{
+ int pstate;
+
+ DEVLOCK(cgpu, pstate);
+
+ if (cgpu->usbdev)
+ cgpu->usbdev->usecps = true;
+
+ DEVUNLOCK(cgpu, pstate);
+}
+
+void usb_disable_cps(struct cgpu_info *cgpu)
+{
+ int pstate;
+
+ DEVLOCK(cgpu, pstate);
+
+ if (cgpu->usbdev)
+ cgpu->usbdev->usecps = false;
+
+ DEVUNLOCK(cgpu, pstate);
+}
+
+/*
+ * The value returned (0) when usbdev is NULL
+ * doesn't matter since it also means the next call to
+ * any usbutils function will fail with a nodev
+ */
+int usb_interface(struct cgpu_info *cgpu)
+{
+ int interface = 0;
+ int pstate;
+
+ DEVLOCK(cgpu, pstate);
+
+ if (cgpu->usbdev)
+ interface = cgpu->usbdev->found->interface;
+
+ DEVUNLOCK(cgpu, pstate);
+
+ return interface;
+}
+
+enum sub_ident usb_ident(struct cgpu_info *cgpu)
+{
+ enum sub_ident ident = IDENT_UNK;
+ int pstate;
+
+ DEVLOCK(cgpu, pstate);
+
+ if (cgpu->usbdev)
+ ident = cgpu->usbdev->ident;
+
+ DEVUNLOCK(cgpu, pstate);
+
+ return ident;
+}
+
+void usb_set_pps(struct cgpu_info *cgpu, uint16_t PrefPacketSize)
+{
+ int pstate;
+
+ DEVLOCK(cgpu, pstate);
+
+ if (cgpu->usbdev)
+ cgpu->usbdev->PrefPacketSize = PrefPacketSize;
+
+ DEVUNLOCK(cgpu, pstate);
+}
+
// Need to set all devices with matching usbdev
void usb_set_dev_start(struct cgpu_info *cgpu)
{
diff --git a/usbutils.h b/usbutils.h
index 0b8406a..151d0a3 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -168,6 +168,7 @@ struct cg_usb_device {
enum sub_ident ident;
uint16_t usbver;
int cps;
+ bool usecps;
char *prod_string;
char *manuf_string;
char *serial_string;
@@ -177,6 +178,8 @@ struct cg_usb_device {
uint32_t bufsiz;
uint32_t bufamt;
uint16_t PrefPacketSize;
+ struct timeval last_write_tv;
+ size_t last_write_siz;
};
#define USB_NOSTAT 0
@@ -207,6 +210,11 @@ struct cg_usb_info {
uint64_t clear_err_count;
uint64_t retry_err_count;
uint64_t clear_fail_count;
+
+ uint64_t read_delay_count;
+ double total_read_delay;
+ uint64_t write_delay_count;
+ double total_write_delay;
};
enum usb_cmds {
@@ -298,6 +306,12 @@ void usb_buffer_enable(struct cgpu_info *cgpu);
void usb_buffer_disable(struct cgpu_info *cgpu);
void usb_buffer_clear(struct cgpu_info *cgpu);
uint32_t usb_buffer_size(struct cgpu_info *cgpu);
+void usb_set_cps(struct cgpu_info *cgpu, int cps);
+void usb_enable_cps(struct cgpu_info *cgpu);
+void usb_disable_cps(struct cgpu_info *cgpu);
+int usb_interface(struct cgpu_info *cgpu);
+enum sub_ident usb_ident(struct cgpu_info *cgpu);
+void usb_set_pps(struct cgpu_info *cgpu, uint16_t PrefPacketSize);
void usb_set_dev_start(struct cgpu_info *cgpu);
void usb_cleanup();
void usb_initialise();