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