Home | History | Annotate | Line # | Download | only in pcmcia
if_awi_pcmcia.c revision 1.30
      1 /* $NetBSD: if_awi_pcmcia.c,v 1.30 2004/08/10 16:43:47 mycroft Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1999, 2004 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Bill Sommerfeld
      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 /*
     40  * PCMCIA attachment for BayStack 650 802.11FH PCMCIA card,
     41  * based on the AMD 79c930 802.11 controller chip.
     42  *
     43  * This attachment can probably be trivially adapted for other FH and
     44  * DS cards based on the same chipset.
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: if_awi_pcmcia.c,v 1.30 2004/08/10 16:43:47 mycroft Exp $");
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/mbuf.h>
     53 #include <sys/socket.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/errno.h>
     56 #include <sys/syslog.h>
     57 #include <sys/select.h>
     58 #include <sys/device.h>
     59 
     60 #include <net/if.h>
     61 #include <net/if_dl.h>
     62 #include <net/if_ether.h>
     63 #include <net/if_media.h>
     64 
     65 #include <net80211/ieee80211_var.h>
     66 #include <net80211/ieee80211_compat.h>
     67 
     68 #include <machine/cpu.h>
     69 #include <machine/bus.h>
     70 #include <machine/intr.h>
     71 
     72 #include <dev/ic/am79c930reg.h>
     73 #include <dev/ic/am79c930var.h>
     74 #include <dev/ic/awireg.h>
     75 #include <dev/ic/awivar.h>
     76 
     77 #include <dev/pcmcia/pcmciareg.h>
     78 #include <dev/pcmcia/pcmciavar.h>
     79 #include <dev/pcmcia/pcmciadevs.h>
     80 
     81 static int awi_pcmcia_match(struct device *, struct cfdata *, void *);
     82 static int awi_pcmcia_validate_config(struct pcmcia_config_entry *);
     83 static void awi_pcmcia_attach(struct device *, struct device *, void *);
     84 static int awi_pcmcia_detach(struct device *, int);
     85 static int awi_pcmcia_enable(struct awi_softc *);
     86 static void awi_pcmcia_disable(struct awi_softc *);
     87 
     88 struct awi_pcmcia_softc {
     89 	struct awi_softc sc_awi;		/* real "awi" softc */
     90 
     91 	/* PCMCIA-specific goo */
     92 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     93 	void *sc_ih;				/* interrupt handler */
     94 
     95 	int sc_state;
     96 #define	AWI_PCMCIA_ATTACH1	1
     97 #define	AWI_PCMCIA_ATTACH2	2
     98 #define	AWI_PCMCIA_ATTACHED	3
     99 };
    100 
    101 CFATTACH_DECL(awi_pcmcia, sizeof(struct awi_pcmcia_softc),
    102     awi_pcmcia_match, awi_pcmcia_attach, awi_pcmcia_detach, awi_activate);
    103 
    104 static const struct awi_pcmcia_product {
    105 	u_int32_t	app_vendor;	/* vendor ID */
    106 	u_int32_t	app_product;	/* product ID */
    107 	const char	*app_cisinfo[4]; /* CIS information */
    108 	const char	*app_name;	/* product name */
    109 } awi_pcmcia_products[] = {
    110 	{ PCMCIA_VENDOR_BAY,		PCMCIA_PRODUCT_BAY_STACK_650,
    111 	  PCMCIA_CIS_BAY_STACK_650,	"" },
    112 
    113 	{ PCMCIA_VENDOR_BAY,		PCMCIA_PRODUCT_BAY_STACK_660,
    114 	  PCMCIA_CIS_BAY_STACK_660,	"" },
    115 
    116 	{ PCMCIA_VENDOR_BAY,		PCMCIA_PRODUCT_BAY_SURFER_PRO,
    117 	  PCMCIA_CIS_BAY_SURFER_PRO,	"" },
    118 
    119 	{ PCMCIA_VENDOR_AMD,		PCMCIA_PRODUCT_AMD_AM79C930,
    120 	  PCMCIA_CIS_AMD_AM79C930,	"" },
    121 
    122 	{ PCMCIA_VENDOR_ICOM,		PCMCIA_PRODUCT_ICOM_SL200,
    123 	  PCMCIA_CIS_ICOM_SL200,	"" },
    124 
    125 	{ PCMCIA_VENDOR_NOKIA,		PCMCIA_PRODUCT_NOKIA_C020_WLAN,
    126 	  PCMCIA_CIS_NOKIA_C020_WLAN,	"" },
    127 
    128 	{ PCMCIA_VENDOR_FARALLON,	PCMCIA_PRODUCT_FARALLON_SKYLINE,
    129 	  PCMCIA_CIS_FARALLON_SKYLINE,	"" },
    130 
    131 	{ 0,				0,
    132 	  { NULL, NULL, NULL, NULL },	NULL },
    133 };
    134 
    135 static const struct awi_pcmcia_product *
    136 	awi_pcmcia_lookup __P((struct pcmcia_attach_args *));
    137 
    138 static const struct awi_pcmcia_product *
    139 awi_pcmcia_lookup(pa)
    140 	struct pcmcia_attach_args *pa;
    141 {
    142 	const struct awi_pcmcia_product *app;
    143 
    144 	for (app = awi_pcmcia_products; app->app_name != NULL; app++) {
    145 		/* match by vendor/product id */
    146 		if (pa->manufacturer != PCMCIA_VENDOR_INVALID &&
    147 		    pa->manufacturer == app->app_vendor &&
    148 		    pa->product != PCMCIA_PRODUCT_INVALID &&
    149 		    pa->product == app->app_product)
    150 			return (app);
    151 
    152 		/* match by CIS information */
    153 		if (pa->card->cis1_info[0] != NULL &&
    154 		    app->app_cisinfo[0] != NULL &&
    155 		    strcmp(pa->card->cis1_info[0], app->app_cisinfo[0]) == 0 &&
    156 		    pa->card->cis1_info[1] != NULL &&
    157 		    app->app_cisinfo[1] != NULL &&
    158 		    strcmp(pa->card->cis1_info[1], app->app_cisinfo[1]) == 0)
    159 			return (app);
    160 	}
    161 
    162 	return (NULL);
    163 }
    164 
    165 static int
    166 awi_pcmcia_enable(sc)
    167 	struct awi_softc *sc;
    168 {
    169 	struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc;
    170 	struct pcmcia_function *pf = psc->sc_pf;
    171 	int error;
    172 
    173 	if (psc->sc_state == AWI_PCMCIA_ATTACH1) {
    174 		psc->sc_state = AWI_PCMCIA_ATTACH2;
    175 		return (0);
    176 	}
    177 
    178 	/* establish the interrupt. */
    179 	psc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, awi_intr, sc);
    180 	if (!psc->sc_ih)
    181 		return (EIO);
    182 
    183 	error = pcmcia_function_enable(pf);
    184 	if (error) {
    185 		pcmcia_intr_disestablish(pf, psc->sc_ih);
    186 		psc->sc_ih = 0;
    187 	}
    188 
    189 	return (error);
    190 }
    191 
    192 static void
    193 awi_pcmcia_disable(sc)
    194 	struct awi_softc *sc;
    195 {
    196 	struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc;
    197 	struct pcmcia_function *pf = psc->sc_pf;
    198 
    199 	pcmcia_function_disable(pf);
    200 	pcmcia_intr_disestablish(pf, psc->sc_ih);
    201 	psc->sc_ih = 0;
    202 }
    203 
    204 static int
    205 awi_pcmcia_match(parent, match, aux)
    206 	struct device *parent;
    207 	struct cfdata *match;
    208 	void *aux;
    209 {
    210 	struct pcmcia_attach_args *pa = aux;
    211 
    212 	if (awi_pcmcia_lookup(pa) != NULL)
    213 		return (1);
    214 
    215 	return (0);
    216 }
    217 
    218 static int
    219 awi_pcmcia_validate_config(cfe)
    220 	struct pcmcia_config_entry *cfe;
    221 {
    222 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
    223 	    cfe->num_iospace < 1 ||
    224 	    cfe->iospace[0].length < AM79C930_IO_SIZE)
    225 		return (EINVAL);
    226 	if (cfe->num_memspace < 1) {
    227 		cfe->memspace[0].length = AM79C930_MEM_SIZE;
    228 		cfe->memspace[0].cardaddr = 0;
    229 		cfe->memspace[0].hostaddr = 0;
    230 	} else if (cfe->memspace[0].length < AM79C930_MEM_SIZE)
    231 		return (EINVAL);
    232 	return (0);
    233 }
    234 
    235 static void
    236 awi_pcmcia_attach(parent, self, aux)
    237 	struct device  *parent, *self;
    238 	void           *aux;
    239 {
    240 	struct awi_pcmcia_softc *psc = (void *)self;
    241 	struct awi_softc *sc = &psc->sc_awi;
    242 	struct pcmcia_attach_args *pa = aux;
    243 	struct pcmcia_config_entry *cfe;
    244 	int error;
    245 
    246 	aprint_normal("\n");
    247 	psc->sc_pf = pa->pf;
    248 
    249 	error = pcmcia_function_configure(pa->pf, awi_pcmcia_validate_config);
    250 	if (error) {
    251 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
    252 		    error);
    253 		return;
    254 	}
    255 
    256 	cfe = pa->pf->cfe;
    257 	sc->sc_chip.sc_bustype = AM79C930_BUS_PCMCIA;
    258 	sc->sc_chip.sc_iot = cfe->iospace[0].handle.iot;
    259 	sc->sc_chip.sc_ioh = cfe->iospace[0].handle.ioh;
    260 	if (cfe->num_memspace > 0) {
    261 		sc->sc_chip.sc_memt = cfe->memspace[0].handle.memt;
    262 		sc->sc_chip.sc_memh = cfe->memspace[0].handle.memh;
    263 		am79c930_chip_init(&sc->sc_chip, 1);
    264 	} else
    265 		am79c930_chip_init(&sc->sc_chip, 0);
    266 
    267 	error = awi_pcmcia_enable(sc);
    268         if (error)
    269                 goto fail;
    270 
    271 	awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
    272 	if (memcmp(sc->sc_banner, "PCnetMobile:", 12))
    273 		goto fail2;
    274 
    275 	sc->sc_enable = awi_pcmcia_enable;
    276 	sc->sc_disable = awi_pcmcia_disable;
    277 
    278 	sc->sc_cansleep = 1;
    279 
    280 	psc->sc_state = AWI_PCMCIA_ATTACH1;
    281 	sc->sc_enabled = 1;
    282 	if (awi_attach(sc) != 0) {
    283 		printf("%s: failed to attach controller\n", self->dv_xname);
    284 		goto fail2;
    285 	}
    286 
    287 	if (psc->sc_state == AWI_PCMCIA_ATTACH1)
    288 		awi_pcmcia_disable(sc);
    289 	sc->sc_enabled = 0;
    290 	psc->sc_state = AWI_PCMCIA_ATTACHED;
    291 	return;
    292 
    293 fail2:
    294 	awi_pcmcia_disable(sc);
    295 	sc->sc_enabled = 0;
    296 fail:
    297 	pcmcia_function_unconfigure(pa->pf);
    298 }
    299 
    300 static int
    301 awi_pcmcia_detach(self, flags)
    302 	struct device *self;
    303 	int flags;
    304 {
    305 	struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)self;
    306 	int error;
    307 
    308 	if (psc->sc_state != AWI_PCMCIA_ATTACHED)
    309 		return (0);
    310 
    311 	error = awi_detach(&psc->sc_awi);
    312 	if (error)
    313 		return (error);
    314 
    315 	pcmcia_function_unconfigure(psc->sc_pf);
    316 
    317 	return (0);
    318 }
    319