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