xref: /netbsd-src/sys/dev/pcmcia/if_wi_pcmcia.c (revision 0c4ddb1599a0bea866fde8522a74cfbd2f68cd1b)
1 /* $NetBSD: if_wi_pcmcia.c,v 1.79 2008/04/28 20:23:56 martin 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@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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * PCMCIA attachment for Lucent & Intersil WaveLAN PCMCIA card
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: if_wi_pcmcia.c,v 1.79 2008/04/28 20:23:56 martin Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/callout.h>
42 #include <sys/device.h>
43 #include <sys/proc.h>
44 #include <sys/socket.h>
45 
46 #include <net/if.h>
47 #include <net/if_ether.h>
48 #include <net/if_media.h>
49 
50 #include <net80211/ieee80211_netbsd.h>
51 #include <net80211/ieee80211_var.h>
52 #include <net80211/ieee80211_radiotap.h>
53 #include <net80211/ieee80211_rssadapt.h>
54 
55 #include <sys/cpu.h>
56 #include <sys/bus.h>
57 #include <sys/intr.h>
58 
59 #include <dev/ic/wi_ieee.h>
60 #include <dev/ic/wireg.h>
61 #include <dev/ic/wivar.h>
62 
63 #include <dev/pcmcia/pcmciareg.h>
64 #include <dev/pcmcia/pcmciavar.h>
65 #include <dev/pcmcia/pcmciadevs.h>
66 
67 #ifdef _LKM
68 #define WI_PCMCIA_SPECTRUM24T_FW 1
69 #else
70 #include <opt_if_wi_pcmcia.h>
71 #endif
72 
73 #if WI_PCMCIA_SPECTRUM24T_FW
74 #include <dev/microcode/wi/spectrum24t_cf.h>
75 #endif
76 
77 static int	wi_pcmcia_match(struct device *, struct cfdata *, void *);
78 static int	wi_pcmcia_validate_config(struct pcmcia_config_entry *);
79 static void	wi_pcmcia_attach(struct device *, struct device *, void *);
80 static int	wi_pcmcia_detach(struct device *, int);
81 static int	wi_pcmcia_enable(struct wi_softc *);
82 static void	wi_pcmcia_disable(struct wi_softc *);
83 
84 #if WI_PCMCIA_SPECTRUM24T_FW
85 /* support to download firmware for symbol CF card */
86 static int	wi_pcmcia_load_firm(struct wi_softc *, const void *, int, const void *, int);
87 static int	wi_pcmcia_write_firm(struct wi_softc *, const void *, int, const void *, int);
88 static int	wi_pcmcia_set_hcr(struct wi_softc *, int);
89 #endif
90 
91 struct wi_pcmcia_softc {
92 	struct wi_softc sc_wi;
93 
94 	int sc_symbol_cf;			/* Spectrum24t CF card */
95 
96 	struct pcmcia_function *sc_pf;		/* PCMCIA function */
97 	int sc_state;
98 #define	WI_PCMCIA_ATTACHED	3
99 };
100 
101 CFATTACH_DECL(wi_pcmcia, sizeof(struct wi_pcmcia_softc),
102     wi_pcmcia_match, wi_pcmcia_attach, wi_pcmcia_detach, wi_activate);
103 
104 static const struct pcmcia_product wi_pcmcia_products[] = {
105 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
106 	  PCMCIA_CIS_SMC_2632W },
107 
108 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
109 	  PCMCIA_CIS_NANOSPEED_PRISM2 },
110 
111 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
112 	  PCMCIA_CIS_NEC_CMZ_RT_WP },
113 
114 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
115 	  PCMCIA_CIS_NTT_ME_WLAN },
116 
117 	{ PCMCIA_VENDOR_LUCENT, PCMCIA_PRODUCT_LUCENT_HERMES,
118 	  PCMCIA_CIS_INVALID },
119 
120 	{ PCMCIA_VENDOR_LUCENT, PCMCIA_PRODUCT_LUCENT_HERMES2,
121 	  PCMCIA_CIS_INVALID },
122 
123 	{ PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3CRWE737A,
124 	  PCMCIA_CIS_3COM_3CRWE737A },
125 
126 	{ PCMCIA_VENDOR_ALVARION,
127 	  PCMCIA_PRODUCT_ALVARION_BREEZENET,
128 	  PCMCIA_CIS_ALVARION_BREEZENET },
129 
130 	{ PCMCIA_VENDOR_COREGA, PCMCIA_PRODUCT_COREGA_WIRELESS_LAN_PCC_11,
131 	  PCMCIA_CIS_COREGA_WIRELESS_LAN_PCC_11 },
132 
133 	{ PCMCIA_VENDOR_COREGA, PCMCIA_PRODUCT_COREGA_WIRELESS_LAN_PCCA_11,
134 	  PCMCIA_CIS_COREGA_WIRELESS_LAN_PCCA_11 },
135 
136 	{ PCMCIA_VENDOR_COREGA, PCMCIA_PRODUCT_COREGA_WIRELESS_LAN_PCCB_11,
137 	  PCMCIA_CIS_COREGA_WIRELESS_LAN_PCCB_11 },
138 
139 	{ PCMCIA_VENDOR_COREGA, PCMCIA_PRODUCT_COREGA_WIRELESS_LAN_PCCL_11,
140 	  PCMCIA_CIS_COREGA_WIRELESS_LAN_PCCL_11 },
141 
142 	{ PCMCIA_VENDOR_COREGA, PCMCIA_PRODUCT_COREGA_WIRELESS_LAN_WLCFL_11,
143 	  PCMCIA_CIS_COREGA_WIRELESS_LAN_WLCFL_11 },
144 
145 	{ PCMCIA_VENDOR_INTEL, PCMCIA_PRODUCT_INTEL_PRO_WLAN_2011,
146 	  PCMCIA_CIS_INTEL_PRO_WLAN_2011 },
147 
148 	{ PCMCIA_VENDOR_INTERSIL2, PCMCIA_PRODUCT_INTERSIL2_PRISM2,
149 	  PCMCIA_CIS_INTERSIL2_PRISM2 },
150 
151 	{ PCMCIA_VENDOR_INTERSIL2, PCMCIA_PRODUCT_GEMTEK_WLAN,
152 	  PCMCIA_CIS_GEMTEK_WLAN },
153 
154 	{ PCMCIA_VENDOR_SAMSUNG, PCMCIA_PRODUCT_SAMSUNG_SWL_2000N,
155 	  PCMCIA_CIS_SAMSUNG_SWL_2000N },
156 
157 	{ PCMCIA_VENDOR_ELSA, PCMCIA_PRODUCT_ELSA_XI300_IEEE,
158 	  PCMCIA_CIS_ELSA_XI300_IEEE },
159 
160 	{ PCMCIA_VENDOR_ELSA, PCMCIA_PRODUCT_ELSA_XI325_IEEE,
161 	  PCMCIA_CIS_ELSA_XI325_IEEE },
162 
163 	{ PCMCIA_VENDOR_ELSA, PCMCIA_PRODUCT_ELSA_XI800_IEEE,
164 	  PCMCIA_CIS_ELSA_XI800_IEEE },
165 
166 	{ PCMCIA_VENDOR_ELSA, PCMCIA_PRODUCT_ELSA_SMC2531WB,
167 	  PCMCIA_CIS_ELSA_SMC2531WB },
168 
169 	{ PCMCIA_VENDOR_COMPAQ, PCMCIA_PRODUCT_COMPAQ_NC5004,
170 	  PCMCIA_CIS_COMPAQ_NC5004 },
171 
172 	{ PCMCIA_VENDOR_CONTEC, PCMCIA_PRODUCT_CONTEC_FX_DS110_PCC,
173 	  PCMCIA_CIS_CONTEC_FX_DS110_PCC },
174 
175 	{ PCMCIA_VENDOR_TDK, PCMCIA_PRODUCT_TDK_LAK_CD011WL,
176 	  PCMCIA_CIS_TDK_LAK_CD011WL },
177 
178 	{ PCMCIA_VENDOR_IODATA2, PCMCIA_PRODUCT_IODATA2_WNB11PCM,
179 	  PCMCIA_CIS_IODATA2_WNB11PCM },
180 
181 	{ PCMCIA_VENDOR_IODATA2, PCMCIA_PRODUCT_IODATA2_WCF12,
182 	  PCMCIA_CIS_IODATA2_WCF12 },
183 
184 	{ PCMCIA_VENDOR_BUFFALO, PCMCIA_PRODUCT_BUFFALO_WLI_PCM_S11,
185 	  PCMCIA_CIS_BUFFALO_WLI_PCM_S11 },
186 
187 	{ PCMCIA_VENDOR_BUFFALO, PCMCIA_PRODUCT_BUFFALO_WLI_CF_S11G,
188 	  PCMCIA_CIS_BUFFALO_WLI_CF_S11G },
189 
190 	{ PCMCIA_VENDOR_EMTAC, PCMCIA_PRODUCT_EMTAC_WLAN,
191 	  PCMCIA_CIS_EMTAC_WLAN },
192 
193 	{ PCMCIA_VENDOR_INTERSIL, PCMCIA_PRODUCT_INTERSIL_ISL37100P,
194 	  PCMCIA_CIS_INTERSIL_ISL37100P },
195 
196 	{ PCMCIA_VENDOR_INTERSIL, PCMCIA_PRODUCT_INTERSIL_ISL37110P,
197 	  PCMCIA_CIS_INTERSIL_ISL37110P },
198 
199 	{ PCMCIA_VENDOR_INTERSIL, PCMCIA_PRODUCT_INTERSIL_ISL37300P,
200 	  PCMCIA_CIS_INTERSIL_ISL37300P },
201 
202 	{ PCMCIA_VENDOR_SIMPLETECH, PCMCIA_PRODUCT_SIMPLETECH_SPECTRUM24,
203 	  PCMCIA_CIS_SIMPLETECH_SPECTRUM24 },
204 
205 	{ PCMCIA_VENDOR_ERICSSON, PCMCIA_PRODUCT_ERICSSON_WIRELESSLAN,
206 	  PCMCIA_CIS_ERICSSON_WIRELESSLAN },
207 
208 	{ PCMCIA_VENDOR_SYMBOL, PCMCIA_PRODUCT_SYMBOL_LA4100,
209 	  PCMCIA_CIS_SYMBOL_LA4100 },
210 
211 	{ PCMCIA_VENDOR_LINKSYS2, PCMCIA_PRODUCT_LINKSYS2_IWN,
212 	  PCMCIA_CIS_NANOSPEED_PRISM2 },
213 
214 	{ PCMCIA_VENDOR_LINKSYS2, PCMCIA_PRODUCT_LINKSYS2_IWN3,
215 	  PCMCIA_CIS_LINKSYS2_IWN3 },
216 
217 	{ PCMCIA_VENDOR_LINKSYS2, PCMCIA_PRODUCT_LINKSYS2_WCF11,
218 	  PCMCIA_CIS_LINKSYS2_WCF11 },
219 
220 	{ PCMCIA_VENDOR_MICROSOFT, PCMCIA_PRODUCT_MICROSOFT_MN_520,
221 	  PCMCIA_CIS_MICROSOFT_MN_520 },
222 
223 	{ PCMCIA_VENDOR_PLANEX, PCMCIA_PRODUCT_PLANEX_GWNS11H,
224 	  PCMCIA_CIS_PLANEX_GWNS11H },
225 
226 	{ PCMCIA_VENDOR_BAY, PCMCIA_PRODUCT_BAY_EMOBILITY_11B,
227 	  PCMCIA_CIS_BAY_EMOBILITY_11B },
228 
229 	{ PCMCIA_VENDOR_ACTIONTEC, PCMCIA_PRODUCT_ACTIONTEC_PRISM,
230 	  PCMCIA_CIS_ACTIONTEC_PRISM },
231 
232 	{ PCMCIA_VENDOR_DLINK_2, PCMCIA_PRODUCT_DLINK_DWL650H,
233 	  PCMCIA_CIS_DLINK_DWL650H },
234 
235 	{ PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_WL110,
236 	  PCMCIA_CIS_FUJITSU_WL110 },
237 
238 	{ PCMCIA_VENDOR_ARTEM, PCMCIA_PRODUCT_ARTEM_ONAIR,
239 	  PCMCIA_CIS_ARTEM_ONAIR },
240 
241 	{ PCMCIA_VENDOR_ASUSTEK, PCMCIA_PRODUCT_ASUSTEK_WL_100,
242 	  PCMCIA_CIS_ASUSTEK_WL_100 },
243 
244 	{ PCMCIA_VENDOR_PROXIM, PCMCIA_PRODUCT_PROXIM_RANGELANDS_8430,
245 	  PCMCIA_CIS_PROXIM_RANGELANDS_8430 },
246 
247 	{ PCMCIA_VENDOR_SIEMENS, PCMCIA_PRODUCT_SIEMENS_SS1021,
248 	  PCMCIA_CIS_SIEMENS_SS1021 },
249 };
250 static const size_t wi_pcmcia_nproducts =
251     sizeof(wi_pcmcia_products) / sizeof(wi_pcmcia_products[0]);
252 
253 static int
254 wi_pcmcia_match(struct device *parent, struct cfdata *match,
255     void *aux)
256 {
257 	struct pcmcia_attach_args *pa = aux;
258 
259 	if (pcmcia_product_lookup(pa, wi_pcmcia_products, wi_pcmcia_nproducts,
260 	    sizeof(wi_pcmcia_products[0]), NULL))
261 		return (1);
262 	return (0);
263 }
264 
265 static int
266 wi_pcmcia_enable(sc)
267 	struct wi_softc *sc;
268 {
269 	struct wi_pcmcia_softc *psc = (struct wi_pcmcia_softc *)sc;
270 	struct pcmcia_function *pf = psc->sc_pf;
271 	int error;
272 
273 	/* establish the interrupt. */
274 	sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, wi_intr, sc);
275 	if (!sc->sc_ih)
276 		return (EIO);
277 
278 	error = pcmcia_function_enable(pf);
279 	if (error) {
280 		pcmcia_intr_disestablish(pf, sc->sc_ih);
281 		sc->sc_ih = 0;
282 		return (EIO);
283 	}
284 
285 	if (psc->sc_symbol_cf) {
286 #if WI_PCMCIA_SPECTRUM24T_FW
287 		if (wi_pcmcia_load_firm(sc,
288 		    spectrum24t_primsym, sizeof(spectrum24t_primsym),
289 		    spectrum24t_secsym, sizeof(spectrum24t_secsym))) {
290 			aprint_error_dev(&sc->sc_dev, "couldn't load firmware\n");
291 			wi_pcmcia_disable(sc);
292 			return (EIO);
293 		}
294 #else
295 		aprint_error_dev(&sc->sc_dev, "firmware load not configured\n");
296 		return EIO;
297 #endif
298 	}
299 	DELAY(1000);
300 
301 	return (0);
302 }
303 
304 static void
305 wi_pcmcia_disable(sc)
306 	struct wi_softc *sc;
307 {
308 	struct wi_pcmcia_softc *psc = (struct wi_pcmcia_softc *)sc;
309 
310 	pcmcia_function_disable(psc->sc_pf);
311 	pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
312 	sc->sc_ih = 0;
313 }
314 
315 static int
316 wi_pcmcia_validate_config(cfe)
317 	struct pcmcia_config_entry *cfe;
318 {
319 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
320 	    cfe->num_iospace != 1 ||
321 	    cfe->iospace[0].length < WI_IOSIZE)
322 		return (EINVAL);
323 	cfe->num_memspace = 0;
324 	return (0);
325 }
326 
327 static void
328 wi_pcmcia_attach(struct device  *parent, struct device *self,
329     void *aux)
330 {
331 	struct wi_pcmcia_softc *psc = (void *)self;
332 	struct wi_softc *sc = &psc->sc_wi;
333 	struct pcmcia_attach_args *pa = aux;
334 	struct pcmcia_config_entry *cfe;
335 	int haveaddr;
336 	int error;
337 
338 	aprint_naive("\n");
339 
340 	psc->sc_pf = pa->pf;
341 
342 	error = pcmcia_function_configure(pa->pf, wi_pcmcia_validate_config);
343 	if (error) {
344 		aprint_error_dev(self, "configure failed, error=%d\n",
345 		    error);
346 		return;
347 	}
348 
349 	cfe = pa->pf->cfe;
350 	sc->sc_iot = cfe->iospace[0].handle.iot;
351 	sc->sc_ioh = cfe->iospace[0].handle.ioh;
352 
353 	if (pa->manufacturer == PCMCIA_VENDOR_SYMBOL &&
354 	    pa->product == PCMCIA_PRODUCT_SYMBOL_LA4100)
355 		psc->sc_symbol_cf = 1;
356 	/*
357 	 * XXX: Sony PEGA-WL100 CF card has a same vendor/product id as
358 	 *	Intel PCMCIA card.  It may be incorrect to detect by the
359 	 *	initial value of COR register.
360 	 */
361 	if (pa->manufacturer == PCMCIA_VENDOR_INTEL &&
362 	    pa->product == PCMCIA_PRODUCT_INTEL_PRO_WLAN_2011 &&
363 	    CSR_READ_2(sc, WI_COR) == WI_COR_IOMODE)
364 		psc->sc_symbol_cf = 1;
365 
366 	error = wi_pcmcia_enable(sc);
367 	if (error)
368 		goto fail;
369 
370 	sc->sc_pci = 0;
371 	sc->sc_enable = wi_pcmcia_enable;
372 	sc->sc_disable = wi_pcmcia_disable;
373 
374 	printf("%s:", device_xname(self));
375 
376 	haveaddr = pa->pf->pf_funce_lan_nidlen == IEEE80211_ADDR_LEN;
377 	if (wi_attach(sc, haveaddr ? pa->pf->pf_funce_lan_nid : 0) != 0) {
378 		aprint_error_dev(self, "failed to attach controller\n");
379 		goto fail2;
380 	}
381 
382 	if (!pmf_device_register(self, NULL, NULL))
383 		aprint_error_dev(self, "couldn't establish power handler\n");
384 	else
385 		pmf_class_network_register(self, &sc->sc_if);
386 
387 	wi_pcmcia_disable(sc);
388 	psc->sc_state = WI_PCMCIA_ATTACHED;
389 	return;
390 
391 fail2:
392 	wi_pcmcia_disable(sc);
393 fail:
394 	pcmcia_function_unconfigure(pa->pf);
395 }
396 
397 static int
398 wi_pcmcia_detach(struct device *self, int flags)
399 {
400 	struct wi_pcmcia_softc *psc = (struct wi_pcmcia_softc *)self;
401 	int error;
402 
403 	if (psc->sc_state != WI_PCMCIA_ATTACHED)
404 		return (0);
405 
406 	error = wi_detach(&psc->sc_wi);
407 	if (error != 0)
408 		return (error);
409 
410 	pcmcia_function_unconfigure(psc->sc_pf);
411 
412 	return (0);
413 }
414 
415 /*
416  * Special routines to download firmware for Symbol CF card.
417  * XXX: This should be modified generic into any PRISM-2 based card.
418  */
419 
420 #define	WI_SBCF_PDIADDR		0x3100
421 
422 /* unaligned load little endian */
423 #define	GETLE32(p)	((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24))
424 #define	GETLE16(p)	((p)[0] | ((p)[1]<<8))
425 
426 #if WI_PCMCIA_SPECTRUM24T_FW
427 static int
428 wi_pcmcia_load_firm(sc, primsym, primlen, secsym, seclen)
429 	struct wi_softc *sc;
430 	const void *primsym, *secsym;
431 	int primlen, seclen;
432 {
433 	u_int8_t ebuf[256];
434 	int i;
435 
436 	/* load primary code and run it */
437 	wi_pcmcia_set_hcr(sc, WI_HCR_EEHOLD);
438 	if (wi_pcmcia_write_firm(sc, primsym, primlen, NULL, 0))
439 		return EIO;
440 	wi_pcmcia_set_hcr(sc, WI_HCR_RUN);
441 	for (i = 0; ; i++) {
442 		if (i == 10)
443 			return ETIMEDOUT;
444 		tsleep(sc, PWAIT, "wiinit", 1);
445 		if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT)
446 			break;
447 		/* write the magic key value to unlock aux port */
448 		CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0);
449 		CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1);
450 		CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2);
451 		CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL);
452 	}
453 
454 	/* issue read EEPROM command: XXX copied from wi_cmd() */
455 	CSR_WRITE_2(sc, WI_PARAM0, 0);
456 	CSR_WRITE_2(sc, WI_PARAM1, 0);
457 	CSR_WRITE_2(sc, WI_PARAM2, 0);
458 	CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE);
459         for (i = 0; i < WI_TIMEOUT; i++) {
460                 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
461                         break;
462                 DELAY(1);
463         }
464         CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
465 
466 	CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ);
467 	CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ);
468 	CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA,
469 	    (u_int16_t *)ebuf, sizeof(ebuf) / 2);
470 	if (GETLE16(ebuf) > sizeof(ebuf))
471 		return EIO;
472 	if (wi_pcmcia_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf)))
473 		return EIO;
474 	return 0;
475 }
476 
477 static int
478 wi_pcmcia_write_firm(sc, buf, buflen, ebuf, ebuflen)
479 	struct wi_softc *sc;
480 	const void *buf, *ebuf;
481 	int buflen, ebuflen;
482 {
483 	const u_int8_t *p, *ep, *q, *eq;
484 	char *endp;
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, &endp, 10);
497 	p = (void *)endp;
498 	pdrlen = strtoul(p + 1, &endp, 10);
499 	p = (void *)endp;
500 	while (p < ep && *p++ != 0x1a);	/* skip rest of header */
501 
502 	/*
503 	 * Block records: address[4], length[2], data[length];
504 	 */
505 	for (i = 0; i < nblk; i++) {
506 		addr = GETLE32(p);	p += 4;
507 		len  = GETLE16(p);	p += 2;
508 		CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
509 		CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
510 		CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
511 		    (const u_int16_t *)p, len / 2);
512 		p += len;
513 	}
514 
515 	/*
516 	 * PDR: id[4], address[4], length[4];
517 	 */
518 	for (i = 0; i < pdrlen; ) {
519 		id   = GETLE32(p);	p += 4; i += 4;
520 		addr = GETLE32(p);	p += 4; i += 4;
521 		len  = GETLE32(p);	p += 4; i += 4;
522 		/* replace PDR entry with the values from EEPROM, if any */
523 		for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) {
524 			elen = GETLE16(q);	q += 2;
525 			eid  = GETLE16(q);	q += 2;
526 			elen--;		/* elen includes eid */
527 			if (eid == 0)
528 				break;
529 			if (eid != id)
530 				continue;
531 			CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
532 			CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
533 			CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
534 			    (const u_int16_t *)q, len / 2);
535 			break;
536 		}
537 	}
538 	return 0;
539 }
540 
541 static int
542 wi_pcmcia_set_hcr(sc, mode)
543 	struct wi_softc *sc;
544 	int mode;
545 {
546 	u_int16_t hcr;
547 
548 	CSR_WRITE_2(sc, WI_COR, WI_COR_RESET);
549 	tsleep(sc, PWAIT, "wiinit", 1);
550 	hcr = CSR_READ_2(sc, WI_HCR);
551 	hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE);
552 	CSR_WRITE_2(sc, WI_HCR, hcr);
553 	tsleep(sc, PWAIT, "wiinit", 1);
554 	CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE);
555 	tsleep(sc, PWAIT, "wiinit", 1);
556 	return 0;
557 }
558 #endif
559