wi.c revision 1.96 1 /* $NetBSD: wi.c,v 1.96 2002/10/02 17:11:34 onoe Exp $ */
2
3 /*
4 * Copyright (c) 1997, 1998, 1999
5 * Bill Paul <wpaul (at) ctr.columbia.edu>. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /*
36 * Lucent WaveLAN/IEEE 802.11 PCMCIA driver for NetBSD.
37 *
38 * Original FreeBSD driver written by Bill Paul <wpaul (at) ctr.columbia.edu>
39 * Electrical Engineering Department
40 * Columbia University, New York City
41 */
42
43 /*
44 * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
45 * from Lucent. Unlike the older cards, the new ones are programmed
46 * entirely via a firmware-driven controller called the Hermes.
47 * Unfortunately, Lucent will not release the Hermes programming manual
48 * without an NDA (if at all). What they do release is an API library
49 * called the HCF (Hardware Control Functions) which is supposed to
50 * do the device-specific operations of a device driver for you. The
51 * publically available version of the HCF library (the 'HCF Light') is
52 * a) extremely gross, b) lacks certain features, particularly support
53 * for 802.11 frames, and c) is contaminated by the GNU Public License.
54 *
55 * This driver does not use the HCF or HCF Light at all. Instead, it
56 * programs the Hermes controller directly, using information gleaned
57 * from the HCF Light code and corresponding documentation.
58 *
59 * This driver supports both the PCMCIA and ISA versions of the
60 * WaveLAN/IEEE cards. Note however that the ISA card isn't really
61 * anything of the sort: it's actually a PCMCIA bridge adapter
62 * that fits into an ISA slot, into which a PCMCIA WaveLAN card is
63 * inserted. Consequently, you need to use the pccard support for
64 * both the ISA and PCMCIA adapters.
65 */
66
67 /*
68 * FreeBSD driver ported to NetBSD by Bill Sommerfeld in the back of the
69 * Oslo IETF plenary meeting.
70 */
71
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: wi.c,v 1.96 2002/10/02 17:11:34 onoe Exp $");
74
75 #define WI_HERMES_AUTOINC_WAR /* Work around data write autoinc bug. */
76 #define WI_HERMES_STATS_WAR /* Work around stats counter bug. */
77
78 #include "bpfilter.h"
79
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/callout.h>
83 #include <sys/device.h>
84 #include <sys/socket.h>
85 #include <sys/mbuf.h>
86 #include <sys/ioctl.h>
87 #include <sys/kernel.h> /* for hz */
88 #include <sys/proc.h>
89
90 #include <net/if.h>
91 #include <net/if_dl.h>
92 #include <net/if_media.h>
93 #include <net/if_ether.h>
94 #include <net/if_ieee80211.h>
95
96 #if NBPFILTER > 0
97 #include <net/bpf.h>
98 #include <net/bpfdesc.h>
99 #endif
100
101 #include <machine/bus.h>
102
103 #include <dev/ic/wi_ieee.h>
104 #include <dev/ic/wireg.h>
105 #include <dev/ic/wivar.h>
106
107 static int wi_init(struct ifnet *);
108 static void wi_stop(struct ifnet *, int);
109 static void wi_start(struct ifnet *);
110 static int wi_reset(struct wi_softc *);
111 static void wi_watchdog(struct ifnet *);
112 static int wi_ioctl(struct ifnet *, u_long, caddr_t);
113 static int wi_media_change(struct ifnet *);
114 static void wi_media_status(struct ifnet *, struct ifmediareq *);
115
116 static void wi_rx_intr(struct wi_softc *);
117 static void wi_tx_intr(struct wi_softc *);
118 static void wi_info_intr(struct wi_softc *);
119
120 static int wi_get_cfg(struct ifnet *, u_long, caddr_t);
121 static int wi_set_cfg(struct ifnet *, u_long, caddr_t);
122 static int wi_write_txrate(struct wi_softc *);
123 static int wi_write_wep(struct wi_softc *);
124 static int wi_write_multi(struct wi_softc *);
125 static int wi_alloc_fid(struct wi_softc *, int, int *);
126 static void wi_read_nicid(struct wi_softc *);
127 static int wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
128
129 static int wi_cmd(struct wi_softc *, int, int, int, int);
130 static int wi_seek_bap(struct wi_softc *, int, int);
131 static int wi_read_bap(struct wi_softc *, int, int, void *, int);
132 static int wi_write_bap(struct wi_softc *, int, int, void *, int);
133 static int wi_read_rid(struct wi_softc *, int, void *, int *);
134 static int wi_write_rid(struct wi_softc *, int, void *, int);
135
136 static int wi_newstate(void *, enum ieee80211_state);
137
138 static int wi_scan_ap(struct wi_softc *);
139 static void wi_scan_result(struct wi_softc *, int, int);
140
141 static inline int
142 wi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
143 {
144
145 val = htole16(val);
146 return wi_write_rid(sc, rid, &val, sizeof(val));
147 }
148
149 #ifdef WI_DEBUG
150 int wi_debug = 0;
151
152 #define DPRINTF(X) if (wi_debug) printf X
153 #define DPRINTF2(X) if (wi_debug > 1) printf X
154 #else
155 #define DPRINTF(X)
156 #define DPRINTF2(X)
157 #endif
158
159 #define WI_INTRS (WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
160
161 struct wi_card_ident
162 wi_card_ident[] = {
163 /* CARD_ID CARD_NAME FIRM_TYPE */
164 { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT },
165 { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT },
166 { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT },
167 { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL },
168 { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL },
169 { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL },
170 { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL },
171 { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL },
172 { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL },
173 { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL },
174 { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL },
175 { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL },
176 { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
177 { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
178 { WI_NIC_3842_PCMCIA_ATM_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
179 { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
180 { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
181 { WI_NIC_3842_MINI_ATM_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
182 { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
183 { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
184 { WI_NIC_3842_PCI_ATM_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
185 { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
186 { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
187 { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
188 { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
189 { 0, NULL, 0 },
190 };
191
192 int
193 wi_attach(struct wi_softc *sc)
194 {
195 struct ieee80211com *ic = &sc->sc_ic;
196 struct ifnet *ifp = &ic->ic_if;
197 int i, nrate, mword, buflen;
198 u_int8_t r;
199 u_int16_t val;
200 u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
201 static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
202 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
203 };
204 int s;
205
206 s = splnet();
207
208 /* Make sure interrupts are disabled. */
209 CSR_WRITE_2(sc, WI_INT_EN, 0);
210 CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
211
212 /* Reset the NIC. */
213 if (wi_reset(sc) != 0) {
214 splx(s);
215 return 1;
216 }
217
218 buflen = IEEE80211_ADDR_LEN;
219 if (wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen) != 0 ||
220 IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) {
221 printf(" could not get mac address, attach failed\n");
222 splx(s);
223 return 1;
224 }
225
226 printf(" 802.11 address %s\n", ether_sprintf(ic->ic_myaddr));
227
228 /* Read NIC identification */
229 wi_read_nicid(sc);
230
231 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
232 ifp->if_softc = sc;
233 ifp->if_start = wi_start;
234 ifp->if_ioctl = wi_ioctl;
235 ifp->if_watchdog = wi_watchdog;
236 ifp->if_init = wi_init;
237 ifp->if_stop = wi_stop;
238 ifp->if_flags =
239 IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST | IFF_NOTRAILERS;
240 IFQ_SET_READY(&ifp->if_snd);
241
242 ic->ic_phytype = IEEE80211_T_DS;
243 ic->ic_opmode = IEEE80211_M_STA;
244 ic->ic_flags = IEEE80211_F_HASPMGT | IEEE80211_F_HASAHDEMO;
245 ic->ic_state = IEEE80211_S_INIT;
246 ic->ic_newstate = wi_newstate;
247
248 /* Find available channel */
249 buflen = sizeof(val);
250 if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
251 val = htole16(0x1fff); /* assume 1-11 */
252 for (i = 0; i < 16; i++) {
253 if (isset((u_int8_t*)&val, i))
254 setbit(ic->ic_chan_avail, i + 1);
255 }
256
257 /* Find default IBSS channel */
258 buflen = sizeof(val);
259 if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0)
260 ic->ic_ibss_chan = le16toh(val);
261 else {
262 /* use lowest available channel */
263 for (i = 0; i < 16; i++) {
264 if (isset(ic->ic_chan_avail, i))
265 break;
266 }
267 ic->ic_ibss_chan = i;
268 }
269
270 /*
271 * Set flags based on firmware version.
272 */
273 switch (sc->sc_firmware_type) {
274 case WI_LUCENT:
275 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
276 #ifdef WI_HERMES_AUTOINC_WAR
277 /* XXX: not confirmed, but never seen for recent firmware */
278 if (sc->sc_sta_firmware_ver < 40000) {
279 sc->sc_flags |= WI_FLAGS_BUG_AUTOINC;
280 }
281 #endif
282 if (sc->sc_sta_firmware_ver >= 60000)
283 sc->sc_flags |= WI_FLAGS_HAS_MOR;
284 if (sc->sc_sta_firmware_ver >= 60006)
285 ic->ic_flags |= IEEE80211_F_HASIBSS;
286 sc->sc_ibss_port = 1;
287 break;
288
289 case WI_INTERSIL:
290 sc->sc_flags |= WI_FLAGS_HAS_ROAMING;
291 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
292 if (sc->sc_sta_firmware_ver >= 800) {
293 ic->ic_flags |= IEEE80211_F_HASHOSTAP;
294 ic->ic_flags |= IEEE80211_F_HASIBSS;
295 }
296 sc->sc_ibss_port = 0;
297 break;
298
299 case WI_SYMBOL:
300 sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY;
301 if (sc->sc_sta_firmware_ver >= 25000)
302 ic->ic_flags |= IEEE80211_F_HASIBSS;
303 sc->sc_ibss_port = 4;
304 break;
305 }
306
307 /*
308 * Find out if we support WEP on this card.
309 */
310 buflen = sizeof(val);
311 if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
312 val != htole16(0))
313 ic->ic_flags |= IEEE80211_F_HASWEP;
314
315 /* Find supported rates. */
316 buflen = sizeof(ratebuf);
317 if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
318 nrate = le16toh(*(u_int16_t *)ratebuf);
319 if (nrate > IEEE80211_RATE_SIZE)
320 nrate = IEEE80211_RATE_SIZE;
321 memcpy(ic->ic_sup_rates, ratebuf + 2, nrate);
322 }
323 buflen = sizeof(val);
324
325 sc->sc_max_datalen = 2304;
326 sc->sc_rts_thresh = 2347;
327 sc->sc_system_scale = 1;
328 sc->sc_cnfauthmode = 1;
329 sc->sc_roaming_mode = 1;
330
331 ifmedia_init(&sc->sc_media, 0, wi_media_change, wi_media_status);
332 printf("%s: supported rates: ", sc->sc_dev.dv_xname);
333 #define ADD(s, o) ifmedia_add(&sc->sc_media, \
334 IFM_MAKEWORD(IFM_IEEE80211, (s), (o), 0), 0, NULL)
335 ADD(IFM_AUTO, 0);
336 if (ic->ic_flags & IEEE80211_F_HASHOSTAP)
337 ADD(IFM_AUTO, IFM_IEEE80211_HOSTAP);
338 if (ic->ic_flags & IEEE80211_F_HASIBSS)
339 ADD(IFM_AUTO, IFM_IEEE80211_ADHOC);
340 ADD(IFM_AUTO, IFM_IEEE80211_ADHOC | IFM_FLAG0);
341 for (i = 0; i < nrate; i++) {
342 r = ic->ic_sup_rates[i];
343 mword = ieee80211_rate2media(r, IEEE80211_T_DS);
344 if (mword == 0)
345 continue;
346 printf("%s%d%sMbps", (i != 0 ? " " : ""),
347 (r & IEEE80211_RATE_VAL) / 2, ((r & 0x1) != 0 ? ".5" : ""));
348 ADD(mword, 0);
349 if (ic->ic_flags & IEEE80211_F_HASHOSTAP)
350 ADD(mword, IFM_IEEE80211_HOSTAP);
351 if (ic->ic_flags & IEEE80211_F_HASIBSS)
352 ADD(mword, IFM_IEEE80211_ADHOC);
353 ADD(mword, IFM_IEEE80211_ADHOC | IFM_FLAG0);
354 }
355 printf("\n");
356 ifmedia_set(&sc->sc_media, IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO, 0, 0));
357 #undef ADD
358
359 /*
360 * Call MI attach routines.
361 */
362
363 if_attach(ifp);
364 ieee80211_ifattach(ifp);
365
366 /* Attach is successful. */
367 sc->sc_attached = 1;
368
369 splx(s);
370 return 0;
371 }
372
373 int
374 wi_detach(struct wi_softc *sc)
375 {
376 struct ifnet *ifp = &sc->sc_ic.ic_if;
377 int s;
378
379 if (!sc->sc_attached)
380 return 0;
381
382 s = splnet();
383
384 /* Delete all remaining media. */
385 ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY);
386
387 ieee80211_ifdetach(ifp);
388 if_detach(ifp);
389 if (sc->sc_enabled) {
390 if (sc->sc_disable)
391 (*sc->sc_disable)(sc);
392 sc->sc_enabled = 0;
393 }
394 splx(s);
395 return 0;
396 }
397
398 int
399 wi_activate(struct device *self, enum devact act)
400 {
401 struct wi_softc *sc = (struct wi_softc *)self;
402 int rv = 0, s;
403
404 s = splnet();
405 switch (act) {
406 case DVACT_ACTIVATE:
407 rv = EOPNOTSUPP;
408 break;
409
410 case DVACT_DEACTIVATE:
411 if_deactivate(&sc->sc_ic.ic_if);
412 break;
413 }
414 splx(s);
415 return rv;
416 }
417
418 void
419 wi_power(struct wi_softc *sc, int why)
420 {
421 struct ifnet *ifp = &sc->sc_ic.ic_if;
422 int s;
423
424 s = splnet();
425 switch (why) {
426 case PWR_SUSPEND:
427 case PWR_STANDBY:
428 wi_stop(ifp, 1);
429 break;
430 case PWR_RESUME:
431 if (ifp->if_flags & IFF_UP) {
432 wi_init(ifp);
433 (void)wi_intr(sc);
434 }
435 break;
436 case PWR_SOFTSUSPEND:
437 case PWR_SOFTSTANDBY:
438 case PWR_SOFTRESUME:
439 break;
440 }
441 splx(s);
442 }
443
444 void
445 wi_shutdown(struct wi_softc *sc)
446 {
447 struct ifnet *ifp = &sc->sc_ic.ic_if;
448
449 if (sc->sc_attached)
450 wi_stop(ifp, 1);
451 }
452
453 int
454 wi_intr(void *arg)
455 {
456 int i;
457 struct wi_softc *sc = arg;
458 struct ifnet *ifp = &sc->sc_ic.ic_if;
459 u_int16_t status, raw_status, last_status;
460
461 if (sc->sc_enabled == 0 ||
462 (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 ||
463 (ifp->if_flags & IFF_RUNNING) == 0)
464 return 0;
465
466 if ((ifp->if_flags & IFF_UP) == 0) {
467 CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
468 CSR_WRITE_2(sc, WI_INT_EN, 0);
469 return 1;
470 }
471
472 /* maximum 10 loops per interrupt */
473 last_status = 0;
474 for (i = 0; i < 10; i++) {
475 /*
476 * Only believe a status bit when we enter wi_intr, or when
477 * the bit was "off" the last time through the loop. This is
478 * my strategy to avoid racing the hardware/firmware if I
479 * can re-read the event status register more quickly than
480 * it is updated.
481 */
482 raw_status = CSR_READ_2(sc, WI_EVENT_STAT);
483 status = raw_status & ~last_status;
484 if ((status & WI_INTRS) == 0)
485 break;
486 last_status = raw_status;
487
488 if (status & WI_EV_RX)
489 wi_rx_intr(sc);
490
491 if (status & WI_EV_ALLOC)
492 wi_tx_intr(sc);
493
494 if (status & WI_EV_INFO)
495 wi_info_intr(sc);
496
497 if (!(ifp->if_flags & IFF_OACTIVE) &&
498 !IFQ_IS_EMPTY(&ifp->if_snd))
499 wi_start(ifp);
500 }
501
502 return 1;
503 }
504
505 static int
506 wi_init(struct ifnet *ifp)
507 {
508 struct wi_softc *sc = ifp->if_softc;
509 struct ieee80211com *ic = &sc->sc_ic;
510 struct wi_joinreq join;
511 int i;
512 int error = 0, wasenabled;
513
514 DPRINTF(("wi_init: enabled %d\n", sc->sc_enabled));
515 wasenabled = sc->sc_enabled;
516 if (!sc->sc_enabled) {
517 if ((error = (*sc->sc_enable)(sc)) != 0)
518 goto out;
519 sc->sc_enabled = 1;
520 } else
521 wi_stop(ifp, 0);
522
523 /* Symbol firmware cannot be initialized more than once */
524 if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
525 if ((error = wi_reset(sc)) != 0)
526 goto out;
527 }
528
529 /* common 802.11 configuration */
530 switch (ic->ic_opmode) {
531 case IEEE80211_M_STA:
532 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS);
533 break;
534 case IEEE80211_M_IBSS:
535 wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port);
536 break;
537 case IEEE80211_M_AHDEMO:
538 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
539 break;
540 case IEEE80211_M_HOSTAP:
541 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP);
542 break;
543 }
544
545 /* Intersil interprets this RID as joining ESS even in IBSS mode */
546 if (sc->sc_firmware_type == WI_LUCENT &&
547 (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0)
548 wi_write_val(sc, WI_RID_CREATE_IBSS, 1);
549 else
550 wi_write_val(sc, WI_RID_CREATE_IBSS, 0);
551 wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
552 wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid,
553 ic->ic_des_esslen);
554 wi_write_val(sc, WI_RID_OWN_CHNL, ic->ic_ibss_chan);
555 wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen);
556 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
557 wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN);
558 wi_write_val(sc, WI_RID_PM_ENABLED,
559 (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
560
561 /* not yet common 802.11 configuration */
562 wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen);
563 wi_write_val(sc, WI_RID_RTS_THRESH, sc->sc_rts_thresh);
564
565 /* driver specific 802.11 configuration */
566 if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)
567 wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale);
568 if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
569 wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode);
570 if (sc->sc_flags & WI_FLAGS_HAS_MOR)
571 wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven);
572 wi_write_txrate(sc);
573 wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen);
574
575 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
576 sc->sc_firmware_type == WI_INTERSIL) {
577 wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval);
578 wi_write_val(sc, WI_RID_BASIC_RATE, 0x03); /* 1, 2 */
579 wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */
580 wi_write_val(sc, WI_RID_DTIM_PERIOD, 1);
581 }
582
583 /*
584 * Initialize promisc mode.
585 * Being in the Host-AP mode causes a great
586 * deal of pain if primisc mode is set.
587 * Therefore we avoid confusing the firmware
588 * and always reset promisc mode in Host-AP
589 * mode. Host-AP sees all the packets anyway.
590 */
591 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
592 (ifp->if_flags & IFF_PROMISC) != 0) {
593 wi_write_val(sc, WI_RID_PROMISC, 1);
594 } else {
595 wi_write_val(sc, WI_RID_PROMISC, 0);
596 }
597
598 /* Configure WEP. */
599 if (ic->ic_flags & IEEE80211_F_HASWEP)
600 wi_write_wep(sc);
601
602 /* Set multicast filter. */
603 wi_write_multi(sc);
604
605 if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
606 sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
607 if (sc->sc_firmware_type == WI_SYMBOL)
608 sc->sc_buflen = 1585; /* XXX */
609 for (i = 0; i < WI_NTXBUF; i++) {
610 error = wi_alloc_fid(sc, sc->sc_buflen,
611 &sc->sc_txd[i].d_fid);
612 if (error) {
613 printf("%s: tx buffer allocation failed\n",
614 sc->sc_dev.dv_xname);
615 goto out;
616 }
617 DPRINTF2(("wi_init: txbuf %d allocated %x\n", i,
618 sc->sc_txd[i].d_fid));
619 sc->sc_txd[i].d_len = 0;
620 }
621 }
622 sc->sc_txcur = sc->sc_txnext = 0;
623 if (ic->ic_opmode == IEEE80211_M_IBSS)
624 ic->ic_flags |= IEEE80211_F_IBSSON;
625 else
626 ic->ic_flags &= ~IEEE80211_F_IBSSON;
627
628 /* Enable port 0 */
629 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
630 ifp->if_flags |= IFF_RUNNING;
631 ifp->if_flags &= ~IFF_OACTIVE;
632 if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
633 ic->ic_opmode == IEEE80211_M_HOSTAP)
634 wi_newstate(sc, IEEE80211_S_RUN);
635
636 /* Enable interrupts */
637 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
638
639 if (!wasenabled &&
640 ic->ic_opmode == IEEE80211_M_HOSTAP &&
641 sc->sc_firmware_type == WI_INTERSIL) {
642 /* XXX: some card need to be re-enabled for hostap */
643 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
644 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
645 }
646
647 if (ic->ic_opmode == IEEE80211_M_STA &&
648 ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
649 ic->ic_des_chan != IEEE80211_CHAN_ANY)) {
650 memset(&join, 0, sizeof(join));
651 if (ic->ic_flags & IEEE80211_F_DESBSSID)
652 IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
653 if (ic->ic_des_chan != IEEE80211_CHAN_ANY)
654 join.wi_chan = htole16(ic->ic_des_chan);
655 wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
656 }
657
658 out:
659 if (error) {
660 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
661 wi_stop(ifp, 0);
662 }
663 DPRINTF(("wi_init: return %d\n", error));
664 return error;
665 }
666
667 static void
668 wi_stop(struct ifnet *ifp, int disable)
669 {
670 struct wi_softc *sc = ifp->if_softc;
671
672 DPRINTF(("wi_stop: disable %d\n", disable));
673 ieee80211_new_state(ifp, IEEE80211_S_INIT, -1);
674 if (sc->sc_enabled) {
675 CSR_WRITE_2(sc, WI_INT_EN, 0);
676 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
677 if (disable) {
678 if (sc->sc_disable)
679 (*sc->sc_disable)(sc);
680 sc->sc_enabled = 0;
681 }
682 }
683
684 sc->sc_tx_timer = 0;
685 sc->sc_scan_timer = 0;
686 sc->sc_naps = 0;
687 ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
688 ifp->if_timer = 0;
689 }
690
691 static void
692 wi_start(struct ifnet *ifp)
693 {
694 struct wi_softc *sc = ifp->if_softc;
695 struct ieee80211com *ic = &sc->sc_ic;
696 struct ieee80211_frame *wh;
697 struct mbuf *m0, *m;
698 struct wi_frame frmhdr;
699 int cur, fid, off;
700
701 if (ifp->if_flags & IFF_OACTIVE)
702 return;
703
704 memset(&frmhdr, 0, sizeof(frmhdr));
705 cur = sc->sc_txnext;
706 for (;;) {
707 IF_POLL(&ic->ic_mgtq, m0);
708 if (m0 != NULL) {
709 if (sc->sc_txd[cur].d_len != 0) {
710 ifp->if_flags |= IFF_OACTIVE;
711 break;
712 }
713 IF_DEQUEUE(&ic->ic_mgtq, m0);
714 m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
715 (caddr_t)&frmhdr.wi_ehdr);
716 frmhdr.wi_ehdr.ether_type = 0;
717 wh = mtod(m0, struct ieee80211_frame *);
718 } else {
719 if (ic->ic_state != IEEE80211_S_RUN)
720 break;
721 IFQ_POLL(&ifp->if_snd, m0);
722 if (m0 == NULL)
723 break;
724 if (sc->sc_txd[cur].d_len != 0) {
725 ifp->if_flags |= IFF_OACTIVE;
726 break;
727 }
728 IFQ_DEQUEUE(&ifp->if_snd, m0);
729 ifp->if_opackets++;
730 m_copydata(m0, 0, ETHER_HDR_LEN,
731 (caddr_t)&frmhdr.wi_ehdr);
732 #if NBPFILTER > 0
733 if (ifp->if_bpf)
734 bpf_mtap(ifp->if_bpf, m0);
735 #endif
736
737 if ((m0 = ieee80211_encap(ifp, m0)) == NULL) {
738 ifp->if_oerrors++;
739 continue;
740 }
741 wh = mtod(m0, struct ieee80211_frame *);
742 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
743 !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
744 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
745 IEEE80211_FC0_TYPE_DATA &&
746 ieee80211_find_node(ic, wh->i_addr1) == NULL) {
747 m_freem(m0);
748 ifp->if_oerrors++;
749 continue;
750 }
751 if (ic->ic_flags & IEEE80211_F_WEPON)
752 wh->i_fc[1] |= IEEE80211_FC1_WEP;
753
754 }
755 #if NBPFILTER > 0
756 if (ic->ic_rawbpf)
757 bpf_mtap(ic->ic_rawbpf, m0);
758 #endif
759 frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11);
760 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
761 (wh->i_fc[1] & IEEE80211_FC1_WEP)) {
762 if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) {
763 ifp->if_oerrors++;
764 continue;
765 }
766 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
767 }
768 m_copydata(m0, 0, sizeof(struct ieee80211_frame),
769 (caddr_t)&frmhdr.wi_whdr);
770 m_adj(m0, sizeof(struct ieee80211_frame));
771 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
772 #if NBPFILTER > 0
773 if (sc->sc_drvbpf) {
774 struct mbuf mb;
775
776 M_COPY_PKTHDR(&mb, m0);
777 mb.m_data = (caddr_t)&frmhdr;
778 mb.m_len = sizeof(frmhdr);
779 mb.m_next = m0;
780 mb.m_pkthdr.len += mb.m_len;
781 bpf_mtap(sc->sc_drvbpf, &mb);
782 }
783 #endif
784 fid = sc->sc_txd[cur].d_fid;
785 wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr));
786 off = sizeof(frmhdr);
787 for (m = m0; m != NULL; m = m->m_next) {
788 if (m->m_len == 0)
789 continue;
790 wi_write_bap(sc, fid, off, m->m_data, m->m_len);
791 off += m->m_len;
792 }
793 m_freem(m0);
794 sc->sc_txd[cur].d_len = off;
795 if (sc->sc_txcur == cur) {
796 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
797 printf("%s: xmit failed\n",
798 sc->sc_dev.dv_xname);
799 sc->sc_txd[cur].d_len = 0;
800 continue;
801 }
802 sc->sc_tx_timer = 5;
803 ifp->if_timer = 1;
804 }
805 sc->sc_txnext = cur = (cur + 1) % WI_NTXBUF;
806 }
807 }
808
809
810 static int
811 wi_reset(struct wi_softc *sc)
812 {
813 int i, error;
814
815 DPRINTF(("wi_reset\n"));
816 error = 0;
817 for (i = 0; i < 5; i++) {
818 DELAY(20*1000); /* XXX: way too long! */
819 if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
820 break;
821 }
822 if (error) {
823 printf("%s: init failed\n", sc->sc_dev.dv_xname);
824 return error;
825 }
826 CSR_WRITE_2(sc, WI_INT_EN, 0);
827 CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
828
829 /* Calibrate timer. */
830 wi_write_val(sc, WI_RID_TICK_TIME, 0);
831 return 0;
832 }
833
834 static void
835 wi_watchdog(struct ifnet *ifp)
836 {
837 struct wi_softc *sc = ifp->if_softc;
838
839 ifp->if_timer = 0;
840 if (!sc->sc_enabled)
841 return;
842
843 if (sc->sc_tx_timer) {
844 if (--sc->sc_tx_timer == 0) {
845 printf("%s: device timeout\n", ifp->if_xname);
846 ifp->if_oerrors++;
847 wi_init(ifp);
848 return;
849 }
850 ifp->if_timer = 1;
851 }
852
853 if (sc->sc_scan_timer) {
854 if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
855 sc->sc_firmware_type == WI_INTERSIL) {
856 DPRINTF(("wi_watchdog: inquire scan\n"));
857 wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
858 }
859 if (sc->sc_scan_timer)
860 ifp->if_timer = 1;
861 }
862
863 /* TODO: rate control */
864 ieee80211_watchdog(ifp);
865 }
866
867 static int
868 wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
869 {
870 struct wi_softc *sc = ifp->if_softc;
871 struct ieee80211com *ic = &sc->sc_ic;
872 struct ifreq *ifr = (struct ifreq *)data;
873 int s, error = 0;
874
875 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
876 return ENXIO;
877
878 s = splnet();
879
880 switch (cmd) {
881 case SIOCSIFFLAGS:
882 if (ifp->if_flags & IFF_UP) {
883 if (sc->sc_enabled) {
884 /*
885 * To avoid rescanning another access point,
886 * do not call wi_init() here. Instead,
887 * only reflect promisc mode settings.
888 */
889 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
890 (ifp->if_flags & IFF_PROMISC) != 0)
891 wi_write_val(sc, WI_RID_PROMISC, 1);
892 else
893 wi_write_val(sc, WI_RID_PROMISC, 0);
894 } else
895 error = wi_init(ifp);
896 } else if (sc->sc_enabled)
897 wi_stop(ifp, 1);
898 break;
899 case SIOCSIFMEDIA:
900 case SIOCGIFMEDIA:
901 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
902 break;
903 case SIOCADDMULTI:
904 case SIOCDELMULTI:
905 error = (cmd == SIOCADDMULTI) ?
906 ether_addmulti(ifr, &sc->sc_ic.ic_ec) :
907 ether_delmulti(ifr, &sc->sc_ic.ic_ec);
908 if (error == ENETRESET) {
909 if (sc->sc_enabled) {
910 /* do not rescan */
911 error = wi_write_multi(sc);
912 } else
913 error = 0;
914 }
915 break;
916 case SIOCGIFGENERIC:
917 error = wi_get_cfg(ifp, cmd, data);
918 break;
919 case SIOCSIFGENERIC:
920 error = suser(curproc->p_ucred, &curproc->p_acflag);
921 if (error)
922 break;
923 error = wi_set_cfg(ifp, cmd, data);
924 if (error == ENETRESET) {
925 if (sc->sc_enabled)
926 error = wi_init(ifp);
927 else
928 error = 0;
929 }
930 break;
931 default:
932 error = ieee80211_ioctl(ifp, cmd, data);
933 if (error == ENETRESET) {
934 if (sc->sc_enabled)
935 error = wi_init(ifp);
936 else
937 error = 0;
938 }
939 break;
940 }
941 splx(s);
942 return error;
943 }
944
945 static int
946 wi_media_change(struct ifnet *ifp)
947 {
948 struct wi_softc *sc = ifp->if_softc;
949 struct ieee80211com *ic = &sc->sc_ic;
950 struct ifmedia_entry *ime;
951 enum ieee80211_opmode newmode;
952 int i, rate, error = 0;
953
954 ime = sc->sc_media.ifm_cur;
955 if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) {
956 i = -1;
957 } else {
958 rate = ieee80211_media2rate(ime->ifm_media, IEEE80211_T_DS);
959 if (rate == 0)
960 return EINVAL;
961 for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
962 if ((ic->ic_sup_rates[i] & IEEE80211_RATE_VAL) == rate)
963 break;
964 }
965 if (i == IEEE80211_RATE_SIZE)
966 return EINVAL;
967 }
968 if (ic->ic_fixed_rate != i) {
969 ic->ic_fixed_rate = i;
970 error = ENETRESET;
971 }
972
973 if ((ime->ifm_media & IFM_IEEE80211_ADHOC) &&
974 (ime->ifm_media & IFM_FLAG0))
975 newmode = IEEE80211_M_AHDEMO;
976 else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
977 newmode = IEEE80211_M_IBSS;
978 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
979 newmode = IEEE80211_M_HOSTAP;
980 else
981 newmode = IEEE80211_M_STA;
982 if (ic->ic_opmode != newmode) {
983 ic->ic_opmode = newmode;
984 error = ENETRESET;
985 }
986 if (error == ENETRESET) {
987 if (sc->sc_enabled)
988 error = wi_init(ifp);
989 else
990 error = 0;
991 }
992 ifp->if_baudrate = ifmedia_baudrate(sc->sc_media.ifm_cur->ifm_media);
993
994 return error;
995 }
996
997 static void
998 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
999 {
1000 struct wi_softc *sc = ifp->if_softc;
1001 struct ieee80211com *ic = &sc->sc_ic;
1002 u_int16_t val;
1003 int rate, len;
1004
1005 if (sc->sc_enabled == 0) {
1006 imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
1007 imr->ifm_status = 0;
1008 return;
1009 }
1010
1011 imr->ifm_status = IFM_AVALID;
1012 imr->ifm_active = IFM_IEEE80211;
1013 if (ic->ic_state == IEEE80211_S_RUN)
1014 imr->ifm_status |= IFM_ACTIVE;
1015 if (ic->ic_fixed_rate != -1)
1016 rate = ic->ic_sup_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
1017 else {
1018 len = sizeof(val);
1019 if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) != 0)
1020 rate = 0;
1021 else {
1022 /* convert to 802.11 rate */
1023 rate = val * 2;
1024 if (sc->sc_firmware_type == WI_LUCENT) {
1025 if (rate == 10)
1026 rate = 11; /* 5.5Mbps */
1027 } else {
1028 if (rate == 4*2)
1029 rate = 11; /* 5.5Mbps */
1030 else if (rate == 8*2)
1031 rate = 22; /* 11Mbps */
1032 }
1033 }
1034 }
1035 imr->ifm_active |= ieee80211_rate2media(rate, IEEE80211_T_DS);
1036 switch (ic->ic_opmode) {
1037 case IEEE80211_M_STA:
1038 break;
1039 case IEEE80211_M_IBSS:
1040 imr->ifm_active |= IFM_IEEE80211_ADHOC;
1041 break;
1042 case IEEE80211_M_AHDEMO:
1043 imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1044 break;
1045 case IEEE80211_M_HOSTAP:
1046 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1047 break;
1048 }
1049 }
1050
1051 static void
1052 wi_rx_intr(struct wi_softc *sc)
1053 {
1054 struct ieee80211com *ic = &sc->sc_ic;
1055 struct ifnet *ifp = &ic->ic_if;
1056 struct wi_frame frmhdr;
1057 struct mbuf *m;
1058 struct ieee80211_frame *wh;
1059 int fid, len, off, rssi;
1060 u_int16_t status;
1061 u_int32_t rstamp;
1062
1063 fid = CSR_READ_2(sc, WI_RX_FID);
1064
1065 /* First read in the frame header */
1066 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1067 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1068 ifp->if_ierrors++;
1069 DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1070 return;
1071 }
1072
1073 /*
1074 * Drop undecryptable or packets with receive errors here
1075 */
1076 status = le16toh(frmhdr.wi_status);
1077 if (status & WI_STAT_ERRSTAT) {
1078 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1079 ifp->if_ierrors++;
1080 DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1081 return;
1082 }
1083 rssi = frmhdr.wi_rx_signal;
1084 rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1085 le16toh(frmhdr.wi_rx_tstamp1);
1086
1087 len = le16toh(frmhdr.wi_dat_len);
1088 off = ALIGN(sizeof(struct ieee80211_frame));
1089
1090 MGETHDR(m, M_DONTWAIT, MT_DATA);
1091 if (m == NULL) {
1092 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1093 ifp->if_ierrors++;
1094 DPRINTF(("wi_rx_intr: MGET failed\n"));
1095 return;
1096 }
1097 if (off + len > MHLEN) {
1098 MCLGET(m, M_DONTWAIT);
1099 if ((m->m_flags & M_EXT) == 0) {
1100 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1101 m_freem(m);
1102 ifp->if_ierrors++;
1103 DPRINTF(("wi_rx_intr: MCLGET failed\n"));
1104 return;
1105 }
1106 }
1107
1108 m->m_data += off - sizeof(struct ieee80211_frame);
1109 memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1110 wi_read_bap(sc, fid, sizeof(frmhdr),
1111 m->m_data + sizeof(struct ieee80211_frame), len);
1112 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1113 m->m_pkthdr.rcvif = ifp;
1114
1115 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1116
1117 #if NBPFILTER > 0
1118 if (sc->sc_drvbpf) {
1119 struct mbuf mb;
1120
1121 M_COPY_PKTHDR(&mb, m);
1122 mb.m_data = (caddr_t)&frmhdr;
1123 mb.m_len = sizeof(frmhdr);
1124 mb.m_next = m;
1125 mb.m_pkthdr.len += mb.m_len;
1126 bpf_mtap(sc->sc_drvbpf, &mb);
1127 }
1128 #endif
1129 wh = mtod(m, struct ieee80211_frame *);
1130 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1131 /*
1132 * WEP is decrypted by hardware. Clear WEP bit
1133 * header for ieee80211_input().
1134 */
1135 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1136 }
1137 ieee80211_input(ifp, m, rssi, rstamp);
1138 }
1139
1140 static void
1141 wi_tx_intr(struct wi_softc *sc)
1142 {
1143 struct ieee80211com *ic = &sc->sc_ic;
1144 struct ifnet *ifp = &ic->ic_if;
1145 int fid, cur;
1146
1147 fid = CSR_READ_2(sc, WI_ALLOC_FID);
1148 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1149
1150 cur = sc->sc_txcur;
1151 if (sc->sc_txd[cur].d_fid != fid) {
1152 printf("%s: bad alloc %x != %x, cur %d nxt %d\n",
1153 sc->sc_dev.dv_xname, fid, sc->sc_txd[cur].d_fid, cur,
1154 sc->sc_txnext);
1155 return;
1156 }
1157 sc->sc_tx_timer = 0;
1158 sc->sc_txd[cur].d_len = 0;
1159 sc->sc_txcur = cur = (cur + 1) % WI_NTXBUF;
1160 if (sc->sc_txd[cur].d_len == 0)
1161 ifp->if_flags &= ~IFF_OACTIVE;
1162 else {
1163 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1164 0, 0)) {
1165 printf("%s: xmit failed\n", sc->sc_dev.dv_xname);
1166 sc->sc_txd[cur].d_len = 0;
1167 } else {
1168 sc->sc_tx_timer = 5;
1169 ifp->if_timer = 1;
1170 }
1171 }
1172 }
1173
1174 static void
1175 wi_info_intr(struct wi_softc *sc)
1176 {
1177 struct ieee80211com *ic = &sc->sc_ic;
1178 struct ifnet *ifp = &ic->ic_if;
1179 int i, fid, len, off;
1180 u_int16_t ltbuf[2];
1181 u_int16_t stat;
1182 u_int32_t *ptr;
1183
1184 fid = CSR_READ_2(sc, WI_INFO_FID);
1185 wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
1186
1187 switch (le16toh(ltbuf[1])) {
1188
1189 case WI_INFO_LINK_STAT:
1190 wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1191 DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1192 switch (le16toh(stat)) {
1193 case CONNECTED:
1194 if (ic->ic_state == IEEE80211_S_RUN)
1195 break;
1196 /* FALLTHROUGH */
1197 case AP_CHANGE:
1198 ieee80211_new_state(ifp, IEEE80211_S_RUN, -1);
1199 break;
1200 case AP_IN_RANGE:
1201 break;
1202 case AP_OUT_OF_RANGE:
1203 if (sc->sc_firmware_type == WI_SYMBOL &&
1204 sc->sc_scan_timer > 0) {
1205 if (wi_cmd(sc, WI_CMD_INQUIRE,
1206 WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
1207 sc->sc_scan_timer = 0;
1208 break;
1209 }
1210 /* FALLTHROUGH */
1211 case DISCONNECTED:
1212 case ASSOC_FAILED:
1213 if (ic->ic_opmode == IEEE80211_M_STA)
1214 ieee80211_new_state(ifp, IEEE80211_S_INIT, -1);
1215 break;
1216 }
1217 break;
1218
1219 case WI_INFO_COUNTERS:
1220 /* some card versions have a larger stats structure */
1221 len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1222 ptr = (u_int32_t *)&sc->sc_stats;
1223 off = sizeof(ltbuf);
1224 for (i = 0; i < len; i++, off += 2, ptr++) {
1225 wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1226 #ifdef WI_HERMES_STATS_WAR
1227 if (stat & 0xf000)
1228 stat = ~stat;
1229 #endif
1230 *ptr += stat;
1231 }
1232 ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1233 sc->sc_stats.wi_tx_multi_retries +
1234 sc->sc_stats.wi_tx_retry_limit;
1235 break;
1236
1237 case WI_INFO_SCAN_RESULTS:
1238 case WI_INFO_HOST_SCAN_RESULTS:
1239 wi_scan_result(sc, fid, le16toh(ltbuf[0]));
1240 break;
1241
1242 default:
1243 DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1244 le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1245 break;
1246 }
1247 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1248 }
1249
1250 /*
1251 * Allocate a region of memory inside the NIC and zero
1252 * it out.
1253 */
1254 static int
1255 wi_write_multi(struct wi_softc *sc)
1256 {
1257 struct ifnet *ifp = &sc->sc_ic.ic_if;
1258 int n = 0;
1259 struct wi_mcast mlist;
1260 struct ether_multi *enm;
1261 struct ether_multistep estep;
1262
1263 if ((ifp->if_flags & IFF_PROMISC) != 0) {
1264 allmulti:
1265 ifp->if_flags |= IFF_ALLMULTI;
1266 memset(&mlist, 0, sizeof(mlist));
1267 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1268 sizeof(mlist));
1269 }
1270
1271 n = 0;
1272 ETHER_FIRST_MULTI(estep, &sc->sc_ic.ic_ec, enm);
1273 while (enm != NULL) {
1274 /* Punt on ranges or too many multicast addresses. */
1275 if (!IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi) ||
1276 n >= sizeof(mlist) / sizeof(mlist.wi_mcast[0]))
1277 goto allmulti;
1278
1279 IEEE80211_ADDR_COPY(&mlist.wi_mcast[n], enm->enm_addrlo);
1280 n++;
1281 ETHER_NEXT_MULTI(estep, enm);
1282 }
1283 ifp->if_flags &= ~IFF_ALLMULTI;
1284 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1285 IEEE80211_ADDR_LEN * n);
1286 }
1287
1288
1289 static void
1290 wi_read_nicid(sc)
1291 struct wi_softc *sc;
1292 {
1293 struct wi_card_ident *id;
1294 char *p;
1295 int len;
1296 u_int16_t ver[4];
1297
1298 /* getting chip identity */
1299 memset(ver, 0, sizeof(ver));
1300 len = sizeof(ver);
1301 wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1302 printf("%s: using ", sc->sc_dev.dv_xname);
1303 DPRINTF2(("wi_read_nicid: CARD_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3])));
1304
1305 sc->sc_firmware_type = WI_NOTYPE;
1306 for (id = wi_card_ident; id->card_name != NULL; id++) {
1307 if (le16toh(ver[0]) == id->card_id) {
1308 printf("%s", id->card_name);
1309 sc->sc_firmware_type = id->firm_type;
1310 break;
1311 }
1312 }
1313 if (sc->sc_firmware_type == WI_NOTYPE) {
1314 if (le16toh(ver[0]) & 0x8000) {
1315 printf("Unknown PRISM2 chip");
1316 sc->sc_firmware_type = WI_INTERSIL;
1317 } else {
1318 printf("Unknown Lucent chip");
1319 sc->sc_firmware_type = WI_LUCENT;
1320 }
1321 }
1322
1323 /* get primary firmware version (Only Prism chips) */
1324 if (sc->sc_firmware_type != WI_LUCENT) {
1325 memset(ver, 0, sizeof(ver));
1326 len = sizeof(ver);
1327 wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1328 sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1329 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1330 DPRINTF2(("wi_read_nicid: PRI_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3])));
1331 }
1332
1333 /* get station firmware version */
1334 memset(ver, 0, sizeof(ver));
1335 len = sizeof(ver);
1336 wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1337 sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1338 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1339 DPRINTF2(("wi_read_nicid: STA_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3])));
1340 if (sc->sc_firmware_type == WI_INTERSIL &&
1341 (sc->sc_sta_firmware_ver == 10102 ||
1342 sc->sc_sta_firmware_ver == 20102)) {
1343 char ident[12];
1344 memset(ident, 0, sizeof(ident));
1345 len = sizeof(ident);
1346 /* value should be the format like "V2.00-11" */
1347 if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1348 *(p = (char *)ident) >= 'A' &&
1349 p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1350 sc->sc_firmware_type = WI_SYMBOL;
1351 sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1352 (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1353 (p[6] - '0') * 10 + (p[7] - '0');
1354 }
1355 DPRINTF2(("wi_read_nicid: SYMBOL_ID: %x %x %x %x\n", le16toh(ident[0]), le16toh(ident[1]), le16toh(ident[2]), le16toh(ident[3])));
1356 }
1357
1358 printf("\n%s: %s Firmware: ", sc->sc_dev.dv_xname,
1359 sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
1360 (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
1361 if (sc->sc_firmware_type != WI_LUCENT) /* XXX */
1362 printf("Primary (%u.%u.%u), ",
1363 sc->sc_pri_firmware_ver / 10000,
1364 (sc->sc_pri_firmware_ver % 10000) / 100,
1365 sc->sc_pri_firmware_ver % 100);
1366 printf("Station (%u.%u.%u)\n",
1367 sc->sc_sta_firmware_ver / 10000,
1368 (sc->sc_sta_firmware_ver % 10000) / 100,
1369 sc->sc_sta_firmware_ver % 100);
1370 }
1371
1372 static int
1373 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1374 {
1375 struct wi_ssid ssid;
1376
1377 if (buflen > IEEE80211_NWID_LEN)
1378 return ENOBUFS;
1379 memset(&ssid, 0, sizeof(ssid));
1380 ssid.wi_len = htole16(buflen);
1381 memcpy(ssid.wi_ssid, buf, buflen);
1382 return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1383 }
1384
1385 static int
1386 wi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
1387 {
1388 struct wi_softc *sc = ifp->if_softc;
1389 struct ieee80211com *ic = &sc->sc_ic;
1390 struct ifreq *ifr = (struct ifreq *)data;
1391 struct wi_req wreq;
1392 int len, n, error;
1393
1394 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1395 if (error)
1396 return error;
1397 len = (wreq.wi_len - 1) * 2;
1398 if (len < sizeof(u_int16_t))
1399 return ENOSPC;
1400 if (len > sizeof(wreq.wi_val))
1401 len = sizeof(wreq.wi_val);
1402
1403 switch (wreq.wi_type) {
1404
1405 case WI_RID_IFACE_STATS:
1406 memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
1407 if (len < sizeof(sc->sc_stats))
1408 error = ENOSPC;
1409 else
1410 len = sizeof(sc->sc_stats);
1411 break;
1412
1413 case WI_RID_ENCRYPTION:
1414 case WI_RID_TX_CRYPT_KEY:
1415 case WI_RID_DEFLT_CRYPT_KEYS:
1416 return ieee80211_cfgget(ifp, cmd, data);
1417
1418 case WI_RID_TX_RATE:
1419 if (ic->ic_fixed_rate < 0)
1420 wreq.wi_val[0] = htole16(3); /*XXX*/
1421 else
1422 wreq.wi_val[0] = htole16(
1423 (ic->ic_sup_rates[ic->ic_fixed_rate] &
1424 IEEE80211_RATE_VAL) / 2);
1425 len = sizeof(u_int16_t);
1426 break;
1427
1428 case WI_RID_MICROWAVE_OVEN:
1429 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
1430 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1431 &len);
1432 break;
1433 }
1434 wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
1435 len = sizeof(u_int16_t);
1436 break;
1437
1438 case WI_RID_ROAMING_MODE:
1439 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
1440 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1441 &len);
1442 break;
1443 }
1444 wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
1445 len = sizeof(u_int16_t);
1446 break;
1447
1448 case WI_RID_SYSTEM_SCALE:
1449 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
1450 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1451 &len);
1452 break;
1453 }
1454 wreq.wi_val[0] = htole16(sc->sc_system_scale);
1455 len = sizeof(u_int16_t);
1456 break;
1457
1458 case WI_RID_READ_APS:
1459 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1460 return ieee80211_cfgget(ifp, cmd, data);
1461 if (sc->sc_scan_timer > 0) {
1462 error = EINPROGRESS;
1463 break;
1464 }
1465 n = sc->sc_naps;
1466 if (len < sizeof(n)) {
1467 error = ENOSPC;
1468 break;
1469 }
1470 if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
1471 n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
1472 len = sizeof(n) + sizeof(struct wi_apinfo) * n;
1473 memcpy(wreq.wi_val, &n, sizeof(n));
1474 memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
1475 sizeof(struct wi_apinfo) * n);
1476 break;
1477
1478 default:
1479 if (sc->sc_enabled) {
1480 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1481 &len);
1482 break;
1483 }
1484 switch (wreq.wi_type) {
1485 case WI_RID_MAX_DATALEN:
1486 wreq.wi_val[0] = htole16(sc->sc_max_datalen);
1487 len = sizeof(u_int16_t);
1488 break;
1489 case WI_RID_RTS_THRESH:
1490 wreq.wi_val[0] = htole16(sc->sc_rts_thresh);
1491 len = sizeof(u_int16_t);
1492 break;
1493 case WI_RID_CNFAUTHMODE:
1494 wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
1495 len = sizeof(u_int16_t);
1496 break;
1497 case WI_RID_NODENAME:
1498 if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
1499 error = ENOSPC;
1500 break;
1501 }
1502 len = sc->sc_nodelen + sizeof(u_int16_t);
1503 wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
1504 memcpy(&wreq.wi_val[1], sc->sc_nodename,
1505 sc->sc_nodelen);
1506 break;
1507 default:
1508 return ieee80211_cfgget(ifp, cmd, data);
1509 }
1510 break;
1511 }
1512 if (error)
1513 return error;
1514 wreq.wi_len = (len + 1) / 2 + 1;
1515 return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
1516 }
1517
1518 static int
1519 wi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
1520 {
1521 struct wi_softc *sc = ifp->if_softc;
1522 struct ieee80211com *ic = &sc->sc_ic;
1523 struct ifreq *ifr = (struct ifreq *)data;
1524 struct wi_req wreq;
1525 struct mbuf *m;
1526 int i, len, error;
1527
1528 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1529 if (error)
1530 return error;
1531 len = (wreq.wi_len - 1) * 2;
1532 switch (wreq.wi_type) {
1533 case WI_RID_NODENAME:
1534 if (le16toh(wreq.wi_val[0]) * 2 > len ||
1535 le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
1536 error = ENOSPC;
1537 break;
1538 }
1539 if (sc->sc_enabled) {
1540 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
1541 len);
1542 if (error)
1543 break;
1544 }
1545 sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
1546 memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen);
1547 break;
1548
1549 case WI_RID_MICROWAVE_OVEN:
1550 case WI_RID_ROAMING_MODE:
1551 case WI_RID_SYSTEM_SCALE:
1552 if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
1553 (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
1554 break;
1555 if (wreq.wi_type == WI_RID_ROAMING_MODE &&
1556 (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
1557 break;
1558 if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
1559 (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
1560 break;
1561 /* FALLTHROUGH */
1562 case WI_RID_RTS_THRESH:
1563 case WI_RID_CNFAUTHMODE:
1564 case WI_RID_MAX_DATALEN:
1565 if (sc->sc_enabled) {
1566 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
1567 sizeof(u_int16_t));
1568 if (error)
1569 break;
1570 }
1571 switch (wreq.wi_type) {
1572 case WI_RID_RTS_THRESH:
1573 sc->sc_rts_thresh = le16toh(wreq.wi_val[0]);
1574 break;
1575 case WI_RID_MICROWAVE_OVEN:
1576 sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
1577 break;
1578 case WI_RID_ROAMING_MODE:
1579 sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
1580 break;
1581 case WI_RID_SYSTEM_SCALE:
1582 sc->sc_system_scale = le16toh(wreq.wi_val[0]);
1583 break;
1584 case WI_RID_CNFAUTHMODE:
1585 sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
1586 break;
1587 case WI_RID_MAX_DATALEN:
1588 sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
1589 break;
1590 }
1591 break;
1592
1593 case WI_RID_TX_RATE:
1594 switch (le16toh(wreq.wi_val[0])) {
1595 case 3:
1596 ic->ic_fixed_rate = -1;
1597 break;
1598 default:
1599 for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
1600 if ((ic->ic_sup_rates[i] & IEEE80211_RATE_VAL)
1601 / 2 == le16toh(wreq.wi_val[0]))
1602 break;
1603 }
1604 if (i == IEEE80211_RATE_SIZE)
1605 return EINVAL;
1606 ic->ic_fixed_rate = i;
1607 }
1608 if (sc->sc_enabled)
1609 error = wi_write_txrate(sc);
1610 break;
1611
1612 case WI_RID_SCAN_APS:
1613 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
1614 error = wi_scan_ap(sc);
1615 break;
1616
1617 case WI_RID_MGMT_XMIT:
1618 if (!sc->sc_enabled) {
1619 error = ENETDOWN;
1620 break;
1621 }
1622 if (ic->ic_mgtq.ifq_len > 5) {
1623 error = EAGAIN;
1624 break;
1625 }
1626 /* XXX wi_len looks in u_int8_t, not in u_int16_t */
1627 m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL);
1628 if (m == NULL) {
1629 error = ENOMEM;
1630 break;
1631 }
1632 IF_ENQUEUE(&ic->ic_mgtq, m);
1633 break;
1634
1635 default:
1636 if (sc->sc_enabled) {
1637 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
1638 len);
1639 if (error)
1640 break;
1641 }
1642 error = ieee80211_cfgset(ifp, cmd, data);
1643 break;
1644 }
1645 return error;
1646 }
1647
1648 static int
1649 wi_write_txrate(struct wi_softc *sc)
1650 {
1651 struct ieee80211com *ic = &sc->sc_ic;
1652 int i;
1653 u_int16_t rate;
1654
1655 if (ic->ic_fixed_rate < 0)
1656 rate = 0; /* auto */
1657 else
1658 rate = (ic->ic_sup_rates[ic->ic_fixed_rate] &
1659 IEEE80211_RATE_VAL) / 2;
1660
1661 /* rate: 0, 1, 2, 5, 11 */
1662
1663 switch (sc->sc_firmware_type) {
1664 case WI_LUCENT:
1665 if (rate == 0)
1666 rate = 3; /* auto */
1667 break;
1668 default:
1669 for (i = 8; i > 0; i >>= 1) {
1670 if (rate >= i)
1671 break;
1672 }
1673 if (i == 0)
1674 rate = 0xf; /* auto */
1675 else
1676 rate = i;
1677 break;
1678 }
1679 return wi_write_val(sc, WI_RID_TX_RATE, rate);
1680 }
1681
1682 static int
1683 wi_write_wep(struct wi_softc *sc)
1684 {
1685 struct ieee80211com *ic = &sc->sc_ic;
1686 int error = 0;
1687 int i, keylen;
1688 u_int16_t val;
1689 struct wi_key wkey[IEEE80211_WEP_NKID];
1690
1691 switch (sc->sc_firmware_type) {
1692 case WI_LUCENT:
1693 val = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0;
1694 error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
1695 if (error)
1696 break;
1697 error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_wep_txkey);
1698 if (error)
1699 break;
1700 memset(wkey, 0, sizeof(wkey));
1701 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1702 keylen = ic->ic_nw_keys[i].wk_len;
1703 wkey[i].wi_keylen = htole16(keylen);
1704 memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
1705 keylen);
1706 }
1707 error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
1708 wkey, sizeof(wkey));
1709 break;
1710
1711 case WI_INTERSIL:
1712 case WI_SYMBOL:
1713 if (ic->ic_flags & IEEE80211_F_WEPON) {
1714 /*
1715 * ONLY HWB3163 EVAL-CARD Firmware version
1716 * less than 0.8 variant2
1717 *
1718 * If promiscuous mode disable, Prism2 chip
1719 * does not work with WEP .
1720 * It is under investigation for details.
1721 * (ichiro (at) netbsd.org)
1722 */
1723 if (sc->sc_firmware_type == WI_INTERSIL &&
1724 sc->sc_sta_firmware_ver < 802 ) {
1725 /* firm ver < 0.8 variant 2 */
1726 wi_write_val(sc, WI_RID_PROMISC, 1);
1727 }
1728 wi_write_val(sc, WI_RID_CNFAUTHMODE,
1729 sc->sc_cnfauthmode);
1730 val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
1731 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1732 val |= HOST_ENCRYPT;
1733 } else {
1734 wi_write_val(sc, WI_RID_CNFAUTHMODE, 1); /* open */
1735 val = HOST_ENCRYPT | HOST_DECRYPT;
1736 }
1737 error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
1738 if (error)
1739 break;
1740 error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
1741 ic->ic_wep_txkey);
1742 if (error)
1743 break;
1744 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1745 keylen = ic->ic_nw_keys[i].wk_len;
1746 if (keylen > IEEE80211_WEP_KEYLEN)
1747 keylen = 13; /* 104bit keys */
1748 else
1749 keylen = IEEE80211_WEP_KEYLEN;
1750 error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
1751 ic->ic_nw_keys[i].wk_key, keylen);
1752 if (error)
1753 break;
1754 }
1755 break;
1756 }
1757 return error;
1758 }
1759
1760 /* Must be called at proper protection level! */
1761 static int
1762 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
1763 {
1764 int i, status;
1765
1766 /* wait for the busy bit to clear */
1767 for (i = 0; ; i++) {
1768 if ((CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY) == 0)
1769 break;
1770 if (i == WI_TIMEOUT) {
1771 printf("%s: wi_cmd: BUSY did not clear, "
1772 "cmd=0x%x, prev=0x%x\n", sc->sc_dev.dv_xname,
1773 cmd, CSR_READ_2(sc, WI_COMMAND));
1774 return EIO;
1775 }
1776 DELAY(1);
1777 }
1778
1779 CSR_WRITE_2(sc, WI_PARAM0, val0);
1780 CSR_WRITE_2(sc, WI_PARAM1, val1);
1781 CSR_WRITE_2(sc, WI_PARAM2, val2);
1782 CSR_WRITE_2(sc, WI_COMMAND, cmd);
1783
1784 if (cmd == WI_CMD_INI) {
1785 /* XXX: should sleep here. */
1786 DELAY(100*1000);
1787 }
1788 /* wait for the cmd completed bit */
1789 for (i = 0; i < WI_TIMEOUT; i++) {
1790 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
1791 break;
1792 DELAY(1);
1793 }
1794
1795 status = CSR_READ_2(sc, WI_STATUS);
1796
1797 /* Ack the command */
1798 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
1799
1800 if (i == WI_TIMEOUT) {
1801 printf("%s: command timed out, cmd=0x%x, arg=0x%x\n",
1802 sc->sc_dev.dv_xname, cmd, val0);
1803 return ETIMEDOUT;
1804 }
1805
1806 if (status & WI_STAT_CMD_RESULT) {
1807 printf("%s: command failed, cmd=0x%x, arg=0x%x\n",
1808 sc->sc_dev.dv_xname, cmd, val0);
1809 return EIO;
1810 }
1811 return 0;
1812 }
1813
1814 static int
1815 wi_seek_bap(struct wi_softc *sc, int id, int off)
1816 {
1817 int i, status;
1818
1819 CSR_WRITE_2(sc, WI_SEL0, id);
1820 CSR_WRITE_2(sc, WI_OFF0, off);
1821
1822 for (i = 0; ; i++) {
1823 status = CSR_READ_2(sc, WI_OFF0);
1824 if ((status & WI_OFF_BUSY) == 0)
1825 break;
1826 if (i == WI_TIMEOUT) {
1827 printf("%s: timeout in wi_seek to %x/%x\n",
1828 sc->sc_dev.dv_xname, id, off);
1829 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
1830 return ETIMEDOUT;
1831 }
1832 DELAY(1);
1833 }
1834 if (status & WI_OFF_ERR) {
1835 printf("%s: failed in wi_seek to %x/%x\n",
1836 sc->sc_dev.dv_xname, id, off);
1837 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
1838 return EIO;
1839 }
1840 sc->sc_bap_id = id;
1841 sc->sc_bap_off = off;
1842 return 0;
1843 }
1844
1845 static int
1846 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
1847 {
1848 int error, cnt;
1849
1850 if (buflen == 0)
1851 return 0;
1852 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1853 if ((error = wi_seek_bap(sc, id, off)) != 0)
1854 return error;
1855 }
1856 cnt = (buflen + 1) / 2;
1857 CSR_READ_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
1858 sc->sc_bap_off += cnt * 2;
1859 return 0;
1860 }
1861
1862 static int
1863 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
1864 {
1865 int error, cnt;
1866
1867 if (buflen == 0)
1868 return 0;
1869
1870 #ifdef WI_HERMES_AUTOINC_WAR
1871 again:
1872 #endif
1873 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1874 if ((error = wi_seek_bap(sc, id, off)) != 0)
1875 return error;
1876 }
1877 cnt = (buflen + 1) / 2;
1878 CSR_WRITE_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
1879 sc->sc_bap_off += cnt * 2;
1880
1881 #ifdef WI_HERMES_AUTOINC_WAR
1882 /*
1883 * According to the comments in the HCF Light code, there is a bug
1884 * in the Hermes (or possibly in certain Hermes firmware revisions)
1885 * where the chip's internal autoincrement counter gets thrown off
1886 * during data writes: the autoincrement is missed, causing one
1887 * data word to be overwritten and subsequent words to be written to
1888 * the wrong memory locations. The end result is that we could end
1889 * up transmitting bogus frames without realizing it. The workaround
1890 * for this is to write a couple of extra guard words after the end
1891 * of the transfer, then attempt to read then back. If we fail to
1892 * locate the guard words where we expect them, we preform the
1893 * transfer over again.
1894 */
1895 if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
1896 CSR_WRITE_2(sc, WI_DATA0, 0x1234);
1897 CSR_WRITE_2(sc, WI_DATA0, 0x5678);
1898 wi_seek_bap(sc, id, sc->sc_bap_off);
1899 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
1900 if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
1901 CSR_READ_2(sc, WI_DATA0) != 0x5678) {
1902 printf("%s: detect auto increment bug, try again\n",
1903 sc->sc_dev.dv_xname);
1904 goto again;
1905 }
1906 }
1907 #endif
1908 return 0;
1909 }
1910
1911 static int
1912 wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
1913 {
1914 int i;
1915
1916 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
1917 printf("%s: failed to allocate %d bytes on NIC\n",
1918 sc->sc_dev.dv_xname, len);
1919 return ENOMEM;
1920 }
1921
1922 for (i = 0; i < WI_TIMEOUT; i++) {
1923 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
1924 break;
1925 if (i == WI_TIMEOUT) {
1926 printf("%s: timeout in alloc\n", sc->sc_dev.dv_xname);
1927 return ETIMEDOUT;
1928 }
1929 DELAY(1);
1930 }
1931 *idp = CSR_READ_2(sc, WI_ALLOC_FID);
1932 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1933 return 0;
1934 }
1935
1936 static int
1937 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
1938 {
1939 int error, len;
1940 u_int16_t ltbuf[2];
1941
1942 /* Tell the NIC to enter record read mode. */
1943 error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
1944 if (error)
1945 return error;
1946
1947 error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
1948 if (error)
1949 return error;
1950
1951 if (le16toh(ltbuf[1]) != rid) {
1952 printf("%s: record read mismatch, rid=%x, got=%x\n",
1953 sc->sc_dev.dv_xname, rid, le16toh(ltbuf[1]));
1954 return EIO;
1955 }
1956 len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */
1957 if (*buflenp < len) {
1958 printf("%s: record buffer is too small, "
1959 "rid=%x, size=%d, len=%d\n",
1960 sc->sc_dev.dv_xname, rid, *buflenp, len);
1961 return ENOSPC;
1962 }
1963 *buflenp = len;
1964 return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
1965 }
1966
1967 static int
1968 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
1969 {
1970 int error;
1971 u_int16_t ltbuf[2];
1972
1973 ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */
1974 ltbuf[1] = htole16(rid);
1975
1976 error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
1977 if (error)
1978 return error;
1979 error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
1980 if (error)
1981 return error;
1982
1983 return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
1984 }
1985
1986 static int
1987 wi_newstate(void *arg, enum ieee80211_state nstate)
1988 {
1989 struct wi_softc *sc = arg;
1990 struct ieee80211com *ic = &sc->sc_ic;
1991 struct ieee80211_node *ni = &ic->ic_bss;
1992 int i, buflen;
1993 u_int16_t val;
1994 struct wi_ssid ssid;
1995 enum ieee80211_state ostate;
1996 #ifdef WI_DEBUG
1997 static const char *stname[] =
1998 { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" };
1999 #endif /* WI_DEBUG */
2000
2001 ostate = ic->ic_state;
2002 DPRINTF(("wi_newstate: %s -> %s\n", stname[ostate], stname[nstate]));
2003
2004 ic->ic_state = nstate;
2005 switch (nstate) {
2006 case IEEE80211_S_INIT:
2007 ic->ic_flags &= ~IEEE80211_F_SIBSS;
2008 return 0;
2009
2010 case IEEE80211_S_RUN:
2011 buflen = IEEE80211_ADDR_LEN;
2012 wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen);
2013 IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
2014 buflen = sizeof(val);
2015 wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
2016 ni->ni_chan = le16toh(val);
2017
2018 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2019 ni->ni_esslen = ic->ic_des_esslen;
2020 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
2021 ni->ni_nrate = 0;
2022 for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
2023 if (ic->ic_sup_rates[i])
2024 ni->ni_rates[ni->ni_nrate++] =
2025 ic->ic_sup_rates[i];
2026 }
2027 ni->ni_intval = ic->ic_lintval;
2028 ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
2029 if (ic->ic_flags & IEEE80211_F_WEPON)
2030 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
2031 } else {
2032 buflen = sizeof(ssid);
2033 wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
2034 ni->ni_esslen = le16toh(ssid.wi_len);
2035 if (ni->ni_esslen > IEEE80211_NWID_LEN)
2036 ni->ni_esslen = IEEE80211_NWID_LEN; /*XXX*/
2037 memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
2038 }
2039 break;
2040
2041 case IEEE80211_S_SCAN:
2042 case IEEE80211_S_AUTH:
2043 case IEEE80211_S_ASSOC:
2044 break;
2045 }
2046
2047 /* skip standard ieee80211 handling */
2048 return EINPROGRESS;
2049 }
2050
2051 static int
2052 wi_scan_ap(struct wi_softc *sc)
2053 {
2054 int error = 0;
2055 u_int16_t val[2];
2056
2057 if (!sc->sc_enabled)
2058 return ENXIO;
2059 switch (sc->sc_firmware_type) {
2060 case WI_LUCENT:
2061 (void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
2062 break;
2063 case WI_INTERSIL:
2064 val[0] = 0x3fff; /* channel */
2065 val[1] = 0x000f; /* tx rate */
2066 error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
2067 break;
2068 case WI_SYMBOL:
2069 /*
2070 * XXX only supported on 3.x ?
2071 */
2072 val[0] = BSCAN_BCAST | BSCAN_ONETIME;
2073 error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
2074 val, sizeof(val[0]));
2075 break;
2076 }
2077 if (error == 0) {
2078 sc->sc_scan_timer = WI_SCAN_WAIT;
2079 sc->sc_ic.ic_if.if_timer = 1;
2080 DPRINTF(("wi_scan_ap: start scanning\n"));
2081 }
2082 return error;
2083 }
2084
2085 static void
2086 wi_scan_result(struct wi_softc *sc, int fid, int cnt)
2087 {
2088 int i, naps, off, szbuf;
2089 struct wi_scan_header ws_hdr; /* Prism2 header */
2090 struct wi_scan_data_p2 ws_dat; /* Prism2 scantable*/
2091 struct wi_apinfo *ap;
2092
2093 off = sizeof(u_int16_t) * 2;
2094 memset(&ws_hdr, 0, sizeof(ws_hdr));
2095 switch (sc->sc_firmware_type) {
2096 case WI_INTERSIL:
2097 wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
2098 off += sizeof(ws_hdr);
2099 szbuf = sizeof(struct wi_scan_data_p2);
2100 break;
2101 case WI_SYMBOL:
2102 szbuf = sizeof(struct wi_scan_data_p2) + 6;
2103 break;
2104 case WI_LUCENT:
2105 szbuf = sizeof(struct wi_scan_data);
2106 break;
2107 }
2108 naps = (cnt * 2 + 2 - off) / szbuf;
2109 if (naps > MAXAPINFO)
2110 naps = MAXAPINFO;
2111 sc->sc_naps = naps;
2112 /* Read Data */
2113 ap = sc->sc_aps;
2114 memset(&ws_dat, 0, sizeof(ws_dat));
2115 for (i = 0; i < naps; i++, ap++) {
2116 wi_read_bap(sc, fid, off, &ws_dat,
2117 (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
2118 DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off,
2119 ether_sprintf(ws_dat.wi_bssid)));
2120 off += szbuf;
2121 ap->scanreason = le16toh(ws_hdr.wi_reason);
2122 memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
2123 ap->channel = le16toh(ws_dat.wi_chid);
2124 ap->signal = le16toh(ws_dat.wi_signal);
2125 ap->noise = le16toh(ws_dat.wi_noise);
2126 ap->quality = ap->signal - ap->noise;
2127 ap->capinfo = le16toh(ws_dat.wi_capinfo);
2128 ap->interval = le16toh(ws_dat.wi_interval);
2129 ap->rate = le16toh(ws_dat.wi_rate);
2130 ap->namelen = le16toh(ws_dat.wi_namelen);
2131 if (ap->namelen > sizeof(ap->name))
2132 ap->namelen = sizeof(ap->name);
2133 memcpy(ap->name, ws_dat.wi_name, ap->namelen);
2134 }
2135 /* Done scanning */
2136 sc->sc_scan_timer = 0;
2137 DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
2138 }
2139