if_wi.c revision 119784
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 119784 2003-09-05 22:29:30Z sam $");
66113038Sobrien
67109323Ssam#define WI_HERMES_AUTOINC_WAR	/* Work around data write autoinc bug. */
68109323Ssam#define WI_HERMES_STATS_WAR	/* Work around stats counter bug. */
69109323Ssam
70109323Ssam#define	NBPFILTER	1
71109323Ssam
7246492Swpaul#include <sys/param.h>
7346492Swpaul#include <sys/systm.h>
7495706Simp#if __FreeBSD_version >= 500033
7595533Smike#include <sys/endian.h>
7695706Simp#endif
7746492Swpaul#include <sys/sockio.h>
7846492Swpaul#include <sys/mbuf.h>
7983366Sjulian#include <sys/proc.h>
8046492Swpaul#include <sys/kernel.h>
8146492Swpaul#include <sys/socket.h>
8253702Swpaul#include <sys/module.h>
8353702Swpaul#include <sys/bus.h>
8494486Simp#include <sys/random.h>
8553702Swpaul#include <sys/syslog.h>
8653702Swpaul#include <sys/sysctl.h>
8746492Swpaul
8853702Swpaul#include <machine/bus.h>
8953702Swpaul#include <machine/resource.h>
9095534Simp#include <machine/clock.h>
91116951Ssam#include <machine/atomic.h>
9253702Swpaul#include <sys/rman.h>
9353702Swpaul
9446492Swpaul#include <net/if.h>
9546492Swpaul#include <net/if_arp.h>
9646492Swpaul#include <net/ethernet.h>
9746492Swpaul#include <net/if_dl.h>
9846492Swpaul#include <net/if_media.h>
9946492Swpaul#include <net/if_types.h>
10046492Swpaul
101116951Ssam#include <net80211/ieee80211_var.h>
102116951Ssam#include <net80211/ieee80211_ioctl.h>
103119784Ssam#include <net80211/ieee80211_radiotap.h>
104116951Ssam
10546492Swpaul#include <netinet/in.h>
10646492Swpaul#include <netinet/in_systm.h>
10746492Swpaul#include <netinet/in_var.h>
10846492Swpaul#include <netinet/ip.h>
10946492Swpaul#include <netinet/if_ether.h>
11046492Swpaul
11146492Swpaul#include <net/bpf.h>
11246492Swpaul
11370808Speter#include <dev/wi/if_wavelan_ieee.h>
114119784Ssam#include <dev/wi/if_wireg.h>
11593611Simp#include <dev/wi/if_wivar.h>
11646492Swpaul
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	 */
25953702Swpaul	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
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;
303109323Ssam	ifp->if_unit = sc->sc_unit;
30446492Swpaul	ifp->if_name = "wi";
30546492Swpaul	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
30646492Swpaul	ifp->if_ioctl = wi_ioctl;
30746492Swpaul	ifp->if_start = wi_start;
30846492Swpaul	ifp->if_watchdog = wi_watchdog;
30946492Swpaul	ifp->if_init = wi_init;
31046492Swpaul	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
31146492Swpaul
312109323Ssam	ic->ic_phytype = IEEE80211_T_DS;
313109323Ssam	ic->ic_opmode = IEEE80211_M_STA;
314116951Ssam	ic->ic_caps = IEEE80211_C_PMGT | IEEE80211_C_AHDEMO;
315109323Ssam	ic->ic_state = IEEE80211_S_INIT;
31646492Swpaul
317116951Ssam	/*
318116951Ssam	 * Query the card for available channels and setup the
319116951Ssam	 * channel table.  We assume these are all 11b channels.
320116951Ssam	 */
321109323Ssam	buflen = sizeof(val);
322109323Ssam	if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
323109323Ssam		val = htole16(0x1fff);	/* assume 1-11 */
324116951Ssam	KASSERT(val != 0, ("wi_attach: no available channels listed!"));
325116951Ssam
326116951Ssam	val <<= 1;			/* shift for base 1 indices */
327116951Ssam	for (i = 1; i < 16; i++) {
328116951Ssam		if (isset((u_int8_t*)&val, i)) {
329116951Ssam			ic->ic_channels[i].ic_freq =
330116951Ssam				ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
331116951Ssam			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
332116951Ssam		}
333109323Ssam	}
33446492Swpaul
33546563Swpaul	/*
33646563Swpaul	 * Read the default channel from the NIC. This may vary
33746563Swpaul	 * depending on the country where the NIC was purchased, so
33846563Swpaul	 * we can't hard-code a default and expect it to work for
33946563Swpaul	 * everyone.
340116951Ssam	 *
341116951Ssam	 * If no channel is specified, let the 802.11 code select.
34246563Swpaul	 */
343109323Ssam	buflen = sizeof(val);
344116951Ssam	if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0) {
345116951Ssam		val = le16toh(val);
346116951Ssam		KASSERT(val < IEEE80211_CHAN_MAX &&
347116951Ssam			ic->ic_channels[val].ic_flags != 0,
348116951Ssam			("wi_attach: invalid own channel %u!", val));
349116951Ssam		ic->ic_ibss_chan = &ic->ic_channels[val];
350117042Ssam	} else {
351117042Ssam		device_printf(dev,
352117042Ssam			"WI_RID_OWN_CHNL failed, using first channel!\n");
353117042Ssam		ic->ic_ibss_chan = &ic->ic_channels[0];
354109323Ssam	}
35546563Swpaul
35656965Swpaul	/*
35798440Simp	 * Set flags based on firmware version.
35898440Simp	 */
35998440Simp	switch (sc->sc_firmware_type) {
36098440Simp	case WI_LUCENT:
361112363Simp		sc->sc_ntxbuf = 1;
362109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
363109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
364109323Ssam		/* XXX: not confirmed, but never seen for recent firmware */
365109323Ssam		if (sc->sc_sta_firmware_ver <  40000) {
366109323Ssam			sc->sc_flags |= WI_FLAGS_BUG_AUTOINC;
367109323Ssam		}
368109323Ssam#endif
36998440Simp		if (sc->sc_sta_firmware_ver >= 60000)
370109323Ssam			sc->sc_flags |= WI_FLAGS_HAS_MOR;
371117855Ssam		if (sc->sc_sta_firmware_ver >= 60006) {
372116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
373117855Ssam			ic->ic_caps |= IEEE80211_C_MONITOR;
374117855Ssam		}
375109323Ssam		sc->sc_ibss_port = htole16(1);
376119784Ssam
377119784Ssam		sc->sc_min_rssi = WI_LUCENT_MIN_RSSI;
378119784Ssam		sc->sc_max_rssi = WI_LUCENT_MAX_RSSI;
379119784Ssam		sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
38098440Simp		break;
381109323Ssam
38298440Simp	case WI_INTERSIL:
383112363Simp		sc->sc_ntxbuf = WI_NTXBUF;
384109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR;
385109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_ROAMING;
386109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
387109323Ssam		if (sc->sc_sta_firmware_ver > 10101)
388109323Ssam			sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
389117855Ssam		if (sc->sc_sta_firmware_ver >= 800) {
390116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
391117855Ssam			ic->ic_caps |= IEEE80211_C_MONITOR;
392117855Ssam		}
393109396Simp		/*
394109396Simp		 * version 0.8.3 and newer are the only ones that are known
395109396Simp		 * to currently work.  Earlier versions can be made to work,
396109396Simp		 * at least according to the Linux driver.
397109396Simp		 */
398100734Simp		if (sc->sc_sta_firmware_ver >= 803)
399116951Ssam			ic->ic_caps |= IEEE80211_C_HOSTAP;
400109323Ssam		sc->sc_ibss_port = htole16(0);
401119784Ssam
402119784Ssam		sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
403119784Ssam		sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
404119784Ssam		sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
40598440Simp		break;
406109323Ssam
40798440Simp	case WI_SYMBOL:
408112363Simp		sc->sc_ntxbuf = 1;
409109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY;
41098440Simp		if (sc->sc_sta_firmware_ver >= 25000)
411116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
412109323Ssam		sc->sc_ibss_port = htole16(4);
413119784Ssam
414119784Ssam		sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
415119784Ssam		sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
416119784Ssam		sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
41798440Simp		break;
41898440Simp	}
41998440Simp
42098440Simp	/*
42156965Swpaul	 * Find out if we support WEP on this card.
42256965Swpaul	 */
423109323Ssam	buflen = sizeof(val);
424109323Ssam	if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
425109323Ssam	    val != htole16(0))
426116951Ssam		ic->ic_caps |= IEEE80211_C_WEP;
42756965Swpaul
428109323Ssam	/* Find supported rates. */
429109323Ssam	buflen = sizeof(ratebuf);
430116951Ssam	rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
431109323Ssam	if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
432116951Ssam		nrates = le16toh(*(u_int16_t *)ratebuf);
433116951Ssam		if (nrates > IEEE80211_RATE_MAXSIZE)
434116951Ssam			nrates = IEEE80211_RATE_MAXSIZE;
435116951Ssam		rs->rs_nrates = 0;
436116951Ssam		for (i = 0; i < nrates; i++)
437116951Ssam			if (ratebuf[2+i])
438116951Ssam				rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i];
439109323Ssam	} else {
440109323Ssam		/* XXX fallback on error? */
441116951Ssam		rs->rs_nrates = 0;
44298440Simp	}
44377217Sphk
444109323Ssam	buflen = sizeof(val);
445109323Ssam	if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
446109323Ssam	    wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
447119784Ssam		sc->sc_dbm_offset = le16toh(val);
448119784Ssam	}
44946492Swpaul
450109323Ssam	sc->sc_max_datalen = 2304;
451109323Ssam	sc->sc_system_scale = 1;
452109323Ssam	sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN;
453109323Ssam	sc->sc_roaming_mode = 1;
45446492Swpaul
455109323Ssam	sc->sc_portnum = WI_DEFAULT_PORT;
456109323Ssam	sc->sc_authtype = WI_DEFAULT_AUTHTYPE;
45787383Simp
458109323Ssam	bzero(sc->sc_nodename, sizeof(sc->sc_nodename));
459109323Ssam	sc->sc_nodelen = sizeof(WI_DEFAULT_NODENAME) - 1;
460109323Ssam	bcopy(WI_DEFAULT_NODENAME, sc->sc_nodename, sc->sc_nodelen);
46187383Simp
462109323Ssam	bzero(sc->sc_net_name, sizeof(sc->sc_net_name));
463109323Ssam	bcopy(WI_DEFAULT_NETNAME, sc->sc_net_name,
464109323Ssam	    sizeof(WI_DEFAULT_NETNAME) - 1);
46593733Simp
46693733Simp	/*
467109323Ssam	 * Call MI attach routine.
46893733Simp	 */
469109323Ssam	ieee80211_ifattach(ifp);
470117812Ssam	/* override state transition method */
471117812Ssam	sc->sc_newstate = ic->ic_newstate;
472117812Ssam	ic->ic_newstate = wi_newstate;
473116951Ssam	ieee80211_media_init(ifp, wi_media_change, wi_media_status);
474109323Ssam
475119784Ssam#if NBPFILTER > 0
476119784Ssam	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
477119784Ssam		sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
478119784Ssam		&sc->sc_drvbpf);
479119784Ssam	/*
480119784Ssam	 * Initialize constant fields.
481119784Ssam	 *
482119784Ssam	 * NB: the channel is setup each time we transition to the
483119784Ssam	 *     RUN state to avoid filling it in for each frame.
484119784Ssam	 */
485119784Ssam	sc->sc_tx_th.wt_ihdr.it_len = sizeof(sc->sc_tx_th);
486119784Ssam	sc->sc_tx_th.wt_ihdr.it_present = WI_TX_RADIOTAP_PRESENT;
487119784Ssam
488119784Ssam	sc->sc_rx_th.wr_ihdr.it_len = sizeof(sc->sc_rx_th);
489119784Ssam	sc->sc_rx_th.wr_ihdr.it_present = WI_RX_RADIOTAP_PRESENT0;
490119784Ssam	sc->sc_rx_th.wr_present1 = WI_RX_RADIOTAP_PRESENT1;
491119784Ssam#endif
492109323Ssam	return (0);
49387383Simp}
49487383Simp
495109323Ssamint
496109323Ssamwi_detach(device_t dev)
49746492Swpaul{
498109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
499109323Ssam	struct ifnet *ifp = &sc->sc_ic.ic_if;
500109323Ssam	WI_LOCK_DECL();
50146492Swpaul
502109323Ssam	WI_LOCK(sc);
50346492Swpaul
504109323Ssam	/* check if device was removed */
505109323Ssam	sc->wi_gone = !bus_child_present(dev);
50646492Swpaul
507109323Ssam	wi_stop(ifp, 0);
50846492Swpaul
509119784Ssam#if NBPFILTER > 0
510119784Ssam	bpfdetach(ifp);
511119784Ssam#endif
512109323Ssam	ieee80211_ifdetach(ifp);
513114190Simp	WI_UNLOCK(sc);
514109323Ssam	bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
515109323Ssam	wi_free(dev);
516109323Ssam#if __FreeBSD_version >= 500000
517109323Ssam	mtx_destroy(&sc->sc_mtx);
51893359Simp#endif
519109323Ssam	return (0);
520109323Ssam}
52193359Simp
522109323Ssam#ifdef __NetBSD__
523109323Ssamint
524109323Ssamwi_activate(struct device *self, enum devact act)
525109323Ssam{
526109323Ssam	struct wi_softc *sc = (struct wi_softc *)self;
527109323Ssam	int rv = 0, s;
52893359Simp
529109323Ssam	s = splnet();
530109323Ssam	switch (act) {
531109323Ssam	case DVACT_ACTIVATE:
532109323Ssam		rv = EOPNOTSUPP;
533109323Ssam		break;
53493359Simp
535109323Ssam	case DVACT_DEACTIVATE:
536109323Ssam		if_deactivate(&sc->sc_ic.ic_if);
537109323Ssam		break;
538109323Ssam	}
539109323Ssam	splx(s);
540109323Ssam	return rv;
541109323Ssam}
54293359Simp
543109323Ssamvoid
544109323Ssamwi_power(struct wi_softc *sc, int why)
545109323Ssam{
546109323Ssam	struct ifnet *ifp = &sc->sc_ic.ic_if;
547109323Ssam	int s;
54893359Simp
549109323Ssam	s = splnet();
550109323Ssam	switch (why) {
551109323Ssam	case PWR_SUSPEND:
552109323Ssam	case PWR_STANDBY:
553109323Ssam		wi_stop(ifp, 1);
554109323Ssam		break;
555109323Ssam	case PWR_RESUME:
556109323Ssam		if (ifp->if_flags & IFF_UP) {
557109323Ssam			wi_init(ifp);
558109323Ssam			(void)wi_intr(sc);
55994405Simp		}
560109323Ssam		break;
561109323Ssam	case PWR_SOFTSUSPEND:
562109323Ssam	case PWR_SOFTSTANDBY:
563109323Ssam	case PWR_SOFTRESUME:
564109323Ssam		break;
56593359Simp	}
566109323Ssam	splx(s);
56746492Swpaul}
568109323Ssam#endif /* __NetBSD__ */
56946492Swpaul
570109323Ssamvoid
571109323Ssamwi_shutdown(device_t dev)
57246492Swpaul{
573109323Ssam	struct wi_softc *sc = device_get_softc(dev);
57446492Swpaul
575109323Ssam	wi_stop(&sc->sc_if, 1);
57646492Swpaul}
57746492Swpaul
578109323Ssamvoid
579109323Ssamwi_intr(void *arg)
58046492Swpaul{
581109323Ssam	struct wi_softc *sc = arg;
582109323Ssam	struct ifnet *ifp = &sc->sc_ic.ic_if;
583112363Simp	u_int16_t status;
584109323Ssam	WI_LOCK_DECL();
58546492Swpaul
586109323Ssam	WI_LOCK(sc);
58746492Swpaul
588109323Ssam	if (sc->wi_gone || (ifp->if_flags & IFF_UP) == 0) {
589113327Simp		CSR_WRITE_2(sc, WI_INT_EN, 0);
590109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
591109323Ssam		WI_UNLOCK(sc);
59246492Swpaul		return;
593109323Ssam	}
59446492Swpaul
595112363Simp	/* Disable interrupts. */
596112363Simp	CSR_WRITE_2(sc, WI_INT_EN, 0);
59746492Swpaul
598112363Simp	status = CSR_READ_2(sc, WI_EVENT_STAT);
599112363Simp	if (status & WI_EV_RX)
600112363Simp		wi_rx_intr(sc);
601112363Simp	if (status & WI_EV_ALLOC)
602112363Simp		wi_tx_intr(sc);
603112363Simp	if (status & WI_EV_TX_EXC)
604112363Simp		wi_tx_ex_intr(sc);
605112363Simp	if (status & WI_EV_INFO)
606112363Simp		wi_info_intr(sc);
607112363Simp	if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
608112363Simp	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 &&
609112363Simp	    _IF_QLEN(&ifp->if_snd) != 0)
610112363Simp		wi_start(ifp);
61146492Swpaul
612112363Simp	/* Re-enable interrupts. */
613112363Simp	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
61446492Swpaul
615109323Ssam	WI_UNLOCK(sc);
61646492Swpaul
61746492Swpaul	return;
61846492Swpaul}
61946492Swpaul
620109323Ssamvoid
621109323Ssamwi_init(void *arg)
62246492Swpaul{
623109323Ssam	struct wi_softc *sc = arg;
624109323Ssam	struct ifnet *ifp = &sc->sc_if;
625109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
626109323Ssam	struct wi_joinreq join;
627109323Ssam	int i;
628109323Ssam	int error = 0, wasenabled;
629109323Ssam	struct ifaddr *ifa;
630109323Ssam	struct sockaddr_dl *sdl;
631109323Ssam	WI_LOCK_DECL();
63246492Swpaul
633109323Ssam	WI_LOCK(sc);
63467092Swpaul
635109323Ssam	if (sc->wi_gone) {
636109323Ssam		WI_UNLOCK(sc);
63746492Swpaul		return;
63846492Swpaul	}
63946492Swpaul
640112362Simp	if ((wasenabled = sc->sc_enabled))
641109323Ssam		wi_stop(ifp, 0);
642112362Simp	wi_reset(sc);
64346492Swpaul
644109323Ssam	/* common 802.11 configuration */
645109323Ssam	ic->ic_flags &= ~IEEE80211_F_IBSSON;
646109323Ssam	sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
647109323Ssam	switch (ic->ic_opmode) {
648109323Ssam	case IEEE80211_M_STA:
649109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS);
650109323Ssam		break;
651109323Ssam	case IEEE80211_M_IBSS:
652109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port);
653109323Ssam		ic->ic_flags |= IEEE80211_F_IBSSON;
654109323Ssam		break;
655109323Ssam	case IEEE80211_M_AHDEMO:
656109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
657109323Ssam		break;
658109323Ssam	case IEEE80211_M_HOSTAP:
659109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP);
660109323Ssam		break;
661117855Ssam	case IEEE80211_M_MONITOR:
662117855Ssam		if (sc->sc_firmware_type == WI_LUCENT)
663117855Ssam			wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
664117855Ssam		wi_cmd(sc, WI_CMD_DEBUG | (WI_TEST_MONITOR << 8), 0, 0, 0);
665117855Ssam		break;
66646492Swpaul	}
66746492Swpaul
668109323Ssam	/* Intersil interprets this RID as joining ESS even in IBSS mode */
669109323Ssam	if (sc->sc_firmware_type == WI_LUCENT &&
670109323Ssam	    (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0)
671109323Ssam		wi_write_val(sc, WI_RID_CREATE_IBSS, 1);
672109323Ssam	else
673109323Ssam		wi_write_val(sc, WI_RID_CREATE_IBSS, 0);
674109323Ssam	wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
675109323Ssam	wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid,
676109323Ssam	    ic->ic_des_esslen);
677116951Ssam	wi_write_val(sc, WI_RID_OWN_CHNL,
678116951Ssam		ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
679109323Ssam	wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen);
68046492Swpaul
681109323Ssam	ifa = ifaddr_byindex(ifp->if_index);
682109323Ssam	sdl = (struct sockaddr_dl *) ifa->ifa_addr;
683109323Ssam	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(sdl));
684109323Ssam	wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN);
68575373Salfred
686109323Ssam	wi_write_val(sc, WI_RID_PM_ENABLED,
687109323Ssam	    (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
68846492Swpaul
689109323Ssam	/* not yet common 802.11 configuration */
690109323Ssam	wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen);
691116951Ssam	wi_write_val(sc, WI_RID_RTS_THRESH, ic->ic_rtsthreshold);
692109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
693116951Ssam		wi_write_val(sc, WI_RID_FRAG_THRESH, ic->ic_fragthreshold);
69446492Swpaul
695109323Ssam	/* driver specific 802.11 configuration */
696109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)
697109323Ssam		wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale);
698109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
699109323Ssam		wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode);
700109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_MOR)
701109323Ssam		wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven);
702109323Ssam	wi_write_txrate(sc);
703109323Ssam	wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen);
70446492Swpaul
705109323Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
706109323Ssam	    sc->sc_firmware_type == WI_INTERSIL) {
707109323Ssam		wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval);
708109323Ssam		wi_write_val(sc, WI_RID_BASIC_RATE, 0x03);   /* 1, 2 */
709109323Ssam		wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */
710109323Ssam		wi_write_val(sc, WI_RID_DTIM_PERIOD, 1);
71146492Swpaul	}
71246492Swpaul
713109323Ssam	/*
714109323Ssam	 * Initialize promisc mode.
715109323Ssam	 *	Being in the Host-AP mode causes a great
716109323Ssam	 *	deal of pain if primisc mode is set.
717109323Ssam	 *	Therefore we avoid confusing the firmware
718109323Ssam	 *	and always reset promisc mode in Host-AP
719109323Ssam	 *	mode.  Host-AP sees all the packets anyway.
720109323Ssam	 */
721109323Ssam	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
722109323Ssam	    (ifp->if_flags & IFF_PROMISC) != 0) {
723109323Ssam		wi_write_val(sc, WI_RID_PROMISC, 1);
724109323Ssam	} else {
725109323Ssam		wi_write_val(sc, WI_RID_PROMISC, 0);
72675373Salfred	}
72746492Swpaul
728109323Ssam	/* Configure WEP. */
729116951Ssam	if (ic->ic_caps & IEEE80211_C_WEP)
730109323Ssam		wi_write_wep(sc);
73167092Swpaul
732109323Ssam	/* Set multicast filter. */
733109323Ssam	wi_write_multi(sc);
73446492Swpaul
735114124Simp	/* Allocate fids for the card */
736109323Ssam	if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
737109323Ssam		sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
738109323Ssam		if (sc->sc_firmware_type == WI_SYMBOL)
739109323Ssam			sc->sc_buflen = 1585;	/* XXX */
740112363Simp		for (i = 0; i < sc->sc_ntxbuf; i++) {
741109323Ssam			error = wi_alloc_fid(sc, sc->sc_buflen,
742109323Ssam			    &sc->sc_txd[i].d_fid);
743109323Ssam			if (error) {
744109323Ssam				device_printf(sc->sc_dev,
745109323Ssam				    "tx buffer allocation failed (error %u)\n",
746109323Ssam				    error);
747109323Ssam				goto out;
748109323Ssam			}
749109323Ssam			sc->sc_txd[i].d_len = 0;
75070073Swpaul		}
75170073Swpaul	}
752109323Ssam	sc->sc_txcur = sc->sc_txnext = 0;
75370073Swpaul
754109323Ssam	/* Enable desired port */
755109323Ssam	wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
75646492Swpaul
757114124Simp	sc->sc_enabled = 1;
758109323Ssam	ifp->if_flags |= IFF_RUNNING;
759109323Ssam	ifp->if_flags &= ~IFF_OACTIVE;
760109323Ssam	if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
761117855Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR ||
762109323Ssam	    ic->ic_opmode == IEEE80211_M_HOSTAP)
763117812Ssam		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
76446492Swpaul
765109323Ssam	/* Enable interrupts */
766109323Ssam	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
76746492Swpaul
768109323Ssam	if (!wasenabled &&
769109323Ssam	    ic->ic_opmode == IEEE80211_M_HOSTAP &&
770109323Ssam	    sc->sc_firmware_type == WI_INTERSIL) {
771109323Ssam		/* XXX: some card need to be re-enabled for hostap */
772109323Ssam		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
773109323Ssam		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
77475149Simp	}
77594397Simp
776109323Ssam	if (ic->ic_opmode == IEEE80211_M_STA &&
777109323Ssam	    ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
778116951Ssam	    ic->ic_des_chan != IEEE80211_CHAN_ANYC)) {
779109323Ssam		memset(&join, 0, sizeof(join));
780109323Ssam		if (ic->ic_flags & IEEE80211_F_DESBSSID)
781109323Ssam			IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
782116951Ssam		if (ic->ic_des_chan != IEEE80211_CHAN_ANYC)
783116951Ssam			join.wi_chan = htole16(
784116951Ssam				ieee80211_chan2ieee(ic, ic->ic_des_chan));
785109323Ssam		/* Lucent firmware does not support the JOIN RID. */
786109323Ssam		if (sc->sc_firmware_type != WI_LUCENT)
787109323Ssam			wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
78894397Simp	}
78975373Salfred
790109323Ssam	WI_UNLOCK(sc);
79146492Swpaul	return;
792109323Ssamout:
793109323Ssam	if (error) {
794109323Ssam		if_printf(ifp, "interface not running\n");
795109323Ssam		wi_stop(ifp, 0);
79670073Swpaul	}
797110224Simp	WI_UNLOCK(sc);
798109323Ssam	DPRINTF(("wi_init: return %d\n", error));
799109323Ssam	return;
80046492Swpaul}
80146492Swpaul
802109323Ssamvoid
803109323Ssamwi_stop(struct ifnet *ifp, int disable)
80446492Swpaul{
805117812Ssam	struct ieee80211com *ic = (struct ieee80211com *) ifp;
806109323Ssam	struct wi_softc *sc = ifp->if_softc;
807109323Ssam	WI_LOCK_DECL();
80846492Swpaul
809109323Ssam	WI_LOCK(sc);
81074998Swpaul
811117812Ssam	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
812109323Ssam	if (sc->sc_enabled && !sc->wi_gone) {
813109323Ssam		CSR_WRITE_2(sc, WI_INT_EN, 0);
814109323Ssam		wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
815109323Ssam		if (disable) {
816109323Ssam#ifdef __NetBSD__
817109323Ssam			if (sc->sc_disable)
818109323Ssam				(*sc->sc_disable)(sc);
819109323Ssam#endif
820116951Ssam			sc->sc_enabled = 0;
82170073Swpaul		}
82270073Swpaul	}
82370073Swpaul
824109323Ssam	sc->sc_tx_timer = 0;
825109323Ssam	sc->sc_scan_timer = 0;
826109323Ssam	sc->sc_syn_timer = 0;
827109323Ssam	sc->sc_false_syns = 0;
828109323Ssam	sc->sc_naps = 0;
829109323Ssam	ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
830109323Ssam	ifp->if_timer = 0;
83146492Swpaul
832109323Ssam	WI_UNLOCK(sc);
83346492Swpaul}
83446492Swpaul
835109323Ssamstatic void
836109323Ssamwi_start(struct ifnet *ifp)
83746492Swpaul{
838109323Ssam	struct wi_softc	*sc = ifp->if_softc;
839109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
840119150Ssam	struct ieee80211_node *ni;
841109323Ssam	struct ieee80211_frame *wh;
842109323Ssam	struct mbuf *m0;
843109323Ssam	struct wi_frame frmhdr;
844119150Ssam	int cur, fid, off, error;
845109323Ssam	WI_LOCK_DECL();
84646492Swpaul
847109323Ssam	WI_LOCK(sc);
84846492Swpaul
849109323Ssam	if (sc->wi_gone) {
850109323Ssam		WI_UNLOCK(sc);
851109323Ssam		return;
85246492Swpaul	}
853109323Ssam	if (sc->sc_flags & WI_FLAGS_OUTRANGE) {
854109323Ssam		WI_UNLOCK(sc);
855109323Ssam		return;
85675373Salfred	}
85746492Swpaul
858109323Ssam	memset(&frmhdr, 0, sizeof(frmhdr));
859109323Ssam	cur = sc->sc_txnext;
860109323Ssam	for (;;) {
861109323Ssam		IF_POLL(&ic->ic_mgtq, m0);
862109323Ssam		if (m0 != NULL) {
863109323Ssam			if (sc->sc_txd[cur].d_len != 0) {
864109323Ssam				ifp->if_flags |= IFF_OACTIVE;
865109323Ssam				break;
866109323Ssam			}
867109323Ssam			IF_DEQUEUE(&ic->ic_mgtq, m0);
868119150Ssam			/*
869119150Ssam			 * Hack!  The referenced node pointer is in the
870119150Ssam			 * rcvif field of the packet header.  This is
871119150Ssam			 * placed there by ieee80211_mgmt_output because
872119150Ssam			 * we need to hold the reference with the frame
873119150Ssam			 * and there's no other way (other than packet
874119150Ssam			 * tags which we consider too expensive to use)
875119150Ssam			 * to pass it along.
876119150Ssam			 */
877119150Ssam			ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
878119150Ssam			m0->m_pkthdr.rcvif = NULL;
879119150Ssam
880109323Ssam			m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
881109323Ssam			    (caddr_t)&frmhdr.wi_ehdr);
882109323Ssam			frmhdr.wi_ehdr.ether_type = 0;
883109323Ssam                        wh = mtod(m0, struct ieee80211_frame *);
884109323Ssam		} else {
885109323Ssam			if (ic->ic_state != IEEE80211_S_RUN)
886109323Ssam				break;
887109323Ssam			IFQ_POLL(&ifp->if_snd, m0);
888109323Ssam			if (m0 == NULL)
889109323Ssam				break;
890109323Ssam			if (sc->sc_txd[cur].d_len != 0) {
891109323Ssam				ifp->if_flags |= IFF_OACTIVE;
892109323Ssam				break;
893109323Ssam			}
894109323Ssam			IFQ_DEQUEUE(&ifp->if_snd, m0);
895109323Ssam			ifp->if_opackets++;
896109323Ssam			m_copydata(m0, 0, ETHER_HDR_LEN,
897109323Ssam			    (caddr_t)&frmhdr.wi_ehdr);
898109323Ssam#if NBPFILTER > 0
899109323Ssam			BPF_MTAP(ifp, m0);
900109323Ssam#endif
90146492Swpaul
902119150Ssam			m0 = ieee80211_encap(ifp, m0, &ni);
903119150Ssam			if (m0 == NULL) {
904109323Ssam				ifp->if_oerrors++;
905109323Ssam				continue;
906109323Ssam			}
907109323Ssam                        wh = mtod(m0, struct ieee80211_frame *);
908109323Ssam			if (ic->ic_flags & IEEE80211_F_WEPON)
909109323Ssam				wh->i_fc[1] |= IEEE80211_FC1_WEP;
91046492Swpaul
911109323Ssam		}
912109323Ssam#if NBPFILTER > 0
913109323Ssam		if (ic->ic_rawbpf)
914109323Ssam			bpf_mtap(ic->ic_rawbpf, m0);
915109323Ssam#endif
916109323Ssam		frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
917109323Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
918109323Ssam		    (wh->i_fc[1] & IEEE80211_FC1_WEP)) {
919109323Ssam			if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) {
920109323Ssam				ifp->if_oerrors++;
921119150Ssam				if (ni && ni != ic->ic_bss)
922119150Ssam					ieee80211_free_node(ic, ni);
923109323Ssam				continue;
924109323Ssam			}
925109323Ssam			frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
926109323Ssam		}
927109323Ssam		m_copydata(m0, 0, sizeof(struct ieee80211_frame),
928109323Ssam		    (caddr_t)&frmhdr.wi_whdr);
929109323Ssam		m_adj(m0, sizeof(struct ieee80211_frame));
930109323Ssam		frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
931109323Ssam#if NBPFILTER > 0
932109323Ssam		if (sc->sc_drvbpf) {
933109323Ssam			struct mbuf *mb;
93446492Swpaul
935111119Simp			MGETHDR(mb, M_DONTWAIT, m0->m_type);
936109323Ssam			if (mb != NULL) {
937109323Ssam				mb->m_next = m0;
938119784Ssam				mb->m_data = (caddr_t)&sc->sc_tx_th;
939119784Ssam				mb->m_len = sizeof(sc->sc_tx_th);
940109323Ssam				mb->m_pkthdr.len += mb->m_len;
941109323Ssam				bpf_mtap(sc->sc_drvbpf, mb);
942109323Ssam				m_free(mb);
943109323Ssam			}
944109323Ssam		}
94546492Swpaul#endif
946109323Ssam		if (IFF_DUMPPKTS(ifp))
947116951Ssam			wi_dump_pkt(&frmhdr, NULL, -1);
948109323Ssam		fid = sc->sc_txd[cur].d_fid;
949109323Ssam		off = sizeof(frmhdr);
950119150Ssam		error = wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0
951119150Ssam		     || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
952119150Ssam		m_freem(m0);
953119150Ssam		if (ni && ni != ic->ic_bss)
954119150Ssam			ieee80211_free_node(ic, ni);
955119150Ssam		if (error) {
956109323Ssam			ifp->if_oerrors++;
957109323Ssam			continue;
958109323Ssam		}
959109323Ssam		sc->sc_txd[cur].d_len = off;
960109323Ssam		if (sc->sc_txcur == cur) {
961109323Ssam			if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
962109323Ssam				if_printf(ifp, "xmit failed\n");
963109323Ssam				sc->sc_txd[cur].d_len = 0;
964109323Ssam				continue;
965109323Ssam			}
966109323Ssam			sc->sc_tx_timer = 5;
967109323Ssam			ifp->if_timer = 1;
968109323Ssam		}
969112363Simp		sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
97074838Salfred	}
97146492Swpaul
972109323Ssam	WI_UNLOCK(sc);
97346492Swpaul}
97446492Swpaul
97588546Salfredstatic int
976109323Ssamwi_reset(struct wi_softc *sc)
97746492Swpaul{
978114124Simp	struct ieee80211com *ic = &sc->sc_ic;
979114124Simp	struct ifnet *ifp = &ic->ic_if;
980114124Simp#define WI_INIT_TRIES 3
981114124Simp	int i;
982114138Simp	int error = 0;
983114124Simp	int tries;
984114124Simp
985114124Simp	/* Symbol firmware cannot be initialized more than once */
986116276Simp	if (sc->sc_firmware_type != WI_INTERSIL && sc->sc_reset)
987112362Simp		return (0);
988114124Simp	if (sc->sc_firmware_type == WI_SYMBOL)
989114124Simp		tries = 1;
990114124Simp	else
991114124Simp		tries = WI_INIT_TRIES;
992112362Simp
993114124Simp	for (i = 0; i < tries; i++) {
994109323Ssam		if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
99546492Swpaul			break;
996109323Ssam		DELAY(WI_DELAY * 1000);
99746492Swpaul	}
998114124Simp	sc->sc_reset = 1;
99946492Swpaul
1000114124Simp	if (i == tries) {
1001114124Simp		if_printf(ifp, "init failed\n");
1002114124Simp		return (error);
100375373Salfred	}
100446492Swpaul
1005109323Ssam	CSR_WRITE_2(sc, WI_INT_EN, 0);
1006114124Simp	CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
100746492Swpaul
1008109323Ssam	/* Calibrate timer. */
1009114124Simp	wi_write_val(sc, WI_RID_TICK_TIME, 8);
1010114124Simp
1011114124Simp	return (0);
1012109323Ssam#undef WI_INIT_TRIES
101346492Swpaul}
101446492Swpaul
101588546Salfredstatic void
1016109323Ssamwi_watchdog(struct ifnet *ifp)
101746492Swpaul{
1018109323Ssam	struct wi_softc	*sc = ifp->if_softc;
101946492Swpaul
1020109323Ssam	ifp->if_timer = 0;
1021109323Ssam	if (!sc->sc_enabled)
1022109323Ssam		return;
102346492Swpaul
1024109323Ssam	if (sc->sc_tx_timer) {
1025109323Ssam		if (--sc->sc_tx_timer == 0) {
1026109323Ssam			if_printf(ifp, "device timeout\n");
1027109323Ssam			ifp->if_oerrors++;
1028109323Ssam			wi_init(ifp->if_softc);
1029109323Ssam			return;
1030109323Ssam		}
1031109323Ssam		ifp->if_timer = 1;
103246492Swpaul	}
103346492Swpaul
1034109323Ssam	if (sc->sc_scan_timer) {
1035109323Ssam		if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
1036109323Ssam		    sc->sc_firmware_type == WI_INTERSIL) {
1037109323Ssam			DPRINTF(("wi_watchdog: inquire scan\n"));
1038109323Ssam			wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
103946492Swpaul		}
1040109323Ssam		if (sc->sc_scan_timer)
1041109323Ssam			ifp->if_timer = 1;
104246492Swpaul	}
104346492Swpaul
1044109323Ssam	if (sc->sc_syn_timer) {
1045109323Ssam		if (--sc->sc_syn_timer == 0) {
1046117812Ssam			struct ieee80211com *ic = (struct ieee80211com *) ifp;
1047109323Ssam			DPRINTF2(("wi_watchdog: %d false syns\n",
1048109323Ssam			    sc->sc_false_syns));
1049109323Ssam			sc->sc_false_syns = 0;
1050117812Ssam			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1051109323Ssam			sc->sc_syn_timer = 5;
1052109323Ssam		}
1053109323Ssam		ifp->if_timer = 1;
105446492Swpaul	}
105546492Swpaul
1056109323Ssam	/* TODO: rate control */
1057109323Ssam	ieee80211_watchdog(ifp);
105846492Swpaul}
105946492Swpaul
106088546Salfredstatic int
1061109323Ssamwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
106246492Swpaul{
1063109323Ssam	struct wi_softc *sc = ifp->if_softc;
1064109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1065109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
1066109323Ssam	struct ieee80211req *ireq;
1067109323Ssam	u_int8_t nodename[IEEE80211_NWID_LEN];
1068109323Ssam	int error = 0;
106995534Simp#if __FreeBSD_version >= 500000
1070109323Ssam	struct thread *td = curthread;
107195534Simp#else
1072109323Ssam	struct proc *td = curproc;		/* Little white lie */
107395534Simp#endif
1074109323Ssam	struct wi_req wreq;
1075109323Ssam	WI_LOCK_DECL();
107646492Swpaul
1077109323Ssam	WI_LOCK(sc);
107846492Swpaul
107961818Sroberto	if (sc->wi_gone) {
108061818Sroberto		error = ENODEV;
108161818Sroberto		goto out;
108261818Sroberto	}
108346492Swpaul
1084109323Ssam	switch (cmd) {
108546492Swpaul	case SIOCSIFFLAGS:
1086100876Simp		/*
1087101139Simp		 * Can't do promisc and hostap at the same time.  If all that's
1088101139Simp		 * changing is the promisc flag, try to short-circuit a call to
1089101139Simp		 * wi_init() by just setting PROMISC in the hardware.
1090100876Simp		 */
109146492Swpaul		if (ifp->if_flags & IFF_UP) {
1092109323Ssam			if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1093101139Simp			    ifp->if_flags & IFF_RUNNING) {
1094101139Simp				if (ifp->if_flags & IFF_PROMISC &&
1095109323Ssam				    !(sc->sc_if_flags & IFF_PROMISC)) {
1096109323Ssam					wi_write_val(sc, WI_RID_PROMISC, 1);
1097101139Simp				} else if (!(ifp->if_flags & IFF_PROMISC) &&
1098109323Ssam				    sc->sc_if_flags & IFF_PROMISC) {
1099109323Ssam					wi_write_val(sc, WI_RID_PROMISC, 0);
1100101139Simp				} else {
1101101139Simp					wi_init(sc);
1102101139Simp				}
1103100876Simp			} else {
110446492Swpaul				wi_init(sc);
1105100876Simp			}
110646492Swpaul		} else {
110746492Swpaul			if (ifp->if_flags & IFF_RUNNING) {
1108109323Ssam				wi_stop(ifp, 0);
110946492Swpaul			}
111046492Swpaul		}
1111109323Ssam		sc->sc_if_flags = ifp->if_flags;
111246492Swpaul		error = 0;
111346492Swpaul		break;
111446492Swpaul	case SIOCADDMULTI:
111546492Swpaul	case SIOCDELMULTI:
1116109323Ssam		error = wi_write_multi(sc);
111746492Swpaul		break;
1118109323Ssam	case SIOCGIFGENERIC:
1119109323Ssam		error = wi_get_cfg(ifp, cmd, data);
112046492Swpaul		break;
1121109323Ssam	case SIOCSIFGENERIC:
1122109323Ssam		error = suser(td);
112346492Swpaul		if (error)
112446492Swpaul			break;
1125109323Ssam		error = wi_set_cfg(ifp, cmd, data);
112646492Swpaul		break;
112793359Simp	case SIOCGPRISM2DEBUG:
112893359Simp		error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
112993359Simp		if (error)
113093359Simp			break;
113193733Simp		if (!(ifp->if_flags & IFF_RUNNING) ||
113293733Simp		    sc->sc_firmware_type == WI_LUCENT) {
113393359Simp			error = EIO;
113493359Simp			break;
113593359Simp		}
113693359Simp		error = wi_get_debug(sc, &wreq);
113793359Simp		if (error == 0)
113893359Simp			error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
113993359Simp		break;
114093359Simp	case SIOCSPRISM2DEBUG:
114193593Sjhb		if ((error = suser(td)))
114293359Simp			goto out;
114393359Simp		error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
114493359Simp		if (error)
114593359Simp			break;
114693359Simp		error = wi_set_debug(sc, &wreq);
114793359Simp		break;
114877217Sphk	case SIOCG80211:
1149109323Ssam		ireq = (struct ieee80211req *) data;
1150109323Ssam		switch (ireq->i_type) {
115177217Sphk		case IEEE80211_IOC_STATIONNAME:
1152109323Ssam			ireq->i_len = sc->sc_nodelen + 1;
1153109323Ssam			error = copyout(sc->sc_nodename, ireq->i_data,
1154109323Ssam					ireq->i_len);
115577217Sphk			break;
1156109323Ssam		default:
1157109323Ssam			error = ieee80211_ioctl(ifp, cmd, data);
115877217Sphk			break;
115977217Sphk		}
116077217Sphk		break;
116177217Sphk	case SIOCS80211:
1162109323Ssam		error = suser(td);
1163109323Ssam		if (error)
1164109323Ssam			break;
1165109323Ssam		ireq = (struct ieee80211req *) data;
1166109323Ssam		switch (ireq->i_type) {
1167109323Ssam		case IEEE80211_IOC_STATIONNAME:
116877217Sphk			if (ireq->i_val != 0 ||
116977217Sphk			    ireq->i_len > IEEE80211_NWID_LEN) {
117077217Sphk				error = EINVAL;
117177217Sphk				break;
117277217Sphk			}
1173109323Ssam			memset(nodename, 0, IEEE80211_NWID_LEN);
1174109323Ssam			error = copyin(ireq->i_data, nodename, ireq->i_len);
1175109323Ssam			if (error)
117677217Sphk				break;
1177109323Ssam			if (sc->sc_enabled) {
1178109323Ssam				error = wi_write_ssid(sc, WI_RID_NODENAME,
1179109323Ssam					nodename, ireq->i_len);
1180109323Ssam				if (error)
1181109323Ssam					break;
118277217Sphk			}
1183109323Ssam			memcpy(sc->sc_nodename, nodename, IEEE80211_NWID_LEN);
1184109323Ssam			sc->sc_nodelen = ireq->i_len;
118577217Sphk			break;
118677217Sphk		default:
1187109323Ssam			error = ieee80211_ioctl(ifp, cmd, data);
118877217Sphk			break;
118977217Sphk		}
119077217Sphk		break;
119146492Swpaul	default:
1192109323Ssam		error = ieee80211_ioctl(ifp, cmd, data);
119346492Swpaul		break;
119446492Swpaul	}
1195109323Ssam	if (error == ENETRESET) {
1196109323Ssam		if (sc->sc_enabled)
1197116951Ssam			wi_init(sc);	/* XXX no error return */
1198109323Ssam		error = 0;
1199109323Ssam	}
120061818Srobertoout:
1201109323Ssam	WI_UNLOCK(sc);
120246492Swpaul
1203109323Ssam	return (error);
120446492Swpaul}
120546492Swpaul
1206109323Ssamstatic int
1207109323Ssamwi_media_change(struct ifnet *ifp)
120846492Swpaul{
1209109323Ssam	struct wi_softc *sc = ifp->if_softc;
1210116951Ssam	int error;
121146492Swpaul
1212116951Ssam	error = ieee80211_media_change(ifp);
1213109323Ssam	if (error == ENETRESET) {
1214109323Ssam		if (sc->sc_enabled)
1215116951Ssam			wi_init(sc);	/* XXX no error return */
1216109323Ssam		error = 0;
1217109323Ssam	}
1218109323Ssam	return error;
1219109323Ssam}
122046492Swpaul
1221109323Ssamstatic void
1222109323Ssamwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1223109323Ssam{
1224109323Ssam	struct wi_softc *sc = ifp->if_softc;
1225109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1226109323Ssam	u_int16_t val;
1227109323Ssam	int rate, len;
122846492Swpaul
1229109323Ssam	if (sc->wi_gone || !sc->sc_enabled) {
1230109323Ssam		imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
1231109323Ssam		imr->ifm_status = 0;
1232109323Ssam		return;
1233109323Ssam	}
123446492Swpaul
1235109323Ssam	imr->ifm_status = IFM_AVALID;
1236109323Ssam	imr->ifm_active = IFM_IEEE80211;
1237109323Ssam	if (ic->ic_state == IEEE80211_S_RUN &&
1238109323Ssam	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
1239109323Ssam		imr->ifm_status |= IFM_ACTIVE;
1240109323Ssam	len = sizeof(val);
1241109323Ssam	if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) != 0)
1242109323Ssam		rate = 0;
1243109323Ssam	else {
1244109323Ssam		/* convert to 802.11 rate */
1245109323Ssam		rate = val * 2;
1246109323Ssam		if (sc->sc_firmware_type == WI_LUCENT) {
1247112501Simp			if (rate == 4 * 2)
1248109323Ssam				rate = 11;	/* 5.5Mbps */
1249112501Simp			else if (rate == 5 * 2)
1250112501Simp				rate = 22;	/* 11Mbps */
1251109323Ssam		} else {
1252109323Ssam			if (rate == 4*2)
1253109323Ssam				rate = 11;	/* 5.5Mbps */
1254109323Ssam			else if (rate == 8*2)
1255109323Ssam				rate = 22;	/* 11Mbps */
1256109323Ssam		}
1257109323Ssam	}
1258116951Ssam	imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
1259109323Ssam	switch (ic->ic_opmode) {
1260109323Ssam	case IEEE80211_M_STA:
1261109323Ssam		break;
1262109323Ssam	case IEEE80211_M_IBSS:
1263109323Ssam		imr->ifm_active |= IFM_IEEE80211_ADHOC;
1264109323Ssam		break;
1265109323Ssam	case IEEE80211_M_AHDEMO:
1266109323Ssam		imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1267109323Ssam		break;
1268109323Ssam	case IEEE80211_M_HOSTAP:
1269109323Ssam		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1270109323Ssam		break;
1271117855Ssam	case IEEE80211_M_MONITOR:
1272117855Ssam		imr->ifm_active |= IFM_IEEE80211_MONITOR;
1273117855Ssam		break;
1274109323Ssam	}
1275109323Ssam}
127646492Swpaul
1277109323Ssamstatic void
1278109323Ssamwi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1279109323Ssam{
1280109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1281116951Ssam	struct ieee80211_node *ni = ic->ic_bss;
1282109323Ssam	struct ifnet *ifp = &ic->ic_if;
128398440Simp
1284109323Ssam	if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1285109323Ssam		return;
128646492Swpaul
1287109323Ssam	DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid)));
1288109323Ssam	DPRINTF(("%s ?\n", ether_sprintf(new_bssid)));
128946492Swpaul
1290109323Ssam	/* In promiscuous mode, the BSSID field is not a reliable
1291109323Ssam	 * indicator of the firmware's BSSID. Damp spurious
1292109323Ssam	 * change-of-BSSID indications.
1293109323Ssam	 */
1294109323Ssam	if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1295109323Ssam	    sc->sc_false_syns >= WI_MAX_FALSE_SYNS)
1296109323Ssam		return;
129746492Swpaul
1298117812Ssam	ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1299109323Ssam}
130046492Swpaul
1301109323Ssamstatic void
1302116898Ssamwi_rx_monitor(struct wi_softc *sc, int fid)
1303116898Ssam{
1304116898Ssam	struct ieee80211com *ic = &sc->sc_ic;
1305116898Ssam	struct ifnet *ifp = &ic->ic_if;
1306116898Ssam	struct wi_frame *rx_frame;
1307116898Ssam	struct mbuf *m;
1308116898Ssam	int datlen, hdrlen;
1309116898Ssam
1310116898Ssam	/* first allocate mbuf for packet storage */
1311116898Ssam	m = m_getcl(M_DONTWAIT, MT_DATA, 0);
1312116898Ssam	if (m == NULL) {
1313116898Ssam		ifp->if_ierrors++;
1314116898Ssam		return;
1315116898Ssam	}
1316116898Ssam
1317116898Ssam	m->m_pkthdr.rcvif = ifp;
1318116898Ssam
1319116898Ssam	/* now read wi_frame first so we know how much data to read */
1320116898Ssam	if (wi_read_bap(sc, fid, 0, mtod(m, caddr_t), sizeof(*rx_frame))) {
1321116898Ssam		ifp->if_ierrors++;
1322116898Ssam		goto done;
1323116898Ssam	}
1324116898Ssam
1325116898Ssam	rx_frame = mtod(m, struct wi_frame *);
1326116898Ssam
1327116898Ssam	switch ((rx_frame->wi_status & WI_STAT_MAC_PORT) >> 8) {
1328116898Ssam	case 7:
1329116898Ssam		switch (rx_frame->wi_whdr.i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1330116898Ssam		case IEEE80211_FC0_TYPE_DATA:
1331116898Ssam			hdrlen = WI_DATA_HDRLEN;
1332116898Ssam			datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1333116898Ssam			break;
1334116898Ssam		case IEEE80211_FC0_TYPE_MGT:
1335116898Ssam			hdrlen = WI_MGMT_HDRLEN;
1336116898Ssam			datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1337116898Ssam			break;
1338116898Ssam		case IEEE80211_FC0_TYPE_CTL:
1339116898Ssam			/*
1340116898Ssam			 * prism2 cards don't pass control packets
1341116898Ssam			 * down properly or consistently, so we'll only
1342116898Ssam			 * pass down the header.
1343116898Ssam			 */
1344116898Ssam			hdrlen = WI_CTL_HDRLEN;
1345116898Ssam			datlen = 0;
1346116898Ssam			break;
1347116898Ssam		default:
1348116898Ssam			if_printf(ifp, "received packet of unknown type "
1349116898Ssam				"on port 7\n");
1350116898Ssam			ifp->if_ierrors++;
1351116898Ssam			goto done;
1352116898Ssam		}
1353116898Ssam		break;
1354116898Ssam	case 0:
1355116898Ssam		hdrlen = WI_DATA_HDRLEN;
1356116898Ssam		datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1357116898Ssam		break;
1358116898Ssam	default:
1359116898Ssam		if_printf(ifp, "received packet on invalid "
1360116898Ssam		    "port (wi_status=0x%x)\n", rx_frame->wi_status);
1361116898Ssam		ifp->if_ierrors++;
1362116898Ssam		goto done;
1363116898Ssam	}
1364116898Ssam
1365116898Ssam	if (hdrlen + datlen + 2 > MCLBYTES) {
1366116898Ssam		if_printf(ifp, "oversized packet received "
1367116898Ssam		    "(wi_dat_len=%d, wi_status=0x%x)\n",
1368116898Ssam		    datlen, rx_frame->wi_status);
1369116898Ssam		ifp->if_ierrors++;
1370116898Ssam		goto done;
1371116898Ssam	}
1372116898Ssam
1373116898Ssam	if (wi_read_bap(sc, fid, hdrlen, mtod(m, caddr_t) + hdrlen,
1374116898Ssam	    datlen + 2) == 0) {
1375116898Ssam		m->m_pkthdr.len = m->m_len = hdrlen + datlen;
1376116898Ssam		ifp->if_ipackets++;
1377116898Ssam		BPF_MTAP(ifp, m);	/* Handle BPF listeners. */
1378116898Ssam	} else
1379116898Ssam		ifp->if_ierrors++;
1380116898Ssamdone:
1381116898Ssam	m_freem(m);
1382116898Ssam}
1383116898Ssam
1384116898Ssamstatic void
1385109323Ssamwi_rx_intr(struct wi_softc *sc)
1386109323Ssam{
1387109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1388109323Ssam	struct ifnet *ifp = &ic->ic_if;
1389109323Ssam	struct wi_frame frmhdr;
1390109323Ssam	struct mbuf *m;
1391109323Ssam	struct ieee80211_frame *wh;
1392119150Ssam	struct ieee80211_node *ni;
1393109323Ssam	int fid, len, off, rssi;
1394109323Ssam	u_int8_t dir;
1395109323Ssam	u_int16_t status;
1396109323Ssam	u_int32_t rstamp;
139746611Swpaul
1398109323Ssam	fid = CSR_READ_2(sc, WI_RX_FID);
139946611Swpaul
1400116898Ssam	if (sc->wi_debug.wi_monitor) {
1401116898Ssam		/*
1402116898Ssam		 * If we are in monitor mode just
1403116898Ssam		 * read the data from the device.
1404116898Ssam		 */
1405116898Ssam		wi_rx_monitor(sc, fid);
1406116898Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1407116898Ssam		return;
1408116898Ssam	}
1409116898Ssam
1410109323Ssam	/* First read in the frame header */
1411109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1412109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1413109323Ssam		ifp->if_ierrors++;
1414109323Ssam		DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1415109323Ssam		return;
1416109323Ssam	}
141791695Simp
1418109323Ssam	if (IFF_DUMPPKTS(ifp))
1419109323Ssam		wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal);
142046492Swpaul
1421109323Ssam	/*
1422109323Ssam	 * Drop undecryptable or packets with receive errors here
1423109323Ssam	 */
1424109323Ssam	status = le16toh(frmhdr.wi_status);
1425109323Ssam	if (status & WI_STAT_ERRSTAT) {
1426109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1427109323Ssam		ifp->if_ierrors++;
1428109323Ssam		DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1429109323Ssam		return;
1430109323Ssam	}
1431109323Ssam	rssi = frmhdr.wi_rx_signal;
1432109323Ssam	rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1433109323Ssam	    le16toh(frmhdr.wi_rx_tstamp1);
143446492Swpaul
1435109323Ssam	len = le16toh(frmhdr.wi_dat_len);
1436109323Ssam	off = ALIGN(sizeof(struct ieee80211_frame));
143746563Swpaul
1438117855Ssam	/*
1439117855Ssam	 * Sometimes the PRISM2.x returns bogusly large frames. Except
1440117855Ssam	 * in monitor mode, just throw them away.
1441117855Ssam	 */
1442117855Ssam	if (off + len > MCLBYTES) {
1443117855Ssam		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1444117855Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1445117855Ssam			ifp->if_ierrors++;
1446117855Ssam			DPRINTF(("wi_rx_intr: oversized packet\n"));
1447117855Ssam			return;
1448117855Ssam		} else
1449117855Ssam			len = 0;
1450117855Ssam	}
1451117855Ssam
1452111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
1453109323Ssam	if (m == NULL) {
1454109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1455109323Ssam		ifp->if_ierrors++;
1456109323Ssam		DPRINTF(("wi_rx_intr: MGET failed\n"));
1457109323Ssam		return;
1458109323Ssam	}
1459109323Ssam	if (off + len > MHLEN) {
1460111119Simp		MCLGET(m, M_DONTWAIT);
1461109323Ssam		if ((m->m_flags & M_EXT) == 0) {
1462109323Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1463109323Ssam			m_freem(m);
1464109323Ssam			ifp->if_ierrors++;
1465109323Ssam			DPRINTF(("wi_rx_intr: MCLGET failed\n"));
1466109323Ssam			return;
1467109323Ssam		}
1468109323Ssam	}
146946492Swpaul
1470109323Ssam	m->m_data += off - sizeof(struct ieee80211_frame);
1471109323Ssam	memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1472109323Ssam	wi_read_bap(sc, fid, sizeof(frmhdr),
1473109323Ssam	    m->m_data + sizeof(struct ieee80211_frame), len);
1474109323Ssam	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1475109323Ssam	m->m_pkthdr.rcvif = ifp;
147694405Simp
1477109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
147846492Swpaul
1479109323Ssam#if NBPFILTER > 0
1480109323Ssam	if (sc->sc_drvbpf) {
1481109323Ssam		struct mbuf *mb;
1482100876Simp
1483119784Ssam		/* XXX pre-allocate space when setting up recv's */
1484111119Simp		MGETHDR(mb, M_DONTWAIT, m->m_type);
1485109323Ssam		if (mb != NULL) {
1486119784Ssam			/* XXX replace divide by table */
1487119784Ssam			sc->sc_rx_th.wr_rate = frmhdr.wi_rx_rate / 5;
1488119784Ssam			sc->sc_rx_th.wr_antsignal =
1489119784Ssam				WI_RSSI_TO_DBM(sc, frmhdr.wi_rx_signal);
1490119784Ssam			sc->sc_rx_th.wr_antnoise =
1491119784Ssam				WI_RSSI_TO_DBM(sc, frmhdr.wi_rx_silence);
1492119784Ssam			sc->sc_rx_th.wr_time =
1493119784Ssam				htole32((frmhdr.wi_rx_tstamp1 << 16) |
1494119784Ssam					frmhdr.wi_rx_tstamp0);
1495119784Ssam
1496111119Simp			(void) m_dup_pkthdr(mb, m, M_DONTWAIT);
1497109323Ssam			mb->m_next = m;
1498119784Ssam			mb->m_data = (caddr_t)&sc->sc_rx_th;
1499119784Ssam			mb->m_len = sizeof(sc->sc_rx_th);
1500109323Ssam			mb->m_pkthdr.len += mb->m_len;
1501109323Ssam			bpf_mtap(sc->sc_drvbpf, mb);
1502109323Ssam			m_free(mb);
150391695Simp		}
150456965Swpaul	}
1505109323Ssam#endif
1506109323Ssam	wh = mtod(m, struct ieee80211_frame *);
1507109323Ssam	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1508109323Ssam		/*
1509109323Ssam		 * WEP is decrypted by hardware. Clear WEP bit
1510109323Ssam		 * header for ieee80211_input().
1511109323Ssam		 */
1512109323Ssam		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1513109323Ssam	}
151456965Swpaul
1515109323Ssam	/* synchronize driver's BSSID with firmware's BSSID */
1516109323Ssam	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1517109323Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1518109323Ssam		wi_sync_bssid(sc, wh->i_addr3);
151946492Swpaul
1520119150Ssam	/*
1521119150Ssam	 * Locate the node for sender, track state, and
1522119150Ssam	 * then pass this node (referenced) up to the 802.11
1523119150Ssam	 * layer for its use.  We are required to pass
1524119150Ssam	 * something so we fallback to ic_bss when this frame
1525119150Ssam	 * is from an unknown sender.
1526119150Ssam	 */
1527119150Ssam	if (ic->ic_opmode != IEEE80211_M_STA) {
1528119150Ssam		ni = ieee80211_find_node(ic, wh->i_addr2);
1529119150Ssam		if (ni == NULL)
1530119150Ssam			ni = ieee80211_ref_node(ic->ic_bss);
1531119150Ssam	} else
1532119150Ssam		ni = ieee80211_ref_node(ic->ic_bss);
1533119150Ssam	/*
1534119150Ssam	 * Send frame up for processing.
1535119150Ssam	 */
1536119150Ssam	ieee80211_input(ifp, m, ni, rssi, rstamp);
1537119150Ssam	/*
1538119150Ssam	 * The frame may have caused the node to be marked for
1539119150Ssam	 * reclamation (e.g. in response to a DEAUTH message)
1540119150Ssam	 * so use free_node here instead of unref_node.
1541119150Ssam	 */
1542119150Ssam	if (ni == ic->ic_bss)
1543119150Ssam		ieee80211_unref_node(&ni);
1544119150Ssam	else
1545119150Ssam		ieee80211_free_node(ic, ni);
1546109323Ssam}
154746492Swpaul
1548109323Ssamstatic void
1549109323Ssamwi_tx_ex_intr(struct wi_softc *sc)
1550109323Ssam{
1551109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1552109323Ssam	struct ifnet *ifp = &ic->ic_if;
1553109323Ssam	struct wi_frame frmhdr;
1554109323Ssam	int fid;
155546492Swpaul
1556109323Ssam	fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1557109323Ssam	/* Read in the frame header */
1558109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1559109323Ssam		u_int16_t status = le16toh(frmhdr.wi_status);
156046492Swpaul
1561109323Ssam		/*
1562109323Ssam		 * Spontaneous station disconnects appear as xmit
1563109323Ssam		 * errors.  Don't announce them and/or count them
1564109323Ssam		 * as an output error.
1565109323Ssam		 */
1566109323Ssam		if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1567109323Ssam			if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1568109323Ssam				if_printf(ifp, "tx failed");
1569109323Ssam				if (status & WI_TXSTAT_RET_ERR)
1570109323Ssam					printf(", retry limit exceeded");
1571109323Ssam				if (status & WI_TXSTAT_AGED_ERR)
1572109323Ssam					printf(", max transmit lifetime exceeded");
1573109323Ssam				if (status & WI_TXSTAT_DISCONNECT)
1574109323Ssam					printf(", port disconnected");
1575109323Ssam				if (status & WI_TXSTAT_FORM_ERR)
1576109323Ssam					printf(", invalid format (data len %u src %6D)",
1577109323Ssam						le16toh(frmhdr.wi_dat_len),
1578109323Ssam						frmhdr.wi_ehdr.ether_shost, ":");
1579109323Ssam				if (status & ~0xf)
1580109323Ssam					printf(", status=0x%x", status);
1581109323Ssam				printf("\n");
1582109323Ssam			}
1583109323Ssam			ifp->if_oerrors++;
1584109323Ssam		} else {
1585109323Ssam			DPRINTF(("port disconnected\n"));
1586109323Ssam			ifp->if_collisions++;	/* XXX */
1587109323Ssam		}
1588109323Ssam	} else
1589109323Ssam		DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
1590109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1591109323Ssam}
159246492Swpaul
1593109323Ssamstatic void
1594109323Ssamwi_tx_intr(struct wi_softc *sc)
1595109323Ssam{
1596109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1597109323Ssam	struct ifnet *ifp = &ic->ic_if;
1598109323Ssam	int fid, cur;
159994405Simp
1600109323Ssam	fid = CSR_READ_2(sc, WI_ALLOC_FID);
1601109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
160246492Swpaul
1603109323Ssam	cur = sc->sc_txcur;
1604109323Ssam	if (sc->sc_txd[cur].d_fid != fid) {
1605109323Ssam		if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
1606109323Ssam		    fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
1607109323Ssam		return;
1608109323Ssam	}
1609109323Ssam	sc->sc_tx_timer = 0;
1610109323Ssam	sc->sc_txd[cur].d_len = 0;
1611112363Simp	sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
1612109323Ssam	if (sc->sc_txd[cur].d_len == 0)
1613109323Ssam		ifp->if_flags &= ~IFF_OACTIVE;
1614109323Ssam	else {
1615109323Ssam		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1616109323Ssam		    0, 0)) {
1617109323Ssam			if_printf(ifp, "xmit failed\n");
1618109323Ssam			sc->sc_txd[cur].d_len = 0;
1619109323Ssam		} else {
1620109323Ssam			sc->sc_tx_timer = 5;
1621109323Ssam			ifp->if_timer = 1;
1622109323Ssam		}
1623109323Ssam	}
162446492Swpaul}
162546492Swpaul
162688546Salfredstatic void
1627109323Ssamwi_info_intr(struct wi_softc *sc)
162894405Simp{
1629109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1630109323Ssam	struct ifnet *ifp = &ic->ic_if;
1631109323Ssam	int i, fid, len, off;
1632109323Ssam	u_int16_t ltbuf[2];
1633109323Ssam	u_int16_t stat;
1634109323Ssam	u_int32_t *ptr;
163594405Simp
1636109323Ssam	fid = CSR_READ_2(sc, WI_INFO_FID);
1637109323Ssam	wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
163894405Simp
1639109323Ssam	switch (le16toh(ltbuf[1])) {
164094405Simp
1641109323Ssam	case WI_INFO_LINK_STAT:
1642109323Ssam		wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1643109323Ssam		DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1644109323Ssam		switch (le16toh(stat)) {
1645109323Ssam		case WI_INFO_LINK_STAT_CONNECTED:
1646109323Ssam			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1647109323Ssam			if (ic->ic_state == IEEE80211_S_RUN &&
1648109323Ssam			    ic->ic_opmode != IEEE80211_M_IBSS)
1649109323Ssam				break;
1650109323Ssam			/* FALLTHROUGH */
1651109323Ssam		case WI_INFO_LINK_STAT_AP_CHG:
1652117812Ssam			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1653109323Ssam			break;
1654109323Ssam		case WI_INFO_LINK_STAT_AP_INR:
1655109323Ssam			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1656109323Ssam			break;
1657109323Ssam		case WI_INFO_LINK_STAT_AP_OOR:
1658109323Ssam			if (sc->sc_firmware_type == WI_SYMBOL &&
1659109323Ssam			    sc->sc_scan_timer > 0) {
1660109323Ssam				if (wi_cmd(sc, WI_CMD_INQUIRE,
1661109323Ssam				    WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
1662109323Ssam					sc->sc_scan_timer = 0;
1663109323Ssam				break;
1664109323Ssam			}
1665109323Ssam			if (ic->ic_opmode == IEEE80211_M_STA)
1666109323Ssam				sc->sc_flags |= WI_FLAGS_OUTRANGE;
1667109323Ssam			break;
1668109323Ssam		case WI_INFO_LINK_STAT_DISCONNECTED:
1669109323Ssam		case WI_INFO_LINK_STAT_ASSOC_FAILED:
1670109323Ssam			if (ic->ic_opmode == IEEE80211_M_STA)
1671117812Ssam				ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1672109323Ssam			break;
1673109323Ssam		}
1674109323Ssam		break;
167594405Simp
1676109323Ssam	case WI_INFO_COUNTERS:
1677109323Ssam		/* some card versions have a larger stats structure */
1678109323Ssam		len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1679109323Ssam		ptr = (u_int32_t *)&sc->sc_stats;
1680109323Ssam		off = sizeof(ltbuf);
1681109323Ssam		for (i = 0; i < len; i++, off += 2, ptr++) {
1682109323Ssam			wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1683109323Ssam#ifdef WI_HERMES_STATS_WAR
1684109323Ssam			if (stat & 0xf000)
1685109323Ssam				stat = ~stat;
1686109323Ssam#endif
1687109323Ssam			*ptr += stat;
1688109323Ssam		}
1689109323Ssam		ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1690109323Ssam		    sc->sc_stats.wi_tx_multi_retries +
1691109323Ssam		    sc->sc_stats.wi_tx_retry_limit;
1692109323Ssam		break;
1693109323Ssam
1694109323Ssam	case WI_INFO_SCAN_RESULTS:
1695109323Ssam	case WI_INFO_HOST_SCAN_RESULTS:
1696109323Ssam		wi_scan_result(sc, fid, le16toh(ltbuf[0]));
1697109323Ssam		break;
1698109323Ssam
1699109323Ssam	default:
1700109323Ssam		DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1701109323Ssam		    le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1702109323Ssam		break;
170394405Simp	}
1704109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1705109323Ssam}
170694405Simp
1707109323Ssamstatic int
1708109323Ssamwi_write_multi(struct wi_softc *sc)
1709109323Ssam{
1710109323Ssam	struct ifnet *ifp = &sc->sc_ic.ic_if;
1711109323Ssam	int n;
1712109323Ssam	struct ifmultiaddr *ifma;
1713109323Ssam	struct wi_mcast mlist;
171494472Simp
1715109323Ssam	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1716109323Ssamallmulti:
1717109323Ssam		memset(&mlist, 0, sizeof(mlist));
1718109323Ssam		return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1719109323Ssam		    sizeof(mlist));
172094405Simp	}
172194405Simp
1722109323Ssam	n = 0;
1723109323Ssam#if __FreeBSD_version < 500000
1724109323Ssam	LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1725109323Ssam#else
1726109323Ssam	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1727109323Ssam#endif
1728109323Ssam		if (ifma->ifma_addr->sa_family != AF_LINK)
1729109323Ssam			continue;
1730109323Ssam		if (n >= 16)
1731109323Ssam			goto allmulti;
1732109323Ssam		IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1733109323Ssam		    (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1734109323Ssam		n++;
173594405Simp	}
1736109323Ssam	return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1737109323Ssam	    IEEE80211_ADDR_LEN * n);
173894405Simp}
173994405Simp
174094405Simpstatic void
1741109323Ssamwi_read_nicid(struct wi_softc *sc)
174246492Swpaul{
1743109323Ssam	struct wi_card_ident *id;
1744109323Ssam	char *p;
1745109323Ssam	int len;
1746109323Ssam	u_int16_t ver[4];
174746492Swpaul
1748109323Ssam	/* getting chip identity */
1749109323Ssam	memset(ver, 0, sizeof(ver));
1750109323Ssam	len = sizeof(ver);
1751109323Ssam	wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1752109323Ssam	device_printf(sc->sc_dev, "using ");
175346492Swpaul
1754109323Ssam	sc->sc_firmware_type = WI_NOTYPE;
1755109323Ssam	for (id = wi_card_ident; id->card_name != NULL; id++) {
1756109323Ssam		if (le16toh(ver[0]) == id->card_id) {
1757109323Ssam			printf("%s", id->card_name);
1758109323Ssam			sc->sc_firmware_type = id->firm_type;
1759109323Ssam			break;
1760109323Ssam		}
176167092Swpaul	}
1762109323Ssam	if (sc->sc_firmware_type == WI_NOTYPE) {
1763109323Ssam		if (le16toh(ver[0]) & 0x8000) {
1764109323Ssam			printf("Unknown PRISM2 chip");
1765109323Ssam			sc->sc_firmware_type = WI_INTERSIL;
1766109323Ssam		} else {
1767109323Ssam			printf("Unknown Lucent chip");
1768109323Ssam			sc->sc_firmware_type = WI_LUCENT;
1769109323Ssam		}
177067092Swpaul	}
177146492Swpaul
1772109323Ssam	/* get primary firmware version (Only Prism chips) */
1773109323Ssam	if (sc->sc_firmware_type != WI_LUCENT) {
1774109323Ssam		memset(ver, 0, sizeof(ver));
1775109323Ssam		len = sizeof(ver);
1776109323Ssam		wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1777109323Ssam		sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1778109323Ssam		    le16toh(ver[3]) * 100 + le16toh(ver[1]);
177967092Swpaul	}
178046492Swpaul
1781109323Ssam	/* get station firmware version */
1782109323Ssam	memset(ver, 0, sizeof(ver));
1783109323Ssam	len = sizeof(ver);
1784109323Ssam	wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1785109323Ssam	sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1786109323Ssam	    le16toh(ver[3]) * 100 + le16toh(ver[1]);
1787109323Ssam	if (sc->sc_firmware_type == WI_INTERSIL &&
1788109323Ssam	    (sc->sc_sta_firmware_ver == 10102 ||
1789109323Ssam	     sc->sc_sta_firmware_ver == 20102)) {
1790109323Ssam		char ident[12];
1791109323Ssam		memset(ident, 0, sizeof(ident));
1792109323Ssam		len = sizeof(ident);
1793109323Ssam		/* value should be the format like "V2.00-11" */
1794109323Ssam		if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1795109323Ssam		    *(p = (char *)ident) >= 'A' &&
1796109323Ssam		    p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1797109323Ssam			sc->sc_firmware_type = WI_SYMBOL;
1798109323Ssam			sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1799109323Ssam			    (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1800109323Ssam			    (p[6] - '0') * 10 + (p[7] - '0');
180194405Simp		}
180294405Simp	}
1803109323Ssam	printf("\n");
1804109323Ssam	device_printf(sc->sc_dev, "%s Firmware: ",
1805109323Ssam	     sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
1806109323Ssam	    (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
1807109323Ssam	if (sc->sc_firmware_type != WI_LUCENT)	/* XXX */
1808109323Ssam		printf("Primary (%u.%u.%u), ",
1809109323Ssam		    sc->sc_pri_firmware_ver / 10000,
1810109323Ssam		    (sc->sc_pri_firmware_ver % 10000) / 100,
1811109323Ssam		    sc->sc_pri_firmware_ver % 100);
1812109323Ssam	printf("Station (%u.%u.%u)\n",
1813109323Ssam	    sc->sc_sta_firmware_ver / 10000,
1814109323Ssam	    (sc->sc_sta_firmware_ver % 10000) / 100,
1815109323Ssam	    sc->sc_sta_firmware_ver % 100);
1816109323Ssam}
181746492Swpaul
1818109323Ssamstatic int
1819109323Ssamwi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1820109323Ssam{
1821109323Ssam	struct wi_ssid ssid;
182246492Swpaul
1823109323Ssam	if (buflen > IEEE80211_NWID_LEN)
1824109323Ssam		return ENOBUFS;
1825109323Ssam	memset(&ssid, 0, sizeof(ssid));
1826109323Ssam	ssid.wi_len = htole16(buflen);
1827109323Ssam	memcpy(ssid.wi_ssid, buf, buflen);
1828109323Ssam	return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1829109323Ssam}
183046492Swpaul
1831109323Ssamstatic int
1832109323Ssamwi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
1833109323Ssam{
1834109323Ssam	struct wi_softc *sc = ifp->if_softc;
1835109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1836109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
1837109323Ssam	struct wi_req wreq;
1838116898Ssam	struct wi_scan_res *res;
1839116898Ssam	int len, n, error, mif, val, off, i;
184046492Swpaul
1841109323Ssam	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1842109323Ssam	if (error)
1843109323Ssam		return error;
1844109323Ssam	len = (wreq.wi_len - 1) * 2;
1845109323Ssam	if (len < sizeof(u_int16_t))
1846109323Ssam		return ENOSPC;
1847109323Ssam	if (len > sizeof(wreq.wi_val))
1848109323Ssam		len = sizeof(wreq.wi_val);
184946492Swpaul
1850109323Ssam	switch (wreq.wi_type) {
185146492Swpaul
1852109323Ssam	case WI_RID_IFACE_STATS:
1853109323Ssam		memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
1854109323Ssam		if (len < sizeof(sc->sc_stats))
1855109323Ssam			error = ENOSPC;
1856109323Ssam		else
1857109323Ssam			len = sizeof(sc->sc_stats);
1858109323Ssam		break;
185946492Swpaul
1860109323Ssam	case WI_RID_ENCRYPTION:
1861109323Ssam	case WI_RID_TX_CRYPT_KEY:
1862109323Ssam	case WI_RID_DEFLT_CRYPT_KEYS:
1863109323Ssam	case WI_RID_TX_RATE:
1864109323Ssam		return ieee80211_cfgget(ifp, cmd, data);
186546492Swpaul
1866109323Ssam	case WI_RID_MICROWAVE_OVEN:
1867109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
1868109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1869109323Ssam			    &len);
1870109323Ssam			break;
1871109323Ssam		}
1872109323Ssam		wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
1873109323Ssam		len = sizeof(u_int16_t);
1874109323Ssam		break;
187546492Swpaul
1876109323Ssam	case WI_RID_DBM_ADJUST:
1877109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
1878109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1879109323Ssam			    &len);
1880109323Ssam			break;
1881109323Ssam		}
1882119784Ssam		wreq.wi_val[0] = htole16(sc->sc_dbm_offset);
1883109323Ssam		len = sizeof(u_int16_t);
1884109323Ssam		break;
188546492Swpaul
1886109323Ssam	case WI_RID_ROAMING_MODE:
1887109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
1888109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1889109323Ssam			    &len);
1890109323Ssam			break;
1891109323Ssam		}
1892109323Ssam		wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
1893109323Ssam		len = sizeof(u_int16_t);
1894109323Ssam		break;
189546492Swpaul
1896109323Ssam	case WI_RID_SYSTEM_SCALE:
1897109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
1898109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1899109323Ssam			    &len);
1900109323Ssam			break;
1901109323Ssam		}
1902109323Ssam		wreq.wi_val[0] = htole16(sc->sc_system_scale);
1903109323Ssam		len = sizeof(u_int16_t);
1904109323Ssam		break;
190546492Swpaul
1906109323Ssam	case WI_RID_FRAG_THRESH:
1907109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
1908109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1909109323Ssam			    &len);
1910109323Ssam			break;
1911109323Ssam		}
1912116951Ssam		wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
1913109323Ssam		len = sizeof(u_int16_t);
1914109323Ssam		break;
191546492Swpaul
1916109323Ssam	case WI_RID_READ_APS:
1917109323Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1918109323Ssam			return ieee80211_cfgget(ifp, cmd, data);
1919109323Ssam		if (sc->sc_scan_timer > 0) {
1920109323Ssam			error = EINPROGRESS;
1921109323Ssam			break;
1922109323Ssam		}
1923109323Ssam		n = sc->sc_naps;
1924109323Ssam		if (len < sizeof(n)) {
1925109323Ssam			error = ENOSPC;
1926109323Ssam			break;
1927109323Ssam		}
1928109323Ssam		if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
1929109323Ssam			n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
1930109323Ssam		len = sizeof(n) + sizeof(struct wi_apinfo) * n;
1931109323Ssam		memcpy(wreq.wi_val, &n, sizeof(n));
1932109323Ssam		memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
1933109323Ssam		    sizeof(struct wi_apinfo) * n);
1934109323Ssam		break;
193546492Swpaul
1936109323Ssam	case WI_RID_PRISM2:
1937109323Ssam		wreq.wi_val[0] = sc->sc_firmware_type != WI_LUCENT;
1938109323Ssam		len = sizeof(u_int16_t);
1939109323Ssam		break;
194046492Swpaul
1941109323Ssam	case WI_RID_MIF:
1942109323Ssam		mif = wreq.wi_val[0];
1943109323Ssam		error = wi_cmd(sc, WI_CMD_READMIF, mif, 0, 0);
1944109323Ssam		val = CSR_READ_2(sc, WI_RESP0);
1945109323Ssam		wreq.wi_val[0] = val;
1946109323Ssam		len = sizeof(u_int16_t);
1947109323Ssam		break;
194846492Swpaul
1949109323Ssam	case WI_RID_ZERO_CACHE:
1950109323Ssam	case WI_RID_PROCFRAME:		/* ignore for compatibility */
1951109323Ssam		/* XXX ??? */
1952109323Ssam		break;
195346492Swpaul
1954109323Ssam	case WI_RID_READ_CACHE:
1955109323Ssam		return ieee80211_cfgget(ifp, cmd, data);
195646492Swpaul
1957116898Ssam	case WI_RID_SCAN_RES:		/* compatibility interface */
1958116898Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1959116898Ssam			return ieee80211_cfgget(ifp, cmd, data);
1960116898Ssam		if (sc->sc_scan_timer > 0) {
1961116898Ssam			error = EINPROGRESS;
1962116898Ssam			break;
1963116898Ssam		}
1964116898Ssam		n = sc->sc_naps;
1965116898Ssam		off = sc->sc_firmware_type != WI_LUCENT ?
1966116898Ssam			sizeof(struct wi_scan_p2_hdr) : 0;
1967116898Ssam		if (len < off + sizeof(struct wi_scan_res) * n)
1968116898Ssam			n = (len - off) / sizeof(struct wi_scan_res);
1969116898Ssam		len = off + sizeof(struct wi_scan_res) * n;
1970116898Ssam		if (off != 0) {
1971116898Ssam			struct wi_scan_p2_hdr *p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1972116898Ssam			/*
1973116898Ssam			 * Prepend Prism-specific header.
1974116898Ssam			 */
1975116898Ssam			if (len < sizeof(struct wi_scan_p2_hdr)) {
1976116898Ssam				error = ENOSPC;
1977116898Ssam				break;
1978116898Ssam			}
1979116898Ssam			p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1980116898Ssam			p2->wi_rsvd = 0;
1981116898Ssam			p2->wi_reason = n;	/* XXX */
1982116898Ssam		}
1983116898Ssam		for (i = 0; i < n; i++) {
1984116898Ssam			const struct wi_apinfo *ap = &sc->sc_aps[i];
1985116898Ssam
1986116898Ssam			res = (struct wi_scan_res *)((char *)wreq.wi_val + off);
1987116898Ssam			res->wi_chan = ap->channel;
1988116898Ssam			res->wi_noise = ap->noise;
1989116898Ssam			res->wi_signal = ap->signal;
1990116898Ssam			IEEE80211_ADDR_COPY(res->wi_bssid, ap->bssid);
1991116898Ssam			res->wi_interval = ap->interval;
1992116898Ssam			res->wi_capinfo = ap->capinfo;
1993116898Ssam			res->wi_ssid_len = ap->namelen;
1994116898Ssam			memcpy(res->wi_ssid, ap->name,
1995116898Ssam				IEEE80211_NWID_LEN);
1996116898Ssam			if (sc->sc_firmware_type != WI_LUCENT) {
1997116898Ssam				/* XXX not saved from Prism cards */
1998116898Ssam				memset(res->wi_srates, 0,
1999116898Ssam					sizeof(res->wi_srates));
2000116898Ssam				res->wi_rate = ap->rate;
2001116898Ssam				res->wi_rsvd = 0;
2002116898Ssam				off += WI_PRISM2_RES_SIZE;
2003116898Ssam			} else
2004116898Ssam				off += WI_WAVELAN_RES_SIZE;
2005116898Ssam		}
2006116898Ssam		break;
2007116898Ssam
2008109323Ssam	default:
2009109323Ssam		if (sc->sc_enabled) {
2010109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2011109323Ssam			    &len);
2012109323Ssam			break;
2013109323Ssam		}
2014109323Ssam		switch (wreq.wi_type) {
2015109323Ssam		case WI_RID_MAX_DATALEN:
2016109323Ssam			wreq.wi_val[0] = htole16(sc->sc_max_datalen);
2017109323Ssam			len = sizeof(u_int16_t);
2018109323Ssam			break;
2019109323Ssam		case WI_RID_RTS_THRESH:
2020116951Ssam			wreq.wi_val[0] = htole16(ic->ic_rtsthreshold);
2021109323Ssam			len = sizeof(u_int16_t);
2022109323Ssam			break;
2023109323Ssam		case WI_RID_CNFAUTHMODE:
2024109323Ssam			wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
2025109323Ssam			len = sizeof(u_int16_t);
2026109323Ssam			break;
2027109323Ssam		case WI_RID_NODENAME:
2028109323Ssam			if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
2029109323Ssam				error = ENOSPC;
2030109323Ssam				break;
2031109323Ssam			}
2032109323Ssam			len = sc->sc_nodelen + sizeof(u_int16_t);
2033109323Ssam			wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
2034109323Ssam			memcpy(&wreq.wi_val[1], sc->sc_nodename,
2035109323Ssam			    sc->sc_nodelen);
2036109323Ssam			break;
2037109323Ssam		default:
2038109323Ssam			return ieee80211_cfgget(ifp, cmd, data);
2039109323Ssam		}
2040109323Ssam		break;
204146492Swpaul	}
2042109323Ssam	if (error)
2043109323Ssam		return error;
2044109323Ssam	wreq.wi_len = (len + 1) / 2 + 1;
2045109323Ssam	return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
204646492Swpaul}
204746492Swpaul
2048109323Ssamstatic int
2049109323Ssamwi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
205046492Swpaul{
2051109323Ssam	struct wi_softc *sc = ifp->if_softc;
2052109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2053109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
2054109323Ssam	struct wi_req wreq;
2055109323Ssam	struct mbuf *m;
2056109323Ssam	int i, len, error, mif, val;
2057116951Ssam	struct ieee80211_rateset *rs;
205846492Swpaul
2059109323Ssam	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2060109323Ssam	if (error)
2061109323Ssam		return error;
2062116898Ssam	len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0;
2063109323Ssam	switch (wreq.wi_type) {
2064109323Ssam	case WI_RID_DBM_ADJUST:
2065109323Ssam		return ENODEV;
206667092Swpaul
2067109323Ssam	case WI_RID_NODENAME:
2068109323Ssam		if (le16toh(wreq.wi_val[0]) * 2 > len ||
2069109323Ssam		    le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
2070109323Ssam			error = ENOSPC;
2071109323Ssam			break;
2072109323Ssam		}
2073109323Ssam		if (sc->sc_enabled) {
2074109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2075109323Ssam			    len);
2076109323Ssam			if (error)
2077109323Ssam				break;
2078109323Ssam		}
2079109323Ssam		sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
2080109323Ssam		memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen);
2081109323Ssam		break;
208246492Swpaul
2083109323Ssam	case WI_RID_MICROWAVE_OVEN:
2084109323Ssam	case WI_RID_ROAMING_MODE:
2085109323Ssam	case WI_RID_SYSTEM_SCALE:
2086109323Ssam	case WI_RID_FRAG_THRESH:
2087109323Ssam		if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
2088109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
2089109323Ssam			break;
2090109323Ssam		if (wreq.wi_type == WI_RID_ROAMING_MODE &&
2091109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
2092109323Ssam			break;
2093109323Ssam		if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
2094109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
2095109323Ssam			break;
2096109323Ssam		if (wreq.wi_type == WI_RID_FRAG_THRESH &&
2097109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
2098109323Ssam			break;
2099109323Ssam		/* FALLTHROUGH */
2100109323Ssam	case WI_RID_RTS_THRESH:
2101109323Ssam	case WI_RID_CNFAUTHMODE:
2102109323Ssam	case WI_RID_MAX_DATALEN:
2103109323Ssam		if (sc->sc_enabled) {
2104109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2105109323Ssam			    sizeof(u_int16_t));
2106109323Ssam			if (error)
2107109323Ssam				break;
2108109323Ssam		}
2109109323Ssam		switch (wreq.wi_type) {
2110109323Ssam		case WI_RID_FRAG_THRESH:
2111116951Ssam			ic->ic_fragthreshold = le16toh(wreq.wi_val[0]);
2112109323Ssam			break;
2113109323Ssam		case WI_RID_RTS_THRESH:
2114116951Ssam			ic->ic_rtsthreshold = le16toh(wreq.wi_val[0]);
2115109323Ssam			break;
2116109323Ssam		case WI_RID_MICROWAVE_OVEN:
2117109323Ssam			sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
2118109323Ssam			break;
2119109323Ssam		case WI_RID_ROAMING_MODE:
2120109323Ssam			sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
2121109323Ssam			break;
2122109323Ssam		case WI_RID_SYSTEM_SCALE:
2123109323Ssam			sc->sc_system_scale = le16toh(wreq.wi_val[0]);
2124109323Ssam			break;
2125109323Ssam		case WI_RID_CNFAUTHMODE:
2126109323Ssam			sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
2127109323Ssam			break;
2128109323Ssam		case WI_RID_MAX_DATALEN:
2129109323Ssam			sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
2130109323Ssam			break;
2131109323Ssam		}
2132109323Ssam		break;
213394405Simp
2134109323Ssam	case WI_RID_TX_RATE:
2135109323Ssam		switch (le16toh(wreq.wi_val[0])) {
2136109323Ssam		case 3:
2137109323Ssam			ic->ic_fixed_rate = -1;
2138109323Ssam			break;
2139109323Ssam		default:
2140116951Ssam			rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
2141116951Ssam			for (i = 0; i < rs->rs_nrates; i++) {
2142116951Ssam				if ((rs->rs_rates[i] & IEEE80211_RATE_VAL)
2143109323Ssam				    / 2 == le16toh(wreq.wi_val[0]))
2144109323Ssam					break;
2145109323Ssam			}
2146116951Ssam			if (i == rs->rs_nrates)
2147109323Ssam				return EINVAL;
2148109323Ssam			ic->ic_fixed_rate = i;
2149109323Ssam		}
2150109323Ssam		if (sc->sc_enabled)
2151109323Ssam			error = wi_write_txrate(sc);
2152109323Ssam		break;
215346492Swpaul
2154109323Ssam	case WI_RID_SCAN_APS:
2155109323Ssam		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2156116898Ssam			error = wi_scan_ap(sc, 0x3fff, 0x000f);
2157109323Ssam		break;
215846492Swpaul
2159116898Ssam	case WI_RID_SCAN_REQ:		/* compatibility interface */
2160116898Ssam		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2161116898Ssam			error = wi_scan_ap(sc, wreq.wi_val[0], wreq.wi_val[1]);
2162116898Ssam		break;
2163116898Ssam
2164109323Ssam	case WI_RID_MGMT_XMIT:
2165109323Ssam		if (!sc->sc_enabled) {
2166109323Ssam			error = ENETDOWN;
2167109323Ssam			break;
2168109323Ssam		}
2169109323Ssam		if (ic->ic_mgtq.ifq_len > 5) {
2170109323Ssam			error = EAGAIN;
2171109323Ssam			break;
2172109323Ssam		}
2173109323Ssam		/* XXX wi_len looks in u_int8_t, not in u_int16_t */
2174109323Ssam		m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL);
2175109323Ssam		if (m == NULL) {
2176109323Ssam			error = ENOMEM;
2177109323Ssam			break;
2178109323Ssam		}
2179109323Ssam		IF_ENQUEUE(&ic->ic_mgtq, m);
2180109323Ssam		break;
218146492Swpaul
2182109323Ssam	case WI_RID_MIF:
2183109323Ssam		mif = wreq.wi_val[0];
2184109323Ssam		val = wreq.wi_val[1];
2185109323Ssam		error = wi_cmd(sc, WI_CMD_WRITEMIF, mif, val, 0);
2186109323Ssam		break;
218746492Swpaul
2188109323Ssam	case WI_RID_PROCFRAME:		/* ignore for compatibility */
2189109323Ssam		break;
2190109323Ssam
2191116898Ssam	case WI_RID_OWN_SSID:
2192116898Ssam		if (le16toh(wreq.wi_val[0]) * 2 > len ||
2193116898Ssam		    le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) {
2194116898Ssam			error = ENOSPC;
2195116898Ssam			break;
2196116898Ssam		}
2197116898Ssam		memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
2198116898Ssam		ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2;
2199116898Ssam		memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen);
2200116898Ssam		error = ENETRESET;
2201116898Ssam		break;
2202116898Ssam
2203109323Ssam	default:
2204109323Ssam		if (sc->sc_enabled) {
2205109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2206109323Ssam			    len);
2207109323Ssam			if (error)
2208109323Ssam				break;
2209109323Ssam		}
2210109323Ssam		error = ieee80211_cfgset(ifp, cmd, data);
2211109323Ssam		break;
2212109323Ssam	}
2213109323Ssam	return error;
221446492Swpaul}
221546492Swpaul
2216109323Ssamstatic int
2217109323Ssamwi_write_txrate(struct wi_softc *sc)
221846492Swpaul{
2219109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2220109323Ssam	int i;
2221109323Ssam	u_int16_t rate;
222246492Swpaul
2223109323Ssam	if (ic->ic_fixed_rate < 0)
2224109323Ssam		rate = 0;	/* auto */
2225109323Ssam	else
2226116951Ssam		rate = (ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ic->ic_fixed_rate] &
2227109323Ssam		    IEEE80211_RATE_VAL) / 2;
222846492Swpaul
2229109323Ssam	/* rate: 0, 1, 2, 5, 11 */
223046492Swpaul
2231109323Ssam	switch (sc->sc_firmware_type) {
2232109323Ssam	case WI_LUCENT:
2233112501Simp		switch (rate) {
2234112501Simp		case 0:			/* auto == 11mbps auto */
2235112501Simp			rate = 3;
2236112501Simp			break;
2237112501Simp		/* case 1, 2 map to 1, 2*/
2238112501Simp		case 5:			/* 5.5Mbps -> 4 */
2239112501Simp			rate = 4;
2240112501Simp			break;
2241112501Simp		case 11:		/* 11mbps -> 5 */
2242112501Simp			rate = 5;
2243112501Simp			break;
2244112501Simp		default:
2245112501Simp			break;
2246112501Simp		}
2247109323Ssam		break;
2248109323Ssam	default:
2249109323Ssam		/* Choose a bit according to this table.
2250109323Ssam		 *
2251109323Ssam		 * bit | data rate
2252109323Ssam		 * ----+-------------------
2253109323Ssam		 * 0   | 1Mbps
2254109323Ssam		 * 1   | 2Mbps
2255109323Ssam		 * 2   | 5.5Mbps
2256109323Ssam		 * 3   | 11Mbps
2257109323Ssam		 */
2258109323Ssam		for (i = 8; i > 0; i >>= 1) {
2259109323Ssam			if (rate >= i)
2260109323Ssam				break;
2261109323Ssam		}
2262109323Ssam		if (i == 0)
2263109323Ssam			rate = 0xf;	/* auto */
2264109323Ssam		else
2265109323Ssam			rate = i;
2266109323Ssam		break;
2267109323Ssam	}
2268109323Ssam	return wi_write_val(sc, WI_RID_TX_RATE, rate);
2269109323Ssam}
227046492Swpaul
2271109323Ssamstatic int
2272109323Ssamwi_write_wep(struct wi_softc *sc)
2273109323Ssam{
2274109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2275109323Ssam	int error = 0;
2276109323Ssam	int i, keylen;
2277109323Ssam	u_int16_t val;
2278109323Ssam	struct wi_key wkey[IEEE80211_WEP_NKID];
227946492Swpaul
2280109323Ssam	switch (sc->sc_firmware_type) {
2281109323Ssam	case WI_LUCENT:
2282109323Ssam		val = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0;
2283109323Ssam		error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
2284109323Ssam		if (error)
2285109323Ssam			break;
2286109323Ssam		error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_wep_txkey);
2287109323Ssam		if (error)
2288109323Ssam			break;
2289109323Ssam		memset(wkey, 0, sizeof(wkey));
2290109323Ssam		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2291109323Ssam			keylen = ic->ic_nw_keys[i].wk_len;
2292109323Ssam			wkey[i].wi_keylen = htole16(keylen);
2293109323Ssam			memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
2294109323Ssam			    keylen);
2295109323Ssam		}
2296109323Ssam		error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
2297109323Ssam		    wkey, sizeof(wkey));
2298109323Ssam		break;
2299109323Ssam
2300109323Ssam	case WI_INTERSIL:
2301109323Ssam	case WI_SYMBOL:
2302109323Ssam		if (ic->ic_flags & IEEE80211_F_WEPON) {
2303109323Ssam			/*
2304109323Ssam			 * ONLY HWB3163 EVAL-CARD Firmware version
2305109323Ssam			 * less than 0.8 variant2
2306109323Ssam			 *
2307109323Ssam			 *   If promiscuous mode disable, Prism2 chip
2308109323Ssam			 *  does not work with WEP .
2309109323Ssam			 * It is under investigation for details.
2310109323Ssam			 * (ichiro@netbsd.org)
2311109323Ssam			 */
2312109323Ssam			if (sc->sc_firmware_type == WI_INTERSIL &&
2313109323Ssam			    sc->sc_sta_firmware_ver < 802 ) {
2314109323Ssam				/* firm ver < 0.8 variant 2 */
2315109323Ssam				wi_write_val(sc, WI_RID_PROMISC, 1);
2316109323Ssam			}
2317109323Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE,
2318109323Ssam			    sc->sc_cnfauthmode);
2319109323Ssam			val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
2320109323Ssam			/*
2321109323Ssam			 * Encryption firmware has a bug for HostAP mode.
2322109323Ssam			 */
2323109323Ssam			if (sc->sc_firmware_type == WI_INTERSIL &&
2324109323Ssam			    ic->ic_opmode == IEEE80211_M_HOSTAP)
2325109323Ssam				val |= HOST_ENCRYPT;
2326109323Ssam		} else {
2327109323Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE,
2328109323Ssam			    IEEE80211_AUTH_OPEN);
2329109323Ssam			val = HOST_ENCRYPT | HOST_DECRYPT;
2330109323Ssam		}
2331109323Ssam		error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
2332109323Ssam		if (error)
2333109323Ssam			break;
2334109323Ssam		error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
2335109323Ssam		    ic->ic_wep_txkey);
2336109323Ssam		if (error)
2337109323Ssam			break;
2338109323Ssam		/*
2339109323Ssam		 * It seems that the firmware accept 104bit key only if
2340109323Ssam		 * all the keys have 104bit length.  We get the length of
2341109323Ssam		 * the transmit key and use it for all other keys.
2342109323Ssam		 * Perhaps we should use software WEP for such situation.
2343109323Ssam		 */
2344109323Ssam		keylen = ic->ic_nw_keys[ic->ic_wep_txkey].wk_len;
2345109323Ssam		if (keylen > IEEE80211_WEP_KEYLEN)
2346109323Ssam			keylen = 13;	/* 104bit keys */
2347109323Ssam		else
2348109323Ssam			keylen = IEEE80211_WEP_KEYLEN;
2349109323Ssam		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2350109323Ssam			error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
2351109323Ssam			    ic->ic_nw_keys[i].wk_key, keylen);
2352109323Ssam			if (error)
2353109323Ssam				break;
2354109323Ssam		}
2355109323Ssam		break;
2356109323Ssam	}
2357109323Ssam	return error;
235846492Swpaul}
235946492Swpaul
2360109323Ssamstatic int
2361109323Ssamwi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
236246492Swpaul{
2363109323Ssam	int			i, s = 0;
2364109323Ssam	static volatile int count  = 0;
2365109323Ssam
2366109323Ssam	if (count > 0)
2367109323Ssam		panic("Hey partner, hold on there!");
2368109323Ssam	count++;
236953702Swpaul
2370109323Ssam	/* wait for the busy bit to clear */
2371109323Ssam	for (i = 500; i > 0; i--) {	/* 5s */
2372116206Simp		if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
2373109323Ssam			break;
2374109323Ssam		DELAY(10*1000);	/* 10 m sec */
2375109323Ssam	}
2376109323Ssam	if (i == 0) {
2377109323Ssam		device_printf(sc->sc_dev, "wi_cmd: busy bit won't clear.\n" );
2378109323Ssam		count--;
2379109323Ssam		return(ETIMEDOUT);
2380109323Ssam	}
238190580Sbrooks
2382109323Ssam	CSR_WRITE_2(sc, WI_PARAM0, val0);
2383109323Ssam	CSR_WRITE_2(sc, WI_PARAM1, val1);
2384109323Ssam	CSR_WRITE_2(sc, WI_PARAM2, val2);
2385109323Ssam	CSR_WRITE_2(sc, WI_COMMAND, cmd);
238690580Sbrooks
2387109323Ssam	if (cmd == WI_CMD_INI) {
2388109323Ssam		/* XXX: should sleep here. */
2389116206Simp		DELAY(100*1000);		/* 100ms delay for init */
2390109323Ssam	}
2391109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
2392109323Ssam		/*
2393109323Ssam		 * Wait for 'command complete' bit to be
2394109323Ssam		 * set in the event status register.
2395109323Ssam		 */
2396109323Ssam		s = CSR_READ_2(sc, WI_EVENT_STAT);
2397109323Ssam		if (s & WI_EV_CMD) {
2398109323Ssam			/* Ack the event and read result code. */
2399109323Ssam			s = CSR_READ_2(sc, WI_STATUS);
2400109323Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
2401109323Ssam			if (s & WI_STAT_CMD_RESULT) {
2402109323Ssam				count--;
2403109323Ssam				return(EIO);
2404109323Ssam			}
2405109323Ssam			break;
240690580Sbrooks		}
2407109323Ssam		DELAY(WI_DELAY);
2408109323Ssam	}
240990580Sbrooks
2410109323Ssam	count--;
2411109323Ssam	if (i == WI_TIMEOUT) {
2412109323Ssam		device_printf(sc->sc_dev,
2413109323Ssam		    "timeout in wi_cmd 0x%04x; event status 0x%04x\n", cmd, s);
2414109323Ssam		return(ETIMEDOUT);
241553702Swpaul	}
2416109323Ssam	return (0);
2417109323Ssam}
241853702Swpaul
2419109323Ssamstatic int
2420109323Ssamwi_seek_bap(struct wi_softc *sc, int id, int off)
2421109323Ssam{
2422109323Ssam	int i, status;
242390580Sbrooks
2424109323Ssam	CSR_WRITE_2(sc, WI_SEL0, id);
2425109323Ssam	CSR_WRITE_2(sc, WI_OFF0, off);
242690580Sbrooks
2427109323Ssam	for (i = 0; ; i++) {
2428109323Ssam		status = CSR_READ_2(sc, WI_OFF0);
2429109323Ssam		if ((status & WI_OFF_BUSY) == 0)
2430109323Ssam			break;
2431109323Ssam		if (i == WI_TIMEOUT) {
2432109323Ssam			device_printf(sc->sc_dev, "timeout in wi_seek to %x/%x\n",
2433109323Ssam			    id, off);
2434109323Ssam			sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2435109323Ssam			return ETIMEDOUT;
2436109323Ssam		}
2437109323Ssam		DELAY(1);
243853702Swpaul	}
2439109323Ssam	if (status & WI_OFF_ERR) {
2440109323Ssam		device_printf(sc->sc_dev, "failed in wi_seek to %x/%x\n", id, off);
2441109323Ssam		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2442109323Ssam		return EIO;
2443109323Ssam	}
2444109323Ssam	sc->sc_bap_id = id;
2445109323Ssam	sc->sc_bap_off = off;
2446109323Ssam	return 0;
244753702Swpaul}
244853702Swpaul
2449109323Ssamstatic int
2450109323Ssamwi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
245153702Swpaul{
2452109323Ssam	u_int16_t *ptr;
2453109323Ssam	int i, error, cnt;
245453702Swpaul
2455109323Ssam	if (buflen == 0)
2456109323Ssam		return 0;
2457109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2458109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
2459109323Ssam			return error;
246075219Salfred	}
2461109323Ssam	cnt = (buflen + 1) / 2;
2462109323Ssam	ptr = (u_int16_t *)buf;
2463109323Ssam	for (i = 0; i < cnt; i++)
2464109323Ssam		*ptr++ = CSR_READ_2(sc, WI_DATA0);
2465109323Ssam	sc->sc_bap_off += cnt * 2;
2466109323Ssam	return 0;
246753702Swpaul}
246853702Swpaul
2469109323Ssamstatic int
2470109323Ssamwi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
247153702Swpaul{
2472109323Ssam	u_int16_t *ptr;
2473109323Ssam	int i, error, cnt;
247446492Swpaul
2475109323Ssam	if (buflen == 0)
2476109323Ssam		return 0;
247746492Swpaul
2478109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
2479109323Ssam  again:
2480109323Ssam#endif
2481109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2482109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
2483109323Ssam			return error;
2484109323Ssam	}
2485109323Ssam	cnt = (buflen + 1) / 2;
2486109323Ssam	ptr = (u_int16_t *)buf;
2487109323Ssam	for (i = 0; i < cnt; i++)
2488109323Ssam		CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
2489109323Ssam	sc->sc_bap_off += cnt * 2;
2490109323Ssam
2491109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
2492109323Ssam	/*
2493109323Ssam	 * According to the comments in the HCF Light code, there is a bug
2494109323Ssam	 * in the Hermes (or possibly in certain Hermes firmware revisions)
2495109323Ssam	 * where the chip's internal autoincrement counter gets thrown off
2496109323Ssam	 * during data writes:  the autoincrement is missed, causing one
2497109323Ssam	 * data word to be overwritten and subsequent words to be written to
2498109323Ssam	 * the wrong memory locations. The end result is that we could end
2499109323Ssam	 * up transmitting bogus frames without realizing it. The workaround
2500109323Ssam	 * for this is to write a couple of extra guard words after the end
2501109323Ssam	 * of the transfer, then attempt to read then back. If we fail to
2502109323Ssam	 * locate the guard words where we expect them, we preform the
2503109323Ssam	 * transfer over again.
2504109323Ssam	 */
2505109323Ssam	if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
2506109323Ssam		CSR_WRITE_2(sc, WI_DATA0, 0x1234);
2507109323Ssam		CSR_WRITE_2(sc, WI_DATA0, 0x5678);
2508109323Ssam		wi_seek_bap(sc, id, sc->sc_bap_off);
2509109323Ssam		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2510109323Ssam		if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
2511109323Ssam		    CSR_READ_2(sc, WI_DATA0) != 0x5678) {
2512109323Ssam			device_printf(sc->sc_dev,
2513109323Ssam				"detect auto increment bug, try again\n");
2514109323Ssam			goto again;
2515109323Ssam		}
2516109323Ssam	}
2517109323Ssam#endif
2518109323Ssam	return 0;
251946492Swpaul}
252053702Swpaul
2521109323Ssamstatic int
2522109323Ssamwi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
2523109323Ssam{
2524109323Ssam	int error, len;
2525109323Ssam	struct mbuf *m;
252653702Swpaul
2527109323Ssam	for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
2528109323Ssam		if (m->m_len == 0)
2529109323Ssam			continue;
253053702Swpaul
2531109323Ssam		len = min(m->m_len, totlen);
253253702Swpaul
2533109323Ssam		if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
2534109323Ssam			m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
2535109323Ssam			return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
2536109323Ssam			    totlen);
2537109323Ssam		}
253853702Swpaul
2539109323Ssam		if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
2540109323Ssam			return error;
254153702Swpaul
2542109323Ssam		off += m->m_len;
2543109323Ssam		totlen -= len;
2544109323Ssam	}
2545109323Ssam	return 0;
2546109323Ssam}
254753702Swpaul
2548109323Ssamstatic int
2549109323Ssamwi_alloc_fid(struct wi_softc *sc, int len, int *idp)
255053702Swpaul{
255153702Swpaul	int i;
255253702Swpaul
2553109323Ssam	if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
2554109323Ssam		device_printf(sc->sc_dev, "failed to allocate %d bytes on NIC\n",
2555109323Ssam		    len);
2556109323Ssam		return ENOMEM;
255753702Swpaul	}
255853702Swpaul
2559109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
2560109323Ssam		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
2561109323Ssam			break;
2562109323Ssam		if (i == WI_TIMEOUT) {
2563109323Ssam			device_printf(sc->sc_dev, "timeout in alloc\n");
2564109323Ssam			return ETIMEDOUT;
2565109323Ssam		}
2566109323Ssam		DELAY(1);
256753702Swpaul	}
2568109323Ssam	*idp = CSR_READ_2(sc, WI_ALLOC_FID);
2569109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
2570109323Ssam	return 0;
2571109323Ssam}
257253702Swpaul
2573109323Ssamstatic int
2574109323Ssamwi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
2575109323Ssam{
2576109323Ssam	int error, len;
2577109323Ssam	u_int16_t ltbuf[2];
257853702Swpaul
2579109323Ssam	/* Tell the NIC to enter record read mode. */
2580109323Ssam	error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2581109323Ssam	if (error)
2582109323Ssam		return error;
258353702Swpaul
2584109323Ssam	error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2585109323Ssam	if (error)
2586109323Ssam		return error;
258753702Swpaul
2588109323Ssam	if (le16toh(ltbuf[1]) != rid) {
2589109323Ssam		device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n",
2590109323Ssam		    rid, le16toh(ltbuf[1]));
2591109323Ssam		return EIO;
259253702Swpaul	}
2593109323Ssam	len = (le16toh(ltbuf[0]) - 1) * 2;	 /* already got rid */
2594109323Ssam	if (*buflenp < len) {
2595109323Ssam		device_printf(sc->sc_dev, "record buffer is too small, "
2596109323Ssam		    "rid=%x, size=%d, len=%d\n",
2597109323Ssam		    rid, *buflenp, len);
2598109323Ssam		return ENOSPC;
259953702Swpaul	}
2600109323Ssam	*buflenp = len;
2601109323Ssam	return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2602109323Ssam}
260353702Swpaul
2604109323Ssamstatic int
2605109323Ssamwi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2606109323Ssam{
2607109323Ssam	int error;
2608109323Ssam	u_int16_t ltbuf[2];
260953702Swpaul
2610109323Ssam	ltbuf[0] = htole16((buflen + 1) / 2 + 1);	 /* includes rid */
2611109323Ssam	ltbuf[1] = htole16(rid);
261253702Swpaul
2613109323Ssam	error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2614109323Ssam	if (error)
2615109323Ssam		return error;
2616109323Ssam	error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2617109323Ssam	if (error)
2618109323Ssam		return error;
2619102206Simp
2620109323Ssam	return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
262153702Swpaul}
262277217Sphk
262388546Salfredstatic int
2624117812Ssamwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
262577217Sphk{
2626117812Ssam	struct ifnet *ifp = &ic->ic_if;
2627117812Ssam	struct wi_softc *sc = ifp->if_softc;
2628116951Ssam	struct ieee80211_node *ni = ic->ic_bss;
2629116951Ssam	int buflen;
2630109323Ssam	u_int16_t val;
2631109323Ssam	struct wi_ssid ssid;
2632109323Ssam	u_int8_t old_bssid[IEEE80211_ADDR_LEN];
263377217Sphk
2634117812Ssam	DPRINTF(("%s: %s -> %s\n", __func__,
2635117812Ssam		ieee80211_state_name[ic->ic_state],
2636117812Ssam		ieee80211_state_name[nstate]));
2637109323Ssam
2638109323Ssam	switch (nstate) {
2639109323Ssam	case IEEE80211_S_INIT:
2640109323Ssam		ic->ic_flags &= ~IEEE80211_F_SIBSS;
2641109323Ssam		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2642117812Ssam		return (*sc->sc_newstate)(ic, nstate, arg);
2643109323Ssam
2644109323Ssam	case IEEE80211_S_RUN:
2645109323Ssam		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2646109323Ssam		buflen = IEEE80211_ADDR_LEN;
2647109323Ssam		wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen);
2648109323Ssam		IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
2649109323Ssam		buflen = sizeof(val);
2650109323Ssam		wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
2651116951Ssam		/* XXX validate channel */
2652116951Ssam		ni->ni_chan = &ic->ic_channels[le16toh(val)];
2653119784Ssam#if NBPFILTER > 0
2654119784Ssam		sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
2655119784Ssam			htole16(ni->ni_chan->ic_freq);
2656119784Ssam		sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
2657119784Ssam			htole16(ni->ni_chan->ic_flags);
2658119784Ssam#endif
2659109323Ssam
2660109323Ssam		if (IEEE80211_ADDR_EQ(old_bssid, ni->ni_bssid))
2661109323Ssam			sc->sc_false_syns++;
2662109323Ssam		else
2663109323Ssam			sc->sc_false_syns = 0;
2664109323Ssam
2665109323Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2666109323Ssam			ni->ni_esslen = ic->ic_des_esslen;
2667109323Ssam			memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
2668116951Ssam			ni->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B];
2669109323Ssam			ni->ni_intval = ic->ic_lintval;
2670109323Ssam			ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
2671109323Ssam			if (ic->ic_flags & IEEE80211_F_WEPON)
2672109323Ssam				ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
267377217Sphk		} else {
2674109323Ssam			/* XXX check return value */
2675109323Ssam			buflen = sizeof(ssid);
2676109323Ssam			wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
2677109323Ssam			ni->ni_esslen = le16toh(ssid.wi_len);
2678109323Ssam			if (ni->ni_esslen > IEEE80211_NWID_LEN)
2679109323Ssam				ni->ni_esslen = IEEE80211_NWID_LEN;	/*XXX*/
2680109323Ssam			memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
268177217Sphk		}
268277217Sphk		break;
2683109323Ssam
2684109323Ssam	case IEEE80211_S_SCAN:
2685109323Ssam	case IEEE80211_S_AUTH:
2686109323Ssam	case IEEE80211_S_ASSOC:
268777217Sphk		break;
268877217Sphk	}
268977217Sphk
2690117812Ssam	ic->ic_state = nstate;		/* NB: skip normal ieee80211 handling */
2691117812Ssam	return 0;
269277217Sphk}
269377217Sphk
269488546Salfredstatic int
2695116898Ssamwi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate)
269677217Sphk{
2697109323Ssam	int error = 0;
2698109323Ssam	u_int16_t val[2];
269977217Sphk
2700109323Ssam	if (!sc->sc_enabled)
2701109323Ssam		return ENXIO;
2702109323Ssam	switch (sc->sc_firmware_type) {
2703109323Ssam	case WI_LUCENT:
2704109323Ssam		(void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
270598440Simp		break;
2706109323Ssam	case WI_INTERSIL:
2707116898Ssam		val[0] = chanmask;	/* channel */
2708116898Ssam		val[1] = txrate;	/* tx rate */
2709109323Ssam		error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
271098440Simp		break;
2711109323Ssam	case WI_SYMBOL:
2712109323Ssam		/*
2713109323Ssam		 * XXX only supported on 3.x ?
2714109323Ssam		 */
2715109323Ssam		val[0] = BSCAN_BCAST | BSCAN_ONETIME;
2716109323Ssam		error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
2717109323Ssam		    val, sizeof(val[0]));
271898440Simp		break;
271998440Simp	}
2720109323Ssam	if (error == 0) {
2721109323Ssam		sc->sc_scan_timer = WI_SCAN_WAIT;
2722109323Ssam		sc->sc_ic.ic_if.if_timer = 1;
2723116898Ssam		DPRINTF(("wi_scan_ap: start scanning, "
2724116898Ssam			"chamask 0x%x txrate 0x%x\n", chanmask, txrate));
2725109323Ssam	}
2726109323Ssam	return error;
2727109323Ssam}
272877217Sphk
2729109323Ssamstatic void
2730109323Ssamwi_scan_result(struct wi_softc *sc, int fid, int cnt)
2731109323Ssam{
2732109323Ssam#define	N(a)	(sizeof (a) / sizeof (a[0]))
2733109323Ssam	int i, naps, off, szbuf;
2734109323Ssam	struct wi_scan_header ws_hdr;	/* Prism2 header */
2735109323Ssam	struct wi_scan_data_p2 ws_dat;	/* Prism2 scantable*/
2736109323Ssam	struct wi_apinfo *ap;
2737109323Ssam
2738109323Ssam	off = sizeof(u_int16_t) * 2;
2739109323Ssam	memset(&ws_hdr, 0, sizeof(ws_hdr));
2740109323Ssam	switch (sc->sc_firmware_type) {
2741109323Ssam	case WI_INTERSIL:
2742109323Ssam		wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
2743109323Ssam		off += sizeof(ws_hdr);
2744109323Ssam		szbuf = sizeof(struct wi_scan_data_p2);
274577217Sphk		break;
2746109323Ssam	case WI_SYMBOL:
2747109323Ssam		szbuf = sizeof(struct wi_scan_data_p2) + 6;
274877217Sphk		break;
2749109323Ssam	case WI_LUCENT:
2750109323Ssam		szbuf = sizeof(struct wi_scan_data);
275177217Sphk		break;
2752109323Ssam	default:
2753109323Ssam		device_printf(sc->sc_dev,
2754109323Ssam			"wi_scan_result: unknown firmware type %u\n",
2755109323Ssam			sc->sc_firmware_type);
2756109323Ssam		naps = 0;
2757109323Ssam		goto done;
275877217Sphk	}
2759109323Ssam	naps = (cnt * 2 + 2 - off) / szbuf;
2760109323Ssam	if (naps > N(sc->sc_aps))
2761109323Ssam		naps = N(sc->sc_aps);
2762109323Ssam	sc->sc_naps = naps;
2763109323Ssam	/* Read Data */
2764109323Ssam	ap = sc->sc_aps;
2765109323Ssam	memset(&ws_dat, 0, sizeof(ws_dat));
2766109323Ssam	for (i = 0; i < naps; i++, ap++) {
2767109323Ssam		wi_read_bap(sc, fid, off, &ws_dat,
2768109323Ssam		    (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
2769109323Ssam		DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off,
2770109323Ssam		    ether_sprintf(ws_dat.wi_bssid)));
2771109323Ssam		off += szbuf;
2772109323Ssam		ap->scanreason = le16toh(ws_hdr.wi_reason);
2773109323Ssam		memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
2774109323Ssam		ap->channel = le16toh(ws_dat.wi_chid);
2775109323Ssam		ap->signal  = le16toh(ws_dat.wi_signal);
2776109323Ssam		ap->noise   = le16toh(ws_dat.wi_noise);
2777109323Ssam		ap->quality = ap->signal - ap->noise;
2778109323Ssam		ap->capinfo = le16toh(ws_dat.wi_capinfo);
2779109323Ssam		ap->interval = le16toh(ws_dat.wi_interval);
2780109323Ssam		ap->rate    = le16toh(ws_dat.wi_rate);
2781109323Ssam		ap->namelen = le16toh(ws_dat.wi_namelen);
2782109323Ssam		if (ap->namelen > sizeof(ap->name))
2783109323Ssam			ap->namelen = sizeof(ap->name);
2784109323Ssam		memcpy(ap->name, ws_dat.wi_name, ap->namelen);
2785109323Ssam	}
2786109323Ssamdone:
2787109323Ssam	/* Done scanning */
2788109323Ssam	sc->sc_scan_timer = 0;
2789109323Ssam	DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
2790109323Ssam#undef N
279177217Sphk}
279277217Sphk
279388546Salfredstatic void
2794109323Ssamwi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi)
279577217Sphk{
2796109323Ssam	ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr),
2797116951Ssam	    ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL : -1, rssi);
2798109323Ssam	printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n",
2799109323Ssam		le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1),
2800109323Ssam		le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence);
2801109323Ssam	printf(" rx_signal %u rx_rate %u rx_flow %u\n",
2802109323Ssam		wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow);
2803109323Ssam	printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n",
2804109323Ssam		wh->wi_tx_rtry, wh->wi_tx_rate,
2805109323Ssam		le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len));
2806109323Ssam	printf(" ehdr dst %6D src %6D type 0x%x\n",
2807109323Ssam		wh->wi_ehdr.ether_dhost, ":", wh->wi_ehdr.ether_shost, ":",
2808109323Ssam		wh->wi_ehdr.ether_type);
2809109323Ssam}
281077217Sphk
2811109323Ssamint
2812109323Ssamwi_alloc(device_t dev, int rid)
2813109323Ssam{
2814109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2815109323Ssam
2816109323Ssam	if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2817109323Ssam		sc->iobase_rid = rid;
2818109323Ssam		sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2819109323Ssam		    &sc->iobase_rid, 0, ~0, (1 << 6),
2820109323Ssam		    rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2821109323Ssam		if (!sc->iobase) {
2822109323Ssam			device_printf(dev, "No I/O space?!\n");
2823109323Ssam			return (ENXIO);
282498440Simp		}
2825109323Ssam
2826109323Ssam		sc->wi_io_addr = rman_get_start(sc->iobase);
2827109323Ssam		sc->wi_btag = rman_get_bustag(sc->iobase);
2828109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2829109323Ssam	} else {
2830109323Ssam		sc->mem_rid = rid;
2831109323Ssam		sc->mem = bus_alloc_resource(dev, SYS_RES_MEMORY,
2832109323Ssam		    &sc->mem_rid, 0, ~0, 1, RF_ACTIVE);
2833109323Ssam
2834109323Ssam		if (!sc->mem) {
2835109323Ssam			device_printf(dev, "No Mem space on prism2.5?\n");
2836109323Ssam			return (ENXIO);
283777217Sphk		}
2838109323Ssam
2839109323Ssam		sc->wi_btag = rman_get_bustag(sc->mem);
2840109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->mem);
284177217Sphk	}
284277217Sphk
2843109323Ssam
2844109323Ssam	sc->irq_rid = 0;
2845109323Ssam	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
2846109323Ssam	    0, ~0, 1, RF_ACTIVE |
2847109323Ssam	    ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2848109323Ssam
2849109323Ssam	if (!sc->irq) {
2850109323Ssam		wi_free(dev);
2851109323Ssam		device_printf(dev, "No irq?!\n");
2852109323Ssam		return (ENXIO);
285377217Sphk	}
2854109323Ssam
2855109323Ssam	sc->sc_dev = dev;
2856109323Ssam	sc->sc_unit = device_get_unit(dev);
2857109323Ssam
2858109323Ssam	return (0);
285977217Sphk}
286093359Simp
2861109323Ssamvoid
2862109323Ssamwi_free(device_t dev)
2863109323Ssam{
2864109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2865109323Ssam
2866109323Ssam	if (sc->iobase != NULL) {
2867109323Ssam		bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2868109323Ssam		sc->iobase = NULL;
2869109323Ssam	}
2870109323Ssam	if (sc->irq != NULL) {
2871109323Ssam		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2872109323Ssam		sc->irq = NULL;
2873109323Ssam	}
2874109323Ssam	if (sc->mem != NULL) {
2875109323Ssam		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
2876109323Ssam		sc->mem = NULL;
2877109323Ssam	}
2878109323Ssam
2879109323Ssam	return;
2880109323Ssam}
2881109323Ssam
288293359Simpstatic int
2883109323Ssamwi_get_debug(struct wi_softc *sc, struct wi_req *wreq)
288493359Simp{
2885109323Ssam	int error = 0;
288693359Simp
288793359Simp	wreq->wi_len = 1;
288893359Simp
288993359Simp	switch (wreq->wi_type) {
289093359Simp	case WI_DEBUG_SLEEP:
289193359Simp		wreq->wi_len++;
289293359Simp		wreq->wi_val[0] = sc->wi_debug.wi_sleep;
289393359Simp		break;
289493359Simp	case WI_DEBUG_DELAYSUPP:
289593359Simp		wreq->wi_len++;
289693359Simp		wreq->wi_val[0] = sc->wi_debug.wi_delaysupp;
289793359Simp		break;
289893359Simp	case WI_DEBUG_TXSUPP:
289993359Simp		wreq->wi_len++;
290093359Simp		wreq->wi_val[0] = sc->wi_debug.wi_txsupp;
290193359Simp		break;
290293359Simp	case WI_DEBUG_MONITOR:
290393359Simp		wreq->wi_len++;
290493359Simp		wreq->wi_val[0] = sc->wi_debug.wi_monitor;
290593359Simp		break;
290693359Simp	case WI_DEBUG_LEDTEST:
290793359Simp		wreq->wi_len += 3;
290893359Simp		wreq->wi_val[0] = sc->wi_debug.wi_ledtest;
290993359Simp		wreq->wi_val[1] = sc->wi_debug.wi_ledtest_param0;
291093359Simp		wreq->wi_val[2] = sc->wi_debug.wi_ledtest_param1;
291193359Simp		break;
291293359Simp	case WI_DEBUG_CONTTX:
291393359Simp		wreq->wi_len += 2;
291493359Simp		wreq->wi_val[0] = sc->wi_debug.wi_conttx;
291593359Simp		wreq->wi_val[1] = sc->wi_debug.wi_conttx_param0;
291693359Simp		break;
291793359Simp	case WI_DEBUG_CONTRX:
291893359Simp		wreq->wi_len++;
291993359Simp		wreq->wi_val[0] = sc->wi_debug.wi_contrx;
292093359Simp		break;
292193359Simp	case WI_DEBUG_SIGSTATE:
292293359Simp		wreq->wi_len += 2;
292393359Simp		wreq->wi_val[0] = sc->wi_debug.wi_sigstate;
292493359Simp		wreq->wi_val[1] = sc->wi_debug.wi_sigstate_param0;
292593359Simp		break;
292693359Simp	case WI_DEBUG_CONFBITS:
292793359Simp		wreq->wi_len += 2;
292893359Simp		wreq->wi_val[0] = sc->wi_debug.wi_confbits;
292993359Simp		wreq->wi_val[1] = sc->wi_debug.wi_confbits_param0;
293093359Simp		break;
293193359Simp	default:
293293359Simp		error = EIO;
293393359Simp		break;
293493359Simp	}
293593359Simp
293693359Simp	return (error);
293793359Simp}
293893359Simp
293993359Simpstatic int
2940109323Ssamwi_set_debug(struct wi_softc *sc, struct wi_req *wreq)
294193359Simp{
2942109323Ssam	int error = 0;
294393359Simp	u_int16_t		cmd, param0 = 0, param1 = 0;
294493359Simp
294593359Simp	switch (wreq->wi_type) {
294693359Simp	case WI_DEBUG_RESET:
294793359Simp	case WI_DEBUG_INIT:
294893359Simp	case WI_DEBUG_CALENABLE:
294993359Simp		break;
295093359Simp	case WI_DEBUG_SLEEP:
295193359Simp		sc->wi_debug.wi_sleep = 1;
295293359Simp		break;
295393359Simp	case WI_DEBUG_WAKE:
295493359Simp		sc->wi_debug.wi_sleep = 0;
295593359Simp		break;
295693359Simp	case WI_DEBUG_CHAN:
295793359Simp		param0 = wreq->wi_val[0];
295893359Simp		break;
295993359Simp	case WI_DEBUG_DELAYSUPP:
296093359Simp		sc->wi_debug.wi_delaysupp = 1;
296193359Simp		break;
296293359Simp	case WI_DEBUG_TXSUPP:
296393359Simp		sc->wi_debug.wi_txsupp = 1;
296493359Simp		break;
296593359Simp	case WI_DEBUG_MONITOR:
296693359Simp		sc->wi_debug.wi_monitor = 1;
296793359Simp		break;
296893359Simp	case WI_DEBUG_LEDTEST:
296993359Simp		param0 = wreq->wi_val[0];
297093359Simp		param1 = wreq->wi_val[1];
297193359Simp		sc->wi_debug.wi_ledtest = 1;
297293359Simp		sc->wi_debug.wi_ledtest_param0 = param0;
297393359Simp		sc->wi_debug.wi_ledtest_param1 = param1;
297493359Simp		break;
297593359Simp	case WI_DEBUG_CONTTX:
297693359Simp		param0 = wreq->wi_val[0];
297793359Simp		sc->wi_debug.wi_conttx = 1;
297893359Simp		sc->wi_debug.wi_conttx_param0 = param0;
297993359Simp		break;
298093359Simp	case WI_DEBUG_STOPTEST:
298193359Simp		sc->wi_debug.wi_delaysupp = 0;
298293359Simp		sc->wi_debug.wi_txsupp = 0;
298393359Simp		sc->wi_debug.wi_monitor = 0;
298493359Simp		sc->wi_debug.wi_ledtest = 0;
298593359Simp		sc->wi_debug.wi_ledtest_param0 = 0;
298693359Simp		sc->wi_debug.wi_ledtest_param1 = 0;
298793359Simp		sc->wi_debug.wi_conttx = 0;
298893359Simp		sc->wi_debug.wi_conttx_param0 = 0;
298993359Simp		sc->wi_debug.wi_contrx = 0;
299093359Simp		sc->wi_debug.wi_sigstate = 0;
299193359Simp		sc->wi_debug.wi_sigstate_param0 = 0;
299293359Simp		break;
299393359Simp	case WI_DEBUG_CONTRX:
299493359Simp		sc->wi_debug.wi_contrx = 1;
299593359Simp		break;
299693359Simp	case WI_DEBUG_SIGSTATE:
299793359Simp		param0 = wreq->wi_val[0];
299893359Simp		sc->wi_debug.wi_sigstate = 1;
299993359Simp		sc->wi_debug.wi_sigstate_param0 = param0;
300093359Simp		break;
300193359Simp	case WI_DEBUG_CONFBITS:
300293359Simp		param0 = wreq->wi_val[0];
300393359Simp		param1 = wreq->wi_val[1];
300493359Simp		sc->wi_debug.wi_confbits = param0;
300593359Simp		sc->wi_debug.wi_confbits_param0 = param1;
300693359Simp		break;
300793359Simp	default:
300893359Simp		error = EIO;
300993359Simp		break;
301093359Simp	}
301193359Simp
301293359Simp	if (error)
301393359Simp		return (error);
301493359Simp
301593359Simp	cmd = WI_CMD_DEBUG | (wreq->wi_type << 8);
301693359Simp	error = wi_cmd(sc, cmd, param0, param1, 0);
301793359Simp
301893359Simp	return (error);
301993359Simp}
3020101903Simp
3021105076Simp#if __FreeBSD_version >= 500000
3022101903Simp/*
3023101903Simp * Special routines to download firmware for Symbol CF card.
3024101903Simp * XXX: This should be modified generic into any PRISM-2 based card.
3025101903Simp */
3026101903Simp
3027101903Simp#define	WI_SBCF_PDIADDR		0x3100
3028101903Simp
3029101903Simp/* unaligned load little endian */
3030101903Simp#define	GETLE32(p)	((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24))
3031101903Simp#define	GETLE16(p)	((p)[0] | ((p)[1]<<8))
3032101903Simp
3033101903Simpint
3034101903Simpwi_symbol_load_firm(struct wi_softc *sc, const void *primsym, int primlen,
3035101903Simp    const void *secsym, int seclen)
3036101903Simp{
3037101903Simp	uint8_t ebuf[256];
3038101903Simp	int i;
3039101903Simp
3040101903Simp	/* load primary code and run it */
3041101903Simp	wi_symbol_set_hcr(sc, WI_HCR_EEHOLD);
3042101903Simp	if (wi_symbol_write_firm(sc, primsym, primlen, NULL, 0))
3043101903Simp		return EIO;
3044101903Simp	wi_symbol_set_hcr(sc, WI_HCR_RUN);
3045101903Simp	for (i = 0; ; i++) {
3046101903Simp		if (i == 10)
3047101903Simp			return ETIMEDOUT;
3048101903Simp		tsleep(sc, PWAIT, "wiinit", 1);
3049101903Simp		if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT)
3050101903Simp			break;
3051101903Simp		/* write the magic key value to unlock aux port */
3052101903Simp		CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0);
3053101903Simp		CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1);
3054101903Simp		CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2);
3055101903Simp		CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL);
3056101903Simp	}
3057101903Simp
3058101903Simp	/* issue read EEPROM command: XXX copied from wi_cmd() */
3059101903Simp	CSR_WRITE_2(sc, WI_PARAM0, 0);
3060101903Simp	CSR_WRITE_2(sc, WI_PARAM1, 0);
3061101903Simp	CSR_WRITE_2(sc, WI_PARAM2, 0);
3062101903Simp	CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE);
3063101903Simp        for (i = 0; i < WI_TIMEOUT; i++) {
3064101903Simp                if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
3065101903Simp                        break;
3066101903Simp                DELAY(1);
3067101903Simp        }
3068101903Simp        CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
3069101903Simp
3070101903Simp	CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ);
3071101903Simp	CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ);
3072101903Simp	CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA,
3073101903Simp	    (uint16_t *)ebuf, sizeof(ebuf) / 2);
3074101903Simp	if (GETLE16(ebuf) > sizeof(ebuf))
3075101903Simp		return EIO;
3076101903Simp	if (wi_symbol_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf)))
3077101903Simp		return EIO;
3078101903Simp	return 0;
3079101903Simp}
3080101903Simp
3081101903Simpstatic int
3082101903Simpwi_symbol_write_firm(struct wi_softc *sc, const void *buf, int buflen,
3083101903Simp    const void *ebuf, int ebuflen)
3084101903Simp{
3085101903Simp	const uint8_t *p, *ep, *q, *eq;
3086101903Simp	char *tp;
3087101903Simp	uint32_t addr, id, eid;
3088101903Simp	int i, len, elen, nblk, pdrlen;
3089101903Simp
3090101903Simp	/*
3091101903Simp	 * Parse the header of the firmware image.
3092101903Simp	 */
3093101903Simp	p = buf;
3094101903Simp	ep = p + buflen;
3095101903Simp	while (p < ep && *p++ != ' ');	/* FILE: */
3096101903Simp	while (p < ep && *p++ != ' ');	/* filename */
3097101903Simp	while (p < ep && *p++ != ' ');	/* type of the firmware */
3098101903Simp	nblk = strtoul(p, &tp, 10);
3099101903Simp	p = tp;
3100101903Simp	pdrlen = strtoul(p + 1, &tp, 10);
3101101903Simp	p = tp;
3102101903Simp	while (p < ep && *p++ != 0x1a);	/* skip rest of header */
3103101903Simp
3104101903Simp	/*
3105101903Simp	 * Block records: address[4], length[2], data[length];
3106101903Simp	 */
3107101903Simp	for (i = 0; i < nblk; i++) {
3108101903Simp		addr = GETLE32(p);	p += 4;
3109101903Simp		len  = GETLE16(p);	p += 2;
3110101903Simp		CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3111101903Simp		CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3112101903Simp		CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3113101903Simp		    (const uint16_t *)p, len / 2);
3114101903Simp		p += len;
3115101903Simp	}
3116101903Simp
3117101903Simp	/*
3118101903Simp	 * PDR: id[4], address[4], length[4];
3119101903Simp	 */
3120101903Simp	for (i = 0; i < pdrlen; ) {
3121101903Simp		id   = GETLE32(p);	p += 4; i += 4;
3122101903Simp		addr = GETLE32(p);	p += 4; i += 4;
3123101903Simp		len  = GETLE32(p);	p += 4; i += 4;
3124101903Simp		/* replace PDR entry with the values from EEPROM, if any */
3125101903Simp		for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) {
3126101903Simp			elen = GETLE16(q);	q += 2;
3127101903Simp			eid  = GETLE16(q);	q += 2;
3128101903Simp			elen--;		/* elen includes eid */
3129101903Simp			if (eid == 0)
3130101903Simp				break;
3131101903Simp			if (eid != id)
3132101903Simp				continue;
3133101903Simp			CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3134101903Simp			CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3135101903Simp			CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3136101903Simp			    (const uint16_t *)q, len / 2);
3137101903Simp			break;
3138101903Simp		}
3139101903Simp	}
3140101903Simp	return 0;
3141101903Simp}
3142101903Simp
3143101903Simpstatic int
3144101903Simpwi_symbol_set_hcr(struct wi_softc *sc, int mode)
3145101903Simp{
3146101903Simp	uint16_t hcr;
3147101903Simp
3148101903Simp	CSR_WRITE_2(sc, WI_COR, WI_COR_RESET);
3149101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3150101903Simp	hcr = CSR_READ_2(sc, WI_HCR);
3151101903Simp	hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE);
3152101903Simp	CSR_WRITE_2(sc, WI_HCR, hcr);
3153101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3154101903Simp	CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE);
3155101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3156101903Simp	return 0;
3157101903Simp}
3158105076Simp#endif
3159