Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
   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
/*
 *  ISA Plug & Play support
 *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <linux/config.h>
#include <linux/version.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/ioport.h>
#include <linux/string.h>
#include <linux/malloc.h>
#include <linux/proc_fs.h>
#include <linux/delay.h>
#include <asm/io.h>
#include <asm/dma.h>
#include <asm/irq.h>
#include <linux/pci.h>
#include <linux/vmalloc.h>
#include <linux/poll.h>
#include <linux/init.h>
#include <asm/uaccess.h>
#include <linux/isapnp.h>

#ifdef CONFIG_PROC_FS
#include "isapnp_proc.c"
#endif

#if 0
#define ISAPNP_REGION_OK
#endif
#if 0
#define ISAPNP_DEBUG
#endif

struct resource *pidxr_res = NULL;
struct resource *pnpwrp_res = NULL;
struct resource *isapnp_rdp_res = NULL;

int isapnp_disable = 0;			/* Disable ISA PnP */
int isapnp_rdp = 0;			/* Read Data Port */
int isapnp_reset = 1;			/* reset all PnP cards (deactivate) */
int isapnp_skip_pci_scan = 0;		/* skip PCI resource scanning */
int isapnp_verbose = 1;			/* verbose mode */
int isapnp_reserve_irq[16] = { [0 ... 15] = -1 };	/* reserve (don't use) some IRQ */
int isapnp_reserve_dma[8] = { [0 ... 7] = -1 };		/* reserve (don't use) some DMA */
int isapnp_reserve_io[16] = { [0 ... 15] = -1 };	/* reserve (don't use) some I/O region */
int isapnp_reserve_mem[16] = { [0 ... 15] = -1 };	/* reserve (don't use) some memory region */

MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
MODULE_DESCRIPTION("Generic ISA Plug & Play support");
MODULE_PARM(isapnp_disable, "i");
MODULE_PARM_DESC(isapnp_disable, "ISA Plug & Play disable");
MODULE_PARM(isapnp_rdp, "i");
MODULE_PARM_DESC(isapnp_rdp, "ISA Plug & Play read data port");
MODULE_PARM(isapnp_reset, "i");
MODULE_PARM_DESC(isapnp_reset, "ISA Plug & Play reset all cards");
MODULE_PARM(isapnp_skip_pci_scan, "i");
MODULE_PARM_DESC(isapnp_skip_pci_scan, "ISA Plug & Play skip PCI resource scanning");
MODULE_PARM(isapnp_verbose, "i");
MODULE_PARM_DESC(isapnp_verbose, "ISA Plug & Play verbose mode");
MODULE_PARM(isapnp_reserve_irq, "1-16i");
MODULE_PARM_DESC(isapnp_reserve_irq, "ISA Plug & Play - reserve IRQ line(s)");
MODULE_PARM(isapnp_reserve_dma, "1-8i");
MODULE_PARM_DESC(isapnp_reserve_dma, "ISA Plug & Play - reserve DMA channel(s)");
MODULE_PARM(isapnp_reserve_io, "1-16i");
MODULE_PARM_DESC(isapnp_reserve_io, "ISA Plug & Play - reserve I/O region(s) - port,size");
MODULE_PARM(isapnp_reserve_mem, "1-16i");
MODULE_PARM_DESC(isapnp_reserve_mem, "ISA Plug & Play - reserve memory region(s) - address,size");

#define _PIDXR		0x279
#define _PNPWRP		0xa79

/* short tags */
#define _STAG_PNPVERNO		0x01
#define _STAG_LOGDEVID		0x02
#define _STAG_COMPATDEVID	0x03
#define _STAG_IRQ		0x04
#define _STAG_DMA		0x05
#define _STAG_STARTDEP		0x06
#define _STAG_ENDDEP		0x07
#define _STAG_IOPORT		0x08
#define _STAG_FIXEDIO		0x09
#define _STAG_VENDOR		0x0e
#define _STAG_END		0x0f
/* long tags */
#define _LTAG_MEMRANGE		0x81
#define _LTAG_ANSISTR		0x82
#define _LTAG_UNICODESTR	0x83
#define _LTAG_VENDOR		0x84
#define _LTAG_MEM32RANGE	0x85
#define _LTAG_FIXEDMEM32RANGE	0x86

struct pci_bus *isapnp_cards = NULL;	/* ISA PnP cards */
struct pci_dev *isapnp_devices = NULL;	/* ISA PnP devices */
static struct pci_dev *isapnp_last_device = NULL;
static unsigned char isapnp_checksum_value;
static DECLARE_MUTEX(isapnp_cfg_mutex);
static int isapnp_detected = 0;

/* some prototypes */

static int isapnp_config_prepare(struct pci_dev *dev);
static int isapnp_config_activate(struct pci_dev *dev);
static int isapnp_config_deactivate(struct pci_dev *dev);

static inline void write_data(unsigned char x)
{
	outb(x, _PNPWRP);
}

static inline void write_address(unsigned char x)
{
	outb(x, _PIDXR);
	udelay(20);
}

static inline unsigned char read_data(void)
{
	unsigned char val = inb(isapnp_rdp);
	return val;
}

unsigned char isapnp_read_byte(unsigned char idx)
{
	write_address(idx);
	return read_data();
}

unsigned short isapnp_read_word(unsigned char idx)
{
	unsigned short val;

	val = isapnp_read_byte(idx);
	val = (val << 8) + isapnp_read_byte(idx+1);
	return val;
}

unsigned int isapnp_read_dword(unsigned char idx)
{
	unsigned int val;

	val = isapnp_read_byte(idx);
	val = (val << 8) + isapnp_read_byte(idx+1);
	val = (val << 8) + isapnp_read_byte(idx+2);
	val = (val << 8) + isapnp_read_byte(idx+3);
	return val;
}

void isapnp_write_byte(unsigned char idx, unsigned char val)
{
	write_address(idx);
	write_data(val);
}

void isapnp_write_word(unsigned char idx, unsigned short val)
{
	isapnp_write_byte(idx, val >> 8);
	isapnp_write_byte(idx+1, val);
}

void isapnp_write_dword(unsigned char idx, unsigned int val)
{
	isapnp_write_byte(idx, val >> 24);
	isapnp_write_byte(idx+1, val >> 16);
	isapnp_write_byte(idx+2, val >> 8);
	isapnp_write_byte(idx+3, val);
}

static void *isapnp_alloc(long size)
{
	void *result;

	result = kmalloc(size, GFP_KERNEL);
	if (!result)
		return NULL;
	memset(result, 0, size);
	return result;
}

static void isapnp_key(void)
{
	unsigned char code = 0x6a, msb;
	int i;

	mdelay(1);
	write_address(0x00);
	write_address(0x00);

	write_address(code);

	for (i = 1; i < 32; i++) {
		msb = ((code & 0x01) ^ ((code & 0x02) >> 1)) << 7;
		code = (code >> 1) | msb;
		write_address(code);
	}
}

/* place all pnp cards in wait-for-key state */
static void isapnp_wait(void)
{
	isapnp_write_byte(0x02, 0x02);
}

void isapnp_wake(unsigned char csn)
{
	isapnp_write_byte(0x03, csn);
}

void isapnp_device(unsigned char logdev)
{
	isapnp_write_byte(0x07, logdev);
}

void isapnp_activate(unsigned char logdev)
{
	isapnp_device(logdev);
	isapnp_write_byte(ISAPNP_CFG_ACTIVATE, 1);
	udelay(250);
}

void isapnp_deactivate(unsigned char logdev)
{
	isapnp_device(logdev);
	isapnp_write_byte(ISAPNP_CFG_ACTIVATE, 0);
	udelay(500);
}

static void __init isapnp_peek(unsigned char *data, int bytes)
{
	int i, j;
	unsigned char d;

	for (i = 1; i <= bytes; i++) {
		for (j = 0; j < 10; j++) {
			d = isapnp_read_byte(0x05);
			if (d & 1)
				break;
			udelay(100);
		}
		if (!(d & 1)) {
			*data++ = 0xff;
			continue;
		}
		d = isapnp_read_byte(0x04);	/* PRESDI */
		isapnp_checksum_value += d;
		if (data != NULL)
			*data++ = d;
	}
}

#define RDP_STEP	32	/* minimum is 4 */

static int isapnp_next_rdp(void)
{
	int rdp = isapnp_rdp;
	while (rdp <= 0x3ff) {
		if (!check_region(rdp, 1)) {
			isapnp_rdp = rdp;
			return 0;
		}
		rdp += RDP_STEP;
	}
	return -1;
}

/* Set read port address */
static inline void isapnp_set_rdp(void)
{
	isapnp_write_byte(0x00, isapnp_rdp >> 2);
	udelay(100);
}

/*
 *	This code is badly broken. We cannot simply pick ports as the 
 *	ISAPnP specification implies. We should try 4 or 5 safe ports
 *	then bale by default.
 *
 *	This code touches NE2K cards or other devices and your box is
 *	history.
 */

static int __init isapnp_isolate_rdp_select(void)
{
	isapnp_wait();
	isapnp_key();

	/* Control: reset CSN and conditionally everything else too */
	isapnp_write_byte(0x02, isapnp_reset ? 0x05 : 0x04);
	mdelay(2);

	isapnp_wait();
	isapnp_key();
	isapnp_wake(0x00);

	if (isapnp_next_rdp() < 0) {
		isapnp_wait();
		return -1;
	}

	isapnp_set_rdp();
	udelay(1000);
	write_address(0x01);
	udelay(1000);
	return 0;
}

/*
 *  Isolate (assign uniqued CSN) to all ISA PnP devices.
 */

static int __init isapnp_isolate(void)
{
	unsigned char checksum = 0x6a;
	unsigned char chksum = 0x00;
	unsigned char bit = 0x00;
	int data;
	int csn = 0;
	int i;
	int iteration = 1;

	isapnp_rdp = 0x213;
	if (isapnp_isolate_rdp_select() < 0)
		return -1;

	while (1) {
		for (i = 1; i <= 64; i++) {
			data = read_data() << 8;
			udelay(250);
			data = data | read_data();
			udelay(250);
			if (data == 0x55aa)
				bit = 0x01;
			checksum = ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7) | (checksum >> 1);
			bit = 0x00;
		}
		for (i = 65; i <= 72; i++) {
			data = read_data() << 8;
			udelay(250);
			data = data | read_data();
			udelay(250);
			if (data == 0x55aa)
				chksum |= (1 << (i - 65));
		}
		if (checksum != 0x00 && checksum == chksum) {
			csn++;

			isapnp_write_byte(0x06, csn);
			udelay(250);
			iteration++;
			isapnp_wake(0x00);
			isapnp_set_rdp();
			udelay(1000);
			write_address(0x01);
			udelay(1000);
			goto __next;
		}
		if (iteration == 1) {
			isapnp_rdp += RDP_STEP;
			if (isapnp_isolate_rdp_select() < 0)
				return -1;
		} else if (iteration > 1) {
			break;
		}
	      __next:
		checksum = 0x6a;
		chksum = 0x00;
		bit = 0x00;
	}
	isapnp_wait();
	return csn;
}

/*
 *  Read one tag from stream.
 */

static int __init isapnp_read_tag(unsigned char *type, unsigned short *size)
{
	unsigned char tag, tmp[2];

	isapnp_peek(&tag, 1);
	if (tag == 0)				/* invalid tag */
		return -1;
	if (tag & 0x80) {	/* large item */
		*type = tag;
		isapnp_peek(tmp, 2);
		*size = (tmp[1] << 8) | tmp[0];
	} else {
		*type = (tag >> 3) & 0x0f;
		*size = tag & 0x07;
	}
#if 0
	printk("tag = 0x%x, type = 0x%x, size = %i\n", tag, *type, *size);
#endif
	if (type == 0)				/* wrong type */
		return -1;
	if (*type == 0xff && *size == 0xffff)	/* probably invalid data */
		return -1;
	return 0;
}

/*
 *  Skip specified number of bytes from stream.
 */
 
static void __init isapnp_skip_bytes(int count)
{
	isapnp_peek(NULL, count);
}

/*
 *  Parse logical device tag.
 */

static struct pci_dev * __init isapnp_parse_device(struct pci_bus *card, int size, int number)
{
	unsigned char tmp[6];
	struct pci_dev *dev;

	isapnp_peek(tmp, size);
	dev = isapnp_alloc(sizeof(struct pci_dev));
	if (!dev)
		return NULL;
	dev->devfn = number;
	dev->vendor = (tmp[1] << 8) | tmp[0];
	dev->device = (tmp[3] << 8) | tmp[2];
	dev->regs = tmp[4];
	dev->bus = card;
	if (size > 5)
		dev->regs |= tmp[5] << 8;
	dev->prepare = isapnp_config_prepare;
	dev->activate = isapnp_config_activate;
	dev->deactivate = isapnp_config_deactivate;
	return dev;
}

/*
 *  Build new resources structure
 */

static struct isapnp_resources * __init isapnp_build_resources(struct pci_dev *dev, int dependent)
{
	struct isapnp_resources *res, *ptr, *ptra;
	
	res = isapnp_alloc(sizeof(struct isapnp_resources));
	if (!res)
		return NULL;
	res->dev = dev;
	ptr = (struct isapnp_resources *)dev->sysdata;
	while (ptr && ptr->next)
		ptr = ptr->next;
	if (ptr && ptr->dependent && dependent) { /* add to another list */
		ptra = ptr->alt;
		while (ptra && ptra->alt)
			ptra = ptra->alt;
		if (!ptra)
			ptr->alt = res;
		else
			ptra->alt = res;
	} else {
		if (!ptr)
			dev->sysdata = res;
		else
			ptr->next = res;
	}
	if (dependent) {
		res->priority = dependent & 0xff;
		if (res->priority > ISAPNP_RES_PRIORITY_FUNCTIONAL)
			res->priority = ISAPNP_RES_PRIORITY_INVALID;
		res->dependent = 1;
	} else {
		res->priority = ISAPNP_RES_PRIORITY_PREFERRED;
		res->dependent = 0;
	}
	return res;
}

/*
 *  Add IRQ resource to resources list.
 */

static void __init isapnp_add_irq_resource(struct pci_dev *dev,
                                    	       struct isapnp_resources **res,
                                               int dependent, int size)
{
	unsigned char tmp[3];
	struct isapnp_irq *irq, *ptr;

	isapnp_peek(tmp, size);
	irq = isapnp_alloc(sizeof(struct isapnp_irq));
	if (!irq)
		return;
	if (*res == NULL) {
		*res = isapnp_build_resources(dev, dependent);
		if (*res == NULL) {
			kfree(irq);
			return;
		}
	}
	irq->map = (tmp[1] << 8) | tmp[0];
	if (size > 2)
		irq->flags = tmp[2];
	else
		irq->flags = IORESOURCE_IRQ_HIGHEDGE;
	irq->res = *res;
	ptr = (*res)->irq;
	while (ptr && ptr->next)
		ptr = ptr->next;
	if (ptr)
		ptr->next = irq;
	else
		(*res)->irq = irq;
}

/*
 *  Add DMA resource to resources list.
 */

static void __init isapnp_add_dma_resource(struct pci_dev *dev,
                                    	       struct isapnp_resources **res,
                                    	       int dependent, int size)
{
	unsigned char tmp[2];
	struct isapnp_dma *dma, *ptr;

	isapnp_peek(tmp, size);
	dma = isapnp_alloc(sizeof(struct isapnp_dma));
	if (!dma)
		return;
	if (*res == NULL) {
		*res = isapnp_build_resources(dev, dependent);
		if (*res == NULL) {
			kfree(dma);
			return;
		}
	}
	dma->map = tmp[0];
	dma->flags = tmp[1];
	dma->res = *res;
	ptr = (*res)->dma;
	while (ptr && ptr->next)
		ptr = ptr->next;
	if (ptr)
		ptr->next = dma;
	else
		(*res)->dma = dma;
}

/*
 *  Add port resource to resources list.
 */

static void __init isapnp_add_port_resource(struct pci_dev *dev,
						struct isapnp_resources **res,
						int dependent, int size)
{
	unsigned char tmp[7];
	struct isapnp_port *port, *ptr;

	isapnp_peek(tmp, size);
	port = isapnp_alloc(sizeof(struct isapnp_port));
	if (!port)
		return;
	if (*res == NULL) {
		*res = isapnp_build_resources(dev, dependent);
		if (*res == NULL) {
			kfree(port);
			return;
		}
	}
	port->min = (tmp[2] << 8) | tmp[1];
	port->max = (tmp[4] << 8) | tmp[3];
	port->align = tmp[5];
	port->size = tmp[6];
	port->flags = tmp[0] ? ISAPNP_PORT_FLAG_16BITADDR : 0;
	port->res = *res;
	ptr = (*res)->port;
	while (ptr && ptr->next)
		ptr = ptr->next;
	if (ptr)
		ptr->next = port;
	else
		(*res)->port = port;
}

/*
 *  Add fixed port resource to resources list.
 */

static void __init isapnp_add_fixed_port_resource(struct pci_dev *dev,
						      struct isapnp_resources **res,
						      int dependent, int size)
{
	unsigned char tmp[3];
	struct isapnp_port *port, *ptr;

	isapnp_peek(tmp, size);
	port = isapnp_alloc(sizeof(struct isapnp_port));
	if (!port)
		return;
	if (*res == NULL) {
		*res = isapnp_build_resources(dev, dependent);
		if (*res == NULL) {
			kfree(port);
			return;
		}
	}
	port->min = port->max = (tmp[1] << 8) | tmp[0];
	port->size = tmp[2];
	port->align = 0;
	port->flags = ISAPNP_PORT_FLAG_FIXED;
	port->res = *res;
	ptr = (*res)->port;
	while (ptr && ptr->next)
		ptr = ptr->next;
	if (ptr)
		ptr->next = port;
	else
		(*res)->port = port;
}

/*
 *  Add memory resource to resources list.
 */

static void __init isapnp_add_mem_resource(struct pci_dev *dev,
					       struct isapnp_resources **res,
					       int dependent, int size)
{
	unsigned char tmp[9];
	struct isapnp_mem *mem, *ptr;

	isapnp_peek(tmp, size);
	mem = isapnp_alloc(sizeof(struct isapnp_mem));
	if (!mem)
		return;
	if (*res == NULL) {
		*res = isapnp_build_resources(dev, dependent);
		if (*res == NULL) {
			kfree(mem);
			return;
		}
	}
	mem->min = ((tmp[2] << 8) | tmp[1]) << 8;
	mem->max = ((tmp[4] << 8) | tmp[3]) << 8;
	mem->align = (tmp[6] << 8) | tmp[5];
	mem->size = ((tmp[8] << 8) | tmp[7]) << 8;
	mem->flags = tmp[0];
	mem->res = *res;
	ptr = (*res)->mem;
	while (ptr && ptr->next)
		ptr = ptr->next;
	if (ptr)
		ptr->next = mem;
	else
		(*res)->mem = mem;
}

/*
 *  Add 32-bit memory resource to resources list.
 */

static void __init isapnp_add_mem32_resource(struct pci_dev *dev,
						 struct isapnp_resources **res,
						 int dependent, int size)
{
	unsigned char tmp[17];
	struct isapnp_mem32 *mem32, *ptr;

	isapnp_peek(tmp, size);
	mem32 = isapnp_alloc(sizeof(struct isapnp_mem32));
	if (!mem32)
		return;
	if (*res == NULL) {
		*res = isapnp_build_resources(dev, dependent);
		if (*res == NULL) {
			kfree(mem32);
			return;
		}
	}
	memcpy(mem32->data, tmp, 17);
	mem32->res = *res;
	ptr = (*res)->mem32;
	while (ptr && ptr->next)
		ptr = ptr->next;
	if (ptr)
		ptr->next = mem32;
	else
		(*res)->mem32 = mem32;
}

/*
 *  Add 32-bit fixed memory resource to resources list.
 */

static void __init isapnp_add_fixed_mem32_resource(struct pci_dev *dev,
						       struct isapnp_resources **res,
						       int dependent, int size)
{
	unsigned char tmp[17];
	struct isapnp_mem32 *mem32, *ptr;

	isapnp_peek(tmp, size);
	mem32 = isapnp_alloc(sizeof(struct isapnp_mem32));
	if (!mem32)
		return;
	if (*res == NULL) {
		*res = isapnp_build_resources(dev, dependent);
		if (*res == NULL) {
			kfree(mem32);
			return;
		}
	}
	memcpy(mem32->data, tmp, 17);
	mem32->res = *res;
	ptr = (*res)->mem32;
	while (ptr && ptr->next)
		ptr = ptr->next;
	if (ptr)
		ptr->next = mem32;
	else
		(*res)->mem32 = mem32;
}

/*
 *  Parse resource map for logical device.
 */

static int __init isapnp_create_device(struct pci_bus *card,
					   unsigned short size)
{
	int number = 0, skip = 0, dependent = 0, compat = 0;
	unsigned char type, tmp[17];
	struct pci_dev *dev, *prev_dev;
	struct isapnp_resources *res = NULL;
	
	if ((dev = isapnp_parse_device(card, size, number++)) == NULL)
		return 1;
	card->devices = dev;
	if (isapnp_last_device) {
		isapnp_last_device->next = dev;
		isapnp_last_device = dev;
	} else {
		isapnp_devices = isapnp_last_device = dev;
	}
	while (1) {
		if (isapnp_read_tag(&type, &size)<0)
			return 1;
		if (skip && type != _STAG_LOGDEVID && type != _STAG_END)
			goto __skip;
		switch (type) {
		case _STAG_LOGDEVID:
			if (size >= 5 && size <= 6) {
				prev_dev = dev;
				isapnp_config_prepare(dev);
				if ((dev = isapnp_parse_device(card, size, number++)) == NULL)
					return 1;
				prev_dev->sibling = dev;
				isapnp_last_device->next = dev;
				isapnp_last_device = dev;
				size = 0;
				skip = 0;
			} else {
				skip = 1;
			}
			res = NULL;
			dependent = 0;
			compat = 0;
			break;
		case _STAG_COMPATDEVID:
			if (size == 4 && compat < DEVICE_COUNT_COMPATIBLE) {
				isapnp_peek(tmp, 4);
				dev->vendor_compatible[compat] = (tmp[1] << 8) | tmp[0];
				dev->device_compatible[compat] = (tmp[3] << 8) | tmp[2];
				compat++;
				size = 0;
			}
			break;
		case _STAG_IRQ:
			if (size < 2 || size > 3)
				goto __skip;
			isapnp_add_irq_resource(dev, &res, dependent, size);
			size = 0;
			break;
		case _STAG_DMA:
			if (size != 2)
				goto __skip;
			isapnp_add_dma_resource(dev, &res, dependent, size);
			size = 0;
			break;
		case _STAG_STARTDEP:
			if (size > 1)
				goto __skip;
			res = NULL;
			dependent = 0x100 | ISAPNP_RES_PRIORITY_ACCEPTABLE;
			if (size > 0) {
				isapnp_peek(tmp, size);
				dependent = 0x100 | tmp[0];
				size = 0;
			}
			break;
		case _STAG_ENDDEP:
			if (size != 0)
				goto __skip;
			res = NULL;
			dependent = 0;
			break;
		case _STAG_IOPORT:
			if (size != 7)
				goto __skip;
			isapnp_add_port_resource(dev, &res, dependent, size);
			size = 0;
			break;
		case _STAG_FIXEDIO:
			if (size != 3)
				goto __skip;
			isapnp_add_fixed_port_resource(dev, &res, dependent, size);
			size = 0;
			break;
		case _STAG_VENDOR:
			break;
		case _LTAG_MEMRANGE:
			if (size != 9)
				goto __skip;
			isapnp_add_mem_resource(dev, &res, dependent, size);
			size = 0;
			break;
		case _LTAG_ANSISTR:
			if (dev->name[0] == '\0') {
				unsigned short size1 = size > 47 ? 47 : size;
				isapnp_peek(dev->name, size1);
				dev->name[size1] = '\0';
				size -= size1;
			}
			break;
		case _LTAG_UNICODESTR:
			/* silently ignore */
			/* who use unicode for hardware identification? */
			break;
		case _LTAG_VENDOR:
			break;
		case _LTAG_MEM32RANGE:
			if (size != 17)
				goto __skip;
			isapnp_add_mem32_resource(dev, &res, dependent, size);
			size = 0;
			break;
		case _LTAG_FIXEDMEM32RANGE:
			if (size != 17)
				goto __skip;
			isapnp_add_fixed_mem32_resource(dev, &res, dependent, size);
			size = 0;
			break;
		case _STAG_END:
			if (size > 0)
				isapnp_skip_bytes(size);
			return 1;
		default:
			printk("isapnp: unexpected or unknown tag type 0x%x for logical device %i (device %i), ignored\n", type, dev->devfn, card->number);
		}
	      __skip:
	      	if (size > 0)
		      	isapnp_skip_bytes(size);
	}
	isapnp_config_prepare(dev);
	return 0;
}

/*
 *  Parse resource map for ISA PnP card.
 */
 
static void __init isapnp_parse_resource_map(struct pci_bus *card)
{
	unsigned char type, tmp[17];
	unsigned short size;
	
	while (1) {
		if (isapnp_read_tag(&type, &size)<0)
			return;
		switch (type) {
		case _STAG_PNPVERNO:
			if (size != 2)
				goto __skip;
			isapnp_peek(tmp, 2);
			card->pnpver = tmp[0];
			card->productver = tmp[1];
			size = 0;
			break;
		case _STAG_LOGDEVID:
			if (size >= 5 && size <= 6) {
				if (isapnp_create_device(card, size)==1)
					return;
				size = 0;
			}
			break;
		case _STAG_VENDOR:
			break;
		case _LTAG_ANSISTR:
			if (card->name[0] == '\0') {
				unsigned short size1 = size > 47 ? 47 : size;
				isapnp_peek(card->name, size1);
				card->name[size1] = '\0';
				size -= size1;
			}
			break;
		case _LTAG_UNICODESTR:
			/* silently ignore */
			/* who use unicode for hardware identification? */
			break;
		case _LTAG_VENDOR:
			break;
		case _STAG_END:
			if (size > 0)
				isapnp_skip_bytes(size);
			return;
		default:
			printk("isapnp: unexpected or unknown tag type 0x%x for device %i, ignored\n", type, card->number);
		}
	      __skip:
	      	if (size > 0)
		      	isapnp_skip_bytes(size);
	}
}

/*
 *  Compute ISA PnP checksum for first eight bytes.
 */

static unsigned char __init isapnp_checksum(unsigned char *data)
{
	int i, j;
	unsigned char checksum = 0x6a, bit, b;
	
	for (i = 0; i < 8; i++) {
		b = data[i];
		for (j = 0; j < 8; j++) {
			bit = 0;
			if (b & (1 << j))
				bit = 1;
			checksum = ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7) | (checksum >> 1);
		}
	}
	return checksum;
}

/*
 *  Build device list for all present ISA PnP devices.
 */

static int __init isapnp_build_device_list(void)
{
	int csn;
	unsigned char header[9], checksum;
	struct pci_bus *card, *prev = NULL;

	isapnp_wait();
	isapnp_key();
	for (csn = 1; csn <= 10; csn++) {
		isapnp_wake(csn);
		isapnp_peek(header, 9);
		checksum = isapnp_checksum(header);
#if 0
		printk("vendor: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
			header[0], header[1], header[2], header[3],
			header[4], header[5], header[6], header[7], header[8]);
		printk("checksum = 0x%x\n", checksum);
#endif
		if (checksum == 0x00 || checksum != header[8])	/* not valid CSN */
			continue;
		if ((card = isapnp_alloc(sizeof(struct pci_bus))) == NULL)
			continue;
		card->number = csn;
		card->vendor = (header[1] << 8) | header[0];
		card->device = (header[3] << 8) | header[2];
		card->serial = (header[7] << 24) | (header[6] << 16) | (header[5] << 8) | header[4];
		isapnp_checksum_value = 0x00;
		isapnp_parse_resource_map(card);
		if (isapnp_checksum_value != 0x00)
			printk("isapnp: checksum for device %i is not valid (0x%x)\n", csn, isapnp_checksum_value);
		card->checksum = isapnp_checksum_value;
		if (!isapnp_cards)
			isapnp_cards = card;
		else
			prev->next = card;
		prev = card;
	}
	return 0;
}

/*
 *  Basic configuration routines.
 */

int isapnp_present(void)
{
	if (isapnp_devices)
		return 1;
	return 0;
}

int isapnp_cfg_begin(int csn, int logdev)
{
	if (csn < 1 || csn > 10 || logdev > 10)
		return -EINVAL;
	MOD_INC_USE_COUNT;
	down(&isapnp_cfg_mutex);
	isapnp_wait();
	isapnp_key();
	isapnp_wake(csn);
#if 1	/* to avoid malfunction when the isapnptools package is used */
	isapnp_set_rdp();
	udelay(1000);	/* delay 1000us */
	write_address(0x01);
	udelay(1000);	/* delay 1000us */
#endif
	if (logdev >= 0)
		isapnp_device(logdev);
	return 0;
}

int isapnp_cfg_end(void)
{
	isapnp_wait();
	up(&isapnp_cfg_mutex);
	MOD_DEC_USE_COUNT;
	return 0;
}

/*
 *  Resource manager.
 */

static struct isapnp_port *isapnp_find_port(struct pci_dev *dev, int index)
{
	struct isapnp_resources *res;
	struct isapnp_port *port;
	
	if (!dev || index < 0 || index > 7)
		return NULL;
	for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
		for (port = res->port; port; port = port->next) {
			if (!index)
				return port;
			index--;
		}
	}
	return NULL;
}

struct isapnp_irq *isapnp_find_irq(struct pci_dev *dev, int index)
{
	struct isapnp_resources *res, *resa;
	struct isapnp_irq *irq;
	int index1, index2, index3;
	
	if (!dev || index < 0 || index > 7)
		return NULL;
	for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
		index3 = 0;
		for (resa = res; resa; resa = resa->alt) {
			index1 = index;
			index2 = 0;
			for (irq = resa->irq; irq; irq = irq->next) {
				if (!index1)
					return irq;
				index1--;
				index2++;
			}
			if (index3 < index2)
				index3 = index2;
		}
		index -= index3;
	}
	return NULL;
}

struct isapnp_dma *isapnp_find_dma(struct pci_dev *dev, int index)
{
	struct isapnp_resources *res;
	struct isapnp_dma *dma;
	
	if (!dev || index < 0 || index > 7)
		return NULL;
	for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
		for (dma = res->dma; dma; dma = dma->next) {
			if (!index)
				return dma;
			index--;
		}
	}
	return NULL;
}

struct isapnp_mem *isapnp_find_mem(struct pci_dev *dev, int index)
{
	struct isapnp_resources *res;
	struct isapnp_mem *mem;
	
	if (!dev || index < 0 || index > 7)
		return NULL;
	for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
		for (mem = res->mem; mem; mem = mem->next) {
			if (!index)
				return mem;
			index--;
		}
	}
	return NULL;
}

struct isapnp_mem32 *isapnp_find_mem32(struct pci_dev *dev, int index)
{
	struct isapnp_resources *res;
	struct isapnp_mem32 *mem32;
	
	if (!dev || index < 0 || index > 7)
		return NULL;
	for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
		for (mem32 = res->mem32; mem32; mem32 = mem32->next) {
			if (!index)
				return mem32;
			index--;
		}
	}
	return NULL;
}

/*
 *  Device manager.
 */

struct pci_bus *isapnp_find_card(unsigned short vendor,
				 unsigned short device,
				 struct pci_bus *from)
{
	struct pci_bus *card;

	if (from == NULL) {
		from = isapnp_cards;
	} else {
		from = from->next;
	}
	for (card = from; card; card = card->next) {
		if (card->vendor == vendor && card->device == device)
			return card;
	}
	return NULL;
}

struct pci_dev *isapnp_find_dev(struct pci_bus *card,
				unsigned short vendor,
				unsigned short function,
				struct pci_dev *from)
{
	struct pci_dev *dev;
	int idx;
	
	if (card == NULL) {	/* look for a logical device from all cards */
		if (from == NULL) {
			from = isapnp_devices;
		} else {
			from = from->next;
		}
		for (dev = from; dev; dev = dev->next) {
			if (dev->vendor == vendor && dev->device == function)
				return dev;
			for (idx = 0; idx < DEVICE_COUNT_COMPATIBLE; idx++)
				if (dev->vendor_compatible[idx] == vendor &&
				    dev->device_compatible[idx] == function)
					return dev;
		}
	} else {
		if (from == NULL) {
			from = card->devices;
		} else {
			from = from->next;
		}
		if (from->bus != card)	/* something is wrong */
			return NULL;
		for (dev = from; dev; dev = dev->sibling) {
			if (dev->vendor == vendor && dev->device == function)
				return dev;
			for (idx = 0; idx < DEVICE_COUNT_COMPATIBLE; idx++)
				if (dev->vendor_compatible[idx] == vendor &&
				    dev->device_compatible[idx] == function)
					return dev;
		}		
	}
	return NULL;
}

static unsigned int isapnp_dma_resource_flags(struct isapnp_dma *dma)
{
	return dma->flags | IORESOURCE_DMA | IORESOURCE_AUTO;
}

static unsigned int isapnp_mem_resource_flags(struct isapnp_mem *mem)
{
	unsigned int result;

	result = mem->flags | IORESOURCE_MEM | IORESOURCE_AUTO;
	if (!(mem->flags & IORESOURCE_MEM_WRITEABLE))
		result |= IORESOURCE_READONLY;
	if (mem->flags & IORESOURCE_MEM_CACHEABLE)
		result |= IORESOURCE_CACHEABLE;
	if (mem->flags & IORESOURCE_MEM_RANGELENGTH)
		result |= IORESOURCE_RANGELENGTH;
	if (mem->flags & IORESOURCE_MEM_SHADOWABLE)
		result |= IORESOURCE_SHADOWABLE;
	return result;
}

static unsigned int isapnp_irq_resource_flags(struct isapnp_irq *irq)
{
	return irq->flags | IORESOURCE_IRQ | IORESOURCE_AUTO;
}

static unsigned int isapnp_port_resource_flags(struct isapnp_port *port)
{
	return port->flags | IORESOURCE_IO | IORESOURCE_AUTO;
}

static int isapnp_config_prepare(struct pci_dev *dev)
{
	struct isapnp_resources *res, *resa;
	struct isapnp_port *port;
	struct isapnp_irq *irq;
	struct isapnp_dma *dma;
	struct isapnp_mem *mem;
	int port_count, port_count1;
	int irq_count, irq_count1;
	int dma_count, dma_count1;
	int mem_count, mem_count1;
	int idx;

	if (dev == NULL)
		return -EINVAL;
	if (dev->active || dev->ro)
		return -EBUSY;
	for (idx = 0; idx < DEVICE_COUNT_IRQ; idx++) {
		dev->irq_resource[idx].name = NULL;
		dev->irq_resource[idx].start = 0;
		dev->irq_resource[idx].end = 0;
		dev->irq_resource[idx].flags = 0;
	}
	for (idx = 0; idx < DEVICE_COUNT_DMA; idx++) {
		dev->dma_resource[idx].name = NULL;
		dev->dma_resource[idx].start = 0;
		dev->dma_resource[idx].end = 0;
		dev->dma_resource[idx].flags = 0;
	}
	for (idx = 0; idx < DEVICE_COUNT_RESOURCE; idx++) {
		dev->resource[idx].name = NULL;
		dev->resource[idx].start = 0;
		dev->resource[idx].end = 0;
		dev->resource[idx].flags = 0;
	}
	port_count = irq_count = dma_count = mem_count = 0;
	for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
		port_count1 = irq_count1 = dma_count1 = mem_count1 = 0;
		for (resa = res; resa; resa = resa->alt) {
			for (port = resa->port, idx = 0; port; port = port->next, idx++) {
				if (dev->resource[port_count + idx].flags == 0) {
					dev->resource[port_count + idx].flags = isapnp_port_resource_flags(port);
					dev->resource[port_count + idx].end = port->size;
				}
			}
			if (port_count1 < idx)
				port_count1 = idx;
			for (irq = resa->irq, idx = 0; irq; irq = irq->next, idx++) {
				int count = irq_count + idx;
				if (count < DEVICE_COUNT_IRQ) {
					if (dev->irq_resource[count].flags == 0) {
						dev->irq_resource[count].flags = isapnp_irq_resource_flags(irq);
					}
				}
				
			}
			if (irq_count1 < idx)
				irq_count1 = idx;
			for (dma = resa->dma, idx = 0; dma; dma = dma->next, idx++)
				if (dev->dma_resource[idx].flags == 0) {
					dev->dma_resource[idx].flags = isapnp_dma_resource_flags(dma);
				}
			if (dma_count1 < idx)
				dma_count1 = idx;
			for (mem = resa->mem, idx = 0; mem; mem = mem->next, idx++)
				if (dev->resource[mem_count + idx + 8].flags == 0) {
					dev->resource[mem_count + idx + 8].flags = isapnp_mem_resource_flags(mem);
				}
			if (mem_count1 < idx)
				mem_count1 = idx;
		}
		port_count += port_count1;
		irq_count += irq_count1;
		dma_count += dma_count1;
		mem_count += mem_count1;
	}
	return 0;
}

struct isapnp_cfgtmp {
	struct isapnp_port *port[8];
	struct isapnp_irq *irq[2];
	struct isapnp_dma *dma[2];
	struct isapnp_mem *mem[4];
	struct pci_dev *request;
	struct pci_dev result;
};

static int isapnp_alternative_switch(struct isapnp_cfgtmp *cfg,
				     struct isapnp_resources *from,
				     struct isapnp_resources *to)
{
	int tmp, tmp1;
	struct isapnp_port *port;
	struct isapnp_irq *irq;
	struct isapnp_dma *dma;
	struct isapnp_mem *mem;

	if (!cfg)
		return -EINVAL;
	/* process port settings */
	for (tmp = 0; tmp < 8; tmp++) {
		if (!(cfg->request->resource[tmp].flags & IORESOURCE_AUTO))
			continue;		/* don't touch */
		port = cfg->port[tmp];
		if (!port) {
			cfg->port[tmp] = port = isapnp_find_port(cfg->request, tmp);
			if (!port)
				return -EINVAL;
		}
		if (from && port->res == from) {
			while (port->res != to) {
				if (!port->res->alt)
					return -EINVAL;
				port = port->res->alt->port;
				for (tmp1 = tmp; tmp1 > 0 && port; tmp1--)
					port = port->next;
				cfg->port[tmp] = port;
				if (!port)
					return -ENOENT;
				cfg->result.resource[tmp].flags = isapnp_port_resource_flags(port);
			}
		}
	}
	/* process irq settings */
	for (tmp = 0; tmp < 2; tmp++) {
		if (!(cfg->request->irq_resource[tmp].flags & IORESOURCE_AUTO))
			continue;		/* don't touch */
		irq = cfg->irq[tmp];
		if (!irq) {
			cfg->irq[tmp] = irq = isapnp_find_irq(cfg->request, tmp);
			if (!irq)
				return -EINVAL;
		}
		if (from && irq->res == from) {
			while (irq->res != to) {
				if (!irq->res->alt)
					return -EINVAL;
				irq = irq->res->alt->irq;
				for (tmp1 = tmp; tmp1 > 0 && irq; tmp1--)
					irq = irq->next;
				cfg->irq[tmp] = irq;
				if (!irq)
					return -ENOENT;
				cfg->result.irq_resource[tmp].flags = isapnp_irq_resource_flags(irq);
			}
		}
	}
	/* process dma settings */
	for (tmp = 0; tmp < 2; tmp++) {
		if (!(cfg->request->dma_resource[tmp].flags & IORESOURCE_AUTO))
			continue;		/* don't touch */
		dma = cfg->dma[tmp];
		if (!dma) {
			cfg->dma[tmp] = dma = isapnp_find_dma(cfg->request, tmp);
			if (!dma)
				return -EINVAL;
		}
		if (from && dma->res == from) {
			while (dma->res != to) {
				if (!dma->res->alt)
					return -EINVAL;
				dma = dma->res->alt->dma;
				for (tmp1 = tmp; tmp1 > 0 && dma; tmp1--)
					dma = dma->next;
				cfg->dma[tmp] = dma;
				if (!dma)
					return -ENOENT;
				cfg->result.dma_resource[tmp].flags = isapnp_dma_resource_flags(dma);
			}
		}
	}
	/* process memory settings */
	for (tmp = 0; tmp < 4; tmp++) {
		if (!(cfg->request->resource[tmp + 8].flags & IORESOURCE_AUTO))
			continue;		/* don't touch */
		mem = cfg->mem[tmp];
		if (!mem) {
			cfg->mem[tmp] = mem = isapnp_find_mem(cfg->request, tmp);
			if (!mem)
				return -EINVAL;
		}
		if (from && mem->res == from) {
			while (mem->res != to) {
				if (!mem->res->alt)
					return -EINVAL;
				mem = mem->res->alt->mem;
				for (tmp1 = tmp; tmp1 > 0 && mem; tmp1--)
					mem = mem->next;
				cfg->mem[tmp] = mem;
				if (!mem)
					return -ENOENT;
				cfg->result.resource[tmp + 8].flags = isapnp_mem_resource_flags(mem);
			}
		}
	}
	return 0;
}

static int isapnp_check_port(struct isapnp_cfgtmp *cfg, int port, int size, int idx)
{
	int i, tmp, rport, rsize;
	struct isapnp_port *xport;
	struct pci_dev *dev;

	if (check_region(port, size))
		return 1;
	for (i = 0; i < 8; i++) {
		rport = isapnp_reserve_io[i << 1];
		rsize = isapnp_reserve_io[(i << 1) + 1];
		if (port >= rport && port < rport + rsize)
			return 1;
		if (port + size > rport && port + size < (rport + rsize) - 1)
			return 1;
	}
	for (dev = isapnp_devices; dev; dev = dev->next) {
		if (dev->active) {
			for (tmp = 0; tmp < 8; tmp++) {
				if (dev->resource[tmp].flags) {
					rport = dev->resource[tmp].start;
					rsize = (dev->resource[tmp].end - rport) + 1;
					if (port >= rport && port < rport + rsize)
						return 1;
					if (port + size > rport && port + size < (rport + rsize) - 1)
						return 1;
				}
			}
		}
	}
	for (i = 0; i < 8; i++) {
		unsigned int flags;
		if (i == idx)
			continue;
		flags = cfg->request->resource[i].flags;
		if (!flags)
			continue;
		tmp = cfg->request->resource[i].start;
		if (flags & IORESOURCE_AUTO) {		/* auto */
			xport = cfg->port[i];
			if (!xport)
				return 1;
			if (cfg->result.resource[i].flags & IORESOURCE_AUTO)
				continue;
			tmp = cfg->result.resource[i].start;
			if (tmp + xport->size >= port && tmp <= port + xport->size)
				return 1;
			continue;
		}
		if (port == tmp)
			return 1;
		xport = isapnp_find_port(cfg->request, i);
		if (!xport)
			return 1;
		if (tmp + xport->size >= port && tmp <= port + xport->size)
			return 1;
	}
	return 0;
}

static int isapnp_valid_port(struct isapnp_cfgtmp *cfg, int idx)
{
	int err;
	unsigned long *value1, *value2;
	struct isapnp_port *port;

	if (!cfg || idx < 0 || idx > 7)
		return -EINVAL;
	if (!(cfg->result.resource[idx].flags & IORESOURCE_AUTO)) /* don't touch */
		return 0;
      __again:
      	port = cfg->port[idx];
      	if (!port)
      		return -EINVAL;
      	value1 = &cfg->result.resource[idx].start;
      	value2 = &cfg->result.resource[idx].end;
	if (cfg->result.resource[idx].flags & IORESOURCE_AUTO) {
		cfg->result.resource[idx].flags &= ~IORESOURCE_AUTO;
		*value1 = port->min;
		*value2 = port->min + port->size - 1;
		if (!isapnp_check_port(cfg, *value1, port->size, idx))
			return 0;
	}
	do {
		*value1 += port->align;
		*value2 = *value1 + port->size - 1;
		if (*value1 > port->max || !port->align) {
			if (port->res && port->res->alt) {
				if ((err = isapnp_alternative_switch(cfg, port->res, port->res->alt))<0)
					return err;
				goto __again;
			}
			return -ENOENT;
		}
	} while (isapnp_check_port(cfg, *value1, port->size, idx));
	return 0;
}

static void isapnp_test_handler(int irq, void *dev_id, struct pt_regs *regs)
{
}

static int isapnp_check_interrupt(struct isapnp_cfgtmp *cfg, int irq, int idx)
{
	int i;
	struct pci_dev *dev;

	if (irq < 0 || irq > 15)
		return 1;
	for (i = 0; i < 16; i++) {
		if (isapnp_reserve_irq[i] == irq)
			return 1;
	}
	for (dev = isapnp_devices; dev; dev = dev->next) {
		if (dev->active) {
			if (dev->irq_resource[0].start == irq ||
			    dev->irq_resource[1].start == irq)
				return 1;
		}
	}
	if (request_irq(irq, isapnp_test_handler, SA_INTERRUPT, "isapnp", NULL))
		return 1;
	free_irq(irq, NULL);
	for (i = 0; i < DEVICE_COUNT_IRQ; i++) {
		if (i == idx)
			continue;
		if (!cfg->result.irq_resource[i].flags)
			continue;
		if (cfg->result.irq_resource[i].flags & IORESOURCE_AUTO)
			continue;
		if (cfg->result.irq_resource[i].start == irq)
			return 1;
	}
	return 0;
}

static int isapnp_valid_irq(struct isapnp_cfgtmp *cfg, int idx)
{
	/* IRQ priority: this table is good for i386 */
	static unsigned short xtab[16] = {
		5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
	};
	int err, i;
	unsigned long *value1, *value2;
	struct isapnp_irq *irq;

	if (!cfg || idx < 0 || idx > 1)
		return -EINVAL;
	if (!(cfg->result.irq_resource[idx].flags & IORESOURCE_AUTO))
		return 0;
      __again:
      	irq = cfg->irq[idx];
      	if (!irq)
      		return -EINVAL;
      	value1 = &cfg->result.irq_resource[idx].start;
      	value2 = &cfg->result.irq_resource[idx].end;
	if (cfg->result.irq_resource[idx].flags & IORESOURCE_AUTO) {
		for (i = 0; i < 16 && !(irq->map & (1<<xtab[i])); i++);
		if (i >= 16)
			return -ENOENT;
		cfg->result.irq_resource[idx].flags &= ~IORESOURCE_AUTO;
		if (!isapnp_check_interrupt(cfg, *value1 = *value2 = xtab[i], idx))
			return 0;
	}
	do {
		for (i = 0; i < 16 && xtab[i] != *value1; i++);
		for (i++; i < 16 && !(irq->map & (1<<xtab[i])); i++);
		if (i >= 16) {
			if (irq->res && irq->res->alt) {
				if ((err = isapnp_alternative_switch(cfg, irq->res, irq->res->alt))<0)
					return err;
				goto __again;
			}
			return -ENOENT;
		} else {
			*value1 = *value2 = xtab[i];
		}
	} while (isapnp_check_interrupt(cfg, *value1, idx));
	return 0;
}

static int isapnp_check_dma(struct isapnp_cfgtmp *cfg, int dma, int idx)
{
	int i;
	struct pci_dev *dev;

	if (dma < 0 || dma == 4 || dma > 7)
		return 1;
	for (i = 0; i < 8; i++) {
		if (isapnp_reserve_dma[i] == dma)
			return 1;
	}
	for (dev = isapnp_devices; dev; dev = dev->next) {
		if (dev->active) {
			if (dev->dma_resource[0].start == dma || dev->dma_resource[1].start == dma)
				return 1;
		}
	}
	if (request_dma(dma, "isapnp"))
		return 1;
	free_dma(dma);
	for (i = 0; i < 2; i++) {
		if (i == idx)
			continue;
		if (!cfg->result.dma_resource[i].flags ||
		    (cfg->result.dma_resource[i].flags & IORESOURCE_AUTO))
			continue;
		if (cfg->result.dma_resource[i].start == dma)
			return 1;
	}
	return 0;
}

static int isapnp_valid_dma(struct isapnp_cfgtmp *cfg, int idx)
{
	int err, i;
	unsigned long *value1, *value2;
	struct isapnp_dma *dma;

	if (!cfg || idx < 0 || idx > 1)
		return -EINVAL;
	if (!(cfg->result.dma_resource[idx].flags & IORESOURCE_AUTO))	/* don't touch */
		return 0;
      __again:
      	dma = cfg->dma[idx];
      	if (!dma)
      		return -EINVAL;
      	value1 = &cfg->result.dma_resource[idx].start;
      	value2 = &cfg->result.dma_resource[idx].end;
	if (cfg->result.dma_resource[idx].flags & IORESOURCE_AUTO) {
		for (i = 0; i < 8 && !(dma->map & (1<<i)); i++);
		if (i >= 8)
			return -ENOENT;
		cfg->result.dma_resource[idx].flags &= ~IORESOURCE_AUTO;
		if (!isapnp_check_dma(cfg, *value1 = *value2 = i, idx))
			return 0;
	}
	do {
		for (i = *value1 + 1; i < 8 && !(dma->map & (1<<i)); i++);
		if (i >= 8) {
			if (dma->res && dma->res->alt) {
				if ((err = isapnp_alternative_switch(cfg, dma->res, dma->res->alt))<0)
					return err;
				goto __again;
			}
			return -ENOENT;
		} else {
			*value1 = *value2 = i;
		}
	} while (isapnp_check_dma(cfg, *value1, idx));
	return 0;
}

static int isapnp_check_mem(struct isapnp_cfgtmp *cfg, unsigned int addr, unsigned int size, int idx)
{
	int i, tmp;
	unsigned int raddr, rsize;
	struct pci_dev *dev;
	struct isapnp_mem *xmem;

	for (i = 0; i < 8; i++) {
		raddr = (unsigned int)isapnp_reserve_mem[i << 1];
		rsize = (unsigned int)isapnp_reserve_mem[(i << 1) + 1];
		if (addr >= raddr && addr < raddr + rsize)
			return 1;
		if (addr + size > raddr && addr + size < (raddr + rsize) - 1)
			return 1;
		if (__check_region(&iomem_resource, addr, size))
			return 1;
	}
	for (dev = isapnp_devices; dev; dev = dev->next) {
		if (dev->active) {
			for (tmp = 0; tmp < 4; tmp++) {
				if (dev->resource[tmp].flags) {
					raddr = dev->resource[tmp + 8].start;
					rsize = (dev->resource[tmp + 8].end - raddr) + 1;
					if (addr >= raddr && addr < raddr + rsize)
						return 1;
					if (addr + size > raddr && addr + size < (raddr + rsize) - 1)
						return 1;
				}
			}
		}
	}
	for (i = 0; i < 4; i++) {
		unsigned int flags = cfg->request->resource[i + 8].flags;
		if (i == idx)
			continue;
		if (!flags)
			continue;
		tmp = cfg->result.resource[i + 8].start;
		if (flags & IORESOURCE_AUTO) {		/* auto */
			xmem = cfg->mem[i];
			if (!xmem)
				return 1;
			if (cfg->result.resource[i + 8].flags & IORESOURCE_AUTO)
				continue;
			if (tmp + xmem->size >= addr && tmp <= addr + xmem->size)
				return 1;
			continue;
		}
		if (addr == tmp)
			return 1;
		xmem = isapnp_find_mem(cfg->request, i);
		if (!xmem)
			return 1;
		if (tmp + xmem->size >= addr && tmp <= addr + xmem->size)
			return 1;
	}
	return 0;
}

static int isapnp_valid_mem(struct isapnp_cfgtmp *cfg, int idx)
{
	int err;
	unsigned long *value1, *value2;
	struct isapnp_mem *mem;

	if (!cfg || idx < 0 || idx > 3)
		return -EINVAL;
	if (!(cfg->result.resource[idx + 8].flags & IORESOURCE_AUTO)) /* don't touch */
		return 0;
      __again:
      	mem = cfg->mem[idx];
      	if (!mem)
      		return -EINVAL;
      	value1 = &cfg->result.resource[idx].start;
      	value2 = &cfg->result.resource[idx].end;
	if (cfg->result.resource[idx + 8].flags & IORESOURCE_AUTO) {
		cfg->result.resource[idx + 8].flags &= ~IORESOURCE_AUTO;
		*value1 = mem->min;
		*value2 = mem->min + mem->size - 1;
		if (!isapnp_check_mem(cfg, *value1, mem->size, idx))
			return 0;
	}
	do {
		*value1 += mem->align;
		*value2 = *value1 + mem->size - 1;
		if (*value1 >= 8 || !mem->align) {
			if (mem->res && mem->res->alt) {
				if ((err = isapnp_alternative_switch(cfg, mem->res, mem->res->alt))<0)
					return err;
				goto __again;
			}
			return -ENOENT;
		}
	} while (isapnp_check_mem(cfg, *value1, mem->size, idx));
	return 0;
}

static int isapnp_check_valid(struct isapnp_cfgtmp *cfg)
{
	int tmp;
	
	for (tmp = 0; tmp < 8; tmp++)
		if (cfg->result.resource[tmp].flags & IORESOURCE_AUTO)
			return -EAGAIN;
	for (tmp = 0; tmp < 2; tmp++)
		if (cfg->result.irq_resource[tmp].flags & IORESOURCE_AUTO)
			return -EAGAIN;
	for (tmp = 0; tmp < 2; tmp++)
		if (cfg->result.dma_resource[tmp].flags & IORESOURCE_AUTO)
			return -EAGAIN;
	for (tmp = 0; tmp < 4; tmp++)
		if (cfg->result.resource[tmp + 8].flags & IORESOURCE_AUTO)
			return -EAGAIN;
	return 0;
}

static int isapnp_config_activate(struct pci_dev *dev)
{
	struct isapnp_cfgtmp cfg;
	int tmp, fauto, err;
	
	if (!dev)
		return -EINVAL;
	if (dev->active)
		return -EBUSY;
	memset(&cfg, 0, sizeof(cfg));
	cfg.request = dev;
	memcpy(&cfg.result, dev, sizeof(struct pci_dev));
	/* check if all values are set, otherwise try auto-configuration */
	for (tmp = fauto = 0; !fauto && tmp < 8; tmp++) {
		if (dev->resource[tmp].flags & IORESOURCE_AUTO)
			fauto++;
	}
	for (tmp = 0; !fauto && tmp < 2; tmp++) {
		if (dev->irq_resource[tmp].flags & IORESOURCE_AUTO)
			fauto++;
	}
	for (tmp = 0; !fauto && tmp < 2; tmp++) {
		if (dev->dma_resource[tmp].flags & IORESOURCE_AUTO)
			fauto++;
	}
	for (tmp = 0; !fauto && tmp < 4; tmp++) {
		if (dev->resource[tmp + 8].flags & IORESOURCE_AUTO)
			fauto++;
	}
	if (!fauto)
		goto __skip_auto;
	/* set variables to initial values */
	if ((err = isapnp_alternative_switch(&cfg, NULL, NULL))<0)
		return err;
	/* find first valid configuration */
	fauto = 0;
	do {
		for (tmp = 0; tmp < 8 && cfg.result.resource[tmp].flags; tmp++)
			if ((err = isapnp_valid_port(&cfg, tmp))<0)
				return err;
		for (tmp = 0; tmp < 2 && cfg.result.irq_resource[tmp].flags; tmp++)
			if ((err = isapnp_valid_irq(&cfg, tmp))<0)
				return err;
		for (tmp = 0; tmp < 2 && cfg.result.dma_resource[tmp].flags; tmp++)
			if ((err = isapnp_valid_dma(&cfg, tmp))<0)
				return err;
		for (tmp = 0; tmp < 4 && cfg.result.resource[tmp + 8].flags; tmp++)
			if ((err = isapnp_valid_mem(&cfg, tmp))<0)
				return err;
	} while (isapnp_check_valid(&cfg)<0 && fauto++ < 20);
	if (fauto >= 20)
		return -EAGAIN;
      __skip_auto:
      	/* we have valid configuration, try configure hardware */
      	isapnp_cfg_begin(dev->bus->number, dev->devfn);
	dev->active = 1;
	dev->irq_resource[0] = cfg.result.irq_resource[0];
	dev->irq_resource[1] = cfg.result.irq_resource[1];
	dev->dma_resource[0] = cfg.result.dma_resource[0];
	dev->dma_resource[1] = cfg.result.dma_resource[1];
	for (tmp = 0; tmp < 12; tmp++) {
		dev->resource[tmp] = cfg.result.resource[tmp];
	}	
	for (tmp = 0; tmp < 8 && dev->resource[tmp].flags; tmp++)
		isapnp_write_word(ISAPNP_CFG_PORT+(tmp<<1), dev->resource[tmp].start);
	for (tmp = 0; tmp < 2 && dev->irq_resource[tmp].flags; tmp++) {
		int irq = dev->irq_resource[tmp].start;
		if (irq == 2)
			irq = 9;
		isapnp_write_byte(ISAPNP_CFG_IRQ+(tmp<<1), irq);
	}
	for (tmp = 0; tmp < 2 && dev->dma_resource[tmp].flags; tmp++)
		isapnp_write_byte(ISAPNP_CFG_DMA+tmp, dev->dma_resource[tmp].start);
	for (tmp = 0; tmp < 4 && dev->resource[tmp+8].flags; tmp++)
		isapnp_write_word(ISAPNP_CFG_MEM+(tmp<<2), (dev->resource[tmp + 8].start >> 8) & 0xffff);
	isapnp_activate(dev->devfn);
	isapnp_cfg_end();
	return 0;
}

static int isapnp_config_deactivate(struct pci_dev *dev)
{
	if (!dev || !dev->active)
		return -EINVAL;
      	isapnp_cfg_begin(dev->bus->number, dev->devfn);
	isapnp_deactivate(dev->devfn);
	dev->active = 0;
	isapnp_cfg_end();
	return 0;
}

void isapnp_resource_change(struct resource *resource,
			    unsigned long start,
			    unsigned long size)
{
	if (resource == NULL)
		return;
	resource->flags &= ~IORESOURCE_AUTO;
	resource->start = start;
	resource->end = start + size - 1;
}

/*
 *  Inititialization.
 */

#ifdef MODULE

static void isapnp_free_port(struct isapnp_port *port)
{
	struct isapnp_port *next;

	while (port) {
		next = port->next;
		kfree(port);
		port = next;
	}
}

static void isapnp_free_irq(struct isapnp_irq *irq)
{
	struct isapnp_irq *next;

	while (irq) {
		next = irq->next;
		kfree(irq);
		irq = next;
	}
}

static void isapnp_free_dma(struct isapnp_dma *dma)
{
	struct isapnp_dma *next;

	while (dma) {
		next = dma->next;
		kfree(dma);
		dma = next;
	}
}

static void isapnp_free_mem(struct isapnp_mem *mem)
{
	struct isapnp_mem *next;

	while (mem) {
		next = mem->next;
		kfree(mem);
		mem = next;
	}
}

static void isapnp_free_mem32(struct isapnp_mem32 *mem32)
{
	struct isapnp_mem32 *next;

	while (mem32) {
		next = mem32->next;
		kfree(mem32);
		mem32 = next;
	}
}

static void isapnp_free_resources(struct isapnp_resources *resources, int alt)
{
	struct isapnp_resources *next;

	while (resources) {
		next = alt ? resources->alt : resources->next;
		isapnp_free_port(resources->port);
		isapnp_free_irq(resources->irq);
		isapnp_free_dma(resources->dma);
		isapnp_free_mem(resources->mem);
		isapnp_free_mem32(resources->mem32);
		if (!alt && resources->alt)
			isapnp_free_resources(resources->alt, 1);
		kfree(resources);
		resources = next;
	}
}

static void isapnp_free_device(struct pci_dev *dev)
{
	struct pci_dev *next;

	while (dev) {
		next = dev->sibling;
		isapnp_free_resources((struct isapnp_resources *)dev->sysdata, 0);
		kfree(dev);
		dev = next;
	}
}

#endif /* MODULE */

static void isapnp_free_all_resources(void)
{
#ifdef MODULE
	struct pci_bus *card, *cardnext;
#endif

#ifdef ISAPNP_REGION_OK
	release_resource(pidxr_res);
#endif
	release_resource(pnpwrp_res);
	if (isapnp_rdp >= 0x203 && isapnp_rdp <= 0x3ff)
		release_resource(isapnp_rdp_res);
#ifdef MODULE
	for (card = isapnp_cards; card; card = cardnext) {
		cardnext = card->next;
		isapnp_free_device(card->devices);
		kfree(card);
	}
#ifdef CONFIG_PROC_FS
	isapnp_proc_done();
#endif
#endif
}

static int __init isapnp_do_reserve_irq(int irq)
{
	int i;
	
	if (irq < 0 || irq > 15)
		return -EINVAL;
	for (i = 0; i < 16; i++) {
		if (isapnp_reserve_irq[i] == irq)
			return 0;
	}
	for (i = 0; i < 16; i++) {
		if (isapnp_reserve_irq[i] < 0) {
			isapnp_reserve_irq[i] = irq;
#ifdef ISAPNP_DEBUG
			printk("IRQ %i is reserved now.\n", irq);
#endif
			return 0;
		}
	}
	return -ENOMEM;
}

#ifdef CONFIG_PCI

static void __init isapnp_pci_init(void)
{
	int devfn;
	struct pci_dev *dev;
	
	for (devfn = 0; devfn < 255; devfn++) {
		dev = pci_find_slot(0, devfn);
		if (dev != NULL)
			break;
	}
	if (dev == NULL)
		return;
	while (dev) {
#ifdef ISAPNP_DEBUG
		printk("PCI: reserved IRQ: %i\n", dev->irq);
#endif
		if (dev->irq > 0)
			isapnp_do_reserve_irq(dev->irq);
		dev = dev->next;
	}
}

#endif /* CONFIG_PCI */

EXPORT_SYMBOL(isapnp_present);
EXPORT_SYMBOL(isapnp_cfg_begin);
EXPORT_SYMBOL(isapnp_cfg_end);
EXPORT_SYMBOL(isapnp_read_byte);
EXPORT_SYMBOL(isapnp_read_word);
EXPORT_SYMBOL(isapnp_read_dword);
EXPORT_SYMBOL(isapnp_write_byte);
EXPORT_SYMBOL(isapnp_write_word);
EXPORT_SYMBOL(isapnp_write_dword);
EXPORT_SYMBOL(isapnp_wake);
EXPORT_SYMBOL(isapnp_device);
EXPORT_SYMBOL(isapnp_activate);
EXPORT_SYMBOL(isapnp_deactivate);
EXPORT_SYMBOL(isapnp_find_card);
EXPORT_SYMBOL(isapnp_find_dev);
EXPORT_SYMBOL(isapnp_resource_change);

int __init isapnp_init(void)
{
	int cards;
	struct pci_bus *card;
	struct pci_dev *dev;

	if (isapnp_disable) {
		isapnp_detected = 0;
		printk("isapnp: ISA Plug & Play support disabled\n");
		return 0;
	}
#ifdef ISAPNP_REGION_OK
	pidxr_res=request_region(_PIDXR, 1, "isapnp index");
	if(!pidxr_res) {
		printk("isapnp: Index Register 0x%x already used\n", _PIDXR);
		return -EBUSY;
	}
#endif
	pnpwrp_res=request_region(_PNPWRP, 1, "isapnp write");
	if(!pnpwrp_res) {
		printk("isapnp: Write Data Register 0x%x already used\n", _PNPWRP);
		return -EBUSY;
	}
	
	/*
	 *	Print a message. The existing ISAPnP code is hanging machines
	 *	so let the user know where.
	 */
	 
	printk("isapnp: Scanning for Pnp cards...\n");
	if (isapnp_rdp >= 0x203 && isapnp_rdp <= 0x3ff) {
		isapnp_rdp |= 3;
		isapnp_rdp_res=request_region(isapnp_rdp, 1, "isapnp read");
		if(!isapnp_rdp_res) {
			printk("isapnp: Read Data Register 0x%x already used\n", isapnp_rdp);
			return -EBUSY;
		}
		isapnp_set_rdp();
	}
	isapnp_detected = 1;
	if (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff) {
		cards = isapnp_isolate();
		if (cards < 0 || 
		    (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff)) {
			isapnp_free_all_resources();
			isapnp_detected = 0;
			printk("isapnp: No Plug & Play device found\n");
			return 0;
		}
		isapnp_rdp_res=request_region(isapnp_rdp, 1, "isapnp read");
	}
	isapnp_build_device_list();
	cards = 0;
	for (card = isapnp_cards; card; card = card->next)
		cards++;
	if (isapnp_verbose) {
		for (card = isapnp_cards; card; card = card->next) {
			printk( "isapnp: Card '%s'\n", card->name[0]?card->name:"Unknown");
			if (isapnp_verbose < 2)
				continue;
			for (dev = card->devices; dev; dev = dev->next)
				printk("isapnp:   Device '%s'\n", dev->name[0]?card->name:"Unknown");
		}
	}
	if (cards) {
		printk("isapnp: %i Plug & Play card%s detected total\n", cards, cards>1?"s":"");
	} else {
		printk("isapnp: No Plug & Play card found\n");
	}
#ifdef CONFIG_PCI
	if (!isapnp_skip_pci_scan)
		isapnp_pci_init();
#endif
#ifdef CONFIG_PROC_FS
	isapnp_proc_init();
#endif
	return 0;
}

#ifdef MODULE

int init_module(void)
{
	return isapnp_init();
}

void cleanup_module(void)
{
	if (isapnp_detected)
		isapnp_free_all_resources();
}

#endif