if_wi.c revision 123098
1109323Ssam/*	$NetBSD: wi.c,v 1.109 2003/01/09 08:52:19 dyoung Exp $	*/
2109323Ssam
346492Swpaul/*
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 123098 2003-12-02 04:59:59Z imp $");
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
117109323Ssam#define IF_POLL(ifq, m)		((m) = (ifq)->ifq_head)
118109323Ssam#define	IFQ_POLL(ifq, m)	IF_POLL((ifq), (m))
119109323Ssam#define IFQ_DEQUEUE(ifq, m)	IF_DEQUEUE((ifq), (m))
120109323Ssam
12191693Simpstatic void wi_start(struct ifnet *);
122109323Ssamstatic int  wi_reset(struct wi_softc *);
12391693Simpstatic void wi_watchdog(struct ifnet *);
124109323Ssamstatic int  wi_ioctl(struct ifnet *, u_long, caddr_t);
125109323Ssamstatic int  wi_media_change(struct ifnet *);
126109323Ssamstatic void wi_media_status(struct ifnet *, struct ifmediareq *);
12746492Swpaul
128109323Ssamstatic void wi_rx_intr(struct wi_softc *);
129109323Ssamstatic void wi_tx_intr(struct wi_softc *);
130109323Ssamstatic void wi_tx_ex_intr(struct wi_softc *);
131109323Ssamstatic void wi_info_intr(struct wi_softc *);
13246492Swpaul
133109323Ssamstatic int  wi_get_cfg(struct ifnet *, u_long, caddr_t);
134109323Ssamstatic int  wi_set_cfg(struct ifnet *, u_long, caddr_t);
135109323Ssamstatic int  wi_write_txrate(struct wi_softc *);
136109323Ssamstatic int  wi_write_wep(struct wi_softc *);
137109323Ssamstatic int  wi_write_multi(struct wi_softc *);
138109323Ssamstatic int  wi_alloc_fid(struct wi_softc *, int, int *);
139109323Ssamstatic void wi_read_nicid(struct wi_softc *);
140109323Ssamstatic int  wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
14153702Swpaul
142109323Ssamstatic int  wi_cmd(struct wi_softc *, int, int, int, int);
143109323Ssamstatic int  wi_seek_bap(struct wi_softc *, int, int);
144109323Ssamstatic int  wi_read_bap(struct wi_softc *, int, int, void *, int);
145109323Ssamstatic int  wi_write_bap(struct wi_softc *, int, int, void *, int);
146109323Ssamstatic int  wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
147109323Ssamstatic int  wi_read_rid(struct wi_softc *, int, void *, int *);
148109323Ssamstatic int  wi_write_rid(struct wi_softc *, int, void *, int);
14977217Sphk
150117812Ssamstatic int  wi_newstate(struct ieee80211com *, enum ieee80211_state, int);
151109323Ssam
152116898Ssamstatic int  wi_scan_ap(struct wi_softc *, u_int16_t, u_int16_t);
153109323Ssamstatic void wi_scan_result(struct wi_softc *, int, int);
154109323Ssam
155109323Ssamstatic void wi_dump_pkt(struct wi_frame *, struct ieee80211_node *, int rssi);
156109323Ssam
15793359Simpstatic int wi_get_debug(struct wi_softc *, struct wi_req *);
15893359Simpstatic int wi_set_debug(struct wi_softc *, struct wi_req *);
15993359Simp
160105076Simp#if __FreeBSD_version >= 500000
161101903Simp/* support to download firmware for symbol CF card */
162101903Simpstatic int wi_symbol_write_firm(struct wi_softc *, const void *, int,
163101903Simp		const void *, int);
164101903Simpstatic int wi_symbol_set_hcr(struct wi_softc *, int);
165105076Simp#endif
166101903Simp
167109323Ssamstatic __inline int
168109323Ssamwi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
169109323Ssam{
17053702Swpaul
171109323Ssam	val = htole16(val);
172109323Ssam	return wi_write_rid(sc, rid, &val, sizeof(val));
173109323Ssam}
174109323Ssam
175109592SsamSYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters");
176109592Ssam
177109323Ssamstatic	struct timeval lasttxerror;	/* time of last tx error msg */
178109323Ssamstatic	int curtxeps;			/* current tx error msgs/sec */
179111559Ssamstatic	int wi_txerate = 0;		/* tx error rate: max msgs/sec */
180109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate,
181111559Ssam	    0, "max tx error msgs/sec; 0 to disable msgs");
182109323Ssam
183109323Ssam#define	WI_DEBUG
184109323Ssam#ifdef WI_DEBUG
185109323Ssamstatic	int wi_debug = 0;
186109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug,
187109592Ssam	    0, "control debugging printfs");
188109323Ssam
189109323Ssam#define	DPRINTF(X)	if (wi_debug) printf X
190109323Ssam#define	DPRINTF2(X)	if (wi_debug > 1) printf X
191109323Ssam#define	IFF_DUMPPKTS(_ifp) \
192109323Ssam	(((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
193109323Ssam#else
194109323Ssam#define	DPRINTF(X)
195109323Ssam#define	DPRINTF2(X)
196109323Ssam#define	IFF_DUMPPKTS(_ifp)	0
197109323Ssam#endif
198109323Ssam
199109323Ssam#define WI_INTRS	(WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
200109323Ssam
20193825Simpstruct wi_card_ident wi_card_ident[] = {
20293825Simp	/* CARD_ID			CARD_NAME		FIRM_TYPE */
20393825Simp	{ WI_NIC_LUCENT_ID,		WI_NIC_LUCENT_STR,	WI_LUCENT },
20493825Simp	{ WI_NIC_SONY_ID,		WI_NIC_SONY_STR,	WI_LUCENT },
20593825Simp	{ WI_NIC_LUCENT_EMB_ID,		WI_NIC_LUCENT_EMB_STR,	WI_LUCENT },
20693825Simp	{ WI_NIC_EVB2_ID,		WI_NIC_EVB2_STR,	WI_INTERSIL },
20793825Simp	{ WI_NIC_HWB3763_ID,		WI_NIC_HWB3763_STR,	WI_INTERSIL },
20893825Simp	{ WI_NIC_HWB3163_ID,		WI_NIC_HWB3163_STR,	WI_INTERSIL },
20993825Simp	{ WI_NIC_HWB3163B_ID,		WI_NIC_HWB3163B_STR,	WI_INTERSIL },
21093825Simp	{ WI_NIC_EVB3_ID,		WI_NIC_EVB3_STR,	WI_INTERSIL },
21193825Simp	{ WI_NIC_HWB1153_ID,		WI_NIC_HWB1153_STR,	WI_INTERSIL },
21293825Simp	{ WI_NIC_P2_SST_ID,		WI_NIC_P2_SST_STR,	WI_INTERSIL },
21393825Simp	{ WI_NIC_EVB2_SST_ID,		WI_NIC_EVB2_SST_STR,	WI_INTERSIL },
21493825Simp	{ WI_NIC_3842_EVA_ID,		WI_NIC_3842_EVA_STR,	WI_INTERSIL },
21593825Simp	{ WI_NIC_3842_PCMCIA_AMD_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
21693825Simp	{ WI_NIC_3842_PCMCIA_SST_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
217101355Simp	{ WI_NIC_3842_PCMCIA_ATL_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
218101355Simp	{ WI_NIC_3842_PCMCIA_ATS_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
21993825Simp	{ WI_NIC_3842_MINI_AMD_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
22093825Simp	{ WI_NIC_3842_MINI_SST_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
221101355Simp	{ WI_NIC_3842_MINI_ATL_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
222101355Simp	{ WI_NIC_3842_MINI_ATS_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
22393825Simp	{ WI_NIC_3842_PCI_AMD_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
22493825Simp	{ WI_NIC_3842_PCI_SST_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
225101355Simp	{ WI_NIC_3842_PCI_ATS_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
226101355Simp	{ WI_NIC_3842_PCI_ATL_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
22793825Simp	{ WI_NIC_P3_PCMCIA_AMD_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
22893825Simp	{ WI_NIC_P3_PCMCIA_SST_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
229101355Simp	{ WI_NIC_P3_PCMCIA_ATL_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
230101355Simp	{ WI_NIC_P3_PCMCIA_ATS_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
23193825Simp	{ WI_NIC_P3_MINI_AMD_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
23293825Simp	{ WI_NIC_P3_MINI_SST_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
233101355Simp	{ WI_NIC_P3_MINI_ATL_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
234101355Simp	{ WI_NIC_P3_MINI_ATS_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
23593825Simp	{ 0,	NULL,	0 },
23693825Simp};
23793825Simp
238109323Ssamdevclass_t wi_devclass;
23946492Swpaul
24093611Simpint
241109323Ssamwi_attach(device_t dev)
24274906Salfred{
243109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
244109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
245109323Ssam	struct ifnet *ifp = &ic->ic_if;
246116951Ssam	int i, nrates, buflen;
247109323Ssam	u_int16_t val;
248109323Ssam	u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
249116951Ssam	struct ieee80211_rateset *rs;
250109323Ssam	static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
251109323Ssam		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
252109323Ssam	};
253109323Ssam	int error;
25474906Salfred
255109323Ssam	/*
256109323Ssam	 * NB: no locking is needed here; don't put it here
257109323Ssam	 *     unless you can prove it!
258109323Ssam	 */
259121697Ssam	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
26074998Swpaul	    wi_intr, sc, &sc->wi_intrhand);
26153702Swpaul
26253702Swpaul	if (error) {
26353702Swpaul		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
26453702Swpaul		wi_free(dev);
26553702Swpaul		return (error);
26653702Swpaul	}
26753702Swpaul
26895534Simp#if __FreeBSD_version >= 500000
269109323Ssam	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
27093818Sjhb	    MTX_DEF | MTX_RECURSE);
27195534Simp#endif
27267092Swpaul
27346492Swpaul	/* Reset the NIC. */
274112096Simp	if (wi_reset(sc) != 0)
275109323Ssam		return ENXIO;		/* XXX */
27646492Swpaul
27776438Swpaul	/*
27876438Swpaul	 * Read the station address.
27976438Swpaul	 * And do it twice. I've seen PRISM-based cards that return
28076438Swpaul	 * an error when trying to read it the first time, which causes
28176438Swpaul	 * the probe to fail.
28276438Swpaul	 */
283109323Ssam	buflen = IEEE80211_ADDR_LEN;
284109323Ssam	error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
285109323Ssam	if (error != 0) {
286109323Ssam		buflen = IEEE80211_ADDR_LEN;
287109323Ssam		error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
288109323Ssam	}
289109323Ssam	if (error || IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) {
290109323Ssam		if (error != 0)
291109323Ssam			device_printf(dev, "mac read failed %d\n", error);
292109323Ssam		else
293109323Ssam			device_printf(dev, "mac read failed (all zeros)\n");
29475149Simp		wi_free(dev);
29575149Simp		return (error);
29675149Simp	}
297109323Ssam	device_printf(dev, "802.11 address: %6D\n", ic->ic_myaddr, ":");
29846492Swpaul
299109323Ssam	/* Read NIC identification */
300109323Ssam	wi_read_nicid(sc);
30146492Swpaul
30246492Swpaul	ifp->if_softc = sc;
303121816Sbrooks	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
30446492Swpaul	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
30546492Swpaul	ifp->if_ioctl = wi_ioctl;
30646492Swpaul	ifp->if_start = wi_start;
30746492Swpaul	ifp->if_watchdog = wi_watchdog;
30846492Swpaul	ifp->if_init = wi_init;
30946492Swpaul	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
31046492Swpaul
311109323Ssam	ic->ic_phytype = IEEE80211_T_DS;
312109323Ssam	ic->ic_opmode = IEEE80211_M_STA;
313116951Ssam	ic->ic_caps = IEEE80211_C_PMGT | IEEE80211_C_AHDEMO;
314109323Ssam	ic->ic_state = IEEE80211_S_INIT;
31546492Swpaul
316116951Ssam	/*
317116951Ssam	 * Query the card for available channels and setup the
318116951Ssam	 * channel table.  We assume these are all 11b channels.
319116951Ssam	 */
320109323Ssam	buflen = sizeof(val);
321109323Ssam	if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
322109323Ssam		val = htole16(0x1fff);	/* assume 1-11 */
323116951Ssam	KASSERT(val != 0, ("wi_attach: no available channels listed!"));
324116951Ssam
325116951Ssam	val <<= 1;			/* shift for base 1 indices */
326116951Ssam	for (i = 1; i < 16; i++) {
327116951Ssam		if (isset((u_int8_t*)&val, i)) {
328116951Ssam			ic->ic_channels[i].ic_freq =
329116951Ssam				ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
330116951Ssam			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
331116951Ssam		}
332109323Ssam	}
33346492Swpaul
33446563Swpaul	/*
33546563Swpaul	 * Read the default channel from the NIC. This may vary
33646563Swpaul	 * depending on the country where the NIC was purchased, so
33746563Swpaul	 * we can't hard-code a default and expect it to work for
33846563Swpaul	 * everyone.
339116951Ssam	 *
340116951Ssam	 * If no channel is specified, let the 802.11 code select.
34146563Swpaul	 */
342109323Ssam	buflen = sizeof(val);
343116951Ssam	if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0) {
344116951Ssam		val = le16toh(val);
345116951Ssam		KASSERT(val < IEEE80211_CHAN_MAX &&
346116951Ssam			ic->ic_channels[val].ic_flags != 0,
347116951Ssam			("wi_attach: invalid own channel %u!", val));
348116951Ssam		ic->ic_ibss_chan = &ic->ic_channels[val];
349117042Ssam	} else {
350117042Ssam		device_printf(dev,
351117042Ssam			"WI_RID_OWN_CHNL failed, using first channel!\n");
352117042Ssam		ic->ic_ibss_chan = &ic->ic_channels[0];
353109323Ssam	}
35446563Swpaul
35556965Swpaul	/*
35698440Simp	 * Set flags based on firmware version.
35798440Simp	 */
35898440Simp	switch (sc->sc_firmware_type) {
35998440Simp	case WI_LUCENT:
360112363Simp		sc->sc_ntxbuf = 1;
361109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
362109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
363109323Ssam		/* XXX: not confirmed, but never seen for recent firmware */
364109323Ssam		if (sc->sc_sta_firmware_ver <  40000) {
365109323Ssam			sc->sc_flags |= WI_FLAGS_BUG_AUTOINC;
366109323Ssam		}
367109323Ssam#endif
36898440Simp		if (sc->sc_sta_firmware_ver >= 60000)
369109323Ssam			sc->sc_flags |= WI_FLAGS_HAS_MOR;
370117855Ssam		if (sc->sc_sta_firmware_ver >= 60006) {
371116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
372117855Ssam			ic->ic_caps |= IEEE80211_C_MONITOR;
373117855Ssam		}
374109323Ssam		sc->sc_ibss_port = htole16(1);
375119784Ssam
376119784Ssam		sc->sc_min_rssi = WI_LUCENT_MIN_RSSI;
377119784Ssam		sc->sc_max_rssi = WI_LUCENT_MAX_RSSI;
378119784Ssam		sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
37998440Simp		break;
380109323Ssam
38198440Simp	case WI_INTERSIL:
382112363Simp		sc->sc_ntxbuf = WI_NTXBUF;
383109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR;
384109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_ROAMING;
385109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
386109323Ssam		if (sc->sc_sta_firmware_ver > 10101)
387109323Ssam			sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
388117855Ssam		if (sc->sc_sta_firmware_ver >= 800) {
389116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
390117855Ssam			ic->ic_caps |= IEEE80211_C_MONITOR;
391117855Ssam		}
392109396Simp		/*
393109396Simp		 * version 0.8.3 and newer are the only ones that are known
394109396Simp		 * to currently work.  Earlier versions can be made to work,
395109396Simp		 * at least according to the Linux driver.
396109396Simp		 */
397100734Simp		if (sc->sc_sta_firmware_ver >= 803)
398116951Ssam			ic->ic_caps |= IEEE80211_C_HOSTAP;
399109323Ssam		sc->sc_ibss_port = htole16(0);
400119784Ssam
401119784Ssam		sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
402119784Ssam		sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
403119784Ssam		sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
40498440Simp		break;
405109323Ssam
40698440Simp	case WI_SYMBOL:
407112363Simp		sc->sc_ntxbuf = 1;
408109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY;
40998440Simp		if (sc->sc_sta_firmware_ver >= 25000)
410116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
411109323Ssam		sc->sc_ibss_port = htole16(4);
412119784Ssam
413119784Ssam		sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
414119784Ssam		sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
415119784Ssam		sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
41698440Simp		break;
41798440Simp	}
41898440Simp
41998440Simp	/*
42056965Swpaul	 * Find out if we support WEP on this card.
42156965Swpaul	 */
422109323Ssam	buflen = sizeof(val);
423109323Ssam	if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
424109323Ssam	    val != htole16(0))
425116951Ssam		ic->ic_caps |= IEEE80211_C_WEP;
42656965Swpaul
427109323Ssam	/* Find supported rates. */
428109323Ssam	buflen = sizeof(ratebuf);
429116951Ssam	rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
430109323Ssam	if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
431116951Ssam		nrates = le16toh(*(u_int16_t *)ratebuf);
432116951Ssam		if (nrates > IEEE80211_RATE_MAXSIZE)
433116951Ssam			nrates = IEEE80211_RATE_MAXSIZE;
434116951Ssam		rs->rs_nrates = 0;
435116951Ssam		for (i = 0; i < nrates; i++)
436116951Ssam			if (ratebuf[2+i])
437116951Ssam				rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i];
438109323Ssam	} else {
439109323Ssam		/* XXX fallback on error? */
440116951Ssam		rs->rs_nrates = 0;
44198440Simp	}
44277217Sphk
443109323Ssam	buflen = sizeof(val);
444109323Ssam	if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
445109323Ssam	    wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
446119784Ssam		sc->sc_dbm_offset = le16toh(val);
447119784Ssam	}
44846492Swpaul
449109323Ssam	sc->sc_max_datalen = 2304;
450109323Ssam	sc->sc_system_scale = 1;
451109323Ssam	sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN;
452109323Ssam	sc->sc_roaming_mode = 1;
45346492Swpaul
454109323Ssam	sc->sc_portnum = WI_DEFAULT_PORT;
455109323Ssam	sc->sc_authtype = WI_DEFAULT_AUTHTYPE;
45687383Simp
457109323Ssam	bzero(sc->sc_nodename, sizeof(sc->sc_nodename));
458109323Ssam	sc->sc_nodelen = sizeof(WI_DEFAULT_NODENAME) - 1;
459109323Ssam	bcopy(WI_DEFAULT_NODENAME, sc->sc_nodename, sc->sc_nodelen);
46087383Simp
461109323Ssam	bzero(sc->sc_net_name, sizeof(sc->sc_net_name));
462109323Ssam	bcopy(WI_DEFAULT_NETNAME, sc->sc_net_name,
463109323Ssam	    sizeof(WI_DEFAULT_NETNAME) - 1);
46493733Simp
46593733Simp	/*
466109323Ssam	 * Call MI attach routine.
46793733Simp	 */
468109323Ssam	ieee80211_ifattach(ifp);
469117812Ssam	/* override state transition method */
470117812Ssam	sc->sc_newstate = ic->ic_newstate;
471117812Ssam	ic->ic_newstate = wi_newstate;
472116951Ssam	ieee80211_media_init(ifp, wi_media_change, wi_media_status);
473109323Ssam
474119784Ssam#if NBPFILTER > 0
475119784Ssam	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
476119784Ssam		sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
477119784Ssam		&sc->sc_drvbpf);
478119784Ssam	/*
479119784Ssam	 * Initialize constant fields.
480119784Ssam	 *
481119784Ssam	 * NB: the channel is setup each time we transition to the
482119784Ssam	 *     RUN state to avoid filling it in for each frame.
483119784Ssam	 */
484119784Ssam	sc->sc_tx_th.wt_ihdr.it_len = sizeof(sc->sc_tx_th);
485119784Ssam	sc->sc_tx_th.wt_ihdr.it_present = WI_TX_RADIOTAP_PRESENT;
486119784Ssam
487119784Ssam	sc->sc_rx_th.wr_ihdr.it_len = sizeof(sc->sc_rx_th);
488119784Ssam	sc->sc_rx_th.wr_ihdr.it_present = WI_RX_RADIOTAP_PRESENT0;
489119784Ssam	sc->sc_rx_th.wr_present1 = WI_RX_RADIOTAP_PRESENT1;
490119784Ssam#endif
491109323Ssam	return (0);
49287383Simp}
49387383Simp
494109323Ssamint
495109323Ssamwi_detach(device_t dev)
49646492Swpaul{
497109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
498109323Ssam	struct ifnet *ifp = &sc->sc_ic.ic_if;
499109323Ssam	WI_LOCK_DECL();
50046492Swpaul
501109323Ssam	WI_LOCK(sc);
50246492Swpaul
503109323Ssam	/* check if device was removed */
504123098Simp	sc->wi_gone |= !bus_child_present(dev);
50546492Swpaul
506109323Ssam	wi_stop(ifp, 0);
50746492Swpaul
508119784Ssam#if NBPFILTER > 0
509119784Ssam	bpfdetach(ifp);
510119784Ssam#endif
511109323Ssam	ieee80211_ifdetach(ifp);
512114190Simp	WI_UNLOCK(sc);
513109323Ssam	bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
514109323Ssam	wi_free(dev);
515109323Ssam#if __FreeBSD_version >= 500000
516109323Ssam	mtx_destroy(&sc->sc_mtx);
51793359Simp#endif
518109323Ssam	return (0);
519109323Ssam}
52093359Simp
521109323Ssam#ifdef __NetBSD__
522109323Ssamint
523109323Ssamwi_activate(struct device *self, enum devact act)
524109323Ssam{
525109323Ssam	struct wi_softc *sc = (struct wi_softc *)self;
526109323Ssam	int rv = 0, s;
52793359Simp
528109323Ssam	s = splnet();
529109323Ssam	switch (act) {
530109323Ssam	case DVACT_ACTIVATE:
531109323Ssam		rv = EOPNOTSUPP;
532109323Ssam		break;
53393359Simp
534109323Ssam	case DVACT_DEACTIVATE:
535109323Ssam		if_deactivate(&sc->sc_ic.ic_if);
536109323Ssam		break;
537109323Ssam	}
538109323Ssam	splx(s);
539109323Ssam	return rv;
540109323Ssam}
54193359Simp
542109323Ssamvoid
543109323Ssamwi_power(struct wi_softc *sc, int why)
544109323Ssam{
545109323Ssam	struct ifnet *ifp = &sc->sc_ic.ic_if;
546109323Ssam	int s;
54793359Simp
548109323Ssam	s = splnet();
549109323Ssam	switch (why) {
550109323Ssam	case PWR_SUSPEND:
551109323Ssam	case PWR_STANDBY:
552109323Ssam		wi_stop(ifp, 1);
553109323Ssam		break;
554109323Ssam	case PWR_RESUME:
555109323Ssam		if (ifp->if_flags & IFF_UP) {
556109323Ssam			wi_init(ifp);
557109323Ssam			(void)wi_intr(sc);
55894405Simp		}
559109323Ssam		break;
560109323Ssam	case PWR_SOFTSUSPEND:
561109323Ssam	case PWR_SOFTSTANDBY:
562109323Ssam	case PWR_SOFTRESUME:
563109323Ssam		break;
56493359Simp	}
565109323Ssam	splx(s);
56646492Swpaul}
567109323Ssam#endif /* __NetBSD__ */
56846492Swpaul
569109323Ssamvoid
570109323Ssamwi_shutdown(device_t dev)
57146492Swpaul{
572109323Ssam	struct wi_softc *sc = device_get_softc(dev);
57346492Swpaul
574109323Ssam	wi_stop(&sc->sc_if, 1);
57546492Swpaul}
57646492Swpaul
577109323Ssamvoid
578109323Ssamwi_intr(void *arg)
57946492Swpaul{
580109323Ssam	struct wi_softc *sc = arg;
581109323Ssam	struct ifnet *ifp = &sc->sc_ic.ic_if;
582112363Simp	u_int16_t status;
583109323Ssam	WI_LOCK_DECL();
58446492Swpaul
585109323Ssam	WI_LOCK(sc);
58646492Swpaul
587123098Simp	if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) {
588113327Simp		CSR_WRITE_2(sc, WI_INT_EN, 0);
589123098Simp		CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
590109323Ssam		WI_UNLOCK(sc);
59146492Swpaul		return;
592109323Ssam	}
59346492Swpaul
594112363Simp	/* Disable interrupts. */
595112363Simp	CSR_WRITE_2(sc, WI_INT_EN, 0);
59646492Swpaul
597112363Simp	status = CSR_READ_2(sc, WI_EVENT_STAT);
598112363Simp	if (status & WI_EV_RX)
599112363Simp		wi_rx_intr(sc);
600112363Simp	if (status & WI_EV_ALLOC)
601112363Simp		wi_tx_intr(sc);
602112363Simp	if (status & WI_EV_TX_EXC)
603112363Simp		wi_tx_ex_intr(sc);
604112363Simp	if (status & WI_EV_INFO)
605112363Simp		wi_info_intr(sc);
606112363Simp	if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
607112363Simp	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 &&
608112363Simp	    _IF_QLEN(&ifp->if_snd) != 0)
609112363Simp		wi_start(ifp);
61046492Swpaul
611112363Simp	/* Re-enable interrupts. */
612112363Simp	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
61346492Swpaul
614109323Ssam	WI_UNLOCK(sc);
61546492Swpaul
61646492Swpaul	return;
61746492Swpaul}
61846492Swpaul
619109323Ssamvoid
620109323Ssamwi_init(void *arg)
62146492Swpaul{
622109323Ssam	struct wi_softc *sc = arg;
623109323Ssam	struct ifnet *ifp = &sc->sc_if;
624109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
625109323Ssam	struct wi_joinreq join;
626109323Ssam	int i;
627109323Ssam	int error = 0, wasenabled;
628109323Ssam	struct ifaddr *ifa;
629109323Ssam	struct sockaddr_dl *sdl;
630109323Ssam	WI_LOCK_DECL();
63146492Swpaul
632109323Ssam	WI_LOCK(sc);
63367092Swpaul
634109323Ssam	if (sc->wi_gone) {
635109323Ssam		WI_UNLOCK(sc);
63646492Swpaul		return;
63746492Swpaul	}
63846492Swpaul
639112362Simp	if ((wasenabled = sc->sc_enabled))
640123098Simp		wi_stop(ifp, 1);
641112362Simp	wi_reset(sc);
64246492Swpaul
643109323Ssam	/* common 802.11 configuration */
644109323Ssam	ic->ic_flags &= ~IEEE80211_F_IBSSON;
645109323Ssam	sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
646109323Ssam	switch (ic->ic_opmode) {
647109323Ssam	case IEEE80211_M_STA:
648109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS);
649109323Ssam		break;
650109323Ssam	case IEEE80211_M_IBSS:
651109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port);
652109323Ssam		ic->ic_flags |= IEEE80211_F_IBSSON;
653109323Ssam		break;
654109323Ssam	case IEEE80211_M_AHDEMO:
655109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
656109323Ssam		break;
657109323Ssam	case IEEE80211_M_HOSTAP:
658122087Sgreen		/*
659122087Sgreen		 * For PRISM cards, override the empty SSID, because in
660122087Sgreen		 * HostAP mode the controller will lock up otherwise.
661122087Sgreen		 */
662122087Sgreen		if (sc->sc_firmware_type == WI_INTERSIL &&
663122087Sgreen		    ic->ic_des_esslen == 0) {
664122087Sgreen			ic->ic_des_essid[0] = ' ';
665122087Sgreen			ic->ic_des_esslen = 1;
666122087Sgreen		}
667109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP);
668109323Ssam		break;
669117855Ssam	case IEEE80211_M_MONITOR:
670117855Ssam		if (sc->sc_firmware_type == WI_LUCENT)
671117855Ssam			wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
672117855Ssam		wi_cmd(sc, WI_CMD_DEBUG | (WI_TEST_MONITOR << 8), 0, 0, 0);
673117855Ssam		break;
67446492Swpaul	}
67546492Swpaul
676109323Ssam	/* Intersil interprets this RID as joining ESS even in IBSS mode */
677109323Ssam	if (sc->sc_firmware_type == WI_LUCENT &&
678109323Ssam	    (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0)
679109323Ssam		wi_write_val(sc, WI_RID_CREATE_IBSS, 1);
680109323Ssam	else
681109323Ssam		wi_write_val(sc, WI_RID_CREATE_IBSS, 0);
682109323Ssam	wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
683109323Ssam	wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid,
684109323Ssam	    ic->ic_des_esslen);
685116951Ssam	wi_write_val(sc, WI_RID_OWN_CHNL,
686116951Ssam		ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
687109323Ssam	wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen);
68846492Swpaul
689109323Ssam	ifa = ifaddr_byindex(ifp->if_index);
690109323Ssam	sdl = (struct sockaddr_dl *) ifa->ifa_addr;
691109323Ssam	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(sdl));
692109323Ssam	wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN);
69375373Salfred
694109323Ssam	wi_write_val(sc, WI_RID_PM_ENABLED,
695109323Ssam	    (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
69646492Swpaul
697109323Ssam	/* not yet common 802.11 configuration */
698109323Ssam	wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen);
699116951Ssam	wi_write_val(sc, WI_RID_RTS_THRESH, ic->ic_rtsthreshold);
700109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
701116951Ssam		wi_write_val(sc, WI_RID_FRAG_THRESH, ic->ic_fragthreshold);
70246492Swpaul
703109323Ssam	/* driver specific 802.11 configuration */
704109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)
705109323Ssam		wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale);
706109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
707109323Ssam		wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode);
708109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_MOR)
709109323Ssam		wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven);
710109323Ssam	wi_write_txrate(sc);
711109323Ssam	wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen);
71246492Swpaul
713109323Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
714109323Ssam	    sc->sc_firmware_type == WI_INTERSIL) {
715109323Ssam		wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval);
716109323Ssam		wi_write_val(sc, WI_RID_BASIC_RATE, 0x03);   /* 1, 2 */
717109323Ssam		wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */
718109323Ssam		wi_write_val(sc, WI_RID_DTIM_PERIOD, 1);
71946492Swpaul	}
72046492Swpaul
721109323Ssam	/*
722109323Ssam	 * Initialize promisc mode.
723109323Ssam	 *	Being in the Host-AP mode causes a great
724109323Ssam	 *	deal of pain if primisc mode is set.
725109323Ssam	 *	Therefore we avoid confusing the firmware
726109323Ssam	 *	and always reset promisc mode in Host-AP
727109323Ssam	 *	mode.  Host-AP sees all the packets anyway.
728109323Ssam	 */
729109323Ssam	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
730109323Ssam	    (ifp->if_flags & IFF_PROMISC) != 0) {
731109323Ssam		wi_write_val(sc, WI_RID_PROMISC, 1);
732109323Ssam	} else {
733109323Ssam		wi_write_val(sc, WI_RID_PROMISC, 0);
73475373Salfred	}
73546492Swpaul
736109323Ssam	/* Configure WEP. */
737116951Ssam	if (ic->ic_caps & IEEE80211_C_WEP)
738109323Ssam		wi_write_wep(sc);
73967092Swpaul
740109323Ssam	/* Set multicast filter. */
741109323Ssam	wi_write_multi(sc);
74246492Swpaul
743114124Simp	/* Allocate fids for the card */
744109323Ssam	if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
745109323Ssam		sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
746109323Ssam		if (sc->sc_firmware_type == WI_SYMBOL)
747109323Ssam			sc->sc_buflen = 1585;	/* XXX */
748112363Simp		for (i = 0; i < sc->sc_ntxbuf; i++) {
749109323Ssam			error = wi_alloc_fid(sc, sc->sc_buflen,
750109323Ssam			    &sc->sc_txd[i].d_fid);
751109323Ssam			if (error) {
752109323Ssam				device_printf(sc->sc_dev,
753109323Ssam				    "tx buffer allocation failed (error %u)\n",
754109323Ssam				    error);
755109323Ssam				goto out;
756109323Ssam			}
757109323Ssam			sc->sc_txd[i].d_len = 0;
75870073Swpaul		}
75970073Swpaul	}
760109323Ssam	sc->sc_txcur = sc->sc_txnext = 0;
76170073Swpaul
762109323Ssam	/* Enable desired port */
763109323Ssam	wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
76446492Swpaul
765114124Simp	sc->sc_enabled = 1;
766109323Ssam	ifp->if_flags |= IFF_RUNNING;
767109323Ssam	ifp->if_flags &= ~IFF_OACTIVE;
768109323Ssam	if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
769117855Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR ||
770109323Ssam	    ic->ic_opmode == IEEE80211_M_HOSTAP)
771117812Ssam		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
77246492Swpaul
773109323Ssam	/* Enable interrupts */
774109323Ssam	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
77546492Swpaul
776109323Ssam	if (!wasenabled &&
777109323Ssam	    ic->ic_opmode == IEEE80211_M_HOSTAP &&
778109323Ssam	    sc->sc_firmware_type == WI_INTERSIL) {
779109323Ssam		/* XXX: some card need to be re-enabled for hostap */
780109323Ssam		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
781109323Ssam		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
78275149Simp	}
78394397Simp
784109323Ssam	if (ic->ic_opmode == IEEE80211_M_STA &&
785109323Ssam	    ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
786116951Ssam	    ic->ic_des_chan != IEEE80211_CHAN_ANYC)) {
787109323Ssam		memset(&join, 0, sizeof(join));
788109323Ssam		if (ic->ic_flags & IEEE80211_F_DESBSSID)
789109323Ssam			IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
790116951Ssam		if (ic->ic_des_chan != IEEE80211_CHAN_ANYC)
791116951Ssam			join.wi_chan = htole16(
792116951Ssam				ieee80211_chan2ieee(ic, ic->ic_des_chan));
793109323Ssam		/* Lucent firmware does not support the JOIN RID. */
794109323Ssam		if (sc->sc_firmware_type != WI_LUCENT)
795109323Ssam			wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
79694397Simp	}
79775373Salfred
798109323Ssam	WI_UNLOCK(sc);
79946492Swpaul	return;
800109323Ssamout:
801109323Ssam	if (error) {
802109323Ssam		if_printf(ifp, "interface not running\n");
803123098Simp		wi_stop(ifp, 1);
80470073Swpaul	}
805110224Simp	WI_UNLOCK(sc);
806109323Ssam	DPRINTF(("wi_init: return %d\n", error));
807109323Ssam	return;
80846492Swpaul}
80946492Swpaul
810109323Ssamvoid
811109323Ssamwi_stop(struct ifnet *ifp, int disable)
81246492Swpaul{
813117812Ssam	struct ieee80211com *ic = (struct ieee80211com *) ifp;
814109323Ssam	struct wi_softc *sc = ifp->if_softc;
815109323Ssam	WI_LOCK_DECL();
81646492Swpaul
817109323Ssam	WI_LOCK(sc);
81874998Swpaul
819123098Simp	DELAY(100000);
820123098Simp
821117812Ssam	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
822109323Ssam	if (sc->sc_enabled && !sc->wi_gone) {
823109323Ssam		CSR_WRITE_2(sc, WI_INT_EN, 0);
824109323Ssam		wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
825109323Ssam		if (disable) {
826109323Ssam#ifdef __NetBSD__
827109323Ssam			if (sc->sc_disable)
828109323Ssam				(*sc->sc_disable)(sc);
829109323Ssam#endif
830116951Ssam			sc->sc_enabled = 0;
83170073Swpaul		}
832123098Simp	} else if (sc->wi_gone && disable)	/* gone --> not enabled */
833123098Simp	    sc->sc_enabled = 0;
83470073Swpaul
835109323Ssam	sc->sc_tx_timer = 0;
836109323Ssam	sc->sc_scan_timer = 0;
837109323Ssam	sc->sc_syn_timer = 0;
838109323Ssam	sc->sc_false_syns = 0;
839109323Ssam	sc->sc_naps = 0;
840109323Ssam	ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
841109323Ssam	ifp->if_timer = 0;
84246492Swpaul
843109323Ssam	WI_UNLOCK(sc);
84446492Swpaul}
84546492Swpaul
846109323Ssamstatic void
847109323Ssamwi_start(struct ifnet *ifp)
84846492Swpaul{
849109323Ssam	struct wi_softc	*sc = ifp->if_softc;
850109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
851119150Ssam	struct ieee80211_node *ni;
852109323Ssam	struct ieee80211_frame *wh;
853109323Ssam	struct mbuf *m0;
854109323Ssam	struct wi_frame frmhdr;
855119150Ssam	int cur, fid, off, error;
856109323Ssam	WI_LOCK_DECL();
85746492Swpaul
858109323Ssam	WI_LOCK(sc);
85946492Swpaul
860109323Ssam	if (sc->wi_gone) {
861109323Ssam		WI_UNLOCK(sc);
862109323Ssam		return;
86346492Swpaul	}
864109323Ssam	if (sc->sc_flags & WI_FLAGS_OUTRANGE) {
865109323Ssam		WI_UNLOCK(sc);
866109323Ssam		return;
86775373Salfred	}
86846492Swpaul
869109323Ssam	memset(&frmhdr, 0, sizeof(frmhdr));
870109323Ssam	cur = sc->sc_txnext;
871109323Ssam	for (;;) {
872109323Ssam		IF_POLL(&ic->ic_mgtq, m0);
873109323Ssam		if (m0 != NULL) {
874109323Ssam			if (sc->sc_txd[cur].d_len != 0) {
875109323Ssam				ifp->if_flags |= IFF_OACTIVE;
876109323Ssam				break;
877109323Ssam			}
878109323Ssam			IF_DEQUEUE(&ic->ic_mgtq, m0);
879119150Ssam			/*
880119150Ssam			 * Hack!  The referenced node pointer is in the
881119150Ssam			 * rcvif field of the packet header.  This is
882119150Ssam			 * placed there by ieee80211_mgmt_output because
883119150Ssam			 * we need to hold the reference with the frame
884119150Ssam			 * and there's no other way (other than packet
885119150Ssam			 * tags which we consider too expensive to use)
886119150Ssam			 * to pass it along.
887119150Ssam			 */
888119150Ssam			ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
889119150Ssam			m0->m_pkthdr.rcvif = NULL;
890119150Ssam
891109323Ssam			m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
892109323Ssam			    (caddr_t)&frmhdr.wi_ehdr);
893109323Ssam			frmhdr.wi_ehdr.ether_type = 0;
894109323Ssam                        wh = mtod(m0, struct ieee80211_frame *);
895109323Ssam		} else {
896109323Ssam			if (ic->ic_state != IEEE80211_S_RUN)
897109323Ssam				break;
898109323Ssam			IFQ_POLL(&ifp->if_snd, m0);
899109323Ssam			if (m0 == NULL)
900109323Ssam				break;
901109323Ssam			if (sc->sc_txd[cur].d_len != 0) {
902109323Ssam				ifp->if_flags |= IFF_OACTIVE;
903109323Ssam				break;
904109323Ssam			}
905109323Ssam			IFQ_DEQUEUE(&ifp->if_snd, m0);
906109323Ssam			ifp->if_opackets++;
907109323Ssam			m_copydata(m0, 0, ETHER_HDR_LEN,
908109323Ssam			    (caddr_t)&frmhdr.wi_ehdr);
909109323Ssam#if NBPFILTER > 0
910109323Ssam			BPF_MTAP(ifp, m0);
911109323Ssam#endif
91246492Swpaul
913119150Ssam			m0 = ieee80211_encap(ifp, m0, &ni);
914119150Ssam			if (m0 == NULL) {
915109323Ssam				ifp->if_oerrors++;
916109323Ssam				continue;
917109323Ssam			}
918109323Ssam                        wh = mtod(m0, struct ieee80211_frame *);
919109323Ssam			if (ic->ic_flags & IEEE80211_F_WEPON)
920109323Ssam				wh->i_fc[1] |= IEEE80211_FC1_WEP;
92146492Swpaul
922109323Ssam		}
923109323Ssam#if NBPFILTER > 0
924109323Ssam		if (ic->ic_rawbpf)
925109323Ssam			bpf_mtap(ic->ic_rawbpf, m0);
926109323Ssam#endif
927109323Ssam		frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
928109323Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
929109323Ssam		    (wh->i_fc[1] & IEEE80211_FC1_WEP)) {
930109323Ssam			if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) {
931109323Ssam				ifp->if_oerrors++;
932119150Ssam				if (ni && ni != ic->ic_bss)
933119150Ssam					ieee80211_free_node(ic, ni);
934109323Ssam				continue;
935109323Ssam			}
936109323Ssam			frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
937109323Ssam		}
938109323Ssam		m_copydata(m0, 0, sizeof(struct ieee80211_frame),
939109323Ssam		    (caddr_t)&frmhdr.wi_whdr);
940109323Ssam		m_adj(m0, sizeof(struct ieee80211_frame));
941109323Ssam		frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
942109323Ssam#if NBPFILTER > 0
943109323Ssam		if (sc->sc_drvbpf) {
944109323Ssam			struct mbuf *mb;
94546492Swpaul
946111119Simp			MGETHDR(mb, M_DONTWAIT, m0->m_type);
947109323Ssam			if (mb != NULL) {
948109323Ssam				mb->m_next = m0;
949119784Ssam				mb->m_data = (caddr_t)&sc->sc_tx_th;
950119784Ssam				mb->m_len = sizeof(sc->sc_tx_th);
951109323Ssam				mb->m_pkthdr.len += mb->m_len;
952109323Ssam				bpf_mtap(sc->sc_drvbpf, mb);
953109323Ssam				m_free(mb);
954109323Ssam			}
955109323Ssam		}
95646492Swpaul#endif
957109323Ssam		if (IFF_DUMPPKTS(ifp))
958116951Ssam			wi_dump_pkt(&frmhdr, NULL, -1);
959109323Ssam		fid = sc->sc_txd[cur].d_fid;
960109323Ssam		off = sizeof(frmhdr);
961119150Ssam		error = wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0
962119150Ssam		     || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
963119150Ssam		m_freem(m0);
964119150Ssam		if (ni && ni != ic->ic_bss)
965119150Ssam			ieee80211_free_node(ic, ni);
966119150Ssam		if (error) {
967109323Ssam			ifp->if_oerrors++;
968109323Ssam			continue;
969109323Ssam		}
970109323Ssam		sc->sc_txd[cur].d_len = off;
971109323Ssam		if (sc->sc_txcur == cur) {
972109323Ssam			if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
973109323Ssam				if_printf(ifp, "xmit failed\n");
974109323Ssam				sc->sc_txd[cur].d_len = 0;
975109323Ssam				continue;
976109323Ssam			}
977109323Ssam			sc->sc_tx_timer = 5;
978109323Ssam			ifp->if_timer = 1;
979109323Ssam		}
980112363Simp		sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
98174838Salfred	}
98246492Swpaul
983109323Ssam	WI_UNLOCK(sc);
98446492Swpaul}
98546492Swpaul
98688546Salfredstatic int
987109323Ssamwi_reset(struct wi_softc *sc)
98846492Swpaul{
989114124Simp	struct ieee80211com *ic = &sc->sc_ic;
990114124Simp	struct ifnet *ifp = &ic->ic_if;
991114124Simp#define WI_INIT_TRIES 3
992114124Simp	int i;
993114138Simp	int error = 0;
994114124Simp	int tries;
995114124Simp
996114124Simp	/* Symbol firmware cannot be initialized more than once */
997123098Simp	if (sc->sc_firmware_type == WI_SYMBOL && sc->sc_reset)
998112362Simp		return (0);
999114124Simp	if (sc->sc_firmware_type == WI_SYMBOL)
1000114124Simp		tries = 1;
1001114124Simp	else
1002114124Simp		tries = WI_INIT_TRIES;
1003112362Simp
1004114124Simp	for (i = 0; i < tries; i++) {
1005109323Ssam		if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
100646492Swpaul			break;
1007109323Ssam		DELAY(WI_DELAY * 1000);
100846492Swpaul	}
1009114124Simp	sc->sc_reset = 1;
101046492Swpaul
1011114124Simp	if (i == tries) {
1012114124Simp		if_printf(ifp, "init failed\n");
1013114124Simp		return (error);
101475373Salfred	}
101546492Swpaul
1016109323Ssam	CSR_WRITE_2(sc, WI_INT_EN, 0);
1017114124Simp	CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
101846492Swpaul
1019109323Ssam	/* Calibrate timer. */
1020114124Simp	wi_write_val(sc, WI_RID_TICK_TIME, 8);
1021114124Simp
1022114124Simp	return (0);
1023109323Ssam#undef WI_INIT_TRIES
102446492Swpaul}
102546492Swpaul
102688546Salfredstatic void
1027109323Ssamwi_watchdog(struct ifnet *ifp)
102846492Swpaul{
1029109323Ssam	struct wi_softc	*sc = ifp->if_softc;
103046492Swpaul
1031109323Ssam	ifp->if_timer = 0;
1032109323Ssam	if (!sc->sc_enabled)
1033109323Ssam		return;
103446492Swpaul
1035109323Ssam	if (sc->sc_tx_timer) {
1036109323Ssam		if (--sc->sc_tx_timer == 0) {
1037109323Ssam			if_printf(ifp, "device timeout\n");
1038109323Ssam			ifp->if_oerrors++;
1039109323Ssam			wi_init(ifp->if_softc);
1040109323Ssam			return;
1041109323Ssam		}
1042109323Ssam		ifp->if_timer = 1;
104346492Swpaul	}
104446492Swpaul
1045109323Ssam	if (sc->sc_scan_timer) {
1046109323Ssam		if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
1047109323Ssam		    sc->sc_firmware_type == WI_INTERSIL) {
1048109323Ssam			DPRINTF(("wi_watchdog: inquire scan\n"));
1049109323Ssam			wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
105046492Swpaul		}
1051109323Ssam		if (sc->sc_scan_timer)
1052109323Ssam			ifp->if_timer = 1;
105346492Swpaul	}
105446492Swpaul
1055109323Ssam	if (sc->sc_syn_timer) {
1056109323Ssam		if (--sc->sc_syn_timer == 0) {
1057117812Ssam			struct ieee80211com *ic = (struct ieee80211com *) ifp;
1058109323Ssam			DPRINTF2(("wi_watchdog: %d false syns\n",
1059109323Ssam			    sc->sc_false_syns));
1060109323Ssam			sc->sc_false_syns = 0;
1061117812Ssam			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1062109323Ssam			sc->sc_syn_timer = 5;
1063109323Ssam		}
1064109323Ssam		ifp->if_timer = 1;
106546492Swpaul	}
106646492Swpaul
1067109323Ssam	/* TODO: rate control */
1068109323Ssam	ieee80211_watchdog(ifp);
106946492Swpaul}
107046492Swpaul
107188546Salfredstatic int
1072109323Ssamwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
107346492Swpaul{
1074109323Ssam	struct wi_softc *sc = ifp->if_softc;
1075109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1076109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
1077109323Ssam	struct ieee80211req *ireq;
1078109323Ssam	u_int8_t nodename[IEEE80211_NWID_LEN];
1079109323Ssam	int error = 0;
108095534Simp#if __FreeBSD_version >= 500000
1081109323Ssam	struct thread *td = curthread;
108295534Simp#else
1083109323Ssam	struct proc *td = curproc;		/* Little white lie */
108495534Simp#endif
1085109323Ssam	struct wi_req wreq;
1086109323Ssam	WI_LOCK_DECL();
108746492Swpaul
1088109323Ssam	WI_LOCK(sc);
108946492Swpaul
109061818Sroberto	if (sc->wi_gone) {
109161818Sroberto		error = ENODEV;
109261818Sroberto		goto out;
109361818Sroberto	}
109446492Swpaul
1095109323Ssam	switch (cmd) {
109646492Swpaul	case SIOCSIFFLAGS:
1097100876Simp		/*
1098101139Simp		 * Can't do promisc and hostap at the same time.  If all that's
1099101139Simp		 * changing is the promisc flag, try to short-circuit a call to
1100101139Simp		 * wi_init() by just setting PROMISC in the hardware.
1101100876Simp		 */
110246492Swpaul		if (ifp->if_flags & IFF_UP) {
1103109323Ssam			if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1104101139Simp			    ifp->if_flags & IFF_RUNNING) {
1105101139Simp				if (ifp->if_flags & IFF_PROMISC &&
1106109323Ssam				    !(sc->sc_if_flags & IFF_PROMISC)) {
1107109323Ssam					wi_write_val(sc, WI_RID_PROMISC, 1);
1108101139Simp				} else if (!(ifp->if_flags & IFF_PROMISC) &&
1109109323Ssam				    sc->sc_if_flags & IFF_PROMISC) {
1110109323Ssam					wi_write_val(sc, WI_RID_PROMISC, 0);
1111101139Simp				} else {
1112101139Simp					wi_init(sc);
1113101139Simp				}
1114100876Simp			} else {
111546492Swpaul				wi_init(sc);
1116100876Simp			}
111746492Swpaul		} else {
111846492Swpaul			if (ifp->if_flags & IFF_RUNNING) {
1119123098Simp				wi_stop(ifp, 1);
112046492Swpaul			}
1121123098Simp			sc->wi_gone = 0;
112246492Swpaul		}
1123109323Ssam		sc->sc_if_flags = ifp->if_flags;
112446492Swpaul		error = 0;
112546492Swpaul		break;
112646492Swpaul	case SIOCADDMULTI:
112746492Swpaul	case SIOCDELMULTI:
1128109323Ssam		error = wi_write_multi(sc);
112946492Swpaul		break;
1130109323Ssam	case SIOCGIFGENERIC:
1131109323Ssam		error = wi_get_cfg(ifp, cmd, data);
113246492Swpaul		break;
1133109323Ssam	case SIOCSIFGENERIC:
1134109323Ssam		error = suser(td);
113546492Swpaul		if (error)
113646492Swpaul			break;
1137109323Ssam		error = wi_set_cfg(ifp, cmd, data);
113846492Swpaul		break;
113993359Simp	case SIOCGPRISM2DEBUG:
114093359Simp		error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
114193359Simp		if (error)
114293359Simp			break;
114393733Simp		if (!(ifp->if_flags & IFF_RUNNING) ||
114493733Simp		    sc->sc_firmware_type == WI_LUCENT) {
114593359Simp			error = EIO;
114693359Simp			break;
114793359Simp		}
114893359Simp		error = wi_get_debug(sc, &wreq);
114993359Simp		if (error == 0)
115093359Simp			error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
115193359Simp		break;
115293359Simp	case SIOCSPRISM2DEBUG:
115393593Sjhb		if ((error = suser(td)))
115493359Simp			goto out;
115593359Simp		error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
115693359Simp		if (error)
115793359Simp			break;
115893359Simp		error = wi_set_debug(sc, &wreq);
115993359Simp		break;
116077217Sphk	case SIOCG80211:
1161109323Ssam		ireq = (struct ieee80211req *) data;
1162109323Ssam		switch (ireq->i_type) {
116377217Sphk		case IEEE80211_IOC_STATIONNAME:
1164109323Ssam			ireq->i_len = sc->sc_nodelen + 1;
1165109323Ssam			error = copyout(sc->sc_nodename, ireq->i_data,
1166109323Ssam					ireq->i_len);
116777217Sphk			break;
1168109323Ssam		default:
1169109323Ssam			error = ieee80211_ioctl(ifp, cmd, data);
117077217Sphk			break;
117177217Sphk		}
117277217Sphk		break;
117377217Sphk	case SIOCS80211:
1174109323Ssam		error = suser(td);
1175109323Ssam		if (error)
1176109323Ssam			break;
1177109323Ssam		ireq = (struct ieee80211req *) data;
1178109323Ssam		switch (ireq->i_type) {
1179109323Ssam		case IEEE80211_IOC_STATIONNAME:
118077217Sphk			if (ireq->i_val != 0 ||
118177217Sphk			    ireq->i_len > IEEE80211_NWID_LEN) {
118277217Sphk				error = EINVAL;
118377217Sphk				break;
118477217Sphk			}
1185109323Ssam			memset(nodename, 0, IEEE80211_NWID_LEN);
1186109323Ssam			error = copyin(ireq->i_data, nodename, ireq->i_len);
1187109323Ssam			if (error)
118877217Sphk				break;
1189109323Ssam			if (sc->sc_enabled) {
1190109323Ssam				error = wi_write_ssid(sc, WI_RID_NODENAME,
1191109323Ssam					nodename, ireq->i_len);
1192109323Ssam				if (error)
1193109323Ssam					break;
119477217Sphk			}
1195109323Ssam			memcpy(sc->sc_nodename, nodename, IEEE80211_NWID_LEN);
1196109323Ssam			sc->sc_nodelen = ireq->i_len;
119777217Sphk			break;
119877217Sphk		default:
1199109323Ssam			error = ieee80211_ioctl(ifp, cmd, data);
120077217Sphk			break;
120177217Sphk		}
120277217Sphk		break;
120346492Swpaul	default:
1204109323Ssam		error = ieee80211_ioctl(ifp, cmd, data);
120546492Swpaul		break;
120646492Swpaul	}
1207109323Ssam	if (error == ENETRESET) {
1208109323Ssam		if (sc->sc_enabled)
1209116951Ssam			wi_init(sc);	/* XXX no error return */
1210109323Ssam		error = 0;
1211109323Ssam	}
121261818Srobertoout:
1213109323Ssam	WI_UNLOCK(sc);
121446492Swpaul
1215109323Ssam	return (error);
121646492Swpaul}
121746492Swpaul
1218109323Ssamstatic int
1219109323Ssamwi_media_change(struct ifnet *ifp)
122046492Swpaul{
1221109323Ssam	struct wi_softc *sc = ifp->if_softc;
1222116951Ssam	int error;
122346492Swpaul
1224116951Ssam	error = ieee80211_media_change(ifp);
1225109323Ssam	if (error == ENETRESET) {
1226109323Ssam		if (sc->sc_enabled)
1227116951Ssam			wi_init(sc);	/* XXX no error return */
1228109323Ssam		error = 0;
1229109323Ssam	}
1230109323Ssam	return error;
1231109323Ssam}
123246492Swpaul
1233109323Ssamstatic void
1234109323Ssamwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1235109323Ssam{
1236109323Ssam	struct wi_softc *sc = ifp->if_softc;
1237109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1238109323Ssam	u_int16_t val;
1239109323Ssam	int rate, len;
124046492Swpaul
1241109323Ssam	if (sc->wi_gone || !sc->sc_enabled) {
1242109323Ssam		imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
1243109323Ssam		imr->ifm_status = 0;
1244109323Ssam		return;
1245109323Ssam	}
124646492Swpaul
1247109323Ssam	imr->ifm_status = IFM_AVALID;
1248109323Ssam	imr->ifm_active = IFM_IEEE80211;
1249109323Ssam	if (ic->ic_state == IEEE80211_S_RUN &&
1250109323Ssam	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
1251109323Ssam		imr->ifm_status |= IFM_ACTIVE;
1252109323Ssam	len = sizeof(val);
1253109323Ssam	if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) != 0)
1254109323Ssam		rate = 0;
1255109323Ssam	else {
1256109323Ssam		/* convert to 802.11 rate */
1257109323Ssam		rate = val * 2;
1258109323Ssam		if (sc->sc_firmware_type == WI_LUCENT) {
1259112501Simp			if (rate == 4 * 2)
1260109323Ssam				rate = 11;	/* 5.5Mbps */
1261112501Simp			else if (rate == 5 * 2)
1262112501Simp				rate = 22;	/* 11Mbps */
1263109323Ssam		} else {
1264109323Ssam			if (rate == 4*2)
1265109323Ssam				rate = 11;	/* 5.5Mbps */
1266109323Ssam			else if (rate == 8*2)
1267109323Ssam				rate = 22;	/* 11Mbps */
1268109323Ssam		}
1269109323Ssam	}
1270116951Ssam	imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
1271109323Ssam	switch (ic->ic_opmode) {
1272109323Ssam	case IEEE80211_M_STA:
1273109323Ssam		break;
1274109323Ssam	case IEEE80211_M_IBSS:
1275109323Ssam		imr->ifm_active |= IFM_IEEE80211_ADHOC;
1276109323Ssam		break;
1277109323Ssam	case IEEE80211_M_AHDEMO:
1278109323Ssam		imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1279109323Ssam		break;
1280109323Ssam	case IEEE80211_M_HOSTAP:
1281109323Ssam		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1282109323Ssam		break;
1283117855Ssam	case IEEE80211_M_MONITOR:
1284117855Ssam		imr->ifm_active |= IFM_IEEE80211_MONITOR;
1285117855Ssam		break;
1286109323Ssam	}
1287109323Ssam}
128846492Swpaul
1289109323Ssamstatic void
1290109323Ssamwi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1291109323Ssam{
1292109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1293116951Ssam	struct ieee80211_node *ni = ic->ic_bss;
1294109323Ssam	struct ifnet *ifp = &ic->ic_if;
129598440Simp
1296109323Ssam	if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1297109323Ssam		return;
129846492Swpaul
1299109323Ssam	DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid)));
1300109323Ssam	DPRINTF(("%s ?\n", ether_sprintf(new_bssid)));
130146492Swpaul
1302109323Ssam	/* In promiscuous mode, the BSSID field is not a reliable
1303109323Ssam	 * indicator of the firmware's BSSID. Damp spurious
1304109323Ssam	 * change-of-BSSID indications.
1305109323Ssam	 */
1306109323Ssam	if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1307109323Ssam	    sc->sc_false_syns >= WI_MAX_FALSE_SYNS)
1308109323Ssam		return;
130946492Swpaul
1310117812Ssam	ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1311109323Ssam}
131246492Swpaul
1313109323Ssamstatic void
1314116898Ssamwi_rx_monitor(struct wi_softc *sc, int fid)
1315116898Ssam{
1316116898Ssam	struct ieee80211com *ic = &sc->sc_ic;
1317116898Ssam	struct ifnet *ifp = &ic->ic_if;
1318116898Ssam	struct wi_frame *rx_frame;
1319116898Ssam	struct mbuf *m;
1320116898Ssam	int datlen, hdrlen;
1321116898Ssam
1322116898Ssam	/* first allocate mbuf for packet storage */
1323116898Ssam	m = m_getcl(M_DONTWAIT, MT_DATA, 0);
1324116898Ssam	if (m == NULL) {
1325116898Ssam		ifp->if_ierrors++;
1326116898Ssam		return;
1327116898Ssam	}
1328116898Ssam
1329116898Ssam	m->m_pkthdr.rcvif = ifp;
1330116898Ssam
1331116898Ssam	/* now read wi_frame first so we know how much data to read */
1332116898Ssam	if (wi_read_bap(sc, fid, 0, mtod(m, caddr_t), sizeof(*rx_frame))) {
1333116898Ssam		ifp->if_ierrors++;
1334116898Ssam		goto done;
1335116898Ssam	}
1336116898Ssam
1337116898Ssam	rx_frame = mtod(m, struct wi_frame *);
1338116898Ssam
1339116898Ssam	switch ((rx_frame->wi_status & WI_STAT_MAC_PORT) >> 8) {
1340116898Ssam	case 7:
1341116898Ssam		switch (rx_frame->wi_whdr.i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1342116898Ssam		case IEEE80211_FC0_TYPE_DATA:
1343116898Ssam			hdrlen = WI_DATA_HDRLEN;
1344116898Ssam			datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1345116898Ssam			break;
1346116898Ssam		case IEEE80211_FC0_TYPE_MGT:
1347116898Ssam			hdrlen = WI_MGMT_HDRLEN;
1348116898Ssam			datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1349116898Ssam			break;
1350116898Ssam		case IEEE80211_FC0_TYPE_CTL:
1351116898Ssam			/*
1352116898Ssam			 * prism2 cards don't pass control packets
1353116898Ssam			 * down properly or consistently, so we'll only
1354116898Ssam			 * pass down the header.
1355116898Ssam			 */
1356116898Ssam			hdrlen = WI_CTL_HDRLEN;
1357116898Ssam			datlen = 0;
1358116898Ssam			break;
1359116898Ssam		default:
1360116898Ssam			if_printf(ifp, "received packet of unknown type "
1361116898Ssam				"on port 7\n");
1362116898Ssam			ifp->if_ierrors++;
1363116898Ssam			goto done;
1364116898Ssam		}
1365116898Ssam		break;
1366116898Ssam	case 0:
1367116898Ssam		hdrlen = WI_DATA_HDRLEN;
1368116898Ssam		datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1369116898Ssam		break;
1370116898Ssam	default:
1371116898Ssam		if_printf(ifp, "received packet on invalid "
1372116898Ssam		    "port (wi_status=0x%x)\n", rx_frame->wi_status);
1373116898Ssam		ifp->if_ierrors++;
1374116898Ssam		goto done;
1375116898Ssam	}
1376116898Ssam
1377116898Ssam	if (hdrlen + datlen + 2 > MCLBYTES) {
1378116898Ssam		if_printf(ifp, "oversized packet received "
1379116898Ssam		    "(wi_dat_len=%d, wi_status=0x%x)\n",
1380116898Ssam		    datlen, rx_frame->wi_status);
1381116898Ssam		ifp->if_ierrors++;
1382116898Ssam		goto done;
1383116898Ssam	}
1384116898Ssam
1385116898Ssam	if (wi_read_bap(sc, fid, hdrlen, mtod(m, caddr_t) + hdrlen,
1386116898Ssam	    datlen + 2) == 0) {
1387116898Ssam		m->m_pkthdr.len = m->m_len = hdrlen + datlen;
1388116898Ssam		ifp->if_ipackets++;
1389116898Ssam		BPF_MTAP(ifp, m);	/* Handle BPF listeners. */
1390116898Ssam	} else
1391116898Ssam		ifp->if_ierrors++;
1392116898Ssamdone:
1393116898Ssam	m_freem(m);
1394116898Ssam}
1395116898Ssam
1396116898Ssamstatic void
1397109323Ssamwi_rx_intr(struct wi_softc *sc)
1398109323Ssam{
1399109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1400109323Ssam	struct ifnet *ifp = &ic->ic_if;
1401109323Ssam	struct wi_frame frmhdr;
1402109323Ssam	struct mbuf *m;
1403109323Ssam	struct ieee80211_frame *wh;
1404119150Ssam	struct ieee80211_node *ni;
1405109323Ssam	int fid, len, off, rssi;
1406109323Ssam	u_int8_t dir;
1407109323Ssam	u_int16_t status;
1408109323Ssam	u_int32_t rstamp;
140946611Swpaul
1410109323Ssam	fid = CSR_READ_2(sc, WI_RX_FID);
141146611Swpaul
1412116898Ssam	if (sc->wi_debug.wi_monitor) {
1413116898Ssam		/*
1414116898Ssam		 * If we are in monitor mode just
1415116898Ssam		 * read the data from the device.
1416116898Ssam		 */
1417116898Ssam		wi_rx_monitor(sc, fid);
1418116898Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1419116898Ssam		return;
1420116898Ssam	}
1421116898Ssam
1422109323Ssam	/* First read in the frame header */
1423109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1424109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1425109323Ssam		ifp->if_ierrors++;
1426109323Ssam		DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1427109323Ssam		return;
1428109323Ssam	}
142991695Simp
1430109323Ssam	if (IFF_DUMPPKTS(ifp))
1431109323Ssam		wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal);
143246492Swpaul
1433109323Ssam	/*
1434109323Ssam	 * Drop undecryptable or packets with receive errors here
1435109323Ssam	 */
1436109323Ssam	status = le16toh(frmhdr.wi_status);
1437109323Ssam	if (status & WI_STAT_ERRSTAT) {
1438109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1439109323Ssam		ifp->if_ierrors++;
1440109323Ssam		DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1441109323Ssam		return;
1442109323Ssam	}
1443109323Ssam	rssi = frmhdr.wi_rx_signal;
1444109323Ssam	rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1445109323Ssam	    le16toh(frmhdr.wi_rx_tstamp1);
144646492Swpaul
1447109323Ssam	len = le16toh(frmhdr.wi_dat_len);
1448109323Ssam	off = ALIGN(sizeof(struct ieee80211_frame));
144946563Swpaul
1450117855Ssam	/*
1451117855Ssam	 * Sometimes the PRISM2.x returns bogusly large frames. Except
1452117855Ssam	 * in monitor mode, just throw them away.
1453117855Ssam	 */
1454117855Ssam	if (off + len > MCLBYTES) {
1455117855Ssam		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1456117855Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1457117855Ssam			ifp->if_ierrors++;
1458117855Ssam			DPRINTF(("wi_rx_intr: oversized packet\n"));
1459117855Ssam			return;
1460117855Ssam		} else
1461117855Ssam			len = 0;
1462117855Ssam	}
1463117855Ssam
1464111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
1465109323Ssam	if (m == NULL) {
1466109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1467109323Ssam		ifp->if_ierrors++;
1468109323Ssam		DPRINTF(("wi_rx_intr: MGET failed\n"));
1469109323Ssam		return;
1470109323Ssam	}
1471109323Ssam	if (off + len > MHLEN) {
1472111119Simp		MCLGET(m, M_DONTWAIT);
1473109323Ssam		if ((m->m_flags & M_EXT) == 0) {
1474109323Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1475109323Ssam			m_freem(m);
1476109323Ssam			ifp->if_ierrors++;
1477109323Ssam			DPRINTF(("wi_rx_intr: MCLGET failed\n"));
1478109323Ssam			return;
1479109323Ssam		}
1480109323Ssam	}
148146492Swpaul
1482109323Ssam	m->m_data += off - sizeof(struct ieee80211_frame);
1483109323Ssam	memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1484109323Ssam	wi_read_bap(sc, fid, sizeof(frmhdr),
1485109323Ssam	    m->m_data + sizeof(struct ieee80211_frame), len);
1486109323Ssam	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1487109323Ssam	m->m_pkthdr.rcvif = ifp;
148894405Simp
1489109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
149046492Swpaul
1491109323Ssam#if NBPFILTER > 0
1492109323Ssam	if (sc->sc_drvbpf) {
1493109323Ssam		struct mbuf *mb;
1494100876Simp
1495119784Ssam		/* XXX pre-allocate space when setting up recv's */
1496111119Simp		MGETHDR(mb, M_DONTWAIT, m->m_type);
1497109323Ssam		if (mb != NULL) {
1498119784Ssam			/* XXX replace divide by table */
1499119784Ssam			sc->sc_rx_th.wr_rate = frmhdr.wi_rx_rate / 5;
1500119784Ssam			sc->sc_rx_th.wr_antsignal =
1501119784Ssam				WI_RSSI_TO_DBM(sc, frmhdr.wi_rx_signal);
1502119784Ssam			sc->sc_rx_th.wr_antnoise =
1503119784Ssam				WI_RSSI_TO_DBM(sc, frmhdr.wi_rx_silence);
1504119784Ssam			sc->sc_rx_th.wr_time =
1505119784Ssam				htole32((frmhdr.wi_rx_tstamp1 << 16) |
1506119784Ssam					frmhdr.wi_rx_tstamp0);
1507119784Ssam
1508111119Simp			(void) m_dup_pkthdr(mb, m, M_DONTWAIT);
1509109323Ssam			mb->m_next = m;
1510119784Ssam			mb->m_data = (caddr_t)&sc->sc_rx_th;
1511119784Ssam			mb->m_len = sizeof(sc->sc_rx_th);
1512109323Ssam			mb->m_pkthdr.len += mb->m_len;
1513109323Ssam			bpf_mtap(sc->sc_drvbpf, mb);
1514109323Ssam			m_free(mb);
151591695Simp		}
151656965Swpaul	}
1517109323Ssam#endif
1518109323Ssam	wh = mtod(m, struct ieee80211_frame *);
1519109323Ssam	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1520109323Ssam		/*
1521109323Ssam		 * WEP is decrypted by hardware. Clear WEP bit
1522109323Ssam		 * header for ieee80211_input().
1523109323Ssam		 */
1524109323Ssam		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1525109323Ssam	}
152656965Swpaul
1527109323Ssam	/* synchronize driver's BSSID with firmware's BSSID */
1528109323Ssam	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1529109323Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1530109323Ssam		wi_sync_bssid(sc, wh->i_addr3);
153146492Swpaul
1532119150Ssam	/*
1533119150Ssam	 * Locate the node for sender, track state, and
1534119150Ssam	 * then pass this node (referenced) up to the 802.11
1535119150Ssam	 * layer for its use.  We are required to pass
1536119150Ssam	 * something so we fallback to ic_bss when this frame
1537119150Ssam	 * is from an unknown sender.
1538119150Ssam	 */
1539119150Ssam	if (ic->ic_opmode != IEEE80211_M_STA) {
1540119150Ssam		ni = ieee80211_find_node(ic, wh->i_addr2);
1541119150Ssam		if (ni == NULL)
1542119150Ssam			ni = ieee80211_ref_node(ic->ic_bss);
1543119150Ssam	} else
1544119150Ssam		ni = ieee80211_ref_node(ic->ic_bss);
1545119150Ssam	/*
1546119150Ssam	 * Send frame up for processing.
1547119150Ssam	 */
1548119150Ssam	ieee80211_input(ifp, m, ni, rssi, rstamp);
1549119150Ssam	/*
1550119150Ssam	 * The frame may have caused the node to be marked for
1551119150Ssam	 * reclamation (e.g. in response to a DEAUTH message)
1552119150Ssam	 * so use free_node here instead of unref_node.
1553119150Ssam	 */
1554119150Ssam	if (ni == ic->ic_bss)
1555119150Ssam		ieee80211_unref_node(&ni);
1556119150Ssam	else
1557119150Ssam		ieee80211_free_node(ic, ni);
1558109323Ssam}
155946492Swpaul
1560109323Ssamstatic void
1561109323Ssamwi_tx_ex_intr(struct wi_softc *sc)
1562109323Ssam{
1563109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1564109323Ssam	struct ifnet *ifp = &ic->ic_if;
1565109323Ssam	struct wi_frame frmhdr;
1566109323Ssam	int fid;
156746492Swpaul
1568109323Ssam	fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1569109323Ssam	/* Read in the frame header */
1570109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1571109323Ssam		u_int16_t status = le16toh(frmhdr.wi_status);
157246492Swpaul
1573109323Ssam		/*
1574109323Ssam		 * Spontaneous station disconnects appear as xmit
1575109323Ssam		 * errors.  Don't announce them and/or count them
1576109323Ssam		 * as an output error.
1577109323Ssam		 */
1578109323Ssam		if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1579109323Ssam			if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1580109323Ssam				if_printf(ifp, "tx failed");
1581109323Ssam				if (status & WI_TXSTAT_RET_ERR)
1582109323Ssam					printf(", retry limit exceeded");
1583109323Ssam				if (status & WI_TXSTAT_AGED_ERR)
1584109323Ssam					printf(", max transmit lifetime exceeded");
1585109323Ssam				if (status & WI_TXSTAT_DISCONNECT)
1586109323Ssam					printf(", port disconnected");
1587109323Ssam				if (status & WI_TXSTAT_FORM_ERR)
1588109323Ssam					printf(", invalid format (data len %u src %6D)",
1589109323Ssam						le16toh(frmhdr.wi_dat_len),
1590109323Ssam						frmhdr.wi_ehdr.ether_shost, ":");
1591109323Ssam				if (status & ~0xf)
1592109323Ssam					printf(", status=0x%x", status);
1593109323Ssam				printf("\n");
1594109323Ssam			}
1595109323Ssam			ifp->if_oerrors++;
1596109323Ssam		} else {
1597109323Ssam			DPRINTF(("port disconnected\n"));
1598109323Ssam			ifp->if_collisions++;	/* XXX */
1599109323Ssam		}
1600109323Ssam	} else
1601109323Ssam		DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
1602109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1603109323Ssam}
160446492Swpaul
1605109323Ssamstatic void
1606109323Ssamwi_tx_intr(struct wi_softc *sc)
1607109323Ssam{
1608109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1609109323Ssam	struct ifnet *ifp = &ic->ic_if;
1610109323Ssam	int fid, cur;
161194405Simp
1612123098Simp	if (sc->wi_gone)
1613123098Simp		return;
1614123098Simp
1615109323Ssam	fid = CSR_READ_2(sc, WI_ALLOC_FID);
1616109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
161746492Swpaul
1618109323Ssam	cur = sc->sc_txcur;
1619109323Ssam	if (sc->sc_txd[cur].d_fid != fid) {
1620109323Ssam		if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
1621109323Ssam		    fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
1622109323Ssam		return;
1623109323Ssam	}
1624109323Ssam	sc->sc_tx_timer = 0;
1625109323Ssam	sc->sc_txd[cur].d_len = 0;
1626112363Simp	sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
1627109323Ssam	if (sc->sc_txd[cur].d_len == 0)
1628109323Ssam		ifp->if_flags &= ~IFF_OACTIVE;
1629109323Ssam	else {
1630109323Ssam		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1631109323Ssam		    0, 0)) {
1632109323Ssam			if_printf(ifp, "xmit failed\n");
1633109323Ssam			sc->sc_txd[cur].d_len = 0;
1634109323Ssam		} else {
1635109323Ssam			sc->sc_tx_timer = 5;
1636109323Ssam			ifp->if_timer = 1;
1637109323Ssam		}
1638109323Ssam	}
163946492Swpaul}
164046492Swpaul
164188546Salfredstatic void
1642109323Ssamwi_info_intr(struct wi_softc *sc)
164394405Simp{
1644109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1645109323Ssam	struct ifnet *ifp = &ic->ic_if;
1646109323Ssam	int i, fid, len, off;
1647109323Ssam	u_int16_t ltbuf[2];
1648109323Ssam	u_int16_t stat;
1649109323Ssam	u_int32_t *ptr;
165094405Simp
1651109323Ssam	fid = CSR_READ_2(sc, WI_INFO_FID);
1652109323Ssam	wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
165394405Simp
1654109323Ssam	switch (le16toh(ltbuf[1])) {
165594405Simp
1656109323Ssam	case WI_INFO_LINK_STAT:
1657109323Ssam		wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1658109323Ssam		DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1659109323Ssam		switch (le16toh(stat)) {
1660109323Ssam		case WI_INFO_LINK_STAT_CONNECTED:
1661109323Ssam			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1662109323Ssam			if (ic->ic_state == IEEE80211_S_RUN &&
1663109323Ssam			    ic->ic_opmode != IEEE80211_M_IBSS)
1664109323Ssam				break;
1665109323Ssam			/* FALLTHROUGH */
1666109323Ssam		case WI_INFO_LINK_STAT_AP_CHG:
1667117812Ssam			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1668109323Ssam			break;
1669109323Ssam		case WI_INFO_LINK_STAT_AP_INR:
1670109323Ssam			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1671109323Ssam			break;
1672109323Ssam		case WI_INFO_LINK_STAT_AP_OOR:
1673109323Ssam			if (sc->sc_firmware_type == WI_SYMBOL &&
1674109323Ssam			    sc->sc_scan_timer > 0) {
1675109323Ssam				if (wi_cmd(sc, WI_CMD_INQUIRE,
1676109323Ssam				    WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
1677109323Ssam					sc->sc_scan_timer = 0;
1678109323Ssam				break;
1679109323Ssam			}
1680109323Ssam			if (ic->ic_opmode == IEEE80211_M_STA)
1681109323Ssam				sc->sc_flags |= WI_FLAGS_OUTRANGE;
1682109323Ssam			break;
1683109323Ssam		case WI_INFO_LINK_STAT_DISCONNECTED:
1684109323Ssam		case WI_INFO_LINK_STAT_ASSOC_FAILED:
1685109323Ssam			if (ic->ic_opmode == IEEE80211_M_STA)
1686117812Ssam				ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1687109323Ssam			break;
1688109323Ssam		}
1689109323Ssam		break;
169094405Simp
1691109323Ssam	case WI_INFO_COUNTERS:
1692109323Ssam		/* some card versions have a larger stats structure */
1693109323Ssam		len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1694109323Ssam		ptr = (u_int32_t *)&sc->sc_stats;
1695109323Ssam		off = sizeof(ltbuf);
1696109323Ssam		for (i = 0; i < len; i++, off += 2, ptr++) {
1697109323Ssam			wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1698109323Ssam#ifdef WI_HERMES_STATS_WAR
1699109323Ssam			if (stat & 0xf000)
1700109323Ssam				stat = ~stat;
1701109323Ssam#endif
1702109323Ssam			*ptr += stat;
1703109323Ssam		}
1704109323Ssam		ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1705109323Ssam		    sc->sc_stats.wi_tx_multi_retries +
1706109323Ssam		    sc->sc_stats.wi_tx_retry_limit;
1707109323Ssam		break;
1708109323Ssam
1709109323Ssam	case WI_INFO_SCAN_RESULTS:
1710109323Ssam	case WI_INFO_HOST_SCAN_RESULTS:
1711109323Ssam		wi_scan_result(sc, fid, le16toh(ltbuf[0]));
1712109323Ssam		break;
1713109323Ssam
1714109323Ssam	default:
1715109323Ssam		DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1716109323Ssam		    le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1717109323Ssam		break;
171894405Simp	}
1719109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1720109323Ssam}
172194405Simp
1722109323Ssamstatic int
1723109323Ssamwi_write_multi(struct wi_softc *sc)
1724109323Ssam{
1725109323Ssam	struct ifnet *ifp = &sc->sc_ic.ic_if;
1726109323Ssam	int n;
1727109323Ssam	struct ifmultiaddr *ifma;
1728109323Ssam	struct wi_mcast mlist;
172994472Simp
1730109323Ssam	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1731109323Ssamallmulti:
1732109323Ssam		memset(&mlist, 0, sizeof(mlist));
1733109323Ssam		return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1734109323Ssam		    sizeof(mlist));
173594405Simp	}
173694405Simp
1737109323Ssam	n = 0;
1738109323Ssam#if __FreeBSD_version < 500000
1739109323Ssam	LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1740109323Ssam#else
1741109323Ssam	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1742109323Ssam#endif
1743109323Ssam		if (ifma->ifma_addr->sa_family != AF_LINK)
1744109323Ssam			continue;
1745109323Ssam		if (n >= 16)
1746109323Ssam			goto allmulti;
1747109323Ssam		IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1748109323Ssam		    (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1749109323Ssam		n++;
175094405Simp	}
1751109323Ssam	return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1752109323Ssam	    IEEE80211_ADDR_LEN * n);
175394405Simp}
175494405Simp
175594405Simpstatic void
1756109323Ssamwi_read_nicid(struct wi_softc *sc)
175746492Swpaul{
1758109323Ssam	struct wi_card_ident *id;
1759109323Ssam	char *p;
1760109323Ssam	int len;
1761109323Ssam	u_int16_t ver[4];
176246492Swpaul
1763109323Ssam	/* getting chip identity */
1764109323Ssam	memset(ver, 0, sizeof(ver));
1765109323Ssam	len = sizeof(ver);
1766109323Ssam	wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1767109323Ssam	device_printf(sc->sc_dev, "using ");
176846492Swpaul
1769109323Ssam	sc->sc_firmware_type = WI_NOTYPE;
1770109323Ssam	for (id = wi_card_ident; id->card_name != NULL; id++) {
1771109323Ssam		if (le16toh(ver[0]) == id->card_id) {
1772109323Ssam			printf("%s", id->card_name);
1773109323Ssam			sc->sc_firmware_type = id->firm_type;
1774109323Ssam			break;
1775109323Ssam		}
177667092Swpaul	}
1777109323Ssam	if (sc->sc_firmware_type == WI_NOTYPE) {
1778109323Ssam		if (le16toh(ver[0]) & 0x8000) {
1779109323Ssam			printf("Unknown PRISM2 chip");
1780109323Ssam			sc->sc_firmware_type = WI_INTERSIL;
1781109323Ssam		} else {
1782109323Ssam			printf("Unknown Lucent chip");
1783109323Ssam			sc->sc_firmware_type = WI_LUCENT;
1784109323Ssam		}
178567092Swpaul	}
178646492Swpaul
1787109323Ssam	/* get primary firmware version (Only Prism chips) */
1788109323Ssam	if (sc->sc_firmware_type != WI_LUCENT) {
1789109323Ssam		memset(ver, 0, sizeof(ver));
1790109323Ssam		len = sizeof(ver);
1791109323Ssam		wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1792109323Ssam		sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1793109323Ssam		    le16toh(ver[3]) * 100 + le16toh(ver[1]);
179467092Swpaul	}
179546492Swpaul
1796109323Ssam	/* get station firmware version */
1797109323Ssam	memset(ver, 0, sizeof(ver));
1798109323Ssam	len = sizeof(ver);
1799109323Ssam	wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1800109323Ssam	sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1801109323Ssam	    le16toh(ver[3]) * 100 + le16toh(ver[1]);
1802109323Ssam	if (sc->sc_firmware_type == WI_INTERSIL &&
1803109323Ssam	    (sc->sc_sta_firmware_ver == 10102 ||
1804109323Ssam	     sc->sc_sta_firmware_ver == 20102)) {
1805109323Ssam		char ident[12];
1806109323Ssam		memset(ident, 0, sizeof(ident));
1807109323Ssam		len = sizeof(ident);
1808109323Ssam		/* value should be the format like "V2.00-11" */
1809109323Ssam		if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1810109323Ssam		    *(p = (char *)ident) >= 'A' &&
1811109323Ssam		    p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1812109323Ssam			sc->sc_firmware_type = WI_SYMBOL;
1813109323Ssam			sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1814109323Ssam			    (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1815109323Ssam			    (p[6] - '0') * 10 + (p[7] - '0');
181694405Simp		}
181794405Simp	}
1818109323Ssam	printf("\n");
1819109323Ssam	device_printf(sc->sc_dev, "%s Firmware: ",
1820109323Ssam	     sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
1821109323Ssam	    (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
1822109323Ssam	if (sc->sc_firmware_type != WI_LUCENT)	/* XXX */
1823109323Ssam		printf("Primary (%u.%u.%u), ",
1824109323Ssam		    sc->sc_pri_firmware_ver / 10000,
1825109323Ssam		    (sc->sc_pri_firmware_ver % 10000) / 100,
1826109323Ssam		    sc->sc_pri_firmware_ver % 100);
1827109323Ssam	printf("Station (%u.%u.%u)\n",
1828109323Ssam	    sc->sc_sta_firmware_ver / 10000,
1829109323Ssam	    (sc->sc_sta_firmware_ver % 10000) / 100,
1830109323Ssam	    sc->sc_sta_firmware_ver % 100);
1831109323Ssam}
183246492Swpaul
1833109323Ssamstatic int
1834109323Ssamwi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1835109323Ssam{
1836109323Ssam	struct wi_ssid ssid;
183746492Swpaul
1838109323Ssam	if (buflen > IEEE80211_NWID_LEN)
1839109323Ssam		return ENOBUFS;
1840109323Ssam	memset(&ssid, 0, sizeof(ssid));
1841109323Ssam	ssid.wi_len = htole16(buflen);
1842109323Ssam	memcpy(ssid.wi_ssid, buf, buflen);
1843109323Ssam	return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1844109323Ssam}
184546492Swpaul
1846109323Ssamstatic int
1847109323Ssamwi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
1848109323Ssam{
1849109323Ssam	struct wi_softc *sc = ifp->if_softc;
1850109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1851109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
1852109323Ssam	struct wi_req wreq;
1853116898Ssam	struct wi_scan_res *res;
1854122015Sgreen	size_t reslen;
1855116898Ssam	int len, n, error, mif, val, off, i;
185646492Swpaul
1857109323Ssam	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1858109323Ssam	if (error)
1859109323Ssam		return error;
1860109323Ssam	len = (wreq.wi_len - 1) * 2;
1861109323Ssam	if (len < sizeof(u_int16_t))
1862109323Ssam		return ENOSPC;
1863109323Ssam	if (len > sizeof(wreq.wi_val))
1864109323Ssam		len = sizeof(wreq.wi_val);
186546492Swpaul
1866109323Ssam	switch (wreq.wi_type) {
186746492Swpaul
1868109323Ssam	case WI_RID_IFACE_STATS:
1869109323Ssam		memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
1870109323Ssam		if (len < sizeof(sc->sc_stats))
1871109323Ssam			error = ENOSPC;
1872109323Ssam		else
1873109323Ssam			len = sizeof(sc->sc_stats);
1874109323Ssam		break;
187546492Swpaul
1876109323Ssam	case WI_RID_ENCRYPTION:
1877109323Ssam	case WI_RID_TX_CRYPT_KEY:
1878109323Ssam	case WI_RID_DEFLT_CRYPT_KEYS:
1879109323Ssam	case WI_RID_TX_RATE:
1880109323Ssam		return ieee80211_cfgget(ifp, cmd, data);
188146492Swpaul
1882109323Ssam	case WI_RID_MICROWAVE_OVEN:
1883109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
1884109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1885109323Ssam			    &len);
1886109323Ssam			break;
1887109323Ssam		}
1888109323Ssam		wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
1889109323Ssam		len = sizeof(u_int16_t);
1890109323Ssam		break;
189146492Swpaul
1892109323Ssam	case WI_RID_DBM_ADJUST:
1893109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
1894109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1895109323Ssam			    &len);
1896109323Ssam			break;
1897109323Ssam		}
1898119784Ssam		wreq.wi_val[0] = htole16(sc->sc_dbm_offset);
1899109323Ssam		len = sizeof(u_int16_t);
1900109323Ssam		break;
190146492Swpaul
1902109323Ssam	case WI_RID_ROAMING_MODE:
1903109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
1904109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1905109323Ssam			    &len);
1906109323Ssam			break;
1907109323Ssam		}
1908109323Ssam		wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
1909109323Ssam		len = sizeof(u_int16_t);
1910109323Ssam		break;
191146492Swpaul
1912109323Ssam	case WI_RID_SYSTEM_SCALE:
1913109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
1914109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1915109323Ssam			    &len);
1916109323Ssam			break;
1917109323Ssam		}
1918109323Ssam		wreq.wi_val[0] = htole16(sc->sc_system_scale);
1919109323Ssam		len = sizeof(u_int16_t);
1920109323Ssam		break;
192146492Swpaul
1922109323Ssam	case WI_RID_FRAG_THRESH:
1923109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
1924109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1925109323Ssam			    &len);
1926109323Ssam			break;
1927109323Ssam		}
1928116951Ssam		wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
1929109323Ssam		len = sizeof(u_int16_t);
1930109323Ssam		break;
193146492Swpaul
1932109323Ssam	case WI_RID_READ_APS:
1933109323Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1934109323Ssam			return ieee80211_cfgget(ifp, cmd, data);
1935109323Ssam		if (sc->sc_scan_timer > 0) {
1936109323Ssam			error = EINPROGRESS;
1937109323Ssam			break;
1938109323Ssam		}
1939109323Ssam		n = sc->sc_naps;
1940109323Ssam		if (len < sizeof(n)) {
1941109323Ssam			error = ENOSPC;
1942109323Ssam			break;
1943109323Ssam		}
1944109323Ssam		if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
1945109323Ssam			n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
1946109323Ssam		len = sizeof(n) + sizeof(struct wi_apinfo) * n;
1947109323Ssam		memcpy(wreq.wi_val, &n, sizeof(n));
1948109323Ssam		memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
1949109323Ssam		    sizeof(struct wi_apinfo) * n);
1950109323Ssam		break;
195146492Swpaul
1952109323Ssam	case WI_RID_PRISM2:
1953109323Ssam		wreq.wi_val[0] = sc->sc_firmware_type != WI_LUCENT;
1954109323Ssam		len = sizeof(u_int16_t);
1955109323Ssam		break;
195646492Swpaul
1957109323Ssam	case WI_RID_MIF:
1958109323Ssam		mif = wreq.wi_val[0];
1959109323Ssam		error = wi_cmd(sc, WI_CMD_READMIF, mif, 0, 0);
1960109323Ssam		val = CSR_READ_2(sc, WI_RESP0);
1961109323Ssam		wreq.wi_val[0] = val;
1962109323Ssam		len = sizeof(u_int16_t);
1963109323Ssam		break;
196446492Swpaul
1965109323Ssam	case WI_RID_ZERO_CACHE:
1966109323Ssam	case WI_RID_PROCFRAME:		/* ignore for compatibility */
1967109323Ssam		/* XXX ??? */
1968109323Ssam		break;
196946492Swpaul
1970109323Ssam	case WI_RID_READ_CACHE:
1971109323Ssam		return ieee80211_cfgget(ifp, cmd, data);
197246492Swpaul
1973116898Ssam	case WI_RID_SCAN_RES:		/* compatibility interface */
1974116898Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1975116898Ssam			return ieee80211_cfgget(ifp, cmd, data);
1976116898Ssam		if (sc->sc_scan_timer > 0) {
1977116898Ssam			error = EINPROGRESS;
1978116898Ssam			break;
1979116898Ssam		}
1980116898Ssam		n = sc->sc_naps;
1981122015Sgreen		if (sc->sc_firmware_type == WI_LUCENT) {
1982122015Sgreen			off = 0;
1983122015Sgreen			reslen = WI_WAVELAN_RES_SIZE;
1984122015Sgreen		} else {
1985122015Sgreen			off = sizeof(struct wi_scan_p2_hdr);
1986122015Sgreen			reslen = WI_PRISM2_RES_SIZE;
1987122015Sgreen		}
1988122015Sgreen		if (len < off + reslen * n)
1989122015Sgreen			n = (len - off) / reslen;
1990122015Sgreen		len = off + reslen * n;
1991116898Ssam		if (off != 0) {
1992116898Ssam			struct wi_scan_p2_hdr *p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1993116898Ssam			/*
1994116898Ssam			 * Prepend Prism-specific header.
1995116898Ssam			 */
1996116898Ssam			if (len < sizeof(struct wi_scan_p2_hdr)) {
1997116898Ssam				error = ENOSPC;
1998116898Ssam				break;
1999116898Ssam			}
2000116898Ssam			p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
2001116898Ssam			p2->wi_rsvd = 0;
2002116898Ssam			p2->wi_reason = n;	/* XXX */
2003116898Ssam		}
2004122015Sgreen		for (i = 0; i < n; i++, off += reslen) {
2005116898Ssam			const struct wi_apinfo *ap = &sc->sc_aps[i];
2006116898Ssam
2007116898Ssam			res = (struct wi_scan_res *)((char *)wreq.wi_val + off);
2008116898Ssam			res->wi_chan = ap->channel;
2009116898Ssam			res->wi_noise = ap->noise;
2010116898Ssam			res->wi_signal = ap->signal;
2011116898Ssam			IEEE80211_ADDR_COPY(res->wi_bssid, ap->bssid);
2012116898Ssam			res->wi_interval = ap->interval;
2013116898Ssam			res->wi_capinfo = ap->capinfo;
2014116898Ssam			res->wi_ssid_len = ap->namelen;
2015116898Ssam			memcpy(res->wi_ssid, ap->name,
2016116898Ssam				IEEE80211_NWID_LEN);
2017116898Ssam			if (sc->sc_firmware_type != WI_LUCENT) {
2018116898Ssam				/* XXX not saved from Prism cards */
2019116898Ssam				memset(res->wi_srates, 0,
2020116898Ssam					sizeof(res->wi_srates));
2021116898Ssam				res->wi_rate = ap->rate;
2022116898Ssam				res->wi_rsvd = 0;
2023122015Sgreen			}
2024116898Ssam		}
2025116898Ssam		break;
2026116898Ssam
2027109323Ssam	default:
2028109323Ssam		if (sc->sc_enabled) {
2029109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2030109323Ssam			    &len);
2031109323Ssam			break;
2032109323Ssam		}
2033109323Ssam		switch (wreq.wi_type) {
2034109323Ssam		case WI_RID_MAX_DATALEN:
2035109323Ssam			wreq.wi_val[0] = htole16(sc->sc_max_datalen);
2036109323Ssam			len = sizeof(u_int16_t);
2037109323Ssam			break;
2038109323Ssam		case WI_RID_RTS_THRESH:
2039116951Ssam			wreq.wi_val[0] = htole16(ic->ic_rtsthreshold);
2040109323Ssam			len = sizeof(u_int16_t);
2041109323Ssam			break;
2042109323Ssam		case WI_RID_CNFAUTHMODE:
2043109323Ssam			wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
2044109323Ssam			len = sizeof(u_int16_t);
2045109323Ssam			break;
2046109323Ssam		case WI_RID_NODENAME:
2047109323Ssam			if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
2048109323Ssam				error = ENOSPC;
2049109323Ssam				break;
2050109323Ssam			}
2051109323Ssam			len = sc->sc_nodelen + sizeof(u_int16_t);
2052109323Ssam			wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
2053109323Ssam			memcpy(&wreq.wi_val[1], sc->sc_nodename,
2054109323Ssam			    sc->sc_nodelen);
2055109323Ssam			break;
2056109323Ssam		default:
2057109323Ssam			return ieee80211_cfgget(ifp, cmd, data);
2058109323Ssam		}
2059109323Ssam		break;
206046492Swpaul	}
2061109323Ssam	if (error)
2062109323Ssam		return error;
2063109323Ssam	wreq.wi_len = (len + 1) / 2 + 1;
2064109323Ssam	return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
206546492Swpaul}
206646492Swpaul
2067109323Ssamstatic int
2068109323Ssamwi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
206946492Swpaul{
2070109323Ssam	struct wi_softc *sc = ifp->if_softc;
2071109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2072109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
2073109323Ssam	struct wi_req wreq;
2074109323Ssam	struct mbuf *m;
2075109323Ssam	int i, len, error, mif, val;
2076116951Ssam	struct ieee80211_rateset *rs;
207746492Swpaul
2078109323Ssam	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2079109323Ssam	if (error)
2080109323Ssam		return error;
2081116898Ssam	len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0;
2082109323Ssam	switch (wreq.wi_type) {
2083109323Ssam	case WI_RID_DBM_ADJUST:
2084109323Ssam		return ENODEV;
208567092Swpaul
2086109323Ssam	case WI_RID_NODENAME:
2087109323Ssam		if (le16toh(wreq.wi_val[0]) * 2 > len ||
2088109323Ssam		    le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
2089109323Ssam			error = ENOSPC;
2090109323Ssam			break;
2091109323Ssam		}
2092109323Ssam		if (sc->sc_enabled) {
2093109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2094109323Ssam			    len);
2095109323Ssam			if (error)
2096109323Ssam				break;
2097109323Ssam		}
2098109323Ssam		sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
2099109323Ssam		memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen);
2100109323Ssam		break;
210146492Swpaul
2102109323Ssam	case WI_RID_MICROWAVE_OVEN:
2103109323Ssam	case WI_RID_ROAMING_MODE:
2104109323Ssam	case WI_RID_SYSTEM_SCALE:
2105109323Ssam	case WI_RID_FRAG_THRESH:
2106109323Ssam		if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
2107109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
2108109323Ssam			break;
2109109323Ssam		if (wreq.wi_type == WI_RID_ROAMING_MODE &&
2110109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
2111109323Ssam			break;
2112109323Ssam		if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
2113109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
2114109323Ssam			break;
2115109323Ssam		if (wreq.wi_type == WI_RID_FRAG_THRESH &&
2116109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
2117109323Ssam			break;
2118109323Ssam		/* FALLTHROUGH */
2119109323Ssam	case WI_RID_RTS_THRESH:
2120109323Ssam	case WI_RID_CNFAUTHMODE:
2121109323Ssam	case WI_RID_MAX_DATALEN:
2122109323Ssam		if (sc->sc_enabled) {
2123109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2124109323Ssam			    sizeof(u_int16_t));
2125109323Ssam			if (error)
2126109323Ssam				break;
2127109323Ssam		}
2128109323Ssam		switch (wreq.wi_type) {
2129109323Ssam		case WI_RID_FRAG_THRESH:
2130116951Ssam			ic->ic_fragthreshold = le16toh(wreq.wi_val[0]);
2131109323Ssam			break;
2132109323Ssam		case WI_RID_RTS_THRESH:
2133116951Ssam			ic->ic_rtsthreshold = le16toh(wreq.wi_val[0]);
2134109323Ssam			break;
2135109323Ssam		case WI_RID_MICROWAVE_OVEN:
2136109323Ssam			sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
2137109323Ssam			break;
2138109323Ssam		case WI_RID_ROAMING_MODE:
2139109323Ssam			sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
2140109323Ssam			break;
2141109323Ssam		case WI_RID_SYSTEM_SCALE:
2142109323Ssam			sc->sc_system_scale = le16toh(wreq.wi_val[0]);
2143109323Ssam			break;
2144109323Ssam		case WI_RID_CNFAUTHMODE:
2145109323Ssam			sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
2146109323Ssam			break;
2147109323Ssam		case WI_RID_MAX_DATALEN:
2148109323Ssam			sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
2149109323Ssam			break;
2150109323Ssam		}
2151109323Ssam		break;
215294405Simp
2153109323Ssam	case WI_RID_TX_RATE:
2154109323Ssam		switch (le16toh(wreq.wi_val[0])) {
2155109323Ssam		case 3:
2156109323Ssam			ic->ic_fixed_rate = -1;
2157109323Ssam			break;
2158109323Ssam		default:
2159116951Ssam			rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
2160116951Ssam			for (i = 0; i < rs->rs_nrates; i++) {
2161116951Ssam				if ((rs->rs_rates[i] & IEEE80211_RATE_VAL)
2162109323Ssam				    / 2 == le16toh(wreq.wi_val[0]))
2163109323Ssam					break;
2164109323Ssam			}
2165116951Ssam			if (i == rs->rs_nrates)
2166109323Ssam				return EINVAL;
2167109323Ssam			ic->ic_fixed_rate = i;
2168109323Ssam		}
2169109323Ssam		if (sc->sc_enabled)
2170109323Ssam			error = wi_write_txrate(sc);
2171109323Ssam		break;
217246492Swpaul
2173109323Ssam	case WI_RID_SCAN_APS:
2174109323Ssam		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2175116898Ssam			error = wi_scan_ap(sc, 0x3fff, 0x000f);
2176109323Ssam		break;
217746492Swpaul
2178116898Ssam	case WI_RID_SCAN_REQ:		/* compatibility interface */
2179116898Ssam		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2180116898Ssam			error = wi_scan_ap(sc, wreq.wi_val[0], wreq.wi_val[1]);
2181116898Ssam		break;
2182116898Ssam
2183109323Ssam	case WI_RID_MGMT_XMIT:
2184109323Ssam		if (!sc->sc_enabled) {
2185109323Ssam			error = ENETDOWN;
2186109323Ssam			break;
2187109323Ssam		}
2188109323Ssam		if (ic->ic_mgtq.ifq_len > 5) {
2189109323Ssam			error = EAGAIN;
2190109323Ssam			break;
2191109323Ssam		}
2192109323Ssam		/* XXX wi_len looks in u_int8_t, not in u_int16_t */
2193109323Ssam		m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL);
2194109323Ssam		if (m == NULL) {
2195109323Ssam			error = ENOMEM;
2196109323Ssam			break;
2197109323Ssam		}
2198109323Ssam		IF_ENQUEUE(&ic->ic_mgtq, m);
2199109323Ssam		break;
220046492Swpaul
2201109323Ssam	case WI_RID_MIF:
2202109323Ssam		mif = wreq.wi_val[0];
2203109323Ssam		val = wreq.wi_val[1];
2204109323Ssam		error = wi_cmd(sc, WI_CMD_WRITEMIF, mif, val, 0);
2205109323Ssam		break;
220646492Swpaul
2207109323Ssam	case WI_RID_PROCFRAME:		/* ignore for compatibility */
2208109323Ssam		break;
2209109323Ssam
2210116898Ssam	case WI_RID_OWN_SSID:
2211116898Ssam		if (le16toh(wreq.wi_val[0]) * 2 > len ||
2212116898Ssam		    le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) {
2213116898Ssam			error = ENOSPC;
2214116898Ssam			break;
2215116898Ssam		}
2216116898Ssam		memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
2217116898Ssam		ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2;
2218116898Ssam		memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen);
2219116898Ssam		error = ENETRESET;
2220116898Ssam		break;
2221116898Ssam
2222109323Ssam	default:
2223109323Ssam		if (sc->sc_enabled) {
2224109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2225109323Ssam			    len);
2226109323Ssam			if (error)
2227109323Ssam				break;
2228109323Ssam		}
2229109323Ssam		error = ieee80211_cfgset(ifp, cmd, data);
2230109323Ssam		break;
2231109323Ssam	}
2232109323Ssam	return error;
223346492Swpaul}
223446492Swpaul
2235109323Ssamstatic int
2236109323Ssamwi_write_txrate(struct wi_softc *sc)
223746492Swpaul{
2238109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2239109323Ssam	int i;
2240109323Ssam	u_int16_t rate;
224146492Swpaul
2242109323Ssam	if (ic->ic_fixed_rate < 0)
2243109323Ssam		rate = 0;	/* auto */
2244109323Ssam	else
2245116951Ssam		rate = (ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ic->ic_fixed_rate] &
2246109323Ssam		    IEEE80211_RATE_VAL) / 2;
224746492Swpaul
2248109323Ssam	/* rate: 0, 1, 2, 5, 11 */
224946492Swpaul
2250109323Ssam	switch (sc->sc_firmware_type) {
2251109323Ssam	case WI_LUCENT:
2252112501Simp		switch (rate) {
2253112501Simp		case 0:			/* auto == 11mbps auto */
2254112501Simp			rate = 3;
2255112501Simp			break;
2256112501Simp		/* case 1, 2 map to 1, 2*/
2257112501Simp		case 5:			/* 5.5Mbps -> 4 */
2258112501Simp			rate = 4;
2259112501Simp			break;
2260112501Simp		case 11:		/* 11mbps -> 5 */
2261112501Simp			rate = 5;
2262112501Simp			break;
2263112501Simp		default:
2264112501Simp			break;
2265112501Simp		}
2266109323Ssam		break;
2267109323Ssam	default:
2268109323Ssam		/* Choose a bit according to this table.
2269109323Ssam		 *
2270109323Ssam		 * bit | data rate
2271109323Ssam		 * ----+-------------------
2272109323Ssam		 * 0   | 1Mbps
2273109323Ssam		 * 1   | 2Mbps
2274109323Ssam		 * 2   | 5.5Mbps
2275109323Ssam		 * 3   | 11Mbps
2276109323Ssam		 */
2277109323Ssam		for (i = 8; i > 0; i >>= 1) {
2278109323Ssam			if (rate >= i)
2279109323Ssam				break;
2280109323Ssam		}
2281109323Ssam		if (i == 0)
2282109323Ssam			rate = 0xf;	/* auto */
2283109323Ssam		else
2284109323Ssam			rate = i;
2285109323Ssam		break;
2286109323Ssam	}
2287109323Ssam	return wi_write_val(sc, WI_RID_TX_RATE, rate);
2288109323Ssam}
228946492Swpaul
2290109323Ssamstatic int
2291109323Ssamwi_write_wep(struct wi_softc *sc)
2292109323Ssam{
2293109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2294109323Ssam	int error = 0;
2295109323Ssam	int i, keylen;
2296109323Ssam	u_int16_t val;
2297109323Ssam	struct wi_key wkey[IEEE80211_WEP_NKID];
229846492Swpaul
2299109323Ssam	switch (sc->sc_firmware_type) {
2300109323Ssam	case WI_LUCENT:
2301109323Ssam		val = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0;
2302109323Ssam		error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
2303109323Ssam		if (error)
2304109323Ssam			break;
2305109323Ssam		error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_wep_txkey);
2306109323Ssam		if (error)
2307109323Ssam			break;
2308109323Ssam		memset(wkey, 0, sizeof(wkey));
2309109323Ssam		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2310109323Ssam			keylen = ic->ic_nw_keys[i].wk_len;
2311109323Ssam			wkey[i].wi_keylen = htole16(keylen);
2312109323Ssam			memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
2313109323Ssam			    keylen);
2314109323Ssam		}
2315109323Ssam		error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
2316109323Ssam		    wkey, sizeof(wkey));
2317109323Ssam		break;
2318109323Ssam
2319109323Ssam	case WI_INTERSIL:
2320109323Ssam	case WI_SYMBOL:
2321109323Ssam		if (ic->ic_flags & IEEE80211_F_WEPON) {
2322109323Ssam			/*
2323109323Ssam			 * ONLY HWB3163 EVAL-CARD Firmware version
2324109323Ssam			 * less than 0.8 variant2
2325109323Ssam			 *
2326109323Ssam			 *   If promiscuous mode disable, Prism2 chip
2327109323Ssam			 *  does not work with WEP .
2328109323Ssam			 * It is under investigation for details.
2329109323Ssam			 * (ichiro@netbsd.org)
2330109323Ssam			 */
2331109323Ssam			if (sc->sc_firmware_type == WI_INTERSIL &&
2332109323Ssam			    sc->sc_sta_firmware_ver < 802 ) {
2333109323Ssam				/* firm ver < 0.8 variant 2 */
2334109323Ssam				wi_write_val(sc, WI_RID_PROMISC, 1);
2335109323Ssam			}
2336109323Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE,
2337109323Ssam			    sc->sc_cnfauthmode);
2338109323Ssam			val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
2339109323Ssam			/*
2340109323Ssam			 * Encryption firmware has a bug for HostAP mode.
2341109323Ssam			 */
2342109323Ssam			if (sc->sc_firmware_type == WI_INTERSIL &&
2343109323Ssam			    ic->ic_opmode == IEEE80211_M_HOSTAP)
2344109323Ssam				val |= HOST_ENCRYPT;
2345109323Ssam		} else {
2346109323Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE,
2347109323Ssam			    IEEE80211_AUTH_OPEN);
2348109323Ssam			val = HOST_ENCRYPT | HOST_DECRYPT;
2349109323Ssam		}
2350109323Ssam		error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
2351109323Ssam		if (error)
2352109323Ssam			break;
2353109323Ssam		error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
2354109323Ssam		    ic->ic_wep_txkey);
2355109323Ssam		if (error)
2356109323Ssam			break;
2357109323Ssam		/*
2358109323Ssam		 * It seems that the firmware accept 104bit key only if
2359109323Ssam		 * all the keys have 104bit length.  We get the length of
2360109323Ssam		 * the transmit key and use it for all other keys.
2361109323Ssam		 * Perhaps we should use software WEP for such situation.
2362109323Ssam		 */
2363109323Ssam		keylen = ic->ic_nw_keys[ic->ic_wep_txkey].wk_len;
2364109323Ssam		if (keylen > IEEE80211_WEP_KEYLEN)
2365109323Ssam			keylen = 13;	/* 104bit keys */
2366109323Ssam		else
2367109323Ssam			keylen = IEEE80211_WEP_KEYLEN;
2368109323Ssam		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2369109323Ssam			error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
2370109323Ssam			    ic->ic_nw_keys[i].wk_key, keylen);
2371109323Ssam			if (error)
2372109323Ssam				break;
2373109323Ssam		}
2374109323Ssam		break;
2375109323Ssam	}
2376109323Ssam	return error;
237746492Swpaul}
237846492Swpaul
2379109323Ssamstatic int
2380109323Ssamwi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
238146492Swpaul{
2382109323Ssam	int			i, s = 0;
2383109323Ssam	static volatile int count  = 0;
2384109323Ssam
2385123098Simp	if (sc->wi_gone)
2386123098Simp		return (ENODEV);
2387123098Simp
2388109323Ssam	if (count > 0)
2389109323Ssam		panic("Hey partner, hold on there!");
2390109323Ssam	count++;
239153702Swpaul
2392109323Ssam	/* wait for the busy bit to clear */
2393123098Simp	for (i = 500; i > 0; i--) {	/* 500ms */
2394116206Simp		if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
2395109323Ssam			break;
2396123098Simp		DELAY(1*1000);	/* 1ms */
2397109323Ssam	}
2398109323Ssam	if (i == 0) {
2399109323Ssam		device_printf(sc->sc_dev, "wi_cmd: busy bit won't clear.\n" );
2400123098Simp		sc->wi_gone = 1;
2401109323Ssam		count--;
2402109323Ssam		return(ETIMEDOUT);
2403109323Ssam	}
240490580Sbrooks
2405109323Ssam	CSR_WRITE_2(sc, WI_PARAM0, val0);
2406109323Ssam	CSR_WRITE_2(sc, WI_PARAM1, val1);
2407109323Ssam	CSR_WRITE_2(sc, WI_PARAM2, val2);
2408109323Ssam	CSR_WRITE_2(sc, WI_COMMAND, cmd);
240990580Sbrooks
2410109323Ssam	if (cmd == WI_CMD_INI) {
2411109323Ssam		/* XXX: should sleep here. */
2412116206Simp		DELAY(100*1000);		/* 100ms delay for init */
2413109323Ssam	}
2414109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
2415109323Ssam		/*
2416109323Ssam		 * Wait for 'command complete' bit to be
2417109323Ssam		 * set in the event status register.
2418109323Ssam		 */
2419109323Ssam		s = CSR_READ_2(sc, WI_EVENT_STAT);
2420109323Ssam		if (s & WI_EV_CMD) {
2421109323Ssam			/* Ack the event and read result code. */
2422109323Ssam			s = CSR_READ_2(sc, WI_STATUS);
2423109323Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
2424109323Ssam			if (s & WI_STAT_CMD_RESULT) {
2425109323Ssam				count--;
2426109323Ssam				return(EIO);
2427109323Ssam			}
2428109323Ssam			break;
242990580Sbrooks		}
2430109323Ssam		DELAY(WI_DELAY);
2431109323Ssam	}
243290580Sbrooks
2433109323Ssam	count--;
2434109323Ssam	if (i == WI_TIMEOUT) {
2435109323Ssam		device_printf(sc->sc_dev,
2436109323Ssam		    "timeout in wi_cmd 0x%04x; event status 0x%04x\n", cmd, s);
2437123098Simp		if (s == 0xffff)
2438123098Simp			sc->wi_gone = 1;
2439109323Ssam		return(ETIMEDOUT);
244053702Swpaul	}
2441109323Ssam	return (0);
2442109323Ssam}
244353702Swpaul
2444109323Ssamstatic int
2445109323Ssamwi_seek_bap(struct wi_softc *sc, int id, int off)
2446109323Ssam{
2447109323Ssam	int i, status;
244890580Sbrooks
2449109323Ssam	CSR_WRITE_2(sc, WI_SEL0, id);
2450109323Ssam	CSR_WRITE_2(sc, WI_OFF0, off);
245190580Sbrooks
2452109323Ssam	for (i = 0; ; i++) {
2453109323Ssam		status = CSR_READ_2(sc, WI_OFF0);
2454109323Ssam		if ((status & WI_OFF_BUSY) == 0)
2455109323Ssam			break;
2456109323Ssam		if (i == WI_TIMEOUT) {
2457109323Ssam			device_printf(sc->sc_dev, "timeout in wi_seek to %x/%x\n",
2458109323Ssam			    id, off);
2459109323Ssam			sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2460123098Simp			if (status == 0xffff)
2461123098Simp				sc->wi_gone = 1;
2462109323Ssam			return ETIMEDOUT;
2463109323Ssam		}
2464109323Ssam		DELAY(1);
246553702Swpaul	}
2466109323Ssam	if (status & WI_OFF_ERR) {
2467109323Ssam		device_printf(sc->sc_dev, "failed in wi_seek to %x/%x\n", id, off);
2468109323Ssam		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2469109323Ssam		return EIO;
2470109323Ssam	}
2471109323Ssam	sc->sc_bap_id = id;
2472109323Ssam	sc->sc_bap_off = off;
2473109323Ssam	return 0;
247453702Swpaul}
247553702Swpaul
2476109323Ssamstatic int
2477109323Ssamwi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
247853702Swpaul{
2479109323Ssam	u_int16_t *ptr;
2480109323Ssam	int i, error, cnt;
248153702Swpaul
2482109323Ssam	if (buflen == 0)
2483109323Ssam		return 0;
2484109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2485109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
2486109323Ssam			return error;
248775219Salfred	}
2488109323Ssam	cnt = (buflen + 1) / 2;
2489109323Ssam	ptr = (u_int16_t *)buf;
2490109323Ssam	for (i = 0; i < cnt; i++)
2491109323Ssam		*ptr++ = CSR_READ_2(sc, WI_DATA0);
2492109323Ssam	sc->sc_bap_off += cnt * 2;
2493109323Ssam	return 0;
249453702Swpaul}
249553702Swpaul
2496109323Ssamstatic int
2497109323Ssamwi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
249853702Swpaul{
2499109323Ssam	u_int16_t *ptr;
2500109323Ssam	int i, error, cnt;
250146492Swpaul
2502109323Ssam	if (buflen == 0)
2503109323Ssam		return 0;
250446492Swpaul
2505109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
2506109323Ssam  again:
2507109323Ssam#endif
2508109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2509109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
2510109323Ssam			return error;
2511109323Ssam	}
2512109323Ssam	cnt = (buflen + 1) / 2;
2513109323Ssam	ptr = (u_int16_t *)buf;
2514109323Ssam	for (i = 0; i < cnt; i++)
2515109323Ssam		CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
2516109323Ssam	sc->sc_bap_off += cnt * 2;
2517109323Ssam
2518109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
2519109323Ssam	/*
2520109323Ssam	 * According to the comments in the HCF Light code, there is a bug
2521109323Ssam	 * in the Hermes (or possibly in certain Hermes firmware revisions)
2522109323Ssam	 * where the chip's internal autoincrement counter gets thrown off
2523109323Ssam	 * during data writes:  the autoincrement is missed, causing one
2524109323Ssam	 * data word to be overwritten and subsequent words to be written to
2525109323Ssam	 * the wrong memory locations. The end result is that we could end
2526109323Ssam	 * up transmitting bogus frames without realizing it. The workaround
2527109323Ssam	 * for this is to write a couple of extra guard words after the end
2528109323Ssam	 * of the transfer, then attempt to read then back. If we fail to
2529109323Ssam	 * locate the guard words where we expect them, we preform the
2530109323Ssam	 * transfer over again.
2531109323Ssam	 */
2532109323Ssam	if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
2533109323Ssam		CSR_WRITE_2(sc, WI_DATA0, 0x1234);
2534109323Ssam		CSR_WRITE_2(sc, WI_DATA0, 0x5678);
2535109323Ssam		wi_seek_bap(sc, id, sc->sc_bap_off);
2536109323Ssam		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2537109323Ssam		if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
2538109323Ssam		    CSR_READ_2(sc, WI_DATA0) != 0x5678) {
2539109323Ssam			device_printf(sc->sc_dev,
2540109323Ssam				"detect auto increment bug, try again\n");
2541109323Ssam			goto again;
2542109323Ssam		}
2543109323Ssam	}
2544109323Ssam#endif
2545109323Ssam	return 0;
254646492Swpaul}
254753702Swpaul
2548109323Ssamstatic int
2549109323Ssamwi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
2550109323Ssam{
2551109323Ssam	int error, len;
2552109323Ssam	struct mbuf *m;
255353702Swpaul
2554109323Ssam	for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
2555109323Ssam		if (m->m_len == 0)
2556109323Ssam			continue;
255753702Swpaul
2558109323Ssam		len = min(m->m_len, totlen);
255953702Swpaul
2560109323Ssam		if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
2561109323Ssam			m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
2562109323Ssam			return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
2563109323Ssam			    totlen);
2564109323Ssam		}
256553702Swpaul
2566109323Ssam		if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
2567109323Ssam			return error;
256853702Swpaul
2569109323Ssam		off += m->m_len;
2570109323Ssam		totlen -= len;
2571109323Ssam	}
2572109323Ssam	return 0;
2573109323Ssam}
257453702Swpaul
2575109323Ssamstatic int
2576109323Ssamwi_alloc_fid(struct wi_softc *sc, int len, int *idp)
257753702Swpaul{
257853702Swpaul	int i;
257953702Swpaul
2580109323Ssam	if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
2581109323Ssam		device_printf(sc->sc_dev, "failed to allocate %d bytes on NIC\n",
2582109323Ssam		    len);
2583109323Ssam		return ENOMEM;
258453702Swpaul	}
258553702Swpaul
2586109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
2587109323Ssam		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
2588109323Ssam			break;
2589109323Ssam		if (i == WI_TIMEOUT) {
2590109323Ssam			device_printf(sc->sc_dev, "timeout in alloc\n");
2591109323Ssam			return ETIMEDOUT;
2592109323Ssam		}
2593109323Ssam		DELAY(1);
259453702Swpaul	}
2595109323Ssam	*idp = CSR_READ_2(sc, WI_ALLOC_FID);
2596109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
2597109323Ssam	return 0;
2598109323Ssam}
259953702Swpaul
2600109323Ssamstatic int
2601109323Ssamwi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
2602109323Ssam{
2603109323Ssam	int error, len;
2604109323Ssam	u_int16_t ltbuf[2];
260553702Swpaul
2606109323Ssam	/* Tell the NIC to enter record read mode. */
2607109323Ssam	error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2608109323Ssam	if (error)
2609109323Ssam		return error;
261053702Swpaul
2611109323Ssam	error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2612109323Ssam	if (error)
2613109323Ssam		return error;
261453702Swpaul
2615109323Ssam	if (le16toh(ltbuf[1]) != rid) {
2616109323Ssam		device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n",
2617109323Ssam		    rid, le16toh(ltbuf[1]));
2618109323Ssam		return EIO;
261953702Swpaul	}
2620109323Ssam	len = (le16toh(ltbuf[0]) - 1) * 2;	 /* already got rid */
2621109323Ssam	if (*buflenp < len) {
2622109323Ssam		device_printf(sc->sc_dev, "record buffer is too small, "
2623109323Ssam		    "rid=%x, size=%d, len=%d\n",
2624109323Ssam		    rid, *buflenp, len);
2625109323Ssam		return ENOSPC;
262653702Swpaul	}
2627109323Ssam	*buflenp = len;
2628109323Ssam	return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2629109323Ssam}
263053702Swpaul
2631109323Ssamstatic int
2632109323Ssamwi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2633109323Ssam{
2634109323Ssam	int error;
2635109323Ssam	u_int16_t ltbuf[2];
263653702Swpaul
2637109323Ssam	ltbuf[0] = htole16((buflen + 1) / 2 + 1);	 /* includes rid */
2638109323Ssam	ltbuf[1] = htole16(rid);
263953702Swpaul
2640109323Ssam	error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2641109323Ssam	if (error)
2642109323Ssam		return error;
2643109323Ssam	error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2644109323Ssam	if (error)
2645109323Ssam		return error;
2646102206Simp
2647109323Ssam	return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
264853702Swpaul}
264977217Sphk
265088546Salfredstatic int
2651117812Ssamwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
265277217Sphk{
2653117812Ssam	struct ifnet *ifp = &ic->ic_if;
2654117812Ssam	struct wi_softc *sc = ifp->if_softc;
2655116951Ssam	struct ieee80211_node *ni = ic->ic_bss;
2656116951Ssam	int buflen;
2657109323Ssam	u_int16_t val;
2658109323Ssam	struct wi_ssid ssid;
2659109323Ssam	u_int8_t old_bssid[IEEE80211_ADDR_LEN];
266077217Sphk
2661117812Ssam	DPRINTF(("%s: %s -> %s\n", __func__,
2662117812Ssam		ieee80211_state_name[ic->ic_state],
2663117812Ssam		ieee80211_state_name[nstate]));
2664109323Ssam
2665109323Ssam	switch (nstate) {
2666109323Ssam	case IEEE80211_S_INIT:
2667109323Ssam		ic->ic_flags &= ~IEEE80211_F_SIBSS;
2668109323Ssam		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2669117812Ssam		return (*sc->sc_newstate)(ic, nstate, arg);
2670109323Ssam
2671109323Ssam	case IEEE80211_S_RUN:
2672109323Ssam		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2673109323Ssam		buflen = IEEE80211_ADDR_LEN;
2674109323Ssam		wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen);
2675109323Ssam		IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
2676109323Ssam		buflen = sizeof(val);
2677109323Ssam		wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
2678116951Ssam		/* XXX validate channel */
2679116951Ssam		ni->ni_chan = &ic->ic_channels[le16toh(val)];
2680119784Ssam#if NBPFILTER > 0
2681119784Ssam		sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
2682119784Ssam			htole16(ni->ni_chan->ic_freq);
2683119784Ssam		sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
2684119784Ssam			htole16(ni->ni_chan->ic_flags);
2685119784Ssam#endif
2686109323Ssam
2687109323Ssam		if (IEEE80211_ADDR_EQ(old_bssid, ni->ni_bssid))
2688109323Ssam			sc->sc_false_syns++;
2689109323Ssam		else
2690109323Ssam			sc->sc_false_syns = 0;
2691109323Ssam
2692109323Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2693109323Ssam			ni->ni_esslen = ic->ic_des_esslen;
2694109323Ssam			memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
2695116951Ssam			ni->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B];
2696109323Ssam			ni->ni_intval = ic->ic_lintval;
2697109323Ssam			ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
2698109323Ssam			if (ic->ic_flags & IEEE80211_F_WEPON)
2699109323Ssam				ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
270077217Sphk		} else {
2701109323Ssam			/* XXX check return value */
2702109323Ssam			buflen = sizeof(ssid);
2703109323Ssam			wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
2704109323Ssam			ni->ni_esslen = le16toh(ssid.wi_len);
2705109323Ssam			if (ni->ni_esslen > IEEE80211_NWID_LEN)
2706109323Ssam				ni->ni_esslen = IEEE80211_NWID_LEN;	/*XXX*/
2707109323Ssam			memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
270877217Sphk		}
270977217Sphk		break;
2710109323Ssam
2711109323Ssam	case IEEE80211_S_SCAN:
2712109323Ssam	case IEEE80211_S_AUTH:
2713109323Ssam	case IEEE80211_S_ASSOC:
271477217Sphk		break;
271577217Sphk	}
271677217Sphk
2717117812Ssam	ic->ic_state = nstate;		/* NB: skip normal ieee80211 handling */
2718117812Ssam	return 0;
271977217Sphk}
272077217Sphk
272188546Salfredstatic int
2722116898Ssamwi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate)
272377217Sphk{
2724109323Ssam	int error = 0;
2725109323Ssam	u_int16_t val[2];
272677217Sphk
2727109323Ssam	if (!sc->sc_enabled)
2728109323Ssam		return ENXIO;
2729109323Ssam	switch (sc->sc_firmware_type) {
2730109323Ssam	case WI_LUCENT:
2731109323Ssam		(void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
273298440Simp		break;
2733109323Ssam	case WI_INTERSIL:
2734116898Ssam		val[0] = chanmask;	/* channel */
2735116898Ssam		val[1] = txrate;	/* tx rate */
2736109323Ssam		error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
273798440Simp		break;
2738109323Ssam	case WI_SYMBOL:
2739109323Ssam		/*
2740109323Ssam		 * XXX only supported on 3.x ?
2741109323Ssam		 */
2742109323Ssam		val[0] = BSCAN_BCAST | BSCAN_ONETIME;
2743109323Ssam		error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
2744109323Ssam		    val, sizeof(val[0]));
274598440Simp		break;
274698440Simp	}
2747109323Ssam	if (error == 0) {
2748109323Ssam		sc->sc_scan_timer = WI_SCAN_WAIT;
2749109323Ssam		sc->sc_ic.ic_if.if_timer = 1;
2750116898Ssam		DPRINTF(("wi_scan_ap: start scanning, "
2751116898Ssam			"chamask 0x%x txrate 0x%x\n", chanmask, txrate));
2752109323Ssam	}
2753109323Ssam	return error;
2754109323Ssam}
275577217Sphk
2756109323Ssamstatic void
2757109323Ssamwi_scan_result(struct wi_softc *sc, int fid, int cnt)
2758109323Ssam{
2759109323Ssam#define	N(a)	(sizeof (a) / sizeof (a[0]))
2760109323Ssam	int i, naps, off, szbuf;
2761109323Ssam	struct wi_scan_header ws_hdr;	/* Prism2 header */
2762109323Ssam	struct wi_scan_data_p2 ws_dat;	/* Prism2 scantable*/
2763109323Ssam	struct wi_apinfo *ap;
2764109323Ssam
2765109323Ssam	off = sizeof(u_int16_t) * 2;
2766109323Ssam	memset(&ws_hdr, 0, sizeof(ws_hdr));
2767109323Ssam	switch (sc->sc_firmware_type) {
2768109323Ssam	case WI_INTERSIL:
2769109323Ssam		wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
2770109323Ssam		off += sizeof(ws_hdr);
2771109323Ssam		szbuf = sizeof(struct wi_scan_data_p2);
277277217Sphk		break;
2773109323Ssam	case WI_SYMBOL:
2774109323Ssam		szbuf = sizeof(struct wi_scan_data_p2) + 6;
277577217Sphk		break;
2776109323Ssam	case WI_LUCENT:
2777109323Ssam		szbuf = sizeof(struct wi_scan_data);
277877217Sphk		break;
2779109323Ssam	default:
2780109323Ssam		device_printf(sc->sc_dev,
2781109323Ssam			"wi_scan_result: unknown firmware type %u\n",
2782109323Ssam			sc->sc_firmware_type);
2783109323Ssam		naps = 0;
2784109323Ssam		goto done;
278577217Sphk	}
2786109323Ssam	naps = (cnt * 2 + 2 - off) / szbuf;
2787109323Ssam	if (naps > N(sc->sc_aps))
2788109323Ssam		naps = N(sc->sc_aps);
2789109323Ssam	sc->sc_naps = naps;
2790109323Ssam	/* Read Data */
2791109323Ssam	ap = sc->sc_aps;
2792109323Ssam	memset(&ws_dat, 0, sizeof(ws_dat));
2793109323Ssam	for (i = 0; i < naps; i++, ap++) {
2794109323Ssam		wi_read_bap(sc, fid, off, &ws_dat,
2795109323Ssam		    (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
2796109323Ssam		DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off,
2797109323Ssam		    ether_sprintf(ws_dat.wi_bssid)));
2798109323Ssam		off += szbuf;
2799109323Ssam		ap->scanreason = le16toh(ws_hdr.wi_reason);
2800109323Ssam		memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
2801109323Ssam		ap->channel = le16toh(ws_dat.wi_chid);
2802109323Ssam		ap->signal  = le16toh(ws_dat.wi_signal);
2803109323Ssam		ap->noise   = le16toh(ws_dat.wi_noise);
2804109323Ssam		ap->quality = ap->signal - ap->noise;
2805109323Ssam		ap->capinfo = le16toh(ws_dat.wi_capinfo);
2806109323Ssam		ap->interval = le16toh(ws_dat.wi_interval);
2807109323Ssam		ap->rate    = le16toh(ws_dat.wi_rate);
2808109323Ssam		ap->namelen = le16toh(ws_dat.wi_namelen);
2809109323Ssam		if (ap->namelen > sizeof(ap->name))
2810109323Ssam			ap->namelen = sizeof(ap->name);
2811109323Ssam		memcpy(ap->name, ws_dat.wi_name, ap->namelen);
2812109323Ssam	}
2813109323Ssamdone:
2814109323Ssam	/* Done scanning */
2815109323Ssam	sc->sc_scan_timer = 0;
2816109323Ssam	DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
2817109323Ssam#undef N
281877217Sphk}
281977217Sphk
282088546Salfredstatic void
2821109323Ssamwi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi)
282277217Sphk{
2823109323Ssam	ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr),
2824116951Ssam	    ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL : -1, rssi);
2825109323Ssam	printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n",
2826109323Ssam		le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1),
2827109323Ssam		le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence);
2828109323Ssam	printf(" rx_signal %u rx_rate %u rx_flow %u\n",
2829109323Ssam		wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow);
2830109323Ssam	printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n",
2831109323Ssam		wh->wi_tx_rtry, wh->wi_tx_rate,
2832109323Ssam		le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len));
2833109323Ssam	printf(" ehdr dst %6D src %6D type 0x%x\n",
2834109323Ssam		wh->wi_ehdr.ether_dhost, ":", wh->wi_ehdr.ether_shost, ":",
2835109323Ssam		wh->wi_ehdr.ether_type);
2836109323Ssam}
283777217Sphk
2838109323Ssamint
2839109323Ssamwi_alloc(device_t dev, int rid)
2840109323Ssam{
2841109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2842109323Ssam
2843109323Ssam	if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2844109323Ssam		sc->iobase_rid = rid;
2845109323Ssam		sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2846109323Ssam		    &sc->iobase_rid, 0, ~0, (1 << 6),
2847109323Ssam		    rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2848109323Ssam		if (!sc->iobase) {
2849109323Ssam			device_printf(dev, "No I/O space?!\n");
2850109323Ssam			return (ENXIO);
285198440Simp		}
2852109323Ssam
2853109323Ssam		sc->wi_io_addr = rman_get_start(sc->iobase);
2854109323Ssam		sc->wi_btag = rman_get_bustag(sc->iobase);
2855109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2856109323Ssam	} else {
2857109323Ssam		sc->mem_rid = rid;
2858109323Ssam		sc->mem = bus_alloc_resource(dev, SYS_RES_MEMORY,
2859109323Ssam		    &sc->mem_rid, 0, ~0, 1, RF_ACTIVE);
2860109323Ssam
2861109323Ssam		if (!sc->mem) {
2862109323Ssam			device_printf(dev, "No Mem space on prism2.5?\n");
2863109323Ssam			return (ENXIO);
286477217Sphk		}
2865109323Ssam
2866109323Ssam		sc->wi_btag = rman_get_bustag(sc->mem);
2867109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->mem);
286877217Sphk	}
286977217Sphk
2870109323Ssam
2871109323Ssam	sc->irq_rid = 0;
2872109323Ssam	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
2873109323Ssam	    0, ~0, 1, RF_ACTIVE |
2874109323Ssam	    ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2875109323Ssam
2876109323Ssam	if (!sc->irq) {
2877109323Ssam		wi_free(dev);
2878109323Ssam		device_printf(dev, "No irq?!\n");
2879109323Ssam		return (ENXIO);
288077217Sphk	}
2881109323Ssam
2882109323Ssam	sc->sc_dev = dev;
2883109323Ssam	sc->sc_unit = device_get_unit(dev);
2884109323Ssam
2885109323Ssam	return (0);
288677217Sphk}
288793359Simp
2888109323Ssamvoid
2889109323Ssamwi_free(device_t dev)
2890109323Ssam{
2891109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2892109323Ssam
2893109323Ssam	if (sc->iobase != NULL) {
2894109323Ssam		bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2895109323Ssam		sc->iobase = NULL;
2896109323Ssam	}
2897109323Ssam	if (sc->irq != NULL) {
2898109323Ssam		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2899109323Ssam		sc->irq = NULL;
2900109323Ssam	}
2901109323Ssam	if (sc->mem != NULL) {
2902109323Ssam		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
2903109323Ssam		sc->mem = NULL;
2904109323Ssam	}
2905109323Ssam
2906109323Ssam	return;
2907109323Ssam}
2908109323Ssam
290993359Simpstatic int
2910109323Ssamwi_get_debug(struct wi_softc *sc, struct wi_req *wreq)
291193359Simp{
2912109323Ssam	int error = 0;
291393359Simp
291493359Simp	wreq->wi_len = 1;
291593359Simp
291693359Simp	switch (wreq->wi_type) {
291793359Simp	case WI_DEBUG_SLEEP:
291893359Simp		wreq->wi_len++;
291993359Simp		wreq->wi_val[0] = sc->wi_debug.wi_sleep;
292093359Simp		break;
292193359Simp	case WI_DEBUG_DELAYSUPP:
292293359Simp		wreq->wi_len++;
292393359Simp		wreq->wi_val[0] = sc->wi_debug.wi_delaysupp;
292493359Simp		break;
292593359Simp	case WI_DEBUG_TXSUPP:
292693359Simp		wreq->wi_len++;
292793359Simp		wreq->wi_val[0] = sc->wi_debug.wi_txsupp;
292893359Simp		break;
292993359Simp	case WI_DEBUG_MONITOR:
293093359Simp		wreq->wi_len++;
293193359Simp		wreq->wi_val[0] = sc->wi_debug.wi_monitor;
293293359Simp		break;
293393359Simp	case WI_DEBUG_LEDTEST:
293493359Simp		wreq->wi_len += 3;
293593359Simp		wreq->wi_val[0] = sc->wi_debug.wi_ledtest;
293693359Simp		wreq->wi_val[1] = sc->wi_debug.wi_ledtest_param0;
293793359Simp		wreq->wi_val[2] = sc->wi_debug.wi_ledtest_param1;
293893359Simp		break;
293993359Simp	case WI_DEBUG_CONTTX:
294093359Simp		wreq->wi_len += 2;
294193359Simp		wreq->wi_val[0] = sc->wi_debug.wi_conttx;
294293359Simp		wreq->wi_val[1] = sc->wi_debug.wi_conttx_param0;
294393359Simp		break;
294493359Simp	case WI_DEBUG_CONTRX:
294593359Simp		wreq->wi_len++;
294693359Simp		wreq->wi_val[0] = sc->wi_debug.wi_contrx;
294793359Simp		break;
294893359Simp	case WI_DEBUG_SIGSTATE:
294993359Simp		wreq->wi_len += 2;
295093359Simp		wreq->wi_val[0] = sc->wi_debug.wi_sigstate;
295193359Simp		wreq->wi_val[1] = sc->wi_debug.wi_sigstate_param0;
295293359Simp		break;
295393359Simp	case WI_DEBUG_CONFBITS:
295493359Simp		wreq->wi_len += 2;
295593359Simp		wreq->wi_val[0] = sc->wi_debug.wi_confbits;
295693359Simp		wreq->wi_val[1] = sc->wi_debug.wi_confbits_param0;
295793359Simp		break;
295893359Simp	default:
295993359Simp		error = EIO;
296093359Simp		break;
296193359Simp	}
296293359Simp
296393359Simp	return (error);
296493359Simp}
296593359Simp
296693359Simpstatic int
2967109323Ssamwi_set_debug(struct wi_softc *sc, struct wi_req *wreq)
296893359Simp{
2969109323Ssam	int error = 0;
297093359Simp	u_int16_t		cmd, param0 = 0, param1 = 0;
297193359Simp
297293359Simp	switch (wreq->wi_type) {
297393359Simp	case WI_DEBUG_RESET:
297493359Simp	case WI_DEBUG_INIT:
297593359Simp	case WI_DEBUG_CALENABLE:
297693359Simp		break;
297793359Simp	case WI_DEBUG_SLEEP:
297893359Simp		sc->wi_debug.wi_sleep = 1;
297993359Simp		break;
298093359Simp	case WI_DEBUG_WAKE:
298193359Simp		sc->wi_debug.wi_sleep = 0;
298293359Simp		break;
298393359Simp	case WI_DEBUG_CHAN:
298493359Simp		param0 = wreq->wi_val[0];
298593359Simp		break;
298693359Simp	case WI_DEBUG_DELAYSUPP:
298793359Simp		sc->wi_debug.wi_delaysupp = 1;
298893359Simp		break;
298993359Simp	case WI_DEBUG_TXSUPP:
299093359Simp		sc->wi_debug.wi_txsupp = 1;
299193359Simp		break;
299293359Simp	case WI_DEBUG_MONITOR:
299393359Simp		sc->wi_debug.wi_monitor = 1;
299493359Simp		break;
299593359Simp	case WI_DEBUG_LEDTEST:
299693359Simp		param0 = wreq->wi_val[0];
299793359Simp		param1 = wreq->wi_val[1];
299893359Simp		sc->wi_debug.wi_ledtest = 1;
299993359Simp		sc->wi_debug.wi_ledtest_param0 = param0;
300093359Simp		sc->wi_debug.wi_ledtest_param1 = param1;
300193359Simp		break;
300293359Simp	case WI_DEBUG_CONTTX:
300393359Simp		param0 = wreq->wi_val[0];
300493359Simp		sc->wi_debug.wi_conttx = 1;
300593359Simp		sc->wi_debug.wi_conttx_param0 = param0;
300693359Simp		break;
300793359Simp	case WI_DEBUG_STOPTEST:
300893359Simp		sc->wi_debug.wi_delaysupp = 0;
300993359Simp		sc->wi_debug.wi_txsupp = 0;
301093359Simp		sc->wi_debug.wi_monitor = 0;
301193359Simp		sc->wi_debug.wi_ledtest = 0;
301293359Simp		sc->wi_debug.wi_ledtest_param0 = 0;
301393359Simp		sc->wi_debug.wi_ledtest_param1 = 0;
301493359Simp		sc->wi_debug.wi_conttx = 0;
301593359Simp		sc->wi_debug.wi_conttx_param0 = 0;
301693359Simp		sc->wi_debug.wi_contrx = 0;
301793359Simp		sc->wi_debug.wi_sigstate = 0;
301893359Simp		sc->wi_debug.wi_sigstate_param0 = 0;
301993359Simp		break;
302093359Simp	case WI_DEBUG_CONTRX:
302193359Simp		sc->wi_debug.wi_contrx = 1;
302293359Simp		break;
302393359Simp	case WI_DEBUG_SIGSTATE:
302493359Simp		param0 = wreq->wi_val[0];
302593359Simp		sc->wi_debug.wi_sigstate = 1;
302693359Simp		sc->wi_debug.wi_sigstate_param0 = param0;
302793359Simp		break;
302893359Simp	case WI_DEBUG_CONFBITS:
302993359Simp		param0 = wreq->wi_val[0];
303093359Simp		param1 = wreq->wi_val[1];
303193359Simp		sc->wi_debug.wi_confbits = param0;
303293359Simp		sc->wi_debug.wi_confbits_param0 = param1;
303393359Simp		break;
303493359Simp	default:
303593359Simp		error = EIO;
303693359Simp		break;
303793359Simp	}
303893359Simp
303993359Simp	if (error)
304093359Simp		return (error);
304193359Simp
304293359Simp	cmd = WI_CMD_DEBUG | (wreq->wi_type << 8);
304393359Simp	error = wi_cmd(sc, cmd, param0, param1, 0);
304493359Simp
304593359Simp	return (error);
304693359Simp}
3047101903Simp
3048105076Simp#if __FreeBSD_version >= 500000
3049101903Simp/*
3050101903Simp * Special routines to download firmware for Symbol CF card.
3051101903Simp * XXX: This should be modified generic into any PRISM-2 based card.
3052101903Simp */
3053101903Simp
3054101903Simp#define	WI_SBCF_PDIADDR		0x3100
3055101903Simp
3056101903Simp/* unaligned load little endian */
3057101903Simp#define	GETLE32(p)	((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24))
3058101903Simp#define	GETLE16(p)	((p)[0] | ((p)[1]<<8))
3059101903Simp
3060101903Simpint
3061101903Simpwi_symbol_load_firm(struct wi_softc *sc, const void *primsym, int primlen,
3062101903Simp    const void *secsym, int seclen)
3063101903Simp{
3064101903Simp	uint8_t ebuf[256];
3065101903Simp	int i;
3066101903Simp
3067101903Simp	/* load primary code and run it */
3068101903Simp	wi_symbol_set_hcr(sc, WI_HCR_EEHOLD);
3069101903Simp	if (wi_symbol_write_firm(sc, primsym, primlen, NULL, 0))
3070101903Simp		return EIO;
3071101903Simp	wi_symbol_set_hcr(sc, WI_HCR_RUN);
3072101903Simp	for (i = 0; ; i++) {
3073101903Simp		if (i == 10)
3074101903Simp			return ETIMEDOUT;
3075101903Simp		tsleep(sc, PWAIT, "wiinit", 1);
3076101903Simp		if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT)
3077101903Simp			break;
3078101903Simp		/* write the magic key value to unlock aux port */
3079101903Simp		CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0);
3080101903Simp		CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1);
3081101903Simp		CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2);
3082101903Simp		CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL);
3083101903Simp	}
3084101903Simp
3085101903Simp	/* issue read EEPROM command: XXX copied from wi_cmd() */
3086101903Simp	CSR_WRITE_2(sc, WI_PARAM0, 0);
3087101903Simp	CSR_WRITE_2(sc, WI_PARAM1, 0);
3088101903Simp	CSR_WRITE_2(sc, WI_PARAM2, 0);
3089101903Simp	CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE);
3090101903Simp        for (i = 0; i < WI_TIMEOUT; i++) {
3091101903Simp                if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
3092101903Simp                        break;
3093101903Simp                DELAY(1);
3094101903Simp        }
3095101903Simp        CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
3096101903Simp
3097101903Simp	CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ);
3098101903Simp	CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ);
3099101903Simp	CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA,
3100101903Simp	    (uint16_t *)ebuf, sizeof(ebuf) / 2);
3101101903Simp	if (GETLE16(ebuf) > sizeof(ebuf))
3102101903Simp		return EIO;
3103101903Simp	if (wi_symbol_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf)))
3104101903Simp		return EIO;
3105101903Simp	return 0;
3106101903Simp}
3107101903Simp
3108101903Simpstatic int
3109101903Simpwi_symbol_write_firm(struct wi_softc *sc, const void *buf, int buflen,
3110101903Simp    const void *ebuf, int ebuflen)
3111101903Simp{
3112101903Simp	const uint8_t *p, *ep, *q, *eq;
3113101903Simp	char *tp;
3114101903Simp	uint32_t addr, id, eid;
3115101903Simp	int i, len, elen, nblk, pdrlen;
3116101903Simp
3117101903Simp	/*
3118101903Simp	 * Parse the header of the firmware image.
3119101903Simp	 */
3120101903Simp	p = buf;
3121101903Simp	ep = p + buflen;
3122101903Simp	while (p < ep && *p++ != ' ');	/* FILE: */
3123101903Simp	while (p < ep && *p++ != ' ');	/* filename */
3124101903Simp	while (p < ep && *p++ != ' ');	/* type of the firmware */
3125101903Simp	nblk = strtoul(p, &tp, 10);
3126101903Simp	p = tp;
3127101903Simp	pdrlen = strtoul(p + 1, &tp, 10);
3128101903Simp	p = tp;
3129101903Simp	while (p < ep && *p++ != 0x1a);	/* skip rest of header */
3130101903Simp
3131101903Simp	/*
3132101903Simp	 * Block records: address[4], length[2], data[length];
3133101903Simp	 */
3134101903Simp	for (i = 0; i < nblk; i++) {
3135101903Simp		addr = GETLE32(p);	p += 4;
3136101903Simp		len  = GETLE16(p);	p += 2;
3137101903Simp		CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3138101903Simp		CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3139101903Simp		CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3140101903Simp		    (const uint16_t *)p, len / 2);
3141101903Simp		p += len;
3142101903Simp	}
3143101903Simp
3144101903Simp	/*
3145101903Simp	 * PDR: id[4], address[4], length[4];
3146101903Simp	 */
3147101903Simp	for (i = 0; i < pdrlen; ) {
3148101903Simp		id   = GETLE32(p);	p += 4; i += 4;
3149101903Simp		addr = GETLE32(p);	p += 4; i += 4;
3150101903Simp		len  = GETLE32(p);	p += 4; i += 4;
3151101903Simp		/* replace PDR entry with the values from EEPROM, if any */
3152101903Simp		for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) {
3153101903Simp			elen = GETLE16(q);	q += 2;
3154101903Simp			eid  = GETLE16(q);	q += 2;
3155101903Simp			elen--;		/* elen includes eid */
3156101903Simp			if (eid == 0)
3157101903Simp				break;
3158101903Simp			if (eid != id)
3159101903Simp				continue;
3160101903Simp			CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3161101903Simp			CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3162101903Simp			CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3163101903Simp			    (const uint16_t *)q, len / 2);
3164101903Simp			break;
3165101903Simp		}
3166101903Simp	}
3167101903Simp	return 0;
3168101903Simp}
3169101903Simp
3170101903Simpstatic int
3171101903Simpwi_symbol_set_hcr(struct wi_softc *sc, int mode)
3172101903Simp{
3173101903Simp	uint16_t hcr;
3174101903Simp
3175101903Simp	CSR_WRITE_2(sc, WI_COR, WI_COR_RESET);
3176101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3177101903Simp	hcr = CSR_READ_2(sc, WI_HCR);
3178101903Simp	hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE);
3179101903Simp	CSR_WRITE_2(sc, WI_HCR, hcr);
3180101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3181101903Simp	CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE);
3182101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3183101903Simp	return 0;
3184101903Simp}
3185105076Simp#endif
3186