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