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