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