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