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@infradead.org>
25 * 2001-06-03 Added release_region calls to correspond with
26 * request_region calls when a failure occurs. Also
27 * added KERN_* constants to printk() calls.
28 * 2001-11-07 Added isapnp_{,un}register_driver calls along the lines
29 * of the pci driver interface
30 * Kai Germaschewski <kai.germaschewski@gmx.de>
31 * 2002-06-06 Made the use of dma channel 0 configurable
32 * Gerald Teschl <gerald.teschl@univie.ac.at>
35 #include <linux/config.h>
36 #include <linux/version.h>
37 #include <linux/module.h>
38 #include <linux/kernel.h>
39 #include <linux/errno.h>
40 #include <linux/ioport.h>
41 #include <linux/string.h>
42 #include <linux/slab.h>
43 #include <linux/delay.h>
47 #include <linux/pci.h>
48 #include <linux/init.h>
49 #include <linux/isapnp.h>
51 LIST_HEAD(isapnp_cards);
52 LIST_HEAD(isapnp_devices);
55 #define ISAPNP_REGION_OK
61 int isapnp_disable; /* Disable ISA PnP */
62 int isapnp_rdp; /* Read Data Port */
63 int isapnp_reset = 1; /* reset all PnP cards (deactivate) */
64 int isapnp_allow_dma0 = -1; /* allow dma 0 during auto activation: -1=off (:default), 0=off (set by user), 1=on */
65 int isapnp_skip_pci_scan; /* skip PCI resource scanning */
66 int isapnp_verbose = 1; /* verbose mode */
67 int isapnp_reserve_irq[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some IRQ */
68 int isapnp_reserve_dma[8] = { [0 ... 7] = -1 }; /* reserve (don't use) some DMA */
69 int isapnp_reserve_io[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some I/O region */
70 int isapnp_reserve_mem[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some memory region */
72 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
73 MODULE_DESCRIPTION("Generic ISA Plug & Play support");
74 MODULE_PARM(isapnp_disable, "i");
75 MODULE_PARM_DESC(isapnp_disable, "ISA Plug & Play disable");
76 MODULE_PARM(isapnp_rdp, "i");
77 MODULE_PARM_DESC(isapnp_rdp, "ISA Plug & Play read data port");
78 MODULE_PARM(isapnp_reset, "i");
79 MODULE_PARM_DESC(isapnp_reset, "ISA Plug & Play reset all cards");
80 MODULE_PARM(isapnp_allow_dma0, "i");
81 MODULE_PARM_DESC(isapnp_allow_dma0, "Allow dma value 0 during auto activation");
82 MODULE_PARM(isapnp_skip_pci_scan, "i");
83 MODULE_PARM_DESC(isapnp_skip_pci_scan, "ISA Plug & Play skip PCI resource scanning");
84 MODULE_PARM(isapnp_verbose, "i");
85 MODULE_PARM_DESC(isapnp_verbose, "ISA Plug & Play verbose mode");
86 MODULE_PARM(isapnp_reserve_irq, "1-16i");
87 MODULE_PARM_DESC(isapnp_reserve_irq, "ISA Plug & Play - reserve IRQ line(s)");
88 MODULE_PARM(isapnp_reserve_dma, "1-8i");
89 MODULE_PARM_DESC(isapnp_reserve_dma, "ISA Plug & Play - reserve DMA channel(s)");
90 MODULE_PARM(isapnp_reserve_io, "1-16i");
91 MODULE_PARM_DESC(isapnp_reserve_io, "ISA Plug & Play - reserve I/O region(s) - port,size");
92 MODULE_PARM(isapnp_reserve_mem, "1-16i");
93 MODULE_PARM_DESC(isapnp_reserve_mem, "ISA Plug & Play - reserve memory region(s) - address,size");
94 MODULE_LICENSE("GPL");
100 #define _STAG_PNPVERNO 0x01
101 #define _STAG_LOGDEVID 0x02
102 #define _STAG_COMPATDEVID 0x03
103 #define _STAG_IRQ 0x04
104 #define _STAG_DMA 0x05
105 #define _STAG_STARTDEP 0x06
106 #define _STAG_ENDDEP 0x07
107 #define _STAG_IOPORT 0x08
108 #define _STAG_FIXEDIO 0x09
109 #define _STAG_VENDOR 0x0e
110 #define _STAG_END 0x0f
112 #define _LTAG_MEMRANGE 0x81
113 #define _LTAG_ANSISTR 0x82
114 #define _LTAG_UNICODESTR 0x83
115 #define _LTAG_VENDOR 0x84
116 #define _LTAG_MEM32RANGE 0x85
117 #define _LTAG_FIXEDMEM32RANGE 0x86
119 static unsigned char isapnp_checksum_value;
120 static DECLARE_MUTEX(isapnp_cfg_mutex);
121 static int isapnp_detected;
123 /* some prototypes */
125 static int isapnp_config_prepare(struct pci_dev *dev);
126 static int isapnp_config_activate(struct pci_dev *dev);
127 static int isapnp_config_deactivate(struct pci_dev *dev);
129 static inline void write_data(unsigned char x)
134 static inline void write_address(unsigned char x)
140 static inline unsigned char read_data(void)
142 unsigned char val = inb(isapnp_rdp);
146 unsigned char isapnp_read_byte(unsigned char idx)
152 unsigned short isapnp_read_word(unsigned char idx)
156 val = isapnp_read_byte(idx);
157 val = (val << 8) + isapnp_read_byte(idx+1);
161 unsigned int isapnp_read_dword(unsigned char idx)
165 val = isapnp_read_byte(idx);
166 val = (val << 8) + isapnp_read_byte(idx+1);
167 val = (val << 8) + isapnp_read_byte(idx+2);
168 val = (val << 8) + isapnp_read_byte(idx+3);
172 void isapnp_write_byte(unsigned char idx, unsigned char val)
178 void isapnp_write_word(unsigned char idx, unsigned short val)
180 isapnp_write_byte(idx, val >> 8);
181 isapnp_write_byte(idx+1, val);
184 void isapnp_write_dword(unsigned char idx, unsigned int val)
186 isapnp_write_byte(idx, val >> 24);
187 isapnp_write_byte(idx+1, val >> 16);
188 isapnp_write_byte(idx+2, val >> 8);
189 isapnp_write_byte(idx+3, val);
192 void *isapnp_alloc(long size)
196 result = kmalloc(size, GFP_KERNEL);
199 memset(result, 0, size);
203 static void isapnp_key(void)
205 unsigned char code = 0x6a, msb;
214 for (i = 1; i < 32; i++) {
215 msb = ((code & 0x01) ^ ((code & 0x02) >> 1)) << 7;
216 code = (code >> 1) | msb;
221 /* place all pnp cards in wait-for-key state */
222 static void isapnp_wait(void)
224 isapnp_write_byte(0x02, 0x02);
227 void isapnp_wake(unsigned char csn)
229 isapnp_write_byte(0x03, csn);
232 void isapnp_device(unsigned char logdev)
234 isapnp_write_byte(0x07, logdev);
237 void isapnp_activate(unsigned char logdev)
239 isapnp_device(logdev);
240 isapnp_write_byte(ISAPNP_CFG_ACTIVATE, 1);
244 void isapnp_deactivate(unsigned char logdev)
246 isapnp_device(logdev);
247 isapnp_write_byte(ISAPNP_CFG_ACTIVATE, 0);
251 static void __init isapnp_peek(unsigned char *data, int bytes)
256 for (i = 1; i <= bytes; i++) {
257 for (j = 0; j < 20; j++) {
258 d = isapnp_read_byte(0x05);
268 d = isapnp_read_byte(0x04); /* PRESDI */
269 isapnp_checksum_value += d;
275 #define RDP_STEP 32 /* minimum is 4 */
277 static int isapnp_next_rdp(void)
279 int rdp = isapnp_rdp;
280 while (rdp <= 0x3ff) {
282 * We cannot use NE2000 probe spaces for ISAPnP or we
283 * will lock up machines.
285 if ((rdp < 0x280 || rdp > 0x380) && !check_region(rdp, 1))
295 /* Set read port address */
296 static inline void isapnp_set_rdp(void)
298 isapnp_write_byte(0x00, isapnp_rdp >> 2);
303 * Perform an isolation. The port selection code now tries to avoid
304 * "dangerous to read" ports.
307 static int __init isapnp_isolate_rdp_select(void)
312 /* Control: reset CSN and conditionally everything else too */
313 isapnp_write_byte(0x02, isapnp_reset ? 0x05 : 0x04);
320 if (isapnp_next_rdp() < 0) {
333 * Isolate (assign uniqued CSN) to all ISA PnP devices.
336 static int __init isapnp_isolate(void)
338 unsigned char checksum = 0x6a;
339 unsigned char chksum = 0x00;
340 unsigned char bit = 0x00;
347 if (isapnp_isolate_rdp_select() < 0)
351 for (i = 1; i <= 64; i++) {
352 data = read_data() << 8;
354 data = data | read_data();
358 checksum = ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7) | (checksum >> 1);
361 for (i = 65; i <= 72; i++) {
362 data = read_data() << 8;
364 data = data | read_data();
367 chksum |= (1 << (i - 65));
369 if (checksum != 0x00 && checksum == chksum) {
372 isapnp_write_byte(0x06, csn);
382 if (iteration == 1) {
383 isapnp_rdp += RDP_STEP;
384 if (isapnp_isolate_rdp_select() < 0)
386 } else if (iteration > 1) {
399 * Read one tag from stream.
402 static int __init isapnp_read_tag(unsigned char *type, unsigned short *size)
404 unsigned char tag, tmp[2];
406 isapnp_peek(&tag, 1);
407 if (tag == 0) /* invalid tag */
409 if (tag & 0x80) { /* large item */
412 *size = (tmp[1] << 8) | tmp[0];
414 *type = (tag >> 3) & 0x0f;
418 printk(KERN_DEBUG "tag = 0x%x, type = 0x%x, size = %i\n", tag, *type, *size);
420 if (type == 0) /* wrong type */
422 if (*type == 0xff && *size == 0xffff) /* probably invalid data */
428 * Skip specified number of bytes from stream.
431 static void __init isapnp_skip_bytes(int count)
433 isapnp_peek(NULL, count);
437 * Parse logical device tag.
440 static struct pci_dev * __init isapnp_parse_device(struct pci_bus *card, int size, int number)
442 unsigned char tmp[6];
445 isapnp_peek(tmp, size);
446 dev = isapnp_alloc(sizeof(struct pci_dev));
449 dev->dma_mask = 0x00ffffff;
451 dev->vendor = (tmp[1] << 8) | tmp[0];
452 dev->device = (tmp[3] << 8) | tmp[2];
456 dev->regs |= tmp[5] << 8;
457 dev->prepare = isapnp_config_prepare;
458 dev->activate = isapnp_config_activate;
459 dev->deactivate = isapnp_config_deactivate;
464 * Build new resources structure
467 static struct isapnp_resources * __init isapnp_build_resources(struct pci_dev *dev, int dependent)
469 struct isapnp_resources *res, *ptr, *ptra;
471 res = isapnp_alloc(sizeof(struct isapnp_resources));
475 ptr = (struct isapnp_resources *)dev->sysdata;
476 while (ptr && ptr->next)
478 if (ptr && ptr->dependent && dependent) { /* add to another list */
480 while (ptra && ptra->alt)
493 res->priority = dependent & 0xff;
494 if (res->priority > ISAPNP_RES_PRIORITY_FUNCTIONAL)
495 res->priority = ISAPNP_RES_PRIORITY_INVALID;
498 res->priority = ISAPNP_RES_PRIORITY_PREFERRED;
505 * Add IRQ resource to resources list.
508 static void __init isapnp_add_irq_resource(struct pci_dev *dev,
509 struct isapnp_resources **res,
510 int dependent, int size)
512 unsigned char tmp[3];
514 struct isapnp_irq *irq, *ptr;
516 isapnp_peek(tmp, size);
517 irq = isapnp_alloc(sizeof(struct isapnp_irq));
521 *res = isapnp_build_resources(dev, dependent);
527 irq->map = (tmp[1] << 8) | tmp[0];
531 irq->flags = IORESOURCE_IRQ_HIGHEDGE;
534 while (ptr && ptr->next)
542 if (irq->map & (1<<i))
543 pcibios_penalize_isa_irq(i);
548 * Add DMA resource to resources list.
551 static void __init isapnp_add_dma_resource(struct pci_dev *dev,
552 struct isapnp_resources **res,
553 int dependent, int size)
555 unsigned char tmp[2];
556 struct isapnp_dma *dma, *ptr;
558 isapnp_peek(tmp, size);
559 dma = isapnp_alloc(sizeof(struct isapnp_dma));
563 *res = isapnp_build_resources(dev, dependent);
573 while (ptr && ptr->next)
582 * Add port resource to resources list.
585 static void __init isapnp_add_port_resource(struct pci_dev *dev,
586 struct isapnp_resources **res,
587 int dependent, int size)
589 unsigned char tmp[7];
590 struct isapnp_port *port, *ptr;
592 isapnp_peek(tmp, size);
593 port = isapnp_alloc(sizeof(struct isapnp_port));
597 *res = isapnp_build_resources(dev, dependent);
603 port->min = (tmp[2] << 8) | tmp[1];
604 port->max = (tmp[4] << 8) | tmp[3];
605 port->align = tmp[5];
607 port->flags = tmp[0] ? ISAPNP_PORT_FLAG_16BITADDR : 0;
610 while (ptr && ptr->next)
619 * Add fixed port resource to resources list.
622 static void __init isapnp_add_fixed_port_resource(struct pci_dev *dev,
623 struct isapnp_resources **res,
624 int dependent, int size)
626 unsigned char tmp[3];
627 struct isapnp_port *port, *ptr;
629 isapnp_peek(tmp, size);
630 port = isapnp_alloc(sizeof(struct isapnp_port));
634 *res = isapnp_build_resources(dev, dependent);
640 port->min = port->max = (tmp[1] << 8) | tmp[0];
643 port->flags = ISAPNP_PORT_FLAG_FIXED;
646 while (ptr && ptr->next)
655 * Add memory resource to resources list.
658 static void __init isapnp_add_mem_resource(struct pci_dev *dev,
659 struct isapnp_resources **res,
660 int dependent, int size)
662 unsigned char tmp[9];
663 struct isapnp_mem *mem, *ptr;
665 isapnp_peek(tmp, size);
666 mem = isapnp_alloc(sizeof(struct isapnp_mem));
670 *res = isapnp_build_resources(dev, dependent);
676 mem->min = ((tmp[2] << 8) | tmp[1]) << 8;
677 mem->max = ((tmp[4] << 8) | tmp[3]) << 8;
678 mem->align = (tmp[6] << 8) | tmp[5];
679 mem->size = ((tmp[8] << 8) | tmp[7]) << 8;
683 while (ptr && ptr->next)
692 * Add 32-bit memory resource to resources list.
695 static void __init isapnp_add_mem32_resource(struct pci_dev *dev,
696 struct isapnp_resources **res,
697 int dependent, int size)
699 unsigned char tmp[17];
700 struct isapnp_mem32 *mem32, *ptr;
702 isapnp_peek(tmp, size);
703 mem32 = isapnp_alloc(sizeof(struct isapnp_mem32));
707 *res = isapnp_build_resources(dev, dependent);
713 memcpy(mem32->data, tmp, 17);
716 while (ptr && ptr->next)
721 (*res)->mem32 = mem32;
725 * Add 32-bit fixed memory resource to resources list.
728 static void __init isapnp_add_fixed_mem32_resource(struct pci_dev *dev,
729 struct isapnp_resources **res,
730 int dependent, int size)
732 unsigned char tmp[17];
733 struct isapnp_mem32 *mem32, *ptr;
735 isapnp_peek(tmp, size);
736 mem32 = isapnp_alloc(sizeof(struct isapnp_mem32));
740 *res = isapnp_build_resources(dev, dependent);
746 memcpy(mem32->data, tmp, 17);
749 while (ptr && ptr->next)
754 (*res)->mem32 = mem32;
758 * Parse card name for ISA PnP device.
762 isapnp_parse_name(char *name, unsigned int name_max, unsigned short *size)
764 if (name[0] == '\0') {
765 unsigned short size1 = *size >= name_max ? (name_max - 1) : *size;
766 isapnp_peek(name, size1);
770 /* clean whitespace from end of string */
771 while (size1 > 0 && name[--size1] == ' ')
777 * Parse resource map for logical device.
780 static int __init isapnp_create_device(struct pci_bus *card,
783 int number = 0, skip = 0, dependent = 0, compat = 0;
784 unsigned char type, tmp[17];
786 struct isapnp_resources *res = NULL;
788 if ((dev = isapnp_parse_device(card, size, number++)) == NULL)
790 list_add(&dev->bus_list, &card->devices);
791 list_add_tail(&dev->global_list, &isapnp_devices);
793 if (isapnp_read_tag(&type, &size)<0)
795 if (skip && type != _STAG_LOGDEVID && type != _STAG_END)
799 if (size >= 5 && size <= 6) {
800 isapnp_config_prepare(dev);
801 if ((dev = isapnp_parse_device(card, size, number++)) == NULL)
803 list_add_tail(&dev->bus_list, &card->devices);
804 list_add_tail(&dev->global_list, &isapnp_devices);
814 case _STAG_COMPATDEVID:
815 if (size == 4 && compat < DEVICE_COUNT_COMPATIBLE) {
817 dev->vendor_compatible[compat] = (tmp[1] << 8) | tmp[0];
818 dev->device_compatible[compat] = (tmp[3] << 8) | tmp[2];
824 if (size < 2 || size > 3)
826 isapnp_add_irq_resource(dev, &res, dependent, size);
832 isapnp_add_dma_resource(dev, &res, dependent, size);
839 dependent = 0x100 | ISAPNP_RES_PRIORITY_ACCEPTABLE;
841 isapnp_peek(tmp, size);
842 dependent = 0x100 | tmp[0];
855 isapnp_add_port_resource(dev, &res, dependent, size);
861 isapnp_add_fixed_port_resource(dev, &res, dependent, size);
869 isapnp_add_mem_resource(dev, &res, dependent, size);
873 isapnp_parse_name(dev->name, sizeof(dev->name), &size);
875 case _LTAG_UNICODESTR:
876 /* silently ignore */
877 /* who use unicode for hardware identification? */
881 case _LTAG_MEM32RANGE:
884 isapnp_add_mem32_resource(dev, &res, dependent, size);
887 case _LTAG_FIXEDMEM32RANGE:
890 isapnp_add_fixed_mem32_resource(dev, &res, dependent, size);
895 isapnp_skip_bytes(size);
896 isapnp_config_prepare(dev);
899 printk(KERN_ERR "isapnp: unexpected or unknown tag type 0x%x for logical device %i (device %i), ignored\n", type, dev->devfn, card->number);
903 isapnp_skip_bytes(size);
905 isapnp_config_prepare(dev);
910 * Parse resource map for ISA PnP card.
913 static void __init isapnp_parse_resource_map(struct pci_bus *card)
915 unsigned char type, tmp[17];
919 if (isapnp_read_tag(&type, &size)<0)
926 card->pnpver = tmp[0];
927 card->productver = tmp[1];
931 if (size >= 5 && size <= 6) {
932 if (isapnp_create_device(card, size)==1)
940 isapnp_parse_name(card->name, sizeof(card->name), &size);
942 case _LTAG_UNICODESTR:
943 /* silently ignore */
944 /* who use unicode for hardware identification? */
950 isapnp_skip_bytes(size);
953 printk(KERN_ERR "isapnp: unexpected or unknown tag type 0x%x for device %i, ignored\n", type, card->number);
957 isapnp_skip_bytes(size);
962 * Compute ISA PnP checksum for first eight bytes.
965 static unsigned char __init isapnp_checksum(unsigned char *data)
968 unsigned char checksum = 0x6a, bit, b;
970 for (i = 0; i < 8; i++) {
972 for (j = 0; j < 8; j++) {
976 checksum = ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7) | (checksum >> 1);
983 * Build device list for all present ISA PnP devices.
986 static int __init isapnp_build_device_list(void)
989 unsigned char header[9], checksum;
990 struct pci_bus *card;
995 for (csn = 1; csn <= 10; csn++) {
997 isapnp_peek(header, 9);
998 checksum = isapnp_checksum(header);
1000 printk(KERN_DEBUG "vendor: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
1001 header[0], header[1], header[2], header[3],
1002 header[4], header[5], header[6], header[7], header[8]);
1003 printk(KERN_DEBUG "checksum = 0x%x\n", checksum);
1005 /* Don't be strict on the checksum, here !
1006 e.g. 'SCM SwapBox Plug and Play' has header[8]==0 (should be: b7)*/
1009 else if (checksum == 0x00 || checksum != header[8]) /* not valid CSN */
1011 if ((card = isapnp_alloc(sizeof(struct pci_bus))) == NULL)
1015 card->vendor = (header[1] << 8) | header[0];
1016 card->device = (header[3] << 8) | header[2];
1017 card->serial = (header[7] << 24) | (header[6] << 16) | (header[5] << 8) | header[4];
1018 isapnp_checksum_value = 0x00;
1019 INIT_LIST_HEAD(&card->children);
1020 INIT_LIST_HEAD(&card->devices);
1021 isapnp_parse_resource_map(card);
1022 if (isapnp_checksum_value != 0x00)
1023 printk(KERN_ERR "isapnp: checksum for device %i is not valid (0x%x)\n", csn, isapnp_checksum_value);
1024 card->checksum = isapnp_checksum_value;
1026 list_add_tail(&card->node, &isapnp_cards);
1028 isapnp_for_each_dev(dev) {
1029 isapnp_fixup_device(dev);
1035 * Basic configuration routines.
1038 int isapnp_present(void)
1040 return !list_empty(&isapnp_devices);
1043 int isapnp_cfg_begin(int csn, int logdev)
1045 if (csn < 1 || csn > 10 || logdev > 10)
1048 down(&isapnp_cfg_mutex);
1052 #if 1 /* to avoid malfunction when the isapnptools package is used */
1054 udelay(1000); /* delay 1000us */
1055 write_address(0x01);
1056 udelay(1000); /* delay 1000us */
1059 isapnp_device(logdev);
1063 int isapnp_cfg_end(void)
1066 up(&isapnp_cfg_mutex);
1075 static struct isapnp_port *isapnp_find_port(struct pci_dev *dev, int index)
1077 struct isapnp_resources *res;
1078 struct isapnp_port *port;
1080 if (!dev || index < 0 || index > 7)
1082 for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1083 for (port = res->port; port; port = port->next) {
1092 struct isapnp_irq *isapnp_find_irq(struct pci_dev *dev, int index)
1094 struct isapnp_resources *res, *resa;
1095 struct isapnp_irq *irq;
1096 int index1, index2, index3;
1098 if (!dev || index < 0 || index > 7)
1100 for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1102 for (resa = res; resa; resa = resa->alt) {
1105 for (irq = resa->irq; irq; irq = irq->next) {
1111 if (index3 < index2)
1119 struct isapnp_dma *isapnp_find_dma(struct pci_dev *dev, int index)
1121 struct isapnp_resources *res;
1122 struct isapnp_dma *dma;
1124 if (!dev || index < 0 || index > 7)
1126 for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1127 for (dma = res->dma; dma; dma = dma->next) {
1136 struct isapnp_mem *isapnp_find_mem(struct pci_dev *dev, int index)
1138 struct isapnp_resources *res;
1139 struct isapnp_mem *mem;
1141 if (!dev || index < 0 || index > 7)
1143 for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1144 for (mem = res->mem; mem; mem = mem->next) {
1153 struct isapnp_mem32 *isapnp_find_mem32(struct pci_dev *dev, int index)
1155 struct isapnp_resources *res;
1156 struct isapnp_mem32 *mem32;
1158 if (!dev || index < 0 || index > 7)
1160 for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1161 for (mem32 = res->mem32; mem32; mem32 = mem32->next) {
1174 struct pci_bus *isapnp_find_card(unsigned short vendor,
1175 unsigned short device,
1176 struct pci_bus *from)
1178 struct list_head *list;
1180 list = isapnp_cards.next;
1182 list = from->node.next;
1184 while (list != &isapnp_cards) {
1185 struct pci_bus *card = pci_bus_b(list);
1186 if (card->vendor == vendor && card->device == device)
1193 struct pci_dev *isapnp_find_dev(struct pci_bus *card,
1194 unsigned short vendor,
1195 unsigned short function,
1196 struct pci_dev *from)
1198 if (card == NULL) { /* look for a logical device from all cards */
1199 struct list_head *list;
1201 list = isapnp_devices.next;
1203 list = from->global_list.next;
1205 while (list != &isapnp_devices) {
1207 struct pci_dev *dev = pci_dev_g(list);
1209 if (dev->vendor == vendor && dev->device == function)
1211 for (idx = 0; idx < DEVICE_COUNT_COMPATIBLE; idx++)
1212 if (dev->vendor_compatible[idx] == vendor &&
1213 dev->device_compatible[idx] == function)
1218 struct list_head *list;
1220 list = card->devices.next;
1222 list = from->bus_list.next;
1223 if (from->bus != card) /* something is wrong */
1226 while (list != &card->devices) {
1228 struct pci_dev *dev = pci_dev_b(list);
1230 if (dev->vendor == vendor && dev->device == function)
1232 for (idx = 0; idx < DEVICE_COUNT_COMPATIBLE; idx++)
1233 if (dev->vendor_compatible[idx] == vendor &&
1234 dev->device_compatible[idx] == function)
1242 static const struct isapnp_card_id *
1243 isapnp_match_card(const struct isapnp_card_id *ids, struct pci_bus *card)
1247 while (ids->card_vendor || ids->card_device) {
1248 if ((ids->card_vendor == ISAPNP_ANY_ID || ids->card_vendor == card->vendor) &&
1249 (ids->card_device == ISAPNP_ANY_ID || ids->card_device == card->device)) {
1250 for (idx = 0; idx < ISAPNP_CARD_DEVS; idx++) {
1251 if (ids->devs[idx].vendor == 0 &&
1252 ids->devs[idx].function == 0)
1254 if (isapnp_find_dev(card,
1255 ids->devs[idx].vendor,
1256 ids->devs[idx].function,
1268 int isapnp_probe_cards(const struct isapnp_card_id *ids,
1269 int (*probe)(struct pci_bus *_card,
1270 const struct isapnp_card_id *_id))
1272 struct pci_bus *card;
1273 const struct isapnp_card_id *id;
1276 if (ids == NULL || probe == NULL)
1278 isapnp_for_each_card(card) {
1279 id = isapnp_match_card(ids, card);
1280 if (id != NULL && probe(card, id) >= 0)
1286 static const struct isapnp_device_id *
1287 isapnp_match_dev(const struct isapnp_device_id *ids, struct pci_dev *dev)
1289 while (ids->card_vendor || ids->card_device) {
1290 if ((ids->card_vendor == ISAPNP_ANY_ID || ids->card_vendor == dev->bus->vendor) &&
1291 (ids->card_device == ISAPNP_ANY_ID || ids->card_device == dev->bus->device) &&
1292 (ids->vendor == ISAPNP_ANY_ID || ids->vendor == dev->vendor) &&
1293 (ids->function == ISAPNP_ANY_ID || ids->function == dev->device))
1300 int isapnp_probe_devs(const struct isapnp_device_id *ids,
1301 int (*probe)(struct pci_dev *dev,
1302 const struct isapnp_device_id *id))
1305 struct pci_dev *dev;
1306 const struct isapnp_device_id *id;
1309 if (ids == NULL || probe == NULL)
1311 isapnp_for_each_dev(dev) {
1312 id = isapnp_match_dev(ids, dev);
1313 if (id != NULL && probe(dev, id) >= 0)
1319 int isapnp_activate_dev(struct pci_dev *dev, const char *name)
1323 /* Device already active? Let's use it and inform the caller */
1327 if ((err = dev->activate(dev)) < 0) {
1328 printk(KERN_ERR "isapnp: config of %s failed (out of resources?)[%d]\n", name, err);
1329 dev->deactivate(dev);
1336 static unsigned int isapnp_dma_resource_flags(struct isapnp_dma *dma)
1338 return dma->flags | IORESOURCE_DMA | IORESOURCE_AUTO;
1341 static unsigned int isapnp_mem_resource_flags(struct isapnp_mem *mem)
1343 unsigned int result;
1345 result = mem->flags | IORESOURCE_MEM | IORESOURCE_AUTO;
1346 if (!(mem->flags & IORESOURCE_MEM_WRITEABLE))
1347 result |= IORESOURCE_READONLY;
1348 if (mem->flags & IORESOURCE_MEM_CACHEABLE)
1349 result |= IORESOURCE_CACHEABLE;
1350 if (mem->flags & IORESOURCE_MEM_RANGELENGTH)
1351 result |= IORESOURCE_RANGELENGTH;
1352 if (mem->flags & IORESOURCE_MEM_SHADOWABLE)
1353 result |= IORESOURCE_SHADOWABLE;
1357 static unsigned int isapnp_irq_resource_flags(struct isapnp_irq *irq)
1359 return irq->flags | IORESOURCE_IRQ | IORESOURCE_AUTO;
1362 static unsigned int isapnp_port_resource_flags(struct isapnp_port *port)
1364 return port->flags | IORESOURCE_IO | IORESOURCE_AUTO;
1367 static int isapnp_config_prepare(struct pci_dev *dev)
1369 struct isapnp_resources *res, *resa;
1370 struct isapnp_port *port;
1371 struct isapnp_irq *irq;
1372 struct isapnp_dma *dma;
1373 struct isapnp_mem *mem;
1374 int port_count, port_count1;
1375 int irq_count, irq_count1;
1376 int dma_count, dma_count1;
1377 int mem_count, mem_count1;
1382 if (dev->active || dev->ro)
1384 for (idx = 0; idx < DEVICE_COUNT_IRQ; idx++) {
1385 dev->irq_resource[idx].name = NULL;
1386 dev->irq_resource[idx].start = 0;
1387 dev->irq_resource[idx].end = 0;
1388 dev->irq_resource[idx].flags = 0;
1390 for (idx = 0; idx < DEVICE_COUNT_DMA; idx++) {
1391 dev->dma_resource[idx].name = NULL;
1392 dev->dma_resource[idx].start = 0;
1393 dev->dma_resource[idx].end = 0;
1394 dev->dma_resource[idx].flags = 0;
1396 for (idx = 0; idx < DEVICE_COUNT_RESOURCE; idx++) {
1397 dev->resource[idx].name = NULL;
1398 dev->resource[idx].start = 0;
1399 dev->resource[idx].end = 0;
1400 dev->resource[idx].flags = 0;
1402 port_count = irq_count = dma_count = mem_count = 0;
1403 for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1404 port_count1 = irq_count1 = dma_count1 = mem_count1 = 0;
1405 for (resa = res; resa; resa = resa->alt) {
1406 for (port = resa->port, idx = 0; port; port = port->next, idx++) {
1407 if (dev->resource[port_count + idx].flags == 0) {
1408 dev->resource[port_count + idx].flags = isapnp_port_resource_flags(port);
1409 dev->resource[port_count + idx].end = port->size;
1412 if (port_count1 < idx)
1414 for (irq = resa->irq, idx = 0; irq; irq = irq->next, idx++) {
1415 int count = irq_count + idx;
1416 if (count < DEVICE_COUNT_IRQ) {
1417 if (dev->irq_resource[count].flags == 0) {
1418 dev->irq_resource[count].flags = isapnp_irq_resource_flags(irq);
1423 if (irq_count1 < idx)
1425 for (dma = resa->dma, idx = 0; dma; dma = dma->next, idx++)
1426 if (dev->dma_resource[idx].flags == 0) {
1427 dev->dma_resource[idx].flags = isapnp_dma_resource_flags(dma);
1429 if (dma_count1 < idx)
1431 for (mem = resa->mem, idx = 0; mem; mem = mem->next, idx++)
1432 if (dev->resource[mem_count + idx + 8].flags == 0) {
1433 dev->resource[mem_count + idx + 8].flags = isapnp_mem_resource_flags(mem);
1435 if (mem_count1 < idx)
1438 port_count += port_count1;
1439 irq_count += irq_count1;
1440 dma_count += dma_count1;
1441 mem_count += mem_count1;
1446 struct isapnp_cfgtmp {
1447 struct isapnp_port *port[8];
1448 struct isapnp_irq *irq[2];
1449 struct isapnp_dma *dma[2];
1450 struct isapnp_mem *mem[4];
1451 struct pci_dev *request;
1452 struct pci_dev result;
1455 static int isapnp_alternative_switch(struct isapnp_cfgtmp *cfg,
1456 struct isapnp_resources *from,
1457 struct isapnp_resources *to)
1460 struct isapnp_port *port;
1461 struct isapnp_irq *irq;
1462 struct isapnp_dma *dma;
1463 struct isapnp_mem *mem;
1467 /* process port settings */
1468 for (tmp = 0; tmp < 8; tmp++) {
1469 if (!(cfg->request->resource[tmp].flags & IORESOURCE_AUTO))
1470 continue; /* don't touch */
1471 port = cfg->port[tmp];
1473 cfg->port[tmp] = port = isapnp_find_port(cfg->request, tmp);
1477 if (from && port->res == from) {
1478 while (port->res != to) {
1479 if (!port->res->alt)
1481 port = port->res->alt->port;
1482 for (tmp1 = tmp; tmp1 > 0 && port; tmp1--)
1484 cfg->port[tmp] = port;
1487 cfg->result.resource[tmp].flags = isapnp_port_resource_flags(port);
1491 /* process irq settings */
1492 for (tmp = 0; tmp < 2; tmp++) {
1493 if (!(cfg->request->irq_resource[tmp].flags & IORESOURCE_AUTO))
1494 continue; /* don't touch */
1495 irq = cfg->irq[tmp];
1497 cfg->irq[tmp] = irq = isapnp_find_irq(cfg->request, tmp);
1501 if (from && irq->res == from) {
1502 while (irq->res != to) {
1505 irq = irq->res->alt->irq;
1506 for (tmp1 = tmp; tmp1 > 0 && irq; tmp1--)
1508 cfg->irq[tmp] = irq;
1511 cfg->result.irq_resource[tmp].flags = isapnp_irq_resource_flags(irq);
1515 /* process dma settings */
1516 for (tmp = 0; tmp < 2; tmp++) {
1517 if (!(cfg->request->dma_resource[tmp].flags & IORESOURCE_AUTO))
1518 continue; /* don't touch */
1519 dma = cfg->dma[tmp];
1521 cfg->dma[tmp] = dma = isapnp_find_dma(cfg->request, tmp);
1525 if (from && dma->res == from) {
1526 while (dma->res != to) {
1529 dma = dma->res->alt->dma;
1530 for (tmp1 = tmp; tmp1 > 0 && dma; tmp1--)
1532 cfg->dma[tmp] = dma;
1535 cfg->result.dma_resource[tmp].flags = isapnp_dma_resource_flags(dma);
1539 /* process memory settings */
1540 for (tmp = 0; tmp < 4; tmp++) {
1541 if (!(cfg->request->resource[tmp + 8].flags & IORESOURCE_AUTO))
1542 continue; /* don't touch */
1543 mem = cfg->mem[tmp];
1545 cfg->mem[tmp] = mem = isapnp_find_mem(cfg->request, tmp);
1549 if (from && mem->res == from) {
1550 while (mem->res != to) {
1553 mem = mem->res->alt->mem;
1554 for (tmp1 = tmp; tmp1 > 0 && mem; tmp1--)
1556 cfg->mem[tmp] = mem;
1559 cfg->result.resource[tmp + 8].flags = isapnp_mem_resource_flags(mem);
1566 static int isapnp_check_port(struct isapnp_cfgtmp *cfg, int port, int size, int idx)
1568 int i, tmp, rport, rsize;
1569 struct isapnp_port *xport;
1570 struct pci_dev *dev;
1572 if (check_region(port, size))
1574 for (i = 0; i < 8; i++) {
1575 rport = isapnp_reserve_io[i << 1];
1576 rsize = isapnp_reserve_io[(i << 1) + 1];
1577 if (port >= rport && port < rport + rsize)
1579 if (port + size > rport && port + size < (rport + rsize) - 1)
1583 isapnp_for_each_dev(dev) {
1585 for (tmp = 0; tmp < 8; tmp++) {
1586 if (dev->resource[tmp].flags) {
1587 rport = dev->resource[tmp].start;
1588 rsize = (dev->resource[tmp].end - rport) + 1;
1589 if (port >= rport && port < rport + rsize)
1591 if (port + size > rport && port + size < (rport + rsize) - 1)
1597 for (i = 0; i < 8; i++) {
1601 flags = cfg->request->resource[i].flags;
1604 tmp = cfg->request->resource[i].start;
1605 if (flags & IORESOURCE_AUTO) { /* auto */
1606 xport = cfg->port[i];
1609 if (cfg->result.resource[i].flags & IORESOURCE_AUTO)
1611 tmp = cfg->result.resource[i].start;
1612 if (tmp + xport->size >= port && tmp <= port + xport->size)
1618 xport = isapnp_find_port(cfg->request, i);
1621 if (tmp + xport->size >= port && tmp <= port + xport->size)
1627 static int isapnp_valid_port(struct isapnp_cfgtmp *cfg, int idx)
1630 unsigned long *value1, *value2;
1631 struct isapnp_port *port;
1633 if (!cfg || idx < 0 || idx > 7)
1635 if (!(cfg->result.resource[idx].flags & IORESOURCE_AUTO)) /* don't touch */
1638 port = cfg->port[idx];
1641 value1 = &cfg->result.resource[idx].start;
1642 value2 = &cfg->result.resource[idx].end;
1643 if (cfg->result.resource[idx].flags & IORESOURCE_AUTO) {
1644 cfg->result.resource[idx].flags &= ~IORESOURCE_AUTO;
1645 *value1 = port->min;
1646 *value2 = port->min + port->size - 1;
1647 if (!isapnp_check_port(cfg, *value1, port->size, idx))
1651 *value1 += port->align;
1652 *value2 = *value1 + port->size - 1;
1653 if (*value1 > port->max || !port->align) {
1654 if (port->res && port->res->alt) {
1655 if ((err = isapnp_alternative_switch(cfg, port->res, port->res->alt))<0)
1661 } while (isapnp_check_port(cfg, *value1, port->size, idx));
1665 static void isapnp_test_handler(int irq, void *dev_id, struct pt_regs *regs)
1669 static int isapnp_check_interrupt(struct isapnp_cfgtmp *cfg, int irq, int idx)
1672 struct pci_dev *dev;
1674 if (irq < 0 || irq > 15)
1676 for (i = 0; i < 16; i++) {
1677 if (isapnp_reserve_irq[i] == irq)
1680 isapnp_for_each_dev(dev) {
1682 if ((dev->irq_resource[0].flags && dev->irq_resource[0].start == irq) ||
1683 (dev->irq_resource[1].flags && dev->irq_resource[1].start == irq))
1688 if (!isapnp_skip_pci_scan) {
1689 pci_for_each_dev(dev) {
1690 if (dev->irq == irq)
1695 if (request_irq(irq, isapnp_test_handler, SA_INTERRUPT, "isapnp", NULL))
1697 free_irq(irq, NULL);
1698 for (i = 0; i < DEVICE_COUNT_IRQ; i++) {
1701 if (!cfg->result.irq_resource[i].flags)
1703 if (cfg->result.irq_resource[i].flags & IORESOURCE_AUTO)
1705 if (cfg->result.irq_resource[i].start == irq)
1711 static int isapnp_valid_irq(struct isapnp_cfgtmp *cfg, int idx)
1713 /* IRQ priority: this table is good for i386 */
1714 static unsigned short xtab[16] = {
1715 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
1718 unsigned long *value1, *value2;
1719 struct isapnp_irq *irq;
1721 if (!cfg || idx < 0 || idx > 1)
1723 if (!(cfg->result.irq_resource[idx].flags & IORESOURCE_AUTO))
1726 irq = cfg->irq[idx];
1729 value1 = &cfg->result.irq_resource[idx].start;
1730 value2 = &cfg->result.irq_resource[idx].end;
1731 if (cfg->result.irq_resource[idx].flags & IORESOURCE_AUTO) {
1732 for (i = 0; i < 16 && !(irq->map & (1<<xtab[i])); i++);
1735 cfg->result.irq_resource[idx].flags &= ~IORESOURCE_AUTO;
1736 if (!isapnp_check_interrupt(cfg, *value1 = *value2 = xtab[i], idx))
1740 for (i = 0; i < 16 && xtab[i] != *value1; i++);
1741 for (i++; i < 16 && !(irq->map & (1<<xtab[i])); i++);
1743 if (irq->res && irq->res->alt) {
1744 if ((err = isapnp_alternative_switch(cfg, irq->res, irq->res->alt))<0)
1750 *value1 = *value2 = xtab[i];
1752 } while (isapnp_check_interrupt(cfg, *value1, idx));
1756 static int isapnp_check_dma(struct isapnp_cfgtmp *cfg, int dma, int idx)
1759 struct pci_dev *dev;
1761 /* Some machines allow DMA 0, but others don't. In fact on some
1762 boxes DMA 0 is the memory refresh. Play safe */
1763 if (isapnp_allow_dma0 == 1)
1765 if (dma < mindma || dma == 4 || dma > 7)
1767 for (i = 0; i < 8; i++) {
1768 if (isapnp_reserve_dma[i] == dma)
1771 isapnp_for_each_dev(dev) {
1773 if ((dev->dma_resource[0].flags && dev->dma_resource[0].start == dma) ||
1774 (dev->dma_resource[1].flags && dev->dma_resource[1].start == dma))
1778 if (request_dma(dma, "isapnp"))
1781 for (i = 0; i < 2; i++) {
1784 if (!cfg->result.dma_resource[i].flags ||
1785 (cfg->result.dma_resource[i].flags & IORESOURCE_AUTO))
1787 if (cfg->result.dma_resource[i].start == dma)
1793 static int isapnp_valid_dma(struct isapnp_cfgtmp *cfg, int idx)
1795 /* DMA priority: this table is good for i386 */
1796 static unsigned short xtab[16] = {
1797 1, 3, 5, 6, 7, 0, 2, 4
1800 unsigned long *value1, *value2;
1801 struct isapnp_dma *dma;
1803 if (!cfg || idx < 0 || idx > 1)
1805 if (!(cfg->result.dma_resource[idx].flags & IORESOURCE_AUTO)) /* don't touch */
1808 dma = cfg->dma[idx];
1811 value1 = &cfg->result.dma_resource[idx].start;
1812 value2 = &cfg->result.dma_resource[idx].end;
1813 if (cfg->result.dma_resource[idx].flags & IORESOURCE_AUTO) {
1814 for (i = 0; i < 8 && !(dma->map & (1<<xtab[i])); i++);
1817 cfg->result.dma_resource[idx].flags &= ~IORESOURCE_AUTO;
1818 if (!isapnp_check_dma(cfg, *value1 = *value2 = xtab[i], idx))
1822 for (i = 0; i < 8 && xtab[i] != *value1; i++);
1823 for (i++; i < 8 && !(dma->map & (1<<xtab[i])); i++);
1825 if (dma->res && dma->res->alt) {
1826 if ((err = isapnp_alternative_switch(cfg, dma->res, dma->res->alt))<0)
1832 *value1 = *value2 = xtab[i];
1834 } while (isapnp_check_dma(cfg, *value1, idx));
1838 static int isapnp_check_mem(struct isapnp_cfgtmp *cfg, unsigned int addr, unsigned int size, int idx)
1841 unsigned int raddr, rsize;
1842 struct isapnp_mem *xmem;
1843 struct pci_dev *dev;
1845 for (i = 0; i < 8; i++) {
1846 raddr = (unsigned int)isapnp_reserve_mem[i << 1];
1847 rsize = (unsigned int)isapnp_reserve_mem[(i << 1) + 1];
1848 if (addr >= raddr && addr < raddr + rsize)
1850 if (addr + size > raddr && addr + size < (raddr + rsize) - 1)
1852 if (__check_region(&iomem_resource, addr, size))
1855 isapnp_for_each_dev(dev) {
1857 for (tmp = 0; tmp < 4; tmp++) {
1858 if (dev->resource[tmp].flags) {
1859 raddr = dev->resource[tmp + 8].start;
1860 rsize = (dev->resource[tmp + 8].end - raddr) + 1;
1861 if (addr >= raddr && addr < raddr + rsize)
1863 if (addr + size > raddr && addr + size < (raddr + rsize) - 1)
1869 for (i = 0; i < 4; i++) {
1870 unsigned int flags = cfg->request->resource[i + 8].flags;
1875 tmp = cfg->result.resource[i + 8].start;
1876 if (flags & IORESOURCE_AUTO) { /* auto */
1880 if (cfg->result.resource[i + 8].flags & IORESOURCE_AUTO)
1882 if (tmp + xmem->size >= addr && tmp <= addr + xmem->size)
1888 xmem = isapnp_find_mem(cfg->request, i);
1891 if (tmp + xmem->size >= addr && tmp <= addr + xmem->size)
1897 static int isapnp_valid_mem(struct isapnp_cfgtmp *cfg, int idx)
1900 unsigned long *value1, *value2;
1901 struct isapnp_mem *mem;
1903 if (!cfg || idx < 0 || idx > 3)
1905 if (!(cfg->result.resource[idx + 8].flags & IORESOURCE_AUTO)) /* don't touch */
1908 mem = cfg->mem[idx];
1911 value1 = &cfg->result.resource[idx + 8].start;
1912 value2 = &cfg->result.resource[idx + 8].end;
1913 if (cfg->result.resource[idx + 8].flags & IORESOURCE_AUTO) {
1914 cfg->result.resource[idx + 8].flags &= ~IORESOURCE_AUTO;
1916 *value2 = mem->min + mem->size - 1;
1917 if (!isapnp_check_mem(cfg, *value1, mem->size, idx))
1921 *value1 += mem->align;
1922 *value2 = *value1 + mem->size - 1;
1923 if (*value1 > mem->max || !mem->align) {
1924 if (mem->res && mem->res->alt) {
1925 if ((err = isapnp_alternative_switch(cfg, mem->res, mem->res->alt))<0)
1931 } while (isapnp_check_mem(cfg, *value1, mem->size, idx));
1935 static int isapnp_check_valid(struct isapnp_cfgtmp *cfg)
1939 for (tmp = 0; tmp < 8; tmp++)
1940 if (cfg->result.resource[tmp].flags & IORESOURCE_AUTO)
1942 for (tmp = 0; tmp < 2; tmp++)
1943 if (cfg->result.irq_resource[tmp].flags & IORESOURCE_AUTO)
1945 for (tmp = 0; tmp < 2; tmp++)
1946 if (cfg->result.dma_resource[tmp].flags & IORESOURCE_AUTO)
1948 for (tmp = 0; tmp < 4; tmp++)
1949 if (cfg->result.resource[tmp + 8].flags & IORESOURCE_AUTO)
1954 static int isapnp_config_activate(struct pci_dev *dev)
1956 struct isapnp_cfgtmp cfg;
1957 int tmp, fauto, err;
1963 memset(&cfg, 0, sizeof(cfg));
1965 memcpy(&cfg.result, dev, sizeof(struct pci_dev));
1966 /* check if all values are set, otherwise try auto-configuration */
1967 for (tmp = fauto = 0; !fauto && tmp < 8; tmp++) {
1968 if (dev->resource[tmp].flags & IORESOURCE_AUTO)
1971 for (tmp = 0; !fauto && tmp < 2; tmp++) {
1972 if (dev->irq_resource[tmp].flags & IORESOURCE_AUTO)
1975 for (tmp = 0; !fauto && tmp < 2; tmp++) {
1976 if (dev->dma_resource[tmp].flags & IORESOURCE_AUTO)
1979 for (tmp = 0; !fauto && tmp < 4; tmp++) {
1980 if (dev->resource[tmp + 8].flags & IORESOURCE_AUTO)
1985 /* set variables to initial values */
1986 if ((err = isapnp_alternative_switch(&cfg, NULL, NULL))<0)
1988 /* find first valid configuration */
1991 for (tmp = 0; tmp < 8 && cfg.result.resource[tmp].flags; tmp++)
1992 if ((err = isapnp_valid_port(&cfg, tmp))<0)
1994 for (tmp = 0; tmp < 2 && cfg.result.irq_resource[tmp].flags; tmp++)
1995 if ((err = isapnp_valid_irq(&cfg, tmp))<0)
1997 for (tmp = 0; tmp < 2 && cfg.result.dma_resource[tmp].flags; tmp++)
1998 if ((err = isapnp_valid_dma(&cfg, tmp))<0)
2000 for (tmp = 0; tmp < 4 && cfg.result.resource[tmp + 8].flags; tmp++)
2001 if ((err = isapnp_valid_mem(&cfg, tmp))<0)
2003 } while (isapnp_check_valid(&cfg)<0 && fauto++ < 20);
2007 /* we have valid configuration, try configure hardware */
2008 isapnp_cfg_begin(dev->bus->number, dev->devfn);
2010 dev->irq_resource[0] = cfg.result.irq_resource[0];
2011 dev->irq_resource[1] = cfg.result.irq_resource[1];
2012 dev->dma_resource[0] = cfg.result.dma_resource[0];
2013 dev->dma_resource[1] = cfg.result.dma_resource[1];
2014 for (tmp = 0; tmp < 12; tmp++) {
2015 dev->resource[tmp] = cfg.result.resource[tmp];
2017 for (tmp = 0; tmp < 8 && dev->resource[tmp].flags; tmp++)
2018 isapnp_write_word(ISAPNP_CFG_PORT+(tmp<<1), dev->resource[tmp].start);
2019 for (tmp = 0; tmp < 2 && dev->irq_resource[tmp].flags; tmp++) {
2020 int irq = dev->irq_resource[tmp].start;
2023 isapnp_write_byte(ISAPNP_CFG_IRQ+(tmp<<1), irq);
2025 for (tmp = 0; tmp < 2 && dev->dma_resource[tmp].flags; tmp++)
2026 isapnp_write_byte(ISAPNP_CFG_DMA+tmp, dev->dma_resource[tmp].start);
2027 for (tmp = 0; tmp < 4 && dev->resource[tmp+8].flags; tmp++)
2028 isapnp_write_word(ISAPNP_CFG_MEM+(tmp<<2), (dev->resource[tmp + 8].start >> 8) & 0xffff);
2029 isapnp_activate(dev->devfn);
2034 static int isapnp_config_deactivate(struct pci_dev *dev)
2036 if (!dev || !dev->active)
2038 isapnp_cfg_begin(dev->bus->number, dev->devfn);
2039 isapnp_deactivate(dev->devfn);
2045 void isapnp_resource_change(struct resource *resource,
2046 unsigned long start,
2049 if (resource == NULL)
2051 resource->flags &= ~IORESOURCE_AUTO;
2052 resource->start = start;
2053 resource->end = start + size - 1;
2062 static void isapnp_free_port(struct isapnp_port *port)
2064 struct isapnp_port *next;
2073 static void isapnp_free_irq(struct isapnp_irq *irq)
2075 struct isapnp_irq *next;
2084 static void isapnp_free_dma(struct isapnp_dma *dma)
2086 struct isapnp_dma *next;
2095 static void isapnp_free_mem(struct isapnp_mem *mem)
2097 struct isapnp_mem *next;
2106 static void isapnp_free_mem32(struct isapnp_mem32 *mem32)
2108 struct isapnp_mem32 *next;
2117 static void isapnp_free_resources(struct isapnp_resources *resources, int alt)
2119 struct isapnp_resources *next;
2122 next = alt ? resources->alt : resources->next;
2123 isapnp_free_port(resources->port);
2124 isapnp_free_irq(resources->irq);
2125 isapnp_free_dma(resources->dma);
2126 isapnp_free_mem(resources->mem);
2127 isapnp_free_mem32(resources->mem32);
2128 if (!alt && resources->alt)
2129 isapnp_free_resources(resources->alt, 1);
2135 static void isapnp_free_card(struct pci_bus *card)
2137 while (!list_empty(&card->devices)) {
2138 struct list_head *list = card->devices.next;
2139 struct pci_dev *dev = pci_dev_b(list);
2141 isapnp_free_resources((struct isapnp_resources *)dev->sysdata, 0);
2147 static void isapnp_free_all_resources(void)
2149 #ifdef ISAPNP_REGION_OK
2150 release_region(_PIDXR, 1);
2152 release_region(_PNPWRP, 1);
2153 release_region(isapnp_rdp, 1);
2154 #ifdef CONFIG_PROC_FS
2157 while (!list_empty(&isapnp_cards)) {
2158 struct list_head *list = isapnp_cards.next;
2160 isapnp_free_card(pci_bus_b(list));
2166 static int isapnp_announce_device(struct isapnp_driver *drv,
2167 struct pci_dev *dev)
2169 const struct isapnp_device_id *id;
2172 if (drv->id_table) {
2173 id = isapnp_match_dev(drv->id_table, dev);
2181 if (drv->probe(dev, id) >= 0) {
2182 dev->driver = (struct pci_driver *) drv;
2190 * isapnp_dev_driver - get the isapnp_driver of a device
2191 * @dev: the device to query
2193 * Returns the appropriate isapnp_driver structure or %NULL if there is no
2194 * registered driver for the device.
2196 static struct isapnp_driver *isapnp_dev_driver(const struct pci_dev *dev)
2198 return (struct isapnp_driver *) dev->driver;
2201 static LIST_HEAD(isapnp_drivers);
2204 * isapnp_register_driver - register a new ISAPnP driver
2205 * @drv: the driver structure to register
2207 * Adds the driver structure to the list of registered ISAPnP drivers
2208 * Returns the number of isapnp devices which were claimed by the driver
2209 * during registration. The driver remains registered even if the
2210 * return value is zero.
2212 int isapnp_register_driver(struct isapnp_driver *drv)
2214 struct pci_dev *dev;
2217 list_add_tail(&drv->node, &isapnp_drivers);
2219 isapnp_for_each_dev(dev) {
2220 if (!isapnp_dev_driver(dev))
2221 count += isapnp_announce_device(drv, dev);
2227 * isapnp_unregister_driver - unregister an isapnp driver
2228 * @drv: the driver structure to unregister
2230 * Deletes the driver structure from the list of registered ISAPnP drivers,
2231 * gives it a chance to clean up by calling its remove() function for
2232 * each device it was responsible for, and marks those devices as
2235 void isapnp_unregister_driver(struct isapnp_driver *drv)
2237 struct pci_dev *dev;
2239 list_del(&drv->node);
2240 isapnp_for_each_dev(dev) {
2241 if (dev->driver == (struct pci_driver *) drv) {
2249 EXPORT_SYMBOL(isapnp_cards);
2250 EXPORT_SYMBOL(isapnp_devices);
2251 EXPORT_SYMBOL(isapnp_present);
2252 EXPORT_SYMBOL(isapnp_cfg_begin);
2253 EXPORT_SYMBOL(isapnp_cfg_end);
2254 EXPORT_SYMBOL(isapnp_read_byte);
2255 EXPORT_SYMBOL(isapnp_read_word);
2256 EXPORT_SYMBOL(isapnp_read_dword);
2257 EXPORT_SYMBOL(isapnp_write_byte);
2258 EXPORT_SYMBOL(isapnp_write_word);
2259 EXPORT_SYMBOL(isapnp_write_dword);
2260 EXPORT_SYMBOL(isapnp_wake);
2261 EXPORT_SYMBOL(isapnp_device);
2262 EXPORT_SYMBOL(isapnp_activate);
2263 EXPORT_SYMBOL(isapnp_deactivate);
2264 EXPORT_SYMBOL(isapnp_find_card);
2265 EXPORT_SYMBOL(isapnp_find_dev);
2266 EXPORT_SYMBOL(isapnp_probe_cards);
2267 EXPORT_SYMBOL(isapnp_probe_devs);
2268 EXPORT_SYMBOL(isapnp_activate_dev);
2269 EXPORT_SYMBOL(isapnp_resource_change);
2270 EXPORT_SYMBOL(isapnp_register_driver);
2271 EXPORT_SYMBOL(isapnp_unregister_driver);
2273 int __init isapnp_init(void)
2276 struct pci_bus *card;
2278 if (isapnp_disable) {
2279 isapnp_detected = 0;
2280 printk(KERN_INFO "isapnp: ISA Plug & Play support disabled\n");
2283 #ifdef ISAPNP_REGION_OK
2284 if (!request_region(_PIDXR, 1, "isapnp index")) {
2285 printk(KERN_ERR "isapnp: Index Register 0x%x already used\n", _PIDXR);
2289 if (!request_region(_PNPWRP, 1, "isapnp write")) {
2290 printk(KERN_ERR "isapnp: Write Data Register 0x%x already used\n", _PNPWRP);
2291 #ifdef ISAPNP_REGION_OK
2292 release_region(_PIDXR, 1);
2298 * Print a message. The existing ISAPnP code is hanging machines
2299 * so let the user know where.
2302 printk(KERN_INFO "isapnp: Scanning for PnP cards...\n");
2303 if (isapnp_rdp >= 0x203 && isapnp_rdp <= 0x3ff) {
2305 if (!request_region(isapnp_rdp, 1, "isapnp read")) {
2306 printk(KERN_ERR "isapnp: Read Data Register 0x%x already used\n", isapnp_rdp);
2307 #ifdef ISAPNP_REGION_OK
2308 release_region(_PIDXR, 1);
2310 release_region(_PNPWRP, 1);
2315 isapnp_detected = 1;
2316 if (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff) {
2317 cards = isapnp_isolate();
2319 (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff)) {
2320 #ifdef ISAPNP_REGION_OK
2321 release_region(_PIDXR, 1);
2323 release_region(_PNPWRP, 1);
2324 isapnp_detected = 0;
2325 printk(KERN_INFO "isapnp: No Plug & Play device found\n");
2328 request_region(isapnp_rdp, 1, "isapnp read");
2330 isapnp_build_device_list();
2333 isapnp_for_each_card(card) {
2335 if (isapnp_verbose) {
2336 struct list_head *devlist;
2337 printk(KERN_INFO "isapnp: Card '%s'\n", card->name[0]?card->name:"Unknown");
2338 if (isapnp_verbose < 2)
2340 for (devlist = card->devices.next; devlist != &card->devices; devlist = devlist->next) {
2341 struct pci_dev *dev = pci_dev_b(devlist);
2342 printk(KERN_INFO "isapnp: Device '%s'\n", dev->name[0]?card->name:"Unknown");
2347 printk(KERN_INFO "isapnp: %i Plug & Play card%s detected total\n", cards, cards>1?"s":"");
2349 printk(KERN_INFO "isapnp: No Plug & Play card found\n");
2351 #ifdef CONFIG_PROC_FS
2359 int init_module(void)
2361 return isapnp_init();
2364 void cleanup_module(void)
2366 if (isapnp_detected)
2367 isapnp_free_all_resources();
2372 /* format is: noisapnp */
2374 static int __init isapnp_setup_disable(char *str)
2380 __setup("noisapnp", isapnp_setup_disable);
2382 /* format is: isapnp=rdp,reset,skip_pci_scan,verbose */
2384 static int __init isapnp_setup_isapnp(char *str)
2386 (void)((get_option(&str,&isapnp_rdp) == 2) &&
2387 (get_option(&str,&isapnp_reset) == 2) &&
2388 (get_option(&str,&isapnp_skip_pci_scan) == 2) &&
2389 (get_option(&str,&isapnp_verbose) == 2));
2393 __setup("isapnp=", isapnp_setup_isapnp);
2395 /* format is: isapnp_reserve_irq=irq1[,irq2] .... */
2397 static int __init isapnp_setup_reserve_irq(char *str)
2401 for (i = 0; i < 16; i++)
2402 if (get_option(&str,&isapnp_reserve_irq[i]) != 2)
2407 __setup("isapnp_reserve_irq=", isapnp_setup_reserve_irq);
2409 /* format is: isapnp_reserve_dma=dma1[,dma2] .... */
2411 static int __init isapnp_setup_reserve_dma(char *str)
2415 for (i = 0; i < 8; i++)
2416 if (get_option(&str,&isapnp_reserve_dma[i]) != 2)
2421 __setup("isapnp_reserve_dma=", isapnp_setup_reserve_dma);
2423 /* format is: isapnp_reserve_io=io1,size1[,io2,size2] .... */
2425 static int __init isapnp_setup_reserve_io(char *str)
2429 for (i = 0; i < 16; i++)
2430 if (get_option(&str,&isapnp_reserve_io[i]) != 2)
2435 __setup("isapnp_reserve_io=", isapnp_setup_reserve_io);
2437 /* format is: isapnp_reserve_mem=mem1,size1[,mem2,size2] .... */
2439 static int __init isapnp_setup_reserve_mem(char *str)
2443 for (i = 0; i < 16; i++)
2444 if (get_option(&str,&isapnp_reserve_mem[i]) != 2)
2449 __setup("isapnp_reserve_mem=", isapnp_setup_reserve_mem);