Home | History | Annotate | Line # | Download | only in pcmcia
if_wi_pcmcia.c revision 1.58
      1 /* $NetBSD: if_wi_pcmcia.c,v 1.58 2004/08/10 22:49:12 mycroft Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2001, 2004 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), and by Charles M. Hannum.
      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.58 2004/08/10 22:49:12 mycroft 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 int	wi_pcmcia_validate_config __P((struct pcmcia_config_entry *));
     78 static void	wi_pcmcia_attach __P((struct device *, struct device *, void *));
     79 static int	wi_pcmcia_detach __P((struct device *, int));
     80 static int	wi_pcmcia_enable __P((struct wi_softc *));
     81 static void	wi_pcmcia_disable __P((struct wi_softc *));
     82 static void	wi_pcmcia_powerhook __P((int, void *));
     83 static void	wi_pcmcia_shutdown __P((void *));
     84 
     85 /* support to download firmware for symbol CF card */
     86 static int	wi_pcmcia_load_firm __P((struct wi_softc *, const void *, int, const void *, int));
     87 static int	wi_pcmcia_write_firm __P((struct wi_softc *, const void *, int, const void *, int));
     88 static int	wi_pcmcia_set_hcr __P((struct wi_softc *, int));
     89 
     90 struct wi_pcmcia_softc {
     91 	struct wi_softc sc_wi;
     92 
     93 	void *sc_powerhook;			/* power hook descriptor */
     94 	void *sc_sdhook;			/* shutdown hook */
     95 	int sc_symbol_cf;			/* Spectrum24t CF card */
     96 
     97 	struct pcmcia_function *sc_pf;		/* PCMCIA function */
     98 	int sc_state;
     99 #define	WI_PCMCIA_ATTACHED	3
    100 };
    101 
    102 CFATTACH_DECL(wi_pcmcia, sizeof(struct wi_pcmcia_softc),
    103     wi_pcmcia_match, wi_pcmcia_attach, wi_pcmcia_detach, wi_activate);
    104 
    105 static const struct pcmcia_product wi_pcmcia_products[] = {
    106 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    107 	  PCMCIA_CIS_SMC_2632W },
    108 
    109 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    110 	  PCMCIA_CIS_NANOSPEED_PRISM2 },
    111 
    112 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    113 	  PCMCIA_CIS_NEC_CMZ_RT_WP },
    114 
    115 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    116 	  PCMCIA_CIS_NTT_ME_WLAN },
    117 
    118 	{ PCMCIA_VENDOR_LUCENT, PCMCIA_PRODUCT_LUCENT_HERMES,
    119 	  PCMCIA_CIS_INVALID },
    120 
    121 	{ PCMCIA_VENDOR_LUCENT, PCMCIA_PRODUCT_LUCENT_HERMES2,
    122 	  PCMCIA_CIS_INVALID },
    123 
    124 	{ PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3CRWE737A,
    125 	  PCMCIA_CIS_3COM_3CRWE737A },
    126 
    127 	{ PCMCIA_VENDOR_COREGA, PCMCIA_PRODUCT_COREGA_WIRELESS_LAN_PCC_11,
    128 	  PCMCIA_CIS_COREGA_WIRELESS_LAN_PCC_11 },
    129 
    130 	{ PCMCIA_VENDOR_COREGA, PCMCIA_PRODUCT_COREGA_WIRELESS_LAN_PCCA_11,
    131 	  PCMCIA_CIS_COREGA_WIRELESS_LAN_PCCA_11 },
    132 
    133 	{ PCMCIA_VENDOR_COREGA, PCMCIA_PRODUCT_COREGA_WIRELESS_LAN_PCCB_11,
    134 	  PCMCIA_CIS_COREGA_WIRELESS_LAN_PCCB_11 },
    135 
    136 	{ PCMCIA_VENDOR_COREGA, PCMCIA_PRODUCT_COREGA_WIRELESS_LAN_PCCL_11,
    137 	  PCMCIA_CIS_COREGA_WIRELESS_LAN_PCCL_11 },
    138 
    139 	{ PCMCIA_VENDOR_COREGA, PCMCIA_PRODUCT_COREGA_WIRELESS_LAN_WLCFL_11,
    140 	  PCMCIA_CIS_COREGA_WIRELESS_LAN_WLCFL_11 },
    141 
    142 	{ PCMCIA_VENDOR_INTEL, PCMCIA_PRODUCT_INTEL_PRO_WLAN_2011,
    143 	  PCMCIA_CIS_INTEL_PRO_WLAN_2011 },
    144 
    145 	{ PCMCIA_VENDOR_INTERSIL, PCMCIA_PRODUCT_INTERSIL_PRISM2,
    146 	  PCMCIA_CIS_INTERSIL_PRISM2 },
    147 
    148 	{ PCMCIA_VENDOR_INTERSIL, PCMCIA_PRODUCT_GEMTEK_WLAN,
    149 	  PCMCIA_CIS_GEMTEK_WLAN },
    150 
    151 	{ PCMCIA_VENDOR_SAMSUNG, PCMCIA_PRODUCT_SAMSUNG_SWL_2000N,
    152 	  PCMCIA_CIS_SAMSUNG_SWL_2000N },
    153 
    154 	{ PCMCIA_VENDOR_ELSA, PCMCIA_PRODUCT_ELSA_XI300_IEEE,
    155 	  PCMCIA_CIS_ELSA_XI300_IEEE },
    156 
    157 	{ PCMCIA_VENDOR_ELSA, PCMCIA_PRODUCT_ELSA_XI325_IEEE,
    158 	  PCMCIA_CIS_ELSA_XI325_IEEE },
    159 
    160 	{ PCMCIA_VENDOR_ELSA, PCMCIA_PRODUCT_ELSA_XI800_IEEE,
    161 	  PCMCIA_CIS_ELSA_XI800_IEEE },
    162 
    163 	{ PCMCIA_VENDOR_COMPAQ, PCMCIA_PRODUCT_COMPAQ_NC5004,
    164 	  PCMCIA_CIS_COMPAQ_NC5004 },
    165 
    166 	{ PCMCIA_VENDOR_CONTEC, PCMCIA_PRODUCT_CONTEC_FX_DS110_PCC,
    167 	  PCMCIA_CIS_CONTEC_FX_DS110_PCC },
    168 
    169 	{ PCMCIA_VENDOR_TDK, PCMCIA_PRODUCT_TDK_LAK_CD011WL,
    170 	  PCMCIA_CIS_TDK_LAK_CD011WL },
    171 
    172 	{ PCMCIA_VENDOR_IODATA2, PCMCIA_PRODUCT_IODATA2_WNB11PCM,
    173 	  PCMCIA_CIS_IODATA2_WNB11PCM },
    174 
    175 	{ PCMCIA_VENDOR_IODATA2, PCMCIA_PRODUCT_IODATA2_WCF12,
    176 	  PCMCIA_CIS_IODATA2_WCF12 },
    177 
    178 	{ PCMCIA_VENDOR_BUFFALO, PCMCIA_PRODUCT_BUFFALO_WLI_PCM_S11,
    179 	  PCMCIA_CIS_BUFFALO_WLI_PCM_S11 },
    180 
    181 	{ PCMCIA_VENDOR_BUFFALO, PCMCIA_PRODUCT_BUFFALO_WLI_CF_S11G,
    182 	  PCMCIA_CIS_BUFFALO_WLI_CF_S11G },
    183 
    184 	{ PCMCIA_VENDOR_EMTAC, PCMCIA_PRODUCT_EMTAC_WLAN,
    185 	  PCMCIA_CIS_EMTAC_WLAN },
    186 
    187 	{ PCMCIA_VENDOR_NETGEAR_2, PCMCIA_PRODUCT_NETGEAR_2_MA401,
    188 	  PCMCIA_CIS_NETGEAR_2_MA401 },
    189 
    190 	{ PCMCIA_VENDOR_SIMPLETECH, PCMCIA_PRODUCT_SIMPLETECH_SPECTRUM24,
    191 	  PCMCIA_CIS_SIMPLETECH_SPECTRUM24 },
    192 
    193 	{ PCMCIA_VENDOR_ERICSSON, PCMCIA_PRODUCT_ERICSSON_WIRELESSLAN,
    194 	  PCMCIA_CIS_ERICSSON_WIRELESSLAN },
    195 
    196 	{ PCMCIA_VENDOR_SYMBOL, PCMCIA_PRODUCT_SYMBOL_LA4100,
    197 	  PCMCIA_CIS_SYMBOL_LA4100 },
    198 
    199 	{ PCMCIA_VENDOR_LINKSYS2, PCMCIA_PRODUCT_LINKSYS2_IWN,
    200 	  PCMCIA_CIS_NANOSPEED_PRISM2 },
    201 
    202 	{ PCMCIA_VENDOR_LINKSYS2, PCMCIA_PRODUCT_LINKSYS2_IWN3,
    203 	  PCMCIA_CIS_LINKSYS2_IWN3 },
    204 
    205 	{ PCMCIA_VENDOR_LINKSYS2, PCMCIA_PRODUCT_LINKSYS2_WCF11,
    206 	  PCMCIA_CIS_LINKSYS2_WCF11 },
    207 
    208 	{ PCMCIA_VENDOR_PLANEX, PCMCIA_PRODUCT_PLANEX_GWNS11H,
    209 	  PCMCIA_CIS_PLANEX_GWNS11H },
    210 
    211 	{ PCMCIA_VENDOR_BAY, PCMCIA_PRODUCT_BAY_EMOBILITY_11B,
    212 	  PCMCIA_CIS_BAY_EMOBILITY_11B },
    213 
    214 	{ PCMCIA_VENDOR_ACTIONTEC, PCMCIA_PRODUCT_ACTIONTEC_PRISM,
    215 	  PCMCIA_CIS_ACTIONTEC_PRISM },
    216 
    217 	{ PCMCIA_VENDOR_DLINK_2, PCMCIA_PRODUCT_DLINK_DWL650H,
    218 	  PCMCIA_CIS_DLINK_DWL650H },
    219 
    220 	{ PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_WL110,
    221 	  PCMCIA_CIS_FUJITSU_WL110 },
    222 
    223 	{ PCMCIA_VENDOR_ARTEM, PCMCIA_PRODUCT_ARTEM_ONAIR,
    224 	  PCMCIA_CIS_ARTEM_ONAIR },
    225 
    226 	{ PCMCIA_VENDOR_ASUSTEK, PCMCIA_PRODUCT_ASUSTEK_WL_100,
    227 	  PCMCIA_CIS_ASUSTEK_WL_100 },
    228 };
    229 static const size_t wi_pcmcia_nproducts =
    230     sizeof(wi_pcmcia_products) / sizeof(wi_pcmcia_products[0]);
    231 
    232 static int
    233 wi_pcmcia_match(parent, match, aux)
    234 	struct device *parent;
    235 	struct cfdata *match;
    236 	void *aux;
    237 {
    238 	struct pcmcia_attach_args *pa = aux;
    239 
    240 	if (pcmcia_product_lookup(pa, wi_pcmcia_products, wi_pcmcia_nproducts,
    241 	    sizeof(wi_pcmcia_products[0]), NULL))
    242 		return (1);
    243 	return (0);
    244 }
    245 
    246 static int
    247 wi_pcmcia_enable(sc)
    248 	struct wi_softc *sc;
    249 {
    250 	struct wi_pcmcia_softc *psc = (struct wi_pcmcia_softc *)sc;
    251 	struct pcmcia_function *pf = psc->sc_pf;
    252 	int error;
    253 
    254 	/* establish the interrupt. */
    255 	sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, wi_intr, sc);
    256 	if (!sc->sc_ih)
    257 		return (EIO);
    258 
    259 	error = pcmcia_function_enable(pf);
    260 	if (error) {
    261 		pcmcia_intr_disestablish(pf, sc->sc_ih);
    262 		sc->sc_ih = 0;
    263 		return (EIO);
    264 	}
    265 
    266 	if (psc->sc_symbol_cf) {
    267 		if (wi_pcmcia_load_firm(sc,
    268 		    spectrum24t_primsym, sizeof(spectrum24t_primsym),
    269 		    spectrum24t_secsym, sizeof(spectrum24t_secsym))) {
    270 			printf("%s: couldn't load firmware\n",
    271 			    sc->sc_dev.dv_xname);
    272 			wi_pcmcia_disable(sc);
    273 			return (EIO);
    274 		}
    275 	}
    276 	DELAY(1000);
    277 
    278 	return (0);
    279 }
    280 
    281 static void
    282 wi_pcmcia_disable(sc)
    283 	struct wi_softc *sc;
    284 {
    285 	struct wi_pcmcia_softc *psc = (struct wi_pcmcia_softc *)sc;
    286 
    287 	pcmcia_function_disable(psc->sc_pf);
    288 	pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    289 	sc->sc_ih = 0;
    290 }
    291 
    292 static int
    293 wi_pcmcia_validate_config(cfe)
    294 	struct pcmcia_config_entry *cfe;
    295 {
    296 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
    297 	    cfe->num_iospace != 1 ||
    298 	    cfe->iospace[0].length < WI_IOSIZE)
    299 		return (EINVAL);
    300 	cfe->num_memspace = 0;
    301 	return (0);
    302 }
    303 
    304 static void
    305 wi_pcmcia_attach(parent, self, aux)
    306 	struct device  *parent, *self;
    307 	void           *aux;
    308 {
    309 	struct wi_pcmcia_softc *psc = (void *)self;
    310 	struct wi_softc *sc = &psc->sc_wi;
    311 	struct pcmcia_attach_args *pa = aux;
    312 	struct pcmcia_config_entry *cfe;
    313 	int haveaddr;
    314 	int error;
    315 
    316 	psc->sc_pf = pa->pf;
    317 
    318 	error = pcmcia_function_configure(pa->pf, wi_pcmcia_validate_config);
    319 	if (error) {
    320 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
    321 		    error);
    322 		return;
    323 	}
    324 
    325 	cfe = pa->pf->cfe;
    326 	sc->sc_iot = cfe->iospace[0].handle.iot;
    327 	sc->sc_ioh = cfe->iospace[0].handle.ioh;
    328 
    329 	if (pa->manufacturer == PCMCIA_VENDOR_SYMBOL &&
    330 	    pa->product == PCMCIA_PRODUCT_SYMBOL_LA4100)
    331 		psc->sc_symbol_cf = 1;
    332 	/*
    333 	 * XXX: Sony PEGA-WL100 CF card has a same vendor/product id as
    334 	 *	Intel PCMCIA card.  It may be incorrect to detect by the
    335 	 *	initial value of COR register.
    336 	 */
    337 	if (pa->manufacturer == PCMCIA_VENDOR_INTEL &&
    338 	    pa->product == PCMCIA_PRODUCT_INTEL_PRO_WLAN_2011 &&
    339 	    CSR_READ_2(sc, WI_COR) == WI_COR_IOMODE)
    340 		psc->sc_symbol_cf = 1;
    341 
    342 	error = wi_pcmcia_enable(sc);
    343 	if (error)
    344 		goto fail;
    345 
    346 	sc->sc_pci = 0;
    347 	sc->sc_enable = wi_pcmcia_enable;
    348 	sc->sc_disable = wi_pcmcia_disable;
    349 
    350 	printf("%s:", self->dv_xname);
    351 
    352 	haveaddr = pa->pf->pf_funce_lan_nidlen == IEEE80211_ADDR_LEN;
    353 	if (wi_attach(sc, haveaddr ? pa->pf->pf_funce_lan_nid : 0) != 0) {
    354 		aprint_error("%s: failed to attach controller\n", self->dv_xname);
    355 		goto fail2;
    356 	}
    357 
    358 	psc->sc_sdhook    = shutdownhook_establish(wi_pcmcia_shutdown, psc);
    359 	psc->sc_powerhook = powerhook_establish(wi_pcmcia_powerhook, psc);
    360 
    361 	wi_pcmcia_disable(sc);
    362 	psc->sc_state = WI_PCMCIA_ATTACHED;
    363 	return;
    364 
    365 fail2:
    366 	wi_pcmcia_disable(sc);
    367 fail:
    368 	pcmcia_function_unconfigure(pa->pf);
    369 }
    370 
    371 static int
    372 wi_pcmcia_detach(self, flags)
    373 	struct device *self;
    374 	int flags;
    375 {
    376 	struct wi_pcmcia_softc *psc = (struct wi_pcmcia_softc *)self;
    377 	int error;
    378 
    379 	if (psc->sc_state != WI_PCMCIA_ATTACHED)
    380 		return (0);
    381 
    382 	if (psc->sc_powerhook)
    383 		powerhook_disestablish(psc->sc_powerhook);
    384 	if (psc->sc_sdhook)
    385 		shutdownhook_disestablish(psc->sc_sdhook);
    386 
    387 	error = wi_detach(&psc->sc_wi);
    388 	if (error != 0)
    389 		return (error);
    390 
    391 	pcmcia_function_unconfigure(psc->sc_pf);
    392 
    393 	return (0);
    394 }
    395 
    396 static void
    397 wi_pcmcia_powerhook(why, arg)
    398 	int why;
    399 	void *arg;
    400 {
    401 	struct wi_pcmcia_softc *psc = arg;
    402 	struct wi_softc *sc = &psc->sc_wi;
    403 
    404 	wi_power(sc, why);
    405 }
    406 
    407 static void
    408 wi_pcmcia_shutdown(arg)
    409 	void *arg;
    410 {
    411 	struct wi_pcmcia_softc *psc = arg;
    412 	struct wi_softc *sc = &psc->sc_wi;
    413 
    414 	wi_shutdown(sc);
    415 }
    416 
    417 /*
    418  * Special routines to download firmware for Symbol CF card.
    419  * XXX: This should be modified generic into any PRISM-2 based card.
    420  */
    421 
    422 #define	WI_SBCF_PDIADDR		0x3100
    423 
    424 /* unaligned load little endian */
    425 #define	GETLE32(p)	((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24))
    426 #define	GETLE16(p)	((p)[0] | ((p)[1]<<8))
    427 
    428 static int
    429 wi_pcmcia_load_firm(sc, primsym, primlen, secsym, seclen)
    430 	struct wi_softc *sc;
    431 	const void *primsym, *secsym;
    432 	int primlen, seclen;
    433 {
    434 	u_int8_t ebuf[256];
    435 	int i;
    436 
    437 	/* load primary code and run it */
    438 	wi_pcmcia_set_hcr(sc, WI_HCR_EEHOLD);
    439 	if (wi_pcmcia_write_firm(sc, primsym, primlen, NULL, 0))
    440 		return EIO;
    441 	wi_pcmcia_set_hcr(sc, WI_HCR_RUN);
    442 	for (i = 0; ; i++) {
    443 		if (i == 10)
    444 			return ETIMEDOUT;
    445 		tsleep(sc, PWAIT, "wiinit", 1);
    446 		if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT)
    447 			break;
    448 		/* write the magic key value to unlock aux port */
    449 		CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0);
    450 		CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1);
    451 		CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2);
    452 		CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL);
    453 	}
    454 
    455 	/* issue read EEPROM command: XXX copied from wi_cmd() */
    456 	CSR_WRITE_2(sc, WI_PARAM0, 0);
    457 	CSR_WRITE_2(sc, WI_PARAM1, 0);
    458 	CSR_WRITE_2(sc, WI_PARAM2, 0);
    459 	CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE);
    460         for (i = 0; i < WI_TIMEOUT; i++) {
    461                 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
    462                         break;
    463                 DELAY(1);
    464         }
    465         CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
    466 
    467 	CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ);
    468 	CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ);
    469 	CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA,
    470 	    (u_int16_t *)ebuf, sizeof(ebuf) / 2);
    471 	if (GETLE16(ebuf) > sizeof(ebuf))
    472 		return EIO;
    473 	if (wi_pcmcia_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf)))
    474 		return EIO;
    475 	return 0;
    476 }
    477 
    478 static int
    479 wi_pcmcia_write_firm(sc, buf, buflen, ebuf, ebuflen)
    480 	struct wi_softc *sc;
    481 	const void *buf, *ebuf;
    482 	int buflen, ebuflen;
    483 {
    484 	const u_int8_t *p, *ep, *q, *eq;
    485 	u_int32_t addr, id, eid;
    486 	int i, len, elen, nblk, pdrlen;
    487 
    488 	/*
    489 	 * Parse the header of the firmware image.
    490 	 */
    491 	p = buf;
    492 	ep = p + buflen;
    493 	while (p < ep && *p++ != ' ');	/* FILE: */
    494 	while (p < ep && *p++ != ' ');	/* filename */
    495 	while (p < ep && *p++ != ' ');	/* type of the firmware */
    496 	nblk = strtoul(p, (void *)&p, 10);
    497 	pdrlen = strtoul(p + 1, (void *)&p, 10);
    498 	while (p < ep && *p++ != 0x1a);	/* skip rest of header */
    499 
    500 	/*
    501 	 * Block records: address[4], length[2], data[length];
    502 	 */
    503 	for (i = 0; i < nblk; i++) {
    504 		addr = GETLE32(p);	p += 4;
    505 		len  = GETLE16(p);	p += 2;
    506 		CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
    507 		CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
    508 		CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
    509 		    (const u_int16_t *)p, len / 2);
    510 		p += len;
    511 	}
    512 
    513 	/*
    514 	 * PDR: id[4], address[4], length[4];
    515 	 */
    516 	for (i = 0; i < pdrlen; ) {
    517 		id   = GETLE32(p);	p += 4; i += 4;
    518 		addr = GETLE32(p);	p += 4; i += 4;
    519 		len  = GETLE32(p);	p += 4; i += 4;
    520 		/* replace PDR entry with the values from EEPROM, if any */
    521 		for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) {
    522 			elen = GETLE16(q);	q += 2;
    523 			eid  = GETLE16(q);	q += 2;
    524 			elen--;		/* elen includes eid */
    525 			if (eid == 0)
    526 				break;
    527 			if (eid != id)
    528 				continue;
    529 			CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
    530 			CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
    531 			CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
    532 			    (const u_int16_t *)q, len / 2);
    533 			break;
    534 		}
    535 	}
    536 	return 0;
    537 }
    538 
    539 static int
    540 wi_pcmcia_set_hcr(sc, mode)
    541 	struct wi_softc *sc;
    542 	int mode;
    543 {
    544 	u_int16_t hcr;
    545 
    546 	CSR_WRITE_2(sc, WI_COR, WI_COR_RESET);
    547 	tsleep(sc, PWAIT, "wiinit", 1);
    548 	hcr = CSR_READ_2(sc, WI_HCR);
    549 	hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE);
    550 	CSR_WRITE_2(sc, WI_HCR, hcr);
    551 	tsleep(sc, PWAIT, "wiinit", 1);
    552 	CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE);
    553 	tsleep(sc, PWAIT, "wiinit", 1);
    554 	return 0;
    555 }
    556