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 | /* * forte.c - ForteMedia FM801 OSS Driver * * Written by Martin K. Petersen <mkp@mkp.net> * Copyright (C) 2002 Hewlett-Packard Company * Portions Copyright (C) 2003 Martin K. Petersen * * Latest version: http://mkp.net/forte/ * * Based upon the ALSA FM801 driver by Jaroslav Kysela and OSS drivers * by Thomas Sailer, Alan Cox, Zach Brown, and Jeff Garzik. Thanks * guys! * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA * */ #include <linux/config.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/spinlock.h> #include <linux/pci.h> #include <linux/delay.h> #include <linux/poll.h> #include <linux/sound.h> #include <linux/ac97_codec.h> #include <linux/interrupt.h> #include <linux/proc_fs.h> #include <asm/uaccess.h> #include <asm/io.h> #define DRIVER_NAME "forte" #define DRIVER_VERSION "$Id: forte.c,v 1.63 2003/03/01 05:32:42 mkp Exp $" #define PFX DRIVER_NAME ": " #undef M_DEBUG #ifdef M_DEBUG #define DPRINTK(args...) printk(KERN_WARNING args) #else #define DPRINTK(args...) #endif /* Card capabilities */ #define FORTE_CAPS (DSP_CAP_MMAP | DSP_CAP_TRIGGER) /* Supported audio formats */ #define FORTE_FMTS (AFMT_U8 | AFMT_S16_LE) /* Buffers */ #define FORTE_MIN_FRAG_SIZE 256 #define FORTE_MAX_FRAG_SIZE PAGE_SIZE #define FORTE_DEF_FRAG_SIZE 256 #define FORTE_MIN_FRAGMENTS 2 #define FORTE_MAX_FRAGMENTS 256 #define FORTE_DEF_FRAGMENTS 2 #define FORTE_MIN_BUF_MSECS 500 #define FORTE_MAX_BUF_MSECS 1000 /* PCI BARs */ #define FORTE_PCM_VOL 0x00 /* PCM Output Volume */ #define FORTE_FM_VOL 0x02 /* FM Output Volume */ #define FORTE_I2S_VOL 0x04 /* I2S Volume */ #define FORTE_REC_SRC 0x06 /* Record Source */ #define FORTE_PLY_CTRL 0x08 /* Playback Control */ #define FORTE_PLY_COUNT 0x0a /* Playback Count */ #define FORTE_PLY_BUF1 0x0c /* Playback Buffer I */ #define FORTE_PLY_BUF2 0x10 /* Playback Buffer II */ #define FORTE_CAP_CTRL 0x14 /* Capture Control */ #define FORTE_CAP_COUNT 0x16 /* Capture Count */ #define FORTE_CAP_BUF1 0x18 /* Capture Buffer I */ #define FORTE_CAP_BUF2 0x1c /* Capture Buffer II */ #define FORTE_CODEC_CTRL 0x22 /* Codec Control */ #define FORTE_I2S_MODE 0x24 /* I2S Mode Control */ #define FORTE_VOLUME 0x26 /* Volume Up/Down/Mute Status */ #define FORTE_I2C_CTRL 0x29 /* I2C Control */ #define FORTE_AC97_CMD 0x2a /* AC'97 Command */ #define FORTE_AC97_DATA 0x2c /* AC'97 Data */ #define FORTE_MPU401_DATA 0x30 /* MPU401 Data */ #define FORTE_MPU401_CMD 0x31 /* MPU401 Command */ #define FORTE_GPIO_CTRL 0x52 /* General Purpose I/O Control */ #define FORTE_GEN_CTRL 0x54 /* General Control */ #define FORTE_IRQ_MASK 0x56 /* Interrupt Mask */ #define FORTE_IRQ_STATUS 0x5a /* Interrupt Status */ #define FORTE_OPL3_BANK0 0x68 /* OPL3 Status Read / Bank 0 Write */ #define FORTE_OPL3_DATA0 0x69 /* OPL3 Data 0 Write */ #define FORTE_OPL3_BANK1 0x6a /* OPL3 Bank 1 Write */ #define FORTE_OPL3_DATA1 0x6b /* OPL3 Bank 1 Write */ #define FORTE_POWERDOWN 0x70 /* Blocks Power Down Control */ #define FORTE_CAP_OFFSET FORTE_CAP_CTRL - FORTE_PLY_CTRL #define FORTE_AC97_ADDR_SHIFT 10 /* Playback and record control register bits */ #define FORTE_BUF1_LAST (1<<1) #define FORTE_BUF2_LAST (1<<2) #define FORTE_START (1<<5) #define FORTE_PAUSE (1<<6) #define FORTE_IMMED_STOP (1<<7) #define FORTE_RATE_SHIFT 8 #define FORTE_RATE_MASK (15 << FORTE_RATE_SHIFT) #define FORTE_CHANNELS_4 (1<<12) /* Playback only */ #define FORTE_CHANNELS_6 (2<<12) /* Playback only */ #define FORTE_CHANNELS_6MS (3<<12) /* Playback only */ #define FORTE_CHANNELS_MASK (3<<12) #define FORTE_16BIT (1<<14) #define FORTE_STEREO (1<<15) /* IRQ status bits */ #define FORTE_IRQ_PLAYBACK (1<<8) #define FORTE_IRQ_CAPTURE (1<<9) #define FORTE_IRQ_VOLUME (1<<14) #define FORTE_IRQ_MPU (1<<15) /* CODEC control */ #define FORTE_CC_CODEC_RESET (1<<5) #define FORTE_CC_AC97_RESET (1<<6) /* AC97 cmd */ #define FORTE_AC97_WRITE (0<<7) #define FORTE_AC97_READ (1<<7) #define FORTE_AC97_DP_INVALID (0<<8) #define FORTE_AC97_DP_VALID (1<<8) #define FORTE_AC97_PORT_RDY (0<<9) #define FORTE_AC97_PORT_BSY (1<<9) struct forte_channel { const char *name; unsigned short ctrl; /* Ctrl BAR contents */ unsigned long iobase; /* Ctrl BAR address */ wait_queue_head_t wait; void *buf; /* Buffer */ dma_addr_t buf_handle; /* Buffer handle */ unsigned int record; unsigned int format; unsigned int rate; unsigned int stereo; unsigned int frag_sz; /* Current fragment size */ unsigned int frag_num; /* Current # of fragments */ unsigned int frag_msecs; /* Milliseconds per frag */ unsigned int buf_sz; /* Current buffer size */ unsigned int hwptr; /* Tail */ unsigned int swptr; /* Head */ unsigned int filled_frags; /* Fragments currently full */ unsigned int next_buf; /* Index of next buffer */ unsigned int active; /* Channel currently in use */ unsigned int mapped; /* mmap */ unsigned int buf_pages; /* Real size of buffer */ unsigned int nr_irqs; /* Number of interrupts */ unsigned int bytes; /* Total bytes */ unsigned int residue; /* Partial fragment */ }; struct forte_chip { struct pci_dev *pci_dev; unsigned long iobase; int irq; struct semaphore open_sem; /* Device access */ spinlock_t lock; /* State */ spinlock_t ac97_lock; struct ac97_codec *ac97; int multichannel; int dsp; /* OSS handle */ int trigger; /* mmap I/O trigger */ struct forte_channel play; struct forte_channel rec; }; static int channels[] = { 2, 4, 6, }; static int rates[] = { 5500, 8000, 9600, 11025, 16000, 19200, 22050, 32000, 38400, 44100, 48000, }; static struct forte_chip *forte; static int found; /* AC97 Codec -------------------------------------------------------------- */ /** * forte_ac97_wait: * @chip: fm801 instance whose AC97 codec to wait on * * FIXME: * Stop busy-waiting */ static inline int forte_ac97_wait (struct forte_chip *chip) { int i = 10000; while ( (inw (chip->iobase + FORTE_AC97_CMD) & FORTE_AC97_PORT_BSY) && i-- ) cpu_relax(); return i == 0; } /** * forte_ac97_read: * @codec: AC97 codec to read from * @reg: register to read */ static u16 forte_ac97_read (struct ac97_codec *codec, u8 reg) { u16 ret = 0; struct forte_chip *chip = codec->private_data; spin_lock (&chip->ac97_lock); /* Knock, knock */ if (forte_ac97_wait (chip)) { printk (KERN_ERR PFX "ac97_read: Serial bus busy\n"); goto out; } /* Send read command */ outw (reg | (1<<7), chip->iobase + FORTE_AC97_CMD); if (forte_ac97_wait (chip)) { printk (KERN_ERR PFX "ac97_read: Bus busy reading reg 0x%x\n", reg); goto out; } /* Sanity checking */ if (inw (chip->iobase + FORTE_AC97_CMD) & FORTE_AC97_DP_INVALID) { printk (KERN_ERR PFX "ac97_read: Invalid data port"); goto out; } /* Fetch result */ ret = inw (chip->iobase + FORTE_AC97_DATA); out: spin_unlock (&chip->ac97_lock); return ret; } /** * forte_ac97_write: * @codec: AC97 codec to send command to * @reg: register to write * @val: value to write */ static void forte_ac97_write (struct ac97_codec *codec, u8 reg, u16 val) { struct forte_chip *chip = codec->private_data; spin_lock (&chip->ac97_lock); /* Knock, knock */ if (forte_ac97_wait (chip)) { printk (KERN_ERR PFX "ac97_write: Serial bus busy\n"); goto out; } outw (val, chip->iobase + FORTE_AC97_DATA); outb (reg | FORTE_AC97_WRITE, chip->iobase + FORTE_AC97_CMD); /* Wait for completion */ if (forte_ac97_wait (chip)) { printk (KERN_ERR PFX "ac97_write: Bus busy after write\n"); goto out; } out: spin_unlock (&chip->ac97_lock); } /* Mixer ------------------------------------------------------------------- */ /** * forte_mixer_open: * @inode: * @file: */ static int forte_mixer_open (struct inode *inode, struct file *file) { struct forte_chip *chip = forte; file->private_data = chip->ac97; return 0; } /** * forte_mixer_release: * @inode: * @file: */ static int forte_mixer_release (struct inode *inode, struct file *file) { /* We will welease Wodewick */ return 0; } /** * forte_mixer_ioctl: * @inode: * @file: */ static int forte_mixer_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { struct ac97_codec *codec = (struct ac97_codec *) file->private_data; return codec->mixer_ioctl (codec, cmd, arg); } static struct file_operations forte_mixer_fops = { .owner = THIS_MODULE, .llseek = no_llseek, .ioctl = forte_mixer_ioctl, .open = forte_mixer_open, .release = forte_mixer_release, }; /* Channel ----------------------------------------------------------------- */ /** * forte_channel_reset: * @channel: Channel to reset * * Locking: Must be called with lock held. */ static void forte_channel_reset (struct forte_channel *channel) { if (!channel || !channel->iobase) return; DPRINTK ("%s: channel = %s\n", __FUNCTION__, channel->name); channel->ctrl &= ~FORTE_START; outw (channel->ctrl, channel->iobase + FORTE_PLY_CTRL); /* We always play at least two fragments, hence these defaults */ channel->hwptr = channel->frag_sz; channel->next_buf = 1; channel->swptr = 0; channel->filled_frags = 0; channel->active = 0; channel->bytes = 0; channel->nr_irqs = 0; channel->mapped = 0; channel->residue = 0; } /** * forte_channel_start: * @channel: Channel to start (record/playback) * * Locking: Must be called with lock held. */ static void inline forte_channel_start (struct forte_channel *channel) { if (!channel || !channel->iobase || channel->active) return; channel->ctrl &= ~(FORTE_PAUSE | FORTE_BUF1_LAST | FORTE_BUF2_LAST | FORTE_IMMED_STOP); channel->ctrl |= FORTE_START; channel->active = 1; outw (channel->ctrl, channel->iobase + FORTE_PLY_CTRL); } /** * forte_channel_stop: * @channel: Channel to stop * * Locking: Must be called with lock held. */ static void inline forte_channel_stop (struct forte_channel *channel) { if (!channel || !channel->iobase) return; channel->ctrl &= ~(FORTE_START | FORTE_PAUSE); channel->ctrl |= FORTE_IMMED_STOP; channel->active = 0; outw (channel->ctrl, channel->iobase + FORTE_PLY_CTRL); } /** * forte_channel_pause: * @channel: Channel to pause * * Locking: Must be called with lock held. */ static void inline forte_channel_pause (struct forte_channel *channel) { if (!channel || !channel->iobase) return; channel->ctrl |= FORTE_PAUSE; channel->active = 0; outw (channel->ctrl, channel->iobase + FORTE_PLY_CTRL); } /** * forte_channel_rate: * @channel: Channel whose rate to set. Playback and record are * independent. * @rate: Channel rate in Hz * * Locking: Must be called with lock held. */ static int forte_channel_rate (struct forte_channel *channel, unsigned int rate) { int new_rate; if (!channel || !channel->iobase) return -EINVAL; /* The FM801 only supports a handful of fixed frequencies. * We find the value closest to what userland requested. */ if (rate <= 6250) { rate = 5500; new_rate = 0; } else if (rate <= 8800) { rate = 8000; new_rate = 1; } else if (rate <= 10312) { rate = 9600; new_rate = 2; } else if (rate <= 13512) { rate = 11025; new_rate = 3; } else if (rate <= 17600) { rate = 16000; new_rate = 4; } else if (rate <= 20625) { rate = 19200; new_rate = 5; } else if (rate <= 27025) { rate = 22050; new_rate = 6; } else if (rate <= 35200) { rate = 32000; new_rate = 7; } else if (rate <= 41250) { rate = 38400; new_rate = 8; } else if (rate <= 46050) { rate = 44100; new_rate = 9; } else { rate = 48000; new_rate = 10; } channel->ctrl &= ~FORTE_RATE_MASK; channel->ctrl |= new_rate << FORTE_RATE_SHIFT; channel->rate = rate; DPRINTK ("%s: %s rate = %d\n", __FUNCTION__, channel->name, rate); return rate; } /** * forte_channel_format: * @channel: Channel whose audio format to set * @format: OSS format ID * * Locking: Must be called with lock held. */ static int forte_channel_format (struct forte_channel *channel, int format) { if (!channel || !channel->iobase) return -EINVAL; switch (format) { case AFMT_QUERY: break; case AFMT_U8: channel->ctrl &= ~FORTE_16BIT; channel->format = AFMT_U8; break; case AFMT_S16_LE: default: channel->ctrl |= FORTE_16BIT; channel->format = AFMT_S16_LE; break; } DPRINTK ("%s: %s want %d format, got %d\n", __FUNCTION__, channel->name, format, channel->format); return channel->format; } /** * forte_channel_stereo: * @channel: Channel to toggle * @stereo: 0 for Mono, 1 for Stereo * * Locking: Must be called with lock held. */ static int forte_channel_stereo (struct forte_channel *channel, unsigned int stereo) { int ret; if (!channel || !channel->iobase) return -EINVAL; DPRINTK ("%s: %s stereo = %d\n", __FUNCTION__, channel->name, stereo); switch (stereo) { case 0: channel->ctrl &= ~(FORTE_STEREO | FORTE_CHANNELS_MASK); channel-> stereo = stereo; ret = stereo; break; case 1: channel->ctrl &= ~FORTE_CHANNELS_MASK; channel->ctrl |= FORTE_STEREO; channel-> stereo = stereo; ret = stereo; break; default: DPRINTK ("Unsupported channel format"); ret = -EINVAL; break; } return ret; } /** * forte_channel_buffer: * @channel: Channel whose buffer to set up * * Locking: Must be called with lock held. */ static void forte_channel_buffer (struct forte_channel *channel, int sz, int num) { unsigned int msecs, shift; /* Go away, I'm busy */ if (channel->filled_frags || channel->bytes) return; /* Fragment size must be a power of 2 */ shift = 0; sz++; while (sz >>= 1) shift++; channel->frag_sz = 1 << shift; /* Round fragment size to something reasonable */ if (channel->frag_sz < FORTE_MIN_FRAG_SIZE) channel->frag_sz = FORTE_MIN_FRAG_SIZE; if (channel->frag_sz > FORTE_MAX_FRAG_SIZE) channel->frag_sz = FORTE_MAX_FRAG_SIZE; /* Find fragment length in milliseconds */ msecs = channel->frag_sz / (channel->format == AFMT_S16_LE ? 2 : 1) / (channel->stereo ? 2 : 1) / (channel->rate / 1000); channel->frag_msecs = msecs; /* Pick a suitable number of fragments */ if (msecs * num < FORTE_MIN_BUF_MSECS) num = FORTE_MIN_BUF_MSECS / msecs; if (msecs * num > FORTE_MAX_BUF_MSECS) num = FORTE_MAX_BUF_MSECS / msecs; /* Fragment number must be a power of 2 */ shift = 0; while (num >>= 1) shift++; channel->frag_num = 1 << (shift + 1); /* Round fragment number to something reasonable */ if (channel->frag_num < FORTE_MIN_FRAGMENTS) channel->frag_num = FORTE_MIN_FRAGMENTS; if (channel->frag_num > FORTE_MAX_FRAGMENTS) channel->frag_num = FORTE_MAX_FRAGMENTS; channel->buf_sz = channel->frag_sz * channel->frag_num; DPRINTK ("%s: %s frag_sz = %d, frag_num = %d, buf_sz = %d\n", __FUNCTION__, channel->name, channel->frag_sz, channel->frag_num, channel->buf_sz); } /** * forte_channel_prep: * @channel: Channel whose buffer to prepare * * Locking: Lock held. */ static void forte_channel_prep (struct forte_channel *channel) { struct page *page; int i; if (channel->buf) return; forte_channel_buffer (channel, channel->frag_sz, channel->frag_num); channel->buf_pages = channel->buf_sz >> PAGE_SHIFT; if (channel->buf_sz % PAGE_SIZE) channel->buf_pages++; DPRINTK ("%s: %s frag_sz = %d, frag_num = %d, buf_sz = %d, pg = %d\n", __FUNCTION__, channel->name, channel->frag_sz, channel->frag_num, channel->buf_sz, channel->buf_pages); /* DMA buffer */ channel->buf = pci_alloc_consistent (forte->pci_dev, channel->buf_pages * PAGE_SIZE, &channel->buf_handle); if (!channel->buf || !channel->buf_handle) BUG(); page = virt_to_page (channel->buf); /* FIXME: can this go away ? */ for (i = 0 ; i < channel->buf_pages ; i++) SetPageReserved(page++); /* Prep buffer registers */ outw (channel->frag_sz - 1, channel->iobase + FORTE_PLY_COUNT); outl (channel->buf_handle, channel->iobase + FORTE_PLY_BUF1); outl (channel->buf_handle + channel->frag_sz, channel->iobase + FORTE_PLY_BUF2); /* Reset hwptr */ channel->hwptr = channel->frag_sz; channel->next_buf = 1; DPRINTK ("%s: %s buffer @ %p (%p)\n", __FUNCTION__, channel->name, channel->buf, channel->buf_handle); } /** * forte_channel_drain: * @chip: * @channel: * * Locking: Don't hold the lock. */ static inline int forte_channel_drain (struct forte_channel *channel) { DECLARE_WAITQUEUE (wait, current); unsigned long flags; DPRINTK ("%s\n", __FUNCTION__); if (channel->mapped) { spin_lock_irqsave (&forte->lock, flags); forte_channel_stop (channel); spin_unlock_irqrestore (&forte->lock, flags); return 0; } spin_lock_irqsave (&forte->lock, flags); add_wait_queue (&channel->wait, &wait); for (;;) { if (channel->active == 0 || channel->filled_frags == 1) break; spin_unlock_irqrestore (&forte->lock, flags); __set_current_state (TASK_INTERRUPTIBLE); schedule(); spin_lock_irqsave (&forte->lock, flags); } forte_channel_stop (channel); forte_channel_reset (channel); set_current_state (TASK_RUNNING); remove_wait_queue (&channel->wait, &wait); spin_unlock_irqrestore (&forte->lock, flags); return 0; } /** * forte_channel_init: * @chip: Forte chip instance the channel hangs off * @channel: Channel to initialize * * Description: * Initializes a channel, sets defaults, and allocates * buffers. * * Locking: No lock held. */ static int forte_channel_init (struct forte_chip *chip, struct forte_channel *channel) { DPRINTK ("%s: chip iobase @ %p\n", __FUNCTION__, (void *)chip->iobase); spin_lock_irq (&chip->lock); memset (channel, 0x0, sizeof (*channel)); if (channel == &chip->play) { channel->name = "PCM_OUT"; channel->iobase = chip->iobase; DPRINTK ("%s: PCM-OUT iobase @ %p\n", __FUNCTION__, (void *) channel->iobase); } else if (channel == &chip->rec) { channel->name = "PCM_IN"; channel->iobase = chip->iobase + FORTE_CAP_OFFSET; channel->record = 1; DPRINTK ("%s: PCM-IN iobase @ %p\n", __FUNCTION__, (void *) channel->iobase); } else BUG(); init_waitqueue_head (&channel->wait); /* Defaults: 48kHz, 16-bit, stereo */ channel->ctrl = inw (channel->iobase + FORTE_PLY_CTRL); forte_channel_reset (channel); forte_channel_stereo (channel, 1); forte_channel_format (channel, AFMT_S16_LE); forte_channel_rate (channel, 48000); channel->frag_sz = FORTE_DEF_FRAG_SIZE; channel->frag_num = FORTE_DEF_FRAGMENTS; chip->trigger = 0; spin_unlock_irq (&chip->lock); return 0; } /** * forte_channel_free: * @chip: Chip this channel hangs off * @channel: Channel to nuke * * Description: * Resets channel and frees buffers. * * Locking: Hold your horses. */ static void forte_channel_free (struct forte_chip *chip, struct forte_channel *channel) { DPRINTK ("%s: %s\n", __FUNCTION__, channel->name); if (!channel->buf_handle) return; pci_free_consistent (chip->pci_dev, channel->buf_pages * PAGE_SIZE, channel->buf, channel->buf_handle); memset (channel, 0x0, sizeof (*channel)); } /* DSP --------------------------------------------------------------------- */ /** * forte_dsp_ioctl: */ static int forte_dsp_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { int ival=0, ret, rval=0, rd, wr, count; struct forte_chip *chip; struct audio_buf_info abi; struct count_info cinfo; void __user *argp = (void __user *)arg; int __user *p = argp; chip = file->private_data; if (file->f_mode & FMODE_WRITE) wr = 1; else wr = 0; if (file->f_mode & FMODE_READ) rd = 1; else rd = 0; switch (cmd) { case OSS_GETVERSION: return put_user (SOUND_VERSION, p); case SNDCTL_DSP_GETCAPS: DPRINTK ("%s: GETCAPS\n", __FUNCTION__); ival = FORTE_CAPS; /* DUPLEX */ return put_user (ival, p); case SNDCTL_DSP_GETFMTS: DPRINTK ("%s: GETFMTS\n", __FUNCTION__); ival = FORTE_FMTS; /* U8, 16LE */ return put_user (ival, p); case SNDCTL_DSP_SETFMT: /* U8, 16LE */ DPRINTK ("%s: SETFMT\n", __FUNCTION__); if (get_user (ival, p)) return -EFAULT; spin_lock_irq (&chip->lock); if (rd) { forte_channel_stop (&chip->rec); rval = forte_channel_format (&chip->rec, ival); } if (wr) { forte_channel_stop (&chip->rec); rval = forte_channel_format (&chip->play, ival); } spin_unlock_irq (&chip->lock); return put_user (rval, p); case SNDCTL_DSP_STEREO: /* 0 - mono, 1 - stereo */ DPRINTK ("%s: STEREO\n", __FUNCTION__); if (get_user (ival, p)) return -EFAULT; spin_lock_irq (&chip->lock); if (rd) { forte_channel_stop (&chip->rec); rval = forte_channel_stereo (&chip->rec, ival); } if (wr) { forte_channel_stop (&chip->rec); rval = forte_channel_stereo (&chip->play, ival); } spin_unlock_irq (&chip->lock); return put_user (rval, p); case SNDCTL_DSP_CHANNELS: /* 1 - mono, 2 - stereo */ DPRINTK ("%s: CHANNELS\n", __FUNCTION__); if (get_user (ival, p)) return -EFAULT; spin_lock_irq (&chip->lock); if (rd) { forte_channel_stop (&chip->rec); rval = forte_channel_stereo (&chip->rec, ival-1) + 1; } if (wr) { forte_channel_stop (&chip->play); rval = forte_channel_stereo (&chip->play, ival-1) + 1; } spin_unlock_irq (&chip->lock); return put_user (rval, p); case SNDCTL_DSP_SPEED: DPRINTK ("%s: SPEED\n", __FUNCTION__); if (get_user (ival, p)) return -EFAULT; spin_lock_irq (&chip->lock); if (rd) { forte_channel_stop (&chip->rec); rval = forte_channel_rate (&chip->rec, ival); } if (wr) { forte_channel_stop (&chip->play); rval = forte_channel_rate (&chip->play, ival); } spin_unlock_irq (&chip->lock); return put_user(rval, p); case SNDCTL_DSP_GETBLKSIZE: DPRINTK ("%s: GETBLKSIZE\n", __FUNCTION__); spin_lock_irq (&chip->lock); if (rd) ival = chip->rec.frag_sz; if (wr) ival = chip->play.frag_sz; spin_unlock_irq (&chip->lock); return put_user (ival, p); case SNDCTL_DSP_RESET: DPRINTK ("%s: RESET\n", __FUNCTION__); spin_lock_irq (&chip->lock); if (rd) forte_channel_reset (&chip->rec); if (wr) forte_channel_reset (&chip->play); spin_unlock_irq (&chip->lock); return 0; case SNDCTL_DSP_SYNC: DPRINTK ("%s: SYNC\n", __FUNCTION__); if (wr) ret = forte_channel_drain (&chip->play); return 0; case SNDCTL_DSP_POST: DPRINTK ("%s: POST\n", __FUNCTION__); if (wr) { spin_lock_irq (&chip->lock); if (chip->play.filled_frags) forte_channel_start (&chip->play); spin_unlock_irq (&chip->lock); } return 0; case SNDCTL_DSP_SETFRAGMENT: DPRINTK ("%s: SETFRAGMENT\n", __FUNCTION__); if (get_user (ival, p)) return -EFAULT; spin_lock_irq (&chip->lock); if (rd) { forte_channel_buffer (&chip->rec, ival & 0xffff, (ival >> 16) & 0xffff); ival = (chip->rec.frag_num << 16) + chip->rec.frag_sz; } if (wr) { forte_channel_buffer (&chip->play, ival & 0xffff, (ival >> 16) & 0xffff); ival = (chip->play.frag_num << 16) +chip->play.frag_sz; } spin_unlock_irq (&chip->lock); return put_user (ival, p); case SNDCTL_DSP_GETISPACE: DPRINTK ("%s: GETISPACE\n", __FUNCTION__); if (!rd) return -EINVAL; spin_lock_irq (&chip->lock); abi.fragstotal = chip->rec.frag_num; abi.fragsize = chip->rec.frag_sz; if (chip->rec.mapped) { abi.fragments = chip->rec.frag_num - 2; abi.bytes = abi.fragments * abi.fragsize; } else { abi.fragments = chip->rec.filled_frags; abi.bytes = abi.fragments * abi.fragsize; } spin_unlock_irq (&chip->lock); return copy_to_user (argp, &abi, sizeof (abi)) ? -EFAULT : 0; case SNDCTL_DSP_GETIPTR: DPRINTK ("%s: GETIPTR\n", __FUNCTION__); if (!rd) return -EINVAL; spin_lock_irq (&chip->lock); if (chip->rec.active) cinfo.ptr = chip->rec.hwptr; else cinfo.ptr = 0; cinfo.bytes = chip->rec.bytes; cinfo.blocks = chip->rec.nr_irqs; chip->rec.nr_irqs = 0; spin_unlock_irq (&chip->lock); return copy_to_user (argp, &cinfo, sizeof (cinfo)) ? -EFAULT : 0; case SNDCTL_DSP_GETOSPACE: if (!wr) return -EINVAL; spin_lock_irq (&chip->lock); abi.fragstotal = chip->play.frag_num; abi.fragsize = chip->play.frag_sz; if (chip->play.mapped) { abi.fragments = chip->play.frag_num - 2; abi.bytes = chip->play.buf_sz; } else { abi.fragments = chip->play.frag_num - chip->play.filled_frags; if (chip->play.residue) abi.fragments--; abi.bytes = abi.fragments * abi.fragsize + chip->play.residue; } spin_unlock_irq (&chip->lock); return copy_to_user (argp, &abi, sizeof (abi)) ? -EFAULT : 0; case SNDCTL_DSP_GETOPTR: if (!wr) return -EINVAL; spin_lock_irq (&chip->lock); if (chip->play.active) cinfo.ptr = chip->play.hwptr; else cinfo.ptr = 0; cinfo.bytes = chip->play.bytes; cinfo.blocks = chip->play.nr_irqs; chip->play.nr_irqs = 0; spin_unlock_irq (&chip->lock); return copy_to_user (argp, &cinfo, sizeof (cinfo)) ? -EFAULT : 0; case SNDCTL_DSP_GETODELAY: if (!wr) return -EINVAL; spin_lock_irq (&chip->lock); if (!chip->play.active) { ival = 0; } else if (chip->play.mapped) { count = inw (chip->play.iobase + FORTE_PLY_COUNT) + 1; ival = chip->play.frag_sz - count; } else { ival = chip->play.filled_frags * chip->play.frag_sz; if (chip->play.residue) ival += chip->play.frag_sz - chip->play.residue; } spin_unlock_irq (&chip->lock); return put_user (ival, p); case SNDCTL_DSP_SETDUPLEX: DPRINTK ("%s: SETDUPLEX\n", __FUNCTION__); return -EINVAL; case SNDCTL_DSP_GETTRIGGER: DPRINTK ("%s: GETTRIGGER\n", __FUNCTION__); return put_user (chip->trigger, p); case SNDCTL_DSP_SETTRIGGER: if (get_user (ival, p)) return -EFAULT; DPRINTK ("%s: SETTRIGGER %d\n", __FUNCTION__, ival); if (wr) { spin_lock_irq (&chip->lock); if (ival & PCM_ENABLE_OUTPUT) forte_channel_start (&chip->play); else { chip->trigger = 1; forte_channel_prep (&chip->play); forte_channel_stop (&chip->play); } spin_unlock_irq (&chip->lock); } else if (rd) { spin_lock_irq (&chip->lock); if (ival & PCM_ENABLE_INPUT) forte_channel_start (&chip->rec); else { chip->trigger = 1; forte_channel_prep (&chip->rec); forte_channel_stop (&chip->rec); } spin_unlock_irq (&chip->lock); } return 0; case SOUND_PCM_READ_RATE: DPRINTK ("%s: PCM_READ_RATE\n", __FUNCTION__); return put_user (chip->play.rate, p); case SOUND_PCM_READ_CHANNELS: DPRINTK ("%s: PCM_READ_CHANNELS\n", __FUNCTION__); return put_user (chip->play.stereo, p); case SOUND_PCM_READ_BITS: DPRINTK ("%s: PCM_READ_BITS\n", __FUNCTION__); return put_user (chip->play.format, p); case SNDCTL_DSP_NONBLOCK: DPRINTK ("%s: DSP_NONBLOCK\n", __FUNCTION__); file->f_flags |= O_NONBLOCK; return 0; default: DPRINTK ("Unsupported ioctl: %x (%p)\n", cmd, argp); break; } return -EINVAL; } /** * forte_dsp_open: */ static int forte_dsp_open (struct inode *inode, struct file *file) { struct forte_chip *chip = forte; /* FIXME: HACK FROM HELL! */ if (file->f_flags & O_NONBLOCK) { if (down_trylock (&chip->open_sem)) { DPRINTK ("%s: returning -EAGAIN\n", __FUNCTION__); return -EAGAIN; } } else { if (down_interruptible (&chip->open_sem)) { DPRINTK ("%s: returning -ERESTARTSYS\n", __FUNCTION__); return -ERESTARTSYS; } } file->private_data = forte; DPRINTK ("%s: dsp opened by %d\n", __FUNCTION__, current->pid); if (file->f_mode & FMODE_WRITE) forte_channel_init (forte, &forte->play); if (file->f_mode & FMODE_READ) forte_channel_init (forte, &forte->rec); return nonseekable_open(inode, file); } /** * forte_dsp_release: */ static int forte_dsp_release (struct inode *inode, struct file *file) { struct forte_chip *chip = file->private_data; int ret = 0; DPRINTK ("%s: chip @ %p\n", __FUNCTION__, chip); if (file->f_mode & FMODE_WRITE) { forte_channel_drain (&chip->play); spin_lock_irq (&chip->lock); forte_channel_free (chip, &chip->play); spin_unlock_irq (&chip->lock); } if (file->f_mode & FMODE_READ) { while (chip->rec.filled_frags > 0) interruptible_sleep_on (&chip->rec.wait); spin_lock_irq (&chip->lock); forte_channel_stop (&chip->rec); forte_channel_free (chip, &chip->rec); spin_unlock_irq (&chip->lock); } up (&chip->open_sem); return ret; } /** * forte_dsp_poll: * */ static unsigned int forte_dsp_poll (struct file *file, struct poll_table_struct *wait) { struct forte_chip *chip; struct forte_channel *channel; unsigned int mask = 0; chip = file->private_data; if (file->f_mode & FMODE_WRITE) { channel = &chip->play; if (channel->active) poll_wait (file, &channel->wait, wait); spin_lock_irq (&chip->lock); if (channel->frag_num - channel->filled_frags > 0) mask |= POLLOUT | POLLWRNORM; spin_unlock_irq (&chip->lock); } if (file->f_mode & FMODE_READ) { channel = &chip->rec; if (channel->active) poll_wait (file, &channel->wait, wait); spin_lock_irq (&chip->lock); if (channel->filled_frags > 0) mask |= POLLIN | POLLRDNORM; spin_unlock_irq (&chip->lock); } return mask; } /** * forte_dsp_mmap: */ static int forte_dsp_mmap (struct file *file, struct vm_area_struct *vma) { struct forte_chip *chip; struct forte_channel *channel; unsigned long size; int ret; chip = file->private_data; DPRINTK ("%s: start %lXh, size %ld, pgoff %ld\n", __FUNCTION__, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_pgoff); spin_lock_irq (&chip->lock); if (vma->vm_flags & VM_WRITE && chip->play.active) { ret = -EBUSY; goto out; } if (vma->vm_flags & VM_READ && chip->rec.active) { ret = -EBUSY; goto out; } if (file->f_mode & FMODE_WRITE) channel = &chip->play; else if (file->f_mode & FMODE_READ) channel = &chip->rec; else { ret = -EINVAL; goto out; } forte_channel_prep (channel); channel->mapped = 1; if (vma->vm_pgoff != 0) { ret = -EINVAL; goto out; } size = vma->vm_end - vma->vm_start; if (size > channel->buf_pages * PAGE_SIZE) { DPRINTK ("%s: size (%ld) > buf_sz (%d) \n", __FUNCTION__, size, channel->buf_sz); ret = -EINVAL; goto out; } if (remap_pfn_range(vma, vma->vm_start, virt_to_phys(channel->buf) >> PAGE_SHIFT, size, vma->vm_page_prot)) { DPRINTK ("%s: remap el a no worko\n", __FUNCTION__); ret = -EAGAIN; goto out; } ret = 0; out: spin_unlock_irq (&chip->lock); return ret; } /** * forte_dsp_write: */ static ssize_t forte_dsp_write (struct file *file, const char __user *buffer, size_t bytes, loff_t *ppos) { struct forte_chip *chip; struct forte_channel *channel; unsigned int i = bytes, sz = 0; unsigned long flags; if (!access_ok (VERIFY_READ, buffer, bytes)) return -EFAULT; chip = (struct forte_chip *) file->private_data; if (!chip) BUG(); channel = &chip->play; if (!channel) BUG(); spin_lock_irqsave (&chip->lock, flags); /* Set up buffers with the right fragment size */ forte_channel_prep (channel); while (i) { /* All fragment buffers in use -> wait */ if (channel->frag_num - channel->filled_frags == 0) { DECLARE_WAITQUEUE (wait, current); /* For trigger or non-blocking operation, get out */ if (chip->trigger || file->f_flags & O_NONBLOCK) { spin_unlock_irqrestore (&chip->lock, flags); return -EAGAIN; } /* Otherwise wait for buffers */ add_wait_queue (&channel->wait, &wait); for (;;) { spin_unlock_irqrestore (&chip->lock, flags); set_current_state (TASK_INTERRUPTIBLE); schedule(); spin_lock_irqsave (&chip->lock, flags); if (channel->frag_num - channel->filled_frags) break; } remove_wait_queue (&channel->wait, &wait); set_current_state (TASK_RUNNING); if (signal_pending (current)) { spin_unlock_irqrestore (&chip->lock, flags); return -ERESTARTSYS; } } if (channel->residue) sz = channel->residue; else if (i > channel->frag_sz) sz = channel->frag_sz; else sz = i; spin_unlock_irqrestore (&chip->lock, flags); if (copy_from_user ((void *) channel->buf + channel->swptr, buffer, sz)) return -EFAULT; spin_lock_irqsave (&chip->lock, flags); /* Advance software pointer */ buffer += sz; channel->swptr += sz; channel->swptr %= channel->buf_sz; i -= sz; /* Only bump filled_frags if a full fragment has been written */ if (channel->swptr % channel->frag_sz == 0) { channel->filled_frags++; channel->residue = 0; } else channel->residue = channel->frag_sz - sz; /* If playback isn't active, start it */ if (channel->active == 0 && chip->trigger == 0) forte_channel_start (channel); } spin_unlock_irqrestore (&chip->lock, flags); return bytes - i; } /** * forte_dsp_read: */ static ssize_t forte_dsp_read (struct file *file, char __user *buffer, size_t bytes, loff_t *ppos) { struct forte_chip *chip; struct forte_channel *channel; unsigned int i = bytes, sz; unsigned long flags; if (!access_ok (VERIFY_WRITE, buffer, bytes)) return -EFAULT; chip = (struct forte_chip *) file->private_data; if (!chip) BUG(); channel = &chip->rec; if (!channel) BUG(); spin_lock_irqsave (&chip->lock, flags); /* Set up buffers with the right fragment size */ forte_channel_prep (channel); /* Start recording */ if (!chip->trigger) forte_channel_start (channel); while (i) { /* No fragment buffers in use -> wait */ if (channel->filled_frags == 0) { DECLARE_WAITQUEUE (wait, current); /* For trigger mode operation, get out */ if (chip->trigger) { spin_unlock_irqrestore (&chip->lock, flags); return -EAGAIN; } add_wait_queue (&channel->wait, &wait); for (;;) { if (channel->active == 0) break; if (channel->filled_frags) break; spin_unlock_irqrestore (&chip->lock, flags); set_current_state (TASK_INTERRUPTIBLE); schedule(); spin_lock_irqsave (&chip->lock, flags); } set_current_state (TASK_RUNNING); remove_wait_queue (&channel->wait, &wait); } if (i > channel->frag_sz) sz = channel->frag_sz; else sz = i; spin_unlock_irqrestore (&chip->lock, flags); if (copy_to_user (buffer, (void *)channel->buf+channel->swptr, sz)) { DPRINTK ("%s: copy_to_user failed\n", __FUNCTION__); return -EFAULT; } spin_lock_irqsave (&chip->lock, flags); /* Advance software pointer */ buffer += sz; if (channel->filled_frags > 0) channel->filled_frags--; channel->swptr += channel->frag_sz; channel->swptr %= channel->buf_sz; i -= sz; } spin_unlock_irqrestore (&chip->lock, flags); return bytes - i; } static struct file_operations forte_dsp_fops = { .owner = THIS_MODULE, .llseek = &no_llseek, .read = &forte_dsp_read, .write = &forte_dsp_write, .poll = &forte_dsp_poll, .ioctl = &forte_dsp_ioctl, .open = &forte_dsp_open, .release = &forte_dsp_release, .mmap = &forte_dsp_mmap, }; /* Common ------------------------------------------------------------------ */ /** * forte_interrupt: */ static irqreturn_t forte_interrupt (int irq, void *dev_id, struct pt_regs *regs) { struct forte_chip *chip = dev_id; struct forte_channel *channel = NULL; u16 status, count; status = inw (chip->iobase + FORTE_IRQ_STATUS); /* If this is not for us, get outta here ASAP */ if ((status & (FORTE_IRQ_PLAYBACK | FORTE_IRQ_CAPTURE)) == 0) return IRQ_NONE; if (status & FORTE_IRQ_PLAYBACK) { channel = &chip->play; spin_lock (&chip->lock); if (channel->frag_sz == 0) goto pack; /* Declare a fragment done */ if (channel->filled_frags > 0) channel->filled_frags--; channel->bytes += channel->frag_sz; channel->nr_irqs++; /* Flip-flop between buffer I and II */ channel->next_buf ^= 1; /* Advance hardware pointer by fragment size and wrap around */ channel->hwptr += channel->frag_sz; channel->hwptr %= channel->buf_sz; /* Buffer I or buffer II BAR */ outl (channel->buf_handle + channel->hwptr, channel->next_buf == 0 ? channel->iobase + FORTE_PLY_BUF1 : channel->iobase + FORTE_PLY_BUF2); /* If the currently playing fragment is last, schedule pause */ if (channel->filled_frags == 1) forte_channel_pause (channel); pack: /* Acknowledge interrupt */ outw (FORTE_IRQ_PLAYBACK, chip->iobase + FORTE_IRQ_STATUS); if (waitqueue_active (&channel->wait)) wake_up_all (&channel->wait); spin_unlock (&chip->lock); } if (status & FORTE_IRQ_CAPTURE) { channel = &chip->rec; spin_lock (&chip->lock); /* One fragment filled */ channel->filled_frags++; /* Get # of completed bytes */ count = inw (channel->iobase + FORTE_PLY_COUNT) + 1; if (count == 0) { DPRINTK ("%s: last, filled_frags = %d\n", __FUNCTION__, channel->filled_frags); channel->filled_frags = 0; goto rack; } /* Buffer I or buffer II BAR */ outl (channel->buf_handle + channel->hwptr, channel->next_buf == 0 ? channel->iobase + FORTE_PLY_BUF1 : channel->iobase + FORTE_PLY_BUF2); /* Flip-flop between buffer I and II */ channel->next_buf ^= 1; /* Advance hardware pointer by fragment size and wrap around */ channel->hwptr += channel->frag_sz; channel->hwptr %= channel->buf_sz; /* Out of buffers */ if (channel->filled_frags == channel->frag_num - 1) forte_channel_stop (channel); rack: /* Acknowledge interrupt */ outw (FORTE_IRQ_CAPTURE, chip->iobase + FORTE_IRQ_STATUS); spin_unlock (&chip->lock); if (waitqueue_active (&channel->wait)) wake_up_all (&channel->wait); } return IRQ_HANDLED; } /** * forte_proc_read: */ static int forte_proc_read (char *page, char **start, off_t off, int count, int *eof, void *data) { int i = 0, p_rate, p_chan, r_rate; unsigned short p_reg, r_reg; i += sprintf (page, "ForteMedia FM801 OSS Lite driver\n%s\n \n", DRIVER_VERSION); if (!forte->iobase) return i; p_rate = p_chan = -1; p_reg = inw (forte->iobase + FORTE_PLY_CTRL); p_rate = (p_reg >> 8) & 15; p_chan = (p_reg >> 12) & 3; if (p_rate >= 0 || p_rate <= 10) p_rate = rates[p_rate]; if (p_chan >= 0 || p_chan <= 2) p_chan = channels[p_chan]; r_rate = -1; r_reg = inw (forte->iobase + FORTE_CAP_CTRL); r_rate = (r_reg >> 8) & 15; if (r_rate >= 0 || r_rate <= 10) r_rate = rates[r_rate]; i += sprintf (page + i, " Playback Capture\n" "FIFO empty : %-3s %-3s\n" "Buf1 Last : %-3s %-3s\n" "Buf2 Last : %-3s %-3s\n" "Started : %-3s %-3s\n" "Paused : %-3s %-3s\n" "Immed Stop : %-3s %-3s\n" "Rate : %-5d %-5d\n" "Channels : %-5d -\n" "16-bit : %-3s %-3s\n" "Stereo : %-3s %-3s\n" " \n" "Buffer Sz : %-6d %-6d\n" "Frag Sz : %-6d %-6d\n" "Frag Num : %-6d %-6d\n" "Frag msecs : %-6d %-6d\n" "Used Frags : %-6d %-6d\n" "Mapped : %-3s %-3s\n", p_reg & 1<<0 ? "yes" : "no", r_reg & 1<<0 ? "yes" : "no", p_reg & 1<<1 ? "yes" : "no", r_reg & 1<<1 ? "yes" : "no", p_reg & 1<<2 ? "yes" : "no", r_reg & 1<<2 ? "yes" : "no", p_reg & 1<<5 ? "yes" : "no", r_reg & 1<<5 ? "yes" : "no", p_reg & 1<<6 ? "yes" : "no", r_reg & 1<<6 ? "yes" : "no", p_reg & 1<<7 ? "yes" : "no", r_reg & 1<<7 ? "yes" : "no", p_rate, r_rate, p_chan, p_reg & 1<<14 ? "yes" : "no", r_reg & 1<<14 ? "yes" : "no", p_reg & 1<<15 ? "yes" : "no", r_reg & 1<<15 ? "yes" : "no", forte->play.buf_sz, forte->rec.buf_sz, forte->play.frag_sz, forte->rec.frag_sz, forte->play.frag_num, forte->rec.frag_num, forte->play.frag_msecs, forte->rec.frag_msecs, forte->play.filled_frags, forte->rec.filled_frags, forte->play.mapped ? "yes" : "no", forte->rec.mapped ? "yes" : "no" ); return i; } /** * forte_proc_init: * * Creates driver info entries in /proc */ static int __init forte_proc_init (void) { if (!proc_mkdir ("driver/forte", NULL)) return -EIO; if (!create_proc_read_entry ("driver/forte/chip", 0, NULL, forte_proc_read, forte)) { remove_proc_entry ("driver/forte", NULL); return -EIO; } if (!create_proc_read_entry("driver/forte/ac97", 0, NULL, ac97_read_proc, forte->ac97)) { remove_proc_entry ("driver/forte/chip", NULL); remove_proc_entry ("driver/forte", NULL); return -EIO; } return 0; } /** * forte_proc_remove: * * Removes driver info entries in /proc */ static void forte_proc_remove (void) { remove_proc_entry ("driver/forte/ac97", NULL); remove_proc_entry ("driver/forte/chip", NULL); remove_proc_entry ("driver/forte", NULL); } /** * forte_chip_init: * @chip: Chip instance to initialize * * Description: * Resets chip, configures codec and registers the driver with * the sound subsystem. * * Press and hold Start for 8 secs, then switch on Run * and hold for 4 seconds. Let go of Start. Numbers * assume a properly oiled TWG. */ static int __devinit forte_chip_init (struct forte_chip *chip) { u8 revision; u16 cmdw; struct ac97_codec *codec; pci_read_config_byte (chip->pci_dev, PCI_REVISION_ID, &revision); if (revision >= 0xB1) { chip->multichannel = 1; printk (KERN_INFO PFX "Multi-channel device detected.\n"); } /* Reset chip */ outw (FORTE_CC_CODEC_RESET | FORTE_CC_AC97_RESET, chip->iobase + FORTE_CODEC_CTRL); udelay(100); outw (0, chip->iobase + FORTE_CODEC_CTRL); /* Request read from AC97 */ outw (FORTE_AC97_READ | (0 << FORTE_AC97_ADDR_SHIFT), chip->iobase + FORTE_AC97_CMD); mdelay(750); if ((inw (chip->iobase + FORTE_AC97_CMD) & (3<<8)) != (1<<8)) { printk (KERN_INFO PFX "AC97 codec not responding"); return -EIO; } /* Init volume */ outw (0x0808, chip->iobase + FORTE_PCM_VOL); outw (0x9f1f, chip->iobase + FORTE_FM_VOL); outw (0x8808, chip->iobase + FORTE_I2S_VOL); /* I2S control - I2S mode */ outw (0x0003, chip->iobase + FORTE_I2S_MODE); /* Interrupt setup - unmask PLAYBACK & CAPTURE */ cmdw = inw (chip->iobase + FORTE_IRQ_MASK); cmdw &= ~0x0003; outw (cmdw, chip->iobase + FORTE_IRQ_MASK); /* Interrupt clear */ outw (FORTE_IRQ_PLAYBACK|FORTE_IRQ_CAPTURE, chip->iobase + FORTE_IRQ_STATUS); /* Set up the AC97 codec */ if ((codec = ac97_alloc_codec()) == NULL) return -ENOMEM; codec->private_data = chip; codec->codec_read = forte_ac97_read; codec->codec_write = forte_ac97_write; codec->id = 0; if (ac97_probe_codec (codec) == 0) { printk (KERN_ERR PFX "codec probe failed\n"); ac97_release_codec(codec); return -1; } /* Register mixer */ if ((codec->dev_mixer = register_sound_mixer (&forte_mixer_fops, -1)) < 0) { printk (KERN_ERR PFX "couldn't register mixer!\n"); ac97_release_codec(codec); return -1; } chip->ac97 = codec; /* Register DSP */ if ((chip->dsp = register_sound_dsp (&forte_dsp_fops, -1) ) < 0) { printk (KERN_ERR PFX "couldn't register dsp!\n"); return -1; } /* Register with /proc */ if (forte_proc_init()) { printk (KERN_ERR PFX "couldn't add entries to /proc!\n"); return -1; } return 0; } /** * forte_probe: * @pci_dev: PCI struct for probed device * @pci_id: * * Description: * Allocates chip instance, I/O region, and IRQ */ static int __init forte_probe (struct pci_dev *pci_dev, const struct pci_device_id *pci_id) { struct forte_chip *chip; int ret = 0; /* FIXME: Support more than one chip */ if (found++) return -EIO; /* Ignition */ if (pci_enable_device (pci_dev)) return -EIO; pci_set_master (pci_dev); /* Allocate chip instance and configure */ forte = (struct forte_chip *) kmalloc (sizeof (struct forte_chip), GFP_KERNEL); chip = forte; if (chip == NULL) { printk (KERN_WARNING PFX "Out of memory"); return -ENOMEM; } memset (chip, 0, sizeof (struct forte_chip)); chip->pci_dev = pci_dev; init_MUTEX(&chip->open_sem); spin_lock_init (&chip->lock); spin_lock_init (&chip->ac97_lock); if (! request_region (pci_resource_start (pci_dev, 0), pci_resource_len (pci_dev, 0), DRIVER_NAME)) { printk (KERN_WARNING PFX "Unable to reserve I/O space"); ret = -ENOMEM; goto error; } chip->iobase = pci_resource_start (pci_dev, 0); chip->irq = pci_dev->irq; if (request_irq (chip->irq, forte_interrupt, SA_SHIRQ, DRIVER_NAME, chip)) { printk (KERN_WARNING PFX "Unable to reserve IRQ"); ret = -EIO; goto error; } pci_set_drvdata (pci_dev, chip); printk (KERN_INFO PFX "FM801 chip found at 0x%04lX-0x%04lX IRQ %u\n", chip->iobase, pci_resource_end (pci_dev, 0), chip->irq); /* Power it up */ if ((ret = forte_chip_init (chip)) == 0) return 0; error: if (chip->irq) free_irq (chip->irq, chip); if (chip->iobase) release_region (pci_resource_start (pci_dev, 0), pci_resource_len (pci_dev, 0)); kfree (chip); return ret; } /** * forte_remove: * @pci_dev: PCI device to unclaim * */ static void forte_remove (struct pci_dev *pci_dev) { struct forte_chip *chip = pci_get_drvdata (pci_dev); if (chip == NULL) return; /* Turn volume down to avoid popping */ outw (0x1f1f, chip->iobase + FORTE_PCM_VOL); outw (0x1f1f, chip->iobase + FORTE_FM_VOL); outw (0x1f1f, chip->iobase + FORTE_I2S_VOL); forte_proc_remove(); free_irq (chip->irq, chip); release_region (chip->iobase, pci_resource_len (pci_dev, 0)); unregister_sound_dsp (chip->dsp); unregister_sound_mixer (chip->ac97->dev_mixer); ac97_release_codec(chip->ac97); kfree (chip); printk (KERN_INFO PFX "driver released\n"); } static struct pci_device_id forte_pci_ids[] = { { 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, { 0, } }; static struct pci_driver forte_pci_driver = { .name = DRIVER_NAME, .id_table = forte_pci_ids, .probe = forte_probe, .remove = forte_remove, }; /** * forte_init_module: * */ static int __init forte_init_module (void) { printk (KERN_INFO PFX DRIVER_VERSION "\n"); return pci_register_driver (&forte_pci_driver); } /** * forte_cleanup_module: * */ static void __exit forte_cleanup_module (void) { pci_unregister_driver (&forte_pci_driver); } module_init(forte_init_module); module_exit(forte_cleanup_module); MODULE_AUTHOR("Martin K. Petersen <mkp@mkp.net>"); MODULE_DESCRIPTION("ForteMedia FM801 OSS Driver"); MODULE_LICENSE("GPL"); MODULE_DEVICE_TABLE (pci, forte_pci_ids); |