if_wi.c revision 144167
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 144167 2005-03-26 23:43:54Z sam $");
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>
7983366Sjulian#include <sys/proc.h>
8046492Swpaul#include <sys/kernel.h>
8146492Swpaul#include <sys/socket.h>
8253702Swpaul#include <sys/module.h>
8353702Swpaul#include <sys/bus.h>
8494486Simp#include <sys/random.h>
8553702Swpaul#include <sys/syslog.h>
8653702Swpaul#include <sys/sysctl.h>
8746492Swpaul
8853702Swpaul#include <machine/bus.h>
8953702Swpaul#include <machine/resource.h>
9095534Simp#include <machine/clock.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 *);
118109323Ssamstatic int  wi_reset(struct wi_softc *);
11991693Simpstatic void wi_watchdog(struct ifnet *);
120109323Ssamstatic int  wi_ioctl(struct ifnet *, u_long, caddr_t);
121109323Ssamstatic int  wi_media_change(struct ifnet *);
122109323Ssamstatic void wi_media_status(struct ifnet *, struct ifmediareq *);
12346492Swpaul
124109323Ssamstatic void wi_rx_intr(struct wi_softc *);
125109323Ssamstatic void wi_tx_intr(struct wi_softc *);
126109323Ssamstatic void wi_tx_ex_intr(struct wi_softc *);
127109323Ssamstatic void wi_info_intr(struct wi_softc *);
12846492Swpaul
129109323Ssamstatic int  wi_get_cfg(struct ifnet *, u_long, caddr_t);
130109323Ssamstatic int  wi_set_cfg(struct ifnet *, u_long, caddr_t);
131109323Ssamstatic int  wi_write_txrate(struct wi_softc *);
132109323Ssamstatic int  wi_write_wep(struct wi_softc *);
133109323Ssamstatic int  wi_write_multi(struct wi_softc *);
134109323Ssamstatic int  wi_alloc_fid(struct wi_softc *, int, int *);
135109323Ssamstatic void wi_read_nicid(struct wi_softc *);
136109323Ssamstatic int  wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
13753702Swpaul
138109323Ssamstatic int  wi_cmd(struct wi_softc *, int, int, int, int);
139109323Ssamstatic int  wi_seek_bap(struct wi_softc *, int, int);
140109323Ssamstatic int  wi_read_bap(struct wi_softc *, int, int, void *, int);
141109323Ssamstatic int  wi_write_bap(struct wi_softc *, int, int, void *, int);
142109323Ssamstatic int  wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
143109323Ssamstatic int  wi_read_rid(struct wi_softc *, int, void *, int *);
144109323Ssamstatic int  wi_write_rid(struct wi_softc *, int, void *, int);
14577217Sphk
146117812Ssamstatic int  wi_newstate(struct ieee80211com *, enum ieee80211_state, int);
147109323Ssam
148116898Ssamstatic int  wi_scan_ap(struct wi_softc *, u_int16_t, u_int16_t);
149109323Ssamstatic void wi_scan_result(struct wi_softc *, int, int);
150109323Ssam
151109323Ssamstatic void wi_dump_pkt(struct wi_frame *, struct ieee80211_node *, int rssi);
152109323Ssam
15393359Simpstatic int wi_get_debug(struct wi_softc *, struct wi_req *);
15493359Simpstatic int wi_set_debug(struct wi_softc *, struct wi_req *);
15593359Simp
156105076Simp#if __FreeBSD_version >= 500000
157101903Simp/* support to download firmware for symbol CF card */
158101903Simpstatic int wi_symbol_write_firm(struct wi_softc *, const void *, int,
159101903Simp		const void *, int);
160101903Simpstatic int wi_symbol_set_hcr(struct wi_softc *, int);
161105076Simp#endif
162101903Simp
163109323Ssamstatic __inline int
164109323Ssamwi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
165109323Ssam{
16653702Swpaul
167109323Ssam	val = htole16(val);
168109323Ssam	return wi_write_rid(sc, rid, &val, sizeof(val));
169109323Ssam}
170109323Ssam
171109592SsamSYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters");
172109592Ssam
173109323Ssamstatic	struct timeval lasttxerror;	/* time of last tx error msg */
174109323Ssamstatic	int curtxeps;			/* current tx error msgs/sec */
175111559Ssamstatic	int wi_txerate = 0;		/* tx error rate: max msgs/sec */
176109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate,
177111559Ssam	    0, "max tx error msgs/sec; 0 to disable msgs");
178109323Ssam
179109323Ssam#define	WI_DEBUG
180109323Ssam#ifdef WI_DEBUG
181109323Ssamstatic	int wi_debug = 0;
182109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug,
183109592Ssam	    0, "control debugging printfs");
184109323Ssam
185109323Ssam#define	DPRINTF(X)	if (wi_debug) printf X
186109323Ssam#define	DPRINTF2(X)	if (wi_debug > 1) printf X
187109323Ssam#define	IFF_DUMPPKTS(_ifp) \
188109323Ssam	(((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
189109323Ssam#else
190109323Ssam#define	DPRINTF(X)
191109323Ssam#define	DPRINTF2(X)
192109323Ssam#define	IFF_DUMPPKTS(_ifp)	0
193109323Ssam#endif
194109323Ssam
195109323Ssam#define WI_INTRS	(WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
196109323Ssam
19793825Simpstruct wi_card_ident wi_card_ident[] = {
19893825Simp	/* CARD_ID			CARD_NAME		FIRM_TYPE */
19993825Simp	{ WI_NIC_LUCENT_ID,		WI_NIC_LUCENT_STR,	WI_LUCENT },
20093825Simp	{ WI_NIC_SONY_ID,		WI_NIC_SONY_STR,	WI_LUCENT },
20193825Simp	{ WI_NIC_LUCENT_EMB_ID,		WI_NIC_LUCENT_EMB_STR,	WI_LUCENT },
20293825Simp	{ WI_NIC_EVB2_ID,		WI_NIC_EVB2_STR,	WI_INTERSIL },
20393825Simp	{ WI_NIC_HWB3763_ID,		WI_NIC_HWB3763_STR,	WI_INTERSIL },
20493825Simp	{ WI_NIC_HWB3163_ID,		WI_NIC_HWB3163_STR,	WI_INTERSIL },
20593825Simp	{ WI_NIC_HWB3163B_ID,		WI_NIC_HWB3163B_STR,	WI_INTERSIL },
20693825Simp	{ WI_NIC_EVB3_ID,		WI_NIC_EVB3_STR,	WI_INTERSIL },
20793825Simp	{ WI_NIC_HWB1153_ID,		WI_NIC_HWB1153_STR,	WI_INTERSIL },
20893825Simp	{ WI_NIC_P2_SST_ID,		WI_NIC_P2_SST_STR,	WI_INTERSIL },
20993825Simp	{ WI_NIC_EVB2_SST_ID,		WI_NIC_EVB2_SST_STR,	WI_INTERSIL },
21093825Simp	{ WI_NIC_3842_EVA_ID,		WI_NIC_3842_EVA_STR,	WI_INTERSIL },
21193825Simp	{ WI_NIC_3842_PCMCIA_AMD_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
21293825Simp	{ WI_NIC_3842_PCMCIA_SST_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
213101355Simp	{ WI_NIC_3842_PCMCIA_ATL_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
214101355Simp	{ WI_NIC_3842_PCMCIA_ATS_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
21593825Simp	{ WI_NIC_3842_MINI_AMD_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
21693825Simp	{ WI_NIC_3842_MINI_SST_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
217101355Simp	{ WI_NIC_3842_MINI_ATL_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
218101355Simp	{ WI_NIC_3842_MINI_ATS_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
21993825Simp	{ WI_NIC_3842_PCI_AMD_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
22093825Simp	{ WI_NIC_3842_PCI_SST_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
221101355Simp	{ WI_NIC_3842_PCI_ATS_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
222101355Simp	{ WI_NIC_3842_PCI_ATL_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
22393825Simp	{ WI_NIC_P3_PCMCIA_AMD_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
22493825Simp	{ WI_NIC_P3_PCMCIA_SST_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
225101355Simp	{ WI_NIC_P3_PCMCIA_ATL_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
226101355Simp	{ WI_NIC_P3_PCMCIA_ATS_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
22793825Simp	{ WI_NIC_P3_MINI_AMD_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
22893825Simp	{ WI_NIC_P3_MINI_SST_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
229101355Simp	{ WI_NIC_P3_MINI_ATL_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
230101355Simp	{ WI_NIC_P3_MINI_ATS_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
23193825Simp	{ 0,	NULL,	0 },
23293825Simp};
23393825Simp
234109323Ssamdevclass_t wi_devclass;
23546492Swpaul
23693611Simpint
237109323Ssamwi_attach(device_t dev)
23874906Salfred{
239109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
240109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
241138571Ssam	struct ifnet *ifp = &sc->sc_if;
242116951Ssam	int i, nrates, buflen;
243109323Ssam	u_int16_t val;
244109323Ssam	u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
245116951Ssam	struct ieee80211_rateset *rs;
246109323Ssam	static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
247109323Ssam		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
248109323Ssam	};
249109323Ssam	int error;
25074906Salfred
251109323Ssam	/*
252109323Ssam	 * NB: no locking is needed here; don't put it here
253109323Ssam	 *     unless you can prove it!
254109323Ssam	 */
255121697Ssam	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
25674998Swpaul	    wi_intr, sc, &sc->wi_intrhand);
25753702Swpaul
25853702Swpaul	if (error) {
25953702Swpaul		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
26053702Swpaul		wi_free(dev);
26153702Swpaul		return (error);
26253702Swpaul	}
26353702Swpaul
26495534Simp#if __FreeBSD_version >= 500000
265109323Ssam	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
26693818Sjhb	    MTX_DEF | MTX_RECURSE);
26795534Simp#endif
26867092Swpaul
269138571Ssam	sc->sc_firmware_type = WI_NOTYPE;
270123339Simp	sc->wi_cmd_count = 500;
27146492Swpaul	/* Reset the NIC. */
272112096Simp	if (wi_reset(sc) != 0)
273109323Ssam		return ENXIO;		/* XXX */
27446492Swpaul
27576438Swpaul	/*
27676438Swpaul	 * Read the station address.
27776438Swpaul	 * And do it twice. I've seen PRISM-based cards that return
27876438Swpaul	 * an error when trying to read it the first time, which causes
27976438Swpaul	 * the probe to fail.
28076438Swpaul	 */
281109323Ssam	buflen = IEEE80211_ADDR_LEN;
282109323Ssam	error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
283109323Ssam	if (error != 0) {
284109323Ssam		buflen = IEEE80211_ADDR_LEN;
285109323Ssam		error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
286109323Ssam	}
287109323Ssam	if (error || IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) {
288109323Ssam		if (error != 0)
289109323Ssam			device_printf(dev, "mac read failed %d\n", error);
290109323Ssam		else
291109323Ssam			device_printf(dev, "mac read failed (all zeros)\n");
29275149Simp		wi_free(dev);
29375149Simp		return (error);
29475149Simp	}
29546492Swpaul
296109323Ssam	/* Read NIC identification */
297109323Ssam	wi_read_nicid(sc);
29846492Swpaul
29946492Swpaul	ifp->if_softc = sc;
300121816Sbrooks	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
30146492Swpaul	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
30246492Swpaul	ifp->if_ioctl = wi_ioctl;
30346492Swpaul	ifp->if_start = wi_start;
30446492Swpaul	ifp->if_watchdog = wi_watchdog;
30546492Swpaul	ifp->if_init = wi_init;
306132986Smlaier	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
307132986Smlaier	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
308132986Smlaier	IFQ_SET_READY(&ifp->if_snd);
30946492Swpaul
310138571Ssam	ic->ic_ifp = ifp;
311109323Ssam	ic->ic_phytype = IEEE80211_T_DS;
312109323Ssam	ic->ic_opmode = IEEE80211_M_STA;
313109323Ssam	ic->ic_state = IEEE80211_S_INIT;
314138571Ssam	ic->ic_caps = IEEE80211_C_PMGT
315138571Ssam		    | IEEE80211_C_WEP		/* everyone supports WEP */
316138571Ssam		    ;
317138571Ssam	ic->ic_max_aid = WI_MAX_AID;
31846492Swpaul
319116951Ssam	/*
320116951Ssam	 * Query the card for available channels and setup the
321116951Ssam	 * channel table.  We assume these are all 11b channels.
322116951Ssam	 */
323109323Ssam	buflen = sizeof(val);
324109323Ssam	if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
325109323Ssam		val = htole16(0x1fff);	/* assume 1-11 */
326116951Ssam	KASSERT(val != 0, ("wi_attach: no available channels listed!"));
327116951Ssam
328116951Ssam	val <<= 1;			/* shift for base 1 indices */
329116951Ssam	for (i = 1; i < 16; i++) {
330116951Ssam		if (isset((u_int8_t*)&val, i)) {
331116951Ssam			ic->ic_channels[i].ic_freq =
332116951Ssam				ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
333116951Ssam			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
334116951Ssam		}
335109323Ssam	}
33646492Swpaul
33746563Swpaul	/*
33846563Swpaul	 * Read the default channel from the NIC. This may vary
33946563Swpaul	 * depending on the country where the NIC was purchased, so
34046563Swpaul	 * we can't hard-code a default and expect it to work for
34146563Swpaul	 * everyone.
342116951Ssam	 *
343116951Ssam	 * If no channel is specified, let the 802.11 code select.
34446563Swpaul	 */
345109323Ssam	buflen = sizeof(val);
346116951Ssam	if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0) {
347116951Ssam		val = le16toh(val);
348116951Ssam		KASSERT(val < IEEE80211_CHAN_MAX &&
349116951Ssam			ic->ic_channels[val].ic_flags != 0,
350116951Ssam			("wi_attach: invalid own channel %u!", val));
351116951Ssam		ic->ic_ibss_chan = &ic->ic_channels[val];
352117042Ssam	} else {
353117042Ssam		device_printf(dev,
354117042Ssam			"WI_RID_OWN_CHNL failed, using first channel!\n");
355117042Ssam		ic->ic_ibss_chan = &ic->ic_channels[0];
356109323Ssam	}
35746563Swpaul
35856965Swpaul	/*
35998440Simp	 * Set flags based on firmware version.
36098440Simp	 */
36198440Simp	switch (sc->sc_firmware_type) {
36298440Simp	case WI_LUCENT:
363112363Simp		sc->sc_ntxbuf = 1;
364109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
365109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
366109323Ssam		/* XXX: not confirmed, but never seen for recent firmware */
367109323Ssam		if (sc->sc_sta_firmware_ver <  40000) {
368109323Ssam			sc->sc_flags |= WI_FLAGS_BUG_AUTOINC;
369109323Ssam		}
370109323Ssam#endif
37198440Simp		if (sc->sc_sta_firmware_ver >= 60000)
372109323Ssam			sc->sc_flags |= WI_FLAGS_HAS_MOR;
373117855Ssam		if (sc->sc_sta_firmware_ver >= 60006) {
374116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
375117855Ssam			ic->ic_caps |= IEEE80211_C_MONITOR;
376117855Ssam		}
377109323Ssam		sc->sc_ibss_port = htole16(1);
378119784Ssam
379119784Ssam		sc->sc_min_rssi = WI_LUCENT_MIN_RSSI;
380119784Ssam		sc->sc_max_rssi = WI_LUCENT_MAX_RSSI;
381119784Ssam		sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
38298440Simp		break;
383109323Ssam
38498440Simp	case WI_INTERSIL:
385112363Simp		sc->sc_ntxbuf = WI_NTXBUF;
386109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR;
387109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_ROAMING;
388109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
389123339Simp		/*
390123339Simp		 * Old firmware are slow, so give peace a chance.
391123339Simp		 */
392123339Simp		if (sc->sc_sta_firmware_ver < 10000)
393123339Simp			sc->wi_cmd_count = 5000;
394109323Ssam		if (sc->sc_sta_firmware_ver > 10101)
395109323Ssam			sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
396117855Ssam		if (sc->sc_sta_firmware_ver >= 800) {
397116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
398117855Ssam			ic->ic_caps |= IEEE80211_C_MONITOR;
399117855Ssam		}
400109396Simp		/*
401109396Simp		 * version 0.8.3 and newer are the only ones that are known
402109396Simp		 * to currently work.  Earlier versions can be made to work,
403109396Simp		 * at least according to the Linux driver.
404109396Simp		 */
405100734Simp		if (sc->sc_sta_firmware_ver >= 803)
406116951Ssam			ic->ic_caps |= IEEE80211_C_HOSTAP;
407109323Ssam		sc->sc_ibss_port = htole16(0);
408119784Ssam
409119784Ssam		sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
410119784Ssam		sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
411119784Ssam		sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
41298440Simp		break;
413109323Ssam
41498440Simp	case WI_SYMBOL:
415112363Simp		sc->sc_ntxbuf = 1;
416109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY;
41798440Simp		if (sc->sc_sta_firmware_ver >= 25000)
418116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
419109323Ssam		sc->sc_ibss_port = htole16(4);
420119784Ssam
421119784Ssam		sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
422119784Ssam		sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
423119784Ssam		sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
42498440Simp		break;
42598440Simp	}
42698440Simp
42798440Simp	/*
42856965Swpaul	 * Find out if we support WEP on this card.
42956965Swpaul	 */
430109323Ssam	buflen = sizeof(val);
431109323Ssam	if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
432109323Ssam	    val != htole16(0))
433116951Ssam		ic->ic_caps |= IEEE80211_C_WEP;
43456965Swpaul
435109323Ssam	/* Find supported rates. */
436109323Ssam	buflen = sizeof(ratebuf);
437116951Ssam	rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
438109323Ssam	if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
439116951Ssam		nrates = le16toh(*(u_int16_t *)ratebuf);
440116951Ssam		if (nrates > IEEE80211_RATE_MAXSIZE)
441116951Ssam			nrates = IEEE80211_RATE_MAXSIZE;
442116951Ssam		rs->rs_nrates = 0;
443116951Ssam		for (i = 0; i < nrates; i++)
444116951Ssam			if (ratebuf[2+i])
445116951Ssam				rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i];
446109323Ssam	} else {
447109323Ssam		/* XXX fallback on error? */
448116951Ssam		rs->rs_nrates = 0;
44998440Simp	}
45077217Sphk
451109323Ssam	buflen = sizeof(val);
452109323Ssam	if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
453109323Ssam	    wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
454119784Ssam		sc->sc_dbm_offset = le16toh(val);
455119784Ssam	}
45646492Swpaul
457109323Ssam	sc->sc_max_datalen = 2304;
458109323Ssam	sc->sc_system_scale = 1;
459109323Ssam	sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN;
460109323Ssam	sc->sc_roaming_mode = 1;
46146492Swpaul
462109323Ssam	sc->sc_portnum = WI_DEFAULT_PORT;
463109323Ssam	sc->sc_authtype = WI_DEFAULT_AUTHTYPE;
46487383Simp
465109323Ssam	bzero(sc->sc_nodename, sizeof(sc->sc_nodename));
466109323Ssam	sc->sc_nodelen = sizeof(WI_DEFAULT_NODENAME) - 1;
467109323Ssam	bcopy(WI_DEFAULT_NODENAME, sc->sc_nodename, sc->sc_nodelen);
46887383Simp
469109323Ssam	bzero(sc->sc_net_name, sizeof(sc->sc_net_name));
470109323Ssam	bcopy(WI_DEFAULT_NETNAME, sc->sc_net_name,
471109323Ssam	    sizeof(WI_DEFAULT_NETNAME) - 1);
47293733Simp
47393733Simp	/*
474109323Ssam	 * Call MI attach routine.
47593733Simp	 */
476138571Ssam	ieee80211_ifattach(ic);
477117812Ssam	/* override state transition method */
478117812Ssam	sc->sc_newstate = ic->ic_newstate;
479117812Ssam	ic->ic_newstate = wi_newstate;
480138571Ssam	ieee80211_media_init(ic, wi_media_change, wi_media_status);
481109323Ssam
482119784Ssam#if NBPFILTER > 0
483119784Ssam	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
484119784Ssam		sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
485119784Ssam		&sc->sc_drvbpf);
486119784Ssam	/*
487119784Ssam	 * Initialize constant fields.
488127698Ssam	 * XXX make header lengths a multiple of 32-bits so subsequent
489127698Ssam	 *     headers are properly aligned; this is a kludge to keep
490127698Ssam	 *     certain applications happy.
491119784Ssam	 *
492119784Ssam	 * NB: the channel is setup each time we transition to the
493119784Ssam	 *     RUN state to avoid filling it in for each frame.
494119784Ssam	 */
495127698Ssam	sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t));
496127698Ssam	sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len);
497127698Ssam	sc->sc_tx_th.wt_ihdr.it_present = htole32(WI_TX_RADIOTAP_PRESENT);
498119784Ssam
499127698Ssam	sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t));
500127698Ssam	sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len);
501127698Ssam	sc->sc_rx_th.wr_ihdr.it_present = htole32(WI_RX_RADIOTAP_PRESENT);
502119784Ssam#endif
503138571Ssam
504138571Ssam	if (bootverbose)
505138571Ssam		ieee80211_announce(ic);
506138571Ssam
507109323Ssam	return (0);
50887383Simp}
50987383Simp
510109323Ssamint
511109323Ssamwi_detach(device_t dev)
51246492Swpaul{
513109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
514138571Ssam	struct ifnet *ifp = &sc->sc_if;
515109323Ssam	WI_LOCK_DECL();
51646492Swpaul
517109323Ssam	WI_LOCK(sc);
51846492Swpaul
519109323Ssam	/* check if device was removed */
520123098Simp	sc->wi_gone |= !bus_child_present(dev);
52146492Swpaul
522109323Ssam	wi_stop(ifp, 0);
52346492Swpaul
524119784Ssam#if NBPFILTER > 0
525119784Ssam	bpfdetach(ifp);
526119784Ssam#endif
527138571Ssam	ieee80211_ifdetach(&sc->sc_ic);
528114190Simp	WI_UNLOCK(sc);
529109323Ssam	bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
530109323Ssam	wi_free(dev);
531109323Ssam#if __FreeBSD_version >= 500000
532109323Ssam	mtx_destroy(&sc->sc_mtx);
53393359Simp#endif
534109323Ssam	return (0);
535109323Ssam}
53693359Simp
537109323Ssam#ifdef __NetBSD__
538109323Ssamint
539109323Ssamwi_activate(struct device *self, enum devact act)
540109323Ssam{
541109323Ssam	struct wi_softc *sc = (struct wi_softc *)self;
542109323Ssam	int rv = 0, s;
54393359Simp
544109323Ssam	s = splnet();
545109323Ssam	switch (act) {
546109323Ssam	case DVACT_ACTIVATE:
547109323Ssam		rv = EOPNOTSUPP;
548109323Ssam		break;
54993359Simp
550109323Ssam	case DVACT_DEACTIVATE:
551138571Ssam		if_deactivate(&sc->sc_if);
552109323Ssam		break;
553109323Ssam	}
554109323Ssam	splx(s);
555109323Ssam	return rv;
556109323Ssam}
55793359Simp
558109323Ssamvoid
559109323Ssamwi_power(struct wi_softc *sc, int why)
560109323Ssam{
561138571Ssam	struct ifnet *ifp = &sc->sc_if;
562109323Ssam	int s;
56393359Simp
564109323Ssam	s = splnet();
565109323Ssam	switch (why) {
566109323Ssam	case PWR_SUSPEND:
567109323Ssam	case PWR_STANDBY:
568109323Ssam		wi_stop(ifp, 1);
569109323Ssam		break;
570109323Ssam	case PWR_RESUME:
571109323Ssam		if (ifp->if_flags & IFF_UP) {
572109323Ssam			wi_init(ifp);
573109323Ssam			(void)wi_intr(sc);
57494405Simp		}
575109323Ssam		break;
576109323Ssam	case PWR_SOFTSUSPEND:
577109323Ssam	case PWR_SOFTSTANDBY:
578109323Ssam	case PWR_SOFTRESUME:
579109323Ssam		break;
58093359Simp	}
581109323Ssam	splx(s);
58246492Swpaul}
583109323Ssam#endif /* __NetBSD__ */
58446492Swpaul
585109323Ssamvoid
586109323Ssamwi_shutdown(device_t dev)
58746492Swpaul{
588109323Ssam	struct wi_softc *sc = device_get_softc(dev);
58946492Swpaul
590109323Ssam	wi_stop(&sc->sc_if, 1);
59146492Swpaul}
59246492Swpaul
593109323Ssamvoid
594109323Ssamwi_intr(void *arg)
59546492Swpaul{
596109323Ssam	struct wi_softc *sc = arg;
597138571Ssam	struct ifnet *ifp = &sc->sc_if;
598112363Simp	u_int16_t status;
599109323Ssam	WI_LOCK_DECL();
60046492Swpaul
601109323Ssam	WI_LOCK(sc);
60246492Swpaul
603123098Simp	if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) {
604113327Simp		CSR_WRITE_2(sc, WI_INT_EN, 0);
605123098Simp		CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
606109323Ssam		WI_UNLOCK(sc);
60746492Swpaul		return;
608109323Ssam	}
60946492Swpaul
610112363Simp	/* Disable interrupts. */
611112363Simp	CSR_WRITE_2(sc, WI_INT_EN, 0);
61246492Swpaul
613112363Simp	status = CSR_READ_2(sc, WI_EVENT_STAT);
614112363Simp	if (status & WI_EV_RX)
615112363Simp		wi_rx_intr(sc);
616112363Simp	if (status & WI_EV_ALLOC)
617112363Simp		wi_tx_intr(sc);
618112363Simp	if (status & WI_EV_TX_EXC)
619112363Simp		wi_tx_ex_intr(sc);
620112363Simp	if (status & WI_EV_INFO)
621112363Simp		wi_info_intr(sc);
622112363Simp	if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
623112363Simp	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 &&
624132986Smlaier	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
625112363Simp		wi_start(ifp);
62646492Swpaul
627112363Simp	/* Re-enable interrupts. */
628112363Simp	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
62946492Swpaul
630109323Ssam	WI_UNLOCK(sc);
63146492Swpaul
63246492Swpaul	return;
63346492Swpaul}
63446492Swpaul
635109323Ssamvoid
636109323Ssamwi_init(void *arg)
63746492Swpaul{
638109323Ssam	struct wi_softc *sc = arg;
639109323Ssam	struct ifnet *ifp = &sc->sc_if;
640109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
641109323Ssam	struct wi_joinreq join;
642109323Ssam	int i;
643109323Ssam	int error = 0, wasenabled;
644109323Ssam	struct ifaddr *ifa;
645109323Ssam	struct sockaddr_dl *sdl;
646109323Ssam	WI_LOCK_DECL();
64746492Swpaul
648109323Ssam	WI_LOCK(sc);
64967092Swpaul
650109323Ssam	if (sc->wi_gone) {
651109323Ssam		WI_UNLOCK(sc);
65246492Swpaul		return;
65346492Swpaul	}
65446492Swpaul
655112362Simp	if ((wasenabled = sc->sc_enabled))
656123098Simp		wi_stop(ifp, 1);
657112362Simp	wi_reset(sc);
65846492Swpaul
659109323Ssam	/* common 802.11 configuration */
660109323Ssam	ic->ic_flags &= ~IEEE80211_F_IBSSON;
661109323Ssam	sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
662109323Ssam	switch (ic->ic_opmode) {
663109323Ssam	case IEEE80211_M_STA:
664109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS);
665109323Ssam		break;
666109323Ssam	case IEEE80211_M_IBSS:
667109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port);
668109323Ssam		ic->ic_flags |= IEEE80211_F_IBSSON;
669109323Ssam		break;
670109323Ssam	case IEEE80211_M_AHDEMO:
671109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
672109323Ssam		break;
673109323Ssam	case IEEE80211_M_HOSTAP:
674122087Sgreen		/*
675122087Sgreen		 * For PRISM cards, override the empty SSID, because in
676122087Sgreen		 * HostAP mode the controller will lock up otherwise.
677122087Sgreen		 */
678122087Sgreen		if (sc->sc_firmware_type == WI_INTERSIL &&
679122087Sgreen		    ic->ic_des_esslen == 0) {
680122087Sgreen			ic->ic_des_essid[0] = ' ';
681122087Sgreen			ic->ic_des_esslen = 1;
682122087Sgreen		}
683109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP);
684109323Ssam		break;
685117855Ssam	case IEEE80211_M_MONITOR:
686117855Ssam		if (sc->sc_firmware_type == WI_LUCENT)
687117855Ssam			wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
688117855Ssam		wi_cmd(sc, WI_CMD_DEBUG | (WI_TEST_MONITOR << 8), 0, 0, 0);
689117855Ssam		break;
69046492Swpaul	}
69146492Swpaul
692109323Ssam	/* Intersil interprets this RID as joining ESS even in IBSS mode */
693109323Ssam	if (sc->sc_firmware_type == WI_LUCENT &&
694109323Ssam	    (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0)
695109323Ssam		wi_write_val(sc, WI_RID_CREATE_IBSS, 1);
696109323Ssam	else
697109323Ssam		wi_write_val(sc, WI_RID_CREATE_IBSS, 0);
698109323Ssam	wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
699109323Ssam	wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid,
700109323Ssam	    ic->ic_des_esslen);
701116951Ssam	wi_write_val(sc, WI_RID_OWN_CHNL,
702116951Ssam		ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
703109323Ssam	wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen);
70446492Swpaul
705109323Ssam	ifa = ifaddr_byindex(ifp->if_index);
706109323Ssam	sdl = (struct sockaddr_dl *) ifa->ifa_addr;
707109323Ssam	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(sdl));
708109323Ssam	wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN);
70975373Salfred
710138571Ssam	if (ic->ic_caps & IEEE80211_C_PMGT)
711138571Ssam		wi_write_val(sc, WI_RID_PM_ENABLED,
712138571Ssam		    (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
71346492Swpaul
714109323Ssam	/* not yet common 802.11 configuration */
715109323Ssam	wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen);
716116951Ssam	wi_write_val(sc, WI_RID_RTS_THRESH, ic->ic_rtsthreshold);
717109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
718116951Ssam		wi_write_val(sc, WI_RID_FRAG_THRESH, ic->ic_fragthreshold);
71946492Swpaul
720109323Ssam	/* driver specific 802.11 configuration */
721109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)
722109323Ssam		wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale);
723109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
724109323Ssam		wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode);
725109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_MOR)
726109323Ssam		wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven);
727109323Ssam	wi_write_txrate(sc);
728109323Ssam	wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen);
72946492Swpaul
730109323Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
731109323Ssam	    sc->sc_firmware_type == WI_INTERSIL) {
732109323Ssam		wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval);
733109323Ssam		wi_write_val(sc, WI_RID_BASIC_RATE, 0x03);   /* 1, 2 */
734109323Ssam		wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */
735109323Ssam		wi_write_val(sc, WI_RID_DTIM_PERIOD, 1);
73646492Swpaul	}
73746492Swpaul
738109323Ssam	/*
739109323Ssam	 * Initialize promisc mode.
740109323Ssam	 *	Being in the Host-AP mode causes a great
741109323Ssam	 *	deal of pain if primisc mode is set.
742109323Ssam	 *	Therefore we avoid confusing the firmware
743109323Ssam	 *	and always reset promisc mode in Host-AP
744109323Ssam	 *	mode.  Host-AP sees all the packets anyway.
745109323Ssam	 */
746109323Ssam	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
747109323Ssam	    (ifp->if_flags & IFF_PROMISC) != 0) {
748109323Ssam		wi_write_val(sc, WI_RID_PROMISC, 1);
749109323Ssam	} else {
750109323Ssam		wi_write_val(sc, WI_RID_PROMISC, 0);
75175373Salfred	}
75246492Swpaul
753109323Ssam	/* Configure WEP. */
754116951Ssam	if (ic->ic_caps & IEEE80211_C_WEP)
755109323Ssam		wi_write_wep(sc);
75667092Swpaul
757109323Ssam	/* Set multicast filter. */
758109323Ssam	wi_write_multi(sc);
75946492Swpaul
760114124Simp	/* Allocate fids for the card */
761109323Ssam	if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
762109323Ssam		sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
763109323Ssam		if (sc->sc_firmware_type == WI_SYMBOL)
764109323Ssam			sc->sc_buflen = 1585;	/* XXX */
765112363Simp		for (i = 0; i < sc->sc_ntxbuf; i++) {
766109323Ssam			error = wi_alloc_fid(sc, sc->sc_buflen,
767109323Ssam			    &sc->sc_txd[i].d_fid);
768109323Ssam			if (error) {
769109323Ssam				device_printf(sc->sc_dev,
770109323Ssam				    "tx buffer allocation failed (error %u)\n",
771109323Ssam				    error);
772109323Ssam				goto out;
773109323Ssam			}
774109323Ssam			sc->sc_txd[i].d_len = 0;
77570073Swpaul		}
77670073Swpaul	}
777109323Ssam	sc->sc_txcur = sc->sc_txnext = 0;
77870073Swpaul
779109323Ssam	/* Enable desired port */
780109323Ssam	wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
78146492Swpaul
782114124Simp	sc->sc_enabled = 1;
783109323Ssam	ifp->if_flags |= IFF_RUNNING;
784109323Ssam	ifp->if_flags &= ~IFF_OACTIVE;
785109323Ssam	if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
786139542Ssam	    ic->ic_opmode == IEEE80211_M_IBSS ||
787117855Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR ||
788109323Ssam	    ic->ic_opmode == IEEE80211_M_HOSTAP)
789139542Ssam		ieee80211_create_ibss(ic, ic->ic_ibss_chan);
79046492Swpaul
791109323Ssam	/* Enable interrupts */
792109323Ssam	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
79346492Swpaul
794109323Ssam	if (!wasenabled &&
795109323Ssam	    ic->ic_opmode == IEEE80211_M_HOSTAP &&
796109323Ssam	    sc->sc_firmware_type == WI_INTERSIL) {
797109323Ssam		/* XXX: some card need to be re-enabled for hostap */
798109323Ssam		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
799109323Ssam		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
80075149Simp	}
80194397Simp
802109323Ssam	if (ic->ic_opmode == IEEE80211_M_STA &&
803109323Ssam	    ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
804116951Ssam	    ic->ic_des_chan != IEEE80211_CHAN_ANYC)) {
805109323Ssam		memset(&join, 0, sizeof(join));
806109323Ssam		if (ic->ic_flags & IEEE80211_F_DESBSSID)
807109323Ssam			IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
808116951Ssam		if (ic->ic_des_chan != IEEE80211_CHAN_ANYC)
809116951Ssam			join.wi_chan = htole16(
810116951Ssam				ieee80211_chan2ieee(ic, ic->ic_des_chan));
811109323Ssam		/* Lucent firmware does not support the JOIN RID. */
812109323Ssam		if (sc->sc_firmware_type != WI_LUCENT)
813109323Ssam			wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
81494397Simp	}
81575373Salfred
816109323Ssam	WI_UNLOCK(sc);
81746492Swpaul	return;
818109323Ssamout:
819109323Ssam	if (error) {
820109323Ssam		if_printf(ifp, "interface not running\n");
821123098Simp		wi_stop(ifp, 1);
82270073Swpaul	}
823110224Simp	WI_UNLOCK(sc);
824109323Ssam	DPRINTF(("wi_init: return %d\n", error));
825109323Ssam	return;
82646492Swpaul}
82746492Swpaul
828109323Ssamvoid
829109323Ssamwi_stop(struct ifnet *ifp, int disable)
83046492Swpaul{
831109323Ssam	struct wi_softc *sc = ifp->if_softc;
832138571Ssam	struct ieee80211com *ic = &sc->sc_ic;
833109323Ssam	WI_LOCK_DECL();
83446492Swpaul
835109323Ssam	WI_LOCK(sc);
83674998Swpaul
837123098Simp	DELAY(100000);
838123098Simp
839117812Ssam	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
840109323Ssam	if (sc->sc_enabled && !sc->wi_gone) {
841109323Ssam		CSR_WRITE_2(sc, WI_INT_EN, 0);
842109323Ssam		wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
843109323Ssam		if (disable) {
844109323Ssam#ifdef __NetBSD__
845109323Ssam			if (sc->sc_disable)
846109323Ssam				(*sc->sc_disable)(sc);
847109323Ssam#endif
848116951Ssam			sc->sc_enabled = 0;
84970073Swpaul		}
850123098Simp	} else if (sc->wi_gone && disable)	/* gone --> not enabled */
851123098Simp	    sc->sc_enabled = 0;
85270073Swpaul
853109323Ssam	sc->sc_tx_timer = 0;
854109323Ssam	sc->sc_scan_timer = 0;
855109323Ssam	sc->sc_false_syns = 0;
856109323Ssam	sc->sc_naps = 0;
857109323Ssam	ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
858109323Ssam	ifp->if_timer = 0;
85946492Swpaul
860109323Ssam	WI_UNLOCK(sc);
86146492Swpaul}
86246492Swpaul
863109323Ssamstatic void
864109323Ssamwi_start(struct ifnet *ifp)
86546492Swpaul{
866109323Ssam	struct wi_softc	*sc = ifp->if_softc;
867109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
868119150Ssam	struct ieee80211_node *ni;
869109323Ssam	struct ieee80211_frame *wh;
870138571Ssam	struct ether_header *eh;
871109323Ssam	struct mbuf *m0;
872109323Ssam	struct wi_frame frmhdr;
873119150Ssam	int cur, fid, off, error;
874109323Ssam	WI_LOCK_DECL();
87546492Swpaul
876109323Ssam	WI_LOCK(sc);
87746492Swpaul
878109323Ssam	if (sc->wi_gone) {
879109323Ssam		WI_UNLOCK(sc);
880109323Ssam		return;
88146492Swpaul	}
882109323Ssam	if (sc->sc_flags & WI_FLAGS_OUTRANGE) {
883109323Ssam		WI_UNLOCK(sc);
884109323Ssam		return;
88575373Salfred	}
88646492Swpaul
887109323Ssam	memset(&frmhdr, 0, sizeof(frmhdr));
888109323Ssam	cur = sc->sc_txnext;
889109323Ssam	for (;;) {
890109323Ssam		IF_POLL(&ic->ic_mgtq, m0);
891109323Ssam		if (m0 != NULL) {
892109323Ssam			if (sc->sc_txd[cur].d_len != 0) {
893109323Ssam				ifp->if_flags |= IFF_OACTIVE;
894109323Ssam				break;
895109323Ssam			}
896109323Ssam			IF_DEQUEUE(&ic->ic_mgtq, m0);
897119150Ssam			/*
898119150Ssam			 * Hack!  The referenced node pointer is in the
899119150Ssam			 * rcvif field of the packet header.  This is
900119150Ssam			 * placed there by ieee80211_mgmt_output because
901119150Ssam			 * we need to hold the reference with the frame
902119150Ssam			 * and there's no other way (other than packet
903119150Ssam			 * tags which we consider too expensive to use)
904119150Ssam			 * to pass it along.
905119150Ssam			 */
906119150Ssam			ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
907119150Ssam			m0->m_pkthdr.rcvif = NULL;
908119150Ssam
909109323Ssam			m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
910109323Ssam			    (caddr_t)&frmhdr.wi_ehdr);
911109323Ssam			frmhdr.wi_ehdr.ether_type = 0;
912109323Ssam                        wh = mtod(m0, struct ieee80211_frame *);
913109323Ssam		} else {
914109323Ssam			if (ic->ic_state != IEEE80211_S_RUN)
915109323Ssam				break;
916132986Smlaier			IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
917109323Ssam			if (m0 == NULL)
918109323Ssam				break;
919109323Ssam			if (sc->sc_txd[cur].d_len != 0) {
920132986Smlaier				IFQ_DRV_PREPEND(&ifp->if_snd, m0);
921109323Ssam				ifp->if_flags |= IFF_OACTIVE;
922109323Ssam				break;
923109323Ssam			}
924138571Ssam			if (m0->m_len < sizeof(struct ether_header) &&
925138571Ssam			    (m0 = m_pullup(m0, sizeof(struct ether_header))) == NULL) {
926138571Ssam				ifp->if_oerrors++;
927138571Ssam				continue;
928138571Ssam			}
929138571Ssam			eh = mtod(m0, struct ether_header *);
930138571Ssam			ni = ieee80211_find_txnode(ic, eh->ether_dhost);
931138571Ssam			if (ni == NULL) {
932138571Ssam				m_freem(m0);
933138571Ssam				continue;
934138571Ssam			}
935109323Ssam			ifp->if_opackets++;
936109323Ssam			m_copydata(m0, 0, ETHER_HDR_LEN,
937109323Ssam			    (caddr_t)&frmhdr.wi_ehdr);
938109323Ssam#if NBPFILTER > 0
939109323Ssam			BPF_MTAP(ifp, m0);
940109323Ssam#endif
94146492Swpaul
942138571Ssam			m0 = ieee80211_encap(ic, m0, ni);
943119150Ssam			if (m0 == NULL) {
944109323Ssam				ifp->if_oerrors++;
945138571Ssam				ieee80211_free_node(ni);
946109323Ssam				continue;
947109323Ssam			}
948109323Ssam                        wh = mtod(m0, struct ieee80211_frame *);
949109323Ssam		}
950109323Ssam#if NBPFILTER > 0
951109323Ssam		if (ic->ic_rawbpf)
952109323Ssam			bpf_mtap(ic->ic_rawbpf, m0);
953109323Ssam#endif
954109323Ssam		frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
955138952Ssam		/* XXX check key for SWCRYPT instead of using operating mode */
956138952Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
957138952Ssam		    (wh->i_fc[1] & IEEE80211_FC1_WEP)) {
958138571Ssam			struct ieee80211_key *k;
959138571Ssam
960138571Ssam			k = ieee80211_crypto_encap(ic, ni, m0);
961138571Ssam			if (k == NULL) {
962138571Ssam				if (ni != NULL)
963138571Ssam					ieee80211_free_node(ni);
964143299Ssam				m_freem(m0);
965109323Ssam				continue;
966109323Ssam			}
967138952Ssam			frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
968109323Ssam		}
969109323Ssam#if NBPFILTER > 0
970109323Ssam		if (sc->sc_drvbpf) {
971123927Ssam			sc->sc_tx_th.wt_rate =
972123927Ssam				ni->ni_rates.rs_rates[ni->ni_txrate];
973123922Ssam			bpf_mtap2(sc->sc_drvbpf,
974127698Ssam				&sc->sc_tx_th, sc->sc_tx_th_len, m0);
975109323Ssam		}
97646492Swpaul#endif
977127697Ssam		m_copydata(m0, 0, sizeof(struct ieee80211_frame),
978127697Ssam		    (caddr_t)&frmhdr.wi_whdr);
979127697Ssam		m_adj(m0, sizeof(struct ieee80211_frame));
980127697Ssam		frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
981109323Ssam		if (IFF_DUMPPKTS(ifp))
982116951Ssam			wi_dump_pkt(&frmhdr, NULL, -1);
983109323Ssam		fid = sc->sc_txd[cur].d_fid;
984109323Ssam		off = sizeof(frmhdr);
985119150Ssam		error = wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0
986119150Ssam		     || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
987119150Ssam		m_freem(m0);
988138571Ssam		if (ni != NULL)
989138571Ssam			ieee80211_free_node(ni);
990119150Ssam		if (error) {
991109323Ssam			ifp->if_oerrors++;
992109323Ssam			continue;
993109323Ssam		}
994109323Ssam		sc->sc_txd[cur].d_len = off;
995109323Ssam		if (sc->sc_txcur == cur) {
996109323Ssam			if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
997109323Ssam				if_printf(ifp, "xmit failed\n");
998109323Ssam				sc->sc_txd[cur].d_len = 0;
999109323Ssam				continue;
1000109323Ssam			}
1001109323Ssam			sc->sc_tx_timer = 5;
1002109323Ssam			ifp->if_timer = 1;
1003109323Ssam		}
1004112363Simp		sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
100574838Salfred	}
100646492Swpaul
1007109323Ssam	WI_UNLOCK(sc);
100846492Swpaul}
100946492Swpaul
101088546Salfredstatic int
1011109323Ssamwi_reset(struct wi_softc *sc)
101246492Swpaul{
1013138571Ssam	struct ifnet *ifp = &sc->sc_if;
1014114124Simp#define WI_INIT_TRIES 3
1015114124Simp	int i;
1016114138Simp	int error = 0;
1017114124Simp	int tries;
1018114124Simp
1019114124Simp	/* Symbol firmware cannot be initialized more than once */
1020123098Simp	if (sc->sc_firmware_type == WI_SYMBOL && sc->sc_reset)
1021112362Simp		return (0);
1022114124Simp	if (sc->sc_firmware_type == WI_SYMBOL)
1023114124Simp		tries = 1;
1024114124Simp	else
1025114124Simp		tries = WI_INIT_TRIES;
1026112362Simp
1027114124Simp	for (i = 0; i < tries; i++) {
1028109323Ssam		if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
102946492Swpaul			break;
1030109323Ssam		DELAY(WI_DELAY * 1000);
103146492Swpaul	}
1032114124Simp	sc->sc_reset = 1;
103346492Swpaul
1034114124Simp	if (i == tries) {
1035114124Simp		if_printf(ifp, "init failed\n");
1036114124Simp		return (error);
103775373Salfred	}
103846492Swpaul
1039109323Ssam	CSR_WRITE_2(sc, WI_INT_EN, 0);
1040114124Simp	CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
104146492Swpaul
1042109323Ssam	/* Calibrate timer. */
1043114124Simp	wi_write_val(sc, WI_RID_TICK_TIME, 8);
1044114124Simp
1045114124Simp	return (0);
1046109323Ssam#undef WI_INIT_TRIES
104746492Swpaul}
104846492Swpaul
104988546Salfredstatic void
1050109323Ssamwi_watchdog(struct ifnet *ifp)
105146492Swpaul{
1052109323Ssam	struct wi_softc	*sc = ifp->if_softc;
105346492Swpaul
1054109323Ssam	ifp->if_timer = 0;
1055109323Ssam	if (!sc->sc_enabled)
1056109323Ssam		return;
105746492Swpaul
1058109323Ssam	if (sc->sc_tx_timer) {
1059109323Ssam		if (--sc->sc_tx_timer == 0) {
1060109323Ssam			if_printf(ifp, "device timeout\n");
1061109323Ssam			ifp->if_oerrors++;
1062109323Ssam			wi_init(ifp->if_softc);
1063109323Ssam			return;
1064109323Ssam		}
1065109323Ssam		ifp->if_timer = 1;
106646492Swpaul	}
106746492Swpaul
1068109323Ssam	if (sc->sc_scan_timer) {
1069109323Ssam		if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
1070109323Ssam		    sc->sc_firmware_type == WI_INTERSIL) {
1071109323Ssam			DPRINTF(("wi_watchdog: inquire scan\n"));
1072109323Ssam			wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
107346492Swpaul		}
1074109323Ssam		if (sc->sc_scan_timer)
1075109323Ssam			ifp->if_timer = 1;
107646492Swpaul	}
107746492Swpaul
1078109323Ssam	/* TODO: rate control */
1079138571Ssam	ieee80211_watchdog(&sc->sc_ic);
108046492Swpaul}
108146492Swpaul
108288546Salfredstatic int
1083109323Ssamwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
108446492Swpaul{
1085109323Ssam	struct wi_softc *sc = ifp->if_softc;
1086109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1087109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
1088109323Ssam	struct ieee80211req *ireq;
1089109323Ssam	u_int8_t nodename[IEEE80211_NWID_LEN];
1090109323Ssam	int error = 0;
109195534Simp#if __FreeBSD_version >= 500000
1092109323Ssam	struct thread *td = curthread;
109395534Simp#else
1094109323Ssam	struct proc *td = curproc;		/* Little white lie */
109595534Simp#endif
1096109323Ssam	struct wi_req wreq;
1097109323Ssam	WI_LOCK_DECL();
109846492Swpaul
1099138571Ssam	if (sc->wi_gone)
1100138571Ssam		return (ENODEV);
110146492Swpaul
1102109323Ssam	switch (cmd) {
110346492Swpaul	case SIOCSIFFLAGS:
1104100876Simp		/*
1105101139Simp		 * Can't do promisc and hostap at the same time.  If all that's
1106101139Simp		 * changing is the promisc flag, try to short-circuit a call to
1107101139Simp		 * wi_init() by just setting PROMISC in the hardware.
1108100876Simp		 */
1109138571Ssam		WI_LOCK(sc);
111046492Swpaul		if (ifp->if_flags & IFF_UP) {
1111109323Ssam			if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1112101139Simp			    ifp->if_flags & IFF_RUNNING) {
1113101139Simp				if (ifp->if_flags & IFF_PROMISC &&
1114109323Ssam				    !(sc->sc_if_flags & IFF_PROMISC)) {
1115109323Ssam					wi_write_val(sc, WI_RID_PROMISC, 1);
1116101139Simp				} else if (!(ifp->if_flags & IFF_PROMISC) &&
1117109323Ssam				    sc->sc_if_flags & IFF_PROMISC) {
1118109323Ssam					wi_write_val(sc, WI_RID_PROMISC, 0);
1119101139Simp				} else {
1120101139Simp					wi_init(sc);
1121101139Simp				}
1122100876Simp			} else {
112346492Swpaul				wi_init(sc);
1124100876Simp			}
112546492Swpaul		} else {
112646492Swpaul			if (ifp->if_flags & IFF_RUNNING) {
1127123098Simp				wi_stop(ifp, 1);
112846492Swpaul			}
1129123098Simp			sc->wi_gone = 0;
113046492Swpaul		}
1131109323Ssam		sc->sc_if_flags = ifp->if_flags;
1132138571Ssam		WI_UNLOCK(sc);
113346492Swpaul		error = 0;
113446492Swpaul		break;
113546492Swpaul	case SIOCADDMULTI:
113646492Swpaul	case SIOCDELMULTI:
1137138571Ssam		WI_LOCK(sc);
1138109323Ssam		error = wi_write_multi(sc);
1139138571Ssam		WI_UNLOCK(sc);
114046492Swpaul		break;
1141109323Ssam	case SIOCGIFGENERIC:
1142138571Ssam		WI_LOCK(sc);
1143109323Ssam		error = wi_get_cfg(ifp, cmd, data);
1144138571Ssam		WI_UNLOCK(sc);
114546492Swpaul		break;
1146109323Ssam	case SIOCSIFGENERIC:
1147109323Ssam		error = suser(td);
1148138571Ssam		if (error == 0)
1149138571Ssam			error = wi_set_cfg(ifp, cmd, data);
115046492Swpaul		break;
115193359Simp	case SIOCGPRISM2DEBUG:
115293359Simp		error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
115393359Simp		if (error)
115493359Simp			break;
115593733Simp		if (!(ifp->if_flags & IFF_RUNNING) ||
115693733Simp		    sc->sc_firmware_type == WI_LUCENT) {
115793359Simp			error = EIO;
115893359Simp			break;
115993359Simp		}
116093359Simp		error = wi_get_debug(sc, &wreq);
116193359Simp		if (error == 0)
116293359Simp			error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
116393359Simp		break;
116493359Simp	case SIOCSPRISM2DEBUG:
116593593Sjhb		if ((error = suser(td)))
1166138571Ssam			return (error);
116793359Simp		error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
116893359Simp		if (error)
116993359Simp			break;
1170138571Ssam		WI_LOCK(sc);
117193359Simp		error = wi_set_debug(sc, &wreq);
1172138571Ssam		WI_UNLOCK(sc);
117393359Simp		break;
117477217Sphk	case SIOCG80211:
1175109323Ssam		ireq = (struct ieee80211req *) data;
1176138571Ssam		if (ireq->i_type == IEEE80211_IOC_STATIONNAME) {
1177109323Ssam			ireq->i_len = sc->sc_nodelen + 1;
1178109323Ssam			error = copyout(sc->sc_nodename, ireq->i_data,
1179109323Ssam					ireq->i_len);
118077217Sphk			break;
118177217Sphk		}
1182138571Ssam		goto ioctl_common;
118377217Sphk	case SIOCS80211:
1184109323Ssam		ireq = (struct ieee80211req *) data;
1185138571Ssam		if (ireq->i_type == IEEE80211_IOC_STATIONNAME) {
1186138571Ssam			error = suser(td);
1187138571Ssam			if (error)
1188138571Ssam				break;
118977217Sphk			if (ireq->i_val != 0 ||
119077217Sphk			    ireq->i_len > IEEE80211_NWID_LEN) {
119177217Sphk				error = EINVAL;
119277217Sphk				break;
119377217Sphk			}
1194109323Ssam			memset(nodename, 0, IEEE80211_NWID_LEN);
1195109323Ssam			error = copyin(ireq->i_data, nodename, ireq->i_len);
1196109323Ssam			if (error)
119777217Sphk				break;
1198138571Ssam			WI_LOCK(sc);
1199109323Ssam			if (sc->sc_enabled) {
1200109323Ssam				error = wi_write_ssid(sc, WI_RID_NODENAME,
1201109323Ssam					nodename, ireq->i_len);
120277217Sphk			}
1203138571Ssam			if (error == 0) {
1204138571Ssam				memcpy(sc->sc_nodename, nodename,
1205138571Ssam					IEEE80211_NWID_LEN);
1206138571Ssam				sc->sc_nodelen = ireq->i_len;
1207138571Ssam			}
1208138571Ssam			WI_UNLOCK(sc);
120977217Sphk			break;
121077217Sphk		}
1211138571Ssam		goto ioctl_common;
121246492Swpaul	default:
1213138571Ssam	ioctl_common:
1214138571Ssam		WI_LOCK(sc);
1215138571Ssam		error = ieee80211_ioctl(ic, cmd, data);
1216138571Ssam		if (error == ENETRESET) {
1217138571Ssam			if (sc->sc_enabled)
1218138571Ssam				wi_init(sc);	/* XXX no error return */
1219138571Ssam			error = 0;
1220138571Ssam		}
1221138571Ssam		WI_UNLOCK(sc);
122246492Swpaul		break;
122346492Swpaul	}
1224109323Ssam	return (error);
122546492Swpaul}
122646492Swpaul
1227109323Ssamstatic int
1228109323Ssamwi_media_change(struct ifnet *ifp)
122946492Swpaul{
1230109323Ssam	struct wi_softc *sc = ifp->if_softc;
1231116951Ssam	int error;
123246492Swpaul
1233116951Ssam	error = ieee80211_media_change(ifp);
1234109323Ssam	if (error == ENETRESET) {
1235109323Ssam		if (sc->sc_enabled)
1236116951Ssam			wi_init(sc);	/* XXX no error return */
1237109323Ssam		error = 0;
1238109323Ssam	}
1239109323Ssam	return error;
1240109323Ssam}
124146492Swpaul
1242109323Ssamstatic void
1243109323Ssamwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1244109323Ssam{
1245109323Ssam	struct wi_softc *sc = ifp->if_softc;
1246109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1247109323Ssam	u_int16_t val;
1248109323Ssam	int rate, len;
124946492Swpaul
1250109323Ssam	if (sc->wi_gone || !sc->sc_enabled) {
1251109323Ssam		imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
1252109323Ssam		imr->ifm_status = 0;
1253109323Ssam		return;
1254109323Ssam	}
125546492Swpaul
1256109323Ssam	imr->ifm_status = IFM_AVALID;
1257109323Ssam	imr->ifm_active = IFM_IEEE80211;
1258109323Ssam	if (ic->ic_state == IEEE80211_S_RUN &&
1259109323Ssam	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
1260109323Ssam		imr->ifm_status |= IFM_ACTIVE;
1261109323Ssam	len = sizeof(val);
1262138571Ssam	if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) == 0 &&
1263138571Ssam	    len == sizeof(val)) {
1264109323Ssam		/* convert to 802.11 rate */
1265138571Ssam		val = le16toh(val);
1266109323Ssam		rate = val * 2;
1267109323Ssam		if (sc->sc_firmware_type == WI_LUCENT) {
1268138571Ssam			if (rate == 10)
1269109323Ssam				rate = 11;	/* 5.5Mbps */
1270109323Ssam		} else {
1271109323Ssam			if (rate == 4*2)
1272109323Ssam				rate = 11;	/* 5.5Mbps */
1273109323Ssam			else if (rate == 8*2)
1274109323Ssam				rate = 22;	/* 11Mbps */
1275109323Ssam		}
1276138571Ssam	} else
1277138571Ssam		rate = 0;
1278116951Ssam	imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
1279109323Ssam	switch (ic->ic_opmode) {
1280109323Ssam	case IEEE80211_M_STA:
1281109323Ssam		break;
1282109323Ssam	case IEEE80211_M_IBSS:
1283109323Ssam		imr->ifm_active |= IFM_IEEE80211_ADHOC;
1284109323Ssam		break;
1285109323Ssam	case IEEE80211_M_AHDEMO:
1286109323Ssam		imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1287109323Ssam		break;
1288109323Ssam	case IEEE80211_M_HOSTAP:
1289109323Ssam		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1290109323Ssam		break;
1291117855Ssam	case IEEE80211_M_MONITOR:
1292117855Ssam		imr->ifm_active |= IFM_IEEE80211_MONITOR;
1293117855Ssam		break;
1294109323Ssam	}
1295109323Ssam}
129646492Swpaul
1297109323Ssamstatic void
1298109323Ssamwi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1299109323Ssam{
1300109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1301116951Ssam	struct ieee80211_node *ni = ic->ic_bss;
1302138571Ssam	struct ifnet *ifp = &sc->sc_if;
130398440Simp
1304109323Ssam	if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1305109323Ssam		return;
130646492Swpaul
1307109323Ssam	DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid)));
1308109323Ssam	DPRINTF(("%s ?\n", ether_sprintf(new_bssid)));
130946492Swpaul
1310109323Ssam	/* In promiscuous mode, the BSSID field is not a reliable
1311109323Ssam	 * indicator of the firmware's BSSID. Damp spurious
1312109323Ssam	 * change-of-BSSID indications.
1313109323Ssam	 */
1314109323Ssam	if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1315138571Ssam	    !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns,
1316138571Ssam	                 WI_MAX_FALSE_SYNS))
1317109323Ssam		return;
131846492Swpaul
1319139542Ssam	sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1);
1320139542Ssam	/*
1321139542Ssam	 * XXX hack; we should create a new node with the new bssid
1322139542Ssam	 * and replace the existing ic_bss with it but since we don't
1323139542Ssam	 * process management frames to collect state we cheat by
1324139542Ssam	 * reusing the existing node as we know wi_newstate will be
1325139542Ssam	 * called and it will overwrite the node state.
1326139542Ssam	 */
1327139542Ssam	ieee80211_sta_join(ic, ieee80211_ref_node(ni));
1328109323Ssam}
132946492Swpaul
1330109323Ssamstatic void
1331116898Ssamwi_rx_monitor(struct wi_softc *sc, int fid)
1332116898Ssam{
1333138571Ssam	struct ifnet *ifp = &sc->sc_if;
1334116898Ssam	struct wi_frame *rx_frame;
1335116898Ssam	struct mbuf *m;
1336116898Ssam	int datlen, hdrlen;
1337116898Ssam
1338116898Ssam	/* first allocate mbuf for packet storage */
1339116898Ssam	m = m_getcl(M_DONTWAIT, MT_DATA, 0);
1340116898Ssam	if (m == NULL) {
1341116898Ssam		ifp->if_ierrors++;
1342116898Ssam		return;
1343116898Ssam	}
1344116898Ssam
1345116898Ssam	m->m_pkthdr.rcvif = ifp;
1346116898Ssam
1347116898Ssam	/* now read wi_frame first so we know how much data to read */
1348116898Ssam	if (wi_read_bap(sc, fid, 0, mtod(m, caddr_t), sizeof(*rx_frame))) {
1349116898Ssam		ifp->if_ierrors++;
1350116898Ssam		goto done;
1351116898Ssam	}
1352116898Ssam
1353116898Ssam	rx_frame = mtod(m, struct wi_frame *);
1354116898Ssam
1355116898Ssam	switch ((rx_frame->wi_status & WI_STAT_MAC_PORT) >> 8) {
1356116898Ssam	case 7:
1357116898Ssam		switch (rx_frame->wi_whdr.i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1358116898Ssam		case IEEE80211_FC0_TYPE_DATA:
1359116898Ssam			hdrlen = WI_DATA_HDRLEN;
1360116898Ssam			datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1361116898Ssam			break;
1362116898Ssam		case IEEE80211_FC0_TYPE_MGT:
1363116898Ssam			hdrlen = WI_MGMT_HDRLEN;
1364116898Ssam			datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1365116898Ssam			break;
1366116898Ssam		case IEEE80211_FC0_TYPE_CTL:
1367116898Ssam			/*
1368116898Ssam			 * prism2 cards don't pass control packets
1369116898Ssam			 * down properly or consistently, so we'll only
1370116898Ssam			 * pass down the header.
1371116898Ssam			 */
1372116898Ssam			hdrlen = WI_CTL_HDRLEN;
1373116898Ssam			datlen = 0;
1374116898Ssam			break;
1375116898Ssam		default:
1376116898Ssam			if_printf(ifp, "received packet of unknown type "
1377116898Ssam				"on port 7\n");
1378116898Ssam			ifp->if_ierrors++;
1379116898Ssam			goto done;
1380116898Ssam		}
1381116898Ssam		break;
1382116898Ssam	case 0:
1383116898Ssam		hdrlen = WI_DATA_HDRLEN;
1384116898Ssam		datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1385116898Ssam		break;
1386116898Ssam	default:
1387116898Ssam		if_printf(ifp, "received packet on invalid "
1388116898Ssam		    "port (wi_status=0x%x)\n", rx_frame->wi_status);
1389116898Ssam		ifp->if_ierrors++;
1390116898Ssam		goto done;
1391116898Ssam	}
1392116898Ssam
1393116898Ssam	if (hdrlen + datlen + 2 > MCLBYTES) {
1394116898Ssam		if_printf(ifp, "oversized packet received "
1395116898Ssam		    "(wi_dat_len=%d, wi_status=0x%x)\n",
1396116898Ssam		    datlen, rx_frame->wi_status);
1397116898Ssam		ifp->if_ierrors++;
1398116898Ssam		goto done;
1399116898Ssam	}
1400116898Ssam
1401116898Ssam	if (wi_read_bap(sc, fid, hdrlen, mtod(m, caddr_t) + hdrlen,
1402116898Ssam	    datlen + 2) == 0) {
1403116898Ssam		m->m_pkthdr.len = m->m_len = hdrlen + datlen;
1404116898Ssam		ifp->if_ipackets++;
1405116898Ssam		BPF_MTAP(ifp, m);	/* Handle BPF listeners. */
1406116898Ssam	} else
1407116898Ssam		ifp->if_ierrors++;
1408116898Ssamdone:
1409116898Ssam	m_freem(m);
1410116898Ssam}
1411116898Ssam
1412116898Ssamstatic void
1413109323Ssamwi_rx_intr(struct wi_softc *sc)
1414109323Ssam{
1415109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1416138571Ssam	struct ifnet *ifp = &sc->sc_if;
1417109323Ssam	struct wi_frame frmhdr;
1418109323Ssam	struct mbuf *m;
1419109323Ssam	struct ieee80211_frame *wh;
1420119150Ssam	struct ieee80211_node *ni;
1421109323Ssam	int fid, len, off, rssi;
1422109323Ssam	u_int8_t dir;
1423109323Ssam	u_int16_t status;
1424109323Ssam	u_int32_t rstamp;
142546611Swpaul
1426109323Ssam	fid = CSR_READ_2(sc, WI_RX_FID);
142746611Swpaul
1428116898Ssam	if (sc->wi_debug.wi_monitor) {
1429116898Ssam		/*
1430116898Ssam		 * If we are in monitor mode just
1431116898Ssam		 * read the data from the device.
1432116898Ssam		 */
1433116898Ssam		wi_rx_monitor(sc, fid);
1434116898Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1435116898Ssam		return;
1436116898Ssam	}
1437116898Ssam
1438109323Ssam	/* First read in the frame header */
1439109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1440109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1441109323Ssam		ifp->if_ierrors++;
1442109323Ssam		DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1443109323Ssam		return;
1444109323Ssam	}
144591695Simp
1446109323Ssam	if (IFF_DUMPPKTS(ifp))
1447109323Ssam		wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal);
144846492Swpaul
1449109323Ssam	/*
1450109323Ssam	 * Drop undecryptable or packets with receive errors here
1451109323Ssam	 */
1452109323Ssam	status = le16toh(frmhdr.wi_status);
1453109323Ssam	if (status & WI_STAT_ERRSTAT) {
1454109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1455109323Ssam		ifp->if_ierrors++;
1456109323Ssam		DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1457109323Ssam		return;
1458109323Ssam	}
1459109323Ssam	rssi = frmhdr.wi_rx_signal;
1460109323Ssam	rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1461109323Ssam	    le16toh(frmhdr.wi_rx_tstamp1);
146246492Swpaul
1463109323Ssam	len = le16toh(frmhdr.wi_dat_len);
1464109323Ssam	off = ALIGN(sizeof(struct ieee80211_frame));
146546563Swpaul
1466117855Ssam	/*
1467117855Ssam	 * Sometimes the PRISM2.x returns bogusly large frames. Except
1468117855Ssam	 * in monitor mode, just throw them away.
1469117855Ssam	 */
1470117855Ssam	if (off + len > MCLBYTES) {
1471117855Ssam		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1472117855Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1473117855Ssam			ifp->if_ierrors++;
1474117855Ssam			DPRINTF(("wi_rx_intr: oversized packet\n"));
1475117855Ssam			return;
1476117855Ssam		} else
1477117855Ssam			len = 0;
1478117855Ssam	}
1479117855Ssam
1480111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
1481109323Ssam	if (m == NULL) {
1482109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1483109323Ssam		ifp->if_ierrors++;
1484109323Ssam		DPRINTF(("wi_rx_intr: MGET failed\n"));
1485109323Ssam		return;
1486109323Ssam	}
1487109323Ssam	if (off + len > MHLEN) {
1488111119Simp		MCLGET(m, M_DONTWAIT);
1489109323Ssam		if ((m->m_flags & M_EXT) == 0) {
1490109323Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1491109323Ssam			m_freem(m);
1492109323Ssam			ifp->if_ierrors++;
1493109323Ssam			DPRINTF(("wi_rx_intr: MCLGET failed\n"));
1494109323Ssam			return;
1495109323Ssam		}
1496109323Ssam	}
149746492Swpaul
1498109323Ssam	m->m_data += off - sizeof(struct ieee80211_frame);
1499109323Ssam	memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1500109323Ssam	wi_read_bap(sc, fid, sizeof(frmhdr),
1501109323Ssam	    m->m_data + sizeof(struct ieee80211_frame), len);
1502109323Ssam	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1503109323Ssam	m->m_pkthdr.rcvif = ifp;
150494405Simp
1505109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
150646492Swpaul
1507138952Ssam	wh = mtod(m, struct ieee80211_frame *);
1508138952Ssam	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1509138952Ssam		/*
1510138952Ssam		 * WEP is decrypted by hardware and the IV
1511138952Ssam		 * is stripped.  Clear WEP bit so we don't
1512138952Ssam		 * try to process it in ieee80211_input.
1513138952Ssam		 * XXX fix for TKIP, et. al.
1514138952Ssam		 */
1515138952Ssam		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1516138952Ssam	}
1517138952Ssam
1518109323Ssam#if NBPFILTER > 0
1519109323Ssam	if (sc->sc_drvbpf) {
1520123927Ssam		/* XXX replace divide by table */
1521123922Ssam		sc->sc_rx_th.wr_rate = frmhdr.wi_rx_rate / 5;
1522127148Sgreen		sc->sc_rx_th.wr_antsignal = frmhdr.wi_rx_signal;
1523127148Sgreen		sc->sc_rx_th.wr_antnoise = frmhdr.wi_rx_silence;
1524123927Ssam		sc->sc_rx_th.wr_flags = 0;
1525123927Ssam		if (frmhdr.wi_status & WI_STAT_PCF)
1526123927Ssam			sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_CFP;
1527138952Ssam		/* XXX IEEE80211_RADIOTAP_F_WEP */
1528123922Ssam		bpf_mtap2(sc->sc_drvbpf,
1529127698Ssam			&sc->sc_rx_th, sc->sc_rx_th_len, m);
153056965Swpaul	}
1531109323Ssam#endif
153256965Swpaul
1533109323Ssam	/* synchronize driver's BSSID with firmware's BSSID */
1534109323Ssam	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1535109323Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1536109323Ssam		wi_sync_bssid(sc, wh->i_addr3);
153746492Swpaul
1538119150Ssam	/*
1539119150Ssam	 * Locate the node for sender, track state, and
1540119150Ssam	 * then pass this node (referenced) up to the 802.11
1541138571Ssam	 * layer for its use.
1542119150Ssam	 */
1543138571Ssam	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *) wh);
1544119150Ssam	/*
1545119150Ssam	 * Send frame up for processing.
1546119150Ssam	 */
1547138571Ssam	ieee80211_input(ic, m, ni, rssi, rstamp);
1548119150Ssam	/*
1549119150Ssam	 * The frame may have caused the node to be marked for
1550119150Ssam	 * reclamation (e.g. in response to a DEAUTH message)
1551119150Ssam	 * so use free_node here instead of unref_node.
1552119150Ssam	 */
1553138571Ssam	ieee80211_free_node(ni);
1554109323Ssam}
155546492Swpaul
1556109323Ssamstatic void
1557109323Ssamwi_tx_ex_intr(struct wi_softc *sc)
1558109323Ssam{
1559138571Ssam	struct ifnet *ifp = &sc->sc_if;
1560109323Ssam	struct wi_frame frmhdr;
1561109323Ssam	int fid;
156246492Swpaul
1563109323Ssam	fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1564109323Ssam	/* Read in the frame header */
1565109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1566109323Ssam		u_int16_t status = le16toh(frmhdr.wi_status);
156746492Swpaul
1568109323Ssam		/*
1569109323Ssam		 * Spontaneous station disconnects appear as xmit
1570109323Ssam		 * errors.  Don't announce them and/or count them
1571109323Ssam		 * as an output error.
1572109323Ssam		 */
1573109323Ssam		if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1574109323Ssam			if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1575109323Ssam				if_printf(ifp, "tx failed");
1576109323Ssam				if (status & WI_TXSTAT_RET_ERR)
1577109323Ssam					printf(", retry limit exceeded");
1578109323Ssam				if (status & WI_TXSTAT_AGED_ERR)
1579109323Ssam					printf(", max transmit lifetime exceeded");
1580109323Ssam				if (status & WI_TXSTAT_DISCONNECT)
1581109323Ssam					printf(", port disconnected");
1582109323Ssam				if (status & WI_TXSTAT_FORM_ERR)
1583109323Ssam					printf(", invalid format (data len %u src %6D)",
1584109323Ssam						le16toh(frmhdr.wi_dat_len),
1585109323Ssam						frmhdr.wi_ehdr.ether_shost, ":");
1586109323Ssam				if (status & ~0xf)
1587109323Ssam					printf(", status=0x%x", status);
1588109323Ssam				printf("\n");
1589109323Ssam			}
1590109323Ssam			ifp->if_oerrors++;
1591109323Ssam		} else {
1592109323Ssam			DPRINTF(("port disconnected\n"));
1593109323Ssam			ifp->if_collisions++;	/* XXX */
1594109323Ssam		}
1595109323Ssam	} else
1596109323Ssam		DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
1597109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1598109323Ssam}
159946492Swpaul
1600109323Ssamstatic void
1601109323Ssamwi_tx_intr(struct wi_softc *sc)
1602109323Ssam{
1603138571Ssam	struct ifnet *ifp = &sc->sc_if;
1604109323Ssam	int fid, cur;
160594405Simp
1606123098Simp	if (sc->wi_gone)
1607123098Simp		return;
1608123098Simp
1609109323Ssam	fid = CSR_READ_2(sc, WI_ALLOC_FID);
1610109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
161146492Swpaul
1612109323Ssam	cur = sc->sc_txcur;
1613109323Ssam	if (sc->sc_txd[cur].d_fid != fid) {
1614109323Ssam		if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
1615109323Ssam		    fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
1616109323Ssam		return;
1617109323Ssam	}
1618109323Ssam	sc->sc_tx_timer = 0;
1619109323Ssam	sc->sc_txd[cur].d_len = 0;
1620112363Simp	sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
1621109323Ssam	if (sc->sc_txd[cur].d_len == 0)
1622109323Ssam		ifp->if_flags &= ~IFF_OACTIVE;
1623109323Ssam	else {
1624109323Ssam		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1625109323Ssam		    0, 0)) {
1626109323Ssam			if_printf(ifp, "xmit failed\n");
1627109323Ssam			sc->sc_txd[cur].d_len = 0;
1628109323Ssam		} else {
1629109323Ssam			sc->sc_tx_timer = 5;
1630109323Ssam			ifp->if_timer = 1;
1631109323Ssam		}
1632109323Ssam	}
163346492Swpaul}
163446492Swpaul
163588546Salfredstatic void
1636109323Ssamwi_info_intr(struct wi_softc *sc)
163794405Simp{
1638109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1639138571Ssam	struct ifnet *ifp = &sc->sc_if;
1640109323Ssam	int i, fid, len, off;
1641109323Ssam	u_int16_t ltbuf[2];
1642109323Ssam	u_int16_t stat;
1643109323Ssam	u_int32_t *ptr;
164494405Simp
1645109323Ssam	fid = CSR_READ_2(sc, WI_INFO_FID);
1646109323Ssam	wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
164794405Simp
1648109323Ssam	switch (le16toh(ltbuf[1])) {
164994405Simp
1650109323Ssam	case WI_INFO_LINK_STAT:
1651109323Ssam		wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1652109323Ssam		DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1653109323Ssam		switch (le16toh(stat)) {
1654109323Ssam		case WI_INFO_LINK_STAT_CONNECTED:
1655109323Ssam			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1656109323Ssam			if (ic->ic_state == IEEE80211_S_RUN &&
1657109323Ssam			    ic->ic_opmode != IEEE80211_M_IBSS)
1658109323Ssam				break;
1659109323Ssam			/* FALLTHROUGH */
1660109323Ssam		case WI_INFO_LINK_STAT_AP_CHG:
1661117812Ssam			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1662109323Ssam			break;
1663109323Ssam		case WI_INFO_LINK_STAT_AP_INR:
1664109323Ssam			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1665109323Ssam			break;
1666109323Ssam		case WI_INFO_LINK_STAT_AP_OOR:
1667109323Ssam			if (sc->sc_firmware_type == WI_SYMBOL &&
1668109323Ssam			    sc->sc_scan_timer > 0) {
1669109323Ssam				if (wi_cmd(sc, WI_CMD_INQUIRE,
1670109323Ssam				    WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
1671109323Ssam					sc->sc_scan_timer = 0;
1672109323Ssam				break;
1673109323Ssam			}
1674109323Ssam			if (ic->ic_opmode == IEEE80211_M_STA)
1675109323Ssam				sc->sc_flags |= WI_FLAGS_OUTRANGE;
1676109323Ssam			break;
1677109323Ssam		case WI_INFO_LINK_STAT_DISCONNECTED:
1678109323Ssam		case WI_INFO_LINK_STAT_ASSOC_FAILED:
1679109323Ssam			if (ic->ic_opmode == IEEE80211_M_STA)
1680117812Ssam				ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1681109323Ssam			break;
1682109323Ssam		}
1683109323Ssam		break;
168494405Simp
1685109323Ssam	case WI_INFO_COUNTERS:
1686109323Ssam		/* some card versions have a larger stats structure */
1687109323Ssam		len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1688109323Ssam		ptr = (u_int32_t *)&sc->sc_stats;
1689109323Ssam		off = sizeof(ltbuf);
1690109323Ssam		for (i = 0; i < len; i++, off += 2, ptr++) {
1691109323Ssam			wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1692109323Ssam#ifdef WI_HERMES_STATS_WAR
1693109323Ssam			if (stat & 0xf000)
1694109323Ssam				stat = ~stat;
1695109323Ssam#endif
1696109323Ssam			*ptr += stat;
1697109323Ssam		}
1698109323Ssam		ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1699109323Ssam		    sc->sc_stats.wi_tx_multi_retries +
1700109323Ssam		    sc->sc_stats.wi_tx_retry_limit;
1701109323Ssam		break;
1702109323Ssam
1703109323Ssam	case WI_INFO_SCAN_RESULTS:
1704109323Ssam	case WI_INFO_HOST_SCAN_RESULTS:
1705109323Ssam		wi_scan_result(sc, fid, le16toh(ltbuf[0]));
1706109323Ssam		break;
1707109323Ssam
1708109323Ssam	default:
1709109323Ssam		DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1710109323Ssam		    le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1711109323Ssam		break;
171294405Simp	}
1713109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1714109323Ssam}
171594405Simp
1716109323Ssamstatic int
1717109323Ssamwi_write_multi(struct wi_softc *sc)
1718109323Ssam{
1719138571Ssam	struct ifnet *ifp = &sc->sc_if;
1720109323Ssam	int n;
1721109323Ssam	struct ifmultiaddr *ifma;
1722109323Ssam	struct wi_mcast mlist;
172394472Simp
1724109323Ssam	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1725109323Ssamallmulti:
1726109323Ssam		memset(&mlist, 0, sizeof(mlist));
1727109323Ssam		return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1728109323Ssam		    sizeof(mlist));
172994405Simp	}
173094405Simp
1731109323Ssam	n = 0;
1732109323Ssam#if __FreeBSD_version < 500000
1733109323Ssam	LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1734109323Ssam#else
1735109323Ssam	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1736109323Ssam#endif
1737109323Ssam		if (ifma->ifma_addr->sa_family != AF_LINK)
1738109323Ssam			continue;
1739109323Ssam		if (n >= 16)
1740109323Ssam			goto allmulti;
1741109323Ssam		IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1742109323Ssam		    (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1743109323Ssam		n++;
174494405Simp	}
1745109323Ssam	return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1746109323Ssam	    IEEE80211_ADDR_LEN * n);
174794405Simp}
174894405Simp
174994405Simpstatic void
1750109323Ssamwi_read_nicid(struct wi_softc *sc)
175146492Swpaul{
1752109323Ssam	struct wi_card_ident *id;
1753109323Ssam	char *p;
1754109323Ssam	int len;
1755109323Ssam	u_int16_t ver[4];
175646492Swpaul
1757109323Ssam	/* getting chip identity */
1758109323Ssam	memset(ver, 0, sizeof(ver));
1759109323Ssam	len = sizeof(ver);
1760109323Ssam	wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1761109323Ssam	device_printf(sc->sc_dev, "using ");
176246492Swpaul
1763109323Ssam	sc->sc_firmware_type = WI_NOTYPE;
1764109323Ssam	for (id = wi_card_ident; id->card_name != NULL; id++) {
1765109323Ssam		if (le16toh(ver[0]) == id->card_id) {
1766109323Ssam			printf("%s", id->card_name);
1767109323Ssam			sc->sc_firmware_type = id->firm_type;
1768109323Ssam			break;
1769109323Ssam		}
177067092Swpaul	}
1771109323Ssam	if (sc->sc_firmware_type == WI_NOTYPE) {
1772109323Ssam		if (le16toh(ver[0]) & 0x8000) {
1773109323Ssam			printf("Unknown PRISM2 chip");
1774109323Ssam			sc->sc_firmware_type = WI_INTERSIL;
1775109323Ssam		} else {
1776109323Ssam			printf("Unknown Lucent chip");
1777109323Ssam			sc->sc_firmware_type = WI_LUCENT;
1778109323Ssam		}
177967092Swpaul	}
178046492Swpaul
1781109323Ssam	/* get primary firmware version (Only Prism chips) */
1782109323Ssam	if (sc->sc_firmware_type != WI_LUCENT) {
1783109323Ssam		memset(ver, 0, sizeof(ver));
1784109323Ssam		len = sizeof(ver);
1785109323Ssam		wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1786109323Ssam		sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1787109323Ssam		    le16toh(ver[3]) * 100 + le16toh(ver[1]);
178867092Swpaul	}
178946492Swpaul
1790109323Ssam	/* get station firmware version */
1791109323Ssam	memset(ver, 0, sizeof(ver));
1792109323Ssam	len = sizeof(ver);
1793109323Ssam	wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1794109323Ssam	sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1795109323Ssam	    le16toh(ver[3]) * 100 + le16toh(ver[1]);
1796109323Ssam	if (sc->sc_firmware_type == WI_INTERSIL &&
1797109323Ssam	    (sc->sc_sta_firmware_ver == 10102 ||
1798109323Ssam	     sc->sc_sta_firmware_ver == 20102)) {
1799109323Ssam		char ident[12];
1800109323Ssam		memset(ident, 0, sizeof(ident));
1801109323Ssam		len = sizeof(ident);
1802109323Ssam		/* value should be the format like "V2.00-11" */
1803109323Ssam		if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1804109323Ssam		    *(p = (char *)ident) >= 'A' &&
1805109323Ssam		    p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1806109323Ssam			sc->sc_firmware_type = WI_SYMBOL;
1807109323Ssam			sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1808109323Ssam			    (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1809109323Ssam			    (p[6] - '0') * 10 + (p[7] - '0');
181094405Simp		}
181194405Simp	}
1812109323Ssam	printf("\n");
1813109323Ssam	device_printf(sc->sc_dev, "%s Firmware: ",
1814109323Ssam	     sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
1815109323Ssam	    (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
1816109323Ssam	if (sc->sc_firmware_type != WI_LUCENT)	/* XXX */
1817109323Ssam		printf("Primary (%u.%u.%u), ",
1818109323Ssam		    sc->sc_pri_firmware_ver / 10000,
1819109323Ssam		    (sc->sc_pri_firmware_ver % 10000) / 100,
1820109323Ssam		    sc->sc_pri_firmware_ver % 100);
1821109323Ssam	printf("Station (%u.%u.%u)\n",
1822109323Ssam	    sc->sc_sta_firmware_ver / 10000,
1823109323Ssam	    (sc->sc_sta_firmware_ver % 10000) / 100,
1824109323Ssam	    sc->sc_sta_firmware_ver % 100);
1825109323Ssam}
182646492Swpaul
1827109323Ssamstatic int
1828109323Ssamwi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1829109323Ssam{
1830109323Ssam	struct wi_ssid ssid;
183146492Swpaul
1832109323Ssam	if (buflen > IEEE80211_NWID_LEN)
1833109323Ssam		return ENOBUFS;
1834109323Ssam	memset(&ssid, 0, sizeof(ssid));
1835109323Ssam	ssid.wi_len = htole16(buflen);
1836109323Ssam	memcpy(ssid.wi_ssid, buf, buflen);
1837109323Ssam	return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1838109323Ssam}
183946492Swpaul
1840109323Ssamstatic int
1841109323Ssamwi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
1842109323Ssam{
1843109323Ssam	struct wi_softc *sc = ifp->if_softc;
1844109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1845109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
1846109323Ssam	struct wi_req wreq;
1847116898Ssam	struct wi_scan_res *res;
1848122015Sgreen	size_t reslen;
1849116898Ssam	int len, n, error, mif, val, off, i;
185046492Swpaul
1851109323Ssam	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1852109323Ssam	if (error)
1853109323Ssam		return error;
1854109323Ssam	len = (wreq.wi_len - 1) * 2;
1855109323Ssam	if (len < sizeof(u_int16_t))
1856109323Ssam		return ENOSPC;
1857109323Ssam	if (len > sizeof(wreq.wi_val))
1858109323Ssam		len = sizeof(wreq.wi_val);
185946492Swpaul
1860109323Ssam	switch (wreq.wi_type) {
186146492Swpaul
1862109323Ssam	case WI_RID_IFACE_STATS:
1863109323Ssam		memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
1864109323Ssam		if (len < sizeof(sc->sc_stats))
1865109323Ssam			error = ENOSPC;
1866109323Ssam		else
1867109323Ssam			len = sizeof(sc->sc_stats);
1868109323Ssam		break;
186946492Swpaul
1870109323Ssam	case WI_RID_ENCRYPTION:
1871109323Ssam	case WI_RID_TX_CRYPT_KEY:
1872109323Ssam	case WI_RID_DEFLT_CRYPT_KEYS:
1873109323Ssam	case WI_RID_TX_RATE:
1874138571Ssam		return ieee80211_cfgget(ic, cmd, data);
187546492Swpaul
1876109323Ssam	case WI_RID_MICROWAVE_OVEN:
1877109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
1878109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1879109323Ssam			    &len);
1880109323Ssam			break;
1881109323Ssam		}
1882109323Ssam		wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
1883109323Ssam		len = sizeof(u_int16_t);
1884109323Ssam		break;
188546492Swpaul
1886109323Ssam	case WI_RID_DBM_ADJUST:
1887109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
1888109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1889109323Ssam			    &len);
1890109323Ssam			break;
1891109323Ssam		}
1892119784Ssam		wreq.wi_val[0] = htole16(sc->sc_dbm_offset);
1893109323Ssam		len = sizeof(u_int16_t);
1894109323Ssam		break;
189546492Swpaul
1896109323Ssam	case WI_RID_ROAMING_MODE:
1897109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
1898109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1899109323Ssam			    &len);
1900109323Ssam			break;
1901109323Ssam		}
1902109323Ssam		wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
1903109323Ssam		len = sizeof(u_int16_t);
1904109323Ssam		break;
190546492Swpaul
1906109323Ssam	case WI_RID_SYSTEM_SCALE:
1907109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
1908109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1909109323Ssam			    &len);
1910109323Ssam			break;
1911109323Ssam		}
1912109323Ssam		wreq.wi_val[0] = htole16(sc->sc_system_scale);
1913109323Ssam		len = sizeof(u_int16_t);
1914109323Ssam		break;
191546492Swpaul
1916109323Ssam	case WI_RID_FRAG_THRESH:
1917109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
1918109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1919109323Ssam			    &len);
1920109323Ssam			break;
1921109323Ssam		}
1922116951Ssam		wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
1923109323Ssam		len = sizeof(u_int16_t);
1924109323Ssam		break;
192546492Swpaul
1926109323Ssam	case WI_RID_READ_APS:
1927109323Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1928138571Ssam			return ieee80211_cfgget(ic, cmd, data);
1929109323Ssam		if (sc->sc_scan_timer > 0) {
1930109323Ssam			error = EINPROGRESS;
1931109323Ssam			break;
1932109323Ssam		}
1933109323Ssam		n = sc->sc_naps;
1934109323Ssam		if (len < sizeof(n)) {
1935109323Ssam			error = ENOSPC;
1936109323Ssam			break;
1937109323Ssam		}
1938109323Ssam		if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
1939109323Ssam			n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
1940109323Ssam		len = sizeof(n) + sizeof(struct wi_apinfo) * n;
1941109323Ssam		memcpy(wreq.wi_val, &n, sizeof(n));
1942109323Ssam		memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
1943109323Ssam		    sizeof(struct wi_apinfo) * n);
1944109323Ssam		break;
194546492Swpaul
1946109323Ssam	case WI_RID_PRISM2:
1947109323Ssam		wreq.wi_val[0] = sc->sc_firmware_type != WI_LUCENT;
1948109323Ssam		len = sizeof(u_int16_t);
1949109323Ssam		break;
195046492Swpaul
1951109323Ssam	case WI_RID_MIF:
1952109323Ssam		mif = wreq.wi_val[0];
1953109323Ssam		error = wi_cmd(sc, WI_CMD_READMIF, mif, 0, 0);
1954109323Ssam		val = CSR_READ_2(sc, WI_RESP0);
1955109323Ssam		wreq.wi_val[0] = val;
1956109323Ssam		len = sizeof(u_int16_t);
1957109323Ssam		break;
195846492Swpaul
1959109323Ssam	case WI_RID_ZERO_CACHE:
1960109323Ssam	case WI_RID_PROCFRAME:		/* ignore for compatibility */
1961109323Ssam		/* XXX ??? */
1962109323Ssam		break;
196346492Swpaul
1964109323Ssam	case WI_RID_READ_CACHE:
1965138571Ssam		return ieee80211_cfgget(ic, cmd, data);
196646492Swpaul
1967116898Ssam	case WI_RID_SCAN_RES:		/* compatibility interface */
1968116898Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1969138571Ssam			return ieee80211_cfgget(ic, cmd, data);
1970116898Ssam		if (sc->sc_scan_timer > 0) {
1971116898Ssam			error = EINPROGRESS;
1972116898Ssam			break;
1973116898Ssam		}
1974116898Ssam		n = sc->sc_naps;
1975122015Sgreen		if (sc->sc_firmware_type == WI_LUCENT) {
1976122015Sgreen			off = 0;
1977122015Sgreen			reslen = WI_WAVELAN_RES_SIZE;
1978122015Sgreen		} else {
1979122015Sgreen			off = sizeof(struct wi_scan_p2_hdr);
1980122015Sgreen			reslen = WI_PRISM2_RES_SIZE;
1981122015Sgreen		}
1982122015Sgreen		if (len < off + reslen * n)
1983122015Sgreen			n = (len - off) / reslen;
1984122015Sgreen		len = off + reslen * n;
1985116898Ssam		if (off != 0) {
1986116898Ssam			struct wi_scan_p2_hdr *p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1987116898Ssam			/*
1988116898Ssam			 * Prepend Prism-specific header.
1989116898Ssam			 */
1990116898Ssam			if (len < sizeof(struct wi_scan_p2_hdr)) {
1991116898Ssam				error = ENOSPC;
1992116898Ssam				break;
1993116898Ssam			}
1994116898Ssam			p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1995116898Ssam			p2->wi_rsvd = 0;
1996116898Ssam			p2->wi_reason = n;	/* XXX */
1997116898Ssam		}
1998122015Sgreen		for (i = 0; i < n; i++, off += reslen) {
1999116898Ssam			const struct wi_apinfo *ap = &sc->sc_aps[i];
2000116898Ssam
2001116898Ssam			res = (struct wi_scan_res *)((char *)wreq.wi_val + off);
2002116898Ssam			res->wi_chan = ap->channel;
2003116898Ssam			res->wi_noise = ap->noise;
2004116898Ssam			res->wi_signal = ap->signal;
2005116898Ssam			IEEE80211_ADDR_COPY(res->wi_bssid, ap->bssid);
2006116898Ssam			res->wi_interval = ap->interval;
2007116898Ssam			res->wi_capinfo = ap->capinfo;
2008116898Ssam			res->wi_ssid_len = ap->namelen;
2009116898Ssam			memcpy(res->wi_ssid, ap->name,
2010116898Ssam				IEEE80211_NWID_LEN);
2011116898Ssam			if (sc->sc_firmware_type != WI_LUCENT) {
2012116898Ssam				/* XXX not saved from Prism cards */
2013116898Ssam				memset(res->wi_srates, 0,
2014116898Ssam					sizeof(res->wi_srates));
2015116898Ssam				res->wi_rate = ap->rate;
2016116898Ssam				res->wi_rsvd = 0;
2017122015Sgreen			}
2018116898Ssam		}
2019116898Ssam		break;
2020116898Ssam
2021109323Ssam	default:
2022109323Ssam		if (sc->sc_enabled) {
2023109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2024109323Ssam			    &len);
2025109323Ssam			break;
2026109323Ssam		}
2027109323Ssam		switch (wreq.wi_type) {
2028109323Ssam		case WI_RID_MAX_DATALEN:
2029109323Ssam			wreq.wi_val[0] = htole16(sc->sc_max_datalen);
2030109323Ssam			len = sizeof(u_int16_t);
2031109323Ssam			break;
2032109323Ssam		case WI_RID_RTS_THRESH:
2033116951Ssam			wreq.wi_val[0] = htole16(ic->ic_rtsthreshold);
2034109323Ssam			len = sizeof(u_int16_t);
2035109323Ssam			break;
2036109323Ssam		case WI_RID_CNFAUTHMODE:
2037109323Ssam			wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
2038109323Ssam			len = sizeof(u_int16_t);
2039109323Ssam			break;
2040109323Ssam		case WI_RID_NODENAME:
2041109323Ssam			if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
2042109323Ssam				error = ENOSPC;
2043109323Ssam				break;
2044109323Ssam			}
2045109323Ssam			len = sc->sc_nodelen + sizeof(u_int16_t);
2046109323Ssam			wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
2047109323Ssam			memcpy(&wreq.wi_val[1], sc->sc_nodename,
2048109323Ssam			    sc->sc_nodelen);
2049109323Ssam			break;
2050109323Ssam		default:
2051138571Ssam			return ieee80211_cfgget(ic, cmd, data);
2052109323Ssam		}
2053109323Ssam		break;
205446492Swpaul	}
2055109323Ssam	if (error)
2056109323Ssam		return error;
2057109323Ssam	wreq.wi_len = (len + 1) / 2 + 1;
2058109323Ssam	return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
205946492Swpaul}
206046492Swpaul
2061109323Ssamstatic int
2062109323Ssamwi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
206346492Swpaul{
2064109323Ssam	struct wi_softc *sc = ifp->if_softc;
2065109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2066109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
2067109323Ssam	struct wi_req wreq;
2068109323Ssam	struct mbuf *m;
2069109323Ssam	int i, len, error, mif, val;
2070116951Ssam	struct ieee80211_rateset *rs;
2071138571Ssam	WI_LOCK_DECL();
207246492Swpaul
2073109323Ssam	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2074109323Ssam	if (error)
2075109323Ssam		return error;
2076116898Ssam	len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0;
2077109323Ssam	switch (wreq.wi_type) {
2078109323Ssam	case WI_RID_DBM_ADJUST:
2079109323Ssam		return ENODEV;
208067092Swpaul
2081109323Ssam	case WI_RID_NODENAME:
2082109323Ssam		if (le16toh(wreq.wi_val[0]) * 2 > len ||
2083109323Ssam		    le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
2084109323Ssam			error = ENOSPC;
2085109323Ssam			break;
2086109323Ssam		}
2087138571Ssam		WI_LOCK(sc);
2088138571Ssam		if (sc->sc_enabled)
2089109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2090109323Ssam			    len);
2091138571Ssam		if (error == 0) {
2092138571Ssam			sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
2093138571Ssam			memcpy(sc->sc_nodename, &wreq.wi_val[1],
2094138571Ssam				sc->sc_nodelen);
2095109323Ssam		}
2096138571Ssam		WI_UNLOCK(sc);
2097109323Ssam		break;
209846492Swpaul
2099109323Ssam	case WI_RID_MICROWAVE_OVEN:
2100109323Ssam	case WI_RID_ROAMING_MODE:
2101109323Ssam	case WI_RID_SYSTEM_SCALE:
2102109323Ssam	case WI_RID_FRAG_THRESH:
2103138571Ssam		/* XXX unlocked reads */
2104109323Ssam		if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
2105109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
2106109323Ssam			break;
2107109323Ssam		if (wreq.wi_type == WI_RID_ROAMING_MODE &&
2108109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
2109109323Ssam			break;
2110109323Ssam		if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
2111109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
2112109323Ssam			break;
2113109323Ssam		if (wreq.wi_type == WI_RID_FRAG_THRESH &&
2114109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
2115109323Ssam			break;
2116109323Ssam		/* FALLTHROUGH */
2117109323Ssam	case WI_RID_RTS_THRESH:
2118109323Ssam	case WI_RID_CNFAUTHMODE:
2119109323Ssam	case WI_RID_MAX_DATALEN:
2120138571Ssam		WI_LOCK(sc);
2121109323Ssam		if (sc->sc_enabled) {
2122109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2123109323Ssam			    sizeof(u_int16_t));
2124138571Ssam			if (error != 0) {
2125138571Ssam				WI_UNLOCK(sc);
2126109323Ssam				break;
2127138571Ssam			}
2128109323Ssam		}
2129109323Ssam		switch (wreq.wi_type) {
2130109323Ssam		case WI_RID_FRAG_THRESH:
2131116951Ssam			ic->ic_fragthreshold = le16toh(wreq.wi_val[0]);
2132109323Ssam			break;
2133109323Ssam		case WI_RID_RTS_THRESH:
2134116951Ssam			ic->ic_rtsthreshold = le16toh(wreq.wi_val[0]);
2135109323Ssam			break;
2136109323Ssam		case WI_RID_MICROWAVE_OVEN:
2137109323Ssam			sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
2138109323Ssam			break;
2139109323Ssam		case WI_RID_ROAMING_MODE:
2140109323Ssam			sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
2141109323Ssam			break;
2142109323Ssam		case WI_RID_SYSTEM_SCALE:
2143109323Ssam			sc->sc_system_scale = le16toh(wreq.wi_val[0]);
2144109323Ssam			break;
2145109323Ssam		case WI_RID_CNFAUTHMODE:
2146109323Ssam			sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
2147109323Ssam			break;
2148109323Ssam		case WI_RID_MAX_DATALEN:
2149109323Ssam			sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
2150109323Ssam			break;
2151109323Ssam		}
2152138571Ssam		WI_UNLOCK(sc);
2153109323Ssam		break;
215494405Simp
2155109323Ssam	case WI_RID_TX_RATE:
2156138571Ssam		WI_LOCK(sc);
2157109323Ssam		switch (le16toh(wreq.wi_val[0])) {
2158109323Ssam		case 3:
2159109323Ssam			ic->ic_fixed_rate = -1;
2160109323Ssam			break;
2161109323Ssam		default:
2162116951Ssam			rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
2163116951Ssam			for (i = 0; i < rs->rs_nrates; i++) {
2164116951Ssam				if ((rs->rs_rates[i] & IEEE80211_RATE_VAL)
2165109323Ssam				    / 2 == le16toh(wreq.wi_val[0]))
2166109323Ssam					break;
2167109323Ssam			}
2168138571Ssam			if (i == rs->rs_nrates) {
2169138571Ssam				WI_UNLOCK(sc);
2170109323Ssam				return EINVAL;
2171138571Ssam			}
2172109323Ssam			ic->ic_fixed_rate = i;
2173109323Ssam		}
2174109323Ssam		if (sc->sc_enabled)
2175109323Ssam			error = wi_write_txrate(sc);
2176138571Ssam		WI_UNLOCK(sc);
2177109323Ssam		break;
217846492Swpaul
2179109323Ssam	case WI_RID_SCAN_APS:
2180138571Ssam		WI_LOCK(sc);
2181109323Ssam		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2182116898Ssam			error = wi_scan_ap(sc, 0x3fff, 0x000f);
2183138571Ssam		WI_UNLOCK(sc);
2184109323Ssam		break;
218546492Swpaul
2186116898Ssam	case WI_RID_SCAN_REQ:		/* compatibility interface */
2187138571Ssam		WI_LOCK(sc);
2188116898Ssam		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2189116898Ssam			error = wi_scan_ap(sc, wreq.wi_val[0], wreq.wi_val[1]);
2190138571Ssam		WI_UNLOCK(sc);
2191116898Ssam		break;
2192116898Ssam
2193109323Ssam	case WI_RID_MGMT_XMIT:
2194138571Ssam		WI_LOCK(sc);
2195138571Ssam		if (!sc->sc_enabled)
2196109323Ssam			error = ENETDOWN;
2197138571Ssam		else if (ic->ic_mgtq.ifq_len > 5)
2198109323Ssam			error = EAGAIN;
2199138571Ssam		else {
2200138571Ssam			/* NB: m_devget uses M_DONTWAIT so can hold the lock */
2201138571Ssam			/* XXX wi_len looks in u_int8_t, not in u_int16_t */
2202138571Ssam			m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0,
2203138571Ssam				ifp, NULL);
2204138571Ssam			if (m != NULL)
2205138571Ssam				IF_ENQUEUE(&ic->ic_mgtq, m);
2206138571Ssam			else
2207138571Ssam				error = ENOMEM;
2208109323Ssam		}
2209138571Ssam		WI_UNLOCK(sc);
2210109323Ssam		break;
221146492Swpaul
2212109323Ssam	case WI_RID_MIF:
2213109323Ssam		mif = wreq.wi_val[0];
2214109323Ssam		val = wreq.wi_val[1];
2215138571Ssam		WI_LOCK(sc);
2216109323Ssam		error = wi_cmd(sc, WI_CMD_WRITEMIF, mif, val, 0);
2217138571Ssam		WI_UNLOCK(sc);
2218109323Ssam		break;
221946492Swpaul
2220109323Ssam	case WI_RID_PROCFRAME:		/* ignore for compatibility */
2221109323Ssam		break;
2222109323Ssam
2223116898Ssam	case WI_RID_OWN_SSID:
2224116898Ssam		if (le16toh(wreq.wi_val[0]) * 2 > len ||
2225116898Ssam		    le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) {
2226116898Ssam			error = ENOSPC;
2227116898Ssam			break;
2228116898Ssam		}
2229138571Ssam		WI_LOCK(sc);
2230116898Ssam		memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
2231116898Ssam		ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2;
2232116898Ssam		memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen);
2233138571Ssam		if (sc->sc_enabled)
2234138571Ssam			wi_init(sc);	/* XXX no error return */
2235138571Ssam		WI_UNLOCK(sc);
2236116898Ssam		break;
2237116898Ssam
2238109323Ssam	default:
2239138571Ssam		WI_LOCK(sc);
2240138571Ssam		if (sc->sc_enabled)
2241109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2242109323Ssam			    len);
2243138571Ssam		if (error == 0) {
2244138571Ssam			/* XXX ieee80211_cfgset does a copyin */
2245138571Ssam			error = ieee80211_cfgset(ic, cmd, data);
2246138571Ssam			if (error == ENETRESET) {
2247138571Ssam				if (sc->sc_enabled)
2248138571Ssam					wi_init(sc);
2249138571Ssam				error = 0;
2250138571Ssam			}
2251109323Ssam		}
2252138571Ssam		WI_UNLOCK(sc);
2253109323Ssam		break;
2254109323Ssam	}
2255109323Ssam	return error;
225646492Swpaul}
225746492Swpaul
2258109323Ssamstatic int
2259109323Ssamwi_write_txrate(struct wi_softc *sc)
226046492Swpaul{
2261109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2262109323Ssam	int i;
2263109323Ssam	u_int16_t rate;
226446492Swpaul
2265109323Ssam	if (ic->ic_fixed_rate < 0)
2266109323Ssam		rate = 0;	/* auto */
2267109323Ssam	else
2268116951Ssam		rate = (ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ic->ic_fixed_rate] &
2269109323Ssam		    IEEE80211_RATE_VAL) / 2;
227046492Swpaul
2271109323Ssam	/* rate: 0, 1, 2, 5, 11 */
227246492Swpaul
2273109323Ssam	switch (sc->sc_firmware_type) {
2274109323Ssam	case WI_LUCENT:
2275112501Simp		switch (rate) {
2276112501Simp		case 0:			/* auto == 11mbps auto */
2277112501Simp			rate = 3;
2278112501Simp			break;
2279112501Simp		/* case 1, 2 map to 1, 2*/
2280112501Simp		case 5:			/* 5.5Mbps -> 4 */
2281112501Simp			rate = 4;
2282112501Simp			break;
2283112501Simp		case 11:		/* 11mbps -> 5 */
2284112501Simp			rate = 5;
2285112501Simp			break;
2286112501Simp		default:
2287112501Simp			break;
2288112501Simp		}
2289109323Ssam		break;
2290109323Ssam	default:
2291109323Ssam		/* Choose a bit according to this table.
2292109323Ssam		 *
2293109323Ssam		 * bit | data rate
2294109323Ssam		 * ----+-------------------
2295109323Ssam		 * 0   | 1Mbps
2296109323Ssam		 * 1   | 2Mbps
2297109323Ssam		 * 2   | 5.5Mbps
2298109323Ssam		 * 3   | 11Mbps
2299109323Ssam		 */
2300109323Ssam		for (i = 8; i > 0; i >>= 1) {
2301109323Ssam			if (rate >= i)
2302109323Ssam				break;
2303109323Ssam		}
2304109323Ssam		if (i == 0)
2305109323Ssam			rate = 0xf;	/* auto */
2306109323Ssam		else
2307109323Ssam			rate = i;
2308109323Ssam		break;
2309109323Ssam	}
2310109323Ssam	return wi_write_val(sc, WI_RID_TX_RATE, rate);
2311109323Ssam}
231246492Swpaul
2313109323Ssamstatic int
2314109323Ssamwi_write_wep(struct wi_softc *sc)
2315109323Ssam{
2316109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2317109323Ssam	int error = 0;
2318109323Ssam	int i, keylen;
2319109323Ssam	u_int16_t val;
2320109323Ssam	struct wi_key wkey[IEEE80211_WEP_NKID];
232146492Swpaul
2322109323Ssam	switch (sc->sc_firmware_type) {
2323109323Ssam	case WI_LUCENT:
2324138571Ssam		val = (ic->ic_flags & IEEE80211_F_PRIVACY) ? 1 : 0;
2325109323Ssam		error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
2326109323Ssam		if (error)
2327109323Ssam			break;
2328138988Smdodd		if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0)
2329138988Smdodd			break;
2330138988Smdodd		error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_def_txkey);
2331138988Smdodd		if (error)
2332138988Smdodd			break;
2333138988Smdodd		memset(wkey, 0, sizeof(wkey));
2334138988Smdodd		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2335138988Smdodd			keylen = ic->ic_nw_keys[i].wk_keylen;
2336138988Smdodd			wkey[i].wi_keylen = htole16(keylen);
2337138988Smdodd			memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
2338138988Smdodd			    keylen);
2339109323Ssam		}
2340138988Smdodd		error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
2341138988Smdodd		    wkey, sizeof(wkey));
2342109323Ssam		break;
2343109323Ssam
2344109323Ssam	case WI_INTERSIL:
2345109323Ssam	case WI_SYMBOL:
2346138571Ssam		if (ic->ic_flags & IEEE80211_F_PRIVACY) {
2347109323Ssam			/*
2348109323Ssam			 * ONLY HWB3163 EVAL-CARD Firmware version
2349109323Ssam			 * less than 0.8 variant2
2350109323Ssam			 *
2351109323Ssam			 *   If promiscuous mode disable, Prism2 chip
2352109323Ssam			 *  does not work with WEP .
2353109323Ssam			 * It is under investigation for details.
2354109323Ssam			 * (ichiro@netbsd.org)
2355109323Ssam			 */
2356109323Ssam			if (sc->sc_firmware_type == WI_INTERSIL &&
2357109323Ssam			    sc->sc_sta_firmware_ver < 802 ) {
2358109323Ssam				/* firm ver < 0.8 variant 2 */
2359109323Ssam				wi_write_val(sc, WI_RID_PROMISC, 1);
2360109323Ssam			}
2361109323Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE,
2362109323Ssam			    sc->sc_cnfauthmode);
2363109323Ssam			val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
2364109323Ssam			/*
2365109323Ssam			 * Encryption firmware has a bug for HostAP mode.
2366109323Ssam			 */
2367109323Ssam			if (sc->sc_firmware_type == WI_INTERSIL &&
2368109323Ssam			    ic->ic_opmode == IEEE80211_M_HOSTAP)
2369109323Ssam				val |= HOST_ENCRYPT;
2370109323Ssam		} else {
2371109323Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE,
2372109323Ssam			    IEEE80211_AUTH_OPEN);
2373109323Ssam			val = HOST_ENCRYPT | HOST_DECRYPT;
2374109323Ssam		}
2375109323Ssam		error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
2376109323Ssam		if (error)
2377109323Ssam			break;
2378138988Smdodd		if ((val & PRIVACY_INVOKED) == 0)
2379138988Smdodd			break;
2380138988Smdodd		error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
2381138988Smdodd		    ic->ic_def_txkey);
2382138988Smdodd		if (error)
2383138988Smdodd			break;
2384138988Smdodd		if (val & HOST_DECRYPT)
2385138988Smdodd			break;
2386138988Smdodd		/*
2387138988Smdodd		 * It seems that the firmware accept 104bit key only if
2388138988Smdodd		 * all the keys have 104bit length.  We get the length of
2389138988Smdodd		 * the transmit key and use it for all other keys.
2390138988Smdodd		 * Perhaps we should use software WEP for such situation.
2391138988Smdodd		 */
2392138988Smdodd		if (ic->ic_def_txkey != IEEE80211_KEYIX_NONE)
2393138988Smdodd			keylen = ic->ic_nw_keys[ic->ic_def_txkey].wk_keylen;
2394138988Smdodd		else	/* XXX should not hapen */
2395138988Smdodd			keylen = IEEE80211_WEP_KEYLEN;
2396138988Smdodd		if (keylen > IEEE80211_WEP_KEYLEN)
2397138988Smdodd			keylen = 13;	/* 104bit keys */
2398138988Smdodd		else
2399138988Smdodd			keylen = IEEE80211_WEP_KEYLEN;
2400138988Smdodd		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2401138988Smdodd			error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
2402138988Smdodd			    ic->ic_nw_keys[i].wk_key, keylen);
2403109323Ssam			if (error)
2404109323Ssam				break;
2405109323Ssam		}
2406109323Ssam		break;
2407109323Ssam	}
2408109323Ssam	return error;
240946492Swpaul}
241046492Swpaul
2411109323Ssamstatic int
2412109323Ssamwi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
241346492Swpaul{
2414109323Ssam	int			i, s = 0;
2415109323Ssam	static volatile int count  = 0;
2416109323Ssam
2417123098Simp	if (sc->wi_gone)
2418123098Simp		return (ENODEV);
2419123098Simp
2420109323Ssam	if (count > 0)
2421109323Ssam		panic("Hey partner, hold on there!");
2422109323Ssam	count++;
242353702Swpaul
2424109323Ssam	/* wait for the busy bit to clear */
2425123339Simp	for (i = sc->wi_cmd_count; i > 0; i--) {	/* 500ms */
2426116206Simp		if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
2427109323Ssam			break;
2428123098Simp		DELAY(1*1000);	/* 1ms */
2429109323Ssam	}
2430109323Ssam	if (i == 0) {
2431109323Ssam		device_printf(sc->sc_dev, "wi_cmd: busy bit won't clear.\n" );
2432123098Simp		sc->wi_gone = 1;
2433109323Ssam		count--;
2434109323Ssam		return(ETIMEDOUT);
2435109323Ssam	}
243690580Sbrooks
2437109323Ssam	CSR_WRITE_2(sc, WI_PARAM0, val0);
2438109323Ssam	CSR_WRITE_2(sc, WI_PARAM1, val1);
2439109323Ssam	CSR_WRITE_2(sc, WI_PARAM2, val2);
2440109323Ssam	CSR_WRITE_2(sc, WI_COMMAND, cmd);
244190580Sbrooks
2442109323Ssam	if (cmd == WI_CMD_INI) {
2443109323Ssam		/* XXX: should sleep here. */
2444116206Simp		DELAY(100*1000);		/* 100ms delay for init */
2445109323Ssam	}
2446109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
2447109323Ssam		/*
2448109323Ssam		 * Wait for 'command complete' bit to be
2449109323Ssam		 * set in the event status register.
2450109323Ssam		 */
2451109323Ssam		s = CSR_READ_2(sc, WI_EVENT_STAT);
2452109323Ssam		if (s & WI_EV_CMD) {
2453109323Ssam			/* Ack the event and read result code. */
2454109323Ssam			s = CSR_READ_2(sc, WI_STATUS);
2455109323Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
2456109323Ssam			if (s & WI_STAT_CMD_RESULT) {
2457109323Ssam				count--;
2458109323Ssam				return(EIO);
2459109323Ssam			}
2460109323Ssam			break;
246190580Sbrooks		}
2462109323Ssam		DELAY(WI_DELAY);
2463109323Ssam	}
246490580Sbrooks
2465109323Ssam	count--;
2466109323Ssam	if (i == WI_TIMEOUT) {
2467109323Ssam		device_printf(sc->sc_dev,
2468109323Ssam		    "timeout in wi_cmd 0x%04x; event status 0x%04x\n", cmd, s);
2469123098Simp		if (s == 0xffff)
2470123098Simp			sc->wi_gone = 1;
2471109323Ssam		return(ETIMEDOUT);
247253702Swpaul	}
2473109323Ssam	return (0);
2474109323Ssam}
247553702Swpaul
2476109323Ssamstatic int
2477109323Ssamwi_seek_bap(struct wi_softc *sc, int id, int off)
2478109323Ssam{
2479109323Ssam	int i, status;
248090580Sbrooks
2481109323Ssam	CSR_WRITE_2(sc, WI_SEL0, id);
2482109323Ssam	CSR_WRITE_2(sc, WI_OFF0, off);
248390580Sbrooks
2484109323Ssam	for (i = 0; ; i++) {
2485109323Ssam		status = CSR_READ_2(sc, WI_OFF0);
2486109323Ssam		if ((status & WI_OFF_BUSY) == 0)
2487109323Ssam			break;
2488109323Ssam		if (i == WI_TIMEOUT) {
2489109323Ssam			device_printf(sc->sc_dev, "timeout in wi_seek to %x/%x\n",
2490109323Ssam			    id, off);
2491109323Ssam			sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2492123098Simp			if (status == 0xffff)
2493123098Simp				sc->wi_gone = 1;
2494109323Ssam			return ETIMEDOUT;
2495109323Ssam		}
2496109323Ssam		DELAY(1);
249753702Swpaul	}
2498109323Ssam	if (status & WI_OFF_ERR) {
2499109323Ssam		device_printf(sc->sc_dev, "failed in wi_seek to %x/%x\n", id, off);
2500109323Ssam		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2501109323Ssam		return EIO;
2502109323Ssam	}
2503109323Ssam	sc->sc_bap_id = id;
2504109323Ssam	sc->sc_bap_off = off;
2505109323Ssam	return 0;
250653702Swpaul}
250753702Swpaul
2508109323Ssamstatic int
2509109323Ssamwi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
251053702Swpaul{
2511109323Ssam	u_int16_t *ptr;
2512109323Ssam	int i, error, cnt;
251353702Swpaul
2514109323Ssam	if (buflen == 0)
2515109323Ssam		return 0;
2516109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2517109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
2518109323Ssam			return error;
251975219Salfred	}
2520109323Ssam	cnt = (buflen + 1) / 2;
2521109323Ssam	ptr = (u_int16_t *)buf;
2522109323Ssam	for (i = 0; i < cnt; i++)
2523109323Ssam		*ptr++ = CSR_READ_2(sc, WI_DATA0);
2524109323Ssam	sc->sc_bap_off += cnt * 2;
2525109323Ssam	return 0;
252653702Swpaul}
252753702Swpaul
2528109323Ssamstatic int
2529109323Ssamwi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
253053702Swpaul{
2531109323Ssam	u_int16_t *ptr;
2532109323Ssam	int i, error, cnt;
253346492Swpaul
2534109323Ssam	if (buflen == 0)
2535109323Ssam		return 0;
253646492Swpaul
2537109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
2538109323Ssam  again:
2539109323Ssam#endif
2540109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2541109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
2542109323Ssam			return error;
2543109323Ssam	}
2544109323Ssam	cnt = (buflen + 1) / 2;
2545109323Ssam	ptr = (u_int16_t *)buf;
2546109323Ssam	for (i = 0; i < cnt; i++)
2547109323Ssam		CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
2548109323Ssam	sc->sc_bap_off += cnt * 2;
2549109323Ssam
2550109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
2551109323Ssam	/*
2552109323Ssam	 * According to the comments in the HCF Light code, there is a bug
2553109323Ssam	 * in the Hermes (or possibly in certain Hermes firmware revisions)
2554109323Ssam	 * where the chip's internal autoincrement counter gets thrown off
2555109323Ssam	 * during data writes:  the autoincrement is missed, causing one
2556109323Ssam	 * data word to be overwritten and subsequent words to be written to
2557109323Ssam	 * the wrong memory locations. The end result is that we could end
2558109323Ssam	 * up transmitting bogus frames without realizing it. The workaround
2559109323Ssam	 * for this is to write a couple of extra guard words after the end
2560109323Ssam	 * of the transfer, then attempt to read then back. If we fail to
2561109323Ssam	 * locate the guard words where we expect them, we preform the
2562109323Ssam	 * transfer over again.
2563109323Ssam	 */
2564109323Ssam	if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
2565109323Ssam		CSR_WRITE_2(sc, WI_DATA0, 0x1234);
2566109323Ssam		CSR_WRITE_2(sc, WI_DATA0, 0x5678);
2567109323Ssam		wi_seek_bap(sc, id, sc->sc_bap_off);
2568109323Ssam		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2569109323Ssam		if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
2570109323Ssam		    CSR_READ_2(sc, WI_DATA0) != 0x5678) {
2571109323Ssam			device_printf(sc->sc_dev,
2572109323Ssam				"detect auto increment bug, try again\n");
2573109323Ssam			goto again;
2574109323Ssam		}
2575109323Ssam	}
2576109323Ssam#endif
2577109323Ssam	return 0;
257846492Swpaul}
257953702Swpaul
2580109323Ssamstatic int
2581109323Ssamwi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
2582109323Ssam{
2583109323Ssam	int error, len;
2584109323Ssam	struct mbuf *m;
258553702Swpaul
2586109323Ssam	for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
2587109323Ssam		if (m->m_len == 0)
2588109323Ssam			continue;
258953702Swpaul
2590109323Ssam		len = min(m->m_len, totlen);
259153702Swpaul
2592109323Ssam		if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
2593109323Ssam			m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
2594109323Ssam			return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
2595109323Ssam			    totlen);
2596109323Ssam		}
259753702Swpaul
2598109323Ssam		if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
2599109323Ssam			return error;
260053702Swpaul
2601109323Ssam		off += m->m_len;
2602109323Ssam		totlen -= len;
2603109323Ssam	}
2604109323Ssam	return 0;
2605109323Ssam}
260653702Swpaul
2607109323Ssamstatic int
2608109323Ssamwi_alloc_fid(struct wi_softc *sc, int len, int *idp)
260953702Swpaul{
261053702Swpaul	int i;
261153702Swpaul
2612109323Ssam	if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
2613109323Ssam		device_printf(sc->sc_dev, "failed to allocate %d bytes on NIC\n",
2614109323Ssam		    len);
2615109323Ssam		return ENOMEM;
261653702Swpaul	}
261753702Swpaul
2618109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
2619109323Ssam		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
2620109323Ssam			break;
2621109323Ssam		DELAY(1);
262253702Swpaul	}
2623144167Ssam	if (i == WI_TIMEOUT) {
2624144167Ssam		device_printf(sc->sc_dev, "timeout in alloc\n");
2625144167Ssam		return ETIMEDOUT;
2626144167Ssam	}
2627109323Ssam	*idp = CSR_READ_2(sc, WI_ALLOC_FID);
2628109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
2629109323Ssam	return 0;
2630109323Ssam}
263153702Swpaul
2632109323Ssamstatic int
2633109323Ssamwi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
2634109323Ssam{
2635109323Ssam	int error, len;
2636109323Ssam	u_int16_t ltbuf[2];
263753702Swpaul
2638109323Ssam	/* Tell the NIC to enter record read mode. */
2639109323Ssam	error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2640109323Ssam	if (error)
2641109323Ssam		return error;
264253702Swpaul
2643109323Ssam	error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2644109323Ssam	if (error)
2645109323Ssam		return error;
264653702Swpaul
2647109323Ssam	if (le16toh(ltbuf[1]) != rid) {
2648109323Ssam		device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n",
2649109323Ssam		    rid, le16toh(ltbuf[1]));
2650109323Ssam		return EIO;
265153702Swpaul	}
2652109323Ssam	len = (le16toh(ltbuf[0]) - 1) * 2;	 /* already got rid */
2653109323Ssam	if (*buflenp < len) {
2654109323Ssam		device_printf(sc->sc_dev, "record buffer is too small, "
2655109323Ssam		    "rid=%x, size=%d, len=%d\n",
2656109323Ssam		    rid, *buflenp, len);
2657109323Ssam		return ENOSPC;
265853702Swpaul	}
2659109323Ssam	*buflenp = len;
2660109323Ssam	return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2661109323Ssam}
266253702Swpaul
2663109323Ssamstatic int
2664109323Ssamwi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2665109323Ssam{
2666109323Ssam	int error;
2667109323Ssam	u_int16_t ltbuf[2];
266853702Swpaul
2669109323Ssam	ltbuf[0] = htole16((buflen + 1) / 2 + 1);	 /* includes rid */
2670109323Ssam	ltbuf[1] = htole16(rid);
267153702Swpaul
2672109323Ssam	error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2673109323Ssam	if (error)
2674109323Ssam		return error;
2675109323Ssam	error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2676109323Ssam	if (error)
2677109323Ssam		return error;
2678102206Simp
2679109323Ssam	return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
268053702Swpaul}
268177217Sphk
268288546Salfredstatic int
2683117812Ssamwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
268477217Sphk{
2685138571Ssam	struct ifnet *ifp = ic->ic_ifp;
2686117812Ssam	struct wi_softc *sc = ifp->if_softc;
2687139542Ssam	struct ieee80211_node *ni;
2688116951Ssam	int buflen;
2689109323Ssam	u_int16_t val;
2690109323Ssam	struct wi_ssid ssid;
2691109323Ssam	u_int8_t old_bssid[IEEE80211_ADDR_LEN];
269277217Sphk
2693117812Ssam	DPRINTF(("%s: %s -> %s\n", __func__,
2694117812Ssam		ieee80211_state_name[ic->ic_state],
2695117812Ssam		ieee80211_state_name[nstate]));
2696109323Ssam
2697139542Ssam	/*
2698139542Ssam	 * Internal to the driver the INIT and RUN states are used
2699139542Ssam	 * so bypass the net80211 state machine for other states.
2700139542Ssam	 * Beware however that this requires use to net80211 state
2701139542Ssam	 * management that otherwise would be handled for us.
2702139542Ssam	 */
2703109323Ssam	switch (nstate) {
2704109323Ssam	case IEEE80211_S_INIT:
2705109323Ssam		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2706117812Ssam		return (*sc->sc_newstate)(ic, nstate, arg);
2707109323Ssam
2708138571Ssam	case IEEE80211_S_SCAN:
2709138571Ssam	case IEEE80211_S_AUTH:
2710138571Ssam	case IEEE80211_S_ASSOC:
2711138571Ssam		ic->ic_state = nstate;	/* NB: skip normal ieee80211 handling */
2712138571Ssam		break;
2713138571Ssam
2714109323Ssam	case IEEE80211_S_RUN:
2715139542Ssam		ni = ic->ic_bss;
2716109323Ssam		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2717109323Ssam		buflen = IEEE80211_ADDR_LEN;
2718138571Ssam		IEEE80211_ADDR_COPY(old_bssid, ni->ni_bssid);
2719109323Ssam		wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen);
2720109323Ssam		IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
2721109323Ssam		buflen = sizeof(val);
2722109323Ssam		wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
2723116951Ssam		/* XXX validate channel */
2724116951Ssam		ni->ni_chan = &ic->ic_channels[le16toh(val)];
2725138951Ssam		ic->ic_ibss_chan = ni->ni_chan;
2726119784Ssam#if NBPFILTER > 0
2727119784Ssam		sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
2728119784Ssam			htole16(ni->ni_chan->ic_freq);
2729119784Ssam		sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
2730119784Ssam			htole16(ni->ni_chan->ic_flags);
2731119784Ssam#endif
2732139542Ssam		if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
2733138952Ssam			/*
2734138952Ssam			 * XXX hack; unceremoniously clear
2735138952Ssam			 * IEEE80211_F_DROPUNENC when operating with
2736138952Ssam			 * wep enabled so we don't drop unencoded frames
2737138952Ssam			 * at the 802.11 layer.  This is necessary because
2738138952Ssam			 * we must strip the WEP bit from the 802.11 header
2739138952Ssam			 * before passing frames to ieee80211_input because
2740138952Ssam			 * the card has already stripped the WEP crypto
2741138952Ssam			 * header from the packet.
2742138952Ssam			 */
2743138952Ssam			if (ic->ic_flags & IEEE80211_F_PRIVACY)
2744138952Ssam				ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
2745109323Ssam			/* XXX check return value */
2746109323Ssam			buflen = sizeof(ssid);
2747109323Ssam			wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
2748109323Ssam			ni->ni_esslen = le16toh(ssid.wi_len);
2749109323Ssam			if (ni->ni_esslen > IEEE80211_NWID_LEN)
2750109323Ssam				ni->ni_esslen = IEEE80211_NWID_LEN;	/*XXX*/
2751109323Ssam			memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
275277217Sphk		}
2753138571Ssam		return (*sc->sc_newstate)(ic, nstate, arg);
275477217Sphk	}
2755117812Ssam	return 0;
275677217Sphk}
275777217Sphk
275888546Salfredstatic int
2759116898Ssamwi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate)
276077217Sphk{
2761109323Ssam	int error = 0;
2762109323Ssam	u_int16_t val[2];
276377217Sphk
2764109323Ssam	if (!sc->sc_enabled)
2765109323Ssam		return ENXIO;
2766109323Ssam	switch (sc->sc_firmware_type) {
2767109323Ssam	case WI_LUCENT:
2768109323Ssam		(void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
276998440Simp		break;
2770109323Ssam	case WI_INTERSIL:
2771138571Ssam		val[0] = htole16(chanmask);	/* channel */
2772138571Ssam		val[1] = htole16(txrate);	/* tx rate */
2773109323Ssam		error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
277498440Simp		break;
2775109323Ssam	case WI_SYMBOL:
2776109323Ssam		/*
2777109323Ssam		 * XXX only supported on 3.x ?
2778109323Ssam		 */
2779109323Ssam		val[0] = BSCAN_BCAST | BSCAN_ONETIME;
2780109323Ssam		error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
2781109323Ssam		    val, sizeof(val[0]));
278298440Simp		break;
278398440Simp	}
2784109323Ssam	if (error == 0) {
2785109323Ssam		sc->sc_scan_timer = WI_SCAN_WAIT;
2786138571Ssam		sc->sc_if.if_timer = 1;
2787116898Ssam		DPRINTF(("wi_scan_ap: start scanning, "
2788116898Ssam			"chamask 0x%x txrate 0x%x\n", chanmask, txrate));
2789109323Ssam	}
2790109323Ssam	return error;
2791109323Ssam}
279277217Sphk
2793109323Ssamstatic void
2794109323Ssamwi_scan_result(struct wi_softc *sc, int fid, int cnt)
2795109323Ssam{
2796109323Ssam#define	N(a)	(sizeof (a) / sizeof (a[0]))
2797109323Ssam	int i, naps, off, szbuf;
2798109323Ssam	struct wi_scan_header ws_hdr;	/* Prism2 header */
2799109323Ssam	struct wi_scan_data_p2 ws_dat;	/* Prism2 scantable*/
2800109323Ssam	struct wi_apinfo *ap;
2801109323Ssam
2802109323Ssam	off = sizeof(u_int16_t) * 2;
2803109323Ssam	memset(&ws_hdr, 0, sizeof(ws_hdr));
2804109323Ssam	switch (sc->sc_firmware_type) {
2805109323Ssam	case WI_INTERSIL:
2806109323Ssam		wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
2807109323Ssam		off += sizeof(ws_hdr);
2808109323Ssam		szbuf = sizeof(struct wi_scan_data_p2);
280977217Sphk		break;
2810109323Ssam	case WI_SYMBOL:
2811109323Ssam		szbuf = sizeof(struct wi_scan_data_p2) + 6;
281277217Sphk		break;
2813109323Ssam	case WI_LUCENT:
2814109323Ssam		szbuf = sizeof(struct wi_scan_data);
281577217Sphk		break;
2816109323Ssam	default:
2817109323Ssam		device_printf(sc->sc_dev,
2818109323Ssam			"wi_scan_result: unknown firmware type %u\n",
2819109323Ssam			sc->sc_firmware_type);
2820109323Ssam		naps = 0;
2821109323Ssam		goto done;
282277217Sphk	}
2823109323Ssam	naps = (cnt * 2 + 2 - off) / szbuf;
2824109323Ssam	if (naps > N(sc->sc_aps))
2825109323Ssam		naps = N(sc->sc_aps);
2826109323Ssam	sc->sc_naps = naps;
2827109323Ssam	/* Read Data */
2828109323Ssam	ap = sc->sc_aps;
2829109323Ssam	memset(&ws_dat, 0, sizeof(ws_dat));
2830109323Ssam	for (i = 0; i < naps; i++, ap++) {
2831109323Ssam		wi_read_bap(sc, fid, off, &ws_dat,
2832109323Ssam		    (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
2833109323Ssam		DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off,
2834109323Ssam		    ether_sprintf(ws_dat.wi_bssid)));
2835109323Ssam		off += szbuf;
2836109323Ssam		ap->scanreason = le16toh(ws_hdr.wi_reason);
2837109323Ssam		memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
2838109323Ssam		ap->channel = le16toh(ws_dat.wi_chid);
2839109323Ssam		ap->signal  = le16toh(ws_dat.wi_signal);
2840109323Ssam		ap->noise   = le16toh(ws_dat.wi_noise);
2841109323Ssam		ap->quality = ap->signal - ap->noise;
2842109323Ssam		ap->capinfo = le16toh(ws_dat.wi_capinfo);
2843109323Ssam		ap->interval = le16toh(ws_dat.wi_interval);
2844109323Ssam		ap->rate    = le16toh(ws_dat.wi_rate);
2845109323Ssam		ap->namelen = le16toh(ws_dat.wi_namelen);
2846109323Ssam		if (ap->namelen > sizeof(ap->name))
2847109323Ssam			ap->namelen = sizeof(ap->name);
2848109323Ssam		memcpy(ap->name, ws_dat.wi_name, ap->namelen);
2849109323Ssam	}
2850109323Ssamdone:
2851109323Ssam	/* Done scanning */
2852109323Ssam	sc->sc_scan_timer = 0;
2853109323Ssam	DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
2854109323Ssam#undef N
285577217Sphk}
285677217Sphk
285788546Salfredstatic void
2858109323Ssamwi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi)
285977217Sphk{
2860109323Ssam	ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr),
2861116951Ssam	    ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL : -1, rssi);
2862109323Ssam	printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n",
2863109323Ssam		le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1),
2864109323Ssam		le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence);
2865109323Ssam	printf(" rx_signal %u rx_rate %u rx_flow %u\n",
2866109323Ssam		wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow);
2867109323Ssam	printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n",
2868109323Ssam		wh->wi_tx_rtry, wh->wi_tx_rate,
2869109323Ssam		le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len));
2870109323Ssam	printf(" ehdr dst %6D src %6D type 0x%x\n",
2871109323Ssam		wh->wi_ehdr.ether_dhost, ":", wh->wi_ehdr.ether_shost, ":",
2872109323Ssam		wh->wi_ehdr.ether_type);
2873109323Ssam}
287477217Sphk
2875109323Ssamint
2876109323Ssamwi_alloc(device_t dev, int rid)
2877109323Ssam{
2878109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2879109323Ssam
2880109323Ssam	if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2881109323Ssam		sc->iobase_rid = rid;
2882109323Ssam		sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2883109323Ssam		    &sc->iobase_rid, 0, ~0, (1 << 6),
2884109323Ssam		    rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2885109323Ssam		if (!sc->iobase) {
2886109323Ssam			device_printf(dev, "No I/O space?!\n");
2887109323Ssam			return (ENXIO);
288898440Simp		}
2889109323Ssam
2890109323Ssam		sc->wi_io_addr = rman_get_start(sc->iobase);
2891109323Ssam		sc->wi_btag = rman_get_bustag(sc->iobase);
2892109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2893109323Ssam	} else {
2894109323Ssam		sc->mem_rid = rid;
2895127135Snjl		sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2896127135Snjl		    &sc->mem_rid, RF_ACTIVE);
2897109323Ssam
2898109323Ssam		if (!sc->mem) {
2899109323Ssam			device_printf(dev, "No Mem space on prism2.5?\n");
2900109323Ssam			return (ENXIO);
290177217Sphk		}
2902109323Ssam
2903109323Ssam		sc->wi_btag = rman_get_bustag(sc->mem);
2904109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->mem);
290577217Sphk	}
290677217Sphk
2907109323Ssam
2908109323Ssam	sc->irq_rid = 0;
2909127135Snjl	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2910127135Snjl	    RF_ACTIVE |
2911109323Ssam	    ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2912109323Ssam
2913109323Ssam	if (!sc->irq) {
2914109323Ssam		wi_free(dev);
2915109323Ssam		device_printf(dev, "No irq?!\n");
2916109323Ssam		return (ENXIO);
291777217Sphk	}
2918109323Ssam
2919109323Ssam	sc->sc_dev = dev;
2920109323Ssam	sc->sc_unit = device_get_unit(dev);
2921109323Ssam
2922109323Ssam	return (0);
292377217Sphk}
292493359Simp
2925109323Ssamvoid
2926109323Ssamwi_free(device_t dev)
2927109323Ssam{
2928109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2929109323Ssam
2930109323Ssam	if (sc->iobase != NULL) {
2931109323Ssam		bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2932109323Ssam		sc->iobase = NULL;
2933109323Ssam	}
2934109323Ssam	if (sc->irq != NULL) {
2935109323Ssam		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2936109323Ssam		sc->irq = NULL;
2937109323Ssam	}
2938109323Ssam	if (sc->mem != NULL) {
2939109323Ssam		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
2940109323Ssam		sc->mem = NULL;
2941109323Ssam	}
2942109323Ssam
2943109323Ssam	return;
2944109323Ssam}
2945109323Ssam
294693359Simpstatic int
2947109323Ssamwi_get_debug(struct wi_softc *sc, struct wi_req *wreq)
294893359Simp{
2949109323Ssam	int error = 0;
295093359Simp
295193359Simp	wreq->wi_len = 1;
295293359Simp
295393359Simp	switch (wreq->wi_type) {
295493359Simp	case WI_DEBUG_SLEEP:
295593359Simp		wreq->wi_len++;
295693359Simp		wreq->wi_val[0] = sc->wi_debug.wi_sleep;
295793359Simp		break;
295893359Simp	case WI_DEBUG_DELAYSUPP:
295993359Simp		wreq->wi_len++;
296093359Simp		wreq->wi_val[0] = sc->wi_debug.wi_delaysupp;
296193359Simp		break;
296293359Simp	case WI_DEBUG_TXSUPP:
296393359Simp		wreq->wi_len++;
296493359Simp		wreq->wi_val[0] = sc->wi_debug.wi_txsupp;
296593359Simp		break;
296693359Simp	case WI_DEBUG_MONITOR:
296793359Simp		wreq->wi_len++;
296893359Simp		wreq->wi_val[0] = sc->wi_debug.wi_monitor;
296993359Simp		break;
297093359Simp	case WI_DEBUG_LEDTEST:
297193359Simp		wreq->wi_len += 3;
297293359Simp		wreq->wi_val[0] = sc->wi_debug.wi_ledtest;
297393359Simp		wreq->wi_val[1] = sc->wi_debug.wi_ledtest_param0;
297493359Simp		wreq->wi_val[2] = sc->wi_debug.wi_ledtest_param1;
297593359Simp		break;
297693359Simp	case WI_DEBUG_CONTTX:
297793359Simp		wreq->wi_len += 2;
297893359Simp		wreq->wi_val[0] = sc->wi_debug.wi_conttx;
297993359Simp		wreq->wi_val[1] = sc->wi_debug.wi_conttx_param0;
298093359Simp		break;
298193359Simp	case WI_DEBUG_CONTRX:
298293359Simp		wreq->wi_len++;
298393359Simp		wreq->wi_val[0] = sc->wi_debug.wi_contrx;
298493359Simp		break;
298593359Simp	case WI_DEBUG_SIGSTATE:
298693359Simp		wreq->wi_len += 2;
298793359Simp		wreq->wi_val[0] = sc->wi_debug.wi_sigstate;
298893359Simp		wreq->wi_val[1] = sc->wi_debug.wi_sigstate_param0;
298993359Simp		break;
299093359Simp	case WI_DEBUG_CONFBITS:
299193359Simp		wreq->wi_len += 2;
299293359Simp		wreq->wi_val[0] = sc->wi_debug.wi_confbits;
299393359Simp		wreq->wi_val[1] = sc->wi_debug.wi_confbits_param0;
299493359Simp		break;
299593359Simp	default:
299693359Simp		error = EIO;
299793359Simp		break;
299893359Simp	}
299993359Simp
300093359Simp	return (error);
300193359Simp}
300293359Simp
300393359Simpstatic int
3004109323Ssamwi_set_debug(struct wi_softc *sc, struct wi_req *wreq)
300593359Simp{
3006109323Ssam	int error = 0;
300793359Simp	u_int16_t		cmd, param0 = 0, param1 = 0;
300893359Simp
300993359Simp	switch (wreq->wi_type) {
301093359Simp	case WI_DEBUG_RESET:
301193359Simp	case WI_DEBUG_INIT:
301293359Simp	case WI_DEBUG_CALENABLE:
301393359Simp		break;
301493359Simp	case WI_DEBUG_SLEEP:
301593359Simp		sc->wi_debug.wi_sleep = 1;
301693359Simp		break;
301793359Simp	case WI_DEBUG_WAKE:
301893359Simp		sc->wi_debug.wi_sleep = 0;
301993359Simp		break;
302093359Simp	case WI_DEBUG_CHAN:
302193359Simp		param0 = wreq->wi_val[0];
302293359Simp		break;
302393359Simp	case WI_DEBUG_DELAYSUPP:
302493359Simp		sc->wi_debug.wi_delaysupp = 1;
302593359Simp		break;
302693359Simp	case WI_DEBUG_TXSUPP:
302793359Simp		sc->wi_debug.wi_txsupp = 1;
302893359Simp		break;
302993359Simp	case WI_DEBUG_MONITOR:
303093359Simp		sc->wi_debug.wi_monitor = 1;
303193359Simp		break;
303293359Simp	case WI_DEBUG_LEDTEST:
303393359Simp		param0 = wreq->wi_val[0];
303493359Simp		param1 = wreq->wi_val[1];
303593359Simp		sc->wi_debug.wi_ledtest = 1;
303693359Simp		sc->wi_debug.wi_ledtest_param0 = param0;
303793359Simp		sc->wi_debug.wi_ledtest_param1 = param1;
303893359Simp		break;
303993359Simp	case WI_DEBUG_CONTTX:
304093359Simp		param0 = wreq->wi_val[0];
304193359Simp		sc->wi_debug.wi_conttx = 1;
304293359Simp		sc->wi_debug.wi_conttx_param0 = param0;
304393359Simp		break;
304493359Simp	case WI_DEBUG_STOPTEST:
304593359Simp		sc->wi_debug.wi_delaysupp = 0;
304693359Simp		sc->wi_debug.wi_txsupp = 0;
304793359Simp		sc->wi_debug.wi_monitor = 0;
304893359Simp		sc->wi_debug.wi_ledtest = 0;
304993359Simp		sc->wi_debug.wi_ledtest_param0 = 0;
305093359Simp		sc->wi_debug.wi_ledtest_param1 = 0;
305193359Simp		sc->wi_debug.wi_conttx = 0;
305293359Simp		sc->wi_debug.wi_conttx_param0 = 0;
305393359Simp		sc->wi_debug.wi_contrx = 0;
305493359Simp		sc->wi_debug.wi_sigstate = 0;
305593359Simp		sc->wi_debug.wi_sigstate_param0 = 0;
305693359Simp		break;
305793359Simp	case WI_DEBUG_CONTRX:
305893359Simp		sc->wi_debug.wi_contrx = 1;
305993359Simp		break;
306093359Simp	case WI_DEBUG_SIGSTATE:
306193359Simp		param0 = wreq->wi_val[0];
306293359Simp		sc->wi_debug.wi_sigstate = 1;
306393359Simp		sc->wi_debug.wi_sigstate_param0 = param0;
306493359Simp		break;
306593359Simp	case WI_DEBUG_CONFBITS:
306693359Simp		param0 = wreq->wi_val[0];
306793359Simp		param1 = wreq->wi_val[1];
306893359Simp		sc->wi_debug.wi_confbits = param0;
306993359Simp		sc->wi_debug.wi_confbits_param0 = param1;
307093359Simp		break;
307193359Simp	default:
307293359Simp		error = EIO;
307393359Simp		break;
307493359Simp	}
307593359Simp
307693359Simp	if (error)
307793359Simp		return (error);
307893359Simp
307993359Simp	cmd = WI_CMD_DEBUG | (wreq->wi_type << 8);
308093359Simp	error = wi_cmd(sc, cmd, param0, param1, 0);
308193359Simp
308293359Simp	return (error);
308393359Simp}
3084101903Simp
3085105076Simp#if __FreeBSD_version >= 500000
3086101903Simp/*
3087101903Simp * Special routines to download firmware for Symbol CF card.
3088101903Simp * XXX: This should be modified generic into any PRISM-2 based card.
3089101903Simp */
3090101903Simp
3091101903Simp#define	WI_SBCF_PDIADDR		0x3100
3092101903Simp
3093101903Simp/* unaligned load little endian */
3094101903Simp#define	GETLE32(p)	((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24))
3095101903Simp#define	GETLE16(p)	((p)[0] | ((p)[1]<<8))
3096101903Simp
3097101903Simpint
3098101903Simpwi_symbol_load_firm(struct wi_softc *sc, const void *primsym, int primlen,
3099101903Simp    const void *secsym, int seclen)
3100101903Simp{
3101101903Simp	uint8_t ebuf[256];
3102101903Simp	int i;
3103101903Simp
3104101903Simp	/* load primary code and run it */
3105101903Simp	wi_symbol_set_hcr(sc, WI_HCR_EEHOLD);
3106101903Simp	if (wi_symbol_write_firm(sc, primsym, primlen, NULL, 0))
3107101903Simp		return EIO;
3108101903Simp	wi_symbol_set_hcr(sc, WI_HCR_RUN);
3109101903Simp	for (i = 0; ; i++) {
3110101903Simp		if (i == 10)
3111101903Simp			return ETIMEDOUT;
3112101903Simp		tsleep(sc, PWAIT, "wiinit", 1);
3113101903Simp		if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT)
3114101903Simp			break;
3115101903Simp		/* write the magic key value to unlock aux port */
3116101903Simp		CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0);
3117101903Simp		CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1);
3118101903Simp		CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2);
3119101903Simp		CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL);
3120101903Simp	}
3121101903Simp
3122101903Simp	/* issue read EEPROM command: XXX copied from wi_cmd() */
3123101903Simp	CSR_WRITE_2(sc, WI_PARAM0, 0);
3124101903Simp	CSR_WRITE_2(sc, WI_PARAM1, 0);
3125101903Simp	CSR_WRITE_2(sc, WI_PARAM2, 0);
3126101903Simp	CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE);
3127101903Simp        for (i = 0; i < WI_TIMEOUT; i++) {
3128101903Simp                if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
3129101903Simp                        break;
3130101903Simp                DELAY(1);
3131101903Simp        }
3132101903Simp        CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
3133101903Simp
3134101903Simp	CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ);
3135101903Simp	CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ);
3136101903Simp	CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA,
3137101903Simp	    (uint16_t *)ebuf, sizeof(ebuf) / 2);
3138101903Simp	if (GETLE16(ebuf) > sizeof(ebuf))
3139101903Simp		return EIO;
3140101903Simp	if (wi_symbol_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf)))
3141101903Simp		return EIO;
3142101903Simp	return 0;
3143101903Simp}
3144101903Simp
3145101903Simpstatic int
3146101903Simpwi_symbol_write_firm(struct wi_softc *sc, const void *buf, int buflen,
3147101903Simp    const void *ebuf, int ebuflen)
3148101903Simp{
3149101903Simp	const uint8_t *p, *ep, *q, *eq;
3150101903Simp	char *tp;
3151101903Simp	uint32_t addr, id, eid;
3152101903Simp	int i, len, elen, nblk, pdrlen;
3153101903Simp
3154101903Simp	/*
3155101903Simp	 * Parse the header of the firmware image.
3156101903Simp	 */
3157101903Simp	p = buf;
3158101903Simp	ep = p + buflen;
3159101903Simp	while (p < ep && *p++ != ' ');	/* FILE: */
3160101903Simp	while (p < ep && *p++ != ' ');	/* filename */
3161101903Simp	while (p < ep && *p++ != ' ');	/* type of the firmware */
3162101903Simp	nblk = strtoul(p, &tp, 10);
3163101903Simp	p = tp;
3164101903Simp	pdrlen = strtoul(p + 1, &tp, 10);
3165101903Simp	p = tp;
3166101903Simp	while (p < ep && *p++ != 0x1a);	/* skip rest of header */
3167101903Simp
3168101903Simp	/*
3169101903Simp	 * Block records: address[4], length[2], data[length];
3170101903Simp	 */
3171101903Simp	for (i = 0; i < nblk; i++) {
3172101903Simp		addr = GETLE32(p);	p += 4;
3173101903Simp		len  = GETLE16(p);	p += 2;
3174101903Simp		CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3175101903Simp		CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3176101903Simp		CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3177101903Simp		    (const uint16_t *)p, len / 2);
3178101903Simp		p += len;
3179101903Simp	}
3180101903Simp
3181101903Simp	/*
3182101903Simp	 * PDR: id[4], address[4], length[4];
3183101903Simp	 */
3184101903Simp	for (i = 0; i < pdrlen; ) {
3185101903Simp		id   = GETLE32(p);	p += 4; i += 4;
3186101903Simp		addr = GETLE32(p);	p += 4; i += 4;
3187101903Simp		len  = GETLE32(p);	p += 4; i += 4;
3188101903Simp		/* replace PDR entry with the values from EEPROM, if any */
3189101903Simp		for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) {
3190101903Simp			elen = GETLE16(q);	q += 2;
3191101903Simp			eid  = GETLE16(q);	q += 2;
3192101903Simp			elen--;		/* elen includes eid */
3193101903Simp			if (eid == 0)
3194101903Simp				break;
3195101903Simp			if (eid != id)
3196101903Simp				continue;
3197101903Simp			CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3198101903Simp			CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3199101903Simp			CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3200101903Simp			    (const uint16_t *)q, len / 2);
3201101903Simp			break;
3202101903Simp		}
3203101903Simp	}
3204101903Simp	return 0;
3205101903Simp}
3206101903Simp
3207101903Simpstatic int
3208101903Simpwi_symbol_set_hcr(struct wi_softc *sc, int mode)
3209101903Simp{
3210101903Simp	uint16_t hcr;
3211101903Simp
3212101903Simp	CSR_WRITE_2(sc, WI_COR, WI_COR_RESET);
3213101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3214101903Simp	hcr = CSR_READ_2(sc, WI_HCR);
3215101903Simp	hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE);
3216101903Simp	CSR_WRITE_2(sc, WI_HCR, hcr);
3217101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3218101903Simp	CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE);
3219101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3220101903Simp	return 0;
3221101903Simp}
3222105076Simp#endif
3223