Home | History | Annotate | Line # | Download | only in pcmcia
if_an_pcmcia.c revision 1.36
      1 /* $NetBSD: if_an_pcmcia.c,v 1.36 2008/07/03 18:10:08 drochner 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: if_an_pcmcia.c,v 1.36 2008/07/03 18:10:08 drochner Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/callout.h>
     38 #include <sys/mbuf.h>
     39 #include <sys/socket.h>
     40 #include <sys/ioctl.h>
     41 #include <sys/errno.h>
     42 #include <sys/syslog.h>
     43 #include <sys/select.h>
     44 #include <sys/device.h>
     45 
     46 #include <net/if.h>
     47 #include <net/if_dl.h>
     48 #include <net/if_ether.h>
     49 #include <net/if_media.h>
     50 
     51 #include <net80211/ieee80211_netbsd.h>
     52 #include <net80211/ieee80211_var.h>
     53 
     54 #include <sys/cpu.h>
     55 #include <sys/bus.h>
     56 #include <sys/intr.h>
     57 
     58 #include <dev/ic/anreg.h>
     59 #include <dev/ic/anvar.h>
     60 
     61 #include <dev/pcmcia/pcmciareg.h>
     62 #include <dev/pcmcia/pcmciavar.h>
     63 #include <dev/pcmcia/pcmciadevs.h>
     64 
     65 static int an_pcmcia_match(struct device *, struct cfdata *, void *);
     66 static int an_pcmcia_validate_config(struct pcmcia_config_entry *);
     67 static void an_pcmcia_attach(struct device *, struct device *, void *);
     68 static int an_pcmcia_detach(struct device *, int);
     69 static int an_pcmcia_enable(struct an_softc *);
     70 static void an_pcmcia_disable(struct an_softc *);
     71 
     72 struct an_pcmcia_softc {
     73 	struct an_softc sc_an;			/* real "an" softc */
     74 
     75 	/* PCMCIA-specific goo */
     76 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     77 	int sc_io_window;			/* our i/o window */
     78 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     79 	void *sc_ih;				/* interrupt handle */
     80 
     81 	int sc_state;
     82 #define	AN_PCMCIA_ATTACHED	3
     83 };
     84 
     85 CFATTACH_DECL_NEW(an_pcmcia, sizeof(struct an_pcmcia_softc),
     86     an_pcmcia_match, an_pcmcia_attach, an_pcmcia_detach, an_activate);
     87 
     88 static const struct pcmcia_product an_pcmcia_products[] = {
     89 	{ PCMCIA_VENDOR_AIRONET,	PCMCIA_PRODUCT_AIRONET_PC4800,
     90 	  PCMCIA_CIS_AIRONET_PC4800 },
     91 	{ PCMCIA_VENDOR_AIRONET,	PCMCIA_PRODUCT_AIRONET_PC4500,
     92 	  PCMCIA_CIS_AIRONET_PC4500 },
     93 	{ PCMCIA_VENDOR_AIRONET,	PCMCIA_PRODUCT_AIRONET_350,
     94 	  PCMCIA_CIS_AIRONET_350 },
     95 };
     96 static const size_t an_pcmcia_nproducts =
     97     sizeof(an_pcmcia_products) / sizeof(an_pcmcia_products[0]);
     98 
     99 static int
    100 an_pcmcia_match(struct device *parent, struct cfdata *match,
    101     void *aux)
    102 {
    103 	struct pcmcia_attach_args *pa = aux;
    104 
    105 	if (pcmcia_product_lookup(pa, an_pcmcia_products, an_pcmcia_nproducts,
    106 	    sizeof(an_pcmcia_products[0]), NULL))
    107 		return (1);
    108 	return (0);
    109 }
    110 
    111 static int
    112 an_pcmcia_validate_config(cfe)
    113 	struct pcmcia_config_entry *cfe;
    114 {
    115 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
    116 	    cfe->num_iospace < 1)
    117 		return (EINVAL);
    118 	return (0);
    119 }
    120 
    121 static void
    122 an_pcmcia_attach(struct device  *parent, struct device *self,
    123     void *aux)
    124 {
    125 	struct an_pcmcia_softc *psc = device_private(self);
    126 	struct an_softc *sc = &psc->sc_an;
    127 	struct pcmcia_attach_args *pa = aux;
    128 	struct pcmcia_config_entry *cfe;
    129 	int error;
    130 
    131 	sc->sc_dev = self;
    132 	psc->sc_pf = pa->pf;
    133 
    134 	error = pcmcia_function_configure(pa->pf, an_pcmcia_validate_config);
    135 	if (error) {
    136 		aprint_error_dev(self, "configure failed, error=%d\n",
    137 		    error);
    138 		return;
    139 	}
    140 
    141 	cfe = pa->pf->cfe;
    142 	sc->sc_iot = cfe->iospace[0].handle.iot;
    143 	sc->sc_ioh = cfe->iospace[0].handle.ioh;
    144 
    145 	error = an_pcmcia_enable(sc);
    146 	if (error)
    147 		goto fail;
    148 
    149 	sc->sc_enabled = 1;
    150 	sc->sc_enable = an_pcmcia_enable;
    151 	sc->sc_disable = an_pcmcia_disable;
    152 
    153 	error = an_attach(sc);
    154 	if (error) {
    155 		aprint_error_dev(self, "failed to attach controller\n");
    156 		goto fail2;
    157 	}
    158 
    159 	if (!pmf_device_register(self, NULL, NULL))
    160 		aprint_error_dev(self, "couldn't establish power handler\n");
    161 	else
    162 		pmf_class_network_register(self, &sc->sc_if);
    163 
    164 	an_pcmcia_disable(sc);
    165 	sc->sc_enabled = 0;
    166 	psc->sc_state = AN_PCMCIA_ATTACHED;
    167 	return;
    168 
    169 fail2:
    170 	an_pcmcia_disable(sc);
    171 	sc->sc_enabled = 0;
    172 fail:
    173 	pcmcia_function_unconfigure(pa->pf);
    174 }
    175 
    176 
    177 static int
    178 an_pcmcia_detach(struct device *self, int flags)
    179 {
    180 	struct an_pcmcia_softc *psc = device_private(self);
    181 	int error;
    182 
    183 	if (psc->sc_state != AN_PCMCIA_ATTACHED)
    184 		return (0);
    185 
    186 	pmf_device_deregister(self);
    187 
    188 	error = an_detach(&psc->sc_an);
    189 	if (error)
    190 		return (error);
    191 
    192 	pcmcia_function_unconfigure(psc->sc_pf);
    193 
    194 	return (0);
    195 }
    196 
    197 static int
    198 an_pcmcia_enable(sc)
    199 	struct an_softc *sc;
    200 {
    201 	struct an_pcmcia_softc *psc = (void *)sc;
    202 	int error;
    203 
    204 	/* establish the interrupt. */
    205 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, an_intr, sc);
    206 	if (!psc->sc_ih)
    207 		return (EIO);
    208 
    209 	error = pcmcia_function_enable(psc->sc_pf);
    210 	if (error) {
    211 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    212 		psc->sc_ih = 0;
    213 	}
    214 
    215 	return (error);
    216 }
    217 
    218 static void
    219 an_pcmcia_disable(sc)
    220 	struct an_softc *sc;
    221 {
    222 	struct an_pcmcia_softc *psc = (void *)sc;
    223 
    224 	pcmcia_function_disable(psc->sc_pf);
    225 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    226 	psc->sc_ih = 0;
    227 }
    228