Loading...
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 | /* * Open Host Controller Interface driver for USB. * * (C) Copyright 1999 Gregory P. Smith <greg@electricrain.com> * Significant code from the following individuals has also been used: * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at> [ohci-hcd.c] * (C) Copyright 1999 Linus Torvalds [uhci.c] * * This is the "other" host controller interface for USB. You will * find this on many non-Intel based motherboards, and of course the * Mac. As Linus hacked his UHCI driver together first, I originally * modeled this after his.. (it should be obvious) * * To get started in USB, I used the "Universal Serial Bus System * Architecture" book by Mindshare, Inc. It was a reasonable introduction * and overview of USB and the two dominant host controller interfaces * however you're better off just reading the real specs available * from www.usb.org as you'll need them to get enough detailt to * actually implement a HCD. The book has many typos and omissions * Beware, the specs are the victim of a committee. * * This code was written with Guinness on the brain, xsnow on the desktop * and Orbital, Orb, Enya & Massive Attack on the CD player. What a life! ;) * * No filesystems were harmed in the development of this code. * * $Id: ohci.c,v 1.43 1999/05/16 22:35:24 greg Exp $ */ #include <linux/config.h> #include <linux/module.h> #include <linux/pci.h> #include <linux/kernel.h> #include <linux/delay.h> #include <linux/ioport.h> #include <linux/sched.h> #include <linux/malloc.h> #include <linux/smp_lock.h> #include <linux/errno.h> #include <asm/spinlock.h> #include <asm/io.h> #include <asm/irq.h> #include <asm/system.h> #include "ohci.h" #ifdef CONFIG_APM #include <linux/apm_bios.h> static int handle_apm_event(apm_event_t event); static int apm_resume = 0; #endif static DECLARE_WAIT_QUEUE_HEAD(ohci_configure); #ifdef CONFIG_USB_OHCI_DEBUG #define OHCI_DEBUG /* to make typing it easier.. */ #endif int MegaDebug = 0; /* SIGUSR2 to the control thread toggles this */ #ifdef OHCI_TIMER static struct timer_list ohci_timer; /* timer for root hub polling */ #endif static spinlock_t ohci_edtd_lock = SPIN_LOCK_UNLOCKED; #define FIELDS_OF_ED(e) le32_to_cpup(&e->status), le32_to_cpup(&e->tail_td), \ le32_to_cpup(&e->_head_td), le32_to_cpup(&e->next_ed) #define FIELDS_OF_TD(t) le32_to_cpup(&t->info), le32_to_cpup(&t->cur_buf), \ le32_to_cpup(&t->next_td), le32_to_cpup(&t->buf_end) #ifdef OHCI_DEBUG static const char *cc_names[16] = { "no error", "CRC error", "bit stuff error", "data toggle mismatch", "stall", "device not responding", "PID check failed", "unexpected PID", "data overrun", "data underrun", "reserved (10)", "reserved (11)", "buffer overrun", "buffer underrun", "not accessed (14)", "not accessed" }; #endif /* * Add a chain of TDs to the end of the TD list on a given ED. * * This function uses the first TD of the chain as the new dummy TD * for the ED, and uses the old dummy TD instead of the first TD * of the chain. The reason for this is that this makes it possible * to update the TD chain without needing any locking between the * CPU and the OHCI controller. * * The return value is the pointer to the new first TD (the old * dummy TD). * * Important! This function is not re-entrant w.r.t. each ED. * Locking ohci_edtd_lock while using the function is a must * if there is any possibility of another CPU or an interrupt routine * calling this function with the same ED. * * This function can be called by the interrupt handler. */ static struct ohci_td *ohci_add_td_to_ed(struct ohci_td *td, struct ohci_td *last_td, struct ohci_ed *ed) { struct ohci_td *t, *dummy_td; u32 new_dummy; if (ed->tail_td == 0) { printk(KERN_ERR "eek! an ED without a dummy_td\n"); return td; } /* Get a pointer to the current dummy TD. */ dummy_td = bus_to_virt(ed_tail_td(ed)); for (t = td; ; t = bus_to_virt(le32_to_cpup(&t->next_td))) { t->ed = ed; if (t == last_td) break; } /* Make the last TD point back to the first, since it * will become the new dummy TD. */ new_dummy = cpu_to_le32(virt_to_bus(td)); last_td->next_td = new_dummy; /* Copy the contents of the first TD into the dummy */ *dummy_td = *td; /* Turn the first TD into a dummy */ make_dumb_td(td); /* Set the HC's tail pointer to the new dummy */ ed->tail_td = new_dummy; return dummy_td; /* replacement head of chain */ } /* ohci_add_td_to_ed() */ /* * Add a whole chain of TDs to an ED using the above function. * The same restrictions apply. * * XXX This function is being removed in the future! XXX */ static struct ohci_td *ohci_add_td_chain_to_ed(struct ohci_td *td, struct ohci_ed *ed) { struct ohci_td *cur_td; if (!td) return NULL; /* Find the last TD in this chain, storing its pointer in cur_td */ cur_td = td; for (;;) { __u32 next_td = cur_td->next_td; /* advance to the next td, exit if there isn't one */ if (!next_td) break; cur_td = bus_to_virt(le32_to_cpup(&next_td)); } return td = ohci_add_td_to_ed(td, cur_td, ed); } /* ohci_add_td_chain_to_ed() */ /* .......... */ void ohci_start_control(struct ohci *ohci) { /* tell the HC to start processing the control list */ writel_set(OHCI_USB_CLE, &ohci->regs->control); writel_set(OHCI_CMDSTAT_CLF, &ohci->regs->cmdstatus); } void ohci_start_bulk(struct ohci *ohci) { /* tell the HC to start processing the bulk list */ writel_set(OHCI_USB_BLE, &ohci->regs->control); writel_set(OHCI_CMDSTAT_BLF, &ohci->regs->cmdstatus); } void ohci_start_periodic(struct ohci *ohci) { /* enable processing periodic (intr) transfers starting next frame */ writel_set(OHCI_USB_PLE, &ohci->regs->control); } void ohci_start_isoc(struct ohci *ohci) { /* enable processing isoc. transfers starting next frame */ writel_set(OHCI_USB_IE, &ohci->regs->control); } /* * Add an ED to the hardware register ED list pointed to by hw_listhead_p * This function only makes sense for Control and Bulk EDs. */ static void ohci_add_ed_to_hw(struct ohci_ed *ed, void* hw_listhead_p) { __u32 listhead; unsigned long flags; spin_lock_irqsave(&ohci_edtd_lock, flags); listhead = readl(hw_listhead_p); /* if the list is not empty, insert this ED at the front */ /* XXX should they go on the end? */ ed->next_ed = cpu_to_le32(listhead); /* update the hardware listhead pointer */ writel(virt_to_bus(ed), hw_listhead_p); spin_unlock_irqrestore(&ohci_edtd_lock, flags); } /* ohci_add_ed_to_hw() */ /* * Put a control ED on the controller's list */ void ohci_add_control_ed(struct ohci *ohci, struct ohci_ed *ed) { ohci_add_ed_to_hw(ed, &ohci->regs->ed_controlhead); ohci_start_control(ohci); } /* ohci_add_control_ed() */ /* * Put a bulk ED on the controller's list */ void ohci_add_bulk_ed(struct ohci *ohci, struct ohci_ed *ed) { ohci_add_ed_to_hw(ed, &ohci->regs->ed_bulkhead); ohci_start_bulk(ohci); } /* ohci_add_bulk_ed() */ /* * Put a periodic ED on the appropriate list given the period. */ void ohci_add_periodic_ed(struct ohci *ohci, struct ohci_ed *ed, int period) { struct ohci_ed *int_ed; struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub); unsigned long flags; /* * Pick a good frequency endpoint based on the requested period */ int_ed = &root_hub->ed[ms_to_ed_int(period)]; #ifdef OHCI_DEBUG printk(KERN_DEBUG "usb-ohci: Using INT ED queue %d for %dms period\n", ms_to_ed_int(period), period); #endif spin_lock_irqsave(&ohci_edtd_lock, flags); /* * Insert this ED at the front of the list. */ ed->next_ed = int_ed->next_ed; int_ed->next_ed = cpu_to_le32(virt_to_bus(ed)); spin_unlock_irqrestore(&ohci_edtd_lock, flags); ohci_start_periodic(ohci); } /* ohci_add_periodic_ed() */ /* * Put an isochronous ED on the controller's list */ inline void ohci_add_isoc_ed(struct ohci *ohci, struct ohci_ed *ed) { ohci_add_periodic_ed(ohci, ed, 1); } /* * This will be used for the interrupt to wake us up on the next SOF */ DECLARE_WAIT_QUEUE_HEAD(start_of_frame_wakeup); static void ohci_wait_sof(struct ohci_regs *regs) { DECLARE_WAITQUEUE(wait, current); add_wait_queue(&start_of_frame_wakeup, &wait); /* clear the SOF interrupt status and enable it */ writel(OHCI_INTR_SF, ®s->intrstatus); writel(OHCI_INTR_SF, ®s->intrenable); schedule_timeout(HZ/10); remove_wait_queue(&start_of_frame_wakeup, &wait); } /* * Guarantee that an ED is safe to be modified by the HCD (us). * * This function can NOT be called from an interrupt. */ void ohci_wait_for_ed_safe(struct ohci_regs *regs, struct ohci_ed *ed, int ed_type) { __u32 *hw_listcurrent; /* tell the controller to skip this ED */ ed->status |= cpu_to_le32(OHCI_ED_SKIP); switch (ed_type) { case HCD_ED_CONTROL: hw_listcurrent = ®s->ed_controlcurrent; break; case HCD_ED_BULK: hw_listcurrent = ®s->ed_bulkcurrent; break; case HCD_ED_ISOC: case HCD_ED_INT: hw_listcurrent = ®s->ed_periodcurrent; break; default: return; } /* * If the HC is processing this ED we need to wait until the * at least the next frame. */ if (virt_to_bus(ed) == readl(hw_listcurrent)) { #ifdef OHCI_DEBUG printk(KERN_INFO "Waiting a frame for OHC to finish with ED %p [%x %x %x %x]\n", ed, FIELDS_OF_ED(ed)); #endif ohci_wait_sof(regs); } return; /* The ED is now safe */ } /* ohci_wait_for_ed_safe() */ /* * Remove an ED from the HC's list. * This function can ONLY be used for Control or Bulk EDs. * * Note that the SKIP bit is left on in the removed ED. */ void ohci_remove_norm_ed_from_hw(struct ohci *ohci, struct ohci_ed *ed, int ed_type) { unsigned long flags; struct ohci_regs *regs = ohci->regs; struct ohci_ed *cur; __u32 bus_ed = virt_to_bus(ed); __u32 bus_cur; __u32 *hw_listhead_p; if (ed == NULL || !bus_ed) return; ed->status |= cpu_to_le32(OHCI_ED_SKIP); switch (ed_type) { case HCD_ED_CONTROL: hw_listhead_p = ®s->ed_controlhead; break; case HCD_ED_BULK: hw_listhead_p = ®s->ed_bulkhead; break; default: printk(KERN_ERR "Unknown HCD ED type %d.\n", ed_type); return; } bus_cur = readl(hw_listhead_p); if (bus_cur == 0) return; /* the list is already empty */ cur = bus_to_virt(bus_cur); spin_lock_irqsave(&ohci_edtd_lock, flags); /* if its the head ED, move the head */ if (bus_cur == bus_ed) { writel(le32_to_cpup(&cur->next_ed), hw_listhead_p); } else if (cur->next_ed != 0) { struct ohci_ed *prev; /* walk the list and unlink the ED if found */ do { prev = cur; cur = bus_to_virt(le32_to_cpup(&cur->next_ed)); if (cur == ed) { /* unlink from the list */ prev->next_ed = cur->next_ed; break; } } while (cur->next_ed != 0); } /* * Make sure this ED is not being accessed by the HC as we speak. */ ohci_wait_for_ed_safe(regs, ed, ed_type); /* clear any links from the ED for safety */ ed->next_ed = 0; spin_unlock_irqrestore(&ohci_edtd_lock, flags); } /* ohci_remove_norm_ed_from_hw() */ /* * Remove an ED from the controller's control list. Note that the SKIP bit * is left on in the removed ED. */ inline void ohci_remove_control_ed(struct ohci *ohci, struct ohci_ed *ed) { ohci_remove_norm_ed_from_hw(ohci, ed, HCD_ED_CONTROL); } /* * Remove an ED from the controller's bulk list. Note that the SKIP bit * is left on in the removed ED. */ inline void ohci_remove_bulk_ed(struct ohci *ohci, struct ohci_ed *ed) { ohci_remove_norm_ed_from_hw(ohci, ed, HCD_ED_BULK); } /* * Remove all the EDs which have a given device address from a list. * Used when the device is unplugged. * Returns 1 if anything was changed. */ static int ohci_remove_device_list(__u32 *headp, int devnum) { struct ohci_ed *ed; __u32 *prevp = headp; int removed = 0; while (*prevp != 0) { ed = bus_to_virt(le32_to_cpup(prevp)); if ((le32_to_cpup(&ed->status) & OHCI_ED_FA) == devnum) { /* set the controller to skip this one and remove it from the list */ ed->status |= cpu_to_le32(OHCI_ED_SKIP); *prevp = ed->next_ed; removed = 1; } else { prevp = &ed->next_ed; } } wmb(); return removed; } /* * Remove all the EDs for a given device from all lists. */ void ohci_remove_device(struct ohci *ohci, int devnum) { unsigned long flags; __u32 head; struct ohci_regs *regs = ohci->regs; struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub); spin_lock_irqsave(&ohci_edtd_lock, flags); /* Control list */ head = cpu_to_le32(readl(®s->ed_controlhead)); if (ohci_remove_device_list(&head, devnum)) writel(le32_to_cpup(&head), ®s->ed_controlhead); /* Bulk list */ head = cpu_to_le32(readl(®s->ed_bulkhead)); if (ohci_remove_device_list(&head, devnum)) writel(le32_to_cpup(&head), ®s->ed_bulkhead); /* Interrupt/iso list */ head = cpu_to_le32(virt_to_bus(&root_hub->ed[ED_INT_32])); ohci_remove_device_list(&head, devnum); /* * Wait until the start of the next frame to ensure * that the HC has seen any changes. */ ohci_wait_sof(ohci->regs); spin_unlock_irqrestore(&ohci_edtd_lock, flags); } /* * Remove a TD from the given EDs TD list. The TD is freed as well. */ static void ohci_remove_td_from_ed(struct ohci_td *td, struct ohci_ed *ed) { unsigned long flags; struct ohci_td *head_td; if ((td == NULL) || (ed == NULL)) return; if (ed_head_td(ed) == 0) return; spin_lock_irqsave(&ohci_edtd_lock, flags); /* set the "skip me bit" in this ED */ ed->status |= cpu_to_le32(OHCI_ED_SKIP); /* XXX Assuming this list will never be circular */ head_td = bus_to_virt(ed_head_td(ed)); if (virt_to_bus(td) == ed_head_td(ed)) { /* It's the first TD, remove it. */ set_ed_head_td(ed, head_td->next_td); } else { struct ohci_td *prev_td, *cur_td; /* FIXME: collapse this into a nice simple loop :) */ if (head_td->next_td != 0) { prev_td = head_td; cur_td = bus_to_virt(le32_to_cpup(&head_td->next_td)); for (;;) { if (td == cur_td) { /* remove it */ prev_td->next_td = cur_td->next_td; break; } if (cur_td->next_td == 0) break; prev_td = cur_td; cur_td = bus_to_virt(le32_to_cpup(&cur_td->next_td)); } } } td->next_td = 0; /* remove the TDs links */ td->ed = NULL; /* return this TD to the pool of free TDs */ ohci_free_td(td); /* unset the "skip me bit" in this ED */ ed->status &= cpu_to_le32(~OHCI_ED_SKIP); spin_unlock_irqrestore(&ohci_edtd_lock, flags); } /* ohci_remove_td_from_ed() */ /* * Get a pointer (virtual) to an available TD from the given device's * pool. Return NULL if none are left. */ static struct ohci_td *ohci_get_free_td(struct ohci_device *dev) { int idx; #if 0 printk(KERN_DEBUG "in ohci_get_free_td()\n"); #endif /* FIXME: this is horribly inefficient */ for (idx=0; idx < NUM_TDS; idx++) { #if 0 show_ohci_td(&dev->td[idx]); #endif if (!td_allocated(dev->td[idx])) { struct ohci_td *new_td = &dev->td[idx]; /* zero out the TD */ memset(new_td, 0, sizeof(*new_td)); /* mark the new TDs as unaccessed */ new_td->info = cpu_to_le32(OHCI_TD_CC_NEW); /* mark it as allocated */ allocate_td(new_td); return new_td; } } printk(KERN_ERR "usb-ohci: unable to allocate a TD\n"); return NULL; } /* ohci_get_free_td() */ /* * Get a pointer (virtual) to an available TD from the given device's * pool. Return NULL if none are left. * * NOTE: This function does not allocate and attach the dummy_td. * That is done in ohci_fill_ed(). FIXME: it should probably be moved * into here. */ static struct ohci_ed *ohci_get_free_ed(struct ohci_device *dev) { int idx; /* FIXME: this is horribly inefficient */ for (idx=0; idx < NUM_EDS; idx++) { if (!ed_allocated(dev->ed[idx])) { struct ohci_ed *new_ed = &dev->ed[idx]; /* zero out the ED */ memset(new_ed, 0, sizeof(*new_ed)); /* all new EDs start with the SKIP bit set */ new_ed->status |= cpu_to_le32(OHCI_ED_SKIP); /* mark it as allocated */ allocate_ed(new_ed); return new_ed; } } printk(KERN_ERR "usb-ohci: unable to allocate an ED\n"); return NULL; } /* ohci_get_free_ed() */ /* * Free an OHCI ED and all of the TDs on its list. It is assumed that * this ED is not active. You should call ohci_wait_for_ed_safe() * beforehand if you can't guarantee that. */ void ohci_free_ed(struct ohci_ed *ed) { if (!ed) return; if (ed_head_td(ed) != 0) { struct ohci_td *td, *tail_td, *next_td; td = bus_to_virt(ed_head_td(ed)); tail_td = bus_to_virt(ed_tail_td(ed)); for (;;) { next_td = bus_to_virt(le32_to_cpup(&td->next_td)); ohci_free_td(td); if (td == tail_td) break; td = next_td; } } ed->status &= cpu_to_le32(~(__u32)ED_ALLOCATED); } /* ohci_free_ed() */ /* * Initialize a TD * * dir = OHCI_TD_D_IN, OHCI_TD_D_OUT, or OHCI_TD_D_SETUP * toggle = TOGGLE_AUTO, TOGGLE_DATA0, TOGGLE_DATA1 */ struct ohci_td *ohci_fill_new_td(struct ohci_td *td, int dir, int toggle, __u32 flags, void *data, __u32 len, void *dev_id, usb_device_irq completed) { /* hardware fields */ td->info = cpu_to_le32(OHCI_TD_CC_NEW | (dir & OHCI_TD_D) | (toggle & OHCI_TD_DT) | flags); td->cur_buf = (data == NULL) ? 0 : cpu_to_le32(virt_to_bus(data)); td->buf_end = (len == 0) ? 0 : cpu_to_le32(le32_to_cpup(&td->cur_buf) + len - 1); /* driver fields */ td->data = data; td->dev_id = dev_id; td->completed = completed; #if 0 printk(KERN_DEBUG "ohci_fill_new_td created:\n"); show_ohci_td(td); #endif return td; } /* ohci_fill_new_td() */ /* * Initialize a new ED on device dev, including allocating and putting the * dummy tail_td on its queue if it doesn't already have one. Any * TDs on this ED other than the dummy will be lost (so there better * not be any!). This assumes that the ED is Allocated and will * force the Allocated bit on. */ struct ohci_ed *ohci_fill_ed(struct ohci_device *dev, struct ohci_ed *ed, int maxpacketsize, int lowspeed, int endp_id, int isoc_tds) { struct ohci_td *dummy_td; if (ed_head_td(ed) != ed_tail_td(ed)) printk(KERN_ERR "Reusing a non-empty ED %p!\n", ed); if (!ed->tail_td) { dummy_td = ohci_get_free_td(dev); if (dummy_td == NULL) { printk(KERN_ERR "Error allocating dummy TD for ED %p\n", ed); return NULL; /* no dummy available! */ } make_dumb_td(dummy_td); /* flag it as a dummy */ ed->tail_td = cpu_to_le32(virt_to_bus(dummy_td)); } else { dummy_td = bus_to_virt(ed_tail_td(ed)); if (!td_dummy(*dummy_td)) printk(KERN_ERR "ED %p's dummy %p is screwy\n", ed, dummy_td); } /* set the head TD to the dummy and clear the Carry & Halted bits */ ed->_head_td = ed->tail_td; ed->status = cpu_to_le32( ed_set_maxpacket(maxpacketsize) | ed_set_speed(lowspeed) | (endp_id & 0x7ff) | ((isoc_tds == 0) ? OHCI_ED_F_NORM : OHCI_ED_F_ISOC)); allocate_ed(ed); ed->next_ed = 0; return ed; } /* ohci_fill_ed() */ /* * Create a chain of Normal TDs to be used for a large data transfer * (bulk or control). * * Returns the head TD in the chain. */ struct ohci_td *ohci_build_td_chain(struct ohci_device *dev, void *data, unsigned int len, int dir, __u32 toggle, int round, int auto_free, void* dev_id, usb_device_irq handler, __u32 next_td) { struct ohci_td *head, *cur_td; __u32 bus_data_start, bus_data_end; unsigned short max_page0_len; if (!data || (len == 0)) return NULL; /* Setup the first TD, leaving buf_end = 0 */ head = ohci_get_free_td(dev); if (head == NULL) { printk(KERN_ERR "usb-ohci: out of TDs\n"); return NULL; } ohci_fill_new_td(head, td_set_dir_out(dir), toggle & OHCI_TD_DT, (round ? OHCI_TD_ROUND : 0), data, 0, dev_id, handler); if (!auto_free) noauto_free_td(head); cur_td = head; /* AFICT, that the OHCI controller takes care of the innards of * bulk & control data transfers by sending zero length * packets as necessary if the transfer falls on an even packet * size boundary, we don't need a special TD for that. */ while (len > 0) { bus_data_start = virt_to_bus(data); bus_data_end = virt_to_bus(data+(len-1)); /* check the 4096 byte alignment of the start of the data */ max_page0_len = 0x1000 - (bus_data_start & 0xfff); /* check if the remaining data occupies more than two pages */ if ((max_page0_len < len) && (len - max_page0_len > 0x1000)) { struct ohci_td *new_td; /* Point this TD to data up through the end of * the second page */ cur_td->buf_end = bus_data_start + (max_page0_len + 0xfff); /* adjust the data pointer & remaining length */ data += (max_page0_len + 0x1000); len -= (max_page0_len + 0x1000); /* TODO lookup effect of rounding bit on * individual TDs vs. whole TD chain transfers; * disable cur_td's rounding bit here if needed. */ /* mark that this is not the last TD... */ clear_td_endofchain(cur_td); /* allocate another td */ new_td = ohci_get_free_td(dev); if (new_td == NULL) { printk(KERN_ERR "usb-ohci: out of TDs\n"); /* FIXME: free any allocated TDs */ return NULL; } ohci_fill_new_td(new_td, td_set_dir_out(dir), TOGGLE_AUTO, /* toggle Data0/1 via the ED */ round ? OHCI_TD_ROUND : 0, data, 0, dev_id, handler); if (!auto_free) noauto_free_td(new_td); /* Link the new TD to the chain & advance */ cur_td->next_td = virt_to_bus(new_td); cur_td = new_td; } else { /* Last TD in this chain, normal buf_end is fine */ cur_td->buf_end = bus_data_end; set_td_endofchain(cur_td); len = 0; break; } } /* while */ /* link the given next_td to the end of this chain */ cur_td->next_td = next_td; return head; } /* ohci_build_td_chain() */ /* * Compute the number of bytes that have been transferred on a given * TD. Do not call this on TDs that are active on the host * controller. */ static __u16 ohci_td_bytes_done(struct ohci_td *td) { __u16 result; __u32 bus_data_start, bus_data_end; bus_data_start = virt_to_bus(td->data); if (!td->data || !bus_data_start) return 0; /* if cur_buf is 0, all data has been transferred */ if (!td->cur_buf) { return td->buf_end - bus_data_start + 1; } bus_data_end = td->cur_buf; /* is it on the same page? */ if ((bus_data_start & ~0xfff) == (bus_data_end & ~0xfff)) { result = bus_data_end - bus_data_start; } else { /* compute the amount transferred on the first page */ result = 0x1000 - (bus_data_start & 0xfff); /* add the amount done in the second page */ result += (bus_data_end & 0xfff); } return result; } /* ohci_td_bytes_done() */ /********************************** * OHCI interrupt list operations * **********************************/ /* * Request an interrupt handler for one "pipe" of a USB device. * (this function is pretty minimal right now) * * At the moment this is only good for input interrupts. (ie: for a * mouse or keyboard) * * Period is desired polling interval in ms. The closest, shorter * match will be used. Powers of two from 1-32 are supported by OHCI. */ static int ohci_request_irq(struct usb_device *usb, unsigned int pipe, usb_device_irq handler, int period, void *dev_id) { struct ohci_device *dev = usb_to_ohci(usb); struct ohci_td *td; struct ohci_ed *interrupt_ed; /* endpoint descriptor for this irq */ int maxps = usb_maxpacket(usb, pipe); /* Get an ED and TD */ interrupt_ed = ohci_get_free_ed(dev); if (!interrupt_ed) { printk(KERN_ERR "Out of EDs on device %p in ohci_request_irq\n", dev); return -1; } td = ohci_get_free_td(dev); if (!td) { printk(KERN_ERR "Out of TDs in ohci_request_irq\n"); ohci_free_ed(interrupt_ed); return -1; } /* * Set the max packet size, device speed, endpoint number, usb * device number (function address), and type of TD. */ ohci_fill_ed(dev, interrupt_ed, maxps, usb_pipeslow(pipe), usb_pipe_endpdev(pipe), 0 /* normal TDs */); /* Fill in the TD */ if (maxps > sizeof(dev->data)) maxps = sizeof(dev->data); ohci_fill_new_td(td, td_set_dir_out(usb_pipeout(pipe)), TOGGLE_AUTO, OHCI_TD_ROUND, dev->data, maxps, dev_id, handler); /* * TODO: be aware of how the OHCI controller deals with DMA * spanning more than one page. */ /* * Put the TD onto our ED and make sure its ready to run */ td = ohci_add_td_to_ed(td, td, interrupt_ed); interrupt_ed->status &= cpu_to_le32(~OHCI_ED_SKIP); ohci_unhalt_ed(interrupt_ed); /* Make sure all the stores above get done before * the store which tells the OHCI about the new ed. */ wmb(); /* Assimilate the new ED into the collective */ ohci_add_periodic_ed(dev->ohci, interrupt_ed, period); /* FIXME: return a request handle that can be used by the * caller to cancel this request. Be sure its guaranteed not * to be re-used until the caller is guaranteed to know that * the transfer has ended or been cancelled */ return 0; } /* ohci_request_irq() */ /* * Control thread operations: */ static DECLARE_WAIT_QUEUE_HEAD(control_wakeup); /* * This is the handler that gets called when a control transaction * completes. * * This function is called from the interrupt handler. */ static int ohci_control_completed(int stats, void *buffer, int len, void *dev_id) { /* pass the TDs completion status back to control_msg */ if (dev_id) { int *completion_status = (int *)dev_id; *completion_status = stats; } wake_up(&control_wakeup); return 0; } /* ohci_control_completed() */ /* * Send or receive a control message on a "pipe" * * The cmd parameter is a pointer to the 8 byte setup command to be * sent. * * A control message contains: * - The command itself * - An optional data phase (if len > 0) * - Status complete phase * * This function can NOT be called from an interrupt. */ static int ohci_control_msg(struct usb_device *usb, unsigned int pipe, devrequest *cmd, void *data, int len) { struct ohci_device *dev = usb_to_ohci(usb); struct ohci_ed *control_ed = ohci_get_free_ed(dev); struct ohci_td *setup_td, *data_td, *status_td; DECLARE_WAITQUEUE(wait, current); int completion_status = -1; devrequest our_cmd; /* byte-swap fields of cmd if necessary */ our_cmd = *cmd; cpu_to_le16s(&our_cmd.value); cpu_to_le16s(&our_cmd.index); cpu_to_le16s(&our_cmd.length); #ifdef OHCI_DEBUG if (MegaDebug) printk(KERN_DEBUG "ohci_control_msg %p (ohci_dev: %p) pipe %x, cmd %p, data %p, len %d\n", usb, dev, pipe, cmd, data, len); #endif if (!control_ed) { printk(KERN_ERR "usb-ohci: couldn't get ED for dev %p\n", dev); return -1; } /* get a TD to send this control message with */ setup_td = ohci_get_free_td(dev); if (!setup_td) { printk(KERN_ERR "usb-ohci: couldn't get TD for dev %p [cntl setup]\n", dev); ohci_free_ed(control_ed); return -1; } /* * Set the max packet size, device speed, endpoint number, usb * device number (function address), and type of TD. * */ ohci_fill_ed(dev, control_ed, usb_maxpacket(usb,pipe), usb_pipeslow(pipe), usb_pipe_endpdev(pipe), 0 /* normal TDs */); /* * Build the control TD */ /* * Set the not accessed condition code, allow odd sized data, * and set the data transfer type to SETUP. Setup DATA always * uses a DATA0 packet. * * The setup packet contains a devrequest (usb.h) which * will always be 8 bytes long. */ ohci_fill_new_td(setup_td, OHCI_TD_D_SETUP, TOGGLE_DATA0, OHCI_TD_IOC_OFF, &our_cmd, 8, /* cmd is always 8 bytes long */ &completion_status, NULL); /* Allocate a TD for the control xfer status */ status_td = ohci_get_free_td(dev); if (!status_td) { printk("usb-ohci: couldn't get TD for dev %p [cntl status]\n", dev); ohci_free_td(setup_td); ohci_free_ed(control_ed); return -1; } /* The control status packet always uses a DATA1 * Give "dev_id" the address of completion_status so that the * TDs status can be passed back to us from the IRQ. */ ohci_fill_new_td(status_td, td_set_dir_in(usb_pipeout(pipe) | (len == 0)), TOGGLE_DATA1, 0 /* flags */, NULL /* data */, 0 /* data len */, &completion_status, ohci_control_completed); status_td->next_td = 0; /* end of TDs */ /* If there is data to transfer, create the chain of data TDs * followed by the status TD. */ if (len > 0) { data_td = ohci_build_td_chain( dev, data, len, usb_pipeout(pipe), TOGGLE_DATA1, 1 /* round */, 1 /* autofree */, &completion_status, NULL /* no handler here */, virt_to_bus(status_td) ); if (!data_td) { printk(KERN_ERR "usb-ohci: couldn't allocate control data TDs for dev %p\n", dev); ohci_free_td(setup_td); ohci_free_td(status_td); ohci_free_ed(control_ed); return -1; } /* link the to the data & status TDs */ setup_td->next_td = virt_to_bus(data_td); } else { /* no data TDs, link to the status TD */ setup_td->next_td = virt_to_bus(status_td); } /* * Add the control TDs to the control ED (setup_td is the first) */ setup_td = ohci_add_td_chain_to_ed(setup_td, control_ed); control_ed->status &= ~OHCI_ED_SKIP; ohci_unhalt_ed(control_ed); #ifdef OHCI_DEBUG if (MegaDebug) { /* complete transaction debugging output (before) */ printk(KERN_DEBUG " Control ED %lx:\n", virt_to_bus(control_ed)); show_ohci_ed(control_ed); printk(KERN_DEBUG " Control TD chain:\n"); show_ohci_td_chain(setup_td); printk(KERN_DEBUG " OHCI Controller Status:\n"); show_ohci_status(dev->ohci); } #endif /* * Start the control transaction.. */ current->state = TASK_UNINTERRUPTIBLE; add_wait_queue(&control_wakeup, &wait); /* Give the ED to the HC */ ohci_add_control_ed(dev->ohci, control_ed); schedule_timeout(HZ/10); remove_wait_queue(&control_wakeup, &wait); #ifdef OHCI_DEBUG if (MegaDebug) { /* complete transaction debugging output (after) */ printk(KERN_DEBUG " *after* Control ED %lx:\n", virt_to_bus(control_ed)); show_ohci_ed(control_ed); printk(KERN_DEBUG " *after* Control TD chain:\n"); show_ohci_td_chain(setup_td); printk(KERN_DEBUG " *after* OHCI Controller Status:\n"); show_ohci_status(dev->ohci); } #endif /* no TD cleanup, the TDs were auto-freed as they finished */ /* remove the control ED from the HC */ ohci_remove_control_ed(dev->ohci, control_ed); ohci_free_ed(control_ed); /* return it to the pool */ #ifdef OHCI_DEBUG if (completion_status != 0) { char *what = (completion_status < 0)? "timed out": cc_names[completion_status & 0xf]; printk(KERN_ERR "ohci_control_msg: %s on pipe %x cmd %x %x %x %x %x\n", what, pipe, cmd->requesttype, cmd->request, cmd->value, cmd->index, cmd->length); } else if (!usb_pipeout(pipe)) { unsigned char *q = data; int i; printk(KERN_DEBUG "ctrl msg %x %x %x %x %x on pipe %x returned:", cmd->requesttype, cmd->request, cmd->value, cmd->index, cmd->length, pipe); for (i = 0; i < len; ++i) { if (i % 16 == 0) printk("\n" KERN_DEBUG); printk(" %x", q[i]); } printk("\n"); } #endif return completion_status; } /* ohci_control_msg() */ /********************************************************************** * Bulk transfer processing **********************************************************************/ /* * Internal state for an ohci_bulk_request */ struct ohci_bulk_request_state { struct usb_device *usb_dev; unsigned int pipe; /* usb "pipe" */ void *data; /* ptr to data */ int length; /* length to transfer */ int _bytes_done; /* bytes transferred so far */ unsigned long *bytes_transferred_p; /* where to increment */ void *dev_id; /* pass to the completion handler */ usb_device_irq completion; /* completion handler */ }; /* * this handles the individual TDs of a (possibly) larger bulk * request. It keeps track of the total bytes transferred, calls the * final completion handler, etc. */ static int ohci_bulk_td_handler(int stats, void *buffer, int len, void *dev_id) { struct ohci_bulk_request_state *req; req = (struct ohci_bulk_request_state *) dev_id; #ifdef OHCI_DEBUG printk(KERN_DEBUG "ohci_bulk_td_handler stats %x, buffer %p, len %d, req %p\n", stats, buffer, len, req); #endif /* only count TDs that were completed successfully */ if (stats == USB_ST_NOERROR) req->_bytes_done += len; /* call the real completion handler when done or on an error */ if ((stats != USB_ST_NOERROR) || (req->_bytes_done >= req->length && req->completion != NULL)) { *req->bytes_transferred_p += req->_bytes_done; #ifdef OHCI_DEBUG printk(KERN_DEBUG "usb-ohci: bulk request %p ending\n", req); #endif req->completion(stats, buffer, req->_bytes_done, req->dev_id); } return 0; /* do not re-queue the TD */ } /* ohci_bulk_td_handler() */ /* * Request to send or receive bulk data. The completion() function * will be called when the transfer has completed or been aborted due * to an error. * * bytes_transferred_p is a pointer to an integer that will be * -incremented- by the number of bytes that have been successfully * transferred. The interrupt handler will update it after each * internal TD completes successfully. * * This function can NOT be called from an interrupt (?) * (TODO: verify & fix this if needed). * * Returns: a pointer to the ED being used for this request. At the * moment, removing & freeing it is the responsibilty of the caller. */ static struct ohci_ed* ohci_request_bulk(struct ohci_bulk_request_state *bulk_request) { /* local names for the readonly fields */ struct usb_device *usb_dev = bulk_request->usb_dev; unsigned int pipe = bulk_request->pipe; void *data = bulk_request->data; int len = bulk_request->length; struct ohci_device *dev = usb_to_ohci(usb_dev); struct ohci_ed *bulk_ed; struct ohci_td *head_td; unsigned long flags; #ifdef OHCI_DEBUG printk(KERN_DEBUG "ohci_request_bulk(%p) ohci_dev %p, completion %p, pipe %x, data %p, len %d\n", bulk_request, dev, bulk_request->completion, pipe, data, len); #endif bulk_ed = ohci_get_free_ed(dev); if (!bulk_ed) { printk("usb-ohci: couldn't get ED for dev %p\n", dev); return NULL; } /* allocate & fill in the TDs for this request */ head_td = ohci_build_td_chain(dev, data, len, usb_pipeout(pipe), TOGGLE_AUTO, 0 /* round not required */, 1 /* autofree */, bulk_request, /* dev_id: the bulk_request */ ohci_bulk_td_handler, 0 /* no additional TDs */); if (!head_td) { printk("usb-ohci: couldn't get TDs for dev %p\n", dev); ohci_free_ed(bulk_ed); return NULL; } /* Set the max packet size, device speed, endpoint number, usb * device number (function address), and type of TD. */ ohci_fill_ed(dev, bulk_ed, usb_maxpacket(usb_dev, pipe), usb_pipeslow(pipe), usb_pipe_endpdev(pipe), 0 /* bulk uses normal TDs */); /* initialize the internal counter */ bulk_request->_bytes_done = 0; /* * Add the TDs to the ED */ spin_lock_irqsave(&ohci_edtd_lock, flags); bulk_ed->status |= OHCI_ED_SKIP; head_td = ohci_add_td_chain_to_ed(head_td, bulk_ed); bulk_ed->status &= ~OHCI_ED_SKIP; ohci_unhalt_ed(bulk_ed); spin_unlock_irqrestore(&ohci_edtd_lock, flags); #ifdef OHCI_DEBUG if (MegaDebug) { /* complete request debugging output (before) */ printk(KERN_DEBUG " Bulk ED %lx:\n", virt_to_bus(bulk_ed)); show_ohci_ed(bulk_ed); printk(KERN_DEBUG " Bulk TDs %lx:\n", virt_to_bus(head_td)); show_ohci_td_chain(head_td); } #endif /* Give the ED to the HC */ ohci_add_bulk_ed(dev->ohci, bulk_ed); return bulk_ed; } /* ohci_request_bulk() */ static DECLARE_WAIT_QUEUE_HEAD(bulk_wakeup); static int ohci_bulk_msg_completed(int stats, void *buffer, int len, void *dev_id) { #ifdef OHCI_DEBUG printk("ohci_bulk_msg_completed %x, %p, %d, %p\n", stats, buffer, len, dev_id); #endif if (dev_id != NULL) { int *completion_status = (int *)dev_id; *completion_status = stats; } wake_up(&bulk_wakeup); return 0; /* don't requeue the TD */ } /* ohci_bulk_msg_completed() */ static int ohci_bulk_msg(struct usb_device *usb_dev, unsigned int pipe, void *data, int len, unsigned long *bytes_transferred_p) { DECLARE_WAITQUEUE(wait, current); int completion_status = USB_ST_INTERNALERROR; struct ohci_bulk_request_state req; struct ohci_ed *req_ed; #ifdef OHCI_DEBUG printk(KERN_DEBUG "ohci_bulk_msg %p pipe %x, data %p, len %d, bytes_transferred %p\n", usb_dev, pipe, data, len, bytes_transferred_p); #endif /* initialize bytes transferred to nothing */ *bytes_transferred_p = 0; /* Hopefully this is similar to the "URP" (USB Request Packet) code * that michael gee is working on... */ req.usb_dev = usb_dev; req.pipe = pipe; req.data = data; req.length = len; req.bytes_transferred_p = bytes_transferred_p; req.dev_id = &completion_status; req.completion = ohci_bulk_msg_completed; /* * Start the transaction.. */ current->state = TASK_UNINTERRUPTIBLE; add_wait_queue(&bulk_wakeup, &wait); req_ed = ohci_request_bulk(&req); /* FIXME this should to wait for a caller specified time... */ schedule_timeout(HZ*5); /* it'll only stay in this state of the request never finished */ if (completion_status == USB_ST_INTERNALERROR) { struct ohci_device *dev = usb_to_ohci(usb_dev); struct ohci_regs *regs = dev->ohci->regs; #ifdef OHCI_DEBUG printk(KERN_DEBUG "ohci_bulk_msg timing out\n"); #endif /* XXX This code should go into a function used to stop * a previously requested bulk transfer. -greg */ /* stop the transfer & collect the number of bytes */ ohci_wait_for_ed_safe(regs, req_ed, HCD_ED_BULK); /* Get the number of bytes transferred out of the head TD * on the ED if it didn't finish while we were waiting. */ if ( ed_head_td(req_ed) && (ed_head_td(req_ed) != ed_tail_td(req_ed)) ) { struct ohci_td *partial_td; partial_td = bus_to_virt(ed_head_td(req_ed)); #ifdef OHCI_DEBUG if (MegaDebug) { show_ohci_td(partial_td); } #endif /* Record the bytes as transferred */ *bytes_transferred_p += ohci_td_bytes_done(partial_td); /* If there was an unreported error, return it. * Otherwise return a timeout */ completion_status = OHCI_TD_CC_GET(partial_td->info); if (completion_status == USB_ST_NOERROR) { completion_status = USB_ST_TIMEOUT; } } } remove_wait_queue(&bulk_wakeup, &wait); /* remove the ED from the HC */ ohci_remove_bulk_ed(usb_to_ohci(usb_dev)->ohci, req_ed); ohci_free_ed(req_ed); /* return it to the pool */ #ifdef OHCI_DEBUG printk(KERN_DEBUG "ohci_bulk_msg done, status %x (bytes_transferred = %ld).\n", completion_status, *bytes_transferred_p); #endif return completion_status; } /* ohci_bulk_msg() */ /* .......... */ /* * Allocate a new USB device to be attached to an OHCI controller */ static struct usb_device *ohci_usb_allocate(struct usb_device *parent) { struct usb_device *usb_dev; struct ohci_device *dev; int idx; /* * Allocate the generic USB device */ usb_dev = kmalloc(sizeof(*usb_dev), GFP_KERNEL); if (!usb_dev) return NULL; memset(usb_dev, 0, sizeof(*usb_dev)); /* * Allocate an OHCI device (EDs and TDs for this device) */ dev = kmalloc(sizeof(*dev), GFP_KERNEL); if (!dev) { kfree(usb_dev); return NULL; } memset(dev, 0, sizeof(*dev)); /* Initialize all EDs in a new device with the skip flag so that * they are ignored by the controller until set otherwise. */ for (idx = 0; idx < NUM_EDS; ++idx) { dev->ed[idx].status = cpu_to_le32(OHCI_ED_SKIP); } /* * Link them together */ usb_dev->hcpriv = dev; dev->usb = usb_dev; /* * Link the device to its parent (hub, etc..) if any. */ usb_dev->parent = parent; if (parent) { usb_dev->bus = parent->bus; dev->ohci = usb_to_ohci(parent)->ohci; } return usb_dev; } /* ohci_usb_allocate() */ /* * Free a usb device. * * TODO This function needs to take better care of the EDs and TDs, etc. */ static int ohci_usb_deallocate(struct usb_device *usb_dev) { struct ohci_device *dev = usb_to_ohci(usb_dev); ohci_remove_device(dev->ohci, usb_dev->devnum); /* kfree(usb_to_ohci(usb_dev)); */ /* kfree(usb_dev); */ return 0; } /* * functions for the generic USB driver */ struct usb_operations ohci_device_operations = { ohci_usb_allocate, ohci_usb_deallocate, ohci_control_msg, ohci_bulk_msg, ohci_request_irq, }; /* * Reset an OHCI controller. Returns >= 0 on success. * * Afterwards the HC will be in the "suspend" state which prevents you * from writing to some registers. Bring it to the operational state * ASAP. */ static int reset_hc(struct ohci *ohci) { int timeout = 10000; /* prevent an infinite loop */ #if 0 printk(KERN_INFO "usb-ohci: resetting HC %p\n", ohci); #endif writel(~0x0, &ohci->regs->intrdisable); /* Disable HC interrupts */ writel(1, &ohci->regs->cmdstatus); /* HC Reset */ writel_mask(0x3f, &ohci->regs->control); /* move to UsbReset state */ while ((readl(&ohci->regs->cmdstatus) & OHCI_CMDSTAT_HCR) != 0) { if (!--timeout) { printk(KERN_ERR "usb-ohci: USB HC reset timed out!\n"); return -1; } udelay(1); } printk(KERN_DEBUG "usb-ohci: HC %p reset.\n", ohci); return 0; } /* reset_hc() */ /* * Reset and start an OHCI controller. Returns >= 0 on success. */ static int start_hc(struct ohci *ohci) { int ret = 0; int fminterval; __u32 what_to_enable; struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub); fminterval = readl(&ohci->regs->fminterval) & 0x3fff; #if 0 printk(KERN_DEBUG "entering start_hc %p\n", ohci); #endif if (reset_hc(ohci) < 0) return -1; /* restore registers cleared by the reset */ writel(virt_to_bus(root_hub->hcca), &ohci->regs->hcca); /* * XXX Should fminterval also be set here? * The spec suggests 0x2edf [11,999]. (FIXME: make this a constant) */ /* fminterval |= (0x2edf << 16); */ fminterval = (10240 << 16) | 11999; writel(fminterval, &ohci->regs->fminterval); /* Start periodic transfers at 90% of fminterval (fmremaining * counts down; this will put them in the first 10% of the * frame). */ writel((0x2edf*9)/10, &ohci->regs->periodicstart); /* * FNO (frame number overflow) could be enabled... they * occur every 32768 frames (every 32-33 seconds). This is * useful for debugging and as a bus heartbeat. -greg */ /* Choose the interrupts we care about */ what_to_enable = OHCI_INTR_MIE | #ifdef OHCI_RHSC_INT OHCI_INTR_RHSC | #endif /* | OHCI_INTR_FNO */ OHCI_INTR_WDH; writel( what_to_enable, &ohci->regs->intrenable); /* Enter the USB Operational state & start the frames a flowing.. */ writel_set(OHCI_USB_OPER, &ohci->regs->control); /* Enable control lists */ writel_set(OHCI_USB_IE | OHCI_USB_CLE | OHCI_USB_BLE, &ohci->regs->control); /* Force global power enable -gal@cs.uni-magdeburg.de */ /* * This turns on global power switching for all the ports * and tells the HC that all of the ports should be powered on * all of the time. * * TODO: This could be battery draining for laptops.. We * should implement power switching. */ writel_set( OHCI_ROOT_A_NPS, &ohci->regs->roothub.a ); writel_mask( ~((__u32)OHCI_ROOT_A_PSM), &ohci->regs->roothub.a ); /* Turn on power to the root hub ports (thanks Roman!) */ writel( OHCI_ROOT_LPSC, &ohci->regs->roothub.status ); printk(KERN_INFO "usb-ohci: host controller operational\n"); return ret; } /* start_hc() */ /* * Reset a root hub port */ static void ohci_reset_port(struct ohci *ohci, unsigned int port) { int status; /* Don't allow overflows. */ if (port >= MAX_ROOT_PORTS) { printk(KERN_ERR "usb-ohci: bad port #%d in ohci_reset_port\n", port); port = MAX_ROOT_PORTS-1; } writel(PORT_PRS, &ohci->regs->roothub.portstatus[port]); /* Reset */ /* * Wait for the reset to complete. */ wait_ms(20); /* check port status to see that the reset completed */ status = readl(&ohci->regs->roothub.portstatus[port]); if (status & PORT_PRS) { /* reset failed, try harder? */ printk(KERN_ERR "usb-ohci: port %d reset failed, retrying\n", port); writel(PORT_PRS, &ohci->regs->roothub.portstatus[port]); wait_ms(50); } /* TODO we might need to re-enable the port here or is that * done elsewhere? */ } /* ohci_reset_port */ /* * This gets called if the connect status on the root hub changes. */ static void ohci_connect_change(struct ohci * ohci, int port) { struct usb_device *usb_dev; struct ohci_device *dev; struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub); /* memory I/O address of the port status register */ __u32 *portaddr = &ohci->regs->roothub.portstatus[port]; int portstatus; #ifdef OHCI_DEBUG printk(KERN_DEBUG "ohci_connect_change on port %d\n", port); #endif /* * Because of the status change we have to forget * everything we think we know about the device * on this root hub port. It may have changed. */ usb_disconnect(root_hub->usb->children + port); portstatus = readl(portaddr); /* disable the port if nothing is connected */ if (!(portstatus & PORT_CCS)) { writel(PORT_CCS, portaddr); /* We need to reset the CSC bit -after- disabling the * port because it causes the CSC bit to come on * again... */ wait_ms(20); writel(PORT_CSC, portaddr); #ifdef OHCI_DEBUG printk(KERN_DEBUG "ohci port %d disabled, nothing connected.\n", port); #endif return; } /* * Allocate a device for the new thingy that's been attached */ usb_dev = ohci_usb_allocate(root_hub->usb); dev = usb_dev->hcpriv; dev->ohci = ohci; usb_connect(dev->usb); /* link it into the bus's device tree */ root_hub->usb->children[port] = usb_dev; wait_ms(200); /* wait for powerup; XXX is this needed? */ ohci_reset_port(ohci, port); /* Get information on speed by using LSD */ usb_dev->slow = readl(portaddr) & PORT_LSDA ? 1 : 0; /* * Do generic USB device tree processing on the new device. */ usb_new_device(usb_dev); } /* ohci_connect_change() */ /* * This gets called when the root hub configuration * has changed. Just go through each port, seeing if * there is something interesting happening. */ static void ohci_check_configuration(struct ohci *ohci) { struct ohci_regs *regs = ohci->regs; int num = 0; int maxport = readl(&ohci->regs->roothub) & 0xff; __u32 rh_change_flags = PORT_CSC | PORT_PESC; /* root hub status changes */ #ifdef OHCI_DEBUG printk(KERN_DEBUG "entering ohci_check_configuration %p\n", ohci); #endif do { __u32 *portstatus_p = ®s->roothub.portstatus[num]; if (readl(portstatus_p) & rh_change_flags) { /* acknowledge the root hub status changes */ writel_set(rh_change_flags, portstatus_p); /* disable the port if nothing is on it */ /* check the port for a nifty device */ ohci_connect_change(ohci, num); } } while (++num < maxport); #if 0 printk(KERN_DEBUG "leaving ohci_check_configuration %p\n", ohci); #endif } /* ohci_check_configuration() */ /* * Check root hub port status and wake the control thread up if * anything has changed. * * This function is called from the interrupt handler. */ static void ohci_root_hub_events(struct ohci *ohci) { int num = 0; struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub); int maxport = root_hub->usb->maxchild; if (!waitqueue_active(&ohci_configure)) return; do { __u32 *portstatus_p = &ohci->regs->roothub.portstatus[num]; if (readl(portstatus_p) & PORT_CSC) { if (waitqueue_active(&ohci_configure)) wake_up(&ohci_configure); return; } } while (++num < maxport); } /* ohci_root_hub_events() */ /* * The done list is in reverse order; we need to process TDs in the * order they were finished (FIFO). This function builds the FIFO * list using the next_dl_td pointer. * * This function originally by Roman Weissgaerber (weissg@vienna.at) * * This function is called from the interrupt handler. */ static struct ohci_td * ohci_reverse_donelist(struct ohci * ohci) { __u32 td_list_hc; struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub); struct ohci_hcca *hcca = root_hub->hcca; struct ohci_td *td_list = NULL; struct ohci_td *td_rev = NULL; td_list_hc = le32_to_cpup(&hcca->donehead) & 0xfffffff0; hcca->donehead = 0; while(td_list_hc) { td_list = (struct ohci_td *) bus_to_virt(td_list_hc); td_list->next_dl_td = td_rev; td_rev = td_list; td_list_hc = le32_to_cpup(&td_list->next_td) & 0xfffffff0; } return td_list; } /* ohci_reverse_donelist() */ /* * Collect this interrupt's goodies off of the list of finished TDs * that the OHCI controller is kind enough to setup for us. * * This function is called from the interrupt handler. */ static void ohci_reap_donelist(struct ohci *ohci) { struct ohci_td *td; /* used for walking the list */ /* um... isn't this dangerous to do in an interrupt handler? -greg */ // spin_lock(&ohci_edtd_lock); /* create the FIFO ordered donelist */ td = ohci_reverse_donelist(ohci); while (td != NULL) { struct ohci_td *next_td = td->next_dl_td; int cc = OHCI_TD_CC_GET(le32_to_cpup(&td->info)); if (td_dummy(*td)) printk(KERN_ERR "yikes! reaping a dummy TD\n"); if (cc != 0 && ohci_ed_halted(td->ed) && !td_endofchain(*td)) { /* * There was an error on this TD and the ED * is halted, and this was not the last TD * of the transaction, so there will be TDs * to clean off the ED. */ struct ohci_ed *ed = td->ed; struct ohci_td *tail_td = bus_to_virt(ed_tail_td(ed)); struct ohci_td *ntd; ohci_free_td(td); td = ntd = bus_to_virt(ed_head_td(ed)); while (td != tail_td) { ntd = bus_to_virt(le32_to_cpup(&td->next_td)); /* only deal with TDs from this ED, * the host controller could have * processed other endpoints at the * same time as this one.. */ if (td->ed == ed) { if (td_endofchain(*td)) break; /* FIXME: unlink this TD from the * reverse donelist! */ ohci_free_td(td); } td = ntd; } /* Set the ED head past the ones we cleaned off, and clear the halted flag */ set_ed_head_td(ed, virt_to_bus(ntd)); ohci_unhalt_ed(ed); /* If we didn't find an endofchain TD, give up */ if (td == tail_td) { td = next_td; continue; } } /* Check if TD should be re-queued */ if ((td->completed != NULL) && (td->completed(cc, td->data, ohci_td_bytes_done(td), td->dev_id))) { /* Mark the TD as active again: * Set the not accessed condition code * Reset the Error count */ td->info |= cpu_to_le32(OHCI_TD_CC_NEW); clear_td_errorcount(td); /* reset the toggle field to TOGGLE_AUTO (0) */ td->info &= cpu_to_le32(~OHCI_TD_DT); /* point it back to the start of the data buffer */ td->cur_buf = cpu_to_le32(virt_to_bus(td->data)); /* insert it back on its ED */ ohci_add_td_to_ed(td, td, td->ed); ohci_unhalt_ed(td->ed); } else { /* return it to the pool of free TDs */ if (can_auto_free(*td)) ohci_free_td(td); } td = next_td; } // spin_unlock(&ohci_edtd_lock); } /* ohci_reap_donelist() */ /* * Get annoyed at the controller for bothering us. * This pretty much follows the OHCI v1.0a spec, section 5.3. */ static void ohci_interrupt(int irq, void *__ohci, struct pt_regs *r) { struct ohci *ohci = __ohci; struct ohci_regs *regs = ohci->regs; struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub); struct ohci_hcca *hcca = root_hub->hcca; __u32 status, context; /* Save the status of the interrupts that are enabled */ status = readl(®s->intrstatus); status &= readl(®s->intrenable); /* make context = the interrupt status bits that we care about */ if (hcca->donehead != 0) { context = OHCI_INTR_WDH; /* hcca donehead needs processing */ if (hcca->donehead & cpu_to_le32(1)) { context |= status; /* other status change to check */ } } else { context = status; if (!context) { /* TODO increment a useless interrupt counter here */ return; } } /* Disable HC interrupts */ /* why? - paulus */ writel(OHCI_INTR_MIE, ®s->intrdisable); #if 0 /* Only do this for SERIOUS debugging, be sure kern.debug logs * are not going to the console as this can cause your * machine to lock up if so... -greg */ show_ohci_status(ohci); #endif /* Process the done list */ if (context & OHCI_INTR_WDH) { /* See which TD's completed.. */ ohci_reap_donelist(ohci); /* reset the done queue and tell the controller */ hcca->donehead = 0; /* XXX already done in ohci_reverse_donelist */ writel(OHCI_INTR_WDH, ®s->intrstatus); context &= ~OHCI_INTR_WDH; /* mark this as checked */ } #ifdef OHCI_RHSC_INT /* NOTE: this is very funky on some USB controllers (ie: it * doesn't work right). Using the ohci_timer instead to poll * the root hub is a much better choice. */ /* Process any root hub status changes */ if (context & OHCI_INTR_RHSC) { /* Wake the thread to process root hub events */ if (waitqueue_active(&ohci_configure)) wake_up(&ohci_configure); writel(OHCI_INTR_RHSC, ®s->intrstatus); /* * Don't unset RHSC in context; it should be disabled. * The control thread will re-enable it after it has * checked the root hub status. */ } #endif /* Start of Frame interrupts, used during safe ED removal */ if (context & (OHCI_INTR_SF)) { writel(OHCI_INTR_SF, ®s->intrstatus); if (waitqueue_active(&start_of_frame_wakeup)) wake_up(&start_of_frame_wakeup); /* Do NOT mark the frame start interrupt as checked * as we don't want to receive any more of them until * asked. */ } /* Check those "other" pesky bits */ if (context & (OHCI_INTR_FNO)) { writel(OHCI_INTR_FNO, ®s->intrstatus); context &= ~OHCI_INTR_FNO; /* mark this as checked */ } if (context & OHCI_INTR_SO) { writel(OHCI_INTR_SO, ®s->intrstatus); context &= ~OHCI_INTR_SO; /* mark this as checked */ } if (context & OHCI_INTR_RD) { writel(OHCI_INTR_RD, ®s->intrstatus); context &= ~OHCI_INTR_RD; /* mark this as checked */ } if (context & OHCI_INTR_UE) { /* FIXME: need to have the control thread reset the * controller now and keep a count of unrecoverable * errors. If there are too many, it should just shut * the broken controller down entirely. */ writel(OHCI_INTR_UE, ®s->intrstatus); context &= ~OHCI_INTR_UE; /* mark this as checked */ } if (context & OHCI_INTR_OC) { writel(OHCI_INTR_OC, ®s->intrstatus); context &= ~OHCI_INTR_OC; /* mark this as checked */ } /* Mask out any remaining unprocessed or unmasked interrupts * so that we don't get any more of them. */ if (context & ~OHCI_INTR_MIE) { writel(context, ®s->intrdisable); } /* Re-enable HC interrupts */ writel(OHCI_INTR_MIE, ®s->intrenable); } /* ohci_interrupt() */ /* * Allocate the resources required for running an OHCI controller. * Host controller interrupts must not be running while calling this * function or the penguins will get angry. * * The mem_base parameter must be the usable -virtual- address of the * host controller's memory mapped I/O registers. */ static struct ohci *alloc_ohci(void* mem_base) { int i; struct ohci *ohci; struct usb_bus *bus; struct ohci_device *dev; struct usb_device *usb; #if 0 printk(KERN_DEBUG "entering alloc_ohci %p\n", mem_base); #endif ohci = kmalloc(sizeof(*ohci), GFP_KERNEL); if (!ohci) return NULL; memset(ohci, 0, sizeof(*ohci)); ohci->irq = -1; ohci->regs = mem_base; INIT_LIST_HEAD(&ohci->interrupt_list); bus = kmalloc(sizeof(*bus), GFP_KERNEL); if (!bus) return NULL; memset(bus, 0, sizeof(*bus)); ohci->bus = bus; bus->hcpriv = ohci; bus->op = &ohci_device_operations; /* * Allocate the USB device structure and root hub. * * Here we allocate our own root hub and TDs as well as the * OHCI host controller communications area. The HCCA is just * a nice pool of memory with pointers to endpoint descriptors * for the different interrupts. */ usb = ohci_usb_allocate(NULL); if (!usb) return NULL; dev = usb_to_ohci(usb); ohci->bus->root_hub= ohci_to_usb(dev); usb->bus = bus; /* Initialize the root hub */ dev->ohci = ohci; /* link back to the controller */ /* * Allocate the Host Controller Communications Area on a 256 * byte boundary. XXX take the easy way out and just grab a * page as that's guaranteed to have a nice boundary. */ dev->hcca = (struct ohci_hcca *) __get_free_page(GFP_KERNEL); memset(dev->hcca, 0, sizeof(struct ohci_hcca)); /* Tell the controller where the HCCA is */ writel(virt_to_bus(dev->hcca), &ohci->regs->hcca); #if 0 printk(KERN_DEBUG "usb-ohci: HCCA allocated at %p (bus %p)\n", dev->hcca, (void*)virt_to_bus(dev->hcca)); #endif /* Get the number of ports on the root hub */ usb->maxchild = readl(&ohci->regs->roothub.a) & 0xff; if (usb->maxchild > MAX_ROOT_PORTS) { printk(KERN_INFO "usb-ohci: Limited to %d ports\n", MAX_ROOT_PORTS); usb->maxchild = MAX_ROOT_PORTS; } if (usb->maxchild < 1) { printk(KERN_ERR "usb-ohci: Less than one root hub port? Impossible!\n"); usb->maxchild = 1; } printk(KERN_DEBUG "usb-ohci: %d root hub ports found\n", usb->maxchild); /* * Initialize the ED polling "tree" (for simplicity's sake in * this driver many nodes in the tree will be identical) */ dev->ed[ED_INT_32].next_ed = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_16])); dev->ed[ED_INT_16].next_ed = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_8])); dev->ed[ED_INT_8].next_ed = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_4])); dev->ed[ED_INT_4].next_ed = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_2])); dev->ed[ED_INT_2].next_ed = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_1])); /* * Initialize the polling table to call interrupts at the * intended intervals. Note that these EDs are just * placeholders. They have their SKIP bit set and are used as * list heads to insert real EDs onto. */ dev->hcca->int_table[0] = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_1])); for (i = 1; i < NUM_INTS; i++) { if (i & 16) dev->hcca->int_table[i] = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_32])); if (i & 8) dev->hcca->int_table[i] = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_16])); if (i & 4) dev->hcca->int_table[i] = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_8])); if (i & 2) dev->hcca->int_table[i] = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_4])); if (i & 1) dev->hcca->int_table[i] = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_2])); } /* * Tell the controller where the control and bulk lists are. * The lists start out empty. */ writel(0, &ohci->regs->ed_controlhead); writel(0, &ohci->regs->ed_bulkhead); #ifdef OHCI_DEBUG printk(KERN_DEBUG "alloc_ohci(): controller\n"); show_ohci_status(ohci); #endif #if 0 printk(KERN_DEBUG "leaving alloc_ohci %p\n", ohci); #endif return ohci; } /* alloc_ohci() */ /* * De-allocate all resoueces.. */ static void release_ohci(struct ohci *ohci) { printk(KERN_INFO "Releasing OHCI controller 0x%p\n", ohci); #ifdef OHCI_TIMER /* stop our timer */ del_timer(&ohci_timer); #endif if (ohci->irq >= 0) { free_irq(ohci->irq, ohci); ohci->irq = -1; } /* stop all OHCI interrupts */ writel(~0x0, &ohci->regs->intrdisable); if (ohci->bus->root_hub) { struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub); /* ensure that HC is stopped before releasing the HCCA */ writel(OHCI_USB_SUSPEND, &ohci->regs->control); free_page((unsigned long) root_hub->hcca); kfree(ohci->bus->root_hub); root_hub->hcca = NULL; ohci->bus->root_hub = NULL; } /* unmap the IO address space */ iounmap(ohci->regs); kfree(ohci); MOD_DEC_USE_COUNT; /* If the ohci itself were dynamic we'd free it here */ printk(KERN_DEBUG "usb-ohci: HC resources released.\n"); } /* release_ohci() */ /* * USB OHCI control thread */ static int ohci_control_thread(void * __ohci) { struct ohci *ohci = (struct ohci *)__ohci; /* * I'm unfamiliar with the SMP kernel locking.. where should * this be released and what does it do? -greg */ lock_kernel(); /* * This thread doesn't need any user-level access, * so get rid of all of our resources.. */ printk(KERN_DEBUG "ohci-control thread code for 0x%p code at 0x%p\n", __ohci, &ohci_control_thread); exit_mm(current); exit_files(current); exit_fs(current); strcpy(current->comm, "ohci-control"); usb_register_bus(ohci->bus); /* * Damn the torpedoes, full speed ahead */ if (start_hc(ohci) < 0) { printk(KERN_ERR "usb-ohci: failed to start the controller\n"); release_ohci(ohci); usb_deregister_bus(ohci->bus); printk(KERN_INFO "leaving ohci_control_thread %p\n", __ohci); return 0; } for(;;) { siginfo_t info; int unsigned long signr; wait_ms(200); /* check the root hub configuration for changes. */ ohci_check_configuration(ohci); /* re-enable root hub status change interrupts. */ #ifdef OHCI_RHSC_INT writel(OHCI_INTR_RHSC, &ohci->regs->intrenable); #endif printk(KERN_DEBUG "ohci-control thread sleeping\n"); interruptible_sleep_on(&ohci_configure); #ifdef CONFIG_APM if (apm_resume) { apm_resume = 0; if (start_hc(ohci) < 0) break; continue; } #endif /* * If we were woken up by a signal, see if its useful, * otherwise exit. */ if (signal_pending(current)) { /* sending SIGUSR1 makes us print out some info */ spin_lock_irq(¤t->sigmask_lock); signr = dequeue_signal(¤t->blocked, &info); spin_unlock_irq(¤t->sigmask_lock); if(signr == SIGUSR1) { /* TODO: have it do a full ed/td queue dump? */ printk(KERN_DEBUG "OHCI status dump:\n"); show_ohci_status(ohci); } else if (signr == SIGUSR2) { /* toggle mega TD/ED debugging output */ #ifdef OHCI_DEBUG MegaDebug = !MegaDebug; printk(KERN_DEBUG "usb-ohci: Mega debugging %sabled.\n", MegaDebug ? "en" : "dis"); #endif } else { /* unknown signal, exit the thread */ break; } } } /* for (;;) */ reset_hc(ohci); release_ohci(ohci); usb_deregister_bus(ohci->bus); printk(KERN_DEBUG "ohci-control thread for 0x%p exiting\n", __ohci); return 0; } /* ohci_control_thread() */ #ifdef CONFIG_APM static int handle_apm_event(apm_event_t event) { static int down = 0; switch (event) { case APM_SYS_SUSPEND: case APM_USER_SUSPEND: if (down) { printk(KERN_DEBUG "usb-ohci: received extra suspend event\n"); break; } down = 1; break; case APM_NORMAL_RESUME: case APM_CRITICAL_RESUME: if (!down) { printk(KERN_DEBUG "usb-ohci: received bogus resume event\n"); break; } down = 0; if (waitqueue_active(&ohci_configure)) { apm_resume = 1; wake_up(&ohci_configure); } break; } return 0; } /* handle_apm_event() */ #endif #ifdef OHCI_TIMER /* * Inspired by Iñaky's driver. This function is a timer routine that * is called every OHCI_TIMER_FREQ ms. It polls the root hub for * status changes as on my system the RHSC interrupt just doesn't * play well with others.. (so RHSC is turned off by default in this * driver) * [my controller is a "SiS 7001 USB (rev 16)"] * -greg */ static void ohci_timer_func (unsigned long ohci_ptr) { struct ohci *ohci = (struct ohci*)ohci_ptr; ohci_root_hub_events(ohci); /* set the next timer */ mod_timer(&ohci_timer, jiffies + ((OHCI_TIMER_FREQ*HZ)/1000)); } /* ohci_timer_func() */ #endif /* * Increment the module usage count, start the control thread and * return success if the controller is good. */ static int found_ohci(int irq, void* mem_base) { int retval; struct ohci *ohci; #if 0 printk(KERN_DEBUG "entering found_ohci %d %p\n", irq, mem_base); #endif /* Allocate the running OHCI structures */ ohci = alloc_ohci(mem_base); if (!ohci) { return -ENOMEM; } #ifdef OHCI_TIMER init_timer(&ohci_timer); ohci_timer.expires = jiffies + ((OHCI_TIMER_FREQ*HZ)/1000); ohci_timer.data = (unsigned long)ohci; ohci_timer.function = ohci_timer_func; add_timer(&ohci_timer); #endif retval = -EBUSY; if (request_irq(irq, ohci_interrupt, SA_SHIRQ, "usb-ohci", ohci) == 0) { int pid; ohci->irq = irq; #ifdef OHCI_DEBUG printk(KERN_DEBUG "usb-ohci: forking ohci-control thread for 0x%p\n", ohci); #endif /* fork off the handler */ pid = kernel_thread(ohci_control_thread, ohci, CLONE_FS | CLONE_FILES | CLONE_SIGHAND); if (pid >= 0) { return 0; } retval = pid; } else { printk(KERN_ERR "usb-ohci: Couldn't allocate interrupt %d\n", irq); } release_ohci(ohci); #ifdef OHCI_DEBUG printk(KERN_DEBUG "leaving found_ohci %d %p\n", irq, mem_base); #endif return retval; } /* found_ohci() */ /* * If this controller is for real, map the IO memory and proceed */ static int init_ohci(struct pci_dev *dev) { unsigned long mem_base = dev->base_address[0]; /* If its OHCI, its memory */ if (mem_base & PCI_BASE_ADDRESS_SPACE_IO) return -ENODEV; /* Get the memory address and map it for IO */ mem_base &= PCI_BASE_ADDRESS_MEM_MASK; /* no interrupt won't work... */ if (dev->irq == 0) { printk(KERN_ERR "usb-ohci: no irq assigned? check your BIOS settings.\n"); return -ENODEV; } /* * FIXME ioremap_nocache isn't implemented on all CPUs (such * as the Alpha) [?] What should I use instead... * * The iounmap() is done on in release_ohci. */ mem_base = (unsigned long) ioremap_nocache(mem_base, 4096); if (!mem_base) { printk(KERN_ERR "Error mapping OHCI memory\n"); return -EFAULT; } MOD_INC_USE_COUNT; #ifdef OHCI_DEBUG printk(KERN_INFO "usb-ohci: Warning! Gobs of debugging output has been enabled.\n"); printk(KERN_INFO " Check your kern.debug logs for the bulk of it.\n"); #endif if (found_ohci(dev->irq, (void *) mem_base) < 0) { MOD_DEC_USE_COUNT; return -1; } return 0; } /* init_ohci() */ /* TODO this should be named following Linux convention and go in pci.h */ #define PCI_CLASS_SERIAL_USB_OHCI ((PCI_CLASS_SERIAL_USB << 8) | 0x0010) /* * Search the PCI bus for an OHCI USB controller and set it up * * If anyone wants multiple controllers this will need to be * updated.. Right now, it just picks the first one it finds. */ int ohci_init(void) { int retval; struct pci_dev *dev = NULL; /*u8 type;*/ if (sizeof(struct ohci_device) > 4096) { printk(KERN_ERR "usb-ohci: struct ohci_device to large\n"); return -ENODEV; } printk(KERN_INFO "OHCI USB Driver loading\n"); retval = -ENODEV; for (;;) { /* Find an OHCI USB controller */ dev = pci_find_class(PCI_CLASS_SERIAL_USB_OHCI, dev); if (!dev) break; /* Verify that its OpenHCI by checking for MMIO */ /* pci_read_config_byte(dev, PCI_CLASS_PROG, &type); if (!type) continue; */ /* Ok, set it up */ retval = init_ohci(dev); if (retval < 0) continue; #ifdef CONFIG_APM apm_register_callback(&handle_apm_event); #endif return 0; /* no error */ } return retval; } /* ohci_init */ #ifdef MODULE /* * Clean up when unloading the module */ void cleanup_module(void){ # ifdef CONFIG_APM apm_unregister_callback(&handle_apm_event); # endif printk(KERN_ERR "usb-ohci: module unloaded\n"); } int init_module(void){ return ohci_init(); } #endif //MODULE /* vim:sw=8 */ |