xref: /freebsd/sys/isa/pnp.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
1 /*
2  * Copyright (c) 1996, Sujal M. Patel
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *	$FreeBSD$
27  *      from: pnp.c,v 1.11 1999/05/06 22:11:19 peter Exp
28  */
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/malloc.h>
36 #include <isa/isavar.h>
37 #include <isa/pnpreg.h>
38 #include <isa/pnpvar.h>
39 #include <machine/bus.h>
40 
41 typedef struct _pnp_id {
42 	u_int32_t vendor_id;
43 	u_int32_t serial;
44 	u_char checksum;
45 } pnp_id;
46 
47 struct pnp_set_config_arg {
48 	int	csn;		/* Card number to configure */
49 	int	ldn;		/* Logical device on card */
50 };
51 
52 struct pnp_quirk {
53 	u_int32_t vendor_id;	/* Vendor of the card */
54 	u_int32_t logical_id;	/* ID of the device with quirk */
55 	int	type;
56 #define PNP_QUIRK_WRITE_REG	1 /* Need to write a pnp register  */
57 #define PNP_QUIRK_EXTRA_IO	2 /* Has extra io ports  */
58 	int	arg1;
59 	int	arg2;
60 };
61 
62 struct pnp_quirk pnp_quirks[] = {
63 	/*
64 	 * The Gravis UltraSound needs register 0xf2 to be set to 0xff
65 	 * to enable power.
66 	 * XXX need to know the logical device id.
67 	 */
68 	{ 0x0100561e /* GRV0001 */,	0,
69 	  PNP_QUIRK_WRITE_REG,	0xf2,	 0xff },
70 	/*
71 	 * An emu8000 does not give us other than the first
72 	 * port.
73 	 */
74 	{ 0x26008c0e /* SB16 */,	0x21008c0e,
75 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
76 	{ 0x42008c0e /* SB32(CTL0042) */,	0x21008c0e,
77 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
78 	{ 0x44008c0e /* SB32(CTL0044) */,	0x21008c0e,
79 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
80 	{ 0x49008c0e /* SB32(CTL0049) */,	0x21008c0e,
81 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
82 	{ 0xf1008c0e /* SB32(CTL00f1) */,	0x21008c0e,
83 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
84 	{ 0xc1008c0e /* SB64(CTL00c1) */,	0x22008c0e,
85 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
86 	{ 0xe4008c0e /* SB64(CTL00e4) */,	0x22008c0e,
87 	  PNP_QUIRK_EXTRA_IO,	0x400,	 0x800 },
88 
89 	{ 0 }
90 };
91 
92 #if 0
93 /*
94  * these entries are initialized using the autoconfig menu
95  * The struct is invalid (and must be initialized) if the first
96  * CSN is zero. The init code fills invalid entries with CSN 255
97  * which is not a supported value.
98  */
99 
100 struct pnp_cinfo pnp_ldn_overrides[MAX_PNP_LDN] = {
101     { 0 }
102 };
103 #endif
104 
105 /* The READ_DATA port that we are using currently */
106 static int pnp_rd_port;
107 
108 static void   pnp_send_initiation_key(void);
109 static int    pnp_get_serial(pnp_id *p);
110 static int    pnp_isolation_protocol(device_t parent);
111 
112 char *
113 pnp_eisaformat(u_int32_t id)
114 {
115 	u_int8_t *data = (u_int8_t *) &id;
116 	static char idbuf[8];
117 	const char  hextoascii[] = "0123456789abcdef";
118 
119 	idbuf[0] = '@' + ((data[0] & 0x7c) >> 2);
120 	idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5));
121 	idbuf[2] = '@' + (data[1] & 0x1f);
122 	idbuf[3] = hextoascii[(data[2] >> 4)];
123 	idbuf[4] = hextoascii[(data[2] & 0xf)];
124 	idbuf[5] = hextoascii[(data[3] >> 4)];
125 	idbuf[6] = hextoascii[(data[3] & 0xf)];
126 	idbuf[7] = 0;
127 	return(idbuf);
128 }
129 
130 static void
131 pnp_write(int d, u_char r)
132 {
133 	outb (_PNP_ADDRESS, d);
134 	outb (_PNP_WRITE_DATA, r);
135 }
136 
137 #if 0
138 
139 static u_char
140 pnp_read(int d)
141 {
142 	outb (_PNP_ADDRESS, d);
143 	return (inb(3 | (pnp_rd_port <<2)));
144 }
145 
146 #endif
147 
148 /*
149  * Send Initiation LFSR as described in "Plug and Play ISA Specification",
150  * Intel May 94.
151  */
152 static void
153 pnp_send_initiation_key()
154 {
155 	int cur, i;
156 
157 	/* Reset the LSFR */
158 	outb(_PNP_ADDRESS, 0);
159 	outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */
160 
161 	cur = 0x6a;
162 	outb(_PNP_ADDRESS, cur);
163 
164 	for (i = 1; i < 32; i++) {
165 		cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff);
166 		outb(_PNP_ADDRESS, cur);
167 	}
168 }
169 
170 
171 /*
172  * Get the device's serial number.  Returns 1 if the serial is valid.
173  */
174 static int
175 pnp_get_serial(pnp_id *p)
176 {
177 	int i, bit, valid = 0, sum = 0x6a;
178 	u_char *data = (u_char *)p;
179 
180 	bzero(data, sizeof(char) * 9);
181 	outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
182 	for (i = 0; i < 72; i++) {
183 		bit = inb((pnp_rd_port << 2) | 0x3) == 0x55;
184 		DELAY(250);	/* Delay 250 usec */
185 
186 		/* Can't Short Circuit the next evaluation, so 'and' is last */
187 		bit = (inb((pnp_rd_port << 2) | 0x3) == 0xaa) && bit;
188 		DELAY(250);	/* Delay 250 usec */
189 
190 		valid = valid || bit;
191 
192 		if (i < 64)
193 			sum = (sum >> 1) |
194 				(((sum ^ (sum >> 1) ^ bit) << 7) & 0xff);
195 
196 		data[i / 8] = (data[i / 8] >> 1) | (bit ? 0x80 : 0);
197 	}
198 
199 	valid = valid && (data[8] == sum);
200 
201 	return valid;
202 }
203 
204 /*
205  * Fill's the buffer with resource info from the device.
206  * Returns the number of characters read.
207  */
208 static int
209 pnp_get_resource_info(u_char *buffer, int len)
210 {
211 	int i, j, count;
212 	u_char temp;
213 
214 	count = 0;
215 	for (i = 0; i < len; i++) {
216 		outb(_PNP_ADDRESS, PNP_STATUS);
217 		for (j = 0; j < 100; j++) {
218 			if ((inb((pnp_rd_port << 2) | 0x3)) & 0x1)
219 				break;
220 			DELAY(1);
221 		}
222 		if (j == 100) {
223 			printf("PnP device failed to report resource data\n");
224 			return count;
225 		}
226 		outb(_PNP_ADDRESS, PNP_RESOURCE_DATA);
227 		temp = inb((pnp_rd_port << 2) | 0x3);
228 		if (buffer != NULL)
229 			buffer[i] = temp;
230 		count++;
231 	}
232 	return count;
233 }
234 
235 #if 0
236 /*
237  * write_pnp_parms initializes a logical device with the parms
238  * in d, and then activates the board if the last parameter is 1.
239  */
240 
241 static int
242 write_pnp_parms(struct pnp_cinfo *d, pnp_id *p, int ldn)
243 {
244     int i, empty = -1 ;
245 
246     pnp_write (SET_LDN, ldn );
247     i = pnp_read(SET_LDN) ;
248     if (i != ldn) {
249 	printf("Warning: LDN %d does not exist\n", ldn);
250     }
251     for (i = 0; i < 8; i++) {
252 	pnp_write(IO_CONFIG_BASE + i * 2, d->ic_port[i] >> 8 );
253 	pnp_write(IO_CONFIG_BASE + i * 2 + 1, d->ic_port[i] & 0xff );
254     }
255     for (i = 0; i < 4; i++) {
256 	pnp_write(MEM_CONFIG + i*8, (d->ic_mem[i].base >> 16) & 0xff );
257 	pnp_write(MEM_CONFIG + i*8+1, (d->ic_mem[i].base >> 8) & 0xff );
258 	pnp_write(MEM_CONFIG + i*8+2, d->ic_mem[i].control & 0xff );
259 	pnp_write(MEM_CONFIG + i*8+3, (d->ic_mem[i].range >> 16) & 0xff );
260 	pnp_write(MEM_CONFIG + i*8+4, (d->ic_mem[i].range >> 8) & 0xff );
261     }
262     for (i = 0; i < 2; i++) {
263 	pnp_write(IRQ_CONFIG + i*2    , d->irq[i] );
264 	pnp_write(IRQ_CONFIG + i*2 + 1, d->irq_type[i] );
265 	pnp_write(DRQ_CONFIG + i, d->drq[i] );
266     }
267     /*
268      * store parameters read into the current kernel
269      * so manual editing next time is easier
270      */
271     for (i = 0 ; i < MAX_PNP_LDN; i++) {
272 	if (pnp_ldn_overrides[i].csn == d->csn &&
273 		pnp_ldn_overrides[i].ldn == ldn) {
274 	    d->flags = pnp_ldn_overrides[i].flags ;
275 	    pnp_ldn_overrides[i] = *d ;
276 	    break ;
277 	} else if (pnp_ldn_overrides[i].csn < 1 ||
278 		pnp_ldn_overrides[i].csn == 255)
279 	    empty = i ;
280     }
281     if (i== MAX_PNP_LDN && empty != -1)
282 	pnp_ldn_overrides[empty] = *d;
283 
284     /*
285      * Here should really perform the range check, and
286      * return a failure if not successful.
287      */
288     pnp_write (IO_RANGE_CHECK, 0);
289     DELAY(1000); /* XXX is it really necessary ? */
290     pnp_write (ACTIVATE, d->enable ? 1 : 0);
291     DELAY(1000); /* XXX is it really necessary ? */
292     return 1 ;
293 }
294 #endif
295 
296 /*
297  * This function is called after the bus has assigned resource
298  * locations for a logical device.
299  */
300 static void
301 pnp_set_config(void *arg, struct isa_config *config, int enable)
302 {
303 	int csn = ((struct pnp_set_config_arg *) arg)->csn;
304 	int ldn = ((struct pnp_set_config_arg *) arg)->ldn;
305 	int i;
306 
307 	/*
308 	 * First put all cards into Sleep state with the initiation
309 	 * key, then put our card into Config state.
310 	 */
311 	pnp_send_initiation_key();
312 	pnp_write(PNP_WAKE, csn);
313 
314 	/*
315 	 * Select our logical device so that we can program it.
316 	 */
317 	pnp_write(PNP_SET_LDN, ldn);
318 
319 	/*
320 	 * Constrain the number of resources we will try to program
321 	 */
322 	if (config->ic_nmem > ISA_PNP_NMEM) {
323 	    printf("too many ISA memory ranges (%d > %d)\n", config->ic_nmem, ISA_PNP_NMEM);
324 	    config->ic_nmem = ISA_PNP_NMEM;
325 	}
326 	if (config->ic_nport > ISA_PNP_NPORT) {
327 	    printf("too many ISA I/O ranges (%d > %d)\n", config->ic_nport, ISA_PNP_NPORT);
328 	    config->ic_nport = ISA_PNP_NPORT;
329 	}
330 	if (config->ic_nirq > ISA_PNP_NIRQ) {
331 	    printf("too many ISA IRQs (%d > %d)\n", config->ic_nirq, ISA_PNP_NIRQ);
332 	    config->ic_nirq = ISA_PNP_NIRQ;
333 	}
334 	if (config->ic_ndrq > ISA_PNP_NDRQ) {
335 	    printf("too many ISA DRQs (%d > %d)\n", config->ic_ndrq, ISA_PNP_NDRQ);
336 	    config->ic_ndrq = ISA_PNP_NDRQ;
337 	}
338 
339 	/*
340 	 * Now program the resources.
341 	 */
342 	for (i = 0; i < config->ic_nmem; i++) {
343 		u_int32_t start = config->ic_mem[i].ir_start;
344 		u_int32_t size =  config->ic_mem[i].ir_size;
345 		if (start & 0xff)
346 			panic("pnp_set_config: bogus memory assignment");
347 		pnp_write(PNP_MEM_BASE_HIGH(i), (start >> 16) & 0xff);
348 		pnp_write(PNP_MEM_BASE_LOW(i), (start >> 8) & 0xff);
349 		pnp_write(PNP_MEM_RANGE_HIGH(i), (size >> 16) & 0xff);
350 		pnp_write(PNP_MEM_RANGE_LOW(i), (size >> 8) & 0xff);
351 	}
352 	for (; i < ISA_PNP_NMEM; i++) {
353 		pnp_write(PNP_MEM_BASE_HIGH(i), 0);
354 		pnp_write(PNP_MEM_BASE_LOW(i), 0);
355 		pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
356 		pnp_write(PNP_MEM_RANGE_LOW(i), 0);
357 	}
358 
359 	for (i = 0; i < config->ic_nport; i++) {
360 		u_int32_t start = config->ic_port[i].ir_start;
361 		pnp_write(PNP_IO_BASE_HIGH(i), (start >> 8) & 0xff);
362 		pnp_write(PNP_IO_BASE_LOW(i), (start >> 0) & 0xff);
363 	}
364 	for (; i < ISA_PNP_NPORT; i++) {
365 		pnp_write(PNP_IO_BASE_HIGH(i), 0);
366 		pnp_write(PNP_IO_BASE_LOW(i), 0);
367 	}
368 
369 	for (i = 0; i < config->ic_nirq; i++) {
370 		int irq = ffs(config->ic_irqmask[i]) - 1;
371 		pnp_write(PNP_IRQ_LEVEL(i), irq);
372 		pnp_write(PNP_IRQ_TYPE(i), 2); /* XXX */
373 	}
374 	for (; i < ISA_PNP_NIRQ; i++) {
375 		/*
376 		 * IRQ 0 is not a valid interrupt selection and
377 		 * represents no interrupt selection.
378 		 */
379 		pnp_write(PNP_IRQ_LEVEL(i), 0);
380 	}
381 
382 	for (i = 0; i < config->ic_ndrq; i++) {
383 		int drq = ffs(config->ic_drqmask[i]) - 1;
384 		pnp_write(PNP_DMA_CHANNEL(i), drq);
385 	}
386 	for (; i < ISA_PNP_NDRQ; i++) {
387 		/*
388 		 * DMA channel 4, the cascade channel is used to
389 		 * indicate no DMA channel is active.
390 		 */
391 		pnp_write(PNP_DMA_CHANNEL(i), 4);
392 	}
393 
394 	pnp_write(PNP_ACTIVATE, enable ? 1 : 0);
395 
396 	/*
397 	 * Wake everyone up again, we are finished.
398 	 */
399 	pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
400 }
401 
402 /*
403  * Process quirks for a logical device.. The card must be in Config state.
404  */
405 void
406 pnp_check_quirks(u_int32_t vendor_id, u_int32_t logical_id, int ldn, struct isa_config *config)
407 {
408 	struct pnp_quirk *qp;
409 
410 	for (qp = &pnp_quirks[0]; qp->vendor_id; qp++) {
411 		if (qp->vendor_id == vendor_id
412 		    && (qp->logical_id == 0
413 			|| qp->logical_id == logical_id)) {
414 			switch (qp->type) {
415 			case PNP_QUIRK_WRITE_REG:
416 				pnp_write(PNP_SET_LDN, ldn);
417 				pnp_write(qp->arg1, qp->arg2);
418 				break;
419 			case PNP_QUIRK_EXTRA_IO:
420 				if (config == NULL)
421 					break;
422 				if (qp->arg1 != 0) {
423 					config->ic_nport++;
424 					config->ic_port[config->ic_nport - 1] = config->ic_port[0];
425 					config->ic_port[config->ic_nport - 1].ir_start += qp->arg1;
426 					config->ic_port[config->ic_nport - 1].ir_end += qp->arg1;
427 				}
428 				if (qp->arg2 != 0) {
429 					config->ic_nport++;
430 					config->ic_port[config->ic_nport - 1] = config->ic_port[0];
431 					config->ic_port[config->ic_nport - 1].ir_start += qp->arg2;
432 					config->ic_port[config->ic_nport - 1].ir_end += qp->arg2;
433 				}
434 				break;
435 			}
436 		}
437 	}
438 }
439 
440 /*
441  * Scan Resource Data for Logical Devices.
442  *
443  * This function exits as soon as it gets an error reading *ANY*
444  * Resource Data or it reaches the end of Resource Data.  In the first
445  * case the return value will be TRUE, FALSE otherwise.
446  */
447 static int
448 pnp_create_devices(device_t parent, pnp_id *p, int csn,
449 		   u_char *resources, int len)
450 {
451 	u_char tag, *resp, *resinfo, *startres = 0;
452 	int large_len, scanning = len, retval = FALSE;
453 	u_int32_t logical_id;
454 	u_int32_t compat_id;
455 	device_t dev = 0;
456 	int ldn = 0;
457 	struct pnp_set_config_arg *csnldn;
458 	char buf[100];
459 	char *desc = 0;
460 
461 	resp = resources;
462 	while (scanning > 0) {
463 		tag = *resp++;
464 		scanning--;
465 		if (PNP_RES_TYPE(tag) != 0) {
466 			/* Large resource */
467 			if (scanning < 2) {
468 				scanning = 0;
469 				continue;
470 			}
471 			large_len = resp[0] + (resp[1] << 8);
472 			resp += 2;
473 
474 			if (scanning < large_len) {
475 				scanning = 0;
476 				continue;
477 			}
478 			resinfo = resp;
479 			resp += large_len;
480 			scanning -= large_len;
481 
482 			if (PNP_LRES_NUM(tag) == PNP_TAG_ID_ANSI) {
483 				if (large_len > sizeof(buf) - 1)
484 					large_len = sizeof(buf) - 1;
485 				bcopy(resinfo, buf, large_len);
486 
487 				/*
488 				 * Trim trailing spaces.
489 				 */
490 				while (buf[large_len-1] == ' ')
491 					large_len--;
492 				buf[large_len] = '\0';
493 				desc = buf;
494 				if (dev)
495 					device_set_desc_copy(dev, desc);
496 				continue;
497 			}
498 
499 			continue;
500 		}
501 
502 		/* Small resource */
503 		if (scanning < PNP_SRES_LEN(tag)) {
504 			scanning = 0;
505 			continue;
506 		}
507 		resinfo = resp;
508 		resp += PNP_SRES_LEN(tag);
509 		scanning -= PNP_SRES_LEN(tag);;
510 
511 		switch (PNP_SRES_NUM(tag)) {
512 		case PNP_TAG_LOGICAL_DEVICE:
513 			/*
514 			 * Parse the resources for the previous
515 			 * logical device (if any).
516 			 */
517 			if (startres) {
518 				pnp_parse_resources(dev, startres,
519 						    resinfo - startres - 1,
520 						    p->vendor_id, logical_id, ldn);
521 				dev = 0;
522 				startres = 0;
523 			}
524 
525 			/*
526 			 * A new logical device. Scan for end of
527 			 * resources.
528 			 */
529 			bcopy(resinfo, &logical_id, 4);
530 			pnp_check_quirks(p->vendor_id, logical_id, ldn, NULL);
531 			compat_id = 0;
532 			dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, -1);
533 			if (desc)
534 				device_set_desc_copy(dev, desc);
535 			isa_set_vendorid(dev, p->vendor_id);
536 			isa_set_serial(dev, p->serial);
537 			isa_set_logicalid(dev, logical_id);
538 			csnldn = malloc(sizeof *csnldn, M_DEVBUF, M_NOWAIT);
539 			if (!csnldn) {
540 				device_printf(parent,
541 					      "out of memory\n");
542 				scanning = 0;
543 				break;
544 			}
545 			csnldn->csn = csn;
546 			csnldn->ldn = ldn;
547 			ISA_SET_CONFIG_CALLBACK(parent, dev,
548 						pnp_set_config, csnldn);
549 			ldn++;
550 			startres = resp;
551 			break;
552 
553 		case PNP_TAG_END:
554 			if (!startres) {
555 				device_printf(parent,
556 					      "malformed resources\n");
557 				scanning = 0;
558 				break;
559 			}
560 			pnp_parse_resources(dev, startres,
561 					    resinfo - startres - 1,
562 					    p->vendor_id, logical_id, ldn);
563 			dev = 0;
564 			startres = 0;
565 			scanning = 0;
566 			break;
567 
568 		default:
569 			/* Skip this resource */
570 			break;
571 		}
572 	}
573 
574 	return retval;
575 }
576 
577 /*
578  * Read 'amount' bytes of resources from the card, allocating memory
579  * as needed. If a buffer is already available, it should be passed in
580  * '*resourcesp' and its length in '*spacep'. The number of resource
581  * bytes already in the buffer should be passed in '*lenp'. The memory
582  * allocated will be returned in '*resourcesp' with its size and the
583  * number of bytes of resources in '*spacep' and '*lenp' respectively.
584  */
585 static int
586 pnp_read_bytes(int amount, u_char **resourcesp, int *spacep, int *lenp)
587 {
588 	u_char *resources = *resourcesp;
589 	u_char *newres;
590 	int space = *spacep;
591 	int len = *lenp;
592 
593 	if (space == 0) {
594 		space = 1024;
595 		resources = malloc(space, M_TEMP, M_NOWAIT);
596 		if (!resources)
597 			return ENOMEM;
598 	}
599 
600 	if (len + amount > space) {
601 		int extra = 1024;
602 		while (len + amount > space + extra)
603 			extra += 1024;
604 		newres = malloc(space + extra, M_TEMP, M_NOWAIT);
605 		if (!newres)
606 			return ENOMEM;
607 		bcopy(resources, newres, len);
608 		free(resources, M_TEMP);
609 		resources = newres;
610 		space += extra;
611 	}
612 
613 	if (pnp_get_resource_info(resources + len, amount) != amount)
614 		return EINVAL;
615 	len += amount;
616 
617 	*resourcesp = resources;
618 	*spacep = space;
619 	*lenp = len;
620 
621 	return 0;
622 }
623 
624 /*
625  * Read all resources from the card, allocating memory as needed. If a
626  * buffer is already available, it should be passed in '*resourcesp'
627  * and its length in '*spacep'. The memory allocated will be returned
628  * in '*resourcesp' with its size and the number of bytes of resources
629  * in '*spacep' and '*lenp' respectively.
630  */
631 static int
632 pnp_read_resources(u_char **resourcesp, int *spacep, int *lenp)
633 {
634 	u_char *resources = *resourcesp;
635 	int space = *spacep;
636 	int len = 0;
637 	int error, done;
638 	u_char tag;
639 
640 	error = 0;
641 	done = 0;
642 	while (!done) {
643 		error = pnp_read_bytes(1, &resources, &space, &len);
644 		if (error)
645 			goto out;
646 		tag = resources[len-1];
647 		if (PNP_RES_TYPE(tag) == 0) {
648 			/*
649 			 * Small resource, read contents.
650 			 */
651 			error = pnp_read_bytes(PNP_SRES_LEN(tag),
652 					       &resources, &space, &len);
653 			if (error)
654 				goto out;
655 			if (PNP_SRES_NUM(tag) == PNP_TAG_END)
656 				done = 1;
657 		} else {
658 			/*
659 			 * Large resource, read length and contents.
660 			 */
661 			error = pnp_read_bytes(2, &resources, &space, &len);
662 			if (error)
663 				goto out;
664 			error = pnp_read_bytes(resources[len-2]
665 					       + (resources[len-1] << 8),
666 					       &resources, &space, &len);
667 			if (error)
668 				goto out;
669 		}
670 	}
671 
672  out:
673 	*resourcesp = resources;
674 	*spacep = space;
675 	*lenp = len;
676 	return error;
677 }
678 
679 /*
680  * Run the isolation protocol. Use pnp_rd_port as the READ_DATA port
681  * value (caller should try multiple READ_DATA locations before giving
682  * up). Upon exiting, all cards are aware that they should use
683  * pnp_rd_port as the READ_DATA port.
684  *
685  * In the first pass, a csn is assigned to each board and pnp_id's
686  * are saved to an array, pnp_devices. In the second pass, each
687  * card is woken up and the device configuration is called.
688  */
689 static int
690 pnp_isolation_protocol(device_t parent)
691 {
692 	int csn;
693 	pnp_id id;
694 	int found = 0, len;
695 	u_char *resources = 0;
696 	int space = 0;
697 	int error;
698 
699 	/*
700 	 * Put all cards into the Sleep state so that we can clear
701 	 * their CSNs.
702 	 */
703 	pnp_send_initiation_key();
704 
705 	/*
706 	 * Clear the CSN for all cards.
707 	 */
708 	pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_RESET_CSN);
709 
710 	/*
711 	 * Move all cards to the Isolation state.
712 	 */
713 	pnp_write(PNP_WAKE, 0);
714 
715 	/*
716 	 * Tell them where the read point is going to be this time.
717 	 */
718 	pnp_write(PNP_SET_RD_DATA, pnp_rd_port);
719 
720 	for (csn = 1; csn < PNP_MAX_CARDS; csn++) {
721 		/*
722 		 * Start the serial isolation protocol.
723 		 */
724 		outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
725 		DELAY(1000);	/* Delay 1 msec */
726 
727 		if (pnp_get_serial(&id)) {
728 			/*
729 			 * We have read the id from a card
730 			 * successfully. The card which won the
731 			 * isolation protocol will be in Isolation
732 			 * mode and all others will be in Sleep.
733 			 * Program the CSN of the isolated card
734 			 * (taking it to Config state) and read its
735 			 * resources, creating devices as we find
736 			 * logical devices on the card.
737 			 */
738 			pnp_write(PNP_SET_CSN, csn);
739 			error = pnp_read_resources(&resources,
740 						   &space,
741 						   &len);
742 			if (error)
743 				break;
744 			pnp_create_devices(parent, &id, csn,
745 					   resources, len);
746 			found++;
747 		} else
748 			break;
749 
750 		/*
751 		 * Put this card back to the Sleep state and
752 		 * simultaneously move all cards which don't have a
753 		 * CSN yet to Isolation state.
754 		 */
755 		pnp_write(PNP_WAKE, 0);
756 	}
757 
758 	/*
759 	 * Unless we have chosen the wrong read port, all cards will
760 	 * be in Sleep state. Put them back into WaitForKey for
761 	 * now. Their resources will be programmed later.
762 	 */
763 	pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
764 
765 	/*
766 	 * Cleanup.
767 	 */
768 	if (resources)
769 		free(resources, M_TEMP);
770 
771 	return found;
772 }
773 
774 
775 /*
776  * pnp_identify()
777  *
778  * autoconfiguration of pnp devices. This routine just runs the
779  * isolation protocol over several ports, until one is successful.
780  *
781  * may be called more than once ?
782  *
783  */
784 
785 static void
786 pnp_identify(driver_t *driver, device_t parent)
787 {
788 	int num_pnp_devs;
789 
790 #if 0
791 	if (pnp_ldn_overrides[0].csn == 0) {
792 		if (bootverbose)
793 			printf("Initializing PnP override table\n");
794 		bzero (pnp_ldn_overrides, sizeof(pnp_ldn_overrides));
795 		pnp_ldn_overrides[0].csn = 255 ;
796 	}
797 #endif
798 
799 	/* Try various READ_DATA ports from 0x203-0x3ff */
800 	for (pnp_rd_port = 0x80; (pnp_rd_port < 0xff); pnp_rd_port += 0x10) {
801 		if (bootverbose)
802 			printf("Trying Read_Port at %x\n", (pnp_rd_port << 2) | 0x3);
803 
804 		num_pnp_devs = pnp_isolation_protocol(parent);
805 		if (num_pnp_devs)
806 			break;
807 	}
808 }
809 
810 static device_method_t pnp_methods[] = {
811 	/* Device interface */
812 	DEVMETHOD(device_identify,	pnp_identify),
813 
814 	{ 0, 0 }
815 };
816 
817 static driver_t pnp_driver = {
818 	"pnp",
819 	pnp_methods,
820 	1,			/* no softc */
821 };
822 
823 static devclass_t pnp_devclass;
824 
825 DRIVER_MODULE(pnp, isa, pnp_driver, pnp_devclass, 0, 0);
826