Home | History | Annotate | Line # | Download | only in pcmcia
if_awi_pcmcia.c revision 1.16
      1 /* $NetBSD: if_awi_pcmcia.c,v 1.16 2000/06/09 13:05:02 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 	{ PCMCIA_VENDOR_FARALLON,	PCMCIA_PRODUCT_FARALLON_SKYLINE,
    151 	  PCMCIA_CIS_FARALLON_SKYLINE,	PCMCIA_STR_FARALLON_SKYLINE },
    152 
    153 	{ 0,				0,
    154 	  { NULL, NULL, NULL, NULL },	NULL },
    155 };
    156 
    157 static struct awi_pcmcia_product *
    158 	awi_pcmcia_lookup __P((struct pcmcia_attach_args *));
    159 
    160 static struct awi_pcmcia_product *
    161 awi_pcmcia_lookup(pa)
    162 	struct pcmcia_attach_args *pa;
    163 {
    164 	struct awi_pcmcia_product *app;
    165 
    166 	for (app = awi_pcmcia_products; app->app_name != NULL; app++) {
    167 		/* match by vendor/product id */
    168 		if (pa->manufacturer != PCMCIA_VENDOR_INVALID &&
    169 		    pa->manufacturer == app->app_vendor &&
    170 		    pa->product != PCMCIA_PRODUCT_INVALID &&
    171 		    pa->product == app->app_product)
    172 			return (app);
    173 
    174 		/* match by CIS information */
    175 		if (pa->card->cis1_info[0] != NULL &&
    176 		    app->app_cisinfo[0] != NULL &&
    177 		    strcmp(pa->card->cis1_info[0], app->app_cisinfo[0]) == 0 &&
    178 		    pa->card->cis1_info[1] != NULL &&
    179 		    app->app_cisinfo[1] != NULL &&
    180 		    strcmp(pa->card->cis1_info[1], app->app_cisinfo[1]) == 0)
    181 			return (app);
    182 	}
    183 
    184 	return (NULL);
    185 }
    186 
    187 static int
    188 awi_pcmcia_enable(sc)
    189 	struct awi_softc *sc;
    190 {
    191 	struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc;
    192 	struct pcmcia_function *pf = psc->sc_pf;
    193 
    194 	/* establish the interrupt. */
    195 	sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, awi_intr, sc);
    196 	if (sc->sc_ih == NULL) {
    197 		printf("%s: couldn't establish interrupt\n",
    198 		    sc->sc_dev.dv_xname);
    199 		return (1);
    200 	}
    201 
    202 	if (pcmcia_function_enable(pf)) {
    203 		pcmcia_intr_disestablish(pf, sc->sc_ih);
    204 		return (1);
    205 	}
    206 	DELAY(1000);
    207 
    208 	return (0);
    209 }
    210 
    211 static void
    212 awi_pcmcia_disable(sc)
    213 	struct awi_softc *sc;
    214 {
    215 	struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc;
    216 	struct pcmcia_function *pf = psc->sc_pf;
    217 
    218 	pcmcia_function_disable(pf);
    219 	pcmcia_intr_disestablish(pf, sc->sc_ih);
    220 }
    221 
    222 static int
    223 awi_pcmcia_match(parent, match, aux)
    224 	struct device *parent;
    225 	struct cfdata *match;
    226 	void *aux;
    227 {
    228 	struct pcmcia_attach_args *pa = aux;
    229 
    230 	if (awi_pcmcia_lookup(pa) != NULL)
    231 		return (1);
    232 
    233 	return (0);
    234 }
    235 
    236 static int
    237 awi_pcmcia_find(psc, pa, cfe)
    238 	struct awi_pcmcia_softc *psc;
    239 	struct pcmcia_attach_args *pa;
    240 	struct pcmcia_config_entry *cfe;
    241 {
    242 	struct awi_softc *sc = &psc->sc_awi;
    243 	int fail = 0;
    244 
    245 	/*
    246 	 * see if we can read the firmware version sanely
    247 	 * through the i/o ports.
    248 	 * if not, try a different CIS string..
    249 	 */
    250 	if (pcmcia_io_alloc(psc->sc_pf, cfe->iospace[0].start,
    251 	    cfe->iospace[0].length, AM79C930_IO_ALIGN,
    252 	    &psc->sc_pcioh) != 0)
    253 		goto fail;
    254 
    255 	if (pcmcia_io_map(psc->sc_pf, PCMCIA_WIDTH_AUTO, 0, psc->sc_pcioh.size,
    256 	    &psc->sc_pcioh, &psc->sc_io_window))
    257 		goto fail_io_free;
    258 
    259 	/* Enable the card. */
    260 	pcmcia_function_init(psc->sc_pf, cfe);
    261 	if (pcmcia_function_enable(psc->sc_pf))
    262 		goto fail_io_unmap;
    263 
    264 	sc->sc_chip.sc_bustype = AM79C930_BUS_PCMCIA;
    265 	sc->sc_chip.sc_iot = psc->sc_pcioh.iot;
    266 	sc->sc_chip.sc_ioh = psc->sc_pcioh.ioh;
    267 	am79c930_chip_init(&sc->sc_chip, 0);
    268 
    269 	DELAY(1000);
    270 
    271 	awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
    272 
    273 	if (memcmp(sc->sc_banner, "PCnetMobile:", 12) == 0)
    274 		return (0);
    275 
    276 	fail++;
    277 	pcmcia_function_disable(psc->sc_pf);
    278 
    279  fail_io_unmap:
    280 	fail++;
    281 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    282 
    283  fail_io_free:
    284 	fail++;
    285 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    286  fail:
    287 	fail++;
    288 	psc->sc_io_window = -1;
    289 	return (fail);
    290 }
    291 
    292 static void
    293 awi_pcmcia_attach(parent, self, aux)
    294 	struct device  *parent, *self;
    295 	void           *aux;
    296 {
    297 	struct awi_pcmcia_softc *psc = (void *)self;
    298 	struct awi_softc *sc = &psc->sc_awi;
    299 	struct awi_pcmcia_product *app;
    300 	struct pcmcia_attach_args *pa = aux;
    301 	struct pcmcia_config_entry *cfe;
    302 	bus_addr_t memoff;
    303 
    304 	app = awi_pcmcia_lookup(pa);
    305 	if (app == NULL)
    306 		panic("awi_pcmcia_attach: impossible");
    307 
    308 	psc->sc_pf = pa->pf;
    309 
    310 	for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
    311 	     cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
    312 		if (cfe->iftype != PCMCIA_IFTYPE_IO)
    313 			continue;
    314 		if (cfe->num_iospace < 1)
    315 			continue;
    316 		if (cfe->iospace[0].length < AM79C930_IO_SIZE)
    317 			continue;
    318 
    319 		if (awi_pcmcia_find(psc, pa, cfe) == 0)
    320 			break;
    321 	}
    322 	if (cfe == NULL) {
    323 		printf(": no suitable CIS info found\n");
    324 		goto no_config_entry;
    325 	}
    326 
    327 	sc->sc_enabled = 1;
    328 	printf(": %s\n", app->app_name);
    329 
    330 	psc->sc_mem_window = -1;
    331 	if (pcmcia_mem_alloc(psc->sc_pf, AM79C930_MEM_SIZE,
    332 	    &psc->sc_memh) != 0) {
    333 		printf("%s: unable to allocate memory space; using i/o only\n",
    334 		    sc->sc_dev.dv_xname);
    335 	} else if (pcmcia_mem_map(psc->sc_pf,
    336 	    PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON, AM79C930_MEM_BASE,
    337 	    AM79C930_MEM_SIZE, &psc->sc_memh, &memoff, &psc->sc_mem_window)) {
    338 		printf("%s: unable to map memory space; using i/o only\n",
    339 		    sc->sc_dev.dv_xname);
    340 		pcmcia_mem_free(psc->sc_pf, &psc->sc_memh);
    341 	} else {
    342 		sc->sc_chip.sc_memt = psc->sc_memh.memt;
    343 		sc->sc_chip.sc_memh = psc->sc_memh.memh;
    344 		am79c930_chip_init(&sc->sc_chip, 1);
    345 	}
    346 
    347 	sc->sc_enable = awi_pcmcia_enable;
    348 	sc->sc_disable = awi_pcmcia_disable;
    349 
    350 	/* establish the interrupt. */
    351 	sc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, awi_intr, sc);
    352 	if (sc->sc_ih == NULL) {
    353 		printf("%s: couldn't establish interrupt\n",
    354 		    sc->sc_dev.dv_xname);
    355 		goto no_interrupt;
    356 	}
    357 	sc->sc_ifp = &sc->sc_ec.ec_if;
    358 	sc->sc_cansleep = 1;
    359 
    360 	if (awi_attach(sc) != 0) {
    361 		printf("%s: failed to attach controller\n",
    362 		    sc->sc_dev.dv_xname);
    363 		goto attach_failed;
    364 	}
    365 	psc->sc_powerhook = powerhook_establish(awi_pcmcia_powerhook, psc);
    366 
    367 	sc->sc_enabled = 0;
    368 	/* disable device and disestablish the interrupt */
    369 	awi_pcmcia_disable(sc);
    370 	return;
    371 
    372  attach_failed:
    373 	pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    374 
    375  no_interrupt:
    376 	/* Unmap our memory window and space */
    377 	if (psc->sc_mem_window != -1) {
    378 		pcmcia_mem_unmap(psc->sc_pf, psc->sc_mem_window);
    379 		pcmcia_mem_free(psc->sc_pf, &psc->sc_memh);
    380 	}
    381 
    382 	/* Unmap our i/o window and space */
    383 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    384 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    385 
    386 	/* Disable the function */
    387 	pcmcia_function_disable(psc->sc_pf);
    388 
    389  no_config_entry:
    390 	psc->sc_io_window = -1;
    391 }
    392 
    393 
    394 static int
    395 awi_pcmcia_detach(self, flags)
    396 	struct device *self;
    397 	int flags;
    398 {
    399 	struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)self;
    400 	int error;
    401 
    402 	if (psc->sc_io_window == -1)
    403 		/* Nothing to detach. */
    404 		return (0);
    405 
    406 	if (psc->sc_powerhook != NULL)
    407 		powerhook_disestablish(psc->sc_powerhook);
    408 
    409 	error = awi_detach(&psc->sc_awi);
    410 	if (error != 0)
    411 		return (error);
    412 
    413 	/* Unmap our memory window and free memory space */
    414 	if (psc->sc_mem_window != -1) {
    415 		pcmcia_mem_unmap(psc->sc_pf, psc->sc_mem_window);
    416 		pcmcia_mem_free(psc->sc_pf, &psc->sc_memh);
    417 	}
    418 
    419 	/* Unmap our i/o window. */
    420 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    421 
    422 	/* Free our i/o space. */
    423 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    424 	return (0);
    425 }
    426 
    427 static void
    428 awi_pcmcia_powerhook(why, arg)
    429 	int why;
    430 	void *arg;
    431 {
    432 	struct awi_pcmcia_softc *psc = arg;
    433 	struct awi_softc *sc = &psc->sc_awi;
    434 
    435 	awi_power(sc, why);
    436 }
    437