Home | History | Annotate | Line # | Download | only in pcmcia
pcmcia.c revision 1.82.22.1
      1  1.82.22.1     joerg /*	$NetBSD: pcmcia.c,v 1.82.22.1 2007/09/04 15:05:21 joerg Exp $	*/
      2       1.42   mycroft 
      3       1.42   mycroft /*
      4       1.42   mycroft  * Copyright (c) 2004 Charles M. Hannum.  All rights reserved.
      5       1.42   mycroft  *
      6       1.42   mycroft  * Redistribution and use in source and binary forms, with or without
      7       1.42   mycroft  * modification, are permitted provided that the following conditions
      8       1.42   mycroft  * are met:
      9       1.42   mycroft  * 1. Redistributions of source code must retain the above copyright
     10       1.42   mycroft  *    notice, this list of conditions and the following disclaimer.
     11       1.42   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.42   mycroft  *    notice, this list of conditions and the following disclaimer in the
     13       1.42   mycroft  *    documentation and/or other materials provided with the distribution.
     14       1.42   mycroft  * 3. All advertising materials mentioning features or use of this software
     15       1.42   mycroft  *    must display the following acknowledgement:
     16       1.42   mycroft  *      This product includes software developed by Charles M. Hannum.
     17       1.42   mycroft  * 4. The name of the author may not be used to endorse or promote products
     18       1.42   mycroft  *    derived from this software without specific prior written permission.
     19       1.42   mycroft  */
     20        1.2   thorpej 
     21        1.2   thorpej /*
     22        1.2   thorpej  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
     23        1.2   thorpej  *
     24        1.2   thorpej  * Redistribution and use in source and binary forms, with or without
     25        1.2   thorpej  * modification, are permitted provided that the following conditions
     26        1.2   thorpej  * are met:
     27        1.2   thorpej  * 1. Redistributions of source code must retain the above copyright
     28        1.2   thorpej  *    notice, this list of conditions and the following disclaimer.
     29        1.2   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     30        1.2   thorpej  *    notice, this list of conditions and the following disclaimer in the
     31        1.2   thorpej  *    documentation and/or other materials provided with the distribution.
     32        1.2   thorpej  * 3. All advertising materials mentioning features or use of this software
     33        1.2   thorpej  *    must display the following acknowledgement:
     34        1.2   thorpej  *	This product includes software developed by Marc Horowitz.
     35        1.2   thorpej  * 4. The name of the author may not be used to endorse or promote products
     36        1.2   thorpej  *    derived from this software without specific prior written permission.
     37        1.2   thorpej  *
     38        1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     39        1.2   thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     40        1.2   thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     41        1.2   thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     42        1.2   thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     43        1.2   thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     44        1.2   thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     45        1.2   thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     46        1.2   thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     47        1.2   thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     48        1.2   thorpej  */
     49       1.26     lukem 
     50       1.26     lukem #include <sys/cdefs.h>
     51  1.82.22.1     joerg __KERNEL_RCSID(0, "$NetBSD: pcmcia.c,v 1.82.22.1 2007/09/04 15:05:21 joerg Exp $");
     52        1.2   thorpej 
     53        1.2   thorpej #include "opt_pcmciaverbose.h"
     54        1.2   thorpej 
     55        1.2   thorpej #include <sys/param.h>
     56        1.2   thorpej #include <sys/systm.h>
     57        1.2   thorpej #include <sys/device.h>
     58        1.2   thorpej 
     59  1.82.22.1     joerg #include <net/if.h>
     60  1.82.22.1     joerg 
     61        1.2   thorpej #include <dev/pcmcia/pcmciareg.h>
     62        1.2   thorpej #include <dev/pcmcia/pcmciachip.h>
     63        1.2   thorpej #include <dev/pcmcia/pcmciavar.h>
     64       1.21       uch #ifdef IT8368E_LEGACY_MODE /* XXX -uch */
     65       1.21       uch #include <arch/hpcmips/dev/it8368var.h>
     66       1.21       uch #endif
     67        1.2   thorpej 
     68        1.3     enami #include "locators.h"
     69        1.3     enami 
     70        1.2   thorpej #ifdef PCMCIADEBUG
     71        1.2   thorpej int	pcmcia_debug = 0;
     72        1.2   thorpej #define	DPRINTF(arg) if (pcmcia_debug) printf arg
     73        1.2   thorpej #else
     74        1.2   thorpej #define	DPRINTF(arg)
     75        1.2   thorpej #endif
     76        1.2   thorpej 
     77        1.2   thorpej #ifdef PCMCIAVERBOSE
     78        1.2   thorpej int	pcmcia_verbose = 1;
     79        1.2   thorpej #else
     80        1.2   thorpej int	pcmcia_verbose = 0;
     81        1.2   thorpej #endif
     82        1.2   thorpej 
     83       1.72     perry int	pcmcia_match(struct device *, struct cfdata *, void *);
     84       1.72     perry void	pcmcia_attach(struct device *, struct device *, void *);
     85       1.71  drochner int	pcmcia_rescan(struct device *, const char *, const int *);
     86       1.71  drochner void	pcmcia_childdetached(struct device *, struct device *);
     87       1.72     perry int	pcmcia_print(void *, const char *);
     88        1.2   thorpej 
     89       1.71  drochner CFATTACH_DECL2(pcmcia, sizeof(struct pcmcia_softc),
     90       1.71  drochner     pcmcia_match, pcmcia_attach, NULL, NULL,
     91       1.71  drochner     pcmcia_rescan, pcmcia_childdetached);
     92        1.2   thorpej 
     93        1.2   thorpej int
     94        1.2   thorpej pcmcia_ccr_read(pf, ccr)
     95        1.2   thorpej 	struct pcmcia_function *pf;
     96        1.2   thorpej 	int ccr;
     97        1.2   thorpej {
     98        1.2   thorpej 
     99        1.2   thorpej 	return (bus_space_read_1(pf->pf_ccrt, pf->pf_ccrh,
    100       1.43   mycroft 	    pf->pf_ccr_offset + ccr * 2));
    101        1.2   thorpej }
    102        1.2   thorpej 
    103        1.2   thorpej void
    104        1.2   thorpej pcmcia_ccr_write(pf, ccr, val)
    105        1.2   thorpej 	struct pcmcia_function *pf;
    106        1.2   thorpej 	int ccr;
    107        1.2   thorpej 	int val;
    108        1.2   thorpej {
    109        1.2   thorpej 
    110       1.43   mycroft 	if (pf->ccr_mask & (1 << ccr)) {
    111        1.2   thorpej 		bus_space_write_1(pf->pf_ccrt, pf->pf_ccrh,
    112       1.43   mycroft 		    pf->pf_ccr_offset + ccr * 2, val);
    113        1.2   thorpej 	}
    114        1.2   thorpej }
    115        1.2   thorpej 
    116        1.2   thorpej int
    117       1.82  christos pcmcia_match(struct device *parent, struct cfdata *match, void *aux)
    118        1.2   thorpej {
    119       1.14      haya 	struct pcmciabus_attach_args *paa = aux;
    120       1.14      haya 
    121       1.30   thorpej 	if (strcmp(paa->paa_busname, match->cf_name)) {
    122       1.14      haya 	    return 0;
    123       1.14      haya 	}
    124        1.2   thorpej 	/* if the autoconfiguration got this far, there's a socket here */
    125        1.2   thorpej 	return (1);
    126        1.2   thorpej }
    127        1.2   thorpej 
    128        1.2   thorpej void
    129       1.82  christos pcmcia_attach(struct device *parent, struct device *self, void *aux)
    130        1.2   thorpej {
    131        1.2   thorpej 	struct pcmciabus_attach_args *paa = aux;
    132        1.2   thorpej 	struct pcmcia_softc *sc = (struct pcmcia_softc *) self;
    133        1.2   thorpej 
    134        1.2   thorpej 	printf("\n");
    135        1.2   thorpej 
    136        1.2   thorpej 	sc->pct = paa->pct;
    137        1.2   thorpej 	sc->pch = paa->pch;
    138        1.2   thorpej 	sc->iobase = paa->iobase;
    139        1.2   thorpej 	sc->iosize = paa->iosize;
    140        1.2   thorpej 
    141        1.2   thorpej 	sc->ih = NULL;
    142        1.2   thorpej }
    143        1.2   thorpej 
    144        1.2   thorpej int
    145        1.2   thorpej pcmcia_card_attach(dev)
    146        1.2   thorpej 	struct device *dev;
    147        1.2   thorpej {
    148        1.2   thorpej 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    149        1.2   thorpej 	struct pcmcia_function *pf;
    150       1.71  drochner 	int error;
    151       1.77  drochner 	static const int wildcard[PCMCIACF_NLOCS] = {
    152       1.81  drochner 		PCMCIACF_FUNCTION_DEFAULT
    153       1.71  drochner 	};
    154        1.2   thorpej 
    155        1.2   thorpej 	/*
    156        1.2   thorpej 	 * this is here so that when socket_enable calls gettype, trt happens
    157        1.2   thorpej 	 */
    158       1.29     lukem 	SIMPLEQ_FIRST(&sc->card.pf_head) = NULL;
    159        1.2   thorpej 
    160       1.59   mycroft 	pcmcia_socket_enable(dev);
    161        1.2   thorpej 
    162        1.2   thorpej 	pcmcia_read_cis(sc);
    163       1.59   mycroft 	pcmcia_check_cis_quirks(sc);
    164        1.2   thorpej 
    165       1.71  drochner #if 1 /* XXX remove this, done below ??? */
    166        1.2   thorpej 	/*
    167        1.2   thorpej 	 * bail now if the card has no functions, or if there was an error in
    168        1.2   thorpej 	 * the cis.
    169        1.2   thorpej 	 */
    170       1.64   mycroft 	if (sc->card.error ||
    171       1.64   mycroft 	    SIMPLEQ_EMPTY(&sc->card.pf_head)) {
    172       1.64   mycroft 		printf("%s: card appears to have bogus CIS\n",
    173       1.64   mycroft 		    sc->dev.dv_xname);
    174       1.71  drochner 		error = EIO;
    175       1.59   mycroft 		goto done;
    176       1.64   mycroft 	}
    177       1.71  drochner #endif
    178        1.2   thorpej 
    179        1.2   thorpej 	if (pcmcia_verbose)
    180        1.2   thorpej 		pcmcia_print_cis(sc);
    181        1.2   thorpej 
    182       1.29     lukem 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    183       1.29     lukem 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    184        1.2   thorpej 			continue;
    185        1.2   thorpej 
    186       1.11   thorpej #ifdef DIAGNOSTIC
    187       1.11   thorpej 		if (pf->child != NULL) {
    188       1.11   thorpej 			printf("%s: %s still attached to function %d!\n",
    189       1.11   thorpej 			    sc->dev.dv_xname, pf->child->dv_xname,
    190       1.11   thorpej 			    pf->number);
    191       1.11   thorpej 			panic("pcmcia_card_attach");
    192       1.11   thorpej 		}
    193       1.11   thorpej #endif
    194        1.2   thorpej 		pf->sc = sc;
    195       1.11   thorpej 		pf->child = NULL;
    196        1.2   thorpej 		pf->cfe = NULL;
    197       1.52   mycroft 		pf->pf_ih = NULL;
    198        1.2   thorpej 	}
    199        1.2   thorpej 
    200       1.71  drochner 	error = pcmcia_rescan(dev, "pcmcia", wildcard);
    201       1.71  drochner done:
    202       1.71  drochner 	pcmcia_socket_disable(dev);
    203       1.71  drochner 	return (error);
    204       1.71  drochner }
    205       1.71  drochner 
    206       1.71  drochner int
    207       1.82  christos pcmcia_rescan(struct device *self, const char *ifattr,
    208       1.80  christos     const int *locators)
    209       1.71  drochner {
    210       1.71  drochner 	struct pcmcia_softc *sc = (struct pcmcia_softc *)self;
    211       1.71  drochner 	struct pcmcia_function *pf;
    212       1.71  drochner 	struct pcmcia_attach_args paa;
    213       1.75  drochner 	int locs[PCMCIACF_NLOCS];
    214       1.71  drochner 
    215       1.71  drochner 	if (sc->card.error ||
    216       1.71  drochner 	    SIMPLEQ_EMPTY(&sc->card.pf_head)) {
    217       1.71  drochner 		/* XXX silently ignore if no card present? */
    218       1.71  drochner 		return (EIO);
    219       1.71  drochner 	}
    220       1.71  drochner 
    221       1.29     lukem 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    222       1.29     lukem 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    223        1.2   thorpej 			continue;
    224        1.2   thorpej 
    225       1.71  drochner 		if ((locators[PCMCIACF_FUNCTION] != PCMCIACF_FUNCTION_DEFAULT)
    226       1.71  drochner 		    && (locators[PCMCIACF_FUNCTION] != pf->number))
    227       1.71  drochner 			continue;
    228       1.71  drochner 
    229       1.71  drochner 		if (pf->child)
    230       1.71  drochner 			continue;
    231       1.71  drochner 
    232       1.75  drochner 		locs[PCMCIACF_FUNCTION] = pf->number;
    233       1.71  drochner 
    234        1.2   thorpej 		paa.manufacturer = sc->card.manufacturer;
    235        1.2   thorpej 		paa.product = sc->card.product;
    236        1.2   thorpej 		paa.card = &sc->card;
    237        1.2   thorpej 		paa.pf = pf;
    238        1.2   thorpej 
    239       1.75  drochner 		pf->child = config_found_sm_loc(self, "pcmcia", locs, &paa,
    240       1.76  drochner 						pcmcia_print,
    241       1.76  drochner 						config_stdsubmatch);
    242        1.2   thorpej 	}
    243        1.2   thorpej 
    244       1.71  drochner 	return (0);
    245        1.2   thorpej }
    246        1.2   thorpej 
    247        1.2   thorpej void
    248       1.11   thorpej pcmcia_card_detach(dev, flags)
    249        1.2   thorpej 	struct device *dev;
    250       1.11   thorpej 	int flags;		/* DETACH_* flags */
    251        1.2   thorpej {
    252       1.10   thorpej 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    253       1.11   thorpej 	struct pcmcia_function *pf;
    254       1.11   thorpej 	int error;
    255       1.10   thorpej 
    256       1.11   thorpej 	/*
    257       1.11   thorpej 	 * We are running on either the PCMCIA socket's event thread
    258       1.11   thorpej 	 * or in user context detaching a device by user request.
    259       1.10   thorpej 	 */
    260       1.29     lukem 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    261       1.69   mycroft 		pf->pf_flags |= PFF_DETACHED;
    262       1.29     lukem 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    263       1.11   thorpej 			continue;
    264       1.11   thorpej 		if (pf->child == NULL)
    265       1.11   thorpej 			continue;
    266       1.11   thorpej 		DPRINTF(("%s: detaching %s (function %d)\n",
    267       1.11   thorpej 		    sc->dev.dv_xname, pf->child->dv_xname, pf->number));
    268       1.11   thorpej 		if ((error = config_detach(pf->child, flags)) != 0) {
    269       1.11   thorpej 			printf("%s: error %d detaching %s (function %d)\n",
    270       1.11   thorpej 			    sc->dev.dv_xname, error, pf->child->dv_xname,
    271       1.11   thorpej 			    pf->number);
    272       1.71  drochner 		}
    273       1.11   thorpej 	}
    274       1.53   mycroft 
    275       1.53   mycroft 	if (sc->sc_enabled_count != 0) {
    276       1.53   mycroft #ifdef DIAGNOSTIC
    277       1.53   mycroft 		printf("pcmcia_card_detach: enabled_count should be 0 here??\n");
    278       1.53   mycroft #endif
    279       1.53   mycroft 		pcmcia_chip_socket_disable(sc->pct, sc->pch);
    280       1.53   mycroft 		sc->sc_enabled_count = 0;
    281       1.53   mycroft 	}
    282       1.11   thorpej }
    283       1.11   thorpej 
    284       1.11   thorpej void
    285       1.71  drochner pcmcia_childdetached(struct device *self, struct device *child)
    286       1.71  drochner {
    287       1.71  drochner 	struct pcmcia_softc *sc = (struct pcmcia_softc *)self;
    288       1.71  drochner 	struct pcmcia_function *pf;
    289       1.71  drochner 
    290       1.71  drochner 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    291       1.71  drochner 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    292       1.71  drochner 			continue;
    293       1.71  drochner 		if (pf->child == child) {
    294       1.79   thorpej 			KASSERT(device_locator(child, PCMCIACF_FUNCTION)
    295       1.71  drochner 				== pf->number);
    296       1.71  drochner 			pf->child = NULL;
    297       1.71  drochner 			return;
    298       1.71  drochner 		}
    299       1.71  drochner 	}
    300       1.71  drochner 
    301       1.71  drochner 	printf("%s: pcmcia_childdetached: %s not found\n",
    302       1.71  drochner 	       self->dv_xname, child->dv_xname);
    303       1.71  drochner }
    304       1.71  drochner 
    305       1.71  drochner void
    306       1.11   thorpej pcmcia_card_deactivate(dev)
    307       1.11   thorpej 	struct device *dev;
    308       1.11   thorpej {
    309       1.11   thorpej 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
    310       1.11   thorpej 	struct pcmcia_function *pf;
    311       1.10   thorpej 
    312       1.11   thorpej 	/*
    313       1.11   thorpej 	 * We're in the chip's card removal interrupt handler.
    314       1.11   thorpej 	 * Deactivate the child driver.  The PCMCIA socket's
    315       1.11   thorpej 	 * event thread will run later to finish the detach.
    316       1.11   thorpej 	 */
    317       1.29     lukem 	SIMPLEQ_FOREACH(pf, &sc->card.pf_head, pf_list) {
    318       1.29     lukem 		if (SIMPLEQ_EMPTY(&pf->cfe_head))
    319       1.11   thorpej 			continue;
    320       1.11   thorpej 		if (pf->child == NULL)
    321       1.11   thorpej 			continue;
    322       1.11   thorpej 		DPRINTF(("%s: deactivating %s (function %d)\n",
    323       1.11   thorpej 		    sc->dev.dv_xname, pf->child->dv_xname, pf->number));
    324       1.11   thorpej 		config_deactivate(pf->child);
    325       1.11   thorpej 	}
    326        1.2   thorpej }
    327        1.2   thorpej 
    328        1.2   thorpej int
    329        1.2   thorpej pcmcia_print(arg, pnp)
    330        1.2   thorpej 	void *arg;
    331        1.2   thorpej 	const char *pnp;
    332        1.2   thorpej {
    333        1.2   thorpej 	struct pcmcia_attach_args *pa = arg;
    334        1.2   thorpej 	struct pcmcia_softc *sc = pa->pf->sc;
    335        1.2   thorpej 	struct pcmcia_card *card = &sc->card;
    336       1.18  augustss 	char devinfo[256];
    337        1.2   thorpej 
    338       1.38   mycroft 	if (pnp)
    339       1.38   mycroft 		aprint_normal("%s", pnp);
    340       1.38   mycroft 
    341       1.38   mycroft 	pcmcia_devinfo(card, !!pnp, devinfo, sizeof(devinfo));
    342       1.38   mycroft 
    343       1.57   mycroft 	aprint_normal(" function %d: %s\n", pa->pf->number, devinfo);
    344        1.2   thorpej 
    345        1.2   thorpej 	return (UNCONF);
    346       1.18  augustss }
    347       1.18  augustss 
    348       1.18  augustss void
    349       1.18  augustss pcmcia_devinfo(card, showhex, cp, cplen)
    350       1.18  augustss 	struct pcmcia_card *card;
    351       1.19     enami 	int showhex;
    352       1.18  augustss 	char *cp;
    353       1.38   mycroft 	size_t cplen;
    354       1.18  augustss {
    355       1.18  augustss 	int i, n;
    356       1.18  augustss 
    357       1.37   mycroft 	if (cplen > 1) {
    358       1.37   mycroft 		*cp++ = '<';
    359       1.38   mycroft 		*cp = '\0';
    360       1.37   mycroft 		cplen--;
    361       1.37   mycroft 	}
    362       1.37   mycroft 
    363       1.37   mycroft 	for (i = 0; i < 4 && card->cis1_info[i] != NULL && cplen > 1; i++) {
    364       1.39   mycroft 		n = snprintf(cp, cplen, "%s%s", i ? ", " : "",
    365       1.18  augustss 		        card->cis1_info[i]);
    366       1.18  augustss 		cp += n;
    367       1.38   mycroft 		if (cplen < n)
    368       1.38   mycroft 			return;
    369       1.18  augustss 		cplen -= n;
    370       1.18  augustss 	}
    371       1.37   mycroft 
    372       1.37   mycroft 	if (cplen > 1) {
    373       1.37   mycroft 		*cp++ = '>';
    374       1.38   mycroft 		*cp = '\0';
    375       1.37   mycroft 		cplen--;
    376       1.37   mycroft 	}
    377       1.37   mycroft 
    378       1.37   mycroft 	if (showhex && cplen > 1)
    379       1.37   mycroft 		snprintf(cp, cplen, " (manufacturer 0x%04x, product 0x%04x)",
    380       1.37   mycroft 		    card->manufacturer, card->product);
    381       1.16       cgd }
    382       1.16       cgd 
    383       1.56   mycroft const void *
    384       1.56   mycroft pcmcia_product_lookup(pa, tab, nent, ent_size, matchfn)
    385       1.16       cgd 	struct pcmcia_attach_args *pa;
    386       1.56   mycroft 	const void *tab;
    387       1.56   mycroft 	size_t nent;
    388       1.16       cgd 	size_t ent_size;
    389       1.16       cgd 	pcmcia_product_match_fn matchfn;
    390       1.16       cgd {
    391       1.56   mycroft         const struct pcmcia_product *pp;
    392       1.56   mycroft 	int n;
    393       1.16       cgd 	int matches;
    394       1.16       cgd 
    395       1.16       cgd #ifdef DIAGNOSTIC
    396       1.56   mycroft 	if (sizeof *pp > ent_size)
    397       1.73     perry 		panic("pcmcia_product_lookup: bogus ent_size %ld",
    398       1.17   nathanw 		      (long) ent_size);
    399       1.16       cgd #endif
    400       1.16       cgd 
    401       1.56   mycroft         for (pp = tab, n = nent; n; pp = (const struct pcmcia_product *)
    402       1.56   mycroft 	      ((const char *)pp + ent_size), n--) {
    403       1.56   mycroft 		/* see if it matches vendor/product */
    404       1.58   mycroft 		matches = 0;
    405       1.56   mycroft 		if ((pp->pp_vendor != PCMCIA_VENDOR_INVALID &&
    406       1.56   mycroft 		     pp->pp_vendor == pa->manufacturer) &&
    407       1.56   mycroft 		    (pp->pp_product != PCMCIA_PRODUCT_INVALID &&
    408       1.56   mycroft 		     pp->pp_product == pa->product))
    409       1.56   mycroft 			matches = 1;
    410       1.58   mycroft 		if ((pp->pp_cisinfo[0] && pa->card->cis1_info[0] &&
    411       1.56   mycroft 		          !strcmp(pp->pp_cisinfo[0], pa->card->cis1_info[0])) &&
    412       1.56   mycroft 		         (pp->pp_cisinfo[1] && pa->card->cis1_info[1] &&
    413       1.56   mycroft 		          !strcmp(pp->pp_cisinfo[1], pa->card->cis1_info[1])) &&
    414       1.56   mycroft 		         (!pp->pp_cisinfo[2] || (pa->card->cis1_info[2] &&
    415       1.56   mycroft 		           !strcmp(pp->pp_cisinfo[2], pa->card->cis1_info[2]))) &&
    416       1.56   mycroft 		         (!pp->pp_cisinfo[3] || (pa->card->cis1_info[3] &&
    417       1.56   mycroft 		           !strcmp(pp->pp_cisinfo[3], pa->card->cis1_info[3]))))
    418       1.56   mycroft 			matches = 1;
    419       1.16       cgd 
    420       1.16       cgd 		/* if a separate match function is given, let it override */
    421       1.56   mycroft 		if (matchfn)
    422       1.56   mycroft 			matches = (*matchfn)(pa, pp, matches);
    423       1.16       cgd 
    424       1.16       cgd 		if (matches)
    425       1.56   mycroft                         return (pp);
    426       1.16       cgd         }
    427       1.56   mycroft         return (0);
    428        1.2   thorpej }
    429        1.2   thorpej 
    430       1.60   mycroft void
    431       1.70   mycroft pcmcia_socket_settype(dev, type)
    432       1.70   mycroft 	struct device *dev;
    433       1.70   mycroft 	int type;
    434        1.2   thorpej {
    435       1.60   mycroft 	struct pcmcia_softc *sc = (void *)dev;
    436        1.2   thorpej 
    437       1.60   mycroft 	pcmcia_chip_socket_settype(sc->pct, sc->pch, type);
    438        1.2   thorpej }
    439        1.2   thorpej 
    440        1.2   thorpej /*
    441        1.2   thorpej  * Initialize a PCMCIA function.  May be called as long as the function is
    442        1.2   thorpej  * disabled.
    443        1.2   thorpej  */
    444        1.2   thorpej void
    445        1.2   thorpej pcmcia_function_init(pf, cfe)
    446        1.2   thorpej 	struct pcmcia_function *pf;
    447        1.2   thorpej 	struct pcmcia_config_entry *cfe;
    448        1.2   thorpej {
    449        1.2   thorpej 	if (pf->pf_flags & PFF_ENABLED)
    450        1.2   thorpej 		panic("pcmcia_function_init: function is enabled");
    451        1.2   thorpej 
    452        1.2   thorpej 	/* Remember which configuration entry we are using. */
    453        1.2   thorpej 	pf->cfe = cfe;
    454        1.2   thorpej }
    455        1.2   thorpej 
    456       1.59   mycroft void
    457       1.59   mycroft pcmcia_socket_enable(dev)
    458       1.40   mycroft 	struct device *dev;
    459        1.5      marc {
    460       1.40   mycroft 	struct pcmcia_softc *sc = (void *)dev;
    461       1.40   mycroft 
    462       1.59   mycroft 	if (sc->sc_enabled_count++ == 0)
    463       1.59   mycroft 		pcmcia_chip_socket_enable(sc->pct, sc->pch);
    464       1.59   mycroft 	DPRINTF(("%s: ++enabled_count = %d\n", sc->dev.dv_xname,
    465       1.59   mycroft 		 sc->sc_enabled_count));
    466        1.5      marc }
    467        1.5      marc 
    468       1.59   mycroft void
    469       1.59   mycroft pcmcia_socket_disable(dev)
    470       1.40   mycroft 	struct device *dev;
    471        1.5      marc {
    472       1.40   mycroft 	struct pcmcia_softc *sc = (void *)dev;
    473       1.40   mycroft 
    474       1.59   mycroft 	if (--sc->sc_enabled_count == 0)
    475       1.59   mycroft 		pcmcia_chip_socket_disable(sc->pct, sc->pch);
    476       1.59   mycroft 	DPRINTF(("%s: --enabled_count = %d\n", sc->dev.dv_xname,
    477       1.59   mycroft 		 sc->sc_enabled_count));
    478        1.5      marc }
    479        1.5      marc 
    480        1.2   thorpej /* Enable a PCMCIA function */
    481        1.2   thorpej int
    482        1.2   thorpej pcmcia_function_enable(pf)
    483        1.2   thorpej 	struct pcmcia_function *pf;
    484        1.2   thorpej {
    485       1.53   mycroft 	struct pcmcia_softc *sc = pf->sc;
    486        1.2   thorpej 	struct pcmcia_function *tmp;
    487        1.2   thorpej 	int reg;
    488       1.55   mycroft 	int error;
    489        1.2   thorpej 
    490        1.2   thorpej 	if (pf->cfe == NULL)
    491        1.2   thorpej 		panic("pcmcia_function_enable: function not initialized");
    492        1.2   thorpej 
    493        1.5      marc 	/*
    494        1.5      marc 	 * Increase the reference count on the socket, enabling power, if
    495        1.5      marc 	 * necessary.
    496        1.5      marc 	 */
    497       1.59   mycroft 	pcmcia_socket_enable(&sc->dev);
    498       1.74  christos 	pcmcia_socket_settype(&sc->dev, pf->cfe->iftype);
    499        1.5      marc 
    500        1.2   thorpej 	if (pf->pf_flags & PFF_ENABLED) {
    501        1.2   thorpej 		/*
    502        1.2   thorpej 		 * Don't do anything if we're already enabled.
    503        1.2   thorpej 		 */
    504        1.2   thorpej 		return (0);
    505        1.2   thorpej 	}
    506        1.2   thorpej 
    507        1.2   thorpej 	/*
    508        1.2   thorpej 	 * it's possible for different functions' CCRs to be in the same
    509        1.2   thorpej 	 * underlying page.  Check for that.
    510        1.2   thorpej 	 */
    511        1.2   thorpej 
    512       1.53   mycroft 	SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) {
    513        1.2   thorpej 		if ((tmp->pf_flags & PFF_ENABLED) &&
    514        1.2   thorpej 		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
    515        1.2   thorpej 		    ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
    516        1.2   thorpej 		     (tmp->ccr_base - tmp->pf_ccr_offset +
    517        1.2   thorpej 		      tmp->pf_ccr_realsize))) {
    518        1.2   thorpej 			pf->pf_ccrt = tmp->pf_ccrt;
    519        1.2   thorpej 			pf->pf_ccrh = tmp->pf_ccrh;
    520        1.2   thorpej 			pf->pf_ccr_realsize = tmp->pf_ccr_realsize;
    521        1.2   thorpej 
    522        1.2   thorpej 			/*
    523        1.2   thorpej 			 * pf->pf_ccr_offset = (tmp->pf_ccr_offset -
    524        1.2   thorpej 			 * tmp->ccr_base) + pf->ccr_base;
    525        1.2   thorpej 			 */
    526        1.2   thorpej 			pf->pf_ccr_offset =
    527        1.2   thorpej 			    (tmp->pf_ccr_offset + pf->ccr_base) -
    528        1.2   thorpej 			    tmp->ccr_base;
    529        1.2   thorpej 			pf->pf_ccr_window = tmp->pf_ccr_window;
    530        1.2   thorpej 			break;
    531        1.2   thorpej 		}
    532        1.2   thorpej 	}
    533        1.2   thorpej 
    534        1.2   thorpej 	if (tmp == NULL) {
    535       1.55   mycroft 		error = pcmcia_mem_alloc(pf, PCMCIA_CCR_SIZE, &pf->pf_pcmh);
    536       1.55   mycroft 		if (error)
    537        1.2   thorpej 			goto bad;
    538        1.2   thorpej 
    539       1.55   mycroft 		error = pcmcia_mem_map(pf, PCMCIA_MEM_ATTR, pf->ccr_base,
    540        1.2   thorpej 		    PCMCIA_CCR_SIZE, &pf->pf_pcmh, &pf->pf_ccr_offset,
    541       1.55   mycroft 		    &pf->pf_ccr_window);
    542       1.55   mycroft 		if (error) {
    543        1.2   thorpej 			pcmcia_mem_free(pf, &pf->pf_pcmh);
    544        1.2   thorpej 			goto bad;
    545        1.2   thorpej 		}
    546        1.2   thorpej 	}
    547        1.2   thorpej 
    548       1.68   mycroft 	if (pcmcia_mfc(sc) || 1) {
    549       1.41   mycroft 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE0,
    550       1.65   mycroft 				 (pf->pf_mfc_iobase >>  0) & 0xff);
    551       1.41   mycroft 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE1,
    552       1.65   mycroft 				 (pf->pf_mfc_iobase >>  8) & 0xff);
    553       1.41   mycroft 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE2,
    554       1.65   mycroft 				 (pf->pf_mfc_iobase >> 16) & 0xff);
    555       1.41   mycroft 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOBASE3,
    556       1.65   mycroft 				 (pf->pf_mfc_iobase >> 24) & 0xff);
    557       1.65   mycroft 		pcmcia_ccr_write(pf, PCMCIA_CCR_IOLIMIT,
    558       1.65   mycroft 				 pf->pf_mfc_iomax - pf->pf_mfc_iobase);
    559       1.41   mycroft 	}
    560       1.41   mycroft 
    561       1.67   mycroft 	reg = 0;
    562       1.67   mycroft 	if (pf->cfe->flags & PCMCIA_CFE_AUDIO)
    563       1.67   mycroft 		reg |= PCMCIA_CCR_STATUS_AUDIO;
    564       1.67   mycroft 	pcmcia_ccr_write(pf, PCMCIA_CCR_STATUS, reg);
    565       1.67   mycroft 
    566       1.67   mycroft 	pcmcia_ccr_write(pf, PCMCIA_CCR_SOCKETCOPY, 0);
    567       1.67   mycroft 
    568        1.2   thorpej 	reg = (pf->cfe->number & PCMCIA_CCR_OPTION_CFINDEX);
    569        1.2   thorpej 	reg |= PCMCIA_CCR_OPTION_LEVIREQ;
    570       1.53   mycroft 	if (pcmcia_mfc(sc)) {
    571        1.5      marc 		reg |= (PCMCIA_CCR_OPTION_FUNC_ENABLE |
    572        1.5      marc 			PCMCIA_CCR_OPTION_ADDR_DECODE);
    573       1.52   mycroft 		if (pf->pf_ih)
    574        1.5      marc 			reg |= PCMCIA_CCR_OPTION_IREQ_ENABLE;
    575        1.5      marc 
    576        1.5      marc 	}
    577        1.2   thorpej 	pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    578        1.2   thorpej 
    579       1.12      marc #ifdef PCMCIADEBUG
    580       1.12      marc 	if (pcmcia_debug) {
    581       1.53   mycroft 		SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) {
    582       1.12      marc 			printf("%s: function %d CCR at %d offset %lx: "
    583       1.12      marc 			       "%x %x %x %x, %x %x %x %x, %x\n",
    584       1.12      marc 			       tmp->sc->dev.dv_xname, tmp->number,
    585       1.15   aymeric 			       tmp->pf_ccr_window,
    586       1.15   aymeric 			       (unsigned long) tmp->pf_ccr_offset,
    587       1.43   mycroft 			       pcmcia_ccr_read(tmp, 0),
    588       1.43   mycroft 			       pcmcia_ccr_read(tmp, 1),
    589       1.43   mycroft 			       pcmcia_ccr_read(tmp, 2),
    590       1.43   mycroft 			       pcmcia_ccr_read(tmp, 3),
    591       1.43   mycroft 
    592       1.43   mycroft 			       pcmcia_ccr_read(tmp, 5),
    593       1.73     perry 			       pcmcia_ccr_read(tmp, 6),
    594       1.43   mycroft 			       pcmcia_ccr_read(tmp, 7),
    595       1.43   mycroft 			       pcmcia_ccr_read(tmp, 8),
    596        1.5      marc 
    597       1.43   mycroft 			       pcmcia_ccr_read(tmp, 9));
    598       1.12      marc 		}
    599       1.12      marc 	}
    600       1.12      marc #endif
    601        1.5      marc 
    602       1.21       uch #ifdef IT8368E_LEGACY_MODE
    603       1.21       uch 	/* return to I/O mode */
    604       1.21       uch 	it8368_mode(pf, IT8368_IO_MODE, IT8368_WIDTH_16);
    605       1.21       uch #endif
    606       1.66   mycroft 
    607       1.66   mycroft 	pf->pf_flags |= PFF_ENABLED;
    608        1.2   thorpej 	return (0);
    609        1.2   thorpej 
    610       1.55   mycroft bad:
    611        1.2   thorpej 	/*
    612        1.2   thorpej 	 * Decrement the reference count, and power down the socket, if
    613        1.2   thorpej 	 * necessary.
    614        1.2   thorpej 	 */
    615       1.55   mycroft 	printf("%s: couldn't map the CCR\n", pf->child->dv_xname);
    616       1.59   mycroft 	pcmcia_socket_disable(&sc->dev);
    617        1.5      marc 
    618       1.55   mycroft 	return (error);
    619        1.2   thorpej }
    620        1.2   thorpej 
    621        1.2   thorpej /* Disable PCMCIA function. */
    622        1.2   thorpej void
    623        1.2   thorpej pcmcia_function_disable(pf)
    624        1.2   thorpej 	struct pcmcia_function *pf;
    625        1.2   thorpej {
    626       1.59   mycroft 	struct pcmcia_softc *sc = pf->sc;
    627        1.2   thorpej 	struct pcmcia_function *tmp;
    628       1.48   mycroft 	int reg;
    629        1.2   thorpej 
    630        1.2   thorpej 	if (pf->cfe == NULL)
    631        1.2   thorpej 		panic("pcmcia_function_enable: function not initialized");
    632        1.2   thorpej 
    633        1.2   thorpej 	if ((pf->pf_flags & PFF_ENABLED) == 0) {
    634        1.2   thorpej 		/*
    635       1.36    briggs 		 * Don't do anything but decrement if we're already disabled.
    636        1.2   thorpej 		 */
    637       1.36    briggs 		goto out;
    638        1.2   thorpej 	}
    639        1.2   thorpej 
    640       1.69   mycroft 	if (pcmcia_mfc(sc) &&
    641       1.69   mycroft 	    (pf->pf_flags & PFF_DETACHED) == 0) {
    642       1.48   mycroft 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    643       1.48   mycroft 		reg &= ~(PCMCIA_CCR_OPTION_FUNC_ENABLE|
    644       1.48   mycroft 			 PCMCIA_CCR_OPTION_ADDR_DECODE|
    645       1.48   mycroft 		         PCMCIA_CCR_OPTION_IREQ_ENABLE);
    646       1.48   mycroft 		pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    647       1.48   mycroft 	}
    648       1.48   mycroft 
    649        1.2   thorpej 	/*
    650        1.2   thorpej 	 * it's possible for different functions' CCRs to be in the same
    651        1.2   thorpej 	 * underlying page.  Check for that.  Note we mark us as disabled
    652        1.2   thorpej 	 * first to avoid matching ourself.
    653        1.2   thorpej 	 */
    654        1.2   thorpej 
    655        1.2   thorpej 	pf->pf_flags &= ~PFF_ENABLED;
    656       1.59   mycroft 	SIMPLEQ_FOREACH(tmp, &sc->card.pf_head, pf_list) {
    657        1.2   thorpej 		if ((tmp->pf_flags & PFF_ENABLED) &&
    658        1.2   thorpej 		    (pf->ccr_base >= (tmp->ccr_base - tmp->pf_ccr_offset)) &&
    659        1.2   thorpej 		    ((pf->ccr_base + PCMCIA_CCR_SIZE) <=
    660        1.2   thorpej 		(tmp->ccr_base - tmp->pf_ccr_offset + tmp->pf_ccr_realsize)))
    661        1.2   thorpej 			break;
    662        1.2   thorpej 	}
    663        1.2   thorpej 
    664        1.2   thorpej 	/* Not used by anyone else; unmap the CCR. */
    665        1.2   thorpej 	if (tmp == NULL) {
    666        1.2   thorpej 		pcmcia_mem_unmap(pf, pf->pf_ccr_window);
    667        1.2   thorpej 		pcmcia_mem_free(pf, &pf->pf_pcmh);
    668        1.2   thorpej 	}
    669        1.2   thorpej 
    670       1.36    briggs out:
    671        1.2   thorpej 	/*
    672        1.2   thorpej 	 * Decrement the reference count, and power down the socket, if
    673        1.2   thorpej 	 * necessary.
    674        1.2   thorpej 	 */
    675       1.59   mycroft 	pcmcia_socket_disable(&sc->dev);
    676        1.2   thorpej }
    677        1.2   thorpej 
    678        1.2   thorpej int
    679       1.41   mycroft pcmcia_io_map(pf, width, pcihp, windowp)
    680        1.2   thorpej 	struct pcmcia_function *pf;
    681        1.2   thorpej 	int width;
    682        1.2   thorpej 	struct pcmcia_io_handle *pcihp;
    683        1.2   thorpej 	int *windowp;
    684        1.2   thorpej {
    685       1.68   mycroft 	struct pcmcia_softc *sc = pf->sc;
    686       1.49   mycroft 	int error;
    687        1.2   thorpej 
    688       1.49   mycroft 	if (pf->pf_flags & PFF_ENABLED)
    689       1.51   mycroft 		printf("pcmcia_io_map: function is enabled!\n");
    690       1.49   mycroft 
    691       1.68   mycroft 	error = pcmcia_chip_io_map(sc->pct, sc->pch,
    692       1.49   mycroft 	    width, 0, pcihp->size, pcihp, windowp);
    693       1.49   mycroft 	if (error)
    694       1.49   mycroft 		return (error);
    695        1.2   thorpej 
    696        1.2   thorpej 	/*
    697        1.2   thorpej 	 * XXX in the multifunction multi-iospace-per-function case, this
    698        1.2   thorpej 	 * needs to cooperate with io_alloc to make sure that the spaces
    699        1.2   thorpej 	 * don't overlap, and that the ccr's are set correctly
    700        1.2   thorpej 	 */
    701        1.2   thorpej 
    702       1.68   mycroft 	if (pcmcia_mfc(sc) || 1) {
    703       1.65   mycroft 		bus_addr_t iobase = pcihp->addr;
    704       1.65   mycroft 		bus_addr_t iomax = pcihp->addr + pcihp->size - 1;
    705        1.5      marc 
    706       1.65   mycroft 		DPRINTF(("window iobase %lx iomax %lx\n", (long)iobase,
    707       1.65   mycroft 		    (long)iomax));
    708       1.65   mycroft 		if (pf->pf_mfc_iobase == 0) {
    709       1.65   mycroft 			pf->pf_mfc_iobase = iobase;
    710       1.65   mycroft 			pf->pf_mfc_iomax = iomax;
    711       1.65   mycroft 		} else {
    712       1.65   mycroft 			if (iobase < pf->pf_mfc_iobase)
    713       1.65   mycroft 				pf->pf_mfc_iobase = iobase;
    714       1.65   mycroft 			if (iomax > pf->pf_mfc_iomax)
    715       1.65   mycroft 				pf->pf_mfc_iomax = iomax;
    716       1.65   mycroft 		}
    717       1.65   mycroft 		DPRINTF(("function iobase %lx iomax %lx\n",
    718       1.65   mycroft 		    (long)pf->pf_mfc_iobase, (long)pf->pf_mfc_iomax));
    719        1.2   thorpej 	}
    720       1.41   mycroft 
    721        1.2   thorpej 	return (0);
    722       1.11   thorpej }
    723       1.11   thorpej 
    724       1.11   thorpej void
    725       1.11   thorpej pcmcia_io_unmap(pf, window)
    726       1.11   thorpej 	struct pcmcia_function *pf;
    727       1.11   thorpej 	int window;
    728       1.11   thorpej {
    729       1.68   mycroft 	struct pcmcia_softc *sc = pf->sc;
    730       1.11   thorpej 
    731       1.49   mycroft 	if (pf->pf_flags & PFF_ENABLED)
    732       1.51   mycroft 		printf("pcmcia_io_unmap: function is enabled!\n");
    733       1.49   mycroft 
    734       1.68   mycroft 	pcmcia_chip_io_unmap(sc->pct, sc->pch, window);
    735        1.2   thorpej }
    736        1.2   thorpej 
    737        1.2   thorpej void *
    738        1.2   thorpej pcmcia_intr_establish(pf, ipl, ih_fct, ih_arg)
    739        1.2   thorpej 	struct pcmcia_function *pf;
    740        1.2   thorpej 	int ipl;
    741       1.72     perry 	int (*ih_fct)(void *);
    742        1.2   thorpej 	void *ih_arg;
    743        1.2   thorpej {
    744        1.2   thorpej 
    745       1.49   mycroft 	if (pf->pf_flags & PFF_ENABLED)
    746       1.51   mycroft 		printf("pcmcia_intr_establish: function is enabled!\n");
    747       1.52   mycroft 	if (pf->pf_ih)
    748       1.52   mycroft 		panic("pcmcia_intr_establish: already done\n");
    749       1.49   mycroft 
    750       1.52   mycroft 	pf->pf_ih = pcmcia_chip_intr_establish(pf->sc->pct, pf->sc->pch,
    751       1.52   mycroft 	    pf, ipl, ih_fct, ih_arg);
    752       1.55   mycroft 	if (!pf->pf_ih)
    753       1.55   mycroft 		printf("%s: interrupt establish failed\n", pf->child->dv_xname);
    754       1.52   mycroft 	return (pf->pf_ih);
    755        1.2   thorpej }
    756        1.2   thorpej 
    757        1.2   thorpej void
    758        1.2   thorpej pcmcia_intr_disestablish(pf, ih)
    759        1.2   thorpej 	struct pcmcia_function *pf;
    760        1.2   thorpej 	void *ih;
    761        1.2   thorpej {
    762       1.52   mycroft 
    763       1.49   mycroft 	if (pf->pf_flags & PFF_ENABLED)
    764       1.51   mycroft 		printf("pcmcia_intr_disestablish: function is enabled!\n");
    765       1.52   mycroft 	if (!pf->pf_ih)
    766       1.52   mycroft 		panic("pcmcia_intr_distestablish: already done\n");
    767       1.49   mycroft 
    768       1.52   mycroft 	pcmcia_chip_intr_disestablish(pf->sc->pct, pf->sc->pch, ih);
    769       1.52   mycroft 	pf->pf_ih = 0;
    770        1.2   thorpej }
    771       1.53   mycroft 
    772       1.53   mycroft int
    773       1.53   mycroft pcmcia_config_alloc(pf, cfe)
    774       1.53   mycroft 	struct pcmcia_function *pf;
    775       1.53   mycroft 	struct pcmcia_config_entry *cfe;
    776       1.53   mycroft {
    777       1.61   mycroft 	int error = 0;
    778       1.53   mycroft 	int n, m;
    779       1.53   mycroft 
    780       1.53   mycroft 	for (n = 0; n < cfe->num_iospace; n++) {
    781       1.53   mycroft 		bus_addr_t start = cfe->iospace[n].start;
    782       1.53   mycroft 		bus_size_t length = cfe->iospace[n].length;
    783       1.53   mycroft 		bus_size_t align = cfe->iomask ? (1 << cfe->iomask) :
    784       1.53   mycroft 		    length;
    785       1.53   mycroft 		bus_size_t skew = start & (align - 1);
    786       1.53   mycroft 
    787       1.54   mycroft 		if ((start - skew) == 0 && align < 0x400) {
    788       1.54   mycroft 			if (skew)
    789       1.54   mycroft 				printf("Drats!  I need a skew!\n");
    790       1.53   mycroft 			start = 0;
    791       1.54   mycroft 		}
    792       1.53   mycroft 
    793       1.53   mycroft 		DPRINTF(("pcmcia_config_alloc: io %d start=%lx length=%lx align=%lx skew=%lx\n",
    794       1.53   mycroft 		    n, (long)start, (long)length, (long)align, (long)skew));
    795       1.53   mycroft 
    796       1.53   mycroft 		error = pcmcia_io_alloc(pf, start, length, align,
    797       1.53   mycroft 		    &cfe->iospace[n].handle);
    798       1.53   mycroft 		if (error)
    799       1.53   mycroft 			break;
    800       1.53   mycroft 	}
    801       1.53   mycroft 	if (n < cfe->num_iospace) {
    802       1.53   mycroft 		for (m = 0; m < n; m++)
    803       1.53   mycroft 			pcmcia_io_free(pf, &cfe->iospace[m].handle);
    804       1.53   mycroft 		return (error);
    805       1.53   mycroft 	}
    806       1.53   mycroft 
    807       1.53   mycroft 	for (n = 0; n < cfe->num_memspace; n++) {
    808       1.53   mycroft 		bus_size_t length = cfe->memspace[n].length;
    809       1.53   mycroft 
    810       1.53   mycroft 		DPRINTF(("pcmcia_config_alloc: mem %d length %lx\n", n,
    811       1.53   mycroft 		    (long)length));
    812       1.53   mycroft 
    813       1.53   mycroft 		error = pcmcia_mem_alloc(pf, length, &cfe->memspace[n].handle);
    814       1.53   mycroft 		if (error)
    815       1.53   mycroft 			break;
    816       1.53   mycroft 	}
    817       1.53   mycroft 	if (n < cfe->num_memspace) {
    818       1.53   mycroft 		for (m = 0; m < cfe->num_iospace; m++)
    819       1.53   mycroft 			pcmcia_io_free(pf, &cfe->iospace[m].handle);
    820       1.53   mycroft 		for (m = 0; m < n; m++)
    821       1.53   mycroft 			pcmcia_mem_free(pf, &cfe->memspace[m].handle);
    822       1.53   mycroft 		return (error);
    823       1.53   mycroft 	}
    824       1.53   mycroft 
    825       1.53   mycroft 	/* This one's good! */
    826       1.61   mycroft 	return (error);
    827       1.53   mycroft }
    828       1.53   mycroft 
    829       1.53   mycroft void
    830       1.53   mycroft pcmcia_config_free(pf)
    831       1.53   mycroft 	struct pcmcia_function *pf;
    832       1.53   mycroft {
    833       1.53   mycroft 	struct pcmcia_config_entry *cfe = pf->cfe;
    834       1.53   mycroft 	int m;
    835       1.53   mycroft 
    836       1.53   mycroft 	for (m = 0; m < cfe->num_iospace; m++)
    837       1.53   mycroft 		pcmcia_io_free(pf, &cfe->iospace[m].handle);
    838       1.53   mycroft 	for (m = 0; m < cfe->num_memspace; m++)
    839       1.53   mycroft 		pcmcia_mem_free(pf, &cfe->memspace[m].handle);
    840       1.53   mycroft }
    841       1.53   mycroft 
    842       1.53   mycroft int
    843       1.53   mycroft pcmcia_config_map(pf)
    844       1.53   mycroft 	struct pcmcia_function *pf;
    845       1.53   mycroft {
    846       1.53   mycroft 	struct pcmcia_config_entry *cfe = pf->cfe;
    847       1.61   mycroft 	int error = 0;
    848       1.53   mycroft 	int n, m;
    849       1.53   mycroft 
    850       1.53   mycroft 	for (n = 0; n < cfe->num_iospace; n++) {
    851       1.53   mycroft 		int width;
    852       1.53   mycroft 
    853       1.62   mycroft 		if (cfe->flags & PCMCIA_CFE_IO16)
    854       1.62   mycroft 			width = PCMCIA_WIDTH_AUTO;
    855       1.62   mycroft 		else
    856       1.53   mycroft 			width = PCMCIA_WIDTH_IO8;
    857       1.53   mycroft 		error = pcmcia_io_map(pf, width, &cfe->iospace[n].handle,
    858       1.53   mycroft 		    &cfe->iospace[n].window);
    859       1.53   mycroft 		if (error)
    860       1.53   mycroft 			break;
    861       1.53   mycroft 	}
    862       1.53   mycroft 	if (n < cfe->num_iospace) {
    863       1.53   mycroft 		for (m = 0; m < n; m++)
    864       1.53   mycroft 			pcmcia_io_unmap(pf, cfe->iospace[m].window);
    865       1.53   mycroft 		return (error);
    866       1.53   mycroft 	}
    867       1.53   mycroft 
    868       1.53   mycroft 	for (n = 0; n < cfe->num_memspace; n++) {
    869       1.53   mycroft 		bus_size_t length = cfe->memspace[n].length;
    870       1.53   mycroft 		int width;
    871       1.53   mycroft 
    872       1.53   mycroft 		DPRINTF(("pcmcia_config_alloc: mem %d length %lx\n", n,
    873       1.53   mycroft 		    (long)length));
    874       1.53   mycroft 
    875       1.53   mycroft 		/*XXX*/
    876       1.53   mycroft 		width = PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON;
    877       1.53   mycroft 		error = pcmcia_mem_map(pf, width, 0, length,
    878       1.53   mycroft 		    &cfe->memspace[n].handle, &cfe->memspace[n].offset,
    879       1.53   mycroft 		    &cfe->memspace[n].window);
    880       1.53   mycroft 		if (error)
    881       1.53   mycroft 			break;
    882       1.53   mycroft 	}
    883       1.53   mycroft 	if (n < cfe->num_memspace) {
    884       1.53   mycroft 		for (m = 0; m < cfe->num_iospace; m++)
    885       1.53   mycroft 			pcmcia_io_unmap(pf, cfe->iospace[m].window);
    886       1.53   mycroft 		for (m = 0; m < n; m++)
    887       1.53   mycroft 			pcmcia_mem_unmap(pf, cfe->memspace[m].window);
    888       1.53   mycroft 		return (error);
    889       1.53   mycroft 	}
    890       1.53   mycroft 
    891       1.53   mycroft 	/* This one's good! */
    892       1.61   mycroft 	return (error);
    893       1.53   mycroft }
    894       1.53   mycroft 
    895       1.53   mycroft void
    896       1.53   mycroft pcmcia_config_unmap(pf)
    897       1.53   mycroft 	struct pcmcia_function *pf;
    898       1.53   mycroft {
    899       1.53   mycroft 	struct pcmcia_config_entry *cfe = pf->cfe;
    900       1.53   mycroft 	int m;
    901       1.53   mycroft 
    902       1.53   mycroft 	for (m = 0; m < cfe->num_iospace; m++)
    903       1.53   mycroft 		pcmcia_io_unmap(pf, cfe->iospace[m].window);
    904       1.53   mycroft 	for (m = 0; m < cfe->num_memspace; m++)
    905       1.53   mycroft 		pcmcia_mem_unmap(pf, cfe->memspace[m].window);
    906       1.53   mycroft }
    907       1.53   mycroft 
    908       1.53   mycroft int
    909       1.53   mycroft pcmcia_function_configure(pf, validator)
    910       1.53   mycroft 	struct pcmcia_function *pf;
    911       1.72     perry 	int (*validator)(struct pcmcia_config_entry *);
    912       1.53   mycroft {
    913       1.53   mycroft 	struct pcmcia_config_entry *cfe;
    914       1.53   mycroft 	int error = ENOENT;
    915       1.53   mycroft 
    916       1.53   mycroft 	SIMPLEQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
    917       1.53   mycroft 		error = validator(cfe);
    918       1.53   mycroft 		if (error)
    919       1.53   mycroft 			continue;
    920       1.53   mycroft 		error = pcmcia_config_alloc(pf, cfe);
    921       1.53   mycroft 		if (!error)
    922       1.53   mycroft 			break;
    923       1.53   mycroft 	}
    924       1.53   mycroft 	if (!cfe) {
    925       1.53   mycroft 		DPRINTF(("pcmcia_function_configure: no config entry found, error=%d\n",
    926       1.53   mycroft 		    error));
    927       1.53   mycroft 		return (error);
    928       1.53   mycroft 	}
    929       1.53   mycroft 
    930       1.53   mycroft 	/* Remember which configuration entry we are using. */
    931       1.53   mycroft 	pf->cfe = cfe;
    932       1.53   mycroft 
    933       1.53   mycroft 	error = pcmcia_config_map(pf);
    934       1.53   mycroft 	if (error) {
    935       1.53   mycroft 		DPRINTF(("pcmcia_function_configure: map failed, error=%d\n",
    936       1.53   mycroft 		    error));
    937       1.53   mycroft 		return (error);
    938       1.53   mycroft 	}
    939       1.53   mycroft 
    940       1.53   mycroft 	return (0);
    941       1.53   mycroft }
    942       1.53   mycroft 
    943       1.53   mycroft void
    944       1.53   mycroft pcmcia_function_unconfigure(pf)
    945       1.53   mycroft 	struct pcmcia_function *pf;
    946       1.53   mycroft {
    947       1.53   mycroft 
    948       1.53   mycroft 	pcmcia_config_unmap(pf);
    949       1.53   mycroft 	pcmcia_config_free(pf);
    950       1.53   mycroft }
    951  1.82.22.1     joerg 
    952  1.82.22.1     joerg pnp_status_t
    953  1.82.22.1     joerg pcmcia_net_generic_power(device_t dv, pnp_request_t req, void *opaque,
    954  1.82.22.1     joerg     pnp_state_t *device_state, struct ifnet *ifp)
    955  1.82.22.1     joerg {
    956  1.82.22.1     joerg 	pnp_status_t status;
    957  1.82.22.1     joerg 	pnp_state_t *cur_state;
    958  1.82.22.1     joerg 	pnp_capabilities_t *caps;
    959  1.82.22.1     joerg 	int s;
    960  1.82.22.1     joerg 
    961  1.82.22.1     joerg 	status = PNP_STATUS_UNSUPPORTED;
    962  1.82.22.1     joerg 
    963  1.82.22.1     joerg 	switch (req) {
    964  1.82.22.1     joerg 	case PNP_REQUEST_GET_CAPABILITIES:
    965  1.82.22.1     joerg 		caps = opaque;
    966  1.82.22.1     joerg 		caps->state = PNP_STATE_D0 | PNP_STATE_D3;
    967  1.82.22.1     joerg 		status = PNP_STATUS_SUCCESS;
    968  1.82.22.1     joerg 		break;
    969  1.82.22.1     joerg 	case PNP_REQUEST_SET_STATE:
    970  1.82.22.1     joerg 		cur_state = opaque;
    971  1.82.22.1     joerg 		switch (*cur_state) {
    972  1.82.22.1     joerg 		case PNP_STATE_D0:
    973  1.82.22.1     joerg 			s = splnet();
    974  1.82.22.1     joerg 
    975  1.82.22.1     joerg 			if (ifp->if_flags & IFF_UP) {
    976  1.82.22.1     joerg 				ifp->if_flags &= ~IFF_RUNNING;
    977  1.82.22.1     joerg 				(*ifp->if_init)(ifp);
    978  1.82.22.1     joerg 				(*ifp->if_start)(ifp);
    979  1.82.22.1     joerg 			}
    980  1.82.22.1     joerg 
    981  1.82.22.1     joerg 			splx(s);
    982  1.82.22.1     joerg 			break;
    983  1.82.22.1     joerg 		case PNP_STATE_D3:
    984  1.82.22.1     joerg 			s = splnet();
    985  1.82.22.1     joerg 			(*ifp->if_stop)(ifp, 1);
    986  1.82.22.1     joerg 			splx(s);
    987  1.82.22.1     joerg 			break;
    988  1.82.22.1     joerg 		default:
    989  1.82.22.1     joerg 			return PNP_STATUS_UNSUPPORTED;
    990  1.82.22.1     joerg 		}
    991  1.82.22.1     joerg 		*device_state = *cur_state;
    992  1.82.22.1     joerg 		status = PNP_STATUS_SUCCESS;
    993  1.82.22.1     joerg 
    994  1.82.22.1     joerg 		break;
    995  1.82.22.1     joerg 	case PNP_REQUEST_GET_STATE:
    996  1.82.22.1     joerg 		cur_state = opaque;
    997  1.82.22.1     joerg 
    998  1.82.22.1     joerg 		*cur_state = *device_state;
    999  1.82.22.1     joerg 		status = PNP_STATUS_SUCCESS;
   1000  1.82.22.1     joerg 		break;
   1001  1.82.22.1     joerg 	default:
   1002  1.82.22.1     joerg 		status = PNP_STATUS_UNSUPPORTED;
   1003  1.82.22.1     joerg 	}
   1004  1.82.22.1     joerg 
   1005  1.82.22.1     joerg 	return status;
   1006  1.82.22.1     joerg }
   1007