summaryrefslogblamecommitdiffstats
path: root/private/ntos/ndis/madge/driver/hwi_pnp.c
blob: 29e9821dbd08d400356086f0952c08f2e8d6cfac (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

























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                    
/****************************************************************************
*                                                                          
*      THE HARDWARE INTERFACE MODULE (SMART PNP RINGNODES)                 
*      ===================================================                 
*                                                                          
*      HWI_PNP.C : Part of the FASTMAC TOOL-KIT (FTK)                      
*                                                                          
*      Copyright (c) Madge Networks Ltd. 1990-1994                         
*      Developed by AC                                                     
*      From code by MF                                                     
*      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_PNP.C module contains the routines specific to the PnP card      
* 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_PNP

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

local WBOOLEAN
hwi_pnp_valid_io_location(
    WORD io_location
    );

local WBOOLEAN
hwi_pnp_read_node_address(
    ADAPTER * adapter
    );

local WBOOLEAN
hwi_pnp_valid_irq_channel(
    ADAPTER * adapter
    );

local WBOOLEAN
hwi_pnp_test_for_id(
    ADAPTER * adapter
    );

local WORD
pnp_read_a_word(
    ADAPTER * adapter,
    WORD      index
    );

#ifndef FTK_NO_PROBE

local WBOOLEAN
hwi_pnp_probe_find_card(
    WORD io_location
    );

local WORD
hwi_pnp_probe_get_irq(
    WORD io_location
    );

local WORD
pnp_probe_read_a_word(
    WORD  io_location,
    WORD index
    );

#endif

#ifndef FTK_NO_PROBE
/****************************************************************************
*
*                        hwi_pnp_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 PNP based adapters this should be a subset of
* {0x3a20, 0x920, 0x940, 0x960, 0x980, 0xa20, 0xa40, 0xa60, 0xa80, 0xb20,
* 0xb40, 0xb60, 0xb80}.
*
* UINT    number_locations
*
* This is the number of IO locations in the above list.
*
* BODY :
* ======
* 
* The  hwi_pnp_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 PROBE_FAILURE if
* there's a problem.
*
****************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_pnp_probe_card)
#endif

export UINT 
hwi_pnp_probe_card(
    PROBE * resources,
    UINT    length,
    WORD  * valid_locations,
    UINT    number_locations
    )
{
    WORD    temp_word;
    UINT    adapters_found;
    UINT    i;

    /*
     * Sanity check the bounds.
     */

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

    /*
     * Validate the IO locations.
     */

    for (i = 0; i < number_locations; i++)
    {
        if (!hwi_pnp_valid_io_location(valid_locations[i]))
        {
           return PROBE_FAILURE;
        }
    }

    adapters_found = 0;

    for (i = 0; i < number_locations; i++)
    {
        /*
         * Make sure that we haven't run out of PROBE structures.
         */
        if (adapters_found >= length)
        {
           return adapters_found;
        }

        if (hwi_pnp_probe_find_card(valid_locations[i]))
        {

            /*
             * Found a card! Now fill out the probe structure.
             */

            resources[adapters_found].io_location            = valid_locations[i];
            resources[adapters_found].adapter_card_bus_type  = ADAPTER_CARD_PNP_BUS_TYPE;
            resources[adapters_found].adapter_card_type      = ADAPTER_CARD_TYPE_16_4_PNP;
            resources[adapters_found].adapter_card_revision  = ADAPTER_CARD_PNP;
            resources[adapters_found].transfer_mode          = PIO_DATA_TRANSFER_MODE;

            /*
             *   Now find out how much RAM we have.
             */

            temp_word = pnp_probe_read_a_word( 
                            valid_locations[i],
                            PNP_HWARE_FEATURES1);

            temp_word &= PNP_DRAM_SIZE_MASK;

            /*
             *   Convert the DRAM size to multiples of bytes.
             */

            resources[adapters_found].adapter_ram_size = temp_word * 64;

            /*
             *   Now find out what IRQ we are using.
             */

            resources[adapters_found].interrupt_number = hwi_pnp_probe_get_irq( valid_locations[i]);

            adapters_found++;
        }
    }

    return adapters_found;

}
#endif

/****************************************************************************
*                                                                          
*                      hwi_pnp_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_pnp_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 there is a valid adapter at the required IO address. If so it 
* reads the node address from the BIA PROM and sets up and checks numerous 
* on-board registers for correct operation.                                
*                                                                          
* 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. 
* Similarly, operating system calls are used to enable DMA if required. If 
* DMA is not used then the adapter is set up for PIO.                      
*                                                                          
*                                                                          
* 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.                                                             
*                                                                          
****************************************************************************/        

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_pnp_install_card)
#endif

export WBOOLEAN
hwi_pnp_install_card(
    ADAPTER *        adapter,
    DOWNLOAD_IMAGE * download_image
    )
{
    ADAPTER_HANDLE adapter_handle = adapter->adapter_handle;
    WORD           control_1      = adapter->io_location +
                                        PNP_CONTROL_REGISTER_1;
    WORD           config_addr    = adapter->io_location +
                                        PNP_CONFIG_ADDRESS_REGISTER;
    WORD           config_data    = adapter->io_location +
                                        PNP_CONFIG_DATA_REGISTER;
    WORD           chip_type;
    WORD           ram_size;
    BYTE           byte;
    WORD           sif_base;


    /*
     *   Firstly do some validation on the user supplied adapter details      
     *   check that the IO location is valid                                  
     *   if routine fails return failure (error record already filled in)     
     *   In theory we shouldnt have to do this - but Chicago has been known   
     *   to pass out junk !!
     */

    if (!hwi_pnp_valid_io_location(adapter->io_location))
    {
       
    	adapter->error_record.type = ERROR_TYPE_HWI;
    	adapter->error_record.value = HWI_E_02_BAD_IO_LOCATION;
    	return FALSE;
    }

    /*
     *   save IO locations of the SIF registers
     */

    sif_base = adapter->io_location + PNP_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 + EAGLE_SIFACL;
    adapter->sif_adr2    = sif_base + EAGLE_SIFADR;
    adapter->sif_adx     = sif_base + EAGLE_SIFADX;
    adapter->sif_dmalen  = sif_base + EAGLE_DMALEN;
    adapter->sif_sdmadat = sif_base + EAGLE_SDMADAT;
    adapter->sif_sdmaadr = sif_base + EAGLE_SDMAADR;
    adapter->sif_sdmaadx = sif_base + EAGLE_SDMAADX;

    adapter->io_range    = PNP_IO_RANGE;

    /*
     *    Make sure this is a real PNP card by reading the ID register 
     */

    if(!hwi_pnp_test_for_id(adapter))
    {
    	adapter->error_record.type = ERROR_TYPE_HWI;
    	adapter->error_record.value = HWI_E_05_ADAPTER_NOT_FOUND;
	return FALSE;
    }

    /*
     * You might want to check that we have not already checked for a card  
     * at this address (or its rev3/4 equivalent).
     * Read the node address for the specified IO location.                 
     */

    if (!hwi_pnp_read_node_address(adapter))
    {
    	adapter->error_record.type = ERROR_TYPE_HWI;
    	adapter->error_record.value = HWI_E_05_ADAPTER_NOT_FOUND;
	return FALSE;
    }

    /*
     * Check the transfer mode - this is very easy as we only do PIO 
     * (albeit EAGLE PseudoDMA).
     */

    if (adapter->transfer_mode != PIO_DATA_TRANSFER_MODE)
    {
        return FALSE;
    }

    /*
     *   Check the IRQ channel supplied.                                      
     *   In theory we shouldnt have to do this - but Chicago has been known  
     *   to pass out junk !!
     */

    if (!hwi_pnp_valid_irq_channel(adapter))
    {
        /*
         *   Fill in error record and return
         */
    	adapter->error_record.type = ERROR_TYPE_HWI;
    	adapter->error_record.value = HWI_E_03_BAD_INTERRUPT_NUMBER;
	return FALSE;
    }

    /*
     * These things can all be assumed for the pnp.
     */
  
    adapter->adapter_card_type     = ADAPTER_CARD_TYPE_16_4_PNP;
    adapter->adapter_card_revision = ADAPTER_CARD_PNP;

    /*
    *   Now find out how much RAM we have.
    */

    ram_size = pnp_read_a_word(adapter,
                               PNP_HWARE_FEATURES1);

    ram_size &= PNP_DRAM_SIZE_MASK;

    /*
    *   Convert the DRAM size to multiples of bytes.
    */

    adapter->adapter_ram_size      = ram_size * 64;
    adapter->edge_triggered_ints   = TRUE;
    adapter->EaglePsDMA            = TRUE;

    /*
     * Set the ring speed if required.
     */

    /*
     * RCS 2/11/94 Added code to set the adapter->nselout bits so that
     * the ring speed is fully adopted by the adapter when "hwi_halt_eagle()"
     * is called.
     * 
     * Also set the "ringspeed configured" bit if the "set_ring_speed"
     * feature is specified so that we will use the value.
     * 
     * Also return an error if the ringspeed hasnt been configured and
     * the user hasnt specified "set_ring_speed".
     */

    /*
     *  Is this a C30 based card???
     *  If it is then we can't drive the lowest bit of the vendor config
     *  register as this gives an indication of ring_speed error (Potentially).
     *
     */

    chip_type = pnp_read_a_word(
                    adapter,
                    PNP_HWARE_FEATURES3);
    chip_type &= PNP_C30_MASK;

    /*
     *   First read the current settings
     */

    sys_outsb(
        adapter_handle,
        config_addr,
        PNP_VENDOR_CONFIG_BYTE);

    byte = sys_insb(
               adapter_handle,
               config_data);

    if (adapter->set_ring_speed != 0)
    {
        if (adapter->set_ring_speed == 4)
        {
            if ( chip_type != PNP_C30 )
            {
                /*
                 *   Set bits to select 4mbits as the ring speed
                 */
                byte |= (PNP_VENDOR_CONFIG_4MBITS + PNP_VENDOR_CONFIG_PXTAL);
            }
            else
            {
                byte |= PNP_VENDOR_CONFIG_4MBITS;
            }
        }
        else if (adapter->set_ring_speed == 16)
        {
            if ( chip_type != PNP_C30 )
            {
                /*
                 *   Clear bits to select 16mbits as the ring speed
                 */

                byte &= ~(PNP_VENDOR_CONFIG_4MBITS +
                          PNP_VENDOR_CONFIG_PXTAL);
            }
            else
            {
                byte &= ~(PNP_VENDOR_CONFIG_4MBITS);
            }
        }

        /*
         *   Show ring speed as having been configured
         */

        byte |= PNP_VENDOR_CONFIG_RSVALID;

        /*
         *   and write it back to the card
         */

        sys_outsb(
            adapter_handle,
            config_data,
            byte);
    }

    /*
     * Use the value in "byte" to set the NSELOUT bits or it will still
     * only run at 16 !!
     */

    if (byte & PNP_VENDOR_CONFIG_RSVALID)
    {
        if (byte & PNP_VENDOR_CONFIG_4MBITS)
        {
            adapter->nselout_bits = PNP_RING_SPEED_4;
        }
        else
        {
            adapter->nselout_bits = PNP_RING_SPEED_16;
        }
    }
    else
    {
        /*
         * The user MUST configure the RING SPEED before we will let him use
         * the card.
         * He can either run the CONFIG util or use the FORCE4/FORCE16
         * mechanism.
         * Fill in error record and return
         */

    	adapter->error_record.type = ERROR_TYPE_HWI;
    	adapter->error_record.value = HWI_E_0E_NO_SPEED_SELECTED;

	return FALSE;
    }

    /*
     * Bring adapter out of reset state (ensure that SCS is zero before
     * doing this). If active float channel ready is set in the Plug and
     * Play hardware flags then set the PNP_CHRDY_ACTIVE bit.
     */

    byte = PNP_CTRL1_NSRESET;

    if ((pnp_read_a_word(
             adapter, 
             PNP_HWARE_PNP_FLAGS) & PNP_ACTIVE_FLOAT_CHRDY) != 0)
    {
        byte |= PNP_CTRL1_CHRDY_ACTIVE;
    }

    sys_outsb(
        adapter_handle,
        control_1,
        byte);

    /*
     *   Halt the Eagle prior to downloading the MAC code - this will also    
     *   write the ringspeed bits into the SIFACL register.
     */

    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 ATULA cards       
     *   if routine fails return failure (error record already filled in)
     */

    if (!hwi_download_code(
             adapter,
	     (DOWNLOAD_RECORD *) download_image,
	     hwi_pnp_set_dio_address))
    {
    	return FALSE;
    }

    /*
     *   Restart the Eagle to initiate bring up diagnostics.
     */

    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))
    {
        return FALSE;
    }

    /*
     *   set DIO address to point to EAGLE DATA page 0x10000L
     */

    hwi_pnp_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_handle,
                                   adapter->interrupt_number);

        /*
         *   if fail enable irq channel then fill in error record and return
         */
	if (!adapter->interrupts_on)
        {
	    adapter->error_record.type = ERROR_TYPE_HWI;
	    adapter->error_record.value = HWI_E_0B_FAIL_IRQ_ENABLE;
	    return FALSE;
	}
    }
    else
    {
    	adapter->interrupts_on = TRUE;
    }

    /*
     *   return successfully
     */

    return TRUE;
}


/****************************************************************************
*                                                                          
*                      hwi_pnp_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_pnp_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 by the 
* ATULA in order to do 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 
* - it is the ATULA which allows us to do it via in/out instructions. Also 
* note that the IO location for the PIO is mapped onto the location of the 
* EAGLE SIFDAT register -  the  PIO  does  not  actually  use  the  SIFDAT 
* register so it's value is not effected by this routine.                  
*                                                                          
*                                                                          
* RETURNS :                                                                
* =========                                                                
*                                                                          
* The routine always successfully completes.                               
*                                                                          
****************************************************************************/             

#ifdef FTK_IRQ_FUNCTION
#pragma FTK_IRQ_FUNCTION(hwi_pnp_interrupt_handler)
#endif

export void
hwi_pnp_interrupt_handler(
    ADAPTER *      adapter
    )
{
    WORD           sifacl;
    WORD           sifint_value;
    WORD           sifint_tmp;
    WBOOLEAN       sifint_occurred = FALSE;
    WBOOLEAN       pioint_occurred = FALSE;
    BYTE FAR *     pio_virtaddr;
    WORD           lo_word;
    DWORD          hi_word;
    WORD           pio_len_bytes;
    WBOOLEAN       pio_from_adapter;
    ADAPTER_HANDLE adapter_handle;

    adapter_handle = adapter->adapter_handle;

    /*
     *   inform system about the IO ports we are going to access              
     *   eanble maximum number of IO locations used by any adapter card       
     *   do this so at driver level can disable IO not knowing adapter type
     */

#ifndef FTK_NO_IO_ENABLE
    macro_enable_io(adapter);
#endif
   
    /*
     *   check for SIF interrupt or PIO interrupt
     */

    /*
     * Mask off any further interrupts while we read SIFINT (note that this 
     * does not mask off Pseudo DMA interrupts)
     */

    macro_clearw_bit(
        adapter_handle,
        adapter->sif_acl,
        EAGLE_SIFACL_SINTEN
        );

    sifint_value = sys_insw(adapter_handle, adapter->sif_int);
    do
    {
        sifint_tmp = sifint_value;
        sifint_value = sys_insw(adapter_handle, adapter->sif_int);
    }
    while (sifint_tmp != sifint_value);

    if ((sifint_value & EAGLE_SIFINT_SYSTEM) != 0)
    {
	/*
         *   SIF interrupt has occurred
         *   SRB free, adapter check or received frame interrupt
         */

        sifint_occurred = TRUE;

	/*
         *   clear EAGLE_SIFINT_HOST_IRQ to acknowledge interrupt at SIF
         */

	sys_outsw(adapter_handle, adapter->sif_int, 0);
    }

    sifacl = sys_insw(adapter_handle, adapter->sif_acl);

    if ((sifacl & EAGLE_SIFACL_SWHRQ) != 0)
    {
    	/*
         *   PIO interrupt has occurred                                       
	 *   data transfer to/from adapter interrupt
         */

        pioint_occurred = TRUE;

        macro_setw_bit(
            adapter_handle, 
            adapter->sif_acl,
            EAGLE_SIFACL_SWHLDA
            );

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

	pio_from_adapter = sys_insw(
                               adapter_handle,
                               adapter->sif_acl
                                ) & EAGLE_SIFACL_SWDDIR;

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

        lo_word          = sys_insw(
                               adapter_handle,
                               adapter->sif_sdmaadr
                               );

        hi_word          =  (DWORD) sys_insw(
                                        adapter_handle,
                                        adapter->sif_sdmaadx
                                        );

        pio_virtaddr     = (BYTE FAR *) ((hi_word << 16) | ((DWORD) lo_word));

        /*
         *   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
             */

            /*
             * transfer whole WORDs to Fastmac receive buffer
	     * NOT FORGETTING the possibility of a trailing byte.
             */

	    sys_rep_insw(
                adapter_handle,
                adapter->sif_sdmadat,
                pio_virtaddr,
                (WORD) (pio_len_bytes >> 1)
                );

    	    if (pio_len_bytes % 2)
	    {
                /*
	         * Finally transfer any trailing byte.
                 */

		*(pio_virtaddr + pio_len_bytes - 1) =
                    sys_insb(adapter_handle, adapter->sif_sdmadat);
    	    }
	}
	else
	{
	    /*
             *   transfer into adapter memory from the host
             */

            sys_rep_outsw(
                adapter_handle,
                adapter->sif_sdmadat,
                pio_virtaddr,
                (WORD) (pio_len_bytes >> 1)
                );

	    if (pio_len_bytes % 2)
            {
    	        sys_outsb(
                    adapter_handle,
                    adapter->sif_sdmadat,
                    *(pio_virtaddr+pio_len_bytes-1)
                    );     
            }
        }
    }

#ifndef FTK_NO_CLEAR_IRQ

    if (sifint_occurred || pioint_occurred)
    {
	/*
         *   acknowledge/clear interrupt at interrupt controller
         */

	sys_clear_controller_interrupt(
            adapter_handle,
            adapter->interrupt_number);
    }

#endif

    if (sifint_occurred)
    {
        /*
         *   call driver with details of SIF interrupt
         */

    	driver_interrupt_entry(adapter_handle, adapter, sifint_value);
    }

    /*
     * Read SIFACL until the SWHLDA bit has cleared.
     */

    do
    {
        sifacl = sys_insw(adapter_handle, adapter->sif_acl);
    }
    while ((sifacl & EAGLE_SIFACL_SWHLDA) != 0);

    /*
     * Now set SINTEN in SIFACL to regenerate interrupts.
     */

    sys_outsw(
        adapter_handle, 
        adapter->sif_acl, 
        (WORD) (sifacl | EAGLE_SIFACL_SINTEN)
        );

    /*
     *   let system know we have finished accessing the IO ports
     */

#ifndef FTK_NO_IO_ENABLE
    macro_disable_io( adapter );
#endif
}


/****************************************************************************
*                                                                          
*                      hwi_pnp_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_pnp_remove_card routine is called  by  hwi_remove_adapter.  It  
* disables interrupts if they are being used. It also resets the adapter.  
*                                                                          
*                                                                          
* RETURNS :                                                                
* =========                                                                
*                                                                          
* The routine always successfully completes.                               
*                                                                          
****************************************************************************/      

#ifdef FTK_RES_FUNCTION
#pragma FTK_RES_FUNCTION(hwi_pnp_remove_card)
#endif

export void
hwi_pnp_remove_card(
    ADAPTER * adapter
    )
{
    ADAPTER_HANDLE adapter_handle = adapter->adapter_handle;
    WORD           control_1      = adapter->io_location +
                                        PNP_CONTROL_REGISTER_1;
    WORD           sifacl;

    /*
     *   interrupt must be disabled at adapter before unpatching interrupt
     *   even in polling mode we must turn off interrupts at adapter
     */

    sifacl = sys_insw(adapter_handle, adapter->sif_acl);
    sifacl = (sifacl & ~(EAGLE_SIFACL_PSDMAEN | EAGLE_SIFACL_SINTEN));

    sys_outsw(adapter_handle,
              adapter->sif_acl,
              sifacl);

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

    	adapter->interrupts_on = FALSE;
    }


    /*
     *   perform adapter reset, set PNP_CTRL1_NSRESET low
     */

    sys_outsb(adapter_handle, control_1, !PNP_CTRL1_NSRESET);
}


/****************************************************************************
*                                                                          
*                      hwi_atula_set_dio_address                           
*                      =========================                           
*                                                                          
* The hwi_atula_set_dio_address routine is used,  with  ATULA  cards,  for 
* putting  a  32 bit DIO address into the SIF DIO address and extended DIO 
* address registers. Note that the extended  address  register  should  be 
* loaded first.                                                            
*                                                                          
****************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_pnp_set_dio_address)
#endif

export void
hwi_pnp_set_dio_address(
    ADAPTER * adapter,
    DWORD     dio_address
    )
{
    ADAPTER_HANDLE adapter_handle = adapter->adapter_handle;
    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
     */

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

    /*
     *   load DIO address register with low 16 bits of address
     */

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

}


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

/*---------------------------------------------------------------------------
|                                                                          
|                      hwi_pnp_read_node_address                          
|                      ==========================                          
|                                                                          
| The hwi_pnp_read_node_address routine reads in the node address from    
| the BIA, and checks that it is a valid Madge node address.               
|                                                                          
---------------------------------------------------------------------------*/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_pnp_read_node_address)
#endif

local WBOOLEAN
hwi_pnp_read_node_address(
    ADAPTER * adapter
    )
{
    WORD ioport = adapter->io_location;

    adapter->permanent_address.byte[0] = 0;
    adapter->permanent_address.byte[1] = 0;
    adapter->permanent_address.byte[2] = (BYTE) pnp_read_a_word(adapter, 15);
    adapter->permanent_address.byte[3] = (BYTE) pnp_read_a_word(adapter, 14);
    adapter->permanent_address.byte[4] = (BYTE) pnp_read_a_word(adapter, 13);
    adapter->permanent_address.byte[5] = (BYTE) pnp_read_a_word(adapter, 12);

    return (adapter->permanent_address.byte[2] == MADGE_NODE_BYTE_2);
}


/*---------------------------------------------------------------------------
|                                                                          
|                      hwi_pnp_valid_io_location                          
|                      ==========================                          
|                                                                          
| The hwi_pnp_valid_io_location routine checks to see if  the  user  has  
| supplied a valid IO location for a PNP adapter card.                     
|                                                                          
---------------------------------------------------------------------------*/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_pnp_valid_io_location)
#endif

local WBOOLEAN
hwi_pnp_valid_io_location(
    WORD io_location
    )
{
    WBOOLEAN io_valid;

    switch (io_location)
    {

        case 0x1A20     :
        case 0x2A20     :
        case 0x3A20     :

        /*
         * The following are needed coz Chicago won't configure our
         * PNP card at xA20 (a problem with Chicago !??)
         */

        case 0x0140     :       /* In case CHICAGO cant find a free one */

        /*
         * It (Chicago) also wont allow cards to be at a 10 bit alias
         * of each other ! (despite the fact we set the bit that says
         * we fully decode all sixteen bits of the address !!)
         */
        
        case 0x0920     :
        case 0x0940     :
        case 0x0960     :
        case 0x0980     :

        case 0x0A20     :
        case 0x0A40     :
        case 0x0A60     :
        case 0x0A80     :
        case 0x0AA0     :

        case 0x0B20     :
        case 0x0B40     :
        case 0x0B60     :
        case 0x0B80     :

        /*
         *    These are the valid user supplied io locations
         */
            io_valid = TRUE;
            break;


        default         :

       /*
        *    Anything else is invalid
        */
	    io_valid = FALSE;
	    break;
    }

    return io_valid;
}


/*---------------------------------------------------------------------------
|                                                                          
|                      hwi_pnp_valid_irq_channel                          
|                      ==========================                          
|                                                                          
| The hwi_pnp_valid_irq_channel routine checks to see if  the  user  has  
| supplied a valid interrupt number for a PNP                              
|                                                                          
| If  the  user  has  stated that polling mode is to be used, then this is 
| always okay. If not, then a check is made that the user given interrupt  
| number is a valid number for  the  card  type.                           
|                                                                          
---------------------------------------------------------------------------*/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_pnp_valid_irq_channel)
#endif

local WBOOLEAN
hwi_pnp_valid_irq_channel(
    ADAPTER * adapter
    )
{
    WBOOLEAN int_valid;

    /*
     *   assume that interrupt number is valid
     */

    int_valid = TRUE;

    /*
     *   no need to do any check on interrupt number if in polling mode
     */

    if (adapter->interrupt_number != POLLING_INTERRUPTS_MODE)
    {
        /*
         *    Can only check that the interrupt number given is valid
         */

        switch (adapter->interrupt_number)
        {
            case  2 :
            case  3 :
            case  7 :
            case  9 :
            case 10 :
            case 11 :
            case 15 :
                break;

            default :
                int_valid = FALSE;
                break;

        }
    }

    if (!int_valid)
    {
    	adapter->error_record.type  = ERROR_TYPE_HWI;
	adapter->error_record.value = HWI_E_03_BAD_INTERRUPT_NUMBER;
    }

    return int_valid;
}


#ifndef FTK_NO_PROBE
/*---------------------------------------------------------------------------
|                                       
|                       hwi_pnp_probe_find_card
|                       =======================
|                                                                          
|  The hwi_pnp_find_card checks if a PNP card is at a particular location.
|  This is called by the probe routines.
|                                                                          
---------------------------------------------------------------------------*/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_pnp_probe_find_card)
#endif

local WBOOLEAN
hwi_pnp_probe_find_card(
    WORD io_location
    )
{
    WORD id_reg = io_location + PNP_ID_REGISTER;
    WORD i;

    /*
     *   Search for leading 'm'
     */

    for (i = 0; i < 4; i++)
    {
        if (sys_insb(0 , id_reg) == 'm')
        {
            /*
             *    Next byte must be 'd'.
             */

    	    if (sys_insb(0 , id_reg) == 'd')
            {
                return TRUE;
            }
        }
    }
            
    /*
     *    PNP ID not seen, or incorrect!
     */

    return FALSE;
}

/*---------------------------------------------------------------------------
|                                       
|                       hwi_pnp_probe_get_irq
|                       =====================
|                                                                          
|  The hwi_pnp_gwt_irq gets the Interrupt used by a plug and play card.
|  This is called by the probe routines.
|                                                                          
---------------------------------------------------------------------------*/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_pnp_probe_get_irq)
#endif

local WORD
hwi_pnp_probe_get_irq(
    WORD io_location
    )
{
    WORD interrupt;

    sys_probe_outsb(
        (io_location + PNP_CONFIG_ADDRESS_REGISTER),
        PNP_VENDOR_CONFIG_IRQ);

    interrupt = (WORD)sys_probe_insb(
                          (io_location+PNP_CONFIG_DATA_REGISTER));

    return interrupt;
}



#endif

/*---------------------------------------------------------------------------
|                                                                          
|                      hwi_pnp_test_for_id                                 
|                      ===================                                 
|                                                                          
| The hwi_pnp_test_for_id routine confirms that a real PNP card exists     
| at the supplied addreess by checking the contents of the ID register.    
|                                                                          
---------------------------------------------------------------------------*/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_pnp_test_for_id)
#endif

local WBOOLEAN
hwi_pnp_test_for_id(
    ADAPTER *      adapter
    )
{
    ADAPTER_HANDLE adapter_handle = adapter->adapter_handle;
    WORD           id_reg = adapter->io_location + PNP_ID_REGISTER;
    WORD           i;

    /*
    *   Search for leading 'm'
    */
    for (i = 0; i < 4; i++)
    {
        if (sys_insb(adapter_handle, id_reg) == 'm')
        {
            /*
            *    Next byte must be 'd' 
            */
    	    if (sys_insb(adapter_handle, id_reg) == 'd')
            {
                return TRUE;
            }
            break;
        }
    }
            
    /*
    *  PNP ID not seen, or incorrect.
    */
    return FALSE;
}


/*-------------------------------------------------------------------------*/

#ifndef     FTK_NO_PROBE

/************************************************************************
 * 
 * Support routines for the serial device fitted to the Plug aNd Play
 * cards. To be used by the probe functions.
 * 
 ************************************************************************/

local   void     pnp_probe_delay(       WORD io_location );
local   void     pnp_probe_set_clk(     WORD io_location );
local   void     pnp_probe_clr_clk(     WORD io_location );
local   void     pnp_probe_twitch_clk(  WORD io_location );
local   void     pnp_probe_start_bit(   WORD io_location );
local   void     pnp_probe_stop_bit(    WORD io_location );
local   WBOOLEAN pnp_probe_wait_ack(    WORD io_location );
local   WBOOLEAN pnp_probe_dummy_wait_ack(WORD io_location );


/************************************************************************
 * Read a byte from the control register for the specified adapter.
 *
 * Inputs  : Adapter structure.
 *
 * Outputs : Value read from control register.
 ***********************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_probe_read_ctrl)
#endif

local WORD pnp_probe_read_ctrl( WORD io_location )
{
   return sys_probe_insb( io_location + PNP_CON_REG_OFFSET );
}


/************************************************************************
 * Write a byte to the control register for the specified adapter.
 *
 * Inputs  : Adapter structure.
 *           The data to be written.
 *
 * Outputs : None.
 ***********************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_probe_write_ctrl)
#endif

local void pnp_probe_write_ctrl( WORD io_location, WORD data)
{
   sys_probe_outsb( io_location + PNP_CON_REG_OFFSET, (BYTE)data );
}


/************************************************************************
 * Delay to allow for serial device timing issues
 *
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_probe_delay)
#endif

local void pnp_probe_delay( WORD io_location )
{
    UINT i;

    for (i = 0; i < PNP_DELAY_CNT; i++)
    {
        sys_probe_insb( io_location );
    }
}

/************************************************************************
 * Set the serial device clock bit
 *
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_probe_set_clk)
#endif

local void pnp_probe_set_clk( WORD io_location )
{
    WORD temp;

    temp  = pnp_probe_read_ctrl( io_location );
    temp |= PNP_SSK;

    pnp_probe_write_ctrl( io_location, temp);

    pnp_probe_delay( io_location );                        
}


/************************************************************************
 * Clears the serial device clock bit
 *
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_probe_clr_clk)
#endif

local void pnp_probe_clr_clk( WORD io_location ) 
{
    WORD temp;

    temp  = pnp_probe_read_ctrl( io_location );
    temp &= ~PNP_SSK;

    pnp_probe_write_ctrl( io_location, temp);

    pnp_probe_delay( io_location );                        
}


/************************************************************************
 * Puts the serial device data port into OUTPUT mode
 *
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_probe_set_eeden)
#endif

local void pnp_probe_set_eeden( WORD io_location )
{
    WORD temp;

    temp  = pnp_probe_read_ctrl( io_location );
    temp |= PNP_EEDEN;                      
    
    pnp_probe_write_ctrl( io_location , temp);

    pnp_probe_delay( io_location );                        
}


/************************************************************************
 * Puts the serial device data port into INPUT mode
 *
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_probe_clr_eeden)
#endif

local void pnp_probe_clr_eeden( WORD io_location )
{
    WORD temp;

    temp  = pnp_probe_read_ctrl( io_location );
    temp &= ~PNP_EEDEN;                     
    
    pnp_probe_write_ctrl( io_location, temp);

    pnp_probe_delay( io_location );                        
}


/************************************************************************
 * Sets the clears the serial device clock bit to strobe data into device
 *
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_probe_twitch_clk)
#endif

local void pnp_probe_twitch_clk( WORD io_location )
{
    pnp_probe_set_clk( io_location );
    pnp_probe_clr_clk( io_location );
}


/************************************************************************
 * Sends a start bit to the serial device.
 *
 * This is done by a 1 to 0 transition of the data bit while the clock
 * bit it 1.
 *
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_probe_start_bit)
#endif

local void pnp_probe_start_bit( WORD io_location )
{
    WORD temp;

    temp  = pnp_probe_read_ctrl( io_location );
    temp &= ~(PNP_EEDO + PNP_EEDEN + PNP_SSK);
    temp |= (PNP_EEDO + PNP_EEDEN);           
    
    pnp_probe_write_ctrl( io_location, temp);

    pnp_probe_delay( io_location );                         
                                               
    temp |= PNP_SSK;                          
    
    pnp_probe_write_ctrl( io_location, temp);

    pnp_probe_delay( io_location );                         
                                               
    temp &= ~PNP_EEDO;

    pnp_probe_write_ctrl( io_location , temp);

    pnp_probe_delay( io_location );                         
                                               
    temp &= ~(PNP_EEDO + PNP_EEDEN + PNP_SSK);
    
    pnp_probe_write_ctrl( io_location , temp);

    pnp_probe_delay( io_location );                         
}


/************************************************************************
 * Sends a stop bit to the serial device.
 *
 * This is done by a 0 to 1 transition of the data bit while the clock
 * bit it 1.
 *
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_probe_stop_bit)
#endif

local void pnp_probe_stop_bit( WORD io_location )
{
    WORD temp;

    temp  = pnp_probe_read_ctrl( io_location );
    temp &= ~(PNP_EEDO + PNP_EEDEN + PNP_SSK);
    temp |= (PNP_EEDEN);                      
    
    pnp_probe_write_ctrl( io_location, temp);

    pnp_probe_delay( io_location );                         
                                               
    temp |= PNP_SSK;                          
    
    pnp_probe_write_ctrl(io_location, temp);

    pnp_probe_delay(io_location);                         
                                               
    temp |= PNP_EEDO;                         
    
    pnp_probe_write_ctrl(io_location, temp);

    pnp_probe_delay(io_location);                         
                                               
    temp &= ~(PNP_EEDO + PNP_EEDEN + PNP_SSK);
    
    pnp_probe_write_ctrl(io_location, temp);

    pnp_probe_delay(io_location);                         
}


/************************************************************************
 * Waits for the serial device to say its accepted the last cmd/data
 *
 * Inputs  : Adapter structure
 *
 * Outputs : TRUE if OK, FALSE if it timed out
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_probe_wait_ack)
#endif

local WBOOLEAN pnp_probe_wait_ack(WORD  io_location)
{
    WORD temp;
    WORD i;

    temp  = pnp_probe_read_ctrl(io_location);
    temp &= ~(PNP_EEDO + PNP_EEDEN + PNP_SSK);
    
    pnp_probe_write_ctrl(io_location, temp);

    pnp_probe_delay(io_location);                         
                                               
    for (i = 0; i < PNP_WAIT_CNT; i++)
    {
        pnp_probe_set_clk(io_location);

        temp = pnp_probe_read_ctrl(io_location);

        pnp_probe_clr_clk(io_location);

        if (!(temp & PNP_EEDO))
        {
            return TRUE;
        }
    }
    
    return FALSE;
}


/************************************************************************
 * Waits for the serial device to say its passed the last of the data to
 * be read.
 *
 * Inputs  : Adapter structure
 *
 * Outputs : TRUE if OK, FALSE if it timed out
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_probe_dummy_wait_ack)
#endif

local WBOOLEAN pnp_probe_dummy_wait_ack(WORD  io_location)
{
    WORD temp;
    WORD i;

    temp  = pnp_probe_read_ctrl(io_location);
    temp &= ~(PNP_EEDO + PNP_EEDEN + PNP_SSK);
    
    pnp_probe_write_ctrl(io_location, temp);

    pnp_probe_delay(io_location);                         
                                               
    for (i = 0; i < PNP_WAIT_CNT ; i++)
    {
        pnp_probe_set_clk(io_location);

        temp = pnp_probe_read_ctrl(io_location);

        pnp_probe_clr_clk(io_location);

        if (temp & PNP_EEDO)
        {
            return TRUE;
        }
    }
    
    return FALSE;
}


/************************************************************************
 * Writes a bit to the serial device
 * be read.
 *
 * Inputs  : Adapter structure
 *           The data bit to be written
 *
 * Outputs : TRUE if OK, FALSE if it timed out
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_probe_write_data)
#endif

local void pnp_probe_write_data(WORD  io_location, WORD data)
{
    WORD temp;

    temp  = pnp_probe_read_ctrl(io_location);
    temp &= ~(PNP_EEDO);
    temp |= (data & 0x0080) >> 6;
    
    pnp_probe_write_ctrl(io_location, temp);

    pnp_probe_delay(io_location);                     
}


/************************************************************************
 * 
 * Routine to read a byte from the serial device fitted to the Plug aNd Play
 * cards.
 * 
 * Inputs :   Adapter structure.
 *            Offset (address) in the serial device to read
 * 
 * Outputs :  A word with the interstng byte in the LSB
 * 
 * RCS 22/07/94
 * 
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_probe_read_a_word)
#endif

local WORD pnp_probe_read_a_word(WORD io_location, WORD index)
{
    WORD temp;
    WORD data_byte = 0;
    WORD i;

    /*
    *   Wake up the device
    */
    pnp_probe_start_bit(io_location);

    /*
    *    Set data 'OUTPUT' mode
    */
    pnp_probe_set_eeden(io_location);

    /*
    *   Send WRITE CMD - a dummy really to allow us to set the READ address!
    */
    temp = PNP_WRITE_CMD;

    /*
    *   MSB first !
    */
    for (i = 0; i < 8; i++)
    {
        pnp_probe_write_data(io_location, temp);
        pnp_probe_twitch_clk(io_location);
        temp = temp << 1;
    }

    if (!pnp_probe_wait_ack(io_location))
    {
        /*
        *   Return sommat invalid if it timed out !
        */
        return 0xffff;
    }

    /*
    *    Set data 'OUTPUT' mode
    */
    pnp_probe_set_eeden(io_location);

    /*
    *   Send Address in ROM
    */
    temp = index;

    /*
    *    MSB first !
    */
    for (i = 0; i < 8; i++)
    {
        pnp_probe_write_data(io_location, temp);
        pnp_probe_twitch_clk(io_location);
        temp = temp << 1;
    }

    if (!pnp_probe_wait_ack(io_location))
    {
        /*
        *    Return sommat invalid if it timed out !
        */
        return 0xffff;
    }

    pnp_probe_start_bit(io_location);

    /*
    *    Set data 'OUTPUT' mode
    */
    pnp_probe_set_eeden(io_location);

    /*
    *    Send READ CMD
    */
    temp = PNP_READ_CMD;

    /*
    *    MSB first !
    */
    for (i = 0; i < 8 ;i++)
    {
        pnp_probe_write_data(io_location, temp);
        pnp_probe_twitch_clk(io_location);
        temp = temp << 1;
    }

    if (!pnp_probe_wait_ack(io_location))
    {
        /*
        *    Return sommat invalid if it timed out !
        */
        return 0xffff;
    }

    /*
    *    Set data 'INPUT' mode
    */
    pnp_probe_clr_eeden(io_location);

    /*
    *    Now read the serial data - MSB first !
    */
    for (i = 0; i < 8 ;i++)
    {
        pnp_probe_set_clk(io_location);

        temp = pnp_probe_read_ctrl(io_location);

        pnp_probe_clr_clk(io_location);

        temp      &= PNP_EEDO;
        temp       = temp >> 1;
        data_byte  = data_byte << 1;
        data_byte &= 0xfffe;
        data_byte |= temp;
    }

    if (!pnp_probe_dummy_wait_ack(io_location))
    {
        /*
        *    Return sommat invalid if it timed out !
        */

        return 0xffff;
    }

    pnp_probe_stop_bit(io_location);

    return data_byte;
}

#endif

/************************************************************************
 * 
 * Support routines for the serial device fitted to the Plug aNd Play
 * cards.
 * 
 * RCS 22/07/94
 * 
 ************************************************************************/


local   void     pnp_delay(ADAPTER *);
local   void     pnp_set_clk(ADAPTER *);
local   void     pnp_clr_clk(ADAPTER *); 
local   void     pnp_twitch_clk(ADAPTER *);
local   void     pnp_start_bit(ADAPTER *);
local   void     pnp_stop_bit(ADAPTER *);
local   WBOOLEAN pnp_wait_ack(ADAPTER *);
local   WBOOLEAN pnp_dummy_wait_ack(ADAPTER *);
local   void     pnp_write_data(ADAPTER *, WORD);


/************************************************************************
 * Read a byte from the control register for the specified adapter.
 *
 * Inputs  : Adapter structure.
 *
 * Outputs : Value read from control register.
 ***********************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_read_ctrl)
#endif

local WORD pnp_read_ctrl(ADAPTER * adapter)
{
   return sys_insb(
              adapter->adapter_handle, 
              (WORD) (adapter->io_location + PNP_CON_REG_OFFSET)
              );
}


/************************************************************************
 * Write a byte to the control register for the specified adapter.
 *
 * Inputs  : Adapter structure.
 *           The data to be written.
 *
 * Outputs : None.
 ***********************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_write_ctrl)
#endif

local void pnp_write_ctrl(ADAPTER * adapter, WORD data)
{
   sys_outsb(
       adapter->adapter_handle, 
       (WORD) (adapter->io_location + PNP_CON_REG_OFFSET),
       (BYTE) data
       );
}


/************************************************************************
 * Delay to allow for serial device timing issues
 *
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_delay)
#endif

local void pnp_delay(ADAPTER * adapter)
{
    WORD i;

    for (i = 0; i < PNP_DELAY_CNT; i++)
    {
        sys_insb(adapter->adapter_handle, adapter->io_location);
    }
}


/************************************************************************
 * Set the serial device clock bit
 *
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_set_clk)
#endif

local void pnp_set_clk(ADAPTER * adapter)
{
    WORD temp;

    temp  = pnp_read_ctrl(adapter);
    temp |= PNP_SSK;

    pnp_write_ctrl(adapter, temp);

    pnp_delay(adapter);                        
}


/************************************************************************
 * Clears the serial device clock bit
 *
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_clr_clk)
#endif

local void pnp_clr_clk(ADAPTER * adapter) 
{
    WORD temp;

    temp  = pnp_read_ctrl(adapter);
    temp &= ~PNP_SSK;

    pnp_write_ctrl(adapter, temp);

    pnp_delay(adapter);                        
}


/************************************************************************
 * Puts the serial device data port into OUTPUT mode
 *
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_set_eeden)
#endif

local void pnp_set_eeden(ADAPTER * adapter)
{
    WORD temp;

    temp  = pnp_read_ctrl(adapter);
    temp |= PNP_EEDEN;                      
    
    pnp_write_ctrl(adapter, temp);

    pnp_delay(adapter);                        
}


/************************************************************************
 * Puts the serial device data port into INPUT mode
 *
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_clr_eeden)
#endif

local void pnp_clr_eeden(ADAPTER * adapter) 
{
    WORD temp;

    temp  = pnp_read_ctrl(adapter);
    temp &= ~PNP_EEDEN;                     
    
    pnp_write_ctrl(adapter, temp);

    pnp_delay(adapter);                        
}


/************************************************************************
 * Sets the clears the serial device clock bit to strobe data into device
 *
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_twitch_clk)
#endif

local void pnp_twitch_clk(ADAPTER * adapter)
{
    pnp_set_clk(adapter);
    pnp_clr_clk(adapter);
}


/************************************************************************
 * Sends a start bit to the serial device.
 *
 * This is done by a 1 to 0 transition of the data bit while the clock
 * bit it 1.
 *
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_start_bit)
#endif

local void pnp_start_bit(ADAPTER * adapter)
{
    WORD temp;

    temp  = pnp_read_ctrl(adapter);
    temp &= ~(PNP_EEDO + PNP_EEDEN + PNP_SSK);
    temp |= (PNP_EEDO + PNP_EEDEN);           
    
    pnp_write_ctrl(adapter, temp);

    pnp_delay(adapter);                         
                                               
    temp |= PNP_SSK;                          
    
    pnp_write_ctrl(adapter, temp);

    pnp_delay(adapter);                         
                                               
    temp &= ~PNP_EEDO;

    pnp_write_ctrl(adapter, temp);

    pnp_delay(adapter);                         
                                               
    temp &= ~(PNP_EEDO + PNP_EEDEN + PNP_SSK);
    
    pnp_write_ctrl(adapter, temp);

    pnp_delay(adapter);                         
}


/************************************************************************
 * Sends a stop bit to the serial device.
 *
 * This is done by a 0 to 1 transition of the data bit while the clock
 * bit it 1.
 *
 * Inputs  : Adapter structure
 *
 * Outputs : None
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_stop_bit)
#endif

local void pnp_stop_bit(ADAPTER * adapter)
{
    WORD temp;

    temp  = pnp_read_ctrl(adapter);
    temp &= ~(PNP_EEDO + PNP_EEDEN + PNP_SSK);
    temp |= (PNP_EEDEN);                      
    
    pnp_write_ctrl(adapter, temp);

    pnp_delay(adapter);                         
                                               
    temp |= PNP_SSK;                          
    
    pnp_write_ctrl(adapter, temp);

    pnp_delay(adapter);                         
                                               
    temp |= PNP_EEDO;                         
    
    pnp_write_ctrl(adapter, temp);

    pnp_delay(adapter);                         
                                               
    temp &= ~(PNP_EEDO + PNP_EEDEN + PNP_SSK);
    
    pnp_write_ctrl(adapter, temp);

    pnp_delay(adapter);                         
}


/************************************************************************
 * Waits for the serial device to say its accepted the last cmd/data
 *
 * Inputs  : Adapter structure
 *
 * Outputs : TRUE if OK, FALSE if it timed out
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_wait_ack)
#endif

local WBOOLEAN pnp_wait_ack(ADAPTER * adapter)
{
    WORD temp;
    WORD i;

    temp  = pnp_read_ctrl(adapter);
    temp &= ~(PNP_EEDO + PNP_EEDEN + PNP_SSK);
    
    pnp_write_ctrl(adapter, temp);

    pnp_delay(adapter);                         
                                               
    for (i = 0; i < PNP_WAIT_CNT; i++)
    {
        pnp_set_clk(adapter);

        temp = pnp_read_ctrl(adapter);

        pnp_clr_clk(adapter);

        if (!(temp & PNP_EEDO))
        {
            return TRUE;
        }
    }
    
    return FALSE;
}


/************************************************************************
 * Waits for the serial device to say its passed the last of the data to
 * be read.
 *
 * Inputs  : Adapter structure
 *
 * Outputs : TRUE if OK, FALSE if it timed out
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_dummy_wait_ack)
#endif

local WBOOLEAN pnp_dummy_wait_ack(ADAPTER * adapter)
{
    WORD temp;
    WORD i;

    temp  = pnp_read_ctrl(adapter);
    temp &= ~(PNP_EEDO + PNP_EEDEN + PNP_SSK);
    
    pnp_write_ctrl(adapter, temp);

    pnp_delay(adapter);                         
                                               
    for (i = 0; i < PNP_WAIT_CNT ; i++)
    {
        pnp_set_clk(adapter);

        temp = pnp_read_ctrl(adapter);

        pnp_clr_clk(adapter);

        if (temp & PNP_EEDO)
        {
            return TRUE;
        }
    }
    
    return FALSE;
}


/************************************************************************
 * Writes a bit to the serial device
 * be read.
 *
 * Inputs  : Adapter structure
 *           The data bit to be written
 *
 * Outputs : TRUE if OK, FALSE if it timed out
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_write_data)
#endif

local void pnp_write_data(ADAPTER * adapter, WORD data)
{
    WORD temp;

    temp  = pnp_read_ctrl(adapter);
    temp &= ~(PNP_EEDO);
    temp |= (data & 0x0080) >> 6;
    
    pnp_write_ctrl(adapter, temp);

    pnp_delay(adapter);                     
}


/************************************************************************
 * 
 * Routine to read a byte from the serial device fitted to the Plug aNd Play
 * cards.
 * 
 * Inputs :   Adapter structure.
 *            Offset (address) in the serial device to read
 * 
 * Outputs :  A word with the interstng byte in the LSB
 * 
 * RCS 22/07/94
 * 
 ************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(pnp_read_a_word)
#endif

local WORD pnp_read_a_word(ADAPTER * adapter, WORD index)
{
    WORD temp;
    WORD data_byte = 0;
    WORD i;

    /*
    *    Wake up the device
    */
    pnp_start_bit(adapter);

    /*
    *    Set data 'OUTPUT' mode
    */
    pnp_set_eeden(adapter);

    /*
    *   Send WRITE CMD - a dummy really to allow us to set the READ address! 
    */
    temp = PNP_WRITE_CMD;

    /*
    *    MSB first !
    */
    for (i = 0; i < 8; i++)
    {
        pnp_write_data(adapter, temp);
        pnp_twitch_clk(adapter);
        temp = temp << 1;
    }

    if (!pnp_wait_ack(adapter))
    {
        /*
        *   Return sommat invalid if it timed out !
        */
        return 0xffff;
    }

    /*
    *    Set data 'OUTPUT' mode
    */
    pnp_set_eeden(adapter);

    /*
    *    Send Address in ROM
    */
    temp = index;

    /*
    *    MSB first !
    */
    for (i = 0; i < 8; i++)
    {
        pnp_write_data(adapter, temp);
        pnp_twitch_clk(adapter);
        temp = temp << 1;
    }

    if (!pnp_wait_ack(adapter))
    {
        /*
        *    Return sommat invalid if it timed out !
        */
        return 0xffff;
    }

    pnp_start_bit(adapter);

    /*
    *    Set data 'OUTPUT' mode
    */
    pnp_set_eeden(adapter);

    /*
    *    Send READ CMD
    */
    temp = PNP_READ_CMD;

    /*
    *   MSB first !
    */
    for (i = 0; i < 8 ;i++)
    {
        pnp_write_data(adapter, temp);
        pnp_twitch_clk(adapter);
        temp = temp << 1;
    }

    if (!pnp_wait_ack(adapter))
    {
        /*
        *   Return sommat invalid if it timed out !
        */
        return 0xffff;
    }

    /*
    *   Set data 'INPUT' mode
    */
    pnp_clr_eeden(adapter);

    /*
    *   Now read the serial data - MSB first !
    */
    for (i = 0; i < 8 ;i++)
    {
        pnp_set_clk(adapter);

        temp = pnp_read_ctrl(adapter);

        pnp_clr_clk(adapter);

        temp      &= PNP_EEDO;
        temp       = temp >> 1;
        data_byte  = data_byte << 1;
        data_byte &= 0xfffe;
        data_byte |= temp;
    }

    if (!pnp_dummy_wait_ack(adapter))
    {
        /*
        *   Return sommat invalid if it timed out !
        */
        return 0xffff;
    }

    pnp_stop_bit(adapter);

    return data_byte;
}

#endif

/******** End of HWI_PNP.C *************************************************/