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 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
/*
* Copyright 2012-2013 Andrew Smith
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version. See COPYING for more details.
*/
#include "config.h"
#include <stdint.h>
#include <stdbool.h>
#include "logging.h"
#include "miner.h"
#include "usbutils.h"
#define NODEV(err) ((err) == LIBUSB_ERROR_NO_DEVICE || \
(err) == LIBUSB_ERROR_PIPE || \
(err) == LIBUSB_ERROR_OTHER)
#ifdef USE_ICARUS
#define DRV_ICARUS 1
#endif
#ifdef USE_BITFORCE
#define DRV_BITFORCE 2
#endif
#ifdef USE_MODMINER
#define DRV_MODMINER 3
#endif
#define DRV_LAST -1
#define USB_CONFIG 1
#define EPI(x) (LIBUSB_ENDPOINT_IN | (unsigned char)(x))
#define EPO(x) (LIBUSB_ENDPOINT_OUT | (unsigned char)(x))
#ifdef WIN32
#define BITFORCE_TIMEOUT_MS 500
#define MODMINER_TIMEOUT_MS 200
#else
#define BITFORCE_TIMEOUT_MS 200
#define MODMINER_TIMEOUT_MS 100
#endif
#ifdef USE_BITFORCE
// N.B. transfer size is 512 with USB2.0, but only 64 with USB1.1
static struct usb_endpoints bfl_eps[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
};
#endif
#ifdef USE_MODMINER
static struct usb_endpoints mmq_eps[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(3), 0 }
};
#endif
// TODO: Add support for (at least) Isochronous endpoints
static struct usb_find_devices find_dev[] = {
/*
#ifdef USE_ICARUS
{ DRV_ICARUS, "ICA", 0x067b, 0x0230, true, EPI(3), EPO(2), 1 },
{ DRV_ICARUS, "LOT", 0x0403, 0x6001, false, EPI(0), EPO(0), 1 },
{ DRV_ICARUS, "CM1", 0x067b, 0x0230, false, EPI(0), EPO(0), 1 },
#endif
*/
#ifdef USE_BITFORCE
{
.drv = DRV_BITFORCE,
.name = "BFL",
.idVendor = 0x0403,
.idProduct = 0x6014,
.config = 1,
.interface = 0,
.timeout = BITFORCE_TIMEOUT_MS,
.epcount = ARRAY_SIZE(bfl_eps),
.eps = bfl_eps },
#endif
#ifdef USE_MODMINER
{
.drv = DRV_MODMINER,
.name = "MMQ",
.idVendor = 0x1fc9,
.idProduct = 0x0003,
.config = 1,
.interface = 1,
.timeout = MODMINER_TIMEOUT_MS,
.epcount = ARRAY_SIZE(mmq_eps),
.eps = mmq_eps },
#endif
{ DRV_LAST, NULL, 0, 0, 0, 0, 0, 0, NULL }
};
#ifdef USE_BITFORCE
extern struct device_drv bitforce_drv;
#endif
#ifdef USE_ICARUS
extern struct device_drv icarus_drv;
#endif
#ifdef USE_MODMINER
extern struct device_drv modminer_drv;
#endif
#define STRBUFLEN 256
static const char *BLANK = "";
static bool stats_initialised = false;
struct cg_usb_stats_item {
uint64_t count;
double total_delay;
double min_delay;
double max_delay;
struct timeval first;
struct timeval last;
};
#define CMD_CMD 0
#define CMD_TIMEOUT 1
#define CMD_ERROR 2
struct cg_usb_stats_details {
int seq;
struct cg_usb_stats_item item[CMD_ERROR+1];
};
struct cg_usb_stats {
char *name;
int device_id;
struct cg_usb_stats_details *details;
};
#define SEQ0 0
#define SEQ1 1
static struct cg_usb_stats *usb_stats = NULL;
static int next_stat = 0;
static const char **usb_commands;
static const char *C_REJECTED_S = "RejectedNoDevice";
static const char *C_PING_S = "Ping";
static const char *C_CLEAR_S = "Clear";
static const char *C_REQUESTVERSION_S = "RequestVersion";
static const char *C_GETVERSION_S = "GetVersion";
static const char *C_REQUESTFPGACOUNT_S = "RequestFPGACount";
static const char *C_GETFPGACOUNT_S = "GetFPGACount";
static const char *C_STARTPROGRAM_S = "StartProgram";
static const char *C_STARTPROGRAMSTATUS_S = "StartProgramStatus";
static const char *C_PROGRAM_S = "Program";
static const char *C_PROGRAMSTATUS_S = "ProgramStatus";
static const char *C_PROGRAMSTATUS2_S = "ProgramStatus2";
static const char *C_FINALPROGRAMSTATUS_S = "FinalProgramStatus";
static const char *C_SETCLOCK_S = "SetClock";
static const char *C_REPLYSETCLOCK_S = "ReplySetClock";
static const char *C_REQUESTUSERCODE_S = "RequestUserCode";
static const char *C_GETUSERCODE_S = "GetUserCode";
static const char *C_REQUESTTEMPERATURE_S = "RequestTemperature";
static const char *C_GETTEMPERATURE_S = "GetTemperature";
static const char *C_SENDWORK_S = "SendWork";
static const char *C_SENDWORKSTATUS_S = "SendWorkStatus";
static const char *C_REQUESTWORKSTATUS_S = "RequestWorkStatus";
static const char *C_GETWORKSTATUS_S = "GetWorkStatus";
static const char *C_REQUESTIDENTIFY_S = "RequestIdentify";
static const char *C_GETIDENTIFY_S = "GetIdentify";
static const char *C_REQUESTFLASH_S = "RequestFlash";
static const char *C_REQUESTSENDWORK_S = "RequestSendWork";
static const char *C_REQUESTSENDWORKSTATUS_S = "RequestSendWorkStatus";
static const char *C_RESET_S = "Reset";
static const char *C_SETBAUD_S = "SetBaud";
static const char *C_SETDATA_S = "SetDataCtrl";
static const char *C_SETFLOW_S = "SetFlowCtrl";
static const char *C_SETMODEM_S = "SetModemCtrl";
static const char *C_PURGERX_S = "PurgeRx";
static const char *C_PURGETX_S = "PurgeTx";
#ifdef EOL
#undef EOL
#endif
#define EOL "\n"
static const char *DESDEV = "Device";
static const char *DESCON = "Config";
static const char *DESSTR = "String";
static const char *DESINT = "Interface";
static const char *DESEP = "Endpoint";
static const char *DESHID = "HID";
static const char *DESRPT = "Report";
static const char *DESPHY = "Physical";
static const char *DESHUB = "Hub";
static const char *EPIN = "In: ";
static const char *EPOUT = "Out: ";
static const char *EPX = "?: ";
static const char *CONTROL = "Control";
static const char *ISOCHRONOUS_X = "Isochronous+?";
static const char *ISOCHRONOUS_N_X = "Isochronous+None+?";
static const char *ISOCHRONOUS_N_D = "Isochronous+None+Data";
static const char *ISOCHRONOUS_N_F = "Isochronous+None+Feedback";
static const char *ISOCHRONOUS_N_I = "Isochronous+None+Implicit";
static const char *ISOCHRONOUS_A_X = "Isochronous+Async+?";
static const char *ISOCHRONOUS_A_D = "Isochronous+Async+Data";
static const char *ISOCHRONOUS_A_F = "Isochronous+Async+Feedback";
static const char *ISOCHRONOUS_A_I = "Isochronous+Async+Implicit";
static const char *ISOCHRONOUS_D_X = "Isochronous+Adaptive+?";
static const char *ISOCHRONOUS_D_D = "Isochronous+Adaptive+Data";
static const char *ISOCHRONOUS_D_F = "Isochronous+Adaptive+Feedback";
static const char *ISOCHRONOUS_D_I = "Isochronous+Adaptive+Implicit";
static const char *ISOCHRONOUS_S_X = "Isochronous+Sync+?";
static const char *ISOCHRONOUS_S_D = "Isochronous+Sync+Data";
static const char *ISOCHRONOUS_S_F = "Isochronous+Sync+Feedback";
static const char *ISOCHRONOUS_S_I = "Isochronous+Sync+Implicit";
static const char *BULK = "Bulk";
static const char *INTERRUPT = "Interrupt";
static const char *UNKNOWN = "Unknown";
static const char *destype(uint8_t bDescriptorType)
{
switch (bDescriptorType) {
case LIBUSB_DT_DEVICE:
return DESDEV;
case LIBUSB_DT_CONFIG:
return DESCON;
case LIBUSB_DT_STRING:
return DESSTR;
case LIBUSB_DT_INTERFACE:
return DESINT;
case LIBUSB_DT_ENDPOINT:
return DESEP;
case LIBUSB_DT_HID:
return DESHID;
case LIBUSB_DT_REPORT:
return DESRPT;
case LIBUSB_DT_PHYSICAL:
return DESPHY;
case LIBUSB_DT_HUB:
return DESHUB;
}
return UNKNOWN;
}
static const char *epdir(uint8_t bEndpointAddress)
{
switch (bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) {
case LIBUSB_ENDPOINT_IN:
return EPIN;
case LIBUSB_ENDPOINT_OUT:
return EPOUT;
}
return EPX;
}
static const char *epatt(uint8_t bmAttributes)
{
switch(bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) {
case LIBUSB_TRANSFER_TYPE_CONTROL:
return CONTROL;
case LIBUSB_TRANSFER_TYPE_BULK:
return BULK;
case LIBUSB_TRANSFER_TYPE_INTERRUPT:
return INTERRUPT;
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
switch(bmAttributes & LIBUSB_ISO_SYNC_TYPE_MASK) {
case LIBUSB_ISO_SYNC_TYPE_NONE:
switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
case LIBUSB_ISO_USAGE_TYPE_DATA:
return ISOCHRONOUS_N_D;
case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
return ISOCHRONOUS_N_F;
case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
return ISOCHRONOUS_N_I;
}
return ISOCHRONOUS_N_X;
case LIBUSB_ISO_SYNC_TYPE_ASYNC:
switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
case LIBUSB_ISO_USAGE_TYPE_DATA:
return ISOCHRONOUS_A_D;
case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
return ISOCHRONOUS_A_F;
case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
return ISOCHRONOUS_A_I;
}
return ISOCHRONOUS_A_X;
case LIBUSB_ISO_SYNC_TYPE_ADAPTIVE:
switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
case LIBUSB_ISO_USAGE_TYPE_DATA:
return ISOCHRONOUS_D_D;
case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
return ISOCHRONOUS_D_F;
case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
return ISOCHRONOUS_D_I;
}
return ISOCHRONOUS_D_X;
case LIBUSB_ISO_SYNC_TYPE_SYNC:
switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
case LIBUSB_ISO_USAGE_TYPE_DATA:
return ISOCHRONOUS_S_D;
case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
return ISOCHRONOUS_S_F;
case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
return ISOCHRONOUS_S_I;
}
return ISOCHRONOUS_S_X;
}
return ISOCHRONOUS_X;
}
return UNKNOWN;
}
static void append(char **buf, char *append, size_t *off, size_t *len)
{
int new = strlen(append);
if ((new + *off) >= *len)
{
*len *= 2;
*buf = realloc(*buf, *len);
}
strcpy(*buf + *off, append);
*off += new;
}
static bool setgetdes(ssize_t count, libusb_device *dev, struct libusb_device_handle *handle, struct libusb_config_descriptor **config, int cd, char **buf, size_t *off, size_t *len)
{
char tmp[512];
int err;
err = libusb_set_configuration(handle, cd);
if (err) {
sprintf(tmp, EOL " ** dev %d: Failed to set config descriptor to %d, err %d",
(int)count, cd, err);
append(buf, tmp, off, len);
return false;
}
err = libusb_get_active_config_descriptor(dev, config);
if (err) {
sprintf(tmp, EOL " ** dev %d: Failed to get active config descriptor set to %d, err %d",
(int)count, cd, err);
append(buf, tmp, off, len);
return false;
}
sprintf(tmp, EOL " ** dev %d: Set & Got active config descriptor to %d, err %d",
(int)count, cd, err);
append(buf, tmp, off, len);
return true;
}
static void usb_full(ssize_t count, libusb_device *dev, char **buf, size_t *off, size_t *len)
{
struct libusb_device_descriptor desc;
struct libusb_device_handle *handle;
struct libusb_config_descriptor *config;
const struct libusb_interface_descriptor *idesc;
const struct libusb_endpoint_descriptor *epdesc;
unsigned char man[STRBUFLEN+1];
unsigned char prod[STRBUFLEN+1];
unsigned char ser[STRBUFLEN+1];
char tmp[512];
int err, i, j, k;
err = libusb_get_device_descriptor(dev, &desc);
if (err) {
sprintf(tmp, EOL ".USB dev %d: Failed to get descriptor, err %d",
(int)count, err);
append(buf, tmp, off, len);
return;
}
sprintf(tmp, EOL ".USB dev %d: Device Descriptor:" EOL "\tLength: %d" EOL
"\tDescriptor Type: %s" EOL "\tUSB: %04x" EOL "\tDeviceClass: %d" EOL
"\tDeviceSubClass: %d" EOL "\tDeviceProtocol: %d" EOL "\tMaxPacketSize0: %d" EOL
"\tidVendor: %04x" EOL "\tidProduct: %04x" EOL "\tDeviceRelease: %x" EOL
"\tNumConfigurations: %d",
(int)count, (int)(desc.bLength), destype(desc.bDescriptorType),
desc.bcdUSB, (int)(desc.bDeviceClass), (int)(desc.bDeviceSubClass),
(int)(desc.bDeviceProtocol), (int)(desc.bMaxPacketSize0),
desc.idVendor, desc.idProduct, desc.bcdDevice,
(int)(desc.bNumConfigurations));
append(buf, tmp, off, len);
err = libusb_open(dev, &handle);
if (err) {
sprintf(tmp, EOL " ** dev %d: Failed to open, err %d", (int)count, err);
append(buf, tmp, off, len);
return;
}
if (libusb_kernel_driver_active(handle, 0) == 1) {
sprintf(tmp, EOL " * dev %d: kernel attached", (int)count);
append(buf, tmp, off, len);
}
err = libusb_get_active_config_descriptor(dev, &config);
if (err) {
if (!setgetdes(count, dev, handle, &config, 1, buf, off, len)
&& !setgetdes(count, dev, handle, &config, 0, buf, off, len)) {
libusb_close(handle);
sprintf(tmp, EOL " ** dev %d: Failed to set config descriptor to %d or %d",
(int)count, 1, 0);
append(buf, tmp, off, len);
return;
}
}
sprintf(tmp, EOL " dev %d: Active Config:" EOL "\tDescriptorType: %s" EOL
"\tNumInterfaces: %d" EOL "\tConfigurationValue: %d" EOL
"\tAttributes: %d" EOL "\tMaxPower: %d",
(int)count, destype(config->bDescriptorType),
(int)(config->bNumInterfaces), (int)(config->iConfiguration),
(int)(config->bmAttributes), (int)(config->MaxPower));
append(buf, tmp, off, len);
for (i = 0; i < (int)(config->bNumInterfaces); i++) {
for (j = 0; j < config->interface[i].num_altsetting; j++) {
idesc = &(config->interface[i].altsetting[j]);
sprintf(tmp, EOL " _dev %d: Interface Descriptor %d:" EOL
"\tDescriptorType: %s" EOL "\tInterfaceNumber: %d" EOL
"\tNumEndpoints: %d" EOL "\tInterfaceClass: %d" EOL
"\tInterfaceSubClass: %d" EOL "\tInterfaceProtocol: %d",
(int)count, j, destype(idesc->bDescriptorType),
(int)(idesc->bInterfaceNumber),
(int)(idesc->bNumEndpoints),
(int)(idesc->bInterfaceClass),
(int)(idesc->bInterfaceSubClass),
(int)(idesc->bInterfaceProtocol));
append(buf, tmp, off, len);
for (k = 0; k < (int)(idesc->bNumEndpoints); k++) {
epdesc = &(idesc->endpoint[k]);
sprintf(tmp, EOL " __dev %d: Interface %d Endpoint %d:" EOL
"\tDescriptorType: %s" EOL
"\tEndpointAddress: %s0x%x" EOL
"\tAttributes: %s" EOL "\tMaxPacketSize: %d" EOL
"\tInterval: %d" EOL "\tRefresh: %d",
(int)count, (int)(idesc->bInterfaceNumber), k,
destype(epdesc->bDescriptorType),
epdir(epdesc->bEndpointAddress),
(int)(epdesc->bEndpointAddress),
epatt(epdesc->bmAttributes),
epdesc->wMaxPacketSize,
(int)(epdesc->bInterval),
(int)(epdesc->bRefresh));
append(buf, tmp, off, len);
}
}
}
libusb_free_config_descriptor(config);
config = NULL;
err = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, man, STRBUFLEN);
if (err < 0)
sprintf((char *)man, "** err(%d)", err);
err = libusb_get_string_descriptor_ascii(handle, desc.iProduct, prod, STRBUFLEN);
if (err < 0)
sprintf((char *)prod, "** err(%d)", err);
err = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, ser, STRBUFLEN);
if (err < 0)
sprintf((char *)ser, "** err(%d)", err);
sprintf(tmp, EOL " dev %d: More Info:" EOL "\tManufacturer: '%s'" EOL
"\tProduct: '%s'" EOL "\tSerial '%s'",
(int)count, man, prod, ser);
append(buf, tmp, off, len);
libusb_close(handle);
}
// Function to dump all USB devices
static void usb_all()
{
libusb_device **list;
ssize_t count, i;
char *buf;
size_t len, off;
count = libusb_get_device_list(NULL, &list);
if (count < 0) {
applog(LOG_ERR, "USB all: failed, err %d", (int)count);
return;
}
if (count == 0)
applog(LOG_WARNING, "USB all: found no devices");
else
{
len = 10000;
buf = malloc(len+1);
sprintf(buf, "USB all: found %d devices", (int)count);
off = strlen(buf);
for (i = 0; i < count; i++)
usb_full(i, list[i], &buf, &off, &len);
applog(LOG_WARNING, "%s", buf);
free(buf);
}
libusb_free_device_list(list, 1);
}
static void cgusb_check_init()
{
mutex_lock(&cgusb_lock);
if (stats_initialised == false) {
// N.B. environment LIBUSB_DEBUG also sets libusb_set_debug()
if (opt_usbdump >= 0) {
libusb_set_debug(NULL, opt_usbdump);
usb_all();
}
usb_commands = malloc(sizeof(*usb_commands) * C_MAX);
// use constants so the stat generation is very quick
// and the association between number and name can't
// be missalined easily
usb_commands[C_REJECTED] = C_REJECTED_S;
usb_commands[C_PING] = C_PING_S;
usb_commands[C_CLEAR] = C_CLEAR_S;
usb_commands[C_REQUESTVERSION] = C_REQUESTVERSION_S;
usb_commands[C_GETVERSION] = C_GETVERSION_S;
usb_commands[C_REQUESTFPGACOUNT] = C_REQUESTFPGACOUNT_S;
usb_commands[C_GETFPGACOUNT] = C_GETFPGACOUNT_S;
usb_commands[C_STARTPROGRAM] = C_STARTPROGRAM_S;
usb_commands[C_STARTPROGRAMSTATUS] = C_STARTPROGRAMSTATUS_S;
usb_commands[C_PROGRAM] = C_PROGRAM_S;
usb_commands[C_PROGRAMSTATUS] = C_PROGRAMSTATUS_S;
usb_commands[C_PROGRAMSTATUS2] = C_PROGRAMSTATUS2_S;
usb_commands[C_FINALPROGRAMSTATUS] = C_FINALPROGRAMSTATUS_S;
usb_commands[C_SETCLOCK] = C_SETCLOCK_S;
usb_commands[C_REPLYSETCLOCK] = C_REPLYSETCLOCK_S;
usb_commands[C_REQUESTUSERCODE] = C_REQUESTUSERCODE_S;
usb_commands[C_GETUSERCODE] = C_GETUSERCODE_S;
usb_commands[C_REQUESTTEMPERATURE] = C_REQUESTTEMPERATURE_S;
usb_commands[C_GETTEMPERATURE] = C_GETTEMPERATURE_S;
usb_commands[C_SENDWORK] = C_SENDWORK_S;
usb_commands[C_SENDWORKSTATUS] = C_SENDWORKSTATUS_S;
usb_commands[C_REQUESTWORKSTATUS] = C_REQUESTWORKSTATUS_S;
usb_commands[C_GETWORKSTATUS] = C_GETWORKSTATUS_S;
usb_commands[C_REQUESTIDENTIFY] = C_REQUESTIDENTIFY_S;
usb_commands[C_GETIDENTIFY] = C_GETIDENTIFY_S;
usb_commands[C_REQUESTFLASH] = C_REQUESTFLASH_S;
usb_commands[C_REQUESTSENDWORK] = C_REQUESTSENDWORK_S;
usb_commands[C_REQUESTSENDWORKSTATUS] = C_REQUESTSENDWORKSTATUS_S;
usb_commands[C_RESET] = C_RESET_S;
usb_commands[C_SETBAUD] = C_SETBAUD_S;
usb_commands[C_SETDATA] = C_SETDATA_S;
usb_commands[C_SETFLOW] = C_SETFLOW_S;
usb_commands[C_SETMODEM] = C_SETMODEM_S;
usb_commands[C_PURGERX] = C_PURGERX_S;
usb_commands[C_PURGETX] = C_PURGETX_S;
}
mutex_unlock(&cgusb_lock);
}
#ifdef WIN32
#else
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
union semun {
int sem;
struct semid_ds *seminfo;
ushort *all;
};
#endif
// Any errors should always be printed since they will rarely if ever occur
// and thus it is best to always display them
static bool cgminer_usb_lock_bd(struct device_drv *drv, uint8_t bus_number, uint8_t device_address)
{
#ifdef WIN32
// NOOP for now
drv = 0;
bus_number = device_address = 0;
return true;
#else
struct semid_ds seminfo;
union semun opt;
char name[64];
key_t key;
int fd, sem, count;
sprintf(name, "/tmp/cgminer-usb-%d-%d", (int)bus_number, (int)device_address);
fd = open(name, O_CREAT|O_RDONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if (fd == -1) {
applog(LOG_ERR,
"SEM: %s USB open failed '%s' err (%d) %s",
drv->dname, name, errno, strerror(errno));
return false;
}
close(fd);
key = ftok(name, 'K');
sem = semget(key, 1, IPC_CREAT | IPC_EXCL | 438);
if (sem < 0) {
if (errno != EEXIST) {
applog(LOG_ERR,
"SEM: %s USB failed to get '%s' err (%d) %s",
drv->dname, name, errno, strerror(errno));
return false;
}
sem = semget(key, 1, 0);
if (sem < 0) {
applog(LOG_ERR,
"SEM: %s USB failed to access '%s' err (%d) %s",
drv->dname, name, errno, strerror(errno));
return false;
}
opt.seminfo = &seminfo;
count = 0;
while (++count) {
// Should NEVER take 100ms
if (count > 99) {
applog(LOG_ERR,
"SEM: %s USB timeout waiting for (%d) '%s'",
drv->dname, sem, name);
return false;
}
if (semctl(sem, 0, IPC_STAT, opt) == -1) {
applog(LOG_ERR,
"SEM: %s USB failed to wait for (%d) '%s' count %d err (%d) %s",
drv->dname, sem, name, count, errno, strerror(errno));
return false;
}
if (opt.seminfo->sem_otime != 0)
break;
nmsleep(1);
}
}
struct sembuf sops[] = {
{ 0, 0, IPC_NOWAIT | SEM_UNDO },
{ 0, 1, IPC_NOWAIT | SEM_UNDO }
};
if (semop(sem, sops, 2)) {
if (errno == EAGAIN) {
applog(LOG_WARNING,
"SEM: %s USB failed to get (%d) '%s' - device in use",
drv->dname, sem, name);
} else {
applog(LOG_DEBUG,
"SEM: %s USB failed to get (%d) '%s' err(%d) %s",
drv->dname, sem, name, errno, strerror(errno));
}
return false;
}
return true;
#endif
}
static bool cgminer_usb_lock(struct device_drv *drv, libusb_device *dev)
{
return cgminer_usb_lock_bd(drv, libusb_get_bus_number(dev), libusb_get_device_address(dev));
}
// Any errors should always be printed since they will rarely if ever occur
// and thus it is best to always display them
static void cgminer_usb_unlock_bd(struct device_drv *drv, uint8_t bus_number, uint8_t device_address)
{
#ifdef WIN32
// NOOP for now
drv = 0;
bus_number = device_address = 0;
#else
char name[64];
key_t key;
int fd, sem;
sprintf(name, "/tmp/cgminer-usb-%d-%d", (int)bus_number, (int)device_address);
fd = open(name, O_CREAT|O_RDONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if (fd == -1) {
applog(LOG_ERR,
"SEM: %s USB open failed '%s' for release err (%d) %s",
drv->dname, name, errno, strerror(errno));
return;
}
close(fd);
key = ftok(name, 'K');
sem = semget(key, 1, 0);
if (sem < 0) {
applog(LOG_ERR,
"SEM: %s USB failed to get '%s' for release err (%d) %s",
drv->dname, name, errno, strerror(errno));
return;
}
struct sembuf sops[] = {
{ 0, -1, SEM_UNDO }
};
// Allow a 10ms timeout
// exceeding this timeout means it would probably never succeed anyway
struct timespec timeout = { 0, 10000000 };
// Wait forever since we shoud be the one who has it
if (semtimedop(sem, sops, 1, &timeout)) {
applog(LOG_ERR,
"SEM: %d USB failed to release '%s' err (%d) %s",
drv->dname, name, errno, strerror(errno));
}
return;
#endif
}
static void cgminer_usb_unlock(struct device_drv *drv, libusb_device *dev)
{
cgminer_usb_unlock_bd(drv, libusb_get_bus_number(dev), libusb_get_device_address(dev));
}
static struct cg_usb_device *free_cgusb(struct cg_usb_device *cgusb)
{
if (cgusb->serial_string && cgusb->serial_string != BLANK)
free(cgusb->serial_string);
if (cgusb->manuf_string && cgusb->manuf_string != BLANK)
free(cgusb->manuf_string);
if (cgusb->prod_string && cgusb->prod_string != BLANK)
free(cgusb->prod_string);
free(cgusb->descriptor);
free(cgusb->found);
free(cgusb);
return NULL;
}
void usb_uninit(struct cgpu_info *cgpu)
{
libusb_release_interface(cgpu->usbdev->handle, cgpu->usbdev->found->interface);
libusb_close(cgpu->usbdev->handle);
cgpu->usbdev = free_cgusb(cgpu->usbdev);
}
void release_cgpu(struct cgpu_info *cgpu)
{
struct cg_usb_device *cgusb = cgpu->usbdev;
int i;
cgpu->usbinfo.nodev = true;
cgpu->usbinfo.nodev_count++;
gettimeofday(&(cgpu->usbinfo.last_nodev), NULL);
// Any devices sharing the same USB device should be marked also
// Currently only MMQ shares a USB device
for (i = 0; i < total_devices; i++)
if (devices[i] != cgpu && devices[i]->usbdev == cgusb) {
devices[i]->usbinfo.nodev = true;
devices[i]->usbinfo.nodev_count++;
memcpy(&(devices[i]->usbinfo.last_nodev),
&(cgpu->usbinfo.last_nodev), sizeof(struct timeval));
devices[i]->usbdev = NULL;
}
usb_uninit(cgpu);
cgminer_usb_unlock_bd(cgpu->drv, cgpu->usbinfo.bus_number, cgpu->usbinfo.device_address);
}
bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found)
{
struct cg_usb_device *cgusb = NULL;
struct libusb_config_descriptor *config = NULL;
const struct libusb_interface_descriptor *idesc;
const struct libusb_endpoint_descriptor *epdesc;
unsigned char strbuf[STRBUFLEN+1];
char devstr[STRBUFLEN+1];
int err, i, j, k;
cgpu->usbinfo.bus_number = libusb_get_bus_number(dev);
cgpu->usbinfo.device_address = libusb_get_device_address(dev);
sprintf(devstr, "- %s device %d:%d", found->name,
cgpu->usbinfo.bus_number, cgpu->usbinfo.device_address);
cgusb = calloc(1, sizeof(*cgusb));
cgusb->found = found;
cgusb->descriptor = calloc(1, sizeof(*(cgusb->descriptor)));
err = libusb_get_device_descriptor(dev, cgusb->descriptor);
if (err) {
applog(LOG_DEBUG,
"USB init failed to get descriptor, err %d %s",
err, devstr);
goto dame;
}
err = libusb_open(dev, &(cgusb->handle));
if (err) {
switch (err) {
case LIBUSB_ERROR_ACCESS:
applog(LOG_ERR,
"USB init open device failed, err %d, "
"you dont have priviledge to access %s",
err, devstr);
break;
#ifdef WIN32
// Windows specific message
case LIBUSB_ERROR_NOT_SUPPORTED:
applog(LOG_ERR,
"USB init, open device failed, err %d, "
"you need to install a Windows USB driver for %s",
err, devstr);
break;
#endif
default:
applog(LOG_DEBUG,
"USB init, open failed, err %d %s",
err, devstr);
}
goto dame;
}
if (libusb_kernel_driver_active(cgusb->handle, found->config) == 1) {
applog(LOG_DEBUG, "USB init, kernel attached ... %s", devstr);
err = libusb_detach_kernel_driver(cgusb->handle, found->config);
if (err == 0) {
applog(LOG_DEBUG,
"USB init, kernel detached successfully %s",
devstr);
} else {
applog(LOG_WARNING,
"USB init, kernel detach failed, err %d in use? %s",
err, devstr);
goto cldame;
}
}
err = libusb_set_configuration(cgusb->handle, found->config);
if (err) {
switch(err) {
case LIBUSB_ERROR_BUSY:
applog(LOG_WARNING,
"USB init, set config %d in use %s",
found->config, devstr);
break;
default:
applog(LOG_DEBUG,
"USB init, failed to set config to %d, err %d %s",
found->config, err, devstr);
}
goto cldame;
}
err = libusb_get_active_config_descriptor(dev, &config);
if (err) {
applog(LOG_DEBUG,
"USB init, failed to get config descriptor %d, err %d %s",
found->config, err, devstr);
goto cldame;
}
if ((int)(config->bNumInterfaces) <= found->interface)
goto cldame;
for (i = 0; i < found->epcount; i++)
found->eps[i].found = false;
for (i = 0; i < config->interface[found->interface].num_altsetting; i++) {
idesc = &(config->interface[found->interface].altsetting[i]);
for (j = 0; j < (int)(idesc->bNumEndpoints); j++) {
epdesc = &(idesc->endpoint[j]);
for (k = 0; k < found->epcount; k++) {
if (!found->eps[k].found) {
if (epdesc->bmAttributes == found->eps[k].att
&& epdesc->wMaxPacketSize >= found->eps[k].size
&& epdesc->bEndpointAddress == found->eps[k].ep) {
found->eps[k].found = true;
break;
}
}
}
}
}
for (i = 0; i < found->epcount; i++)
if (found->eps[i].found == false)
goto cldame;
err = libusb_claim_interface(cgusb->handle, found->interface);
if (err) {
switch(err) {
case LIBUSB_ERROR_BUSY:
applog(LOG_WARNING,
"USB init, claim interface %d in use %s",
found->interface, devstr);
break;
default:
applog(LOG_DEBUG,
"USB init, claim interface %d failed, err %d %s",
found->interface, err, devstr);
}
goto cldame;
}
cgusb->usbver = cgusb->descriptor->bcdUSB;
// TODO: allow this with the right version of the libusb include and running library
// cgusb->speed = libusb_get_device_speed(dev);
err = libusb_get_string_descriptor_ascii(cgusb->handle,
cgusb->descriptor->iProduct, strbuf, STRBUFLEN);
if (err > 0)
cgusb->prod_string = strdup((char *)strbuf);
else
cgusb->prod_string = (char *)BLANK;
err = libusb_get_string_descriptor_ascii(cgusb->handle,
cgusb->descriptor->iManufacturer, strbuf, STRBUFLEN);
if (err > 0)
cgusb->manuf_string = strdup((char *)strbuf);
else
cgusb->manuf_string = (char *)BLANK;
err = libusb_get_string_descriptor_ascii(cgusb->handle,
cgusb->descriptor->iSerialNumber, strbuf, STRBUFLEN);
if (err > 0)
cgusb->serial_string = strdup((char *)strbuf);
else
cgusb->serial_string = (char *)BLANK;
// TODO: ?
// cgusb->fwVersion <- for temp1/temp2 decision? or serial? (driver-modminer.c)
// cgusb->interfaceVersion
applog(LOG_DEBUG,
"USB init %s usbver=%04x prod='%s' manuf='%s' serial='%s'",
devstr, cgusb->usbver, cgusb->prod_string,
cgusb->manuf_string, cgusb->serial_string);
cgpu->usbdev = cgusb;
libusb_free_config_descriptor(config);
// Allow a name change based on the idVendor+idProduct
// N.B. must be done before calling add_cgpu()
if (strcmp(cgpu->drv->name, found->name)) {
if (!cgpu->drv->copy)
cgpu->drv = copy_drv(cgpu->drv);
cgpu->drv->name = (char *)(found->name);
}
return true;
cldame:
libusb_close(cgusb->handle);
dame:
if (config)
libusb_free_config_descriptor(config);
cgusb = free_cgusb(cgusb);
return false;
}
static bool usb_check_device(struct device_drv *drv, struct libusb_device *dev, struct usb_find_devices *look)
{
struct libusb_device_descriptor desc;
int err;
err = libusb_get_device_descriptor(dev, &desc);
if (err) {
applog(LOG_DEBUG, "USB check device: Failed to get descriptor, err %d", err);
return false;
}
if (desc.idVendor != look->idVendor || desc.idProduct != look->idProduct) {
applog(LOG_DEBUG, "%s looking for %s %04x:%04x but found %04x:%04x instead",
drv->name, look->name, look->idVendor, look->idProduct, desc.idVendor, desc.idProduct);
return false;
}
applog(LOG_DEBUG, "%s looking for and found %s %04x:%04x",
drv->name, look->name, look->idVendor, look->idProduct);
return true;
}
static struct usb_find_devices *usb_check_each(int drvnum, struct device_drv *drv, struct libusb_device *dev)
{
struct usb_find_devices *found;
int i;
for (i = 0; find_dev[i].drv != DRV_LAST; i++)
if (find_dev[i].drv == drvnum) {
if (usb_check_device(drv, dev, &(find_dev[i]))) {
found = malloc(sizeof(*found));
memcpy(found, &(find_dev[i]), sizeof(*found));
return found;
}
}
return NULL;
}
static struct usb_find_devices *usb_check(__maybe_unused struct device_drv *drv, __maybe_unused struct libusb_device *dev)
{
#ifdef USE_BITFORCE
if (drv->drv == DRIVER_BITFORCE)
return usb_check_each(DRV_BITFORCE, drv, dev);
#endif
#ifdef USE_ICARUS
if (drv->drv == DRIVER_ICARUS)
return usb_check_each(DRV_ICARUS, drv, dev);
#endif
#ifdef USE_MODMINER
if (drv->drv == DRIVER_MODMINER)
return usb_check_each(DRV_MODMINER, drv, dev);
#endif
return NULL;
}
void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *))
{
libusb_device **list;
ssize_t count, i;
struct usb_find_devices *found;
cgusb_check_init();
count = libusb_get_device_list(NULL, &list);
if (count < 0) {
applog(LOG_DEBUG, "USB scan devices: failed, err %d", count);
return;
}
if (count == 0)
applog(LOG_DEBUG, "USB scan devices: found no devices");
for (i = 0; i < count; i++) {
found = usb_check(drv, list[i]);
if (found != NULL) {
if (cgminer_usb_lock(drv, list[i]) == false)
free(found);
else {
if (!device_detect(list[i], found))
cgminer_usb_unlock(drv, list[i]);
}
}
}
libusb_free_device_list(list, 1);
}
// Set this to 0 to remove stats processing
#define DO_USB_STATS 1
#if DO_USB_STATS
#define USB_STATS(sgpu, sta, fin, err, cmd, seq) stats(cgpu, sta, fin, err, cmd, seq)
#define STATS_TIMEVAL(tv) gettimeofday(tv, NULL)
#else
#define USB_STATS(sgpu, sta, fin, err, cmd, seq)
#define STATS_TIMEVAL(tv)
#endif
// The stat data can be spurious due to not locking it before copying it -
// however that would require the stat() function to also lock and release
// a mutex every time a usb read or write is called which would slow
// things down more
struct api_data *api_usb_stats(__maybe_unused int *count)
{
#if DO_USB_STATS
struct cg_usb_stats_details *details;
struct cg_usb_stats *sta;
struct api_data *root = NULL;
int device;
int cmdseq;
cgusb_check_init();
if (next_stat == 0)
return NULL;
while (*count < next_stat * C_MAX * 2) {
device = *count / (C_MAX * 2);
cmdseq = *count % (C_MAX * 2);
(*count)++;
sta = &(usb_stats[device]);
details = &(sta->details[cmdseq]);
// Only show stats that have results
if (details->item[CMD_CMD].count == 0 &&
details->item[CMD_TIMEOUT].count == 0 &&
details->item[CMD_ERROR].count == 0)
continue;
root = api_add_string(root, "Name", sta->name, false);
root = api_add_int(root, "ID", &(sta->device_id), false);
root = api_add_const(root, "Stat", usb_commands[cmdseq/2], false);
root = api_add_int(root, "Seq", &(details->seq), true);
root = api_add_uint64(root, "Count",
&(details->item[CMD_CMD].count), true);
root = api_add_double(root, "Total Delay",
&(details->item[CMD_CMD].total_delay), true);
root = api_add_double(root, "Min Delay",
&(details->item[CMD_CMD].min_delay), true);
root = api_add_double(root, "Max Delay",
&(details->item[CMD_CMD].max_delay), true);
root = api_add_uint64(root, "Timeout Count",
&(details->item[CMD_TIMEOUT].count), true);
root = api_add_double(root, "Timeout Total Delay",
&(details->item[CMD_TIMEOUT].total_delay), true);
root = api_add_double(root, "Timeout Min Delay",
&(details->item[CMD_TIMEOUT].min_delay), true);
root = api_add_double(root, "Timeout Max Delay",
&(details->item[CMD_TIMEOUT].max_delay), true);
root = api_add_uint64(root, "Error Count",
&(details->item[CMD_ERROR].count), true);
root = api_add_double(root, "Error Total Delay",
&(details->item[CMD_ERROR].total_delay), true);
root = api_add_double(root, "Error Min Delay",
&(details->item[CMD_ERROR].min_delay), true);
root = api_add_double(root, "Error Max Delay",
&(details->item[CMD_ERROR].max_delay), true);
root = api_add_timeval(root, "First Command",
&(details->item[CMD_CMD].first), true);
root = api_add_timeval(root, "Last Command",
&(details->item[CMD_CMD].last), true);
root = api_add_timeval(root, "First Timeout",
&(details->item[CMD_TIMEOUT].first), true);
root = api_add_timeval(root, "Last Timeout",
&(details->item[CMD_TIMEOUT].last), true);
root = api_add_timeval(root, "First Error",
&(details->item[CMD_ERROR].first), true);
root = api_add_timeval(root, "Last Error",
&(details->item[CMD_ERROR].last), true);
return root;
}
#endif
return NULL;
}
#if DO_USB_STATS
static void newstats(struct cgpu_info *cgpu)
{
int i;
cgpu->usbinfo.usbstat = ++next_stat;
usb_stats = realloc(usb_stats, sizeof(*usb_stats) * next_stat);
usb_stats[next_stat-1].name = cgpu->drv->name;
usb_stats[next_stat-1].device_id = -1;
usb_stats[next_stat-1].details = calloc(1, sizeof(struct cg_usb_stats_details) * C_MAX * 2);
for (i = 1; i < C_MAX * 2; i += 2)
usb_stats[next_stat-1].details[i].seq = 1;
}
#endif
void update_usb_stats(__maybe_unused struct cgpu_info *cgpu)
{
#if DO_USB_STATS
if (cgpu->usbinfo.usbstat < 1)
newstats(cgpu);
// we don't know the device_id until after add_cgpu()
usb_stats[cgpu->usbinfo.usbstat - 1].device_id = cgpu->device_id;
#endif
}
#if DO_USB_STATS
static void stats(struct cgpu_info *cgpu, struct timeval *tv_start, struct timeval *tv_finish, int err, enum usb_cmds cmd, int seq)
{
struct cg_usb_stats_details *details;
double diff;
int item;
if (cgpu->usbinfo.usbstat < 1)
newstats(cgpu);
details = &(usb_stats[cgpu->usbinfo.usbstat - 1].details[cmd * 2 + seq]);
diff = tdiff(tv_finish, tv_start);
switch (err) {
case LIBUSB_SUCCESS:
item = CMD_CMD;
break;
case LIBUSB_ERROR_TIMEOUT:
item = CMD_TIMEOUT;
break;
default:
item = CMD_ERROR;
break;
}
if (details->item[item].count == 0) {
details->item[item].min_delay = diff;
memcpy(&(details->item[item].first), tv_start, sizeof(*tv_start));
} else if (diff < details->item[item].min_delay)
details->item[item].min_delay = diff;
if (diff > details->item[item].max_delay)
details->item[item].max_delay = diff;
details->item[item].total_delay += diff;
memcpy(&(details->item[item].last), tv_start, sizeof(tv_start));
details->item[item].count++;
}
static void rejected_inc(struct cgpu_info *cgpu)
{
struct cg_usb_stats_details *details;
int item = CMD_ERROR;
if (cgpu->usbinfo.usbstat < 1)
newstats(cgpu);
details = &(usb_stats[cgpu->usbinfo.usbstat - 1].details[C_REJECTED * 2 + 0]);
details->item[item].count++;
}
#endif
int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, int eol, enum usb_cmds cmd, bool ftdi)
{
struct cg_usb_device *usbdev = cgpu->usbdev;
#if DO_USB_STATS
struct timeval tv_start;
#endif
struct timeval read_start, tv_finish;
unsigned int initial_timeout;
double max, done;
int err, got, tot, i;
bool first = true;
if (cgpu->usbinfo.nodev) {
*buf = '\0';
*processed = 0;
#if DO_USB_STATS
rejected_inc(cgpu);
#endif
return LIBUSB_ERROR_NO_DEVICE;
}
if (timeout == DEVTIMEOUT)
timeout = usbdev->found->timeout;
if (eol == -1) {
got = 0;
STATS_TIMEVAL(&tv_start);
err = libusb_bulk_transfer(usbdev->handle,
usbdev->found->eps[ep].ep,
(unsigned char *)buf,
bufsiz, &got, timeout);
STATS_TIMEVAL(&tv_finish);
USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, SEQ0);
if (ftdi) {
// first 2 bytes returned are an FTDI status
if (got > 2) {
got -= 2;
memmove(buf, buf+2, got+1);
} else {
got = 0;
*buf = '\0';
}
}
*processed = got;
if (NODEV(err))
release_cgpu(cgpu);
return err;
}
tot = 0;
err = LIBUSB_SUCCESS;
initial_timeout = timeout;
max = ((double)timeout) / 1000.0;
gettimeofday(&read_start, NULL);
while (bufsiz) {
got = 0;
STATS_TIMEVAL(&tv_start);
err = libusb_bulk_transfer(usbdev->handle,
usbdev->found->eps[ep].ep,
(unsigned char *)buf,
bufsiz, &got, timeout);
gettimeofday(&tv_finish, NULL);
USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, first ? SEQ0 : SEQ1);
if (ftdi) {
// first 2 bytes returned are an FTDI status
if (got > 2) {
got -= 2;
memmove(buf, buf+2, got+1);
} else {
got = 0;
*buf = '\0';
}
}
tot += got;
if (err)
break;
// WARNING - this will return data past EOL ('if' there is extra data)
for (i = 0; i < got; i++)
if (buf[i] == eol)
goto goteol;
buf += got;
bufsiz -= got;
first = false;
done = tdiff(&tv_finish, &read_start);
// N.B. this is return LIBUSB_SUCCESS with whatever size has already been read
if (unlikely(done >= max))
break;
timeout = initial_timeout - (done * 1000);
}
goteol:
*processed = tot;
if (NODEV(err))
release_cgpu(cgpu);
return err;
}
int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds cmd)
{
struct cg_usb_device *usbdev = cgpu->usbdev;
#if DO_USB_STATS
struct timeval tv_start, tv_finish;
#endif
int err, sent;
if (cgpu->usbinfo.nodev) {
*processed = 0;
#if DO_USB_STATS
rejected_inc(cgpu);
#endif
return LIBUSB_ERROR_NO_DEVICE;
}
sent = 0;
STATS_TIMEVAL(&tv_start);
err = libusb_bulk_transfer(usbdev->handle,
usbdev->found->eps[ep].ep,
(unsigned char *)buf,
bufsiz, &sent,
timeout == DEVTIMEOUT ? usbdev->found->timeout : timeout);
STATS_TIMEVAL(&tv_finish);
USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, SEQ0);
*processed = sent;
if (NODEV(err))
release_cgpu(cgpu);
return err;
}
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)
{
struct cg_usb_device *usbdev = cgpu->usbdev;
#if DO_USB_STATS
struct timeval tv_start, tv_finish;
#endif
int err;
if (cgpu->usbinfo.nodev) {
#if DO_USB_STATS
rejected_inc(cgpu);
#endif
return LIBUSB_ERROR_NO_DEVICE;
}
STATS_TIMEVAL(&tv_start);
err = libusb_control_transfer(usbdev->handle, request_type,
bRequest, wValue, wIndex, NULL, 0,
timeout == DEVTIMEOUT ? usbdev->found->timeout : timeout);
STATS_TIMEVAL(&tv_finish);
USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, SEQ0);
if (NODEV(err))
release_cgpu(cgpu);
return err;
}
void usb_cleanup()
{
// TODO:
}