Merge with Linux 2.3.40.
[linux-2.6/linux-mips.git] / drivers / pnp / isapnp.c
blobec81da498942bbcac995d5d73be1b75a2b53a5ae
1 /*
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.
20 * Changelog:
21 * 2000-01-01 Added quirks handling for buggy hardware
22 * Peter Denison <peterd@pnd-pc.demon.co.uk>
25 #include <linux/config.h>
26 #include <linux/version.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/errno.h>
30 #include <linux/ioport.h>
31 #include <linux/string.h>
32 #include <linux/malloc.h>
33 #include <linux/delay.h>
34 #include <asm/io.h>
35 #include <asm/dma.h>
36 #include <asm/irq.h>
37 #include <linux/pci.h>
38 #include <linux/init.h>
39 #include <linux/isapnp.h>
41 LIST_HEAD(isapnp_cards);
42 LIST_HEAD(isapnp_devices);
44 #if 0
45 #define ISAPNP_REGION_OK
46 #endif
47 #if 0
48 #define ISAPNP_DEBUG
49 #endif
51 struct resource *pidxr_res = NULL;
52 struct resource *pnpwrp_res = NULL;
53 struct resource *isapnp_rdp_res = NULL;
55 int isapnp_disable = 0; /* Disable ISA PnP */
56 int isapnp_rdp = 0; /* Read Data Port */
57 int isapnp_reset = 1; /* reset all PnP cards (deactivate) */
58 int isapnp_skip_pci_scan = 0; /* skip PCI resource scanning */
59 int isapnp_verbose = 1; /* verbose mode */
60 int isapnp_reserve_irq[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some IRQ */
61 int isapnp_reserve_dma[8] = { [0 ... 7] = -1 }; /* reserve (don't use) some DMA */
62 int isapnp_reserve_io[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some I/O region */
63 int isapnp_reserve_mem[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some memory region */
65 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
66 MODULE_DESCRIPTION("Generic ISA Plug & Play support");
67 MODULE_PARM(isapnp_disable, "i");
68 MODULE_PARM_DESC(isapnp_disable, "ISA Plug & Play disable");
69 MODULE_PARM(isapnp_rdp, "i");
70 MODULE_PARM_DESC(isapnp_rdp, "ISA Plug & Play read data port");
71 MODULE_PARM(isapnp_reset, "i");
72 MODULE_PARM_DESC(isapnp_reset, "ISA Plug & Play reset all cards");
73 MODULE_PARM(isapnp_skip_pci_scan, "i");
74 MODULE_PARM_DESC(isapnp_skip_pci_scan, "ISA Plug & Play skip PCI resource scanning");
75 MODULE_PARM(isapnp_verbose, "i");
76 MODULE_PARM_DESC(isapnp_verbose, "ISA Plug & Play verbose mode");
77 MODULE_PARM(isapnp_reserve_irq, "1-16i");
78 MODULE_PARM_DESC(isapnp_reserve_irq, "ISA Plug & Play - reserve IRQ line(s)");
79 MODULE_PARM(isapnp_reserve_dma, "1-8i");
80 MODULE_PARM_DESC(isapnp_reserve_dma, "ISA Plug & Play - reserve DMA channel(s)");
81 MODULE_PARM(isapnp_reserve_io, "1-16i");
82 MODULE_PARM_DESC(isapnp_reserve_io, "ISA Plug & Play - reserve I/O region(s) - port,size");
83 MODULE_PARM(isapnp_reserve_mem, "1-16i");
84 MODULE_PARM_DESC(isapnp_reserve_mem, "ISA Plug & Play - reserve memory region(s) - address,size");
86 #define _PIDXR 0x279
87 #define _PNPWRP 0xa79
89 /* short tags */
90 #define _STAG_PNPVERNO 0x01
91 #define _STAG_LOGDEVID 0x02
92 #define _STAG_COMPATDEVID 0x03
93 #define _STAG_IRQ 0x04
94 #define _STAG_DMA 0x05
95 #define _STAG_STARTDEP 0x06
96 #define _STAG_ENDDEP 0x07
97 #define _STAG_IOPORT 0x08
98 #define _STAG_FIXEDIO 0x09
99 #define _STAG_VENDOR 0x0e
100 #define _STAG_END 0x0f
101 /* long tags */
102 #define _LTAG_MEMRANGE 0x81
103 #define _LTAG_ANSISTR 0x82
104 #define _LTAG_UNICODESTR 0x83
105 #define _LTAG_VENDOR 0x84
106 #define _LTAG_MEM32RANGE 0x85
107 #define _LTAG_FIXEDMEM32RANGE 0x86
109 static unsigned char isapnp_checksum_value;
110 static DECLARE_MUTEX(isapnp_cfg_mutex);
111 static int isapnp_detected = 0;
113 /* some prototypes */
115 static int isapnp_config_prepare(struct pci_dev *dev);
116 static int isapnp_config_activate(struct pci_dev *dev);
117 static int isapnp_config_deactivate(struct pci_dev *dev);
119 static inline void write_data(unsigned char x)
121 outb(x, _PNPWRP);
124 static inline void write_address(unsigned char x)
126 outb(x, _PIDXR);
127 udelay(20);
130 static inline unsigned char read_data(void)
132 unsigned char val = inb(isapnp_rdp);
133 return val;
136 unsigned char isapnp_read_byte(unsigned char idx)
138 write_address(idx);
139 return read_data();
142 unsigned short isapnp_read_word(unsigned char idx)
144 unsigned short val;
146 val = isapnp_read_byte(idx);
147 val = (val << 8) + isapnp_read_byte(idx+1);
148 return val;
151 unsigned int isapnp_read_dword(unsigned char idx)
153 unsigned int val;
155 val = isapnp_read_byte(idx);
156 val = (val << 8) + isapnp_read_byte(idx+1);
157 val = (val << 8) + isapnp_read_byte(idx+2);
158 val = (val << 8) + isapnp_read_byte(idx+3);
159 return val;
162 void isapnp_write_byte(unsigned char idx, unsigned char val)
164 write_address(idx);
165 write_data(val);
168 void isapnp_write_word(unsigned char idx, unsigned short val)
170 isapnp_write_byte(idx, val >> 8);
171 isapnp_write_byte(idx+1, val);
174 void isapnp_write_dword(unsigned char idx, unsigned int val)
176 isapnp_write_byte(idx, val >> 24);
177 isapnp_write_byte(idx+1, val >> 16);
178 isapnp_write_byte(idx+2, val >> 8);
179 isapnp_write_byte(idx+3, val);
182 void *isapnp_alloc(long size)
184 void *result;
186 result = kmalloc(size, GFP_KERNEL);
187 if (!result)
188 return NULL;
189 memset(result, 0, size);
190 return result;
193 static void isapnp_key(void)
195 unsigned char code = 0x6a, msb;
196 int i;
198 mdelay(1);
199 write_address(0x00);
200 write_address(0x00);
202 write_address(code);
204 for (i = 1; i < 32; i++) {
205 msb = ((code & 0x01) ^ ((code & 0x02) >> 1)) << 7;
206 code = (code >> 1) | msb;
207 write_address(code);
211 /* place all pnp cards in wait-for-key state */
212 static void isapnp_wait(void)
214 isapnp_write_byte(0x02, 0x02);
217 void isapnp_wake(unsigned char csn)
219 isapnp_write_byte(0x03, csn);
222 void isapnp_device(unsigned char logdev)
224 isapnp_write_byte(0x07, logdev);
227 void isapnp_activate(unsigned char logdev)
229 isapnp_device(logdev);
230 isapnp_write_byte(ISAPNP_CFG_ACTIVATE, 1);
231 udelay(250);
234 void isapnp_deactivate(unsigned char logdev)
236 isapnp_device(logdev);
237 isapnp_write_byte(ISAPNP_CFG_ACTIVATE, 0);
238 udelay(500);
241 static void __init isapnp_peek(unsigned char *data, int bytes)
243 int i, j;
244 unsigned char d;
246 for (i = 1; i <= bytes; i++) {
247 for (j = 0; j < 10; j++) {
248 d = isapnp_read_byte(0x05);
249 if (d & 1)
250 break;
251 udelay(100);
253 if (!(d & 1)) {
254 *data++ = 0xff;
255 continue;
257 d = isapnp_read_byte(0x04); /* PRESDI */
258 isapnp_checksum_value += d;
259 if (data != NULL)
260 *data++ = d;
264 #define RDP_STEP 32 /* minimum is 4 */
266 static int isapnp_next_rdp(void)
268 int rdp = isapnp_rdp;
269 while (rdp <= 0x3ff) {
270 if (!check_region(rdp, 1)) {
271 isapnp_rdp = rdp;
272 return 0;
274 rdp += RDP_STEP;
276 * We cannot use NE2000 probe spaces for ISAPnP or we
277 * will lock up machines.
279 if(rdp >= 0x280 && rdp <= 0x380)
280 continue;
282 return -1;
285 /* Set read port address */
286 static inline void isapnp_set_rdp(void)
288 isapnp_write_byte(0x00, isapnp_rdp >> 2);
289 udelay(100);
293 * Perform an isolation. The port selection code now tries to avoid
294 * "dangerous to read" ports.
297 static int __init isapnp_isolate_rdp_select(void)
299 isapnp_wait();
300 isapnp_key();
302 /* Control: reset CSN and conditionally everything else too */
303 isapnp_write_byte(0x02, isapnp_reset ? 0x05 : 0x04);
304 mdelay(2);
306 isapnp_wait();
307 isapnp_key();
308 isapnp_wake(0x00);
310 if (isapnp_next_rdp() < 0) {
311 isapnp_wait();
312 return -1;
315 isapnp_set_rdp();
316 udelay(1000);
317 write_address(0x01);
318 udelay(1000);
319 return 0;
323 * Isolate (assign uniqued CSN) to all ISA PnP devices.
326 static int __init isapnp_isolate(void)
328 unsigned char checksum = 0x6a;
329 unsigned char chksum = 0x00;
330 unsigned char bit = 0x00;
331 int data;
332 int csn = 0;
333 int i;
334 int iteration = 1;
336 isapnp_rdp = 0x213;
337 if (isapnp_isolate_rdp_select() < 0)
338 return -1;
340 while (1) {
341 for (i = 1; i <= 64; i++) {
342 data = read_data() << 8;
343 udelay(250);
344 data = data | read_data();
345 udelay(250);
346 if (data == 0x55aa)
347 bit = 0x01;
348 checksum = ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7) | (checksum >> 1);
349 bit = 0x00;
351 for (i = 65; i <= 72; i++) {
352 data = read_data() << 8;
353 udelay(250);
354 data = data | read_data();
355 udelay(250);
356 if (data == 0x55aa)
357 chksum |= (1 << (i - 65));
359 if (checksum != 0x00 && checksum == chksum) {
360 csn++;
362 isapnp_write_byte(0x06, csn);
363 udelay(250);
364 iteration++;
365 isapnp_wake(0x00);
366 isapnp_set_rdp();
367 udelay(1000);
368 write_address(0x01);
369 udelay(1000);
370 goto __next;
372 if (iteration == 1) {
373 isapnp_rdp += RDP_STEP;
374 if (isapnp_isolate_rdp_select() < 0)
375 return -1;
376 } else if (iteration > 1) {
377 break;
379 __next:
380 checksum = 0x6a;
381 chksum = 0x00;
382 bit = 0x00;
384 isapnp_wait();
385 return csn;
389 * Read one tag from stream.
392 static int __init isapnp_read_tag(unsigned char *type, unsigned short *size)
394 unsigned char tag, tmp[2];
396 isapnp_peek(&tag, 1);
397 if (tag == 0) /* invalid tag */
398 return -1;
399 if (tag & 0x80) { /* large item */
400 *type = tag;
401 isapnp_peek(tmp, 2);
402 *size = (tmp[1] << 8) | tmp[0];
403 } else {
404 *type = (tag >> 3) & 0x0f;
405 *size = tag & 0x07;
407 #if 0
408 printk("tag = 0x%x, type = 0x%x, size = %i\n", tag, *type, *size);
409 #endif
410 if (type == 0) /* wrong type */
411 return -1;
412 if (*type == 0xff && *size == 0xffff) /* probably invalid data */
413 return -1;
414 return 0;
418 * Skip specified number of bytes from stream.
421 static void __init isapnp_skip_bytes(int count)
423 isapnp_peek(NULL, count);
427 * Parse logical device tag.
430 static struct pci_dev * __init isapnp_parse_device(struct pci_bus *card, int size, int number)
432 unsigned char tmp[6];
433 struct pci_dev *dev;
435 isapnp_peek(tmp, size);
436 dev = isapnp_alloc(sizeof(struct pci_dev));
437 if (!dev)
438 return NULL;
439 dev->devfn = number;
440 dev->vendor = (tmp[1] << 8) | tmp[0];
441 dev->device = (tmp[3] << 8) | tmp[2];
442 dev->regs = tmp[4];
443 dev->bus = card;
444 if (size > 5)
445 dev->regs |= tmp[5] << 8;
446 dev->prepare = isapnp_config_prepare;
447 dev->activate = isapnp_config_activate;
448 dev->deactivate = isapnp_config_deactivate;
449 return dev;
453 * Build new resources structure
456 static struct isapnp_resources * __init isapnp_build_resources(struct pci_dev *dev, int dependent)
458 struct isapnp_resources *res, *ptr, *ptra;
460 res = isapnp_alloc(sizeof(struct isapnp_resources));
461 if (!res)
462 return NULL;
463 res->dev = dev;
464 ptr = (struct isapnp_resources *)dev->sysdata;
465 while (ptr && ptr->next)
466 ptr = ptr->next;
467 if (ptr && ptr->dependent && dependent) { /* add to another list */
468 ptra = ptr->alt;
469 while (ptra && ptra->alt)
470 ptra = ptra->alt;
471 if (!ptra)
472 ptr->alt = res;
473 else
474 ptra->alt = res;
475 } else {
476 if (!ptr)
477 dev->sysdata = res;
478 else
479 ptr->next = res;
481 if (dependent) {
482 res->priority = dependent & 0xff;
483 if (res->priority > ISAPNP_RES_PRIORITY_FUNCTIONAL)
484 res->priority = ISAPNP_RES_PRIORITY_INVALID;
485 res->dependent = 1;
486 } else {
487 res->priority = ISAPNP_RES_PRIORITY_PREFERRED;
488 res->dependent = 0;
490 return res;
494 * Add IRQ resource to resources list.
497 static void __init isapnp_add_irq_resource(struct pci_dev *dev,
498 struct isapnp_resources **res,
499 int dependent, int size)
501 unsigned char tmp[3];
502 struct isapnp_irq *irq, *ptr;
504 isapnp_peek(tmp, size);
505 irq = isapnp_alloc(sizeof(struct isapnp_irq));
506 if (!irq)
507 return;
508 if (*res == NULL) {
509 *res = isapnp_build_resources(dev, dependent);
510 if (*res == NULL) {
511 kfree(irq);
512 return;
515 irq->map = (tmp[1] << 8) | tmp[0];
516 if (size > 2)
517 irq->flags = tmp[2];
518 else
519 irq->flags = IORESOURCE_IRQ_HIGHEDGE;
520 irq->res = *res;
521 ptr = (*res)->irq;
522 while (ptr && ptr->next)
523 ptr = ptr->next;
524 if (ptr)
525 ptr->next = irq;
526 else
527 (*res)->irq = irq;
531 * Add DMA resource to resources list.
534 static void __init isapnp_add_dma_resource(struct pci_dev *dev,
535 struct isapnp_resources **res,
536 int dependent, int size)
538 unsigned char tmp[2];
539 struct isapnp_dma *dma, *ptr;
541 isapnp_peek(tmp, size);
542 dma = isapnp_alloc(sizeof(struct isapnp_dma));
543 if (!dma)
544 return;
545 if (*res == NULL) {
546 *res = isapnp_build_resources(dev, dependent);
547 if (*res == NULL) {
548 kfree(dma);
549 return;
552 dma->map = tmp[0];
553 dma->flags = tmp[1];
554 dma->res = *res;
555 ptr = (*res)->dma;
556 while (ptr && ptr->next)
557 ptr = ptr->next;
558 if (ptr)
559 ptr->next = dma;
560 else
561 (*res)->dma = dma;
565 * Add port resource to resources list.
568 static void __init isapnp_add_port_resource(struct pci_dev *dev,
569 struct isapnp_resources **res,
570 int dependent, int size)
572 unsigned char tmp[7];
573 struct isapnp_port *port, *ptr;
575 isapnp_peek(tmp, size);
576 port = isapnp_alloc(sizeof(struct isapnp_port));
577 if (!port)
578 return;
579 if (*res == NULL) {
580 *res = isapnp_build_resources(dev, dependent);
581 if (*res == NULL) {
582 kfree(port);
583 return;
586 port->min = (tmp[2] << 8) | tmp[1];
587 port->max = (tmp[4] << 8) | tmp[3];
588 port->align = tmp[5];
589 port->size = tmp[6];
590 port->flags = tmp[0] ? ISAPNP_PORT_FLAG_16BITADDR : 0;
591 port->res = *res;
592 ptr = (*res)->port;
593 while (ptr && ptr->next)
594 ptr = ptr->next;
595 if (ptr)
596 ptr->next = port;
597 else
598 (*res)->port = port;
602 * Add fixed port resource to resources list.
605 static void __init isapnp_add_fixed_port_resource(struct pci_dev *dev,
606 struct isapnp_resources **res,
607 int dependent, int size)
609 unsigned char tmp[3];
610 struct isapnp_port *port, *ptr;
612 isapnp_peek(tmp, size);
613 port = isapnp_alloc(sizeof(struct isapnp_port));
614 if (!port)
615 return;
616 if (*res == NULL) {
617 *res = isapnp_build_resources(dev, dependent);
618 if (*res == NULL) {
619 kfree(port);
620 return;
623 port->min = port->max = (tmp[1] << 8) | tmp[0];
624 port->size = tmp[2];
625 port->align = 0;
626 port->flags = ISAPNP_PORT_FLAG_FIXED;
627 port->res = *res;
628 ptr = (*res)->port;
629 while (ptr && ptr->next)
630 ptr = ptr->next;
631 if (ptr)
632 ptr->next = port;
633 else
634 (*res)->port = port;
638 * Add memory resource to resources list.
641 static void __init isapnp_add_mem_resource(struct pci_dev *dev,
642 struct isapnp_resources **res,
643 int dependent, int size)
645 unsigned char tmp[9];
646 struct isapnp_mem *mem, *ptr;
648 isapnp_peek(tmp, size);
649 mem = isapnp_alloc(sizeof(struct isapnp_mem));
650 if (!mem)
651 return;
652 if (*res == NULL) {
653 *res = isapnp_build_resources(dev, dependent);
654 if (*res == NULL) {
655 kfree(mem);
656 return;
659 mem->min = ((tmp[2] << 8) | tmp[1]) << 8;
660 mem->max = ((tmp[4] << 8) | tmp[3]) << 8;
661 mem->align = (tmp[6] << 8) | tmp[5];
662 mem->size = ((tmp[8] << 8) | tmp[7]) << 8;
663 mem->flags = tmp[0];
664 mem->res = *res;
665 ptr = (*res)->mem;
666 while (ptr && ptr->next)
667 ptr = ptr->next;
668 if (ptr)
669 ptr->next = mem;
670 else
671 (*res)->mem = mem;
675 * Add 32-bit memory resource to resources list.
678 static void __init isapnp_add_mem32_resource(struct pci_dev *dev,
679 struct isapnp_resources **res,
680 int dependent, int size)
682 unsigned char tmp[17];
683 struct isapnp_mem32 *mem32, *ptr;
685 isapnp_peek(tmp, size);
686 mem32 = isapnp_alloc(sizeof(struct isapnp_mem32));
687 if (!mem32)
688 return;
689 if (*res == NULL) {
690 *res = isapnp_build_resources(dev, dependent);
691 if (*res == NULL) {
692 kfree(mem32);
693 return;
696 memcpy(mem32->data, tmp, 17);
697 mem32->res = *res;
698 ptr = (*res)->mem32;
699 while (ptr && ptr->next)
700 ptr = ptr->next;
701 if (ptr)
702 ptr->next = mem32;
703 else
704 (*res)->mem32 = mem32;
708 * Add 32-bit fixed memory resource to resources list.
711 static void __init isapnp_add_fixed_mem32_resource(struct pci_dev *dev,
712 struct isapnp_resources **res,
713 int dependent, int size)
715 unsigned char tmp[17];
716 struct isapnp_mem32 *mem32, *ptr;
718 isapnp_peek(tmp, size);
719 mem32 = isapnp_alloc(sizeof(struct isapnp_mem32));
720 if (!mem32)
721 return;
722 if (*res == NULL) {
723 *res = isapnp_build_resources(dev, dependent);
724 if (*res == NULL) {
725 kfree(mem32);
726 return;
729 memcpy(mem32->data, tmp, 17);
730 mem32->res = *res;
731 ptr = (*res)->mem32;
732 while (ptr && ptr->next)
733 ptr = ptr->next;
734 if (ptr)
735 ptr->next = mem32;
736 else
737 (*res)->mem32 = mem32;
741 * Parse resource map for logical device.
744 static int __init isapnp_create_device(struct pci_bus *card,
745 unsigned short size)
747 int number = 0, skip = 0, dependent = 0, compat = 0;
748 unsigned char type, tmp[17];
749 struct pci_dev *dev;
750 struct isapnp_resources *res = NULL;
752 if ((dev = isapnp_parse_device(card, size, number++)) == NULL)
753 return 1;
754 list_add(&dev->bus_list, &card->devices);
755 list_add_tail(&dev->global_list, &isapnp_devices);
756 while (1) {
757 if (isapnp_read_tag(&type, &size)<0)
758 return 1;
759 if (skip && type != _STAG_LOGDEVID && type != _STAG_END)
760 goto __skip;
761 switch (type) {
762 case _STAG_LOGDEVID:
763 if (size >= 5 && size <= 6) {
764 isapnp_config_prepare(dev);
765 if ((dev = isapnp_parse_device(card, size, number++)) == NULL)
766 return 1;
767 list_add_tail(&dev->bus_list, &card->devices);
768 list_add_tail(&dev->global_list, &isapnp_devices);
769 size = 0;
770 skip = 0;
771 } else {
772 skip = 1;
774 res = NULL;
775 dependent = 0;
776 compat = 0;
777 break;
778 case _STAG_COMPATDEVID:
779 if (size == 4 && compat < DEVICE_COUNT_COMPATIBLE) {
780 isapnp_peek(tmp, 4);
781 dev->vendor_compatible[compat] = (tmp[1] << 8) | tmp[0];
782 dev->device_compatible[compat] = (tmp[3] << 8) | tmp[2];
783 compat++;
784 size = 0;
786 break;
787 case _STAG_IRQ:
788 if (size < 2 || size > 3)
789 goto __skip;
790 isapnp_add_irq_resource(dev, &res, dependent, size);
791 size = 0;
792 break;
793 case _STAG_DMA:
794 if (size != 2)
795 goto __skip;
796 isapnp_add_dma_resource(dev, &res, dependent, size);
797 size = 0;
798 break;
799 case _STAG_STARTDEP:
800 if (size > 1)
801 goto __skip;
802 res = NULL;
803 dependent = 0x100 | ISAPNP_RES_PRIORITY_ACCEPTABLE;
804 if (size > 0) {
805 isapnp_peek(tmp, size);
806 dependent = 0x100 | tmp[0];
807 size = 0;
809 break;
810 case _STAG_ENDDEP:
811 if (size != 0)
812 goto __skip;
813 res = NULL;
814 dependent = 0;
815 break;
816 case _STAG_IOPORT:
817 if (size != 7)
818 goto __skip;
819 isapnp_add_port_resource(dev, &res, dependent, size);
820 size = 0;
821 break;
822 case _STAG_FIXEDIO:
823 if (size != 3)
824 goto __skip;
825 isapnp_add_fixed_port_resource(dev, &res, dependent, size);
826 size = 0;
827 break;
828 case _STAG_VENDOR:
829 break;
830 case _LTAG_MEMRANGE:
831 if (size != 9)
832 goto __skip;
833 isapnp_add_mem_resource(dev, &res, dependent, size);
834 size = 0;
835 break;
836 case _LTAG_ANSISTR:
837 if (dev->name[0] == '\0') {
838 unsigned short size1 = size > 47 ? 47 : size;
839 isapnp_peek(dev->name, size1);
840 dev->name[size1] = '\0';
841 size -= size1;
843 break;
844 case _LTAG_UNICODESTR:
845 /* silently ignore */
846 /* who use unicode for hardware identification? */
847 break;
848 case _LTAG_VENDOR:
849 break;
850 case _LTAG_MEM32RANGE:
851 if (size != 17)
852 goto __skip;
853 isapnp_add_mem32_resource(dev, &res, dependent, size);
854 size = 0;
855 break;
856 case _LTAG_FIXEDMEM32RANGE:
857 if (size != 17)
858 goto __skip;
859 isapnp_add_fixed_mem32_resource(dev, &res, dependent, size);
860 size = 0;
861 break;
862 case _STAG_END:
863 if (size > 0)
864 isapnp_skip_bytes(size);
865 return 1;
866 default:
867 printk("isapnp: unexpected or unknown tag type 0x%x for logical device %i (device %i), ignored\n", type, dev->devfn, card->number);
869 __skip:
870 if (size > 0)
871 isapnp_skip_bytes(size);
873 isapnp_config_prepare(dev);
874 return 0;
878 * Parse resource map for ISA PnP card.
881 static void __init isapnp_parse_resource_map(struct pci_bus *card)
883 unsigned char type, tmp[17];
884 unsigned short size;
886 while (1) {
887 if (isapnp_read_tag(&type, &size)<0)
888 return;
889 switch (type) {
890 case _STAG_PNPVERNO:
891 if (size != 2)
892 goto __skip;
893 isapnp_peek(tmp, 2);
894 card->pnpver = tmp[0];
895 card->productver = tmp[1];
896 size = 0;
897 break;
898 case _STAG_LOGDEVID:
899 if (size >= 5 && size <= 6) {
900 if (isapnp_create_device(card, size)==1)
901 return;
902 size = 0;
904 break;
905 case _STAG_VENDOR:
906 break;
907 case _LTAG_ANSISTR:
908 if (card->name[0] == '\0') {
909 unsigned short size1 = size > 47 ? 47 : size;
910 isapnp_peek(card->name, size1);
911 card->name[size1] = '\0';
912 size -= size1;
914 break;
915 case _LTAG_UNICODESTR:
916 /* silently ignore */
917 /* who use unicode for hardware identification? */
918 break;
919 case _LTAG_VENDOR:
920 break;
921 case _STAG_END:
922 if (size > 0)
923 isapnp_skip_bytes(size);
924 return;
925 default:
926 printk("isapnp: unexpected or unknown tag type 0x%x for device %i, ignored\n", type, card->number);
928 __skip:
929 if (size > 0)
930 isapnp_skip_bytes(size);
935 * Compute ISA PnP checksum for first eight bytes.
938 static unsigned char __init isapnp_checksum(unsigned char *data)
940 int i, j;
941 unsigned char checksum = 0x6a, bit, b;
943 for (i = 0; i < 8; i++) {
944 b = data[i];
945 for (j = 0; j < 8; j++) {
946 bit = 0;
947 if (b & (1 << j))
948 bit = 1;
949 checksum = ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7) | (checksum >> 1);
952 return checksum;
956 * Build device list for all present ISA PnP devices.
959 static int __init isapnp_build_device_list(void)
961 int csn;
962 unsigned char header[9], checksum;
963 struct pci_bus *card;
964 struct pci_dev *dev;
966 isapnp_wait();
967 isapnp_key();
968 for (csn = 1; csn <= 10; csn++) {
969 isapnp_wake(csn);
970 isapnp_peek(header, 9);
971 checksum = isapnp_checksum(header);
972 #if 0
973 printk("vendor: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
974 header[0], header[1], header[2], header[3],
975 header[4], header[5], header[6], header[7], header[8]);
976 printk("checksum = 0x%x\n", checksum);
977 #endif
978 if (checksum == 0x00 || checksum != header[8]) /* not valid CSN */
979 continue;
980 if ((card = isapnp_alloc(sizeof(struct pci_bus))) == NULL)
981 continue;
982 card->number = csn;
983 card->vendor = (header[1] << 8) | header[0];
984 card->device = (header[3] << 8) | header[2];
985 card->serial = (header[7] << 24) | (header[6] << 16) | (header[5] << 8) | header[4];
986 isapnp_checksum_value = 0x00;
987 INIT_LIST_HEAD(&card->children);
988 INIT_LIST_HEAD(&card->devices);
989 isapnp_parse_resource_map(card);
990 if (isapnp_checksum_value != 0x00)
991 printk("isapnp: checksum for device %i is not valid (0x%x)\n", csn, isapnp_checksum_value);
992 card->checksum = isapnp_checksum_value;
994 list_add_tail(&card->node, &isapnp_cards);
996 isapnp_for_each_dev(dev) {
997 isapnp_fixup_device(dev);
999 return 0;
1003 * Basic configuration routines.
1006 int isapnp_present(void)
1008 return !list_empty(&isapnp_devices);
1011 int isapnp_cfg_begin(int csn, int logdev)
1013 if (csn < 1 || csn > 10 || logdev > 10)
1014 return -EINVAL;
1015 MOD_INC_USE_COUNT;
1016 down(&isapnp_cfg_mutex);
1017 isapnp_wait();
1018 isapnp_key();
1019 isapnp_wake(csn);
1020 #if 1 /* to avoid malfunction when the isapnptools package is used */
1021 isapnp_set_rdp();
1022 udelay(1000); /* delay 1000us */
1023 write_address(0x01);
1024 udelay(1000); /* delay 1000us */
1025 #endif
1026 if (logdev >= 0)
1027 isapnp_device(logdev);
1028 return 0;
1031 int isapnp_cfg_end(void)
1033 isapnp_wait();
1034 up(&isapnp_cfg_mutex);
1035 MOD_DEC_USE_COUNT;
1036 return 0;
1040 * Resource manager.
1043 static struct isapnp_port *isapnp_find_port(struct pci_dev *dev, int index)
1045 struct isapnp_resources *res;
1046 struct isapnp_port *port;
1048 if (!dev || index < 0 || index > 7)
1049 return NULL;
1050 for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1051 for (port = res->port; port; port = port->next) {
1052 if (!index)
1053 return port;
1054 index--;
1057 return NULL;
1060 struct isapnp_irq *isapnp_find_irq(struct pci_dev *dev, int index)
1062 struct isapnp_resources *res, *resa;
1063 struct isapnp_irq *irq;
1064 int index1, index2, index3;
1066 if (!dev || index < 0 || index > 7)
1067 return NULL;
1068 for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1069 index3 = 0;
1070 for (resa = res; resa; resa = resa->alt) {
1071 index1 = index;
1072 index2 = 0;
1073 for (irq = resa->irq; irq; irq = irq->next) {
1074 if (!index1)
1075 return irq;
1076 index1--;
1077 index2++;
1079 if (index3 < index2)
1080 index3 = index2;
1082 index -= index3;
1084 return NULL;
1087 struct isapnp_dma *isapnp_find_dma(struct pci_dev *dev, int index)
1089 struct isapnp_resources *res;
1090 struct isapnp_dma *dma;
1092 if (!dev || index < 0 || index > 7)
1093 return NULL;
1094 for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1095 for (dma = res->dma; dma; dma = dma->next) {
1096 if (!index)
1097 return dma;
1098 index--;
1101 return NULL;
1104 struct isapnp_mem *isapnp_find_mem(struct pci_dev *dev, int index)
1106 struct isapnp_resources *res;
1107 struct isapnp_mem *mem;
1109 if (!dev || index < 0 || index > 7)
1110 return NULL;
1111 for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1112 for (mem = res->mem; mem; mem = mem->next) {
1113 if (!index)
1114 return mem;
1115 index--;
1118 return NULL;
1121 struct isapnp_mem32 *isapnp_find_mem32(struct pci_dev *dev, int index)
1123 struct isapnp_resources *res;
1124 struct isapnp_mem32 *mem32;
1126 if (!dev || index < 0 || index > 7)
1127 return NULL;
1128 for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1129 for (mem32 = res->mem32; mem32; mem32 = mem32->next) {
1130 if (!index)
1131 return mem32;
1132 index--;
1135 return NULL;
1139 * Device manager.
1142 struct pci_bus *isapnp_find_card(unsigned short vendor,
1143 unsigned short device,
1144 struct pci_bus *from)
1146 struct list_head *list;
1148 list = isapnp_cards.next;
1149 if (from)
1150 list = from->node.next;
1152 while (list != &isapnp_cards) {
1153 struct pci_bus *card = pci_bus_b(list);
1154 if (card->vendor == vendor && card->device == device)
1155 return card;
1156 list = list->next;
1158 return NULL;
1161 struct pci_dev *isapnp_find_dev(struct pci_bus *card,
1162 unsigned short vendor,
1163 unsigned short function,
1164 struct pci_dev *from)
1166 if (card == NULL) { /* look for a logical device from all cards */
1167 struct list_head *list;
1169 list = isapnp_devices.next;
1170 if (from)
1171 list = from->global_list.next;
1173 while (list != &isapnp_devices) {
1174 int idx;
1175 struct pci_dev *dev = pci_dev_g(list);
1177 if (dev->vendor == vendor && dev->device == function)
1178 return dev;
1179 for (idx = 0; idx < DEVICE_COUNT_COMPATIBLE; idx++)
1180 if (dev->vendor_compatible[idx] == vendor &&
1181 dev->device_compatible[idx] == function)
1182 return dev;
1183 list = list->next;
1185 } else {
1186 struct list_head *list;
1188 list = card->devices.next;
1189 if (from) {
1190 list = from->bus_list.next;
1191 if (from->bus != card) /* something is wrong */
1192 return NULL;
1194 while (list != &card->devices) {
1195 int idx;
1196 struct pci_dev *dev = pci_dev_b(list);
1198 if (dev->vendor == vendor && dev->device == function)
1199 return dev;
1200 for (idx = 0; idx < DEVICE_COUNT_COMPATIBLE; idx++)
1201 if (dev->vendor_compatible[idx] == vendor &&
1202 dev->device_compatible[idx] == function)
1203 return dev;
1204 list = list->next;
1207 return NULL;
1210 static unsigned int isapnp_dma_resource_flags(struct isapnp_dma *dma)
1212 return dma->flags | IORESOURCE_DMA | IORESOURCE_AUTO;
1215 static unsigned int isapnp_mem_resource_flags(struct isapnp_mem *mem)
1217 unsigned int result;
1219 result = mem->flags | IORESOURCE_MEM | IORESOURCE_AUTO;
1220 if (!(mem->flags & IORESOURCE_MEM_WRITEABLE))
1221 result |= IORESOURCE_READONLY;
1222 if (mem->flags & IORESOURCE_MEM_CACHEABLE)
1223 result |= IORESOURCE_CACHEABLE;
1224 if (mem->flags & IORESOURCE_MEM_RANGELENGTH)
1225 result |= IORESOURCE_RANGELENGTH;
1226 if (mem->flags & IORESOURCE_MEM_SHADOWABLE)
1227 result |= IORESOURCE_SHADOWABLE;
1228 return result;
1231 static unsigned int isapnp_irq_resource_flags(struct isapnp_irq *irq)
1233 return irq->flags | IORESOURCE_IRQ | IORESOURCE_AUTO;
1236 static unsigned int isapnp_port_resource_flags(struct isapnp_port *port)
1238 return port->flags | IORESOURCE_IO | IORESOURCE_AUTO;
1241 static int isapnp_config_prepare(struct pci_dev *dev)
1243 struct isapnp_resources *res, *resa;
1244 struct isapnp_port *port;
1245 struct isapnp_irq *irq;
1246 struct isapnp_dma *dma;
1247 struct isapnp_mem *mem;
1248 int port_count, port_count1;
1249 int irq_count, irq_count1;
1250 int dma_count, dma_count1;
1251 int mem_count, mem_count1;
1252 int idx;
1254 if (dev == NULL)
1255 return -EINVAL;
1256 if (dev->active || dev->ro)
1257 return -EBUSY;
1258 for (idx = 0; idx < DEVICE_COUNT_IRQ; idx++) {
1259 dev->irq_resource[idx].name = NULL;
1260 dev->irq_resource[idx].start = 0;
1261 dev->irq_resource[idx].end = 0;
1262 dev->irq_resource[idx].flags = 0;
1264 for (idx = 0; idx < DEVICE_COUNT_DMA; idx++) {
1265 dev->dma_resource[idx].name = NULL;
1266 dev->dma_resource[idx].start = 0;
1267 dev->dma_resource[idx].end = 0;
1268 dev->dma_resource[idx].flags = 0;
1270 for (idx = 0; idx < DEVICE_COUNT_RESOURCE; idx++) {
1271 dev->resource[idx].name = NULL;
1272 dev->resource[idx].start = 0;
1273 dev->resource[idx].end = 0;
1274 dev->resource[idx].flags = 0;
1276 port_count = irq_count = dma_count = mem_count = 0;
1277 for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1278 port_count1 = irq_count1 = dma_count1 = mem_count1 = 0;
1279 for (resa = res; resa; resa = resa->alt) {
1280 for (port = resa->port, idx = 0; port; port = port->next, idx++) {
1281 if (dev->resource[port_count + idx].flags == 0) {
1282 dev->resource[port_count + idx].flags = isapnp_port_resource_flags(port);
1283 dev->resource[port_count + idx].end = port->size;
1286 if (port_count1 < idx)
1287 port_count1 = idx;
1288 for (irq = resa->irq, idx = 0; irq; irq = irq->next, idx++) {
1289 int count = irq_count + idx;
1290 if (count < DEVICE_COUNT_IRQ) {
1291 if (dev->irq_resource[count].flags == 0) {
1292 dev->irq_resource[count].flags = isapnp_irq_resource_flags(irq);
1297 if (irq_count1 < idx)
1298 irq_count1 = idx;
1299 for (dma = resa->dma, idx = 0; dma; dma = dma->next, idx++)
1300 if (dev->dma_resource[idx].flags == 0) {
1301 dev->dma_resource[idx].flags = isapnp_dma_resource_flags(dma);
1303 if (dma_count1 < idx)
1304 dma_count1 = idx;
1305 for (mem = resa->mem, idx = 0; mem; mem = mem->next, idx++)
1306 if (dev->resource[mem_count + idx + 8].flags == 0) {
1307 dev->resource[mem_count + idx + 8].flags = isapnp_mem_resource_flags(mem);
1309 if (mem_count1 < idx)
1310 mem_count1 = idx;
1312 port_count += port_count1;
1313 irq_count += irq_count1;
1314 dma_count += dma_count1;
1315 mem_count += mem_count1;
1317 return 0;
1320 struct isapnp_cfgtmp {
1321 struct isapnp_port *port[8];
1322 struct isapnp_irq *irq[2];
1323 struct isapnp_dma *dma[2];
1324 struct isapnp_mem *mem[4];
1325 struct pci_dev *request;
1326 struct pci_dev result;
1329 static int isapnp_alternative_switch(struct isapnp_cfgtmp *cfg,
1330 struct isapnp_resources *from,
1331 struct isapnp_resources *to)
1333 int tmp, tmp1;
1334 struct isapnp_port *port;
1335 struct isapnp_irq *irq;
1336 struct isapnp_dma *dma;
1337 struct isapnp_mem *mem;
1339 if (!cfg)
1340 return -EINVAL;
1341 /* process port settings */
1342 for (tmp = 0; tmp < 8; tmp++) {
1343 if (!(cfg->request->resource[tmp].flags & IORESOURCE_AUTO))
1344 continue; /* don't touch */
1345 port = cfg->port[tmp];
1346 if (!port) {
1347 cfg->port[tmp] = port = isapnp_find_port(cfg->request, tmp);
1348 if (!port)
1349 return -EINVAL;
1351 if (from && port->res == from) {
1352 while (port->res != to) {
1353 if (!port->res->alt)
1354 return -EINVAL;
1355 port = port->res->alt->port;
1356 for (tmp1 = tmp; tmp1 > 0 && port; tmp1--)
1357 port = port->next;
1358 cfg->port[tmp] = port;
1359 if (!port)
1360 return -ENOENT;
1361 cfg->result.resource[tmp].flags = isapnp_port_resource_flags(port);
1365 /* process irq settings */
1366 for (tmp = 0; tmp < 2; tmp++) {
1367 if (!(cfg->request->irq_resource[tmp].flags & IORESOURCE_AUTO))
1368 continue; /* don't touch */
1369 irq = cfg->irq[tmp];
1370 if (!irq) {
1371 cfg->irq[tmp] = irq = isapnp_find_irq(cfg->request, tmp);
1372 if (!irq)
1373 return -EINVAL;
1375 if (from && irq->res == from) {
1376 while (irq->res != to) {
1377 if (!irq->res->alt)
1378 return -EINVAL;
1379 irq = irq->res->alt->irq;
1380 for (tmp1 = tmp; tmp1 > 0 && irq; tmp1--)
1381 irq = irq->next;
1382 cfg->irq[tmp] = irq;
1383 if (!irq)
1384 return -ENOENT;
1385 cfg->result.irq_resource[tmp].flags = isapnp_irq_resource_flags(irq);
1389 /* process dma settings */
1390 for (tmp = 0; tmp < 2; tmp++) {
1391 if (!(cfg->request->dma_resource[tmp].flags & IORESOURCE_AUTO))
1392 continue; /* don't touch */
1393 dma = cfg->dma[tmp];
1394 if (!dma) {
1395 cfg->dma[tmp] = dma = isapnp_find_dma(cfg->request, tmp);
1396 if (!dma)
1397 return -EINVAL;
1399 if (from && dma->res == from) {
1400 while (dma->res != to) {
1401 if (!dma->res->alt)
1402 return -EINVAL;
1403 dma = dma->res->alt->dma;
1404 for (tmp1 = tmp; tmp1 > 0 && dma; tmp1--)
1405 dma = dma->next;
1406 cfg->dma[tmp] = dma;
1407 if (!dma)
1408 return -ENOENT;
1409 cfg->result.dma_resource[tmp].flags = isapnp_dma_resource_flags(dma);
1413 /* process memory settings */
1414 for (tmp = 0; tmp < 4; tmp++) {
1415 if (!(cfg->request->resource[tmp + 8].flags & IORESOURCE_AUTO))
1416 continue; /* don't touch */
1417 mem = cfg->mem[tmp];
1418 if (!mem) {
1419 cfg->mem[tmp] = mem = isapnp_find_mem(cfg->request, tmp);
1420 if (!mem)
1421 return -EINVAL;
1423 if (from && mem->res == from) {
1424 while (mem->res != to) {
1425 if (!mem->res->alt)
1426 return -EINVAL;
1427 mem = mem->res->alt->mem;
1428 for (tmp1 = tmp; tmp1 > 0 && mem; tmp1--)
1429 mem = mem->next;
1430 cfg->mem[tmp] = mem;
1431 if (!mem)
1432 return -ENOENT;
1433 cfg->result.resource[tmp + 8].flags = isapnp_mem_resource_flags(mem);
1437 return 0;
1440 static int isapnp_check_port(struct isapnp_cfgtmp *cfg, int port, int size, int idx)
1442 int i, tmp, rport, rsize;
1443 struct isapnp_port *xport;
1444 struct pci_dev *dev;
1446 if (check_region(port, size))
1447 return 1;
1448 for (i = 0; i < 8; i++) {
1449 rport = isapnp_reserve_io[i << 1];
1450 rsize = isapnp_reserve_io[(i << 1) + 1];
1451 if (port >= rport && port < rport + rsize)
1452 return 1;
1453 if (port + size > rport && port + size < (rport + rsize) - 1)
1454 return 1;
1457 isapnp_for_each_dev(dev) {
1458 if (dev->active) {
1459 for (tmp = 0; tmp < 8; tmp++) {
1460 if (dev->resource[tmp].flags) {
1461 rport = dev->resource[tmp].start;
1462 rsize = (dev->resource[tmp].end - rport) + 1;
1463 if (port >= rport && port < rport + rsize)
1464 return 1;
1465 if (port + size > rport && port + size < (rport + rsize) - 1)
1466 return 1;
1471 for (i = 0; i < 8; i++) {
1472 unsigned int flags;
1473 if (i == idx)
1474 continue;
1475 flags = cfg->request->resource[i].flags;
1476 if (!flags)
1477 continue;
1478 tmp = cfg->request->resource[i].start;
1479 if (flags & IORESOURCE_AUTO) { /* auto */
1480 xport = cfg->port[i];
1481 if (!xport)
1482 return 1;
1483 if (cfg->result.resource[i].flags & IORESOURCE_AUTO)
1484 continue;
1485 tmp = cfg->result.resource[i].start;
1486 if (tmp + xport->size >= port && tmp <= port + xport->size)
1487 return 1;
1488 continue;
1490 if (port == tmp)
1491 return 1;
1492 xport = isapnp_find_port(cfg->request, i);
1493 if (!xport)
1494 return 1;
1495 if (tmp + xport->size >= port && tmp <= port + xport->size)
1496 return 1;
1498 return 0;
1501 static int isapnp_valid_port(struct isapnp_cfgtmp *cfg, int idx)
1503 int err;
1504 unsigned long *value1, *value2;
1505 struct isapnp_port *port;
1507 if (!cfg || idx < 0 || idx > 7)
1508 return -EINVAL;
1509 if (!(cfg->result.resource[idx].flags & IORESOURCE_AUTO)) /* don't touch */
1510 return 0;
1511 __again:
1512 port = cfg->port[idx];
1513 if (!port)
1514 return -EINVAL;
1515 value1 = &cfg->result.resource[idx].start;
1516 value2 = &cfg->result.resource[idx].end;
1517 if (cfg->result.resource[idx].flags & IORESOURCE_AUTO) {
1518 cfg->result.resource[idx].flags &= ~IORESOURCE_AUTO;
1519 *value1 = port->min;
1520 *value2 = port->min + port->size - 1;
1521 if (!isapnp_check_port(cfg, *value1, port->size, idx))
1522 return 0;
1524 do {
1525 *value1 += port->align;
1526 *value2 = *value1 + port->size - 1;
1527 if (*value1 > port->max || !port->align) {
1528 if (port->res && port->res->alt) {
1529 if ((err = isapnp_alternative_switch(cfg, port->res, port->res->alt))<0)
1530 return err;
1531 goto __again;
1533 return -ENOENT;
1535 } while (isapnp_check_port(cfg, *value1, port->size, idx));
1536 return 0;
1539 static void isapnp_test_handler(int irq, void *dev_id, struct pt_regs *regs)
1543 static int isapnp_check_interrupt(struct isapnp_cfgtmp *cfg, int irq, int idx)
1545 int i;
1546 struct pci_dev *dev;
1548 if (irq < 0 || irq > 15)
1549 return 1;
1550 for (i = 0; i < 16; i++) {
1551 if (isapnp_reserve_irq[i] == irq)
1552 return 1;
1554 isapnp_for_each_dev(dev) {
1555 if (dev->active) {
1556 if (dev->irq_resource[0].start == irq ||
1557 dev->irq_resource[1].start == irq)
1558 return 1;
1561 if (request_irq(irq, isapnp_test_handler, SA_INTERRUPT, "isapnp", NULL))
1562 return 1;
1563 free_irq(irq, NULL);
1564 for (i = 0; i < DEVICE_COUNT_IRQ; i++) {
1565 if (i == idx)
1566 continue;
1567 if (!cfg->result.irq_resource[i].flags)
1568 continue;
1569 if (cfg->result.irq_resource[i].flags & IORESOURCE_AUTO)
1570 continue;
1571 if (cfg->result.irq_resource[i].start == irq)
1572 return 1;
1574 return 0;
1577 static int isapnp_valid_irq(struct isapnp_cfgtmp *cfg, int idx)
1579 /* IRQ priority: this table is good for i386 */
1580 static unsigned short xtab[16] = {
1581 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
1583 int err, i;
1584 unsigned long *value1, *value2;
1585 struct isapnp_irq *irq;
1587 if (!cfg || idx < 0 || idx > 1)
1588 return -EINVAL;
1589 if (!(cfg->result.irq_resource[idx].flags & IORESOURCE_AUTO))
1590 return 0;
1591 __again:
1592 irq = cfg->irq[idx];
1593 if (!irq)
1594 return -EINVAL;
1595 value1 = &cfg->result.irq_resource[idx].start;
1596 value2 = &cfg->result.irq_resource[idx].end;
1597 if (cfg->result.irq_resource[idx].flags & IORESOURCE_AUTO) {
1598 for (i = 0; i < 16 && !(irq->map & (1<<xtab[i])); i++);
1599 if (i >= 16)
1600 return -ENOENT;
1601 cfg->result.irq_resource[idx].flags &= ~IORESOURCE_AUTO;
1602 if (!isapnp_check_interrupt(cfg, *value1 = *value2 = xtab[i], idx))
1603 return 0;
1605 do {
1606 for (i = 0; i < 16 && xtab[i] != *value1; i++);
1607 for (i++; i < 16 && !(irq->map & (1<<xtab[i])); i++);
1608 if (i >= 16) {
1609 if (irq->res && irq->res->alt) {
1610 if ((err = isapnp_alternative_switch(cfg, irq->res, irq->res->alt))<0)
1611 return err;
1612 goto __again;
1614 return -ENOENT;
1615 } else {
1616 *value1 = *value2 = xtab[i];
1618 } while (isapnp_check_interrupt(cfg, *value1, idx));
1619 return 0;
1622 static int isapnp_check_dma(struct isapnp_cfgtmp *cfg, int dma, int idx)
1624 int i;
1625 struct pci_dev *dev;
1627 /* Some machines allow DMA 0, but others don't. In fact on some
1628 boxes DMA 0 is the memory refresh. Play safe */
1630 if (dma < 1 || dma == 4 || dma > 7)
1631 return 1;
1632 for (i = 0; i < 8; i++) {
1633 if (isapnp_reserve_dma[i] == dma)
1634 return 1;
1636 isapnp_for_each_dev(dev) {
1637 if (dev->active) {
1638 if (dev->dma_resource[0].start == dma || dev->dma_resource[1].start == dma)
1639 return 1;
1642 if (request_dma(dma, "isapnp"))
1643 return 1;
1644 free_dma(dma);
1645 for (i = 0; i < 2; i++) {
1646 if (i == idx)
1647 continue;
1648 if (!cfg->result.dma_resource[i].flags ||
1649 (cfg->result.dma_resource[i].flags & IORESOURCE_AUTO))
1650 continue;
1651 if (cfg->result.dma_resource[i].start == dma)
1652 return 1;
1654 return 0;
1657 static int isapnp_valid_dma(struct isapnp_cfgtmp *cfg, int idx)
1659 int err, i;
1660 unsigned long *value1, *value2;
1661 struct isapnp_dma *dma;
1663 if (!cfg || idx < 0 || idx > 1)
1664 return -EINVAL;
1665 if (!(cfg->result.dma_resource[idx].flags & IORESOURCE_AUTO)) /* don't touch */
1666 return 0;
1667 __again:
1668 dma = cfg->dma[idx];
1669 if (!dma)
1670 return -EINVAL;
1671 value1 = &cfg->result.dma_resource[idx].start;
1672 value2 = &cfg->result.dma_resource[idx].end;
1673 if (cfg->result.dma_resource[idx].flags & IORESOURCE_AUTO) {
1674 for (i = 0; i < 8 && !(dma->map & (1<<i)); i++);
1675 if (i >= 8)
1676 return -ENOENT;
1677 cfg->result.dma_resource[idx].flags &= ~IORESOURCE_AUTO;
1678 if (!isapnp_check_dma(cfg, *value1 = *value2 = i, idx))
1679 return 0;
1681 do {
1682 for (i = *value1 + 1; i < 8 && !(dma->map & (1<<i)); i++);
1683 if (i >= 8) {
1684 if (dma->res && dma->res->alt) {
1685 if ((err = isapnp_alternative_switch(cfg, dma->res, dma->res->alt))<0)
1686 return err;
1687 goto __again;
1689 return -ENOENT;
1690 } else {
1691 *value1 = *value2 = i;
1693 } while (isapnp_check_dma(cfg, *value1, idx));
1694 return 0;
1697 static int isapnp_check_mem(struct isapnp_cfgtmp *cfg, unsigned int addr, unsigned int size, int idx)
1699 int i, tmp;
1700 unsigned int raddr, rsize;
1701 struct isapnp_mem *xmem;
1702 struct pci_dev *dev;
1704 for (i = 0; i < 8; i++) {
1705 raddr = (unsigned int)isapnp_reserve_mem[i << 1];
1706 rsize = (unsigned int)isapnp_reserve_mem[(i << 1) + 1];
1707 if (addr >= raddr && addr < raddr + rsize)
1708 return 1;
1709 if (addr + size > raddr && addr + size < (raddr + rsize) - 1)
1710 return 1;
1711 if (__check_region(&iomem_resource, addr, size))
1712 return 1;
1714 isapnp_for_each_dev(dev) {
1715 if (dev->active) {
1716 for (tmp = 0; tmp < 4; tmp++) {
1717 if (dev->resource[tmp].flags) {
1718 raddr = dev->resource[tmp + 8].start;
1719 rsize = (dev->resource[tmp + 8].end - raddr) + 1;
1720 if (addr >= raddr && addr < raddr + rsize)
1721 return 1;
1722 if (addr + size > raddr && addr + size < (raddr + rsize) - 1)
1723 return 1;
1728 for (i = 0; i < 4; i++) {
1729 unsigned int flags = cfg->request->resource[i + 8].flags;
1730 if (i == idx)
1731 continue;
1732 if (!flags)
1733 continue;
1734 tmp = cfg->result.resource[i + 8].start;
1735 if (flags & IORESOURCE_AUTO) { /* auto */
1736 xmem = cfg->mem[i];
1737 if (!xmem)
1738 return 1;
1739 if (cfg->result.resource[i + 8].flags & IORESOURCE_AUTO)
1740 continue;
1741 if (tmp + xmem->size >= addr && tmp <= addr + xmem->size)
1742 return 1;
1743 continue;
1745 if (addr == tmp)
1746 return 1;
1747 xmem = isapnp_find_mem(cfg->request, i);
1748 if (!xmem)
1749 return 1;
1750 if (tmp + xmem->size >= addr && tmp <= addr + xmem->size)
1751 return 1;
1753 return 0;
1756 static int isapnp_valid_mem(struct isapnp_cfgtmp *cfg, int idx)
1758 int err;
1759 unsigned long *value1, *value2;
1760 struct isapnp_mem *mem;
1762 if (!cfg || idx < 0 || idx > 3)
1763 return -EINVAL;
1764 if (!(cfg->result.resource[idx + 8].flags & IORESOURCE_AUTO)) /* don't touch */
1765 return 0;
1766 __again:
1767 mem = cfg->mem[idx];
1768 if (!mem)
1769 return -EINVAL;
1770 value1 = &cfg->result.resource[idx + 8].start;
1771 value2 = &cfg->result.resource[idx + 8].end;
1772 if (cfg->result.resource[idx + 8].flags & IORESOURCE_AUTO) {
1773 cfg->result.resource[idx + 8].flags &= ~IORESOURCE_AUTO;
1774 *value1 = mem->min;
1775 *value2 = mem->min + mem->size - 1;
1776 if (!isapnp_check_mem(cfg, *value1, mem->size, idx))
1777 return 0;
1779 do {
1780 *value1 += mem->align;
1781 *value2 = *value1 + mem->size - 1;
1782 if (*value1 > mem->max || !mem->align) {
1783 if (mem->res && mem->res->alt) {
1784 if ((err = isapnp_alternative_switch(cfg, mem->res, mem->res->alt))<0)
1785 return err;
1786 goto __again;
1788 return -ENOENT;
1790 } while (isapnp_check_mem(cfg, *value1, mem->size, idx));
1791 return 0;
1794 static int isapnp_check_valid(struct isapnp_cfgtmp *cfg)
1796 int tmp;
1798 for (tmp = 0; tmp < 8; tmp++)
1799 if (cfg->result.resource[tmp].flags & IORESOURCE_AUTO)
1800 return -EAGAIN;
1801 for (tmp = 0; tmp < 2; tmp++)
1802 if (cfg->result.irq_resource[tmp].flags & IORESOURCE_AUTO)
1803 return -EAGAIN;
1804 for (tmp = 0; tmp < 2; tmp++)
1805 if (cfg->result.dma_resource[tmp].flags & IORESOURCE_AUTO)
1806 return -EAGAIN;
1807 for (tmp = 0; tmp < 4; tmp++)
1808 if (cfg->result.resource[tmp + 8].flags & IORESOURCE_AUTO)
1809 return -EAGAIN;
1810 return 0;
1813 static int isapnp_config_activate(struct pci_dev *dev)
1815 struct isapnp_cfgtmp cfg;
1816 int tmp, fauto, err;
1818 if (!dev)
1819 return -EINVAL;
1820 if (dev->active)
1821 return -EBUSY;
1822 memset(&cfg, 0, sizeof(cfg));
1823 cfg.request = dev;
1824 memcpy(&cfg.result, dev, sizeof(struct pci_dev));
1825 /* check if all values are set, otherwise try auto-configuration */
1826 for (tmp = fauto = 0; !fauto && tmp < 8; tmp++) {
1827 if (dev->resource[tmp].flags & IORESOURCE_AUTO)
1828 fauto++;
1830 for (tmp = 0; !fauto && tmp < 2; tmp++) {
1831 if (dev->irq_resource[tmp].flags & IORESOURCE_AUTO)
1832 fauto++;
1834 for (tmp = 0; !fauto && tmp < 2; tmp++) {
1835 if (dev->dma_resource[tmp].flags & IORESOURCE_AUTO)
1836 fauto++;
1838 for (tmp = 0; !fauto && tmp < 4; tmp++) {
1839 if (dev->resource[tmp + 8].flags & IORESOURCE_AUTO)
1840 fauto++;
1842 if (!fauto)
1843 goto __skip_auto;
1844 /* set variables to initial values */
1845 if ((err = isapnp_alternative_switch(&cfg, NULL, NULL))<0)
1846 return err;
1847 /* find first valid configuration */
1848 fauto = 0;
1849 do {
1850 for (tmp = 0; tmp < 8 && cfg.result.resource[tmp].flags; tmp++)
1851 if ((err = isapnp_valid_port(&cfg, tmp))<0)
1852 return err;
1853 for (tmp = 0; tmp < 2 && cfg.result.irq_resource[tmp].flags; tmp++)
1854 if ((err = isapnp_valid_irq(&cfg, tmp))<0)
1855 return err;
1856 for (tmp = 0; tmp < 2 && cfg.result.dma_resource[tmp].flags; tmp++)
1857 if ((err = isapnp_valid_dma(&cfg, tmp))<0)
1858 return err;
1859 for (tmp = 0; tmp < 4 && cfg.result.resource[tmp + 8].flags; tmp++)
1860 if ((err = isapnp_valid_mem(&cfg, tmp))<0)
1861 return err;
1862 } while (isapnp_check_valid(&cfg)<0 && fauto++ < 20);
1863 if (fauto >= 20)
1864 return -EAGAIN;
1865 __skip_auto:
1866 /* we have valid configuration, try configure hardware */
1867 isapnp_cfg_begin(dev->bus->number, dev->devfn);
1868 dev->active = 1;
1869 dev->irq_resource[0] = cfg.result.irq_resource[0];
1870 dev->irq_resource[1] = cfg.result.irq_resource[1];
1871 dev->dma_resource[0] = cfg.result.dma_resource[0];
1872 dev->dma_resource[1] = cfg.result.dma_resource[1];
1873 for (tmp = 0; tmp < 12; tmp++) {
1874 dev->resource[tmp] = cfg.result.resource[tmp];
1876 for (tmp = 0; tmp < 8 && dev->resource[tmp].flags; tmp++)
1877 isapnp_write_word(ISAPNP_CFG_PORT+(tmp<<1), dev->resource[tmp].start);
1878 for (tmp = 0; tmp < 2 && dev->irq_resource[tmp].flags; tmp++) {
1879 int irq = dev->irq_resource[tmp].start;
1880 if (irq == 2)
1881 irq = 9;
1882 isapnp_write_byte(ISAPNP_CFG_IRQ+(tmp<<1), irq);
1884 for (tmp = 0; tmp < 2 && dev->dma_resource[tmp].flags; tmp++)
1885 isapnp_write_byte(ISAPNP_CFG_DMA+tmp, dev->dma_resource[tmp].start);
1886 for (tmp = 0; tmp < 4 && dev->resource[tmp+8].flags; tmp++)
1887 isapnp_write_word(ISAPNP_CFG_MEM+(tmp<<2), (dev->resource[tmp + 8].start >> 8) & 0xffff);
1888 isapnp_activate(dev->devfn);
1889 isapnp_cfg_end();
1890 return 0;
1893 static int isapnp_config_deactivate(struct pci_dev *dev)
1895 if (!dev || !dev->active)
1896 return -EINVAL;
1897 isapnp_cfg_begin(dev->bus->number, dev->devfn);
1898 isapnp_deactivate(dev->devfn);
1899 dev->active = 0;
1900 isapnp_cfg_end();
1901 return 0;
1904 void isapnp_resource_change(struct resource *resource,
1905 unsigned long start,
1906 unsigned long size)
1908 if (resource == NULL)
1909 return;
1910 resource->flags &= ~IORESOURCE_AUTO;
1911 resource->start = start;
1912 resource->end = start + size - 1;
1916 * Inititialization.
1919 #ifdef MODULE
1921 static void isapnp_free_port(struct isapnp_port *port)
1923 struct isapnp_port *next;
1925 while (port) {
1926 next = port->next;
1927 kfree(port);
1928 port = next;
1932 static void isapnp_free_irq(struct isapnp_irq *irq)
1934 struct isapnp_irq *next;
1936 while (irq) {
1937 next = irq->next;
1938 kfree(irq);
1939 irq = next;
1943 static void isapnp_free_dma(struct isapnp_dma *dma)
1945 struct isapnp_dma *next;
1947 while (dma) {
1948 next = dma->next;
1949 kfree(dma);
1950 dma = next;
1954 static void isapnp_free_mem(struct isapnp_mem *mem)
1956 struct isapnp_mem *next;
1958 while (mem) {
1959 next = mem->next;
1960 kfree(mem);
1961 mem = next;
1965 static void isapnp_free_mem32(struct isapnp_mem32 *mem32)
1967 struct isapnp_mem32 *next;
1969 while (mem32) {
1970 next = mem32->next;
1971 kfree(mem32);
1972 mem32 = next;
1976 static void isapnp_free_resources(struct isapnp_resources *resources, int alt)
1978 struct isapnp_resources *next;
1980 while (resources) {
1981 next = alt ? resources->alt : resources->next;
1982 isapnp_free_port(resources->port);
1983 isapnp_free_irq(resources->irq);
1984 isapnp_free_dma(resources->dma);
1985 isapnp_free_mem(resources->mem);
1986 isapnp_free_mem32(resources->mem32);
1987 if (!alt && resources->alt)
1988 isapnp_free_resources(resources->alt, 1);
1989 kfree(resources);
1990 resources = next;
1994 static void isapnp_free_card(struct pci_bus *card)
1996 while (!list_empty(&card->devices)) {
1997 struct list_head *list = card->devices.next;
1998 struct pci_dev *dev = pci_dev_b(list);
1999 list_del(list);
2000 isapnp_free_resources((struct isapnp_resources *)dev->sysdata, 0);
2001 kfree(dev);
2003 kfree(card);
2006 #endif /* MODULE */
2008 static void isapnp_free_all_resources(void)
2010 #ifdef ISAPNP_REGION_OK
2011 release_resource(pidxr_res);
2012 #endif
2013 release_resource(pnpwrp_res);
2014 if (isapnp_rdp >= 0x203 && isapnp_rdp <= 0x3ff)
2015 release_resource(isapnp_rdp_res);
2016 #ifdef MODULE
2017 while (!list_empty(&isapnp_cards)) {
2018 struct list_head *list = isapnp_cards.next;
2019 list_del(list);
2020 isapnp_free_card(pci_bus_b(list));
2022 #ifdef CONFIG_PROC_FS
2023 isapnp_proc_done();
2024 #endif
2025 #endif
2028 static int __init isapnp_do_reserve_irq(int irq)
2030 int i;
2032 if (irq < 0 || irq > 15)
2033 return -EINVAL;
2034 for (i = 0; i < 16; i++) {
2035 if (isapnp_reserve_irq[i] == irq)
2036 return 0;
2038 for (i = 0; i < 16; i++) {
2039 if (isapnp_reserve_irq[i] < 0) {
2040 isapnp_reserve_irq[i] = irq;
2041 #ifdef ISAPNP_DEBUG
2042 printk("IRQ %i is reserved now.\n", irq);
2043 #endif
2044 return 0;
2047 return -ENOMEM;
2050 #ifdef CONFIG_PCI
2052 static void __init isapnp_pci_init(void)
2054 struct pci_dev *dev;
2056 pci_for_each_dev(dev) {
2057 #ifdef ISAPNP_DEBUG
2058 printk("PCI: reserved IRQ: %i\n", dev->irq);
2059 #endif
2060 if (dev->irq > 0)
2061 isapnp_do_reserve_irq(dev->irq);
2065 #endif /* CONFIG_PCI */
2067 EXPORT_SYMBOL(isapnp_present);
2068 EXPORT_SYMBOL(isapnp_cfg_begin);
2069 EXPORT_SYMBOL(isapnp_cfg_end);
2070 EXPORT_SYMBOL(isapnp_read_byte);
2071 EXPORT_SYMBOL(isapnp_read_word);
2072 EXPORT_SYMBOL(isapnp_read_dword);
2073 EXPORT_SYMBOL(isapnp_write_byte);
2074 EXPORT_SYMBOL(isapnp_write_word);
2075 EXPORT_SYMBOL(isapnp_write_dword);
2076 EXPORT_SYMBOL(isapnp_wake);
2077 EXPORT_SYMBOL(isapnp_device);
2078 EXPORT_SYMBOL(isapnp_activate);
2079 EXPORT_SYMBOL(isapnp_deactivate);
2080 EXPORT_SYMBOL(isapnp_find_card);
2081 EXPORT_SYMBOL(isapnp_find_dev);
2082 EXPORT_SYMBOL(isapnp_resource_change);
2084 int __init isapnp_init(void)
2086 int cards;
2087 struct pci_bus *card;
2089 if (isapnp_disable) {
2090 isapnp_detected = 0;
2091 printk("isapnp: ISA Plug & Play support disabled\n");
2092 return 0;
2094 #ifdef ISAPNP_REGION_OK
2095 pidxr_res=request_region(_PIDXR, 1, "isapnp index");
2096 if(!pidxr_res) {
2097 printk("isapnp: Index Register 0x%x already used\n", _PIDXR);
2098 return -EBUSY;
2100 #endif
2101 pnpwrp_res=request_region(_PNPWRP, 1, "isapnp write");
2102 if(!pnpwrp_res) {
2103 printk("isapnp: Write Data Register 0x%x already used\n", _PNPWRP);
2104 return -EBUSY;
2108 * Print a message. The existing ISAPnP code is hanging machines
2109 * so let the user know where.
2112 printk("isapnp: Scanning for Pnp cards...\n");
2113 if (isapnp_rdp >= 0x203 && isapnp_rdp <= 0x3ff) {
2114 isapnp_rdp |= 3;
2115 isapnp_rdp_res=request_region(isapnp_rdp, 1, "isapnp read");
2116 if(!isapnp_rdp_res) {
2117 printk("isapnp: Read Data Register 0x%x already used\n", isapnp_rdp);
2118 return -EBUSY;
2120 isapnp_set_rdp();
2122 isapnp_detected = 1;
2123 if (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff) {
2124 cards = isapnp_isolate();
2125 if (cards < 0 ||
2126 (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff)) {
2127 isapnp_free_all_resources();
2128 isapnp_detected = 0;
2129 printk("isapnp: No Plug & Play device found\n");
2130 return 0;
2132 isapnp_rdp_res=request_region(isapnp_rdp, 1, "isapnp read");
2134 isapnp_build_device_list();
2135 cards = 0;
2137 isapnp_for_each_card(card) {
2138 cards++;
2139 if (isapnp_verbose) {
2140 struct list_head *devlist;
2141 printk( "isapnp: Card '%s'\n", card->name[0]?card->name:"Unknown");
2142 if (isapnp_verbose < 2)
2143 continue;
2144 for (devlist = card->devices.next; devlist != &card->devices; devlist = devlist->next) {
2145 struct pci_dev *dev = pci_dev_b(devlist);
2146 printk("isapnp: Device '%s'\n", dev->name[0]?card->name:"Unknown");
2150 if (cards) {
2151 printk("isapnp: %i Plug & Play card%s detected total\n", cards, cards>1?"s":"");
2152 } else {
2153 printk("isapnp: No Plug & Play card found\n");
2155 #ifdef CONFIG_PCI
2156 if (!isapnp_skip_pci_scan)
2157 isapnp_pci_init();
2158 #endif
2159 #ifdef CONFIG_PROC_FS
2160 isapnp_proc_init();
2161 #endif
2162 return 0;
2165 #ifdef MODULE
2167 int init_module(void)
2169 return isapnp_init();
2172 void cleanup_module(void)
2174 if (isapnp_detected)
2175 isapnp_free_all_resources();
2178 #endif