Home | History | Annotate | Line # | Download | only in pcmcia
if_an_pcmcia.c revision 1.31.22.1
      1  1.31.22.1     joerg /* $NetBSD: if_an_pcmcia.c,v 1.31.22.1 2007/09/04 15:10:14 joerg 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.31.22.1     joerg __KERNEL_RCSID(0, "$NetBSD: if_an_pcmcia.c,v 1.31.22.1 2007/09/04 15:10:14 joerg 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.27    dyoung #include <net80211/ieee80211_netbsd.h>
     59       1.16    dyoung #include <net80211/ieee80211_var.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.26     perry static int an_pcmcia_match(struct device *, struct cfdata *, void *);
     73       1.26     perry static int an_pcmcia_validate_config(struct pcmcia_config_entry *);
     74       1.26     perry static void an_pcmcia_attach(struct device *, struct device *, void *);
     75       1.26     perry static int an_pcmcia_detach(struct device *, int);
     76       1.26     perry static int an_pcmcia_enable(struct an_softc *);
     77       1.26     perry static void an_pcmcia_disable(struct an_softc *);
     78        1.1      onoe 
     79        1.1      onoe struct an_pcmcia_softc {
     80        1.5      onoe 	struct an_softc sc_an;			/* real "an" softc */
     81  1.31.22.1     joerg 	pnp_state_t sc_power_state;
     82        1.1      onoe 
     83        1.1      onoe 	/* PCMCIA-specific goo */
     84        1.1      onoe 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     85        1.1      onoe 	int sc_io_window;			/* our i/o window */
     86        1.1      onoe 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     87        1.5      onoe 	void *sc_ih;				/* interrupt handle */
     88       1.25   mycroft 
     89       1.25   mycroft 	int sc_state;
     90       1.25   mycroft #define	AN_PCMCIA_ATTACHED	3
     91        1.1      onoe };
     92        1.1      onoe 
     93       1.13   thorpej CFATTACH_DECL(an_pcmcia, sizeof(struct an_pcmcia_softc),
     94       1.14   thorpej     an_pcmcia_match, an_pcmcia_attach, an_pcmcia_detach, an_activate);
     95        1.1      onoe 
     96       1.24   mycroft static const struct pcmcia_product an_pcmcia_products[] = {
     97        1.1      onoe 	{ PCMCIA_VENDOR_AIRONET,	PCMCIA_PRODUCT_AIRONET_PC4800,
     98       1.18   mycroft 	  PCMCIA_CIS_AIRONET_PC4800 },
     99        1.4      onoe 	{ PCMCIA_VENDOR_AIRONET,	PCMCIA_PRODUCT_AIRONET_PC4500,
    100       1.18   mycroft 	  PCMCIA_CIS_AIRONET_PC4500 },
    101        1.9      onoe 	{ PCMCIA_VENDOR_AIRONET,	PCMCIA_PRODUCT_AIRONET_350,
    102       1.18   mycroft 	  PCMCIA_CIS_AIRONET_350 },
    103        1.1      onoe };
    104       1.22   mycroft static const size_t an_pcmcia_nproducts =
    105       1.22   mycroft     sizeof(an_pcmcia_products) / sizeof(an_pcmcia_products[0]);
    106        1.1      onoe 
    107        1.1      onoe static int
    108       1.31  christos an_pcmcia_match(struct device *parent, struct cfdata *match,
    109       1.30  christos     void *aux)
    110        1.1      onoe {
    111        1.1      onoe 	struct pcmcia_attach_args *pa = aux;
    112        1.1      onoe 
    113       1.22   mycroft 	if (pcmcia_product_lookup(pa, an_pcmcia_products, an_pcmcia_nproducts,
    114       1.22   mycroft 	    sizeof(an_pcmcia_products[0]), NULL))
    115       1.22   mycroft 		return (1);
    116       1.22   mycroft 	return (0);
    117        1.1      onoe }
    118        1.1      onoe 
    119       1.25   mycroft static int
    120       1.25   mycroft an_pcmcia_validate_config(cfe)
    121       1.25   mycroft 	struct pcmcia_config_entry *cfe;
    122       1.25   mycroft {
    123       1.25   mycroft 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
    124       1.25   mycroft 	    cfe->num_iospace < 1)
    125       1.25   mycroft 		return (EINVAL);
    126       1.25   mycroft 	return (0);
    127       1.25   mycroft }
    128       1.25   mycroft 
    129  1.31.22.1     joerg static pnp_status_t
    130  1.31.22.1     joerg an_pcmcia_power(device_t dv, pnp_request_t req, void *opaque)
    131  1.31.22.1     joerg {
    132  1.31.22.1     joerg 	struct an_pcmcia_softc *sc = device_private(dv);
    133  1.31.22.1     joerg 
    134  1.31.22.1     joerg 	return pcmcia_net_generic_power(dv, req, opaque, &sc->sc_power_state,
    135  1.31.22.1     joerg 	    &sc->sc_an.sc_if);
    136  1.31.22.1     joerg }
    137  1.31.22.1     joerg 
    138        1.1      onoe static void
    139       1.31  christos an_pcmcia_attach(struct device  *parent, struct device *self,
    140       1.30  christos     void *aux)
    141        1.1      onoe {
    142        1.1      onoe 	struct an_pcmcia_softc *psc = (void *)self;
    143        1.1      onoe 	struct an_softc *sc = &psc->sc_an;
    144        1.1      onoe 	struct pcmcia_attach_args *pa = aux;
    145        1.1      onoe 	struct pcmcia_config_entry *cfe;
    146       1.25   mycroft 	int error;
    147        1.8      onoe 
    148        1.7      onoe 	psc->sc_pf = pa->pf;
    149  1.31.22.1     joerg 	psc->sc_power_state = PNP_STATE_D0;
    150        1.7      onoe 
    151       1.25   mycroft 	error = pcmcia_function_configure(pa->pf, an_pcmcia_validate_config);
    152       1.25   mycroft 	if (error) {
    153       1.25   mycroft 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
    154       1.25   mycroft 		    error);
    155       1.25   mycroft 		return;
    156        1.7      onoe 	}
    157        1.1      onoe 
    158       1.25   mycroft 	cfe = pa->pf->cfe;
    159       1.25   mycroft 	sc->sc_iot = cfe->iospace[0].handle.iot;
    160       1.25   mycroft 	sc->sc_ioh = cfe->iospace[0].handle.ioh;
    161       1.25   mycroft 
    162       1.25   mycroft 	error = an_pcmcia_enable(sc);
    163       1.25   mycroft 	if (error)
    164       1.25   mycroft 		goto fail;
    165        1.7      onoe 
    166        1.1      onoe 	sc->sc_enabled = 1;
    167        1.1      onoe 	sc->sc_enable = an_pcmcia_enable;
    168        1.1      onoe 	sc->sc_disable = an_pcmcia_disable;
    169        1.1      onoe 
    170       1.25   mycroft 	error = an_attach(sc);
    171       1.25   mycroft 	if (error) {
    172       1.25   mycroft 		aprint_error("%s: failed to attach controller\n",
    173       1.25   mycroft 		    self->dv_xname);
    174       1.25   mycroft 		goto fail2;
    175        1.1      onoe 	}
    176       1.25   mycroft 
    177  1.31.22.1     joerg 	if (pnp_register(self, an_pcmcia_power) != PNP_STATUS_SUCCESS)
    178  1.31.22.1     joerg 		aprint_error("%s: couldn't establish power handler\n",
    179  1.31.22.1     joerg 		    device_xname(self));
    180        1.1      onoe 
    181       1.25   mycroft 	an_pcmcia_disable(sc);
    182       1.21   mycroft 	sc->sc_enabled = 0;
    183       1.25   mycroft 	psc->sc_state = AN_PCMCIA_ATTACHED;
    184        1.1      onoe 	return;
    185        1.1      onoe 
    186       1.25   mycroft fail2:
    187       1.25   mycroft 	an_pcmcia_disable(sc);
    188       1.21   mycroft 	sc->sc_enabled = 0;
    189       1.25   mycroft fail:
    190       1.25   mycroft 	pcmcia_function_unconfigure(pa->pf);
    191        1.1      onoe }
    192        1.1      onoe 
    193        1.1      onoe 
    194        1.1      onoe static int
    195       1.31  christos an_pcmcia_detach(struct device *self, int flags)
    196        1.1      onoe {
    197       1.25   mycroft 	struct an_pcmcia_softc *psc = (void *)self;
    198        1.1      onoe 	int error;
    199        1.1      onoe 
    200       1.25   mycroft 	if (psc->sc_state != AN_PCMCIA_ATTACHED)
    201        1.1      onoe 		return (0);
    202        1.1      onoe 
    203  1.31.22.1     joerg 	pnp_deregister(self);
    204        1.1      onoe 
    205        1.1      onoe 	error = an_detach(&psc->sc_an);
    206       1.25   mycroft 	if (error)
    207        1.1      onoe 		return (error);
    208        1.1      onoe 
    209       1.25   mycroft 	pcmcia_function_unconfigure(psc->sc_pf);
    210        1.1      onoe 
    211        1.1      onoe 	return (0);
    212        1.1      onoe }
    213       1.25   mycroft 
    214       1.25   mycroft static int
    215       1.25   mycroft an_pcmcia_enable(sc)
    216       1.25   mycroft 	struct an_softc *sc;
    217       1.25   mycroft {
    218       1.25   mycroft 	struct an_pcmcia_softc *psc = (void *)sc;
    219       1.25   mycroft 	int error;
    220       1.25   mycroft 
    221       1.25   mycroft 	/* establish the interrupt. */
    222       1.25   mycroft 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, an_intr, sc);
    223       1.25   mycroft 	if (!psc->sc_ih)
    224       1.25   mycroft 		return (EIO);
    225       1.25   mycroft 
    226       1.25   mycroft 	error = pcmcia_function_enable(psc->sc_pf);
    227       1.25   mycroft 	if (error) {
    228       1.25   mycroft 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    229       1.25   mycroft 		psc->sc_ih = 0;
    230       1.25   mycroft 	}
    231       1.25   mycroft 
    232       1.25   mycroft 	return (error);
    233       1.25   mycroft }
    234       1.25   mycroft 
    235       1.25   mycroft static void
    236       1.25   mycroft an_pcmcia_disable(sc)
    237       1.25   mycroft 	struct an_softc *sc;
    238       1.25   mycroft {
    239       1.25   mycroft 	struct an_pcmcia_softc *psc = (void *)sc;
    240       1.25   mycroft 
    241       1.25   mycroft 	pcmcia_function_disable(psc->sc_pf);
    242       1.25   mycroft 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    243       1.25   mycroft 	psc->sc_ih = 0;
    244       1.25   mycroft }
    245