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