aboutsummaryrefslogblamecommitdiffstats
path: root/sound/oss/cs46xx.c
blob: 53881bc91bba9c0aef2c02ffde9e6325e9e78824 (plain) (tree)
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
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
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
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319



























































































                                                                                 
                        



















































































































































                                                                                                    
                                

























































                                                                                                    
                         












































































                                                                                          
                                        














                                                                                      












































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                              
                                
                                   
                                  















































































































































































































































































































































































































                                                                                                      
                                           

                                                     
                                                             

                                      
                                                     


                                                              
                                                   
















                                                                                               
                                             






























                                                                                
                                           
                                                                               
                                             


























































































































































































                                                                                          
                                































                                                                                         
                                                  




                                                            
                                                

































                                                                          
                                  



























                                                                                                 
                                






















































                                                                                        
                                                  




                                                            
                                                






























                                                                                    
                                  



































































































































                                                                                          
                                

























                                                                                       
                                  
























































































































































































































































































































































































































































































































































































































































































































































































                                                                                                
                                                







































                                                                                       
                                               

                                          
                                               














                                                                                           
                                                 









                                                                                  
                                                







































                                                                                       
                                               

                                          
                                               














                                                                                           
                                                 






























                                                                                                
                                                       




                                                                                         

                                                             























                                                                                                            
                                                       




                                                                                         

                                                             








































































































































































































































                                                                                                            
                                                                   




















































































































































































































































































































































































































































































































































                                                                                                       
                                                                      
























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                                                                                       






























































































































































































                                                                                                     
                                           
















                                                                            




































































































































































                                                                                    
                                                      














                                                                                






                                                                           





                                                                          
                                 











                                                                         
/*
 *	Crystal SoundFusion CS46xx driver
 *
 *	Copyright 1998-2001 Cirrus Logic Corporation <pcaudio@crystal.cirrus.com>
 *						<twoller@crystal.cirrus.com>
 *	Copyright 1999-2000 Jaroslav Kysela <perex@suse.cz>
 *	Copyright 2000 Alan Cox <alan@redhat.com>
 *
 *	The core of this code is taken from the ALSA project driver by 
 *	Jaroslav. Please send Jaroslav the credit for the driver and 
 *	report bugs in this port to <alan@redhat.com>
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *	Current maintainers:
 *		Cirrus Logic Corporation, Thomas Woller (tw)
 *			<twoller@crystal.cirrus.com>
 *		Nils Faerber (nf)
 *			<nils@kernelconcepts.de>
 *		Thanks to David Pollard for testing.
 *
 *	Changes:
 *	20000909-nf	Changed cs_read, cs_write and drain_dac
 *	20001025-tw	Separate Playback/Capture structs and buffers.
 *			Added Scatter/Gather support for Playback.
 *			Added Capture.
 *	20001027-nf	Port to kernel 2.4.0-test9, some clean-ups
 *			Start of powermanagement support (CS46XX_PM).
 *	20001128-tw	Add module parm for default buffer order.
 *			added DMA_GFP flag to kmalloc dma buffer allocs.
 *			backfill silence to eliminate stuttering on
 *			underruns.
 *	20001201-tw	add resyncing of swptr on underruns.
 *	20001205-tw-nf	fixed GETOSPACE ioctl() after open()
 *	20010113-tw	patch from Hans Grobler general cleanup.
 *	20010117-tw	2.4.0 pci cleanup, wrapper code for 2.2.16-2.4.0
 *	20010118-tw	basic PM support for 2.2.16+ and 2.4.0/2.4.2.
 *	20010228-dh	patch from David Huggins - cs_update_ptr recursion.
 *	20010409-tw	add hercules game theatre XP amp code.
 *	20010420-tw	cleanup powerdown/up code.
 *	20010521-tw	eliminate pops, and fixes for powerdown.
 *	20010525-tw	added fixes for thinkpads with powerdown logic.
 *	20010723-sh     patch from Horms (Simon Horman) -
 *	                SOUND_PCM_READ_BITS returns bits as set in driver
 *	                rather than a logical or of the possible values.
 *	                Various ioctls handle the case where the device
 *	                is open for reading or writing but not both better.
 *
 *	Status:
 *	Playback/Capture supported from 8k-48k.
 *	16Bit Signed LE & 8Bit Unsigned, with Mono or Stereo supported.
 *
 *	APM/PM - 2.2.x APM is enabled and functioning fine. APM can also
 *	be enabled for 2.4.x by modifying the CS46XX_ACPI_SUPPORT macro
 *	definition.
 *
 *      Hercules Game Theatre XP - the EGPIO2 pin controls the external Amp,
 *	so, use the drain/polarity to enable.  
 *	hercules_egpio_disable set to 1, will force a 0 to EGPIODR.
 *
 *	VTB Santa Cruz - the GPIO7/GPIO8 on the Secondary Codec control
 *	the external amplifier for the "back" speakers, since we do not
 *	support the secondary codec then this external amp is also not
 *	turned on.
 */
 
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/string.h>
#include <linux/ioport.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/sound.h>
#include <linux/slab.h>
#include <linux/soundcard.h>
#include <linux/pci.h>
#include <linux/bitops.h>
#include <linux/init.h>
#include <linux/poll.h>
#include <linux/ac97_codec.h>
#include <linux/mutex.h>

#include <asm/io.h>
#include <asm/dma.h>
#include <asm/uaccess.h>

#include "cs46xxpm-24.h"
#include "cs46xx_wrapper-24.h"
#include "cs461x.h"

/* MIDI buffer sizes */
#define CS_MIDIINBUF  500
#define CS_MIDIOUTBUF 500

#define ADC_RUNNING	1
#define DAC_RUNNING	2

#define CS_FMT_16BIT	1		/* These are fixed in fact */
#define CS_FMT_STEREO	2
#define CS_FMT_MASK	3

#define CS_TYPE_ADC	1
#define CS_TYPE_DAC	2

#define CS_TRUE 	1
#define CS_FALSE 	0

#define CS_INC_USE_COUNT(m) (atomic_inc(m))
#define CS_DEC_USE_COUNT(m) (atomic_dec(m))
#define CS_DEC_AND_TEST(m) (atomic_dec_and_test(m))
#define CS_IN_USE(m) (atomic_read(m) != 0)

#define CS_DBGBREAKPOINT {__asm__("INT $3");}
/*
 *	CS461x definitions
 */
 
#define CS461X_BA0_SIZE		0x2000
#define CS461X_BA1_DATA0_SIZE	0x3000
#define CS461X_BA1_DATA1_SIZE	0x3800
#define CS461X_BA1_PRG_SIZE	0x7000
#define CS461X_BA1_REG_SIZE	0x0100

#define GOF_PER_SEC	200

#define CSDEBUG_INTERFACE 1
#define CSDEBUG 1
/*
 * Turn on/off debugging compilation by using 1/0 respectively for CSDEBUG
 *
 *
 * CSDEBUG is usual mode is set to 1, then use the
 * cs_debuglevel and cs_debugmask to turn on or off debugging.
 * Debug level of 1 has been defined to be kernel errors and info
 * that should be printed on any released driver.
 */
#if CSDEBUG
#define CS_DBGOUT(mask,level,x) if((cs_debuglevel >= (level)) && ((mask) & cs_debugmask)) {x;} 
#else
#define CS_DBGOUT(mask,level,x) 
#endif
/*
 * cs_debugmask areas
 */
#define CS_INIT	 	0x00000001		/* initialization and probe functions */
#define CS_ERROR 	0x00000002		/* tmp debugging bit placeholder */
#define CS_INTERRUPT	0x00000004		/* interrupt handler (separate from all other) */
#define CS_FUNCTION 	0x00000008		/* enter/leave functions */
#define CS_WAVE_WRITE 	0x00000010		/* write information for wave */
#define CS_WAVE_READ 	0x00000020		/* read information for wave */
#define CS_MIDI_WRITE 	0x00000040		/* write information for midi */
#define CS_MIDI_READ 	0x00000080		/* read information for midi */
#define CS_MPU401_WRITE 0x00000100		/* write information for mpu401 */
#define CS_MPU401_READ 	0x00000200		/* read information for mpu401 */
#define CS_OPEN		0x00000400		/* all open functions in the driver */
#define CS_RELEASE	0x00000800		/* all release functions in the driver */
#define CS_PARMS	0x00001000		/* functional and operational parameters */
#define CS_IOCTL	0x00002000		/* ioctl (non-mixer) */
#define CS_PM		0x00004000		/* PM */
#define CS_TMP		0x10000000		/* tmp debug mask bit */

#define CS_IOCTL_CMD_SUSPEND	0x1	// suspend
#define CS_IOCTL_CMD_RESUME	0x2	// resume

#if CSDEBUG
static unsigned long cs_debuglevel=1;			/* levels range from 1-9 */
module_param(cs_debuglevel, ulong, 0644);
static unsigned long cs_debugmask=CS_INIT | CS_ERROR;	/* use CS_DBGOUT with various mask values */
module_param(cs_debugmask, ulong, 0644);
#endif
static unsigned long hercules_egpio_disable;  /* if non-zero set all EGPIO to 0 */
module_param(hercules_egpio_disable, ulong, 0);
static unsigned long initdelay=700;  /* PM delay in millisecs */
module_param(initdelay, ulong, 0);
static unsigned long powerdown=-1;  /* turn on/off powerdown processing in driver */
module_param(powerdown, ulong, 0);
#define DMABUF_DEFAULTORDER 3
static unsigned long defaultorder=DMABUF_DEFAULTORDER;
module_param(defaultorder, ulong, 0);

static int external_amp;
module_param(external_amp, bool, 0);
static int thinkpad;
module_param(thinkpad, bool, 0);

/*
* set the powerdown module parm to 0 to disable all 
* powerdown. also set thinkpad to 1 to disable powerdown, 
* but also to enable the clkrun functionality.
*/
static unsigned cs_powerdown=1;
static unsigned cs_laptop_wait=1;

/* An instance of the 4610 channel */
struct cs_channel 
{
	int used;
	int num;
	void *state;
};

#define CS46XX_MAJOR_VERSION "1"
#define CS46XX_MINOR_VERSION "28"

#ifdef __ia64__
#define CS46XX_ARCH	     	"64"	//architecture key
#else
#define CS46XX_ARCH	     	"32"	//architecture key
#endif

static struct list_head cs46xx_devs = { &cs46xx_devs, &cs46xx_devs };

/* magic numbers to protect our data structures */
#define CS_CARD_MAGIC		0x43525553 /* "CRUS" */
#define CS_STATE_MAGIC		0x4c4f4749 /* "LOGI" */
#define NR_HW_CH		3

/* maxinum number of AC97 codecs connected, AC97 2.0 defined 4 */
#define NR_AC97		2

static const unsigned sample_size[] = { 1, 2, 2, 4 };
static const unsigned sample_shift[] = { 0, 1, 1, 2 };

/* "software" or virtual channel, an instance of opened /dev/dsp */
struct cs_state {
	unsigned int magic;
	struct cs_card *card;	/* Card info */

	/* single open lock mechanism, only used for recording */
	struct mutex open_mutex;
	wait_queue_head_t open_wait;

	/* file mode */
	mode_t open_mode;

	/* virtual channel number */
	int virt;
	
	struct dmabuf {
		/* wave sample stuff */
		unsigned int rate;
		unsigned char fmt, enable;

		/* hardware channel */
		struct cs_channel *channel;
		int pringbuf;		/* Software ring slot */
		void *pbuf;		/* 4K hardware DMA buffer */

		/* OSS buffer management stuff */
		void *rawbuf;
		dma_addr_t dma_handle;
		unsigned buforder;
		unsigned numfrag;
		unsigned fragshift;
		unsigned divisor;
		unsigned type;
		void *tmpbuff;			/* tmp buffer for sample conversions */
		dma_addr_t dmaaddr;
		dma_addr_t dmaaddr_tmpbuff;
		unsigned buforder_tmpbuff;	/* Log base 2 of size in bytes.. */

		/* our buffer acts like a circular ring */
		unsigned hwptr;		/* where dma last started, updated by update_ptr */
		unsigned swptr;		/* where driver last clear/filled, updated by read/write */
		int count;		/* bytes to be comsumed or been generated by dma machine */
		unsigned total_bytes;	/* total bytes dmaed by hardware */
		unsigned blocks;	/* total blocks */

		unsigned error;		/* number of over/underruns */
		unsigned underrun;	/* underrun pending before next write has occurred */
		wait_queue_head_t wait;	/* put process on wait queue when no more space in buffer */

		/* redundant, but makes calculations easier */
		unsigned fragsize;
		unsigned dmasize;
		unsigned fragsamples;

		/* OSS stuff */
		unsigned mapped:1;
		unsigned ready:1;
		unsigned endcleared:1;
		unsigned SGok:1;
		unsigned update_flag;
		unsigned ossfragshift;
		int ossmaxfrags;
		unsigned subdivision;
	} dmabuf;
	/* Guard against mmap/write/read races */
	struct mutex sem;
};

struct cs_card {
	struct cs_channel channel[2];
	unsigned int magic;

	/* We keep cs461x cards in a linked list */
	struct cs_card *next;

	/* The cs461x has a certain amount of cross channel interaction
	   so we use a single per card lock */
	spinlock_t lock;
	
	/* Keep AC97 sane */
	spinlock_t ac97_lock;

	/* mixer use count */
	atomic_t mixer_use_cnt;

	/* PCI device stuff */
	struct pci_dev * pci_dev;
	struct list_head list;

	unsigned int pctl, cctl;	/* Hardware DMA flag sets */

	/* soundcore stuff */
	int dev_audio;
	int dev_midi;

	/* structures for abstraction of hardware facilities, codecs, banks and channels*/
	struct ac97_codec *ac97_codec[NR_AC97];
	struct cs_state *states[2];

	u16 ac97_features;
	
	int amplifier;			/* Amplifier control */
	void (*amplifier_ctrl)(struct cs_card *, int);
	void (*amp_init)(struct cs_card *);
	
	int active;			/* Active clocking */
	void (*active_ctrl)(struct cs_card *, int);
	
	/* hardware resources */
	unsigned long ba0_addr;
	unsigned long ba1_addr;
	u32 irq;
	
	/* mappings */
	void __iomem *ba0;
	union
	{
		struct
		{
			u8 __iomem *data0;
			u8 __iomem *data1;
			u8 __iomem *pmem;
			u8 __iomem *reg;
		} name;
		u8 __iomem *idx[4];
	} ba1;
	
	/* Function support */
	struct cs_channel *(*alloc_pcm_channel)(struct cs_card *);
	struct cs_channel *(*alloc_rec_pcm_channel)(struct cs_card *);
	void (*free_pcm_channel)(struct cs_card *, int chan);

	/* /dev/midi stuff */
	struct {
		unsigned ird, iwr, icnt;
		unsigned ord, owr, ocnt;
		wait_queue_head_t open_wait;
		wait_queue_head_t iwait;
		wait_queue_head_t owait;
		spinlock_t lock;
		unsigned char ibuf[CS_MIDIINBUF];
		unsigned char obuf[CS_MIDIOUTBUF];
		mode_t open_mode;
		struct mutex open_mutex;
	} midi;
	struct cs46xx_pm pm;
};

static int cs_open_mixdev(struct inode *inode, struct file *file);
static int cs_release_mixdev(struct inode *inode, struct file *file);
static int cs_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd,
				unsigned long arg);
static int cs_hardware_init(struct cs_card *card);
static int cs46xx_powerup(struct cs_card *card, unsigned int type);
static int cs461x_powerdown(struct cs_card *card, unsigned int type, int suspendflag);
static void cs461x_clear_serial_FIFOs(struct cs_card *card, int type);
static int cs46xx_suspend_tbl(struct pci_dev *pcidev, pm_message_t state);
static int cs46xx_resume_tbl(struct pci_dev *pcidev);

#if CSDEBUG

/* DEBUG ROUTINES */

#define SOUND_MIXER_CS_GETDBGLEVEL 	_SIOWR('M',120, int)
#define SOUND_MIXER_CS_SETDBGLEVEL 	_SIOWR('M',121, int)
#define SOUND_MIXER_CS_GETDBGMASK 	_SIOWR('M',122, int)
#define SOUND_MIXER_CS_SETDBGMASK 	_SIOWR('M',123, int)
#define SOUND_MIXER_CS_APM	 	_SIOWR('M',124, int)

static void printioctl(unsigned int x)
{
    unsigned int i;
    unsigned char vidx;
	/* these values are incorrect for the ac97 driver, fix.
         * Index of mixtable1[] member is Device ID 
         * and must be <= SOUND_MIXER_NRDEVICES.
         * Value of array member is index into s->mix.vol[]
         */
        static const unsigned char mixtable1[SOUND_MIXER_NRDEVICES] = {
                [SOUND_MIXER_PCM]     = 1,   /* voice */
                [SOUND_MIXER_LINE1]   = 2,   /* AUX */
                [SOUND_MIXER_CD]      = 3,   /* CD */
                [SOUND_MIXER_LINE]    = 4,   /* Line */
                [SOUND_MIXER_SYNTH]   = 5,   /* FM */
                [SOUND_MIXER_MIC]     = 6,   /* Mic */
                [SOUND_MIXER_SPEAKER] = 7,   /* Speaker */
                [SOUND_MIXER_RECLEV]  = 8,   /* Recording level */
                [SOUND_MIXER_VOLUME]  = 9    /* Master Volume */
        };
        
    switch(x) 
    {
	case SOUND_MIXER_CS_GETDBGMASK:
		CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_GETDBGMASK: ") );
		break;
	case SOUND_MIXER_CS_GETDBGLEVEL:
		CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_GETDBGLEVEL: ") );
		break;
	case SOUND_MIXER_CS_SETDBGMASK:
		CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_SETDBGMASK: ") );
		break;
	case SOUND_MIXER_CS_SETDBGLEVEL:
		CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CS_SETDBGLEVEL: ") );
		break;
        case OSS_GETVERSION:
		CS_DBGOUT(CS_IOCTL, 4, printk("OSS_GETVERSION: ") );
		break;
        case SNDCTL_DSP_SYNC:
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SYNC: ") );
		break;
        case SNDCTL_DSP_SETDUPLEX:
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETDUPLEX: ") );
		break;
        case SNDCTL_DSP_GETCAPS:
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETCAPS: ") );
		break;
        case SNDCTL_DSP_RESET:
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_RESET: ") );
		break;
        case SNDCTL_DSP_SPEED:
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SPEED: ") );
		break;
        case SNDCTL_DSP_STEREO:
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_STEREO: ") );
		break;
        case SNDCTL_DSP_CHANNELS:
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_CHANNELS: ") );
		break;
        case SNDCTL_DSP_GETFMTS: 
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETFMTS: ") );
		break;
        case SNDCTL_DSP_SETFMT: 
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETFMT: ") );
		break;
        case SNDCTL_DSP_POST:
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_POST: ") );
		break;
        case SNDCTL_DSP_GETTRIGGER:
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETTRIGGER: ") );
		break;
        case SNDCTL_DSP_SETTRIGGER:
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETTRIGGER: ") );
		break;
        case SNDCTL_DSP_GETOSPACE:
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETOSPACE: ") );
		break;
        case SNDCTL_DSP_GETISPACE:
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETISPACE: ") );
		break;
        case SNDCTL_DSP_NONBLOCK:
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_NONBLOCK: ") );
		break;
        case SNDCTL_DSP_GETODELAY:
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETODELAY: ") );
		break;
        case SNDCTL_DSP_GETIPTR:
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETIPTR: ") );
		break;
        case SNDCTL_DSP_GETOPTR:
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETOPTR: ") );
		break;
        case SNDCTL_DSP_GETBLKSIZE:
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETBLKSIZE: ") );
		break;
        case SNDCTL_DSP_SETFRAGMENT:
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETFRAGMENT: ") );
		break;
        case SNDCTL_DSP_SUBDIVIDE:
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SUBDIVIDE: ") );
		break;
        case SOUND_PCM_READ_RATE:
		CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_RATE: ") );
		break;
        case SOUND_PCM_READ_CHANNELS:
		CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_CHANNELS: ") );
		break;
        case SOUND_PCM_READ_BITS:
		CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_BITS: ") );
		break;
        case SOUND_PCM_WRITE_FILTER:
		CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_WRITE_FILTER: ") );
		break;
        case SNDCTL_DSP_SETSYNCRO:
		CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETSYNCRO: ") );
		break;
        case SOUND_PCM_READ_FILTER:
		CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_FILTER: ") );
		break;

        case SOUND_MIXER_PRIVATE1:
		CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE1: ") );
		break;
        case SOUND_MIXER_PRIVATE2:
		CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE2: ") );
		break;
        case SOUND_MIXER_PRIVATE3:
		CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE3: ") );
		break;
        case SOUND_MIXER_PRIVATE4:
		CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE4: ") );
		break;
        case SOUND_MIXER_PRIVATE5:
		CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE5: ") );
		break;
        case SOUND_MIXER_INFO:
		CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_INFO: ") );
		break;
        case SOUND_OLD_MIXER_INFO:
		CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_OLD_MIXER_INFO: ") );
		break;

	default:
		switch (_IOC_NR(x)) 
		{
			case SOUND_MIXER_VOLUME:
				CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_VOLUME: ") );
				break;
			case SOUND_MIXER_SPEAKER:
				CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_SPEAKER: ") );
				break;
			case SOUND_MIXER_RECLEV:
				CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_RECLEV: ") );
				break;
			case SOUND_MIXER_MIC:
				CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_MIC: ") );
				break;
			case SOUND_MIXER_SYNTH:
				CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_SYNTH: ") );
				break;
			case SOUND_MIXER_RECSRC: 
				CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_RECSRC: ") );
				break;
			case SOUND_MIXER_DEVMASK:
				CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_DEVMASK: ") );
				break;
			case SOUND_MIXER_RECMASK:
				CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_RECMASK: ") );
				break;
			case SOUND_MIXER_STEREODEVS: 
				CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_STEREODEVS: ") );
				break;
			case SOUND_MIXER_CAPS:
				CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CAPS:") );
				break;
			default:
				i = _IOC_NR(x);
				if (i >= SOUND_MIXER_NRDEVICES || !(vidx = mixtable1[i]))
				{
					CS_DBGOUT(CS_IOCTL, 4, printk("UNKNOWN IOCTL: 0x%.8x NR=%d ",x,i) );
				}
				else
				{
					CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_IOCTL AC9x: 0x%.8x NR=%d ",
							x,i) );
				}
				break;
		}
    }
    CS_DBGOUT(CS_IOCTL, 4, printk("command = 0x%x IOC_NR=%d\n",x, _IOC_NR(x)) );
}
#endif

/*
 *  common I/O routines
 */

static void cs461x_poke(struct cs_card *codec, unsigned long reg, unsigned int val)
{
	writel(val, codec->ba1.idx[(reg >> 16) & 3]+(reg&0xffff));
}

static unsigned int cs461x_peek(struct cs_card *codec, unsigned long reg)
{
	return readl(codec->ba1.idx[(reg >> 16) & 3]+(reg&0xffff));
}

static void cs461x_pokeBA0(struct cs_card *codec, unsigned long reg, unsigned int val)
{
	writel(val, codec->ba0+reg);
}

static unsigned int cs461x_peekBA0(struct cs_card *codec, unsigned long reg)
{
	return readl(codec->ba0+reg);
}


static u16 cs_ac97_get(struct ac97_codec *dev, u8 reg);
static void cs_ac97_set(struct ac97_codec *dev, u8 reg, u16 data);

static struct cs_channel *cs_alloc_pcm_channel(struct cs_card *card)
{
	if(card->channel[1].used==1)
		return NULL;
	card->channel[1].used=1;
	card->channel[1].num=1;
	return &card->channel[1];
}

static struct cs_channel *cs_alloc_rec_pcm_channel(struct cs_card *card)
{
	if(card->channel[0].used==1)
		return NULL;
	card->channel[0].used=1;
	card->channel[0].num=0;
	return &card->channel[0];
}

static void cs_free_pcm_channel(struct cs_card *card, int channel)
{
	card->channel[channel].state = NULL;
	card->channel[channel].used=0;
}

/*
 * setup a divisor value to help with conversion from
 * 16bit Stereo, down to 8bit stereo/mono or 16bit mono.
 * assign a divisor of 1 if using 16bit Stereo as that is
 * the only format that the static image will capture.
 */
static void cs_set_divisor(struct dmabuf *dmabuf)
{
	if(dmabuf->type == CS_TYPE_DAC)
		dmabuf->divisor = 1;
	else if( !(dmabuf->fmt & CS_FMT_STEREO) && 
	    (dmabuf->fmt & CS_FMT_16BIT))
		dmabuf->divisor = 2;
	else if( (dmabuf->fmt & CS_FMT_STEREO) && 
	    !(dmabuf->fmt & CS_FMT_16BIT))
		dmabuf->divisor = 2;
	else if( !(dmabuf->fmt & CS_FMT_STEREO) && 
	    !(dmabuf->fmt & CS_FMT_16BIT))
		dmabuf->divisor = 4;
	else
		dmabuf->divisor = 1;

	CS_DBGOUT(CS_PARMS | CS_FUNCTION, 8, printk(
		"cs46xx: cs_set_divisor()- %s %d\n",
			(dmabuf->type == CS_TYPE_ADC) ? "ADC" : "DAC", 
			dmabuf->divisor) );
}

/*
* mute some of the more prevalent registers to avoid popping.
*/
static void cs_mute(struct cs_card *card, int state) 
{
	struct ac97_codec *dev=card->ac97_codec[0];

	CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO "cs46xx: cs_mute()+ %s\n",
		(state == CS_TRUE) ? "Muting" : "UnMuting") );

	if(state == CS_TRUE)
	{
	/*
	* fix pops when powering up on thinkpads
	*/
		card->pm.u32AC97_master_volume = (u32)cs_ac97_get( dev, 
				(u8)BA0_AC97_MASTER_VOLUME); 
		card->pm.u32AC97_headphone_volume = (u32)cs_ac97_get(dev, 
				(u8)BA0_AC97_HEADPHONE_VOLUME); 
		card->pm.u32AC97_master_volume_mono = (u32)cs_ac97_get(dev, 
				(u8)BA0_AC97_MASTER_VOLUME_MONO); 
		card->pm.u32AC97_pcm_out_volume = (u32)cs_ac97_get(dev, 
				(u8)BA0_AC97_PCM_OUT_VOLUME);
			
		cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME, 0x8000);
		cs_ac97_set(dev, (u8)BA0_AC97_HEADPHONE_VOLUME, 0x8000);
		cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME_MONO, 0x8000);
		cs_ac97_set(dev, (u8)BA0_AC97_PCM_OUT_VOLUME, 0x8000);
	}
	else
	{
		cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME, card->pm.u32AC97_master_volume);
		cs_ac97_set(dev, (u8)BA0_AC97_HEADPHONE_VOLUME, card->pm.u32AC97_headphone_volume);
		cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME_MONO, card->pm.u32AC97_master_volume_mono);
		cs_ac97_set(dev, (u8)BA0_AC97_PCM_OUT_VOLUME, card->pm.u32AC97_pcm_out_volume);
	}
	CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO "cs46xx: cs_mute()-\n"));
}

/* set playback sample rate */
static unsigned int cs_set_dac_rate(struct cs_state * state, unsigned int rate)
{	
	struct dmabuf *dmabuf = &state->dmabuf;
	unsigned int tmp1, tmp2;
	unsigned int phiIncr;
	unsigned int correctionPerGOF, correctionPerSec;
	unsigned long flags;

	CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_dac_rate()+ %d\n",rate) );

	/*
	 *  Compute the values used to drive the actual sample rate conversion.
	 *  The following formulas are being computed, using inline assembly
	 *  since we need to use 64 bit arithmetic to compute the values:
	 *
	 *  phiIncr = floor((Fs,in * 2^26) / Fs,out)
	 *  correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
         *                                   GOF_PER_SEC)
         *  ulCorrectionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -M
         *                       GOF_PER_SEC * correctionPerGOF
	 *
	 *  i.e.
	 *
	 *  phiIncr:other = dividend:remainder((Fs,in * 2^26) / Fs,out)
	 *  correctionPerGOF:correctionPerSec =
	 *      dividend:remainder(ulOther / GOF_PER_SEC)
	 */
	tmp1 = rate << 16;
	phiIncr = tmp1 / 48000;
	tmp1 -= phiIncr * 48000;
	tmp1 <<= 10;
	phiIncr <<= 10;
	tmp2 = tmp1 / 48000;
	phiIncr += tmp2;
	tmp1 -= tmp2 * 48000;
	correctionPerGOF = tmp1 / GOF_PER_SEC;
	tmp1 -= correctionPerGOF * GOF_PER_SEC;
	correctionPerSec = tmp1;

	/*
	 *  Fill in the SampleRateConverter control block.
	 */
	 
	spin_lock_irqsave(&state->card->lock, flags);
	cs461x_poke(state->card, BA1_PSRC,
	  ((correctionPerSec << 16) & 0xFFFF0000) | (correctionPerGOF & 0xFFFF));
	cs461x_poke(state->card, BA1_PPI, phiIncr);
	spin_unlock_irqrestore(&state->card->lock, flags);
	dmabuf->rate = rate;
	
	CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_dac_rate()- %d\n",rate) );
	return rate;
}

/* set recording sample rate */
static unsigned int cs_set_adc_rate(struct cs_state * state, unsigned int rate)
{
	struct dmabuf *dmabuf = &state->dmabuf;
	struct cs_card *card = state->card;
	unsigned int phiIncr, coeffIncr, tmp1, tmp2;
	unsigned int correctionPerGOF, correctionPerSec, initialDelay;
	unsigned int frameGroupLength, cnt;
	unsigned long flags;
	CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_adc_rate()+ %d\n",rate) );

	/*
	 *  We can only decimate by up to a factor of 1/9th the hardware rate.
	 *  Correct the value if an attempt is made to stray outside that limit.
	 */
	if ((rate * 9) < 48000)
		rate = 48000 / 9;

	/*
	 *  We can not capture at at rate greater than the Input Rate (48000).
	 *  Return an error if an attempt is made to stray outside that limit.
	 */
	if (rate > 48000)
		rate = 48000;

	/*
	 *  Compute the values used to drive the actual sample rate conversion.
	 *  The following formulas are being computed, using inline assembly
	 *  since we need to use 64 bit arithmetic to compute the values:
	 *
	 *     coeffIncr = -floor((Fs,out * 2^23) / Fs,in)
	 *     phiIncr = floor((Fs,in * 2^26) / Fs,out)
	 *     correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
	 *                                GOF_PER_SEC)
	 *     correctionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -
	 *                          GOF_PER_SEC * correctionPerGOF
	 *     initialDelay = ceil((24 * Fs,in) / Fs,out)
	 *
	 * i.e.
	 *
	 *     coeffIncr = neg(dividend((Fs,out * 2^23) / Fs,in))
	 *     phiIncr:ulOther = dividend:remainder((Fs,in * 2^26) / Fs,out)
	 *     correctionPerGOF:correctionPerSec =
	 * 	    dividend:remainder(ulOther / GOF_PER_SEC)
	 *     initialDelay = dividend(((24 * Fs,in) + Fs,out - 1) / Fs,out)
	 */

	tmp1 = rate << 16;
	coeffIncr = tmp1 / 48000;
	tmp1 -= coeffIncr * 48000;
	tmp1 <<= 7;
	coeffIncr <<= 7;
	coeffIncr += tmp1 / 48000;
	coeffIncr ^= 0xFFFFFFFF;
	coeffIncr++;
	tmp1 = 48000 << 16;
	phiIncr = tmp1 / rate;
	tmp1 -= phiIncr * rate;
	tmp1 <<= 10;
	phiIncr <<= 10;
	tmp2 = tmp1 / rate;
	phiIncr += tmp2;
	tmp1 -= tmp2 * rate;
	correctionPerGOF = tmp1 / GOF_PER_SEC;
	tmp1 -= correctionPerGOF * GOF_PER_SEC;
	correctionPerSec = tmp1;
	initialDelay = ((48000 * 24) + rate - 1) / rate;

	/*
	 *  Fill in the VariDecimate control block.
	 */
	spin_lock_irqsave(&card->lock, flags);
	cs461x_poke(card, BA1_CSRC,
		((correctionPerSec << 16) & 0xFFFF0000) | (correctionPerGOF & 0xFFFF));
	cs461x_poke(card, BA1_CCI, coeffIncr);
	cs461x_poke(card, BA1_CD,
		(((BA1_VARIDEC_BUF_1 + (initialDelay << 2)) << 16) & 0xFFFF0000) | 0x80);
	cs461x_poke(card, BA1_CPI, phiIncr);
	spin_unlock_irqrestore(&card->lock, flags);

	/*
	 *  Figure out the frame group length for the write back task.  Basically,
	 *  this is just the factors of 24000 (2^6*3*5^3) that are not present in
	 *  the output sample rate.
	 */
	frameGroupLength = 1;
	for (cnt = 2; cnt <= 64; cnt *= 2) {
		if (((rate / cnt) * cnt) != rate)
			frameGroupLength *= 2;
	}
	if (((rate / 3) * 3) != rate) {
		frameGroupLength *= 3;
	}
	for (cnt = 5; cnt <= 125; cnt *= 5) {
		if (((rate / cnt) * cnt) != rate) 
			frameGroupLength *= 5;
        }

	/*
	 * Fill in the WriteBack control block.
	 */
	spin_lock_irqsave(&card->lock, flags);
	cs461x_poke(card, BA1_CFG1, frameGroupLength);
	cs461x_poke(card, BA1_CFG2, (0x00800000 | frameGroupLength));
	cs461x_poke(card, BA1_CCST, 0x0000FFFF);
	cs461x_poke(card, BA1_CSPB, ((65536 * rate) / 24000));
	cs461x_poke(card, (BA1_CSPB + 4), 0x0000FFFF);
	spin_unlock_irqrestore(&card->lock, flags);
	dmabuf->rate = rate;
	CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_set_adc_rate()- %d\n",rate) );
	return rate;
}

/* prepare channel attributes for playback */ 
static void cs_play_setup(struct cs_state *state)
{
	struct dmabuf *dmabuf = &state->dmabuf;
	struct cs_card *card = state->card;
        unsigned int tmp, Count, playFormat;

	CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_play_setup()+\n") );
        cs461x_poke(card, BA1_PVOL, 0x80008000);
        if(!dmabuf->SGok)
               cs461x_poke(card, BA1_PBA, virt_to_bus(dmabuf->pbuf));
    
        Count = 4;                                                          
        playFormat=cs461x_peek(card, BA1_PFIE);                             
        if ((dmabuf->fmt & CS_FMT_STEREO)) {                                
                playFormat &= ~DMA_RQ_C2_AC_MONO_TO_STEREO;                 
                Count *= 2;                                                 
        }                                                                   
        else                                                                
                playFormat |= DMA_RQ_C2_AC_MONO_TO_STEREO;                  
                                                                            
        if ((dmabuf->fmt & CS_FMT_16BIT)) {                                 
                playFormat &= ~(DMA_RQ_C2_AC_8_TO_16_BIT                    
                           | DMA_RQ_C2_AC_SIGNED_CONVERT);                  
                Count *= 2;                                                 
        }                                                                   
        else                                                                
                playFormat |= (DMA_RQ_C2_AC_8_TO_16_BIT                     
                           | DMA_RQ_C2_AC_SIGNED_CONVERT);                  
                                                                            
        cs461x_poke(card, BA1_PFIE, playFormat);                            
                                                                            
        tmp = cs461x_peek(card, BA1_PDTC);                                  
        tmp &= 0xfffffe00;                                                  
        cs461x_poke(card, BA1_PDTC, tmp | --Count);                         

	CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_play_setup()-\n") );

}

static struct InitStruct
{
    u32 off;
    u32 val;
} InitArray[] = { {0x00000040, 0x3fc0000f},
                  {0x0000004c, 0x04800000},

                  {0x000000b3, 0x00000780},
                  {0x000000b7, 0x00000000},
                  {0x000000bc, 0x07800000},

                  {0x000000cd, 0x00800000},
                };

/*
 * "SetCaptureSPValues()" -- Initialize record task values before each
 * 	capture startup.  
 */
static void SetCaptureSPValues(struct cs_card *card)
{
	unsigned i, offset;
	CS_DBGOUT(CS_FUNCTION, 8, printk("cs46xx: SetCaptureSPValues()+\n") );
	for(i=0; i<sizeof(InitArray)/sizeof(struct InitStruct); i++)
	{
		offset = InitArray[i].off*4; /* 8bit to 32bit offset value */
		cs461x_poke(card, offset, InitArray[i].val );
	}
	CS_DBGOUT(CS_FUNCTION, 8, printk("cs46xx: SetCaptureSPValues()-\n") );
}

/* prepare channel attributes for recording */
static void cs_rec_setup(struct cs_state *state)
{
	struct cs_card *card = state->card;
	struct dmabuf *dmabuf = &state->dmabuf;
	CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_rec_setup()+\n") );

	SetCaptureSPValues(card);

	/*
	 * set the attenuation to 0dB 
	 */
	cs461x_poke(card, BA1_CVOL, 0x80008000);

	/*
	 * set the physical address of the capture buffer into the SP
	 */
	cs461x_poke(card, BA1_CBA, virt_to_bus(dmabuf->rawbuf));

	CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_rec_setup()-\n") );
}


/* get current playback/recording dma buffer pointer (byte offset from LBA),
   called with spinlock held! */
   
static inline unsigned cs_get_dma_addr(struct cs_state *state)
{
	struct dmabuf *dmabuf = &state->dmabuf;
	u32 offset;
	
	if ( (!(dmabuf->enable & DAC_RUNNING)) &&
	     (!(dmabuf->enable & ADC_RUNNING) ) )
	{
		CS_DBGOUT(CS_ERROR, 2, printk(
			"cs46xx: ERROR cs_get_dma_addr(): not enabled \n") );
		return 0;
	}
		
	/*
	 * granularity is byte boundary, good part.
	 */
	if(dmabuf->enable & DAC_RUNNING)
	{
		offset = cs461x_peek(state->card, BA1_PBA);                                  
	}
	else /* ADC_RUNNING must be set */
	{
		offset = cs461x_peek(state->card, BA1_CBA);                                  
	}
	CS_DBGOUT(CS_PARMS | CS_FUNCTION, 9, 
		printk("cs46xx: cs_get_dma_addr() %d\n",offset) );
	offset = (u32)bus_to_virt((unsigned long)offset) - (u32)dmabuf->rawbuf;
	CS_DBGOUT(CS_PARMS | CS_FUNCTION, 8, 
		printk("cs46xx: cs_get_dma_addr()- %d\n",offset) );
	return offset;
}

static void resync_dma_ptrs(struct cs_state *state)
{
	struct dmabuf *dmabuf;
	
	CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: resync_dma_ptrs()+ \n") );
	if(state)
	{
		dmabuf = &state->dmabuf;
		dmabuf->hwptr=dmabuf->swptr = 0;
		dmabuf->pringbuf = 0;
	}
	CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: resync_dma_ptrs()- \n") );
}
	
/* Stop recording (lock held) */
static inline void __stop_adc(struct cs_state *state)
{
	struct dmabuf *dmabuf = &state->dmabuf;
	struct cs_card *card = state->card;
	unsigned int tmp;
	
	dmabuf->enable &= ~ADC_RUNNING;
	
	tmp = cs461x_peek(card, BA1_CCTL);
	tmp &= 0xFFFF0000;
	cs461x_poke(card, BA1_CCTL, tmp );
}

static void stop_adc(struct cs_state *state)
{
	unsigned long flags;

	CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: stop_adc()+ \n") );
	spin_lock_irqsave(&state->card->lock, flags);
	__stop_adc(state);
	spin_unlock_irqrestore(&state->card->lock, flags);
	CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: stop_adc()- \n") );
}

static void start_adc(struct cs_state *state)
{
	struct dmabuf *dmabuf = &state->dmabuf;
	struct cs_card *card = state->card;
	unsigned long flags;
	unsigned int tmp;

	spin_lock_irqsave(&card->lock, flags);
	if (!(dmabuf->enable & ADC_RUNNING) && 
	     ((dmabuf->mapped || dmabuf->count < (signed)dmabuf->dmasize) 
	       && dmabuf->ready) && 
	       ((card->pm.flags & CS46XX_PM_IDLE) || 
	        (card->pm.flags & CS46XX_PM_RESUMED)) )
	{
		dmabuf->enable |= ADC_RUNNING;
		cs_set_divisor(dmabuf);
		tmp = cs461x_peek(card, BA1_CCTL);
		tmp &= 0xFFFF0000;
		tmp |= card->cctl;
		CS_DBGOUT(CS_FUNCTION, 2, printk(
			"cs46xx: start_adc() poke 0x%x \n",tmp) );
		cs461x_poke(card, BA1_CCTL, tmp);
	}
	spin_unlock_irqrestore(&card->lock, flags);
}

/* stop playback (lock held) */
static inline void __stop_dac(struct cs_state *state)
{
	struct dmabuf *dmabuf = &state->dmabuf;
	struct cs_card *card = state->card;
	unsigned int tmp;

	dmabuf->enable &= ~DAC_RUNNING;
	
	tmp=cs461x_peek(card, BA1_PCTL);
	tmp&=0xFFFF;
	cs461x_poke(card, BA1_PCTL, tmp);
}

static void stop_dac(struct cs_state *state)
{
	unsigned long flags;

	CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: stop_dac()+ \n") );
	spin_lock_irqsave(&state->card->lock, flags);
	__stop_dac(state);
	spin_unlock_irqrestore(&state->card->lock, flags);
	CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: stop_dac()- \n") );
}	

static void start_dac(struct cs_state *state)
{
	struct dmabuf *dmabuf = &state->dmabuf;
	struct cs_card *card = state->card;
	unsigned long flags;
	int tmp;

	CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: start_dac()+ \n") );
	spin_lock_irqsave(&card->lock, flags);
	if (!(dmabuf->enable & DAC_RUNNING) && 
	    ((dmabuf->mapped || dmabuf->count > 0) && dmabuf->ready) &&
	       ((card->pm.flags & CS46XX_PM_IDLE) || 
	        (card->pm.flags & CS46XX_PM_RESUMED)) )
	{
		dmabuf->enable |= DAC_RUNNING;
		tmp = cs461x_peek(card, BA1_PCTL);
		tmp &= 0xFFFF;
		tmp |= card->pctl;
		CS_DBGOUT(CS_PARMS, 6, printk(
		    "cs46xx: start_dac() poke card=%p tmp=0x%.08x addr=%p \n",
		    card, (unsigned)tmp, 
		    card->ba1.idx[(BA1_PCTL >> 16) & 3]+(BA1_PCTL&0xffff) ) );
		cs461x_poke(card, BA1_PCTL, tmp);
	}
	spin_unlock_irqrestore(&card->lock, flags);
	CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: start_dac()- \n") );
}

#define DMABUF_MINORDER 1

/*
 * allocate DMA buffer, playback and recording buffers are separate.
 */
static int alloc_dmabuf(struct cs_state *state)
{

	struct cs_card *card=state->card;
	struct dmabuf *dmabuf = &state->dmabuf;
	void *rawbuf = NULL;
	void *tmpbuff = NULL;
	int order;
	struct page *map, *mapend;
	unsigned long df;
	
	dmabuf->ready  = dmabuf->mapped = 0;
	dmabuf->SGok = 0;
/*
* check for order within limits, but do not overwrite value.
*/
	if((defaultorder > 1) && (defaultorder < 12))
		df = defaultorder;
	else
		df = 2;	

	for (order = df; order >= DMABUF_MINORDER; order--)
		if ( (rawbuf = (void *) pci_alloc_consistent(
			card->pci_dev, PAGE_SIZE << order, &dmabuf->dmaaddr)))
			    break;
	if (!rawbuf) {
		CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
			"cs46xx: alloc_dmabuf(): unable to allocate rawbuf\n"));
		return -ENOMEM;
	}
	dmabuf->buforder = order;
	dmabuf->rawbuf = rawbuf;
	// Now mark the pages as reserved; otherwise the 
	// remap_pfn_range() in cs46xx_mmap doesn't work.
	// 1. get index to last page in mem_map array for rawbuf.
	mapend = virt_to_page(dmabuf->rawbuf + 
		(PAGE_SIZE << dmabuf->buforder) - 1);

	// 2. mark each physical page in range as 'reserved'.
	for (map = virt_to_page(dmabuf->rawbuf); map <= mapend; map++)
		cs4x_mem_map_reserve(map);

	CS_DBGOUT(CS_PARMS, 9, printk("cs46xx: alloc_dmabuf(): allocated %ld (order = %d) bytes at %p\n",
	       PAGE_SIZE << order, order, rawbuf) );

/*
*  only allocate the conversion buffer for the ADC
*/
	if(dmabuf->type == CS_TYPE_DAC)
	{
		dmabuf->tmpbuff = NULL;
		dmabuf->buforder_tmpbuff = 0;
		return 0;
	}
/*
 * now the temp buffer for 16/8 conversions
 */

	tmpbuff = (void *) pci_alloc_consistent(
		card->pci_dev, PAGE_SIZE << order, &dmabuf->dmaaddr_tmpbuff);

	if (!tmpbuff)
		return -ENOMEM;
	CS_DBGOUT(CS_PARMS, 9, printk("cs46xx: allocated %ld (order = %d) bytes at %p\n",
	       PAGE_SIZE << order, order, tmpbuff) );

	dmabuf->tmpbuff = tmpbuff;
	dmabuf->buforder_tmpbuff = order;
	
	// Now mark the pages as reserved; otherwise the 
	// remap_pfn_range() in cs46xx_mmap doesn't work.
	// 1. get index to last page in mem_map array for rawbuf.
	mapend = virt_to_page(dmabuf->tmpbuff + 
		(PAGE_SIZE << dmabuf->buforder_tmpbuff) - 1);

	// 2. mark each physical page in range as 'reserved'.
	for (map = virt_to_page(dmabuf->tmpbuff); map <= mapend; map++)
		cs4x_mem_map_reserve(map);
	return 0;
}

/* free DMA buffer */
static void dealloc_dmabuf(struct cs_state *state)
{
	struct dmabuf *dmabuf = &state->dmabuf;
	struct page *map, *mapend;

	if (dmabuf->rawbuf) {
		// Undo prog_dmabuf()'s marking the pages as reserved 
		mapend = virt_to_page(dmabuf->rawbuf + 
				(PAGE_SIZE << dmabuf->buforder) - 1);
		for (map = virt_to_page(dmabuf->rawbuf); map <= mapend; map++)
			cs4x_mem_map_unreserve(map);
		free_dmabuf(state->card, dmabuf);
	}

	if (dmabuf->tmpbuff) {
		// Undo prog_dmabuf()'s marking the pages as reserved 
		mapend = virt_to_page(dmabuf->tmpbuff +
				(PAGE_SIZE << dmabuf->buforder_tmpbuff) - 1);
		for (map = virt_to_page(dmabuf->tmpbuff); map <= mapend; map++)
			cs4x_mem_map_unreserve(map);
		free_dmabuf2(state->card, dmabuf);
	}

	dmabuf->rawbuf = NULL;
	dmabuf->tmpbuff = NULL;
	dmabuf->mapped = dmabuf->ready = 0;
	dmabuf->SGok = 0;
}

static int __prog_dmabuf(struct cs_state *state)
{
        struct dmabuf *dmabuf = &state->dmabuf;
        unsigned long flags;
        unsigned long allocated_pages, allocated_bytes;                     
        unsigned long tmp1, tmp2, fmt=0;                                           
        unsigned long *ptmp = (unsigned long *) dmabuf->pbuf;               
        unsigned long SGarray[9], nSGpages=0;                               
        int ret;

	CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf()+ \n"));
/*
 * check for CAPTURE and use only non-sg for initial release
 */
	if(dmabuf->type == CS_TYPE_ADC)
	{
		CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf() ADC\n"));
		/* 
		 * add in non-sg support for capture.
		 */
		spin_lock_irqsave(&state->card->lock, flags);
	/* add code to reset the rawbuf memory. TRW */
		resync_dma_ptrs(state);
		dmabuf->total_bytes = dmabuf->blocks = 0;
		dmabuf->count = dmabuf->error = dmabuf->underrun = 0;

		dmabuf->SGok = 0;                                                   

		spin_unlock_irqrestore(&state->card->lock, flags);

		/* allocate DMA buffer if not allocated yet */
		if (!dmabuf->rawbuf || !dmabuf->tmpbuff)
			if ((ret = alloc_dmabuf(state)))
				return ret; 
	/*
	 * static image only supports 16Bit signed, stereo - hard code fmt
	 */
		fmt = CS_FMT_16BIT | CS_FMT_STEREO;

		dmabuf->numfrag = 2;                                        
		dmabuf->fragsize = 2048;                                    
		dmabuf->fragsamples = 2048 >> sample_shift[fmt];    
		dmabuf->dmasize = 4096;                                     
		dmabuf->fragshift = 11;                                     

		memset(dmabuf->rawbuf, (fmt & CS_FMT_16BIT) ? 0 : 0x80,
		       dmabuf->dmasize);
        	memset(dmabuf->tmpbuff, (fmt & CS_FMT_16BIT) ? 0 : 0x80, 
			PAGE_SIZE<<dmabuf->buforder_tmpbuff);      

		/*
		 *      Now set up the ring
		 */

		spin_lock_irqsave(&state->card->lock, flags);
		cs_rec_setup(state);
		spin_unlock_irqrestore(&state->card->lock, flags);

		/* set the ready flag for the dma buffer */
		dmabuf->ready = 1;

		CS_DBGOUT(CS_PARMS, 4, printk(
			"cs46xx: prog_dmabuf(): CAPTURE rate=%d fmt=0x%x numfrag=%d "
			"fragsize=%d dmasize=%d\n",
			    dmabuf->rate, dmabuf->fmt, dmabuf->numfrag,
			    dmabuf->fragsize, dmabuf->dmasize) );

		CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf()- 0 \n"));
		return 0;
	}
	else if (dmabuf->type == CS_TYPE_DAC)
	{
	/*
	 * Must be DAC
	 */
		CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf() DAC\n"));
		spin_lock_irqsave(&state->card->lock, flags);
		resync_dma_ptrs(state);
		dmabuf->total_bytes = dmabuf->blocks = 0;
		dmabuf->count = dmabuf->error = dmabuf->underrun = 0;

		dmabuf->SGok = 0;                                                   

		spin_unlock_irqrestore(&state->card->lock, flags);

		/* allocate DMA buffer if not allocated yet */
		if (!dmabuf->rawbuf)
			if ((ret = alloc_dmabuf(state)))
				return ret;

		allocated_pages = 1 << dmabuf->buforder;                            
		allocated_bytes = allocated_pages*PAGE_SIZE;                        
										    
		if(allocated_pages < 2)                                             
		{
			CS_DBGOUT(CS_FUNCTION, 4, printk(
			    "cs46xx: prog_dmabuf() Error: allocated_pages too small (%d)\n",
				(unsigned)allocated_pages));
			return -ENOMEM;
		}
										    
		/* Use all the pages allocated, fragsize 4k. */
		/* Use 'pbuf' for S/G page map table. */
		dmabuf->SGok = 1;           /* Use S/G. */

		nSGpages = allocated_bytes/4096;    /* S/G pages always 4k. */
										    
		     /* Set up S/G variables. */
		*ptmp = virt_to_bus(dmabuf->rawbuf);                                
		*(ptmp+1) = 0x00000008;                                             
		for(tmp1= 1; tmp1 < nSGpages; tmp1++) {                             
			*(ptmp+2*tmp1) = virt_to_bus( (dmabuf->rawbuf)+4096*tmp1);  
			if( tmp1 == nSGpages-1)                                     
				tmp2 = 0xbfff0000;
			else                                                        
				tmp2 = 0x80000000+8*(tmp1+1);                       
			*(ptmp+2*tmp1+1) = tmp2;                                    
		}                                                                   
		SGarray[0] = 0x82c0200d;                                            
		SGarray[1] = 0xffff0000;                                            
		SGarray[2] = *ptmp;                                                 
		SGarray[3] = 0x00010600;                                            
		SGarray[4] = *(ptmp+2);                                             
		SGarray[5] = 0x80000010;                                            
		SGarray[6] = *ptmp;                                                 
		SGarray[7] = *(ptmp+2);                                             
		SGarray[8] = (virt_to_bus(dmabuf->pbuf) & 0xffff000) | 0x10;        

		if (dmabuf->SGok) {                                                 
			dmabuf->numfrag = nSGpages;                                 
			dmabuf->fragsize = 4096;                                    
			dmabuf->fragsamples = 4096 >> sample_shift[dmabuf->fmt];    
			dmabuf->fragshift = 12;                                     
			dmabuf->dmasize = dmabuf->numfrag*4096;                     
		}                                                                   
		else {                                                              
			SGarray[0] = 0xf2c0000f;                                    
			SGarray[1] = 0x00000200;                                    
			SGarray[2] = 0;                                             
			SGarray[3] = 0x00010600;                                    
			SGarray[4]=SGarray[5]=SGarray[6]=SGarray[7]=SGarray[8] = 0; 
			dmabuf->numfrag = 2;                                        
			dmabuf->fragsize = 2048;                                    
			dmabuf->fragsamples = 2048 >> sample_shift[dmabuf->fmt];    
			dmabuf->dmasize = 4096;                                     
			dmabuf->fragshift = 11;                                     
		}
		for(tmp1 = 0; tmp1 < sizeof(SGarray)/4; tmp1++)                     
			cs461x_poke( state->card, BA1_PDTC+tmp1*4, SGarray[tmp1]);  

		memset(dmabuf->rawbuf, (dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80,
		       dmabuf->dmasize);

		/*
		 *      Now set up the ring
		 */

		spin_lock_irqsave(&state->card->lock, flags);
		cs_play_setup(state);
		spin_unlock_irqrestore(&state->card->lock, flags);

		/* set the ready flag for the dma buffer */
		dmabuf->ready = 1;

		CS_DBGOUT(CS_PARMS, 4, printk(
			"cs46xx: prog_dmabuf(): PLAYBACK rate=%d fmt=0x%x numfrag=%d "
			"fragsize=%d dmasize=%d\n",
			    dmabuf->rate, dmabuf->fmt, dmabuf->numfrag,
			    dmabuf->fragsize, dmabuf->dmasize) );

		CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf()- \n"));
		return 0;
	}
	else
	{
		CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: prog_dmabuf()- Invalid Type %d\n",
			dmabuf->type));
	}
	return 1;
}

static int prog_dmabuf(struct cs_state *state)
{
	int ret;
	
	mutex_lock(&state->sem);
	ret = __prog_dmabuf(state);
	mutex_unlock(&state->sem);
	
	return ret;
}

static void cs_clear_tail(struct cs_state *state)
{
}

static int drain_dac(struct cs_state *state, int nonblock)
{
	DECLARE_WAITQUEUE(wait, current);
	struct dmabuf *dmabuf = &state->dmabuf;
	struct cs_card *card=state->card;
	unsigned long flags;
	unsigned long tmo;
	int count;

	CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: drain_dac()+ \n"));
	if (dmabuf->mapped || !dmabuf->ready)
	{
		CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: drain_dac()- 0, not ready\n"));
		return 0;
	}

	add_wait_queue(&dmabuf->wait, &wait);
	for (;;) {
		/* It seems that we have to set the current state to TASK_INTERRUPTIBLE
		   every time to make the process really go to sleep */
		current->state = TASK_INTERRUPTIBLE;

		spin_lock_irqsave(&state->card->lock, flags);
		count = dmabuf->count;
		spin_unlock_irqrestore(&state->card->lock, flags);

		if (count <= 0)
			break;

		if (signal_pending(current))
			break;

		if (nonblock) {
			remove_wait_queue(&dmabuf->wait, &wait);
			current->state = TASK_RUNNING;
			return -EBUSY;
		}

		tmo = (dmabuf->dmasize * HZ) / dmabuf->rate;
		tmo >>= sample_shift[dmabuf->fmt];
		tmo += (2048*HZ)/dmabuf->rate;
		
		if (!schedule_timeout(tmo ? tmo : 1) && tmo){
			printk(KERN_ERR "cs46xx: drain_dac, dma timeout? %d\n", count);
			break;
		}
	}
	remove_wait_queue(&dmabuf->wait, &wait);
	current->state = TASK_RUNNING;
	if (signal_pending(current))
	{
		CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: drain_dac()- -ERESTARTSYS\n"));
		/*
		* set to silence and let that clear the fifos.
		*/
		cs461x_clear_serial_FIFOs(card, CS_TYPE_DAC);
		return -ERESTARTSYS;
	}

	CS_DBGOUT(CS_FUNCTION, 4, printk("cs46xx: drain_dac()- 0\n"));
	return 0;
}


/* update buffer manangement pointers, especially, dmabuf->count and dmabuf->hwptr */
static void cs_update_ptr(struct cs_card *card, int wake)
{
	struct cs_state *state;
	struct dmabuf *dmabuf;
	unsigned hwptr;
	int diff;

	/* error handling and process wake up for ADC */
	state = card->states[0];
	if(state)
	{
		dmabuf = &state->dmabuf;
		if (dmabuf->enable & ADC_RUNNING) {
			/* update hardware pointer */
			hwptr = cs_get_dma_addr(state);

			diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
			CS_DBGOUT(CS_PARMS, 9, printk(
				"cs46xx: cs_update_ptr()+ ADC hwptr=%d diff=%d\n", 
				hwptr,diff) );
			dmabuf->hwptr = hwptr;
			dmabuf->total_bytes += diff;
			dmabuf->count += diff;
			if (dmabuf->count > dmabuf->dmasize)
				dmabuf->count = dmabuf->dmasize;

			if(dmabuf->mapped)
			{
				if (wake && dmabuf->count >= (signed)dmabuf->fragsize)
					wake_up(&dmabuf->wait);
			} else 
			{
				if (wake && dmabuf->count > 0)
					wake_up(&dmabuf->wait);
			}
		}
	}

/*
 * Now the DAC
 */
	state = card->states[1];
	if(state)
	{
		dmabuf = &state->dmabuf;
		/* error handling and process wake up for DAC */
		if (dmabuf->enable & DAC_RUNNING) {
			/* update hardware pointer */
			hwptr = cs_get_dma_addr(state);

			diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
			CS_DBGOUT(CS_PARMS, 9, printk(
				"cs46xx: cs_update_ptr()+ DAC hwptr=%d diff=%d\n", 
				hwptr,diff) );
			dmabuf->hwptr = hwptr;
			dmabuf->total_bytes += diff;
			if (dmabuf->mapped) {
				dmabuf->count += diff;
				if (wake && dmabuf->count >= (signed)dmabuf->fragsize)
					wake_up(&dmabuf->wait);
				/*
				 * other drivers use fragsize, but don't see any sense
				 * in that, since dmasize is the buffer asked for
				 * via mmap.
				 */
				if( dmabuf->count > dmabuf->dmasize)
					dmabuf->count &= dmabuf->dmasize-1;
			} else {
				dmabuf->count -= diff;
				/*
				 * backfill with silence and clear out the last 
				 * "diff" number of bytes.
				 */
				if(hwptr >= diff)
				{
					memset(dmabuf->rawbuf + hwptr - diff, 
						(dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80, diff);
				}
				else
				{
					memset(dmabuf->rawbuf, 
						(dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80,
						(unsigned)hwptr);
					memset((char *)dmabuf->rawbuf + 
							dmabuf->dmasize + hwptr - diff,
						(dmabuf->fmt & CS_FMT_16BIT) ? 0 : 0x80, 
						diff - hwptr); 
				}

				if (dmabuf->count < 0 || dmabuf->count > dmabuf->dmasize) {
					CS_DBGOUT(CS_ERROR, 2, printk(KERN_INFO
					  "cs46xx: ERROR DAC count<0 or count > dmasize (%d)\n",
					  	dmabuf->count));
					/* 
					* buffer underrun or buffer overrun, reset the
					* count of bytes written back to 0.
					*/
					if(dmabuf->count < 0)
						dmabuf->underrun=1;
					dmabuf->count = 0;
					dmabuf->error++;
				}
				if (wake && dmabuf->count < (signed)dmabuf->dmasize/2)
					wake_up(&dmabuf->wait);
			}
		}
	}
}


/* hold spinlock for the following! */
static void cs_handle_midi(struct cs_card *card)
{
        unsigned char ch;
        int wake;
        unsigned temp1;

        wake = 0;
        while (!(cs461x_peekBA0(card,  BA0_MIDSR) & MIDSR_RBE)) {
                ch = cs461x_peekBA0(card, BA0_MIDRP);
                if (card->midi.icnt < CS_MIDIINBUF) {
                        card->midi.ibuf[card->midi.iwr] = ch;
                        card->midi.iwr = (card->midi.iwr + 1) % CS_MIDIINBUF;
                        card->midi.icnt++;
                }
                wake = 1;
        }
        if (wake)
                wake_up(&card->midi.iwait);
        wake = 0;
        while (!(cs461x_peekBA0(card,  BA0_MIDSR) & MIDSR_TBF) && card->midi.ocnt > 0) {
                temp1 = ( card->midi.obuf[card->midi.ord] ) & 0x000000ff;
                cs461x_pokeBA0(card, BA0_MIDWP,temp1);
                card->midi.ord = (card->midi.ord + 1) % CS_MIDIOUTBUF;
                card->midi.ocnt--;
                if (card->midi.ocnt < CS_MIDIOUTBUF-16)
                        wake = 1;
        }
        if (wake)
                wake_up(&card->midi.owait);
}

static irqreturn_t cs_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
	struct cs_card *card = (struct cs_card *)dev_id;
	/* Single channel card */
	struct cs_state *recstate = card->channel[0].state;
	struct cs_state *playstate = card->channel[1].state;
	u32 status;

	CS_DBGOUT(CS_INTERRUPT, 9, printk("cs46xx: cs_interrupt()+ \n"));

	spin_lock(&card->lock);

	status = cs461x_peekBA0(card, BA0_HISR);
	
	if ((status & 0x7fffffff) == 0)
	{
		cs461x_pokeBA0(card, BA0_HICR, HICR_CHGM|HICR_IEV);
		spin_unlock(&card->lock);
		return IRQ_HANDLED;	/* Might be IRQ_NONE.. */
	}
	
	/*
	 * check for playback or capture interrupt only
	 */
	if( ((status & HISR_VC0) && playstate && playstate->dmabuf.ready) || 
	    (((status & HISR_VC1) && recstate && recstate->dmabuf.ready)) )
	{
		CS_DBGOUT(CS_INTERRUPT, 8, printk(
			"cs46xx: cs_interrupt() interrupt bit(s) set (0x%x)\n",status));
		cs_update_ptr(card, CS_TRUE);
	}

        if( status & HISR_MIDI )
                cs_handle_midi(card);
	
 	/* clear 'em */
	cs461x_pokeBA0(card, BA0_HICR, HICR_CHGM|HICR_IEV);
	spin_unlock(&card->lock);
	CS_DBGOUT(CS_INTERRUPT, 9, printk("cs46xx: cs_interrupt()- \n"));
	return IRQ_HANDLED;
}


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

static ssize_t cs_midi_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
{
        struct cs_card *card = (struct cs_card *)file->private_data;
        ssize_t ret;
        unsigned long flags;
        unsigned ptr;
        int cnt;

        if (!access_ok(VERIFY_WRITE, buffer, count))
                return -EFAULT;
        ret = 0;
        while (count > 0) {
                spin_lock_irqsave(&card->lock, flags);
                ptr = card->midi.ird;
                cnt = CS_MIDIINBUF - ptr;
                if (card->midi.icnt < cnt)
                        cnt = card->midi.icnt;
                spin_unlock_irqrestore(&card->lock, flags);
                if (cnt > count)
                        cnt = count;
                if (cnt <= 0) {
                        if (file->f_flags & O_NONBLOCK)
                                return ret ? ret : -EAGAIN;
                        interruptible_sleep_on(&card->midi.iwait);
                        if (signal_pending(current))
                                return ret ? ret : -ERESTARTSYS;
                        continue;
                }
                if (copy_to_user(buffer, card->midi.ibuf + ptr, cnt))
                        return ret ? ret : -EFAULT;
                ptr = (ptr + cnt) % CS_MIDIINBUF;
                spin_lock_irqsave(&card->lock, flags);
                card->midi.ird = ptr;
                card->midi.icnt -= cnt;
                spin_unlock_irqrestore(&card->lock, flags);
                count -= cnt;
                buffer += cnt;
                ret += cnt;
        }
        return ret;
}


static ssize_t cs_midi_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
{
        struct cs_card *card = (struct cs_card *)file->private_data;
        ssize_t ret;
        unsigned long flags;
        unsigned ptr;
        int cnt;

        if (!access_ok(VERIFY_READ, buffer, count))
                return -EFAULT;
        ret = 0;
        while (count > 0) {
                spin_lock_irqsave(&card->lock, flags);
                ptr = card->midi.owr;
                cnt = CS_MIDIOUTBUF - ptr;
                if (card->midi.ocnt + cnt > CS_MIDIOUTBUF)
                        cnt = CS_MIDIOUTBUF - card->midi.ocnt;
                if (cnt <= 0)
                        cs_handle_midi(card);
                spin_unlock_irqrestore(&card->lock, flags);
                if (cnt > count)
                        cnt = count;
                if (cnt <= 0) {
                        if (file->f_flags & O_NONBLOCK)
                                return ret ? ret : -EAGAIN;
                        interruptible_sleep_on(&card->midi.owait);
                        if (signal_pending(current))
                                return ret ? ret : -ERESTARTSYS;
                        continue;
                }
                if (copy_from_user(card->midi.obuf + ptr, buffer, cnt))
                        return ret ? ret : -EFAULT;
                ptr = (ptr + cnt) % CS_MIDIOUTBUF;
                spin_lock_irqsave(&card->lock, flags);
                card->midi.owr = ptr;
                card->midi.ocnt += cnt;
                spin_unlock_irqrestore(&card->lock, flags);
                count -= cnt;
                buffer += cnt;
                ret += cnt;
                spin_lock_irqsave(&card->lock, flags);
                cs_handle_midi(card);
                spin_unlock_irqrestore(&card->lock, flags);
        }
        return ret;
}


static unsigned int cs_midi_poll(struct file *file, struct poll_table_struct *wait)
{
        struct cs_card *card = (struct cs_card *)file->private_data;
        unsigned long flags;
        unsigned int mask = 0;

        if (file->f_flags & FMODE_WRITE)
                poll_wait(file, &card->midi.owait, wait);
        if (file->f_flags & FMODE_READ)
                poll_wait(file, &card->midi.iwait, wait);
        spin_lock_irqsave(&card->lock, flags);
        if (file->f_flags & FMODE_READ) {
                if (card->midi.icnt > 0)
                        mask |= POLLIN | POLLRDNORM;
        }
        if (file->f_flags & FMODE_WRITE) {
                if (card->midi.ocnt < CS_MIDIOUTBUF)
                        mask |= POLLOUT | POLLWRNORM;
        }
        spin_unlock_irqrestore(&card->lock, flags);
        return mask;
}


static int cs_midi_open(struct inode *inode, struct file *file)
{
        unsigned int minor = iminor(inode);
        struct cs_card *card=NULL;
        unsigned long flags;
	struct list_head *entry;

	list_for_each(entry, &cs46xx_devs)
	{
		card = list_entry(entry, struct cs_card, list);
		if (card->dev_midi == minor)
			break;
	}

	if (entry == &cs46xx_devs)
		return -ENODEV;
	if (!card)
	{
		CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
			"cs46xx: cs46xx_midi_open(): Error - unable to find card struct\n"));
		return -ENODEV;
	}

        file->private_data = card;
        /* wait for device to become free */
        mutex_lock(&card->midi.open_mutex);
        while (card->midi.open_mode & file->f_mode) {
                if (file->f_flags & O_NONBLOCK) {
                        mutex_unlock(&card->midi.open_mutex);
                        return -EBUSY;
                }
                mutex_unlock(&card->midi.open_mutex);
                interruptible_sleep_on(&card->midi.open_wait);
                if (signal_pending(current))
                        return -ERESTARTSYS;
                mutex_lock(&card->midi.open_mutex);
        }
        spin_lock_irqsave(&card->midi.lock, flags);
        if (!(card->midi.open_mode & (FMODE_READ | FMODE_WRITE))) {
                card->midi.ird = card->midi.iwr = card->midi.icnt = 0;
                card->midi.ord = card->midi.owr = card->midi.ocnt = 0;
                card->midi.ird = card->midi.iwr = card->midi.icnt = 0;
                cs461x_pokeBA0(card, BA0_MIDCR, 0x0000000f);            /* Enable xmit, rcv. */
                cs461x_pokeBA0(card, BA0_HICR, HICR_IEV | HICR_CHGM);   /* Enable interrupts */
        }
        if (file->f_mode & FMODE_READ) {
                card->midi.ird = card->midi.iwr = card->midi.icnt = 0;
        }
        if (file->f_mode & FMODE_WRITE) {
                card->midi.ord = card->midi.owr = card->midi.ocnt = 0;
        }
        spin_unlock_irqrestore(&card->midi.lock, flags);
        card->midi.open_mode |= (file->f_mode & (FMODE_READ | FMODE_WRITE));
        mutex_unlock(&card->midi.open_mutex);
        return 0;
}


static int cs_midi_release(struct inode *inode, struct file *file)
{
        struct cs_card *card = (struct cs_card *)file->private_data;
        DECLARE_WAITQUEUE(wait, current);
        unsigned long flags;
        unsigned count, tmo;

        if (file->f_mode & FMODE_WRITE) {
                current->state = TASK_INTERRUPTIBLE;
                add_wait_queue(&card->midi.owait, &wait);
                for (;;) {
                        spin_lock_irqsave(&card->midi.lock, flags);
                        count = card->midi.ocnt;
                        spin_unlock_irqrestore(&card->midi.lock, flags);
                        if (count <= 0)
                                break;
                        if (signal_pending(current))
                                break;
                        if (file->f_flags & O_NONBLOCK)
                        	break;
                        tmo = (count * HZ) / 3100;
                        if (!schedule_timeout(tmo ? : 1) && tmo)
                                printk(KERN_DEBUG "cs46xx: midi timed out??\n");
                }
                remove_wait_queue(&card->midi.owait, &wait);
                current->state = TASK_RUNNING;
        }
        mutex_lock(&card->midi.open_mutex);
        card->midi.open_mode &= (~(file->f_mode & (FMODE_READ | FMODE_WRITE)));
        mutex_unlock(&card->midi.open_mutex);
        wake_up(&card->midi.open_wait);
        return 0;
}

/*
 *   Midi file operations struct.
 */
static /*const*/ struct file_operations cs_midi_fops = {
	CS_OWNER	CS_THIS_MODULE
	.llseek		= no_llseek,
	.read		= cs_midi_read,
	.write		= cs_midi_write,
	.poll		= cs_midi_poll,
	.open		= cs_midi_open,
	.release	= cs_midi_release,
};

/*
 *
 * CopySamples copies 16-bit stereo signed samples from the source to the
 * destination, possibly converting down to unsigned 8-bit and/or mono.
 * count specifies the number of output bytes to write.
 *
 *  Arguments:
 *
 *  dst             - Pointer to a destination buffer.
 *  src             - Pointer to a source buffer
 *  count           - The number of bytes to copy into the destination buffer.
 *  fmt             - CS_FMT_16BIT and/or CS_FMT_STEREO bits
 *  dmabuf          - pointer to the dma buffer structure
 *
 * NOTES: only call this routine if the output desired is not 16 Signed Stereo
 * 	
 *
 */
static void CopySamples(char *dst, char *src, int count, unsigned fmt, 
		struct dmabuf *dmabuf)
{

    s32 s32AudioSample;
    s16 *psSrc=(s16 *)src;
    s16 *psDst=(s16 *)dst;
    u8 *pucDst=(u8 *)dst;

    CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO "cs46xx: CopySamples()+ ") );
    CS_DBGOUT(CS_WAVE_READ, 8, printk(KERN_INFO
	" dst=%p src=%p count=%d fmt=0x%x\n",
	dst,src,count,fmt) );

    /*
     * See if the data should be output as 8-bit unsigned stereo.
     */
    if((fmt & CS_FMT_STEREO) && !(fmt & CS_FMT_16BIT))
    {
        /*
         * Convert each 16-bit signed stereo sample to 8-bit unsigned 
	 * stereo using rounding.
         */
        psSrc = (s16 *)src;
	count = count/2;
        while(count--)
        {
            *(pucDst++) = (u8)(((s16)(*psSrc++) + (s16)0x8000) >> 8);
        }
    }
    /*
     * See if the data should be output at 8-bit unsigned mono.
     */
    else if(!(fmt & CS_FMT_STEREO) && !(fmt & CS_FMT_16BIT))
    {
        /*
         * Convert each 16-bit signed stereo sample to 8-bit unsigned 
	 * mono using averaging and rounding.
         */
        psSrc = (s16 *)src;
	count = count/2;
        while(count--)
        {
	    s32AudioSample = ((*psSrc)+(*(psSrc + 1)))/2 + (s32)0x80;
	    if(s32AudioSample > 0x7fff)
	    	s32AudioSample = 0x7fff;
            *(pucDst++) = (u8)(((s16)s32AudioSample + (s16)0x8000) >> 8);
	    psSrc += 2;
        }
    }
    /*
     * See if the data should be output at 16-bit signed mono.
     */
    else if(!(fmt & CS_FMT_STEREO) && (fmt & CS_FMT_16BIT))
    {
        /*
         * Convert each 16-bit signed stereo sample to 16-bit signed 
	 * mono using averaging.
         */
        psSrc = (s16 *)src;
	count = count/2;
        while(count--)
        {
            *(psDst++) = (s16)((*psSrc)+(*(psSrc + 1)))/2;
	    psSrc += 2;
        }
    }
}

/*
 * cs_copy_to_user()
 * replacement for the standard copy_to_user, to allow for a conversion from
 * 16 bit to 8 bit and from stereo to mono, if the record conversion is active.  
 * The current CS46xx/CS4280 static image only records in 16bit unsigned Stereo, 
 * so we convert from any of the other format combinations.
 */
static unsigned cs_copy_to_user(
	struct cs_state *s, 
	void __user *dest, 
	void *hwsrc, 
	unsigned cnt, 
	unsigned *copied)
{
	struct dmabuf *dmabuf = &s->dmabuf;
	void *src = hwsrc;  /* default to the standard destination buffer addr */

	CS_DBGOUT(CS_FUNCTION, 6, printk(KERN_INFO 
		"cs_copy_to_user()+ fmt=0x%x cnt=%d dest=%p\n",
		dmabuf->fmt,(unsigned)cnt,dest) );

	if(cnt > dmabuf->dmasize)
	{
		cnt = dmabuf->dmasize;
	}
	if(!cnt)
	{
		*copied = 0;
		return 0;
	}
	if(dmabuf->divisor != 1)
	{
		if(!dmabuf->tmpbuff)
		{
			*copied = cnt/dmabuf->divisor;
			return 0;
		}

		CopySamples((char *)dmabuf->tmpbuff, (char *)hwsrc, cnt, 
			dmabuf->fmt, dmabuf);
		src = dmabuf->tmpbuff;
		cnt = cnt/dmabuf->divisor;
	}
        if (copy_to_user(dest, src, cnt))
	{
		CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_ERR 
			"cs46xx: cs_copy_to_user()- fault dest=%p src=%p cnt=%d\n",
				dest,src,cnt) );
		*copied = 0;
		return -EFAULT;
	}
	*copied = cnt;
	CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO 
		"cs46xx: cs_copy_to_user()- copied bytes is %d \n",cnt) );
	return 0;
}

/* in this loop, dmabuf.count signifies the amount of data that is waiting to be copied to
   the user's buffer.  it is filled by the dma machine and drained by this loop. */
static ssize_t cs_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
{
	struct cs_card *card = (struct cs_card *) file->private_data;
	struct cs_state *state;
	DECLARE_WAITQUEUE(wait, current);
	struct dmabuf *dmabuf;
	ssize_t ret = 0;
	unsigned long flags;
	unsigned swptr;
	int cnt;
	unsigned copied=0;

	CS_DBGOUT(CS_WAVE_READ | CS_FUNCTION, 4, 
		printk("cs46xx: cs_read()+ %zd\n",count) );
	state = (struct cs_state *)card->states[0];
	if(!state)
		return -ENODEV;
	dmabuf = &state->dmabuf;

	if (dmabuf->mapped)
		return -ENXIO;
	if (!access_ok(VERIFY_WRITE, buffer, count))
		return -EFAULT;
	
	mutex_lock(&state->sem);
	if (!dmabuf->ready && (ret = __prog_dmabuf(state)))
		goto out2;

	add_wait_queue(&state->dmabuf.wait, &wait);
	while (count > 0) {
		while(!(card->pm.flags & CS46XX_PM_IDLE))
		{
			schedule();
			if (signal_pending(current)) {
				if(!ret) ret = -ERESTARTSYS;
				goto out;
			}
		}
		spin_lock_irqsave(&state->card->lock, flags);
		swptr = dmabuf->swptr;
		cnt = dmabuf->dmasize - swptr;
		if (dmabuf->count < cnt)
			cnt = dmabuf->count;
		if (cnt <= 0)
			__set_current_state(TASK_INTERRUPTIBLE);
		spin_unlock_irqrestore(&state->card->lock, flags);

		if (cnt > (count * dmabuf->divisor))
			cnt = count * dmabuf->divisor;
		if (cnt <= 0) {
			/* buffer is empty, start the dma machine and wait for data to be
			   recorded */
			start_adc(state);
			if (file->f_flags & O_NONBLOCK) {
				if (!ret) ret = -EAGAIN;
				goto out;
 			}
			mutex_unlock(&state->sem);
			schedule();
			if (signal_pending(current)) {
				if(!ret) ret = -ERESTARTSYS;
				goto out;
			}
			mutex_lock(&state->sem);
			if (dmabuf->mapped) 
			{
				if(!ret)
					ret = -ENXIO;
				goto out;
			}
 			continue;
		}

		CS_DBGOUT(CS_WAVE_READ, 2, printk(KERN_INFO 
			"_read() copy_to cnt=%d count=%zd ", cnt,count) );
		CS_DBGOUT(CS_WAVE_READ, 8, printk(KERN_INFO 
			" .dmasize=%d .count=%d buffer=%p ret=%zd\n",
			dmabuf->dmasize,dmabuf->count,buffer,ret) );

                if (cs_copy_to_user(state, buffer, 
			(char *)dmabuf->rawbuf + swptr, cnt, &copied))
		{
			if (!ret) ret = -EFAULT;
			goto out;
		}
                swptr = (swptr + cnt) % dmabuf->dmasize;
                spin_lock_irqsave(&card->lock, flags);
                dmabuf->swptr = swptr;
                dmabuf->count -= cnt;
                spin_unlock_irqrestore(&card->lock, flags);
                count -= copied;
                buffer += copied;
                ret += copied;
                start_adc(state);
	}
out:
	remove_wait_queue(&state->dmabuf.wait, &wait);
out2:
	mutex_unlock(&state->sem);
	set_current_state(TASK_RUNNING);
	CS_DBGOUT(CS_WAVE_READ | CS_FUNCTION, 4, 
		printk("cs46xx: cs_read()- %zd\n",ret) );
	return ret;
}

/* in this loop, dmabuf.count signifies the amount of data that is waiting to be dma to
   the soundcard.  it is drained by the dma machine and filled by this loop. */
static ssize_t cs_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
{
	struct cs_card *card = (struct cs_card *) file->private_data;
	struct cs_state *state;
	DECLARE_WAITQUEUE(wait, current);
	struct dmabuf *dmabuf;
	ssize_t ret;
	unsigned long flags;
	unsigned swptr;
	int cnt;

	CS_DBGOUT(CS_WAVE_WRITE | CS_FUNCTION, 4,
		printk("cs46xx: cs_write called, count = %zd\n", count) );
	state = (struct cs_state *)card->states[1];
	if(!state)
		return -ENODEV;
	if (!access_ok(VERIFY_READ, buffer, count))
		return -EFAULT;
	dmabuf = &state->dmabuf;

	mutex_lock(&state->sem);
	if (dmabuf->mapped)
	{
		ret = -ENXIO;
		goto out;
	}

	if (!dmabuf->ready && (ret = __prog_dmabuf(state)))
		goto out;
	add_wait_queue(&state->dmabuf.wait, &wait);
	ret = 0;
/*
* Start the loop to read from the user's buffer and write to the dma buffer.
* check for PM events and underrun/overrun in the loop.
*/
	while (count > 0) {
		while(!(card->pm.flags & CS46XX_PM_IDLE))
		{
			schedule();
			if (signal_pending(current)) {
				if(!ret) ret = -ERESTARTSYS;
				goto out;
			}
		}
		spin_lock_irqsave(&state->card->lock, flags);
		if (dmabuf->count < 0) {
			/* buffer underrun, we are recovering from sleep_on_timeout,
			   resync hwptr and swptr */
			dmabuf->count = 0;
			dmabuf->swptr = dmabuf->hwptr;
		}
		if (dmabuf->underrun)
		{
			dmabuf->underrun = 0;
			dmabuf->hwptr = cs_get_dma_addr(state);
			dmabuf->swptr = dmabuf->hwptr;
		}

		swptr = dmabuf->swptr;
		cnt = dmabuf->dmasize - swptr;
		if (dmabuf->count + cnt > dmabuf->dmasize)
			cnt = dmabuf->dmasize - dmabuf->count;
		if (cnt <= 0)
			__set_current_state(TASK_INTERRUPTIBLE);
		spin_unlock_irqrestore(&state->card->lock, flags);

		if (cnt > count)
			cnt = count;
		if (cnt <= 0) {
			/* buffer is full, start the dma machine and wait for data to be
			   played */
			start_dac(state);
			if (file->f_flags & O_NONBLOCK) {
				if (!ret) ret = -EAGAIN;
				goto out;
 			}
			mutex_unlock(&state->sem);
			schedule();
 			if (signal_pending(current)) {
				if(!ret) ret = -ERESTARTSYS;
				goto out;
 			}
			mutex_lock(&state->sem);
			if (dmabuf->mapped)
			{
				if(!ret)
					ret = -ENXIO;
				goto out;
			}
 			continue;
 		}
		if (copy_from_user(dmabuf->rawbuf + swptr, buffer, cnt)) {
			if (!ret) ret = -EFAULT;
			goto out;
		}
		spin_lock_irqsave(&state->card->lock, flags);
		swptr = (swptr + cnt) % dmabuf->dmasize;
		dmabuf->swptr = swptr;
		dmabuf->count += cnt;
		if(dmabuf->count > dmabuf->dmasize)
		{
			CS_DBGOUT(CS_WAVE_WRITE | CS_ERROR, 2, printk(
			    "cs46xx: cs_write() d->count > dmasize - resetting\n"));
			dmabuf->count = dmabuf->dmasize;
		}
		dmabuf->endcleared = 0;
		spin_unlock_irqrestore(&state->card->lock, flags);

		count -= cnt;
		buffer += cnt;
		ret += cnt;
		start_dac(state);
	}
out:
	mutex_unlock(&state->sem);
	remove_wait_queue(&state->dmabuf.wait, &wait);
	set_current_state(TASK_RUNNING);

	CS_DBGOUT(CS_WAVE_WRITE | CS_FUNCTION, 2, 
		printk("cs46xx: cs_write()- ret=%zd\n", ret) );
	return ret;
}

static unsigned int cs_poll(struct file *file, struct poll_table_struct *wait)
{
	struct cs_card *card = (struct cs_card *)file->private_data;
	struct dmabuf *dmabuf;
	struct cs_state *state;

	unsigned long flags;
	unsigned int mask = 0;

	CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_poll()+ \n"));
	if (!(file->f_mode & (FMODE_WRITE | FMODE_READ)))
	{
		return -EINVAL;
	}
	if (file->f_mode & FMODE_WRITE)
	{
		state = card->states[1];
		if(state)
		{
			dmabuf = &state->dmabuf;
			poll_wait(file, &dmabuf->wait, wait);
		}
	}
	if (file->f_mode & FMODE_READ)
	{
		state = card->states[0];
		if(state)
		{
			dmabuf = &state->dmabuf;
			poll_wait(file, &dmabuf->wait, wait);
		}
	}

	spin_lock_irqsave(&card->lock, flags);
	cs_update_ptr(card, CS_FALSE);
	if (file->f_mode & FMODE_READ) {
		state = card->states[0];
		if(state)
		{
			dmabuf = &state->dmabuf;
			if (dmabuf->count >= (signed)dmabuf->fragsize)
				mask |= POLLIN | POLLRDNORM;
		}
	}
	if (file->f_mode & FMODE_WRITE) {
		state = card->states[1];
		if(state)
		{
			dmabuf = &state->dmabuf;
			if (dmabuf->mapped) {
				if (dmabuf->count >= (signed)dmabuf->fragsize)
				    mask |= POLLOUT | POLLWRNORM;
			} else {
				if ((signed)dmabuf->dmasize >= dmabuf->count 
					+ (signed)dmabuf->fragsize)
				    mask |= POLLOUT | POLLWRNORM;
			}
		}
	}
	spin_unlock_irqrestore(&card->lock, flags);

	CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_poll()- (0x%x) \n",
		mask));
	return mask;
}

/*
 *	We let users mmap the ring buffer. Its not the real DMA buffer but
 *	that side of the code is hidden in the IRQ handling. We do a software
 *	emulation of DMA from a 64K or so buffer into a 2K FIFO. 
 *	(the hardware probably deserves a moan here but Crystal send me nice
 *	toys ;)).
 */
 
static int cs_mmap(struct file *file, struct vm_area_struct *vma)
{
	struct cs_card *card = (struct cs_card *)file->private_data;
	struct cs_state *state;
	struct dmabuf *dmabuf;
	int ret = 0;
	unsigned long size;

	CS_DBGOUT(CS_FUNCTION | CS_PARMS, 2, printk("cs46xx: cs_mmap()+ file=%p %s %s\n", 
		file, vma->vm_flags & VM_WRITE ? "VM_WRITE" : "",
		vma->vm_flags & VM_READ ? "VM_READ" : "") );

	if (vma->vm_flags & VM_WRITE) {
		state = card->states[1];
		if(state)
		{
			CS_DBGOUT(CS_OPEN, 2, printk(
			  "cs46xx: cs_mmap() VM_WRITE - state TRUE prog_dmabuf DAC\n") );
			if ((ret = prog_dmabuf(state)) != 0)
				return ret;
		}
	} else if (vma->vm_flags & VM_READ) {
		state = card->states[0];
		if(state)
		{
			CS_DBGOUT(CS_OPEN, 2, printk(
			  "cs46xx: cs_mmap() VM_READ - state TRUE prog_dmabuf ADC\n") );
			if ((ret = prog_dmabuf(state)) != 0)
				return ret;
		}
	} else {
		CS_DBGOUT(CS_ERROR, 2, printk(
		  "cs46xx: cs_mmap() return -EINVAL\n") );
		return -EINVAL;
	}

/*
 * For now ONLY support playback, but seems like the only way to use
 * mmap() is to open an FD with RDWR, just read or just write access
 * does not function, get an error back from the kernel.
 * Also, QuakeIII opens with RDWR!  So, there must be something
 * to needing read/write access mapping.  So, allow read/write but 
 * use the DAC only.
 */
	state = card->states[1];  
	if (!state) {
		ret = -EINVAL;
		goto out;
	}

	mutex_lock(&state->sem);
	dmabuf = &state->dmabuf;
	if (cs4x_pgoff(vma) != 0)
	{
		ret = -EINVAL;
		goto out;
	}
	size = vma->vm_end - vma->vm_start;

	CS_DBGOUT(CS_PARMS, 2, printk("cs46xx: cs_mmap(): size=%d\n",(unsigned)size) );

	if (size > (PAGE_SIZE << dmabuf->buforder))
	{
		ret = -EINVAL;
		goto out;
	}
	if (remap_pfn_range(vma, vma->vm_start,
			     virt_to_phys(dmabuf->rawbuf) >> PAGE_SHIFT,
			     size, vma->vm_page_prot))
	{
		ret = -EAGAIN;
		goto out;
	}
	dmabuf->mapped = 1;

	CS_DBGOUT(CS_FUNCTION, 2, printk("cs46xx: cs_mmap()-\n") );
out:
	mutex_unlock(&state->sem);
	return ret;	
}

static int cs_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
	struct cs_card *card = (struct cs_card *)file->private_data;
	struct cs_state *state;
	struct dmabuf *dmabuf=NULL;
	unsigned long flags;
	audio_buf_info abinfo;
	count_info cinfo;
	int val, valsave, mapped, ret;
	void __user *argp = (void __user *)arg;
	int __user *p = argp;

	state = (struct cs_state *)card->states[0];
	if(state)
	{
		dmabuf = &state->dmabuf;
		mapped = (file->f_mode & FMODE_READ) && dmabuf->mapped;
	}
	state = (struct cs_state *)card->states[1];
	if(state)
	{
		dmabuf = &state->dmabuf;
		mapped |= (file->f_mode & FMODE_WRITE) && dmabuf->mapped;
	}
		
#if CSDEBUG
	printioctl(cmd);
#endif

	switch (cmd) 
	{
	case OSS_GETVERSION:
		return put_user(SOUND_VERSION, p);

	case SNDCTL_DSP_RESET:
		/* FIXME: spin_lock ? */
		if (file->f_mode & FMODE_WRITE) {
			state = (struct cs_state *)card->states[1];
			if(state)
			{
				dmabuf = &state->dmabuf;
				stop_dac(state);
				synchronize_irq(card->irq);
				dmabuf->ready = 0;
				resync_dma_ptrs(state);
				dmabuf->swptr = dmabuf->hwptr = 0;
				dmabuf->count = dmabuf->total_bytes = 0;
				dmabuf->blocks = 0;
				dmabuf->SGok = 0;
			}
		}
		if (file->f_mode & FMODE_READ) {
			state = (struct cs_state *)card->states[0];
			if(state)
			{
				dmabuf = &state->dmabuf;
				stop_adc(state);
				synchronize_irq(card->irq);
				resync_dma_ptrs(state);
				dmabuf->ready = 0;
				dmabuf->swptr = dmabuf->hwptr = 0;
				dmabuf->count = dmabuf->total_bytes = 0;
				dmabuf->blocks = 0;
				dmabuf->SGok = 0;
			}
		}
		CS_DBGOUT(CS_IOCTL, 2, printk("cs46xx: DSP_RESET()-\n") );
		return 0;

	case SNDCTL_DSP_SYNC:
		if (file->f_mode & FMODE_WRITE)
			return drain_dac(state, file->f_flags & O_NONBLOCK);
		return 0;

	case SNDCTL_DSP_SPEED: /* set sample rate */
		if (get_user(val, p))
			return -EFAULT;
		if (val >= 0) {
			if (file->f_mode & FMODE_READ) {
				state = (struct cs_state *)card->states[0];
				if(state)
				{
					dmabuf = &state->dmabuf;
					stop_adc(state);
					dmabuf->ready = 0;
					dmabuf->SGok = 0;
					cs_set_adc_rate(state, val);
					cs_set_divisor(dmabuf);
				}
			}
			if (file->f_mode & FMODE_WRITE) {
				state = (struct cs_state *)card->states[1];
				if(state)
				{
					dmabuf = &state->dmabuf;
					stop_dac(state);
					dmabuf->ready = 0;
					dmabuf->SGok = 0;
					cs_set_dac_rate(state, val);
					cs_set_divisor(dmabuf);
				}
			}
			CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(
			    "cs46xx: cs_ioctl() DSP_SPEED %s %s %d\n",
				file->f_mode & FMODE_WRITE ? "DAC" : "",
				file->f_mode & FMODE_READ ? "ADC" : "",
				dmabuf->rate ) );
			return put_user(dmabuf->rate, p);
		}
		return put_user(0, p);

	case SNDCTL_DSP_STEREO: /* set stereo or mono channel */
		if (get_user(val, p))
			return -EFAULT;
		if (file->f_mode & FMODE_WRITE) {
			state = (struct cs_state *)card->states[1];
			if(state)
			{
				dmabuf = &state->dmabuf;
				stop_dac(state);
				dmabuf->ready = 0;
				dmabuf->SGok = 0;
				if(val)
					dmabuf->fmt |= CS_FMT_STEREO;
				else
					dmabuf->fmt &= ~CS_FMT_STEREO;
				cs_set_divisor(dmabuf);
				CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(
				    "cs46xx: DSP_STEREO() DAC %s\n",
				    (dmabuf->fmt & CS_FMT_STEREO) ?
					"STEREO":"MONO") );
			}
		}
		if (file->f_mode & FMODE_READ) {
			state = (struct cs_state *)card->states[0];
			if(state)
			{
				dmabuf = &state->dmabuf;
				stop_adc(state);
				dmabuf->ready = 0;
				dmabuf->SGok = 0;
				if(val)
					dmabuf->fmt |= CS_FMT_STEREO;
				else
					dmabuf->fmt &= ~CS_FMT_STEREO;
				cs_set_divisor(dmabuf);
				CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(
				    "cs46xx: DSP_STEREO() ADC %s\n",
				    (dmabuf->fmt & CS_FMT_STEREO) ?
					"STEREO":"MONO") );
			}
		}
		return 0;

	case SNDCTL_DSP_GETBLKSIZE:
		if (file->f_mode & FMODE_WRITE) {
			state = (struct cs_state *)card->states[1];
			if(state)
			{
				dmabuf = &state->dmabuf;
				if ((val = prog_dmabuf(state)))
					return val;
				return put_user(dmabuf->fragsize, p);
			}
		}
		if (file->f_mode & FMODE_READ) {
			state = (struct cs_state *)card->states[0];
			if(state)
			{
				dmabuf = &state->dmabuf;
				if ((val = prog_dmabuf(state)))
					return val;
				return put_user(dmabuf->fragsize/dmabuf->divisor, 
						p);
			}
		}
		return put_user(0, p);

	case SNDCTL_DSP_GETFMTS: /* Returns a mask of supported sample format*/
		return put_user(AFMT_S16_LE | AFMT_U8, p);

	case SNDCTL_DSP_SETFMT: /* Select sample format */
		if (get_user(val, p))
			return -EFAULT;
		CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(
		    "cs46xx: cs_ioctl() DSP_SETFMT %s %s %s %s\n",
			file->f_mode & FMODE_WRITE ? "DAC" : "",
			file->f_mode & FMODE_READ ? "ADC" : "",
			val == AFMT_S16_LE ? "16Bit Signed" : "",
			val == AFMT_U8 ? "8Bit Unsigned" : "") );
		valsave = val;
		if (val != AFMT_QUERY) {
			if(val==AFMT_S16_LE || val==AFMT_U8)
			{
				if (file->f_mode & FMODE_WRITE) {
					state = (struct cs_state *)card->states[1];
					if(state)
					{
						dmabuf = &state->dmabuf;
						stop_dac(state);
						dmabuf->ready = 0;
						dmabuf->SGok = 0;
						if(val==AFMT_S16_LE)
							dmabuf->fmt |= CS_FMT_16BIT;
						else
							dmabuf->fmt &= ~CS_FMT_16BIT;
						cs_set_divisor(dmabuf);
						if((ret = prog_dmabuf(state)))
							return ret;
					}
				}
				if (file->f_mode & FMODE_READ) {
					val = valsave;
					state = (struct cs_state *)card->states[0];
					if(state)
					{
						dmabuf = &state->dmabuf;
						stop_adc(state);
						dmabuf->ready = 0;
						dmabuf->SGok = 0;
						if(val==AFMT_S16_LE)
							dmabuf->fmt |= CS_FMT_16BIT;
						else
							dmabuf->fmt &= ~CS_FMT_16BIT;
						cs_set_divisor(dmabuf);
						if((ret = prog_dmabuf(state)))
							return ret;
					}
				}
			}
			else
			{
				CS_DBGOUT(CS_IOCTL | CS_ERROR, 2, printk(
				    "cs46xx: DSP_SETFMT() Unsupported format (0x%x)\n",
					valsave) );
			}
		}
		else
		{
			if(file->f_mode & FMODE_WRITE)
			{
				state = (struct cs_state *)card->states[1];
				if(state)
					dmabuf = &state->dmabuf;
			}
			else if(file->f_mode & FMODE_READ)
			{
				state = (struct cs_state *)card->states[0];
				if(state)
					dmabuf = &state->dmabuf;
			}
		}
		if(dmabuf)
		{
			if(dmabuf->fmt & CS_FMT_16BIT)
				return put_user(AFMT_S16_LE, p);
			else
				return put_user(AFMT_U8, p);
		}
		return put_user(0, p);

	case SNDCTL_DSP_CHANNELS:
		if (get_user(val, p))
			return -EFAULT;
		if (val != 0) {
			if (file->f_mode & FMODE_WRITE) {
				state = (struct cs_state *)card->states[1];
				if(state)
				{
					dmabuf = &state->dmabuf;
					stop_dac(state);
					dmabuf->ready = 0;
					dmabuf->SGok = 0;
					if(val>1)
						dmabuf->fmt |= CS_FMT_STEREO;
					else
						dmabuf->fmt &= ~CS_FMT_STEREO;
					cs_set_divisor(dmabuf);
					if (prog_dmabuf(state))
						return 0;
				}
			}
			if (file->f_mode & FMODE_READ) {
				state = (struct cs_state *)card->states[0];
				if(state)
				{
					dmabuf = &state->dmabuf;
					stop_adc(state);
					dmabuf->ready = 0;
					dmabuf->SGok = 0;
					if(val>1)
						dmabuf->fmt |= CS_FMT_STEREO;
					else
						dmabuf->fmt &= ~CS_FMT_STEREO;
					cs_set_divisor(dmabuf);
					if (prog_dmabuf(state))
						return 0;
				}
			}
		}
		return put_user((dmabuf->fmt & CS_FMT_STEREO) ? 2 : 1,
				p);

	case SNDCTL_DSP_POST:
		/*
		 * There will be a longer than normal pause in the data.
		 * so... do nothing, because there is nothing that we can do.
		 */
		return 0;

	case SNDCTL_DSP_SUBDIVIDE:
		if (file->f_mode & FMODE_WRITE) {
			state = (struct cs_state *)card->states[1];
			if(state)
			{
				dmabuf = &state->dmabuf;
				if (dmabuf->subdivision)
					return -EINVAL;
				if (get_user(val, p))
					return -EFAULT;
				if (val != 1 && val != 2)
					return -EINVAL;
				dmabuf->subdivision = val;
			}
		}
		if (file->f_mode & FMODE_READ) {
			state = (struct cs_state *)card->states[0];
			if(state)
			{
				dmabuf = &state->dmabuf;
				if (dmabuf->subdivision)
					return -EINVAL;
				if (get_user(val, p))
					return -EFAULT;
				if (val != 1 && val != 2)
					return -EINVAL;
				dmabuf->subdivision = val;
			}
		}
		return 0;

	case SNDCTL_DSP_SETFRAGMENT:
		if (get_user(val, p))
			return -EFAULT;

		if (file->f_mode & FMODE_WRITE) {
			state = (struct cs_state *)card->states[1];
			if(state)
			{
				dmabuf = &state->dmabuf;
				dmabuf->ossfragshift = val & 0xffff;
				dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
			}
		}
		if (file->f_mode & FMODE_READ) {
			state = (struct cs_state *)card->states[0];
			if(state)
			{
				dmabuf = &state->dmabuf;
				dmabuf->ossfragshift = val & 0xffff;
				dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
			}
		}
		return 0;

	case SNDCTL_DSP_GETOSPACE:
		if (!(file->f_mode & FMODE_WRITE))
			return -EINVAL;
		state = (struct cs_state *)card->states[1];
		if(state)
		{
			dmabuf = &state->dmabuf;
			spin_lock_irqsave(&state->card->lock, flags);
			cs_update_ptr(card, CS_TRUE);
			abinfo.fragsize = dmabuf->fragsize;
			abinfo.fragstotal = dmabuf->numfrag;
		/*
		 * for mmap we always have total space available
		 */
			if (dmabuf->mapped)
				abinfo.bytes = dmabuf->dmasize;
			else
				abinfo.bytes = dmabuf->dmasize - dmabuf->count;

			abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
			spin_unlock_irqrestore(&state->card->lock, flags);
			return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
		}
		return -ENODEV;

	case SNDCTL_DSP_GETISPACE:
		if (!(file->f_mode & FMODE_READ))
			return -EINVAL;
		state = (struct cs_state *)card->states[0];
		if(state)
		{
			dmabuf = &state->dmabuf;
			spin_lock_irqsave(&state->card->lock, flags);
			cs_update_ptr(card, CS_TRUE);
			abinfo.fragsize = dmabuf->fragsize/dmabuf->divisor;
			abinfo.bytes = dmabuf->count/dmabuf->divisor;
			abinfo.fragstotal = dmabuf->numfrag;
			abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
			spin_unlock_irqrestore(&state->card->lock, flags);
			return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
		}
		return -ENODEV;

	case SNDCTL_DSP_NONBLOCK:
		file->f_flags |= O_NONBLOCK;
		return 0;

	case SNDCTL_DSP_GETCAPS:
		return put_user(DSP_CAP_REALTIME|DSP_CAP_TRIGGER|DSP_CAP_MMAP,
			    p);

	case SNDCTL_DSP_GETTRIGGER:
		val = 0;
		CS_DBGOUT(CS_IOCTL, 2, printk("cs46xx: DSP_GETTRIGGER()+\n") );
		if (file->f_mode & FMODE_WRITE)
		{
			state = (struct cs_state *)card->states[1];
			if(state)
			{
				dmabuf = &state->dmabuf;
				if(dmabuf->enable & DAC_RUNNING)
					val |= PCM_ENABLE_INPUT;
			}
		}
		if (file->f_mode & FMODE_READ)
		{
			if(state)
			{
				state = (struct cs_state *)card->states[0];
				dmabuf = &state->dmabuf;
				if(dmabuf->enable & ADC_RUNNING)
					val |= PCM_ENABLE_OUTPUT;
			}
		}
		CS_DBGOUT(CS_IOCTL, 2, printk("cs46xx: DSP_GETTRIGGER()- val=0x%x\n",val) );
		return put_user(val, p);

	case SNDCTL_DSP_SETTRIGGER:
		if (get_user(val, p))
			return -EFAULT;
		if (file->f_mode & FMODE_READ) {
			state = (struct cs_state *)card->states[0];
			if(state)
			{
				dmabuf = &state->dmabuf;
				if (val & PCM_ENABLE_INPUT) {
					if (!dmabuf->ready && (ret = prog_dmabuf(state)))
						return ret;
					start_adc(state);
				} else
					stop_adc(state);
			}
		}
		if (file->f_mode & FMODE_WRITE) {
			state = (struct cs_state *)card->states[1];
			if(state)
			{
				dmabuf = &state->dmabuf;
				if (val & PCM_ENABLE_OUTPUT) {
					if (!dmabuf->ready && (ret = prog_dmabuf(state)))
						return ret;
					start_dac(state);
				} else
					stop_dac(state);
			}
		}
		return 0;

	case SNDCTL_DSP_GETIPTR:
		if (!(file->f_mode & FMODE_READ))
			return -EINVAL;
		state = (struct cs_state *)card->states[0];
		if(state)
		{
			dmabuf = &state->dmabuf;
			spin_lock_irqsave(&state->card->lock, flags);
			cs_update_ptr(card, CS_TRUE);
			cinfo.bytes = dmabuf->total_bytes/dmabuf->divisor;
			cinfo.blocks = dmabuf->count/dmabuf->divisor >> dmabuf->fragshift;
			cinfo.ptr = dmabuf->hwptr/dmabuf->divisor;
			spin_unlock_irqrestore(&state->card->lock, flags);
			if (copy_to_user(argp, &cinfo, sizeof(cinfo)))
				return -EFAULT;
			return 0;
		}
		return -ENODEV;

	case SNDCTL_DSP_GETOPTR:
		if (!(file->f_mode & FMODE_WRITE))
			return -EINVAL;
		state = (struct cs_state *)card->states[1];
		if(state)
		{
			dmabuf = &state->dmabuf;
			spin_lock_irqsave(&state->card->lock, flags);
			cs_update_ptr(card, CS_TRUE);
			cinfo.bytes = dmabuf->total_bytes;
			if (dmabuf->mapped)
			{
				cinfo.blocks = (cinfo.bytes >> dmabuf->fragshift) 
							- dmabuf->blocks;
				CS_DBGOUT(CS_PARMS, 8, 
					printk("total_bytes=%d blocks=%d dmabuf->blocks=%d\n", 
					cinfo.bytes,cinfo.blocks,dmabuf->blocks) );
				dmabuf->blocks = cinfo.bytes >> dmabuf->fragshift;
			}
			else
			{
				cinfo.blocks = dmabuf->count >> dmabuf->fragshift;
			}
			cinfo.ptr = dmabuf->hwptr;

			CS_DBGOUT(CS_PARMS, 4, printk(
			    "cs46xx: GETOPTR bytes=%d blocks=%d ptr=%d\n",
				cinfo.bytes,cinfo.blocks,cinfo.ptr) );
			spin_unlock_irqrestore(&state->card->lock, flags);
			if (copy_to_user(argp, &cinfo, sizeof(cinfo)))
				return -EFAULT;
			return 0;
		}
		return -ENODEV;

	case SNDCTL_DSP_SETDUPLEX:
		return 0;

	case SNDCTL_DSP_GETODELAY:
		if (!(file->f_mode & FMODE_WRITE))
			return -EINVAL;
		state = (struct cs_state *)card->states[1];
		if(state)
		{
			dmabuf = &state->dmabuf;
			spin_lock_irqsave(&state->card->lock, flags);
			cs_update_ptr(card, CS_TRUE);
			val = dmabuf->count;
			spin_unlock_irqrestore(&state->card->lock, flags);
		}
		else
			val = 0;
		return put_user(val, p);

	case SOUND_PCM_READ_RATE:
		if(file->f_mode & FMODE_READ)
			state = (struct cs_state *)card->states[0];
		else 
			state = (struct cs_state *)card->states[1];
		if(state)
		{
			dmabuf = &state->dmabuf;
			return put_user(dmabuf->rate, p);
		}
		return put_user(0, p);
		

	case SOUND_PCM_READ_CHANNELS:
		if(file->f_mode & FMODE_READ)
			state = (struct cs_state *)card->states[0];
		else 
			state = (struct cs_state *)card->states[1];
		if(state)
		{
			dmabuf = &state->dmabuf;
			return put_user((dmabuf->fmt & CS_FMT_STEREO) ? 2 : 1,
				p);
		}
		return put_user(0, p);

	case SOUND_PCM_READ_BITS:
		if(file->f_mode & FMODE_READ)
			state = (struct cs_state *)card->states[0];
		else 
			state = (struct cs_state *)card->states[1];
		if(state)
		{
			dmabuf = &state->dmabuf;
			return put_user((dmabuf->fmt & CS_FMT_16BIT) ? 
			  	AFMT_S16_LE : AFMT_U8, p);

		}
		return put_user(0, p);

	case SNDCTL_DSP_MAPINBUF:
	case SNDCTL_DSP_MAPOUTBUF:
	case SNDCTL_DSP_SETSYNCRO:
	case SOUND_PCM_WRITE_FILTER:
	case SOUND_PCM_READ_FILTER:
		return -EINVAL;
	}
	return -EINVAL;
}


/*
 *	AMP control - null AMP
 */
 
static void amp_none(struct cs_card *card, int change)
{	
}

/*
 *	Crystal EAPD mode
 */
 
static void amp_voyetra(struct cs_card *card, int change)
{
	/* Manage the EAPD bit on the Crystal 4297 
	   and the Analog AD1885 */
	   
	int old=card->amplifier;
	
	card->amplifier+=change;
	if(card->amplifier && !old)
	{
		/* Turn the EAPD amp on */
		cs_ac97_set(card->ac97_codec[0],  AC97_POWER_CONTROL, 
			cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) |
				0x8000);
	}
	else if(old && !card->amplifier)
	{
		/* Turn the EAPD amp off */
		cs_ac97_set(card->ac97_codec[0],  AC97_POWER_CONTROL, 
			cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) &
				~0x8000);
	}
}

		       
/*
 *	Game Theatre XP card - EGPIO[2] is used to enable the external amp.
 */
 
static void amp_hercules(struct cs_card *card, int change)
{
	int old=card->amplifier;
	if(!card)
	{
		CS_DBGOUT(CS_ERROR, 2, printk(KERN_INFO 
			"cs46xx: amp_hercules() called before initialized.\n"));
		return;
	}
	card->amplifier+=change;
	if( (card->amplifier && !old) && !(hercules_egpio_disable))
	{
		CS_DBGOUT(CS_PARMS, 4, printk(KERN_INFO 
			"cs46xx: amp_hercules() external amp enabled\n"));
		cs461x_pokeBA0(card, BA0_EGPIODR, 
			EGPIODR_GPOE2);     /* enable EGPIO2 output */
		cs461x_pokeBA0(card, BA0_EGPIOPTR, 
			EGPIOPTR_GPPT2);   /* open-drain on output */
	}
	else if(old && !card->amplifier)
	{
		CS_DBGOUT(CS_PARMS, 4, printk(KERN_INFO 
			"cs46xx: amp_hercules() external amp disabled\n"));
		cs461x_pokeBA0(card, BA0_EGPIODR, 0); /* disable */
		cs461x_pokeBA0(card, BA0_EGPIOPTR, 0); /* disable */
	}
}

/*
 *	Handle the CLKRUN on a thinkpad. We must disable CLKRUN support
 *	whenever we need to beat on the chip.
 *
 *	The original idea and code for this hack comes from David Kaiser at
 *	Linuxcare. Perhaps one day Crystal will document their chips well
 *	enough to make them useful.
 */
 
static void clkrun_hack(struct cs_card *card, int change)
{
	struct pci_dev *acpi_dev;
	u16 control;
	u8 pp;
	unsigned long port;
	int old=card->active;
	
	card->active+=change;
	
	acpi_dev = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
	if(acpi_dev == NULL)
		return;		/* Not a thinkpad thats for sure */

	/* Find the control port */		
	pci_read_config_byte(acpi_dev, 0x41, &pp);
	port=pp<<8;

	/* Read ACPI port */	
	control=inw(port+0x10);

	/* Flip CLKRUN off while running */
	if(!card->active && old)
	{
		CS_DBGOUT(CS_PARMS , 9, printk( KERN_INFO
			"cs46xx: clkrun() enable clkrun - change=%d active=%d\n",
				change,card->active));
		outw(control|0x2000, port+0x10);
	}
	else 
	{
	/*
	* sometimes on a resume the bit is set, so always reset the bit.
	*/
		CS_DBGOUT(CS_PARMS , 9, printk( KERN_INFO
			"cs46xx: clkrun() disable clkrun - change=%d active=%d\n",
				change,card->active));
		outw(control&~0x2000, port+0x10);
	}
}

	
static int cs_open(struct inode *inode, struct file *file)
{
	struct cs_card *card = (struct cs_card *)file->private_data;
	struct cs_state *state = NULL;
	struct dmabuf *dmabuf = NULL;
	struct list_head *entry;
        unsigned int minor = iminor(inode);
	int ret=0;
	unsigned int tmp;

	CS_DBGOUT(CS_OPEN | CS_FUNCTION, 2, printk("cs46xx: cs_open()+ file=%p %s %s\n",
		file, file->f_mode & FMODE_WRITE ? "FMODE_WRITE" : "",
		file->f_mode & FMODE_READ ? "FMODE_READ" : "") );

	list_for_each(entry, &cs46xx_devs)
	{
		card = list_entry(entry, struct cs_card, list);

		if (!((card->dev_audio ^ minor) & ~0xf))
			break;
	}
	if (entry == &cs46xx_devs)
		return -ENODEV;
	if (!card) {
		CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
			"cs46xx: cs_open(): Error - unable to find audio card struct\n"));
		return -ENODEV;
	}

	/*
	 * hardcode state[0] for capture, [1] for playback
	 */
	if(file->f_mode & FMODE_READ)
	{
		CS_DBGOUT(CS_WAVE_READ, 2, printk("cs46xx: cs_open() FMODE_READ\n") );
		if (card->states[0] == NULL) {
			state = card->states[0] = (struct cs_state *)
				kmalloc(sizeof(struct cs_state), GFP_KERNEL);
			if (state == NULL)
				return -ENOMEM;
			memset(state, 0, sizeof(struct cs_state));
			mutex_init(&state->sem);
			dmabuf = &state->dmabuf;
			dmabuf->pbuf = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
			if(dmabuf->pbuf==NULL)
			{
				kfree(state);
				card->states[0]=NULL;
				return -ENOMEM;
			}
		}
		else
		{
			state = card->states[0];
			if(state->open_mode & FMODE_READ)
				return -EBUSY;
		}
		dmabuf->channel = card->alloc_rec_pcm_channel(card);
			
		if (dmabuf->channel == NULL) {
			kfree (card->states[0]);
			card->states[0] = NULL;
			return -ENODEV;
		}

		/* Now turn on external AMP if needed */
		state->card = card;
		state->card->active_ctrl(state->card,1);
		state->card->amplifier_ctrl(state->card,1);
		
		if( (tmp = cs46xx_powerup(card, CS_POWER_ADC)) )
		{
			CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
				"cs46xx: cs46xx_powerup of ADC failed (0x%x)\n",tmp) );
			return -EIO;
		}

		dmabuf->channel->state = state;
		/* initialize the virtual channel */
		state->virt = 0;
		state->magic = CS_STATE_MAGIC;
		init_waitqueue_head(&dmabuf->wait);
		mutex_init(&state->open_mutex);
		file->private_data = card;

		mutex_lock(&state->open_mutex);

		/* set default sample format. According to OSS Programmer's Guide  /dev/dsp
		   should be default to unsigned 8-bits, mono, with sample rate 8kHz and
		   /dev/dspW will accept 16-bits sample */

		/* Default input is 8bit mono */
		dmabuf->fmt &= ~CS_FMT_MASK;
		dmabuf->type = CS_TYPE_ADC;
		dmabuf->ossfragshift = 0;
		dmabuf->ossmaxfrags  = 0;
		dmabuf->subdivision  = 0;
		cs_set_adc_rate(state, 8000);
		cs_set_divisor(dmabuf);

		state->open_mode |= FMODE_READ;
		mutex_unlock(&state->open_mutex);
	}
	if(file->f_mode & FMODE_WRITE)
	{
		CS_DBGOUT(CS_OPEN, 2, printk("cs46xx: cs_open() FMODE_WRITE\n") );
		if (card->states[1] == NULL) {
			state = card->states[1] = (struct cs_state *)
				kmalloc(sizeof(struct cs_state), GFP_KERNEL);
			if (state == NULL)
				return -ENOMEM;
			memset(state, 0, sizeof(struct cs_state));
			mutex_init(&state->sem);
			dmabuf = &state->dmabuf;
			dmabuf->pbuf = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
			if(dmabuf->pbuf==NULL)
			{
				kfree(state);
				card->states[1]=NULL;
				return -ENOMEM;
			}
		}
		else
		{
			state = card->states[1];
			if(state->open_mode & FMODE_WRITE)
				return -EBUSY;
		}
		dmabuf->channel = card->alloc_pcm_channel(card);
			
		if (dmabuf->channel == NULL) {
			kfree (card->states[1]);
			card->states[1] = NULL;
			return -ENODEV;
		}

		/* Now turn on external AMP if needed */
		state->card = card;
		state->card->active_ctrl(state->card,1);
		state->card->amplifier_ctrl(state->card,1);

		if( (tmp = cs46xx_powerup(card, CS_POWER_DAC)) )
		{
			CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
				"cs46xx: cs46xx_powerup of DAC failed (0x%x)\n",tmp) );
			return -EIO;
		}
		
		dmabuf->channel->state = state;
		/* initialize the virtual channel */
		state->virt = 1;
		state->magic = CS_STATE_MAGIC;
		init_waitqueue_head(&dmabuf->wait);
		mutex_init(&state->open_mutex);
		file->private_data = card;

		mutex_lock(&state->open_mutex);

		/* set default sample format. According to OSS Programmer's Guide  /dev/dsp
		   should be default to unsigned 8-bits, mono, with sample rate 8kHz and
		   /dev/dspW will accept 16-bits sample */

		/* Default output is 8bit mono. */
		dmabuf->fmt &= ~CS_FMT_MASK;
		dmabuf->type = CS_TYPE_DAC;
		dmabuf->ossfragshift = 0;
		dmabuf->ossmaxfrags  = 0;
		dmabuf->subdivision  = 0;
		cs_set_dac_rate(state, 8000);
		cs_set_divisor(dmabuf);

		state->open_mode |= FMODE_WRITE;
		mutex_unlock(&state->open_mutex);
		if((ret = prog_dmabuf(state)))
			return ret;
	}
	CS_DBGOUT(CS_OPEN | CS_FUNCTION, 2, printk("cs46xx: cs_open()- 0\n") );
	return nonseekable_open(inode, file);
}

static int cs_release(struct inode *inode, struct file *file)
{
	struct cs_card *card = (struct cs_card *)file->private_data;
	struct dmabuf *dmabuf;
	struct cs_state *state;
	unsigned int tmp;
	CS_DBGOUT(CS_RELEASE | CS_FUNCTION, 2, printk("cs46xx: cs_release()+ file=%p %s %s\n",
		file, file->f_mode & FMODE_WRITE ? "FMODE_WRITE" : "",
		file->f_mode & FMODE_READ ? "FMODE_READ" : "") );

	if (!(file->f_mode & (FMODE_WRITE | FMODE_READ)))
	{
		return -EINVAL;
	}
	state = card->states[1];
	if(state)
	{
		if ( (state->open_mode & FMODE_WRITE) & (file->f_mode & FMODE_WRITE) )
		{
			CS_DBGOUT(CS_RELEASE, 2, printk("cs46xx: cs_release() FMODE_WRITE\n") );
			dmabuf = &state->dmabuf;
			cs_clear_tail(state);
			drain_dac(state, file->f_flags & O_NONBLOCK);
			/* stop DMA state machine and free DMA buffers/channels */
			mutex_lock(&state->open_mutex);
			stop_dac(state);
			dealloc_dmabuf(state);
			state->card->free_pcm_channel(state->card, dmabuf->channel->num);
			free_page((unsigned long)state->dmabuf.pbuf);

			/* we're covered by the open_mutex */
			mutex_unlock(&state->open_mutex);
			state->card->states[state->virt] = NULL;
			state->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE);

			if( (tmp = cs461x_powerdown(card, CS_POWER_DAC, CS_FALSE )) )
			{
				CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO 
					"cs46xx: cs_release_mixdev() powerdown DAC failure (0x%x)\n",tmp) );
			}

			/* Now turn off external AMP if needed */
			state->card->amplifier_ctrl(state->card, -1);
			state->card->active_ctrl(state->card, -1);

			kfree(state);
		}
	}

	state = card->states[0];
	if(state)
	{
		if ( (state->open_mode & FMODE_READ) & (file->f_mode & FMODE_READ) )
		{
			CS_DBGOUT(CS_RELEASE, 2, printk("cs46xx: cs_release() FMODE_READ\n") );
			dmabuf = &state->dmabuf;
			mutex_lock(&state->open_mutex);
			stop_adc(state);
			dealloc_dmabuf(state);
			state->card->free_pcm_channel(state->card, dmabuf->channel->num);
			free_page((unsigned long)state->dmabuf.pbuf);

			/* we're covered by the open_mutex */
			mutex_unlock(&state->open_mutex);
			state->card->states[state->virt] = NULL;
			state->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE);

			if( (tmp = cs461x_powerdown(card, CS_POWER_ADC, CS_FALSE )) )
			{
				CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO 
					"cs46xx: cs_release_mixdev() powerdown ADC failure (0x%x)\n",tmp) );
			}

			/* Now turn off external AMP if needed */
			state->card->amplifier_ctrl(state->card, -1);
			state->card->active_ctrl(state->card, -1);

			kfree(state);
		}
	}

	CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 2, printk("cs46xx: cs_release()- 0\n") );
	return 0;
}

static void printpm(struct cs_card *s)
{
	CS_DBGOUT(CS_PM, 9, printk("pm struct:\n"));
	CS_DBGOUT(CS_PM, 9, printk("flags:0x%x u32CLKCR1_SAVE: 0%x u32SSPMValue: 0x%x\n",
		(unsigned)s->pm.flags,s->pm.u32CLKCR1_SAVE,s->pm.u32SSPMValue));
	CS_DBGOUT(CS_PM, 9, printk("u32PPLVCvalue: 0x%x u32PPRVCvalue: 0x%x\n",
		s->pm.u32PPLVCvalue,s->pm.u32PPRVCvalue));
	CS_DBGOUT(CS_PM, 9, printk("u32FMLVCvalue: 0x%x u32FMRVCvalue: 0x%x\n",
		s->pm.u32FMLVCvalue,s->pm.u32FMRVCvalue));
	CS_DBGOUT(CS_PM, 9, printk("u32GPIORvalue: 0x%x u32JSCTLvalue: 0x%x\n",
		s->pm.u32GPIORvalue,s->pm.u32JSCTLvalue));
	CS_DBGOUT(CS_PM, 9, printk("u32SSCR: 0x%x u32SRCSA: 0x%x\n",
		s->pm.u32SSCR,s->pm.u32SRCSA));
	CS_DBGOUT(CS_PM, 9, printk("u32DacASR: 0x%x u32AdcASR: 0x%x\n",
		s->pm.u32DacASR,s->pm.u32AdcASR));
	CS_DBGOUT(CS_PM, 9, printk("u32DacSR: 0x%x u32AdcSR: 0x%x\n",
		s->pm.u32DacSR,s->pm.u32AdcSR));
	CS_DBGOUT(CS_PM, 9, printk("u32MIDCR_Save: 0x%x\n",
		s->pm.u32MIDCR_Save));
	CS_DBGOUT(CS_PM, 9, printk("u32AC97_powerdown: 0x%x _general_purpose 0x%x\n",
		s->pm.u32AC97_powerdown,s->pm.u32AC97_general_purpose));
	CS_DBGOUT(CS_PM, 9, printk("u32AC97_master_volume: 0x%x\n",
		s->pm.u32AC97_master_volume));
	CS_DBGOUT(CS_PM, 9, printk("u32AC97_headphone_volume: 0x%x\n",
		s->pm.u32AC97_headphone_volume));
	CS_DBGOUT(CS_PM, 9, printk("u32AC97_master_volume_mono: 0x%x\n",
		s->pm.u32AC97_master_volume_mono));
	CS_DBGOUT(CS_PM, 9, printk("u32AC97_pcm_out_volume: 0x%x\n",
		s->pm.u32AC97_pcm_out_volume));
	CS_DBGOUT(CS_PM, 9, printk("dmabuf_swptr_play: 0x%x dmabuf_count_play: %d\n",
		s->pm.dmabuf_swptr_play,s->pm.dmabuf_count_play));
	CS_DBGOUT(CS_PM, 9, printk("dmabuf_swptr_capture: 0x%x dmabuf_count_capture: %d\n",
		s->pm.dmabuf_swptr_capture,s->pm.dmabuf_count_capture));

}

/****************************************************************************
*
*  Suspend - save the ac97 regs, mute the outputs and power down the part.  
*
****************************************************************************/
static void cs46xx_ac97_suspend(struct cs_card *card)
{
	int Count,i;
	struct ac97_codec *dev=card->ac97_codec[0];
	unsigned int tmp;

	CS_DBGOUT(CS_PM, 9, printk("cs46xx: cs46xx_ac97_suspend()+\n"));

	if(card->states[1])
	{
		stop_dac(card->states[1]);
		resync_dma_ptrs(card->states[1]);
	}
	if(card->states[0])
	{
		stop_adc(card->states[0]);
		resync_dma_ptrs(card->states[0]);
	}

	for(Count = 0x2, i=0; (Count <= CS46XX_AC97_HIGHESTREGTORESTORE)
			&& (i < CS46XX_AC97_NUMBER_RESTORE_REGS); 
		Count += 2, i++)
	{
		card->pm.ac97[i] = cs_ac97_get(dev, BA0_AC97_RESET + Count);
	}
/*
* Save the ac97 volume registers as well as the current powerdown state.
* Now, mute the all the outputs (master, headphone, and mono), as well
* as the PCM volume, in preparation for powering down the entire part.
	card->pm.u32AC97_master_volume = (u32)cs_ac97_get( dev, 
			(u8)BA0_AC97_MASTER_VOLUME); 
	card->pm.u32AC97_headphone_volume = (u32)cs_ac97_get(dev, 
			(u8)BA0_AC97_HEADPHONE_VOLUME); 
	card->pm.u32AC97_master_volume_mono = (u32)cs_ac97_get(dev, 
			(u8)BA0_AC97_MASTER_VOLUME_MONO); 
	card->pm.u32AC97_pcm_out_volume = (u32)cs_ac97_get(dev, 
			(u8)BA0_AC97_PCM_OUT_VOLUME);
*/ 
/*
* mute the outputs
*/
	cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME, 0x8000);
	cs_ac97_set(dev, (u8)BA0_AC97_HEADPHONE_VOLUME, 0x8000);
	cs_ac97_set(dev, (u8)BA0_AC97_MASTER_VOLUME_MONO, 0x8000);
	cs_ac97_set(dev, (u8)BA0_AC97_PCM_OUT_VOLUME, 0x8000);

/*
* save the registers that cause pops
*/
	card->pm.u32AC97_powerdown = (u32)cs_ac97_get(dev, (u8)AC97_POWER_CONTROL); 
	card->pm.u32AC97_general_purpose = (u32)cs_ac97_get(dev, (u8)BA0_AC97_GENERAL_PURPOSE); 
/*
* And power down everything on the AC97 codec.
* well, for now, only power down the DAC/ADC and MIXER VREFON components. 
* trouble with removing VREF.
*/
	if( (tmp = cs461x_powerdown(card, CS_POWER_DAC | CS_POWER_ADC |
			CS_POWER_MIXVON, CS_TRUE )) )
	{
		CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
			"cs46xx: cs46xx_ac97_suspend() failure (0x%x)\n",tmp) );
	}

	CS_DBGOUT(CS_PM, 9, printk("cs46xx: cs46xx_ac97_suspend()-\n"));
}

/****************************************************************************
*
*  Resume - power up the part and restore its registers..  
*
****************************************************************************/
static void cs46xx_ac97_resume(struct cs_card *card)
{
	int Count,i;
	struct ac97_codec *dev=card->ac97_codec[0];

	CS_DBGOUT(CS_PM, 9, printk("cs46xx: cs46xx_ac97_resume()+\n"));

/*
* First, we restore the state of the general purpose register.  This
* contains the mic select (mic1 or mic2) and if we restore this after
* we restore the mic volume/boost state and mic2 was selected at
* suspend time, we will end up with a brief period of time where mic1
* is selected with the volume/boost settings for mic2, causing
* acoustic feedback.  So we restore the general purpose register
* first, thereby getting the correct mic selected before we restore
* the mic volume/boost.
*/
	cs_ac97_set(dev, (u8)BA0_AC97_GENERAL_PURPOSE, 
		(u16)card->pm.u32AC97_general_purpose);
/*
* Now, while the outputs are still muted, restore the state of power
* on the AC97 part.
*/
	cs_ac97_set(dev, (u8)BA0_AC97_POWERDOWN, (u16)card->pm.u32AC97_powerdown);
	mdelay(5 * cs_laptop_wait);
/*
* Restore just the first set of registers, from register number
* 0x02 to the register number that ulHighestRegToRestore specifies.
*/
	for(	Count = 0x2, i=0; 
		(Count <= CS46XX_AC97_HIGHESTREGTORESTORE)
			&& (i < CS46XX_AC97_NUMBER_RESTORE_REGS); 
		Count += 2, i++)
	{
		cs_ac97_set(dev, (u8)(BA0_AC97_RESET + Count), (u16)card->pm.ac97[i]);
	}

	/* Check if we have to init the amplifier */
	if(card->amp_init)
		card->amp_init(card);
        
	CS_DBGOUT(CS_PM, 9, printk("cs46xx: cs46xx_ac97_resume()-\n"));
}


static int cs46xx_restart_part(struct cs_card *card)
{
	struct dmabuf *dmabuf;
	CS_DBGOUT(CS_PM | CS_FUNCTION, 4, 
		printk( "cs46xx: cs46xx_restart_part()+\n"));
	if(card->states[1])
	{
		dmabuf = &card->states[1]->dmabuf;
		dmabuf->ready = 0;
		resync_dma_ptrs(card->states[1]);
		cs_set_divisor(dmabuf);
		if(__prog_dmabuf(card->states[1]))
		{
			CS_DBGOUT(CS_PM | CS_ERROR, 1, 
				printk("cs46xx: cs46xx_restart_part()- (-1) prog_dmabuf() dac error\n"));
			return -1;
		}
		cs_set_dac_rate(card->states[1], dmabuf->rate);
	}
	if(card->states[0])
	{
		dmabuf = &card->states[0]->dmabuf;
		dmabuf->ready = 0;
		resync_dma_ptrs(card->states[0]);
		cs_set_divisor(dmabuf);
		if(__prog_dmabuf(card->states[0]))
		{
			CS_DBGOUT(CS_PM | CS_ERROR, 1, 
				printk("cs46xx: cs46xx_restart_part()- (-1) prog_dmabuf() adc error\n"));
			return -1;
		}
		cs_set_adc_rate(card->states[0], dmabuf->rate);
	}
	card->pm.flags |= CS46XX_PM_RESUMED;
	if(card->states[0])
		start_adc(card->states[0]);
	if(card->states[1])
		start_dac(card->states[1]);

	card->pm.flags |= CS46XX_PM_IDLE;
	card->pm.flags &= ~(CS46XX_PM_SUSPENDING | CS46XX_PM_SUSPENDED 
			| CS46XX_PM_RESUMING | CS46XX_PM_RESUMED);
	if(card->states[0])
		wake_up(&card->states[0]->dmabuf.wait);
	if(card->states[1])
		wake_up(&card->states[1]->dmabuf.wait);

	CS_DBGOUT(CS_PM | CS_FUNCTION, 4, 
		printk( "cs46xx: cs46xx_restart_part()-\n"));
	return 0;
}


static void cs461x_reset(struct cs_card *card);
static void cs461x_proc_stop(struct cs_card *card);
static int cs46xx_suspend(struct cs_card *card, pm_message_t state)
{
	unsigned int tmp;
	CS_DBGOUT(CS_PM | CS_FUNCTION, 4, 
		printk("cs46xx: cs46xx_suspend()+ flags=0x%x s=%p\n",
			(unsigned)card->pm.flags,card));
/*
* check the current state, only suspend if IDLE
*/
	if(!(card->pm.flags & CS46XX_PM_IDLE))
	{
		CS_DBGOUT(CS_PM | CS_ERROR, 2, 
			printk("cs46xx: cs46xx_suspend() unable to suspend, not IDLE\n"));
		return 1;
	}
	card->pm.flags &= ~CS46XX_PM_IDLE;
	card->pm.flags |= CS46XX_PM_SUSPENDING;

	card->active_ctrl(card,1);
	
	tmp = cs461x_peek(card, BA1_PFIE);
	tmp &= ~0x0000f03f;
	tmp |=  0x00000010;
	cs461x_poke(card, BA1_PFIE, tmp);	/* playback interrupt disable */

	tmp = cs461x_peek(card, BA1_CIE);
	tmp &= ~0x0000003f;
	tmp |=  0x00000011;
	cs461x_poke(card, BA1_CIE, tmp);	/* capture interrupt disable */

	/*
         *  Stop playback DMA.
	 */
	tmp = cs461x_peek(card, BA1_PCTL);
	cs461x_poke(card, BA1_PCTL, tmp & 0x0000ffff);

	/*
         *  Stop capture DMA.
	 */
	tmp = cs461x_peek(card, BA1_CCTL);
	cs461x_poke(card, BA1_CCTL, tmp & 0xffff0000);

	if(card->states[1])
	{
		card->pm.dmabuf_swptr_play = card->states[1]->dmabuf.swptr;
		card->pm.dmabuf_count_play = card->states[1]->dmabuf.count;
	}
	if(card->states[0])
	{
		card->pm.dmabuf_swptr_capture = card->states[0]->dmabuf.swptr;
		card->pm.dmabuf_count_capture = card->states[0]->dmabuf.count;
	}

	cs46xx_ac97_suspend(card);

	/*
         *  Reset the processor.
         */
	cs461x_reset(card);

	cs461x_proc_stop(card);

	/*
	 *  Power down the DAC and ADC.  For now leave the other areas on.
	 */
	cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, 0x0300);

	/*
	 *  Power down the PLL.
	 */
	cs461x_pokeBA0(card, BA0_CLKCR1, 0);

	/*
	 *  Turn off the Processor by turning off the software clock enable flag in 
	 *  the clock control register.
	 */
	tmp = cs461x_peekBA0(card, BA0_CLKCR1) & ~CLKCR1_SWCE;
	cs461x_pokeBA0(card, BA0_CLKCR1, tmp);

	card->active_ctrl(card,-1);

	card->pm.flags &= ~CS46XX_PM_SUSPENDING;
	card->pm.flags |= CS46XX_PM_SUSPENDED;

	printpm(card);

	CS_DBGOUT(CS_PM | CS_FUNCTION, 4, 
		printk("cs46xx: cs46xx_suspend()- flags=0x%x\n",
			(unsigned)card->pm.flags));
	return 0;
}

static int cs46xx_resume(struct cs_card *card)
{
	int i;

	CS_DBGOUT(CS_PM | CS_FUNCTION, 4, 
		printk( "cs46xx: cs46xx_resume()+ flags=0x%x\n",
			(unsigned)card->pm.flags));
	if(!(card->pm.flags & CS46XX_PM_SUSPENDED))
	{
		CS_DBGOUT(CS_PM | CS_ERROR, 2, 
			printk("cs46xx: cs46xx_resume() unable to resume, not SUSPENDED\n"));
		return 1;
	}
	card->pm.flags |= CS46XX_PM_RESUMING;
	card->pm.flags &= ~CS46XX_PM_SUSPENDED;
	printpm(card);
	card->active_ctrl(card, 1);

	for(i=0;i<5;i++)
	{
		if (cs_hardware_init(card) != 0)
		{
			CS_DBGOUT(CS_PM | CS_ERROR, 4, printk(
				"cs46xx: cs46xx_resume()- ERROR in cs_hardware_init()\n"));
			mdelay(10 * cs_laptop_wait);
			cs461x_reset(card);
			continue;
		}
		break;
	}
	if(i>=4)
	{
		CS_DBGOUT(CS_PM | CS_ERROR, 1, printk(
			"cs46xx: cs46xx_resume()- cs_hardware_init() failed, retried %d times.\n",i));
		return 0;
	}

	if(cs46xx_restart_part(card))
	{
		CS_DBGOUT(CS_PM | CS_ERROR, 4, printk(
			"cs46xx: cs46xx_resume(): cs46xx_restart_part() returned error\n"));
	}

	card->active_ctrl(card, -1);

	CS_DBGOUT(CS_PM | CS_FUNCTION, 4, printk("cs46xx: cs46xx_resume()- flags=0x%x\n",
		(unsigned)card->pm.flags));
	return 0;
}

static /*const*/ struct file_operations cs461x_fops = {
	CS_OWNER	CS_THIS_MODULE
	.llseek		= no_llseek,
	.read		= cs_read,
	.write		= cs_write,
	.poll		= cs_poll,
	.ioctl		= cs_ioctl,
	.mmap		= cs_mmap,
	.open		= cs_open,
	.release	= cs_release,
};

/* Write AC97 codec registers */


static u16 _cs_ac97_get(struct ac97_codec *dev, u8 reg)
{
	struct cs_card *card = dev->private_data;
	int count,loopcnt;
	unsigned int tmp;
	u16 ret;
	
	/*
	 *  1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
	 *  2. Write ACCDA = Command Data Register = 470h    for data to write to AC97 
	 *  3. Write ACCTL = Control Register = 460h for initiating the write
	 *  4. Read ACCTL = 460h, DCV should be reset by now and 460h = 17h
	 *  5. if DCV not cleared, break and return error
	 *  6. Read ACSTS = Status Register = 464h, check VSTS bit
	 */

	cs461x_peekBA0(card, BA0_ACSDA);

	/*
	 *  Setup the AC97 control registers on the CS461x to send the
	 *  appropriate command to the AC97 to perform the read.
	 *  ACCAD = Command Address Register = 46Ch
	 *  ACCDA = Command Data Register = 470h
	 *  ACCTL = Control Register = 460h
	 *  set DCV - will clear when process completed
	 *  set CRW - Read command
	 *  set VFRM - valid frame enabled
	 *  set ESYN - ASYNC generation enabled
	 *  set RSTN - ARST# inactive, AC97 codec not reset
	 */

	cs461x_pokeBA0(card, BA0_ACCAD, reg);
	cs461x_pokeBA0(card, BA0_ACCDA, 0);
	cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_DCV | ACCTL_CRW |
					     ACCTL_VFRM | ACCTL_ESYN |
					     ACCTL_RSTN);


	/*
	 *  Wait for the read to occur.
	 */
	if(!(card->pm.flags & CS46XX_PM_IDLE))
		loopcnt = 2000;
	else
		loopcnt = 500 * cs_laptop_wait;
 	loopcnt *= cs_laptop_wait;
	for (count = 0; count < loopcnt; count++) {
		/*
		 *  First, we want to wait for a short time.
	 	 */
		udelay(10 * cs_laptop_wait);
		/*
		 *  Now, check to see if the read has completed.
		 *  ACCTL = 460h, DCV should be reset by now and 460h = 17h
		 */
		if (!(cs461x_peekBA0(card, BA0_ACCTL) & ACCTL_DCV))
			break;
	}

	/*
	 *  Make sure the read completed.
	 */
	if (cs461x_peekBA0(card, BA0_ACCTL) & ACCTL_DCV) {
		CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
			"cs46xx: AC'97 read problem (ACCTL_DCV), reg = 0x%x returning 0xffff\n", reg));
		return 0xffff;
	}

	/*
	 *  Wait for the valid status bit to go active.
	 */

	if(!(card->pm.flags & CS46XX_PM_IDLE))
		loopcnt = 2000;
	else
		loopcnt = 1000;
 	loopcnt *= cs_laptop_wait;
	for (count = 0; count < loopcnt; count++) {
		/*
		 *  Read the AC97 status register.
		 *  ACSTS = Status Register = 464h
		 *  VSTS - Valid Status
		 */
		if (cs461x_peekBA0(card, BA0_ACSTS) & ACSTS_VSTS)
			break;
		udelay(10 * cs_laptop_wait);
	}
	
	/*
	 *  Make sure we got valid status.
	 */
	if (!( (tmp=cs461x_peekBA0(card, BA0_ACSTS)) & ACSTS_VSTS)) {
		CS_DBGOUT(CS_ERROR, 2, printk(KERN_WARNING 
			"cs46xx: AC'97 read problem (ACSTS_VSTS), reg = 0x%x val=0x%x 0xffff \n", 
				reg, tmp));
		return 0xffff;
	}

	/*
	 *  Read the data returned from the AC97 register.
	 *  ACSDA = Status Data Register = 474h
	 */
	CS_DBGOUT(CS_FUNCTION, 9, printk(KERN_INFO
		"cs46xx: cs_ac97_get() reg = 0x%x, val = 0x%x, BA0_ACCAD = 0x%x\n", 
			reg, cs461x_peekBA0(card, BA0_ACSDA),
			cs461x_peekBA0(card, BA0_ACCAD)));
	ret = cs461x_peekBA0(card, BA0_ACSDA);
	return ret;
}

static u16 cs_ac97_get(struct ac97_codec *dev, u8 reg)
{
	u16 ret;
	struct cs_card *card = dev->private_data;
	
	spin_lock(&card->ac97_lock);
	ret = _cs_ac97_get(dev, reg);
	spin_unlock(&card->ac97_lock);
	return ret;
}

static void cs_ac97_set(struct ac97_codec *dev, u8 reg, u16 val)
{
	struct cs_card *card = dev->private_data;
	int count;
	int val2 = 0;
	
	spin_lock(&card->ac97_lock);
	
	if(reg == AC97_CD_VOL)
	{
		val2 = _cs_ac97_get(dev, AC97_CD_VOL);
	}
	
	
	/*
	 *  1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
	 *  2. Write ACCDA = Command Data Register = 470h    for data to write to AC97
	 *  3. Write ACCTL = Control Register = 460h for initiating the write
	 *  4. Read ACCTL = 460h, DCV should be reset by now and 460h = 07h
	 *  5. if DCV not cleared, break and return error
	 */

	/*
	 *  Setup the AC97 control registers on the CS461x to send the
	 *  appropriate command to the AC97 to perform the read.
	 *  ACCAD = Command Address Register = 46Ch
	 *  ACCDA = Command Data Register = 470h
	 *  ACCTL = Control Register = 460h
	 *  set DCV - will clear when process completed
	 *  reset CRW - Write command
	 *  set VFRM - valid frame enabled
	 *  set ESYN - ASYNC generation enabled
	 *  set RSTN - ARST# inactive, AC97 codec not reset
         */
	cs461x_pokeBA0(card, BA0_ACCAD, reg);
	cs461x_pokeBA0(card, BA0_ACCDA, val);
	cs461x_peekBA0(card, BA0_ACCTL);
	cs461x_pokeBA0(card, BA0_ACCTL, 0 | ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
	cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_DCV | ACCTL_VFRM |
				             ACCTL_ESYN | ACCTL_RSTN);
	for (count = 0; count < 1000; count++) {
		/*
		 *  First, we want to wait for a short time.
		 */
		udelay(10 * cs_laptop_wait);
		/*
		 *  Now, check to see if the write has completed.
		 *  ACCTL = 460h, DCV should be reset by now and 460h = 07h
		 */
		if (!(cs461x_peekBA0(card, BA0_ACCTL) & ACCTL_DCV))
			break;
	}
	/*
	 *  Make sure the write completed.
	 */
	if (cs461x_peekBA0(card, BA0_ACCTL) & ACCTL_DCV)
	{
		CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
			"cs46xx: AC'97 write problem, reg = 0x%x, val = 0x%x\n", reg, val));
	}

	spin_unlock(&card->ac97_lock);

	/*
	 *	Adjust power if the mixer is selected/deselected according
	 *	to the CD.
	 *
	 *	IF the CD is a valid input source (mixer or direct) AND
	 *		the CD is not muted THEN power is needed
	 *
	 *	We do two things. When record select changes the input to
	 *	add/remove the CD we adjust the power count if the CD is
	 *	unmuted.
	 *
	 *	When the CD mute changes we adjust the power level if the
	 *	CD was a valid input.
	 *
	 *      We also check for CD volume != 0, as the CD mute isn't
	 *      normally tweaked from userspace.
	 */
	 
	/* CD mute change ? */
	
	if(reg==AC97_CD_VOL)
	{
		/* Mute bit change ? */
		if((val2^val)&0x8000 || ((val2 == 0x1f1f || val == 0x1f1f) && val2 != val))
		{
			/* This is a hack but its cleaner than the alternatives.
			   Right now card->ac97_codec[0] might be NULL as we are
			   still doing codec setup. This does an early assignment
			   to avoid the problem if it occurs */
			   
			if(card->ac97_codec[0]==NULL)
				card->ac97_codec[0]=dev;
				
			/* Mute on */
			if(val&0x8000 || val == 0x1f1f)
				card->amplifier_ctrl(card, -1);
			else /* Mute off power on */
			{
				if(card->amp_init)
					card->amp_init(card);
				card->amplifier_ctrl(card, 1);
			}
		}
	}
}


/* OSS /dev/mixer file operation methods */

static int cs_open_mixdev(struct inode *inode, struct file *file)
{
	int i=0;
	unsigned int minor = iminor(inode);
	struct cs_card *card=NULL;
	struct list_head *entry;
	unsigned int tmp;

	CS_DBGOUT(CS_FUNCTION | CS_OPEN, 4,
		  printk(KERN_INFO "cs46xx: cs_open_mixdev()+\n"));

	list_for_each(entry, &cs46xx_devs)
	{
		card = list_entry(entry, struct cs_card, list);
		for (i = 0; i < NR_AC97; i++)
			if (card->ac97_codec[i] != NULL &&
			    card->ac97_codec[i]->dev_mixer == minor)
				goto match;
	}
	if (!card)
	{
		CS_DBGOUT(CS_FUNCTION | CS_OPEN | CS_ERROR, 2,
			printk(KERN_INFO "cs46xx: cs46xx_open_mixdev()- -ENODEV\n"));
		return -ENODEV;
	}
 match:
	if(!card->ac97_codec[i])
		return -ENODEV;
	file->private_data = card->ac97_codec[i];

	card->active_ctrl(card,1);
	if(!CS_IN_USE(&card->mixer_use_cnt))
	{
		if( (tmp = cs46xx_powerup(card, CS_POWER_MIXVON )) )
		{
			CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
				"cs46xx: cs_open_mixdev() powerup failure (0x%x)\n",tmp) );
			return -EIO;
		}
	}
	card->amplifier_ctrl(card, 1);
	CS_INC_USE_COUNT(&card->mixer_use_cnt);
	CS_DBGOUT(CS_FUNCTION | CS_OPEN, 4,
		  printk(KERN_INFO "cs46xx: cs_open_mixdev()- 0\n"));
	return nonseekable_open(inode, file);
}

static int cs_release_mixdev(struct inode *inode, struct file *file)
{
	unsigned int minor = iminor(inode);
	struct cs_card *card=NULL;
	struct list_head *entry;
	int i;
	unsigned int tmp;

	CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 4,
		  printk(KERN_INFO "cs46xx: cs_release_mixdev()+\n"));
	list_for_each(entry, &cs46xx_devs)
	{
		card = list_entry(entry, struct cs_card, list);
		for (i = 0; i < NR_AC97; i++)
			if (card->ac97_codec[i] != NULL &&
			    card->ac97_codec[i]->dev_mixer == minor)
				goto match;
	}
	if (!card)
	{
		CS_DBGOUT(CS_FUNCTION | CS_OPEN | CS_ERROR, 2,
			printk(KERN_INFO "cs46xx: cs46xx_open_mixdev()- -ENODEV\n"));
		return -ENODEV;
	}
match:
	if(!CS_DEC_AND_TEST(&card->mixer_use_cnt))
	{
		CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 4,
			  printk(KERN_INFO "cs46xx: cs_release_mixdev()- no powerdown, usecnt>0\n"));
		card->active_ctrl(card, -1);
		card->amplifier_ctrl(card, -1);
		return 0;
	}
/*
* ok, no outstanding mixer opens, so powerdown.
*/
	if( (tmp = cs461x_powerdown(card, CS_POWER_MIXVON, CS_FALSE )) )
	{
		CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
			"cs46xx: cs_release_mixdev() powerdown MIXVON failure (0x%x)\n",tmp) );
		card->active_ctrl(card, -1);
		card->amplifier_ctrl(card, -1);
		return -EIO;
	}
	card->active_ctrl(card, -1);
	card->amplifier_ctrl(card, -1);
	CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 4,
		  printk(KERN_INFO "cs46xx: cs_release_mixdev()- 0\n"));
	return 0;
}

static int cs_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd,
				unsigned long arg)
{
	struct ac97_codec *codec = (struct ac97_codec *)file->private_data;
	struct cs_card *card=NULL;
	struct list_head *entry;
	unsigned long __user *p = (long __user *)arg;

#if CSDEBUG_INTERFACE
        int val;

	if( 	(cmd == SOUND_MIXER_CS_GETDBGMASK) || 
		(cmd == SOUND_MIXER_CS_SETDBGMASK) ||
		(cmd == SOUND_MIXER_CS_GETDBGLEVEL) ||
		(cmd == SOUND_MIXER_CS_SETDBGLEVEL) ||
		(cmd == SOUND_MIXER_CS_APM))
	{
	    switch(cmd)
	    {

		case SOUND_MIXER_CS_GETDBGMASK:
			return put_user(cs_debugmask, p);
		
		case SOUND_MIXER_CS_GETDBGLEVEL:
			return put_user(cs_debuglevel, p);

		case SOUND_MIXER_CS_SETDBGMASK:
			if (get_user(val, p))
				return -EFAULT;
			cs_debugmask = val;
			return 0;

		case SOUND_MIXER_CS_SETDBGLEVEL:
			if (get_user(val, p))
				return -EFAULT;
			cs_debuglevel = val;
			return 0;

		case SOUND_MIXER_CS_APM:
			if (get_user(val, p))
				return -EFAULT;
			if(val == CS_IOCTL_CMD_SUSPEND) 
			{
				list_for_each(entry, &cs46xx_devs)
				{
					card = list_entry(entry, struct cs_card, list);
					cs46xx_suspend(card, PMSG_ON);
				}

			}
			else if(val == CS_IOCTL_CMD_RESUME)
			{
				list_for_each(entry, &cs46xx_devs)
				{
					card = list_entry(entry, struct cs_card, list);
					cs46xx_resume(card);
				}
			}
			else
			{
				CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO
				    "cs46xx: mixer_ioctl(): invalid APM cmd (%d)\n",
					val));
			}
			return 0;

		default:
			CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO 
				"cs46xx: mixer_ioctl(): ERROR unknown debug cmd\n") );
			return 0;
	    }
	}
#endif
	return codec->mixer_ioctl(codec, cmd, arg);
}

static /*const*/ struct file_operations cs_mixer_fops = {
	CS_OWNER	CS_THIS_MODULE
	.llseek		= no_llseek,
	.ioctl		= cs_ioctl_mixdev,
	.open		= cs_open_mixdev,
	.release	= cs_release_mixdev,
};

/* AC97 codec initialisation. */
static int __init cs_ac97_init(struct cs_card *card)
{
	int num_ac97 = 0;
	int ready_2nd = 0;
	struct ac97_codec *codec;
	u16 eid;

	CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO 
		"cs46xx: cs_ac97_init()+\n") );

	for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
		if ((codec = ac97_alloc_codec()) == NULL)
			return -ENOMEM;

		/* initialize some basic codec information, other fields will be filled
		   in ac97_probe_codec */
		codec->private_data = card;
		codec->id = num_ac97;

		codec->codec_read = cs_ac97_get;
		codec->codec_write = cs_ac97_set;
	
		if (ac97_probe_codec(codec) == 0)
		{
			CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO 
				"cs46xx: cs_ac97_init()- codec number %d not found\n",
					num_ac97) );
			card->ac97_codec[num_ac97] = NULL;
			break;
		}
		CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO 
			"cs46xx: cs_ac97_init() found codec %d\n",num_ac97) );

		eid = cs_ac97_get(codec, AC97_EXTENDED_ID);
		
		if(eid==0xFFFF)
		{
			printk(KERN_WARNING "cs46xx: codec %d not present\n",num_ac97);
			ac97_release_codec(codec);
			break;
		}
		
		card->ac97_features = eid;
			
		if ((codec->dev_mixer = register_sound_mixer(&cs_mixer_fops, -1)) < 0) {
			printk(KERN_ERR "cs46xx: couldn't register mixer!\n");
			ac97_release_codec(codec);
			break;
		}
		card->ac97_codec[num_ac97] = codec;

		CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO 
			"cs46xx: cs_ac97_init() ac97_codec[%d] set to %p\n",
				(unsigned int)num_ac97,
				codec));
		/* if there is no secondary codec at all, don't probe any more */
		if (!ready_2nd)
		{
			num_ac97 += 1;
			break;
		}
	}
	CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO 
		"cs46xx: cs_ac97_init()- %d\n", (unsigned int)num_ac97));
	return num_ac97;
}

/*
 * load the static image into the DSP
 */
#include "cs461x_image.h"
static void cs461x_download_image(struct cs_card *card)
{
    unsigned i, j, temp1, temp2, offset, count;
    unsigned char __iomem *pBA1 = ioremap(card->ba1_addr, 0x40000);
    for( i=0; i < CLEAR__COUNT; i++)
    {
        offset = ClrStat[i].BA1__DestByteOffset;
        count  = ClrStat[i].BA1__SourceSize;
        for(  temp1 = offset; temp1<(offset+count); temp1+=4 )
              writel(0, pBA1+temp1);
    }

    for(i=0; i<FILL__COUNT; i++)
    {
        temp2 = FillStat[i].Offset;
        for(j=0; j<(FillStat[i].Size)/4; j++)
        {
            temp1 = (FillStat[i]).pFill[j];
            writel(temp1, pBA1+temp2+j*4);
        }
    }
    iounmap(pBA1);
}


/*
 *  Chip reset
 */

static void cs461x_reset(struct cs_card *card)
{
	int idx;

	/*
	 *  Write the reset bit of the SP control register.
	 */
	cs461x_poke(card, BA1_SPCR, SPCR_RSTSP);

	/*
	 *  Write the control register.
	 */
	cs461x_poke(card, BA1_SPCR, SPCR_DRQEN);

	/*
	 *  Clear the trap registers.
	 */
	for (idx = 0; idx < 8; idx++) {
		cs461x_poke(card, BA1_DREG, DREG_REGID_TRAP_SELECT + idx);
		cs461x_poke(card, BA1_TWPR, 0xFFFF);
	}
	cs461x_poke(card, BA1_DREG, 0);

	/*
	 *  Set the frame timer to reflect the number of cycles per frame.
	 */
	cs461x_poke(card, BA1_FRMT, 0xadf);
}

static void cs461x_clear_serial_FIFOs(struct cs_card *card, int type)
{
	int idx, loop, startfifo=0, endfifo=0, powerdown1 = 0;
	unsigned int tmp;

	/*
	 *  See if the devices are powered down.  If so, we must power them up first
	 *  or they will not respond.
	 */
	if (!((tmp = cs461x_peekBA0(card, BA0_CLKCR1)) & CLKCR1_SWCE)) {
		cs461x_pokeBA0(card, BA0_CLKCR1, tmp | CLKCR1_SWCE);
		powerdown1 = 1;
	}

	/*
	 *  We want to clear out the serial port FIFOs so we don't end up playing
	 *  whatever random garbage happens to be in them.  We fill the sample FIFOS
	 *  with zero (silence).
         */
	cs461x_pokeBA0(card, BA0_SERBWP, 0);

	/*
	* Check for which FIFO locations to clear, if we are currently
	* playing or capturing then we don't want to put in 128 bytes of
	* "noise".
	 */
	if(type & CS_TYPE_DAC)
	{
		startfifo = 128;
		endfifo = 256;
	}
	if(type & CS_TYPE_ADC)
	{
		startfifo = 0;
		if(!endfifo)
			endfifo = 128;
	}
	/*
	 *  Fill sample FIFO locations (256 locations total).
	 */
	for (idx = startfifo; idx < endfifo; idx++) {
		/*
		 *  Make sure the previous FIFO write operation has completed.
		 */
		for (loop = 0; loop < 5; loop++) {
			udelay(50);
			if (!(cs461x_peekBA0(card, BA0_SERBST) & SERBST_WBSY))
				break;
		}
		if (cs461x_peekBA0(card, BA0_SERBST) & SERBST_WBSY) {
			if (powerdown1)
				cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
		}
		/*
		 *  Write the serial port FIFO index.
		 */
		cs461x_pokeBA0(card, BA0_SERBAD, idx);
		/*
		 *  Tell the serial port to load the new value into the FIFO location.
		 */
		cs461x_pokeBA0(card, BA0_SERBCM, SERBCM_WRC);
	}
	/*
	 *  Now, if we powered up the devices, then power them back down again.
	 *  This is kinda ugly, but should never happen.
	 */
	if (powerdown1)
		cs461x_pokeBA0(card, BA0_CLKCR1, tmp);
}


static int cs461x_powerdown(struct cs_card *card, unsigned int type, int suspendflag)
{
	int count;
	unsigned int tmp=0,muted=0;

	CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO 
		"cs46xx: cs461x_powerdown()+ type=0x%x\n",type));
	if(!cs_powerdown && !suspendflag)
	{
		CS_DBGOUT(CS_FUNCTION, 8, printk(KERN_INFO 
			"cs46xx: cs461x_powerdown() DISABLED exiting\n"));
		return 0;
	}
	tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
	CS_DBGOUT(CS_FUNCTION, 8, printk(KERN_INFO 
		"cs46xx: cs461x_powerdown() powerdown reg=0x%x\n",tmp));
/*
* if powering down only the VREF, and not powering down the DAC/ADC,
* then do not power down the VREF, UNLESS both the DAC and ADC are not
* currently powered down.  If powering down DAC and ADC, then
* it is possible to power down the VREF (ON).
*/
	if (    ((type & CS_POWER_MIXVON) && 
		 (!(type & CS_POWER_ADC) || (!(type & CS_POWER_DAC))) )
	      && 
		((tmp & CS_AC97_POWER_CONTROL_ADC_ON) ||
		 (tmp & CS_AC97_POWER_CONTROL_DAC_ON) ) )
	{
		CS_DBGOUT(CS_FUNCTION, 8, printk(KERN_INFO 
			"cs46xx: cs461x_powerdown()- 0  unable to powerdown. tmp=0x%x\n",tmp));
		return 0;
	}
/*
* for now, always keep power to the mixer block.
* not sure why it's a problem but it seems to be if we power off.
*/
	type &= ~CS_POWER_MIXVON;
	type &= ~CS_POWER_MIXVOFF;

	/*
	 *  Power down indicated areas.
	 */
	if(type & CS_POWER_MIXVOFF)
	{

		CS_DBGOUT(CS_FUNCTION, 4, 
			printk(KERN_INFO "cs46xx: cs461x_powerdown()+ MIXVOFF\n"));
		/*
		 *  Power down the MIXER (VREF ON) on the AC97 card.  
		 */
		tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
		if (tmp & CS_AC97_POWER_CONTROL_MIXVOFF_ON)
		{
			if(!muted)
			{
				cs_mute(card, CS_TRUE);
				muted=1;
			}
			tmp |= CS_AC97_POWER_CONTROL_MIXVOFF;
			cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
			/*
			 *  Now, we wait until we sample a ready state.
			 */
			for (count = 0; count < 32; count++) {
				/*
				 *  First, lets wait a short while to let things settle out a
				 *  bit, and to prevent retrying the read too quickly.
				 */
				udelay(500);

				/*
				 *  Read the current state of the power control register.
				 */
				if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
					CS_AC97_POWER_CONTROL_MIXVOFF_ON))
					break;
			}
			
			/*
			 *  Check the status..
			 */
			if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
				CS_AC97_POWER_CONTROL_MIXVOFF_ON)
			{
				CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
					"cs46xx: powerdown MIXVOFF failed\n"));
				return 1;
			}
		}
	}
	if(type & CS_POWER_MIXVON)
	{

		CS_DBGOUT(CS_FUNCTION, 4, 
			printk(KERN_INFO "cs46xx: cs461x_powerdown()+ MIXVON\n"));
		/*
		 *  Power down the MIXER (VREF ON) on the AC97 card.  
		 */
		tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
		if (tmp & CS_AC97_POWER_CONTROL_MIXVON_ON)
		{
			if(!muted)
			{
				cs_mute(card, CS_TRUE);
				muted=1;
			}
			tmp |= CS_AC97_POWER_CONTROL_MIXVON;
			cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
			/*
			 *  Now, we wait until we sample a ready state.
			 */
			for (count = 0; count < 32; count++) {
				/*
				 *  First, lets wait a short while to let things settle out a
				 *  bit, and to prevent retrying the read too quickly.
				 */
				udelay(500);

				/*
				 *  Read the current state of the power control register.
				 */
				if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
					CS_AC97_POWER_CONTROL_MIXVON_ON))
					break;
			}
			
			/*
			 *  Check the status..
			 */
			if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
				CS_AC97_POWER_CONTROL_MIXVON_ON)
			{
				CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
					"cs46xx: powerdown MIXVON failed\n"));
				return 1;
			}
		}
	}
	if(type & CS_POWER_ADC)
	{
		/*
		 *  Power down the ADC on the AC97 card.  
		 */
		CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO "cs46xx: cs461x_powerdown()+ ADC\n"));
		tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
		if (tmp & CS_AC97_POWER_CONTROL_ADC_ON)
		{
			if(!muted)
			{
				cs_mute(card, CS_TRUE);
				muted=1;
			}
			tmp |= CS_AC97_POWER_CONTROL_ADC;
			cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );

			/*
			 *  Now, we wait until we sample a ready state.
			 */
			for (count = 0; count < 32; count++) {
				/*
				 *  First, lets wait a short while to let things settle out a
				 *  bit, and to prevent retrying the read too quickly.
				 */
				udelay(500);

				/*
				 *  Read the current state of the power control register.
				 */
				if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
					CS_AC97_POWER_CONTROL_ADC_ON))
					break;
			}

			/*
			 *  Check the status..
			 */
			if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
				CS_AC97_POWER_CONTROL_ADC_ON)
			{
				CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
					"cs46xx: powerdown ADC failed\n"));
				return 1;
			}
		}
	}
	if(type & CS_POWER_DAC)
	{
		/*
		 *  Power down the DAC on the AC97 card.  
		 */

		CS_DBGOUT(CS_FUNCTION, 4, 
			printk(KERN_INFO "cs46xx: cs461x_powerdown()+ DAC\n"));
		tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
		if (tmp & CS_AC97_POWER_CONTROL_DAC_ON)
		{
			if(!muted)
			{
				cs_mute(card, CS_TRUE);
				muted=1;
			}
			tmp |= CS_AC97_POWER_CONTROL_DAC;
			cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
			/*
			 *  Now, we wait until we sample a ready state.
			 */
			for (count = 0; count < 32; count++) {
				/*
				 *  First, lets wait a short while to let things settle out a
				 *  bit, and to prevent retrying the read too quickly.
				 */
				udelay(500);

				/*
				 *  Read the current state of the power control register.
				 */
				if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
					CS_AC97_POWER_CONTROL_DAC_ON))
					break;
			}
			
			/*
			 *  Check the status..
			 */
			if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
				CS_AC97_POWER_CONTROL_DAC_ON)
			{
				CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
					"cs46xx: powerdown DAC failed\n"));
				return 1;
			}
		}
	}
	tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
	if(muted)
		cs_mute(card, CS_FALSE);
	CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO 
		"cs46xx: cs461x_powerdown()- 0 tmp=0x%x\n",tmp));
	return 0;
}

static int cs46xx_powerup(struct cs_card *card, unsigned int type)
{
	int count;
	unsigned int tmp=0,muted=0;

	CS_DBGOUT(CS_FUNCTION, 8, printk(KERN_INFO 
		"cs46xx: cs46xx_powerup()+ type=0x%x\n",type));
	/*
	* check for VREF and powerup if need to.
	*/
	if(type & CS_POWER_MIXVON)
		type |= CS_POWER_MIXVOFF;
	if(type & (CS_POWER_DAC | CS_POWER_ADC))
		type |= CS_POWER_MIXVON | CS_POWER_MIXVOFF;

	/*
	 *  Power up indicated areas.
	 */
	if(type & CS_POWER_MIXVOFF)
	{

		CS_DBGOUT(CS_FUNCTION, 4, 
			printk(KERN_INFO "cs46xx: cs46xx_powerup()+ MIXVOFF\n"));
		/*
		 *  Power up the MIXER (VREF ON) on the AC97 card.  
		 */
		tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
		if (!(tmp & CS_AC97_POWER_CONTROL_MIXVOFF_ON))
		{
			if(!muted)
			{
				cs_mute(card, CS_TRUE);
				muted=1;
			}
			tmp &= ~CS_AC97_POWER_CONTROL_MIXVOFF;
			cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
			/*
			 *  Now, we wait until we sample a ready state.
			 */
			for (count = 0; count < 32; count++) {
				/*
				 *  First, lets wait a short while to let things settle out a
				 *  bit, and to prevent retrying the read too quickly.
				 */
				udelay(500);

				/*
				 *  Read the current state of the power control register.
				 */
				if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
					CS_AC97_POWER_CONTROL_MIXVOFF_ON)
					break;
			}
			
			/*
			 *  Check the status..
			 */
			if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
				CS_AC97_POWER_CONTROL_MIXVOFF_ON))
			{
				CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
					"cs46xx: powerup MIXVOFF failed\n"));
				return 1;
			}
		}
	}
	if(type & CS_POWER_MIXVON)
	{

		CS_DBGOUT(CS_FUNCTION, 4, 
			printk(KERN_INFO "cs46xx: cs46xx_powerup()+ MIXVON\n"));
		/*
		 *  Power up the MIXER (VREF ON) on the AC97 card.  
		 */
		tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
		if (!(tmp & CS_AC97_POWER_CONTROL_MIXVON_ON))
		{
			if(!muted)
			{
				cs_mute(card, CS_TRUE);
				muted=1;
			}
			tmp &= ~CS_AC97_POWER_CONTROL_MIXVON;
			cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
			/*
			 *  Now, we wait until we sample a ready state.
			 */
			for (count = 0; count < 32; count++) {
				/*
				 *  First, lets wait a short while to let things settle out a
				 *  bit, and to prevent retrying the read too quickly.
				 */
				udelay(500);

				/*
				 *  Read the current state of the power control register.
				 */
				if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
					CS_AC97_POWER_CONTROL_MIXVON_ON)
					break;
			}
			
			/*
			 *  Check the status..
			 */
			if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
				CS_AC97_POWER_CONTROL_MIXVON_ON))
			{
				CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
					"cs46xx: powerup MIXVON failed\n"));
				return 1;
			}
		}
	}
	if(type & CS_POWER_ADC)
	{
		/*
		 *  Power up the ADC on the AC97 card.  
		 */
		CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO "cs46xx: cs46xx_powerup()+ ADC\n"));
		tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
		if (!(tmp & CS_AC97_POWER_CONTROL_ADC_ON))
		{
			if(!muted)
			{
				cs_mute(card, CS_TRUE);
				muted=1;
			}
			tmp &= ~CS_AC97_POWER_CONTROL_ADC;
			cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );

			/*
			 *  Now, we wait until we sample a ready state.
			 */
			for (count = 0; count < 32; count++) {
				/*
				 *  First, lets wait a short while to let things settle out a
				 *  bit, and to prevent retrying the read too quickly.
				 */
				udelay(500);

				/*
				 *  Read the current state of the power control register.
				 */
				if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
					CS_AC97_POWER_CONTROL_ADC_ON)
					break;
			}

			/*
			 *  Check the status..
			 */
			if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
				CS_AC97_POWER_CONTROL_ADC_ON))
			{
				CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
					"cs46xx: powerup ADC failed\n"));
				return 1;
			}
		}
	}
	if(type & CS_POWER_DAC)
	{
		/*
		 *  Power up the DAC on the AC97 card.  
		 */

		CS_DBGOUT(CS_FUNCTION, 4, 
			printk(KERN_INFO "cs46xx: cs46xx_powerup()+ DAC\n"));
		tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
		if (!(tmp & CS_AC97_POWER_CONTROL_DAC_ON))
		{
			if(!muted)
			{
				cs_mute(card, CS_TRUE);
				muted=1;
			}
			tmp &= ~CS_AC97_POWER_CONTROL_DAC;
			cs_ac97_set(card->ac97_codec[0], AC97_POWER_CONTROL, tmp );
			/*
			 *  Now, we wait until we sample a ready state.
			 */
			for (count = 0; count < 32; count++) {
				/*
				 *  First, lets wait a short while to let things settle out a
				 *  bit, and to prevent retrying the read too quickly.
				 */
				udelay(500);

				/*
				 *  Read the current state of the power control register.
				 */
				if (cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
					CS_AC97_POWER_CONTROL_DAC_ON)
					break;
			}
			
			/*
			 *  Check the status..
			 */
			if (!(cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL) & 
				CS_AC97_POWER_CONTROL_DAC_ON))
			{
				CS_DBGOUT(CS_ERROR, 1, printk(KERN_WARNING 
					"cs46xx: powerup DAC failed\n"));
				return 1;
			}
		}
	}
	tmp = cs_ac97_get(card->ac97_codec[0], AC97_POWER_CONTROL);
	if(muted)
		cs_mute(card, CS_FALSE);
	CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO 
		"cs46xx: cs46xx_powerup()- 0 tmp=0x%x\n",tmp));
	return 0;
}


static void cs461x_proc_start(struct cs_card *card)
{
	int cnt;

	/*
	 *  Set the frame timer to reflect the number of cycles per frame.
	 */
	cs461x_poke(card, BA1_FRMT, 0xadf);
	/*
	 *  Turn on the run, run at frame, and DMA enable bits in the local copy of
	 *  the SP control register.
	 */
	cs461x_poke(card, BA1_SPCR, SPCR_RUN | SPCR_RUNFR | SPCR_DRQEN);
	/*
	 *  Wait until the run at frame bit resets itself in the SP control
	 *  register.
	 */
	for (cnt = 0; cnt < 25; cnt++) {
		udelay(50);
		if (!(cs461x_peek(card, BA1_SPCR) & SPCR_RUNFR))
			break;
	}

	if (cs461x_peek(card, BA1_SPCR) & SPCR_RUNFR)
		printk(KERN_WARNING "cs46xx: SPCR_RUNFR never reset\n");
}

static void cs461x_proc_stop(struct cs_card *card)
{
	/*
	 *  Turn off the run, run at frame, and DMA enable bits in the local copy of
	 *  the SP control register.
	 */
	cs461x_poke(card, BA1_SPCR, 0);
}

static int cs_hardware_init(struct cs_card *card)
{
	unsigned long end_time;
	unsigned int tmp,count;
	
	CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO 
		"cs46xx: cs_hardware_init()+\n") );
	/* 
	 *  First, blast the clock control register to zero so that the PLL starts
         *  out in a known state, and blast the master serial port control register
         *  to zero so that the serial ports also start out in a known state.
         */
        cs461x_pokeBA0(card, BA0_CLKCR1, 0);
        cs461x_pokeBA0(card, BA0_SERMC1, 0);

	/*
	 *  If we are in AC97 mode, then we must set the part to a host controlled
         *  AC-link.  Otherwise, we won't be able to bring up the link.
         */        
        cs461x_pokeBA0(card, BA0_SERACC, SERACC_HSP | SERACC_CODEC_TYPE_1_03);	/* 1.03 card */
        /* cs461x_pokeBA0(card, BA0_SERACC, SERACC_HSP | SERACC_CODEC_TYPE_2_0); */ /* 2.00 card */

        /*
         *  Drive the ARST# pin low for a minimum of 1uS (as defined in the AC97
         *  spec) and then drive it high.  This is done for non AC97 modes since
         *  there might be logic external to the CS461x that uses the ARST# line
         *  for a reset.
         */
        cs461x_pokeBA0(card, BA0_ACCTL, 1);
        udelay(50);
        cs461x_pokeBA0(card, BA0_ACCTL, 0);
        udelay(50);
        cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_RSTN);

	/*
	 *  The first thing we do here is to enable sync generation.  As soon
	 *  as we start receiving bit clock, we'll start producing the SYNC
	 *  signal.
	 */
	cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_ESYN | ACCTL_RSTN);

	/*
	 *  Now wait for a short while to allow the AC97 part to start
	 *  generating bit clock (so we don't try to start the PLL without an
	 *  input clock).
	 */
	mdelay(5 * cs_laptop_wait);		/* 1 should be enough ?? (and pigs might fly) */

	/*
	 *  Set the serial port timing configuration, so that
	 *  the clock control circuit gets its clock from the correct place.
	 */
	cs461x_pokeBA0(card, BA0_SERMC1, SERMC1_PTC_AC97);

	/*
	* The part seems to not be ready for a while after a resume.
	* so, if we are resuming, then wait for 700 mils.  Note that 600 mils
	* is not enough for some platforms! tested on an IBM Thinkpads and 
	* reference cards.
	*/
	if(!(card->pm.flags & CS46XX_PM_IDLE))
		mdelay(initdelay);
	/*
	 *  Write the selected clock control setup to the hardware.  Do not turn on
	 *  SWCE yet (if requested), so that the devices clocked by the output of
	 *  PLL are not clocked until the PLL is stable.
	 */
	cs461x_pokeBA0(card, BA0_PLLCC, PLLCC_LPF_1050_2780_KHZ | PLLCC_CDR_73_104_MHZ);
	cs461x_pokeBA0(card, BA0_PLLM, 0x3a);
	cs461x_pokeBA0(card, BA0_CLKCR2, CLKCR2_PDIVS_8);

	/*
	 *  Power up the PLL.
	 */
	cs461x_pokeBA0(card, BA0_CLKCR1, CLKCR1_PLLP);

	/*
         *  Wait until the PLL has stabilized.
	 */
	mdelay(5 * cs_laptop_wait);		/* Again 1 should be enough ?? */

	/*
	 *  Turn on clocking of the core so that we can setup the serial ports.
	 */
	tmp = cs461x_peekBA0(card, BA0_CLKCR1) | CLKCR1_SWCE;
	cs461x_pokeBA0(card, BA0_CLKCR1, tmp);

	/*
	 *  Fill the serial port FIFOs with silence.
	 */
	cs461x_clear_serial_FIFOs(card,CS_TYPE_DAC | CS_TYPE_ADC);

	/*
	 *  Set the serial port FIFO pointer to the first sample in the FIFO.
	 */
	/* cs461x_pokeBA0(card, BA0_SERBSP, 0); */

	/*
	 *  Write the serial port configuration to the part.  The master
	 *  enable bit is not set until all other values have been written.
	 */
	cs461x_pokeBA0(card, BA0_SERC1, SERC1_SO1F_AC97 | SERC1_SO1EN);
	cs461x_pokeBA0(card, BA0_SERC2, SERC2_SI1F_AC97 | SERC1_SO1EN);
	cs461x_pokeBA0(card, BA0_SERMC1, SERMC1_PTC_AC97 | SERMC1_MSPE);


	mdelay(5 * cs_laptop_wait);		/* Shouldnt be needed ?? */
	
/*
* If we are resuming under 2.2.x then we can not schedule a timeout.
* so, just spin the CPU.
*/
	if(card->pm.flags & CS46XX_PM_IDLE)
	{
	/*
	 * Wait for the card ready signal from the AC97 card.
	 */
		end_time = jiffies + 3 * (HZ >> 2);
		do {
		/*
		 *  Read the AC97 status register to see if we've seen a CODEC READY
		 *  signal from the AC97 card.
		 */
			if (cs461x_peekBA0(card, BA0_ACSTS) & ACSTS_CRDY)
				break;
			current->state = TASK_UNINTERRUPTIBLE;
			schedule_timeout(1);
		} while (time_before(jiffies, end_time));
	}
	else
	{
		for (count = 0; count < 100; count++) {
		// First, we want to wait for a short time.
			udelay(25 * cs_laptop_wait);

			if (cs461x_peekBA0(card, BA0_ACSTS) & ACSTS_CRDY)
				break;
		}
	}

	/*
	 *  Make sure CODEC is READY.
	 */
	if (!(cs461x_peekBA0(card, BA0_ACSTS) & ACSTS_CRDY)) {
		CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_WARNING  
			"cs46xx: create - never read card ready from AC'97\n"));
		CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_WARNING  
			"cs46xx: probably not a bug, try using the CS4232 driver,\n"));
		CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_WARNING  
			"cs46xx: or turn off any automatic Power Management support in the BIOS.\n"));
		return -EIO;
	}

	/*
	 *  Assert the vaid frame signal so that we can start sending commands
	 *  to the AC97 card.
	 */
	cs461x_pokeBA0(card, BA0_ACCTL, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);

	if(card->pm.flags & CS46XX_PM_IDLE)
	{
	/*
	 *  Wait until we've sampled input slots 3 and 4 as valid, meaning that
	 *  the card is pumping ADC data across the AC-link.
	 */
		end_time = jiffies + 3 * (HZ >> 2);
		do {
			/*
			 *  Read the input slot valid register and see if input slots 3 and
			 *  4 are valid yet.
			 */
			if ((cs461x_peekBA0(card, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) == (ACISV_ISV3 | ACISV_ISV4))
				break;
			current->state = TASK_UNINTERRUPTIBLE;
			schedule_timeout(1);
		} while (time_before(jiffies, end_time));
	}
	else
	{
		for (count = 0; count < 100; count++) {
		// First, we want to wait for a short time.
			udelay(25 * cs_laptop_wait);

			if ((cs461x_peekBA0(card, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) == (ACISV_ISV3 | ACISV_ISV4))
				break;
		}
	}
	/*
	 *  Make sure input slots 3 and 4 are valid.  If not, then return
	 *  an error.
	 */
	if ((cs461x_peekBA0(card, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) != (ACISV_ISV3 | ACISV_ISV4)) {
		printk(KERN_WARNING "cs46xx: create - never read ISV3 & ISV4 from AC'97\n");
		return -EIO;
	}

	/*
	 *  Now, assert valid frame and the slot 3 and 4 valid bits.  This will
	 *  commense the transfer of digital audio data to the AC97 card.
	 */
	cs461x_pokeBA0(card, BA0_ACOSV, ACOSV_SLV3 | ACOSV_SLV4);

	/*
	 *  Turn off the Processor by turning off the software clock enable flag in 
	 *  the clock control register.
	 */
	/* tmp = cs461x_peekBA0(card, BA0_CLKCR1) & ~CLKCR1_SWCE; */
	/* cs461x_pokeBA0(card, BA0_CLKCR1, tmp); */

	/*
         *  Reset the processor.
         */
	cs461x_reset(card);

	/*
         *  Download the image to the processor.
	 */
	
	cs461x_download_image(card);

	/*
         *  Stop playback DMA.
	 */
	tmp = cs461x_peek(card, BA1_PCTL);
	card->pctl = tmp & 0xffff0000;
	cs461x_poke(card, BA1_PCTL, tmp & 0x0000ffff);

	/*
         *  Stop capture DMA.
	 */
	tmp = cs461x_peek(card, BA1_CCTL);
	card->cctl = tmp & 0x0000ffff;
	cs461x_poke(card, BA1_CCTL, tmp & 0xffff0000);

	/* initialize AC97 codec and register /dev/mixer */
	if(card->pm.flags & CS46XX_PM_IDLE)
	{
		if (cs_ac97_init(card) <= 0)
		{
			CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
				"cs46xx: cs_ac97_init() failure\n") );
			return -EIO;
		}
	}
	else
	{
		cs46xx_ac97_resume(card);
	}
	
	cs461x_proc_start(card);

	/*
	 *  Enable interrupts on the part.
	 */
	cs461x_pokeBA0(card, BA0_HICR, HICR_IEV | HICR_CHGM);

	tmp = cs461x_peek(card, BA1_PFIE);
	tmp &= ~0x0000f03f;
	cs461x_poke(card, BA1_PFIE, tmp);	/* playback interrupt enable */

	tmp = cs461x_peek(card, BA1_CIE);
	tmp &= ~0x0000003f;
	tmp |=  0x00000001;
	cs461x_poke(card, BA1_CIE, tmp);	/* capture interrupt enable */	

	/*
	 *  If IDLE then Power down the part.  We will power components up 
	 *  when we need them.  
	 */
	if(card->pm.flags & CS46XX_PM_IDLE)
	{
		if(!cs_powerdown)
		{
			if( (tmp = cs46xx_powerup(card, CS_POWER_DAC | CS_POWER_ADC |
					CS_POWER_MIXVON )) )
			{
				CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
					"cs46xx: cs461x_powerup() failure (0x%x)\n",tmp) );
				return -EIO;
			}
		}
		else
		{
			if( (tmp = cs461x_powerdown(card, CS_POWER_DAC | CS_POWER_ADC |
					CS_POWER_MIXVON, CS_FALSE )) )
			{
				CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
					"cs46xx: cs461x_powerdown() failure (0x%x)\n",tmp) );
				return -EIO;
			}
		}
	}
	CS_DBGOUT(CS_FUNCTION | CS_INIT, 2, printk(KERN_INFO 
		"cs46xx: cs_hardware_init()- 0\n"));
	return 0;
}

/* install the driver, we do not allocate hardware channel nor DMA buffer now, they are defered 
   until "ACCESS" time (in prog_dmabuf called by open/read/write/ioctl/mmap) */
   
/*
 *	Card subid table
 */
 
struct cs_card_type
{
	u16 vendor;
	u16 id;
	char *name;
	void (*amp)(struct cs_card *, int);
	void (*amp_init)(struct cs_card *);
	void (*active)(struct cs_card *, int);
};

static struct cs_card_type cards[] = {
	{
		.vendor	= 0x1489,
		.id	= 0x7001,
		.name	= "Genius Soundmaker 128 value",
		.amp	= amp_none,
	},
	{
		.vendor	= 0x5053,
		.id	= 0x3357,
		.name	= "Voyetra",
		.amp	= amp_voyetra,
	},
	{
		.vendor	= 0x1071,
		.id	= 0x6003,
		.name	= "Mitac MI6020/21",
		.amp	= amp_voyetra,
	},
	{
		.vendor	= 0x14AF,
		.id	= 0x0050,
		.name	= "Hercules Game Theatre XP",
		.amp	= amp_hercules,
	},
	{
		.vendor	= 0x1681,
		.id	= 0x0050,
		.name	= "Hercules Game Theatre XP",
		.amp	= amp_hercules,
	},
	{
		.vendor	= 0x1681,
		.id	= 0x0051,
		.name	= "Hercules Game Theatre XP",
		.amp	= amp_hercules,
	},
	{
		.vendor	= 0x1681,
		.id	= 0x0052,
		.name	= "Hercules Game Theatre XP",
		.amp	= amp_hercules,
	},
	{
		.vendor	= 0x1681,
		.id	= 0x0053,
		.name	= "Hercules Game Theatre XP",
		.amp	= amp_hercules,
	},
	{
		.vendor	= 0x1681,
		.id	= 0x0054,
		.name	= "Hercules Game Theatre XP",
		.amp	= amp_hercules,
	},
	{
		.vendor	= 0x1681,
		.id	= 0xa010,
		.name	= "Hercules Fortissimo II",
		.amp	= amp_none,
	},
	/* Not sure if the 570 needs the clkrun hack */
	{
		.vendor	= PCI_VENDOR_ID_IBM,
		.id	= 0x0132,
		.name	= "Thinkpad 570",
		.amp	= amp_none,
		.active	= clkrun_hack,
	},
	{
		.vendor	= PCI_VENDOR_ID_IBM,
		.id	= 0x0153,
		.name	= "Thinkpad 600X/A20/T20",
		.amp	= amp_none,
		.active	= clkrun_hack,
	},
	{
		.vendor	= PCI_VENDOR_ID_IBM,
		.id	= 0x1010,
		.name	= "Thinkpad 600E (unsupported)",
	},
	{
		.name	= "Card without SSID set",
	},
	{ 0, },
};

MODULE_AUTHOR("Alan Cox <alan@redhat.com>, Jaroslav Kysela, <pcaudio@crystal.cirrus.com>");
MODULE_DESCRIPTION("Crystal SoundFusion Audio Support");
MODULE_LICENSE("GPL");


static const char cs46xx_banner[] = KERN_INFO "Crystal 4280/46xx + AC97 Audio, version " CS46XX_MAJOR_VERSION "." CS46XX_MINOR_VERSION "." CS46XX_ARCH ", " __TIME__ " " __DATE__ "\n";
static const char fndmsg[] = KERN_INFO "cs46xx: Found %d audio device(s).\n";

static int __devinit cs46xx_probe(struct pci_dev *pci_dev,
				  const struct pci_device_id *pciid)
{
	int i,j;
	u16 ss_card, ss_vendor;
	struct cs_card *card;
	dma_addr_t dma_mask;
	struct cs_card_type *cp = &cards[0];

	CS_DBGOUT(CS_FUNCTION | CS_INIT, 2,
		  printk(KERN_INFO "cs46xx: probe()+\n"));

	dma_mask = 0xffffffff;	/* this enables playback and recording */
	if (pci_enable_device(pci_dev)) {
		CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_ERR
			 "cs46xx: pci_enable_device() failed\n"));
		return -1;
	}
	if (!RSRCISMEMORYREGION(pci_dev, 0) ||
	    !RSRCISMEMORYREGION(pci_dev, 1)) {
		CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
			 "cs46xx: probe()- Memory region not assigned\n"));
		return -1;
	}
	if (pci_dev->irq == 0) {
		CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
			 "cs46xx: probe() IRQ not assigned\n"));
		return -1;
	}
	if (!pci_dma_supported(pci_dev, 0xffffffff)) {
		CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
		      "cs46xx: probe() architecture does not support 32bit PCI busmaster DMA\n"));
		return -1;
	}
	pci_read_config_word(pci_dev, PCI_SUBSYSTEM_VENDOR_ID, &ss_vendor);
	pci_read_config_word(pci_dev, PCI_SUBSYSTEM_ID, &ss_card);

	if ((card = kmalloc(sizeof(struct cs_card), GFP_KERNEL)) == NULL) {
		printk(KERN_ERR "cs46xx: out of memory\n");
		return -ENOMEM;
	}
	memset(card, 0, sizeof(*card));
	card->ba0_addr = RSRCADDRESS(pci_dev, 0);
	card->ba1_addr = RSRCADDRESS(pci_dev, 1);
	card->pci_dev = pci_dev;
	card->irq = pci_dev->irq;
	card->magic = CS_CARD_MAGIC;
	spin_lock_init(&card->lock);
	spin_lock_init(&card->ac97_lock);

	pci_set_master(pci_dev);

	printk(cs46xx_banner);
	printk(KERN_INFO "cs46xx: Card found at 0x%08lx and 0x%08lx, IRQ %d\n",
	       card->ba0_addr, card->ba1_addr, card->irq);

	card->alloc_pcm_channel = cs_alloc_pcm_channel;
	card->alloc_rec_pcm_channel = cs_alloc_rec_pcm_channel;
	card->free_pcm_channel = cs_free_pcm_channel;
	card->amplifier_ctrl = amp_none;
	card->active_ctrl = amp_none;

	while (cp->name)
	{
		if(cp->vendor == ss_vendor && cp->id == ss_card)
		{
			card->amplifier_ctrl = cp->amp;
			if(cp->active)
				card->active_ctrl = cp->active;
			if(cp->amp_init)
				card->amp_init = cp->amp_init;
			break;
		}
		cp++;
	}
	if (cp->name==NULL)
	{
		printk(KERN_INFO "cs46xx: Unknown card (%04X:%04X) at 0x%08lx/0x%08lx, IRQ %d\n",
			ss_vendor, ss_card, card->ba0_addr, card->ba1_addr,  card->irq);
	}
	else
	{
		printk(KERN_INFO "cs46xx: %s (%04X:%04X) at 0x%08lx/0x%08lx, IRQ %d\n",
			cp->name, ss_vendor, ss_card, card->ba0_addr, card->ba1_addr, card->irq);
	}
	
	if (card->amplifier_ctrl==NULL)
	{
		card->amplifier_ctrl = amp_none;
		card->active_ctrl = clkrun_hack;
	}		

	if (external_amp == 1)
	{
		printk(KERN_INFO "cs46xx: Crystal EAPD support forced on.\n");
		card->amplifier_ctrl = amp_voyetra;
	}

	if (thinkpad == 1)
	{
		printk(KERN_INFO "cs46xx: Activating CLKRUN hack for Thinkpad.\n");
		card->active_ctrl = clkrun_hack;
	}
/*
* The thinkpads don't work well without runtime updating on their kernel 
* delay values (or any laptop with variable CPU speeds really).
* so, just to be safe set the init delay to 2100.  Eliminates
* failures on T21 Thinkpads.  remove this code when the udelay
* and mdelay kernel code is replaced by a pm timer, or the delays
* work well for battery and/or AC power both.
*/
	if(card->active_ctrl == clkrun_hack)
	{
		initdelay = 2100;
		cs_laptop_wait = 5;
	}
	if((card->active_ctrl == clkrun_hack) && !(powerdown == 1))
	{
/*
* for some currently unknown reason, powering down the DAC and ADC component
* blocks on thinkpads causes some funky behavior... distoorrrtion and ac97 
* codec access problems.  probably the serial clock becomes unsynced. 
* added code to sync the chips back up, but only helped about 70% the time.
*/
		cs_powerdown = 0;
	}
	if(powerdown == 0)
		cs_powerdown = 0;
	card->active_ctrl(card, 1);

	/* claim our iospace and irq */
	
	card->ba0 = ioremap_nocache(card->ba0_addr, CS461X_BA0_SIZE);
	card->ba1.name.data0 = ioremap_nocache(card->ba1_addr + BA1_SP_DMEM0, CS461X_BA1_DATA0_SIZE);
	card->ba1.name.data1 = ioremap_nocache(card->ba1_addr + BA1_SP_DMEM1, CS461X_BA1_DATA1_SIZE);
	card->ba1.name.pmem = ioremap_nocache(card->ba1_addr + BA1_SP_PMEM, CS461X_BA1_PRG_SIZE);
	card->ba1.name.reg = ioremap_nocache(card->ba1_addr + BA1_SP_REG, CS461X_BA1_REG_SIZE);
	
	CS_DBGOUT(CS_INIT, 4, printk(KERN_INFO 
		"cs46xx: card=%p card->ba0=%p\n",card,card->ba0) );
	CS_DBGOUT(CS_INIT, 4, printk(KERN_INFO 
		"cs46xx: card->ba1=%p %p %p %p\n",
			card->ba1.name.data0,
			card->ba1.name.data1,
			card->ba1.name.pmem,
			card->ba1.name.reg) );

	if(card->ba0 == 0 || card->ba1.name.data0 == 0 ||
		card->ba1.name.data1 == 0 || card->ba1.name.pmem == 0 ||
		card->ba1.name.reg == 0)
		goto fail2;
		
	if (request_irq(card->irq, &cs_interrupt, SA_SHIRQ, "cs46xx", card)) {
		printk(KERN_ERR "cs46xx: unable to allocate irq %d\n", card->irq);
		goto fail2;
	}
	/* register /dev/dsp */
	if ((card->dev_audio = register_sound_dsp(&cs461x_fops, -1)) < 0) {
		printk(KERN_ERR "cs46xx: unable to register dsp\n");
		goto fail;
	}

        /* register /dev/midi */
        if((card->dev_midi = register_sound_midi(&cs_midi_fops, -1)) < 0)
                printk(KERN_ERR "cs46xx: unable to register midi\n");
                
	card->pm.flags |= CS46XX_PM_IDLE;
	for(i=0;i<5;i++)
	{
		if (cs_hardware_init(card) != 0)
		{
			CS_DBGOUT(CS_ERROR, 4, printk(
				"cs46xx: ERROR in cs_hardware_init()... retrying\n"));
			for (j = 0; j < NR_AC97; j++)
				if (card->ac97_codec[j] != NULL) {
					unregister_sound_mixer(card->ac97_codec[j]->dev_mixer);
					ac97_release_codec(card->ac97_codec[j]);
				}
			mdelay(10 * cs_laptop_wait);
			continue;
		}
		break;
	}
	if(i>=4)
	{
		CS_DBGOUT(CS_PM | CS_ERROR, 1, printk(
			"cs46xx: cs46xx_probe()- cs_hardware_init() failed, retried %d times.\n",i));
                unregister_sound_dsp(card->dev_audio);
                if(card->dev_midi)
                        unregister_sound_midi(card->dev_midi);
                goto fail;
	}

        init_waitqueue_head(&card->midi.open_wait);
        mutex_init(&card->midi.open_mutex);
        init_waitqueue_head(&card->midi.iwait);
        init_waitqueue_head(&card->midi.owait);
        cs461x_pokeBA0(card, BA0_MIDCR, MIDCR_MRST);   
        cs461x_pokeBA0(card, BA0_MIDCR, 0);   

	/* 
	* Check if we have to init the amplifier, but probably already done
	* since the CD logic in the ac97 init code will turn on the ext amp.
	*/
	if(cp->amp_init)
		cp->amp_init(card);
        card->active_ctrl(card, -1);

	PCI_SET_DRIVER_DATA(pci_dev, card);
	PCI_SET_DMA_MASK(pci_dev, dma_mask);
	list_add(&card->list, &cs46xx_devs);

	CS_DBGOUT(CS_PM, 9, printk(KERN_INFO "cs46xx: pm.flags=0x%x card=%p\n",
		(unsigned)card->pm.flags,card));

	CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
		"cs46xx: probe()- device allocated successfully\n"));
        return 0;

fail:
	free_irq(card->irq, card);
fail2:
	if(card->ba0)
		iounmap(card->ba0);
	if(card->ba1.name.data0)
		iounmap(card->ba1.name.data0);
	if(card->ba1.name.data1)
		iounmap(card->ba1.name.data1);
	if(card->ba1.name.pmem)
		iounmap(card->ba1.name.pmem);
	if(card->ba1.name.reg)
		iounmap(card->ba1.name.reg);
	kfree(card);
	CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_INFO
		"cs46xx: probe()- no device allocated\n"));
	return -ENODEV;
} // probe_cs46xx

// --------------------------------------------------------------------- 

static void __devexit cs46xx_remove(struct pci_dev *pci_dev)
{
	struct cs_card *card = PCI_GET_DRIVER_DATA(pci_dev);
	int i;
	unsigned int tmp;
	
	CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
		 "cs46xx: cs46xx_remove()+\n"));

	card->active_ctrl(card,1);
	
	tmp = cs461x_peek(card, BA1_PFIE);
	tmp &= ~0x0000f03f;
	tmp |=  0x00000010;
	cs461x_poke(card, BA1_PFIE, tmp);	/* playback interrupt disable */

	tmp = cs461x_peek(card, BA1_CIE);
	tmp &= ~0x0000003f;
	tmp |=  0x00000011;
	cs461x_poke(card, BA1_CIE, tmp);	/* capture interrupt disable */

	/*
         *  Stop playback DMA.
	 */
	tmp = cs461x_peek(card, BA1_PCTL);
	cs461x_poke(card, BA1_PCTL, tmp & 0x0000ffff);

	/*
         *  Stop capture DMA.
	 */
	tmp = cs461x_peek(card, BA1_CCTL);
	cs461x_poke(card, BA1_CCTL, tmp & 0xffff0000);

	/*
         *  Reset the processor.
         */
	cs461x_reset(card);

	cs461x_proc_stop(card);

	/*
	 *  Power down the DAC and ADC.  We will power them up (if) when we need
	 *  them.
	 */
	if( (tmp = cs461x_powerdown(card, CS_POWER_DAC | CS_POWER_ADC |
			CS_POWER_MIXVON, CS_TRUE )) )
	{
		CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_INFO 
			"cs46xx: cs461x_powerdown() failure (0x%x)\n",tmp) );
	}

	/*
	 *  Power down the PLL.
	 */
	cs461x_pokeBA0(card, BA0_CLKCR1, 0);

	/*
	 *  Turn off the Processor by turning off the software clock enable flag in 
	 *  the clock control register.
	 */
	tmp = cs461x_peekBA0(card, BA0_CLKCR1) & ~CLKCR1_SWCE;
	cs461x_pokeBA0(card, BA0_CLKCR1, tmp);

	card->active_ctrl(card,-1);

	/* free hardware resources */
	free_irq(card->irq, card);
	iounmap(card->ba0);
	iounmap(card->ba1.name.data0);
	iounmap(card->ba1.name.data1);
	iounmap(card->ba1.name.pmem);
	iounmap(card->ba1.name.reg);
	
	/* unregister audio devices */
	for (i = 0; i < NR_AC97; i++)
		if (card->ac97_codec[i] != NULL) {
			unregister_sound_mixer(card->ac97_codec[i]->dev_mixer);
			ac97_release_codec(card->ac97_codec[i]);
		}
	unregister_sound_dsp(card->dev_audio);
        if(card->dev_midi)
                unregister_sound_midi(card->dev_midi);
	list_del(&card->list);
	kfree(card);
	PCI_SET_DRIVER_DATA(pci_dev,NULL);

	CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
		 "cs46xx: cs46xx_remove()-: remove successful\n"));
}

enum {
	CS46XX_4610 = 0,
	CS46XX_4612,  	/* same as 4630 */
	CS46XX_4615,  	/* same as 4624 */
};

static struct pci_device_id cs46xx_pci_tbl[] = {
	{
		.vendor	     = PCI_VENDOR_ID_CIRRUS,
		.device	     = PCI_DEVICE_ID_CIRRUS_4610,
		.subvendor   = PCI_ANY_ID,
		.subdevice   = PCI_ANY_ID,
		.driver_data = CS46XX_4610,
	},
	{
		.vendor	     = PCI_VENDOR_ID_CIRRUS,
		.device	     = PCI_DEVICE_ID_CIRRUS_4612,
		.subvendor   = PCI_ANY_ID,
		.subdevice   = PCI_ANY_ID,
		.driver_data = CS46XX_4612,
	},
	{
		.vendor	     = PCI_VENDOR_ID_CIRRUS,
		.device	     = PCI_DEVICE_ID_CIRRUS_4615,
		.subvendor   = PCI_ANY_ID,
		.subdevice   = PCI_ANY_ID,
		.driver_data = CS46XX_4615,
	},
	{ 0, },
};

MODULE_DEVICE_TABLE(pci, cs46xx_pci_tbl);

static struct pci_driver cs46xx_pci_driver = {
	.name	  = "cs46xx",
	.id_table = cs46xx_pci_tbl,
	.probe	  = cs46xx_probe,
	.remove	  = __devexit_p(cs46xx_remove),
	.suspend  = CS46XX_SUSPEND_TBL,
	.resume	  = CS46XX_RESUME_TBL,
};

static int __init cs46xx_init_module(void)
{
	int rtn = 0;
	CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO 
		"cs46xx: cs46xx_init_module()+ \n"));
	rtn = pci_register_driver(&cs46xx_pci_driver);

	if(rtn == -ENODEV)
	{
		CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk( 
			"cs46xx: Unable to detect valid cs46xx device\n"));
	}

	CS_DBGOUT(CS_INIT | CS_FUNCTION, 2,
		  printk(KERN_INFO "cs46xx: cs46xx_init_module()- (%d)\n",rtn));
	return rtn;
}

static void __exit cs46xx_cleanup_module(void)
{
	pci_unregister_driver(&cs46xx_pci_driver);
	CS_DBGOUT(CS_INIT | CS_FUNCTION, 2,
		  printk(KERN_INFO "cs46xx: cleanup_cs46xx() finished\n"));
}

module_init(cs46xx_init_module);
module_exit(cs46xx_cleanup_module);

#if CS46XX_ACPI_SUPPORT
static int cs46xx_suspend_tbl(struct pci_dev *pcidev, pm_message_t state)
{
	struct cs_card *s = PCI_GET_DRIVER_DATA(pcidev);
	CS_DBGOUT(CS_PM | CS_FUNCTION, 2, 
		printk(KERN_INFO "cs46xx: cs46xx_suspend_tbl request\n"));
	cs46xx_suspend(s, state);
	return 0;
}

static int cs46xx_resume_tbl(struct pci_dev *pcidev)
{
	struct cs_card *s = PCI_GET_DRIVER_DATA(pcidev);
	CS_DBGOUT(CS_PM | CS_FUNCTION, 2, 
		printk(KERN_INFO "cs46xx: cs46xx_resume_tbl request\n"));
	cs46xx_resume(s);
	return 0;
}
#endif