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