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