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 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 | /*****************************************************************************/ /* * audio.c -- USB Audio Class driver * * Copyright (C) 1999, 2000, 2001, 2003, 2004 * Alan Cox (alan@lxorguk.ukuu.org.uk) * Thomas Sailer (sailer@ife.ee.ethz.ch) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Debugging: * Use the 'lsusb' utility to dump the descriptors. * * 1999-09-07: Alan Cox * Parsing Audio descriptor patch * 1999-09-08: Thomas Sailer * Added OSS compatible data io functions; both parts of the * driver remain to be glued together * 1999-09-10: Thomas Sailer * Beautified the driver. Added sample format conversions. * Still not properly glued with the parsing code. * The parsing code seems to have its problems btw, * Since it parses all available configs but doesn't * store which iface/altsetting belongs to which config. * 1999-09-20: Thomas Sailer * Threw out Alan's parsing code and implemented my own one. * You cannot reasonnably linearly parse audio descriptors, * especially the AudioClass descriptors have to be considered * pointer lists. Mixer parsing untested, due to lack of device. * First stab at synch pipe implementation, the Dallas USB DAC * wants to use an Asynch out pipe. usb_audio_state now basically * only contains lists of mixer and wave devices. We can therefore * now have multiple mixer/wave devices per USB device. * 1999-10-28: Thomas Sailer * Converted to URB API. Fixed a taskstate/wakeup semantics mistake * that made the driver consume all available CPU cycles. * Now runs stable on UHCI-Acher/Fliegl/Sailer. * 1999-10-31: Thomas Sailer * Audio can now be unloaded if it is not in use by any mixer * or dsp client (formerly you had to disconnect the audio devices * from the USB port) * Finally, about three months after ordering, my "Maxxtro SPK222" * speakers arrived, isn't disdata a great mail order company 8-) * Parse class specific endpoint descriptor of the audiostreaming * interfaces and take the endpoint attributes from there. * Unbelievably, the Philips USB DAC has a sampling rate range * of over a decade, yet does not support the sampling rate control! * No wonder it sounds so bad, has very audible sampling rate * conversion distortion. Don't try to listen to it using * decent headphones! * "Let's make things better" -> but please Philips start with your * own stuff!!!! * 1999-11-02: Thomas Sailer * It takes the Philips boxes several seconds to acquire synchronisation * that means they won't play short sounds. Should probably maintain * the ISO datastream even if there's nothing to play. * Fix counting the total_bytes counter, RealPlayer G2 depends on it. * 1999-12-20: Thomas Sailer * Fix bad bug in conversion to per interface probing. * disconnect was called multiple times for the audio device, * leading to a premature freeing of the audio structures * 2000-05-13: Thomas Sailer * I don't remember who changed the find_format routine, * but the change was completely broken for the Dallas * chip. Anyway taking sampling rate into account in find_format * is bad and should not be done unless there are devices with * completely broken audio descriptors. Unless someone shows * me such a descriptor, I will not allow find_format to * take the sampling rate into account. * Also, the former find_format made: * - mpg123 play mono instead of stereo * - sox completely fail for wav's with sample rates < 44.1kHz * for the Dallas chip. * Also fix a rather long standing problem with applications that * use "small" writes producing no sound at all. * 2000-05-15: Thomas Sailer * My fears came true, the Philips camera indeed has pretty stupid * audio descriptors. * 2000-05-17: Thomas Sailer * Nemsoft spotted my stupid last minute change, thanks * 2000-05-19: Thomas Sailer * Fixed FEATURE_UNIT thinkos found thanks to the KC Technology * Xtend device. Basically the driver treated FEATURE_UNIT's sourced * by mono terminals as stereo. * 2000-05-20: Thomas Sailer * SELECTOR support (and thus selecting record channels from the mixer). * Somewhat peculiar due to OSS interface limitations. Only works * for channels where a "slider" is already in front of it (i.e. * a MIXER unit or a FEATURE unit with volume capability). * 2000-11-26: Thomas Sailer * Workaround for Dallas DS4201. The DS4201 uses PCM8 as format tag for * its 8 bit modes, but expects signed data (and should therefore have used PCM). * 2001-03-10: Thomas Sailer * provide abs function, prevent picking up a bogus kernel macro * for abs. Bug report by Andrew Morton <andrewm@uow.edu.au> * 2001-06-16: Bryce Nesbitt <bryce@obviously.com> * Fix SNDCTL_DSP_STEREO API violation * 2003-04-08: Oliver Neukum (oliver@neukum.name): * Setting a configuration is done by usbcore and must not be overridden * 2004-02-27: Workaround for broken synch descriptors * 2004-03-07: Alan Stern <stern@rowland.harvard.edu> * Add usb_ifnum_to_if() and usb_altnum_to_altsetting() support. * Use the in-memory descriptors instead of reading them from the device. * */ /* * Strategy: * * Alan Cox and Thomas Sailer are starting to dig at opposite ends and * are hoping to meet in the middle, just like tunnel diggers :) * Alan tackles the descriptor parsing, Thomas the actual data IO and the * OSS compatible interface. * * Data IO implementation issues * * A mmap'able ring buffer per direction is implemented, because * almost every OSS app expects it. It is however impractical to * transmit/receive USB data directly into and out of the ring buffer, * due to alignment and synchronisation issues. Instead, the ring buffer * feeds a constant time delay line that handles the USB issues. * * Now we first try to find an alternate setting that exactly matches * the sample format requested by the user. If we find one, we do not * need to perform any sample rate conversions. If there is no matching * altsetting, we choose the closest one and perform sample format * conversions. We never do sample rate conversion; these are too * expensive to be performed in the kernel. * * Current status: no known HCD-specific issues. * * Generally: Due to the brokenness of the Audio Class spec * it seems generally impossible to write a generic Audio Class driver, * so a reasonable driver should implement the features that are actually * used. * * Parsing implementation issues * * One cannot reasonably parse the AudioClass descriptors linearly. * Therefore the current implementation features routines to look * for a specific descriptor in the descriptor list. * * How does the parsing work? First, all interfaces are searched * for an AudioControl class interface. If found, the config descriptor * that belongs to the current configuration is searched and * the HEADER descriptor is found. It contains a list of * all AudioStreaming and MIDIStreaming devices. This list is then walked, * and all AudioStreaming interfaces are classified into input and output * interfaces (according to the endpoint0 direction in altsetting1) (MIDIStreaming * is currently not supported). The input & output list is then used * to group inputs and outputs together and issued pairwise to the * AudioStreaming class parser. Finally, all OUTPUT_TERMINAL descriptors * are walked and issued to the mixer construction routine. * * The AudioStreaming parser simply enumerates all altsettings belonging * to the specified interface. It looks for AS_GENERAL and FORMAT_TYPE * class specific descriptors to extract the sample format/sample rate * data. Only sample format types PCM and PCM8 are supported right now, and * only FORMAT_TYPE_I is handled. The isochronous data endpoint needs to * be the first endpoint of the interface, and the optional synchronisation * isochronous endpoint the second one. * * Mixer construction works as follows: The various TERMINAL and UNIT * descriptors span a tree from the root (OUTPUT_TERMINAL) through the * intermediate nodes (UNITs) to the leaves (INPUT_TERMINAL). We walk * that tree in a depth first manner. FEATURE_UNITs may contribute volume, * bass and treble sliders to the mixer, MIXER_UNITs volume sliders. * The terminal type encoded in the INPUT_TERMINALs feeds a heuristic * to determine "meaningful" OSS slider numbers, however we will see * how well this works in practice. Other features are not used at the * moment, they seem less often used. Also, it seems difficult at least * to construct recording source switches from SELECTOR_UNITs, but * since there are not many USB ADC's available, we leave that for later. */ /*****************************************************************************/ #include <linux/kernel.h> #include <linux/slab.h> #include <linux/string.h> #include <linux/timer.h> #include <linux/sched.h> #include <linux/smp_lock.h> #include <linux/module.h> #include <linux/sound.h> #include <linux/soundcard.h> #include <linux/list.h> #include <linux/vmalloc.h> #include <linux/init.h> #include <linux/poll.h> #include <linux/bitops.h> #include <asm/uaccess.h> #include <asm/io.h> #include <linux/usb.h> #include "audio.h" /* * Version Information */ #define DRIVER_VERSION "v1.0.0" #define DRIVER_AUTHOR "Alan Cox <alan@lxorguk.ukuu.org.uk>, Thomas Sailer (sailer@ife.ee.ethz.ch)" #define DRIVER_DESC "USB Audio Class driver" #define AUDIO_DEBUG 1 #define SND_DEV_DSP16 5 #define dprintk(x) /* --------------------------------------------------------------------- */ /* * Linked list of all audio devices... */ static struct list_head audiodevs = LIST_HEAD_INIT(audiodevs); static DECLARE_MUTEX(open_sem); /* * wait queue for processes wanting to open an USB audio device */ static DECLARE_WAIT_QUEUE_HEAD(open_wait); #define MAXFORMATS MAX_ALT #define DMABUFSHIFT 17 /* 128k worth of DMA buffer */ #define NRSGBUF (1U<<(DMABUFSHIFT-PAGE_SHIFT)) /* * This influences: * - Latency * - Interrupt rate * - Synchronisation behaviour * Don't touch this if you don't understand all of the above. */ #define DESCFRAMES 5 #define SYNCFRAMES DESCFRAMES #define MIXFLG_STEREOIN 1 #define MIXFLG_STEREOOUT 2 struct mixerchannel { __u16 value; __u16 osschannel; /* number of the OSS channel */ __s16 minval, maxval; __u16 slctunitid; __u8 unitid; __u8 selector; __u8 chnum; __u8 flags; }; struct audioformat { unsigned int format; unsigned int sratelo; unsigned int sratehi; unsigned char altsetting; unsigned char attributes; }; struct dmabuf { /* buffer data format */ unsigned int format; unsigned int srate; /* physical buffer */ unsigned char *sgbuf[NRSGBUF]; unsigned bufsize; unsigned numfrag; unsigned fragshift; unsigned wrptr, rdptr; unsigned total_bytes; int count; unsigned error; /* over/underrun */ wait_queue_head_t wait; /* redundant, but makes calculations easier */ unsigned fragsize; unsigned dmasize; /* OSS stuff */ unsigned mapped:1; unsigned ready:1; unsigned ossfragshift; int ossmaxfrags; unsigned subdivision; }; struct usb_audio_state; #define FLG_URB0RUNNING 1 #define FLG_URB1RUNNING 2 #define FLG_SYNC0RUNNING 4 #define FLG_SYNC1RUNNING 8 #define FLG_RUNNING 16 #define FLG_CONNECTED 32 struct my_data_urb { struct urb *urb; }; struct my_sync_urb { struct urb *urb; }; struct usb_audiodev { struct list_head list; struct usb_audio_state *state; /* soundcore stuff */ int dev_audio; /* wave stuff */ mode_t open_mode; spinlock_t lock; /* DMA buffer access spinlock */ struct usbin { int interface; /* Interface number, -1 means not used */ unsigned int format; /* USB data format */ unsigned int datapipe; /* the data input pipe */ unsigned int syncpipe; /* the synchronisation pipe - 0 for anything but adaptive IN mode */ unsigned int syncinterval; /* P for adaptive IN mode, 0 otherwise */ unsigned int freqn; /* nominal sampling rate in USB format, i.e. fs/1000 in Q10.14 */ unsigned int freqmax; /* maximum sampling rate, used for buffer management */ unsigned int phase; /* phase accumulator */ unsigned int flags; /* see FLG_ defines */ struct my_data_urb durb[2]; /* ISO descriptors for the data endpoint */ struct my_sync_urb surb[2]; /* ISO sync pipe descriptor if needed */ struct dmabuf dma; } usbin; struct usbout { int interface; /* Interface number, -1 means not used */ unsigned int format; /* USB data format */ unsigned int datapipe; /* the data input pipe */ unsigned int syncpipe; /* the synchronisation pipe - 0 for anything but asynchronous OUT mode */ unsigned int syncinterval; /* P for asynchronous OUT mode, 0 otherwise */ unsigned int freqn; /* nominal sampling rate in USB format, i.e. fs/1000 in Q10.14 */ unsigned int freqm; /* momentary sampling rate in USB format, i.e. fs/1000 in Q10.14 */ unsigned int freqmax; /* maximum sampling rate, used for buffer management */ unsigned int phase; /* phase accumulator */ unsigned int flags; /* see FLG_ defines */ struct my_data_urb durb[2]; /* ISO descriptors for the data endpoint */ struct my_sync_urb surb[2]; /* ISO sync pipe descriptor if needed */ struct dmabuf dma; } usbout; unsigned int numfmtin, numfmtout; struct audioformat fmtin[MAXFORMATS]; struct audioformat fmtout[MAXFORMATS]; }; struct usb_mixerdev { struct list_head list; struct usb_audio_state *state; /* soundcore stuff */ int dev_mixer; unsigned char iface; /* interface number of the AudioControl interface */ /* USB format descriptions */ unsigned int numch, modcnt; /* mixch is last and gets allocated dynamically */ struct mixerchannel ch[0]; }; struct usb_audio_state { struct list_head audiodev; /* USB device */ struct usb_device *usbdev; struct list_head audiolist; struct list_head mixerlist; unsigned count; /* usage counter; NOTE: the usb stack is also considered a user */ }; /* private audio format extensions */ #define AFMT_STEREO 0x80000000 #define AFMT_ISSTEREO(x) ((x) & AFMT_STEREO) #define AFMT_IS16BIT(x) ((x) & (AFMT_S16_LE|AFMT_S16_BE|AFMT_U16_LE|AFMT_U16_BE)) #define AFMT_ISUNSIGNED(x) ((x) & (AFMT_U8|AFMT_U16_LE|AFMT_U16_BE)) #define AFMT_BYTESSHIFT(x) ((AFMT_ISSTEREO(x) ? 1 : 0) + (AFMT_IS16BIT(x) ? 1 : 0)) #define AFMT_BYTES(x) (1<<AFMT_BYTESSHFIT(x)) /* --------------------------------------------------------------------- */ static inline unsigned ld2(unsigned int x) { unsigned r = 0; if (x >= 0x10000) { x >>= 16; r += 16; } if (x >= 0x100) { x >>= 8; r += 8; } if (x >= 0x10) { x >>= 4; r += 4; } if (x >= 4) { x >>= 2; r += 2; } if (x >= 2) r++; return r; } /* --------------------------------------------------------------------- */ /* * OSS compatible ring buffer management. The ring buffer may be mmap'ed into * an application address space. * * I first used the rvmalloc stuff copied from bttv. Alan Cox did not like it, so * we now use an array of pointers to a single page each. This saves us the * kernel page table manipulations, but we have to do a page table alike mechanism * (though only one indirection) in software. */ static void dmabuf_release(struct dmabuf *db) { unsigned int nr; void *p; for(nr = 0; nr < NRSGBUF; nr++) { if (!(p = db->sgbuf[nr])) continue; ClearPageReserved(virt_to_page(p)); free_page((unsigned long)p); db->sgbuf[nr] = NULL; } db->mapped = db->ready = 0; } static int dmabuf_init(struct dmabuf *db) { unsigned int nr, bytepersec, bufs; void *p; /* initialize some fields */ db->rdptr = db->wrptr = db->total_bytes = db->count = db->error = 0; /* calculate required buffer size */ bytepersec = db->srate << AFMT_BYTESSHIFT(db->format); bufs = 1U << DMABUFSHIFT; if (db->ossfragshift) { if ((1000 << db->ossfragshift) < bytepersec) db->fragshift = ld2(bytepersec/1000); else db->fragshift = db->ossfragshift; } else { db->fragshift = ld2(bytepersec/100/(db->subdivision ? db->subdivision : 1)); if (db->fragshift < 3) db->fragshift = 3; } db->numfrag = bufs >> db->fragshift; while (db->numfrag < 4 && db->fragshift > 3) { db->fragshift--; db->numfrag = bufs >> db->fragshift; } db->fragsize = 1 << db->fragshift; if (db->ossmaxfrags >= 4 && db->ossmaxfrags < db->numfrag) db->numfrag = db->ossmaxfrags; db->dmasize = db->numfrag << db->fragshift; for(nr = 0; nr < NRSGBUF; nr++) { if (!db->sgbuf[nr]) { p = (void *)get_zeroed_page(GFP_KERNEL); if (!p) return -ENOMEM; db->sgbuf[nr] = p; SetPageReserved(virt_to_page(p)); } memset(db->sgbuf[nr], AFMT_ISUNSIGNED(db->format) ? 0x80 : 0, PAGE_SIZE); if ((nr << PAGE_SHIFT) >= db->dmasize) break; } db->bufsize = nr << PAGE_SHIFT; db->ready = 1; dprintk((KERN_DEBUG "usbaudio: dmabuf_init bytepersec %d bufs %d ossfragshift %d ossmaxfrags %d " "fragshift %d fragsize %d numfrag %d dmasize %d bufsize %d fmt 0x%x srate %d\n", bytepersec, bufs, db->ossfragshift, db->ossmaxfrags, db->fragshift, db->fragsize, db->numfrag, db->dmasize, db->bufsize, db->format, db->srate)); return 0; } static int dmabuf_mmap(struct vm_area_struct *vma, struct dmabuf *db, unsigned long start, unsigned long size, pgprot_t prot) { unsigned int nr; if (!db->ready || db->mapped || (start | size) & (PAGE_SIZE-1) || size > db->bufsize) return -EINVAL; size >>= PAGE_SHIFT; for(nr = 0; nr < size; nr++) if (!db->sgbuf[nr]) return -EINVAL; db->mapped = 1; for(nr = 0; nr < size; nr++) { if (remap_page_range(vma, start, virt_to_phys(db->sgbuf[nr]), PAGE_SIZE, prot)) return -EAGAIN; start += PAGE_SIZE; } return 0; } static void dmabuf_copyin(struct dmabuf *db, const void *buffer, unsigned int size) { unsigned int pgrem, rem; db->total_bytes += size; for (;;) { if (size <= 0) return; pgrem = ((~db->wrptr) & (PAGE_SIZE-1)) + 1; if (pgrem > size) pgrem = size; rem = db->dmasize - db->wrptr; if (pgrem > rem) pgrem = rem; memcpy((db->sgbuf[db->wrptr >> PAGE_SHIFT]) + (db->wrptr & (PAGE_SIZE-1)), buffer, pgrem); size -= pgrem; buffer += pgrem; db->wrptr += pgrem; if (db->wrptr >= db->dmasize) db->wrptr = 0; } } static void dmabuf_copyout(struct dmabuf *db, void *buffer, unsigned int size) { unsigned int pgrem, rem; db->total_bytes += size; for (;;) { if (size <= 0) return; pgrem = ((~db->rdptr) & (PAGE_SIZE-1)) + 1; if (pgrem > size) pgrem = size; rem = db->dmasize - db->rdptr; if (pgrem > rem) pgrem = rem; memcpy(buffer, (db->sgbuf[db->rdptr >> PAGE_SHIFT]) + (db->rdptr & (PAGE_SIZE-1)), pgrem); size -= pgrem; buffer += pgrem; db->rdptr += pgrem; if (db->rdptr >= db->dmasize) db->rdptr = 0; } } static int dmabuf_copyin_user(struct dmabuf *db, unsigned int ptr, const void __user *buffer, unsigned int size) { unsigned int pgrem, rem; if (!db->ready || db->mapped) return -EINVAL; for (;;) { if (size <= 0) return 0; pgrem = ((~ptr) & (PAGE_SIZE-1)) + 1; if (pgrem > size) pgrem = size; rem = db->dmasize - ptr; if (pgrem > rem) pgrem = rem; if (copy_from_user((db->sgbuf[ptr >> PAGE_SHIFT]) + (ptr & (PAGE_SIZE-1)), buffer, pgrem)) return -EFAULT; size -= pgrem; buffer += pgrem; ptr += pgrem; if (ptr >= db->dmasize) ptr = 0; } } static int dmabuf_copyout_user(struct dmabuf *db, unsigned int ptr, void __user *buffer, unsigned int size) { unsigned int pgrem, rem; if (!db->ready || db->mapped) return -EINVAL; for (;;) { if (size <= 0) return 0; pgrem = ((~ptr) & (PAGE_SIZE-1)) + 1; if (pgrem > size) pgrem = size; rem = db->dmasize - ptr; if (pgrem > rem) pgrem = rem; if (copy_to_user(buffer, (db->sgbuf[ptr >> PAGE_SHIFT]) + (ptr & (PAGE_SIZE-1)), pgrem)) return -EFAULT; size -= pgrem; buffer += pgrem; ptr += pgrem; if (ptr >= db->dmasize) ptr = 0; } } /* --------------------------------------------------------------------- */ /* * USB I/O code. We do sample format conversion if necessary */ static void usbin_stop(struct usb_audiodev *as) { struct usbin *u = &as->usbin; unsigned long flags; unsigned int i, notkilled = 1; spin_lock_irqsave(&as->lock, flags); u->flags &= ~FLG_RUNNING; i = u->flags; spin_unlock_irqrestore(&as->lock, flags); while (i & (FLG_URB0RUNNING|FLG_URB1RUNNING|FLG_SYNC0RUNNING|FLG_SYNC1RUNNING)) { set_current_state(notkilled ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE); schedule_timeout(1); spin_lock_irqsave(&as->lock, flags); i = u->flags; spin_unlock_irqrestore(&as->lock, flags); if (notkilled && signal_pending(current)) { if (i & FLG_URB0RUNNING) usb_unlink_urb(u->durb[0].urb); if (i & FLG_URB1RUNNING) usb_unlink_urb(u->durb[1].urb); if (i & FLG_SYNC0RUNNING) usb_unlink_urb(u->surb[0].urb); if (i & FLG_SYNC1RUNNING) usb_unlink_urb(u->surb[1].urb); notkilled = 0; } } set_current_state(TASK_RUNNING); if (u->durb[0].urb->transfer_buffer) kfree(u->durb[0].urb->transfer_buffer); if (u->durb[1].urb->transfer_buffer) kfree(u->durb[1].urb->transfer_buffer); if (u->surb[0].urb->transfer_buffer) kfree(u->surb[0].urb->transfer_buffer); if (u->surb[1].urb->transfer_buffer) kfree(u->surb[1].urb->transfer_buffer); u->durb[0].urb->transfer_buffer = u->durb[1].urb->transfer_buffer = u->surb[0].urb->transfer_buffer = u->surb[1].urb->transfer_buffer = NULL; } static inline void usbin_release(struct usb_audiodev *as) { usbin_stop(as); } static void usbin_disc(struct usb_audiodev *as) { struct usbin *u = &as->usbin; unsigned long flags; spin_lock_irqsave(&as->lock, flags); u->flags &= ~(FLG_RUNNING | FLG_CONNECTED); spin_unlock_irqrestore(&as->lock, flags); usbin_stop(as); } static void conversion(const void *ibuf, unsigned int ifmt, void *obuf, unsigned int ofmt, void *tmp, unsigned int scnt) { unsigned int cnt, i; __s16 *sp, *sp2, s; unsigned char *bp; cnt = scnt; if (AFMT_ISSTEREO(ifmt)) cnt <<= 1; sp = ((__s16 *)tmp) + cnt; switch (ifmt & ~AFMT_STEREO) { case AFMT_U8: for (bp = ((unsigned char *)ibuf)+cnt, i = 0; i < cnt; i++) { bp--; sp--; *sp = (*bp ^ 0x80) << 8; } break; case AFMT_S8: for (bp = ((unsigned char *)ibuf)+cnt, i = 0; i < cnt; i++) { bp--; sp--; *sp = *bp << 8; } break; case AFMT_U16_LE: for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) { bp -= 2; sp--; *sp = (bp[0] | (bp[1] << 8)) ^ 0x8000; } break; case AFMT_U16_BE: for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) { bp -= 2; sp--; *sp = (bp[1] | (bp[0] << 8)) ^ 0x8000; } break; case AFMT_S16_LE: for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) { bp -= 2; sp--; *sp = bp[0] | (bp[1] << 8); } break; case AFMT_S16_BE: for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) { bp -= 2; sp--; *sp = bp[1] | (bp[0] << 8); } break; } if (!AFMT_ISSTEREO(ifmt) && AFMT_ISSTEREO(ofmt)) { /* expand from mono to stereo */ for (sp = ((__s16 *)tmp)+scnt, sp2 = ((__s16 *)tmp)+2*scnt, i = 0; i < scnt; i++) { sp--; sp2 -= 2; sp2[0] = sp2[1] = sp[0]; } } if (AFMT_ISSTEREO(ifmt) && !AFMT_ISSTEREO(ofmt)) { /* contract from stereo to mono */ for (sp = sp2 = ((__s16 *)tmp), i = 0; i < scnt; i++, sp++, sp2 += 2) sp[0] = (sp2[0] + sp2[1]) >> 1; } cnt = scnt; if (AFMT_ISSTEREO(ofmt)) cnt <<= 1; sp = ((__s16 *)tmp); bp = ((unsigned char *)obuf); switch (ofmt & ~AFMT_STEREO) { case AFMT_U8: for (i = 0; i < cnt; i++, sp++, bp++) *bp = (*sp >> 8) ^ 0x80; break; case AFMT_S8: for (i = 0; i < cnt; i++, sp++, bp++) *bp = *sp >> 8; break; case AFMT_U16_LE: for (i = 0; i < cnt; i++, sp++, bp += 2) { s = *sp; bp[0] = s; bp[1] = (s >> 8) ^ 0x80; } break; case AFMT_U16_BE: for (i = 0; i < cnt; i++, sp++, bp += 2) { s = *sp; bp[1] = s; bp[0] = (s >> 8) ^ 0x80; } break; case AFMT_S16_LE: for (i = 0; i < cnt; i++, sp++, bp += 2) { s = *sp; bp[0] = s; bp[1] = s >> 8; } break; case AFMT_S16_BE: for (i = 0; i < cnt; i++, sp++, bp += 2) { s = *sp; bp[1] = s; bp[0] = s >> 8; } break; } } static void usbin_convert(struct usbin *u, unsigned char *buffer, unsigned int samples) { union { __s16 s[64]; unsigned char b[0]; } tmp; unsigned int scnt, maxs, ufmtsh, dfmtsh; ufmtsh = AFMT_BYTESSHIFT(u->format); dfmtsh = AFMT_BYTESSHIFT(u->dma.format); maxs = (AFMT_ISSTEREO(u->dma.format | u->format)) ? 32 : 64; while (samples > 0) { scnt = samples; if (scnt > maxs) scnt = maxs; conversion(buffer, u->format, tmp.b, u->dma.format, tmp.b, scnt); dmabuf_copyin(&u->dma, tmp.b, scnt << dfmtsh); buffer += scnt << ufmtsh; samples -= scnt; } } static int usbin_prepare_desc(struct usbin *u, struct urb *urb) { unsigned int i, maxsize, offs; maxsize = (u->freqmax + 0x3fff) >> (14 - AFMT_BYTESSHIFT(u->format)); //printk(KERN_DEBUG "usbin_prepare_desc: maxsize %d freq 0x%x format 0x%x\n", maxsize, u->freqn, u->format); for (i = offs = 0; i < DESCFRAMES; i++, offs += maxsize) { urb->iso_frame_desc[i].length = maxsize; urb->iso_frame_desc[i].offset = offs; } urb->interval = 1; return 0; } /* * return value: 0 if descriptor should be restarted, -1 otherwise * convert sample format on the fly if necessary */ static int usbin_retire_desc(struct usbin *u, struct urb *urb) { unsigned int i, ufmtsh, dfmtsh, err = 0, cnt, scnt, dmafree; unsigned char *cp; ufmtsh = AFMT_BYTESSHIFT(u->format); dfmtsh = AFMT_BYTESSHIFT(u->dma.format); for (i = 0; i < DESCFRAMES; i++) { cp = ((unsigned char *)urb->transfer_buffer) + urb->iso_frame_desc[i].offset; if (urb->iso_frame_desc[i].status) { dprintk((KERN_DEBUG "usbin_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status)); continue; } scnt = urb->iso_frame_desc[i].actual_length >> ufmtsh; if (!scnt) continue; cnt = scnt << dfmtsh; if (!u->dma.mapped) { dmafree = u->dma.dmasize - u->dma.count; if (cnt > dmafree) { scnt = dmafree >> dfmtsh; cnt = scnt << dfmtsh; err++; } } u->dma.count += cnt; if (u->format == u->dma.format) { /* we do not need format conversion */ dprintk((KERN_DEBUG "usbaudio: no sample format conversion\n")); dmabuf_copyin(&u->dma, cp, cnt); } else { /* we need sampling format conversion */ dprintk((KERN_DEBUG "usbaudio: sample format conversion %x != %x\n", u->format, u->dma.format)); usbin_convert(u, cp, scnt); } } if (err) u->dma.error++; if (u->dma.count >= (signed)u->dma.fragsize) wake_up(&u->dma.wait); return err ? -1 : 0; } static void usbin_completed(struct urb *urb, struct pt_regs *regs) { struct usb_audiodev *as = (struct usb_audiodev *)urb->context; struct usbin *u = &as->usbin; unsigned long flags; unsigned int mask; int suret = 0; #if 0 printk(KERN_DEBUG "usbin_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags); #endif if (urb == u->durb[0].urb) mask = FLG_URB0RUNNING; else if (urb == u->durb[1].urb) mask = FLG_URB1RUNNING; else { mask = 0; printk(KERN_ERR "usbin_completed: panic: unknown URB\n"); } urb->dev = as->state->usbdev; spin_lock_irqsave(&as->lock, flags); if (!usbin_retire_desc(u, urb) && u->flags & FLG_RUNNING && !usbin_prepare_desc(u, urb) && (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) { u->flags |= mask; } else { u->flags &= ~(mask | FLG_RUNNING); wake_up(&u->dma.wait); printk(KERN_DEBUG "usbin_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret); } spin_unlock_irqrestore(&as->lock, flags); } /* * we output sync data */ static int usbin_sync_prepare_desc(struct usbin *u, struct urb *urb) { unsigned char *cp = urb->transfer_buffer; unsigned int i, offs; for (i = offs = 0; i < SYNCFRAMES; i++, offs += 3, cp += 3) { urb->iso_frame_desc[i].length = 3; urb->iso_frame_desc[i].offset = offs; cp[0] = u->freqn; cp[1] = u->freqn >> 8; cp[2] = u->freqn >> 16; } urb->interval = 1; return 0; } /* * return value: 0 if descriptor should be restarted, -1 otherwise */ static int usbin_sync_retire_desc(struct usbin *u, struct urb *urb) { unsigned int i; for (i = 0; i < SYNCFRAMES; i++) if (urb->iso_frame_desc[0].status) dprintk((KERN_DEBUG "usbin_sync_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status)); return 0; } static void usbin_sync_completed(struct urb *urb, struct pt_regs *regs) { struct usb_audiodev *as = (struct usb_audiodev *)urb->context; struct usbin *u = &as->usbin; unsigned long flags; unsigned int mask; int suret = 0; #if 0 printk(KERN_DEBUG "usbin_sync_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags); #endif if (urb == u->surb[0].urb) mask = FLG_SYNC0RUNNING; else if (urb == u->surb[1].urb) mask = FLG_SYNC1RUNNING; else { mask = 0; printk(KERN_ERR "usbin_sync_completed: panic: unknown URB\n"); } urb->dev = as->state->usbdev; spin_lock_irqsave(&as->lock, flags); if (!usbin_sync_retire_desc(u, urb) && u->flags & FLG_RUNNING && !usbin_sync_prepare_desc(u, urb) && (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) { u->flags |= mask; } else { u->flags &= ~(mask | FLG_RUNNING); wake_up(&u->dma.wait); dprintk((KERN_DEBUG "usbin_sync_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret)); } spin_unlock_irqrestore(&as->lock, flags); } static int usbin_start(struct usb_audiodev *as) { struct usb_device *dev = as->state->usbdev; struct usbin *u = &as->usbin; struct urb *urb; unsigned long flags; unsigned int maxsze, bufsz; #if 0 printk(KERN_DEBUG "usbin_start: device %d ufmt 0x%08x dfmt 0x%08x srate %d\n", dev->devnum, u->format, u->dma.format, u->dma.srate); #endif /* allocate USB storage if not already done */ spin_lock_irqsave(&as->lock, flags); if (!(u->flags & FLG_CONNECTED)) { spin_unlock_irqrestore(&as->lock, flags); return -EIO; } if (!(u->flags & FLG_RUNNING)) { spin_unlock_irqrestore(&as->lock, flags); u->freqn = ((u->dma.srate << 11) + 62) / 125; /* this will overflow at approx 2MSPS */ u->freqmax = u->freqn + (u->freqn >> 2); u->phase = 0; maxsze = (u->freqmax + 0x3fff) >> (14 - AFMT_BYTESSHIFT(u->format)); bufsz = DESCFRAMES * maxsze; if (u->durb[0].urb->transfer_buffer) kfree(u->durb[0].urb->transfer_buffer); u->durb[0].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL); u->durb[0].urb->transfer_buffer_length = bufsz; if (u->durb[1].urb->transfer_buffer) kfree(u->durb[1].urb->transfer_buffer); u->durb[1].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL); u->durb[1].urb->transfer_buffer_length = bufsz; if (u->syncpipe) { if (u->surb[0].urb->transfer_buffer) kfree(u->surb[0].urb->transfer_buffer); u->surb[0].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL); u->surb[0].urb->transfer_buffer_length = 3*SYNCFRAMES; if (u->surb[1].urb->transfer_buffer) kfree(u->surb[1].urb->transfer_buffer); u->surb[1].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL); u->surb[1].urb->transfer_buffer_length = 3*SYNCFRAMES; } if (!u->durb[0].urb->transfer_buffer || !u->durb[1].urb->transfer_buffer || (u->syncpipe && (!u->surb[0].urb->transfer_buffer || !u->surb[1].urb->transfer_buffer))) { printk(KERN_ERR "usbaudio: cannot start playback device %d\n", dev->devnum); return 0; } spin_lock_irqsave(&as->lock, flags); } if (u->dma.count >= u->dma.dmasize && !u->dma.mapped) { spin_unlock_irqrestore(&as->lock, flags); return 0; } u->flags |= FLG_RUNNING; if (!(u->flags & FLG_URB0RUNNING)) { urb = u->durb[0].urb; urb->dev = dev; urb->pipe = u->datapipe; urb->transfer_flags = URB_ISO_ASAP; urb->number_of_packets = DESCFRAMES; urb->context = as; urb->complete = usbin_completed; if (!usbin_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL)) u->flags |= FLG_URB0RUNNING; else u->flags &= ~FLG_RUNNING; } if (u->flags & FLG_RUNNING && !(u->flags & FLG_URB1RUNNING)) { urb = u->durb[1].urb; urb->dev = dev; urb->pipe = u->datapipe; urb->transfer_flags = URB_ISO_ASAP; urb->number_of_packets = DESCFRAMES; urb->context = as; urb->complete = usbin_completed; if (!usbin_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL)) u->flags |= FLG_URB1RUNNING; else u->flags &= ~FLG_RUNNING; } if (u->syncpipe) { if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC0RUNNING)) { urb = u->surb[0].urb; urb->dev = dev; urb->pipe = u->syncpipe; urb->transfer_flags = URB_ISO_ASAP; urb->number_of_packets = SYNCFRAMES; urb->context = as; urb->complete = usbin_sync_completed; /* stride: u->syncinterval */ if (!usbin_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL)) u->flags |= FLG_SYNC0RUNNING; else u->flags &= ~FLG_RUNNING; } if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC1RUNNING)) { urb = u->surb[1].urb; urb->dev = dev; urb->pipe = u->syncpipe; urb->transfer_flags = URB_ISO_ASAP; urb->number_of_packets = SYNCFRAMES; urb->context = as; urb->complete = usbin_sync_completed; /* stride: u->syncinterval */ if (!usbin_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL)) u->flags |= FLG_SYNC1RUNNING; else u->flags &= ~FLG_RUNNING; } } spin_unlock_irqrestore(&as->lock, flags); return 0; } static void usbout_stop(struct usb_audiodev *as) { struct usbout *u = &as->usbout; unsigned long flags; unsigned int i, notkilled = 1; spin_lock_irqsave(&as->lock, flags); u->flags &= ~FLG_RUNNING; i = u->flags; spin_unlock_irqrestore(&as->lock, flags); while (i & (FLG_URB0RUNNING|FLG_URB1RUNNING|FLG_SYNC0RUNNING|FLG_SYNC1RUNNING)) { set_current_state(notkilled ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE); schedule_timeout(1); spin_lock_irqsave(&as->lock, flags); i = u->flags; spin_unlock_irqrestore(&as->lock, flags); if (notkilled && signal_pending(current)) { if (i & FLG_URB0RUNNING) usb_unlink_urb(u->durb[0].urb); if (i & FLG_URB1RUNNING) usb_unlink_urb(u->durb[1].urb); if (i & FLG_SYNC0RUNNING) usb_unlink_urb(u->surb[0].urb); if (i & FLG_SYNC1RUNNING) usb_unlink_urb(u->surb[1].urb); notkilled = 0; } } set_current_state(TASK_RUNNING); if (u->durb[0].urb->transfer_buffer) kfree(u->durb[0].urb->transfer_buffer); if (u->durb[1].urb->transfer_buffer) kfree(u->durb[1].urb->transfer_buffer); if (u->surb[0].urb->transfer_buffer) kfree(u->surb[0].urb->transfer_buffer); if (u->surb[1].urb->transfer_buffer) kfree(u->surb[1].urb->transfer_buffer); u->durb[0].urb->transfer_buffer = u->durb[1].urb->transfer_buffer = u->surb[0].urb->transfer_buffer = u->surb[1].urb->transfer_buffer = NULL; } static inline void usbout_release(struct usb_audiodev *as) { usbout_stop(as); } static void usbout_disc(struct usb_audiodev *as) { struct usbout *u = &as->usbout; unsigned long flags; spin_lock_irqsave(&as->lock, flags); u->flags &= ~(FLG_RUNNING | FLG_CONNECTED); spin_unlock_irqrestore(&as->lock, flags); usbout_stop(as); } static void usbout_convert(struct usbout *u, unsigned char *buffer, unsigned int samples) { union { __s16 s[64]; unsigned char b[0]; } tmp; unsigned int scnt, maxs, ufmtsh, dfmtsh; ufmtsh = AFMT_BYTESSHIFT(u->format); dfmtsh = AFMT_BYTESSHIFT(u->dma.format); maxs = (AFMT_ISSTEREO(u->dma.format | u->format)) ? 32 : 64; while (samples > 0) { scnt = samples; if (scnt > maxs) scnt = maxs; dmabuf_copyout(&u->dma, tmp.b, scnt << dfmtsh); conversion(tmp.b, u->dma.format, buffer, u->format, tmp.b, scnt); buffer += scnt << ufmtsh; samples -= scnt; } } static int usbout_prepare_desc(struct usbout *u, struct urb *urb) { unsigned int i, ufmtsh, dfmtsh, err = 0, cnt, scnt, offs; unsigned char *cp = urb->transfer_buffer; ufmtsh = AFMT_BYTESSHIFT(u->format); dfmtsh = AFMT_BYTESSHIFT(u->dma.format); for (i = offs = 0; i < DESCFRAMES; i++) { urb->iso_frame_desc[i].offset = offs; u->phase = (u->phase & 0x3fff) + u->freqm; scnt = u->phase >> 14; if (!scnt) { urb->iso_frame_desc[i].length = 0; continue; } cnt = scnt << dfmtsh; if (!u->dma.mapped) { if (cnt > u->dma.count) { scnt = u->dma.count >> dfmtsh; cnt = scnt << dfmtsh; err++; } u->dma.count -= cnt; } else u->dma.count += cnt; if (u->format == u->dma.format) { /* we do not need format conversion */ dmabuf_copyout(&u->dma, cp, cnt); } else { /* we need sampling format conversion */ usbout_convert(u, cp, scnt); } cnt = scnt << ufmtsh; urb->iso_frame_desc[i].length = cnt; offs += cnt; cp += cnt; } urb->interval = 1; if (err) u->dma.error++; if (u->dma.mapped) { if (u->dma.count >= (signed)u->dma.fragsize) wake_up(&u->dma.wait); } else { if ((signed)u->dma.dmasize >= u->dma.count + (signed)u->dma.fragsize) wake_up(&u->dma.wait); } return err ? -1 : 0; } /* * return value: 0 if descriptor should be restarted, -1 otherwise */ static int usbout_retire_desc(struct usbout *u, struct urb *urb) { unsigned int i; for (i = 0; i < DESCFRAMES; i++) { if (urb->iso_frame_desc[i].status) { dprintk((KERN_DEBUG "usbout_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status)); continue; } } return 0; } static void usbout_completed(struct urb *urb, struct pt_regs *regs) { struct usb_audiodev *as = (struct usb_audiodev *)urb->context; struct usbout *u = &as->usbout; unsigned long flags; unsigned int mask; int suret = 0; #if 0 printk(KERN_DEBUG "usbout_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags); #endif if (urb == u->durb[0].urb) mask = FLG_URB0RUNNING; else if (urb == u->durb[1].urb) mask = FLG_URB1RUNNING; else { mask = 0; printk(KERN_ERR "usbout_completed: panic: unknown URB\n"); } urb->dev = as->state->usbdev; spin_lock_irqsave(&as->lock, flags); if (!usbout_retire_desc(u, urb) && u->flags & FLG_RUNNING && !usbout_prepare_desc(u, urb) && (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) { u->flags |= mask; } else { u->flags &= ~(mask | FLG_RUNNING); wake_up(&u->dma.wait); dprintk((KERN_DEBUG "usbout_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret)); } spin_unlock_irqrestore(&as->lock, flags); } static int usbout_sync_prepare_desc(struct usbout *u, struct urb *urb) { unsigned int i, offs; for (i = offs = 0; i < SYNCFRAMES; i++, offs += 3) { urb->iso_frame_desc[i].length = 3; urb->iso_frame_desc[i].offset = offs; } urb->interval = 1; return 0; } /* * return value: 0 if descriptor should be restarted, -1 otherwise */ static int usbout_sync_retire_desc(struct usbout *u, struct urb *urb) { unsigned char *cp = urb->transfer_buffer; unsigned int f, i; for (i = 0; i < SYNCFRAMES; i++, cp += 3) { if (urb->iso_frame_desc[i].status) { dprintk((KERN_DEBUG "usbout_sync_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status)); continue; } if (urb->iso_frame_desc[i].actual_length < 3) { dprintk((KERN_DEBUG "usbout_sync_retire_desc: frame %u length %d\n", i, urb->iso_frame_desc[i].actual_length)); continue; } f = cp[0] | (cp[1] << 8) | (cp[2] << 16); if (abs(f - u->freqn) > (u->freqn >> 3) || f > u->freqmax) { printk(KERN_WARNING "usbout_sync_retire_desc: requested frequency %u (nominal %u) out of range!\n", f, u->freqn); continue; } u->freqm = f; } return 0; } static void usbout_sync_completed(struct urb *urb, struct pt_regs *regs) { struct usb_audiodev *as = (struct usb_audiodev *)urb->context; struct usbout *u = &as->usbout; unsigned long flags; unsigned int mask; int suret = 0; #if 0 printk(KERN_DEBUG "usbout_sync_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags); #endif if (urb == u->surb[0].urb) mask = FLG_SYNC0RUNNING; else if (urb == u->surb[1].urb) mask = FLG_SYNC1RUNNING; else { mask = 0; printk(KERN_ERR "usbout_sync_completed: panic: unknown URB\n"); } urb->dev = as->state->usbdev; spin_lock_irqsave(&as->lock, flags); if (!usbout_sync_retire_desc(u, urb) && u->flags & FLG_RUNNING && !usbout_sync_prepare_desc(u, urb) && (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) { u->flags |= mask; } else { u->flags &= ~(mask | FLG_RUNNING); wake_up(&u->dma.wait); dprintk((KERN_DEBUG "usbout_sync_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret)); } spin_unlock_irqrestore(&as->lock, flags); } static int usbout_start(struct usb_audiodev *as) { struct usb_device *dev = as->state->usbdev; struct usbout *u = &as->usbout; struct urb *urb; unsigned long flags; unsigned int maxsze, bufsz; #if 0 printk(KERN_DEBUG "usbout_start: device %d ufmt 0x%08x dfmt 0x%08x srate %d\n", dev->devnum, u->format, u->dma.format, u->dma.srate); #endif /* allocate USB storage if not already done */ spin_lock_irqsave(&as->lock, flags); if (!(u->flags & FLG_CONNECTED)) { spin_unlock_irqrestore(&as->lock, flags); return -EIO; } if (!(u->flags & FLG_RUNNING)) { spin_unlock_irqrestore(&as->lock, flags); u->freqn = u->freqm = ((u->dma.srate << 11) + 62) / 125; /* this will overflow at approx 2MSPS */ u->freqmax = u->freqn + (u->freqn >> 2); u->phase = 0; maxsze = (u->freqmax + 0x3fff) >> (14 - AFMT_BYTESSHIFT(u->format)); bufsz = DESCFRAMES * maxsze; if (u->durb[0].urb->transfer_buffer) kfree(u->durb[0].urb->transfer_buffer); u->durb[0].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL); u->durb[0].urb->transfer_buffer_length = bufsz; if (u->durb[1].urb->transfer_buffer) kfree(u->durb[1].urb->transfer_buffer); u->durb[1].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL); u->durb[1].urb->transfer_buffer_length = bufsz; if (u->syncpipe) { if (u->surb[0].urb->transfer_buffer) kfree(u->surb[0].urb->transfer_buffer); u->surb[0].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL); u->surb[0].urb->transfer_buffer_length = 3*SYNCFRAMES; if (u->surb[1].urb->transfer_buffer) kfree(u->surb[1].urb->transfer_buffer); u->surb[1].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL); u->surb[1].urb->transfer_buffer_length = 3*SYNCFRAMES; } if (!u->durb[0].urb->transfer_buffer || !u->durb[1].urb->transfer_buffer || (u->syncpipe && (!u->surb[0].urb->transfer_buffer || !u->surb[1].urb->transfer_buffer))) { printk(KERN_ERR "usbaudio: cannot start playback device %d\n", dev->devnum); return 0; } spin_lock_irqsave(&as->lock, flags); } if (u->dma.count <= 0 && !u->dma.mapped) { spin_unlock_irqrestore(&as->lock, flags); return 0; } u->flags |= FLG_RUNNING; if (!(u->flags & FLG_URB0RUNNING)) { urb = u->durb[0].urb; urb->dev = dev; urb->pipe = u->datapipe; urb->transfer_flags = URB_ISO_ASAP; urb->number_of_packets = DESCFRAMES; urb->context = as; urb->complete = usbout_completed; if (!usbout_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC)) u->flags |= FLG_URB0RUNNING; else u->flags &= ~FLG_RUNNING; } if (u->flags & FLG_RUNNING && !(u->flags & FLG_URB1RUNNING)) { urb = u->durb[1].urb; urb->dev = dev; urb->pipe = u->datapipe; urb->transfer_flags = URB_ISO_ASAP; urb->number_of_packets = DESCFRAMES; urb->context = as; urb->complete = usbout_completed; if (!usbout_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC)) u->flags |= FLG_URB1RUNNING; else u->flags &= ~FLG_RUNNING; } if (u->syncpipe) { if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC0RUNNING)) { urb = u->surb[0].urb; urb->dev = dev; urb->pipe = u->syncpipe; urb->transfer_flags = URB_ISO_ASAP; urb->number_of_packets = SYNCFRAMES; urb->context = as; urb->complete = usbout_sync_completed; /* stride: u->syncinterval */ if (!usbout_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC)) u->flags |= FLG_SYNC0RUNNING; else u->flags &= ~FLG_RUNNING; } if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC1RUNNING)) { urb = u->surb[1].urb; urb->dev = dev; urb->pipe = u->syncpipe; urb->transfer_flags = URB_ISO_ASAP; urb->number_of_packets = SYNCFRAMES; urb->context = as; urb->complete = usbout_sync_completed; /* stride: u->syncinterval */ if (!usbout_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC)) u->flags |= FLG_SYNC1RUNNING; else u->flags &= ~FLG_RUNNING; } } spin_unlock_irqrestore(&as->lock, flags); return 0; } /* --------------------------------------------------------------------- */ static unsigned int format_goodness(struct audioformat *afp, unsigned int fmt, unsigned int srate) { unsigned int g = 0; if (srate < afp->sratelo) g += afp->sratelo - srate; if (srate > afp->sratehi) g += srate - afp->sratehi; if (AFMT_ISSTEREO(afp->format) && !AFMT_ISSTEREO(fmt)) g += 0x100000; if (!AFMT_ISSTEREO(afp->format) && AFMT_ISSTEREO(fmt)) g += 0x400000; if (AFMT_IS16BIT(afp->format) && !AFMT_IS16BIT(fmt)) g += 0x100000; if (!AFMT_IS16BIT(afp->format) && AFMT_IS16BIT(fmt)) g += 0x400000; return g; } static int find_format(struct audioformat *afp, unsigned int nr, unsigned int fmt, unsigned int srate) { unsigned int i, g, gb = ~0; int j = -1; /* default to failure */ /* find "best" format (according to format_goodness) */ for (i = 0; i < nr; i++) { g = format_goodness(&afp[i], fmt, srate); if (g >= gb) continue; j = i; gb = g; } return j; } static int set_format_in(struct usb_audiodev *as) { struct usb_device *dev = as->state->usbdev; struct usb_host_interface *alts; struct usb_interface *iface; struct usbin *u = &as->usbin; struct dmabuf *d = &u->dma; struct audioformat *fmt; unsigned int ep; unsigned char data[3]; int fmtnr, ret; iface = usb_ifnum_to_if(dev, u->interface); if (!iface) return 0; fmtnr = find_format(as->fmtin, as->numfmtin, d->format, d->srate); if (fmtnr < 0) { printk(KERN_ERR "usbaudio: set_format_in(): failed to find desired format/speed combination.\n"); return -1; } fmt = as->fmtin + fmtnr; alts = usb_altnum_to_altsetting(iface, fmt->altsetting); u->format = fmt->format; u->datapipe = usb_rcvisocpipe(dev, alts->endpoint[0].desc.bEndpointAddress & 0xf); u->syncpipe = u->syncinterval = 0; if ((alts->endpoint[0].desc.bmAttributes & 0x0c) == 0x08) { if (alts->desc.bNumEndpoints < 2 || alts->endpoint[1].desc.bmAttributes != 0x01 || alts->endpoint[1].desc.bSynchAddress != 0 || alts->endpoint[1].desc.bEndpointAddress != (alts->endpoint[0].desc.bSynchAddress & 0x7f)) { printk(KERN_WARNING "usbaudio: device %d interface %d altsetting %d claims adaptive in " "but has invalid synch pipe; treating as asynchronous in\n", dev->devnum, u->interface, fmt->altsetting); } else { u->syncpipe = usb_sndisocpipe(dev, alts->endpoint[1].desc.bEndpointAddress & 0xf); u->syncinterval = alts->endpoint[1].desc.bRefresh; } } if (d->srate < fmt->sratelo) d->srate = fmt->sratelo; if (d->srate > fmt->sratehi) d->srate = fmt->sratehi; dprintk((KERN_DEBUG "usbaudio: set_format_in: usb_set_interface %u %u\n", u->interface, fmt->altsetting)); if (usb_set_interface(dev, alts->desc.bInterfaceNumber, fmt->altsetting) < 0) { printk(KERN_WARNING "usbaudio: usb_set_interface failed, device %d interface %d altsetting %d\n", dev->devnum, u->interface, fmt->altsetting); return -1; } if (fmt->sratelo == fmt->sratehi) return 0; ep = usb_pipeendpoint(u->datapipe) | (u->datapipe & USB_DIR_IN); /* if endpoint has pitch control, enable it */ if (fmt->attributes & 0x02) { data[0] = 1; if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, PITCH_CONTROL << 8, ep, data, 1, HZ)) < 0) { printk(KERN_ERR "usbaudio: failure (error %d) to set output pitch control device %d interface %u endpoint 0x%x to %u\n", ret, dev->devnum, u->interface, ep, d->srate); return -1; } } /* if endpoint has sampling rate control, set it */ if (fmt->attributes & 0x01) { data[0] = d->srate; data[1] = d->srate >> 8; data[2] = d->srate >> 16; if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, SAMPLING_FREQ_CONTROL << 8, ep, data, 3, HZ)) < 0) { printk(KERN_ERR "usbaudio: failure (error %d) to set input sampling frequency device %d interface %u endpoint 0x%x to %u\n", ret, dev->devnum, u->interface, ep, d->srate); return -1; } if ((ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN, SAMPLING_FREQ_CONTROL << 8, ep, data, 3, HZ)) < 0) { printk(KERN_ERR "usbaudio: failure (error %d) to get input sampling frequency device %d interface %u endpoint 0x%x\n", ret, dev->devnum, u->interface, ep); return -1; } dprintk((KERN_DEBUG "usbaudio: set_format_in: device %d interface %d altsetting %d srate req: %u real %u\n", dev->devnum, u->interface, fmt->altsetting, d->srate, data[0] | (data[1] << 8) | (data[2] << 16))); d->srate = data[0] | (data[1] << 8) | (data[2] << 16); } dprintk((KERN_DEBUG "usbaudio: set_format_in: USB format 0x%x, DMA format 0x%x srate %u\n", u->format, d->format, d->srate)); return 0; } static int set_format_out(struct usb_audiodev *as) { struct usb_device *dev = as->state->usbdev; struct usb_host_interface *alts; struct usb_interface *iface; struct usbout *u = &as->usbout; struct dmabuf *d = &u->dma; struct audioformat *fmt; unsigned int ep; unsigned char data[3]; int fmtnr, ret; iface = usb_ifnum_to_if(dev, u->interface); if (!iface) return 0; fmtnr = find_format(as->fmtout, as->numfmtout, d->format, d->srate); if (fmtnr < 0) { printk(KERN_ERR "usbaudio: set_format_out(): failed to find desired format/speed combination.\n"); return -1; } fmt = as->fmtout + fmtnr; u->format = fmt->format; alts = usb_altnum_to_altsetting(iface, fmt->altsetting); u->datapipe = usb_sndisocpipe(dev, alts->endpoint[0].desc.bEndpointAddress & 0xf); u->syncpipe = u->syncinterval = 0; if ((alts->endpoint[0].desc.bmAttributes & 0x0c) == 0x04) { #if 0 printk(KERN_DEBUG "bNumEndpoints 0x%02x endpoint[1].bmAttributes 0x%02x\n" KERN_DEBUG "endpoint[1].bSynchAddress 0x%02x endpoint[1].bEndpointAddress 0x%02x\n" KERN_DEBUG "endpoint[0].bSynchAddress 0x%02x\n", alts->bNumEndpoints, alts->endpoint[1].bmAttributes, alts->endpoint[1].bSynchAddress, alts->endpoint[1].bEndpointAddress, alts->endpoint[0].bSynchAddress); #endif if (alts->desc.bNumEndpoints < 2 || alts->endpoint[1].desc.bmAttributes != 0x01 || alts->endpoint[1].desc.bSynchAddress != 0 || alts->endpoint[1].desc.bEndpointAddress != (alts->endpoint[0].desc.bSynchAddress | 0x80)) { printk(KERN_WARNING "usbaudio: device %d interface %d altsetting %d claims asynch out " "but has invalid synch pipe; treating as adaptive out\n", dev->devnum, u->interface, fmt->altsetting); } else { u->syncpipe = usb_rcvisocpipe(dev, alts->endpoint[1].desc.bEndpointAddress & 0xf); u->syncinterval = alts->endpoint[1].desc.bRefresh; } } if (d->srate < fmt->sratelo) d->srate = fmt->sratelo; if (d->srate > fmt->sratehi) d->srate = fmt->sratehi; dprintk((KERN_DEBUG "usbaudio: set_format_out: usb_set_interface %u %u\n", u->interface, fmt->altsetting)); if (usb_set_interface(dev, u->interface, fmt->altsetting) < 0) { printk(KERN_WARNING "usbaudio: usb_set_interface failed, device %d interface %d altsetting %d\n", dev->devnum, u->interface, fmt->altsetting); return -1; } if (fmt->sratelo == fmt->sratehi) return 0; ep = usb_pipeendpoint(u->datapipe) | (u->datapipe & USB_DIR_IN); /* if endpoint has pitch control, enable it */ if (fmt->attributes & 0x02) { data[0] = 1; if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, PITCH_CONTROL << 8, ep, data, 1, HZ)) < 0) { printk(KERN_ERR "usbaudio: failure (error %d) to set output pitch control device %d interface %u endpoint 0x%x to %u\n", ret, dev->devnum, u->interface, ep, d->srate); return -1; } } /* if endpoint has sampling rate control, set it */ if (fmt->attributes & 0x01) { data[0] = d->srate; data[1] = d->srate >> 8; data[2] = d->srate >> 16; if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, SAMPLING_FREQ_CONTROL << 8, ep, data, 3, HZ)) < 0) { printk(KERN_ERR "usbaudio: failure (error %d) to set output sampling frequency device %d interface %u endpoint 0x%x to %u\n", ret, dev->devnum, u->interface, ep, d->srate); return -1; } if ((ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN, SAMPLING_FREQ_CONTROL << 8, ep, data, 3, HZ)) < 0) { printk(KERN_ERR "usbaudio: failure (error %d) to get output sampling frequency device %d interface %u endpoint 0x%x\n", ret, dev->devnum, u->interface, ep); return -1; } dprintk((KERN_DEBUG "usbaudio: set_format_out: device %d interface %d altsetting %d srate req: %u real %u\n", dev->devnum, u->interface, fmt->altsetting, d->srate, data[0] | (data[1] << 8) | (data[2] << 16))); d->srate = data[0] | (data[1] << 8) | (data[2] << 16); } dprintk((KERN_DEBUG "usbaudio: set_format_out: USB format 0x%x, DMA format 0x%x srate %u\n", u->format, d->format, d->srate)); return 0; } static int set_format(struct usb_audiodev *s, unsigned int fmode, unsigned int fmt, unsigned int srate) { int ret1 = 0, ret2 = 0; if (!(fmode & (FMODE_READ|FMODE_WRITE))) return -EINVAL; if (fmode & FMODE_READ) { usbin_stop(s); s->usbin.dma.ready = 0; if (fmt == AFMT_QUERY) fmt = s->usbin.dma.format; else s->usbin.dma.format = fmt; if (!srate) srate = s->usbin.dma.srate; else s->usbin.dma.srate = srate; } if (fmode & FMODE_WRITE) { usbout_stop(s); s->usbout.dma.ready = 0; if (fmt == AFMT_QUERY) fmt = s->usbout.dma.format; else s->usbout.dma.format = fmt; if (!srate) srate = s->usbout.dma.srate; else s->usbout.dma.srate = srate; } if (fmode & FMODE_READ) ret1 = set_format_in(s); if (fmode & FMODE_WRITE) ret2 = set_format_out(s); return ret1 ? ret1 : ret2; } /* --------------------------------------------------------------------- */ static int wrmixer(struct usb_mixerdev *ms, unsigned mixch, unsigned value) { struct usb_device *dev = ms->state->usbdev; unsigned char data[2]; struct mixerchannel *ch; int v1, v2, v3; if (mixch >= ms->numch) return -1; ch = &ms->ch[mixch]; v3 = ch->maxval - ch->minval; v1 = value & 0xff; v2 = (value >> 8) & 0xff; if (v1 > 100) v1 = 100; if (v2 > 100) v2 = 100; if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT))) v2 = v1; ch->value = v1 | (v2 << 8); v1 = (v1 * v3) / 100 + ch->minval; v2 = (v2 * v3) / 100 + ch->minval; switch (ch->selector) { case 0: /* mixer unit request */ data[0] = v1; data[1] = v1 >> 8; if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, (ch->chnum << 8) | 1, ms->iface | (ch->unitid << 8), data, 2, HZ) < 0) goto err; if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT))) return 0; data[0] = v2; data[1] = v2 >> 8; if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, ((ch->chnum + !!(ch->flags & MIXFLG_STEREOIN)) << 8) | (1 + !!(ch->flags & MIXFLG_STEREOOUT)), ms->iface | (ch->unitid << 8), data, 2, HZ) < 0) goto err; return 0; /* various feature unit controls */ case VOLUME_CONTROL: data[0] = v1; data[1] = v1 >> 8; if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, (ch->selector << 8) | ch->chnum, ms->iface | (ch->unitid << 8), data, 2, HZ) < 0) goto err; if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT))) return 0; data[0] = v2; data[1] = v2 >> 8; if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, (ch->selector << 8) | (ch->chnum + 1), ms->iface | (ch->unitid << 8), data, 2, HZ) < 0) goto err; return 0; case BASS_CONTROL: case MID_CONTROL: case TREBLE_CONTROL: data[0] = v1 >> 8; if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, (ch->selector << 8) | ch->chnum, ms->iface | (ch->unitid << 8), data, 1, HZ) < 0) goto err; if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT))) return 0; data[0] = v2 >> 8; if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, (ch->selector << 8) | (ch->chnum + 1), ms->iface | (ch->unitid << 8), data, 1, HZ) < 0) goto err; return 0; default: return -1; } return 0; err: printk(KERN_ERR "usbaudio: mixer request device %u if %u unit %u ch %u selector %u failed\n", dev->devnum, ms->iface, ch->unitid, ch->chnum, ch->selector); return -1; } static int get_rec_src(struct usb_mixerdev *ms) { struct usb_device *dev = ms->state->usbdev; unsigned int mask = 0, retmask = 0; unsigned int i, j; unsigned char buf; int err = 0; for (i = 0; i < ms->numch; i++) { if (!ms->ch[i].slctunitid || (mask & (1 << i))) continue; if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, 0, ms->iface | (ms->ch[i].slctunitid << 8), &buf, 1, HZ) < 0) { err = -EIO; printk(KERN_ERR "usbaudio: selector read request device %u if %u unit %u failed\n", dev->devnum, ms->iface, ms->ch[i].slctunitid & 0xff); continue; } for (j = i; j < ms->numch; j++) { if ((ms->ch[i].slctunitid ^ ms->ch[j].slctunitid) & 0xff) continue; mask |= 1 << j; if (buf == (ms->ch[j].slctunitid >> 8)) retmask |= 1 << ms->ch[j].osschannel; } } if (err) return -EIO; return retmask; } static int set_rec_src(struct usb_mixerdev *ms, int srcmask) { struct usb_device *dev = ms->state->usbdev; unsigned int mask = 0, smask, bmask; unsigned int i, j; unsigned char buf; int err = 0; for (i = 0; i < ms->numch; i++) { if (!ms->ch[i].slctunitid || (mask & (1 << i))) continue; if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, 0, ms->iface | (ms->ch[i].slctunitid << 8), &buf, 1, HZ) < 0) { err = -EIO; printk(KERN_ERR "usbaudio: selector read request device %u if %u unit %u failed\n", dev->devnum, ms->iface, ms->ch[i].slctunitid & 0xff); continue; } /* first generate smask */ smask = bmask = 0; for (j = i; j < ms->numch; j++) { if ((ms->ch[i].slctunitid ^ ms->ch[j].slctunitid) & 0xff) continue; smask |= 1 << ms->ch[j].osschannel; if (buf == (ms->ch[j].slctunitid >> 8)) bmask |= 1 << ms->ch[j].osschannel; mask |= 1 << j; } /* check for multiple set sources */ j = hweight32(srcmask & smask); if (j == 0) continue; if (j > 1) srcmask &= ~bmask; for (j = i; j < ms->numch; j++) { if ((ms->ch[i].slctunitid ^ ms->ch[j].slctunitid) & 0xff) continue; if (!(srcmask & (1 << ms->ch[j].osschannel))) continue; buf = ms->ch[j].slctunitid >> 8; if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, 0, ms->iface | (ms->ch[j].slctunitid << 8), &buf, 1, HZ) < 0) { err = -EIO; printk(KERN_ERR "usbaudio: selector write request device %u if %u unit %u failed\n", dev->devnum, ms->iface, ms->ch[j].slctunitid & 0xff); continue; } } } return err ? -EIO : 0; } /* --------------------------------------------------------------------- */ /* * should be called with open_sem hold, so that no new processes * look at the audio device to be destroyed */ static void release(struct usb_audio_state *s) { struct usb_audiodev *as; struct usb_mixerdev *ms; s->count--; if (s->count) { up(&open_sem); return; } up(&open_sem); wake_up(&open_wait); while (!list_empty(&s->audiolist)) { as = list_entry(s->audiolist.next, struct usb_audiodev, list); list_del(&as->list); usbin_release(as); usbout_release(as); dmabuf_release(&as->usbin.dma); dmabuf_release(&as->usbout.dma); usb_free_urb(as->usbin.durb[0].urb); usb_free_urb(as->usbin.durb[1].urb); usb_free_urb(as->usbin.surb[0].urb); usb_free_urb(as->usbin.surb[1].urb); usb_free_urb(as->usbout.durb[0].urb); usb_free_urb(as->usbout.durb[1].urb); usb_free_urb(as->usbout.surb[0].urb); usb_free_urb(as->usbout.surb[1].urb); kfree(as); } while (!list_empty(&s->mixerlist)) { ms = list_entry(s->mixerlist.next, struct usb_mixerdev, list); list_del(&ms->list); kfree(ms); } kfree(s); } static inline int prog_dmabuf_in(struct usb_audiodev *as) { usbin_stop(as); return dmabuf_init(&as->usbin.dma); } static inline int prog_dmabuf_out(struct usb_audiodev *as) { usbout_stop(as); return dmabuf_init(&as->usbout.dma); } /* --------------------------------------------------------------------- */ static int usb_audio_open_mixdev(struct inode *inode, struct file *file) { unsigned int minor = iminor(inode); struct list_head *devs, *mdevs; struct usb_mixerdev *ms; struct usb_audio_state *s; down(&open_sem); list_for_each(devs, &audiodevs) { s = list_entry(devs, struct usb_audio_state, audiodev); list_for_each(mdevs, &s->mixerlist) { ms = list_entry(mdevs, struct usb_mixerdev, list); if (ms->dev_mixer == minor) goto mixer_found; } } up(&open_sem); return -ENODEV; mixer_found: if (!s->usbdev) { up(&open_sem); return -EIO; } file->private_data = ms; s->count++; up(&open_sem); return nonseekable_open(inode, file); } static int usb_audio_release_mixdev(struct inode *inode, struct file *file) { struct usb_mixerdev *ms = (struct usb_mixerdev *)file->private_data; struct usb_audio_state *s; lock_kernel(); s = ms->state; down(&open_sem); release(s); unlock_kernel(); return 0; } static int usb_audio_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { struct usb_mixerdev *ms = (struct usb_mixerdev *)file->private_data; int i, j, val; int __user *user_arg = (int __user *)arg; if (!ms->state->usbdev) return -ENODEV; if (cmd == SOUND_MIXER_INFO) { mixer_info info; memset(&info, 0, sizeof(info)); strncpy(info.id, "USB_AUDIO", sizeof(info.id)); strncpy(info.name, "USB Audio Class Driver", sizeof(info.name)); info.modify_counter = ms->modcnt; if (copy_to_user((void __user *)arg, &info, sizeof(info))) return -EFAULT; return 0; } if (cmd == SOUND_OLD_MIXER_INFO) { _old_mixer_info info; memset(&info, 0, sizeof(info)); strncpy(info.id, "USB_AUDIO", sizeof(info.id)); strncpy(info.name, "USB Audio Class Driver", sizeof(info.name)); if (copy_to_user((void __user *)arg, &info, sizeof(info))) return -EFAULT; return 0; } if (cmd == OSS_GETVERSION) return put_user(SOUND_VERSION, user_arg); if (_IOC_TYPE(cmd) != 'M' || _IOC_SIZE(cmd) != sizeof(int)) return -EINVAL; if (_IOC_DIR(cmd) == _IOC_READ) { switch (_IOC_NR(cmd)) { case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */ val = get_rec_src(ms); if (val < 0) return val; return put_user(val, user_arg); case SOUND_MIXER_DEVMASK: /* Arg contains a bit for each supported device */ for (val = i = 0; i < ms->numch; i++) val |= 1 << ms->ch[i].osschannel; return put_user(val, user_arg); case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */ for (val = i = 0; i < ms->numch; i++) if (ms->ch[i].slctunitid) val |= 1 << ms->ch[i].osschannel; return put_user(val, user_arg); case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */ for (val = i = 0; i < ms->numch; i++) if (ms->ch[i].flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)) val |= 1 << ms->ch[i].osschannel; return put_user(val, user_arg); case SOUND_MIXER_CAPS: return put_user(SOUND_CAP_EXCL_INPUT, user_arg); default: i = _IOC_NR(cmd); if (i >= SOUND_MIXER_NRDEVICES) return -EINVAL; for (j = 0; j < ms->numch; j++) { if (ms->ch[j].osschannel == i) { return put_user(ms->ch[j].value, user_arg); } } return -EINVAL; } } if (_IOC_DIR(cmd) != (_IOC_READ|_IOC_WRITE)) return -EINVAL; ms->modcnt++; switch (_IOC_NR(cmd)) { case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */ if (get_user(val, user_arg)) return -EFAULT; return set_rec_src(ms, val); default: i = _IOC_NR(cmd); if (i >= SOUND_MIXER_NRDEVICES) return -EINVAL; for (j = 0; j < ms->numch && ms->ch[j].osschannel != i; j++); if (j >= ms->numch) return -EINVAL; if (get_user(val, user_arg)) return -EFAULT; if (wrmixer(ms, j, val)) return -EIO; return put_user(ms->ch[j].value, user_arg); } } static /*const*/ struct file_operations usb_mixer_fops = { .owner = THIS_MODULE, .llseek = no_llseek, .ioctl = usb_audio_ioctl_mixdev, .open = usb_audio_open_mixdev, .release = usb_audio_release_mixdev, }; /* --------------------------------------------------------------------- */ static int drain_out(struct usb_audiodev *as, int nonblock) { DECLARE_WAITQUEUE(wait, current); unsigned long flags; int count, tmo; if (as->usbout.dma.mapped || !as->usbout.dma.ready) return 0; usbout_start(as); add_wait_queue(&as->usbout.dma.wait, &wait); for (;;) { __set_current_state(TASK_INTERRUPTIBLE); spin_lock_irqsave(&as->lock, flags); count = as->usbout.dma.count; spin_unlock_irqrestore(&as->lock, flags); if (count <= 0) break; if (signal_pending(current)) break; if (nonblock) { remove_wait_queue(&as->usbout.dma.wait, &wait); set_current_state(TASK_RUNNING); return -EBUSY; } tmo = 3 * HZ * count / as->usbout.dma.srate; tmo >>= AFMT_BYTESSHIFT(as->usbout.dma.format); if (!schedule_timeout(tmo + 1)) { printk(KERN_DEBUG "usbaudio: dma timed out??\n"); break; } } remove_wait_queue(&as->usbout.dma.wait, &wait); set_current_state(TASK_RUNNING); if (signal_pending(current)) return -ERESTARTSYS; return 0; } /* --------------------------------------------------------------------- */ static ssize_t usb_audio_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos) { struct usb_audiodev *as = (struct usb_audiodev *)file->private_data; DECLARE_WAITQUEUE(wait, current); ssize_t ret = 0; unsigned long flags; unsigned int ptr; int cnt, err; if (as->usbin.dma.mapped) return -ENXIO; if (!as->usbin.dma.ready && (ret = prog_dmabuf_in(as))) return ret; if (!access_ok(VERIFY_WRITE, buffer, count)) return -EFAULT; add_wait_queue(&as->usbin.dma.wait, &wait); while (count > 0) { spin_lock_irqsave(&as->lock, flags); ptr = as->usbin.dma.rdptr; cnt = as->usbin.dma.count; /* set task state early to avoid wakeup races */ if (cnt <= 0) __set_current_state(TASK_INTERRUPTIBLE); spin_unlock_irqrestore(&as->lock, flags); if (cnt > count) cnt = count; if (cnt <= 0) { if (usbin_start(as)) { if (!ret) ret = -ENODEV; break; } if (file->f_flags & O_NONBLOCK) { if (!ret) ret = -EAGAIN; break; } schedule(); if (signal_pending(current)) { if (!ret) ret = -ERESTARTSYS; break; } continue; } if ((err = dmabuf_copyout_user(&as->usbin.dma, ptr, buffer, cnt))) { if (!ret) ret = err; break; } ptr += cnt; if (ptr >= as->usbin.dma.dmasize) ptr -= as->usbin.dma.dmasize; spin_lock_irqsave(&as->lock, flags); as->usbin.dma.rdptr = ptr; as->usbin.dma.count -= cnt; spin_unlock_irqrestore(&as->lock, flags); count -= cnt; buffer += cnt; ret += cnt; } __set_current_state(TASK_RUNNING); remove_wait_queue(&as->usbin.dma.wait, &wait); return ret; } static ssize_t usb_audio_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) { struct usb_audiodev *as = (struct usb_audiodev *)file->private_data; DECLARE_WAITQUEUE(wait, current); ssize_t ret = 0; unsigned long flags; unsigned int ptr; unsigned int start_thr; int cnt, err; if (as->usbout.dma.mapped) return -ENXIO; if (!as->usbout.dma.ready && (ret = prog_dmabuf_out(as))) return ret; if (!access_ok(VERIFY_READ, buffer, count)) return -EFAULT; start_thr = (as->usbout.dma.srate << AFMT_BYTESSHIFT(as->usbout.dma.format)) / (1000 / (3 * DESCFRAMES)); add_wait_queue(&as->usbout.dma.wait, &wait); while (count > 0) { #if 0 printk(KERN_DEBUG "usb_audio_write: count %u dma: count %u rdptr %u wrptr %u dmasize %u fragsize %u flags 0x%02x taskst 0x%lx\n", count, as->usbout.dma.count, as->usbout.dma.rdptr, as->usbout.dma.wrptr, as->usbout.dma.dmasize, as->usbout.dma.fragsize, as->usbout.flags, current->state); #endif spin_lock_irqsave(&as->lock, flags); if (as->usbout.dma.count < 0) { as->usbout.dma.count = 0; as->usbout.dma.rdptr = as->usbout.dma.wrptr; } ptr = as->usbout.dma.wrptr; cnt = as->usbout.dma.dmasize - as->usbout.dma.count; /* set task state early to avoid wakeup races */ if (cnt <= 0) __set_current_state(TASK_INTERRUPTIBLE); spin_unlock_irqrestore(&as->lock, flags); if (cnt > count) cnt = count; if (cnt <= 0) { if (usbout_start(as)) { if (!ret) ret = -ENODEV; break; } if (file->f_flags & O_NONBLOCK) { if (!ret) ret = -EAGAIN; break; } schedule(); if (signal_pending(current)) { if (!ret) ret = -ERESTARTSYS; break; } continue; } if ((err = dmabuf_copyin_user(&as->usbout.dma, ptr, buffer, cnt))) { if (!ret) ret = err; break; } ptr += cnt; if (ptr >= as->usbout.dma.dmasize) ptr -= as->usbout.dma.dmasize; spin_lock_irqsave(&as->lock, flags); as->usbout.dma.wrptr = ptr; as->usbout.dma.count += cnt; spin_unlock_irqrestore(&as->lock, flags); count -= cnt; buffer += cnt; ret += cnt; if (as->usbout.dma.count >= start_thr && usbout_start(as)) { if (!ret) ret = -ENODEV; break; } } __set_current_state(TASK_RUNNING); remove_wait_queue(&as->usbout.dma.wait, &wait); return ret; } /* Called without the kernel lock - fine */ static unsigned int usb_audio_poll(struct file *file, struct poll_table_struct *wait) { struct usb_audiodev *as = (struct usb_audiodev *)file->private_data; unsigned long flags; unsigned int mask = 0; if (file->f_mode & FMODE_WRITE) { if (!as->usbout.dma.ready) prog_dmabuf_out(as); poll_wait(file, &as->usbout.dma.wait, wait); } if (file->f_mode & FMODE_READ) { if (!as->usbin.dma.ready) prog_dmabuf_in(as); poll_wait(file, &as->usbin.dma.wait, wait); } spin_lock_irqsave(&as->lock, flags); if (file->f_mode & FMODE_READ) { if (as->usbin.dma.count >= (signed)as->usbin.dma.fragsize) mask |= POLLIN | POLLRDNORM; } if (file->f_mode & FMODE_WRITE) { if (as->usbout.dma.mapped) { if (as->usbout.dma.count >= (signed)as->usbout.dma.fragsize) mask |= POLLOUT | POLLWRNORM; } else { if ((signed)as->usbout.dma.dmasize >= as->usbout.dma.count + (signed)as->usbout.dma.fragsize) mask |= POLLOUT | POLLWRNORM; } } spin_unlock_irqrestore(&as->lock, flags); return mask; } static int usb_audio_mmap(struct file *file, struct vm_area_struct *vma) { struct usb_audiodev *as = (struct usb_audiodev *)file->private_data; struct dmabuf *db; int ret = -EINVAL; lock_kernel(); if (vma->vm_flags & VM_WRITE) { if ((ret = prog_dmabuf_out(as)) != 0) goto out; db = &as->usbout.dma; } else if (vma->vm_flags & VM_READ) { if ((ret = prog_dmabuf_in(as)) != 0) goto out; db = &as->usbin.dma; } else goto out; ret = -EINVAL; if (vma->vm_pgoff != 0) goto out; ret = dmabuf_mmap(vma, db, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot); out: unlock_kernel(); return ret; } static int usb_audio_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { struct usb_audiodev *as = (struct usb_audiodev *)file->private_data; struct usb_audio_state *s = as->state; int __user *user_arg = (int __user *)arg; unsigned long flags; audio_buf_info abinfo; count_info cinfo; int val = 0; int val2, mapped, ret; if (!s->usbdev) return -EIO; mapped = ((file->f_mode & FMODE_WRITE) && as->usbout.dma.mapped) || ((file->f_mode & FMODE_READ) && as->usbin.dma.mapped); #if 0 if (arg) get_user(val, (int *)arg); printk(KERN_DEBUG "usbaudio: usb_audio_ioctl cmd=%x arg=%lx *arg=%d\n", cmd, arg, val) #endif switch (cmd) { case OSS_GETVERSION: return put_user(SOUND_VERSION, user_arg); case SNDCTL_DSP_SYNC: if (file->f_mode & FMODE_WRITE) return drain_out(as, 0/*file->f_flags & O_NONBLOCK*/); return 0; case SNDCTL_DSP_SETDUPLEX: return 0; case SNDCTL_DSP_GETCAPS: return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME | DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_BATCH, user_arg); case SNDCTL_DSP_RESET: if (file->f_mode & FMODE_WRITE) { usbout_stop(as); as->usbout.dma.rdptr = as->usbout.dma.wrptr = as->usbout.dma.count = as->usbout.dma.total_bytes = 0; } if (file->f_mode & FMODE_READ) { usbin_stop(as); as->usbin.dma.rdptr = as->usbin.dma.wrptr = as->usbin.dma.count = as->usbin.dma.total_bytes = 0; } return 0; case SNDCTL_DSP_SPEED: if (get_user(val, user_arg)) return -EFAULT; if (val >= 0) { if (val < 4000) val = 4000; if (val > 100000) val = 100000; if (set_format(as, file->f_mode, AFMT_QUERY, val)) return -EIO; } return put_user((file->f_mode & FMODE_READ) ? as->usbin.dma.srate : as->usbout.dma.srate, user_arg); case SNDCTL_DSP_STEREO: if (get_user(val, user_arg)) return -EFAULT; val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format; if (val) val2 |= AFMT_STEREO; else val2 &= ~AFMT_STEREO; if (set_format(as, file->f_mode, val2, 0)) return -EIO; return 0; case SNDCTL_DSP_CHANNELS: if (get_user(val, user_arg)) return -EFAULT; if (val != 0) { val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format; if (val == 1) val2 &= ~AFMT_STEREO; else val2 |= AFMT_STEREO; if (set_format(as, file->f_mode, val2, 0)) return -EIO; } val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format; return put_user(AFMT_ISSTEREO(val2) ? 2 : 1, user_arg); case SNDCTL_DSP_GETFMTS: /* Returns a mask */ return put_user(AFMT_U8 | AFMT_U16_LE | AFMT_U16_BE | AFMT_S8 | AFMT_S16_LE | AFMT_S16_BE, user_arg); case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/ if (get_user(val, user_arg)) return -EFAULT; if (val != AFMT_QUERY) { if (hweight32(val) != 1) return -EINVAL; if (!(val & (AFMT_U8 | AFMT_U16_LE | AFMT_U16_BE | AFMT_S8 | AFMT_S16_LE | AFMT_S16_BE))) return -EINVAL; val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format; val |= val2 & AFMT_STEREO; if (set_format(as, file->f_mode, val, 0)) return -EIO; } val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format; return put_user(val2 & ~AFMT_STEREO, user_arg); case SNDCTL_DSP_POST: return 0; case SNDCTL_DSP_GETTRIGGER: val = 0; if (file->f_mode & FMODE_READ && as->usbin.flags & FLG_RUNNING) val |= PCM_ENABLE_INPUT; if (file->f_mode & FMODE_WRITE && as->usbout.flags & FLG_RUNNING) val |= PCM_ENABLE_OUTPUT; return put_user(val, user_arg); case SNDCTL_DSP_SETTRIGGER: if (get_user(val, user_arg)) return -EFAULT; if (file->f_mode & FMODE_READ) { if (val & PCM_ENABLE_INPUT) { if (!as->usbin.dma.ready && (ret = prog_dmabuf_in(as))) return ret; if (usbin_start(as)) return -ENODEV; } else usbin_stop(as); } if (file->f_mode & FMODE_WRITE) { if (val & PCM_ENABLE_OUTPUT) { if (!as->usbout.dma.ready && (ret = prog_dmabuf_out(as))) return ret; if (usbout_start(as)) return -ENODEV; } else usbout_stop(as); } return 0; case SNDCTL_DSP_GETOSPACE: if (!(file->f_mode & FMODE_WRITE)) return -EINVAL; if (!(as->usbout.flags & FLG_RUNNING) && (val = prog_dmabuf_out(as)) != 0) return val; spin_lock_irqsave(&as->lock, flags); abinfo.fragsize = as->usbout.dma.fragsize; abinfo.bytes = as->usbout.dma.dmasize - as->usbout.dma.count; abinfo.fragstotal = as->usbout.dma.numfrag; abinfo.fragments = abinfo.bytes >> as->usbout.dma.fragshift; spin_unlock_irqrestore(&as->lock, flags); return copy_to_user((void __user *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0; case SNDCTL_DSP_GETISPACE: if (!(file->f_mode & FMODE_READ)) return -EINVAL; if (!(as->usbin.flags & FLG_RUNNING) && (val = prog_dmabuf_in(as)) != 0) return val; spin_lock_irqsave(&as->lock, flags); abinfo.fragsize = as->usbin.dma.fragsize; abinfo.bytes = as->usbin.dma.count; abinfo.fragstotal = as->usbin.dma.numfrag; abinfo.fragments = abinfo.bytes >> as->usbin.dma.fragshift; spin_unlock_irqrestore(&as->lock, flags); return copy_to_user((void __user *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0; case SNDCTL_DSP_NONBLOCK: file->f_flags |= O_NONBLOCK; return 0; case SNDCTL_DSP_GETODELAY: if (!(file->f_mode & FMODE_WRITE)) return -EINVAL; spin_lock_irqsave(&as->lock, flags); val = as->usbout.dma.count; spin_unlock_irqrestore(&as->lock, flags); return put_user(val, user_arg); case SNDCTL_DSP_GETIPTR: if (!(file->f_mode & FMODE_READ)) return -EINVAL; spin_lock_irqsave(&as->lock, flags); cinfo.bytes = as->usbin.dma.total_bytes; cinfo.blocks = as->usbin.dma.count >> as->usbin.dma.fragshift; cinfo.ptr = as->usbin.dma.wrptr; if (as->usbin.dma.mapped) as->usbin.dma.count &= as->usbin.dma.fragsize-1; spin_unlock_irqrestore(&as->lock, flags); if (copy_to_user((void __user *)arg, &cinfo, sizeof(cinfo))) return -EFAULT; return 0; case SNDCTL_DSP_GETOPTR: if (!(file->f_mode & FMODE_WRITE)) return -EINVAL; spin_lock_irqsave(&as->lock, flags); cinfo.bytes = as->usbout.dma.total_bytes; cinfo.blocks = as->usbout.dma.count >> as->usbout.dma.fragshift; cinfo.ptr = as->usbout.dma.rdptr; if (as->usbout.dma.mapped) as->usbout.dma.count &= as->usbout.dma.fragsize-1; spin_unlock_irqrestore(&as->lock, flags); if (copy_to_user((void __user *)arg, &cinfo, sizeof(cinfo))) return -EFAULT; return 0; case SNDCTL_DSP_GETBLKSIZE: if (file->f_mode & FMODE_WRITE) { if ((val = prog_dmabuf_out(as))) return val; return put_user(as->usbout.dma.fragsize, user_arg); } if ((val = prog_dmabuf_in(as))) return val; return put_user(as->usbin.dma.fragsize, user_arg); case SNDCTL_DSP_SETFRAGMENT: if (get_user(val, user_arg)) return -EFAULT; if (file->f_mode & FMODE_READ) { as->usbin.dma.ossfragshift = val & 0xffff; as->usbin.dma.ossmaxfrags = (val >> 16) & 0xffff; if (as->usbin.dma.ossfragshift < 4) as->usbin.dma.ossfragshift = 4; if (as->usbin.dma.ossfragshift > 15) as->usbin.dma.ossfragshift = 15; if (as->usbin.dma.ossmaxfrags < 4) as->usbin.dma.ossmaxfrags = 4; } if (file->f_mode & FMODE_WRITE) { as->usbout.dma.ossfragshift = val & 0xffff; as->usbout.dma.ossmaxfrags = (val >> 16) & 0xffff; if (as->usbout.dma.ossfragshift < 4) as->usbout.dma.ossfragshift = 4; if (as->usbout.dma.ossfragshift > 15) as->usbout.dma.ossfragshift = 15; if (as->usbout.dma.ossmaxfrags < 4) as->usbout.dma.ossmaxfrags = 4; } return 0; case SNDCTL_DSP_SUBDIVIDE: if ((file->f_mode & FMODE_READ && as->usbin.dma.subdivision) || (file->f_mode & FMODE_WRITE && as->usbout.dma.subdivision)) return -EINVAL; if (get_user(val, user_arg)) return -EFAULT; if (val != 1 && val != 2 && val != 4) return -EINVAL; if (file->f_mode & FMODE_READ) as->usbin.dma.subdivision = val; if (file->f_mode & FMODE_WRITE) as->usbout.dma.subdivision = val; return 0; case SOUND_PCM_READ_RATE: return put_user((file->f_mode & FMODE_READ) ? as->usbin.dma.srate : as->usbout.dma.srate, user_arg); case SOUND_PCM_READ_CHANNELS: val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format; return put_user(AFMT_ISSTEREO(val2) ? 2 : 1, user_arg); case SOUND_PCM_READ_BITS: val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format; return put_user(AFMT_IS16BIT(val2) ? 16 : 8, user_arg); case SOUND_PCM_WRITE_FILTER: case SNDCTL_DSP_SETSYNCRO: case SOUND_PCM_READ_FILTER: return -EINVAL; } dprintk((KERN_DEBUG "usbaudio: usb_audio_ioctl - no command found\n")); return -ENOIOCTLCMD; } static int usb_audio_open(struct inode *inode, struct file *file) { unsigned int minor = iminor(inode); DECLARE_WAITQUEUE(wait, current); struct list_head *devs, *adevs; struct usb_audiodev *as; struct usb_audio_state *s; for (;;) { down(&open_sem); list_for_each(devs, &audiodevs) { s = list_entry(devs, struct usb_audio_state, audiodev); list_for_each(adevs, &s->audiolist) { as = list_entry(adevs, struct usb_audiodev, list); if (!((as->dev_audio ^ minor) & ~0xf)) goto device_found; } } up(&open_sem); return -ENODEV; device_found: if (!s->usbdev) { up(&open_sem); return -EIO; } /* wait for device to become free */ if (!(as->open_mode & file->f_mode)) break; if (file->f_flags & O_NONBLOCK) { up(&open_sem); return -EBUSY; } __set_current_state(TASK_INTERRUPTIBLE); add_wait_queue(&open_wait, &wait); up(&open_sem); schedule(); __set_current_state(TASK_RUNNING); remove_wait_queue(&open_wait, &wait); if (signal_pending(current)) return -ERESTARTSYS; } if (file->f_mode & FMODE_READ) as->usbin.dma.ossfragshift = as->usbin.dma.ossmaxfrags = as->usbin.dma.subdivision = 0; if (file->f_mode & FMODE_WRITE) as->usbout.dma.ossfragshift = as->usbout.dma.ossmaxfrags = as->usbout.dma.subdivision = 0; if (set_format(as, file->f_mode, ((minor & 0xf) == SND_DEV_DSP16) ? AFMT_S16_LE : AFMT_U8 /* AFMT_ULAW */, 8000)) { up(&open_sem); return -EIO; } file->private_data = as; as->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE); s->count++; up(&open_sem); return nonseekable_open(inode, file); } static int usb_audio_release(struct inode *inode, struct file *file) { struct usb_audiodev *as = (struct usb_audiodev *)file->private_data; struct usb_audio_state *s; struct usb_device *dev; lock_kernel(); s = as->state; dev = s->usbdev; if (file->f_mode & FMODE_WRITE) drain_out(as, file->f_flags & O_NONBLOCK); down(&open_sem); if (file->f_mode & FMODE_WRITE) { usbout_stop(as); if (dev && as->usbout.interface >= 0) usb_set_interface(dev, as->usbout.interface, 0); dmabuf_release(&as->usbout.dma); usbout_release(as); } if (file->f_mode & FMODE_READ) { usbin_stop(as); if (dev && as->usbin.interface >= 0) usb_set_interface(dev, as->usbin.interface, 0); dmabuf_release(&as->usbin.dma); usbin_release(as); } as->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE); release(s); wake_up(&open_wait); unlock_kernel(); return 0; } static /*const*/ struct file_operations usb_audio_fops = { .owner = THIS_MODULE, .llseek = no_llseek, .read = usb_audio_read, .write = usb_audio_write, .poll = usb_audio_poll, .ioctl = usb_audio_ioctl, .mmap = usb_audio_mmap, .open = usb_audio_open, .release = usb_audio_release, }; /* --------------------------------------------------------------------- */ static int usb_audio_probe(struct usb_interface *iface, const struct usb_device_id *id); static void usb_audio_disconnect(struct usb_interface *iface); static struct usb_device_id usb_audio_ids [] = { { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS), .bInterfaceClass = USB_CLASS_AUDIO, .bInterfaceSubClass = 1}, { } /* Terminating entry */ }; MODULE_DEVICE_TABLE (usb, usb_audio_ids); static struct usb_driver usb_audio_driver = { .owner = THIS_MODULE, .name = "audio", .probe = usb_audio_probe, .disconnect = usb_audio_disconnect, .id_table = usb_audio_ids, }; static void *find_descriptor(void *descstart, unsigned int desclen, void *after, u8 dtype, int iface, int altsetting) { u8 *p, *end, *next; int ifc = -1, as = -1; p = descstart; end = p + desclen; for (; p < end;) { if (p[0] < 2) return NULL; next = p + p[0]; if (next > end) return NULL; if (p[1] == USB_DT_INTERFACE) { /* minimum length of interface descriptor */ if (p[0] < 9) return NULL; ifc = p[2]; as = p[3]; } if (p[1] == dtype && (!after || (void *)p > after) && (iface == -1 || iface == ifc) && (altsetting == -1 || altsetting == as)) { return p; } p = next; } return NULL; } static void *find_csinterface_descriptor(void *descstart, unsigned int desclen, void *after, u8 dsubtype, int iface, int altsetting) { unsigned char *p; p = find_descriptor(descstart, desclen, after, USB_DT_CS_INTERFACE, iface, altsetting); while (p) { if (p[0] >= 3 && p[2] == dsubtype) return p; p = find_descriptor(descstart, desclen, p, USB_DT_CS_INTERFACE, iface, altsetting); } return NULL; } static void *find_audiocontrol_unit(void *descstart, unsigned int desclen, void *after, u8 unit, int iface) { unsigned char *p; p = find_descriptor(descstart, desclen, after, USB_DT_CS_INTERFACE, iface, -1); while (p) { if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit) return p; p = find_descriptor(descstart, desclen, p, USB_DT_CS_INTERFACE, iface, -1); } return NULL; } static void usb_audio_parsestreaming(struct usb_audio_state *s, unsigned char *buffer, unsigned int buflen, int asifin, int asifout) { struct usb_device *dev = s->usbdev; struct usb_audiodev *as; struct usb_host_interface *alts; struct usb_interface *iface; struct audioformat *fp; unsigned char *fmt, *csep; unsigned int i, j, k, format, idx; if (!(as = kmalloc(sizeof(struct usb_audiodev), GFP_KERNEL))) return; memset(as, 0, sizeof(struct usb_audiodev)); init_waitqueue_head(&as->usbin.dma.wait); init_waitqueue_head(&as->usbout.dma.wait); spin_lock_init(&as->lock); as->usbin.durb[0].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL); as->usbin.durb[1].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL); as->usbin.surb[0].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL); as->usbin.surb[1].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL); as->usbout.durb[0].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL); as->usbout.durb[1].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL); as->usbout.surb[0].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL); as->usbout.surb[1].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL); if ((!as->usbin.durb[0].urb) || (!as->usbin.durb[1].urb) || (!as->usbin.surb[0].urb) || (!as->usbin.surb[1].urb) || (!as->usbout.durb[0].urb) || (!as->usbout.durb[1].urb) || (!as->usbout.surb[0].urb) || (!as->usbout.surb[1].urb)) { usb_free_urb(as->usbin.durb[0].urb); usb_free_urb(as->usbin.durb[1].urb); usb_free_urb(as->usbin.surb[0].urb); usb_free_urb(as->usbin.surb[1].urb); usb_free_urb(as->usbout.durb[0].urb); usb_free_urb(as->usbout.durb[1].urb); usb_free_urb(as->usbout.surb[0].urb); usb_free_urb(as->usbout.surb[1].urb); kfree(as); return; } as->state = s; as->usbin.interface = asifin; as->usbout.interface = asifout; /* search for input formats */ if (asifin >= 0) { as->usbin.flags = FLG_CONNECTED; iface = usb_ifnum_to_if(dev, asifin); for (idx = 0; idx < iface->num_altsetting; idx++) { alts = &iface->altsetting[idx]; i = alts->desc.bAlternateSetting; if (alts->desc.bInterfaceClass != USB_CLASS_AUDIO || alts->desc.bInterfaceSubClass != 2) continue; if (alts->desc.bNumEndpoints < 1) { if (i != 0) { /* altsetting 0 has no endpoints (Section B.3.4.1) */ printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u does not have an endpoint\n", dev->devnum, asifin, i); } continue; } if ((alts->endpoint[0].desc.bmAttributes & 0x03) != 0x01 || !(alts->endpoint[0].desc.bEndpointAddress & 0x80)) { printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u first endpoint not isochronous in\n", dev->devnum, asifin, i); continue; } fmt = find_csinterface_descriptor(buffer, buflen, NULL, AS_GENERAL, asifin, i); if (!fmt) { printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n", dev->devnum, asifin, i); continue; } if (fmt[0] < 7 || fmt[6] != 0 || (fmt[5] != 1 && fmt[5] != 2)) { printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u format not supported\n", dev->devnum, asifin, i); continue; } format = (fmt[5] == 2) ? (AFMT_U16_LE | AFMT_U8) : (AFMT_S16_LE | AFMT_S8); fmt = find_csinterface_descriptor(buffer, buflen, NULL, FORMAT_TYPE, asifin, i); if (!fmt) { printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n", dev->devnum, asifin, i); continue; } if (fmt[0] < 8+3*(fmt[7] ? fmt[7] : 2) || fmt[3] != 1) { printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not supported\n", dev->devnum, asifin, i); continue; } if (fmt[4] < 1 || fmt[4] > 2 || fmt[5] < 1 || fmt[5] > 2) { printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u unsupported channels %u framesize %u\n", dev->devnum, asifin, i, fmt[4], fmt[5]); continue; } csep = find_descriptor(buffer, buflen, NULL, USB_DT_CS_ENDPOINT, asifin, i); if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) { printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u no or invalid class specific endpoint descriptor\n", dev->devnum, asifin, i); continue; } if (as->numfmtin >= MAXFORMATS) continue; fp = &as->fmtin[as->numfmtin++]; if (fmt[5] == 2) format &= (AFMT_U16_LE | AFMT_S16_LE); else format &= (AFMT_U8 | AFMT_S8); if (fmt[4] == 2) format |= AFMT_STEREO; fp->format = format; fp->altsetting = i; fp->sratelo = fp->sratehi = fmt[8] | (fmt[9] << 8) | (fmt[10] << 16); printk(KERN_INFO "usbaudio: valid input sample rate %u\n", fp->sratelo); for (j = fmt[7] ? (fmt[7]-1) : 1; j > 0; j--) { k = fmt[8+3*j] | (fmt[9+3*j] << 8) | (fmt[10+3*j] << 16); printk(KERN_INFO "usbaudio: valid input sample rate %u\n", k); if (k > fp->sratehi) fp->sratehi = k; if (k < fp->sratelo) fp->sratelo = k; } fp->attributes = csep[3]; printk(KERN_INFO "usbaudio: device %u interface %u altsetting %u: format 0x%08x sratelo %u sratehi %u attributes 0x%02x\n", dev->devnum, asifin, i, fp->format, fp->sratelo, fp->sratehi, fp->attributes); } } /* search for output formats */ if (asifout >= 0) { as->usbout.flags = FLG_CONNECTED; iface = usb_ifnum_to_if(dev, asifout); for (idx = 0; idx < iface->num_altsetting; idx++) { alts = &iface->altsetting[idx]; i = alts->desc.bAlternateSetting; if (alts->desc.bInterfaceClass != USB_CLASS_AUDIO || alts->desc.bInterfaceSubClass != 2) continue; if (alts->desc.bNumEndpoints < 1) { /* altsetting 0 should never have iso EPs */ if (i != 0) printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u does not have an endpoint\n", dev->devnum, asifout, i); continue; } if ((alts->endpoint[0].desc.bmAttributes & 0x03) != 0x01 || (alts->endpoint[0].desc.bEndpointAddress & 0x80)) { printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u first endpoint not isochronous out\n", dev->devnum, asifout, i); continue; } /* See USB audio formats manual, section 2 */ fmt = find_csinterface_descriptor(buffer, buflen, NULL, AS_GENERAL, asifout, i); if (!fmt) { printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n", dev->devnum, asifout, i); continue; } if (fmt[0] < 7 || fmt[6] != 0 || (fmt[5] != 1 && fmt[5] != 2)) { printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u format not supported\n", dev->devnum, asifout, i); continue; } format = (fmt[5] == 2) ? (AFMT_U16_LE | AFMT_U8) : (AFMT_S16_LE | AFMT_S8); /* Dallas DS4201 workaround */ if (dev->descriptor.idVendor == 0x04fa && dev->descriptor.idProduct == 0x4201) format = (AFMT_S16_LE | AFMT_S8); fmt = find_csinterface_descriptor(buffer, buflen, NULL, FORMAT_TYPE, asifout, i); if (!fmt) { printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n", dev->devnum, asifout, i); continue; } if (fmt[0] < 8+3*(fmt[7] ? fmt[7] : 2) || fmt[3] != 1) { printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not supported\n", dev->devnum, asifout, i); continue; } if (fmt[4] < 1 || fmt[4] > 2 || fmt[5] < 1 || fmt[5] > 2) { printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u unsupported channels %u framesize %u\n", dev->devnum, asifout, i, fmt[4], fmt[5]); continue; } csep = find_descriptor(buffer, buflen, NULL, USB_DT_CS_ENDPOINT, asifout, i); if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) { printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u no or invalid class specific endpoint descriptor\n", dev->devnum, asifout, i); continue; } if (as->numfmtout >= MAXFORMATS) continue; fp = &as->fmtout[as->numfmtout++]; if (fmt[5] == 2) format &= (AFMT_U16_LE | AFMT_S16_LE); else format &= (AFMT_U8 | AFMT_S8); if (fmt[4] == 2) format |= AFMT_STEREO; fp->format = format; fp->altsetting = i; fp->sratelo = fp->sratehi = fmt[8] | (fmt[9] << 8) | (fmt[10] << 16); printk(KERN_INFO "usbaudio: valid output sample rate %u\n", fp->sratelo); for (j = fmt[7] ? (fmt[7]-1) : 1; j > 0; j--) { k = fmt[8+3*j] | (fmt[9+3*j] << 8) | (fmt[10+3*j] << 16); printk(KERN_INFO "usbaudio: valid output sample rate %u\n", k); if (k > fp->sratehi) fp->sratehi = k; if (k < fp->sratelo) fp->sratelo = k; } fp->attributes = csep[3]; printk(KERN_INFO "usbaudio: device %u interface %u altsetting %u: format 0x%08x sratelo %u sratehi %u attributes 0x%02x\n", dev->devnum, asifout, i, fp->format, fp->sratelo, fp->sratehi, fp->attributes); } } if (as->numfmtin == 0 && as->numfmtout == 0) { usb_free_urb(as->usbin.durb[0].urb); usb_free_urb(as->usbin.durb[1].urb); usb_free_urb(as->usbin.surb[0].urb); usb_free_urb(as->usbin.surb[1].urb); usb_free_urb(as->usbout.durb[0].urb); usb_free_urb(as->usbout.durb[1].urb); usb_free_urb(as->usbout.surb[0].urb); usb_free_urb(as->usbout.surb[1].urb); kfree(as); return; } if ((as->dev_audio = register_sound_dsp(&usb_audio_fops, -1)) < 0) { printk(KERN_ERR "usbaudio: cannot register dsp\n"); usb_free_urb(as->usbin.durb[0].urb); usb_free_urb(as->usbin.durb[1].urb); usb_free_urb(as->usbin.surb[0].urb); usb_free_urb(as->usbin.surb[1].urb); usb_free_urb(as->usbout.durb[0].urb); usb_free_urb(as->usbout.durb[1].urb); usb_free_urb(as->usbout.surb[0].urb); usb_free_urb(as->usbout.surb[1].urb); kfree(as); return; } printk(KERN_INFO "usbaudio: registered dsp 14,%d\n", as->dev_audio); /* everything successful */ list_add_tail(&as->list, &s->audiolist); } struct consmixstate { struct usb_audio_state *s; unsigned char *buffer; unsigned int buflen; unsigned int ctrlif; struct mixerchannel mixch[SOUND_MIXER_NRDEVICES]; unsigned int nrmixch; unsigned int mixchmask; unsigned long unitbitmap[32/sizeof(unsigned long)]; /* return values */ unsigned int nrchannels; unsigned int termtype; unsigned int chconfig; }; static struct mixerchannel *getmixchannel(struct consmixstate *state, unsigned int nr) { struct mixerchannel *c; if (nr >= SOUND_MIXER_NRDEVICES) { printk(KERN_ERR "usbaudio: invalid OSS mixer channel %u\n", nr); return NULL; } if (!(state->mixchmask & (1 << nr))) { printk(KERN_WARNING "usbaudio: OSS mixer channel %u already in use\n", nr); return NULL; } c = &state->mixch[state->nrmixch++]; c->osschannel = nr; state->mixchmask &= ~(1 << nr); return c; } static unsigned int getvolchannel(struct consmixstate *state) { unsigned int u; if ((state->termtype & 0xff00) == 0x0000 && (state->mixchmask & SOUND_MASK_VOLUME)) return SOUND_MIXER_VOLUME; if ((state->termtype & 0xff00) == 0x0100) { if (state->mixchmask & SOUND_MASK_PCM) return SOUND_MIXER_PCM; if (state->mixchmask & SOUND_MASK_ALTPCM) return SOUND_MIXER_ALTPCM; } if ((state->termtype & 0xff00) == 0x0200 && (state->mixchmask & SOUND_MASK_MIC)) return SOUND_MIXER_MIC; if ((state->termtype & 0xff00) == 0x0300 && (state->mixchmask & SOUND_MASK_SPEAKER)) return SOUND_MIXER_SPEAKER; if ((state->termtype & 0xff00) == 0x0500) { if (state->mixchmask & SOUND_MASK_PHONEIN) return SOUND_MIXER_PHONEIN; if (state->mixchmask & SOUND_MASK_PHONEOUT) return SOUND_MIXER_PHONEOUT; } if (state->termtype >= 0x710 && state->termtype <= 0x711 && (state->mixchmask & SOUND_MASK_RADIO)) return SOUND_MIXER_RADIO; if (state->termtype >= 0x709 && state->termtype <= 0x70f && (state->mixchmask & SOUND_MASK_VIDEO)) return SOUND_MIXER_VIDEO; u = ffs(state->mixchmask & (SOUND_MASK_LINE | SOUND_MASK_CD | SOUND_MASK_LINE1 | SOUND_MASK_LINE2 | SOUND_MASK_LINE3 | SOUND_MASK_DIGITAL1 | SOUND_MASK_DIGITAL2 | SOUND_MASK_DIGITAL3)); return u-1; } static void prepmixch(struct consmixstate *state) { struct usb_device *dev = state->s->usbdev; struct mixerchannel *ch; unsigned char buf[2]; __s16 v1; unsigned int v2, v3; if (!state->nrmixch || state->nrmixch > SOUND_MIXER_NRDEVICES) return; ch = &state->mixch[state->nrmixch-1]; switch (ch->selector) { case 0: /* mixer unit request */ if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MIN, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, (ch->chnum << 8) | 1, state->ctrlif | (ch->unitid << 8), buf, 2, HZ) < 0) goto err; ch->minval = buf[0] | (buf[1] << 8); if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MAX, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, (ch->chnum << 8) | 1, state->ctrlif | (ch->unitid << 8), buf, 2, HZ) < 0) goto err; ch->maxval = buf[0] | (buf[1] << 8); v2 = ch->maxval - ch->minval; if (!v2) v2 = 1; if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, (ch->chnum << 8) | 1, state->ctrlif | (ch->unitid << 8), buf, 2, HZ) < 0) goto err; v1 = buf[0] | (buf[1] << 8); v3 = v1 - ch->minval; v3 = 100 * v3 / v2; if (v3 > 100) v3 = 100; ch->value = v3; if (ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)) { if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, ((ch->chnum + !!(ch->flags & MIXFLG_STEREOIN)) << 8) | (1 + !!(ch->flags & MIXFLG_STEREOOUT)), state->ctrlif | (ch->unitid << 8), buf, 2, HZ) < 0) goto err; v1 = buf[0] | (buf[1] << 8); v3 = v1 - ch->minval; v3 = 100 * v3 / v2; if (v3 > 100) v3 = 100; } ch->value |= v3 << 8; break; /* various feature unit controls */ case VOLUME_CONTROL: if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MIN, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 2, HZ) < 0) goto err; ch->minval = buf[0] | (buf[1] << 8); if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MAX, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 2, HZ) < 0) goto err; ch->maxval = buf[0] | (buf[1] << 8); if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 2, HZ) < 0) goto err; v1 = buf[0] | (buf[1] << 8); v2 = ch->maxval - ch->minval; v3 = v1 - ch->minval; if (!v2) v2 = 1; v3 = 100 * v3 / v2; if (v3 > 100) v3 = 100; ch->value = v3; if (ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)) { if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, (ch->selector << 8) | (ch->chnum + 1), state->ctrlif | (ch->unitid << 8), buf, 2, HZ) < 0) goto err; v1 = buf[0] | (buf[1] << 8); v3 = v1 - ch->minval; v3 = 100 * v3 / v2; if (v3 > 100) v3 = 100; } ch->value |= v3 << 8; break; case BASS_CONTROL: case MID_CONTROL: case TREBLE_CONTROL: if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MIN, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 1, HZ) < 0) goto err; ch->minval = buf[0] << 8; if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MAX, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 1, HZ) < 0) goto err; ch->maxval = buf[0] << 8; if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 1, HZ) < 0) goto err; v1 = buf[0] << 8; v2 = ch->maxval - ch->minval; v3 = v1 - ch->minval; if (!v2) v2 = 1; v3 = 100 * v3 / v2; if (v3 > 100) v3 = 100; ch->value = v3; if (ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)) { if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, (ch->selector << 8) | (ch->chnum + 1), state->ctrlif | (ch->unitid << 8), buf, 1, HZ) < 0) goto err; v1 = buf[0] << 8; v3 = v1 - ch->minval; v3 = 100 * v3 / v2; if (v3 > 100) v3 = 100; } ch->value |= v3 << 8; break; default: goto err; } return; err: printk(KERN_ERR "usbaudio: mixer request device %u if %u unit %u ch %u selector %u failed\n", dev->devnum, state->ctrlif, ch->unitid, ch->chnum, ch->selector); if (state->nrmixch) state->nrmixch--; } static void usb_audio_recurseunit(struct consmixstate *state, unsigned char unitid); static inline int checkmixbmap(unsigned char *bmap, unsigned char flg, unsigned int inidx, unsigned int numoch) { unsigned int idx; idx = inidx*numoch; if (!(bmap[-(idx >> 3)] & (0x80 >> (idx & 7)))) return 0; if (!(flg & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT))) return 1; idx = (inidx+!!(flg & MIXFLG_STEREOIN))*numoch+!!(flg & MIXFLG_STEREOOUT); if (!(bmap[-(idx >> 3)] & (0x80 >> (idx & 7)))) return 0; return 1; } static void usb_audio_mixerunit(struct consmixstate *state, unsigned char *mixer) { unsigned int nroutch = mixer[5+mixer[4]]; unsigned int chidx[SOUND_MIXER_NRDEVICES+1]; unsigned int termt[SOUND_MIXER_NRDEVICES]; unsigned char flg = (nroutch >= 2) ? MIXFLG_STEREOOUT : 0; unsigned char *bmap = &mixer[9+mixer[4]]; unsigned int bmapsize; struct mixerchannel *ch; unsigned int i; if (!mixer[4]) { printk(KERN_ERR "usbaudio: unit %u invalid MIXER_UNIT descriptor\n", mixer[3]); return; } if (mixer[4] > SOUND_MIXER_NRDEVICES) { printk(KERN_ERR "usbaudio: mixer unit %u: too many input pins\n", mixer[3]); return; } chidx[0] = 0; for (i = 0; i < mixer[4]; i++) { usb_audio_recurseunit(state, mixer[5+i]); chidx[i+1] = chidx[i] + state->nrchannels; termt[i] = state->termtype; } state->termtype = 0; state->chconfig = mixer[6+mixer[4]] | (mixer[7+mixer[4]] << 8); bmapsize = (nroutch * chidx[mixer[4]] + 7) >> 3; bmap += bmapsize - 1; if (mixer[0] < 10+mixer[4]+bmapsize) { printk(KERN_ERR "usbaudio: unit %u invalid MIXER_UNIT descriptor (bitmap too small)\n", mixer[3]); return; } for (i = 0; i < mixer[4]; i++) { state->termtype = termt[i]; if (chidx[i+1]-chidx[i] >= 2) { flg |= MIXFLG_STEREOIN; if (checkmixbmap(bmap, flg, chidx[i], nroutch)) { ch = getmixchannel(state, getvolchannel(state)); if (ch) { ch->unitid = mixer[3]; ch->selector = 0; ch->chnum = chidx[i]+1; ch->flags = flg; prepmixch(state); } continue; } } flg &= ~MIXFLG_STEREOIN; if (checkmixbmap(bmap, flg, chidx[i], nroutch)) { ch = getmixchannel(state, getvolchannel(state)); if (ch) { ch->unitid = mixer[3]; ch->selector = 0; ch->chnum = chidx[i]+1; ch->flags = flg; prepmixch(state); } } } state->termtype = 0; } static struct mixerchannel *slctsrc_findunit(struct consmixstate *state, __u8 unitid) { unsigned int i; for (i = 0; i < state->nrmixch; i++) if (state->mixch[i].unitid == unitid) return &state->mixch[i]; return NULL; } static void usb_audio_selectorunit(struct consmixstate *state, unsigned char *selector) { unsigned int chnum, i, mixch; struct mixerchannel *mch; if (!selector[4]) { printk(KERN_ERR "usbaudio: unit %u invalid SELECTOR_UNIT descriptor\n", selector[3]); return; } mixch = state->nrmixch; usb_audio_recurseunit(state, selector[5]); if (state->nrmixch != mixch) { mch = &state->mixch[state->nrmixch-1]; mch->slctunitid = selector[3] | (1 << 8); } else if ((mch = slctsrc_findunit(state, selector[5]))) { mch->slctunitid = selector[3] | (1 << 8); } else { printk(KERN_INFO "usbaudio: selector unit %u: ignoring channel 1\n", selector[3]); } chnum = state->nrchannels; for (i = 1; i < selector[4]; i++) { mixch = state->nrmixch; usb_audio_recurseunit(state, selector[5+i]); if (chnum != state->nrchannels) { printk(KERN_ERR "usbaudio: selector unit %u: input pins with varying channel numbers\n", selector[3]); state->termtype = 0; state->chconfig = 0; state->nrchannels = 0; return; } if (state->nrmixch != mixch) { mch = &state->mixch[state->nrmixch-1]; mch->slctunitid = selector[3] | ((i + 1) << 8); } else if ((mch = slctsrc_findunit(state, selector[5+i]))) { mch->slctunitid = selector[3] | ((i + 1) << 8); } else { printk(KERN_INFO "usbaudio: selector unit %u: ignoring channel %u\n", selector[3], i+1); } } state->termtype = 0; state->chconfig = 0; } /* in the future we might try to handle 3D etc. effect units */ static void usb_audio_processingunit(struct consmixstate *state, unsigned char *proc) { unsigned int i; for (i = 0; i < proc[6]; i++) usb_audio_recurseunit(state, proc[7+i]); state->nrchannels = proc[7+proc[6]]; state->termtype = 0; state->chconfig = proc[8+proc[6]] | (proc[9+proc[6]] << 8); } /* See Audio Class Spec, section 4.3.2.5 */ static void usb_audio_featureunit(struct consmixstate *state, unsigned char *ftr) { struct mixerchannel *ch; unsigned short chftr, mchftr; #if 0 struct usb_device *dev = state->s->usbdev; unsigned char data[1]; #endif unsigned char nr_logical_channels, i; usb_audio_recurseunit(state, ftr[4]); if (ftr[5] == 0 ) { printk(KERN_ERR "usbaudio: wrong controls size in feature unit %u\n",ftr[3]); return; } if (state->nrchannels == 0) { printk(KERN_ERR "usbaudio: feature unit %u source has no channels\n", ftr[3]); return; } if (state->nrchannels > 2) printk(KERN_WARNING "usbaudio: feature unit %u: OSS mixer interface does not support more than 2 channels\n", ftr[3]); nr_logical_channels=(ftr[0]-7)/ftr[5]-1; if (nr_logical_channels != state->nrchannels) { printk(KERN_WARNING "usbaudio: warning: found %d of %d logical channels.\n", state->nrchannels,nr_logical_channels); if (state->nrchannels == 1 && nr_logical_channels==0) { printk(KERN_INFO "usbaudio: assuming the channel found is the master channel (got a Philips camera?). Should be fine.\n"); } else if (state->nrchannels == 1 && nr_logical_channels==2) { printk(KERN_INFO "usbaudio: assuming that a stereo channel connected directly to a mixer is missing in search (got Labtec headset?). Should be fine.\n"); state->nrchannels=nr_logical_channels; } else { printk(KERN_WARNING "usbaudio: no idea what's going on..., contact linux-usb-devel@lists.sourceforge.net\n"); } } /* There is always a master channel */ mchftr = ftr[6]; /* Binary AND over logical channels if they exist */ if (nr_logical_channels) { chftr = ftr[6+ftr[5]]; for (i = 2; i <= nr_logical_channels; i++) chftr &= ftr[6+i*ftr[5]]; } else { chftr = 0; } /* volume control */ if (chftr & 2) { ch = getmixchannel(state, getvolchannel(state)); if (ch) { ch->unitid = ftr[3]; ch->selector = VOLUME_CONTROL; ch->chnum = 1; ch->flags = (state->nrchannels > 1) ? (MIXFLG_STEREOIN | MIXFLG_STEREOOUT) : 0; prepmixch(state); } } else if (mchftr & 2) { ch = getmixchannel(state, getvolchannel(state)); if (ch) { ch->unitid = ftr[3]; ch->selector = VOLUME_CONTROL; ch->chnum = 0; ch->flags = 0; prepmixch(state); } } /* bass control */ if (chftr & 4) { ch = getmixchannel(state, SOUND_MIXER_BASS); if (ch) { ch->unitid = ftr[3]; ch->selector = BASS_CONTROL; ch->chnum = 1; ch->flags = (state->nrchannels > 1) ? (MIXFLG_STEREOIN | MIXFLG_STEREOOUT) : 0; prepmixch(state); } } else if (mchftr & 4) { ch = getmixchannel(state, SOUND_MIXER_BASS); if (ch) { ch->unitid = ftr[3]; ch->selector = BASS_CONTROL; ch->chnum = 0; ch->flags = 0; prepmixch(state); } } /* treble control */ if (chftr & 16) { ch = getmixchannel(state, SOUND_MIXER_TREBLE); if (ch) { ch->unitid = ftr[3]; ch->selector = TREBLE_CONTROL; ch->chnum = 1; ch->flags = (state->nrchannels > 1) ? (MIXFLG_STEREOIN | MIXFLG_STEREOOUT) : 0; prepmixch(state); } } else if (mchftr & 16) { ch = getmixchannel(state, SOUND_MIXER_TREBLE); if (ch) { ch->unitid = ftr[3]; ch->selector = TREBLE_CONTROL; ch->chnum = 0; ch->flags = 0; prepmixch(state); } } #if 0 /* if there are mute controls, unmute them */ /* does not seem to be necessary, and the Dallas chip does not seem to support the "all" channel (255) */ if ((chftr & 1) || (mchftr & 1)) { printk(KERN_DEBUG "usbaudio: unmuting feature unit %u interface %u\n", ftr[3], state->ctrlif); data[0] = 0; if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, (MUTE_CONTROL << 8) | 0xff, state->ctrlif | (ftr[3] << 8), data, 1, HZ) < 0) printk(KERN_WARNING "usbaudio: failure to unmute feature unit %u interface %u\n", ftr[3], state->ctrlif); } #endif } static void usb_audio_recurseunit(struct consmixstate *state, unsigned char unitid) { unsigned char *p1; unsigned int i, j; if (test_and_set_bit(unitid, state->unitbitmap)) { printk(KERN_INFO "usbaudio: mixer path revisits unit %d\n", unitid); return; } p1 = find_audiocontrol_unit(state->buffer, state->buflen, NULL, unitid, state->ctrlif); if (!p1) { printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid); return; } state->nrchannels = 0; state->termtype = 0; state->chconfig = 0; switch (p1[2]) { case INPUT_TERMINAL: if (p1[0] < 12) { printk(KERN_ERR "usbaudio: unit %u: invalid INPUT_TERMINAL descriptor\n", unitid); return; } state->nrchannels = p1[7]; state->termtype = p1[4] | (p1[5] << 8); state->chconfig = p1[8] | (p1[9] << 8); return; case MIXER_UNIT: if (p1[0] < 10 || p1[0] < 10+p1[4]) { printk(KERN_ERR "usbaudio: unit %u: invalid MIXER_UNIT descriptor\n", unitid); return; } usb_audio_mixerunit(state, p1); return; case SELECTOR_UNIT: if (p1[0] < 6 || p1[0] < 6+p1[4]) { printk(KERN_ERR "usbaudio: unit %u: invalid SELECTOR_UNIT descriptor\n", unitid); return; } usb_audio_selectorunit(state, p1); return; case FEATURE_UNIT: /* See USB Audio Class Spec 4.3.2.5 */ if (p1[0] < 7 || p1[0] < 7+p1[5]) { printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid); return; } usb_audio_featureunit(state, p1); return; case PROCESSING_UNIT: if (p1[0] < 13 || p1[0] < 13+p1[6] || p1[0] < 13+p1[6]+p1[11+p1[6]]) { printk(KERN_ERR "usbaudio: unit %u: invalid PROCESSING_UNIT descriptor\n", unitid); return; } usb_audio_processingunit(state, p1); return; case EXTENSION_UNIT: if (p1[0] < 13 || p1[0] < 13+p1[6] || p1[0] < 13+p1[6]+p1[11+p1[6]]) { printk(KERN_ERR "usbaudio: unit %u: invalid EXTENSION_UNIT descriptor\n", unitid); return; } for (j = i = 0; i < p1[6]; i++) { usb_audio_recurseunit(state, p1[7+i]); if (!i) j = state->termtype; else if (j != state->termtype) j = 0; } state->nrchannels = p1[7+p1[6]]; state->chconfig = p1[8+p1[6]] | (p1[9+p1[6]] << 8); state->termtype = j; return; default: printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]); return; } } static void usb_audio_constructmixer(struct usb_audio_state *s, unsigned char *buffer, unsigned int buflen, unsigned int ctrlif, unsigned char *oterm) { struct usb_mixerdev *ms; struct consmixstate state; memset(&state, 0, sizeof(state)); state.s = s; state.nrmixch = 0; state.mixchmask = ~0; state.buffer = buffer; state.buflen = buflen; state.ctrlif = ctrlif; set_bit(oterm[3], state.unitbitmap); /* mark terminal ID as visited */ printk(KERN_DEBUG "usbaudio: constructing mixer for Terminal %u type 0x%04x\n", oterm[3], oterm[4] | (oterm[5] << 8)); usb_audio_recurseunit(&state, oterm[7]); if (!state.nrmixch) { printk(KERN_INFO "usbaudio: no mixer controls found for Terminal %u\n", oterm[3]); return; } if (!(ms = kmalloc(sizeof(struct usb_mixerdev)+state.nrmixch*sizeof(struct mixerchannel), GFP_KERNEL))) return; memset(ms, 0, sizeof(struct usb_mixerdev)); memcpy(&ms->ch, &state.mixch, state.nrmixch*sizeof(struct mixerchannel)); ms->state = s; ms->iface = ctrlif; ms->numch = state.nrmixch; if ((ms->dev_mixer = register_sound_mixer(&usb_mixer_fops, -1)) < 0) { printk(KERN_ERR "usbaudio: cannot register mixer\n"); kfree(ms); return; } printk(KERN_INFO "usbaudio: registered mixer 14,%d\n", ms->dev_mixer); list_add_tail(&ms->list, &s->mixerlist); } /* arbitrary limit, we won't check more interfaces than this */ #define USB_MAXINTERFACES 32 static struct usb_audio_state *usb_audio_parsecontrol(struct usb_device *dev, unsigned char *buffer, unsigned int buflen, unsigned int ctrlif) { struct usb_audio_state *s; struct usb_interface *iface; struct usb_host_interface *alt; unsigned char ifin[USB_MAXINTERFACES], ifout[USB_MAXINTERFACES]; unsigned char *p1; unsigned int i, j, k, numifin = 0, numifout = 0; if (!(s = kmalloc(sizeof(struct usb_audio_state), GFP_KERNEL))) return NULL; memset(s, 0, sizeof(struct usb_audio_state)); INIT_LIST_HEAD(&s->audiolist); INIT_LIST_HEAD(&s->mixerlist); s->usbdev = dev; s->count = 1; /* find audiocontrol interface */ if (!(p1 = find_csinterface_descriptor(buffer, buflen, NULL, HEADER, ctrlif, -1))) { printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u no HEADER found\n", dev->devnum, ctrlif); goto ret; } if (p1[0] < 8 + p1[7]) { printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u HEADER error\n", dev->devnum, ctrlif); goto ret; } if (!p1[7]) printk(KERN_INFO "usbaudio: device %d audiocontrol interface %u has no AudioStreaming and MidiStreaming interfaces\n", dev->devnum, ctrlif); for (i = 0; i < p1[7]; i++) { j = p1[8+i]; iface = usb_ifnum_to_if(dev, j); if (!iface) { printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u does not exist\n", dev->devnum, ctrlif, j); continue; } if (iface->num_altsetting == 1) { printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u has only 1 altsetting.\n", dev->devnum, ctrlif); continue; } alt = usb_altnum_to_altsetting(iface, 0); if (!alt) { printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u has no altsetting 0\n", dev->devnum, ctrlif, j); continue; } if (alt->desc.bInterfaceClass != USB_CLASS_AUDIO) { printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u is not an AudioClass interface\n", dev->devnum, ctrlif, j); continue; } if (alt->desc.bInterfaceSubClass == 3) { printk(KERN_INFO "usbaudio: device %d audiocontrol interface %u interface %u MIDIStreaming not supported\n", dev->devnum, ctrlif, j); continue; } if (alt->desc.bInterfaceSubClass != 2) { printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u invalid AudioClass subtype\n", dev->devnum, ctrlif, j); continue; } if (alt->desc.bNumEndpoints > 0) { /* Check all endpoints; should they all have a bandwidth of 0 ? */ for (k = 0; k < alt->desc.bNumEndpoints; k++) { if (alt->endpoint[k].desc.wMaxPacketSize > 0) { printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u endpoint %d does not have 0 bandwidth at alt[0]\n", dev->devnum, ctrlif, k); break; } } if (k < alt->desc.bNumEndpoints) continue; } alt = usb_altnum_to_altsetting(iface, 1); if (!alt) { printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u has no altsetting 1\n", dev->devnum, ctrlif, j); continue; } if (alt->desc.bNumEndpoints < 1) { printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u has no endpoint\n", dev->devnum, ctrlif, j); continue; } /* note: this requires the data endpoint to be ep0 and the optional sync ep to be ep1, which seems to be the case */ if (alt->endpoint[0].desc.bEndpointAddress & USB_DIR_IN) { if (numifin < USB_MAXINTERFACES) { ifin[numifin++] = j; usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1); } } else { if (numifout < USB_MAXINTERFACES) { ifout[numifout++] = j; usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1); } } } printk(KERN_INFO "usbaudio: device %d audiocontrol interface %u has %u input and %u output AudioStreaming interfaces\n", dev->devnum, ctrlif, numifin, numifout); for (i = 0; i < numifin && i < numifout; i++) usb_audio_parsestreaming(s, buffer, buflen, ifin[i], ifout[i]); for (j = i; j < numifin; j++) usb_audio_parsestreaming(s, buffer, buflen, ifin[i], -1); for (j = i; j < numifout; j++) usb_audio_parsestreaming(s, buffer, buflen, -1, ifout[i]); /* now walk through all OUTPUT_TERMINAL descriptors to search for mixers */ p1 = find_csinterface_descriptor(buffer, buflen, NULL, OUTPUT_TERMINAL, ctrlif, -1); while (p1) { if (p1[0] >= 9) usb_audio_constructmixer(s, buffer, buflen, ctrlif, p1); p1 = find_csinterface_descriptor(buffer, buflen, p1, OUTPUT_TERMINAL, ctrlif, -1); } ret: if (list_empty(&s->audiolist) && list_empty(&s->mixerlist)) { kfree(s); return NULL; } /* everything successful */ down(&open_sem); list_add_tail(&s->audiodev, &audiodevs); up(&open_sem); printk(KERN_DEBUG "usb_audio_parsecontrol: usb_audio_state at %p\n", s); return s; } /* we only care for the currently active configuration */ static int usb_audio_probe(struct usb_interface *intf, const struct usb_device_id *id) { struct usb_device *dev = interface_to_usbdev (intf); struct usb_audio_state *s; unsigned char *buffer; unsigned int buflen; #if 0 printk(KERN_DEBUG "usbaudio: Probing if %i: IC %x, ISC %x\n", ifnum, config->interface[ifnum].altsetting[0].desc.bInterfaceClass, config->interface[ifnum].altsetting[0].desc.bInterfaceSubClass); #endif /* * audiocontrol interface found * find which configuration number is active */ buffer = dev->rawdescriptors[dev->actconfig - dev->config]; buflen = dev->actconfig->desc.wTotalLength; s = usb_audio_parsecontrol(dev, buffer, buflen, intf->altsetting->desc.bInterfaceNumber); if (s) { usb_set_intfdata (intf, s); return 0; } return -ENODEV; } /* a revoke facility would make things simpler */ static void usb_audio_disconnect(struct usb_interface *intf) { struct usb_audio_state *s = usb_get_intfdata (intf); struct list_head *list; struct usb_audiodev *as; struct usb_mixerdev *ms; if (!s) return; /* we get called with -1 for every audiostreaming interface registered */ if (s == (struct usb_audio_state *)-1) { dprintk((KERN_DEBUG "usbaudio: note, usb_audio_disconnect called with -1\n")); return; } if (!s->usbdev) { dprintk((KERN_DEBUG "usbaudio: error, usb_audio_disconnect already called for %p!\n", s)); return; } down(&open_sem); list_del_init(&s->audiodev); s->usbdev = NULL; usb_set_intfdata (intf, NULL); /* deregister all audio and mixer devices, so no new processes can open this device */ list_for_each(list, &s->audiolist) { as = list_entry(list, struct usb_audiodev, list); usbin_disc(as); usbout_disc(as); wake_up(&as->usbin.dma.wait); wake_up(&as->usbout.dma.wait); if (as->dev_audio >= 0) { unregister_sound_dsp(as->dev_audio); printk(KERN_INFO "usbaudio: unregister dsp 14,%d\n", as->dev_audio); } as->dev_audio = -1; } list_for_each(list, &s->mixerlist) { ms = list_entry(list, struct usb_mixerdev, list); if (ms->dev_mixer >= 0) { unregister_sound_mixer(ms->dev_mixer); printk(KERN_INFO "usbaudio: unregister mixer 14,%d\n", ms->dev_mixer); } ms->dev_mixer = -1; } release(s); wake_up(&open_wait); } static int __init usb_audio_init(void) { int result = usb_register(&usb_audio_driver); if (result == 0) info(DRIVER_VERSION ":" DRIVER_DESC); return result; } static void __exit usb_audio_cleanup(void) { usb_deregister(&usb_audio_driver); } module_init(usb_audio_init); module_exit(usb_audio_cleanup); MODULE_AUTHOR( DRIVER_AUTHOR ); MODULE_DESCRIPTION( DRIVER_DESC ); MODULE_LICENSE("GPL"); |