wi.c revision 1.125 1 /* $NetBSD: wi.c,v 1.125 2003/05/16 01:26:18 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.125 2003/05/16 01:26:18 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; /* 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 printf("%s: tx failed", sc->sc_dev.dv_xname);
1327 if (status & WI_TXSTAT_RET_ERR)
1328 printf(", retry limit exceeded");
1329 if (status & WI_TXSTAT_AGED_ERR)
1330 printf(", max transmit lifetime exceeded");
1331 if (status & WI_TXSTAT_DISCONNECT)
1332 printf(", port disconnected");
1333 if (status & WI_TXSTAT_FORM_ERR)
1334 printf(", invalid format (data len %u src %s)",
1335 le16toh(frmhdr.wi_dat_len),
1336 ether_sprintf(frmhdr.wi_ehdr.ether_shost));
1337 if (status & ~0xf)
1338 printf(", status=0x%x", status);
1339 printf("\n");
1340 }
1341 ifp->if_oerrors++;
1342 } else {
1343 DPRINTF(("port disconnected\n"));
1344 ifp->if_collisions++; /* XXX */
1345 }
1346 } else
1347 DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
1348 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1349 }
1350
1351 static void
1352 wi_tx_intr(struct wi_softc *sc)
1353 {
1354 struct ieee80211com *ic = &sc->sc_ic;
1355 struct ifnet *ifp = &ic->ic_if;
1356 int fid, cur;
1357
1358 fid = CSR_READ_2(sc, WI_ALLOC_FID);
1359 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1360
1361 cur = sc->sc_txcur;
1362 if (sc->sc_txd[cur].d_fid != fid) {
1363 printf("%s: bad alloc %x != %x, cur %d nxt %d\n",
1364 sc->sc_dev.dv_xname, fid, sc->sc_txd[cur].d_fid, cur,
1365 sc->sc_txnext);
1366 return;
1367 }
1368 sc->sc_tx_timer = 0;
1369 sc->sc_txd[cur].d_len = 0;
1370 sc->sc_txcur = cur = (cur + 1) % WI_NTXBUF;
1371 if (sc->sc_txd[cur].d_len == 0)
1372 ifp->if_flags &= ~IFF_OACTIVE;
1373 else {
1374 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1375 0, 0)) {
1376 printf("%s: xmit failed\n", sc->sc_dev.dv_xname);
1377 sc->sc_txd[cur].d_len = 0;
1378 } else {
1379 sc->sc_tx_timer = 5;
1380 ifp->if_timer = 1;
1381 }
1382 }
1383 }
1384
1385 static void
1386 wi_info_intr(struct wi_softc *sc)
1387 {
1388 struct ieee80211com *ic = &sc->sc_ic;
1389 struct ifnet *ifp = &ic->ic_if;
1390 int i, fid, len, off;
1391 u_int16_t ltbuf[2];
1392 u_int16_t stat;
1393 u_int32_t *ptr;
1394
1395 fid = CSR_READ_2(sc, WI_INFO_FID);
1396 wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
1397
1398 switch (le16toh(ltbuf[1])) {
1399
1400 case WI_INFO_LINK_STAT:
1401 wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1402 DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1403 switch (le16toh(stat)) {
1404 case CONNECTED:
1405 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1406 if (ic->ic_state == IEEE80211_S_RUN &&
1407 ic->ic_opmode != IEEE80211_M_IBSS)
1408 break;
1409 /* FALLTHROUGH */
1410 case AP_CHANGE:
1411 ieee80211_new_state(ifp, IEEE80211_S_RUN, -1);
1412 break;
1413 case AP_IN_RANGE:
1414 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1415 break;
1416 case AP_OUT_OF_RANGE:
1417 if (sc->sc_firmware_type == WI_SYMBOL &&
1418 sc->sc_scan_timer > 0) {
1419 if (wi_cmd(sc, WI_CMD_INQUIRE,
1420 WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
1421 sc->sc_scan_timer = 0;
1422 break;
1423 }
1424 if (ic->ic_opmode == IEEE80211_M_STA)
1425 sc->sc_flags |= WI_FLAGS_OUTRANGE;
1426 break;
1427 case DISCONNECTED:
1428 case ASSOC_FAILED:
1429 if (ic->ic_opmode == IEEE80211_M_STA)
1430 ieee80211_new_state(ifp, IEEE80211_S_INIT, -1);
1431 break;
1432 }
1433 break;
1434
1435 case WI_INFO_COUNTERS:
1436 /* some card versions have a larger stats structure */
1437 len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1438 ptr = (u_int32_t *)&sc->sc_stats;
1439 off = sizeof(ltbuf);
1440 for (i = 0; i < len; i++, off += 2, ptr++) {
1441 wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1442 #ifdef WI_HERMES_STATS_WAR
1443 if (stat & 0xf000)
1444 stat = ~stat;
1445 #endif
1446 *ptr += stat;
1447 }
1448 ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1449 sc->sc_stats.wi_tx_multi_retries +
1450 sc->sc_stats.wi_tx_retry_limit;
1451 break;
1452
1453 case WI_INFO_SCAN_RESULTS:
1454 case WI_INFO_HOST_SCAN_RESULTS:
1455 wi_scan_result(sc, fid, le16toh(ltbuf[0]));
1456 break;
1457
1458 default:
1459 DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1460 le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1461 break;
1462 }
1463 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1464 }
1465
1466 static int
1467 wi_write_multi(struct wi_softc *sc)
1468 {
1469 struct ifnet *ifp = &sc->sc_ic.ic_if;
1470 int n;
1471 struct wi_mcast mlist;
1472 struct ether_multi *enm;
1473 struct ether_multistep estep;
1474
1475 if ((ifp->if_flags & IFF_PROMISC) != 0) {
1476 allmulti:
1477 ifp->if_flags |= IFF_ALLMULTI;
1478 memset(&mlist, 0, sizeof(mlist));
1479 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1480 sizeof(mlist));
1481 }
1482
1483 n = 0;
1484 ETHER_FIRST_MULTI(estep, &sc->sc_ic.ic_ec, enm);
1485 while (enm != NULL) {
1486 /* Punt on ranges or too many multicast addresses. */
1487 if (!IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi) ||
1488 n >= sizeof(mlist) / sizeof(mlist.wi_mcast[0]))
1489 goto allmulti;
1490
1491 IEEE80211_ADDR_COPY(&mlist.wi_mcast[n], enm->enm_addrlo);
1492 n++;
1493 ETHER_NEXT_MULTI(estep, enm);
1494 }
1495 ifp->if_flags &= ~IFF_ALLMULTI;
1496 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1497 IEEE80211_ADDR_LEN * n);
1498 }
1499
1500
1501 static void
1502 wi_read_nicid(struct wi_softc *sc)
1503 {
1504 struct wi_card_ident *id;
1505 char *p;
1506 int len;
1507 u_int16_t ver[4];
1508
1509 /* getting chip identity */
1510 memset(ver, 0, sizeof(ver));
1511 len = sizeof(ver);
1512 wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1513 printf("%s: using ", sc->sc_dev.dv_xname);
1514 DPRINTF2(("wi_read_nicid: CARD_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3])));
1515
1516 sc->sc_firmware_type = WI_NOTYPE;
1517 for (id = wi_card_ident; id->card_name != NULL; id++) {
1518 if (le16toh(ver[0]) == id->card_id) {
1519 printf("%s", id->card_name);
1520 sc->sc_firmware_type = id->firm_type;
1521 break;
1522 }
1523 }
1524 if (sc->sc_firmware_type == WI_NOTYPE) {
1525 if (le16toh(ver[0]) & 0x8000) {
1526 printf("Unknown PRISM2 chip");
1527 sc->sc_firmware_type = WI_INTERSIL;
1528 } else {
1529 printf("Unknown Lucent chip");
1530 sc->sc_firmware_type = WI_LUCENT;
1531 }
1532 }
1533
1534 /* get primary firmware version (Only Prism chips) */
1535 if (sc->sc_firmware_type != WI_LUCENT) {
1536 memset(ver, 0, sizeof(ver));
1537 len = sizeof(ver);
1538 wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1539 sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1540 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1541 }
1542
1543 /* get station firmware version */
1544 memset(ver, 0, sizeof(ver));
1545 len = sizeof(ver);
1546 wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1547 sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1548 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1549 if (sc->sc_firmware_type == WI_INTERSIL &&
1550 (sc->sc_sta_firmware_ver == 10102 ||
1551 sc->sc_sta_firmware_ver == 20102)) {
1552 char ident[12];
1553 memset(ident, 0, sizeof(ident));
1554 len = sizeof(ident);
1555 /* value should be the format like "V2.00-11" */
1556 if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1557 *(p = (char *)ident) >= 'A' &&
1558 p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1559 sc->sc_firmware_type = WI_SYMBOL;
1560 sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1561 (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1562 (p[6] - '0') * 10 + (p[7] - '0');
1563 }
1564 }
1565
1566 printf("\n%s: %s Firmware: ", sc->sc_dev.dv_xname,
1567 sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
1568 (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
1569 if (sc->sc_firmware_type != WI_LUCENT) /* XXX */
1570 printf("Primary (%u.%u.%u), ",
1571 sc->sc_pri_firmware_ver / 10000,
1572 (sc->sc_pri_firmware_ver % 10000) / 100,
1573 sc->sc_pri_firmware_ver % 100);
1574 printf("Station (%u.%u.%u)\n",
1575 sc->sc_sta_firmware_ver / 10000,
1576 (sc->sc_sta_firmware_ver % 10000) / 100,
1577 sc->sc_sta_firmware_ver % 100);
1578 }
1579
1580 static int
1581 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1582 {
1583 struct wi_ssid ssid;
1584
1585 if (buflen > IEEE80211_NWID_LEN)
1586 return ENOBUFS;
1587 memset(&ssid, 0, sizeof(ssid));
1588 ssid.wi_len = htole16(buflen);
1589 memcpy(ssid.wi_ssid, buf, buflen);
1590 return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1591 }
1592
1593 static int
1594 wi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
1595 {
1596 struct wi_softc *sc = ifp->if_softc;
1597 struct ieee80211com *ic = &sc->sc_ic;
1598 struct ifreq *ifr = (struct ifreq *)data;
1599 struct wi_req wreq;
1600 int len, n, error;
1601
1602 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1603 if (error)
1604 return error;
1605 len = (wreq.wi_len - 1) * 2;
1606 if (len < sizeof(u_int16_t))
1607 return ENOSPC;
1608 if (len > sizeof(wreq.wi_val))
1609 len = sizeof(wreq.wi_val);
1610
1611 switch (wreq.wi_type) {
1612
1613 case WI_RID_IFACE_STATS:
1614 memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
1615 if (len < sizeof(sc->sc_stats))
1616 error = ENOSPC;
1617 else
1618 len = sizeof(sc->sc_stats);
1619 break;
1620
1621 case WI_RID_ENCRYPTION:
1622 case WI_RID_TX_CRYPT_KEY:
1623 case WI_RID_DEFLT_CRYPT_KEYS:
1624 case WI_RID_TX_RATE:
1625 return ieee80211_cfgget(ifp, cmd, data);
1626
1627 case WI_RID_MICROWAVE_OVEN:
1628 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
1629 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1630 &len);
1631 break;
1632 }
1633 wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
1634 len = sizeof(u_int16_t);
1635 break;
1636
1637 case WI_RID_DBM_ADJUST:
1638 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
1639 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1640 &len);
1641 break;
1642 }
1643 wreq.wi_val[0] = htole16(sc->sc_dbm_adjust);
1644 len = sizeof(u_int16_t);
1645 break;
1646
1647 case WI_RID_ROAMING_MODE:
1648 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
1649 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1650 &len);
1651 break;
1652 }
1653 wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
1654 len = sizeof(u_int16_t);
1655 break;
1656
1657 case WI_RID_SYSTEM_SCALE:
1658 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
1659 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1660 &len);
1661 break;
1662 }
1663 wreq.wi_val[0] = htole16(sc->sc_system_scale);
1664 len = sizeof(u_int16_t);
1665 break;
1666
1667 case WI_RID_FRAG_THRESH:
1668 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
1669 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1670 &len);
1671 break;
1672 }
1673 wreq.wi_val[0] = htole16(sc->sc_frag_thresh);
1674 len = sizeof(u_int16_t);
1675 break;
1676
1677 case WI_RID_READ_APS:
1678 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1679 return ieee80211_cfgget(ifp, cmd, data);
1680 if (sc->sc_scan_timer > 0) {
1681 error = EINPROGRESS;
1682 break;
1683 }
1684 n = sc->sc_naps;
1685 if (len < sizeof(n)) {
1686 error = ENOSPC;
1687 break;
1688 }
1689 if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
1690 n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
1691 len = sizeof(n) + sizeof(struct wi_apinfo) * n;
1692 memcpy(wreq.wi_val, &n, sizeof(n));
1693 memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
1694 sizeof(struct wi_apinfo) * n);
1695 break;
1696
1697 default:
1698 if (sc->sc_enabled) {
1699 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1700 &len);
1701 break;
1702 }
1703 switch (wreq.wi_type) {
1704 case WI_RID_MAX_DATALEN:
1705 wreq.wi_val[0] = htole16(sc->sc_max_datalen);
1706 len = sizeof(u_int16_t);
1707 break;
1708 case WI_RID_FRAG_THRESH:
1709 wreq.wi_val[0] = htole16(sc->sc_frag_thresh);
1710 len = sizeof(u_int16_t);
1711 break;
1712 case WI_RID_RTS_THRESH:
1713 wreq.wi_val[0] = htole16(sc->sc_rts_thresh);
1714 len = sizeof(u_int16_t);
1715 break;
1716 case WI_RID_CNFAUTHMODE:
1717 wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
1718 len = sizeof(u_int16_t);
1719 break;
1720 case WI_RID_NODENAME:
1721 if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
1722 error = ENOSPC;
1723 break;
1724 }
1725 len = sc->sc_nodelen + sizeof(u_int16_t);
1726 wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
1727 memcpy(&wreq.wi_val[1], sc->sc_nodename,
1728 sc->sc_nodelen);
1729 break;
1730 default:
1731 return ieee80211_cfgget(ifp, cmd, data);
1732 }
1733 break;
1734 }
1735 if (error)
1736 return error;
1737 wreq.wi_len = (len + 1) / 2 + 1;
1738 return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
1739 }
1740
1741 static int
1742 wi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
1743 {
1744 struct wi_softc *sc = ifp->if_softc;
1745 struct ieee80211com *ic = &sc->sc_ic;
1746 struct ifreq *ifr = (struct ifreq *)data;
1747 struct wi_req wreq;
1748 struct mbuf *m;
1749 int i, len, error;
1750
1751 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1752 if (error)
1753 return error;
1754 len = (wreq.wi_len - 1) * 2;
1755 switch (wreq.wi_type) {
1756 case WI_RID_DBM_ADJUST:
1757 return ENODEV;
1758
1759 case WI_RID_NODENAME:
1760 if (le16toh(wreq.wi_val[0]) * 2 > len ||
1761 le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
1762 error = ENOSPC;
1763 break;
1764 }
1765 if (sc->sc_enabled) {
1766 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
1767 len);
1768 if (error)
1769 break;
1770 }
1771 sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
1772 memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen);
1773 break;
1774
1775 case WI_RID_MICROWAVE_OVEN:
1776 case WI_RID_ROAMING_MODE:
1777 case WI_RID_SYSTEM_SCALE:
1778 case WI_RID_FRAG_THRESH:
1779 if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
1780 (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
1781 break;
1782 if (wreq.wi_type == WI_RID_ROAMING_MODE &&
1783 (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
1784 break;
1785 if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
1786 (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
1787 break;
1788 if (wreq.wi_type == WI_RID_FRAG_THRESH &&
1789 (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
1790 break;
1791 /* FALLTHROUGH */
1792 case WI_RID_RTS_THRESH:
1793 case WI_RID_CNFAUTHMODE:
1794 case WI_RID_MAX_DATALEN:
1795 if (sc->sc_enabled) {
1796 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
1797 sizeof(u_int16_t));
1798 if (error)
1799 break;
1800 }
1801 switch (wreq.wi_type) {
1802 case WI_RID_FRAG_THRESH:
1803 sc->sc_frag_thresh = le16toh(wreq.wi_val[0]);
1804 break;
1805 case WI_RID_RTS_THRESH:
1806 sc->sc_rts_thresh = le16toh(wreq.wi_val[0]);
1807 break;
1808 case WI_RID_MICROWAVE_OVEN:
1809 sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
1810 break;
1811 case WI_RID_ROAMING_MODE:
1812 sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
1813 break;
1814 case WI_RID_SYSTEM_SCALE:
1815 sc->sc_system_scale = le16toh(wreq.wi_val[0]);
1816 break;
1817 case WI_RID_CNFAUTHMODE:
1818 sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
1819 break;
1820 case WI_RID_MAX_DATALEN:
1821 sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
1822 break;
1823 }
1824 break;
1825
1826 case WI_RID_TX_RATE:
1827 switch (le16toh(wreq.wi_val[0])) {
1828 case 3:
1829 ic->ic_fixed_rate = -1;
1830 break;
1831 default:
1832 for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
1833 if ((ic->ic_sup_rates[i] & IEEE80211_RATE_VAL)
1834 / 2 == le16toh(wreq.wi_val[0]))
1835 break;
1836 }
1837 if (i == IEEE80211_RATE_SIZE)
1838 return EINVAL;
1839 ic->ic_fixed_rate = i;
1840 }
1841 if (sc->sc_enabled)
1842 error = wi_write_txrate(sc);
1843 break;
1844
1845 case WI_RID_SCAN_APS:
1846 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
1847 error = wi_scan_ap(sc, 0x3fff, 0x000f);
1848 break;
1849
1850 case WI_RID_MGMT_XMIT:
1851 if (!sc->sc_enabled) {
1852 error = ENETDOWN;
1853 break;
1854 }
1855 if (ic->ic_mgtq.ifq_len > 5) {
1856 error = EAGAIN;
1857 break;
1858 }
1859 /* XXX wi_len looks in u_int8_t, not in u_int16_t */
1860 m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL);
1861 if (m == NULL) {
1862 error = ENOMEM;
1863 break;
1864 }
1865 IF_ENQUEUE(&ic->ic_mgtq, m);
1866 break;
1867
1868 default:
1869 if (sc->sc_enabled) {
1870 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
1871 len);
1872 if (error)
1873 break;
1874 }
1875 error = ieee80211_cfgset(ifp, cmd, data);
1876 break;
1877 }
1878 return error;
1879 }
1880
1881 static int
1882 wi_write_txrate(struct wi_softc *sc)
1883 {
1884 struct ieee80211com *ic = &sc->sc_ic;
1885 int i;
1886 u_int16_t rate;
1887
1888 if (ic->ic_fixed_rate < 0)
1889 rate = 0; /* auto */
1890 else
1891 rate = (ic->ic_sup_rates[ic->ic_fixed_rate] &
1892 IEEE80211_RATE_VAL) / 2;
1893
1894 /* rate: 0, 1, 2, 5, 11 */
1895
1896 switch (sc->sc_firmware_type) {
1897 case WI_LUCENT:
1898 if (rate == 0)
1899 rate = 3; /* auto */
1900 break;
1901 default:
1902 /* Choose a bit according to this table.
1903 *
1904 * bit | data rate
1905 * ----+-------------------
1906 * 0 | 1Mbps
1907 * 1 | 2Mbps
1908 * 2 | 5.5Mbps
1909 * 3 | 11Mbps
1910 */
1911 for (i = 8; i > 0; i >>= 1) {
1912 if (rate >= i)
1913 break;
1914 }
1915 if (i == 0)
1916 rate = 0xf; /* auto */
1917 else
1918 rate = i;
1919 break;
1920 }
1921 return wi_write_val(sc, WI_RID_TX_RATE, rate);
1922 }
1923
1924 static int
1925 wi_write_wep(struct wi_softc *sc)
1926 {
1927 struct ieee80211com *ic = &sc->sc_ic;
1928 int error = 0;
1929 int i, keylen;
1930 u_int16_t val;
1931 struct wi_key wkey[IEEE80211_WEP_NKID];
1932
1933 switch (sc->sc_firmware_type) {
1934 case WI_LUCENT:
1935 val = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0;
1936 error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
1937 if (error)
1938 break;
1939 error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_wep_txkey);
1940 if (error)
1941 break;
1942 memset(wkey, 0, sizeof(wkey));
1943 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1944 keylen = ic->ic_nw_keys[i].wk_len;
1945 wkey[i].wi_keylen = htole16(keylen);
1946 memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
1947 keylen);
1948 }
1949 error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
1950 wkey, sizeof(wkey));
1951 break;
1952
1953 case WI_INTERSIL:
1954 case WI_SYMBOL:
1955 if (ic->ic_flags & IEEE80211_F_WEPON) {
1956 /*
1957 * ONLY HWB3163 EVAL-CARD Firmware version
1958 * less than 0.8 variant2
1959 *
1960 * If promiscuous mode disable, Prism2 chip
1961 * does not work with WEP .
1962 * It is under investigation for details.
1963 * (ichiro (at) netbsd.org)
1964 */
1965 if (sc->sc_firmware_type == WI_INTERSIL &&
1966 sc->sc_sta_firmware_ver < 802 ) {
1967 /* firm ver < 0.8 variant 2 */
1968 wi_write_val(sc, WI_RID_PROMISC, 1);
1969 }
1970 wi_write_val(sc, WI_RID_CNFAUTHMODE,
1971 sc->sc_cnfauthmode);
1972 val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
1973 /*
1974 * Encryption firmware has a bug for HostAP mode.
1975 */
1976 if (sc->sc_firmware_type == WI_INTERSIL &&
1977 ic->ic_opmode == IEEE80211_M_HOSTAP)
1978 val |= HOST_ENCRYPT;
1979 } else {
1980 wi_write_val(sc, WI_RID_CNFAUTHMODE,
1981 IEEE80211_AUTH_OPEN);
1982 val = HOST_ENCRYPT | HOST_DECRYPT;
1983 }
1984 error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
1985 if (error)
1986 break;
1987 error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
1988 ic->ic_wep_txkey);
1989 if (error)
1990 break;
1991 /*
1992 * It seems that the firmware accept 104bit key only if
1993 * all the keys have 104bit length. We get the length of
1994 * the transmit key and use it for all other keys.
1995 * Perhaps we should use software WEP for such situation.
1996 */
1997 keylen = ic->ic_nw_keys[ic->ic_wep_txkey].wk_len;
1998 if (keylen > IEEE80211_WEP_KEYLEN)
1999 keylen = 13; /* 104bit keys */
2000 else
2001 keylen = IEEE80211_WEP_KEYLEN;
2002 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2003 error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
2004 ic->ic_nw_keys[i].wk_key, keylen);
2005 if (error)
2006 break;
2007 }
2008 break;
2009 }
2010 return error;
2011 }
2012
2013 /* Must be called at proper protection level! */
2014 static int
2015 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
2016 {
2017 int i, status;
2018
2019 /* wait for the busy bit to clear */
2020 for (i = 500; i > 0; i--) { /* 5s */
2021 if ((CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY) == 0)
2022 break;
2023 DELAY(10*1000); /* 10 m sec */
2024 }
2025 if (i == 0) {
2026 printf("%s: wi_cmd: busy bit won't clear.\n",
2027 sc->sc_dev.dv_xname);
2028 return(ETIMEDOUT);
2029 }
2030 CSR_WRITE_2(sc, WI_PARAM0, val0);
2031 CSR_WRITE_2(sc, WI_PARAM1, val1);
2032 CSR_WRITE_2(sc, WI_PARAM2, val2);
2033 CSR_WRITE_2(sc, WI_COMMAND, cmd);
2034
2035 if (cmd == WI_CMD_INI) {
2036 /* XXX: should sleep here. */
2037 DELAY(100*1000);
2038 }
2039 /* wait for the cmd completed bit */
2040 for (i = 0; i < WI_TIMEOUT; i++) {
2041 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
2042 break;
2043 DELAY(WI_DELAY);
2044 }
2045
2046 status = CSR_READ_2(sc, WI_STATUS);
2047
2048 /* Ack the command */
2049 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
2050
2051 if (i == WI_TIMEOUT) {
2052 printf("%s: command timed out, cmd=0x%x, arg=0x%x\n",
2053 sc->sc_dev.dv_xname, cmd, val0);
2054 return ETIMEDOUT;
2055 }
2056
2057 if (status & WI_STAT_CMD_RESULT) {
2058 printf("%s: command failed, cmd=0x%x, arg=0x%x\n",
2059 sc->sc_dev.dv_xname, cmd, val0);
2060 return EIO;
2061 }
2062 return 0;
2063 }
2064
2065 static int
2066 wi_seek_bap(struct wi_softc *sc, int id, int off)
2067 {
2068 int i, status;
2069
2070 CSR_WRITE_2(sc, WI_SEL0, id);
2071 CSR_WRITE_2(sc, WI_OFF0, off);
2072
2073 for (i = 0; ; i++) {
2074 status = CSR_READ_2(sc, WI_OFF0);
2075 if ((status & WI_OFF_BUSY) == 0)
2076 break;
2077 if (i == WI_TIMEOUT) {
2078 printf("%s: timeout in wi_seek to %x/%x\n",
2079 sc->sc_dev.dv_xname, id, off);
2080 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2081 return ETIMEDOUT;
2082 }
2083 DELAY(1);
2084 }
2085 if (status & WI_OFF_ERR) {
2086 printf("%s: failed in wi_seek to %x/%x\n",
2087 sc->sc_dev.dv_xname, id, off);
2088 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2089 return EIO;
2090 }
2091 sc->sc_bap_id = id;
2092 sc->sc_bap_off = off;
2093 return 0;
2094 }
2095
2096 static int
2097 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
2098 {
2099 int error, cnt;
2100
2101 if (buflen == 0)
2102 return 0;
2103 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2104 if ((error = wi_seek_bap(sc, id, off)) != 0)
2105 return error;
2106 }
2107 cnt = (buflen + 1) / 2;
2108 CSR_READ_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
2109 sc->sc_bap_off += cnt * 2;
2110 return 0;
2111 }
2112
2113 static int
2114 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
2115 {
2116 int error, cnt;
2117
2118 if (buflen == 0)
2119 return 0;
2120
2121 #ifdef WI_HERMES_AUTOINC_WAR
2122 again:
2123 #endif
2124 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2125 if ((error = wi_seek_bap(sc, id, off)) != 0)
2126 return error;
2127 }
2128 cnt = (buflen + 1) / 2;
2129 CSR_WRITE_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
2130 sc->sc_bap_off += cnt * 2;
2131
2132 #ifdef WI_HERMES_AUTOINC_WAR
2133 /*
2134 * According to the comments in the HCF Light code, there is a bug
2135 * in the Hermes (or possibly in certain Hermes firmware revisions)
2136 * where the chip's internal autoincrement counter gets thrown off
2137 * during data writes: the autoincrement is missed, causing one
2138 * data word to be overwritten and subsequent words to be written to
2139 * the wrong memory locations. The end result is that we could end
2140 * up transmitting bogus frames without realizing it. The workaround
2141 * for this is to write a couple of extra guard words after the end
2142 * of the transfer, then attempt to read then back. If we fail to
2143 * locate the guard words where we expect them, we preform the
2144 * transfer over again.
2145 */
2146 if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
2147 CSR_WRITE_2(sc, WI_DATA0, 0x1234);
2148 CSR_WRITE_2(sc, WI_DATA0, 0x5678);
2149 wi_seek_bap(sc, id, sc->sc_bap_off);
2150 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2151 if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
2152 CSR_READ_2(sc, WI_DATA0) != 0x5678) {
2153 printf("%s: detect auto increment bug, try again\n",
2154 sc->sc_dev.dv_xname);
2155 goto again;
2156 }
2157 }
2158 #endif
2159 return 0;
2160 }
2161
2162 static int
2163 wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
2164 {
2165 int error, len;
2166 struct mbuf *m;
2167
2168 for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
2169 if (m->m_len == 0)
2170 continue;
2171
2172 len = min(m->m_len, totlen);
2173
2174 if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
2175 m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
2176 return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
2177 totlen);
2178 }
2179
2180 if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
2181 return error;
2182
2183 off += m->m_len;
2184 totlen -= len;
2185 }
2186 return 0;
2187 }
2188
2189 static int
2190 wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
2191 {
2192 int i;
2193
2194 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
2195 printf("%s: failed to allocate %d bytes on NIC\n",
2196 sc->sc_dev.dv_xname, len);
2197 return ENOMEM;
2198 }
2199
2200 for (i = 0; i < WI_TIMEOUT; i++) {
2201 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
2202 break;
2203 if (i == WI_TIMEOUT) {
2204 printf("%s: timeout in alloc\n", sc->sc_dev.dv_xname);
2205 return ETIMEDOUT;
2206 }
2207 DELAY(1);
2208 }
2209 *idp = CSR_READ_2(sc, WI_ALLOC_FID);
2210 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
2211 return 0;
2212 }
2213
2214 static int
2215 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
2216 {
2217 int error, len;
2218 u_int16_t ltbuf[2];
2219
2220 /* Tell the NIC to enter record read mode. */
2221 error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2222 if (error)
2223 return error;
2224
2225 error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2226 if (error)
2227 return error;
2228
2229 if (le16toh(ltbuf[1]) != rid) {
2230 printf("%s: record read mismatch, rid=%x, got=%x\n",
2231 sc->sc_dev.dv_xname, rid, le16toh(ltbuf[1]));
2232 return EIO;
2233 }
2234 len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */
2235 if (*buflenp < len) {
2236 printf("%s: record buffer is too small, "
2237 "rid=%x, size=%d, len=%d\n",
2238 sc->sc_dev.dv_xname, rid, *buflenp, len);
2239 return ENOSPC;
2240 }
2241 *buflenp = len;
2242 return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2243 }
2244
2245 static int
2246 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2247 {
2248 int error;
2249 u_int16_t ltbuf[2];
2250
2251 ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */
2252 ltbuf[1] = htole16(rid);
2253
2254 error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2255 if (error)
2256 return error;
2257 error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2258 if (error)
2259 return error;
2260
2261 return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
2262 }
2263
2264 static int
2265 wi_newstate(void *arg, enum ieee80211_state nstate)
2266 {
2267 struct wi_softc *sc = arg;
2268 struct ieee80211com *ic = &sc->sc_ic;
2269 struct ieee80211_node *ni = &ic->ic_bss;
2270 int i, buflen;
2271 u_int16_t val;
2272 struct wi_ssid ssid;
2273 u_int8_t old_bssid[IEEE80211_ADDR_LEN];
2274 enum ieee80211_state ostate;
2275 #ifdef WI_DEBUG
2276 static const char *stname[] =
2277 { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" };
2278 #endif /* WI_DEBUG */
2279
2280 ostate = ic->ic_state;
2281 DPRINTF(("wi_newstate: %s -> %s\n", stname[ostate], stname[nstate]));
2282
2283 ic->ic_state = nstate;
2284 switch (nstate) {
2285 case IEEE80211_S_INIT:
2286 ic->ic_flags &= ~IEEE80211_F_SIBSS;
2287 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2288 return 0;
2289
2290 case IEEE80211_S_RUN:
2291 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2292 buflen = IEEE80211_ADDR_LEN;
2293 IEEE80211_ADDR_COPY(old_bssid, ni->ni_bssid);
2294 wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen);
2295 IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
2296 buflen = sizeof(val);
2297 wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
2298 ni->ni_chan = le16toh(val);
2299
2300 if (IEEE80211_ADDR_EQ(old_bssid, ni->ni_bssid))
2301 sc->sc_false_syns++;
2302 else
2303 sc->sc_false_syns = 0;
2304
2305 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2306 ni->ni_esslen = ic->ic_des_esslen;
2307 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
2308 ni->ni_nrate = 0;
2309 for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
2310 if (ic->ic_sup_rates[i])
2311 ni->ni_rates[ni->ni_nrate++] =
2312 ic->ic_sup_rates[i];
2313 }
2314 ni->ni_intval = ic->ic_lintval;
2315 ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
2316 if (ic->ic_flags & IEEE80211_F_WEPON)
2317 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
2318 } else {
2319 buflen = sizeof(ssid);
2320 wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
2321 ni->ni_esslen = le16toh(ssid.wi_len);
2322 if (ni->ni_esslen > IEEE80211_NWID_LEN)
2323 ni->ni_esslen = IEEE80211_NWID_LEN; /*XXX*/
2324 memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
2325 }
2326 break;
2327
2328 case IEEE80211_S_SCAN:
2329 case IEEE80211_S_AUTH:
2330 case IEEE80211_S_ASSOC:
2331 break;
2332 }
2333
2334 /* skip standard ieee80211 handling */
2335 return EINPROGRESS;
2336 }
2337
2338 static int
2339 wi_set_tim(struct ieee80211com *ic, int aid, int which)
2340 {
2341 struct wi_softc *sc = ic->ic_softc;
2342
2343 aid &= ~0xc000;
2344 if (which)
2345 aid |= 0x8000;
2346
2347 return wi_write_val(sc, WI_RID_SET_TIM, aid);
2348 }
2349
2350 static int
2351 wi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate)
2352 {
2353 int error = 0;
2354 u_int16_t val[2];
2355
2356 if (!sc->sc_enabled)
2357 return ENXIO;
2358 switch (sc->sc_firmware_type) {
2359 case WI_LUCENT:
2360 (void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
2361 break;
2362 case WI_INTERSIL:
2363 val[0] = chanmask; /* channel */
2364 val[1] = txrate; /* tx rate */
2365 error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
2366 break;
2367 case WI_SYMBOL:
2368 /*
2369 * XXX only supported on 3.x ?
2370 */
2371 val[0] = BSCAN_BCAST | BSCAN_ONETIME;
2372 error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
2373 val, sizeof(val[0]));
2374 break;
2375 }
2376 if (error == 0) {
2377 sc->sc_scan_timer = WI_SCAN_WAIT;
2378 sc->sc_ic.ic_if.if_timer = 1;
2379 DPRINTF(("wi_scan_ap: start scanning, "
2380 "chanmask 0x%x txrate 0x%x\n", chanmask, txrate));
2381 }
2382 return error;
2383 }
2384
2385 static void
2386 wi_scan_result(struct wi_softc *sc, int fid, int cnt)
2387 {
2388 #define N(a) (sizeof (a) / sizeof (a[0]))
2389 int i, naps, off, szbuf;
2390 struct wi_scan_header ws_hdr; /* Prism2 header */
2391 struct wi_scan_data_p2 ws_dat; /* Prism2 scantable*/
2392 struct wi_apinfo *ap;
2393
2394 off = sizeof(u_int16_t) * 2;
2395 memset(&ws_hdr, 0, sizeof(ws_hdr));
2396 switch (sc->sc_firmware_type) {
2397 case WI_INTERSIL:
2398 wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
2399 off += sizeof(ws_hdr);
2400 szbuf = sizeof(struct wi_scan_data_p2);
2401 break;
2402 case WI_SYMBOL:
2403 szbuf = sizeof(struct wi_scan_data_p2) + 6;
2404 break;
2405 case WI_LUCENT:
2406 szbuf = sizeof(struct wi_scan_data);
2407 break;
2408 default:
2409 printf("%s: wi_scan_result: unknown firmware type %u\n",
2410 sc->sc_dev.dv_xname, sc->sc_firmware_type);
2411 naps = 0;
2412 goto done;
2413 }
2414 naps = (cnt * 2 + 2 - off) / szbuf;
2415 if (naps > N(sc->sc_aps))
2416 naps = N(sc->sc_aps);
2417 sc->sc_naps = naps;
2418 /* Read Data */
2419 ap = sc->sc_aps;
2420 memset(&ws_dat, 0, sizeof(ws_dat));
2421 for (i = 0; i < naps; i++, ap++) {
2422 wi_read_bap(sc, fid, off, &ws_dat,
2423 (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
2424 DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off,
2425 ether_sprintf(ws_dat.wi_bssid)));
2426 off += szbuf;
2427 ap->scanreason = le16toh(ws_hdr.wi_reason);
2428 memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
2429 ap->channel = le16toh(ws_dat.wi_chid);
2430 ap->signal = le16toh(ws_dat.wi_signal);
2431 ap->noise = le16toh(ws_dat.wi_noise);
2432 ap->quality = ap->signal - ap->noise;
2433 ap->capinfo = le16toh(ws_dat.wi_capinfo);
2434 ap->interval = le16toh(ws_dat.wi_interval);
2435 ap->rate = le16toh(ws_dat.wi_rate);
2436 ap->namelen = le16toh(ws_dat.wi_namelen);
2437 if (ap->namelen > sizeof(ap->name))
2438 ap->namelen = sizeof(ap->name);
2439 memcpy(ap->name, ws_dat.wi_name, ap->namelen);
2440 }
2441 done:
2442 /* Done scanning */
2443 sc->sc_scan_timer = 0;
2444 DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
2445 #undef N
2446 }
2447
2448 static void
2449 wi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi)
2450 {
2451 ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr),
2452 ni ? ni->ni_rates[ni->ni_txrate] & IEEE80211_RATE_VAL : -1, rssi);
2453 printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n",
2454 le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1),
2455 le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence);
2456 printf(" rx_signal %u rx_rate %u rx_flow %u\n",
2457 wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow);
2458 printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n",
2459 wh->wi_tx_rtry, wh->wi_tx_rate,
2460 le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len));
2461 printf(" ehdr dst %s src %s type 0x%x\n",
2462 ether_sprintf(wh->wi_ehdr.ether_dhost),
2463 ether_sprintf(wh->wi_ehdr.ether_shost),
2464 wh->wi_ehdr.ether_type);
2465 }
2466