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