wi.c revision 1.126 1 /* $NetBSD: wi.c,v 1.126 2003/05/17 16:46:03 christos 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.126 2003/05/17 16:46:03 christos 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 *, u_int16_t, u_int16_t);
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 = 0; /* 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 /* common 802.11 configuration */
568 ic->ic_flags &= ~IEEE80211_F_IBSSON;
569 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
570 switch (ic->ic_opmode) {
571 case IEEE80211_M_STA:
572 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS);
573 break;
574 case IEEE80211_M_IBSS:
575 wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port);
576 ic->ic_flags |= IEEE80211_F_IBSSON;
577 sc->sc_syn_timer = 5;
578 ifp->if_timer = 1;
579 break;
580 case IEEE80211_M_AHDEMO:
581 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
582 break;
583 case IEEE80211_M_HOSTAP:
584 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP);
585 break;
586 case IEEE80211_M_MONITOR:
587 wi_cmd(sc, WI_CMD_TEST | (WI_TEST_MONITOR << 8), 0, 0, 0);
588 break;
589 }
590
591 /* Intersil interprets this RID as joining ESS even in IBSS mode */
592 if (sc->sc_firmware_type == WI_LUCENT &&
593 (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0)
594 wi_write_val(sc, WI_RID_CREATE_IBSS, 1);
595 else
596 wi_write_val(sc, WI_RID_CREATE_IBSS, 0);
597 wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
598 wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid,
599 ic->ic_des_esslen);
600 wi_write_val(sc, WI_RID_OWN_CHNL, ic->ic_ibss_chan);
601 wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen);
602 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
603 wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN);
604 wi_write_val(sc, WI_RID_PM_ENABLED,
605 (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
606
607 /* not yet common 802.11 configuration */
608 wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen);
609 wi_write_val(sc, WI_RID_RTS_THRESH, sc->sc_rts_thresh);
610 if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
611 wi_write_val(sc, WI_RID_FRAG_THRESH, sc->sc_frag_thresh);
612
613 /* driver specific 802.11 configuration */
614 if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)
615 wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale);
616 if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
617 wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode);
618 if (sc->sc_flags & WI_FLAGS_HAS_MOR)
619 wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven);
620 wi_write_txrate(sc);
621 wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen);
622
623 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
624 sc->sc_firmware_type == WI_INTERSIL) {
625 wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval);
626 wi_write_val(sc, WI_RID_BASIC_RATE, 0x03); /* 1, 2 */
627 wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */
628 wi_write_val(sc, WI_RID_DTIM_PERIOD, 1);
629 }
630
631 /*
632 * Initialize promisc mode.
633 * Being in the Host-AP mode causes a great
634 * deal of pain if primisc mode is set.
635 * Therefore we avoid confusing the firmware
636 * and always reset promisc mode in Host-AP
637 * mode. Host-AP sees all the packets anyway.
638 */
639 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
640 (ifp->if_flags & IFF_PROMISC) != 0) {
641 wi_write_val(sc, WI_RID_PROMISC, 1);
642 } else {
643 wi_write_val(sc, WI_RID_PROMISC, 0);
644 }
645
646 /* Configure WEP. */
647 if (ic->ic_flags & IEEE80211_F_HASWEP)
648 wi_write_wep(sc);
649
650 /* Set multicast filter. */
651 wi_write_multi(sc);
652
653 if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
654 sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
655 if (sc->sc_firmware_type == WI_SYMBOL)
656 sc->sc_buflen = 1585; /* XXX */
657 for (i = 0; i < WI_NTXBUF; i++) {
658 error = wi_alloc_fid(sc, sc->sc_buflen,
659 &sc->sc_txd[i].d_fid);
660 if (error) {
661 printf("%s: tx buffer allocation failed\n",
662 sc->sc_dev.dv_xname);
663 goto out;
664 }
665 DPRINTF2(("wi_init: txbuf %d allocated %x\n", i,
666 sc->sc_txd[i].d_fid));
667 sc->sc_txd[i].d_len = 0;
668 }
669 }
670 sc->sc_txcur = sc->sc_txnext = 0;
671
672 /* Enable desired port */
673 wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
674 ifp->if_flags |= IFF_RUNNING;
675 ifp->if_flags &= ~IFF_OACTIVE;
676 if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
677 ic->ic_opmode == IEEE80211_M_MONITOR ||
678 ic->ic_opmode == IEEE80211_M_HOSTAP)
679 wi_newstate(sc, IEEE80211_S_RUN);
680
681 /* Enable interrupts */
682 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
683
684 if (!wasenabled &&
685 ic->ic_opmode == IEEE80211_M_HOSTAP &&
686 sc->sc_firmware_type == WI_INTERSIL) {
687 /* XXX: some card need to be re-enabled for hostap */
688 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
689 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
690 }
691
692 if (ic->ic_opmode == IEEE80211_M_STA &&
693 ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
694 ic->ic_des_chan != IEEE80211_CHAN_ANY)) {
695 memset(&join, 0, sizeof(join));
696 if (ic->ic_flags & IEEE80211_F_DESBSSID)
697 IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
698 if (ic->ic_des_chan != IEEE80211_CHAN_ANY)
699 join.wi_chan = htole16(ic->ic_des_chan);
700 /* Lucent firmware does not support the JOIN RID. */
701 if (sc->sc_firmware_type != WI_LUCENT)
702 wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
703 }
704
705 out:
706 if (error) {
707 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
708 wi_stop(ifp, 0);
709 }
710 DPRINTF(("wi_init: return %d\n", error));
711 return error;
712 }
713
714 static void
715 wi_stop(struct ifnet *ifp, int disable)
716 {
717 struct wi_softc *sc = ifp->if_softc;
718 int s;
719
720 s = splnet();
721
722 DPRINTF(("wi_stop: disable %d\n", disable));
723 /* Writing registers of a detached wi provokes an
724 * MCHK on PowerPC, but disabling keeps wi from writing
725 * registers, so disable before doing anything else.
726 */
727 if (sc->sc_attached)
728 ieee80211_new_state(ifp, IEEE80211_S_INIT, -1);
729 if (sc->sc_enabled) {
730 CSR_WRITE_2(sc, WI_INT_EN, 0);
731 wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
732 if (disable) {
733 if (sc->sc_disable)
734 (*sc->sc_disable)(sc);
735 sc->sc_enabled = 0;
736 }
737 }
738 if (!sc->sc_attached)
739 ieee80211_new_state(ifp, IEEE80211_S_INIT, -1);
740
741 sc->sc_tx_timer = 0;
742 sc->sc_scan_timer = 0;
743 sc->sc_syn_timer = 0;
744 sc->sc_false_syns = 0;
745 sc->sc_naps = 0;
746 ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
747 ifp->if_timer = 0;
748
749 splx(s);
750 }
751
752 static void
753 wi_start(struct ifnet *ifp)
754 {
755 struct wi_softc *sc = ifp->if_softc;
756 struct ieee80211com *ic = &sc->sc_ic;
757 struct ieee80211_node *ni;
758 struct ieee80211_frame *wh;
759 struct mbuf *m0;
760 struct wi_frame frmhdr;
761 int cur, fid, off;
762
763 if (ifp->if_flags & IFF_OACTIVE)
764 return;
765 if (sc->sc_flags & WI_FLAGS_OUTRANGE)
766 return;
767
768 memset(&frmhdr, 0, sizeof(frmhdr));
769 cur = sc->sc_txnext;
770 for (;;) {
771 if (!IF_IS_EMPTY(&ic->ic_mgtq)) {
772 if (sc->sc_txd[cur].d_len != 0) {
773 ifp->if_flags |= IFF_OACTIVE;
774 break;
775 }
776 IF_DEQUEUE(&ic->ic_mgtq, m0);
777 m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
778 (caddr_t)&frmhdr.wi_ehdr);
779 frmhdr.wi_ehdr.ether_type = 0;
780 wh = mtod(m0, struct ieee80211_frame *);
781 } else if (!IF_IS_EMPTY(&ic->ic_pwrsaveq)) {
782 struct llc *llc;
783
784 /*
785 * Should these packets be processed after the
786 * regular packets or before? Since they are being
787 * probed for, they are probably less time critical
788 * than other packets, but, on the other hand,
789 * we want the power saving nodes to go back to
790 * sleep as quickly as possible to save power...
791 */
792
793 if (ic->ic_state != IEEE80211_S_RUN)
794 break;
795
796 if (sc->sc_txd[cur].d_len != 0) {
797 ifp->if_flags |= IFF_OACTIVE;
798 break;
799 }
800 IF_DEQUEUE(&ic->ic_pwrsaveq, m0);
801 wh = mtod(m0, struct ieee80211_frame *);
802 llc = (struct llc *) (wh + 1);
803 m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
804 (caddr_t)&frmhdr.wi_ehdr);
805 frmhdr.wi_ehdr.ether_type = llc->llc_snap.ether_type;
806 } else {
807 if (ic->ic_state != IEEE80211_S_RUN)
808 break;
809 IFQ_POLL(&ifp->if_snd, m0);
810 if (m0 == NULL)
811 break;
812 if (sc->sc_txd[cur].d_len != 0) {
813 ifp->if_flags |= IFF_OACTIVE;
814 break;
815 }
816 IFQ_DEQUEUE(&ifp->if_snd, m0);
817 ifp->if_opackets++;
818 m_copydata(m0, 0, ETHER_HDR_LEN,
819 (caddr_t)&frmhdr.wi_ehdr);
820 #if NBPFILTER > 0
821 if (ifp->if_bpf)
822 bpf_mtap(ifp->if_bpf, m0);
823 #endif
824
825 if ((m0 = ieee80211_encap(ifp, m0)) == NULL) {
826 ifp->if_oerrors++;
827 continue;
828 }
829 wh = mtod(m0, struct ieee80211_frame *);
830 if (ic->ic_flags & IEEE80211_F_WEPON)
831 wh->i_fc[1] |= IEEE80211_FC1_WEP;
832 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
833 !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
834 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
835 IEEE80211_FC0_TYPE_DATA) {
836 ni = ieee80211_find_node(ic, wh->i_addr1);
837 if (ni == NULL || ni->ni_associd == 0) {
838 m_freem(m0);
839 ifp->if_oerrors++;
840 continue;
841 }
842 if (ni->ni_pwrsave & IEEE80211_PS_SLEEP) {
843 ieee80211_pwrsave(ic, ni, m0);
844 continue;
845 }
846 }
847 }
848 #if NBPFILTER > 0
849 if (ic->ic_rawbpf)
850 bpf_mtap(ic->ic_rawbpf, m0);
851 #endif
852 frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
853 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
854 (wh->i_fc[1] & IEEE80211_FC1_WEP)) {
855 if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) {
856 ifp->if_oerrors++;
857 continue;
858 }
859 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
860 }
861 m_copydata(m0, 0, sizeof(struct ieee80211_frame),
862 (caddr_t)&frmhdr.wi_whdr);
863 m_adj(m0, sizeof(struct ieee80211_frame));
864 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
865 #if NBPFILTER > 0
866 if (sc->sc_drvbpf) {
867 struct mbuf mb;
868
869 M_COPY_PKTHDR(&mb, m0);
870 mb.m_data = (caddr_t)&frmhdr;
871 mb.m_len = sizeof(frmhdr);
872 mb.m_next = m0;
873 mb.m_pkthdr.len += mb.m_len;
874 bpf_mtap(sc->sc_drvbpf, &mb);
875 }
876 #endif
877 if (IFF_DUMPPKTS(ifp))
878 wi_dump_pkt(&frmhdr, ni, -1);
879 fid = sc->sc_txd[cur].d_fid;
880 off = sizeof(frmhdr);
881 if (wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0 ||
882 wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0) {
883 ifp->if_oerrors++;
884 m_freem(m0);
885 continue;
886 }
887 m_freem(m0);
888 sc->sc_txd[cur].d_len = off;
889 if (sc->sc_txcur == cur) {
890 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
891 printf("%s: xmit failed\n",
892 sc->sc_dev.dv_xname);
893 sc->sc_txd[cur].d_len = 0;
894 continue;
895 }
896 sc->sc_tx_timer = 5;
897 ifp->if_timer = 1;
898 }
899 sc->sc_txnext = cur = (cur + 1) % WI_NTXBUF;
900 }
901 }
902
903
904 static int
905 wi_reset(struct wi_softc *sc)
906 {
907 int i, error;
908
909 DPRINTF(("wi_reset\n"));
910
911 if (sc->sc_reset)
912 (*sc->sc_reset)(sc);
913
914 error = 0;
915 for (i = 0; i < 5; i++) {
916 DELAY(20*1000); /* XXX: way too long! */
917 if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
918 break;
919 }
920 if (error) {
921 printf("%s: init failed\n", sc->sc_dev.dv_xname);
922 return error;
923 }
924 CSR_WRITE_2(sc, WI_INT_EN, 0);
925 CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
926
927 /* Calibrate timer. */
928 wi_write_val(sc, WI_RID_TICK_TIME, 0);
929 return 0;
930 }
931
932 static void
933 wi_watchdog(struct ifnet *ifp)
934 {
935 struct wi_softc *sc = ifp->if_softc;
936
937 ifp->if_timer = 0;
938 if (!sc->sc_enabled)
939 return;
940
941 if (sc->sc_tx_timer) {
942 if (--sc->sc_tx_timer == 0) {
943 printf("%s: device timeout\n", ifp->if_xname);
944 ifp->if_oerrors++;
945 wi_init(ifp);
946 return;
947 }
948 ifp->if_timer = 1;
949 }
950
951 if (sc->sc_scan_timer) {
952 if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
953 sc->sc_firmware_type == WI_INTERSIL) {
954 DPRINTF(("wi_watchdog: inquire scan\n"));
955 wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
956 }
957 if (sc->sc_scan_timer)
958 ifp->if_timer = 1;
959 }
960
961 if (sc->sc_syn_timer) {
962 if (--sc->sc_syn_timer == 0) {
963 DPRINTF2(("%s: %d false syns\n",
964 sc->sc_dev.dv_xname, sc->sc_false_syns));
965 sc->sc_false_syns = 0;
966 ieee80211_new_state(ifp, IEEE80211_S_RUN, -1);
967 sc->sc_syn_timer = 5;
968 }
969 ifp->if_timer = 1;
970 }
971
972 /* TODO: rate control */
973 ieee80211_watchdog(ifp);
974 }
975
976 static int
977 wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
978 {
979 struct wi_softc *sc = ifp->if_softc;
980 struct ieee80211com *ic = &sc->sc_ic;
981 struct ifreq *ifr = (struct ifreq *)data;
982 int s, error = 0;
983
984 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
985 return ENXIO;
986
987 s = splnet();
988
989 switch (cmd) {
990 case SIOCSIFFLAGS:
991 /*
992 * Can't do promisc and hostap at the same time. If all that's
993 * changing is the promisc flag, try to short-circuit a call to
994 * wi_init() by just setting PROMISC in the hardware.
995 */
996 if (ifp->if_flags & IFF_UP) {
997 if (sc->sc_enabled) {
998 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
999 (ifp->if_flags & IFF_PROMISC) != 0)
1000 wi_write_val(sc, WI_RID_PROMISC, 1);
1001 else
1002 wi_write_val(sc, WI_RID_PROMISC, 0);
1003 } else
1004 error = wi_init(ifp);
1005 } else if (sc->sc_enabled)
1006 wi_stop(ifp, 1);
1007 break;
1008 case SIOCSIFMEDIA:
1009 case SIOCGIFMEDIA:
1010 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1011 break;
1012 case SIOCADDMULTI:
1013 case SIOCDELMULTI:
1014 error = (cmd == SIOCADDMULTI) ?
1015 ether_addmulti(ifr, &sc->sc_ic.ic_ec) :
1016 ether_delmulti(ifr, &sc->sc_ic.ic_ec);
1017 if (error == ENETRESET) {
1018 if (sc->sc_enabled) {
1019 /* do not rescan */
1020 error = wi_write_multi(sc);
1021 } else
1022 error = 0;
1023 }
1024 break;
1025 case SIOCGIFGENERIC:
1026 error = wi_get_cfg(ifp, cmd, data);
1027 break;
1028 case SIOCSIFGENERIC:
1029 error = suser(curproc->p_ucred, &curproc->p_acflag);
1030 if (error)
1031 break;
1032 error = wi_set_cfg(ifp, cmd, data);
1033 if (error == ENETRESET) {
1034 if (sc->sc_enabled)
1035 error = wi_init(ifp);
1036 else
1037 error = 0;
1038 }
1039 break;
1040 case SIOCS80211BSSID:
1041 if (sc->sc_firmware_type == WI_LUCENT) {
1042 error = ENODEV;
1043 break;
1044 }
1045 /* fall through */
1046 default:
1047 error = ieee80211_ioctl(ifp, cmd, data);
1048 if (error == ENETRESET) {
1049 if (sc->sc_enabled)
1050 error = wi_init(ifp);
1051 else
1052 error = 0;
1053 }
1054 break;
1055 }
1056 splx(s);
1057 return error;
1058 }
1059
1060 static int
1061 wi_media_change(struct ifnet *ifp)
1062 {
1063 struct wi_softc *sc = ifp->if_softc;
1064 struct ieee80211com *ic = &sc->sc_ic;
1065 struct ifmedia_entry *ime;
1066 enum ieee80211_opmode newmode;
1067 int i, rate, error = 0;
1068
1069 ime = sc->sc_media.ifm_cur;
1070 if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) {
1071 i = -1;
1072 } else {
1073 rate = ieee80211_media2rate(ime->ifm_media, IEEE80211_T_DS);
1074 if (rate == 0)
1075 return EINVAL;
1076 for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
1077 if ((ic->ic_sup_rates[i] & IEEE80211_RATE_VAL) == rate)
1078 break;
1079 }
1080 if (i == IEEE80211_RATE_SIZE)
1081 return EINVAL;
1082 }
1083 if (ic->ic_fixed_rate != i) {
1084 ic->ic_fixed_rate = i;
1085 error = ENETRESET;
1086 }
1087
1088 if ((ime->ifm_media & IFM_IEEE80211_ADHOC) &&
1089 (ime->ifm_media & IFM_FLAG0))
1090 newmode = IEEE80211_M_AHDEMO;
1091 else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
1092 newmode = IEEE80211_M_IBSS;
1093 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
1094 newmode = IEEE80211_M_HOSTAP;
1095 else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
1096 newmode = IEEE80211_M_MONITOR;
1097 else
1098 newmode = IEEE80211_M_STA;
1099 if (ic->ic_opmode != newmode) {
1100 ic->ic_opmode = newmode;
1101 error = ENETRESET;
1102 }
1103 if (error == ENETRESET) {
1104 if (sc->sc_enabled)
1105 error = wi_init(ifp);
1106 else
1107 error = 0;
1108 }
1109 ifp->if_baudrate = ifmedia_baudrate(sc->sc_media.ifm_cur->ifm_media);
1110
1111 return error;
1112 }
1113
1114 static void
1115 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1116 {
1117 struct wi_softc *sc = ifp->if_softc;
1118 struct ieee80211com *ic = &sc->sc_ic;
1119 u_int16_t val;
1120 int rate, len;
1121
1122 if (sc->sc_enabled == 0) {
1123 imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
1124 imr->ifm_status = 0;
1125 return;
1126 }
1127
1128 imr->ifm_status = IFM_AVALID;
1129 imr->ifm_active = IFM_IEEE80211;
1130 if (ic->ic_state == IEEE80211_S_RUN &&
1131 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
1132 imr->ifm_status |= IFM_ACTIVE;
1133 len = sizeof(val);
1134 if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) != 0)
1135 rate = 0;
1136 else {
1137 /* convert to 802.11 rate */
1138 rate = val * 2;
1139 if (sc->sc_firmware_type == WI_LUCENT) {
1140 if (rate == 10)
1141 rate = 11; /* 5.5Mbps */
1142 } else {
1143 if (rate == 4*2)
1144 rate = 11; /* 5.5Mbps */
1145 else if (rate == 8*2)
1146 rate = 22; /* 11Mbps */
1147 }
1148 }
1149 imr->ifm_active |= ieee80211_rate2media(rate, IEEE80211_T_DS);
1150 switch (ic->ic_opmode) {
1151 case IEEE80211_M_STA:
1152 break;
1153 case IEEE80211_M_IBSS:
1154 imr->ifm_active |= IFM_IEEE80211_ADHOC;
1155 break;
1156 case IEEE80211_M_AHDEMO:
1157 imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1158 break;
1159 case IEEE80211_M_HOSTAP:
1160 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1161 break;
1162 case IEEE80211_M_MONITOR:
1163 imr->ifm_active |= IFM_IEEE80211_MONITOR;
1164 break;
1165 }
1166 }
1167
1168 static void
1169 wi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1170 {
1171 struct ieee80211com *ic = &sc->sc_ic;
1172 struct ieee80211_node *ni = &ic->ic_bss;
1173 struct ifnet *ifp = &ic->ic_if;
1174
1175 if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1176 return;
1177
1178 DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid)));
1179 DPRINTF(("%s ?\n", ether_sprintf(new_bssid)));
1180
1181 /* In promiscuous mode, the BSSID field is not a reliable
1182 * indicator of the firmware's BSSID. Damp spurious
1183 * change-of-BSSID indications.
1184 */
1185 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1186 sc->sc_false_syns >= WI_MAX_FALSE_SYNS)
1187 return;
1188
1189 ieee80211_new_state(ifp, IEEE80211_S_RUN, -1);
1190 }
1191
1192 static void
1193 wi_rx_intr(struct wi_softc *sc)
1194 {
1195 struct ieee80211com *ic = &sc->sc_ic;
1196 struct ifnet *ifp = &ic->ic_if;
1197 struct wi_frame frmhdr;
1198 struct mbuf *m;
1199 struct ieee80211_frame *wh;
1200 int fid, len, off, rssi;
1201 u_int8_t dir;
1202 u_int16_t status;
1203 u_int32_t rstamp;
1204
1205 fid = CSR_READ_2(sc, WI_RX_FID);
1206
1207 /* First read in the frame header */
1208 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1209 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1210 ifp->if_ierrors++;
1211 DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1212 return;
1213 }
1214
1215 if (IFF_DUMPPKTS(ifp))
1216 wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal);
1217
1218 /*
1219 * Drop undecryptable or packets with receive errors here
1220 */
1221 status = le16toh(frmhdr.wi_status);
1222 if (status & WI_STAT_ERRSTAT) {
1223 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1224 ifp->if_ierrors++;
1225 DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1226 return;
1227 }
1228 rssi = frmhdr.wi_rx_signal;
1229 rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1230 le16toh(frmhdr.wi_rx_tstamp1);
1231
1232 len = le16toh(frmhdr.wi_dat_len);
1233 off = ALIGN(sizeof(struct ieee80211_frame));
1234
1235 /* Sometimes the PRISM2.x returns bogusly large frames. Except
1236 * in monitor mode, just throw them away.
1237 */
1238 if (off + len > MCLBYTES) {
1239 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1240 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1241 ifp->if_ierrors++;
1242 DPRINTF(("wi_rx_intr: oversized packet\n"));
1243 return;
1244 } else
1245 len = 0;
1246 }
1247
1248 MGETHDR(m, M_DONTWAIT, MT_DATA);
1249 if (m == NULL) {
1250 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1251 ifp->if_ierrors++;
1252 DPRINTF(("wi_rx_intr: MGET failed\n"));
1253 return;
1254 }
1255 if (off + len > MHLEN) {
1256 MCLGET(m, M_DONTWAIT);
1257 if ((m->m_flags & M_EXT) == 0) {
1258 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1259 m_freem(m);
1260 ifp->if_ierrors++;
1261 DPRINTF(("wi_rx_intr: MCLGET failed\n"));
1262 return;
1263 }
1264 }
1265
1266 m->m_data += off - sizeof(struct ieee80211_frame);
1267 memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1268 wi_read_bap(sc, fid, sizeof(frmhdr),
1269 m->m_data + sizeof(struct ieee80211_frame), len);
1270 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1271 m->m_pkthdr.rcvif = ifp;
1272
1273 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1274
1275 #if NBPFILTER > 0
1276 if (sc->sc_drvbpf) {
1277 struct mbuf mb;
1278
1279 M_COPY_PKTHDR(&mb, m);
1280 mb.m_data = (caddr_t)&frmhdr;
1281 frmhdr.wi_rx_signal -= sc->sc_dbm_adjust;
1282 frmhdr.wi_rx_silence -= sc->sc_dbm_adjust;
1283 mb.m_len = (char *)&frmhdr.wi_whdr - (char *)&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 curtxeps = 0;
1327 printf("%s: tx failed", sc->sc_dev.dv_xname);
1328 if (status & WI_TXSTAT_RET_ERR)
1329 printf(", retry limit exceeded");
1330 if (status & WI_TXSTAT_AGED_ERR)
1331 printf(", max transmit lifetime exceeded");
1332 if (status & WI_TXSTAT_DISCONNECT)
1333 printf(", port disconnected");
1334 if (status & WI_TXSTAT_FORM_ERR)
1335 printf(", invalid format (data len %u src %s)",
1336 le16toh(frmhdr.wi_dat_len),
1337 ether_sprintf(frmhdr.wi_ehdr.ether_shost));
1338 if (status & ~0xf)
1339 printf(", status=0x%x", status);
1340 printf("\n");
1341 }
1342 ifp->if_oerrors++;
1343 } else {
1344 DPRINTF(("port disconnected\n"));
1345 ifp->if_collisions++; /* XXX */
1346 }
1347 } else
1348 DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
1349 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1350 }
1351
1352 static void
1353 wi_tx_intr(struct wi_softc *sc)
1354 {
1355 struct ieee80211com *ic = &sc->sc_ic;
1356 struct ifnet *ifp = &ic->ic_if;
1357 int fid, cur;
1358
1359 fid = CSR_READ_2(sc, WI_ALLOC_FID);
1360 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1361
1362 cur = sc->sc_txcur;
1363 if (sc->sc_txd[cur].d_fid != fid) {
1364 printf("%s: bad alloc %x != %x, cur %d nxt %d\n",
1365 sc->sc_dev.dv_xname, fid, sc->sc_txd[cur].d_fid, cur,
1366 sc->sc_txnext);
1367 return;
1368 }
1369 sc->sc_tx_timer = 0;
1370 sc->sc_txd[cur].d_len = 0;
1371 sc->sc_txcur = cur = (cur + 1) % WI_NTXBUF;
1372 if (sc->sc_txd[cur].d_len == 0)
1373 ifp->if_flags &= ~IFF_OACTIVE;
1374 else {
1375 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1376 0, 0)) {
1377 printf("%s: xmit failed\n", sc->sc_dev.dv_xname);
1378 sc->sc_txd[cur].d_len = 0;
1379 } else {
1380 sc->sc_tx_timer = 5;
1381 ifp->if_timer = 1;
1382 }
1383 }
1384 }
1385
1386 static void
1387 wi_info_intr(struct wi_softc *sc)
1388 {
1389 struct ieee80211com *ic = &sc->sc_ic;
1390 struct ifnet *ifp = &ic->ic_if;
1391 int i, fid, len, off;
1392 u_int16_t ltbuf[2];
1393 u_int16_t stat;
1394 u_int32_t *ptr;
1395
1396 fid = CSR_READ_2(sc, WI_INFO_FID);
1397 wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
1398
1399 switch (le16toh(ltbuf[1])) {
1400
1401 case WI_INFO_LINK_STAT:
1402 wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1403 DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1404 switch (le16toh(stat)) {
1405 case CONNECTED:
1406 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1407 if (ic->ic_state == IEEE80211_S_RUN &&
1408 ic->ic_opmode != IEEE80211_M_IBSS)
1409 break;
1410 /* FALLTHROUGH */
1411 case AP_CHANGE:
1412 ieee80211_new_state(ifp, IEEE80211_S_RUN, -1);
1413 break;
1414 case AP_IN_RANGE:
1415 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1416 break;
1417 case AP_OUT_OF_RANGE:
1418 if (sc->sc_firmware_type == WI_SYMBOL &&
1419 sc->sc_scan_timer > 0) {
1420 if (wi_cmd(sc, WI_CMD_INQUIRE,
1421 WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
1422 sc->sc_scan_timer = 0;
1423 break;
1424 }
1425 if (ic->ic_opmode == IEEE80211_M_STA)
1426 sc->sc_flags |= WI_FLAGS_OUTRANGE;
1427 break;
1428 case DISCONNECTED:
1429 case ASSOC_FAILED:
1430 if (ic->ic_opmode == IEEE80211_M_STA)
1431 ieee80211_new_state(ifp, IEEE80211_S_INIT, -1);
1432 break;
1433 }
1434 break;
1435
1436 case WI_INFO_COUNTERS:
1437 /* some card versions have a larger stats structure */
1438 len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1439 ptr = (u_int32_t *)&sc->sc_stats;
1440 off = sizeof(ltbuf);
1441 for (i = 0; i < len; i++, off += 2, ptr++) {
1442 wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1443 #ifdef WI_HERMES_STATS_WAR
1444 if (stat & 0xf000)
1445 stat = ~stat;
1446 #endif
1447 *ptr += stat;
1448 }
1449 ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1450 sc->sc_stats.wi_tx_multi_retries +
1451 sc->sc_stats.wi_tx_retry_limit;
1452 break;
1453
1454 case WI_INFO_SCAN_RESULTS:
1455 case WI_INFO_HOST_SCAN_RESULTS:
1456 wi_scan_result(sc, fid, le16toh(ltbuf[0]));
1457 break;
1458
1459 default:
1460 DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1461 le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1462 break;
1463 }
1464 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1465 }
1466
1467 static int
1468 wi_write_multi(struct wi_softc *sc)
1469 {
1470 struct ifnet *ifp = &sc->sc_ic.ic_if;
1471 int n;
1472 struct wi_mcast mlist;
1473 struct ether_multi *enm;
1474 struct ether_multistep estep;
1475
1476 if ((ifp->if_flags & IFF_PROMISC) != 0) {
1477 allmulti:
1478 ifp->if_flags |= IFF_ALLMULTI;
1479 memset(&mlist, 0, sizeof(mlist));
1480 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1481 sizeof(mlist));
1482 }
1483
1484 n = 0;
1485 ETHER_FIRST_MULTI(estep, &sc->sc_ic.ic_ec, enm);
1486 while (enm != NULL) {
1487 /* Punt on ranges or too many multicast addresses. */
1488 if (!IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi) ||
1489 n >= sizeof(mlist) / sizeof(mlist.wi_mcast[0]))
1490 goto allmulti;
1491
1492 IEEE80211_ADDR_COPY(&mlist.wi_mcast[n], enm->enm_addrlo);
1493 n++;
1494 ETHER_NEXT_MULTI(estep, enm);
1495 }
1496 ifp->if_flags &= ~IFF_ALLMULTI;
1497 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1498 IEEE80211_ADDR_LEN * n);
1499 }
1500
1501
1502 static void
1503 wi_read_nicid(struct wi_softc *sc)
1504 {
1505 struct wi_card_ident *id;
1506 char *p;
1507 int len;
1508 u_int16_t ver[4];
1509
1510 /* getting chip identity */
1511 memset(ver, 0, sizeof(ver));
1512 len = sizeof(ver);
1513 wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1514 printf("%s: using ", sc->sc_dev.dv_xname);
1515 DPRINTF2(("wi_read_nicid: CARD_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3])));
1516
1517 sc->sc_firmware_type = WI_NOTYPE;
1518 for (id = wi_card_ident; id->card_name != NULL; id++) {
1519 if (le16toh(ver[0]) == id->card_id) {
1520 printf("%s", id->card_name);
1521 sc->sc_firmware_type = id->firm_type;
1522 break;
1523 }
1524 }
1525 if (sc->sc_firmware_type == WI_NOTYPE) {
1526 if (le16toh(ver[0]) & 0x8000) {
1527 printf("Unknown PRISM2 chip");
1528 sc->sc_firmware_type = WI_INTERSIL;
1529 } else {
1530 printf("Unknown Lucent chip");
1531 sc->sc_firmware_type = WI_LUCENT;
1532 }
1533 }
1534
1535 /* get primary firmware version (Only Prism chips) */
1536 if (sc->sc_firmware_type != WI_LUCENT) {
1537 memset(ver, 0, sizeof(ver));
1538 len = sizeof(ver);
1539 wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1540 sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1541 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1542 }
1543
1544 /* get station firmware version */
1545 memset(ver, 0, sizeof(ver));
1546 len = sizeof(ver);
1547 wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1548 sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1549 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1550 if (sc->sc_firmware_type == WI_INTERSIL &&
1551 (sc->sc_sta_firmware_ver == 10102 ||
1552 sc->sc_sta_firmware_ver == 20102)) {
1553 char ident[12];
1554 memset(ident, 0, sizeof(ident));
1555 len = sizeof(ident);
1556 /* value should be the format like "V2.00-11" */
1557 if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1558 *(p = (char *)ident) >= 'A' &&
1559 p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1560 sc->sc_firmware_type = WI_SYMBOL;
1561 sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1562 (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1563 (p[6] - '0') * 10 + (p[7] - '0');
1564 }
1565 }
1566
1567 printf("\n%s: %s Firmware: ", sc->sc_dev.dv_xname,
1568 sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
1569 (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
1570 if (sc->sc_firmware_type != WI_LUCENT) /* XXX */
1571 printf("Primary (%u.%u.%u), ",
1572 sc->sc_pri_firmware_ver / 10000,
1573 (sc->sc_pri_firmware_ver % 10000) / 100,
1574 sc->sc_pri_firmware_ver % 100);
1575 printf("Station (%u.%u.%u)\n",
1576 sc->sc_sta_firmware_ver / 10000,
1577 (sc->sc_sta_firmware_ver % 10000) / 100,
1578 sc->sc_sta_firmware_ver % 100);
1579 }
1580
1581 static int
1582 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1583 {
1584 struct wi_ssid ssid;
1585
1586 if (buflen > IEEE80211_NWID_LEN)
1587 return ENOBUFS;
1588 memset(&ssid, 0, sizeof(ssid));
1589 ssid.wi_len = htole16(buflen);
1590 memcpy(ssid.wi_ssid, buf, buflen);
1591 return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1592 }
1593
1594 static int
1595 wi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
1596 {
1597 struct wi_softc *sc = ifp->if_softc;
1598 struct ieee80211com *ic = &sc->sc_ic;
1599 struct ifreq *ifr = (struct ifreq *)data;
1600 struct wi_req wreq;
1601 int len, n, error;
1602
1603 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1604 if (error)
1605 return error;
1606 len = (wreq.wi_len - 1) * 2;
1607 if (len < sizeof(u_int16_t))
1608 return ENOSPC;
1609 if (len > sizeof(wreq.wi_val))
1610 len = sizeof(wreq.wi_val);
1611
1612 switch (wreq.wi_type) {
1613
1614 case WI_RID_IFACE_STATS:
1615 memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
1616 if (len < sizeof(sc->sc_stats))
1617 error = ENOSPC;
1618 else
1619 len = sizeof(sc->sc_stats);
1620 break;
1621
1622 case WI_RID_ENCRYPTION:
1623 case WI_RID_TX_CRYPT_KEY:
1624 case WI_RID_DEFLT_CRYPT_KEYS:
1625 case WI_RID_TX_RATE:
1626 return ieee80211_cfgget(ifp, cmd, data);
1627
1628 case WI_RID_MICROWAVE_OVEN:
1629 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
1630 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1631 &len);
1632 break;
1633 }
1634 wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
1635 len = sizeof(u_int16_t);
1636 break;
1637
1638 case WI_RID_DBM_ADJUST:
1639 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
1640 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1641 &len);
1642 break;
1643 }
1644 wreq.wi_val[0] = htole16(sc->sc_dbm_adjust);
1645 len = sizeof(u_int16_t);
1646 break;
1647
1648 case WI_RID_ROAMING_MODE:
1649 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
1650 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1651 &len);
1652 break;
1653 }
1654 wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
1655 len = sizeof(u_int16_t);
1656 break;
1657
1658 case WI_RID_SYSTEM_SCALE:
1659 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
1660 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1661 &len);
1662 break;
1663 }
1664 wreq.wi_val[0] = htole16(sc->sc_system_scale);
1665 len = sizeof(u_int16_t);
1666 break;
1667
1668 case WI_RID_FRAG_THRESH:
1669 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
1670 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1671 &len);
1672 break;
1673 }
1674 wreq.wi_val[0] = htole16(sc->sc_frag_thresh);
1675 len = sizeof(u_int16_t);
1676 break;
1677
1678 case WI_RID_READ_APS:
1679 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1680 return ieee80211_cfgget(ifp, cmd, data);
1681 if (sc->sc_scan_timer > 0) {
1682 error = EINPROGRESS;
1683 break;
1684 }
1685 n = sc->sc_naps;
1686 if (len < sizeof(n)) {
1687 error = ENOSPC;
1688 break;
1689 }
1690 if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
1691 n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
1692 len = sizeof(n) + sizeof(struct wi_apinfo) * n;
1693 memcpy(wreq.wi_val, &n, sizeof(n));
1694 memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
1695 sizeof(struct wi_apinfo) * n);
1696 break;
1697
1698 default:
1699 if (sc->sc_enabled) {
1700 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1701 &len);
1702 break;
1703 }
1704 switch (wreq.wi_type) {
1705 case WI_RID_MAX_DATALEN:
1706 wreq.wi_val[0] = htole16(sc->sc_max_datalen);
1707 len = sizeof(u_int16_t);
1708 break;
1709 case WI_RID_FRAG_THRESH:
1710 wreq.wi_val[0] = htole16(sc->sc_frag_thresh);
1711 len = sizeof(u_int16_t);
1712 break;
1713 case WI_RID_RTS_THRESH:
1714 wreq.wi_val[0] = htole16(sc->sc_rts_thresh);
1715 len = sizeof(u_int16_t);
1716 break;
1717 case WI_RID_CNFAUTHMODE:
1718 wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
1719 len = sizeof(u_int16_t);
1720 break;
1721 case WI_RID_NODENAME:
1722 if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
1723 error = ENOSPC;
1724 break;
1725 }
1726 len = sc->sc_nodelen + sizeof(u_int16_t);
1727 wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
1728 memcpy(&wreq.wi_val[1], sc->sc_nodename,
1729 sc->sc_nodelen);
1730 break;
1731 default:
1732 return ieee80211_cfgget(ifp, cmd, data);
1733 }
1734 break;
1735 }
1736 if (error)
1737 return error;
1738 wreq.wi_len = (len + 1) / 2 + 1;
1739 return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
1740 }
1741
1742 static int
1743 wi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
1744 {
1745 struct wi_softc *sc = ifp->if_softc;
1746 struct ieee80211com *ic = &sc->sc_ic;
1747 struct ifreq *ifr = (struct ifreq *)data;
1748 struct wi_req wreq;
1749 struct mbuf *m;
1750 int i, len, error;
1751
1752 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1753 if (error)
1754 return error;
1755 len = (wreq.wi_len - 1) * 2;
1756 switch (wreq.wi_type) {
1757 case WI_RID_DBM_ADJUST:
1758 return ENODEV;
1759
1760 case WI_RID_NODENAME:
1761 if (le16toh(wreq.wi_val[0]) * 2 > len ||
1762 le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
1763 error = ENOSPC;
1764 break;
1765 }
1766 if (sc->sc_enabled) {
1767 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
1768 len);
1769 if (error)
1770 break;
1771 }
1772 sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
1773 memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen);
1774 break;
1775
1776 case WI_RID_MICROWAVE_OVEN:
1777 case WI_RID_ROAMING_MODE:
1778 case WI_RID_SYSTEM_SCALE:
1779 case WI_RID_FRAG_THRESH:
1780 if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
1781 (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
1782 break;
1783 if (wreq.wi_type == WI_RID_ROAMING_MODE &&
1784 (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
1785 break;
1786 if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
1787 (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
1788 break;
1789 if (wreq.wi_type == WI_RID_FRAG_THRESH &&
1790 (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
1791 break;
1792 /* FALLTHROUGH */
1793 case WI_RID_RTS_THRESH:
1794 case WI_RID_CNFAUTHMODE:
1795 case WI_RID_MAX_DATALEN:
1796 if (sc->sc_enabled) {
1797 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
1798 sizeof(u_int16_t));
1799 if (error)
1800 break;
1801 }
1802 switch (wreq.wi_type) {
1803 case WI_RID_FRAG_THRESH:
1804 sc->sc_frag_thresh = le16toh(wreq.wi_val[0]);
1805 break;
1806 case WI_RID_RTS_THRESH:
1807 sc->sc_rts_thresh = le16toh(wreq.wi_val[0]);
1808 break;
1809 case WI_RID_MICROWAVE_OVEN:
1810 sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
1811 break;
1812 case WI_RID_ROAMING_MODE:
1813 sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
1814 break;
1815 case WI_RID_SYSTEM_SCALE:
1816 sc->sc_system_scale = le16toh(wreq.wi_val[0]);
1817 break;
1818 case WI_RID_CNFAUTHMODE:
1819 sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
1820 break;
1821 case WI_RID_MAX_DATALEN:
1822 sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
1823 break;
1824 }
1825 break;
1826
1827 case WI_RID_TX_RATE:
1828 switch (le16toh(wreq.wi_val[0])) {
1829 case 3:
1830 ic->ic_fixed_rate = -1;
1831 break;
1832 default:
1833 for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
1834 if ((ic->ic_sup_rates[i] & IEEE80211_RATE_VAL)
1835 / 2 == le16toh(wreq.wi_val[0]))
1836 break;
1837 }
1838 if (i == IEEE80211_RATE_SIZE)
1839 return EINVAL;
1840 ic->ic_fixed_rate = i;
1841 }
1842 if (sc->sc_enabled)
1843 error = wi_write_txrate(sc);
1844 break;
1845
1846 case WI_RID_SCAN_APS:
1847 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
1848 error = wi_scan_ap(sc, 0x3fff, 0x000f);
1849 break;
1850
1851 case WI_RID_MGMT_XMIT:
1852 if (!sc->sc_enabled) {
1853 error = ENETDOWN;
1854 break;
1855 }
1856 if (ic->ic_mgtq.ifq_len > 5) {
1857 error = EAGAIN;
1858 break;
1859 }
1860 /* XXX wi_len looks in u_int8_t, not in u_int16_t */
1861 m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL);
1862 if (m == NULL) {
1863 error = ENOMEM;
1864 break;
1865 }
1866 IF_ENQUEUE(&ic->ic_mgtq, m);
1867 break;
1868
1869 default:
1870 if (sc->sc_enabled) {
1871 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
1872 len);
1873 if (error)
1874 break;
1875 }
1876 error = ieee80211_cfgset(ifp, cmd, data);
1877 break;
1878 }
1879 return error;
1880 }
1881
1882 static int
1883 wi_write_txrate(struct wi_softc *sc)
1884 {
1885 struct ieee80211com *ic = &sc->sc_ic;
1886 int i;
1887 u_int16_t rate;
1888
1889 if (ic->ic_fixed_rate < 0)
1890 rate = 0; /* auto */
1891 else
1892 rate = (ic->ic_sup_rates[ic->ic_fixed_rate] &
1893 IEEE80211_RATE_VAL) / 2;
1894
1895 /* rate: 0, 1, 2, 5, 11 */
1896
1897 switch (sc->sc_firmware_type) {
1898 case WI_LUCENT:
1899 if (rate == 0)
1900 rate = 3; /* auto */
1901 break;
1902 default:
1903 /* Choose a bit according to this table.
1904 *
1905 * bit | data rate
1906 * ----+-------------------
1907 * 0 | 1Mbps
1908 * 1 | 2Mbps
1909 * 2 | 5.5Mbps
1910 * 3 | 11Mbps
1911 */
1912 for (i = 8; i > 0; i >>= 1) {
1913 if (rate >= i)
1914 break;
1915 }
1916 if (i == 0)
1917 rate = 0xf; /* auto */
1918 else
1919 rate = i;
1920 break;
1921 }
1922 return wi_write_val(sc, WI_RID_TX_RATE, rate);
1923 }
1924
1925 static int
1926 wi_write_wep(struct wi_softc *sc)
1927 {
1928 struct ieee80211com *ic = &sc->sc_ic;
1929 int error = 0;
1930 int i, keylen;
1931 u_int16_t val;
1932 struct wi_key wkey[IEEE80211_WEP_NKID];
1933
1934 switch (sc->sc_firmware_type) {
1935 case WI_LUCENT:
1936 val = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0;
1937 error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
1938 if (error)
1939 break;
1940 error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_wep_txkey);
1941 if (error)
1942 break;
1943 memset(wkey, 0, sizeof(wkey));
1944 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1945 keylen = ic->ic_nw_keys[i].wk_len;
1946 wkey[i].wi_keylen = htole16(keylen);
1947 memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
1948 keylen);
1949 }
1950 error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
1951 wkey, sizeof(wkey));
1952 break;
1953
1954 case WI_INTERSIL:
1955 case WI_SYMBOL:
1956 if (ic->ic_flags & IEEE80211_F_WEPON) {
1957 /*
1958 * ONLY HWB3163 EVAL-CARD Firmware version
1959 * less than 0.8 variant2
1960 *
1961 * If promiscuous mode disable, Prism2 chip
1962 * does not work with WEP .
1963 * It is under investigation for details.
1964 * (ichiro (at) netbsd.org)
1965 */
1966 if (sc->sc_firmware_type == WI_INTERSIL &&
1967 sc->sc_sta_firmware_ver < 802 ) {
1968 /* firm ver < 0.8 variant 2 */
1969 wi_write_val(sc, WI_RID_PROMISC, 1);
1970 }
1971 wi_write_val(sc, WI_RID_CNFAUTHMODE,
1972 sc->sc_cnfauthmode);
1973 val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
1974 /*
1975 * Encryption firmware has a bug for HostAP mode.
1976 */
1977 if (sc->sc_firmware_type == WI_INTERSIL &&
1978 ic->ic_opmode == IEEE80211_M_HOSTAP)
1979 val |= HOST_ENCRYPT;
1980 } else {
1981 wi_write_val(sc, WI_RID_CNFAUTHMODE,
1982 IEEE80211_AUTH_OPEN);
1983 val = HOST_ENCRYPT | HOST_DECRYPT;
1984 }
1985 error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
1986 if (error)
1987 break;
1988 error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
1989 ic->ic_wep_txkey);
1990 if (error)
1991 break;
1992 /*
1993 * It seems that the firmware accept 104bit key only if
1994 * all the keys have 104bit length. We get the length of
1995 * the transmit key and use it for all other keys.
1996 * Perhaps we should use software WEP for such situation.
1997 */
1998 keylen = ic->ic_nw_keys[ic->ic_wep_txkey].wk_len;
1999 if (keylen > IEEE80211_WEP_KEYLEN)
2000 keylen = 13; /* 104bit keys */
2001 else
2002 keylen = IEEE80211_WEP_KEYLEN;
2003 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2004 error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
2005 ic->ic_nw_keys[i].wk_key, keylen);
2006 if (error)
2007 break;
2008 }
2009 break;
2010 }
2011 return error;
2012 }
2013
2014 /* Must be called at proper protection level! */
2015 static int
2016 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
2017 {
2018 int i, status;
2019
2020 /* wait for the busy bit to clear */
2021 for (i = 500; i > 0; i--) { /* 5s */
2022 if ((CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY) == 0)
2023 break;
2024 DELAY(10*1000); /* 10 m sec */
2025 }
2026 if (i == 0) {
2027 printf("%s: wi_cmd: busy bit won't clear.\n",
2028 sc->sc_dev.dv_xname);
2029 return(ETIMEDOUT);
2030 }
2031 CSR_WRITE_2(sc, WI_PARAM0, val0);
2032 CSR_WRITE_2(sc, WI_PARAM1, val1);
2033 CSR_WRITE_2(sc, WI_PARAM2, val2);
2034 CSR_WRITE_2(sc, WI_COMMAND, cmd);
2035
2036 if (cmd == WI_CMD_INI) {
2037 /* XXX: should sleep here. */
2038 DELAY(100*1000);
2039 }
2040 /* wait for the cmd completed bit */
2041 for (i = 0; i < WI_TIMEOUT; i++) {
2042 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
2043 break;
2044 DELAY(WI_DELAY);
2045 }
2046
2047 status = CSR_READ_2(sc, WI_STATUS);
2048
2049 /* Ack the command */
2050 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
2051
2052 if (i == WI_TIMEOUT) {
2053 printf("%s: command timed out, cmd=0x%x, arg=0x%x\n",
2054 sc->sc_dev.dv_xname, cmd, val0);
2055 return ETIMEDOUT;
2056 }
2057
2058 if (status & WI_STAT_CMD_RESULT) {
2059 printf("%s: command failed, cmd=0x%x, arg=0x%x\n",
2060 sc->sc_dev.dv_xname, cmd, val0);
2061 return EIO;
2062 }
2063 return 0;
2064 }
2065
2066 static int
2067 wi_seek_bap(struct wi_softc *sc, int id, int off)
2068 {
2069 int i, status;
2070
2071 CSR_WRITE_2(sc, WI_SEL0, id);
2072 CSR_WRITE_2(sc, WI_OFF0, off);
2073
2074 for (i = 0; ; i++) {
2075 status = CSR_READ_2(sc, WI_OFF0);
2076 if ((status & WI_OFF_BUSY) == 0)
2077 break;
2078 if (i == WI_TIMEOUT) {
2079 printf("%s: timeout in wi_seek to %x/%x\n",
2080 sc->sc_dev.dv_xname, id, off);
2081 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2082 return ETIMEDOUT;
2083 }
2084 DELAY(1);
2085 }
2086 if (status & WI_OFF_ERR) {
2087 printf("%s: failed in wi_seek to %x/%x\n",
2088 sc->sc_dev.dv_xname, id, off);
2089 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2090 return EIO;
2091 }
2092 sc->sc_bap_id = id;
2093 sc->sc_bap_off = off;
2094 return 0;
2095 }
2096
2097 static int
2098 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
2099 {
2100 int error, cnt;
2101
2102 if (buflen == 0)
2103 return 0;
2104 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2105 if ((error = wi_seek_bap(sc, id, off)) != 0)
2106 return error;
2107 }
2108 cnt = (buflen + 1) / 2;
2109 CSR_READ_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
2110 sc->sc_bap_off += cnt * 2;
2111 return 0;
2112 }
2113
2114 static int
2115 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
2116 {
2117 int error, cnt;
2118
2119 if (buflen == 0)
2120 return 0;
2121
2122 #ifdef WI_HERMES_AUTOINC_WAR
2123 again:
2124 #endif
2125 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2126 if ((error = wi_seek_bap(sc, id, off)) != 0)
2127 return error;
2128 }
2129 cnt = (buflen + 1) / 2;
2130 CSR_WRITE_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
2131 sc->sc_bap_off += cnt * 2;
2132
2133 #ifdef WI_HERMES_AUTOINC_WAR
2134 /*
2135 * According to the comments in the HCF Light code, there is a bug
2136 * in the Hermes (or possibly in certain Hermes firmware revisions)
2137 * where the chip's internal autoincrement counter gets thrown off
2138 * during data writes: the autoincrement is missed, causing one
2139 * data word to be overwritten and subsequent words to be written to
2140 * the wrong memory locations. The end result is that we could end
2141 * up transmitting bogus frames without realizing it. The workaround
2142 * for this is to write a couple of extra guard words after the end
2143 * of the transfer, then attempt to read then back. If we fail to
2144 * locate the guard words where we expect them, we preform the
2145 * transfer over again.
2146 */
2147 if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
2148 CSR_WRITE_2(sc, WI_DATA0, 0x1234);
2149 CSR_WRITE_2(sc, WI_DATA0, 0x5678);
2150 wi_seek_bap(sc, id, sc->sc_bap_off);
2151 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2152 if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
2153 CSR_READ_2(sc, WI_DATA0) != 0x5678) {
2154 printf("%s: detect auto increment bug, try again\n",
2155 sc->sc_dev.dv_xname);
2156 goto again;
2157 }
2158 }
2159 #endif
2160 return 0;
2161 }
2162
2163 static int
2164 wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
2165 {
2166 int error, len;
2167 struct mbuf *m;
2168
2169 for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
2170 if (m->m_len == 0)
2171 continue;
2172
2173 len = min(m->m_len, totlen);
2174
2175 if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
2176 m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
2177 return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
2178 totlen);
2179 }
2180
2181 if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
2182 return error;
2183
2184 off += m->m_len;
2185 totlen -= len;
2186 }
2187 return 0;
2188 }
2189
2190 static int
2191 wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
2192 {
2193 int i;
2194
2195 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
2196 printf("%s: failed to allocate %d bytes on NIC\n",
2197 sc->sc_dev.dv_xname, len);
2198 return ENOMEM;
2199 }
2200
2201 for (i = 0; i < WI_TIMEOUT; i++) {
2202 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
2203 break;
2204 if (i == WI_TIMEOUT) {
2205 printf("%s: timeout in alloc\n", sc->sc_dev.dv_xname);
2206 return ETIMEDOUT;
2207 }
2208 DELAY(1);
2209 }
2210 *idp = CSR_READ_2(sc, WI_ALLOC_FID);
2211 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
2212 return 0;
2213 }
2214
2215 static int
2216 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
2217 {
2218 int error, len;
2219 u_int16_t ltbuf[2];
2220
2221 /* Tell the NIC to enter record read mode. */
2222 error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2223 if (error)
2224 return error;
2225
2226 error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2227 if (error)
2228 return error;
2229
2230 if (le16toh(ltbuf[1]) != rid) {
2231 printf("%s: record read mismatch, rid=%x, got=%x\n",
2232 sc->sc_dev.dv_xname, rid, le16toh(ltbuf[1]));
2233 return EIO;
2234 }
2235 len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */
2236 if (*buflenp < len) {
2237 printf("%s: record buffer is too small, "
2238 "rid=%x, size=%d, len=%d\n",
2239 sc->sc_dev.dv_xname, rid, *buflenp, len);
2240 return ENOSPC;
2241 }
2242 *buflenp = len;
2243 return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2244 }
2245
2246 static int
2247 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2248 {
2249 int error;
2250 u_int16_t ltbuf[2];
2251
2252 ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */
2253 ltbuf[1] = htole16(rid);
2254
2255 error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2256 if (error)
2257 return error;
2258 error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2259 if (error)
2260 return error;
2261
2262 return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
2263 }
2264
2265 static int
2266 wi_newstate(void *arg, enum ieee80211_state nstate)
2267 {
2268 struct wi_softc *sc = arg;
2269 struct ieee80211com *ic = &sc->sc_ic;
2270 struct ieee80211_node *ni = &ic->ic_bss;
2271 int i, buflen;
2272 u_int16_t val;
2273 struct wi_ssid ssid;
2274 u_int8_t old_bssid[IEEE80211_ADDR_LEN];
2275 enum ieee80211_state ostate;
2276 #ifdef WI_DEBUG
2277 static const char *stname[] =
2278 { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" };
2279 #endif /* WI_DEBUG */
2280
2281 ostate = ic->ic_state;
2282 DPRINTF(("wi_newstate: %s -> %s\n", stname[ostate], stname[nstate]));
2283
2284 ic->ic_state = nstate;
2285 switch (nstate) {
2286 case IEEE80211_S_INIT:
2287 ic->ic_flags &= ~IEEE80211_F_SIBSS;
2288 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2289 return 0;
2290
2291 case IEEE80211_S_RUN:
2292 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2293 buflen = IEEE80211_ADDR_LEN;
2294 IEEE80211_ADDR_COPY(old_bssid, ni->ni_bssid);
2295 wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen);
2296 IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
2297 buflen = sizeof(val);
2298 wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
2299 ni->ni_chan = le16toh(val);
2300
2301 if (IEEE80211_ADDR_EQ(old_bssid, ni->ni_bssid))
2302 sc->sc_false_syns++;
2303 else
2304 sc->sc_false_syns = 0;
2305
2306 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2307 ni->ni_esslen = ic->ic_des_esslen;
2308 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
2309 ni->ni_nrate = 0;
2310 for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
2311 if (ic->ic_sup_rates[i])
2312 ni->ni_rates[ni->ni_nrate++] =
2313 ic->ic_sup_rates[i];
2314 }
2315 ni->ni_intval = ic->ic_lintval;
2316 ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
2317 if (ic->ic_flags & IEEE80211_F_WEPON)
2318 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
2319 } else {
2320 buflen = sizeof(ssid);
2321 wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
2322 ni->ni_esslen = le16toh(ssid.wi_len);
2323 if (ni->ni_esslen > IEEE80211_NWID_LEN)
2324 ni->ni_esslen = IEEE80211_NWID_LEN; /*XXX*/
2325 memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
2326 }
2327 break;
2328
2329 case IEEE80211_S_SCAN:
2330 case IEEE80211_S_AUTH:
2331 case IEEE80211_S_ASSOC:
2332 break;
2333 }
2334
2335 /* skip standard ieee80211 handling */
2336 return EINPROGRESS;
2337 }
2338
2339 static int
2340 wi_set_tim(struct ieee80211com *ic, int aid, int which)
2341 {
2342 struct wi_softc *sc = ic->ic_softc;
2343
2344 aid &= ~0xc000;
2345 if (which)
2346 aid |= 0x8000;
2347
2348 return wi_write_val(sc, WI_RID_SET_TIM, aid);
2349 }
2350
2351 static int
2352 wi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate)
2353 {
2354 int error = 0;
2355 u_int16_t val[2];
2356
2357 if (!sc->sc_enabled)
2358 return ENXIO;
2359 switch (sc->sc_firmware_type) {
2360 case WI_LUCENT:
2361 (void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
2362 break;
2363 case WI_INTERSIL:
2364 val[0] = chanmask; /* channel */
2365 val[1] = txrate; /* tx rate */
2366 error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
2367 break;
2368 case WI_SYMBOL:
2369 /*
2370 * XXX only supported on 3.x ?
2371 */
2372 val[0] = BSCAN_BCAST | BSCAN_ONETIME;
2373 error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
2374 val, sizeof(val[0]));
2375 break;
2376 }
2377 if (error == 0) {
2378 sc->sc_scan_timer = WI_SCAN_WAIT;
2379 sc->sc_ic.ic_if.if_timer = 1;
2380 DPRINTF(("wi_scan_ap: start scanning, "
2381 "chanmask 0x%x txrate 0x%x\n", chanmask, txrate));
2382 }
2383 return error;
2384 }
2385
2386 static void
2387 wi_scan_result(struct wi_softc *sc, int fid, int cnt)
2388 {
2389 #define N(a) (sizeof (a) / sizeof (a[0]))
2390 int i, naps, off, szbuf;
2391 struct wi_scan_header ws_hdr; /* Prism2 header */
2392 struct wi_scan_data_p2 ws_dat; /* Prism2 scantable*/
2393 struct wi_apinfo *ap;
2394
2395 off = sizeof(u_int16_t) * 2;
2396 memset(&ws_hdr, 0, sizeof(ws_hdr));
2397 switch (sc->sc_firmware_type) {
2398 case WI_INTERSIL:
2399 wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
2400 off += sizeof(ws_hdr);
2401 szbuf = sizeof(struct wi_scan_data_p2);
2402 break;
2403 case WI_SYMBOL:
2404 szbuf = sizeof(struct wi_scan_data_p2) + 6;
2405 break;
2406 case WI_LUCENT:
2407 szbuf = sizeof(struct wi_scan_data);
2408 break;
2409 default:
2410 printf("%s: wi_scan_result: unknown firmware type %u\n",
2411 sc->sc_dev.dv_xname, sc->sc_firmware_type);
2412 naps = 0;
2413 goto done;
2414 }
2415 naps = (cnt * 2 + 2 - off) / szbuf;
2416 if (naps > N(sc->sc_aps))
2417 naps = N(sc->sc_aps);
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:
2443 /* Done scanning */
2444 sc->sc_scan_timer = 0;
2445 DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
2446 #undef N
2447 }
2448
2449 static void
2450 wi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi)
2451 {
2452 ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr),
2453 ni ? ni->ni_rates[ni->ni_txrate] & IEEE80211_RATE_VAL : -1, rssi);
2454 printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n",
2455 le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1),
2456 le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence);
2457 printf(" rx_signal %u rx_rate %u rx_flow %u\n",
2458 wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow);
2459 printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n",
2460 wh->wi_tx_rtry, wh->wi_tx_rate,
2461 le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len));
2462 printf(" ehdr dst %s src %s type 0x%x\n",
2463 ether_sprintf(wh->wi_ehdr.ether_dhost),
2464 ether_sprintf(wh->wi_ehdr.ether_shost),
2465 wh->wi_ehdr.ether_type);
2466 }
2467