Hash :
9ecef180
Author :
Date :
2013-06-26T12:53:05
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 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609
/*
* Copyright 2012-2013 Andrew Smith
* Copyright 2013 Con Kolivas <kernel@kolivas.org>
*
* 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 <ctype.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)
#define NOCONTROLDEV(err) ((err) == LIBUSB_ERROR_NO_DEVICE || \
(err) == LIBUSB_ERROR_OTHER)
#ifdef USE_BFLSC
#define DRV_BFLSC 1
#endif
#ifdef USE_BITFORCE
#define DRV_BITFORCE 2
#endif
#ifdef USE_MODMINER
#define DRV_MODMINER 3
#endif
#ifdef USE_ZTEX
#define DRV_ZTEX 4
#endif
#ifdef USE_ICARUS
#define DRV_ICARUS 5
#endif
#ifdef USE_AVALON
#define DRV_AVALON 6
#endif
#define DRV_LAST -1
#define USB_CONFIG 1
#ifdef WIN32
#define BFLSC_TIMEOUT_MS 999
#define BITFORCE_TIMEOUT_MS 999
#define MODMINER_TIMEOUT_MS 999
#define AVALON_TIMEOUT_MS 999
#define ICARUS_TIMEOUT_MS 999
#else
#define BFLSC_TIMEOUT_MS 300
#define BITFORCE_TIMEOUT_MS 200
#define MODMINER_TIMEOUT_MS 100
#define AVALON_TIMEOUT_MS 200
#define ICARUS_TIMEOUT_MS 200
#endif
#define USB_READ_MINPOLL 40
#ifdef USE_BFLSC
// N.B. transfer size is 512 with USB2.0, but only 64 with USB1.1
static struct usb_endpoints bas_eps[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
};
#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
#ifdef USE_AVALON
static struct usb_endpoints ava_eps[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
};
#endif
#ifdef USE_ICARUS
static struct usb_endpoints ica_eps[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
};
static struct usb_endpoints amu_eps[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0 }
};
static struct usb_endpoints llt_eps[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
};
static struct usb_endpoints cmr1_eps[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
/*
Interface 1
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(4), 0 },
Interface 2
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(5), 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(6), 0 },
Interface 3
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(7), 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(8), 0 }
*/
};
static struct usb_endpoints cmr2_eps[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
};
#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",
.ident = IDENT_BAS,
.idVendor = IDVENDOR_FTDI,
.idProduct = 0x6014,
//.iManufacturer = "Butterfly Labs",
.iProduct = "BitFORCE SHA256 SC",
.kernel = 0,
.config = 1,
.interface = 0,
.timeout = BFLSC_TIMEOUT_MS,
.latency = LATENCY_STD,
.epcount = ARRAY_SIZE(bas_eps),
.eps = bas_eps },
#endif
#ifdef USE_BITFORCE
{
.drv = DRV_BITFORCE,
.name = "BFL",
.ident = IDENT_BFL,
.idVendor = IDVENDOR_FTDI,
.idProduct = 0x6014,
.iManufacturer = "Butterfly Labs Inc.",
.iProduct = "BitFORCE SHA256",
.kernel = 0,
.config = 1,
.interface = 0,
.timeout = BITFORCE_TIMEOUT_MS,
.latency = LATENCY_STD,
.epcount = ARRAY_SIZE(bfl_eps),
.eps = bfl_eps },
#endif
#ifdef USE_MODMINER
{
.drv = DRV_MODMINER,
.name = "MMQ",
.ident = IDENT_MMQ,
.idVendor = 0x1fc9,
.idProduct = 0x0003,
.kernel = 0,
.config = 1,
.interface = 1,
.timeout = MODMINER_TIMEOUT_MS,
.latency = LATENCY_UNUSED,
.epcount = ARRAY_SIZE(mmq_eps),
.eps = mmq_eps },
#endif
#ifdef USE_AVALON
{
.drv = DRV_AVALON,
.name = "AVA",
.ident = IDENT_AVA,
.idVendor = IDVENDOR_FTDI,
.idProduct = 0x6001,
.kernel = 0,
.config = 1,
.interface = 0,
.timeout = AVALON_TIMEOUT_MS,
.latency = 10,
.epcount = ARRAY_SIZE(ava_eps),
.eps = ava_eps },
#endif
#ifdef USE_ICARUS
{
.drv = DRV_ICARUS,
.name = "ICA",
.ident = IDENT_ICA,
.idVendor = 0x067b,
.idProduct = 0x2303,
.kernel = 0,
.config = 1,
.interface = 0,
.timeout = ICARUS_TIMEOUT_MS,
.latency = LATENCY_UNUSED,
.epcount = ARRAY_SIZE(ica_eps),
.eps = ica_eps },
{
.drv = DRV_ICARUS,
.name = "AMU",
.ident = IDENT_AMU,
.idVendor = 0x10c4,
.idProduct = 0xea60,
.kernel = 0,
.config = 1,
.interface = 0,
.timeout = ICARUS_TIMEOUT_MS,
.latency = LATENCY_UNUSED,
.epcount = ARRAY_SIZE(amu_eps),
.eps = amu_eps },
{
.drv = DRV_ICARUS,
.name = "BLT",
.ident = IDENT_BLT,
.idVendor = IDVENDOR_FTDI,
.idProduct = 0x6001,
.iProduct = "FT232R USB UART",
.kernel = 0,
.config = 1,
.interface = 0,
.timeout = ICARUS_TIMEOUT_MS,
.latency = LATENCY_STD,
.epcount = ARRAY_SIZE(llt_eps),
.eps = llt_eps },
// For any that don't match the above "BLT"
{
.drv = DRV_ICARUS,
.name = "LLT",
.ident = IDENT_LLT,
.idVendor = IDVENDOR_FTDI,
.idProduct = 0x6001,
.kernel = 0,
.config = 1,
.interface = 0,
.timeout = ICARUS_TIMEOUT_MS,
.latency = LATENCY_STD,
.epcount = ARRAY_SIZE(llt_eps),
.eps = llt_eps },
{
.drv = DRV_ICARUS,
.name = "CMR",
.ident = IDENT_CMR1,
.idVendor = IDVENDOR_FTDI,
.idProduct = 0x8350,
.iProduct = "Cairnsmore1",
.kernel = 0,
.config = 1,
.interface = 0,
.timeout = ICARUS_TIMEOUT_MS,
.latency = LATENCY_STD,
.epcount = ARRAY_SIZE(cmr1_eps),
.eps = cmr1_eps },
{
.drv = DRV_ICARUS,
.name = "CMR",
.ident = IDENT_CMR2,
.idVendor = IDVENDOR_FTDI,
.idProduct = 0x6014,
.iProduct = "Cairnsmore1",
.kernel = 0,
.config = 1,
.interface = 0,
.timeout = ICARUS_TIMEOUT_MS,
.latency = LATENCY_STD,
.epcount = ARRAY_SIZE(cmr2_eps),
.eps = cmr2_eps },
#endif
#ifdef USE_ZTEX
// This is here so cgminer -n shows them
// the ztex driver (as at 201303) doesn't use usbutils
{
.drv = DRV_ZTEX,
.name = "ZTX",
.ident = IDENT_ZTX,
.idVendor = 0x221a,
.idProduct = 0x0100,
.kernel = 0,
.config = 1,
.interface = 1,
.timeout = 100,
.latency = LATENCY_UNUSED,
.epcount = 0,
.eps = NULL },
#endif
{ DRV_LAST, NULL, 0, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 0, NULL }
};
#ifdef USE_BFLSC
extern struct device_drv bflsc_drv;
#endif
#ifdef USE_BITFORCE
extern struct device_drv bitforce_drv;
#endif
#ifdef USE_MODMINER
extern struct device_drv modminer_drv;
#endif
#ifdef USE_ICARUS
extern struct device_drv icarus_drv;
#endif
#ifdef USE_AVALON
extern struct device_drv avalon_drv;
#endif
#define STRBUFLEN 256
static const char *BLANK = "";
static const char *space = " ";
static const char *nodatareturned = "no data returned ";
#define IOERR_CHECK(cgpu, err) \
if (err == LIBUSB_ERROR_IO) { \
cgpu->usbinfo.ioerr_count++; \
cgpu->usbinfo.continuous_ioerr_count++; \
} else { \
cgpu->usbinfo.continuous_ioerr_count = 0; \
}
#if 0 // enable USBDEBUG - only during development testing
static const char *debug_true_str = "true";
static const char *debug_false_str = "false";
static const char *nodevstr = "=NODEV";
#define bool_str(boo) ((boo) ? debug_true_str : debug_false_str)
#define isnodev(err) (NODEV(err) ? nodevstr : BLANK)
#define USBDEBUG(fmt, ...) applog(LOG_WARNING, fmt, ##__VA_ARGS__)
#else
#define USBDEBUG(fmt, ...)
#endif
// For device limits by driver
static struct driver_count {
uint32_t count;
uint32_t limit;
} drv_count[DRIVER_MAX];
// For device limits by list of bus/dev
static struct usb_busdev {
int bus_number;
int device_address;
void *resource1;
void *resource2;
} *busdev;
static int busdev_count = 0;
// Total device limit
static int total_count = 0;
static int total_limit = 999999;
struct usb_in_use_list {
struct usb_busdev in_use;
struct usb_in_use_list *prev;
struct usb_in_use_list *next;
};
// List of in use devices
static struct usb_in_use_list *in_use_head = NULL;
struct resource_work {
bool lock;
const char *dname;
uint8_t bus_number;
uint8_t device_address;
struct resource_work *next;
};
// Pending work for the reslock thread
struct resource_work *res_work_head = NULL;
struct resource_reply {
uint8_t bus_number;
uint8_t device_address;
bool got;
struct resource_reply *next;
};
// Replies to lock requests
struct resource_reply *res_reply_head = NULL;
// Set this to 0 to remove stats processing
#define DO_USB_STATS 1
static bool stats_initialised = false;
#if DO_USB_STATS
// NONE must be 0 - calloced
#define MODE_NONE 0
#define MODE_CTRL_READ (1 << 0)
#define MODE_CTRL_WRITE (1 << 1)
#define MODE_BULK_READ (1 << 2)
#define MODE_BULK_WRITE (1 << 3)
#define MODE_SEP_STR "+"
#define MODE_NONE_STR "X"
#define MODE_CTRL_READ_STR "cr"
#define MODE_CTRL_WRITE_STR "cw"
#define MODE_BULK_READ_STR "br"
#define MODE_BULK_WRITE_STR "bw"
// One for each CMD, TIMEOUT, ERROR
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
// One for each C_CMD
struct cg_usb_stats_details {
int seq;
uint32_t modes;
struct cg_usb_stats_item item[CMD_ERROR+1];
};
// One for each device
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 = USB_NOSTAT;
#define USB_STATS(sgpu, sta, fin, err, mode, cmd, seq) \
stats(cgpu, sta, fin, err, mode, cmd, seq)
#define STATS_TIMEVAL(tv) cgtime(tv)
#define USB_REJECT(sgpu, mode) rejected_inc(sgpu, mode)
#else
#define USB_STATS(sgpu, sta, fin, err, mode, cmd, seq)
#define STATS_TIMEVAL(tv)
#define USB_REJECT(sgpu, mode)
#endif // DO_USB_STATS
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";
static const char *C_FLASHREPLY_S = "FlashReply";
static const char *C_REQUESTDETAILS_S = "RequestDetails";
static const char *C_GETDETAILS_S = "GetDetails";
static const char *C_REQUESTRESULTS_S = "RequestResults";
static const char *C_GETRESULTS_S = "GetResults";
static const char *C_REQUESTQUEJOB_S = "RequestQueJob";
static const char *C_REQUESTQUEJOBSTATUS_S = "RequestQueJobStatus";
static const char *C_QUEJOB_S = "QueJob";
static const char *C_QUEJOBSTATUS_S = "QueJobStatus";
static const char *C_QUEFLUSH_S = "QueFlush";
static const char *C_QUEFLUSHREPLY_S = "QueFlushReply";
static const char *C_REQUESTVOLTS_S = "RequestVolts";
static const char *C_SENDTESTWORK_S = "SendTestWork";
static const char *C_LATENCY_S = "SetLatency";
static const char *C_SETLINE_S = "SetLine";
static const char *C_VENDOR_S = "Vendor";
static const char *C_SETFAN_S = "SetFan";
static const char *C_FANREPLY_S = "GetFan";
static const char *C_AVALON_TASK_S = "AvalonTask";
static const char *C_AVALON_READ_S = "AvalonRead";
static const char *C_GET_AVALON_READY_S = "AvalonReady";
static const char *C_AVALON_RESET_S = "AvalonReset";
static const char *C_GET_AVALON_RESET_S = "GetAvalonReset";
static const char *C_FTDI_STATUS_S = "FTDIStatus";
static const char *C_ENABLE_UART_S = "EnableUART";
#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 *err_io_str = " IO Error";
static const char *err_access_str = " Access Denied-a";
static const char *err_timeout_str = " Reply Timeout";
static const char *err_pipe_str = " Access denied-p";
static const char *err_other_str = " Access denied-o";
static const char *usberrstr(int err)
{
switch (err) {
case LIBUSB_ERROR_IO:
return err_io_str;
case LIBUSB_ERROR_ACCESS:
return err_access_str;
case LIBUSB_ERROR_TIMEOUT:
return err_timeout_str;
case LIBUSB_ERROR_PIPE:
return err_pipe_str;
case LIBUSB_ERROR_OTHER:
return err_other_str;
}
return BLANK;
}
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);
if (unlikely(!*buf))
quit(1, "USB failed to realloc append");
}
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, int level)
{
struct libusb_device_descriptor desc;
uint8_t bus_number;
uint8_t device_address;
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 (opt_usb_list_all && err) {
sprintf(tmp, EOL ".USB dev %d: Failed to get descriptor, err %d",
(int)(++(*count)), err);
append(buf, tmp, off, len);
return;
}
bus_number = libusb_get_bus_number(dev);
device_address = libusb_get_device_address(dev);
if (!opt_usb_list_all) {
bool known = false;
for (i = 0; find_dev[i].drv != DRV_LAST; i++)
if ((find_dev[i].idVendor == desc.idVendor) &&
(find_dev[i].idProduct == desc.idProduct)) {
known = true;
break;
}
if (!known)
return;
}
(*count)++;
if (level == 0) {
sprintf(tmp, EOL ".USB dev %d: Bus %d Device %d ID: %04x:%04x",
(int)(*count), (int)bus_number, (int)device_address,
desc.idVendor, desc.idProduct);
} else {
sprintf(tmp, EOL ".USB dev %d: Bus %d Device %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)bus_number, (int)device_address,
(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;
}
err = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, man, STRBUFLEN);
if (err < 0)
sprintf((char *)man, "** err(%d)%s", err, usberrstr(err));
err = libusb_get_string_descriptor_ascii(handle, desc.iProduct, prod, STRBUFLEN);
if (err < 0)
sprintf((char *)prod, "** err(%d)%s", err, usberrstr(err));
if (level == 0) {
libusb_close(handle);
sprintf(tmp, EOL " Manufacturer: '%s'" EOL " Product: '%s'", man, prod);
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.iSerialNumber, ser, STRBUFLEN);
if (err < 0)
sprintf((char *)ser, "** err(%d)%s", err, usberrstr(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
void usb_all(int level)
{
libusb_device **list;
ssize_t count, i, j;
char *buf;
size_t len, off;
count = libusb_get_device_list(NULL, &list);
if (count < 0) {
applog(LOG_ERR, "USB all: failed, err %d%s", (int)count, usberrstr((int)count));
return;
}
if (count == 0)
applog(LOG_WARNING, "USB all: found no devices");
else
{
len = 10000;
buf = malloc(len+1);
if (unlikely(!buf))
quit(1, "USB failed to malloc buf in usb_all");
sprintf(buf, "USB all: found %d devices", (int)count);
off = strlen(buf);
if (!opt_usb_list_all)
append(&buf, " - listing known devices", &off, &len);
j = -1;
for (i = 0; i < count; i++)
usb_full(&j, list[i], &buf, &off, &len, level);
_applog(LOG_WARNING, buf);
free(buf);
if (j == -1)
applog(LOG_WARNING, "No known USB devices");
else
applog(LOG_WARNING, "%d %sUSB devices",
(int)(++j), opt_usb_list_all ? BLANK : "known ");
}
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(opt_usbdump);
}
usb_commands = malloc(sizeof(*usb_commands) * C_MAX);
if (unlikely(!usb_commands))
quit(1, "USB failed to malloc usb_commands");
// 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;
usb_commands[C_FLASHREPLY] = C_FLASHREPLY_S;
usb_commands[C_REQUESTDETAILS] = C_REQUESTDETAILS_S;
usb_commands[C_GETDETAILS] = C_GETDETAILS_S;
usb_commands[C_REQUESTRESULTS] = C_REQUESTRESULTS_S;
usb_commands[C_GETRESULTS] = C_GETRESULTS_S;
usb_commands[C_REQUESTQUEJOB] = C_REQUESTQUEJOB_S;
usb_commands[C_REQUESTQUEJOBSTATUS] = C_REQUESTQUEJOBSTATUS_S;
usb_commands[C_QUEJOB] = C_QUEJOB_S;
usb_commands[C_QUEJOBSTATUS] = C_QUEJOBSTATUS_S;
usb_commands[C_QUEFLUSH] = C_QUEFLUSH_S;
usb_commands[C_QUEFLUSHREPLY] = C_QUEFLUSHREPLY_S;
usb_commands[C_REQUESTVOLTS] = C_REQUESTVOLTS_S;
usb_commands[C_SENDTESTWORK] = C_SENDTESTWORK_S;
usb_commands[C_LATENCY] = C_LATENCY_S;
usb_commands[C_SETLINE] = C_SETLINE_S;
usb_commands[C_VENDOR] = C_VENDOR_S;
usb_commands[C_SETFAN] = C_SETFAN_S;
usb_commands[C_FANREPLY] = C_FANREPLY_S;
usb_commands[C_AVALON_TASK] = C_AVALON_TASK_S;
usb_commands[C_AVALON_READ] = C_AVALON_READ_S;
usb_commands[C_GET_AVALON_READY] = C_GET_AVALON_READY_S;
usb_commands[C_AVALON_RESET] = C_AVALON_RESET_S;
usb_commands[C_GET_AVALON_RESET] = C_GET_AVALON_RESET_S;
usb_commands[C_FTDI_STATUS] = C_FTDI_STATUS_S;
usb_commands[C_ENABLE_UART] = C_ENABLE_UART_S;
stats_initialised = true;
}
mutex_unlock(&cgusb_lock);
}
const char *usb_cmdname(enum usb_cmds cmd)
{
cgusb_check_init();
return usb_commands[cmd];
}
void usb_applog(struct cgpu_info *cgpu, enum usb_cmds cmd, char *msg, int amount, int err)
{
if (msg && !*msg)
msg = NULL;
if (!msg && amount == 0 && err == LIBUSB_SUCCESS)
msg = (char *)nodatareturned;
applog(LOG_ERR, "%s%i: %s failed%s%s (err=%d amt=%d)",
cgpu->drv->name, cgpu->device_id,
usb_cmdname(cmd),
msg ? space : BLANK, msg ? msg : BLANK,
err, amount);
}
static void in_use_store_ress(uint8_t bus_number, uint8_t device_address, void *resource1, void *resource2)
{
struct usb_in_use_list *in_use_tmp;
bool found = false, empty = true;
mutex_lock(&cgusb_lock);
in_use_tmp = in_use_head;
while (in_use_tmp) {
if (in_use_tmp->in_use.bus_number == (int)bus_number &&
in_use_tmp->in_use.device_address == (int)device_address) {
found = true;
if (in_use_tmp->in_use.resource1)
empty = false;
in_use_tmp->in_use.resource1 = resource1;
if (in_use_tmp->in_use.resource2)
empty = false;
in_use_tmp->in_use.resource2 = resource2;
break;
}
in_use_tmp = in_use_tmp->next;
}
mutex_unlock(&cgusb_lock);
if (found == false)
applog(LOG_ERR, "FAIL: USB store_ress not found (%d:%d)",
(int)bus_number, (int)device_address);
if (empty == false)
applog(LOG_ERR, "FAIL: USB store_ress not empty (%d:%d)",
(int)bus_number, (int)device_address);
}
static void in_use_get_ress(uint8_t bus_number, uint8_t device_address, void **resource1, void **resource2)
{
struct usb_in_use_list *in_use_tmp;
bool found = false, empty = false;
mutex_lock(&cgusb_lock);
in_use_tmp = in_use_head;
while (in_use_tmp) {
if (in_use_tmp->in_use.bus_number == (int)bus_number &&
in_use_tmp->in_use.device_address == (int)device_address) {
found = true;
if (!in_use_tmp->in_use.resource1)
empty = true;
*resource1 = in_use_tmp->in_use.resource1;
in_use_tmp->in_use.resource1 = NULL;
if (!in_use_tmp->in_use.resource2)
empty = true;
*resource2 = in_use_tmp->in_use.resource2;
in_use_tmp->in_use.resource2 = NULL;
break;
}
in_use_tmp = in_use_tmp->next;
}
mutex_unlock(&cgusb_lock);
if (found == false)
applog(LOG_ERR, "FAIL: USB get_lock not found (%d:%d)",
(int)bus_number, (int)device_address);
if (empty == true)
applog(LOG_ERR, "FAIL: USB get_lock empty (%d:%d)",
(int)bus_number, (int)device_address);
}
static bool __is_in_use(uint8_t bus_number, uint8_t device_address)
{
struct usb_in_use_list *in_use_tmp;
bool ret = false;
in_use_tmp = in_use_head;
while (in_use_tmp) {
if (in_use_tmp->in_use.bus_number == (int)bus_number &&
in_use_tmp->in_use.device_address == (int)device_address) {
ret = true;
break;
}
in_use_tmp = in_use_tmp->next;
}
return ret;
}
static bool is_in_use_bd(uint8_t bus_number, uint8_t device_address)
{
bool ret;
mutex_lock(&cgusb_lock);
ret = __is_in_use(bus_number, device_address);
mutex_unlock(&cgusb_lock);
return ret;
}
static bool is_in_use(libusb_device *dev)
{
return is_in_use_bd(libusb_get_bus_number(dev), libusb_get_device_address(dev));
}
static void add_in_use(uint8_t bus_number, uint8_t device_address)
{
struct usb_in_use_list *in_use_tmp;
bool found = false;
mutex_lock(&cgusb_lock);
if (unlikely(__is_in_use(bus_number, device_address))) {
found = true;
goto nofway;
}
in_use_tmp = calloc(1, sizeof(*in_use_tmp));
if (unlikely(!in_use_tmp))
quit(1, "USB failed to calloc in_use_tmp");
in_use_tmp->in_use.bus_number = (int)bus_number;
in_use_tmp->in_use.device_address = (int)device_address;
in_use_tmp->next = in_use_head;
if (in_use_head)
in_use_head->prev = in_use_tmp;
in_use_head = in_use_tmp;
nofway:
mutex_unlock(&cgusb_lock);
if (found)
applog(LOG_ERR, "FAIL: USB add already in use (%d:%d)",
(int)bus_number, (int)device_address);
}
static void remove_in_use(uint8_t bus_number, uint8_t device_address)
{
struct usb_in_use_list *in_use_tmp;
bool found = false;
mutex_lock(&cgusb_lock);
in_use_tmp = in_use_head;
while (in_use_tmp) {
if (in_use_tmp->in_use.bus_number == (int)bus_number &&
in_use_tmp->in_use.device_address == (int)device_address) {
found = true;
if (in_use_tmp == in_use_head) {
in_use_head = in_use_head->next;
if (in_use_head)
in_use_head->prev = NULL;
} else {
in_use_tmp->prev->next = in_use_tmp->next;
if (in_use_tmp->next)
in_use_tmp->next->prev = in_use_tmp->prev;
}
free(in_use_tmp);
break;
}
in_use_tmp = in_use_tmp->next;
}
mutex_unlock(&cgusb_lock);
if (!found)
applog(LOG_ERR, "FAIL: USB remove not already in use (%d:%d)",
(int)bus_number, (int)device_address);
}
static bool cgminer_usb_lock_bd(struct device_drv *drv, uint8_t bus_number, uint8_t device_address)
{
struct resource_work *res_work;
bool ret;
applog(LOG_DEBUG, "USB lock %s %d-%d", drv->dname, (int)bus_number, (int)device_address);
res_work = calloc(1, sizeof(*res_work));
if (unlikely(!res_work))
quit(1, "USB failed to calloc lock res_work");
res_work->lock = true;
res_work->dname = (const char *)(drv->dname);
res_work->bus_number = bus_number;
res_work->device_address = device_address;
mutex_lock(&cgusbres_lock);
res_work->next = res_work_head;
res_work_head = res_work;
mutex_unlock(&cgusbres_lock);
cgsem_post(&usb_resource_sem);
// TODO: add a timeout fail - restart the resource thread?
while (true) {
nmsleep(50);
mutex_lock(&cgusbres_lock);
if (res_reply_head) {
struct resource_reply *res_reply_prev = NULL;
struct resource_reply *res_reply = res_reply_head;
while (res_reply) {
if (res_reply->bus_number == bus_number &&
res_reply->device_address == device_address) {
if (res_reply_prev)
res_reply_prev->next = res_reply->next;
else
res_reply_head = res_reply->next;
mutex_unlock(&cgusbres_lock);
ret = res_reply->got;
free(res_reply);
return ret;
}
res_reply_prev = res_reply;
res_reply = res_reply->next;
}
}
mutex_unlock(&cgusbres_lock);
}
}
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));
}
static void cgminer_usb_unlock_bd(struct device_drv *drv, uint8_t bus_number, uint8_t device_address)
{
struct resource_work *res_work;
applog(LOG_DEBUG, "USB unlock %s %d-%d", drv->dname, (int)bus_number, (int)device_address);
res_work = calloc(1, sizeof(*res_work));
if (unlikely(!res_work))
quit(1, "USB failed to calloc unlock res_work");
res_work->lock = false;
res_work->dname = (const char *)(drv->dname);
res_work->bus_number = bus_number;
res_work->device_address = device_address;
mutex_lock(&cgusbres_lock);
res_work->next = res_work_head;
res_work_head = res_work;
mutex_unlock(&cgusbres_lock);
cgsem_post(&usb_resource_sem);
return;
}
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)
{
applog(LOG_DEBUG, "USB free %s", cgusb->found->name);
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);
if (cgusb->buffer)
free(cgusb->buffer);
free(cgusb);
return NULL;
}
void usb_uninit(struct cgpu_info *cgpu)
{
applog(LOG_DEBUG, "USB uninit %s%i",
cgpu->drv->name, cgpu->device_id);
// May have happened already during a failed initialisation
// if release_cgpu() was called due to a USB NODEV(err)
if (!cgpu->usbdev)
return;
if (!libusb_release_interface(cgpu->usbdev->handle, cgpu->usbdev->found->interface)) {
cg_wlock(&cgusb_fd_lock);
libusb_close(cgpu->usbdev->handle);
cg_wunlock(&cgusb_fd_lock);
}
cgpu->usbdev = free_cgusb(cgpu->usbdev);
}
/*
* N.B. this is always called inside
* DEVLOCK(cgpu, pstate);
*/
static void release_cgpu(struct cgpu_info *cgpu)
{
struct cg_usb_device *cgusb = cgpu->usbdev;
struct cgpu_info *lookcgpu;
int i;
applog(LOG_DEBUG, "USB release %s%i",
cgpu->drv->name, cgpu->device_id);
// It has already been done
if (cgpu->usbinfo.nodev)
return;
zombie_devs++;
total_count--;
drv_count[cgpu->drv->drv_id].count--;
cgpu->usbinfo.nodev = true;
cgpu->usbinfo.nodev_count++;
cgtime(&cgpu->usbinfo.last_nodev);
// 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++) {
lookcgpu = get_devices(i);
if (lookcgpu != cgpu && lookcgpu->usbdev == cgusb) {
total_count--;
drv_count[lookcgpu->drv->drv_id].count--;
lookcgpu->usbinfo.nodev = true;
lookcgpu->usbinfo.nodev_count++;
memcpy(&(lookcgpu->usbinfo.last_nodev),
&(cgpu->usbinfo.last_nodev), sizeof(struct timeval));
lookcgpu->usbdev = NULL;
}
}
usb_uninit(cgpu);
cgminer_usb_unlock_bd(cgpu->drv, cgpu->usbinfo.bus_number, cgpu->usbinfo.device_address);
}
// Currently only used by MMQ
struct cgpu_info *usb_copy_cgpu(struct cgpu_info *orig)
{
struct cgpu_info *copy = calloc(1, sizeof(*copy));
if (unlikely(!copy))
quit(1, "Failed to calloc cgpu for %s in usb_copy_cgpu", orig->drv->dname);
copy->name = orig->name;
copy->drv = copy_drv(orig->drv);
copy->deven = orig->deven;
copy->threads = orig->threads;
copy->usbdev = orig->usbdev;
memcpy(&(copy->usbinfo), &(orig->usbinfo), sizeof(copy->usbinfo));
copy->usbinfo.nodev = (copy->usbdev == NULL);
copy->usbinfo.devlock = orig->usbinfo.devlock;
return copy;
}
struct cgpu_info *usb_alloc_cgpu(struct device_drv *drv, int threads)
{
struct cgpu_info *cgpu = calloc(1, sizeof(*cgpu));
if (unlikely(!cgpu))
quit(1, "Failed to calloc cgpu for %s in usb_alloc_cgpu", drv->dname);
cgpu->drv = drv;
cgpu->deven = DEV_ENABLED;
cgpu->threads = threads;
cgpu->usbinfo.nodev = true;
cgpu->usbinfo.devlock = calloc(1, sizeof(*(cgpu->usbinfo.devlock)));
if (unlikely(!cgpu->usbinfo.devlock))
quit(1, "Failed to calloc devlock for %s in usb_alloc_cgpu", drv->dname);
rwlock_init(cgpu->usbinfo.devlock);
return cgpu;
}
struct cgpu_info *usb_free_cgpu_devlock(struct cgpu_info *cgpu, bool free_devlock)
{
if (cgpu->drv->copy)
free(cgpu->drv);
free(cgpu->device_path);
if (free_devlock)
free(cgpu->usbinfo.devlock);
free(cgpu);
return NULL;
}
#define USB_INIT_FAIL 0
#define USB_INIT_OK 1
#define USB_INIT_IGNORE 2
/*
* WARNING - these assume DEVLOCK(cgpu, pstate) is called first and
* DEVUNLOCK(cgpu, pstate) in called in the same function with the same pstate
* given to DEVLOCK.
* You must call DEVUNLOCK(cgpu, pstate) before exiting the function or it will leave
* the thread Cancelability unrestored
*/
#define DEVLOCK(cgpu, _pth_state) do { \
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &_pth_state); \
wr_lock(cgpu->usbinfo.devlock); \
} while (0)
#define DEVUNLOCK(cgpu, _pth_state) do { \
wr_unlock(cgpu->usbinfo.devlock); \
pthread_setcancelstate(_pth_state, NULL); \
} while (0)
static int _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 devpath[32];
char devstr[STRBUFLEN+1];
int err, i, j, k, pstate;
int bad = USB_INIT_FAIL;
DEVLOCK(cgpu, pstate);
cgpu->usbinfo.bus_number = libusb_get_bus_number(dev);
cgpu->usbinfo.device_address = libusb_get_device_address(dev);
sprintf(devpath, "%d:%d",
(int)(cgpu->usbinfo.bus_number),
(int)(cgpu->usbinfo.device_address));
cgpu->device_path = strdup(devpath);
sprintf(devstr, "- %s device %s", found->name, devpath);
cgusb = calloc(1, sizeof(*cgusb));
if (unlikely(!cgusb))
quit(1, "USB failed to calloc _usb_init cgusb");
cgusb->found = found;
if (found->idVendor == IDVENDOR_FTDI)
cgusb->usb_type = USB_TYPE_FTDI;
cgusb->ident = found->ident;
cgusb->descriptor = calloc(1, sizeof(*(cgusb->descriptor)));
if (unlikely(!cgusb->descriptor))
quit(1, "USB failed to calloc _usb_init 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;
}
cg_wlock(&cgusb_fd_lock);
err = libusb_open(dev, &(cgusb->handle));
cg_wunlock(&cgusb_fd_lock);
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 WinUSB driver for %s",
err, devstr);
break;
#endif
default:
applog(LOG_DEBUG,
"USB init, open failed, err %d %s",
err, devstr);
}
goto dame;
}
#ifndef WIN32
if (libusb_kernel_driver_active(cgusb->handle, found->kernel) == 1) {
applog(LOG_DEBUG, "USB init, kernel attached ... %s", devstr);
err = libusb_detach_kernel_driver(cgusb->handle, found->kernel);
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;
}
}
#endif
if (found->iManufacturer) {
unsigned char man[STRBUFLEN+1];
err = libusb_get_string_descriptor_ascii(cgusb->handle,
cgusb->descriptor->iManufacturer,
man, STRBUFLEN);
if (err < 0) {
applog(LOG_DEBUG,
"USB init, failed to get iManufacturer, err %d %s",
err, devstr);
goto cldame;
}
if (strcmp((char *)man, found->iManufacturer)) {
applog(LOG_DEBUG, "USB init, iManufacturer mismatch %s",
devstr);
bad = USB_INIT_IGNORE;
goto cldame;
}
}
if (found->iProduct) {
unsigned char prod[STRBUFLEN+1];
err = libusb_get_string_descriptor_ascii(cgusb->handle,
cgusb->descriptor->iProduct,
prod, STRBUFLEN);
if (err < 0) {
applog(LOG_DEBUG,
"USB init, failed to get iProduct, err %d %s",
err, devstr);
goto cldame;
}
if (strcmp((char *)prod, found->iProduct)) {
applog(LOG_DEBUG, "USB init, iProduct mismatch %s",
devstr);
bad = USB_INIT_IGNORE;
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, err %d %s",
err, devstr);
goto cldame;
}
if ((int)(config->bNumInterfaces) <= found->interface) {
applog(LOG_DEBUG, "USB init bNumInterfaces <= interface %s",
devstr);
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;
found->wMaxPacketSize = epdesc->wMaxPacketSize;
break;
}
}
}
}
}
for (i = 0; i < found->epcount; i++) {
if (found->eps[i].found == false) {
applog(LOG_DEBUG, "USB init found == false %s",
devstr);
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;
cgpu->usbinfo.nodev = false;
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);
}
bad = USB_INIT_OK;
goto out_unlock;
cldame:
cg_wlock(&cgusb_fd_lock);
libusb_close(cgusb->handle);
cg_wunlock(&cgusb_fd_lock);
dame:
if (config)
libusb_free_config_descriptor(config);
cgusb = free_cgusb(cgusb);
out_unlock:
DEVUNLOCK(cgpu, pstate);
return bad;
}
bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found)
{
int ret;
ret = _usb_init(cgpu, dev, found);
if (ret == USB_INIT_FAIL)
applog(LOG_ERR, "%s detect (%d:%d) failed to initialise (incorrect device?)",
cgpu->drv->dname,
(int)(cgpu->usbinfo.bus_number),
(int)(cgpu->usbinfo.device_address));
return (ret == USB_INIT_OK);
}
static bool usb_check_device(struct device_drv *drv, struct libusb_device *dev, struct usb_find_devices *look)
{
struct libusb_device_descriptor desc;
int bus_number, device_address;
int err, i;
bool ok;
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;
}
if (busdev_count > 0) {
bus_number = (int)libusb_get_bus_number(dev);
device_address = (int)libusb_get_device_address(dev);
ok = false;
for (i = 0; i < busdev_count; i++) {
if (bus_number == busdev[i].bus_number) {
if (busdev[i].device_address == -1 ||
device_address == busdev[i].device_address) {
ok = true;
break;
}
}
}
if (!ok) {
applog(LOG_DEBUG, "%s rejected %s %04x:%04x with bus:dev (%d:%d)",
drv->name, look->name, look->idVendor, look->idProduct,
bus_number, device_address);
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));
if (unlikely(!found))
quit(1, "USB failed to malloc 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)
{
if (drv_count[drv->drv_id].count >= drv_count[drv->drv_id].limit) {
applog(LOG_DEBUG,
"USB scan devices3: %s limit %d reached",
drv->dname, drv_count[drv->drv_id].limit);
return NULL;
}
#ifdef USE_BFLSC
if (drv->drv_id == DRIVER_BFLSC)
return usb_check_each(DRV_BFLSC, drv, dev);
#endif
#ifdef USE_BITFORCE
if (drv->drv_id == DRIVER_BITFORCE)
return usb_check_each(DRV_BITFORCE, drv, dev);
#endif
#ifdef USE_MODMINER
if (drv->drv_id == DRIVER_MODMINER)
return usb_check_each(DRV_MODMINER, drv, dev);
#endif
#ifdef USE_ICARUS
if (drv->drv_id == DRIVER_ICARUS)
return usb_check_each(DRV_ICARUS, drv, dev);
#endif
#ifdef USE_AVALON
if (drv->drv_id == DRIVER_AVALON)
return usb_check_each(DRV_AVALON, 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;
applog(LOG_DEBUG, "USB scan devices: checking for %s devices", drv->name);
if (total_count >= total_limit) {
applog(LOG_DEBUG, "USB scan devices: total limit %d reached", total_limit);
return;
}
if (drv_count[drv->drv_id].count >= drv_count[drv->drv_id].limit) {
applog(LOG_DEBUG,
"USB scan devices: %s limit %d reached",
drv->dname, drv_count[drv->drv_id].limit);
return;
}
count = libusb_get_device_list(NULL, &list);
if (count < 0) {
applog(LOG_DEBUG, "USB scan devices: failed, err %d", (int)count);
return;
}
if (count == 0)
applog(LOG_DEBUG, "USB scan devices: found no devices");
for (i = 0; i < count; i++) {
if (total_count >= total_limit) {
applog(LOG_DEBUG, "USB scan devices2: total limit %d reached", total_limit);
break;
}
if (drv_count[drv->drv_id].count >= drv_count[drv->drv_id].limit) {
applog(LOG_DEBUG,
"USB scan devices2: %s limit %d reached",
drv->dname, drv_count[drv->drv_id].limit);
break;
}
found = usb_check(drv, list[i]);
if (found != NULL) {
if (is_in_use(list[i]) || cgminer_usb_lock(drv, list[i]) == false)
free(found);
else {
if (!device_detect(list[i], found))
cgminer_usb_unlock(drv, list[i]);
else {
total_count++;
drv_count[drv->drv_id].count++;
}
}
}
}
libusb_free_device_list(list, 1);
}
#if DO_USB_STATS
static void modes_str(char *buf, uint32_t modes)
{
bool first;
*buf = '\0';
if (modes == MODE_NONE)
strcpy(buf, MODE_NONE_STR);
else {
first = true;
if (modes & MODE_CTRL_READ) {
strcpy(buf, MODE_CTRL_READ_STR);
first = false;
}
if (modes & MODE_CTRL_WRITE) {
if (!first)
strcat(buf, MODE_SEP_STR);
strcat(buf, MODE_CTRL_WRITE_STR);
first = false;
}
if (modes & MODE_BULK_READ) {
if (!first)
strcat(buf, MODE_SEP_STR);
strcat(buf, MODE_BULK_READ_STR);
first = false;
}
if (modes & MODE_BULK_WRITE) {
if (!first)
strcat(buf, MODE_SEP_STR);
strcat(buf, MODE_BULK_WRITE_STR);
first = false;
}
}
}
#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;
char modes_s[32];
if (next_stat == USB_NOSTAT)
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);
modes_str(modes_s, details->modes);
root = api_add_string(root, "Modes", modes_s, 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;
mutex_lock(&cgusb_lock);
cgpu->usbinfo.usbstat = next_stat + 1;
usb_stats = realloc(usb_stats, sizeof(*usb_stats) * (next_stat+1));
if (unlikely(!usb_stats))
quit(1, "USB failed to realloc usb_stats %d", next_stat+1);
usb_stats[next_stat].name = cgpu->drv->name;
usb_stats[next_stat].device_id = -1;
usb_stats[next_stat].details = calloc(1, sizeof(struct cg_usb_stats_details) * C_MAX * 2);
if (unlikely(!usb_stats[next_stat].details))
quit(1, "USB failed to calloc details for %d", next_stat+1);
for (i = 1; i < C_MAX * 2; i += 2)
usb_stats[next_stat].details[i].seq = 1;
next_stat++;
mutex_unlock(&cgusb_lock);
}
#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, int mode, 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]);
details->modes |= mode;
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, uint32_t mode)
{
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->modes |= mode;
details->item[item].count++;
}
#endif
static char *find_end(unsigned char *buf, unsigned char *ptr, int ptrlen, int tot, char *end, int endlen, bool first)
{
unsigned char *search;
if (endlen > tot)
return NULL;
// If end is only 1 char - do a faster search
if (endlen == 1) {
if (first)
search = buf;
else
search = ptr;
return strchr((char *)search, *end);
} else {
if (first)
search = buf;
else {
// must allow end to have been chopped in 2
if ((tot - ptrlen) >= (endlen - 1))
search = ptr - (endlen - 1);
else
search = ptr - (tot - ptrlen);
}
return strstr((char *)search, end);
}
}
#define USB_MAX_READ 8192
#define USB_RETRY_MAX 5
static int
usb_bulk_transfer(struct libusb_device_handle *dev_handle,
unsigned char endpoint, unsigned char *data, int length,
int *transferred, unsigned int timeout,
struct cgpu_info *cgpu, enum usb_cmds cmd)
{
uint16_t MaxPacketSize;
int err, errn, tries = 0;
/* Limit length of transfer to the largest this descriptor supports
* and leave the higher level functions to transfer more if needed. */
if (cgpu->usbdev->PrefPacketSize)
MaxPacketSize = cgpu->usbdev->PrefPacketSize;
else
MaxPacketSize = cgpu->usbdev->found->wMaxPacketSize;
if (length > MaxPacketSize)
length = MaxPacketSize;
cg_rlock(&cgusb_fd_lock);
err = libusb_bulk_transfer(dev_handle, endpoint, data, length,
transferred, timeout);
errn = errno;
cg_runlock(&cgusb_fd_lock);
if (err < 0)
applog(LOG_DEBUG, "%s%i: %s (amt=%d err=%d ern=%d)",
cgpu->drv->name, cgpu->device_id,
usb_cmdname(cmd), *transferred, err, errn);
if (err == LIBUSB_ERROR_PIPE) {
cgpu->usbinfo.last_pipe = time(NULL);
cgpu->usbinfo.pipe_count++;
applog(LOG_INFO, "%s%i: libusb pipe error, trying to clear",
cgpu->drv->name, cgpu->device_id);
do {
err = libusb_clear_halt(dev_handle, endpoint);
if (unlikely(err == LIBUSB_ERROR_NOT_FOUND ||
err == LIBUSB_ERROR_NO_DEVICE)) {
cgpu->usbinfo.clear_err_count++;
break;
}
cg_rlock(&cgusb_fd_lock);
err = libusb_bulk_transfer(dev_handle, endpoint, data,
length, transferred, timeout);
errn = errno;
cg_runlock(&cgusb_fd_lock);
if (err < 0)
applog(LOG_DEBUG, "%s%i: %s (amt=%d err=%d ern=%d)",
cgpu->drv->name, cgpu->device_id,
usb_cmdname(cmd), *transferred, err, errn);
if (err)
cgpu->usbinfo.retry_err_count++;
} while (err == LIBUSB_ERROR_PIPE && tries++ < USB_RETRY_MAX);
applog(LOG_DEBUG, "%s%i: libusb pipe error%scleared",
cgpu->drv->name, cgpu->device_id, err ? " not " : " ");
if (err)
cgpu->usbinfo.clear_fail_count++;
}
return err;
}
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;
bool ftdi;
#if DO_USB_STATS
struct timeval tv_start;
#endif
struct timeval read_start, tv_finish;
unsigned int initial_timeout;
double max, done;
int bufleft, err, got, tot, pstate;
bool first = true;
char *search;
int endlen;
// We add 4: 1 for null, 2 for FTDI status and 1 to round to 4 bytes
unsigned char usbbuf[USB_MAX_READ+4], *ptr;
size_t usbbufread;
DEVLOCK(cgpu, pstate);
if (cgpu->usbinfo.nodev) {
*buf = '\0';
*processed = 0;
USB_REJECT(cgpu, MODE_BULK_READ);
err = LIBUSB_ERROR_NO_DEVICE;
goto out_unlock;
}
usbdev = cgpu->usbdev;
ftdi = (usbdev->usb_type == USB_TYPE_FTDI);
USBDEBUG("USB debug: _usb_read(%s (nodev=%s),ep=%d,buf=%p,bufsiz=%zu,proc=%p,timeout=%u,end=%s,cmd=%s,ftdi=%s,readonce=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), ep, buf, bufsiz, processed, timeout, end ? (char *)str_text((char *)end) : "NULL", usb_cmdname(cmd), bool_str(ftdi), bool_str(readonce));
if (bufsiz > USB_MAX_READ)
quit(1, "%s USB read request %d too large (max=%d)", cgpu->drv->name, (int)bufsiz, USB_MAX_READ);
if (timeout == DEVTIMEOUT)
timeout = usbdev->found->timeout;
if (end == NULL) {
if (usbdev->buffer && usbdev->bufamt) {
tot = usbdev->bufamt;
bufleft = bufsiz - tot;
memcpy(usbbuf, usbdev->buffer, tot);
ptr = usbbuf + tot;
usbdev->bufamt = 0;
} else {
tot = 0;
bufleft = bufsiz;
ptr = usbbuf;
}
err = LIBUSB_SUCCESS;
initial_timeout = timeout;
max = ((double)timeout) / 1000.0;
cgtime(&read_start);
while (bufleft > 0) {
// TODO: use (USB_MAX_READ - tot) always?
if (usbdev->buffer)
usbbufread = USB_MAX_READ - tot;
else {
if (ftdi)
usbbufread = bufleft + 2;
else
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, usbbufread, &got, timeout,
cgpu, cmd);
cgtime(&tv_finish);
USB_STATS(cgpu, &tv_start, &tv_finish, err,
MODE_BULK_READ, cmd, first ? SEQ0 : SEQ1);
ptr[got] = '\0';
USBDEBUG("USB debug: @_usb_read(%s (nodev=%s)) first=%s err=%d%s got=%d ptr='%s' usbbufread=%zu", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), bool_str(first), err, isnodev(err), got, (char *)str_text((char *)ptr), usbbufread);
IOERR_CHECK(cgpu, err);
if (ftdi) {
// first 2 bytes returned are an FTDI status
if (got > 2) {
got -= 2;
memmove(ptr, ptr+2, got+1);
} else {
got = 0;
*ptr = '\0';
}
}
tot += got;
if (err || readonce)
break;
ptr += got;
bufleft -= 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);
if (!timeout)
break;
}
// N.B. usbdev->buffer was emptied before the while() loop
if (usbdev->buffer && tot > (int)bufsiz) {
usbdev->bufamt = tot - bufsiz;
memcpy(usbdev->buffer, ptr + bufsiz, usbdev->bufamt);
tot -= usbdev->bufamt;
usbbuf[tot] = '\0';
applog(LOG_ERR, "USB: %s%i read1 buffering %d extra bytes",
cgpu->drv->name, cgpu->device_id, usbdev->bufamt);
}
*processed = tot;
memcpy((char *)buf, (const char *)usbbuf, (tot < (int)bufsiz) ? tot + 1 : (int)bufsiz);
if (NODEV(err))
release_cgpu(cgpu);
goto out_unlock;
}
if (usbdev->buffer && usbdev->bufamt) {
tot = usbdev->bufamt;
bufleft = bufsiz - tot;
memcpy(usbbuf, usbdev->buffer, tot);
ptr = usbbuf + tot;
usbdev->bufamt = 0;
} else {
tot = 0;
bufleft = bufsiz;
ptr = usbbuf;
}
endlen = strlen(end);
err = LIBUSB_SUCCESS;
initial_timeout = timeout;
max = ((double)timeout) / 1000.0;
cgtime(&read_start);
while (bufleft > 0) {
// TODO: use (USB_MAX_READ - tot) always?
if (usbdev->buffer)
usbbufread = USB_MAX_READ - tot;
else {
if (ftdi)
usbbufread = bufleft + 2;
else
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,
usbbufread, &got, timeout,
cgpu, cmd);
cgtime(&tv_finish);
USB_STATS(cgpu, &tv_start, &tv_finish, err,
MODE_BULK_READ, cmd, first ? SEQ0 : SEQ1);
ptr[got] = '\0';
USBDEBUG("USB debug: @_usb_read(%s (nodev=%s)) first=%s err=%d%s got=%d ptr='%s' usbbufread=%zu", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), bool_str(first), err, isnodev(err), got, (char *)str_text((char *)ptr), usbbufread);
IOERR_CHECK(cgpu, err);
if (ftdi) {
// first 2 bytes returned are an FTDI status
if (got > 2) {
got -= 2;
memmove(ptr, ptr+2, got+1);
} else {
got = 0;
*ptr = '\0';
}
}
tot += got;
if (err || readonce)
break;
if (find_end(usbbuf, ptr, got, tot, (char *)end, endlen, first))
break;
ptr += got;
bufleft -= 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);
if (!timeout)
break;
}
if (usbdev->buffer) {
bool dobuffer = false;
if ((search = find_end(usbbuf, usbbuf, tot, tot, (char *)end, endlen, true))) {
// end finishes after bufsiz
if ((search + endlen - (char *)usbbuf) > (int)bufsiz) {
usbdev->bufamt = tot - bufsiz;
dobuffer = true;
} else {
// extra data after end
if (*(search + endlen)) {
usbdev->bufamt = tot - (search + endlen - (char *)usbbuf);
dobuffer = true;
}
}
} else {
// no end, but still bigger than bufsiz
if (tot > (int)bufsiz) {
usbdev->bufamt = tot - bufsiz;
dobuffer = true;
}
}
if (dobuffer) {
tot -= usbdev->bufamt;
memcpy(usbdev->buffer, usbbuf + tot, usbdev->bufamt);
usbbuf[tot] = '\0';
applog(LOG_ERR, "USB: %s%i read2 buffering %d extra bytes",
cgpu->drv->name, cgpu->device_id, usbdev->bufamt);
}
}
*processed = tot;
memcpy((char *)buf, (const char *)usbbuf, (tot < (int)bufsiz) ? tot + 1 : (int)bufsiz);
if (NODEV(err))
release_cgpu(cgpu);
out_unlock:
DEVUNLOCK(cgpu, pstate);
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;
#if DO_USB_STATS
struct timeval tv_start;
#endif
struct timeval read_start, tv_finish;
unsigned int initial_timeout;
double max, done;
__maybe_unused bool first = true;
int err, sent, tot, pstate;
DEVLOCK(cgpu, pstate);
USBDEBUG("USB debug: _usb_write(%s (nodev=%s),ep=%d,buf='%s',bufsiz=%zu,proc=%p,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), ep, (char *)str_text(buf), bufsiz, processed, timeout, usb_cmdname(cmd));
*processed = 0;
if (cgpu->usbinfo.nodev) {
USB_REJECT(cgpu, MODE_BULK_WRITE);
err = LIBUSB_ERROR_NO_DEVICE;
goto out_unlock;
}
usbdev = cgpu->usbdev;
if (timeout == DEVTIMEOUT)
timeout = usbdev->found->timeout;
tot = 0;
err = LIBUSB_SUCCESS;
initial_timeout = timeout;
max = ((double)timeout) / 1000.0;
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,
(unsigned char *)buf, bufsiz, &sent,
timeout, cgpu, cmd);
cgtime(&tv_finish);
USB_STATS(cgpu, &tv_start, &tv_finish, err,
MODE_BULK_WRITE, cmd, first ? SEQ0 : SEQ1);
USBDEBUG("USB debug: @_usb_write(%s (nodev=%s)) err=%d%s sent=%d", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), err, isnodev(err), sent);
IOERR_CHECK(cgpu, err);
tot += sent;
if (err)
break;
buf += sent;
bufsiz -= sent;
first = false;
done = tdiff(&tv_finish, &read_start);
// N.B. this is: return LIBUSB_SUCCESS with whatever size was written
if (unlikely(done >= max))
break;
timeout = initial_timeout - (done * 1000);
if (!timeout)
break;
}
*processed = tot;
if (NODEV(err))
release_cgpu(cgpu);
out_unlock:
DEVUNLOCK(cgpu, pstate);
return err;
}
int __usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint32_t *data, int siz, unsigned int timeout, __maybe_unused enum usb_cmds cmd)
{
struct cg_usb_device *usbdev;
#if DO_USB_STATS
struct timeval tv_start, tv_finish;
#endif
uint32_t *buf = NULL;
int err, i, bufsiz;
USBDEBUG("USB debug: _usb_transfer(%s (nodev=%s),type=%"PRIu8",req=%"PRIu8",value=%"PRIu16",index=%"PRIu16",siz=%d,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), request_type, bRequest, wValue, wIndex, siz, timeout, usb_cmdname(cmd));
if (cgpu->usbinfo.nodev) {
USB_REJECT(cgpu, MODE_CTRL_WRITE);
err = LIBUSB_ERROR_NO_DEVICE;
goto out_;
}
usbdev = cgpu->usbdev;
USBDEBUG("USB debug: @_usb_transfer() data=%s", bin2hex((unsigned char *)data, (size_t)siz));
if (siz > 0) {
bufsiz = siz - 1;
bufsiz >>= 2;
bufsiz++;
buf = malloc(bufsiz << 2);
if (unlikely(!buf))
quit(1, "Failed to malloc in _usb_transfer");
for (i = 0; i < bufsiz; i++)
buf[i] = htole32(data[i]);
}
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,
bRequest, wValue, wIndex, (unsigned char *)buf, (uint16_t)siz,
timeout == DEVTIMEOUT ? usbdev->found->timeout : timeout);
cg_runlock(&cgusb_fd_lock);
STATS_TIMEVAL(&tv_finish);
USB_STATS(cgpu, &tv_start, &tv_finish, err, MODE_CTRL_WRITE, cmd, SEQ0);
USBDEBUG("USB debug: @_usb_transfer(%s (nodev=%s)) err=%d%s", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), err, isnodev(err));
IOERR_CHECK(cgpu, err);
free(buf);
if (NOCONTROLDEV(err))
release_cgpu(cgpu);
out_:
return err;
}
int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint32_t *data, int siz, unsigned int timeout, enum usb_cmds cmd)
{
int pstate, err;
DEVLOCK(cgpu, pstate);
err = __usb_transfer(cgpu, request_type, bRequest, wValue, wIndex, data, siz, timeout, cmd);
DEVUNLOCK(cgpu, pstate);
return err;
}
int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, char *buf, int bufsiz, int *amount, unsigned int timeout, __maybe_unused enum usb_cmds cmd)
{
struct cg_usb_device *usbdev;
#if DO_USB_STATS
struct timeval tv_start, tv_finish;
#endif
int err, pstate;
DEVLOCK(cgpu, pstate);
USBDEBUG("USB debug: _usb_transfer_read(%s (nodev=%s),type=%"PRIu8",req=%"PRIu8",value=%"PRIu16",index=%"PRIu16",bufsiz=%d,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), request_type, bRequest, wValue, wIndex, bufsiz, timeout, usb_cmdname(cmd));
if (cgpu->usbinfo.nodev) {
USB_REJECT(cgpu, MODE_CTRL_READ);
err = LIBUSB_ERROR_NO_DEVICE;
goto out_unlock;
}
usbdev = cgpu->usbdev;
*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,
bRequest, wValue, wIndex,
(unsigned char *)buf, (uint16_t)bufsiz,
timeout == DEVTIMEOUT ? usbdev->found->timeout : timeout);
cg_runlock(&cgusb_fd_lock);
STATS_TIMEVAL(&tv_finish);
USB_STATS(cgpu, &tv_start, &tv_finish, err, MODE_CTRL_READ, cmd, SEQ0);
USBDEBUG("USB debug: @_usb_transfer_read(%s (nodev=%s)) amt/err=%d%s%s%s", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), err, isnodev(err), err > 0 ? " = " : BLANK, err > 0 ? bin2hex((unsigned char *)buf, (size_t)err) : BLANK);
IOERR_CHECK(cgpu, err);
if (err > 0) {
*amount = err;
err = 0;
} else if (NOCONTROLDEV(err))
release_cgpu(cgpu);
out_unlock:
DEVUNLOCK(cgpu, pstate);
return err;
}
#define FTDI_STATUS_B0_MASK (FTDI_RS0_CTS | FTDI_RS0_DSR | FTDI_RS0_RI | FTDI_RS0_RLSD)
#define FTDI_RS0_CTS (1 << 4)
#define FTDI_RS0_DSR (1 << 5)
#define FTDI_RS0_RI (1 << 6)
#define FTDI_RS0_RLSD (1 << 7)
/* Clear to send for FTDI */
int usb_ftdi_cts(struct cgpu_info *cgpu)
{
char buf[2], ret;
int err, amount;
err = _usb_transfer_read(cgpu, (uint8_t)FTDI_TYPE_IN, (uint8_t)5,
(uint16_t)0, (uint16_t)0, buf, 2,
&amount, DEVTIMEOUT, C_FTDI_STATUS);
/* We return true in case drivers are waiting indefinitely to try and
* write to something that's not there. */
if (err)
return true;
ret = buf[0] & FTDI_STATUS_B0_MASK;
return (ret & FTDI_RS0_CTS);
}
int usb_ftdi_set_latency(struct cgpu_info *cgpu)
{
int err = 0;
int pstate;
DEVLOCK(cgpu, pstate);
if (cgpu->usbdev) {
if (cgpu->usbdev->usb_type != USB_TYPE_FTDI) {
applog(LOG_ERR, "%s: cgid %d latency request on non-FTDI device",
cgpu->drv->name, cgpu->cgminer_id);
err = LIBUSB_ERROR_NOT_SUPPORTED;
} else if (cgpu->usbdev->found->latency == LATENCY_UNUSED) {
applog(LOG_ERR, "%s: cgid %d invalid latency (UNUSED)",
cgpu->drv->name, cgpu->cgminer_id);
err = LIBUSB_ERROR_NOT_SUPPORTED;
}
if (!err)
err = __usb_transfer(cgpu, FTDI_TYPE_OUT, FTDI_REQUEST_LATENCY,
cgpu->usbdev->found->latency,
cgpu->usbdev->found->interface,
NULL, 0, DEVTIMEOUT, C_LATENCY);
}
DEVUNLOCK(cgpu, pstate);
applog(LOG_DEBUG, "%s: cgid %d %s got err %d",
cgpu->drv->name, cgpu->cgminer_id,
usb_cmdname(C_LATENCY), err);
return err;
}
void usb_buffer_enable(struct cgpu_info *cgpu)
{
struct cg_usb_device *cgusb;
int pstate;
DEVLOCK(cgpu, pstate);
cgusb = cgpu->usbdev;
if (cgusb && !cgusb->buffer) {
cgusb->bufamt = 0;
cgusb->buffer = malloc(USB_MAX_READ+1);
if (!cgusb->buffer)
quit(1, "Failed to malloc buffer for USB %s%i",
cgpu->drv->name, cgpu->device_id);
cgusb->bufsiz = USB_MAX_READ;
}
DEVUNLOCK(cgpu, pstate);
}
void usb_buffer_disable(struct cgpu_info *cgpu)
{
struct cg_usb_device *cgusb;
int pstate;
DEVLOCK(cgpu, pstate);
cgusb = cgpu->usbdev;
if (cgusb && cgusb->buffer) {
cgusb->bufamt = 0;
cgusb->bufsiz = 0;
free(cgusb->buffer);
cgusb->buffer = NULL;
}
DEVUNLOCK(cgpu, pstate);
}
void usb_buffer_clear(struct cgpu_info *cgpu)
{
int pstate;
DEVLOCK(cgpu, pstate);
if (cgpu->usbdev)
cgpu->usbdev->bufamt = 0;
DEVUNLOCK(cgpu, pstate);
}
uint32_t usb_buffer_size(struct cgpu_info *cgpu)
{
uint32_t ret = 0;
int pstate;
DEVLOCK(cgpu, pstate);
if (cgpu->usbdev)
ret = cgpu->usbdev->bufamt;
DEVUNLOCK(cgpu, pstate);
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)
{
struct cg_usb_device *cgusb;
struct cgpu_info *cgpu2;
struct timeval now;
int pstate;
DEVLOCK(cgpu, pstate);
cgusb = cgpu->usbdev;
// If the device wasn't dropped
if (cgusb != NULL) {
int i;
cgtime(&now);
for (i = 0; i < total_devices; i++) {
cgpu2 = get_devices(i);
if (cgpu2->usbdev == cgusb)
copy_time(&(cgpu2->dev_start_tv), &now);
}
}
DEVUNLOCK(cgpu, pstate);
}
void usb_cleanup()
{
struct cgpu_info *cgpu;
int count;
int i;
hotplug_time = 0;
nmsleep(10);
count = 0;
for (i = 0; i < total_devices; i++) {
cgpu = devices[i];
switch (cgpu->drv->drv_id) {
case DRIVER_BFLSC:
case DRIVER_BITFORCE:
case DRIVER_MODMINER:
case DRIVER_ICARUS:
case DRIVER_AVALON:
wr_lock(cgpu->usbinfo.devlock);
release_cgpu(cgpu);
wr_unlock(cgpu->usbinfo.devlock);
count++;
break;
default:
break;
}
}
/*
* Must attempt to wait for the resource thread to release coz
* during a restart it won't automatically release them in linux
*/
if (count) {
struct timeval start, now;
cgtime(&start);
while (42) {
nmsleep(50);
mutex_lock(&cgusbres_lock);
if (!res_work_head)
break;
cgtime(&now);
if (tdiff(&now, &start) > 0.366) {
applog(LOG_WARNING,
"usb_cleanup gave up waiting for resource thread");
break;
}
mutex_unlock(&cgusbres_lock);
}
mutex_unlock(&cgusbres_lock);
}
cgsem_destroy(&usb_resource_sem);
}
void usb_initialise()
{
char *fre, *ptr, *comma, *colon;
int bus, dev, lim, i;
bool found;
for (i = 0; i < DRIVER_MAX; i++) {
drv_count[i].count = 0;
drv_count[i].limit = 999999;
}
cgusb_check_init();
if (opt_usb_select && *opt_usb_select) {
// Absolute device limit
if (*opt_usb_select == ':') {
total_limit = atoi(opt_usb_select+1);
if (total_limit < 0)
quit(1, "Invalid --usb total limit");
// Comma list of bus:dev devices to match
} else if (isdigit(*opt_usb_select)) {
fre = ptr = strdup(opt_usb_select);
do {
comma = strchr(ptr, ',');
if (comma)
*(comma++) = '\0';
colon = strchr(ptr, ':');
if (!colon)
quit(1, "Invalid --usb bus:dev missing ':'");
*(colon++) = '\0';
if (!isdigit(*ptr))
quit(1, "Invalid --usb bus:dev - bus must be a number");
if (!isdigit(*colon) && *colon != '*')
quit(1, "Invalid --usb bus:dev - dev must be a number or '*'");
bus = atoi(ptr);
if (bus <= 0)
quit(1, "Invalid --usb bus:dev - bus must be > 0");
if (*colon == '*')
dev = -1;
else {
dev = atoi(colon);
if (dev <= 0)
quit(1, "Invalid --usb bus:dev - dev must be > 0 or '*'");
}
busdev = realloc(busdev, sizeof(*busdev) * (++busdev_count));
if (unlikely(!busdev))
quit(1, "USB failed to realloc busdev");
busdev[busdev_count-1].bus_number = bus;
busdev[busdev_count-1].device_address = dev;
ptr = comma;
} while (ptr);
free(fre);
// Comma list of DRV:limit
} else {
fre = ptr = strdup(opt_usb_select);
do {
comma = strchr(ptr, ',');
if (comma)
*(comma++) = '\0';
colon = strchr(ptr, ':');
if (!colon)
quit(1, "Invalid --usb DRV:limit missing ':'");
*(colon++) = '\0';
if (!isdigit(*colon))
quit(1, "Invalid --usb DRV:limit - limit must be a number");
lim = atoi(colon);
if (lim < 0)
quit(1, "Invalid --usb DRV:limit - limit must be >= 0");
found = false;
#ifdef USE_BFLSC
if (strcasecmp(ptr, bflsc_drv.name) == 0) {
drv_count[bflsc_drv.drv_id].limit = lim;
found = true;
}
#endif
#ifdef USE_BITFORCE
if (!found && strcasecmp(ptr, bitforce_drv.name) == 0) {
drv_count[bitforce_drv.drv_id].limit = lim;
found = true;
}
#endif
#ifdef USE_MODMINER
if (!found && strcasecmp(ptr, modminer_drv.name) == 0) {
drv_count[modminer_drv.drv_id].limit = lim;
found = true;
}
#endif
#ifdef USE_ICARUS
if (!found && strcasecmp(ptr, icarus_drv.name) == 0) {
drv_count[icarus_drv.drv_id].limit = lim;
found = true;
}
#endif
#ifdef USE_AVALON
if (!found && strcasecmp(ptr, avalon_drv.name) == 0) {
drv_count[avalon_drv.drv_id].limit = lim;
found = true;
}
#endif
if (!found)
quit(1, "Invalid --usb DRV:limit - unknown DRV='%s'", ptr);
ptr = comma;
} while (ptr);
free(fre);
}
}
}
#ifndef WIN32
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifndef __APPLE__
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
struct seminfo *__buf;
};
#endif
#else
static LPSECURITY_ATTRIBUTES unsec(LPSECURITY_ATTRIBUTES sec)
{
FreeSid(((PSECURITY_DESCRIPTOR)(sec->lpSecurityDescriptor))->Group);
free(sec->lpSecurityDescriptor);
free(sec);
return NULL;
}
static LPSECURITY_ATTRIBUTES mksec(const char *dname, uint8_t bus_number, uint8_t device_address)
{
SID_IDENTIFIER_AUTHORITY SIDAuthWorld = {SECURITY_WORLD_SID_AUTHORITY};
PSID gsid = NULL;
LPSECURITY_ATTRIBUTES sec_att = NULL;
PSECURITY_DESCRIPTOR sec_des = NULL;
sec_des = malloc(sizeof(*sec_des));
if (unlikely(!sec_des))
quit(1, "MTX: Failed to malloc LPSECURITY_DESCRIPTOR");
if (!InitializeSecurityDescriptor(sec_des, SECURITY_DESCRIPTOR_REVISION)) {
applog(LOG_ERR,
"MTX: %s (%d:%d) USB failed to init secdes err (%d)",
dname, (int)bus_number, (int)device_address,
(int)GetLastError());
free(sec_des);
return NULL;
}
if (!SetSecurityDescriptorDacl(sec_des, TRUE, NULL, FALSE)) {
applog(LOG_ERR,
"MTX: %s (%d:%d) USB failed to secdes dacl err (%d)",
dname, (int)bus_number, (int)device_address,
(int)GetLastError());
free(sec_des);
return NULL;
}
if(!AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &gsid)) {
applog(LOG_ERR,
"MTX: %s (%d:%d) USB failed to create gsid err (%d)",
dname, (int)bus_number, (int)device_address,
(int)GetLastError());
free(sec_des);
return NULL;
}
if (!SetSecurityDescriptorGroup(sec_des, gsid, FALSE)) {
applog(LOG_ERR,
"MTX: %s (%d:%d) USB failed to secdes grp err (%d)",
dname, (int)bus_number, (int)device_address,
(int)GetLastError());
FreeSid(gsid);
free(sec_des);
return NULL;
}
sec_att = malloc(sizeof(*sec_att));
if (unlikely(!sec_att))
quit(1, "MTX: Failed to malloc LPSECURITY_ATTRIBUTES");
sec_att->nLength = sizeof(*sec_att);
sec_att->lpSecurityDescriptor = sec_des;
sec_att->bInheritHandle = FALSE;
return sec_att;
}
#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 resource_lock(const char *dname, uint8_t bus_number, uint8_t device_address)
{
applog(LOG_DEBUG, "USB res lock %s %d-%d", dname, (int)bus_number, (int)device_address);
#ifdef WIN32
struct cgpu_info *cgpu;
LPSECURITY_ATTRIBUTES sec;
HANDLE usbMutex;
char name[64];
DWORD res;
int i;
if (is_in_use_bd(bus_number, device_address))
return false;
sprintf(name, "cg-usb-%d-%d", (int)bus_number, (int)device_address);
sec = mksec(dname, bus_number, device_address);
if (!sec)
return false;
usbMutex = CreateMutex(sec, FALSE, name);
if (usbMutex == NULL) {
applog(LOG_ERR,
"MTX: %s USB failed to get '%s' err (%d)",
dname, name, (int)GetLastError());
sec = unsec(sec);
return false;
}
res = WaitForSingleObject(usbMutex, 0);
switch(res) {
case WAIT_OBJECT_0:
case WAIT_ABANDONED:
// Am I using it already?
for (i = 0; i < total_devices; i++) {
cgpu = get_devices(i);
if (cgpu->usbinfo.bus_number == bus_number &&
cgpu->usbinfo.device_address == device_address &&
cgpu->usbinfo.nodev == false) {
if (ReleaseMutex(usbMutex)) {
applog(LOG_WARNING,
"MTX: %s USB can't get '%s' - device in use",
dname, name);
goto fail;
}
applog(LOG_ERR,
"MTX: %s USB can't get '%s' - device in use - failure (%d)",
dname, name, (int)GetLastError());
goto fail;
}
}
break;
case WAIT_TIMEOUT:
if (!hotplug_mode)
applog(LOG_WARNING,
"MTX: %s USB failed to get '%s' - device in use",
dname, name);
goto fail;
case WAIT_FAILED:
applog(LOG_ERR,
"MTX: %s USB failed to get '%s' err (%d)",
dname, name, (int)GetLastError());
goto fail;
default:
applog(LOG_ERR,
"MTX: %s USB failed to get '%s' unknown reply (%d)",
dname, name, (int)res);
goto fail;
}
add_in_use(bus_number, device_address);
in_use_store_ress(bus_number, device_address, (void *)usbMutex, (void *)sec);
return true;
fail:
CloseHandle(usbMutex);
sec = unsec(sec);
return false;
#else
struct semid_ds seminfo;
union semun opt;
char name[64];
key_t *key;
int *sem;
int fd, count;
if (is_in_use_bd(bus_number, device_address))
return false;
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",
dname, name, errno, strerror(errno));
goto _out;
}
close(fd);
key = malloc(sizeof(*key));
if (unlikely(!key))
quit(1, "SEM: Failed to malloc key");
sem = malloc(sizeof(*sem));
if (unlikely(!sem))
quit(1, "SEM: Failed to malloc sem");
*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",
dname, name, errno, strerror(errno));
goto free_out;
}
*sem = semget(*key, 1, 0);
if (*sem < 0) {
applog(LOG_ERR,
"SEM: %s USB failed to access '%s' err (%d) %s",
dname, name, errno, strerror(errno));
goto free_out;
}
opt.buf = &seminfo;
count = 0;
while (++count) {
// Should NEVER take 100ms
if (count > 99) {
applog(LOG_ERR,
"SEM: %s USB timeout waiting for (%d) '%s'",
dname, *sem, name);
goto free_out;
}
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",
dname, *sem, name, count, errno, strerror(errno));
goto free_out;
}
if (opt.buf->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) {
if (!hotplug_mode)
applog(LOG_WARNING,
"SEM: %s USB failed to get (%d) '%s' - device in use",
dname, *sem, name);
} else {
applog(LOG_DEBUG,
"SEM: %s USB failed to get (%d) '%s' err (%d) %s",
dname, *sem, name, errno, strerror(errno));
}
goto free_out;
}
add_in_use(bus_number, device_address);
in_use_store_ress(bus_number, device_address, (void *)key, (void *)sem);
return true;
free_out:
free(sem);
free(key);
_out:
return false;
#endif
}
// Any errors should always be printed since they will rarely if ever occur
// and thus it is best to always display them
static void resource_unlock(const char *dname, uint8_t bus_number, uint8_t device_address)
{
applog(LOG_DEBUG, "USB res unlock %s %d-%d", dname, (int)bus_number, (int)device_address);
#ifdef WIN32
LPSECURITY_ATTRIBUTES sec = NULL;
HANDLE usbMutex = NULL;
char name[64];
sprintf(name, "cg-usb-%d-%d", (int)bus_number, (int)device_address);
in_use_get_ress(bus_number, device_address, (void **)(&usbMutex), (void **)(&sec));
if (!usbMutex || !sec)
goto fila;
if (!ReleaseMutex(usbMutex))
applog(LOG_ERR,
"MTX: %s USB failed to release '%s' err (%d)",
dname, name, (int)GetLastError());
fila:
if (usbMutex)
CloseHandle(usbMutex);
if (sec)
unsec(sec);
remove_in_use(bus_number, device_address);
return;
#else
char name[64];
key_t *key = NULL;
int *sem = NULL;
sprintf(name, "/tmp/cgminer-usb-%d-%d", (int)bus_number, (int)device_address);
in_use_get_ress(bus_number, device_address, (void **)(&key), (void **)(&sem));
if (!key || !sem)
goto fila;
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 };
if (semtimedop(*sem, sops, 1, &timeout)) {
applog(LOG_ERR,
"SEM: %s USB failed to release '%s' err (%d) %s",
dname, name, errno, strerror(errno));
}
if (semctl(*sem, 0, IPC_RMID)) {
applog(LOG_WARNING,
"SEM: %s USB failed to remove SEM '%s' err (%d) %s",
dname, name, errno, strerror(errno));
}
fila:
free(sem);
free(key);
remove_in_use(bus_number, device_address);
return;
#endif
}
static void resource_process()
{
struct resource_work *res_work = NULL;
struct resource_reply *res_reply = NULL;
bool ok;
applog(LOG_DEBUG, "RES: %s (%d:%d) lock=%d",
res_work_head->dname,
(int)res_work_head->bus_number,
(int)res_work_head->device_address,
res_work_head->lock);
if (res_work_head->lock) {
ok = resource_lock(res_work_head->dname,
res_work_head->bus_number,
res_work_head->device_address);
applog(LOG_DEBUG, "RES: %s (%d:%d) lock ok=%d",
res_work_head->dname,
(int)res_work_head->bus_number,
(int)res_work_head->device_address,
ok);
res_reply = calloc(1, sizeof(*res_reply));
if (unlikely(!res_reply))
quit(1, "USB failed to calloc res_reply");
res_reply->bus_number = res_work_head->bus_number;
res_reply->device_address = res_work_head->device_address;
res_reply->got = ok;
res_reply->next = res_reply_head;
res_reply_head = res_reply;
}
else
resource_unlock(res_work_head->dname,
res_work_head->bus_number,
res_work_head->device_address);
res_work = res_work_head;
res_work_head = res_work_head->next;
free(res_work);
}
void *usb_resource_thread(void __maybe_unused *userdata)
{
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
RenameThread("usbresource");
applog(LOG_DEBUG, "RES: thread starting");
while (42) {
/* Wait to be told we have work to do */
cgsem_wait(&usb_resource_sem);
mutex_lock(&cgusbres_lock);
while (res_work_head)
resource_process();
mutex_unlock(&cgusbres_lock);
}
return NULL;
}