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