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