ehci.c 95.5 KB
Newer Older
W
wenjun 已提交
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
/* $FreeBSD: releng/11.4/sys/dev/usb/controller/ehci.c 331722 2018-03-29 02:50:57Z eadler $ */
/*-
 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
 * Copyright (c) 2004 The NetBSD Foundation, Inc. All rights reserved.
 * Copyright (c) 2004 Lennart Augustsson. All rights reserved.
 * Copyright (c) 2004 Charles M. Hannum. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
 *
 * The EHCI 0.96 spec can be found at
 * http://developer.intel.com/technology/usb/download/ehci-r096.pdf
 * The EHCI 1.0 spec can be found at
 * http://developer.intel.com/technology/usb/download/ehci-r10.pdf
 * and the USB 2.0 spec at
 * http://www.usb.org/developers/docs/usb_20.zip
 *
 */

#include "implementation/global_implementation.h"
#include "controller/ehci.h"
#include "controller/ehcireg.h"


#define	EHCI_BUS2SC(bus) \
   ((ehci_softc_t *)(((uint8_t *)(bus)) - \
    ((uint8_t *)&(((ehci_softc_t *)0)->sc_bus))))

#undef	USB_DEBUG_VAR
#define	USB_DEBUG_VAR   ehcidebug

#ifdef LOSCFG_USB_DEBUG
static int ehcidebug = 0;
static int ehciiaadbug = 0;
static int ehcilostintrbug = 0;

static void ehci_dump_regs(ehci_softc_t *sc);
static void ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *sqh);

void
usb_ehci_debug_func(int level)
{
	ehcidebug = level;
	PRINTK("The level of usb ehci debug is %d\n", level);
}
DEBUG_MODULE(ehci, usb_ehci_debug_func);
#endif

SPIN_LOCK_INIT(g_usb_ehci_qh_spinlock);

#define	EHCI_INTR_ENDPT 1

extern const struct usb_bus_methods ehci_bus_methods;
extern const struct usb_pipe_methods ehci_device_bulk_methods;
extern const struct usb_pipe_methods ehci_device_ctrl_methods;
extern const struct usb_pipe_methods ehci_device_intr_methods;
extern const struct usb_pipe_methods ehci_device_isoc_fs_methods;
extern const struct usb_pipe_methods ehci_device_isoc_hs_methods;

static void ehci_do_poll(struct usb_bus *);
static void ehci_device_done(struct usb_xfer *, usb_error_t);
static uint8_t ehci_check_transfer(struct usb_xfer *);
static void ehci_timeout(void *);
static void ehci_poll_timeout(void *);

static void ehci_root_intr(ehci_softc_t *sc);

struct ehci_std_temp {
	ehci_softc_t *sc;
	struct usb_page_cache *pc;
	ehci_qtd_t *td;
	ehci_qtd_t *td_next;
	uint32_t average;
	uint32_t qtd_status;
	uint32_t len;
	uint16_t max_frame_size;
	uint8_t	shortpkt;
	uint8_t	auto_data_toggle;
	uint8_t	setup_alt_next;
	uint8_t	last_frame;
};

/* cb: usb_bus_mem_alloc_all_cb */
/* usb_bus_mem_flush_all_cb */
void
ehci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
{
	ehci_softc_t *sc = EHCI_BUS2SC(bus);
	uint32_t i;

	cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
	    sizeof(uint32_t) * EHCI_FRAMELIST_COUNT, EHCI_FRAMELIST_ALIGN);

	cb(bus, &sc->sc_hw.terminate_pc, &sc->sc_hw.terminate_pg,
	    sizeof(struct ehci_qh_sub), EHCI_QH_ALIGN);

	cb(bus, &sc->sc_hw.async_start_pc, &sc->sc_hw.async_start_pg,
	    sizeof(ehci_qh_t), EHCI_QH_ALIGN);

	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
		cb(bus, sc->sc_hw.intr_start_pc + i,
		    sc->sc_hw.intr_start_pg + i,
		    sizeof(ehci_qh_t), EHCI_QH_ALIGN);
	}

	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
		cb(bus, sc->sc_hw.isoc_hs_start_pc + i,
		    sc->sc_hw.isoc_hs_start_pg + i,
		    sizeof(ehci_itd_t), EHCI_ITD_ALIGN);
	}

	for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
		cb(bus, sc->sc_hw.isoc_fs_start_pc + i,
		    sc->sc_hw.isoc_fs_start_pg + i,
		    sizeof(ehci_sitd_t), EHCI_SITD_ALIGN);
	}
}

usb_error_t
ehci_reset(ehci_softc_t *sc)
{
	uint32_t hcr;
	int i;

	EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
	for (i = 0; i < 100; i++) {
		usb_pause_mtx(NULL, hz / 128);
		hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
		if (!hcr) {
			if (sc->sc_vendor_post_reset != NULL)
				sc->sc_vendor_post_reset(sc);
			return (USB_ERR_NORMAL_COMPLETION);
		}
	}
	device_printf(sc->sc_bus.bdev, "Reset timeout\n");
	return (USB_ERR_IOERROR);
}

static usb_error_t
ehci_hcreset(ehci_softc_t *sc)
{
	uint32_t hcr = 0;
	int i;

	EOWRITE4(sc, EHCI_USBCMD, 0);	/* Halt controller */
	for (i = 0; i < 100; i++) {
		usb_pause_mtx(NULL, hz / 128);
		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
		if (hcr)
			break;
	}
	if (!hcr)
		/*
		 * Fall through and try reset anyway even though
		 * Table 2-9 in the EHCI spec says this will result
		 * in undefined behavior.
		 */
		device_printf(sc->sc_bus.bdev, "stop timeout\n");

	return (ehci_reset(sc));
}

static usb_error_t
ehci_init_sub(struct ehci_softc *sc)
{
	struct usb_page_search buf_res;
	uint32_t cparams;
	uint32_t hcr = 0;
	uint8_t i;

	cparams = EREAD4(sc, EHCI_HCCPARAMS);

	DPRINTF("cparams=0x%x\n", cparams);

	if (EHCI_HCC_64BIT(cparams)) {
		DPRINTF("HCC uses 64-bit structures\n");

		/* MUST clear segment register if 64 bit capable */
		EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
	}

	usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
	EOWRITE4(sc, EHCI_PERIODICLISTBASE, buf_res.physaddr);

	usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);
	EOWRITE4(sc, EHCI_ASYNCLISTADDR, buf_res.physaddr | EHCI_LINK_QH);

	/* enable interrupts */
	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);

	/* turn on controller */
	EOWRITE4(sc, EHCI_USBCMD,
	    EHCI_CMD_ITC_1 |		/* 1 microframes interrupt delay */
	    (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
	    EHCI_CMD_ASE |
	    EHCI_CMD_PSE |
	    EHCI_CMD_RS);

	/* Take over port ownership */
	EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);

	for (i = 0; i < 100; i++) {
		usb_pause_mtx(NULL, hz / 128);
		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
		if (!hcr) {
			break;
		}
	}
	if (hcr) {
		device_printf(sc->sc_bus.bdev, "run timeout\n");
		return (USB_ERR_IOERROR);
	}
	return (USB_ERR_NORMAL_COMPLETION);
}

usb_error_t
ehci_init(ehci_softc_t *sc)
{
	struct usb_page_search buf_res;
	struct ehci_qh_sub *qh_sub;
	ehci_qh_t *qh_t;
	uint32_t version;
	uint32_t sparams;
	uint32_t *pframes;
	uint16_t i;
	uint16_t x;
	uint16_t y;
	uint16_t bit;
	usb_error_t err = USB_ERR_NORMAL_COMPLETION;

	DPRINTF("start\n");

	callout_init_mtx(&sc->sc_tmo_pcd, &sc->sc_bus.bus_mtx, 0);
	callout_init_mtx(&sc->sc_tmo_poll, &sc->sc_bus.bus_mtx, 0);

	sc->sc_offs = EHCI_CAPLENGTH(EREAD4(sc, EHCI_CAPLEN_HCIVERSION));

#ifdef LOSCFG_USB_DEBUG
	if (ehciiaadbug)
		sc->sc_flags |= EHCI_SCFLG_IAADBUG;
	if (ehcilostintrbug)
		sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
	if (ehcidebug > 2) {
		ehci_dump_regs(sc);
	}
#endif

	version = EHCI_HCIVERSION(EREAD4(sc, EHCI_CAPLEN_HCIVERSION));
	device_printf(sc->sc_bus.bdev, "EHCI version %x.%x\n",
	    version >> 8, version & 0xff);

	sparams = EREAD4(sc, EHCI_HCSPARAMS);
	DPRINTF("sparams=0x%x\n", sparams);

	sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
	sc->sc_bus.usbrev = USB_REV_2_0;
	if (!(sc->sc_flags & EHCI_SCFLG_DONTRESET)) {
		/* Reset the controller */
		DPRINTF("%s: resetting\n",
		    device_get_nameunit(sc->sc_bus.bdev));
		err = ehci_hcreset(sc);
		if (err) {
			device_printf(sc->sc_bus.bdev, "reset timeout\n");
			return (err);
		}
	}

	/*
	 * use current frame-list-size selection 0: 1024*4 bytes 1:  512*4
	 * bytes 2:  256*4 bytes 3:	  unknown
	 */
	if (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD)) == 3) {
		device_printf(sc->sc_bus.bdev, "invalid frame-list-size\n");
		return (USB_ERR_IOERROR);
	}
	/* set up the bus struct */
	sc->sc_bus.methods = &ehci_bus_methods;

	sc->sc_eintrs = EHCI_NORMAL_INTRS;

	usbd_get_page(&sc->sc_hw.terminate_pc, 0, &buf_res);
	qh_sub = (struct ehci_qh_sub *)buf_res.buffer;

	sc->sc_terminate_self = htohc32(sc, buf_res.physaddr);

	/* init terminate TD */
	qh_sub->qtd_next =
	    htohc32(sc, EHCI_LINK_TERMINATE);
	qh_sub->qtd_altnext =
	    htohc32(sc, EHCI_LINK_TERMINATE);
	qh_sub->qtd_status =
	    htohc32(sc, EHCI_QTD_HALTED);

	for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
		usbd_get_page(sc->sc_hw.intr_start_pc + i, 0, &buf_res);

		qh_t = (ehci_qh_t *)buf_res.buffer;

		/* initialize page cache pointer */

		qh_t->page_cache = sc->sc_hw.intr_start_pc + i;

		/* store a pointer to queue head */

		sc->sc_intr_p_last[i] = qh_t;

		qh_t->qh_self =
		    htohc32(sc, buf_res.physaddr) |
		    htohc32(sc, EHCI_LINK_QH);

		qh_t->qh_endp =
		    htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
		qh_t->qh_endphub =
		    htohc32(sc, EHCI_QH_SET_MULT(1));
		qh_t->qh_curqtd = 0;

		qh_t->qh_qtd.qtd_next =
		    htohc32(sc, EHCI_LINK_TERMINATE);
		qh_t->qh_qtd.qtd_altnext =
		    htohc32(sc, EHCI_LINK_TERMINATE);
		qh_t->qh_qtd.qtd_status =
		    htohc32(sc, EHCI_QTD_HALTED);
	}

	/*
	 * the QHs are arranged to give poll intervals that are
	 * powers of 2 times 1ms
	 */
	bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
	while (bit) {
		x = bit;
		while (x & bit) {
			ehci_qh_t *qh_x;
			ehci_qh_t *qh_y;

			y = (x ^ bit) | (bit / 2);

			qh_x = sc->sc_intr_p_last[x];
			qh_y = sc->sc_intr_p_last[y];

			/*
			 * the next QH has half the poll interval
			 */
			qh_x->qh_link = qh_y->qh_self;

			x++;
		}
		bit >>= 1;
	}

	qh_t = sc->sc_intr_p_last[0];

	/* the last (1ms) QH terminates */
	qh_t->qh_link = htohc32(sc, EHCI_LINK_TERMINATE);

	for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) {
		ehci_sitd_t *sitd;
		ehci_itd_t *itd;

		usbd_get_page(sc->sc_hw.isoc_fs_start_pc + i, 0, &buf_res);
		sitd = (ehci_sitd_t *)buf_res.buffer;

		/* initialize page cache pointer */

		sitd->page_cache = sc->sc_hw.isoc_fs_start_pc + i;

		/* store a pointer to the transfer descriptor */

		sc->sc_isoc_fs_p_last[i] = sitd;

		/* initialize full speed isochronous */

		sitd->sitd_self =
		    htohc32(sc, buf_res.physaddr) |
		    htohc32(sc, EHCI_LINK_SITD);

		sitd->sitd_back =
		    htohc32(sc, EHCI_LINK_TERMINATE);

		sitd->sitd_next =
		    sc->sc_intr_p_last[i | (EHCI_VIRTUAL_FRAMELIST_COUNT / 2)]->qh_self;


		usbd_get_page(sc->sc_hw.isoc_hs_start_pc + i, 0, &buf_res);

		itd = (ehci_itd_t *)buf_res.buffer;

		/* initialize page cache pointer */

		itd->page_cache = sc->sc_hw.isoc_hs_start_pc + i;

		/* store a pointer to the transfer descriptor */

		sc->sc_isoc_hs_p_last[i] = itd;

		/* initialize high speed isochronous */

		itd->itd_self =
		    htohc32(sc, buf_res.physaddr) |
		    htohc32(sc, EHCI_LINK_ITD);

		itd->itd_next =
		    sitd->sitd_self;
	}

	usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);

	pframes = (uint32_t *)buf_res.buffer;

	/*
	 * execution order:
	 * pframes -> high speed isochronous ->
	 *    full speed isochronous -> interrupt QH's
	 */
	for (i = 0; i < EHCI_FRAMELIST_COUNT; i++) {
		pframes[i] = sc->sc_isoc_hs_p_last
		    [i & (EHCI_VIRTUAL_FRAMELIST_COUNT - 1)]->itd_self;
	}

	usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res);

	qh_t = (ehci_qh_t *)buf_res.buffer;

	/* initialize page cache pointer */

	qh_t->page_cache = &sc->sc_hw.async_start_pc;

	/* store a pointer to the queue head */

	sc->sc_async_p_last = qh_t;

	/* init dummy QH that starts the async list */

	qh_t->qh_self =
	    htohc32(sc, buf_res.physaddr) |
	    htohc32(sc, EHCI_LINK_QH);

	/* fill the QH */
	qh_t->qh_endp =
	    htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
	qh_t->qh_endphub = htohc32(sc, EHCI_QH_SET_MULT(1));
	qh_t->qh_link = qh_t->qh_self;
	qh_t->qh_curqtd = 0;

	/* fill the overlay qTD */
	qh_t->qh_qtd.qtd_next = htohc32(sc, EHCI_LINK_TERMINATE);
	qh_t->qh_qtd.qtd_altnext = htohc32(sc, EHCI_LINK_TERMINATE);
	qh_t->qh_qtd.qtd_status = htohc32(sc, EHCI_QTD_HALTED);

	/* flush all cache into memory */

	usb_bus_mem_flush_all(&sc->sc_bus, &ehci_iterate_hw_softc);

#ifdef LOSCFG_USB_DEBUG
	if (ehcidebug) {
		ehci_dump_sqh(sc, sc->sc_async_p_last);
	}
#endif

	/* finial setup */
	err = ehci_init_sub(sc);

	if (!err) {
		/* catch any lost interrupts */
		ehci_do_poll(&sc->sc_bus);
	}
	return (err);
}

/*
 * shut down the controller when the system is going down
 */
void
ehci_detach(ehci_softc_t *sc)
{
	USB_BUS_LOCK(&sc->sc_bus);

	callout_stop(&sc->sc_tmo_pcd);
	callout_stop(&sc->sc_tmo_poll);

	EOWRITE4(sc, EHCI_USBINTR, 0);
	USB_BUS_UNLOCK(&sc->sc_bus);

	if (ehci_hcreset(sc)) {
		DPRINTF("reset failed!\n");
	}

	/* XXX let stray task complete */
	usb_pause_mtx(NULL, hz / 20);

	callout_drain(&sc->sc_tmo_pcd);
	callout_drain(&sc->sc_tmo_poll);
}

static void
ehci_suspend(ehci_softc_t *sc)
{
	DPRINTF("stopping the HC\n");

	/* reset HC */
	(void)ehci_hcreset(sc);
}

static void
ehci_resume(ehci_softc_t *sc)
{
	/* reset HC */
	(void)ehci_hcreset(sc);

	/* setup HC */
	(void)ehci_init_sub(sc);

	/* catch any lost interrupts */
	ehci_do_poll(&sc->sc_bus);
}

#ifdef LOSCFG_USB_DEBUG
static void
ehci_dump_regs(ehci_softc_t *sc)
{
	uint32_t i;

	i = EOREAD4(sc, EHCI_USBCMD);
	PRINTK("cmd=0x%08x\n", i);

	if (i & EHCI_CMD_ITC_1)
		PRINTK(" EHCI_CMD_ITC_1\n");
	if (i & EHCI_CMD_ITC_2)
		PRINTK(" EHCI_CMD_ITC_2\n");
	if (i & EHCI_CMD_ITC_4)
		PRINTK(" EHCI_CMD_ITC_4\n");
	if (i & EHCI_CMD_ITC_8)
		PRINTK(" EHCI_CMD_ITC_8\n");
	if (i & EHCI_CMD_ITC_16)
		PRINTK(" EHCI_CMD_ITC_16\n");
	if (i & EHCI_CMD_ITC_32)
		PRINTK(" EHCI_CMD_ITC_32\n");
	if (i & EHCI_CMD_ITC_64)
		PRINTK(" EHCI_CMD_ITC_64\n");
	if (i & EHCI_CMD_ASPME)
		PRINTK(" EHCI_CMD_ASPME\n");
	if (i & EHCI_CMD_ASPMC)
		PRINTK(" EHCI_CMD_ASPMC\n");
	if (i & EHCI_CMD_LHCR)
		PRINTK(" EHCI_CMD_LHCR\n");
	if (i & EHCI_CMD_IAAD)
		PRINTK(" EHCI_CMD_IAAD\n");
	if (i & EHCI_CMD_ASE)
		PRINTK(" EHCI_CMD_ASE\n");
	if (i & EHCI_CMD_PSE)
		PRINTK(" EHCI_CMD_PSE\n");
	if (i & EHCI_CMD_FLS_M)
		PRINTK(" EHCI_CMD_FLS_M\n");
	if (i & EHCI_CMD_HCRESET)
		PRINTK(" EHCI_CMD_HCRESET\n");
	if (i & EHCI_CMD_RS)
		PRINTK(" EHCI_CMD_RS\n");

	i = EOREAD4(sc, EHCI_USBSTS);

	PRINTK("sts=0x%08x\n", i);

	if (i & EHCI_STS_ASS)
		PRINTK(" EHCI_STS_ASS\n");
	if (i & EHCI_STS_PSS)
		PRINTK(" EHCI_STS_PSS\n");
	if (i & EHCI_STS_REC)
		PRINTK(" EHCI_STS_REC\n");
	if (i & EHCI_STS_HCH)
		PRINTK(" EHCI_STS_HCH\n");
	if (i & EHCI_STS_IAA)
		PRINTK(" EHCI_STS_IAA\n");
	if (i & EHCI_STS_HSE)
		PRINTK(" EHCI_STS_HSE\n");
	if (i & EHCI_STS_FLR)
		PRINTK(" EHCI_STS_FLR\n");
	if (i & EHCI_STS_PCD)
		PRINTK(" EHCI_STS_PCD\n");
	if (i & EHCI_STS_ERRINT)
		PRINTK(" EHCI_STS_ERRINT\n");
	if (i & EHCI_STS_INT)
		PRINTK(" EHCI_STS_INT\n");

	PRINTK("intr=0x%08x\n",
	    EOREAD4(sc, EHCI_USBINTR));
	PRINTK("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
	    EOREAD4(sc, EHCI_FRINDEX),
	    EOREAD4(sc, EHCI_CTRLDSSEGMENT),
	    EOREAD4(sc, EHCI_PERIODICLISTBASE),
	    EOREAD4(sc, EHCI_ASYNCLISTADDR));
	for (i = 1; i <= sc->sc_noport; i++) {
		PRINTK("port %d status=0x%08x\n", i,
		    EOREAD4(sc, EHCI_PORTSC(i)));
	}
}

static void
ehci_dump_link(ehci_softc_t *sc, uint32_t link, int type)
{
	link = hc32toh(sc, link);
	PRINTK("0x%08x", link);
	if (link & EHCI_LINK_TERMINATE)
		PRINTK("<T>");
	else {
		PRINTK("<");
		if (type) {
			switch (EHCI_LINK_TYPE(link)) {
			case EHCI_LINK_ITD:
				PRINTK("ITD");
				break;
			case EHCI_LINK_QH:
				PRINTK("QH");
				break;
			case EHCI_LINK_SITD:
				PRINTK("SITD");
				break;
			case EHCI_LINK_FSTN:
				PRINTK("FSTN");
				break;
			}
		}
		PRINTK(">");
	}
}

static void
ehci_dump_qtd(ehci_softc_t *sc, ehci_qtd_t *qtd)
{
	uint32_t s;

	PRINTK("  next=");
	ehci_dump_link(sc, qtd->qtd_next, 0);
	PRINTK(" altnext=");
	ehci_dump_link(sc, qtd->qtd_altnext, 0);
	PRINTK("\n");
	s = hc32toh(sc, qtd->qtd_status);
	PRINTK("  status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
	    s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
	    EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
	PRINTK("    cerr=%d pid=%d stat=%s%s%s%s%s%s%s%s\n",
	    EHCI_QTD_GET_CERR(s), EHCI_QTD_GET_PID(s),
	    (s & EHCI_QTD_ACTIVE) ? "ACTIVE" : "NOT_ACTIVE",
	    (s & EHCI_QTD_HALTED) ? "-HALTED" : "",
	    (s & EHCI_QTD_BUFERR) ? "-BUFERR" : "",
	    (s & EHCI_QTD_BABBLE) ? "-BABBLE" : "",
	    (s & EHCI_QTD_XACTERR) ? "-XACTERR" : "",
	    (s & EHCI_QTD_MISSEDMICRO) ? "-MISSED" : "",
	    (s & EHCI_QTD_SPLITXSTATE) ? "-SPLIT" : "",
	    (s & EHCI_QTD_PINGSTATE) ? "-PING" : "");

	for (s = 0; s < 5; s++) {
		PRINTK("  buffer[%d]=0x%08x\n", s,
		    hc32toh(sc, qtd->qtd_buffer[s]));
	}
	for (s = 0; s < 5; s++) {
		PRINTK("  buffer_hi[%d]=0x%08x\n", s,
		    hc32toh(sc, qtd->qtd_buffer_hi[s]));
	}
}

static uint8_t
ehci_dump_sqtd(ehci_softc_t *sc, ehci_qtd_t *sqtd)
{
	uint8_t temp;

	usb_pc_cpu_invalidate(sqtd->page_cache);
	PRINTK("QTD(%p) at 0x%08x:\n", sqtd, hc32toh(sc, sqtd->qtd_self));
	ehci_dump_qtd(sc, sqtd);
	temp = (sqtd->qtd_next & htohc32(sc, EHCI_LINK_TERMINATE)) ? 1 : 0;
	return (temp);
}

static void
ehci_dump_sqtds(ehci_softc_t *sc, ehci_qtd_t *sqtd)
{
	uint16_t i;
	uint8_t stop;

	stop = 0;
	for (i = 0; sqtd && (i < 20) && !stop; sqtd = sqtd->obj_next, i++) {
		stop = ehci_dump_sqtd(sc, sqtd);
	}
	if (sqtd) {
		PRINTK("dump aborted, too many TDs\n");
	}
}

static void
ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *qh)
{
	uint32_t endp;
	uint32_t endphub;

	usb_pc_cpu_invalidate(qh->page_cache);
	PRINTK("QH(%p) at 0x%08x:\n", qh, hc32toh(sc, qh->qh_self) & ~0x1F);
	PRINTK("  link=");
	ehci_dump_link(sc, qh->qh_link, 1);
	PRINTK("\n");
	endp = hc32toh(sc, qh->qh_endp);
	PRINTK("  endp=0x%08x\n", endp);
	PRINTK("    addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
	    EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
	    EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp),
	    EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
	PRINTK("    mpl=0x%x ctl=%d nrl=%d\n",
	    EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
	    EHCI_QH_GET_NRL(endp));
	endphub = hc32toh(sc, qh->qh_endphub);
	PRINTK("  endphub=0x%08x\n", endphub);
	PRINTK("    smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
	    EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
	    EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
	    EHCI_QH_GET_MULT(endphub));
	PRINTK("  curqtd=");
	ehci_dump_link(sc, qh->qh_curqtd, 0);
	PRINTK("\n");
	PRINTK("Overlay qTD:\n");
	ehci_dump_qtd(sc, (void *)&qh->qh_qtd);
}

static void
ehci_dump_sitd(ehci_softc_t *sc, ehci_sitd_t *sitd)
{
	usb_pc_cpu_invalidate(sitd->page_cache);
	PRINTK("SITD(%p) at 0x%08x\n", sitd, hc32toh(sc, sitd->sitd_self) & ~0x1F);
	PRINTK(" next=0x%08x\n", hc32toh(sc, sitd->sitd_next));
	PRINTK(" portaddr=0x%08x dir=%s addr=%d endpt=0x%x port=0x%x huba=0x%x\n",
	    hc32toh(sc, sitd->sitd_portaddr),
	    (sitd->sitd_portaddr & htohc32(sc, EHCI_SITD_SET_DIR_IN))
	    ? "in" : "out",
	    EHCI_SITD_GET_ADDR(hc32toh(sc, sitd->sitd_portaddr)),
	    EHCI_SITD_GET_ENDPT(hc32toh(sc, sitd->sitd_portaddr)),
	    EHCI_SITD_GET_PORT(hc32toh(sc, sitd->sitd_portaddr)),
	    EHCI_SITD_GET_HUBA(hc32toh(sc, sitd->sitd_portaddr)));
	PRINTK(" mask=0x%08x\n", hc32toh(sc, sitd->sitd_mask));
	PRINTK(" status=0x%08x <%s> len=0x%x\n", hc32toh(sc, sitd->sitd_status),
	    (sitd->sitd_status & htohc32(sc, EHCI_SITD_ACTIVE)) ? "ACTIVE" : "",
	    EHCI_SITD_GET_LEN(hc32toh(sc, sitd->sitd_status)));
	PRINTK(" back=0x%08x, bp=0x%08x,0x%08x,0x%08x,0x%08x\n",
	    hc32toh(sc, sitd->sitd_back),
	    hc32toh(sc, sitd->sitd_bp[0]),
	    hc32toh(sc, sitd->sitd_bp[1]),
	    hc32toh(sc, sitd->sitd_bp_hi[0]),
	    hc32toh(sc, sitd->sitd_bp_hi[1]));
}

static void
ehci_dump_itd(ehci_softc_t *sc, ehci_itd_t *itd)
{
	usb_pc_cpu_invalidate(itd->page_cache);
	PRINTK("ITD(%p) at 0x%08x\n", itd, hc32toh(sc, itd->itd_self) & ~0x1F);
	PRINTK(" next=0x%08x\n", hc32toh(sc, itd->itd_next));
	PRINTK(" status[0]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[0]),
	    (itd->itd_status[0] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
	PRINTK(" status[1]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[1]),
	    (itd->itd_status[1] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
	PRINTK(" status[2]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[2]),
	    (itd->itd_status[2] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
	PRINTK(" status[3]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[3]),
	    (itd->itd_status[3] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
	PRINTK(" status[4]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[4]),
	    (itd->itd_status[4] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
	PRINTK(" status[5]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[5]),
	    (itd->itd_status[5] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
	PRINTK(" status[6]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[6]),
	    (itd->itd_status[6] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
	PRINTK(" status[7]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[7]),
	    (itd->itd_status[7] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : "");
	PRINTK(" bp[0]=0x%08x\n", hc32toh(sc, itd->itd_bp[0]));
	PRINTK("  addr=0x%02x; endpt=0x%01x\n",
	    EHCI_ITD_GET_ADDR(hc32toh(sc, itd->itd_bp[0])),
	    EHCI_ITD_GET_ENDPT(hc32toh(sc, itd->itd_bp[0])));
	PRINTK(" bp[1]=0x%08x\n", hc32toh(sc, itd->itd_bp[1]));
	PRINTK(" dir=%s; mpl=0x%02x\n",
	    (hc32toh(sc, itd->itd_bp[1]) & EHCI_ITD_SET_DIR_IN) ? "in" : "out",
	    EHCI_ITD_GET_MPL(hc32toh(sc, itd->itd_bp[1])));
	PRINTK(" bp[2..6]=0x%08x,0x%08x,0x%08x,0x%08x,0x%08x\n",
	    hc32toh(sc, itd->itd_bp[2]),
	    hc32toh(sc, itd->itd_bp[3]),
	    hc32toh(sc, itd->itd_bp[4]),
	    hc32toh(sc, itd->itd_bp[5]),
	    hc32toh(sc, itd->itd_bp[6]));
	PRINTK(" bp_hi=0x%08x,0x%08x,0x%08x,0x%08x,\n"
	    "      0x%08x,0x%08x,0x%08x\n",
	    hc32toh(sc, itd->itd_bp_hi[0]),
	    hc32toh(sc, itd->itd_bp_hi[1]),
	    hc32toh(sc, itd->itd_bp_hi[2]),
	    hc32toh(sc, itd->itd_bp_hi[3]),
	    hc32toh(sc, itd->itd_bp_hi[4]),
	    hc32toh(sc, itd->itd_bp_hi[5]),
	    hc32toh(sc, itd->itd_bp_hi[6]));
}

static void
ehci_dump_isoc(ehci_softc_t *sc)
{
	ehci_itd_t *itd;
	ehci_sitd_t *sitd;
	uint16_t max = 1000;
	uint16_t pos;

	pos = (EOREAD4(sc, EHCI_FRINDEX) / 8) &
	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);

	PRINTK("%s: isochronous dump from frame 0x%03x:\n",
	    __FUNCTION__, pos);

	itd = sc->sc_isoc_hs_p_last[pos];
	sitd = sc->sc_isoc_fs_p_last[pos];

	while (itd && max && max--) {
		ehci_dump_itd(sc, itd);
		itd = itd->prev;
	}

	while (sitd && max && max--) {
		ehci_dump_sitd(sc, sitd);
		sitd = sitd->prev;
	}
}

#endif

static void
ehci_transfer_intr_enqueue(struct usb_xfer *xfer)
{
	/* check for early completion */
	if (ehci_check_transfer(xfer)) {
		DPRINTFN(0, " ehci_check_transfer return\n");
		return;
	}
	DPRINTFN(7, " enqueue\n");
	/* put transfer on interrupt queue */
	usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);

	/* start timeout, if any */
	if (xfer->timeout != 0) {
		usbd_transfer_timeout_ms(xfer, &ehci_timeout, xfer->timeout);
	}
}

#define	EHCI_APPEND_FS_TD(std, last) (last) = _ehci_append_fs_td(std, last)
static ehci_sitd_t *
_ehci_append_fs_td(ehci_sitd_t *ehci_std, ehci_sitd_t *last)
{
	DPRINTFN(11, "%p to %p\n", ehci_std, last);

	/* (sc->sc_bus.mtx) must be locked */

	ehci_std->next = last->next;
	ehci_std->sitd_next = last->sitd_next;

	ehci_std->prev = last;

	usb_pc_cpu_flush(ehci_std->page_cache);

	/*
	 * the last->next->prev is never followed: std->next->prev = std;
	 */
	last->next = ehci_std;
	last->sitd_next = ehci_std->sitd_self;

	usb_pc_cpu_flush(last->page_cache);

	return (ehci_std);
}

#define	EHCI_APPEND_HS_TD(std, last) (last) = _ehci_append_hs_td(std, last)
static ehci_itd_t *
_ehci_append_hs_td(ehci_itd_t *ehci_std, ehci_itd_t *last)
{
	DPRINTFN(11, "%p to %p\n", ehci_std, last);

	/* (sc->sc_bus.mtx) must be locked */

	ehci_std->next = last->next;
	ehci_std->itd_next = last->itd_next;

	ehci_std->prev = last;

	usb_pc_cpu_flush(ehci_std->page_cache);

	/*
	 * the last->next->prev is never followed: std->next->prev = std;
	 */
	last->next = ehci_std;
	last->itd_next = ehci_std->itd_self;

	usb_pc_cpu_flush(last->page_cache);

	return (ehci_std);
}

#define	EHCI_APPEND_QH(sqh, last) (last) = _ehci_append_qh(sqh, last)
static ehci_qh_t *
_ehci_append_qh(ehci_qh_t *sqh, ehci_qh_t *last)
{
	DPRINTFN(11, "%p to %p\n", sqh, last);

	if (sqh->prev != NULL) {
		/* should not happen */
		DPRINTFN(0, "QH already linked!\n");
		return (last);
	}
	/* (sc->sc_bus.mtx) must be locked */

	sqh->next = last->next;
	sqh->qh_link = last->qh_link;

	sqh->prev = last;

	usb_pc_cpu_flush(sqh->page_cache);

	/*
	 * the last->next->prev is never followed: sqh->next->prev = sqh;
	 */

	last->next = sqh;
	last->qh_link = sqh->qh_self;

	usb_pc_cpu_flush(last->page_cache);

	return (sqh);
}

#define	EHCI_REMOVE_FS_TD(std, last) (last) = _ehci_remove_fs_td(std, last)
static ehci_sitd_t *
_ehci_remove_fs_td(ehci_sitd_t *ehci_std, ehci_sitd_t *last)
{
	DPRINTFN(11, "%p from %p\n", ehci_std, last);

	/* (sc->sc_bus.mtx) must be locked */

	ehci_std->prev->next = ehci_std->next;
	ehci_std->prev->sitd_next = ehci_std->sitd_next;

	usb_pc_cpu_flush(ehci_std->prev->page_cache);

	if (ehci_std->next) {
		ehci_std->next->prev = ehci_std->prev;
		usb_pc_cpu_flush(ehci_std->next->page_cache);
	}
	return ((last == ehci_std) ? ehci_std->prev : last);
}

#define	EHCI_REMOVE_HS_TD(std, last) (last) = _ehci_remove_hs_td(std, last)
static ehci_itd_t *
_ehci_remove_hs_td(ehci_itd_t *ehci_std, ehci_itd_t *last)
{
	DPRINTFN(11, "%p from %p\n", ehci_std, last);

	/* (sc->sc_bus.mtx) must be locked */

	ehci_std->prev->next = ehci_std->next;
	ehci_std->prev->itd_next = ehci_std->itd_next;

	usb_pc_cpu_flush(ehci_std->prev->page_cache);

	if (ehci_std->next) {
		ehci_std->next->prev = ehci_std->prev;
		usb_pc_cpu_flush(ehci_std->next->page_cache);
	}
	return ((last == ehci_std) ? ehci_std->prev : last);
}

#define	EHCI_REMOVE_QH(sqh, last) (last) = _ehci_remove_qh(sqh, last)
static ehci_qh_t *
_ehci_remove_qh(ehci_qh_t *sqh, ehci_qh_t *last)
{
	DPRINTFN(11, "%p from %p\n", sqh, last);

	/* (sc->sc_bus.mtx) must be locked */

	/* only remove if not removed from a queue */
	if (sqh->prev) {

		sqh->prev->next = sqh->next;
		sqh->prev->qh_link = sqh->qh_link;

		usb_pc_cpu_flush(sqh->prev->page_cache);

		if (sqh->next) {
			sqh->next->prev = sqh->prev;
			usb_pc_cpu_flush(sqh->next->page_cache);
		}
		last = ((last == sqh) ? sqh->prev : last);

		sqh->prev = 0;

		usb_pc_cpu_flush(sqh->page_cache);
	}
	return (last);
}

static void
ehci_data_toggle_update(struct usb_xfer *xfer, uint16_t actlen, uint16_t xlen)
{
	uint16_t rem;
	uint8_t dt;

	/* count number of full packets */
	dt = (actlen / xfer->max_packet_size) & 1;

	/* compute remainder */
	rem = actlen % xfer->max_packet_size;

	if (rem > 0)
		dt ^= 1;	/* short packet at the end */
	else if (actlen != xlen)
		dt ^= 1;	/* zero length packet at the end */
	else if (xlen == 0)
		dt ^= 1;	/* zero length transfer */

	xfer->endpoint->toggle_next ^= dt;
}

static usb_error_t
ehci_non_isoc_done_sub(struct usb_xfer *xfer)
{
	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
	ehci_qtd_t *td;
	ehci_qtd_t *td_alt_next;
	uint32_t status;
	uint16_t len;

	td = (ehci_qtd_t *)xfer->td_transfer_cache;
	td_alt_next = td->alt_next;

	if (xfer->aframes != xfer->nframes) {
		usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
	}
	while (1) {

		usb_pc_cpu_invalidate(td->page_cache);
		status = hc32toh(sc, td->qtd_status);

		len = EHCI_QTD_GET_BYTES(status);

		/*
	         * Verify the status length and
		 * add the length to "frlengths[]":
	         */
		if (len > td->len) {
			/* should not happen */
			DPRINTF("Invalid status length, "
			    "0x%04x/0x%04x bytes\n", len, td->len);
			status |= EHCI_QTD_HALTED;
		} else if (xfer->aframes != xfer->nframes) {
			xfer->frlengths[xfer->aframes] += td->len - len;
			/* manually update data toggle */
			ehci_data_toggle_update(xfer, td->len - len, td->len);
		}

		/* Check for last transfer */
		if (((void *)td) == xfer->td_transfer_last) {
			td = NULL;
			break;
		}
		/* Check for transfer error */
		if (status & EHCI_QTD_HALTED) {
			/* the transfer is finished */
			td = NULL;
			break;
		}
		/* Check for short transfer */
		if (len > 0) {
			if (xfer->flags_int.short_frames_ok) {
				/* follow alt next */
				td = td->alt_next;
			} else {
				/* the transfer is finished */
				td = NULL;
			}
			break;
		}
		td = td->obj_next;

		if (td->alt_next != td_alt_next) {
			/* this USB frame is complete */
			break;
		}
	}

	/* update transfer cache */

	xfer->td_transfer_cache = td;

#ifdef LOSCFG_USB_DEBUG
	if (status & EHCI_QTD_STATERRS) {
		DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x"
		    "status=%s%s%s%s%s%s%s%s\n",
		    xfer->address, xfer->endpointno, xfer->aframes,
		    (status & EHCI_QTD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
		    (status & EHCI_QTD_HALTED) ? "[HALTED]" : "",
		    (status & EHCI_QTD_BUFERR) ? "[BUFERR]" : "",
		    (status & EHCI_QTD_BABBLE) ? "[BABBLE]" : "",
		    (status & EHCI_QTD_XACTERR) ? "[XACTERR]" : "",
		    (status & EHCI_QTD_MISSEDMICRO) ? "[MISSED]" : "",
		    (status & EHCI_QTD_SPLITXSTATE) ? "[SPLIT]" : "",
		    (status & EHCI_QTD_PINGSTATE) ? "[PING]" : "");
	}
#endif
	if (status & EHCI_QTD_HALTED) {
		if ((xfer->xroot->udev->parent_hs_hub != NULL) ||
		    (xfer->xroot->udev->address != 0)) {
			/* try to separate I/O errors from STALL */
			if (EHCI_QTD_GET_CERR(status) == 0)
				return (USB_ERR_IOERROR);
		}
		return (USB_ERR_STALLED);
	}
	return (USB_ERR_NORMAL_COMPLETION);
}

static void
ehci_non_isoc_done(struct usb_xfer *xfer)
{
	ehci_qh_t *qh;
	usb_error_t err = USB_ERR_NORMAL_COMPLETION;

	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n", xfer, xfer->endpoint);

#ifdef LOSCFG_USB_DEBUG
	if (ehcidebug > 10) {
		ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
		ehci_dump_sqtds(sc, xfer->td_transfer_first);
	}
#endif

	/* extract data toggle directly from the QH's overlay area */
	qh = (ehci_qh_t *)xfer->qh_start[xfer->flags_int.curr_dma_set];

	usb_pc_cpu_invalidate(qh->page_cache);

	/* reset scanner */
	xfer->td_transfer_cache = xfer->td_transfer_first;
	if (xfer->flags_int.control_xfr) {
		if (xfer->flags_int.control_hdr) {
			err = ehci_non_isoc_done_sub(xfer);
		}
		xfer->aframes = 1;

		if (xfer->td_transfer_cache == NULL) {
			goto done;
		}
	}
	while (xfer->aframes != xfer->nframes) {

		err = ehci_non_isoc_done_sub(xfer);
		xfer->aframes++;

		if (xfer->td_transfer_cache == NULL) {
			goto done;
		}
	}

	if (xfer->flags_int.control_xfr &&
	    !xfer->flags_int.control_act) {

		err = ehci_non_isoc_done_sub(xfer);
	}
done:
	ehci_device_done(xfer, err);
}

/*------------------------------------------------------------------------*
 *	ehci_check_transfer
 *
 * Return values:
 *    0: USB transfer is not finished
 * Else: USB transfer is finished
 *------------------------------------------------------------------------*/
static uint8_t
ehci_check_transfer(struct usb_xfer *xfer)
{
	const struct usb_pipe_methods *methods = xfer->endpoint->methods;
	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);

	uint32_t status;

	DPRINTFN(13, "xfer=%p checking transfer\n", xfer);

	if (methods == &ehci_device_isoc_fs_methods) {
		ehci_sitd_t *td;

		/* isochronous full speed transfer */

		td = (ehci_sitd_t *)xfer->td_transfer_last;
		usb_pc_cpu_invalidate(td->page_cache);
		status = hc32toh(sc, td->sitd_status);

		/* also check if first is complete */

		td = (ehci_sitd_t *)xfer->td_transfer_first;
		usb_pc_cpu_invalidate(td->page_cache);
		status |= hc32toh(sc, td->sitd_status);

		if (!(status & EHCI_SITD_ACTIVE)) {
			ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
			goto transferred;
		}
	} else if (methods == &ehci_device_isoc_hs_methods) {
		ehci_itd_t *td;

		/* isochronous high speed transfer */

		/* check last transfer */
		td = (ehci_itd_t *)xfer->td_transfer_last;
		usb_pc_cpu_invalidate(td->page_cache);
		status = td->itd_status[0];
		status |= td->itd_status[1];
		status |= td->itd_status[2];
		status |= td->itd_status[3];
		status |= td->itd_status[4];
		status |= td->itd_status[5];
		status |= td->itd_status[6];
		status |= td->itd_status[7];

		/* also check first transfer */
		td = (ehci_itd_t *)xfer->td_transfer_first;
		usb_pc_cpu_invalidate(td->page_cache);
		status |= td->itd_status[0];
		status |= td->itd_status[1];
		status |= td->itd_status[2];
		status |= td->itd_status[3];
		status |= td->itd_status[4];
		status |= td->itd_status[5];
		status |= td->itd_status[6];
		status |= td->itd_status[7];

		/* if no transactions are active we continue */
		if (!(status & htohc32(sc, EHCI_ITD_ACTIVE))) {
			ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
			goto transferred;
		}
	} else {
		ehci_qtd_t *td;
		ehci_qh_t *qh;

		/* non-isochronous transfer */

		/*
		 * check whether there is an error somewhere in the middle,
		 * or whether there was a short packet (SPD and not ACTIVE)
		 */
		td = (ehci_qtd_t *)xfer->td_transfer_cache;

		qh = (ehci_qh_t *)xfer->qh_start[xfer->flags_int.curr_dma_set];

		usb_pc_cpu_invalidate(qh->page_cache);

		status = hc32toh(sc, qh->qh_qtd.qtd_status);
		if (status & EHCI_QTD_ACTIVE) {
			/* transfer is pending */
			goto done;
		}

		while (1) {
			usb_pc_cpu_invalidate(td->page_cache);
			status = hc32toh(sc, td->qtd_status);

			/*
			 * Check if there is an active TD which
			 * indicates that the transfer isn't done.
			 */
			if (status & EHCI_QTD_ACTIVE) {
				/* update cache */
				xfer->td_transfer_cache = td;
				goto done;
			}
			/*
			 * last transfer descriptor makes the transfer done
			 */
			if (((void *)td) == xfer->td_transfer_last) {
				break;
			}
			/*
			 * any kind of error makes the transfer done
			 */
			if (status & EHCI_QTD_HALTED) {
				break;
			}
			/*
			 * if there is no alternate next transfer, a short
			 * packet also makes the transfer done
			 */
			if (EHCI_QTD_GET_BYTES(status)) {
				if (xfer->flags_int.short_frames_ok) {
					/* follow alt next */
					if (td->alt_next) {
						td = td->alt_next;
						continue;
					}
				}
				/* transfer is done */
				break;
			}
			td = td->obj_next;
		}
		ehci_non_isoc_done(xfer);
		goto transferred;
	}

done:
	DPRINTFN(13, "xfer=%p is still active\n", xfer);
	return (0);

transferred:
	return (1);
}

static void
ehci_pcd_enable(ehci_softc_t *sc)
{
	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);

	sc->sc_eintrs |= EHCI_STS_PCD;
	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);

	/* acknowledge any PCD interrupt */
	EOWRITE4(sc, EHCI_USBSTS, EHCI_STS_PCD);

	ehci_root_intr(sc);
}

static void
ehci_interrupt_poll(ehci_softc_t *sc)
{
	struct usb_xfer *xfer;

repeat:
	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
		/*
		 * check if transfer is transferred
		 */
		if (ehci_check_transfer(xfer)) {
			/* queue has been modified */
			goto repeat;
		}
	}
}

/*
 * Some EHCI chips from VIA / ATI seem to trigger interrupts before
 * writing back the qTD status, or miss signalling occasionally under
 * heavy load.  If the host machine is too fast, we can miss
 * transaction completion - when we scan the active list the
 * transaction still seems to be active. This generally exhibits
 * itself as a umass stall that never recovers.
 *
 * We work around this behaviour by setting up this callback after any
 * softintr that completes with transactions still pending, giving us
 * another chance to check for completion after the writeback has
 * taken place.
 */
static void
ehci_poll_timeout(void *ehci_arg)
{
	ehci_softc_t *sc = (ehci_softc_t *)ehci_arg;

	DPRINTFN(3, "\n");
	ehci_interrupt_poll(sc);
}

/*------------------------------------------------------------------------*
 *	ehci_interrupt - EHCI interrupt handler
 *
 * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
 * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
 * is present !
 *------------------------------------------------------------------------*/
void
ehci_interrupt(unsigned int irq, ehci_softc_t *sc)
{
	uint32_t status;

	USB_BUS_LOCK(&sc->sc_bus);
	DPRINTFN(16, "real interrupt\n");

#ifdef LOSCFG_USB_DEBUG
	if (ehcidebug > 15) {
		ehci_dump_regs(sc);
	}
#endif

	status = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));

	if (status == 0) {
		/* the interrupt was not for us */
		goto done;
	}
	if (!(status & sc->sc_eintrs)) {
		goto done;
	}
	EOWRITE4(sc, EHCI_USBSTS, status);	/* acknowledge */

	status &= sc->sc_eintrs;

#ifdef LOSCFG_USB_DEBUG
	if (status & EHCI_STS_HSE) {
		ehci_dump_regs(sc);
		ehci_dump_isoc(sc);
	}
#endif
	if (status & EHCI_STS_PCD) {
		/*
		 * Disable PCD interrupt for now, because it will be
		 * on until the port has been reset.
		 */
		sc->sc_eintrs &= ~EHCI_STS_PCD;
		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);

		ehci_root_intr(sc);

		/* do not allow RHSC interrupts > 1 per second */
		callout_reset(&sc->sc_tmo_pcd, hz,
		    (void *)&ehci_pcd_enable, sc);
	}
	status &= ~(EHCI_STS_INT | EHCI_STS_ERRINT | EHCI_STS_PCD | EHCI_STS_IAA);

	if (status != 0) {
		/* block unprocessed interrupts */
		sc->sc_eintrs &= ~status;
		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
	}
	/* poll all the USB transfers */
	ehci_interrupt_poll(sc);

	if (sc->sc_flags & EHCI_SCFLG_LOSTINTRBUG) {
		callout_reset(&sc->sc_tmo_poll, hz / 128,
		    (void *)&ehci_poll_timeout, sc);
	}

done:
	USB_BUS_UNLOCK(&sc->sc_bus);
}

/*
 * called when a request does not complete
 */
static void
ehci_timeout(void *ehci_arg)
{
	struct usb_xfer *xfer = (struct usb_xfer *)ehci_arg;

	DPRINTF("xfer=%p\n", xfer);

	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);

	/* transfer is transferred */
	ehci_device_done(xfer, USB_ERR_TIMEOUT);
}

static void
ehci_do_poll(struct usb_bus *bus)
{
	ehci_softc_t *sc = EHCI_BUS2SC(bus);

	USB_BUS_LOCK(&sc->sc_bus);
	ehci_interrupt_poll(sc);
	USB_BUS_UNLOCK(&sc->sc_bus);
}

static void
ehci_setup_standard_chain_sub(struct ehci_std_temp *temp)
{
	struct usb_page_search buf_res;
	ehci_qtd_t *td;
	ehci_qtd_t *td_next;
	ehci_qtd_t *td_alt_next;
	uint32_t buf_offset;
	uint32_t average;
	uint32_t len_old;
	uint32_t terminate;
	uint32_t qtd_altnext;
	uint8_t shortpkt_old;
	uint8_t precompute;

	terminate = temp->sc->sc_terminate_self;
	qtd_altnext = temp->sc->sc_terminate_self;
	td_alt_next = NULL;
	buf_offset = 0;
	shortpkt_old = temp->shortpkt;
	len_old = temp->len;
	precompute = 1;

restart:

	td = temp->td;
	td_next = temp->td_next;

	while (1) {

		if (temp->len == 0) {

			if (temp->shortpkt) {
				break;
			}
			/* send a Zero Length Packet, ZLP, last */

			temp->shortpkt = 1;
			average = 0;

		} else {

			average = temp->average;

			if (temp->len < average) {
				if (temp->len % temp->max_frame_size) {
					temp->shortpkt = 1;
				}
				average = temp->len;
			}
		}

		if (td_next == NULL) {
			panic("%s: out of EHCI transfer descriptors!", __FUNCTION__);
		}
		/* get next TD */

		td = td_next;
		td_next = td->obj_next;

		/* check if we are pre-computing */

		if (precompute) {

			/* update remaining length */

			temp->len -= average;

			continue;
		}
		/* fill out current TD */

		td->qtd_status =
		    temp->qtd_status |
		    htohc32(temp->sc, EHCI_QTD_IOC |
		    EHCI_QTD_SET_BYTES(average));

		if (average == 0) {

			if (temp->auto_data_toggle == 0) {

				/* update data toggle, ZLP case */

				temp->qtd_status ^=
				    htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
			}
			td->len = 0;

			/* properly reset reserved fields */
			td->qtd_buffer[0] = 0;
			td->qtd_buffer[1] = 0;
			td->qtd_buffer[2] = 0;
			td->qtd_buffer[3] = 0;
			td->qtd_buffer[4] = 0;
			td->qtd_buffer_hi[0] = 0;
			td->qtd_buffer_hi[1] = 0;
			td->qtd_buffer_hi[2] = 0;
			td->qtd_buffer_hi[3] = 0;
			td->qtd_buffer_hi[4] = 0;
		} else {

			uint8_t x;

			if (temp->auto_data_toggle == 0) {

				/* update data toggle */

				if (((average + temp->max_frame_size - 1) /
				    temp->max_frame_size) & 1) {
					temp->qtd_status ^=
					    htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK);
				}
			}
			td->len = average;

			/* update remaining length */

			temp->len -= average;

			/* fill out buffer pointers */

			usbd_get_page(temp->pc, buf_offset, &buf_res);
			td->qtd_buffer[0] =
#if USB_HAVE_BUSDMA
			    htohc32(temp->sc, buf_res.physaddr);
#else
			    htohc32(temp->sc, (unsigned int)buf_res.buffer);
#endif
			td->qtd_buffer_hi[0] = 0;

			x = 1;

			while (average > EHCI_PAGE_SIZE) {
				average -= EHCI_PAGE_SIZE;
				buf_offset += EHCI_PAGE_SIZE;
				usbd_get_page(temp->pc, buf_offset, &buf_res);
				td->qtd_buffer[x] =
				    htohc32(temp->sc,
#if USB_HAVE_BUSDMA
				    buf_res.physaddr & (~0xFFF));
#else
				    (unsigned int)buf_res.buffer & (~0xFFF));
#endif
				td->qtd_buffer_hi[x] = 0;
				x++;
			}

			/*
			 * NOTE: The "average" variable is never zero after
			 * exiting the loop above !
			 *
			 * NOTE: We have to subtract one from the offset to
			 * ensure that we are computing the physical address
			 * of a valid page !
			 */
			buf_offset += average;
			usbd_get_page(temp->pc, buf_offset - 1, &buf_res);
			td->qtd_buffer[x] =
			    htohc32(temp->sc,
#if USB_HAVE_BUSDMA
			    buf_res.physaddr & (~0xFFF));
#else
			    (unsigned int)buf_res.buffer & (~0xFFF));
#endif
			td->qtd_buffer_hi[x] = 0;

			/* properly reset reserved fields */
			while (++x < EHCI_QTD_NBUFFERS) {
				td->qtd_buffer[x] = 0;
				td->qtd_buffer_hi[x] = 0;
			}
		}

		if (td_next) {

			/* link the current TD with the next one */
			td->qtd_next = td_next->qtd_self;
		}

		td->qtd_altnext = qtd_altnext;
		td->alt_next = td_alt_next;

		usb_pc_cpu_flush(td->page_cache);
	}

	if (precompute) {
		precompute = 0;

		/* setup alt next pointer, if any */
		if (temp->last_frame) {
			td_alt_next = NULL;
			qtd_altnext = terminate;
		} else {
			/* we use this field internally */
			td_alt_next = td_next;
			if (temp->setup_alt_next && td_next) {
				qtd_altnext = td_next->qtd_self;
			} else {
				qtd_altnext = terminate;
			}
		}

		/* restore */
		temp->shortpkt = shortpkt_old;
		temp->len = len_old;
		goto restart;
	}
	temp->td = td;
	temp->td_next = td_next;
}

static void
ehci_setup_standard_chain(struct usb_xfer *xfer, ehci_qh_t **qh_last)
{
	struct ehci_std_temp temp;
	const struct usb_pipe_methods *methods;
	ehci_qh_t *qh;
	ehci_qtd_t *td;
	uint32_t qh_endp;
	uint32_t qh_endphub;
	uint32_t x;
	uint32_t int_save;

	DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
	    xfer->address, UE_GET_ADDR(xfer->endpointno),
	    xfer->sumlen, usbd_get_speed(xfer->xroot->udev));

	temp.average = xfer->max_hc_frame_size;
	temp.max_frame_size = xfer->max_frame_size;
	temp.sc = EHCI_BUS2SC(xfer->xroot->bus);

	/* toggle the DMA set we are using */
	xfer->flags_int.curr_dma_set ^= 1;

	/* get next DMA set */
	td = (ehci_qtd_t *)xfer->td_start[xfer->flags_int.curr_dma_set];

	xfer->td_transfer_first = td;
	xfer->td_transfer_cache = td;

	temp.td = NULL;
	temp.td_next = td;
	temp.qtd_status = 0;
	temp.last_frame = 0;
	temp.setup_alt_next = xfer->flags_int.short_frames_ok;

	if (xfer->flags_int.control_xfr) {
		if (xfer->endpoint->toggle_next) {
			/* DATA1 is next */
			temp.qtd_status |=
			    htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1U));
		}
		temp.auto_data_toggle = 0;
	} else {
		temp.auto_data_toggle = 1;
	}

	if ((xfer->xroot->udev->parent_hs_hub != NULL) ||
	    (xfer->xroot->udev->address != 0)) {
		/* max 3 retries */
		temp.qtd_status |=
		    htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
	}
	/* check if we should prepend a setup message */

	if (xfer->flags_int.control_xfr) {
		if (xfer->flags_int.control_hdr) {

			xfer->endpoint->toggle_next = 0;

			temp.qtd_status &=
			    htohc32(temp.sc, EHCI_QTD_SET_CERR(3));
			temp.qtd_status |= htohc32(temp.sc,
			    EHCI_QTD_ACTIVE |
			    EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
			    EHCI_QTD_SET_TOGGLE(0));

			temp.len = xfer->frlengths[0];
			temp.pc = xfer->frbuffers + 0;
			temp.shortpkt = temp.len ? 1 : 0;
			/* check for last frame */
			if (xfer->nframes == 1) {
				/* no STATUS stage yet, SETUP is last */
				if (xfer->flags_int.control_act) {
					temp.last_frame = 1;
					temp.setup_alt_next = 0;
				}
			}
			ehci_setup_standard_chain_sub(&temp);
		}
		x = 1;
	} else {
		x = 0;
	}

	while (x != xfer->nframes) {

		/* DATA0 / DATA1 message */

		temp.len = xfer->frlengths[x];
		temp.pc = xfer->frbuffers + x;

		x++;

		if (x == xfer->nframes) {
			if (xfer->flags_int.control_xfr) {
				/* no STATUS stage yet, DATA is last */
				if (xfer->flags_int.control_act) {
					temp.last_frame = 1;
					temp.setup_alt_next = 0;
				}
			} else {
				temp.last_frame = 1;
				temp.setup_alt_next = 0;
			}
		}
		/* keep previous data toggle and error count */

		temp.qtd_status &=
		    htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
		    EHCI_QTD_SET_TOGGLE(1U));

		if (temp.len == 0) {

			/* make sure that we send an USB packet */

			temp.shortpkt = 0;

		} else {

			/* regular data transfer */

			temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
		}

		/* set endpoint direction */

		temp.qtd_status |=
		    (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ?
		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
		    EHCI_QTD_SET_PID(EHCI_QTD_PID_IN)) :
		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
		    EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT));

		ehci_setup_standard_chain_sub(&temp);
	}

	/* check if we should append a status stage */

	if (xfer->flags_int.control_xfr &&
	    !xfer->flags_int.control_act) {

		/*
		 * Send a DATA1 message and invert the current endpoint
		 * direction.
		 */

		temp.qtd_status &= htohc32(temp.sc, EHCI_QTD_SET_CERR(3) |
		    EHCI_QTD_SET_TOGGLE(1U));
		temp.qtd_status |=
		    (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) ?
		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
		    EHCI_QTD_SET_PID(EHCI_QTD_PID_IN) |
		    EHCI_QTD_SET_TOGGLE(1U)) :
		    htohc32(temp.sc, EHCI_QTD_ACTIVE |
		    EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT) |
		    EHCI_QTD_SET_TOGGLE(1U));

		temp.len = 0;
		temp.pc = NULL;
		temp.shortpkt = 0;
		temp.last_frame = 1;
		temp.setup_alt_next = 0;

		ehci_setup_standard_chain_sub(&temp);
	}
	td = temp.td;
	if (td == NULL)
		return;

	/* the last TD terminates the transfer: */
	td->qtd_next = htohc32(temp.sc, EHCI_LINK_TERMINATE);
	td->qtd_altnext = htohc32(temp.sc, EHCI_LINK_TERMINATE);

	usb_pc_cpu_flush(td->page_cache);

	/* must have at least one frame! */

	xfer->td_transfer_last = td;

#ifdef LOSCFG_USB_DEBUG
	if (ehcidebug > 8) {
		DPRINTF("nexttog=%d; data before transfer:\n",
		    xfer->endpoint->toggle_next);
		ehci_dump_sqtds(temp.sc,
		    xfer->td_transfer_first);
	}
#endif

	methods = xfer->endpoint->methods;

	qh = (ehci_qh_t *)xfer->qh_start[xfer->flags_int.curr_dma_set];

	/* the "qh_link" field is filled when the QH is added */

	qh_endp =
	    (EHCI_QH_SET_ADDR(xfer->address) |
	    EHCI_QH_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
	    EHCI_QH_SET_MPL(xfer->max_packet_size));

	if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) {
		qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH);
		if (methods != &ehci_device_intr_methods)
			qh_endp |= EHCI_QH_SET_NRL(8U);
	} else {

		if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_FULL) {
			qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_FULL);
		} else {
			qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_LOW);
		}

		if (methods == &ehci_device_ctrl_methods) {
			qh_endp |= EHCI_QH_CTL;
		}
		if (methods != &ehci_device_intr_methods) {
			/* Only try one time per microframe! */
			qh_endp |= EHCI_QH_SET_NRL(1);
		}
	}

	if (temp.auto_data_toggle == 0) {
		/* software computes the data toggle */
		qh_endp |= EHCI_QH_DTC;
	}

	qh->qh_endp = htohc32(temp.sc, qh_endp);

	qh_endphub =
	    (EHCI_QH_SET_MULT(xfer->max_packet_count & 3) |
	    EHCI_QH_SET_CMASK(xfer->endpoint->usb_cmask) |
	    EHCI_QH_SET_SMASK(xfer->endpoint->usb_smask) |
	    EHCI_QH_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
	    EHCI_QH_SET_PORT(xfer->xroot->udev->hs_port_no));

	qh->qh_endphub = htohc32(temp.sc, qh_endphub);
	qh->qh_curqtd = 0;

	/* fill the overlay qTD */

	if (temp.auto_data_toggle && xfer->endpoint->toggle_next) {
		/* DATA1 is next */
		qh->qh_qtd.qtd_status = htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1U));
	} else {
		qh->qh_qtd.qtd_status = 0;
	}

	td = (ehci_qtd_t *)xfer->td_transfer_first;

	qh->qh_qtd.qtd_next = td->qtd_self;
	qh->qh_qtd.qtd_altnext =
	    htohc32(temp.sc, EHCI_LINK_TERMINATE);

	/* properly reset reserved fields */
	qh->qh_qtd.qtd_buffer[0] = 0;
	qh->qh_qtd.qtd_buffer[1] = 0;
	qh->qh_qtd.qtd_buffer[2] = 0;
	qh->qh_qtd.qtd_buffer[3] = 0;
	qh->qh_qtd.qtd_buffer[4] = 0;
	qh->qh_qtd.qtd_buffer_hi[0] = 0;
	qh->qh_qtd.qtd_buffer_hi[1] = 0;
	qh->qh_qtd.qtd_buffer_hi[2] = 0;
	qh->qh_qtd.qtd_buffer_hi[3] = 0;
	qh->qh_qtd.qtd_buffer_hi[4] = 0;

	usb_pc_cpu_flush(qh->page_cache);

	if (xfer->xroot->udev->flags.self_suspended == 0) {
		spin_lock_irqsave(&g_usb_ehci_qh_spinlock, int_save);
		EHCI_APPEND_QH(qh, *qh_last);
		spin_unlock_irqrestore(&g_usb_ehci_qh_spinlock, int_save);
	}
}

static void
ehci_root_intr(ehci_softc_t *sc)
{
	uint16_t i;
	uint16_t m;
	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);

	/* clear any old interrupt data */
	(void)memset_s(sc->sc_hub_idata, sizeof(sc->sc_hub_idata), 0, sizeof(sc->sc_hub_idata));

	/* set bits */
	m = (sc->sc_noport + 1);
	if (m > (8 * sizeof(sc->sc_hub_idata))) {
		m = (8 * sizeof(sc->sc_hub_idata));
	}
	for (i = 1; i < m; i++) {
		/* pick out CHANGE bits from the status register */
		if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR) {
			sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
			DPRINTF("port %d changed\n", i);
		}
	}
	uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
	    sizeof(sc->sc_hub_idata));
}

static void
ehci_isoc_fs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
{
	uint32_t nframes = xfer->nframes;
	uint32_t status;
	uint32_t *plen = xfer->frlengths;
	uint16_t len = 0;
	ehci_sitd_t *td = (ehci_sitd_t *)xfer->td_transfer_first;
	ehci_sitd_t **pp_last = &sc->sc_isoc_fs_p_last[xfer->qh_pos];

	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
	    xfer, xfer->endpoint);

	while (nframes--) {
		if (td == NULL) {
			panic("%s:%d: out of TD's\n",
			    __FUNCTION__, __LINE__);
		}
		if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
			pp_last = &sc->sc_isoc_fs_p_last[0];
		}
#ifdef LOSCFG_USB_DEBUG
		if (ehcidebug > 15) {
			DPRINTF("isoc FS-TD\n");
			ehci_dump_sitd(sc, td);
		}
#endif
		usb_pc_cpu_invalidate(td->page_cache);
		status = hc32toh(sc, td->sitd_status);

		len = EHCI_SITD_GET_LEN(status);

		DPRINTFN(2, "status=0x%08x, rem=%u\n", status, len);

		if (*plen >= len) {
			len = *plen - len;
		} else {
			len = 0;
		}

		*plen = len;

		/* remove FS-TD from schedule */
		EHCI_REMOVE_FS_TD(td, *pp_last);

		pp_last++;
		plen++;
		td = td->obj_next;
	}

	xfer->aframes = xfer->nframes;
}

static void
ehci_isoc_hs_done(ehci_softc_t *sc, struct usb_xfer *xfer)
{
	uint32_t nframes = xfer->nframes;
	uint32_t status;
	uint32_t *plen = xfer->frlengths;
	uint16_t len = 0;
	uint8_t td_no = 0;
	ehci_itd_t *td = (ehci_itd_t *)xfer->td_transfer_first;
	ehci_itd_t **pp_last = &sc->sc_isoc_hs_p_last[xfer->qh_pos];

	DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
	    xfer, xfer->endpoint);

	while (nframes) {
		if (td == NULL) {
			panic("%s:%d: out of TD's\n",
			    __FUNCTION__, __LINE__);
		}
		if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
			pp_last = &sc->sc_isoc_hs_p_last[0];
		}
#ifdef LOSCFG_USB_DEBUG
		if (ehcidebug > 15) {
			DPRINTF("isoc HS-TD\n");
			ehci_dump_itd(sc, td);
		}
#endif

		usb_pc_cpu_invalidate(td->page_cache);
		status = hc32toh(sc, td->itd_status[td_no]);

		len = EHCI_ITD_GET_LEN(status);

		DPRINTFN(2, "status=0x%08x, len=%u\n", status, len);

		if (xfer->endpoint->usb_smask & (1 << td_no)) {

			if (*plen >= len) {
				/*
				 * The length is valid. NOTE: The
				 * complete length is written back
				 * into the status field, and not the
				 * remainder like with other transfer
				 * descriptor types.
				 */
			} else {
				/* Invalid length - truncate */
				len = 0;
			}

			*plen = len;
			plen++;
			nframes--;
		}

		td_no++;

		if ((td_no == 8) || (nframes == 0)) {
			/* remove HS-TD from schedule */
			EHCI_REMOVE_HS_TD(td, *pp_last);
			pp_last++;

			td_no = 0;
			td = td->obj_next;
		}
	}
	xfer->aframes = xfer->nframes;
}

/* NOTE: "done" can be run two times in a row,
 * from close and from interrupt
 */
static void
ehci_device_done(struct usb_xfer *xfer, usb_error_t error)
{
	uintptr_t interrupt_ret;
	const struct usb_pipe_methods *methods = xfer->endpoint->methods;
	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);

	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);

	DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
	    xfer, xfer->endpoint, error);

	if ((methods == &ehci_device_bulk_methods) ||
	    (methods == &ehci_device_ctrl_methods)) {
#ifdef LOSCFG_USB_DEBUG
		if (ehcidebug > 8) {
			DPRINTF("nexttog=%d; data after transfer:\n",
			    xfer->endpoint->toggle_next);
			ehci_dump_sqtds(sc,
			    xfer->td_transfer_first);
		}
#endif

		spin_lock_irqsave(&g_usb_ehci_qh_spinlock, interrupt_ret);
		EHCI_REMOVE_QH((ehci_qh_t *)xfer->qh_start[xfer->flags_int.curr_dma_set],
		    sc->sc_async_p_last);
		spin_unlock_irqrestore(&g_usb_ehci_qh_spinlock, interrupt_ret);
	}
	if (methods == &ehci_device_intr_methods) {
		spin_lock_irqsave(&g_usb_ehci_qh_spinlock, interrupt_ret);
		EHCI_REMOVE_QH((ehci_qh_t *)xfer->qh_start[xfer->flags_int.curr_dma_set],
		    sc->sc_intr_p_last[xfer->qh_pos]);
		spin_unlock_irqrestore(&g_usb_ehci_qh_spinlock, interrupt_ret);
	}
	/*
	 * Only finish isochronous transfers once which will update
	 * "xfer->frlengths".
	 */
	if (xfer->td_transfer_first &&
	    xfer->td_transfer_last) {
		if (methods == &ehci_device_isoc_fs_methods) {
			ehci_isoc_fs_done(sc, xfer);
		}
		if (methods == &ehci_device_isoc_hs_methods) {
			ehci_isoc_hs_done(sc, xfer);
		}
		xfer->td_transfer_first = NULL;
		xfer->td_transfer_last = NULL;
	}
	/* dequeue transfer and start next transfer */
	usbd_transfer_done(xfer, error);
}

/*------------------------------------------------------------------------*
 * ehci bulk support
 *------------------------------------------------------------------------*/
static void
ehci_device_bulk_open(struct usb_xfer *xfer)
{
	return;
}

static void
ehci_device_bulk_close(struct usb_xfer *xfer)
{
	ehci_device_done(xfer, USB_ERR_CANCELLED);
}

static void
ehci_device_bulk_enter(struct usb_xfer *xfer)
{
	return;
}

static void
ehci_doorbell_async(struct ehci_softc *sc)
{
	uint32_t temp;

	/*
	 * XXX Performance quirk: Some Host Controllers have a too low
	 * interrupt rate. Issue an IAAD to stimulate the Host
	 * Controller after queueing the BULK transfer.
	 *
	 * XXX Force the host controller to refresh any QH caches.
	 */
	temp = EOREAD4(sc, EHCI_USBCMD);
	if (!(temp & EHCI_CMD_IAAD))
		EOWRITE4(sc, EHCI_USBCMD, temp | EHCI_CMD_IAAD);
}

static void
ehci_device_bulk_start(struct usb_xfer *xfer)
{
	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);

	/* setup TD's and QH */
	ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);

	/* put transfer on interrupt queue */
	ehci_transfer_intr_enqueue(xfer);

	/*
	 * XXX Certain nVidia chipsets choke when using the IAAD
	 * feature too frequently.
	 */
	if (sc->sc_flags & EHCI_SCFLG_IAADBUG)
		return;

	ehci_doorbell_async(sc);
}

const struct usb_pipe_methods ehci_device_bulk_methods =
{
	.open = ehci_device_bulk_open,
	.close = ehci_device_bulk_close,
	.enter = ehci_device_bulk_enter,
	.start = ehci_device_bulk_start,
};

/*------------------------------------------------------------------------*
 * ehci control support
 *------------------------------------------------------------------------*/
static void
ehci_device_ctrl_open(struct usb_xfer *xfer)
{
	return;
}

static void
ehci_device_ctrl_close(struct usb_xfer *xfer)
{
	ehci_device_done(xfer, USB_ERR_CANCELLED);
}

static void
ehci_device_ctrl_enter(struct usb_xfer *xfer)
{
	return;
}

static void
ehci_device_ctrl_start(struct usb_xfer *xfer)
{
	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);

	/* setup TD's and QH */
	ehci_setup_standard_chain(xfer, &sc->sc_async_p_last);

	/* put transfer on interrupt queue */
	ehci_transfer_intr_enqueue(xfer);
}

const struct usb_pipe_methods ehci_device_ctrl_methods =
{
	.open = ehci_device_ctrl_open,
	.close = ehci_device_ctrl_close,
	.enter = ehci_device_ctrl_enter,
	.start = ehci_device_ctrl_start,
};

/*------------------------------------------------------------------------*
 * ehci interrupt support
 *------------------------------------------------------------------------*/
static void
ehci_device_intr_open(struct usb_xfer *xfer)
{
	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
	uint16_t best;
	uint16_t bit;
	uint16_t x;

	usb_hs_bandwidth_alloc(xfer);

	/*
	 * Find the best QH position corresponding to the given interval:
	 */

	best = 0;
	bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2;
	while (bit) {
		if (xfer->interval >= bit) {
			x = bit;
			best = bit;
			while (x & bit) {
				if (sc->sc_intr_stat[x] <
				    sc->sc_intr_stat[best]) {
					best = x;
				}
				x++;
			}
			break;
		}
		bit >>= 1;
	}

	sc->sc_intr_stat[best]++;
	xfer->qh_pos = best;

	DPRINTFN(3, "best=%d interval=%d\n",
	    best, xfer->interval);
}

static void
ehci_device_intr_close(struct usb_xfer *xfer)
{
	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);

	sc->sc_intr_stat[xfer->qh_pos]--;

	ehci_device_done(xfer, USB_ERR_CANCELLED);

	/* bandwidth must be freed after device done */
	usb_hs_bandwidth_free(xfer);
}

static void
ehci_device_intr_enter(struct usb_xfer *xfer)
{
	return;
}

static void
ehci_device_intr_start(struct usb_xfer *xfer)
{
	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);

	/* setup TD's and QH */
	ehci_setup_standard_chain(xfer, &sc->sc_intr_p_last[xfer->qh_pos]);

	/* put transfer on interrupt queue */
	ehci_transfer_intr_enqueue(xfer);
}

const struct usb_pipe_methods ehci_device_intr_methods =
{
	.open = ehci_device_intr_open,
	.close = ehci_device_intr_close,
	.enter = ehci_device_intr_enter,
	.start = ehci_device_intr_start,
};

/*------------------------------------------------------------------------*
 * ehci full speed isochronous support
 *------------------------------------------------------------------------*/
static void
ehci_device_isoc_fs_open(struct usb_xfer *xfer)
{
	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
	ehci_sitd_t *td;
	uint32_t sitd_portaddr;
	uint8_t ds;

	sitd_portaddr =
	    EHCI_SITD_SET_ADDR(xfer->address) |
	    EHCI_SITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) |
	    EHCI_SITD_SET_HUBA(xfer->xroot->udev->hs_hub_addr) |
	    EHCI_SITD_SET_PORT(xfer->xroot->udev->hs_port_no);

	if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN)
		sitd_portaddr |= EHCI_SITD_SET_DIR_IN;

	sitd_portaddr = htohc32(sc, sitd_portaddr);

	/* initialize all TD's */

	for (ds = 0; ds != 2; ds++) {

		for (td = (ehci_sitd_t *)xfer->td_start[ds]; td; td = td->obj_next) {

			td->sitd_portaddr = sitd_portaddr;

			/*
			 * TODO: make some kind of automatic
			 * SMASK/CMASK selection based on micro-frame
			 * usage
			 *
			 * micro-frame usage (8 microframes per 1ms)
			 */
			td->sitd_back = htohc32(sc, EHCI_LINK_TERMINATE);

			usb_pc_cpu_flush(td->page_cache);
		}
	}
}

static void
ehci_device_isoc_fs_close(struct usb_xfer *xfer)
{
	ehci_device_done(xfer, USB_ERR_CANCELLED);
}

static void
ehci_device_isoc_fs_enter(struct usb_xfer *xfer)
{
	struct usb_page_search buf_res;
	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
	ehci_sitd_t *td;
	ehci_sitd_t *td_last = NULL;
	ehci_sitd_t **pp_last;
	uint32_t *plen;
	uint32_t buf_offset;
	uint32_t nframes;
	uint32_t temp;
	uint32_t sitd_mask;
	uint16_t tlen;
	uint8_t sa;
	uint8_t sb;

#ifdef LOSCFG_USB_DEBUG
	uint8_t once = 1;
#endif

	DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
	    xfer, xfer->endpoint->isoc_next, xfer->nframes);

	/* get the current frame index */

	nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;

	/*
	 * check if the frame index is within the window where the frames
	 * will be inserted
	 */
	buf_offset = (nframes - xfer->endpoint->isoc_next) &
	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);

	if ((xfer->endpoint->is_synced == 0) ||
	    (buf_offset < xfer->nframes)) {
		/*
		 * If there is data underflow or the pipe queue is empty we
		 * schedule the transfer a few frames ahead of the current
		 * frame position. Else two isochronous transfers might
		 * overlap.
		 */
		xfer->endpoint->isoc_next = (nframes + 3) &
		    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
		xfer->endpoint->is_synced = 1;
		DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
	}
	/*
	 * compute how many milliseconds the insertion is ahead of the
	 * current frame position:
	 */
	buf_offset = (xfer->endpoint->isoc_next - nframes) &
	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);

	/*
	 * pre-compute when the isochronous transfer will be finished:
	 */
	xfer->isoc_time_complete =
	    usb_isoc_time_expand(&sc->sc_bus, nframes) +
	    buf_offset + xfer->nframes;

	/* get the real number of frames */

	nframes = xfer->nframes;

	buf_offset = 0;

	plen = xfer->frlengths;

	/* toggle the DMA set we are using */
	xfer->flags_int.curr_dma_set ^= 1;

	/* get next DMA set */
	td = (ehci_sitd_t *)xfer->td_start[xfer->flags_int.curr_dma_set];
	xfer->td_transfer_first = td;

	pp_last = &sc->sc_isoc_fs_p_last[xfer->endpoint->isoc_next];

	/* store starting position */

	xfer->qh_pos = xfer->endpoint->isoc_next;

	while (nframes--) {
		if (td == NULL) {
			panic("%s:%d: out of TD's\n",
			    __FUNCTION__, __LINE__);
		}
		if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT])
			pp_last = &sc->sc_isoc_fs_p_last[0];

		/* reuse sitd_portaddr and sitd_back from last transfer */

		if (*plen > xfer->max_frame_size) {
#ifdef LOSCFG_USB_DEBUG
			if (once) {
				once = 0;
				PRINTK("%s: frame length(%d) exceeds %d "
				    "bytes (frame truncated)\n",
				    __FUNCTION__, *plen,
				    xfer->max_frame_size);
			}
#endif
			*plen = xfer->max_frame_size;
		}

		/* allocate a slot */

		sa = usbd_fs_isoc_schedule_alloc_slot(xfer,
		    xfer->isoc_time_complete - nframes - 1);

		if (sa == 255) {
			/*
			 * Schedule is FULL, set length to zero:
			 */

			*plen = 0;
			sa = USB_FS_ISOC_UFRAME_MAX - 1;
		}
		if (*plen) {
			/*
			 * only call "usbd_get_page()" when we have a
			 * non-zero length
			 */
			usbd_get_page(xfer->frbuffers, buf_offset, &buf_res);
#if USB_HAVE_BUSDMA
			td->sitd_bp[0] = htohc32(sc, buf_res.physaddr);
#else
			td->sitd_bp[0] = htohc32(sc, (unsigned int)buf_res.buffer);
#endif
			buf_offset += *plen;
			/*
			 * NOTE: We need to subtract one from the offset so
			 * that we are on a valid page!
			 */
			usbd_get_page(xfer->frbuffers, buf_offset - 1,
			    &buf_res);
#if USB_HAVE_BUSDMA
			temp = buf_res.physaddr & ~0xFFF;
#else
			temp = (unsigned int)buf_res.buffer & ~0xFFF;
#endif
		} else {
			td->sitd_bp[0] = 0;
			temp = 0;
		}

		if (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) {
			tlen = *plen;
			if (tlen <= 188) {
				temp |= 1;	/* T-count = 1, TP = ALL */
				tlen = 1;
			} else {
				tlen += 187;
				tlen /= 188;
				temp |= (uint32_t)tlen;	/* T-count = [1..6] */
				temp |= 8;	/* TP = Begin */
			}

			tlen += sa;

			if (tlen >= 8) {
				sb = 0;
			} else {
				sb = (1 << tlen);
			}

			sa = (1 << sa);
			sa = (sb - sa) & 0x3F;
			sb = 0;
		} else {
			sb = (-(4 << sa)) & 0xFE;
			sa = (1 << sa) & 0x3F;
		}

		sitd_mask = (EHCI_SITD_SET_SMASK(sa) |
		    EHCI_SITD_SET_CMASK(sb));

		td->sitd_bp[1] = htohc32(sc, temp);

		td->sitd_mask = htohc32(sc, sitd_mask);

		if (nframes == 0) {
			td->sitd_status = htohc32(sc,
			    EHCI_SITD_IOC |
			    EHCI_SITD_ACTIVE |
			    EHCI_SITD_SET_LEN(*plen));
		} else {
			td->sitd_status = htohc32(sc,
			    EHCI_SITD_ACTIVE |
			    EHCI_SITD_SET_LEN(*plen));
		}
		usb_pc_cpu_flush(td->page_cache);

#ifdef LOSCFG_USB_DEBUG
		if (ehcidebug > 15) {
			DPRINTF("FS-TD %d\n", nframes);
			ehci_dump_sitd(sc, td);
		}
#endif
		/* insert TD into schedule */
		EHCI_APPEND_FS_TD(td, *pp_last);
		pp_last++;

		plen++;
		td_last = td;
		td = td->obj_next;
	}

	xfer->td_transfer_last = td_last;

	/* update isoc_next */
	xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_fs_p_last[0]) &
	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);

	/*
	 * We don't allow cancelling of the SPLIT transaction USB FULL
	 * speed transfer, because it disturbs the bandwidth
	 * computation algorithm.
	 */
	xfer->flags_int.can_cancel_immed = 0;
}

static void
ehci_device_isoc_fs_start(struct usb_xfer *xfer)
{
	/*
	 * We don't allow cancelling of the SPLIT transaction USB FULL
	 * speed transfer, because it disturbs the bandwidth
	 * computation algorithm.
	 */
	xfer->flags_int.can_cancel_immed = 0;

	/* set a default timeout */
	if (xfer->timeout == 0)
		xfer->timeout = 500; /* ms */

	/* put transfer on interrupt queue */
	ehci_transfer_intr_enqueue(xfer);
}

const struct usb_pipe_methods ehci_device_isoc_fs_methods = {
	.open = ehci_device_isoc_fs_open,
	.close = ehci_device_isoc_fs_close,
	.enter = ehci_device_isoc_fs_enter,
	.start = ehci_device_isoc_fs_start,
};

/*------------------------------------------------------------------------*
 * ehci high speed isochronous support
 *------------------------------------------------------------------------*/
static void
ehci_device_isoc_hs_open(struct usb_xfer *xfer)
{
	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
	ehci_itd_t *td;
	uint32_t temp;
	uint8_t ds;

	usb_hs_bandwidth_alloc(xfer);

	/* initialize all TD's */

	for (ds = 0; ds != 2; ds++) {

		for (td = (ehci_itd_t *)xfer->td_start[ds]; td; td = td->obj_next) {

			/* set TD inactive */
			td->itd_status[0] = 0;
			td->itd_status[1] = 0;
			td->itd_status[2] = 0;
			td->itd_status[3] = 0;
			td->itd_status[4] = 0;
			td->itd_status[5] = 0;
			td->itd_status[6] = 0;
			td->itd_status[7] = 0;

			/* set endpoint and address */
			td->itd_bp[0] = htohc32(sc,
			    EHCI_ITD_SET_ADDR(xfer->address) |
			    EHCI_ITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)));

			temp =
			    EHCI_ITD_SET_MPL(xfer->max_packet_size & 0x7FF);

			/* set direction */
			if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) {
				temp |= EHCI_ITD_SET_DIR_IN;
			}
			/* set maximum packet size */
			td->itd_bp[1] = htohc32(sc, temp);

			/* set transfer multiplier */
			td->itd_bp[2] = htohc32(sc, xfer->max_packet_count & 3);

			usb_pc_cpu_flush(td->page_cache);
		}
	}
}

static void
ehci_device_isoc_hs_close(struct usb_xfer *xfer)
{
	ehci_device_done(xfer, USB_ERR_CANCELLED);

	/* bandwidth must be freed after device done */
	usb_hs_bandwidth_free(xfer);
}

static void
ehci_device_isoc_hs_enter(struct usb_xfer *xfer)
{
	struct usb_page_search buf_res;
	ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus);
	ehci_itd_t *td;
	ehci_itd_t *td_last = NULL;
	ehci_itd_t **pp_last;
	bus_size_t page_addr;
	uint32_t *plen;
	uint32_t status;
	uint32_t buf_offset;
	uint32_t nframes;
	uint32_t itd_offset[8 + 1];
	uint8_t x;
	uint8_t td_no;
	uint8_t page_no;
	uint8_t shift = usbd_xfer_get_fps_shift(xfer);

#ifdef LOSCFG_USB_DEBUG
	uint8_t once = 1;
#endif

	DPRINTFN(6, "xfer=%p next=%d nframes=%d shift=%d\n",
	    xfer, xfer->endpoint->isoc_next, xfer->nframes, (int)shift);

	/* get the current frame index */

	nframes = EOREAD4(sc, EHCI_FRINDEX) / 8;

	/*
	 * check if the frame index is within the window where the frames
	 * will be inserted
	 */
	buf_offset = (nframes - xfer->endpoint->isoc_next) &
	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);

	if ((xfer->endpoint->is_synced == 0) ||
	    (buf_offset < (((xfer->nframes << shift) + 7) / 8))) {
		/*
		 * If there is data underflow or the pipe queue is empty we
		 * schedule the transfer a few frames ahead of the current
		 * frame position. Else two isochronous transfers might
		 * overlap.
		 */
		xfer->endpoint->isoc_next = (nframes + 3) &
		    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
		xfer->endpoint->is_synced = 1;
		DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
	}
	/*
	 * compute how many milliseconds the insertion is ahead of the
	 * current frame position:
	 */
	buf_offset = (xfer->endpoint->isoc_next - nframes) &
	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);

	/*
	 * pre-compute when the isochronous transfer will be finished:
	 */
	xfer->isoc_time_complete =
	    usb_isoc_time_expand(&sc->sc_bus, nframes) + buf_offset +
	    (((xfer->nframes << shift) + 7) / 8);

	/* get the real number of frames */

	nframes = xfer->nframes;

	buf_offset = 0;
	td_no = 0;

	plen = xfer->frlengths;

	/* toggle the DMA set we are using */
	xfer->flags_int.curr_dma_set ^= 1;

	/* get next DMA set */
	td = (ehci_itd_t *)xfer->td_start[xfer->flags_int.curr_dma_set];
	xfer->td_transfer_first = td;

	pp_last = &sc->sc_isoc_hs_p_last[xfer->endpoint->isoc_next];

	/* store starting position */

	xfer->qh_pos = xfer->endpoint->isoc_next;

	while (nframes) {
		if (td == NULL) {
			panic("%s:%d: out of TD's\n",
			    __FUNCTION__, __LINE__);
		}
		if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) {
			pp_last = &sc->sc_isoc_hs_p_last[0];
		}
		/* range check */
		if (*plen > xfer->max_frame_size) {
#ifdef LOSCFG_USB_DEBUG
			if (once) {
				once = 0;
				PRINTK("%s: frame length(%d) exceeds %d bytes "
				    "(frame truncated)\n",
				    __FUNCTION__, *plen, xfer->max_frame_size);
			}
#endif
			*plen = xfer->max_frame_size;
		}

		if (xfer->endpoint->usb_smask & (1 << td_no)) {
			status = (EHCI_ITD_SET_LEN(*plen) |
			    EHCI_ITD_ACTIVE |
			    EHCI_ITD_SET_PG(0));
			td->itd_status[td_no] = htohc32(sc, status);
			itd_offset[td_no] = buf_offset;
			buf_offset += *plen;
			plen++;
			nframes --;
		} else {
			td->itd_status[td_no] = 0;	/* not active */
			itd_offset[td_no] = buf_offset;
		}

		td_no++;

		if ((td_no == 8) || (nframes == 0)) {

			/* the rest of the transfers are not active, if any */
			for (x = td_no; x != 8; x++) {
				td->itd_status[x] = 0;	/* not active */
			}

			/* check if there is any data to be transferred */
			if (itd_offset[0] != buf_offset) {
				page_no = 0;
				itd_offset[td_no] = buf_offset;

				/* get first page offset */
				usbd_get_page(xfer->frbuffers, itd_offset[0], &buf_res);
				/* get page address */
#if USB_HAVE_BUSDMA
				page_addr = buf_res.physaddr & ~0xFFF;
#else
				page_addr = (unsigned int)buf_res.buffer & ~0xFFF;
#endif
				/* update page address */
				td->itd_bp[0] &= htohc32(sc, 0xFFF);
				td->itd_bp[0] |= htohc32(sc, page_addr);

				for (x = 0; x != td_no; x++) {
					/* set page number and page offset */
					status = (EHCI_ITD_SET_PG(page_no) |
#if USB_HAVE_BUSDMA
					    (buf_res.physaddr & 0xFFF));
#else
					((unsigned int)buf_res.buffer & 0xFFF));
#endif
					td->itd_status[x] |= htohc32(sc, status);

					/* get next page offset */
					if (itd_offset[x + 1] == buf_offset) {
						/*
						 * We subtract one so that
						 * we don't go off the last
						 * page!
						 */
						usbd_get_page(xfer->frbuffers, buf_offset - 1, &buf_res);
					} else {
						usbd_get_page(xfer->frbuffers, itd_offset[x + 1], &buf_res);
					}
#if USB_HAVE_BUSDMA
					/* check if we need a new page */
					if ((buf_res.physaddr ^ page_addr) & ~0xFFF) {
						/* new page needed */
						page_addr = buf_res.physaddr & ~0xFFF;
#else
					/* check if we need a new page */
					if (((unsigned int)buf_res.buffer ^ page_addr) & ~0xFFF) {
						/* new page needed */
						page_addr = (unsigned int)buf_res.buffer & ~0xFFF;
#endif
						if (page_no == 6) {
							panic("%s: too many pages\n", __FUNCTION__);
						}
						page_no++;
						page_no%= EHCI_ITD_BP_MAX;
						/* update page address */
						td->itd_bp[page_no] &= htohc32(sc, 0xFFF);
						td->itd_bp[page_no] |= htohc32(sc, page_addr);
					}
				}
			}
			/* set IOC bit if we are complete */
			if (nframes == 0) {
				td->itd_status[td_no - 1] |= htohc32(sc, EHCI_ITD_IOC);
			}
			usb_pc_cpu_flush(td->page_cache);
#ifdef LOSCFG_USB_DEBUG
			if (ehcidebug > 15) {
				DPRINTF("HS-TD %d\n", nframes);
				ehci_dump_itd(sc, td);
			}
#endif
			/* insert TD into schedule */
			EHCI_APPEND_HS_TD(td, *pp_last);
			pp_last++;

			td_no = 0;
			td_last = td;
			td = td->obj_next;
		}
	}

	xfer->td_transfer_last = td_last;

	/* update isoc_next */
	xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_hs_p_last[0]) &
	    (EHCI_VIRTUAL_FRAMELIST_COUNT - 1);
}

static void
ehci_device_isoc_hs_start(struct usb_xfer *xfer)
{
	/* put transfer on interrupt queue */
	ehci_transfer_intr_enqueue(xfer);
}

const struct usb_pipe_methods ehci_device_isoc_hs_methods = {
	.open = ehci_device_isoc_hs_open,
	.close = ehci_device_isoc_hs_close,
	.enter = ehci_device_isoc_hs_enter,
	.start = ehci_device_isoc_hs_start,
};

/*------------------------------------------------------------------------*
 * ehci root control support
 *------------------------------------------------------------------------*
 * Simulate a hardware hub by handling all the necessary requests.
 *------------------------------------------------------------------------*/

static const
struct usb_device_descriptor ehci_devd = {
	sizeof(struct usb_device_descriptor),
	UDESC_DEVICE,			/* type */
	{0x00, 0x02},			/* USB version */
	UDCLASS_HUB,			/* class */
	UDSUBCLASS_HUB,			/* subclass */
	UDPROTO_HSHUBSTT,		/* protocol */
	64,				/* max packet */
	{0}, {0}, {0x00, 0x01},		/* device id */
	1, 2, 0,			/* string indicies */
	1				/* # of configurations */
};

static const
struct usb_device_qualifier ehci_odevd = {
	sizeof(struct usb_device_qualifier),
	UDESC_DEVICE_QUALIFIER,		/* type */
	{0x00, 0x02},			/* USB version */
	UDCLASS_HUB,			/* class */
	UDSUBCLASS_HUB,			/* subclass */
	UDPROTO_FSHUB,			/* protocol */
	0,				/* max packet */
	0,				/* # of configurations */
	0
};

static const struct ehci_config_desc ehci_confd = {
	.confd = {
		.bLength = sizeof(struct usb_config_descriptor),
		.bDescriptorType = UDESC_CONFIG,
		.wTotalLength[0] = sizeof(ehci_confd),
		.bNumInterface = 1,
		.bConfigurationValue = 1,
		.iConfiguration = 0,
		.bmAttributes = UC_SELF_POWERED,
		.bMaxPower = 0		/* max power */
	},
	.ifcd = {
		.bLength = sizeof(struct usb_interface_descriptor),
		.bDescriptorType = UDESC_INTERFACE,
		.bNumEndpoints = 1,
		.bInterfaceClass = UICLASS_HUB,
		.bInterfaceSubClass = UISUBCLASS_HUB,
		.bInterfaceProtocol = 0,
	},
	.endpd = {
		.bLength = sizeof(struct usb_endpoint_descriptor),
		.bDescriptorType = UDESC_ENDPOINT,
		.bEndpointAddress = UE_DIR_IN | EHCI_INTR_ENDPT,
		.bmAttributes = UE_INTERRUPT,
		.wMaxPacketSize[0] = 8,	/* max packet (63 ports) */
		.bInterval = 255,
	},
};

static const
struct usb_hub_descriptor ehci_hubd = {
	.bDescLength = 0,		/* dynamic length */
	.bDescriptorType = UDESC_HUB,
};

uint16_t
ehci_get_port_speed_portsc(struct ehci_softc *sc, uint16_t index)
{
	uint32_t v;

	v = EOREAD4(sc, EHCI_PORTSC(index));
	v = (v >> EHCI_PORTSC_PSPD_SHIFT) & EHCI_PORTSC_PSPD_MASK;

	if (v == EHCI_PORT_SPEED_HIGH)
		return (UPS_HIGH_SPEED);
	if (v == EHCI_PORT_SPEED_LOW)
		return (UPS_LOW_SPEED);
	return (0);
}

uint16_t
ehci_get_port_speed_hostc(struct ehci_softc *sc, uint16_t index)
{
	uint32_t v;

	v = EOREAD4(sc, EHCI_HOSTC(index));
	v = (v >> EHCI_HOSTC_PSPD_SHIFT) & EHCI_HOSTC_PSPD_MASK;

	if (v == EHCI_PORT_SPEED_HIGH)
		return (UPS_HIGH_SPEED);
	if (v == EHCI_PORT_SPEED_LOW)
		return (UPS_LOW_SPEED);
	return (0);
}

static usb_error_t
ehci_roothub_exec(struct usb_device *udev,
    struct usb_device_request *req, const void **pptr, uint16_t *plength)
{
	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
	const char *str_ptr;
	const void *ptr;
	uint32_t port;
	uint32_t v;
	uint16_t len;
	uint16_t i;
	uint16_t value;
	uint16_t index;
	usb_error_t err = USB_ERR_NORMAL_COMPLETION;

	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);

	/* buffer reset */
	ptr = (const void *)&sc->sc_hub_desc;
	len = 0;

	value = UGETW(req->wValue);
	index = UGETW(req->wIndex);

	DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
	    "wValue=0x%04x wIndex=0x%04x\n",
	    req->bmRequestType, req->bRequest,
	    UGETW(req->wLength), value, index);

#define	C(x,y) ((x) | ((y) << 8))
	switch (C(req->bRequest, req->bmRequestType)) {
	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
		/*
		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
		 * for the integrated root hub.
		 */
		break;
	case C(UR_GET_CONFIG, UT_READ_DEVICE):
		len = 1;
		sc->sc_hub_desc.temp[0] = sc->sc_conf;
		break;
	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
		switch (value >> 8) {
		case UDESC_DEVICE:
			if ((value & 0xff) != 0) {
				err = USB_ERR_IOERROR;
				goto done;
			}
			len = sizeof(ehci_devd);
			ptr = (const void *)&ehci_devd;
			break;
			/*
			 * We can't really operate at another speed,
			 * but the specification says we need this
			 * descriptor:
			 */
		case UDESC_DEVICE_QUALIFIER:
			if ((value & 0xff) != 0) {
				err = USB_ERR_IOERROR;
				goto done;
			}
			len = sizeof(ehci_odevd);
			ptr = (const void *)&ehci_odevd;
			break;

		case UDESC_CONFIG:
			if ((value & 0xff) != 0) {
				err = USB_ERR_IOERROR;
				goto done;
			}
			len = sizeof(ehci_confd);
			ptr = (const void *)&ehci_confd;
			break;

		case UDESC_STRING:
			switch (value & 0xff) {
			case 0:	/* Language table */
				str_ptr = "\001";
				break;

			case 1:	/* Vendor */
				str_ptr = sc->sc_vendor;
				break;

			case 2:	/* Product */
				str_ptr = "EHCI root HUB";
				break;

			default:
				str_ptr = "";
				break;
			}

			len = usb_make_str_desc(
			    sc->sc_hub_desc.temp,
			    sizeof(sc->sc_hub_desc.temp),
			    str_ptr);
			break;
		default:
			err = USB_ERR_IOERROR;
			goto done;
		}
		break;
	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
		len = 1;
		sc->sc_hub_desc.temp[0] = 0;
		break;
	case C(UR_GET_STATUS, UT_READ_DEVICE):
		len = 2;
		USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
		break;
	case C(UR_GET_STATUS, UT_READ_INTERFACE):
	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
		len = 2;
		USETW(sc->sc_hub_desc.stat.wStatus, 0);
		break;
	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
		if (value >= EHCI_MAX_DEVICES) {
			err = USB_ERR_IOERROR;
			goto done;
		}
		sc->sc_addr = value;
		break;
	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
		if ((value != 0) && (value != 1)) {
			err = USB_ERR_IOERROR;
			goto done;
		}
		sc->sc_conf = value;
		break;
	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
		break;
	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
		err = USB_ERR_IOERROR;
		goto done;
	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
		break;
	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
		break;
		/* Hub requests */
	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
		break;
	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
		DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");

		if ((index < 1) ||
		    (index > sc->sc_noport)) {
			err = USB_ERR_IOERROR;
			goto done;
		}
		port = EHCI_PORTSC(index);
		v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
		switch (value) {
		case UHF_PORT_ENABLE:
			EOWRITE4(sc, port, v & ~EHCI_PS_PE);
			break;
		case UHF_PORT_SUSPEND:
			if ((v & EHCI_PS_SUSP) && (!(v & EHCI_PS_FPR))) {

				/*
				 * waking up a High Speed device is rather
				 * complicated if
				 */
				EOWRITE4(sc, port, v | EHCI_PS_FPR);
			}
			/* wait 20ms for resume sequence to complete */
			usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);

			EOWRITE4(sc, port, v & ~(EHCI_PS_SUSP |
			    EHCI_PS_FPR | (3 << 10) /* High Speed */ ));

			/* 4ms settle time */
			usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
			break;
		case UHF_PORT_POWER:
			EOWRITE4(sc, port, v & ~EHCI_PS_PP);
			break;
		case UHF_PORT_TEST:
			DPRINTFN(3, "clear port test "
			    "%d\n", index);
			break;
		case UHF_PORT_INDICATOR:
			DPRINTFN(3, "clear port ind "
			    "%d\n", index);
			EOWRITE4(sc, port, v & ~EHCI_PS_PIC);
			break;
		case UHF_C_PORT_CONNECTION:
			EOWRITE4(sc, port, v | EHCI_PS_CSC);
			break;
		case UHF_C_PORT_ENABLE:
			EOWRITE4(sc, port, v | EHCI_PS_PEC);
			break;
		case UHF_C_PORT_SUSPEND:
			EOWRITE4(sc, port, v | EHCI_PS_SUSP);
			break;
		case UHF_C_PORT_OVER_CURRENT:
			EOWRITE4(sc, port, v | EHCI_PS_OCC);
			break;
		case UHF_C_PORT_RESET:
			sc->sc_isreset = 0;
			break;
		default:
			err = USB_ERR_IOERROR;
			goto done;
		}
		break;
	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
		if ((value & 0xff) != 0) {
			err = USB_ERR_IOERROR;
			goto done;
		}
		v = EREAD4(sc, EHCI_HCSPARAMS);

		sc->sc_hub_desc.hubd = ehci_hubd;
		sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;

		if (EHCI_HCS_PPC(v))
			i = UHD_PWR_INDIVIDUAL;
		else
			i = UHD_PWR_NO_SWITCH;

		if (EHCI_HCS_P_INDICATOR(v))
			i |= UHD_PORT_IND;

		USETW(sc->sc_hub_desc.hubd.wHubCharacteristics, i);
		/* XXX can't find out? */
		sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 200;
		/* XXX don't know if ports are removable or not */
		sc->sc_hub_desc.hubd.bDescLength =
		    8 + ((sc->sc_noport + 7) / 8);
		len = sc->sc_hub_desc.hubd.bDescLength;
		break;
	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
		len = 16;
		(void)memset_s(sc->sc_hub_desc.temp, sizeof(sc->sc_hub_desc.temp), 0, len);
		break;
	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
		DPRINTFN(9, "get port status i=%d\n",
		    index);
		if ((index < 1) ||
		    (index > sc->sc_noport)) {
			err = USB_ERR_IOERROR;
			goto done;
		}
		v = EOREAD4(sc, EHCI_PORTSC(index));
		DPRINTFN(1, "port status=0x%04x\n", v);
		if (sc->sc_flags & EHCI_SCFLG_TT) {
			if (sc->sc_vendor_get_port_speed != NULL) {
				i = sc->sc_vendor_get_port_speed(sc, index);
			} else {
				device_printf(sc->sc_bus.bdev,
				    "EHCI_SCFLG_TT quirk is set but "
				    "sc_vendor_get_hub_speed() is NULL\n");
				i = UPS_HIGH_SPEED;
			}
		} else {
			i = UPS_HIGH_SPEED;
		}
		if (v & EHCI_PS_CS)
			i |= UPS_CURRENT_CONNECT_STATUS;
		if (v & EHCI_PS_PE)
			i |= UPS_PORT_ENABLED;
		if ((v & EHCI_PS_SUSP) && !(v & EHCI_PS_FPR))
			i |= UPS_SUSPEND;
		if (v & EHCI_PS_OCA)
			i |= UPS_OVERCURRENT_INDICATOR;
		if (v & EHCI_PS_PR)
			i |= UPS_RESET;
		if (v & EHCI_PS_PP)
			i |= UPS_PORT_POWER;
		USETW(sc->sc_hub_desc.ps.wPortStatus, i);
		i = 0;
		if (v & EHCI_PS_CSC)
			i |= UPS_C_CONNECT_STATUS;
		if (v & EHCI_PS_PEC)
			i |= UPS_C_PORT_ENABLED;
		if (v & EHCI_PS_OCC)
			i |= UPS_C_OVERCURRENT_INDICATOR;
		if (v & EHCI_PS_FPR)
			i |= UPS_C_SUSPEND;
		if (sc->sc_isreset)
			i |= UPS_C_PORT_RESET;
		USETW(sc->sc_hub_desc.ps.wPortChange, i);
		len = sizeof(sc->sc_hub_desc.ps);
		break;
	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
		err = USB_ERR_IOERROR;
		goto done;
	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
		break;
	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
		if ((index < 1) ||
		    (index > sc->sc_noport)) {
			err = USB_ERR_IOERROR;
			goto done;
		}
		port = EHCI_PORTSC(index);
		v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR;
		switch (value) {
		case UHF_PORT_ENABLE:
			EOWRITE4(sc, port, v | EHCI_PS_PE);
			break;
		case UHF_PORT_SUSPEND:
			EOWRITE4(sc, port, v | EHCI_PS_SUSP);
			break;
		case UHF_PORT_RESET:
			DPRINTFN(6, "reset port %d\n", index);
			if (EHCI_PS_IS_LOWSPEED(v) &&
			    (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
				/* Low speed device, give up ownership. */
				DPRINTFN(6, "Low speed device is not support!!!!\n");
				err = USB_ERR_INVAL;
				goto done;
			}
			/* Start reset sequence. */
			v &= ~(EHCI_PS_PE | EHCI_PS_PR);
			EOWRITE4(sc, port, v | EHCI_PS_PR);

			/* Wait for reset to complete. */
			usb_pause_mtx(&sc->sc_bus.bus_mtx,
			    USB_MS_TO_TICKS(usb_port_root_reset_delay));

			/* Terminate reset sequence. */
			if (!(sc->sc_flags & EHCI_SCFLG_NORESTERM))
				EOWRITE4(sc, port, v);

			/* Wait for HC to complete reset. */
			usb_pause_mtx(&sc->sc_bus.bus_mtx,
			    USB_MS_TO_TICKS(EHCI_PORT_RESET_COMPLETE));

			v = EOREAD4(sc, port);
			DPRINTF("ehci after reset, status=0x%08x\n", v);
			if (v & EHCI_PS_PR) {
				device_printf(sc->sc_bus.bdev,
				    "port reset timeout\n");
				err = USB_ERR_TIMEOUT;
				goto done;
			}
			if (!(v & EHCI_PS_PE) &&
			    (sc->sc_flags & EHCI_SCFLG_TT) == 0) {
				/* Not a high speed device, give up ownership.*/
				DPRINTFN(6, "Full speed device is not support!!!!\n");
				err = USB_ERR_INVAL;
				goto done;
			}
			sc->sc_isreset = 1;
			DPRINTF("ehci port %d reset, status = 0x%08x\n",
			    index, v);
			break;

		case UHF_PORT_POWER:
			DPRINTFN(3, "set port power %d\n", index);
			EOWRITE4(sc, port, v | EHCI_PS_PP);
			break;

		case UHF_PORT_TEST:
			DPRINTFN(3, "set port test %d\n", index);
			break;

		case UHF_PORT_INDICATOR:
			DPRINTFN(3, "set port ind %d\n", index);
			EOWRITE4(sc, port, v | EHCI_PS_PIC);
			break;

		default:
			err = USB_ERR_IOERROR;
			goto done;
		}
		break;
	case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
	case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
	case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
	case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
		break;
	default:
		err = USB_ERR_IOERROR;
		goto done;
	}
done:
	*plength = len;
	*pptr = ptr;
	return (err);
}

static void
ehci_xfer_setup(struct usb_setup_params *parm)
{
	struct usb_page_search page_info;
	struct usb_page_cache *pc;
	ehci_softc_t *sc;
	struct usb_xfer *xfer;
	void *last_obj;
	uint32_t nqtd;
	uint32_t nqh;
	uint32_t nsitd;
	uint32_t nitd;
	uint32_t n;

	sc = EHCI_BUS2SC(parm->udev->bus);
	xfer = parm->curr_xfer;

	nqtd = 0;
	nqh = 0;
	nsitd = 0;
	nitd = 0;

	/*
	 * compute maximum number of some structures
	 */
	if (parm->methods == &ehci_device_ctrl_methods) {

		/*
		 * The proof for the "nqtd" formula is illustrated like
		 * this:
		 *
		 * +------------------------------------+
		 * |                                    |
		 * |         |remainder ->              |
		 * |   +-----+---+                      |
		 * |   | xxx | x | frm 0                |
		 * |   +-----+---++                     |
		 * |   | xxx | xx | frm 1               |
		 * |   +-----+----+                     |
		 * |            ...                     |
		 * +------------------------------------+
		 *
		 * "xxx" means a completely full USB transfer descriptor
		 *
		 * "x" and "xx" means a short USB packet
		 *
		 * For the remainder of an USB transfer modulo
		 * "max_data_length" we need two USB transfer descriptors.
		 * One to transfer the remaining data and one to finalise
		 * with a zero length packet in case the "force_short_xfer"
		 * flag is set. We only need two USB transfer descriptors in
		 * the case where the transfer length of the first one is a
		 * factor of "max_frame_size". The rest of the needed USB
		 * transfer descriptors is given by the buffer size divided
		 * by the maximum data payload.
		 */
		parm->hc_max_packet_size = 0x400;
		parm->hc_max_packet_count = 1;
		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
#if USB_HAVE_BUSDMA
		xfer->flags_int.bdma_enable = 1;
#endif
		usbd_transfer_setup_sub(parm);

		nqh = 1;
		nqtd = ((2 * xfer->nframes) + 1	/* STATUS */
		    + (xfer->max_data_length / xfer->max_hc_frame_size));

	} else if (parm->methods == &ehci_device_bulk_methods) {

		parm->hc_max_packet_size = 0x400;
		parm->hc_max_packet_count = 1;
		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
#if USB_HAVE_BUSDMA
		xfer->flags_int.bdma_enable = 1;
#endif

		usbd_transfer_setup_sub(parm);

		nqh = 1;
		nqtd = ((2 * xfer->nframes)
		    + (xfer->max_data_length / xfer->max_hc_frame_size));

	} else if (parm->methods == &ehci_device_intr_methods) {

		if (parm->speed == USB_SPEED_HIGH) {
			parm->hc_max_packet_size = 0x400;
			parm->hc_max_packet_count = 3;
		} else if (parm->speed == USB_SPEED_FULL) {
			parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME;
			parm->hc_max_packet_count = 1;
		} else {
			parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME / 8;
			parm->hc_max_packet_count = 1;
		}

		parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX;
#if USB_HAVE_BUSDMA
		xfer->flags_int.bdma_enable = 1;
#endif

		usbd_transfer_setup_sub(parm);

		nqh = 1;
		nqtd = ((2 * xfer->nframes)
		    + (xfer->max_data_length / xfer->max_hc_frame_size));

	} else if (parm->methods == &ehci_device_isoc_fs_methods) {

		parm->hc_max_packet_size = 0x3FF;
		parm->hc_max_packet_count = 1;
		parm->hc_max_frame_size = 0x3FF;
#if USB_HAVE_BUSDMA
		xfer->flags_int.bdma_enable = 1;
#endif

		usbd_transfer_setup_sub(parm);

		nsitd = xfer->nframes;

	} else if (parm->methods == &ehci_device_isoc_hs_methods) {

		parm->hc_max_packet_size = 0x400;
		parm->hc_max_packet_count = 3;
		parm->hc_max_frame_size = 0xC00;
#if USB_HAVE_BUSDMA
		xfer->flags_int.bdma_enable = 1;
#endif

		usbd_transfer_setup_sub(parm);

		nitd = ((xfer->nframes + 7) / 8) <<
		    usbd_xfer_get_fps_shift(xfer);

	} else {

		parm->hc_max_packet_size = 0x400;
		parm->hc_max_packet_count = 1;
		parm->hc_max_frame_size = 0x400;

		usbd_transfer_setup_sub(parm);
	}

alloc_dma_set:

	if (parm->err) {
		return;
	}
	/*
	 * Allocate queue heads and transfer descriptors
	 */
	last_obj = NULL;

	if (usbd_transfer_setup_sub_malloc(
	    parm, &pc, sizeof(ehci_itd_t),
	    EHCI_ITD_ALIGN, nitd)) {
		parm->err = USB_ERR_NOMEM;
		return;
	}
	if (parm->buf) {
		for (n = 0; n != nitd; n++) {
			ehci_itd_t *td;

			usbd_get_page(pc + n, 0, &page_info);

			td = (ehci_itd_t *)page_info.buffer;

			/* init TD */
#if USB_HAVE_BUSDMA
			td->itd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_ITD);
#else
			td->itd_self = htohc32(sc, (unsigned int)page_info.buffer | EHCI_LINK_ITD);
#endif
			td->obj_next = (ehci_itd_t *)last_obj;
			td->page_cache = pc + n;

			last_obj = td;

			usb_pc_cpu_flush(pc + n);
		}
	}
	if (usbd_transfer_setup_sub_malloc(
	    parm, &pc, sizeof(ehci_sitd_t),
	    EHCI_SITD_ALIGN, nsitd)) {
		parm->err = USB_ERR_NOMEM;
		return;
	}
	if (parm->buf) {
		for (n = 0; n != nsitd; n++) {
			ehci_sitd_t *td;

			usbd_get_page(pc + n, 0, &page_info);

			td = (ehci_sitd_t *)page_info.buffer;

			/* init TD */
#if USB_HAVE_BUSDMA
			td->sitd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_SITD);
#else
			td->sitd_self = htohc32(sc, (unsigned int)page_info.buffer | EHCI_LINK_SITD);
#endif
			td->obj_next = (ehci_sitd_t *)last_obj;
			td->page_cache = pc + n;

			last_obj = td;

			usb_pc_cpu_flush(pc + n);
		}
	}
	if (usbd_transfer_setup_sub_malloc(
	    parm, &pc, sizeof(ehci_qtd_t),
	    EHCI_QTD_ALIGN, nqtd)) {
		parm->err = USB_ERR_NOMEM;
		return;
	}
	if (parm->buf) {
		for (n = 0; n != nqtd; n++) {
			ehci_qtd_t *qtd;

			usbd_get_page(pc + n, 0, &page_info);

			qtd = (ehci_qtd_t *)page_info.buffer;

			/* init TD */
#if USB_HAVE_BUSDMA
			qtd->qtd_self = htohc32(sc, page_info.physaddr);
#else
			qtd->qtd_self = htohc32(sc, (unsigned int)page_info.buffer);
#endif
			qtd->obj_next = (ehci_qtd_t *)last_obj;
			qtd->page_cache = pc + n;

			last_obj = qtd;

			usb_pc_cpu_flush(pc + n);
		}
	}
	xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;

	last_obj = NULL;

	if (usbd_transfer_setup_sub_malloc(
	    parm, &pc, sizeof(ehci_qh_t),
	    EHCI_QH_ALIGN, nqh)) {
		parm->err = USB_ERR_NOMEM;
		return;
	}
	if (parm->buf) {
		for (n = 0; n != nqh; n++) {
			ehci_qh_t *qh;

			usbd_get_page(pc + n, 0, &page_info);

			qh = (ehci_qh_t *)page_info.buffer;

			/* init QH */
#if USB_HAVE_BUSDMA
			qh->qh_self = htohc32(sc, page_info.physaddr | EHCI_LINK_QH);
#else
			qh->qh_self = htohc32(sc, (unsigned int)page_info.buffer | EHCI_LINK_QH);
#endif
			qh->obj_next = (ehci_qh_t *)last_obj;
			qh->page_cache = pc + n;

			last_obj = qh;

			usb_pc_cpu_flush(pc + n);
		}
	}
	xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;

	if (!xfer->flags_int.curr_dma_set) {
		xfer->flags_int.curr_dma_set = 1;
		goto alloc_dma_set;
	}
}

static void
ehci_xfer_unsetup(struct usb_xfer *xfer)
{
	return;
}

static void
ehci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
    struct usb_endpoint *ep)
{
	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);

	DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
	    ep, udev->address,
	    edesc->bEndpointAddress, udev->flags.usb_mode,
	    sc->sc_addr);

	if (udev->device_index != sc->sc_addr) {

		if ((udev->speed != USB_SPEED_HIGH) &&
		    ((udev->hs_hub_addr == 0) ||
		    (udev->hs_port_no == 0) ||
		    (udev->parent_hs_hub == NULL) ||
		    (udev->parent_hs_hub->hub == NULL))) {
			/* We need a transaction translator */
			DPRINTFN(2, "no hub or no port\n");
			goto done;
		}
		switch (edesc->bmAttributes & UE_XFERTYPE) {
		case UE_CONTROL:
			ep->methods = &ehci_device_ctrl_methods;
			DPRINTFN(2, "UE_CONTROL\n");
			break;
		case UE_INTERRUPT:
			ep->methods = &ehci_device_intr_methods;
			DPRINTFN(2, "UE_INTERRUPT\n");
			break;
		case UE_ISOCHRONOUS:
			if (udev->speed == USB_SPEED_HIGH) {
				ep->methods = &ehci_device_isoc_hs_methods;
			} else if (udev->speed == USB_SPEED_FULL) {
				ep->methods = &ehci_device_isoc_fs_methods;
			}
			DPRINTFN(2, "UE_ISOCHRONOUS\n");
			break;
		case UE_BULK:
			ep->methods = &ehci_device_bulk_methods;
			DPRINTFN(2, "UE_BULK\n");
			break;
		default:
			/* do nothing */
			break;
		}
	}
done:
	return;
}

static void
ehci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
{
	/*
	 * Wait until the hardware has finished any possible use of
	 * the transfer descriptor(s) and QH
	 */
	*pus = (1125);			/* microseconds */
}

static void
ehci_device_resume(struct usb_device *udev)
{
	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
	struct usb_xfer *xfer;
	const struct usb_pipe_methods *methods;

	DPRINTF("\n");

	USB_BUS_LOCK(udev->bus);

	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {

		if (xfer->xroot->udev == udev) {

			methods = xfer->endpoint->methods;

			if ((methods == &ehci_device_bulk_methods) ||
			    (methods == &ehci_device_ctrl_methods)) {
				EHCI_APPEND_QH((ehci_qh_t *)xfer->qh_start[xfer->flags_int.curr_dma_set],
				    sc->sc_async_p_last);
			}
			if (methods == &ehci_device_intr_methods) {
				EHCI_APPEND_QH((ehci_qh_t *)xfer->qh_start[xfer->flags_int.curr_dma_set],
				    sc->sc_intr_p_last[xfer->qh_pos]);
			}
		}
	}

	USB_BUS_UNLOCK(udev->bus);

	return;
}

static void
ehci_device_suspend(struct usb_device *udev)
{
	ehci_softc_t *sc = EHCI_BUS2SC(udev->bus);
	struct usb_xfer *xfer;
	const struct usb_pipe_methods *methods;

	DPRINTF("\n");

	USB_BUS_LOCK(udev->bus);

	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {

		if (xfer->xroot->udev == udev) {

			methods = xfer->endpoint->methods;

			if ((methods == &ehci_device_bulk_methods) ||
			    (methods == &ehci_device_ctrl_methods)) {
				EHCI_REMOVE_QH((ehci_qh_t *)xfer->qh_start[xfer->flags_int.curr_dma_set],
				    sc->sc_async_p_last);
			}
			if (methods == &ehci_device_intr_methods) {
				EHCI_REMOVE_QH((ehci_qh_t *)xfer->qh_start[xfer->flags_int.curr_dma_set],
				    sc->sc_intr_p_last[xfer->qh_pos]);
			}
		}
	}

	USB_BUS_UNLOCK(udev->bus);
}

static void
ehci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
{
	struct ehci_softc *sc = EHCI_BUS2SC(bus);

	switch (state) {
	case USB_HW_POWER_SUSPEND:
	case USB_HW_POWER_SHUTDOWN:
		ehci_suspend(sc);
		break;
	case USB_HW_POWER_RESUME:
		ehci_resume(sc);
		break;
	default:
		break;
	}
}

static void
ehci_set_hw_power(struct usb_bus *bus)
{
	ehci_softc_t *sc = EHCI_BUS2SC(bus);
	uint32_t temp;
	uint32_t flags;

	DPRINTF("\n");

	USB_BUS_LOCK(bus);

	flags = bus->hw_power_state;

	temp = EOREAD4(sc, EHCI_USBCMD);

	temp &= ~(EHCI_CMD_ASE | EHCI_CMD_PSE);

	if (flags & (USB_HW_POWER_CONTROL |
	    USB_HW_POWER_BULK)) {
		DPRINTF("Async is active\n");
		temp |= EHCI_CMD_ASE;
	}
	if (flags & (USB_HW_POWER_INTERRUPT |
	    USB_HW_POWER_ISOC)) {
		DPRINTF("Periodic is active\n");
		temp |= EHCI_CMD_PSE;
	}
	EOWRITE4(sc, EHCI_USBCMD, temp);

	USB_BUS_UNLOCK(bus);

	return;
}

static void
ehci_start_dma_delay_second(struct usb_xfer *xfer)
{
	struct ehci_softc *sc = EHCI_BUS2SC(xfer->xroot->bus);

	DPRINTF("\n");

	/* trigger doorbell */
	ehci_doorbell_async(sc);

	/* give the doorbell 4ms */
	usbd_transfer_timeout_ms(xfer,
	    (void (*)(void *))&usb_dma_delay_done_cb, 4);
}

/*
 * Ring the doorbell twice before freeing any DMA descriptors. Some host
 * controllers apparently cache the QH descriptors and need a message
 * that the cache needs to be discarded.
 */
static void
ehci_start_dma_delay(struct usb_xfer *xfer)
{
	struct ehci_softc *sc = EHCI_BUS2SC(xfer->xroot->bus);

	DPRINTF("\n");

	/* trigger doorbell */
	ehci_doorbell_async(sc);

	/* give the doorbell 4ms */
	usbd_transfer_timeout_ms(xfer,
	    (void (*)(void *))&ehci_start_dma_delay_second, 4);
}

const struct usb_bus_methods ehci_bus_methods = {
	.endpoint_init = ehci_ep_init,
	.xfer_setup = ehci_xfer_setup,
	.xfer_unsetup = ehci_xfer_unsetup,
	.get_dma_delay = ehci_get_dma_delay,
	.device_resume = ehci_device_resume,
	.device_suspend = ehci_device_suspend,
	.set_hw_power = ehci_set_hw_power,
	.set_hw_power_sleep = ehci_set_hw_power_sleep,
	.roothub_exec = ehci_roothub_exec,
	.xfer_poll = ehci_do_poll,
	.start_dma_delay = ehci_start_dma_delay,
};
#undef USB_DEBUG_VAR