Home | History | Annotate | Line # | Download | only in pcmcia
if_an_pcmcia.c revision 1.22
      1  1.22  mycroft /* $NetBSD: if_an_pcmcia.c,v 1.22 2004/08/10 18:39:08 mycroft Exp $ */
      2   1.1     onoe 
      3   1.1     onoe /*-
      4  1.22  mycroft  * Copyright (c) 2000, 2004 The NetBSD Foundation, Inc.
      5   1.1     onoe  * All rights reserved.
      6   1.1     onoe  *
      7   1.1     onoe  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1     onoe  * by Atsushi Onoe
      9   1.1     onoe  *
     10   1.1     onoe  * Redistribution and use in source and binary forms, with or without
     11   1.1     onoe  * modification, are permitted provided that the following conditions
     12   1.1     onoe  * are met:
     13   1.1     onoe  * 1. Redistributions of source code must retain the above copyright
     14   1.1     onoe  *    notice, this list of conditions and the following disclaimer.
     15   1.1     onoe  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1     onoe  *    notice, this list of conditions and the following disclaimer in the
     17   1.1     onoe  *    documentation and/or other materials provided with the distribution.
     18   1.1     onoe  * 3. All advertising materials mentioning features or use of this software
     19   1.1     onoe  *    must display the following acknowledgement:
     20   1.1     onoe  *        This product includes software developed by the NetBSD
     21   1.1     onoe  *        Foundation, Inc. and its contributors.
     22   1.1     onoe  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1     onoe  *    contributors may be used to endorse or promote products derived
     24   1.1     onoe  *    from this software without specific prior written permission.
     25   1.1     onoe  *
     26   1.1     onoe  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1     onoe  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1     onoe  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1     onoe  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1     onoe  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1     onoe  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1     onoe  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1     onoe  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1     onoe  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1     onoe  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1     onoe  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1     onoe  */
     38  1.11    lukem 
     39  1.11    lukem #include <sys/cdefs.h>
     40  1.22  mycroft __KERNEL_RCSID(0, "$NetBSD: if_an_pcmcia.c,v 1.22 2004/08/10 18:39:08 mycroft Exp $");
     41   1.1     onoe 
     42   1.1     onoe #include <sys/param.h>
     43   1.1     onoe #include <sys/systm.h>
     44   1.1     onoe #include <sys/callout.h>
     45   1.1     onoe #include <sys/mbuf.h>
     46   1.1     onoe #include <sys/socket.h>
     47   1.1     onoe #include <sys/ioctl.h>
     48   1.1     onoe #include <sys/errno.h>
     49   1.1     onoe #include <sys/syslog.h>
     50   1.1     onoe #include <sys/select.h>
     51   1.1     onoe #include <sys/device.h>
     52   1.1     onoe 
     53   1.1     onoe #include <net/if.h>
     54   1.1     onoe #include <net/if_dl.h>
     55   1.1     onoe #include <net/if_ether.h>
     56   1.1     onoe #include <net/if_media.h>
     57  1.16   dyoung 
     58  1.16   dyoung #include <net80211/ieee80211_var.h>
     59  1.16   dyoung #include <net80211/ieee80211_compat.h>
     60   1.1     onoe 
     61   1.1     onoe #include <machine/cpu.h>
     62   1.1     onoe #include <machine/bus.h>
     63   1.1     onoe #include <machine/intr.h>
     64   1.1     onoe 
     65   1.3     onoe #include <dev/ic/anreg.h>
     66   1.1     onoe #include <dev/ic/anvar.h>
     67   1.1     onoe 
     68   1.1     onoe #include <dev/pcmcia/pcmciareg.h>
     69   1.1     onoe #include <dev/pcmcia/pcmciavar.h>
     70   1.1     onoe #include <dev/pcmcia/pcmciadevs.h>
     71   1.1     onoe 
     72   1.1     onoe static int an_pcmcia_match __P((struct device *, struct cfdata *, void *));
     73   1.1     onoe static void an_pcmcia_attach __P((struct device *, struct device *, void *));
     74   1.1     onoe static int an_pcmcia_detach __P((struct device *, int));
     75   1.1     onoe static int an_pcmcia_enable __P((struct an_softc *));
     76   1.1     onoe static void an_pcmcia_disable __P((struct an_softc *));
     77   1.1     onoe 
     78   1.1     onoe struct an_pcmcia_softc {
     79   1.5     onoe 	struct an_softc sc_an;			/* real "an" softc */
     80   1.1     onoe 
     81   1.1     onoe 	/* PCMCIA-specific goo */
     82   1.1     onoe 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     83   1.1     onoe 	int sc_io_window;			/* our i/o window */
     84   1.1     onoe 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     85   1.5     onoe 	void *sc_ih;				/* interrupt handle */
     86   1.1     onoe 	void *sc_powerhook;			/* power hook descriptor */
     87   1.1     onoe };
     88   1.1     onoe 
     89  1.13  thorpej CFATTACH_DECL(an_pcmcia, sizeof(struct an_pcmcia_softc),
     90  1.14  thorpej     an_pcmcia_match, an_pcmcia_attach, an_pcmcia_detach, an_activate);
     91   1.1     onoe 
     92  1.22  mycroft static struct pcmcia_product an_pcmcia_products[] = {
     93   1.1     onoe 	{ PCMCIA_VENDOR_AIRONET,	PCMCIA_PRODUCT_AIRONET_PC4800,
     94  1.18  mycroft 	  PCMCIA_CIS_AIRONET_PC4800 },
     95   1.4     onoe 	{ PCMCIA_VENDOR_AIRONET,	PCMCIA_PRODUCT_AIRONET_PC4500,
     96  1.18  mycroft 	  PCMCIA_CIS_AIRONET_PC4500 },
     97   1.9     onoe 	{ PCMCIA_VENDOR_AIRONET,	PCMCIA_PRODUCT_AIRONET_350,
     98  1.18  mycroft 	  PCMCIA_CIS_AIRONET_350 },
     99   1.1     onoe };
    100  1.22  mycroft static const size_t an_pcmcia_nproducts =
    101  1.22  mycroft     sizeof(an_pcmcia_products) / sizeof(an_pcmcia_products[0]);
    102   1.1     onoe 
    103   1.1     onoe static int
    104   1.1     onoe an_pcmcia_enable(sc)
    105   1.1     onoe 	struct an_softc *sc;
    106   1.1     onoe {
    107   1.1     onoe 	struct an_pcmcia_softc *psc = (struct an_pcmcia_softc *)sc;
    108   1.1     onoe 	struct pcmcia_function *pf = psc->sc_pf;
    109   1.1     onoe 
    110   1.1     onoe 	/* establish the interrupt. */
    111   1.5     onoe 	psc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, an_intr, sc);
    112   1.5     onoe 	if (psc->sc_ih == NULL) {
    113   1.1     onoe 		printf("%s: couldn't establish interrupt\n",
    114  1.17     onoe 		    sc->sc_dev.dv_xname);
    115   1.1     onoe 		return (1);
    116   1.1     onoe 	}
    117   1.1     onoe 
    118   1.1     onoe 	if (pcmcia_function_enable(pf)) {
    119   1.5     onoe 		pcmcia_intr_disestablish(pf, psc->sc_ih);
    120   1.1     onoe 		return (1);
    121   1.1     onoe 	}
    122   1.1     onoe 	DELAY(1000);
    123   1.1     onoe 
    124   1.1     onoe 	return (0);
    125   1.1     onoe }
    126   1.1     onoe 
    127   1.1     onoe static void
    128   1.1     onoe an_pcmcia_disable(sc)
    129   1.1     onoe 	struct an_softc *sc;
    130   1.1     onoe {
    131   1.1     onoe 	struct an_pcmcia_softc *psc = (struct an_pcmcia_softc *)sc;
    132   1.1     onoe 	struct pcmcia_function *pf = psc->sc_pf;
    133   1.1     onoe 
    134   1.1     onoe 	pcmcia_function_disable(pf);
    135   1.5     onoe 	pcmcia_intr_disestablish(pf, psc->sc_ih);
    136   1.1     onoe }
    137   1.1     onoe 
    138   1.1     onoe static int
    139   1.1     onoe an_pcmcia_match(parent, match, aux)
    140   1.1     onoe 	struct device *parent;
    141   1.1     onoe 	struct cfdata *match;
    142   1.1     onoe 	void *aux;
    143   1.1     onoe {
    144   1.1     onoe 	struct pcmcia_attach_args *pa = aux;
    145   1.1     onoe 
    146  1.22  mycroft 	if (pcmcia_product_lookup(pa, an_pcmcia_products, an_pcmcia_nproducts,
    147  1.22  mycroft 	    sizeof(an_pcmcia_products[0]), NULL))
    148  1.22  mycroft 		return (1);
    149  1.22  mycroft 	return (0);
    150   1.1     onoe }
    151   1.1     onoe 
    152   1.1     onoe static void
    153   1.1     onoe an_pcmcia_attach(parent, self, aux)
    154   1.1     onoe 	struct device  *parent, *self;
    155   1.1     onoe 	void           *aux;
    156   1.1     onoe {
    157   1.1     onoe 	struct an_pcmcia_softc *psc = (void *)self;
    158   1.1     onoe 	struct an_softc *sc = &psc->sc_an;
    159   1.1     onoe 	struct pcmcia_attach_args *pa = aux;
    160   1.1     onoe 	struct pcmcia_config_entry *cfe;
    161   1.8     onoe 
    162  1.19  mycroft 	aprint_normal("\n");
    163   1.1     onoe 
    164   1.7     onoe 	psc->sc_pf = pa->pf;
    165   1.7     onoe 	if ((cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head)) == NULL) {
    166  1.17     onoe 		printf("%s: no suitable CIS info found\n", sc->sc_dev.dv_xname);
    167   1.7     onoe 		goto fail1;
    168   1.7     onoe 	}
    169   1.7     onoe 
    170   1.7     onoe 	if (pcmcia_io_alloc(psc->sc_pf, cfe->iospace[0].start,
    171   1.7     onoe 	    cfe->iospace[0].length, AN_IOSIZ, &psc->sc_pcioh) != 0) {
    172   1.8     onoe 		printf("%s: failed to allocate io space\n",
    173  1.17     onoe 		    sc->sc_dev.dv_xname);
    174   1.7     onoe 		goto fail1;
    175   1.7     onoe 	}
    176   1.1     onoe 
    177  1.20  mycroft 	if (pcmcia_io_map(psc->sc_pf, PCMCIA_WIDTH_AUTO, &psc->sc_pcioh,
    178  1.20  mycroft 	    &psc->sc_io_window) != 0) {
    179  1.17     onoe 		printf("%s: failed to map io space\n", sc->sc_dev.dv_xname);
    180   1.7     onoe 		goto fail2;
    181   1.7     onoe 	}
    182   1.1     onoe 
    183  1.21  mycroft 	sc->sc_iot = psc->sc_pcioh.iot;
    184  1.21  mycroft 	sc->sc_ioh = psc->sc_pcioh.ioh;
    185  1.21  mycroft 
    186   1.7     onoe 	pcmcia_function_init(psc->sc_pf, cfe);
    187   1.1     onoe 
    188  1.21  mycroft 	if (an_pcmcia_enable(sc)) {
    189  1.21  mycroft 		printf("%s: enable failed\n", sc->sc_dev.dv_xname);
    190   1.7     onoe 		goto fail3;
    191   1.1     onoe 	}
    192   1.7     onoe 
    193   1.1     onoe 	sc->sc_enabled = 1;
    194   1.1     onoe 	sc->sc_enable = an_pcmcia_enable;
    195   1.1     onoe 	sc->sc_disable = an_pcmcia_disable;
    196   1.1     onoe 
    197   1.1     onoe 	if (an_attach(sc) != 0) {
    198   1.1     onoe 		printf("%s: failed to attach controller\n",
    199  1.17     onoe 		    sc->sc_dev.dv_xname);
    200  1.21  mycroft 		goto fail4;
    201   1.1     onoe 	}
    202   1.7     onoe 	psc->sc_powerhook = powerhook_establish(an_power, sc);
    203   1.1     onoe 
    204   1.1     onoe 	/* disable device and disestablish the interrupt */
    205  1.21  mycroft 	sc->sc_enabled = 0;
    206   1.1     onoe 	an_pcmcia_disable(sc);
    207   1.1     onoe 	return;
    208   1.1     onoe 
    209  1.21  mycroft fail4:
    210  1.21  mycroft 	sc->sc_enabled = 0;
    211  1.21  mycroft 	an_pcmcia_disable(sc);
    212  1.21  mycroft fail3:
    213   1.1     onoe 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    214  1.21  mycroft fail2:
    215   1.1     onoe 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    216  1.21  mycroft fail1:
    217   1.1     onoe 	psc->sc_io_window = -1;
    218   1.1     onoe }
    219   1.1     onoe 
    220   1.1     onoe 
    221   1.1     onoe static int
    222   1.1     onoe an_pcmcia_detach(self, flags)
    223   1.1     onoe 	struct device *self;
    224   1.1     onoe 	int flags;
    225   1.1     onoe {
    226   1.1     onoe 	struct an_pcmcia_softc *psc = (struct an_pcmcia_softc *)self;
    227   1.1     onoe 	int error;
    228   1.1     onoe 
    229   1.1     onoe 	if (psc->sc_io_window == -1)
    230   1.1     onoe 		/* Nothing to detach. */
    231   1.1     onoe 		return (0);
    232   1.1     onoe 
    233   1.1     onoe 	if (psc->sc_powerhook != NULL)
    234   1.1     onoe 		powerhook_disestablish(psc->sc_powerhook);
    235   1.1     onoe 
    236   1.1     onoe 	error = an_detach(&psc->sc_an);
    237   1.1     onoe 	if (error != 0)
    238   1.1     onoe 		return (error);
    239   1.1     onoe 
    240   1.1     onoe 	/* Unmap our i/o window. */
    241   1.1     onoe 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    242   1.1     onoe 
    243   1.1     onoe 	/* Free our i/o space. */
    244   1.1     onoe 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    245   1.1     onoe 	return (0);
    246   1.1     onoe }
    247