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