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