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