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