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