1/*
2 *  ISA Plug & Play support
3 *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 *
5 *
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.
10 *
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.
15 *
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.
19 *
20 *  Changelog:
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>
33 */
34
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>
44#include <asm/io.h>
45#include <asm/dma.h>
46#include <asm/irq.h>
47#include <linux/pci.h>
48#include <linux/init.h>
49#include <linux/isapnp.h>
50
51LIST_HEAD(isapnp_cards);
52LIST_HEAD(isapnp_devices);
53
54
55int isapnp_disable;			/* Disable ISA PnP */
56int isapnp_rdp;				/* Read Data Port */
57int isapnp_reset = 1;			/* reset all PnP cards (deactivate) */
58int isapnp_allow_dma0 = -1;		/* allow dma 0 during auto activation: -1=off (:default), 0=off (set by user), 1=on */
59int isapnp_skip_pci_scan;		/* skip PCI resource scanning */
60int isapnp_verbose = 1;			/* verbose mode */
61int isapnp_reserve_irq[16] = { [0 ... 15] = -1 };	/* reserve (don't use) some IRQ */
62int isapnp_reserve_dma[8] = { [0 ... 7] = -1 };		/* reserve (don't use) some DMA */
63int isapnp_reserve_io[16] = { [0 ... 15] = -1 };	/* reserve (don't use) some I/O region */
64int isapnp_reserve_mem[16] = { [0 ... 15] = -1 };	/* reserve (don't use) some memory region */
65
66MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
67MODULE_DESCRIPTION("Generic ISA Plug & Play support");
68MODULE_PARM(isapnp_disable, "i");
69MODULE_PARM_DESC(isapnp_disable, "ISA Plug & Play disable");
70MODULE_PARM(isapnp_rdp, "i");
71MODULE_PARM_DESC(isapnp_rdp, "ISA Plug & Play read data port");
72MODULE_PARM(isapnp_reset, "i");
73MODULE_PARM_DESC(isapnp_reset, "ISA Plug & Play reset all cards");
74MODULE_PARM(isapnp_allow_dma0, "i");
75MODULE_PARM_DESC(isapnp_allow_dma0, "Allow dma value 0 during auto activation");
76MODULE_PARM(isapnp_skip_pci_scan, "i");
77MODULE_PARM_DESC(isapnp_skip_pci_scan, "ISA Plug & Play skip PCI resource scanning");
78MODULE_PARM(isapnp_verbose, "i");
79MODULE_PARM_DESC(isapnp_verbose, "ISA Plug & Play verbose mode");
80MODULE_PARM(isapnp_reserve_irq, "1-16i");
81MODULE_PARM_DESC(isapnp_reserve_irq, "ISA Plug & Play - reserve IRQ line(s)");
82MODULE_PARM(isapnp_reserve_dma, "1-8i");
83MODULE_PARM_DESC(isapnp_reserve_dma, "ISA Plug & Play - reserve DMA channel(s)");
84MODULE_PARM(isapnp_reserve_io, "1-16i");
85MODULE_PARM_DESC(isapnp_reserve_io, "ISA Plug & Play - reserve I/O region(s) - port,size");
86MODULE_PARM(isapnp_reserve_mem, "1-16i");
87MODULE_PARM_DESC(isapnp_reserve_mem, "ISA Plug & Play - reserve memory region(s) - address,size");
88MODULE_LICENSE("GPL");
89
90#define _PIDXR		0x279
91#define _PNPWRP		0xa79
92
93/* short tags */
94#define _STAG_PNPVERNO		0x01
95#define _STAG_LOGDEVID		0x02
96#define _STAG_COMPATDEVID	0x03
97#define _STAG_IRQ		0x04
98#define _STAG_DMA		0x05
99#define _STAG_STARTDEP		0x06
100#define _STAG_ENDDEP		0x07
101#define _STAG_IOPORT		0x08
102#define _STAG_FIXEDIO		0x09
103#define _STAG_VENDOR		0x0e
104#define _STAG_END		0x0f
105/* long tags */
106#define _LTAG_MEMRANGE		0x81
107#define _LTAG_ANSISTR		0x82
108#define _LTAG_UNICODESTR	0x83
109#define _LTAG_VENDOR		0x84
110#define _LTAG_MEM32RANGE	0x85
111#define _LTAG_FIXEDMEM32RANGE	0x86
112
113static unsigned char isapnp_checksum_value;
114static DECLARE_MUTEX(isapnp_cfg_mutex);
115static int isapnp_detected;
116
117/* some prototypes */
118
119static int isapnp_config_prepare(struct pci_dev *dev);
120static int isapnp_config_activate(struct pci_dev *dev);
121static int isapnp_config_deactivate(struct pci_dev *dev);
122
123static inline void write_data(unsigned char x)
124{
125	outb(x, _PNPWRP);
126}
127
128static inline void write_address(unsigned char x)
129{
130	outb(x, _PIDXR);
131	udelay(20);
132}
133
134static inline unsigned char read_data(void)
135{
136	unsigned char val = inb(isapnp_rdp);
137	return val;
138}
139
140unsigned char isapnp_read_byte(unsigned char idx)
141{
142	write_address(idx);
143	return read_data();
144}
145
146unsigned short isapnp_read_word(unsigned char idx)
147{
148	unsigned short val;
149
150	val = isapnp_read_byte(idx);
151	val = (val << 8) + isapnp_read_byte(idx+1);
152	return val;
153}
154
155unsigned int isapnp_read_dword(unsigned char idx)
156{
157	unsigned int val;
158
159	val = isapnp_read_byte(idx);
160	val = (val << 8) + isapnp_read_byte(idx+1);
161	val = (val << 8) + isapnp_read_byte(idx+2);
162	val = (val << 8) + isapnp_read_byte(idx+3);
163	return val;
164}
165
166void isapnp_write_byte(unsigned char idx, unsigned char val)
167{
168	write_address(idx);
169	write_data(val);
170}
171
172void isapnp_write_word(unsigned char idx, unsigned short val)
173{
174	isapnp_write_byte(idx, val >> 8);
175	isapnp_write_byte(idx+1, val);
176}
177
178void isapnp_write_dword(unsigned char idx, unsigned int val)
179{
180	isapnp_write_byte(idx, val >> 24);
181	isapnp_write_byte(idx+1, val >> 16);
182	isapnp_write_byte(idx+2, val >> 8);
183	isapnp_write_byte(idx+3, val);
184}
185
186void *isapnp_alloc(long size)
187{
188	void *result;
189
190	result = kmalloc(size, GFP_KERNEL);
191	if (!result)
192		return NULL;
193	memset(result, 0, size);
194	return result;
195}
196
197static void isapnp_key(void)
198{
199	unsigned char code = 0x6a, msb;
200	int i;
201
202	mdelay(1);
203	write_address(0x00);
204	write_address(0x00);
205
206	write_address(code);
207
208	for (i = 1; i < 32; i++) {
209		msb = ((code & 0x01) ^ ((code & 0x02) >> 1)) << 7;
210		code = (code >> 1) | msb;
211		write_address(code);
212	}
213}
214
215/* place all pnp cards in wait-for-key state */
216static void isapnp_wait(void)
217{
218	isapnp_write_byte(0x02, 0x02);
219}
220
221void isapnp_wake(unsigned char csn)
222{
223	isapnp_write_byte(0x03, csn);
224}
225
226void isapnp_device(unsigned char logdev)
227{
228	isapnp_write_byte(0x07, logdev);
229}
230
231void isapnp_activate(unsigned char logdev)
232{
233	isapnp_device(logdev);
234	isapnp_write_byte(ISAPNP_CFG_ACTIVATE, 1);
235	udelay(250);
236}
237
238void isapnp_deactivate(unsigned char logdev)
239{
240	isapnp_device(logdev);
241	isapnp_write_byte(ISAPNP_CFG_ACTIVATE, 0);
242	udelay(500);
243}
244
245static void __init isapnp_peek(unsigned char *data, int bytes)
246{
247	int i, j;
248	unsigned char d=0;
249
250	for (i = 1; i <= bytes; i++) {
251		for (j = 0; j < 20; j++) {
252			d = isapnp_read_byte(0x05);
253			if (d & 1)
254				break;
255			udelay(100);
256		}
257		if (!(d & 1)) {
258			if (data != NULL)
259				*data++ = 0xff;
260			continue;
261		}
262		d = isapnp_read_byte(0x04);	/* PRESDI */
263		isapnp_checksum_value += d;
264		if (data != NULL)
265			*data++ = d;
266	}
267}
268
269#define RDP_STEP	32	/* minimum is 4 */
270
271static int isapnp_next_rdp(void)
272{
273	int rdp = isapnp_rdp;
274	while (rdp <= 0x3ff) {
275		/*
276		 *	We cannot use NE2000 probe spaces for ISAPnP or we
277		 *	will lock up machines.
278		 */
279		if ((rdp < 0x280 || rdp >  0x380) && !check_region(rdp, 1))
280		{
281			isapnp_rdp = rdp;
282			return 0;
283		}
284		rdp += RDP_STEP;
285	}
286	return -1;
287}
288
289/* Set read port address */
290static inline void isapnp_set_rdp(void)
291{
292	isapnp_write_byte(0x00, isapnp_rdp >> 2);
293	udelay(100);
294}
295
296/*
297 *	Perform an isolation. The port selection code now tries to avoid
298 *	"dangerous to read" ports.
299 */
300
301static int __init isapnp_isolate_rdp_select(void)
302{
303	isapnp_wait();
304	isapnp_key();
305
306	/* Control: reset CSN and conditionally everything else too */
307	isapnp_write_byte(0x02, isapnp_reset ? 0x05 : 0x04);
308	mdelay(2);
309
310	isapnp_wait();
311	isapnp_key();
312	isapnp_wake(0x00);
313
314	if (isapnp_next_rdp() < 0) {
315		isapnp_wait();
316		return -1;
317	}
318
319	isapnp_set_rdp();
320	udelay(1000);
321	write_address(0x01);
322	udelay(1000);
323	return 0;
324}
325
326/*
327 *  Isolate (assign uniqued CSN) to all ISA PnP devices.
328 */
329
330static int __init isapnp_isolate(void)
331{
332	unsigned char checksum = 0x6a;
333	unsigned char chksum = 0x00;
334	unsigned char bit = 0x00;
335	int data;
336	int csn = 0;
337	int i;
338	int iteration = 1;
339
340	isapnp_rdp = 0x213;
341	if (isapnp_isolate_rdp_select() < 0)
342		return -1;
343
344	while (1) {
345		for (i = 1; i <= 64; i++) {
346			data = read_data() << 8;
347			udelay(250);
348			data = data | read_data();
349			udelay(250);
350			if (data == 0x55aa)
351				bit = 0x01;
352			checksum = ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7) | (checksum >> 1);
353			bit = 0x00;
354		}
355		for (i = 65; i <= 72; i++) {
356			data = read_data() << 8;
357			udelay(250);
358			data = data | read_data();
359			udelay(250);
360			if (data == 0x55aa)
361				chksum |= (1 << (i - 65));
362		}
363		if (checksum != 0x00 && checksum == chksum) {
364			csn++;
365
366			isapnp_write_byte(0x06, csn);
367			udelay(250);
368			iteration++;
369			isapnp_wake(0x00);
370			isapnp_set_rdp();
371			udelay(1000);
372			write_address(0x01);
373			udelay(1000);
374			goto __next;
375		}
376		if (iteration == 1) {
377			isapnp_rdp += RDP_STEP;
378			if (isapnp_isolate_rdp_select() < 0)
379				return -1;
380		} else if (iteration > 1) {
381			break;
382		}
383	      __next:
384		checksum = 0x6a;
385		chksum = 0x00;
386		bit = 0x00;
387	}
388	isapnp_wait();
389	return csn;
390}
391
392/*
393 *  Read one tag from stream.
394 */
395
396static int __init isapnp_read_tag(unsigned char *type, unsigned short *size)
397{
398	unsigned char tag, tmp[2];
399
400	isapnp_peek(&tag, 1);
401	if (tag == 0)				/* invalid tag */
402		return -1;
403	if (tag & 0x80) {	/* large item */
404		*type = tag;
405		isapnp_peek(tmp, 2);
406		*size = (tmp[1] << 8) | tmp[0];
407	} else {
408		*type = (tag >> 3) & 0x0f;
409		*size = tag & 0x07;
410	}
411	if (type == 0)				/* wrong type */
412		return -1;
413	if (*type == 0xff && *size == 0xffff)	/* probably invalid data */
414		return -1;
415	return 0;
416}
417
418/*
419 *  Skip specified number of bytes from stream.
420 */
421
422static void __init isapnp_skip_bytes(int count)
423{
424	isapnp_peek(NULL, count);
425}
426
427/*
428 *  Parse logical device tag.
429 */
430
431static struct pci_dev * __init isapnp_parse_device(struct pci_bus *card, int size, int number)
432{
433	unsigned char tmp[6];
434	struct pci_dev *dev;
435
436	isapnp_peek(tmp, size);
437	dev = isapnp_alloc(sizeof(struct pci_dev));
438	if (!dev)
439		return NULL;
440	dev->dma_mask = 0x00ffffff;
441	dev->devfn = number;
442	dev->vendor = (tmp[1] << 8) | tmp[0];
443	dev->device = (tmp[3] << 8) | tmp[2];
444	dev->regs = tmp[4];
445	dev->bus = card;
446	if (size > 5)
447		dev->regs |= tmp[5] << 8;
448	dev->prepare = isapnp_config_prepare;
449	dev->activate = isapnp_config_activate;
450	dev->deactivate = isapnp_config_deactivate;
451	return dev;
452}
453
454/*
455 *  Build new resources structure
456 */
457
458static struct isapnp_resources * __init isapnp_build_resources(struct pci_dev *dev, int dependent)
459{
460	struct isapnp_resources *res, *ptr, *ptra;
461
462	res = isapnp_alloc(sizeof(struct isapnp_resources));
463	if (!res)
464		return NULL;
465	res->dev = dev;
466	ptr = (struct isapnp_resources *)dev->sysdata;
467	while (ptr && ptr->next)
468		ptr = ptr->next;
469	if (ptr && ptr->dependent && dependent) { /* add to another list */
470		ptra = ptr->alt;
471		while (ptra && ptra->alt)
472			ptra = ptra->alt;
473		if (!ptra)
474			ptr->alt = res;
475		else
476			ptra->alt = res;
477	} else {
478		if (!ptr)
479			dev->sysdata = res;
480		else
481			ptr->next = res;
482	}
483	if (dependent) {
484		res->priority = dependent & 0xff;
485		if (res->priority > ISAPNP_RES_PRIORITY_FUNCTIONAL)
486			res->priority = ISAPNP_RES_PRIORITY_INVALID;
487		res->dependent = 1;
488	} else {
489		res->priority = ISAPNP_RES_PRIORITY_PREFERRED;
490		res->dependent = 0;
491	}
492	return res;
493}
494
495/*
496 *  Add IRQ resource to resources list.
497 */
498
499static void __init isapnp_add_irq_resource(struct pci_dev *dev,
500                                    	       struct isapnp_resources **res,
501                                               int dependent, int size)
502{
503	unsigned char tmp[3];
504	int i;
505	struct isapnp_irq *irq, *ptr;
506
507	isapnp_peek(tmp, size);
508	irq = isapnp_alloc(sizeof(struct isapnp_irq));
509	if (!irq)
510		return;
511	if (*res == NULL) {
512		*res = isapnp_build_resources(dev, dependent);
513		if (*res == NULL) {
514			kfree(irq);
515			return;
516		}
517	}
518	irq->map = (tmp[1] << 8) | tmp[0];
519	if (size > 2)
520		irq->flags = tmp[2];
521	else
522		irq->flags = IORESOURCE_IRQ_HIGHEDGE;
523	irq->res = *res;
524	ptr = (*res)->irq;
525	while (ptr && ptr->next)
526		ptr = ptr->next;
527	if (ptr)
528		ptr->next = irq;
529	else
530		(*res)->irq = irq;
531#ifdef CONFIG_PCI
532	for (i=0; i<16; i++)
533		if (irq->map & (1<<i))
534			pcibios_penalize_isa_irq(i);
535#endif
536}
537
538/*
539 *  Add DMA resource to resources list.
540 */
541
542static void __init isapnp_add_dma_resource(struct pci_dev *dev,
543                                    	       struct isapnp_resources **res,
544                                    	       int dependent, int size)
545{
546	unsigned char tmp[2];
547	struct isapnp_dma *dma, *ptr;
548
549	isapnp_peek(tmp, size);
550	dma = isapnp_alloc(sizeof(struct isapnp_dma));
551	if (!dma)
552		return;
553	if (*res == NULL) {
554		*res = isapnp_build_resources(dev, dependent);
555		if (*res == NULL) {
556			kfree(dma);
557			return;
558		}
559	}
560	dma->map = tmp[0];
561	dma->flags = tmp[1];
562	dma->res = *res;
563	ptr = (*res)->dma;
564	while (ptr && ptr->next)
565		ptr = ptr->next;
566	if (ptr)
567		ptr->next = dma;
568	else
569		(*res)->dma = dma;
570}
571
572/*
573 *  Add port resource to resources list.
574 */
575
576static void __init isapnp_add_port_resource(struct pci_dev *dev,
577						struct isapnp_resources **res,
578						int dependent, int size)
579{
580	unsigned char tmp[7];
581	struct isapnp_port *port, *ptr;
582
583	isapnp_peek(tmp, size);
584	port = isapnp_alloc(sizeof(struct isapnp_port));
585	if (!port)
586		return;
587	if (*res == NULL) {
588		*res = isapnp_build_resources(dev, dependent);
589		if (*res == NULL) {
590			kfree(port);
591			return;
592		}
593	}
594	port->min = (tmp[2] << 8) | tmp[1];
595	port->max = (tmp[4] << 8) | tmp[3];
596	port->align = tmp[5];
597	port->size = tmp[6];
598	port->flags = tmp[0] ? ISAPNP_PORT_FLAG_16BITADDR : 0;
599	port->res = *res;
600	ptr = (*res)->port;
601	while (ptr && ptr->next)
602		ptr = ptr->next;
603	if (ptr)
604		ptr->next = port;
605	else
606		(*res)->port = port;
607}
608
609/*
610 *  Add fixed port resource to resources list.
611 */
612
613static void __init isapnp_add_fixed_port_resource(struct pci_dev *dev,
614						      struct isapnp_resources **res,
615						      int dependent, int size)
616{
617	unsigned char tmp[3];
618	struct isapnp_port *port, *ptr;
619
620	isapnp_peek(tmp, size);
621	port = isapnp_alloc(sizeof(struct isapnp_port));
622	if (!port)
623		return;
624	if (*res == NULL) {
625		*res = isapnp_build_resources(dev, dependent);
626		if (*res == NULL) {
627			kfree(port);
628			return;
629		}
630	}
631	port->min = port->max = (tmp[1] << 8) | tmp[0];
632	port->size = tmp[2];
633	port->align = 0;
634	port->flags = ISAPNP_PORT_FLAG_FIXED;
635	port->res = *res;
636	ptr = (*res)->port;
637	while (ptr && ptr->next)
638		ptr = ptr->next;
639	if (ptr)
640		ptr->next = port;
641	else
642		(*res)->port = port;
643}
644
645/*
646 *  Add memory resource to resources list.
647 */
648
649static void __init isapnp_add_mem_resource(struct pci_dev *dev,
650					       struct isapnp_resources **res,
651					       int dependent, int size)
652{
653	unsigned char tmp[9];
654	struct isapnp_mem *mem, *ptr;
655
656	isapnp_peek(tmp, size);
657	mem = isapnp_alloc(sizeof(struct isapnp_mem));
658	if (!mem)
659		return;
660	if (*res == NULL) {
661		*res = isapnp_build_resources(dev, dependent);
662		if (*res == NULL) {
663			kfree(mem);
664			return;
665		}
666	}
667	mem->min = ((tmp[2] << 8) | tmp[1]) << 8;
668	mem->max = ((tmp[4] << 8) | tmp[3]) << 8;
669	mem->align = (tmp[6] << 8) | tmp[5];
670	mem->size = ((tmp[8] << 8) | tmp[7]) << 8;
671	mem->flags = tmp[0];
672	mem->res = *res;
673	ptr = (*res)->mem;
674	while (ptr && ptr->next)
675		ptr = ptr->next;
676	if (ptr)
677		ptr->next = mem;
678	else
679		(*res)->mem = mem;
680}
681
682/*
683 *  Add 32-bit memory resource to resources list.
684 */
685
686static void __init isapnp_add_mem32_resource(struct pci_dev *dev,
687						 struct isapnp_resources **res,
688						 int dependent, int size)
689{
690	unsigned char tmp[17];
691	struct isapnp_mem32 *mem32, *ptr;
692
693	isapnp_peek(tmp, size);
694	mem32 = isapnp_alloc(sizeof(struct isapnp_mem32));
695	if (!mem32)
696		return;
697	if (*res == NULL) {
698		*res = isapnp_build_resources(dev, dependent);
699		if (*res == NULL) {
700			kfree(mem32);
701			return;
702		}
703	}
704	memcpy(mem32->data, tmp, 17);
705	mem32->res = *res;
706	ptr = (*res)->mem32;
707	while (ptr && ptr->next)
708		ptr = ptr->next;
709	if (ptr)
710		ptr->next = mem32;
711	else
712		(*res)->mem32 = mem32;
713}
714
715/*
716 *  Add 32-bit fixed memory resource to resources list.
717 */
718
719static void __init isapnp_add_fixed_mem32_resource(struct pci_dev *dev,
720						       struct isapnp_resources **res,
721						       int dependent, int size)
722{
723	unsigned char tmp[17];
724	struct isapnp_mem32 *mem32, *ptr;
725
726	isapnp_peek(tmp, size);
727	mem32 = isapnp_alloc(sizeof(struct isapnp_mem32));
728	if (!mem32)
729		return;
730	if (*res == NULL) {
731		*res = isapnp_build_resources(dev, dependent);
732		if (*res == NULL) {
733			kfree(mem32);
734			return;
735		}
736	}
737	memcpy(mem32->data, tmp, 17);
738	mem32->res = *res;
739	ptr = (*res)->mem32;
740	while (ptr && ptr->next)
741		ptr = ptr->next;
742	if (ptr)
743		ptr->next = mem32;
744	else
745		(*res)->mem32 = mem32;
746}
747
748/*
749 *  Parse card name for ISA PnP device.
750 */
751
752static void __init
753isapnp_parse_name(char *name, unsigned int name_max, unsigned short *size)
754{
755	if (name[0] == '\0') {
756		unsigned short size1 = *size >= name_max ? (name_max - 1) : *size;
757		isapnp_peek(name, size1);
758		name[size1] = '\0';
759		*size -= size1;
760
761		/* clean whitespace from end of string */
762		while (size1 > 0  &&  name[--size1] == ' ')
763			name[size1] = '\0';
764	}
765}
766
767/*
768 *  Parse resource map for logical device.
769 */
770
771static int __init isapnp_create_device(struct pci_bus *card,
772					   unsigned short size)
773{
774	int number = 0, skip = 0, dependent = 0, compat = 0;
775	unsigned char type, tmp[17];
776	struct pci_dev *dev;
777	struct isapnp_resources *res = NULL;
778
779	if ((dev = isapnp_parse_device(card, size, number++)) == NULL)
780		return 1;
781	list_add(&dev->bus_list, &card->devices);
782	list_add_tail(&dev->global_list, &isapnp_devices);
783	while (1) {
784		if (isapnp_read_tag(&type, &size)<0)
785			return 1;
786		if (skip && type != _STAG_LOGDEVID && type != _STAG_END)
787			goto __skip;
788		switch (type) {
789		case _STAG_LOGDEVID:
790			if (size >= 5 && size <= 6) {
791				isapnp_config_prepare(dev);
792				if ((dev = isapnp_parse_device(card, size, number++)) == NULL)
793					return 1;
794				list_add_tail(&dev->bus_list, &card->devices);
795				list_add_tail(&dev->global_list, &isapnp_devices);
796				size = 0;
797				skip = 0;
798			} else {
799				skip = 1;
800			}
801			res = NULL;
802			dependent = 0;
803			compat = 0;
804			break;
805		case _STAG_COMPATDEVID:
806			if (size == 4 && compat < DEVICE_COUNT_COMPATIBLE) {
807				isapnp_peek(tmp, 4);
808				dev->vendor_compatible[compat] = (tmp[1] << 8) | tmp[0];
809				dev->device_compatible[compat] = (tmp[3] << 8) | tmp[2];
810				compat++;
811				size = 0;
812			}
813			break;
814		case _STAG_IRQ:
815			if (size < 2 || size > 3)
816				goto __skip;
817			isapnp_add_irq_resource(dev, &res, dependent, size);
818			size = 0;
819			break;
820		case _STAG_DMA:
821			if (size != 2)
822				goto __skip;
823			isapnp_add_dma_resource(dev, &res, dependent, size);
824			size = 0;
825			break;
826		case _STAG_STARTDEP:
827			if (size > 1)
828				goto __skip;
829			res = NULL;
830			dependent = 0x100 | ISAPNP_RES_PRIORITY_ACCEPTABLE;
831			if (size > 0) {
832				isapnp_peek(tmp, size);
833				dependent = 0x100 | tmp[0];
834				size = 0;
835			}
836			break;
837		case _STAG_ENDDEP:
838			if (size != 0)
839				goto __skip;
840			res = NULL;
841			dependent = 0;
842			break;
843		case _STAG_IOPORT:
844			if (size != 7)
845				goto __skip;
846			isapnp_add_port_resource(dev, &res, dependent, size);
847			size = 0;
848			break;
849		case _STAG_FIXEDIO:
850			if (size != 3)
851				goto __skip;
852			isapnp_add_fixed_port_resource(dev, &res, dependent, size);
853			size = 0;
854			break;
855		case _STAG_VENDOR:
856			break;
857		case _LTAG_MEMRANGE:
858			if (size != 9)
859				goto __skip;
860			isapnp_add_mem_resource(dev, &res, dependent, size);
861			size = 0;
862			break;
863		case _LTAG_ANSISTR:
864			isapnp_parse_name(dev->name, sizeof(dev->name), &size);
865			break;
866		case _LTAG_UNICODESTR:
867			/* silently ignore */
868			/* who use unicode for hardware identification? */
869			break;
870		case _LTAG_VENDOR:
871			break;
872		case _LTAG_MEM32RANGE:
873			if (size != 17)
874				goto __skip;
875			isapnp_add_mem32_resource(dev, &res, dependent, size);
876			size = 0;
877			break;
878		case _LTAG_FIXEDMEM32RANGE:
879			if (size != 17)
880				goto __skip;
881			isapnp_add_fixed_mem32_resource(dev, &res, dependent, size);
882			size = 0;
883			break;
884		case _STAG_END:
885			if (size > 0)
886				isapnp_skip_bytes(size);
887			isapnp_config_prepare(dev);
888			return 1;
889		default:
890			printk(KERN_ERR "isapnp: unexpected or unknown tag type 0x%x for logical device %i (device %i), ignored\n", type, dev->devfn, card->number);
891		}
892	      __skip:
893	      	if (size > 0)
894		      	isapnp_skip_bytes(size);
895	}
896	isapnp_config_prepare(dev);
897	return 0;
898}
899
900/*
901 *  Parse resource map for ISA PnP card.
902 */
903
904static void __init isapnp_parse_resource_map(struct pci_bus *card)
905{
906	unsigned char type, tmp[17];
907	unsigned short size;
908
909	while (1) {
910		if (isapnp_read_tag(&type, &size)<0)
911			return;
912		switch (type) {
913		case _STAG_PNPVERNO:
914			if (size != 2)
915				goto __skip;
916			isapnp_peek(tmp, 2);
917			card->pnpver = tmp[0];
918			card->productver = tmp[1];
919			size = 0;
920			break;
921		case _STAG_LOGDEVID:
922			if (size >= 5 && size <= 6) {
923				if (isapnp_create_device(card, size)==1)
924					return;
925				size = 0;
926			}
927			break;
928		case _STAG_VENDOR:
929			break;
930		case _LTAG_ANSISTR:
931			isapnp_parse_name(card->name, sizeof(card->name), &size);
932			break;
933		case _LTAG_UNICODESTR:
934			/* silently ignore */
935			/* who use unicode for hardware identification? */
936			break;
937		case _LTAG_VENDOR:
938			break;
939		case _STAG_END:
940			if (size > 0)
941				isapnp_skip_bytes(size);
942			return;
943		default:
944			printk(KERN_ERR "isapnp: unexpected or unknown tag type 0x%x for device %i, ignored\n", type, card->number);
945		}
946	      __skip:
947	      	if (size > 0)
948		      	isapnp_skip_bytes(size);
949	}
950}
951
952/*
953 *  Compute ISA PnP checksum for first eight bytes.
954 */
955
956static unsigned char __init isapnp_checksum(unsigned char *data)
957{
958	int i, j;
959	unsigned char checksum = 0x6a, bit, b;
960
961	for (i = 0; i < 8; i++) {
962		b = data[i];
963		for (j = 0; j < 8; j++) {
964			bit = 0;
965			if (b & (1 << j))
966				bit = 1;
967			checksum = ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7) | (checksum >> 1);
968		}
969	}
970	return checksum;
971}
972
973/*
974 *  Build device list for all present ISA PnP devices.
975 */
976
977static int __init isapnp_build_device_list(void)
978{
979	int csn;
980	unsigned char header[9], checksum;
981	struct pci_bus *card;
982	struct pci_dev *dev;
983
984	isapnp_wait();
985	isapnp_key();
986	for (csn = 1; csn <= 10; csn++) {
987		isapnp_wake(csn);
988		isapnp_peek(header, 9);
989		checksum = isapnp_checksum(header);
990		/* Don't be strict on the checksum, here !
991                   e.g. 'SCM SwapBox Plug and Play' has header[8]==0 (should be: b7)*/
992		if (header[8] == 0)
993			;
994		else if (checksum == 0x00 || checksum != header[8])	/* not valid CSN */
995			continue;
996		if ((card = isapnp_alloc(sizeof(struct pci_bus))) == NULL)
997			continue;
998
999		card->number = csn;
1000		card->vendor = (header[1] << 8) | header[0];
1001		card->device = (header[3] << 8) | header[2];
1002		card->serial = (header[7] << 24) | (header[6] << 16) | (header[5] << 8) | header[4];
1003		isapnp_checksum_value = 0x00;
1004		INIT_LIST_HEAD(&card->children);
1005		INIT_LIST_HEAD(&card->devices);
1006		isapnp_parse_resource_map(card);
1007		if (isapnp_checksum_value != 0x00)
1008			printk(KERN_ERR "isapnp: checksum for device %i is not valid (0x%x)\n", csn, isapnp_checksum_value);
1009		card->checksum = isapnp_checksum_value;
1010
1011		list_add_tail(&card->node, &isapnp_cards);
1012	}
1013	isapnp_for_each_dev(dev) {
1014		isapnp_fixup_device(dev);
1015	}
1016	return 0;
1017}
1018
1019/*
1020 *  Basic configuration routines.
1021 */
1022
1023int isapnp_present(void)
1024{
1025	return !list_empty(&isapnp_devices);
1026}
1027
1028int isapnp_cfg_begin(int csn, int logdev)
1029{
1030	if (csn < 1 || csn > 10 || logdev > 10)
1031		return -EINVAL;
1032	MOD_INC_USE_COUNT;
1033	down(&isapnp_cfg_mutex);
1034	isapnp_wait();
1035	isapnp_key();
1036	isapnp_wake(csn);
1037	isapnp_set_rdp();
1038	udelay(1000);	/* delay 1000us */
1039	write_address(0x01);
1040	udelay(1000);	/* delay 1000us */
1041	if (logdev >= 0)
1042		isapnp_device(logdev);
1043	return 0;
1044}
1045
1046int isapnp_cfg_end(void)
1047{
1048	isapnp_wait();
1049	up(&isapnp_cfg_mutex);
1050	MOD_DEC_USE_COUNT;
1051	return 0;
1052}
1053
1054/*
1055 *  Resource manager.
1056 */
1057
1058static struct isapnp_port *isapnp_find_port(struct pci_dev *dev, int index)
1059{
1060	struct isapnp_resources *res;
1061	struct isapnp_port *port;
1062
1063	if (!dev || index < 0 || index > 7)
1064		return NULL;
1065	for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1066		for (port = res->port; port; port = port->next) {
1067			if (!index)
1068				return port;
1069			index--;
1070		}
1071	}
1072	return NULL;
1073}
1074
1075struct isapnp_irq *isapnp_find_irq(struct pci_dev *dev, int index)
1076{
1077	struct isapnp_resources *res, *resa;
1078	struct isapnp_irq *irq;
1079	int index1, index2, index3;
1080
1081	if (!dev || index < 0 || index > 7)
1082		return NULL;
1083	for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1084		index3 = 0;
1085		for (resa = res; resa; resa = resa->alt) {
1086			index1 = index;
1087			index2 = 0;
1088			for (irq = resa->irq; irq; irq = irq->next) {
1089				if (!index1)
1090					return irq;
1091				index1--;
1092				index2++;
1093			}
1094			if (index3 < index2)
1095				index3 = index2;
1096		}
1097		index -= index3;
1098	}
1099	return NULL;
1100}
1101
1102struct isapnp_dma *isapnp_find_dma(struct pci_dev *dev, int index)
1103{
1104	struct isapnp_resources *res;
1105	struct isapnp_dma *dma;
1106
1107	if (!dev || index < 0 || index > 7)
1108		return NULL;
1109	for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1110		for (dma = res->dma; dma; dma = dma->next) {
1111			if (!index)
1112				return dma;
1113			index--;
1114		}
1115	}
1116	return NULL;
1117}
1118
1119struct isapnp_mem *isapnp_find_mem(struct pci_dev *dev, int index)
1120{
1121	struct isapnp_resources *res;
1122	struct isapnp_mem *mem;
1123
1124	if (!dev || index < 0 || index > 7)
1125		return NULL;
1126	for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1127		for (mem = res->mem; mem; mem = mem->next) {
1128			if (!index)
1129				return mem;
1130			index--;
1131		}
1132	}
1133	return NULL;
1134}
1135
1136struct isapnp_mem32 *isapnp_find_mem32(struct pci_dev *dev, int index)
1137{
1138	struct isapnp_resources *res;
1139	struct isapnp_mem32 *mem32;
1140
1141	if (!dev || index < 0 || index > 7)
1142		return NULL;
1143	for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1144		for (mem32 = res->mem32; mem32; mem32 = mem32->next) {
1145			if (!index)
1146				return mem32;
1147			index--;
1148		}
1149	}
1150	return NULL;
1151}
1152
1153/*
1154 *  Device manager.
1155 */
1156
1157struct pci_bus *isapnp_find_card(unsigned short vendor,
1158				 unsigned short device,
1159				 struct pci_bus *from)
1160{
1161	struct list_head *list;
1162
1163	list = isapnp_cards.next;
1164	if (from)
1165		list = from->node.next;
1166
1167	while (list != &isapnp_cards) {
1168		struct pci_bus *card = pci_bus_b(list);
1169		if (card->vendor == vendor && card->device == device)
1170			return card;
1171		list = list->next;
1172	}
1173	return NULL;
1174}
1175
1176struct pci_dev *isapnp_find_dev(struct pci_bus *card,
1177				unsigned short vendor,
1178				unsigned short function,
1179				struct pci_dev *from)
1180{
1181	if (card == NULL) {	/* look for a logical device from all cards */
1182		struct list_head *list;
1183
1184		list = isapnp_devices.next;
1185		if (from)
1186			list = from->global_list.next;
1187
1188		while (list != &isapnp_devices) {
1189			int idx;
1190			struct pci_dev *dev = pci_dev_g(list);
1191
1192			if (dev->vendor == vendor && dev->device == function)
1193				return dev;
1194			for (idx = 0; idx < DEVICE_COUNT_COMPATIBLE; idx++)
1195				if (dev->vendor_compatible[idx] == vendor &&
1196				    dev->device_compatible[idx] == function)
1197					return dev;
1198			list = list->next;
1199		}
1200	} else {
1201		struct list_head *list;
1202
1203		list = card->devices.next;
1204		if (from) {
1205			list = from->bus_list.next;
1206			if (from->bus != card)	/* something is wrong */
1207				return NULL;
1208		}
1209		while (list != &card->devices) {
1210			int idx;
1211			struct pci_dev *dev = pci_dev_b(list);
1212
1213			if (dev->vendor == vendor && dev->device == function)
1214				return dev;
1215			for (idx = 0; idx < DEVICE_COUNT_COMPATIBLE; idx++)
1216				if (dev->vendor_compatible[idx] == vendor &&
1217				    dev->device_compatible[idx] == function)
1218					return dev;
1219			list = list->next;
1220		}
1221	}
1222	return NULL;
1223}
1224
1225static const struct isapnp_card_id *
1226isapnp_match_card(const struct isapnp_card_id *ids, struct pci_bus *card)
1227{
1228	int idx;
1229
1230	while (ids->card_vendor || ids->card_device) {
1231		if ((ids->card_vendor == ISAPNP_ANY_ID || ids->card_vendor == card->vendor) &&
1232		    (ids->card_device == ISAPNP_ANY_ID || ids->card_device == card->device)) {
1233			for (idx = 0; idx < ISAPNP_CARD_DEVS; idx++) {
1234				if (ids->devs[idx].vendor == 0 &&
1235				    ids->devs[idx].function == 0)
1236					return ids;
1237				if (isapnp_find_dev(card,
1238						    ids->devs[idx].vendor,
1239						    ids->devs[idx].function,
1240						    NULL) == NULL)
1241					goto __next;
1242			}
1243			return ids;
1244		}
1245	      __next:
1246		ids++;
1247	}
1248	return NULL;
1249}
1250
1251int isapnp_probe_cards(const struct isapnp_card_id *ids,
1252		       int (*probe)(struct pci_bus *_card,
1253		       		    const struct isapnp_card_id *_id))
1254{
1255	struct pci_bus *card;
1256	const struct isapnp_card_id *id;
1257	int count = 0;
1258
1259	if (ids == NULL || probe == NULL)
1260		return -EINVAL;
1261	isapnp_for_each_card(card) {
1262		id = isapnp_match_card(ids, card);
1263		if (id != NULL && probe(card, id) >= 0)
1264			count++;
1265	}
1266	return count;
1267}
1268
1269static const struct isapnp_device_id *
1270isapnp_match_dev(const struct isapnp_device_id *ids, struct pci_dev *dev)
1271{
1272	while (ids->card_vendor || ids->card_device) {
1273		if ((ids->card_vendor == ISAPNP_ANY_ID || ids->card_vendor == dev->bus->vendor) &&
1274		    (ids->card_device == ISAPNP_ANY_ID || ids->card_device == dev->bus->device) &&
1275                    (ids->vendor == ISAPNP_ANY_ID || ids->vendor == dev->vendor) &&
1276                    (ids->function == ISAPNP_ANY_ID || ids->function == dev->device))
1277			return ids;
1278		ids++;
1279	}
1280	return NULL;
1281}
1282
1283int isapnp_probe_devs(const struct isapnp_device_id *ids,
1284		      int (*probe)(struct pci_dev *dev,
1285		                   const struct isapnp_device_id *id))
1286{
1287
1288	struct pci_dev *dev;
1289	const struct isapnp_device_id *id;
1290	int count = 0;
1291
1292	if (ids == NULL || probe == NULL)
1293		return -EINVAL;
1294	isapnp_for_each_dev(dev) {
1295		id = isapnp_match_dev(ids, dev);
1296		if (id != NULL && probe(dev, id) >= 0)
1297			count++;
1298	}
1299	return count;
1300}
1301
1302int isapnp_activate_dev(struct pci_dev *dev, const char *name)
1303{
1304	int err;
1305
1306	/* Device already active? Let's use it and inform the caller */
1307	if (dev->active)
1308		return -EBUSY;
1309
1310	if ((err = dev->activate(dev)) < 0) {
1311		printk(KERN_ERR "isapnp: config of %s failed (out of resources?)[%d]\n", name, err);
1312		dev->deactivate(dev);
1313		return err;
1314	}
1315
1316	return 0;
1317}
1318
1319static unsigned int isapnp_dma_resource_flags(struct isapnp_dma *dma)
1320{
1321	return dma->flags | IORESOURCE_DMA | IORESOURCE_AUTO;
1322}
1323
1324static unsigned int isapnp_mem_resource_flags(struct isapnp_mem *mem)
1325{
1326	unsigned int result;
1327
1328	result = mem->flags | IORESOURCE_MEM | IORESOURCE_AUTO;
1329	if (!(mem->flags & IORESOURCE_MEM_WRITEABLE))
1330		result |= IORESOURCE_READONLY;
1331	if (mem->flags & IORESOURCE_MEM_CACHEABLE)
1332		result |= IORESOURCE_CACHEABLE;
1333	if (mem->flags & IORESOURCE_MEM_RANGELENGTH)
1334		result |= IORESOURCE_RANGELENGTH;
1335	if (mem->flags & IORESOURCE_MEM_SHADOWABLE)
1336		result |= IORESOURCE_SHADOWABLE;
1337	return result;
1338}
1339
1340static unsigned int isapnp_irq_resource_flags(struct isapnp_irq *irq)
1341{
1342	return irq->flags | IORESOURCE_IRQ | IORESOURCE_AUTO;
1343}
1344
1345static unsigned int isapnp_port_resource_flags(struct isapnp_port *port)
1346{
1347	return port->flags | IORESOURCE_IO | IORESOURCE_AUTO;
1348}
1349
1350static int isapnp_config_prepare(struct pci_dev *dev)
1351{
1352	struct isapnp_resources *res, *resa;
1353	struct isapnp_port *port;
1354	struct isapnp_irq *irq;
1355	struct isapnp_dma *dma;
1356	struct isapnp_mem *mem;
1357	int port_count, port_count1;
1358	int irq_count, irq_count1;
1359	int dma_count, dma_count1;
1360	int mem_count, mem_count1;
1361	int idx;
1362
1363	if (dev == NULL)
1364		return -EINVAL;
1365	if (dev->active || dev->ro)
1366		return -EBUSY;
1367	for (idx = 0; idx < DEVICE_COUNT_IRQ; idx++) {
1368		dev->irq_resource[idx].name = NULL;
1369		dev->irq_resource[idx].start = 0;
1370		dev->irq_resource[idx].end = 0;
1371		dev->irq_resource[idx].flags = 0;
1372	}
1373	for (idx = 0; idx < DEVICE_COUNT_DMA; idx++) {
1374		dev->dma_resource[idx].name = NULL;
1375		dev->dma_resource[idx].start = 0;
1376		dev->dma_resource[idx].end = 0;
1377		dev->dma_resource[idx].flags = 0;
1378	}
1379	for (idx = 0; idx < DEVICE_COUNT_RESOURCE; idx++) {
1380		dev->resource[idx].name = NULL;
1381		dev->resource[idx].start = 0;
1382		dev->resource[idx].end = 0;
1383		dev->resource[idx].flags = 0;
1384	}
1385	port_count = irq_count = dma_count = mem_count = 0;
1386	for (res = (struct isapnp_resources *)dev->sysdata; res; res = res->next) {
1387		port_count1 = irq_count1 = dma_count1 = mem_count1 = 0;
1388		for (resa = res; resa; resa = resa->alt) {
1389			for (port = resa->port, idx = 0; port; port = port->next, idx++) {
1390				if (dev->resource[port_count + idx].flags == 0) {
1391					dev->resource[port_count + idx].flags = isapnp_port_resource_flags(port);
1392					dev->resource[port_count + idx].end = port->size;
1393				}
1394			}
1395			if (port_count1 < idx)
1396				port_count1 = idx;
1397			for (irq = resa->irq, idx = 0; irq; irq = irq->next, idx++) {
1398				int count = irq_count + idx;
1399				if (count < DEVICE_COUNT_IRQ) {
1400					if (dev->irq_resource[count].flags == 0) {
1401						dev->irq_resource[count].flags = isapnp_irq_resource_flags(irq);
1402					}
1403				}
1404
1405			}
1406			if (irq_count1 < idx)
1407				irq_count1 = idx;
1408			for (dma = resa->dma, idx = 0; dma; dma = dma->next, idx++)
1409				if (dev->dma_resource[idx].flags == 0) {
1410					dev->dma_resource[idx].flags = isapnp_dma_resource_flags(dma);
1411				}
1412			if (dma_count1 < idx)
1413				dma_count1 = idx;
1414			for (mem = resa->mem, idx = 0; mem; mem = mem->next, idx++)
1415				if (dev->resource[mem_count + idx + 8].flags == 0) {
1416					dev->resource[mem_count + idx + 8].flags = isapnp_mem_resource_flags(mem);
1417				}
1418			if (mem_count1 < idx)
1419				mem_count1 = idx;
1420		}
1421		port_count += port_count1;
1422		irq_count += irq_count1;
1423		dma_count += dma_count1;
1424		mem_count += mem_count1;
1425	}
1426	return 0;
1427}
1428
1429struct isapnp_cfgtmp {
1430	struct isapnp_port *port[8];
1431	struct isapnp_irq *irq[2];
1432	struct isapnp_dma *dma[2];
1433	struct isapnp_mem *mem[4];
1434	struct pci_dev *request;
1435	struct pci_dev result;
1436};
1437
1438static int isapnp_alternative_switch(struct isapnp_cfgtmp *cfg,
1439				     struct isapnp_resources *from,
1440				     struct isapnp_resources *to)
1441{
1442	int tmp, tmp1;
1443	struct isapnp_port *port;
1444	struct isapnp_irq *irq;
1445	struct isapnp_dma *dma;
1446	struct isapnp_mem *mem;
1447
1448	if (!cfg)
1449		return -EINVAL;
1450	/* process port settings */
1451	for (tmp = 0; tmp < 8; tmp++) {
1452		if (!(cfg->request->resource[tmp].flags & IORESOURCE_AUTO))
1453			continue;		/* don't touch */
1454		port = cfg->port[tmp];
1455		if (!port) {
1456			cfg->port[tmp] = port = isapnp_find_port(cfg->request, tmp);
1457			if (!port)
1458				return -EINVAL;
1459		}
1460		if (from && port->res == from) {
1461			while (port->res != to) {
1462				if (!port->res->alt)
1463					return -EINVAL;
1464				port = port->res->alt->port;
1465				for (tmp1 = tmp; tmp1 > 0 && port; tmp1--)
1466					port = port->next;
1467				cfg->port[tmp] = port;
1468				if (!port)
1469					return -ENOENT;
1470				cfg->result.resource[tmp].flags = isapnp_port_resource_flags(port);
1471			}
1472		}
1473	}
1474	/* process irq settings */
1475	for (tmp = 0; tmp < 2; tmp++) {
1476		if (!(cfg->request->irq_resource[tmp].flags & IORESOURCE_AUTO))
1477			continue;		/* don't touch */
1478		irq = cfg->irq[tmp];
1479		if (!irq) {
1480			cfg->irq[tmp] = irq = isapnp_find_irq(cfg->request, tmp);
1481			if (!irq)
1482				return -EINVAL;
1483		}
1484		if (from && irq->res == from) {
1485			while (irq->res != to) {
1486				if (!irq->res->alt)
1487					return -EINVAL;
1488				irq = irq->res->alt->irq;
1489				for (tmp1 = tmp; tmp1 > 0 && irq; tmp1--)
1490					irq = irq->next;
1491				cfg->irq[tmp] = irq;
1492				if (!irq)
1493					return -ENOENT;
1494				cfg->result.irq_resource[tmp].flags = isapnp_irq_resource_flags(irq);
1495			}
1496		}
1497	}
1498	/* process dma settings */
1499	for (tmp = 0; tmp < 2; tmp++) {
1500		if (!(cfg->request->dma_resource[tmp].flags & IORESOURCE_AUTO))
1501			continue;		/* don't touch */
1502		dma = cfg->dma[tmp];
1503		if (!dma) {
1504			cfg->dma[tmp] = dma = isapnp_find_dma(cfg->request, tmp);
1505			if (!dma)
1506				return -EINVAL;
1507		}
1508		if (from && dma->res == from) {
1509			while (dma->res != to) {
1510				if (!dma->res->alt)
1511					return -EINVAL;
1512				dma = dma->res->alt->dma;
1513				for (tmp1 = tmp; tmp1 > 0 && dma; tmp1--)
1514					dma = dma->next;
1515				cfg->dma[tmp] = dma;
1516				if (!dma)
1517					return -ENOENT;
1518				cfg->result.dma_resource[tmp].flags = isapnp_dma_resource_flags(dma);
1519			}
1520		}
1521	}
1522	/* process memory settings */
1523	for (tmp = 0; tmp < 4; tmp++) {
1524		if (!(cfg->request->resource[tmp + 8].flags & IORESOURCE_AUTO))
1525			continue;		/* don't touch */
1526		mem = cfg->mem[tmp];
1527		if (!mem) {
1528			cfg->mem[tmp] = mem = isapnp_find_mem(cfg->request, tmp);
1529			if (!mem)
1530				return -EINVAL;
1531		}
1532		if (from && mem->res == from) {
1533			while (mem->res != to) {
1534				if (!mem->res->alt)
1535					return -EINVAL;
1536				mem = mem->res->alt->mem;
1537				for (tmp1 = tmp; tmp1 > 0 && mem; tmp1--)
1538					mem = mem->next;
1539				cfg->mem[tmp] = mem;
1540				if (!mem)
1541					return -ENOENT;
1542				cfg->result.resource[tmp + 8].flags = isapnp_mem_resource_flags(mem);
1543			}
1544		}
1545	}
1546	return 0;
1547}
1548
1549static int isapnp_check_port(struct isapnp_cfgtmp *cfg, int port, int size, int idx)
1550{
1551	int i, tmp, rport, rsize;
1552	struct isapnp_port *xport;
1553	struct pci_dev *dev;
1554
1555	if (check_region(port, size))
1556		return 1;
1557	for (i = 0; i < 8; i++) {
1558		rport = isapnp_reserve_io[i << 1];
1559		rsize = isapnp_reserve_io[(i << 1) + 1];
1560		if (port >= rport && port < rport + rsize)
1561			return 1;
1562		if (port + size > rport && port + size < (rport + rsize) - 1)
1563			return 1;
1564	}
1565
1566	isapnp_for_each_dev(dev) {
1567		if (dev->active) {
1568			for (tmp = 0; tmp < 8; tmp++) {
1569				if (dev->resource[tmp].flags) {
1570					rport = dev->resource[tmp].start;
1571					rsize = (dev->resource[tmp].end - rport) + 1;
1572					if (port >= rport && port < rport + rsize)
1573						return 1;
1574					if (port + size > rport && port + size < (rport + rsize) - 1)
1575						return 1;
1576				}
1577			}
1578		}
1579	}
1580	for (i = 0; i < 8; i++) {
1581		unsigned int flags;
1582		if (i == idx)
1583			continue;
1584		flags = cfg->request->resource[i].flags;
1585		if (!flags)
1586			continue;
1587		tmp = cfg->request->resource[i].start;
1588		if (flags & IORESOURCE_AUTO) {		/* auto */
1589			xport = cfg->port[i];
1590			if (!xport)
1591				return 1;
1592			if (cfg->result.resource[i].flags & IORESOURCE_AUTO)
1593				continue;
1594			tmp = cfg->result.resource[i].start;
1595			if (tmp + xport->size >= port && tmp <= port + xport->size)
1596				return 1;
1597			continue;
1598		}
1599		if (port == tmp)
1600			return 1;
1601		xport = isapnp_find_port(cfg->request, i);
1602		if (!xport)
1603			return 1;
1604		if (tmp + xport->size >= port && tmp <= port + xport->size)
1605			return 1;
1606	}
1607	return 0;
1608}
1609
1610static int isapnp_valid_port(struct isapnp_cfgtmp *cfg, int idx)
1611{
1612	int err;
1613	unsigned long *value1, *value2;
1614	struct isapnp_port *port;
1615
1616	if (!cfg || idx < 0 || idx > 7)
1617		return -EINVAL;
1618	if (!(cfg->result.resource[idx].flags & IORESOURCE_AUTO)) /* don't touch */
1619		return 0;
1620      __again:
1621      	port = cfg->port[idx];
1622      	if (!port)
1623      		return -EINVAL;
1624      	value1 = &cfg->result.resource[idx].start;
1625      	value2 = &cfg->result.resource[idx].end;
1626	if (cfg->result.resource[idx].flags & IORESOURCE_AUTO) {
1627		cfg->result.resource[idx].flags &= ~IORESOURCE_AUTO;
1628		*value1 = port->min;
1629		*value2 = port->min + port->size - 1;
1630		if (!isapnp_check_port(cfg, *value1, port->size, idx))
1631			return 0;
1632	}
1633	do {
1634		*value1 += port->align;
1635		*value2 = *value1 + port->size - 1;
1636		if (*value1 > port->max || !port->align) {
1637			if (port->res && port->res->alt) {
1638				if ((err = isapnp_alternative_switch(cfg, port->res, port->res->alt))<0)
1639					return err;
1640				goto __again;
1641			}
1642			return -ENOENT;
1643		}
1644	} while (isapnp_check_port(cfg, *value1, port->size, idx));
1645	return 0;
1646}
1647
1648static void isapnp_test_handler(int irq, void *dev_id, struct pt_regs *regs)
1649{
1650}
1651
1652static int isapnp_check_interrupt(struct isapnp_cfgtmp *cfg, int irq, int idx)
1653{
1654	int i;
1655	struct pci_dev *dev;
1656
1657	if (irq < 0 || irq > 15)
1658		return 1;
1659	for (i = 0; i < 16; i++) {
1660		if (isapnp_reserve_irq[i] == irq)
1661			return 1;
1662	}
1663	isapnp_for_each_dev(dev) {
1664		if (dev->active) {
1665			if ((dev->irq_resource[0].flags && dev->irq_resource[0].start == irq) ||
1666			    (dev->irq_resource[1].flags && dev->irq_resource[1].start == irq))
1667				return 1;
1668		}
1669	}
1670#ifdef CONFIG_PCI
1671	if (!isapnp_skip_pci_scan) {
1672		pci_for_each_dev(dev) {
1673			if (dev->irq == irq)
1674				return 1;
1675		}
1676	}
1677#endif
1678	if (request_irq(irq, isapnp_test_handler, SA_INTERRUPT, "isapnp", NULL))
1679		return 1;
1680	free_irq(irq, NULL);
1681	for (i = 0; i < DEVICE_COUNT_IRQ; i++) {
1682		if (i == idx)
1683			continue;
1684		if (!cfg->result.irq_resource[i].flags)
1685			continue;
1686		if (cfg->result.irq_resource[i].flags & IORESOURCE_AUTO)
1687			continue;
1688		if (cfg->result.irq_resource[i].start == irq)
1689			return 1;
1690	}
1691	return 0;
1692}
1693
1694static int isapnp_valid_irq(struct isapnp_cfgtmp *cfg, int idx)
1695{
1696	/* IRQ priority: this table is good for i386 */
1697	static unsigned short xtab[16] = {
1698		5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
1699	};
1700	int err, i;
1701	unsigned long *value1, *value2;
1702	struct isapnp_irq *irq;
1703
1704	if (!cfg || idx < 0 || idx > 1)
1705		return -EINVAL;
1706	if (!(cfg->result.irq_resource[idx].flags & IORESOURCE_AUTO))
1707		return 0;
1708      __again:
1709      	irq = cfg->irq[idx];
1710      	if (!irq)
1711      		return -EINVAL;
1712      	value1 = &cfg->result.irq_resource[idx].start;
1713      	value2 = &cfg->result.irq_resource[idx].end;
1714	if (cfg->result.irq_resource[idx].flags & IORESOURCE_AUTO) {
1715		for (i = 0; i < 16 && !(irq->map & (1<<xtab[i])); i++);
1716		if (i >= 16)
1717			return -ENOENT;
1718		cfg->result.irq_resource[idx].flags &= ~IORESOURCE_AUTO;
1719		if (!isapnp_check_interrupt(cfg, *value1 = *value2 = xtab[i], idx))
1720			return 0;
1721	}
1722	do {
1723		for (i = 0; i < 16 && xtab[i] != *value1; i++);
1724		for (i++; i < 16 && !(irq->map & (1<<xtab[i])); i++);
1725		if (i >= 16) {
1726			if (irq->res && irq->res->alt) {
1727				if ((err = isapnp_alternative_switch(cfg, irq->res, irq->res->alt))<0)
1728					return err;
1729				goto __again;
1730			}
1731			return -ENOENT;
1732		} else {
1733			*value1 = *value2 = xtab[i];
1734		}
1735	} while (isapnp_check_interrupt(cfg, *value1, idx));
1736	return 0;
1737}
1738
1739static int isapnp_check_dma(struct isapnp_cfgtmp *cfg, int dma, int idx)
1740{
1741	int i, mindma =1;
1742	struct pci_dev *dev;
1743
1744	/* Some machines allow DMA 0, but others don't. In fact on some
1745	   boxes DMA 0 is the memory refresh. Play safe */
1746	if (isapnp_allow_dma0 == 1)
1747		mindma = 0;
1748	if (dma < mindma || dma == 4 || dma > 7)
1749		return 1;
1750	for (i = 0; i < 8; i++) {
1751		if (isapnp_reserve_dma[i] == dma)
1752			return 1;
1753	}
1754	isapnp_for_each_dev(dev) {
1755		if (dev->active) {
1756			if ((dev->dma_resource[0].flags && dev->dma_resource[0].start == dma) ||
1757			    (dev->dma_resource[1].flags && dev->dma_resource[1].start == dma))
1758				return 1;
1759		}
1760	}
1761	if (request_dma(dma, "isapnp"))
1762		return 1;
1763	free_dma(dma);
1764	for (i = 0; i < 2; i++) {
1765		if (i == idx)
1766			continue;
1767		if (!cfg->result.dma_resource[i].flags ||
1768		    (cfg->result.dma_resource[i].flags & IORESOURCE_AUTO))
1769			continue;
1770		if (cfg->result.dma_resource[i].start == dma)
1771			return 1;
1772	}
1773	return 0;
1774}
1775
1776static int isapnp_valid_dma(struct isapnp_cfgtmp *cfg, int idx)
1777{
1778	/* DMA priority: this table is good for i386 */
1779	static unsigned short xtab[16] = {
1780		1, 3, 5, 6, 7, 0, 2, 4
1781	};
1782	int err, i;
1783	unsigned long *value1, *value2;
1784	struct isapnp_dma *dma;
1785
1786	if (!cfg || idx < 0 || idx > 1)
1787		return -EINVAL;
1788	if (!(cfg->result.dma_resource[idx].flags & IORESOURCE_AUTO))	/* don't touch */
1789		return 0;
1790      __again:
1791      	dma = cfg->dma[idx];
1792      	if (!dma)
1793      		return -EINVAL;
1794      	value1 = &cfg->result.dma_resource[idx].start;
1795      	value2 = &cfg->result.dma_resource[idx].end;
1796	if (cfg->result.dma_resource[idx].flags & IORESOURCE_AUTO) {
1797		for (i = 0; i < 8 && !(dma->map & (1<<xtab[i])); i++);
1798		if (i >= 8)
1799			return -ENOENT;
1800		cfg->result.dma_resource[idx].flags &= ~IORESOURCE_AUTO;
1801		if (!isapnp_check_dma(cfg, *value1 = *value2 = xtab[i], idx))
1802			return 0;
1803	}
1804	do {
1805		for (i = 0; i < 8 && xtab[i] != *value1; i++);
1806		for (i++; i < 8 && !(dma->map & (1<<xtab[i])); i++);
1807		if (i >= 8) {
1808			if (dma->res && dma->res->alt) {
1809				if ((err = isapnp_alternative_switch(cfg, dma->res, dma->res->alt))<0)
1810					return err;
1811				goto __again;
1812			}
1813			return -ENOENT;
1814		} else {
1815			*value1 = *value2 = xtab[i];
1816		}
1817	} while (isapnp_check_dma(cfg, *value1, idx));
1818	return 0;
1819}
1820
1821static int isapnp_check_mem(struct isapnp_cfgtmp *cfg, unsigned int addr, unsigned int size, int idx)
1822{
1823	int i, tmp;
1824	unsigned int raddr, rsize;
1825	struct isapnp_mem *xmem;
1826	struct pci_dev *dev;
1827
1828	for (i = 0; i < 8; i++) {
1829		raddr = (unsigned int)isapnp_reserve_mem[i << 1];
1830		rsize = (unsigned int)isapnp_reserve_mem[(i << 1) + 1];
1831		if (addr >= raddr && addr < raddr + rsize)
1832			return 1;
1833		if (addr + size > raddr && addr + size < (raddr + rsize) - 1)
1834			return 1;
1835		if (__check_region(&iomem_resource, addr, size))
1836			return 1;
1837	}
1838	isapnp_for_each_dev(dev) {
1839		if (dev->active) {
1840			for (tmp = 0; tmp < 4; tmp++) {
1841				if (dev->resource[tmp].flags) {
1842					raddr = dev->resource[tmp + 8].start;
1843					rsize = (dev->resource[tmp + 8].end - raddr) + 1;
1844					if (addr >= raddr && addr < raddr + rsize)
1845						return 1;
1846					if (addr + size > raddr && addr + size < (raddr + rsize) - 1)
1847						return 1;
1848				}
1849			}
1850		}
1851	}
1852	for (i = 0; i < 4; i++) {
1853		unsigned int flags = cfg->request->resource[i + 8].flags;
1854		if (i == idx)
1855			continue;
1856		if (!flags)
1857			continue;
1858		tmp = cfg->result.resource[i + 8].start;
1859		if (flags & IORESOURCE_AUTO) {		/* auto */
1860			xmem = cfg->mem[i];
1861			if (!xmem)
1862				return 1;
1863			if (cfg->result.resource[i + 8].flags & IORESOURCE_AUTO)
1864				continue;
1865			if (tmp + xmem->size >= addr && tmp <= addr + xmem->size)
1866				return 1;
1867			continue;
1868		}
1869		if (addr == tmp)
1870			return 1;
1871		xmem = isapnp_find_mem(cfg->request, i);
1872		if (!xmem)
1873			return 1;
1874		if (tmp + xmem->size >= addr && tmp <= addr + xmem->size)
1875			return 1;
1876	}
1877	return 0;
1878}
1879
1880static int isapnp_valid_mem(struct isapnp_cfgtmp *cfg, int idx)
1881{
1882	int err;
1883	unsigned long *value1, *value2;
1884	struct isapnp_mem *mem;
1885
1886	if (!cfg || idx < 0 || idx > 3)
1887		return -EINVAL;
1888	if (!(cfg->result.resource[idx + 8].flags & IORESOURCE_AUTO)) /* don't touch */
1889		return 0;
1890      __again:
1891      	mem = cfg->mem[idx];
1892      	if (!mem)
1893      		return -EINVAL;
1894      	value1 = &cfg->result.resource[idx + 8].start;
1895      	value2 = &cfg->result.resource[idx + 8].end;
1896	if (cfg->result.resource[idx + 8].flags & IORESOURCE_AUTO) {
1897		cfg->result.resource[idx + 8].flags &= ~IORESOURCE_AUTO;
1898		*value1 = mem->min;
1899		*value2 = mem->min + mem->size - 1;
1900		if (!isapnp_check_mem(cfg, *value1, mem->size, idx))
1901			return 0;
1902	}
1903	do {
1904		*value1 += mem->align;
1905		*value2 = *value1 + mem->size - 1;
1906		if (*value1 > mem->max || !mem->align) {
1907			if (mem->res && mem->res->alt) {
1908				if ((err = isapnp_alternative_switch(cfg, mem->res, mem->res->alt))<0)
1909					return err;
1910				goto __again;
1911			}
1912			return -ENOENT;
1913		}
1914	} while (isapnp_check_mem(cfg, *value1, mem->size, idx));
1915	return 0;
1916}
1917
1918static int isapnp_check_valid(struct isapnp_cfgtmp *cfg)
1919{
1920	int tmp;
1921
1922	for (tmp = 0; tmp < 8; tmp++)
1923		if (cfg->result.resource[tmp].flags & IORESOURCE_AUTO)
1924			return -EAGAIN;
1925	for (tmp = 0; tmp < 2; tmp++)
1926		if (cfg->result.irq_resource[tmp].flags & IORESOURCE_AUTO)
1927			return -EAGAIN;
1928	for (tmp = 0; tmp < 2; tmp++)
1929		if (cfg->result.dma_resource[tmp].flags & IORESOURCE_AUTO)
1930			return -EAGAIN;
1931	for (tmp = 0; tmp < 4; tmp++)
1932		if (cfg->result.resource[tmp + 8].flags & IORESOURCE_AUTO)
1933			return -EAGAIN;
1934	return 0;
1935}
1936
1937static int isapnp_config_activate(struct pci_dev *dev)
1938{
1939	struct isapnp_cfgtmp cfg;
1940	int tmp, fauto, err;
1941
1942	if (!dev)
1943		return -EINVAL;
1944	if (dev->active)
1945		return -EBUSY;
1946	memset(&cfg, 0, sizeof(cfg));
1947	cfg.request = dev;
1948	memcpy(&cfg.result, dev, sizeof(struct pci_dev));
1949	/* check if all values are set, otherwise try auto-configuration */
1950	for (tmp = fauto = 0; !fauto && tmp < 8; tmp++) {
1951		if (dev->resource[tmp].flags & IORESOURCE_AUTO)
1952			fauto++;
1953	}
1954	for (tmp = 0; !fauto && tmp < 2; tmp++) {
1955		if (dev->irq_resource[tmp].flags & IORESOURCE_AUTO)
1956			fauto++;
1957	}
1958	for (tmp = 0; !fauto && tmp < 2; tmp++) {
1959		if (dev->dma_resource[tmp].flags & IORESOURCE_AUTO)
1960			fauto++;
1961	}
1962	for (tmp = 0; !fauto && tmp < 4; tmp++) {
1963		if (dev->resource[tmp + 8].flags & IORESOURCE_AUTO)
1964			fauto++;
1965	}
1966	if (!fauto)
1967		goto __skip_auto;
1968	/* set variables to initial values */
1969	if ((err = isapnp_alternative_switch(&cfg, NULL, NULL))<0)
1970		return err;
1971	/* find first valid configuration */
1972	fauto = 0;
1973	do {
1974		for (tmp = 0; tmp < 8 && cfg.result.resource[tmp].flags; tmp++)
1975			if ((err = isapnp_valid_port(&cfg, tmp))<0)
1976				return err;
1977		for (tmp = 0; tmp < 2 && cfg.result.irq_resource[tmp].flags; tmp++)
1978			if ((err = isapnp_valid_irq(&cfg, tmp))<0)
1979				return err;
1980		for (tmp = 0; tmp < 2 && cfg.result.dma_resource[tmp].flags; tmp++)
1981			if ((err = isapnp_valid_dma(&cfg, tmp))<0)
1982				return err;
1983		for (tmp = 0; tmp < 4 && cfg.result.resource[tmp + 8].flags; tmp++)
1984			if ((err = isapnp_valid_mem(&cfg, tmp))<0)
1985				return err;
1986	} while (isapnp_check_valid(&cfg)<0 && fauto++ < 20);
1987	if (fauto >= 20)
1988		return -EAGAIN;
1989      __skip_auto:
1990      	/* we have valid configuration, try configure hardware */
1991      	isapnp_cfg_begin(dev->bus->number, dev->devfn);
1992	dev->active = 1;
1993	dev->irq_resource[0] = cfg.result.irq_resource[0];
1994	dev->irq_resource[1] = cfg.result.irq_resource[1];
1995	dev->dma_resource[0] = cfg.result.dma_resource[0];
1996	dev->dma_resource[1] = cfg.result.dma_resource[1];
1997	for (tmp = 0; tmp < 12; tmp++) {
1998		dev->resource[tmp] = cfg.result.resource[tmp];
1999	}
2000	for (tmp = 0; tmp < 8 && dev->resource[tmp].flags; tmp++)
2001		isapnp_write_word(ISAPNP_CFG_PORT+(tmp<<1), dev->resource[tmp].start);
2002	for (tmp = 0; tmp < 2 && dev->irq_resource[tmp].flags; tmp++) {
2003		int irq = dev->irq_resource[tmp].start;
2004		if (irq == 2)
2005			irq = 9;
2006		isapnp_write_byte(ISAPNP_CFG_IRQ+(tmp<<1), irq);
2007	}
2008	for (tmp = 0; tmp < 2 && dev->dma_resource[tmp].flags; tmp++)
2009		isapnp_write_byte(ISAPNP_CFG_DMA+tmp, dev->dma_resource[tmp].start);
2010	for (tmp = 0; tmp < 4 && dev->resource[tmp+8].flags; tmp++)
2011		isapnp_write_word(ISAPNP_CFG_MEM+(tmp<<2), (dev->resource[tmp + 8].start >> 8) & 0xffff);
2012	isapnp_activate(dev->devfn);
2013	isapnp_cfg_end();
2014	return 0;
2015}
2016
2017static int isapnp_config_deactivate(struct pci_dev *dev)
2018{
2019	if (!dev || !dev->active)
2020		return -EINVAL;
2021      	isapnp_cfg_begin(dev->bus->number, dev->devfn);
2022	isapnp_deactivate(dev->devfn);
2023	dev->active = 0;
2024	isapnp_cfg_end();
2025	return 0;
2026}
2027
2028void isapnp_resource_change(struct resource *resource,
2029			    unsigned long start,
2030			    unsigned long size)
2031{
2032	if (resource == NULL)
2033		return;
2034	resource->flags &= ~IORESOURCE_AUTO;
2035	resource->start = start;
2036	resource->end = start + size - 1;
2037}
2038
2039/*
2040 *  Inititialization.
2041 */
2042
2043#ifdef MODULE
2044
2045static void isapnp_free_port(struct isapnp_port *port)
2046{
2047	struct isapnp_port *next;
2048
2049	while (port) {
2050		next = port->next;
2051		kfree(port);
2052		port = next;
2053	}
2054}
2055
2056static void isapnp_free_irq(struct isapnp_irq *irq)
2057{
2058	struct isapnp_irq *next;
2059
2060	while (irq) {
2061		next = irq->next;
2062		kfree(irq);
2063		irq = next;
2064	}
2065}
2066
2067static void isapnp_free_dma(struct isapnp_dma *dma)
2068{
2069	struct isapnp_dma *next;
2070
2071	while (dma) {
2072		next = dma->next;
2073		kfree(dma);
2074		dma = next;
2075	}
2076}
2077
2078static void isapnp_free_mem(struct isapnp_mem *mem)
2079{
2080	struct isapnp_mem *next;
2081
2082	while (mem) {
2083		next = mem->next;
2084		kfree(mem);
2085		mem = next;
2086	}
2087}
2088
2089static void isapnp_free_mem32(struct isapnp_mem32 *mem32)
2090{
2091	struct isapnp_mem32 *next;
2092
2093	while (mem32) {
2094		next = mem32->next;
2095		kfree(mem32);
2096		mem32 = next;
2097	}
2098}
2099
2100static void isapnp_free_resources(struct isapnp_resources *resources, int alt)
2101{
2102	struct isapnp_resources *next;
2103
2104	while (resources) {
2105		next = alt ? resources->alt : resources->next;
2106		isapnp_free_port(resources->port);
2107		isapnp_free_irq(resources->irq);
2108		isapnp_free_dma(resources->dma);
2109		isapnp_free_mem(resources->mem);
2110		isapnp_free_mem32(resources->mem32);
2111		if (!alt && resources->alt)
2112			isapnp_free_resources(resources->alt, 1);
2113		kfree(resources);
2114		resources = next;
2115	}
2116}
2117
2118static void isapnp_free_card(struct pci_bus *card)
2119{
2120	while (!list_empty(&card->devices)) {
2121		struct list_head *list = card->devices.next;
2122		struct pci_dev *dev = pci_dev_b(list);
2123		list_del(list);
2124		isapnp_free_resources((struct isapnp_resources *)dev->sysdata, 0);
2125		kfree(dev);
2126	}
2127	kfree(card);
2128}
2129
2130static void isapnp_free_all_resources(void)
2131{
2132#ifdef ISAPNP_REGION_OK
2133	release_region(_PIDXR, 1);
2134#endif
2135	release_region(_PNPWRP, 1);
2136	release_region(isapnp_rdp, 1);
2137#ifdef CONFIG_PROC_FS
2138	isapnp_proc_done();
2139#endif
2140	while (!list_empty(&isapnp_cards)) {
2141		struct list_head *list = isapnp_cards.next;
2142		list_del(list);
2143		isapnp_free_card(pci_bus_b(list));
2144	}
2145}
2146
2147#endif /* MODULE */
2148
2149static int isapnp_announce_device(struct isapnp_driver *drv,
2150				  struct pci_dev *dev)
2151{
2152	const struct isapnp_device_id *id;
2153	int ret = 0;
2154
2155	if (drv->id_table) {
2156		id = isapnp_match_dev(drv->id_table, dev);
2157		if (!id) {
2158			ret = 0;
2159			goto out;
2160		}
2161	} else
2162		id = NULL;
2163
2164	if (drv->probe(dev, id) >= 0) {
2165		dev->driver = (struct pci_driver *) drv;
2166		ret = 1;
2167	}
2168out:
2169	return ret;
2170}
2171
2172/**
2173 * isapnp_dev_driver - get the isapnp_driver of a device
2174 * @dev: the device to query
2175 *
2176 * Returns the appropriate isapnp_driver structure or %NULL if there is no
2177 * registered driver for the device.
2178 */
2179static struct isapnp_driver *isapnp_dev_driver(const struct pci_dev *dev)
2180{
2181	return (struct isapnp_driver *) dev->driver;
2182}
2183
2184static LIST_HEAD(isapnp_drivers);
2185
2186/**
2187 * isapnp_register_driver - register a new ISAPnP driver
2188 * @drv: the driver structure to register
2189 *
2190 * Adds the driver structure to the list of registered ISAPnP drivers
2191 * Returns the number of isapnp devices which were claimed by the driver
2192 * during registration.  The driver remains registered even if the
2193 * return value is zero.
2194 */
2195int isapnp_register_driver(struct isapnp_driver *drv)
2196{
2197	struct pci_dev *dev;
2198	int count = 0;
2199
2200	list_add_tail(&drv->node, &isapnp_drivers);
2201
2202	isapnp_for_each_dev(dev) {
2203		if (!isapnp_dev_driver(dev))
2204			count += isapnp_announce_device(drv, dev);
2205	}
2206	return count;
2207}
2208
2209/**
2210 * isapnp_unregister_driver - unregister an isapnp driver
2211 * @drv: the driver structure to unregister
2212 *
2213 * Deletes the driver structure from the list of registered ISAPnP drivers,
2214 * gives it a chance to clean up by calling its remove() function for
2215 * each device it was responsible for, and marks those devices as
2216 * driverless.
2217 */
2218void isapnp_unregister_driver(struct isapnp_driver *drv)
2219{
2220	struct pci_dev *dev;
2221
2222	list_del(&drv->node);
2223	isapnp_for_each_dev(dev) {
2224		if (dev->driver == (struct pci_driver *) drv) {
2225			if (drv->remove)
2226				drv->remove(dev);
2227			dev->driver = NULL;
2228		}
2229	}
2230}
2231
2232EXPORT_SYMBOL(isapnp_cards);
2233EXPORT_SYMBOL(isapnp_devices);
2234EXPORT_SYMBOL(isapnp_present);
2235EXPORT_SYMBOL(isapnp_cfg_begin);
2236EXPORT_SYMBOL(isapnp_cfg_end);
2237EXPORT_SYMBOL(isapnp_read_byte);
2238EXPORT_SYMBOL(isapnp_read_word);
2239EXPORT_SYMBOL(isapnp_read_dword);
2240EXPORT_SYMBOL(isapnp_write_byte);
2241EXPORT_SYMBOL(isapnp_write_word);
2242EXPORT_SYMBOL(isapnp_write_dword);
2243EXPORT_SYMBOL(isapnp_wake);
2244EXPORT_SYMBOL(isapnp_device);
2245EXPORT_SYMBOL(isapnp_activate);
2246EXPORT_SYMBOL(isapnp_deactivate);
2247EXPORT_SYMBOL(isapnp_find_card);
2248EXPORT_SYMBOL(isapnp_find_dev);
2249EXPORT_SYMBOL(isapnp_probe_cards);
2250EXPORT_SYMBOL(isapnp_probe_devs);
2251EXPORT_SYMBOL(isapnp_activate_dev);
2252EXPORT_SYMBOL(isapnp_resource_change);
2253EXPORT_SYMBOL(isapnp_register_driver);
2254EXPORT_SYMBOL(isapnp_unregister_driver);
2255
2256int __init isapnp_init(void)
2257{
2258	int cards;
2259	struct pci_bus *card;
2260
2261	if (isapnp_disable) {
2262		isapnp_detected = 0;
2263		printk(KERN_INFO "isapnp: ISA Plug & Play support disabled\n");
2264		return 0;
2265	}
2266#ifdef ISAPNP_REGION_OK
2267	if (!request_region(_PIDXR, 1, "isapnp index")) {
2268		printk(KERN_ERR "isapnp: Index Register 0x%x already used\n", _PIDXR);
2269		return -EBUSY;
2270	}
2271#endif
2272	if (!request_region(_PNPWRP, 1, "isapnp write")) {
2273		printk(KERN_ERR "isapnp: Write Data Register 0x%x already used\n", _PNPWRP);
2274#ifdef ISAPNP_REGION_OK
2275		release_region(_PIDXR, 1);
2276#endif
2277		return -EBUSY;
2278	}
2279
2280	/*
2281	 *	Print a message. The existing ISAPnP code is hanging machines
2282	 *	so let the user know where.
2283	 */
2284
2285	printk(KERN_INFO "isapnp: Scanning for PnP cards...\n");
2286	if (isapnp_rdp >= 0x203 && isapnp_rdp <= 0x3ff) {
2287		isapnp_rdp |= 3;
2288		if (!request_region(isapnp_rdp, 1, "isapnp read")) {
2289			printk(KERN_ERR "isapnp: Read Data Register 0x%x already used\n", isapnp_rdp);
2290#ifdef ISAPNP_REGION_OK
2291			release_region(_PIDXR, 1);
2292#endif
2293			release_region(_PNPWRP, 1);
2294			return -EBUSY;
2295		}
2296		isapnp_set_rdp();
2297	}
2298	isapnp_detected = 1;
2299	if (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff) {
2300		cards = isapnp_isolate();
2301		if (cards < 0 ||
2302		    (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff)) {
2303#ifdef ISAPNP_REGION_OK
2304			release_region(_PIDXR, 1);
2305#endif
2306			release_region(_PNPWRP, 1);
2307			isapnp_detected = 0;
2308			printk(KERN_INFO "isapnp: No Plug & Play device found\n");
2309			return 0;
2310		}
2311		request_region(isapnp_rdp, 1, "isapnp read");
2312	}
2313	isapnp_build_device_list();
2314	cards = 0;
2315
2316	isapnp_for_each_card(card) {
2317		cards++;
2318		if (isapnp_verbose) {
2319			struct list_head *devlist;
2320			printk(KERN_INFO "isapnp: Card '%s'\n", card->name[0]?card->name:"Unknown");
2321			if (isapnp_verbose < 2)
2322				continue;
2323			for (devlist = card->devices.next; devlist != &card->devices; devlist = devlist->next) {
2324				struct pci_dev *dev = pci_dev_b(devlist);
2325				printk(KERN_INFO "isapnp:   Device '%s'\n", dev->name[0]?card->name:"Unknown");
2326			}
2327		}
2328	}
2329	if (cards) {
2330		printk(KERN_INFO "isapnp: %i Plug & Play card%s detected total\n", cards, cards>1?"s":"");
2331	} else {
2332		printk(KERN_INFO "isapnp: No Plug & Play card found\n");
2333	}
2334#ifdef CONFIG_PROC_FS
2335	isapnp_proc_init();
2336#endif
2337	return 0;
2338}
2339
2340#ifdef MODULE
2341
2342int init_module(void)
2343{
2344	return isapnp_init();
2345}
2346
2347void cleanup_module(void)
2348{
2349	if (isapnp_detected)
2350		isapnp_free_all_resources();
2351}
2352
2353#else
2354
2355/* format is: noisapnp */
2356
2357static int __init isapnp_setup_disable(char *str)
2358{
2359	isapnp_disable = 1;
2360	return 1;
2361}
2362
2363__setup("noisapnp", isapnp_setup_disable);
2364
2365/* format is: isapnp=rdp,reset,skip_pci_scan,verbose */
2366
2367static int __init isapnp_setup_isapnp(char *str)
2368{
2369	(void)((get_option(&str,&isapnp_rdp) == 2) &&
2370	       (get_option(&str,&isapnp_reset) == 2) &&
2371	       (get_option(&str,&isapnp_skip_pci_scan) == 2) &&
2372	       (get_option(&str,&isapnp_verbose) == 2));
2373	return 1;
2374}
2375
2376__setup("isapnp=", isapnp_setup_isapnp);
2377
2378/* format is: isapnp_reserve_irq=irq1[,irq2] .... */
2379
2380static int __init isapnp_setup_reserve_irq(char *str)
2381{
2382	int i;
2383
2384	for (i = 0; i < 16; i++)
2385		if (get_option(&str,&isapnp_reserve_irq[i]) != 2)
2386			break;
2387	return 1;
2388}
2389
2390__setup("isapnp_reserve_irq=", isapnp_setup_reserve_irq);
2391
2392/* format is: isapnp_reserve_dma=dma1[,dma2] .... */
2393
2394static int __init isapnp_setup_reserve_dma(char *str)
2395{
2396	int i;
2397
2398	for (i = 0; i < 8; i++)
2399		if (get_option(&str,&isapnp_reserve_dma[i]) != 2)
2400			break;
2401	return 1;
2402}
2403
2404__setup("isapnp_reserve_dma=", isapnp_setup_reserve_dma);
2405
2406/* format is: isapnp_reserve_io=io1,size1[,io2,size2] .... */
2407
2408static int __init isapnp_setup_reserve_io(char *str)
2409{
2410	int i;
2411
2412	for (i = 0; i < 16; i++)
2413		if (get_option(&str,&isapnp_reserve_io[i]) != 2)
2414			break;
2415	return 1;
2416}
2417
2418__setup("isapnp_reserve_io=", isapnp_setup_reserve_io);
2419
2420/* format is: isapnp_reserve_mem=mem1,size1[,mem2,size2] .... */
2421
2422static int __init isapnp_setup_reserve_mem(char *str)
2423{
2424	int i;
2425
2426	for (i = 0; i < 16; i++)
2427		if (get_option(&str,&isapnp_reserve_mem[i]) != 2)
2428			break;
2429	return 1;
2430}
2431
2432__setup("isapnp_reserve_mem=", isapnp_setup_reserve_mem);
2433
2434#endif
2435