core.c 27.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *  ISA Plug & Play support
3
 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Changelog:
 *  2000-01-01	Added quirks handling for buggy hardware
 *		Peter Denison <peterd@pnd-pc.demon.co.uk>
 *  2000-06-14	Added isapnp_probe_devs() and isapnp_activate_dev()
 *		Christoph Hellwig <hch@infradead.org>
 *  2001-06-03  Added release_region calls to correspond with
 *		request_region calls when a failure occurs.  Also
 *		added KERN_* constants to printk() calls.
 *  2001-11-07  Added isapnp_{,un}register_driver calls along the lines
 *              of the pci driver interface
 *              Kai Germaschewski <kai.germaschewski@gmx.de>
 *  2002-06-06  Made the use of dma channel 0 configurable
 *              Gerald Teschl <gerald.teschl@univie.ac.at>
 *  2002-10-06  Ported to PnP Layer - Adam Belay <ambx1@neo.rr.com>
 *  2003-08-11	Resource Management Updates - Adam Belay <ambx1@neo.rr.com>
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/isapnp.h>
44
#include <linux/mutex.h>
L
Linus Torvalds 已提交
45 46
#include <asm/io.h>

47 48
#include "../base.h"

L
Linus Torvalds 已提交
49 50 51 52
#if 0
#define ISAPNP_REGION_OK
#endif

B
Bjorn Helgaas 已提交
53 54 55 56
int isapnp_disable;		/* Disable ISA PnP */
static int isapnp_rdp;		/* Read Data Port */
static int isapnp_reset = 1;	/* reset all PnP cards (deactivate) */
static int isapnp_verbose = 1;	/* verbose mode */
L
Linus Torvalds 已提交
57

58
MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
L
Linus Torvalds 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
MODULE_DESCRIPTION("Generic ISA Plug & Play support");
module_param(isapnp_disable, int, 0);
MODULE_PARM_DESC(isapnp_disable, "ISA Plug & Play disable");
module_param(isapnp_rdp, int, 0);
MODULE_PARM_DESC(isapnp_rdp, "ISA Plug & Play read data port");
module_param(isapnp_reset, int, 0);
MODULE_PARM_DESC(isapnp_reset, "ISA Plug & Play reset all cards");
module_param(isapnp_verbose, int, 0);
MODULE_PARM_DESC(isapnp_verbose, "ISA Plug & Play verbose mode");
MODULE_LICENSE("GPL");

#define _PIDXR		0x279
#define _PNPWRP		0xa79

/* short tags */
#define _STAG_PNPVERNO		0x01
#define _STAG_LOGDEVID		0x02
#define _STAG_COMPATDEVID	0x03
#define _STAG_IRQ		0x04
#define _STAG_DMA		0x05
#define _STAG_STARTDEP		0x06
#define _STAG_ENDDEP		0x07
#define _STAG_IOPORT		0x08
#define _STAG_FIXEDIO		0x09
#define _STAG_VENDOR		0x0e
#define _STAG_END		0x0f
/* long tags */
#define _LTAG_MEMRANGE		0x81
#define _LTAG_ANSISTR		0x82
#define _LTAG_UNICODESTR	0x83
#define _LTAG_VENDOR		0x84
#define _LTAG_MEM32RANGE	0x85
#define _LTAG_FIXEDMEM32RANGE	0x86

93 94 95 96 97 98 99 100
/* Logical device control and configuration registers */

#define ISAPNP_CFG_ACTIVATE	0x30	/* byte */
#define ISAPNP_CFG_MEM		0x40	/* 4 * dword */
#define ISAPNP_CFG_PORT		0x60	/* 8 * word */
#define ISAPNP_CFG_IRQ		0x70	/* 2 * word */
#define ISAPNP_CFG_DMA		0x74	/* 2 * byte */

101 102 103 104 105 106 107 108 109
/*
 * Sizes of ISAPNP logical device configuration register sets.
 * See PNP-ISA-v1.0a.pdf, Appendix A.
 */
#define ISAPNP_MAX_MEM		4
#define ISAPNP_MAX_PORT		8
#define ISAPNP_MAX_IRQ		2
#define ISAPNP_MAX_DMA		2

L
Linus Torvalds 已提交
110
static unsigned char isapnp_checksum_value;
111
static DEFINE_MUTEX(isapnp_cfg_mutex);
L
Linus Torvalds 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
static int isapnp_csn_count;

/* some prototypes */

static inline void write_data(unsigned char x)
{
	outb(x, _PNPWRP);
}

static inline void write_address(unsigned char x)
{
	outb(x, _PIDXR);
	udelay(20);
}

static inline unsigned char read_data(void)
{
	unsigned char val = inb(isapnp_rdp);
	return val;
}

unsigned char isapnp_read_byte(unsigned char idx)
{
	write_address(idx);
	return read_data();
}

static unsigned short isapnp_read_word(unsigned char idx)
{
	unsigned short val;

	val = isapnp_read_byte(idx);
B
Bjorn Helgaas 已提交
144
	val = (val << 8) + isapnp_read_byte(idx + 1);
L
Linus Torvalds 已提交
145 146 147 148 149 150 151 152 153 154 155 156
	return val;
}

void isapnp_write_byte(unsigned char idx, unsigned char val)
{
	write_address(idx);
	write_data(val);
}

static void isapnp_write_word(unsigned char idx, unsigned short val)
{
	isapnp_write_byte(idx, val >> 8);
B
Bjorn Helgaas 已提交
157
	isapnp_write_byte(idx + 1, val);
L
Linus Torvalds 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
}

static void isapnp_key(void)
{
	unsigned char code = 0x6a, msb;
	int i;

	mdelay(1);
	write_address(0x00);
	write_address(0x00);

	write_address(code);

	for (i = 1; i < 32; i++) {
		msb = ((code & 0x01) ^ ((code & 0x02) >> 1)) << 7;
		code = (code >> 1) | msb;
		write_address(code);
	}
}

/* place all pnp cards in wait-for-key state */
static void isapnp_wait(void)
{
	isapnp_write_byte(0x02, 0x02);
}

static void isapnp_wake(unsigned char csn)
{
	isapnp_write_byte(0x03, csn);
}

static void isapnp_device(unsigned char logdev)
{
	isapnp_write_byte(0x07, logdev);
}

static void isapnp_activate(unsigned char logdev)
{
	isapnp_device(logdev);
	isapnp_write_byte(ISAPNP_CFG_ACTIVATE, 1);
	udelay(250);
}

static void isapnp_deactivate(unsigned char logdev)
{
	isapnp_device(logdev);
	isapnp_write_byte(ISAPNP_CFG_ACTIVATE, 0);
	udelay(500);
}

static void __init isapnp_peek(unsigned char *data, int bytes)
{
	int i, j;
B
Bjorn Helgaas 已提交
211
	unsigned char d = 0;
L
Linus Torvalds 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

	for (i = 1; i <= bytes; i++) {
		for (j = 0; j < 20; j++) {
			d = isapnp_read_byte(0x05);
			if (d & 1)
				break;
			udelay(100);
		}
		if (!(d & 1)) {
			if (data != NULL)
				*data++ = 0xff;
			continue;
		}
		d = isapnp_read_byte(0x04);	/* PRESDI */
		isapnp_checksum_value += d;
		if (data != NULL)
			*data++ = d;
	}
}

#define RDP_STEP	32	/* minimum is 4 */

static int isapnp_next_rdp(void)
{
	int rdp = isapnp_rdp;
	static int old_rdp = 0;
B
Bjorn Helgaas 已提交
238 239

	if (old_rdp) {
L
Linus Torvalds 已提交
240 241 242 243 244
		release_region(old_rdp, 1);
		old_rdp = 0;
	}
	while (rdp <= 0x3ff) {
		/*
B
Bjorn Helgaas 已提交
245 246
		 *      We cannot use NE2000 probe spaces for ISAPnP or we
		 *      will lock up machines.
L
Linus Torvalds 已提交
247
		 */
B
Bjorn Helgaas 已提交
248 249
		if ((rdp < 0x280 || rdp > 0x380)
		    && request_region(rdp, 1, "ISAPnP")) {
L
Linus Torvalds 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
			isapnp_rdp = rdp;
			old_rdp = rdp;
			return 0;
		}
		rdp += RDP_STEP;
	}
	return -1;
}

/* Set read port address */
static inline void isapnp_set_rdp(void)
{
	isapnp_write_byte(0x00, isapnp_rdp >> 2);
	udelay(100);
}

/*
 *	Perform an isolation. The port selection code now tries to avoid
 *	"dangerous to read" ports.
 */
static int __init isapnp_isolate_rdp_select(void)
{
	isapnp_wait();
	isapnp_key();

	/* Control: reset CSN and conditionally everything else too */
	isapnp_write_byte(0x02, isapnp_reset ? 0x05 : 0x04);
	mdelay(2);

	isapnp_wait();
	isapnp_key();
	isapnp_wake(0x00);

	if (isapnp_next_rdp() < 0) {
		isapnp_wait();
		return -1;
	}

	isapnp_set_rdp();
	udelay(1000);
	write_address(0x01);
	udelay(1000);
	return 0;
}

/*
 *  Isolate (assign uniqued CSN) to all ISA PnP devices.
 */
static int __init isapnp_isolate(void)
{
	unsigned char checksum = 0x6a;
	unsigned char chksum = 0x00;
	unsigned char bit = 0x00;
	int data;
	int csn = 0;
	int i;
	int iteration = 1;

	isapnp_rdp = 0x213;
	if (isapnp_isolate_rdp_select() < 0)
		return -1;

	while (1) {
		for (i = 1; i <= 64; i++) {
			data = read_data() << 8;
			udelay(250);
			data = data | read_data();
			udelay(250);
			if (data == 0x55aa)
				bit = 0x01;
B
Bjorn Helgaas 已提交
320 321 322
			checksum =
			    ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7)
			    | (checksum >> 1);
L
Linus Torvalds 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
			bit = 0x00;
		}
		for (i = 65; i <= 72; i++) {
			data = read_data() << 8;
			udelay(250);
			data = data | read_data();
			udelay(250);
			if (data == 0x55aa)
				chksum |= (1 << (i - 65));
		}
		if (checksum != 0x00 && checksum == chksum) {
			csn++;

			isapnp_write_byte(0x06, csn);
			udelay(250);
			iteration++;
			isapnp_wake(0x00);
			isapnp_set_rdp();
			udelay(1000);
			write_address(0x01);
			udelay(1000);
			goto __next;
		}
		if (iteration == 1) {
			isapnp_rdp += RDP_STEP;
			if (isapnp_isolate_rdp_select() < 0)
				return -1;
		} else if (iteration > 1) {
			break;
		}
B
Bjorn Helgaas 已提交
353
__next:
L
Linus Torvalds 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
		if (csn == 255)
			break;
		checksum = 0x6a;
		chksum = 0x00;
		bit = 0x00;
	}
	isapnp_wait();
	isapnp_csn_count = csn;
	return csn;
}

/*
 *  Read one tag from stream.
 */
static int __init isapnp_read_tag(unsigned char *type, unsigned short *size)
{
	unsigned char tag, tmp[2];

	isapnp_peek(&tag, 1);
B
Bjorn Helgaas 已提交
373
	if (tag == 0)		/* invalid tag */
L
Linus Torvalds 已提交
374 375 376 377 378 379 380 381 382 383
		return -1;
	if (tag & 0x80) {	/* large item */
		*type = tag;
		isapnp_peek(tmp, 2);
		*size = (tmp[1] << 8) | tmp[0];
	} else {
		*type = (tag >> 3) & 0x0f;
		*size = tag & 0x07;
	}
#if 0
B
Bjorn Helgaas 已提交
384 385
	printk(KERN_DEBUG "tag = 0x%x, type = 0x%x, size = %i\n", tag, *type,
	       *size);
L
Linus Torvalds 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
#endif
	if (*type == 0xff && *size == 0xffff)	/* probably invalid data */
		return -1;
	return 0;
}

/*
 *  Skip specified number of bytes from stream.
 */
static void __init isapnp_skip_bytes(int count)
{
	isapnp_peek(NULL, count);
}

/*
 *  Parse logical device tag.
 */
B
Bjorn Helgaas 已提交
403 404
static struct pnp_dev *__init isapnp_parse_device(struct pnp_card *card,
						  int size, int number)
L
Linus Torvalds 已提交
405 406 407
{
	unsigned char tmp[6];
	struct pnp_dev *dev;
B
Bjorn Helgaas 已提交
408 409
	u32 eisa_id;
	char id[8];
L
Linus Torvalds 已提交
410 411

	isapnp_peek(tmp, size);
B
Bjorn Helgaas 已提交
412 413
	eisa_id = tmp[0] | tmp[1] << 8 | tmp[2] << 16 | tmp[3] << 24;
	pnp_eisa_id_to_string(eisa_id, id);
B
Bjorn Helgaas 已提交
414 415 416 417 418

	dev = pnp_alloc_dev(&isapnp_protocol, number, id);
	if (!dev)
		return NULL;

L
Linus Torvalds 已提交
419 420 421 422 423
	dev->card = card;
	dev->capabilities |= PNP_CONFIGURABLE;
	dev->capabilities |= PNP_READ;
	dev->capabilities |= PNP_WRITE;
	dev->capabilities |= PNP_DISABLE;
424
	pnp_init_resources(dev);
L
Linus Torvalds 已提交
425 426 427 428 429 430
	return dev;
}

/*
 *  Add IRQ resource to resources list.
 */
431 432
static void __init isapnp_parse_irq_resource(struct pnp_dev *dev,
					     struct pnp_option *option,
B
Bjorn Helgaas 已提交
433
					     int size)
L
Linus Torvalds 已提交
434 435 436 437 438 439
{
	unsigned char tmp[3];
	struct pnp_irq *irq;
	unsigned long bits;

	isapnp_peek(tmp, size);
440
	irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
L
Linus Torvalds 已提交
441 442 443 444 445 446 447 448
	if (!irq)
		return;
	bits = (tmp[1] << 8) | tmp[0];
	bitmap_copy(irq->map, &bits, 16);
	if (size > 2)
		irq->flags = tmp[2];
	else
		irq->flags = IORESOURCE_IRQ_HIGHEDGE;
449
	pnp_register_irq_resource(dev, option, irq);
L
Linus Torvalds 已提交
450 451 452 453 454
}

/*
 *  Add DMA resource to resources list.
 */
455 456
static void __init isapnp_parse_dma_resource(struct pnp_dev *dev,
					     struct pnp_option *option,
B
Bjorn Helgaas 已提交
457
					     int size)
L
Linus Torvalds 已提交
458 459 460 461 462
{
	unsigned char tmp[2];
	struct pnp_dma *dma;

	isapnp_peek(tmp, size);
463
	dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
L
Linus Torvalds 已提交
464 465 466 467
	if (!dma)
		return;
	dma->map = tmp[0];
	dma->flags = tmp[1];
468
	pnp_register_dma_resource(dev, option, dma);
L
Linus Torvalds 已提交
469 470 471 472 473
}

/*
 *  Add port resource to resources list.
 */
474 475
static void __init isapnp_parse_port_resource(struct pnp_dev *dev,
					      struct pnp_option *option,
B
Bjorn Helgaas 已提交
476
					      int size)
L
Linus Torvalds 已提交
477 478 479 480 481
{
	unsigned char tmp[7];
	struct pnp_port *port;

	isapnp_peek(tmp, size);
482
	port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
L
Linus Torvalds 已提交
483 484 485 486 487 488 489
	if (!port)
		return;
	port->min = (tmp[2] << 8) | tmp[1];
	port->max = (tmp[4] << 8) | tmp[3];
	port->align = tmp[5];
	port->size = tmp[6];
	port->flags = tmp[0] ? PNP_PORT_FLAG_16BITADDR : 0;
490
	pnp_register_port_resource(dev, option, port);
L
Linus Torvalds 已提交
491 492 493 494 495
}

/*
 *  Add fixed port resource to resources list.
 */
496 497
static void __init isapnp_parse_fixed_port_resource(struct pnp_dev *dev,
						    struct pnp_option *option,
B
Bjorn Helgaas 已提交
498
						    int size)
L
Linus Torvalds 已提交
499 500 501 502 503
{
	unsigned char tmp[3];
	struct pnp_port *port;

	isapnp_peek(tmp, size);
504
	port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
L
Linus Torvalds 已提交
505 506 507 508 509 510
	if (!port)
		return;
	port->min = port->max = (tmp[1] << 8) | tmp[0];
	port->size = tmp[2];
	port->align = 0;
	port->flags = PNP_PORT_FLAG_FIXED;
511
	pnp_register_port_resource(dev, option, port);
L
Linus Torvalds 已提交
512 513 514 515 516
}

/*
 *  Add memory resource to resources list.
 */
517 518
static void __init isapnp_parse_mem_resource(struct pnp_dev *dev,
					     struct pnp_option *option,
B
Bjorn Helgaas 已提交
519
					     int size)
L
Linus Torvalds 已提交
520 521 522 523 524
{
	unsigned char tmp[9];
	struct pnp_mem *mem;

	isapnp_peek(tmp, size);
525
	mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
L
Linus Torvalds 已提交
526 527 528 529 530 531 532
	if (!mem)
		return;
	mem->min = ((tmp[2] << 8) | tmp[1]) << 8;
	mem->max = ((tmp[4] << 8) | tmp[3]) << 8;
	mem->align = (tmp[6] << 8) | tmp[5];
	mem->size = ((tmp[8] << 8) | tmp[7]) << 8;
	mem->flags = tmp[0];
533
	pnp_register_mem_resource(dev, option, mem);
L
Linus Torvalds 已提交
534 535 536 537 538
}

/*
 *  Add 32-bit memory resource to resources list.
 */
539 540
static void __init isapnp_parse_mem32_resource(struct pnp_dev *dev,
					       struct pnp_option *option,
B
Bjorn Helgaas 已提交
541
					       int size)
L
Linus Torvalds 已提交
542 543 544 545 546
{
	unsigned char tmp[17];
	struct pnp_mem *mem;

	isapnp_peek(tmp, size);
547
	mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
L
Linus Torvalds 已提交
548 549 550 551
	if (!mem)
		return;
	mem->min = (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
	mem->max = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
B
Bjorn Helgaas 已提交
552 553 554 555
	mem->align =
	    (tmp[12] << 24) | (tmp[11] << 16) | (tmp[10] << 8) | tmp[9];
	mem->size =
	    (tmp[16] << 24) | (tmp[15] << 16) | (tmp[14] << 8) | tmp[13];
L
Linus Torvalds 已提交
556
	mem->flags = tmp[0];
557
	pnp_register_mem_resource(dev, option, mem);
L
Linus Torvalds 已提交
558 559 560 561 562
}

/*
 *  Add 32-bit fixed memory resource to resources list.
 */
563 564
static void __init isapnp_parse_fixed_mem32_resource(struct pnp_dev *dev,
						     struct pnp_option *option,
B
Bjorn Helgaas 已提交
565
						     int size)
L
Linus Torvalds 已提交
566 567 568 569 570
{
	unsigned char tmp[9];
	struct pnp_mem *mem;

	isapnp_peek(tmp, size);
571
	mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
L
Linus Torvalds 已提交
572 573
	if (!mem)
		return;
B
Bjorn Helgaas 已提交
574 575
	mem->min = mem->max =
	    (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
L
Linus Torvalds 已提交
576 577 578
	mem->size = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
	mem->align = 0;
	mem->flags = tmp[0];
579
	pnp_register_mem_resource(dev, option, mem);
L
Linus Torvalds 已提交
580 581 582 583
}

/*
 *  Parse card name for ISA PnP device.
B
Bjorn Helgaas 已提交
584
 */
L
Linus Torvalds 已提交
585 586 587 588
static void __init
isapnp_parse_name(char *name, unsigned int name_max, unsigned short *size)
{
	if (name[0] == '\0') {
B
Bjorn Helgaas 已提交
589 590
		unsigned short size1 =
		    *size >= name_max ? (name_max - 1) : *size;
L
Linus Torvalds 已提交
591 592 593 594 595
		isapnp_peek(name, size1);
		name[size1] = '\0';
		*size -= size1;

		/* clean whitespace from end of string */
B
Bjorn Helgaas 已提交
596
		while (size1 > 0 && name[--size1] == ' ')
L
Linus Torvalds 已提交
597 598 599 600 601 602 603 604 605 606 607 608 609 610
			name[size1] = '\0';
	}
}

/*
 *  Parse resource map for logical device.
 */
static int __init isapnp_create_device(struct pnp_card *card,
				       unsigned short size)
{
	int number = 0, skip = 0, priority = 0, compat = 0;
	unsigned char type, tmp[17];
	struct pnp_option *option;
	struct pnp_dev *dev;
B
Bjorn Helgaas 已提交
611 612
	u32 eisa_id;
	char id[8];
B
Bjorn Helgaas 已提交
613

L
Linus Torvalds 已提交
614 615 616 617 618 619 620
	if ((dev = isapnp_parse_device(card, size, number++)) == NULL)
		return 1;
	option = pnp_register_independent_option(dev);
	if (!option) {
		kfree(dev);
		return 1;
	}
B
Bjorn Helgaas 已提交
621
	pnp_add_card_device(card, dev);
L
Linus Torvalds 已提交
622 623

	while (1) {
B
Bjorn Helgaas 已提交
624
		if (isapnp_read_tag(&type, &size) < 0)
L
Linus Torvalds 已提交
625 626 627 628 629 630
			return 1;
		if (skip && type != _STAG_LOGDEVID && type != _STAG_END)
			goto __skip;
		switch (type) {
		case _STAG_LOGDEVID:
			if (size >= 5 && size <= 6) {
B
Bjorn Helgaas 已提交
631 632 633
				if ((dev =
				     isapnp_parse_device(card, size,
							 number++)) == NULL)
L
Linus Torvalds 已提交
634 635 636 637
					return 1;
				size = 0;
				skip = 0;
				option = pnp_register_independent_option(dev);
J
Jesper Juhl 已提交
638 639
				if (!option) {
					kfree(dev);
L
Linus Torvalds 已提交
640
					return 1;
J
Jesper Juhl 已提交
641
				}
B
Bjorn Helgaas 已提交
642
				pnp_add_card_device(card, dev);
L
Linus Torvalds 已提交
643 644 645 646 647 648 649 650 651
			} else {
				skip = 1;
			}
			priority = 0;
			compat = 0;
			break;
		case _STAG_COMPATDEVID:
			if (size == 4 && compat < DEVICE_COUNT_COMPATIBLE) {
				isapnp_peek(tmp, 4);
B
Bjorn Helgaas 已提交
652 653 654 655
				eisa_id = tmp[0] | tmp[1] << 8 |
					  tmp[2] << 16 | tmp[3] << 24;
				pnp_eisa_id_to_string(eisa_id, id);
				pnp_add_id(dev, id);
L
Linus Torvalds 已提交
656 657 658 659 660 661 662
				compat++;
				size = 0;
			}
			break;
		case _STAG_IRQ:
			if (size < 2 || size > 3)
				goto __skip;
663
			isapnp_parse_irq_resource(dev, option, size);
L
Linus Torvalds 已提交
664 665 666 667 668
			size = 0;
			break;
		case _STAG_DMA:
			if (size != 2)
				goto __skip;
669
			isapnp_parse_dma_resource(dev, option, size);
L
Linus Torvalds 已提交
670 671 672 673 674 675 676 677 678 679 680
			size = 0;
			break;
		case _STAG_STARTDEP:
			if (size > 1)
				goto __skip;
			priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
			if (size > 0) {
				isapnp_peek(tmp, size);
				priority = 0x100 | tmp[0];
				size = 0;
			}
B
Bjorn Helgaas 已提交
681
			option = pnp_register_dependent_option(dev, priority);
L
Linus Torvalds 已提交
682 683 684 685 686 687 688
			if (!option)
				return 1;
			break;
		case _STAG_ENDDEP:
			if (size != 0)
				goto __skip;
			priority = 0;
689
			dev_dbg(&dev->dev, "end dependent options\n");
L
Linus Torvalds 已提交
690 691 692 693
			break;
		case _STAG_IOPORT:
			if (size != 7)
				goto __skip;
694
			isapnp_parse_port_resource(dev, option, size);
L
Linus Torvalds 已提交
695 696 697 698 699
			size = 0;
			break;
		case _STAG_FIXEDIO:
			if (size != 3)
				goto __skip;
700
			isapnp_parse_fixed_port_resource(dev, option, size);
L
Linus Torvalds 已提交
701 702 703 704 705 706 707
			size = 0;
			break;
		case _STAG_VENDOR:
			break;
		case _LTAG_MEMRANGE:
			if (size != 9)
				goto __skip;
708
			isapnp_parse_mem_resource(dev, option, size);
L
Linus Torvalds 已提交
709 710 711 712 713 714 715 716 717 718 719 720 721 722
			size = 0;
			break;
		case _LTAG_ANSISTR:
			isapnp_parse_name(dev->name, sizeof(dev->name), &size);
			break;
		case _LTAG_UNICODESTR:
			/* silently ignore */
			/* who use unicode for hardware identification? */
			break;
		case _LTAG_VENDOR:
			break;
		case _LTAG_MEM32RANGE:
			if (size != 17)
				goto __skip;
723
			isapnp_parse_mem32_resource(dev, option, size);
L
Linus Torvalds 已提交
724 725 726 727 728
			size = 0;
			break;
		case _LTAG_FIXEDMEM32RANGE:
			if (size != 9)
				goto __skip;
729
			isapnp_parse_fixed_mem32_resource(dev, option, size);
L
Linus Torvalds 已提交
730 731 732 733 734 735 736
			size = 0;
			break;
		case _STAG_END:
			if (size > 0)
				isapnp_skip_bytes(size);
			return 1;
		default:
737 738
			dev_err(&dev->dev, "unknown tag %#x (card %i), "
				"ignored\n", type, card->number);
L
Linus Torvalds 已提交
739
		}
B
Bjorn Helgaas 已提交
740
__skip:
B
Bjorn Helgaas 已提交
741 742
		if (size > 0)
			isapnp_skip_bytes(size);
L
Linus Torvalds 已提交
743 744 745 746 747 748 749 750 751 752 753 754 755
	}
	return 0;
}

/*
 *  Parse resource map for ISA PnP card.
 */
static void __init isapnp_parse_resource_map(struct pnp_card *card)
{
	unsigned char type, tmp[17];
	unsigned short size;

	while (1) {
B
Bjorn Helgaas 已提交
756
		if (isapnp_read_tag(&type, &size) < 0)
L
Linus Torvalds 已提交
757 758 759 760 761 762 763 764 765 766 767 768
			return;
		switch (type) {
		case _STAG_PNPVERNO:
			if (size != 2)
				goto __skip;
			isapnp_peek(tmp, 2);
			card->pnpver = tmp[0];
			card->productver = tmp[1];
			size = 0;
			break;
		case _STAG_LOGDEVID:
			if (size >= 5 && size <= 6) {
B
Bjorn Helgaas 已提交
769
				if (isapnp_create_device(card, size) == 1)
L
Linus Torvalds 已提交
770 771 772 773 774 775 776
					return;
				size = 0;
			}
			break;
		case _STAG_VENDOR:
			break;
		case _LTAG_ANSISTR:
B
Bjorn Helgaas 已提交
777 778
			isapnp_parse_name(card->name, sizeof(card->name),
					  &size);
L
Linus Torvalds 已提交
779 780 781 782 783 784 785 786 787 788 789 790
			break;
		case _LTAG_UNICODESTR:
			/* silently ignore */
			/* who use unicode for hardware identification? */
			break;
		case _LTAG_VENDOR:
			break;
		case _STAG_END:
			if (size > 0)
				isapnp_skip_bytes(size);
			return;
		default:
791 792
			dev_err(&card->dev, "unknown tag %#x, ignored\n",
			       type);
L
Linus Torvalds 已提交
793
		}
B
Bjorn Helgaas 已提交
794
__skip:
B
Bjorn Helgaas 已提交
795 796
		if (size > 0)
			isapnp_skip_bytes(size);
L
Linus Torvalds 已提交
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
	}
}

/*
 *  Compute ISA PnP checksum for first eight bytes.
 */
static unsigned char __init isapnp_checksum(unsigned char *data)
{
	int i, j;
	unsigned char checksum = 0x6a, bit, b;

	for (i = 0; i < 8; i++) {
		b = data[i];
		for (j = 0; j < 8; j++) {
			bit = 0;
			if (b & (1 << j))
				bit = 1;
B
Bjorn Helgaas 已提交
814 815 816
			checksum =
			    ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7)
			    | (checksum >> 1);
L
Linus Torvalds 已提交
817 818 819 820 821 822 823 824 825 826 827 828 829
		}
	}
	return checksum;
}

/*
 *  Build device list for all present ISA PnP devices.
 */
static int __init isapnp_build_device_list(void)
{
	int csn;
	unsigned char header[9], checksum;
	struct pnp_card *card;
830 831
	u32 eisa_id;
	char id[8];
L
Linus Torvalds 已提交
832 833 834 835 836 837 838

	isapnp_wait();
	isapnp_key();
	for (csn = 1; csn <= isapnp_csn_count; csn++) {
		isapnp_wake(csn);
		isapnp_peek(header, 9);
		checksum = isapnp_checksum(header);
839 840 841
		eisa_id = header[0] | header[1] << 8 |
			  header[2] << 16 | header[3] << 24;
		pnp_eisa_id_to_string(eisa_id, id);
B
Bjorn Helgaas 已提交
842 843 844 845
		card = pnp_alloc_card(&isapnp_protocol, csn, id);
		if (!card)
			continue;

846 847 848 849 850 851 852
#if 0
		dev_info(&card->dev,
		       "vendor: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
		       header[0], header[1], header[2], header[3], header[4],
		       header[5], header[6], header[7], header[8]);
		dev_info(&card->dev, "checksum = %#x\n", checksum);
#endif
B
Bjorn Helgaas 已提交
853
		INIT_LIST_HEAD(&card->devices);
B
Bjorn Helgaas 已提交
854 855 856
		card->serial =
		    (header[7] << 24) | (header[6] << 16) | (header[5] << 8) |
		    header[4];
L
Linus Torvalds 已提交
857 858 859
		isapnp_checksum_value = 0x00;
		isapnp_parse_resource_map(card);
		if (isapnp_checksum_value != 0x00)
860 861
			dev_err(&card->dev, "invalid checksum %#x\n",
				isapnp_checksum_value);
L
Linus Torvalds 已提交
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
		card->checksum = isapnp_checksum_value;

		pnp_add_card(card);
	}
	isapnp_wait();
	return 0;
}

/*
 *  Basic configuration routines.
 */

int isapnp_present(void)
{
	struct pnp_card *card;
B
Bjorn Helgaas 已提交
877

L
Linus Torvalds 已提交
878 879 880 881 882 883 884 885 886 887 888
	pnp_for_each_card(card) {
		if (card->protocol == &isapnp_protocol)
			return 1;
	}
	return 0;
}

int isapnp_cfg_begin(int csn, int logdev)
{
	if (csn < 1 || csn > isapnp_csn_count || logdev > 10)
		return -EINVAL;
889
	mutex_lock(&isapnp_cfg_mutex);
L
Linus Torvalds 已提交
890 891 892 893 894 895 896 897 898
	isapnp_wait();
	isapnp_key();
	isapnp_wake(csn);
#if 0
	/* to avoid malfunction when the isapnptools package is used */
	/* we must set RDP to our value again */
	/* it is possible to set RDP only in the isolation phase */
	/*   Jens Thoms Toerring <Jens.Toerring@physik.fu-berlin.de> */
	isapnp_write_byte(0x02, 0x04);	/* clear CSN of card */
B
Bjorn Helgaas 已提交
899 900 901 902 903
	mdelay(2);		/* is this necessary? */
	isapnp_wake(csn);	/* bring card into sleep state */
	isapnp_wake(0);		/* bring card into isolation state */
	isapnp_set_rdp();	/* reset the RDP port */
	udelay(1000);		/* delay 1000us */
L
Linus Torvalds 已提交
904
	isapnp_write_byte(0x06, csn);	/* reset CSN to previous value */
B
Bjorn Helgaas 已提交
905
	udelay(250);		/* is this necessary? */
L
Linus Torvalds 已提交
906 907 908 909 910 911 912 913 914
#endif
	if (logdev >= 0)
		isapnp_device(logdev);
	return 0;
}

int isapnp_cfg_end(void)
{
	isapnp_wait();
915
	mutex_unlock(&isapnp_cfg_mutex);
L
Linus Torvalds 已提交
916 917 918 919
	return 0;
}

/*
B
Bjorn Helgaas 已提交
920
 *  Initialization.
L
Linus Torvalds 已提交
921 922 923 924 925 926 927 928
 */

EXPORT_SYMBOL(isapnp_protocol);
EXPORT_SYMBOL(isapnp_present);
EXPORT_SYMBOL(isapnp_cfg_begin);
EXPORT_SYMBOL(isapnp_cfg_end);
EXPORT_SYMBOL(isapnp_write_byte);

929
static int isapnp_get_resources(struct pnp_dev *dev)
L
Linus Torvalds 已提交
930
{
931
	struct pnp_resource *pnp_res;
932
	int i, ret;
L
Linus Torvalds 已提交
933

934 935 936
	dev_dbg(&dev->dev, "get resources\n");
	pnp_init_resources(dev);
	isapnp_cfg_begin(dev->card->number, dev->number);
L
Linus Torvalds 已提交
937
	dev->active = isapnp_read_byte(ISAPNP_CFG_ACTIVATE);
938 939 940 941 942 943
	if (!dev->active)
		goto __end;

	for (i = 0; i < ISAPNP_MAX_PORT; i++) {
		ret = isapnp_read_word(ISAPNP_CFG_PORT + (i << 1));
		if (ret) {
944 945
			pnp_res = pnp_add_io_resource(dev, ret, ret, 0);
			if (pnp_res)
946
				pnp_res->index = i;
L
Linus Torvalds 已提交
947
		}
948 949 950 951
	}
	for (i = 0; i < ISAPNP_MAX_MEM; i++) {
		ret = isapnp_read_word(ISAPNP_CFG_MEM + (i << 3)) << 8;
		if (ret) {
952 953
			pnp_res = pnp_add_mem_resource(dev, ret, ret, 0);
			if (pnp_res)
954
				pnp_res->index = i;
L
Linus Torvalds 已提交
955
		}
956 957 958 959
	}
	for (i = 0; i < ISAPNP_MAX_IRQ; i++) {
		ret = isapnp_read_word(ISAPNP_CFG_IRQ + (i << 1)) >> 8;
		if (ret) {
960 961
			pnp_res = pnp_add_irq_resource(dev, ret, 0);
			if (pnp_res)
962
				pnp_res->index = i;
L
Linus Torvalds 已提交
963
		}
964 965 966 967
	}
	for (i = 0; i < ISAPNP_MAX_DMA; i++) {
		ret = isapnp_read_byte(ISAPNP_CFG_DMA + i);
		if (ret != 4) {
968 969
			pnp_res = pnp_add_dma_resource(dev, ret, 0);
			if  (pnp_res)
970
				pnp_res->index = i;
L
Linus Torvalds 已提交
971 972
		}
	}
B
Bjorn Helgaas 已提交
973

974
__end:
L
Linus Torvalds 已提交
975
	isapnp_cfg_end();
976
	return 0;
L
Linus Torvalds 已提交
977 978
}

979
static int isapnp_set_resources(struct pnp_dev *dev)
L
Linus Torvalds 已提交
980
{
981
	struct pnp_resource *pnp_res;
982
	struct resource *res;
983
	int tmp, index;
L
Linus Torvalds 已提交
984

985
	dev_dbg(&dev->dev, "set resources\n");
L
Linus Torvalds 已提交
986 987
	isapnp_cfg_begin(dev->card->number, dev->number);
	dev->active = 1;
988
	for (tmp = 0; tmp < ISAPNP_MAX_PORT; tmp++) {
989 990 991 992
		pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_IO, tmp);
		if (!pnp_res)
			continue;
		res = &pnp_res->res;
993
		if (pnp_resource_valid(res)) {
994
			index = pnp_res->index;
995
			dev_dbg(&dev->dev, "  set io  %d to %#llx\n",
996 997
				index, (unsigned long long) res->start);
			isapnp_write_word(ISAPNP_CFG_PORT + (index << 1),
998 999
					  res->start);
		}
1000
	}
1001
	for (tmp = 0; tmp < ISAPNP_MAX_IRQ; tmp++) {
1002 1003 1004 1005
		pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_IRQ, tmp);
		if (!pnp_res)
			continue;
		res = &pnp_res->res;
1006 1007 1008 1009
		if (pnp_resource_valid(res)) {
			int irq = res->start;
			if (irq == 2)
				irq = 9;
1010 1011 1012
			index = pnp_res->index;
			dev_dbg(&dev->dev, "  set irq %d to %d\n", index, irq);
			isapnp_write_byte(ISAPNP_CFG_IRQ + (index << 1), irq);
1013
		}
L
Linus Torvalds 已提交
1014
	}
1015
	for (tmp = 0; tmp < ISAPNP_MAX_DMA; tmp++) {
1016 1017 1018 1019
		pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_DMA, tmp);
		if (!pnp_res)
			continue;
		res = &pnp_res->res;
1020
		if (pnp_resource_valid(res)) {
1021
			index = pnp_res->index;
1022
			dev_dbg(&dev->dev, "  set dma %d to %lld\n",
1023 1024
				index, (unsigned long long) res->start);
			isapnp_write_byte(ISAPNP_CFG_DMA + index, res->start);
1025
		}
1026
	}
1027
	for (tmp = 0; tmp < ISAPNP_MAX_MEM; tmp++) {
1028 1029 1030 1031
		pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_MEM, tmp);
		if (!pnp_res)
			continue;
		res = &pnp_res->res;
1032
		if (pnp_resource_valid(res)) {
1033
			index = pnp_res->index;
1034
			dev_dbg(&dev->dev, "  set mem %d to %#llx\n",
1035 1036
				index, (unsigned long long) res->start);
			isapnp_write_word(ISAPNP_CFG_MEM + (index << 3),
1037 1038
					  (res->start >> 8) & 0xffff);
		}
1039
	}
L
Linus Torvalds 已提交
1040 1041 1042 1043 1044 1045 1046 1047
	/* FIXME: We aren't handling 32bit mems properly here */
	isapnp_activate(dev->number);
	isapnp_cfg_end();
	return 0;
}

static int isapnp_disable_resources(struct pnp_dev *dev)
{
B
Bjorn Helgaas 已提交
1048
	if (!dev->active)
L
Linus Torvalds 已提交
1049 1050 1051 1052 1053 1054 1055 1056 1057
		return -EINVAL;
	isapnp_cfg_begin(dev->card->number, dev->number);
	isapnp_deactivate(dev->number);
	dev->active = 0;
	isapnp_cfg_end();
	return 0;
}

struct pnp_protocol isapnp_protocol = {
B
Bjorn Helgaas 已提交
1058 1059 1060
	.name = "ISA Plug and Play",
	.get = isapnp_get_resources,
	.set = isapnp_set_resources,
L
Linus Torvalds 已提交
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
	.disable = isapnp_disable_resources,
};

static int __init isapnp_init(void)
{
	int cards;
	struct pnp_card *card;
	struct pnp_dev *dev;

	if (isapnp_disable) {
		printk(KERN_INFO "isapnp: ISA Plug & Play support disabled\n");
		return 0;
	}
1074 1075 1076 1077
#ifdef CONFIG_PPC_MERGE
	if (check_legacy_ioport(_PIDXR) || check_legacy_ioport(_PNPWRP))
		return -EINVAL;
#endif
L
Linus Torvalds 已提交
1078 1079
#ifdef ISAPNP_REGION_OK
	if (!request_region(_PIDXR, 1, "isapnp index")) {
B
Bjorn Helgaas 已提交
1080 1081
		printk(KERN_ERR "isapnp: Index Register 0x%x already used\n",
		       _PIDXR);
L
Linus Torvalds 已提交
1082 1083 1084 1085
		return -EBUSY;
	}
#endif
	if (!request_region(_PNPWRP, 1, "isapnp write")) {
B
Bjorn Helgaas 已提交
1086 1087 1088
		printk(KERN_ERR
		       "isapnp: Write Data Register 0x%x already used\n",
		       _PNPWRP);
L
Linus Torvalds 已提交
1089 1090 1091 1092 1093 1094
#ifdef ISAPNP_REGION_OK
		release_region(_PIDXR, 1);
#endif
		return -EBUSY;
	}

B
Bjorn Helgaas 已提交
1095
	if (pnp_register_protocol(&isapnp_protocol) < 0)
L
Linus Torvalds 已提交
1096 1097 1098
		return -EBUSY;

	/*
B
Bjorn Helgaas 已提交
1099 1100
	 *      Print a message. The existing ISAPnP code is hanging machines
	 *      so let the user know where.
L
Linus Torvalds 已提交
1101
	 */
B
Bjorn Helgaas 已提交
1102

L
Linus Torvalds 已提交
1103 1104 1105 1106
	printk(KERN_INFO "isapnp: Scanning for PnP cards...\n");
	if (isapnp_rdp >= 0x203 && isapnp_rdp <= 0x3ff) {
		isapnp_rdp |= 3;
		if (!request_region(isapnp_rdp, 1, "isapnp read")) {
B
Bjorn Helgaas 已提交
1107 1108 1109
			printk(KERN_ERR
			       "isapnp: Read Data Register 0x%x already used\n",
			       isapnp_rdp);
L
Linus Torvalds 已提交
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
#ifdef ISAPNP_REGION_OK
			release_region(_PIDXR, 1);
#endif
			release_region(_PNPWRP, 1);
			return -EBUSY;
		}
		isapnp_set_rdp();
	}
	if (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff) {
		cards = isapnp_isolate();
B
Bjorn Helgaas 已提交
1120
		if (cards < 0 || (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff)) {
L
Linus Torvalds 已提交
1121 1122 1123 1124
#ifdef ISAPNP_REGION_OK
			release_region(_PIDXR, 1);
#endif
			release_region(_PNPWRP, 1);
B
Bjorn Helgaas 已提交
1125 1126
			printk(KERN_INFO
			       "isapnp: No Plug & Play device found\n");
L
Linus Torvalds 已提交
1127 1128 1129 1130 1131 1132 1133
			return 0;
		}
		request_region(isapnp_rdp, 1, "isapnp read");
	}
	isapnp_build_device_list();
	cards = 0;

B
Bjorn Helgaas 已提交
1134
	protocol_for_each_card(&isapnp_protocol, card) {
L
Linus Torvalds 已提交
1135 1136
		cards++;
		if (isapnp_verbose) {
1137 1138
			dev_info(&card->dev, "card '%s'\n",
			       card->name[0] ? card->name : "unknown");
L
Linus Torvalds 已提交
1139 1140
			if (isapnp_verbose < 2)
				continue;
B
Bjorn Helgaas 已提交
1141
			card_for_each_dev(card, dev) {
1142 1143
				dev_info(&card->dev, "device '%s'\n",
				       dev->name[0] ? dev->name : "unknown");
L
Linus Torvalds 已提交
1144 1145 1146
			}
		}
	}
B
Bjorn Helgaas 已提交
1147
	if (cards)
B
Bjorn Helgaas 已提交
1148 1149 1150
		printk(KERN_INFO
		       "isapnp: %i Plug & Play card%s detected total\n", cards,
		       cards > 1 ? "s" : "");
B
Bjorn Helgaas 已提交
1151
	else
L
Linus Torvalds 已提交
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
		printk(KERN_INFO "isapnp: No Plug & Play card found\n");

	isapnp_proc_init();
	return 0;
}

device_initcall(isapnp_init);

/* format is: noisapnp */

static int __init isapnp_setup_disable(char *str)
{
	isapnp_disable = 1;
	return 1;
}

__setup("noisapnp", isapnp_setup_disable);

/* format is: isapnp=rdp,reset,skip_pci_scan,verbose */

static int __init isapnp_setup_isapnp(char *str)
{
B
Bjorn Helgaas 已提交
1174 1175 1176
	(void)((get_option(&str, &isapnp_rdp) == 2) &&
	       (get_option(&str, &isapnp_reset) == 2) &&
	       (get_option(&str, &isapnp_verbose) == 2));
L
Linus Torvalds 已提交
1177 1178 1179 1180
	return 1;
}

__setup("isapnp=", isapnp_setup_isapnp);