2 * ISA Plug & Play support
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * 2000-01-01 Added quirks handling for buggy hardware
22 * Peter Denison <peterd@pnd-pc.demon.co.uk>
25 #include <linux/config.h>
26 #include <linux/version.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/errno.h>
30 #include <linux/ioport.h>
31 #include <linux/string.h>
32 #include <linux/malloc.h>
33 #include <linux/delay.h>
37 #include <linux/pci.h>
38 #include <linux/init.h>
39 #include <linux/isapnp.h>
41 LIST_HEAD(isapnp_cards
);
42 LIST_HEAD(isapnp_devices
);
45 #define ISAPNP_REGION_OK
51 struct resource
*pidxr_res
;
52 struct resource
*pnpwrp_res
;
53 struct resource
*isapnp_rdp_res
;
55 int isapnp_disable
; /* Disable ISA PnP */
56 int isapnp_rdp
; /* Read Data Port */
57 int isapnp_reset
= 1; /* reset all PnP cards (deactivate) */
58 int isapnp_skip_pci_scan
; /* skip PCI resource scanning */
59 int isapnp_verbose
= 1; /* verbose mode */
60 int isapnp_reserve_irq
[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some IRQ */
61 int isapnp_reserve_dma
[8] = { [0 ... 7] = -1 }; /* reserve (don't use) some DMA */
62 int isapnp_reserve_io
[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some I/O region */
63 int isapnp_reserve_mem
[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some memory region */
65 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
66 MODULE_DESCRIPTION("Generic ISA Plug & Play support");
67 MODULE_PARM(isapnp_disable
, "i");
68 MODULE_PARM_DESC(isapnp_disable
, "ISA Plug & Play disable");
69 MODULE_PARM(isapnp_rdp
, "i");
70 MODULE_PARM_DESC(isapnp_rdp
, "ISA Plug & Play read data port");
71 MODULE_PARM(isapnp_reset
, "i");
72 MODULE_PARM_DESC(isapnp_reset
, "ISA Plug & Play reset all cards");
73 MODULE_PARM(isapnp_skip_pci_scan
, "i");
74 MODULE_PARM_DESC(isapnp_skip_pci_scan
, "ISA Plug & Play skip PCI resource scanning");
75 MODULE_PARM(isapnp_verbose
, "i");
76 MODULE_PARM_DESC(isapnp_verbose
, "ISA Plug & Play verbose mode");
77 MODULE_PARM(isapnp_reserve_irq
, "1-16i");
78 MODULE_PARM_DESC(isapnp_reserve_irq
, "ISA Plug & Play - reserve IRQ line(s)");
79 MODULE_PARM(isapnp_reserve_dma
, "1-8i");
80 MODULE_PARM_DESC(isapnp_reserve_dma
, "ISA Plug & Play - reserve DMA channel(s)");
81 MODULE_PARM(isapnp_reserve_io
, "1-16i");
82 MODULE_PARM_DESC(isapnp_reserve_io
, "ISA Plug & Play - reserve I/O region(s) - port,size");
83 MODULE_PARM(isapnp_reserve_mem
, "1-16i");
84 MODULE_PARM_DESC(isapnp_reserve_mem
, "ISA Plug & Play - reserve memory region(s) - address,size");
90 #define _STAG_PNPVERNO 0x01
91 #define _STAG_LOGDEVID 0x02
92 #define _STAG_COMPATDEVID 0x03
93 #define _STAG_IRQ 0x04
94 #define _STAG_DMA 0x05
95 #define _STAG_STARTDEP 0x06
96 #define _STAG_ENDDEP 0x07
97 #define _STAG_IOPORT 0x08
98 #define _STAG_FIXEDIO 0x09
99 #define _STAG_VENDOR 0x0e
100 #define _STAG_END 0x0f
102 #define _LTAG_MEMRANGE 0x81
103 #define _LTAG_ANSISTR 0x82
104 #define _LTAG_UNICODESTR 0x83
105 #define _LTAG_VENDOR 0x84
106 #define _LTAG_MEM32RANGE 0x85
107 #define _LTAG_FIXEDMEM32RANGE 0x86
109 static unsigned char isapnp_checksum_value
;
110 static DECLARE_MUTEX(isapnp_cfg_mutex
);
111 static int isapnp_detected
;
113 /* some prototypes */
115 static int isapnp_config_prepare(struct pci_dev
*dev
);
116 static int isapnp_config_activate(struct pci_dev
*dev
);
117 static int isapnp_config_deactivate(struct pci_dev
*dev
);
119 static inline void write_data(unsigned char x
)
124 static inline void write_address(unsigned char x
)
130 static inline unsigned char read_data(void)
132 unsigned char val
= inb(isapnp_rdp
);
136 unsigned char isapnp_read_byte(unsigned char idx
)
142 unsigned short isapnp_read_word(unsigned char idx
)
146 val
= isapnp_read_byte(idx
);
147 val
= (val
<< 8) + isapnp_read_byte(idx
+1);
151 unsigned int isapnp_read_dword(unsigned char idx
)
155 val
= isapnp_read_byte(idx
);
156 val
= (val
<< 8) + isapnp_read_byte(idx
+1);
157 val
= (val
<< 8) + isapnp_read_byte(idx
+2);
158 val
= (val
<< 8) + isapnp_read_byte(idx
+3);
162 void isapnp_write_byte(unsigned char idx
, unsigned char val
)
168 void isapnp_write_word(unsigned char idx
, unsigned short val
)
170 isapnp_write_byte(idx
, val
>> 8);
171 isapnp_write_byte(idx
+1, val
);
174 void isapnp_write_dword(unsigned char idx
, unsigned int val
)
176 isapnp_write_byte(idx
, val
>> 24);
177 isapnp_write_byte(idx
+1, val
>> 16);
178 isapnp_write_byte(idx
+2, val
>> 8);
179 isapnp_write_byte(idx
+3, val
);
182 void *isapnp_alloc(long size
)
186 result
= kmalloc(size
, GFP_KERNEL
);
189 memset(result
, 0, size
);
193 static void isapnp_key(void)
195 unsigned char code
= 0x6a, msb
;
204 for (i
= 1; i
< 32; i
++) {
205 msb
= ((code
& 0x01) ^ ((code
& 0x02) >> 1)) << 7;
206 code
= (code
>> 1) | msb
;
211 /* place all pnp cards in wait-for-key state */
212 static void isapnp_wait(void)
214 isapnp_write_byte(0x02, 0x02);
217 void isapnp_wake(unsigned char csn
)
219 isapnp_write_byte(0x03, csn
);
222 void isapnp_device(unsigned char logdev
)
224 isapnp_write_byte(0x07, logdev
);
227 void isapnp_activate(unsigned char logdev
)
229 isapnp_device(logdev
);
230 isapnp_write_byte(ISAPNP_CFG_ACTIVATE
, 1);
234 void isapnp_deactivate(unsigned char logdev
)
236 isapnp_device(logdev
);
237 isapnp_write_byte(ISAPNP_CFG_ACTIVATE
, 0);
241 static void __init
isapnp_peek(unsigned char *data
, int bytes
)
246 for (i
= 1; i
<= bytes
; i
++) {
247 for (j
= 0; j
< 10; j
++) {
248 d
= isapnp_read_byte(0x05);
257 d
= isapnp_read_byte(0x04); /* PRESDI */
258 isapnp_checksum_value
+= d
;
264 #define RDP_STEP 32 /* minimum is 4 */
266 static int isapnp_next_rdp(void)
268 int rdp
= isapnp_rdp
;
269 while (rdp
<= 0x3ff) {
270 if (!check_region(rdp
, 1)) {
276 * We cannot use NE2000 probe spaces for ISAPnP or we
277 * will lock up machines.
279 if(rdp
>= 0x280 && rdp
<= 0x380)
285 /* Set read port address */
286 static inline void isapnp_set_rdp(void)
288 isapnp_write_byte(0x00, isapnp_rdp
>> 2);
293 * Perform an isolation. The port selection code now tries to avoid
294 * "dangerous to read" ports.
297 static int __init
isapnp_isolate_rdp_select(void)
302 /* Control: reset CSN and conditionally everything else too */
303 isapnp_write_byte(0x02, isapnp_reset
? 0x05 : 0x04);
310 if (isapnp_next_rdp() < 0) {
323 * Isolate (assign uniqued CSN) to all ISA PnP devices.
326 static int __init
isapnp_isolate(void)
328 unsigned char checksum
= 0x6a;
329 unsigned char chksum
= 0x00;
330 unsigned char bit
= 0x00;
337 if (isapnp_isolate_rdp_select() < 0)
341 for (i
= 1; i
<= 64; i
++) {
342 data
= read_data() << 8;
344 data
= data
| read_data();
348 checksum
= ((((checksum
^ (checksum
>> 1)) & 0x01) ^ bit
) << 7) | (checksum
>> 1);
351 for (i
= 65; i
<= 72; i
++) {
352 data
= read_data() << 8;
354 data
= data
| read_data();
357 chksum
|= (1 << (i
- 65));
359 if (checksum
!= 0x00 && checksum
== chksum
) {
362 isapnp_write_byte(0x06, csn
);
372 if (iteration
== 1) {
373 isapnp_rdp
+= RDP_STEP
;
374 if (isapnp_isolate_rdp_select() < 0)
376 } else if (iteration
> 1) {
389 * Read one tag from stream.
392 static int __init
isapnp_read_tag(unsigned char *type
, unsigned short *size
)
394 unsigned char tag
, tmp
[2];
396 isapnp_peek(&tag
, 1);
397 if (tag
== 0) /* invalid tag */
399 if (tag
& 0x80) { /* large item */
402 *size
= (tmp
[1] << 8) | tmp
[0];
404 *type
= (tag
>> 3) & 0x0f;
408 printk("tag = 0x%x, type = 0x%x, size = %i\n", tag
, *type
, *size
);
410 if (type
== 0) /* wrong type */
412 if (*type
== 0xff && *size
== 0xffff) /* probably invalid data */
418 * Skip specified number of bytes from stream.
421 static void __init
isapnp_skip_bytes(int count
)
423 isapnp_peek(NULL
, count
);
427 * Parse logical device tag.
430 static struct pci_dev
* __init
isapnp_parse_device(struct pci_bus
*card
, int size
, int number
)
432 unsigned char tmp
[6];
435 isapnp_peek(tmp
, size
);
436 dev
= isapnp_alloc(sizeof(struct pci_dev
));
439 dev
->dma_mask
= 0x00ffffff;
441 dev
->vendor
= (tmp
[1] << 8) | tmp
[0];
442 dev
->device
= (tmp
[3] << 8) | tmp
[2];
446 dev
->regs
|= tmp
[5] << 8;
447 dev
->prepare
= isapnp_config_prepare
;
448 dev
->activate
= isapnp_config_activate
;
449 dev
->deactivate
= isapnp_config_deactivate
;
454 * Build new resources structure
457 static struct isapnp_resources
* __init
isapnp_build_resources(struct pci_dev
*dev
, int dependent
)
459 struct isapnp_resources
*res
, *ptr
, *ptra
;
461 res
= isapnp_alloc(sizeof(struct isapnp_resources
));
465 ptr
= (struct isapnp_resources
*)dev
->sysdata
;
466 while (ptr
&& ptr
->next
)
468 if (ptr
&& ptr
->dependent
&& dependent
) { /* add to another list */
470 while (ptra
&& ptra
->alt
)
483 res
->priority
= dependent
& 0xff;
484 if (res
->priority
> ISAPNP_RES_PRIORITY_FUNCTIONAL
)
485 res
->priority
= ISAPNP_RES_PRIORITY_INVALID
;
488 res
->priority
= ISAPNP_RES_PRIORITY_PREFERRED
;
495 * Add IRQ resource to resources list.
498 static void __init
isapnp_add_irq_resource(struct pci_dev
*dev
,
499 struct isapnp_resources
**res
,
500 int dependent
, int size
)
502 unsigned char tmp
[3];
504 struct isapnp_irq
*irq
, *ptr
;
506 isapnp_peek(tmp
, size
);
507 irq
= isapnp_alloc(sizeof(struct isapnp_irq
));
511 *res
= isapnp_build_resources(dev
, dependent
);
517 irq
->map
= (tmp
[1] << 8) | tmp
[0];
521 irq
->flags
= IORESOURCE_IRQ_HIGHEDGE
;
524 while (ptr
&& ptr
->next
)
532 if (irq
->map
& (1<<i
))
533 pcibios_penalize_isa_irq(i
);
538 * Add DMA resource to resources list.
541 static void __init
isapnp_add_dma_resource(struct pci_dev
*dev
,
542 struct isapnp_resources
**res
,
543 int dependent
, int size
)
545 unsigned char tmp
[2];
546 struct isapnp_dma
*dma
, *ptr
;
548 isapnp_peek(tmp
, size
);
549 dma
= isapnp_alloc(sizeof(struct isapnp_dma
));
553 *res
= isapnp_build_resources(dev
, dependent
);
563 while (ptr
&& ptr
->next
)
572 * Add port resource to resources list.
575 static void __init
isapnp_add_port_resource(struct pci_dev
*dev
,
576 struct isapnp_resources
**res
,
577 int dependent
, int size
)
579 unsigned char tmp
[7];
580 struct isapnp_port
*port
, *ptr
;
582 isapnp_peek(tmp
, size
);
583 port
= isapnp_alloc(sizeof(struct isapnp_port
));
587 *res
= isapnp_build_resources(dev
, dependent
);
593 port
->min
= (tmp
[2] << 8) | tmp
[1];
594 port
->max
= (tmp
[4] << 8) | tmp
[3];
595 port
->align
= tmp
[5];
597 port
->flags
= tmp
[0] ? ISAPNP_PORT_FLAG_16BITADDR
: 0;
600 while (ptr
&& ptr
->next
)
609 * Add fixed port resource to resources list.
612 static void __init
isapnp_add_fixed_port_resource(struct pci_dev
*dev
,
613 struct isapnp_resources
**res
,
614 int dependent
, int size
)
616 unsigned char tmp
[3];
617 struct isapnp_port
*port
, *ptr
;
619 isapnp_peek(tmp
, size
);
620 port
= isapnp_alloc(sizeof(struct isapnp_port
));
624 *res
= isapnp_build_resources(dev
, dependent
);
630 port
->min
= port
->max
= (tmp
[1] << 8) | tmp
[0];
633 port
->flags
= ISAPNP_PORT_FLAG_FIXED
;
636 while (ptr
&& ptr
->next
)
645 * Add memory resource to resources list.
648 static void __init
isapnp_add_mem_resource(struct pci_dev
*dev
,
649 struct isapnp_resources
**res
,
650 int dependent
, int size
)
652 unsigned char tmp
[9];
653 struct isapnp_mem
*mem
, *ptr
;
655 isapnp_peek(tmp
, size
);
656 mem
= isapnp_alloc(sizeof(struct isapnp_mem
));
660 *res
= isapnp_build_resources(dev
, dependent
);
666 mem
->min
= ((tmp
[2] << 8) | tmp
[1]) << 8;
667 mem
->max
= ((tmp
[4] << 8) | tmp
[3]) << 8;
668 mem
->align
= (tmp
[6] << 8) | tmp
[5];
669 mem
->size
= ((tmp
[8] << 8) | tmp
[7]) << 8;
673 while (ptr
&& ptr
->next
)
682 * Add 32-bit memory resource to resources list.
685 static void __init
isapnp_add_mem32_resource(struct pci_dev
*dev
,
686 struct isapnp_resources
**res
,
687 int dependent
, int size
)
689 unsigned char tmp
[17];
690 struct isapnp_mem32
*mem32
, *ptr
;
692 isapnp_peek(tmp
, size
);
693 mem32
= isapnp_alloc(sizeof(struct isapnp_mem32
));
697 *res
= isapnp_build_resources(dev
, dependent
);
703 memcpy(mem32
->data
, tmp
, 17);
706 while (ptr
&& ptr
->next
)
711 (*res
)->mem32
= mem32
;
715 * Add 32-bit fixed memory resource to resources list.
718 static void __init
isapnp_add_fixed_mem32_resource(struct pci_dev
*dev
,
719 struct isapnp_resources
**res
,
720 int dependent
, int size
)
722 unsigned char tmp
[17];
723 struct isapnp_mem32
*mem32
, *ptr
;
725 isapnp_peek(tmp
, size
);
726 mem32
= isapnp_alloc(sizeof(struct isapnp_mem32
));
730 *res
= isapnp_build_resources(dev
, dependent
);
736 memcpy(mem32
->data
, tmp
, 17);
739 while (ptr
&& ptr
->next
)
744 (*res
)->mem32
= mem32
;
748 * Parse resource map for logical device.
751 static int __init
isapnp_create_device(struct pci_bus
*card
,
754 int number
= 0, skip
= 0, dependent
= 0, compat
= 0;
755 unsigned char type
, tmp
[17];
757 struct isapnp_resources
*res
= NULL
;
759 if ((dev
= isapnp_parse_device(card
, size
, number
++)) == NULL
)
761 list_add(&dev
->bus_list
, &card
->devices
);
762 list_add_tail(&dev
->global_list
, &isapnp_devices
);
764 if (isapnp_read_tag(&type
, &size
)<0)
766 if (skip
&& type
!= _STAG_LOGDEVID
&& type
!= _STAG_END
)
770 if (size
>= 5 && size
<= 6) {
771 isapnp_config_prepare(dev
);
772 if ((dev
= isapnp_parse_device(card
, size
, number
++)) == NULL
)
774 list_add_tail(&dev
->bus_list
, &card
->devices
);
775 list_add_tail(&dev
->global_list
, &isapnp_devices
);
785 case _STAG_COMPATDEVID
:
786 if (size
== 4 && compat
< DEVICE_COUNT_COMPATIBLE
) {
788 dev
->vendor_compatible
[compat
] = (tmp
[1] << 8) | tmp
[0];
789 dev
->device_compatible
[compat
] = (tmp
[3] << 8) | tmp
[2];
795 if (size
< 2 || size
> 3)
797 isapnp_add_irq_resource(dev
, &res
, dependent
, size
);
803 isapnp_add_dma_resource(dev
, &res
, dependent
, size
);
810 dependent
= 0x100 | ISAPNP_RES_PRIORITY_ACCEPTABLE
;
812 isapnp_peek(tmp
, size
);
813 dependent
= 0x100 | tmp
[0];
826 isapnp_add_port_resource(dev
, &res
, dependent
, size
);
832 isapnp_add_fixed_port_resource(dev
, &res
, dependent
, size
);
840 isapnp_add_mem_resource(dev
, &res
, dependent
, size
);
844 if (dev
->name
[0] == '\0') {
845 unsigned short size1
= size
> 47 ? 47 : size
;
846 isapnp_peek(dev
->name
, size1
);
847 dev
->name
[size1
] = '\0';
851 case _LTAG_UNICODESTR
:
852 /* silently ignore */
853 /* who use unicode for hardware identification? */
857 case _LTAG_MEM32RANGE
:
860 isapnp_add_mem32_resource(dev
, &res
, dependent
, size
);
863 case _LTAG_FIXEDMEM32RANGE
:
866 isapnp_add_fixed_mem32_resource(dev
, &res
, dependent
, size
);
871 isapnp_skip_bytes(size
);
874 printk("isapnp: unexpected or unknown tag type 0x%x for logical device %i (device %i), ignored\n", type
, dev
->devfn
, card
->number
);
878 isapnp_skip_bytes(size
);
880 isapnp_config_prepare(dev
);
885 * Parse resource map for ISA PnP card.
888 static void __init
isapnp_parse_resource_map(struct pci_bus
*card
)
890 unsigned char type
, tmp
[17];
894 if (isapnp_read_tag(&type
, &size
)<0)
901 card
->pnpver
= tmp
[0];
902 card
->productver
= tmp
[1];
906 if (size
>= 5 && size
<= 6) {
907 if (isapnp_create_device(card
, size
)==1)
915 if (card
->name
[0] == '\0') {
916 unsigned short size1
= size
> 47 ? 47 : size
;
917 isapnp_peek(card
->name
, size1
);
918 card
->name
[size1
] = '\0';
922 case _LTAG_UNICODESTR
:
923 /* silently ignore */
924 /* who use unicode for hardware identification? */
930 isapnp_skip_bytes(size
);
933 printk("isapnp: unexpected or unknown tag type 0x%x for device %i, ignored\n", type
, card
->number
);
937 isapnp_skip_bytes(size
);
942 * Compute ISA PnP checksum for first eight bytes.
945 static unsigned char __init
isapnp_checksum(unsigned char *data
)
948 unsigned char checksum
= 0x6a, bit
, b
;
950 for (i
= 0; i
< 8; i
++) {
952 for (j
= 0; j
< 8; j
++) {
956 checksum
= ((((checksum
^ (checksum
>> 1)) & 0x01) ^ bit
) << 7) | (checksum
>> 1);
963 * Build device list for all present ISA PnP devices.
966 static int __init
isapnp_build_device_list(void)
969 unsigned char header
[9], checksum
;
970 struct pci_bus
*card
;
975 for (csn
= 1; csn
<= 10; csn
++) {
977 isapnp_peek(header
, 9);
978 checksum
= isapnp_checksum(header
);
980 printk("vendor: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
981 header
[0], header
[1], header
[2], header
[3],
982 header
[4], header
[5], header
[6], header
[7], header
[8]);
983 printk("checksum = 0x%x\n", checksum
);
985 if (checksum
== 0x00 || checksum
!= header
[8]) /* not valid CSN */
987 if ((card
= isapnp_alloc(sizeof(struct pci_bus
))) == NULL
)
990 card
->vendor
= (header
[1] << 8) | header
[0];
991 card
->device
= (header
[3] << 8) | header
[2];
992 card
->serial
= (header
[7] << 24) | (header
[6] << 16) | (header
[5] << 8) | header
[4];
993 isapnp_checksum_value
= 0x00;
994 INIT_LIST_HEAD(&card
->children
);
995 INIT_LIST_HEAD(&card
->devices
);
996 isapnp_parse_resource_map(card
);
997 if (isapnp_checksum_value
!= 0x00)
998 printk("isapnp: checksum for device %i is not valid (0x%x)\n", csn
, isapnp_checksum_value
);
999 card
->checksum
= isapnp_checksum_value
;
1001 list_add_tail(&card
->node
, &isapnp_cards
);
1003 isapnp_for_each_dev(dev
) {
1004 isapnp_fixup_device(dev
);
1010 * Basic configuration routines.
1013 int isapnp_present(void)
1015 return !list_empty(&isapnp_devices
);
1018 int isapnp_cfg_begin(int csn
, int logdev
)
1020 if (csn
< 1 || csn
> 10 || logdev
> 10)
1023 down(&isapnp_cfg_mutex
);
1027 #if 1 /* to avoid malfunction when the isapnptools package is used */
1029 udelay(1000); /* delay 1000us */
1030 write_address(0x01);
1031 udelay(1000); /* delay 1000us */
1034 isapnp_device(logdev
);
1038 int isapnp_cfg_end(void)
1041 up(&isapnp_cfg_mutex
);
1050 static struct isapnp_port
*isapnp_find_port(struct pci_dev
*dev
, int index
)
1052 struct isapnp_resources
*res
;
1053 struct isapnp_port
*port
;
1055 if (!dev
|| index
< 0 || index
> 7)
1057 for (res
= (struct isapnp_resources
*)dev
->sysdata
; res
; res
= res
->next
) {
1058 for (port
= res
->port
; port
; port
= port
->next
) {
1067 struct isapnp_irq
*isapnp_find_irq(struct pci_dev
*dev
, int index
)
1069 struct isapnp_resources
*res
, *resa
;
1070 struct isapnp_irq
*irq
;
1071 int index1
, index2
, index3
;
1073 if (!dev
|| index
< 0 || index
> 7)
1075 for (res
= (struct isapnp_resources
*)dev
->sysdata
; res
; res
= res
->next
) {
1077 for (resa
= res
; resa
; resa
= resa
->alt
) {
1080 for (irq
= resa
->irq
; irq
; irq
= irq
->next
) {
1086 if (index3
< index2
)
1094 struct isapnp_dma
*isapnp_find_dma(struct pci_dev
*dev
, int index
)
1096 struct isapnp_resources
*res
;
1097 struct isapnp_dma
*dma
;
1099 if (!dev
|| index
< 0 || index
> 7)
1101 for (res
= (struct isapnp_resources
*)dev
->sysdata
; res
; res
= res
->next
) {
1102 for (dma
= res
->dma
; dma
; dma
= dma
->next
) {
1111 struct isapnp_mem
*isapnp_find_mem(struct pci_dev
*dev
, int index
)
1113 struct isapnp_resources
*res
;
1114 struct isapnp_mem
*mem
;
1116 if (!dev
|| index
< 0 || index
> 7)
1118 for (res
= (struct isapnp_resources
*)dev
->sysdata
; res
; res
= res
->next
) {
1119 for (mem
= res
->mem
; mem
; mem
= mem
->next
) {
1128 struct isapnp_mem32
*isapnp_find_mem32(struct pci_dev
*dev
, int index
)
1130 struct isapnp_resources
*res
;
1131 struct isapnp_mem32
*mem32
;
1133 if (!dev
|| index
< 0 || index
> 7)
1135 for (res
= (struct isapnp_resources
*)dev
->sysdata
; res
; res
= res
->next
) {
1136 for (mem32
= res
->mem32
; mem32
; mem32
= mem32
->next
) {
1149 struct pci_bus
*isapnp_find_card(unsigned short vendor
,
1150 unsigned short device
,
1151 struct pci_bus
*from
)
1153 struct list_head
*list
;
1155 list
= isapnp_cards
.next
;
1157 list
= from
->node
.next
;
1159 while (list
!= &isapnp_cards
) {
1160 struct pci_bus
*card
= pci_bus_b(list
);
1161 if (card
->vendor
== vendor
&& card
->device
== device
)
1168 struct pci_dev
*isapnp_find_dev(struct pci_bus
*card
,
1169 unsigned short vendor
,
1170 unsigned short function
,
1171 struct pci_dev
*from
)
1173 if (card
== NULL
) { /* look for a logical device from all cards */
1174 struct list_head
*list
;
1176 list
= isapnp_devices
.next
;
1178 list
= from
->global_list
.next
;
1180 while (list
!= &isapnp_devices
) {
1182 struct pci_dev
*dev
= pci_dev_g(list
);
1184 if (dev
->vendor
== vendor
&& dev
->device
== function
)
1186 for (idx
= 0; idx
< DEVICE_COUNT_COMPATIBLE
; idx
++)
1187 if (dev
->vendor_compatible
[idx
] == vendor
&&
1188 dev
->device_compatible
[idx
] == function
)
1193 struct list_head
*list
;
1195 list
= card
->devices
.next
;
1197 list
= from
->bus_list
.next
;
1198 if (from
->bus
!= card
) /* something is wrong */
1201 while (list
!= &card
->devices
) {
1203 struct pci_dev
*dev
= pci_dev_b(list
);
1205 if (dev
->vendor
== vendor
&& dev
->device
== function
)
1207 for (idx
= 0; idx
< DEVICE_COUNT_COMPATIBLE
; idx
++)
1208 if (dev
->vendor_compatible
[idx
] == vendor
&&
1209 dev
->device_compatible
[idx
] == function
)
1217 static const struct isapnp_card_id
*
1218 isapnp_match_card(const struct isapnp_card_id
*ids
, struct pci_bus
*card
)
1222 while (ids
->vendor
|| ids
->device
) {
1223 if ((ids
->vendor
== ISAPNP_ANY_ID
|| ids
->vendor
== card
->vendor
) &&
1224 (ids
->device
== ISAPNP_ANY_ID
|| ids
->device
== card
->device
)) {
1225 for (idx
= 0; idx
< ISAPNP_CARD_DEVS
; idx
++) {
1226 if (ids
->devs
[idx
].vendor
== 0 &&
1227 ids
->devs
[idx
].function
== 0)
1229 if (isapnp_find_dev(card
,
1230 ids
->devs
[idx
].vendor
,
1231 ids
->devs
[idx
].function
,
1243 int isapnp_probe_cards(const struct isapnp_card_id
*ids
,
1244 int (*probe
)(struct pci_bus
*_card
,
1245 const struct isapnp_card_id
*_id
))
1247 struct pci_bus
*card
;
1248 const struct isapnp_card_id
*id
;
1251 if (ids
== NULL
|| probe
== NULL
)
1253 isapnp_for_each_card(card
) {
1254 id
= isapnp_match_card(ids
, card
);
1255 if (id
!= NULL
&& probe(card
, id
) >= 0)
1261 static unsigned int isapnp_dma_resource_flags(struct isapnp_dma
*dma
)
1263 return dma
->flags
| IORESOURCE_DMA
| IORESOURCE_AUTO
;
1266 static unsigned int isapnp_mem_resource_flags(struct isapnp_mem
*mem
)
1268 unsigned int result
;
1270 result
= mem
->flags
| IORESOURCE_MEM
| IORESOURCE_AUTO
;
1271 if (!(mem
->flags
& IORESOURCE_MEM_WRITEABLE
))
1272 result
|= IORESOURCE_READONLY
;
1273 if (mem
->flags
& IORESOURCE_MEM_CACHEABLE
)
1274 result
|= IORESOURCE_CACHEABLE
;
1275 if (mem
->flags
& IORESOURCE_MEM_RANGELENGTH
)
1276 result
|= IORESOURCE_RANGELENGTH
;
1277 if (mem
->flags
& IORESOURCE_MEM_SHADOWABLE
)
1278 result
|= IORESOURCE_SHADOWABLE
;
1282 static unsigned int isapnp_irq_resource_flags(struct isapnp_irq
*irq
)
1284 return irq
->flags
| IORESOURCE_IRQ
| IORESOURCE_AUTO
;
1287 static unsigned int isapnp_port_resource_flags(struct isapnp_port
*port
)
1289 return port
->flags
| IORESOURCE_IO
| IORESOURCE_AUTO
;
1292 static int isapnp_config_prepare(struct pci_dev
*dev
)
1294 struct isapnp_resources
*res
, *resa
;
1295 struct isapnp_port
*port
;
1296 struct isapnp_irq
*irq
;
1297 struct isapnp_dma
*dma
;
1298 struct isapnp_mem
*mem
;
1299 int port_count
, port_count1
;
1300 int irq_count
, irq_count1
;
1301 int dma_count
, dma_count1
;
1302 int mem_count
, mem_count1
;
1307 if (dev
->active
|| dev
->ro
)
1309 for (idx
= 0; idx
< DEVICE_COUNT_IRQ
; idx
++) {
1310 dev
->irq_resource
[idx
].name
= NULL
;
1311 dev
->irq_resource
[idx
].start
= 0;
1312 dev
->irq_resource
[idx
].end
= 0;
1313 dev
->irq_resource
[idx
].flags
= 0;
1315 for (idx
= 0; idx
< DEVICE_COUNT_DMA
; idx
++) {
1316 dev
->dma_resource
[idx
].name
= NULL
;
1317 dev
->dma_resource
[idx
].start
= 0;
1318 dev
->dma_resource
[idx
].end
= 0;
1319 dev
->dma_resource
[idx
].flags
= 0;
1321 for (idx
= 0; idx
< DEVICE_COUNT_RESOURCE
; idx
++) {
1322 dev
->resource
[idx
].name
= NULL
;
1323 dev
->resource
[idx
].start
= 0;
1324 dev
->resource
[idx
].end
= 0;
1325 dev
->resource
[idx
].flags
= 0;
1327 port_count
= irq_count
= dma_count
= mem_count
= 0;
1328 for (res
= (struct isapnp_resources
*)dev
->sysdata
; res
; res
= res
->next
) {
1329 port_count1
= irq_count1
= dma_count1
= mem_count1
= 0;
1330 for (resa
= res
; resa
; resa
= resa
->alt
) {
1331 for (port
= resa
->port
, idx
= 0; port
; port
= port
->next
, idx
++) {
1332 if (dev
->resource
[port_count
+ idx
].flags
== 0) {
1333 dev
->resource
[port_count
+ idx
].flags
= isapnp_port_resource_flags(port
);
1334 dev
->resource
[port_count
+ idx
].end
= port
->size
;
1337 if (port_count1
< idx
)
1339 for (irq
= resa
->irq
, idx
= 0; irq
; irq
= irq
->next
, idx
++) {
1340 int count
= irq_count
+ idx
;
1341 if (count
< DEVICE_COUNT_IRQ
) {
1342 if (dev
->irq_resource
[count
].flags
== 0) {
1343 dev
->irq_resource
[count
].flags
= isapnp_irq_resource_flags(irq
);
1348 if (irq_count1
< idx
)
1350 for (dma
= resa
->dma
, idx
= 0; dma
; dma
= dma
->next
, idx
++)
1351 if (dev
->dma_resource
[idx
].flags
== 0) {
1352 dev
->dma_resource
[idx
].flags
= isapnp_dma_resource_flags(dma
);
1354 if (dma_count1
< idx
)
1356 for (mem
= resa
->mem
, idx
= 0; mem
; mem
= mem
->next
, idx
++)
1357 if (dev
->resource
[mem_count
+ idx
+ 8].flags
== 0) {
1358 dev
->resource
[mem_count
+ idx
+ 8].flags
= isapnp_mem_resource_flags(mem
);
1360 if (mem_count1
< idx
)
1363 port_count
+= port_count1
;
1364 irq_count
+= irq_count1
;
1365 dma_count
+= dma_count1
;
1366 mem_count
+= mem_count1
;
1371 struct isapnp_cfgtmp
{
1372 struct isapnp_port
*port
[8];
1373 struct isapnp_irq
*irq
[2];
1374 struct isapnp_dma
*dma
[2];
1375 struct isapnp_mem
*mem
[4];
1376 struct pci_dev
*request
;
1377 struct pci_dev result
;
1380 static int isapnp_alternative_switch(struct isapnp_cfgtmp
*cfg
,
1381 struct isapnp_resources
*from
,
1382 struct isapnp_resources
*to
)
1385 struct isapnp_port
*port
;
1386 struct isapnp_irq
*irq
;
1387 struct isapnp_dma
*dma
;
1388 struct isapnp_mem
*mem
;
1392 /* process port settings */
1393 for (tmp
= 0; tmp
< 8; tmp
++) {
1394 if (!(cfg
->request
->resource
[tmp
].flags
& IORESOURCE_AUTO
))
1395 continue; /* don't touch */
1396 port
= cfg
->port
[tmp
];
1398 cfg
->port
[tmp
] = port
= isapnp_find_port(cfg
->request
, tmp
);
1402 if (from
&& port
->res
== from
) {
1403 while (port
->res
!= to
) {
1404 if (!port
->res
->alt
)
1406 port
= port
->res
->alt
->port
;
1407 for (tmp1
= tmp
; tmp1
> 0 && port
; tmp1
--)
1409 cfg
->port
[tmp
] = port
;
1412 cfg
->result
.resource
[tmp
].flags
= isapnp_port_resource_flags(port
);
1416 /* process irq settings */
1417 for (tmp
= 0; tmp
< 2; tmp
++) {
1418 if (!(cfg
->request
->irq_resource
[tmp
].flags
& IORESOURCE_AUTO
))
1419 continue; /* don't touch */
1420 irq
= cfg
->irq
[tmp
];
1422 cfg
->irq
[tmp
] = irq
= isapnp_find_irq(cfg
->request
, tmp
);
1426 if (from
&& irq
->res
== from
) {
1427 while (irq
->res
!= to
) {
1430 irq
= irq
->res
->alt
->irq
;
1431 for (tmp1
= tmp
; tmp1
> 0 && irq
; tmp1
--)
1433 cfg
->irq
[tmp
] = irq
;
1436 cfg
->result
.irq_resource
[tmp
].flags
= isapnp_irq_resource_flags(irq
);
1440 /* process dma settings */
1441 for (tmp
= 0; tmp
< 2; tmp
++) {
1442 if (!(cfg
->request
->dma_resource
[tmp
].flags
& IORESOURCE_AUTO
))
1443 continue; /* don't touch */
1444 dma
= cfg
->dma
[tmp
];
1446 cfg
->dma
[tmp
] = dma
= isapnp_find_dma(cfg
->request
, tmp
);
1450 if (from
&& dma
->res
== from
) {
1451 while (dma
->res
!= to
) {
1454 dma
= dma
->res
->alt
->dma
;
1455 for (tmp1
= tmp
; tmp1
> 0 && dma
; tmp1
--)
1457 cfg
->dma
[tmp
] = dma
;
1460 cfg
->result
.dma_resource
[tmp
].flags
= isapnp_dma_resource_flags(dma
);
1464 /* process memory settings */
1465 for (tmp
= 0; tmp
< 4; tmp
++) {
1466 if (!(cfg
->request
->resource
[tmp
+ 8].flags
& IORESOURCE_AUTO
))
1467 continue; /* don't touch */
1468 mem
= cfg
->mem
[tmp
];
1470 cfg
->mem
[tmp
] = mem
= isapnp_find_mem(cfg
->request
, tmp
);
1474 if (from
&& mem
->res
== from
) {
1475 while (mem
->res
!= to
) {
1478 mem
= mem
->res
->alt
->mem
;
1479 for (tmp1
= tmp
; tmp1
> 0 && mem
; tmp1
--)
1481 cfg
->mem
[tmp
] = mem
;
1484 cfg
->result
.resource
[tmp
+ 8].flags
= isapnp_mem_resource_flags(mem
);
1491 static int isapnp_check_port(struct isapnp_cfgtmp
*cfg
, int port
, int size
, int idx
)
1493 int i
, tmp
, rport
, rsize
;
1494 struct isapnp_port
*xport
;
1495 struct pci_dev
*dev
;
1497 if (check_region(port
, size
))
1499 for (i
= 0; i
< 8; i
++) {
1500 rport
= isapnp_reserve_io
[i
<< 1];
1501 rsize
= isapnp_reserve_io
[(i
<< 1) + 1];
1502 if (port
>= rport
&& port
< rport
+ rsize
)
1504 if (port
+ size
> rport
&& port
+ size
< (rport
+ rsize
) - 1)
1508 isapnp_for_each_dev(dev
) {
1510 for (tmp
= 0; tmp
< 8; tmp
++) {
1511 if (dev
->resource
[tmp
].flags
) {
1512 rport
= dev
->resource
[tmp
].start
;
1513 rsize
= (dev
->resource
[tmp
].end
- rport
) + 1;
1514 if (port
>= rport
&& port
< rport
+ rsize
)
1516 if (port
+ size
> rport
&& port
+ size
< (rport
+ rsize
) - 1)
1522 for (i
= 0; i
< 8; i
++) {
1526 flags
= cfg
->request
->resource
[i
].flags
;
1529 tmp
= cfg
->request
->resource
[i
].start
;
1530 if (flags
& IORESOURCE_AUTO
) { /* auto */
1531 xport
= cfg
->port
[i
];
1534 if (cfg
->result
.resource
[i
].flags
& IORESOURCE_AUTO
)
1536 tmp
= cfg
->result
.resource
[i
].start
;
1537 if (tmp
+ xport
->size
>= port
&& tmp
<= port
+ xport
->size
)
1543 xport
= isapnp_find_port(cfg
->request
, i
);
1546 if (tmp
+ xport
->size
>= port
&& tmp
<= port
+ xport
->size
)
1552 static int isapnp_valid_port(struct isapnp_cfgtmp
*cfg
, int idx
)
1555 unsigned long *value1
, *value2
;
1556 struct isapnp_port
*port
;
1558 if (!cfg
|| idx
< 0 || idx
> 7)
1560 if (!(cfg
->result
.resource
[idx
].flags
& IORESOURCE_AUTO
)) /* don't touch */
1563 port
= cfg
->port
[idx
];
1566 value1
= &cfg
->result
.resource
[idx
].start
;
1567 value2
= &cfg
->result
.resource
[idx
].end
;
1568 if (cfg
->result
.resource
[idx
].flags
& IORESOURCE_AUTO
) {
1569 cfg
->result
.resource
[idx
].flags
&= ~IORESOURCE_AUTO
;
1570 *value1
= port
->min
;
1571 *value2
= port
->min
+ port
->size
- 1;
1572 if (!isapnp_check_port(cfg
, *value1
, port
->size
, idx
))
1576 *value1
+= port
->align
;
1577 *value2
= *value1
+ port
->size
- 1;
1578 if (*value1
> port
->max
|| !port
->align
) {
1579 if (port
->res
&& port
->res
->alt
) {
1580 if ((err
= isapnp_alternative_switch(cfg
, port
->res
, port
->res
->alt
))<0)
1586 } while (isapnp_check_port(cfg
, *value1
, port
->size
, idx
));
1590 static void isapnp_test_handler(int irq
, void *dev_id
, struct pt_regs
*regs
)
1594 static int isapnp_check_interrupt(struct isapnp_cfgtmp
*cfg
, int irq
, int idx
)
1597 struct pci_dev
*dev
;
1599 if (irq
< 0 || irq
> 15)
1601 for (i
= 0; i
< 16; i
++) {
1602 if (isapnp_reserve_irq
[i
] == irq
)
1605 isapnp_for_each_dev(dev
) {
1607 if (dev
->irq_resource
[0].start
== irq
||
1608 dev
->irq_resource
[1].start
== irq
)
1613 if (!isapnp_skip_pci_scan
) {
1614 pci_for_each_dev(dev
) {
1615 if (dev
->irq
== irq
)
1620 if (request_irq(irq
, isapnp_test_handler
, SA_INTERRUPT
, "isapnp", NULL
))
1622 free_irq(irq
, NULL
);
1623 for (i
= 0; i
< DEVICE_COUNT_IRQ
; i
++) {
1626 if (!cfg
->result
.irq_resource
[i
].flags
)
1628 if (cfg
->result
.irq_resource
[i
].flags
& IORESOURCE_AUTO
)
1630 if (cfg
->result
.irq_resource
[i
].start
== irq
)
1636 static int isapnp_valid_irq(struct isapnp_cfgtmp
*cfg
, int idx
)
1638 /* IRQ priority: this table is good for i386 */
1639 static unsigned short xtab
[16] = {
1640 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
1643 unsigned long *value1
, *value2
;
1644 struct isapnp_irq
*irq
;
1646 if (!cfg
|| idx
< 0 || idx
> 1)
1648 if (!(cfg
->result
.irq_resource
[idx
].flags
& IORESOURCE_AUTO
))
1651 irq
= cfg
->irq
[idx
];
1654 value1
= &cfg
->result
.irq_resource
[idx
].start
;
1655 value2
= &cfg
->result
.irq_resource
[idx
].end
;
1656 if (cfg
->result
.irq_resource
[idx
].flags
& IORESOURCE_AUTO
) {
1657 for (i
= 0; i
< 16 && !(irq
->map
& (1<<xtab
[i
])); i
++);
1660 cfg
->result
.irq_resource
[idx
].flags
&= ~IORESOURCE_AUTO
;
1661 if (!isapnp_check_interrupt(cfg
, *value1
= *value2
= xtab
[i
], idx
))
1665 for (i
= 0; i
< 16 && xtab
[i
] != *value1
; i
++);
1666 for (i
++; i
< 16 && !(irq
->map
& (1<<xtab
[i
])); i
++);
1668 if (irq
->res
&& irq
->res
->alt
) {
1669 if ((err
= isapnp_alternative_switch(cfg
, irq
->res
, irq
->res
->alt
))<0)
1675 *value1
= *value2
= xtab
[i
];
1677 } while (isapnp_check_interrupt(cfg
, *value1
, idx
));
1681 static int isapnp_check_dma(struct isapnp_cfgtmp
*cfg
, int dma
, int idx
)
1684 struct pci_dev
*dev
;
1686 /* Some machines allow DMA 0, but others don't. In fact on some
1687 boxes DMA 0 is the memory refresh. Play safe */
1689 if (dma
< 1 || dma
== 4 || dma
> 7)
1691 for (i
= 0; i
< 8; i
++) {
1692 if (isapnp_reserve_dma
[i
] == dma
)
1695 isapnp_for_each_dev(dev
) {
1697 if (dev
->dma_resource
[0].start
== dma
|| dev
->dma_resource
[1].start
== dma
)
1701 if (request_dma(dma
, "isapnp"))
1704 for (i
= 0; i
< 2; i
++) {
1707 if (!cfg
->result
.dma_resource
[i
].flags
||
1708 (cfg
->result
.dma_resource
[i
].flags
& IORESOURCE_AUTO
))
1710 if (cfg
->result
.dma_resource
[i
].start
== dma
)
1716 static int isapnp_valid_dma(struct isapnp_cfgtmp
*cfg
, int idx
)
1719 unsigned long *value1
, *value2
;
1720 struct isapnp_dma
*dma
;
1722 if (!cfg
|| idx
< 0 || idx
> 1)
1724 if (!(cfg
->result
.dma_resource
[idx
].flags
& IORESOURCE_AUTO
)) /* don't touch */
1727 dma
= cfg
->dma
[idx
];
1730 value1
= &cfg
->result
.dma_resource
[idx
].start
;
1731 value2
= &cfg
->result
.dma_resource
[idx
].end
;
1732 if (cfg
->result
.dma_resource
[idx
].flags
& IORESOURCE_AUTO
) {
1733 for (i
= 0; i
< 8 && !(dma
->map
& (1<<i
)); i
++);
1736 cfg
->result
.dma_resource
[idx
].flags
&= ~IORESOURCE_AUTO
;
1737 if (!isapnp_check_dma(cfg
, *value1
= *value2
= i
, idx
))
1741 for (i
= *value1
+ 1; i
< 8 && !(dma
->map
& (1<<i
)); i
++);
1743 if (dma
->res
&& dma
->res
->alt
) {
1744 if ((err
= isapnp_alternative_switch(cfg
, dma
->res
, dma
->res
->alt
))<0)
1750 *value1
= *value2
= i
;
1752 } while (isapnp_check_dma(cfg
, *value1
, idx
));
1756 static int isapnp_check_mem(struct isapnp_cfgtmp
*cfg
, unsigned int addr
, unsigned int size
, int idx
)
1759 unsigned int raddr
, rsize
;
1760 struct isapnp_mem
*xmem
;
1761 struct pci_dev
*dev
;
1763 for (i
= 0; i
< 8; i
++) {
1764 raddr
= (unsigned int)isapnp_reserve_mem
[i
<< 1];
1765 rsize
= (unsigned int)isapnp_reserve_mem
[(i
<< 1) + 1];
1766 if (addr
>= raddr
&& addr
< raddr
+ rsize
)
1768 if (addr
+ size
> raddr
&& addr
+ size
< (raddr
+ rsize
) - 1)
1770 if (__check_region(&iomem_resource
, addr
, size
))
1773 isapnp_for_each_dev(dev
) {
1775 for (tmp
= 0; tmp
< 4; tmp
++) {
1776 if (dev
->resource
[tmp
].flags
) {
1777 raddr
= dev
->resource
[tmp
+ 8].start
;
1778 rsize
= (dev
->resource
[tmp
+ 8].end
- raddr
) + 1;
1779 if (addr
>= raddr
&& addr
< raddr
+ rsize
)
1781 if (addr
+ size
> raddr
&& addr
+ size
< (raddr
+ rsize
) - 1)
1787 for (i
= 0; i
< 4; i
++) {
1788 unsigned int flags
= cfg
->request
->resource
[i
+ 8].flags
;
1793 tmp
= cfg
->result
.resource
[i
+ 8].start
;
1794 if (flags
& IORESOURCE_AUTO
) { /* auto */
1798 if (cfg
->result
.resource
[i
+ 8].flags
& IORESOURCE_AUTO
)
1800 if (tmp
+ xmem
->size
>= addr
&& tmp
<= addr
+ xmem
->size
)
1806 xmem
= isapnp_find_mem(cfg
->request
, i
);
1809 if (tmp
+ xmem
->size
>= addr
&& tmp
<= addr
+ xmem
->size
)
1815 static int isapnp_valid_mem(struct isapnp_cfgtmp
*cfg
, int idx
)
1818 unsigned long *value1
, *value2
;
1819 struct isapnp_mem
*mem
;
1821 if (!cfg
|| idx
< 0 || idx
> 3)
1823 if (!(cfg
->result
.resource
[idx
+ 8].flags
& IORESOURCE_AUTO
)) /* don't touch */
1826 mem
= cfg
->mem
[idx
];
1829 value1
= &cfg
->result
.resource
[idx
+ 8].start
;
1830 value2
= &cfg
->result
.resource
[idx
+ 8].end
;
1831 if (cfg
->result
.resource
[idx
+ 8].flags
& IORESOURCE_AUTO
) {
1832 cfg
->result
.resource
[idx
+ 8].flags
&= ~IORESOURCE_AUTO
;
1834 *value2
= mem
->min
+ mem
->size
- 1;
1835 if (!isapnp_check_mem(cfg
, *value1
, mem
->size
, idx
))
1839 *value1
+= mem
->align
;
1840 *value2
= *value1
+ mem
->size
- 1;
1841 if (*value1
> mem
->max
|| !mem
->align
) {
1842 if (mem
->res
&& mem
->res
->alt
) {
1843 if ((err
= isapnp_alternative_switch(cfg
, mem
->res
, mem
->res
->alt
))<0)
1849 } while (isapnp_check_mem(cfg
, *value1
, mem
->size
, idx
));
1853 static int isapnp_check_valid(struct isapnp_cfgtmp
*cfg
)
1857 for (tmp
= 0; tmp
< 8; tmp
++)
1858 if (cfg
->result
.resource
[tmp
].flags
& IORESOURCE_AUTO
)
1860 for (tmp
= 0; tmp
< 2; tmp
++)
1861 if (cfg
->result
.irq_resource
[tmp
].flags
& IORESOURCE_AUTO
)
1863 for (tmp
= 0; tmp
< 2; tmp
++)
1864 if (cfg
->result
.dma_resource
[tmp
].flags
& IORESOURCE_AUTO
)
1866 for (tmp
= 0; tmp
< 4; tmp
++)
1867 if (cfg
->result
.resource
[tmp
+ 8].flags
& IORESOURCE_AUTO
)
1872 static int isapnp_config_activate(struct pci_dev
*dev
)
1874 struct isapnp_cfgtmp cfg
;
1875 int tmp
, fauto
, err
;
1881 memset(&cfg
, 0, sizeof(cfg
));
1883 memcpy(&cfg
.result
, dev
, sizeof(struct pci_dev
));
1884 /* check if all values are set, otherwise try auto-configuration */
1885 for (tmp
= fauto
= 0; !fauto
&& tmp
< 8; tmp
++) {
1886 if (dev
->resource
[tmp
].flags
& IORESOURCE_AUTO
)
1889 for (tmp
= 0; !fauto
&& tmp
< 2; tmp
++) {
1890 if (dev
->irq_resource
[tmp
].flags
& IORESOURCE_AUTO
)
1893 for (tmp
= 0; !fauto
&& tmp
< 2; tmp
++) {
1894 if (dev
->dma_resource
[tmp
].flags
& IORESOURCE_AUTO
)
1897 for (tmp
= 0; !fauto
&& tmp
< 4; tmp
++) {
1898 if (dev
->resource
[tmp
+ 8].flags
& IORESOURCE_AUTO
)
1903 /* set variables to initial values */
1904 if ((err
= isapnp_alternative_switch(&cfg
, NULL
, NULL
))<0)
1906 /* find first valid configuration */
1909 for (tmp
= 0; tmp
< 8 && cfg
.result
.resource
[tmp
].flags
; tmp
++)
1910 if ((err
= isapnp_valid_port(&cfg
, tmp
))<0)
1912 for (tmp
= 0; tmp
< 2 && cfg
.result
.irq_resource
[tmp
].flags
; tmp
++)
1913 if ((err
= isapnp_valid_irq(&cfg
, tmp
))<0)
1915 for (tmp
= 0; tmp
< 2 && cfg
.result
.dma_resource
[tmp
].flags
; tmp
++)
1916 if ((err
= isapnp_valid_dma(&cfg
, tmp
))<0)
1918 for (tmp
= 0; tmp
< 4 && cfg
.result
.resource
[tmp
+ 8].flags
; tmp
++)
1919 if ((err
= isapnp_valid_mem(&cfg
, tmp
))<0)
1921 } while (isapnp_check_valid(&cfg
)<0 && fauto
++ < 20);
1925 /* we have valid configuration, try configure hardware */
1926 isapnp_cfg_begin(dev
->bus
->number
, dev
->devfn
);
1928 dev
->irq_resource
[0] = cfg
.result
.irq_resource
[0];
1929 dev
->irq_resource
[1] = cfg
.result
.irq_resource
[1];
1930 dev
->dma_resource
[0] = cfg
.result
.dma_resource
[0];
1931 dev
->dma_resource
[1] = cfg
.result
.dma_resource
[1];
1932 for (tmp
= 0; tmp
< 12; tmp
++) {
1933 dev
->resource
[tmp
] = cfg
.result
.resource
[tmp
];
1935 for (tmp
= 0; tmp
< 8 && dev
->resource
[tmp
].flags
; tmp
++)
1936 isapnp_write_word(ISAPNP_CFG_PORT
+(tmp
<<1), dev
->resource
[tmp
].start
);
1937 for (tmp
= 0; tmp
< 2 && dev
->irq_resource
[tmp
].flags
; tmp
++) {
1938 int irq
= dev
->irq_resource
[tmp
].start
;
1941 isapnp_write_byte(ISAPNP_CFG_IRQ
+(tmp
<<1), irq
);
1943 for (tmp
= 0; tmp
< 2 && dev
->dma_resource
[tmp
].flags
; tmp
++)
1944 isapnp_write_byte(ISAPNP_CFG_DMA
+tmp
, dev
->dma_resource
[tmp
].start
);
1945 for (tmp
= 0; tmp
< 4 && dev
->resource
[tmp
+8].flags
; tmp
++)
1946 isapnp_write_word(ISAPNP_CFG_MEM
+(tmp
<<2), (dev
->resource
[tmp
+ 8].start
>> 8) & 0xffff);
1947 isapnp_activate(dev
->devfn
);
1952 static int isapnp_config_deactivate(struct pci_dev
*dev
)
1954 if (!dev
|| !dev
->active
)
1956 isapnp_cfg_begin(dev
->bus
->number
, dev
->devfn
);
1957 isapnp_deactivate(dev
->devfn
);
1963 void isapnp_resource_change(struct resource
*resource
,
1964 unsigned long start
,
1967 if (resource
== NULL
)
1969 resource
->flags
&= ~IORESOURCE_AUTO
;
1970 resource
->start
= start
;
1971 resource
->end
= start
+ size
- 1;
1980 static void isapnp_free_port(struct isapnp_port
*port
)
1982 struct isapnp_port
*next
;
1991 static void isapnp_free_irq(struct isapnp_irq
*irq
)
1993 struct isapnp_irq
*next
;
2002 static void isapnp_free_dma(struct isapnp_dma
*dma
)
2004 struct isapnp_dma
*next
;
2013 static void isapnp_free_mem(struct isapnp_mem
*mem
)
2015 struct isapnp_mem
*next
;
2024 static void isapnp_free_mem32(struct isapnp_mem32
*mem32
)
2026 struct isapnp_mem32
*next
;
2035 static void isapnp_free_resources(struct isapnp_resources
*resources
, int alt
)
2037 struct isapnp_resources
*next
;
2040 next
= alt
? resources
->alt
: resources
->next
;
2041 isapnp_free_port(resources
->port
);
2042 isapnp_free_irq(resources
->irq
);
2043 isapnp_free_dma(resources
->dma
);
2044 isapnp_free_mem(resources
->mem
);
2045 isapnp_free_mem32(resources
->mem32
);
2046 if (!alt
&& resources
->alt
)
2047 isapnp_free_resources(resources
->alt
, 1);
2053 static void isapnp_free_card(struct pci_bus
*card
)
2055 while (!list_empty(&card
->devices
)) {
2056 struct list_head
*list
= card
->devices
.next
;
2057 struct pci_dev
*dev
= pci_dev_b(list
);
2059 isapnp_free_resources((struct isapnp_resources
*)dev
->sysdata
, 0);
2067 static void isapnp_free_all_resources(void)
2069 #ifdef ISAPNP_REGION_OK
2071 release_resource(pidxr_res
);
2074 release_resource(pnpwrp_res
);
2075 if (isapnp_rdp
>= 0x203 && isapnp_rdp
<= 0x3ff && isapnp_rdp_res
)
2076 release_resource(isapnp_rdp_res
);
2078 #ifdef CONFIG_PROC_FS
2081 while (!list_empty(&isapnp_cards
)) {
2082 struct list_head
*list
= isapnp_cards
.next
;
2084 isapnp_free_card(pci_bus_b(list
));
2089 EXPORT_SYMBOL(isapnp_cards
);
2090 EXPORT_SYMBOL(isapnp_devices
);
2091 EXPORT_SYMBOL(isapnp_present
);
2092 EXPORT_SYMBOL(isapnp_cfg_begin
);
2093 EXPORT_SYMBOL(isapnp_cfg_end
);
2094 EXPORT_SYMBOL(isapnp_read_byte
);
2095 EXPORT_SYMBOL(isapnp_read_word
);
2096 EXPORT_SYMBOL(isapnp_read_dword
);
2097 EXPORT_SYMBOL(isapnp_write_byte
);
2098 EXPORT_SYMBOL(isapnp_write_word
);
2099 EXPORT_SYMBOL(isapnp_write_dword
);
2100 EXPORT_SYMBOL(isapnp_wake
);
2101 EXPORT_SYMBOL(isapnp_device
);
2102 EXPORT_SYMBOL(isapnp_activate
);
2103 EXPORT_SYMBOL(isapnp_deactivate
);
2104 EXPORT_SYMBOL(isapnp_find_card
);
2105 EXPORT_SYMBOL(isapnp_find_dev
);
2106 EXPORT_SYMBOL(isapnp_probe_cards
);
2107 EXPORT_SYMBOL(isapnp_resource_change
);
2109 int __init
isapnp_init(void)
2112 struct pci_bus
*card
;
2114 if (isapnp_disable
) {
2115 isapnp_detected
= 0;
2116 printk("isapnp: ISA Plug & Play support disabled\n");
2119 #ifdef ISAPNP_REGION_OK
2120 pidxr_res
=request_region(_PIDXR
, 1, "isapnp index");
2122 printk("isapnp: Index Register 0x%x already used\n", _PIDXR
);
2126 pnpwrp_res
=request_region(_PNPWRP
, 1, "isapnp write");
2128 printk("isapnp: Write Data Register 0x%x already used\n", _PNPWRP
);
2133 * Print a message. The existing ISAPnP code is hanging machines
2134 * so let the user know where.
2137 printk("isapnp: Scanning for Pnp cards...\n");
2138 if (isapnp_rdp
>= 0x203 && isapnp_rdp
<= 0x3ff) {
2140 isapnp_rdp_res
=request_region(isapnp_rdp
, 1, "isapnp read");
2141 if(!isapnp_rdp_res
) {
2142 printk("isapnp: Read Data Register 0x%x already used\n", isapnp_rdp
);
2147 isapnp_detected
= 1;
2148 if (isapnp_rdp
< 0x203 || isapnp_rdp
> 0x3ff) {
2149 cards
= isapnp_isolate();
2151 (isapnp_rdp
< 0x203 || isapnp_rdp
> 0x3ff)) {
2152 isapnp_free_all_resources();
2153 isapnp_detected
= 0;
2154 printk("isapnp: No Plug & Play device found\n");
2157 isapnp_rdp_res
=request_region(isapnp_rdp
, 1, "isapnp read");
2159 isapnp_build_device_list();
2162 isapnp_for_each_card(card
) {
2164 if (isapnp_verbose
) {
2165 struct list_head
*devlist
;
2166 printk( "isapnp: Card '%s'\n", card
->name
[0]?card
->name
:"Unknown");
2167 if (isapnp_verbose
< 2)
2169 for (devlist
= card
->devices
.next
; devlist
!= &card
->devices
; devlist
= devlist
->next
) {
2170 struct pci_dev
*dev
= pci_dev_b(devlist
);
2171 printk("isapnp: Device '%s'\n", dev
->name
[0]?card
->name
:"Unknown");
2176 printk("isapnp: %i Plug & Play card%s detected total\n", cards
, cards
>1?"s":"");
2178 printk("isapnp: No Plug & Play card found\n");
2180 #ifdef CONFIG_PROC_FS
2188 int init_module(void)
2190 return isapnp_init();
2193 void cleanup_module(void)
2195 if (isapnp_detected
)
2196 isapnp_free_all_resources();