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