Home | History | Annotate | Line # | Download | only in pcmcia
if_wi_pcmcia.c revision 1.33
      1 /* $NetBSD: if_wi_pcmcia.c,v 1.33 2003/03/29 16:47:27 perry 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.33 2003/03/29 16:47:27 perry 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/proc.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 #include <machine/cpu.h>
     59 #include <machine/bus.h>
     60 #include <machine/intr.h>
     61 
     62 #include <dev/ic/wi_ieee.h>
     63 #include <dev/ic/wireg.h>
     64 #include <dev/ic/wivar.h>
     65 
     66 #include <dev/pcmcia/pcmciareg.h>
     67 #include <dev/pcmcia/pcmciavar.h>
     68 #include <dev/pcmcia/pcmciadevs.h>
     69 
     70 #include <dev/microcode/wi/spectrum24t_cf.h>
     71 
     72 static int	wi_pcmcia_match __P((struct device *, struct cfdata *, void *));
     73 static void	wi_pcmcia_attach __P((struct device *, struct device *, void *));
     74 static int	wi_pcmcia_detach __P((struct device *, int));
     75 static int	wi_pcmcia_enable __P((struct wi_softc *));
     76 static void	wi_pcmcia_disable __P((struct wi_softc *));
     77 static void	wi_pcmcia_powerhook __P((int, void *));
     78 static void	wi_pcmcia_shutdown __P((void *));
     79 
     80 /* support to download firmware for symbol CF card */
     81 static int	wi_pcmcia_load_firm __P((struct wi_softc *, const void *, int, const void *, int));
     82 static int	wi_pcmcia_write_firm __P((struct wi_softc *, const void *, int, const void *, int));
     83 static int	wi_pcmcia_set_hcr __P((struct wi_softc *, int));
     84 
     85 
     86 static const struct wi_pcmcia_product
     87 		*wi_pcmcia_lookup __P((struct pcmcia_attach_args *pa));
     88 
     89 struct wi_pcmcia_softc {
     90 	struct wi_softc sc_wi;
     91 
     92 	/* PCMCIA-specific */
     93 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     94 	int sc_io_window;			/* our i/o window */
     95 	struct pcmcia_function *sc_pf;		/* PCMCIA function */
     96 	void *sc_powerhook;			/* power hook descriptor */
     97 	void *sc_sdhook;			/* shutdown hook */
     98 	int sc_symbol_cf;			/* Spectrum24t CF card */
     99 };
    100 
    101 static int wi_pcmcia_find __P((struct wi_pcmcia_softc *,
    102 	struct pcmcia_attach_args *, struct pcmcia_config_entry *));
    103 
    104 CFATTACH_DECL(wi_pcmcia, sizeof(struct wi_pcmcia_softc),
    105     wi_pcmcia_match, wi_pcmcia_attach, wi_pcmcia_detach, wi_activate);
    106 
    107 static const struct wi_pcmcia_product {
    108 	u_int32_t	pp_vendor;	/* vendor ID */
    109 	u_int32_t	pp_product;	/* product ID */
    110 	const char	*pp_cisinfo[4];	/* CIS information */
    111 	const char	*pp_name;	/* product name */
    112 } wi_pcmcia_products[] = {
    113 	{ PCMCIA_VENDOR_LUCENT,
    114 	  PCMCIA_PRODUCT_LUCENT_WAVELAN_IEEE,
    115 	  PCMCIA_CIS_LUCENT_WAVELAN_IEEE,
    116 	  PCMCIA_STR_LUCENT_WAVELAN_IEEE },
    117 
    118 	{ PCMCIA_VENDOR_3COM,
    119 	  PCMCIA_PRODUCT_3COM_3CRWE737A,
    120 	  PCMCIA_CIS_3COM_3CRWE737A,
    121 	  PCMCIA_STR_3COM_3CRWE737A },
    122 
    123 	{ PCMCIA_VENDOR_COREGA,
    124 	  PCMCIA_PRODUCT_COREGA_WIRELESS_LAN_PCC_11,
    125 	  PCMCIA_CIS_COREGA_WIRELESS_LAN_PCC_11,
    126 	  PCMCIA_STR_COREGA_WIRELESS_LAN_PCC_11 },
    127 
    128 	{ PCMCIA_VENDOR_COREGA,
    129 	  PCMCIA_PRODUCT_COREGA_WIRELESS_LAN_PCCA_11,
    130 	  PCMCIA_CIS_COREGA_WIRELESS_LAN_PCCA_11,
    131 	  PCMCIA_STR_COREGA_WIRELESS_LAN_PCCA_11 },
    132 
    133 	{ PCMCIA_VENDOR_COREGA,
    134 	  PCMCIA_PRODUCT_COREGA_WIRELESS_LAN_PCCB_11,
    135 	  PCMCIA_CIS_COREGA_WIRELESS_LAN_PCCB_11,
    136 	  PCMCIA_STR_COREGA_WIRELESS_LAN_PCCB_11 },
    137 
    138 	{ PCMCIA_VENDOR_COREGA,
    139 	  PCMCIA_PRODUCT_COREGA_WIRELESS_LAN_PCCL_11,
    140 	  PCMCIA_CIS_COREGA_WIRELESS_LAN_PCCL_11,
    141 	  PCMCIA_STR_COREGA_WIRELESS_LAN_PCCL_11 },
    142 
    143 	{ PCMCIA_VENDOR_INTEL,
    144 	  PCMCIA_PRODUCT_INTEL_PRO_WLAN_2011,
    145 	  PCMCIA_CIS_INTEL_PRO_WLAN_2011,
    146 	  PCMCIA_STR_INTEL_PRO_WLAN_2011 },
    147 
    148 	{ PCMCIA_VENDOR_INTERSIL,
    149 	  PCMCIA_PRODUCT_INTERSIL_PRISM2,
    150 	  PCMCIA_CIS_INTERSIL_PRISM2,
    151 	  PCMCIA_STR_INTERSIL_PRISM2 },
    152 
    153 	{ PCMCIA_VENDOR_SAMSUNG,
    154 	  PCMCIA_PRODUCT_SAMSUNG_SWL_2000N,
    155 	  PCMCIA_CIS_SAMSUNG_SWL_2000N,
    156 	  PCMCIA_STR_SAMSUNG_SWL_2000N },
    157 
    158 	{ PCMCIA_VENDOR_LUCENT,
    159 	  PCMCIA_PRODUCT_LUCENT_WAVELAN_IEEE,
    160 	  PCMCIA_CIS_SMC_2632W,
    161 	  PCMCIA_STR_SMC_2632W },
    162 
    163 	{ PCMCIA_VENDOR_LUCENT,
    164 	  PCMCIA_PRODUCT_LUCENT_WAVELAN_IEEE,
    165 	  PCMCIA_CIS_NANOSPEED_PRISM2,
    166 	  PCMCIA_STR_NANOSPEED_PRISM2 },
    167 
    168 	{ PCMCIA_VENDOR_LINKSYS2,
    169 	  PCMCIA_PRODUCT_LINKSYS2_IWN,
    170 	  PCMCIA_CIS_NANOSPEED_PRISM2,
    171 	  PCMCIA_STR_NANOSPEED_PRISM2 },
    172 
    173 	{ PCMCIA_VENDOR_ELSA,
    174 	  PCMCIA_PRODUCT_ELSA_XI300_IEEE,
    175 	  PCMCIA_CIS_ELSA_XI300_IEEE,
    176 	  PCMCIA_STR_ELSA_XI300_IEEE },
    177 
    178 	{ PCMCIA_VENDOR_ELSA,
    179 	  PCMCIA_PRODUCT_ELSA_XI325_IEEE,
    180 	  PCMCIA_CIS_ELSA_XI325_IEEE,
    181 	  PCMCIA_STR_ELSA_XI325_IEEE },
    182 
    183 	{ PCMCIA_VENDOR_ELSA,
    184 	  PCMCIA_PRODUCT_ELSA_XI800_IEEE,
    185 	  PCMCIA_CIS_ELSA_XI800_IEEE,
    186 	  PCMCIA_STR_ELSA_XI800_IEEE },
    187 
    188 	{ PCMCIA_VENDOR_COMPAQ,
    189 	  PCMCIA_PRODUCT_COMPAQ_NC5004,
    190 	  PCMCIA_CIS_COMPAQ_NC5004,
    191 	  PCMCIA_STR_COMPAQ_NC5004 },
    192 
    193 	{ PCMCIA_VENDOR_CONTEC,
    194 	  PCMCIA_PRODUCT_CONTEC_FX_DS110_PCC,
    195 	  PCMCIA_CIS_CONTEC_FX_DS110_PCC,
    196 	  PCMCIA_STR_CONTEC_FX_DS110_PCC },
    197 
    198 	{ PCMCIA_VENDOR_TDK,
    199 	  PCMCIA_PRODUCT_TDK_LAK_CD011WL,
    200 	  PCMCIA_CIS_TDK_LAK_CD011WL,
    201 	  PCMCIA_STR_TDK_LAK_CD011WL },
    202 
    203 	{ PCMCIA_VENDOR_LUCENT,
    204 	  PCMCIA_PRODUCT_LUCENT_WAVELAN_IEEE,
    205 	  PCMCIA_CIS_NEC_CMZ_RT_WP,
    206 	  PCMCIA_STR_NEC_CMZ_RT_WP },
    207 
    208 	{ PCMCIA_VENDOR_LUCENT,
    209 	  PCMCIA_PRODUCT_LUCENT_WAVELAN_IEEE,
    210 	  PCMCIA_CIS_NTT_ME_WLAN,
    211 	  PCMCIA_STR_NTT_ME_WLAN },
    212 
    213 	{ PCMCIA_VENDOR_IODATA2,
    214 	  PCMCIA_PRODUCT_IODATA2_WNB11PCM,
    215 	  PCMCIA_CIS_IODATA2_WNB11PCM,
    216 	  PCMCIA_STR_IODATA2_WNB11PCM },
    217 
    218 	{ PCMCIA_VENDOR_BUFFALO,
    219 	  PCMCIA_PRODUCT_BUFFALO_WLI_PCM_S11,
    220 	  PCMCIA_CIS_BUFFALO_WLI_PCM_S11,
    221 	  PCMCIA_STR_BUFFALO_WLI_PCM_S11 },
    222 
    223 	{ PCMCIA_VENDOR_BUFFALO,
    224 	  PCMCIA_PRODUCT_BUFFALO_WLI_CF_S11G,
    225 	  PCMCIA_CIS_BUFFALO_WLI_CF_S11G,
    226 	  PCMCIA_STR_BUFFALO_WLI_CF_S11G },
    227 
    228 	{ PCMCIA_VENDOR_EMTAC,
    229 	  PCMCIA_PRODUCT_EMTAC_WLAN,
    230 	  PCMCIA_CIS_EMTAC_WLAN,
    231 	  PCMCIA_STR_EMTAC_WLAN },
    232 
    233 	{ PCMCIA_VENDOR_NETGEAR_2,
    234 	  PCMCIA_PRODUCT_NETGEAR_2_MA401,
    235 	  PCMCIA_CIS_NETGEAR_2_MA401,
    236 	  PCMCIA_STR_NETGEAR_2_MA401 },
    237 
    238 	{ PCMCIA_VENDOR_INTERSIL,
    239 	  PCMCIA_PRODUCT_GEMTEK_WLAN,
    240 	  PCMCIA_CIS_GEMTEK_WLAN,
    241 	  PCMCIA_STR_GEMTEK_WLAN },
    242 
    243 	{ PCMCIA_VENDOR_SIMPLETECH,
    244 	  PCMCIA_PRODUCT_SIMPLETECH_SPECTRUM24_ALT,
    245 	  PCMCIA_CIS_SIMPLETECH_SPECTRUM24_ALT,
    246 	  PCMCIA_STR_SIMPLETECH_SPECTRUM24_ALT },
    247 
    248 	{ PCMCIA_VENDOR_ERICSSON,
    249 	  PCMCIA_PRODUCT_ERICSSON_WIRELESSLAN,
    250 	  PCMCIA_CIS_ERICSSON_WIRELESSLAN,
    251 	  PCMCIA_STR_ERICSSON_WIRELESSLAN },
    252 
    253 	{ PCMCIA_VENDOR_SYMBOL,
    254 	  PCMCIA_PRODUCT_SYMBOL_LA4100,
    255 	  PCMCIA_CIS_SYMBOL_LA4100,
    256 	  PCMCIA_STR_SYMBOL_LA4100 },
    257 
    258 	{ PCMCIA_VENDOR_LINKSYS2,
    259 	  PCMCIA_PRODUCT_LINKSYS2_IWN3,
    260 	  PCMCIA_CIS_LINKSYS2_IWN3,
    261 	  PCMCIA_STR_LINKSYS2_IWN3 },
    262 
    263 	{ PCMCIA_VENDOR_LINKSYS2,
    264 	  PCMCIA_PRODUCT_LINKSYS2_WCF11,
    265 	  PCMCIA_CIS_LINKSYS2_WCF11,
    266 	  PCMCIA_STR_LINKSYS2_WCF11 },
    267 
    268 	{ PCMCIA_VENDOR_PLANEX,
    269 	  PCMCIA_PRODUCT_PLANEX_GWNS11H,
    270 	  PCMCIA_CIS_PLANEX_GWNS11H,
    271 	  PCMCIA_STR_PLANEX_GWNS11H },
    272 
    273 	{ PCMCIA_VENDOR_BAY,
    274 	  PCMCIA_PRODUCT_BAY_EMOBILITY_11B,
    275 	  PCMCIA_CIS_BAY_EMOBILITY_11B,
    276 	  PCMCIA_STR_BAY_EMOBILITY_11B },
    277 
    278 	{ PCMCIA_VENDOR_ACTIONTEC,
    279 	  PCMCIA_PRODUCT_ACTIONTEC_PRISM,
    280 	  PCMCIA_CIS_ACTIONTEC_PRISM,
    281 	  PCMCIA_STR_ACTIONTEC_PRISM },
    282 
    283 	{ PCMCIA_VENDOR_DLINK_2,
    284 	  PCMCIA_PRODUCT_DLINK_DWL650H,
    285 	  PCMCIA_CIS_DLINK_DWL650H,
    286 	  PCMCIA_STR_DLINK_DWL650H },
    287 
    288 	{ 0,
    289 	  0,
    290 	  { NULL, NULL, NULL, NULL },
    291 	  NULL }
    292 };
    293 
    294 static const struct wi_pcmcia_product *
    295 wi_pcmcia_lookup(pa)
    296 	struct pcmcia_attach_args *pa;
    297 {
    298 	const struct wi_pcmcia_product *pp;
    299 
    300 	/* match by CIS information */
    301 	for (pp = wi_pcmcia_products; pp->pp_name != NULL; pp++) {
    302 		if (pa->card->cis1_info[0] != NULL &&
    303 		    pp->pp_cisinfo[0] != NULL &&
    304 		    strcmp(pa->card->cis1_info[0], pp->pp_cisinfo[0]) == 0 &&
    305 		    pa->card->cis1_info[1] != NULL &&
    306 		    pp->pp_cisinfo[1] != NULL &&
    307 		    strcmp(pa->card->cis1_info[1], pp->pp_cisinfo[1]) == 0)
    308 			return pp;
    309 	}
    310 
    311 	/* match by vendor/product id */
    312 	for (pp = wi_pcmcia_products; pp->pp_name != NULL; pp++) {
    313 		if (pa->manufacturer != PCMCIA_VENDOR_INVALID &&
    314 		    pa->manufacturer == pp->pp_vendor &&
    315 		    pa->product != PCMCIA_PRODUCT_INVALID &&
    316 		    pa->product == pp->pp_product)
    317 			return pp;
    318 	}
    319 
    320 	return (NULL);
    321 }
    322 
    323 static int
    324 wi_pcmcia_match(parent, match, aux)
    325 	struct device *parent;
    326 	struct cfdata *match;
    327 	void *aux;
    328 {
    329 	struct pcmcia_attach_args *pa = aux;
    330 
    331 	if (wi_pcmcia_lookup(pa) != NULL)
    332 		return (1);
    333 	return (0);
    334 }
    335 
    336 static int
    337 wi_pcmcia_enable(sc)
    338 	struct wi_softc *sc;
    339 {
    340 	struct wi_pcmcia_softc *psc = (struct wi_pcmcia_softc *)sc;
    341 	struct pcmcia_function *pf = psc->sc_pf;
    342 
    343 	/* establish the interrupt. */
    344 	sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, wi_intr, sc);
    345 	if (sc->sc_ih == NULL) {
    346 		printf("%s: couldn't establish interrupt\n",
    347 		    sc->sc_dev.dv_xname);
    348 		return (EIO);
    349 	}
    350 	if (pcmcia_function_enable(pf) != 0) {
    351 		printf("%s: couldn't enable card\n", sc->sc_dev.dv_xname);
    352 		pcmcia_intr_disestablish(pf, sc->sc_ih);
    353 		return (EIO);
    354 	}
    355 	DELAY(1000);
    356 	if (psc->sc_symbol_cf) {
    357 		if (wi_pcmcia_load_firm(sc,
    358 		    spectrum24t_primsym, sizeof(spectrum24t_primsym),
    359 		    spectrum24t_secsym, sizeof(spectrum24t_secsym))) {
    360 			printf("%s: couldn't load firmware\n",
    361 			    sc->sc_dev.dv_xname);
    362 			wi_pcmcia_disable(sc);
    363 			return (EIO);
    364 		}
    365 	}
    366 	return (0);
    367 }
    368 
    369 static void
    370 wi_pcmcia_disable(sc)
    371 	struct wi_softc *sc;
    372 {
    373 	struct wi_pcmcia_softc *psc = (struct wi_pcmcia_softc *)sc;
    374 
    375 	pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    376 	pcmcia_function_disable(psc->sc_pf);
    377 }
    378 
    379 static int
    380 wi_pcmcia_find(psc, pa, cfe)
    381 	struct wi_pcmcia_softc *psc;
    382 	struct pcmcia_attach_args *pa;
    383 	struct pcmcia_config_entry *cfe;
    384 {
    385 	struct wi_softc *sc = &psc->sc_wi;
    386 
    387 	/* Allocate/map I/O space. */
    388 	if (pcmcia_io_alloc(psc->sc_pf, cfe->iospace[0].start,
    389 	    cfe->iospace[0].length, WI_IOSIZE,
    390 	    &psc->sc_pcioh) != 0) {
    391 		printf("%s: can't allocate i/o space\n",
    392 			sc->sc_dev.dv_xname);
    393 		goto fail1;
    394 	}
    395 	printf("%s:", sc->sc_dev.dv_xname);
    396 	if (pcmcia_io_map(psc->sc_pf, PCMCIA_WIDTH_AUTO, 0,
    397 	    psc->sc_pcioh.size, &psc->sc_pcioh,
    398 	    &psc->sc_io_window) != 0) {
    399 		printf(" can't map i/o space\n");
    400 		goto fail2;
    401 	}
    402 	/* Enable the card */
    403 	pcmcia_function_init(psc->sc_pf, cfe);
    404 	if (pcmcia_function_enable(psc->sc_pf)) {
    405 		printf("%s: function enable failed\n", sc->sc_dev.dv_xname);
    406 		goto fail3;
    407 	}
    408 
    409 	sc->sc_iot = psc->sc_pcioh.iot;
    410 	sc->sc_ioh = psc->sc_pcioh.ioh;
    411 
    412 	DELAY(1000);
    413 	return(0);
    414 
    415 fail3:
    416 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    417 fail2:
    418 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    419 fail1:
    420 	psc->sc_io_window = -1;
    421 	return(1);
    422 }
    423 
    424 static void
    425 wi_pcmcia_attach(parent, self, aux)
    426 	struct device  *parent, *self;
    427 	void           *aux;
    428 {
    429 	struct wi_pcmcia_softc *psc = (void *)self;
    430 	struct wi_softc *sc = &psc->sc_wi;
    431 	const struct wi_pcmcia_product *pp;
    432 	struct pcmcia_attach_args *pa = aux;
    433 	struct pcmcia_config_entry *cfe;
    434 	char devinfo[256];
    435 
    436 	/* Print out what we are. */
    437 	pcmcia_devinfo(&pa->pf->sc->card, 0, devinfo, sizeof(devinfo));
    438 	printf(": %s\n", devinfo);
    439 
    440 	psc->sc_pf = pa->pf;
    441 
    442 	SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
    443 		if (cfe->iftype != PCMCIA_IFTYPE_IO)
    444 			continue;
    445 		if (cfe->iospace[0].length < WI_IOSIZE)
    446 			continue;
    447 		if (wi_pcmcia_find(psc, pa, cfe) == 0)
    448 			break;
    449 	}
    450 	if (cfe == NULL) {
    451 		printf(": no suitable CIS info found\n");
    452 		goto no_config_entry;
    453 	}
    454 
    455 	pp = wi_pcmcia_lookup(pa);
    456 	if (pp == NULL)
    457 		panic("wi_pcmcia_attach: impossible");
    458 
    459 	sc->sc_pci = 0;
    460 	sc->sc_enabled = 1;
    461 	sc->sc_enable = wi_pcmcia_enable;
    462 	sc->sc_disable = wi_pcmcia_disable;
    463 	if (pp->pp_vendor == PCMCIA_VENDOR_SYMBOL &&
    464 	    pp->pp_product == PCMCIA_PRODUCT_SYMBOL_LA4100)
    465 		psc->sc_symbol_cf = 1;
    466 	/*
    467 	 * XXX: Sony PEGA-WL100 CF card has a same vendor/product id as
    468 	 *	Intel PCMCIA card.  It may be incorrect to detect by the
    469 	 *	initial value of COR register.
    470 	 */
    471 	if (pp->pp_vendor == PCMCIA_VENDOR_INTEL &&
    472 	    pp->pp_product == PCMCIA_PRODUCT_INTEL_PRO_WLAN_2011 &&
    473 	    CSR_READ_2(sc, WI_COR) == WI_COR_IOMODE)
    474 		psc->sc_symbol_cf = 1;
    475 
    476 	if (psc->sc_symbol_cf) {
    477 		if (wi_pcmcia_load_firm(sc,
    478 		    spectrum24t_primsym, sizeof(spectrum24t_primsym),
    479 		    spectrum24t_secsym, sizeof(spectrum24t_secsym))) {
    480 			printf("%s: couldn't load firmware\n",
    481 				sc->sc_dev.dv_xname);
    482 			goto no_interrupt;
    483 		}
    484 	}
    485 
    486 	/* establish the interrupt. */
    487 	sc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, wi_intr, sc);
    488 	if (sc->sc_ih == NULL) {
    489 		printf("%s: couldn't establish interrupt\n",
    490 		        sc->sc_dev.dv_xname);
    491 		goto no_interrupt;
    492 	}
    493 
    494 	if (wi_attach(sc) != 0) {
    495 		printf("%s: failed to attach controller\n",
    496 		    sc->sc_dev.dv_xname);
    497 			goto attach_failed;
    498 	}
    499 
    500 	psc->sc_sdhook    = shutdownhook_establish(wi_pcmcia_shutdown, psc);
    501 	psc->sc_powerhook = powerhook_establish(wi_pcmcia_powerhook, psc);
    502 
    503 	/* disable the card */
    504 	sc->sc_enabled = 0;
    505 	wi_pcmcia_disable(sc);
    506 
    507 	return;
    508 
    509 attach_failed:
    510 	pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    511 no_interrupt:
    512 	pcmcia_function_disable(psc->sc_pf);
    513 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    514 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    515 no_config_entry:
    516 	psc->sc_io_window = -1;
    517 }
    518 
    519 static int
    520 wi_pcmcia_detach(self, flags)
    521 	struct device *self;
    522 	int flags;
    523 {
    524 	struct wi_pcmcia_softc *psc = (struct wi_pcmcia_softc *)self;
    525 	int error;
    526 
    527 	if (psc->sc_io_window == -1)
    528 		/* Nothing to detach. */
    529 		return (0);
    530 
    531 	if (psc->sc_powerhook != NULL)
    532 		powerhook_disestablish(psc->sc_powerhook);
    533 	if (psc->sc_sdhook != NULL)
    534 		shutdownhook_disestablish(psc->sc_sdhook);
    535 
    536 	error = wi_detach(&psc->sc_wi);
    537 	if (error != 0)
    538 		return (error);
    539 
    540 	/* Unmap our i/o window. */
    541 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    542 
    543 	/* Free our i/o space. */
    544 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    545 	return (0);
    546 }
    547 
    548 static void
    549 wi_pcmcia_powerhook(why, arg)
    550 	int why;
    551 	void *arg;
    552 {
    553 	struct wi_pcmcia_softc *psc = arg;
    554 	struct wi_softc *sc = &psc->sc_wi;
    555 
    556 	wi_power(sc, why);
    557 }
    558 
    559 static void
    560 wi_pcmcia_shutdown(arg)
    561 	void *arg;
    562 {
    563 	struct wi_pcmcia_softc *psc = arg;
    564 	struct wi_softc *sc = &psc->sc_wi;
    565 
    566 	wi_shutdown(sc);
    567 }
    568 
    569 /*
    570  * Special routines to download firmware for Symbol CF card.
    571  * XXX: This should be modified generic into any PRISM-2 based card.
    572  */
    573 
    574 #define	WI_SBCF_PDIADDR		0x3100
    575 
    576 /* unaligned load little endian */
    577 #define	GETLE32(p)	((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24))
    578 #define	GETLE16(p)	((p)[0] | ((p)[1]<<8))
    579 
    580 static int
    581 wi_pcmcia_load_firm(sc, primsym, primlen, secsym, seclen)
    582 	struct wi_softc *sc;
    583 	const void *primsym, *secsym;
    584 	int primlen, seclen;
    585 {
    586 	u_int8_t ebuf[256];
    587 	int i;
    588 
    589 	/* load primary code and run it */
    590 	wi_pcmcia_set_hcr(sc, WI_HCR_EEHOLD);
    591 	if (wi_pcmcia_write_firm(sc, primsym, primlen, NULL, 0))
    592 		return EIO;
    593 	wi_pcmcia_set_hcr(sc, WI_HCR_RUN);
    594 	for (i = 0; ; i++) {
    595 		if (i == 10)
    596 			return ETIMEDOUT;
    597 		tsleep(sc, PWAIT, "wiinit", 1);
    598 		if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT)
    599 			break;
    600 		/* write the magic key value to unlock aux port */
    601 		CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0);
    602 		CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1);
    603 		CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2);
    604 		CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL);
    605 	}
    606 
    607 	/* issue read EEPROM command: XXX copied from wi_cmd() */
    608 	CSR_WRITE_2(sc, WI_PARAM0, 0);
    609 	CSR_WRITE_2(sc, WI_PARAM1, 0);
    610 	CSR_WRITE_2(sc, WI_PARAM2, 0);
    611 	CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE);
    612         for (i = 0; i < WI_TIMEOUT; i++) {
    613                 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
    614                         break;
    615                 DELAY(1);
    616         }
    617         CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
    618 
    619 	CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ);
    620 	CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ);
    621 	CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA,
    622 	    (u_int16_t *)ebuf, sizeof(ebuf) / 2);
    623 	if (GETLE16(ebuf) > sizeof(ebuf))
    624 		return EIO;
    625 	if (wi_pcmcia_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf)))
    626 		return EIO;
    627 	return 0;
    628 }
    629 
    630 static int
    631 wi_pcmcia_write_firm(sc, buf, buflen, ebuf, ebuflen)
    632 	struct wi_softc *sc;
    633 	const void *buf, *ebuf;
    634 	int buflen, ebuflen;
    635 {
    636 	const u_int8_t *p, *ep, *q, *eq;
    637 	u_int32_t addr, id, eid;
    638 	int i, len, elen, nblk, pdrlen;
    639 
    640 	/*
    641 	 * Parse the header of the firmware image.
    642 	 */
    643 	p = buf;
    644 	ep = p + buflen;
    645 	while (p < ep && *p++ != ' ');	/* FILE: */
    646 	while (p < ep && *p++ != ' ');	/* filename */
    647 	while (p < ep && *p++ != ' ');	/* type of the firmware */
    648 	nblk = strtoul(p, (void *)&p, 10);
    649 	pdrlen = strtoul(p + 1, (void *)&p, 10);
    650 	while (p < ep && *p++ != 0x1a);	/* skip rest of header */
    651 
    652 	/*
    653 	 * Block records: address[4], length[2], data[length];
    654 	 */
    655 	for (i = 0; i < nblk; i++) {
    656 		addr = GETLE32(p);	p += 4;
    657 		len  = GETLE16(p);	p += 2;
    658 		CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
    659 		CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
    660 		CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
    661 		    (const u_int16_t *)p, len / 2);
    662 		p += len;
    663 	}
    664 
    665 	/*
    666 	 * PDR: id[4], address[4], length[4];
    667 	 */
    668 	for (i = 0; i < pdrlen; ) {
    669 		id   = GETLE32(p);	p += 4; i += 4;
    670 		addr = GETLE32(p);	p += 4; i += 4;
    671 		len  = GETLE32(p);	p += 4; i += 4;
    672 		/* replace PDR entry with the values from EEPROM, if any */
    673 		for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) {
    674 			elen = GETLE16(q);	q += 2;
    675 			eid  = GETLE16(q);	q += 2;
    676 			elen--;		/* elen includes eid */
    677 			if (eid == 0)
    678 				break;
    679 			if (eid != id)
    680 				continue;
    681 			CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
    682 			CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
    683 			CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
    684 			    (const u_int16_t *)q, len / 2);
    685 			break;
    686 		}
    687 	}
    688 	return 0;
    689 }
    690 
    691 static int
    692 wi_pcmcia_set_hcr(sc, mode)
    693 	struct wi_softc *sc;
    694 	int mode;
    695 {
    696 	u_int16_t hcr;
    697 
    698 	CSR_WRITE_2(sc, WI_COR, WI_COR_RESET);
    699 	tsleep(sc, PWAIT, "wiinit", 1);
    700 	hcr = CSR_READ_2(sc, WI_HCR);
    701 	hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE);
    702 	CSR_WRITE_2(sc, WI_HCR, hcr);
    703 	tsleep(sc, PWAIT, "wiinit", 1);
    704 	CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE);
    705 	tsleep(sc, PWAIT, "wiinit", 1);
    706 	return 0;
    707 }
    708