aboutsummaryrefslogtreecommitdiffstats
path: root/sound/oss/wavfront.c
blob: 1dec3958cc7b0494aea66ded7f34b635a6625fb6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
/*  -*- linux-c -*-
 *
 * sound/wavfront.c
 *
 * A Linux driver for Turtle Beach WaveFront Series (Maui, Tropez, Tropez Plus)
 *
 * This driver supports the onboard wavetable synthesizer (an ICS2115),
 * including patch, sample and program loading and unloading, conversion
 * of GUS patches during loading, and full user-level access to all
 * WaveFront commands. It tries to provide semi-intelligent patch and
 * sample management as well.
 *
 * It also provides support for the ICS emulation of an MPU-401.  Full
 * support for the ICS emulation's "virtual MIDI mode" is provided in
 * wf_midi.c.
 *
 * Support is also provided for the Tropez Plus' onboard FX processor,
 * a Yamaha YSS225. Currently, code exists to configure the YSS225,
 * and there is an interface allowing tweaking of any of its memory
 * addresses. However, I have been unable to decipher the logical
 * positioning of the configuration info for various effects, so for
 * now, you just get the YSS225 in the same state as Turtle Beach's
 * "SETUPSND.EXE" utility leaves it.
 *
 * The boards' DAC/ADC (a Crystal CS4232) is supported by cs4232.[co],
 * This chip also controls the configuration of the card: the wavefront
 * synth is logical unit 4.
 *
 *
 * Supported devices:
 *
 *   /dev/dsp                      - using cs4232+ad1848 modules, OSS compatible
 *   /dev/midiNN and /dev/midiNN+1 - using wf_midi code, OSS compatible
 *   /dev/synth00                  - raw synth interface
 * 
 **********************************************************************
 *
 * Copyright (C) by Paul Barton-Davis 1998
 *
 * Some portions of this file are taken from work that is
 * copyright (C) by Hannu Savolainen 1993-1996
 *
 * Although the relevant code here is all new, the handling of
 * sample/alias/multi- samples is entirely based on a driver by Matt
 * Martin and Rutger Nijlunsing which demonstrated how to get things
 * to work correctly. The GUS patch loading code has been almost
 * unaltered by me, except to fit formatting and function names in the
 * rest of the file. Many thanks to them.
 *
 * Appreciation and thanks to Hannu Savolainen for his early work on the Maui
 * driver, and answering a few questions while this one was developed.
 *
 * Absolutely NO thanks to Turtle Beach/Voyetra and Yamaha for their
 * complete lack of help in developing this driver, and in particular
 * for their utter silence in response to questions about undocumented
 * aspects of configuring a WaveFront soundcard, particularly the
 * effects processor.
 *
 * $Id: wavfront.c,v 0.7 1998/09/09 15:47:36 pbd Exp $
 *
 * This program is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
 * Version 2 (June 1991). See the "COPYING" file distributed with this software
 * for more info.
 *
 * Changes:
 * 11-10-2000	Bartlomiej Zolnierkiewicz <bkz@linux-ide.org>
 *		Added some __init and __initdata to entries in yss225.c
 */

#include <linux/module.h>

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/smp_lock.h>
#include <linux/ptrace.h>
#include <linux/fcntl.h>
#include <linux/syscalls.h>
#include <linux/ioport.h>    
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/config.h>

#include <linux/delay.h>

#include "sound_config.h"

#include <linux/wavefront.h>

#define _MIDI_SYNTH_C_
#define MIDI_SYNTH_NAME	"WaveFront MIDI"
#define MIDI_SYNTH_CAPS	SYNTH_CAP_INPUT
#include "midi_synth.h"

/* Compile-time control of the extent to which OSS is supported.

   I consider /dev/sequencer to be an anachronism, but given its
   widespread usage by various Linux MIDI software, it seems worth
   offering support to it if it's not too painful. Instead of using
   /dev/sequencer, I recommend:

     for synth programming and patch loading: /dev/synthNN
     for kernel-synchronized MIDI sequencing: the ALSA sequencer
     for direct MIDI control: /dev/midiNN

   I have never tried static compilation into the kernel. The #if's
   for this are really just notes to myself about what the code is
   for.
*/

#define OSS_SUPPORT_SEQ            0x1  /* use of /dev/sequencer */
#define OSS_SUPPORT_STATIC_INSTALL 0x2  /* static compilation into kernel */

#define OSS_SUPPORT_LEVEL          0x1  /* just /dev/sequencer for now */

#if    OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
static int (*midi_load_patch) (int devno, int format, const char __user *addr,
			       int offs, int count, int pmgr_flag) = NULL;
#endif /* OSS_SUPPORT_SEQ */

/* if WF_DEBUG not defined, no run-time debugging messages will
   be available via the debug flag setting. Given the current
   beta state of the driver, this will remain set until a future 
   version.
*/

#define WF_DEBUG 1

#ifdef WF_DEBUG

/* Thank goodness for gcc's preprocessor ... */

#define DPRINT(cond, format, args...) \
       if ((dev.debug & (cond)) == (cond)) { \
	     printk (KERN_DEBUG LOGNAME format, ## args); \
       }
#else
#define DPRINT(cond, format, args...)
#endif

#define LOGNAME "WaveFront: "

/* bitmasks for WaveFront status port value */

#define STAT_RINTR_ENABLED	0x01
#define STAT_CAN_READ		0x02
#define STAT_INTR_READ		0x04
#define STAT_WINTR_ENABLED	0x10
#define STAT_CAN_WRITE		0x20
#define STAT_INTR_WRITE		0x40

/*** Module-accessible parameters ***************************************/

static int wf_raw;     /* we normally check for "raw state" to firmware
			   loading. if set, then during driver loading, the
			   state of the board is ignored, and we reset the
			   board and load the firmware anyway.
			*/
		   
static int fx_raw = 1; /* if this is zero, we'll leave the FX processor in
		          whatever state it is when the driver is loaded.
		          The default is to download the microprogram and
		          associated coefficients to set it up for "default"
		          operation, whatever that means.
		       */

static int debug_default;  /* you can set this to control debugging
			      during driver loading. it takes any combination
			      of the WF_DEBUG_* flags defined in
			      wavefront.h
			   */

/* XXX this needs to be made firmware and hardware version dependent */

static char *ospath = "/etc/sound/wavefront.os"; /* where to find a processed
					            version of the WaveFront OS
					          */

static int wait_polls = 2000; /* This is a number of tries we poll the
				 status register before resorting to sleeping.
				 WaveFront being an ISA card each poll takes
				 about 1.2us. So before going to
			         sleep we wait up to 2.4ms in a loop.
			     */

static int sleep_length = HZ/100; /* This says how long we're going to
				     sleep between polls.
			             10ms sounds reasonable for fast response.
			          */

static int sleep_tries = 50;       /* Wait for status 0.5 seconds total. */

static int reset_time = 2; /* hundreths of a second we wait after a HW reset for
			      the expected interrupt.
			   */

static int ramcheck_time = 20;    /* time in seconds to wait while ROM code
			             checks on-board RAM.
			          */

static int osrun_time = 10;  /* time in seconds we wait for the OS to
			        start running.
			     */

module_param(wf_raw, int, 0);
module_param(fx_raw, int, 0);
module_param(debug_default, int, 0);
module_param(wait_polls, int, 0);
module_param(sleep_length, int, 0);
module_param(sleep_tries, int, 0);
module_param(ospath, charp, 0);
module_param(reset_time, int, 0);
module_param(ramcheck_time, int, 0);
module_param(osrun_time, int, 0);

/***************************************************************************/

/* Note: because this module doesn't export any symbols, this really isn't
   a global variable, even if it looks like one. I was quite confused by
   this when I started writing this as a (newer) module -- pbd.
*/

struct wf_config {
	int devno;            /* device number from kernel */
	int irq;              /* "you were one, one of the few ..." */
	int base;             /* low i/o port address */

#define mpu_data_port    base 
#define mpu_command_port base + 1 /* write semantics */
#define mpu_status_port  base + 1 /* read semantics */
#define data_port        base + 2 
#define status_port      base + 3 /* read semantics */
#define control_port     base + 3 /* write semantics  */
#define block_port       base + 4 /* 16 bit, writeonly */
#define last_block_port  base + 6 /* 16 bit, writeonly */

	/* FX ports. These are mapped through the ICS2115 to the YS225.
	   The ICS2115 takes care of flipping the relevant pins on the
	   YS225 so that access to each of these ports does the right
	   thing. Note: these are NOT documented by Turtle Beach.
	*/

#define fx_status       base + 8 
#define fx_op           base + 8 
#define fx_lcr          base + 9 
#define fx_dsp_addr     base + 0xa
#define fx_dsp_page     base + 0xb 
#define fx_dsp_lsb      base + 0xc 
#define fx_dsp_msb      base + 0xd 
#define fx_mod_addr     base + 0xe
#define fx_mod_data     base + 0xf 

	volatile int irq_ok;               /* set by interrupt handler */
        volatile int irq_cnt;              /* ditto */
	int opened;                        /* flag, holds open(2) mode */
	char debug;                        /* debugging flags */
	int freemem;                       /* installed RAM, in bytes */ 

	int synth_dev;                     /* devno for "raw" synth */
	int mididev;                       /* devno for internal MIDI */
	int ext_mididev;                   /* devno for external MIDI */ 
        int fx_mididev;                    /* devno for FX MIDI interface */
#if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
	int oss_dev;                      /* devno for OSS sequencer synth */
#endif /* OSS_SUPPORT_SEQ */

	char fw_version[2];                /* major = [0], minor = [1] */
	char hw_version[2];                /* major = [0], minor = [1] */
	char israw;                        /* needs Motorola microcode */
	char has_fx;                       /* has FX processor (Tropez+) */
	char prog_status[WF_MAX_PROGRAM];  /* WF_SLOT_* */
	char patch_status[WF_MAX_PATCH];   /* WF_SLOT_* */
	char sample_status[WF_MAX_SAMPLE]; /* WF_ST_* | WF_SLOT_* */
	int samples_used;                  /* how many */
	char interrupts_on;                /* h/w MPU interrupts enabled ? */
	char rom_samples_rdonly;           /* can we write on ROM samples */
	wait_queue_head_t interrupt_sleeper; 
} dev;

static DEFINE_SPINLOCK(lock);
static int  detect_wffx(void);
static int  wffx_ioctl (wavefront_fx_info *);
static int  wffx_init (void);

static int wavefront_delete_sample (int sampnum);
static int wavefront_find_free_sample (void);

/* From wf_midi.c */

extern int  virtual_midi_enable  (void);
extern int  virtual_midi_disable (void);
extern int  detect_wf_mpu (int, int);
extern int  install_wf_mpu (void);
extern int  uninstall_wf_mpu (void);

typedef struct {
	int cmd;
	char *action;
	unsigned int read_cnt;
	unsigned int write_cnt;
	int need_ack;
} wavefront_command;

static struct {
	int errno;
	const char *errstr;
} wavefront_errors[] = {
	{ 0x01, "Bad sample number" },
	{ 0x02, "Out of sample memory" },
	{ 0x03, "Bad patch number" },
	{ 0x04, "Error in number of voices" },
	{ 0x06, "Sample load already in progress" },
	{ 0x0B, "No sample load request pending" },
	{ 0x0E, "Bad MIDI channel number" },
	{ 0x10, "Download Record Error" },
	{ 0x80, "Success" },
	{ 0 }
};

#define NEEDS_ACK 1

static wavefront_command wavefront_commands[] = {
	{ WFC_SET_SYNTHVOL, "set synthesizer volume", 0, 1, NEEDS_ACK },
	{ WFC_GET_SYNTHVOL, "get synthesizer volume", 1, 0, 0},
	{ WFC_SET_NVOICES, "set number of voices", 0, 1, NEEDS_ACK },
	{ WFC_GET_NVOICES, "get number of voices", 1, 0, 0 },
	{ WFC_SET_TUNING, "set synthesizer tuning", 0, 2, NEEDS_ACK },
	{ WFC_GET_TUNING, "get synthesizer tuning", 2, 0, 0 },
	{ WFC_DISABLE_CHANNEL, "disable synth channel", 0, 1, NEEDS_ACK },
	{ WFC_ENABLE_CHANNEL, "enable synth channel", 0, 1, NEEDS_ACK },
	{ WFC_GET_CHANNEL_STATUS, "get synth channel status", 3, 0, 0 },
	{ WFC_MISYNTH_OFF, "disable midi-in to synth", 0, 0, NEEDS_ACK },
	{ WFC_MISYNTH_ON, "enable midi-in to synth", 0, 0, NEEDS_ACK },
	{ WFC_VMIDI_ON, "enable virtual midi mode", 0, 0, NEEDS_ACK },
	{ WFC_VMIDI_OFF, "disable virtual midi mode", 0, 0, NEEDS_ACK },
	{ WFC_MIDI_STATUS, "report midi status", 1, 0, 0 },
	{ WFC_FIRMWARE_VERSION, "report firmware version", 2, 0, 0 },
	{ WFC_HARDWARE_VERSION, "report hardware version", 2, 0, 0 },
	{ WFC_GET_NSAMPLES, "report number of samples", 2, 0, 0 },
	{ WFC_INSTOUT_LEVELS, "report instantaneous output levels", 7, 0, 0 },
	{ WFC_PEAKOUT_LEVELS, "report peak output levels", 7, 0, 0 },
	{ WFC_DOWNLOAD_SAMPLE, "download sample",
	  0, WF_SAMPLE_BYTES, NEEDS_ACK },
	{ WFC_DOWNLOAD_BLOCK, "download block", 0, 0, NEEDS_ACK},
	{ WFC_DOWNLOAD_SAMPLE_HEADER, "download sample header",
	  0, WF_SAMPLE_HDR_BYTES, NEEDS_ACK },
	{ WFC_UPLOAD_SAMPLE_HEADER, "upload sample header", 13, 2, 0 },

	/* This command requires a variable number of bytes to be written.
	   There is a hack in wavefront_cmd() to support this. The actual
	   count is passed in as the read buffer ptr, cast appropriately.
	   Ugh.
	*/

	{ WFC_DOWNLOAD_MULTISAMPLE, "download multisample", 0, 0, NEEDS_ACK },

	/* This one is a hack as well. We just read the first byte of the
	   response, don't fetch an ACK, and leave the rest to the 
	   calling function. Ugly, ugly, ugly.
	*/

	{ WFC_UPLOAD_MULTISAMPLE, "upload multisample", 2, 1, 0 },
	{ WFC_DOWNLOAD_SAMPLE_ALIAS, "download sample alias",
	  0, WF_ALIAS_BYTES, NEEDS_ACK },
	{ WFC_UPLOAD_SAMPLE_ALIAS, "upload sample alias", WF_ALIAS_BYTES, 2, 0},
	{ WFC_DELETE_SAMPLE, "delete sample", 0, 2, NEEDS_ACK },
	{ WFC_IDENTIFY_SAMPLE_TYPE, "identify sample type", 5, 2, 0 },
	{ WFC_UPLOAD_SAMPLE_PARAMS, "upload sample parameters" },
	{ WFC_REPORT_FREE_MEMORY, "report free memory", 4, 0, 0 },
	{ WFC_DOWNLOAD_PATCH, "download patch", 0, 134, NEEDS_ACK },
	{ WFC_UPLOAD_PATCH, "upload patch", 132, 2, 0 },
	{ WFC_DOWNLOAD_PROGRAM, "download program", 0, 33, NEEDS_ACK },
	{ WFC_UPLOAD_PROGRAM, "upload program", 32, 1, 0 },
	{ WFC_DOWNLOAD_EDRUM_PROGRAM, "download enhanced drum program", 0, 9,
	  NEEDS_ACK},
	{ WFC_UPLOAD_EDRUM_PROGRAM, "upload enhanced drum program", 8, 1, 0},
	{ WFC_SET_EDRUM_CHANNEL, "set enhanced drum program channel",
	  0, 1, NEEDS_ACK },
	{ WFC_DISABLE_DRUM_PROGRAM, "disable drum program", 0, 1, NEEDS_ACK },
	{ WFC_REPORT_CHANNEL_PROGRAMS, "report channel program numbers",
	  32, 0, 0 },
	{ WFC_NOOP, "the no-op command", 0, 0, NEEDS_ACK },
	{ 0x00 }
};

static const char *
wavefront_errorstr (int errnum)

{
	int i;

	for (i = 0; wavefront_errors[i].errstr; i++) {
		if (wavefront_errors[i].errno == errnum) {
			return wavefront_errors[i].errstr;
		}
	}

	return "Unknown WaveFront error";
}

static wavefront_command *
wavefront_get_command (int cmd) 

{
	int i;

	for (i = 0; wavefront_commands[i].cmd != 0; i++) {
		if (cmd == wavefront_commands[i].cmd) {
			return &wavefront_commands[i];
		}
	}

	return (wavefront_command *) 0;
}

static inline int
wavefront_status (void) 

{
	return inb (dev.status_port);
}

static int
wavefront_wait (int mask)

{
	int i;

	for (i = 0; i < wait_polls; i++)
		if (wavefront_status() & mask)
			return 1;

	for (i = 0; i < sleep_tries; i++) {

		if (wavefront_status() & mask) {
			set_current_state(TASK_RUNNING);
			return 1;
		}

		set_current_state(TASK_INTERRUPTIBLE);
		schedule_timeout(sleep_length);
		if (signal_pending(current))
			break;
	}

	set_current_state(TASK_RUNNING);
	return 0;
}

static int
wavefront_read (void)

{
	if (wavefront_wait (STAT_CAN_READ))
		return inb (dev.data_port);

	DPRINT (WF_DEBUG_DATA, "read timeout.\n");

	return -1;
}

static int
wavefront_write (unsigned char data)

{
	if (wavefront_wait (STAT_CAN_WRITE)) {
		outb (data, dev.data_port);
		return 0;
	}

	DPRINT (WF_DEBUG_DATA, "write timeout.\n");

	return -1;
}

static int
wavefront_cmd (int cmd, unsigned char *rbuf, unsigned char *wbuf)

{
	int ack;
	int i;
	int c;
	wavefront_command *wfcmd;

	if ((wfcmd = wavefront_get_command (cmd)) == (wavefront_command *) 0) {
		printk (KERN_WARNING LOGNAME "command 0x%x not supported.\n",
			cmd);
		return 1;
	}

	/* Hack to handle the one variable-size write command. See
	   wavefront_send_multisample() for the other half of this
	   gross and ugly strategy.
	*/

	if (cmd == WFC_DOWNLOAD_MULTISAMPLE) {
		wfcmd->write_cnt = (unsigned int) rbuf;
		rbuf = NULL;
	}

	DPRINT (WF_DEBUG_CMD, "0x%x [%s] (%d,%d,%d)\n",
			       cmd, wfcmd->action, wfcmd->read_cnt,
			       wfcmd->write_cnt, wfcmd->need_ack);
    
	if (wavefront_write (cmd)) { 
		DPRINT ((WF_DEBUG_IO|WF_DEBUG_CMD), "cannot request "
						     "0x%x [%s].\n",
						     cmd, wfcmd->action);
		return 1;
	} 

	if (wfcmd->write_cnt > 0) {
		DPRINT (WF_DEBUG_DATA, "writing %d bytes "
					"for 0x%x\n",
					wfcmd->write_cnt, cmd);

		for (i = 0; i < wfcmd->write_cnt; i++) {
			if (wavefront_write (wbuf[i])) {
				DPRINT (WF_DEBUG_IO, "bad write for byte "
						      "%d of 0x%x [%s].\n",
						      i, cmd, wfcmd->action);
				return 1;
			}

			DPRINT (WF_DEBUG_DATA, "write[%d] = 0x%x\n",
						i, wbuf[i]);
		}
	}

	if (wfcmd->read_cnt > 0) {
		DPRINT (WF_DEBUG_DATA, "reading %d ints "
					"for 0x%x\n",
					wfcmd->read_cnt, cmd);

		for (i = 0; i < wfcmd->read_cnt; i++) {

			if ((c = wavefront_read()) == -1) {
				DPRINT (WF_DEBUG_IO, "bad read for byte "
						      "%d of 0x%x [%s].\n",
						      i, cmd, wfcmd->action);
				return 1;
			}

			/* Now handle errors. Lots of special cases here */
	    
			if (c == 0xff) { 
				if ((c = wavefront_read ()) == -1) {
					DPRINT (WF_DEBUG_IO, "bad read for "
							      "error byte at "
							      "read byte %d "
							      "of 0x%x [%s].\n",
							      i, cmd,
							      wfcmd->action);
					return 1;
				}

				/* Can you believe this madness ? */

				if (c == 1 &&
				    wfcmd->cmd == WFC_IDENTIFY_SAMPLE_TYPE) {
					rbuf[0] = WF_ST_EMPTY;
					return (0);

				} else if (c == 3 &&
					   wfcmd->cmd == WFC_UPLOAD_PATCH) {

					return 3;

				} else if (c == 1 &&
					   wfcmd->cmd == WFC_UPLOAD_PROGRAM) {

					return 1;

				} else {

					DPRINT (WF_DEBUG_IO, "error %d (%s) "
							      "during "
							      "read for byte "
							      "%d of 0x%x "
							      "[%s].\n",
							      c,
							      wavefront_errorstr (c),
							      i, cmd,
							      wfcmd->action);
					return 1;

				}
		
		} else {
				rbuf[i] = c;
			}
			
			DPRINT (WF_DEBUG_DATA, "read[%d] = 0x%x\n",i, rbuf[i]);
		}
	}
	
	if ((wfcmd->read_cnt == 0 && wfcmd->write_cnt == 0) || wfcmd->need_ack) {

		DPRINT (WF_DEBUG_CMD, "reading ACK for 0x%x\n", cmd);

		/* Some commands need an ACK, but return zero instead
		   of the standard value.
		*/
	    
		if ((ack = wavefront_read()) == 0) {
			ack = WF_ACK;
		}
	
		if (ack != WF_ACK) {
			if (ack == -1) {
				DPRINT (WF_DEBUG_IO, "cannot read ack for "
						      "0x%x [%s].\n",
						      cmd, wfcmd->action);
				return 1;
		
			} else {
				int err = -1; /* something unknown */

				if (ack == 0xff) { /* explicit error */
		    
					if ((err = wavefront_read ()) == -1) {
						DPRINT (WF_DEBUG_DATA,
							"cannot read err "
							"for 0x%x [%s].\n",
							cmd, wfcmd->action);
					}
				}
				
				DPRINT (WF_DEBUG_IO, "0x%x [%s] "
					"failed (0x%x, 0x%x, %s)\n",
					cmd, wfcmd->action, ack, err,
					wavefront_errorstr (err));
				
				return -err;
			}
		}
		
		DPRINT (WF_DEBUG_DATA, "ack received "
					"for 0x%x [%s]\n",
					cmd, wfcmd->action);
	} else {

		DPRINT (WF_DEBUG_CMD, "0x%x [%s] does not need "
				       "ACK (%d,%d,%d)\n",
				       cmd, wfcmd->action, wfcmd->read_cnt,
				       wfcmd->write_cnt, wfcmd->need_ack);
	}

	return 0;
	
}

/***********************************************************************
WaveFront: data munging   

Things here are weird. All data written to the board cannot 
have its most significant bit set. Any data item with values 
potentially > 0x7F (127) must be split across multiple bytes.

Sometimes, we need to munge numeric values that are represented on
the x86 side as 8-32 bit values. Sometimes, we need to munge data
that is represented on the x86 side as an array of bytes. The most
efficient approach to handling both cases seems to be to use 2
different functions for munging and 2 for de-munging. This avoids
weird casting and worrying about bit-level offsets.

**********************************************************************/

static 
unsigned char *
munge_int32 (unsigned int src,
	     unsigned char *dst,
	     unsigned int dst_size)
{
	int i;

	for (i = 0;i < dst_size; i++) {
		*dst = src & 0x7F;  /* Mask high bit of LSB */
		src = src >> 7;     /* Rotate Right 7 bits  */
	                            /* Note: we leave the upper bits in place */ 

		dst++;
 	};
	return dst;
};

static int 
demunge_int32 (unsigned char* src, int src_size)

{
	int i;
 	int outval = 0;
	
 	for (i = src_size - 1; i >= 0; i--) {
		outval=(outval<<7)+src[i];
	}

	return outval;
};

static 
unsigned char *
munge_buf (unsigned char *src, unsigned char *dst, unsigned int dst_size)

{
	int i;
	unsigned int last = dst_size / 2;

	for (i = 0; i < last; i++) {
		*dst++ = src[i] & 0x7f;
		*dst++ = src[i] >> 7;
	}
	return dst;
}

static 
unsigned char *
demunge_buf (unsigned char *src, unsigned char *dst, unsigned int src_bytes)

{
	int i;
	unsigned char *end = src + src_bytes;
    
	end = src + src_bytes;

	/* NOTE: src and dst *CAN* point to the same address */

	for (i = 0; src != end; i++) {
		dst[i] = *src++;
		dst[i] |= (*src++)<<7;
	}

	return dst;
}

/***********************************************************************
WaveFront: sample, patch and program management.
***********************************************************************/

static int
wavefront_delete_sample (int sample_num)

{
	unsigned char wbuf[2];
	int x;

	wbuf[0] = sample_num & 0x7f;
	wbuf[1] = sample_num >> 7;

	if ((x = wavefront_cmd (WFC_DELETE_SAMPLE, NULL, wbuf)) == 0) {
		dev.sample_status[sample_num] = WF_ST_EMPTY;
	}

	return x;
}

static int
wavefront_get_sample_status (int assume_rom)

{
	int i;
	unsigned char rbuf[32], wbuf[32];
	unsigned int    sc_real, sc_alias, sc_multi;

	/* check sample status */
    
	if (wavefront_cmd (WFC_GET_NSAMPLES, rbuf, wbuf)) {
		printk (KERN_WARNING LOGNAME "cannot request sample count.\n");
		return -1;
	} 
    
	sc_real = sc_alias = sc_multi = dev.samples_used = 0;
    
	for (i = 0; i < WF_MAX_SAMPLE; i++) {
	
		wbuf[0] = i & 0x7f;
		wbuf[1] = i >> 7;

		if (wavefront_cmd (WFC_IDENTIFY_SAMPLE_TYPE, rbuf, wbuf)) {
			printk (KERN_WARNING LOGNAME
				"cannot identify sample "
				"type of slot %d\n", i);
			dev.sample_status[i] = WF_ST_EMPTY;
			continue;
		}

		dev.sample_status[i] = (WF_SLOT_FILLED|rbuf[0]);

		if (assume_rom) {
			dev.sample_status[i] |= WF_SLOT_ROM;
		}

		switch (rbuf[0] & WF_ST_MASK) {
		case WF_ST_SAMPLE:
			sc_real++;
			break;
		case WF_ST_MULTISAMPLE:
			sc_multi++;
			break;
		case WF_ST_ALIAS:
			sc_alias++;
			break;
		case WF_ST_EMPTY:
			break;

		default:
			printk (KERN_WARNING LOGNAME "unknown sample type for "
				"slot %d (0x%x)\n", 
				i, rbuf[0]);
		}

		if (rbuf[0] != WF_ST_EMPTY) {
			dev.samples_used++;
		} 
	}

	printk (KERN_INFO LOGNAME
		"%d samples used (%d real, %d aliases, %d multi), "
		"%d empty\n", dev.samples_used, sc_real, sc_alias, sc_multi,
		WF_MAX_SAMPLE - dev.samples_used);


	return (0);

}

static int
wavefront_get_patch_status (void)

{
	unsigned char patchbuf[WF_PATCH_BYTES];
	unsigned char patchnum[2];
	wavefront_patch *p;
	int i, x, cnt, cnt2;

	for (i = 0; i < WF_MAX_PATCH; i++) {
		patchnum[0] = i & 0x7f;
		patchnum[1] = i >> 7;

		if ((x = wavefront_cmd (WFC_UPLOAD_PATCH, patchbuf,
					patchnum)) == 0) {

			dev.patch_status[i] |= WF_SLOT_FILLED;
			p = (wavefront_patch *) patchbuf;
			dev.sample_status
				[p->sample_number|(p->sample_msb<<7)] |=
				WF_SLOT_USED;
	    
		} else if (x == 3) { /* Bad patch number */
			dev.patch_status[i] = 0;
		} else {
			printk (KERN_ERR LOGNAME "upload patch "
				"error 0x%x\n", x);
			dev.patch_status[i] = 0;
			return 1;
		}
	}

	/* program status has already filled in slot_used bits */

	for (i = 0, cnt = 0, cnt2 = 0; i < WF_MAX_PATCH; i++) {
		if (dev.patch_status[i] & WF_SLOT_FILLED) {
			cnt++;
		}
		if (dev.patch_status[i] & WF_SLOT_USED) {
			cnt2++;
		}
	
	}
	printk (KERN_INFO LOGNAME
		"%d patch slots filled, %d in use\n", cnt, cnt2);

	return (0);
}

static int
wavefront_get_program_status (void)

{
	unsigned char progbuf[WF_PROGRAM_BYTES];
	wavefront_program prog;
	unsigned char prognum;
	int i, x, l, cnt;

	for (i = 0; i < WF_MAX_PROGRAM; i++) {
		prognum = i;

		if ((x = wavefront_cmd (WFC_UPLOAD_PROGRAM, progbuf,
					&prognum)) == 0) {

			dev.prog_status[i] |= WF_SLOT_USED;

			demunge_buf (progbuf, (unsigned char *) &prog,
				     WF_PROGRAM_BYTES);

			for (l = 0; l < WF_NUM_LAYERS; l++) {
				if (prog.layer[l].mute) {
					dev.patch_status
						[prog.layer[l].patch_number] |=
						WF_SLOT_USED;
				}
			}
		} else if (x == 1) { /* Bad program number */
			dev.prog_status[i] = 0;
		} else {
			printk (KERN_ERR LOGNAME "upload program "
				"error 0x%x\n", x);
			dev.prog_status[i] = 0;
		}
	}

	for (i = 0, cnt = 0; i < WF_MAX_PROGRAM; i++) {
		if (dev.prog_status[i]) {
			cnt++;
		}
	}

	printk (KERN_INFO LOGNAME "%d programs slots in use\n", cnt);

	return (0);
}

static int
wavefront_send_patch (wavefront_patch_info *header)

{
	unsigned char buf[WF_PATCH_BYTES+2];
	unsigned char *bptr;

	DPRINT (WF_DEBUG_LOAD_PATCH, "downloading patch %d\n",
				      header->number);

	dev.patch_status[header->number] |= WF_SLOT_FILLED;

	bptr = buf;
	bptr = munge_int32 (header->number, buf, 2);
	munge_buf ((unsigned char *)&header->hdr.p, bptr, WF_PATCH_BYTES);
    
	if (wavefront_cmd (WFC_DOWNLOAD_PATCH, NULL, buf)) {
		printk (KERN_ERR LOGNAME "download patch failed\n");
		return -(EIO);
	}

	return (0);
}

static int
wavefront_send_program (wavefront_patch_info *header)

{
	unsigned char buf[WF_PROGRAM_BYTES+1];
	int i;

	DPRINT (WF_DEBUG_LOAD_PATCH, "downloading program %d\n",
		header->number);

	dev.prog_status[header->number] = WF_SLOT_USED;

	/* XXX need to zero existing SLOT_USED bit for program_status[i]
	   where `i' is the program that's being (potentially) overwritten.
	*/
    
	for (i = 0; i < WF_NUM_LAYERS; i++) {
		if (header->hdr.pr.layer[i].mute) {
			dev.patch_status[header->hdr.pr.layer[i].patch_number] |=
				WF_SLOT_USED;

			/* XXX need to mark SLOT_USED for sample used by
			   patch_number, but this means we have to load it. Ick.
			*/
		}
	}

	buf[0] = header->number;
	munge_buf ((unsigned char *)&header->hdr.pr, &buf[1], WF_PROGRAM_BYTES);
    
	if (wavefront_cmd (WFC_DOWNLOAD_PROGRAM, NULL, buf)) {
		printk (KERN_WARNING LOGNAME "download patch failed\n");	
		return -(EIO);
	}

	return (0);
}

static int
wavefront_freemem (void)

{
	char rbuf[8];

	if (wavefront_cmd (WFC_REPORT_FREE_MEMORY, rbuf, NULL)) {
		printk (KERN_WARNING LOGNAME "can't get memory stats.\n");
		return -1;
	} else {
		return demunge_int32 (rbuf, 4);
	}
}

static int
wavefront_send_sample (wavefront_patch_info *header,
		       UINT16 __user *dataptr,
		       int data_is_unsigned)

{
	/* samples are downloaded via a 16-bit wide i/o port
	   (you could think of it as 2 adjacent 8-bit wide ports
	   but its less efficient that way). therefore, all
	   the blocksizes and so forth listed in the documentation,
	   and used conventionally to refer to sample sizes,
	   which are given in 8-bit units (bytes), need to be
	   divided by 2.
        */

	UINT16 sample_short;
	UINT32 length;
	UINT16 __user *data_end = NULL;
	unsigned int i;
	const int max_blksize = 4096/2;
	unsigned int written;
	unsigned int blocksize;
	int dma_ack;
	int blocknum;
	unsigned char sample_hdr[WF_SAMPLE_HDR_BYTES];
	unsigned char *shptr;
	int skip = 0;
	int initial_skip = 0;

	DPRINT (WF_DEBUG_LOAD_PATCH, "sample %sdownload for slot %d, "
				      "type %d, %d bytes from %p\n",
				      header->size ? "" : "header ", 
				      header->number, header->subkey,
				      header->size,
				      header->dataptr);

	if (header->number == WAVEFRONT_FIND_FREE_SAMPLE_SLOT) {
		int x;

		if ((x = wavefront_find_free_sample ()) < 0) {
			return -ENOMEM;
		}
		printk (KERN_DEBUG LOGNAME "unspecified sample => %d\n", x);
		header->number = x;
	}

	if (header->size) {

		/* XXX it's a debatable point whether or not RDONLY semantics
		   on the ROM samples should cover just the sample data or
		   the sample header. For now, it only covers the sample data,
		   so anyone is free at all times to rewrite sample headers.

		   My reason for this is that we have the sample headers
		   available in the WFB file for General MIDI, and so these
		   can always be reset if needed. The sample data, however,
		   cannot be recovered without a complete reset and firmware
		   reload of the ICS2115, which is a very expensive operation.

		   So, doing things this way allows us to honor the notion of
		   "RESETSAMPLES" reasonably cheaply. Note however, that this
		   is done purely at user level: there is no WFB parser in
		   this driver, and so a complete reset (back to General MIDI,
		   or theoretically some other configuration) is the
		   responsibility of the user level library. 

		   To try to do this in the kernel would be a little
		   crazy: we'd need 158K of kernel space just to hold
		   a copy of the patch/program/sample header data.
		*/

		if (dev.rom_samples_rdonly) {
			if (dev.sample_status[header->number] & WF_SLOT_ROM) {
				printk (KERN_ERR LOGNAME "sample slot %d "
					"write protected\n",
					header->number);
				return -EACCES;
			}
		}

		wavefront_delete_sample (header->number);
	}

	if (header->size) {
		dev.freemem = wavefront_freemem ();

		if (dev.freemem < header->size) {
			printk (KERN_ERR LOGNAME
				"insufficient memory to "
				"load %d byte sample.\n",
				header->size);
			return -ENOMEM;
		}
	
	}

	skip = WF_GET_CHANNEL(&header->hdr.s);

	if (skip > 0 && header->hdr.s.SampleResolution != LINEAR_16BIT) {
		printk (KERN_ERR LOGNAME "channel selection only "
			"possible on 16-bit samples");
		return -(EINVAL);
	}

	switch (skip) {
	case 0:
		initial_skip = 0;
		skip = 1;
		break;
	case 1:
		initial_skip = 0;
		skip = 2;
		break;
	case 2:
		initial_skip = 1;
		skip = 2;
		break;
	case 3:
		initial_skip = 2;
		skip = 3;
		break;
	case 4:
		initial_skip = 3;
		skip = 4;
		break;
	case 5:
		initial_skip = 4;
		skip = 5;
		break;
	case 6:
		initial_skip = 5;
		skip = 6;
		break;
	}

	DPRINT (WF_DEBUG_LOAD_PATCH, "channel selection: %d => "
				      "initial skip = %d, skip = %d\n",
				      WF_GET_CHANNEL (&header->hdr.s),
				      initial_skip, skip);
    
	/* Be safe, and zero the "Unused" bits ... */

	WF_SET_CHANNEL(&header->hdr.s, 0);

	/* adjust size for 16 bit samples by dividing by two.  We always
	   send 16 bits per write, even for 8 bit samples, so the length
	   is always half the size of the sample data in bytes.
	*/

	length = header->size / 2;

	/* the data we're sent has not been munged, and in fact, the
	   header we have to send isn't just a munged copy either.
	   so, build the sample header right here.
	*/

	shptr = &sample_hdr[0];

	shptr = munge_int32 (header->number, shptr, 2);

	if (header->size) {
		shptr = munge_int32 (length, shptr, 4);
	}

	/* Yes, a 4 byte result doesn't contain all of the offset bits,
	   but the offset only uses 24 bits.
	*/

	shptr = munge_int32 (*((UINT32 *) &header->hdr.s.sampleStartOffset),
			     shptr, 4);
	shptr = munge_int32 (*((UINT32 *) &header->hdr.s.loopStartOffset),
			     shptr, 4);
	shptr = munge_int32 (*((UINT32 *) &header->hdr.s.loopEndOffset),
			     shptr, 4);
	shptr = munge_int32 (*((UINT32 *) &header->hdr.s.sampleEndOffset),
			     shptr, 4);
	
	/* This one is truly weird. What kind of weirdo decided that in
	   a system dominated by 16 and 32 bit integers, they would use
	   a just 12 bits ?
	*/
	
	shptr = munge_int32 (header->hdr.s.FrequencyBias, shptr, 3);
	
	/* Why is this nybblified, when the MSB is *always* zero ? 
	   Anyway, we can't take address of bitfield, so make a
	   good-faith guess at where it starts.
	*/
	
	shptr = munge_int32 (*(&header->hdr.s.FrequencyBias+1),
			     shptr, 2);

	if (wavefront_cmd (header->size ?
			   WFC_DOWNLOAD_SAMPLE : WFC_DOWNLOAD_SAMPLE_HEADER,
			   NULL, sample_hdr)) {
		printk (KERN_WARNING LOGNAME "sample %sdownload refused.\n",
			header->size ? "" : "header ");
		return -(EIO);
	}

	if (header->size == 0) {
		goto sent; /* Sorry. Just had to have one somewhere */
	}
    
	data_end = dataptr + length;

	/* Do any initial skip over an unused channel's data */

	dataptr += initial_skip;
    
	for (written = 0, blocknum = 0;
	     written < length; written += max_blksize, blocknum++) {
	
		if ((length - written) > max_blksize) {
			blocksize = max_blksize;
		} else {
			/* round to nearest 16-byte value */
			blocksize = ((length-written+7)&~0x7);
		}

		if (wavefront_cmd (WFC_DOWNLOAD_BLOCK, NULL, NULL)) {
			printk (KERN_WARNING LOGNAME "download block "
				"request refused.\n");
			return -(EIO);
		}

		for (i = 0; i < blocksize; i++) {

			if (dataptr < data_end) {
		
				__get_user (sample_short, dataptr);
				dataptr += skip;
		
				if (data_is_unsigned) { /* GUS ? */

					if (WF_SAMPLE_IS_8BIT(&header->hdr.s)) {
			
						/* 8 bit sample
						 resolution, sign
						 extend both bytes.
						*/
			
						((unsigned char*)
						 &sample_short)[0] += 0x7f;
						((unsigned char*)
						 &sample_short)[1] += 0x7f;
			
					} else {
			
						/* 16 bit sample
						 resolution, sign
						 extend the MSB.
						*/
			
						sample_short += 0x7fff;
					}
				}

			} else {

				/* In padding section of final block:

				   Don't fetch unsupplied data from
				   user space, just continue with
				   whatever the final value was.
				*/
			}
	    
			if (i < blocksize - 1) {
				outw (sample_short, dev.block_port);
			} else {
				outw (sample_short, dev.last_block_port);
			}
		}

		/* Get "DMA page acknowledge", even though its really
		   nothing to do with DMA at all.
		*/
	
		if ((dma_ack = wavefront_read ()) != WF_DMA_ACK) {
			if (dma_ack == -1) {
				printk (KERN_ERR LOGNAME "upload sample "
					"DMA ack timeout\n");
				return -(EIO);
			} else {
				printk (KERN_ERR LOGNAME "upload sample "
					"DMA ack error 0x%x\n",
					dma_ack);
				return -(EIO);
			}
		}
	}

	dev.sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_SAMPLE);

	/* Note, label is here because sending the sample header shouldn't
	   alter the sample_status info at all.
	*/

 sent:
	return (0);
}

static int
wavefront_send_alias (wavefront_patch_info *header)

{
	unsigned char alias_hdr[WF_ALIAS_BYTES];

	DPRINT (WF_DEBUG_LOAD_PATCH, "download alias, %d is "
				      "alias for %d\n",
				      header->number,
				      header->hdr.a.OriginalSample);
    
	munge_int32 (header->number, &alias_hdr[0], 2);
	munge_int32 (header->hdr.a.OriginalSample, &alias_hdr[2], 2);
	munge_int32 (*((unsigned int *)&header->hdr.a.sampleStartOffset),
		     &alias_hdr[4], 4);
	munge_int32 (*((unsigned int *)&header->hdr.a.loopStartOffset),
		     &alias_hdr[8], 4);
	munge_int32 (*((unsigned int *)&header->hdr.a.loopEndOffset),
		     &alias_hdr[12], 4);
	munge_int32 (*((unsigned int *)&header->hdr.a.sampleEndOffset),
		     &alias_hdr[16], 4);
	munge_int32 (header->hdr.a.FrequencyBias, &alias_hdr[20], 3);
	munge_int32 (*(&header->hdr.a.FrequencyBias+1), &alias_hdr[23], 2);

	if (wavefront_cmd (WFC_DOWNLOAD_SAMPLE_ALIAS, NULL, alias_hdr)) {
		printk (KERN_ERR LOGNAME "download alias failed.\n");
		return -(EIO);
	}

	dev.sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_ALIAS);

	return (0);
}

static int
wavefront_send_multisample (wavefront_patch_info *header)
{
	int i;
	int num_samples;
	unsigned char msample_hdr[WF_MSAMPLE_BYTES];

	munge_int32 (header->number, &msample_hdr[0], 2);

	/* You'll recall at this point that the "number of samples" value
	   in a wavefront_multisample struct is actually the log2 of the
	   real number of samples.
	*/

	num_samples = (1<<(header->hdr.ms.NumberOfSamples&7));
	msample_hdr[2] = (unsigned char) header->hdr.ms.NumberOfSamples;

	DPRINT (WF_DEBUG_LOAD_PATCH, "multi %d with %d=%d samples\n",
				      header->number,
				      header->hdr.ms.NumberOfSamples,
				      num_samples);

	for (i = 0; i < num_samples; i++) {
		DPRINT(WF_DEBUG_LOAD_PATCH|WF_DEBUG_DATA, "sample[%d] = %d\n",
		       i, header->hdr.ms.SampleNumber[i]);
		munge_int32 (header->hdr.ms.SampleNumber[i],
		     &msample_hdr[3+(i*2)], 2);
	}
    
	/* Need a hack here to pass in the number of bytes
	   to be written to the synth. This is ugly, and perhaps
	   one day, I'll fix it.
	*/

	if (wavefront_cmd (WFC_DOWNLOAD_MULTISAMPLE, 
			   (unsigned char *) ((num_samples*2)+3),
			   msample_hdr)) {
		printk (KERN_ERR LOGNAME "download of multisample failed.\n");
		return -(EIO);
	}

	dev.sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_MULTISAMPLE);

	return (0);
}

static int
wavefront_fetch_multisample (wavefront_patch_info *header)
{
	int i;
	unsigned char log_ns[1];
	unsigned char number[2];
	int num_samples;

	munge_int32 (header->number, number, 2);
    
	if (wavefront_cmd (WFC_UPLOAD_MULTISAMPLE, log_ns, number)) {
		printk (KERN_ERR LOGNAME "upload multisample failed.\n");
		return -(EIO);
	}
    
	DPRINT (WF_DEBUG_DATA, "msample %d has %d samples\n",
				header->number, log_ns[0]);

	header->hdr.ms.NumberOfSamples = log_ns[0];

	/* get the number of samples ... */

	num_samples = (1 << log_ns[0]);
    
	for (i = 0; i < num_samples; i++) {
		s8 d[2];
	
		if ((d[0] = wavefront_read ()) == -1) {
			printk (KERN_ERR LOGNAME "upload multisample failed "
				"during sample loop.\n");
			return -(EIO);
		}

		if ((d[1] = wavefront_read ()) == -1) {
			printk (KERN_ERR LOGNAME "upload multisample failed "
				"during sample loop.\n");
			return -(EIO);
		}
	
		header->hdr.ms.SampleNumber[i] =
			demunge_int32 ((unsigned char *) d, 2);
	
		DPRINT (WF_DEBUG_DATA, "msample sample[%d] = %d\n",
					i, header->hdr.ms.SampleNumber[i]);
	}

	return (0);
}


static int
wavefront_send_drum (wavefront_patch_info *header)

{
	unsigned char drumbuf[WF_DRUM_BYTES];
	wavefront_drum *drum = &header->hdr.d;
	int i;

	DPRINT (WF_DEBUG_LOAD_PATCH, "downloading edrum for MIDI "
		"note %d, patch = %d\n", 
		header->number, drum->PatchNumber);

	drumbuf[0] = header->number & 0x7f;

	for (i = 0; i < 4; i++) {
		munge_int32 (((unsigned char *)drum)[i], &drumbuf[1+(i*2)], 2);
	}

	if (wavefront_cmd (WFC_DOWNLOAD_EDRUM_PROGRAM, NULL, drumbuf)) {
		printk (KERN_ERR LOGNAME "download drum failed.\n");
		return -(EIO);
	}

	return (0);
}

static int 
wavefront_find_free_sample (void)

{
	int i;

	for (i = 0; i < WF_MAX_SAMPLE; i++) {
		if (!(dev.sample_status[i] & WF_SLOT_FILLED)) {
			return i;
		}
	}
	printk (KERN_WARNING LOGNAME "no free sample slots!\n");
	return -1;
}

static int 
wavefront_find_free_patch (void)

{
	int i;

	for (i = 0; i < WF_MAX_PATCH; i++) {
		if (!(dev.patch_status[i] & WF_SLOT_FILLED)) {
			return i;
		}
	}
	printk (KERN_WARNING LOGNAME "no free patch slots!\n");
	return -1;
}

static int 
log2_2048(int n)

{
	int tbl[]={0, 0, 2048, 3246, 4096, 4755, 5294, 5749, 6143,
		   6492, 6803, 7084, 7342, 7578, 7797, 8001, 8192,
		   8371, 8540, 8699, 8851, 8995, 9132, 9264, 9390,
		   9510, 9626, 9738, 9845, 9949, 10049, 10146};
	int i;

	/* Returns 2048*log2(n) */

	/* FIXME: this is like doing integer math
	   on quantum particles (RuN) */

	i=0;
	while(n>=32*256) {
		n>>=8;
		i+=2048*8;
	}
	while(n>=32) {
		n>>=1;
		i+=2048;
	}
	i+=tbl[n];
	return(i);
}

static int
wavefront_load_gus_patch (int devno, int format, const char __user *addr,
			  int offs, int count, int pmgr_flag)
{
	struct patch_info guspatch;
	wavefront_patch_info *samp, *pat, *prog;
	wavefront_patch *patp;
	wavefront_sample *sampp;
	wavefront_program *progp;

	int i,base_note;
	long sizeof_patch;
	int rc = -ENOMEM;

	samp = kmalloc(3 * sizeof(wavefront_patch_info), GFP_KERNEL);
	if (!samp)
		goto free_fail;
	pat = samp + 1;
	prog = pat + 1;

	/* Copy in the header of the GUS patch */

	sizeof_patch = (long) &guspatch.data[0] - (long) &guspatch; 
	if (copy_from_user(&((char *) &guspatch)[offs],
			   &(addr)[offs], sizeof_patch - offs)) {
		rc = -EFAULT;
		goto free_fail;
	}

	if ((i = wavefront_find_free_patch ()) == -1) {
		rc = -EBUSY;
		goto free_fail;
	}
	pat->number = i;
	pat->subkey = WF_ST_PATCH;
	patp = &pat->hdr.p;

	if ((i = wavefront_find_free_sample ()) == -1) {
		rc = -EBUSY;
		goto free_fail;
	}
	samp->number = i;
	samp->subkey = WF_ST_SAMPLE;
	samp->size = guspatch.len;
	sampp = &samp->hdr.s;

	prog->number = guspatch.instr_no;
	progp = &prog->hdr.pr;

	/* Setup the patch structure */

	patp->amplitude_bias=guspatch.volume;
	patp->portamento=0;
	patp->sample_number= samp->number & 0xff;
	patp->sample_msb= samp->number >> 8;
	patp->pitch_bend= /*12*/ 0;
	patp->mono=1;
	patp->retrigger=1;
	patp->nohold=(guspatch.mode & WAVE_SUSTAIN_ON) ? 0:1;
	patp->frequency_bias=0;
	patp->restart=0;
	patp->reuse=0;
	patp->reset_lfo=1;
	patp->fm_src2=0;
	patp->fm_src1=WF_MOD_MOD_WHEEL;
	patp->am_src=WF_MOD_PRESSURE;
	patp->am_amount=127;
	patp->fc1_mod_amount=0;
	patp->fc2_mod_amount=0; 
	patp->fm_amount1=0;
	patp->fm_amount2=0;
	patp->envelope1.attack_level=127;
	patp->envelope1.decay1_level=127;
	patp->envelope1.decay2_level=127;
	patp->envelope1.sustain_level=127;
	patp->envelope1.release_level=0;
	patp->envelope2.attack_velocity=127;
	patp->envelope2.attack_level=127;
	patp->envelope2.decay1_level=127;
	patp->envelope2.decay2_level=127;
	patp->envelope2.sustain_level=127;
	patp->envelope2.release_level=0;
	patp->envelope2.attack_velocity=127;
	patp->randomizer=0;

	/* Program for this patch */

	progp->layer[0].patch_number= pat->number; /* XXX is this right ? */
	progp->layer[0].mute=1;
	progp->layer[0].pan_or_mod=1;
	progp->layer[0].pan=7;
	progp->layer[0].mix_level=127  /* guspatch.volume */;
	progp->layer[0].split_type=0;
	progp->layer[0].split_point=0;
	progp->layer[0].play_below=0;

	for (i = 1; i < 4; i++) {
		progp->layer[i].mute=0;
	}

	/* Sample data */

	sampp->SampleResolution=((~guspatch.mode & WAVE_16_BITS)<<1);

	for (base_note=0;
	     note_to_freq (base_note) < guspatch.base_note;
	     base_note++);

	if ((guspatch.base_note-note_to_freq(base_note))
	    >(note_to_freq(base_note)-guspatch.base_note))
		base_note++;

	printk(KERN_DEBUG "ref freq=%d,base note=%d\n",
	       guspatch.base_freq,
	       base_note);

	sampp->FrequencyBias = (29550 - log2_2048(guspatch.base_freq)
				+ base_note*171);
	printk(KERN_DEBUG "Freq Bias is %d\n", sampp->FrequencyBias);
	sampp->Loop=(guspatch.mode & WAVE_LOOPING) ? 1:0;
	sampp->sampleStartOffset.Fraction=0;
	sampp->sampleStartOffset.Integer=0;
	sampp->loopStartOffset.Fraction=0;
	sampp->loopStartOffset.Integer=guspatch.loop_start
		>>((guspatch.mode&WAVE_16_BITS) ? 1:0);
	sampp->loopEndOffset.Fraction=0;
	sampp->loopEndOffset.Integer=guspatch.loop_end
		>>((guspatch.mode&WAVE_16_BITS) ? 1:0);
	sampp->sampleEndOffset.Fraction=0;
	sampp->sampleEndOffset.Integer=guspatch.len >> (guspatch.mode&1);
	sampp->Bidirectional=(guspatch.mode&WAVE_BIDIR_LOOP) ? 1:0;
	sampp->Reverse=(guspatch.mode&WAVE_LOOP_BACK) ? 1:0;

	/* Now ship it down */

	wavefront_send_sample (samp,
			       (unsigned short __user *) &(addr)[sizeof_patch],
			       (guspatch.mode & WAVE_UNSIGNED) ? 1:0);
	wavefront_send_patch (pat);
	wavefront_send_program (prog);

	/* Now pan as best we can ... use the slave/internal MIDI device
	   number if it exists (since it talks to the WaveFront), or the
	   master otherwise.
	*/

	if (dev.mididev > 0) {
		midi_synth_controller (dev.mididev, guspatch.instr_no, 10,
				       ((guspatch.panning << 4) > 127) ?
				       127 : (guspatch.panning << 4));
	}
	rc = 0;

free_fail:
	kfree(samp);
	return rc;
}

static int
wavefront_load_patch (const char __user *addr)


{
	wavefront_patch_info header;
	
	if (copy_from_user (&header, addr, sizeof(wavefront_patch_info) -
			    sizeof(wavefront_any))) {
		printk (KERN_WARNING LOGNAME "bad address for load patch.\n");
		return -EFAULT;
	}

	DPRINT (WF_DEBUG_LOAD_PATCH, "download "
				      "Sample type: %d "
				      "Sample number: %d "
				      "Sample size: %d\n",
				      header.subkey,
				      header.number,
				      header.size);

	switch (header.subkey) {
	case WF_ST_SAMPLE:  /* sample or sample_header, based on patch->size */

		if (copy_from_user((unsigned char *) &header.hdr.s,
				   (unsigned char __user *) header.hdrptr,
				   sizeof (wavefront_sample)))
			return -EFAULT;

		return wavefront_send_sample (&header, header.dataptr, 0);

	case WF_ST_MULTISAMPLE:

		if (copy_from_user(&header.hdr.s, header.hdrptr,
				   sizeof(wavefront_multisample)))
			return -EFAULT;

		return wavefront_send_multisample (&header);


	case WF_ST_ALIAS:

		if (copy_from_user(&header.hdr.a, header.hdrptr,
				   sizeof (wavefront_alias)))
			return -EFAULT;

		return wavefront_send_alias (&header);

	case WF_ST_DRUM:
		if (copy_from_user(&header.hdr.d, header.hdrptr,
				   sizeof (wavefront_drum)))
			return -EFAULT;

		return wavefront_send_drum (&header);

	case WF_ST_PATCH:
		if (copy_from_user(&header.hdr.p, header.hdrptr,
				   sizeof (wavefront_patch)))
			return -EFAULT;

		return wavefront_send_patch (&header);

	case WF_ST_PROGRAM:
		if (copy_from_user(&header.hdr.pr, header.hdrptr,
				   sizeof (wavefront_program)))
			return -EFAULT;

		return wavefront_send_program (&header);

	default:
		printk (KERN_ERR LOGNAME "unknown patch type %d.\n",
			header.subkey);
		return -(EINVAL);
	}

	return 0;
}

/***********************************************************************
WaveFront: /dev/sequencer{,2} and other hardware-dependent interfaces
***********************************************************************/

static void
process_sample_hdr (UCHAR8 *buf)

{
	wavefront_sample s;
	UCHAR8 *ptr;

	ptr = buf;

	/* The board doesn't send us an exact copy of a "wavefront_sample"
	   in response to an Upload Sample Header command. Instead, we 
	   have to convert the data format back into our data structure,
	   just as in the Download Sample command, where we have to do
	   something very similar in the reverse direction.
	*/

	*((UINT32 *) &s.sampleStartOffset) = demunge_int32 (ptr, 4); ptr += 4;
	*((UINT32 *) &s.loopStartOffset) = demunge_int32 (ptr, 4); ptr += 4;
	*((UINT32 *) &s.loopEndOffset) = demunge_int32 (ptr, 4); ptr += 4;
	*((UINT32 *) &s.sampleEndOffset) = demunge_int32 (ptr, 4); ptr += 4;
	*((UINT32 *) &s.FrequencyBias) = demunge_int32 (ptr, 3); ptr += 3;

	s.SampleResolution = *ptr & 0x3;
	s.Loop = *ptr & 0x8;
	s.Bidirectional = *ptr & 0x10;
	s.Reverse = *ptr & 0x40;

	/* Now copy it back to where it came from */

	memcpy (buf, (unsigned char *) &s, sizeof (wavefront_sample));
}

static int
wavefront_synth_control (int cmd, wavefront_control *wc)

{
	unsigned char patchnumbuf[2];
	int i;

	DPRINT (WF_DEBUG_CMD, "synth control with "
		"cmd 0x%x\n", wc->cmd);

	/* Pre-handling of or for various commands */

	switch (wc->cmd) {
	case WFC_DISABLE_INTERRUPTS:
		printk (KERN_INFO LOGNAME "interrupts disabled.\n");
		outb (0x80|0x20, dev.control_port);
		dev.interrupts_on = 0;
		return 0;

	case WFC_ENABLE_INTERRUPTS:
		printk (KERN_INFO LOGNAME "interrupts enabled.\n");
		outb (0x80|0x40|0x20, dev.control_port);
		dev.interrupts_on = 1;
		return 0;

	case WFC_INTERRUPT_STATUS:
		wc->rbuf[0] = dev.interrupts_on;
		return 0;

	case WFC_ROMSAMPLES_RDONLY:
		dev.rom_samples_rdonly = wc->wbuf[0];
		wc->status = 0;
		return 0;

	case WFC_IDENTIFY_SLOT_TYPE:
		i = wc->wbuf[0] | (wc->wbuf[1] << 7);
		if (i <0 || i >= WF_MAX_SAMPLE) {
			printk (KERN_WARNING LOGNAME "invalid slot ID %d\n",
				i);
			wc->status = EINVAL;
			return 0;
		}
		wc->rbuf[0] = dev.sample_status[i];
		wc->status = 0;
		return 0;

	case WFC_DEBUG_DRIVER:
		dev.debug = wc->wbuf[0];
		printk (KERN_INFO LOGNAME "debug = 0x%x\n", dev.debug);
		return 0;

	case WFC_FX_IOCTL:
		wffx_ioctl ((wavefront_fx_info *) &wc->wbuf[0]);
		return 0;

	case WFC_UPLOAD_PATCH:
		munge_int32 (*((UINT32 *) wc->wbuf), patchnumbuf, 2);
		memcpy (wc->wbuf, patchnumbuf, 2);
		break;

	case WFC_UPLOAD_MULTISAMPLE:
		/* multisamples have to be handled differently, and
		   cannot be dealt with properly by wavefront_cmd() alone.
		*/
		wc->status = wavefront_fetch_multisample
			((wavefront_patch_info *) wc->rbuf);
		return 0;

	case WFC_UPLOAD_SAMPLE_ALIAS:
		printk (KERN_INFO LOGNAME "support for sample alias upload "
			"being considered.\n");
		wc->status = EINVAL;
		return -EINVAL;
	}

	wc->status = wavefront_cmd (wc->cmd, wc->rbuf, wc->wbuf);

	/* Post-handling of certain commands.

	   In particular, if the command was an upload, demunge the data
	   so that the user-level doesn't have to think about it.
	*/

	if (wc->status == 0) {
		switch (wc->cmd) {
			/* intercept any freemem requests so that we know
			   we are always current with the user-level view
			   of things.
			*/

		case WFC_REPORT_FREE_MEMORY:
			dev.freemem = demunge_int32 (wc->rbuf, 4);
			break;

		case WFC_UPLOAD_PATCH:
			demunge_buf (wc->rbuf, wc->rbuf, WF_PATCH_BYTES);
			break;

		case WFC_UPLOAD_PROGRAM:
			demunge_buf (wc->rbuf, wc->rbuf, WF_PROGRAM_BYTES);
			break;

		case WFC_UPLOAD_EDRUM_PROGRAM:
			demunge_buf (wc->rbuf, wc->rbuf, WF_DRUM_BYTES - 1);
			break;

		case WFC_UPLOAD_SAMPLE_HEADER:
			process_sample_hdr (wc->rbuf);
			break;

		case WFC_UPLOAD_SAMPLE_ALIAS:
			printk (KERN_INFO LOGNAME "support for "
				"sample aliases still "
				"being considered.\n");
			break;

		case WFC_VMIDI_OFF:
			if (virtual_midi_disable () < 0) {
				return -(EIO);
			}
			break;

		case WFC_VMIDI_ON:
			if (virtual_midi_enable () < 0) {
				return -(EIO);
			}
			break;
		}
	}

	return 0;
}


/***********************************************************************/
/* WaveFront: Linux file system interface (for access via raw synth)    */
/***********************************************************************/

static int 
wavefront_open (struct inode *inode, struct file *file)
{
	/* XXX fix me */
	dev.opened = file->f_flags;
	return 0;
}

static int
wavefront_release(struct inode *inode, struct file *file)
{
	lock_kernel();
	dev.opened = 0;
	dev.debug = 0;
	unlock_kernel();
	return 0;
}

static int
wavefront_ioctl(struct inode *inode, struct file *file,
		unsigned int cmd, unsigned long arg)
{
	wavefront_control wc;
	int err;

	switch (cmd) {

	case WFCTL_WFCMD:
		if (copy_from_user(&wc, (void __user *) arg, sizeof (wc)))
			return -EFAULT;
		
		if ((err = wavefront_synth_control (cmd, &wc)) == 0) {
			if (copy_to_user ((void __user *) arg, &wc, sizeof (wc)))
				return -EFAULT;
		}

		return err;
		
	case WFCTL_LOAD_SPP:
		return wavefront_load_patch ((const char __user *) arg);
		
	default:
		printk (KERN_WARNING LOGNAME "invalid ioctl %#x\n", cmd);
		return -(EINVAL);

	}
	return 0;
}

static /*const*/ struct file_operations wavefront_fops = {
	.owner		= THIS_MODULE,
	.llseek		= no_llseek,
	.ioctl		= wavefront_ioctl,
	.open		= wavefront_open,
	.release	= wavefront_release,
};


/***********************************************************************/
/* WaveFront: OSS installation and support interface                   */
/***********************************************************************/

#if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ

static struct synth_info wavefront_info =
{"Turtle Beach WaveFront", 0, SYNTH_TYPE_SAMPLE, SAMPLE_TYPE_WAVEFRONT,
 0, 32, 0, 0, SYNTH_CAP_INPUT};

static int
wavefront_oss_open (int devno, int mode)

{
	dev.opened = mode;
	return 0;
}

static void
wavefront_oss_close (int devno)

{
	dev.opened = 0;
	dev.debug = 0;
	return;
}

static int
wavefront_oss_ioctl (int devno, unsigned int cmd, void __user * arg)

{
	wavefront_control wc;
	int err;

	switch (cmd) {
	case SNDCTL_SYNTH_INFO:
		if(copy_to_user(arg, &wavefront_info, sizeof (wavefront_info)))
			return -EFAULT;
		return 0;

	case SNDCTL_SEQ_RESETSAMPLES:
//		printk (KERN_WARNING LOGNAME "driver cannot reset samples.\n");
		return 0; /* don't force an error */

	case SNDCTL_SEQ_PERCMODE:
		return 0; /* don't force an error */

	case SNDCTL_SYNTH_MEMAVL:
		if ((dev.freemem = wavefront_freemem ()) < 0) {
			printk (KERN_ERR LOGNAME "cannot get memory size\n");
			return -EIO;
		} else {
			return dev.freemem;
		}
		break;

	case SNDCTL_SYNTH_CONTROL:
		if(copy_from_user (&wc, arg, sizeof (wc)))
			err = -EFAULT;
		else if ((err = wavefront_synth_control (cmd, &wc)) == 0) {
			if(copy_to_user (arg, &wc, sizeof (wc)))
				err = -EFAULT;
		}

		return err;

	default:
		return -(EINVAL);
	}
}

static int
wavefront_oss_load_patch (int devno, int format, const char __user *addr,
			  int offs, int count, int pmgr_flag)
{

	if (format == SYSEX_PATCH) {	/* Handled by midi_synth.c */
		if (midi_load_patch == NULL) {
			printk (KERN_ERR LOGNAME
				"SYSEX not loadable: "
				"no midi patch loader!\n");
			return -(EINVAL);
		}

		return midi_load_patch (devno, format, addr,
					offs, count, pmgr_flag);

	} else if (format == GUS_PATCH) {
		return wavefront_load_gus_patch (devno, format,
						 addr, offs, count, pmgr_flag);

	} else if (format != WAVEFRONT_PATCH) {
		printk (KERN_ERR LOGNAME "unknown patch format %d\n", format);
		return -(EINVAL);
	}

	if (count < sizeof (wavefront_patch_info)) {
		printk (KERN_ERR LOGNAME "sample header too short\n");
		return -(EINVAL);
	}

	/* "addr" points to a user-space wavefront_patch_info */

	return wavefront_load_patch (addr);
}	

static struct synth_operations wavefront_operations =
{
	.owner		= THIS_MODULE,
	.id		= "WaveFront",
	.info		= &wavefront_info,
	.midi_dev	= 0,
	.synth_type	= SYNTH_TYPE_SAMPLE,
	.synth_subtype	= SAMPLE_TYPE_WAVEFRONT,
	.open		= wavefront_oss_open,
	.close		= wavefront_oss_close,
	.ioctl		= wavefront_oss_ioctl,
	.kill_note	= midi_synth_kill_note,
	.start_note	= midi_synth_start_note,
	.set_instr	= midi_synth_set_instr,
	.reset		= midi_synth_reset,
	.load_patch	= midi_synth_load_patch,
	.aftertouch	= midi_synth_aftertouch,
	.controller	= midi_synth_controller,
	.panning	= midi_synth_panning,
	.bender		= midi_synth_bender,
	.setup_voice	= midi_synth_setup_voice
};
#endif /* OSS_SUPPORT_SEQ */

#if OSS_SUPPORT_LEVEL & OSS_SUPPORT_STATIC_INSTALL

static void __init attach_wavefront (struct address_info *hw_config)
{
    (void) install_wavefront ();
}

static int __init probe_wavefront (struct address_info *hw_config)
{
    return !detect_wavefront (hw_config->irq, hw_config->io_base);
}

static void __exit unload_wavefront (struct address_info *hw_config) 
{
    (void) uninstall_wavefront ();
}

#endif /* OSS_SUPPORT_STATIC_INSTALL */

/***********************************************************************/
/* WaveFront: Linux modular sound kernel installation interface        */
/***********************************************************************/

static irqreturn_t
wavefrontintr(int irq, void *dev_id, struct pt_regs *dummy)
{
	struct wf_config *hw = dev_id;

	/*
	   Some comments on interrupts. I attempted a version of this
	   driver that used interrupts throughout the code instead of
	   doing busy and/or sleep-waiting. Alas, it appears that once
	   the Motorola firmware is downloaded, the card *never*
	   generates an RX interrupt. These are successfully generated
	   during firmware loading, and after that wavefront_status()
	   reports that an interrupt is pending on the card from time
	   to time, but it never seems to be delivered to this
	   driver. Note also that wavefront_status() continues to
	   report that RX interrupts are enabled, suggesting that I
	   didn't goof up and disable them by mistake.

	   Thus, I stepped back to a prior version of
	   wavefront_wait(), the only place where this really
	   matters. Its sad, but I've looked through the code to check
	   on things, and I really feel certain that the Motorola
	   firmware prevents RX-ready interrupts.
	*/

	if ((wavefront_status() & (STAT_INTR_READ|STAT_INTR_WRITE)) == 0) {
		return IRQ_NONE;
	}

	hw->irq_ok = 1;
	hw->irq_cnt++;
	wake_up_interruptible (&hw->interrupt_sleeper);
	return IRQ_HANDLED;
}

/* STATUS REGISTER 

0 Host Rx Interrupt Enable (1=Enabled)
1 Host Rx Register Full (1=Full)
2 Host Rx Interrupt Pending (1=Interrupt)
3 Unused
4 Host Tx Interrupt (1=Enabled)
5 Host Tx Register empty (1=Empty)
6 Host Tx Interrupt Pending (1=Interrupt)
7 Unused
*/

static int
wavefront_interrupt_bits (int irq)

{
	int bits;

	switch (irq) {
	case 9:
		bits = 0x00;
		break;
	case 5:
		bits = 0x08;
		break;
	case 12:
		bits = 0x10;
		break;
	case 15:
		bits = 0x18;
		break;
	
	default:
		printk (KERN_WARNING LOGNAME "invalid IRQ %d\n", irq);
		bits = -1;
	}

	return bits;
}

static void
wavefront_should_cause_interrupt (int val, int port, int timeout)

{
	unsigned long flags;

	/* this will not help on SMP - but at least it compiles */
	spin_lock_irqsave(&lock, flags);
	dev.irq_ok = 0;
	outb (val,port);
	interruptible_sleep_on_timeout (&dev.interrupt_sleeper, timeout);
	spin_unlock_irqrestore(&lock,flags);
}

static int __init wavefront_hw_reset (void)
{
	int bits;
	int hwv[2];
	unsigned long irq_mask;
	short reported_irq;

	/* IRQ already checked in init_module() */

	bits = wavefront_interrupt_bits (dev.irq);

	printk (KERN_DEBUG LOGNAME "autodetecting WaveFront IRQ\n");

	irq_mask = probe_irq_on ();

	outb (0x0, dev.control_port); 
	outb (0x80 | 0x40 | bits, dev.data_port);	
	wavefront_should_cause_interrupt(0x80|0x40|0x10|0x1,
					 dev.control_port,
					 (reset_time*HZ)/100);

	reported_irq = probe_irq_off (irq_mask);

	if (reported_irq != dev.irq) {
		if (reported_irq == 0) {
			printk (KERN_ERR LOGNAME
				"No unassigned interrupts detected "
				"after h/w reset\n");
		} else if (reported_irq < 0) {
			printk (KERN_ERR LOGNAME
				"Multiple unassigned interrupts detected "
				"after h/w reset\n");
		} else {
			printk (KERN_ERR LOGNAME "autodetected IRQ %d not the "
				"value provided (%d)\n", reported_irq,
				dev.irq);
		}
		dev.irq = -1;
		return 1;
	} else {
		printk (KERN_INFO LOGNAME "autodetected IRQ at %d\n",
			reported_irq);
	}

	if (request_irq (dev.irq, wavefrontintr,
			 IRQF_DISABLED|IRQF_SHARED,
			 "wavefront synth", &dev) < 0) {
		printk (KERN_WARNING LOGNAME "IRQ %d not available!\n",
			dev.irq);
		return 1;
	}

	/* try reset of port */
      
	outb (0x0, dev.control_port); 
  
	/* At this point, the board is in reset, and the H/W initialization
	   register is accessed at the same address as the data port.
     
	   Bit 7 - Enable IRQ Driver	
	   0 - Tri-state the Wave-Board drivers for the PC Bus IRQs
	   1 - Enable IRQ selected by bits 5:3 to be driven onto the PC Bus.
     
	   Bit 6 - MIDI Interface Select

	   0 - Use the MIDI Input from the 26-pin WaveBlaster
	   compatible header as the serial MIDI source
	   1 - Use the MIDI Input from the 9-pin D connector as the
	   serial MIDI source.
     
	   Bits 5:3 - IRQ Selection
	   0 0 0 - IRQ 2/9
	   0 0 1 - IRQ 5
	   0 1 0 - IRQ 12
	   0 1 1 - IRQ 15
	   1 0 0 - Reserved
	   1 0 1 - Reserved
	   1 1 0 - Reserved
	   1 1 1 - Reserved
     
	   Bits 2:1 - Reserved
	   Bit 0 - Disable Boot ROM
	   0 - memory accesses to 03FC30-03FFFFH utilize the internal Boot ROM
	   1 - memory accesses to 03FC30-03FFFFH are directed to external 
	   storage.
     
	*/

	/* configure hardware: IRQ, enable interrupts, 
	   plus external 9-pin MIDI interface selected
	*/

	outb (0x80 | 0x40 | bits, dev.data_port);	
  
	/* CONTROL REGISTER

	   0 Host Rx Interrupt Enable (1=Enabled)      0x1
	   1 Unused                                    0x2
	   2 Unused                                    0x4
	   3 Unused                                    0x8
	   4 Host Tx Interrupt Enable                 0x10
	   5 Mute (0=Mute; 1=Play)                    0x20
	   6 Master Interrupt Enable (1=Enabled)      0x40
	   7 Master Reset (0=Reset; 1=Run)            0x80

	   Take us out of reset, mute output, master + TX + RX interrupts on.
	   
	   We'll get an interrupt presumably to tell us that the TX
	   register is clear.
	*/

	wavefront_should_cause_interrupt(0x80|0x40|0x10|0x1,
					 dev.control_port,
					 (reset_time*HZ)/100);

	/* Note: data port is now the data port, not the h/w initialization
	   port.
	 */

	if (!dev.irq_ok) {
		printk (KERN_WARNING LOGNAME
			"intr not received after h/w un-reset.\n");
		goto gone_bad;
	} 

	dev.interrupts_on = 1;
	
	/* Note: data port is now the data port, not the h/w initialization
	   port.

	   At this point, only "HW VERSION" or "DOWNLOAD OS" commands
	   will work. So, issue one of them, and wait for TX
	   interrupt. This can take a *long* time after a cold boot,
	   while the ISC ROM does its RAM test. The SDK says up to 4
	   seconds - with 12MB of RAM on a Tropez+, it takes a lot
	   longer than that (~16secs). Note that the card understands
	   the difference between a warm and a cold boot, so
	   subsequent ISC2115 reboots (say, caused by module
	   reloading) will get through this much faster.

	   XXX Interesting question: why is no RX interrupt received first ?
	*/

	wavefront_should_cause_interrupt(WFC_HARDWARE_VERSION, 
					 dev.data_port, ramcheck_time*HZ);

	if (!dev.irq_ok) {
		printk (KERN_WARNING LOGNAME
			"post-RAM-check interrupt not received.\n");
		goto gone_bad;
	} 

	if (!wavefront_wait (STAT_CAN_READ)) {
		printk (KERN_WARNING LOGNAME
			"no response to HW version cmd.\n");
		goto gone_bad;
	}
	
	if ((hwv[0] = wavefront_read ()) == -1) {
		printk (KERN_WARNING LOGNAME
			"board not responding correctly.\n");
		goto gone_bad;
	}

	if (hwv[0] == 0xFF) { /* NAK */

		/* Board's RAM test failed. Try to read error code,
		   and tell us about it either way.
		*/
		
		if ((hwv[0] = wavefront_read ()) == -1) {
			printk (KERN_WARNING LOGNAME "on-board RAM test failed "
				"(bad error code).\n");
		} else {
			printk (KERN_WARNING LOGNAME "on-board RAM test failed "
				"(error code: 0x%x).\n",
				hwv[0]);
		}
		goto gone_bad;
	}

	/* We're OK, just get the next byte of the HW version response */

	if ((hwv[1] = wavefront_read ()) == -1) {
		printk (KERN_WARNING LOGNAME "incorrect h/w response.\n");
		goto gone_bad;
	}

	printk (KERN_INFO LOGNAME "hardware version %d.%d\n",
		hwv[0], hwv[1]);

	return 0;


     gone_bad:
	if (dev.irq >= 0) {
		free_irq (dev.irq, &dev);
		dev.irq = -1;
	}
	return (1);
}

static int __init detect_wavefront (int irq, int io_base)
{
	unsigned char   rbuf[4], wbuf[4];

	/* TB docs say the device takes up 8 ports, but we know that
	   if there is an FX device present (i.e. a Tropez+) it really
	   consumes 16.
	*/

	if (!request_region (io_base, 16, "wavfront")) {
		printk (KERN_ERR LOGNAME "IO address range 0x%x - 0x%x "
			"already in use - ignored\n", dev.base,
			dev.base+15);
		return -1;
	}
  
	dev.irq = irq;
	dev.base = io_base;
	dev.israw = 0;
	dev.debug = debug_default;
	dev.interrupts_on = 0;
	dev.irq_cnt = 0;
	dev.rom_samples_rdonly = 1; /* XXX default lock on ROM sample slots */

	if (wavefront_cmd (WFC_FIRMWARE_VERSION, rbuf, wbuf) == 0) {

		dev.fw_version[0] = rbuf[0];
		dev.fw_version[1] = rbuf[1];
		printk (KERN_INFO LOGNAME
			"firmware %d.%d already loaded.\n",
			rbuf[0], rbuf[1]);

		/* check that a command actually works */
      
		if (wavefront_cmd (WFC_HARDWARE_VERSION,
				   rbuf, wbuf) == 0) {
			dev.hw_version[0] = rbuf[0];
			dev.hw_version[1] = rbuf[1];
		} else {
			printk (KERN_WARNING LOGNAME "not raw, but no "
				"hardware version!\n");
			release_region (io_base, 16);
			return 0;
		}

		if (!wf_raw) {
			/* will re-acquire region in install_wavefront() */
			release_region (io_base, 16);
			return 1;
		} else {
			printk (KERN_INFO LOGNAME
				"reloading firmware anyway.\n");
			dev.israw = 1;
		}

	} else {

		dev.israw = 1;
		printk (KERN_INFO LOGNAME
			"no response to firmware probe, assume raw.\n");

	}

	init_waitqueue_head (&dev.interrupt_sleeper);

	if (wavefront_hw_reset ()) {
		printk (KERN_WARNING LOGNAME "hardware reset failed\n");
		release_region (io_base, 16);
		return 0;
	}

	/* Check for FX device, present only on Tropez+ */

	dev.has_fx = (detect_wffx () == 0);

	/* will re-acquire region in install_wavefront() */
	release_region (io_base, 16);
	return 1;
}

#include "os.h"
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <asm/uaccess.h>


static int
wavefront_download_firmware (char *path)

{
	unsigned char section[WF_SECTION_MAX];
	char section_length; /* yes, just a char; max value is WF_SECTION_MAX */
	int section_cnt_downloaded = 0;
	int fd;
	int c;
	int i;
	mm_segment_t fs;

	/* This tries to be a bit cleverer than the stuff Alan Cox did for
	   the generic sound firmware, in that it actually knows
	   something about the structure of the Motorola firmware. In
	   particular, it uses a version that has been stripped of the
	   20K of useless header information, and had section lengths
	   added, making it possible to load the entire OS without any
	   [kv]malloc() activity, since the longest entity we ever read is
	   42 bytes (well, WF_SECTION_MAX) long.
	*/

	fs = get_fs();
	set_fs (get_ds());

	if ((fd = sys_open (path, 0, 0)) < 0) {
		printk (KERN_WARNING LOGNAME "Unable to load \"%s\".\n",
			path);
		return 1;
	}

	while (1) {
		int x;

		if ((x = sys_read (fd, &section_length, sizeof (section_length))) !=
		    sizeof (section_length)) {
			printk (KERN_ERR LOGNAME "firmware read error.\n");
			goto failure;
		}

		if (section_length == 0) {
			break;
		}

		if (sys_read (fd, section, section_length) != section_length) {
			printk (KERN_ERR LOGNAME "firmware section "
				"read error.\n");
			goto failure;
		}

		/* Send command */
	
		if (wavefront_write (WFC_DOWNLOAD_OS)) {
			goto failure;
		}
	
		for (i = 0; i < section_length; i++) {
			if (wavefront_write (section[i])) {
				goto failure;
			}
		}
	
		/* get ACK */
	
		if (wavefront_wait (STAT_CAN_READ)) {

			if ((c = inb (dev.data_port)) != WF_ACK) {

				printk (KERN_ERR LOGNAME "download "
					"of section #%d not "
					"acknowledged, ack = 0x%x\n",
					section_cnt_downloaded + 1, c);
				goto failure;
		
			}

		} else {
			printk (KERN_ERR LOGNAME "time out for firmware ACK.\n");
			goto failure;
		}

	}

	sys_close (fd);
	set_fs (fs);
	return 0;

 failure:
	sys_close (fd);
	set_fs (fs);
	printk (KERN_ERR "\nWaveFront: firmware download failed!!!\n");
	return 1;
}

static int __init wavefront_config_midi (void)
{
	unsigned char rbuf[4], wbuf[4];
    
	if (detect_wf_mpu (dev.irq, dev.base) < 0) {
		printk (KERN_WARNING LOGNAME
			"could not find working MIDI device\n");
		return -1;
	} 

	if ((dev.mididev = install_wf_mpu ()) < 0) {
		printk (KERN_WARNING LOGNAME
			"MIDI interfaces not configured\n");
		return -1;
	}

	/* Route external MIDI to WaveFront synth (by default) */
    
	if (wavefront_cmd (WFC_MISYNTH_ON, rbuf, wbuf)) {
		printk (KERN_WARNING LOGNAME
			"cannot enable MIDI-IN to synth routing.\n");
		/* XXX error ? */
	}


#if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
	/* Get the regular MIDI patch loading function, so we can
	   use it if we ever get handed a SYSEX patch. This is
	   unlikely, because its so damn slow, but we may as well
	   leave this functionality from maui.c behind, since it
	   could be useful for sequencer applications that can
	   only use MIDI to do patch loading.
	*/

	if (midi_devs[dev.mididev]->converter != NULL) {
		midi_load_patch = midi_devs[dev.mididev]->converter->load_patch;
		midi_devs[dev.mididev]->converter->load_patch =
		    &wavefront_oss_load_patch;
	}

#endif /* OSS_SUPPORT_SEQ */
	
	/* Turn on Virtual MIDI, but first *always* turn it off,
	   since otherwise consectutive reloads of the driver will
	   never cause the hardware to generate the initial "internal" or 
	   "external" source bytes in the MIDI data stream. This
	   is pretty important, since the internal hardware generally will
	   be used to generate none or very little MIDI output, and
	   thus the only source of MIDI data is actually external. Without
	   the switch bytes, the driver will think it all comes from
	   the internal interface. Duh.
	*/

	if (wavefront_cmd (WFC_VMIDI_OFF, rbuf, wbuf)) { 
		printk (KERN_WARNING LOGNAME
			"virtual MIDI mode not disabled\n");
		return 0; /* We're OK, but missing the external MIDI dev */
	}

	if ((dev.ext_mididev = virtual_midi_enable ()) < 0) {
		printk (KERN_WARNING LOGNAME "no virtual MIDI access.\n");
	} else {
		if (wavefront_cmd (WFC_VMIDI_ON, rbuf, wbuf)) {
			printk (KERN_WARNING LOGNAME
				"cannot enable virtual MIDI mode.\n");
			virtual_midi_disable ();
		} 
	}
    
	return 0;
}

static int __init wavefront_do_reset (int atboot)
{
	char voices[1];

	if (!atboot && wavefront_hw_reset ()) {
		printk (KERN_WARNING LOGNAME "hw reset failed.\n");
		goto gone_bad;
	}

	if (dev.israw) {
		if (wavefront_download_firmware (ospath)) {
			goto gone_bad;
		}

		dev.israw = 0;

		/* Wait for the OS to get running. The protocol for
		   this is non-obvious, and was determined by
		   using port-IO tracing in DOSemu and some
		   experimentation here.
		   
		   Rather than using timed waits, use interrupts creatively.
		*/

		wavefront_should_cause_interrupt (WFC_NOOP,
						  dev.data_port,
						  (osrun_time*HZ));

		if (!dev.irq_ok) {
			printk (KERN_WARNING LOGNAME
				"no post-OS interrupt.\n");
			goto gone_bad;
		}
		
		/* Now, do it again ! */
		
		wavefront_should_cause_interrupt (WFC_NOOP,
						  dev.data_port, (10*HZ));
		
		if (!dev.irq_ok) {
			printk (KERN_WARNING LOGNAME
				"no post-OS interrupt(2).\n");
			goto gone_bad;
		}

		/* OK, no (RX/TX) interrupts any more, but leave mute
		   in effect. 
		*/
		
		outb (0x80|0x40, dev.control_port); 

		/* No need for the IRQ anymore */
		
		free_irq (dev.irq, &dev);

	}

	if (dev.has_fx && fx_raw) {
		wffx_init ();
	}

	/* SETUPSND.EXE asks for sample memory config here, but since i
	   have no idea how to interpret the result, we'll forget
	   about it.
	*/
	
	if ((dev.freemem = wavefront_freemem ()) < 0) {
		goto gone_bad;
	}
		
	printk (KERN_INFO LOGNAME "available DRAM %dk\n", dev.freemem / 1024);

	if (wavefront_write (0xf0) ||
	    wavefront_write (1) ||
	    (wavefront_read () < 0)) {
		dev.debug = 0;
		printk (KERN_WARNING LOGNAME "MPU emulation mode not set.\n");
		goto gone_bad;
	}

	voices[0] = 32;

	if (wavefront_cmd (WFC_SET_NVOICES, NULL, voices)) {
		printk (KERN_WARNING LOGNAME
			"cannot set number of voices to 32.\n");
		goto gone_bad;
	}


	return 0;

 gone_bad:
	/* reset that sucker so that it doesn't bother us. */

	outb (0x0, dev.control_port);
	dev.interrupts_on = 0;
	if (dev.irq >= 0) {
		free_irq (dev.irq, &dev);
	}
	return 1;
}

static int __init wavefront_init (int atboot)
{
	int samples_are_from_rom;

	if (dev.israw) {
		samples_are_from_rom = 1;
	} else {
		/* XXX is this always true ? */
		samples_are_from_rom = 0;
	}

	if (dev.israw || fx_raw) {
		if (wavefront_do_reset (atboot)) {
			return -1;
		}
	}

	wavefront_get_sample_status (samples_are_from_rom);
	wavefront_get_program_status ();
	wavefront_get_patch_status ();

	/* Start normal operation: unreset, master interrupt enabled, no mute
	*/

	outb (0x80|0x40|0x20, dev.control_port); 

	return (0);
}

static int __init install_wavefront (void)
{
	if (!request_region (dev.base+2, 6, "wavefront synth"))
		return -1;

	if (dev.has_fx) {
		if (!request_region (dev.base+8, 8, "wavefront fx")) {
			release_region (dev.base+2, 6);
			return -1;
		}
	}

	if ((dev.synth_dev = register_sound_synth (&wavefront_fops, -1)) < 0) {
		printk (KERN_ERR LOGNAME "cannot register raw synth\n");
		goto err_out;
	}

#if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
	if ((dev.oss_dev = sound_alloc_synthdev()) == -1) {
		printk (KERN_ERR LOGNAME "Too many sequencers\n");
		/* FIXME: leak: should unregister sound synth */
		goto err_out;
	} else {
		synth_devs[dev.oss_dev] = &wavefront_operations;
	}
#endif /* OSS_SUPPORT_SEQ */

	if (wavefront_init (1) < 0) {
		printk (KERN_WARNING LOGNAME "initialization failed.\n");

#if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
		sound_unload_synthdev (dev.oss_dev);
#endif /* OSS_SUPPORT_SEQ */ 

		goto err_out;
	}
    
	if (wavefront_config_midi ()) {
		printk (KERN_WARNING LOGNAME "could not initialize MIDI.\n");
	}

	return dev.oss_dev;

err_out:
	release_region (dev.base+2, 6);
	if (dev.has_fx)
		release_region (dev.base+8, 8);
	return -1;
}

static void __exit uninstall_wavefront (void)
{
	/* the first two i/o addresses are freed by the wf_mpu code */
	release_region (dev.base+2, 6);

	if (dev.has_fx) {
		release_region (dev.base+8, 8);
	}

	unregister_sound_synth (dev.synth_dev);

#if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
	sound_unload_synthdev (dev.oss_dev);
#endif /* OSS_SUPPORT_SEQ */ 
	uninstall_wf_mpu ();
}

/***********************************************************************/
/*   WaveFront FX control                                              */
/***********************************************************************/

#include "yss225.h"

/* Control bits for the Load Control Register
 */

#define FX_LSB_TRANSFER 0x01    /* transfer after DSP LSB byte written */
#define FX_MSB_TRANSFER 0x02    /* transfer after DSP MSB byte written */
#define FX_AUTO_INCR    0x04    /* auto-increment DSP address after transfer */

static int
wffx_idle (void) 
    
{
	int i;
	unsigned int x = 0x80;
    
	for (i = 0; i < 1000; i++) {
		x = inb (dev.fx_status);
		if ((x & 0x80) == 0) {
			break;
		}
	}
    
	if (x & 0x80) {
		printk (KERN_ERR LOGNAME "FX device never idle.\n");
		return 0;
	}
    
	return (1);
}

int __init detect_wffx (void)
{
	/* This is a crude check, but its the best one I have for now.
	   Certainly on the Maui and the Tropez, wffx_idle() will
	   report "never idle", which suggests that this test should
	   work OK.
	*/

	if (inb (dev.fx_status) & 0x80) {
		printk (KERN_INFO LOGNAME "Hmm, probably a Maui or Tropez.\n");
		return -1;
	}

	return 0;
}	

static void
wffx_mute (int onoff)
    
{
	if (!wffx_idle()) {
		return;
	}
    
	outb (onoff ? 0x02 : 0x00, dev.fx_op);
}

static int
wffx_memset (int page,
	     int addr, int cnt, unsigned short *data)
{
	if (page < 0 || page > 7) {
		printk (KERN_ERR LOGNAME "FX memset: "
			"page must be >= 0 and <= 7\n");
		return -(EINVAL);
	}

	if (addr < 0 || addr > 0x7f) {
		printk (KERN_ERR LOGNAME "FX memset: "
			"addr must be >= 0 and <= 7f\n");
		return -(EINVAL);
	}

	if (cnt == 1) {

		outb (FX_LSB_TRANSFER, dev.fx_lcr);
		outb (page, dev.fx_dsp_page);
		outb (addr, dev.fx_dsp_addr);
		outb ((data[0] >> 8), dev.fx_dsp_msb);
		outb ((data[0] & 0xff), dev.fx_dsp_lsb);

		printk (KERN_INFO LOGNAME "FX: addr %d:%x set to 0x%x\n",
			page, addr, data[0]);
	
	} else {
		int i;

		outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
		outb (page, dev.fx_dsp_page);
		outb (addr, dev.fx_dsp_addr);

		for (i = 0; i < cnt; i++) {
			outb ((data[i] >> 8), dev.fx_dsp_msb);
			outb ((data[i] & 0xff), dev.fx_dsp_lsb);
			if (!wffx_idle ()) {
				break;
			}
		}

		if (i != cnt) {
			printk (KERN_WARNING LOGNAME
				"FX memset "
				"(0x%x, 0x%x, %p, %d) incomplete\n",
				page, addr, data, cnt);
			return -(EIO);
		}
	}

	return 0;
}

static int
wffx_ioctl (wavefront_fx_info *r)

{
	unsigned short page_data[256];
	unsigned short *pd;

	switch (r->request) {
	case WFFX_MUTE:
		wffx_mute (r->data[0]);
		return 0;

	case WFFX_MEMSET:

		if (r->data[2] <= 0) {
			printk (KERN_ERR LOGNAME "cannot write "
				"<= 0 bytes to FX\n");
			return -(EINVAL);
		} else if (r->data[2] == 1) {
			pd = (unsigned short *) &r->data[3];
		} else {
			if (r->data[2] > sizeof (page_data)) {
				printk (KERN_ERR LOGNAME "cannot write "
					"> 255 bytes to FX\n");
				return -(EINVAL);
			}
			if (copy_from_user(page_data,
					   (unsigned char __user *)r->data[3],
					   r->data[2]))
				return -EFAULT;
			pd = page_data;
		}

		return wffx_memset (r->data[0], /* page */
				    r->data[1], /* addr */
				    r->data[2], /* cnt */
				    pd);

	default:
		printk (KERN_WARNING LOGNAME
			"FX: ioctl %d not yet supported\n",
			r->request);
		return -(EINVAL);
	}
}

/* YSS225 initialization.

   This code was developed using DOSEMU. The Turtle Beach SETUPSND
   utility was run with I/O tracing in DOSEMU enabled, and a reconstruction
   of the port I/O done, using the Yamaha faxback document as a guide
   to add more logic to the code. Its really pretty weird.

   There was an alternative approach of just dumping the whole I/O
   sequence as a series of port/value pairs and a simple loop
   that output it. However, I hope that eventually I'll get more
   control over what this code does, and so I tried to stick with
   a somewhat "algorithmic" approach.
*/

static int __init wffx_init (void)
{
	int i;
	int j;

	/* Set all bits for all channels on the MOD unit to zero */
	/* XXX But why do this twice ? */

	for (j = 0; j < 2; j++) {
		for (i = 0x10; i <= 0xff; i++) {
	    
			if (!wffx_idle ()) {
				return (-1);
			}
	    
			outb (i, dev.fx_mod_addr);
			outb (0x0, dev.fx_mod_data);
		}
	}

	if (!wffx_idle()) return (-1);
	outb (0x02, dev.fx_op);                        /* mute on */

	if (!wffx_idle()) return (-1);
	outb (0x07, dev.fx_dsp_page);
	outb (0x44, dev.fx_dsp_addr);
	outb (0x00, dev.fx_dsp_msb);
	outb (0x00, dev.fx_dsp_lsb);
	if (!wffx_idle()) return (-1);
	outb (0x07, dev.fx_dsp_page);
	outb (0x42, dev.fx_dsp_addr);
	outb (0x00, dev.fx_dsp_msb);
	outb (0x00, dev.fx_dsp_lsb);
	if (!wffx_idle()) return (-1);
	outb (0x07, dev.fx_dsp_page);
	outb (0x43, dev.fx_dsp_addr);
	outb (0x00, dev.fx_dsp_msb);
	outb (0x00, dev.fx_dsp_lsb);
	if (!wffx_idle()) return (-1);
	outb (0x07, dev.fx_dsp_page);
	outb (0x7c, dev.fx_dsp_addr);
	outb (0x00, dev.fx_dsp_msb);
	outb (0x00, dev.fx_dsp_lsb);
	if (!wffx_idle()) return (-1);
	outb (0x07, dev.fx_dsp_page);
	outb (0x7e, dev.fx_dsp_addr);
	outb (0x00, dev.fx_dsp_msb);
	outb (0x00, dev.fx_dsp_lsb);
	if (!wffx_idle()) return (-1);
	outb (0x07, dev.fx_dsp_page);
	outb (0x46, dev.fx_dsp_addr);
	outb (0x00, dev.fx_dsp_msb);
	outb (0x00, dev.fx_dsp_lsb);
	if (!wffx_idle()) return (-1);
	outb (0x07, dev.fx_dsp_page);
	outb (0x49, dev.fx_dsp_addr);
	outb (0x00, dev.fx_dsp_msb);
	outb (0x00, dev.fx_dsp_lsb);
	if (!wffx_idle()) return (-1);
	outb (0x07, dev.fx_dsp_page);
	outb (0x47, dev.fx_dsp_addr);
	outb (0x00, dev.fx_dsp_msb);
	outb (0x00, dev.fx_dsp_lsb);
	if (!wffx_idle()) return (-1);
	outb (0x07, dev.fx_dsp_page);
	outb (0x4a, dev.fx_dsp_addr);
	outb (0x00, dev.fx_dsp_msb);
	outb (0x00, dev.fx_dsp_lsb);

	/* either because of stupidity by TB's programmers, or because it
	   actually does something, rezero the MOD page.
	*/
	for (i = 0x10; i <= 0xff; i++) {
	
		if (!wffx_idle ()) {
			return (-1);
		}
	
		outb (i, dev.fx_mod_addr);
		outb (0x0, dev.fx_mod_data);
	}
	/* load page zero */

	outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
	outb (0x00, dev.fx_dsp_page);
	outb (0x00, dev.fx_dsp_addr);

	for (i = 0; i < sizeof (page_zero); i += 2) {
		outb (page_zero[i], dev.fx_dsp_msb);
		outb (page_zero[i+1], dev.fx_dsp_lsb);
		if (!wffx_idle()) return (-1);
	}

	/* Now load page one */

	outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
	outb (0x01, dev.fx_dsp_page);
	outb (0x00, dev.fx_dsp_addr);

	for (i = 0; i < sizeof (page_one); i += 2) {
		outb (page_one[i], dev.fx_dsp_msb);
		outb (page_one[i+1], dev.fx_dsp_lsb);
		if (!wffx_idle()) return (-1);
	}
    
	outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
	outb (0x02, dev.fx_dsp_page);
	outb (0x00, dev.fx_dsp_addr);

	for (i = 0; i < sizeof (page_two); i++) {
		outb (page_two[i], dev.fx_dsp_lsb);
		if (!wffx_idle()) return (-1);
	}
    
	outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
	outb (0x03, dev.fx_dsp_page);
	outb (0x00, dev.fx_dsp_addr);

	for (i = 0; i < sizeof (page_three); i++) {
		outb (page_three[i], dev.fx_dsp_lsb);
		if (!wffx_idle()) return (-1);
	}
    
	outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
	outb (0x04, dev.fx_dsp_page);
	outb (0x00, dev.fx_dsp_addr);

	for (i = 0; i < sizeof (page_four); i++) {
		outb (page_four[i], dev.fx_dsp_lsb);
		if (!wffx_idle()) return (-1);
	}

	/* Load memory area (page six) */
    
	outb (FX_LSB_TRANSFER, dev.fx_lcr); 
	outb (0x06, dev.fx_dsp_page); 

	for (i = 0; i < sizeof (page_six); i += 3) {
		outb (page_six[i], dev.fx_dsp_addr);
		outb (page_six[i+1], dev.fx_dsp_msb);
		outb (page_six[i+2], dev.fx_dsp_lsb);
		if (!wffx_idle()) return (-1);
	}
    
	outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
	outb (0x07, dev.fx_dsp_page);
	outb (0x00, dev.fx_dsp_addr);

	for (i = 0; i < sizeof (page_seven); i += 2) {
		outb (page_seven[i], dev.fx_dsp_msb);
		outb (page_seven[i+1], dev.fx_dsp_lsb);
		if (!wffx_idle()) return (-1);
	}

	/* Now setup the MOD area. We do this algorithmically in order to
	   save a little data space. It could be done in the same fashion
	   as the "pages".
	*/

	for (i = 0x00; i <= 0x0f; i++) {
		outb (0x01, dev.fx_mod_addr);
		outb (i, dev.fx_mod_data);
		if (!wffx_idle()) return (-1);
		outb (0x02, dev.fx_mod_addr);
		outb (0x00, dev.fx_mod_data);
		if (!wffx_idle()) return (-1);
	}

	for (i = 0xb0; i <= 0xbf; i++) {
		outb (i, dev.fx_mod_addr);
		outb (0x20, dev.fx_mod_data);
		if (!wffx_idle()) return (-1);
	}

	for (i = 0xf0; i <= 0xff; i++) {
		outb (i, dev.fx_mod_addr);
		outb (0x20, dev.fx_mod_data);
		if (!wffx_idle()) return (-1);
	}

	for (i = 0x10; i <= 0x1d; i++) {
		outb (i, dev.fx_mod_addr);
		outb (0xff, dev.fx_mod_data);
		if (!wffx_idle()) return (-1);
	}

	outb (0x1e, dev.fx_mod_addr);
	outb (0x40, dev.fx_mod_data);
	if (!wffx_idle()) return (-1);

	for (i = 0x1f; i <= 0x2d; i++) {
		outb (i, dev.fx_mod_addr);
		outb (0xff, dev.fx_mod_data);
		if (!wffx_idle()) return (-1);
	}

	outb (0x2e, dev.fx_mod_addr);
	outb (0x00, dev.fx_mod_data);
	if (!wffx_idle()) return (-1);

	for (i = 0x2f; i <= 0x3e; i++) {
		outb (i, dev.fx_mod_addr);
		outb (0x00, dev.fx_mod_data);
		if (!wffx_idle()) return (-1);
	}

	outb (0x3f, dev.fx_mod_addr);
	outb (0x20, dev.fx_mod_data);
	if (!wffx_idle()) return (-1);

	for (i = 0x40; i <= 0x4d; i++) {
		outb (i, dev.fx_mod_addr);
		outb (0x00, dev.fx_mod_data);
		if (!wffx_idle()) return (-1);
	}

	outb (0x4e, dev.fx_mod_addr);
	outb (0x0e, dev.fx_mod_data);
	if (!wffx_idle()) return (-1);
	outb (0x4f, dev.fx_mod_addr);
	outb (0x0e, dev.fx_mod_data);
	if (!wffx_idle()) return (-1);


	for (i = 0x50; i <= 0x6b; i++) {
		outb (i, dev.fx_mod_addr);
		outb (0x00, dev.fx_mod_data);
		if (!wffx_idle()) return (-1);
	}

	outb (0x6c, dev.fx_mod_addr);
	outb (0x40, dev.fx_mod_data);
	if (!wffx_idle()) return (-1);

	outb (0x6d, dev.fx_mod_addr);
	outb (0x00, dev.fx_mod_data);
	if (!wffx_idle()) return (-1);

	outb (0x6e, dev.fx_mod_addr);
	outb (0x40, dev.fx_mod_data);
	if (!wffx_idle()) return (-1);

	outb (0x6f, dev.fx_mod_addr);
	outb (0x40, dev.fx_mod_data);
	if (!wffx_idle()) return (-1);

	for (i = 0x70; i <= 0x7f; i++) {
		outb (i, dev.fx_mod_addr);
		outb (0xc0, dev.fx_mod_data);
		if (!wffx_idle()) return (-1);
	}
    
	for (i = 0x80; i <= 0xaf; i++) {
		outb (i, dev.fx_mod_addr);
		outb (0x00, dev.fx_mod_data);
		if (!wffx_idle()) return (-1);
	}

	for (i = 0xc0; i <= 0xdd; i++) {
		outb (i, dev.fx_mod_addr);
		outb (0x00, dev.fx_mod_data);
		if (!wffx_idle()) return (-1);
	}

	outb (0xde, dev.fx_mod_addr);
	outb (0x10, dev.fx_mod_data);
	if (!wffx_idle()) return (-1);
	outb (0xdf, dev.fx_mod_addr);
	outb (0x10, dev.fx_mod_data);
	if (!wffx_idle()) return (-1);

	for (i = 0xe0; i <= 0xef; i++) {
		outb (i, dev.fx_mod_addr);
		outb (0x00, dev.fx_mod_data);
		if (!wffx_idle()) return (-1);
	}

	for (i = 0x00; i <= 0x0f; i++) {
		outb (0x01, dev.fx_mod_addr);
		outb (i, dev.fx_mod_data);
		outb (0x02, dev.fx_mod_addr);
		outb (0x01, dev.fx_mod_data);
		if (!wffx_idle()) return (-1);
	}

	outb (0x02, dev.fx_op); /* mute on */

	/* Now set the coefficients and so forth for the programs above */

	for (i = 0; i < sizeof (coefficients); i += 4) {
		outb (coefficients[i], dev.fx_dsp_page);
		outb (coefficients[i+1], dev.fx_dsp_addr);
		outb (coefficients[i+2], dev.fx_dsp_msb);
		outb (coefficients[i+3], dev.fx_dsp_lsb);
		if (!wffx_idle()) return (-1);
	}

	/* Some settings (?) that are too small to bundle into loops */

	if (!wffx_idle()) return (-1);
	outb (0x1e, dev.fx_mod_addr);
	outb (0x14, dev.fx_mod_data);
	if (!wffx_idle()) return (-1);
	outb (0xde, dev.fx_mod_addr);
	outb (0x20, dev.fx_mod_data);
	if (!wffx_idle()) return (-1);
	outb (0xdf, dev.fx_mod_addr);
	outb (0x20, dev.fx_mod_data);
    
	/* some more coefficients */

	if (!wffx_idle()) return (-1);
	outb (0x06, dev.fx_dsp_page);
	outb (0x78, dev.fx_dsp_addr);
	outb (0x00, dev.fx_dsp_msb);
	outb (0x40, dev.fx_dsp_lsb);
	if (!wffx_idle()) return (-1);
	outb (0x07, dev.fx_dsp_page);
	outb (0x03, dev.fx_dsp_addr);
	outb (0x0f, dev.fx_dsp_msb);
	outb (0xff, dev.fx_dsp_lsb);
	if (!wffx_idle()) return (-1);
	outb (0x07, dev.fx_dsp_page);
	outb (0x0b, dev.fx_dsp_addr);
	outb (0x0f, dev.fx_dsp_msb);
	outb (0xff, dev.fx_dsp_lsb);
	if (!wffx_idle()) return (-1);
	outb (0x07, dev.fx_dsp_page);
	outb (0x02, dev.fx_dsp_addr);
	outb (0x00, dev.fx_dsp_msb);
	outb (0x00, dev.fx_dsp_lsb);
	if (!wffx_idle()) return (-1);
	outb (0x07, dev.fx_dsp_page);
	outb (0x0a, dev.fx_dsp_addr);
	outb (0x00, dev.fx_dsp_msb);
	outb (0x00, dev.fx_dsp_lsb);
	if (!wffx_idle()) return (-1);
	outb (0x07, dev.fx_dsp_page);
	outb (0x46, dev.fx_dsp_addr);
	outb (0x00, dev.fx_dsp_msb);
	outb (0x00, dev.fx_dsp_lsb);
	if (!wffx_idle()) return (-1);
	outb (0x07, dev.fx_dsp_page);
	outb (0x49, dev.fx_dsp_addr);
	outb (0x00, dev.fx_dsp_msb);
	outb (0x00, dev.fx_dsp_lsb);
    
	/* Now, for some strange reason, lets reload every page
	   and all the coefficients over again. I have *NO* idea
	   why this is done. I do know that no sound is produced
	   is this phase is omitted.
	*/

	outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
	outb (0x00, dev.fx_dsp_page);  
	outb (0x10, dev.fx_dsp_addr);

	for (i = 0; i < sizeof (page_zero_v2); i += 2) {
		outb (page_zero_v2[i], dev.fx_dsp_msb);
		outb (page_zero_v2[i+1], dev.fx_dsp_lsb);
		if (!wffx_idle()) return (-1);
	}
    
	outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
	outb (0x01, dev.fx_dsp_page);
	outb (0x10, dev.fx_dsp_addr);

	for (i = 0; i < sizeof (page_one_v2); i += 2) {
		outb (page_one_v2[i], dev.fx_dsp_msb);
		outb (page_one_v2[i+1], dev.fx_dsp_lsb);
		if (!wffx_idle()) return (-1);
	}
    
	if (!wffx_idle()) return (-1);
	if (!wffx_idle()) return (-1);
    
	outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
	outb (0x02, dev.fx_dsp_page);
	outb (0x10, dev.fx_dsp_addr);

	for (i = 0; i < sizeof (page_two_v2); i++) {
		outb (page_two_v2[i], dev.fx_dsp_lsb);
		if (!wffx_idle()) return (-1);
	}
	outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
	outb (0x03, dev.fx_dsp_page);
	outb (0x10, dev.fx_dsp_addr);

	for (i = 0; i < sizeof (page_three_v2); i++) {
		outb (page_three_v2[i], dev.fx_dsp_lsb);
		if (!wffx_idle()) return (-1);
	}
    
	outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
	outb (0x04, dev.fx_dsp_page);
	outb (0x10, dev.fx_dsp_addr);

	for (i = 0; i < sizeof (page_four_v2); i++) {
		outb (page_four_v2[i], dev.fx_dsp_lsb);
		if (!wffx_idle()) return (-1);
	}
    
	outb (FX_LSB_TRANSFER, dev.fx_lcr);
	outb (0x06, dev.fx_dsp_page);

	/* Page six v.2 is algorithmic */
    
	for (i = 0x10; i <= 0x3e; i += 2) {
		outb (i, dev.fx_dsp_addr);
		outb (0x00, dev.fx_dsp_msb);
		outb (0x00, dev.fx_dsp_lsb);
		if (!wffx_idle()) return (-1);
	}

	outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
	outb (0x07, dev.fx_dsp_page);
	outb (0x10, dev.fx_dsp_addr);

	for (i = 0; i < sizeof (page_seven_v2); i += 2) {
		outb (page_seven_v2[i], dev.fx_dsp_msb);
		outb (page_seven_v2[i+1], dev.fx_dsp_lsb);
		if (!wffx_idle()) return (-1);
	}

	for (i = 0x00; i < sizeof(mod_v2); i += 2) {
		outb (mod_v2[i], dev.fx_mod_addr);
		outb (mod_v2[i+1], dev.fx_mod_data);
		if (!wffx_idle()) return (-1);
	}

	for (i = 0; i < sizeof (coefficients2); i += 4) {
		outb (coefficients2[i], dev.fx_dsp_page);
		outb (coefficients2[i+1], dev.fx_dsp_addr);
		outb (coefficients2[i+2], dev.fx_dsp_msb);
		outb (coefficients2[i+3], dev.fx_dsp_lsb);
		if (!wffx_idle()) return (-1);
	}

	for (i = 0; i < sizeof (coefficients3); i += 2) {
		int x;

		outb (0x07, dev.fx_dsp_page);
		x = (i % 4) ? 0x4e : 0x4c;
		outb (x, dev.fx_dsp_addr);
		outb (coefficients3[i], dev.fx_dsp_msb);
		outb (coefficients3[i+1], dev.fx_dsp_lsb);
	}

	outb (0x00, dev.fx_op); /* mute off */
	if (!wffx_idle()) return (-1);

	return (0);
}

static int io = -1;
static int irq = -1;

MODULE_AUTHOR      ("Paul Barton-Davis <pbd@op.net>");
MODULE_DESCRIPTION ("Turtle Beach WaveFront Linux Driver");
MODULE_LICENSE("GPL");
module_param       (io, int, 0);
module_param       (irq, int, 0);

static int __init init_wavfront (void)
{
	printk ("Turtle Beach WaveFront Driver\n"
		"Copyright (C) by Hannu Solvainen, "
		"Paul Barton-Davis 1993-1998.\n");

	/* XXX t'would be lovely to ask the CS4232 for these values, eh ? */

	if (io == -1 || irq == -1) {
		printk (KERN_INFO LOGNAME "irq and io options must be set.\n");
		return -EINVAL;
	}

	if (wavefront_interrupt_bits (irq) < 0) {
		printk (KERN_INFO LOGNAME
			"IRQ must be 9, 5, 12 or 15 (not %d)\n", irq);
		return -ENODEV;
	}

	if (detect_wavefront (irq, io) < 0) {
		return -ENODEV;
	} 

	if (install_wavefront () < 0) {
		return -EIO;
	}

	return 0;
}

static void __exit cleanup_wavfront (void)
{
	uninstall_wavefront ();
}

module_init(init_wavfront);
module_exit(cleanup_wavfront);