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