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