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>
23 * 2000-06-14 Added isapnp_probe_devs() and isapnp_activate_dev()
24 * Christoph Hellwig <hch@caldera.de>
27 #include <linux/config.h>
28 #include <linux/version.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/errno.h>
32 #include <linux/ioport.h>
33 #include <linux/string.h>
34 #include <linux/malloc.h>
35 #include <linux/delay.h>
39 #include <linux/pci.h>
40 #include <linux/init.h>
41 #include <linux/isapnp.h>
43 LIST_HEAD(isapnp_cards
);
44 LIST_HEAD(isapnp_devices
);
47 #define ISAPNP_REGION_OK
53 struct resource
*pidxr_res
;
54 struct resource
*pnpwrp_res
;
55 struct resource
*isapnp_rdp_res
;
57 int isapnp_disable
; /* Disable ISA PnP */
58 int isapnp_rdp
; /* Read Data Port */
59 int isapnp_reset
= 1; /* reset all PnP cards (deactivate) */
60 int isapnp_skip_pci_scan
; /* skip PCI resource scanning */
61 int isapnp_verbose
= 1; /* verbose mode */
62 int isapnp_reserve_irq
[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some IRQ */
63 int isapnp_reserve_dma
[8] = { [0 ... 7] = -1 }; /* reserve (don't use) some DMA */
64 int isapnp_reserve_io
[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some I/O region */
65 int isapnp_reserve_mem
[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some memory region */
67 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
68 MODULE_DESCRIPTION("Generic ISA Plug & Play support");
69 MODULE_PARM(isapnp_disable
, "i");
70 MODULE_PARM_DESC(isapnp_disable
, "ISA Plug & Play disable");
71 MODULE_PARM(isapnp_rdp
, "i");
72 MODULE_PARM_DESC(isapnp_rdp
, "ISA Plug & Play read data port");
73 MODULE_PARM(isapnp_reset
, "i");
74 MODULE_PARM_DESC(isapnp_reset
, "ISA Plug & Play reset all cards");
75 MODULE_PARM(isapnp_skip_pci_scan
, "i");
76 MODULE_PARM_DESC(isapnp_skip_pci_scan
, "ISA Plug & Play skip PCI resource scanning");
77 MODULE_PARM(isapnp_verbose
, "i");
78 MODULE_PARM_DESC(isapnp_verbose
, "ISA Plug & Play verbose mode");
79 MODULE_PARM(isapnp_reserve_irq
, "1-16i");
80 MODULE_PARM_DESC(isapnp_reserve_irq
, "ISA Plug & Play - reserve IRQ line(s)");
81 MODULE_PARM(isapnp_reserve_dma
, "1-8i");
82 MODULE_PARM_DESC(isapnp_reserve_dma
, "ISA Plug & Play - reserve DMA channel(s)");
83 MODULE_PARM(isapnp_reserve_io
, "1-16i");
84 MODULE_PARM_DESC(isapnp_reserve_io
, "ISA Plug & Play - reserve I/O region(s) - port,size");
85 MODULE_PARM(isapnp_reserve_mem
, "1-16i");
86 MODULE_PARM_DESC(isapnp_reserve_mem
, "ISA Plug & Play - reserve memory region(s) - address,size");
92 #define _STAG_PNPVERNO 0x01
93 #define _STAG_LOGDEVID 0x02
94 #define _STAG_COMPATDEVID 0x03
95 #define _STAG_IRQ 0x04
96 #define _STAG_DMA 0x05
97 #define _STAG_STARTDEP 0x06
98 #define _STAG_ENDDEP 0x07
99 #define _STAG_IOPORT 0x08
100 #define _STAG_FIXEDIO 0x09
101 #define _STAG_VENDOR 0x0e
102 #define _STAG_END 0x0f
104 #define _LTAG_MEMRANGE 0x81
105 #define _LTAG_ANSISTR 0x82
106 #define _LTAG_UNICODESTR 0x83
107 #define _LTAG_VENDOR 0x84
108 #define _LTAG_MEM32RANGE 0x85
109 #define _LTAG_FIXEDMEM32RANGE 0x86
111 static unsigned char isapnp_checksum_value
;
112 static DECLARE_MUTEX(isapnp_cfg_mutex
);
113 static int isapnp_detected
;
115 /* some prototypes */
117 static int isapnp_config_prepare(struct pci_dev
*dev
);
118 static int isapnp_config_activate(struct pci_dev
*dev
);
119 static int isapnp_config_deactivate(struct pci_dev
*dev
);
121 static inline void write_data(unsigned char x
)
126 static inline void write_address(unsigned char x
)
132 static inline unsigned char read_data(void)
134 unsigned char val
= inb(isapnp_rdp
);
138 unsigned char isapnp_read_byte(unsigned char idx
)
144 unsigned short isapnp_read_word(unsigned char idx
)
148 val
= isapnp_read_byte(idx
);
149 val
= (val
<< 8) + isapnp_read_byte(idx
+1);
153 unsigned int isapnp_read_dword(unsigned char idx
)
157 val
= isapnp_read_byte(idx
);
158 val
= (val
<< 8) + isapnp_read_byte(idx
+1);
159 val
= (val
<< 8) + isapnp_read_byte(idx
+2);
160 val
= (val
<< 8) + isapnp_read_byte(idx
+3);
164 void isapnp_write_byte(unsigned char idx
, unsigned char val
)
170 void isapnp_write_word(unsigned char idx
, unsigned short val
)
172 isapnp_write_byte(idx
, val
>> 8);
173 isapnp_write_byte(idx
+1, val
);
176 void isapnp_write_dword(unsigned char idx
, unsigned int val
)
178 isapnp_write_byte(idx
, val
>> 24);
179 isapnp_write_byte(idx
+1, val
>> 16);
180 isapnp_write_byte(idx
+2, val
>> 8);
181 isapnp_write_byte(idx
+3, val
);
184 void *isapnp_alloc(long size
)
188 result
= kmalloc(size
, GFP_KERNEL
);
191 memset(result
, 0, size
);
195 static void isapnp_key(void)
197 unsigned char code
= 0x6a, msb
;
206 for (i
= 1; i
< 32; i
++) {
207 msb
= ((code
& 0x01) ^ ((code
& 0x02) >> 1)) << 7;
208 code
= (code
>> 1) | msb
;
213 /* place all pnp cards in wait-for-key state */
214 static void isapnp_wait(void)
216 isapnp_write_byte(0x02, 0x02);
219 void isapnp_wake(unsigned char csn
)
221 isapnp_write_byte(0x03, csn
);
224 void isapnp_device(unsigned char logdev
)
226 isapnp_write_byte(0x07, logdev
);
229 void isapnp_activate(unsigned char logdev
)
231 isapnp_device(logdev
);
232 isapnp_write_byte(ISAPNP_CFG_ACTIVATE
, 1);
236 void isapnp_deactivate(unsigned char logdev
)
238 isapnp_device(logdev
);
239 isapnp_write_byte(ISAPNP_CFG_ACTIVATE
, 0);
243 static void __init
isapnp_peek(unsigned char *data
, int bytes
)
248 for (i
= 1; i
<= bytes
; i
++) {
249 for (j
= 0; j
< 20; j
++) {
250 d
= isapnp_read_byte(0x05);
260 d
= isapnp_read_byte(0x04); /* PRESDI */
261 isapnp_checksum_value
+= d
;
267 #define RDP_STEP 32 /* minimum is 4 */
269 static int isapnp_next_rdp(void)
271 int rdp
= isapnp_rdp
;
272 while (rdp
<= 0x3ff) {
274 * We cannot use NE2000 probe spaces for ISAPnP or we
275 * will lock up machines.
277 if ((rdp
< 0x280 || rdp
> 0x380) && !check_region(rdp
, 1))
287 /* Set read port address */
288 static inline void isapnp_set_rdp(void)
290 isapnp_write_byte(0x00, isapnp_rdp
>> 2);
295 * Perform an isolation. The port selection code now tries to avoid
296 * "dangerous to read" ports.
299 static int __init
isapnp_isolate_rdp_select(void)
304 /* Control: reset CSN and conditionally everything else too */
305 isapnp_write_byte(0x02, isapnp_reset
? 0x05 : 0x04);
312 if (isapnp_next_rdp() < 0) {
325 * Isolate (assign uniqued CSN) to all ISA PnP devices.
328 static int __init
isapnp_isolate(void)
330 unsigned char checksum
= 0x6a;
331 unsigned char chksum
= 0x00;
332 unsigned char bit
= 0x00;
339 if (isapnp_isolate_rdp_select() < 0)
343 for (i
= 1; i
<= 64; i
++) {
344 data
= read_data() << 8;
346 data
= data
| read_data();
350 checksum
= ((((checksum
^ (checksum
>> 1)) & 0x01) ^ bit
) << 7) | (checksum
>> 1);
353 for (i
= 65; i
<= 72; i
++) {
354 data
= read_data() << 8;
356 data
= data
| read_data();
359 chksum
|= (1 << (i
- 65));
361 if (checksum
!= 0x00 && checksum
== chksum
) {
364 isapnp_write_byte(0x06, csn
);
374 if (iteration
== 1) {
375 isapnp_rdp
+= RDP_STEP
;
376 if (isapnp_isolate_rdp_select() < 0)
378 } else if (iteration
> 1) {
391 * Read one tag from stream.
394 static int __init
isapnp_read_tag(unsigned char *type
, unsigned short *size
)
396 unsigned char tag
, tmp
[2];
398 isapnp_peek(&tag
, 1);
399 if (tag
== 0) /* invalid tag */
401 if (tag
& 0x80) { /* large item */
404 *size
= (tmp
[1] << 8) | tmp
[0];
406 *type
= (tag
>> 3) & 0x0f;
410 printk("tag = 0x%x, type = 0x%x, size = %i\n", tag
, *type
, *size
);
412 if (type
== 0) /* wrong type */
414 if (*type
== 0xff && *size
== 0xffff) /* probably invalid data */
420 * Skip specified number of bytes from stream.
423 static void __init
isapnp_skip_bytes(int count
)
425 isapnp_peek(NULL
, count
);
429 * Parse logical device tag.
432 static struct pci_dev
* __init
isapnp_parse_device(struct pci_bus
*card
, int size
, int number
)
434 unsigned char tmp
[6];
437 isapnp_peek(tmp
, size
);
438 dev
= isapnp_alloc(sizeof(struct pci_dev
));
441 dev
->dma_mask
= 0x00ffffff;
443 dev
->vendor
= (tmp
[1] << 8) | tmp
[0];
444 dev
->device
= (tmp
[3] << 8) | tmp
[2];
448 dev
->regs
|= tmp
[5] << 8;
449 dev
->prepare
= isapnp_config_prepare
;
450 dev
->activate
= isapnp_config_activate
;
451 dev
->deactivate
= isapnp_config_deactivate
;
456 * Build new resources structure
459 static struct isapnp_resources
* __init
isapnp_build_resources(struct pci_dev
*dev
, int dependent
)
461 struct isapnp_resources
*res
, *ptr
, *ptra
;
463 res
= isapnp_alloc(sizeof(struct isapnp_resources
));
467 ptr
= (struct isapnp_resources
*)dev
->sysdata
;
468 while (ptr
&& ptr
->next
)
470 if (ptr
&& ptr
->dependent
&& dependent
) { /* add to another list */
472 while (ptra
&& ptra
->alt
)
485 res
->priority
= dependent
& 0xff;
486 if (res
->priority
> ISAPNP_RES_PRIORITY_FUNCTIONAL
)
487 res
->priority
= ISAPNP_RES_PRIORITY_INVALID
;
490 res
->priority
= ISAPNP_RES_PRIORITY_PREFERRED
;
497 * Add IRQ resource to resources list.
500 static void __init
isapnp_add_irq_resource(struct pci_dev
*dev
,
501 struct isapnp_resources
**res
,
502 int dependent
, int size
)
504 unsigned char tmp
[3];
506 struct isapnp_irq
*irq
, *ptr
;
508 isapnp_peek(tmp
, size
);
509 irq
= isapnp_alloc(sizeof(struct isapnp_irq
));
513 *res
= isapnp_build_resources(dev
, dependent
);
519 irq
->map
= (tmp
[1] << 8) | tmp
[0];
523 irq
->flags
= IORESOURCE_IRQ_HIGHEDGE
;
526 while (ptr
&& ptr
->next
)
534 if (irq
->map
& (1<<i
))
535 pcibios_penalize_isa_irq(i
);
540 * Add DMA resource to resources list.
543 static void __init
isapnp_add_dma_resource(struct pci_dev
*dev
,
544 struct isapnp_resources
**res
,
545 int dependent
, int size
)
547 unsigned char tmp
[2];
548 struct isapnp_dma
*dma
, *ptr
;
550 isapnp_peek(tmp
, size
);
551 dma
= isapnp_alloc(sizeof(struct isapnp_dma
));
555 *res
= isapnp_build_resources(dev
, dependent
);
565 while (ptr
&& ptr
->next
)
574 * Add port resource to resources list.
577 static void __init
isapnp_add_port_resource(struct pci_dev
*dev
,
578 struct isapnp_resources
**res
,
579 int dependent
, int size
)
581 unsigned char tmp
[7];
582 struct isapnp_port
*port
, *ptr
;
584 isapnp_peek(tmp
, size
);
585 port
= isapnp_alloc(sizeof(struct isapnp_port
));
589 *res
= isapnp_build_resources(dev
, dependent
);
595 port
->min
= (tmp
[2] << 8) | tmp
[1];
596 port
->max
= (tmp
[4] << 8) | tmp
[3];
597 port
->align
= tmp
[5];
599 port
->flags
= tmp
[0] ? ISAPNP_PORT_FLAG_16BITADDR
: 0;
602 while (ptr
&& ptr
->next
)
611 * Add fixed port resource to resources list.
614 static void __init
isapnp_add_fixed_port_resource(struct pci_dev
*dev
,
615 struct isapnp_resources
**res
,
616 int dependent
, int size
)
618 unsigned char tmp
[3];
619 struct isapnp_port
*port
, *ptr
;
621 isapnp_peek(tmp
, size
);
622 port
= isapnp_alloc(sizeof(struct isapnp_port
));
626 *res
= isapnp_build_resources(dev
, dependent
);
632 port
->min
= port
->max
= (tmp
[1] << 8) | tmp
[0];
635 port
->flags
= ISAPNP_PORT_FLAG_FIXED
;
638 while (ptr
&& ptr
->next
)
647 * Add memory resource to resources list.
650 static void __init
isapnp_add_mem_resource(struct pci_dev
*dev
,
651 struct isapnp_resources
**res
,
652 int dependent
, int size
)
654 unsigned char tmp
[9];
655 struct isapnp_mem
*mem
, *ptr
;
657 isapnp_peek(tmp
, size
);
658 mem
= isapnp_alloc(sizeof(struct isapnp_mem
));
662 *res
= isapnp_build_resources(dev
, dependent
);
668 mem
->min
= ((tmp
[2] << 8) | tmp
[1]) << 8;
669 mem
->max
= ((tmp
[4] << 8) | tmp
[3]) << 8;
670 mem
->align
= (tmp
[6] << 8) | tmp
[5];
671 mem
->size
= ((tmp
[8] << 8) | tmp
[7]) << 8;
675 while (ptr
&& ptr
->next
)
684 * Add 32-bit memory resource to resources list.
687 static void __init
isapnp_add_mem32_resource(struct pci_dev
*dev
,
688 struct isapnp_resources
**res
,
689 int dependent
, int size
)
691 unsigned char tmp
[17];
692 struct isapnp_mem32
*mem32
, *ptr
;
694 isapnp_peek(tmp
, size
);
695 mem32
= isapnp_alloc(sizeof(struct isapnp_mem32
));
699 *res
= isapnp_build_resources(dev
, dependent
);
705 memcpy(mem32
->data
, tmp
, 17);
708 while (ptr
&& ptr
->next
)
713 (*res
)->mem32
= mem32
;
717 * Add 32-bit fixed memory resource to resources list.
720 static void __init
isapnp_add_fixed_mem32_resource(struct pci_dev
*dev
,
721 struct isapnp_resources
**res
,
722 int dependent
, int size
)
724 unsigned char tmp
[17];
725 struct isapnp_mem32
*mem32
, *ptr
;
727 isapnp_peek(tmp
, size
);
728 mem32
= isapnp_alloc(sizeof(struct isapnp_mem32
));
732 *res
= isapnp_build_resources(dev
, dependent
);
738 memcpy(mem32
->data
, tmp
, 17);
741 while (ptr
&& ptr
->next
)
746 (*res
)->mem32
= mem32
;
750 * Parse card name for ISA PnP device.
754 isapnp_parse_name(char *name
, unsigned int name_max
, unsigned short *size
)
756 if (name
[0] == '\0') {
757 unsigned short size1
= *size
>= name_max
? (name_max
- 1) : *size
;
758 isapnp_peek(name
, size1
);
762 /* clean whitespace from end of string */
763 while (size1
> 0 && name
[--size1
] == ' ')
769 * Parse resource map for logical device.
772 static int __init
isapnp_create_device(struct pci_bus
*card
,
775 int number
= 0, skip
= 0, dependent
= 0, compat
= 0;
776 unsigned char type
, tmp
[17];
778 struct isapnp_resources
*res
= NULL
;
780 if ((dev
= isapnp_parse_device(card
, size
, number
++)) == NULL
)
782 list_add(&dev
->bus_list
, &card
->devices
);
783 list_add_tail(&dev
->global_list
, &isapnp_devices
);
785 if (isapnp_read_tag(&type
, &size
)<0)
787 if (skip
&& type
!= _STAG_LOGDEVID
&& type
!= _STAG_END
)
791 if (size
>= 5 && size
<= 6) {
792 isapnp_config_prepare(dev
);
793 if ((dev
= isapnp_parse_device(card
, size
, number
++)) == NULL
)
795 list_add_tail(&dev
->bus_list
, &card
->devices
);
796 list_add_tail(&dev
->global_list
, &isapnp_devices
);
806 case _STAG_COMPATDEVID
:
807 if (size
== 4 && compat
< DEVICE_COUNT_COMPATIBLE
) {
809 dev
->vendor_compatible
[compat
] = (tmp
[1] << 8) | tmp
[0];
810 dev
->device_compatible
[compat
] = (tmp
[3] << 8) | tmp
[2];
816 if (size
< 2 || size
> 3)
818 isapnp_add_irq_resource(dev
, &res
, dependent
, size
);
824 isapnp_add_dma_resource(dev
, &res
, dependent
, size
);
831 dependent
= 0x100 | ISAPNP_RES_PRIORITY_ACCEPTABLE
;
833 isapnp_peek(tmp
, size
);
834 dependent
= 0x100 | tmp
[0];
847 isapnp_add_port_resource(dev
, &res
, dependent
, size
);
853 isapnp_add_fixed_port_resource(dev
, &res
, dependent
, size
);
861 isapnp_add_mem_resource(dev
, &res
, dependent
, size
);
865 isapnp_parse_name(dev
->name
, sizeof(dev
->name
), &size
);
867 case _LTAG_UNICODESTR
:
868 /* silently ignore */
869 /* who use unicode for hardware identification? */
873 case _LTAG_MEM32RANGE
:
876 isapnp_add_mem32_resource(dev
, &res
, dependent
, size
);
879 case _LTAG_FIXEDMEM32RANGE
:
882 isapnp_add_fixed_mem32_resource(dev
, &res
, dependent
, size
);
887 isapnp_skip_bytes(size
);
890 printk("isapnp: unexpected or unknown tag type 0x%x for logical device %i (device %i), ignored\n", type
, dev
->devfn
, card
->number
);
894 isapnp_skip_bytes(size
);
896 isapnp_config_prepare(dev
);
901 * Parse resource map for ISA PnP card.
904 static void __init
isapnp_parse_resource_map(struct pci_bus
*card
)
906 unsigned char type
, tmp
[17];
910 if (isapnp_read_tag(&type
, &size
)<0)
917 card
->pnpver
= tmp
[0];
918 card
->productver
= tmp
[1];
922 if (size
>= 5 && size
<= 6) {
923 if (isapnp_create_device(card
, size
)==1)
931 isapnp_parse_name(card
->name
, sizeof(card
->name
), &size
);
933 case _LTAG_UNICODESTR
:
934 /* silently ignore */
935 /* who use unicode for hardware identification? */
941 isapnp_skip_bytes(size
);
944 printk("isapnp: unexpected or unknown tag type 0x%x for device %i, ignored\n", type
, card
->number
);
948 isapnp_skip_bytes(size
);
953 * Compute ISA PnP checksum for first eight bytes.
956 static unsigned char __init
isapnp_checksum(unsigned char *data
)
959 unsigned char checksum
= 0x6a, bit
, b
;
961 for (i
= 0; i
< 8; i
++) {
963 for (j
= 0; j
< 8; j
++) {
967 checksum
= ((((checksum
^ (checksum
>> 1)) & 0x01) ^ bit
) << 7) | (checksum
>> 1);
974 * Build device list for all present ISA PnP devices.
977 static int __init
isapnp_build_device_list(void)
980 unsigned char header
[9], checksum
;
981 struct pci_bus
*card
;
986 for (csn
= 1; csn
<= 10; csn
++) {
988 isapnp_peek(header
, 9);
989 checksum
= isapnp_checksum(header
);
991 printk("vendor: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
992 header
[0], header
[1], header
[2], header
[3],
993 header
[4], header
[5], header
[6], header
[7], header
[8]);
994 printk("checksum = 0x%x\n", checksum
);
996 if (checksum
== 0x00 || checksum
!= header
[8]) /* not valid CSN */
998 if ((card
= isapnp_alloc(sizeof(struct pci_bus
))) == NULL
)
1001 card
->vendor
= (header
[1] << 8) | header
[0];
1002 card
->device
= (header
[3] << 8) | header
[2];
1003 card
->serial
= (header
[7] << 24) | (header
[6] << 16) | (header
[5] << 8) | header
[4];
1004 isapnp_checksum_value
= 0x00;
1005 INIT_LIST_HEAD(&card
->children
);
1006 INIT_LIST_HEAD(&card
->devices
);
1007 isapnp_parse_resource_map(card
);
1008 if (isapnp_checksum_value
!= 0x00)
1009 printk("isapnp: checksum for device %i is not valid (0x%x)\n", csn
, isapnp_checksum_value
);
1010 card
->checksum
= isapnp_checksum_value
;
1012 list_add_tail(&card
->node
, &isapnp_cards
);
1014 isapnp_for_each_dev(dev
) {
1015 isapnp_fixup_device(dev
);
1021 * Basic configuration routines.
1024 int isapnp_present(void)
1026 return !list_empty(&isapnp_devices
);
1029 int isapnp_cfg_begin(int csn
, int logdev
)
1031 if (csn
< 1 || csn
> 10 || logdev
> 10)
1034 down(&isapnp_cfg_mutex
);
1038 #if 1 /* to avoid malfunction when the isapnptools package is used */
1040 udelay(1000); /* delay 1000us */
1041 write_address(0x01);
1042 udelay(1000); /* delay 1000us */
1045 isapnp_device(logdev
);
1049 int isapnp_cfg_end(void)
1052 up(&isapnp_cfg_mutex
);
1061 static struct isapnp_port
*isapnp_find_port(struct pci_dev
*dev
, int index
)
1063 struct isapnp_resources
*res
;
1064 struct isapnp_port
*port
;
1066 if (!dev
|| index
< 0 || index
> 7)
1068 for (res
= (struct isapnp_resources
*)dev
->sysdata
; res
; res
= res
->next
) {
1069 for (port
= res
->port
; port
; port
= port
->next
) {
1078 struct isapnp_irq
*isapnp_find_irq(struct pci_dev
*dev
, int index
)
1080 struct isapnp_resources
*res
, *resa
;
1081 struct isapnp_irq
*irq
;
1082 int index1
, index2
, index3
;
1084 if (!dev
|| index
< 0 || index
> 7)
1086 for (res
= (struct isapnp_resources
*)dev
->sysdata
; res
; res
= res
->next
) {
1088 for (resa
= res
; resa
; resa
= resa
->alt
) {
1091 for (irq
= resa
->irq
; irq
; irq
= irq
->next
) {
1097 if (index3
< index2
)
1105 struct isapnp_dma
*isapnp_find_dma(struct pci_dev
*dev
, int index
)
1107 struct isapnp_resources
*res
;
1108 struct isapnp_dma
*dma
;
1110 if (!dev
|| index
< 0 || index
> 7)
1112 for (res
= (struct isapnp_resources
*)dev
->sysdata
; res
; res
= res
->next
) {
1113 for (dma
= res
->dma
; dma
; dma
= dma
->next
) {
1122 struct isapnp_mem
*isapnp_find_mem(struct pci_dev
*dev
, int index
)
1124 struct isapnp_resources
*res
;
1125 struct isapnp_mem
*mem
;
1127 if (!dev
|| index
< 0 || index
> 7)
1129 for (res
= (struct isapnp_resources
*)dev
->sysdata
; res
; res
= res
->next
) {
1130 for (mem
= res
->mem
; mem
; mem
= mem
->next
) {
1139 struct isapnp_mem32
*isapnp_find_mem32(struct pci_dev
*dev
, int index
)
1141 struct isapnp_resources
*res
;
1142 struct isapnp_mem32
*mem32
;
1144 if (!dev
|| index
< 0 || index
> 7)
1146 for (res
= (struct isapnp_resources
*)dev
->sysdata
; res
; res
= res
->next
) {
1147 for (mem32
= res
->mem32
; mem32
; mem32
= mem32
->next
) {
1160 struct pci_bus
*isapnp_find_card(unsigned short vendor
,
1161 unsigned short device
,
1162 struct pci_bus
*from
)
1164 struct list_head
*list
;
1166 list
= isapnp_cards
.next
;
1168 list
= from
->node
.next
;
1170 while (list
!= &isapnp_cards
) {
1171 struct pci_bus
*card
= pci_bus_b(list
);
1172 if (card
->vendor
== vendor
&& card
->device
== device
)
1179 struct pci_dev
*isapnp_find_dev(struct pci_bus
*card
,
1180 unsigned short vendor
,
1181 unsigned short function
,
1182 struct pci_dev
*from
)
1184 if (card
== NULL
) { /* look for a logical device from all cards */
1185 struct list_head
*list
;
1187 list
= isapnp_devices
.next
;
1189 list
= from
->global_list
.next
;
1191 while (list
!= &isapnp_devices
) {
1193 struct pci_dev
*dev
= pci_dev_g(list
);
1195 if (dev
->vendor
== vendor
&& dev
->device
== function
)
1197 for (idx
= 0; idx
< DEVICE_COUNT_COMPATIBLE
; idx
++)
1198 if (dev
->vendor_compatible
[idx
] == vendor
&&
1199 dev
->device_compatible
[idx
] == function
)
1204 struct list_head
*list
;
1206 list
= card
->devices
.next
;
1208 list
= from
->bus_list
.next
;
1209 if (from
->bus
!= card
) /* something is wrong */
1212 while (list
!= &card
->devices
) {
1214 struct pci_dev
*dev
= pci_dev_b(list
);
1216 if (dev
->vendor
== vendor
&& dev
->device
== function
)
1218 for (idx
= 0; idx
< DEVICE_COUNT_COMPATIBLE
; idx
++)
1219 if (dev
->vendor_compatible
[idx
] == vendor
&&
1220 dev
->device_compatible
[idx
] == function
)
1228 static const struct isapnp_card_id
*
1229 isapnp_match_card(const struct isapnp_card_id
*ids
, struct pci_bus
*card
)
1233 while (ids
->card_vendor
|| ids
->card_device
) {
1234 if ((ids
->card_vendor
== ISAPNP_ANY_ID
|| ids
->card_vendor
== card
->vendor
) &&
1235 (ids
->card_device
== ISAPNP_ANY_ID
|| ids
->card_device
== card
->device
)) {
1236 for (idx
= 0; idx
< ISAPNP_CARD_DEVS
; idx
++) {
1237 if (ids
->devs
[idx
].vendor
== 0 &&
1238 ids
->devs
[idx
].function
== 0)
1240 if (isapnp_find_dev(card
,
1241 ids
->devs
[idx
].vendor
,
1242 ids
->devs
[idx
].function
,
1254 int isapnp_probe_cards(const struct isapnp_card_id
*ids
,
1255 int (*probe
)(struct pci_bus
*_card
,
1256 const struct isapnp_card_id
*_id
))
1258 struct pci_bus
*card
;
1259 const struct isapnp_card_id
*id
;
1262 if (ids
== NULL
|| probe
== NULL
)
1264 isapnp_for_each_card(card
) {
1265 id
= isapnp_match_card(ids
, card
);
1266 if (id
!= NULL
&& probe(card
, id
) >= 0)
1272 static const struct isapnp_device_id
*
1273 isapnp_match_dev(const struct isapnp_device_id
*ids
, struct pci_dev
*dev
)
1275 while (ids
->card_vendor
|| ids
->card_device
) {
1276 if ((ids
->card_vendor
== ISAPNP_ANY_ID
|| ids
->card_vendor
== dev
->bus
->vendor
) &&
1277 (ids
->card_device
== ISAPNP_ANY_ID
|| ids
->card_device
== dev
->bus
->device
) &&
1278 (ids
->vendor
== ISAPNP_ANY_ID
|| ids
->vendor
== dev
->vendor
) &&
1279 (ids
->function
== ISAPNP_ANY_ID
|| ids
->function
== dev
->device
))
1286 int isapnp_probe_devs(const struct isapnp_device_id
*ids
,
1287 int (*probe
)(struct pci_dev
*dev
,
1288 const struct isapnp_device_id
*id
))
1291 struct pci_dev
*dev
;
1292 const struct isapnp_device_id
*id
;
1295 if (ids
== NULL
|| probe
== NULL
)
1297 isapnp_for_each_dev(dev
) {
1298 id
= isapnp_match_dev(ids
, dev
);
1299 if (id
!= NULL
&& probe(dev
, id
) >= 0)
1305 int isapnp_activate_dev(struct pci_dev
*dev
, const char *name
)
1309 /* Device already active? Let's use it and inform the caller */
1313 if ((err
= dev
->activate(dev
)) < 0) {
1314 printk(KERN_ERR
"isapnp: config of %s failed (out of resources?)[%d]\n", name
, err
);
1315 dev
->deactivate(dev
);
1322 static unsigned int isapnp_dma_resource_flags(struct isapnp_dma
*dma
)
1324 return dma
->flags
| IORESOURCE_DMA
| IORESOURCE_AUTO
;
1327 static unsigned int isapnp_mem_resource_flags(struct isapnp_mem
*mem
)
1329 unsigned int result
;
1331 result
= mem
->flags
| IORESOURCE_MEM
| IORESOURCE_AUTO
;
1332 if (!(mem
->flags
& IORESOURCE_MEM_WRITEABLE
))
1333 result
|= IORESOURCE_READONLY
;
1334 if (mem
->flags
& IORESOURCE_MEM_CACHEABLE
)
1335 result
|= IORESOURCE_CACHEABLE
;
1336 if (mem
->flags
& IORESOURCE_MEM_RANGELENGTH
)
1337 result
|= IORESOURCE_RANGELENGTH
;
1338 if (mem
->flags
& IORESOURCE_MEM_SHADOWABLE
)
1339 result
|= IORESOURCE_SHADOWABLE
;
1343 static unsigned int isapnp_irq_resource_flags(struct isapnp_irq
*irq
)
1345 return irq
->flags
| IORESOURCE_IRQ
| IORESOURCE_AUTO
;
1348 static unsigned int isapnp_port_resource_flags(struct isapnp_port
*port
)
1350 return port
->flags
| IORESOURCE_IO
| IORESOURCE_AUTO
;
1353 static int isapnp_config_prepare(struct pci_dev
*dev
)
1355 struct isapnp_resources
*res
, *resa
;
1356 struct isapnp_port
*port
;
1357 struct isapnp_irq
*irq
;
1358 struct isapnp_dma
*dma
;
1359 struct isapnp_mem
*mem
;
1360 int port_count
, port_count1
;
1361 int irq_count
, irq_count1
;
1362 int dma_count
, dma_count1
;
1363 int mem_count
, mem_count1
;
1368 if (dev
->active
|| dev
->ro
)
1370 for (idx
= 0; idx
< DEVICE_COUNT_IRQ
; idx
++) {
1371 dev
->irq_resource
[idx
].name
= NULL
;
1372 dev
->irq_resource
[idx
].start
= 0;
1373 dev
->irq_resource
[idx
].end
= 0;
1374 dev
->irq_resource
[idx
].flags
= 0;
1376 for (idx
= 0; idx
< DEVICE_COUNT_DMA
; idx
++) {
1377 dev
->dma_resource
[idx
].name
= NULL
;
1378 dev
->dma_resource
[idx
].start
= 0;
1379 dev
->dma_resource
[idx
].end
= 0;
1380 dev
->dma_resource
[idx
].flags
= 0;
1382 for (idx
= 0; idx
< DEVICE_COUNT_RESOURCE
; idx
++) {
1383 dev
->resource
[idx
].name
= NULL
;
1384 dev
->resource
[idx
].start
= 0;
1385 dev
->resource
[idx
].end
= 0;
1386 dev
->resource
[idx
].flags
= 0;
1388 port_count
= irq_count
= dma_count
= mem_count
= 0;
1389 for (res
= (struct isapnp_resources
*)dev
->sysdata
; res
; res
= res
->next
) {
1390 port_count1
= irq_count1
= dma_count1
= mem_count1
= 0;
1391 for (resa
= res
; resa
; resa
= resa
->alt
) {
1392 for (port
= resa
->port
, idx
= 0; port
; port
= port
->next
, idx
++) {
1393 if (dev
->resource
[port_count
+ idx
].flags
== 0) {
1394 dev
->resource
[port_count
+ idx
].flags
= isapnp_port_resource_flags(port
);
1395 dev
->resource
[port_count
+ idx
].end
= port
->size
;
1398 if (port_count1
< idx
)
1400 for (irq
= resa
->irq
, idx
= 0; irq
; irq
= irq
->next
, idx
++) {
1401 int count
= irq_count
+ idx
;
1402 if (count
< DEVICE_COUNT_IRQ
) {
1403 if (dev
->irq_resource
[count
].flags
== 0) {
1404 dev
->irq_resource
[count
].flags
= isapnp_irq_resource_flags(irq
);
1409 if (irq_count1
< idx
)
1411 for (dma
= resa
->dma
, idx
= 0; dma
; dma
= dma
->next
, idx
++)
1412 if (dev
->dma_resource
[idx
].flags
== 0) {
1413 dev
->dma_resource
[idx
].flags
= isapnp_dma_resource_flags(dma
);
1415 if (dma_count1
< idx
)
1417 for (mem
= resa
->mem
, idx
= 0; mem
; mem
= mem
->next
, idx
++)
1418 if (dev
->resource
[mem_count
+ idx
+ 8].flags
== 0) {
1419 dev
->resource
[mem_count
+ idx
+ 8].flags
= isapnp_mem_resource_flags(mem
);
1421 if (mem_count1
< idx
)
1424 port_count
+= port_count1
;
1425 irq_count
+= irq_count1
;
1426 dma_count
+= dma_count1
;
1427 mem_count
+= mem_count1
;
1432 struct isapnp_cfgtmp
{
1433 struct isapnp_port
*port
[8];
1434 struct isapnp_irq
*irq
[2];
1435 struct isapnp_dma
*dma
[2];
1436 struct isapnp_mem
*mem
[4];
1437 struct pci_dev
*request
;
1438 struct pci_dev result
;
1441 static int isapnp_alternative_switch(struct isapnp_cfgtmp
*cfg
,
1442 struct isapnp_resources
*from
,
1443 struct isapnp_resources
*to
)
1446 struct isapnp_port
*port
;
1447 struct isapnp_irq
*irq
;
1448 struct isapnp_dma
*dma
;
1449 struct isapnp_mem
*mem
;
1453 /* process port settings */
1454 for (tmp
= 0; tmp
< 8; tmp
++) {
1455 if (!(cfg
->request
->resource
[tmp
].flags
& IORESOURCE_AUTO
))
1456 continue; /* don't touch */
1457 port
= cfg
->port
[tmp
];
1459 cfg
->port
[tmp
] = port
= isapnp_find_port(cfg
->request
, tmp
);
1463 if (from
&& port
->res
== from
) {
1464 while (port
->res
!= to
) {
1465 if (!port
->res
->alt
)
1467 port
= port
->res
->alt
->port
;
1468 for (tmp1
= tmp
; tmp1
> 0 && port
; tmp1
--)
1470 cfg
->port
[tmp
] = port
;
1473 cfg
->result
.resource
[tmp
].flags
= isapnp_port_resource_flags(port
);
1477 /* process irq settings */
1478 for (tmp
= 0; tmp
< 2; tmp
++) {
1479 if (!(cfg
->request
->irq_resource
[tmp
].flags
& IORESOURCE_AUTO
))
1480 continue; /* don't touch */
1481 irq
= cfg
->irq
[tmp
];
1483 cfg
->irq
[tmp
] = irq
= isapnp_find_irq(cfg
->request
, tmp
);
1487 if (from
&& irq
->res
== from
) {
1488 while (irq
->res
!= to
) {
1491 irq
= irq
->res
->alt
->irq
;
1492 for (tmp1
= tmp
; tmp1
> 0 && irq
; tmp1
--)
1494 cfg
->irq
[tmp
] = irq
;
1497 cfg
->result
.irq_resource
[tmp
].flags
= isapnp_irq_resource_flags(irq
);
1501 /* process dma settings */
1502 for (tmp
= 0; tmp
< 2; tmp
++) {
1503 if (!(cfg
->request
->dma_resource
[tmp
].flags
& IORESOURCE_AUTO
))
1504 continue; /* don't touch */
1505 dma
= cfg
->dma
[tmp
];
1507 cfg
->dma
[tmp
] = dma
= isapnp_find_dma(cfg
->request
, tmp
);
1511 if (from
&& dma
->res
== from
) {
1512 while (dma
->res
!= to
) {
1515 dma
= dma
->res
->alt
->dma
;
1516 for (tmp1
= tmp
; tmp1
> 0 && dma
; tmp1
--)
1518 cfg
->dma
[tmp
] = dma
;
1521 cfg
->result
.dma_resource
[tmp
].flags
= isapnp_dma_resource_flags(dma
);
1525 /* process memory settings */
1526 for (tmp
= 0; tmp
< 4; tmp
++) {
1527 if (!(cfg
->request
->resource
[tmp
+ 8].flags
& IORESOURCE_AUTO
))
1528 continue; /* don't touch */
1529 mem
= cfg
->mem
[tmp
];
1531 cfg
->mem
[tmp
] = mem
= isapnp_find_mem(cfg
->request
, tmp
);
1535 if (from
&& mem
->res
== from
) {
1536 while (mem
->res
!= to
) {
1539 mem
= mem
->res
->alt
->mem
;
1540 for (tmp1
= tmp
; tmp1
> 0 && mem
; tmp1
--)
1542 cfg
->mem
[tmp
] = mem
;
1545 cfg
->result
.resource
[tmp
+ 8].flags
= isapnp_mem_resource_flags(mem
);
1552 static int isapnp_check_port(struct isapnp_cfgtmp
*cfg
, int port
, int size
, int idx
)
1554 int i
, tmp
, rport
, rsize
;
1555 struct isapnp_port
*xport
;
1556 struct pci_dev
*dev
;
1558 if (check_region(port
, size
))
1560 for (i
= 0; i
< 8; i
++) {
1561 rport
= isapnp_reserve_io
[i
<< 1];
1562 rsize
= isapnp_reserve_io
[(i
<< 1) + 1];
1563 if (port
>= rport
&& port
< rport
+ rsize
)
1565 if (port
+ size
> rport
&& port
+ size
< (rport
+ rsize
) - 1)
1569 isapnp_for_each_dev(dev
) {
1571 for (tmp
= 0; tmp
< 8; tmp
++) {
1572 if (dev
->resource
[tmp
].flags
) {
1573 rport
= dev
->resource
[tmp
].start
;
1574 rsize
= (dev
->resource
[tmp
].end
- rport
) + 1;
1575 if (port
>= rport
&& port
< rport
+ rsize
)
1577 if (port
+ size
> rport
&& port
+ size
< (rport
+ rsize
) - 1)
1583 for (i
= 0; i
< 8; i
++) {
1587 flags
= cfg
->request
->resource
[i
].flags
;
1590 tmp
= cfg
->request
->resource
[i
].start
;
1591 if (flags
& IORESOURCE_AUTO
) { /* auto */
1592 xport
= cfg
->port
[i
];
1595 if (cfg
->result
.resource
[i
].flags
& IORESOURCE_AUTO
)
1597 tmp
= cfg
->result
.resource
[i
].start
;
1598 if (tmp
+ xport
->size
>= port
&& tmp
<= port
+ xport
->size
)
1604 xport
= isapnp_find_port(cfg
->request
, i
);
1607 if (tmp
+ xport
->size
>= port
&& tmp
<= port
+ xport
->size
)
1613 static int isapnp_valid_port(struct isapnp_cfgtmp
*cfg
, int idx
)
1616 unsigned long *value1
, *value2
;
1617 struct isapnp_port
*port
;
1619 if (!cfg
|| idx
< 0 || idx
> 7)
1621 if (!(cfg
->result
.resource
[idx
].flags
& IORESOURCE_AUTO
)) /* don't touch */
1624 port
= cfg
->port
[idx
];
1627 value1
= &cfg
->result
.resource
[idx
].start
;
1628 value2
= &cfg
->result
.resource
[idx
].end
;
1629 if (cfg
->result
.resource
[idx
].flags
& IORESOURCE_AUTO
) {
1630 cfg
->result
.resource
[idx
].flags
&= ~IORESOURCE_AUTO
;
1631 *value1
= port
->min
;
1632 *value2
= port
->min
+ port
->size
- 1;
1633 if (!isapnp_check_port(cfg
, *value1
, port
->size
, idx
))
1637 *value1
+= port
->align
;
1638 *value2
= *value1
+ port
->size
- 1;
1639 if (*value1
> port
->max
|| !port
->align
) {
1640 if (port
->res
&& port
->res
->alt
) {
1641 if ((err
= isapnp_alternative_switch(cfg
, port
->res
, port
->res
->alt
))<0)
1647 } while (isapnp_check_port(cfg
, *value1
, port
->size
, idx
));
1651 static void isapnp_test_handler(int irq
, void *dev_id
, struct pt_regs
*regs
)
1655 static int isapnp_check_interrupt(struct isapnp_cfgtmp
*cfg
, int irq
, int idx
)
1658 struct pci_dev
*dev
;
1660 if (irq
< 0 || irq
> 15)
1662 for (i
= 0; i
< 16; i
++) {
1663 if (isapnp_reserve_irq
[i
] == irq
)
1666 isapnp_for_each_dev(dev
) {
1668 if (dev
->irq_resource
[0].start
== irq
||
1669 dev
->irq_resource
[1].start
== irq
)
1674 if (!isapnp_skip_pci_scan
) {
1675 pci_for_each_dev(dev
) {
1676 if (dev
->irq
== irq
)
1681 if (request_irq(irq
, isapnp_test_handler
, SA_INTERRUPT
, "isapnp", NULL
))
1683 free_irq(irq
, NULL
);
1684 for (i
= 0; i
< DEVICE_COUNT_IRQ
; i
++) {
1687 if (!cfg
->result
.irq_resource
[i
].flags
)
1689 if (cfg
->result
.irq_resource
[i
].flags
& IORESOURCE_AUTO
)
1691 if (cfg
->result
.irq_resource
[i
].start
== irq
)
1697 static int isapnp_valid_irq(struct isapnp_cfgtmp
*cfg
, int idx
)
1699 /* IRQ priority: this table is good for i386 */
1700 static unsigned short xtab
[16] = {
1701 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
1704 unsigned long *value1
, *value2
;
1705 struct isapnp_irq
*irq
;
1707 if (!cfg
|| idx
< 0 || idx
> 1)
1709 if (!(cfg
->result
.irq_resource
[idx
].flags
& IORESOURCE_AUTO
))
1712 irq
= cfg
->irq
[idx
];
1715 value1
= &cfg
->result
.irq_resource
[idx
].start
;
1716 value2
= &cfg
->result
.irq_resource
[idx
].end
;
1717 if (cfg
->result
.irq_resource
[idx
].flags
& IORESOURCE_AUTO
) {
1718 for (i
= 0; i
< 16 && !(irq
->map
& (1<<xtab
[i
])); i
++);
1721 cfg
->result
.irq_resource
[idx
].flags
&= ~IORESOURCE_AUTO
;
1722 if (!isapnp_check_interrupt(cfg
, *value1
= *value2
= xtab
[i
], idx
))
1726 for (i
= 0; i
< 16 && xtab
[i
] != *value1
; i
++);
1727 for (i
++; i
< 16 && !(irq
->map
& (1<<xtab
[i
])); i
++);
1729 if (irq
->res
&& irq
->res
->alt
) {
1730 if ((err
= isapnp_alternative_switch(cfg
, irq
->res
, irq
->res
->alt
))<0)
1736 *value1
= *value2
= xtab
[i
];
1738 } while (isapnp_check_interrupt(cfg
, *value1
, idx
));
1742 static int isapnp_check_dma(struct isapnp_cfgtmp
*cfg
, int dma
, int idx
)
1745 struct pci_dev
*dev
;
1747 /* Some machines allow DMA 0, but others don't. In fact on some
1748 boxes DMA 0 is the memory refresh. Play safe */
1750 if (dma
< 1 || dma
== 4 || dma
> 7)
1752 for (i
= 0; i
< 8; i
++) {
1753 if (isapnp_reserve_dma
[i
] == dma
)
1756 isapnp_for_each_dev(dev
) {
1758 if (dev
->dma_resource
[0].start
== dma
|| dev
->dma_resource
[1].start
== dma
)
1762 if (request_dma(dma
, "isapnp"))
1765 for (i
= 0; i
< 2; i
++) {
1768 if (!cfg
->result
.dma_resource
[i
].flags
||
1769 (cfg
->result
.dma_resource
[i
].flags
& IORESOURCE_AUTO
))
1771 if (cfg
->result
.dma_resource
[i
].start
== dma
)
1777 static int isapnp_valid_dma(struct isapnp_cfgtmp
*cfg
, int idx
)
1780 unsigned long *value1
, *value2
;
1781 struct isapnp_dma
*dma
;
1783 if (!cfg
|| idx
< 0 || idx
> 1)
1785 if (!(cfg
->result
.dma_resource
[idx
].flags
& IORESOURCE_AUTO
)) /* don't touch */
1788 dma
= cfg
->dma
[idx
];
1791 value1
= &cfg
->result
.dma_resource
[idx
].start
;
1792 value2
= &cfg
->result
.dma_resource
[idx
].end
;
1793 if (cfg
->result
.dma_resource
[idx
].flags
& IORESOURCE_AUTO
) {
1794 for (i
= 0; i
< 8 && !(dma
->map
& (1<<i
)); i
++);
1797 cfg
->result
.dma_resource
[idx
].flags
&= ~IORESOURCE_AUTO
;
1798 if (!isapnp_check_dma(cfg
, *value1
= *value2
= i
, idx
))
1802 for (i
= *value1
+ 1; i
< 8 && !(dma
->map
& (1<<i
)); i
++);
1804 if (dma
->res
&& dma
->res
->alt
) {
1805 if ((err
= isapnp_alternative_switch(cfg
, dma
->res
, dma
->res
->alt
))<0)
1811 *value1
= *value2
= i
;
1813 } while (isapnp_check_dma(cfg
, *value1
, idx
));
1817 static int isapnp_check_mem(struct isapnp_cfgtmp
*cfg
, unsigned int addr
, unsigned int size
, int idx
)
1820 unsigned int raddr
, rsize
;
1821 struct isapnp_mem
*xmem
;
1822 struct pci_dev
*dev
;
1824 for (i
= 0; i
< 8; i
++) {
1825 raddr
= (unsigned int)isapnp_reserve_mem
[i
<< 1];
1826 rsize
= (unsigned int)isapnp_reserve_mem
[(i
<< 1) + 1];
1827 if (addr
>= raddr
&& addr
< raddr
+ rsize
)
1829 if (addr
+ size
> raddr
&& addr
+ size
< (raddr
+ rsize
) - 1)
1831 if (__check_region(&iomem_resource
, addr
, size
))
1834 isapnp_for_each_dev(dev
) {
1836 for (tmp
= 0; tmp
< 4; tmp
++) {
1837 if (dev
->resource
[tmp
].flags
) {
1838 raddr
= dev
->resource
[tmp
+ 8].start
;
1839 rsize
= (dev
->resource
[tmp
+ 8].end
- raddr
) + 1;
1840 if (addr
>= raddr
&& addr
< raddr
+ rsize
)
1842 if (addr
+ size
> raddr
&& addr
+ size
< (raddr
+ rsize
) - 1)
1848 for (i
= 0; i
< 4; i
++) {
1849 unsigned int flags
= cfg
->request
->resource
[i
+ 8].flags
;
1854 tmp
= cfg
->result
.resource
[i
+ 8].start
;
1855 if (flags
& IORESOURCE_AUTO
) { /* auto */
1859 if (cfg
->result
.resource
[i
+ 8].flags
& IORESOURCE_AUTO
)
1861 if (tmp
+ xmem
->size
>= addr
&& tmp
<= addr
+ xmem
->size
)
1867 xmem
= isapnp_find_mem(cfg
->request
, i
);
1870 if (tmp
+ xmem
->size
>= addr
&& tmp
<= addr
+ xmem
->size
)
1876 static int isapnp_valid_mem(struct isapnp_cfgtmp
*cfg
, int idx
)
1879 unsigned long *value1
, *value2
;
1880 struct isapnp_mem
*mem
;
1882 if (!cfg
|| idx
< 0 || idx
> 3)
1884 if (!(cfg
->result
.resource
[idx
+ 8].flags
& IORESOURCE_AUTO
)) /* don't touch */
1887 mem
= cfg
->mem
[idx
];
1890 value1
= &cfg
->result
.resource
[idx
+ 8].start
;
1891 value2
= &cfg
->result
.resource
[idx
+ 8].end
;
1892 if (cfg
->result
.resource
[idx
+ 8].flags
& IORESOURCE_AUTO
) {
1893 cfg
->result
.resource
[idx
+ 8].flags
&= ~IORESOURCE_AUTO
;
1895 *value2
= mem
->min
+ mem
->size
- 1;
1896 if (!isapnp_check_mem(cfg
, *value1
, mem
->size
, idx
))
1900 *value1
+= mem
->align
;
1901 *value2
= *value1
+ mem
->size
- 1;
1902 if (*value1
> mem
->max
|| !mem
->align
) {
1903 if (mem
->res
&& mem
->res
->alt
) {
1904 if ((err
= isapnp_alternative_switch(cfg
, mem
->res
, mem
->res
->alt
))<0)
1910 } while (isapnp_check_mem(cfg
, *value1
, mem
->size
, idx
));
1914 static int isapnp_check_valid(struct isapnp_cfgtmp
*cfg
)
1918 for (tmp
= 0; tmp
< 8; tmp
++)
1919 if (cfg
->result
.resource
[tmp
].flags
& IORESOURCE_AUTO
)
1921 for (tmp
= 0; tmp
< 2; tmp
++)
1922 if (cfg
->result
.irq_resource
[tmp
].flags
& IORESOURCE_AUTO
)
1924 for (tmp
= 0; tmp
< 2; tmp
++)
1925 if (cfg
->result
.dma_resource
[tmp
].flags
& IORESOURCE_AUTO
)
1927 for (tmp
= 0; tmp
< 4; tmp
++)
1928 if (cfg
->result
.resource
[tmp
+ 8].flags
& IORESOURCE_AUTO
)
1933 static int isapnp_config_activate(struct pci_dev
*dev
)
1935 struct isapnp_cfgtmp cfg
;
1936 int tmp
, fauto
, err
;
1942 memset(&cfg
, 0, sizeof(cfg
));
1944 memcpy(&cfg
.result
, dev
, sizeof(struct pci_dev
));
1945 /* check if all values are set, otherwise try auto-configuration */
1946 for (tmp
= fauto
= 0; !fauto
&& tmp
< 8; tmp
++) {
1947 if (dev
->resource
[tmp
].flags
& IORESOURCE_AUTO
)
1950 for (tmp
= 0; !fauto
&& tmp
< 2; tmp
++) {
1951 if (dev
->irq_resource
[tmp
].flags
& IORESOURCE_AUTO
)
1954 for (tmp
= 0; !fauto
&& tmp
< 2; tmp
++) {
1955 if (dev
->dma_resource
[tmp
].flags
& IORESOURCE_AUTO
)
1958 for (tmp
= 0; !fauto
&& tmp
< 4; tmp
++) {
1959 if (dev
->resource
[tmp
+ 8].flags
& IORESOURCE_AUTO
)
1964 /* set variables to initial values */
1965 if ((err
= isapnp_alternative_switch(&cfg
, NULL
, NULL
))<0)
1967 /* find first valid configuration */
1970 for (tmp
= 0; tmp
< 8 && cfg
.result
.resource
[tmp
].flags
; tmp
++)
1971 if ((err
= isapnp_valid_port(&cfg
, tmp
))<0)
1973 for (tmp
= 0; tmp
< 2 && cfg
.result
.irq_resource
[tmp
].flags
; tmp
++)
1974 if ((err
= isapnp_valid_irq(&cfg
, tmp
))<0)
1976 for (tmp
= 0; tmp
< 2 && cfg
.result
.dma_resource
[tmp
].flags
; tmp
++)
1977 if ((err
= isapnp_valid_dma(&cfg
, tmp
))<0)
1979 for (tmp
= 0; tmp
< 4 && cfg
.result
.resource
[tmp
+ 8].flags
; tmp
++)
1980 if ((err
= isapnp_valid_mem(&cfg
, tmp
))<0)
1982 } while (isapnp_check_valid(&cfg
)<0 && fauto
++ < 20);
1986 /* we have valid configuration, try configure hardware */
1987 isapnp_cfg_begin(dev
->bus
->number
, dev
->devfn
);
1989 dev
->irq_resource
[0] = cfg
.result
.irq_resource
[0];
1990 dev
->irq_resource
[1] = cfg
.result
.irq_resource
[1];
1991 dev
->dma_resource
[0] = cfg
.result
.dma_resource
[0];
1992 dev
->dma_resource
[1] = cfg
.result
.dma_resource
[1];
1993 for (tmp
= 0; tmp
< 12; tmp
++) {
1994 dev
->resource
[tmp
] = cfg
.result
.resource
[tmp
];
1996 for (tmp
= 0; tmp
< 8 && dev
->resource
[tmp
].flags
; tmp
++)
1997 isapnp_write_word(ISAPNP_CFG_PORT
+(tmp
<<1), dev
->resource
[tmp
].start
);
1998 for (tmp
= 0; tmp
< 2 && dev
->irq_resource
[tmp
].flags
; tmp
++) {
1999 int irq
= dev
->irq_resource
[tmp
].start
;
2002 isapnp_write_byte(ISAPNP_CFG_IRQ
+(tmp
<<1), irq
);
2004 for (tmp
= 0; tmp
< 2 && dev
->dma_resource
[tmp
].flags
; tmp
++)
2005 isapnp_write_byte(ISAPNP_CFG_DMA
+tmp
, dev
->dma_resource
[tmp
].start
);
2006 for (tmp
= 0; tmp
< 4 && dev
->resource
[tmp
+8].flags
; tmp
++)
2007 isapnp_write_word(ISAPNP_CFG_MEM
+(tmp
<<2), (dev
->resource
[tmp
+ 8].start
>> 8) & 0xffff);
2008 isapnp_activate(dev
->devfn
);
2013 static int isapnp_config_deactivate(struct pci_dev
*dev
)
2015 if (!dev
|| !dev
->active
)
2017 isapnp_cfg_begin(dev
->bus
->number
, dev
->devfn
);
2018 isapnp_deactivate(dev
->devfn
);
2024 void isapnp_resource_change(struct resource
*resource
,
2025 unsigned long start
,
2028 if (resource
== NULL
)
2030 resource
->flags
&= ~IORESOURCE_AUTO
;
2031 resource
->start
= start
;
2032 resource
->end
= start
+ size
- 1;
2041 static void isapnp_free_port(struct isapnp_port
*port
)
2043 struct isapnp_port
*next
;
2052 static void isapnp_free_irq(struct isapnp_irq
*irq
)
2054 struct isapnp_irq
*next
;
2063 static void isapnp_free_dma(struct isapnp_dma
*dma
)
2065 struct isapnp_dma
*next
;
2074 static void isapnp_free_mem(struct isapnp_mem
*mem
)
2076 struct isapnp_mem
*next
;
2085 static void isapnp_free_mem32(struct isapnp_mem32
*mem32
)
2087 struct isapnp_mem32
*next
;
2096 static void isapnp_free_resources(struct isapnp_resources
*resources
, int alt
)
2098 struct isapnp_resources
*next
;
2101 next
= alt
? resources
->alt
: resources
->next
;
2102 isapnp_free_port(resources
->port
);
2103 isapnp_free_irq(resources
->irq
);
2104 isapnp_free_dma(resources
->dma
);
2105 isapnp_free_mem(resources
->mem
);
2106 isapnp_free_mem32(resources
->mem32
);
2107 if (!alt
&& resources
->alt
)
2108 isapnp_free_resources(resources
->alt
, 1);
2114 static void isapnp_free_card(struct pci_bus
*card
)
2116 while (!list_empty(&card
->devices
)) {
2117 struct list_head
*list
= card
->devices
.next
;
2118 struct pci_dev
*dev
= pci_dev_b(list
);
2120 isapnp_free_resources((struct isapnp_resources
*)dev
->sysdata
, 0);
2128 static void isapnp_free_all_resources(void)
2130 #ifdef ISAPNP_REGION_OK
2132 release_resource(pidxr_res
);
2135 release_resource(pnpwrp_res
);
2136 if (isapnp_rdp
>= 0x203 && isapnp_rdp
<= 0x3ff && isapnp_rdp_res
)
2137 release_resource(isapnp_rdp_res
);
2139 #ifdef CONFIG_PROC_FS
2142 while (!list_empty(&isapnp_cards
)) {
2143 struct list_head
*list
= isapnp_cards
.next
;
2145 isapnp_free_card(pci_bus_b(list
));
2150 EXPORT_SYMBOL(isapnp_cards
);
2151 EXPORT_SYMBOL(isapnp_devices
);
2152 EXPORT_SYMBOL(isapnp_present
);
2153 EXPORT_SYMBOL(isapnp_cfg_begin
);
2154 EXPORT_SYMBOL(isapnp_cfg_end
);
2155 EXPORT_SYMBOL(isapnp_read_byte
);
2156 EXPORT_SYMBOL(isapnp_read_word
);
2157 EXPORT_SYMBOL(isapnp_read_dword
);
2158 EXPORT_SYMBOL(isapnp_write_byte
);
2159 EXPORT_SYMBOL(isapnp_write_word
);
2160 EXPORT_SYMBOL(isapnp_write_dword
);
2161 EXPORT_SYMBOL(isapnp_wake
);
2162 EXPORT_SYMBOL(isapnp_device
);
2163 EXPORT_SYMBOL(isapnp_activate
);
2164 EXPORT_SYMBOL(isapnp_deactivate
);
2165 EXPORT_SYMBOL(isapnp_find_card
);
2166 EXPORT_SYMBOL(isapnp_find_dev
);
2167 EXPORT_SYMBOL(isapnp_probe_cards
);
2168 EXPORT_SYMBOL(isapnp_probe_devs
);
2169 EXPORT_SYMBOL(isapnp_activate_dev
);
2170 EXPORT_SYMBOL(isapnp_resource_change
);
2172 int __init
isapnp_init(void)
2175 struct pci_bus
*card
;
2177 if (isapnp_disable
) {
2178 isapnp_detected
= 0;
2179 printk("isapnp: ISA Plug & Play support disabled\n");
2182 #ifdef ISAPNP_REGION_OK
2183 pidxr_res
=request_region(_PIDXR
, 1, "isapnp index");
2185 printk("isapnp: Index Register 0x%x already used\n", _PIDXR
);
2189 pnpwrp_res
=request_region(_PNPWRP
, 1, "isapnp write");
2191 printk("isapnp: Write Data Register 0x%x already used\n", _PNPWRP
);
2196 * Print a message. The existing ISAPnP code is hanging machines
2197 * so let the user know where.
2200 printk("isapnp: Scanning for Pnp cards...\n");
2201 if (isapnp_rdp
>= 0x203 && isapnp_rdp
<= 0x3ff) {
2203 isapnp_rdp_res
=request_region(isapnp_rdp
, 1, "isapnp read");
2204 if(!isapnp_rdp_res
) {
2205 printk("isapnp: Read Data Register 0x%x already used\n", isapnp_rdp
);
2210 isapnp_detected
= 1;
2211 if (isapnp_rdp
< 0x203 || isapnp_rdp
> 0x3ff) {
2212 cards
= isapnp_isolate();
2214 (isapnp_rdp
< 0x203 || isapnp_rdp
> 0x3ff)) {
2215 isapnp_free_all_resources();
2216 isapnp_detected
= 0;
2217 printk("isapnp: No Plug & Play device found\n");
2220 isapnp_rdp_res
=request_region(isapnp_rdp
, 1, "isapnp read");
2222 isapnp_build_device_list();
2225 isapnp_for_each_card(card
) {
2227 if (isapnp_verbose
) {
2228 struct list_head
*devlist
;
2229 printk( "isapnp: Card '%s'\n", card
->name
[0]?card
->name
:"Unknown");
2230 if (isapnp_verbose
< 2)
2232 for (devlist
= card
->devices
.next
; devlist
!= &card
->devices
; devlist
= devlist
->next
) {
2233 struct pci_dev
*dev
= pci_dev_b(devlist
);
2234 printk("isapnp: Device '%s'\n", dev
->name
[0]?card
->name
:"Unknown");
2239 printk("isapnp: %i Plug & Play card%s detected total\n", cards
, cards
>1?"s":"");
2241 printk("isapnp: No Plug & Play card found\n");
2243 #ifdef CONFIG_PROC_FS
2251 int init_module(void)
2253 return isapnp_init();
2256 void cleanup_module(void)
2258 if (isapnp_detected
)
2259 isapnp_free_all_resources();