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
|
/*****************************************************************************/
/*
* sonicvibes.c -- S3 Sonic Vibes audio driver.
*
* Copyright (C) 1998-1999 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.
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Special thanks to David C. Niemi
*
*
* Module command line parameters:
* none so far
*
*
* Supported devices:
* /dev/dsp standard /dev/dsp device, (mostly) OSS compatible
* /dev/mixer standard /dev/mixer device, (mostly) OSS compatible
* /dev/midi simple MIDI UART interface, no ioctl
*
* The card has both an FM and a Wavetable synth, but I have to figure
* out first how to drive them...
*
* Revision history
* 06.05.98 0.1 Initial release
* 10.05.98 0.2 Fixed many bugs, esp. ADC rate calculation
* First stab at a simple midi interface (no bells&whistles)
* 13.05.98 0.3 Fix stupid cut&paste error: set_adc_rate was called instead of
* set_dac_rate in the FMODE_WRITE case in sv_open
* Fix hwptr out of bounds (now mpg123 works)
* 14.05.98 0.4 Don't allow excessive interrupt rates
* 08.06.98 0.5 First release using Alan Cox' soundcore instead of miscdevice
* 03.08.98 0.6 Do not include modversions.h
* Now mixer behaviour can basically be selected between
* "OSS documented" and "OSS actual" behaviour
* 31.08.98 0.7 Fix realplayer problems - dac.count issues
* 10.12.98 0.8 Fix drain_dac trying to wait on not yet initialized DMA
* 16.12.98 0.9 Fix a few f_file & FMODE_ bugs
* 06.01.99 0.10 remove the silly SA_INTERRUPT flag.
* hopefully killed the egcs section type conflict
* 12.03.99 0.11 cinfo.blocks should be reset after GETxPTR ioctl.
* reported by Johan Maes <joma@telindus.be>
* 22.03.99 0.12 return EAGAIN instead of EBUSY when O_NONBLOCK
* read/write cannot be executed
* 05.04.99 0.13 added code to sv_read and sv_write which should detect
* lockups of the sound chip and revive it. This is basically
* an ugly hack, but at least applications using this driver
* won't hang forever. I don't know why these lockups happen,
* it might well be the motherboard chipset (an early 486 PCI
* board with ALI chipset), since every busmastering 100MB
* ethernet card I've tried (Realtek 8139 and Macronix tulip clone)
* exhibit similar behaviour (they work for a couple of packets
* and then lock up and can be revived by ifconfig down/up).
* 07.04.99 0.14 implemented the following ioctl's: SOUND_PCM_READ_RATE,
* SOUND_PCM_READ_CHANNELS, SOUND_PCM_READ_BITS;
* Alpha fixes reported by Peter Jones <pjones@redhat.com>
* Note: dmaio hack might still be wrong on archs other than i386
*
*/
/*****************************************************************************/
#include <linux/version.h>
#include <linux/module.h>
#include <linux/string.h>
#include <linux/ioport.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/sound.h>
#include <linux/malloc.h>
#include <linux/soundcard.h>
#include <linux/pci.h>
#include <asm/io.h>
#include <asm/dma.h>
#include <linux/init.h>
#include <linux/poll.h>
#include <asm/spinlock.h>
#include <asm/uaccess.h>
#include <asm/hardirq.h>
#include "dm.h"
/* --------------------------------------------------------------------- */
#undef OSS_DOCUMENTED_MIXER_SEMANTICS
/* --------------------------------------------------------------------- */
#ifndef PCI_VENDOR_ID_S3
#define PCI_VENDOR_ID_S3 0x5333
#endif
#ifndef PCI_DEVICE_ID_S3_SONICVIBES
#define PCI_DEVICE_ID_S3_SONICVIBES 0xca00
#endif
#define SV_MAGIC ((PCI_VENDOR_ID_S3<<16)|PCI_DEVICE_ID_S3_SONICVIBES)
#define SV_EXTENT_SB 0x10
#define SV_EXTENT_ENH 0x10
#define SV_EXTENT_SYNTH 0x4
#define SV_EXTENT_MIDI 0x4
#define SV_EXTENT_GAME 0x8
#define SV_EXTENT_DMA 0x10
#define SV_MIDI_DATA 0
#define SV_MIDI_COMMAND 1
#define SV_MIDI_STATUS 1
#define SV_DMA_ADDR0 0
#define SV_DMA_ADDR1 1
#define SV_DMA_ADDR2 2
#define SV_DMA_ADDR3 3
#define SV_DMA_COUNT0 4
#define SV_DMA_COUNT1 5
#define SV_DMA_COUNT2 6
#define SV_DMA_MODE 0xb
#define SV_DMA_RESET 0xd
#define SV_DMA_MASK 0xf
/*
* DONT reset the DMA controllers unless you understand
* the reset semantics. Assuming reset semantics as in
* the 8237 does not work.
*/
#define DMA_MODE_AUTOINIT 0x10
#define DMA_MODE_READ 0x44 /* I/O to memory, no autoinit, increment, single mode */
#define DMA_MODE_WRITE 0x48 /* memory to I/O, no autoinit, increment, single mode */
#define SV_CODEC_CONTROL 0
#define SV_CODEC_INTMASK 1
#define SV_CODEC_STATUS 2
#define SV_CODEC_IADDR 4
#define SV_CODEC_IDATA 5
#define SV_CCTRL_RESET 0x80
#define SV_CCTRL_INTADRIVE 0x20
#define SV_CCTRL_WAVETABLE 0x08
#define SV_CCTRL_REVERB 0x04
#define SV_CCTRL_ENHANCED 0x01
#define SV_CINTMASK_DMAA 0x01
#define SV_CINTMASK_DMAC 0x04
#define SV_CINTMASK_SPECIAL 0x08
#define SV_CINTMASK_UPDOWN 0x40
#define SV_CINTMASK_MIDI 0x80
#define SV_CSTAT_DMAA 0x01
#define SV_CSTAT_DMAC 0x04
#define SV_CSTAT_SPECIAL 0x08
#define SV_CSTAT_UPDOWN 0x40
#define SV_CSTAT_MIDI 0x80
#define SV_CIADDR_TRD 0x80
#define SV_CIADDR_MCE 0x40
/* codec indirect registers */
#define SV_CIMIX_ADCINL 0x00
#define SV_CIMIX_ADCINR 0x01
#define SV_CIMIX_AUX1INL 0x02
#define SV_CIMIX_AUX1INR 0x03
#define SV_CIMIX_CDINL 0x04
#define SV_CIMIX_CDINR 0x05
#define SV_CIMIX_LINEINL 0x06
#define SV_CIMIX_LINEINR 0x07
#define SV_CIMIX_MICIN 0x08
#define SV_CIMIX_SYNTHINL 0x0A
#define SV_CIMIX_SYNTHINR 0x0B
#define SV_CIMIX_AUX2INL 0x0C
#define SV_CIMIX_AUX2INR 0x0D
#define SV_CIMIX_ANALOGINL 0x0E
#define SV_CIMIX_ANALOGINR 0x0F
#define SV_CIMIX_PCMINL 0x10
#define SV_CIMIX_PCMINR 0x11
#define SV_CIGAMECONTROL 0x09
#define SV_CIDATAFMT 0x12
#define SV_CIENABLE 0x13
#define SV_CIUPDOWN 0x14
#define SV_CIREVISION 0x15
#define SV_CIADCOUTPUT 0x16
#define SV_CIDMAABASECOUNT1 0x18
#define SV_CIDMAABASECOUNT0 0x19
#define SV_CIDMACBASECOUNT1 0x1c
#define SV_CIDMACBASECOUNT0 0x1d
#define SV_CIPCMSR0 0x1e
#define SV_CIPCMSR1 0x1f
#define SV_CISYNTHSR0 0x20
#define SV_CISYNTHSR1 0x21
#define SV_CIADCCLKSOURCE 0x22
#define SV_CIADCALTSR 0x23
#define SV_CIADCPLLM 0x24
#define SV_CIADCPLLN 0x25
#define SV_CISYNTHPLLM 0x26
#define SV_CISYNTHPLLN 0x27
#define SV_CIUARTCONTROL 0x2a
#define SV_CIDRIVECONTROL 0x2b
#define SV_CISRSSPACE 0x2c
#define SV_CISRSCENTER 0x2d
#define SV_CIWAVETABLESRC 0x2e
#define SV_CIANALOGPWRDOWN 0x30
#define SV_CIDIGITALPWRDOWN 0x31
#define SV_CIMIX_ADCSRC_CD 0x20
#define SV_CIMIX_ADCSRC_DAC 0x40
#define SV_CIMIX_ADCSRC_AUX2 0x60
#define SV_CIMIX_ADCSRC_LINE 0x80
#define SV_CIMIX_ADCSRC_AUX1 0xa0
#define SV_CIMIX_ADCSRC_MIC 0xc0
#define SV_CIMIX_ADCSRC_MIXOUT 0xe0
#define SV_CIMIX_ADCSRC_MASK 0xe0
#define SV_CFMT_STEREO 0x01
#define SV_CFMT_16BIT 0x02
#define SV_CFMT_MASK 0x03
#define SV_CFMT_ASHIFT 0
#define SV_CFMT_CSHIFT 4
static const unsigned sample_size[] = { 1, 2, 2, 4 };
static const unsigned sample_shift[] = { 0, 1, 1, 2 };
#define SV_CENABLE_PPE 0x4
#define SV_CENABLE_RE 0x2
#define SV_CENABLE_PE 0x1
/* MIDI buffer sizes */
#define MIDIINBUF 256
#define MIDIOUTBUF 256
#define FMODE_MIDI_SHIFT 2
#define FMODE_MIDI_READ (FMODE_READ << FMODE_MIDI_SHIFT)
#define FMODE_MIDI_WRITE (FMODE_WRITE << FMODE_MIDI_SHIFT)
#define FMODE_DMFM 0x10
#define SND_DEV_DSP16 5
/* --------------------------------------------------------------------- */
struct sv_state {
/* magic */
unsigned int magic;
/* we keep sv cards in a linked list */
struct sv_state *next;
/* soundcore stuff */
int dev_audio;
int dev_mixer;
int dev_midi;
int dev_dmfm;
/* hardware resources */
unsigned long iosb, ioenh, iosynth, iomidi, iogame; /* long for SPARC */
unsigned int iodmaa, iodmac, irq;
/* mixer stuff */
struct {
unsigned int modcnt;
#ifndef OSS_DOCUMENTED_MIXER_SEMANTICS
unsigned short vol[13];
#endif /* OSS_DOCUMENTED_MIXER_SEMANTICS */
} mix;
/* wave stuff */
unsigned int rateadc, ratedac;
unsigned char fmt, enable;
spinlock_t lock;
struct semaphore open_sem;
mode_t open_mode;
wait_queue_head_t open_wait;
struct dmabuf {
void *rawbuf;
unsigned buforder;
unsigned numfrag;
unsigned fragshift;
unsigned hwptr, swptr;
unsigned total_bytes;
int count;
unsigned error; /* over/underrun */
wait_queue_head_t wait;
/* redundant, but makes calculations easier */
unsigned fragsize;
unsigned dmasize;
unsigned fragsamples;
/* OSS stuff */
unsigned mapped:1;
unsigned ready:1;
unsigned endcleared:1;
unsigned ossfragshift;
int ossmaxfrags;
unsigned subdivision;
} dma_dac, dma_adc;
/* midi stuff */
struct {
unsigned ird, iwr, icnt;
unsigned ord, owr, ocnt;
wait_queue_head_t iwait;
wait_queue_head_t owait;
struct timer_list timer;
unsigned char ibuf[MIDIINBUF];
unsigned char obuf[MIDIOUTBUF];
} midi;
};
/* --------------------------------------------------------------------- */
static struct sv_state *devs = NULL;
static unsigned long wavetable_mem = 0;
/* --------------------------------------------------------------------- */
extern __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;
}
/*
* hweightN: returns the hamming weight (i.e. the number
* of bits set) of a N-bit word
*/
#ifdef hweight32
#undef hweight32
#endif
extern __inline__ unsigned int hweight32(unsigned int w)
{
unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
}
/* --------------------------------------------------------------------- */
/*
* Why use byte IO? Nobody knows, but S3 does it also in their Windows driver.
*/
#undef DMABYTEIO
static void set_dmaa(struct sv_state *s, unsigned int addr, unsigned int count)
{
#ifdef DMABYTEIO
unsigned io = s->iodmaa, u;
count--;
for (u = 4; u > 0; u--, addr >>= 8, io++)
outb(addr & 0xff, io);
for (u = 3; u > 0; u--, count >>= 8, io++)
outb(count & 0xff, io);
#else /* DMABYTEIO */
count--;
outl(addr, s->iodmaa + SV_DMA_ADDR0);
outl(count, s->iodmaa + SV_DMA_COUNT0);
#endif /* DMABYTEIO */
outb(0x18, s->iodmaa + SV_DMA_MODE);
}
static void set_dmac(struct sv_state *s, unsigned int addr, unsigned int count)
{
#ifdef DMABYTEIO
unsigned io = s->iodmac, u;
count >>= 1;
count--;
for (u = 4; u > 0; u--, addr >>= 8, io++)
outb(addr & 0xff, io);
for (u = 3; u > 0; u--, count >>= 8, io++)
outb(count & 0xff, io);
#else /* DMABYTEIO */
count >>= 1;
count--;
outl(addr, s->iodmac + SV_DMA_ADDR0);
outl(count, s->iodmac + SV_DMA_COUNT0);
#endif /* DMABYTEIO */
outb(0x14, s->iodmac + SV_DMA_MODE);
}
extern __inline__ unsigned get_dmaa(struct sv_state *s)
{
#ifdef DMABYTEIO
unsigned io = s->iodmaa+6, v = 0, u;
for (u = 3; u > 0; u--, io--) {
v <<= 8;
v |= inb(io);
}
return v + 1;
#else /* DMABYTEIO */
return (inl(s->iodmaa + SV_DMA_COUNT0) & 0xffffff) + 1;
#endif /* DMABYTEIO */
}
extern __inline__ unsigned get_dmac(struct sv_state *s)
{
#ifdef DMABYTEIO
unsigned io = s->iodmac+6, v = 0, u;
for (u = 3; u > 0; u--, io--) {
v <<= 8;
v |= inb(io);
}
return (v + 1) << 1;
#else /* DMABYTEIO */
return ((inl(s->iodmac + SV_DMA_COUNT0) & 0xffffff) + 1) << 1;
#endif /* DMABYTEIO */
}
static void wrindir(struct sv_state *s, unsigned char idx, unsigned char data)
{
outb(idx & 0x3f, s->ioenh + SV_CODEC_IADDR);
udelay(10);
outb(data, s->ioenh + SV_CODEC_IDATA);
udelay(10);
}
static unsigned char rdindir(struct sv_state *s, unsigned char idx)
{
unsigned char v;
outb(idx & 0x3f, s->ioenh + SV_CODEC_IADDR);
udelay(10);
v = inb(s->ioenh + SV_CODEC_IDATA);
udelay(10);
return v;
}
static void set_fmt(struct sv_state *s, unsigned char mask, unsigned char data)
{
unsigned long flags;
spin_lock_irqsave(&s->lock, flags);
outb(SV_CIDATAFMT | SV_CIADDR_MCE, s->ioenh + SV_CODEC_IADDR);
if (mask) {
s->fmt = inb(s->ioenh + SV_CODEC_IDATA);
udelay(10);
}
s->fmt = (s->fmt & mask) | data;
outb(s->fmt, s->ioenh + SV_CODEC_IDATA);
udelay(10);
outb(0, s->ioenh + SV_CODEC_IADDR);
spin_unlock_irqrestore(&s->lock, flags);
udelay(10);
}
static void frobindir(struct sv_state *s, unsigned char idx, unsigned char mask, unsigned char data)
{
outb(idx & 0x3f, s->ioenh + SV_CODEC_IADDR);
udelay(10);
outb((inb(s->ioenh + SV_CODEC_IDATA) & mask) ^ data, s->ioenh + SV_CODEC_IDATA);
udelay(10);
}
#define REFFREQUENCY 24576000
#define ADCMULT 512
#define FULLRATE 48000
static unsigned setpll(struct sv_state *s, unsigned char reg, unsigned rate)
{
unsigned long flags;
unsigned char r, m, n;
unsigned xm, xn, xr, xd, metric = ~0U;
/* the warnings about m and n used uninitialized are bogus and may safely be ignored */
if (rate < 625000/ADCMULT)
rate = 625000/ADCMULT;
if (rate > 150000000/ADCMULT)
rate = 150000000/ADCMULT;
/* slight violation of specs, needed for continuous sampling rates */
for (r = 0; rate < 75000000/ADCMULT; r += 0x20, rate <<= 1);
for (xn = 3; xn < 35; xn++)
for (xm = 3; xm < 130; xm++) {
xr = REFFREQUENCY/ADCMULT * xm / xn;
xd = abs((signed)(xr - rate));
if (xd < metric) {
metric = xd;
m = xm - 2;
n = xn - 2;
}
}
reg &= 0x3f;
spin_lock_irqsave(&s->lock, flags);
outb(reg, s->ioenh + SV_CODEC_IADDR);
udelay(10);
outb(m, s->ioenh + SV_CODEC_IDATA);
udelay(10);
outb(reg+1, s->ioenh + SV_CODEC_IADDR);
udelay(10);
outb(r | n, s->ioenh + SV_CODEC_IDATA);
spin_unlock_irqrestore(&s->lock, flags);
udelay(10);
return (REFFREQUENCY/ADCMULT * (m + 2) / (n + 2)) >> ((r >> 5) & 7);
}
#if 0
static unsigned getpll(struct sv_state *s, unsigned char reg)
{
unsigned long flags;
unsigned char m, n;
reg &= 0x3f;
spin_lock_irqsave(&s->lock, flags);
outb(reg, s->ioenh + SV_CODEC_IADDR);
udelay(10);
m = inb(s->ioenh + SV_CODEC_IDATA);
udelay(10);
outb(reg+1, s->ioenh + SV_CODEC_IADDR);
udelay(10);
n = inb(s->ioenh + SV_CODEC_IDATA);
spin_unlock_irqrestore(&s->lock, flags);
udelay(10);
return (REFFREQUENCY/ADCMULT * (m + 2) / ((n & 0x1f) + 2)) >> ((n >> 5) & 7);
}
#endif
static void set_dac_rate(struct sv_state *s, unsigned rate)
{
unsigned div;
unsigned long flags;
if (rate > 48000)
rate = 48000;
if (rate < 4000)
rate = 4000;
div = (rate * 65536 + FULLRATE/2) / FULLRATE;
if (div > 65535)
div = 65535;
spin_lock_irqsave(&s->lock, flags);
wrindir(s, SV_CIPCMSR1, div >> 8);
wrindir(s, SV_CIPCMSR0, div);
spin_unlock_irqrestore(&s->lock, flags);
s->ratedac = (div * FULLRATE + 32768) / 65536;
}
static void set_adc_rate(struct sv_state *s, unsigned rate)
{
unsigned long flags;
unsigned rate1, rate2, div;
if (rate > 48000)
rate = 48000;
if (rate < 4000)
rate = 4000;
rate1 = setpll(s, SV_CIADCPLLM, rate);
div = (48000 + rate/2) / rate;
if (div > 8)
div = 8;
rate2 = (48000 + div/2) / div;
spin_lock_irqsave(&s->lock, flags);
wrindir(s, SV_CIADCALTSR, (div-1) << 4);
if (abs((signed)(rate-rate2)) <= abs((signed)(rate-rate1))) {
wrindir(s, SV_CIADCCLKSOURCE, 0x10);
s->rateadc = rate2;
} else {
wrindir(s, SV_CIADCCLKSOURCE, 0x00);
s->rateadc = rate1;
}
spin_unlock_irqrestore(&s->lock, flags);
}
/* --------------------------------------------------------------------- */
extern inline void stop_adc(struct sv_state *s)
{
unsigned long flags;
spin_lock_irqsave(&s->lock, flags);
s->enable &= ~SV_CENABLE_RE;
wrindir(s, SV_CIENABLE, s->enable);
spin_unlock_irqrestore(&s->lock, flags);
}
extern inline void stop_dac(struct sv_state *s)
{
unsigned long flags;
spin_lock_irqsave(&s->lock, flags);
s->enable &= ~(SV_CENABLE_PPE | SV_CENABLE_PE);
wrindir(s, SV_CIENABLE, s->enable);
spin_unlock_irqrestore(&s->lock, flags);
}
static void start_dac(struct sv_state *s)
{
unsigned long flags;
spin_lock_irqsave(&s->lock, flags);
if ((s->dma_dac.mapped || s->dma_dac.count > 0) && s->dma_dac.ready) {
s->enable = (s->enable & ~SV_CENABLE_PPE) | SV_CENABLE_PE;
wrindir(s, SV_CIENABLE, s->enable);
}
spin_unlock_irqrestore(&s->lock, flags);
}
static void start_adc(struct sv_state *s)
{
unsigned long flags;
spin_lock_irqsave(&s->lock, flags);
if ((s->dma_adc.mapped || s->dma_adc.count < (signed)(s->dma_adc.dmasize - 2*s->dma_adc.fragsize))
&& s->dma_adc.ready) {
s->enable |= SV_CENABLE_RE;
wrindir(s, SV_CIENABLE, s->enable);
}
spin_unlock_irqrestore(&s->lock, flags);
}
/* --------------------------------------------------------------------- */
#define DMABUF_DEFAULTORDER (17-PAGE_SHIFT)
#define DMABUF_MINORDER 1
static void dealloc_dmabuf(struct dmabuf *db)
{
unsigned long map, mapend;
if (db->rawbuf) {
/* undo marking the pages as reserved */
mapend = MAP_NR(db->rawbuf + (PAGE_SIZE << db->buforder) - 1);
for (map = MAP_NR(db->rawbuf); map <= mapend; map++)
clear_bit(PG_reserved, &mem_map[map].flags);
free_pages((unsigned long)db->rawbuf, db->buforder);
}
db->rawbuf = NULL;
db->mapped = db->ready = 0;
}
/* DMAA is used for playback, DMAC is used for recording */
static int prog_dmabuf(struct sv_state *s, unsigned rec)
{
struct dmabuf *db = rec ? &s->dma_adc : &s->dma_dac;
unsigned rate = rec ? s->rateadc : s->ratedac;
int order;
unsigned bytepersec;
unsigned bufs;
unsigned long map, mapend;
unsigned char fmt;
unsigned long flags;
spin_lock_irqsave(&s->lock, flags);
fmt = s->fmt;
if (rec) {
s->enable &= ~SV_CENABLE_RE;
fmt >>= SV_CFMT_CSHIFT;
} else {
s->enable &= ~SV_CENABLE_PE;
fmt >>= SV_CFMT_ASHIFT;
}
wrindir(s, SV_CIENABLE, s->enable);
spin_unlock_irqrestore(&s->lock, flags);
fmt &= SV_CFMT_MASK;
db->hwptr = db->swptr = db->total_bytes = db->count = db->error = db->endcleared = 0;
if (!db->rawbuf) {
db->ready = db->mapped = 0;
for (order = DMABUF_DEFAULTORDER; order >= DMABUF_MINORDER && !db->rawbuf; order--)
db->rawbuf = (void *)__get_free_pages(GFP_KERNEL | GFP_DMA, order);
if (!db->rawbuf)
return -ENOMEM;
db->buforder = order;
if ((virt_to_bus(db->rawbuf) ^ (virt_to_bus(db->rawbuf) + (PAGE_SIZE << db->buforder) - 1)) & ~0xffff)
printk(KERN_DEBUG "sv: DMA buffer crosses 64k boundary: busaddr 0x%lx size %ld\n",
virt_to_bus(db->rawbuf), PAGE_SIZE << db->buforder);
if ((virt_to_bus(db->rawbuf) + (PAGE_SIZE << db->buforder) - 1) & ~0xffffff)
printk(KERN_DEBUG "sv: DMA buffer beyond 16MB: busaddr 0x%lx size %ld\n",
virt_to_bus(db->rawbuf), PAGE_SIZE << db->buforder);
/* now mark the pages as reserved; otherwise remap_page_range doesn't do what we want */
mapend = MAP_NR(db->rawbuf + (PAGE_SIZE << db->buforder) - 1);
for (map = MAP_NR(db->rawbuf); map <= mapend; map++)
set_bit(PG_reserved, &mem_map[map].flags);
}
bytepersec = rate << sample_shift[fmt];
bufs = PAGE_SIZE << db->buforder;
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->fragsamples = db->fragsize >> sample_shift[fmt];
db->dmasize = db->numfrag << db->fragshift;
memset(db->rawbuf, (fmt & SV_CFMT_16BIT) ? 0 : 0x80, db->dmasize);
spin_lock_irqsave(&s->lock, flags);
if (rec) {
set_dmac(s, virt_to_bus(db->rawbuf), db->numfrag << db->fragshift);
/* program enhanced mode registers */
wrindir(s, SV_CIDMACBASECOUNT1, (db->fragsamples-1) >> 8);
wrindir(s, SV_CIDMACBASECOUNT0, db->fragsamples-1);
} else {
set_dmaa(s, virt_to_bus(db->rawbuf), db->numfrag << db->fragshift);
/* program enhanced mode registers */
wrindir(s, SV_CIDMAABASECOUNT1, (db->fragsamples-1) >> 8);
wrindir(s, SV_CIDMAABASECOUNT0, db->fragsamples-1);
}
spin_unlock_irqrestore(&s->lock, flags);
db->ready = 1;
return 0;
}
extern __inline__ void clear_advance(struct sv_state *s)
{
unsigned char c = (s->fmt & (SV_CFMT_16BIT << SV_CFMT_ASHIFT)) ? 0 : 0x80;
unsigned char *buf = s->dma_dac.rawbuf;
unsigned bsize = s->dma_dac.dmasize;
unsigned bptr = s->dma_dac.swptr;
unsigned len = s->dma_dac.fragsize;
if (bptr + len > bsize) {
unsigned x = bsize - bptr;
memset(buf + bptr, c, x);
bptr = 0;
len -= x;
}
memset(buf + bptr, c, len);
}
/* call with spinlock held! */
static void sv_update_ptr(struct sv_state *s)
{
unsigned hwptr;
int diff;
/* update ADC pointer */
if (s->dma_adc.ready) {
hwptr = (s->dma_adc.dmasize - get_dmac(s)) % s->dma_adc.dmasize;
diff = (s->dma_adc.dmasize + hwptr - s->dma_adc.hwptr) % s->dma_adc.dmasize;
s->dma_adc.hwptr = hwptr;
s->dma_adc.total_bytes += diff;
s->dma_adc.count += diff;
if (s->dma_adc.count >= (signed)s->dma_adc.fragsize)
wake_up(&s->dma_adc.wait);
if (!s->dma_adc.mapped) {
if (s->dma_adc.count > (signed)(s->dma_adc.dmasize - ((3 * s->dma_adc.fragsize) >> 1))) {
s->enable &= ~SV_CENABLE_RE;
wrindir(s, SV_CIENABLE, s->enable);
s->dma_adc.error++;
}
}
}
/* update DAC pointer */
if (s->dma_dac.ready) {
hwptr = (s->dma_dac.dmasize - get_dmaa(s)) % s->dma_dac.dmasize;
diff = (s->dma_dac.dmasize + hwptr - s->dma_dac.hwptr) % s->dma_dac.dmasize;
s->dma_dac.hwptr = hwptr;
s->dma_dac.total_bytes += diff;
if (s->dma_dac.mapped) {
s->dma_dac.count += diff;
if (s->dma_dac.count >= (signed)s->dma_dac.fragsize)
wake_up(&s->dma_dac.wait);
} else {
s->dma_dac.count -= diff;
if (s->dma_dac.count <= 0) {
s->enable &= ~SV_CENABLE_PE;
wrindir(s, SV_CIENABLE, s->enable);
s->dma_dac.error++;
} else if (s->dma_dac.count <= (signed)s->dma_dac.fragsize && !s->dma_dac.endcleared) {
clear_advance(s);
s->dma_dac.endcleared = 1;
}
if (s->dma_dac.count + (signed)s->dma_dac.fragsize <= (signed)s->dma_dac.dmasize)
wake_up(&s->dma_dac.wait);
}
}
}
/* hold spinlock for the following! */
static void sv_handle_midi(struct sv_state *s)
{
unsigned char ch;
int wake;
wake = 0;
while (!(inb(s->iomidi+1) & 0x80)) {
ch = inb(s->iomidi);
if (s->midi.icnt < MIDIINBUF) {
s->midi.ibuf[s->midi.iwr] = ch;
s->midi.iwr = (s->midi.iwr + 1) % MIDIINBUF;
s->midi.icnt++;
}
wake = 1;
}
if (wake)
wake_up(&s->midi.iwait);
wake = 0;
while (!(inb(s->iomidi+1) & 0x40) && s->midi.ocnt > 0) {
outb(s->midi.obuf[s->midi.ord], s->iomidi);
s->midi.ord = (s->midi.ord + 1) % MIDIOUTBUF;
s->midi.ocnt--;
if (s->midi.ocnt < MIDIOUTBUF-16)
wake = 1;
}
if (wake)
wake_up(&s->midi.owait);
}
static void sv_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
struct sv_state *s = (struct sv_state *)dev_id;
unsigned int intsrc;
/* fastpath out, to ease interrupt sharing */
intsrc = inb(s->ioenh + SV_CODEC_STATUS);
if (!(intsrc & (SV_CSTAT_DMAA | SV_CSTAT_DMAC | SV_CSTAT_MIDI)))
return;
spin_lock(&s->lock);
sv_update_ptr(s);
sv_handle_midi(s);
spin_unlock(&s->lock);
}
static void sv_midi_timer(unsigned long data)
{
struct sv_state *s = (struct sv_state *)data;
unsigned long flags;
spin_lock_irqsave(&s->lock, flags);
sv_handle_midi(s);
spin_unlock_irqrestore(&s->lock, flags);
s->midi.timer.expires = jiffies+1;
add_timer(&s->midi.timer);
}
/* --------------------------------------------------------------------- */
static const char invalid_magic[] = KERN_CRIT "sv: invalid magic value\n";
#define VALIDATE_STATE(s) \
({ \
if (!(s) || (s)->magic != SV_MAGIC) { \
printk(invalid_magic); \
return -ENXIO; \
} \
})
/* --------------------------------------------------------------------- */
#define MT_4 1
#define MT_5MUTE 2
#define MT_4MUTEMONO 3
#define MT_6MUTE 4
static const struct {
unsigned left:5;
unsigned right:5;
unsigned type:3;
unsigned rec:3;
} mixtable[SOUND_MIXER_NRDEVICES] = {
[SOUND_MIXER_RECLEV] = { SV_CIMIX_ADCINL, SV_CIMIX_ADCINR, MT_4, 0 },
[SOUND_MIXER_LINE1] = { SV_CIMIX_AUX1INL, SV_CIMIX_AUX1INR, MT_5MUTE, 5 },
[SOUND_MIXER_CD] = { SV_CIMIX_CDINL, SV_CIMIX_CDINR, MT_5MUTE, 1 },
[SOUND_MIXER_LINE] = { SV_CIMIX_LINEINL, SV_CIMIX_LINEINR, MT_5MUTE, 4 },
[SOUND_MIXER_MIC] = { SV_CIMIX_MICIN, SV_CIMIX_ADCINL, MT_4MUTEMONO, 6 },
[SOUND_MIXER_SYNTH] = { SV_CIMIX_SYNTHINL, SV_CIMIX_SYNTHINR, MT_5MUTE, 2 },
[SOUND_MIXER_LINE2] = { SV_CIMIX_AUX2INL, SV_CIMIX_AUX2INR, MT_5MUTE, 3 },
[SOUND_MIXER_VOLUME] = { SV_CIMIX_ANALOGINL, SV_CIMIX_ANALOGINR, MT_5MUTE, 7 },
[SOUND_MIXER_PCM] = { SV_CIMIX_PCMINL, SV_CIMIX_PCMINR, MT_6MUTE, 0 }
};
#ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
static int return_mixval(struct sv_state *s, unsigned i, int *arg)
{
unsigned long flags;
unsigned char l, r, rl, rr;
spin_lock_irqsave(&s->lock, flags);
l = rdindir(s, mixtable[i].left);
r = rdindir(s, mixtable[i].right);
spin_unlock_irqrestore(&s->lock, flags);
switch (mixtable[i].type) {
case MT_4:
r &= 0xf;
l &= 0xf;
rl = 10 + 6 * (l & 15);
rr = 10 + 6 * (r & 15);
break;
case MT_4MUTEMONO:
rl = 55 - 3 * (l & 15);
if (r & 0x10)
rl += 45;
rr = rl;
r = l;
break;
case MT_5MUTE:
default:
rl = 100 - 3 * (l & 31);
rr = 100 - 3 * (r & 31);
break;
case MT_6MUTE:
rl = 100 - 3 * (l & 63) / 2;
rr = 100 - 3 * (r & 63) / 2;
break;
}
if (l & 0x80)
rl = 0;
if (r & 0x80)
rr = 0;
return put_user((rr << 8) | rl, arg);
}
#else /* OSS_DOCUMENTED_MIXER_SEMANTICS */
static const unsigned char volidx[SOUND_MIXER_NRDEVICES] =
{
[SOUND_MIXER_RECLEV] = 1,
[SOUND_MIXER_LINE1] = 2,
[SOUND_MIXER_CD] = 3,
[SOUND_MIXER_LINE] = 4,
[SOUND_MIXER_MIC] = 5,
[SOUND_MIXER_SYNTH] = 6,
[SOUND_MIXER_LINE2] = 7,
[SOUND_MIXER_VOLUME] = 8,
[SOUND_MIXER_PCM] = 9
};
#endif /* OSS_DOCUMENTED_MIXER_SEMANTICS */
static unsigned mixer_recmask(struct sv_state *s)
{
unsigned long flags;
int i, j;
spin_lock_irqsave(&s->lock, flags);
j = rdindir(s, SV_CIMIX_ADCINL) >> 5;
spin_unlock_irqrestore(&s->lock, flags);
j &= 7;
for (i = 0; i < SOUND_MIXER_NRDEVICES && mixtable[i].rec != j; i++);
return 1 << i;
}
static int mixer_ioctl(struct sv_state *s, unsigned int cmd, unsigned long arg)
{
unsigned long flags;
int i, val;
unsigned char l, r, rl, rr;
VALIDATE_STATE(s);
if (cmd == SOUND_MIXER_INFO) {
mixer_info info;
strncpy(info.id, "SonicVibes", sizeof(info.id));
strncpy(info.name, "S3 SonicVibes", sizeof(info.name));
info.modify_counter = s->mix.modcnt;
if (copy_to_user((void *)arg, &info, sizeof(info)))
return -EFAULT;
return 0;
}
if (cmd == SOUND_OLD_MIXER_INFO) {
_old_mixer_info info;
strncpy(info.id, "SonicVibes", sizeof(info.id));
strncpy(info.name, "S3 SonicVibes", sizeof(info.name));
if (copy_to_user((void *)arg, &info, sizeof(info)))
return -EFAULT;
return 0;
}
if (cmd == OSS_GETVERSION)
return put_user(SOUND_VERSION, (int *)arg);
if (cmd == SOUND_MIXER_PRIVATE1) { /* SRS settings */
get_user_ret(val, (int *)arg, -EFAULT);
spin_lock_irqsave(&s->lock, flags);
if (val & 1) {
if (val & 2) {
l = 4 - ((val >> 2) & 7);
if (l & ~3)
l = 4;
r = 4 - ((val >> 5) & 7);
if (r & ~3)
r = 4;
wrindir(s, SV_CISRSSPACE, l);
wrindir(s, SV_CISRSCENTER, r);
} else
wrindir(s, SV_CISRSSPACE, 0x80);
}
l = rdindir(s, SV_CISRSSPACE);
r = rdindir(s, SV_CISRSCENTER);
spin_unlock_irqrestore(&s->lock, flags);
if (l & 0x80)
return put_user(0, (int *)arg);
return put_user(((4 - (l & 7)) << 2) | ((4 - (r & 7)) << 5) | 2, (int *)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 */
return put_user(mixer_recmask(s), (int *)arg);
case SOUND_MIXER_DEVMASK: /* Arg contains a bit for each supported device */
for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
if (mixtable[i].type)
val |= 1 << i;
return put_user(val, (int *)arg);
case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */
for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
if (mixtable[i].rec)
val |= 1 << i;
return put_user(val, (int *)arg);
case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */
for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
if (mixtable[i].type && mixtable[i].type != MT_4MUTEMONO)
val |= 1 << i;
return put_user(val, (int *)arg);
case SOUND_MIXER_CAPS:
return put_user(SOUND_CAP_EXCL_INPUT, (int *)arg);
default:
i = _IOC_NR(cmd);
if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].type)
return -EINVAL;
#ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
return return_mixval(s, i, (int *)arg);
#else /* OSS_DOCUMENTED_MIXER_SEMANTICS */
if (!volidx[i])
return -EINVAL;
return put_user(s->mix.vol[volidx[i]-1], (int *)arg);
#endif /* OSS_DOCUMENTED_MIXER_SEMANTICS */
}
}
if (_IOC_DIR(cmd) != (_IOC_READ|_IOC_WRITE))
return -EINVAL;
s->mix.modcnt++;
switch (_IOC_NR(cmd)) {
case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
get_user_ret(val, (int *)arg, -EFAULT);
i = hweight32(val);
if (i == 0)
return 0; /*val = mixer_recmask(s);*/
else if (i > 1)
val &= ~mixer_recmask(s);
for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
if (!(val & (1 << i)))
continue;
if (mixtable[i].rec)
break;
}
if (!mixtable[i].rec)
return 0;
spin_lock_irqsave(&s->lock, flags);
frobindir(s, SV_CIMIX_ADCINL, 0x1f, mixtable[i].rec << 5);
frobindir(s, SV_CIMIX_ADCINR, 0x1f, mixtable[i].rec << 5);
spin_unlock_irqrestore(&s->lock, flags);
return 0;
default:
i = _IOC_NR(cmd);
if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].type)
return -EINVAL;
get_user_ret(val, (int *)arg, -EFAULT);
l = val & 0xff;
r = (val >> 8) & 0xff;
if (mixtable[i].type == MT_4MUTEMONO)
l = (r + l) / 2;
if (l > 100)
l = 100;
if (r > 100)
r = 100;
spin_lock_irqsave(&s->lock, flags);
switch (mixtable[i].type) {
case MT_4:
if (l >= 10)
l -= 10;
if (r >= 10)
r -= 10;
frobindir(s, mixtable[i].left, 0xf0, l / 6);
frobindir(s, mixtable[i].right, 0xf0, l / 6);
break;
case MT_4MUTEMONO:
rr = 0;
if (l < 10)
rl = 0x80;
else {
if (l >= 55) {
rr = 0x10;
l -= 45;
}
rl = (55 - l) / 3;
}
wrindir(s, mixtable[i].left, rl);
frobindir(s, mixtable[i].right, ~0x10, rr);
break;
case MT_5MUTE:
if (l < 7)
rl = 0x80;
else
rl = (100 - l) / 3;
if (r < 7)
rr = 0x80;
else
rr = (100 - r) / 3;
wrindir(s, mixtable[i].left, rl);
wrindir(s, mixtable[i].right, rr);
break;
case MT_6MUTE:
if (l < 6)
rl = 0x80;
else
rl = (100 - l) * 2 / 3;
if (r < 6)
rr = 0x80;
else
rr = (100 - r) * 2 / 3;
wrindir(s, mixtable[i].left, rl);
wrindir(s, mixtable[i].right, rr);
break;
}
spin_unlock_irqrestore(&s->lock, flags);
#ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
return return_mixval(s, i, (int *)arg);
#else /* OSS_DOCUMENTED_MIXER_SEMANTICS */
if (!volidx[i])
return -EINVAL;
s->mix.vol[volidx[i]-1] = val;
return put_user(s->mix.vol[volidx[i]-1], (int *)arg);
#endif /* OSS_DOCUMENTED_MIXER_SEMANTICS */
}
}
/* --------------------------------------------------------------------- */
static loff_t sv_llseek(struct file *file, loff_t offset, int origin)
{
return -ESPIPE;
}
/* --------------------------------------------------------------------- */
static int sv_open_mixdev(struct inode *inode, struct file *file)
{
int minor = MINOR(inode->i_rdev);
struct sv_state *s = devs;
while (s && s->dev_mixer != minor)
s = s->next;
if (!s)
return -ENODEV;
VALIDATE_STATE(s);
file->private_data = s;
MOD_INC_USE_COUNT;
return 0;
}
static int sv_release_mixdev(struct inode *inode, struct file *file)
{
struct sv_state *s = (struct sv_state *)file->private_data;
VALIDATE_STATE(s);
MOD_DEC_USE_COUNT;
return 0;
}
static int sv_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
return mixer_ioctl((struct sv_state *)file->private_data, cmd, arg);
}
static /*const*/ struct file_operations sv_mixer_fops = {
&sv_llseek,
NULL, /* read */
NULL, /* write */
NULL, /* readdir */
NULL, /* poll */
&sv_ioctl_mixdev,
NULL, /* mmap */
&sv_open_mixdev,
NULL, /* flush */
&sv_release_mixdev,
NULL, /* fsync */
NULL, /* fasync */
NULL, /* check_media_change */
NULL, /* revalidate */
NULL, /* lock */
};
/* --------------------------------------------------------------------- */
static int drain_dac(struct sv_state *s, int nonblock)
{
DECLARE_WAITQUEUE(wait, current);
unsigned long flags;
int count, tmo;
if (s->dma_dac.mapped || !s->dma_dac.ready)
return 0;
current->state = TASK_INTERRUPTIBLE;
add_wait_queue(&s->dma_dac.wait, &wait);
for (;;) {
spin_lock_irqsave(&s->lock, flags);
count = s->dma_dac.count;
spin_unlock_irqrestore(&s->lock, flags);
if (count <= 0)
break;
if (signal_pending(current))
break;
if (nonblock) {
remove_wait_queue(&s->dma_dac.wait, &wait);
current->state = TASK_RUNNING;
return -EBUSY;
}
tmo = (count * HZ) / s->ratedac;
tmo >>= sample_shift[(s->fmt >> SV_CFMT_ASHIFT) & SV_CFMT_MASK];
if (!schedule_timeout(tmo ? : 1) && tmo)
printk(KERN_DEBUG "sv: dma timed out??\n");
}
remove_wait_queue(&s->dma_dac.wait, &wait);
current->state = TASK_RUNNING;
if (signal_pending(current))
return -ERESTARTSYS;
return 0;
}
/* --------------------------------------------------------------------- */
static ssize_t sv_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
{
struct sv_state *s = (struct sv_state *)file->private_data;
ssize_t ret;
unsigned long flags;
unsigned swptr;
int cnt;
VALIDATE_STATE(s);
if (ppos != &file->f_pos)
return -ESPIPE;
if (s->dma_adc.mapped)
return -ENXIO;
if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
return ret;
if (!access_ok(VERIFY_WRITE, buffer, count))
return -EFAULT;
ret = 0;
#if 0
spin_lock_irqsave(&s->lock, flags);
sv_update_ptr(s);
spin_unlock_irqrestore(&s->lock, flags);
#endif
while (count > 0) {
spin_lock_irqsave(&s->lock, flags);
swptr = s->dma_adc.swptr;
cnt = s->dma_adc.dmasize-swptr;
if (s->dma_adc.count < cnt)
cnt = s->dma_adc.count;
spin_unlock_irqrestore(&s->lock, flags);
if (cnt > count)
cnt = count;
if (cnt <= 0) {
start_adc(s);
if (file->f_flags & O_NONBLOCK)
return ret ? ret : -EAGAIN;
if (!interruptible_sleep_on_timeout(&s->dma_adc.wait, HZ)) {
printk(KERN_DEBUG "sv: read: chip lockup? dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
s->dma_adc.dmasize, s->dma_adc.fragsize, s->dma_adc.count,
s->dma_adc.hwptr, s->dma_adc.swptr);
stop_adc(s);
spin_lock_irqsave(&s->lock, flags);
set_dmac(s, virt_to_bus(s->dma_adc.rawbuf), s->dma_adc.numfrag << s->dma_adc.fragshift);
/* program enhanced mode registers */
wrindir(s, SV_CIDMACBASECOUNT1, (s->dma_adc.fragsamples-1) >> 8);
wrindir(s, SV_CIDMACBASECOUNT0, s->dma_adc.fragsamples-1);
s->dma_adc.count = s->dma_adc.hwptr = s->dma_adc.swptr = 0;
spin_unlock_irqrestore(&s->lock, flags);
}
if (signal_pending(current))
return ret ? ret : -ERESTARTSYS;
continue;
}
if (copy_to_user(buffer, s->dma_adc.rawbuf + swptr, cnt))
return ret ? ret : -EFAULT;
swptr = (swptr + cnt) % s->dma_adc.dmasize;
spin_lock_irqsave(&s->lock, flags);
s->dma_adc.swptr = swptr;
s->dma_adc.count -= cnt;
spin_unlock_irqrestore(&s->lock, flags);
count -= cnt;
buffer += cnt;
ret += cnt;
start_adc(s);
}
return ret;
}
static ssize_t sv_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
{
struct sv_state *s = (struct sv_state *)file->private_data;
ssize_t ret;
unsigned long flags;
unsigned swptr;
int cnt;
VALIDATE_STATE(s);
if (ppos != &file->f_pos)
return -ESPIPE;
if (s->dma_dac.mapped)
return -ENXIO;
if (!s->dma_dac.ready && (ret = prog_dmabuf(s, 0)))
return ret;
if (!access_ok(VERIFY_READ, buffer, count))
return -EFAULT;
ret = 0;
#if 0
spin_lock_irqsave(&s->lock, flags);
sv_update_ptr(s);
spin_unlock_irqrestore(&s->lock, flags);
#endif
while (count > 0) {
spin_lock_irqsave(&s->lock, flags);
if (s->dma_dac.count < 0) {
s->dma_dac.count = 0;
s->dma_dac.swptr = s->dma_dac.hwptr;
}
swptr = s->dma_dac.swptr;
cnt = s->dma_dac.dmasize-swptr;
if (s->dma_dac.count + cnt > s->dma_dac.dmasize)
cnt = s->dma_dac.dmasize - s->dma_dac.count;
spin_unlock_irqrestore(&s->lock, flags);
if (cnt > count)
cnt = count;
if (cnt <= 0) {
start_dac(s);
if (file->f_flags & O_NONBLOCK)
return ret ? ret : -EAGAIN;
if (!interruptible_sleep_on_timeout(&s->dma_dac.wait, HZ)) {
printk(KERN_DEBUG "sv: write: chip lockup? dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
s->dma_dac.dmasize, s->dma_dac.fragsize, s->dma_dac.count,
s->dma_dac.hwptr, s->dma_dac.swptr);
stop_dac(s);
spin_lock_irqsave(&s->lock, flags);
set_dmaa(s, virt_to_bus(s->dma_dac.rawbuf), s->dma_dac.numfrag << s->dma_dac.fragshift);
/* program enhanced mode registers */
wrindir(s, SV_CIDMAABASECOUNT1, (s->dma_dac.fragsamples-1) >> 8);
wrindir(s, SV_CIDMAABASECOUNT0, s->dma_dac.fragsamples-1);
s->dma_dac.count = s->dma_dac.hwptr = s->dma_dac.swptr = 0;
spin_unlock_irqrestore(&s->lock, flags);
}
if (signal_pending(current))
return ret ? ret : -ERESTARTSYS;
continue;
}
if (copy_from_user(s->dma_dac.rawbuf + swptr, buffer, cnt))
return ret ? ret : -EFAULT;
swptr = (swptr + cnt) % s->dma_dac.dmasize;
spin_lock_irqsave(&s->lock, flags);
s->dma_dac.swptr = swptr;
s->dma_dac.count += cnt;
s->dma_dac.endcleared = 0;
spin_unlock_irqrestore(&s->lock, flags);
count -= cnt;
buffer += cnt;
ret += cnt;
start_dac(s);
}
return ret;
}
static unsigned int sv_poll(struct file *file, struct poll_table_struct *wait)
{
struct sv_state *s = (struct sv_state *)file->private_data;
unsigned long flags;
unsigned int mask = 0;
VALIDATE_STATE(s);
if (file->f_mode & FMODE_WRITE)
poll_wait(file, &s->dma_dac.wait, wait);
if (file->f_mode & FMODE_READ)
poll_wait(file, &s->dma_adc.wait, wait);
spin_lock_irqsave(&s->lock, flags);
sv_update_ptr(s);
if (file->f_mode & FMODE_READ) {
if (s->dma_adc.count >= (signed)s->dma_adc.fragsize)
mask |= POLLIN | POLLRDNORM;
}
if (file->f_mode & FMODE_WRITE) {
if (s->dma_dac.mapped) {
if (s->dma_dac.count >= (signed)s->dma_dac.fragsize)
mask |= POLLOUT | POLLWRNORM;
} else {
if ((signed)s->dma_dac.dmasize >= s->dma_dac.count + (signed)s->dma_dac.fragsize)
mask |= POLLOUT | POLLWRNORM;
}
}
spin_unlock_irqrestore(&s->lock, flags);
return mask;
}
static int sv_mmap(struct file *file, struct vm_area_struct *vma)
{
struct sv_state *s = (struct sv_state *)file->private_data;
struct dmabuf *db;
int ret;
unsigned long size;
VALIDATE_STATE(s);
if (vma->vm_flags & VM_WRITE) {
if ((ret = prog_dmabuf(s, 1)) != 0)
return ret;
db = &s->dma_dac;
} else if (vma->vm_flags & VM_READ) {
if ((ret = prog_dmabuf(s, 0)) != 0)
return ret;
db = &s->dma_adc;
} else
return -EINVAL;
if (vma->vm_offset != 0)
return -EINVAL;
size = vma->vm_end - vma->vm_start;
if (size > (PAGE_SIZE << db->buforder))
return -EINVAL;
if (remap_page_range(vma->vm_start, virt_to_phys(db->rawbuf), size, vma->vm_page_prot))
return -EAGAIN;
db->mapped = 1;
return 0;
}
static int sv_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
struct sv_state *s = (struct sv_state *)file->private_data;
unsigned long flags;
audio_buf_info abinfo;
count_info cinfo;
int val, mapped, ret;
unsigned char fmtm, fmtd;
VALIDATE_STATE(s);
mapped = ((file->f_mode & FMODE_WRITE) && s->dma_dac.mapped) ||
((file->f_mode & FMODE_READ) && s->dma_adc.mapped);
switch (cmd) {
case OSS_GETVERSION:
return put_user(SOUND_VERSION, (int *)arg);
case SNDCTL_DSP_SYNC:
if (file->f_mode & FMODE_WRITE)
return drain_dac(s, 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, (int *)arg);
case SNDCTL_DSP_RESET:
if (file->f_mode & FMODE_WRITE) {
stop_dac(s);
synchronize_irq();
s->dma_dac.swptr = s->dma_dac.hwptr = s->dma_dac.count = s->dma_dac.total_bytes = 0;
}
if (file->f_mode & FMODE_READ) {
stop_adc(s);
synchronize_irq();
s->dma_adc.swptr = s->dma_adc.hwptr = s->dma_adc.count = s->dma_adc.total_bytes = 0;
}
return 0;
case SNDCTL_DSP_SPEED:
get_user_ret(val, (int *)arg, -EFAULT);
if (val >= 0) {
if (file->f_mode & FMODE_READ) {
stop_adc(s);
s->dma_adc.ready = 0;
set_adc_rate(s, val);
}
if (file->f_mode & FMODE_WRITE) {
stop_dac(s);
s->dma_dac.ready = 0;
set_dac_rate(s, val);
}
}
return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, (int *)arg);
case SNDCTL_DSP_STEREO:
get_user_ret(val, (int *)arg, -EFAULT);
fmtd = 0;
fmtm = ~0;
if (file->f_mode & FMODE_READ) {
stop_adc(s);
s->dma_adc.ready = 0;
if (val)
fmtd |= SV_CFMT_STEREO << SV_CFMT_CSHIFT;
else
fmtm &= ~(SV_CFMT_STEREO << SV_CFMT_CSHIFT);
}
if (file->f_mode & FMODE_WRITE) {
stop_dac(s);
s->dma_dac.ready = 0;
if (val)
fmtd |= SV_CFMT_STEREO << SV_CFMT_ASHIFT;
else
fmtm &= ~(SV_CFMT_STEREO << SV_CFMT_ASHIFT);
}
set_fmt(s, fmtm, fmtd);
return 0;
case SNDCTL_DSP_CHANNELS:
get_user_ret(val, (int *)arg, -EFAULT);
if (val != 0) {
fmtd = 0;
fmtm = ~0;
if (file->f_mode & FMODE_READ) {
stop_adc(s);
s->dma_adc.ready = 0;
if (val >= 2)
fmtd |= SV_CFMT_STEREO << SV_CFMT_CSHIFT;
else
fmtm &= ~(SV_CFMT_STEREO << SV_CFMT_CSHIFT);
}
if (file->f_mode & FMODE_WRITE) {
stop_dac(s);
s->dma_dac.ready = 0;
if (val >= 2)
fmtd |= SV_CFMT_STEREO << SV_CFMT_ASHIFT;
else
fmtm &= ~(SV_CFMT_STEREO << SV_CFMT_ASHIFT);
}
set_fmt(s, fmtm, fmtd);
}
return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (SV_CFMT_STEREO << SV_CFMT_CSHIFT)
: (SV_CFMT_STEREO << SV_CFMT_ASHIFT))) ? 2 : 1, (int *)arg);
case SNDCTL_DSP_GETFMTS: /* Returns a mask */
return put_user(AFMT_S16_LE|AFMT_U8, (int *)arg);
case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
get_user_ret(val, (int *)arg, -EFAULT);
if (val != AFMT_QUERY) {
fmtd = 0;
fmtm = ~0;
if (file->f_mode & FMODE_READ) {
stop_adc(s);
s->dma_adc.ready = 0;
if (val == AFMT_S16_LE)
fmtd |= SV_CFMT_16BIT << SV_CFMT_CSHIFT;
else
fmtm &= ~(SV_CFMT_16BIT << SV_CFMT_CSHIFT);
}
if (file->f_mode & FMODE_WRITE) {
stop_dac(s);
s->dma_dac.ready = 0;
if (val == AFMT_S16_LE)
fmtd |= SV_CFMT_16BIT << SV_CFMT_ASHIFT;
else
fmtm &= ~(SV_CFMT_16BIT << SV_CFMT_ASHIFT);
}
set_fmt(s, fmtm, fmtd);
}
return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (SV_CFMT_16BIT << SV_CFMT_CSHIFT)
: (SV_CFMT_16BIT << SV_CFMT_ASHIFT))) ? AFMT_S16_LE : AFMT_U8, (int *)arg);
case SNDCTL_DSP_POST:
return 0;
case SNDCTL_DSP_GETTRIGGER:
val = 0;
if (file->f_mode & FMODE_READ && s->enable & SV_CENABLE_RE)
val |= PCM_ENABLE_INPUT;
if (file->f_mode & FMODE_WRITE && s->enable & SV_CENABLE_PE)
val |= PCM_ENABLE_OUTPUT;
return put_user(val, (int *)arg);
case SNDCTL_DSP_SETTRIGGER:
get_user_ret(val, (int *)arg, -EFAULT);
if (file->f_mode & FMODE_READ) {
if (val & PCM_ENABLE_INPUT) {
if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
return ret;
start_adc(s);
} else
stop_adc(s);
}
if (file->f_mode & FMODE_WRITE) {
if (val & PCM_ENABLE_OUTPUT) {
if (!s->dma_dac.ready && (ret = prog_dmabuf(s, 0)))
return ret;
start_dac(s);
} else
stop_dac(s);
}
return 0;
case SNDCTL_DSP_GETOSPACE:
if (!(file->f_mode & FMODE_WRITE))
return -EINVAL;
if (!(s->enable & SV_CENABLE_PE) && (val = prog_dmabuf(s, 0)) != 0)
return val;
spin_lock_irqsave(&s->lock, flags);
sv_update_ptr(s);
abinfo.fragsize = s->dma_dac.fragsize;
abinfo.bytes = s->dma_dac.dmasize - s->dma_dac.count;
abinfo.fragstotal = s->dma_dac.numfrag;
abinfo.fragments = abinfo.bytes >> s->dma_dac.fragshift;
spin_unlock_irqrestore(&s->lock, flags);
return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
case SNDCTL_DSP_GETISPACE:
if (!(file->f_mode & FMODE_READ))
return -EINVAL;
if (!(s->enable & SV_CENABLE_RE) && (val = prog_dmabuf(s, 1)) != 0)
return val;
spin_lock_irqsave(&s->lock, flags);
sv_update_ptr(s);
abinfo.fragsize = s->dma_adc.fragsize;
abinfo.bytes = s->dma_adc.count;
abinfo.fragstotal = s->dma_adc.numfrag;
abinfo.fragments = abinfo.bytes >> s->dma_adc.fragshift;
spin_unlock_irqrestore(&s->lock, flags);
return copy_to_user((void *)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(&s->lock, flags);
sv_update_ptr(s);
val = s->dma_dac.count;
spin_unlock_irqrestore(&s->lock, flags);
return put_user(val, (int *)arg);
case SNDCTL_DSP_GETIPTR:
if (!(file->f_mode & FMODE_READ))
return -EINVAL;
spin_lock_irqsave(&s->lock, flags);
sv_update_ptr(s);
cinfo.bytes = s->dma_adc.total_bytes;
cinfo.blocks = s->dma_adc.count >> s->dma_adc.fragshift;
cinfo.ptr = s->dma_adc.hwptr;
if (s->dma_adc.mapped)
s->dma_adc.count &= s->dma_adc.fragsize-1;
spin_unlock_irqrestore(&s->lock, flags);
return copy_to_user((void *)arg, &cinfo, sizeof(cinfo));
case SNDCTL_DSP_GETOPTR:
if (!(file->f_mode & FMODE_WRITE))
return -EINVAL;
spin_lock_irqsave(&s->lock, flags);
sv_update_ptr(s);
cinfo.bytes = s->dma_dac.total_bytes;
cinfo.blocks = s->dma_dac.count >> s->dma_dac.fragshift;
cinfo.ptr = s->dma_dac.hwptr;
if (s->dma_dac.mapped)
s->dma_dac.count &= s->dma_dac.fragsize-1;
spin_unlock_irqrestore(&s->lock, flags);
return copy_to_user((void *)arg, &cinfo, sizeof(cinfo));
case SNDCTL_DSP_GETBLKSIZE:
if (file->f_mode & FMODE_WRITE) {
if ((val = prog_dmabuf(s, 0)))
return val;
return put_user(s->dma_dac.fragsize, (int *)arg);
}
if ((val = prog_dmabuf(s, 1)))
return val;
return put_user(s->dma_adc.fragsize, (int *)arg);
case SNDCTL_DSP_SETFRAGMENT:
get_user_ret(val, (int *)arg, -EFAULT);
if (file->f_mode & FMODE_READ) {
s->dma_adc.ossfragshift = val & 0xffff;
s->dma_adc.ossmaxfrags = (val >> 16) & 0xffff;
if (s->dma_adc.ossfragshift < 4)
s->dma_adc.ossfragshift = 4;
if (s->dma_adc.ossfragshift > 15)
s->dma_adc.ossfragshift = 15;
if (s->dma_adc.ossmaxfrags < 4)
s->dma_adc.ossmaxfrags = 4;
}
if (file->f_mode & FMODE_WRITE) {
s->dma_dac.ossfragshift = val & 0xffff;
s->dma_dac.ossmaxfrags = (val >> 16) & 0xffff;
if (s->dma_dac.ossfragshift < 4)
s->dma_dac.ossfragshift = 4;
if (s->dma_dac.ossfragshift > 15)
s->dma_dac.ossfragshift = 15;
if (s->dma_dac.ossmaxfrags < 4)
s->dma_dac.ossmaxfrags = 4;
}
return 0;
case SNDCTL_DSP_SUBDIVIDE:
if ((file->f_mode & FMODE_READ && s->dma_adc.subdivision) ||
(file->f_mode & FMODE_WRITE && s->dma_dac.subdivision))
return -EINVAL;
get_user_ret(val, (int *)arg, -EFAULT);
if (val != 1 && val != 2 && val != 4)
return -EINVAL;
if (file->f_mode & FMODE_READ)
s->dma_adc.subdivision = val;
if (file->f_mode & FMODE_WRITE)
s->dma_dac.subdivision = val;
return 0;
case SOUND_PCM_READ_RATE:
return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, (int *)arg);
case SOUND_PCM_READ_CHANNELS:
return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (SV_CFMT_STEREO << SV_CFMT_CSHIFT)
: (SV_CFMT_STEREO << SV_CFMT_ASHIFT))) ? 2 : 1, (int *)arg);
case SOUND_PCM_READ_BITS:
return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (SV_CFMT_16BIT << SV_CFMT_CSHIFT)
: (SV_CFMT_16BIT << SV_CFMT_ASHIFT))) ? 16 : 8, (int *)arg);
case SOUND_PCM_WRITE_FILTER:
case SNDCTL_DSP_SETSYNCRO:
case SOUND_PCM_READ_FILTER:
return -EINVAL;
}
return mixer_ioctl(s, cmd, arg);
}
static int sv_open(struct inode *inode, struct file *file)
{
int minor = MINOR(inode->i_rdev);
struct sv_state *s = devs;
unsigned char fmtm = ~0, fmts = 0;
while (s && ((s->dev_audio ^ minor) & ~0xf))
s = s->next;
if (!s)
return -ENODEV;
VALIDATE_STATE(s);
file->private_data = s;
/* wait for device to become free */
down(&s->open_sem);
while (s->open_mode & file->f_mode) {
if (file->f_flags & O_NONBLOCK) {
up(&s->open_sem);
return -EBUSY;
}
up(&s->open_sem);
interruptible_sleep_on(&s->open_wait);
if (signal_pending(current))
return -ERESTARTSYS;
down(&s->open_sem);
}
if (file->f_mode & FMODE_READ) {
fmtm &= ~((SV_CFMT_STEREO | SV_CFMT_16BIT) << SV_CFMT_CSHIFT);
if ((minor & 0xf) == SND_DEV_DSP16)
fmts |= SV_CFMT_16BIT << SV_CFMT_CSHIFT;
s->dma_adc.ossfragshift = s->dma_adc.ossmaxfrags = s->dma_adc.subdivision = 0;
set_adc_rate(s, 8000);
}
if (file->f_mode & FMODE_WRITE) {
fmtm &= ~((SV_CFMT_STEREO | SV_CFMT_16BIT) << SV_CFMT_ASHIFT);
if ((minor & 0xf) == SND_DEV_DSP16)
fmts |= SV_CFMT_16BIT << SV_CFMT_ASHIFT;
s->dma_dac.ossfragshift = s->dma_dac.ossmaxfrags = s->dma_dac.subdivision = 0;
set_dac_rate(s, 8000);
}
set_fmt(s, fmtm, fmts);
s->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
up(&s->open_sem);
MOD_INC_USE_COUNT;
return 0;
}
static int sv_release(struct inode *inode, struct file *file)
{
struct sv_state *s = (struct sv_state *)file->private_data;
VALIDATE_STATE(s);
if (file->f_mode & FMODE_WRITE)
drain_dac(s, file->f_flags & O_NONBLOCK);
down(&s->open_sem);
if (file->f_mode & FMODE_WRITE) {
stop_dac(s);
dealloc_dmabuf(&s->dma_dac);
}
if (file->f_mode & FMODE_READ) {
stop_adc(s);
dealloc_dmabuf(&s->dma_adc);
}
s->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE);
up(&s->open_sem);
wake_up(&s->open_wait);
MOD_DEC_USE_COUNT;
return 0;
}
static /*const*/ struct file_operations sv_audio_fops = {
&sv_llseek,
&sv_read,
&sv_write,
NULL, /* readdir */
&sv_poll,
&sv_ioctl,
&sv_mmap,
&sv_open,
NULL, /* flush */
&sv_release,
NULL, /* fsync */
NULL, /* fasync */
NULL, /* check_media_change */
NULL, /* revalidate */
NULL, /* lock */
};
/* --------------------------------------------------------------------- */
static ssize_t sv_midi_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
{
struct sv_state *s = (struct sv_state *)file->private_data;
ssize_t ret;
unsigned long flags;
unsigned ptr;
int cnt;
VALIDATE_STATE(s);
if (ppos != &file->f_pos)
return -ESPIPE;
if (!access_ok(VERIFY_WRITE, buffer, count))
return -EFAULT;
ret = 0;
while (count > 0) {
spin_lock_irqsave(&s->lock, flags);
ptr = s->midi.ird;
cnt = MIDIINBUF - ptr;
if (s->midi.icnt < cnt)
cnt = s->midi.icnt;
spin_unlock_irqrestore(&s->lock, flags);
if (cnt > count)
cnt = count;
if (cnt <= 0) {
if (file->f_flags & O_NONBLOCK)
return ret ? ret : -EAGAIN;
interruptible_sleep_on(&s->midi.iwait);
if (signal_pending(current))
return ret ? ret : -ERESTARTSYS;
continue;
}
if (copy_to_user(buffer, s->midi.ibuf + ptr, cnt))
return ret ? ret : -EFAULT;
ptr = (ptr + cnt) % MIDIINBUF;
spin_lock_irqsave(&s->lock, flags);
s->midi.ird = ptr;
s->midi.icnt -= cnt;
spin_unlock_irqrestore(&s->lock, flags);
count -= cnt;
buffer += cnt;
ret += cnt;
}
return ret;
}
static ssize_t sv_midi_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
{
struct sv_state *s = (struct sv_state *)file->private_data;
ssize_t ret;
unsigned long flags;
unsigned ptr;
int cnt;
VALIDATE_STATE(s);
if (ppos != &file->f_pos)
return -ESPIPE;
if (!access_ok(VERIFY_READ, buffer, count))
return -EFAULT;
ret = 0;
while (count > 0) {
spin_lock_irqsave(&s->lock, flags);
ptr = s->midi.owr;
cnt = MIDIOUTBUF - ptr;
if (s->midi.ocnt + cnt > MIDIOUTBUF)
cnt = MIDIOUTBUF - s->midi.ocnt;
if (cnt <= 0)
sv_handle_midi(s);
spin_unlock_irqrestore(&s->lock, flags);
if (cnt > count)
cnt = count;
if (cnt <= 0) {
if (file->f_flags & O_NONBLOCK)
return ret ? ret : -EAGAIN;
interruptible_sleep_on(&s->midi.owait);
if (signal_pending(current))
return ret ? ret : -ERESTARTSYS;
continue;
}
if (copy_from_user(s->midi.obuf + ptr, buffer, cnt))
return ret ? ret : -EFAULT;
ptr = (ptr + cnt) % MIDIOUTBUF;
spin_lock_irqsave(&s->lock, flags);
s->midi.owr = ptr;
s->midi.ocnt += cnt;
spin_unlock_irqrestore(&s->lock, flags);
count -= cnt;
buffer += cnt;
ret += cnt;
spin_lock_irqsave(&s->lock, flags);
sv_handle_midi(s);
spin_unlock_irqrestore(&s->lock, flags);
}
return ret;
}
static unsigned int sv_midi_poll(struct file *file, struct poll_table_struct *wait)
{
struct sv_state *s = (struct sv_state *)file->private_data;
unsigned long flags;
unsigned int mask = 0;
VALIDATE_STATE(s);
if (file->f_mode & FMODE_WRITE)
poll_wait(file, &s->midi.owait, wait);
if (file->f_mode & FMODE_READ)
poll_wait(file, &s->midi.iwait, wait);
spin_lock_irqsave(&s->lock, flags);
if (file->f_mode & FMODE_READ) {
if (s->midi.icnt > 0)
mask |= POLLIN | POLLRDNORM;
}
if (file->f_mode & FMODE_WRITE) {
if (s->midi.ocnt < MIDIOUTBUF)
mask |= POLLOUT | POLLWRNORM;
}
spin_unlock_irqrestore(&s->lock, flags);
return mask;
}
static int sv_midi_open(struct inode *inode, struct file *file)
{
int minor = MINOR(inode->i_rdev);
struct sv_state *s = devs;
unsigned long flags;
while (s && s->dev_midi != minor)
s = s->next;
if (!s)
return -ENODEV;
VALIDATE_STATE(s);
file->private_data = s;
/* wait for device to become free */
down(&s->open_sem);
while (s->open_mode & (file->f_mode << FMODE_MIDI_SHIFT)) {
if (file->f_flags & O_NONBLOCK) {
up(&s->open_sem);
return -EBUSY;
}
up(&s->open_sem);
interruptible_sleep_on(&s->open_wait);
if (signal_pending(current))
return -ERESTARTSYS;
down(&s->open_sem);
}
spin_lock_irqsave(&s->lock, flags);
if (!(s->open_mode & (FMODE_MIDI_READ | FMODE_MIDI_WRITE))) {
s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
s->midi.ord = s->midi.owr = s->midi.ocnt = 0;
//outb(inb(s->ioenh + SV_CODEC_CONTROL) | SV_CCTRL_WAVETABLE, s->ioenh + SV_CODEC_CONTROL);
outb(inb(s->ioenh + SV_CODEC_INTMASK) | SV_CINTMASK_MIDI, s->ioenh + SV_CODEC_INTMASK);
wrindir(s, SV_CIUARTCONTROL, 5); /* output MIDI data to external and internal synth */
wrindir(s, SV_CIWAVETABLESRC, 1); /* Wavetable in PC RAM */
outb(0xff, s->iomidi+1); /* reset command */
outb(0x3f, s->iomidi+1); /* uart command */
if (!(inb(s->iomidi+1) & 0x80))
inb(s->iomidi);
s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
init_timer(&s->midi.timer);
s->midi.timer.expires = jiffies+1;
s->midi.timer.data = (unsigned long)s;
s->midi.timer.function = sv_midi_timer;
add_timer(&s->midi.timer);
}
if (file->f_mode & FMODE_READ) {
s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
}
if (file->f_mode & FMODE_WRITE) {
s->midi.ord = s->midi.owr = s->midi.ocnt = 0;
}
spin_unlock_irqrestore(&s->lock, flags);
s->open_mode |= (file->f_mode << FMODE_MIDI_SHIFT) & (FMODE_MIDI_READ | FMODE_MIDI_WRITE);
up(&s->open_sem);
MOD_INC_USE_COUNT;
return 0;
}
static int sv_midi_release(struct inode *inode, struct file *file)
{
struct sv_state *s = (struct sv_state *)file->private_data;
DECLARE_WAITQUEUE(wait, current);
unsigned long flags;
unsigned count, tmo;
VALIDATE_STATE(s);
if (file->f_mode & FMODE_WRITE) {
current->state = TASK_INTERRUPTIBLE;
add_wait_queue(&s->midi.owait, &wait);
for (;;) {
spin_lock_irqsave(&s->lock, flags);
count = s->midi.ocnt;
spin_unlock_irqrestore(&s->lock, flags);
if (count <= 0)
break;
if (signal_pending(current))
break;
if (file->f_flags & O_NONBLOCK) {
remove_wait_queue(&s->midi.owait, &wait);
current->state = TASK_RUNNING;
return -EBUSY;
}
tmo = (count * HZ) / 3100;
if (!schedule_timeout(tmo ? : 1) && tmo)
printk(KERN_DEBUG "sv: midi timed out??\n");
}
remove_wait_queue(&s->midi.owait, &wait);
current->state = TASK_RUNNING;
}
down(&s->open_sem);
s->open_mode &= (~(file->f_mode << FMODE_MIDI_SHIFT)) & (FMODE_MIDI_READ|FMODE_MIDI_WRITE);
spin_lock_irqsave(&s->lock, flags);
if (!(s->open_mode & (FMODE_MIDI_READ | FMODE_MIDI_WRITE))) {
outb(inb(s->ioenh + SV_CODEC_INTMASK) & ~SV_CINTMASK_MIDI, s->ioenh + SV_CODEC_INTMASK);
del_timer(&s->midi.timer);
}
spin_unlock_irqrestore(&s->lock, flags);
up(&s->open_sem);
wake_up(&s->open_wait);
MOD_DEC_USE_COUNT;
return 0;
}
static /*const*/ struct file_operations sv_midi_fops = {
&sv_llseek,
&sv_midi_read,
&sv_midi_write,
NULL, /* readdir */
&sv_midi_poll,
NULL, /* ioctl */
NULL, /* mmap */
&sv_midi_open,
NULL, /* flush */
&sv_midi_release,
NULL, /* fsync */
NULL, /* fasync */
NULL, /* check_media_change */
NULL, /* revalidate */
NULL, /* lock */
};
/* --------------------------------------------------------------------- */
static int sv_dmfm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
static const unsigned char op_offset[18] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15
};
struct sv_state *s = (struct sv_state *)file->private_data;
struct dm_fm_voice v;
struct dm_fm_note n;
struct dm_fm_params p;
unsigned int io;
unsigned int regb;
switch (cmd) {
case FM_IOCTL_RESET:
for (regb = 0xb0; regb < 0xb9; regb++) {
outb(regb, s->iosynth);
outb(0, s->iosynth+1);
outb(regb, s->iosynth+2);
outb(0, s->iosynth+3);
}
return 0;
case FM_IOCTL_PLAY_NOTE:
if (copy_from_user(&n, (void *)arg, sizeof(n)))
return -EFAULT;
if (n.voice >= 18)
return -EINVAL;
if (n.voice >= 9) {
regb = n.voice - 9;
io = s->iosynth+2;
} else {
regb = n.voice;
io = s->iosynth;
}
outb(0xa0 + regb, io);
outb(n.fnum & 0xff, io+1);
outb(0xb0 + regb, io);
outb(((n.fnum >> 8) & 3) | ((n.octave & 7) << 2) | ((n.key_on & 1) << 5), io+1);
return 0;
case FM_IOCTL_SET_VOICE:
if (copy_from_user(&v, (void *)arg, sizeof(v)))
return -EFAULT;
if (v.voice >= 18)
return -EINVAL;
regb = op_offset[v.voice];
io = s->iosynth + ((v.op & 1) << 1);
outb(0x20 + regb, io);
outb(((v.am & 1) << 7) | ((v.vibrato & 1) << 6) | ((v.do_sustain & 1) << 5) |
((v.kbd_scale & 1) << 4) | (v.harmonic & 0xf), io+1);
outb(0x40 + regb, io);
outb(((v.scale_level & 0x3) << 6) | (v.volume & 0x3f), io+1);
outb(0x60 + regb, io);
outb(((v.attack & 0xf) << 4) | (v.decay & 0xf), io+1);
outb(0x80 + regb, io);
outb(((v.sustain & 0xf) << 4) | (v.release & 0xf), io+1);
outb(0xe0 + regb, io);
outb(v.waveform & 0x7, io+1);
if (n.voice >= 9) {
regb = n.voice - 9;
io = s->iosynth+2;
} else {
regb = n.voice;
io = s->iosynth;
}
outb(0xc0 + regb, io);
outb(((v.right & 1) << 5) | ((v.left & 1) << 4) | ((v.feedback & 7) << 1) |
(v.connection & 1), io+1);
return 0;
case FM_IOCTL_SET_PARAMS:
if (copy_from_user(&p, (void *)arg, sizeof(p)))
return -EFAULT;
outb(0x08, s->iosynth);
outb((p.kbd_split & 1) << 6, s->iosynth+1);
outb(0xbd, s->iosynth);
outb(((p.am_depth & 1) << 7) | ((p.vib_depth & 1) << 6) | ((p.rhythm & 1) << 5) | ((p.bass & 1) << 4) |
((p.snare & 1) << 3) | ((p.tomtom & 1) << 2) | ((p.cymbal & 1) << 1) | (p.hihat & 1), s->iosynth+1);
return 0;
case FM_IOCTL_SET_OPL:
outb(4, s->iosynth+2);
outb(arg, s->iosynth+3);
return 0;
case FM_IOCTL_SET_MODE:
outb(5, s->iosynth+2);
outb(arg & 1, s->iosynth+3);
return 0;
default:
return -EINVAL;
}
}
static int sv_dmfm_open(struct inode *inode, struct file *file)
{
int minor = MINOR(inode->i_rdev);
struct sv_state *s = devs;
while (s && s->dev_dmfm != minor)
s = s->next;
if (!s)
return -ENODEV;
VALIDATE_STATE(s);
file->private_data = s;
/* wait for device to become free */
down(&s->open_sem);
while (s->open_mode & FMODE_DMFM) {
if (file->f_flags & O_NONBLOCK) {
up(&s->open_sem);
return -EBUSY;
}
up(&s->open_sem);
interruptible_sleep_on(&s->open_wait);
if (signal_pending(current))
return -ERESTARTSYS;
down(&s->open_sem);
}
/* init the stuff */
outb(1, s->iosynth);
outb(0x20, s->iosynth+1); /* enable waveforms */
outb(4, s->iosynth+2);
outb(0, s->iosynth+3); /* no 4op enabled */
outb(5, s->iosynth+2);
outb(1, s->iosynth+3); /* enable OPL3 */
s->open_mode |= FMODE_DMFM;
up(&s->open_sem);
MOD_INC_USE_COUNT;
return 0;
}
static int sv_dmfm_release(struct inode *inode, struct file *file)
{
struct sv_state *s = (struct sv_state *)file->private_data;
unsigned int regb;
VALIDATE_STATE(s);
down(&s->open_sem);
s->open_mode &= ~FMODE_DMFM;
for (regb = 0xb0; regb < 0xb9; regb++) {
outb(regb, s->iosynth);
outb(0, s->iosynth+1);
outb(regb, s->iosynth+2);
outb(0, s->iosynth+3);
}
up(&s->open_sem);
wake_up(&s->open_wait);
MOD_DEC_USE_COUNT;
return 0;
}
static /*const*/ struct file_operations sv_dmfm_fops = {
&sv_llseek,
NULL, /* read */
NULL, /* write */
NULL, /* readdir */
NULL, /* poll */
&sv_dmfm_ioctl,
NULL, /* mmap */
&sv_dmfm_open,
NULL, /* flush */
&sv_dmfm_release,
NULL, /* fsync */
NULL, /* fasync */
NULL, /* check_media_change */
NULL, /* revalidate */
NULL, /* lock */
};
/* --------------------------------------------------------------------- */
/* maximum number of devices */
#define NR_DEVICE 5
static int reverb[NR_DEVICE] = { 0, };
#if 0
static int wavetable[NR_DEVICE] = { 0, };
#endif
static unsigned dmaio = 0xac00;
/* --------------------------------------------------------------------- */
static struct initvol {
int mixch;
int vol;
} initvol[] __initdata = {
{ SOUND_MIXER_WRITE_RECLEV, 0x4040 },
{ SOUND_MIXER_WRITE_LINE1, 0x4040 },
{ SOUND_MIXER_WRITE_CD, 0x4040 },
{ SOUND_MIXER_WRITE_LINE, 0x4040 },
{ SOUND_MIXER_WRITE_MIC, 0x4040 },
{ SOUND_MIXER_WRITE_SYNTH, 0x4040 },
{ SOUND_MIXER_WRITE_LINE2, 0x4040 },
{ SOUND_MIXER_WRITE_VOLUME, 0x4040 },
{ SOUND_MIXER_WRITE_PCM, 0x4040 }
};
#ifdef MODULE
__initfunc(int init_module(void))
#else
__initfunc(int init_sonicvibes(void))
#endif
{
struct sv_state *s;
struct pci_dev *pcidev = NULL;
mm_segment_t fs;
int i, val, index = 0;
if (!pci_present()) /* No PCI bus in this machine! */
return -ENODEV;
printk(KERN_INFO "sv: version v0.14 time " __TIME__ " " __DATE__ "\n");
#if 0
if (!(wavetable_mem = __get_free_pages(GFP_KERNEL, 20-PAGE_SHIFT)))
printk(KERN_INFO "sv: cannot allocate 1MB of contiguous nonpageable memory for wavetable data\n");
#endif
while (index < NR_DEVICE &&
(pcidev = pci_find_device(PCI_VENDOR_ID_S3, PCI_DEVICE_ID_S3_SONICVIBES, pcidev))) {
if (pcidev->base_address[1] == 0 ||
(pcidev->base_address[1] & PCI_BASE_ADDRESS_SPACE) != PCI_BASE_ADDRESS_SPACE_IO)
continue;
if (pcidev->base_address[2] == 0 ||
(pcidev->base_address[2] & PCI_BASE_ADDRESS_SPACE) != PCI_BASE_ADDRESS_SPACE_IO)
continue;
if (pcidev->base_address[3] == 0 ||
(pcidev->base_address[3] & PCI_BASE_ADDRESS_SPACE) != PCI_BASE_ADDRESS_SPACE_IO)
continue;
if (pcidev->irq == 0)
continue;
if (!(s = kmalloc(sizeof(struct sv_state), GFP_KERNEL))) {
printk(KERN_WARNING "sv: out of memory\n");
continue;
}
memset(s, 0, sizeof(struct sv_state));
init_waitqueue_head(&s->dma_adc.wait);
init_waitqueue_head(&s->dma_dac.wait);
init_waitqueue_head(&s->open_wait);
init_waitqueue_head(&s->midi.iwait);
init_waitqueue_head(&s->midi.owait);
init_MUTEX(&s->open_sem);
s->magic = SV_MAGIC;
s->iosb = pcidev->base_address[0] & PCI_BASE_ADDRESS_IO_MASK;
s->ioenh = pcidev->base_address[1] & PCI_BASE_ADDRESS_IO_MASK;
s->iosynth = pcidev->base_address[2] & PCI_BASE_ADDRESS_IO_MASK;
s->iomidi = pcidev->base_address[3] & PCI_BASE_ADDRESS_IO_MASK;
s->iogame = pcidev->base_address[4] & PCI_BASE_ADDRESS_IO_MASK;
pci_read_config_dword(pcidev, 0x40, &s->iodmaa);
pci_read_config_dword(pcidev, 0x48, &s->iodmac);
dmaio &= ~(SV_EXTENT_DMA-1);
s->iodmaa &= ~(SV_EXTENT_DMA-1);
s->iodmac &= ~(SV_EXTENT_DMA-1);
if (!(s->iodmaa)) {
s->iodmaa = dmaio;
dmaio += SV_EXTENT_DMA;
printk(KERN_INFO "sv: BIOS did not allocate DDMA channel A io, allocated at %#x\n",
s->iodmaa);
}
if (!(s->iodmac)) {
s->iodmac = dmaio;
dmaio += SV_EXTENT_DMA;
printk(KERN_INFO "sv: BIOS did not allocate DDMA channel C io, allocated at %#x\n",
s->iodmac);
}
pci_write_config_dword(pcidev, 0x40, s->iodmaa | 9); /* enable and use extended mode */
pci_write_config_dword(pcidev, 0x48, s->iodmac | 9); /* enable */
printk(KERN_DEBUG "sv: io ports: %#lx %#lx %#lx %#lx %#lx %#x %#x\n",
s->iosb, s->ioenh, s->iosynth, s->iomidi, s->iogame, s->iodmaa, s->iodmac);
if (s->ioenh == 0 || s->iodmaa == 0 || s->iodmac == 0)
continue;
s->irq = pcidev->irq;
/* hack */
pci_write_config_dword(pcidev, 0x60, wavetable_mem >> 12); /* wavetable base address */
if (check_region(s->ioenh, SV_EXTENT_ENH)) {
printk(KERN_ERR "sv: io ports %#lx-%#lx in use\n", s->ioenh, s->ioenh+SV_EXTENT_ENH-1);
goto err_region5;
}
request_region(s->ioenh, SV_EXTENT_ENH, "S3 SonicVibes PCM");
if (check_region(s->iodmaa, SV_EXTENT_DMA)) {
printk(KERN_ERR "sv: io ports %#x-%#x in use\n", s->iodmaa, s->iodmaa+SV_EXTENT_DMA-1);
goto err_region4;
}
request_region(s->iodmaa, SV_EXTENT_DMA, "S3 SonicVibes DMAA");
if (check_region(s->iodmac, SV_EXTENT_DMA)) {
printk(KERN_ERR "sv: io ports %#x-%#x in use\n", s->iodmac, s->iodmac+SV_EXTENT_DMA-1);
goto err_region3;
}
request_region(s->iodmac, SV_EXTENT_DMA, "S3 SonicVibes DMAC");
if (check_region(s->iomidi, SV_EXTENT_MIDI)) {
printk(KERN_ERR "sv: io ports %#lx-%#lx in use\n", s->iomidi, s->iomidi+SV_EXTENT_MIDI-1);
goto err_region2;
}
request_region(s->iomidi, SV_EXTENT_MIDI, "S3 SonicVibes Midi");
if (check_region(s->iosynth, SV_EXTENT_SYNTH)) {
printk(KERN_ERR "sv: io ports %#lx-%#lx in use\n", s->iosynth, s->iosynth+SV_EXTENT_SYNTH-1);
goto err_region1;
}
request_region(s->iosynth, SV_EXTENT_SYNTH, "S3 SonicVibes Synth");
/* initialize codec registers */
outb(0x80, s->ioenh + SV_CODEC_CONTROL); /* assert reset */
udelay(50);
outb(0x00, s->ioenh + SV_CODEC_CONTROL); /* deassert reset */
udelay(50);
outb(SV_CCTRL_INTADRIVE | SV_CCTRL_ENHANCED /*| SV_CCTRL_WAVETABLE */
| (reverb[index] ? SV_CCTRL_REVERB : 0), s->ioenh + SV_CODEC_CONTROL);
inb(s->ioenh + SV_CODEC_STATUS); /* clear ints */
wrindir(s, SV_CIDRIVECONTROL, 0); /* drive current 16mA */
wrindir(s, SV_CIENABLE, s->enable = 0); /* disable DMAA and DMAC */
outb(~(SV_CINTMASK_DMAA | SV_CINTMASK_DMAC), s->ioenh + SV_CODEC_INTMASK);
//outb(0xff, s->iodmaa + SV_DMA_RESET);
//outb(0xff, s->iodmac + SV_DMA_RESET);
inb(s->ioenh + SV_CODEC_STATUS); /* ack interrupts */
wrindir(s, SV_CIADCCLKSOURCE, 0); /* use pll as ADC clock source */
wrindir(s, SV_CIANALOGPWRDOWN, 0); /* power up the analog parts of the device */
wrindir(s, SV_CIDIGITALPWRDOWN, 0); /* power up the digital parts of the device */
setpll(s, SV_CIADCPLLM, 8000);
wrindir(s, SV_CISRSSPACE, 0x80); /* SRS off */
wrindir(s, SV_CIPCMSR0, (8000 * 65536 / FULLRATE) & 0xff);
wrindir(s, SV_CIPCMSR1, ((8000 * 65536 / FULLRATE) >> 8) & 0xff);
wrindir(s, SV_CIADCOUTPUT, 0);
/* request irq */
if (request_irq(s->irq, sv_interrupt, SA_SHIRQ, "S3 SonicVibes", s)) {
printk(KERN_ERR "sv: irq %u in use\n", s->irq);
goto err_irq;
}
printk(KERN_INFO "sv: found adapter at io %#lx irq %u dmaa %#06x dmac %#06x revision %u\n",
s->ioenh, s->irq, s->iodmaa, s->iodmac, rdindir(s, SV_CIREVISION));
/* register devices */
if ((s->dev_audio = register_sound_dsp(&sv_audio_fops, -1)) < 0)
goto err_dev1;
if ((s->dev_mixer = register_sound_mixer(&sv_mixer_fops, -1)) < 0)
goto err_dev2;
if ((s->dev_midi = register_sound_midi(&sv_midi_fops, -1)) < 0)
goto err_dev3;
if ((s->dev_dmfm = register_sound_special(&sv_dmfm_fops, 15 /* ?? */)) < 0)
goto err_dev4;
/* initialize the chips */
fs = get_fs();
set_fs(KERNEL_DS);
val = SOUND_MASK_LINE|SOUND_MASK_SYNTH;
mixer_ioctl(s, SOUND_MIXER_WRITE_RECSRC, (unsigned long)&val);
for (i = 0; i < sizeof(initvol)/sizeof(initvol[0]); i++) {
val = initvol[i].vol;
mixer_ioctl(s, initvol[i].mixch, (unsigned long)&val);
}
set_fs(fs);
/* queue it for later freeing */
s->next = devs;
devs = s;
index++;
continue;
err_dev4:
unregister_sound_midi(s->dev_midi);
err_dev3:
unregister_sound_mixer(s->dev_mixer);
err_dev2:
unregister_sound_dsp(s->dev_audio);
err_dev1:
printk(KERN_ERR "sv: cannot register misc device\n");
free_irq(s->irq, s);
err_irq:
release_region(s->iosynth, SV_EXTENT_SYNTH);
err_region1:
release_region(s->iomidi, SV_EXTENT_MIDI);
err_region2:
release_region(s->iodmac, SV_EXTENT_DMA);
err_region3:
release_region(s->iodmaa, SV_EXTENT_DMA);
err_region4:
release_region(s->ioenh, SV_EXTENT_ENH);
err_region5:
kfree_s(s, sizeof(struct sv_state));
}
if (!devs) {
if (wavetable_mem)
free_pages(wavetable_mem, 20-PAGE_SHIFT);
return -ENODEV;
}
return 0;
}
/* --------------------------------------------------------------------- */
#ifdef MODULE
MODULE_PARM(reverb, "1-" __MODULE_STRING(NR_DEVICE) "i");
MODULE_PARM_DESC(reverb, "if 1 enables the reverb circuitry. NOTE: your card must have the reverb RAM");
#if 0
MODULE_PARM(wavetable, "1-" __MODULE_STRING(NR_DEVICE) "i");
MODULE_PARM_DESC(wavetable, "if 1 the wavetable synth is enabled");
#endif
MODULE_PARM(dmaio, "i");
MODULE_PARM_DESC(dmaio, "if the motherboard BIOS did not allocate DDMA io, allocate them starting at this address");
MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
MODULE_DESCRIPTION("S3 SonicVibes Driver");
void cleanup_module(void)
{
struct sv_state *s;
while ((s = devs)) {
devs = devs->next;
outb(~0, s->ioenh + SV_CODEC_INTMASK); /* disable ints */
synchronize_irq();
inb(s->ioenh + SV_CODEC_STATUS); /* ack interrupts */
wrindir(s, SV_CIENABLE, 0); /* disable DMAA and DMAC */
//outb(0, s->iodmaa + SV_DMA_RESET);
//outb(0, s->iodmac + SV_DMA_RESET);
free_irq(s->irq, s);
release_region(s->iodmac, SV_EXTENT_DMA);
release_region(s->iodmaa, SV_EXTENT_DMA);
release_region(s->ioenh, SV_EXTENT_ENH);
release_region(s->iomidi, SV_EXTENT_MIDI);
release_region(s->iosynth, SV_EXTENT_SYNTH);
unregister_sound_dsp(s->dev_audio);
unregister_sound_mixer(s->dev_mixer);
unregister_sound_midi(s->dev_midi);
unregister_sound_special(s->dev_dmfm);
kfree_s(s, sizeof(struct sv_state));
}
if (wavetable_mem)
free_pages(wavetable_mem, 20-PAGE_SHIFT);
printk(KERN_INFO "sv: unloading\n");
}
#endif /* MODULE */
|