summaryrefslogtreecommitdiff
path: root/drivers/dahdi/wctdm24xxp/base.c
blob: 4c539b358fd1fb338c269075c6bd8c7a6fae589a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
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
4174
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
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
/*
 * Wildcard TDM2400P TDM FXS/FXO Interface Driver for DAHDI Telephony interface
 *
 * Written by Mark Spencer <markster@digium.com>
 * Support for TDM800P and VPM150M by Matthew Fredrickson <creslin@digium.com>
 *
 * Support for Hx8 by Andrew Kohlsmith <akohlsmith@mixdown.ca> and Matthew
 * Fredrickson <creslin@digium.com>
 *
 * Copyright (C) 2005 - 2012 Digium, Inc.
 * All rights reserved.
 *
 * Sections for QRV cards written by Jim Dixon <jim@lambdatel.com>
 * Copyright (C) 2006, Jim Dixon and QRV Communications
 * All rights reserved.
 *
 */

/*
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2 as published by the
 * Free Software Foundation. See the LICENSE file included with
 * this program for more details.
 */

/* For QRV DRI cards, gain is signed short, expressed in hundredths of
db (in reference to 1v Peak @ 1000Hz) , as follows:

Rx Gain: -11.99 to 15.52 db
Tx Gain - No Pre-Emphasis: -35.99 to 12.00 db
Tx Gain - W/Pre-Emphasis: -23.99 to 0.00 db
*/

#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/workqueue.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/firmware.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
#include <linux/semaphore.h>
#else
#include <asm/semaphore.h>
#endif
#include <linux/crc32.h>

#include <stdbool.h>

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
/* Define this if you would like to load the modules in parallel.  While this
 * can speed up loads when multiple cards handled by this driver are installed,
 * it also makes it impossible to abort module loads with ctrl-c */
#undef USE_ASYNC_INIT
#include <linux/async.h>
#else
#undef USE_ASYNC_INIT
#endif

#include <dahdi/kernel.h>
#include <dahdi/wctdm_user.h>

#include "proslic.h"

#include "wctdm24xxp.h"
#include "xhfc.h"

#include "adt_lec.h"

#include "voicebus/GpakCust.h"
#include "voicebus/GpakApi.h"

#if VOICEBUS_SFRAME_SIZE != SFRAME_SIZE
#error SFRAME_SIZE must match the VOICEBUS_SFRAME_SIZE
#endif

/*
  Experimental max loop current limit for the proslic
  Loop current limit is from 20 mA to 41 mA in steps of 3
  (according to datasheet)
  So set the value below to:
  0x00 : 20mA (default)
  0x01 : 23mA
  0x02 : 26mA
  0x03 : 29mA
  0x04 : 32mA
  0x05 : 35mA
  0x06 : 37mA
  0x07 : 41mA
*/
static int loopcurrent = 20;

/* Following define is a logical exclusive OR to determine if the polarity of an fxs line is to be reversed.
 * 	The items taken into account are:
 * 	overall polarity reversal for the module,
 * 	polarity reversal for the port,
 * 	and the state of the line reversal MWI indicator
 */
#define POLARITY_XOR(fxs) \
	((reversepolarity != 0) ^ ((fxs)->reversepolarity != 0) ^ \
	((fxs)->vmwi_linereverse != 0))

static int reversepolarity = 0;

static alpha  indirect_regs[] =
{
{0,255,"DTMF_ROW_0_PEAK",0x55C2},
{1,255,"DTMF_ROW_1_PEAK",0x51E6},
{2,255,"DTMF_ROW2_PEAK",0x4B85},
{3,255,"DTMF_ROW3_PEAK",0x4937},
{4,255,"DTMF_COL1_PEAK",0x3333},
{5,255,"DTMF_FWD_TWIST",0x0202},
{6,255,"DTMF_RVS_TWIST",0x0202},
{7,255,"DTMF_ROW_RATIO_TRES",0x0198},
{8,255,"DTMF_COL_RATIO_TRES",0x0198},
{9,255,"DTMF_ROW_2ND_ARM",0x0611},
{10,255,"DTMF_COL_2ND_ARM",0x0202},
{11,255,"DTMF_PWR_MIN_TRES",0x00E5},
{12,255,"DTMF_OT_LIM_TRES",0x0A1C},
{13,0,"OSC1_COEF",0x7B30},
{14,1,"OSC1X",0x0063},
{15,2,"OSC1Y",0x0000},
{16,3,"OSC2_COEF",0x7870},
{17,4,"OSC2X",0x007D},
{18,5,"OSC2Y",0x0000},
{19,6,"RING_V_OFF",0x0000},
{20,7,"RING_OSC",0x7EF0},
{21,8,"RING_X",0x0160},
{22,9,"RING_Y",0x0000},
{23,255,"PULSE_ENVEL",0x2000},
{24,255,"PULSE_X",0x2000},
{25,255,"PULSE_Y",0x0000},
//{26,13,"RECV_DIGITAL_GAIN",0x4000},	// playback volume set lower
{26,13,"RECV_DIGITAL_GAIN",0x2000},	// playback volume set lower
{27,14,"XMIT_DIGITAL_GAIN",0x4000},
//{27,14,"XMIT_DIGITAL_GAIN",0x2000},
{28,15,"LOOP_CLOSE_TRES",0x1000},
{29,16,"RING_TRIP_TRES",0x3600},
{30,17,"COMMON_MIN_TRES",0x1000},
{31,18,"COMMON_MAX_TRES",0x0200},
{32,19,"PWR_ALARM_Q1Q2",0x07C0},
{33,20,"PWR_ALARM_Q3Q4", 0x4C00 /* 0x2600 */},
{34,21,"PWR_ALARM_Q5Q6",0x1B80},
{35,22,"LOOP_CLOSURE_FILTER",0x8000},
{36,23,"RING_TRIP_FILTER",0x0320},
{37,24,"TERM_LP_POLE_Q1Q2",0x008C},
{38,25,"TERM_LP_POLE_Q3Q4",0x0100},
{39,26,"TERM_LP_POLE_Q5Q6",0x0010},
{40,27,"CM_BIAS_RINGING",0x0C00},
{41,64,"DCDC_MIN_V",0x0C00},
{42,255,"DCDC_XTRA",0x1000},
{43,66,"LOOP_CLOSE_TRES_LOW",0x1000},
};

/* names of HWEC modules */
static const char *vpmadt032_name = "VPMADT032";
static const char *vpmoct_name = "VPMOCT032";

/* Undefine to enable Power alarm / Transistor debug -- note: do not
   enable for normal operation! */
/* #define PAQ_DEBUG */

#define DEBUG_CARD (1 << 0)
#define DEBUG_ECHOCAN (1 << 1)

#include "fxo_modes.h"

struct wctdm_desc {
	const char *name;
	const int flags;
	const int ports;
};

static const struct wctdm_desc wctdm2400 = { "Wildcard TDM2400P", 0, 24 };
static const struct wctdm_desc wctdm800 = { "Wildcard TDM800P", 0, 8 };
static const struct wctdm_desc wctdm410 = { "Wildcard TDM410P", 0, 4 };
static const struct wctdm_desc wcaex2400 = { "Wildcard AEX2400", FLAG_EXPRESS, 24 };
static const struct wctdm_desc wcaex800 = { "Wildcard AEX800", FLAG_EXPRESS, 8 };
static const struct wctdm_desc wcaex410 = { "Wildcard AEX410", FLAG_EXPRESS, 4 };
static const struct wctdm_desc wcha80000 = { "HA8-0000", 0, 8 };
static const struct wctdm_desc wchb80000 = { "HB8-0000", FLAG_EXPRESS, 8 };

static inline bool is_pcie(const struct wctdm *wc)
{
	return (wc->desc->flags & FLAG_EXPRESS) > 0;
}

/**
 * Returns true if the card is one of the Hybrid Digital Analog Cards.
 */
static inline bool is_hx8(const struct wctdm *wc)
{
	return (&wcha80000 == wc->desc) || (&wchb80000 == wc->desc);
}

static inline struct dahdi_chan *
get_dahdi_chan(const struct wctdm *wc, struct wctdm_module *const mod)
{
	return wc->aspan->span.chans[mod->card];
}

static inline void
mod_hooksig(struct wctdm *wc, struct wctdm_module *mod, enum dahdi_rxsig rxsig)
{
	dahdi_hooksig(get_dahdi_chan(wc, mod), rxsig);
}

struct wctdm *ifaces[WC_MAX_IFACES];
DEFINE_SEMAPHORE(ifacelock);

static void wctdm_release(struct wctdm *wc);

static int fxovoltage = 0;
static unsigned int battdebounce;
static unsigned int battalarm;
static unsigned int battthresh;
static int debug = 0;
#ifdef DEBUG
static int robust = 0;
static int digitalloopback;
#endif
static int lowpower = 0;
static int boostringer = 0;
static int fastringer = 0;
static int _opermode = 0;
static char *opermode = "FCC";
static int fxshonormode = 0;
static int alawoverride = 0;
static char *companding = "auto";
static int fastpickup = -1; /* -1 auto, 0 no, 1 yes */
static int fxotxgain = 0;
static int fxorxgain = 0;
static int fxstxgain = 0;
static int fxsrxgain = 0;
static int nativebridge = 0;
static int ringdebounce = DEFAULT_RING_DEBOUNCE;
static int latency = VOICEBUS_DEFAULT_LATENCY;
static unsigned int max_latency = VOICEBUS_DEFAULT_MAXLATENCY;
static int forceload;

#define MS_PER_HOOKCHECK	(1)
#define NEONMWI_ON_DEBOUNCE	(100/MS_PER_HOOKCHECK)
static int neonmwi_monitor = 0; 	/* Note: this causes use of full wave ring detect */
static int neonmwi_level = 75;		/* neon mwi trip voltage */
static int neonmwi_envelope = 10;
static int neonmwi_offlimit = 16000;  /* Time in milliseconds the monitor is checked before saying no message is waiting */
static int neonmwi_offlimit_cycles;  /* Time in milliseconds the monitor is checked before saying no message is waiting */

static int vpmsupport = 1;

static int vpmnlptype = DEFAULT_NLPTYPE;
static int vpmnlpthresh = DEFAULT_NLPTHRESH;
static int vpmnlpmaxsupp = DEFAULT_NLPMAXSUPP;

static void echocan_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec);

static const struct dahdi_echocan_features vpm_ec_features = {
	.NLP_automatic = 1,
	.CED_tx_detect = 1,
	.CED_rx_detect = 1,
};

static const struct dahdi_echocan_ops vpm_ec_ops = {
	.echocan_free = echocan_free,
};

static int
wctdm_init_proslic(struct wctdm *wc, struct wctdm_module *const mod, int fast,
		   int manual, int sane);

static void set_offsets(struct wctdm_module *const mod, int altcs)
{
	int card = mod->card;
	int bit;

	mod->subaddr = (altcs) ? 0 : (mod->card & 0x3);

	for (bit = 0; bit < ARRAY_SIZE(mod->offsets); ++bit) {
		/* Let's add some trickery to make the TDM410 work */
		if (altcs == 3) {
			if (card == 2) {
				card = 4;
				altcs = 0;
			} else if (card == 3) {
				card = 5;
				altcs = 2;
			}
		}
		mod->offsets[bit] = ((((card & 0x3) * 3 + bit) * 7) +
					(card >> 2) + altcs +
					((altcs) ? -21 : 0));
	}
}

static inline __attribute_const__ int
CMD_BYTE(const struct wctdm_module *const mod, const int bit)
{
	return mod->offsets[bit];
}

static inline __attribute_const__ int VPM_CMD_BYTE(int timeslot, int bit)
{
	return ((((timeslot) & 0x3) * 3 + (bit)) * 7) + ((timeslot) >> 2);
}

typedef int (*bg_work_func_t)(struct wctdm *wc, unsigned long data);

struct bg {
	struct workqueue_struct *wq;
	struct work_struct	work;
	struct completion	complete;
	struct wctdm		*wc;
	bg_work_func_t		fn;
	unsigned long		param;
	int			ret;
};

#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
static void bg_work_func(void *data)
{
	struct bg *bg = data;
#else
static void bg_work_func(struct work_struct *work)
{
	struct bg *bg = container_of(work, struct bg, work);
#endif
	bg->ret = bg->fn(bg->wc, bg->param);
	complete(&bg->complete);
}

/**
 * bg_create - Call a function running in a background thread.
 * @wc:		The board structure passed to fn
 * @fn:		The function to run in it's own thread.
 * @parma:	An extra parameter to pass to the fn.
 *
 * Returns NULL if the thread could not be created, otherwise a pointer to be
 * passed to bg_join in order to get the return value.
 *
 * The function 'fn' will be run in a new thread. The return value is the
 * return from the bg_join function.
 *
 * This would probably be best served by concurrency managed workqueues before
 * merging, but this will at least work on the older kernels tht DAHDI
 * supports.
 */
static struct bg *
bg_create(struct wctdm *wc, bg_work_func_t fn, unsigned long param)
{
	struct bg *bg;

	bg = kzalloc(sizeof(*bg), GFP_KERNEL);
	if (!bg)
		return NULL;

	bg->wq = create_singlethread_workqueue("wctdm_bg");
	if (!bg->wq) {
		kfree(bg);
		return NULL;
	}

	init_completion(&bg->complete);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
	INIT_WORK(&bg->work, bg_work_func, bg);
#else
	INIT_WORK(&bg->work, bg_work_func);
#endif

	bg->wc = wc;
	bg->fn = fn;
	bg->param = param;

	queue_work(bg->wq, &bg->work);

	return bg;
}

/**
 * bg_join - Wait for a background function to complete and get the result.
 * @bg:		Pointer returned from the bg_create call.
 *
 * Returns the result of the function passed to bg_create.
 */
static int bg_join(struct bg *bg)
{
	int ret = -ERESTARTSYS;

	if (unlikely(!bg))
		return -EINVAL;

	while (ret)
		ret = wait_for_completion_interruptible(&bg->complete);

	ret = bg->ret;
	destroy_workqueue(bg->wq);
	kfree(bg);

	return ret;
}

static void
setchanconfig_from_state(struct vpmadt032 *vpm, int channel,
			 GpakChannelConfig_t *chanconfig)
{
	GpakEcanParms_t *p;

	BUG_ON(!vpm);

	chanconfig->PcmInPortA = 3;
	chanconfig->PcmInSlotA = channel;
	chanconfig->PcmOutPortA = SerialPortNull;
	chanconfig->PcmOutSlotA = channel;
	chanconfig->PcmInPortB = 2;
	chanconfig->PcmInSlotB = channel;
	chanconfig->PcmOutPortB = 3;
	chanconfig->PcmOutSlotB = channel;
	chanconfig->ToneTypesA = Null_tone;
	chanconfig->MuteToneA = Disabled;
	chanconfig->FaxCngDetA = Disabled;
	chanconfig->ToneTypesB = Null_tone;
	chanconfig->EcanEnableA = Enabled;
	chanconfig->EcanEnableB = Disabled;
	chanconfig->MuteToneB = Disabled;
	chanconfig->FaxCngDetB = Disabled;

	/* The software companding will be overridden on a channel by channel
	 * basis when the channel is enabled. */
	chanconfig->SoftwareCompand = cmpPCMU;

	chanconfig->FrameRate = rate2ms;
	p = &chanconfig->EcanParametersA;

	vpmadt032_get_default_parameters(p);

	p->EcanNlpType = vpm->curecstate[channel].nlp_type;
	p->EcanNlpThreshold = vpm->curecstate[channel].nlp_threshold;
	p->EcanNlpMaxSuppress = vpm->curecstate[channel].nlp_max_suppress;

	memcpy(&chanconfig->EcanParametersB,
		&chanconfig->EcanParametersA,
		sizeof(chanconfig->EcanParametersB));
}

struct vpmadt032_channel_setup {
	struct work_struct	work;
	struct wctdm		*wc;
};

#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
static void vpm_setup_work_func(void *data)
{
	struct vpmadt032_channel_setup *setup = data;
#else
static void vpm_setup_work_func(struct work_struct *work)
{
	struct vpmadt032_channel_setup *setup =
		container_of(work, struct vpmadt032_channel_setup, work);
#endif
	int i;
	int res;
	GpakChannelConfig_t chanconfig;
	GPAK_ChannelConfigStat_t cstatus;
	GPAK_AlgControlStat_t algstatus;
	GpakPortConfig_t portconfig = {0};
	gpakConfigPortStatus_t configportstatus;
	GPAK_PortConfigStat_t pstatus;
	struct vpmadt032 *vpm;
	struct wctdm *const wc = setup->wc;

	WARN_ON(!wc);
	WARN_ON(!wc->vpmadt032);
	if (unlikely(!wc || !wc->vpmadt032))
		return;
	vpm = wc->vpmadt032;

	/* First Serial Port config */
	portconfig.SlotsSelect1 = SlotCfgNone;
	portconfig.FirstBlockNum1 = 0;
	portconfig.FirstSlotMask1 = 0x0000;
	portconfig.SecBlockNum1 = 1;
	portconfig.SecSlotMask1 = 0x0000;
	portconfig.SerialWordSize1 = SerWordSize8;
	portconfig.CompandingMode1 = cmpNone;
	portconfig.TxFrameSyncPolarity1 = FrameSyncActHigh;
	portconfig.RxFrameSyncPolarity1 = FrameSyncActHigh;
	portconfig.TxClockPolarity1 = SerClockActHigh;
	portconfig.RxClockPolarity1 = SerClockActHigh;
	portconfig.TxDataDelay1 = DataDelay0;
	portconfig.RxDataDelay1 = DataDelay0;
	portconfig.DxDelay1 = Disabled;
	portconfig.ThirdSlotMask1 = 0x0000;
	portconfig.FouthSlotMask1 = 0x0000;
	portconfig.FifthSlotMask1 = 0x0000;
	portconfig.SixthSlotMask1 = 0x0000;
	portconfig.SevenSlotMask1 = 0x0000;
	portconfig.EightSlotMask1 = 0x0000;

	/* Second Serial Port config */
	portconfig.SlotsSelect2 = SlotCfg2Groups;
	portconfig.FirstBlockNum2 = 0;
	portconfig.FirstSlotMask2 = 0xffff;
	portconfig.SecBlockNum2 = 1;
	portconfig.SecSlotMask2 = 0xffff;
	portconfig.SerialWordSize2 = SerWordSize8;
	portconfig.CompandingMode2 = cmpNone;
	portconfig.TxFrameSyncPolarity2 = FrameSyncActHigh;
	portconfig.RxFrameSyncPolarity2 = FrameSyncActHigh;
	portconfig.TxClockPolarity2 = SerClockActHigh;
	portconfig.RxClockPolarity2 = SerClockActLow;
	portconfig.TxDataDelay2 = DataDelay0;
	portconfig.RxDataDelay2 = DataDelay0;
	portconfig.DxDelay2 = Disabled;
	portconfig.ThirdSlotMask2 = 0x0000;
	portconfig.FouthSlotMask2 = 0x0000;
	portconfig.FifthSlotMask2 = 0x0000;
	portconfig.SixthSlotMask2 = 0x0000;
	portconfig.SevenSlotMask2 = 0x0000;
	portconfig.EightSlotMask2 = 0x0000;

	/* Third Serial Port Config */
	portconfig.SlotsSelect3 = SlotCfg2Groups;
	portconfig.FirstBlockNum3 = 0;
	portconfig.FirstSlotMask3 = 0xffff;
	portconfig.SecBlockNum3 = 1;
	portconfig.SecSlotMask3 = 0xffff;
	portconfig.SerialWordSize3 = SerWordSize8;
	portconfig.CompandingMode3 = cmpNone;
	portconfig.TxFrameSyncPolarity3 = FrameSyncActHigh;
	portconfig.RxFrameSyncPolarity3 = FrameSyncActHigh;
	portconfig.TxClockPolarity3 = SerClockActHigh;
	portconfig.RxClockPolarity3 = SerClockActLow;
	portconfig.TxDataDelay3 = DataDelay0;
	portconfig.RxDataDelay3 = DataDelay0;
	portconfig.DxDelay3 = Disabled;
	portconfig.ThirdSlotMask3 = 0x0000;
	portconfig.FouthSlotMask3 = 0x0000;
	portconfig.FifthSlotMask3 = 0x0000;
	portconfig.SixthSlotMask3 = 0x0000;
	portconfig.SevenSlotMask3 = 0x0000;
	portconfig.EightSlotMask3 = 0x0000;

	if ((configportstatus = gpakConfigurePorts(vpm->dspid, &portconfig, &pstatus))) {
		dev_notice(&wc->vb.pdev->dev, "Configuration of ports failed (%d)!\n", configportstatus);
		return;
	} else {
		if (vpm->options.debug & DEBUG_ECHOCAN)
			dev_info(&wc->vb.pdev->dev, "Configured McBSP ports successfully\n");
	}

	res = gpakPingDsp(vpm->dspid, &vpm->version);
	if (res) {
		dev_notice(&wc->vb.pdev->dev, "Error pinging DSP (%d)\n", res);
		return;
	}

	for (i = 0; i < vpm->options.channels; ++i) {
		vpm->curecstate[i].tap_length = 0;
		vpm->curecstate[i].nlp_type = vpm->options.vpmnlptype;
		vpm->curecstate[i].nlp_threshold = vpm->options.vpmnlpthresh;
		vpm->curecstate[i].nlp_max_suppress =
						vpm->options.vpmnlpmaxsupp;
		vpm->curecstate[i].companding = ADT_COMP_ULAW;
		vpm->setchanconfig_from_state(vpm, i, &chanconfig);

		res = gpakConfigureChannel(vpm->dspid, i, tdmToTdm,
					   &chanconfig, &cstatus);
		if (res) {
			dev_notice(&wc->vb.pdev->dev,
				   "Unable to configure channel #%d (%d)",
				   i, res);
			if (res == 1)
				printk(KERN_CONT ", reason %d", cstatus);
			printk(KERN_CONT "\n");
			goto exit;
		}

		res = gpakAlgControl(vpm->dspid, i, BypassEcanA, &algstatus);
		if (res) {
			dev_notice(&wc->vb.pdev->dev,
				   "Unable to disable echo can on channel %d "
				   "(reason %d:%d)\n", i + 1, res, algstatus);
			goto exit;
		}

		res = gpakAlgControl(vpm->dspid, i,
				     BypassSwCompanding, &algstatus);
		if (res) {
			dev_notice(&wc->vb.pdev->dev,
				   "Unable to disable echo can on channel %d "
				   "(reason %d:%d)\n", i + 1, res, algstatus);
			goto exit;
		}
	}

	res = gpakPingDsp(vpm->dspid, &vpm->version);
	if (res) {
		dev_notice(&wc->vb.pdev->dev, "Error pinging DSP (%d)\n", res);
		goto exit;
	}

	set_bit(VPM150M_ACTIVE, &vpm->control);

exit:
	kfree(setup);
}

static int config_vpmadt032(struct vpmadt032 *vpm, struct wctdm *wc)
{
	struct vpmadt032_channel_setup *setup;

	/* Because the channel configuration can take such a long time, let's
	 * move this out onto the VPM workqueue so the system can proceeded
	 * with startup. */

	setup = kzalloc(sizeof(*setup), GFP_KERNEL);
	if (!setup)
		return -ENOMEM;

	setup->wc = wc;

#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
	INIT_WORK(&setup->work, vpm_setup_work_func, setup);
#else
	INIT_WORK(&setup->work, vpm_setup_work_func);
#endif

	queue_work(vpm->wq, &setup->work);

	return 0;
}

/**
 * is_good_frame() - Whether the SFRAME received was one sent.
 *
 */
static inline bool is_good_frame(const u8 *sframe)
{
        const u8 a = sframe[0*(EFRAME_SIZE+EFRAME_GAP) + (EFRAME_SIZE+1)];
        const u8 b = sframe[1*(EFRAME_SIZE+EFRAME_GAP) + (EFRAME_SIZE+1)];
        return a != b;
}

static inline void cmd_dequeue_vpmoct(struct wctdm *wc, u8 *eframe)
{
	struct vpmoct *vpm = wc->vpmoct;
	struct vpmoct_cmd *cmd;
	u8 i;

	/* Pop a command off pending list */
	spin_lock(&vpm->list_lock);
	if (list_empty(&vpm->pending_list)) {
		spin_unlock(&vpm->list_lock);
		return;
	}

	cmd = list_entry(vpm->pending_list.next, struct vpmoct_cmd, node);
	if (is_vpmoct_cmd_read(cmd))
		list_move_tail(&cmd->node, &vpm->active_list);
	else
		list_del_init(&cmd->node);

	/* Skip audio (24 bytes) and ignore first 6 timeslots */
	eframe += 30;

	/* Save ident so we can match the return eframe */
	cmd->txident = wc->txident;

	/* We have four timeslots to work with for a regular spi packet */
	/* TODO: Create debug flag for this in dev */

	/* The vpmoct requires a "sync" spi command as the first three bytes
	 * of an eframe */
	eframe[7*0] = 0x12;
	eframe[7*1] = 0x34;
	eframe[7*2] = 0x56;
	eframe[7*3] = cmd->command;
	eframe[7*4] = cmd->address;
	eframe[7*5] = cmd->data[0];
	for (i = 1; i < cmd->chunksize; i++)
		eframe[(7*5)+7*i] = cmd->data[i];

	/* Clean up fire-and-forget messages from memory */
	if (list_empty(&cmd->node))
		kfree(cmd);

	spin_unlock(&vpm->list_lock);
}

static inline void cmd_dequeue_vpmadt032(struct wctdm *wc, u8 *eframe)
{
	struct vpmadt032_cmd *curcmd = NULL;
	struct vpmadt032 *vpmadt032 = wc->vpmadt032;
	int x;
	unsigned char leds = ~((wc->framecount / 1000) % 8) & 0x7;

	/* Skip audio */
	eframe += 24;

	if (test_bit(VPM150M_HPIRESET, &vpmadt032->control)) {
		if (debug & DEBUG_ECHOCAN)
			dev_info(&wc->vb.pdev->dev, "HW Resetting VPMADT032...\n");
		for (x = 24; x < 28; x++) {
			if (x == 24) {
				if (test_and_clear_bit(VPM150M_HPIRESET,
						       &vpmadt032->control)) {
					eframe[VPM_CMD_BYTE(x, 0)] = 0x0b;
				} else {
					eframe[VPM_CMD_BYTE(x, 0)] = leds;
				}
			} else {
				eframe[VPM_CMD_BYTE(x, 0)] = leds;
			}
			eframe[VPM_CMD_BYTE(x, 1)] = 0;
			eframe[VPM_CMD_BYTE(x, 2)] = 0x00;
		}
		return;
	}

	if ((curcmd = vpmadt032_get_ready_cmd(vpmadt032))) {
		curcmd->txident = wc->txident;
#if 0
		// if (printk_ratelimit()) 
			dev_info(&wc->vb.pdev->dev, "Transmitting txident = %d, desc = 0x%x, addr = 0x%x, data = 0x%x\n", curcmd->txident, curcmd->desc, curcmd->address, curcmd->data);
#endif
		if (curcmd->desc & __VPM150M_RWPAGE) {
			/* Set CTRL access to page*/
			eframe[VPM_CMD_BYTE(24, 0)] = (0x8 << 4);
			eframe[VPM_CMD_BYTE(24, 1)] = 0;
			eframe[VPM_CMD_BYTE(24, 2)] = 0x20;

			/* Do a page write */
			if (curcmd->desc & __VPM150M_WR)
				eframe[VPM_CMD_BYTE(25, 0)] = ((0x8 | 0x4) << 4);
			else
				eframe[VPM_CMD_BYTE(25, 0)] = ((0x8 | 0x4 | 0x1) << 4);
			eframe[VPM_CMD_BYTE(25, 1)] = 0;
			if (curcmd->desc & __VPM150M_WR)
				eframe[VPM_CMD_BYTE(25, 2)] = curcmd->data & 0xf;
			else
				eframe[VPM_CMD_BYTE(25, 2)] = 0;

			/* Clear XADD */
			eframe[VPM_CMD_BYTE(26, 0)] = (0x8 << 4);
			eframe[VPM_CMD_BYTE(26, 1)] = 0;
			eframe[VPM_CMD_BYTE(26, 2)] = 0;

			/* Fill in to buffer to size */
			eframe[VPM_CMD_BYTE(27, 0)] = 0;
			eframe[VPM_CMD_BYTE(27, 1)] = 0;
			eframe[VPM_CMD_BYTE(27, 2)] = 0;

		} else {
			/* Set address */
			eframe[VPM_CMD_BYTE(24, 0)] = ((0x8 | 0x4) << 4);
			eframe[VPM_CMD_BYTE(24, 1)] = (curcmd->address >> 8) & 0xff;
			eframe[VPM_CMD_BYTE(24, 2)] = curcmd->address & 0xff;

			/* Send/Get our data */
			eframe[VPM_CMD_BYTE(25, 0)] = (curcmd->desc & __VPM150M_WR) ?
				((0x8 | (0x3 << 1)) << 4) : ((0x8 | (0x3 << 1) | 0x1) << 4);
			eframe[VPM_CMD_BYTE(25, 1)] = (curcmd->data >> 8) & 0xff;
			eframe[VPM_CMD_BYTE(25, 2)] = curcmd->data & 0xff;
			
			eframe[VPM_CMD_BYTE(26, 0)] = 0;
			eframe[VPM_CMD_BYTE(26, 1)] = 0;
			eframe[VPM_CMD_BYTE(26, 2)] = 0;

			/* Fill in the rest */
			eframe[VPM_CMD_BYTE(27, 0)] = 0;
			eframe[VPM_CMD_BYTE(27, 1)] = 0;
			eframe[VPM_CMD_BYTE(27, 2)] = 0;
		}
	} else if (test_and_clear_bit(VPM150M_SWRESET, &vpmadt032->control)) {
		for (x = 24; x < 28; x++) {
			if (x == 24)
				eframe[VPM_CMD_BYTE(x, 0)] = (0x8 << 4);
			else
				eframe[VPM_CMD_BYTE(x, 0)] = 0x00;
			eframe[VPM_CMD_BYTE(x, 1)] = 0;
			if (x == 24)
				eframe[VPM_CMD_BYTE(x, 2)] = 0x01;
			else
				eframe[VPM_CMD_BYTE(x, 2)] = 0x00;
		}
	} else {
		for (x = 24; x < 28; x++) {
			eframe[VPM_CMD_BYTE(x, 0)] = 0x00;
			eframe[VPM_CMD_BYTE(x, 1)] = 0x00;
			eframe[VPM_CMD_BYTE(x, 2)] = 0x00;
		}
	}

	/* Add our leds in */
	for (x = 24; x < 28; x++) {
		eframe[VPM_CMD_BYTE(x, 0)] |= leds;
	}
}

/* Call with wc->reglock held and local interrupts disabled */
static void _cmd_dequeue(struct wctdm *wc, u8 *eframe, int card, int pos)
{
	struct wctdm_module *const mod = &wc->mods[card];
	unsigned int curcmd=0;

	/* QRV only use commands relating to the first channel */
	if ((card & 0x03) && (mod->type == QRV))
		return;

	/* Skip audio */
	eframe += 24;
	/* Search for something waiting to transmit */
	if (pos) {
		if (!list_empty(&mod->pending_cmds)) {
			struct wctdm_cmd *const cmd =
				list_entry(mod->pending_cmds.next,
					   struct wctdm_cmd, node);
			curcmd = cmd->cmd;
			cmd->ident = wc->txident;
			list_move_tail(&cmd->node, &mod->active_cmds);
		}
	}

	if (!curcmd) {
		/* If nothing else, use filler */
		switch (mod->type) {
		case FXS:
			curcmd = CMD_RD(LINE_STATE);
			break;
		case FXO:
			curcmd = CMD_RD(12);
			break;
		case BRI:
			curcmd = 0x101010;
			break;
		case QRV:
			curcmd = CMD_RD(3);
			break;
		default:
			break;
		}
	}

	switch (mod->type) {
	case FXS:
		eframe[CMD_BYTE(mod, 0)] = (1 << (mod->subaddr));
		if (curcmd & __CMD_WR)
			eframe[CMD_BYTE(mod, 1)] = (curcmd >> 8) & 0x7f;
		else
			eframe[CMD_BYTE(mod, 1)] = 0x80 | ((curcmd >> 8) & 0x7f);
		eframe[CMD_BYTE(mod, 2)] = curcmd & 0xff;
		break;

	case FXO:
	{
		static const int ADDRS[4] = {0x00, 0x08, 0x04, 0x0c};
		if (curcmd & __CMD_WR)
			eframe[CMD_BYTE(mod, 0)] = 0x20 | ADDRS[mod->subaddr];
		else
			eframe[CMD_BYTE(mod, 0)] = 0x60 | ADDRS[mod->subaddr];
		eframe[CMD_BYTE(mod, 1)] = (curcmd >> 8) & 0xff;
		eframe[CMD_BYTE(mod, 2)] = curcmd & 0xff;
		break;
	}
	case FXSINIT:
		/* Special case, we initialize the FXS's into the three-byte command mode then
		   switch to the regular mode.  To send it into thee byte mode, treat the path as
		   6 two-byte commands and in the last one we initialize register 0 to 0x80. All modules
		   read this as the command to switch to daisy chain mode and we're done.  */
		eframe[CMD_BYTE(mod, 0)] = 0x00;
		eframe[CMD_BYTE(mod, 1)] = 0x00;
		if ((card & 0x1) == 0x1) 
			eframe[CMD_BYTE(mod, 2)] = 0x80;
		else
			eframe[CMD_BYTE(mod, 2)] = 0x00;
		break;

	case BRI:
		if (unlikely((curcmd != 0x101010) && (curcmd & 0x1010) == 0x1010)) /* b400m CPLD */
			eframe[CMD_BYTE(mod, 0)] = 0x55;
		else /* xhfc */
			eframe[CMD_BYTE(mod, 0)] = 0x10;
		eframe[CMD_BYTE(mod, 1)] = (curcmd >> 8) & 0xff;
		eframe[CMD_BYTE(mod, 2)] = curcmd & 0xff;
		break;

	case QRV:
		eframe[CMD_BYTE(mod, 0)] = 0x00;
		if (!curcmd) {
			eframe[CMD_BYTE(mod, 1)] = 0x00;
			eframe[CMD_BYTE(mod, 2)] = 0x00;
		} else {
			if (curcmd & __CMD_WR)
				eframe[CMD_BYTE(mod, 1)] = 0x40 | ((curcmd >> 8) & 0x3f);
			else
				eframe[CMD_BYTE(mod, 1)] = 0xc0 | ((curcmd >> 8) & 0x3f);
			eframe[CMD_BYTE(mod, 2)] = curcmd & 0xff;
		}
		break;

	case NONE:
		eframe[CMD_BYTE(mod, 0)] = 0x10;
		eframe[CMD_BYTE(mod, 1)] = 0x10;
		eframe[CMD_BYTE(mod, 2)] = 0x10;
		break;
	}
}

static inline void cmd_decipher_vpmoct(struct wctdm *wc, const u8 *eframe)
{
	struct vpmoct *vpm = wc->vpmoct;
	struct vpmoct_cmd *cmd;
	int i;

	/* Skip audio and first 6 timeslots */
	eframe += 30;

	spin_lock(&vpm->list_lock);
	/* No command to handle, just exit */
	if (list_empty(&vpm->active_list)) {
		spin_unlock(&vpm->list_lock);
		return;
	}

	cmd = list_entry(vpm->active_list.next, struct vpmoct_cmd, node);
	if (wc->rxident == cmd->txident)
		list_del_init(&cmd->node);
	else
		cmd = NULL;
	spin_unlock(&vpm->list_lock);

	if (!cmd)
		return;

	/* Store result, Ignoring the first "sync spi command" bytes */
	cmd->command = eframe[7*3];
	cmd->address = eframe[7*4];
	for (i = 0; i < cmd->chunksize; ++i)
		cmd->data[i] = eframe[7*(5+i)];
	complete(&cmd->complete);
}

static inline void cmd_decipher_vpmadt032(struct wctdm *wc, const u8 *eframe)
{
	struct vpmadt032 *const vpm = wc->vpmadt032;
	struct vpmadt032_cmd *cmd;

	BUG_ON(!vpm);

	/* If the hardware is not processing any commands currently, then
	 * there is nothing for us to do here. */
	if (list_empty(&vpm->active_cmds)) {
		return;
	}

	spin_lock(&vpm->list_lock);
	cmd = list_entry(vpm->active_cmds.next, struct vpmadt032_cmd, node);
	if (wc->rxident == cmd->txident) {
		list_del_init(&cmd->node);
	} else {
		cmd = NULL;
	}
	spin_unlock(&vpm->list_lock);

	if (!cmd)
		return;

	/* Skip audio */
	eframe += 24;

	/* Store result */
	cmd->data = (0xff & eframe[VPM_CMD_BYTE(25, 1)]) << 8;
	cmd->data |= eframe[VPM_CMD_BYTE(25, 2)];
	if (cmd->desc & __VPM150M_WR) {
		kfree(cmd);
	} else {
		cmd->desc |= __VPM150M_FIN;
		complete(&cmd->complete);
	}
}

/**
 * Call with the reglock held and local interrupts disabled
 */
static void _cmd_decipher(struct wctdm *wc, const u8 *eframe, int card)
{
	enum { TDM_BYTES = 24, };
	struct wctdm_module *const mod = &wc->mods[card];
	struct wctdm_cmd *cmd;
	u8 address;
	u8 value;

	if (list_empty(&mod->active_cmds))
		return;

	cmd = list_entry(mod->active_cmds.next, struct wctdm_cmd, node);
	if (cmd->ident != wc->rxident)
		return;

	list_del(&cmd->node);

	if (cmd->cmd & __CMD_WR) {
		kfree(cmd);
		return;
	}

	address = (cmd->cmd >> 8) & 0xff;

	cmd->cmd = eframe[TDM_BYTES + CMD_BYTE(mod, 2)];

	value = (cmd->cmd & 0xff);

	if (cmd->complete) {
		complete(cmd->complete);
		return;
	}

	list_add(&cmd->node, &wc->free_isr_commands);

	switch (mod->type) {
	case FXS:
		if (68 == address)
			mod->mod.fxs.hook_state_shadow = value;
		else
			mod->mod.fxs.linefeed_control_shadow = value;
		break;
	case FXO:
		if (5 == address)
			mod->mod.fxo.hook_ring_shadow = value;
		else
			mod->mod.fxo.line_voltage_status = value;
		break;
	case QRV:
		/* wctdm_isr_getreg(wc, mod, 3); */ /* COR/CTCSS state */
		/* TODO: This looks broken to me, but I have no way to
		 * resolved it. */
		/* wc->mods[card & 0xfc].cmds[USER_COMMANDS + 1] = CMD_RD(3); */
		break;
	default:
		break;
	}
}

/* Call with wc.reglock held and local interrupts disabled. */
static void
wctdm_isr_getreg(struct wctdm *wc, struct wctdm_module *const mod, u8 address)
{
	struct wctdm_cmd *cmd;

	if (!list_empty(&wc->free_isr_commands)) {
		cmd = list_entry(wc->free_isr_commands.next,
				 struct wctdm_cmd, node);
		list_del(&cmd->node);
	} else {
		cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC);
		if (unlikely(!cmd))
			return;
	}

	cmd->cmd = CMD_RD(address);
	cmd->complete = NULL;

	list_add(&cmd->node, &mod->pending_cmds);
}

/* Must be called with wc.reglock held and local interrupts disabled */
static inline void
wctdm_setreg_intr(struct wctdm *wc, struct wctdm_module *mod, int addr, int val)
{
	struct wctdm_cmd *cmd;

	cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC);
	if (unlikely(!cmd))
		return;

	cmd->complete = NULL;
	cmd->cmd = CMD_WR(addr, val);

	list_add_tail(&cmd->node, &mod->pending_cmds);
}


static void cmd_checkisr(struct wctdm *wc, struct wctdm_module *const mod)
{
	if (mod->sethook) {
		wctdm_setreg_intr(wc, mod, ((mod->sethook >> 8) & 0xff),
				  mod->sethook & 0xff);
		mod->sethook = 0;
		return;
	}

	switch (mod->type) {
	case FXS:
		wctdm_isr_getreg(wc, mod, 68); /* Hook state */
#ifdef PAQ_DEBUG
		wctdm_isr_getreg(wc, mod, 19); /* Transistor interrupts */
#else
		wctdm_isr_getreg(wc, mod, LINE_STATE);
#endif
		break;
	case FXO:
		wctdm_isr_getreg(wc, mod, 5);  /* Hook/Ring state */
		wctdm_isr_getreg(wc, mod, 29); /* Battery */
		break;
	case QRV:
		wctdm_isr_getreg(wc, mod, 3); /* COR/CTCSS state */
		/* TODO: This looks broken to me, but I have no way to
		 * resolved it. */
		/* wc->mods[card & 0xfc].cmds[USER_COMMANDS + 1] = CMD_RD(3); */
		break;
	case BRI:
		/* TODO: Two calls needed here? */
		wctdm_bri_checkisr(wc, mod, 0);
		wctdm_bri_checkisr(wc, mod, 1);
		break;
	default:
		break;
	}
}

/**
 * insert_tdm_data() - Move TDM data from channels to sframe.
 *
 */
static void insert_tdm_data(const struct wctdm *wc, u8 *sframe)
{
	int i;
	register u8 *chanchunk;

	for (i = 0; i < wc->avchannels; i += 4) {
		chanchunk = &wc->chans[0 + i]->chan.writechunk[0];
		sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*0] = chanchunk[0];
		sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*1] = chanchunk[1];
		sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*2] = chanchunk[2];
		sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*3] = chanchunk[3];
		sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*4] = chanchunk[4];
		sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*5] = chanchunk[5];
		sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*6] = chanchunk[6];
		sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*7] = chanchunk[7];

		chanchunk = &wc->chans[1 + i]->chan.writechunk[0];
		sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*0] = chanchunk[0];
		sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*1] = chanchunk[1];
		sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*2] = chanchunk[2];
		sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*3] = chanchunk[3];
		sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*4] = chanchunk[4];
		sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*5] = chanchunk[5];
		sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*6] = chanchunk[6];
		sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*7] = chanchunk[7];

		chanchunk = &wc->chans[2 + i]->chan.writechunk[0];
		sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*0] = chanchunk[0];
		sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*1] = chanchunk[1];
		sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*2] = chanchunk[2];
		sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*3] = chanchunk[3];
		sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*4] = chanchunk[4];
		sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*5] = chanchunk[5];
		sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*6] = chanchunk[6];
		sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*7] = chanchunk[7];

		chanchunk = &wc->chans[3 + i]->chan.writechunk[0];
		sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*0] = chanchunk[0];
		sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*1] = chanchunk[1];
		sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*2] = chanchunk[2];
		sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*3] = chanchunk[3];
		sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*4] = chanchunk[4];
		sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*5] = chanchunk[5];
		sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*6] = chanchunk[6];
		sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*7] = chanchunk[7];
	}
}

static inline void wctdm_transmitprep(struct wctdm *wc, unsigned char *sframe)
{
	int x, y;
	struct dahdi_span *s;
	unsigned char *eframe = sframe;

	/* Calculate Transmission */
	if (likely(is_initialized(wc))) {
		for (x = 0; x < MAX_SPANS; x++) {
			if (wc->spans[x]) {
				s = &wc->spans[x]->span;
				_dahdi_transmit(s);
			}
		}
		insert_tdm_data(wc, sframe);
#ifdef CONFIG_VOICEBUS_ECREFERENCE
		for (x = 0; x < wc->avchannels; ++x) {
			__dahdi_fifo_put(wc->ec_reference[x],
					 wc->chans[x]->chan.writechunk,
					 DAHDI_CHUNKSIZE);
		}
#endif
	}

	spin_lock(&wc->reglock);
	for (x = 0; x < DAHDI_CHUNKSIZE; x++) {
		/* Send a sample, as a 32-bit word */

		/* TODO: ABK: hmm, this was originally mods_per_board, but we
		 * need to worry about all the active "voice" timeslots, since
		 * BRI modules have a different number of TDM channels than
		 * installed modules. */
		for (y = 0; y < wc->avchannels; y++) {
			if (y < wc->mods_per_board)
				_cmd_dequeue(wc, eframe, y, x);
		}

		if (wc->vpmadt032)
			cmd_dequeue_vpmadt032(wc, eframe);
		else if (wc->vpmoct)
			cmd_dequeue_vpmoct(wc, eframe);

		if (x < DAHDI_CHUNKSIZE - 1) {
			eframe[EFRAME_SIZE] = wc->ctlreg;
			eframe[EFRAME_SIZE + 1] = wc->txident++;
			if (4 == wc->desc->ports)
				eframe[EFRAME_SIZE + 2] = wc->tdm410leds;
		}
		eframe += (EFRAME_SIZE + EFRAME_GAP);
	}
	spin_unlock(&wc->reglock);
}

int wctdm_setreg(struct wctdm *wc, struct wctdm_module *mod, int addr, int val)
{
	struct wctdm_cmd *cmd;
	unsigned long flags;

#if 0 /* TODO */
	/* QRV and BRI cards are only addressed at their first "port" */
	if ((card & 0x03) && ((wc->mods[card].type ==  QRV) ||
	    (wc->mods[card].type ==  BRI)))
		return 0;
#endif

	cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
	if (unlikely(!cmd))
		return -ENOMEM;

	cmd->complete = NULL;
	cmd->cmd = CMD_WR(addr, val);

	spin_lock_irqsave(&wc->reglock, flags);
	list_add_tail(&cmd->node, &mod->pending_cmds);
	spin_unlock_irqrestore(&wc->reglock, flags);

	return 0;
}

int wctdm_getreg(struct wctdm *wc, struct wctdm_module *const mod, int addr)
{
	unsigned long flags;
	struct wctdm_cmd *cmd;
	int val;

#if 0 /* TODO */
	/* if a QRV card, use only its first channel */  
	if (wc->mods[card].type ==  QRV) {
		if (card & 3)
			return 0;
	}
#endif

	cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
	if (!cmd)
		return -ENOMEM;

	cmd->complete = kmalloc(sizeof(*cmd->complete), GFP_KERNEL);
	if (!cmd->complete) {
		kfree(cmd);
		return -ENOMEM;
	}

	init_completion(cmd->complete);

	cmd->cmd = CMD_RD(addr);

	spin_lock_irqsave(&wc->reglock, flags);
	list_add_tail(&cmd->node, &mod->pending_cmds);
	spin_unlock_irqrestore(&wc->reglock, flags);

	wait_for_completion(cmd->complete);
	val = cmd->cmd & 0xff;

	kfree(cmd->complete);
	kfree(cmd);

	return val;
}

static int wctdm_getregs(struct wctdm *wc, struct wctdm_module *const mod,
			 int *const addresses, const size_t count)
{
	int x;
	unsigned long flags;
	struct wctdm_cmd *cmd;
	struct wctdm_cmd **cmds = kmalloc(sizeof(cmd) * count, GFP_KERNEL);

	if (!cmds)
		return -ENOMEM;

	for (x = 0; x < count; ++x) {
		cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
		if (!cmd) {
			kfree(cmds);
			return -ENOMEM;
		}

		cmd->complete = kmalloc(sizeof(*cmd->complete), GFP_KERNEL);
		if (!cmd->complete) {
			kfree(cmd);
			kfree(cmds);
			return -ENOMEM;
		}

		init_completion(cmd->complete);

		cmd->cmd = CMD_RD(addresses[x]);

		spin_lock_irqsave(&wc->reglock, flags);
		list_add_tail(&cmd->node, &mod->pending_cmds);
		spin_unlock_irqrestore(&wc->reglock, flags);

		cmds[x] = cmd;
	}

	for (x = count - 1; x >= 0; --x) {
		cmd = cmds[x];
		wait_for_completion(cmd->complete);
		addresses[x] = cmd->cmd & 0xff;
		kfree(cmd->complete);
		kfree(cmd);
	}

	kfree(cmds);
	return 0;
}

/**
 * call with wc->reglock held and interrupts disabled.
 */
static void cmd_retransmit(struct wctdm *wc)
{
	int x;

	for (x = 0; x < wc->mods_per_board; x++) {
		struct wctdm_module *const mod = &wc->mods[x];
		if (mod->type == BRI)
			continue;
		list_splice_init(&mod->active_cmds, &mod->pending_cmds);
	}

#ifdef VPM_SUPPORT
	if (wc->vpmadt032)
		vpmadt032_resend(wc->vpmadt032);
#endif
}

/**
 * extract_tdm_data() - Move TDM data from sframe to channels.
 *
 */
static void extract_tdm_data(struct wctdm *wc, const u8 *sframe)
{
	int i;
	register u8 *chanchunk;

	for (i = 0; i < wc->avchannels; i += 4) {
		chanchunk = &wc->chans[0 + i]->chan.readchunk[0];
		chanchunk[0] = sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*0];
		chanchunk[1] = sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*1];
		chanchunk[2] = sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*2];
		chanchunk[3] = sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*3];
		chanchunk[4] = sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*4];
		chanchunk[5] = sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*5];
		chanchunk[6] = sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*6];
		chanchunk[7] = sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*7];

		chanchunk = &wc->chans[1 + i]->chan.readchunk[0];
		chanchunk[0] = sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*0];
		chanchunk[1] = sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*1];
		chanchunk[2] = sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*2];
		chanchunk[3] = sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*3];
		chanchunk[4] = sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*4];
		chanchunk[5] = sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*5];
		chanchunk[6] = sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*6];
		chanchunk[7] = sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*7];

		chanchunk = &wc->chans[2 + i]->chan.readchunk[0];
		chanchunk[0] = sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*0];
		chanchunk[1] = sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*1];
		chanchunk[2] = sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*2];
		chanchunk[3] = sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*3];
		chanchunk[4] = sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*4];
		chanchunk[5] = sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*5];
		chanchunk[6] = sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*6];
		chanchunk[7] = sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*7];

		chanchunk = &wc->chans[3 + i]->chan.readchunk[0];
		chanchunk[0] = sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*0];
		chanchunk[1] = sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*1];
		chanchunk[2] = sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*2];
		chanchunk[3] = sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*3];
		chanchunk[4] = sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*4];
		chanchunk[5] = sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*5];
		chanchunk[6] = sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*6];
		chanchunk[7] = sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*7];
	}

	/* Pre-echo with the vpmoct overwrites the 24th timeslot with the
	 * specified channel's pre-echo audio stream. This data is ignored
	 * on all but the 24xx card, so we store it in a temporary buffer.
	 */
	if (wc->vpmoct && wc->vpmoct->preecho_enabled) {
		chanchunk = &wc->vpmoct->preecho_buf[0];
		chanchunk[0] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*0];
		chanchunk[1] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*1];
		chanchunk[2] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*2];
		chanchunk[3] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*3];
		chanchunk[4] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*4];
		chanchunk[5] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*5];
		chanchunk[6] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*6];
		chanchunk[7] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*7];
	}
}

static inline void wctdm_receiveprep(struct wctdm *wc, const u8 *sframe)
{
	int x, y;
	bool irqmiss = false;
	unsigned char expected;
	const u8 *eframe = sframe;

	if (unlikely(!is_good_frame(sframe)))
		return;

	spin_lock(&wc->reglock);

	if (likely(is_initialized(wc)))
		extract_tdm_data(wc, sframe);

	for (x = 0; x < DAHDI_CHUNKSIZE; x++) {
		if (x < DAHDI_CHUNKSIZE - 1) {
			expected = wc->rxident + 1;
			wc->rxident = eframe[EFRAME_SIZE + 1];
			if (wc->rxident != expected) {
				irqmiss = true;
				cmd_retransmit(wc);
			}
		}

		for (y = 0; y < wc->avchannels; y++)
			_cmd_decipher(wc, eframe, y);

		if (wc->vpmadt032)
			cmd_decipher_vpmadt032(wc, eframe);
		else if (wc->vpmoct)
			cmd_decipher_vpmoct(wc, eframe);

		eframe += (EFRAME_SIZE + EFRAME_GAP);
	}
	spin_unlock(&wc->reglock);

	/* XXX We're wasting 8 taps.  We should get closer :( */
	if (likely(is_initialized(wc))) {
		for (x = 0; x < wc->avchannels; x++) {
			struct wctdm_chan *const wchan = wc->chans[x];
			struct dahdi_chan *const c = &wchan->chan;
#ifdef CONFIG_VOICEBUS_ECREFERENCE
			unsigned char buffer[DAHDI_CHUNKSIZE];
			__dahdi_fifo_get(wc->ec_reference[x], buffer,
				    ARRAY_SIZE(buffer));
			_dahdi_ec_chunk(c, c->readchunk, buffer);
#else
			if ((wc->vpmoct) &&
			    (wchan->timeslot == wc->vpmoct->preecho_timeslot) &&
			    (wc->vpmoct->preecho_enabled)) {
				__dahdi_ec_chunk(c, c->readchunk,
						 wc->vpmoct->preecho_buf,
						 c->writechunk);
			} else {
				__dahdi_ec_chunk(c, c->readchunk, c->readchunk,
						 c->writechunk);
			}
#endif
		}

		for (x = 0; x < MAX_SPANS; x++) {
			if (wc->spans[x]) {
				struct dahdi_span *s = &wc->spans[x]->span;
#if 1
				/* Check for digital spans */
				if (s->ops->chanconfig == b400m_chanconfig) {
					BUG_ON(!is_hx8(wc));
					if (s->flags & DAHDI_FLAG_RUNNING)
						b400m_dchan(s);

				}
#endif
				_dahdi_receive(s);
				if (unlikely(irqmiss))
					wc->ddev->irqmisses++;
			}
		}
	}
}

static int wait_access(struct wctdm *wc, struct wctdm_module *const mod)
{
	unsigned char data = 0;
	int count = 0;

	#define MAX 10 /* attempts */

	/* Wait for indirect access */
	while (count++ < MAX) {
		data = wctdm_getreg(wc, mod, I_STATUS);
		if (!data)
			return 0;
	}

	if (count > (MAX-1)) {
		dev_notice(&wc->vb.pdev->dev,
			   " ##### Loop error (%02x) #####\n", data);
	}

	return 0;
}

static unsigned char translate_3215(unsigned char address)
{
	int x;
	for (x = 0; x < ARRAY_SIZE(indirect_regs); x++) {
		if (indirect_regs[x].address == address) {
			address = indirect_regs[x].altaddr;
			break;
		}
	}
	return address;
}

static int
wctdm_proslic_setreg_indirect(struct wctdm *wc, struct wctdm_module *const mod,
			      unsigned char address, unsigned short data)
{
	int res = -1;

	address = translate_3215(address);
	if (address == 255)
		return 0;

	if (!wait_access(wc, mod)) {
		wctdm_setreg(wc, mod, IDA_LO, (u8)(data & 0xFF));
		wctdm_setreg(wc, mod, IDA_HI, (u8)((data & 0xFF00)>>8));
		wctdm_setreg(wc, mod, IAA, address);
		res = 0;
	};
	return res;
}

static int
wctdm_proslic_getreg_indirect(struct wctdm *wc, struct wctdm_module *const mod,
			      unsigned char address)
{ 
	int res = -1;
	char *p=NULL;

	address = translate_3215(address);
	if (address == 255)
		return 0;

	if (!wait_access(wc, mod)) {
		wctdm_setreg(wc, mod, IAA, address);
		if (!wait_access(wc, mod)) {
			int addresses[2] = {IDA_LO, IDA_HI};
			wctdm_getregs(wc, mod, addresses,
				      ARRAY_SIZE(addresses));
			res = addresses[0] | (addresses[1] << 8);
		} else
			p = "Failed to wait inside\n";
	} else
		p = "failed to wait\n";
	if (p)
		dev_notice(&wc->vb.pdev->dev, "%s", p);
	return res;
}

static int
wctdm_proslic_init_indirect_regs(struct wctdm *wc, struct wctdm_module *mod)
{
	unsigned char i;

	for (i = 0; i < ARRAY_SIZE(indirect_regs); i++) {
		if (wctdm_proslic_setreg_indirect(wc, mod,
				indirect_regs[i].address,
				indirect_regs[i].initial))
			return -1;
	}

	return 0;
}

static int
wctdm_proslic_verify_indirect_regs(struct wctdm *wc, struct wctdm_module *mod)
{ 
	int passed = 1;
	unsigned short i, initial;
	int j;

	for (i = 0; i < ARRAY_SIZE(indirect_regs); i++) {
		j = wctdm_proslic_getreg_indirect(wc, mod,
						(u8)indirect_regs[i].address);
		if (j < 0) {
			dev_notice(&wc->vb.pdev->dev, "Failed to read indirect register %d\n", i);
			return -1;
		}
		initial = indirect_regs[i].initial;

		if ((j != initial) && (indirect_regs[i].altaddr != 255)) {
			dev_notice(&wc->vb.pdev->dev,
				   "!!!!!!! %s  iREG %X = %X  should be %X\n",
				   indirect_regs[i].name,
				   indirect_regs[i].address, j, initial);
			 passed = 0;
		}	
	}

	if (passed) {
		if (debug & DEBUG_CARD) {
			dev_info(&wc->vb.pdev->dev,
			 "Init Indirect Registers completed successfully.\n");
		}
	} else {
		dev_notice(&wc->vb.pdev->dev,
			" !!!!! Init Indirect Registers UNSUCCESSFULLY.\n");
		return -1;
	}
	return 0;
}

/**
 * wctdm_proslic_check_oppending -
 *
 * Ensures that a write to the line feed register on the SLIC has been
 * processed. If it hasn't after the timeout value, then it will resend the
 * command and wait for another timeout period.
 *
 */
static void
wctdm_proslic_check_oppending(struct wctdm *wc, struct wctdm_module *const mod)
{
	struct fxs *const fxs = &mod->mod.fxs;

	if (!(fxs->lasttxhook & SLIC_LF_OPPENDING))
		return;

	/* Monitor the Pending LF state change, for the next 100ms */
	spin_lock(&wc->reglock);


	if (!(fxs->lasttxhook & SLIC_LF_OPPENDING)) {
		spin_unlock(&wc->reglock);
		return;
	}

	if ((fxs->linefeed_control_shadow & SLIC_LF_SETMASK) ==
	    (fxs->lasttxhook & SLIC_LF_SETMASK)) {
		fxs->lasttxhook &= SLIC_LF_SETMASK;
		if (debug & DEBUG_CARD) {
			dev_info(&wc->vb.pdev->dev,
				 "SLIC_LF OK: card=%d shadow=%02x "
				 "lasttxhook=%02x framecount=%ld\n", mod->card,
				 fxs->linefeed_control_shadow,
				 fxs->lasttxhook, wc->framecount);
		}
	} else if (time_after(wc->framecount, fxs->oppending_timeout)) {
		/* Check again in 100 ms */
		fxs->oppending_timeout = wc->framecount + 100;

		wctdm_setreg_intr(wc, mod, LINE_STATE, fxs->lasttxhook);
		if (debug & DEBUG_CARD) {
			dev_info(&wc->vb.pdev->dev,
				 "SLIC_LF RETRY: card=%d shadow=%02x "
				 "lasttxhook=%02x framecount=%ld\n", mod->card,
				 fxs->linefeed_control_shadow,
				 fxs->lasttxhook, wc->framecount);
		}
	}

	spin_unlock(&wc->reglock);
}

/* 256ms interrupt */
static void
wctdm_proslic_recheck_sanity(struct wctdm *wc, struct wctdm_module *const mod)
{
	struct fxs *const fxs = &mod->mod.fxs;
	int res;
#ifdef PAQ_DEBUG
	res = mod->isrshadow[1];
	res &= ~0x3;
	if (res) {
		mod->isrshadow[1] = 0;
		fxs->palarms++;
		if (fxs->palarms < MAX_ALARMS) {
			dev_notice(&wc->vb.pdev->dev, "Power alarm (%02x) on module %d, resetting!\n", res, card + 1);
			mod->sethook = CMD_WR(19, res);
			/* Update shadow register to avoid extra power alarms until next read */
			mod->isrshadow[1] = 0;
		} else {
			if (fxs->palarms == MAX_ALARMS)
				dev_notice(&wc->vb.pdev->dev, "Too many power alarms on card %d, NOT resetting!\n", card + 1);
		}
	}
#else
	spin_lock(&wc->reglock);

	/* reg 64 has to be zero at last isr read */
	res = !fxs->linefeed_control_shadow &&
		!(fxs->lasttxhook & SLIC_LF_OPPENDING) && /* not a transition */
		fxs->lasttxhook; /* not an intended zero */
	
	if (res) {
		fxs->palarms++;
		if (fxs->palarms < MAX_ALARMS) {
			dev_notice(&wc->vb.pdev->dev,
				   "Power alarm on module %d, resetting!\n",
				   mod->card + 1);
			if (fxs->lasttxhook == SLIC_LF_RINGING) {
				fxs->lasttxhook = POLARITY_XOR(fxs) ?
							SLIC_LF_ACTIVE_REV :
							SLIC_LF_ACTIVE_FWD;;
			}
			fxs->lasttxhook |= SLIC_LF_OPPENDING;
			mod->sethook = CMD_WR(LINE_STATE, fxs->lasttxhook);
			fxs->oppending_timeout = wc->framecount + 100;

			/* Update shadow register to avoid extra power alarms
			 * until next read */
			fxs->linefeed_control_shadow = fxs->lasttxhook;
		} else {
			if (fxs->palarms == MAX_ALARMS) {
				dev_notice(&wc->vb.pdev->dev,
					   "Too many power alarms on card %d, "
					   "NOT resetting!\n", mod->card + 1);
			}
		}
	}
	spin_unlock(&wc->reglock);
#endif
}

static void wctdm_qrvdri_check_hook(struct wctdm *wc, int card)
{
	signed char b,b1;
	int qrvcard = card & 0xfc;

	if (wc->mods[card].mod.qrv.debtime >= 2)
		wc->mods[card].mod.qrv.debtime--;
	b = wc->mods[qrvcard].mod.qrv.isrshadow[0]; /* Hook/Ring state */
	b &= 0xcc; /* use bits 3-4 and 6-7 only */

	if (wc->mods[qrvcard].mod.qrv.radmode & RADMODE_IGNORECOR)
		b &= ~4;
	else if (!(wc->mods[qrvcard].mod.qrv.radmode & RADMODE_INVERTCOR))
		b ^= 4;
	if (wc->mods[qrvcard + 1].mod.qrv.radmode | RADMODE_IGNORECOR)
		b &= ~0x40;
	else if (!(wc->mods[qrvcard + 1].mod.qrv.radmode | RADMODE_INVERTCOR))
		b ^= 0x40;

	if ((wc->mods[qrvcard].mod.qrv.radmode & RADMODE_IGNORECT) ||
	    (!(wc->mods[qrvcard].mod.qrv.radmode & RADMODE_EXTTONE)))
		b &= ~8;
	else if (!(wc->mods[qrvcard].mod.qrv.radmode & RADMODE_EXTINVERT))
		b ^= 8;
	if ((wc->mods[qrvcard + 1].mod.qrv.radmode & RADMODE_IGNORECT) ||
	    (!(wc->mods[qrvcard + 1].mod.qrv.radmode & RADMODE_EXTTONE)))
		b &= ~0x80;
	else if (!(wc->mods[qrvcard + 1].mod.qrv.radmode & RADMODE_EXTINVERT))
		b ^= 0x80;
	/* now b & MASK should be zero, if its active */
	/* check for change in chan 0 */
	if ((!(b & 0xc)) != wc->mods[qrvcard + 2].mod.qrv.hook)
	{
		wc->mods[qrvcard].mod.qrv.debtime = wc->mods[qrvcard].mod.qrv.debouncetime;
		wc->mods[qrvcard + 2].mod.qrv.hook = !(b & 0xc);
	} 
	/* if timed-out and ready */
	if (wc->mods[qrvcard].mod.qrv.debtime == 1) {
		b1 = wc->mods[qrvcard + 2].mod.qrv.hook;
		if (debug) {
			dev_info(&wc->vb.pdev->dev,
				 "QRV channel %d rx state changed to %d\n",
				 qrvcard, wc->mods[qrvcard + 2].mod.qrv.hook);
		}
		dahdi_hooksig(wc->aspan->span.chans[qrvcard],
			(b1) ? DAHDI_RXSIG_OFFHOOK : DAHDI_RXSIG_ONHOOK);
		wc->mods[card].mod.qrv.debtime = 0;
	}
	/* check for change in chan 1 */
	if ((!(b & 0xc0)) != wc->mods[qrvcard + 3].mod.qrv.hook)
	{
		wc->mods[qrvcard + 1].mod.qrv.debtime = QRV_DEBOUNCETIME;
		wc->mods[qrvcard + 3].mod.qrv.hook = !(b & 0xc0);
	}
	if (wc->mods[qrvcard + 1].mod.qrv.debtime == 1) {
		b1 = wc->mods[qrvcard + 3].mod.qrv.hook;
		if (debug) {
			dev_info(&wc->vb.pdev->dev,
				 "QRV channel %d rx state changed to %d\n",
				 qrvcard + 1, wc->mods[qrvcard + 3].mod.qrv.hook);
		}
		dahdi_hooksig(wc->aspan->span.chans[qrvcard + 1],
			(b1) ? DAHDI_RXSIG_OFFHOOK : DAHDI_RXSIG_ONHOOK);
		wc->mods[card].mod.qrv.debtime = 0;
	}
	return;
}

static inline bool is_fxo_ringing(const struct fxo *const fxo)
{
	return ((fxo->hook_ring_shadow & 0x60) &&
		((fxo->battery_state == BATTERY_PRESENT) ||
		 (fxo->battery_state == BATTERY_DEBOUNCING_LOST)));
}

static inline bool is_fxo_ringing_positive(const struct fxo *const fxo)
{
	return (((fxo->hook_ring_shadow & 0x60) == 0x20) &&
		((fxo->battery_state == BATTERY_PRESENT) ||
		 (fxo->battery_state == BATTERY_DEBOUNCING_LOST)));
}

static inline bool is_fxo_ringing_negative(const struct fxo *const fxo)
{
	return (((fxo->hook_ring_shadow & 0x60) == 0x40) &&
		((fxo->battery_state == BATTERY_PRESENT) ||
		 (fxo->battery_state == BATTERY_DEBOUNCING_LOST)));
}

static inline void set_ring(struct fxo *fxo, enum ring_detector_state new)
{
	fxo->ring_state = new;
}

static void wctdm_fxo_ring_detect(struct wctdm *wc, struct wctdm_module *mod)
{
	struct fxo *const fxo = &mod->mod.fxo;
	static const unsigned int POLARITY_CHANGES_NEEDED = 2;

	/* Look for ring status bits (Ring Detect Signal Negative and Ring
	 * Detect Signal Positive) to transition back and forth
	 * POLARITY_CHANGES_NEEDED times to indicate that a ring is occurring.
	 * Provide some number of samples to allow for the transitions to occur
	 * before giving up.  NOTE: neon mwi voltages will trigger one of these
	 * bits to go active but not to have transitions between the two bits
	 * (i.e. no negative to positive or positive to negative traversals) */

	switch (fxo->ring_state) {
	case DEBOUNCING_RINGING_POSITIVE:
		if (is_fxo_ringing_negative(fxo)) {
			if (++fxo->ring_polarity_change_count >
						POLARITY_CHANGES_NEEDED) {
				mod_hooksig(wc, mod, DAHDI_RXSIG_RING);
				set_ring(fxo, RINGING);
				if (debug) {
					dev_info(&wc->vb.pdev->dev,
						 "RING on %s!\n",
						 get_dahdi_chan(wc, mod)->name);
				}
			} else {
				set_ring(fxo, DEBOUNCING_RINGING_NEGATIVE);
			}
		} else if (time_after(wc->framecount,
				      fxo->ringdebounce_timer)) {
			set_ring(fxo, RINGOFF);
		}
		break;
	case DEBOUNCING_RINGING_NEGATIVE:
		if (is_fxo_ringing_positive(fxo)) {
			if (++fxo->ring_polarity_change_count >
						POLARITY_CHANGES_NEEDED) {
				mod_hooksig(wc, mod, DAHDI_RXSIG_RING);
				set_ring(fxo, RINGING);
				if (debug) {
					dev_info(&wc->vb.pdev->dev,
						 "RING on %s!\n",
						 get_dahdi_chan(wc, mod)->name);
				}
			} else {
				set_ring(fxo, DEBOUNCING_RINGING_POSITIVE);
			}
		} else if (time_after(wc->framecount,
				      fxo->ringdebounce_timer)) {
			set_ring(fxo, RINGOFF);
		}
		break;
	case RINGING:
		if (!is_fxo_ringing(fxo)) {
			set_ring(fxo, DEBOUNCING_RINGOFF);
			fxo->ringdebounce_timer =
					wc->framecount + ringdebounce / 8;
		}
		break;
	case DEBOUNCING_RINGOFF:
		if (!is_fxo_ringing(fxo)) {
			if (time_after(wc->framecount,
				       fxo->ringdebounce_timer)) {
				if (debug) {
					dev_info(&wc->vb.pdev->dev,
						 "NO RING on %s!\n",
						 get_dahdi_chan(wc, mod)->name);
				}
				mod_hooksig(wc, mod, DAHDI_RXSIG_OFFHOOK);
				set_ring(fxo, RINGOFF);
			}
		} else {
			set_ring(fxo, RINGING);
		}
		break;
	case RINGOFF:
		if (is_fxo_ringing(fxo)) {
			/* Look for positive/negative crossings in ring status
			 * reg */
			if (is_fxo_ringing_positive(fxo))
				set_ring(fxo, DEBOUNCING_RINGING_POSITIVE);
			else
				set_ring(fxo, DEBOUNCING_RINGING_NEGATIVE);
			fxo->ringdebounce_timer =
					wc->framecount + ringdebounce / 8;
			fxo->ring_polarity_change_count = 0;
		}
		break;
	}
}

#define MS_PER_CHECK_HOOK 1

static void
wctdm_check_battery_lost(struct wctdm *wc, struct wctdm_module *const mod)
{
	struct fxo *const fxo = &mod->mod.fxo;

	/* possible existing states:
	   battery lost, no debounce timer
	   battery lost, debounce timer (going to battery present)
	   battery present or unknown, no debounce timer
	   battery present or unknown, debounce timer (going to battery lost)
	*/
	switch (fxo->battery_state) {
	case BATTERY_DEBOUNCING_PRESENT:
		/* we were going to BATTERY_PRESENT, but
		 * battery was lost again. */
		fxo->battery_state = BATTERY_LOST;
		break;
	case BATTERY_UNKNOWN:
		mod_hooksig(wc, mod, DAHDI_RXSIG_ONHOOK);
	case BATTERY_DEBOUNCING_PRESENT_ALARM: /* intentional drop through */
	case BATTERY_PRESENT:
		fxo->battery_state = BATTERY_DEBOUNCING_LOST;
		fxo->battdebounce_timer = wc->framecount + battdebounce;
		break;
	case BATTERY_DEBOUNCING_LOST:
		if (time_after(wc->framecount, fxo->battdebounce_timer)) {
			if (debug) {
				dev_info(&wc->vb.pdev->dev,
					 "NO BATTERY on %d/%d!\n",
					 wc->aspan->span.spanno,
					 mod->card + 1);
			}
#ifdef	JAPAN
			if (!wc->ohdebounce && wc->offhook) {
				dahdi_hooksig(wc->aspan->chans[card],
					      DAHDI_RXSIG_ONHOOK);
				if (debug) {
					dev_info(&wc->vb.pdev->dev,
						 "Signalled On Hook\n");
				}
#ifdef	ZERO_BATT_RING
				wc->onhook++;
#endif
			}
#else
			mod_hooksig(wc, mod, DAHDI_RXSIG_ONHOOK);
#endif
			/* set the alarm timer, taking into account that part
			 * of its time period has already passed while
			 * debouncing occurred */
			fxo->battery_state = BATTERY_DEBOUNCING_LOST_ALARM;
			fxo->battdebounce_timer = wc->framecount +
						   battalarm - battdebounce;
		}
		break;
	case BATTERY_DEBOUNCING_LOST_ALARM:
		if (time_after(wc->framecount, fxo->battdebounce_timer)) {
			fxo->battery_state = BATTERY_LOST;
			dahdi_alarm_channel(get_dahdi_chan(wc, mod),
					    DAHDI_ALARM_RED);
		}
		break;
	case BATTERY_LOST:
		break;
	}
}

static void
wctdm_check_battery_present(struct wctdm *wc, struct wctdm_module *const mod)
{
	struct fxo *const fxo = &mod->mod.fxo;

	switch (fxo->battery_state) {
	case BATTERY_DEBOUNCING_PRESENT:
		if (time_after(jiffies, fxo->battdebounce_timer)) {
			if (debug) {
				dev_info(&wc->vb.pdev->dev,
					 "BATTERY on %d/%d (%s)!\n",
					 wc->aspan->span.spanno, mod->card + 1,
					 (fxo->line_voltage_status < 0) ?
						"-" : "+");
			}
#ifdef	ZERO_BATT_RING
			if (wc->onhook) {
				wc->onhook = 0;
				dahdi_hooksig(wc->aspan->chans[card],
					      DAHDI_RXSIG_OFFHOOK);
				if (debug) {
					dev_info(&wc->vb.pdev->dev,
						 "Signalled Off Hook\n");
				}
			}
#else
			mod_hooksig(wc, mod, DAHDI_RXSIG_OFFHOOK);
#endif
			/* set the alarm timer, taking into account that part
			 * of its time period has already passed while
			 * debouncing occurred */
			fxo->battery_state = BATTERY_DEBOUNCING_PRESENT_ALARM;
			fxo->battdebounce_timer = jiffies +
				msecs_to_jiffies(battalarm - battdebounce);
		}
		break;
	case BATTERY_DEBOUNCING_PRESENT_ALARM:
		if (time_after(jiffies, fxo->battdebounce_timer)) {
			fxo->battery_state = BATTERY_PRESENT;
			dahdi_alarm_channel(get_dahdi_chan(wc, mod),
					    DAHDI_ALARM_NONE);
		}
		break;
	case BATTERY_PRESENT:
		break;
	case BATTERY_DEBOUNCING_LOST:
		/* we were going to BATTERY_LOST, but battery appeared again,
		 * so clear the debounce timer */
		fxo->battery_state = BATTERY_PRESENT;
		break;
	case BATTERY_UNKNOWN:
		mod_hooksig(wc, mod, DAHDI_RXSIG_OFFHOOK);
	case BATTERY_LOST: /* intentional drop through */
	case BATTERY_DEBOUNCING_LOST_ALARM:
		fxo->battery_state = BATTERY_DEBOUNCING_PRESENT;
		fxo->battdebounce_timer = jiffies +
						msecs_to_jiffies(battdebounce);
		break;
	}
}

static void
wctdm_fxo_stop_debouncing_polarity(struct wctdm *wc,
				   struct wctdm_module *const mod)
{
	struct fxo *const fxo = &mod->mod.fxo;
	switch (fxo->polarity_state) {
	case UNKNOWN_POLARITY:
		break;
	case POLARITY_DEBOUNCE_POSITIVE:
		fxo->polarity_state = POLARITY_NEGATIVE;
		break;
	case POLARITY_POSITIVE:
		break;
	case POLARITY_DEBOUNCE_NEGATIVE:
		fxo->polarity_state = POLARITY_POSITIVE;
		break;
	case POLARITY_NEGATIVE:
		break;
	};
}

static void
wctdm_fxo_check_polarity(struct wctdm *wc, struct wctdm_module *const mod,
			 const bool positive_polarity)
{
	struct fxo *const fxo = &mod->mod.fxo;

	switch (fxo->polarity_state) {
	case UNKNOWN_POLARITY:
		fxo->polarity_state = (positive_polarity) ? POLARITY_POSITIVE :
							    POLARITY_NEGATIVE;
		break;
	case POLARITY_DEBOUNCE_POSITIVE:
		if (!positive_polarity) {
			fxo->polarity_state = POLARITY_NEGATIVE;
		} else if (time_after(wc->framecount, fxo->poldebounce_timer)) {
			fxo->polarity_state = POLARITY_POSITIVE;
			dahdi_qevent_lock(get_dahdi_chan(wc, mod),
					  DAHDI_EVENT_POLARITY);
			if (debug & DEBUG_CARD) {
				dev_info(&wc->vb.pdev->dev,
					 "%s: Polarity NEGATIVE -> POSITIVE\n",
					 get_dahdi_chan(wc, mod)->name);
			}
		}
		break;
	case POLARITY_POSITIVE:
		if (!positive_polarity) {
			fxo->polarity_state = POLARITY_DEBOUNCE_NEGATIVE;
			fxo->poldebounce_timer = wc->framecount +
							POLARITY_DEBOUNCE;
		}
		break;
	case POLARITY_DEBOUNCE_NEGATIVE:
		if (positive_polarity) {
			fxo->polarity_state = POLARITY_POSITIVE;
		} else if (time_after(wc->framecount, fxo->poldebounce_timer)) {
			dahdi_qevent_lock(get_dahdi_chan(wc, mod),
					  DAHDI_EVENT_POLARITY);
			if (debug & DEBUG_CARD) {
				dev_info(&wc->vb.pdev->dev,
					 "%s: Polarity POSITIVE -> NEGATIVE\n",
					 get_dahdi_chan(wc, mod)->name);
			}
			fxo->polarity_state = POLARITY_NEGATIVE;
		}
		break;
	case POLARITY_NEGATIVE:
		if (positive_polarity) {
			fxo->polarity_state = POLARITY_DEBOUNCE_POSITIVE;
			fxo->poldebounce_timer = wc->framecount +
							POLARITY_DEBOUNCE;
		}
		break;
	};
}

static void
wctdm_voicedaa_check_hook(struct wctdm *wc, struct wctdm_module *const mod)
{
	signed char b;
	u8 abs_voltage;
	struct fxo *const fxo = &mod->mod.fxo;

	/* Try to track issues that plague slot one FXO's */
	b = fxo->hook_ring_shadow & 0x9b;

	if (fxo->offhook) {
		if (b != 0x9)
			wctdm_setreg_intr(wc, mod, 5, 0x9);
	} else {
		if (b != 0x8)
			wctdm_setreg_intr(wc, mod, 5, 0x8);

		wctdm_fxo_ring_detect(wc, mod);
	}

	abs_voltage = abs(fxo->line_voltage_status);

	if (fxovoltage && time_after(wc->framecount, fxo->display_fxovoltage)) {
		/* Every 100 ms */
		fxo->display_fxovoltage = wc->framecount + 100;
		dev_info(&wc->vb.pdev->dev,
			 "Port %d: Voltage: %d\n",
			 mod->card + 1, fxo->line_voltage_status);
	}

	if (unlikely(DAHDI_RXSIG_INITIAL == get_dahdi_chan(wc, mod)->rxhooksig)) {
		/*
		 * dahdi-base will set DAHDI_RXSIG_INITIAL after a
		 * DAHDI_STARTUP or DAHDI_CHANCONFIG ioctl so that new events
		 * will be queued on the channel with the current received
		 * hook state.  Channels that use robbed-bit signalling always
		 * report the current received state via the dahdi_rbsbits
		 * call. Since we only call dahdi_hooksig when we've detected
		 * a change to report, let's forget our current state in order
		 * to force us to report it again via dahdi_hooksig.
		 *
		 */
		fxo->battery_state = BATTERY_UNKNOWN;
	}

	if (abs_voltage < battthresh) {
		wctdm_fxo_stop_debouncing_polarity(wc, mod);
		wctdm_check_battery_lost(wc, mod);
	} else {
		wctdm_check_battery_present(wc, mod);
		wctdm_fxo_check_polarity(wc, mod,
					 (fxo->line_voltage_status > 0));
	}

	/* Look for neon mwi pulse */
	if (neonmwi_monitor && !fxo->offhook) {
		/* Look for 4 consecutive voltage readings
		* where the voltage is over the neon limit but
		* does not vary greatly from the last reading
		*/
		if (fxo->battery_state == BATTERY_PRESENT &&
				  abs_voltage > neonmwi_level &&
				  (0 == fxo->neonmwi_last_voltage ||
				  (fxo->line_voltage_status >= fxo->neonmwi_last_voltage - neonmwi_envelope &&
				  fxo->line_voltage_status <= fxo->neonmwi_last_voltage + neonmwi_envelope))) {
			fxo->neonmwi_last_voltage = fxo->line_voltage_status;
			if (NEONMWI_ON_DEBOUNCE == fxo->neonmwi_debounce) {
				fxo->neonmwi_offcounter = neonmwi_offlimit_cycles;
				if (0 == fxo->neonmwi_state) {
					dahdi_qevent_lock(get_dahdi_chan(wc, mod), DAHDI_EVENT_NEONMWI_ACTIVE);
					fxo->neonmwi_state = 1;
					if (debug)
						dev_info(&wc->vb.pdev->dev, "NEON MWI active for card %d\n", mod->card+1);
				}
				fxo->neonmwi_debounce++;  /* terminate the processing */
			} else if (NEONMWI_ON_DEBOUNCE > fxo->neonmwi_debounce) {
				fxo->neonmwi_debounce++;
			} else { /* Insure the count gets reset */
				fxo->neonmwi_offcounter = neonmwi_offlimit_cycles;
			}
		} else {
			fxo->neonmwi_debounce = 0;
			fxo->neonmwi_last_voltage = 0;
		}
		/* If no neon mwi pulse for given period of time, indicte no neon mwi state */
		if (fxo->neonmwi_state && 0 < fxo->neonmwi_offcounter ) {
			fxo->neonmwi_offcounter--;
			if (0 == fxo->neonmwi_offcounter) {
				dahdi_qevent_lock(get_dahdi_chan(wc, mod), DAHDI_EVENT_NEONMWI_INACTIVE);
				fxo->neonmwi_state = 0;
				if (debug)
					dev_info(&wc->vb.pdev->dev, "NEON MWI cleared for card %d\n", mod->card+1);
			}
		}
	}
#undef MS_PER_CHECK_HOOK
}

static void
wctdm_fxs_hooksig(struct wctdm *wc, struct wctdm_module *const mod,
		  enum dahdi_txsig txsig)
{
	int x = 0;
	unsigned long flags;
	struct fxs *const fxs = &mod->mod.fxs;

	spin_lock_irqsave(&wc->reglock, flags);
	switch (txsig) {
	case DAHDI_TXSIG_ONHOOK:
		switch (get_dahdi_chan(wc, mod)->sig) {
		case DAHDI_SIG_FXOGS:
			x = (POLARITY_XOR(fxs)) ?
					SLIC_LF_RING_OPEN :
					SLIC_LF_TIP_OPEN;
			break;
		case DAHDI_SIG_EM:
		case DAHDI_SIG_FXOKS:
		case DAHDI_SIG_FXOLS:
		default:
			x = fxs->idletxhookstate;
			break;
		}
		break;
	case DAHDI_TXSIG_OFFHOOK:
		switch (get_dahdi_chan(wc, mod)->sig) {
		case DAHDI_SIG_EM:
			x = (POLARITY_XOR(fxs)) ?
					SLIC_LF_ACTIVE_FWD :
					SLIC_LF_ACTIVE_REV;
			break;
		default:
			x = fxs->idletxhookstate;
			break;
		}
		break;
	case DAHDI_TXSIG_START:
		x = SLIC_LF_RINGING;
		break;
	case DAHDI_TXSIG_KEWL:
		x = SLIC_LF_OPEN;
		break;
	default:
		spin_unlock_irqrestore(&wc->reglock, flags);
		dev_notice(&wc->vb.pdev->dev,
			"wctdm24xxp: Can't set tx state to %d\n", txsig);
		return;
	}

	if (x != fxs->lasttxhook) {
		fxs->lasttxhook = x | SLIC_LF_OPPENDING;
		mod->sethook = CMD_WR(LINE_STATE, fxs->lasttxhook);
		fxs->oppending_timeout = wc->framecount + 100;
		spin_unlock_irqrestore(&wc->reglock, flags);

		if (debug & DEBUG_CARD) {
			dev_info(&wc->vb.pdev->dev, "Setting FXS hook state "
				 "to %d (%02x) framecount=%ld\n", txsig, x,
				 wc->framecount);
		}
	} else {
		spin_unlock_irqrestore(&wc->reglock, flags);
	}
}

static void wctdm_fxs_off_hook(struct wctdm *wc, struct wctdm_module *const mod)
{
	struct fxs *const fxs = &mod->mod.fxs;

	if (debug & DEBUG_CARD)
		dev_info(&wc->vb.pdev->dev,
			"fxs_off_hook: Card %d Going off hook\n", mod->card);
	switch (fxs->lasttxhook) {
	case SLIC_LF_RINGING:		/* Ringing */
	case SLIC_LF_OHTRAN_FWD:	/* Forward On Hook Transfer */
	case SLIC_LF_OHTRAN_REV:	/* Reverse On Hook Transfer */
		/* just detected OffHook, during Ringing or OnHookTransfer */
		fxs->idletxhookstate = POLARITY_XOR(fxs) ?
						SLIC_LF_ACTIVE_REV :
						SLIC_LF_ACTIVE_FWD;
		break;
	}
	if ((fxs->lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_OPEN)
		wctdm_fxs_hooksig(wc, mod, DAHDI_TXSIG_OFFHOOK);
	dahdi_hooksig(get_dahdi_chan(wc, mod), DAHDI_RXSIG_OFFHOOK);

#ifdef DEBUG
	if (robust)
		wctdm_init_proslic(wc, mod, 1, 0, 1);
#endif
}

/**
 * wctdm_fxs_on_hook - Report on hook to DAHDI.
 * @wc:		Board hosting the module.
 * @card:	Index of the module / port to place on hook.
 *
 * If we are intentionally dropping battery to signal a forward
 * disconnect we do not want to place the line "On-Hook". In this
 * case, the core of DAHDI will place us on hook when one of the RBS
 * timers expires.
 *
 */
static void wctdm_fxs_on_hook(struct wctdm *wc, struct wctdm_module *const mod)
{
	if (debug & DEBUG_CARD) {
		dev_info(&wc->vb.pdev->dev,
			"fxs_on_hook: Card %d Going on hook\n", mod->card);
	}

	if ((mod->mod.fxs.lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_OPEN)
		wctdm_fxs_hooksig(wc, mod, DAHDI_TXSIG_ONHOOK);
	dahdi_hooksig(get_dahdi_chan(wc, mod), DAHDI_RXSIG_ONHOOK);
}

static const char *wctdm_echocan_name(const struct dahdi_chan *chan)
{
	struct wctdm *wc = chan->pvt;
	if (wc->vpmadt032)
		return vpmadt032_name;
	else if (wc->vpmoct)
		return vpmoct_name;

	return NULL;
}

static int wctdm_echocan_create(struct dahdi_chan *chan,
				struct dahdi_echocanparams *ecp,
				struct dahdi_echocanparam *p,
				struct dahdi_echocan_state **ec)
{
	struct wctdm *wc = chan->pvt;
	struct wctdm_chan *wchan = container_of(chan, struct wctdm_chan, chan);
	const struct dahdi_echocan_ops *ops;
	const struct dahdi_echocan_features *features;
	enum adt_companding comp;


#ifdef VPM_SUPPORT
	if (!vpmsupport)
		return -ENODEV;
#endif
	if (wc->vpmadt032) {
		ops = &vpm_ec_ops;
		features = &vpm_ec_features;

		*ec = &wchan->ec;
		(*ec)->ops = ops;
		(*ec)->features = *features;

		comp = (DAHDI_LAW_ALAW == chan->span->deflaw) ?
					ADT_COMP_ALAW : ADT_COMP_ULAW;

		return vpmadt032_echocan_create(wc->vpmadt032, wchan->timeslot,
						comp, ecp, p);
	} else if (wc->vpmoct) {
		ops = &vpm_ec_ops;
		features = &vpm_ec_features;

		*ec = &wchan->ec;
		(*ec)->ops = ops;
		(*ec)->features = *features;
		return vpmoct_echocan_create(wc->vpmoct, wchan->timeslot,
					     chan->span->deflaw);
	} else {
		return -ENODEV;
	}
}

static void echocan_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec)
{
	struct wctdm *wc = chan->pvt;
	struct wctdm_chan *wchan = container_of(chan, struct wctdm_chan, chan);

	if (wc->vpmadt032) {
		memset(ec, 0, sizeof(*ec));
		vpmadt032_echocan_free(wc->vpmadt032, wchan->timeslot, ec);
	} else if (wc->vpmoct) {
		memset(ec, 0, sizeof(*ec));
		vpmoct_echocan_free(wc->vpmoct, wchan->timeslot);
	}
}

/* 1ms interrupt */
static void wctdm_isr_misc_fxs(struct wctdm *wc, struct wctdm_module *const mod)
{
	struct fxs *const fxs = &mod->mod.fxs;

	if (time_after(wc->framecount, fxs->check_alarm)) {
		/* Accept an alarm once per 10 seconds */
		fxs->check_alarm = wc->framecount + (1000*10);
		if (fxs->palarms)
			fxs->palarms--;
	}

	if (fxs->off_hook && !(fxs->hook_state_shadow & 1)) {
		wctdm_fxs_on_hook(wc, mod);
		fxs->off_hook = 0;
	} else if (!fxs->off_hook && (fxs->hook_state_shadow & 1)) {
		wctdm_fxs_off_hook(wc, mod);
		fxs->off_hook = 1;
	}

	wctdm_proslic_check_oppending(wc, mod);

	if (time_after(wc->framecount, fxs->check_proslic)) {
		fxs->check_proslic = wc->framecount + 250; /* every 250ms */
		wctdm_proslic_recheck_sanity(wc, mod);
	}

	if (SLIC_LF_RINGING == fxs->lasttxhook) {
		/* RINGing, prepare for OHT */
		fxs->ohttimer = wc->framecount + OHT_TIMER;
		/* OHT mode when idle */
		fxs->idletxhookstate = POLARITY_XOR(fxs) ? SLIC_LF_OHTRAN_REV :
							    SLIC_LF_OHTRAN_FWD;
	} else if (fxs->oht_active) {
		/* check if still OnHook */
		if (!fxs->off_hook) {
			if (time_before(wc->framecount, fxs->ohttimer))
				return;

			/* Switch to active */
			fxs->idletxhookstate = POLARITY_XOR(fxs) ? SLIC_LF_ACTIVE_REV :
								    SLIC_LF_ACTIVE_FWD;
			spin_lock(&wc->reglock);
			if (SLIC_LF_OHTRAN_FWD == fxs->lasttxhook) {
				/* Apply the change if appropriate */
				fxs->lasttxhook = SLIC_LF_OPPENDING | SLIC_LF_ACTIVE_FWD;
				/* Data enqueued here */
				mod->sethook = CMD_WR(LINE_STATE, fxs->lasttxhook);
				if (debug & DEBUG_CARD) {
					dev_info(&wc->vb.pdev->dev,
						 "Channel %d OnHookTransfer "
						 "stop\n", mod->card);
				}
			} else if (SLIC_LF_OHTRAN_REV == fxs->lasttxhook) {
				/* Apply the change if appropriate */
				fxs->lasttxhook = SLIC_LF_OPPENDING | SLIC_LF_ACTIVE_REV;
				/* Data enqueued here */
				mod->sethook = CMD_WR(LINE_STATE, fxs->lasttxhook);
				if (debug & DEBUG_CARD) {
					dev_info(&wc->vb.pdev->dev,
						 "Channel %d OnHookTransfer "
						 "stop\n", mod->card);
				}
			}
			spin_unlock(&wc->reglock);
		} else {
			fxs->oht_active = 0;
			/* Switch to active */
			fxs->idletxhookstate = POLARITY_XOR(fxs) ? SLIC_LF_ACTIVE_REV : SLIC_LF_ACTIVE_FWD;
			if (debug & DEBUG_CARD) {
				dev_info(&wc->vb.pdev->dev,
					 "Channel %d OnHookTransfer abort\n",
					 mod->card);
			}
		}

	}
}

/* 1ms interrupt */
static inline void wctdm_isr_misc(struct wctdm *wc)
{
	int x;

	if (unlikely(!is_initialized(wc)))
		return;

	for (x = 0; x < wc->mods_per_board; x++) {
		struct wctdm_module *const mod = &wc->mods[x];

		spin_lock(&wc->reglock);
		cmd_checkisr(wc, mod);
		spin_unlock(&wc->reglock);

		switch (mod->type) {
		case FXS:
			wctdm_isr_misc_fxs(wc, mod);
			break;
		case FXO:
			wctdm_voicedaa_check_hook(wc, mod);
			break;
		case QRV:
			wctdm_qrvdri_check_hook(wc, x);
			break;
		default:
			break;
		}
	}
}

static void handle_receive(struct voicebus *vb, struct list_head *buffers)
{
	struct wctdm *wc = container_of(vb, struct wctdm, vb);
	struct vbb *vbb;
	list_for_each_entry(vbb, buffers, entry)
		wctdm_receiveprep(wc, vbb->data);
}

static void handle_transmit(struct voicebus *vb, struct list_head *buffers)
{
	struct wctdm *wc = container_of(vb, struct wctdm, vb);
	struct vbb *vbb;

	list_for_each_entry(vbb, buffers, entry) {
		memset(vbb->data, 0, sizeof(vbb->data));
		wctdm_transmitprep(wc, vbb->data);
		wctdm_isr_misc(wc);
		wc->framecount++;
	}
}

struct sframe_packet {
	struct list_head node;
	u8 sframe[SFRAME_SIZE];
};

/**
 * handle_hx8_bootmode_receive() - queue up the receive packet for later...
 *
 * This function is called from interrupt context and isn't optimal, but it's
 * not the main code path.
 */
static void handle_hx8_bootmode_receive(struct wctdm *wc, const void *vbb)
{
	struct sframe_packet *frame;

	frame = kzalloc(sizeof(*frame), GFP_ATOMIC);
	if (unlikely(!frame)) {
		WARN_ON(1);
		return;
	}

	memcpy(frame->sframe, vbb, sizeof(frame->sframe));
	spin_lock(&wc->frame_list_lock);
	list_add_tail(&frame->node, &wc->frame_list);
	spin_unlock(&wc->frame_list_lock);

	/* Wake up anyone waiting for a new packet. */
	wake_up(&wc->regq);
	return;
}

static void handle_hx8_receive(struct voicebus *vb, struct list_head *buffers)
{
	struct wctdm *wc = container_of(vb, struct wctdm, vb);
	struct vbb *vbb;
	list_for_each_entry(vbb, buffers, entry)
		handle_hx8_bootmode_receive(wc, vbb->data);
}

static void handle_hx8_transmit(struct voicebus *vb, struct list_head *buffers)
{
	struct vbb *vbb, *n;

	list_for_each_entry_safe(vbb, n, buffers, entry) {
		list_del(&vbb->entry);
		dma_pool_free(vb->pool, vbb, vbb->dma_addr);
	}
}

static int wctdm_voicedaa_insane(struct wctdm *wc, struct wctdm_module *mod)
{
	int blah;
	blah = wctdm_getreg(wc, mod, 2);
	if (blah != 0x3)
		return -2;
	blah = wctdm_getreg(wc, mod, 11);
	if (debug & DEBUG_CARD) {
		dev_info(&wc->vb.pdev->dev,
			 "VoiceDAA System: %02x\n", blah & 0xf);
	}
	return 0;
}

static int
wctdm_proslic_insane(struct wctdm *wc, struct wctdm_module *const mod)
{
	int blah, reg1, insane_report;
	insane_report=0;

	blah = wctdm_getreg(wc, mod, 0);
	if (blah != 0xff && (debug & DEBUG_CARD)) {
		dev_info(&wc->vb.pdev->dev,
			 "ProSLIC on module %d, product %d, "
			 "version %d\n", mod->card, (blah & 0x30) >> 4,
			 (blah & 0xf));
	}

#if 0
	if ((blah & 0x30) >> 4) {
		dev_info(&wc->vb.pdev->dev,
			 "ProSLIC on module %d is not a 3210.\n", mod->card);
		return -1;
	}
#endif
	if (((blah & 0xf) == 0) || ((blah & 0xf) == 0xf)) {
		/* SLIC not loaded */
		return -1;
	}

	/* let's be really sure this is an FXS before we continue */
	reg1 = wctdm_getreg(wc, mod, 1);
	if ((0x80 != (blah & 0xf0)) || (0x88 != reg1)) {
		if (debug & DEBUG_CARD) {
			dev_info(&wc->vb.pdev->dev,
				 "DEBUG: not FXS b/c reg0=%x or "
				 "reg1 != 0x88 (%x).\n", blah, reg1);
		}
		return -1;
	}

	blah = wctdm_getreg(wc, mod, 8);
	if (blah != 0x2) {
		dev_notice(&wc->vb.pdev->dev,
			   "ProSLIC on module %d insane (1) %d should be 2\n",
			   mod->card, blah);
		return -1;
	} else if (insane_report) {
		dev_notice(&wc->vb.pdev->dev,
			   "ProSLIC on module %d Reg 8 Reads %d Expected "
			   "is 0x2\n", mod->card, blah);
	}

	blah = wctdm_getreg(wc, mod, 64);
	if (blah != 0x0) {
		dev_notice(&wc->vb.pdev->dev,
			   "ProSLIC on module %d insane (2)\n",
			   mod->card);
		return -1;
	} else if (insane_report) {
		dev_notice(&wc->vb.pdev->dev,
			   "ProSLIC on module %d Reg 64 Reads %d Expected "
			   "is 0x0\n", mod->card, blah);
	}

	blah = wctdm_getreg(wc, mod, 11);
	if (blah != 0x33) {
		dev_notice(&wc->vb.pdev->dev,
			   "ProSLIC on module %d insane (3)\n", mod->card);
		return -1;
	} else if (insane_report) {
		dev_notice(&wc->vb.pdev->dev,
			   "ProSLIC on module %d Reg 11 Reads %d "
			   "Expected is 0x33\n", mod->card, blah);
	}

	/* Just be sure it's setup right. */
	wctdm_setreg(wc, mod, 30, 0);

	if (debug & DEBUG_CARD) {
		dev_info(&wc->vb.pdev->dev,
			 "ProSLIC on module %d seems sane.\n", mod->card);
	}
	return 0;
}

static int
wctdm_proslic_powerleak_test(struct wctdm *wc, struct wctdm_module *const mod)
{
	unsigned long start;
	unsigned char vbat;

	/* Turn off linefeed */
	wctdm_setreg(wc, mod, LINE_STATE, 0);

	/* Power down */
	wctdm_setreg(wc, mod, 14, 0x10);

	start = jiffies;

	/* TODO: Why is this sleep necessary.  Without it, the first read
	 * comes back with a 0 value. */
	msleep(20);

	while ((vbat = wctdm_getreg(wc, mod, 82)) > 0x6) {
		if (time_after(jiffies, start + HZ/4))
			break;
	}

	if (vbat < 0x06) {
		dev_notice(&wc->vb.pdev->dev,
			   "Excessive leakage detected on module %d: %d "
			   "volts (%02x) after %d ms\n", mod->card,
			   376 * vbat / 1000, vbat,
			   (int)((jiffies - start) * 1000 / HZ));
		return -1;
	} else if (debug & DEBUG_CARD) {
		dev_info(&wc->vb.pdev->dev,
			 "Post-leakage voltage: %d volts\n", 376 * vbat / 1000);
	}
	return 0;
}

static int wctdm_powerup_proslic(struct wctdm *wc,
				 struct wctdm_module *mod, int fast)
{
	unsigned char vbat;
	unsigned long origjiffies;
	int lim;

	/* Set period of DC-DC converter to 1/64 khz */
	wctdm_setreg(wc, mod, 92, 0xc0 /* was 0xff */);

	/* Wait for VBat to powerup */
	origjiffies = jiffies;

	/* Disable powerdown */
	wctdm_setreg(wc, mod, 14, 0);

	/* If fast, don't bother checking anymore */
	if (fast)
		return 0;

	while ((vbat = wctdm_getreg(wc, mod, 82)) < 0xc0) {
		/* Wait no more than 500ms */
		if ((jiffies - origjiffies) > HZ/2) {
			break;
		}
	}

	if (vbat < 0xc0) {
		dev_notice(&wc->vb.pdev->dev, "ProSLIC on module %d failed to powerup within %d ms (%d mV only)\n\n -- DID YOU REMEMBER TO PLUG IN THE HD POWER CABLE TO THE TDM CARD??\n",
		       mod->card, (int)(((jiffies - origjiffies) * 1000 / HZ)),
			vbat * 375);
		return -1;
	} else if (debug & DEBUG_CARD) {
		dev_info(&wc->vb.pdev->dev,
			 "ProSLIC on module %d powered up to -%d volts (%02x) "
			 "in %d ms\n", mod->card, vbat * 376 / 1000, vbat,
			 (int)(((jiffies - origjiffies) * 1000 / HZ)));
	}

        /* Proslic max allowed loop current, reg 71 LOOP_I_LIMIT */
        /* If out of range, just set it to the default value     */
        lim = (loopcurrent - 20) / 3;
        if ( loopcurrent > 41 ) {
                lim = 0;
                if (debug & DEBUG_CARD)
                        dev_info(&wc->vb.pdev->dev, "Loop current out of range! Setting to default 20mA!\n");
        }
        else if (debug & DEBUG_CARD)
                        dev_info(&wc->vb.pdev->dev, "Loop current set to %dmA!\n",(lim*3)+20);
	wctdm_setreg(wc, mod, LOOP_I_LIMIT, lim);

	/* Engage DC-DC converter */
	wctdm_setreg(wc, mod, 93, 0x19 /* was 0x19 */);
	return 0;

}

static int
wctdm_proslic_manual_calibrate(struct wctdm *wc, struct wctdm_module *const mod)
{
	unsigned long origjiffies;
	unsigned char i;

	/* Disable all interupts in DR21-23 */
	wctdm_setreg(wc, mod, 21, 0);
	wctdm_setreg(wc, mod, 22, 0);
	wctdm_setreg(wc, mod, 23, 0);

	wctdm_setreg(wc, mod, 64, 0);

	/* (0x18) Calibrations without the ADC and DAC offset and without
	 * common mode calibration. */
	wctdm_setreg(wc, mod, 97, 0x18);

	/* (0x47) Calibrate common mode and differential DAC mode DAC + ILIM */
	wctdm_setreg(wc, mod, 96, 0x47);

	origjiffies=jiffies;
	while (wctdm_getreg(wc, mod, 96) != 0) {
		if ((jiffies-origjiffies) > 80)
			return -1;
	}
//Initialized DR 98 and 99 to get consistant results.
// 98 and 99 are the results registers and the search should have same intial conditions.

/*******************************The following is the manual gain mismatch calibration****************************/
/*******************************This is also available as a function *******************************************/
	msleep(10);
	wctdm_proslic_setreg_indirect(wc, mod, 88, 0);
	wctdm_proslic_setreg_indirect(wc, mod, 89, 0);
	wctdm_proslic_setreg_indirect(wc, mod, 90, 0);
	wctdm_proslic_setreg_indirect(wc, mod, 91, 0);
	wctdm_proslic_setreg_indirect(wc, mod, 92, 0);
	wctdm_proslic_setreg_indirect(wc, mod, 93, 0);

	/* This is necessary if the calibration occurs other than at reset */
	wctdm_setreg(wc, mod, 98, 0x10);
	wctdm_setreg(wc, mod, 99, 0x10);

	for ( i=0x1f; i>0; i--)
	{
		wctdm_setreg(wc, mod, 98, i);
		msleep(40);
		if ((wctdm_getreg(wc, mod, 88)) == 0)
			break;
	} // for

	for ( i=0x1f; i>0; i--)
	{
		wctdm_setreg(wc, mod, 99, i);
		msleep(40);
		if ((wctdm_getreg(wc, mod, 89)) == 0)
			break;
	}//for

/*******************************The preceding is the manual gain mismatch calibration****************************/
/**********************************The following is the longitudinal Balance Cal***********************************/
	wctdm_setreg(wc, mod, 64, 1);
	msleep(100);

	wctdm_setreg(wc, mod, 64, 0);

	/* enable interrupt for the balance Cal */
	wctdm_setreg(wc, mod, 23, 0x4);

	/* this is a singular calibration bit for longitudinal calibration */
	wctdm_setreg(wc, mod, 97, 0x1);
	wctdm_setreg(wc, mod, 96, 0x40);

	wctdm_getreg(wc, mod, 96); /* Read Reg 96 just cause */

	wctdm_setreg(wc, mod, 21, 0xFF);
	wctdm_setreg(wc, mod, 22, 0xFF);
	wctdm_setreg(wc, mod, 23, 0xFF);

	/**The preceding is the longitudinal Balance Cal***/
	return(0);

}

static int
wctdm_proslic_calibrate(struct wctdm *wc, struct wctdm_module *mod)
{
	unsigned long origjiffies;
	int x;

	/* Perform all calibrations */
	wctdm_setreg(wc, mod, 97, 0x1f);
	
	/* Begin, no speedup */
	wctdm_setreg(wc, mod, 96, 0x5f);

	/* Wait for it to finish */
	origjiffies = jiffies;
	while (wctdm_getreg(wc, mod, 96)) {
		if (time_after(jiffies, (origjiffies + (2*HZ)))) {
			dev_notice(&wc->vb.pdev->dev,
				   "Timeout waiting for calibration of "
				   "module %d\n", mod->card);
			return -1;
		}
	}
	
	if (debug & DEBUG_CARD) {
		/* Print calibration parameters */
		dev_info(&wc->vb.pdev->dev,
			 "Calibration Vector Regs 98 - 107:\n");
		for (x=98;x<108;x++) {
			dev_info(&wc->vb.pdev->dev,
				 "%d: %02x\n", x, wctdm_getreg(wc, mod, x));
		}
	}
	return 0;
}

/*********************************************************************
 * Set the hwgain on the analog modules
 *
 * card = the card position for this module (0-23)
 * gain = gain in dB x10 (e.g. -3.5dB  would be gain=-35)
 * tx = (0 for rx; 1 for tx)
 *
 *******************************************************************/
static int
wctdm_set_hwgain(struct wctdm *wc, struct wctdm_module *mod,
		 __s32 gain, __u32 tx)
{
	if (mod->type != FXO) {
		dev_notice(&wc->vb.pdev->dev,
			   "Cannot adjust gain. Unsupported module type!\n");
		return -1;
	}

	if (tx) {
		if (debug) {
			dev_info(&wc->vb.pdev->dev,
				 "setting FXO tx gain for card=%d to %d\n",
				 mod->card, gain);
		}
		if (gain >=  -150 && gain <= 0) {
			wctdm_setreg(wc, mod, 38, 16 + (gain / -10));
			wctdm_setreg(wc, mod, 40, 16 + (-gain % 10));
		} else if (gain <= 120 && gain > 0) {
			wctdm_setreg(wc, mod, 38, gain/10);
			wctdm_setreg(wc, mod, 40, (gain%10));
		} else {
			dev_notice(&wc->vb.pdev->dev,
				   "FXO tx gain is out of range (%d)\n", gain);
			return -1;
		}
	} else { /* rx */
		if (debug) {
			dev_info(&wc->vb.pdev->dev,
				 "setting FXO rx gain for card=%d to %d\n",
				 mod->card, gain);
		}
		if (gain >=  -150 && gain <= 0) {
			wctdm_setreg(wc, mod, 39, 16 + (gain / -10));
			wctdm_setreg(wc, mod, 41, 16 + (-gain % 10));
		} else if (gain <= 120 && gain > 0) {
			wctdm_setreg(wc, mod, 39, gain/10);
			wctdm_setreg(wc, mod, 41, (gain%10));
		} else {
			dev_notice(&wc->vb.pdev->dev,
				   "FXO rx gain is out of range (%d)\n", gain);
			return -1;
		}
	}

	return 0;
}

static int set_lasttxhook_interruptible(struct wctdm *wc, struct fxs *fxs,
					unsigned newval, int *psethook)
{
	int res = 0;
	unsigned long flags;
	int timeout = 0;

	do {
		spin_lock_irqsave(&wc->reglock, flags);
		if (SLIC_LF_OPPENDING & fxs->lasttxhook) {
			spin_unlock_irqrestore(&wc->reglock, flags);
			if (timeout++ > 100)
				return -1;
			msleep(1);
		} else {
			fxs->lasttxhook = (newval & SLIC_LF_SETMASK) | SLIC_LF_OPPENDING;
			*psethook = CMD_WR(LINE_STATE, fxs->lasttxhook);
			spin_unlock_irqrestore(&wc->reglock, flags);
			break;
		}
	} while (1);

	return res;
}

/* Must be called from within an interruptible context */
static int set_vmwi(struct wctdm *wc, struct wctdm_module *const mod)
{
	int x;
	struct fxs *const fxs = &mod->mod.fxs;

	/* Presently only supports line reversal MWI */
	if ((fxs->vmwi_active_messages) &&
	    (fxs->vmwisetting.vmwi_type & DAHDI_VMWI_LREV))
		fxs->vmwi_linereverse = 1;
	else
		fxs->vmwi_linereverse = 0;

	/* Set line polarity for new VMWI state */
	if (POLARITY_XOR(fxs)) {
		fxs->idletxhookstate |= SLIC_LF_REVMASK;
		/* Do not set while currently ringing or open */
		if (((fxs->lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_RINGING)  &&
		    ((fxs->lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_OPEN)) {
			x = fxs->lasttxhook;
			x |= SLIC_LF_REVMASK;
			set_lasttxhook_interruptible(wc, fxs, x, &mod->sethook);
		}
	} else {
		fxs->idletxhookstate &= ~SLIC_LF_REVMASK;
		/* Do not set while currently ringing or open */
		if (((fxs->lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_RINGING) &&
		    ((fxs->lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_OPEN)) {
			x = fxs->lasttxhook;
			x &= ~SLIC_LF_REVMASK;
			set_lasttxhook_interruptible(wc, fxs, x, &mod->sethook);
		}
	}
	if (debug) {
		dev_info(&wc->vb.pdev->dev,
			 "Setting VMWI on channel %d, messages=%d, lrev=%d\n",
			 mod->card, fxs->vmwi_active_messages,
			 fxs->vmwi_linereverse);
	}
	return 0;
}

static void
wctdm_voicedaa_set_ts(struct wctdm *wc, struct wctdm_module *mod, int ts)
{
	wctdm_setreg(wc, mod, 34, (ts * 8) & 0xff);
	wctdm_setreg(wc, mod, 35, (ts * 8) >> 8);
	wctdm_setreg(wc, mod, 36, (ts * 8) & 0xff);
	wctdm_setreg(wc, mod, 37, (ts * 8) >> 8);

	if (debug) {
		dev_info(&wc->vb.pdev->dev,
			 "voicedaa: card %d new timeslot: %d\n",
			 mod->card + 1, ts);
	}
}

static int
wctdm_init_voicedaa(struct wctdm *wc, struct wctdm_module *mod,
		    int fast, int manual, int sane)
{
	unsigned char reg16=0, reg26=0, reg30=0, reg31=0;
	unsigned long flags;
	long newjiffies;

#if 0	/* TODO */
	if ((wc->mods[card & 0xfc].type == QRV) ||
	    (wc->mods[card & 0xfc].type == BRI))
		return -2;
#endif

	spin_lock_irqsave(&wc->reglock, flags);
	mod->type = NONE;
	spin_unlock_irqrestore(&wc->reglock, flags);
	msleep(20);

	spin_lock_irqsave(&wc->reglock, flags);
	mod->type = FXO;
	spin_unlock_irqrestore(&wc->reglock, flags);
	msleep(20);

	if (!sane && wctdm_voicedaa_insane(wc, mod))
		return -2;

	/* Software reset */
	wctdm_setreg(wc, mod, 1, 0x80);
	msleep(100);

	/* Set On-hook speed, Ringer impedence, and ringer threshold */
	reg16 |= (fxo_modes[_opermode].ohs << 6);
	reg16 |= (fxo_modes[_opermode].rz << 1);
	reg16 |= (fxo_modes[_opermode].rt);
	wctdm_setreg(wc, mod, 16, reg16);

	/* Enable ring detector full-wave rectifier mode */
	wctdm_setreg(wc, mod, 18, 2);
	wctdm_setreg(wc, mod, 24, 0);
	
	/* Set DC Termination:
	   Tip/Ring voltage adjust, minimum operational current, current limitation */
	reg26 |= (fxo_modes[_opermode].dcv << 6);
	reg26 |= (fxo_modes[_opermode].mini << 4);
	reg26 |= (fxo_modes[_opermode].ilim << 1);
	wctdm_setreg(wc, mod, 26, reg26);

	/* Set AC Impedence */
	reg30 = (fxo_modes[_opermode].acim);
	wctdm_setreg(wc, mod, 30, reg30);

	/* Misc. DAA parameters */

	/* If fast pickup is set, then the off hook counter will be set to 8
	 * ms, otherwise 128 ms. */
	reg31 = (fastpickup) ? 0xe3 : 0xa3;

	reg31 |= (fxo_modes[_opermode].ohs2 << 3);
	wctdm_setreg(wc, mod, 31, reg31);

	wctdm_voicedaa_set_ts(wc, mod, mod->card);

	/* Enable ISO-Cap */
	wctdm_setreg(wc, mod, 6, 0x00);

	/* Turn off the calibration delay when fastpickup is enabled. */
	if (fastpickup)
		wctdm_setreg(wc, mod, 17, wctdm_getreg(wc, mod, 17) | 0x20);

	/* Wait 1000ms for ISO-cap to come up */
	newjiffies = jiffies;
	newjiffies += 2 * HZ;

	while ((jiffies < newjiffies) && !(wctdm_getreg(wc, mod, 11) & 0xf0))
		msleep(100);

	if (!(wctdm_getreg(wc, mod, 11) & 0xf0)) {
		dev_notice(&wc->vb.pdev->dev, "VoiceDAA did not bring up ISO link properly!\n");
		return -1;
	}

	if (debug & DEBUG_CARD) {
		dev_info(&wc->vb.pdev->dev, "ISO-Cap is now up, line side: %02x rev %02x\n", 
		       wctdm_getreg(wc, mod, 11) >> 4,
		       (wctdm_getreg(wc, mod, 13) >> 2) & 0xf);
	}

	/* Enable on-hook line monitor */
	wctdm_setreg(wc, mod, 5, 0x08);
	
	/* Take values for fxotxgain and fxorxgain and apply them to module */
	wctdm_set_hwgain(wc, mod, fxotxgain, 1);
	wctdm_set_hwgain(wc, mod, fxorxgain, 0);

#ifdef DEBUG
	if (digitalloopback) {
		dev_info(&wc->vb.pdev->dev,
			 "Turning on digital loopback for port %d.\n",
			 mod->card + 1);
		wctdm_setreg(wc, mod, 10, 0x01);
	}
#endif

	if (debug) {
		dev_info(&wc->vb.pdev->dev,
			 "DEBUG fxotxgain:%i.%i fxorxgain:%i.%i\n",
			 (wctdm_getreg(wc, mod, 38)/16) ?
				-(wctdm_getreg(wc, mod, 38) - 16) :
				wctdm_getreg(wc, mod, 38),
			 (wctdm_getreg(wc, mod, 40)/16) ?
				-(wctdm_getreg(wc, mod, 40) - 16) :
				wctdm_getreg(wc, mod, 40),
			 (wctdm_getreg(wc, mod, 39)/16) ?
				-(wctdm_getreg(wc, mod, 39) - 16) :
				wctdm_getreg(wc, mod, 39),
			 (wctdm_getreg(wc, mod, 41)/16) ?
				-(wctdm_getreg(wc, mod, 41) - 16) :
				wctdm_getreg(wc, mod, 41));
	}
	
	return 0;
}

static void
wctdm_proslic_set_ts(struct wctdm *wc, struct wctdm_module *mod, int ts)
{
	wctdm_setreg(wc, mod, 2, (ts * 8) & 0xff); /* Tx Start low byte  0 */
	wctdm_setreg(wc, mod, 3, (ts * 8) >> 8);   /* Tx Start high byte 0 */
	wctdm_setreg(wc, mod, 4, (ts * 8) & 0xff); /* Rx Start low byte  0 */
	wctdm_setreg(wc, mod, 5, (ts * 8) >> 8);   /* Rx Start high byte 0 */

	if (debug) {
		dev_info(&wc->vb.pdev->dev,
			 "proslic: card %d new timeslot: %d\n",
			 mod->card + 1, ts);
	}
}

static int
wctdm_init_proslic(struct wctdm *wc, struct wctdm_module *const mod,
		   int fast, int manual, int sane)
{

	struct fxs *const fxs = &mod->mod.fxs;
	unsigned short tmp[5];
	unsigned long flags;
	unsigned char r19,r9;
	int x;
	int fxsmode=0;
	int addresses[NUM_CAL_REGS];

#if 0 /* TODO */
	if (wc->mods[mod->card & 0xfc].type == QRV)
		return -2;
#endif

	spin_lock_irqsave(&wc->reglock, flags);
	mod->type = FXS;
	spin_unlock_irqrestore(&wc->reglock, flags);

	/* msleep(100); */

	/* Sanity check the ProSLIC */
	if (!sane && wctdm_proslic_insane(wc, mod))
		return -2;

	/* Initialize VMWI settings */
	memset(&(fxs->vmwisetting), 0, sizeof(fxs->vmwisetting));
	fxs->vmwi_linereverse = 0;

	/* By default, don't send on hook */
	if (!reversepolarity != !fxs->reversepolarity)
		fxs->idletxhookstate = SLIC_LF_ACTIVE_REV;
	else
		fxs->idletxhookstate = SLIC_LF_ACTIVE_FWD;

	if (sane) {
		/* Make sure we turn off the DC->DC converter to prevent anything from blowing up */
		wctdm_setreg(wc, mod, 14, 0x10);
	}

	if (wctdm_proslic_init_indirect_regs(wc, mod)) {
		dev_info(&wc->vb.pdev->dev,
			 "Indirect Registers failed to initialize on "
			 "module %d.\n", mod->card);
		return -1;
	}

	/* Clear scratch pad area */
	wctdm_proslic_setreg_indirect(wc, mod, 97, 0);

	/* Clear digital loopback */
	wctdm_setreg(wc, mod, 8, 0);

	/* Revision C optimization */
	wctdm_setreg(wc, mod, 108, 0xeb);

	/* Disable automatic VBat switching for safety to prevent
	 * Q7 from accidently turning on and burning out.
	 * If pulse dialing has trouble at high REN loads change this to 0x17 */
	wctdm_setreg(wc, mod, 67, 0x07);

	/* Turn off Q7 */
	wctdm_setreg(wc, mod, 66, 1);

	/* Flush ProSLIC digital filters by setting to clear, while
	   saving old values */
	for (x=0;x<5;x++) {
		tmp[x] = wctdm_proslic_getreg_indirect(wc, mod, x + 35);
		wctdm_proslic_setreg_indirect(wc, mod, x + 35, 0x8000);
	}

	/* Power up the DC-DC converter */
	if (wctdm_powerup_proslic(wc, mod, fast)) {
		dev_notice(&wc->vb.pdev->dev,
			   "Unable to do INITIAL ProSLIC powerup on "
			   "module %d\n", mod->card);
		return -1;
	}

	if (!fast) {
		/* Check for power leaks */
		if (wctdm_proslic_powerleak_test(wc, mod)) {
			dev_notice(&wc->vb.pdev->dev,
				   "ProSLIC module %d failed leakage test. "
				   "Check for short circuit\n", mod->card);
		}
		/* Power up again */
		if (wctdm_powerup_proslic(wc, mod, fast)) {
			dev_notice(&wc->vb.pdev->dev,
				   "Unable to do FINAL ProSLIC powerup on "
				   "module %d\n", mod->card);
			return -1;
		}
#ifndef NO_CALIBRATION
		/* Perform calibration */
		if (manual) {
			if (wctdm_proslic_manual_calibrate(wc, mod)) {
				//dev_notice(&wc->vb.pdev->dev, "Proslic failed on Manual Calibration\n");
				if (wctdm_proslic_manual_calibrate(wc, mod)) {
					dev_notice(&wc->vb.pdev->dev, "Proslic Failed on Second Attempt to Calibrate Manually. (Try -DNO_CALIBRATION in Makefile)\n");
					return -1;
				}
				dev_info(&wc->vb.pdev->dev, "Proslic Passed Manual Calibration on Second Attempt\n");
			}
		}
		else {
			if (wctdm_proslic_calibrate(wc, mod))  {
				//dev_notice(&wc->vb.pdev->dev, "ProSlic died on Auto Calibration.\n");
				if (wctdm_proslic_calibrate(wc, mod)) {
					dev_notice(&wc->vb.pdev->dev, "Proslic Failed on Second Attempt to Auto Calibrate\n");
					return -1;
				}
				dev_info(&wc->vb.pdev->dev, "Proslic Passed Auto Calibration on Second Attempt\n");
			}
		}
		/* Perform DC-DC calibration */
		wctdm_setreg(wc, mod, 93, 0x99);
		r19 = wctdm_getreg(wc, mod, 107);
		if ((r19 < 0x2) || (r19 > 0xd)) {
			dev_notice(&wc->vb.pdev->dev, "DC-DC cal has a surprising direct 107 of 0x%02x!\n", r19);
			wctdm_setreg(wc, mod, 107, 0x8);
		}

		/* Save calibration vectors */
		for (x = 0; x < NUM_CAL_REGS; x++)
			addresses[x] = 96 + x;
		wctdm_getregs(wc, mod, addresses, ARRAY_SIZE(addresses));
		for (x = 0; x < NUM_CAL_REGS; x++)
			fxs->calregs.vals[x] = addresses[x];
#endif

	} else {
		/* Restore calibration registers */
		for (x = 0; x < NUM_CAL_REGS; x++)
			wctdm_setreg(wc, mod, 96 + x, fxs->calregs.vals[x]);
	}
	/* Calibration complete, restore original values */
	for (x=0;x<5;x++) {
		wctdm_proslic_setreg_indirect(wc, mod, x + 35, tmp[x]);
	}

	if (wctdm_proslic_verify_indirect_regs(wc, mod)) {
		dev_info(&wc->vb.pdev->dev, "Indirect Registers failed verification.\n");
		return -1;
	}


#if 0
    /* Disable Auto Power Alarm Detect and other "features" */
    wctdm_setreg(wc, card, 67, 0x0e);
    blah = wctdm_getreg(wc, card, 67);
#endif

#if 0
    if (wctdm_proslic_setreg_indirect(wc, card, 97, 0x0)) { // Stanley: for the bad recording fix
		 dev_info(&wc->vb.pdev->dev, "ProSlic IndirectReg Died.\n");
		 return -1;
	}
#endif

	/* U-Law 8-bit interface */
	wctdm_proslic_set_ts(wc, mod, mod->card);

	wctdm_setreg(wc, mod, 18, 0xff); /* clear all interrupt */
	wctdm_setreg(wc, mod, 19, 0xff);
	wctdm_setreg(wc, mod, 20, 0xff);
	wctdm_setreg(wc, mod, 22, 0xff);
	wctdm_setreg(wc, mod, 73, 0x04);

	wctdm_setreg(wc, mod, 69, 0x4);

	if (fxshonormode) {
		static const int ACIM2TISS[16] = { 0x0, 0x1, 0x4, 0x5, 0x7,
						   0x0, 0x0, 0x6, 0x0, 0x0,
						   0x0, 0x2, 0x0, 0x3 };
		fxsmode = ACIM2TISS[fxo_modes[_opermode].acim];
		wctdm_setreg(wc, mod, 10, 0x08 | fxsmode);
		if (fxo_modes[_opermode].ring_osc) {
			wctdm_proslic_setreg_indirect(wc, mod, 20,
					fxo_modes[_opermode].ring_osc);
		}
		if (fxo_modes[_opermode].ring_x) {
			wctdm_proslic_setreg_indirect(wc, mod, 21,
					fxo_modes[_opermode].ring_x);
		}
	}
	if (lowpower)
		wctdm_setreg(wc, mod, 72, 0x10);

#if 0
    wctdm_setreg(wc, card, 21, 0x00); 	// enable interrupt
    wctdm_setreg(wc, card, 22, 0x02); 	// Loop detection interrupt
    wctdm_setreg(wc, card, 23, 0x01); 	// DTMF detection interrupt
#endif

#if 0
    /* Enable loopback */
    wctdm_setreg(wc, card, 8, 0x2);
    wctdm_setreg(wc, card, 14, 0x0);
    wctdm_setreg(wc, card, 64, 0x0);
    wctdm_setreg(wc, card, 1, 0x08);
#endif

	if (fastringer) {
		/* Speed up Ringer */
		wctdm_proslic_setreg_indirect(wc, mod, 20, 0x7e6d);
		wctdm_proslic_setreg_indirect(wc, mod, 21, 0x01b9);
		/* Beef up Ringing voltage to 89V */
		if (boostringer) {
			wctdm_setreg(wc, mod, 74, 0x3f);
			if (wctdm_proslic_setreg_indirect(wc, mod, 21, 0x247))
				return -1;
			dev_info(&wc->vb.pdev->dev,
				 "Boosting fast ringer on slot %d (89V peak)\n",
				 mod->card + 1);
		} else if (lowpower) {
			if (wctdm_proslic_setreg_indirect(wc, mod, 21, 0x14b))
				return -1;
			dev_info(&wc->vb.pdev->dev,
				 "Reducing fast ring power on slot %d "
				 "(50V peak)\n", mod->card + 1);
		} else
			dev_info(&wc->vb.pdev->dev,
				 "Speeding up ringer on slot %d (25Hz)\n",
				 mod->card + 1);
	} else {
		/* Beef up Ringing voltage to 89V */
		if (boostringer) {
			wctdm_setreg(wc, mod, 74, 0x3f);
			if (wctdm_proslic_setreg_indirect(wc, mod, 21, 0x1d1))
				return -1;
			dev_info(&wc->vb.pdev->dev,
				 "Boosting ringer on slot %d (89V peak)\n",
				 mod->card + 1);
		} else if (lowpower) {
			if (wctdm_proslic_setreg_indirect(wc, mod, 21, 0x108))
				return -1;
			dev_info(&wc->vb.pdev->dev,
				 "Reducing ring power on slot %d "
				 "(50V peak)\n", mod->card + 1);
		}
	}

	if (fxstxgain || fxsrxgain) {
		r9 = wctdm_getreg(wc, mod, 9);
		switch (fxstxgain) {
		
			case 35:
				r9+=8;
				break;
			case -35:
				r9+=4;
				break;
			case 0: 
				break;
		}
	
		switch (fxsrxgain) {
			
			case 35:
				r9+=2;
				break;
			case -35:
				r9+=1;
				break;
			case 0:
				break;
		}
		wctdm_setreg(wc, mod, 9, r9);
	}

	if (debug) {
		dev_info(&wc->vb.pdev->dev,
			 "DEBUG: fxstxgain:%s fxsrxgain:%s\n",
			 ((wctdm_getreg(wc, mod, 9) / 8) == 1) ?
				"3.5" : (((wctdm_getreg(wc, mod, 9) / 4) == 1) ?
							"-3.5" : "0.0"),
			 ((wctdm_getreg(wc, mod, 9) / 2) == 1) ?
				"3.5" : ((wctdm_getreg(wc, mod, 9) % 2) ?
							"-3.5" : "0.0"));
	}

	fxs->lasttxhook = fxs->idletxhookstate;
	wctdm_setreg(wc, mod, LINE_STATE, fxs->lasttxhook);

	/* Preset the shadow register so that we won't get a power alarm when
	 * we finish initialization, otherwise the line state register may not
	 * have been read yet. */
	fxs->linefeed_control_shadow = fxs->lasttxhook;
	return 0;
}

static void
wctdm_qrvdri_set_ts(struct wctdm *wc, struct wctdm_module *mod, int ts)
{
	wctdm_setreg(wc, mod, 0x13, ts + 0x80);	/* codec 2 tx, ts0 */
	wctdm_setreg(wc, mod, 0x17, ts + 0x80);	/* codec 0 rx, ts0 */
	wctdm_setreg(wc, mod, 0x14, ts + 0x81);	/* codec 1 tx, ts1 */
	wctdm_setreg(wc, mod, 0x18, ts + 0x81);	/* codec 1 rx, ts1 */

	if (debug) {
		dev_info(&wc->vb.pdev->dev,
			 "qrvdri: card %d new timeslot: %d\n",
			 mod->card + 1, ts);
	}
}

static int wctdm_init_qrvdri(struct wctdm *wc, int card)
{
	struct wctdm_module *const mod = &wc->mods[card];
	unsigned char x,y;

	if (BRI == wc->mods[card & 0xfc].type)
		return -2;

	/* have to set this, at least for now */
	mod->type = QRV;
	if (!(card & 3)) { /* if at base of card, reset and write it */
		struct qrv *const qrv = &mod->mod.qrv;
		struct qrv *const qrv1 = &wc->mods[card + 1].mod.qrv;
		struct qrv *const qrv2 = &wc->mods[card + 2].mod.qrv;
		struct qrv *const qrv3 = &wc->mods[card + 3].mod.qrv;

		wctdm_setreg(wc, mod, 0, 0x80);
		wctdm_setreg(wc, mod, 0, 0x55);
		wctdm_setreg(wc, mod, 1, 0x69);
		qrv->hook = qrv1->hook = 0;
		qrv2->hook = qrv3->hook = 0xff;
		qrv->debouncetime = qrv1->debouncetime = QRV_DEBOUNCETIME;
		qrv->debtime = qrv1->debtime = 0;
		qrv->radmode = qrv1->radmode = 0;
		qrv->txgain = qrv1->txgain = 3599;
		qrv->rxgain = qrv1->rxgain = 1199;
	} else { /* channel is on same card as base, no need to test */
		if (wc->mods[card & 0x7c].type == QRV) {
			/* only lower 2 are valid */
			if (!(card & 2))
				return 0;
		}
		mod->type = NONE;
		return 1;
	}
	x = wctdm_getreg(wc, mod, 0);
	y = wctdm_getreg(wc, mod, 1);
	/* if not a QRV card, return as such */
	if ((x != 0x55) || (y != 0x69))
	{
		mod->type = NONE;
		return 1;
	}
	for (x = 0; x < 0x30; x++) {
		if ((x >= 0x1c) && (x <= 0x1e))
			wctdm_setreg(wc, mod, x, 0xff);
		else
			wctdm_setreg(wc, mod, x, 0);
	}
	wctdm_setreg(wc, mod, 0, 0x80);
	msleep(100);
	wctdm_setreg(wc, mod, 0, 0x10);
	wctdm_setreg(wc, mod, 0, 0x10);
	msleep(100);
	/* set up modes */
	wctdm_setreg(wc, mod, 0, 0x1c);
	/* set up I/O directions */
	wctdm_setreg(wc, mod, 1, 0x33);
	wctdm_setreg(wc, mod, 2, 0x0f);
	wctdm_setreg(wc, mod, 5, 0x0f);
	/* set up I/O to quiescent state */
	wctdm_setreg(wc, mod, 3, 0x11);  /* D0-7 */
	wctdm_setreg(wc, mod, 4, 0xa);  /* D8-11 */
	wctdm_setreg(wc, mod, 7, 0);  /* CS outputs */

	wctdm_qrvdri_set_ts(wc, mod, card);

	/* set up for max gains */
	wctdm_setreg(wc, mod, 0x26, 0x24);
	wctdm_setreg(wc, mod, 0x27, 0x24);
	wctdm_setreg(wc, mod, 0x0b, 0x01);  /* "Transmit" gain codec 0 */
	wctdm_setreg(wc, mod, 0x0c, 0x01);  /* "Transmit" gain codec 1 */
	wctdm_setreg(wc, mod, 0x0f, 0xff);  /* "Receive" gain codec 0 */
	wctdm_setreg(wc, mod, 0x10, 0xff);  /* "Receive" gain codec 1 */
	return 0;
}

static void qrv_dosetup(struct dahdi_chan *chan, struct wctdm *wc)
{
	struct wctdm_module *qrvmod;
	struct wctdm_module *nextqrvmod;
	int qrvcard;
	unsigned char r;
	long l;

	/* actually do something with the values */
	qrvcard = (chan->chanpos - 1) & 0xfc;
	qrvmod = &wc->mods[qrvcard];
	nextqrvmod = &wc->mods[qrvcard + 1];
	if (debug) {
		dev_info(&wc->vb.pdev->dev,
			 "@@@@@ radmodes: %d,%d  rxgains: %d,%d   "
			 "txgains: %d,%d\n", wc->mods[qrvcard].mod.qrv.radmode,
			 nextqrvmod->mod.qrv.radmode,
			 wc->mods[qrvcard].mod.qrv.rxgain,
			 nextqrvmod->mod.qrv.rxgain,
			 wc->mods[qrvcard].mod.qrv.txgain,
			 nextqrvmod->mod.qrv.txgain);
	}
	r = 0;
	if (qrvmod->mod.qrv.radmode & RADMODE_DEEMP)
		r |= 4;
	if (nextqrvmod->mod.qrv.radmode & RADMODE_DEEMP)
		r |= 8;
	if (qrvmod->mod.qrv.rxgain < 1200)
		r |= 1;
	if (nextqrvmod->mod.qrv.rxgain < 1200)
		r |= 2;
	wctdm_setreg(wc, qrvmod, 7, r);
	if (debug) dev_info(&wc->vb.pdev->dev, "@@@@@ setting reg 7 to %02x hex\n",r);
	r = 0;
	if (qrvmod->mod.qrv.radmode & RADMODE_PREEMP)
		r |= 3;
	else if (qrvmod->mod.qrv.txgain >= 3600)
		r |= 1;
	else if (qrvmod->mod.qrv.txgain >= 1200)
		r |= 2;
	if (nextqrvmod->mod.qrv.radmode & RADMODE_PREEMP)
		r |= 0xc;
	else if (nextqrvmod->mod.qrv.txgain >= 3600)
		r |= 4;
	else if (nextqrvmod->mod.qrv.txgain >= 1200)
		r |= 8;
	wctdm_setreg(wc, qrvmod, 4, r);
	if (debug) dev_info(&wc->vb.pdev->dev, "@@@@@ setting reg 4 to %02x hex\n",r);
	r = 0;
	if (qrvmod->mod.qrv.rxgain >= 2400)
		r |= 1;
	if (nextqrvmod->mod.qrv.rxgain >= 2400)
		r |= 2;
	wctdm_setreg(wc, qrvmod, 0x25, r);
	if (debug) dev_info(&wc->vb.pdev->dev, "@@@@@ setting reg 0x25 to %02x hex\n",r);
	r = 0;
	if (qrvmod->mod.qrv.txgain < 2400)
		r |= 1;
	else
		r |= 4;
	if (nextqrvmod->mod.qrv.txgain < 2400)
		r |= 8;
	else
		r |= 0x20;
	wctdm_setreg(wc, qrvmod, 0x26, r);
	if (debug) dev_info(&wc->vb.pdev->dev, "@@@@@ setting reg 0x26 to %02x hex\n",r);
	l = ((long)(qrvmod->mod.qrv.rxgain % 1200) * 10000) / 46875;
	if (l == 0) l = 1;
	if (qrvmod->mod.qrv.rxgain >= 2400)
		l += 181;
	wctdm_setreg(wc, qrvmod, 0x0b, (unsigned char)l);
	if (debug) dev_info(&wc->vb.pdev->dev, "@@@@@ setting reg 0x0b to %02x hex\n",(unsigned char)l);
	l = ((long)(nextqrvmod->mod.qrv.rxgain % 1200) * 10000) / 46875;
	if (l == 0) l = 1;
	if (nextqrvmod->mod.qrv.rxgain >= 2400)
		l += 181;
	wctdm_setreg(wc, qrvmod, 0x0c, (unsigned char)l);
	if (debug) dev_info(&wc->vb.pdev->dev, "@@@@@ setting reg 0x0c to %02x hex\n",(unsigned char)l);
	l = ((long)(qrvmod->mod.qrv.txgain % 1200) * 10000) / 46875;
	if (l == 0) l = 1;
	wctdm_setreg(wc, qrvmod, 0x0f, (unsigned char)l);
	if (debug) dev_info(&wc->vb.pdev->dev, "@@@@@ setting reg 0x0f to %02x hex\n", (unsigned char)l);
	l = ((long)(nextqrvmod->mod.qrv.txgain % 1200) * 10000) / 46875;
	if (l == 0) l = 1;
	wctdm_setreg(wc, qrvmod, 0x10, (unsigned char)l);
	if (debug) dev_info(&wc->vb.pdev->dev, "@@@@@ setting reg 0x10 to %02x hex\n",(unsigned char)l);
	return;
}

static void wctdm24xxp_get_fxs_regs(struct wctdm *wc, struct wctdm_module *mod,
				    struct wctdm_regs *regs)
{
	int  x;

	for (x = 0; x < NUM_INDIRECT_REGS; x++)
		regs->indirect[x] = wctdm_proslic_getreg_indirect(wc, mod, x);

	for (x = 0; x < NUM_REGS; x++)
		regs->direct[x] = wctdm_getreg(wc, mod, x);
}

static void wctdm24xxp_get_fxo_regs(struct wctdm *wc, struct wctdm_module *mod,
				    struct wctdm_regs *regs)
{
	int  x;
	for (x = 0; x < NUM_FXO_REGS; x++)
		regs->direct[x] = wctdm_getreg(wc, mod, x);
}

static void wctdm24xxp_get_qrv_regs(struct wctdm *wc, struct wctdm_module *mod,
				    struct wctdm_regs *regs)
{
	int  x;
	for (x = 0; x < 0x32; x++)
		regs->direct[x] = wctdm_getreg(wc, mod, x);
}

static int wctdm_ioctl(struct dahdi_chan *chan, unsigned int cmd, unsigned long data)
{
	struct wctdm_stats stats;
	struct wctdm_regop regop;
	struct wctdm_echo_coefs echoregs;
	struct dahdi_hwgain hwgain;
	struct wctdm *wc = chan->pvt;
	int x;
	union {
		struct dahdi_radio_stat s;
		struct dahdi_radio_param p;
	} stack;
	struct wctdm_module *const mod = &wc->mods[chan->chanpos - 1];
	struct fxs *const fxs = &mod->mod.fxs;

	switch (cmd) {
	case DAHDI_ONHOOKTRANSFER:
		if (mod->type != FXS)
			return -EINVAL;
		if (get_user(x, (__user int *) data))
			return -EFAULT;

		/* Active mode when idle */
		fxs->idletxhookstate = POLARITY_XOR(fxs) ?
						SLIC_LF_ACTIVE_REV :
						SLIC_LF_ACTIVE_FWD;

		if (((fxs->lasttxhook & SLIC_LF_SETMASK) == SLIC_LF_ACTIVE_FWD) ||
		    ((fxs->lasttxhook & SLIC_LF_SETMASK) == SLIC_LF_ACTIVE_REV)) {
			int res;

			res = set_lasttxhook_interruptible(wc, fxs,
				(POLARITY_XOR(fxs) ?
				SLIC_LF_OHTRAN_REV : SLIC_LF_OHTRAN_FWD),
				&mod->sethook);

			if (debug & DEBUG_CARD) {
				if (res) {
					dev_info(&wc->vb.pdev->dev,
						 "Channel %d TIMEOUT: "
						 "OnHookTransfer start\n",
						 chan->chanpos - 1);
				} else {
					dev_info(&wc->vb.pdev->dev,
						 "Channel %d OnHookTransfer "
						 "start\n", chan->chanpos - 1);
				}
			}

		}

		fxs->ohttimer = wc->framecount + x;
		fxs->oht_active = 1;

		break;
	case DAHDI_VMWI_CONFIG:
		if (mod->type != FXS)
			return -EINVAL;
		if (copy_from_user(&(fxs->vmwisetting),
				   (__user void *)data,
				   sizeof(fxs->vmwisetting)))
			return -EFAULT;
		set_vmwi(wc, mod);
		break;
	case DAHDI_VMWI:
		if (mod->type != FXS)
			return -EINVAL;
		if (get_user(x, (__user int *) data))
			return -EFAULT;
		if (0 > x)
			return -EFAULT;
		fxs->vmwi_active_messages = x;
		set_vmwi(wc, mod);
		break;
	case WCTDM_GET_STATS:
		if (mod->type == FXS) {
			stats.tipvolt = wctdm_getreg(wc, mod, 80) * -376;
			stats.ringvolt = wctdm_getreg(wc, mod, 81) * -376;
			stats.batvolt = wctdm_getreg(wc, mod, 82) * -376;
		} else if (mod->type == FXO) {
			stats.tipvolt = (s8)wctdm_getreg(wc, mod, 29) * 1000;
			stats.ringvolt = (s8)wctdm_getreg(wc, mod, 29) * 1000;
			stats.batvolt = (s8)wctdm_getreg(wc, mod, 29) * 1000;
		} else 
			return -EINVAL;
		if (copy_to_user((__user void *) data, &stats, sizeof(stats)))
			return -EFAULT;
		break;
	case WCTDM_GET_REGS:
	{
		struct wctdm_regs *regs = kzalloc(sizeof(*regs), GFP_KERNEL);
		if (!regs)
			return -ENOMEM;

		if (mod->type == FXS)
			wctdm24xxp_get_fxs_regs(wc, mod, regs);
		else if (mod->type == QRV)
			wctdm24xxp_get_qrv_regs(wc, mod, regs);
		else
			wctdm24xxp_get_fxo_regs(wc, mod, regs);

		if (copy_to_user((__user void *)data, regs, sizeof(*regs))) {
			kfree(regs);
			return -EFAULT;
		}

		kfree(regs);
		break;
	}
	case WCTDM_SET_REG:
		if (copy_from_user(&regop, (__user void *) data, sizeof(regop)))
			return -EFAULT;
		if (regop.indirect) {
			if (mod->type != FXS)
				return -EINVAL;
			dev_info(&wc->vb.pdev->dev, "Setting indirect %d to 0x%04x on %d\n", regop.reg, regop.val, chan->chanpos);
			wctdm_proslic_setreg_indirect(wc, mod, regop.reg,
						      regop.val);
		} else {
			regop.val &= 0xff;
			if (regop.reg == LINE_STATE) {
				/* Set feedback register to indicate the new state that is being set */
				fxs->lasttxhook = (regop.val & 0x0f) |  SLIC_LF_OPPENDING;
			}
			dev_info(&wc->vb.pdev->dev, "Setting direct %d to %04x on %d\n", regop.reg, regop.val, chan->chanpos);
			wctdm_setreg(wc, mod, regop.reg, regop.val);
		}
		break;
	case WCTDM_SET_ECHOTUNE:
		dev_info(&wc->vb.pdev->dev, "-- Setting echo registers: \n");
		if (copy_from_user(&echoregs, (__user void *) data, sizeof(echoregs)))
			return -EFAULT;

		if (mod->type == FXO) {
			/* Set the ACIM register */
			wctdm_setreg(wc, mod, 30, echoregs.acim);

			/* Set the digital echo canceller registers */
			wctdm_setreg(wc, mod, 45, echoregs.coef1);
			wctdm_setreg(wc, mod, 46, echoregs.coef2);
			wctdm_setreg(wc, mod, 47, echoregs.coef3);
			wctdm_setreg(wc, mod, 48, echoregs.coef4);
			wctdm_setreg(wc, mod, 49, echoregs.coef5);
			wctdm_setreg(wc, mod, 50, echoregs.coef6);
			wctdm_setreg(wc, mod, 51, echoregs.coef7);
			wctdm_setreg(wc, mod, 52, echoregs.coef8);

			dev_info(&wc->vb.pdev->dev, "-- Set echo registers successfully\n");

			break;
		} else {
			return -EINVAL;

		}
		break;
	case DAHDI_SET_HWGAIN:
		if (copy_from_user(&hwgain, (__user void *) data, sizeof(hwgain)))
			return -EFAULT;

		wctdm_set_hwgain(wc, mod, hwgain.newgain, hwgain.tx);

		if (debug)
			dev_info(&wc->vb.pdev->dev, "Setting hwgain on channel %d to %d for %s direction\n", 
				chan->chanpos-1, hwgain.newgain, hwgain.tx ? "tx" : "rx");
		break;
#ifdef VPM_SUPPORT
	case DAHDI_TONEDETECT:
		/* Hardware DTMF detection is not supported. */
		return -ENOSYS;
#endif
	case DAHDI_SETPOLARITY:
		if (get_user(x, (__user int *) data))
			return -EFAULT;
		if (mod->type != FXS)
			return -EINVAL;
		/* Can't change polarity while ringing or when open */
		if (((fxs->lasttxhook & SLIC_LF_SETMASK) == SLIC_LF_RINGING) ||
		    ((fxs->lasttxhook & SLIC_LF_SETMASK) == SLIC_LF_OPEN)) {
			if (debug & DEBUG_CARD) {
				dev_info(&wc->vb.pdev->dev,
					 "Channel %d Unable to Set Polarity\n", chan->chanpos - 1);
			}
			return -EINVAL;
		}

		fxs->reversepolarity = (x) ? 1 : 0;

		if (POLARITY_XOR(fxs)) {
			fxs->idletxhookstate |= SLIC_LF_REVMASK;
			x = fxs->lasttxhook & SLIC_LF_SETMASK;
			x |= SLIC_LF_REVMASK;
			if (x != fxs->lasttxhook) { 
				x = set_lasttxhook_interruptible(wc, fxs, x,
								 &mod->sethook);
				if ((debug & DEBUG_CARD) && x) {
					dev_info(&wc->vb.pdev->dev,
						 "Channel %d TIMEOUT: Set Reverse "
						 "Polarity\n", chan->chanpos - 1);
				} else if (debug & DEBUG_CARD) {
					dev_info(&wc->vb.pdev->dev,
						 "Channel %d Set Reverse Polarity\n",
						 chan->chanpos - 1);
				}
			}
		} else {
			fxs->idletxhookstate &= ~SLIC_LF_REVMASK;
			x = fxs->lasttxhook & SLIC_LF_SETMASK;
			x &= ~SLIC_LF_REVMASK;
			if (x != fxs->lasttxhook) { 
				x = set_lasttxhook_interruptible(wc, fxs, x,
								 &mod->sethook);
				if ((debug & DEBUG_CARD) & x) {
					dev_info(&wc->vb.pdev->dev,
						 "Channel %d TIMEOUT: Set Normal "
						 "Polarity\n", chan->chanpos - 1);
				} else if (debug & DEBUG_CARD) {
					dev_info(&wc->vb.pdev->dev,
						 "Channel %d Set Normal Polarity\n",
						 chan->chanpos - 1);
				}
			}
		}
		break;
	case DAHDI_RADIO_GETPARAM:
		if (mod->type != QRV)
			return -ENOTTY;
		if (copy_from_user(&stack.p, (__user void *) data, sizeof(stack.p)))
			return -EFAULT;
		stack.p.data = 0; /* start with 0 value in output */
		switch(stack.p.radpar) {
		case DAHDI_RADPAR_INVERTCOR:
			if (mod->mod.qrv.radmode & RADMODE_INVERTCOR)
				stack.p.data = 1;
			break;
		case DAHDI_RADPAR_IGNORECOR:
			if (mod->mod.qrv.radmode & RADMODE_IGNORECOR)
				stack.p.data = 1;
			break;
		case DAHDI_RADPAR_IGNORECT:
			if (mod->mod.qrv.radmode & RADMODE_IGNORECT)
				stack.p.data = 1;
			break;
		case DAHDI_RADPAR_EXTRXTONE:
			stack.p.data = 0;
			if (mod->mod.qrv.radmode & RADMODE_EXTTONE) {
				stack.p.data = 1;
				if (mod->mod.qrv.radmode & RADMODE_EXTINVERT)
					stack.p.data = 2;
			}
			break;
		case DAHDI_RADPAR_DEBOUNCETIME:
			stack.p.data = mod->mod.qrv.debouncetime;
			break;
		case DAHDI_RADPAR_RXGAIN:
			stack.p.data = mod->mod.qrv.rxgain - 1199;
			break;
		case DAHDI_RADPAR_TXGAIN:
			stack.p.data = mod->mod.qrv.txgain - 3599;
			break;
		case DAHDI_RADPAR_DEEMP:
			stack.p.data = 0;
			if (mod->mod.qrv.radmode & RADMODE_DEEMP)
				stack.p.data = 1;
			break;
		case DAHDI_RADPAR_PREEMP:
			stack.p.data = 0;
			if (mod->mod.qrv.radmode & RADMODE_PREEMP)
				stack.p.data = 1;
			break;
		default:
			return -EINVAL;
		}
		if (copy_to_user((__user void *) data, &stack.p, sizeof(stack.p)))
		    return -EFAULT;
		break;
	case DAHDI_RADIO_SETPARAM:
		if (mod->type != QRV)
			return -ENOTTY;
		if (copy_from_user(&stack.p, (__user void *) data, sizeof(stack.p)))
			return -EFAULT;
		switch(stack.p.radpar) {
		case DAHDI_RADPAR_INVERTCOR:
			if (stack.p.data)
				mod->mod.qrv.radmode |= RADMODE_INVERTCOR;
			else
				mod->mod.qrv.radmode &= ~RADMODE_INVERTCOR;
			return 0;
		case DAHDI_RADPAR_IGNORECOR:
			if (stack.p.data)
				mod->mod.qrv.radmode |= RADMODE_IGNORECOR;
			else
				mod->mod.qrv.radmode &= ~RADMODE_IGNORECOR;
			return 0;
		case DAHDI_RADPAR_IGNORECT:
			if (stack.p.data)
				mod->mod.qrv.radmode |= RADMODE_IGNORECT;
			else
				mod->mod.qrv.radmode &= ~RADMODE_IGNORECT;
			return 0;
		case DAHDI_RADPAR_EXTRXTONE:
			if (stack.p.data)
				mod->mod.qrv.radmode |= RADMODE_EXTTONE;
			else
				mod->mod.qrv.radmode &= ~RADMODE_EXTTONE;
			if (stack.p.data > 1)
				mod->mod.qrv.radmode |= RADMODE_EXTINVERT;
			else
				mod->mod.qrv.radmode &= ~RADMODE_EXTINVERT;
			return 0;
		case DAHDI_RADPAR_DEBOUNCETIME:
			mod->mod.qrv.debouncetime = stack.p.data;
			return 0;
		case DAHDI_RADPAR_RXGAIN:
			/* if out of range */
			if ((stack.p.data <= -1200) || (stack.p.data > 1552))
			{
				return -EINVAL;
			}
			mod->mod.qrv.rxgain = stack.p.data + 1199;
			break;
		case DAHDI_RADPAR_TXGAIN:
			/* if out of range */
			if (mod->mod.qrv.radmode & RADMODE_PREEMP) {
				if ((stack.p.data <= -2400) ||
				    (stack.p.data > 0))
					return -EINVAL;
			} else {
				if ((stack.p.data <= -3600) ||
				    (stack.p.data > 1200))
					return -EINVAL;
			}
			mod->mod.qrv.txgain = stack.p.data + 3599;
			break;
		case DAHDI_RADPAR_DEEMP:
			if (stack.p.data)
				mod->mod.qrv.radmode |= RADMODE_DEEMP;
			else
				mod->mod.qrv.radmode &= ~RADMODE_DEEMP;
			mod->mod.qrv.rxgain = 1199;
			break;
		case DAHDI_RADPAR_PREEMP:
			if (stack.p.data)
				mod->mod.qrv.radmode |= RADMODE_PREEMP;
			else
				mod->mod.qrv.radmode &= ~RADMODE_PREEMP;
			mod->mod.qrv.txgain = 3599;
			break;
		default:
			return -EINVAL;
		}
		qrv_dosetup(chan,wc);
		return 0;				
	default:
		return -ENOTTY;
	}
	return 0;
}

static int wctdm_open(struct dahdi_chan *chan)
{
	struct wctdm *const wc = chan->pvt;
	unsigned long flags;
	struct wctdm_module *const mod = &wc->mods[chan->chanpos - 1];

#if 0
	if (wc->dead)
		return -ENODEV;
#endif
	if (mod->type == FXO) {
		/* Reset the mwi indicators */
		spin_lock_irqsave(&wc->reglock, flags);
		mod->mod.fxo.neonmwi_debounce = 0;
		mod->mod.fxo.neonmwi_offcounter = 0;
		mod->mod.fxo.neonmwi_state = 0;
		spin_unlock_irqrestore(&wc->reglock, flags);
	}

	return 0;
}

static inline struct wctdm *span_to_wctdm(struct dahdi_span *span)
{
	struct wctdm_span *s = container_of(span, struct wctdm_span, span);
	return s->wc;
}

static int wctdm_watchdog(struct dahdi_span *span, int event)
{
	struct wctdm *wc = span_to_wctdm(span);
	dev_info(&wc->vb.pdev->dev, "TDM: Called watchdog\n");
	return 0;
}

static int wctdm_close(struct dahdi_chan *chan)
{
	struct wctdm *wc;
	int x;
	signed char reg;

	wc = chan->pvt;
	for (x = 0; x < wc->mods_per_board; x++) {
		struct wctdm_module *const mod = &wc->mods[x];
		if (FXS == mod->type) {
			mod->mod.fxs.idletxhookstate =
			    POLARITY_XOR(&mod->mod.fxs) ? SLIC_LF_ACTIVE_REV :
							  SLIC_LF_ACTIVE_FWD;
		} else if (QRV == mod->type) {
			int qrvcard = x & 0xfc;

			mod->mod.qrv.hook = 0;
			wc->mods[x + 2].mod.qrv.hook = 0xff;
			mod->mod.qrv.debouncetime = QRV_DEBOUNCETIME;
			mod->mod.qrv.debtime = 0;
			mod->mod.qrv.radmode = 0;
			mod->mod.qrv.txgain = 3599;
			mod->mod.qrv.rxgain = 1199;
			reg = 0;
			if (!wc->mods[qrvcard].mod.qrv.hook)
				reg |= 1;
			if (!wc->mods[qrvcard + 1].mod.qrv.hook)
				reg |= 0x10;
			wc->mods[qrvcard].sethook = CMD_WR(3, reg);
			qrv_dosetup(chan,wc);
		}
	}

	return 0;
}

static int wctdm_hooksig(struct dahdi_chan *chan, enum dahdi_txsig txsig)
{
	struct wctdm *wc = chan->pvt;
	int reg = 0;
	struct wctdm_module *const mod = &wc->mods[chan->chanpos - 1];

	if (mod->type == QRV) {
		const int qrvcard = (chan->chanpos - 1) & 0xfc;

		switch(txsig) {
		case DAHDI_TXSIG_START:
		case DAHDI_TXSIG_OFFHOOK:
			mod->mod.qrv.hook = 1;
			break;
		case DAHDI_TXSIG_ONHOOK:
			mod->mod.qrv.hook = 0;
			break;
		default:
			dev_notice(&wc->vb.pdev->dev, "wctdm24xxp: Can't set tx state to %d\n", txsig);
		}
		reg = 0;
		if (!wc->mods[qrvcard].mod.qrv.hook)
			reg |= 1;
		if (!wc->mods[qrvcard + 1].mod.qrv.hook)
			reg |= 0x10;
		wc->mods[qrvcard].sethook = CMD_WR(3, reg);
		/* wctdm_setreg(wc, qrvcard, 3, reg); */
	} else if (mod->type == FXO) {
		switch(txsig) {
		case DAHDI_TXSIG_START:
		case DAHDI_TXSIG_OFFHOOK:
			mod->mod.fxo.offhook = 1;
			mod->sethook = CMD_WR(5, 0x9);
			/* wctdm_setreg(wc, chan->chanpos - 1, 5, 0x9); */
			break;
		case DAHDI_TXSIG_ONHOOK:
			mod->mod.fxo.offhook = 0;
			mod->sethook = CMD_WR(5, 0x8);
			/* wctdm_setreg(wc, chan->chanpos - 1, 5, 0x8); */
			break;
		default:
			dev_notice(&wc->vb.pdev->dev, "wctdm24xxp: Can't set tx state to %d\n", txsig);
		}
	} else if (mod->type == FXS) {
		wctdm_fxs_hooksig(wc, mod, txsig);
	}
	return 0;
}

static void wctdm_dacs_connect(struct wctdm *wc, int srccard, int dstcard)
{
	struct wctdm_module *const srcmod = &wc->mods[srccard];
	struct wctdm_module *const dstmod = &wc->mods[dstcard];
	unsigned int type;

	if (wc->mods[dstcard].dacssrc > -1) {
		dev_notice(&wc->vb.pdev->dev, "wctdm_dacs_connect: Can't have double sourcing yet!\n");
		return;
	}
	type = wc->mods[srccard].type;
	if ((type == FXS) || (type == FXO)) {
		dev_notice(&wc->vb.pdev->dev,
			   "wctdm_dacs_connect: Unsupported modtype for "
			   "card %d\n", srccard);
		return;
	}
	type = wc->mods[dstcard].type;
	if ((type != FXS) && (type != FXO)) {
		dev_notice(&wc->vb.pdev->dev,
			   "wctdm_dacs_connect: Unsupported modtype "
			   "for card %d\n", dstcard);
		return;
	}

	if (debug) {
		dev_info(&wc->vb.pdev->dev,
			 "connect %d => %d\n", srccard, dstcard);
	}

	dstmod->dacssrc = srccard;

	/* make srccard transmit to srccard+24 on the TDM bus */
	if (srcmod->type == FXS) {
		/* proslic */
		wctdm_setreg(wc, srcmod, PCM_XMIT_START_COUNT_LSB,
			     ((srccard+24) * 8) & 0xff);
		wctdm_setreg(wc, srcmod, PCM_XMIT_START_COUNT_MSB,
			     ((srccard+24) * 8) >> 8);
	} else if (srcmod->type == FXO) {
		/* daa TX */
		wctdm_setreg(wc, srcmod, 34, ((srccard+24) * 8) & 0xff);
		wctdm_setreg(wc, srcmod, 35, ((srccard+24) * 8) >> 8);
	}

	/* have dstcard receive from srccard+24 on the TDM bus */
	if (dstmod->type == FXS) {
		/* proslic */
		wctdm_setreg(wc, dstmod, PCM_RCV_START_COUNT_LSB,
			     ((srccard+24) * 8) & 0xff);
		wctdm_setreg(wc, dstmod, PCM_RCV_START_COUNT_MSB,
			     ((srccard+24) * 8) >> 8);
	} else if (dstmod->type == FXO) {
		/* daa RX */
		wctdm_setreg(wc, dstmod, 36, ((srccard+24) * 8) & 0xff);
		wctdm_setreg(wc, dstmod, 37, ((srccard+24) * 8) >> 8);
	}
}

static void wctdm_dacs_disconnect(struct wctdm *wc, int card)
{
	struct wctdm_module *const mod = &wc->mods[card];
	struct wctdm_module *dacssrc;

	if (mod->dacssrc <= -1)
		return;

	dacssrc = &wc->mods[mod->dacssrc];

	if (debug) {
		dev_info(&wc->vb.pdev->dev, "wctdm_dacs_disconnect: "
			 "restoring TX for %d and RX for %d\n",
			 mod->dacssrc, card);
	}

	/* restore TX (source card) */
	if (dacssrc->type == FXS) {
		wctdm_setreg(wc, dacssrc, PCM_XMIT_START_COUNT_LSB,
			     (mod->dacssrc * 8) & 0xff);
		wctdm_setreg(wc, dacssrc, PCM_XMIT_START_COUNT_MSB,
			     (mod->dacssrc * 8) >> 8);
	} else if (dacssrc->type == FXO) {
		wctdm_setreg(wc, mod, 34, (card * 8) & 0xff);
		wctdm_setreg(wc, mod, 35, (card * 8) >> 8);
	} else {
		dev_warn(&wc->vb.pdev->dev,
			 "WARNING: wctdm_dacs_disconnect() called "
			 "on unsupported modtype\n");
	}

	/* restore RX (this card) */
	if (FXS == mod->type) {
		wctdm_setreg(wc, mod, PCM_RCV_START_COUNT_LSB,
			     (card * 8) & 0xff);
		wctdm_setreg(wc, mod, PCM_RCV_START_COUNT_MSB,
			     (card * 8) >> 8);
	} else if (FXO == mod->type) {
		wctdm_setreg(wc, mod, 36, (card * 8) & 0xff);
		wctdm_setreg(wc, mod, 37, (card * 8) >> 8);
	} else {
		dev_warn(&wc->vb.pdev->dev,
			 "WARNING: wctdm_dacs_disconnect() called "
			 "on unsupported modtype\n");
	}

	mod->dacssrc = -1;
}

static int wctdm_dacs(struct dahdi_chan *dst, struct dahdi_chan *src)
{
	struct wctdm *wc;

	if (!nativebridge)
		return 0; /* should this return -1 since unsuccessful? */

	wc = dst->pvt;

	if (src) {
		wctdm_dacs_connect(wc, src->chanpos - 1, dst->chanpos - 1);
		if (debug)
			dev_info(&wc->vb.pdev->dev, "dacs connecct: %d -> %d!\n\n", src->chanpos, dst->chanpos);
	} else {
		wctdm_dacs_disconnect(wc, dst->chanpos - 1);
		if (debug)
			dev_info(&wc->vb.pdev->dev, "dacs disconnect: %d!\n", dst->chanpos);
	}
	return 0;
}

/**
 * wctdm_wait_for_ready
 *
 * Check if the board has finished any setup and is ready to start processing
 * calls.
 */
int wctdm_wait_for_ready(struct wctdm *wc)
{
	while (!is_initialized(wc)) {
		if (fatal_signal_pending(current))
			return -EIO;
		msleep_interruptible(250);
	}
	return 0;
}

static int wctdm_enable_hw_preechocan(struct dahdi_chan *chan)
{
	struct wctdm *wc = chan->pvt;
	struct wctdm_chan *wchan = container_of(chan, struct wctdm_chan, chan);

	if (!wc->vpmoct)
		return 0;

	return vpmoct_preecho_enable(wc->vpmoct, wchan->timeslot);
}

static void wctdm_disable_hw_preechocan(struct dahdi_chan *chan)
{
	struct wctdm *wc = chan->pvt;
	struct wctdm_chan *wchan = container_of(chan, struct wctdm_chan, chan);

	if (!wc->vpmoct)
		return;

	vpmoct_preecho_disable(wc->vpmoct, wchan->timeslot);
}

/**
 * wctdm_chanconfig - Called when the channels are being configured.
 *
 * Ensure that the card is completely ready to go before we allow the channels
 * to be completely configured. This is to allow lengthy initialization
 * actions to take place in background on driver load and ensure we're synced
 * up by the time dahdi_cfg is run.
 *
 */
static int
wctdm_chanconfig(struct file *file, struct dahdi_chan *chan, int sigtype)
{
	struct wctdm *wc = chan->pvt;

	if ((file->f_flags & O_NONBLOCK) && !is_initialized(wc))
		return -EAGAIN;

	return wctdm_wait_for_ready(wc);
}

/*
 * wctdm24xxp_assigned - Called when span is assigned.
 * @span:	The span that is now assigned.
 *
 * This function is called by the core of DAHDI after the span number and
 * channel numbers have been assigned.
 *
 */
static void wctdm24xxp_assigned(struct dahdi_span *span)
{
	struct dahdi_span *s;
	struct dahdi_device *ddev = span->parent;
	struct wctdm *wc = NULL;

	list_for_each_entry(s, &ddev->spans, device_node) {
		wc = (container_of(s, struct wctdm_span, span))->wc;
		if (!test_bit(DAHDI_FLAGBIT_REGISTERED, &s->flags))
			return;
	}

	if (wc) {
		WARN_ON(0 == wc->not_ready);
		--wc->not_ready;
	}
}

static const struct dahdi_span_ops wctdm24xxp_analog_span_ops = {
	.owner = THIS_MODULE,
	.hooksig = wctdm_hooksig,
	.open = wctdm_open,
	.close = wctdm_close,
	.ioctl = wctdm_ioctl,
	.watchdog = wctdm_watchdog,
	.chanconfig = wctdm_chanconfig,
	.dacs = wctdm_dacs,
	.assigned = wctdm24xxp_assigned,
#ifdef VPM_SUPPORT
	.enable_hw_preechocan = wctdm_enable_hw_preechocan,
	.disable_hw_preechocan = wctdm_disable_hw_preechocan,
	.echocan_create = wctdm_echocan_create,
	.echocan_name = wctdm_echocan_name,
#endif
};

static const struct dahdi_span_ops wctdm24xxp_digital_span_ops = {
	.owner = THIS_MODULE,
	.open = wctdm_open,
	.close = wctdm_close,
	.ioctl = wctdm_ioctl,
	.watchdog = wctdm_watchdog,
	.hdlc_hard_xmit = wctdm_hdlc_hard_xmit,
	.spanconfig = b400m_spanconfig,
	.chanconfig = b400m_chanconfig,
	.dacs = wctdm_dacs,
	.assigned = wctdm24xxp_assigned,
#ifdef VPM_SUPPORT
	.enable_hw_preechocan = wctdm_enable_hw_preechocan,
	.disable_hw_preechocan = wctdm_disable_hw_preechocan,
	.echocan_create = wctdm_echocan_create,
	.echocan_name = wctdm_echocan_name,
#endif
};

static struct wctdm_chan *
wctdm_init_chan(struct wctdm *wc, struct wctdm_span *s, int chanoffset,
		int channo, unsigned int card_position)
{
	struct wctdm_chan *c;

	c = kzalloc(sizeof(*c), GFP_KERNEL);
	if (!c)
		return NULL;

	/* Do not change the procfs representation for non-hx8 cards. */
	if (dahdi_is_digital_span(&s->span)) {
		sprintf(c->chan.name, "WCBRI/%d/%d/%d", card_position,
			s->spanno, channo);
	} else {
		sprintf(c->chan.name, "WCTDM/%d/%d", card_position, channo);
	}

	c->chan.chanpos = channo+1;
	c->chan.span = &s->span;
	c->chan.pvt = wc;
	c->timeslot = chanoffset + channo;
	return c;
}

#if 0
/**
 * wctdm_span_count() - Return the number of spans exported by this board.
 *
 * This is only called during initialization so let's just count the spans each
 * time we need this information as opposed to storing another variable in the
 * wctdm structure.
 */
static int wctdm_span_count(const struct wctdm *wc)
{
	int i;
	int count = 0;
	for (i = 0; i < MAX_SPANS; ++i) {
		if (wc->spans[i])
			++count;
	}
	return count;
}
#endif

static struct wctdm_span *
wctdm_init_span(struct wctdm *wc, int spanno, int chanoffset, int chancount,
		int digital_span, unsigned int card_position)
{
	int x;
	struct wctdm_chan *c;
	struct wctdm_span *s;
	static int spancount;

	s = kzalloc(sizeof(*s), GFP_KERNEL);
	if (!s)
		return NULL;

	/* DAHDI stuff */
	s->span.offset = spanno;

	s->spanno = spancount++;
	s->wc = wc;

	/* Do not change the procfs representation for non-hx8 cards. */
	if (digital_span)
		sprintf(s->span.name, "WCBRI/%d/%d", card_position, s->spanno);
	else
		sprintf(s->span.name, "WCTDM/%d", card_position);

	snprintf(s->span.desc, sizeof(s->span.desc) - 1, "%s", wc->desc->name);

	if (wc->companding == DAHDI_LAW_DEFAULT) {
		if (wc->digi_mods || digital_span)
			/* If we have a BRI module, Auto set to alaw */
			s->span.deflaw = DAHDI_LAW_ALAW;
		else
			/* Auto set to ulaw */
			s->span.deflaw = DAHDI_LAW_MULAW;
	} else if (wc->companding == DAHDI_LAW_ALAW) {
		/* Force everything to alaw */
		s->span.deflaw = DAHDI_LAW_ALAW;
	} else {
		/* Auto set to ulaw */
		s->span.deflaw = DAHDI_LAW_MULAW;
	}

	if (digital_span) {
		s->span.ops = &wctdm24xxp_digital_span_ops;
		s->span.linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_B8ZS | DAHDI_CONFIG_D4;
		s->span.linecompat |= DAHDI_CONFIG_ESF | DAHDI_CONFIG_HDB3 | DAHDI_CONFIG_CCS | DAHDI_CONFIG_CRC4;
		s->span.linecompat |= DAHDI_CONFIG_NTTE | DAHDI_CONFIG_TERM;
		s->span.spantype = SPANTYPE_DIGITAL_BRI_TE;
	} else {
		s->span.ops = &wctdm24xxp_analog_span_ops;
		s->span.flags = DAHDI_FLAG_RBS;
		s->span.spantype = SPANTYPE_ANALOG_MIXED;
		/* analog sigcap handled in fixup_analog_span() */
	}

	s->span.chans = kmalloc(sizeof(struct dahdi_chan *) * chancount, GFP_KERNEL);
	if (!s->span.chans)
		return NULL;

	/* allocate channels for the span */
	for (x = 0; x < chancount; x++) {
		c = wctdm_init_chan(wc, s, chanoffset, x, card_position);
		if (!c)
			return NULL;
		wc->chans[chanoffset + x] = c;
		s->span.chans[x] = &c->chan;
	}

	s->span.channels = chancount;

	if (digital_span) {
		wc->chans[chanoffset + 0]->chan.sigcap = DAHDI_SIG_CLEAR;
		wc->chans[chanoffset + 1]->chan.sigcap = DAHDI_SIG_CLEAR;
		wc->chans[chanoffset + 2]->chan.sigcap = DAHDI_SIG_HARDHDLC;
	}

	wc->spans[spanno] = s;
	return s;
}

/**
 * should_set_alaw() - Should be called after all the spans are initialized.
 *
 * Returns true if the module companding should be set to alaw, otherwise
 * false.
 */
static bool should_set_alaw(const struct wctdm *wc)
{
	if (DAHDI_LAW_DEFAULT == wc->companding)
		return (wc->digi_mods > 0);
	else if (DAHDI_LAW_ALAW == wc->companding)
		return true;
	else
		return false;
}

static void wctdm_fixup_analog_span(struct wctdm *wc, int spanno)
{
	struct dahdi_span *s;
	int x, y;

	/* Finalize signalling  */
	y = 0;
	s = &wc->spans[spanno]->span;

	for (x = 0; x < wc->desc->ports; x++) {
		struct wctdm_module *const mod = &wc->mods[x];
		if (debug) {
			dev_info(&wc->vb.pdev->dev,
				 "fixup_analog: x=%d, y=%d modtype=%d, "
				 "s->chans[%d]=%p\n", x, y, mod->type,
				 y, s->chans[y]);
		}
		if (mod->type == FXO) {
			int val;
			s->chans[y++]->sigcap = DAHDI_SIG_FXSKS | DAHDI_SIG_FXSLS | DAHDI_SIG_SF | DAHDI_SIG_CLEAR;
			val = should_set_alaw(wc) ? 0x20 : 0x28;
#ifdef DEBUG
			val = (digitalloopback) ? 0x30 : val;
#endif
			wctdm_setreg(wc, mod, 33, val);
		} else if (mod->type == FXS) {
			s->chans[y++]->sigcap = DAHDI_SIG_FXOKS | DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS | DAHDI_SIG_SF | DAHDI_SIG_EM | DAHDI_SIG_CLEAR;
			wctdm_setreg(wc, mod, 1,
				     (should_set_alaw(wc) ? 0x20 : 0x28));
		} else if (mod->type == QRV) {
			s->chans[y++]->sigcap = DAHDI_SIG_SF | DAHDI_SIG_EM | DAHDI_SIG_CLEAR;
		} else {
			s->chans[y++]->sigcap = 0;
		}
	}
}

static int wctdm_initialize_vpmadt032(struct wctdm *wc)
{
	int res;
	struct vpmadt032_options options;
	struct vpmadt032 *vpm;
	unsigned long flags;

	options.debug = debug;
	options.vpmnlptype = vpmnlptype;
	options.vpmnlpthresh = vpmnlpthresh;
	options.vpmnlpmaxsupp = vpmnlpmaxsupp;
	if (is_hx8(wc)) {
		/* Hybrid cards potentially have 3 channels of EC on their
		 * ports since they may be BRI spans. */
		options.channels = 3 * wc->desc->ports;
	} else {
		options.channels = wc->desc->ports;
	}

	BUG_ON(options.channels > 24);

	wc->vpmadt032 = vpmadt032_alloc(&options);
	if (!wc->vpmadt032)
		return -ENOMEM;

	wc->vpmadt032->setchanconfig_from_state = setchanconfig_from_state;

	/* Pull the configuration information from the span holding
	 * the analog channels. */
	res = vpmadt032_test(wc->vpmadt032, &wc->vb);
	if (!res)
		res = vpmadt032_init(wc->vpmadt032);
	if (res) {
		vpm = wc->vpmadt032;
		spin_lock_irqsave(&wc->reglock, flags);
		wc->vpmadt032 = NULL;
		spin_unlock_irqrestore(&wc->reglock, flags);
		vpmadt032_free(vpm);
		return res;
	}

	/* Now we need to configure the VPMADT032 module for this
	 * particular board. */
	res = config_vpmadt032(wc->vpmadt032, wc);
	if (res) {
		vpm = wc->vpmadt032;
		spin_lock_irqsave(&wc->reglock, flags);
		wc->vpmadt032 = NULL;
		spin_unlock_irqrestore(&wc->reglock, flags);
		vpmadt032_free(vpm);
		return res;
	}

	return 0;
}

static void wctdm_vpm_load_complete(struct device *dev, bool operational)
{
	unsigned long flags;
	struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
	struct wctdm *wc = pci_get_drvdata(pdev);
	struct vpmoct *vpm = NULL;

	WARN_ON(!wc || !wc->not_ready);
	if (!wc || !wc->not_ready)
		return;

	spin_lock_irqsave(&wc->reglock, flags);
	wc->not_ready--;
	if (operational) {
		wc->ctlreg |= 0x10;
	} else {
		vpm = wc->vpmoct;
		wc->vpmoct = NULL;
	}
	spin_unlock_irqrestore(&wc->reglock, flags);

	if (vpm)
		vpmoct_free(vpm);
}

static int wctdm_initialize_vpm(struct wctdm *wc, unsigned long unused)
{
	int res = 0;

	if (!vpmsupport) {
		dev_notice(&wc->vb.pdev->dev, "VPM: Support Disabled\n");
		return 0;
	}

	res = wctdm_initialize_vpmadt032(wc);
	if (!res) {
		wc->ctlreg |= 0x10;
		return 0;
	} else {
		struct vpmoct *vpm;
		unsigned long flags;

		vpm = vpmoct_alloc();
		if (!vpm) {
			dev_info(&wc->vb.pdev->dev,
			    "Unable to allocate memory for struct vpmoct\n");
			return -ENOMEM;
		}

		vpm->dev = &wc->vb.pdev->dev;

		spin_lock_irqsave(&wc->reglock, flags);
		wc->vpmoct = vpm;
		wc->not_ready++;
		spin_unlock_irqrestore(&wc->reglock, flags);

		res = vpmoct_init(vpm, wctdm_vpm_load_complete);
		if (-EINVAL == res) {
			spin_lock_irqsave(&wc->reglock, flags);
			wc->vpmoct = NULL;
			wc->not_ready--;
			spin_unlock_irqrestore(&wc->reglock, flags);
			vpmoct_free(vpm);
		}
	}
	return 0;
}

static int __wctdm_identify_module_group(struct wctdm *wc, unsigned long base)
{
	int x;
	unsigned long flags;

	for (x = base; x < base + 4; ++x) {
		struct wctdm_module *const mod = &wc->mods[x];
		enum {SANE = 1, UNKNOWN = 0};
		int ret = 0, readi = 0;
		bool altcs = false;

		if (fatal_signal_pending(current))
			break;
retry:
		ret = wctdm_init_proslic(wc, mod, 0, 0, UNKNOWN);
		if (!ret) {
			if (debug & DEBUG_CARD) {
				readi = wctdm_getreg(wc, mod, LOOP_I_LIMIT);
				dev_info(&wc->vb.pdev->dev,
					 "Proslic module %d loop current "
					 "is %dmA\n", x, ((readi*3) + 20));
			}
			continue;
		}

		if (ret != -2) {
			/* Init with Manual Calibration */
			if (!wctdm_init_proslic(wc, mod, 0, 1, SANE)) {

				if (debug & DEBUG_CARD) {
					readi = wctdm_getreg(wc, mod,
							     LOOP_I_LIMIT);
					dev_info(&wc->vb.pdev->dev,
						 "Proslic module %d loop "
						 "current is %dmA\n", x,
						 ((readi*3)+20));
				}
			} else {
				dev_notice(&wc->vb.pdev->dev,
					   "Port %d: FAILED FXS (%s)\n",
					   x + 1, fxshonormode ?
					   fxo_modes[_opermode].name : "FCC");
			}
			continue;
		}

		ret = wctdm_init_voicedaa(wc, mod, 0, 0, UNKNOWN);
		if (!ret)
			continue;

		if (!wctdm_init_qrvdri(wc, x))
			continue;

		if (is_hx8(wc) && !wctdm_init_b400m(wc, x))
			continue;

		if ((wc->desc->ports != 24) && ((x&0x3) == 1) && !altcs) {

			spin_lock_irqsave(&wc->reglock, flags);
			set_offsets(mod, 2);
			altcs = true;

			if (wc->desc->ports == 4) {
				set_offsets(&wc->mods[x+1], 3);
				set_offsets(&wc->mods[x+2], 3);
			}

			mod->type = FXSINIT;
			spin_unlock_irqrestore(&wc->reglock, flags);

			udelay(1000);
			udelay(1000);

			spin_lock_irqsave(&wc->reglock, flags);
			mod->type = FXS;
			spin_unlock_irqrestore(&wc->reglock, flags);

			if (debug & DEBUG_CARD) {
				dev_info(&wc->vb.pdev->dev,
					 "Trying port %d with alternate chip "
					 "select\n", x + 1);
			}
			goto retry;
		}

		mod->type = NONE;
	}
	return 0;
}

/**
 * wctdm_print_moule_configuration - Print the configuration to the kernel log
 * @wc:		The card we're interested in.
 *
 * This is to ensure that the module configuration from each card shows up
 * sequentially in the kernel log, as opposed to interleaved with one another.
 *
 */
static void wctdm_print_module_configuration(const struct wctdm *const wc)
{
	int i;
	static DEFINE_MUTEX(print);

	mutex_lock(&print);
	for (i = 0; i < wc->mods_per_board; ++i) {
		const struct wctdm_module *const mod = &wc->mods[i];

		switch (mod->type) {
		case FXO:
			dev_info(&wc->vb.pdev->dev, "Port %d: Installed -- "
				 "AUTO FXO (%s mode)\n", i + 1,
				 fxo_modes[_opermode].name);
			break;
		case FXS:
			dev_info(&wc->vb.pdev->dev,
				 "Port %d: Installed -- AUTO FXS/DPO\n", i + 1);
			break;
		case BRI:
			dev_info(&wc->vb.pdev->dev, "Port %d: Installed -- BRI "
				 "quad-span module\n", i + 1);
			break;
		case QRV:
			dev_info(&wc->vb.pdev->dev,
				 "Port %d: Installed -- QRV DRI card\n", i + 1);
			break;
		case NONE:
			dev_info(&wc->vb.pdev->dev,
				 "Port %d: Not installed\n", i + 1);
			break;
		case FXSINIT:
			break;
		}
	}
	mutex_unlock(&print);
}

static void wctdm_identify_modules(struct wctdm *wc)
{
	int x;
	unsigned long flags;
	struct bg *bg_work[ARRAY_SIZE(wc->mods)/4 + 1] = {NULL, };

	wc->ctlreg = 0x00;

	/*
	 * This looks a little weird.
	 *
	 * There are only 8 physical ports on the TDM/AEX800, but the code
	 * immediately below sets 24 modules up.  This has to do with the
	 * altcs magic that allows us to have single-port and quad-port
	 * modules on these products.  The variable "mods_per_board" is set to
	 * the appropriate value just below the next code block.
	 *
	 * Now why this is important: The FXS modules come out of reset in a
	 * two-byte, non-chainable SPI mode.  This is currently incompatible
	 * with how we do things, so we need to set them to a chained, 3-byte
	 * command mode.  This is done by setting the module type to FXSINIT
	 * for a little while so that cmd_dequeue will initialize the SLIC
	 * into the appropriate mode.
	 *
	 * This "go to 3-byte chained mode" command, however, wreaks havoc
	 * with HybridBRI.
	 *
	 * The solution: Since HybridBRI is only designed to work in an 8-port
	 * card, and since the single-port modules "show up" in SPI slots >= 8
	 * in these cards, we only set SPI slots 8-23 to FXSINIT.  The
	 * HybridBRI will never see the command that causes it to freak out
	 * and the single-port FXS cards get what they need so that when we
	 * probe with altcs we see them.
	 */

	/* Make sure all units go into daisy chain mode */
	spin_lock_irqsave(&wc->reglock, flags);
	for (x = 0; x < ARRAY_SIZE(wc->mods); x++)
		wc->mods[x].type = FXSINIT;
	spin_unlock_irqrestore(&wc->reglock, flags);

	/* Wait just a bit; this makes sure that cmd_dequeue is emitting SPI
	 * commands in the appropriate mode(s). */
	msleep(20);

	/* Now that all the cards have been reset, we can stop checking them
	 * all if there aren't as many */
	spin_lock_irqsave(&wc->reglock, flags);
	wc->mods_per_board = wc->desc->ports;
	spin_unlock_irqrestore(&wc->reglock, flags);

	BUG_ON(wc->desc->ports % 4);

	/* Detecting and configuring the modules over voicebus takes a
	 * significant amount of time. We can speed things up by performing
	 * this in parallel for each group of four modules. */
	for (x = 0; x < wc->desc->ports/4; x++)
		bg_work[x] = bg_create(wc, __wctdm_identify_module_group, x*4);

	for (x = 0; bg_work[x]; ++x)
		bg_join(bg_work[x]);

	wctdm_print_module_configuration(wc);
}

static struct pci_driver wctdm_driver;

static void wctdm_back_out_gracefully(struct wctdm *wc)
{
	int i;
	unsigned long flags;
	struct vpmadt032 *vpm;
	LIST_HEAD(local_list);

	spin_lock_irqsave(&wc->reglock, flags);
	if (wc->not_ready) {
		wc->not_ready--;
		spin_unlock_irqrestore(&wc->reglock, flags);
		while (wctdm_wait_for_ready(wc))
			schedule();
		spin_lock_irqsave(&wc->reglock, flags);
	}
	spin_unlock_irqrestore(&wc->reglock, flags);

	if (wc->vpmadt032) {
		flush_workqueue(wc->vpmadt032->wq);
		clear_bit(VPM150M_ACTIVE, &wc->vpmadt032->control);
		flush_workqueue(wc->vpmadt032->wq);
		spin_lock_irqsave(&wc->reglock, flags);
		vpm = wc->vpmadt032;
		wc->vpmadt032 = NULL;
		spin_unlock_irqrestore(&wc->reglock, flags);
		vpmadt032_free(vpm);
	}

	voicebus_release(&wc->vb);
#ifdef CONFIG_VOICEBUS_ECREFERENCE
	for (i = 0; i < ARRAY_SIZE(wc->ec_reference); ++i) {
		if (wc->ec_reference[i])
			dahdi_fifo_free(wc->ec_reference[i]);
	}
#endif

	for (i = 0; i < ARRAY_SIZE(wc->spans); ++i) {
		if (wc->spans[i] && wc->spans[i]->span.chans)
			kfree(wc->spans[i]->span.chans);

		kfree(wc->spans[i]);
		wc->spans[i] = NULL;
	}

	spin_lock_irqsave(&wc->reglock, flags);
	for (i = 0; i < ARRAY_SIZE(wc->mods); ++i) {
		struct wctdm_module *const mod = &wc->mods[i];
		kfree(wc->chans[i]);
		wc->chans[i] = NULL;
		list_splice_init(&mod->pending_cmds, &local_list);
		list_splice_init(&mod->active_cmds, &local_list);
	}
	list_splice_init(&wc->free_isr_commands, &local_list);
	spin_unlock_irqrestore(&wc->reglock, flags);

	while (!list_empty(&local_list)) {
		struct wctdm_cmd *cmd;
		cmd = list_entry(local_list.next,
				 struct wctdm_cmd, node);
		list_del(&cmd->node);
		kfree(cmd->complete);
		kfree(cmd);
	}

	spin_lock_irqsave(&wc->frame_list_lock, flags);
	list_splice(&wc->frame_list, &local_list);
	spin_unlock_irqrestore(&wc->frame_list_lock, flags);

	while (!list_empty(&local_list)) {
		struct sframe_packet *frame;
		frame = list_entry(local_list.next,
				   struct sframe_packet, node);
		list_del(&frame->node);
		kfree(frame);
	}

	kfree(wc->board_name);
	kfree(wc->ddev->devicetype);
	kfree(wc->ddev->location);
	dahdi_free_device(wc->ddev);
	kfree(wc);
}

static const struct voicebus_operations voicebus_operations = {
	.handle_receive = handle_receive,
	.handle_transmit = handle_transmit,
};

static const struct voicebus_operations hx8_voicebus_operations = {
	.handle_receive = handle_hx8_receive,
	.handle_transmit = handle_hx8_transmit,
};

struct cmd_results {
	u8 results[8];
};

static int hx8_send_command(struct wctdm *wc, const u8 *command,
				     size_t count, int checksum,
				     int application, int bootloader,
				     struct cmd_results *results)
{
	int ret = 0;
	struct vbb *vbb;
	struct sframe_packet *frame;
	const int MAX_COMMAND_LENGTH = 264 + 4;
	unsigned long flags;
	dma_addr_t dma_addr;

	might_sleep();

	/* can't boot both into the application and the bootloader at once. */
	WARN_ON((application > 0) && (bootloader > 0));
	if ((application > 0) && (bootloader > 0))
		return -EINVAL;

	WARN_ON(count > MAX_COMMAND_LENGTH);
	if (count > MAX_COMMAND_LENGTH)
		return -EINVAL;

	vbb = dma_pool_alloc(wc->vb.pool, GFP_KERNEL, &dma_addr);
	WARN_ON(!vbb);
	if (!vbb)
		return -ENOMEM;

	vbb->dma_addr = dma_addr;
	memset(vbb->data, 0, SFRAME_SIZE);
	memcpy(&vbb->data[EFRAME_SIZE + EFRAME_GAP], command, count);

	vbb->data[EFRAME_SIZE] = 0x80 | ((application) ? 0 : 0x40) |
		((checksum) ? 0x20 : 0) | ((count & 0x100) >> 4);
	vbb->data[EFRAME_SIZE + 1] = count & 0xff;

	if (bootloader)
		vbb->data[EFRAME_SIZE + 3] = 0xAA;

	spin_lock_irqsave(&wc->vb.lock, flags);
	voicebus_transmit(&wc->vb, vbb);
	spin_unlock_irqrestore(&wc->vb.lock, flags);

	/* Do not wait for the response if the caller doesn't care about the
	 * results. */
	if (NULL == results)
		return 0;

	if (!wait_event_timeout(wc->regq, !list_empty(&wc->frame_list), 2*HZ)) {
		dev_err(&wc->vb.pdev->dev, "Timeout waiting "
			"for receive frame.\n");
		ret = -EIO;
	}

	/* We only want the last packet received.  Throw away anything else on
	 * the list */
	frame = NULL;
	spin_lock_irqsave(&wc->frame_list_lock, flags);
	while (!list_empty(&wc->frame_list)) {
		frame = list_entry(wc->frame_list.next,
				   struct sframe_packet, node);
		list_del(&frame->node);
		if (!list_empty(&wc->frame_list)) {
			kfree(frame);
			frame = NULL;
		}
	}
	spin_unlock_irqrestore(&wc->frame_list_lock, flags);

	if (frame) {
		memcpy(results->results, &frame->sframe[EFRAME_SIZE],
		       sizeof(results->results));
	} else {
		ret = -EIO;
	}

	return ret;

}

static int hx8_get_fpga_version(struct wctdm *wc, u8 *major, u8 *minor)
{
	int ret;
	struct cmd_results results;
	u8 command[] = {0xD7, 0x00};

	ret = hx8_send_command(wc, command, ARRAY_SIZE(command),
					0, 0, 0, &results);
	if (ret)
		return ret;

	*major = results.results[0];
	*minor = results.results[2];
	return 0;
}

static void hx8_cleanup_frame_list(struct wctdm *wc)
{
	unsigned long flags;
	LIST_HEAD(local_list);
	struct sframe_packet *frame;

	spin_lock_irqsave(&wc->frame_list_lock, flags);
	list_splice_init(&wc->frame_list, &local_list);
	spin_unlock_irqrestore(&wc->frame_list_lock, flags);

	while (!list_empty(&local_list)) {
		frame = list_entry(local_list.next, struct sframe_packet, node);
		list_del(&frame->node);
		kfree(frame);
	}
}

static int hx8_switch_to_application(struct wctdm *wc)
{
	int ret;
	u8 command[] = {0xD7, 0x00};

	ret = hx8_send_command(wc, command, ARRAY_SIZE(command),
					0, 1, 0, NULL);
	if (ret)
		return ret;

	msleep(1000);
	hx8_cleanup_frame_list(wc);

	return 0;
}

/**
 * hx8_switch_to_bootloader() - Send packet to switch hx8 into bootloader
 *
 */
static int hx8_switch_to_bootloader(struct wctdm *wc)
{
	int ret;
	u8 command[] = {0xD7, 0x00};

	ret = hx8_send_command(wc, command, ARRAY_SIZE(command),
					0, 0, 1, NULL);
	if (ret)
		return ret;

	/* It takes some time for the FPGA to reload and switch it's
	 * configuration. */
	msleep(300);
	hx8_cleanup_frame_list(wc);

	return 0;
}

struct ha80000_firmware {
	u8	header[6];
	u8	major_ver;
	u8	minor_ver;
	u8	data[54648];
	__le32	chksum;
} __attribute__((packed));

static void hx8_send_dummy(struct wctdm *wc)
{
	u8 command[] = {0xD7, 0x00};

	hx8_send_command(wc, command, ARRAY_SIZE(command),
				  0, 0, 0, NULL);
}

static int hx8_read_status_register(struct wctdm *wc, u8 *status)
{
	int ret;
	struct cmd_results results;
	u8 command[] = {0xD7, 0x00};

	ret = hx8_send_command(wc, command, ARRAY_SIZE(command),
					0, 0, 0, &results);
	if (ret)
		return ret;

	*status = results.results[3];
	return 0;
}

static const unsigned int HYBRID_PAGE_SIZE = 264;

static int hx8_write_buffer(struct wctdm *wc, const u8 *buffer, size_t size)
{
	int ret = 0;
	struct cmd_results results;
	int padding_bytes = 0;
	u8 *local_data;
	u8 command[] = {0x84, 0, 0, 0};

	if (size > HYBRID_PAGE_SIZE)
		return -EINVAL;

	if (size < HYBRID_PAGE_SIZE)
		padding_bytes = HYBRID_PAGE_SIZE - size;

	local_data = kmalloc(sizeof(command) + size + padding_bytes, GFP_KERNEL);
	if (!local_data)
		return -ENOMEM;

	memcpy(local_data, command, sizeof(command));
	memcpy(&local_data[sizeof(command)], buffer, size);
	memset(&local_data[sizeof(command) + size], 0xff, padding_bytes);

	ret = hx8_send_command(wc, local_data,
					sizeof(command) + size + padding_bytes,
					1, 0, 0, &results);
	if (ret)
		goto cleanup;

cleanup:
	kfree(local_data);
	return ret;
}

static int hx8_buffer_to_page(struct wctdm *wc, const unsigned int page)
{
	int ret;
	struct cmd_results results;
	u8 command[] = {0x83, (page & 0x180) >> 7, (page & 0x7f) << 1, 0x00};

	ret = hx8_send_command(wc, command, sizeof(command),
					1, 0, 0, &results);
	if (ret)
		return ret;

	return 0;
}

static int hx8_wait_for_ready(struct wctdm *wc, const int timeout)
{
	int ret;
	u8 status;
	unsigned long local_timeout = jiffies + timeout;

	do {
		ret = hx8_read_status_register(wc, &status);
		if (ret)
			return ret;
		if ((status & 0x80) > 0)
			break;
	} while (time_after(local_timeout, jiffies));

	if (time_after(jiffies, local_timeout))
		return -EIO;

	return 0;
}

/**
 * hx8_reload_application - reload the application firmware
 *
 * NOTE: The caller should ensure that the board is in bootloader mode before
 * calling this function.
 */
static int hx8_reload_application(struct wctdm *wc, const struct ha80000_firmware *ha8_fw)
{
	unsigned int cur_page;
	const u8 *data;
	u8 status;
	int ret = 0;
	const int HYBRID_PAGE_COUNT = (sizeof(ha8_fw->data)) / HYBRID_PAGE_SIZE;

	dev_info(&wc->vb.pdev->dev, "Reloading firmware. Do not power down "
			"the system until the process is complete.\n");

	BUG_ON(!ha8_fw);
	might_sleep();

	data = &ha8_fw->data[0];
	ret = hx8_read_status_register(wc, &status);
	if (ret)
		return ret;

	for (cur_page = 0; cur_page < HYBRID_PAGE_COUNT; ++cur_page) {
		ret = hx8_write_buffer(wc, data, HYBRID_PAGE_SIZE);
		if (ret)
			return ret;
		/* The application starts out at page 0x100 */
		ret = hx8_buffer_to_page(wc, 0x100 + cur_page);
		if (ret)
			return ret;

		/* wait no more than a second for the write to the page to
		 * finish */
		ret = hx8_wait_for_ready(wc, HZ);
		if (ret)
			return ret;

		data += HYBRID_PAGE_SIZE;
	}

	return ret;
}

static void print_hx8_recovery_message(struct device *dev)
{
	dev_warn(dev, "The firmware may be corrupted.  Please completely "
		 "power off your system, power on, and then reload the driver "
		 "with the 'forceload' module parameter set to 1 to attempt "
		 "recovery.\n");
}

/**
 * hx8_check_firmware - Check the firmware version and load a new one possibly.
 *
 */
static int hx8_check_firmware(struct wctdm *wc)
{
	int ret;
	u8 major;
	u8 minor;
	const struct firmware *fw;
	const struct ha80000_firmware *ha8_fw;
	struct device *dev = &wc->vb.pdev->dev;
	int retries = 10;

	BUG_ON(!is_hx8(wc));

	might_sleep();

	do {
		hx8_send_dummy(wc);
		ret = hx8_get_fpga_version(wc, &major, &minor);
		if (!ret)
			break;
		if (fatal_signal_pending(current))
			return -EINTR;
	} while (--retries);

	if (ret) {
		print_hx8_recovery_message(dev);
		return ret;
	}

	/* If we're in the bootloader, try to jump into the application. */
	if ((1 == major) && (0x80 == minor) && !forceload) {
		dev_dbg(dev, "Switching to application.\n");
		hx8_switch_to_application(wc);
		ret = hx8_get_fpga_version(wc, &major, &minor);
		if (ret) {
			print_hx8_recovery_message(dev);
			return ret;
		}
	}

	dev_dbg(dev, "FPGA VERSION: %02x.%02x\n", major, minor);

	ret = request_firmware(&fw, "dahdi-fw-hx8.bin", dev);
	if (ret) {
		dev_warn(dev, "Failed to load firmware from userspace, skipping "
			 "check. (%d)\n", ret);
		return 0;
	}
	ha8_fw = (const struct ha80000_firmware *)fw->data;

	if ((fw->size != sizeof(*ha8_fw)) ||
	    (0 != memcmp("DIGIUM", ha8_fw->header, sizeof(ha8_fw->header))) ||
	    ((crc32(~0, (void *)ha8_fw, sizeof(*ha8_fw) - sizeof(u32)) ^ ~0) !=
	      le32_to_cpu(ha8_fw->chksum))) {
		dev_warn(dev, "Firmware file is invalid. Skipping load.\n");
		ret = 0;
		goto cleanup;
	}

	dev_dbg(dev, "FIRMWARE: %02x.%02x\n", ha8_fw->major_ver, ha8_fw->minor_ver);

	if (ha8_fw->major_ver == major &&
	    ha8_fw->minor_ver == minor) {
		dev_dbg(dev, "Firmware versions match, skipping load.\n");
		ret = 0;
		goto cleanup;
	}

	if (2 == major) {
		hx8_switch_to_bootloader(wc);
		ret = hx8_get_fpga_version(wc, &major, &minor);
		if (ret)
			goto cleanup;
	}

	/* so now we're in boot loader mode, ready to load the new firmware. */
	ret = hx8_reload_application(wc, ha8_fw);
	if (ret)
		goto cleanup;

	dev_dbg(dev, "Firmware reloaded.  Booting into application.\n");

	hx8_switch_to_application(wc);
	ret = hx8_get_fpga_version(wc, &major, &minor);
	if (ret)
		goto cleanup;

	dev_dbg(dev, "FPGA VERSION AFTER LOAD: %02x.%02x\n", major, minor);

	if (forceload) {
		dev_warn(dev, "Please unset forceload if your card is able to "
			 "detect the installed modules.\n");
	}

cleanup:
	release_firmware(fw);
	dev_info(dev, "Hx8 firmware version: %d.%02d\n", major, minor);
	return ret;
}

#ifdef CONFIG_VOICEBUS_SYSFS
static ssize_t
voicebus_current_latency_show(struct device *dev,
			      struct device_attribute *attr, char *buf)
{
	unsigned long flags;
	struct wctdm *wc = dev_get_drvdata(dev);
	unsigned int current_latency;
	spin_lock_irqsave(&wc->vb.lock, flags);
	current_latency = wc->vb.min_tx_buffer_count;
	spin_unlock_irqrestore(&wc->vb.lock, flags);
	return sprintf(buf, "%d\n", current_latency);
}

static DEVICE_ATTR(voicebus_current_latency, 0400,
		   voicebus_current_latency_show, NULL);

static ssize_t vpm_firmware_version_show(struct device *dev,
				struct device_attribute *attr,
				char *buf)
{
	int res;
	u16 version = 0;
	struct wctdm *wc = dev_get_drvdata(dev);

	if (wc->vpmadt032) {
		res = gpakPingDsp(wc->vpmadt032->dspid, &version);
		if (res) {
			dev_info(&wc->vb.pdev->dev, "Failed gpakPingDsp %d\n", res);
			version = -1;
		}
	}

	return sprintf(buf, "%x.%02x\n", (version & 0xff00) >> 8, (version & 0xff));
}

static DEVICE_ATTR(vpm_firmware_version, 0400,
		   vpm_firmware_version_show, NULL);
static ssize_t
enable_vpm_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	unsigned long flags;
	struct wctdm *wc = dev_get_drvdata(dev);
	unsigned int enable_vpm;
	spin_lock_irqsave(&wc->reglock, flags);
	enable_vpm = (wc->ctlreg & 0x10) != 0;
	spin_unlock_irqrestore(&wc->reglock, flags);
	return sprintf(buf, "%d\n", enable_vpm);
}

static ssize_t
enable_vpm_store(struct device *dev, struct device_attribute *attr,
		 const char *buf, size_t count)
{
	unsigned long flags;
	struct wctdm *wc = dev_get_drvdata(dev);
	unsigned int enable_vpm;
	if (count < 2)
		return -EINVAL;

	if (('0' == buf[0]) || (0 == buf[0]))
		enable_vpm = 0;
	else
		enable_vpm = 1;

	spin_lock_irqsave(&wc->reglock, flags);
	if (enable_vpm)
		wc->ctlreg |= 0x10;
	else
		wc->ctlreg &= ~0x10;
	spin_unlock_irqrestore(&wc->reglock, flags);
	return count;
}

static DEVICE_ATTR(enable_vpm, 0644,
		   enable_vpm_show, enable_vpm_store);

static void create_sysfs_files(struct wctdm *wc)
{
	int ret;
	ret = device_create_file(&wc->vb.pdev->dev,
				 &dev_attr_voicebus_current_latency);
	if (ret) {
		dev_info(&wc->vb.pdev->dev,
			"Failed to create device attributes.\n");
	}

	ret = device_create_file(&wc->vb.pdev->dev,
				 &dev_attr_vpm_firmware_version);
	if (ret) {
		dev_info(&wc->vb.pdev->dev,
			"Failed to create device attributes.\n");
	}

	ret = device_create_file(&wc->vb.pdev->dev,
				 &dev_attr_enable_vpm);
	if (ret) {
		dev_info(&wc->vb.pdev->dev,
			"Failed to create device attributes.\n");
	}
}

static void remove_sysfs_files(struct wctdm *wc)
{
	device_remove_file(&wc->vb.pdev->dev,
			   &dev_attr_enable_vpm);

	device_remove_file(&wc->vb.pdev->dev,
			   &dev_attr_vpm_firmware_version);

	device_remove_file(&wc->vb.pdev->dev,
			   &dev_attr_voicebus_current_latency);
}

#else

static inline void create_sysfs_files(struct wctdm *wc) { return; }
static inline void remove_sysfs_files(struct wctdm *wc) { return; }

#endif /* CONFIG_VOICEBUS_SYSFS */

static void wctdm_set_tdm410_leds(struct wctdm *wc)
{
	int i;

	if (4 != wc->desc->ports)
		return;

	wc->tdm410leds = 0; /* all on by default */
	for (i = 0; i < wc->desc->ports; ++i) {
		/* Turn off the LED for any module that isn't installed. */
		if (NONE == wc->mods[i].type)
			wc->tdm410leds |= (1 << i);
	}
}

/**
 * wctdm_allocate_irq_commands - Preallocate some commands for use in interrupt context.
 * @wc:	The board which we're allocating for.
 * @count:	The number of IRQ commands to allocate.
 *
 * We need a minimum of 4 * the current latency worth of commands for each
 * analog module. When the latency grows, new commands will be allocated, but
 * this just represents are best guess as to the number of commands we'll need
 * after probing for modules, and reduces the chance that we'll allocate
 * memory in interrupt context when the driver first loads.
 *
 */
static void wctdm_allocate_irq_commands(struct wctdm *wc, unsigned int count)
{
	unsigned long flags;
	LIST_HEAD(local_list);

	if (!count)
		return;

	while (count--) {
		struct wctdm_cmd *cmd;
		cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
		if (!cmd)
			break;
		list_add(&cmd->node, &local_list);
	}

	spin_lock_irqsave(&wc->reglock, flags);
	list_splice(&local_list, &wc->free_isr_commands);
	spin_unlock_irqrestore(&wc->reglock, flags);
}

#ifdef USE_ASYNC_INIT
struct async_data {
	struct pci_dev *pdev;
	const struct pci_device_id *ent;
};
static int __devinit
__wctdm_init_one(struct pci_dev *pdev, const struct pci_device_id *ent,
		 async_cookie_t cookie)
#else
static int __devinit
__wctdm_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
#endif
{
	struct wctdm *wc;
	unsigned int pos;
	int i, ret;
	struct bg *vpm_work;
	int anamods, digimods, curchan, curspan;
	
	neonmwi_offlimit_cycles = neonmwi_offlimit / MS_PER_HOOKCHECK;

	wc = kzalloc(sizeof(*wc), GFP_KERNEL);
	if (!wc)
		return -ENOMEM;

	wc->not_ready = 1;

	down(&ifacelock);
	/* \todo this is a candidate for removal... */
	for (pos = 0; pos < WC_MAX_IFACES; ++pos) {
		if (!ifaces[pos]) {
			ifaces[pos] = wc;
			break;
		}
	}
	up(&ifacelock);

	wc->desc = (struct wctdm_desc *)ent->driver_data;

	/* This is to insure that the analog span is given lowest priority */
	sema_init(&wc->syncsem, 1);
	INIT_LIST_HEAD(&wc->frame_list);
	spin_lock_init(&wc->frame_list_lock);
	init_waitqueue_head(&wc->regq);
	spin_lock_init(&wc->reglock);
	INIT_LIST_HEAD(&wc->free_isr_commands);
	wc->oldsync = -1;

	wc->board_name = kasprintf(GFP_KERNEL, "%s%d", wctdm_driver.name, pos);
	if (!wc->board_name) {
		wctdm_back_out_gracefully(wc);
		return -ENOMEM;
	}

#ifdef CONFIG_VOICEBUS_ECREFERENCE
	for (i = 0; i < ARRAY_SIZE(wc->ec_reference); ++i) {
		/* 256 is the smallest power of 2 that will contains the
		 * maximum possible amount of latency. */
		wc->ec_reference[i] = dahdi_fifo_alloc(256, GFP_KERNEL);

		if (IS_ERR(wc->ec_reference[i])) {
			ret = PTR_ERR(wc->ec_reference[i]);
			wc->ec_reference[i] = NULL;
			wctdm_back_out_gracefully(wc);
			return ret;
		}
	}
#endif

	pci_set_drvdata(pdev, wc);
	wc->vb.ops = &voicebus_operations;
	wc->vb.pdev = pdev;
	wc->vb.debug = &debug;

#ifdef CONFIG_VOICEBUS_DISABLE_ASPM
	if (is_pcie(wc)) {
		pci_disable_link_state(pdev->bus->self, PCIE_LINK_STATE_L0S |
			PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
	};
#endif

	if (is_hx8(wc)) {
		wc->vb.ops = &hx8_voicebus_operations;
		ret = voicebus_boot_init(&wc->vb, wc->board_name);
	} else {
		wc->vb.ops = &voicebus_operations;
		ret = voicebus_init(&wc->vb, wc->board_name);
		voicebus_set_minlatency(&wc->vb, latency);
		voicebus_set_maxlatency(&wc->vb, max_latency);
	}

	if (ret) {
		wctdm_back_out_gracefully(wc);
		return ret;
	}

	create_sysfs_files(wc);

	voicebus_lock_latency(&wc->vb);

	wc->mods_per_board = NUM_MODULES;
	wc->txident = 1;

	if (alawoverride) {
		companding = "alaw";
		dev_info(&wc->vb.pdev->dev, "The module parameter alawoverride"\
					" has been deprecated. Please use the "\
					"parameter companding=alaw instead");
	}

	if (!strcasecmp(companding, "alaw"))
		/* Force this card's companding to alaw */
		wc->companding = DAHDI_LAW_ALAW;
	else if (!strcasecmp(companding, "ulaw"))
		/* Force this card's companding to ulaw */
		wc->companding = DAHDI_LAW_MULAW;
	else
		/* Auto detect this card's companding */
		wc->companding = DAHDI_LAW_DEFAULT;

	for (i = 0; i < ARRAY_SIZE(wc->mods); i++) {
		struct wctdm_module *const mod = &wc->mods[i];
		INIT_LIST_HEAD(&mod->pending_cmds);
		INIT_LIST_HEAD(&mod->active_cmds);
		mod->dacssrc = -1;
		mod->card = i;
		set_offsets(mod, 0);
	}

	/* Start the hardware processing. */
	if (voicebus_start(&wc->vb)) {
		BUG_ON(1);
	}

	if (is_hx8(wc)) {
		ret = hx8_check_firmware(wc);
		if (ret) {
			wctdm_back_out_gracefully(wc);
			return -EIO;
		}

		/* Switch to the normal operating mode for this card. */
		voicebus_stop(&wc->vb);
		wc->vb.ops = &voicebus_operations;
		voicebus_set_minlatency(&wc->vb, latency);
		voicebus_set_maxlatency(&wc->vb, max_latency);
		voicebus_set_hx8_mode(&wc->vb);
		if (voicebus_start(&wc->vb))
			BUG_ON(1);
	}

/* first we have to make sure that we process all module data, we'll fine-tune it later in this routine. */
	wc->avchannels = NUM_MODULES;

	vpm_work = bg_create(wc, wctdm_initialize_vpm, 0);
	if (!vpm_work) {
		wctdm_back_out_gracefully(wc);
		return -ENOMEM;
	}

	/* Now track down what modules are installed */
	wctdm_identify_modules(wc);

	wctdm_set_tdm410_leds(wc);

	if (fatal_signal_pending(current)) {
		wctdm_back_out_gracefully(wc);
		bg_join(vpm_work);
		return -EINTR;
	}

	/* We need to wait for the vpm thread to finish before we setup the
	 * spans in order to ensure they are named properly. */
	bg_join(vpm_work);

/*
 * Walk the module list and create a 3-channel span for every BRI module found.
 * Empty and analog modules get a common span which is allocated outside of this loop.
 */
	anamods = digimods = 0;
	curchan = curspan = 0;
	
	for (i = 0; i < wc->mods_per_board; i++) {
		struct wctdm_module *const mod = &wc->mods[i];
		struct b400m *b4;

		if (mod->type == NONE) {
			++curspan;
			continue;
		} else if (mod->type == BRI) {
			if (!is_hx8(wc)) {
				dev_info(&wc->vb.pdev->dev, "Digital modules "
					"detected on a non-hybrid card. "
					"This is unsupported.\n");
				wctdm_back_out_gracefully(wc);
				bg_join(vpm_work);
				return -EIO;
			}
			wc->spans[curspan] = wctdm_init_span(wc, curspan,
							     curchan, 3, 1,
							     pos);
			if (!wc->spans[curspan]) {
				wctdm_back_out_gracefully(wc);
				bg_join(vpm_work);
				return -EIO;
			}
			b4 = mod->mod.bri;
			b400m_set_dahdi_span(b4, i & 0x03, wc->spans[curspan]);

			++curspan;
			curchan += 3;
			if (!(i & 0x03)) {
				b400m_post_init(b4);
				++digimods;
			}
		} else {
/*
 * FIXME: ABK:
 * create a wctdm_chan for every analog module and link them into a span of their own down below.
 * then evaluate all of the callbacks and hard-code whether they are receiving a dahdi_chan or wctdm_chan *.
 * Finally, move the union from the wctdm structure to the dahdi_chan structure, and we should have something
 * resembling a clean dynamic # of channels/dynamic # of spans driver.
 */
			++curspan;
			++anamods;
		}

		if (digimods > 2) {
			dev_info(&wc->vb.pdev->dev, "More than two digital modules detected. This is unsupported.\n");
			wctdm_back_out_gracefully(wc);
			return -EIO;
		}
	}

	wc->digi_mods = digimods;

/* create an analog span if there are analog modules, or if there are no digital ones. */
	if (anamods || !digimods) {
		if (!digimods) {
			curspan = 0;
		}
		wctdm_init_span(wc, curspan, curchan, wc->desc->ports, 0, pos);
		wctdm_fixup_analog_span(wc, curspan);
		wc->aspan = wc->spans[curspan];
		curchan += wc->desc->ports;
		++curspan;
	}

	/* Now fix up the timeslots for the analog modules, since the digital
	 * modules are always first */
	for (i = 0; i < wc->mods_per_board; i++) {
		struct wctdm_module *const mod = &wc->mods[i];
		switch (mod->type) {
		case FXS:
			wctdm_proslic_set_ts(wc, mod, (digimods * 12) + i);
			break;
		case FXO:
			wctdm_voicedaa_set_ts(wc, mod, (digimods * 12) + i);
			break;
		case QRV:
			wctdm_qrvdri_set_ts(wc, mod, (digimods * 12) + i);
			break;
		default:
			break;
		}
	}

	/* This shouldn't ever occur, but if we don't try to trap it, the driver
	 * will be scribbling into memory it doesn't own. */
	BUG_ON(curchan > 24);

	wc->avchannels = curchan;

#ifdef USE_ASYNC_INIT
		async_synchronize_cookie(cookie);
#endif
	wc->ddev = dahdi_create_device();
	wc->ddev->manufacturer = "Digium";
	wc->ddev->location = kasprintf(GFP_KERNEL, "PCI%s Bus %02d Slot %02d",
				       (wc->desc->flags & FLAG_EXPRESS) ?
						" Express" : "",
				       pdev->bus->number,
				       PCI_SLOT(pdev->devfn) + 1);
	if (!wc->ddev->location) {
		wctdm_back_out_gracefully(wc);
		return -ENOMEM;
	}

	if (wc->vpmadt032) {
		wc->ddev->devicetype = kasprintf(GFP_KERNEL, "%s (VPMADT032)",
						wc->desc->name);
	} else if (wc->vpmoct) {
		wc->ddev->devicetype = kasprintf(GFP_KERNEL, "%s (VPMOCT032)",
						wc->desc->name);
	} else  {
		wc->ddev->devicetype = kasprintf(GFP_KERNEL, "%s",
						wc->desc->name);
	}

	if (!wc->ddev->devicetype) {
		wctdm_back_out_gracefully(wc);
		return -ENOMEM;
	}

	/* We should be ready for DAHDI to come in now. */
	for (i = 0; i < MAX_SPANS; ++i) {
		struct dahdi_span *span;

		if (!wc->spans[i])
			continue;

		span = &wc->spans[i]->span;

		list_add_tail(&span->device_node, &wc->ddev->spans);
	}

	wctdm_allocate_irq_commands(wc, anamods * latency * 4);

	if (dahdi_register_device(wc->ddev, &wc->vb.pdev->dev)) {
		dev_notice(&wc->vb.pdev->dev, "Unable to register device with DAHDI\n");
		wctdm_back_out_gracefully(wc);
		return -1;
	}

	dev_info(&wc->vb.pdev->dev,
		 "Found a %s: %s (%d BRI spans, %d analog %s)\n",
		 (is_hx8(wc)) ? "Hybrid card" : "Wildcard TDM",
		 wc->desc->name, digimods*4, anamods,
		 (anamods == 1) ? "channel" : "channels");
	ret = 0;

	voicebus_unlock_latency(&wc->vb);
	return 0;
}

#ifdef USE_ASYNC_INIT
static __devinit void
wctdm_init_one_async(void *data, async_cookie_t cookie)
{
	struct async_data *dat = data;
	__wctdm_init_one(dat->pdev, dat->ent, cookie);
	kfree(dat);
}

static int __devinit
wctdm_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
{
	struct async_data *dat;

	dat = kmalloc(sizeof(*dat), GFP_KERNEL);
	/* If we can't allocate the memory for the async_data, odds are we won't
	 * be able to initialize the device either, but let's try synchronously
	 * anyway... */
	if (!dat)
		return __wctdm_init_one(pdev, ent, 0);

	dat->pdev = pdev;
	dat->ent = ent;
	async_schedule(wctdm_init_one_async, dat);
	return 0;
}
#else
static int __devinit
wctdm_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
{
	return __wctdm_init_one(pdev, ent);
}
#endif


static void wctdm_release(struct wctdm *wc)
{
	int i;

	if (is_initialized(wc))
		dahdi_unregister_device(wc->ddev);

	down(&ifacelock);
	for (i = 0; i < WC_MAX_IFACES; i++)
		if (ifaces[i] == wc)
			break;
	ifaces[i] = NULL;
	up(&ifacelock);
	
	wctdm_back_out_gracefully(wc);
}

static void __devexit wctdm_remove_one(struct pci_dev *pdev)
{
	int i;
	unsigned long flags;
	struct wctdm *wc = pci_get_drvdata(pdev);
	struct vpmadt032 *vpmadt032;
	struct vpmoct	 *vpmoct;

	if (!wc)
		return;

	vpmadt032 = wc->vpmadt032;
	vpmoct = wc->vpmoct;

	remove_sysfs_files(wc);

	if (vpmadt032) {
		flush_workqueue(vpmadt032->wq);
		clear_bit(VPM150M_ACTIVE, &vpmadt032->control);
		flush_workqueue(vpmadt032->wq);
	} else if (vpmoct) {
		while (wctdm_wait_for_ready(wc))
			schedule();
	}

	flush_scheduled_work();

	/* shut down any BRI modules */
	for (i = 0; i < wc->mods_per_board; i += 4) {
		if (wc->mods[i].type == BRI)
			wctdm_unload_b400m(wc, i);
	}

	voicebus_stop(&wc->vb);

	if (vpmadt032) {
		spin_lock_irqsave(&wc->reglock, flags);
		wc->vpmadt032 = NULL;
		spin_unlock_irqrestore(&wc->reglock, flags);
		vpmadt032_free(vpmadt032);
	} else if (vpmoct) {
		spin_lock_irqsave(&wc->reglock, flags);
		wc->vpmoct = NULL;
		spin_unlock_irqrestore(&wc->reglock, flags);
		vpmoct_free(vpmoct);
	}

	dev_info(&wc->vb.pdev->dev, "Freed a %s\n",
			(is_hx8(wc)) ? "Hybrid card" : "Wildcard");

	/* Release span */
	wctdm_release(wc);
}

static DEFINE_PCI_DEVICE_TABLE(wctdm_pci_tbl) = {
	{ 0xd161, 0x2400, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &wctdm2400 },
	{ 0xd161, 0x0800, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &wctdm800 },
	{ 0xd161, 0x8002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &wcaex800 },
	{ 0xd161, 0x8003, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &wcaex2400 },
	{ 0xd161, 0x8005, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &wctdm410 },
	{ 0xd161, 0x8006, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &wcaex410 },
	{ 0xd161, 0x8007, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &wcha80000 },
	{ 0xd161, 0x8008, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &wchb80000 },
	{ 0 }
};

static void wctdm_shutdown(struct pci_dev *pdev)
{
	struct wctdm *wc = pci_get_drvdata(pdev);
	voicebus_quiesce(&wc->vb);
}

MODULE_DEVICE_TABLE(pci, wctdm_pci_tbl);

static int wctdm_suspend(struct pci_dev *pdev, pm_message_t state)
{
	return -ENOSYS;
}

static struct pci_driver wctdm_driver = {
	.name = "wctdm24xxp",
	.probe = wctdm_init_one,
	.remove = __devexit_p(wctdm_remove_one),
	.shutdown = wctdm_shutdown,
	.suspend = wctdm_suspend,
	.id_table = wctdm_pci_tbl,
};

static int __init wctdm_init(void)
{
	int res;
	int x;

	for (x = 0; x < ARRAY_SIZE(fxo_modes); x++) {
		if (!strcmp(fxo_modes[x].name, opermode))
			break;
	}
	if (x < ARRAY_SIZE(fxo_modes)) {
		_opermode = x;
	} else {
		printk(KERN_NOTICE "Invalid/unknown operating mode '%s' "
		       "specified.  Please choose one of:\n", opermode);
		for (x = 0; x < ARRAY_SIZE(fxo_modes); x++)
			printk(KERN_CONT "  %s\n", fxo_modes[x].name);
		printk(KERN_NOTICE "Note this option is CASE SENSITIVE!\n");
		return -ENODEV;
	}

	if (!strcmp(opermode, "AUSTRALIA")) {
		boostringer = 1;
		fxshonormode = 1;
	}

	if (-1 == fastpickup) {
		if (!strcmp(opermode, "JAPAN"))
			fastpickup = 1;
		else
			fastpickup = 0;
	}

	/* for the voicedaa_check_hook defaults, if the user has not overridden
	   them by specifying them as module parameters, then get the values
	   from the selected operating mode
	*/
	if (battdebounce == 0) {
		battdebounce = fxo_modes[_opermode].battdebounce;
	}
	if (battalarm == 0) {
		battalarm = fxo_modes[_opermode].battalarm;
	}
	if (battthresh == 0) {
		battthresh = fxo_modes[_opermode].battthresh;
	}

	b400m_module_init();

	res = dahdi_pci_module(&wctdm_driver);
	if (res)
		return -ENODEV;

#ifdef USE_ASYNC_INIT
	async_synchronize_full();
#endif
	return 0;
}

static void __exit wctdm_cleanup(void)
{
	pci_unregister_driver(&wctdm_driver);
}


module_param(debug, int, 0600);
module_param(fastpickup, int, 0400);
MODULE_PARM_DESC(fastpickup,
		 "Set to 1 to shorten the calibration delay when taking " \
		 "an FXO port off hook. This can be required for Type-II " \
		 "CID. If -1 the calibration delay will depend on the " \
		 "current opermode.\n");
module_param(fxovoltage, int, 0600);
module_param(loopcurrent, int, 0600);
module_param(reversepolarity, int, 0600);
#ifdef DEBUG
module_param(robust, int, 0600);
module_param(digitalloopback, int, 0400);
MODULE_PARM_DESC(digitalloopback, "Set to 1 to place FXO modules into " \
		 "loopback mode for troubleshooting.");
#endif
module_param(opermode, charp, 0600);
module_param(lowpower, int, 0600);
module_param(boostringer, int, 0600);
module_param(fastringer, int, 0600);
module_param(fxshonormode, int, 0600);
module_param(battdebounce, uint, 0600);
module_param(battalarm, uint, 0600);
module_param(battthresh, uint, 0600);
module_param(nativebridge, int, 0600);
module_param(fxotxgain, int, 0600);
module_param(fxorxgain, int, 0600);
module_param(fxstxgain, int, 0600);
module_param(fxsrxgain, int, 0600);
module_param(ringdebounce, int, 0600);
module_param(latency, int, 0400);
module_param(max_latency, int, 0400);
module_param(neonmwi_monitor, int, 0600);
module_param(neonmwi_level, int, 0600);
module_param(neonmwi_envelope, int, 0600);
module_param(neonmwi_offlimit, int, 0600);
#ifdef VPM_SUPPORT
module_param(vpmsupport, int, 0400);
module_param(vpmnlptype, int, 0400);
module_param(vpmnlpthresh, int, 0400);
module_param(vpmnlpmaxsupp, int, 0400);
#endif

/* Module parameters backed by code in xhfc.c */
module_param(bri_debug, int, 0600);
MODULE_PARM_DESC(bri_debug, "bitmap: 1=general 2=dtmf 4=regops 8=fops 16=ec 32=st state 64=hdlc 128=alarm");

module_param(bri_spanfilter, int, 0600);
MODULE_PARM_DESC(bri_spanfilter, "debug filter for spans. bitmap: 1=port 1, 2=port 2, 4=port 3, 8=port 4");

module_param(bri_alarmdebounce, int, 0600);
MODULE_PARM_DESC(bri_alarmdebounce, "msec to wait before set/clear alarm condition");

module_param(bri_teignorered, int, 0600);
MODULE_PARM_DESC(bri_teignorered, "1=ignore (do not inform DAHDI) if a red alarm exists in TE mode");

module_param(bri_persistentlayer1, int, 0600);
MODULE_PARM_DESC(bri_persistentlayer1, "Set to 0 for disabling automatic layer 1 reactivation (when other end deactivates it)");

module_param(timingcable, int, 0600);
MODULE_PARM_DESC(timingcable, "Set to 1 for enabling timing cable.  This means that *all* cards in the system are linked together with a single timing cable");

module_param(forceload, int, 0600);
MODULE_PARM_DESC(forceload, "Set to 1 in order to force an FPGA reload after power on (currently only for HA8/HB8 cards).");

module_param(alawoverride, int, 0400);
MODULE_PARM_DESC(alawoverride, "This option has been deprecated. Please use "\
			     "the parameter \"companding\" instead");

module_param(companding, charp, 0400);
MODULE_PARM_DESC(companding, "Change the companding to \"auto\" or \"alaw\" " \
		"or \"ulaw\". Auto (default) will set everything to ulaw " \
		"unless a BRI module is installed. It will use alaw in that "
		"case.");

MODULE_DESCRIPTION("VoiceBus Driver for Wildcard Analog and Hybrid Cards");
MODULE_AUTHOR("Digium Incorporated <support@digium.com>");
MODULE_ALIAS("wctdm8xxp");
MODULE_ALIAS("wctdm4xxp");
MODULE_ALIAS("wcaex24xx");
MODULE_ALIAS("wcaex8xx");
MODULE_ALIAS("wcaex4xx");
MODULE_LICENSE("GPL v2");

module_init(wctdm_init);
module_exit(wctdm_cleanup);