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 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634
|
/*
* TE410P Quad-T1/E1 PCI Driver version 0.1, 12/16/02
*
* Written by Mark Spencer <markster@digium.com>
* Based on previous works, designs, and archetectures conceived and
* written by Jim Dixon <jim@lambdatel.com>.
* Further modified, optimized, and maintained by
* Matthew Fredrickson <creslin@digium.com> and
* Russ Meyerriecks <rmeyerriecks@digium.com>
*
* Copyright (C) 2001 Jim Dixon / Zapata Telephony.
* Copyright (C) 2001-2012, Digium, Inc.
*
* All rights reserved.
*
*/
/*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2 as published by the
* Free Software Foundation. See the LICENSE file included with
* this program for more details.
*/
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/spinlock.h>
#include <asm/io.h>
#include <linux/version.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/crc32.h>
#include <linux/slab.h>
#include <stdbool.h>
#include <dahdi/kernel.h>
#include "wct4xxp.h"
#include "vpm450m.h"
/* Work queues are a way to better distribute load on SMP systems */
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20))
/*
* Work queues can significantly improve performance and scalability
* on multi-processor machines, but requires bypassing some kernel
* API's, so it's not guaranteed to be compatible with all kernels.
*/
/* #define ENABLE_WORKQUEUES */
#endif
/* Support first generation cards? */
#define SUPPORT_GEN1
/* Define to get more attention-grabbing but slightly more I/O using
alarm status */
#undef FANCY_ALARM
/* Define to support Digium Voice Processing Module expansion card */
#define VPM_SUPPORT
#define DEBUG_MAIN (1 << 0)
#define DEBUG_DTMF (1 << 1)
#define DEBUG_REGS (1 << 2)
#define DEBUG_TSI (1 << 3)
#define DEBUG_ECHOCAN (1 << 4)
#define DEBUG_RBS (1 << 5)
#define DEBUG_FRAMER (1 << 6)
/* Maximum latency to be used with Gen 5 */
#define GEN5_MAX_LATENCY 127
#ifdef ENABLE_WORKQUEUES
#include <linux/cpu.h>
/* XXX UGLY!!!! XXX We have to access the direct structures of the workqueue which
are only defined within workqueue.c because they don't give us a routine to allow us
to nail a work to a particular thread of the CPU. Nailing to threads gives us substantially
higher scalability in multi-CPU environments though! */
/*
* The per-CPU workqueue (if single thread, we always use cpu 0's).
*
* The sequence counters are for flush_scheduled_work(). It wants to wait
* until until all currently-scheduled works are completed, but it doesn't
* want to be livelocked by new, incoming ones. So it waits until
* remove_sequence is >= the insert_sequence which pertained when
* flush_scheduled_work() was called.
*/
struct cpu_workqueue_struct {
spinlock_t lock;
long remove_sequence; /* Least-recently added (next to run) */
long insert_sequence; /* Next to add */
struct list_head worklist;
wait_queue_head_t more_work;
wait_queue_head_t work_done;
struct workqueue_struct *wq;
task_t *thread;
int run_depth; /* Detect run_workqueue() recursion depth */
} ____cacheline_aligned;
/*
* The externally visible workqueue abstraction is an array of
* per-CPU workqueues:
*/
struct workqueue_struct {
/* TODO: Find out exactly where the API changed */
struct cpu_workqueue_struct *cpu_wq;
const char *name;
struct list_head list; /* Empty if single thread */
};
/* Preempt must be disabled. */
static void __t4_queue_work(struct cpu_workqueue_struct *cwq,
struct work_struct *work)
{
unsigned long flags;
spin_lock_irqsave(&cwq->lock, flags);
work->wq_data = cwq;
list_add_tail(&work->entry, &cwq->worklist);
cwq->insert_sequence++;
wake_up(&cwq->more_work);
spin_unlock_irqrestore(&cwq->lock, flags);
}
/*
* Queue work on a workqueue. Return non-zero if it was successfully
* added.
*
* We queue the work to the CPU it was submitted, but there is no
* guarantee that it will be processed by that CPU.
*/
static inline int t4_queue_work(struct workqueue_struct *wq, struct work_struct *work, int cpu)
{
int ret = 0;
get_cpu();
if (!test_and_set_bit(0, &work->pending)) {
BUG_ON(!list_empty(&work->entry));
__t4_queue_work(wq->cpu_wq + cpu, work);
ret = 1;
}
put_cpu();
return ret;
}
#endif
/*
* Define CONFIG_FORCE_EXTENDED_RESET to allow the qfalc framer extra time
* to reset itself upon hardware initialization. This exits for rare
* cases for customers who are seeing the qfalc returning unexpected
* information at initialization
*/
/* #define CONFIG_FORCE_EXTENDED_RESET */
/* #define CONFIG_NOEXTENDED_RESET */
/*
* Uncomment the following definition in order to disable Active-State Power
* Management on the PCIe bridge for PCIe cards. This has been known to work
* around issues where the BIOS enables it on the cards even though the
* platform does not support it.
*
*/
/* #define CONFIG_WCT4XXP_DISABLE_ASPM */
#if defined(CONFIG_FORCE_EXTENDED_RESET) && defined(CONFIG_NOEXTENDED_RESET)
#error "You cannot define both CONFIG_FORCE_EXTENDED_RESET and " \
"CONFIG_NOEXTENDED_RESET."
#endif
int debug = 0;
static int timingcable = 0;
static int t1e1override = -1; /* deprecated */
static char *default_linemode = "auto";
static int j1mode = 0;
static int sigmode = FRMR_MODE_NO_ADDR_CMP;
static int alarmdebounce = 2500; /* LOF/LFA def to 2.5s AT&T TR54016*/
static int losalarmdebounce = 2500;/* LOS def to 2.5s AT&T TR54016*/
static int aisalarmdebounce = 2500;/* AIS(blue) def to 2.5s AT&T TR54016*/
static int yelalarmdebounce = 500;/* RAI(yellow) def to 0.5s AT&T devguide */
static int max_latency = GEN5_MAX_LATENCY; /* Used to set a maximum latency (if you don't wish it to hard cap it at a certain value) in milliseconds */
#ifdef VPM_SUPPORT
static int vpmsupport = 1;
/* If set to auto, vpmdtmfsupport is enabled for VPM400M and disabled for VPM450M */
static int vpmdtmfsupport = -1; /* -1=auto, 0=disabled, 1=enabled*/
#endif /* VPM_SUPPORT */
/* Enabling bursting can more efficiently utilize PCI bus bandwidth, but
can also cause PCI bus starvation, especially in combination with other
aggressive cards. Please note that burst mode has no effect on CPU
utilization / max number of calls / etc. */
static int noburst;
/* For 56kbps links, set this module parameter to 0x7f */
static int hardhdlcmode = 0xff;
static int latency = 1;
static int ms_per_irq = 1;
static int ignore_rotary;
#ifdef FANCY_ALARM
static int altab[] = {
0, 0, 0, 1, 2, 3, 4, 6, 8, 9, 11, 13, 16, 18, 20, 22, 24, 25, 27, 28, 29, 30, 31, 31, 32, 31, 31, 30, 29, 28, 27, 25, 23, 22, 20, 18, 16, 13, 11, 9, 8, 6, 4, 3, 2, 1, 0, 0,
};
#endif
#define MAX_SPANS 16
#define FLAG_STARTED (1 << 0)
#define FLAG_NMF (1 << 1)
#define FLAG_SENDINGYELLOW (1 << 2)
#define FLAG_2NDGEN (1 << 3)
#define FLAG_2PORT (1 << 4)
#define FLAG_VPM2GEN (1 << 5)
#define FLAG_OCTOPT (1 << 6)
#define FLAG_3RDGEN (1 << 7)
#define FLAG_BURST (1 << 8)
#define FLAG_EXPRESS (1 << 9)
#define FLAG_5THGEN (1 << 10)
#define FLAG_8PORT (1 << 11)
#define CANARY 0xc0de
/* names of available HWEC modules */
#ifdef VPM_SUPPORT
#define T4_VPM_PRESENT (1 << 28)
static const char *vpmoct064_name = "VPMOCT064";
static const char *vpmoct128_name = "VPMOCT128";
static const char *vpmoct256_name = "VPMOCT256";
#endif
struct devtype {
char *desc;
unsigned int flags;
};
static struct devtype wct820p5 = { "Wildcard TE820 (5th Gen)", FLAG_8PORT | FLAG_5THGEN | FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN | FLAG_EXPRESS };
static struct devtype wct420p5 = { "Wildcard TE420 (5th Gen)", FLAG_5THGEN | FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN | FLAG_EXPRESS };
static struct devtype wct410p5 = { "Wildcard TE410P (5th Gen)", FLAG_5THGEN | FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN };
static struct devtype wct405p5 = { "Wildcard TE405P (5th Gen)", FLAG_5THGEN | FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN };
static struct devtype wct220p5 = { "Wildcard TE220 (5th Gen)", FLAG_5THGEN | FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN | FLAG_2PORT | FLAG_EXPRESS };
static struct devtype wct210p5 = { "Wildcard TE210P (5th Gen)", FLAG_5THGEN | FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN | FLAG_2PORT };
static struct devtype wct205p5 = { "Wildcard TE205P (5th Gen)", FLAG_5THGEN | FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN | FLAG_2PORT };
static struct devtype wct4xxp = { "Wildcard TE410P/TE405P (1st Gen)", 0 };
static struct devtype wct420p4 = { "Wildcard TE420 (4th Gen)", FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN | FLAG_EXPRESS };
static struct devtype wct410p4 = { "Wildcard TE410P (4th Gen)", FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN };
static struct devtype wct410p3 = { "Wildcard TE410P (3rd Gen)", FLAG_2NDGEN | FLAG_3RDGEN };
static struct devtype wct405p4 = { "Wildcard TE405P (4th Gen)", FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN };
static struct devtype wct405p3 = { "Wildcard TE405P (3rd Gen)", FLAG_2NDGEN | FLAG_3RDGEN };
static struct devtype wct410p2 = { "Wildcard TE410P (2nd Gen)", FLAG_2NDGEN };
static struct devtype wct405p2 = { "Wildcard TE405P (2nd Gen)", FLAG_2NDGEN };
static struct devtype wct220p4 = { "Wildcard TE220 (4th Gen)", FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN | FLAG_2PORT | FLAG_EXPRESS };
static struct devtype wct205p4 = { "Wildcard TE205P (4th Gen)", FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN | FLAG_2PORT };
static struct devtype wct205p3 = { "Wildcard TE205P (3rd Gen)", FLAG_2NDGEN | FLAG_3RDGEN | FLAG_2PORT };
static struct devtype wct210p4 = { "Wildcard TE210P (4th Gen)", FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN | FLAG_2PORT };
static struct devtype wct210p3 = { "Wildcard TE210P (3rd Gen)", FLAG_2NDGEN | FLAG_3RDGEN | FLAG_2PORT };
static struct devtype wct205 = { "Wildcard TE205P ", FLAG_2NDGEN | FLAG_2PORT };
static struct devtype wct210 = { "Wildcard TE210P ", FLAG_2NDGEN | FLAG_2PORT };
struct t4;
enum linemode {T1, E1, J1};
struct spi_state {
int wrreg;
int rdreg;
};
struct t4_span {
struct t4 *owner;
u32 *writechunk; /* Double-word aligned write memory */
u32 *readchunk; /* Double-word aligned read memory */
enum linemode linemode;
int sync;
int alarmtimer;
int notclear;
unsigned long alarm_time;
unsigned long losalarm_time;
unsigned long aisalarm_time;
unsigned long yelalarm_time;
unsigned long alarmcheck_time;
int spanflags;
int syncpos;
#ifdef SUPPORT_GEN1
int e1check; /* E1 check */
#endif
struct dahdi_span span;
unsigned char txsigs[16]; /* Transmit sigs */
int loopupcnt;
int loopdowncnt;
#ifdef SUPPORT_GEN1
unsigned char ec_chunk1[31][DAHDI_CHUNKSIZE]; /* first EC chunk buffer */
unsigned char ec_chunk2[31][DAHDI_CHUNKSIZE]; /* second EC chunk buffer */
#endif
/* HDLC controller fields */
struct dahdi_chan *sigchan;
unsigned char sigmode;
int sigactive;
int frames_out;
int frames_in;
#ifdef VPM_SUPPORT
unsigned long dtmfactive;
unsigned long dtmfmask;
unsigned long dtmfmutemask;
#endif
#ifdef ENABLE_WORKQUEUES
struct work_struct swork;
#endif
struct dahdi_chan *chans[32]; /* Individual channels */
struct dahdi_echocan_state *ec[32]; /* Echocan state for each channel */
};
struct t4 {
/* This structure exists one per card */
struct pci_dev *dev; /* Pointer to PCI device */
unsigned int intcount;
int num; /* Which card we are */
int syncsrc; /* active sync source */
struct dahdi_device *ddev;
struct t4_span *tspans[8]; /* Individual spans */
int numspans; /* Number of spans on the card */
int blinktimer;
#ifdef FANCY_ALARM
int alarmpos;
#endif
int irq; /* IRQ used by device */
int order; /* Order */
const struct devtype *devtype;
unsigned int reset_required:1; /* If reset needed in serial_setup */
unsigned int falc31:1; /* are we falc v3.1 (atomic not necessary) */
unsigned int t1e1:8; /* T1 / E1 select pins */
int ledreg; /* LED Register */
int ledreg2; /* LED Register2 */
unsigned int gpio;
unsigned int gpioctl;
int e1recover; /* E1 recovery timer */
spinlock_t reglock; /* lock register access */
int spansstarted; /* number of spans started */
u32 *writechunk; /* Double-word aligned write memory */
u32 *readchunk; /* Double-word aligned read memory */
#ifdef ENABLE_WORKQUEUES
atomic_t worklist;
struct workqueue_struct *workq;
#endif
int last0; /* for detecting double-missed IRQ */
/* DMA related fields */
unsigned int dmactrl;
dma_addr_t readdma;
dma_addr_t writedma;
void __iomem *membase; /* Base address of card */
#define T4_CHECK_VPM 0
#define T4_LOADING_FW 1
#define T4_STOP_DMA 2
#define T4_CHECK_TIMING 3
#define T4_CHANGE_LATENCY 4
#define T4_IGNORE_LATENCY 5
unsigned long checkflag;
struct work_struct bh_work;
/* Latency related additions */
unsigned char rxident;
unsigned char lastindex;
int numbufs;
int needed_latency;
#ifdef VPM_SUPPORT
struct vpm450m *vpm;
#endif
struct spi_state st;
};
static inline bool is_pcie(const struct t4 *wc)
{
return (wc->devtype->flags & FLAG_EXPRESS) > 0;
}
static inline bool has_e1_span(const struct t4 *wc)
{
return (wc->t1e1 > 0);
}
static inline bool is_octal(const struct t4 *wc)
{
return (wc->devtype->flags & FLAG_8PORT) > 0;
}
static inline int T4_BASE_SIZE(struct t4 *wc)
{
if (is_octal(wc))
return DAHDI_MAX_CHUNKSIZE * 32 * 8;
else
return DAHDI_MAX_CHUNKSIZE * 32 * 4;
}
/**
* ports_on_framer - The number of ports on the framers.
* @wc: Board to check.
*
* The framer ports could be different the the number of ports on the card
* since the dual spans have four ports internally but two ports extenally.
*
*/
static inline unsigned int ports_on_framer(const struct t4 *wc)
{
return (is_octal(wc)) ? 8 : 4;
}
#ifdef VPM_SUPPORT
static void t4_vpm_init(struct t4 *wc);
static void echocan_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec);
static const struct dahdi_echocan_features vpm_ec_features = {
.NLP_automatic = 1,
.CED_tx_detect = 1,
.CED_rx_detect = 1,
};
static const struct dahdi_echocan_ops vpm_ec_ops = {
.echocan_free = echocan_free,
};
#endif
static void __set_clear(struct t4 *wc, int span);
static int _t4_startup(struct file *file, struct dahdi_span *span);
static int t4_startup(struct file *file, struct dahdi_span *span);
static int t4_shutdown(struct dahdi_span *span);
static int t4_rbsbits(struct dahdi_chan *chan, int bits);
static int t4_maint(struct dahdi_span *span, int cmd);
static int t4_clear_maint(struct dahdi_span *span);
static int t4_reset_counters(struct dahdi_span *span);
#ifdef SUPPORT_GEN1
static int t4_reset_dma(struct t4 *wc);
#endif
static void t4_hdlc_hard_xmit(struct dahdi_chan *chan);
static int t4_ioctl(struct dahdi_chan *chan, unsigned int cmd, unsigned long data);
static void t4_tsi_assign(struct t4 *wc, int fromspan, int fromchan, int tospan, int tochan);
static void t4_tsi_unassign(struct t4 *wc, int tospan, int tochan);
static void __t4_set_rclk_src(struct t4 *wc, int span);
static void __t4_set_sclk_src(struct t4 *wc, int mode, int master, int slave);
static void t4_check_alarms(struct t4 *wc, int span);
static void t4_check_sigbits(struct t4 *wc, int span);
#define WC_RDADDR 0
#define WC_WRADDR 1
#define WC_COUNT 2
#define WC_DMACTRL 3
#define WC_INTR 4
/* #define WC_GPIO 5 */
#define WC_VERSION 6
#define WC_LEDS 7
#define WC_GPIOCTL 8
#define WC_GPIO 9
#define WC_LADDR 10
#define WC_LDATA 11
#define WC_LEDS2 12
#define WC_SET_AUTH (1 << 20)
#define WC_GET_AUTH (1 << 12)
#define WC_LFRMR_CS (1 << 10) /* Framer's ChipSelect signal */
#define WC_LCS (1 << 11)
#define WC_LCS2 (1 << 12)
#define WC_LALE (1 << 13)
#define WC_LFRMR_CS2 (1 << 14) /* Framer's ChipSelect signal 2 */
#define WC_LREAD (1 << 15)
#define WC_LWRITE (1 << 16)
#define WC_ACTIVATE (1 << 12)
#define WC_OFF (0)
#define WC_RED (1)
#define WC_GREEN (2)
#define WC_YELLOW (3)
#define WC_RECOVER 0
#define WC_SELF 1
#define LIM0_T 0x36 /* Line interface mode 0 register */
#define LIM0_LL (1 << 1) /* Local Loop */
#define LIM1_T 0x37 /* Line interface mode 1 register */
#define LIM1_RL (1 << 1) /* Remote Loop */
#define FMR0 0x1C /* Framer Mode Register 0 */
#define FMR0_SIM (1 << 0) /* Alarm Simulation */
#define FMR1_T 0x1D /* Framer Mode Register 1 */
#define FMR1_ECM (1 << 2) /* Error Counter 1sec Interrupt Enable */
#define FMR5 0x21 /* Framer Mode Register 5 */
#define FMR5_XLU (1 << 4) /* Transmit loopup code */
#define FMR5_XLD (1 << 5) /* Transmit loopdown code */
#define FMR5_EIBR (1 << 6) /* Internal Bit Robbing Access */
#define DEC_T 0x60 /* Diable Error Counter */
#define IERR_T 0x1B /* Single Bit Defect Insertion Register */
#define IBV (1 << 0) /* Bipolar violation */
#define IPE (1 << 1) /* PRBS defect */
#define ICASE (1 << 2) /* CAS defect */
#define ICRCE (1 << 3) /* CRC defect */
#define IMFE (1 << 4) /* Multiframe defect */
#define IFASE (1 << 5) /* FAS defect */
#define ISR3_SEC (1 << 6) /* Internal one-second interrupt bit mask */
#define ISR3_ES (1 << 7) /* Errored Second interrupt bit mask */
#define ESM 0x47 /* Errored Second mask register */
#define FMR2_T 0x1E /* Framer Mode Register 2 */
#define FMR2_PLB (1 << 2) /* Framer Mode Register 2 */
#define FECL_T 0x50 /* Framing Error Counter Lower Byte */
#define FECH_T 0x51 /* Framing Error Counter Higher Byte */
#define CVCL_T 0x52 /* Code Violation Counter Lower Byte */
#define CVCH_T 0x53 /* Code Violation Counter Higher Byte */
#define CEC1L_T 0x54 /* CRC Error Counter 1 Lower Byte */
#define CEC1H_T 0x55 /* CRC Error Counter 1 Higher Byte */
#define EBCL_T 0x56 /* E-Bit Error Counter Lower Byte */
#define EBCH_T 0x57 /* E-Bit Error Counter Higher Byte */
#define BECL_T 0x58 /* Bit Error Counter Lower Byte */
#define BECH_T 0x59 /* Bit Error Counter Higher Byte */
#define COEC_T 0x5A /* COFA Event Counter */
#define PRBSSTA_T 0xDA /* PRBS Status Register */
#define LCR1_T 0x3B /* Loop Code Register 1 */
#define EPRM (1 << 7) /* Enable PRBS rx */
#define XPRBS (1 << 6) /* Enable PRBS tx */
#define FLLB (1 << 1) /* Framed line loop/Invert */
#define LLBP (1 << 0) /* Line Loopback Pattern */
#define TPC0_T 0xA8 /* Test Pattern Control Register */
#define FRA (1 << 6) /* Framed/Unframed Selection */
#define PRBS23 (3 << 4) /* Pattern selection (23 poly) */
#define PRM (1 << 2) /* Non framed mode */
#define FRS1_T 0x4D /* Framer Receive Status Reg 1 */
#define LLBDD (1 << 4)
#define LLBAD (1 << 3)
#define MAX_T4_CARDS 64
static struct t4 *cards[MAX_T4_CARDS];
struct t8_firm_header {
u8 header[6];
__le32 chksum;
u8 pad[18];
__le32 version;
} __packed;
#define MAX_TDM_CHAN 32
#define MAX_DTMF_DET 16
#define HDLC_IMR0_MASK (FRMR_IMR0_RME | FRMR_IMR0_RPF)
#define HDLC_IMR1_MASK (FRMR_IMR1_XDU | FRMR_IMR1_XPR)
static inline unsigned int __t4_pci_in(struct t4 *wc, const unsigned int addr)
{
unsigned int res = readl(wc->membase + (addr * sizeof(u32)));
return res;
}
static inline void __t4_pci_out(struct t4 *wc, const unsigned int addr, const unsigned int value)
{
#ifdef DEBUG
unsigned int tmp;
#endif
writel(value, wc->membase + (addr * sizeof(u32)));
#ifdef DEBUG
tmp = __t4_pci_in(wc, WC_VERSION);
if ((tmp & 0xffff0000) != 0xc01a0000)
dev_notice(&wc->dev->dev,
"Version Synchronization Error!\n");
#else
__t4_pci_in(wc, WC_VERSION);
#endif
}
static inline void __t4_gpio_set(struct t4 *wc, unsigned bits, unsigned int val)
{
unsigned int newgpio;
newgpio = wc->gpio & (~bits);
newgpio |= val;
if (newgpio != wc->gpio) {
wc->gpio = newgpio;
__t4_pci_out(wc, WC_GPIO, wc->gpio);
}
}
static inline void __t4_gpio_setdir(struct t4 *wc, unsigned int bits, unsigned int val)
{
unsigned int newgpioctl;
newgpioctl = wc->gpioctl & (~bits);
newgpioctl |= val;
if (newgpioctl != wc->gpioctl) {
wc->gpioctl = newgpioctl;
__t4_pci_out(wc, WC_GPIOCTL, wc->gpioctl);
}
}
static inline void t4_gpio_setdir(struct t4 *wc, unsigned int bits, unsigned int val)
{
unsigned long flags;
spin_lock_irqsave(&wc->reglock, flags);
__t4_gpio_setdir(wc, bits, val);
spin_unlock_irqrestore(&wc->reglock, flags);
}
static inline void t4_gpio_set(struct t4 *wc, unsigned int bits, unsigned int val)
{
unsigned long flags;
spin_lock_irqsave(&wc->reglock, flags);
__t4_gpio_set(wc, bits, val);
spin_unlock_irqrestore(&wc->reglock, flags);
}
static inline void t4_pci_out(struct t4 *wc, const unsigned int addr, const unsigned int value)
{
unsigned long flags;
spin_lock_irqsave(&wc->reglock, flags);
__t4_pci_out(wc, addr, value);
spin_unlock_irqrestore(&wc->reglock, flags);
}
static inline void __t4_set_led(struct t4 *wc, int span, int color)
{
if (span <= 3) {
int oldreg = wc->ledreg;
wc->ledreg &= ~(0x3 << (span << 1));
wc->ledreg |= (color << (span << 1));
if (oldreg != wc->ledreg)
__t4_pci_out(wc, WC_LEDS, wc->ledreg);
} else {
int oldreg = wc->ledreg2;
span &= 3;
wc->ledreg2 &= ~(0x3 << (span << 1));
wc->ledreg2 |= (color << (span << 1));
if (oldreg != wc->ledreg2)
__t4_pci_out(wc, WC_LEDS2, wc->ledreg2);
}
}
static inline void t4_activate(struct t4 *wc)
{
wc->ledreg |= WC_ACTIVATE;
t4_pci_out(wc, WC_LEDS, wc->ledreg);
}
static inline unsigned int t4_pci_in(struct t4 *wc, const unsigned int addr)
{
unsigned int ret;
unsigned long flags;
spin_lock_irqsave(&wc->reglock, flags);
ret = __t4_pci_in(wc, addr);
spin_unlock_irqrestore(&wc->reglock, flags);
return ret;
}
static unsigned int __t4_framer_in(const struct t4 *wc, int unit,
const unsigned int addr)
{
unsigned int ret;
register u32 val;
void __iomem *const wc_laddr = wc->membase + (WC_LADDR*sizeof(u32));
void __iomem *const wc_version = wc->membase + (WC_VERSION*sizeof(u32));
void __iomem *const wc_ldata = wc->membase + (WC_LDATA*sizeof(u32));
int haddr = (((unit & 4) ? 0 : WC_LFRMR_CS2));
unit &= 0x3;
val = ((unit & 0x3) << 8) | (addr & 0xff) | haddr;
writel(val, wc_laddr);
readl(wc_version);
writel(val | WC_LFRMR_CS | WC_LREAD, wc_laddr);
readl(wc_version);
ret = readb(wc_ldata);
writel(val, wc_laddr);
readl(wc_version);
return ret;
}
static unsigned int
t4_framer_in(struct t4 *wc, int unit, const unsigned int addr)
{
unsigned long flags;
unsigned int ret;
spin_lock_irqsave(&wc->reglock, flags);
ret = __t4_framer_in(wc, unit, addr);
spin_unlock_irqrestore(&wc->reglock, flags);
return ret;
}
static void __t4_framer_out(const struct t4 *wc, int unit, const u8 addr,
const unsigned int value)
{
register u32 val;
void __iomem *const wc_laddr = wc->membase + (WC_LADDR*sizeof(u32));
void __iomem *const wc_version = wc->membase + (WC_VERSION*sizeof(u32));
void __iomem *const wc_ldata = wc->membase + (WC_LDATA*sizeof(u32));
int haddr = (((unit & 4) ? 0 : WC_LFRMR_CS2));
val = ((unit & 0x3) << 8) | (addr & 0xff) | haddr;
writel(val, wc_laddr);
readl(wc_version);
writel(value, wc_ldata);
readl(wc_version);
writel(val | WC_LFRMR_CS | WC_LWRITE, wc_laddr);
readl(wc_version);
writel(val, wc_laddr);
readl(wc_version);
}
static void t4_framer_out(struct t4 *wc, int unit,
const unsigned int addr,
const unsigned int value)
{
unsigned long flags;
spin_lock_irqsave(&wc->reglock, flags);
__t4_framer_out(wc, unit, addr, value);
spin_unlock_irqrestore(&wc->reglock, flags);
}
#ifdef VPM_SUPPORT
static inline void __t4_raw_oct_out(struct t4 *wc, const unsigned int addr, const unsigned int value)
{
int octopt = wc->tspans[0]->spanflags & FLAG_OCTOPT;
if (!octopt)
__t4_gpio_set(wc, 0xff, (addr >> 8));
__t4_pci_out(wc, WC_LDATA, 0x10000 | (addr & 0xffff));
if (!octopt)
__t4_pci_out(wc, WC_LADDR, (WC_LWRITE));
__t4_pci_out(wc, WC_LADDR, (WC_LWRITE | WC_LALE));
if (!octopt)
__t4_gpio_set(wc, 0xff, (value >> 8));
__t4_pci_out(wc, WC_LDATA, (value & 0xffff));
__t4_pci_out(wc, WC_LADDR, (WC_LWRITE | WC_LALE | WC_LCS));
__t4_pci_out(wc, WC_LADDR, (0));
}
static inline unsigned int __t4_raw_oct_in(struct t4 *wc, const unsigned int addr)
{
unsigned int ret;
int octopt = wc->tspans[0]->spanflags & FLAG_OCTOPT;
if (!octopt)
__t4_gpio_set(wc, 0xff, (addr >> 8));
__t4_pci_out(wc, WC_LDATA, 0x10000 | (addr & 0xffff));
if (!octopt)
__t4_pci_out(wc, WC_LADDR, (WC_LWRITE));
__t4_pci_out(wc, WC_LADDR, (WC_LWRITE | WC_LALE));
__t4_pci_out(wc, WC_LADDR, (WC_LALE));
if (!octopt) {
__t4_gpio_setdir(wc, 0xff, 0x00);
__t4_gpio_set(wc, 0xff, 0x00);
}
__t4_pci_out(wc, WC_LADDR, (WC_LREAD | WC_LALE | WC_LCS));
if (octopt) {
ret = __t4_pci_in(wc, WC_LDATA) & 0xffff;
} else {
ret = __t4_pci_in(wc, WC_LDATA) & 0xff;
ret |= (__t4_pci_in(wc, WC_GPIO) & 0xff) << 8;
}
__t4_pci_out(wc, WC_LADDR, (0));
if (!octopt)
__t4_gpio_setdir(wc, 0xff, 0xff);
return ret & 0xffff;
}
static inline unsigned int __t4_oct_in(struct t4 *wc, unsigned int addr)
{
#ifdef PEDANTIC_OCTASIC_CHECKING
int count = 1000;
#endif
__t4_raw_oct_out(wc, 0x0008, (addr >> 20));
__t4_raw_oct_out(wc, 0x000a, (addr >> 4) & ((1 << 16) - 1));
__t4_raw_oct_out(wc, 0x0000, (((addr >> 1) & 0x7) << 9) | (1 << 8) | (1));
#ifdef PEDANTIC_OCTASIC_CHECKING
while((__t4_raw_oct_in(wc, 0x0000) & (1 << 8)) && --count);
if (count != 1000)
dev_notice(&wc->dev->dev, "Yah, read can be slow...\n");
if (!count)
dev_notice(&wc->dev->dev, "Read timed out!\n");
#endif
return __t4_raw_oct_in(wc, 0x0004);
}
static inline unsigned int t4_oct_in(struct t4 *wc, const unsigned int addr)
{
unsigned long flags;
unsigned int ret;
spin_lock_irqsave(&wc->reglock, flags);
ret = __t4_oct_in(wc, addr);
spin_unlock_irqrestore(&wc->reglock, flags);
return ret;
}
static inline void __t4_oct_out(struct t4 *wc, unsigned int addr, unsigned int value)
{
#ifdef PEDANTIC_OCTASIC_CHECKING
int count = 1000;
#endif
__t4_raw_oct_out(wc, 0x0008, (addr >> 20));
__t4_raw_oct_out(wc, 0x000a, (addr >> 4) & ((1 << 16) - 1));
__t4_raw_oct_out(wc, 0x0004, value);
__t4_raw_oct_out(wc, 0x0000, (((addr >> 1) & 0x7) << 9) | (1 << 8) | (3 << 12) | 1);
#ifdef PEDANTIC_OCTASIC_CHECKING
while((__t4_raw_oct_in(wc, 0x0000) & (1 << 8)) && --count);
if (count != 1000)
dev_notice(&wc->dev->dev, "Yah, write can be slow\n");
if (!count)
dev_notice(&wc->dev->dev, "Write timed out!\n");
#endif
}
static inline void t4_oct_out(struct t4 *wc, const unsigned int addr, const unsigned int value)
{
unsigned long flags;
spin_lock_irqsave(&wc->reglock, flags);
__t4_oct_out(wc, addr, value);
spin_unlock_irqrestore(&wc->reglock, flags);
}
static void t4_check_vpm(struct t4 *wc)
{
int channel, tone, start, span;
if (vpm450m_checkirq(wc->vpm)) {
while(vpm450m_getdtmf(wc->vpm, &channel, &tone, &start)) {
span = channel & 0x3;
channel >>= 2;
if (!has_e1_span(wc))
channel -= 5;
else
channel -= 1;
if (unlikely(debug))
dev_info(&wc->dev->dev, "Got tone %s of '%c' "
"on channel %d of span %d\n",
(start ? "START" : "STOP"),
tone, channel, span + 1);
if (test_bit(channel, &wc->tspans[span]->dtmfmask) && (tone != 'u')) {
if (start) {
/* The octasic is supposed to mute us, but... Yah, you
guessed it. */
if (test_bit(channel, &wc->tspans[span]->dtmfmutemask)) {
unsigned long flags;
struct dahdi_chan *chan = wc->tspans[span]->span.chans[channel];
int y;
spin_lock_irqsave(&chan->lock, flags);
for (y=0;y<chan->numbufs;y++) {
if ((chan->inreadbuf > -1) && (chan->readidx[y]))
memset(chan->readbuf[chan->inreadbuf], DAHDI_XLAW(0, chan), chan->readidx[y]);
}
spin_unlock_irqrestore(&chan->lock, flags);
}
set_bit(channel, &wc->tspans[span]->dtmfactive);
dahdi_qevent_lock(wc->tspans[span]->span.chans[channel], (DAHDI_EVENT_DTMFDOWN | tone));
} else {
clear_bit(channel, &wc->tspans[span]->dtmfactive);
dahdi_qevent_lock(wc->tspans[span]->span.chans[channel], (DAHDI_EVENT_DTMFUP | tone));
}
}
}
}
}
#endif /* VPM_SUPPORT */
static void hdlc_stop(struct t4 *wc, unsigned int span)
{
struct t4_span *t = wc->tspans[span];
unsigned char imr0, imr1, mode;
unsigned long flags;
int i = 0;
if (debug & DEBUG_FRAMER)
dev_notice(&wc->dev->dev, "Stopping HDLC controller on span "
"%d\n", span+1);
/* Clear receive and transmit timeslots */
for (i = 0; i < 4; i++) {
t4_framer_out(wc, span, FRMR_RTR_BASE + i, 0x00);
t4_framer_out(wc, span, FRMR_TTR_BASE + i, 0x00);
}
spin_lock_irqsave(&wc->reglock, flags);
imr0 = __t4_framer_in(wc, span, FRMR_IMR0);
imr1 = __t4_framer_in(wc, span, FRMR_IMR1);
/* Disable HDLC interrupts */
imr0 |= HDLC_IMR0_MASK;
__t4_framer_out(wc, span, FRMR_IMR0, imr0);
imr1 |= HDLC_IMR1_MASK;
__t4_framer_out(wc, span, FRMR_IMR1, imr1);
mode = __t4_framer_in(wc, span, FRMR_MODE);
mode &= ~FRMR_MODE_HRAC;
__t4_framer_out(wc, span, FRMR_MODE, mode);
spin_unlock_irqrestore(&wc->reglock, flags);
t->sigactive = 0;
}
static inline void __t4_framer_cmd(struct t4 *wc, unsigned int span, int cmd)
{
__t4_framer_out(wc, span, FRMR_CMDR, cmd);
}
static inline void t4_framer_cmd_wait(struct t4 *wc, unsigned int span, int cmd)
{
int sis;
int loops = 0;
/* XXX could be time consuming XXX */
for (;;) {
sis = t4_framer_in(wc, span, FRMR_SIS);
if (!(sis & 0x04))
break;
if (!loops++ && (debug & DEBUG_FRAMER)) {
dev_notice(&wc->dev->dev, "!!!SIS Waiting before cmd "
"%02x\n", cmd);
}
}
if (loops && (debug & DEBUG_FRAMER))
dev_notice(&wc->dev->dev, "!!!SIS waited %d loops\n", loops);
t4_framer_out(wc, span, FRMR_CMDR, cmd);
}
static int hdlc_start(struct t4 *wc, unsigned int span, struct dahdi_chan *chan, unsigned char mode)
{
struct t4_span *t = wc->tspans[span];
unsigned char imr0, imr1;
int offset = chan->chanpos;
unsigned long flags;
if (debug & DEBUG_FRAMER)
dev_info(&wc->dev->dev, "Starting HDLC controller for channel "
"%d span %d\n", offset, span+1);
if (mode != FRMR_MODE_NO_ADDR_CMP)
return -1;
mode |= FRMR_MODE_HRAC;
spin_lock_irqsave(&wc->reglock, flags);
/* Make sure we're in the right mode */
__t4_framer_out(wc, span, FRMR_MODE, mode);
__t4_framer_out(wc, span, FRMR_TSEO, 0x00);
__t4_framer_out(wc, span, FRMR_TSBS1, hardhdlcmode);
/* Set the interframe gaps, etc */
__t4_framer_out(wc, span, FRMR_CCR1, FRMR_CCR1_ITF|FRMR_CCR1_EITS);
__t4_framer_out(wc, span, FRMR_CCR2, FRMR_CCR2_RCRC);
/* Set up the time slot that we want to tx/rx on */
__t4_framer_out(wc, span, FRMR_TTR_BASE + (offset / 8), (0x80 >> (offset % 8)));
__t4_framer_out(wc, span, FRMR_RTR_BASE + (offset / 8), (0x80 >> (offset % 8)));
imr0 = __t4_framer_in(wc, span, FRMR_IMR0);
imr1 = __t4_framer_in(wc, span, FRMR_IMR1);
/* Enable our interrupts again */
imr0 &= ~HDLC_IMR0_MASK;
__t4_framer_out(wc, span, FRMR_IMR0, imr0);
imr1 &= ~HDLC_IMR1_MASK;
__t4_framer_out(wc, span, FRMR_IMR1, imr1);
spin_unlock_irqrestore(&wc->reglock, flags);
/* Reset the signaling controller */
t4_framer_cmd_wait(wc, span, FRMR_CMDR_SRES);
spin_lock_irqsave(&wc->reglock, flags);
t->sigchan = chan;
spin_unlock_irqrestore(&wc->reglock, flags);
t->sigactive = 0;
return 0;
}
static void __set_clear(struct t4 *wc, int span)
{
int i,j;
int oldnotclear;
unsigned short val=0;
struct t4_span *ts = wc->tspans[span];
oldnotclear = ts->notclear;
if (E1 != ts->linemode) {
for (i=0;i<24;i++) {
j = (i/8);
if (ts->span.chans[i]->flags & DAHDI_FLAG_CLEAR) {
val |= 1 << (7 - (i % 8));
ts->notclear &= ~(1 << i);
} else
ts->notclear |= (1 << i);
if ((i % 8)==7) {
if (debug)
dev_notice(&wc->dev->dev, "Putting %d "
"in register %02x on span %d"
"\n", val, 0x2f + j, span + 1);
__t4_framer_out(wc, span, 0x2f + j, val);
val = 0;
}
}
} else {
for (i=0;i<31;i++) {
if (ts->span.chans[i]->flags & DAHDI_FLAG_CLEAR)
ts->notclear &= ~(1 << i);
else
ts->notclear |= (1 << i);
}
}
if (ts->notclear != oldnotclear) {
unsigned char reg;
reg = __t4_framer_in(wc, span, FRMR_IMR0);
if (ts->notclear)
reg &= ~0x08;
else
reg |= 0x08;
__t4_framer_out(wc, span, FRMR_IMR0, reg);
}
}
static int t4_dacs(struct dahdi_chan *dst, struct dahdi_chan *src)
{
struct t4 *wc;
struct t4_span *ts;
wc = dst->pvt;
ts = wc->tspans[dst->span->offset];
if (src && (src->pvt != dst->pvt)) {
if (ts->spanflags & FLAG_2NDGEN)
t4_tsi_unassign(wc, dst->span->offset, dst->chanpos);
wc = src->pvt;
if (ts->spanflags & FLAG_2NDGEN)
t4_tsi_unassign(wc, src->span->offset, src->chanpos);
if (debug)
dev_notice(&wc->dev->dev, "Unassigning %d/%d by "
"default and...\n", src->span->offset,
src->chanpos);
if (debug)
dev_notice(&wc->dev->dev, "Unassigning %d/%d by "
"default\n", dst->span->offset, dst->chanpos);
return -1;
}
if (src) {
t4_tsi_assign(wc, src->span->offset, src->chanpos, dst->span->offset, dst->chanpos);
if (debug)
dev_notice(&wc->dev->dev, "Assigning channel %d/%d -> "
"%d/%d!\n", src->span->offset, src->chanpos,
dst->span->offset, dst->chanpos);
} else {
t4_tsi_unassign(wc, dst->span->offset, dst->chanpos);
if (debug)
dev_notice(&wc->dev->dev, "Unassigning channel %d/%d!"
"\n", dst->span->offset, dst->chanpos);
}
return 0;
}
#ifdef VPM_SUPPORT
void oct_set_reg(void *data, unsigned int reg, unsigned int val)
{
struct t4 *wc = data;
t4_oct_out(wc, reg, val);
}
unsigned int oct_get_reg(void *data, unsigned int reg)
{
struct t4 *wc = data;
unsigned int ret;
ret = t4_oct_in(wc, reg);
return ret;
}
static const char *__t4_echocan_name(struct t4 *wc)
{
if (wc->vpm) {
if (wc->numspans == 2)
return vpmoct064_name;
else if (wc->numspans == 4)
return vpmoct128_name;
else if (wc->numspans == 8)
return vpmoct256_name;
}
return NULL;
}
static const char *t4_echocan_name(const struct dahdi_chan *chan)
{
struct t4 *wc = chan->pvt;
return __t4_echocan_name(wc);
}
static int t4_echocan_create(struct dahdi_chan *chan,
struct dahdi_echocanparams *ecp,
struct dahdi_echocanparam *p,
struct dahdi_echocan_state **ec)
{
struct t4 *wc = chan->pvt;
struct t4_span *tspan = container_of(chan->span, struct t4_span, span);
int channel;
const bool alaw = (chan->span->deflaw == 2);
if (!vpmsupport || !wc->vpm)
return -ENODEV;
if (ecp->param_count > 0) {
dev_warn(&wc->dev->dev, "%s echo canceller does not support "
"parameters; failing request\n",
chan->ec_factory->get_name(chan));
return -EINVAL;
}
*ec = tspan->ec[chan->chanpos - 1];
(*ec)->ops = &vpm_ec_ops;
(*ec)->features = vpm_ec_features;
channel = has_e1_span(wc) ? chan->chanpos : chan->chanpos + 4;
if (is_octal(wc))
channel = channel << 3;
else
channel = channel << 2;
channel |= chan->span->offset;
if (debug & DEBUG_ECHOCAN) {
dev_notice(&wc->dev->dev,
"echocan: Card is %d, Channel is %d, Span is %d, offset is %d length %d\n",
wc->num, chan->chanpos, chan->span->offset,
channel, ecp->tap_length);
}
vpm450m_set_alaw_companding(wc->vpm, channel, alaw);
vpm450m_setec(wc->vpm, channel, ecp->tap_length);
return 0;
}
static void echocan_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec)
{
struct t4 *wc = chan->pvt;
int channel;
if (!wc->vpm)
return;
memset(ec, 0, sizeof(*ec));
channel = has_e1_span(wc) ? chan->chanpos : chan->chanpos + 4;
if (is_octal(wc))
channel = channel << 3;
else
channel = channel << 2;
channel |= chan->span->offset;
if (debug & DEBUG_ECHOCAN) {
dev_notice(&wc->dev->dev,
"echocan: Card is %d, Channel is %d, Span is %d, offset is %d length 0\n",
wc->num, chan->chanpos, chan->span->offset, channel);
}
vpm450m_setec(wc->vpm, channel, 0);
}
#endif
static int t4_ioctl(struct dahdi_chan *chan, unsigned int cmd, unsigned long data)
{
struct t4_regs regs;
struct t4_reg reg;
int x;
struct t4 *wc = chan->pvt;
#ifdef VPM_SUPPORT
int j;
int channel;
struct t4_span *ts = wc->tspans[chan->span->offset];
#endif
switch(cmd) {
case WCT4_SET_REG:
if (copy_from_user(®, (struct t4_reg __user *)data,
sizeof(reg)))
return -EFAULT;
t4_pci_out(wc, reg.reg, reg.val);
break;
case WCT4_GET_REG:
if (copy_from_user(®, (struct t4_reg __user *)data,
sizeof(reg)))
return -EFAULT;
reg.val = t4_pci_in(wc, reg.reg);
if (copy_to_user((struct t4_reg __user *)data,
®, sizeof(reg)))
return -EFAULT;
break;
case WCT4_GET_REGS:
for (x=0;x<NUM_PCI;x++)
regs.pci[x] = t4_pci_in(wc, x);
for (x=0;x<NUM_REGS;x++)
regs.regs[x] = t4_framer_in(wc, chan->span->offset, x);
if (copy_to_user((void __user *) data,
®s, sizeof(regs)))
return -EFAULT;
break;
#ifdef VPM_SUPPORT
case DAHDI_TONEDETECT:
if (get_user(j, (__user int *) data))
return -EFAULT;
if (!wc->vpm)
return -ENOSYS;
if (j && (vpmdtmfsupport == 0))
return -ENOSYS;
if (j & DAHDI_TONEDETECT_ON)
set_bit(chan->chanpos - 1, &ts->dtmfmask);
else
clear_bit(chan->chanpos - 1, &ts->dtmfmask);
if (j & DAHDI_TONEDETECT_MUTE)
set_bit(chan->chanpos - 1, &ts->dtmfmutemask);
else
clear_bit(chan->chanpos - 1, &ts->dtmfmutemask);
channel = has_e1_span(wc) ? chan->chanpos : chan->chanpos + 4;
if (is_octal(wc))
channel = channel << 3;
else
channel = channel << 2;
channel |= chan->span->offset;
vpm450m_setdtmf(wc->vpm, channel, j & DAHDI_TONEDETECT_ON,
j & DAHDI_TONEDETECT_MUTE);
return 0;
#endif
default:
return -ENOTTY;
}
return 0;
}
static void inline t4_hdlc_xmit_fifo(struct t4 *wc, unsigned int span, struct t4_span *ts)
{
int res, i;
unsigned int size = 32;
unsigned char buf[32];
res = dahdi_hdlc_getbuf(ts->sigchan, buf, &size);
if (debug & DEBUG_FRAMER)
dev_notice(&wc->dev->dev, "Got buffer sized %d and res %d "
"for %d\n", size, res, span);
if (size > 0) {
ts->sigactive = 1;
if (debug & DEBUG_FRAMER) {
dev_notice(&wc->dev->dev, "TX(");
for (i = 0; i < size; i++)
dev_notice(&wc->dev->dev, "%s%02x",
(i ? " " : ""), buf[i]);
dev_notice(&wc->dev->dev, ")\n");
}
for (i = 0; i < size; i++)
t4_framer_out(wc, span, FRMR_TXFIFO, buf[i]);
if (res) /* End of message */ {
if (debug & DEBUG_FRAMER)
dev_notice(&wc->dev->dev,
"transmiting XHF|XME\n");
t4_framer_cmd_wait(wc, span, FRMR_CMDR_XHF | FRMR_CMDR_XME);
++ts->frames_out;
if ((debug & DEBUG_FRAMER) && !(ts->frames_out & 0x0f))
dev_notice(&wc->dev->dev, "Transmitted %d "
"frames on span %d\n", ts->frames_out,
span);
} else { /* Still more to transmit */
if (debug & DEBUG_FRAMER)
dev_notice(&wc->dev->dev, "transmiting XHF\n");
t4_framer_cmd_wait(wc, span, FRMR_CMDR_XHF);
}
}
else if (res < 0)
ts->sigactive = 0;
}
static void t4_hdlc_hard_xmit(struct dahdi_chan *chan)
{
struct t4 *wc = chan->pvt;
int span = chan->span->offset;
struct t4_span *ts = wc->tspans[span];
unsigned long flags;
spin_lock_irqsave(&wc->reglock, flags);
if (!ts->sigchan) {
dev_notice(&wc->dev->dev, "t4_hdlc_hard_xmit: Invalid (NULL) "
"signalling channel\n");
spin_unlock_irqrestore(&wc->reglock, flags);
return;
}
spin_unlock_irqrestore(&wc->reglock, flags);
if (debug & DEBUG_FRAMER)
dev_notice(&wc->dev->dev, "t4_hdlc_hard_xmit on channel %s "
"(sigchan %s), sigactive=%d\n", chan->name,
ts->sigchan->name, ts->sigactive);
if ((ts->sigchan == chan) && !ts->sigactive)
t4_hdlc_xmit_fifo(wc, span, ts);
}
/**
* t4_set_framer_bits - Atomically set bits in a framer register.
*/
static void t4_set_framer_bits(struct t4 *wc, unsigned int spanno,
unsigned int const addr, u16 bits)
{
unsigned long flags;
unsigned int reg;
spin_lock_irqsave(&wc->reglock, flags);
reg = __t4_framer_in(wc, spanno, addr);
__t4_framer_out(wc, spanno, addr, (reg | bits));
spin_unlock_irqrestore(&wc->reglock, flags);
}
static int t4_maint(struct dahdi_span *span, int cmd)
{
struct t4_span *ts = container_of(span, struct t4_span, span);
struct t4 *wc = ts->owner;
unsigned int reg;
unsigned long flags;
if (E1 == ts->linemode) {
switch(cmd) {
case DAHDI_MAINT_NONE:
dev_info(&wc->dev->dev, "Clearing all maint modes\n");
t4_clear_maint(span);
break;
case DAHDI_MAINT_LOCALLOOP:
dev_info(&wc->dev->dev,
"Turning on local loopback\n");
t4_clear_maint(span);
t4_set_framer_bits(wc, span->offset, LIM0_T, LIM0_LL);
break;
case DAHDI_MAINT_NETWORKLINELOOP:
dev_info(&wc->dev->dev,
"Turning on network line loopback\n");
t4_clear_maint(span);
t4_set_framer_bits(wc, span->offset, LIM1_T, LIM1_RL);
break;
case DAHDI_MAINT_NETWORKPAYLOADLOOP:
dev_info(&wc->dev->dev,
"Turning on network payload loopback\n");
t4_clear_maint(span);
t4_set_framer_bits(wc, span->offset, FMR2_T, FMR2_PLB);
break;
case DAHDI_MAINT_LOOPUP:
case DAHDI_MAINT_LOOPDOWN:
dev_info(&wc->dev->dev,
"Loopup & loopdown not supported in E1 mode\n");
return -ENOSYS;
case DAHDI_MAINT_FAS_DEFECT:
t4_framer_out(wc, span->offset, IERR_T, IFASE);
break;
case DAHDI_MAINT_MULTI_DEFECT:
t4_framer_out(wc, span->offset, IERR_T, IMFE);
break;
case DAHDI_MAINT_CRC_DEFECT:
t4_framer_out(wc, span->offset, IERR_T, ICRCE);
break;
case DAHDI_MAINT_CAS_DEFECT:
t4_framer_out(wc, span->offset, IERR_T, ICASE);
break;
case DAHDI_MAINT_PRBS_DEFECT:
t4_framer_out(wc, span->offset, IERR_T, IPE);
break;
case DAHDI_MAINT_BIPOLAR_DEFECT:
t4_framer_out(wc, span->offset, IERR_T, IBV);
break;
case DAHDI_RESET_COUNTERS:
t4_reset_counters(span);
break;
case DAHDI_MAINT_ALARM_SIM:
dev_info(&wc->dev->dev, "Invoking alarm state");
t4_set_framer_bits(wc, span->offset, FMR0, FMR0_SIM);
break;
default:
dev_info(&wc->dev->dev,
"Unknown E1 maint command: %d\n", cmd);
return -ENOSYS;
}
} else {
switch(cmd) {
case DAHDI_MAINT_NONE:
dev_info(&wc->dev->dev, "Clearing all maint modes\n");
t4_clear_maint(span);
break;
case DAHDI_MAINT_LOCALLOOP:
dev_info(&wc->dev->dev,
"Turning on local loopback\n");
t4_clear_maint(span);
t4_set_framer_bits(wc, span->offset, LIM0_T, LIM0_LL);
break;
case DAHDI_MAINT_NETWORKLINELOOP:
dev_info(&wc->dev->dev,
"Turning on network line loopback\n");
t4_clear_maint(span);
t4_set_framer_bits(wc, span->offset, LIM1_T, LIM1_RL);
break;
case DAHDI_MAINT_NETWORKPAYLOADLOOP:
dev_info(&wc->dev->dev,
"Turning on network payload loopback\n");
t4_clear_maint(span);
t4_set_framer_bits(wc, span->offset, FMR2_T, FMR2_PLB);
break;
case DAHDI_MAINT_LOOPUP:
dev_info(&wc->dev->dev, "Transmitting loopup code\n");
t4_clear_maint(span);
t4_set_framer_bits(wc, span->offset, FMR5, FMR5_XLU);
ts->span.maintstat = DAHDI_MAINT_REMOTELOOP;
break;
case DAHDI_MAINT_LOOPDOWN:
dev_info(&wc->dev->dev, "Transmitting loopdown code\n");
t4_clear_maint(span);
t4_set_framer_bits(wc, span->offset, FMR5, FMR5_XLD);
break;
case DAHDI_MAINT_FAS_DEFECT:
t4_framer_out(wc, span->offset, IERR_T, IFASE);
break;
case DAHDI_MAINT_MULTI_DEFECT:
t4_framer_out(wc, span->offset, IERR_T, IMFE);
break;
case DAHDI_MAINT_CRC_DEFECT:
t4_framer_out(wc, span->offset, IERR_T, ICRCE);
break;
case DAHDI_MAINT_CAS_DEFECT:
t4_framer_out(wc, span->offset, IERR_T, ICASE);
break;
case DAHDI_MAINT_PRBS_DEFECT:
t4_framer_out(wc, span->offset, IERR_T, IPE);
break;
case DAHDI_MAINT_BIPOLAR_DEFECT:
t4_framer_out(wc, span->offset, IERR_T, IBV);
break;
case DAHDI_MAINT_PRBS:
dev_info(&wc->dev->dev, "PRBS not supported\n");
return -ENOSYS;
case DAHDI_RESET_COUNTERS:
t4_reset_counters(span);
break;
case DAHDI_MAINT_ALARM_SIM:
spin_lock_irqsave(&wc->reglock, flags);
reg = __t4_framer_in(wc, span->offset, FMR0);
/*
* The alarm simulation state machine requires us to
* bring this bit up and down for at least 1 clock cycle
*/
__t4_framer_out(wc, span->offset,
FMR0, (reg | FMR0_SIM));
udelay(1);
__t4_framer_out(wc, span->offset,
FMR0, (reg & ~FMR0_SIM));
udelay(1);
spin_unlock_irqrestore(&wc->reglock, flags);
reg = t4_framer_in(wc, span->offset, 0x4e);
if (debug & DEBUG_MAIN) {
dev_info(&wc->dev->dev,
"FRS2(alarm state): %d\n",
((reg & 0xe0) >> 5));
}
break;
default:
dev_info(&wc->dev->dev, "Unknown T1 maint command:%d\n",
cmd);
break;
}
}
return 0;
}
static int t4_clear_maint(struct dahdi_span *span)
{
struct t4_span *ts = container_of(span, struct t4_span, span);
struct t4 *wc = ts->owner;
unsigned int reg;
unsigned long flags;
spin_lock_irqsave(&wc->reglock, flags);
/* Clear local loop */
reg = __t4_framer_in(wc, span->offset, LIM0_T);
__t4_framer_out(wc, span->offset, LIM0_T, (reg & ~LIM0_LL));
/* Clear Remote Loop */
reg = __t4_framer_in(wc, span->offset, LIM1_T);
__t4_framer_out(wc, span->offset, LIM1_T, (reg & ~LIM1_RL));
/* Clear Remote Payload Loop */
reg = __t4_framer_in(wc, span->offset, FMR2_T);
__t4_framer_out(wc, span->offset, FMR2_T, (reg & ~FMR2_PLB));
/* Clear PRBS */
reg = __t4_framer_in(wc, span->offset, LCR1_T);
__t4_framer_out(wc, span->offset, LCR1_T, (reg & ~(XPRBS | EPRM)));
/* Clear loopup/loopdown signals on the line */
reg = __t4_framer_in(wc, span->offset, FMR5);
__t4_framer_out(wc, span->offset, FMR5, (reg & ~(FMR5_XLU | FMR5_XLD)));
spin_unlock_irqrestore(&wc->reglock, flags);
span->mainttimer = 0;
return 0;
}
static int t4_reset_counters(struct dahdi_span *span)
{
struct t4_span *ts = container_of(span, struct t4_span, span);
memset(&ts->span.count, 0, sizeof(ts->span.count));
return 0;
}
static int t4_rbsbits(struct dahdi_chan *chan, int bits)
{
u_char m,c;
int k,n,b;
struct t4 *wc = chan->pvt;
struct t4_span *ts = wc->tspans[chan->span->offset];
unsigned long flags;
if (debug & DEBUG_RBS)
dev_notice(&wc->dev->dev, "Setting bits to %d on channel %s\n",
bits, chan->name);
spin_lock_irqsave(&wc->reglock, flags);
k = chan->span->offset;
if (E1 == ts->linemode) {
if (chan->chanpos == 16) {
spin_unlock_irqrestore(&wc->reglock, flags);
return 0;
}
n = chan->chanpos - 1;
if (chan->chanpos > 15) n--;
b = (n % 15);
c = ts->txsigs[b];
m = (n / 15) << 2; /* nibble selector */
c &= (0xf << m); /* keep the other nibble */
c |= (bits & 0xf) << (4 - m); /* put our new nibble here */
ts->txsigs[b] = c;
/* output them to the chip */
__t4_framer_out(wc,k,0x71 + b,c);
} else if (ts->span.lineconfig & DAHDI_CONFIG_D4) {
n = chan->chanpos - 1;
b = (n/4);
c = ts->txsigs[b];
m = ((3 - (n % 4)) << 1); /* nibble selector */
c &= ~(0x3 << m); /* keep the other nibble */
c |= ((bits >> 2) & 0x3) << m; /* put our new nibble here */
ts->txsigs[b] = c;
/* output them to the chip */
__t4_framer_out(wc,k,0x70 + b,c);
__t4_framer_out(wc,k,0x70 + b + 6,c);
} else if (ts->span.lineconfig & DAHDI_CONFIG_ESF) {
n = chan->chanpos - 1;
b = (n/2);
c = ts->txsigs[b];
m = ((n % 2) << 2); /* nibble selector */
c &= (0xf << m); /* keep the other nibble */
c |= (bits & 0xf) << (4 - m); /* put our new nibble here */
ts->txsigs[b] = c;
/* output them to the chip */
__t4_framer_out(wc,k,0x70 + b,c);
}
spin_unlock_irqrestore(&wc->reglock, flags);
if (debug & DEBUG_RBS)
dev_notice(&wc->dev->dev, "Finished setting RBS bits\n");
return 0;
}
static int t4_shutdown(struct dahdi_span *span)
{
int tspan;
int wasrunning;
unsigned long flags;
struct t4_span *ts = container_of(span, struct t4_span, span);
struct t4 *wc = ts->owner;
tspan = span->offset + 1;
if (tspan < 0) {
dev_notice(&wc->dev->dev, "T%dXXP: Span '%d' isn't us?\n",
wc->numspans, span->spanno);
return -1;
}
if (debug & DEBUG_MAIN)
dev_notice(&wc->dev->dev, "Shutting down span %d (%s)\n",
span->spanno, span->name);
/* Stop HDLC controller if runned */
if (ts->sigchan)
hdlc_stop(wc, span->offset);
spin_lock_irqsave(&wc->reglock, flags);
wasrunning = span->flags & DAHDI_FLAG_RUNNING;
span->flags &= ~DAHDI_FLAG_RUNNING;
__t4_set_led(wc, span->offset, WC_OFF);
if (((wc->numspans == 8) &&
(!(wc->tspans[0]->span.flags & DAHDI_FLAG_RUNNING)) &&
(!(wc->tspans[1]->span.flags & DAHDI_FLAG_RUNNING)) &&
(!(wc->tspans[2]->span.flags & DAHDI_FLAG_RUNNING)) &&
(!(wc->tspans[3]->span.flags & DAHDI_FLAG_RUNNING)) &&
(!(wc->tspans[4]->span.flags & DAHDI_FLAG_RUNNING)) &&
(!(wc->tspans[5]->span.flags & DAHDI_FLAG_RUNNING)) &&
(!(wc->tspans[6]->span.flags & DAHDI_FLAG_RUNNING)) &&
(!(wc->tspans[7]->span.flags & DAHDI_FLAG_RUNNING)))
||
((wc->numspans == 4) &&
(!(wc->tspans[0]->span.flags & DAHDI_FLAG_RUNNING)) &&
(!(wc->tspans[1]->span.flags & DAHDI_FLAG_RUNNING)) &&
(!(wc->tspans[2]->span.flags & DAHDI_FLAG_RUNNING)) &&
(!(wc->tspans[3]->span.flags & DAHDI_FLAG_RUNNING)))
||
((wc->numspans == 2) &&
(!(wc->tspans[0]->span.flags & DAHDI_FLAG_RUNNING)) &&
(!(wc->tspans[1]->span.flags & DAHDI_FLAG_RUNNING)))) {
/* No longer in use, disable interrupts */
dev_info(&wc->dev->dev, "TE%dXXP: Disabling interrupts since "
"there are no active spans\n", wc->numspans);
set_bit(T4_STOP_DMA, &wc->checkflag);
} else
set_bit(T4_CHECK_TIMING, &wc->checkflag);
spin_unlock_irqrestore(&wc->reglock, flags);
/* Wait for interrupt routine to shut itself down */
msleep(10);
if (wasrunning)
wc->spansstarted--;
if (debug & DEBUG_MAIN)
dev_notice(&wc->dev->dev, "Span %d (%s) shutdown\n",
span->spanno, span->name);
return 0;
}
static void t4_chan_set_sigcap(struct dahdi_span *span, int x)
{
struct t4_span *wc = container_of(span, struct t4_span, span);
struct dahdi_chan *chan = wc->chans[x];
chan->sigcap = DAHDI_SIG_CLEAR;
/* E&M variant supported depends on span type */
if (E1 == wc->linemode) {
/* E1 sigcap setup */
if (span->lineconfig & DAHDI_CONFIG_CCS) {
/* CCS setup */
chan->sigcap |= DAHDI_SIG_MTP2 | DAHDI_SIG_SF |
DAHDI_SIG_HARDHDLC;
return;
}
/* clear out sig and sigcap for channel 16 on E1 CAS
* lines, otherwise, set it correctly */
if (x == 15) {
/* CAS signaling channel setup */
wc->chans[15]->sigcap = 0;
wc->chans[15]->sig = 0;
return;
}
/* normal CAS setup */
chan->sigcap |= DAHDI_SIG_EM_E1 | DAHDI_SIG_FXSLS |
DAHDI_SIG_FXSGS | DAHDI_SIG_FXSKS | DAHDI_SIG_SF |
DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS | DAHDI_SIG_FXOKS |
DAHDI_SIG_CAS | DAHDI_SIG_DACS_RBS;
} else {
/* T1 sigcap setup */
chan->sigcap |= DAHDI_SIG_EM | DAHDI_SIG_FXSLS |
DAHDI_SIG_FXSGS | DAHDI_SIG_FXSKS | DAHDI_SIG_MTP2 |
DAHDI_SIG_SF | DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS |
DAHDI_SIG_FXOKS | DAHDI_SIG_CAS | DAHDI_SIG_DACS_RBS |
DAHDI_SIG_HARDHDLC;
}
}
static int
_t4_spanconfig(struct file *file, struct dahdi_span *span,
struct dahdi_lineconfig *lc)
{
int i;
struct t4_span *ts = container_of(span, struct t4_span, span);
struct t4 *wc = ts->owner;
if (debug)
dev_info(&wc->dev->dev, "About to enter spanconfig!\n");
if (debug & DEBUG_MAIN)
dev_notice(&wc->dev->dev, "TE%dXXP: Configuring span %d\n",
wc->numspans, span->spanno);
if (lc->sync < 0)
lc->sync = 0;
if (lc->sync > wc->numspans) {
dev_warn(&wc->dev->dev, "WARNING: Cannot set priority on span %d to %d. Please set to a number between 1 and %d\n",
span->spanno, lc->sync, wc->numspans);
lc->sync = 0;
}
/* remove this span number from the current sync sources, if there */
for(i = 0; i < wc->numspans; i++) {
if (wc->tspans[i]->sync == span->spanno)
wc->tspans[i]->sync = 0;
}
wc->tspans[span->offset]->syncpos = lc->sync;
/* if a sync src, put it in proper place */
if (lc->sync)
wc->tspans[lc->sync - 1]->sync = span->spanno;
set_bit(T4_CHECK_TIMING, &wc->checkflag);
/* Make sure this is clear in case of multiple startup and shutdown
* iterations */
clear_bit(T4_STOP_DMA, &wc->checkflag);
/* make sure that sigcaps gets updated if necessary */
for (i = 0; i < span->channels; i++)
t4_chan_set_sigcap(span, i);
/* If we're already running, then go ahead and apply the changes */
if (span->flags & DAHDI_FLAG_RUNNING)
return _t4_startup(file, span);
if (debug)
dev_info(&wc->dev->dev, "Done with spanconfig!\n");
return 0;
}
static int
t4_spanconfig(struct file *file, struct dahdi_span *span,
struct dahdi_lineconfig *lc)
{
int ret;
struct dahdi_device *const ddev = span->parent;
struct dahdi_span *s;
ret = _t4_spanconfig(file, span, lc);
/* Make sure all the spans have a basic configuration in case they are
* not all specified in the configuration files. */
lc->sync = 0;
list_for_each_entry(s, &ddev->spans, device_node) {
WARN_ON(!s->channels);
if (!s->channels)
continue;
if (!s->chans[0]->sigcap)
_t4_spanconfig(file, s, lc);
}
return ret;
}
static int
t4_chanconfig(struct file *file, struct dahdi_chan *chan, int sigtype)
{
int alreadyrunning;
unsigned long flags;
struct t4 *wc = chan->pvt;
struct t4_span *ts = wc->tspans[chan->span->offset];
alreadyrunning = ts->span.flags & DAHDI_FLAG_RUNNING;
if (debug & DEBUG_MAIN) {
if (alreadyrunning)
dev_notice(&wc->dev->dev, "TE%dXXP: Reconfigured "
"channel %d (%s) sigtype %d\n", wc->numspans,
chan->channo, chan->name, sigtype);
else
dev_notice(&wc->dev->dev, "TE%dXXP: Configured channel"
" %d (%s) sigtype %d\n", wc->numspans,
chan->channo, chan->name, sigtype);
}
spin_lock_irqsave(&wc->reglock, flags);
if (alreadyrunning)
__set_clear(wc, chan->span->offset);
spin_unlock_irqrestore(&wc->reglock, flags);
/* (re)configure signalling channel */
if ((sigtype == DAHDI_SIG_HARDHDLC) || (ts->sigchan == chan)) {
if (debug & DEBUG_FRAMER)
dev_notice(&wc->dev->dev, "%sonfiguring hardware HDLC "
"on %s\n",
((sigtype == DAHDI_SIG_HARDHDLC) ? "C" : "Unc"),
chan->name);
if (alreadyrunning) {
if (ts->sigchan)
hdlc_stop(wc, ts->sigchan->span->offset);
if (sigtype == DAHDI_SIG_HARDHDLC) {
if (hdlc_start(wc, chan->span->offset, chan, ts->sigmode)) {
dev_notice(&wc->dev->dev, "Error "
"initializing signalling "
"controller\n");
return -1;
}
} else {
spin_lock_irqsave(&wc->reglock, flags);
ts->sigchan = NULL;
spin_unlock_irqrestore(&wc->reglock, flags);
}
}
else {
spin_lock_irqsave(&wc->reglock, flags);
ts->sigchan = (sigtype == DAHDI_SIG_HARDHDLC) ? chan : NULL;
spin_unlock_irqrestore(&wc->reglock, flags);
ts->sigactive = 0;
}
}
return 0;
}
static int set_span_devicetype(struct t4 *wc)
{
#ifdef VPM_SUPPORT
const char *vpmstring = __t4_echocan_name(wc);
if (vpmstring) {
wc->ddev->devicetype = kasprintf(GFP_KERNEL, "%s (%s)",
wc->devtype->desc, vpmstring);
} else {
wc->ddev->devicetype = kasprintf(GFP_KERNEL, wc->devtype->desc);
}
#else
wc->ddev->devicetype = kasprintf(GFP_KERNEL, wc->devtype->desc);
#endif
if (!wc->ddev->devicetype)
return -ENOMEM;
return 0;
}
/* The number of cards we have seen with each
possible 'order' switch setting.
*/
static unsigned int order_index[16];
static void setup_chunks(struct t4 *wc, int which)
{
struct t4_span *ts;
int offset = 1;
int x, y;
int gen2;
int basesize = T4_BASE_SIZE(wc) >> 2;
if (!has_e1_span(wc))
offset += 4;
gen2 = (wc->tspans[0]->spanflags & FLAG_2NDGEN);
for (x = 0; x < wc->numspans; x++) {
ts = wc->tspans[x];
ts->writechunk = (void *)(wc->writechunk + (x * 32 * 2) + (which * (basesize)));
ts->readchunk = (void *)(wc->readchunk + (x * 32 * 2) + (which * (basesize)));
for (y=0;y<wc->tspans[x]->span.channels;y++) {
struct dahdi_chan *mychans = ts->chans[y];
if (gen2) {
mychans->writechunk = (void *)(wc->writechunk + ((x * 32 + y + offset) * 2) + (which * (basesize)));
mychans->readchunk = (void *)(wc->readchunk + ((x * 32 + y + offset) * 2) + (which * (basesize)));
}
}
}
}
static int __t4_hardware_init_1(struct t4 *wc, unsigned int cardflags,
bool first_time);
static int __t4_hardware_init_2(struct t4 *wc, bool first_time);
static int t4_hardware_stop(struct t4 *wc);
static void t4_framer_reset(struct t4 *wc)
{
const bool first_time = false;
bool have_vpm = wc->vpm != NULL;
if (have_vpm) {
release_vpm450m(wc->vpm);
wc->vpm = NULL;
}
t4_hardware_stop(wc);
__t4_set_sclk_src(wc, WC_SELF, 0, 0);
__t4_hardware_init_1(wc, wc->devtype->flags, first_time);
__t4_hardware_init_2(wc, first_time);
if (have_vpm) {
t4_vpm_init(wc);
wc->dmactrl |= (wc->vpm) ? T4_VPM_PRESENT : 0;
t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
}
setup_chunks(wc, 0);
wc->lastindex = 0;
}
/**
* t4_serial_setup - Setup serial parameters and system interface.
* @wc: The card to configure.
*
*/
static void t4_serial_setup(struct t4 *wc)
{
unsigned long flags;
unsigned int unit;
bool reset_required = false;
if (debug) {
dev_info(&wc->dev->dev,
"TE%dXXP: Setting up global serial parameters\n",
wc->numspans);
}
spin_lock_irqsave(&wc->reglock, flags);
reset_required = wc->reset_required > 0;
wc->reset_required = 0;
spin_unlock_irqrestore(&wc->reglock, flags);
if (reset_required)
t4_framer_reset(wc);
spin_lock_irqsave(&wc->reglock, flags);
/* GPC1: Multiplex mode enabled, FSC is output, active low, RCLK from
* channel 0 */
__t4_framer_out(wc, 0, 0x85, 0xe0);
if (is_octal(wc))
__t4_framer_out(wc, 0, FRMR_GPC2, 0x00);
/* IPC: Interrupt push/pull active low */
__t4_framer_out(wc, 0, 0x08, 0x01);
if (is_octal(wc)) {
/* Global clocks (16.384 Mhz CLK) */
__t4_framer_out(wc, 0, 0x92, 0x00); /* GCM1 */
__t4_framer_out(wc, 0, 0x93, 0x18);
__t4_framer_out(wc, 0, 0x94, 0xfb);
__t4_framer_out(wc, 0, 0x95, 0x0b);
__t4_framer_out(wc, 0, 0x96, 0x01);
__t4_framer_out(wc, 0, 0x97, 0x0b);
__t4_framer_out(wc, 0, 0x98, 0xdb);
__t4_framer_out(wc, 0, 0x99, 0xdf);
} else {
/* Global clocks (8.192 Mhz CLK) */
__t4_framer_out(wc, 0, 0x92, 0x00);
__t4_framer_out(wc, 0, 0x93, 0x18);
__t4_framer_out(wc, 0, 0x94, 0xfb);
__t4_framer_out(wc, 0, 0x95, 0x0b);
__t4_framer_out(wc, 0, 0x96, 0x00);
__t4_framer_out(wc, 0, 0x97, 0x0b);
__t4_framer_out(wc, 0, 0x98, 0xdb);
__t4_framer_out(wc, 0, 0x99, 0xdf);
}
spin_unlock_irqrestore(&wc->reglock, flags);
for (unit = 0; unit < ports_on_framer(wc); ++unit) {
spin_lock_irqsave(&wc->reglock, flags);
/* Configure interrupts */
/* GCR: Interrupt on Activation/Deactivation of each */
__t4_framer_out(wc, unit, FRMR_GCR, 0x00);
/* Configure system interface */
if (is_octal(wc)) {
/* SIC1: 16.384 Mhz clock/bus, double buffer receive /
* transmit, byte interleaved */
__t4_framer_out(wc, unit, FRMR_SIC1, 0xc2 | 0x08);
} else {
/* SIC1: 8.192 Mhz clock/bus, double buffer receive /
* transmit, byte interleaved */
__t4_framer_out(wc, unit, FRMR_SIC1, 0xc2);
}
/* SIC2: No FFS, no center receive eliastic buffer, phase */
__t4_framer_out(wc, unit, FRMR_SIC2, 0x20 | (unit << 1));
/* SIC3: Edges for capture */
if (is_octal(wc)) {
__t4_framer_out(wc, unit, FRMR_SIC3, 0x04 | (1 << 4));
} else {
__t4_framer_out(wc, unit, FRMR_SIC3, 0x04);
}
/* CMR2: We provide sync and clock for tx and rx. */
__t4_framer_out(wc, unit, FRMR_CMR2, 0x00);
if (is_octal(wc)) {
/* Set RCLK to 16 MHz */
__t4_framer_out(wc, unit, FRMR_CMR4, 0x5);
if (!has_e1_span(wc)) { /* T1/J1 mode */
__t4_framer_out(wc, unit, FRMR_XC0, 0x07);
__t4_framer_out(wc, unit, FRMR_XC1, 0x04);
if (wc->tspans[unit]->linemode == J1)
__t4_framer_out(wc, unit, FRMR_RC0, 0x87);
else
__t4_framer_out(wc, unit, FRMR_RC0, 0x07);
__t4_framer_out(wc, unit, FRMR_RC1, 0x04);
} else { /* E1 mode */
__t4_framer_out(wc, unit, FRMR_XC0, 0x00);
__t4_framer_out(wc, unit, FRMR_XC1, 0x04);
__t4_framer_out(wc, unit, FRMR_RC0, 0x00);
__t4_framer_out(wc, unit, FRMR_RC1, 0x04);
}
} else {
if (!has_e1_span(wc)) { /* T1/J1 mode */
__t4_framer_out(wc, unit, FRMR_XC0, 0x03);
__t4_framer_out(wc, unit, FRMR_XC1, 0x84);
if (J1 == wc->tspans[unit]->linemode)
__t4_framer_out(wc, unit, FRMR_RC0, 0x83);
else
__t4_framer_out(wc, unit, FRMR_RC0, 0x03);
__t4_framer_out(wc, unit, FRMR_RC1, 0x84);
} else { /* E1 mode */
__t4_framer_out(wc, unit, FRMR_XC0, 0x00);
__t4_framer_out(wc, unit, FRMR_XC1, 0x04);
__t4_framer_out(wc, unit, FRMR_RC0, 0x04);
__t4_framer_out(wc, unit, FRMR_RC1, 0x04);
}
}
/* Configure ports */
/* PC1: SPYR/SPYX input on RPA/XPA */
__t4_framer_out(wc, unit, 0x80, 0x00);
/* PC2: RMFB/XSIG output/input on RPB/XPB */
/* PC3: Some unused stuff */
/* PC4: Some more unused stuff */
if (is_octal(wc)) {
__t4_framer_out(wc, unit, 0x81, 0xBB);
__t4_framer_out(wc, unit, 0x82, 0xf5);
__t4_framer_out(wc, unit, 0x83, 0x35);
} else if (wc->falc31) {
__t4_framer_out(wc, unit, 0x81, 0xBB);
__t4_framer_out(wc, unit, 0x82, 0xBB);
__t4_framer_out(wc, unit, 0x83, 0xBB);
} else {
__t4_framer_out(wc, unit, 0x81, 0x22);
__t4_framer_out(wc, unit, 0x82, 0x65);
__t4_framer_out(wc, unit, 0x83, 0x35);
}
/* PC5: XMFS active low, SCLKR is input, RCLK is output */
__t4_framer_out(wc, unit, 0x84, 0x01);
if (debug & DEBUG_MAIN) {
dev_notice(&wc->dev->dev,
"Successfully initialized serial bus "
"for unit %d\n", unit);
}
spin_unlock_irqrestore(&wc->reglock, flags);
}
}
/**
* t4_span_assigned - Called when the span is assigned by DAHDI.
* @span: Span that has been assigned.
*
* When this function is called, the span has a valid spanno and all the
* channels on the span have valid channel numbers assigned.
*
* This function is necessary because a device may be registered, and
* then user space may then later decide to assign span numbers and the
* channel numbers.
*
*/
static void t4_span_assigned(struct dahdi_span *span)
{
struct t4_span *tspan = container_of(span, struct t4_span, span);
struct t4 *wc = tspan->owner;
struct dahdi_span *pos;
unsigned int unassigned_spans = 0;
unsigned long flags;
/* We use this to make sure all the spans are assigned before
* running the serial setup. */
list_for_each_entry(pos, &wc->ddev->spans, device_node) {
if (!test_bit(DAHDI_FLAGBIT_REGISTERED, &pos->flags))
++unassigned_spans;
}
if (0 == unassigned_spans) {
t4_serial_setup(wc);
set_bit(T4_CHECK_TIMING, &wc->checkflag);
spin_lock_irqsave(&wc->reglock, flags);
__t4_set_sclk_src(wc, WC_SELF, 0, 0);
spin_unlock_irqrestore(&wc->reglock, flags);
}
}
static void free_wc(struct t4 *wc)
{
unsigned int x, y;
flush_scheduled_work();
for (x = 0; x < ARRAY_SIZE(wc->tspans); x++) {
if (!wc->tspans[x])
continue;
for (y = 0; y < ARRAY_SIZE(wc->tspans[x]->chans); y++) {
kfree(wc->tspans[x]->chans[y]);
kfree(wc->tspans[x]->ec[y]);
}
kfree(wc->tspans[x]);
}
kfree(wc->ddev->devicetype);
kfree(wc->ddev->location);
kfree(wc->ddev->hardware_id);
dahdi_free_device(wc->ddev);
kfree(wc);
}
/**
* t4_alloc_channels - Allocate the channels on a span.
* @wc: The board we're allocating for.
* @ts: The span we're allocating for.
* @linemode: Which mode (T1/E1/J1) to use for this span.
*
* This function must only be called before the span is assigned it's
* possible for user processes to have an open reference to the
* channels.
*
*/
static int t4_alloc_channels(struct t4 *wc, struct t4_span *ts,
enum linemode linemode)
{
int i;
if (test_bit(DAHDI_FLAGBIT_REGISTERED, &ts->span.flags)) {
dev_dbg(&wc->dev->dev,
"Cannot allocate channels on a span that is already "
"assigned.\n");
return -EINVAL;
}
/* Cleanup any previously allocated channels. */
for (i = 0; i < ARRAY_SIZE(ts->chans); ++i) {
kfree(ts->chans[i]);
kfree(ts->ec[i]);
ts->chans[i] = NULL;
ts->ec[i] = NULL;
}
ts->linemode = linemode;
for (i = 0; i < ((E1 == ts->linemode) ? 31 : 24); i++) {
struct dahdi_chan *chan;
struct dahdi_echocan_state *ec;
chan = kzalloc(sizeof(*chan), GFP_KERNEL);
if (!chan) {
free_wc(wc);
return -ENOMEM;
}
ts->chans[i] = chan;
ec = kzalloc(sizeof(*ec), GFP_KERNEL);
if (!ec) {
free_wc(wc);
return -ENOMEM;
}
ts->ec[i] = ec;
}
return 0;
}
static void t4_init_one_span(struct t4 *wc, struct t4_span *ts)
{
unsigned long flags;
unsigned int reg;
int i;
snprintf(ts->span.name, sizeof(ts->span.name) - 1,
"TE%d/%d/%d", wc->numspans, wc->num, ts->span.offset + 1);
snprintf(ts->span.desc, sizeof(ts->span.desc) - 1,
"T%dXXP (PCI) Card %d Span %d", wc->numspans, wc->num,
ts->span.offset + 1);
switch (ts->linemode) {
case T1:
ts->span.spantype = SPANTYPE_DIGITAL_T1;
break;
case E1:
ts->span.spantype = SPANTYPE_DIGITAL_E1;
break;
case J1:
ts->span.spantype = SPANTYPE_DIGITAL_J1;
break;
}
/* HDLC Specific init */
ts->sigchan = NULL;
ts->sigmode = sigmode;
ts->sigactive = 0;
if (E1 != ts->linemode) {
ts->span.channels = 24;
ts->span.deflaw = DAHDI_LAW_MULAW;
ts->span.linecompat = DAHDI_CONFIG_AMI |
DAHDI_CONFIG_B8ZS | DAHDI_CONFIG_D4 |
DAHDI_CONFIG_ESF;
} else {
ts->span.channels = 31;
ts->span.deflaw = DAHDI_LAW_ALAW;
ts->span.linecompat = DAHDI_CONFIG_AMI |
DAHDI_CONFIG_HDB3 | DAHDI_CONFIG_CCS |
DAHDI_CONFIG_CRC4;
}
ts->span.chans = ts->chans;
ts->span.flags = DAHDI_FLAG_RBS;
for (i = 0; i < ts->span.channels; i++) {
struct dahdi_chan *const chan = ts->chans[i];
chan->pvt = wc;
snprintf(chan->name, sizeof(chan->name) - 1,
"%s/%d", ts->span.name, i + 1);
t4_chan_set_sigcap(&ts->span, i);
chan->chanpos = i + 1;
}
/* Enable 1sec timer interrupt */
spin_lock_irqsave(&wc->reglock, flags);
reg = __t4_framer_in(wc, ts->span.offset, FMR1_T);
__t4_framer_out(wc, ts->span.offset, FMR1_T, (reg | FMR1_ECM));
/* Enable Errored Second interrupt */
__t4_framer_out(wc, ts->span.offset, ESM, 0);
spin_unlock_irqrestore(&wc->reglock, flags);
t4_reset_counters(&ts->span);
}
/**
* t4_set_linemode - Allows user space to change the linemode before spans are assigned.
* @span: span on which to change the linemode.
* @linemode: A value from enumerated spantypes
*
* This callback is used to override the E1/T1 mode jumper settings and set
* the linemode on for each span. Called when the "spantype" attribute
* is written in sysfs under the dahdi_device.
*
*/
static int t4_set_linemode(struct dahdi_span *span, enum spantypes linemode)
{
struct t4_span *ts = container_of(span, struct t4_span, span);
struct t4 *wc = ts->owner;
int res = 0;
enum linemode mode;
const char *old_name;
static DEFINE_MUTEX(linemode_lock);
unsigned long flags;
dev_dbg(&wc->dev->dev, "Setting '%s' to '%s'\n", span->name,
dahdi_spantype2str(linemode));
if (span->spantype == linemode)
return 0;
spin_lock_irqsave(&wc->reglock, flags);
wc->reset_required = 1;
spin_unlock_irqrestore(&wc->reglock, flags);
/* Do not allow the t1e1 member to be changed by multiple threads. */
mutex_lock(&linemode_lock);
old_name = dahdi_spantype2str(span->spantype);
switch (linemode) {
case SPANTYPE_DIGITAL_T1:
dev_info(&wc->dev->dev,
"Changing from %s to T1 line mode.\n", old_name);
mode = T1;
wc->t1e1 &= ~(1 << span->offset);
break;
case SPANTYPE_DIGITAL_E1:
dev_info(&wc->dev->dev,
"Changing from %s to E1 line mode.\n", old_name);
mode = E1;
wc->t1e1 |= (1 << span->offset);
break;
case SPANTYPE_DIGITAL_J1:
dev_info(&wc->dev->dev,
"Changing from %s to J1 line mode.\n", old_name);
mode = J1;
wc->t1e1 &= ~(1 << span->offset);
break;
default:
dev_err(&wc->dev->dev,
"Got invalid linemode %d from dahdi\n", linemode);
res = -EINVAL;
}
if (!res) {
t4_alloc_channels(wc, ts, mode);
t4_init_one_span(wc, ts);
dahdi_init_span(span);
}
mutex_unlock(&linemode_lock);
return res;
}
static const struct dahdi_span_ops t4_gen1_span_ops = {
.owner = THIS_MODULE,
.spanconfig = t4_spanconfig,
.chanconfig = t4_chanconfig,
.startup = t4_startup,
.shutdown = t4_shutdown,
.rbsbits = t4_rbsbits,
.maint = t4_maint,
.ioctl = t4_ioctl,
.hdlc_hard_xmit = t4_hdlc_hard_xmit,
.assigned = t4_span_assigned,
.set_spantype = t4_set_linemode,
};
static const struct dahdi_span_ops t4_gen2_span_ops = {
.owner = THIS_MODULE,
.spanconfig = t4_spanconfig,
.chanconfig = t4_chanconfig,
.startup = t4_startup,
.shutdown = t4_shutdown,
.rbsbits = t4_rbsbits,
.maint = t4_maint,
.ioctl = t4_ioctl,
.hdlc_hard_xmit = t4_hdlc_hard_xmit,
.dacs = t4_dacs,
.assigned = t4_span_assigned,
.set_spantype = t4_set_linemode,
#ifdef VPM_SUPPORT
.echocan_create = t4_echocan_create,
.echocan_name = t4_echocan_name,
#endif
};
/**
* init_spans - Do first initialization on all the spans
* @wc: Card to initialize the spans on.
*
* This function is called *before* the dahdi_device is first registered
* with the system. What happens in t4_init_one_span can happen between
* when the device is registered and when the spans are assigned via
* sysfs (or automatically).
*
*/
static void init_spans(struct t4 *wc)
{
int x, y;
int gen2;
struct t4_span *ts;
unsigned int reg;
unsigned long flags;
gen2 = (wc->tspans[0]->spanflags & FLAG_2NDGEN);
for (x = 0; x < wc->numspans; x++) {
ts = wc->tspans[x];
sprintf(ts->span.name, "TE%d/%d/%d", wc->numspans, wc->num, x + 1);
snprintf(ts->span.desc, sizeof(ts->span.desc) - 1,
"T%dXXP (PCI) Card %d Span %d", wc->numspans, wc->num, x+1);
switch (ts->linemode) {
case T1:
ts->span.spantype = SPANTYPE_DIGITAL_T1;
break;
case E1:
ts->span.spantype = SPANTYPE_DIGITAL_E1;
break;
case J1:
ts->span.spantype = SPANTYPE_DIGITAL_J1;
break;
}
/* HDLC Specific init */
ts->sigchan = NULL;
ts->sigmode = sigmode;
ts->sigactive = 0;
if (E1 != ts->linemode) {
ts->span.channels = 24;
ts->span.deflaw = DAHDI_LAW_MULAW;
ts->span.linecompat = DAHDI_CONFIG_AMI |
DAHDI_CONFIG_B8ZS | DAHDI_CONFIG_D4 |
DAHDI_CONFIG_ESF;
} else {
ts->span.channels = 31;
ts->span.deflaw = DAHDI_LAW_ALAW;
ts->span.linecompat = DAHDI_CONFIG_AMI |
DAHDI_CONFIG_HDB3 | DAHDI_CONFIG_CCS |
DAHDI_CONFIG_CRC4;
}
ts->span.chans = ts->chans;
ts->span.flags = DAHDI_FLAG_RBS;
ts->owner = wc;
ts->span.offset = x;
ts->writechunk = (void *)(wc->writechunk + x * 32 * 2);
ts->readchunk = (void *)(wc->readchunk + x * 32 * 2);
if (gen2) {
ts->span.ops = &t4_gen2_span_ops;
} else {
ts->span.ops = &t4_gen1_span_ops;
}
for (y=0;y<wc->tspans[x]->span.channels;y++) {
struct dahdi_chan *mychans = ts->chans[y];
sprintf(mychans->name, "TE%d/%d/%d/%d", wc->numspans, wc->num, x + 1, y + 1);
t4_chan_set_sigcap(&ts->span, x);
mychans->pvt = wc;
mychans->chanpos = y + 1;
}
/* Start checking for alarms in 250 ms */
ts->alarmcheck_time = jiffies + msecs_to_jiffies(250);
/* Enable 1sec timer interrupt */
spin_lock_irqsave(&wc->reglock, flags);
reg = __t4_framer_in(wc, x, FMR1_T);
__t4_framer_out(wc, x, FMR1_T, (reg | FMR1_ECM));
/* Enable Errored Second interrupt */
__t4_framer_out(wc, x, ESM, 0);
spin_unlock_irqrestore(&wc->reglock, flags);
t4_reset_counters(&ts->span);
}
set_span_devicetype(wc);
setup_chunks(wc, 0);
wc->lastindex = 0;
}
static int syncsrc = 0;
static int syncnum = 0 /* -1 */;
static int syncspan = 0;
static DEFINE_SPINLOCK(synclock);
static void __t4_set_rclk_src(struct t4 *wc, int span)
{
if (is_octal(wc)) {
int cmr5 = 0x00 | (span << 5);
int cmr1 = 0x38; /* Clock Mode: RCLK sourced by DCO-R1
by default, Disable Clock-Switching */
__t4_framer_out(wc, 0, 0x44, cmr1);
__t4_framer_out(wc, 0, FRMR_CMR5, cmr5);
} else {
int cmr1 = 0x38; /* Clock Mode: RCLK sourced by DCO-R1
by default, Disable Clock-Switching */
cmr1 |= (span << 6);
__t4_framer_out(wc, 0, 0x44, cmr1);
}
dev_info(&wc->dev->dev, "RCLK source set to span %d\n", span+1);
}
static void __t4_set_sclk_src(struct t4 *wc, int mode, int master, int slave)
{
if (slave) {
wc->dmactrl |= (1 << 25);
dev_info(&wc->dev->dev, "SCLK is slaved to timing cable\n");
} else {
wc->dmactrl &= ~(1 << 25);
}
if (master) {
wc->dmactrl |= (1 << 24);
dev_info(&wc->dev->dev, "SCLK is master to timing cable\n");
} else {
wc->dmactrl &= ~(1 << 24);
}
if (mode == WC_RECOVER)
wc->dmactrl |= (1 << 29); /* Recover timing from RCLK */
if (mode == WC_SELF)
wc->dmactrl &= ~(1 << 29);/* Provide timing from MCLK */
__t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
}
static ssize_t t4_timing_master_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct t4 *wc = dev_get_drvdata(dev);
if (wc->dmactrl & (1 << 29))
return sprintf(buf, "%d\n", wc->syncsrc);
else
return sprintf(buf, "%d\n", -1);
}
static DEVICE_ATTR(timing_master, 0400, t4_timing_master_show, NULL);
static void create_sysfs_files(struct t4 *wc)
{
int ret;
ret = device_create_file(&wc->dev->dev,
&dev_attr_timing_master);
if (ret) {
dev_info(&wc->dev->dev,
"Failed to create device attributes.\n");
}
}
static void remove_sysfs_files(struct t4 *wc)
{
device_remove_file(&wc->dev->dev,
&dev_attr_timing_master);
}
static inline void __t4_update_timing(struct t4 *wc)
{
int i;
/* update sync src info */
if (wc->syncsrc != syncsrc) {
dev_info(&wc->dev->dev, "Swapping card %d from %d to %d\n",
wc->num, wc->syncsrc, syncsrc);
wc->syncsrc = syncsrc;
/* Update sync sources */
for (i = 0; i < wc->numspans; i++) {
wc->tspans[i]->span.syncsrc = wc->syncsrc;
}
if (syncnum == wc->num) {
__t4_set_rclk_src(wc, syncspan-1);
__t4_set_sclk_src(wc, WC_RECOVER, 1, 0);
if (debug)
dev_notice(&wc->dev->dev, "Card %d, using sync "
"span %d, master\n", wc->num, syncspan);
} else {
__t4_set_sclk_src(wc, WC_RECOVER, 0, 1);
if (debug)
dev_notice(&wc->dev->dev, "Card %d, using "
"Timing Bus, NOT master\n", wc->num);
}
}
}
static int __t4_findsync(struct t4 *wc)
{
int i;
int x;
unsigned long flags;
int p;
int nonzero;
int newsyncsrc = 0; /* DAHDI span number */
int newsyncnum = 0; /* wct4xxp card number */
int newsyncspan = 0; /* span on given wct4xxp card */
spin_lock_irqsave(&synclock, flags);
if (!wc->num) {
/* If we're the first card, go through all the motions, up to 8 levels
of sync source */
p = 1;
while (p < 8) {
nonzero = 0;
for (x=0;cards[x];x++) {
for (i = 0; i < cards[x]->numspans; i++) {
if (cards[x]->tspans[i]->syncpos) {
nonzero = 1;
if ((cards[x]->tspans[i]->syncpos == p) &&
!(cards[x]->tspans[i]->span.alarms & (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE | DAHDI_ALARM_LOOPBACK)) &&
(cards[x]->tspans[i]->span.flags & DAHDI_FLAG_RUNNING)) {
/* This makes a good sync source */
newsyncsrc = cards[x]->tspans[i]->span.spanno;
newsyncnum = x;
newsyncspan = i + 1;
/* Jump out */
goto found;
}
}
}
}
if (nonzero)
p++;
else
break;
}
found:
if ((syncnum != newsyncnum) || (syncsrc != newsyncsrc) || (newsyncspan != syncspan)) {
if (debug)
dev_notice(&wc->dev->dev, "New syncnum: %d "
"(was %d), syncsrc: %d (was %d), "
"syncspan: %d (was %d)\n", newsyncnum,
syncnum, newsyncsrc, syncsrc,
newsyncspan, syncspan);
syncnum = newsyncnum;
syncsrc = newsyncsrc;
syncspan = newsyncspan;
for (x=0;cards[x];x++) {
__t4_update_timing(cards[x]);
}
}
}
__t4_update_timing(wc);
spin_unlock_irqrestore(&synclock, flags);
return 0;
}
static void __t4_set_timing_source_auto(struct t4 *wc)
{
int x, i;
int firstprio, secondprio;
firstprio = secondprio = 4;
if (debug)
dev_info(&wc->dev->dev, "timing source auto\n");
clear_bit(T4_CHECK_TIMING, &wc->checkflag);
if (timingcable) {
__t4_findsync(wc);
} else {
if (debug)
dev_info(&wc->dev->dev, "Evaluating spans for timing "
"source\n");
for (x=0;x<wc->numspans;x++) {
if ((wc->tspans[x]->span.flags & DAHDI_FLAG_RUNNING) &&
!(wc->tspans[x]->span.alarms & (DAHDI_ALARM_RED |
DAHDI_ALARM_BLUE))) {
if (debug)
dev_info(&wc->dev->dev, "span %d is "
"green : syncpos %d\n", x+1,
wc->tspans[x]->syncpos);
if (wc->tspans[x]->syncpos) {
/* Valid rsync source in recovered
timing mode */
if (firstprio == 4)
firstprio = x;
else if (wc->tspans[x]->syncpos <
wc->tspans[firstprio]->syncpos)
firstprio = x;
} else {
/* Valid rsync source in system timing
mode */
if (secondprio == 4)
secondprio = x;
}
}
}
if (firstprio != 4) {
wc->syncsrc = firstprio;
__t4_set_rclk_src(wc, firstprio);
__t4_set_sclk_src(wc, WC_RECOVER, 0, 0);
dev_info(&wc->dev->dev, "Recovered timing mode, "\
"RCLK set to span %d\n",
firstprio+1);
} else if (secondprio != 4) {
wc->syncsrc = -1;
__t4_set_rclk_src(wc, secondprio);
__t4_set_sclk_src(wc, WC_SELF, 0, 0);
dev_info(&wc->dev->dev, "System timing mode, "\
"RCLK set to span %d\n",
secondprio+1);
} else {
wc->syncsrc = -1;
dev_info(&wc->dev->dev, "All spans in alarm : No valid"\
"span to source RCLK from\n");
/* Default rclk to lock with span 1 */
__t4_set_rclk_src(wc, 0);
__t4_set_sclk_src(wc, WC_SELF, 0, 0);
}
/* Propagate sync selection to dahdi_span struct
* this is read by dahdi_tool to display the span's
* master/slave sync information */
for (i = 0; i < wc->numspans; i++) {
wc->tspans[i]->span.syncsrc = wc->syncsrc + 1;
}
}
}
static void __t4_configure_t1(struct t4 *wc, int unit, int lineconfig, int txlevel)
{
unsigned int fmr4, fmr2, fmr1, fmr0, lim2;
char *framing, *line;
int mytxlevel;
if ((txlevel > 7) || (txlevel < 4))
mytxlevel = 0;
else
mytxlevel = txlevel - 4;
if (is_octal(wc))
fmr1 = 0x9c | 0x02; /* FMR1: Mode 1, T1 mode, CRC on for ESF, 8.192 Mhz system data rate, no XAIS */
else
fmr1 = 0x9c; /* FMR1: Mode 1, T1 mode, CRC on for ESF, 8.192 Mhz system data rate, no XAIS */
fmr2 = 0x20; /* FMR2: no payload loopback, don't auto yellow */
fmr4 = 0x0c; /* FMR4: Lose sync on 2 out of 5 framing bits, auto resync */
lim2 = 0x21; /* LIM2: 50% peak is a "1", Advanced Loss recovery */
lim2 |= (mytxlevel << 6); /* LIM2: Add line buildout */
__t4_framer_out(wc, unit, 0x1d, fmr1);
__t4_framer_out(wc, unit, 0x1e, fmr2);
/* Configure line interface */
if (lineconfig & DAHDI_CONFIG_AMI) {
line = "AMI";
/* workaround for errata #2 in ES v3 09-10-16 */
fmr0 = (is_octal(wc) || wc->falc31) ? 0xb0 : 0xa0;
} else {
line = "B8ZS";
fmr0 = 0xf0;
}
if (lineconfig & DAHDI_CONFIG_D4) {
framing = "D4";
} else {
framing = "ESF";
fmr4 |= 0x2;
fmr2 |= 0xc0;
}
__t4_framer_out(wc, unit, 0x1c, fmr0);
__t4_framer_out(wc, unit, 0x20, fmr4);
__t4_framer_out(wc, unit, FMR5, FMR5_EIBR); /* FMR5: Enable RBS mode */
__t4_framer_out(wc, unit, 0x37, 0xf0 ); /* LIM1: Clear data in case of LOS, Set receiver threshold (0.5V), No remote loop, no DRS */
__t4_framer_out(wc, unit, 0x36, 0x08); /* LIM0: Enable auto long haul mode, no local loop (must be after LIM1) */
__t4_framer_out(wc, unit, 0x02, 0x50); /* CMDR: Reset the receiver and transmitter line interface */
__t4_framer_out(wc, unit, 0x02, 0x00); /* CMDR: Reset the receiver and transmitter line interface */
if (wc->falc31) {
if (debug)
dev_info(&wc->dev->dev, "card %d span %d: setting Rtx "
"to 0ohm for T1\n", wc->num, unit);
__t4_framer_out(wc, unit, 0x86, 0x00); /* PC6: set Rtx to 0ohm for T1 */
// Hitting the bugfix register to fix errata #3
__t4_framer_out(wc, unit, 0xbd, 0x05);
}
__t4_framer_out(wc, unit, 0x3a, lim2); /* LIM2: 50% peak amplitude is a "1" */
__t4_framer_out(wc, unit, 0x38, 0x0a); /* PCD: LOS after 176 consecutive "zeros" */
__t4_framer_out(wc, unit, 0x39, 0x15); /* PCR: 22 "ones" clear LOS */
/* Generate pulse mask for T1 */
switch(mytxlevel) {
case 3:
__t4_framer_out(wc, unit, 0x26, 0x07); /* XPM0 */
__t4_framer_out(wc, unit, 0x27, 0x01); /* XPM1 */
__t4_framer_out(wc, unit, 0x28, 0x00); /* XPM2 */
break;
case 2:
__t4_framer_out(wc, unit, 0x26, 0x8c); /* XPM0 */
__t4_framer_out(wc, unit, 0x27, 0x11); /* XPM1 */
__t4_framer_out(wc, unit, 0x28, 0x01); /* XPM2 */
break;
case 1:
__t4_framer_out(wc, unit, 0x26, 0x8c); /* XPM0 */
__t4_framer_out(wc, unit, 0x27, 0x01); /* XPM1 */
__t4_framer_out(wc, unit, 0x28, 0x00); /* XPM2 */
break;
case 0:
default:
__t4_framer_out(wc, unit, 0x26, 0xd7); /* XPM0 */
__t4_framer_out(wc, unit, 0x27, 0x22); /* XPM1 */
__t4_framer_out(wc, unit, 0x28, 0x01); /* XPM2 */
break;
}
/* Don't mask framer interrupts if hardware HDLC is in use */
__t4_framer_out(wc, unit, FRMR_IMR0, 0xff & ~((wc->tspans[unit]->sigchan) ? HDLC_IMR0_MASK : 0)); /* IMR0: We care about CAS changes, etc */
__t4_framer_out(wc, unit, FRMR_IMR1, 0xff & ~((wc->tspans[unit]->sigchan) ? HDLC_IMR1_MASK : 0)); /* IMR1: We care about nothing */
__t4_framer_out(wc, unit, 0x16, 0x00); /* IMR2: All the alarm stuff! */
__t4_framer_out(wc, unit, 0x17, 0x34); /* IMR3: AIS and friends */
__t4_framer_out(wc, unit, 0x18, 0x3f); /* IMR4: Slips on transmit */
dev_info(&wc->dev->dev, "Span %d configured for %s/%s\n", unit + 1,
framing, line);
}
static void __t4_configure_e1(struct t4 *wc, int unit, int lineconfig)
{
unsigned int fmr2, fmr1, fmr0;
unsigned int cas = 0;
unsigned int imr3extra=0;
char *crc4 = "";
char *framing, *line;
if (is_octal(wc)) {
/* 16 MHz */
fmr1 = 0x44 | 0x02; /* FMR1: E1 mode, Automatic force resync, PCM30 mode, 8.192 Mhz backplane, no XAIS */
} else {
/* 8 MHz */
fmr1 = 0x44; /* FMR1: E1 mode, Automatic force resync, PCM30 mode, 8.192 Mhz backplane, no XAIS */
}
fmr2 = 0x03; /* FMR2: Auto transmit remote alarm, auto loss of multiframe recovery, no payload loopback */
if (lineconfig & DAHDI_CONFIG_CRC4) {
fmr1 |= 0x08; /* CRC4 transmit */
fmr2 |= 0xc0; /* CRC4 receive */
crc4 = "/CRC4";
}
__t4_framer_out(wc, unit, 0x1d, fmr1);
__t4_framer_out(wc, unit, 0x1e, fmr2);
/* Configure line interface */
if (lineconfig & DAHDI_CONFIG_AMI) {
line = "AMI";
/* workaround for errata #2 in ES v3 09-10-16 */
fmr0 = (is_octal(wc) || wc->falc31) ? 0xb0 : 0xa0;
} else {
line = "HDB3";
fmr0 = 0xf0;
}
if (lineconfig & DAHDI_CONFIG_CCS) {
framing = "CCS";
imr3extra = 0x28;
} else {
framing = "CAS";
cas = 0x40;
}
__t4_framer_out(wc, unit, 0x1c, fmr0);
__t4_framer_out(wc, unit, 0x37, 0xf0 /*| 0x6 */ ); /* LIM1: Clear data in case of LOS, Set receiver threshold (0.5V), No remote loop, no DRS */
__t4_framer_out(wc, unit, 0x36, 0x08); /* LIM0: Enable auto long haul mode, no local loop (must be after LIM1) */
__t4_framer_out(wc, unit, 0x02, 0x50); /* CMDR: Reset the receiver and transmitter line interface */
__t4_framer_out(wc, unit, 0x02, 0x00); /* CMDR: Reset the receiver and transmitter line interface */
if (wc->falc31) {
if (debug)
dev_info(&wc->dev->dev,
"setting Rtx to 7.5ohm for E1\n");
__t4_framer_out(wc, unit, 0x86, 0x40); /* PC6: turn on 7.5ohm Rtx for E1 */
}
/* Condition receive line interface for E1 after reset */
__t4_framer_out(wc, unit, 0xbb, 0x17);
__t4_framer_out(wc, unit, 0xbc, 0x55);
__t4_framer_out(wc, unit, 0xbb, 0x97);
__t4_framer_out(wc, unit, 0xbb, 0x11);
__t4_framer_out(wc, unit, 0xbc, 0xaa);
__t4_framer_out(wc, unit, 0xbb, 0x91);
__t4_framer_out(wc, unit, 0xbb, 0x12);
__t4_framer_out(wc, unit, 0xbc, 0x55);
__t4_framer_out(wc, unit, 0xbb, 0x92);
__t4_framer_out(wc, unit, 0xbb, 0x0c);
__t4_framer_out(wc, unit, 0xbb, 0x00);
__t4_framer_out(wc, unit, 0xbb, 0x8c);
__t4_framer_out(wc, unit, 0x3a, 0x20); /* LIM2: 50% peak amplitude is a "1" */
__t4_framer_out(wc, unit, 0x38, 0x0a); /* PCD: LOS after 176 consecutive "zeros" */
__t4_framer_out(wc, unit, 0x39, 0x15); /* PCR: 22 "ones" clear LOS */
__t4_framer_out(wc, unit, 0x20, 0x9f); /* XSW: Spare bits all to 1 */
__t4_framer_out(wc, unit, 0x21, 0x1c|cas); /* XSP: E-bit set when async. AXS auto, XSIF to 1 */
/* Generate pulse mask for E1 */
__t4_framer_out(wc, unit, 0x26, 0x54); /* XPM0 */
__t4_framer_out(wc, unit, 0x27, 0x02); /* XPM1 */
__t4_framer_out(wc, unit, 0x28, 0x00); /* XPM2 */
/* Don't mask framer interrupts if hardware HDLC is in use */
__t4_framer_out(wc, unit, FRMR_IMR0, 0xff & ~((wc->tspans[unit]->sigchan) ? HDLC_IMR0_MASK : 0)); /* IMR0: We care about CRC errors, CAS changes, etc */
__t4_framer_out(wc, unit, FRMR_IMR1, 0x3f & ~((wc->tspans[unit]->sigchan) ? HDLC_IMR1_MASK : 0)); /* IMR1: We care about loopup / loopdown */
__t4_framer_out(wc, unit, 0x16, 0x00); /* IMR2: We care about all the alarm stuff! */
__t4_framer_out(wc, unit, 0x17, 0x04 | imr3extra); /* IMR3: AIS */
__t4_framer_out(wc, unit, 0x18, 0x3f); /* IMR4: We care about slips on transmit */
__t4_framer_out(wc, unit, 0x2f, 0x00);
__t4_framer_out(wc, unit, 0x30, 0x00);
__t4_framer_out(wc, unit, 0x31, 0x00);
dev_info(&wc->dev->dev, "TE%dXXP: Span %d configured for %s/%s%s\n",
wc->numspans, unit + 1, framing, line, crc4);
}
/**
* t4_check_for_interrupts - Return 0 if the card is generating interrupts.
* @wc: The card to check.
*
* If the card is not generating interrupts, this function will also place all
* the spans on the card into red alarm.
*
*/
static int t4_check_for_interrupts(struct t4 *wc)
{
unsigned int starting_intcount = wc->intcount;
unsigned long stop_time = jiffies + HZ*2;
unsigned long flags;
int x;
msleep(20);
spin_lock_irqsave(&wc->reglock, flags);
while (starting_intcount == wc->intcount) {
spin_unlock_irqrestore(&wc->reglock, flags);
if (time_after(jiffies, stop_time)) {
for (x = 0; x < wc->numspans; x++)
wc->tspans[x]->span.alarms = DAHDI_ALARM_RED;
dev_err(&wc->dev->dev, "Interrupts not detected.\n");
return -EIO;
}
msleep(100);
spin_lock_irqsave(&wc->reglock, flags);
}
spin_unlock_irqrestore(&wc->reglock, flags);
return 0;
}
static int _t4_startup(struct file *file, struct dahdi_span *span)
{
#ifdef SUPPORT_GEN1
int i;
#endif
int tspan;
unsigned long flags;
int alreadyrunning;
struct t4_span *ts = container_of(span, struct t4_span, span);
struct t4 *wc = ts->owner;
set_bit(T4_IGNORE_LATENCY, &wc->checkflag);
if (debug)
dev_info(&wc->dev->dev, "About to enter startup!\n");
tspan = span->offset + 1;
if (tspan < 0) {
dev_info(&wc->dev->dev, "TE%dXXP: Span '%d' isn't us?\n",
wc->numspans, span->spanno);
return -1;
}
spin_lock_irqsave(&wc->reglock, flags);
alreadyrunning = span->flags & DAHDI_FLAG_RUNNING;
#ifdef SUPPORT_GEN1
/* initialize the start value for the entire chunk of last ec buffer */
for(i = 0; i < span->channels; i++)
{
memset(ts->ec_chunk1[i],
DAHDI_LIN2X(0,span->chans[i]),DAHDI_CHUNKSIZE);
memset(ts->ec_chunk2[i],
DAHDI_LIN2X(0,span->chans[i]),DAHDI_CHUNKSIZE);
}
#endif
/* Force re-evaluation of timing source */
wc->syncsrc = -1;
set_bit(T4_CHECK_TIMING, &wc->checkflag);
if (E1 == ts->linemode)
__t4_configure_e1(wc, span->offset, span->lineconfig);
else
__t4_configure_t1(wc, span->offset, span->lineconfig, span->txlevel);
/* Note clear channel status */
wc->tspans[span->offset]->notclear = 0;
__set_clear(wc, span->offset);
if (!alreadyrunning) {
span->flags |= DAHDI_FLAG_RUNNING;
wc->spansstarted++;
if (wc->devtype->flags & FLAG_5THGEN)
__t4_pci_out(wc, 5, (ms_per_irq << 16) | wc->numbufs);
else
__t4_pci_out(wc, 5, (1 << 16) | 1);
/* enable interrupts */
/* Start DMA, enabling DMA interrupts on read only */
wc->dmactrl |= (ts->spanflags & FLAG_2NDGEN) ? 0xc0000000 : 0xc0000003;
#ifdef VPM_SUPPORT
wc->dmactrl |= (wc->vpm) ? T4_VPM_PRESENT : 0;
#endif
/* Seed interrupt register */
__t4_pci_out(wc, WC_INTR, 0x0c);
if (noburst || !(ts->spanflags & FLAG_BURST))
wc->dmactrl |= (1 << 26);
else
wc->dmactrl &= ~(1 << 26);
__t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
/* Startup HDLC controller too */
}
if (ts->sigchan) {
struct dahdi_chan *sigchan = ts->sigchan;
spin_unlock_irqrestore(&wc->reglock, flags);
if (hdlc_start(wc, span->offset, sigchan, ts->sigmode)) {
dev_notice(&wc->dev->dev, "Error initializing "
"signalling controller\n");
return -1;
}
spin_lock_irqsave(&wc->reglock, flags);
}
spin_unlock_irqrestore(&wc->reglock, flags);
local_irq_save(flags);
t4_check_alarms(wc, span->offset);
t4_check_sigbits(wc, span->offset);
local_irq_restore(flags);
if (wc->tspans[0]->sync == span->spanno)
dev_info(&wc->dev->dev, "SPAN %d: Primary Sync Source\n",
span->spanno);
if (wc->tspans[1]->sync == span->spanno)
dev_info(&wc->dev->dev, "SPAN %d: Secondary Sync Source\n",
span->spanno);
if (wc->numspans >= 4) {
if (wc->tspans[2]->sync == span->spanno)
dev_info(&wc->dev->dev, "SPAN %d: Tertiary Sync Source"
"\n", span->spanno);
if (wc->tspans[3]->sync == span->spanno)
dev_info(&wc->dev->dev, "SPAN %d: Quaternary Sync "
"Source\n", span->spanno);
}
if (wc->numspans == 8) {
if (wc->tspans[4]->sync == span->spanno)
dev_info(&wc->dev->dev, "SPAN %d: Quinary Sync "
"Source\n", span->spanno);
if (wc->tspans[5]->sync == span->spanno)
dev_info(&wc->dev->dev, "SPAN %d: Senary Sync "
"Source\n", span->spanno);
if (wc->tspans[6]->sync == span->spanno)
dev_info(&wc->dev->dev, "SPAN %d: Septenary Sync "
"Source\n", span->spanno);
if (wc->tspans[7]->sync == span->spanno)
dev_info(&wc->dev->dev, "SPAN %d: Octonary Sync "
"Source\n", span->spanno);
}
if (!alreadyrunning) {
if (t4_check_for_interrupts(wc))
return -EIO;
}
if (debug)
dev_info(&wc->dev->dev, "Completed startup!\n");
clear_bit(T4_IGNORE_LATENCY, &wc->checkflag);
return 0;
}
static int t4_startup(struct file *file, struct dahdi_span *span)
{
int ret;
struct dahdi_device *const ddev = span->parent;
struct dahdi_span *s;
ret = _t4_startup(file, span);
list_for_each_entry(s, &ddev->spans, device_node) {
if (!test_bit(DAHDI_FLAGBIT_RUNNING, &s->flags)) {
_t4_startup(file, s);
}
}
return ret;
}
#ifdef SUPPORT_GEN1
static inline void e1_check(struct t4 *wc, int span, int val)
{
struct t4_span *ts = wc->tspans[span];
if ((ts->span.channels > 24) &&
(ts->span.flags & DAHDI_FLAG_RUNNING) &&
!(ts->span.alarms) &&
(!wc->e1recover)) {
if (val != 0x1b) {
ts->e1check++;
} else
ts->e1check = 0;
if (ts->e1check > 100) {
/* Wait 1000 ms */
wc->e1recover = 1000 * 8;
wc->tspans[0]->e1check = wc->tspans[1]->e1check = 0;
if (wc->numspans == 4)
wc->tspans[2]->e1check = wc->tspans[3]->e1check = 0;
if (debug & DEBUG_MAIN)
dev_notice(&wc->dev->dev, "Detected loss of "
"E1 alignment on span %d!\n", span);
t4_reset_dma(wc);
}
}
}
static void t4_receiveprep(struct t4 *wc, int irq)
{
unsigned int *readchunk;
int dbl = 0;
int x,y,z;
unsigned int tmp;
int offset=0;
if (!has_e1_span(wc))
offset = 4;
if (irq & 1) {
/* First part */
readchunk = wc->readchunk;
if (!wc->last0)
dbl = 1;
wc->last0 = 0;
} else {
readchunk = wc->readchunk + DAHDI_CHUNKSIZE * 32;
if (wc->last0)
dbl = 1;
wc->last0 = 1;
}
if (unlikely(dbl && (debug & DEBUG_MAIN)))
dev_notice(&wc->dev->dev, "Double/missed interrupt detected\n");
for (x=0;x<DAHDI_CHUNKSIZE;x++) {
for (z=0;z<24;z++) {
/* All T1/E1 channels */
tmp = readchunk[z+1+offset];
if (wc->numspans == 4) {
wc->tspans[3]->span.chans[z]->readchunk[x] = tmp & 0xff;
wc->tspans[2]->span.chans[z]->readchunk[x] = (tmp & 0xff00) >> 8;
}
wc->tspans[1]->span.chans[z]->readchunk[x] = (tmp & 0xff0000) >> 16;
wc->tspans[0]->span.chans[z]->readchunk[x] = tmp >> 24;
}
if (has_e1_span(wc)) {
if (wc->e1recover > 0)
wc->e1recover--;
tmp = readchunk[0];
if (wc->numspans == 4) {
e1_check(wc, 3, (tmp & 0x7f));
e1_check(wc, 2, (tmp & 0x7f00) >> 8);
}
e1_check(wc, 1, (tmp & 0x7f0000) >> 16);
e1_check(wc, 0, (tmp & 0x7f000000) >> 24);
for (z=24;z<31;z++) {
/* Only E1 channels now */
tmp = readchunk[z+1];
if (wc->numspans == 4) {
if (wc->tspans[3]->span.channels > 24)
wc->tspans[3]->span.chans[z]->readchunk[x] = tmp & 0xff;
if (wc->tspans[2]->span.channels > 24)
wc->tspans[2]->span.chans[z]->readchunk[x] = (tmp & 0xff00) >> 8;
}
if (wc->tspans[1]->span.channels > 24)
wc->tspans[1]->span.chans[z]->readchunk[x] = (tmp & 0xff0000) >> 16;
if (wc->tspans[0]->span.channels > 24)
wc->tspans[0]->span.chans[z]->readchunk[x] = tmp >> 24;
}
}
/* Advance pointer by 4 TDM frame lengths */
readchunk += 32;
}
for (x=0;x<wc->numspans;x++) {
if (wc->tspans[x]->span.flags & DAHDI_FLAG_RUNNING) {
for (y=0;y<wc->tspans[x]->span.channels;y++) {
/* Echo cancel double buffered data */
dahdi_ec_chunk(wc->tspans[x]->span.chans[y],
wc->tspans[x]->span.chans[y]->readchunk,
wc->tspans[x]->ec_chunk2[y]);
memcpy(wc->tspans[x]->ec_chunk2[y],wc->tspans[x]->ec_chunk1[y],
DAHDI_CHUNKSIZE);
memcpy(wc->tspans[x]->ec_chunk1[y],
wc->tspans[x]->span.chans[y]->writechunk,
DAHDI_CHUNKSIZE);
}
_dahdi_receive(&wc->tspans[x]->span);
}
}
}
#endif
#if (DAHDI_CHUNKSIZE != 8)
#error Sorry, nextgen does not support chunksize != 8
#endif
static void __receive_span(struct t4_span *ts)
{
#ifdef VPM_SUPPORT
int y;
unsigned long merged;
merged = ts->dtmfactive & ts->dtmfmutemask;
if (merged) {
for (y=0;y<ts->span.channels;y++) {
/* Mute any DTMFs which are supposed to be muted */
if (test_bit(y, &merged)) {
memset(ts->span.chans[y]->readchunk, DAHDI_XLAW(0, ts->span.chans[y]), DAHDI_CHUNKSIZE);
}
}
}
#endif
_dahdi_ec_span(&ts->span);
_dahdi_receive(&ts->span);
}
static inline void __transmit_span(struct t4_span *ts)
{
_dahdi_transmit(&ts->span);
}
#ifdef ENABLE_WORKQUEUES
static void workq_handlespan(void *data)
{
struct t4_span *ts = data;
struct t4 *wc = ts->owner;
__receive_span(ts);
__transmit_span(ts);
atomic_dec(&wc->worklist);
if (!atomic_read(&wc->worklist))
t4_pci_out(wc, WC_INTR, 0);
}
#else
static void t4_prep_gen2(struct t4 *wc)
{
int x;
for (x=0;x<wc->numspans;x++) {
if (wc->tspans[x]->span.flags & DAHDI_FLAG_RUNNING) {
__receive_span(wc->tspans[x]);
__transmit_span(wc->tspans[x]);
}
}
}
#endif
#ifdef SUPPORT_GEN1
static void t4_transmitprep(struct t4 *wc, int irq)
{
u32 *writechunk;
int x, y, z;
unsigned int tmp;
int offset = 0;
if (!has_e1_span(wc))
offset = 4;
if (irq & 1) {
/* First part */
writechunk = wc->writechunk + 1;
} else {
writechunk = wc->writechunk + DAHDI_CHUNKSIZE * 32 + 1;
}
for (y=0;y<wc->numspans;y++) {
if (wc->tspans[y]->span.flags & DAHDI_FLAG_RUNNING)
_dahdi_transmit(&wc->tspans[y]->span);
}
for (x=0;x<DAHDI_CHUNKSIZE;x++) {
/* Once per chunk */
for (z=0;z<24;z++) {
/* All T1/E1 channels */
tmp = (wc->tspans[3]->span.chans[z]->writechunk[x]) |
(wc->tspans[2]->span.chans[z]->writechunk[x] << 8) |
(wc->tspans[1]->span.chans[z]->writechunk[x] << 16) |
(wc->tspans[0]->span.chans[z]->writechunk[x] << 24);
writechunk[z+offset] = tmp;
}
if (has_e1_span(wc)) {
for (z=24;z<31;z++) {
/* Only E1 channels now */
tmp = 0;
if (wc->numspans == 4) {
if (wc->tspans[3]->span.channels > 24)
tmp |= wc->tspans[3]->span.chans[z]->writechunk[x];
if (wc->tspans[2]->span.channels > 24)
tmp |= (wc->tspans[2]->span.chans[z]->writechunk[x] << 8);
}
if (wc->tspans[1]->span.channels > 24)
tmp |= (wc->tspans[1]->span.chans[z]->writechunk[x] << 16);
if (wc->tspans[0]->span.channels > 24)
tmp |= (wc->tspans[0]->span.chans[z]->writechunk[x] << 24);
writechunk[z] = tmp;
}
}
/* Advance pointer by 4 TDM frame lengths */
writechunk += 32;
}
}
#endif
static void t4_dahdi_rbsbits(struct dahdi_chan *const chan, int rxs)
{
if ((debug & DEBUG_RBS) && printk_ratelimit()) {
const struct t4_span *tspan = container_of(chan->span,
struct t4_span,
span);
const struct t4 *const wc = tspan->owner;
dev_notice(&wc->dev->dev, "Detected sigbits change on " \
"channel %s to %04x\n", chan->name, rxs);
}
dahdi_rbsbits(chan, rxs);
}
static void t4_check_sigbits(struct t4 *wc, int span)
{
int a,i,rxs;
struct t4_span *ts = wc->tspans[span];
if (debug & DEBUG_RBS)
dev_notice(&wc->dev->dev, "Checking sigbits on span %d\n",
span + 1);
if (E1 == ts->linemode) {
for (i = 0; i < 15; i++) {
a = t4_framer_in(wc, span, 0x71 + i);
/* Get high channel in low bits */
rxs = (a & 0xf);
if (!(ts->span.chans[i+16]->sig & DAHDI_SIG_CLEAR)) {
if (ts->span.chans[i+16]->rxsig != rxs)
t4_dahdi_rbsbits(ts->span.chans[i+16], rxs);
}
rxs = (a >> 4) & 0xf;
if (!(ts->span.chans[i]->sig & DAHDI_SIG_CLEAR)) {
if (ts->span.chans[i]->rxsig != rxs)
t4_dahdi_rbsbits(ts->span.chans[i], rxs);
}
}
} else if (ts->span.lineconfig & DAHDI_CONFIG_D4) {
for (i = 0; i < 24; i+=4) {
a = t4_framer_in(wc, span, 0x70 + (i>>2));
/* Get high channel in low bits */
rxs = (a & 0x3) << 2;
if (!(ts->span.chans[i+3]->sig & DAHDI_SIG_CLEAR)) {
if (ts->span.chans[i+3]->rxsig != rxs)
t4_dahdi_rbsbits(ts->span.chans[i+3], rxs);
}
rxs = (a & 0xc);
if (!(ts->span.chans[i+2]->sig & DAHDI_SIG_CLEAR)) {
if (ts->span.chans[i+2]->rxsig != rxs)
t4_dahdi_rbsbits(ts->span.chans[i+2], rxs);
}
rxs = (a >> 2) & 0xc;
if (!(ts->span.chans[i+1]->sig & DAHDI_SIG_CLEAR)) {
if (ts->span.chans[i+1]->rxsig != rxs)
t4_dahdi_rbsbits(ts->span.chans[i+1], rxs);
}
rxs = (a >> 4) & 0xc;
if (!(ts->span.chans[i]->sig & DAHDI_SIG_CLEAR)) {
if (ts->span.chans[i]->rxsig != rxs)
t4_dahdi_rbsbits(ts->span.chans[i], rxs);
}
}
} else {
for (i = 0; i < 24; i+=2) {
a = t4_framer_in(wc, span, 0x70 + (i>>1));
/* Get high channel in low bits */
rxs = (a & 0xf);
if (!(ts->span.chans[i+1]->sig & DAHDI_SIG_CLEAR)) {
/* XXX Not really reset on every trans! XXX */
if (ts->span.chans[i+1]->rxsig != rxs) {
t4_dahdi_rbsbits(ts->span.chans[i+1], rxs);
}
}
rxs = (a >> 4) & 0xf;
if (!(ts->span.chans[i]->sig & DAHDI_SIG_CLEAR)) {
/* XXX Not really reset on every trans! XXX */
if (ts->span.chans[i]->rxsig != rxs) {
t4_dahdi_rbsbits(ts->span.chans[i], rxs);
}
}
}
}
}
/* Must be called from within hardirq context. */
static void t4_check_alarms(struct t4 *wc, int span)
{
unsigned char c, d, e;
int alarms;
int x,j;
struct t4_span *ts = wc->tspans[span];
if (time_before(jiffies, ts->alarmcheck_time))
return;
if (!(ts->span.flags & DAHDI_FLAG_RUNNING))
return;
spin_lock(&wc->reglock);
c = __t4_framer_in(wc, span, 0x4c);
d = __t4_framer_in(wc, span, 0x4d);
/* Assume no alarms */
alarms = 0;
/* And consider only carrier alarms */
ts->span.alarms &= (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE | DAHDI_ALARM_NOTOPEN);
if (E1 == ts->linemode) {
if (c & 0x04) {
/* No multiframe found, force RAI high after 400ms only if
we haven't found a multiframe since last loss
of frame */
if (!(ts->spanflags & FLAG_NMF)) {
__t4_framer_out(wc, span, 0x20, 0x9f | 0x20); /* LIM0: Force RAI High */
ts->spanflags |= FLAG_NMF;
dev_notice(&wc->dev->dev,
"Lost crc4-multiframe alignment\n");
}
__t4_framer_out(wc, span, 0x1e, 0xc3); /* Reset to CRC4 mode */
__t4_framer_out(wc, span, 0x1c, 0xf2); /* Force Resync */
__t4_framer_out(wc, span, 0x1c, 0xf0); /* Force Resync */
} else if (!(c & 0x02)) {
if ((ts->spanflags & FLAG_NMF)) {
__t4_framer_out(wc, span, 0x20, 0x9f); /* LIM0: Clear forced RAI */
ts->spanflags &= ~FLAG_NMF;
dev_notice(&wc->dev->dev,
"Obtained crc4-multiframe alignment\n");
}
}
} else {
/* Detect loopup code if we're not sending one */
if ((!ts->span.mainttimer) && (d & 0x08)) {
/* Loop-up code detected */
if ((ts->loopupcnt++ > 80) && (ts->span.maintstat != DAHDI_MAINT_REMOTELOOP)) {
dev_notice(&wc->dev->dev,
"span %d: Loopup detected,"\
" enabling remote loop\n",
span+1);
__t4_framer_out(wc, span, 0x36, 0x08); /* LIM0: Disable any local loop */
__t4_framer_out(wc, span, 0x37, 0xf6 ); /* LIM1: Enable remote loop */
ts->span.maintstat = DAHDI_MAINT_REMOTELOOP;
}
} else
ts->loopupcnt = 0;
/* Same for loopdown code */
if ((!ts->span.mainttimer) && (d & 0x10)) {
/* Loop-down code detected */
if ((ts->loopdowncnt++ > 80) && (ts->span.maintstat == DAHDI_MAINT_REMOTELOOP)) {
dev_notice(&wc->dev->dev,
"span %d: Loopdown detected,"\
" disabling remote loop\n",
span+1);
__t4_framer_out(wc, span, 0x36, 0x08); /* LIM0: Disable any local loop */
__t4_framer_out(wc, span, 0x37, 0xf0 ); /* LIM1: Disable remote loop */
ts->span.maintstat = DAHDI_MAINT_NONE;
}
} else
ts->loopdowncnt = 0;
}
if (ts->span.lineconfig & DAHDI_CONFIG_NOTOPEN) {
for (x=0,j=0;x < ts->span.channels;x++)
if ((ts->span.chans[x]->flags & DAHDI_FLAG_OPEN) ||
dahdi_have_netdev(ts->span.chans[x]))
j++;
if (!j)
alarms |= DAHDI_ALARM_NOTOPEN;
}
/* Loss of Frame Alignment */
if (c & 0x20) {
if (!ts->alarm_time) {
if (unlikely(debug)) {
/* starting to debounce LOF/LFA */
dev_info(&wc->dev->dev, "wct%dxxp: LOF/LFA "
"detected on span %d but debouncing "
"for %d ms\n", wc->numspans, span + 1,
alarmdebounce);
}
ts->alarm_time = jiffies +
msecs_to_jiffies(alarmdebounce);
} else if (time_after(jiffies, ts->alarm_time)) {
/* Disable Slip Interrupts */
e = __t4_framer_in(wc, span, 0x17);
__t4_framer_out(wc, span, 0x17, (e|0x03));
alarms |= DAHDI_ALARM_RED;
}
} else {
ts->alarm_time = 0;
}
/* Loss of Signal */
if (c & 0x80) {
if (!ts->losalarm_time) {
if (unlikely(debug)) {
/* starting to debounce LOS */
dev_info(&wc->dev->dev, "wct%dxxp: LOS "
"detected on span %d but debouncing "
"for %d ms\n", wc->numspans,
span + 1, losalarmdebounce);
}
ts->losalarm_time = jiffies +
msecs_to_jiffies(losalarmdebounce);
} else if (time_after(jiffies, ts->losalarm_time)) {
/* Disable Slip Interrupts */
e = __t4_framer_in(wc, span, 0x17);
__t4_framer_out(wc, span, 0x17, (e|0x03));
alarms |= DAHDI_ALARM_RED;
}
} else {
ts->losalarm_time = 0;
}
/* Alarm Indication Signal */
if (c & 0x40) {
if (!ts->aisalarm_time) {
if (unlikely(debug)) {
/* starting to debounce AIS */
dev_info(&wc->dev->dev, "wct%dxxp: AIS "
"detected on span %d but debouncing "
"for %d ms\n", wc->numspans,
span + 1, aisalarmdebounce);
}
ts->aisalarm_time = jiffies +
msecs_to_jiffies(aisalarmdebounce);
} else if (time_after(jiffies, ts->aisalarm_time)) {
alarms |= DAHDI_ALARM_BLUE;
}
} else {
ts->aisalarm_time = 0;
}
/* Add detailed alarm status information to a red alarm state */
if (alarms & DAHDI_ALARM_RED) {
if (c & FRS0_LOS)
alarms |= DAHDI_ALARM_LOS;
if (c & FRS0_LFA)
alarms |= DAHDI_ALARM_LFA;
if (c & FRS0_LMFA)
alarms |= DAHDI_ALARM_LMFA;
}
if (unlikely(debug)) {
/* Check to ensure the xmit line isn't shorted */
if (unlikely(d & FRS1_XLS)) {
dev_info(&wc->dev->dev,
"Detected a possible hardware malfunction"\
" this card may need servicing\n");
}
}
if (((!ts->span.alarms) && alarms) ||
(ts->span.alarms && (!alarms)))
set_bit(T4_CHECK_TIMING, &wc->checkflag);
/* Keep track of recovering */
if ((!alarms) && ts->span.alarms)
ts->alarmtimer = DAHDI_ALARMSETTLE_TIME;
if (ts->alarmtimer)
alarms |= DAHDI_ALARM_RECOVER;
/* If receiving alarms, go into Yellow alarm state */
if (alarms && !(ts->spanflags & FLAG_SENDINGYELLOW)) {
/* We manually do yellow alarm to handle RECOVER and NOTOPEN, otherwise it's auto anyway */
unsigned char fmr4;
fmr4 = __t4_framer_in(wc, span, 0x20);
__t4_framer_out(wc, span, 0x20, fmr4 | 0x20);
dev_info(&wc->dev->dev, "Setting yellow alarm span %d\n",
span+1);
ts->spanflags |= FLAG_SENDINGYELLOW;
} else if ((!alarms) && (ts->spanflags & FLAG_SENDINGYELLOW)) {
unsigned char fmr4;
/* We manually do yellow alarm to handle RECOVER */
fmr4 = __t4_framer_in(wc, span, 0x20);
__t4_framer_out(wc, span, 0x20, fmr4 & ~0x20);
dev_info(&wc->dev->dev, "Clearing yellow alarm span %d\n",
span+1);
/* Re-enable timing slip interrupts */
e = __t4_framer_in(wc, span, 0x17);
__t4_framer_out(wc, span, 0x17, (e & ~(0x03)));
ts->spanflags &= ~FLAG_SENDINGYELLOW;
}
/* Re-check the timing source when we enter/leave alarm, not withstanding
yellow alarm */
if (c & 0x10) { /* receiving yellow (RAI) */
if (!ts->yelalarm_time) {
if (unlikely(debug)) {
/* starting to debounce AIS */
dev_info(&wc->dev->dev, "wct%dxxp: yellow "
"(RAI) detected on span %d but "
"debouncing for %d ms\n",
wc->numspans, span + 1,
yelalarmdebounce);
}
ts->yelalarm_time = jiffies +
msecs_to_jiffies(yelalarmdebounce);
} else if (time_after(jiffies, ts->yelalarm_time)) {
alarms |= DAHDI_ALARM_YELLOW;
}
} else {
ts->yelalarm_time = 0;
}
if (alarms)
ts->alarmcheck_time = jiffies + msecs_to_jiffies(100);
else
ts->alarmcheck_time = jiffies + msecs_to_jiffies(50);
if (ts->span.mainttimer || ts->span.maintstat)
alarms |= DAHDI_ALARM_LOOPBACK;
ts->span.alarms = alarms;
spin_unlock(&wc->reglock);
dahdi_alarm_notify(&ts->span);
}
static void t4_do_counters(struct t4 *wc)
{
int span;
for (span = 0; span < wc->numspans; span++) {
struct t4_span *ts = wc->tspans[span];
spin_lock(&wc->reglock);
if (ts->alarmtimer && (0 == (--ts->alarmtimer)))
ts->span.alarms &= ~(DAHDI_ALARM_RECOVER);
spin_unlock(&wc->reglock);
t4_check_alarms(wc, span);
}
}
static inline void __handle_leds(struct t4 *wc)
{
int x;
wc->blinktimer++;
for (x=0;x<wc->numspans;x++) {
struct t4_span *ts = wc->tspans[x];
if (ts->span.flags & DAHDI_FLAG_RUNNING) {
if ((ts->span.alarms & (DAHDI_ALARM_RED |
DAHDI_ALARM_BLUE)) ||
ts->losalarm_time) {
#ifdef FANCY_ALARM
if (wc->blinktimer == (altab[wc->alarmpos] >> 1)) {
__t4_set_led(wc, x, WC_RED);
}
if (wc->blinktimer == 0xf) {
__t4_set_led(wc, x, WC_OFF);
}
#else
if (wc->blinktimer == 160) {
__t4_set_led(wc, x, WC_RED);
} else if (wc->blinktimer == 480) {
__t4_set_led(wc, x, WC_OFF);
}
#endif
} else if (ts->span.alarms & DAHDI_ALARM_YELLOW) {
/* Yellow Alarm */
__t4_set_led(wc, x, WC_YELLOW);
} else if (ts->span.mainttimer || ts->span.maintstat) {
#ifdef FANCY_ALARM
if (wc->blinktimer == (altab[wc->alarmpos] >> 1)) {
__t4_set_led(wc, x, WC_GREEN);
}
if (wc->blinktimer == 0xf) {
__t4_set_led(wc, x, WC_OFF);
}
#else
if (wc->blinktimer == 160) {
__t4_set_led(wc, x, WC_GREEN);
} else if (wc->blinktimer == 480) {
__t4_set_led(wc, x, WC_OFF);
}
#endif
} else {
/* No Alarm */
__t4_set_led(wc, x, WC_GREEN);
}
} else
__t4_set_led(wc, x, WC_OFF);
}
#ifdef FANCY_ALARM
if (wc->blinktimer == 0xf) {
wc->blinktimer = -1;
wc->alarmpos++;
if (wc->alarmpos >= ARRAY_SIZE(altab))
wc->alarmpos = 0;
}
#else
if (wc->blinktimer == 480)
wc->blinktimer = 0;
#endif
}
static inline void t4_framer_interrupt(struct t4 *wc, int span)
{
/* Check interrupts for a given span */
unsigned char gis, isr0, isr1, isr2, isr3, isr4;
int readsize = -1;
struct t4_span *ts = wc->tspans[span];
struct dahdi_chan *sigchan;
unsigned long flags;
bool recheck_sigbits = false;
/* 1st gen cards isn't used interrupts */
spin_lock_irqsave(&wc->reglock, flags);
gis = __t4_framer_in(wc, span, FRMR_GIS);
isr0 = (gis & FRMR_GIS_ISR0) ? __t4_framer_in(wc, span, FRMR_ISR0) : 0;
isr1 = (gis & FRMR_GIS_ISR1) ? __t4_framer_in(wc, span, FRMR_ISR1) : 0;
isr2 = (gis & FRMR_GIS_ISR2) ? __t4_framer_in(wc, span, FRMR_ISR2) : 0;
isr3 = (gis & FRMR_GIS_ISR3) ? __t4_framer_in(wc, span, FRMR_ISR3) : 0;
isr4 = (gis & FRMR_GIS_ISR4) ? __t4_framer_in(wc, span, FRMR_ISR4) : 0;
if ((debug & DEBUG_FRAMER) && !(isr3 & ISR3_SEC)) {
dev_info(&wc->dev->dev, "gis: %02x, isr0: %02x, isr1: %02x, "\
"isr2: %02x, isr3: %08x, isr4: %02x, intcount=%u\n",
gis, isr0, isr1, isr2, isr3, isr4, wc->intcount);
}
/* Collect performance counters once per second */
if (isr3 & ISR3_SEC) {
ts->span.count.fe += __t4_framer_in(wc, span, FECL_T);
ts->span.count.crc4 += __t4_framer_in(wc, span, CEC1L_T);
ts->span.count.cv += __t4_framer_in(wc, span, CVCL_T);
ts->span.count.ebit += __t4_framer_in(wc, span, EBCL_T);
ts->span.count.be += __t4_framer_in(wc, span, BECL_T);
ts->span.count.prbs = __t4_framer_in(wc, span, FRS1_T);
if (DAHDI_RXSIG_INITIAL == ts->span.chans[0]->rxhooksig)
recheck_sigbits = true;
}
spin_unlock_irqrestore(&wc->reglock, flags);
/* Collect errored second counter once per second */
if (isr3 & ISR3_ES) {
ts->span.count.errsec += 1;
}
if (isr0 & 0x08 || recheck_sigbits)
t4_check_sigbits(wc, span);
if (E1 == ts->linemode) {
/* E1 checks */
if ((isr3 & 0x38) || isr2 || isr1)
t4_check_alarms(wc, span);
} else {
/* T1 checks */
if (isr2 || (isr3 & 0x08))
t4_check_alarms(wc, span);
}
if (!ts->span.alarms) {
if ((isr3 & 0x3) || (isr4 & 0xc0))
ts->span.count.timingslips++;
if (debug & DEBUG_MAIN) {
if (isr3 & 0x02)
dev_notice(&wc->dev->dev, "TE%d10P: RECEIVE "
"slip NEGATIVE on span %d\n",
wc->numspans, span + 1);
if (isr3 & 0x01)
dev_notice(&wc->dev->dev, "TE%d10P: RECEIVE "
"slip POSITIVE on span %d\n",
wc->numspans, span + 1);
if (isr4 & 0x80)
dev_notice(&wc->dev->dev, "TE%dXXP: TRANSMIT "
"slip POSITIVE on span %d\n",
wc->numspans, span + 1);
if (isr4 & 0x40)
dev_notice(&wc->dev->dev, "TE%d10P: TRANSMIT "
"slip NEGATIVE on span %d\n",
wc->numspans, span + 1);
}
} else
ts->span.count.timingslips = 0;
spin_lock_irqsave(&wc->reglock, flags);
/* HDLC controller checks - receive side */
if (!ts->sigchan) {
spin_unlock_irqrestore(&wc->reglock, flags);
return;
}
sigchan = ts->sigchan;
spin_unlock_irqrestore(&wc->reglock, flags);
if (isr0 & FRMR_ISR0_RME) {
readsize = (t4_framer_in(wc, span, FRMR_RBCH) << 8) | t4_framer_in(wc, span, FRMR_RBCL);
if (debug & DEBUG_FRAMER)
dev_notice(&wc->dev->dev, "Received data length is %d "
"(%d)\n", readsize,
readsize & FRMR_RBCL_MAX_SIZE);
/* RPF isn't set on last part of frame */
if ((readsize > 0) && ((readsize &= FRMR_RBCL_MAX_SIZE) == 0))
readsize = FRMR_RBCL_MAX_SIZE + 1;
} else if (isr0 & FRMR_ISR0_RPF)
readsize = FRMR_RBCL_MAX_SIZE + 1;
if (readsize > 0) {
int i;
unsigned char readbuf[FRMR_RBCL_MAX_SIZE + 1];
if (debug & DEBUG_FRAMER)
dev_notice(&wc->dev->dev, "Framer %d: Got RPF/RME! "
"readsize is %d\n", sigchan->span->offset,
readsize);
for (i = 0; i < readsize; i++)
readbuf[i] = t4_framer_in(wc, span, FRMR_RXFIFO);
/* Tell the framer to clear the RFIFO */
t4_framer_cmd_wait(wc, span, FRMR_CMDR_RMC);
if (debug & DEBUG_FRAMER) {
dev_notice(&wc->dev->dev, "RX(");
for (i = 0; i < readsize; i++)
dev_notice(&wc->dev->dev, "%s%02x",
(i ? " " : ""), readbuf[i]);
dev_notice(&wc->dev->dev, ")\n");
}
if (isr0 & FRMR_ISR0_RME) {
/* Do checks for HDLC problems */
unsigned char rsis = readbuf[readsize-1];
unsigned char rsis_reg = t4_framer_in(wc, span, FRMR_RSIS);
++ts->frames_in;
if ((debug & DEBUG_FRAMER) && !(ts->frames_in & 0x0f))
dev_notice(&wc->dev->dev, "Received %d frames "
"on span %d\n", ts->frames_in, span);
if (debug & DEBUG_FRAMER)
dev_notice(&wc->dev->dev, "Received HDLC frame"
" %d. RSIS = 0x%x (%x)\n",
ts->frames_in, rsis, rsis_reg);
if (!(rsis & FRMR_RSIS_CRC16)) {
if (debug & DEBUG_FRAMER)
dev_notice(&wc->dev->dev, "CRC check "
"failed %d\n", span);
dahdi_hdlc_abort(sigchan, DAHDI_EVENT_BADFCS);
} else if (rsis & FRMR_RSIS_RAB) {
if (debug & DEBUG_FRAMER)
dev_notice(&wc->dev->dev, "ABORT of "
"current frame due to "
"overflow %d\n", span);
dahdi_hdlc_abort(sigchan, DAHDI_EVENT_ABORT);
} else if (rsis & FRMR_RSIS_RDO) {
if (debug & DEBUG_FRAMER)
dev_notice(&wc->dev->dev, "HDLC "
"overflow occured %d\n",
span);
dahdi_hdlc_abort(sigchan, DAHDI_EVENT_OVERRUN);
} else if (!(rsis & FRMR_RSIS_VFR)) {
if (debug & DEBUG_FRAMER)
dev_notice(&wc->dev->dev, "Valid Frame"
" check failed on span %d\n",
span);
dahdi_hdlc_abort(sigchan, DAHDI_EVENT_ABORT);
} else {
dahdi_hdlc_putbuf(sigchan, readbuf, readsize - 1);
dahdi_hdlc_finish(sigchan);
if (debug & DEBUG_FRAMER)
dev_notice(&wc->dev->dev, "Received "
"valid HDLC frame on span %d"
"\n", span);
}
} else if (isr0 & FRMR_ISR0_RPF)
dahdi_hdlc_putbuf(sigchan, readbuf, readsize);
}
/* Transmit side */
if (isr1 & FRMR_ISR1_XDU) {
if (debug & DEBUG_FRAMER)
dev_notice(&wc->dev->dev, "XDU: Resetting signal "
"controller!\n");
t4_framer_cmd_wait(wc, span, FRMR_CMDR_SRES);
} else if (isr1 & FRMR_ISR1_XPR) {
if (debug & DEBUG_FRAMER)
dev_notice(&wc->dev->dev, "Sigchan %d is %p\n",
sigchan->chanpos, sigchan);
if (debug & DEBUG_FRAMER)
dev_notice(&wc->dev->dev, "Framer %d: Got XPR!\n",
sigchan->span->offset);
t4_hdlc_xmit_fifo(wc, span, ts);
}
if (isr1 & FRMR_ISR1_ALLS) {
if (debug & DEBUG_FRAMER)
dev_notice(&wc->dev->dev, "ALLS received\n");
}
}
#ifdef SUPPORT_GEN1
static irqreturn_t _t4_interrupt(int irq, void *dev_id)
{
struct t4 *wc = dev_id;
unsigned long flags;
int x;
unsigned int status;
unsigned int status2;
/* Make sure it's really for us */
status = __t4_pci_in(wc, WC_INTR);
/* Process framer interrupts */
status2 = t4_framer_in(wc, 0, FRMR_CIS);
if (status2 & 0x0f) {
for (x = 0; x < wc->numspans; ++x) {
if (status2 & (1 << x))
t4_framer_interrupt(wc, x);
}
}
/* Ignore if it's not for us */
if (!status)
return IRQ_NONE;
__t4_pci_out(wc, WC_INTR, 0);
if (!wc->spansstarted) {
dev_notice(&wc->dev->dev, "Not prepped yet!\n");
return IRQ_NONE;
}
wc->intcount++;
if (status & 0x3) {
t4_receiveprep(wc, status);
t4_transmitprep(wc, status);
}
t4_do_counters(wc);
x = wc->intcount & 15 /* 63 */;
switch(x) {
case 0:
case 1:
case 2:
case 3:
t4_check_sigbits(wc, x);
break;
case 4:
case 5:
case 6:
case 7:
t4_check_alarms(wc, x - 4);
break;
}
spin_lock_irqsave(&wc->reglock, flags);
__handle_leds(wc);
if (test_bit(T4_CHECK_TIMING, &wc->checkflag))
__t4_set_timing_source_auto(wc);
spin_unlock_irqrestore(&wc->reglock, flags);
return IRQ_RETVAL(1);
}
DAHDI_IRQ_HANDLER(t4_interrupt)
{
irqreturn_t ret;
unsigned long flags;
local_irq_save(flags);
ret = _t4_interrupt(irq, dev_id);
local_irq_restore(flags);
return ret;
}
#endif
static int t4_allocate_buffers(struct t4 *wc, int numbufs,
void **oldalloc, dma_addr_t *oldwritedma)
{
void *alloc;
dma_addr_t writedma;
/* 32 channels, Double-buffer, Read/Write, 4 spans */
alloc = pci_alloc_consistent(wc->dev, numbufs * T4_BASE_SIZE(wc) * 2,
&writedma);
if (!alloc) {
dev_notice(&wc->dev->dev, "wct%dxxp: Unable to allocate "
"DMA-able memory\n", wc->numspans);
return -ENOMEM;
}
if (oldwritedma)
*oldwritedma = wc->writedma;
if (oldalloc)
*oldalloc = wc->writechunk;
wc->writechunk = alloc;
wc->writedma = writedma;
/* Read is after the whole write piece (in words) */
wc->readchunk = wc->writechunk + (T4_BASE_SIZE(wc) * numbufs) / 4;
/* Same thing but in bytes... */
wc->readdma = wc->writedma + (T4_BASE_SIZE(wc) * numbufs);
wc->numbufs = numbufs;
/* Initialize Write/Buffers to all blank data */
memset(wc->writechunk, 0x00, T4_BASE_SIZE(wc) * numbufs);
memset(wc->readchunk, 0xff, T4_BASE_SIZE(wc) * numbufs);
if (debug) {
dev_notice(&wc->dev->dev, "DMA memory base of size %d at " \
"%p. Read: %p and Write %p\n",
numbufs * T4_BASE_SIZE(wc) * 2, wc->writechunk,
wc->readchunk, wc->writechunk);
}
return 0;
}
static void t4_increase_latency(struct t4 *wc, int newlatency)
{
unsigned long flags;
void *oldalloc;
dma_addr_t oldaddr;
int oldbufs;
spin_lock_irqsave(&wc->reglock, flags);
__t4_pci_out(wc, WC_DMACTRL, 0x00000000);
/* Acknowledge any pending interrupts */
__t4_pci_out(wc, WC_INTR, 0x00000000);
__t4_pci_in(wc, WC_VERSION);
oldbufs = wc->numbufs;
if (t4_allocate_buffers(wc, newlatency, &oldalloc, &oldaddr)) {
dev_info(&wc->dev->dev, "Error allocating latency buffers for "
"latency of %d\n", newlatency);
__t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
spin_unlock_irqrestore(&wc->reglock, flags);
return;
}
__t4_pci_out(wc, WC_RDADDR, wc->readdma);
__t4_pci_out(wc, WC_WRADDR, wc->writedma);
__t4_pci_in(wc, WC_VERSION);
__t4_pci_out(wc, 5, (ms_per_irq << 16) | newlatency);
__t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
__t4_pci_in(wc, WC_VERSION);
wc->rxident = 0;
wc->lastindex = 0;
spin_unlock_irqrestore(&wc->reglock, flags);
pci_free_consistent(wc->dev, T4_BASE_SIZE(wc) * oldbufs * 2,
oldalloc, oldaddr);
dev_info(&wc->dev->dev, "Increased latency to %d\n", newlatency);
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
static void t4_work_func(void *data)
{
struct t4 *wc = data;
#else
static void t4_work_func(struct work_struct *work)
{
struct t4 *wc = container_of(work, struct t4, bh_work);
#endif
if (test_bit(T4_CHANGE_LATENCY, &wc->checkflag)) {
if (wc->needed_latency != wc->numbufs) {
t4_increase_latency(wc, wc->needed_latency);
clear_bit(T4_CHANGE_LATENCY, &wc->checkflag);
}
}
#ifdef VPM_SUPPORT
if (wc->vpm) {
if (test_and_clear_bit(T4_CHECK_VPM, &wc->checkflag)) {
/* How stupid is it that the octasic can't generate an
* interrupt when there's a tone, in spite of what
* their documentation says? */
t4_check_vpm(wc);
}
}
#endif
}
static irqreturn_t _t4_interrupt_gen2(int irq, void *dev_id)
{
struct t4 *wc = dev_id;
unsigned int status;
unsigned char rxident, expected;
/* Check this first in case we get a spurious interrupt */
if (unlikely(test_bit(T4_STOP_DMA, &wc->checkflag))) {
/* Stop DMA cleanly if requested */
wc->dmactrl = 0x0;
t4_pci_out(wc, WC_DMACTRL, 0x00000000);
/* Acknowledge any pending interrupts */
t4_pci_out(wc, WC_INTR, 0x00000000);
spin_lock(&wc->reglock);
__t4_set_sclk_src(wc, WC_SELF, 0, 0);
spin_unlock(&wc->reglock);
return IRQ_RETVAL(1);
}
/* Make sure it's really for us */
status = __t4_pci_in(wc, WC_INTR);
/* Ignore if it's not for us */
if (!(status & 0x7)) {
return IRQ_NONE;
}
#ifdef ENABLE_WORKQUEUES
__t4_pci_out(wc, WC_INTR, status & 0x00000008);
#endif
if (unlikely(!wc->spansstarted)) {
dev_info(&wc->dev->dev, "Not prepped yet!\n");
return IRQ_NONE;
}
wc->intcount++;
if ((wc->devtype->flags & FLAG_5THGEN) && (status & 0x2)) {
rxident = (status >> 16) & 0x7f;
expected = (wc->rxident + ms_per_irq) % 128;
if ((rxident != expected) && !test_bit(T4_IGNORE_LATENCY, &wc->checkflag)) {
int needed_latency;
int smallest_max;
if (debug & DEBUG_MAIN)
dev_warn(&wc->dev->dev, "Missed interrupt. "
"Expected ident of %d and got ident "
"of %d\n", expected, rxident);
if (test_bit(T4_IGNORE_LATENCY, &wc->checkflag)) {
dev_info(&wc->dev->dev,
"Should have ignored latency\n");
}
if (rxident > wc->rxident) {
needed_latency = rxident - wc->rxident;
} else {
needed_latency = (128 - wc->rxident) + rxident;
}
needed_latency += 1;
smallest_max = (max_latency >= GEN5_MAX_LATENCY) ? GEN5_MAX_LATENCY : max_latency;
if (needed_latency > smallest_max) {
dev_info(&wc->dev->dev, "Truncating latency "
"request to %d instead of %d\n",
smallest_max, needed_latency);
needed_latency = smallest_max;
}
if (needed_latency > wc->numbufs) {
dev_info(&wc->dev->dev, "Need to increase "
"latency. Estimated latency should "
"be %d\n", needed_latency);
wc->ddev->irqmisses++;
wc->needed_latency = needed_latency;
__t4_pci_out(wc, WC_DMACTRL, 0x00000000);
set_bit(T4_CHANGE_LATENCY, &wc->checkflag);
goto out;
}
}
wc->rxident = rxident;
}
#ifdef DEBUG
if (unlikely((wc->intcount < 20)))
dev_dbg(&wc->dev->dev, "2G: Got interrupt, status = %08x, "
"CIS = %04x\n", status, t4_framer_in(wc, 0, FRMR_CIS));
#endif
if (likely(status & 0x2)) {
#ifdef ENABLE_WORKQUEUES
int cpus = num_online_cpus();
atomic_set(&wc->worklist, wc->numspans);
if (wc->tspans[0]->span.flags & DAHDI_FLAG_RUNNING)
t4_queue_work(wc->workq, &wc->tspans[0]->swork, 0);
else
atomic_dec(&wc->worklist);
if (wc->tspans[1]->span.flags & DAHDI_FLAG_RUNNING)
t4_queue_work(wc->workq, &wc->tspans[1]->swork, 1 % cpus);
else
atomic_dec(&wc->worklist);
if (wc->numspans == 4) {
if (wc->tspans[2]->span.flags & DAHDI_FLAG_RUNNING)
t4_queue_work(wc->workq, &wc->tspans[2]->swork, 2 % cpus);
else
atomic_dec(&wc->worklist);
if (wc->tspans[3]->span.flags & DAHDI_FLAG_RUNNING)
t4_queue_work(wc->workq, &wc->tspans[3]->swork, 3 % cpus);
else
atomic_dec(&wc->worklist);
}
#else
unsigned int reg5 = __t4_pci_in(wc, 5);
#ifdef DEBUG
if (wc->intcount < 20)
dev_info(&wc->dev->dev, "Reg 5 is %08x\n", reg5);
#endif
if (wc->devtype->flags & FLAG_5THGEN) {
unsigned int current_index = (reg5 >> 8) & 0x7f;
while (((wc->lastindex + 1) % wc->numbufs) != current_index) {
wc->lastindex = (wc->lastindex + 1) % wc->numbufs;
setup_chunks(wc, wc->lastindex);
t4_prep_gen2(wc);
}
} else {
t4_prep_gen2(wc);
}
#endif
t4_do_counters(wc);
spin_lock(&wc->reglock);
__handle_leds(wc);
spin_unlock(&wc->reglock);
}
if (unlikely(status & 0x1)) {
unsigned char cis;
cis = t4_framer_in(wc, 0, FRMR_CIS);
if (cis & FRMR_CIS_GIS1)
t4_framer_interrupt(wc, 0);
if (cis & FRMR_CIS_GIS2)
t4_framer_interrupt(wc, 1);
if (cis & FRMR_CIS_GIS3)
t4_framer_interrupt(wc, 2);
if (cis & FRMR_CIS_GIS4)
t4_framer_interrupt(wc, 3);
if (is_octal(wc)) {
if (cis & FRMR_CIS_GIS5)
t4_framer_interrupt(wc, 4);
if (cis & FRMR_CIS_GIS6)
t4_framer_interrupt(wc, 5);
if (cis & FRMR_CIS_GIS7)
t4_framer_interrupt(wc, 6);
if (cis & FRMR_CIS_GIS8)
t4_framer_interrupt(wc, 7);
}
}
#ifdef VPM_SUPPORT
if (wc->vpm && vpmdtmfsupport) {
/* How stupid is it that the octasic can't generate an
* interrupt when there's a tone, in spite of what their
* documentation says? */
if (!(wc->intcount & 0xf))
set_bit(T4_CHECK_VPM, &wc->checkflag);
}
#endif
spin_lock(&wc->reglock);
if (unlikely(test_bit(T4_CHECK_TIMING, &wc->checkflag))) {
__t4_set_timing_source_auto(wc);
}
spin_unlock(&wc->reglock);
out:
if (unlikely(test_bit(T4_CHANGE_LATENCY, &wc->checkflag) || test_bit(T4_CHECK_VPM, &wc->checkflag)))
schedule_work(&wc->bh_work);
#ifndef ENABLE_WORKQUEUES
__t4_pci_out(wc, WC_INTR, 0);
#endif
return IRQ_RETVAL(1);
}
DAHDI_IRQ_HANDLER(t4_interrupt_gen2)
{
irqreturn_t ret;
unsigned long flags;
local_irq_save(flags);
ret = _t4_interrupt_gen2(irq, dev_id);
local_irq_restore(flags);
return ret;
}
#ifdef SUPPORT_GEN1
static int t4_reset_dma(struct t4 *wc)
{
/* Turn off DMA and such */
wc->dmactrl = 0x0;
t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
t4_pci_out(wc, WC_COUNT, 0);
t4_pci_out(wc, WC_RDADDR, 0);
t4_pci_out(wc, WC_WRADDR, 0);
t4_pci_out(wc, WC_INTR, 0);
/* Turn it all back on */
t4_pci_out(wc, WC_RDADDR, wc->readdma);
t4_pci_out(wc, WC_WRADDR, wc->writedma);
t4_pci_out(wc, WC_COUNT, ((DAHDI_MAX_CHUNKSIZE * 2 * 32 - 1) << 18) | ((DAHDI_MAX_CHUNKSIZE * 2 * 32 - 1) << 2));
t4_pci_out(wc, WC_INTR, 0);
#ifdef VPM_SUPPORT
wc->dmactrl = 0xc0000000 | (1 << 29) |
((wc->vpm) ? T4_VPM_PRESENT : 0);
#else
wc->dmactrl = 0xc0000000 | (1 << 29);
#endif
if (noburst)
wc->dmactrl |= (1 << 26);
t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
return 0;
}
#endif
#ifdef VPM_SUPPORT
static void t4_vpm_init(struct t4 *wc)
{
int laws[8] = { 0, };
int x;
unsigned int vpm_capacity;
struct firmware embedded_firmware;
const struct firmware *firmware = &embedded_firmware;
#if !defined(HOTPLUG_FIRMWARE)
extern void _binary_dahdi_fw_oct6114_064_bin_size;
extern void _binary_dahdi_fw_oct6114_128_bin_size;
extern void _binary_dahdi_fw_oct6114_256_bin_size;
extern u8 _binary_dahdi_fw_oct6114_064_bin_start[];
extern u8 _binary_dahdi_fw_oct6114_128_bin_start[];
extern u8 _binary_dahdi_fw_oct6114_256_bin_start[];
#else
static const char oct064_firmware[] = "dahdi-fw-oct6114-064.bin";
static const char oct128_firmware[] = "dahdi-fw-oct6114-128.bin";
static const char oct256_firmware[] = "dahdi-fw-oct6114-256.bin";
#endif
if (!vpmsupport) {
dev_info(&wc->dev->dev, "VPM450: Support Disabled\n");
return;
}
/* Turn on GPIO/DATA mux if supported */
t4_gpio_setdir(wc, (1 << 24), (1 << 24));
__t4_raw_oct_out(wc, 0x000a, 0x5678);
__t4_raw_oct_out(wc, 0x0004, 0x1234);
__t4_raw_oct_in(wc, 0x0004);
__t4_raw_oct_in(wc, 0x000a);
if (debug)
dev_notice(&wc->dev->dev, "OCT Result: %04x/%04x\n",
__t4_raw_oct_in(wc, 0x0004),
__t4_raw_oct_in(wc, 0x000a));
if (__t4_raw_oct_in(wc, 0x0004) != 0x1234) {
dev_notice(&wc->dev->dev, "VPM450: Not Present\n");
return;
}
/* Setup alaw vs ulaw rules */
for (x = 0;x < wc->numspans; x++) {
if (wc->tspans[x]->span.channels > 24)
laws[x] = 1;
}
vpm_capacity = get_vpm450m_capacity(&wc->dev->dev);
if (vpm_capacity != wc->numspans * 32) {
dev_info(&wc->dev->dev, "Disabling VPMOCT%03d. TE%dXXP"\
" requires a VPMOCT%03d", vpm_capacity,
wc->numspans, wc->numspans*32);
return;
}
switch (vpm_capacity) {
case 64:
#if defined(HOTPLUG_FIRMWARE)
if ((request_firmware(&firmware, oct064_firmware, &wc->dev->dev) != 0) ||
!firmware) {
dev_notice(&wc->dev->dev, "VPM450: firmware %s not "
"available from userspace\n", oct064_firmware);
return;
}
#else
embedded_firmware.data = _binary_dahdi_fw_oct6114_064_bin_start;
/* Yes... this is weird. objcopy gives us a symbol containing
the size of the firmware, not a pointer a variable containing
the size. The only way we can get the value of the symbol
is to take its address, so we define it as a pointer and
then cast that value to the proper type.
*/
embedded_firmware.size = (size_t) &_binary_dahdi_fw_oct6114_064_bin_size;
#endif
break;
case 128:
#if defined(HOTPLUG_FIRMWARE)
if ((request_firmware(&firmware, oct128_firmware, &wc->dev->dev) != 0) ||
!firmware) {
dev_notice(&wc->dev->dev, "VPM450: firmware %s not "
"available from userspace\n", oct128_firmware);
return;
}
#else
embedded_firmware.data = _binary_dahdi_fw_oct6114_128_bin_start;
/* Yes... this is weird. objcopy gives us a symbol containing
the size of the firmware, not a pointer a variable containing
the size. The only way we can get the value of the symbol
is to take its address, so we define it as a pointer and
then cast that value to the proper type.
*/
embedded_firmware.size = (size_t) &_binary_dahdi_fw_oct6114_128_bin_size;
#endif
break;
case 256:
#if defined(HOTPLUG_FIRMWARE)
if ((request_firmware(&firmware, oct256_firmware, &wc->dev->dev) != 0) ||
!firmware) {
dev_notice(&wc->dev->dev, "VPM450: firmware %s not "
"available from userspace\n", oct256_firmware);
return;
}
#else
embedded_firmware.data = _binary_dahdi_fw_oct6114_256_bin_start;
/* Yes... this is weird. objcopy gives us a symbol containing
the size of the firmware, not a pointer a variable containing
the size. The only way we can get the value of the symbol
is to take its address, so we define it as a pointer and
then cast that value to the proper type.
*/
embedded_firmware.size = (size_t) &_binary_dahdi_fw_oct6114_256_bin_size;
#endif
break;
default:
dev_notice(&wc->dev->dev, "Unsupported channel capacity found "
"on VPM module (%d).\n", vpm_capacity);
return;
}
wc->vpm = init_vpm450m(&wc->dev->dev, laws, wc->numspans, firmware);
if (!wc->vpm) {
dev_notice(&wc->dev->dev, "VPM450: Failed to initialize\n");
if (firmware != &embedded_firmware)
release_firmware(firmware);
return;
}
if (firmware != &embedded_firmware)
release_firmware(firmware);
if (vpmdtmfsupport == -1) {
dev_info(&wc->dev->dev, "VPM450: hardware DTMF disabled.\n");
vpmdtmfsupport = 0;
}
dev_info(&wc->dev->dev, "VPM450: Present and operational servicing %d "
"span(s)\n", wc->numspans);
}
#endif /* VPM_SUPPORT */
static void t4_tsi_reset(struct t4 *wc)
{
int x;
if (is_octal(wc)) {
for (x = 0; x < 256; x++) {
wc->dmactrl &= ~0x0001ffff;
wc->dmactrl |= (0x00004000 | ((x & 0x7f) << 7) | ((x >> 7) << 15));
t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
}
wc->dmactrl &= ~0x0001ffff;
} else {
for (x = 0; x < 128; x++) {
wc->dmactrl &= ~0x00007fff;
wc->dmactrl |= (0x00004000 | (x << 7));
t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
}
wc->dmactrl &= ~0x00007fff;
}
t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
}
/* Note that channels here start from 1 */
static void t4_tsi_assign(struct t4 *wc, int fromspan, int fromchan, int tospan, int tochan)
{
unsigned long flags;
int fromts, tots;
fromts = (fromspan << 5) |(fromchan);
tots = (tospan << 5) | (tochan);
if (!has_e1_span(wc)) {
fromts += 4;
tots += 4;
}
spin_lock_irqsave(&wc->reglock, flags);
if (is_octal(wc)) {
int fromts_b = fromts & 0x7f;
int fromts_t = fromts >> 7;
int tots_b = tots & 0x7f;
int tots_t = tots >> 7;
wc->dmactrl &= ~0x0001ffff;
wc->dmactrl |= ((fromts_t << 16) | (tots_t << 15) | 0x00004000 | (tots_b << 7) | (fromts_b));
__t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
wc->dmactrl &= ~0x0001ffff;
__t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
} else {
wc->dmactrl &= ~0x00007fff;
wc->dmactrl |= (0x00004000 | (tots << 7) | (fromts));
__t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
wc->dmactrl &= ~0x00007fff;
__t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
}
spin_unlock_irqrestore(&wc->reglock, flags);
}
static void t4_tsi_unassign(struct t4 *wc, int tospan, int tochan)
{
unsigned long flags;
int tots;
tots = (tospan << 5) | (tochan);
if (!has_e1_span(wc))
tots += 4;
spin_lock_irqsave(&wc->reglock, flags);
if (is_octal(wc)) {
int tots_b = tots & 0x7f;
int tots_t = tots >> 7;
wc->dmactrl &= ~0x0001ffff;
wc->dmactrl |= ((tots_t << 15) | 0x00004000 | (tots_b << 7));
__t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
if (debug & DEBUG_TSI)
dev_notice(&wc->dev->dev, "Sending '%08x\n", wc->dmactrl);
wc->dmactrl &= ~0x0001ffff;
__t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
} else {
wc->dmactrl &= ~0x00007fff;
wc->dmactrl |= (0x00004000 | (tots << 7));
__t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
if (debug & DEBUG_TSI)
dev_notice(&wc->dev->dev, "Sending '%08x\n", wc->dmactrl);
wc->dmactrl &= ~0x00007fff;
__t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
}
spin_unlock_irqrestore(&wc->reglock, flags);
}
#ifndef CONFIG_NOEXTENDED_RESET
static void t4_extended_reset(struct t4 *wc)
{
unsigned int oldreg = t4_pci_in(wc, 0x4);
udelay(1000);
t4_pci_out(wc, 0x4, 0x42000000);
t4_pci_out(wc, 0xa, 0x42000000);
t4_pci_out(wc, 0xa, 0x00080000);
t4_pci_out(wc, 0xa, 0x00080000);
t4_pci_out(wc, 0xa, 0x00080000);
t4_pci_out(wc, 0xa, 0x00180000);
t4_pci_out(wc, 0xa, 0x00080000);
t4_pci_out(wc, 0xa, 0x00180000);
t4_pci_out(wc, 0xa, 0x00080000);
t4_pci_out(wc, 0xa, 0x00180000);
t4_pci_out(wc, 0xa, 0x00080000);
t4_pci_out(wc, 0xa, 0x00180000);
t4_pci_out(wc, 0xa, 0x00080000);
t4_pci_out(wc, 0xa, 0x00180000);
t4_pci_out(wc, 0xa, 0x00080000);
t4_pci_out(wc, 0xa, 0x00180000);
t4_pci_out(wc, 0x4, oldreg);
udelay(1000);
}
#endif
#define SPI_CS (0)
#define SPI_CLK (1)
#define SPI_IO0 (2)
#define SPI_IO1 (3)
#define SPI_IO3 (4)
#define SPI_IO2 (5)
#define SPI_IN (0)
#define SPI_OUT (1)
#define ESPI_REG 13
static void t8_clear_bit(struct t4 *wc, int whichb)
{
wc->st.wrreg &= ~(1 << whichb);
}
static void t8_set_bit(struct t4 *wc, int whichb, int val)
{
t8_clear_bit(wc, whichb);
wc->st.wrreg |= (val << whichb);
}
static int t8_get_bit(struct t4 *wc, int whichb)
{
return (wc->st.rdreg >> whichb) & 1;
}
static void set_iodir(struct t4 *wc, int whichb, int dir)
{
whichb += 16;
t8_clear_bit(wc, whichb);
t8_set_bit(wc, whichb, dir);
}
static void write_hwreg(struct t4 *wc)
{
t4_pci_out(wc, ESPI_REG, wc->st.wrreg);
}
static void read_hwreg(struct t4 *wc)
{
wc->st.rdreg = t4_pci_in(wc, ESPI_REG);
}
static void set_cs(struct t4 *wc, int state)
{
t8_set_bit(wc, SPI_CS, state);
write_hwreg(wc);
}
static void set_clk(struct t4 *wc, int clk)
{
t8_set_bit(wc, SPI_CLK, clk);
write_hwreg(wc);
}
static void clk_bit_out(struct t4 *wc, int val)
{
t8_set_bit(wc, SPI_IO0, val & 1);
set_clk(wc, 0);
set_clk(wc, 1);
}
static void shift_out(struct t4 *wc, int val)
{
int i;
for (i = 7; i >= 0; i--)
clk_bit_out(wc, (val >> i) & 1);
}
static int clk_bit_in(struct t4 *wc)
{
int ret;
set_clk(wc, 0);
read_hwreg(wc);
ret = t8_get_bit(wc, SPI_IO1);
set_clk(wc, 1);
return ret;
}
static int shift_in(struct t4 *wc)
{
int ret = 0;
int i;
int bit;
for (i = 7; i >= 0; i--) {
bit = clk_bit_in(wc);
ret |= ((bit & 1) << i);
}
return ret;
}
static void write_enable(struct t4 *wc)
{
int cmd = 0x06;
set_cs(wc, 0);
shift_out(wc, cmd);
set_cs(wc, 1);
}
static int read_sr1(struct t4 *wc)
{
int cmd = 0x05;
int ret;
set_cs(wc, 0);
shift_out(wc, cmd);
ret = shift_in(wc);
set_cs(wc, 1);
return ret;
}
static void clear_busy(struct t4 *wc)
{
static const int SR1_BUSY = (1 << 0);
unsigned long stop;
stop = jiffies + 2*HZ;
while (read_sr1(wc) & SR1_BUSY) {
if (time_after(jiffies, stop)) {
if (printk_ratelimit()) {
dev_err(&wc->dev->dev,
"Lockup in %s\n", __func__);
}
break;
}
cond_resched();
}
}
static void sector_erase(struct t4 *wc, uint32_t addr)
{
int cmd = 0x20;
write_enable(wc);
set_cs(wc, 0);
shift_out(wc, cmd);
shift_out(wc, (addr >> 16) & 0xff);
shift_out(wc, (addr >> 8) & 0xff);
shift_out(wc, (addr >> 0) & 0xff);
set_cs(wc, 1);
clear_busy(wc);
}
static void erase_half(struct t4 *wc)
{
uint32_t addr = 0x00080000;
uint32_t i;
dev_info(&wc->dev->dev, "Erasing octal firmware\n");
for (i = addr; i < (addr + 0x80000); i += 4096)
sector_erase(wc, i);
}
#define T8_FLASH_PAGE_SIZE 256UL
static void t8_update_firmware_page(struct t4 *wc, u32 address,
const u8 *page_data, size_t size)
{
int i;
write_enable(wc);
set_cs(wc, 0);
shift_out(wc, 0x02);
shift_out(wc, (address >> 16) & 0xff);
shift_out(wc, (address >> 8) & 0xff);
shift_out(wc, (address >> 0) & 0xff);
for (i = 0; i < size; ++i)
shift_out(wc, page_data[i]);
set_cs(wc, 1);
clear_busy(wc);
}
static int t8_update_firmware(struct t4 *wc, const struct firmware *fw,
const char *t8_firmware)
{
int res;
size_t offset = 0;
const u32 BASE_ADDRESS = 0x00080000;
const u8 *data, *end;
size_t size = 0;
/* Erase flash */
erase_half(wc);
dev_info(&wc->dev->dev,
"Uploading %s. This can take up to 30 seconds.\n", t8_firmware);
data = &fw->data[sizeof(struct t8_firm_header)];
end = &fw->data[fw->size];
while (data < end) {
/* Calculate the tail end of data that's shorter than a page */
size = min(T8_FLASH_PAGE_SIZE, (unsigned long)(end - data));
t8_update_firmware_page(wc, BASE_ADDRESS + offset,
data, size);
data += T8_FLASH_PAGE_SIZE;
offset += T8_FLASH_PAGE_SIZE;
cond_resched();
}
/* Reset te820 fpga after loading firmware */
dev_info(&wc->dev->dev, "Firmware load complete. Reseting device.\n");
res = pci_save_state(wc->dev);
if (res)
goto error_exit;
/* Set the fpga reset bits and clobber the remainder of the
* register, device will be reset anyway */
t4_pci_out(wc, WC_LEDS, 0xe0000000);
msleep(1000);
pci_restore_state(wc->dev);
/* Signal the driver to restart initialization.
* This will back out all initialization so far and
* restart the driver load process */
return -EAGAIN;
error_exit:
return res;
}
static char read_flash_byte(struct t4 *wc, unsigned int addr)
{
int cmd = 0x03;
char data;
set_cs(wc, 0);
shift_out(wc, cmd);
shift_out(wc, (addr >> 16) & 0xff);
shift_out(wc, (addr >> 8) & 0xff);
shift_out(wc, (addr >> 0) & 0xff);
data = shift_in(wc) & 0xff;
set_cs(wc, 1);
return data;
}
/**
* t8_read_serial - Returns the serial number of the board.
* @wc: The board whos serial number we are reading.
*
* The buffer returned is dynamically allocated and must be kfree'd by the
* caller. If memory could not be allocated, NULL is returned.
*
* Must be called in process context.
*
*/
static char *t8_read_serial(struct t4 *wc)
{
int i;
static const int MAX_SERIAL = 14;
static const u32 base_addr = 0x00080000-256;
unsigned char c;
unsigned char *serial = kzalloc(MAX_SERIAL + 1, GFP_KERNEL);
if (!serial)
return NULL;
for (i = 0; i < MAX_SERIAL; ++i) {
c = read_flash_byte(wc, base_addr+i);
if ((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9'))
serial[i] = c;
else
break;
}
if (!i) {
kfree(serial);
serial = NULL;
}
return serial;
}
static void setup_spi(struct t4 *wc)
{
wc->st.rdreg = wc->st.wrreg = 0;
set_iodir(wc, SPI_IO0, SPI_OUT);
set_iodir(wc, SPI_IO1, SPI_IN);
set_iodir(wc, SPI_CS, SPI_OUT);
set_iodir(wc, SPI_CLK, SPI_OUT);
t8_set_bit(wc, SPI_CS, 1);
t8_set_bit(wc, SPI_CLK, 1);
write_hwreg(wc);
}
static int t8_check_firmware(struct t4 *wc, unsigned int version)
{
const struct firmware *fw;
static const char t8_firmware[] = "dahdi-fw-te820.bin";
const struct t8_firm_header *header;
int res = 0;
u32 crc;
res = request_firmware(&fw, t8_firmware, &wc->dev->dev);
if (res) {
dev_info(&wc->dev->dev, "firmware %s not "
"available from userspace\n", t8_firmware);
goto cleanup;
}
header = (const struct t8_firm_header *)fw->data;
/* Check the crc before anything else */
crc = crc32(~0, &fw->data[10], fw->size - 10) ^ ~0;
if (memcmp("DIGIUM", header->header, sizeof(header->header)) ||
(le32_to_cpu(header->chksum) != crc)) {
dev_info(&wc->dev->dev,
"%s is invalid. Please reinstall.\n", t8_firmware);
goto cleanup;
}
/* Spi struct must be setup before any access to flash memory */
setup_spi(wc);
/* Check the two firmware versions */
if (le32_to_cpu(header->version) == version)
goto cleanup;
dev_info(&wc->dev->dev, "%s Version: %08x available for flash\n",
t8_firmware, header->version);
res = t8_update_firmware(wc, fw, t8_firmware);
if (res && res != -EAGAIN) {
dev_info(&wc->dev->dev, "Failed to load firmware %s\n",
t8_firmware);
}
cleanup:
release_firmware(fw);
return res;
}
static int
__t4_hardware_init_1(struct t4 *wc, unsigned int cardflags, bool first_time)
{
unsigned int version;
int res;
version = t4_pci_in(wc, WC_VERSION);
if (is_octal(wc) && first_time) {
dev_info(&wc->dev->dev, "Firmware Version: %01x.%02x\n",
(version & 0xf00) >> 8,
version & 0xff);
} else if (first_time) {
dev_info(&wc->dev->dev, "Firmware Version: %08x\n", version);
}
if (debug) {
dev_info(&wc->dev->dev, "Burst Mode: %s\n",
(!(cardflags & FLAG_BURST) && noburst) ? "Off" : "On");
#ifdef ENABLE_WORKQUEUES
dev_info(&wc->dev->dev, "Work Queues: Enabled\n");
#endif
}
/* Check the field updatable firmware for the wcte820 */
if (is_octal(wc)) {
res = t8_check_firmware(wc, version);
if (res)
return res;
wc->ddev->hardware_id = t8_read_serial(wc);
}
#if defined(CONFIG_FORCE_EXTENDED_RESET)
t4_extended_reset(wc);
#elif !defined(CONFIG_NOEXTENDED_RESET)
if (wc->devtype->flags & FLAG_EXPRESS)
t4_extended_reset(wc);
#endif
/* Make sure DMA engine is not running and interrupts are acknowledged */
wc->dmactrl = 0x0;
t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
/* Reset Framer and friends */
t4_pci_out(wc, WC_LEDS, 0x00000000);
/* Set DMA addresses */
t4_pci_out(wc, WC_RDADDR, wc->readdma);
t4_pci_out(wc, WC_WRADDR, wc->writedma);
/* Setup counters, interrupt flags (ignored in Gen2) */
if (cardflags & FLAG_2NDGEN) {
t4_tsi_reset(wc);
} else {
t4_pci_out(wc, WC_COUNT, ((DAHDI_MAX_CHUNKSIZE * 2 * 32 - 1) << 18) | ((DAHDI_MAX_CHUNKSIZE * 2 * 32 - 1) << 2));
}
/* Reset pending interrupts */
t4_pci_out(wc, WC_INTR, 0x00000000);
/* Read T1/E1 status */
if (first_time) {
if (!strcasecmp("auto", default_linemode)) {
if (-1 == t1e1override) {
wc->t1e1 = (((t4_pci_in(wc, WC_LEDS)) &
0x0f00) >> 8);
wc->t1e1 &= 0xf;
if (is_octal(wc)) {
wc->t1e1 |= ((t4_pci_in(wc, WC_LEDS2)) &
0x0f00) >> 4;
}
} else {
dev_warn(&wc->dev->dev,
"'t1e1override' is deprecated. Please use 'default_linemode'.\n");
wc->t1e1 = t1e1override & 0xf;
}
} else if (!strcasecmp("t1", default_linemode)) {
wc->t1e1 = 0;
} else if (!strcasecmp("e1", default_linemode)) {
wc->t1e1 = 0xff;
} else if (!strcasecmp("j1", default_linemode)) {
wc->t1e1 = 0;
j1mode = 1;
} else {
dev_err(&wc->dev->dev, "'%s' is an unknown linemode.\n",
default_linemode);
wc->t1e1 = 0;
}
}
wc->order = ((t4_pci_in(wc, WC_LEDS)) & 0xf0000000) >> 28;
order_index[wc->order]++;
/* TE820 Auth Check */
if (is_octal(wc)) {
unsigned long stop = jiffies + HZ;
uint32_t donebit;
t4_pci_out(wc, WC_LEDS2, WC_SET_AUTH);
donebit = t4_pci_in(wc, WC_LEDS2);
while (!(donebit & WC_GET_AUTH)) {
if (time_after(jiffies, stop)) {
/* Encryption check failed, stop operation */
dev_info(&wc->dev->dev,
"Failed encryption check. "
"Unloading driver.\n");
return -EIO;
}
msleep(20);
donebit = t4_pci_in(wc, WC_LEDS2);
}
}
return 0;
}
static int t4_hardware_init_1(struct t4 *wc, unsigned int cardflags)
{
return __t4_hardware_init_1(wc, cardflags, true);
}
static int __t4_hardware_init_2(struct t4 *wc, bool first_time)
{
int x;
unsigned int regval;
unsigned long flags;
if (t4_pci_in(wc, WC_VERSION) >= 0xc01a0165) {
wc->tspans[0]->spanflags |= FLAG_OCTOPT;
}
/* Setup LEDS, take out of reset */
t4_pci_out(wc, WC_LEDS, 0x000000ff);
udelay(100);
t4_activate(wc);
udelay(100);
/* In order to find out the QFALC framer version, we have to
* temporarily turn off compat mode and take a peak at VSTR. We turn
* compat back on when we are done.
*
*/
spin_lock_irqsave(&wc->reglock, flags);
regval = __t4_framer_in(wc, 0, 0xd6);
if (is_octal(wc))
regval |= (1 << 4); /* SSI16 - For 16 MHz multiplex mode with comp = 1 */
else
regval |= (1 << 5); /* set COMP_DIS*/
__t4_framer_out(wc, 0, 0xd6, regval);
spin_unlock_irqrestore(&wc->reglock, flags);
if (!is_octal(wc)) {
regval = t4_framer_in(wc, 0, 0x4a);
if (first_time && regval == 0x05) {
dev_info(&wc->dev->dev, "FALC Framer Version: 2.1 or "
"earlier\n");
} else if (regval == 0x20) {
if (first_time)
dev_info(&wc->dev->dev, "FALC Framer Version: 3.1\n");
wc->falc31 = 1;
} else if (first_time) {
dev_info(&wc->dev->dev, "FALC Framer Version: Unknown "
"(VSTR = 0x%02x)\n", regval);
}
}
spin_lock_irqsave(&wc->reglock, flags);
regval = __t4_framer_in(wc, 0, 0xd6);
regval &= ~(1 << 5); /* clear COMP_DIS*/
__t4_framer_out(wc, 0, 0xd6, regval);
__t4_framer_out(wc, 0, 0x4a, 0xaa);
spin_unlock_irqrestore(&wc->reglock, flags);
if (debug) {
dev_info(&wc->dev->dev, "Board ID: %02x\n", wc->order);
for (x = 0; x < 11; x++) {
dev_info(&wc->dev->dev, "Reg %d: 0x%08x\n", x,
t4_pci_in(wc, x));
}
}
wc->gpio = 0x00000000;
t4_pci_out(wc, WC_GPIO, wc->gpio);
t4_gpio_setdir(wc, (1 << 17), (1 << 17));
t4_gpio_setdir(wc, (0xff), (0xff));
return 0;
}
static int t4_hardware_init_2(struct t4 *wc)
{
return __t4_hardware_init_2(wc, true);
}
static int __devinit t4_launch(struct t4 *wc)
{
int x;
int res;
if (test_bit(DAHDI_FLAGBIT_REGISTERED, &wc->tspans[0]->span.flags))
return 0;
if (debug) {
dev_info(&wc->dev->dev,
"TE%dXXP: Launching card: %d\n", wc->numspans,
wc->order);
}
wc->ddev->manufacturer = "Digium";
if (!ignore_rotary && (1 == order_index[wc->order])) {
wc->ddev->location = kasprintf(GFP_KERNEL,
"Board ID Switch %d", wc->order);
} else {
bool express = ((wc->tspans[0]->spanflags & FLAG_EXPRESS) > 0);
wc->ddev->location = kasprintf(GFP_KERNEL,
"PCI%s Bus %02d Slot %02d",
(express) ? " Express" : "",
wc->dev->bus->number,
PCI_SLOT(wc->dev->devfn) + 1);
}
if (!wc->ddev->location)
return -ENOMEM;
for (x = 0; x < wc->numspans; ++x) {
list_add_tail(&wc->tspans[x]->span.device_node,
&wc->ddev->spans);
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
INIT_WORK(&wc->bh_work, t4_work_func, wc);
#else
INIT_WORK(&wc->bh_work, t4_work_func);
#endif
res = dahdi_register_device(wc->ddev, &wc->dev->dev);
if (res) {
dev_err(&wc->dev->dev, "Failed to register with DAHDI.\n");
return res;
}
return 0;
}
/**
* wct4xxp_sort_cards - Sort the cards in card array by rotary switch settings.
*
*/
static void wct4xxp_sort_cards(void)
{
int x;
/* get the current number of probed cards and run a slice of a tail
* insertion sort */
for (x = 0; x < MAX_T4_CARDS; x++) {
if (!cards[x+1])
break;
}
for ( ; x > 0; x--) {
if (cards[x]->order < cards[x-1]->order) {
struct t4 *tmp = cards[x];
cards[x] = cards[x-1];
cards[x-1] = tmp;
} else {
/* if we're not moving it, we won't move any more
* since all cards are sorted on addition */
break;
}
}
}
static int __devinit
t4_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
{
int res;
struct t4 *wc;
unsigned int x;
int init_latency;
if (pci_enable_device(pdev)) {
return -EIO;
}
wc = kzalloc(sizeof(*wc), GFP_KERNEL);
if (!wc)
return -ENOMEM;
wc->ddev = dahdi_create_device();
if (!wc->ddev) {
kfree(wc);
return -ENOMEM;
}
spin_lock_init(&wc->reglock);
wc->devtype = (const struct devtype *)(ent->driver_data);
#ifdef CONFIG_WCT4XXP_DISABLE_ASPM
if (is_pcie(wc)) {
pci_disable_link_state(pdev->bus->self, PCIE_LINK_STATE_L0S |
PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
};
#endif
if (is_octal(wc))
wc->numspans = 8;
else if (wc->devtype->flags & FLAG_2PORT)
wc->numspans = 2;
else
wc->numspans = 4;
wc->membase = pci_iomap(pdev, 0, 0);
/* This rids of the Double missed interrupt message after loading */
wc->last0 = 1;
if (pci_request_regions(pdev, wc->devtype->desc))
dev_info(&pdev->dev, "wct%dxxp: Unable to request regions\n",
wc->numspans);
if (debug)
dev_info(&pdev->dev, "Found TE%dXXP\n", wc->numspans);
wc->dev = pdev;
/* Enable bus mastering */
pci_set_master(pdev);
/* Keep track of which device we are */
pci_set_drvdata(pdev, wc);
if (wc->devtype->flags & FLAG_5THGEN) {
if ((ms_per_irq > 1) && (latency <= ((ms_per_irq) << 1))) {
init_latency = ms_per_irq << 1;
} else {
if (latency > 2)
init_latency = latency;
else
init_latency = 2;
}
dev_info(&wc->dev->dev, "5th gen card with initial latency of "
"%d and %d ms per IRQ\n", init_latency, ms_per_irq);
} else {
if (wc->devtype->flags & FLAG_2NDGEN)
init_latency = 1;
else
init_latency = 2;
}
if (max_latency < init_latency) {
printk(KERN_INFO "maxlatency must be set to something greater than %d ms, increasing it to %d\n", init_latency, init_latency);
max_latency = init_latency;
}
if (t4_allocate_buffers(wc, init_latency, NULL, NULL)) {
return -ENOMEM;
}
/* Initialize hardware */
res = t4_hardware_init_1(wc, wc->devtype->flags);
if (res) {
/* If this function returns -EAGAIN, we expect
* to attempt another driver load. Clean everything
* up first */
pci_iounmap(wc->dev, wc->membase);
pci_release_regions(wc->dev);
pci_free_consistent(wc->dev, T4_BASE_SIZE(wc) * wc->numbufs * 2,
wc->writechunk, wc->writedma);
pci_set_drvdata(wc->dev, NULL);
free_wc(wc);
return res;
}
for(x = 0; x < MAX_T4_CARDS; x++) {
if (!cards[x])
break;
}
if (x >= MAX_T4_CARDS) {
dev_notice(&wc->dev->dev, "No cards[] slot available!!\n");
kfree(wc);
return -ENOMEM;
}
wc->num = x;
cards[x] = wc;
#ifdef ENABLE_WORKQUEUES
if (wc->devtype->flags & FLAG_2NDGEN) {
char tmp[20];
sprintf(tmp, "te%dxxp[%d]", wc->numspans, wc->num);
wc->workq = create_workqueue(tmp);
}
#endif
/* Allocate pieces we need here */
for (x = 0; x < ports_on_framer(wc); x++) {
struct t4_span *ts;
enum linemode linemode;
ts = kzalloc(sizeof(*ts), GFP_KERNEL);
if (!ts) {
free_wc(wc);
return -ENOMEM;
}
wc->tspans[x] = ts;
#ifdef ENABLE_WORKQUEUES
INIT_WORK(&ts->swork, workq_handlespan, ts);
#endif
ts->spanflags |= wc->devtype->flags;
linemode = (wc->t1e1 & (1 << x)) ? E1 : ((j1mode) ? J1 : T1);
t4_alloc_channels(wc, wc->tspans[x], linemode);
}
/* Continue hardware intiialization */
t4_hardware_init_2(wc);
#ifdef SUPPORT_GEN1
if (request_irq(pdev->irq, (wc->devtype->flags & FLAG_2NDGEN) ?
t4_interrupt_gen2 : t4_interrupt,
IRQF_SHARED,
(wc->numspans == 8) ? "wct8xxp" :
(wc->numspans == 2) ? "wct2xxp" :
"wct4xxp",
wc)) {
#else
if (!(wc->tspans[0]->spanflags & FLAG_2NDGEN)) {
dev_notice(&wc->dev->dev, "This driver does not "
"support 1st gen modules\n");
free_wc(wc);
return -ENODEV;
}
if (request_irq(pdev->irq, t4_interrupt_gen2,
IRQF_SHARED, "t4xxp", wc)) {
#endif
dev_notice(&wc->dev->dev, "Unable to request IRQ %d\n",
pdev->irq);
free_wc(wc);
return -EIO;
}
init_spans(wc);
if (!ignore_rotary)
wct4xxp_sort_cards();
if (wc->ddev->hardware_id) {
dev_info(&wc->dev->dev,
"Found a Wildcard: %s (SN: %s)\n", wc->devtype->desc,
wc->ddev->hardware_id);
} else {
dev_info(&wc->dev->dev,
"Found a Wildcard: %s\n", wc->devtype->desc);
}
#ifdef VPM_SUPPORT
if (!wc->vpm) {
t4_vpm_init(wc);
wc->dmactrl |= (wc->vpm) ? T4_VPM_PRESENT : 0;
t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
if (wc->vpm)
set_span_devicetype(wc);
}
#endif
create_sysfs_files(wc);
res = 0;
if (ignore_rotary)
res = t4_launch(wc);
return res;
}
static int t4_hardware_stop(struct t4 *wc)
{
/* Turn off DMA, leave interrupts enabled */
set_bit(T4_STOP_DMA, &wc->checkflag);
/* Wait for interrupts to stop */
msleep(25);
/* Turn off counter, address, etc */
if (wc->tspans[0]->spanflags & FLAG_2NDGEN) {
t4_tsi_reset(wc);
} else {
t4_pci_out(wc, WC_COUNT, 0x000000);
}
t4_pci_out(wc, WC_RDADDR, 0x0000000);
t4_pci_out(wc, WC_WRADDR, 0x0000000);
wc->gpio = 0x00000000;
t4_pci_out(wc, WC_GPIO, wc->gpio);
t4_pci_out(wc, WC_LEDS, 0x00000000);
if (debug) {
dev_notice(&wc->dev->dev, "Stopped TE%dXXP, Turned off DMA\n",
wc->numspans);
}
return 0;
}
static int __devinit
t4_init_one_retry(struct pci_dev *pdev, const struct pci_device_id *ent)
{
int res;
res = t4_init_one(pdev, ent);
/* If the driver was reset by a firmware load,
* try to load once again */
if (-EAGAIN == res) {
res = t4_init_one(pdev, ent);
if (-EAGAIN == res) {
dev_err(&pdev->dev, "Failed to update firmware.\n");
res = -EIO;
}
}
return res;
}
static void _t4_remove_one(struct t4 *wc)
{
int basesize;
if (!wc)
return;
dahdi_unregister_device(wc->ddev);
remove_sysfs_files(wc);
/* Stop hardware */
t4_hardware_stop(wc);
#ifdef VPM_SUPPORT
/* Release vpm */
if (wc->vpm)
release_vpm450m(wc->vpm);
wc->vpm = NULL;
#endif
/* Unregister spans */
basesize = DAHDI_MAX_CHUNKSIZE * 32 * 4;
if (!(wc->tspans[0]->spanflags & FLAG_2NDGEN))
basesize = basesize * 2;
#ifdef ENABLE_WORKQUEUES
if (wc->workq) {
flush_workqueue(wc->workq);
destroy_workqueue(wc->workq);
}
#endif
free_irq(wc->dev->irq, wc);
if (wc->membase)
pci_iounmap(wc->dev, wc->membase);
pci_release_regions(wc->dev);
/* Immediately free resources */
pci_free_consistent(wc->dev, T4_BASE_SIZE(wc) * wc->numbufs * 2,
wc->writechunk, wc->writedma);
order_index[wc->order]--;
cards[wc->num] = NULL;
pci_set_drvdata(wc->dev, NULL);
free_wc(wc);
}
static void __devexit t4_remove_one(struct pci_dev *pdev)
{
struct t4 *wc = pci_get_drvdata(pdev);
if (!wc)
return;
_t4_remove_one(wc);
}
static DEFINE_PCI_DEVICE_TABLE(t4_pci_tbl) =
{
{ 0x10ee, 0x0314, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long)&wct4xxp },
{ 0xd161, 0x1820, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long)&wct820p5 },
{ 0xd161, 0x1420, 0x0005, PCI_ANY_ID, 0, 0, (unsigned long)&wct420p5 },
{ 0xd161, 0x1410, 0x0005, PCI_ANY_ID, 0, 0, (unsigned long)&wct410p5 },
{ 0xd161, 0x1405, 0x0005, PCI_ANY_ID, 0, 0, (unsigned long)&wct405p5 },
{ 0xd161, 0x0420, 0x0004, PCI_ANY_ID, 0, 0, (unsigned long)&wct420p4 },
{ 0xd161, 0x0410, 0x0004, PCI_ANY_ID, 0, 0, (unsigned long)&wct410p4 },
{ 0xd161, 0x0405, 0x0004, PCI_ANY_ID, 0, 0, (unsigned long)&wct405p4 },
{ 0xd161, 0x0410, 0x0003, PCI_ANY_ID, 0, 0, (unsigned long)&wct410p3 },
{ 0xd161, 0x0405, 0x0003, PCI_ANY_ID, 0, 0, (unsigned long)&wct405p3 },
{ 0xd161, 0x0410, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long)&wct410p2 },
{ 0xd161, 0x0405, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long)&wct405p2 },
{ 0xd161, 0x1220, 0x0005, PCI_ANY_ID, 0, 0, (unsigned long)&wct220p5 },
{ 0xd161, 0x1205, 0x0005, PCI_ANY_ID, 0, 0, (unsigned long)&wct205p5 },
{ 0xd161, 0x1210, 0x0005, PCI_ANY_ID, 0, 0, (unsigned long)&wct210p5 },
{ 0xd161, 0x0220, 0x0004, PCI_ANY_ID, 0, 0, (unsigned long)&wct220p4 },
{ 0xd161, 0x0205, 0x0004, PCI_ANY_ID, 0, 0, (unsigned long)&wct205p4 },
{ 0xd161, 0x0210, 0x0004, PCI_ANY_ID, 0, 0, (unsigned long)&wct210p4 },
{ 0xd161, 0x0205, 0x0003, PCI_ANY_ID, 0, 0, (unsigned long)&wct205p3 },
{ 0xd161, 0x0210, 0x0003, PCI_ANY_ID, 0, 0, (unsigned long)&wct210p3 },
{ 0xd161, 0x0205, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long)&wct205 },
{ 0xd161, 0x0210, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long)&wct210 },
{ 0, }
};
static void _t4_shutdown(struct pci_dev *pdev)
{
struct t4 *wc = pci_get_drvdata(pdev);
t4_hardware_stop(wc);
}
static int t4_suspend(struct pci_dev *pdev, pm_message_t state)
{
return -ENOSYS;
}
static struct pci_driver t4_driver = {
.name = "wct4xxp",
.probe = t4_init_one_retry,
.remove = __devexit_p(t4_remove_one),
.shutdown = _t4_shutdown,
.suspend = t4_suspend,
.id_table = t4_pci_tbl,
};
static int __init t4_init(void)
{
int i;
int res;
if (-1 != t1e1override) {
pr_info("'t1e1override' module parameter is deprecated. "
"Please use 'default_linemode' instead.\n");
}
res = dahdi_pci_module(&t4_driver);
if (res)
return -ENODEV;
/* If we're ignoring the rotary switch settings, then we've already
* registered in the context of .probe */
if (!ignore_rotary) {
/* Initialize cards since we have all of them. Warn for
* missing zero and duplicate numbers. */
if (cards[0] && cards[0]->order != 0) {
printk(KERN_NOTICE "wct4xxp: Ident of first card is not zero (%d)\n",
cards[0]->order);
}
for (i = 0; cards[i]; i++) {
/* warn the user of duplicate ident values it is
* probably unintended */
if (debug && res < 15 && cards[i+1] &&
cards[res]->order == cards[i+1]->order) {
printk(KERN_NOTICE "wct4xxp: Duplicate ident "
"value found (%d)\n", cards[i]->order);
}
res = t4_launch(cards[i]);
if (res) {
int j;
for (j = 0; j < i; ++j)
_t4_remove_one(cards[j]);
break;
}
}
}
return res;
}
static void __exit t4_cleanup(void)
{
pci_unregister_driver(&t4_driver);
}
MODULE_AUTHOR("Digium Incorporated <support@digium.com>");
MODULE_DESCRIPTION("Wildcard Dual/Quad/Octal-port Digital Card Driver");
MODULE_ALIAS("wct2xxp");
MODULE_LICENSE("GPL v2");
module_param(debug, int, 0600);
module_param(noburst, int, 0600);
module_param(timingcable, int, 0600);
module_param(t1e1override, int, 0400);
module_param(default_linemode, charp, S_IRUGO);
MODULE_PARM_DESC(default_linemode, "\"auto\"(default), \"e1\", \"t1\", "
"or \"j1\". \"auto\" will use the value from any hardware "
"jumpers.");
module_param(alarmdebounce, int, 0600);
module_param(losalarmdebounce, int, 0600);
module_param(aisalarmdebounce, int, 0600);
module_param(yelalarmdebounce, int, 0600);
module_param(max_latency, int, 0600);
module_param(j1mode, int, 0600);
module_param(sigmode, int, 0600);
module_param(latency, int, 0600);
module_param(ms_per_irq, int, 0600);
module_param(ignore_rotary, int, 0400);
MODULE_PARM_DESC(ignore_rotary, "Set to > 0 to ignore the rotary switch when " \
"registering with DAHDI.");
#ifdef VPM_SUPPORT
module_param(vpmsupport, int, 0600);
module_param(vpmdtmfsupport, int, 0600);
#endif
MODULE_DEVICE_TABLE(pci, t4_pci_tbl);
module_init(t4_init);
module_exit(t4_cleanup);
|