summaryrefslogblamecommitdiffstats
path: root/private/ntos/ndis/madge/driver/hwi_pcmc.c
blob: f5aa76c3a4eb38b5a496f1125eadccc4d24cf7a7 (plain) (tree)
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





































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                 
/****************************************************************************
*                                                                          
* HWI_PCMC.C : Part of the FASTMAC TOOL-KIT (FTK)                     
*                                                                          
* THE HARDWARE INTERFACE MODULE FOR PCMCIA CARDS                        
*
* Copyright (c) Madge Networks Ltd. 1990-1994                         
* 
* COMPANY CONFIDENTIAL                                                        
*                                                                          
****************************************************************************
*                                                                          
* The purpose of the Hardware Interface (HWI) is to supply an adapter card 
* independent interface to any driver.  It  performs  nearly  all  of  the 
* functions  that  involve  affecting  SIF registers on the adapter cards. 
* This includes downloading code to, initializing, and removing adapters.  
*                                                                          
* The HWI_PCMC.C module contains the routines  specific  to  PCMCIA  cards 
* which  are necessary to install an adapter, to initialize an adapter, to 
* remove an adapter and to handle interrupts on an adapter.                
*                                                                          
****************************************************************************/

/*---------------------------------------------------------------------------
|                                                                          
| DEFINITIONS                                         
|                                                                          
---------------------------------------------------------------------------*/

#include "ftk_defs.h"

/*---------------------------------------------------------------------------
|                                                                          
| MODULE ENTRY POINTS                                         
|                                                                          
---------------------------------------------------------------------------*/

#include "ftk_intr.h"   /* routines internal to FTK                        */
#include "ftk_extr.h"   /* routines provided or used by external FTK user  */

#ifndef FTK_NO_PCMCIA

#ifndef FTK_NO_PROBE
/*---------------------------------------------------------------------------
|                                                                          
| Card Services related defines and globals. Used only in this module.     
|                                                                          
---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|                                                                          
| #defines                                                                 
|                                                                          
---------------------------------------------------------------------------*/

#define DETERMINED_BY_CARD_SERVICES     0x00FF

#define MAX_PCMCIA_SOCKETS              0x08

#define PCMCIA_CS_REGISTRATION_TIMEOUT  0xF0000 /* Not real time. Just a   */
						/* for loop counting so    */
						/* 0xF0000 times.          */

/*---------------------------------------------------------------------------
|                                                                          
| These are hardware related defines. These  are  not  put  in  ftk_pcmc.h 
| because:  1.  they  are  only used here and not by ftk user. 2. they are 
| defined using #defines in sys_cs.h which is only included internally.    
|
---------------------------------------------------------------------------*/

/*                                                                          
 * Interrupt channels supported in form of bit mask.
 */

#define PCMCIA_AVAILABLE_IRQ_MASK \
				  \
 (IRQ_2|IRQ_3|IRQ_5|IRQ_6|IRQ_7|IRQ_8|IRQ_10|IRQ_11|IRQ_12|IRQ_13|IRQ_14|IRQ_15)

/*
 * This  is  the value of IRQInfo1 used  in RequestConfiguration. Note that 
 * only level triggered interupt is supported.                              
 */                                                                          

/*
 * User has specified an interrupt channel.
 */

#define PCMCIA_IRQINFO1_SPECIFIED           IRQ_INFO1_LEVEL

/*
 * User has not specified an interrupt channel, will be allocated by PCMCIA 
 * card services.                                                           
 */

#define PCMCIA_IRQINFO1_NOT_SPECIFIED \
				      \
    (IRQ_INFO1_LEVEL | IRQ_INFO1_INFO2_ENABLE)
    

/*---------------------------------------------------------------------------
|                                                                          
| PCMCIA client information                                                
|                                                                          
---------------------------------------------------------------------------*/

#define PCMCIA_VENDOR_NAME           "MADGE"
#define PCMCIA_VENDOR_NAME_LEN       6

				   /* 1234567890123456789012345678901 */
#define PCMCIA_CLIENT_NAME           "SMART 16/4 PCMCIA RINGNODE HWI"
#define PCMCIA_CLIENT_NAME_LEN       31

#define PCMCIA_VENDOR_REVISION       0x0001

#define PCMCIA_VENDOR_REVISION_DATE  0x1C2D   /* 13/01/94 in dos format    */

struct STRUCT_MADGE_CLIENT_INFODD
{
    CS_CLIENT_INFO      info;
    BYTE                NameString[PCMCIA_CLIENT_NAME_LEN];
    BYTE                VendorString[PCMCIA_VENDOR_NAME_LEN];
};

typedef struct STRUCT_MADGE_CLIENT_INFODD MADGE_CLIENT_INFO;

#define DEFAULT_CLIENT_INFO                                                 \
{                                                                           \
    {                                                                       \
	0x0000,                                                             \
	sizeof(MADGE_CLIENT_INFO),                                          \
	RC_ATTR_IO_CLIENT_DEVICE_DRIVER | RC_ATTR_IO_INSERTION_SHARABLE,    \
	PCMCIA_VENDOR_REVISION,                                             \
	CARD_SERVICES_VERSION,                                              \
	PCMCIA_VENDOR_REVISION_DATE,                                        \
	20,                                                                 \
	PCMCIA_CLIENT_NAME_LEN,                                             \
	20+PCMCIA_CLIENT_NAME_LEN,                                          \
	PCMCIA_VENDOR_NAME_LEN,                                             \
    },                                                                      \
    PCMCIA_CLIENT_NAME,                                                     \
    PCMCIA_VENDOR_NAME,                                                     \
}


/*---------------------------------------------------------------------------
|                                                                          
| PCMCIA sockets record structures                                         
|                                                                          
---------------------------------------------------------------------------*/

enum ENUM_SOCKET_STATUS
{
    SOCKET_NOT_INITIALIZED = 0,
    SOCKET_READY,
    SOCKET_IN_USE,
};

typedef enum ENUM_SOCKET_STATUS SOCKET_STATUS;

struct STRUCT_PCMCIA_SOCKET_RECORD
{
    WORD                ClientHandle;
    WORD                io_location;
    WORD                irq_number;
    SOCKET_STATUS       status;
    ADAPTER_HANDLE      adapter_handle;
};
    
typedef struct STRUCT_PCMCIA_SOCKET_RECORD PCMCIA_SOCKET_RECORD;


/*---------------------------------------------------------------------------
|                                                                          
| Things use to set up argument buffer                                     
|                                                                          
---------------------------------------------------------------------------*/

/*
 * Default arg buffer length.
 */

#define MAX_PCMCIA_ARG_BUFFER_LEN       100

/*
 * Arg buffer structure.
 */

/*
 * Madge Card Services expects the string "Madge" prepended to the argument 
 * buffer.                                                                  
 */                                                                          
    
struct STRUCT_PCMCIA_ARG_BUFFER
{
    BYTE  Madge[5];
    BYTE  Buf[MAX_PCMCIA_ARG_BUFFER_LEN];
};

typedef struct STRUCT_PCMCIA_ARG_BUFFER  PCMCIA_ARG_BUFFER;

/*                                                                          
 * A macro which creates a new argument buffer and initializes it.          
 */                                                                          
  
#define NEW_PCMCIA_ARG_BUFFER(Fp)                                           \
									    \
    PCMCIA_ARG_BUFFER _xXx_arg_buf_##Fp = {{'M','a','d','g','e'}, {0x00}};  \
    BYTE FAR * ##Fp = (BYTE FAR *)(_xXx_arg_buf_##Fp.Buf)


/*---------------------------------------------------------------------------
|                                                                          
| Global variables Used by Card Services related routines                  
|                                                                          
---------------------------------------------------------------------------*/

/*
 * PCMCIA Socket Record. One for each socket. Index by socket no. i.e. 0 to
 * MAX_PCMCIA_SOCKETS-1                                                     
 */

PCMCIA_SOCKET_RECORD pcmcia_socket_table[MAX_PCMCIA_SOCKETS] =
{
    {0x0000, 0x0000, 0x0000, SOCKET_NOT_INITIALIZED, 0x0000},
};

WORD CardServicesVersion; /* Version of Card Services found                */

/*
 * A flag set by callback to signal of registration completion
 */

WBOOLEAN RegisterClientCompleted = FALSE;

/*
 * A  signature string found on Madge 16 4 PCMCIA Ringnode. Use in adapter
 * groping.                                                                 
 */

BYTE MADGE_TPLLV1_INFO[] = MADGE_TPLLV1_INFO_STRING;

/*
 * A bit mask of interrupt channel current used by active ringnode.
 */

WORD UsedIrqChannelsMask = 0x0000;

/*
 * The default client information. Reply with this for CLIENT_INFO callback.
 */

MADGE_CLIENT_INFO default_client_info = DEFAULT_CLIENT_INFO;

#endif


/*---------------------------------------------------------------------------
|                                                                          
| LOCAL PROCEDURES                                    
|                                                                          
---------------------------------------------------------------------------*/

local WORD 
hwi_pcmcia_read_eeprom_word(
    ADAPTER * adapter,
    WORD word_address);

local WORD
pcmcia_c46_read_data(
    ADAPTER * adapter);

local void
pcmcia_c46_write_bit(
    ADAPTER * adapter,
    WORD      mask,
    WBOOLEAN  set_bit);

local void
pcmcia_c46_twitch_clock(
    ADAPTER * adapter);

#ifndef FTK_NO_PROBE

local WBOOLEAN
hwi_pcmcia_card_services_setup(
    PROBE *   resource
    );

local WORD
hwi_pcmcia_cs_release_config(
    WORD ClientHandle,
    WORD Socket
    );
    
local WORD
hwi_pcmcia_cs_release_io(
    WORD ClientHandle,
    WORD Socket,
    WORD IoLocation
    );
    
local WORD
hwi_pcmcia_cs_release_irq(
    WORD ClientHandle,
    WORD Socket,
    WORD IRQChannel
    );
    
local WORD
hwi_pcmcia_cs_deregister_client(
    WORD ClientHandle
    );

local WBOOLEAN
hwi_pcmcia_tear_down_cs(
    PROBE   resource
    );

#endif

#ifndef FTK_NO_PROBE
/****************************************************************************
*
*                         hwi_pcmcia_probe_card
*                         =====================
*
*
* PARAMETERS (passed by hwi_probe_adapter) :
* ==========================================
*
* PROBE * resources
*
* resources is an array structures used to identify and record specific 
* information about adapters found.
*
* UINT    length
*
* length is the number of structures pointed to by reources.
*
* WORD *  valid_locations
*
* valid_locations is an array of IO locations to examine for the presence
* of an adapter. For PCMCIA adapters with should be a subset of
* {0x0a20, 0x1a20, 0x2a20, 0x3a20}.
*
* UINT    number_locations
*
* This is the number of IO locations in the above list.
*
* BODY :
* ======
* 
* The  hwi_pcmcia_probe_card routine is called by  hwi_probe_adapter.  It
* reads the id registers to find the type of card and also reads the IRQ. 
*
*
* RETURNS :
* =========
* 
* The routine returns the number of adapters found, or PROBE_FAILURE if
* there's a problem.
*
****************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_pcmcia_probe_card)
#endif

export UINT
hwi_pcmcia_probe_card(
    PROBE * resources,
    UINT    length,
    WORD *  valid_locations,
    UINT    number_locations
    )
{
    UINT    i;
    UINT    j;

    /* 
     * Check the bounds to make sure they're sensible
     */

    if(length <= 0 || number_locations <= 0)
    {
	return PROBE_FAILURE;
    }

    /*
     * j is the number of adapters found.
     */

    j = 0;

    for(i = 0; i < number_locations; i++)
    {
	/*
	 * If we've run out of PROBE structures then return.
	 */

	if(j >= length)
	{
	   return(j);
	}

#ifndef FTK_NO_IO_ENABLE
	macro_probe_enable_io(valid_locations[i], PCMCIA_IO_RANGE);
#endif

	resources[j].io_location = valid_locations[i];

	if ( hwi_pcmcia_card_services_setup(&resources[j]) )
	{
	   resources[j].adapter_card_type =
	       ADAPTER_CARD_TYPE_16_4_PCMCIA;
	   resources[j].adapter_ram_size =
	       PCMCIA_RAM_SIZE;
	   resources[j].adapter_card_bus_type =
	       ADAPTER_CARD_PCMCIA_BUS_TYPE;
	   resources[j].dma_channel =
	       0;
	   resources[j].transfer_mode =
	       PIO_DATA_TRANSFER_MODE;
	   
	   /*
	    * Increment j to point at the next PROBE structure.
	    */

	   j++;

	   /* 
	    * HACK!! Card Services doesn't seem to be re-entrant so quit
	    * straight away.
	    */

	   return j;
	}

#ifndef FTK_NO_IO_ENABLE
	macro_probe_disable_io(resources->io_location, PCMCIA_IO_RANGE);
#endif
    }

    return(j);
}


/****************************************************************************
*                                                                          
*                     hwi_pcmcia_deprobe_card                              
*                     =======================                              
*                                                                          
*                                                                          
* PARAMETERS (passed by hwi_probe_adapter) :                               
* ==========================================                               
*                                                                          
* PROBE            resource                                                
*                                                                          
* This structure is used to identify and record specific information about 
* the adapter.                                                             
*                                                                          
* BODY :                                                                   
* ======                                                                   
*                                                                          
* The  hwi_smart16_probe_card routine is called by  hwi_probe_adapter.  It 
* probes the  adapter card for information such as DMA channel, IRQ number 
* etc. This information can then be supplied by the user when starting the 
* adapter.                                                                 
*                                                                          
*                                                                          
* RETURNS :                                                                
* =========                                                                
*                                                                          
* The routine  returns the  number of adapters found, or zero if there's a 
* problem                                                                  
*                                                                          
****************************************************************************/

#ifdef FTK_RES_FUNCTION
#pragma FTK_RES_FUNCTION(hwi_pcmcia_deprobe_card)
#endif

export WBOOLEAN
hwi_pcmcia_deprobe_card(
    PROBE    resource
    )
{
    WBOOLEAN success;

#ifndef FTK_NO_IO_ENABLE
    macro_probe_enable_io(resource.io_location, PCMCIA_IO_RANGE);
#endif

    /*
     * Release resources requested from card services and deregister.        
     */

    success = hwi_pcmcia_tear_down_cs(resource);

#ifndef FTK_NO_IO_ENABLE
    macro_probe_disable_io(resource.io_location, PCMCIA_IO_RANGE);
#endif

    return(success);
}
#endif

/****************************************************************************
*                                                                          
*                      hwi_pcmcia_install_card                             
*                      =======================                             
*                                                                          
*                                                                          
* PARAMETERS (passed by hwi_install_adapter) :                             
* ============================================                             
*                                                                          
* ADAPTER * adapter                                                        
*                                                                          
* This structure is used to identify and record specific information about 
* the required adapter.                                                    
*                                                                          
* DOWNLOAD_IMAGE * download_image                                          
*                                                                          
* This  is  the code to be downloaded to the adapter. The image must be of 
* the correct type i.e.  must be downloadable into  the  adapter.  If  the 
* pointer is 0 downloading is not done.                                    
*                                                                          
*                                                                          
* BODY :                                                                   
* ======                                                                   
*                                                                          
* The hwi_pcmcia_install_card routine is  called  by  hwi_install_adapter. 
* It  sets  up  the  adapter  card  and downloads the required code to it. 
*                                                                          
* Firstly,  it checks if the I O location and interrupt channel are valid
* Then  it registers with PCMCIA card services, and checks if the required 
* Madge PCMCIA ringnode exists.  If so  it  requests  interrrupt  and  I 
* resources  from  PCMCIA  card  services,  sets  up  and  checks numerous 
* on-board registers for correct operation. Burnt in address  and  default 
* ring speed are read from EEPROM.                                         
*                                                                          
* Then,  it  halts  the  EAGLE, downloads the code, restarts the EAGLE and 
* waits up to 3 seconds for a  valid  bring-up  code.  If  interrupts  are 
* required, these are enabled by operating system specific calls.          
*                                                                          
*                                                                          
* RETURNS :                                                                
* =========                                                                
*                                                                          
* The routine returns TRUE if it succeeds.  If this routine fails (returns 
* FALSE)  then a subsequent call to driver_explain_error, with the adapter 
* handle corresponding to the adapter parameter used here,  will  give  an 
* explanation.                                                             
*                                                                          
****************************************************************************/

void hlpr_unmap_PCMCIA_irq(WORD irq);

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_pcmcia_install_card)
#endif

export WBOOLEAN
hwi_pcmcia_install_card(
    ADAPTER *        adapter,
    DOWNLOAD_IMAGE * download_image
    )
{
    WORD             control_1      = adapter->io_location +
					  PCMCIA_CONTROL_REGISTER_1;
    WORD             control_2      = adapter->io_location +
					  PCMCIA_CONTROL_REGISTER_2;
    WORD             eeprom_word;
    WORD             ring_speed;
    WORD             sif_base;
    BYTE             tmp_byte;

#ifdef PCMCIA_POINT_ENABLE
    /*
     * Make sure we don't lose any interrupts.
     */

    adapter->drop_int = FALSE;

    /*
     * Enable the card.
     */

    if(!sys_pcmcia_point_enable(adapter))
    {
        /*
         * Error record already filled in.
         */ 

        return(FALSE);
    }
#endif

    /*
     * We don't do any validation on the user supplied adapter details
     * since the user has to obtain the values from card services in the
     * first place! (Or did they?)                             
     */

    /*
     * Save IO location of the SIF registers.
     */

    sif_base = adapter->io_location + PCMCIA_FIRST_SIF_REGISTER;

    adapter->sif_dat     = sif_base + EAGLE_SIFDAT;
    adapter->sif_datinc  = sif_base + EAGLE_SIFDAT_INC;
    adapter->sif_adr     = sif_base + EAGLE_SIFADR;
    adapter->sif_int     = sif_base + EAGLE_SIFINT;
    adapter->sif_acl     = sif_base + PCMCIA_EAGLE_SIFACL;
    adapter->sif_adr2    = sif_base + PCMCIA_EAGLE_SIFADR_2;
    adapter->sif_adx     = sif_base + PCMCIA_EAGLE_SIFADX;
    adapter->sif_dmalen  = sif_base + PCMCIA_EAGLE_DMALEN;

    adapter->io_range = PCMCIA_IO_RANGE;

#ifndef FTK_NO_IO_ENABLE
    macro_enable_io(adapter);
#endif

    /*
     * Can be assumed for a PCMCIA adapter.
     */

    adapter->adapter_card_type     = ADAPTER_CARD_TYPE_16_4_PCMCIA;

#if 0

    adapter->adapter_card_revision = 0x0000;
    adapter->adapter_ram_size      = PCMCIA_RAM_SIZE;

#endif

    /*
     * Interrupts are always level triggered.
     */

    adapter->edge_triggered_ints = FALSE;

    /*
     * To get the software handeshake to work properly on PIO
     * we need to set the MC_AND_ISACP_USE_PIO bit in the
     * mc32_config byte.
     */

    adapter->mc32_config = MC_AND_ISACP_USE_PIO;

    /*
     * Read node address from serial EEPROM.
     */

    eeprom_word = hwi_pcmcia_read_eeprom_word(
		      adapter,
		      EEPROM_ADDR_NODEADDR1);
    adapter->permanent_address.byte[0] = (BYTE)(eeprom_word & 0x00FF);
    adapter->permanent_address.byte[1] = (BYTE)(eeprom_word >> 8);

    eeprom_word = hwi_pcmcia_read_eeprom_word(
		      adapter,
		      EEPROM_ADDR_NODEADDR2);
    adapter->permanent_address.byte[2] = (BYTE)(eeprom_word & 0x00FF);
    adapter->permanent_address.byte[3] = (BYTE)(eeprom_word >> 8);

    eeprom_word = hwi_pcmcia_read_eeprom_word(
		      adapter,
		      EEPROM_ADDR_NODEADDR3);
    adapter->permanent_address.byte[4] = (BYTE)(eeprom_word & 0x00FF);
    adapter->permanent_address.byte[5] = (BYTE)(eeprom_word >> 8);

    /*
     * Read ring speed from serial EEPROM.
     */

    ring_speed  = hwi_pcmcia_read_eeprom_word(
		      adapter,
		      EEPROM_ADDR_RINGSPEED);

    /*
     * Read RAM size from serial EEPROM. Use 512k default if nothing
     * specified.
     */

    adapter->adapter_ram_size = hwi_pcmcia_read_eeprom_word(
				    adapter,
				    EEPROM_ADDR_RAMSIZE
				    ) * 128;

    if (adapter->adapter_ram_size == 0)
    {
	adapter->adapter_ram_size = PCMCIA_RAM_SIZE;
    }

    /*
     * Read hardware revision from serial EEPROM.
     */

    adapter->adapter_card_revision = hwi_pcmcia_read_eeprom_word(
					 adapter,
					 EEPROM_ADDR_REVISION);

    adapter->use_32bit_pio = FALSE;

#ifdef FTK_PCMCIA_32BIT_PIO

    if (adapter->adapter_card_revision != 0)
    {
	adapter->use_32bit_pio = TRUE;
    }

#endif

    /* 
     * Set all the EEPROM related bits in control register 2 to zero.
     */
    
    tmp_byte = 0x00;
    
    tmp_byte &=  ~( PCMCIA_CTRL2_E2DO | PCMCIA_CTRL2_E2DI | 
		    PCMCIA_CTRL2_E2CS | PCMCIA_CTRL2_E2SK |
		    PCMCIA_CTRL2_FLSHWREN );
    
    /*
     * Set SIF clock frequency to 16MHz.
     */
    
    tmp_byte |= PCMCIA_CTRL2_SBCKSEL_16;
    
    /*
     * Ring Speed is read from EEPROM, now program it to the front
     * end chip.
     */
    
    if (adapter->set_ring_speed == 16)
    {
	tmp_byte |= ( PCMCIA_CTRL2_4N16 & PCMCIA_CTRL2_4N16_16 ); 
    }
    else if (adapter->set_ring_speed == 4)
    {
	tmp_byte |= ( PCMCIA_CTRL2_4N16 & PCMCIA_CTRL2_4N16_4 ); 
    }
    else if ( ring_speed == EEPROM_RINGSPEED_4 )
    {
	tmp_byte |= ( PCMCIA_CTRL2_4N16 & PCMCIA_CTRL2_4N16_4 ); 
    }
    else 
    {
	tmp_byte |= ( PCMCIA_CTRL2_4N16 & PCMCIA_CTRL2_4N16_16 ); 
    }
    
    /*
     * Write all these setting to control register 2 all in one go.
     */

    sys_outsb(adapter->adapter_handle, control_2, tmp_byte);

    
    /*
     * Bring EAGLE out of reset.
     */

    macro_setb_bit(
	adapter->adapter_handle,
	control_1,
	PCMCIA_CTRL1_SRESET);

    /*
     * Halt EAGLE, no need to page in extended SIF registers for pcmcia.
     */

    hwi_halt_eagle(adapter);

    /*
     * Download code to adapter. View download image as a sequence of
     * download records.                
     * Pass address of routine to set up DIO addresses on pcmcia cards. If 
     * routine fails return failure (error record already filled in).
     */

    if (!hwi_download_code(
	     adapter,
	     (DOWNLOAD_RECORD *) download_image,
	     hwi_pcmcia_set_dio_address))
    {
#ifndef FTK_NO_IO_ENABLE
	macro_disable_io(adapter);
#endif
	return FALSE;
    }

    /*
     * Start EAGLE, no need page in extended SIF registers for pcmcia cards.
     */

    hwi_start_eagle(adapter);

    /*
     * Wait for a valid bring up code, may wait 3 seconds. If routine fails
     * return failure (error record already filled in).
     */


    if (!hwi_get_bring_up_code(adapter))
    {
#ifndef FTK_NO_IO_ENABLE
	macro_disable_io(adapter);
#endif
	return FALSE;
    }

    /*
     * Set DIO address to point to EAGLE DATA page 0x10000L.
     */

    hwi_pcmcia_set_dio_address(adapter, DIO_LOCATION_EAGLE_DATA_PAGE);

    /*
     * Set maximum frame size from the ring speed.
     */

    adapter->max_frame_size = hwi_get_max_frame_size(adapter);

    /* 
     * Get the ring speed.
     */

    adapter->ring_speed = hwi_get_ring_speed(adapter);

    /*
     * If not in polling mode then set up interrupts interrupts_on field
     * is used when disabling interrupts for adapter.
     */

    if (adapter->interrupt_number != POLLING_INTERRUPTS_MODE)
    {
	adapter->interrupts_on = sys_enable_irq_channel(
				     adapter->adapter_handle,
				     adapter->interrupt_number);

	if (!adapter->interrupts_on)
	{
	    adapter->error_record.type = ERROR_TYPE_HWI;
	    adapter->error_record.value = HWI_E_0B_FAIL_IRQ_ENABLE;
#ifndef FTK_NO_IO_ENABLE
	    macro_disable_io(adapter);
#endif
	    return FALSE;
	}
    }
    else
    {
	adapter->interrupts_on = TRUE;
    }
    

    /*
     * Enable interrupts at adapter GCR1 SINTREN.
     */
    
    macro_setb_bit(
	adapter->adapter_handle,
	control_1,
	PCMCIA_CTRL1_SINTREN);
    
    /*
     * PCMCIA adapters do not have DMA channel to setup.
     */
    
#ifndef FTK_NO_IO_ENABLE
    macro_disable_io(adapter);
#endif
    
    return TRUE;
}


/****************************************************************************
*                                                                          
*                      hwi_pcmcia_interrupt_handler                        
*                      ============================                        
*                                                                          
*                                                                          
* PARAMETERS (passed by hwi_interrupt_entry) :                             
* ============================================                             
*                                                                          
* ADAPTER_HANDLE adapter_handle                                            
*                                                                          
* The adapter handle for the adapter so it can later be passed to the user 
* supplied user_receive_frame or user_completed_srb routine.               
*                                                                          
* ADAPTER * adapter                                                        
*                                                                          
* This structure is used to identify and record specific information about 
* the required adapter.                                                    
*                                                                          
*                                                                          
* BODY :                                                                   
* ======                                                                   
*                                                                          
* The hwi_pcmcia_interrupt_handler routine is called,  when  an  interrupt 
* occurs,  by  hwi_interrupt_entry.  It checks to see if a particular card 
* has interrupted.  The interrupt could be generated by the SIF or  a  PIO 
* data  transfer.  Note it could in fact be the case that no interrupt has 
* occured on the particular adapter being checked.                         
*                                                                          
* On SIF interrupts, the interrupt is acknowledged and cleared.  The value 
* in the SIF interrupt register is recorded in order to  pass  it  to  the 
* driver_interrupt_entry routine (along with the adapter details).         
*                                                                          
* On  PIO  interrupts,  the  length, direction and physical address of the 
* transfer is determined.  A system provided routine is called to  do  the 
* data transfer itself.  Note the EAGLE thinks it is doing a DMA transfer. 
*                                                                          
*                                                                          
* RETURNS :                                                                
* =========                                                                
*                                                                          
* The routine always successfully completes.                               
*                                                                        
****************************************************************************/

#ifdef FTK_IRQ_FUNCTION
#pragma FTK_IRQ_FUNCTION(hwi_pcmcia_interrupt_handler)
#endif

#ifndef WIN_BOOK_FIX
      
/*---------------------------------------------------------------------------
Ý
Ý This is the ordinary, none WinBook, interrupt handler.
Ý
Ý--------------------------------------------------------------------------*/

export void
hwi_pcmcia_interrupt_handler(
    ADAPTER *      adapter
    )
{
    ADAPTER_HANDLE adapter_handle  = adapter->adapter_handle;
    WORD           control_1       = adapter->io_location +
					 PCMCIA_CONTROL_REGISTER_1;
    WORD           control_2       = adapter->io_location +
					 PCMCIA_CONTROL_REGISTER_2;
    WORD           pio_location    = adapter->io_location + 
					 PCMCIA_PIO_IO_LOC;
    WORD           sifint_register = adapter->sif_int;
    WORD           sifadr          = adapter->sif_adr;
    WORD           sifdat          = adapter->sif_dat;
    WORD           sifacl          = adapter->sif_acl;
    WORD           sifint_value;
    WORD           sifint_tmp;    
    BYTE     FAR * pio_address;
    WORD           pio_len_bytes;
    WORD           pio_from_adapter;

    WORD           saved_sifadr;
    WORD           dummy;

    DWORD          dma_high_word;
    WORD           dma_low_word;

    WORD           temp_word;

#ifndef FTK_NO_IO_ENABLE 
    macro_enable_io( adapter);
#endif

    /*
     * Check for SIF interrupt or PIO interrupt.
     */

    if ((sys_insw(adapter_handle, sifint_register) &
	     FASTMAC_SIFINT_IRQ_DRIVER) != 0)
    {
	/*
	 * A SIF interrupt has occurred. Thsi could be an SRB free,
	 * an adapter check or a received frame interrupt.
	 */

	/*
	 * Clear SIF interrupt enable bit in control register.
	 */

	macro_clearb_bit(adapter_handle, control_1, PCMCIA_CTRL1_SINTREN);

	/* 
	 * Clear EAGLE_SIFINT_HOST_IRQ to acknowledge interrupt at SIF.
	 */

	/*
	 * WARNING: Do NOT reorder the clearing of the SIFINT register with 
	 *   the reading of it. If SIFINT is cleared after reading it,  any 
	 *   interrupts raised after reading it will  be  lost.  Admittedly 
	 *   this is a small time frame, but it is important.               
	 */

	sys_outsw(adapter_handle, sifint_register, 0);

	/*
	 * Record the EAGLE SIF interrupt register value.
	 */

	/*
	 * WARNING: Continue to read the SIFINT register until it is stable 
	 *   because of a potential problem involving the host reading  the 
	 *   register after the adapter has written the low byte of it, but 
	 *   before it has written the high byte. Failure to wait  for  the 
	 *   SIFINT register to settle can cause spurious interrupts.       
	 */

	sifint_value = sys_insw(adapter_handle, sifint_register);
	do
	{
	    sifint_tmp   = sifint_value;
	    sifint_value = sys_insw(adapter_handle, sifint_register);
	} 
	while (sifint_tmp != sifint_value);

	/*
	 * Acknowledge clear interrupt at interrupt controller.
	 */   

#ifndef FTK_NO_CLEAR_IRQ
	sys_clear_controller_interrupt(
	    adapter_handle,
	    adapter->interrupt_number
	    );
#endif

	/*
	 * Set interrupt enable bit to regenerate any lost interrupts.
	 */

	macro_setb_bit(adapter_handle, control_1, PCMCIA_CTRL1_SINTREN);

	/* 
	 * It is possible that the PCMCIA adapter may have been removed. In
	 * which case we would expect to get 0xffff from an IO location
	 * occupied by the adapter. We will check the value of SIFADR since
	 * this should never be 0xffff.
	 */

	if (sys_insw(adapter_handle, sifadr) == 0xffff)
	{
#ifndef FTK_NO_IO_ENABLE 
	    macro_disable_io( adapter);
#endif

#ifdef FTK_ADAPTER_REMOVED_NOTIFY
	    user_adapter_removed(adapter_handle);
#endif
	    return;
	}

	/*
	 * Call driver with details of SIF interrupt.
	 */

	driver_interrupt_entry(adapter_handle, adapter, sifint_value);

    }
    else if ((sys_insb(adapter_handle, control_1) & PCMCIA_CTRL1_SHRQ) != 0)
    {
	/*
	 * A PIO interrupt has occurred. Transfer data to/from adapter.
	 */

	saved_sifadr = sys_insw(adapter_handle, sifadr);

	/*
	 * Read the physical address for the PIO through DIO space from the 
	 * SIF registers. Because the SIF thinks that it is doing real DMA, 
	 * the SDMAADR SDMAADX registers cannot be paged in, so they  must
	 * be read from their memory mapped locations in Eagle memory.      
	 */

	sys_outsw(adapter_handle, sifadr, DIO_LOCATION_EXT_DMA_ADDR);
	dma_high_word = (DWORD)sys_insw(adapter_handle, sifdat);

	sys_outsw(adapter_handle, sifadr, DIO_LOCATION_DMA_ADDR);
	dma_low_word = sys_insw(adapter_handle, sifdat);

	pio_address = (BYTE FAR *) ((dma_high_word << 16) | 
				    ((DWORD) dma_low_word));

	/*
	 * Read the PIO length from SIF register.
	 */

	pio_len_bytes = sys_insw(adapter_handle, adapter->sif_dmalen);

	/*
	 * Determine what direction the data transfer is to take place in.
	 */

	pio_from_adapter = sys_insw(
			       adapter_handle,
			       sifacl) & EAGLE_SIFACL_SWDDIR;


	/* 
	 * It is possible that the PCMCIA adapter may have been removed. In
	 * which case we would expect to get 0xffff from an IO location
	 * occupied by the adapter. We will check the value of pio_len_bytes
	 * since this should never be 0xffff. We do this test here as we have
	 * read what we think are the DMA location and length. The following
	 * code may DMA rubbish if the adapter goes away after this test 
	 * but at least we know it will happen to/from a valid host buffer.
	 */

	if (pio_len_bytes == 0xffff)
	{
#ifndef FTK_NO_IO_ENABLE 
	    macro_disable_io(adapter);
#endif
#ifndef FTK_NO_CLEAR_IRQ
	    sys_clear_controller_interrupt(
		adapter_handle,
		adapter->interrupt_number
		);
#endif

#ifdef FTK_ADAPTER_REMOVED_NOTIFY
	    user_adapter_removed(adapter_handle);
#endif

	    return;
	}

	/*
	 * We need to use software handshaking across the PIO.              
	 * Start by writing zero to a magic location on the adapter.        
	 */

	sys_outsw(adapter_handle, sifadr, DIO_LOCATION_DMA_CONTROL);
	sys_outsw(adapter_handle, sifdat, 0);

	/*
	 * Start what the SIF thinks is a DMA  but  is  PIO  instead.  This 
	 * involves  clearing  the  SINTREN  bit  and  setting SHLDA bit on 
	 * control register 1. Note that we have to  keep  SRESET  bit  set 
	 * otherwise we will reset the EAGLE.                               
	 */

	sys_outsb(
	    adapter_handle,
	    control_1,
	    PCMCIA_CTRL1_SHLDA | PCMCIA_CTRL1_SRESET);

	/*
	 * Do the actual data transfer.
	 */

	/*
	 * Note that Fastmac only copies whole WORDs to DWORD boundaries
	 * FastmacPlus, however, can transfer any length to any address.
	 */

	if (pio_from_adapter)
	{
	    /*
	     *
	     *
	     * Transfer into host memory from adapter.
	     *
	     *
	     */

	    /*
	     * Deal with an odd leading byte.
	     */

	    if (((card_t) pio_address & 0x01) != 0)
	    {
		/* 
		 * Read a WORD, the top byte is data.
		 */

		temp_word        = sys_insw(adapter_handle, pio_location);
		pio_len_bytes--;
		*(pio_address++) = (BYTE) (temp_word >> 8);
	    }

	    /*
	     * We might be able to do 32 bit PIO.
	     */

	    if (adapter->use_32bit_pio)
	    {
		/*
		 * Deal with an odd leading word. 
		 */

		if (((card_t) pio_address & 0x02) != 0 && pio_len_bytes > 0)
		{
		    /*
		     * There could be one or two bytes of data.
		     */

		    if (pio_len_bytes == 1)
		    {
			pio_len_bytes--;
			*(pio_address) = 
			    sys_insb(adapter_handle, pio_location);
		    }
		    else
		    {
			pio_len_bytes -= 2;
			*((WORD *) pio_address) = 
			    sys_insw(adapter_handle, pio_location);
			pio_address += 2;
		    }
		}

		/*
		 * Deal with the bulk of the dwords.
		 */

		sys_rep_insd(
		    adapter_handle,
		    pio_location,
		    pio_address,
		    (WORD) (pio_len_bytes >> 2)
		    );

		pio_address += (pio_len_bytes & 0xfffc);

		/*
		 * Deal with a trailing word.
		 */

		if ((pio_len_bytes & 0x02) != 0)
		{
		    /*
		     * Read a word.
		     */

		    *((WORD *) pio_address) =
			sys_insw(adapter_handle, pio_location);
		    pio_address += 2;
		}
	    }

	    /*
	     * Otherwise use 16 bit PIO.
	     */

	    else
	    {
		/*
		 * Transfer the bulk of the data.
		 */

		sys_rep_insw( 
		    adapter_handle, 
		    pio_location,
		    pio_address,
		    (WORD) (pio_len_bytes >> 1)
		    );

		pio_address += (pio_len_bytes & 0xfffe);

	    }

	    /*
	     * Finally transfer any trailing byte.                          
	     */

	    if ((pio_len_bytes & 0x01) != 0)
	    {
		*(pio_address) = sys_insb(adapter_handle, pio_location);
	    }
	}

	else
	{
	    /*
	     *
	     *
	     * Transfer into adapter memory from the host.
	     *
	     *
	     */

	    /*
	     * Deal with a leading odd byte.
	     */
	
	    if (((card_t) pio_address & 0x01) != 0)
	    {
		/*
		 * Write a WORD with data in top byte.
		 */

		temp_word = ((WORD) *(pio_address++)) << 8;
		pio_len_bytes--;
		sys_outsw(adapter_handle, pio_location, temp_word);
	    }

	    /*
	     * We might be able to do 32 bit PIO.
	     */

	    if (adapter->use_32bit_pio)
	    {
		/*
		 * Deal with an odd leading word. 
		 */

		if (((card_t) pio_address & 0x02) != 0 && pio_len_bytes > 0)
		{
		    /*
		     * There could be one or two bytes of data. If there
		     * is  only one byte it goes in the high word.
		     */

		    if (pio_len_bytes == 1)
		    {
			pio_len_bytes--;
			sys_outsb(
			    adapter_handle,
			    pio_location,
			    *(pio_address)
			    );
		    }
		    else
		    {
			pio_len_bytes -= 2;
			sys_outsw(
			    adapter_handle,
			    pio_location,
			    *((WORD *) pio_address)
			    );
			pio_address += 2;
		    }
		}

		/*
		 * Deal with the bulk of the dwords.
		 */

		sys_rep_outsd(
		    adapter_handle,
		    pio_location,
		    pio_address,
		    (WORD) (pio_len_bytes >> 2)
		    );

		pio_address += (pio_len_bytes & 0xfffc);

		/*
		 * Deal with a trailing word.
		 */

		if ((pio_len_bytes & 0x02) != 0)
		{
		    /*
		     * Write a word.
		     */

		    sys_outsw(
			adapter_handle,
			pio_location,
			*((WORD *) pio_address)
			);
		    pio_address += 2;
		}
	    }

	    /*
	     * Otherwise use 16 bit PIO.
	     */

	    else
	    {
		sys_rep_outsw(
		    adapter_handle, 
		    pio_location,
		    pio_address,
		    (WORD) (pio_len_bytes >> 1)
		    );
	
		pio_address += (pio_len_bytes & 0xfffe);
	    }

	    /*
	     * Deal with a trailing byte.
	     */
	    
	    if ((pio_len_bytes & 0x01) != 0)
	    {
		sys_outsb(
		    adapter_handle,
		    pio_location,
		    *(pio_address)
		    );
	    }
	}

	/*
	 * Transfer  done.  We  have to tell the hardware we have finished. 
	 * This involves clearing the SHLDA bit to signal the end  of  PIO.
	 * Note that the SRESET bit is kept set otherwise we will reset the 
	 * EAGLE.    
	 */

	sys_outsb( 
	    adapter_handle,
	    control_1,
	    PCMCIA_CTRL1_SRESET);


	/* 
	 * Poll until SHLAD clears.
	 */
	
	/*
	 * Now wait until SHLDA clears. This is needed since while SHLDA is 
	 * set the EAGLE still believes it is in bus master mode;  and  any 
	 * attempt to access the SIF will fail.                             
	 */

	do
	{
	    dummy = sys_insb(adapter_handle, control_1);

	    /*
	     * It is possible that the adapter was removed during the
	     * transfer. If it was we could spin forever. We will test
	     * the value read from control_1, if it is 0xff then we
	     * assume that the adapter has been removed.
	     */

	    if (dummy == 0xff)
	    {
#ifndef FTK_NO_IO_ENABLE 
		 macro_disable_io( adapter);
#endif

#ifndef FTK_NO_CLEAR_IRQ
		 sys_clear_controller_interrupt(
		     adapter_handle,
		     adapter->interrupt_number);
#endif

#ifdef FTK_ADAPTER_REMOVED_NOTIFY
		 user_adapter_removed(adapter_handle);
#endif

		 return;
	    }
	}
	while ((dummy & PCMCIA_CTRL1_SHLDA) != 0);

	/*
	 * Finish off the software handshake process that we started at the 
	 * beginning.    This   time   we   have   to   write   0xFFFF   to 
	 * DIO_LOCATION_DMA_CONTROL                                         
	
	 * Do a read first - otherwise the write might fail                 
	 */                                                                  
	
	sys_outsw(adapter_handle, sifadr, DIO_LOCATION_DMA_CONTROL);
	dummy = sys_insw(adapter_handle, sifdat);                               

	sys_outsw(adapter_handle, sifadr, DIO_LOCATION_DMA_CONTROL);
	sys_outsw(adapter_handle, sifdat, 0xFFFF);

	sys_outsw(adapter_handle, sifadr, saved_sifadr);

	/* 
	 * acknowledge/clear interrupt at interrupt controller.
	 */

#ifndef FTK_NO_CLEAR_IRQ
	sys_clear_controller_interrupt(
	    adapter_handle,
	    adapter->interrupt_number);
#endif

	/*
	 * Set interrupt enable bit to regenerate any lost interrupts.
	 */

	macro_setb_bit(adapter_handle, control_1, PCMCIA_CTRL1_SINTREN);
    }

#ifndef FTK_NO_IO_ENABLE
    macro_disable_io(adapter);
#endif

}

#else 

/*---------------------------------------------------------------------------
Ý
Ý This is the special interrupt handler for WinBooks.
Ý
Ý On a WinBook there is a 120us window after an interrupt has been 
Ý signalled before the interrupt line floats up to a high enough
Ý level that another interrupt can be generated. To avoid loosing
Ý an interrupt in this window we must hold either not allow another
Ý interrupt to be generated in the windows or poll the adapter for
Ý interrupt causes for the duration of the window. Since on some
Ý OSs we cannot spend very long in our interrupt handler we cannot
Ý just poll (which is the fastest scheme) so we use a mixture. We
Ý use the PIO handshake to delay a second PIO interrupt and poll
Ý for SIF interrupts - which are much rarer.
Ý
---------------------------------------------------------------------------*/

export void
hwi_pcmcia_interrupt_handler(
    ADAPTER *      adapter
    )
{
    ADAPTER_HANDLE adapter_handle  = adapter->adapter_handle;
    WORD           control_1       = adapter->io_location +
					 PCMCIA_CONTROL_REGISTER_1;
    WORD           control_2       = adapter->io_location +
					 PCMCIA_CONTROL_REGISTER_2;
    WORD           pio_location    = adapter->io_location + 
					 PCMCIA_PIO_IO_LOC;
    WORD           sifint_register = adapter->sif_int;
    WORD           sifadr          = adapter->sif_adr;
    WORD           sifdat          = adapter->sif_dat;
    WORD           sifacl          = adapter->sif_acl;
    WORD           sifint_value;
    WORD           sifint_tmp;    
    BYTE     FAR * pio_address;
    WORD           pio_len_bytes;
    WORD           pio_from_adapter;

    WORD           saved_sifadr;
    WORD           dummy;

    DWORD          dma_high_word;
    WORD           dma_low_word;

    WORD           temp_word;
    UINT           i;

#ifndef FTK_NO_IO_ENABLE 
    macro_enable_io( adapter);
#endif

    /*
     * Clear SIF interrupt enable bit in control register.
     */

    macro_clearb_bit(adapter_handle, control_1, PCMCIA_CTRL1_SINTREN);

    /*
     * Check for SIF interrupt or PIO interrupt. We do this 80 times
     * as this has been empirically shown to result in a delay of
     * 120us since we cleared SINTREN above.
     */

    for (i = 0; i < 80; i++)
    {
        if ((sys_insw(adapter_handle, sifint_register) &
                FASTMAC_SIFINT_IRQ_DRIVER) != 0)
        {
            /*
	     * A SIF interrupt has occurred. This could be an SRB free,
	     * an adapter check or a received frame interrupt.
	     */

	    /* 
	     * Clear EAGLE_SIFINT_HOST_IRQ to acknowledge interrupt at SIF.
	     */

	    /*
	     * WARNING: Do NOT reorder the clearing of the SIFINT register with 
	     *   the reading of it. If SIFINT is cleared after reading it,  any 
	     *   interrupts raised after reading it will  be  lost.  Admittedly 
	     *   this is a small time frame, but it is important.               
	     */

	    sys_outsw(adapter_handle, sifint_register, 0);

	    /*
	     * Record the EAGLE SIF interrupt register value.
	     */

	    /*
	     * WARNING: Continue to read the SIFINT register until it is stable 
	     *   because of a potential problem involving the host reading  the 
	     *   register after the adapter has written the low byte of it, but 
	     *   before it has written the high byte. Failure to wait  for  the 
	     *   SIFINT register to settle can cause spurious interrupts.       
	     */

	    sifint_value = sys_insw(adapter_handle, sifint_register);
	    do
	    {
	        sifint_tmp   = sifint_value;
	        sifint_value = sys_insw(adapter_handle, sifint_register);
	    } 
	    while (sifint_tmp != sifint_value);

	    /* 
	     * It is possible that the PCMCIA adapter may have been removed. In
	     * which case we would expect to get 0xffff from an IO location
	     * occupied by the adapter. We will check the value of SIFADR since
	     * this should never be 0xffff.
	     */

	    if (sys_insw(adapter_handle, sifadr) == 0xffff)
	    {
        #ifndef FTK_NO_IO_ENABLE 
	        macro_disable_io( adapter);
        #endif

        #ifdef FTK_ADAPTER_REMOVED_NOTIFY
	        user_adapter_removed(adapter_handle);
        #endif
	        return;
	    }

	    /*
	     * Call driver with details of SIF interrupt.
	     */

	    driver_interrupt_entry(adapter_handle, adapter, sifint_value);
        }
        else if ((sys_insb(adapter_handle, control_1) & PCMCIA_CTRL1_SHRQ) != 0)
        {
	    /*
	     * A PIO interrupt has occurred. Transfer data to/from adapter.
	     */

	    saved_sifadr = sys_insw(adapter_handle, sifadr);

            /*
	     * Read the physical address for the PIO through DIO space from the 
	     * SIF registers. Because the SIF thinks that it is doing real DMA, 
	     * the SDMAADR SDMAADX registers cannot be paged in, so they  must
	     * be read from their memory mapped locations in Eagle memory.      
	     */

	    sys_outsw(adapter_handle, sifadr, DIO_LOCATION_EXT_DMA_ADDR);
	    dma_high_word = (DWORD)sys_insw(adapter_handle, sifdat);

            sys_outsw(adapter_handle, sifadr, DIO_LOCATION_DMA_ADDR);
	    dma_low_word = sys_insw(adapter_handle, sifdat);

	    pio_address = (BYTE FAR *) ((dma_high_word << 16) | 
				        ((DWORD) dma_low_word));

	    /*
	     * Read the PIO length from SIF register.
	     */

	    pio_len_bytes = sys_insw(adapter_handle, adapter->sif_dmalen);

	    /*
	     * Determine what direction the data transfer is to take place in.
	     */

	    pio_from_adapter = sys_insw(
			           adapter_handle,
			           sifacl) & EAGLE_SIFACL_SWDDIR;


	    /* 
	     * It is possible that the PCMCIA adapter may have been removed. In
	     * which case we would expect to get 0xffff from an IO location
	     * occupied by the adapter. We will check the value of pio_len_bytes
	     * since this should never be 0xffff. We do this test here as we have
	     * read what we think are the DMA location and length. The following
	     * code may DMA rubbish if the adapter goes away after this test 
	     * but at least we know it will happen to/from a valid host buffer.
	     */

	    if (pio_len_bytes == 0xffff)
	    {
        #ifndef FTK_NO_IO_ENABLE 
	        macro_disable_io(adapter);
        #endif
        #ifndef FTK_NO_CLEAR_IRQ
	        sys_clear_controller_interrupt(
		    adapter_handle,
		    adapter->interrupt_number
		    );
        #endif

        #ifdef FTK_ADAPTER_REMOVED_NOTIFY
	        user_adapter_removed(adapter_handle);
        #endif

	        return;
	    }

	    /*
	     * We need to use software handshaking across the PIO.              
	     * Start by writing zero to a magic location on the adapter.        
	     */

	    sys_outsw(adapter_handle, sifadr, DIO_LOCATION_DMA_CONTROL);
	    sys_outsw(adapter_handle, sifdat, 0);

	    /*
	     * Start what the SIF thinks is a DMA  but  is  PIO  instead.  This 
	     * involves  clearing  the  SINTREN  bit  and  setting SHLDA bit on 
	     * control register 1. Note that we have to  keep  SRESET  bit  set 
	     * otherwise we will reset the EAGLE.                               
	     */

	    macro_setb_bit(adapter_handle, control_1, PCMCIA_CTRL1_SHLDA);

	    /*
	     * Do the actual data transfer.
	     */

	    /*
	     * Note that Fastmac only copies whole WORDs to DWORD boundaries
	     * FastmacPlus, however, can transfer any length to any address.
	     */

	    if (pio_from_adapter)
	    {
	        /*
	         *
	         *
	         * Transfer into host memory from adapter.
	         *
	         *
	         */

	        /*
	         * Deal with an odd leading byte.
	         */

	        if (((card_t) pio_address & 0x01) != 0)
	        {
		    /* 
		     * Read a WORD, the top byte is data.
		     */

		    temp_word        = sys_insw(adapter_handle, pio_location);
		    pio_len_bytes--;
		    *(pio_address++) = (BYTE) (temp_word >> 8);
	        }

	        /*
	         * We might be able to do 32 bit PIO.
	         */

	        if (adapter->use_32bit_pio)
	        {
		    /*
		     * Deal with an odd leading word. 
		     */

		    if (((card_t) pio_address & 0x02) != 0 && pio_len_bytes > 0)
		    {
		        /*
		         * There could be one or two bytes of data.
		         */

		        if (pio_len_bytes == 1)
		        {
			    pio_len_bytes--;
			    *(pio_address) = 
			        sys_insb(adapter_handle, pio_location);
		        }
		        else
		        {
			    pio_len_bytes -= 2;
			    *((WORD *) pio_address) = 
			    sys_insw(adapter_handle, pio_location);
			    pio_address += 2;
		        }
		    }

		    /*
		     * Deal with the bulk of the dwords.
		     */

		    sys_rep_insd(
		        adapter_handle,
		        pio_location,
		        pio_address,
		        (WORD) (pio_len_bytes >> 2)
		        );

		    pio_address += (pio_len_bytes & 0xfffc);

		    /*
		     * Deal with a trailing word.
		     */

		    if ((pio_len_bytes & 0x02) != 0)
		    {
		        /*
		         * Read a word.
		         */

		        *((WORD *) pio_address) =
			    sys_insw(adapter_handle, pio_location);
		        pio_address += 2;
		    }
	        }

	        /*
	         * Otherwise use 16 bit PIO.
	         */

	        else
	        {
		    /*
		     * Transfer the bulk of the data.
		     */

		    sys_rep_insw( 
		        adapter_handle, 
		        pio_location,
		        pio_address,
		        (WORD) (pio_len_bytes >> 1)
		        );

		    pio_address += (pio_len_bytes & 0xfffe);

	        }

	        /*
	         * Finally transfer any trailing byte.                          
	         */

	        if ((pio_len_bytes & 0x01) != 0)
	        {
		    *(pio_address) = sys_insb(adapter_handle, pio_location);
	        }
	    }

	    else
	    {
	        /*
	         *
	         *
	         * Transfer into adapter memory from the host.
	         *
	         *
	         */

	        /*
	         * Deal with a leading odd byte.
	         */
	
	        if (((card_t) pio_address & 0x01) != 0)
	        {
		    /*
		     * Write a WORD with data in top byte.
		     */

		    temp_word = ((WORD) *(pio_address++)) << 8;
		    pio_len_bytes--;
		    sys_outsw(adapter_handle, pio_location, temp_word);
	        }

	        /*
	         * We might be able to do 32 bit PIO.
	         */

	        if (adapter->use_32bit_pio)
	        {
		    /*
		     * Deal with an odd leading word. 
		     */

		    if (((card_t) pio_address & 0x02) != 0 && pio_len_bytes > 0)
		    {
		        /*
		         * There could be one or two bytes of data. If there
		         * is  only one byte it goes in the high word.
		         */

		        if (pio_len_bytes == 1)
		        {
			    pio_len_bytes--;
			    sys_outsb(
			        adapter_handle,
			        pio_location,
			        *(pio_address)
			        );
		        }
		        else
		        {
			    pio_len_bytes -= 2;
			    sys_outsw(
			        adapter_handle,
			        pio_location,
			        *((WORD *) pio_address)
			        );
			    pio_address += 2;
		        }
		    }

		    /*
		     * Deal with the bulk of the dwords.
		     */

		    sys_rep_outsd(
		        adapter_handle,
		        pio_location,
		        pio_address,
		        (WORD) (pio_len_bytes >> 2)
		        );

		    pio_address += (pio_len_bytes & 0xfffc);

		    /*
		     * Deal with a trailing word.
		     */

		    if ((pio_len_bytes & 0x02) != 0)
		    {
		        /*
		         * Write a word.
		         */

		        sys_outsw(
			    adapter_handle,
			    pio_location,
			    *((WORD *) pio_address)
			    );
		        pio_address += 2;
		    }
	        }

	        /*
	         * Otherwise use 16 bit PIO.
	         */

	        else
	        {
		    sys_rep_outsw(
		        adapter_handle, 
		        pio_location,
		        pio_address,
		        (WORD) (pio_len_bytes >> 1)
		        );
	
		    pio_address += (pio_len_bytes & 0xfffe);
	        }

	        /*
	         * Deal with a trailing byte.
	         */
	    
	        if ((pio_len_bytes & 0x01) != 0)
	        {
		    sys_outsb(
		        adapter_handle,
		        pio_location,
		        *(pio_address)
		        );
	        }
	    }

	    /*
	     * Transfer  done.  We  have to tell the hardware we have finished. 
	     * This involves clearing the SHLDA bit to signal the end  of  PIO.
	     * Note that the SRESET bit is kept set otherwise we will reset the 
	     * EAGLE.    
	     */

	    macro_clearb_bit(adapter_handle, control_1, PCMCIA_CTRL1_SHLDA);

	    /* 
	     * Poll until SHLDA clears.
	     */
	
	    /*
	     * Now wait until SHLDA clears. This is needed since while SHLDA is 
	     * set the EAGLE still believes it is in bus master mode;  and  any 
	     * attempt to access the SIF will fail.                             
	     */

	    do
	    {
	        dummy = sys_insb(adapter_handle, control_1);

	        /*
	         * It is possible that the adapter was removed during the
	         * transfer. If it was we could spin forever. We will test
	         * the value read from control_1, if it is 0xff then we
	         * assume that the adapter has been removed.
	         */

	        if (dummy == 0xff)
	        {
#ifndef FTK_NO_IO_ENABLE 
		    macro_disable_io( adapter);
#endif

#ifndef FTK_NO_CLEAR_IRQ
		    sys_clear_controller_interrupt(
		        adapter_handle,
		        adapter->interrupt_number);
#endif

#ifdef FTK_ADAPTER_REMOVED_NOTIFY
		    user_adapter_removed(adapter_handle);
#endif

		    return;
	        }
	    }
	    while ((dummy & PCMCIA_CTRL1_SHLDA) != 0);

            /*
             * We don't clear the handshake here to stop another
             * PIO interrupt being generated for at least 120us
             */
        }
    }


    /*
     * Finish off the software handshake process that we started at the 
     * beginning.    This   time   we   have   to   write   0xFFFF   to 
     * DIO_LOCATION_DMA_CONTROL                                         
     *	
     * Do a read first - otherwise the write might fail                 
     */                                                                  
	
     sys_outsw(adapter_handle, sifadr, DIO_LOCATION_DMA_CONTROL);
     dummy = sys_insw(adapter_handle, sifdat);                               

     sys_outsw(adapter_handle, sifadr, DIO_LOCATION_DMA_CONTROL);
     sys_outsw(adapter_handle, sifdat, 0xFFFF);

     sys_outsw(adapter_handle, sifadr, saved_sifadr);

     /* 
      * acknowledge/clear interrupt at interrupt controller.
      */

#ifndef FTK_NO_CLEAR_IRQ
     sys_clear_controller_interrupt(
         adapter_handle,
         adapter->interrupt_number);
#endif

     /*
      * Set interrupt enable bit to regenerate any lost interrupts.
      */

     macro_setb_bit(adapter_handle, control_1, PCMCIA_CTRL1_SINTREN);

#ifndef FTK_NO_IO_ENABLE
    macro_disable_io(adapter);
#endif
}

#endif


/****************************************************************************
*                                                                          
*                      hwi_pcmcia_remove_card                              
*                      ======================                              
*                                                                          
*                                                                          
* PARAMETERS (passed by hwi_remove_adapter) :                              
* ===========================================                              
*                                                                          
* ADAPTER * adapter                                                        
*                                                                          
* This structure is used to identify and record specific information about 
* the required adapter.                                                    
*                                                                          
*                                                                          
* BODY :                                                                   
* ======                                                                   
*                                                                          
* The hwi_pcmcia_remove_card routine is called by hwi_remove_adapter.   It 
* disables  interrupts if they are being used. It also resets the adapter. 
* Note there is no need to explicitly disable DMA channels.                
*                                                                          
*                                                                          
* RETURNS :                                                                
* =========                                                                
*                                                                          
* The routine always successfully completes.                               
*                                                                          
****************************************************************************/

#ifdef FTK_RES_FUNCTION
#pragma FTK_RES_FUNCTION(hwi_pcmcia_remove_card)
#endif

export void
hwi_pcmcia_remove_card(
    ADAPTER * adapter
    )
{
    WORD      sif_acontrol = adapter->sif_acl;
    WORD      control_1    = adapter->io_location +
				 PCMCIA_CONTROL_REGISTER_1;

    /*
     * Disable interrupts. Only need to do this if interrupts successfully
     * enabled.              
     * Interrupts must be disabled at adapter before unpatching interrupts. 
     * Even in polling mode we must turn off interrupts at adapter.
     */

#ifndef FTK_NO_IO_ENABLE
    macro_enable_io(adapter);
#endif

    if (adapter->interrupts_on)
    {
	macro_clearw_bit(
	    adapter->adapter_handle,
	    sif_acontrol,
	    EAGLE_SIFACL_SINTEN);

#ifdef PCMCIA_POINT_ENABLE
        /* Deconfigure and power down the card before disabling host
         * interrupt handler in case of spurious interrupts from the 
         * PCMCIA controller.
         */

        /*
         * Warn our handler of the likelihood of a spurious interrupt.
         */

        adapter->drop_int = TRUE;
        sys_pcmcia_point_disable(adapter);
#endif

	if (adapter->interrupt_number != POLLING_INTERRUPTS_MODE)
	{
	    sys_disable_irq_channel(
		adapter->adapter_handle, 
		adapter->interrupt_number);
	}

	adapter->interrupts_on = FALSE;
    }

    /*
     * Perform adapter reset, and set EAGLE_SIFACL_ARESET high.
     */

    macro_clearb_bit(
	adapter->adapter_handle,
	control_1,
	PCMCIA_CTRL1_SRESET);

    macro_setw_bit(
	adapter->adapter_handle, 
	sif_acontrol,
	EAGLE_SIFACL_ARESET);

#ifndef FTK_NO_IO_ENABLE
    macro_disable_io(adapter);
#endif
     
}


/****************************************************************************
*                                                                          
*                      hwi_pcmcia_set_dio_address                          
*                      ==========================                          
*                                                                          
* The hwi_pcmcia_set_dio_address routine is used, with pcmcia  cards,  for 
* putting  a  32 bit DIO address into the SIF DIO address and extended DIO 
* address registers.                                                       
*                                                                          
****************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_pcmcia_set_dio_address)
#endif

export void  
hwi_pcmcia_set_dio_address(
    ADAPTER * adapter,
    DWORD     dio_address
    )
{
    WORD      sif_dio_adr  = adapter->sif_adr;
    WORD      sif_dio_adrx = adapter->sif_adx;

    /*
     * Load extended DIO address register with top 16 bits of address.      
     * Always load extended address register first.                         
     * Note pcmcia cards have single page of all SIF registers hence do not 
     * need page in certain SIF registers.                                  
     */

    sys_outsw(
	adapter->adapter_handle, 
	sif_dio_adrx,
	(WORD)(dio_address >> 16));

    /*
     * Load DIO address register with low 16 bits of address.
     */

    sys_outsw( 
	adapter->adapter_handle,
	sif_dio_adr,
	(WORD)(dio_address & 0x0000FFFF));

}


/*---------------------------------------------------------------------------
|                                                                          
| LOCAL PROCEDURES                                    
|                                                                          
---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|                                                                          
|                     pcmcia_c46_write_bit                                 
|                     ====================                                 
|                                                                          
| Write a bit to the SEEPROM control register.                             
|                                                                          
---------------------------------------------------------------------------*/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pcmcia_c46_write_bit)
#endif

local void
pcmcia_c46_write_bit(
    ADAPTER * adapter,
    WORD      mask,
    WBOOLEAN  set_bit)
{
    WORD      ctrl_reg;

    /*
     * Get the current value of the SEEPROM control register.               
     */

    ctrl_reg = sys_insb(
		   adapter->adapter_handle, 
		   (WORD) (adapter->io_location + PCMCIA_CONTROL_REGISTER_2)
		   );

    /*
     * Clear or set the bit.                                                
     */

    if (set_bit)
    {
	ctrl_reg |= mask;
    }
    else
    {
	ctrl_reg &= ~mask;
    }

    /*
     * Write the data to the SEEPROM control register. 
     */

    sys_outsb(
	adapter->adapter_handle, 
	(WORD) (adapter->io_location + PCMCIA_CONTROL_REGISTER_2),
	(BYTE) ctrl_reg);

    /*
     * Wait for a bit.                                                      
     */

    sys_wait_at_least_microseconds(10);                                     

    /*
     * And read the SEEPROM control register back so that the data gets     
     * latched properly.                                                    
     */

    sys_insb(
	adapter->adapter_handle, 
	(WORD) (adapter->io_location + PCMCIA_CONTROL_REGISTER_2));
}


/*---------------------------------------------------------------------------
|                                                                          
|                     pcmcia_c46_twitch_clock                              
|                     =======================                              
|                                                                          
| Toggle the SEEPROM clock.                                                
|
---------------------------------------------------------------------------*/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pcmcia_c46_twitch_clock)
#endif

local void
pcmcia_c46_twitch_clock(
    ADAPTER * adapter
    )
{
    pcmcia_c46_write_bit(adapter, PCMCIA_CTRL2_E2SK, TRUE);
    pcmcia_c46_write_bit(adapter, PCMCIA_CTRL2_E2SK, FALSE);
}


/*---------------------------------------------------------------------------
|                                                                          
|                     pcmcia_c46_read_data                                 
|                     ====================                                 
|                                                                          
| Read a data bit from the SEEPROM control register                        
|                                                                          
---------------------------------------------------------------------------*/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pcmcia_c46_read_data)
#endif

local WORD
pcmcia_c46_read_data(
    ADAPTER * adapter
    )
{
    return sys_insb(
	       adapter->adapter_handle,
	       (WORD) (adapter->io_location + PCMCIA_CONTROL_REGISTER_2)
	       ) & PCMCIA_CTRL2_E2DO;
}


/*---------------------------------------------------------------------------
|                                                                          
|                      hwi_pcmcia_read_eeprom_word                         
|                      ===========================                         
|                                                                          
| hwi_pcmcia_read_eeprom_word takes the address of the  word  to  be  read 
| from  the  AT93C46 serial EEPROM, write to the IO ports a magic sequence 
| to switch the EEPROM to reading mode and finally read the required  word 
| and return.                                                              
|                                                                          
---------------------------------------------------------------------------*/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_pcmcia_read_eeprom_word)
#endif

local WORD 
hwi_pcmcia_read_eeprom_word(
    ADAPTER * adapter,
    WORD      word_address
    )
{
    WORD      i; 
    WORD      cmd_word     = C46_START_BIT | C46_READ_CMD;
    WORD      tmp_word;

    /*
     * Concatenate the address to command word.
     */
    
    cmd_word |= (WORD)((word_address & C46_ADDR_MASK) << C46_ADDR_SHIFT);

    /*
     * Clear data in bit.
     */
    
    pcmcia_c46_write_bit(
	adapter,
	PCMCIA_CTRL2_E2DI,
	FALSE);
    
    /*
     * Assert chip select bit.
     */
    
    pcmcia_c46_write_bit(
	adapter,
	PCMCIA_CTRL2_E2CS,
	TRUE);
    
    /* 
     * Send read command and address.
     */
    
    pcmcia_c46_twitch_clock(
	adapter);

    tmp_word = cmd_word;

    for (i = 0; i < C46_CMD_LENGTH; i++)
    {
	pcmcia_c46_write_bit(
	    adapter, 
	    PCMCIA_CTRL2_E2DI, 
	    (WBOOLEAN) ((tmp_word & 0x8000) != 0));
	pcmcia_c46_twitch_clock(adapter);
	tmp_word <<= 1;
    }
    
    /*
     * Read data word.
     */
    
    tmp_word = 0x0000;

    for (i = 0; i < 16; i++)
    {
	pcmcia_c46_twitch_clock(adapter);

	if (i > 0) 
	{
	    tmp_word <<= 1;
	}

	if (pcmcia_c46_read_data(adapter) != 0)
	{
	    tmp_word |= 0x0001;
	}
    }

    /*
     * Clear data in bit.
     */
    
    pcmcia_c46_write_bit(
	adapter,
	PCMCIA_CTRL2_E2DI,
	FALSE);
    
    /* 
     * Deselect chip.
     */
    
    pcmcia_c46_write_bit(
	adapter,
	PCMCIA_CTRL2_E2CS,
	FALSE);
    
    /*
     * Tick clock.
     */
    
    pcmcia_c46_twitch_clock(adapter);

    return tmp_word;
}

#ifndef FTK_NO_PROBE
/*---------------------------------------------------------------------------
|                                                                          
|                      hwi_pcmcia_card_services_setup                      
|                      ==============================                      
|                                                                          
| hwi_pcmcia_card_services_setup  is called by hwi_pcmcia_install_card. It 
| handles all  the  procedures  required  to  register  with  PCMCIA  Card 
| Serivces and request I O and interrupt resources.                      
|                                                                          
| The  caller  must  fill in interrupt_number and io_location field of the 
| input adapter structure. Interrupt number can be any number supported by 
| the 16 4 PCMCIA ringnode or DETERMINED_BY_CS. A valid  io_location  mus
| be supplied.                                                             
|                                                                         
---------------------------------------------------------------------------*/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_pcmcia_card_services_setup)
#endif

local WBOOLEAN
hwi_pcmcia_card_services_setup(
    PROBE * resource
    )
{
    NEW_PCMCIA_ARG_BUFFER(fp);

    WORD       rc;
    WORD       ClientHandle;
    DWORD      i             = 0;
    WORD       j;
    void FAR * CallBackPtr   = (void FAR *) Callback;
    WORD       socket        = DETERMINED_BY_CARD_SERVICES;
    WORD       irq           = DETERMINED_BY_CARD_SERVICES;
    WORD       io_location   = resource->io_location;

    rc = CardServices(
	     CS_GetCardServicesInfo,        /* Function */
	     NULL,                          /* Handle not used */
	     NULL,                          /* Pointer not used */
	     MAX_PCMCIA_ARG_BUFFER_LEN,     /* argbuffer length */
	     fp );                          /* argbuffer */
    
    if (rc == CMD_SUCCESS)
    {
	CS_GET_CS_INFO_ARG FAR * ptr = (CS_GET_CS_INFO_ARG FAR *)fp;
    
	if ((ptr->Signature[0] == 'C') &&
	    (ptr->Signature[1] == 'S'))
	{
	    CardServicesVersion = ptr->CSLevel;
	}
	else if ((ptr->Signature[0] == 'M') &&
		 (ptr->Signature[1] == 'N'))
	{
	    CardServicesVersion = ptr->CSLevel;
	}
	else
	{
	    return FALSE;
	}
	 
    }
    else
    {
	return FALSE;
    }
	
    {
	CS_REGISTER_CLIENT_ARG FAR * ptr =
	   (CS_REGISTER_CLIENT_ARG FAR *)fp;
	
	ptr->Attributes    = RC_ATTR_IO_CLIENT_DEVICE_DRIVER |
			     RC_ATTR_IO_INSERTION_SHARABLE;
		
	ptr->EventMask     = MASK_CARD_DETECT;
		
	ptr->ClientData[0] = 0x00; /* Not used */
	ptr->ClientData[1] = 0x00; /* CardServices will put DS here. */
	ptr->ClientData[2] = 0x00; /* Not used */
	ptr->ClientData[3] = 0x00; /* Not used */
	ptr->Version       = 0x0201;
    }
   
    /*  
     * Set  RegisterClientCompleted  flag  to  FALSE.   When   registration 
     * complete event occur, call back function will set this flag to TRUE. 
     */

    RegisterClientCompleted = FALSE;
    
    rc = CardServices(
	     CS_RegisterClient,
	     (WORD FAR *)&ClientHandle,
	     (void * FAR *)&CallBackPtr,
	     MAX_PCMCIA_ARG_BUFFER_LEN,
	     fp );

    if (CMD_SUCCESS != rc)
    {
	return FALSE;
    }
     
    while (!RegisterClientCompleted)    
    {
	/* 
	 * Wait for card insertion event to complete,  timeout  if  waiting
	 * for too long.
	 */

	i++;

	if (i == PCMCIA_CS_REGISTRATION_TIMEOUT)
	{
	    return FALSE;
	}
    }

    /*
     * Check  if there is a least one socket ready for use. Return false if 
     * none is found. Remember we have registered with  card  services,  so 
     * deregister before returning.                                         
     */

    for (j = 0; j < MAX_PCMCIA_SOCKETS; j++)
    {
	if (SOCKET_READY == pcmcia_socket_table[j].status)
	{
	    break; 
	} 
    }
	    
    if (MAX_PCMCIA_SOCKETS == j)
    {
	/*
	 * No  socket available. Either no Madge card installed or they are 
	 * already in  use.  Report  error  here.  Must  deregister  before 
	 * returning.                                                       
	 */

	hwi_pcmcia_cs_deregister_client(ClientHandle);
		
	return FALSE;
    }

    if (socket != DETERMINED_BY_CARD_SERVICES)
    {
	/*
	 * User specified a socket number to use. Check if it is available. 
	 */

	if (pcmcia_socket_table[socket].status == SOCKET_NOT_INITIALIZED)
	{
	    /*
	     * No  Madge  Card found in PCMCIA socket specified, deregister 
	     * and report error.                                            
	     */

	    hwi_pcmcia_cs_deregister_client(ClientHandle);
	    
	    return FALSE;
	}
	else if (pcmcia_socket_table[socket].status == SOCKET_IN_USE)
	{
	    /*
	     * Card  in  socket  number  specified  is  currently  in  use, 
	     * deregister and report error.                                 
	     */

	    hwi_pcmcia_cs_deregister_client(ClientHandle);

	    return FALSE;
	}
	else
	{
	    /*
	     * Socket  number  specified  has  a Madge Card in it and it is 
	     * available.                                                   
	     */
	    ;
	}

    }
    else 
    {
	/*
	 * User asked Card Services to choose which socket to use.            
	 * Use the first available one.
	 */
	socket = j;
    }

    /*  
     * Call CS AdjustResourceInfo.
     */                                      

    {
	CS_ADJ_IO_RESOURCE_ARG FAR * ptr =
	    (CS_ADJ_IO_RESOURCE_ARG FAR *)fp;
	
	ptr->Action      = ARI_ACTION_ADD;
	ptr->Resource    = ARI_RESOURCE_IO;
	ptr->BasePort    = io_location;
	ptr->NumPorts    = PCMCIA_IO_RANGE;
	ptr->Attributes  = 0x00;
	ptr->IOAddrLines = PCMCIA_NUMBER_OF_ADDR_LINES;
    }
	
    rc = CardServices(CS_AdjustResourceInfo,
		      NULL,
		      NULL,
		      MAX_PCMCIA_ARG_BUFFER_LEN,
		      fp );

    if (CMD_SUCCESS != rc)
    {
	/*
	 * Requested IO location is not available,  deregister  and  report 
	 * error.
	 */                                                   
	
	hwi_pcmcia_cs_deregister_client(ClientHandle);

	return FALSE;
    }

    /*
     * AdjustResourceInfo does not exist for CS V2.00 on the IBM ThinkPad.  
     * Assume it is ok. Do not bother to check the return code here.        
     */
   
    /*
     * Call CS RequestIO to ask for IO resources.                            
     */

    {
	CS_REQUEST_IO_ARG FAR * ptr = (CS_REQUEST_IO_ARG FAR *) fp;
	
	ptr->Socket      = socket;
	ptr->BasePort1   = io_location;
	ptr->NumPorts1   = PCMCIA_IO_RANGE;
	ptr->Attributes1 = RIO_ATTR_16_BIT_DATA;
	ptr->BasePort2   = 0x0000;
	ptr->NumPorts2   = 0x00;
	ptr->Attributes2 = 0x00;
	ptr->IOAddrLines = PCMCIA_NUMBER_OF_ADDR_LINES;
    
    }
	
    rc = CardServices(CS_RequestIO,
		      (WORD FAR *)&ClientHandle,
		      NULL,
		      MAX_PCMCIA_ARG_BUFFER_LEN,
		      fp );
    

    if (CMD_SUCCESS != rc)
    {
	/*
	 * Requested IO location is not available,  deregister  and  report 
	 * error.
	 */                                                   
	
	hwi_pcmcia_cs_deregister_client(ClientHandle);

	return FALSE;
    }

    /*
     * Call CS RequestIRQ to ask for interrupt resources.
     */

    {
	CS_REQUEST_IRQ_ARG FAR * ptr = (CS_REQUEST_IRQ_ARG FAR *)fp; 
	
	if (DETERMINED_BY_CARD_SERVICES == irq)
	{
	    /*
	     * User  asked  card  services  to  choose  any interrupt channel 
	     * available.                                                    
	     */

	    ptr->IRQInfo1 = PCMCIA_IRQINFO1_NOT_SPECIFIED;
	    
	    /*
	     * List of IRQ channel available (in form of bit mask) for card 
	     * services to choose from. Exclude those already in use.       
	     */

	    ptr->IRQInfo2 = PCMCIA_AVAILABLE_IRQ_MASK & ~UsedIrqChannelsMask;
	}
	else if ((0x0001 << irq) & PCMCIA_AVAILABLE_IRQ_MASK)
	{
	    /*
	     * Use the interrupt channel the user supplied.
	     */
	    
	    ptr->IRQInfo1 = (BYTE)((irq & 0x00FF) |
				  PCMCIA_IRQINFO1_SPECIFIED);
	    ptr->IRQInfo2 = 0x0000;
	}
	else
	{
	    /* 
	     * An invalid interrupt channel is specified.                   
	     * We have already  requested  for  an  IO  resource,  we  must 
	     * release   it   and  deregister  with  card  services  before 
	     * returning.                                                   
	     */

	    hwi_pcmcia_cs_release_io(ClientHandle, socket, io_location);
	    hwi_pcmcia_cs_deregister_client(ClientHandle);
	    
	    return FALSE;
	}

	ptr->Socket     = socket;
	ptr->Attributes = 0x0000;
	
	rc = CardServices(
		 CS_RequestIRQ,
		 (WORD FAR *)&ClientHandle,
		 NULL,
		 MAX_PCMCIA_ARG_BUFFER_LEN,
		 fp );
	
	if (CMD_SUCCESS == rc)
	{
	    /*
	     * Card  Services  successfully  allocated  us   an   interrupt 
	     * channel, record it for later use.                            
	     */

	    irq = (WORD)ptr->AssignedIRQ;
	}
	else
	{   /*
	     * Failed  to  obtain  an  interrupt  channel.  Release  the IO 
	     * allocated and deregister with card services.                 
	     */

	    hwi_pcmcia_cs_release_io(ClientHandle, socket, io_location);
	    hwi_pcmcia_cs_deregister_client(ClientHandle);

	    return FALSE;
	}
    }

    /*
     * Call CS RequestConfiguration.
     */
		
    {
	CS_REQUEST_CONFIG_ARG FAR * ptr = (CS_REQUEST_CONFIG_ARG FAR *)fp;
	
	ptr->Socket      = socket;
	ptr->Attributes  = RC_ATTR_ENABLE_IRQ_STEERING;
	ptr->Vcc         = PCMCIA_VCC;
	ptr->Vpp1        = PCMCIA_VPP1;
	ptr->Vpp2        = PCMCIA_VPP2;
	ptr->IntType     = RC_INTTYPE_MEMORY_AND_IO;
	ptr->ConfigBase  = PCMCIA_CONFIG_BASE;
	ptr->Status      = PCMCIA_STATUS_REG_SETTING;
	ptr->Pin         = PCMCIA_PIN_REG_SETTING;
	ptr->Copy        = PCMCIA_COPY_REG_SETTING;
	ptr->ConfigIndex = PCMCIA_OPTION_REG_SETTING;
	ptr->Present     = PCMCIA_REGISTER_PRESENT;
	
    }
    
    rc = CardServices(
	    CS_RequestConfiguration,
	    (WORD FAR *)&ClientHandle,
	    NULL,
	    MAX_PCMCIA_ARG_BUFFER_LEN,
	    fp );
	    
    if (CMD_SUCCESS != rc)
    {
	/*
	 * RequestConfiguration failed. Release all  resources  and  return 
	 * error.
	 */

	hwi_pcmcia_cs_release_io(ClientHandle, socket, io_location);
	hwi_pcmcia_cs_release_irq(ClientHandle, socket, irq);
	hwi_pcmcia_cs_deregister_client(ClientHandle);
	
	return FALSE;
	
    }

    /*
     * We successfully made all the card services call.  Update  our  local 
     * sockets record here.                                                 
     */
	    
    pcmcia_socket_table[socket].status         = SOCKET_IN_USE;
    pcmcia_socket_table[socket].irq_number     = irq;
    pcmcia_socket_table[socket].io_location    = io_location;
    pcmcia_socket_table[socket].ClientHandle   = ClientHandle;

    /*
     * Record interrupt channel used.
     */
    
    UsedIrqChannelsMask |= ( 0x0001 << irq );

    /*
     * Fill adapter sturcture with resources allocated.
     */
	
    resource->interrupt_number = irq;
    resource->io_location      = io_location;
    resource->socket           = socket;
    
    /*
     * Toggle the top bit of Configuration Option Register to reset card.
     */

    /*
     * Note that this is not necessary for later cards.                     
     * CS 2.00 may not have this function, so just do it but  don't  bother 
     * to check return code.                                                
     */
		
    {
	CS_ACCESS_CONFIG_REG_ARG FAR * ptr =
	    (CS_ACCESS_CONFIG_REG_ARG FAR *) fp;
					
	ptr->Socket = socket;
	ptr->Action = ACR_ACTION_WRITE;
	ptr->Offset = PCMCIA_OPTION_REG;
	ptr->Value  = PCMCIA_COR_SYSRESET;

	CardServices(
	    CS_AccessConfigurationRegister,
	    NULL,
	    NULL,
	    MAX_PCMCIA_ARG_BUFFER_LEN,
	    fp );
	
	ptr->Value  = 0x00;

	CardServices(
	    CS_AccessConfigurationRegister,
	    NULL,
	    NULL,
	    MAX_PCMCIA_ARG_BUFFER_LEN,
	    fp );
	
	ptr->Value = PCMCIA_OPTION_REG_SETTING;

	CardServices(
	    CS_AccessConfigurationRegister,
	    NULL,
	    NULL,
	    MAX_PCMCIA_ARG_BUFFER_LEN,
	    fp );
    }

    return TRUE;
}


/*---------------------------------------------------------------------------
|                                                                          
|                      hwi_pcmcia_cs_release_config                        
|                      ============================                        
|                                                                          
| hwi_pcmcia_cs_release_config releases resources requested  earlier  from 
| Card Services. This is part of the shut down procedure.                  
|                                                                          
---------------------------------------------------------------------------*/

#ifdef FTK_RES_FUNCTION
#pragma FTK_RES_FUNCTION(hwi_pcmcia_cs_release_config)
#endif

local WORD
hwi_pcmcia_cs_release_config( 
    WORD                         ClientHandle,
    WORD                         Socket)
{
    NEW_PCMCIA_ARG_BUFFER(fp);
    WORD                         chandle = ClientHandle;
    CS_RELEASE_CONFIG_ARG FAR * ptr     = (CS_RELEASE_CONFIG_ARG FAR *)fp;

    ptr->Socket = Socket;
	
    return CardServices(
	       CS_ReleaseConfiguration,
	       &chandle,
	       NULL,
	       MAX_PCMCIA_ARG_BUFFER_LEN,
	       fp);
}


/*---------------------------------------------------------------------------
|                                                                          
|                      hwi_pcmcia_cs_release_io                            
|                      ========================                            
|                                                                          
| hwi_pcmcia_cs_release_io releases  the  IO  resources requested earlier
| from Card Services.                                                      
|                                                                          
---------------------------------------------------------------------------*/

#ifdef FTK_RES_FUNCTION
#pragma FTK_RES_FUNCTION(hwi_pcmcia_cs_release_io)
#endif

local WORD
hwi_pcmcia_cs_release_io(
    WORD                     ClientHandle,
    WORD                     Socket,
    WORD                     IoLocation
    )
{
    NEW_PCMCIA_ARG_BUFFER(fp);
    WORD                     chandle = ClientHandle;
    CS_RELEASE_IO_ARG FAR * ptr     = (CS_RELEASE_IO_ARG FAR *)fp;
    
    
    ptr->Socket      = Socket;
    ptr->BasePort1   = IoLocation;
    ptr->NumPorts1   = PCMCIA_IO_RANGE;
    ptr->Attributes1 = RIO_ATTR_16_BIT_DATA;
    ptr->BasePort2   = 0x0000;
    ptr->NumPorts2   = 0x00;
    ptr->Attributes2 = 0x00;
    ptr->IOAddrLines = PCMCIA_NUMBER_OF_ADDR_LINES;
    
    return CardServices(
	       CS_ReleaseIO,
	       (WORD FAR *)&chandle,
	       NULL,
	       MAX_PCMCIA_ARG_BUFFER_LEN,
	       fp);
}


/*---------------------------------------------------------------------------
|                                                                          
|                      hwi_pcmcia_cs_release_irq                           
|                      =========================                           
|                                                                          
| hwi_pcmcia_cs_release_irq  release  the  IRQ resources requested earlier 
| from Card Services.                                                      
|                                                                          
---------------------------------------------------------------------------*/

#ifdef FTK_RES_FUNCTION
#pragma FTK_RES_FUNCTION(hwi_pcmcia_cs_release_irq)
#endif

local WORD
hwi_pcmcia_cs_release_irq(
    WORD                      ClientHandle,
    WORD                      Socket,
    WORD                      IRQChannel)
{
    NEW_PCMCIA_ARG_BUFFER(fp);
    WORD                      chandle = ClientHandle;
    CS_RELEASE_IRQ_ARG FAR * ptr     = (CS_RELEASE_IRQ_ARG FAR *)fp;

    
    ptr->Socket      = Socket;
    ptr->Attributes  = 0x0000;
    ptr->AssignedIRQ = (BYTE)IRQChannel;
	
    return CardServices(
	       CS_ReleaseIRQ,
	       (WORD FAR *)&chandle,
	       NULL,
	       MAX_PCMCIA_ARG_BUFFER_LEN,
	       fp);
}

/*---------------------------------------------------------------------------
|                                                                          
|                      hwi_pcmcia_cs_deregister_client                     
|                      ===============================                     
|                                                                          
| hwi_pcmcia_cs_deregister_client informs PCMCIA card services that we are 
| not  longer  interest in any PCMCIA event and/or resources. It is called 
| in shut  down  or  failure  in  invoking  other  card  services  related 
| function.                                                                
|                                                                          
---------------------------------------------------------------------------*/

#ifdef FTK_RES_FUNCTION
#pragma FTK_RES_FUNCTION(hwi_pcmcia_cs_deregister_client)
#endif

local WORD
hwi_pcmcia_cs_deregister_client(
    WORD ClientHandle
    )
{
    NEW_PCMCIA_ARG_BUFFER(fp);
    WORD chandle = ClientHandle;

    return CardServices(
	       CS_DeregisterClient,
	       (WORD FAR *)&chandle,
	       NULL,
	       0,
	       fp);
}

    
/*---------------------------------------------------------------------------
|                                                                          
|                      hwi_pcmcia_tear_down_cs                             
|                      =======================                             
|                                                                          
| hwi_pcmcia_tear_down_cs is called by hwi_pcmcia_remove card  to  release 
| all  the resources allocated by PCMCIA card services and deregister with 
| card services                                                            
|                                                                          
---------------------------------------------------------------------------*/

#ifdef FTK_RES_FUNCTION
#pragma FTK_RES_FUNCTION(hwi_pcmcia_tear_down_cs)
#endif

local WBOOLEAN
hwi_pcmcia_tear_down_cs(
    PROBE    resource
    )       
{

    WORD     socket        = resource.socket;
    WORD     ClientHandle;
    WORD     io_location;
    WORD     irq_number;
    WBOOLEAN rc1;
    WBOOLEAN rc2;
    WBOOLEAN rc3;
    WBOOLEAN rc4;
    
    
    if (pcmcia_socket_table[socket].status != SOCKET_IN_USE)
    {
	return FALSE;
    }

    /*
     * Socket number found. retrieve clienthandle, irq, iolocation from our 
     * local socket record.
     */
	
    ClientHandle = pcmcia_socket_table[socket].ClientHandle;
    io_location  = pcmcia_socket_table[socket].io_location;
    irq_number   = pcmcia_socket_table[socket].irq_number;

    /*
     * Call the PCMCIA card services routine to bring it down.
     */
	
    rc1 = hwi_pcmcia_cs_release_config(ClientHandle, socket);
    rc2 = hwi_pcmcia_cs_release_io(ClientHandle, socket, io_location);
    rc3 = hwi_pcmcia_cs_release_irq(ClientHandle, socket, irq_number);
    rc4 = hwi_pcmcia_cs_deregister_client(ClientHandle);

    /*
     * Update local record.
     */
      
    pcmcia_socket_table[socket].ClientHandle   = 0x0000;
    pcmcia_socket_table[socket].io_location    = 0x0000;
    pcmcia_socket_table[socket].irq_number     = 0x0000;
    pcmcia_socket_table[socket].status         = SOCKET_NOT_INITIALIZED;

    return (rc1 && rc2 && rc3 && rc4); 
}

    
/*---------------------------------------------------------------------------
|                                                                          
| This is the callback function used by Card Services to notify client any 
| event changes. Its prototype is defined in sys_cs.h module.              
|                                                                          
---------------------------------------------------------------------------*/

#ifdef FTK_RES_FUNCTION
#pragma FTK_RES_FUNCTION(Callback)
#endif

WORD FAR
Callback(
    WORD Function,
    WORD Socket,
    WORD Info,
    void FAR * MTDRequest,
    void FAR * Buffer,
    WORD Misc,
    WORD ClientData1,
    WORD ClientData2,
    WORD ClientData3
    )
{
    NEW_PCMCIA_ARG_BUFFER(fp);
    WORD rc;

    switch (Function)
    {
	case REGISTRATION_COMPLETE :
	    RegisterClientCompleted = TRUE;
	    break;
	    
	case CARD_INSERTION :
	    /*
	     * Call GetFistTuple of card services.
	     */
		
	    {
		CS_GET_FIRST_TUPLE_ARG FAR * ptr = 
		    (CS_GET_FIRST_TUPLE_ARG FAR *)fp;
					
		ptr->Socket       = Socket;
		ptr->Attributes   = 0x0000;
		ptr->DesiredTuple = CISTPL_VERS_1;
		ptr->Reserved     = 0x00;
	    }

	    rc = CardServices(
		     CS_GetFirstTuple,
		     NULL,
		     NULL,
		     MAX_PCMCIA_ARG_BUFFER_LEN,
		     fp);
	    
	    if (rc != CMD_SUCCESS)
	    {
		break;
	    }

	    /* 
	     * Call GetTupleData of card services.
	     */
	    
	    {
		CS_GET_TUPLE_DATA_ARG FAR * ptr = 
		    (CS_GET_TUPLE_DATA_ARG FAR *)fp;
					    
		ptr->TupleOffset  = 0x00;
		ptr->TupleDataMax = MAX_PCMCIA_ARG_BUFFER_LEN;
	    }
		
	    rc = CardServices(
		     CS_GetTupleData,
		     NULL,
		     NULL,
		     MAX_PCMCIA_ARG_BUFFER_LEN,
		     fp);

	    if (rc != CMD_SUCCESS)
	    {
		break;
	    }
		
	    /*
	     * Is it a Madge Smart 16/4 PCMCIA Ringnode?
	     */
		
	    {
		CS_GET_TUPLE_DATA_ARG FAR * ptr = 
		    (CS_GET_TUPLE_DATA_ARG FAR *)fp;
		BYTE FAR *                  info_ptr;
		WORD                         i;
		
		/*
		 * Find the signature strings in the tuple.                 
		 * Allow for CS version 2.00 or below.  See  the  TupleData 
		 * union in the header file.                                
		 */

		if (CardServicesVersion <= 0x0200)
		{
		    info_ptr = ((CS200_CISTPL_VERS_1_DATA FAR *)
				   (ptr->TupleData) )->info; 
		}
		else
		{
		    info_ptr = ((CS201_CISTPL_VERS_1_DATA FAR *)
				   (ptr->TupleData) )->info; 
		}
		
		/*
		 * Compare signature strings. Avoid strcmp, _fstrcmp, 
		 * memcmp, _fmemcmp, etc. to make it model independent.     
		 */

		for (i = 0; i < MADGE_TPLLV1_INFO_LEN; i++)
		{
		    if (*(MADGE_TPLLV1_INFO+i) != *(info_ptr+i))
		    {
			break; 
		    }
		}

			
		if (MADGE_TPLLV1_INFO_LEN == i) /* yes, a madge card. */
		{
		    if (pcmcia_socket_table[Socket].status ==
			    SOCKET_NOT_INITIALIZED)
		    {
			pcmcia_socket_table[Socket].status = SOCKET_READY;
		    }
		    
		    /*
		     * do nothing if status is SOCKET_READY or
		     * SOCKET_IN_USE.
		     */
		}           
	    }
		
	    break;
	    
	case CLIENT_INFO :
	    {
		WORD        buffer_size     = 
		    ((CS_CLIENT_INFO FAR *)Buffer)->MaxLen;
		WORD        len_to_copy;
		WORD        i;
		WORD        madge_info_size = sizeof(MADGE_CLIENT_INFO);
		BYTE FAR * src;
		BYTE FAR * dest;

		/*
		 * Copy  whole  client  info  structure  if there is space,
		 * otherwise just fill the buffer supplied.
		 */
		
		len_to_copy = 
		    (buffer_size > madge_info_size) ? madge_info_size :
						      buffer_size;
		
		src  = (BYTE FAR *)&default_client_info;
		dest = (BYTE FAR *)Buffer;
					    
		for (i = 0; i < len_to_copy; i++)
		{
		    if (i > 1) /* Skip MaxLen field which is preserved. */
		    {
			*(dest+i) = *(src+1);
		    } 
		}
	    }
	    
	    break;
    
	case CARD_REMOVAL:
	    /*
	     * Only remove card in use.                                     
	     * Check  Socket  <  MAX_PCMCIA_SOCKETS   to   prevent   access 
	     * to out-of-bound array element.                               
	     */
	    
	    if ((Socket < MAX_PCMCIA_SOCKETS) &&
		(pcmcia_socket_table[Socket].status == SOCKET_IN_USE))
	    {
		WORD ClientHandle = pcmcia_socket_table[Socket].ClientHandle;
		WORD io_location  = pcmcia_socket_table[Socket].io_location;
		WORD irq_number   = pcmcia_socket_table[Socket].irq_number;

		/*
		 * Call the PCMCIA card services routine to bring it down.
		 */
	
		hwi_pcmcia_cs_release_config(ClientHandle, Socket);
		hwi_pcmcia_cs_release_io(ClientHandle, Socket, io_location);
		hwi_pcmcia_cs_release_irq(ClientHandle, Socket, irq_number);
		hwi_pcmcia_cs_deregister_client(ClientHandle);

		/*
		 * Update local record.
		 */
      
		pcmcia_socket_table[Socket].ClientHandle   = 0x0000;
		pcmcia_socket_table[Socket].io_location    = 0x0000;
		pcmcia_socket_table[Socket].irq_number     = 0x0000;
		pcmcia_socket_table[Socket].status         = 
		    SOCKET_NOT_INITIALIZED;
		pcmcia_socket_table[Socket].adapter_handle = 0x0000;
	    }

	    break;

	default :
	    
	    break;
    }
    
    return CMD_SUCCESS;
}

#endif

#endif

/******** End of HWI_PCMC.C ************************************************/