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 *  2002-10-06  Ported to PnP Layer - Adam Belay <ambx1@neo.rr.com>
34 *  2003-08-11	Resource Management Updates - Adam Belay <ambx1@neo.rr.com>
35 */
36
37#include <linux/module.h>
38#include <linux/kernel.h>
39#include <linux/errno.h>
40#include <linux/slab.h>
41#include <linux/delay.h>
42#include <linux/init.h>
43#include <linux/isapnp.h>
44#include <linux/mutex.h>
45#include <asm/io.h>
46
47
48int isapnp_disable;			/* Disable ISA PnP */
49static int isapnp_rdp;			/* Read Data Port */
50static int isapnp_reset = 1;		/* reset all PnP cards (deactivate) */
51static int isapnp_verbose = 1;		/* verbose mode */
52
53MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
54MODULE_DESCRIPTION("Generic ISA Plug & Play support");
55module_param(isapnp_disable, int, 0);
56MODULE_PARM_DESC(isapnp_disable, "ISA Plug & Play disable");
57module_param(isapnp_rdp, int, 0);
58MODULE_PARM_DESC(isapnp_rdp, "ISA Plug & Play read data port");
59module_param(isapnp_reset, int, 0);
60MODULE_PARM_DESC(isapnp_reset, "ISA Plug & Play reset all cards");
61module_param(isapnp_verbose, int, 0);
62MODULE_PARM_DESC(isapnp_verbose, "ISA Plug & Play verbose mode");
63MODULE_LICENSE("GPL");
64
65#define _PIDXR		0x279
66#define _PNPWRP		0xa79
67
68/* short tags */
69#define _STAG_PNPVERNO		0x01
70#define _STAG_LOGDEVID		0x02
71#define _STAG_COMPATDEVID	0x03
72#define _STAG_IRQ		0x04
73#define _STAG_DMA		0x05
74#define _STAG_STARTDEP		0x06
75#define _STAG_ENDDEP		0x07
76#define _STAG_IOPORT		0x08
77#define _STAG_FIXEDIO		0x09
78#define _STAG_VENDOR		0x0e
79#define _STAG_END		0x0f
80/* long tags */
81#define _LTAG_MEMRANGE		0x81
82#define _LTAG_ANSISTR		0x82
83#define _LTAG_UNICODESTR	0x83
84#define _LTAG_VENDOR		0x84
85#define _LTAG_MEM32RANGE	0x85
86#define _LTAG_FIXEDMEM32RANGE	0x86
87
88static unsigned char isapnp_checksum_value;
89static DEFINE_MUTEX(isapnp_cfg_mutex);
90static int isapnp_detected;
91static int isapnp_csn_count;
92
93/* some prototypes */
94
95static inline void write_data(unsigned char x)
96{
97	outb(x, _PNPWRP);
98}
99
100static inline void write_address(unsigned char x)
101{
102	outb(x, _PIDXR);
103	udelay(20);
104}
105
106static inline unsigned char read_data(void)
107{
108	unsigned char val = inb(isapnp_rdp);
109	return val;
110}
111
112unsigned char isapnp_read_byte(unsigned char idx)
113{
114	write_address(idx);
115	return read_data();
116}
117
118static unsigned short isapnp_read_word(unsigned char idx)
119{
120	unsigned short val;
121
122	val = isapnp_read_byte(idx);
123	val = (val << 8) + isapnp_read_byte(idx+1);
124	return val;
125}
126
127void isapnp_write_byte(unsigned char idx, unsigned char val)
128{
129	write_address(idx);
130	write_data(val);
131}
132
133static void isapnp_write_word(unsigned char idx, unsigned short val)
134{
135	isapnp_write_byte(idx, val >> 8);
136	isapnp_write_byte(idx+1, val);
137}
138
139static void isapnp_key(void)
140{
141	unsigned char code = 0x6a, msb;
142	int i;
143
144	mdelay(1);
145	write_address(0x00);
146	write_address(0x00);
147
148	write_address(code);
149
150	for (i = 1; i < 32; i++) {
151		msb = ((code & 0x01) ^ ((code & 0x02) >> 1)) << 7;
152		code = (code >> 1) | msb;
153		write_address(code);
154	}
155}
156
157/* place all pnp cards in wait-for-key state */
158static void isapnp_wait(void)
159{
160	isapnp_write_byte(0x02, 0x02);
161}
162
163static void isapnp_wake(unsigned char csn)
164{
165	isapnp_write_byte(0x03, csn);
166}
167
168static void isapnp_device(unsigned char logdev)
169{
170	isapnp_write_byte(0x07, logdev);
171}
172
173static void isapnp_activate(unsigned char logdev)
174{
175	isapnp_device(logdev);
176	isapnp_write_byte(ISAPNP_CFG_ACTIVATE, 1);
177	udelay(250);
178}
179
180static void isapnp_deactivate(unsigned char logdev)
181{
182	isapnp_device(logdev);
183	isapnp_write_byte(ISAPNP_CFG_ACTIVATE, 0);
184	udelay(500);
185}
186
187static void __init isapnp_peek(unsigned char *data, int bytes)
188{
189	int i, j;
190	unsigned char d=0;
191
192	for (i = 1; i <= bytes; i++) {
193		for (j = 0; j < 20; j++) {
194			d = isapnp_read_byte(0x05);
195			if (d & 1)
196				break;
197			udelay(100);
198		}
199		if (!(d & 1)) {
200			if (data != NULL)
201				*data++ = 0xff;
202			continue;
203		}
204		d = isapnp_read_byte(0x04);	/* PRESDI */
205		isapnp_checksum_value += d;
206		if (data != NULL)
207			*data++ = d;
208	}
209}
210
211#define RDP_STEP	32	/* minimum is 4 */
212
213static int isapnp_next_rdp(void)
214{
215	int rdp = isapnp_rdp;
216	static int old_rdp = 0;
217
218	if(old_rdp)
219	{
220		release_region(old_rdp, 1);
221		old_rdp = 0;
222	}
223	while (rdp <= 0x3ff) {
224		/*
225		 *	We cannot use NE2000 probe spaces for ISAPnP or we
226		 *	will lock up machines.
227		 */
228		if ((rdp < 0x280 || rdp >  0x380) && request_region(rdp, 1, "ISAPnP"))
229		{
230			isapnp_rdp = rdp;
231			old_rdp = rdp;
232			return 0;
233		}
234		rdp += RDP_STEP;
235	}
236	return -1;
237}
238
239/* Set read port address */
240static inline void isapnp_set_rdp(void)
241{
242	isapnp_write_byte(0x00, isapnp_rdp >> 2);
243	udelay(100);
244}
245
246/*
247 *	Perform an isolation. The port selection code now tries to avoid
248 *	"dangerous to read" ports.
249 */
250
251static int __init isapnp_isolate_rdp_select(void)
252{
253	isapnp_wait();
254	isapnp_key();
255
256	/* Control: reset CSN and conditionally everything else too */
257	isapnp_write_byte(0x02, isapnp_reset ? 0x05 : 0x04);
258	mdelay(2);
259
260	isapnp_wait();
261	isapnp_key();
262	isapnp_wake(0x00);
263
264	if (isapnp_next_rdp() < 0) {
265		isapnp_wait();
266		return -1;
267	}
268
269	isapnp_set_rdp();
270	udelay(1000);
271	write_address(0x01);
272	udelay(1000);
273	return 0;
274}
275
276/*
277 *  Isolate (assign uniqued CSN) to all ISA PnP devices.
278 */
279
280static int __init isapnp_isolate(void)
281{
282	unsigned char checksum = 0x6a;
283	unsigned char chksum = 0x00;
284	unsigned char bit = 0x00;
285	int data;
286	int csn = 0;
287	int i;
288	int iteration = 1;
289
290	isapnp_rdp = 0x213;
291	if (isapnp_isolate_rdp_select() < 0)
292		return -1;
293
294	while (1) {
295		for (i = 1; i <= 64; i++) {
296			data = read_data() << 8;
297			udelay(250);
298			data = data | read_data();
299			udelay(250);
300			if (data == 0x55aa)
301				bit = 0x01;
302			checksum = ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7) | (checksum >> 1);
303			bit = 0x00;
304		}
305		for (i = 65; i <= 72; i++) {
306			data = read_data() << 8;
307			udelay(250);
308			data = data | read_data();
309			udelay(250);
310			if (data == 0x55aa)
311				chksum |= (1 << (i - 65));
312		}
313		if (checksum != 0x00 && checksum == chksum) {
314			csn++;
315
316			isapnp_write_byte(0x06, csn);
317			udelay(250);
318			iteration++;
319			isapnp_wake(0x00);
320			isapnp_set_rdp();
321			udelay(1000);
322			write_address(0x01);
323			udelay(1000);
324			goto __next;
325		}
326		if (iteration == 1) {
327			isapnp_rdp += RDP_STEP;
328			if (isapnp_isolate_rdp_select() < 0)
329				return -1;
330		} else if (iteration > 1) {
331			break;
332		}
333	      __next:
334		if (csn == 255)
335			break;
336		checksum = 0x6a;
337		chksum = 0x00;
338		bit = 0x00;
339	}
340	isapnp_wait();
341	isapnp_csn_count = csn;
342	return csn;
343}
344
345/*
346 *  Read one tag from stream.
347 */
348
349static int __init isapnp_read_tag(unsigned char *type, unsigned short *size)
350{
351	unsigned char tag, tmp[2];
352
353	isapnp_peek(&tag, 1);
354	if (tag == 0)				/* invalid tag */
355		return -1;
356	if (tag & 0x80) {	/* large item */
357		*type = tag;
358		isapnp_peek(tmp, 2);
359		*size = (tmp[1] << 8) | tmp[0];
360	} else {
361		*type = (tag >> 3) & 0x0f;
362		*size = tag & 0x07;
363	}
364	if (type == 0)				/* wrong type */
365		return -1;
366	if (*type == 0xff && *size == 0xffff)	/* probably invalid data */
367		return -1;
368	return 0;
369}
370
371/*
372 *  Skip specified number of bytes from stream.
373 */
374
375static void __init isapnp_skip_bytes(int count)
376{
377	isapnp_peek(NULL, count);
378}
379
380/*
381 *  Parse EISA id.
382 */
383
384static void isapnp_parse_id(struct pnp_dev * dev, unsigned short vendor, unsigned short device)
385{
386	struct pnp_id * id;
387	if (!dev)
388		return;
389	id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
390	if (!id)
391		return;
392	sprintf(id->id, "%c%c%c%x%x%x%x",
393			'A' + ((vendor >> 2) & 0x3f) - 1,
394			'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
395			'A' + ((vendor >> 8) & 0x1f) - 1,
396			(device >> 4) & 0x0f,
397			device & 0x0f,
398			(device >> 12) & 0x0f,
399			(device >> 8) & 0x0f);
400	pnp_add_id(id, dev);
401}
402
403/*
404 *  Parse logical device tag.
405 */
406
407static struct pnp_dev * __init isapnp_parse_device(struct pnp_card *card, int size, int number)
408{
409	unsigned char tmp[6];
410	struct pnp_dev *dev;
411
412	isapnp_peek(tmp, size);
413	dev = kzalloc(sizeof(struct pnp_dev), GFP_KERNEL);
414	if (!dev)
415		return NULL;
416	dev->number = number;
417	isapnp_parse_id(dev, (tmp[1] << 8) | tmp[0], (tmp[3] << 8) | tmp[2]);
418	dev->regs = tmp[4];
419	dev->card = card;
420	if (size > 5)
421		dev->regs |= tmp[5] << 8;
422	dev->protocol = &isapnp_protocol;
423	dev->capabilities |= PNP_CONFIGURABLE;
424	dev->capabilities |= PNP_READ;
425	dev->capabilities |= PNP_WRITE;
426	dev->capabilities |= PNP_DISABLE;
427	pnp_init_resource_table(&dev->res);
428	return dev;
429}
430
431
432/*
433 *  Add IRQ resource to resources list.
434 */
435
436static void __init isapnp_parse_irq_resource(struct pnp_option *option,
437					       int size)
438{
439	unsigned char tmp[3];
440	struct pnp_irq *irq;
441	unsigned long bits;
442
443	isapnp_peek(tmp, size);
444	irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
445	if (!irq)
446		return;
447	bits = (tmp[1] << 8) | tmp[0];
448	bitmap_copy(irq->map, &bits, 16);
449	if (size > 2)
450		irq->flags = tmp[2];
451	else
452		irq->flags = IORESOURCE_IRQ_HIGHEDGE;
453	pnp_register_irq_resource(option, irq);
454	return;
455}
456
457/*
458 *  Add DMA resource to resources list.
459 */
460
461static void __init isapnp_parse_dma_resource(struct pnp_option *option,
462                                    	       int size)
463{
464	unsigned char tmp[2];
465	struct pnp_dma *dma;
466
467	isapnp_peek(tmp, size);
468	dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
469	if (!dma)
470		return;
471	dma->map = tmp[0];
472	dma->flags = tmp[1];
473	pnp_register_dma_resource(option, dma);
474	return;
475}
476
477/*
478 *  Add port resource to resources list.
479 */
480
481static void __init isapnp_parse_port_resource(struct pnp_option *option,
482						int size)
483{
484	unsigned char tmp[7];
485	struct pnp_port *port;
486
487	isapnp_peek(tmp, size);
488	port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
489	if (!port)
490		return;
491	port->min = (tmp[2] << 8) | tmp[1];
492	port->max = (tmp[4] << 8) | tmp[3];
493	port->align = tmp[5];
494	port->size = tmp[6];
495	port->flags = tmp[0] ? PNP_PORT_FLAG_16BITADDR : 0;
496	pnp_register_port_resource(option,port);
497	return;
498}
499
500/*
501 *  Add fixed port resource to resources list.
502 */
503
504static void __init isapnp_parse_fixed_port_resource(struct pnp_option *option,
505						      int size)
506{
507	unsigned char tmp[3];
508	struct pnp_port *port;
509
510	isapnp_peek(tmp, size);
511	port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
512	if (!port)
513		return;
514	port->min = port->max = (tmp[1] << 8) | tmp[0];
515	port->size = tmp[2];
516	port->align = 0;
517	port->flags = PNP_PORT_FLAG_FIXED;
518	pnp_register_port_resource(option,port);
519	return;
520}
521
522/*
523 *  Add memory resource to resources list.
524 */
525
526static void __init isapnp_parse_mem_resource(struct pnp_option *option,
527					       int size)
528{
529	unsigned char tmp[9];
530	struct pnp_mem *mem;
531
532	isapnp_peek(tmp, size);
533	mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
534	if (!mem)
535		return;
536	mem->min = ((tmp[2] << 8) | tmp[1]) << 8;
537	mem->max = ((tmp[4] << 8) | tmp[3]) << 8;
538	mem->align = (tmp[6] << 8) | tmp[5];
539	mem->size = ((tmp[8] << 8) | tmp[7]) << 8;
540	mem->flags = tmp[0];
541	pnp_register_mem_resource(option,mem);
542	return;
543}
544
545/*
546 *  Add 32-bit memory resource to resources list.
547 */
548
549static void __init isapnp_parse_mem32_resource(struct pnp_option *option,
550						 int size)
551{
552	unsigned char tmp[17];
553	struct pnp_mem *mem;
554
555	isapnp_peek(tmp, size);
556	mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
557	if (!mem)
558		return;
559	mem->min = (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
560	mem->max = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
561	mem->align = (tmp[12] << 24) | (tmp[11] << 16) | (tmp[10] << 8) | tmp[9];
562	mem->size = (tmp[16] << 24) | (tmp[15] << 16) | (tmp[14] << 8) | tmp[13];
563	mem->flags = tmp[0];
564	pnp_register_mem_resource(option,mem);
565}
566
567/*
568 *  Add 32-bit fixed memory resource to resources list.
569 */
570
571static void __init isapnp_parse_fixed_mem32_resource(struct pnp_option *option,
572						       int size)
573{
574	unsigned char tmp[9];
575	struct pnp_mem *mem;
576
577	isapnp_peek(tmp, size);
578	mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
579	if (!mem)
580		return;
581	mem->min = mem->max = (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
582	mem->size = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
583	mem->align = 0;
584	mem->flags = tmp[0];
585	pnp_register_mem_resource(option,mem);
586}
587
588/*
589 *  Parse card name for ISA PnP device.
590 */
591
592static void __init
593isapnp_parse_name(char *name, unsigned int name_max, unsigned short *size)
594{
595	if (name[0] == '\0') {
596		unsigned short size1 = *size >= name_max ? (name_max - 1) : *size;
597		isapnp_peek(name, size1);
598		name[size1] = '\0';
599		*size -= size1;
600
601		/* clean whitespace from end of string */
602		while (size1 > 0  &&  name[--size1] == ' ')
603			name[size1] = '\0';
604	}
605}
606
607/*
608 *  Parse resource map for logical device.
609 */
610
611static int __init isapnp_create_device(struct pnp_card *card,
612				       unsigned short size)
613{
614	int number = 0, skip = 0, priority = 0, compat = 0;
615	unsigned char type, tmp[17];
616	struct pnp_option *option;
617	struct pnp_dev *dev;
618	if ((dev = isapnp_parse_device(card, size, number++)) == NULL)
619		return 1;
620	option = pnp_register_independent_option(dev);
621	if (!option) {
622		kfree(dev);
623		return 1;
624	}
625	pnp_add_card_device(card,dev);
626
627	while (1) {
628		if (isapnp_read_tag(&type, &size)<0)
629			return 1;
630		if (skip && type != _STAG_LOGDEVID && type != _STAG_END)
631			goto __skip;
632		switch (type) {
633		case _STAG_LOGDEVID:
634			if (size >= 5 && size <= 6) {
635				if ((dev = isapnp_parse_device(card, size, number++)) == NULL)
636					return 1;
637				size = 0;
638				skip = 0;
639				option = pnp_register_independent_option(dev);
640				if (!option) {
641					kfree(dev);
642					return 1;
643				}
644				pnp_add_card_device(card,dev);
645			} else {
646				skip = 1;
647			}
648			priority = 0;
649			compat = 0;
650			break;
651		case _STAG_COMPATDEVID:
652			if (size == 4 && compat < DEVICE_COUNT_COMPATIBLE) {
653				isapnp_peek(tmp, 4);
654				isapnp_parse_id(dev,(tmp[1] << 8) | tmp[0], (tmp[3] << 8) | tmp[2]);
655				compat++;
656				size = 0;
657			}
658			break;
659		case _STAG_IRQ:
660			if (size < 2 || size > 3)
661				goto __skip;
662			isapnp_parse_irq_resource(option, size);
663			size = 0;
664			break;
665		case _STAG_DMA:
666			if (size != 2)
667				goto __skip;
668			isapnp_parse_dma_resource(option, size);
669			size = 0;
670			break;
671		case _STAG_STARTDEP:
672			if (size > 1)
673				goto __skip;
674			priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
675			if (size > 0) {
676				isapnp_peek(tmp, size);
677				priority = 0x100 | tmp[0];
678				size = 0;
679			}
680			option = pnp_register_dependent_option(dev,priority);
681			if (!option)
682				return 1;
683			break;
684		case _STAG_ENDDEP:
685			if (size != 0)
686				goto __skip;
687			priority = 0;
688			break;
689		case _STAG_IOPORT:
690			if (size != 7)
691				goto __skip;
692			isapnp_parse_port_resource(option, size);
693			size = 0;
694			break;
695		case _STAG_FIXEDIO:
696			if (size != 3)
697				goto __skip;
698			isapnp_parse_fixed_port_resource(option, size);
699			size = 0;
700			break;
701		case _STAG_VENDOR:
702			break;
703		case _LTAG_MEMRANGE:
704			if (size != 9)
705				goto __skip;
706			isapnp_parse_mem_resource(option, size);
707			size = 0;
708			break;
709		case _LTAG_ANSISTR:
710			isapnp_parse_name(dev->name, sizeof(dev->name), &size);
711			break;
712		case _LTAG_UNICODESTR:
713			/* silently ignore */
714			/* who use unicode for hardware identification? */
715			break;
716		case _LTAG_VENDOR:
717			break;
718		case _LTAG_MEM32RANGE:
719			if (size != 17)
720				goto __skip;
721			isapnp_parse_mem32_resource(option, size);
722			size = 0;
723			break;
724		case _LTAG_FIXEDMEM32RANGE:
725			if (size != 9)
726				goto __skip;
727			isapnp_parse_fixed_mem32_resource(option, size);
728			size = 0;
729			break;
730		case _STAG_END:
731			if (size > 0)
732				isapnp_skip_bytes(size);
733			return 1;
734		default:
735			printk(KERN_ERR "isapnp: unexpected or unknown tag type 0x%x for logical device %i (device %i), ignored\n", type, dev->number, card->number);
736		}
737	      __skip:
738	      	if (size > 0)
739		      	isapnp_skip_bytes(size);
740	}
741	return 0;
742}
743
744/*
745 *  Parse resource map for ISA PnP card.
746 */
747
748static void __init isapnp_parse_resource_map(struct pnp_card *card)
749{
750	unsigned char type, tmp[17];
751	unsigned short size;
752
753	while (1) {
754		if (isapnp_read_tag(&type, &size)<0)
755			return;
756		switch (type) {
757		case _STAG_PNPVERNO:
758			if (size != 2)
759				goto __skip;
760			isapnp_peek(tmp, 2);
761			card->pnpver = tmp[0];
762			card->productver = tmp[1];
763			size = 0;
764			break;
765		case _STAG_LOGDEVID:
766			if (size >= 5 && size <= 6) {
767				if (isapnp_create_device(card, size)==1)
768					return;
769				size = 0;
770			}
771			break;
772		case _STAG_VENDOR:
773			break;
774		case _LTAG_ANSISTR:
775			isapnp_parse_name(card->name, sizeof(card->name), &size);
776			break;
777		case _LTAG_UNICODESTR:
778			/* silently ignore */
779			/* who use unicode for hardware identification? */
780			break;
781		case _LTAG_VENDOR:
782			break;
783		case _STAG_END:
784			if (size > 0)
785				isapnp_skip_bytes(size);
786			return;
787		default:
788			printk(KERN_ERR "isapnp: unexpected or unknown tag type 0x%x for device %i, ignored\n", type, card->number);
789		}
790	      __skip:
791	      	if (size > 0)
792		      	isapnp_skip_bytes(size);
793	}
794}
795
796/*
797 *  Compute ISA PnP checksum for first eight bytes.
798 */
799
800static unsigned char __init isapnp_checksum(unsigned char *data)
801{
802	int i, j;
803	unsigned char checksum = 0x6a, bit, b;
804
805	for (i = 0; i < 8; i++) {
806		b = data[i];
807		for (j = 0; j < 8; j++) {
808			bit = 0;
809			if (b & (1 << j))
810				bit = 1;
811			checksum = ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7) | (checksum >> 1);
812		}
813	}
814	return checksum;
815}
816
817/*
818 *  Parse EISA id for ISA PnP card.
819 */
820
821static void isapnp_parse_card_id(struct pnp_card * card, unsigned short vendor, unsigned short device)
822{
823	struct pnp_id * id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
824	if (!id)
825		return;
826	sprintf(id->id, "%c%c%c%x%x%x%x",
827			'A' + ((vendor >> 2) & 0x3f) - 1,
828			'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
829			'A' + ((vendor >> 8) & 0x1f) - 1,
830			(device >> 4) & 0x0f,
831			device & 0x0f,
832			(device >> 12) & 0x0f,
833			(device >> 8) & 0x0f);
834	pnp_add_card_id(id,card);
835}
836
837/*
838 *  Build device list for all present ISA PnP devices.
839 */
840
841static int __init isapnp_build_device_list(void)
842{
843	int csn;
844	unsigned char header[9], checksum;
845	struct pnp_card *card;
846
847	isapnp_wait();
848	isapnp_key();
849	for (csn = 1; csn <= isapnp_csn_count; csn++) {
850		isapnp_wake(csn);
851		isapnp_peek(header, 9);
852		checksum = isapnp_checksum(header);
853		if ((card = kzalloc(sizeof(struct pnp_card), GFP_KERNEL)) == NULL)
854			continue;
855
856		card->number = csn;
857		INIT_LIST_HEAD(&card->devices);
858		isapnp_parse_card_id(card, (header[1] << 8) | header[0], (header[3] << 8) | header[2]);
859		card->serial = (header[7] << 24) | (header[6] << 16) | (header[5] << 8) | header[4];
860		isapnp_checksum_value = 0x00;
861		isapnp_parse_resource_map(card);
862		if (isapnp_checksum_value != 0x00)
863			printk(KERN_ERR "isapnp: checksum for device %i is not valid (0x%x)\n", csn, isapnp_checksum_value);
864		card->checksum = isapnp_checksum_value;
865		card->protocol = &isapnp_protocol;
866
867		pnp_add_card(card);
868	}
869	isapnp_wait();
870	return 0;
871}
872
873/*
874 *  Basic configuration routines.
875 */
876
877int isapnp_present(void)
878{
879	struct pnp_card *card;
880	pnp_for_each_card(card) {
881		if (card->protocol == &isapnp_protocol)
882			return 1;
883	}
884	return 0;
885}
886
887int isapnp_cfg_begin(int csn, int logdev)
888{
889	if (csn < 1 || csn > isapnp_csn_count || logdev > 10)
890		return -EINVAL;
891	mutex_lock(&isapnp_cfg_mutex);
892	isapnp_wait();
893	isapnp_key();
894	isapnp_wake(csn);
895	if (logdev >= 0)
896		isapnp_device(logdev);
897	return 0;
898}
899
900int isapnp_cfg_end(void)
901{
902	isapnp_wait();
903	mutex_unlock(&isapnp_cfg_mutex);
904	return 0;
905}
906
907
908/*
909 *  Inititialization.
910 */
911
912
913EXPORT_SYMBOL(isapnp_protocol);
914EXPORT_SYMBOL(isapnp_present);
915EXPORT_SYMBOL(isapnp_cfg_begin);
916EXPORT_SYMBOL(isapnp_cfg_end);
917EXPORT_SYMBOL(isapnp_write_byte);
918
919static int isapnp_read_resources(struct pnp_dev *dev, struct pnp_resource_table *res)
920{
921	int tmp, ret;
922
923	dev->active = isapnp_read_byte(ISAPNP_CFG_ACTIVATE);
924	if (dev->active) {
925		for (tmp = 0; tmp < PNP_MAX_PORT; tmp++) {
926			ret = isapnp_read_word(ISAPNP_CFG_PORT + (tmp << 1));
927			if (!ret)
928				continue;
929			res->port_resource[tmp].start = ret;
930			res->port_resource[tmp].flags = IORESOURCE_IO;
931		}
932		for (tmp = 0; tmp < PNP_MAX_MEM; tmp++) {
933			ret = isapnp_read_word(ISAPNP_CFG_MEM + (tmp << 3)) << 8;
934			if (!ret)
935				continue;
936			res->mem_resource[tmp].start = ret;
937			res->mem_resource[tmp].flags = IORESOURCE_MEM;
938		}
939		for (tmp = 0; tmp < PNP_MAX_IRQ; tmp++) {
940			ret = (isapnp_read_word(ISAPNP_CFG_IRQ + (tmp << 1)) >> 8);
941			if (!ret)
942				continue;
943			res->irq_resource[tmp].start = res->irq_resource[tmp].end = ret;
944			res->irq_resource[tmp].flags = IORESOURCE_IRQ;
945		}
946		for (tmp = 0; tmp < PNP_MAX_DMA; tmp++) {
947			ret = isapnp_read_byte(ISAPNP_CFG_DMA + tmp);
948			if (ret == 4)
949				continue;
950			res->dma_resource[tmp].start = res->dma_resource[tmp].end = ret;
951			res->dma_resource[tmp].flags = IORESOURCE_DMA;
952		}
953	}
954	return 0;
955}
956
957static int isapnp_get_resources(struct pnp_dev *dev, struct pnp_resource_table * res)
958{
959	int ret;
960	pnp_init_resource_table(res);
961	isapnp_cfg_begin(dev->card->number, dev->number);
962	ret = isapnp_read_resources(dev, res);
963	isapnp_cfg_end();
964	return ret;
965}
966
967static int isapnp_set_resources(struct pnp_dev *dev, struct pnp_resource_table * res)
968{
969	int tmp;
970
971	isapnp_cfg_begin(dev->card->number, dev->number);
972	dev->active = 1;
973	for (tmp = 0; tmp < PNP_MAX_PORT && (res->port_resource[tmp].flags & (IORESOURCE_IO | IORESOURCE_UNSET)) == IORESOURCE_IO; tmp++)
974		isapnp_write_word(ISAPNP_CFG_PORT+(tmp<<1), res->port_resource[tmp].start);
975	for (tmp = 0; tmp < PNP_MAX_IRQ && (res->irq_resource[tmp].flags & (IORESOURCE_IRQ | IORESOURCE_UNSET)) == IORESOURCE_IRQ; tmp++) {
976		int irq = res->irq_resource[tmp].start;
977		if (irq == 2)
978			irq = 9;
979		isapnp_write_byte(ISAPNP_CFG_IRQ+(tmp<<1), irq);
980	}
981	for (tmp = 0; tmp < PNP_MAX_DMA && (res->dma_resource[tmp].flags & (IORESOURCE_DMA | IORESOURCE_UNSET)) == IORESOURCE_DMA; tmp++)
982		isapnp_write_byte(ISAPNP_CFG_DMA+tmp, res->dma_resource[tmp].start);
983	for (tmp = 0; tmp < PNP_MAX_MEM && (res->mem_resource[tmp].flags & (IORESOURCE_MEM | IORESOURCE_UNSET)) == IORESOURCE_MEM; tmp++)
984		isapnp_write_word(ISAPNP_CFG_MEM+(tmp<<3), (res->mem_resource[tmp].start >> 8) & 0xffff);
985	isapnp_activate(dev->number);
986	isapnp_cfg_end();
987	return 0;
988}
989
990static int isapnp_disable_resources(struct pnp_dev *dev)
991{
992	if (!dev || !dev->active)
993		return -EINVAL;
994	isapnp_cfg_begin(dev->card->number, dev->number);
995	isapnp_deactivate(dev->number);
996	dev->active = 0;
997	isapnp_cfg_end();
998	return 0;
999}
1000
1001struct pnp_protocol isapnp_protocol = {
1002	.name	= "ISA Plug and Play",
1003	.get	= isapnp_get_resources,
1004	.set	= isapnp_set_resources,
1005	.disable = isapnp_disable_resources,
1006};
1007
1008static int __init isapnp_init(void)
1009{
1010	int cards;
1011	struct pnp_card *card;
1012	struct pnp_dev *dev;
1013
1014	if (isapnp_disable) {
1015		isapnp_detected = 0;
1016		printk(KERN_INFO "isapnp: ISA Plug & Play support disabled\n");
1017		return 0;
1018	}
1019#ifdef CONFIG_PPC_MERGE
1020	if (check_legacy_ioport(_PIDXR) || check_legacy_ioport(_PNPWRP))
1021		return -EINVAL;
1022#endif
1023#ifdef ISAPNP_REGION_OK
1024	if (!request_region(_PIDXR, 1, "isapnp index")) {
1025		printk(KERN_ERR "isapnp: Index Register 0x%x already used\n", _PIDXR);
1026		return -EBUSY;
1027	}
1028#endif
1029	if (!request_region(_PNPWRP, 1, "isapnp write")) {
1030		printk(KERN_ERR "isapnp: Write Data Register 0x%x already used\n", _PNPWRP);
1031#ifdef ISAPNP_REGION_OK
1032		release_region(_PIDXR, 1);
1033#endif
1034		return -EBUSY;
1035	}
1036
1037	if(pnp_register_protocol(&isapnp_protocol)<0)
1038		return -EBUSY;
1039
1040	/*
1041	 *	Print a message. The existing ISAPnP code is hanging machines
1042	 *	so let the user know where.
1043	 */
1044
1045	printk(KERN_INFO "isapnp: Scanning for PnP cards...\n");
1046	if (isapnp_rdp >= 0x203 && isapnp_rdp <= 0x3ff) {
1047		isapnp_rdp |= 3;
1048		if (!request_region(isapnp_rdp, 1, "isapnp read")) {
1049			printk(KERN_ERR "isapnp: Read Data Register 0x%x already used\n", isapnp_rdp);
1050#ifdef ISAPNP_REGION_OK
1051			release_region(_PIDXR, 1);
1052#endif
1053			release_region(_PNPWRP, 1);
1054			return -EBUSY;
1055		}
1056		isapnp_set_rdp();
1057	}
1058	isapnp_detected = 1;
1059	if (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff) {
1060		cards = isapnp_isolate();
1061		if (cards < 0 ||
1062		    (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff)) {
1063#ifdef ISAPNP_REGION_OK
1064			release_region(_PIDXR, 1);
1065#endif
1066			release_region(_PNPWRP, 1);
1067			isapnp_detected = 0;
1068			printk(KERN_INFO "isapnp: No Plug & Play device found\n");
1069			return 0;
1070		}
1071		request_region(isapnp_rdp, 1, "isapnp read");
1072	}
1073	isapnp_build_device_list();
1074	cards = 0;
1075
1076	protocol_for_each_card(&isapnp_protocol,card) {
1077		cards++;
1078		if (isapnp_verbose) {
1079			printk(KERN_INFO "isapnp: Card '%s'\n", card->name[0]?card->name:"Unknown");
1080			if (isapnp_verbose < 2)
1081				continue;
1082			card_for_each_dev(card,dev) {
1083				printk(KERN_INFO "isapnp:   Device '%s'\n", dev->name[0]?dev->name:"Unknown");
1084			}
1085		}
1086	}
1087	if (cards) {
1088		printk(KERN_INFO "isapnp: %i Plug & Play card%s detected total\n", cards, cards>1?"s":"");
1089	} else {
1090		printk(KERN_INFO "isapnp: No Plug & Play card found\n");
1091	}
1092
1093	isapnp_proc_init();
1094	return 0;
1095}
1096
1097device_initcall(isapnp_init);
1098
1099/* format is: noisapnp */
1100
1101static int __init isapnp_setup_disable(char *str)
1102{
1103	isapnp_disable = 1;
1104	return 1;
1105}
1106
1107__setup("noisapnp", isapnp_setup_disable);
1108
1109/* format is: isapnp=rdp,reset,skip_pci_scan,verbose */
1110
1111static int __init isapnp_setup_isapnp(char *str)
1112{
1113	(void)((get_option(&str,&isapnp_rdp) == 2) &&
1114	       (get_option(&str,&isapnp_reset) == 2) &&
1115	       (get_option(&str,&isapnp_verbose) == 2));
1116	return 1;
1117}
1118
1119__setup("isapnp=", isapnp_setup_isapnp);
1120