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