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