Home | History | Annotate | Line # | Download | only in pcmcia
if_wi_pcmcia.c revision 1.2
      1 /* $NetBSD: if_wi_pcmcia.c,v 1.2 2001/05/15 04:14:07 ichiro Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Ichiro FUKUHARA (ichiro (at) ichiro.org).
      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 Lucent & Intersil WaveLAN PCMCIA card
     41  */
     42 
     43 #include "opt_inet.h"
     44 #include "opt_ns.h"
     45 #include "bpfilter.h"
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/callout.h>
     50 #include <sys/device.h>
     51 #include <sys/socket.h>
     52 
     53 #include <net/if.h>
     54 #include <net/if_ether.h>
     55 #include <net/if_media.h>
     56 #include <net/if_ieee80211.h>
     57 
     58 #ifdef INET
     59 #include <netinet/in.h>
     60 #include <netinet/in_systm.h>
     61 #include <netinet/in_var.h>
     62 #include <netinet/ip.h>
     63 #include <netinet/if_inarp.h>
     64 #endif
     65 
     66 #ifdef NS
     67 #include <netns/ns.h>
     68 #include <netns/ns_if.h>
     69 #endif
     70 
     71 #if NBPFILTER > 0
     72 #include <net/bpf.h>
     73 #include <net/bpfdesc.h>
     74 #endif
     75 
     76 #include <machine/cpu.h>
     77 #include <machine/bus.h>
     78 #include <machine/intr.h>
     79 
     80 #include <dev/ic/wi_ieee.h>
     81 #include <dev/ic/wireg.h>
     82 #include <dev/ic/wivar.h>
     83 
     84 #include <dev/pcmcia/pcmciareg.h>
     85 #include <dev/pcmcia/pcmciavar.h>
     86 #include <dev/pcmcia/pcmciadevs.h>
     87 
     88 static int	wi_pcmcia_match __P((struct device *, struct cfdata *, void *));
     89 static void	wi_pcmcia_attach __P((struct device *, struct device *, void *));
     90 static int	wi_pcmcia_detach __P((struct device *, int));
     91 static int	wi_pcmcia_enable __P((struct wi_softc *));
     92 static void	wi_pcmcia_disable __P((struct wi_softc *));
     93 static void	wi_pcmcia_powerhook __P((int, void *));
     94 static void	wi_pcmcia_shutdown __P((void *));
     95 
     96 static const struct wi_pcmcia_product
     97 		*wi_pcmcia_lookup __P((struct pcmcia_attach_args *pa));
     98 
     99 struct wi_pcmcia_softc {
    100 	struct wi_softc sc_wi;
    101 
    102 	/* PCMCIA-specific */
    103 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
    104 	int sc_io_window;			/* our i/o window */
    105 	struct pcmcia_function *sc_pf;		/* PCMCIA function */
    106 	void *sc_powerhook;			/* power hook descriptor */
    107 	void *sc_sdhook;			/* shutdown hook */
    108 
    109 };
    110 
    111 static int wi_pcmcia_find __P((struct wi_pcmcia_softc *,
    112 	struct pcmcia_attach_args *, struct pcmcia_config_entry *));
    113 
    114 struct cfattach wi_pcmcia_ca = {
    115 	sizeof(struct wi_pcmcia_softc), wi_pcmcia_match, wi_pcmcia_attach,
    116 	wi_pcmcia_detach, wi_activate,
    117 };
    118 
    119 static const struct wi_pcmcia_product {
    120 	u_int32_t	pp_vendor;	/* vendor ID */
    121 	u_int32_t	pp_product;	/* product ID */
    122 	const char	*pp_cisinfo[4];	/* CIS information */
    123 	const char	*pp_name;	/* product name */
    124 } wi_pcmcia_products[] = {
    125 	{ PCMCIA_VENDOR_LUCENT,
    126 	  PCMCIA_PRODUCT_LUCENT_WAVELAN_IEEE,
    127 	  PCMCIA_CIS_LUCENT_WAVELAN_IEEE,
    128 	  PCMCIA_STR_LUCENT_WAVELAN_IEEE },
    129 
    130 	{ PCMCIA_VENDOR_3COM,
    131 	  PCMCIA_PRODUCT_3COM_3CRWE737A,
    132 	  PCMCIA_CIS_3COM_3CRWE737A,
    133 	  PCMCIA_STR_3COM_3CRWE737A },
    134 
    135 	{ PCMCIA_VENDOR_COREGA,
    136 	  PCMCIA_PRODUCT_COREGA_WIRELESS_LAN_PCC_11,
    137 	  PCMCIA_CIS_COREGA_WIRELESS_LAN_PCC_11,
    138 	  PCMCIA_STR_COREGA_WIRELESS_LAN_PCC_11 },
    139 
    140 	{ PCMCIA_VENDOR_COREGA,
    141 	  PCMCIA_PRODUCT_COREGA_WIRELESS_LAN_PCCA_11,
    142 	  PCMCIA_CIS_COREGA_WIRELESS_LAN_PCCA_11,
    143 	  PCMCIA_STR_COREGA_WIRELESS_LAN_PCCA_11 },
    144 
    145 	{ PCMCIA_VENDOR_INTERSIL,
    146 	  PCMCIA_PRODUCT_INTERSIL_PRISM2,
    147 	  PCMCIA_CIS_INTERSIL_PRISM2,
    148 	  PCMCIA_STR_INTERSIL_PRISM2 },
    149 
    150 	{ PCMCIA_VENDOR_SAMSUNG,
    151 	  PCMCIA_PRODUCT_SAMSUNG_SWL_2000N,
    152 	  PCMCIA_CIS_SAMSUNG_SWL_2000N,
    153 	  PCMCIA_STR_SAMSUNG_SWL_2000N },
    154 
    155 	{ PCMCIA_VENDOR_LUCENT,
    156 	  PCMCIA_PRODUCT_LUCENT_WAVELAN_IEEE,
    157 	  PCMCIA_CIS_SMC_2632W,
    158 	  PCMCIA_STR_SMC_2632W },
    159 
    160 	{ PCMCIA_VENDOR_LUCENT,
    161 	  PCMCIA_PRODUCT_LUCENT_WAVELAN_IEEE,
    162 	  PCMCIA_CIS_NANOSPEED_PRISM2,
    163 	  PCMCIA_STR_NANOSPEED_PRISM2 },
    164 
    165 	{ PCMCIA_VENDOR_ELSA,
    166 	  PCMCIA_PRODUCT_ELSA_XI300_IEEE,
    167 	  PCMCIA_CIS_ELSA_XI300_IEEE,
    168 	  PCMCIA_STR_ELSA_XI300_IEEE },
    169 
    170 	{ PCMCIA_VENDOR_COMPAQ,
    171 	  PCMCIA_PRODUCT_COMPAQ_NC5004,
    172 	  PCMCIA_CIS_COMPAQ_NC5004,
    173 	  PCMCIA_STR_COMPAQ_NC5004 },
    174 
    175 	{ PCMCIA_VENDOR_CONTEC,
    176 	  PCMCIA_PRODUCT_CONTEC_FX_DS110_PCC,
    177 	  PCMCIA_CIS_CONTEC_FX_DS110_PCC,
    178 	  PCMCIA_STR_CONTEC_FX_DS110_PCC },
    179 
    180 	{ PCMCIA_VENDOR_TDK,
    181 	  PCMCIA_PRODUCT_TDK_LAK_CD011WL,
    182 	  PCMCIA_CIS_TDK_LAK_CD011WL,
    183 	  PCMCIA_STR_TDK_LAK_CD011WL },
    184 
    185 	{ PCMCIA_VENDOR_LUCENT,
    186 	  PCMCIA_PRODUCT_LUCENT_WAVELAN_IEEE,
    187 	  PCMCIA_CIS_NEC_CMZ_RT_WP,
    188 	  PCMCIA_STR_NEC_CMZ_RT_WP },
    189 
    190 	{ PCMCIA_VENDOR_LUCENT,
    191 	  PCMCIA_PRODUCT_LUCENT_WAVELAN_IEEE,
    192 	  PCMCIA_CIS_NTT_ME_WLAN,
    193 	  PCMCIA_STR_NTT_ME_WLAN },
    194 
    195 	{ PCMCIA_VENDOR_IODATA2,
    196 	  PCMCIA_PRODUCT_IODATA2_WNB11PCM,
    197 	  PCMCIA_CIS_IODATA2_WNB11PCM,
    198 	  PCMCIA_STR_IODATA2_WNB11PCM },
    199 #if 0
    200 	{ PCMCIA_VENDOR_EMTAC,
    201 	  PCMCIA_PRODUCT_EMTAC_WLAN,
    202 	  PCMCIA_CIS_EMTAC_WLAN,
    203 	  PCMCIA_STR_EMTAC_WLAN },
    204 #endif
    205 	{ 0,
    206 	  0,
    207 	  { NULL, NULL, NULL, NULL },
    208 	  NULL }
    209 };
    210 
    211 static const struct wi_pcmcia_product *
    212 wi_pcmcia_lookup(pa)
    213 	struct pcmcia_attach_args *pa;
    214 {
    215 	const struct wi_pcmcia_product *pp;
    216 
    217 	/* match by CIS information */
    218 	for (pp = wi_pcmcia_products; pp->pp_name != NULL; pp++) {
    219 		if (pa->card->cis1_info[0] != NULL &&
    220 		    pp->pp_cisinfo[0] != NULL &&
    221 		    strcmp(pa->card->cis1_info[0], pp->pp_cisinfo[0]) == 0 &&
    222 		    pa->card->cis1_info[1] != NULL &&
    223 		    pp->pp_cisinfo[1] != NULL &&
    224 		    strcmp(pa->card->cis1_info[1], pp->pp_cisinfo[1]) == 0)
    225 			return pp;
    226 	}
    227 
    228 	/* match by vendor/product id */
    229 	for (pp = wi_pcmcia_products; pp->pp_name != NULL; pp++) {
    230 		if (pa->manufacturer != PCMCIA_VENDOR_INVALID &&
    231 		    pa->manufacturer == pp->pp_vendor &&
    232 		    pa->product != PCMCIA_PRODUCT_INVALID &&
    233 		    pa->product == pp->pp_product)
    234 			return pp;
    235 	}
    236 
    237 	return (NULL);
    238 }
    239 
    240 static int
    241 wi_pcmcia_match(parent, match, aux)
    242 	struct device *parent;
    243 	struct cfdata *match;
    244 	void *aux;
    245 {
    246 	struct pcmcia_attach_args *pa = aux;
    247 
    248 	if (wi_pcmcia_lookup(pa) != NULL)
    249 		return (1);
    250 	return (0);
    251 }
    252 
    253 static int
    254 wi_pcmcia_enable(sc)
    255 	struct wi_softc *sc;
    256 {
    257 	struct wi_pcmcia_softc *psc = (struct wi_pcmcia_softc *)sc;
    258 	struct pcmcia_function *pf = psc->sc_pf;
    259 
    260 	/* establish the interrupt. */
    261 	sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, wi_intr, sc);
    262 	if (sc->sc_ih == NULL) {
    263 		printf("%s: couldn't establish interrupt\n",
    264 		    sc->sc_dev.dv_xname);
    265 		return (EIO);
    266 	}
    267 	if (pcmcia_function_enable(pf) != 0) {
    268 		printf("%s: couldn't enable card\n", sc->sc_dev.dv_xname);
    269 		pcmcia_intr_disestablish(pf, sc->sc_ih);
    270 		return (EIO);
    271 	}
    272 	DELAY(1000);
    273 	return (0);
    274 }
    275 
    276 static void
    277 wi_pcmcia_disable(sc)
    278 	struct wi_softc *sc;
    279 {
    280 	struct wi_pcmcia_softc *psc = (struct wi_pcmcia_softc *)sc;
    281 
    282 	pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    283 	pcmcia_function_disable(psc->sc_pf);
    284 }
    285 
    286 static int
    287 wi_pcmcia_find(psc, pa, cfe)
    288 	struct wi_pcmcia_softc *psc;
    289 	struct pcmcia_attach_args *pa;
    290 	struct pcmcia_config_entry *cfe;
    291 {
    292 	struct wi_softc *sc = &psc->sc_wi;
    293 
    294 	/* Allocate/map I/O space. */
    295 	if (pcmcia_io_alloc(psc->sc_pf, cfe->iospace[0].start,
    296 	    cfe->iospace[0].length, WI_IOSIZE,
    297 	    &psc->sc_pcioh) != 0) {
    298 		printf("%s: can't allocate i/o space\n",
    299 			sc->sc_dev.dv_xname);
    300 		goto fail1;
    301 	}
    302 	printf("%s:", sc->sc_dev.dv_xname);
    303 	if (pcmcia_io_map(psc->sc_pf, PCMCIA_WIDTH_AUTO, 0,
    304 	    psc->sc_pcioh.size, &psc->sc_pcioh,
    305 	    &psc->sc_io_window) != 0) {
    306 		printf(" can't map i/o space\n");
    307 		goto fail2;
    308 	}
    309 	/* Enable the card */
    310 	pcmcia_function_init(psc->sc_pf, cfe);
    311 	if (pcmcia_function_enable(psc->sc_pf)) {
    312 		printf("%s: function enable failed\n", sc->sc_dev.dv_xname);
    313 		goto fail3;
    314 	}
    315 
    316 	sc->sc_iot = psc->sc_pcioh.iot;
    317 	sc->sc_ioh = psc->sc_pcioh.ioh;
    318 
    319 	DELAY(1000);
    320 	return(0);
    321 
    322 fail3:
    323 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    324 fail2:
    325 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    326 fail1:
    327 	psc->sc_io_window = -1;
    328 	return(1);
    329 }
    330 
    331 static void
    332 wi_pcmcia_attach(parent, self, aux)
    333 	struct device  *parent, *self;
    334 	void           *aux;
    335 {
    336 	struct wi_pcmcia_softc *psc = (void *)self;
    337 	struct wi_softc *sc = &psc->sc_wi;
    338 	const struct wi_pcmcia_product *pp;
    339 	struct pcmcia_attach_args *pa = aux;
    340 	struct pcmcia_config_entry *cfe;
    341 	char devinfo[256];
    342 
    343 	/* Print out what we are. */
    344 	pcmcia_devinfo(&pa->pf->sc->card, 0, devinfo, sizeof(devinfo));
    345 	printf(": %s\n", devinfo);
    346 
    347 	psc->sc_pf = pa->pf;
    348 
    349 	for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
    350 	     cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
    351 		if (cfe->iftype != PCMCIA_IFTYPE_IO)
    352 			continue;
    353 		if (cfe->iospace[0].length < WI_IOSIZE)
    354 			continue;
    355 		if (wi_pcmcia_find(psc, pa, cfe) == 0)
    356 			break;
    357 	}
    358 	if (cfe == NULL) {
    359 		printf(": no suitable CIS info found\n");
    360 		goto no_config_entry;
    361 	}
    362 
    363 	pp = wi_pcmcia_lookup(pa);
    364 	if (pp == NULL)
    365 		panic("wi_pcmcia_attach: impossible");
    366 
    367 	sc->sc_enabled = 1;
    368 	sc->sc_enable = wi_pcmcia_enable;
    369 	sc->sc_disable = wi_pcmcia_disable;
    370 
    371 	/* establish the interrupt. */
    372 	sc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, wi_intr, sc);
    373 	if (sc->sc_ih == NULL) {
    374 		printf("%s: couldn't establish interrupt\n",
    375 		        sc->sc_dev.dv_xname);
    376 		goto no_interrupt;
    377 	}
    378 
    379 	sc->sc_ifp = &sc->sc_ethercom.ec_if;
    380 	if (wi_attach(sc) != 0) {
    381 		printf("%s: failed to attach controller\n",
    382 		    sc->sc_dev.dv_xname);
    383 			goto attach_failed;
    384 	}
    385 
    386 	psc->sc_sdhook    = shutdownhook_establish(wi_pcmcia_shutdown, psc);
    387 	psc->sc_powerhook = powerhook_establish(wi_pcmcia_powerhook, psc);
    388 
    389 	/* disable the card */
    390 	sc->sc_enabled = 0;
    391 	wi_pcmcia_disable(sc);
    392 
    393 	return;
    394 
    395 attach_failed:
    396 	pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    397 no_interrupt:
    398 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    399 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    400 no_config_entry:
    401 	psc->sc_io_window = -1;
    402 }
    403 
    404 static int
    405 wi_pcmcia_detach(self, flags)
    406 	struct device *self;
    407 	int flags;
    408 {
    409 	struct wi_pcmcia_softc *psc = (struct wi_pcmcia_softc *)self;
    410 	int error;
    411 
    412 	if (psc->sc_io_window == -1)
    413 		/* Nothing to detach. */
    414 		return (0);
    415 
    416 	if (psc->sc_powerhook != NULL)
    417 		powerhook_disestablish(psc->sc_powerhook);
    418 	if (psc->sc_sdhook != NULL)
    419 		shutdownhook_disestablish(psc->sc_sdhook);
    420 
    421 	error = wi_detach(&psc->sc_wi);
    422 	if (error != 0)
    423 		return (error);
    424 
    425 	/* Unmap our i/o window. */
    426 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    427 
    428 	/* Free our i/o space. */
    429 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    430 	return (0);
    431 }
    432 
    433 static void
    434 wi_pcmcia_powerhook(why, arg)
    435 	int why;
    436 	void *arg;
    437 {
    438 	struct wi_pcmcia_softc *psc = arg;
    439 	struct wi_softc *sc = &psc->sc_wi;
    440 
    441 	wi_power(sc, why);
    442 }
    443 
    444 static void
    445 wi_pcmcia_shutdown(arg)
    446 	void *arg;
    447 {
    448 	struct wi_pcmcia_softc *psc = arg;
    449 	struct wi_softc *sc = &psc->sc_wi;
    450 
    451 	wi_shutdown(sc);
    452 }
    453