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