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