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