if_wi.c revision 164033
1109323Ssam/*	$NetBSD: wi.c,v 1.109 2003/01/09 08:52:19 dyoung Exp $	*/
2109323Ssam
3139749Simp/*-
446492Swpaul * Copyright (c) 1997, 1998, 1999
546492Swpaul *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
646492Swpaul *
746492Swpaul * Redistribution and use in source and binary forms, with or without
846492Swpaul * modification, are permitted provided that the following conditions
946492Swpaul * are met:
1046492Swpaul * 1. Redistributions of source code must retain the above copyright
1146492Swpaul *    notice, this list of conditions and the following disclaimer.
1246492Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1346492Swpaul *    notice, this list of conditions and the following disclaimer in the
1446492Swpaul *    documentation and/or other materials provided with the distribution.
1546492Swpaul * 3. All advertising materials mentioning features or use of this software
1646492Swpaul *    must display the following acknowledgement:
1746492Swpaul *	This product includes software developed by Bill Paul.
1846492Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1946492Swpaul *    may be used to endorse or promote products derived from this software
2046492Swpaul *    without specific prior written permission.
2146492Swpaul *
2246492Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2346492Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2446492Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2546492Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2646492Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2746492Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2846492Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2946492Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3046492Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3146492Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3246492Swpaul * THE POSSIBILITY OF SUCH DAMAGE.
3346492Swpaul */
3446492Swpaul
3546492Swpaul/*
36109323Ssam * Lucent WaveLAN/IEEE 802.11 PCMCIA driver.
3746492Swpaul *
38109323Ssam * Original FreeBSD driver written by Bill Paul <wpaul@ctr.columbia.edu>
3946492Swpaul * Electrical Engineering Department
4046492Swpaul * Columbia University, New York City
4146492Swpaul */
4246492Swpaul
4346492Swpaul/*
4447401Swpaul * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
4546492Swpaul * from Lucent. Unlike the older cards, the new ones are programmed
4646492Swpaul * entirely via a firmware-driven controller called the Hermes.
4746492Swpaul * Unfortunately, Lucent will not release the Hermes programming manual
4846492Swpaul * without an NDA (if at all). What they do release is an API library
4946492Swpaul * called the HCF (Hardware Control Functions) which is supposed to
5046492Swpaul * do the device-specific operations of a device driver for you. The
5146492Swpaul * publically available version of the HCF library (the 'HCF Light') is
5247401Swpaul * a) extremely gross, b) lacks certain features, particularly support
5346492Swpaul * for 802.11 frames, and c) is contaminated by the GNU Public License.
5446492Swpaul *
5546492Swpaul * This driver does not use the HCF or HCF Light at all. Instead, it
5646492Swpaul * programs the Hermes controller directly, using information gleaned
5746492Swpaul * from the HCF Light code and corresponding documentation.
5846492Swpaul *
5995534Simp * This driver supports the ISA, PCMCIA and PCI versions of the Lucent
6095534Simp * WaveLan cards (based on the Hermes chipset), as well as the newer
6195534Simp * Prism 2 chipsets with firmware from Intersil and Symbol.
6246492Swpaul */
6346492Swpaul
64113038Sobrien#include <sys/cdefs.h>
65113038Sobrien__FBSDID("$FreeBSD: head/sys/dev/wi/if_wi.c 164033 2006-11-06 13:42:10Z rwatson $");
66113038Sobrien
67109323Ssam#define WI_HERMES_AUTOINC_WAR	/* Work around data write autoinc bug. */
68109323Ssam#define WI_HERMES_STATS_WAR	/* Work around stats counter bug. */
69109323Ssam
70109323Ssam#define	NBPFILTER	1
71109323Ssam
7246492Swpaul#include <sys/param.h>
7346492Swpaul#include <sys/systm.h>
7495706Simp#if __FreeBSD_version >= 500033
7595533Smike#include <sys/endian.h>
7695706Simp#endif
7746492Swpaul#include <sys/sockio.h>
7846492Swpaul#include <sys/mbuf.h>
79164033Srwatson#include <sys/priv.h>
8083366Sjulian#include <sys/proc.h>
8146492Swpaul#include <sys/kernel.h>
8246492Swpaul#include <sys/socket.h>
8353702Swpaul#include <sys/module.h>
8453702Swpaul#include <sys/bus.h>
8594486Simp#include <sys/random.h>
8653702Swpaul#include <sys/syslog.h>
8753702Swpaul#include <sys/sysctl.h>
8846492Swpaul
8953702Swpaul#include <machine/bus.h>
9053702Swpaul#include <machine/resource.h>
91116951Ssam#include <machine/atomic.h>
9253702Swpaul#include <sys/rman.h>
9353702Swpaul
9446492Swpaul#include <net/if.h>
9546492Swpaul#include <net/if_arp.h>
9646492Swpaul#include <net/ethernet.h>
9746492Swpaul#include <net/if_dl.h>
9846492Swpaul#include <net/if_media.h>
9946492Swpaul#include <net/if_types.h>
10046492Swpaul
101116951Ssam#include <net80211/ieee80211_var.h>
102116951Ssam#include <net80211/ieee80211_ioctl.h>
103119784Ssam#include <net80211/ieee80211_radiotap.h>
104116951Ssam
10546492Swpaul#include <netinet/in.h>
10646492Swpaul#include <netinet/in_systm.h>
10746492Swpaul#include <netinet/in_var.h>
10846492Swpaul#include <netinet/ip.h>
10946492Swpaul#include <netinet/if_ether.h>
11046492Swpaul
11146492Swpaul#include <net/bpf.h>
11246492Swpaul
11370808Speter#include <dev/wi/if_wavelan_ieee.h>
114119784Ssam#include <dev/wi/if_wireg.h>
11593611Simp#include <dev/wi/if_wivar.h>
11646492Swpaul
11791693Simpstatic void wi_start(struct ifnet *);
118160991Ssamstatic int  wi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr,
119160991Ssam		struct mbuf *m0);
120160991Ssamstatic int  wi_raw_xmit(struct ieee80211_node *, struct mbuf *,
121160991Ssam		const struct ieee80211_bpf_params *);
122109323Ssamstatic int  wi_reset(struct wi_softc *);
12391693Simpstatic void wi_watchdog(struct ifnet *);
124109323Ssamstatic int  wi_ioctl(struct ifnet *, u_long, caddr_t);
125109323Ssamstatic int  wi_media_change(struct ifnet *);
126109323Ssamstatic void wi_media_status(struct ifnet *, struct ifmediareq *);
12746492Swpaul
128109323Ssamstatic void wi_rx_intr(struct wi_softc *);
129109323Ssamstatic void wi_tx_intr(struct wi_softc *);
130109323Ssamstatic void wi_tx_ex_intr(struct wi_softc *);
131109323Ssamstatic void wi_info_intr(struct wi_softc *);
13246492Swpaul
133150798Savatarstatic int  wi_key_alloc(struct ieee80211com *, const struct ieee80211_key *,
134150798Savatar		ieee80211_keyix *, ieee80211_keyix *);
135150798Savatar
136109323Ssamstatic int  wi_get_cfg(struct ifnet *, u_long, caddr_t);
137109323Ssamstatic int  wi_set_cfg(struct ifnet *, u_long, caddr_t);
138109323Ssamstatic int  wi_write_txrate(struct wi_softc *);
139109323Ssamstatic int  wi_write_wep(struct wi_softc *);
140109323Ssamstatic int  wi_write_multi(struct wi_softc *);
141109323Ssamstatic int  wi_alloc_fid(struct wi_softc *, int, int *);
142109323Ssamstatic void wi_read_nicid(struct wi_softc *);
143109323Ssamstatic int  wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
14453702Swpaul
145109323Ssamstatic int  wi_cmd(struct wi_softc *, int, int, int, int);
146109323Ssamstatic int  wi_seek_bap(struct wi_softc *, int, int);
147109323Ssamstatic int  wi_read_bap(struct wi_softc *, int, int, void *, int);
148109323Ssamstatic int  wi_write_bap(struct wi_softc *, int, int, void *, int);
149109323Ssamstatic int  wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
150109323Ssamstatic int  wi_read_rid(struct wi_softc *, int, void *, int *);
151109323Ssamstatic int  wi_write_rid(struct wi_softc *, int, void *, int);
15277217Sphk
153117812Ssamstatic int  wi_newstate(struct ieee80211com *, enum ieee80211_state, int);
154109323Ssam
155116898Ssamstatic int  wi_scan_ap(struct wi_softc *, u_int16_t, u_int16_t);
156109323Ssamstatic void wi_scan_result(struct wi_softc *, int, int);
157109323Ssam
158109323Ssamstatic void wi_dump_pkt(struct wi_frame *, struct ieee80211_node *, int rssi);
159109323Ssam
16093359Simpstatic int wi_get_debug(struct wi_softc *, struct wi_req *);
16193359Simpstatic int wi_set_debug(struct wi_softc *, struct wi_req *);
16293359Simp
163105076Simp#if __FreeBSD_version >= 500000
164101903Simp/* support to download firmware for symbol CF card */
165101903Simpstatic int wi_symbol_write_firm(struct wi_softc *, const void *, int,
166101903Simp		const void *, int);
167101903Simpstatic int wi_symbol_set_hcr(struct wi_softc *, int);
168105076Simp#endif
169101903Simp
170109323Ssamstatic __inline int
171109323Ssamwi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
172109323Ssam{
17353702Swpaul
174109323Ssam	val = htole16(val);
175109323Ssam	return wi_write_rid(sc, rid, &val, sizeof(val));
176109323Ssam}
177109323Ssam
178109592SsamSYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters");
179109592Ssam
180109323Ssamstatic	struct timeval lasttxerror;	/* time of last tx error msg */
181109323Ssamstatic	int curtxeps;			/* current tx error msgs/sec */
182111559Ssamstatic	int wi_txerate = 0;		/* tx error rate: max msgs/sec */
183109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate,
184111559Ssam	    0, "max tx error msgs/sec; 0 to disable msgs");
185109323Ssam
186109323Ssam#define	WI_DEBUG
187109323Ssam#ifdef WI_DEBUG
188109323Ssamstatic	int wi_debug = 0;
189109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug,
190109592Ssam	    0, "control debugging printfs");
191109323Ssam
192109323Ssam#define	DPRINTF(X)	if (wi_debug) printf X
193109323Ssam#define	DPRINTF2(X)	if (wi_debug > 1) printf X
194109323Ssam#define	IFF_DUMPPKTS(_ifp) \
195109323Ssam	(((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
196109323Ssam#else
197109323Ssam#define	DPRINTF(X)
198109323Ssam#define	DPRINTF2(X)
199109323Ssam#define	IFF_DUMPPKTS(_ifp)	0
200109323Ssam#endif
201109323Ssam
202109323Ssam#define WI_INTRS	(WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
203109323Ssam
20493825Simpstruct wi_card_ident wi_card_ident[] = {
20593825Simp	/* CARD_ID			CARD_NAME		FIRM_TYPE */
20693825Simp	{ WI_NIC_LUCENT_ID,		WI_NIC_LUCENT_STR,	WI_LUCENT },
20793825Simp	{ WI_NIC_SONY_ID,		WI_NIC_SONY_STR,	WI_LUCENT },
20893825Simp	{ WI_NIC_LUCENT_EMB_ID,		WI_NIC_LUCENT_EMB_STR,	WI_LUCENT },
20993825Simp	{ WI_NIC_EVB2_ID,		WI_NIC_EVB2_STR,	WI_INTERSIL },
21093825Simp	{ WI_NIC_HWB3763_ID,		WI_NIC_HWB3763_STR,	WI_INTERSIL },
21193825Simp	{ WI_NIC_HWB3163_ID,		WI_NIC_HWB3163_STR,	WI_INTERSIL },
21293825Simp	{ WI_NIC_HWB3163B_ID,		WI_NIC_HWB3163B_STR,	WI_INTERSIL },
21393825Simp	{ WI_NIC_EVB3_ID,		WI_NIC_EVB3_STR,	WI_INTERSIL },
21493825Simp	{ WI_NIC_HWB1153_ID,		WI_NIC_HWB1153_STR,	WI_INTERSIL },
21593825Simp	{ WI_NIC_P2_SST_ID,		WI_NIC_P2_SST_STR,	WI_INTERSIL },
21693825Simp	{ WI_NIC_EVB2_SST_ID,		WI_NIC_EVB2_SST_STR,	WI_INTERSIL },
21793825Simp	{ WI_NIC_3842_EVA_ID,		WI_NIC_3842_EVA_STR,	WI_INTERSIL },
21893825Simp	{ WI_NIC_3842_PCMCIA_AMD_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
21993825Simp	{ WI_NIC_3842_PCMCIA_SST_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
220101355Simp	{ WI_NIC_3842_PCMCIA_ATL_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
221101355Simp	{ WI_NIC_3842_PCMCIA_ATS_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
22293825Simp	{ WI_NIC_3842_MINI_AMD_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
22393825Simp	{ WI_NIC_3842_MINI_SST_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
224101355Simp	{ WI_NIC_3842_MINI_ATL_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
225101355Simp	{ WI_NIC_3842_MINI_ATS_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
22693825Simp	{ WI_NIC_3842_PCI_AMD_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
22793825Simp	{ WI_NIC_3842_PCI_SST_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
228101355Simp	{ WI_NIC_3842_PCI_ATS_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
229101355Simp	{ WI_NIC_3842_PCI_ATL_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
23093825Simp	{ WI_NIC_P3_PCMCIA_AMD_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
23193825Simp	{ WI_NIC_P3_PCMCIA_SST_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
232101355Simp	{ WI_NIC_P3_PCMCIA_ATL_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
233101355Simp	{ WI_NIC_P3_PCMCIA_ATS_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
23493825Simp	{ WI_NIC_P3_MINI_AMD_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
23593825Simp	{ WI_NIC_P3_MINI_SST_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
236101355Simp	{ WI_NIC_P3_MINI_ATL_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
237101355Simp	{ WI_NIC_P3_MINI_ATS_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
23893825Simp	{ 0,	NULL,	0 },
23993825Simp};
24093825Simp
241109323Ssamdevclass_t wi_devclass;
24246492Swpaul
24393611Simpint
244109323Ssamwi_attach(device_t dev)
24574906Salfred{
246109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
247109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
248147256Sbrooks	struct ifnet *ifp;
249116951Ssam	int i, nrates, buflen;
250109323Ssam	u_int16_t val;
251109323Ssam	u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
252116951Ssam	struct ieee80211_rateset *rs;
253109323Ssam	static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
254109323Ssam		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
255109323Ssam	};
256109323Ssam	int error;
25774906Salfred
258147256Sbrooks	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
259147256Sbrooks	if (ifp == NULL) {
260147256Sbrooks		device_printf(dev, "can not if_alloc\n");
261147256Sbrooks		wi_free(dev);
262147256Sbrooks		return (ENOSPC);
263147256Sbrooks	}
264147256Sbrooks
265109323Ssam	/*
266109323Ssam	 * NB: no locking is needed here; don't put it here
267109323Ssam	 *     unless you can prove it!
268109323Ssam	 */
269121697Ssam	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
27074998Swpaul	    wi_intr, sc, &sc->wi_intrhand);
27153702Swpaul
27253702Swpaul	if (error) {
27353702Swpaul		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
27453702Swpaul		wi_free(dev);
27553702Swpaul		return (error);
27653702Swpaul	}
27753702Swpaul
27895534Simp#if __FreeBSD_version >= 500000
279109323Ssam	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
28093818Sjhb	    MTX_DEF | MTX_RECURSE);
28195534Simp#endif
28267092Swpaul
283138571Ssam	sc->sc_firmware_type = WI_NOTYPE;
284123339Simp	sc->wi_cmd_count = 500;
28546492Swpaul	/* Reset the NIC. */
286112096Simp	if (wi_reset(sc) != 0)
287109323Ssam		return ENXIO;		/* XXX */
28846492Swpaul
28976438Swpaul	/*
29076438Swpaul	 * Read the station address.
29176438Swpaul	 * And do it twice. I've seen PRISM-based cards that return
29276438Swpaul	 * an error when trying to read it the first time, which causes
29376438Swpaul	 * the probe to fail.
29476438Swpaul	 */
295109323Ssam	buflen = IEEE80211_ADDR_LEN;
296109323Ssam	error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
297109323Ssam	if (error != 0) {
298109323Ssam		buflen = IEEE80211_ADDR_LEN;
299109323Ssam		error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
300109323Ssam	}
301109323Ssam	if (error || IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) {
302109323Ssam		if (error != 0)
303109323Ssam			device_printf(dev, "mac read failed %d\n", error);
304148714Simp		else {
305109323Ssam			device_printf(dev, "mac read failed (all zeros)\n");
306148714Simp			error = ENXIO;
307148714Simp		}
30875149Simp		wi_free(dev);
30975149Simp		return (error);
31075149Simp	}
31146492Swpaul
312109323Ssam	/* Read NIC identification */
313109323Ssam	wi_read_nicid(sc);
31446492Swpaul
31546492Swpaul	ifp->if_softc = sc;
316121816Sbrooks	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
31746492Swpaul	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
31846492Swpaul	ifp->if_ioctl = wi_ioctl;
31946492Swpaul	ifp->if_start = wi_start;
32046492Swpaul	ifp->if_watchdog = wi_watchdog;
32146492Swpaul	ifp->if_init = wi_init;
322132986Smlaier	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
323132986Smlaier	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
324132986Smlaier	IFQ_SET_READY(&ifp->if_snd);
32546492Swpaul
326138571Ssam	ic->ic_ifp = ifp;
327109323Ssam	ic->ic_phytype = IEEE80211_T_DS;
328109323Ssam	ic->ic_opmode = IEEE80211_M_STA;
329109323Ssam	ic->ic_state = IEEE80211_S_INIT;
330138571Ssam	ic->ic_caps = IEEE80211_C_PMGT
331138571Ssam		    | IEEE80211_C_WEP		/* everyone supports WEP */
332138571Ssam		    ;
333138571Ssam	ic->ic_max_aid = WI_MAX_AID;
33446492Swpaul
335116951Ssam	/*
336116951Ssam	 * Query the card for available channels and setup the
337116951Ssam	 * channel table.  We assume these are all 11b channels.
338116951Ssam	 */
339109323Ssam	buflen = sizeof(val);
340109323Ssam	if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
341109323Ssam		val = htole16(0x1fff);	/* assume 1-11 */
342116951Ssam	KASSERT(val != 0, ("wi_attach: no available channels listed!"));
343116951Ssam
344116951Ssam	val <<= 1;			/* shift for base 1 indices */
345116951Ssam	for (i = 1; i < 16; i++) {
346144986Smdodd		if (!isset((u_int8_t*)&val, i))
347144986Smdodd			continue;
348144986Smdodd		ic->ic_channels[i].ic_freq =
349144986Smdodd			ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
350144986Smdodd		ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
351109323Ssam	}
35246492Swpaul
35346563Swpaul	/*
35446563Swpaul	 * Read the default channel from the NIC. This may vary
35546563Swpaul	 * depending on the country where the NIC was purchased, so
35646563Swpaul	 * we can't hard-code a default and expect it to work for
35746563Swpaul	 * everyone.
358116951Ssam	 *
359116951Ssam	 * If no channel is specified, let the 802.11 code select.
36046563Swpaul	 */
361109323Ssam	buflen = sizeof(val);
362116951Ssam	if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0) {
363116951Ssam		val = le16toh(val);
364116951Ssam		KASSERT(val < IEEE80211_CHAN_MAX &&
365116951Ssam			ic->ic_channels[val].ic_flags != 0,
366116951Ssam			("wi_attach: invalid own channel %u!", val));
367116951Ssam		ic->ic_ibss_chan = &ic->ic_channels[val];
368117042Ssam	} else {
369117042Ssam		device_printf(dev,
370117042Ssam			"WI_RID_OWN_CHNL failed, using first channel!\n");
371117042Ssam		ic->ic_ibss_chan = &ic->ic_channels[0];
372109323Ssam	}
37346563Swpaul
37456965Swpaul	/*
37598440Simp	 * Set flags based on firmware version.
37698440Simp	 */
37798440Simp	switch (sc->sc_firmware_type) {
37898440Simp	case WI_LUCENT:
379112363Simp		sc->sc_ntxbuf = 1;
380109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
381109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
382109323Ssam		/* XXX: not confirmed, but never seen for recent firmware */
383109323Ssam		if (sc->sc_sta_firmware_ver <  40000) {
384109323Ssam			sc->sc_flags |= WI_FLAGS_BUG_AUTOINC;
385109323Ssam		}
386109323Ssam#endif
38798440Simp		if (sc->sc_sta_firmware_ver >= 60000)
388109323Ssam			sc->sc_flags |= WI_FLAGS_HAS_MOR;
389117855Ssam		if (sc->sc_sta_firmware_ver >= 60006) {
390116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
391117855Ssam			ic->ic_caps |= IEEE80211_C_MONITOR;
392117855Ssam		}
393109323Ssam		sc->sc_ibss_port = htole16(1);
394119784Ssam
395119784Ssam		sc->sc_min_rssi = WI_LUCENT_MIN_RSSI;
396119784Ssam		sc->sc_max_rssi = WI_LUCENT_MAX_RSSI;
397119784Ssam		sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
39898440Simp		break;
399109323Ssam
40098440Simp	case WI_INTERSIL:
401112363Simp		sc->sc_ntxbuf = WI_NTXBUF;
402109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR;
403109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_ROAMING;
404109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
405123339Simp		/*
406123339Simp		 * Old firmware are slow, so give peace a chance.
407123339Simp		 */
408123339Simp		if (sc->sc_sta_firmware_ver < 10000)
409123339Simp			sc->wi_cmd_count = 5000;
410109323Ssam		if (sc->sc_sta_firmware_ver > 10101)
411109323Ssam			sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
412117855Ssam		if (sc->sc_sta_firmware_ver >= 800) {
413116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
414117855Ssam			ic->ic_caps |= IEEE80211_C_MONITOR;
415117855Ssam		}
416109396Simp		/*
417109396Simp		 * version 0.8.3 and newer are the only ones that are known
418109396Simp		 * to currently work.  Earlier versions can be made to work,
419109396Simp		 * at least according to the Linux driver.
420109396Simp		 */
421100734Simp		if (sc->sc_sta_firmware_ver >= 803)
422116951Ssam			ic->ic_caps |= IEEE80211_C_HOSTAP;
423109323Ssam		sc->sc_ibss_port = htole16(0);
424119784Ssam
425119784Ssam		sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
426119784Ssam		sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
427119784Ssam		sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
42898440Simp		break;
429109323Ssam
43098440Simp	case WI_SYMBOL:
431112363Simp		sc->sc_ntxbuf = 1;
432109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY;
43398440Simp		if (sc->sc_sta_firmware_ver >= 25000)
434116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
435109323Ssam		sc->sc_ibss_port = htole16(4);
436119784Ssam
437119784Ssam		sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
438119784Ssam		sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
439119784Ssam		sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
44098440Simp		break;
44198440Simp	}
44298440Simp
44398440Simp	/*
44456965Swpaul	 * Find out if we support WEP on this card.
44556965Swpaul	 */
446109323Ssam	buflen = sizeof(val);
447109323Ssam	if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
448109323Ssam	    val != htole16(0))
449116951Ssam		ic->ic_caps |= IEEE80211_C_WEP;
45056965Swpaul
451109323Ssam	/* Find supported rates. */
452109323Ssam	buflen = sizeof(ratebuf);
453116951Ssam	rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
454109323Ssam	if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
455116951Ssam		nrates = le16toh(*(u_int16_t *)ratebuf);
456116951Ssam		if (nrates > IEEE80211_RATE_MAXSIZE)
457116951Ssam			nrates = IEEE80211_RATE_MAXSIZE;
458116951Ssam		rs->rs_nrates = 0;
459116951Ssam		for (i = 0; i < nrates; i++)
460116951Ssam			if (ratebuf[2+i])
461116951Ssam				rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i];
462109323Ssam	} else {
463109323Ssam		/* XXX fallback on error? */
464116951Ssam		rs->rs_nrates = 0;
46598440Simp	}
46677217Sphk
467109323Ssam	buflen = sizeof(val);
468109323Ssam	if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
469109323Ssam	    wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
470119784Ssam		sc->sc_dbm_offset = le16toh(val);
471119784Ssam	}
47246492Swpaul
473109323Ssam	sc->sc_max_datalen = 2304;
474109323Ssam	sc->sc_system_scale = 1;
475109323Ssam	sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN;
476109323Ssam	sc->sc_roaming_mode = 1;
47746492Swpaul
478109323Ssam	sc->sc_portnum = WI_DEFAULT_PORT;
479109323Ssam	sc->sc_authtype = WI_DEFAULT_AUTHTYPE;
48087383Simp
481109323Ssam	bzero(sc->sc_nodename, sizeof(sc->sc_nodename));
482109323Ssam	sc->sc_nodelen = sizeof(WI_DEFAULT_NODENAME) - 1;
483109323Ssam	bcopy(WI_DEFAULT_NODENAME, sc->sc_nodename, sc->sc_nodelen);
48487383Simp
485109323Ssam	bzero(sc->sc_net_name, sizeof(sc->sc_net_name));
486109323Ssam	bcopy(WI_DEFAULT_NETNAME, sc->sc_net_name,
487109323Ssam	    sizeof(WI_DEFAULT_NETNAME) - 1);
48893733Simp
48993733Simp	/*
490109323Ssam	 * Call MI attach routine.
49193733Simp	 */
492138571Ssam	ieee80211_ifattach(ic);
493117812Ssam	/* override state transition method */
494117812Ssam	sc->sc_newstate = ic->ic_newstate;
495150798Savatar	sc->sc_key_alloc = ic->ic_crypto.cs_key_alloc;
496150798Savatar	ic->ic_crypto.cs_key_alloc = wi_key_alloc;
497117812Ssam	ic->ic_newstate = wi_newstate;
498160991Ssam	ic->ic_raw_xmit = wi_raw_xmit;
499138571Ssam	ieee80211_media_init(ic, wi_media_change, wi_media_status);
500109323Ssam
501119784Ssam#if NBPFILTER > 0
502119784Ssam	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
503119784Ssam		sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
504119784Ssam		&sc->sc_drvbpf);
505119784Ssam	/*
506119784Ssam	 * Initialize constant fields.
507127698Ssam	 * XXX make header lengths a multiple of 32-bits so subsequent
508127698Ssam	 *     headers are properly aligned; this is a kludge to keep
509127698Ssam	 *     certain applications happy.
510119784Ssam	 *
511119784Ssam	 * NB: the channel is setup each time we transition to the
512119784Ssam	 *     RUN state to avoid filling it in for each frame.
513119784Ssam	 */
514127698Ssam	sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t));
515127698Ssam	sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len);
516127698Ssam	sc->sc_tx_th.wt_ihdr.it_present = htole32(WI_TX_RADIOTAP_PRESENT);
517119784Ssam
518127698Ssam	sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t));
519127698Ssam	sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len);
520127698Ssam	sc->sc_rx_th.wr_ihdr.it_present = htole32(WI_RX_RADIOTAP_PRESENT);
521119784Ssam#endif
522138571Ssam
523138571Ssam	if (bootverbose)
524138571Ssam		ieee80211_announce(ic);
525138571Ssam
526109323Ssam	return (0);
52787383Simp}
52887383Simp
529109323Ssamint
530109323Ssamwi_detach(device_t dev)
53146492Swpaul{
532109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
533147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
534109323Ssam	WI_LOCK_DECL();
53546492Swpaul
536109323Ssam	WI_LOCK(sc);
53746492Swpaul
538109323Ssam	/* check if device was removed */
539123098Simp	sc->wi_gone |= !bus_child_present(dev);
54046492Swpaul
541109323Ssam	wi_stop(ifp, 0);
542150678Sru	WI_UNLOCK(sc);
54346492Swpaul
544119784Ssam#if NBPFILTER > 0
545119784Ssam	bpfdetach(ifp);
546119784Ssam#endif
547138571Ssam	ieee80211_ifdetach(&sc->sc_ic);
548109323Ssam	bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
549150306Simp	if_free(sc->sc_ifp);
550109323Ssam	wi_free(dev);
551109323Ssam#if __FreeBSD_version >= 500000
552109323Ssam	mtx_destroy(&sc->sc_mtx);
55393359Simp#endif
554109323Ssam	return (0);
555109323Ssam}
55693359Simp
557109323Ssam#ifdef __NetBSD__
558109323Ssamint
559109323Ssamwi_activate(struct device *self, enum devact act)
560109323Ssam{
561109323Ssam	struct wi_softc *sc = (struct wi_softc *)self;
562109323Ssam	int rv = 0, s;
56393359Simp
564109323Ssam	s = splnet();
565109323Ssam	switch (act) {
566109323Ssam	case DVACT_ACTIVATE:
567109323Ssam		rv = EOPNOTSUPP;
568109323Ssam		break;
56993359Simp
570109323Ssam	case DVACT_DEACTIVATE:
571147256Sbrooks		if_deactivate(sc->sc_ifp);
572109323Ssam		break;
573109323Ssam	}
574109323Ssam	splx(s);
575109323Ssam	return rv;
576109323Ssam}
57793359Simp
578109323Ssamvoid
579109323Ssamwi_power(struct wi_softc *sc, int why)
580109323Ssam{
581147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
582109323Ssam	int s;
58393359Simp
584109323Ssam	s = splnet();
585109323Ssam	switch (why) {
586109323Ssam	case PWR_SUSPEND:
587109323Ssam	case PWR_STANDBY:
588109323Ssam		wi_stop(ifp, 1);
589109323Ssam		break;
590109323Ssam	case PWR_RESUME:
591109323Ssam		if (ifp->if_flags & IFF_UP) {
592109323Ssam			wi_init(ifp);
593109323Ssam			(void)wi_intr(sc);
59494405Simp		}
595109323Ssam		break;
596109323Ssam	case PWR_SOFTSUSPEND:
597109323Ssam	case PWR_SOFTSTANDBY:
598109323Ssam	case PWR_SOFTRESUME:
599109323Ssam		break;
60093359Simp	}
601109323Ssam	splx(s);
60246492Swpaul}
603109323Ssam#endif /* __NetBSD__ */
60446492Swpaul
605109323Ssamvoid
606109323Ssamwi_shutdown(device_t dev)
60746492Swpaul{
608109323Ssam	struct wi_softc *sc = device_get_softc(dev);
60946492Swpaul
610147256Sbrooks	wi_stop(sc->sc_ifp, 1);
61146492Swpaul}
61246492Swpaul
613109323Ssamvoid
614109323Ssamwi_intr(void *arg)
61546492Swpaul{
616109323Ssam	struct wi_softc *sc = arg;
617147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
618112363Simp	u_int16_t status;
619109323Ssam	WI_LOCK_DECL();
62046492Swpaul
621109323Ssam	WI_LOCK(sc);
62246492Swpaul
623123098Simp	if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) {
624113327Simp		CSR_WRITE_2(sc, WI_INT_EN, 0);
625123098Simp		CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
626109323Ssam		WI_UNLOCK(sc);
62746492Swpaul		return;
628109323Ssam	}
62946492Swpaul
630112363Simp	/* Disable interrupts. */
631112363Simp	CSR_WRITE_2(sc, WI_INT_EN, 0);
63246492Swpaul
633112363Simp	status = CSR_READ_2(sc, WI_EVENT_STAT);
634112363Simp	if (status & WI_EV_RX)
635112363Simp		wi_rx_intr(sc);
636112363Simp	if (status & WI_EV_ALLOC)
637112363Simp		wi_tx_intr(sc);
638112363Simp	if (status & WI_EV_TX_EXC)
639112363Simp		wi_tx_ex_intr(sc);
640112363Simp	if (status & WI_EV_INFO)
641112363Simp		wi_info_intr(sc);
642148887Srwatson	if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
643112363Simp	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 &&
644132986Smlaier	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
645112363Simp		wi_start(ifp);
64646492Swpaul
647112363Simp	/* Re-enable interrupts. */
648112363Simp	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
64946492Swpaul
650109323Ssam	WI_UNLOCK(sc);
65146492Swpaul
65246492Swpaul	return;
65346492Swpaul}
65446492Swpaul
655109323Ssamvoid
656109323Ssamwi_init(void *arg)
65746492Swpaul{
658109323Ssam	struct wi_softc *sc = arg;
659147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
660109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
661109323Ssam	struct wi_joinreq join;
662109323Ssam	int i;
663109323Ssam	int error = 0, wasenabled;
664109323Ssam	WI_LOCK_DECL();
66546492Swpaul
666109323Ssam	WI_LOCK(sc);
66767092Swpaul
668109323Ssam	if (sc->wi_gone) {
669109323Ssam		WI_UNLOCK(sc);
67046492Swpaul		return;
67146492Swpaul	}
67246492Swpaul
673112362Simp	if ((wasenabled = sc->sc_enabled))
674123098Simp		wi_stop(ifp, 1);
675112362Simp	wi_reset(sc);
67646492Swpaul
677109323Ssam	/* common 802.11 configuration */
678109323Ssam	ic->ic_flags &= ~IEEE80211_F_IBSSON;
679109323Ssam	sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
680109323Ssam	switch (ic->ic_opmode) {
681109323Ssam	case IEEE80211_M_STA:
682109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS);
683109323Ssam		break;
684109323Ssam	case IEEE80211_M_IBSS:
685109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port);
686109323Ssam		ic->ic_flags |= IEEE80211_F_IBSSON;
687109323Ssam		break;
688109323Ssam	case IEEE80211_M_AHDEMO:
689109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
690109323Ssam		break;
691109323Ssam	case IEEE80211_M_HOSTAP:
692122087Sgreen		/*
693122087Sgreen		 * For PRISM cards, override the empty SSID, because in
694122087Sgreen		 * HostAP mode the controller will lock up otherwise.
695122087Sgreen		 */
696122087Sgreen		if (sc->sc_firmware_type == WI_INTERSIL &&
697122087Sgreen		    ic->ic_des_esslen == 0) {
698122087Sgreen			ic->ic_des_essid[0] = ' ';
699122087Sgreen			ic->ic_des_esslen = 1;
700122087Sgreen		}
701109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP);
702109323Ssam		break;
703117855Ssam	case IEEE80211_M_MONITOR:
704160991Ssam		switch (sc->sc_firmware_type) {
705160991Ssam		case WI_LUCENT:
706117855Ssam			wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
707160991Ssam			break;
708160991Ssam
709160991Ssam		case WI_INTERSIL:
710160991Ssam			wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_APSILENT);
711160991Ssam			break;
712160991Ssam		}
713160991Ssam
714117855Ssam		wi_cmd(sc, WI_CMD_DEBUG | (WI_TEST_MONITOR << 8), 0, 0, 0);
715117855Ssam		break;
71646492Swpaul	}
71746492Swpaul
718109323Ssam	/* Intersil interprets this RID as joining ESS even in IBSS mode */
719109323Ssam	if (sc->sc_firmware_type == WI_LUCENT &&
720109323Ssam	    (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0)
721109323Ssam		wi_write_val(sc, WI_RID_CREATE_IBSS, 1);
722109323Ssam	else
723109323Ssam		wi_write_val(sc, WI_RID_CREATE_IBSS, 0);
724109323Ssam	wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
725109323Ssam	wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid,
726109323Ssam	    ic->ic_des_esslen);
727116951Ssam	wi_write_val(sc, WI_RID_OWN_CHNL,
728116951Ssam		ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
729109323Ssam	wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen);
73046492Swpaul
731152315Sru	IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
732109323Ssam	wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN);
73375373Salfred
734138571Ssam	if (ic->ic_caps & IEEE80211_C_PMGT)
735138571Ssam		wi_write_val(sc, WI_RID_PM_ENABLED,
736138571Ssam		    (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
73746492Swpaul
738109323Ssam	/* not yet common 802.11 configuration */
739109323Ssam	wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen);
740116951Ssam	wi_write_val(sc, WI_RID_RTS_THRESH, ic->ic_rtsthreshold);
741109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
742116951Ssam		wi_write_val(sc, WI_RID_FRAG_THRESH, ic->ic_fragthreshold);
74346492Swpaul
744109323Ssam	/* driver specific 802.11 configuration */
745109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)
746109323Ssam		wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale);
747109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
748109323Ssam		wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode);
749109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_MOR)
750109323Ssam		wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven);
751109323Ssam	wi_write_txrate(sc);
752109323Ssam	wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen);
753160991Ssam	wi_write_val(sc, WI_RID_ALT_RETRY_CNT, 0); /* for IEEE80211_BPF_NOACK */
75446492Swpaul
755109323Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
756109323Ssam	    sc->sc_firmware_type == WI_INTERSIL) {
757148843Ssam		wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_bintval);
758109323Ssam		wi_write_val(sc, WI_RID_BASIC_RATE, 0x03);   /* 1, 2 */
759109323Ssam		wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */
760150797Savatar		wi_write_val(sc, WI_RID_DTIM_PERIOD, ic->ic_dtim_period);
76146492Swpaul	}
76246492Swpaul
763109323Ssam	/*
764109323Ssam	 * Initialize promisc mode.
765109323Ssam	 *	Being in the Host-AP mode causes a great
766109323Ssam	 *	deal of pain if primisc mode is set.
767109323Ssam	 *	Therefore we avoid confusing the firmware
768109323Ssam	 *	and always reset promisc mode in Host-AP
769109323Ssam	 *	mode.  Host-AP sees all the packets anyway.
770109323Ssam	 */
771109323Ssam	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
772109323Ssam	    (ifp->if_flags & IFF_PROMISC) != 0) {
773109323Ssam		wi_write_val(sc, WI_RID_PROMISC, 1);
774109323Ssam	} else {
775109323Ssam		wi_write_val(sc, WI_RID_PROMISC, 0);
77675373Salfred	}
77746492Swpaul
778109323Ssam	/* Configure WEP. */
779146884Savatar	if (ic->ic_caps & IEEE80211_C_WEP) {
780146884Savatar		sc->sc_cnfauthmode = ic->ic_bss->ni_authmode;
781109323Ssam		wi_write_wep(sc);
782150798Savatar	} else
783150798Savatar		sc->sc_encryption = 0;
78467092Swpaul
785109323Ssam	/* Set multicast filter. */
786109323Ssam	wi_write_multi(sc);
78746492Swpaul
788114124Simp	/* Allocate fids for the card */
789109323Ssam	if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
790109323Ssam		sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
791109323Ssam		if (sc->sc_firmware_type == WI_SYMBOL)
792109323Ssam			sc->sc_buflen = 1585;	/* XXX */
793112363Simp		for (i = 0; i < sc->sc_ntxbuf; i++) {
794109323Ssam			error = wi_alloc_fid(sc, sc->sc_buflen,
795109323Ssam			    &sc->sc_txd[i].d_fid);
796109323Ssam			if (error) {
797109323Ssam				device_printf(sc->sc_dev,
798109323Ssam				    "tx buffer allocation failed (error %u)\n",
799109323Ssam				    error);
800109323Ssam				goto out;
801109323Ssam			}
802109323Ssam			sc->sc_txd[i].d_len = 0;
80370073Swpaul		}
80470073Swpaul	}
805109323Ssam	sc->sc_txcur = sc->sc_txnext = 0;
80670073Swpaul
807109323Ssam	/* Enable desired port */
808109323Ssam	wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
80946492Swpaul
810114124Simp	sc->sc_enabled = 1;
811148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
812148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
813109323Ssam	if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
814139542Ssam	    ic->ic_opmode == IEEE80211_M_IBSS ||
815117855Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR ||
816109323Ssam	    ic->ic_opmode == IEEE80211_M_HOSTAP)
817139542Ssam		ieee80211_create_ibss(ic, ic->ic_ibss_chan);
81846492Swpaul
819109323Ssam	/* Enable interrupts */
820109323Ssam	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
82146492Swpaul
822109323Ssam	if (!wasenabled &&
823109323Ssam	    ic->ic_opmode == IEEE80211_M_HOSTAP &&
824109323Ssam	    sc->sc_firmware_type == WI_INTERSIL) {
825109323Ssam		/* XXX: some card need to be re-enabled for hostap */
826109323Ssam		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
827109323Ssam		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
82875149Simp	}
82994397Simp
830109323Ssam	if (ic->ic_opmode == IEEE80211_M_STA &&
831109323Ssam	    ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
832116951Ssam	    ic->ic_des_chan != IEEE80211_CHAN_ANYC)) {
833109323Ssam		memset(&join, 0, sizeof(join));
834109323Ssam		if (ic->ic_flags & IEEE80211_F_DESBSSID)
835109323Ssam			IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
836116951Ssam		if (ic->ic_des_chan != IEEE80211_CHAN_ANYC)
837116951Ssam			join.wi_chan = htole16(
838116951Ssam				ieee80211_chan2ieee(ic, ic->ic_des_chan));
839109323Ssam		/* Lucent firmware does not support the JOIN RID. */
840109323Ssam		if (sc->sc_firmware_type != WI_LUCENT)
841109323Ssam			wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
84294397Simp	}
84375373Salfred
844109323Ssam	WI_UNLOCK(sc);
84546492Swpaul	return;
846109323Ssamout:
847109323Ssam	if (error) {
848109323Ssam		if_printf(ifp, "interface not running\n");
849123098Simp		wi_stop(ifp, 1);
85070073Swpaul	}
851110224Simp	WI_UNLOCK(sc);
852109323Ssam	DPRINTF(("wi_init: return %d\n", error));
853109323Ssam	return;
85446492Swpaul}
85546492Swpaul
856109323Ssamvoid
857109323Ssamwi_stop(struct ifnet *ifp, int disable)
85846492Swpaul{
859109323Ssam	struct wi_softc *sc = ifp->if_softc;
860138571Ssam	struct ieee80211com *ic = &sc->sc_ic;
861109323Ssam	WI_LOCK_DECL();
86246492Swpaul
863109323Ssam	WI_LOCK(sc);
86474998Swpaul
865123098Simp	DELAY(100000);
866123098Simp
867117812Ssam	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
868109323Ssam	if (sc->sc_enabled && !sc->wi_gone) {
869109323Ssam		CSR_WRITE_2(sc, WI_INT_EN, 0);
870109323Ssam		wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
871109323Ssam		if (disable) {
872109323Ssam#ifdef __NetBSD__
873109323Ssam			if (sc->sc_disable)
874109323Ssam				(*sc->sc_disable)(sc);
875109323Ssam#endif
876116951Ssam			sc->sc_enabled = 0;
87770073Swpaul		}
878123098Simp	} else if (sc->wi_gone && disable)	/* gone --> not enabled */
879123098Simp	    sc->sc_enabled = 0;
88070073Swpaul
881109323Ssam	sc->sc_tx_timer = 0;
882109323Ssam	sc->sc_scan_timer = 0;
883109323Ssam	sc->sc_false_syns = 0;
884109323Ssam	sc->sc_naps = 0;
885148887Srwatson	ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE | IFF_DRV_RUNNING);
886109323Ssam	ifp->if_timer = 0;
88746492Swpaul
888109323Ssam	WI_UNLOCK(sc);
88946492Swpaul}
89046492Swpaul
891109323Ssamstatic void
892109323Ssamwi_start(struct ifnet *ifp)
89346492Swpaul{
894109323Ssam	struct wi_softc	*sc = ifp->if_softc;
895109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
896119150Ssam	struct ieee80211_node *ni;
897109323Ssam	struct ieee80211_frame *wh;
898138571Ssam	struct ether_header *eh;
899109323Ssam	struct mbuf *m0;
900109323Ssam	struct wi_frame frmhdr;
901160991Ssam	int cur;
902109323Ssam	WI_LOCK_DECL();
90346492Swpaul
904109323Ssam	WI_LOCK(sc);
90546492Swpaul
906109323Ssam	if (sc->wi_gone) {
907109323Ssam		WI_UNLOCK(sc);
908109323Ssam		return;
90946492Swpaul	}
910109323Ssam	if (sc->sc_flags & WI_FLAGS_OUTRANGE) {
911109323Ssam		WI_UNLOCK(sc);
912109323Ssam		return;
91375373Salfred	}
91446492Swpaul
915109323Ssam	memset(&frmhdr, 0, sizeof(frmhdr));
916109323Ssam	cur = sc->sc_txnext;
917109323Ssam	for (;;) {
918109323Ssam		IF_POLL(&ic->ic_mgtq, m0);
919109323Ssam		if (m0 != NULL) {
920109323Ssam			if (sc->sc_txd[cur].d_len != 0) {
921148887Srwatson				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
922109323Ssam				break;
923109323Ssam			}
924109323Ssam			IF_DEQUEUE(&ic->ic_mgtq, m0);
925119150Ssam			/*
926119150Ssam			 * Hack!  The referenced node pointer is in the
927119150Ssam			 * rcvif field of the packet header.  This is
928119150Ssam			 * placed there by ieee80211_mgmt_output because
929119150Ssam			 * we need to hold the reference with the frame
930119150Ssam			 * and there's no other way (other than packet
931119150Ssam			 * tags which we consider too expensive to use)
932119150Ssam			 * to pass it along.
933119150Ssam			 */
934119150Ssam			ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
935119150Ssam			m0->m_pkthdr.rcvif = NULL;
936119150Ssam
937109323Ssam			m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
938109323Ssam			    (caddr_t)&frmhdr.wi_ehdr);
939109323Ssam			frmhdr.wi_ehdr.ether_type = 0;
940109323Ssam                        wh = mtod(m0, struct ieee80211_frame *);
941109323Ssam		} else {
942109323Ssam			if (ic->ic_state != IEEE80211_S_RUN)
943109323Ssam				break;
944132986Smlaier			IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
945109323Ssam			if (m0 == NULL)
946109323Ssam				break;
947109323Ssam			if (sc->sc_txd[cur].d_len != 0) {
948132986Smlaier				IFQ_DRV_PREPEND(&ifp->if_snd, m0);
949148887Srwatson				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
950109323Ssam				break;
951109323Ssam			}
952138571Ssam			if (m0->m_len < sizeof(struct ether_header) &&
953138571Ssam			    (m0 = m_pullup(m0, sizeof(struct ether_header))) == NULL) {
954138571Ssam				ifp->if_oerrors++;
955138571Ssam				continue;
956138571Ssam			}
957138571Ssam			eh = mtod(m0, struct ether_header *);
958138571Ssam			ni = ieee80211_find_txnode(ic, eh->ether_dhost);
959138571Ssam			if (ni == NULL) {
960138571Ssam				m_freem(m0);
961138571Ssam				continue;
962138571Ssam			}
963109323Ssam			ifp->if_opackets++;
964109323Ssam			m_copydata(m0, 0, ETHER_HDR_LEN,
965109323Ssam			    (caddr_t)&frmhdr.wi_ehdr);
966109323Ssam#if NBPFILTER > 0
967109323Ssam			BPF_MTAP(ifp, m0);
968109323Ssam#endif
96946492Swpaul
970138571Ssam			m0 = ieee80211_encap(ic, m0, ni);
971119150Ssam			if (m0 == NULL) {
972109323Ssam				ifp->if_oerrors++;
973138571Ssam				ieee80211_free_node(ni);
974109323Ssam				continue;
975109323Ssam			}
976109323Ssam                        wh = mtod(m0, struct ieee80211_frame *);
977109323Ssam		}
978109323Ssam#if NBPFILTER > 0
979159180Scsjp		if (bpf_peers_present(ic->ic_rawbpf))
980109323Ssam			bpf_mtap(ic->ic_rawbpf, m0);
981109323Ssam#endif
982109323Ssam		frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
983138952Ssam		/* XXX check key for SWCRYPT instead of using operating mode */
984150798Savatar		if ((wh->i_fc[1] & IEEE80211_FC1_WEP) &&
985150798Savatar		    (sc->sc_encryption & HOST_ENCRYPT)) {
986138571Ssam			struct ieee80211_key *k;
987138571Ssam
988138571Ssam			k = ieee80211_crypto_encap(ic, ni, m0);
989138571Ssam			if (k == NULL) {
990138571Ssam				if (ni != NULL)
991138571Ssam					ieee80211_free_node(ni);
992143299Ssam				m_freem(m0);
993109323Ssam				continue;
994109323Ssam			}
995138952Ssam			frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
996109323Ssam		}
997109323Ssam#if NBPFILTER > 0
998159320Savatar		if (bpf_peers_present(sc->sc_drvbpf)) {
999123927Ssam			sc->sc_tx_th.wt_rate =
1000123927Ssam				ni->ni_rates.rs_rates[ni->ni_txrate];
1001123922Ssam			bpf_mtap2(sc->sc_drvbpf,
1002127698Ssam				&sc->sc_tx_th, sc->sc_tx_th_len, m0);
1003109323Ssam		}
100446492Swpaul#endif
1005127697Ssam		m_copydata(m0, 0, sizeof(struct ieee80211_frame),
1006127697Ssam		    (caddr_t)&frmhdr.wi_whdr);
1007127697Ssam		m_adj(m0, sizeof(struct ieee80211_frame));
1008127697Ssam		frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
1009109323Ssam		if (IFF_DUMPPKTS(ifp))
1010116951Ssam			wi_dump_pkt(&frmhdr, NULL, -1);
1011138571Ssam		if (ni != NULL)
1012138571Ssam			ieee80211_free_node(ni);
1013160991Ssam		if (wi_start_tx(ifp, &frmhdr, m0))
1014109323Ssam			continue;
1015160991Ssam		sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
1016160991Ssam	}
1017160991Ssam
1018160991Ssam	WI_UNLOCK(sc);
1019160991Ssam}
1020160991Ssam
1021160991Ssamstatic int
1022160991Ssamwi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr, struct mbuf *m0)
1023160991Ssam{
1024160991Ssam	struct wi_softc	*sc = ifp->if_softc;
1025160991Ssam	int cur = sc->sc_txnext;
1026160991Ssam	int fid, off, error;
1027160991Ssam
1028160991Ssam	fid = sc->sc_txd[cur].d_fid;
1029160991Ssam	off = sizeof(*frmhdr);
1030160991Ssam	error = wi_write_bap(sc, fid, 0, frmhdr, sizeof(*frmhdr)) != 0
1031160991Ssam	     || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
1032160991Ssam	m_freem(m0);
1033160991Ssam	if (error) {
1034160991Ssam		ifp->if_oerrors++;
1035160991Ssam		return -1;
1036160991Ssam	}
1037160991Ssam	sc->sc_txd[cur].d_len = off;
1038160991Ssam	if (sc->sc_txcur == cur) {
1039160991Ssam		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
1040160991Ssam			if_printf(ifp, "xmit failed\n");
1041160991Ssam			sc->sc_txd[cur].d_len = 0;
1042160991Ssam			return -1;
1043109323Ssam		}
1044160991Ssam		sc->sc_tx_timer = 5;
1045160991Ssam		ifp->if_timer = 1;
1046160991Ssam	}
1047160991Ssam	return 0;
1048160991Ssam}
1049160991Ssam
1050160991Ssamstatic int
1051160991Ssamwi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m0,
1052160991Ssam	    const struct ieee80211_bpf_params *params)
1053160991Ssam{
1054160991Ssam	struct ieee80211com *ic = ni->ni_ic;
1055160991Ssam	struct ifnet *ifp = ic->ic_ifp;
1056160991Ssam	struct wi_softc	*sc = ifp->if_softc;
1057160991Ssam	struct ieee80211_frame *wh;
1058160991Ssam	struct wi_frame frmhdr;
1059160991Ssam	int cur;
1060160991Ssam	int rc = 0;
1061160991Ssam	WI_LOCK_DECL();
1062160991Ssam
1063160991Ssam	WI_LOCK(sc);
1064160991Ssam
1065160991Ssam	if (sc->wi_gone) {
1066160991Ssam		rc = ENETDOWN;
1067160991Ssam		goto out;
1068160991Ssam	}
1069160991Ssam	if (sc->sc_flags & WI_FLAGS_OUTRANGE) {
1070160991Ssam		rc = ENETDOWN;
1071160991Ssam		goto out;
1072160991Ssam	}
1073160991Ssam
1074160991Ssam	memset(&frmhdr, 0, sizeof(frmhdr));
1075160991Ssam	cur = sc->sc_txnext;
1076160991Ssam	if (sc->sc_txd[cur].d_len != 0) {
1077160991Ssam		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1078160991Ssam		rc = ENOBUFS;
1079160991Ssam		goto out;
1080160991Ssam	}
1081160991Ssam	m0->m_pkthdr.rcvif = NULL;
1082160991Ssam
1083160991Ssam	m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
1084160991Ssam	    (caddr_t)&frmhdr.wi_ehdr);
1085160991Ssam	frmhdr.wi_ehdr.ether_type = 0;
1086160991Ssam	wh = mtod(m0, struct ieee80211_frame *);
1087160991Ssam
1088160991Ssam#if NBPFILTER > 0
1089160991Ssam	if (bpf_peers_present(ic->ic_rawbpf))
1090160991Ssam		bpf_mtap(ic->ic_rawbpf, m0);
1091160991Ssam#endif
1092160991Ssam	frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
1093160991Ssam	if (params && (params->ibp_flags & IEEE80211_BPF_NOACK))
1094160991Ssam		frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_ALTRTRY);
1095160991Ssam	/* XXX check key for SWCRYPT instead of using operating mode */
1096160991Ssam	if ((wh->i_fc[1] & IEEE80211_FC1_WEP) &&
1097160991Ssam	    (sc->sc_encryption & HOST_ENCRYPT)) {
1098160991Ssam	    	if (!params ||
1099160991Ssam		    (params && (params->ibp_flags & IEEE80211_BPF_CRYPTO))) {
1100160991Ssam			struct ieee80211_key *k;
1101160991Ssam
1102160991Ssam			k = ieee80211_crypto_encap(ic, ni, m0);
1103160991Ssam			if (k == NULL) {
1104160991Ssam				if (ni != NULL)
1105160991Ssam					ieee80211_free_node(ni);
1106160991Ssam				m_freem(m0);
1107160991Ssam				rc = ENOMEM;
1108160991Ssam				goto out;
1109109323Ssam			}
1110160991Ssam			frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
1111109323Ssam		}
111274838Salfred	}
1113160991Ssam#if NBPFILTER > 0
1114160991Ssam	if (bpf_peers_present(sc->sc_drvbpf)) {
1115160991Ssam		sc->sc_tx_th.wt_rate =
1116160991Ssam			ni->ni_rates.rs_rates[ni->ni_txrate];
1117160991Ssam		bpf_mtap2(sc->sc_drvbpf,
1118160991Ssam			&sc->sc_tx_th, sc->sc_tx_th_len, m0);
1119160991Ssam	}
1120160991Ssam#endif
1121160991Ssam	m_copydata(m0, 0, sizeof(struct ieee80211_frame),
1122160991Ssam	    (caddr_t)&frmhdr.wi_whdr);
1123160991Ssam	m_adj(m0, sizeof(struct ieee80211_frame));
1124160991Ssam	frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
1125160991Ssam	if (IFF_DUMPPKTS(ifp))
1126160991Ssam		wi_dump_pkt(&frmhdr, NULL, -1);
1127160991Ssam	if (ni != NULL)
1128160991Ssam		ieee80211_free_node(ni);
1129160991Ssam	rc = wi_start_tx(ifp, &frmhdr, m0);
1130160991Ssam	if (rc)
1131160991Ssam		goto out;
113246492Swpaul
1133160991Ssam	sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
1134160991Ssamout:
1135109323Ssam	WI_UNLOCK(sc);
1136160991Ssam
1137160991Ssam	return rc;
113846492Swpaul}
113946492Swpaul
114088546Salfredstatic int
1141109323Ssamwi_reset(struct wi_softc *sc)
114246492Swpaul{
1143147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1144114124Simp#define WI_INIT_TRIES 3
1145114124Simp	int i;
1146114138Simp	int error = 0;
1147114124Simp	int tries;
1148114124Simp
1149114124Simp	/* Symbol firmware cannot be initialized more than once */
1150123098Simp	if (sc->sc_firmware_type == WI_SYMBOL && sc->sc_reset)
1151112362Simp		return (0);
1152114124Simp	if (sc->sc_firmware_type == WI_SYMBOL)
1153114124Simp		tries = 1;
1154114124Simp	else
1155114124Simp		tries = WI_INIT_TRIES;
1156112362Simp
1157114124Simp	for (i = 0; i < tries; i++) {
1158109323Ssam		if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
115946492Swpaul			break;
1160109323Ssam		DELAY(WI_DELAY * 1000);
116146492Swpaul	}
1162114124Simp	sc->sc_reset = 1;
116346492Swpaul
1164114124Simp	if (i == tries) {
1165114124Simp		if_printf(ifp, "init failed\n");
1166114124Simp		return (error);
116775373Salfred	}
116846492Swpaul
1169109323Ssam	CSR_WRITE_2(sc, WI_INT_EN, 0);
1170114124Simp	CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
117146492Swpaul
1172109323Ssam	/* Calibrate timer. */
1173114124Simp	wi_write_val(sc, WI_RID_TICK_TIME, 8);
1174114124Simp
1175114124Simp	return (0);
1176109323Ssam#undef WI_INIT_TRIES
117746492Swpaul}
117846492Swpaul
117988546Salfredstatic void
1180109323Ssamwi_watchdog(struct ifnet *ifp)
118146492Swpaul{
1182109323Ssam	struct wi_softc	*sc = ifp->if_softc;
118346492Swpaul
1184109323Ssam	ifp->if_timer = 0;
1185109323Ssam	if (!sc->sc_enabled)
1186109323Ssam		return;
118746492Swpaul
1188109323Ssam	if (sc->sc_tx_timer) {
1189109323Ssam		if (--sc->sc_tx_timer == 0) {
1190109323Ssam			if_printf(ifp, "device timeout\n");
1191109323Ssam			ifp->if_oerrors++;
1192109323Ssam			wi_init(ifp->if_softc);
1193109323Ssam			return;
1194109323Ssam		}
1195109323Ssam		ifp->if_timer = 1;
119646492Swpaul	}
119746492Swpaul
1198109323Ssam	if (sc->sc_scan_timer) {
1199109323Ssam		if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
1200109323Ssam		    sc->sc_firmware_type == WI_INTERSIL) {
1201109323Ssam			DPRINTF(("wi_watchdog: inquire scan\n"));
1202109323Ssam			wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
120346492Swpaul		}
1204109323Ssam		if (sc->sc_scan_timer)
1205109323Ssam			ifp->if_timer = 1;
120646492Swpaul	}
120746492Swpaul
1208109323Ssam	/* TODO: rate control */
1209138571Ssam	ieee80211_watchdog(&sc->sc_ic);
121046492Swpaul}
121146492Swpaul
121288546Salfredstatic int
1213109323Ssamwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
121446492Swpaul{
1215109323Ssam	struct wi_softc *sc = ifp->if_softc;
1216109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1217109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
1218109323Ssam	struct ieee80211req *ireq;
1219109323Ssam	u_int8_t nodename[IEEE80211_NWID_LEN];
1220109323Ssam	int error = 0;
122195534Simp#if __FreeBSD_version >= 500000
1222109323Ssam	struct thread *td = curthread;
122395534Simp#else
1224109323Ssam	struct proc *td = curproc;		/* Little white lie */
122595534Simp#endif
1226109323Ssam	struct wi_req wreq;
1227109323Ssam	WI_LOCK_DECL();
122846492Swpaul
1229138571Ssam	if (sc->wi_gone)
1230138571Ssam		return (ENODEV);
123146492Swpaul
1232109323Ssam	switch (cmd) {
123346492Swpaul	case SIOCSIFFLAGS:
1234100876Simp		/*
1235101139Simp		 * Can't do promisc and hostap at the same time.  If all that's
1236101139Simp		 * changing is the promisc flag, try to short-circuit a call to
1237101139Simp		 * wi_init() by just setting PROMISC in the hardware.
1238100876Simp		 */
1239138571Ssam		WI_LOCK(sc);
124046492Swpaul		if (ifp->if_flags & IFF_UP) {
1241109323Ssam			if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1242148887Srwatson			    ifp->if_drv_flags & IFF_DRV_RUNNING) {
1243101139Simp				if (ifp->if_flags & IFF_PROMISC &&
1244109323Ssam				    !(sc->sc_if_flags & IFF_PROMISC)) {
1245109323Ssam					wi_write_val(sc, WI_RID_PROMISC, 1);
1246101139Simp				} else if (!(ifp->if_flags & IFF_PROMISC) &&
1247109323Ssam				    sc->sc_if_flags & IFF_PROMISC) {
1248109323Ssam					wi_write_val(sc, WI_RID_PROMISC, 0);
1249101139Simp				} else {
1250101139Simp					wi_init(sc);
1251101139Simp				}
1252100876Simp			} else {
125346492Swpaul				wi_init(sc);
1254100876Simp			}
125546492Swpaul		} else {
1256148887Srwatson			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1257123098Simp				wi_stop(ifp, 1);
125846492Swpaul			}
1259123098Simp			sc->wi_gone = 0;
126046492Swpaul		}
1261109323Ssam		sc->sc_if_flags = ifp->if_flags;
1262138571Ssam		WI_UNLOCK(sc);
126346492Swpaul		error = 0;
126446492Swpaul		break;
126546492Swpaul	case SIOCADDMULTI:
126646492Swpaul	case SIOCDELMULTI:
1267138571Ssam		WI_LOCK(sc);
1268109323Ssam		error = wi_write_multi(sc);
1269138571Ssam		WI_UNLOCK(sc);
127046492Swpaul		break;
1271109323Ssam	case SIOCGIFGENERIC:
1272138571Ssam		WI_LOCK(sc);
1273109323Ssam		error = wi_get_cfg(ifp, cmd, data);
1274138571Ssam		WI_UNLOCK(sc);
127546492Swpaul		break;
1276109323Ssam	case SIOCSIFGENERIC:
1277164033Srwatson		error = priv_check(td, PRIV_DRIVER);
1278138571Ssam		if (error == 0)
1279138571Ssam			error = wi_set_cfg(ifp, cmd, data);
128046492Swpaul		break;
128193359Simp	case SIOCGPRISM2DEBUG:
128293359Simp		error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
128393359Simp		if (error)
128493359Simp			break;
1285148887Srwatson		if (!(ifp->if_drv_flags & IFF_DRV_RUNNING) ||
128693733Simp		    sc->sc_firmware_type == WI_LUCENT) {
128793359Simp			error = EIO;
128893359Simp			break;
128993359Simp		}
129093359Simp		error = wi_get_debug(sc, &wreq);
129193359Simp		if (error == 0)
129293359Simp			error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
129393359Simp		break;
129493359Simp	case SIOCSPRISM2DEBUG:
1295164033Srwatson		if ((error = priv_check(td, PRIV_DRIVER)))
1296138571Ssam			return (error);
129793359Simp		error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
129893359Simp		if (error)
129993359Simp			break;
1300138571Ssam		WI_LOCK(sc);
130193359Simp		error = wi_set_debug(sc, &wreq);
1302138571Ssam		WI_UNLOCK(sc);
130393359Simp		break;
130477217Sphk	case SIOCG80211:
1305109323Ssam		ireq = (struct ieee80211req *) data;
1306138571Ssam		if (ireq->i_type == IEEE80211_IOC_STATIONNAME) {
1307109323Ssam			ireq->i_len = sc->sc_nodelen + 1;
1308109323Ssam			error = copyout(sc->sc_nodename, ireq->i_data,
1309109323Ssam					ireq->i_len);
131077217Sphk			break;
131177217Sphk		}
1312138571Ssam		goto ioctl_common;
131377217Sphk	case SIOCS80211:
1314109323Ssam		ireq = (struct ieee80211req *) data;
1315138571Ssam		if (ireq->i_type == IEEE80211_IOC_STATIONNAME) {
1316164033Srwatson			error = priv_check(td, PRIV_NET80211_MANAGE);
1317138571Ssam			if (error)
1318138571Ssam				break;
131977217Sphk			if (ireq->i_val != 0 ||
132077217Sphk			    ireq->i_len > IEEE80211_NWID_LEN) {
132177217Sphk				error = EINVAL;
132277217Sphk				break;
132377217Sphk			}
1324109323Ssam			memset(nodename, 0, IEEE80211_NWID_LEN);
1325109323Ssam			error = copyin(ireq->i_data, nodename, ireq->i_len);
1326109323Ssam			if (error)
132777217Sphk				break;
1328138571Ssam			WI_LOCK(sc);
1329109323Ssam			if (sc->sc_enabled) {
1330109323Ssam				error = wi_write_ssid(sc, WI_RID_NODENAME,
1331109323Ssam					nodename, ireq->i_len);
133277217Sphk			}
1333138571Ssam			if (error == 0) {
1334138571Ssam				memcpy(sc->sc_nodename, nodename,
1335138571Ssam					IEEE80211_NWID_LEN);
1336138571Ssam				sc->sc_nodelen = ireq->i_len;
1337138571Ssam			}
1338138571Ssam			WI_UNLOCK(sc);
133977217Sphk			break;
134077217Sphk		}
1341138571Ssam		goto ioctl_common;
134246492Swpaul	default:
1343138571Ssam	ioctl_common:
1344138571Ssam		WI_LOCK(sc);
1345138571Ssam		error = ieee80211_ioctl(ic, cmd, data);
1346138571Ssam		if (error == ENETRESET) {
1347138571Ssam			if (sc->sc_enabled)
1348138571Ssam				wi_init(sc);	/* XXX no error return */
1349138571Ssam			error = 0;
1350138571Ssam		}
1351138571Ssam		WI_UNLOCK(sc);
135246492Swpaul		break;
135346492Swpaul	}
1354109323Ssam	return (error);
135546492Swpaul}
135646492Swpaul
1357109323Ssamstatic int
1358109323Ssamwi_media_change(struct ifnet *ifp)
135946492Swpaul{
1360109323Ssam	struct wi_softc *sc = ifp->if_softc;
1361116951Ssam	int error;
136246492Swpaul
1363116951Ssam	error = ieee80211_media_change(ifp);
1364109323Ssam	if (error == ENETRESET) {
1365109323Ssam		if (sc->sc_enabled)
1366116951Ssam			wi_init(sc);	/* XXX no error return */
1367109323Ssam		error = 0;
1368109323Ssam	}
1369109323Ssam	return error;
1370109323Ssam}
137146492Swpaul
1372109323Ssamstatic void
1373109323Ssamwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1374109323Ssam{
1375109323Ssam	struct wi_softc *sc = ifp->if_softc;
1376109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1377109323Ssam	u_int16_t val;
1378109323Ssam	int rate, len;
137946492Swpaul
1380149007Ssam	if (sc->wi_gone) {		/* hardware gone (e.g. ejected) */
1381109323Ssam		imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
1382109323Ssam		imr->ifm_status = 0;
1383109323Ssam		return;
1384109323Ssam	}
138546492Swpaul
1386109323Ssam	imr->ifm_status = IFM_AVALID;
1387109323Ssam	imr->ifm_active = IFM_IEEE80211;
1388149007Ssam	if (!sc->sc_enabled) {		/* port !enabled, have no status */
1389149007Ssam		imr->ifm_active |= IFM_NONE;
1390149007Ssam		imr->ifm_status = IFM_AVALID;
1391149007Ssam		return;
1392149007Ssam	}
1393109323Ssam	if (ic->ic_state == IEEE80211_S_RUN &&
1394109323Ssam	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
1395109323Ssam		imr->ifm_status |= IFM_ACTIVE;
1396109323Ssam	len = sizeof(val);
1397138571Ssam	if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) == 0 &&
1398138571Ssam	    len == sizeof(val)) {
1399109323Ssam		/* convert to 802.11 rate */
1400138571Ssam		val = le16toh(val);
1401109323Ssam		rate = val * 2;
1402109323Ssam		if (sc->sc_firmware_type == WI_LUCENT) {
1403138571Ssam			if (rate == 10)
1404109323Ssam				rate = 11;	/* 5.5Mbps */
1405109323Ssam		} else {
1406109323Ssam			if (rate == 4*2)
1407109323Ssam				rate = 11;	/* 5.5Mbps */
1408109323Ssam			else if (rate == 8*2)
1409109323Ssam				rate = 22;	/* 11Mbps */
1410109323Ssam		}
1411138571Ssam	} else
1412138571Ssam		rate = 0;
1413116951Ssam	imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
1414109323Ssam	switch (ic->ic_opmode) {
1415109323Ssam	case IEEE80211_M_STA:
1416109323Ssam		break;
1417109323Ssam	case IEEE80211_M_IBSS:
1418109323Ssam		imr->ifm_active |= IFM_IEEE80211_ADHOC;
1419109323Ssam		break;
1420109323Ssam	case IEEE80211_M_AHDEMO:
1421109323Ssam		imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1422109323Ssam		break;
1423109323Ssam	case IEEE80211_M_HOSTAP:
1424109323Ssam		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1425109323Ssam		break;
1426117855Ssam	case IEEE80211_M_MONITOR:
1427117855Ssam		imr->ifm_active |= IFM_IEEE80211_MONITOR;
1428117855Ssam		break;
1429109323Ssam	}
1430109323Ssam}
143146492Swpaul
1432109323Ssamstatic void
1433109323Ssamwi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1434109323Ssam{
1435109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1436116951Ssam	struct ieee80211_node *ni = ic->ic_bss;
1437147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
143898440Simp
1439109323Ssam	if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1440109323Ssam		return;
144146492Swpaul
1442109323Ssam	DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid)));
1443109323Ssam	DPRINTF(("%s ?\n", ether_sprintf(new_bssid)));
144446492Swpaul
1445109323Ssam	/* In promiscuous mode, the BSSID field is not a reliable
1446109323Ssam	 * indicator of the firmware's BSSID. Damp spurious
1447109323Ssam	 * change-of-BSSID indications.
1448109323Ssam	 */
1449109323Ssam	if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1450138571Ssam	    !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns,
1451138571Ssam	                 WI_MAX_FALSE_SYNS))
1452109323Ssam		return;
145346492Swpaul
1454139542Ssam	sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1);
1455139542Ssam	/*
1456139542Ssam	 * XXX hack; we should create a new node with the new bssid
1457139542Ssam	 * and replace the existing ic_bss with it but since we don't
1458139542Ssam	 * process management frames to collect state we cheat by
1459139542Ssam	 * reusing the existing node as we know wi_newstate will be
1460139542Ssam	 * called and it will overwrite the node state.
1461139542Ssam	 */
1462139542Ssam	ieee80211_sta_join(ic, ieee80211_ref_node(ni));
1463109323Ssam}
146446492Swpaul
1465109323Ssamstatic void
1466116898Ssamwi_rx_monitor(struct wi_softc *sc, int fid)
1467116898Ssam{
1468147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1469116898Ssam	struct wi_frame *rx_frame;
1470116898Ssam	struct mbuf *m;
1471116898Ssam	int datlen, hdrlen;
1472116898Ssam
1473116898Ssam	/* first allocate mbuf for packet storage */
1474116898Ssam	m = m_getcl(M_DONTWAIT, MT_DATA, 0);
1475116898Ssam	if (m == NULL) {
1476116898Ssam		ifp->if_ierrors++;
1477116898Ssam		return;
1478116898Ssam	}
1479116898Ssam
1480116898Ssam	m->m_pkthdr.rcvif = ifp;
1481116898Ssam
1482116898Ssam	/* now read wi_frame first so we know how much data to read */
1483116898Ssam	if (wi_read_bap(sc, fid, 0, mtod(m, caddr_t), sizeof(*rx_frame))) {
1484116898Ssam		ifp->if_ierrors++;
1485116898Ssam		goto done;
1486116898Ssam	}
1487116898Ssam
1488116898Ssam	rx_frame = mtod(m, struct wi_frame *);
1489116898Ssam
1490116898Ssam	switch ((rx_frame->wi_status & WI_STAT_MAC_PORT) >> 8) {
1491116898Ssam	case 7:
1492116898Ssam		switch (rx_frame->wi_whdr.i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1493116898Ssam		case IEEE80211_FC0_TYPE_DATA:
1494116898Ssam			hdrlen = WI_DATA_HDRLEN;
1495116898Ssam			datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1496116898Ssam			break;
1497116898Ssam		case IEEE80211_FC0_TYPE_MGT:
1498116898Ssam			hdrlen = WI_MGMT_HDRLEN;
1499116898Ssam			datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1500116898Ssam			break;
1501116898Ssam		case IEEE80211_FC0_TYPE_CTL:
1502116898Ssam			/*
1503116898Ssam			 * prism2 cards don't pass control packets
1504116898Ssam			 * down properly or consistently, so we'll only
1505116898Ssam			 * pass down the header.
1506116898Ssam			 */
1507116898Ssam			hdrlen = WI_CTL_HDRLEN;
1508116898Ssam			datlen = 0;
1509116898Ssam			break;
1510116898Ssam		default:
1511116898Ssam			if_printf(ifp, "received packet of unknown type "
1512116898Ssam				"on port 7\n");
1513116898Ssam			ifp->if_ierrors++;
1514116898Ssam			goto done;
1515116898Ssam		}
1516116898Ssam		break;
1517116898Ssam	case 0:
1518116898Ssam		hdrlen = WI_DATA_HDRLEN;
1519116898Ssam		datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1520116898Ssam		break;
1521116898Ssam	default:
1522116898Ssam		if_printf(ifp, "received packet on invalid "
1523116898Ssam		    "port (wi_status=0x%x)\n", rx_frame->wi_status);
1524116898Ssam		ifp->if_ierrors++;
1525116898Ssam		goto done;
1526116898Ssam	}
1527116898Ssam
1528116898Ssam	if (hdrlen + datlen + 2 > MCLBYTES) {
1529116898Ssam		if_printf(ifp, "oversized packet received "
1530116898Ssam		    "(wi_dat_len=%d, wi_status=0x%x)\n",
1531116898Ssam		    datlen, rx_frame->wi_status);
1532116898Ssam		ifp->if_ierrors++;
1533116898Ssam		goto done;
1534116898Ssam	}
1535116898Ssam
1536116898Ssam	if (wi_read_bap(sc, fid, hdrlen, mtod(m, caddr_t) + hdrlen,
1537116898Ssam	    datlen + 2) == 0) {
1538116898Ssam		m->m_pkthdr.len = m->m_len = hdrlen + datlen;
1539116898Ssam		ifp->if_ipackets++;
1540116898Ssam		BPF_MTAP(ifp, m);	/* Handle BPF listeners. */
1541116898Ssam	} else
1542116898Ssam		ifp->if_ierrors++;
1543116898Ssamdone:
1544116898Ssam	m_freem(m);
1545116898Ssam}
1546116898Ssam
1547116898Ssamstatic void
1548109323Ssamwi_rx_intr(struct wi_softc *sc)
1549109323Ssam{
1550109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1551147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1552109323Ssam	struct wi_frame frmhdr;
1553109323Ssam	struct mbuf *m;
1554109323Ssam	struct ieee80211_frame *wh;
1555119150Ssam	struct ieee80211_node *ni;
1556109323Ssam	int fid, len, off, rssi;
1557109323Ssam	u_int8_t dir;
1558109323Ssam	u_int16_t status;
1559109323Ssam	u_int32_t rstamp;
156046611Swpaul
1561109323Ssam	fid = CSR_READ_2(sc, WI_RX_FID);
156246611Swpaul
1563116898Ssam	if (sc->wi_debug.wi_monitor) {
1564116898Ssam		/*
1565116898Ssam		 * If we are in monitor mode just
1566116898Ssam		 * read the data from the device.
1567116898Ssam		 */
1568116898Ssam		wi_rx_monitor(sc, fid);
1569116898Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1570116898Ssam		return;
1571116898Ssam	}
1572116898Ssam
1573109323Ssam	/* First read in the frame header */
1574109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1575109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1576109323Ssam		ifp->if_ierrors++;
1577109323Ssam		DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1578109323Ssam		return;
1579109323Ssam	}
158091695Simp
1581109323Ssam	if (IFF_DUMPPKTS(ifp))
1582109323Ssam		wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal);
158346492Swpaul
1584109323Ssam	/*
1585109323Ssam	 * Drop undecryptable or packets with receive errors here
1586109323Ssam	 */
1587109323Ssam	status = le16toh(frmhdr.wi_status);
1588109323Ssam	if (status & WI_STAT_ERRSTAT) {
1589109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1590109323Ssam		ifp->if_ierrors++;
1591109323Ssam		DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1592109323Ssam		return;
1593109323Ssam	}
1594109323Ssam	rssi = frmhdr.wi_rx_signal;
1595109323Ssam	rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1596109323Ssam	    le16toh(frmhdr.wi_rx_tstamp1);
159746492Swpaul
1598109323Ssam	len = le16toh(frmhdr.wi_dat_len);
1599109323Ssam	off = ALIGN(sizeof(struct ieee80211_frame));
160046563Swpaul
1601117855Ssam	/*
1602117855Ssam	 * Sometimes the PRISM2.x returns bogusly large frames. Except
1603117855Ssam	 * in monitor mode, just throw them away.
1604117855Ssam	 */
1605117855Ssam	if (off + len > MCLBYTES) {
1606117855Ssam		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1607117855Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1608117855Ssam			ifp->if_ierrors++;
1609117855Ssam			DPRINTF(("wi_rx_intr: oversized packet\n"));
1610117855Ssam			return;
1611117855Ssam		} else
1612117855Ssam			len = 0;
1613117855Ssam	}
1614117855Ssam
1615111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
1616109323Ssam	if (m == NULL) {
1617109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1618109323Ssam		ifp->if_ierrors++;
1619109323Ssam		DPRINTF(("wi_rx_intr: MGET failed\n"));
1620109323Ssam		return;
1621109323Ssam	}
1622109323Ssam	if (off + len > MHLEN) {
1623111119Simp		MCLGET(m, M_DONTWAIT);
1624109323Ssam		if ((m->m_flags & M_EXT) == 0) {
1625109323Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1626109323Ssam			m_freem(m);
1627109323Ssam			ifp->if_ierrors++;
1628109323Ssam			DPRINTF(("wi_rx_intr: MCLGET failed\n"));
1629109323Ssam			return;
1630109323Ssam		}
1631109323Ssam	}
163246492Swpaul
1633109323Ssam	m->m_data += off - sizeof(struct ieee80211_frame);
1634109323Ssam	memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1635109323Ssam	wi_read_bap(sc, fid, sizeof(frmhdr),
1636109323Ssam	    m->m_data + sizeof(struct ieee80211_frame), len);
1637109323Ssam	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1638109323Ssam	m->m_pkthdr.rcvif = ifp;
163994405Simp
1640109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
164146492Swpaul
1642109323Ssam#if NBPFILTER > 0
1643159180Scsjp	if (bpf_peers_present(sc->sc_drvbpf)) {
1644123927Ssam		/* XXX replace divide by table */
1645123922Ssam		sc->sc_rx_th.wr_rate = frmhdr.wi_rx_rate / 5;
1646127148Sgreen		sc->sc_rx_th.wr_antsignal = frmhdr.wi_rx_signal;
1647127148Sgreen		sc->sc_rx_th.wr_antnoise = frmhdr.wi_rx_silence;
1648123927Ssam		sc->sc_rx_th.wr_flags = 0;
1649123927Ssam		if (frmhdr.wi_status & WI_STAT_PCF)
1650123927Ssam			sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_CFP;
1651138952Ssam		/* XXX IEEE80211_RADIOTAP_F_WEP */
1652123922Ssam		bpf_mtap2(sc->sc_drvbpf,
1653127698Ssam			&sc->sc_rx_th, sc->sc_rx_th_len, m);
165456965Swpaul	}
1655109323Ssam#endif
1656160991Ssam	wh = mtod(m, struct ieee80211_frame *);
1657160991Ssam	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1658160991Ssam		/*
1659160991Ssam		 * WEP is decrypted by hardware and the IV
1660160991Ssam		 * is stripped.  Clear WEP bit so we don't
1661160991Ssam		 * try to process it in ieee80211_input.
1662160991Ssam		 * XXX fix for TKIP, et. al.
1663160991Ssam		 */
1664160991Ssam		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1665160991Ssam	}
166656965Swpaul
1667109323Ssam	/* synchronize driver's BSSID with firmware's BSSID */
1668109323Ssam	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1669109323Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1670109323Ssam		wi_sync_bssid(sc, wh->i_addr3);
167146492Swpaul
1672119150Ssam	/*
1673119150Ssam	 * Locate the node for sender, track state, and
1674119150Ssam	 * then pass this node (referenced) up to the 802.11
1675138571Ssam	 * layer for its use.
1676119150Ssam	 */
1677138571Ssam	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *) wh);
1678119150Ssam	/*
1679119150Ssam	 * Send frame up for processing.
1680119150Ssam	 */
1681138571Ssam	ieee80211_input(ic, m, ni, rssi, rstamp);
1682119150Ssam	/*
1683119150Ssam	 * The frame may have caused the node to be marked for
1684119150Ssam	 * reclamation (e.g. in response to a DEAUTH message)
1685119150Ssam	 * so use free_node here instead of unref_node.
1686119150Ssam	 */
1687138571Ssam	ieee80211_free_node(ni);
1688109323Ssam}
168946492Swpaul
1690109323Ssamstatic void
1691109323Ssamwi_tx_ex_intr(struct wi_softc *sc)
1692109323Ssam{
1693147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1694109323Ssam	struct wi_frame frmhdr;
1695109323Ssam	int fid;
169646492Swpaul
1697109323Ssam	fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1698109323Ssam	/* Read in the frame header */
1699109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1700109323Ssam		u_int16_t status = le16toh(frmhdr.wi_status);
170146492Swpaul
1702109323Ssam		/*
1703109323Ssam		 * Spontaneous station disconnects appear as xmit
1704109323Ssam		 * errors.  Don't announce them and/or count them
1705109323Ssam		 * as an output error.
1706109323Ssam		 */
1707109323Ssam		if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1708109323Ssam			if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1709109323Ssam				if_printf(ifp, "tx failed");
1710109323Ssam				if (status & WI_TXSTAT_RET_ERR)
1711109323Ssam					printf(", retry limit exceeded");
1712109323Ssam				if (status & WI_TXSTAT_AGED_ERR)
1713109323Ssam					printf(", max transmit lifetime exceeded");
1714109323Ssam				if (status & WI_TXSTAT_DISCONNECT)
1715109323Ssam					printf(", port disconnected");
1716109323Ssam				if (status & WI_TXSTAT_FORM_ERR)
1717109323Ssam					printf(", invalid format (data len %u src %6D)",
1718109323Ssam						le16toh(frmhdr.wi_dat_len),
1719109323Ssam						frmhdr.wi_ehdr.ether_shost, ":");
1720109323Ssam				if (status & ~0xf)
1721109323Ssam					printf(", status=0x%x", status);
1722109323Ssam				printf("\n");
1723109323Ssam			}
1724109323Ssam			ifp->if_oerrors++;
1725109323Ssam		} else {
1726109323Ssam			DPRINTF(("port disconnected\n"));
1727109323Ssam			ifp->if_collisions++;	/* XXX */
1728109323Ssam		}
1729109323Ssam	} else
1730109323Ssam		DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
1731109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1732109323Ssam}
173346492Swpaul
1734109323Ssamstatic void
1735109323Ssamwi_tx_intr(struct wi_softc *sc)
1736109323Ssam{
1737147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1738109323Ssam	int fid, cur;
173994405Simp
1740123098Simp	if (sc->wi_gone)
1741123098Simp		return;
1742123098Simp
1743109323Ssam	fid = CSR_READ_2(sc, WI_ALLOC_FID);
1744109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
174546492Swpaul
1746109323Ssam	cur = sc->sc_txcur;
1747109323Ssam	if (sc->sc_txd[cur].d_fid != fid) {
1748109323Ssam		if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
1749109323Ssam		    fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
1750109323Ssam		return;
1751109323Ssam	}
1752109323Ssam	sc->sc_tx_timer = 0;
1753109323Ssam	sc->sc_txd[cur].d_len = 0;
1754112363Simp	sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
1755109323Ssam	if (sc->sc_txd[cur].d_len == 0)
1756148887Srwatson		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1757109323Ssam	else {
1758109323Ssam		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1759109323Ssam		    0, 0)) {
1760109323Ssam			if_printf(ifp, "xmit failed\n");
1761109323Ssam			sc->sc_txd[cur].d_len = 0;
1762109323Ssam		} else {
1763109323Ssam			sc->sc_tx_timer = 5;
1764109323Ssam			ifp->if_timer = 1;
1765109323Ssam		}
1766109323Ssam	}
176746492Swpaul}
176846492Swpaul
176988546Salfredstatic void
1770109323Ssamwi_info_intr(struct wi_softc *sc)
177194405Simp{
1772109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1773147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1774109323Ssam	int i, fid, len, off;
1775109323Ssam	u_int16_t ltbuf[2];
1776109323Ssam	u_int16_t stat;
1777109323Ssam	u_int32_t *ptr;
177894405Simp
1779109323Ssam	fid = CSR_READ_2(sc, WI_INFO_FID);
1780109323Ssam	wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
178194405Simp
1782109323Ssam	switch (le16toh(ltbuf[1])) {
178394405Simp
1784109323Ssam	case WI_INFO_LINK_STAT:
1785109323Ssam		wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1786109323Ssam		DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1787109323Ssam		switch (le16toh(stat)) {
1788109323Ssam		case WI_INFO_LINK_STAT_CONNECTED:
1789109323Ssam			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1790109323Ssam			if (ic->ic_state == IEEE80211_S_RUN &&
1791109323Ssam			    ic->ic_opmode != IEEE80211_M_IBSS)
1792109323Ssam				break;
1793109323Ssam			/* FALLTHROUGH */
1794109323Ssam		case WI_INFO_LINK_STAT_AP_CHG:
1795117812Ssam			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1796109323Ssam			break;
1797109323Ssam		case WI_INFO_LINK_STAT_AP_INR:
1798109323Ssam			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1799109323Ssam			break;
1800109323Ssam		case WI_INFO_LINK_STAT_AP_OOR:
1801109323Ssam			if (sc->sc_firmware_type == WI_SYMBOL &&
1802109323Ssam			    sc->sc_scan_timer > 0) {
1803109323Ssam				if (wi_cmd(sc, WI_CMD_INQUIRE,
1804109323Ssam				    WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
1805109323Ssam					sc->sc_scan_timer = 0;
1806109323Ssam				break;
1807109323Ssam			}
1808109323Ssam			if (ic->ic_opmode == IEEE80211_M_STA)
1809109323Ssam				sc->sc_flags |= WI_FLAGS_OUTRANGE;
1810109323Ssam			break;
1811109323Ssam		case WI_INFO_LINK_STAT_DISCONNECTED:
1812109323Ssam		case WI_INFO_LINK_STAT_ASSOC_FAILED:
1813109323Ssam			if (ic->ic_opmode == IEEE80211_M_STA)
1814117812Ssam				ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1815109323Ssam			break;
1816109323Ssam		}
1817109323Ssam		break;
181894405Simp
1819109323Ssam	case WI_INFO_COUNTERS:
1820109323Ssam		/* some card versions have a larger stats structure */
1821109323Ssam		len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1822109323Ssam		ptr = (u_int32_t *)&sc->sc_stats;
1823109323Ssam		off = sizeof(ltbuf);
1824109323Ssam		for (i = 0; i < len; i++, off += 2, ptr++) {
1825109323Ssam			wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1826109323Ssam#ifdef WI_HERMES_STATS_WAR
1827109323Ssam			if (stat & 0xf000)
1828109323Ssam				stat = ~stat;
1829109323Ssam#endif
1830109323Ssam			*ptr += stat;
1831109323Ssam		}
1832109323Ssam		ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1833109323Ssam		    sc->sc_stats.wi_tx_multi_retries +
1834109323Ssam		    sc->sc_stats.wi_tx_retry_limit;
1835109323Ssam		break;
1836109323Ssam
1837109323Ssam	case WI_INFO_SCAN_RESULTS:
1838109323Ssam	case WI_INFO_HOST_SCAN_RESULTS:
1839109323Ssam		wi_scan_result(sc, fid, le16toh(ltbuf[0]));
1840109323Ssam		break;
1841109323Ssam
1842109323Ssam	default:
1843109323Ssam		DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1844109323Ssam		    le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1845109323Ssam		break;
184694405Simp	}
1847109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1848109323Ssam}
184994405Simp
1850109323Ssamstatic int
1851109323Ssamwi_write_multi(struct wi_softc *sc)
1852109323Ssam{
1853147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1854109323Ssam	int n;
1855109323Ssam	struct ifmultiaddr *ifma;
1856109323Ssam	struct wi_mcast mlist;
185794472Simp
1858109323Ssam	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1859109323Ssamallmulti:
1860109323Ssam		memset(&mlist, 0, sizeof(mlist));
1861109323Ssam		return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1862109323Ssam		    sizeof(mlist));
186394405Simp	}
186494405Simp
1865109323Ssam	n = 0;
1866148654Srwatson	IF_ADDR_LOCK(ifp);
1867109323Ssam#if __FreeBSD_version < 500000
1868109323Ssam	LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1869109323Ssam#else
1870109323Ssam	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1871109323Ssam#endif
1872109323Ssam		if (ifma->ifma_addr->sa_family != AF_LINK)
1873109323Ssam			continue;
1874109323Ssam		if (n >= 16)
1875109323Ssam			goto allmulti;
1876109323Ssam		IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1877109323Ssam		    (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1878109323Ssam		n++;
187994405Simp	}
1880148654Srwatson	IF_ADDR_UNLOCK(ifp);
1881109323Ssam	return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1882109323Ssam	    IEEE80211_ADDR_LEN * n);
188394405Simp}
188494405Simp
188594405Simpstatic void
1886109323Ssamwi_read_nicid(struct wi_softc *sc)
188746492Swpaul{
1888109323Ssam	struct wi_card_ident *id;
1889109323Ssam	char *p;
1890109323Ssam	int len;
1891109323Ssam	u_int16_t ver[4];
189246492Swpaul
1893109323Ssam	/* getting chip identity */
1894109323Ssam	memset(ver, 0, sizeof(ver));
1895109323Ssam	len = sizeof(ver);
1896109323Ssam	wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1897109323Ssam	device_printf(sc->sc_dev, "using ");
189846492Swpaul
1899109323Ssam	sc->sc_firmware_type = WI_NOTYPE;
1900109323Ssam	for (id = wi_card_ident; id->card_name != NULL; id++) {
1901109323Ssam		if (le16toh(ver[0]) == id->card_id) {
1902109323Ssam			printf("%s", id->card_name);
1903109323Ssam			sc->sc_firmware_type = id->firm_type;
1904109323Ssam			break;
1905109323Ssam		}
190667092Swpaul	}
1907109323Ssam	if (sc->sc_firmware_type == WI_NOTYPE) {
1908109323Ssam		if (le16toh(ver[0]) & 0x8000) {
1909109323Ssam			printf("Unknown PRISM2 chip");
1910109323Ssam			sc->sc_firmware_type = WI_INTERSIL;
1911109323Ssam		} else {
1912109323Ssam			printf("Unknown Lucent chip");
1913109323Ssam			sc->sc_firmware_type = WI_LUCENT;
1914109323Ssam		}
191567092Swpaul	}
191646492Swpaul
1917109323Ssam	/* get primary firmware version (Only Prism chips) */
1918109323Ssam	if (sc->sc_firmware_type != WI_LUCENT) {
1919109323Ssam		memset(ver, 0, sizeof(ver));
1920109323Ssam		len = sizeof(ver);
1921109323Ssam		wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1922109323Ssam		sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1923109323Ssam		    le16toh(ver[3]) * 100 + le16toh(ver[1]);
192467092Swpaul	}
192546492Swpaul
1926109323Ssam	/* get station firmware version */
1927109323Ssam	memset(ver, 0, sizeof(ver));
1928109323Ssam	len = sizeof(ver);
1929109323Ssam	wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1930109323Ssam	sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1931109323Ssam	    le16toh(ver[3]) * 100 + le16toh(ver[1]);
1932109323Ssam	if (sc->sc_firmware_type == WI_INTERSIL &&
1933109323Ssam	    (sc->sc_sta_firmware_ver == 10102 ||
1934109323Ssam	     sc->sc_sta_firmware_ver == 20102)) {
1935109323Ssam		char ident[12];
1936109323Ssam		memset(ident, 0, sizeof(ident));
1937109323Ssam		len = sizeof(ident);
1938109323Ssam		/* value should be the format like "V2.00-11" */
1939109323Ssam		if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1940109323Ssam		    *(p = (char *)ident) >= 'A' &&
1941109323Ssam		    p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1942109323Ssam			sc->sc_firmware_type = WI_SYMBOL;
1943109323Ssam			sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1944109323Ssam			    (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1945109323Ssam			    (p[6] - '0') * 10 + (p[7] - '0');
194694405Simp		}
194794405Simp	}
1948109323Ssam	printf("\n");
1949109323Ssam	device_printf(sc->sc_dev, "%s Firmware: ",
1950109323Ssam	     sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
1951109323Ssam	    (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
1952109323Ssam	if (sc->sc_firmware_type != WI_LUCENT)	/* XXX */
1953109323Ssam		printf("Primary (%u.%u.%u), ",
1954109323Ssam		    sc->sc_pri_firmware_ver / 10000,
1955109323Ssam		    (sc->sc_pri_firmware_ver % 10000) / 100,
1956109323Ssam		    sc->sc_pri_firmware_ver % 100);
1957109323Ssam	printf("Station (%u.%u.%u)\n",
1958109323Ssam	    sc->sc_sta_firmware_ver / 10000,
1959109323Ssam	    (sc->sc_sta_firmware_ver % 10000) / 100,
1960109323Ssam	    sc->sc_sta_firmware_ver % 100);
1961109323Ssam}
196246492Swpaul
1963109323Ssamstatic int
1964109323Ssamwi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1965109323Ssam{
1966109323Ssam	struct wi_ssid ssid;
196746492Swpaul
1968109323Ssam	if (buflen > IEEE80211_NWID_LEN)
1969109323Ssam		return ENOBUFS;
1970109323Ssam	memset(&ssid, 0, sizeof(ssid));
1971109323Ssam	ssid.wi_len = htole16(buflen);
1972109323Ssam	memcpy(ssid.wi_ssid, buf, buflen);
1973109323Ssam	return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1974109323Ssam}
197546492Swpaul
1976109323Ssamstatic int
1977109323Ssamwi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
1978109323Ssam{
1979109323Ssam	struct wi_softc *sc = ifp->if_softc;
1980109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1981109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
1982109323Ssam	struct wi_req wreq;
1983116898Ssam	struct wi_scan_res *res;
1984122015Sgreen	size_t reslen;
1985116898Ssam	int len, n, error, mif, val, off, i;
198646492Swpaul
1987109323Ssam	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1988109323Ssam	if (error)
1989109323Ssam		return error;
1990109323Ssam	len = (wreq.wi_len - 1) * 2;
1991109323Ssam	if (len < sizeof(u_int16_t))
1992109323Ssam		return ENOSPC;
1993109323Ssam	if (len > sizeof(wreq.wi_val))
1994109323Ssam		len = sizeof(wreq.wi_val);
199546492Swpaul
1996109323Ssam	switch (wreq.wi_type) {
199746492Swpaul
1998109323Ssam	case WI_RID_IFACE_STATS:
1999109323Ssam		memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
2000109323Ssam		if (len < sizeof(sc->sc_stats))
2001109323Ssam			error = ENOSPC;
2002109323Ssam		else
2003109323Ssam			len = sizeof(sc->sc_stats);
2004109323Ssam		break;
200546492Swpaul
2006109323Ssam	case WI_RID_ENCRYPTION:
2007109323Ssam	case WI_RID_TX_CRYPT_KEY:
2008109323Ssam	case WI_RID_DEFLT_CRYPT_KEYS:
2009109323Ssam	case WI_RID_TX_RATE:
2010138571Ssam		return ieee80211_cfgget(ic, cmd, data);
201146492Swpaul
2012109323Ssam	case WI_RID_MICROWAVE_OVEN:
2013109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
2014109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2015109323Ssam			    &len);
2016109323Ssam			break;
2017109323Ssam		}
2018109323Ssam		wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
2019109323Ssam		len = sizeof(u_int16_t);
2020109323Ssam		break;
202146492Swpaul
2022109323Ssam	case WI_RID_DBM_ADJUST:
2023109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
2024109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2025109323Ssam			    &len);
2026109323Ssam			break;
2027109323Ssam		}
2028119784Ssam		wreq.wi_val[0] = htole16(sc->sc_dbm_offset);
2029109323Ssam		len = sizeof(u_int16_t);
2030109323Ssam		break;
203146492Swpaul
2032109323Ssam	case WI_RID_ROAMING_MODE:
2033109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
2034109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2035109323Ssam			    &len);
2036109323Ssam			break;
2037109323Ssam		}
2038109323Ssam		wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
2039109323Ssam		len = sizeof(u_int16_t);
2040109323Ssam		break;
204146492Swpaul
2042109323Ssam	case WI_RID_SYSTEM_SCALE:
2043109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
2044109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2045109323Ssam			    &len);
2046109323Ssam			break;
2047109323Ssam		}
2048109323Ssam		wreq.wi_val[0] = htole16(sc->sc_system_scale);
2049109323Ssam		len = sizeof(u_int16_t);
2050109323Ssam		break;
205146492Swpaul
2052109323Ssam	case WI_RID_FRAG_THRESH:
2053109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
2054109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2055109323Ssam			    &len);
2056109323Ssam			break;
2057109323Ssam		}
2058116951Ssam		wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
2059109323Ssam		len = sizeof(u_int16_t);
2060109323Ssam		break;
206146492Swpaul
2062109323Ssam	case WI_RID_READ_APS:
2063109323Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
2064138571Ssam			return ieee80211_cfgget(ic, cmd, data);
2065109323Ssam		if (sc->sc_scan_timer > 0) {
2066109323Ssam			error = EINPROGRESS;
2067109323Ssam			break;
2068109323Ssam		}
2069109323Ssam		n = sc->sc_naps;
2070109323Ssam		if (len < sizeof(n)) {
2071109323Ssam			error = ENOSPC;
2072109323Ssam			break;
2073109323Ssam		}
2074109323Ssam		if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
2075109323Ssam			n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
2076109323Ssam		len = sizeof(n) + sizeof(struct wi_apinfo) * n;
2077109323Ssam		memcpy(wreq.wi_val, &n, sizeof(n));
2078109323Ssam		memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
2079109323Ssam		    sizeof(struct wi_apinfo) * n);
2080109323Ssam		break;
208146492Swpaul
2082109323Ssam	case WI_RID_PRISM2:
2083109323Ssam		wreq.wi_val[0] = sc->sc_firmware_type != WI_LUCENT;
2084109323Ssam		len = sizeof(u_int16_t);
2085109323Ssam		break;
208646492Swpaul
2087109323Ssam	case WI_RID_MIF:
2088109323Ssam		mif = wreq.wi_val[0];
2089109323Ssam		error = wi_cmd(sc, WI_CMD_READMIF, mif, 0, 0);
2090109323Ssam		val = CSR_READ_2(sc, WI_RESP0);
2091109323Ssam		wreq.wi_val[0] = val;
2092109323Ssam		len = sizeof(u_int16_t);
2093109323Ssam		break;
209446492Swpaul
2095109323Ssam	case WI_RID_ZERO_CACHE:
2096109323Ssam	case WI_RID_PROCFRAME:		/* ignore for compatibility */
2097109323Ssam		/* XXX ??? */
2098109323Ssam		break;
209946492Swpaul
2100109323Ssam	case WI_RID_READ_CACHE:
2101138571Ssam		return ieee80211_cfgget(ic, cmd, data);
210246492Swpaul
2103116898Ssam	case WI_RID_SCAN_RES:		/* compatibility interface */
2104116898Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
2105138571Ssam			return ieee80211_cfgget(ic, cmd, data);
2106116898Ssam		if (sc->sc_scan_timer > 0) {
2107116898Ssam			error = EINPROGRESS;
2108116898Ssam			break;
2109116898Ssam		}
2110116898Ssam		n = sc->sc_naps;
2111122015Sgreen		if (sc->sc_firmware_type == WI_LUCENT) {
2112122015Sgreen			off = 0;
2113122015Sgreen			reslen = WI_WAVELAN_RES_SIZE;
2114122015Sgreen		} else {
2115122015Sgreen			off = sizeof(struct wi_scan_p2_hdr);
2116122015Sgreen			reslen = WI_PRISM2_RES_SIZE;
2117122015Sgreen		}
2118122015Sgreen		if (len < off + reslen * n)
2119122015Sgreen			n = (len - off) / reslen;
2120122015Sgreen		len = off + reslen * n;
2121116898Ssam		if (off != 0) {
2122116898Ssam			struct wi_scan_p2_hdr *p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
2123116898Ssam			/*
2124116898Ssam			 * Prepend Prism-specific header.
2125116898Ssam			 */
2126116898Ssam			if (len < sizeof(struct wi_scan_p2_hdr)) {
2127116898Ssam				error = ENOSPC;
2128116898Ssam				break;
2129116898Ssam			}
2130116898Ssam			p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
2131116898Ssam			p2->wi_rsvd = 0;
2132116898Ssam			p2->wi_reason = n;	/* XXX */
2133116898Ssam		}
2134122015Sgreen		for (i = 0; i < n; i++, off += reslen) {
2135116898Ssam			const struct wi_apinfo *ap = &sc->sc_aps[i];
2136116898Ssam
2137116898Ssam			res = (struct wi_scan_res *)((char *)wreq.wi_val + off);
2138116898Ssam			res->wi_chan = ap->channel;
2139116898Ssam			res->wi_noise = ap->noise;
2140116898Ssam			res->wi_signal = ap->signal;
2141116898Ssam			IEEE80211_ADDR_COPY(res->wi_bssid, ap->bssid);
2142116898Ssam			res->wi_interval = ap->interval;
2143116898Ssam			res->wi_capinfo = ap->capinfo;
2144116898Ssam			res->wi_ssid_len = ap->namelen;
2145116898Ssam			memcpy(res->wi_ssid, ap->name,
2146116898Ssam				IEEE80211_NWID_LEN);
2147116898Ssam			if (sc->sc_firmware_type != WI_LUCENT) {
2148116898Ssam				/* XXX not saved from Prism cards */
2149116898Ssam				memset(res->wi_srates, 0,
2150116898Ssam					sizeof(res->wi_srates));
2151116898Ssam				res->wi_rate = ap->rate;
2152116898Ssam				res->wi_rsvd = 0;
2153122015Sgreen			}
2154116898Ssam		}
2155116898Ssam		break;
2156116898Ssam
2157109323Ssam	default:
2158109323Ssam		if (sc->sc_enabled) {
2159109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2160109323Ssam			    &len);
2161109323Ssam			break;
2162109323Ssam		}
2163109323Ssam		switch (wreq.wi_type) {
2164109323Ssam		case WI_RID_MAX_DATALEN:
2165109323Ssam			wreq.wi_val[0] = htole16(sc->sc_max_datalen);
2166109323Ssam			len = sizeof(u_int16_t);
2167109323Ssam			break;
2168109323Ssam		case WI_RID_RTS_THRESH:
2169116951Ssam			wreq.wi_val[0] = htole16(ic->ic_rtsthreshold);
2170109323Ssam			len = sizeof(u_int16_t);
2171109323Ssam			break;
2172109323Ssam		case WI_RID_CNFAUTHMODE:
2173109323Ssam			wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
2174109323Ssam			len = sizeof(u_int16_t);
2175109323Ssam			break;
2176109323Ssam		case WI_RID_NODENAME:
2177109323Ssam			if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
2178109323Ssam				error = ENOSPC;
2179109323Ssam				break;
2180109323Ssam			}
2181109323Ssam			len = sc->sc_nodelen + sizeof(u_int16_t);
2182109323Ssam			wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
2183109323Ssam			memcpy(&wreq.wi_val[1], sc->sc_nodename,
2184109323Ssam			    sc->sc_nodelen);
2185109323Ssam			break;
2186109323Ssam		default:
2187138571Ssam			return ieee80211_cfgget(ic, cmd, data);
2188109323Ssam		}
2189109323Ssam		break;
219046492Swpaul	}
2191109323Ssam	if (error)
2192109323Ssam		return error;
2193109323Ssam	wreq.wi_len = (len + 1) / 2 + 1;
2194109323Ssam	return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
219546492Swpaul}
219646492Swpaul
2197109323Ssamstatic int
2198109323Ssamwi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
219946492Swpaul{
2200109323Ssam	struct wi_softc *sc = ifp->if_softc;
2201109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2202109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
2203109323Ssam	struct wi_req wreq;
2204109323Ssam	struct mbuf *m;
2205109323Ssam	int i, len, error, mif, val;
2206116951Ssam	struct ieee80211_rateset *rs;
2207138571Ssam	WI_LOCK_DECL();
220846492Swpaul
2209109323Ssam	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2210109323Ssam	if (error)
2211109323Ssam		return error;
2212116898Ssam	len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0;
2213109323Ssam	switch (wreq.wi_type) {
2214109323Ssam	case WI_RID_DBM_ADJUST:
2215109323Ssam		return ENODEV;
221667092Swpaul
2217109323Ssam	case WI_RID_NODENAME:
2218109323Ssam		if (le16toh(wreq.wi_val[0]) * 2 > len ||
2219109323Ssam		    le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
2220109323Ssam			error = ENOSPC;
2221109323Ssam			break;
2222109323Ssam		}
2223138571Ssam		WI_LOCK(sc);
2224138571Ssam		if (sc->sc_enabled)
2225109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2226109323Ssam			    len);
2227138571Ssam		if (error == 0) {
2228138571Ssam			sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
2229138571Ssam			memcpy(sc->sc_nodename, &wreq.wi_val[1],
2230138571Ssam				sc->sc_nodelen);
2231109323Ssam		}
2232138571Ssam		WI_UNLOCK(sc);
2233109323Ssam		break;
223446492Swpaul
2235109323Ssam	case WI_RID_MICROWAVE_OVEN:
2236109323Ssam	case WI_RID_ROAMING_MODE:
2237109323Ssam	case WI_RID_SYSTEM_SCALE:
2238109323Ssam	case WI_RID_FRAG_THRESH:
2239138571Ssam		/* XXX unlocked reads */
2240109323Ssam		if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
2241109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
2242109323Ssam			break;
2243109323Ssam		if (wreq.wi_type == WI_RID_ROAMING_MODE &&
2244109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
2245109323Ssam			break;
2246109323Ssam		if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
2247109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
2248109323Ssam			break;
2249109323Ssam		if (wreq.wi_type == WI_RID_FRAG_THRESH &&
2250109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
2251109323Ssam			break;
2252109323Ssam		/* FALLTHROUGH */
2253109323Ssam	case WI_RID_RTS_THRESH:
2254109323Ssam	case WI_RID_CNFAUTHMODE:
2255109323Ssam	case WI_RID_MAX_DATALEN:
2256138571Ssam		WI_LOCK(sc);
2257109323Ssam		if (sc->sc_enabled) {
2258109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2259109323Ssam			    sizeof(u_int16_t));
2260138571Ssam			if (error != 0) {
2261138571Ssam				WI_UNLOCK(sc);
2262109323Ssam				break;
2263138571Ssam			}
2264109323Ssam		}
2265109323Ssam		switch (wreq.wi_type) {
2266109323Ssam		case WI_RID_FRAG_THRESH:
2267116951Ssam			ic->ic_fragthreshold = le16toh(wreq.wi_val[0]);
2268109323Ssam			break;
2269109323Ssam		case WI_RID_RTS_THRESH:
2270116951Ssam			ic->ic_rtsthreshold = le16toh(wreq.wi_val[0]);
2271109323Ssam			break;
2272109323Ssam		case WI_RID_MICROWAVE_OVEN:
2273109323Ssam			sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
2274109323Ssam			break;
2275109323Ssam		case WI_RID_ROAMING_MODE:
2276109323Ssam			sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
2277109323Ssam			break;
2278109323Ssam		case WI_RID_SYSTEM_SCALE:
2279109323Ssam			sc->sc_system_scale = le16toh(wreq.wi_val[0]);
2280109323Ssam			break;
2281109323Ssam		case WI_RID_CNFAUTHMODE:
2282109323Ssam			sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
2283109323Ssam			break;
2284109323Ssam		case WI_RID_MAX_DATALEN:
2285109323Ssam			sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
2286109323Ssam			break;
2287109323Ssam		}
2288138571Ssam		WI_UNLOCK(sc);
2289109323Ssam		break;
229094405Simp
2291109323Ssam	case WI_RID_TX_RATE:
2292138571Ssam		WI_LOCK(sc);
2293109323Ssam		switch (le16toh(wreq.wi_val[0])) {
2294109323Ssam		case 3:
2295148290Ssam			ic->ic_fixed_rate = IEEE80211_FIXED_RATE_NONE;
2296109323Ssam			break;
2297109323Ssam		default:
2298116951Ssam			rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
2299116951Ssam			for (i = 0; i < rs->rs_nrates; i++) {
2300116951Ssam				if ((rs->rs_rates[i] & IEEE80211_RATE_VAL)
2301109323Ssam				    / 2 == le16toh(wreq.wi_val[0]))
2302109323Ssam					break;
2303109323Ssam			}
2304138571Ssam			if (i == rs->rs_nrates) {
2305138571Ssam				WI_UNLOCK(sc);
2306109323Ssam				return EINVAL;
2307138571Ssam			}
2308109323Ssam			ic->ic_fixed_rate = i;
2309109323Ssam		}
2310109323Ssam		if (sc->sc_enabled)
2311109323Ssam			error = wi_write_txrate(sc);
2312138571Ssam		WI_UNLOCK(sc);
2313109323Ssam		break;
231446492Swpaul
2315109323Ssam	case WI_RID_SCAN_APS:
2316138571Ssam		WI_LOCK(sc);
2317109323Ssam		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2318116898Ssam			error = wi_scan_ap(sc, 0x3fff, 0x000f);
2319138571Ssam		WI_UNLOCK(sc);
2320109323Ssam		break;
232146492Swpaul
2322116898Ssam	case WI_RID_SCAN_REQ:		/* compatibility interface */
2323138571Ssam		WI_LOCK(sc);
2324116898Ssam		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2325116898Ssam			error = wi_scan_ap(sc, wreq.wi_val[0], wreq.wi_val[1]);
2326138571Ssam		WI_UNLOCK(sc);
2327116898Ssam		break;
2328116898Ssam
2329109323Ssam	case WI_RID_MGMT_XMIT:
2330138571Ssam		WI_LOCK(sc);
2331138571Ssam		if (!sc->sc_enabled)
2332109323Ssam			error = ENETDOWN;
2333138571Ssam		else if (ic->ic_mgtq.ifq_len > 5)
2334109323Ssam			error = EAGAIN;
2335138571Ssam		else {
2336138571Ssam			/* NB: m_devget uses M_DONTWAIT so can hold the lock */
2337138571Ssam			/* XXX wi_len looks in u_int8_t, not in u_int16_t */
2338138571Ssam			m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0,
2339138571Ssam				ifp, NULL);
2340138571Ssam			if (m != NULL)
2341138571Ssam				IF_ENQUEUE(&ic->ic_mgtq, m);
2342138571Ssam			else
2343138571Ssam				error = ENOMEM;
2344109323Ssam		}
2345138571Ssam		WI_UNLOCK(sc);
2346109323Ssam		break;
234746492Swpaul
2348109323Ssam	case WI_RID_MIF:
2349109323Ssam		mif = wreq.wi_val[0];
2350109323Ssam		val = wreq.wi_val[1];
2351138571Ssam		WI_LOCK(sc);
2352109323Ssam		error = wi_cmd(sc, WI_CMD_WRITEMIF, mif, val, 0);
2353138571Ssam		WI_UNLOCK(sc);
2354109323Ssam		break;
235546492Swpaul
2356109323Ssam	case WI_RID_PROCFRAME:		/* ignore for compatibility */
2357109323Ssam		break;
2358109323Ssam
2359116898Ssam	case WI_RID_OWN_SSID:
2360116898Ssam		if (le16toh(wreq.wi_val[0]) * 2 > len ||
2361116898Ssam		    le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) {
2362116898Ssam			error = ENOSPC;
2363116898Ssam			break;
2364116898Ssam		}
2365138571Ssam		WI_LOCK(sc);
2366116898Ssam		memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
2367116898Ssam		ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2;
2368116898Ssam		memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen);
2369138571Ssam		if (sc->sc_enabled)
2370138571Ssam			wi_init(sc);	/* XXX no error return */
2371138571Ssam		WI_UNLOCK(sc);
2372116898Ssam		break;
2373116898Ssam
2374109323Ssam	default:
2375138571Ssam		WI_LOCK(sc);
2376138571Ssam		if (sc->sc_enabled)
2377109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2378109323Ssam			    len);
2379138571Ssam		if (error == 0) {
2380138571Ssam			/* XXX ieee80211_cfgset does a copyin */
2381138571Ssam			error = ieee80211_cfgset(ic, cmd, data);
2382138571Ssam			if (error == ENETRESET) {
2383138571Ssam				if (sc->sc_enabled)
2384138571Ssam					wi_init(sc);
2385138571Ssam				error = 0;
2386138571Ssam			}
2387109323Ssam		}
2388138571Ssam		WI_UNLOCK(sc);
2389109323Ssam		break;
2390109323Ssam	}
2391109323Ssam	return error;
239246492Swpaul}
239346492Swpaul
2394109323Ssamstatic int
2395109323Ssamwi_write_txrate(struct wi_softc *sc)
239646492Swpaul{
2397109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2398109323Ssam	int i;
2399109323Ssam	u_int16_t rate;
240046492Swpaul
2401148290Ssam	if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
2402109323Ssam		rate = 0;	/* auto */
2403109323Ssam	else
2404116951Ssam		rate = (ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ic->ic_fixed_rate] &
2405109323Ssam		    IEEE80211_RATE_VAL) / 2;
240646492Swpaul
2407109323Ssam	/* rate: 0, 1, 2, 5, 11 */
240846492Swpaul
2409109323Ssam	switch (sc->sc_firmware_type) {
2410109323Ssam	case WI_LUCENT:
2411112501Simp		switch (rate) {
2412112501Simp		case 0:			/* auto == 11mbps auto */
2413112501Simp			rate = 3;
2414112501Simp			break;
2415112501Simp		/* case 1, 2 map to 1, 2*/
2416112501Simp		case 5:			/* 5.5Mbps -> 4 */
2417112501Simp			rate = 4;
2418112501Simp			break;
2419112501Simp		case 11:		/* 11mbps -> 5 */
2420112501Simp			rate = 5;
2421112501Simp			break;
2422112501Simp		default:
2423112501Simp			break;
2424112501Simp		}
2425109323Ssam		break;
2426109323Ssam	default:
2427109323Ssam		/* Choose a bit according to this table.
2428109323Ssam		 *
2429109323Ssam		 * bit | data rate
2430109323Ssam		 * ----+-------------------
2431109323Ssam		 * 0   | 1Mbps
2432109323Ssam		 * 1   | 2Mbps
2433109323Ssam		 * 2   | 5.5Mbps
2434109323Ssam		 * 3   | 11Mbps
2435109323Ssam		 */
2436109323Ssam		for (i = 8; i > 0; i >>= 1) {
2437109323Ssam			if (rate >= i)
2438109323Ssam				break;
2439109323Ssam		}
2440109323Ssam		if (i == 0)
2441109323Ssam			rate = 0xf;	/* auto */
2442109323Ssam		else
2443109323Ssam			rate = i;
2444109323Ssam		break;
2445109323Ssam	}
2446109323Ssam	return wi_write_val(sc, WI_RID_TX_RATE, rate);
2447109323Ssam}
244846492Swpaul
2449109323Ssamstatic int
2450150798Savatarwi_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k,
2451150798Savatar	ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
2452150798Savatar{
2453150798Savatar	struct wi_softc *sc = ic->ic_ifp->if_softc;
2454150798Savatar
2455150798Savatar	/*
2456150798Savatar	 * When doing host encryption of outbound frames fail requests
2457150798Savatar	 * for keys that are not marked w/ the SWCRYPT flag so the
2458150798Savatar	 * net80211 layer falls back to s/w crypto.  Note that we also
2459150798Savatar	 * fixup existing keys below to handle mode changes.
2460150798Savatar	 */
2461150798Savatar	if ((sc->sc_encryption & HOST_ENCRYPT) &&
2462150798Savatar	    (k->wk_flags & IEEE80211_KEY_SWCRYPT) == 0)
2463150798Savatar		return 0;
2464150798Savatar	return sc->sc_key_alloc(ic, k, keyix, rxkeyix);
2465150798Savatar}
2466150798Savatar
2467150798Savatarstatic int
2468109323Ssamwi_write_wep(struct wi_softc *sc)
2469109323Ssam{
2470109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2471109323Ssam	int error = 0;
2472109323Ssam	int i, keylen;
2473109323Ssam	u_int16_t val;
2474109323Ssam	struct wi_key wkey[IEEE80211_WEP_NKID];
247546492Swpaul
2476109323Ssam	switch (sc->sc_firmware_type) {
2477109323Ssam	case WI_LUCENT:
2478138571Ssam		val = (ic->ic_flags & IEEE80211_F_PRIVACY) ? 1 : 0;
2479109323Ssam		error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
2480109323Ssam		if (error)
2481109323Ssam			break;
2482138988Smdodd		if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0)
2483138988Smdodd			break;
2484138988Smdodd		error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_def_txkey);
2485138988Smdodd		if (error)
2486138988Smdodd			break;
2487138988Smdodd		memset(wkey, 0, sizeof(wkey));
2488138988Smdodd		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2489138988Smdodd			keylen = ic->ic_nw_keys[i].wk_keylen;
2490138988Smdodd			wkey[i].wi_keylen = htole16(keylen);
2491138988Smdodd			memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
2492138988Smdodd			    keylen);
2493109323Ssam		}
2494138988Smdodd		error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
2495138988Smdodd		    wkey, sizeof(wkey));
2496150798Savatar		sc->sc_encryption = 0;
2497109323Ssam		break;
2498109323Ssam
2499109323Ssam	case WI_INTERSIL:
2500109323Ssam	case WI_SYMBOL:
2501138571Ssam		if (ic->ic_flags & IEEE80211_F_PRIVACY) {
2502109323Ssam			/*
2503109323Ssam			 * ONLY HWB3163 EVAL-CARD Firmware version
2504109323Ssam			 * less than 0.8 variant2
2505109323Ssam			 *
2506109323Ssam			 *   If promiscuous mode disable, Prism2 chip
2507109323Ssam			 *  does not work with WEP .
2508109323Ssam			 * It is under investigation for details.
2509109323Ssam			 * (ichiro@netbsd.org)
2510109323Ssam			 */
2511109323Ssam			if (sc->sc_firmware_type == WI_INTERSIL &&
2512109323Ssam			    sc->sc_sta_firmware_ver < 802 ) {
2513109323Ssam				/* firm ver < 0.8 variant 2 */
2514109323Ssam				wi_write_val(sc, WI_RID_PROMISC, 1);
2515109323Ssam			}
2516109323Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE,
2517109323Ssam			    sc->sc_cnfauthmode);
2518150798Savatar			/* XXX should honor IEEE80211_F_DROPUNENC */
2519109323Ssam			val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
2520109323Ssam			/*
2521109323Ssam			 * Encryption firmware has a bug for HostAP mode.
2522109323Ssam			 */
2523109323Ssam			if (sc->sc_firmware_type == WI_INTERSIL &&
2524109323Ssam			    ic->ic_opmode == IEEE80211_M_HOSTAP)
2525109323Ssam				val |= HOST_ENCRYPT;
2526109323Ssam		} else {
2527109323Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE,
2528109323Ssam			    IEEE80211_AUTH_OPEN);
2529109323Ssam			val = HOST_ENCRYPT | HOST_DECRYPT;
2530109323Ssam		}
2531109323Ssam		error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
2532109323Ssam		if (error)
2533109323Ssam			break;
2534150798Savatar		sc->sc_encryption = val;
2535138988Smdodd		if ((val & PRIVACY_INVOKED) == 0)
2536138988Smdodd			break;
2537138988Smdodd		error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
2538138988Smdodd		    ic->ic_def_txkey);
2539138988Smdodd		if (error)
2540138988Smdodd			break;
2541138988Smdodd		if (val & HOST_DECRYPT)
2542138988Smdodd			break;
2543138988Smdodd		/*
2544138988Smdodd		 * It seems that the firmware accept 104bit key only if
2545138988Smdodd		 * all the keys have 104bit length.  We get the length of
2546138988Smdodd		 * the transmit key and use it for all other keys.
2547138988Smdodd		 * Perhaps we should use software WEP for such situation.
2548138988Smdodd		 */
2549138988Smdodd		if (ic->ic_def_txkey != IEEE80211_KEYIX_NONE)
2550138988Smdodd			keylen = ic->ic_nw_keys[ic->ic_def_txkey].wk_keylen;
2551138988Smdodd		else	/* XXX should not hapen */
2552138988Smdodd			keylen = IEEE80211_WEP_KEYLEN;
2553138988Smdodd		if (keylen > IEEE80211_WEP_KEYLEN)
2554138988Smdodd			keylen = 13;	/* 104bit keys */
2555138988Smdodd		else
2556138988Smdodd			keylen = IEEE80211_WEP_KEYLEN;
2557138988Smdodd		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2558138988Smdodd			error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
2559138988Smdodd			    ic->ic_nw_keys[i].wk_key, keylen);
2560109323Ssam			if (error)
2561109323Ssam				break;
2562109323Ssam		}
2563109323Ssam		break;
2564109323Ssam	}
2565150798Savatar	/*
2566150798Savatar	 * XXX horrible hack; insure pre-existing keys are
2567150798Savatar	 * setup properly to do s/w crypto.
2568150798Savatar	 */
2569150798Savatar	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2570150798Savatar		struct ieee80211_key *k = &ic->ic_nw_keys[i];
2571150798Savatar		if (k->wk_flags & IEEE80211_KEY_XMIT) {
2572150798Savatar			if (sc->sc_encryption & HOST_ENCRYPT)
2573150798Savatar				k->wk_flags |= IEEE80211_KEY_SWCRYPT;
2574150798Savatar			else
2575150798Savatar				k->wk_flags &= ~IEEE80211_KEY_SWCRYPT;
2576150798Savatar		}
2577150798Savatar	}
2578109323Ssam	return error;
257946492Swpaul}
258046492Swpaul
2581109323Ssamstatic int
2582109323Ssamwi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
258346492Swpaul{
2584109323Ssam	int			i, s = 0;
2585109323Ssam
2586123098Simp	if (sc->wi_gone)
2587123098Simp		return (ENODEV);
2588123098Simp
2589109323Ssam	/* wait for the busy bit to clear */
2590123339Simp	for (i = sc->wi_cmd_count; i > 0; i--) {	/* 500ms */
2591116206Simp		if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
2592109323Ssam			break;
2593123098Simp		DELAY(1*1000);	/* 1ms */
2594109323Ssam	}
2595109323Ssam	if (i == 0) {
2596109323Ssam		device_printf(sc->sc_dev, "wi_cmd: busy bit won't clear.\n" );
2597123098Simp		sc->wi_gone = 1;
2598109323Ssam		return(ETIMEDOUT);
2599109323Ssam	}
260090580Sbrooks
2601109323Ssam	CSR_WRITE_2(sc, WI_PARAM0, val0);
2602109323Ssam	CSR_WRITE_2(sc, WI_PARAM1, val1);
2603109323Ssam	CSR_WRITE_2(sc, WI_PARAM2, val2);
2604109323Ssam	CSR_WRITE_2(sc, WI_COMMAND, cmd);
260590580Sbrooks
2606109323Ssam	if (cmd == WI_CMD_INI) {
2607109323Ssam		/* XXX: should sleep here. */
2608116206Simp		DELAY(100*1000);		/* 100ms delay for init */
2609109323Ssam	}
2610109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
2611109323Ssam		/*
2612109323Ssam		 * Wait for 'command complete' bit to be
2613109323Ssam		 * set in the event status register.
2614109323Ssam		 */
2615109323Ssam		s = CSR_READ_2(sc, WI_EVENT_STAT);
2616109323Ssam		if (s & WI_EV_CMD) {
2617109323Ssam			/* Ack the event and read result code. */
2618109323Ssam			s = CSR_READ_2(sc, WI_STATUS);
2619109323Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
2620109323Ssam			if (s & WI_STAT_CMD_RESULT) {
2621109323Ssam				return(EIO);
2622109323Ssam			}
2623109323Ssam			break;
262490580Sbrooks		}
2625109323Ssam		DELAY(WI_DELAY);
2626109323Ssam	}
262790580Sbrooks
2628109323Ssam	if (i == WI_TIMEOUT) {
2629109323Ssam		device_printf(sc->sc_dev,
2630109323Ssam		    "timeout in wi_cmd 0x%04x; event status 0x%04x\n", cmd, s);
2631123098Simp		if (s == 0xffff)
2632123098Simp			sc->wi_gone = 1;
2633109323Ssam		return(ETIMEDOUT);
263453702Swpaul	}
2635109323Ssam	return (0);
2636109323Ssam}
263753702Swpaul
2638109323Ssamstatic int
2639109323Ssamwi_seek_bap(struct wi_softc *sc, int id, int off)
2640109323Ssam{
2641109323Ssam	int i, status;
264290580Sbrooks
2643109323Ssam	CSR_WRITE_2(sc, WI_SEL0, id);
2644109323Ssam	CSR_WRITE_2(sc, WI_OFF0, off);
264590580Sbrooks
2646109323Ssam	for (i = 0; ; i++) {
2647109323Ssam		status = CSR_READ_2(sc, WI_OFF0);
2648109323Ssam		if ((status & WI_OFF_BUSY) == 0)
2649109323Ssam			break;
2650109323Ssam		if (i == WI_TIMEOUT) {
2651109323Ssam			device_printf(sc->sc_dev, "timeout in wi_seek to %x/%x\n",
2652109323Ssam			    id, off);
2653109323Ssam			sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2654123098Simp			if (status == 0xffff)
2655123098Simp				sc->wi_gone = 1;
2656109323Ssam			return ETIMEDOUT;
2657109323Ssam		}
2658109323Ssam		DELAY(1);
265953702Swpaul	}
2660109323Ssam	if (status & WI_OFF_ERR) {
2661109323Ssam		device_printf(sc->sc_dev, "failed in wi_seek to %x/%x\n", id, off);
2662109323Ssam		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2663109323Ssam		return EIO;
2664109323Ssam	}
2665109323Ssam	sc->sc_bap_id = id;
2666109323Ssam	sc->sc_bap_off = off;
2667109323Ssam	return 0;
266853702Swpaul}
266953702Swpaul
2670109323Ssamstatic int
2671109323Ssamwi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
267253702Swpaul{
2673109323Ssam	u_int16_t *ptr;
2674109323Ssam	int i, error, cnt;
267553702Swpaul
2676109323Ssam	if (buflen == 0)
2677109323Ssam		return 0;
2678109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2679109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
2680109323Ssam			return error;
268175219Salfred	}
2682109323Ssam	cnt = (buflen + 1) / 2;
2683109323Ssam	ptr = (u_int16_t *)buf;
2684109323Ssam	for (i = 0; i < cnt; i++)
2685109323Ssam		*ptr++ = CSR_READ_2(sc, WI_DATA0);
2686109323Ssam	sc->sc_bap_off += cnt * 2;
2687109323Ssam	return 0;
268853702Swpaul}
268953702Swpaul
2690109323Ssamstatic int
2691109323Ssamwi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
269253702Swpaul{
2693109323Ssam	u_int16_t *ptr;
2694109323Ssam	int i, error, cnt;
269546492Swpaul
2696109323Ssam	if (buflen == 0)
2697109323Ssam		return 0;
269846492Swpaul
2699109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
2700109323Ssam  again:
2701109323Ssam#endif
2702109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2703109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
2704109323Ssam			return error;
2705109323Ssam	}
2706109323Ssam	cnt = (buflen + 1) / 2;
2707109323Ssam	ptr = (u_int16_t *)buf;
2708109323Ssam	for (i = 0; i < cnt; i++)
2709109323Ssam		CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
2710109323Ssam	sc->sc_bap_off += cnt * 2;
2711109323Ssam
2712109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
2713109323Ssam	/*
2714109323Ssam	 * According to the comments in the HCF Light code, there is a bug
2715109323Ssam	 * in the Hermes (or possibly in certain Hermes firmware revisions)
2716109323Ssam	 * where the chip's internal autoincrement counter gets thrown off
2717109323Ssam	 * during data writes:  the autoincrement is missed, causing one
2718109323Ssam	 * data word to be overwritten and subsequent words to be written to
2719109323Ssam	 * the wrong memory locations. The end result is that we could end
2720109323Ssam	 * up transmitting bogus frames without realizing it. The workaround
2721109323Ssam	 * for this is to write a couple of extra guard words after the end
2722109323Ssam	 * of the transfer, then attempt to read then back. If we fail to
2723109323Ssam	 * locate the guard words where we expect them, we preform the
2724109323Ssam	 * transfer over again.
2725109323Ssam	 */
2726109323Ssam	if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
2727109323Ssam		CSR_WRITE_2(sc, WI_DATA0, 0x1234);
2728109323Ssam		CSR_WRITE_2(sc, WI_DATA0, 0x5678);
2729109323Ssam		wi_seek_bap(sc, id, sc->sc_bap_off);
2730109323Ssam		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2731109323Ssam		if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
2732109323Ssam		    CSR_READ_2(sc, WI_DATA0) != 0x5678) {
2733109323Ssam			device_printf(sc->sc_dev,
2734109323Ssam				"detect auto increment bug, try again\n");
2735109323Ssam			goto again;
2736109323Ssam		}
2737109323Ssam	}
2738109323Ssam#endif
2739109323Ssam	return 0;
274046492Swpaul}
274153702Swpaul
2742109323Ssamstatic int
2743109323Ssamwi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
2744109323Ssam{
2745109323Ssam	int error, len;
2746109323Ssam	struct mbuf *m;
274753702Swpaul
2748109323Ssam	for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
2749109323Ssam		if (m->m_len == 0)
2750109323Ssam			continue;
275153702Swpaul
2752109323Ssam		len = min(m->m_len, totlen);
275353702Swpaul
2754109323Ssam		if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
2755109323Ssam			m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
2756109323Ssam			return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
2757109323Ssam			    totlen);
2758109323Ssam		}
275953702Swpaul
2760109323Ssam		if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
2761109323Ssam			return error;
276253702Swpaul
2763109323Ssam		off += m->m_len;
2764109323Ssam		totlen -= len;
2765109323Ssam	}
2766109323Ssam	return 0;
2767109323Ssam}
276853702Swpaul
2769109323Ssamstatic int
2770109323Ssamwi_alloc_fid(struct wi_softc *sc, int len, int *idp)
277153702Swpaul{
277253702Swpaul	int i;
277353702Swpaul
2774109323Ssam	if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
2775109323Ssam		device_printf(sc->sc_dev, "failed to allocate %d bytes on NIC\n",
2776109323Ssam		    len);
2777109323Ssam		return ENOMEM;
277853702Swpaul	}
277953702Swpaul
2780109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
2781109323Ssam		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
2782109323Ssam			break;
2783109323Ssam		DELAY(1);
278453702Swpaul	}
2785144167Ssam	if (i == WI_TIMEOUT) {
2786144167Ssam		device_printf(sc->sc_dev, "timeout in alloc\n");
2787144167Ssam		return ETIMEDOUT;
2788144167Ssam	}
2789109323Ssam	*idp = CSR_READ_2(sc, WI_ALLOC_FID);
2790109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
2791109323Ssam	return 0;
2792109323Ssam}
279353702Swpaul
2794109323Ssamstatic int
2795109323Ssamwi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
2796109323Ssam{
2797109323Ssam	int error, len;
2798109323Ssam	u_int16_t ltbuf[2];
279953702Swpaul
2800109323Ssam	/* Tell the NIC to enter record read mode. */
2801109323Ssam	error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2802109323Ssam	if (error)
2803109323Ssam		return error;
280453702Swpaul
2805109323Ssam	error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2806109323Ssam	if (error)
2807109323Ssam		return error;
280853702Swpaul
2809109323Ssam	if (le16toh(ltbuf[1]) != rid) {
2810109323Ssam		device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n",
2811109323Ssam		    rid, le16toh(ltbuf[1]));
2812109323Ssam		return EIO;
281353702Swpaul	}
2814109323Ssam	len = (le16toh(ltbuf[0]) - 1) * 2;	 /* already got rid */
2815109323Ssam	if (*buflenp < len) {
2816109323Ssam		device_printf(sc->sc_dev, "record buffer is too small, "
2817109323Ssam		    "rid=%x, size=%d, len=%d\n",
2818109323Ssam		    rid, *buflenp, len);
2819109323Ssam		return ENOSPC;
282053702Swpaul	}
2821109323Ssam	*buflenp = len;
2822109323Ssam	return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2823109323Ssam}
282453702Swpaul
2825109323Ssamstatic int
2826109323Ssamwi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2827109323Ssam{
2828109323Ssam	int error;
2829109323Ssam	u_int16_t ltbuf[2];
283053702Swpaul
2831109323Ssam	ltbuf[0] = htole16((buflen + 1) / 2 + 1);	 /* includes rid */
2832109323Ssam	ltbuf[1] = htole16(rid);
283353702Swpaul
2834109323Ssam	error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2835109323Ssam	if (error)
2836109323Ssam		return error;
2837109323Ssam	error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2838109323Ssam	if (error)
2839109323Ssam		return error;
2840102206Simp
2841109323Ssam	return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
284253702Swpaul}
284377217Sphk
284488546Salfredstatic int
2845117812Ssamwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
284677217Sphk{
2847138571Ssam	struct ifnet *ifp = ic->ic_ifp;
2848117812Ssam	struct wi_softc *sc = ifp->if_softc;
2849139542Ssam	struct ieee80211_node *ni;
2850116951Ssam	int buflen;
2851109323Ssam	u_int16_t val;
2852109323Ssam	struct wi_ssid ssid;
2853109323Ssam	u_int8_t old_bssid[IEEE80211_ADDR_LEN];
285477217Sphk
2855117812Ssam	DPRINTF(("%s: %s -> %s\n", __func__,
2856117812Ssam		ieee80211_state_name[ic->ic_state],
2857117812Ssam		ieee80211_state_name[nstate]));
2858109323Ssam
2859139542Ssam	/*
2860139542Ssam	 * Internal to the driver the INIT and RUN states are used
2861139542Ssam	 * so bypass the net80211 state machine for other states.
2862139542Ssam	 * Beware however that this requires use to net80211 state
2863139542Ssam	 * management that otherwise would be handled for us.
2864139542Ssam	 */
2865109323Ssam	switch (nstate) {
2866109323Ssam	case IEEE80211_S_INIT:
2867109323Ssam		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2868117812Ssam		return (*sc->sc_newstate)(ic, nstate, arg);
2869109323Ssam
2870138571Ssam	case IEEE80211_S_SCAN:
2871138571Ssam	case IEEE80211_S_AUTH:
2872138571Ssam	case IEEE80211_S_ASSOC:
2873138571Ssam		ic->ic_state = nstate;	/* NB: skip normal ieee80211 handling */
2874138571Ssam		break;
2875138571Ssam
2876109323Ssam	case IEEE80211_S_RUN:
2877139542Ssam		ni = ic->ic_bss;
2878109323Ssam		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2879109323Ssam		buflen = IEEE80211_ADDR_LEN;
2880138571Ssam		IEEE80211_ADDR_COPY(old_bssid, ni->ni_bssid);
2881109323Ssam		wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen);
2882109323Ssam		IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
2883109323Ssam		buflen = sizeof(val);
2884109323Ssam		wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
2885116951Ssam		/* XXX validate channel */
2886116951Ssam		ni->ni_chan = &ic->ic_channels[le16toh(val)];
2887148936Ssam		ic->ic_curchan = ni->ni_chan;
2888138951Ssam		ic->ic_ibss_chan = ni->ni_chan;
2889119784Ssam#if NBPFILTER > 0
2890119784Ssam		sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
2891119784Ssam			htole16(ni->ni_chan->ic_freq);
2892119784Ssam		sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
2893119784Ssam			htole16(ni->ni_chan->ic_flags);
2894119784Ssam#endif
2895150798Savatar		/*
2896150798Savatar		 * XXX hack; unceremoniously clear
2897150798Savatar		 * IEEE80211_F_DROPUNENC when operating with
2898150798Savatar		 * wep enabled so we don't drop unencoded frames
2899150798Savatar		 * at the 802.11 layer.  This is necessary because
2900150798Savatar		 * we must strip the WEP bit from the 802.11 header
2901150798Savatar		 * before passing frames to ieee80211_input because
2902150798Savatar		 * the card has already stripped the WEP crypto
2903150798Savatar		 * header from the packet.
2904150798Savatar		 */
2905150798Savatar		if (ic->ic_flags & IEEE80211_F_PRIVACY)
2906150798Savatar			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
2907139542Ssam		if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
2908109323Ssam			/* XXX check return value */
2909109323Ssam			buflen = sizeof(ssid);
2910109323Ssam			wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
2911109323Ssam			ni->ni_esslen = le16toh(ssid.wi_len);
2912109323Ssam			if (ni->ni_esslen > IEEE80211_NWID_LEN)
2913109323Ssam				ni->ni_esslen = IEEE80211_NWID_LEN;	/*XXX*/
2914109323Ssam			memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
291577217Sphk		}
2916138571Ssam		return (*sc->sc_newstate)(ic, nstate, arg);
291777217Sphk	}
2918117812Ssam	return 0;
291977217Sphk}
292077217Sphk
292188546Salfredstatic int
2922116898Ssamwi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate)
292377217Sphk{
2924109323Ssam	int error = 0;
2925109323Ssam	u_int16_t val[2];
292677217Sphk
2927109323Ssam	if (!sc->sc_enabled)
2928109323Ssam		return ENXIO;
2929109323Ssam	switch (sc->sc_firmware_type) {
2930109323Ssam	case WI_LUCENT:
2931109323Ssam		(void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
293298440Simp		break;
2933109323Ssam	case WI_INTERSIL:
2934138571Ssam		val[0] = htole16(chanmask);	/* channel */
2935138571Ssam		val[1] = htole16(txrate);	/* tx rate */
2936109323Ssam		error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
293798440Simp		break;
2938109323Ssam	case WI_SYMBOL:
2939109323Ssam		/*
2940109323Ssam		 * XXX only supported on 3.x ?
2941109323Ssam		 */
2942109323Ssam		val[0] = BSCAN_BCAST | BSCAN_ONETIME;
2943109323Ssam		error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
2944109323Ssam		    val, sizeof(val[0]));
294598440Simp		break;
294698440Simp	}
2947109323Ssam	if (error == 0) {
2948109323Ssam		sc->sc_scan_timer = WI_SCAN_WAIT;
2949147256Sbrooks		sc->sc_ifp->if_timer = 1;
2950116898Ssam		DPRINTF(("wi_scan_ap: start scanning, "
2951116898Ssam			"chamask 0x%x txrate 0x%x\n", chanmask, txrate));
2952109323Ssam	}
2953109323Ssam	return error;
2954109323Ssam}
295577217Sphk
2956109323Ssamstatic void
2957109323Ssamwi_scan_result(struct wi_softc *sc, int fid, int cnt)
2958109323Ssam{
2959109323Ssam#define	N(a)	(sizeof (a) / sizeof (a[0]))
2960109323Ssam	int i, naps, off, szbuf;
2961109323Ssam	struct wi_scan_header ws_hdr;	/* Prism2 header */
2962109323Ssam	struct wi_scan_data_p2 ws_dat;	/* Prism2 scantable*/
2963109323Ssam	struct wi_apinfo *ap;
2964109323Ssam
2965109323Ssam	off = sizeof(u_int16_t) * 2;
2966109323Ssam	memset(&ws_hdr, 0, sizeof(ws_hdr));
2967109323Ssam	switch (sc->sc_firmware_type) {
2968109323Ssam	case WI_INTERSIL:
2969109323Ssam		wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
2970109323Ssam		off += sizeof(ws_hdr);
2971109323Ssam		szbuf = sizeof(struct wi_scan_data_p2);
297277217Sphk		break;
2973109323Ssam	case WI_SYMBOL:
2974109323Ssam		szbuf = sizeof(struct wi_scan_data_p2) + 6;
297577217Sphk		break;
2976109323Ssam	case WI_LUCENT:
2977109323Ssam		szbuf = sizeof(struct wi_scan_data);
297877217Sphk		break;
2979109323Ssam	default:
2980109323Ssam		device_printf(sc->sc_dev,
2981109323Ssam			"wi_scan_result: unknown firmware type %u\n",
2982109323Ssam			sc->sc_firmware_type);
2983109323Ssam		naps = 0;
2984109323Ssam		goto done;
298577217Sphk	}
2986109323Ssam	naps = (cnt * 2 + 2 - off) / szbuf;
2987109323Ssam	if (naps > N(sc->sc_aps))
2988109323Ssam		naps = N(sc->sc_aps);
2989109323Ssam	sc->sc_naps = naps;
2990109323Ssam	/* Read Data */
2991109323Ssam	ap = sc->sc_aps;
2992109323Ssam	memset(&ws_dat, 0, sizeof(ws_dat));
2993109323Ssam	for (i = 0; i < naps; i++, ap++) {
2994109323Ssam		wi_read_bap(sc, fid, off, &ws_dat,
2995109323Ssam		    (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
2996109323Ssam		DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off,
2997109323Ssam		    ether_sprintf(ws_dat.wi_bssid)));
2998109323Ssam		off += szbuf;
2999109323Ssam		ap->scanreason = le16toh(ws_hdr.wi_reason);
3000109323Ssam		memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
3001109323Ssam		ap->channel = le16toh(ws_dat.wi_chid);
3002109323Ssam		ap->signal  = le16toh(ws_dat.wi_signal);
3003109323Ssam		ap->noise   = le16toh(ws_dat.wi_noise);
3004109323Ssam		ap->quality = ap->signal - ap->noise;
3005109323Ssam		ap->capinfo = le16toh(ws_dat.wi_capinfo);
3006109323Ssam		ap->interval = le16toh(ws_dat.wi_interval);
3007109323Ssam		ap->rate    = le16toh(ws_dat.wi_rate);
3008109323Ssam		ap->namelen = le16toh(ws_dat.wi_namelen);
3009109323Ssam		if (ap->namelen > sizeof(ap->name))
3010109323Ssam			ap->namelen = sizeof(ap->name);
3011109323Ssam		memcpy(ap->name, ws_dat.wi_name, ap->namelen);
3012109323Ssam	}
3013109323Ssamdone:
3014109323Ssam	/* Done scanning */
3015109323Ssam	sc->sc_scan_timer = 0;
3016109323Ssam	DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
3017109323Ssam#undef N
301877217Sphk}
301977217Sphk
302088546Salfredstatic void
3021109323Ssamwi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi)
302277217Sphk{
3023109323Ssam	ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr),
3024116951Ssam	    ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL : -1, rssi);
3025109323Ssam	printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n",
3026109323Ssam		le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1),
3027109323Ssam		le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence);
3028109323Ssam	printf(" rx_signal %u rx_rate %u rx_flow %u\n",
3029109323Ssam		wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow);
3030109323Ssam	printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n",
3031109323Ssam		wh->wi_tx_rtry, wh->wi_tx_rate,
3032109323Ssam		le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len));
3033109323Ssam	printf(" ehdr dst %6D src %6D type 0x%x\n",
3034109323Ssam		wh->wi_ehdr.ether_dhost, ":", wh->wi_ehdr.ether_shost, ":",
3035109323Ssam		wh->wi_ehdr.ether_type);
3036109323Ssam}
303777217Sphk
3038109323Ssamint
3039109323Ssamwi_alloc(device_t dev, int rid)
3040109323Ssam{
3041109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
3042109323Ssam
3043109323Ssam	if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
3044109323Ssam		sc->iobase_rid = rid;
3045109323Ssam		sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
3046109323Ssam		    &sc->iobase_rid, 0, ~0, (1 << 6),
3047109323Ssam		    rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
3048109323Ssam		if (!sc->iobase) {
3049109323Ssam			device_printf(dev, "No I/O space?!\n");
3050109323Ssam			return (ENXIO);
305198440Simp		}
3052109323Ssam
3053109323Ssam		sc->wi_io_addr = rman_get_start(sc->iobase);
3054109323Ssam		sc->wi_btag = rman_get_bustag(sc->iobase);
3055109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->iobase);
3056109323Ssam	} else {
3057109323Ssam		sc->mem_rid = rid;
3058127135Snjl		sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
3059127135Snjl		    &sc->mem_rid, RF_ACTIVE);
3060109323Ssam
3061109323Ssam		if (!sc->mem) {
3062109323Ssam			device_printf(dev, "No Mem space on prism2.5?\n");
3063109323Ssam			return (ENXIO);
306477217Sphk		}
3065109323Ssam
3066109323Ssam		sc->wi_btag = rman_get_bustag(sc->mem);
3067109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->mem);
306877217Sphk	}
306977217Sphk
3070109323Ssam
3071109323Ssam	sc->irq_rid = 0;
3072127135Snjl	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
3073127135Snjl	    RF_ACTIVE |
3074109323Ssam	    ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
3075109323Ssam
3076109323Ssam	if (!sc->irq) {
3077109323Ssam		wi_free(dev);
3078109323Ssam		device_printf(dev, "No irq?!\n");
3079109323Ssam		return (ENXIO);
308077217Sphk	}
3081109323Ssam
3082109323Ssam	sc->sc_dev = dev;
3083109323Ssam	sc->sc_unit = device_get_unit(dev);
3084109323Ssam
3085109323Ssam	return (0);
308677217Sphk}
308793359Simp
3088109323Ssamvoid
3089109323Ssamwi_free(device_t dev)
3090109323Ssam{
3091109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
3092109323Ssam
3093109323Ssam	if (sc->iobase != NULL) {
3094109323Ssam		bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
3095109323Ssam		sc->iobase = NULL;
3096109323Ssam	}
3097109323Ssam	if (sc->irq != NULL) {
3098109323Ssam		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
3099109323Ssam		sc->irq = NULL;
3100109323Ssam	}
3101109323Ssam	if (sc->mem != NULL) {
3102109323Ssam		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
3103109323Ssam		sc->mem = NULL;
3104109323Ssam	}
3105109323Ssam
3106109323Ssam	return;
3107109323Ssam}
3108109323Ssam
310993359Simpstatic int
3110109323Ssamwi_get_debug(struct wi_softc *sc, struct wi_req *wreq)
311193359Simp{
3112109323Ssam	int error = 0;
311393359Simp
311493359Simp	wreq->wi_len = 1;
311593359Simp
311693359Simp	switch (wreq->wi_type) {
311793359Simp	case WI_DEBUG_SLEEP:
311893359Simp		wreq->wi_len++;
311993359Simp		wreq->wi_val[0] = sc->wi_debug.wi_sleep;
312093359Simp		break;
312193359Simp	case WI_DEBUG_DELAYSUPP:
312293359Simp		wreq->wi_len++;
312393359Simp		wreq->wi_val[0] = sc->wi_debug.wi_delaysupp;
312493359Simp		break;
312593359Simp	case WI_DEBUG_TXSUPP:
312693359Simp		wreq->wi_len++;
312793359Simp		wreq->wi_val[0] = sc->wi_debug.wi_txsupp;
312893359Simp		break;
312993359Simp	case WI_DEBUG_MONITOR:
313093359Simp		wreq->wi_len++;
313193359Simp		wreq->wi_val[0] = sc->wi_debug.wi_monitor;
313293359Simp		break;
313393359Simp	case WI_DEBUG_LEDTEST:
313493359Simp		wreq->wi_len += 3;
313593359Simp		wreq->wi_val[0] = sc->wi_debug.wi_ledtest;
313693359Simp		wreq->wi_val[1] = sc->wi_debug.wi_ledtest_param0;
313793359Simp		wreq->wi_val[2] = sc->wi_debug.wi_ledtest_param1;
313893359Simp		break;
313993359Simp	case WI_DEBUG_CONTTX:
314093359Simp		wreq->wi_len += 2;
314193359Simp		wreq->wi_val[0] = sc->wi_debug.wi_conttx;
314293359Simp		wreq->wi_val[1] = sc->wi_debug.wi_conttx_param0;
314393359Simp		break;
314493359Simp	case WI_DEBUG_CONTRX:
314593359Simp		wreq->wi_len++;
314693359Simp		wreq->wi_val[0] = sc->wi_debug.wi_contrx;
314793359Simp		break;
314893359Simp	case WI_DEBUG_SIGSTATE:
314993359Simp		wreq->wi_len += 2;
315093359Simp		wreq->wi_val[0] = sc->wi_debug.wi_sigstate;
315193359Simp		wreq->wi_val[1] = sc->wi_debug.wi_sigstate_param0;
315293359Simp		break;
315393359Simp	case WI_DEBUG_CONFBITS:
315493359Simp		wreq->wi_len += 2;
315593359Simp		wreq->wi_val[0] = sc->wi_debug.wi_confbits;
315693359Simp		wreq->wi_val[1] = sc->wi_debug.wi_confbits_param0;
315793359Simp		break;
315893359Simp	default:
315993359Simp		error = EIO;
316093359Simp		break;
316193359Simp	}
316293359Simp
316393359Simp	return (error);
316493359Simp}
316593359Simp
316693359Simpstatic int
3167109323Ssamwi_set_debug(struct wi_softc *sc, struct wi_req *wreq)
316893359Simp{
3169109323Ssam	int error = 0;
317093359Simp	u_int16_t		cmd, param0 = 0, param1 = 0;
317193359Simp
317293359Simp	switch (wreq->wi_type) {
317393359Simp	case WI_DEBUG_RESET:
317493359Simp	case WI_DEBUG_INIT:
317593359Simp	case WI_DEBUG_CALENABLE:
317693359Simp		break;
317793359Simp	case WI_DEBUG_SLEEP:
317893359Simp		sc->wi_debug.wi_sleep = 1;
317993359Simp		break;
318093359Simp	case WI_DEBUG_WAKE:
318193359Simp		sc->wi_debug.wi_sleep = 0;
318293359Simp		break;
318393359Simp	case WI_DEBUG_CHAN:
318493359Simp		param0 = wreq->wi_val[0];
318593359Simp		break;
318693359Simp	case WI_DEBUG_DELAYSUPP:
318793359Simp		sc->wi_debug.wi_delaysupp = 1;
318893359Simp		break;
318993359Simp	case WI_DEBUG_TXSUPP:
319093359Simp		sc->wi_debug.wi_txsupp = 1;
319193359Simp		break;
319293359Simp	case WI_DEBUG_MONITOR:
319393359Simp		sc->wi_debug.wi_monitor = 1;
319493359Simp		break;
319593359Simp	case WI_DEBUG_LEDTEST:
319693359Simp		param0 = wreq->wi_val[0];
319793359Simp		param1 = wreq->wi_val[1];
319893359Simp		sc->wi_debug.wi_ledtest = 1;
319993359Simp		sc->wi_debug.wi_ledtest_param0 = param0;
320093359Simp		sc->wi_debug.wi_ledtest_param1 = param1;
320193359Simp		break;
320293359Simp	case WI_DEBUG_CONTTX:
320393359Simp		param0 = wreq->wi_val[0];
320493359Simp		sc->wi_debug.wi_conttx = 1;
320593359Simp		sc->wi_debug.wi_conttx_param0 = param0;
320693359Simp		break;
320793359Simp	case WI_DEBUG_STOPTEST:
320893359Simp		sc->wi_debug.wi_delaysupp = 0;
320993359Simp		sc->wi_debug.wi_txsupp = 0;
321093359Simp		sc->wi_debug.wi_monitor = 0;
321193359Simp		sc->wi_debug.wi_ledtest = 0;
321293359Simp		sc->wi_debug.wi_ledtest_param0 = 0;
321393359Simp		sc->wi_debug.wi_ledtest_param1 = 0;
321493359Simp		sc->wi_debug.wi_conttx = 0;
321593359Simp		sc->wi_debug.wi_conttx_param0 = 0;
321693359Simp		sc->wi_debug.wi_contrx = 0;
321793359Simp		sc->wi_debug.wi_sigstate = 0;
321893359Simp		sc->wi_debug.wi_sigstate_param0 = 0;
321993359Simp		break;
322093359Simp	case WI_DEBUG_CONTRX:
322193359Simp		sc->wi_debug.wi_contrx = 1;
322293359Simp		break;
322393359Simp	case WI_DEBUG_SIGSTATE:
322493359Simp		param0 = wreq->wi_val[0];
322593359Simp		sc->wi_debug.wi_sigstate = 1;
322693359Simp		sc->wi_debug.wi_sigstate_param0 = param0;
322793359Simp		break;
322893359Simp	case WI_DEBUG_CONFBITS:
322993359Simp		param0 = wreq->wi_val[0];
323093359Simp		param1 = wreq->wi_val[1];
323193359Simp		sc->wi_debug.wi_confbits = param0;
323293359Simp		sc->wi_debug.wi_confbits_param0 = param1;
323393359Simp		break;
323493359Simp	default:
323593359Simp		error = EIO;
323693359Simp		break;
323793359Simp	}
323893359Simp
323993359Simp	if (error)
324093359Simp		return (error);
324193359Simp
324293359Simp	cmd = WI_CMD_DEBUG | (wreq->wi_type << 8);
324393359Simp	error = wi_cmd(sc, cmd, param0, param1, 0);
324493359Simp
324593359Simp	return (error);
324693359Simp}
3247101903Simp
3248105076Simp#if __FreeBSD_version >= 500000
3249101903Simp/*
3250101903Simp * Special routines to download firmware for Symbol CF card.
3251101903Simp * XXX: This should be modified generic into any PRISM-2 based card.
3252101903Simp */
3253101903Simp
3254101903Simp#define	WI_SBCF_PDIADDR		0x3100
3255101903Simp
3256101903Simp/* unaligned load little endian */
3257101903Simp#define	GETLE32(p)	((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24))
3258101903Simp#define	GETLE16(p)	((p)[0] | ((p)[1]<<8))
3259101903Simp
3260101903Simpint
3261101903Simpwi_symbol_load_firm(struct wi_softc *sc, const void *primsym, int primlen,
3262101903Simp    const void *secsym, int seclen)
3263101903Simp{
3264101903Simp	uint8_t ebuf[256];
3265101903Simp	int i;
3266101903Simp
3267101903Simp	/* load primary code and run it */
3268101903Simp	wi_symbol_set_hcr(sc, WI_HCR_EEHOLD);
3269101903Simp	if (wi_symbol_write_firm(sc, primsym, primlen, NULL, 0))
3270101903Simp		return EIO;
3271101903Simp	wi_symbol_set_hcr(sc, WI_HCR_RUN);
3272101903Simp	for (i = 0; ; i++) {
3273101903Simp		if (i == 10)
3274101903Simp			return ETIMEDOUT;
3275101903Simp		tsleep(sc, PWAIT, "wiinit", 1);
3276101903Simp		if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT)
3277101903Simp			break;
3278101903Simp		/* write the magic key value to unlock aux port */
3279101903Simp		CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0);
3280101903Simp		CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1);
3281101903Simp		CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2);
3282101903Simp		CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL);
3283101903Simp	}
3284101903Simp
3285101903Simp	/* issue read EEPROM command: XXX copied from wi_cmd() */
3286101903Simp	CSR_WRITE_2(sc, WI_PARAM0, 0);
3287101903Simp	CSR_WRITE_2(sc, WI_PARAM1, 0);
3288101903Simp	CSR_WRITE_2(sc, WI_PARAM2, 0);
3289101903Simp	CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE);
3290101903Simp        for (i = 0; i < WI_TIMEOUT; i++) {
3291101903Simp                if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
3292101903Simp                        break;
3293101903Simp                DELAY(1);
3294101903Simp        }
3295101903Simp        CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
3296101903Simp
3297101903Simp	CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ);
3298101903Simp	CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ);
3299101903Simp	CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA,
3300101903Simp	    (uint16_t *)ebuf, sizeof(ebuf) / 2);
3301101903Simp	if (GETLE16(ebuf) > sizeof(ebuf))
3302101903Simp		return EIO;
3303101903Simp	if (wi_symbol_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf)))
3304101903Simp		return EIO;
3305101903Simp	return 0;
3306101903Simp}
3307101903Simp
3308101903Simpstatic int
3309101903Simpwi_symbol_write_firm(struct wi_softc *sc, const void *buf, int buflen,
3310101903Simp    const void *ebuf, int ebuflen)
3311101903Simp{
3312101903Simp	const uint8_t *p, *ep, *q, *eq;
3313101903Simp	char *tp;
3314101903Simp	uint32_t addr, id, eid;
3315101903Simp	int i, len, elen, nblk, pdrlen;
3316101903Simp
3317101903Simp	/*
3318101903Simp	 * Parse the header of the firmware image.
3319101903Simp	 */
3320101903Simp	p = buf;
3321101903Simp	ep = p + buflen;
3322101903Simp	while (p < ep && *p++ != ' ');	/* FILE: */
3323101903Simp	while (p < ep && *p++ != ' ');	/* filename */
3324101903Simp	while (p < ep && *p++ != ' ');	/* type of the firmware */
3325101903Simp	nblk = strtoul(p, &tp, 10);
3326101903Simp	p = tp;
3327101903Simp	pdrlen = strtoul(p + 1, &tp, 10);
3328101903Simp	p = tp;
3329101903Simp	while (p < ep && *p++ != 0x1a);	/* skip rest of header */
3330101903Simp
3331101903Simp	/*
3332101903Simp	 * Block records: address[4], length[2], data[length];
3333101903Simp	 */
3334101903Simp	for (i = 0; i < nblk; i++) {
3335101903Simp		addr = GETLE32(p);	p += 4;
3336101903Simp		len  = GETLE16(p);	p += 2;
3337101903Simp		CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3338101903Simp		CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3339101903Simp		CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3340101903Simp		    (const uint16_t *)p, len / 2);
3341101903Simp		p += len;
3342101903Simp	}
3343101903Simp
3344101903Simp	/*
3345101903Simp	 * PDR: id[4], address[4], length[4];
3346101903Simp	 */
3347101903Simp	for (i = 0; i < pdrlen; ) {
3348101903Simp		id   = GETLE32(p);	p += 4; i += 4;
3349101903Simp		addr = GETLE32(p);	p += 4; i += 4;
3350101903Simp		len  = GETLE32(p);	p += 4; i += 4;
3351101903Simp		/* replace PDR entry with the values from EEPROM, if any */
3352101903Simp		for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) {
3353101903Simp			elen = GETLE16(q);	q += 2;
3354101903Simp			eid  = GETLE16(q);	q += 2;
3355101903Simp			elen--;		/* elen includes eid */
3356101903Simp			if (eid == 0)
3357101903Simp				break;
3358101903Simp			if (eid != id)
3359101903Simp				continue;
3360101903Simp			CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3361101903Simp			CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3362101903Simp			CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3363101903Simp			    (const uint16_t *)q, len / 2);
3364101903Simp			break;
3365101903Simp		}
3366101903Simp	}
3367101903Simp	return 0;
3368101903Simp}
3369101903Simp
3370101903Simpstatic int
3371101903Simpwi_symbol_set_hcr(struct wi_softc *sc, int mode)
3372101903Simp{
3373101903Simp	uint16_t hcr;
3374101903Simp
3375101903Simp	CSR_WRITE_2(sc, WI_COR, WI_COR_RESET);
3376101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3377101903Simp	hcr = CSR_READ_2(sc, WI_HCR);
3378101903Simp	hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE);
3379101903Simp	CSR_WRITE_2(sc, WI_HCR, hcr);
3380101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3381101903Simp	CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE);
3382101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3383101903Simp	return 0;
3384101903Simp}
3385105076Simp#endif
3386