xref: /openbmc/linux/drivers/pnp/isapnp/core.c (revision 772defc6)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  ISA Plug & Play support
3c1017a4cSJaroslav Kysela  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *   This program is free software; you can redistribute it and/or modify
71da177e4SLinus Torvalds  *   it under the terms of the GNU General Public License as published by
81da177e4SLinus Torvalds  *   the Free Software Foundation; either version 2 of the License, or
91da177e4SLinus Torvalds  *   (at your option) any later version.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *   This program is distributed in the hope that it will be useful,
121da177e4SLinus Torvalds  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
131da177e4SLinus Torvalds  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
141da177e4SLinus Torvalds  *   GNU General Public License for more details.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *   You should have received a copy of the GNU General Public License
171da177e4SLinus Torvalds  *   along with this program; if not, write to the Free Software
181da177e4SLinus Torvalds  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *  Changelog:
211da177e4SLinus Torvalds  *  2000-01-01	Added quirks handling for buggy hardware
221da177e4SLinus Torvalds  *		Peter Denison <peterd@pnd-pc.demon.co.uk>
231da177e4SLinus Torvalds  *  2000-06-14	Added isapnp_probe_devs() and isapnp_activate_dev()
241da177e4SLinus Torvalds  *		Christoph Hellwig <hch@infradead.org>
251da177e4SLinus Torvalds  *  2001-06-03  Added release_region calls to correspond with
261da177e4SLinus Torvalds  *		request_region calls when a failure occurs.  Also
271da177e4SLinus Torvalds  *		added KERN_* constants to printk() calls.
281da177e4SLinus Torvalds  *  2001-11-07  Added isapnp_{,un}register_driver calls along the lines
291da177e4SLinus Torvalds  *              of the pci driver interface
301da177e4SLinus Torvalds  *              Kai Germaschewski <kai.germaschewski@gmx.de>
311da177e4SLinus Torvalds  *  2002-06-06  Made the use of dma channel 0 configurable
321da177e4SLinus Torvalds  *              Gerald Teschl <gerald.teschl@univie.ac.at>
331da177e4SLinus Torvalds  *  2002-10-06  Ported to PnP Layer - Adam Belay <ambx1@neo.rr.com>
341da177e4SLinus Torvalds  *  2003-08-11	Resource Management Updates - Adam Belay <ambx1@neo.rr.com>
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #include <linux/module.h>
381da177e4SLinus Torvalds #include <linux/kernel.h>
391da177e4SLinus Torvalds #include <linux/errno.h>
401da177e4SLinus Torvalds #include <linux/slab.h>
411da177e4SLinus Torvalds #include <linux/delay.h>
421da177e4SLinus Torvalds #include <linux/init.h>
431da177e4SLinus Torvalds #include <linux/isapnp.h>
4414cc3e2bSIngo Molnar #include <linux/mutex.h>
451da177e4SLinus Torvalds #include <asm/io.h>
461da177e4SLinus Torvalds 
47772defc6SBjorn Helgaas #include "../base.h"
48772defc6SBjorn Helgaas 
491da177e4SLinus Torvalds #if 0
501da177e4SLinus Torvalds #define ISAPNP_REGION_OK
511da177e4SLinus Torvalds #endif
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds int isapnp_disable;		/* Disable ISA PnP */
541da177e4SLinus Torvalds static int isapnp_rdp;		/* Read Data Port */
551da177e4SLinus Torvalds static int isapnp_reset = 1;	/* reset all PnP cards (deactivate) */
561da177e4SLinus Torvalds static int isapnp_verbose = 1;	/* verbose mode */
571da177e4SLinus Torvalds 
58c1017a4cSJaroslav Kysela MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
591da177e4SLinus Torvalds MODULE_DESCRIPTION("Generic ISA Plug & Play support");
601da177e4SLinus Torvalds module_param(isapnp_disable, int, 0);
611da177e4SLinus Torvalds MODULE_PARM_DESC(isapnp_disable, "ISA Plug & Play disable");
621da177e4SLinus Torvalds module_param(isapnp_rdp, int, 0);
631da177e4SLinus Torvalds MODULE_PARM_DESC(isapnp_rdp, "ISA Plug & Play read data port");
641da177e4SLinus Torvalds module_param(isapnp_reset, int, 0);
651da177e4SLinus Torvalds MODULE_PARM_DESC(isapnp_reset, "ISA Plug & Play reset all cards");
661da177e4SLinus Torvalds module_param(isapnp_verbose, int, 0);
671da177e4SLinus Torvalds MODULE_PARM_DESC(isapnp_verbose, "ISA Plug & Play verbose mode");
681da177e4SLinus Torvalds MODULE_LICENSE("GPL");
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds #define _PIDXR		0x279
711da177e4SLinus Torvalds #define _PNPWRP		0xa79
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /* short tags */
741da177e4SLinus Torvalds #define _STAG_PNPVERNO		0x01
751da177e4SLinus Torvalds #define _STAG_LOGDEVID		0x02
761da177e4SLinus Torvalds #define _STAG_COMPATDEVID	0x03
771da177e4SLinus Torvalds #define _STAG_IRQ		0x04
781da177e4SLinus Torvalds #define _STAG_DMA		0x05
791da177e4SLinus Torvalds #define _STAG_STARTDEP		0x06
801da177e4SLinus Torvalds #define _STAG_ENDDEP		0x07
811da177e4SLinus Torvalds #define _STAG_IOPORT		0x08
821da177e4SLinus Torvalds #define _STAG_FIXEDIO		0x09
831da177e4SLinus Torvalds #define _STAG_VENDOR		0x0e
841da177e4SLinus Torvalds #define _STAG_END		0x0f
851da177e4SLinus Torvalds /* long tags */
861da177e4SLinus Torvalds #define _LTAG_MEMRANGE		0x81
871da177e4SLinus Torvalds #define _LTAG_ANSISTR		0x82
881da177e4SLinus Torvalds #define _LTAG_UNICODESTR	0x83
891da177e4SLinus Torvalds #define _LTAG_VENDOR		0x84
901da177e4SLinus Torvalds #define _LTAG_MEM32RANGE	0x85
911da177e4SLinus Torvalds #define _LTAG_FIXEDMEM32RANGE	0x86
921da177e4SLinus Torvalds 
93ca0e8b6fSBjorn Helgaas /* Logical device control and configuration registers */
94ca0e8b6fSBjorn Helgaas 
95ca0e8b6fSBjorn Helgaas #define ISAPNP_CFG_ACTIVATE	0x30	/* byte */
96ca0e8b6fSBjorn Helgaas #define ISAPNP_CFG_MEM		0x40	/* 4 * dword */
97ca0e8b6fSBjorn Helgaas #define ISAPNP_CFG_PORT		0x60	/* 8 * word */
98ca0e8b6fSBjorn Helgaas #define ISAPNP_CFG_IRQ		0x70	/* 2 * word */
99ca0e8b6fSBjorn Helgaas #define ISAPNP_CFG_DMA		0x74	/* 2 * byte */
100ca0e8b6fSBjorn Helgaas 
101245073f0SBjorn Helgaas /*
102245073f0SBjorn Helgaas  * Sizes of ISAPNP logical device configuration register sets.
103245073f0SBjorn Helgaas  * See PNP-ISA-v1.0a.pdf, Appendix A.
104245073f0SBjorn Helgaas  */
105245073f0SBjorn Helgaas #define ISAPNP_MAX_MEM		4
106245073f0SBjorn Helgaas #define ISAPNP_MAX_PORT		8
107245073f0SBjorn Helgaas #define ISAPNP_MAX_IRQ		2
108245073f0SBjorn Helgaas #define ISAPNP_MAX_DMA		2
109245073f0SBjorn Helgaas 
1101da177e4SLinus Torvalds static unsigned char isapnp_checksum_value;
11114cc3e2bSIngo Molnar static DEFINE_MUTEX(isapnp_cfg_mutex);
1121da177e4SLinus Torvalds static int isapnp_csn_count;
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds /* some prototypes */
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds static inline void write_data(unsigned char x)
1171da177e4SLinus Torvalds {
1181da177e4SLinus Torvalds 	outb(x, _PNPWRP);
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds static inline void write_address(unsigned char x)
1221da177e4SLinus Torvalds {
1231da177e4SLinus Torvalds 	outb(x, _PIDXR);
1241da177e4SLinus Torvalds 	udelay(20);
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds static inline unsigned char read_data(void)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	unsigned char val = inb(isapnp_rdp);
1301da177e4SLinus Torvalds 	return val;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds unsigned char isapnp_read_byte(unsigned char idx)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	write_address(idx);
1361da177e4SLinus Torvalds 	return read_data();
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds static unsigned short isapnp_read_word(unsigned char idx)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	unsigned short val;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	val = isapnp_read_byte(idx);
1441da177e4SLinus Torvalds 	val = (val << 8) + isapnp_read_byte(idx + 1);
1451da177e4SLinus Torvalds 	return val;
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds void isapnp_write_byte(unsigned char idx, unsigned char val)
1491da177e4SLinus Torvalds {
1501da177e4SLinus Torvalds 	write_address(idx);
1511da177e4SLinus Torvalds 	write_data(val);
1521da177e4SLinus Torvalds }
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds static void isapnp_write_word(unsigned char idx, unsigned short val)
1551da177e4SLinus Torvalds {
1561da177e4SLinus Torvalds 	isapnp_write_byte(idx, val >> 8);
1571da177e4SLinus Torvalds 	isapnp_write_byte(idx + 1, val);
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds static void isapnp_key(void)
1611da177e4SLinus Torvalds {
1621da177e4SLinus Torvalds 	unsigned char code = 0x6a, msb;
1631da177e4SLinus Torvalds 	int i;
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	mdelay(1);
1661da177e4SLinus Torvalds 	write_address(0x00);
1671da177e4SLinus Torvalds 	write_address(0x00);
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	write_address(code);
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	for (i = 1; i < 32; i++) {
1721da177e4SLinus Torvalds 		msb = ((code & 0x01) ^ ((code & 0x02) >> 1)) << 7;
1731da177e4SLinus Torvalds 		code = (code >> 1) | msb;
1741da177e4SLinus Torvalds 		write_address(code);
1751da177e4SLinus Torvalds 	}
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds /* place all pnp cards in wait-for-key state */
1791da177e4SLinus Torvalds static void isapnp_wait(void)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	isapnp_write_byte(0x02, 0x02);
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds static void isapnp_wake(unsigned char csn)
1851da177e4SLinus Torvalds {
1861da177e4SLinus Torvalds 	isapnp_write_byte(0x03, csn);
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds static void isapnp_device(unsigned char logdev)
1901da177e4SLinus Torvalds {
1911da177e4SLinus Torvalds 	isapnp_write_byte(0x07, logdev);
1921da177e4SLinus Torvalds }
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds static void isapnp_activate(unsigned char logdev)
1951da177e4SLinus Torvalds {
1961da177e4SLinus Torvalds 	isapnp_device(logdev);
1971da177e4SLinus Torvalds 	isapnp_write_byte(ISAPNP_CFG_ACTIVATE, 1);
1981da177e4SLinus Torvalds 	udelay(250);
1991da177e4SLinus Torvalds }
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds static void isapnp_deactivate(unsigned char logdev)
2021da177e4SLinus Torvalds {
2031da177e4SLinus Torvalds 	isapnp_device(logdev);
2041da177e4SLinus Torvalds 	isapnp_write_byte(ISAPNP_CFG_ACTIVATE, 0);
2051da177e4SLinus Torvalds 	udelay(500);
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds static void __init isapnp_peek(unsigned char *data, int bytes)
2091da177e4SLinus Torvalds {
2101da177e4SLinus Torvalds 	int i, j;
2111da177e4SLinus Torvalds 	unsigned char d = 0;
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	for (i = 1; i <= bytes; i++) {
2141da177e4SLinus Torvalds 		for (j = 0; j < 20; j++) {
2151da177e4SLinus Torvalds 			d = isapnp_read_byte(0x05);
2161da177e4SLinus Torvalds 			if (d & 1)
2171da177e4SLinus Torvalds 				break;
2181da177e4SLinus Torvalds 			udelay(100);
2191da177e4SLinus Torvalds 		}
2201da177e4SLinus Torvalds 		if (!(d & 1)) {
2211da177e4SLinus Torvalds 			if (data != NULL)
2221da177e4SLinus Torvalds 				*data++ = 0xff;
2231da177e4SLinus Torvalds 			continue;
2241da177e4SLinus Torvalds 		}
2251da177e4SLinus Torvalds 		d = isapnp_read_byte(0x04);	/* PRESDI */
2261da177e4SLinus Torvalds 		isapnp_checksum_value += d;
2271da177e4SLinus Torvalds 		if (data != NULL)
2281da177e4SLinus Torvalds 			*data++ = d;
2291da177e4SLinus Torvalds 	}
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds #define RDP_STEP	32	/* minimum is 4 */
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds static int isapnp_next_rdp(void)
2351da177e4SLinus Torvalds {
2361da177e4SLinus Torvalds 	int rdp = isapnp_rdp;
2371da177e4SLinus Torvalds 	static int old_rdp = 0;
2381da177e4SLinus Torvalds 
2399dd78466SBjorn Helgaas 	if (old_rdp) {
2401da177e4SLinus Torvalds 		release_region(old_rdp, 1);
2411da177e4SLinus Torvalds 		old_rdp = 0;
2421da177e4SLinus Torvalds 	}
2431da177e4SLinus Torvalds 	while (rdp <= 0x3ff) {
2441da177e4SLinus Torvalds 		/*
2451da177e4SLinus Torvalds 		 *      We cannot use NE2000 probe spaces for ISAPnP or we
2461da177e4SLinus Torvalds 		 *      will lock up machines.
2471da177e4SLinus Torvalds 		 */
2489dd78466SBjorn Helgaas 		if ((rdp < 0x280 || rdp > 0x380)
2499dd78466SBjorn Helgaas 		    && request_region(rdp, 1, "ISAPnP")) {
2501da177e4SLinus Torvalds 			isapnp_rdp = rdp;
2511da177e4SLinus Torvalds 			old_rdp = rdp;
2521da177e4SLinus Torvalds 			return 0;
2531da177e4SLinus Torvalds 		}
2541da177e4SLinus Torvalds 		rdp += RDP_STEP;
2551da177e4SLinus Torvalds 	}
2561da177e4SLinus Torvalds 	return -1;
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds /* Set read port address */
2601da177e4SLinus Torvalds static inline void isapnp_set_rdp(void)
2611da177e4SLinus Torvalds {
2621da177e4SLinus Torvalds 	isapnp_write_byte(0x00, isapnp_rdp >> 2);
2631da177e4SLinus Torvalds 	udelay(100);
2641da177e4SLinus Torvalds }
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds /*
2671da177e4SLinus Torvalds  *	Perform an isolation. The port selection code now tries to avoid
2681da177e4SLinus Torvalds  *	"dangerous to read" ports.
2691da177e4SLinus Torvalds  */
2701da177e4SLinus Torvalds static int __init isapnp_isolate_rdp_select(void)
2711da177e4SLinus Torvalds {
2721da177e4SLinus Torvalds 	isapnp_wait();
2731da177e4SLinus Torvalds 	isapnp_key();
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	/* Control: reset CSN and conditionally everything else too */
2761da177e4SLinus Torvalds 	isapnp_write_byte(0x02, isapnp_reset ? 0x05 : 0x04);
2771da177e4SLinus Torvalds 	mdelay(2);
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	isapnp_wait();
2801da177e4SLinus Torvalds 	isapnp_key();
2811da177e4SLinus Torvalds 	isapnp_wake(0x00);
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	if (isapnp_next_rdp() < 0) {
2841da177e4SLinus Torvalds 		isapnp_wait();
2851da177e4SLinus Torvalds 		return -1;
2861da177e4SLinus Torvalds 	}
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	isapnp_set_rdp();
2891da177e4SLinus Torvalds 	udelay(1000);
2901da177e4SLinus Torvalds 	write_address(0x01);
2911da177e4SLinus Torvalds 	udelay(1000);
2921da177e4SLinus Torvalds 	return 0;
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds /*
2961da177e4SLinus Torvalds  *  Isolate (assign uniqued CSN) to all ISA PnP devices.
2971da177e4SLinus Torvalds  */
2981da177e4SLinus Torvalds static int __init isapnp_isolate(void)
2991da177e4SLinus Torvalds {
3001da177e4SLinus Torvalds 	unsigned char checksum = 0x6a;
3011da177e4SLinus Torvalds 	unsigned char chksum = 0x00;
3021da177e4SLinus Torvalds 	unsigned char bit = 0x00;
3031da177e4SLinus Torvalds 	int data;
3041da177e4SLinus Torvalds 	int csn = 0;
3051da177e4SLinus Torvalds 	int i;
3061da177e4SLinus Torvalds 	int iteration = 1;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	isapnp_rdp = 0x213;
3091da177e4SLinus Torvalds 	if (isapnp_isolate_rdp_select() < 0)
3101da177e4SLinus Torvalds 		return -1;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	while (1) {
3131da177e4SLinus Torvalds 		for (i = 1; i <= 64; i++) {
3141da177e4SLinus Torvalds 			data = read_data() << 8;
3151da177e4SLinus Torvalds 			udelay(250);
3161da177e4SLinus Torvalds 			data = data | read_data();
3171da177e4SLinus Torvalds 			udelay(250);
3181da177e4SLinus Torvalds 			if (data == 0x55aa)
3191da177e4SLinus Torvalds 				bit = 0x01;
3209dd78466SBjorn Helgaas 			checksum =
3219dd78466SBjorn Helgaas 			    ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7)
3229dd78466SBjorn Helgaas 			    | (checksum >> 1);
3231da177e4SLinus Torvalds 			bit = 0x00;
3241da177e4SLinus Torvalds 		}
3251da177e4SLinus Torvalds 		for (i = 65; i <= 72; i++) {
3261da177e4SLinus Torvalds 			data = read_data() << 8;
3271da177e4SLinus Torvalds 			udelay(250);
3281da177e4SLinus Torvalds 			data = data | read_data();
3291da177e4SLinus Torvalds 			udelay(250);
3301da177e4SLinus Torvalds 			if (data == 0x55aa)
3311da177e4SLinus Torvalds 				chksum |= (1 << (i - 65));
3321da177e4SLinus Torvalds 		}
3331da177e4SLinus Torvalds 		if (checksum != 0x00 && checksum == chksum) {
3341da177e4SLinus Torvalds 			csn++;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 			isapnp_write_byte(0x06, csn);
3371da177e4SLinus Torvalds 			udelay(250);
3381da177e4SLinus Torvalds 			iteration++;
3391da177e4SLinus Torvalds 			isapnp_wake(0x00);
3401da177e4SLinus Torvalds 			isapnp_set_rdp();
3411da177e4SLinus Torvalds 			udelay(1000);
3421da177e4SLinus Torvalds 			write_address(0x01);
3431da177e4SLinus Torvalds 			udelay(1000);
3441da177e4SLinus Torvalds 			goto __next;
3451da177e4SLinus Torvalds 		}
3461da177e4SLinus Torvalds 		if (iteration == 1) {
3471da177e4SLinus Torvalds 			isapnp_rdp += RDP_STEP;
3481da177e4SLinus Torvalds 			if (isapnp_isolate_rdp_select() < 0)
3491da177e4SLinus Torvalds 				return -1;
3501da177e4SLinus Torvalds 		} else if (iteration > 1) {
3511da177e4SLinus Torvalds 			break;
3521da177e4SLinus Torvalds 		}
3531da177e4SLinus Torvalds __next:
3541da177e4SLinus Torvalds 		if (csn == 255)
3551da177e4SLinus Torvalds 			break;
3561da177e4SLinus Torvalds 		checksum = 0x6a;
3571da177e4SLinus Torvalds 		chksum = 0x00;
3581da177e4SLinus Torvalds 		bit = 0x00;
3591da177e4SLinus Torvalds 	}
3601da177e4SLinus Torvalds 	isapnp_wait();
3611da177e4SLinus Torvalds 	isapnp_csn_count = csn;
3621da177e4SLinus Torvalds 	return csn;
3631da177e4SLinus Torvalds }
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds /*
3661da177e4SLinus Torvalds  *  Read one tag from stream.
3671da177e4SLinus Torvalds  */
3681da177e4SLinus Torvalds static int __init isapnp_read_tag(unsigned char *type, unsigned short *size)
3691da177e4SLinus Torvalds {
3701da177e4SLinus Torvalds 	unsigned char tag, tmp[2];
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	isapnp_peek(&tag, 1);
3731da177e4SLinus Torvalds 	if (tag == 0)		/* invalid tag */
3741da177e4SLinus Torvalds 		return -1;
3751da177e4SLinus Torvalds 	if (tag & 0x80) {	/* large item */
3761da177e4SLinus Torvalds 		*type = tag;
3771da177e4SLinus Torvalds 		isapnp_peek(tmp, 2);
3781da177e4SLinus Torvalds 		*size = (tmp[1] << 8) | tmp[0];
3791da177e4SLinus Torvalds 	} else {
3801da177e4SLinus Torvalds 		*type = (tag >> 3) & 0x0f;
3811da177e4SLinus Torvalds 		*size = tag & 0x07;
3821da177e4SLinus Torvalds 	}
3831da177e4SLinus Torvalds #if 0
3849dd78466SBjorn Helgaas 	printk(KERN_DEBUG "tag = 0x%x, type = 0x%x, size = %i\n", tag, *type,
3859dd78466SBjorn Helgaas 	       *size);
3861da177e4SLinus Torvalds #endif
3871da177e4SLinus Torvalds 	if (*type == 0xff && *size == 0xffff)	/* probably invalid data */
3881da177e4SLinus Torvalds 		return -1;
3891da177e4SLinus Torvalds 	return 0;
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds /*
3931da177e4SLinus Torvalds  *  Skip specified number of bytes from stream.
3941da177e4SLinus Torvalds  */
3951da177e4SLinus Torvalds static void __init isapnp_skip_bytes(int count)
3961da177e4SLinus Torvalds {
3971da177e4SLinus Torvalds 	isapnp_peek(NULL, count);
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds /*
4011da177e4SLinus Torvalds  *  Parse EISA id.
4021da177e4SLinus Torvalds  */
4039dd78466SBjorn Helgaas static void isapnp_parse_id(struct pnp_dev *dev, unsigned short vendor,
4049dd78466SBjorn Helgaas 			    unsigned short device)
4051da177e4SLinus Torvalds {
406772defc6SBjorn Helgaas 	char id[8];
40707d4e9afSBjorn Helgaas 
408772defc6SBjorn Helgaas 	sprintf(id, "%c%c%c%x%x%x%x",
4091da177e4SLinus Torvalds 		'A' + ((vendor >> 2) & 0x3f) - 1,
4101da177e4SLinus Torvalds 		'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
4111da177e4SLinus Torvalds 		'A' + ((vendor >> 8) & 0x1f) - 1,
4121da177e4SLinus Torvalds 		(device >> 4) & 0x0f,
4139dd78466SBjorn Helgaas 		device & 0x0f, (device >> 12) & 0x0f, (device >> 8) & 0x0f);
414772defc6SBjorn Helgaas 
415772defc6SBjorn Helgaas 	pnp_add_id(dev, id);
4161da177e4SLinus Torvalds }
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds /*
4191da177e4SLinus Torvalds  *  Parse logical device tag.
4201da177e4SLinus Torvalds  */
4219dd78466SBjorn Helgaas static struct pnp_dev *__init isapnp_parse_device(struct pnp_card *card,
4229dd78466SBjorn Helgaas 						  int size, int number)
4231da177e4SLinus Torvalds {
4241da177e4SLinus Torvalds 	unsigned char tmp[6];
4251da177e4SLinus Torvalds 	struct pnp_dev *dev;
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	isapnp_peek(tmp, size);
428cd861280SRobert P. J. Day 	dev = kzalloc(sizeof(struct pnp_dev), GFP_KERNEL);
4291da177e4SLinus Torvalds 	if (!dev)
4301da177e4SLinus Torvalds 		return NULL;
4311da177e4SLinus Torvalds 	dev->number = number;
4321da177e4SLinus Torvalds 	isapnp_parse_id(dev, (tmp[1] << 8) | tmp[0], (tmp[3] << 8) | tmp[2]);
4331da177e4SLinus Torvalds 	dev->regs = tmp[4];
4341da177e4SLinus Torvalds 	dev->card = card;
4351da177e4SLinus Torvalds 	if (size > 5)
4361da177e4SLinus Torvalds 		dev->regs |= tmp[5] << 8;
4371da177e4SLinus Torvalds 	dev->protocol = &isapnp_protocol;
4381da177e4SLinus Torvalds 	dev->capabilities |= PNP_CONFIGURABLE;
4391da177e4SLinus Torvalds 	dev->capabilities |= PNP_READ;
4401da177e4SLinus Torvalds 	dev->capabilities |= PNP_WRITE;
4411da177e4SLinus Torvalds 	dev->capabilities |= PNP_DISABLE;
4421da177e4SLinus Torvalds 	pnp_init_resource_table(&dev->res);
4431da177e4SLinus Torvalds 	return dev;
4441da177e4SLinus Torvalds }
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds /*
4471da177e4SLinus Torvalds  *  Add IRQ resource to resources list.
4481da177e4SLinus Torvalds  */
4491da177e4SLinus Torvalds static void __init isapnp_parse_irq_resource(struct pnp_option *option,
4501da177e4SLinus Torvalds 					     int size)
4511da177e4SLinus Torvalds {
4521da177e4SLinus Torvalds 	unsigned char tmp[3];
4531da177e4SLinus Torvalds 	struct pnp_irq *irq;
4541da177e4SLinus Torvalds 	unsigned long bits;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	isapnp_peek(tmp, size);
457cd861280SRobert P. J. Day 	irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
4581da177e4SLinus Torvalds 	if (!irq)
4591da177e4SLinus Torvalds 		return;
4601da177e4SLinus Torvalds 	bits = (tmp[1] << 8) | tmp[0];
4611da177e4SLinus Torvalds 	bitmap_copy(irq->map, &bits, 16);
4621da177e4SLinus Torvalds 	if (size > 2)
4631da177e4SLinus Torvalds 		irq->flags = tmp[2];
4641da177e4SLinus Torvalds 	else
4651da177e4SLinus Torvalds 		irq->flags = IORESOURCE_IRQ_HIGHEDGE;
4661da177e4SLinus Torvalds 	pnp_register_irq_resource(option, irq);
4671da177e4SLinus Torvalds }
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds /*
4701da177e4SLinus Torvalds  *  Add DMA resource to resources list.
4711da177e4SLinus Torvalds  */
4721da177e4SLinus Torvalds static void __init isapnp_parse_dma_resource(struct pnp_option *option,
4731da177e4SLinus Torvalds 					     int size)
4741da177e4SLinus Torvalds {
4751da177e4SLinus Torvalds 	unsigned char tmp[2];
4761da177e4SLinus Torvalds 	struct pnp_dma *dma;
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	isapnp_peek(tmp, size);
479cd861280SRobert P. J. Day 	dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
4801da177e4SLinus Torvalds 	if (!dma)
4811da177e4SLinus Torvalds 		return;
4821da177e4SLinus Torvalds 	dma->map = tmp[0];
4831da177e4SLinus Torvalds 	dma->flags = tmp[1];
4841da177e4SLinus Torvalds 	pnp_register_dma_resource(option, dma);
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds /*
4881da177e4SLinus Torvalds  *  Add port resource to resources list.
4891da177e4SLinus Torvalds  */
4901da177e4SLinus Torvalds static void __init isapnp_parse_port_resource(struct pnp_option *option,
4911da177e4SLinus Torvalds 					      int size)
4921da177e4SLinus Torvalds {
4931da177e4SLinus Torvalds 	unsigned char tmp[7];
4941da177e4SLinus Torvalds 	struct pnp_port *port;
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	isapnp_peek(tmp, size);
497cd861280SRobert P. J. Day 	port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
4981da177e4SLinus Torvalds 	if (!port)
4991da177e4SLinus Torvalds 		return;
5001da177e4SLinus Torvalds 	port->min = (tmp[2] << 8) | tmp[1];
5011da177e4SLinus Torvalds 	port->max = (tmp[4] << 8) | tmp[3];
5021da177e4SLinus Torvalds 	port->align = tmp[5];
5031da177e4SLinus Torvalds 	port->size = tmp[6];
5041da177e4SLinus Torvalds 	port->flags = tmp[0] ? PNP_PORT_FLAG_16BITADDR : 0;
5051da177e4SLinus Torvalds 	pnp_register_port_resource(option, port);
5061da177e4SLinus Torvalds }
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds /*
5091da177e4SLinus Torvalds  *  Add fixed port resource to resources list.
5101da177e4SLinus Torvalds  */
5111da177e4SLinus Torvalds static void __init isapnp_parse_fixed_port_resource(struct pnp_option *option,
5121da177e4SLinus Torvalds 						    int size)
5131da177e4SLinus Torvalds {
5141da177e4SLinus Torvalds 	unsigned char tmp[3];
5151da177e4SLinus Torvalds 	struct pnp_port *port;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	isapnp_peek(tmp, size);
518cd861280SRobert P. J. Day 	port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
5191da177e4SLinus Torvalds 	if (!port)
5201da177e4SLinus Torvalds 		return;
5211da177e4SLinus Torvalds 	port->min = port->max = (tmp[1] << 8) | tmp[0];
5221da177e4SLinus Torvalds 	port->size = tmp[2];
5231da177e4SLinus Torvalds 	port->align = 0;
5241da177e4SLinus Torvalds 	port->flags = PNP_PORT_FLAG_FIXED;
5251da177e4SLinus Torvalds 	pnp_register_port_resource(option, port);
5261da177e4SLinus Torvalds }
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds /*
5291da177e4SLinus Torvalds  *  Add memory resource to resources list.
5301da177e4SLinus Torvalds  */
5311da177e4SLinus Torvalds static void __init isapnp_parse_mem_resource(struct pnp_option *option,
5321da177e4SLinus Torvalds 					     int size)
5331da177e4SLinus Torvalds {
5341da177e4SLinus Torvalds 	unsigned char tmp[9];
5351da177e4SLinus Torvalds 	struct pnp_mem *mem;
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 	isapnp_peek(tmp, size);
538cd861280SRobert P. J. Day 	mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
5391da177e4SLinus Torvalds 	if (!mem)
5401da177e4SLinus Torvalds 		return;
5411da177e4SLinus Torvalds 	mem->min = ((tmp[2] << 8) | tmp[1]) << 8;
5421da177e4SLinus Torvalds 	mem->max = ((tmp[4] << 8) | tmp[3]) << 8;
5431da177e4SLinus Torvalds 	mem->align = (tmp[6] << 8) | tmp[5];
5441da177e4SLinus Torvalds 	mem->size = ((tmp[8] << 8) | tmp[7]) << 8;
5451da177e4SLinus Torvalds 	mem->flags = tmp[0];
5461da177e4SLinus Torvalds 	pnp_register_mem_resource(option, mem);
5471da177e4SLinus Torvalds }
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds /*
5501da177e4SLinus Torvalds  *  Add 32-bit memory resource to resources list.
5511da177e4SLinus Torvalds  */
5521da177e4SLinus Torvalds static void __init isapnp_parse_mem32_resource(struct pnp_option *option,
5531da177e4SLinus Torvalds 					       int size)
5541da177e4SLinus Torvalds {
5551da177e4SLinus Torvalds 	unsigned char tmp[17];
5561da177e4SLinus Torvalds 	struct pnp_mem *mem;
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 	isapnp_peek(tmp, size);
559cd861280SRobert P. J. Day 	mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
5601da177e4SLinus Torvalds 	if (!mem)
5611da177e4SLinus Torvalds 		return;
5621da177e4SLinus Torvalds 	mem->min = (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
5631da177e4SLinus Torvalds 	mem->max = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
5649dd78466SBjorn Helgaas 	mem->align =
5659dd78466SBjorn Helgaas 	    (tmp[12] << 24) | (tmp[11] << 16) | (tmp[10] << 8) | tmp[9];
5669dd78466SBjorn Helgaas 	mem->size =
5679dd78466SBjorn Helgaas 	    (tmp[16] << 24) | (tmp[15] << 16) | (tmp[14] << 8) | tmp[13];
5681da177e4SLinus Torvalds 	mem->flags = tmp[0];
5691da177e4SLinus Torvalds 	pnp_register_mem_resource(option, mem);
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds /*
5731da177e4SLinus Torvalds  *  Add 32-bit fixed memory resource to resources list.
5741da177e4SLinus Torvalds  */
5751da177e4SLinus Torvalds static void __init isapnp_parse_fixed_mem32_resource(struct pnp_option *option,
5761da177e4SLinus Torvalds 						     int size)
5771da177e4SLinus Torvalds {
5781da177e4SLinus Torvalds 	unsigned char tmp[9];
5791da177e4SLinus Torvalds 	struct pnp_mem *mem;
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	isapnp_peek(tmp, size);
582cd861280SRobert P. J. Day 	mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
5831da177e4SLinus Torvalds 	if (!mem)
5841da177e4SLinus Torvalds 		return;
5859dd78466SBjorn Helgaas 	mem->min = mem->max =
5869dd78466SBjorn Helgaas 	    (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
5871da177e4SLinus Torvalds 	mem->size = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
5881da177e4SLinus Torvalds 	mem->align = 0;
5891da177e4SLinus Torvalds 	mem->flags = tmp[0];
5901da177e4SLinus Torvalds 	pnp_register_mem_resource(option, mem);
5911da177e4SLinus Torvalds }
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds /*
5941da177e4SLinus Torvalds  *  Parse card name for ISA PnP device.
5951da177e4SLinus Torvalds  */
5961da177e4SLinus Torvalds static void __init
5971da177e4SLinus Torvalds isapnp_parse_name(char *name, unsigned int name_max, unsigned short *size)
5981da177e4SLinus Torvalds {
5991da177e4SLinus Torvalds 	if (name[0] == '\0') {
6009dd78466SBjorn Helgaas 		unsigned short size1 =
6019dd78466SBjorn Helgaas 		    *size >= name_max ? (name_max - 1) : *size;
6021da177e4SLinus Torvalds 		isapnp_peek(name, size1);
6031da177e4SLinus Torvalds 		name[size1] = '\0';
6041da177e4SLinus Torvalds 		*size -= size1;
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 		/* clean whitespace from end of string */
6071da177e4SLinus Torvalds 		while (size1 > 0 && name[--size1] == ' ')
6081da177e4SLinus Torvalds 			name[size1] = '\0';
6091da177e4SLinus Torvalds 	}
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds /*
6131da177e4SLinus Torvalds  *  Parse resource map for logical device.
6141da177e4SLinus Torvalds  */
6151da177e4SLinus Torvalds static int __init isapnp_create_device(struct pnp_card *card,
6161da177e4SLinus Torvalds 				       unsigned short size)
6171da177e4SLinus Torvalds {
6181da177e4SLinus Torvalds 	int number = 0, skip = 0, priority = 0, compat = 0;
6191da177e4SLinus Torvalds 	unsigned char type, tmp[17];
6201da177e4SLinus Torvalds 	struct pnp_option *option;
6211da177e4SLinus Torvalds 	struct pnp_dev *dev;
62207d4e9afSBjorn Helgaas 
6231da177e4SLinus Torvalds 	if ((dev = isapnp_parse_device(card, size, number++)) == NULL)
6241da177e4SLinus Torvalds 		return 1;
6251da177e4SLinus Torvalds 	option = pnp_register_independent_option(dev);
6261da177e4SLinus Torvalds 	if (!option) {
6271da177e4SLinus Torvalds 		kfree(dev);
6281da177e4SLinus Torvalds 		return 1;
6291da177e4SLinus Torvalds 	}
6301da177e4SLinus Torvalds 	pnp_add_card_device(card, dev);
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 	while (1) {
6331da177e4SLinus Torvalds 		if (isapnp_read_tag(&type, &size) < 0)
6341da177e4SLinus Torvalds 			return 1;
6351da177e4SLinus Torvalds 		if (skip && type != _STAG_LOGDEVID && type != _STAG_END)
6361da177e4SLinus Torvalds 			goto __skip;
6371da177e4SLinus Torvalds 		switch (type) {
6381da177e4SLinus Torvalds 		case _STAG_LOGDEVID:
6391da177e4SLinus Torvalds 			if (size >= 5 && size <= 6) {
6409dd78466SBjorn Helgaas 				if ((dev =
6419dd78466SBjorn Helgaas 				     isapnp_parse_device(card, size,
6429dd78466SBjorn Helgaas 							 number++)) == NULL)
6431da177e4SLinus Torvalds 					return 1;
6441da177e4SLinus Torvalds 				size = 0;
6451da177e4SLinus Torvalds 				skip = 0;
6461da177e4SLinus Torvalds 				option = pnp_register_independent_option(dev);
647656bde57SJesper Juhl 				if (!option) {
648656bde57SJesper Juhl 					kfree(dev);
6491da177e4SLinus Torvalds 					return 1;
650656bde57SJesper Juhl 				}
6511da177e4SLinus Torvalds 				pnp_add_card_device(card, dev);
6521da177e4SLinus Torvalds 			} else {
6531da177e4SLinus Torvalds 				skip = 1;
6541da177e4SLinus Torvalds 			}
6551da177e4SLinus Torvalds 			priority = 0;
6561da177e4SLinus Torvalds 			compat = 0;
6571da177e4SLinus Torvalds 			break;
6581da177e4SLinus Torvalds 		case _STAG_COMPATDEVID:
6591da177e4SLinus Torvalds 			if (size == 4 && compat < DEVICE_COUNT_COMPATIBLE) {
6601da177e4SLinus Torvalds 				isapnp_peek(tmp, 4);
6619dd78466SBjorn Helgaas 				isapnp_parse_id(dev, (tmp[1] << 8) | tmp[0],
6629dd78466SBjorn Helgaas 						(tmp[3] << 8) | tmp[2]);
6631da177e4SLinus Torvalds 				compat++;
6641da177e4SLinus Torvalds 				size = 0;
6651da177e4SLinus Torvalds 			}
6661da177e4SLinus Torvalds 			break;
6671da177e4SLinus Torvalds 		case _STAG_IRQ:
6681da177e4SLinus Torvalds 			if (size < 2 || size > 3)
6691da177e4SLinus Torvalds 				goto __skip;
6701da177e4SLinus Torvalds 			isapnp_parse_irq_resource(option, size);
6711da177e4SLinus Torvalds 			size = 0;
6721da177e4SLinus Torvalds 			break;
6731da177e4SLinus Torvalds 		case _STAG_DMA:
6741da177e4SLinus Torvalds 			if (size != 2)
6751da177e4SLinus Torvalds 				goto __skip;
6761da177e4SLinus Torvalds 			isapnp_parse_dma_resource(option, size);
6771da177e4SLinus Torvalds 			size = 0;
6781da177e4SLinus Torvalds 			break;
6791da177e4SLinus Torvalds 		case _STAG_STARTDEP:
6801da177e4SLinus Torvalds 			if (size > 1)
6811da177e4SLinus Torvalds 				goto __skip;
6821da177e4SLinus Torvalds 			priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
6831da177e4SLinus Torvalds 			if (size > 0) {
6841da177e4SLinus Torvalds 				isapnp_peek(tmp, size);
6851da177e4SLinus Torvalds 				priority = 0x100 | tmp[0];
6861da177e4SLinus Torvalds 				size = 0;
6871da177e4SLinus Torvalds 			}
6881da177e4SLinus Torvalds 			option = pnp_register_dependent_option(dev, priority);
6891da177e4SLinus Torvalds 			if (!option)
6901da177e4SLinus Torvalds 				return 1;
6911da177e4SLinus Torvalds 			break;
6921da177e4SLinus Torvalds 		case _STAG_ENDDEP:
6931da177e4SLinus Torvalds 			if (size != 0)
6941da177e4SLinus Torvalds 				goto __skip;
6951da177e4SLinus Torvalds 			priority = 0;
6961da177e4SLinus Torvalds 			break;
6971da177e4SLinus Torvalds 		case _STAG_IOPORT:
6981da177e4SLinus Torvalds 			if (size != 7)
6991da177e4SLinus Torvalds 				goto __skip;
7001da177e4SLinus Torvalds 			isapnp_parse_port_resource(option, size);
7011da177e4SLinus Torvalds 			size = 0;
7021da177e4SLinus Torvalds 			break;
7031da177e4SLinus Torvalds 		case _STAG_FIXEDIO:
7041da177e4SLinus Torvalds 			if (size != 3)
7051da177e4SLinus Torvalds 				goto __skip;
7061da177e4SLinus Torvalds 			isapnp_parse_fixed_port_resource(option, size);
7071da177e4SLinus Torvalds 			size = 0;
7081da177e4SLinus Torvalds 			break;
7091da177e4SLinus Torvalds 		case _STAG_VENDOR:
7101da177e4SLinus Torvalds 			break;
7111da177e4SLinus Torvalds 		case _LTAG_MEMRANGE:
7121da177e4SLinus Torvalds 			if (size != 9)
7131da177e4SLinus Torvalds 				goto __skip;
7141da177e4SLinus Torvalds 			isapnp_parse_mem_resource(option, size);
7151da177e4SLinus Torvalds 			size = 0;
7161da177e4SLinus Torvalds 			break;
7171da177e4SLinus Torvalds 		case _LTAG_ANSISTR:
7181da177e4SLinus Torvalds 			isapnp_parse_name(dev->name, sizeof(dev->name), &size);
7191da177e4SLinus Torvalds 			break;
7201da177e4SLinus Torvalds 		case _LTAG_UNICODESTR:
7211da177e4SLinus Torvalds 			/* silently ignore */
7221da177e4SLinus Torvalds 			/* who use unicode for hardware identification? */
7231da177e4SLinus Torvalds 			break;
7241da177e4SLinus Torvalds 		case _LTAG_VENDOR:
7251da177e4SLinus Torvalds 			break;
7261da177e4SLinus Torvalds 		case _LTAG_MEM32RANGE:
7271da177e4SLinus Torvalds 			if (size != 17)
7281da177e4SLinus Torvalds 				goto __skip;
7291da177e4SLinus Torvalds 			isapnp_parse_mem32_resource(option, size);
7301da177e4SLinus Torvalds 			size = 0;
7311da177e4SLinus Torvalds 			break;
7321da177e4SLinus Torvalds 		case _LTAG_FIXEDMEM32RANGE:
7331da177e4SLinus Torvalds 			if (size != 9)
7341da177e4SLinus Torvalds 				goto __skip;
7351da177e4SLinus Torvalds 			isapnp_parse_fixed_mem32_resource(option, size);
7361da177e4SLinus Torvalds 			size = 0;
7371da177e4SLinus Torvalds 			break;
7381da177e4SLinus Torvalds 		case _STAG_END:
7391da177e4SLinus Torvalds 			if (size > 0)
7401da177e4SLinus Torvalds 				isapnp_skip_bytes(size);
7411da177e4SLinus Torvalds 			return 1;
7421da177e4SLinus Torvalds 		default:
7439dd78466SBjorn Helgaas 			printk(KERN_ERR
7449dd78466SBjorn Helgaas 			       "isapnp: unexpected or unknown tag type 0x%x for logical device %i (device %i), ignored\n",
7459dd78466SBjorn Helgaas 			       type, dev->number, card->number);
7461da177e4SLinus Torvalds 		}
7471da177e4SLinus Torvalds __skip:
7481da177e4SLinus Torvalds 		if (size > 0)
7491da177e4SLinus Torvalds 			isapnp_skip_bytes(size);
7501da177e4SLinus Torvalds 	}
7511da177e4SLinus Torvalds 	return 0;
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds /*
7551da177e4SLinus Torvalds  *  Parse resource map for ISA PnP card.
7561da177e4SLinus Torvalds  */
7571da177e4SLinus Torvalds static void __init isapnp_parse_resource_map(struct pnp_card *card)
7581da177e4SLinus Torvalds {
7591da177e4SLinus Torvalds 	unsigned char type, tmp[17];
7601da177e4SLinus Torvalds 	unsigned short size;
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	while (1) {
7631da177e4SLinus Torvalds 		if (isapnp_read_tag(&type, &size) < 0)
7641da177e4SLinus Torvalds 			return;
7651da177e4SLinus Torvalds 		switch (type) {
7661da177e4SLinus Torvalds 		case _STAG_PNPVERNO:
7671da177e4SLinus Torvalds 			if (size != 2)
7681da177e4SLinus Torvalds 				goto __skip;
7691da177e4SLinus Torvalds 			isapnp_peek(tmp, 2);
7701da177e4SLinus Torvalds 			card->pnpver = tmp[0];
7711da177e4SLinus Torvalds 			card->productver = tmp[1];
7721da177e4SLinus Torvalds 			size = 0;
7731da177e4SLinus Torvalds 			break;
7741da177e4SLinus Torvalds 		case _STAG_LOGDEVID:
7751da177e4SLinus Torvalds 			if (size >= 5 && size <= 6) {
7761da177e4SLinus Torvalds 				if (isapnp_create_device(card, size) == 1)
7771da177e4SLinus Torvalds 					return;
7781da177e4SLinus Torvalds 				size = 0;
7791da177e4SLinus Torvalds 			}
7801da177e4SLinus Torvalds 			break;
7811da177e4SLinus Torvalds 		case _STAG_VENDOR:
7821da177e4SLinus Torvalds 			break;
7831da177e4SLinus Torvalds 		case _LTAG_ANSISTR:
7849dd78466SBjorn Helgaas 			isapnp_parse_name(card->name, sizeof(card->name),
7859dd78466SBjorn Helgaas 					  &size);
7861da177e4SLinus Torvalds 			break;
7871da177e4SLinus Torvalds 		case _LTAG_UNICODESTR:
7881da177e4SLinus Torvalds 			/* silently ignore */
7891da177e4SLinus Torvalds 			/* who use unicode for hardware identification? */
7901da177e4SLinus Torvalds 			break;
7911da177e4SLinus Torvalds 		case _LTAG_VENDOR:
7921da177e4SLinus Torvalds 			break;
7931da177e4SLinus Torvalds 		case _STAG_END:
7941da177e4SLinus Torvalds 			if (size > 0)
7951da177e4SLinus Torvalds 				isapnp_skip_bytes(size);
7961da177e4SLinus Torvalds 			return;
7971da177e4SLinus Torvalds 		default:
7989dd78466SBjorn Helgaas 			printk(KERN_ERR
7999dd78466SBjorn Helgaas 			       "isapnp: unexpected or unknown tag type 0x%x for device %i, ignored\n",
8009dd78466SBjorn Helgaas 			       type, card->number);
8011da177e4SLinus Torvalds 		}
8021da177e4SLinus Torvalds __skip:
8031da177e4SLinus Torvalds 		if (size > 0)
8041da177e4SLinus Torvalds 			isapnp_skip_bytes(size);
8051da177e4SLinus Torvalds 	}
8061da177e4SLinus Torvalds }
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds /*
8091da177e4SLinus Torvalds  *  Compute ISA PnP checksum for first eight bytes.
8101da177e4SLinus Torvalds  */
8111da177e4SLinus Torvalds static unsigned char __init isapnp_checksum(unsigned char *data)
8121da177e4SLinus Torvalds {
8131da177e4SLinus Torvalds 	int i, j;
8141da177e4SLinus Torvalds 	unsigned char checksum = 0x6a, bit, b;
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 	for (i = 0; i < 8; i++) {
8171da177e4SLinus Torvalds 		b = data[i];
8181da177e4SLinus Torvalds 		for (j = 0; j < 8; j++) {
8191da177e4SLinus Torvalds 			bit = 0;
8201da177e4SLinus Torvalds 			if (b & (1 << j))
8211da177e4SLinus Torvalds 				bit = 1;
8229dd78466SBjorn Helgaas 			checksum =
8239dd78466SBjorn Helgaas 			    ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7)
8249dd78466SBjorn Helgaas 			    | (checksum >> 1);
8251da177e4SLinus Torvalds 		}
8261da177e4SLinus Torvalds 	}
8271da177e4SLinus Torvalds 	return checksum;
8281da177e4SLinus Torvalds }
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds /*
8311da177e4SLinus Torvalds  *  Parse EISA id for ISA PnP card.
8321da177e4SLinus Torvalds  */
8339dd78466SBjorn Helgaas static void isapnp_parse_card_id(struct pnp_card *card, unsigned short vendor,
8349dd78466SBjorn Helgaas 				 unsigned short device)
8351da177e4SLinus Torvalds {
836cd861280SRobert P. J. Day 	struct pnp_id *id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
83707d4e9afSBjorn Helgaas 
8381da177e4SLinus Torvalds 	if (!id)
8391da177e4SLinus Torvalds 		return;
8401da177e4SLinus Torvalds 	sprintf(id->id, "%c%c%c%x%x%x%x",
8411da177e4SLinus Torvalds 		'A' + ((vendor >> 2) & 0x3f) - 1,
8421da177e4SLinus Torvalds 		'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
8431da177e4SLinus Torvalds 		'A' + ((vendor >> 8) & 0x1f) - 1,
8441da177e4SLinus Torvalds 		(device >> 4) & 0x0f,
8459dd78466SBjorn Helgaas 		device & 0x0f, (device >> 12) & 0x0f, (device >> 8) & 0x0f);
8461da177e4SLinus Torvalds 	pnp_add_card_id(id, card);
8471da177e4SLinus Torvalds }
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds /*
8501da177e4SLinus Torvalds  *  Build device list for all present ISA PnP devices.
8511da177e4SLinus Torvalds  */
8521da177e4SLinus Torvalds static int __init isapnp_build_device_list(void)
8531da177e4SLinus Torvalds {
8541da177e4SLinus Torvalds 	int csn;
8551da177e4SLinus Torvalds 	unsigned char header[9], checksum;
8561da177e4SLinus Torvalds 	struct pnp_card *card;
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 	isapnp_wait();
8591da177e4SLinus Torvalds 	isapnp_key();
8601da177e4SLinus Torvalds 	for (csn = 1; csn <= isapnp_csn_count; csn++) {
8611da177e4SLinus Torvalds 		isapnp_wake(csn);
8621da177e4SLinus Torvalds 		isapnp_peek(header, 9);
8631da177e4SLinus Torvalds 		checksum = isapnp_checksum(header);
8641da177e4SLinus Torvalds #if 0
8659dd78466SBjorn Helgaas 		printk(KERN_DEBUG
8669dd78466SBjorn Helgaas 		       "vendor: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
8679dd78466SBjorn Helgaas 		       header[0], header[1], header[2], header[3], header[4],
8689dd78466SBjorn Helgaas 		       header[5], header[6], header[7], header[8]);
8691da177e4SLinus Torvalds 		printk(KERN_DEBUG "checksum = 0x%x\n", checksum);
8701da177e4SLinus Torvalds #endif
8719dd78466SBjorn Helgaas 		if ((card =
8729dd78466SBjorn Helgaas 		     kzalloc(sizeof(struct pnp_card), GFP_KERNEL)) == NULL)
8731da177e4SLinus Torvalds 			continue;
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds 		card->number = csn;
8761da177e4SLinus Torvalds 		INIT_LIST_HEAD(&card->devices);
8779dd78466SBjorn Helgaas 		isapnp_parse_card_id(card, (header[1] << 8) | header[0],
8789dd78466SBjorn Helgaas 				     (header[3] << 8) | header[2]);
8799dd78466SBjorn Helgaas 		card->serial =
8809dd78466SBjorn Helgaas 		    (header[7] << 24) | (header[6] << 16) | (header[5] << 8) |
8819dd78466SBjorn Helgaas 		    header[4];
8821da177e4SLinus Torvalds 		isapnp_checksum_value = 0x00;
8831da177e4SLinus Torvalds 		isapnp_parse_resource_map(card);
8841da177e4SLinus Torvalds 		if (isapnp_checksum_value != 0x00)
8859dd78466SBjorn Helgaas 			printk(KERN_ERR
8869dd78466SBjorn Helgaas 			       "isapnp: checksum for device %i is not valid (0x%x)\n",
8879dd78466SBjorn Helgaas 			       csn, isapnp_checksum_value);
8881da177e4SLinus Torvalds 		card->checksum = isapnp_checksum_value;
8891da177e4SLinus Torvalds 		card->protocol = &isapnp_protocol;
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 		pnp_add_card(card);
8921da177e4SLinus Torvalds 	}
8931da177e4SLinus Torvalds 	isapnp_wait();
8941da177e4SLinus Torvalds 	return 0;
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds /*
8981da177e4SLinus Torvalds  *  Basic configuration routines.
8991da177e4SLinus Torvalds  */
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds int isapnp_present(void)
9021da177e4SLinus Torvalds {
9031da177e4SLinus Torvalds 	struct pnp_card *card;
90407d4e9afSBjorn Helgaas 
9051da177e4SLinus Torvalds 	pnp_for_each_card(card) {
9061da177e4SLinus Torvalds 		if (card->protocol == &isapnp_protocol)
9071da177e4SLinus Torvalds 			return 1;
9081da177e4SLinus Torvalds 	}
9091da177e4SLinus Torvalds 	return 0;
9101da177e4SLinus Torvalds }
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds int isapnp_cfg_begin(int csn, int logdev)
9131da177e4SLinus Torvalds {
9141da177e4SLinus Torvalds 	if (csn < 1 || csn > isapnp_csn_count || logdev > 10)
9151da177e4SLinus Torvalds 		return -EINVAL;
91614cc3e2bSIngo Molnar 	mutex_lock(&isapnp_cfg_mutex);
9171da177e4SLinus Torvalds 	isapnp_wait();
9181da177e4SLinus Torvalds 	isapnp_key();
9191da177e4SLinus Torvalds 	isapnp_wake(csn);
9201da177e4SLinus Torvalds #if 0
9211da177e4SLinus Torvalds 	/* to avoid malfunction when the isapnptools package is used */
9221da177e4SLinus Torvalds 	/* we must set RDP to our value again */
9231da177e4SLinus Torvalds 	/* it is possible to set RDP only in the isolation phase */
9241da177e4SLinus Torvalds 	/*   Jens Thoms Toerring <Jens.Toerring@physik.fu-berlin.de> */
9251da177e4SLinus Torvalds 	isapnp_write_byte(0x02, 0x04);	/* clear CSN of card */
9261da177e4SLinus Torvalds 	mdelay(2);		/* is this necessary? */
9271da177e4SLinus Torvalds 	isapnp_wake(csn);	/* bring card into sleep state */
9281da177e4SLinus Torvalds 	isapnp_wake(0);		/* bring card into isolation state */
9291da177e4SLinus Torvalds 	isapnp_set_rdp();	/* reset the RDP port */
9301da177e4SLinus Torvalds 	udelay(1000);		/* delay 1000us */
9311da177e4SLinus Torvalds 	isapnp_write_byte(0x06, csn);	/* reset CSN to previous value */
9321da177e4SLinus Torvalds 	udelay(250);		/* is this necessary? */
9331da177e4SLinus Torvalds #endif
9341da177e4SLinus Torvalds 	if (logdev >= 0)
9351da177e4SLinus Torvalds 		isapnp_device(logdev);
9361da177e4SLinus Torvalds 	return 0;
9371da177e4SLinus Torvalds }
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds int isapnp_cfg_end(void)
9401da177e4SLinus Torvalds {
9411da177e4SLinus Torvalds 	isapnp_wait();
94214cc3e2bSIngo Molnar 	mutex_unlock(&isapnp_cfg_mutex);
9431da177e4SLinus Torvalds 	return 0;
9441da177e4SLinus Torvalds }
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds /*
94707d4e9afSBjorn Helgaas  *  Initialization.
9481da177e4SLinus Torvalds  */
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds EXPORT_SYMBOL(isapnp_protocol);
9511da177e4SLinus Torvalds EXPORT_SYMBOL(isapnp_present);
9521da177e4SLinus Torvalds EXPORT_SYMBOL(isapnp_cfg_begin);
9531da177e4SLinus Torvalds EXPORT_SYMBOL(isapnp_cfg_end);
9541da177e4SLinus Torvalds EXPORT_SYMBOL(isapnp_write_byte);
9551da177e4SLinus Torvalds 
9569dd78466SBjorn Helgaas static int isapnp_read_resources(struct pnp_dev *dev,
9579dd78466SBjorn Helgaas 				 struct pnp_resource_table *res)
9581da177e4SLinus Torvalds {
9591da177e4SLinus Torvalds 	int tmp, ret;
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 	dev->active = isapnp_read_byte(ISAPNP_CFG_ACTIVATE);
9621da177e4SLinus Torvalds 	if (dev->active) {
963245073f0SBjorn Helgaas 		for (tmp = 0; tmp < ISAPNP_MAX_PORT; tmp++) {
9641da177e4SLinus Torvalds 			ret = isapnp_read_word(ISAPNP_CFG_PORT + (tmp << 1));
9651da177e4SLinus Torvalds 			if (!ret)
9661da177e4SLinus Torvalds 				continue;
9671da177e4SLinus Torvalds 			res->port_resource[tmp].start = ret;
9681da177e4SLinus Torvalds 			res->port_resource[tmp].flags = IORESOURCE_IO;
9691da177e4SLinus Torvalds 		}
970245073f0SBjorn Helgaas 		for (tmp = 0; tmp < ISAPNP_MAX_MEM; tmp++) {
9719dd78466SBjorn Helgaas 			ret =
9729dd78466SBjorn Helgaas 			    isapnp_read_word(ISAPNP_CFG_MEM + (tmp << 3)) << 8;
9731da177e4SLinus Torvalds 			if (!ret)
9741da177e4SLinus Torvalds 				continue;
9751da177e4SLinus Torvalds 			res->mem_resource[tmp].start = ret;
9761da177e4SLinus Torvalds 			res->mem_resource[tmp].flags = IORESOURCE_MEM;
9771da177e4SLinus Torvalds 		}
978245073f0SBjorn Helgaas 		for (tmp = 0; tmp < ISAPNP_MAX_IRQ; tmp++) {
9799dd78466SBjorn Helgaas 			ret =
9809dd78466SBjorn Helgaas 			    (isapnp_read_word(ISAPNP_CFG_IRQ + (tmp << 1)) >>
9819dd78466SBjorn Helgaas 			     8);
9821da177e4SLinus Torvalds 			if (!ret)
9831da177e4SLinus Torvalds 				continue;
9849dd78466SBjorn Helgaas 			res->irq_resource[tmp].start =
9859dd78466SBjorn Helgaas 			    res->irq_resource[tmp].end = ret;
9861da177e4SLinus Torvalds 			res->irq_resource[tmp].flags = IORESOURCE_IRQ;
9871da177e4SLinus Torvalds 		}
988245073f0SBjorn Helgaas 		for (tmp = 0; tmp < ISAPNP_MAX_DMA; tmp++) {
9891da177e4SLinus Torvalds 			ret = isapnp_read_byte(ISAPNP_CFG_DMA + tmp);
9901da177e4SLinus Torvalds 			if (ret == 4)
9911da177e4SLinus Torvalds 				continue;
9929dd78466SBjorn Helgaas 			res->dma_resource[tmp].start =
9939dd78466SBjorn Helgaas 			    res->dma_resource[tmp].end = ret;
9941da177e4SLinus Torvalds 			res->dma_resource[tmp].flags = IORESOURCE_DMA;
9951da177e4SLinus Torvalds 		}
9961da177e4SLinus Torvalds 	}
9971da177e4SLinus Torvalds 	return 0;
9981da177e4SLinus Torvalds }
9991da177e4SLinus Torvalds 
10009dd78466SBjorn Helgaas static int isapnp_get_resources(struct pnp_dev *dev,
10019dd78466SBjorn Helgaas 				struct pnp_resource_table *res)
10021da177e4SLinus Torvalds {
10031da177e4SLinus Torvalds 	int ret;
10041e0aa9adSBjorn Helgaas 
10051da177e4SLinus Torvalds 	pnp_init_resource_table(res);
10061da177e4SLinus Torvalds 	isapnp_cfg_begin(dev->card->number, dev->number);
10071da177e4SLinus Torvalds 	ret = isapnp_read_resources(dev, res);
10081da177e4SLinus Torvalds 	isapnp_cfg_end();
10091da177e4SLinus Torvalds 	return ret;
10101da177e4SLinus Torvalds }
10111da177e4SLinus Torvalds 
10129dd78466SBjorn Helgaas static int isapnp_set_resources(struct pnp_dev *dev,
10139dd78466SBjorn Helgaas 				struct pnp_resource_table *res)
10141da177e4SLinus Torvalds {
10151da177e4SLinus Torvalds 	int tmp;
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	isapnp_cfg_begin(dev->card->number, dev->number);
10181da177e4SLinus Torvalds 	dev->active = 1;
10199dd78466SBjorn Helgaas 	for (tmp = 0;
1020245073f0SBjorn Helgaas 	     tmp < ISAPNP_MAX_PORT
10219dd78466SBjorn Helgaas 	     && (res->port_resource[tmp].
10229dd78466SBjorn Helgaas 		 flags & (IORESOURCE_IO | IORESOURCE_UNSET)) == IORESOURCE_IO;
10239dd78466SBjorn Helgaas 	     tmp++)
10249dd78466SBjorn Helgaas 		isapnp_write_word(ISAPNP_CFG_PORT + (tmp << 1),
10259dd78466SBjorn Helgaas 				  res->port_resource[tmp].start);
10269dd78466SBjorn Helgaas 	for (tmp = 0;
1027245073f0SBjorn Helgaas 	     tmp < ISAPNP_MAX_IRQ
10289dd78466SBjorn Helgaas 	     && (res->irq_resource[tmp].
10299dd78466SBjorn Helgaas 		 flags & (IORESOURCE_IRQ | IORESOURCE_UNSET)) == IORESOURCE_IRQ;
10309dd78466SBjorn Helgaas 	     tmp++) {
10311da177e4SLinus Torvalds 		int irq = res->irq_resource[tmp].start;
10321da177e4SLinus Torvalds 		if (irq == 2)
10331da177e4SLinus Torvalds 			irq = 9;
10341da177e4SLinus Torvalds 		isapnp_write_byte(ISAPNP_CFG_IRQ + (tmp << 1), irq);
10351da177e4SLinus Torvalds 	}
10369dd78466SBjorn Helgaas 	for (tmp = 0;
1037245073f0SBjorn Helgaas 	     tmp < ISAPNP_MAX_DMA
10389dd78466SBjorn Helgaas 	     && (res->dma_resource[tmp].
10399dd78466SBjorn Helgaas 		 flags & (IORESOURCE_DMA | IORESOURCE_UNSET)) == IORESOURCE_DMA;
10409dd78466SBjorn Helgaas 	     tmp++)
10419dd78466SBjorn Helgaas 		isapnp_write_byte(ISAPNP_CFG_DMA + tmp,
10429dd78466SBjorn Helgaas 				  res->dma_resource[tmp].start);
10439dd78466SBjorn Helgaas 	for (tmp = 0;
1044245073f0SBjorn Helgaas 	     tmp < ISAPNP_MAX_MEM
10459dd78466SBjorn Helgaas 	     && (res->mem_resource[tmp].
10469dd78466SBjorn Helgaas 		 flags & (IORESOURCE_MEM | IORESOURCE_UNSET)) == IORESOURCE_MEM;
10479dd78466SBjorn Helgaas 	     tmp++)
10489dd78466SBjorn Helgaas 		isapnp_write_word(ISAPNP_CFG_MEM + (tmp << 3),
10499dd78466SBjorn Helgaas 				  (res->mem_resource[tmp].start >> 8) & 0xffff);
10501da177e4SLinus Torvalds 	/* FIXME: We aren't handling 32bit mems properly here */
10511da177e4SLinus Torvalds 	isapnp_activate(dev->number);
10521da177e4SLinus Torvalds 	isapnp_cfg_end();
10531da177e4SLinus Torvalds 	return 0;
10541da177e4SLinus Torvalds }
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds static int isapnp_disable_resources(struct pnp_dev *dev)
10571da177e4SLinus Torvalds {
1058b1734913SBjorn Helgaas 	if (!dev->active)
10591da177e4SLinus Torvalds 		return -EINVAL;
10601da177e4SLinus Torvalds 	isapnp_cfg_begin(dev->card->number, dev->number);
10611da177e4SLinus Torvalds 	isapnp_deactivate(dev->number);
10621da177e4SLinus Torvalds 	dev->active = 0;
10631da177e4SLinus Torvalds 	isapnp_cfg_end();
10641da177e4SLinus Torvalds 	return 0;
10651da177e4SLinus Torvalds }
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds struct pnp_protocol isapnp_protocol = {
10681da177e4SLinus Torvalds 	.name = "ISA Plug and Play",
10691da177e4SLinus Torvalds 	.get = isapnp_get_resources,
10701da177e4SLinus Torvalds 	.set = isapnp_set_resources,
10711da177e4SLinus Torvalds 	.disable = isapnp_disable_resources,
10721da177e4SLinus Torvalds };
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds static int __init isapnp_init(void)
10751da177e4SLinus Torvalds {
10761da177e4SLinus Torvalds 	int cards;
10771da177e4SLinus Torvalds 	struct pnp_card *card;
10781da177e4SLinus Torvalds 	struct pnp_dev *dev;
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds 	if (isapnp_disable) {
10811da177e4SLinus Torvalds 		printk(KERN_INFO "isapnp: ISA Plug & Play support disabled\n");
10821da177e4SLinus Torvalds 		return 0;
10831da177e4SLinus Torvalds 	}
108407bd1c4aSDavid Woodhouse #ifdef CONFIG_PPC_MERGE
108507bd1c4aSDavid Woodhouse 	if (check_legacy_ioport(_PIDXR) || check_legacy_ioport(_PNPWRP))
108607bd1c4aSDavid Woodhouse 		return -EINVAL;
108707bd1c4aSDavid Woodhouse #endif
10881da177e4SLinus Torvalds #ifdef ISAPNP_REGION_OK
10891da177e4SLinus Torvalds 	if (!request_region(_PIDXR, 1, "isapnp index")) {
10909dd78466SBjorn Helgaas 		printk(KERN_ERR "isapnp: Index Register 0x%x already used\n",
10919dd78466SBjorn Helgaas 		       _PIDXR);
10921da177e4SLinus Torvalds 		return -EBUSY;
10931da177e4SLinus Torvalds 	}
10941da177e4SLinus Torvalds #endif
10951da177e4SLinus Torvalds 	if (!request_region(_PNPWRP, 1, "isapnp write")) {
10969dd78466SBjorn Helgaas 		printk(KERN_ERR
10979dd78466SBjorn Helgaas 		       "isapnp: Write Data Register 0x%x already used\n",
10989dd78466SBjorn Helgaas 		       _PNPWRP);
10991da177e4SLinus Torvalds #ifdef ISAPNP_REGION_OK
11001da177e4SLinus Torvalds 		release_region(_PIDXR, 1);
11011da177e4SLinus Torvalds #endif
11021da177e4SLinus Torvalds 		return -EBUSY;
11031da177e4SLinus Torvalds 	}
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds 	if (pnp_register_protocol(&isapnp_protocol) < 0)
11061da177e4SLinus Torvalds 		return -EBUSY;
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds 	/*
11091da177e4SLinus Torvalds 	 *      Print a message. The existing ISAPnP code is hanging machines
11101da177e4SLinus Torvalds 	 *      so let the user know where.
11111da177e4SLinus Torvalds 	 */
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 	printk(KERN_INFO "isapnp: Scanning for PnP cards...\n");
11141da177e4SLinus Torvalds 	if (isapnp_rdp >= 0x203 && isapnp_rdp <= 0x3ff) {
11151da177e4SLinus Torvalds 		isapnp_rdp |= 3;
11161da177e4SLinus Torvalds 		if (!request_region(isapnp_rdp, 1, "isapnp read")) {
11179dd78466SBjorn Helgaas 			printk(KERN_ERR
11189dd78466SBjorn Helgaas 			       "isapnp: Read Data Register 0x%x already used\n",
11199dd78466SBjorn Helgaas 			       isapnp_rdp);
11201da177e4SLinus Torvalds #ifdef ISAPNP_REGION_OK
11211da177e4SLinus Torvalds 			release_region(_PIDXR, 1);
11221da177e4SLinus Torvalds #endif
11231da177e4SLinus Torvalds 			release_region(_PNPWRP, 1);
11241da177e4SLinus Torvalds 			return -EBUSY;
11251da177e4SLinus Torvalds 		}
11261da177e4SLinus Torvalds 		isapnp_set_rdp();
11271da177e4SLinus Torvalds 	}
11281da177e4SLinus Torvalds 	if (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff) {
11291da177e4SLinus Torvalds 		cards = isapnp_isolate();
11309dd78466SBjorn Helgaas 		if (cards < 0 || (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff)) {
11311da177e4SLinus Torvalds #ifdef ISAPNP_REGION_OK
11321da177e4SLinus Torvalds 			release_region(_PIDXR, 1);
11331da177e4SLinus Torvalds #endif
11341da177e4SLinus Torvalds 			release_region(_PNPWRP, 1);
11359dd78466SBjorn Helgaas 			printk(KERN_INFO
11369dd78466SBjorn Helgaas 			       "isapnp: No Plug & Play device found\n");
11371da177e4SLinus Torvalds 			return 0;
11381da177e4SLinus Torvalds 		}
11391da177e4SLinus Torvalds 		request_region(isapnp_rdp, 1, "isapnp read");
11401da177e4SLinus Torvalds 	}
11411da177e4SLinus Torvalds 	isapnp_build_device_list();
11421da177e4SLinus Torvalds 	cards = 0;
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	protocol_for_each_card(&isapnp_protocol, card) {
11451da177e4SLinus Torvalds 		cards++;
11461da177e4SLinus Torvalds 		if (isapnp_verbose) {
11479dd78466SBjorn Helgaas 			printk(KERN_INFO "isapnp: Card '%s'\n",
11489dd78466SBjorn Helgaas 			       card->name[0] ? card->name : "Unknown");
11491da177e4SLinus Torvalds 			if (isapnp_verbose < 2)
11501da177e4SLinus Torvalds 				continue;
11511da177e4SLinus Torvalds 			card_for_each_dev(card, dev) {
11529dd78466SBjorn Helgaas 				printk(KERN_INFO "isapnp:   Device '%s'\n",
11539dd78466SBjorn Helgaas 				       dev->name[0] ? dev->name : "Unknown");
11541da177e4SLinus Torvalds 			}
11551da177e4SLinus Torvalds 		}
11561da177e4SLinus Torvalds 	}
11571e0aa9adSBjorn Helgaas 	if (cards)
11589dd78466SBjorn Helgaas 		printk(KERN_INFO
11599dd78466SBjorn Helgaas 		       "isapnp: %i Plug & Play card%s detected total\n", cards,
11609dd78466SBjorn Helgaas 		       cards > 1 ? "s" : "");
11611e0aa9adSBjorn Helgaas 	else
11621da177e4SLinus Torvalds 		printk(KERN_INFO "isapnp: No Plug & Play card found\n");
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	isapnp_proc_init();
11651da177e4SLinus Torvalds 	return 0;
11661da177e4SLinus Torvalds }
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds device_initcall(isapnp_init);
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds /* format is: noisapnp */
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds static int __init isapnp_setup_disable(char *str)
11731da177e4SLinus Torvalds {
11741da177e4SLinus Torvalds 	isapnp_disable = 1;
11751da177e4SLinus Torvalds 	return 1;
11761da177e4SLinus Torvalds }
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds __setup("noisapnp", isapnp_setup_disable);
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds /* format is: isapnp=rdp,reset,skip_pci_scan,verbose */
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds static int __init isapnp_setup_isapnp(char *str)
11831da177e4SLinus Torvalds {
11841da177e4SLinus Torvalds 	(void)((get_option(&str, &isapnp_rdp) == 2) &&
11851da177e4SLinus Torvalds 	       (get_option(&str, &isapnp_reset) == 2) &&
11861da177e4SLinus Torvalds 	       (get_option(&str, &isapnp_verbose) == 2));
11871da177e4SLinus Torvalds 	return 1;
11881da177e4SLinus Torvalds }
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds __setup("isapnp=", isapnp_setup_isapnp);
1191