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