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