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