pnp.c revision 83051
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: head/sys/isa/pnp.c 83051 2001-09-05 03:54:33Z yokota $
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
41typedef struct _pnp_id {
42	u_int32_t vendor_id;
43	u_int32_t serial;
44	u_char checksum;
45} pnp_id;
46
47struct pnp_set_config_arg {
48	int	csn;		/* Card number to configure */
49	int	ldn;		/* Logical device on card */
50};
51
52struct 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
62struct 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
100struct pnp_cinfo pnp_ldn_overrides[MAX_PNP_LDN] = {
101    { 0 }
102};
103#endif
104
105/* The READ_DATA port that we are using currently */
106static int pnp_rd_port;
107
108static void   pnp_send_initiation_key(void);
109static int    pnp_get_serial(pnp_id *p);
110static int    pnp_isolation_protocol(device_t parent);
111
112char *
113pnp_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
130static void
131pnp_write(int d, u_char r)
132{
133	outb (_PNP_ADDRESS, d);
134	outb (_PNP_WRITE_DATA, r);
135}
136
137#if 0
138
139static u_char
140pnp_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 */
152static void
153pnp_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 */
174static int
175pnp_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 */
208static int
209pnp_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
241static int
242write_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 */
300static void
301pnp_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;
344		u_int32_t size;
345
346		/* XXX: should handle memory control register, 32 bit memory */
347		if (config->ic_mem[i].ir_size == 0) {
348			pnp_write(PNP_MEM_BASE_HIGH(i), 0);
349			pnp_write(PNP_MEM_BASE_LOW(i), 0);
350			pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
351			pnp_write(PNP_MEM_RANGE_LOW(i), 0);
352		} else {
353			start = config->ic_mem[i].ir_start;
354			size =  config->ic_mem[i].ir_size;
355			if (start & 0xff)
356				panic("pnp_set_config: bogus memory assignment");
357			pnp_write(PNP_MEM_BASE_HIGH(i), (start >> 16) & 0xff);
358			pnp_write(PNP_MEM_BASE_LOW(i), (start >> 8) & 0xff);
359			pnp_write(PNP_MEM_RANGE_HIGH(i), (size >> 16) & 0xff);
360			pnp_write(PNP_MEM_RANGE_LOW(i), (size >> 8) & 0xff);
361		}
362	}
363	for (; i < ISA_PNP_NMEM; i++) {
364		pnp_write(PNP_MEM_BASE_HIGH(i), 0);
365		pnp_write(PNP_MEM_BASE_LOW(i), 0);
366		pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
367		pnp_write(PNP_MEM_RANGE_LOW(i), 0);
368	}
369
370	for (i = 0; i < config->ic_nport; i++) {
371		u_int32_t start;
372
373		if (config->ic_port[i].ir_size == 0) {
374			pnp_write(PNP_IO_BASE_HIGH(i), 0);
375			pnp_write(PNP_IO_BASE_LOW(i), 0);
376		} else {
377			start = config->ic_port[i].ir_start;
378			pnp_write(PNP_IO_BASE_HIGH(i), (start >> 8) & 0xff);
379			pnp_write(PNP_IO_BASE_LOW(i), (start >> 0) & 0xff);
380		}
381	}
382	for (; i < ISA_PNP_NPORT; i++) {
383		pnp_write(PNP_IO_BASE_HIGH(i), 0);
384		pnp_write(PNP_IO_BASE_LOW(i), 0);
385	}
386
387	for (i = 0; i < config->ic_nirq; i++) {
388		int irq;
389
390		/* XXX: interrupt type */
391		if (config->ic_irqmask[i] == 0) {
392			pnp_write(PNP_IRQ_LEVEL(i), 0);
393			pnp_write(PNP_IRQ_TYPE(i), 2);
394		} else {
395			irq = ffs(config->ic_irqmask[i]) - 1;
396			pnp_write(PNP_IRQ_LEVEL(i), irq);
397			pnp_write(PNP_IRQ_TYPE(i), 2); /* XXX */
398		}
399	}
400	for (; i < ISA_PNP_NIRQ; i++) {
401		/*
402		 * IRQ 0 is not a valid interrupt selection and
403		 * represents no interrupt selection.
404		 */
405		pnp_write(PNP_IRQ_LEVEL(i), 0);
406		pnp_write(PNP_IRQ_TYPE(i), 2);
407	}
408
409	for (i = 0; i < config->ic_ndrq; i++) {
410		int drq;
411
412		if (config->ic_drqmask[i] == 0) {
413			pnp_write(PNP_DMA_CHANNEL(i), 4);
414		} else {
415			drq = ffs(config->ic_drqmask[i]) - 1;
416			pnp_write(PNP_DMA_CHANNEL(i), drq);
417		}
418	}
419	for (; i < ISA_PNP_NDRQ; i++) {
420		/*
421		 * DMA channel 4, the cascade channel is used to
422		 * indicate no DMA channel is active.
423		 */
424		pnp_write(PNP_DMA_CHANNEL(i), 4);
425	}
426
427	pnp_write(PNP_ACTIVATE, enable ? 1 : 0);
428
429	/*
430	 * Wake everyone up again, we are finished.
431	 */
432	pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
433}
434
435/*
436 * Process quirks for a logical device.. The card must be in Config state.
437 */
438void
439pnp_check_quirks(u_int32_t vendor_id, u_int32_t logical_id, int ldn, struct isa_config *config)
440{
441	struct pnp_quirk *qp;
442
443	for (qp = &pnp_quirks[0]; qp->vendor_id; qp++) {
444		if (qp->vendor_id == vendor_id
445		    && (qp->logical_id == 0
446			|| qp->logical_id == logical_id)) {
447			switch (qp->type) {
448			case PNP_QUIRK_WRITE_REG:
449				pnp_write(PNP_SET_LDN, ldn);
450				pnp_write(qp->arg1, qp->arg2);
451				break;
452			case PNP_QUIRK_EXTRA_IO:
453				if (config == NULL)
454					break;
455				if (qp->arg1 != 0) {
456					config->ic_nport++;
457					config->ic_port[config->ic_nport - 1] = config->ic_port[0];
458					config->ic_port[config->ic_nport - 1].ir_start += qp->arg1;
459					config->ic_port[config->ic_nport - 1].ir_end += qp->arg1;
460				}
461				if (qp->arg2 != 0) {
462					config->ic_nport++;
463					config->ic_port[config->ic_nport - 1] = config->ic_port[0];
464					config->ic_port[config->ic_nport - 1].ir_start += qp->arg2;
465					config->ic_port[config->ic_nport - 1].ir_end += qp->arg2;
466				}
467				break;
468			}
469		}
470	}
471}
472
473/*
474 * Scan Resource Data for Logical Devices.
475 *
476 * This function exits as soon as it gets an error reading *ANY*
477 * Resource Data or it reaches the end of Resource Data.  In the first
478 * case the return value will be TRUE, FALSE otherwise.
479 */
480static int
481pnp_create_devices(device_t parent, pnp_id *p, int csn,
482		   u_char *resources, int len)
483{
484	u_char tag, *resp, *resinfo, *startres = 0;
485	int large_len, scanning = len, retval = FALSE;
486	u_int32_t logical_id;
487	u_int32_t compat_id;
488	device_t dev = 0;
489	int ldn = 0;
490	struct pnp_set_config_arg *csnldn;
491	char buf[100];
492	char *desc = 0;
493
494	resp = resources;
495	while (scanning > 0) {
496		tag = *resp++;
497		scanning--;
498		if (PNP_RES_TYPE(tag) != 0) {
499			/* Large resource */
500			if (scanning < 2) {
501				scanning = 0;
502				continue;
503			}
504			large_len = resp[0] + (resp[1] << 8);
505			resp += 2;
506
507			if (scanning < large_len) {
508				scanning = 0;
509				continue;
510			}
511			resinfo = resp;
512			resp += large_len;
513			scanning -= large_len;
514
515			if (PNP_LRES_NUM(tag) == PNP_TAG_ID_ANSI) {
516				if (large_len > sizeof(buf) - 1)
517					large_len = sizeof(buf) - 1;
518				bcopy(resinfo, buf, large_len);
519
520				/*
521				 * Trim trailing spaces.
522				 */
523				while (buf[large_len-1] == ' ')
524					large_len--;
525				buf[large_len] = '\0';
526				desc = buf;
527				if (dev)
528					device_set_desc_copy(dev, desc);
529				continue;
530			}
531
532			continue;
533		}
534
535		/* Small resource */
536		if (scanning < PNP_SRES_LEN(tag)) {
537			scanning = 0;
538			continue;
539		}
540		resinfo = resp;
541		resp += PNP_SRES_LEN(tag);
542		scanning -= PNP_SRES_LEN(tag);;
543
544		switch (PNP_SRES_NUM(tag)) {
545		case PNP_TAG_LOGICAL_DEVICE:
546			/*
547			 * Parse the resources for the previous
548			 * logical device (if any).
549			 */
550			if (startres) {
551				pnp_parse_resources(dev, startres,
552						    resinfo - startres - 1,
553						    ldn);
554				dev = 0;
555				startres = 0;
556			}
557
558			/*
559			 * A new logical device. Scan for end of
560			 * resources.
561			 */
562			bcopy(resinfo, &logical_id, 4);
563			pnp_check_quirks(p->vendor_id, logical_id, ldn, NULL);
564			compat_id = 0;
565			dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, -1);
566			if (desc)
567				device_set_desc_copy(dev, desc);
568			isa_set_vendorid(dev, p->vendor_id);
569			isa_set_serial(dev, p->serial);
570			isa_set_logicalid(dev, logical_id);
571			isa_set_configattr(dev,
572					   ISACFGATTR_CANDISABLE |
573					   ISACFGATTR_DYNAMIC);
574			csnldn = malloc(sizeof *csnldn, M_DEVBUF, M_NOWAIT);
575			if (!csnldn) {
576				device_printf(parent,
577					      "out of memory\n");
578				scanning = 0;
579				break;
580			}
581			csnldn->csn = csn;
582			csnldn->ldn = ldn;
583			ISA_SET_CONFIG_CALLBACK(parent, dev,
584						pnp_set_config, csnldn);
585			ldn++;
586			startres = resp;
587			break;
588
589		case PNP_TAG_END:
590			if (!startres) {
591				device_printf(parent,
592					      "malformed resources\n");
593				scanning = 0;
594				break;
595			}
596			pnp_parse_resources(dev, startres,
597					    resinfo - startres - 1, ldn);
598			dev = 0;
599			startres = 0;
600			scanning = 0;
601			break;
602
603		default:
604			/* Skip this resource */
605			break;
606		}
607	}
608
609	return retval;
610}
611
612/*
613 * Read 'amount' bytes of resources from the card, allocating memory
614 * as needed. If a buffer is already available, it should be passed in
615 * '*resourcesp' and its length in '*spacep'. The number of resource
616 * bytes already in the buffer should be passed in '*lenp'. The memory
617 * allocated will be returned in '*resourcesp' with its size and the
618 * number of bytes of resources in '*spacep' and '*lenp' respectively.
619 */
620static int
621pnp_read_bytes(int amount, u_char **resourcesp, int *spacep, int *lenp)
622{
623	u_char *resources = *resourcesp;
624	u_char *newres;
625	int space = *spacep;
626	int len = *lenp;
627
628	if (space == 0) {
629		space = 1024;
630		resources = malloc(space, M_TEMP, M_NOWAIT);
631		if (!resources)
632			return ENOMEM;
633	}
634
635	if (len + amount > space) {
636		int extra = 1024;
637		while (len + amount > space + extra)
638			extra += 1024;
639		newres = malloc(space + extra, M_TEMP, M_NOWAIT);
640		if (!newres)
641			return ENOMEM;
642		bcopy(resources, newres, len);
643		free(resources, M_TEMP);
644		resources = newres;
645		space += extra;
646	}
647
648	if (pnp_get_resource_info(resources + len, amount) != amount)
649		return EINVAL;
650	len += amount;
651
652	*resourcesp = resources;
653	*spacep = space;
654	*lenp = len;
655
656	return 0;
657}
658
659/*
660 * Read all resources from the card, allocating memory as needed. If a
661 * buffer is already available, it should be passed in '*resourcesp'
662 * and its length in '*spacep'. The memory allocated will be returned
663 * in '*resourcesp' with its size and the number of bytes of resources
664 * in '*spacep' and '*lenp' respectively.
665 */
666static int
667pnp_read_resources(u_char **resourcesp, int *spacep, int *lenp)
668{
669	u_char *resources = *resourcesp;
670	int space = *spacep;
671	int len = 0;
672	int error, done;
673	u_char tag;
674
675	error = 0;
676	done = 0;
677	while (!done) {
678		error = pnp_read_bytes(1, &resources, &space, &len);
679		if (error)
680			goto out;
681		tag = resources[len-1];
682		if (PNP_RES_TYPE(tag) == 0) {
683			/*
684			 * Small resource, read contents.
685			 */
686			error = pnp_read_bytes(PNP_SRES_LEN(tag),
687					       &resources, &space, &len);
688			if (error)
689				goto out;
690			if (PNP_SRES_NUM(tag) == PNP_TAG_END)
691				done = 1;
692		} else {
693			/*
694			 * Large resource, read length and contents.
695			 */
696			error = pnp_read_bytes(2, &resources, &space, &len);
697			if (error)
698				goto out;
699			error = pnp_read_bytes(resources[len-2]
700					       + (resources[len-1] << 8),
701					       &resources, &space, &len);
702			if (error)
703				goto out;
704		}
705	}
706
707 out:
708	*resourcesp = resources;
709	*spacep = space;
710	*lenp = len;
711	return error;
712}
713
714/*
715 * Run the isolation protocol. Use pnp_rd_port as the READ_DATA port
716 * value (caller should try multiple READ_DATA locations before giving
717 * up). Upon exiting, all cards are aware that they should use
718 * pnp_rd_port as the READ_DATA port.
719 *
720 * In the first pass, a csn is assigned to each board and pnp_id's
721 * are saved to an array, pnp_devices. In the second pass, each
722 * card is woken up and the device configuration is called.
723 */
724static int
725pnp_isolation_protocol(device_t parent)
726{
727	int csn;
728	pnp_id id;
729	int found = 0, len;
730	u_char *resources = 0;
731	int space = 0;
732	int error;
733
734	/*
735	 * Put all cards into the Sleep state so that we can clear
736	 * their CSNs.
737	 */
738	pnp_send_initiation_key();
739
740	/*
741	 * Clear the CSN for all cards.
742	 */
743	pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_RESET_CSN);
744
745	/*
746	 * Move all cards to the Isolation state.
747	 */
748	pnp_write(PNP_WAKE, 0);
749
750	/*
751	 * Tell them where the read point is going to be this time.
752	 */
753	pnp_write(PNP_SET_RD_DATA, pnp_rd_port);
754
755	for (csn = 1; csn < PNP_MAX_CARDS; csn++) {
756		/*
757		 * Start the serial isolation protocol.
758		 */
759		outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
760		DELAY(1000);	/* Delay 1 msec */
761
762		if (pnp_get_serial(&id)) {
763			/*
764			 * We have read the id from a card
765			 * successfully. The card which won the
766			 * isolation protocol will be in Isolation
767			 * mode and all others will be in Sleep.
768			 * Program the CSN of the isolated card
769			 * (taking it to Config state) and read its
770			 * resources, creating devices as we find
771			 * logical devices on the card.
772			 */
773			pnp_write(PNP_SET_CSN, csn);
774			error = pnp_read_resources(&resources,
775						   &space,
776						   &len);
777			if (error)
778				break;
779			pnp_create_devices(parent, &id, csn,
780					   resources, len);
781			found++;
782		} else
783			break;
784
785		/*
786		 * Put this card back to the Sleep state and
787		 * simultaneously move all cards which don't have a
788		 * CSN yet to Isolation state.
789		 */
790		pnp_write(PNP_WAKE, 0);
791	}
792
793	/*
794	 * Unless we have chosen the wrong read port, all cards will
795	 * be in Sleep state. Put them back into WaitForKey for
796	 * now. Their resources will be programmed later.
797	 */
798	pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
799
800	/*
801	 * Cleanup.
802	 */
803	if (resources)
804		free(resources, M_TEMP);
805
806	return found;
807}
808
809
810/*
811 * pnp_identify()
812 *
813 * autoconfiguration of pnp devices. This routine just runs the
814 * isolation protocol over several ports, until one is successful.
815 *
816 * may be called more than once ?
817 *
818 */
819
820static void
821pnp_identify(driver_t *driver, device_t parent)
822{
823	int num_pnp_devs;
824
825#if 0
826	if (pnp_ldn_overrides[0].csn == 0) {
827		if (bootverbose)
828			printf("Initializing PnP override table\n");
829		bzero (pnp_ldn_overrides, sizeof(pnp_ldn_overrides));
830		pnp_ldn_overrides[0].csn = 255 ;
831	}
832#endif
833
834	/* Try various READ_DATA ports from 0x203-0x3ff */
835	for (pnp_rd_port = 0x80; (pnp_rd_port < 0xff); pnp_rd_port += 0x10) {
836		if (bootverbose)
837			printf("Trying Read_Port at %x\n", (pnp_rd_port << 2) | 0x3);
838
839		num_pnp_devs = pnp_isolation_protocol(parent);
840		if (num_pnp_devs)
841			break;
842	}
843}
844
845static device_method_t pnp_methods[] = {
846	/* Device interface */
847	DEVMETHOD(device_identify,	pnp_identify),
848
849	{ 0, 0 }
850};
851
852static driver_t pnp_driver = {
853	"pnp",
854	pnp_methods,
855	1,			/* no softc */
856};
857
858static devclass_t pnp_devclass;
859
860DRIVER_MODULE(pnp, isa, pnp_driver, pnp_devclass, 0, 0);
861