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