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
= NULL
;
52 struct resource
*pnpwrp_res
= NULL
;
53 struct resource
*isapnp_rdp_res
= NULL
;
55 int isapnp_disable
= 0; /* Disable ISA PnP */
56 int isapnp_rdp
= 0; /* Read Data Port */
57 int isapnp_reset
= 1; /* reset all PnP cards (deactivate) */
58 int isapnp_skip_pci_scan
= 0; /* 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
= 0;
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
));
440 dev
->vendor
= (tmp
[1] << 8) | tmp
[0];
441 dev
->device
= (tmp
[3] << 8) | tmp
[2];
445 dev
->regs
|= tmp
[5] << 8;
446 dev
->prepare
= isapnp_config_prepare
;
447 dev
->activate
= isapnp_config_activate
;
448 dev
->deactivate
= isapnp_config_deactivate
;
453 * Build new resources structure
456 static struct isapnp_resources
* __init
isapnp_build_resources(struct pci_dev
*dev
, int dependent
)
458 struct isapnp_resources
*res
, *ptr
, *ptra
;
460 res
= isapnp_alloc(sizeof(struct isapnp_resources
));
464 ptr
= (struct isapnp_resources
*)dev
->sysdata
;
465 while (ptr
&& ptr
->next
)
467 if (ptr
&& ptr
->dependent
&& dependent
) { /* add to another list */
469 while (ptra
&& ptra
->alt
)
482 res
->priority
= dependent
& 0xff;
483 if (res
->priority
> ISAPNP_RES_PRIORITY_FUNCTIONAL
)
484 res
->priority
= ISAPNP_RES_PRIORITY_INVALID
;
487 res
->priority
= ISAPNP_RES_PRIORITY_PREFERRED
;
494 * Add IRQ resource to resources list.
497 static void __init
isapnp_add_irq_resource(struct pci_dev
*dev
,
498 struct isapnp_resources
**res
,
499 int dependent
, int size
)
501 unsigned char tmp
[3];
502 struct isapnp_irq
*irq
, *ptr
;
504 isapnp_peek(tmp
, size
);
505 irq
= isapnp_alloc(sizeof(struct isapnp_irq
));
509 *res
= isapnp_build_resources(dev
, dependent
);
515 irq
->map
= (tmp
[1] << 8) | tmp
[0];
519 irq
->flags
= IORESOURCE_IRQ_HIGHEDGE
;
522 while (ptr
&& ptr
->next
)
531 * Add DMA resource to resources list.
534 static void __init
isapnp_add_dma_resource(struct pci_dev
*dev
,
535 struct isapnp_resources
**res
,
536 int dependent
, int size
)
538 unsigned char tmp
[2];
539 struct isapnp_dma
*dma
, *ptr
;
541 isapnp_peek(tmp
, size
);
542 dma
= isapnp_alloc(sizeof(struct isapnp_dma
));
546 *res
= isapnp_build_resources(dev
, dependent
);
556 while (ptr
&& ptr
->next
)
565 * Add port resource to resources list.
568 static void __init
isapnp_add_port_resource(struct pci_dev
*dev
,
569 struct isapnp_resources
**res
,
570 int dependent
, int size
)
572 unsigned char tmp
[7];
573 struct isapnp_port
*port
, *ptr
;
575 isapnp_peek(tmp
, size
);
576 port
= isapnp_alloc(sizeof(struct isapnp_port
));
580 *res
= isapnp_build_resources(dev
, dependent
);
586 port
->min
= (tmp
[2] << 8) | tmp
[1];
587 port
->max
= (tmp
[4] << 8) | tmp
[3];
588 port
->align
= tmp
[5];
590 port
->flags
= tmp
[0] ? ISAPNP_PORT_FLAG_16BITADDR
: 0;
593 while (ptr
&& ptr
->next
)
602 * Add fixed port resource to resources list.
605 static void __init
isapnp_add_fixed_port_resource(struct pci_dev
*dev
,
606 struct isapnp_resources
**res
,
607 int dependent
, int size
)
609 unsigned char tmp
[3];
610 struct isapnp_port
*port
, *ptr
;
612 isapnp_peek(tmp
, size
);
613 port
= isapnp_alloc(sizeof(struct isapnp_port
));
617 *res
= isapnp_build_resources(dev
, dependent
);
623 port
->min
= port
->max
= (tmp
[1] << 8) | tmp
[0];
626 port
->flags
= ISAPNP_PORT_FLAG_FIXED
;
629 while (ptr
&& ptr
->next
)
638 * Add memory resource to resources list.
641 static void __init
isapnp_add_mem_resource(struct pci_dev
*dev
,
642 struct isapnp_resources
**res
,
643 int dependent
, int size
)
645 unsigned char tmp
[9];
646 struct isapnp_mem
*mem
, *ptr
;
648 isapnp_peek(tmp
, size
);
649 mem
= isapnp_alloc(sizeof(struct isapnp_mem
));
653 *res
= isapnp_build_resources(dev
, dependent
);
659 mem
->min
= ((tmp
[2] << 8) | tmp
[1]) << 8;
660 mem
->max
= ((tmp
[4] << 8) | tmp
[3]) << 8;
661 mem
->align
= (tmp
[6] << 8) | tmp
[5];
662 mem
->size
= ((tmp
[8] << 8) | tmp
[7]) << 8;
666 while (ptr
&& ptr
->next
)
675 * Add 32-bit memory resource to resources list.
678 static void __init
isapnp_add_mem32_resource(struct pci_dev
*dev
,
679 struct isapnp_resources
**res
,
680 int dependent
, int size
)
682 unsigned char tmp
[17];
683 struct isapnp_mem32
*mem32
, *ptr
;
685 isapnp_peek(tmp
, size
);
686 mem32
= isapnp_alloc(sizeof(struct isapnp_mem32
));
690 *res
= isapnp_build_resources(dev
, dependent
);
696 memcpy(mem32
->data
, tmp
, 17);
699 while (ptr
&& ptr
->next
)
704 (*res
)->mem32
= mem32
;
708 * Add 32-bit fixed memory resource to resources list.
711 static void __init
isapnp_add_fixed_mem32_resource(struct pci_dev
*dev
,
712 struct isapnp_resources
**res
,
713 int dependent
, int size
)
715 unsigned char tmp
[17];
716 struct isapnp_mem32
*mem32
, *ptr
;
718 isapnp_peek(tmp
, size
);
719 mem32
= isapnp_alloc(sizeof(struct isapnp_mem32
));
723 *res
= isapnp_build_resources(dev
, dependent
);
729 memcpy(mem32
->data
, tmp
, 17);
732 while (ptr
&& ptr
->next
)
737 (*res
)->mem32
= mem32
;
741 * Parse resource map for logical device.
744 static int __init
isapnp_create_device(struct pci_bus
*card
,
747 int number
= 0, skip
= 0, dependent
= 0, compat
= 0;
748 unsigned char type
, tmp
[17];
750 struct isapnp_resources
*res
= NULL
;
752 if ((dev
= isapnp_parse_device(card
, size
, number
++)) == NULL
)
754 list_add(&dev
->bus_list
, &card
->devices
);
755 list_add_tail(&dev
->global_list
, &isapnp_devices
);
757 if (isapnp_read_tag(&type
, &size
)<0)
759 if (skip
&& type
!= _STAG_LOGDEVID
&& type
!= _STAG_END
)
763 if (size
>= 5 && size
<= 6) {
764 isapnp_config_prepare(dev
);
765 if ((dev
= isapnp_parse_device(card
, size
, number
++)) == NULL
)
767 list_add_tail(&dev
->bus_list
, &card
->devices
);
768 list_add_tail(&dev
->global_list
, &isapnp_devices
);
778 case _STAG_COMPATDEVID
:
779 if (size
== 4 && compat
< DEVICE_COUNT_COMPATIBLE
) {
781 dev
->vendor_compatible
[compat
] = (tmp
[1] << 8) | tmp
[0];
782 dev
->device_compatible
[compat
] = (tmp
[3] << 8) | tmp
[2];
788 if (size
< 2 || size
> 3)
790 isapnp_add_irq_resource(dev
, &res
, dependent
, size
);
796 isapnp_add_dma_resource(dev
, &res
, dependent
, size
);
803 dependent
= 0x100 | ISAPNP_RES_PRIORITY_ACCEPTABLE
;
805 isapnp_peek(tmp
, size
);
806 dependent
= 0x100 | tmp
[0];
819 isapnp_add_port_resource(dev
, &res
, dependent
, size
);
825 isapnp_add_fixed_port_resource(dev
, &res
, dependent
, size
);
833 isapnp_add_mem_resource(dev
, &res
, dependent
, size
);
837 if (dev
->name
[0] == '\0') {
838 unsigned short size1
= size
> 47 ? 47 : size
;
839 isapnp_peek(dev
->name
, size1
);
840 dev
->name
[size1
] = '\0';
844 case _LTAG_UNICODESTR
:
845 /* silently ignore */
846 /* who use unicode for hardware identification? */
850 case _LTAG_MEM32RANGE
:
853 isapnp_add_mem32_resource(dev
, &res
, dependent
, size
);
856 case _LTAG_FIXEDMEM32RANGE
:
859 isapnp_add_fixed_mem32_resource(dev
, &res
, dependent
, size
);
864 isapnp_skip_bytes(size
);
867 printk("isapnp: unexpected or unknown tag type 0x%x for logical device %i (device %i), ignored\n", type
, dev
->devfn
, card
->number
);
871 isapnp_skip_bytes(size
);
873 isapnp_config_prepare(dev
);
878 * Parse resource map for ISA PnP card.
881 static void __init
isapnp_parse_resource_map(struct pci_bus
*card
)
883 unsigned char type
, tmp
[17];
887 if (isapnp_read_tag(&type
, &size
)<0)
894 card
->pnpver
= tmp
[0];
895 card
->productver
= tmp
[1];
899 if (size
>= 5 && size
<= 6) {
900 if (isapnp_create_device(card
, size
)==1)
908 if (card
->name
[0] == '\0') {
909 unsigned short size1
= size
> 47 ? 47 : size
;
910 isapnp_peek(card
->name
, size1
);
911 card
->name
[size1
] = '\0';
915 case _LTAG_UNICODESTR
:
916 /* silently ignore */
917 /* who use unicode for hardware identification? */
923 isapnp_skip_bytes(size
);
926 printk("isapnp: unexpected or unknown tag type 0x%x for device %i, ignored\n", type
, card
->number
);
930 isapnp_skip_bytes(size
);
935 * Compute ISA PnP checksum for first eight bytes.
938 static unsigned char __init
isapnp_checksum(unsigned char *data
)
941 unsigned char checksum
= 0x6a, bit
, b
;
943 for (i
= 0; i
< 8; i
++) {
945 for (j
= 0; j
< 8; j
++) {
949 checksum
= ((((checksum
^ (checksum
>> 1)) & 0x01) ^ bit
) << 7) | (checksum
>> 1);
956 * Build device list for all present ISA PnP devices.
959 static int __init
isapnp_build_device_list(void)
962 unsigned char header
[9], checksum
;
963 struct pci_bus
*card
;
968 for (csn
= 1; csn
<= 10; csn
++) {
970 isapnp_peek(header
, 9);
971 checksum
= isapnp_checksum(header
);
973 printk("vendor: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
974 header
[0], header
[1], header
[2], header
[3],
975 header
[4], header
[5], header
[6], header
[7], header
[8]);
976 printk("checksum = 0x%x\n", checksum
);
978 if (checksum
== 0x00 || checksum
!= header
[8]) /* not valid CSN */
980 if ((card
= isapnp_alloc(sizeof(struct pci_bus
))) == NULL
)
983 card
->vendor
= (header
[1] << 8) | header
[0];
984 card
->device
= (header
[3] << 8) | header
[2];
985 card
->serial
= (header
[7] << 24) | (header
[6] << 16) | (header
[5] << 8) | header
[4];
986 isapnp_checksum_value
= 0x00;
987 INIT_LIST_HEAD(&card
->children
);
988 INIT_LIST_HEAD(&card
->devices
);
989 isapnp_parse_resource_map(card
);
990 if (isapnp_checksum_value
!= 0x00)
991 printk("isapnp: checksum for device %i is not valid (0x%x)\n", csn
, isapnp_checksum_value
);
992 card
->checksum
= isapnp_checksum_value
;
994 list_add_tail(&card
->node
, &isapnp_cards
);
996 isapnp_for_each_dev(dev
) {
997 isapnp_fixup_device(dev
);
1003 * Basic configuration routines.
1006 int isapnp_present(void)
1008 return !list_empty(&isapnp_devices
);
1011 int isapnp_cfg_begin(int csn
, int logdev
)
1013 if (csn
< 1 || csn
> 10 || logdev
> 10)
1016 down(&isapnp_cfg_mutex
);
1020 #if 1 /* to avoid malfunction when the isapnptools package is used */
1022 udelay(1000); /* delay 1000us */
1023 write_address(0x01);
1024 udelay(1000); /* delay 1000us */
1027 isapnp_device(logdev
);
1031 int isapnp_cfg_end(void)
1034 up(&isapnp_cfg_mutex
);
1043 static struct isapnp_port
*isapnp_find_port(struct pci_dev
*dev
, int index
)
1045 struct isapnp_resources
*res
;
1046 struct isapnp_port
*port
;
1048 if (!dev
|| index
< 0 || index
> 7)
1050 for (res
= (struct isapnp_resources
*)dev
->sysdata
; res
; res
= res
->next
) {
1051 for (port
= res
->port
; port
; port
= port
->next
) {
1060 struct isapnp_irq
*isapnp_find_irq(struct pci_dev
*dev
, int index
)
1062 struct isapnp_resources
*res
, *resa
;
1063 struct isapnp_irq
*irq
;
1064 int index1
, index2
, index3
;
1066 if (!dev
|| index
< 0 || index
> 7)
1068 for (res
= (struct isapnp_resources
*)dev
->sysdata
; res
; res
= res
->next
) {
1070 for (resa
= res
; resa
; resa
= resa
->alt
) {
1073 for (irq
= resa
->irq
; irq
; irq
= irq
->next
) {
1079 if (index3
< index2
)
1087 struct isapnp_dma
*isapnp_find_dma(struct pci_dev
*dev
, int index
)
1089 struct isapnp_resources
*res
;
1090 struct isapnp_dma
*dma
;
1092 if (!dev
|| index
< 0 || index
> 7)
1094 for (res
= (struct isapnp_resources
*)dev
->sysdata
; res
; res
= res
->next
) {
1095 for (dma
= res
->dma
; dma
; dma
= dma
->next
) {
1104 struct isapnp_mem
*isapnp_find_mem(struct pci_dev
*dev
, int index
)
1106 struct isapnp_resources
*res
;
1107 struct isapnp_mem
*mem
;
1109 if (!dev
|| index
< 0 || index
> 7)
1111 for (res
= (struct isapnp_resources
*)dev
->sysdata
; res
; res
= res
->next
) {
1112 for (mem
= res
->mem
; mem
; mem
= mem
->next
) {
1121 struct isapnp_mem32
*isapnp_find_mem32(struct pci_dev
*dev
, int index
)
1123 struct isapnp_resources
*res
;
1124 struct isapnp_mem32
*mem32
;
1126 if (!dev
|| index
< 0 || index
> 7)
1128 for (res
= (struct isapnp_resources
*)dev
->sysdata
; res
; res
= res
->next
) {
1129 for (mem32
= res
->mem32
; mem32
; mem32
= mem32
->next
) {
1142 struct pci_bus
*isapnp_find_card(unsigned short vendor
,
1143 unsigned short device
,
1144 struct pci_bus
*from
)
1146 struct list_head
*list
;
1148 list
= isapnp_cards
.next
;
1150 list
= from
->node
.next
;
1152 while (list
!= &isapnp_cards
) {
1153 struct pci_bus
*card
= pci_bus_b(list
);
1154 if (card
->vendor
== vendor
&& card
->device
== device
)
1161 struct pci_dev
*isapnp_find_dev(struct pci_bus
*card
,
1162 unsigned short vendor
,
1163 unsigned short function
,
1164 struct pci_dev
*from
)
1166 if (card
== NULL
) { /* look for a logical device from all cards */
1167 struct list_head
*list
;
1169 list
= isapnp_devices
.next
;
1171 list
= from
->global_list
.next
;
1173 while (list
!= &isapnp_devices
) {
1175 struct pci_dev
*dev
= pci_dev_g(list
);
1177 if (dev
->vendor
== vendor
&& dev
->device
== function
)
1179 for (idx
= 0; idx
< DEVICE_COUNT_COMPATIBLE
; idx
++)
1180 if (dev
->vendor_compatible
[idx
] == vendor
&&
1181 dev
->device_compatible
[idx
] == function
)
1186 struct list_head
*list
;
1188 list
= card
->devices
.next
;
1190 list
= from
->bus_list
.next
;
1191 if (from
->bus
!= card
) /* something is wrong */
1194 while (list
!= &card
->devices
) {
1196 struct pci_dev
*dev
= pci_dev_b(list
);
1198 if (dev
->vendor
== vendor
&& dev
->device
== function
)
1200 for (idx
= 0; idx
< DEVICE_COUNT_COMPATIBLE
; idx
++)
1201 if (dev
->vendor_compatible
[idx
] == vendor
&&
1202 dev
->device_compatible
[idx
] == function
)
1210 static unsigned int isapnp_dma_resource_flags(struct isapnp_dma
*dma
)
1212 return dma
->flags
| IORESOURCE_DMA
| IORESOURCE_AUTO
;
1215 static unsigned int isapnp_mem_resource_flags(struct isapnp_mem
*mem
)
1217 unsigned int result
;
1219 result
= mem
->flags
| IORESOURCE_MEM
| IORESOURCE_AUTO
;
1220 if (!(mem
->flags
& IORESOURCE_MEM_WRITEABLE
))
1221 result
|= IORESOURCE_READONLY
;
1222 if (mem
->flags
& IORESOURCE_MEM_CACHEABLE
)
1223 result
|= IORESOURCE_CACHEABLE
;
1224 if (mem
->flags
& IORESOURCE_MEM_RANGELENGTH
)
1225 result
|= IORESOURCE_RANGELENGTH
;
1226 if (mem
->flags
& IORESOURCE_MEM_SHADOWABLE
)
1227 result
|= IORESOURCE_SHADOWABLE
;
1231 static unsigned int isapnp_irq_resource_flags(struct isapnp_irq
*irq
)
1233 return irq
->flags
| IORESOURCE_IRQ
| IORESOURCE_AUTO
;
1236 static unsigned int isapnp_port_resource_flags(struct isapnp_port
*port
)
1238 return port
->flags
| IORESOURCE_IO
| IORESOURCE_AUTO
;
1241 static int isapnp_config_prepare(struct pci_dev
*dev
)
1243 struct isapnp_resources
*res
, *resa
;
1244 struct isapnp_port
*port
;
1245 struct isapnp_irq
*irq
;
1246 struct isapnp_dma
*dma
;
1247 struct isapnp_mem
*mem
;
1248 int port_count
, port_count1
;
1249 int irq_count
, irq_count1
;
1250 int dma_count
, dma_count1
;
1251 int mem_count
, mem_count1
;
1256 if (dev
->active
|| dev
->ro
)
1258 for (idx
= 0; idx
< DEVICE_COUNT_IRQ
; idx
++) {
1259 dev
->irq_resource
[idx
].name
= NULL
;
1260 dev
->irq_resource
[idx
].start
= 0;
1261 dev
->irq_resource
[idx
].end
= 0;
1262 dev
->irq_resource
[idx
].flags
= 0;
1264 for (idx
= 0; idx
< DEVICE_COUNT_DMA
; idx
++) {
1265 dev
->dma_resource
[idx
].name
= NULL
;
1266 dev
->dma_resource
[idx
].start
= 0;
1267 dev
->dma_resource
[idx
].end
= 0;
1268 dev
->dma_resource
[idx
].flags
= 0;
1270 for (idx
= 0; idx
< DEVICE_COUNT_RESOURCE
; idx
++) {
1271 dev
->resource
[idx
].name
= NULL
;
1272 dev
->resource
[idx
].start
= 0;
1273 dev
->resource
[idx
].end
= 0;
1274 dev
->resource
[idx
].flags
= 0;
1276 port_count
= irq_count
= dma_count
= mem_count
= 0;
1277 for (res
= (struct isapnp_resources
*)dev
->sysdata
; res
; res
= res
->next
) {
1278 port_count1
= irq_count1
= dma_count1
= mem_count1
= 0;
1279 for (resa
= res
; resa
; resa
= resa
->alt
) {
1280 for (port
= resa
->port
, idx
= 0; port
; port
= port
->next
, idx
++) {
1281 if (dev
->resource
[port_count
+ idx
].flags
== 0) {
1282 dev
->resource
[port_count
+ idx
].flags
= isapnp_port_resource_flags(port
);
1283 dev
->resource
[port_count
+ idx
].end
= port
->size
;
1286 if (port_count1
< idx
)
1288 for (irq
= resa
->irq
, idx
= 0; irq
; irq
= irq
->next
, idx
++) {
1289 int count
= irq_count
+ idx
;
1290 if (count
< DEVICE_COUNT_IRQ
) {
1291 if (dev
->irq_resource
[count
].flags
== 0) {
1292 dev
->irq_resource
[count
].flags
= isapnp_irq_resource_flags(irq
);
1297 if (irq_count1
< idx
)
1299 for (dma
= resa
->dma
, idx
= 0; dma
; dma
= dma
->next
, idx
++)
1300 if (dev
->dma_resource
[idx
].flags
== 0) {
1301 dev
->dma_resource
[idx
].flags
= isapnp_dma_resource_flags(dma
);
1303 if (dma_count1
< idx
)
1305 for (mem
= resa
->mem
, idx
= 0; mem
; mem
= mem
->next
, idx
++)
1306 if (dev
->resource
[mem_count
+ idx
+ 8].flags
== 0) {
1307 dev
->resource
[mem_count
+ idx
+ 8].flags
= isapnp_mem_resource_flags(mem
);
1309 if (mem_count1
< idx
)
1312 port_count
+= port_count1
;
1313 irq_count
+= irq_count1
;
1314 dma_count
+= dma_count1
;
1315 mem_count
+= mem_count1
;
1320 struct isapnp_cfgtmp
{
1321 struct isapnp_port
*port
[8];
1322 struct isapnp_irq
*irq
[2];
1323 struct isapnp_dma
*dma
[2];
1324 struct isapnp_mem
*mem
[4];
1325 struct pci_dev
*request
;
1326 struct pci_dev result
;
1329 static int isapnp_alternative_switch(struct isapnp_cfgtmp
*cfg
,
1330 struct isapnp_resources
*from
,
1331 struct isapnp_resources
*to
)
1334 struct isapnp_port
*port
;
1335 struct isapnp_irq
*irq
;
1336 struct isapnp_dma
*dma
;
1337 struct isapnp_mem
*mem
;
1341 /* process port settings */
1342 for (tmp
= 0; tmp
< 8; tmp
++) {
1343 if (!(cfg
->request
->resource
[tmp
].flags
& IORESOURCE_AUTO
))
1344 continue; /* don't touch */
1345 port
= cfg
->port
[tmp
];
1347 cfg
->port
[tmp
] = port
= isapnp_find_port(cfg
->request
, tmp
);
1351 if (from
&& port
->res
== from
) {
1352 while (port
->res
!= to
) {
1353 if (!port
->res
->alt
)
1355 port
= port
->res
->alt
->port
;
1356 for (tmp1
= tmp
; tmp1
> 0 && port
; tmp1
--)
1358 cfg
->port
[tmp
] = port
;
1361 cfg
->result
.resource
[tmp
].flags
= isapnp_port_resource_flags(port
);
1365 /* process irq settings */
1366 for (tmp
= 0; tmp
< 2; tmp
++) {
1367 if (!(cfg
->request
->irq_resource
[tmp
].flags
& IORESOURCE_AUTO
))
1368 continue; /* don't touch */
1369 irq
= cfg
->irq
[tmp
];
1371 cfg
->irq
[tmp
] = irq
= isapnp_find_irq(cfg
->request
, tmp
);
1375 if (from
&& irq
->res
== from
) {
1376 while (irq
->res
!= to
) {
1379 irq
= irq
->res
->alt
->irq
;
1380 for (tmp1
= tmp
; tmp1
> 0 && irq
; tmp1
--)
1382 cfg
->irq
[tmp
] = irq
;
1385 cfg
->result
.irq_resource
[tmp
].flags
= isapnp_irq_resource_flags(irq
);
1389 /* process dma settings */
1390 for (tmp
= 0; tmp
< 2; tmp
++) {
1391 if (!(cfg
->request
->dma_resource
[tmp
].flags
& IORESOURCE_AUTO
))
1392 continue; /* don't touch */
1393 dma
= cfg
->dma
[tmp
];
1395 cfg
->dma
[tmp
] = dma
= isapnp_find_dma(cfg
->request
, tmp
);
1399 if (from
&& dma
->res
== from
) {
1400 while (dma
->res
!= to
) {
1403 dma
= dma
->res
->alt
->dma
;
1404 for (tmp1
= tmp
; tmp1
> 0 && dma
; tmp1
--)
1406 cfg
->dma
[tmp
] = dma
;
1409 cfg
->result
.dma_resource
[tmp
].flags
= isapnp_dma_resource_flags(dma
);
1413 /* process memory settings */
1414 for (tmp
= 0; tmp
< 4; tmp
++) {
1415 if (!(cfg
->request
->resource
[tmp
+ 8].flags
& IORESOURCE_AUTO
))
1416 continue; /* don't touch */
1417 mem
= cfg
->mem
[tmp
];
1419 cfg
->mem
[tmp
] = mem
= isapnp_find_mem(cfg
->request
, tmp
);
1423 if (from
&& mem
->res
== from
) {
1424 while (mem
->res
!= to
) {
1427 mem
= mem
->res
->alt
->mem
;
1428 for (tmp1
= tmp
; tmp1
> 0 && mem
; tmp1
--)
1430 cfg
->mem
[tmp
] = mem
;
1433 cfg
->result
.resource
[tmp
+ 8].flags
= isapnp_mem_resource_flags(mem
);
1440 static int isapnp_check_port(struct isapnp_cfgtmp
*cfg
, int port
, int size
, int idx
)
1442 int i
, tmp
, rport
, rsize
;
1443 struct isapnp_port
*xport
;
1444 struct pci_dev
*dev
;
1446 if (check_region(port
, size
))
1448 for (i
= 0; i
< 8; i
++) {
1449 rport
= isapnp_reserve_io
[i
<< 1];
1450 rsize
= isapnp_reserve_io
[(i
<< 1) + 1];
1451 if (port
>= rport
&& port
< rport
+ rsize
)
1453 if (port
+ size
> rport
&& port
+ size
< (rport
+ rsize
) - 1)
1457 isapnp_for_each_dev(dev
) {
1459 for (tmp
= 0; tmp
< 8; tmp
++) {
1460 if (dev
->resource
[tmp
].flags
) {
1461 rport
= dev
->resource
[tmp
].start
;
1462 rsize
= (dev
->resource
[tmp
].end
- rport
) + 1;
1463 if (port
>= rport
&& port
< rport
+ rsize
)
1465 if (port
+ size
> rport
&& port
+ size
< (rport
+ rsize
) - 1)
1471 for (i
= 0; i
< 8; i
++) {
1475 flags
= cfg
->request
->resource
[i
].flags
;
1478 tmp
= cfg
->request
->resource
[i
].start
;
1479 if (flags
& IORESOURCE_AUTO
) { /* auto */
1480 xport
= cfg
->port
[i
];
1483 if (cfg
->result
.resource
[i
].flags
& IORESOURCE_AUTO
)
1485 tmp
= cfg
->result
.resource
[i
].start
;
1486 if (tmp
+ xport
->size
>= port
&& tmp
<= port
+ xport
->size
)
1492 xport
= isapnp_find_port(cfg
->request
, i
);
1495 if (tmp
+ xport
->size
>= port
&& tmp
<= port
+ xport
->size
)
1501 static int isapnp_valid_port(struct isapnp_cfgtmp
*cfg
, int idx
)
1504 unsigned long *value1
, *value2
;
1505 struct isapnp_port
*port
;
1507 if (!cfg
|| idx
< 0 || idx
> 7)
1509 if (!(cfg
->result
.resource
[idx
].flags
& IORESOURCE_AUTO
)) /* don't touch */
1512 port
= cfg
->port
[idx
];
1515 value1
= &cfg
->result
.resource
[idx
].start
;
1516 value2
= &cfg
->result
.resource
[idx
].end
;
1517 if (cfg
->result
.resource
[idx
].flags
& IORESOURCE_AUTO
) {
1518 cfg
->result
.resource
[idx
].flags
&= ~IORESOURCE_AUTO
;
1519 *value1
= port
->min
;
1520 *value2
= port
->min
+ port
->size
- 1;
1521 if (!isapnp_check_port(cfg
, *value1
, port
->size
, idx
))
1525 *value1
+= port
->align
;
1526 *value2
= *value1
+ port
->size
- 1;
1527 if (*value1
> port
->max
|| !port
->align
) {
1528 if (port
->res
&& port
->res
->alt
) {
1529 if ((err
= isapnp_alternative_switch(cfg
, port
->res
, port
->res
->alt
))<0)
1535 } while (isapnp_check_port(cfg
, *value1
, port
->size
, idx
));
1539 static void isapnp_test_handler(int irq
, void *dev_id
, struct pt_regs
*regs
)
1543 static int isapnp_check_interrupt(struct isapnp_cfgtmp
*cfg
, int irq
, int idx
)
1546 struct pci_dev
*dev
;
1548 if (irq
< 0 || irq
> 15)
1550 for (i
= 0; i
< 16; i
++) {
1551 if (isapnp_reserve_irq
[i
] == irq
)
1554 isapnp_for_each_dev(dev
) {
1556 if (dev
->irq_resource
[0].start
== irq
||
1557 dev
->irq_resource
[1].start
== irq
)
1561 if (request_irq(irq
, isapnp_test_handler
, SA_INTERRUPT
, "isapnp", NULL
))
1563 free_irq(irq
, NULL
);
1564 for (i
= 0; i
< DEVICE_COUNT_IRQ
; i
++) {
1567 if (!cfg
->result
.irq_resource
[i
].flags
)
1569 if (cfg
->result
.irq_resource
[i
].flags
& IORESOURCE_AUTO
)
1571 if (cfg
->result
.irq_resource
[i
].start
== irq
)
1577 static int isapnp_valid_irq(struct isapnp_cfgtmp
*cfg
, int idx
)
1579 /* IRQ priority: this table is good for i386 */
1580 static unsigned short xtab
[16] = {
1581 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
1584 unsigned long *value1
, *value2
;
1585 struct isapnp_irq
*irq
;
1587 if (!cfg
|| idx
< 0 || idx
> 1)
1589 if (!(cfg
->result
.irq_resource
[idx
].flags
& IORESOURCE_AUTO
))
1592 irq
= cfg
->irq
[idx
];
1595 value1
= &cfg
->result
.irq_resource
[idx
].start
;
1596 value2
= &cfg
->result
.irq_resource
[idx
].end
;
1597 if (cfg
->result
.irq_resource
[idx
].flags
& IORESOURCE_AUTO
) {
1598 for (i
= 0; i
< 16 && !(irq
->map
& (1<<xtab
[i
])); i
++);
1601 cfg
->result
.irq_resource
[idx
].flags
&= ~IORESOURCE_AUTO
;
1602 if (!isapnp_check_interrupt(cfg
, *value1
= *value2
= xtab
[i
], idx
))
1606 for (i
= 0; i
< 16 && xtab
[i
] != *value1
; i
++);
1607 for (i
++; i
< 16 && !(irq
->map
& (1<<xtab
[i
])); i
++);
1609 if (irq
->res
&& irq
->res
->alt
) {
1610 if ((err
= isapnp_alternative_switch(cfg
, irq
->res
, irq
->res
->alt
))<0)
1616 *value1
= *value2
= xtab
[i
];
1618 } while (isapnp_check_interrupt(cfg
, *value1
, idx
));
1622 static int isapnp_check_dma(struct isapnp_cfgtmp
*cfg
, int dma
, int idx
)
1625 struct pci_dev
*dev
;
1627 /* Some machines allow DMA 0, but others don't. In fact on some
1628 boxes DMA 0 is the memory refresh. Play safe */
1630 if (dma
< 1 || dma
== 4 || dma
> 7)
1632 for (i
= 0; i
< 8; i
++) {
1633 if (isapnp_reserve_dma
[i
] == dma
)
1636 isapnp_for_each_dev(dev
) {
1638 if (dev
->dma_resource
[0].start
== dma
|| dev
->dma_resource
[1].start
== dma
)
1642 if (request_dma(dma
, "isapnp"))
1645 for (i
= 0; i
< 2; i
++) {
1648 if (!cfg
->result
.dma_resource
[i
].flags
||
1649 (cfg
->result
.dma_resource
[i
].flags
& IORESOURCE_AUTO
))
1651 if (cfg
->result
.dma_resource
[i
].start
== dma
)
1657 static int isapnp_valid_dma(struct isapnp_cfgtmp
*cfg
, int idx
)
1660 unsigned long *value1
, *value2
;
1661 struct isapnp_dma
*dma
;
1663 if (!cfg
|| idx
< 0 || idx
> 1)
1665 if (!(cfg
->result
.dma_resource
[idx
].flags
& IORESOURCE_AUTO
)) /* don't touch */
1668 dma
= cfg
->dma
[idx
];
1671 value1
= &cfg
->result
.dma_resource
[idx
].start
;
1672 value2
= &cfg
->result
.dma_resource
[idx
].end
;
1673 if (cfg
->result
.dma_resource
[idx
].flags
& IORESOURCE_AUTO
) {
1674 for (i
= 0; i
< 8 && !(dma
->map
& (1<<i
)); i
++);
1677 cfg
->result
.dma_resource
[idx
].flags
&= ~IORESOURCE_AUTO
;
1678 if (!isapnp_check_dma(cfg
, *value1
= *value2
= i
, idx
))
1682 for (i
= *value1
+ 1; i
< 8 && !(dma
->map
& (1<<i
)); i
++);
1684 if (dma
->res
&& dma
->res
->alt
) {
1685 if ((err
= isapnp_alternative_switch(cfg
, dma
->res
, dma
->res
->alt
))<0)
1691 *value1
= *value2
= i
;
1693 } while (isapnp_check_dma(cfg
, *value1
, idx
));
1697 static int isapnp_check_mem(struct isapnp_cfgtmp
*cfg
, unsigned int addr
, unsigned int size
, int idx
)
1700 unsigned int raddr
, rsize
;
1701 struct isapnp_mem
*xmem
;
1702 struct pci_dev
*dev
;
1704 for (i
= 0; i
< 8; i
++) {
1705 raddr
= (unsigned int)isapnp_reserve_mem
[i
<< 1];
1706 rsize
= (unsigned int)isapnp_reserve_mem
[(i
<< 1) + 1];
1707 if (addr
>= raddr
&& addr
< raddr
+ rsize
)
1709 if (addr
+ size
> raddr
&& addr
+ size
< (raddr
+ rsize
) - 1)
1711 if (__check_region(&iomem_resource
, addr
, size
))
1714 isapnp_for_each_dev(dev
) {
1716 for (tmp
= 0; tmp
< 4; tmp
++) {
1717 if (dev
->resource
[tmp
].flags
) {
1718 raddr
= dev
->resource
[tmp
+ 8].start
;
1719 rsize
= (dev
->resource
[tmp
+ 8].end
- raddr
) + 1;
1720 if (addr
>= raddr
&& addr
< raddr
+ rsize
)
1722 if (addr
+ size
> raddr
&& addr
+ size
< (raddr
+ rsize
) - 1)
1728 for (i
= 0; i
< 4; i
++) {
1729 unsigned int flags
= cfg
->request
->resource
[i
+ 8].flags
;
1734 tmp
= cfg
->result
.resource
[i
+ 8].start
;
1735 if (flags
& IORESOURCE_AUTO
) { /* auto */
1739 if (cfg
->result
.resource
[i
+ 8].flags
& IORESOURCE_AUTO
)
1741 if (tmp
+ xmem
->size
>= addr
&& tmp
<= addr
+ xmem
->size
)
1747 xmem
= isapnp_find_mem(cfg
->request
, i
);
1750 if (tmp
+ xmem
->size
>= addr
&& tmp
<= addr
+ xmem
->size
)
1756 static int isapnp_valid_mem(struct isapnp_cfgtmp
*cfg
, int idx
)
1759 unsigned long *value1
, *value2
;
1760 struct isapnp_mem
*mem
;
1762 if (!cfg
|| idx
< 0 || idx
> 3)
1764 if (!(cfg
->result
.resource
[idx
+ 8].flags
& IORESOURCE_AUTO
)) /* don't touch */
1767 mem
= cfg
->mem
[idx
];
1770 value1
= &cfg
->result
.resource
[idx
+ 8].start
;
1771 value2
= &cfg
->result
.resource
[idx
+ 8].end
;
1772 if (cfg
->result
.resource
[idx
+ 8].flags
& IORESOURCE_AUTO
) {
1773 cfg
->result
.resource
[idx
+ 8].flags
&= ~IORESOURCE_AUTO
;
1775 *value2
= mem
->min
+ mem
->size
- 1;
1776 if (!isapnp_check_mem(cfg
, *value1
, mem
->size
, idx
))
1780 *value1
+= mem
->align
;
1781 *value2
= *value1
+ mem
->size
- 1;
1782 if (*value1
> mem
->max
|| !mem
->align
) {
1783 if (mem
->res
&& mem
->res
->alt
) {
1784 if ((err
= isapnp_alternative_switch(cfg
, mem
->res
, mem
->res
->alt
))<0)
1790 } while (isapnp_check_mem(cfg
, *value1
, mem
->size
, idx
));
1794 static int isapnp_check_valid(struct isapnp_cfgtmp
*cfg
)
1798 for (tmp
= 0; tmp
< 8; tmp
++)
1799 if (cfg
->result
.resource
[tmp
].flags
& IORESOURCE_AUTO
)
1801 for (tmp
= 0; tmp
< 2; tmp
++)
1802 if (cfg
->result
.irq_resource
[tmp
].flags
& IORESOURCE_AUTO
)
1804 for (tmp
= 0; tmp
< 2; tmp
++)
1805 if (cfg
->result
.dma_resource
[tmp
].flags
& IORESOURCE_AUTO
)
1807 for (tmp
= 0; tmp
< 4; tmp
++)
1808 if (cfg
->result
.resource
[tmp
+ 8].flags
& IORESOURCE_AUTO
)
1813 static int isapnp_config_activate(struct pci_dev
*dev
)
1815 struct isapnp_cfgtmp cfg
;
1816 int tmp
, fauto
, err
;
1822 memset(&cfg
, 0, sizeof(cfg
));
1824 memcpy(&cfg
.result
, dev
, sizeof(struct pci_dev
));
1825 /* check if all values are set, otherwise try auto-configuration */
1826 for (tmp
= fauto
= 0; !fauto
&& tmp
< 8; tmp
++) {
1827 if (dev
->resource
[tmp
].flags
& IORESOURCE_AUTO
)
1830 for (tmp
= 0; !fauto
&& tmp
< 2; tmp
++) {
1831 if (dev
->irq_resource
[tmp
].flags
& IORESOURCE_AUTO
)
1834 for (tmp
= 0; !fauto
&& tmp
< 2; tmp
++) {
1835 if (dev
->dma_resource
[tmp
].flags
& IORESOURCE_AUTO
)
1838 for (tmp
= 0; !fauto
&& tmp
< 4; tmp
++) {
1839 if (dev
->resource
[tmp
+ 8].flags
& IORESOURCE_AUTO
)
1844 /* set variables to initial values */
1845 if ((err
= isapnp_alternative_switch(&cfg
, NULL
, NULL
))<0)
1847 /* find first valid configuration */
1850 for (tmp
= 0; tmp
< 8 && cfg
.result
.resource
[tmp
].flags
; tmp
++)
1851 if ((err
= isapnp_valid_port(&cfg
, tmp
))<0)
1853 for (tmp
= 0; tmp
< 2 && cfg
.result
.irq_resource
[tmp
].flags
; tmp
++)
1854 if ((err
= isapnp_valid_irq(&cfg
, tmp
))<0)
1856 for (tmp
= 0; tmp
< 2 && cfg
.result
.dma_resource
[tmp
].flags
; tmp
++)
1857 if ((err
= isapnp_valid_dma(&cfg
, tmp
))<0)
1859 for (tmp
= 0; tmp
< 4 && cfg
.result
.resource
[tmp
+ 8].flags
; tmp
++)
1860 if ((err
= isapnp_valid_mem(&cfg
, tmp
))<0)
1862 } while (isapnp_check_valid(&cfg
)<0 && fauto
++ < 20);
1866 /* we have valid configuration, try configure hardware */
1867 isapnp_cfg_begin(dev
->bus
->number
, dev
->devfn
);
1869 dev
->irq_resource
[0] = cfg
.result
.irq_resource
[0];
1870 dev
->irq_resource
[1] = cfg
.result
.irq_resource
[1];
1871 dev
->dma_resource
[0] = cfg
.result
.dma_resource
[0];
1872 dev
->dma_resource
[1] = cfg
.result
.dma_resource
[1];
1873 for (tmp
= 0; tmp
< 12; tmp
++) {
1874 dev
->resource
[tmp
] = cfg
.result
.resource
[tmp
];
1876 for (tmp
= 0; tmp
< 8 && dev
->resource
[tmp
].flags
; tmp
++)
1877 isapnp_write_word(ISAPNP_CFG_PORT
+(tmp
<<1), dev
->resource
[tmp
].start
);
1878 for (tmp
= 0; tmp
< 2 && dev
->irq_resource
[tmp
].flags
; tmp
++) {
1879 int irq
= dev
->irq_resource
[tmp
].start
;
1882 isapnp_write_byte(ISAPNP_CFG_IRQ
+(tmp
<<1), irq
);
1884 for (tmp
= 0; tmp
< 2 && dev
->dma_resource
[tmp
].flags
; tmp
++)
1885 isapnp_write_byte(ISAPNP_CFG_DMA
+tmp
, dev
->dma_resource
[tmp
].start
);
1886 for (tmp
= 0; tmp
< 4 && dev
->resource
[tmp
+8].flags
; tmp
++)
1887 isapnp_write_word(ISAPNP_CFG_MEM
+(tmp
<<2), (dev
->resource
[tmp
+ 8].start
>> 8) & 0xffff);
1888 isapnp_activate(dev
->devfn
);
1893 static int isapnp_config_deactivate(struct pci_dev
*dev
)
1895 if (!dev
|| !dev
->active
)
1897 isapnp_cfg_begin(dev
->bus
->number
, dev
->devfn
);
1898 isapnp_deactivate(dev
->devfn
);
1904 void isapnp_resource_change(struct resource
*resource
,
1905 unsigned long start
,
1908 if (resource
== NULL
)
1910 resource
->flags
&= ~IORESOURCE_AUTO
;
1911 resource
->start
= start
;
1912 resource
->end
= start
+ size
- 1;
1921 static void isapnp_free_port(struct isapnp_port
*port
)
1923 struct isapnp_port
*next
;
1932 static void isapnp_free_irq(struct isapnp_irq
*irq
)
1934 struct isapnp_irq
*next
;
1943 static void isapnp_free_dma(struct isapnp_dma
*dma
)
1945 struct isapnp_dma
*next
;
1954 static void isapnp_free_mem(struct isapnp_mem
*mem
)
1956 struct isapnp_mem
*next
;
1965 static void isapnp_free_mem32(struct isapnp_mem32
*mem32
)
1967 struct isapnp_mem32
*next
;
1976 static void isapnp_free_resources(struct isapnp_resources
*resources
, int alt
)
1978 struct isapnp_resources
*next
;
1981 next
= alt
? resources
->alt
: resources
->next
;
1982 isapnp_free_port(resources
->port
);
1983 isapnp_free_irq(resources
->irq
);
1984 isapnp_free_dma(resources
->dma
);
1985 isapnp_free_mem(resources
->mem
);
1986 isapnp_free_mem32(resources
->mem32
);
1987 if (!alt
&& resources
->alt
)
1988 isapnp_free_resources(resources
->alt
, 1);
1994 static void isapnp_free_card(struct pci_bus
*card
)
1996 while (!list_empty(&card
->devices
)) {
1997 struct list_head
*list
= card
->devices
.next
;
1998 struct pci_dev
*dev
= pci_dev_b(list
);
2000 isapnp_free_resources((struct isapnp_resources
*)dev
->sysdata
, 0);
2008 static void isapnp_free_all_resources(void)
2010 #ifdef ISAPNP_REGION_OK
2011 release_resource(pidxr_res
);
2013 release_resource(pnpwrp_res
);
2014 if (isapnp_rdp
>= 0x203 && isapnp_rdp
<= 0x3ff)
2015 release_resource(isapnp_rdp_res
);
2017 while (!list_empty(&isapnp_cards
)) {
2018 struct list_head
*list
= isapnp_cards
.next
;
2020 isapnp_free_card(pci_bus_b(list
));
2022 #ifdef CONFIG_PROC_FS
2028 static int __init
isapnp_do_reserve_irq(int irq
)
2032 if (irq
< 0 || irq
> 15)
2034 for (i
= 0; i
< 16; i
++) {
2035 if (isapnp_reserve_irq
[i
] == irq
)
2038 for (i
= 0; i
< 16; i
++) {
2039 if (isapnp_reserve_irq
[i
] < 0) {
2040 isapnp_reserve_irq
[i
] = irq
;
2042 printk("IRQ %i is reserved now.\n", irq
);
2052 static void __init
isapnp_pci_init(void)
2054 struct pci_dev
*dev
;
2056 pci_for_each_dev(dev
) {
2058 printk("PCI: reserved IRQ: %i\n", dev
->irq
);
2061 isapnp_do_reserve_irq(dev
->irq
);
2065 #endif /* CONFIG_PCI */
2067 EXPORT_SYMBOL(isapnp_present
);
2068 EXPORT_SYMBOL(isapnp_cfg_begin
);
2069 EXPORT_SYMBOL(isapnp_cfg_end
);
2070 EXPORT_SYMBOL(isapnp_read_byte
);
2071 EXPORT_SYMBOL(isapnp_read_word
);
2072 EXPORT_SYMBOL(isapnp_read_dword
);
2073 EXPORT_SYMBOL(isapnp_write_byte
);
2074 EXPORT_SYMBOL(isapnp_write_word
);
2075 EXPORT_SYMBOL(isapnp_write_dword
);
2076 EXPORT_SYMBOL(isapnp_wake
);
2077 EXPORT_SYMBOL(isapnp_device
);
2078 EXPORT_SYMBOL(isapnp_activate
);
2079 EXPORT_SYMBOL(isapnp_deactivate
);
2080 EXPORT_SYMBOL(isapnp_find_card
);
2081 EXPORT_SYMBOL(isapnp_find_dev
);
2082 EXPORT_SYMBOL(isapnp_resource_change
);
2084 int __init
isapnp_init(void)
2087 struct pci_bus
*card
;
2089 if (isapnp_disable
) {
2090 isapnp_detected
= 0;
2091 printk("isapnp: ISA Plug & Play support disabled\n");
2094 #ifdef ISAPNP_REGION_OK
2095 pidxr_res
=request_region(_PIDXR
, 1, "isapnp index");
2097 printk("isapnp: Index Register 0x%x already used\n", _PIDXR
);
2101 pnpwrp_res
=request_region(_PNPWRP
, 1, "isapnp write");
2103 printk("isapnp: Write Data Register 0x%x already used\n", _PNPWRP
);
2108 * Print a message. The existing ISAPnP code is hanging machines
2109 * so let the user know where.
2112 printk("isapnp: Scanning for Pnp cards...\n");
2113 if (isapnp_rdp
>= 0x203 && isapnp_rdp
<= 0x3ff) {
2115 isapnp_rdp_res
=request_region(isapnp_rdp
, 1, "isapnp read");
2116 if(!isapnp_rdp_res
) {
2117 printk("isapnp: Read Data Register 0x%x already used\n", isapnp_rdp
);
2122 isapnp_detected
= 1;
2123 if (isapnp_rdp
< 0x203 || isapnp_rdp
> 0x3ff) {
2124 cards
= isapnp_isolate();
2126 (isapnp_rdp
< 0x203 || isapnp_rdp
> 0x3ff)) {
2127 isapnp_free_all_resources();
2128 isapnp_detected
= 0;
2129 printk("isapnp: No Plug & Play device found\n");
2132 isapnp_rdp_res
=request_region(isapnp_rdp
, 1, "isapnp read");
2134 isapnp_build_device_list();
2137 isapnp_for_each_card(card
) {
2139 if (isapnp_verbose
) {
2140 struct list_head
*devlist
;
2141 printk( "isapnp: Card '%s'\n", card
->name
[0]?card
->name
:"Unknown");
2142 if (isapnp_verbose
< 2)
2144 for (devlist
= card
->devices
.next
; devlist
!= &card
->devices
; devlist
= devlist
->next
) {
2145 struct pci_dev
*dev
= pci_dev_b(devlist
);
2146 printk("isapnp: Device '%s'\n", dev
->name
[0]?card
->name
:"Unknown");
2151 printk("isapnp: %i Plug & Play card%s detected total\n", cards
, cards
>1?"s":"");
2153 printk("isapnp: No Plug & Play card found\n");
2156 if (!isapnp_skip_pci_scan
)
2159 #ifdef CONFIG_PROC_FS
2167 int init_module(void)
2169 return isapnp_init();
2172 void cleanup_module(void)
2174 if (isapnp_detected
)
2175 isapnp_free_all_resources();