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