Home | History | Annotate | Line # | Download | only in isapnp
isapnp.c revision 1.23
      1 /*	$NetBSD: isapnp.c,v 1.23 1998/07/31 05:26:15 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996 Christos Zoulas.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Christos Zoulas.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * ISA PnP bus autoconfiguration.
     34  */
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/device.h>
     39 #include <sys/malloc.h>
     40 
     41 #include <machine/bus.h>
     42 
     43 #include <dev/isa/isavar.h>
     44 #include <dev/isa/isadmavar.h>
     45 
     46 #include <dev/isapnp/isapnpreg.h>
     47 #include <dev/isapnp/isapnpvar.h>
     48 #include <dev/isapnp/isapnpdevs.h>
     49 
     50 static void isapnp_init __P((struct isapnp_softc *));
     51 static __inline u_char isapnp_shift_bit __P((struct isapnp_softc *));
     52 static int isapnp_findcard __P((struct isapnp_softc *));
     53 static void isapnp_free_region __P((bus_space_tag_t, struct isapnp_region *));
     54 static int isapnp_alloc_region __P((bus_space_tag_t, struct isapnp_region *));
     55 static int isapnp_alloc_irq __P((isa_chipset_tag_t, struct isapnp_pin *));
     56 static int isapnp_alloc_drq __P((isa_chipset_tag_t, struct isapnp_pin *));
     57 static int isapnp_testconfig __P((bus_space_tag_t, bus_space_tag_t,
     58     struct isapnp_attach_args *, int));
     59 static struct isapnp_attach_args *isapnp_bestconfig __P((struct isapnp_softc *,
     60     struct isapnp_attach_args **));
     61 static void isapnp_print_region __P((const char *, struct isapnp_region *,
     62     size_t));
     63 static void isapnp_configure __P((struct isapnp_softc *,
     64     const struct isapnp_attach_args *));
     65 static void isapnp_print_pin __P((const char *, struct isapnp_pin *, size_t));
     66 static int isapnp_print __P((void *, const char *));
     67 #ifdef _KERNEL
     68 static int isapnp_submatch __P((struct device *, struct cfdata *, void *));
     69 #endif
     70 static int isapnp_find __P((struct isapnp_softc *, int));
     71 static int isapnp_match __P((struct device *, struct cfdata *, void *));
     72 static void isapnp_attach __P((struct device *, struct device *, void *));
     73 static void isapnp_callback __P((struct device *));
     74 
     75 struct cfattach isapnp_ca = {
     76 	sizeof(struct isapnp_softc), isapnp_match, isapnp_attach
     77 };
     78 
     79 /*
     80  * This keeps track if which ISA's we have been probed on.
     81  */
     82 struct isapnp_probe_cookie {
     83 	LIST_ENTRY(isapnp_probe_cookie)	ipc_link;
     84 	struct device *ipc_parent;
     85 };
     86 LIST_HEAD(, isapnp_probe_cookie) isapnp_probes =
     87     LIST_HEAD_INITIALIZER(isapnp_probes);
     88 
     89 /* isapnp_init():
     90  *	Write the PNP initiation key to wake up the cards...
     91  */
     92 static void
     93 isapnp_init(sc)
     94 	struct isapnp_softc *sc;
     95 {
     96 	int i;
     97 	u_char v = ISAPNP_LFSR_INIT;
     98 
     99 	/* First write 0's twice to enter the Wait for Key state */
    100 	ISAPNP_WRITE_ADDR(sc, 0);
    101 	ISAPNP_WRITE_ADDR(sc, 0);
    102 
    103 	/* Send the 32 byte sequence to awake the logic */
    104 	for (i = 0; i < ISAPNP_LFSR_LENGTH; i++) {
    105 		ISAPNP_WRITE_ADDR(sc, v);
    106 		v = ISAPNP_LFSR_NEXT(v);
    107 	}
    108 }
    109 
    110 
    111 /* isapnp_shift_bit():
    112  *	Read a bit at a time from the config card.
    113  */
    114 static __inline u_char
    115 isapnp_shift_bit(sc)
    116 	struct isapnp_softc *sc;
    117 {
    118 	u_char c1, c2;
    119 
    120 	DELAY(250);
    121 	c1 = ISAPNP_READ_DATA(sc);
    122 	DELAY(250);
    123 	c2 = ISAPNP_READ_DATA(sc);
    124 
    125 	if (c1 == 0x55 && c2 == 0xAA)
    126 		return 0x80;
    127 	else
    128 		return 0;
    129 }
    130 
    131 
    132 /* isapnp_findcard():
    133  *	Attempt to read the vendor/serial/checksum for a card
    134  *	If a card is found [the checksum matches], assign the
    135  *	next card number to it and return 1
    136  */
    137 static int
    138 isapnp_findcard(sc)
    139 	struct isapnp_softc *sc;
    140 {
    141 	u_char v = ISAPNP_LFSR_INIT, csum, w;
    142 	int i, b;
    143 
    144 	if (sc->sc_ncards == ISAPNP_MAX_CARDS) {
    145 		printf("%s: Too many pnp cards\n", sc->sc_dev.dv_xname);
    146 		return 0;
    147 	}
    148 
    149 	/* Set the read port */
    150 	isapnp_write_reg(sc, ISAPNP_WAKE, 0);
    151 	isapnp_write_reg(sc, ISAPNP_SET_RD_PORT, sc->sc_read_port >> 2);
    152 	sc->sc_read_port |= 3;
    153 	DELAY(1000);
    154 
    155 	ISAPNP_WRITE_ADDR(sc, ISAPNP_SERIAL_ISOLATION);
    156 	DELAY(1000);
    157 
    158 	/* Read the 8 bytes of the Vendor ID and Serial Number */
    159 	for(i = 0; i < 8; i++) {
    160 		/* Read each bit separately */
    161 		for (w = 0, b = 0; b < 8; b++) {
    162 			u_char neg = isapnp_shift_bit(sc);
    163 
    164 			w >>= 1;
    165 			w |= neg;
    166 			v = ISAPNP_LFSR_NEXT(v) ^ neg;
    167 		}
    168 		sc->sc_id[sc->sc_ncards][i] = w;
    169 	}
    170 
    171 	/* Read the remaining checksum byte */
    172 	for (csum = 0, b = 0; b < 8; b++) {
    173 		u_char neg = isapnp_shift_bit(sc);
    174 
    175 		csum >>= 1;
    176 		csum |= neg;
    177 	}
    178 	sc->sc_id[sc->sc_ncards][8] = csum;
    179 
    180 	if (csum == v) {
    181 		sc->sc_ncards++;
    182 		isapnp_write_reg(sc, ISAPNP_CARD_SELECT_NUM, sc->sc_ncards);
    183 		return 1;
    184 	}
    185 	return 0;
    186 }
    187 
    188 
    189 /* isapnp_free_region():
    190  *	Free a region
    191  */
    192 static void
    193 isapnp_free_region(t, r)
    194 	bus_space_tag_t t;
    195 	struct isapnp_region *r;
    196 {
    197 #ifdef _KERNEL
    198 	bus_space_unmap(t, r->h, r->length);
    199 #endif
    200 }
    201 
    202 
    203 /* isapnp_alloc_region():
    204  *	Allocate a single region if possible
    205  */
    206 static int
    207 isapnp_alloc_region(t, r)
    208 	bus_space_tag_t t;
    209 	struct isapnp_region *r;
    210 {
    211 	int error = 0;
    212 
    213 	for (r->base = r->minbase; r->base <= r->maxbase;
    214 	     r->base += r->align) {
    215 #ifdef _KERNEL
    216 		error = bus_space_map(t, r->base, r->length, 0, &r->h);
    217 #endif
    218 		if (error == 0)
    219 			return 0;
    220 	}
    221 	return error;
    222 }
    223 
    224 
    225 /* isapnp_alloc_irq():
    226  *	Allocate an irq
    227  */
    228 static int
    229 isapnp_alloc_irq(ic, i)
    230 	isa_chipset_tag_t ic;
    231 	struct isapnp_pin *i;
    232 {
    233 	int irq;
    234 #define LEVEL_IRQ (ISAPNP_IRQTYPE_LEVEL_PLUS|ISAPNP_IRQTYPE_LEVEL_MINUS)
    235 	i->type = (i->flags & LEVEL_IRQ) ? IST_LEVEL : IST_EDGE;
    236 
    237 	if (i->bits == 0) {
    238 		i->num = 0;
    239 		return 0;
    240 	}
    241 
    242 	if (isa_intr_alloc(ic, i->bits, i->type, &irq) == 0) {
    243 		i->num = irq;
    244 		return 0;
    245 	}
    246 
    247 	return EINVAL;
    248 }
    249 
    250 /* isapnp_alloc_drq():
    251  *	Allocate a drq
    252  */
    253 static int
    254 isapnp_alloc_drq(ic, i)
    255 	isa_chipset_tag_t ic;
    256 	struct isapnp_pin *i;
    257 {
    258 	int b;
    259 
    260 	if (i->bits == 0) {
    261 		i->num = 0;
    262 		return 0;
    263 	}
    264 
    265 	for (b = 0; b < 8; b++)
    266 		if ((i->bits & (1 << b)) && isa_drq_isfree(ic, b)) {
    267 			i->num = b;
    268 			return 0;
    269 		}
    270 
    271 	return EINVAL;
    272 }
    273 
    274 /* isapnp_testconfig():
    275  *	Test/Allocate the regions used
    276  */
    277 static int
    278 isapnp_testconfig(iot, memt, ipa, alloc)
    279 	bus_space_tag_t iot, memt;
    280 	struct isapnp_attach_args *ipa;
    281 	int alloc;
    282 {
    283 	int nio = 0, nmem = 0, nmem32 = 0, nirq = 0, ndrq = 0;
    284 	int error = 0;
    285 
    286 #ifdef DEBUG_ISAPNP
    287 	isapnp_print_attach(ipa);
    288 #endif
    289 
    290 	for (; nio < ipa->ipa_nio; nio++) {
    291 		error = isapnp_alloc_region(iot, &ipa->ipa_io[nio]);
    292 		if (error)
    293 			goto bad;
    294 	}
    295 
    296 	for (; nmem < ipa->ipa_nmem; nmem++) {
    297 		error = isapnp_alloc_region(memt, &ipa->ipa_mem[nmem]);
    298 		if (error)
    299 			goto bad;
    300 	}
    301 
    302 	for (; nmem32 < ipa->ipa_nmem32; nmem32++) {
    303 		error = isapnp_alloc_region(memt, &ipa->ipa_mem32[nmem32]);
    304 		if (error)
    305 			goto bad;
    306 	}
    307 
    308 	for (; nirq < ipa->ipa_nirq; nirq++) {
    309 		error = isapnp_alloc_irq(ipa->ipa_ic, &ipa->ipa_irq[nirq]);
    310 		if (error)
    311 			goto bad;
    312 	}
    313 
    314 	for (; ndrq < ipa->ipa_ndrq; ndrq++) {
    315 		error = isapnp_alloc_drq(ipa->ipa_ic, &ipa->ipa_drq[ndrq]);
    316 		if (error)
    317 			goto bad;
    318 	}
    319 
    320 	if (alloc)
    321 		return error;
    322 
    323 bad:
    324 #ifdef notyet
    325 	for (ndrq--; ndrq >= 0; ndrq--)
    326 		isapnp_free_pin(&ipa->ipa_drq[ndrq]);
    327 
    328 	for (nirq--; nirq >= 0; nirq--)
    329 		isapnp_free_pin(&ipa->ipa_irq[nirq]);
    330 #endif
    331 
    332 	for (nmem32--; nmem32 >= 0; nmem32--)
    333 		isapnp_free_region(memt, &ipa->ipa_mem32[nmem32]);
    334 
    335 	for (nmem--; nmem >= 0; nmem--)
    336 		isapnp_free_region(memt, &ipa->ipa_mem[nmem]);
    337 
    338 	for (nio--; nio >= 0; nio--)
    339 		isapnp_free_region(iot, &ipa->ipa_io[nio]);
    340 
    341 	return error;
    342 }
    343 
    344 
    345 /* isapnp_config():
    346  *	Test/Allocate the regions used
    347  */
    348 int
    349 isapnp_config(iot, memt, ipa)
    350 	bus_space_tag_t iot, memt;
    351 	struct isapnp_attach_args *ipa;
    352 {
    353 	return isapnp_testconfig(iot, memt, ipa, 1);
    354 }
    355 
    356 
    357 /* isapnp_unconfig():
    358  *	Free the regions used
    359  */
    360 void
    361 isapnp_unconfig(iot, memt, ipa)
    362 	bus_space_tag_t iot, memt;
    363 	struct isapnp_attach_args *ipa;
    364 {
    365 	int i;
    366 
    367 #ifdef notyet
    368 	for (i = 0; i < ipa->ipa_ndrq; i++)
    369 		isapnp_free_pin(&ipa->ipa_drq[i]);
    370 
    371 	for (i = 0; i < ipa->ipa_nirq; i++)
    372 		isapnp_free_pin(&ipa->ipa_irq[i]);
    373 #endif
    374 
    375 	for (i = 0; i < ipa->ipa_nmem32; i++)
    376 		isapnp_free_region(memt, &ipa->ipa_mem32[i]);
    377 
    378 	for (i = 0; i < ipa->ipa_nmem; i++)
    379 		isapnp_free_region(memt, &ipa->ipa_mem[i]);
    380 
    381 	for (i = 0; i < ipa->ipa_nio; i++)
    382 		isapnp_free_region(iot, &ipa->ipa_io[i]);
    383 }
    384 
    385 
    386 /* isapnp_bestconfig():
    387  *	Return the best configuration for each logical device, remove and
    388  *	free all other configurations.
    389  */
    390 static struct isapnp_attach_args *
    391 isapnp_bestconfig(sc, ipa)
    392 	struct isapnp_softc *sc;
    393 	struct isapnp_attach_args **ipa;
    394 {
    395 	struct isapnp_attach_args *c, *best, *f = *ipa;
    396 	int error;
    397 
    398 	for (;;) {
    399 		if (f == NULL)
    400 			return NULL;
    401 
    402 #define SAMEDEV(a, b) (strcmp((a)->ipa_devlogic, (b)->ipa_devlogic) == 0)
    403 
    404 		/* Find the best config */
    405 		for (best = c = f; c != NULL; c = c->ipa_sibling) {
    406 			if (!SAMEDEV(c, f))
    407 				continue;
    408 			if (c->ipa_pref < best->ipa_pref)
    409 				best = c;
    410 		}
    411 
    412 		/*
    413 		 * Make sure the ISA chipset is initialized!  We need
    414 		 * it to test the best config!
    415 		 */
    416 		best->ipa_ic = sc->sc_ic;
    417 
    418 		/* Test the best config */
    419 		error = isapnp_testconfig(sc->sc_iot, sc->sc_memt, best, 0);
    420 
    421 		/* Remove this config from the list */
    422 		if (best == f)
    423 			f = f->ipa_sibling;
    424 		else {
    425 			for (c = f; c->ipa_sibling != best; c = c->ipa_sibling)
    426 				continue;
    427 			c->ipa_sibling = best->ipa_sibling;
    428 		}
    429 
    430 		if (error) {
    431 			best->ipa_pref = ISAPNP_DEP_CONFLICTING;
    432 
    433 			for (c = f; c != NULL; c = c->ipa_sibling)
    434 				if (c != best && SAMEDEV(c, best))
    435 					break;
    436 			/* Last config for this logical device is conflicting */
    437 			if (c == NULL) {
    438 				*ipa = f;
    439 				return best;
    440 			}
    441 
    442 			ISAPNP_FREE(best);
    443 			continue;
    444 		}
    445 		else {
    446 			/* Remove all other configs for this device */
    447 			struct isapnp_attach_args *l = NULL, *n = NULL, *d;
    448 
    449 			for (c = f; c; ) {
    450 				if (c == best)
    451 					continue;
    452 				d = c->ipa_sibling;
    453 				if (SAMEDEV(c, best))
    454 					ISAPNP_FREE(c);
    455 				else {
    456 					if (n)
    457 						n->ipa_sibling = c;
    458 
    459 					else
    460 						l = c;
    461 					n = c;
    462 					c->ipa_sibling = NULL;
    463 				}
    464 				c = d;
    465 			}
    466 			f = l;
    467 		}
    468 		*ipa = f;
    469 		return best;
    470 	}
    471 }
    472 
    473 
    474 /* isapnp_id_to_vendor():
    475  *	Convert a pnp ``compressed ascii'' vendor id to a string
    476  */
    477 char *
    478 isapnp_id_to_vendor(v, id)
    479 	char   *v;
    480 	const u_char *id;
    481 {
    482 	static const char hex[] = "0123456789ABCDEF";
    483 	char *p = v;
    484 
    485 	*p++ = 'A' + (id[0] >> 2) - 1;
    486 	*p++ = 'A' + ((id[0] & 3) << 3) + (id[1] >> 5) - 1;
    487 	*p++ = 'A' + (id[1] & 0x1f) - 1;
    488 	*p++ = hex[id[2] >> 4];
    489 	*p++ = hex[id[2] & 0x0f];
    490 	*p++ = hex[id[3] >> 4];
    491 	*p++ = hex[id[3] & 0x0f];
    492 	*p = '\0';
    493 
    494 	return v;
    495 }
    496 
    497 
    498 /* isapnp_print_region():
    499  *	Print a region allocation
    500  */
    501 static void
    502 isapnp_print_region(str, r, n)
    503 	const char *str;
    504 	struct isapnp_region *r;
    505 	size_t n;
    506 {
    507 	size_t i;
    508 
    509 	if (n == 0)
    510 		return;
    511 
    512 	printf(" %s ", str);
    513 	for (i = 0; i < n; i++, r++) {
    514 		printf("0x%x", r->base);
    515 		if (r->length)
    516 			printf("/%d", r->length);
    517 		if (i != n - 1)
    518 			printf(",");
    519 	}
    520 }
    521 
    522 
    523 /* isapnp_print_pin():
    524  *	Print an irq/drq assignment
    525  */
    526 static void
    527 isapnp_print_pin(str, p, n)
    528 	const char *str;
    529 	struct isapnp_pin *p;
    530 	size_t n;
    531 {
    532 	size_t i;
    533 
    534 	if (n == 0)
    535 		return;
    536 
    537 	printf(" %s ", str);
    538 	for (i = 0; i < n; i++, p++) {
    539 		printf("%d", p->num);
    540 		if (i != n - 1)
    541 			printf(",");
    542 	}
    543 }
    544 
    545 
    546 /* isapnp_print():
    547  *	Print the configuration line for an ISA PnP card.
    548  */
    549 static int
    550 isapnp_print(aux, str)
    551 	void *aux;
    552 	const char *str;
    553 {
    554 	struct isapnp_attach_args *ipa = aux;
    555 
    556 	if (str != NULL)
    557 		printf("%s: <%s, %s, %s, %s>",
    558 		    str, ipa->ipa_devident, ipa->ipa_devlogic,
    559 		    ipa->ipa_devcompat, ipa->ipa_devclass);
    560 
    561 	isapnp_print_region("port", ipa->ipa_io, ipa->ipa_nio);
    562 	isapnp_print_region("mem", ipa->ipa_mem, ipa->ipa_nmem);
    563 	isapnp_print_region("mem32", ipa->ipa_mem32, ipa->ipa_nmem32);
    564 	isapnp_print_pin("irq", ipa->ipa_irq, ipa->ipa_nirq);
    565 	isapnp_print_pin("drq", ipa->ipa_drq, ipa->ipa_ndrq);
    566 
    567 	return UNCONF;
    568 }
    569 
    570 
    571 #ifdef _KERNEL
    572 /* isapnp_submatch():
    573  *	Probe the logical device...
    574  */
    575 static int
    576 isapnp_submatch(parent, match, aux)
    577 	struct device *parent;
    578 	struct cfdata *match;
    579 	void *aux;
    580 {
    581 	struct cfdata *cf = (struct cfdata *) match;
    582 	return ((*cf->cf_attach->ca_match)(parent, match, aux));
    583 }
    584 
    585 
    586 /* isapnp_devmatch():
    587  *	Match a probed device with the information from the driver
    588  */
    589 int
    590 isapnp_devmatch(ipa, dinfo)
    591 	const struct isapnp_attach_args *ipa;
    592 	const struct isapnp_devinfo *dinfo;
    593 {
    594 	const char *const *name;
    595 
    596 	for (name = dinfo->devlogic; *name; name++)
    597 		if (strcmp(*name, ipa->ipa_devlogic) == 0)
    598 			return 1;
    599 
    600 	for (name = dinfo->devcompat; *name; name++)
    601 		if (strcmp(*name, ipa->ipa_devcompat) == 0)
    602 			return 1;
    603 
    604 	return 0;
    605 }
    606 
    607 
    608 /* isapnp_isa_attach_hook():
    609  *	This routine is called from the isa attach code and
    610  *	is a kludge; we are resetting all the cards here in order
    611  *	to undo any card configuration that the bios did for us, in order
    612  *	to avoid having the PnP devices match an isa probe. The correct
    613  *	way of doing this is to read the PnP BIOS and find the card settings
    614  *	from there. Unfortunately it is not as easy as it sounds.
    615  */
    616 void
    617 isapnp_isa_attach_hook(isa_sc)
    618 	struct isa_softc *isa_sc;
    619 {
    620 	struct isapnp_softc sc;
    621 
    622 	sc.sc_iot = isa_sc->sc_iot;
    623 	sc.sc_ncards = 0;
    624 
    625 	if (isapnp_map(&sc))
    626 		return;
    627 
    628 	isapnp_init(&sc);
    629 
    630 	isapnp_write_reg(&sc, ISAPNP_CONFIG_CONTROL, ISAPNP_CC_RESET_DRV);
    631 	DELAY(2000);
    632 
    633 	isapnp_unmap(&sc);
    634 }
    635 #endif
    636 
    637 
    638 /* isapnp_find():
    639  *	Probe and add cards
    640  */
    641 static int
    642 isapnp_find(sc, all)
    643 	struct isapnp_softc *sc;
    644 	int all;
    645 {
    646 	int p;
    647 
    648 	isapnp_init(sc);
    649 
    650 	isapnp_write_reg(sc, ISAPNP_CONFIG_CONTROL, ISAPNP_CC_RESET_DRV);
    651 	DELAY(2000);
    652 
    653 	isapnp_init(sc);
    654 	DELAY(2000);
    655 
    656 	for (p = ISAPNP_RDDATA_MIN; p <= ISAPNP_RDDATA_MAX; p += 4) {
    657 		sc->sc_read_port = p;
    658 		if (isapnp_map_readport(sc))
    659 			continue;
    660 		DPRINTF(("%s: Trying port %x\r", sc->sc_dev.dv_xname, p));
    661 		if (isapnp_findcard(sc))
    662 			break;
    663 		isapnp_unmap_readport(sc);
    664 	}
    665 
    666 	if (p > ISAPNP_RDDATA_MAX) {
    667 		sc->sc_read_port = 0;
    668 		return 0;
    669 	}
    670 
    671 	if (all)
    672 		while (isapnp_findcard(sc))
    673 			continue;
    674 
    675 	return 1;
    676 }
    677 
    678 
    679 /* isapnp_configure():
    680  *	Configure a PnP card
    681  *	XXX: The memory configuration code is wrong. We need to check the
    682  *	     range/length bit an do appropriate sets.
    683  */
    684 static void
    685 isapnp_configure(sc, ipa)
    686 	struct isapnp_softc *sc;
    687 	const struct isapnp_attach_args *ipa;
    688 {
    689 	int i;
    690 	static u_char isapnp_mem_range[] = ISAPNP_MEM_DESC;
    691 	static u_char isapnp_io_range[] = ISAPNP_IO_DESC;
    692 	static u_char isapnp_irq_range[] = ISAPNP_IRQ_DESC;
    693 	static u_char isapnp_drq_range[] = ISAPNP_DRQ_DESC;
    694 	static u_char isapnp_mem32_range[] = ISAPNP_MEM32_DESC;
    695 	const struct isapnp_region *r;
    696 	const struct isapnp_pin *p;
    697 	struct isapnp_region rz;
    698 	struct isapnp_pin pz;
    699 
    700 	memset(&pz, 0, sizeof(pz));
    701 	memset(&rz, 0, sizeof(rz));
    702 
    703 #define B0(a) ((a) & 0xff)
    704 #define B1(a) (((a) >> 8) & 0xff)
    705 #define B2(a) (((a) >> 16) & 0xff)
    706 #define B3(a) (((a) >> 24) & 0xff)
    707 
    708 	for (i = 0; i < sizeof(isapnp_io_range); i++) {
    709 		if (i < ipa->ipa_nio)
    710 			r = &ipa->ipa_io[i];
    711 		else
    712 			r = &rz;
    713 
    714 		isapnp_write_reg(sc,
    715 		    isapnp_io_range[i] + ISAPNP_IO_BASE_15_8, B1(r->base));
    716 		isapnp_write_reg(sc,
    717 		    isapnp_io_range[i] + ISAPNP_IO_BASE_7_0, B0(r->base));
    718 	}
    719 
    720 	for (i = 0; i < sizeof(isapnp_mem_range); i++) {
    721 		if (i < ipa->ipa_nmem)
    722 			r = &ipa->ipa_mem[i];
    723 		else
    724 			r = &rz;
    725 
    726 		isapnp_write_reg(sc,
    727 		    isapnp_mem_range[i] + ISAPNP_MEM_BASE_23_16, B2(r->base));
    728 		isapnp_write_reg(sc,
    729 		    isapnp_mem_range[i] + ISAPNP_MEM_BASE_15_8, B1(r->base));
    730 
    731 		isapnp_write_reg(sc,
    732 		    isapnp_mem_range[i] + ISAPNP_MEM_LRANGE_23_16,
    733 		    B2(r->length));
    734 		isapnp_write_reg(sc,
    735 		    isapnp_mem_range[i] + ISAPNP_MEM_LRANGE_15_8,
    736 		    B1(r->length));
    737 	}
    738 
    739 	for (i = 0; i < sizeof(isapnp_irq_range); i++) {
    740 		u_char v;
    741 
    742 		if (i < ipa->ipa_nirq)
    743 			p = &ipa->ipa_irq[i];
    744 		else
    745 			p = &pz;
    746 
    747 		isapnp_write_reg(sc,
    748 		    isapnp_irq_range[i] + ISAPNP_IRQ_NUMBER, p->num);
    749 
    750 		switch (p->flags) {
    751 		case ISAPNP_IRQTYPE_LEVEL_PLUS:
    752 			v = ISAPNP_IRQ_LEVEL|ISAPNP_IRQ_HIGH;
    753 			break;
    754 
    755 		case ISAPNP_IRQTYPE_EDGE_PLUS:
    756 			v = ISAPNP_IRQ_HIGH;
    757 			break;
    758 
    759 		case ISAPNP_IRQTYPE_LEVEL_MINUS:
    760 			v = ISAPNP_IRQ_LEVEL;
    761 			break;
    762 
    763 		default:
    764 		case ISAPNP_IRQTYPE_EDGE_MINUS:
    765 			v = 0;
    766 			break;
    767 		}
    768 		isapnp_write_reg(sc,
    769 		    isapnp_irq_range[i] + ISAPNP_IRQ_CONTROL, v);
    770 	}
    771 
    772 	for (i = 0; i < sizeof(isapnp_drq_range); i++) {
    773 		u_char v;
    774 
    775 		if (i < ipa->ipa_ndrq)
    776 			v = ipa->ipa_drq[i].num;
    777 		else
    778 			v = 4;
    779 
    780 		isapnp_write_reg(sc, isapnp_drq_range[i], v);
    781 	}
    782 
    783 	for (i = 0; i < sizeof(isapnp_mem32_range); i++) {
    784 		if (i < ipa->ipa_nmem32)
    785 			r = &ipa->ipa_mem32[i];
    786 		else
    787 			r = &rz;
    788 
    789 		isapnp_write_reg(sc,
    790 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_31_24,
    791 		    B3(r->base));
    792 		isapnp_write_reg(sc,
    793 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_23_16,
    794 		    B2(r->base));
    795 		isapnp_write_reg(sc,
    796 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_15_8,
    797 		    B1(r->base));
    798 		isapnp_write_reg(sc,
    799 		    isapnp_mem32_range[i] + ISAPNP_MEM32_BASE_7_0,
    800 		    B0(r->base));
    801 
    802 		isapnp_write_reg(sc,
    803 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_31_24,
    804 		    B3(r->length));
    805 		isapnp_write_reg(sc,
    806 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_23_16,
    807 		    B2(r->length));
    808 		isapnp_write_reg(sc,
    809 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_15_8,
    810 		    B1(r->length));
    811 		isapnp_write_reg(sc,
    812 		    isapnp_mem32_range[i] + ISAPNP_MEM32_LRANGE_7_0,
    813 		    B0(r->length));
    814 	}
    815 }
    816 
    817 
    818 /* isapnp_match():
    819  *	Probe routine
    820  */
    821 static int
    822 isapnp_match(parent, match, aux)
    823 	struct device *parent;
    824 	struct cfdata *match;
    825 	void *aux;
    826 {
    827 	struct isapnp_softc sc;
    828 	struct isa_attach_args *ia = aux;
    829 	struct isapnp_probe_cookie *ipc;
    830 
    831 	/*
    832 	 * Ensure we only probe ISA PnP once; we don't actually consume
    833 	 * bus resources, so we have to prevent being cloned forever.
    834 	 */
    835 	for (ipc = LIST_FIRST(&isapnp_probes); ipc != NULL;
    836 	     ipc = LIST_NEXT(ipc, ipc_link))
    837 		if (ipc->ipc_parent == parent)
    838 			return (0);
    839 
    840 	ipc = malloc(sizeof(*ipc), M_DEVBUF, M_NOWAIT);
    841 	if (ipc == NULL)
    842 		panic("isapnp_match: can't allocate probe cookie");
    843 
    844 	ipc->ipc_parent = parent;
    845 	LIST_INSERT_HEAD(&isapnp_probes, ipc, ipc_link);
    846 
    847 	sc.sc_iot = ia->ia_iot;
    848 	(void) strcpy(sc.sc_dev.dv_xname, "(isapnp probe)");
    849 
    850 	if (isapnp_map(&sc))
    851 		return 0;
    852 
    853 	isapnp_unmap(&sc);
    854 
    855 	/*
    856 	 * We always match.  We must let all legacy ISA devices map
    857 	 * their address spaces before we look for a read port.
    858 	 */
    859 	ia->ia_iobase = ISAPNP_ADDR;
    860 	ia->ia_iosize = 1;
    861 
    862 	return (1);
    863 }
    864 
    865 
    866 /* isapnp_attach
    867  *	Attach the PnP `bus'.
    868  */
    869 static void
    870 isapnp_attach(parent, self, aux)
    871 	struct device *parent, *self;
    872 	void *aux;
    873 {
    874 	struct isapnp_softc *sc = (struct isapnp_softc *) self;
    875 	struct isa_attach_args *ia = aux;
    876 
    877 	sc->sc_iot = ia->ia_iot;
    878 	sc->sc_memt = ia->ia_memt;
    879 	sc->sc_ic = ia->ia_ic;
    880 	sc->sc_dmat = ia->ia_dmat;
    881 	sc->sc_ncards = 0;
    882 
    883 	printf(": ISA Plug 'n Play device support\n");
    884 
    885 	if (isapnp_map(sc)) {
    886 		printf("%s: unable to map PnP register\n",
    887 		    sc->sc_dev.dv_xname);
    888 		return;
    889 	}
    890 
    891 #ifdef _KERNEL
    892 	/*
    893 	 * Defer configuration until the rest of the ISA devices have
    894 	 * attached themselves.
    895 	 */
    896 	config_defer(self, isapnp_callback);
    897 #else
    898 	isapnp_callback(self);
    899 #endif
    900 }
    901 
    902 /* isapnp_callback
    903  *	Find and attach PnP cards.
    904  */
    905 void
    906 isapnp_callback(self)
    907 	struct device *self;
    908 {
    909 	struct isapnp_softc *sc = (struct isapnp_softc *)self;
    910 	struct isapnp_attach_args *ipa, *lpa;
    911 	int found, c, d;
    912 
    913 	/*
    914 	 * Look for cards.  If none are found, we say so and just return.
    915 	 */
    916 	found = isapnp_find(sc, 1);
    917 
    918 	printf("%s: read port 0x%x\n", sc->sc_dev.dv_xname, sc->sc_read_port);
    919 	if (found == 0) {
    920 		printf("%s: no PnP cards found\n", sc->sc_dev.dv_xname);
    921 		return;
    922 	}
    923 
    924 	/*
    925 	 * Now configure all of the cards.
    926 	 */
    927 	for (c = 0; c < sc->sc_ncards; c++) {
    928 		/* Good morning card c */
    929 		isapnp_write_reg(sc, ISAPNP_WAKE, c + 1);
    930 
    931 		if ((ipa = isapnp_get_resource(sc, c)) == NULL)
    932 			continue;
    933 
    934 		DPRINTF(("Selecting attachments\n"));
    935 		for (d = 0;
    936 		    (lpa = isapnp_bestconfig(sc, &ipa)) != NULL; d++) {
    937 			isapnp_write_reg(sc, ISAPNP_LOGICAL_DEV_NUM, d);
    938 			isapnp_configure(sc, lpa);
    939 #ifdef DEBUG_ISAPNP
    940 			{
    941 				struct isapnp_attach_args pa;
    942 
    943 				isapnp_get_config(sc, &pa);
    944 				isapnp_print_config(&pa);
    945 			}
    946 #endif
    947 
    948 			DPRINTF(("%s: configuring <%s, %s, %s, %s>\n",
    949 			    sc->sc_dev.dv_xname,
    950 			    lpa->ipa_devident, lpa->ipa_devlogic,
    951 			    lpa->ipa_devcompat, lpa->ipa_devclass));
    952 			if (lpa->ipa_pref == ISAPNP_DEP_CONFLICTING) {
    953 				printf("%s: <%s, %s, %s, %s> ignored; %s\n",
    954 				    sc->sc_dev.dv_xname,
    955 				    lpa->ipa_devident, lpa->ipa_devlogic,
    956 				    lpa->ipa_devcompat, lpa->ipa_devclass,
    957 				    "resource conflict");
    958 				ISAPNP_FREE(lpa);
    959 				continue;
    960 			}
    961 
    962 			lpa->ipa_ic = sc->sc_ic;
    963 			lpa->ipa_iot = sc->sc_iot;
    964 			lpa->ipa_memt = sc->sc_memt;
    965 			lpa->ipa_dmat = sc->sc_dmat;
    966 
    967 			isapnp_write_reg(sc, ISAPNP_ACTIVATE, 1);
    968 #ifdef _KERNEL
    969 			if (config_found_sm(self, lpa, isapnp_print,
    970 			    isapnp_submatch) == NULL)
    971 				isapnp_write_reg(sc, ISAPNP_ACTIVATE, 0);
    972 #else
    973 			isapnp_print(lpa, NULL);
    974 			printf("\n");
    975 #endif
    976 			ISAPNP_FREE(lpa);
    977 		}
    978 		isapnp_write_reg(sc, ISAPNP_WAKE, 0);    /* Good night cards */
    979 	}
    980 }
    981