Home | History | Annotate | Line # | Download | only in pcmcia
if_awi_pcmcia.c revision 1.15
      1 /* $NetBSD: if_awi_pcmcia.c,v 1.15 2000/06/09 05:31:19 onoe Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1999 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 trivally adapted for other FH and
     44  * DS cards based on the same chipset.
     45  */
     46 
     47 #include "opt_awi.h"
     48 #include "opt_inet.h"
     49 #include "opt_ns.h"
     50 #include "bpfilter.h"
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/mbuf.h>
     55 #include <sys/socket.h>
     56 #include <sys/ioctl.h>
     57 #include <sys/errno.h>
     58 #include <sys/syslog.h>
     59 #include <sys/select.h>
     60 #include <sys/device.h>
     61 
     62 #include <net/if.h>
     63 #include <net/if_dl.h>
     64 #include <net/if_ether.h>
     65 #include <net/if_media.h>
     66 #include <net/if_ieee80211.h>
     67 
     68 #ifdef INET
     69 #include <netinet/in.h>
     70 #include <netinet/in_systm.h>
     71 #include <netinet/in_var.h>
     72 #include <netinet/ip.h>
     73 #include <netinet/if_inarp.h>
     74 #endif
     75 
     76 #ifdef NS
     77 #include <netns/ns.h>
     78 #include <netns/ns_if.h>
     79 #endif
     80 
     81 #if NBPFILTER > 0
     82 #include <net/bpf.h>
     83 #include <net/bpfdesc.h>
     84 #endif
     85 
     86 #include <machine/cpu.h>
     87 #include <machine/bus.h>
     88 #include <machine/intr.h>
     89 
     90 #include <dev/ic/am79c930reg.h>
     91 #include <dev/ic/am79c930var.h>
     92 #include <dev/ic/awireg.h>
     93 #include <dev/ic/awivar.h>
     94 
     95 #include <dev/pcmcia/pcmciareg.h>
     96 #include <dev/pcmcia/pcmciavar.h>
     97 #include <dev/pcmcia/pcmciadevs.h>
     98 
     99 static int awi_pcmcia_match __P((struct device *, struct cfdata *, void *));
    100 static void awi_pcmcia_attach __P((struct device *, struct device *, void *));
    101 static int awi_pcmcia_detach __P((struct device *, int));
    102 static int awi_pcmcia_enable __P((struct awi_softc *));
    103 static void awi_pcmcia_disable __P((struct awi_softc *));
    104 static void awi_pcmcia_powerhook __P((int, void *));
    105 
    106 struct awi_pcmcia_softc {
    107 	struct awi_softc sc_awi;		/* real "awi" softc */
    108 
    109 	/* PCMCIA-specific goo */
    110 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
    111 	struct pcmcia_mem_handle sc_memh;	/* PCMCIA memory space info */
    112 	int sc_io_window;			/* our i/o window */
    113 	int sc_mem_window;			/* our memory window */
    114 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
    115 	void *sc_powerhook;			/* power hook descriptor */
    116 };
    117 
    118 static int	awi_pcmcia_find __P((struct awi_pcmcia_softc *,
    119     struct pcmcia_attach_args *, struct pcmcia_config_entry *));
    120 
    121 struct cfattach awi_pcmcia_ca = {
    122 	sizeof(struct awi_pcmcia_softc), awi_pcmcia_match, awi_pcmcia_attach,
    123 	awi_pcmcia_detach, awi_activate
    124 };
    125 
    126 static struct awi_pcmcia_product {
    127 	u_int32_t	app_vendor;	/* vendor ID */
    128 	u_int32_t	app_product;	/* product ID */
    129 	const char	*app_cisinfo[4]; /* CIS information */
    130 	const char	*app_name;	/* product name */
    131 } awi_pcmcia_products[] = {
    132 	{ PCMCIA_VENDOR_BAY,		PCMCIA_PRODUCT_BAY_STACK_650,
    133 	  PCMCIA_CIS_BAY_STACK_650,	PCMCIA_STR_BAY_STACK_650 },
    134 
    135 	{ PCMCIA_VENDOR_BAY,		PCMCIA_PRODUCT_BAY_STACK_660,
    136 	  PCMCIA_CIS_BAY_STACK_660,	PCMCIA_STR_BAY_STACK_660 },
    137 
    138 	{ PCMCIA_VENDOR_BAY,		PCMCIA_PRODUCT_BAY_SURFER_PRO,
    139 	  PCMCIA_CIS_BAY_SURFER_PRO,	PCMCIA_STR_BAY_SURFER_PRO },
    140 
    141 	{ PCMCIA_VENDOR_AMD,		PCMCIA_PRODUCT_AMD_AM79C930,
    142 	  PCMCIA_CIS_AMD_AM79C930,	PCMCIA_STR_AMD_AM79C930 },
    143 
    144 	{ PCMCIA_VENDOR_ICOM,		PCMCIA_PRODUCT_ICOM_SL200,
    145 	  PCMCIA_CIS_ICOM_SL200,	PCMCIA_STR_ICOM_SL200 },
    146 
    147 	{ PCMCIA_VENDOR_NOKIA,		PCMCIA_PRODUCT_NOKIA_C020_WLAN,
    148 	  PCMCIA_CIS_NOKIA_C020_WLAN,	PCMCIA_STR_NOKIA_C020_WLAN },
    149 
    150 	{ 0,				0,
    151 	  { NULL, NULL, NULL, NULL },	NULL },
    152 };
    153 
    154 static struct awi_pcmcia_product *
    155 	awi_pcmcia_lookup __P((struct pcmcia_attach_args *));
    156 
    157 static struct awi_pcmcia_product *
    158 awi_pcmcia_lookup(pa)
    159 	struct pcmcia_attach_args *pa;
    160 {
    161 	struct awi_pcmcia_product *app;
    162 
    163 	for (app = awi_pcmcia_products; app->app_name != NULL; app++) {
    164 		/* match by vendor/product id */
    165 		if (pa->manufacturer != PCMCIA_VENDOR_INVALID &&
    166 		    pa->manufacturer == app->app_vendor &&
    167 		    pa->product != PCMCIA_PRODUCT_INVALID &&
    168 		    pa->product == app->app_product)
    169 			return (app);
    170 
    171 		/* match by CIS information */
    172 		if (pa->card->cis1_info[0] != NULL &&
    173 		    app->app_cisinfo[0] != NULL &&
    174 		    strcmp(pa->card->cis1_info[0], app->app_cisinfo[0]) == 0 &&
    175 		    pa->card->cis1_info[1] != NULL &&
    176 		    app->app_cisinfo[1] != NULL &&
    177 		    strcmp(pa->card->cis1_info[1], app->app_cisinfo[1]) == 0)
    178 			return (app);
    179 	}
    180 
    181 	return (NULL);
    182 }
    183 
    184 static int
    185 awi_pcmcia_enable(sc)
    186 	struct awi_softc *sc;
    187 {
    188 	struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc;
    189 	struct pcmcia_function *pf = psc->sc_pf;
    190 
    191 	/* establish the interrupt. */
    192 	sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, awi_intr, sc);
    193 	if (sc->sc_ih == NULL) {
    194 		printf("%s: couldn't establish interrupt\n",
    195 		    sc->sc_dev.dv_xname);
    196 		return (1);
    197 	}
    198 
    199 	if (pcmcia_function_enable(pf)) {
    200 		pcmcia_intr_disestablish(pf, sc->sc_ih);
    201 		return (1);
    202 	}
    203 	DELAY(1000);
    204 
    205 	return (0);
    206 }
    207 
    208 static void
    209 awi_pcmcia_disable(sc)
    210 	struct awi_softc *sc;
    211 {
    212 	struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc;
    213 	struct pcmcia_function *pf = psc->sc_pf;
    214 
    215 	pcmcia_function_disable(pf);
    216 	pcmcia_intr_disestablish(pf, sc->sc_ih);
    217 }
    218 
    219 static int
    220 awi_pcmcia_match(parent, match, aux)
    221 	struct device *parent;
    222 	struct cfdata *match;
    223 	void *aux;
    224 {
    225 	struct pcmcia_attach_args *pa = aux;
    226 
    227 	if (awi_pcmcia_lookup(pa) != NULL)
    228 		return (1);
    229 
    230 	return (0);
    231 }
    232 
    233 static int
    234 awi_pcmcia_find(psc, pa, cfe)
    235 	struct awi_pcmcia_softc *psc;
    236 	struct pcmcia_attach_args *pa;
    237 	struct pcmcia_config_entry *cfe;
    238 {
    239 	struct awi_softc *sc = &psc->sc_awi;
    240 	int fail = 0;
    241 
    242 	/*
    243 	 * see if we can read the firmware version sanely
    244 	 * through the i/o ports.
    245 	 * if not, try a different CIS string..
    246 	 */
    247 	if (pcmcia_io_alloc(psc->sc_pf, cfe->iospace[0].start,
    248 	    cfe->iospace[0].length, AM79C930_IO_ALIGN,
    249 	    &psc->sc_pcioh) != 0)
    250 		goto fail;
    251 
    252 	if (pcmcia_io_map(psc->sc_pf, PCMCIA_WIDTH_AUTO, 0, psc->sc_pcioh.size,
    253 	    &psc->sc_pcioh, &psc->sc_io_window))
    254 		goto fail_io_free;
    255 
    256 	/* Enable the card. */
    257 	pcmcia_function_init(psc->sc_pf, cfe);
    258 	if (pcmcia_function_enable(psc->sc_pf))
    259 		goto fail_io_unmap;
    260 
    261 	sc->sc_chip.sc_bustype = AM79C930_BUS_PCMCIA;
    262 	sc->sc_chip.sc_iot = psc->sc_pcioh.iot;
    263 	sc->sc_chip.sc_ioh = psc->sc_pcioh.ioh;
    264 	am79c930_chip_init(&sc->sc_chip, 0);
    265 
    266 	DELAY(1000);
    267 
    268 	awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
    269 
    270 	if (memcmp(sc->sc_banner, "PCnetMobile:", 12) == 0)
    271 		return (0);
    272 
    273 	fail++;
    274 	pcmcia_function_disable(psc->sc_pf);
    275 
    276  fail_io_unmap:
    277 	fail++;
    278 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    279 
    280  fail_io_free:
    281 	fail++;
    282 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    283  fail:
    284 	fail++;
    285 	psc->sc_io_window = -1;
    286 	return (fail);
    287 }
    288 
    289 static void
    290 awi_pcmcia_attach(parent, self, aux)
    291 	struct device  *parent, *self;
    292 	void           *aux;
    293 {
    294 	struct awi_pcmcia_softc *psc = (void *)self;
    295 	struct awi_softc *sc = &psc->sc_awi;
    296 	struct awi_pcmcia_product *app;
    297 	struct pcmcia_attach_args *pa = aux;
    298 	struct pcmcia_config_entry *cfe;
    299 	bus_addr_t memoff;
    300 
    301 	app = awi_pcmcia_lookup(pa);
    302 	if (app == NULL)
    303 		panic("awi_pcmcia_attach: impossible");
    304 
    305 	psc->sc_pf = pa->pf;
    306 
    307 	for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
    308 	     cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
    309 		if (cfe->iftype != PCMCIA_IFTYPE_IO)
    310 			continue;
    311 		if (cfe->num_iospace < 1)
    312 			continue;
    313 		if (cfe->iospace[0].length < AM79C930_IO_SIZE)
    314 			continue;
    315 
    316 		if (awi_pcmcia_find(psc, pa, cfe) == 0)
    317 			break;
    318 	}
    319 	if (cfe == NULL) {
    320 		printf(": no suitable CIS info found\n");
    321 		goto no_config_entry;
    322 	}
    323 
    324 	sc->sc_enabled = 1;
    325 	printf(": %s\n", app->app_name);
    326 
    327 	psc->sc_mem_window = -1;
    328 	if (pcmcia_mem_alloc(psc->sc_pf, AM79C930_MEM_SIZE,
    329 	    &psc->sc_memh) != 0) {
    330 		printf("%s: unable to allocate memory space; using i/o only\n",
    331 		    sc->sc_dev.dv_xname);
    332 	} else if (pcmcia_mem_map(psc->sc_pf,
    333 	    PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON, AM79C930_MEM_BASE,
    334 	    AM79C930_MEM_SIZE, &psc->sc_memh, &memoff, &psc->sc_mem_window)) {
    335 		printf("%s: unable to map memory space; using i/o only\n",
    336 		    sc->sc_dev.dv_xname);
    337 		pcmcia_mem_free(psc->sc_pf, &psc->sc_memh);
    338 	} else {
    339 		sc->sc_chip.sc_memt = psc->sc_memh.memt;
    340 		sc->sc_chip.sc_memh = psc->sc_memh.memh;
    341 		am79c930_chip_init(&sc->sc_chip, 1);
    342 	}
    343 
    344 	sc->sc_enable = awi_pcmcia_enable;
    345 	sc->sc_disable = awi_pcmcia_disable;
    346 
    347 	/* establish the interrupt. */
    348 	sc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, awi_intr, sc);
    349 	if (sc->sc_ih == NULL) {
    350 		printf("%s: couldn't establish interrupt\n",
    351 		    sc->sc_dev.dv_xname);
    352 		goto no_interrupt;
    353 	}
    354 	sc->sc_ifp = &sc->sc_ec.ec_if;
    355 	sc->sc_cansleep = 1;
    356 
    357 	if (awi_attach(sc) != 0) {
    358 		printf("%s: failed to attach controller\n",
    359 		    sc->sc_dev.dv_xname);
    360 		goto attach_failed;
    361 	}
    362 	psc->sc_powerhook = powerhook_establish(awi_pcmcia_powerhook, psc);
    363 
    364 	sc->sc_enabled = 0;
    365 	/* disable device and disestablish the interrupt */
    366 	awi_pcmcia_disable(sc);
    367 	return;
    368 
    369  attach_failed:
    370 	pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    371 
    372  no_interrupt:
    373 	/* Unmap our memory window and space */
    374 	if (psc->sc_mem_window != -1) {
    375 		pcmcia_mem_unmap(psc->sc_pf, psc->sc_mem_window);
    376 		pcmcia_mem_free(psc->sc_pf, &psc->sc_memh);
    377 	}
    378 
    379 	/* Unmap our i/o window and space */
    380 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    381 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    382 
    383 	/* Disable the function */
    384 	pcmcia_function_disable(psc->sc_pf);
    385 
    386  no_config_entry:
    387 	psc->sc_io_window = -1;
    388 }
    389 
    390 
    391 static int
    392 awi_pcmcia_detach(self, flags)
    393 	struct device *self;
    394 	int flags;
    395 {
    396 	struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)self;
    397 	int error;
    398 
    399 	if (psc->sc_io_window == -1)
    400 		/* Nothing to detach. */
    401 		return (0);
    402 
    403 	if (psc->sc_powerhook != NULL)
    404 		powerhook_disestablish(psc->sc_powerhook);
    405 
    406 	error = awi_detach(&psc->sc_awi);
    407 	if (error != 0)
    408 		return (error);
    409 
    410 	/* Unmap our memory window and free memory space */
    411 	if (psc->sc_mem_window != -1) {
    412 		pcmcia_mem_unmap(psc->sc_pf, psc->sc_mem_window);
    413 		pcmcia_mem_free(psc->sc_pf, &psc->sc_memh);
    414 	}
    415 
    416 	/* Unmap our i/o window. */
    417 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    418 
    419 	/* Free our i/o space. */
    420 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    421 	return (0);
    422 }
    423 
    424 static void
    425 awi_pcmcia_powerhook(why, arg)
    426 	int why;
    427 	void *arg;
    428 {
    429 	struct awi_pcmcia_softc *psc = arg;
    430 	struct awi_softc *sc = &psc->sc_awi;
    431 
    432 	awi_power(sc, why);
    433 }
    434