Home | History | Annotate | Line # | Download | only in pcmcia
      1  1.41       chs /* $NetBSD: if_an_pcmcia.c,v 1.41 2012/10/27 17:18:36 chs 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.41       chs __KERNEL_RCSID(0, "$NetBSD: if_an_pcmcia.c,v 1.41 2012/10/27 17:18:36 chs 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.39    cegger static int an_pcmcia_match(device_t, cfdata_t, void *);
     66  1.26     perry static int an_pcmcia_validate_config(struct pcmcia_config_entry *);
     67  1.39    cegger static void an_pcmcia_attach(device_t, device_t, void *);
     68  1.39    cegger static int an_pcmcia_detach(device_t, 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.36  drochner CFATTACH_DECL_NEW(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.41       chs an_pcmcia_match(device_t parent, cfdata_t match, void *aux)
    101   1.1      onoe {
    102   1.1      onoe 	struct pcmcia_attach_args *pa = aux;
    103   1.1      onoe 
    104  1.22   mycroft 	if (pcmcia_product_lookup(pa, an_pcmcia_products, an_pcmcia_nproducts,
    105  1.22   mycroft 	    sizeof(an_pcmcia_products[0]), NULL))
    106  1.22   mycroft 		return (1);
    107  1.22   mycroft 	return (0);
    108   1.1      onoe }
    109   1.1      onoe 
    110  1.25   mycroft static int
    111  1.37       dsl an_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
    112  1.25   mycroft {
    113  1.25   mycroft 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
    114  1.25   mycroft 	    cfe->num_iospace < 1)
    115  1.25   mycroft 		return (EINVAL);
    116  1.25   mycroft 	return (0);
    117  1.25   mycroft }
    118  1.25   mycroft 
    119   1.1      onoe static void
    120  1.41       chs an_pcmcia_attach(device_t parent, device_t self, void *aux)
    121   1.1      onoe {
    122  1.36  drochner 	struct an_pcmcia_softc *psc = device_private(self);
    123   1.1      onoe 	struct an_softc *sc = &psc->sc_an;
    124   1.1      onoe 	struct pcmcia_attach_args *pa = aux;
    125   1.1      onoe 	struct pcmcia_config_entry *cfe;
    126  1.25   mycroft 	int error;
    127   1.8      onoe 
    128  1.36  drochner 	sc->sc_dev = self;
    129   1.7      onoe 	psc->sc_pf = pa->pf;
    130   1.7      onoe 
    131  1.25   mycroft 	error = pcmcia_function_configure(pa->pf, an_pcmcia_validate_config);
    132  1.25   mycroft 	if (error) {
    133  1.34    cegger 		aprint_error_dev(self, "configure failed, error=%d\n",
    134  1.25   mycroft 		    error);
    135  1.25   mycroft 		return;
    136   1.7      onoe 	}
    137   1.1      onoe 
    138  1.25   mycroft 	cfe = pa->pf->cfe;
    139  1.25   mycroft 	sc->sc_iot = cfe->iospace[0].handle.iot;
    140  1.25   mycroft 	sc->sc_ioh = cfe->iospace[0].handle.ioh;
    141  1.25   mycroft 
    142  1.25   mycroft 	error = an_pcmcia_enable(sc);
    143  1.25   mycroft 	if (error)
    144  1.25   mycroft 		goto fail;
    145   1.7      onoe 
    146   1.1      onoe 	sc->sc_enabled = 1;
    147   1.1      onoe 	sc->sc_enable = an_pcmcia_enable;
    148   1.1      onoe 	sc->sc_disable = an_pcmcia_disable;
    149   1.1      onoe 
    150  1.25   mycroft 	error = an_attach(sc);
    151  1.25   mycroft 	if (error) {
    152  1.34    cegger 		aprint_error_dev(self, "failed to attach controller\n");
    153  1.25   mycroft 		goto fail2;
    154   1.1      onoe 	}
    155  1.25   mycroft 
    156  1.40   tsutsui 	if (pmf_device_register(self, NULL, NULL))
    157  1.40   tsutsui 		pmf_class_network_register(self, &sc->sc_if);
    158  1.40   tsutsui 	else
    159  1.33  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    160   1.1      onoe 
    161  1.25   mycroft 	an_pcmcia_disable(sc);
    162  1.21   mycroft 	sc->sc_enabled = 0;
    163  1.25   mycroft 	psc->sc_state = AN_PCMCIA_ATTACHED;
    164   1.1      onoe 	return;
    165   1.1      onoe 
    166  1.25   mycroft fail2:
    167  1.25   mycroft 	an_pcmcia_disable(sc);
    168  1.21   mycroft 	sc->sc_enabled = 0;
    169  1.25   mycroft fail:
    170  1.25   mycroft 	pcmcia_function_unconfigure(pa->pf);
    171   1.1      onoe }
    172   1.1      onoe 
    173   1.1      onoe 
    174   1.1      onoe static int
    175  1.39    cegger an_pcmcia_detach(device_t self, int flags)
    176   1.1      onoe {
    177  1.36  drochner 	struct an_pcmcia_softc *psc = device_private(self);
    178   1.1      onoe 	int error;
    179   1.1      onoe 
    180  1.25   mycroft 	if (psc->sc_state != AN_PCMCIA_ATTACHED)
    181   1.1      onoe 		return (0);
    182   1.1      onoe 
    183  1.33  jmcneill 	pmf_device_deregister(self);
    184   1.1      onoe 
    185   1.1      onoe 	error = an_detach(&psc->sc_an);
    186  1.25   mycroft 	if (error)
    187   1.1      onoe 		return (error);
    188   1.1      onoe 
    189  1.25   mycroft 	pcmcia_function_unconfigure(psc->sc_pf);
    190   1.1      onoe 
    191   1.1      onoe 	return (0);
    192   1.1      onoe }
    193  1.25   mycroft 
    194  1.25   mycroft static int
    195  1.37       dsl an_pcmcia_enable(struct an_softc *sc)
    196  1.25   mycroft {
    197  1.25   mycroft 	struct an_pcmcia_softc *psc = (void *)sc;
    198  1.25   mycroft 	int error;
    199  1.25   mycroft 
    200  1.25   mycroft 	/* establish the interrupt. */
    201  1.25   mycroft 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, an_intr, sc);
    202  1.25   mycroft 	if (!psc->sc_ih)
    203  1.25   mycroft 		return (EIO);
    204  1.25   mycroft 
    205  1.25   mycroft 	error = pcmcia_function_enable(psc->sc_pf);
    206  1.25   mycroft 	if (error) {
    207  1.25   mycroft 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    208  1.25   mycroft 		psc->sc_ih = 0;
    209  1.25   mycroft 	}
    210  1.25   mycroft 
    211  1.25   mycroft 	return (error);
    212  1.25   mycroft }
    213  1.25   mycroft 
    214  1.25   mycroft static void
    215  1.37       dsl an_pcmcia_disable(struct an_softc *sc)
    216  1.25   mycroft {
    217  1.25   mycroft 	struct an_pcmcia_softc *psc = (void *)sc;
    218  1.25   mycroft 
    219  1.25   mycroft 	pcmcia_function_disable(psc->sc_pf);
    220  1.25   mycroft 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    221  1.25   mycroft 	psc->sc_ih = 0;
    222  1.25   mycroft }
    223