if_wi.c revision 139542
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 139542 2005-01-01 17:45:11Z sam $");
66113038Sobrien
67109323Ssam#define WI_HERMES_AUTOINC_WAR	/* Work around data write autoinc bug. */
68109323Ssam#define WI_HERMES_STATS_WAR	/* Work around stats counter bug. */
69109323Ssam
70109323Ssam#define	NBPFILTER	1
71109323Ssam
7246492Swpaul#include <sys/param.h>
7346492Swpaul#include <sys/systm.h>
7495706Simp#if __FreeBSD_version >= 500033
7595533Smike#include <sys/endian.h>
7695706Simp#endif
7746492Swpaul#include <sys/sockio.h>
7846492Swpaul#include <sys/mbuf.h>
7983366Sjulian#include <sys/proc.h>
8046492Swpaul#include <sys/kernel.h>
8146492Swpaul#include <sys/socket.h>
8253702Swpaul#include <sys/module.h>
8353702Swpaul#include <sys/bus.h>
8494486Simp#include <sys/random.h>
8553702Swpaul#include <sys/syslog.h>
8653702Swpaul#include <sys/sysctl.h>
8746492Swpaul
8853702Swpaul#include <machine/bus.h>
8953702Swpaul#include <machine/resource.h>
9095534Simp#include <machine/clock.h>
91116951Ssam#include <machine/atomic.h>
9253702Swpaul#include <sys/rman.h>
9353702Swpaul
9446492Swpaul#include <net/if.h>
9546492Swpaul#include <net/if_arp.h>
9646492Swpaul#include <net/ethernet.h>
9746492Swpaul#include <net/if_dl.h>
9846492Swpaul#include <net/if_media.h>
9946492Swpaul#include <net/if_types.h>
10046492Swpaul
101116951Ssam#include <net80211/ieee80211_var.h>
102116951Ssam#include <net80211/ieee80211_ioctl.h>
103119784Ssam#include <net80211/ieee80211_radiotap.h>
104116951Ssam
10546492Swpaul#include <netinet/in.h>
10646492Swpaul#include <netinet/in_systm.h>
10746492Swpaul#include <netinet/in_var.h>
10846492Swpaul#include <netinet/ip.h>
10946492Swpaul#include <netinet/if_ether.h>
11046492Swpaul
11146492Swpaul#include <net/bpf.h>
11246492Swpaul
11370808Speter#include <dev/wi/if_wavelan_ieee.h>
114119784Ssam#include <dev/wi/if_wireg.h>
11593611Simp#include <dev/wi/if_wivar.h>
11646492Swpaul
11791693Simpstatic void wi_start(struct ifnet *);
118109323Ssamstatic int  wi_reset(struct wi_softc *);
11991693Simpstatic void wi_watchdog(struct ifnet *);
120109323Ssamstatic int  wi_ioctl(struct ifnet *, u_long, caddr_t);
121109323Ssamstatic int  wi_media_change(struct ifnet *);
122109323Ssamstatic void wi_media_status(struct ifnet *, struct ifmediareq *);
12346492Swpaul
124109323Ssamstatic void wi_rx_intr(struct wi_softc *);
125109323Ssamstatic void wi_tx_intr(struct wi_softc *);
126109323Ssamstatic void wi_tx_ex_intr(struct wi_softc *);
127109323Ssamstatic void wi_info_intr(struct wi_softc *);
12846492Swpaul
129109323Ssamstatic int  wi_get_cfg(struct ifnet *, u_long, caddr_t);
130109323Ssamstatic int  wi_set_cfg(struct ifnet *, u_long, caddr_t);
131109323Ssamstatic int  wi_write_txrate(struct wi_softc *);
132109323Ssamstatic int  wi_write_wep(struct wi_softc *);
133109323Ssamstatic int  wi_write_multi(struct wi_softc *);
134109323Ssamstatic int  wi_alloc_fid(struct wi_softc *, int, int *);
135109323Ssamstatic void wi_read_nicid(struct wi_softc *);
136109323Ssamstatic int  wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
13753702Swpaul
138109323Ssamstatic int  wi_cmd(struct wi_softc *, int, int, int, int);
139109323Ssamstatic int  wi_seek_bap(struct wi_softc *, int, int);
140109323Ssamstatic int  wi_read_bap(struct wi_softc *, int, int, void *, int);
141109323Ssamstatic int  wi_write_bap(struct wi_softc *, int, int, void *, int);
142109323Ssamstatic int  wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
143109323Ssamstatic int  wi_read_rid(struct wi_softc *, int, void *, int *);
144109323Ssamstatic int  wi_write_rid(struct wi_softc *, int, void *, int);
14577217Sphk
146117812Ssamstatic int  wi_newstate(struct ieee80211com *, enum ieee80211_state, int);
147109323Ssam
148116898Ssamstatic int  wi_scan_ap(struct wi_softc *, u_int16_t, u_int16_t);
149109323Ssamstatic void wi_scan_result(struct wi_softc *, int, int);
150109323Ssam
151109323Ssamstatic void wi_dump_pkt(struct wi_frame *, struct ieee80211_node *, int rssi);
152109323Ssam
15393359Simpstatic int wi_get_debug(struct wi_softc *, struct wi_req *);
15493359Simpstatic int wi_set_debug(struct wi_softc *, struct wi_req *);
15593359Simp
156105076Simp#if __FreeBSD_version >= 500000
157101903Simp/* support to download firmware for symbol CF card */
158101903Simpstatic int wi_symbol_write_firm(struct wi_softc *, const void *, int,
159101903Simp		const void *, int);
160101903Simpstatic int wi_symbol_set_hcr(struct wi_softc *, int);
161105076Simp#endif
162101903Simp
163109323Ssamstatic __inline int
164109323Ssamwi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
165109323Ssam{
16653702Swpaul
167109323Ssam	val = htole16(val);
168109323Ssam	return wi_write_rid(sc, rid, &val, sizeof(val));
169109323Ssam}
170109323Ssam
171109592SsamSYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters");
172109592Ssam
173109323Ssamstatic	struct timeval lasttxerror;	/* time of last tx error msg */
174109323Ssamstatic	int curtxeps;			/* current tx error msgs/sec */
175111559Ssamstatic	int wi_txerate = 0;		/* tx error rate: max msgs/sec */
176109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate,
177111559Ssam	    0, "max tx error msgs/sec; 0 to disable msgs");
178109323Ssam
179109323Ssam#define	WI_DEBUG
180109323Ssam#ifdef WI_DEBUG
181109323Ssamstatic	int wi_debug = 0;
182109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug,
183109592Ssam	    0, "control debugging printfs");
184109323Ssam
185109323Ssam#define	DPRINTF(X)	if (wi_debug) printf X
186109323Ssam#define	DPRINTF2(X)	if (wi_debug > 1) printf X
187109323Ssam#define	IFF_DUMPPKTS(_ifp) \
188109323Ssam	(((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
189109323Ssam#else
190109323Ssam#define	DPRINTF(X)
191109323Ssam#define	DPRINTF2(X)
192109323Ssam#define	IFF_DUMPPKTS(_ifp)	0
193109323Ssam#endif
194109323Ssam
195109323Ssam#define WI_INTRS	(WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
196109323Ssam
19793825Simpstruct wi_card_ident wi_card_ident[] = {
19893825Simp	/* CARD_ID			CARD_NAME		FIRM_TYPE */
19993825Simp	{ WI_NIC_LUCENT_ID,		WI_NIC_LUCENT_STR,	WI_LUCENT },
20093825Simp	{ WI_NIC_SONY_ID,		WI_NIC_SONY_STR,	WI_LUCENT },
20193825Simp	{ WI_NIC_LUCENT_EMB_ID,		WI_NIC_LUCENT_EMB_STR,	WI_LUCENT },
20293825Simp	{ WI_NIC_EVB2_ID,		WI_NIC_EVB2_STR,	WI_INTERSIL },
20393825Simp	{ WI_NIC_HWB3763_ID,		WI_NIC_HWB3763_STR,	WI_INTERSIL },
20493825Simp	{ WI_NIC_HWB3163_ID,		WI_NIC_HWB3163_STR,	WI_INTERSIL },
20593825Simp	{ WI_NIC_HWB3163B_ID,		WI_NIC_HWB3163B_STR,	WI_INTERSIL },
20693825Simp	{ WI_NIC_EVB3_ID,		WI_NIC_EVB3_STR,	WI_INTERSIL },
20793825Simp	{ WI_NIC_HWB1153_ID,		WI_NIC_HWB1153_STR,	WI_INTERSIL },
20893825Simp	{ WI_NIC_P2_SST_ID,		WI_NIC_P2_SST_STR,	WI_INTERSIL },
20993825Simp	{ WI_NIC_EVB2_SST_ID,		WI_NIC_EVB2_SST_STR,	WI_INTERSIL },
21093825Simp	{ WI_NIC_3842_EVA_ID,		WI_NIC_3842_EVA_STR,	WI_INTERSIL },
21193825Simp	{ WI_NIC_3842_PCMCIA_AMD_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
21293825Simp	{ WI_NIC_3842_PCMCIA_SST_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
213101355Simp	{ WI_NIC_3842_PCMCIA_ATL_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
214101355Simp	{ WI_NIC_3842_PCMCIA_ATS_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
21593825Simp	{ WI_NIC_3842_MINI_AMD_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
21693825Simp	{ WI_NIC_3842_MINI_SST_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
217101355Simp	{ WI_NIC_3842_MINI_ATL_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
218101355Simp	{ WI_NIC_3842_MINI_ATS_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
21993825Simp	{ WI_NIC_3842_PCI_AMD_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
22093825Simp	{ WI_NIC_3842_PCI_SST_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
221101355Simp	{ WI_NIC_3842_PCI_ATS_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
222101355Simp	{ WI_NIC_3842_PCI_ATL_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
22393825Simp	{ WI_NIC_P3_PCMCIA_AMD_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
22493825Simp	{ WI_NIC_P3_PCMCIA_SST_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
225101355Simp	{ WI_NIC_P3_PCMCIA_ATL_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
226101355Simp	{ WI_NIC_P3_PCMCIA_ATS_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
22793825Simp	{ WI_NIC_P3_MINI_AMD_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
22893825Simp	{ WI_NIC_P3_MINI_SST_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
229101355Simp	{ WI_NIC_P3_MINI_ATL_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
230101355Simp	{ WI_NIC_P3_MINI_ATS_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
23193825Simp	{ 0,	NULL,	0 },
23293825Simp};
23393825Simp
234109323Ssamdevclass_t wi_devclass;
23546492Swpaul
23693611Simpint
237109323Ssamwi_attach(device_t dev)
23874906Salfred{
239109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
240109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
241138571Ssam	struct ifnet *ifp = &sc->sc_if;
242116951Ssam	int i, nrates, buflen;
243109323Ssam	u_int16_t val;
244109323Ssam	u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
245116951Ssam	struct ieee80211_rateset *rs;
246109323Ssam	static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
247109323Ssam		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
248109323Ssam	};
249109323Ssam	int error;
25074906Salfred
251109323Ssam	/*
252109323Ssam	 * NB: no locking is needed here; don't put it here
253109323Ssam	 *     unless you can prove it!
254109323Ssam	 */
255121697Ssam	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
25674998Swpaul	    wi_intr, sc, &sc->wi_intrhand);
25753702Swpaul
25853702Swpaul	if (error) {
25953702Swpaul		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
26053702Swpaul		wi_free(dev);
26153702Swpaul		return (error);
26253702Swpaul	}
26353702Swpaul
26495534Simp#if __FreeBSD_version >= 500000
265109323Ssam	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
26693818Sjhb	    MTX_DEF | MTX_RECURSE);
26795534Simp#endif
26867092Swpaul
269138571Ssam	sc->sc_firmware_type = WI_NOTYPE;
270123339Simp	sc->wi_cmd_count = 500;
27146492Swpaul	/* Reset the NIC. */
272112096Simp	if (wi_reset(sc) != 0)
273109323Ssam		return ENXIO;		/* XXX */
27446492Swpaul
27576438Swpaul	/*
27676438Swpaul	 * Read the station address.
27776438Swpaul	 * And do it twice. I've seen PRISM-based cards that return
27876438Swpaul	 * an error when trying to read it the first time, which causes
27976438Swpaul	 * the probe to fail.
28076438Swpaul	 */
281109323Ssam	buflen = IEEE80211_ADDR_LEN;
282109323Ssam	error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
283109323Ssam	if (error != 0) {
284109323Ssam		buflen = IEEE80211_ADDR_LEN;
285109323Ssam		error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
286109323Ssam	}
287109323Ssam	if (error || IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) {
288109323Ssam		if (error != 0)
289109323Ssam			device_printf(dev, "mac read failed %d\n", error);
290109323Ssam		else
291109323Ssam			device_printf(dev, "mac read failed (all zeros)\n");
29275149Simp		wi_free(dev);
29375149Simp		return (error);
29475149Simp	}
29546492Swpaul
296109323Ssam	/* Read NIC identification */
297109323Ssam	wi_read_nicid(sc);
29846492Swpaul
29946492Swpaul	ifp->if_softc = sc;
300121816Sbrooks	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
30146492Swpaul	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
30246492Swpaul	ifp->if_ioctl = wi_ioctl;
30346492Swpaul	ifp->if_start = wi_start;
30446492Swpaul	ifp->if_watchdog = wi_watchdog;
30546492Swpaul	ifp->if_init = wi_init;
306132986Smlaier	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
307132986Smlaier	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
308132986Smlaier	IFQ_SET_READY(&ifp->if_snd);
30946492Swpaul
310138571Ssam	ic->ic_ifp = ifp;
311109323Ssam	ic->ic_phytype = IEEE80211_T_DS;
312109323Ssam	ic->ic_opmode = IEEE80211_M_STA;
313109323Ssam	ic->ic_state = IEEE80211_S_INIT;
314138571Ssam	ic->ic_caps = IEEE80211_C_PMGT
315138571Ssam		    | IEEE80211_C_WEP		/* everyone supports WEP */
316138571Ssam		    ;
317138571Ssam	ic->ic_max_aid = WI_MAX_AID;
31846492Swpaul
319116951Ssam	/*
320116951Ssam	 * Query the card for available channels and setup the
321116951Ssam	 * channel table.  We assume these are all 11b channels.
322116951Ssam	 */
323109323Ssam	buflen = sizeof(val);
324109323Ssam	if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
325109323Ssam		val = htole16(0x1fff);	/* assume 1-11 */
326116951Ssam	KASSERT(val != 0, ("wi_attach: no available channels listed!"));
327116951Ssam
328116951Ssam	val <<= 1;			/* shift for base 1 indices */
329116951Ssam	for (i = 1; i < 16; i++) {
330116951Ssam		if (isset((u_int8_t*)&val, i)) {
331116951Ssam			ic->ic_channels[i].ic_freq =
332116951Ssam				ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
333116951Ssam			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
334116951Ssam		}
335109323Ssam	}
33646492Swpaul
33746563Swpaul	/*
33846563Swpaul	 * Read the default channel from the NIC. This may vary
33946563Swpaul	 * depending on the country where the NIC was purchased, so
34046563Swpaul	 * we can't hard-code a default and expect it to work for
34146563Swpaul	 * everyone.
342116951Ssam	 *
343116951Ssam	 * If no channel is specified, let the 802.11 code select.
34446563Swpaul	 */
345109323Ssam	buflen = sizeof(val);
346116951Ssam	if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0) {
347116951Ssam		val = le16toh(val);
348116951Ssam		KASSERT(val < IEEE80211_CHAN_MAX &&
349116951Ssam			ic->ic_channels[val].ic_flags != 0,
350116951Ssam			("wi_attach: invalid own channel %u!", val));
351116951Ssam		ic->ic_ibss_chan = &ic->ic_channels[val];
352117042Ssam	} else {
353117042Ssam		device_printf(dev,
354117042Ssam			"WI_RID_OWN_CHNL failed, using first channel!\n");
355117042Ssam		ic->ic_ibss_chan = &ic->ic_channels[0];
356109323Ssam	}
35746563Swpaul
35856965Swpaul	/*
35998440Simp	 * Set flags based on firmware version.
36098440Simp	 */
36198440Simp	switch (sc->sc_firmware_type) {
36298440Simp	case WI_LUCENT:
363112363Simp		sc->sc_ntxbuf = 1;
364109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
365109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
366109323Ssam		/* XXX: not confirmed, but never seen for recent firmware */
367109323Ssam		if (sc->sc_sta_firmware_ver <  40000) {
368109323Ssam			sc->sc_flags |= WI_FLAGS_BUG_AUTOINC;
369109323Ssam		}
370109323Ssam#endif
37198440Simp		if (sc->sc_sta_firmware_ver >= 60000)
372109323Ssam			sc->sc_flags |= WI_FLAGS_HAS_MOR;
373117855Ssam		if (sc->sc_sta_firmware_ver >= 60006) {
374116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
375117855Ssam			ic->ic_caps |= IEEE80211_C_MONITOR;
376117855Ssam		}
377109323Ssam		sc->sc_ibss_port = htole16(1);
378119784Ssam
379119784Ssam		sc->sc_min_rssi = WI_LUCENT_MIN_RSSI;
380119784Ssam		sc->sc_max_rssi = WI_LUCENT_MAX_RSSI;
381119784Ssam		sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
38298440Simp		break;
383109323Ssam
38498440Simp	case WI_INTERSIL:
385112363Simp		sc->sc_ntxbuf = WI_NTXBUF;
386109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR;
387109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_ROAMING;
388109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
389123339Simp		/*
390123339Simp		 * Old firmware are slow, so give peace a chance.
391123339Simp		 */
392123339Simp		if (sc->sc_sta_firmware_ver < 10000)
393123339Simp			sc->wi_cmd_count = 5000;
394109323Ssam		if (sc->sc_sta_firmware_ver > 10101)
395109323Ssam			sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
396117855Ssam		if (sc->sc_sta_firmware_ver >= 800) {
397116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
398117855Ssam			ic->ic_caps |= IEEE80211_C_MONITOR;
399117855Ssam		}
400109396Simp		/*
401109396Simp		 * version 0.8.3 and newer are the only ones that are known
402109396Simp		 * to currently work.  Earlier versions can be made to work,
403109396Simp		 * at least according to the Linux driver.
404109396Simp		 */
405100734Simp		if (sc->sc_sta_firmware_ver >= 803)
406116951Ssam			ic->ic_caps |= IEEE80211_C_HOSTAP;
407109323Ssam		sc->sc_ibss_port = htole16(0);
408119784Ssam
409119784Ssam		sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
410119784Ssam		sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
411119784Ssam		sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
41298440Simp		break;
413109323Ssam
41498440Simp	case WI_SYMBOL:
415112363Simp		sc->sc_ntxbuf = 1;
416109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY;
41798440Simp		if (sc->sc_sta_firmware_ver >= 25000)
418116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
419109323Ssam		sc->sc_ibss_port = htole16(4);
420119784Ssam
421119784Ssam		sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
422119784Ssam		sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
423119784Ssam		sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
42498440Simp		break;
42598440Simp	}
42698440Simp
42798440Simp	/*
42856965Swpaul	 * Find out if we support WEP on this card.
42956965Swpaul	 */
430109323Ssam	buflen = sizeof(val);
431109323Ssam	if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
432109323Ssam	    val != htole16(0))
433116951Ssam		ic->ic_caps |= IEEE80211_C_WEP;
43456965Swpaul
435109323Ssam	/* Find supported rates. */
436109323Ssam	buflen = sizeof(ratebuf);
437116951Ssam	rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
438109323Ssam	if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
439116951Ssam		nrates = le16toh(*(u_int16_t *)ratebuf);
440116951Ssam		if (nrates > IEEE80211_RATE_MAXSIZE)
441116951Ssam			nrates = IEEE80211_RATE_MAXSIZE;
442116951Ssam		rs->rs_nrates = 0;
443116951Ssam		for (i = 0; i < nrates; i++)
444116951Ssam			if (ratebuf[2+i])
445116951Ssam				rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i];
446109323Ssam	} else {
447109323Ssam		/* XXX fallback on error? */
448116951Ssam		rs->rs_nrates = 0;
44998440Simp	}
45077217Sphk
451109323Ssam	buflen = sizeof(val);
452109323Ssam	if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
453109323Ssam	    wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
454119784Ssam		sc->sc_dbm_offset = le16toh(val);
455119784Ssam	}
45646492Swpaul
457109323Ssam	sc->sc_max_datalen = 2304;
458109323Ssam	sc->sc_system_scale = 1;
459109323Ssam	sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN;
460109323Ssam	sc->sc_roaming_mode = 1;
46146492Swpaul
462109323Ssam	sc->sc_portnum = WI_DEFAULT_PORT;
463109323Ssam	sc->sc_authtype = WI_DEFAULT_AUTHTYPE;
46487383Simp
465109323Ssam	bzero(sc->sc_nodename, sizeof(sc->sc_nodename));
466109323Ssam	sc->sc_nodelen = sizeof(WI_DEFAULT_NODENAME) - 1;
467109323Ssam	bcopy(WI_DEFAULT_NODENAME, sc->sc_nodename, sc->sc_nodelen);
46887383Simp
469109323Ssam	bzero(sc->sc_net_name, sizeof(sc->sc_net_name));
470109323Ssam	bcopy(WI_DEFAULT_NETNAME, sc->sc_net_name,
471109323Ssam	    sizeof(WI_DEFAULT_NETNAME) - 1);
47293733Simp
47393733Simp	/*
474109323Ssam	 * Call MI attach routine.
47593733Simp	 */
476138571Ssam	ieee80211_ifattach(ic);
477117812Ssam	/* override state transition method */
478117812Ssam	sc->sc_newstate = ic->ic_newstate;
479117812Ssam	ic->ic_newstate = wi_newstate;
480138571Ssam	ieee80211_media_init(ic, wi_media_change, wi_media_status);
481109323Ssam
482119784Ssam#if NBPFILTER > 0
483119784Ssam	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
484119784Ssam		sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
485119784Ssam		&sc->sc_drvbpf);
486119784Ssam	/*
487119784Ssam	 * Initialize constant fields.
488127698Ssam	 * XXX make header lengths a multiple of 32-bits so subsequent
489127698Ssam	 *     headers are properly aligned; this is a kludge to keep
490127698Ssam	 *     certain applications happy.
491119784Ssam	 *
492119784Ssam	 * NB: the channel is setup each time we transition to the
493119784Ssam	 *     RUN state to avoid filling it in for each frame.
494119784Ssam	 */
495127698Ssam	sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t));
496127698Ssam	sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len);
497127698Ssam	sc->sc_tx_th.wt_ihdr.it_present = htole32(WI_TX_RADIOTAP_PRESENT);
498119784Ssam
499127698Ssam	sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t));
500127698Ssam	sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len);
501127698Ssam	sc->sc_rx_th.wr_ihdr.it_present = htole32(WI_RX_RADIOTAP_PRESENT);
502119784Ssam#endif
503138571Ssam
504138571Ssam	if (bootverbose)
505138571Ssam		ieee80211_announce(ic);
506138571Ssam
507109323Ssam	return (0);
50887383Simp}
50987383Simp
510109323Ssamint
511109323Ssamwi_detach(device_t dev)
51246492Swpaul{
513109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
514138571Ssam	struct ifnet *ifp = &sc->sc_if;
515109323Ssam	WI_LOCK_DECL();
51646492Swpaul
517109323Ssam	WI_LOCK(sc);
51846492Swpaul
519109323Ssam	/* check if device was removed */
520123098Simp	sc->wi_gone |= !bus_child_present(dev);
52146492Swpaul
522109323Ssam	wi_stop(ifp, 0);
52346492Swpaul
524119784Ssam#if NBPFILTER > 0
525119784Ssam	bpfdetach(ifp);
526119784Ssam#endif
527138571Ssam	ieee80211_ifdetach(&sc->sc_ic);
528114190Simp	WI_UNLOCK(sc);
529109323Ssam	bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
530109323Ssam	wi_free(dev);
531109323Ssam#if __FreeBSD_version >= 500000
532109323Ssam	mtx_destroy(&sc->sc_mtx);
53393359Simp#endif
534109323Ssam	return (0);
535109323Ssam}
53693359Simp
537109323Ssam#ifdef __NetBSD__
538109323Ssamint
539109323Ssamwi_activate(struct device *self, enum devact act)
540109323Ssam{
541109323Ssam	struct wi_softc *sc = (struct wi_softc *)self;
542109323Ssam	int rv = 0, s;
54393359Simp
544109323Ssam	s = splnet();
545109323Ssam	switch (act) {
546109323Ssam	case DVACT_ACTIVATE:
547109323Ssam		rv = EOPNOTSUPP;
548109323Ssam		break;
54993359Simp
550109323Ssam	case DVACT_DEACTIVATE:
551138571Ssam		if_deactivate(&sc->sc_if);
552109323Ssam		break;
553109323Ssam	}
554109323Ssam	splx(s);
555109323Ssam	return rv;
556109323Ssam}
55793359Simp
558109323Ssamvoid
559109323Ssamwi_power(struct wi_softc *sc, int why)
560109323Ssam{
561138571Ssam	struct ifnet *ifp = &sc->sc_if;
562109323Ssam	int s;
56393359Simp
564109323Ssam	s = splnet();
565109323Ssam	switch (why) {
566109323Ssam	case PWR_SUSPEND:
567109323Ssam	case PWR_STANDBY:
568109323Ssam		wi_stop(ifp, 1);
569109323Ssam		break;
570109323Ssam	case PWR_RESUME:
571109323Ssam		if (ifp->if_flags & IFF_UP) {
572109323Ssam			wi_init(ifp);
573109323Ssam			(void)wi_intr(sc);
57494405Simp		}
575109323Ssam		break;
576109323Ssam	case PWR_SOFTSUSPEND:
577109323Ssam	case PWR_SOFTSTANDBY:
578109323Ssam	case PWR_SOFTRESUME:
579109323Ssam		break;
58093359Simp	}
581109323Ssam	splx(s);
58246492Swpaul}
583109323Ssam#endif /* __NetBSD__ */
58446492Swpaul
585109323Ssamvoid
586109323Ssamwi_shutdown(device_t dev)
58746492Swpaul{
588109323Ssam	struct wi_softc *sc = device_get_softc(dev);
58946492Swpaul
590109323Ssam	wi_stop(&sc->sc_if, 1);
59146492Swpaul}
59246492Swpaul
593109323Ssamvoid
594109323Ssamwi_intr(void *arg)
59546492Swpaul{
596109323Ssam	struct wi_softc *sc = arg;
597138571Ssam	struct ifnet *ifp = &sc->sc_if;
598112363Simp	u_int16_t status;
599109323Ssam	WI_LOCK_DECL();
60046492Swpaul
601109323Ssam	WI_LOCK(sc);
60246492Swpaul
603123098Simp	if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) {
604113327Simp		CSR_WRITE_2(sc, WI_INT_EN, 0);
605123098Simp		CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
606109323Ssam		WI_UNLOCK(sc);
60746492Swpaul		return;
608109323Ssam	}
60946492Swpaul
610112363Simp	/* Disable interrupts. */
611112363Simp	CSR_WRITE_2(sc, WI_INT_EN, 0);
61246492Swpaul
613112363Simp	status = CSR_READ_2(sc, WI_EVENT_STAT);
614112363Simp	if (status & WI_EV_RX)
615112363Simp		wi_rx_intr(sc);
616112363Simp	if (status & WI_EV_ALLOC)
617112363Simp		wi_tx_intr(sc);
618112363Simp	if (status & WI_EV_TX_EXC)
619112363Simp		wi_tx_ex_intr(sc);
620112363Simp	if (status & WI_EV_INFO)
621112363Simp		wi_info_intr(sc);
622112363Simp	if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
623112363Simp	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 &&
624132986Smlaier	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
625112363Simp		wi_start(ifp);
62646492Swpaul
627112363Simp	/* Re-enable interrupts. */
628112363Simp	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
62946492Swpaul
630109323Ssam	WI_UNLOCK(sc);
63146492Swpaul
63246492Swpaul	return;
63346492Swpaul}
63446492Swpaul
635109323Ssamvoid
636109323Ssamwi_init(void *arg)
63746492Swpaul{
638109323Ssam	struct wi_softc *sc = arg;
639109323Ssam	struct ifnet *ifp = &sc->sc_if;
640109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
641109323Ssam	struct wi_joinreq join;
642109323Ssam	int i;
643109323Ssam	int error = 0, wasenabled;
644109323Ssam	struct ifaddr *ifa;
645109323Ssam	struct sockaddr_dl *sdl;
646109323Ssam	WI_LOCK_DECL();
64746492Swpaul
648109323Ssam	WI_LOCK(sc);
64967092Swpaul
650109323Ssam	if (sc->wi_gone) {
651109323Ssam		WI_UNLOCK(sc);
65246492Swpaul		return;
65346492Swpaul	}
65446492Swpaul
655112362Simp	if ((wasenabled = sc->sc_enabled))
656123098Simp		wi_stop(ifp, 1);
657112362Simp	wi_reset(sc);
65846492Swpaul
659109323Ssam	/* common 802.11 configuration */
660109323Ssam	ic->ic_flags &= ~IEEE80211_F_IBSSON;
661109323Ssam	sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
662109323Ssam	switch (ic->ic_opmode) {
663109323Ssam	case IEEE80211_M_STA:
664109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS);
665109323Ssam		break;
666109323Ssam	case IEEE80211_M_IBSS:
667109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port);
668109323Ssam		ic->ic_flags |= IEEE80211_F_IBSSON;
669109323Ssam		break;
670109323Ssam	case IEEE80211_M_AHDEMO:
671109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
672109323Ssam		break;
673109323Ssam	case IEEE80211_M_HOSTAP:
674122087Sgreen		/*
675122087Sgreen		 * For PRISM cards, override the empty SSID, because in
676122087Sgreen		 * HostAP mode the controller will lock up otherwise.
677122087Sgreen		 */
678122087Sgreen		if (sc->sc_firmware_type == WI_INTERSIL &&
679122087Sgreen		    ic->ic_des_esslen == 0) {
680122087Sgreen			ic->ic_des_essid[0] = ' ';
681122087Sgreen			ic->ic_des_esslen = 1;
682122087Sgreen		}
683109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP);
684109323Ssam		break;
685117855Ssam	case IEEE80211_M_MONITOR:
686117855Ssam		if (sc->sc_firmware_type == WI_LUCENT)
687117855Ssam			wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
688117855Ssam		wi_cmd(sc, WI_CMD_DEBUG | (WI_TEST_MONITOR << 8), 0, 0, 0);
689117855Ssam		break;
69046492Swpaul	}
69146492Swpaul
692109323Ssam	/* Intersil interprets this RID as joining ESS even in IBSS mode */
693109323Ssam	if (sc->sc_firmware_type == WI_LUCENT &&
694109323Ssam	    (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0)
695109323Ssam		wi_write_val(sc, WI_RID_CREATE_IBSS, 1);
696109323Ssam	else
697109323Ssam		wi_write_val(sc, WI_RID_CREATE_IBSS, 0);
698109323Ssam	wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
699109323Ssam	wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid,
700109323Ssam	    ic->ic_des_esslen);
701116951Ssam	wi_write_val(sc, WI_RID_OWN_CHNL,
702116951Ssam		ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
703109323Ssam	wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen);
70446492Swpaul
705109323Ssam	ifa = ifaddr_byindex(ifp->if_index);
706109323Ssam	sdl = (struct sockaddr_dl *) ifa->ifa_addr;
707109323Ssam	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(sdl));
708109323Ssam	wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN);
70975373Salfred
710138571Ssam	if (ic->ic_caps & IEEE80211_C_PMGT)
711138571Ssam		wi_write_val(sc, WI_RID_PM_ENABLED,
712138571Ssam		    (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
71346492Swpaul
714109323Ssam	/* not yet common 802.11 configuration */
715109323Ssam	wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen);
716116951Ssam	wi_write_val(sc, WI_RID_RTS_THRESH, ic->ic_rtsthreshold);
717109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
718116951Ssam		wi_write_val(sc, WI_RID_FRAG_THRESH, ic->ic_fragthreshold);
71946492Swpaul
720109323Ssam	/* driver specific 802.11 configuration */
721109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)
722109323Ssam		wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale);
723109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
724109323Ssam		wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode);
725109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_MOR)
726109323Ssam		wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven);
727109323Ssam	wi_write_txrate(sc);
728109323Ssam	wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen);
72946492Swpaul
730109323Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
731109323Ssam	    sc->sc_firmware_type == WI_INTERSIL) {
732109323Ssam		wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval);
733109323Ssam		wi_write_val(sc, WI_RID_BASIC_RATE, 0x03);   /* 1, 2 */
734109323Ssam		wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */
735109323Ssam		wi_write_val(sc, WI_RID_DTIM_PERIOD, 1);
73646492Swpaul	}
73746492Swpaul
738109323Ssam	/*
739109323Ssam	 * Initialize promisc mode.
740109323Ssam	 *	Being in the Host-AP mode causes a great
741109323Ssam	 *	deal of pain if primisc mode is set.
742109323Ssam	 *	Therefore we avoid confusing the firmware
743109323Ssam	 *	and always reset promisc mode in Host-AP
744109323Ssam	 *	mode.  Host-AP sees all the packets anyway.
745109323Ssam	 */
746109323Ssam	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
747109323Ssam	    (ifp->if_flags & IFF_PROMISC) != 0) {
748109323Ssam		wi_write_val(sc, WI_RID_PROMISC, 1);
749109323Ssam	} else {
750109323Ssam		wi_write_val(sc, WI_RID_PROMISC, 0);
75175373Salfred	}
75246492Swpaul
753109323Ssam	/* Configure WEP. */
754116951Ssam	if (ic->ic_caps & IEEE80211_C_WEP)
755109323Ssam		wi_write_wep(sc);
75667092Swpaul
757109323Ssam	/* Set multicast filter. */
758109323Ssam	wi_write_multi(sc);
75946492Swpaul
760114124Simp	/* Allocate fids for the card */
761109323Ssam	if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
762109323Ssam		sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
763109323Ssam		if (sc->sc_firmware_type == WI_SYMBOL)
764109323Ssam			sc->sc_buflen = 1585;	/* XXX */
765112363Simp		for (i = 0; i < sc->sc_ntxbuf; i++) {
766109323Ssam			error = wi_alloc_fid(sc, sc->sc_buflen,
767109323Ssam			    &sc->sc_txd[i].d_fid);
768109323Ssam			if (error) {
769109323Ssam				device_printf(sc->sc_dev,
770109323Ssam				    "tx buffer allocation failed (error %u)\n",
771109323Ssam				    error);
772109323Ssam				goto out;
773109323Ssam			}
774109323Ssam			sc->sc_txd[i].d_len = 0;
77570073Swpaul		}
77670073Swpaul	}
777109323Ssam	sc->sc_txcur = sc->sc_txnext = 0;
77870073Swpaul
779109323Ssam	/* Enable desired port */
780109323Ssam	wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
78146492Swpaul
782114124Simp	sc->sc_enabled = 1;
783109323Ssam	ifp->if_flags |= IFF_RUNNING;
784109323Ssam	ifp->if_flags &= ~IFF_OACTIVE;
785109323Ssam	if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
786139542Ssam	    ic->ic_opmode == IEEE80211_M_IBSS ||
787117855Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR ||
788109323Ssam	    ic->ic_opmode == IEEE80211_M_HOSTAP)
789139542Ssam		ieee80211_create_ibss(ic, ic->ic_ibss_chan);
79046492Swpaul
791109323Ssam	/* Enable interrupts */
792109323Ssam	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
79346492Swpaul
794109323Ssam	if (!wasenabled &&
795109323Ssam	    ic->ic_opmode == IEEE80211_M_HOSTAP &&
796109323Ssam	    sc->sc_firmware_type == WI_INTERSIL) {
797109323Ssam		/* XXX: some card need to be re-enabled for hostap */
798109323Ssam		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
799109323Ssam		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
80075149Simp	}
80194397Simp
802109323Ssam	if (ic->ic_opmode == IEEE80211_M_STA &&
803109323Ssam	    ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
804116951Ssam	    ic->ic_des_chan != IEEE80211_CHAN_ANYC)) {
805109323Ssam		memset(&join, 0, sizeof(join));
806109323Ssam		if (ic->ic_flags & IEEE80211_F_DESBSSID)
807109323Ssam			IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
808116951Ssam		if (ic->ic_des_chan != IEEE80211_CHAN_ANYC)
809116951Ssam			join.wi_chan = htole16(
810116951Ssam				ieee80211_chan2ieee(ic, ic->ic_des_chan));
811109323Ssam		/* Lucent firmware does not support the JOIN RID. */
812109323Ssam		if (sc->sc_firmware_type != WI_LUCENT)
813109323Ssam			wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
81494397Simp	}
81575373Salfred
816109323Ssam	WI_UNLOCK(sc);
81746492Swpaul	return;
818109323Ssamout:
819109323Ssam	if (error) {
820109323Ssam		if_printf(ifp, "interface not running\n");
821123098Simp		wi_stop(ifp, 1);
82270073Swpaul	}
823110224Simp	WI_UNLOCK(sc);
824109323Ssam	DPRINTF(("wi_init: return %d\n", error));
825109323Ssam	return;
82646492Swpaul}
82746492Swpaul
828109323Ssamvoid
829109323Ssamwi_stop(struct ifnet *ifp, int disable)
83046492Swpaul{
831109323Ssam	struct wi_softc *sc = ifp->if_softc;
832138571Ssam	struct ieee80211com *ic = &sc->sc_ic;
833109323Ssam	WI_LOCK_DECL();
83446492Swpaul
835109323Ssam	WI_LOCK(sc);
83674998Swpaul
837123098Simp	DELAY(100000);
838123098Simp
839117812Ssam	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
840109323Ssam	if (sc->sc_enabled && !sc->wi_gone) {
841109323Ssam		CSR_WRITE_2(sc, WI_INT_EN, 0);
842109323Ssam		wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
843109323Ssam		if (disable) {
844109323Ssam#ifdef __NetBSD__
845109323Ssam			if (sc->sc_disable)
846109323Ssam				(*sc->sc_disable)(sc);
847109323Ssam#endif
848116951Ssam			sc->sc_enabled = 0;
84970073Swpaul		}
850123098Simp	} else if (sc->wi_gone && disable)	/* gone --> not enabled */
851123098Simp	    sc->sc_enabled = 0;
85270073Swpaul
853109323Ssam	sc->sc_tx_timer = 0;
854109323Ssam	sc->sc_scan_timer = 0;
855109323Ssam	sc->sc_false_syns = 0;
856109323Ssam	sc->sc_naps = 0;
857109323Ssam	ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
858109323Ssam	ifp->if_timer = 0;
85946492Swpaul
860109323Ssam	WI_UNLOCK(sc);
86146492Swpaul}
86246492Swpaul
863109323Ssamstatic void
864109323Ssamwi_start(struct ifnet *ifp)
86546492Swpaul{
866109323Ssam	struct wi_softc	*sc = ifp->if_softc;
867109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
868119150Ssam	struct ieee80211_node *ni;
869109323Ssam	struct ieee80211_frame *wh;
870138571Ssam	struct ether_header *eh;
871109323Ssam	struct mbuf *m0;
872109323Ssam	struct wi_frame frmhdr;
873119150Ssam	int cur, fid, off, error;
874109323Ssam	WI_LOCK_DECL();
87546492Swpaul
876109323Ssam	WI_LOCK(sc);
87746492Swpaul
878109323Ssam	if (sc->wi_gone) {
879109323Ssam		WI_UNLOCK(sc);
880109323Ssam		return;
88146492Swpaul	}
882109323Ssam	if (sc->sc_flags & WI_FLAGS_OUTRANGE) {
883109323Ssam		WI_UNLOCK(sc);
884109323Ssam		return;
88575373Salfred	}
88646492Swpaul
887109323Ssam	memset(&frmhdr, 0, sizeof(frmhdr));
888109323Ssam	cur = sc->sc_txnext;
889109323Ssam	for (;;) {
890109323Ssam		IF_POLL(&ic->ic_mgtq, m0);
891109323Ssam		if (m0 != NULL) {
892109323Ssam			if (sc->sc_txd[cur].d_len != 0) {
893109323Ssam				ifp->if_flags |= IFF_OACTIVE;
894109323Ssam				break;
895109323Ssam			}
896109323Ssam			IF_DEQUEUE(&ic->ic_mgtq, m0);
897119150Ssam			/*
898119150Ssam			 * Hack!  The referenced node pointer is in the
899119150Ssam			 * rcvif field of the packet header.  This is
900119150Ssam			 * placed there by ieee80211_mgmt_output because
901119150Ssam			 * we need to hold the reference with the frame
902119150Ssam			 * and there's no other way (other than packet
903119150Ssam			 * tags which we consider too expensive to use)
904119150Ssam			 * to pass it along.
905119150Ssam			 */
906119150Ssam			ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
907119150Ssam			m0->m_pkthdr.rcvif = NULL;
908119150Ssam
909109323Ssam			m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
910109323Ssam			    (caddr_t)&frmhdr.wi_ehdr);
911109323Ssam			frmhdr.wi_ehdr.ether_type = 0;
912109323Ssam                        wh = mtod(m0, struct ieee80211_frame *);
913109323Ssam		} else {
914109323Ssam			if (ic->ic_state != IEEE80211_S_RUN)
915109323Ssam				break;
916132986Smlaier			IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
917109323Ssam			if (m0 == NULL)
918109323Ssam				break;
919109323Ssam			if (sc->sc_txd[cur].d_len != 0) {
920132986Smlaier				IFQ_DRV_PREPEND(&ifp->if_snd, m0);
921109323Ssam				ifp->if_flags |= IFF_OACTIVE;
922109323Ssam				break;
923109323Ssam			}
924138571Ssam			if (m0->m_len < sizeof(struct ether_header) &&
925138571Ssam			    (m0 = m_pullup(m0, sizeof(struct ether_header))) == NULL) {
926138571Ssam				ifp->if_oerrors++;
927138571Ssam				continue;
928138571Ssam			}
929138571Ssam			eh = mtod(m0, struct ether_header *);
930138571Ssam			ni = ieee80211_find_txnode(ic, eh->ether_dhost);
931138571Ssam			if (ni == NULL) {
932138571Ssam				m_freem(m0);
933138571Ssam				continue;
934138571Ssam			}
935109323Ssam			ifp->if_opackets++;
936109323Ssam			m_copydata(m0, 0, ETHER_HDR_LEN,
937109323Ssam			    (caddr_t)&frmhdr.wi_ehdr);
938109323Ssam#if NBPFILTER > 0
939109323Ssam			BPF_MTAP(ifp, m0);
940109323Ssam#endif
94146492Swpaul
942138571Ssam			m0 = ieee80211_encap(ic, m0, ni);
943119150Ssam			if (m0 == NULL) {
944109323Ssam				ifp->if_oerrors++;
945138571Ssam				ieee80211_free_node(ni);
946109323Ssam				continue;
947109323Ssam			}
948109323Ssam                        wh = mtod(m0, struct ieee80211_frame *);
949109323Ssam		}
950109323Ssam#if NBPFILTER > 0
951109323Ssam		if (ic->ic_rawbpf)
952109323Ssam			bpf_mtap(ic->ic_rawbpf, m0);
953109323Ssam#endif
954109323Ssam		frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
955138952Ssam		/* XXX check key for SWCRYPT instead of using operating mode */
956138952Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
957138952Ssam		    (wh->i_fc[1] & IEEE80211_FC1_WEP)) {
958138571Ssam			struct ieee80211_key *k;
959138571Ssam
960138571Ssam			k = ieee80211_crypto_encap(ic, ni, m0);
961138571Ssam			if (k == NULL) {
962138571Ssam				if (ni != NULL)
963138571Ssam					ieee80211_free_node(ni);
964109323Ssam				continue;
965109323Ssam			}
966138952Ssam			frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
967109323Ssam		}
968109323Ssam#if NBPFILTER > 0
969109323Ssam		if (sc->sc_drvbpf) {
970123927Ssam			sc->sc_tx_th.wt_rate =
971123927Ssam				ni->ni_rates.rs_rates[ni->ni_txrate];
972123922Ssam			bpf_mtap2(sc->sc_drvbpf,
973127698Ssam				&sc->sc_tx_th, sc->sc_tx_th_len, m0);
974109323Ssam		}
97546492Swpaul#endif
976127697Ssam		m_copydata(m0, 0, sizeof(struct ieee80211_frame),
977127697Ssam		    (caddr_t)&frmhdr.wi_whdr);
978127697Ssam		m_adj(m0, sizeof(struct ieee80211_frame));
979127697Ssam		frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
980109323Ssam		if (IFF_DUMPPKTS(ifp))
981116951Ssam			wi_dump_pkt(&frmhdr, NULL, -1);
982109323Ssam		fid = sc->sc_txd[cur].d_fid;
983109323Ssam		off = sizeof(frmhdr);
984119150Ssam		error = wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0
985119150Ssam		     || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
986119150Ssam		m_freem(m0);
987138571Ssam		if (ni != NULL)
988138571Ssam			ieee80211_free_node(ni);
989119150Ssam		if (error) {
990109323Ssam			ifp->if_oerrors++;
991109323Ssam			continue;
992109323Ssam		}
993109323Ssam		sc->sc_txd[cur].d_len = off;
994109323Ssam		if (sc->sc_txcur == cur) {
995109323Ssam			if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
996109323Ssam				if_printf(ifp, "xmit failed\n");
997109323Ssam				sc->sc_txd[cur].d_len = 0;
998109323Ssam				continue;
999109323Ssam			}
1000109323Ssam			sc->sc_tx_timer = 5;
1001109323Ssam			ifp->if_timer = 1;
1002109323Ssam		}
1003112363Simp		sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
100474838Salfred	}
100546492Swpaul
1006109323Ssam	WI_UNLOCK(sc);
100746492Swpaul}
100846492Swpaul
100988546Salfredstatic int
1010109323Ssamwi_reset(struct wi_softc *sc)
101146492Swpaul{
1012138571Ssam	struct ifnet *ifp = &sc->sc_if;
1013114124Simp#define WI_INIT_TRIES 3
1014114124Simp	int i;
1015114138Simp	int error = 0;
1016114124Simp	int tries;
1017114124Simp
1018114124Simp	/* Symbol firmware cannot be initialized more than once */
1019123098Simp	if (sc->sc_firmware_type == WI_SYMBOL && sc->sc_reset)
1020112362Simp		return (0);
1021114124Simp	if (sc->sc_firmware_type == WI_SYMBOL)
1022114124Simp		tries = 1;
1023114124Simp	else
1024114124Simp		tries = WI_INIT_TRIES;
1025112362Simp
1026114124Simp	for (i = 0; i < tries; i++) {
1027109323Ssam		if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
102846492Swpaul			break;
1029109323Ssam		DELAY(WI_DELAY * 1000);
103046492Swpaul	}
1031114124Simp	sc->sc_reset = 1;
103246492Swpaul
1033114124Simp	if (i == tries) {
1034114124Simp		if_printf(ifp, "init failed\n");
1035114124Simp		return (error);
103675373Salfred	}
103746492Swpaul
1038109323Ssam	CSR_WRITE_2(sc, WI_INT_EN, 0);
1039114124Simp	CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
104046492Swpaul
1041109323Ssam	/* Calibrate timer. */
1042114124Simp	wi_write_val(sc, WI_RID_TICK_TIME, 8);
1043114124Simp
1044114124Simp	return (0);
1045109323Ssam#undef WI_INIT_TRIES
104646492Swpaul}
104746492Swpaul
104888546Salfredstatic void
1049109323Ssamwi_watchdog(struct ifnet *ifp)
105046492Swpaul{
1051109323Ssam	struct wi_softc	*sc = ifp->if_softc;
105246492Swpaul
1053109323Ssam	ifp->if_timer = 0;
1054109323Ssam	if (!sc->sc_enabled)
1055109323Ssam		return;
105646492Swpaul
1057109323Ssam	if (sc->sc_tx_timer) {
1058109323Ssam		if (--sc->sc_tx_timer == 0) {
1059109323Ssam			if_printf(ifp, "device timeout\n");
1060109323Ssam			ifp->if_oerrors++;
1061109323Ssam			wi_init(ifp->if_softc);
1062109323Ssam			return;
1063109323Ssam		}
1064109323Ssam		ifp->if_timer = 1;
106546492Swpaul	}
106646492Swpaul
1067109323Ssam	if (sc->sc_scan_timer) {
1068109323Ssam		if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
1069109323Ssam		    sc->sc_firmware_type == WI_INTERSIL) {
1070109323Ssam			DPRINTF(("wi_watchdog: inquire scan\n"));
1071109323Ssam			wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
107246492Swpaul		}
1073109323Ssam		if (sc->sc_scan_timer)
1074109323Ssam			ifp->if_timer = 1;
107546492Swpaul	}
107646492Swpaul
1077109323Ssam	/* TODO: rate control */
1078138571Ssam	ieee80211_watchdog(&sc->sc_ic);
107946492Swpaul}
108046492Swpaul
108188546Salfredstatic int
1082109323Ssamwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
108346492Swpaul{
1084109323Ssam	struct wi_softc *sc = ifp->if_softc;
1085109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1086109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
1087109323Ssam	struct ieee80211req *ireq;
1088109323Ssam	u_int8_t nodename[IEEE80211_NWID_LEN];
1089109323Ssam	int error = 0;
109095534Simp#if __FreeBSD_version >= 500000
1091109323Ssam	struct thread *td = curthread;
109295534Simp#else
1093109323Ssam	struct proc *td = curproc;		/* Little white lie */
109495534Simp#endif
1095109323Ssam	struct wi_req wreq;
1096109323Ssam	WI_LOCK_DECL();
109746492Swpaul
1098138571Ssam	if (sc->wi_gone)
1099138571Ssam		return (ENODEV);
110046492Swpaul
1101109323Ssam	switch (cmd) {
110246492Swpaul	case SIOCSIFFLAGS:
1103100876Simp		/*
1104101139Simp		 * Can't do promisc and hostap at the same time.  If all that's
1105101139Simp		 * changing is the promisc flag, try to short-circuit a call to
1106101139Simp		 * wi_init() by just setting PROMISC in the hardware.
1107100876Simp		 */
1108138571Ssam		WI_LOCK(sc);
110946492Swpaul		if (ifp->if_flags & IFF_UP) {
1110109323Ssam			if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1111101139Simp			    ifp->if_flags & IFF_RUNNING) {
1112101139Simp				if (ifp->if_flags & IFF_PROMISC &&
1113109323Ssam				    !(sc->sc_if_flags & IFF_PROMISC)) {
1114109323Ssam					wi_write_val(sc, WI_RID_PROMISC, 1);
1115101139Simp				} else if (!(ifp->if_flags & IFF_PROMISC) &&
1116109323Ssam				    sc->sc_if_flags & IFF_PROMISC) {
1117109323Ssam					wi_write_val(sc, WI_RID_PROMISC, 0);
1118101139Simp				} else {
1119101139Simp					wi_init(sc);
1120101139Simp				}
1121100876Simp			} else {
112246492Swpaul				wi_init(sc);
1123100876Simp			}
112446492Swpaul		} else {
112546492Swpaul			if (ifp->if_flags & IFF_RUNNING) {
1126123098Simp				wi_stop(ifp, 1);
112746492Swpaul			}
1128123098Simp			sc->wi_gone = 0;
112946492Swpaul		}
1130109323Ssam		sc->sc_if_flags = ifp->if_flags;
1131138571Ssam		WI_UNLOCK(sc);
113246492Swpaul		error = 0;
113346492Swpaul		break;
113446492Swpaul	case SIOCADDMULTI:
113546492Swpaul	case SIOCDELMULTI:
1136138571Ssam		WI_LOCK(sc);
1137109323Ssam		error = wi_write_multi(sc);
1138138571Ssam		WI_UNLOCK(sc);
113946492Swpaul		break;
1140109323Ssam	case SIOCGIFGENERIC:
1141138571Ssam		WI_LOCK(sc);
1142109323Ssam		error = wi_get_cfg(ifp, cmd, data);
1143138571Ssam		WI_UNLOCK(sc);
114446492Swpaul		break;
1145109323Ssam	case SIOCSIFGENERIC:
1146109323Ssam		error = suser(td);
1147138571Ssam		if (error == 0)
1148138571Ssam			error = wi_set_cfg(ifp, cmd, data);
114946492Swpaul		break;
115093359Simp	case SIOCGPRISM2DEBUG:
115193359Simp		error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
115293359Simp		if (error)
115393359Simp			break;
115493733Simp		if (!(ifp->if_flags & IFF_RUNNING) ||
115593733Simp		    sc->sc_firmware_type == WI_LUCENT) {
115693359Simp			error = EIO;
115793359Simp			break;
115893359Simp		}
115993359Simp		error = wi_get_debug(sc, &wreq);
116093359Simp		if (error == 0)
116193359Simp			error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
116293359Simp		break;
116393359Simp	case SIOCSPRISM2DEBUG:
116493593Sjhb		if ((error = suser(td)))
1165138571Ssam			return (error);
116693359Simp		error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
116793359Simp		if (error)
116893359Simp			break;
1169138571Ssam		WI_LOCK(sc);
117093359Simp		error = wi_set_debug(sc, &wreq);
1171138571Ssam		WI_UNLOCK(sc);
117293359Simp		break;
117377217Sphk	case SIOCG80211:
1174109323Ssam		ireq = (struct ieee80211req *) data;
1175138571Ssam		if (ireq->i_type == IEEE80211_IOC_STATIONNAME) {
1176109323Ssam			ireq->i_len = sc->sc_nodelen + 1;
1177109323Ssam			error = copyout(sc->sc_nodename, ireq->i_data,
1178109323Ssam					ireq->i_len);
117977217Sphk			break;
118077217Sphk		}
1181138571Ssam		goto ioctl_common;
118277217Sphk	case SIOCS80211:
1183109323Ssam		ireq = (struct ieee80211req *) data;
1184138571Ssam		if (ireq->i_type == IEEE80211_IOC_STATIONNAME) {
1185138571Ssam			error = suser(td);
1186138571Ssam			if (error)
1187138571Ssam				break;
118877217Sphk			if (ireq->i_val != 0 ||
118977217Sphk			    ireq->i_len > IEEE80211_NWID_LEN) {
119077217Sphk				error = EINVAL;
119177217Sphk				break;
119277217Sphk			}
1193109323Ssam			memset(nodename, 0, IEEE80211_NWID_LEN);
1194109323Ssam			error = copyin(ireq->i_data, nodename, ireq->i_len);
1195109323Ssam			if (error)
119677217Sphk				break;
1197138571Ssam			WI_LOCK(sc);
1198109323Ssam			if (sc->sc_enabled) {
1199109323Ssam				error = wi_write_ssid(sc, WI_RID_NODENAME,
1200109323Ssam					nodename, ireq->i_len);
120177217Sphk			}
1202138571Ssam			if (error == 0) {
1203138571Ssam				memcpy(sc->sc_nodename, nodename,
1204138571Ssam					IEEE80211_NWID_LEN);
1205138571Ssam				sc->sc_nodelen = ireq->i_len;
1206138571Ssam			}
1207138571Ssam			WI_UNLOCK(sc);
120877217Sphk			break;
120977217Sphk		}
1210138571Ssam		goto ioctl_common;
121146492Swpaul	default:
1212138571Ssam	ioctl_common:
1213138571Ssam		WI_LOCK(sc);
1214138571Ssam		error = ieee80211_ioctl(ic, cmd, data);
1215138571Ssam		if (error == ENETRESET) {
1216138571Ssam			if (sc->sc_enabled)
1217138571Ssam				wi_init(sc);	/* XXX no error return */
1218138571Ssam			error = 0;
1219138571Ssam		}
1220138571Ssam		WI_UNLOCK(sc);
122146492Swpaul		break;
122246492Swpaul	}
1223109323Ssam	return (error);
122446492Swpaul}
122546492Swpaul
1226109323Ssamstatic int
1227109323Ssamwi_media_change(struct ifnet *ifp)
122846492Swpaul{
1229109323Ssam	struct wi_softc *sc = ifp->if_softc;
1230116951Ssam	int error;
123146492Swpaul
1232116951Ssam	error = ieee80211_media_change(ifp);
1233109323Ssam	if (error == ENETRESET) {
1234109323Ssam		if (sc->sc_enabled)
1235116951Ssam			wi_init(sc);	/* XXX no error return */
1236109323Ssam		error = 0;
1237109323Ssam	}
1238109323Ssam	return error;
1239109323Ssam}
124046492Swpaul
1241109323Ssamstatic void
1242109323Ssamwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1243109323Ssam{
1244109323Ssam	struct wi_softc *sc = ifp->if_softc;
1245109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1246109323Ssam	u_int16_t val;
1247109323Ssam	int rate, len;
124846492Swpaul
1249109323Ssam	if (sc->wi_gone || !sc->sc_enabled) {
1250109323Ssam		imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
1251109323Ssam		imr->ifm_status = 0;
1252109323Ssam		return;
1253109323Ssam	}
125446492Swpaul
1255109323Ssam	imr->ifm_status = IFM_AVALID;
1256109323Ssam	imr->ifm_active = IFM_IEEE80211;
1257109323Ssam	if (ic->ic_state == IEEE80211_S_RUN &&
1258109323Ssam	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
1259109323Ssam		imr->ifm_status |= IFM_ACTIVE;
1260109323Ssam	len = sizeof(val);
1261138571Ssam	if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) == 0 &&
1262138571Ssam	    len == sizeof(val)) {
1263109323Ssam		/* convert to 802.11 rate */
1264138571Ssam		val = le16toh(val);
1265109323Ssam		rate = val * 2;
1266109323Ssam		if (sc->sc_firmware_type == WI_LUCENT) {
1267138571Ssam			if (rate == 10)
1268109323Ssam				rate = 11;	/* 5.5Mbps */
1269109323Ssam		} else {
1270109323Ssam			if (rate == 4*2)
1271109323Ssam				rate = 11;	/* 5.5Mbps */
1272109323Ssam			else if (rate == 8*2)
1273109323Ssam				rate = 22;	/* 11Mbps */
1274109323Ssam		}
1275138571Ssam	} else
1276138571Ssam		rate = 0;
1277116951Ssam	imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
1278109323Ssam	switch (ic->ic_opmode) {
1279109323Ssam	case IEEE80211_M_STA:
1280109323Ssam		break;
1281109323Ssam	case IEEE80211_M_IBSS:
1282109323Ssam		imr->ifm_active |= IFM_IEEE80211_ADHOC;
1283109323Ssam		break;
1284109323Ssam	case IEEE80211_M_AHDEMO:
1285109323Ssam		imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1286109323Ssam		break;
1287109323Ssam	case IEEE80211_M_HOSTAP:
1288109323Ssam		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1289109323Ssam		break;
1290117855Ssam	case IEEE80211_M_MONITOR:
1291117855Ssam		imr->ifm_active |= IFM_IEEE80211_MONITOR;
1292117855Ssam		break;
1293109323Ssam	}
1294109323Ssam}
129546492Swpaul
1296109323Ssamstatic void
1297109323Ssamwi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1298109323Ssam{
1299109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1300116951Ssam	struct ieee80211_node *ni = ic->ic_bss;
1301138571Ssam	struct ifnet *ifp = &sc->sc_if;
130298440Simp
1303109323Ssam	if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1304109323Ssam		return;
130546492Swpaul
1306109323Ssam	DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid)));
1307109323Ssam	DPRINTF(("%s ?\n", ether_sprintf(new_bssid)));
130846492Swpaul
1309109323Ssam	/* In promiscuous mode, the BSSID field is not a reliable
1310109323Ssam	 * indicator of the firmware's BSSID. Damp spurious
1311109323Ssam	 * change-of-BSSID indications.
1312109323Ssam	 */
1313109323Ssam	if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1314138571Ssam	    !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns,
1315138571Ssam	                 WI_MAX_FALSE_SYNS))
1316109323Ssam		return;
131746492Swpaul
1318139542Ssam	sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1);
1319139542Ssam	/*
1320139542Ssam	 * XXX hack; we should create a new node with the new bssid
1321139542Ssam	 * and replace the existing ic_bss with it but since we don't
1322139542Ssam	 * process management frames to collect state we cheat by
1323139542Ssam	 * reusing the existing node as we know wi_newstate will be
1324139542Ssam	 * called and it will overwrite the node state.
1325139542Ssam	 */
1326139542Ssam	ieee80211_sta_join(ic, ieee80211_ref_node(ni));
1327109323Ssam}
132846492Swpaul
1329109323Ssamstatic void
1330116898Ssamwi_rx_monitor(struct wi_softc *sc, int fid)
1331116898Ssam{
1332138571Ssam	struct ifnet *ifp = &sc->sc_if;
1333116898Ssam	struct wi_frame *rx_frame;
1334116898Ssam	struct mbuf *m;
1335116898Ssam	int datlen, hdrlen;
1336116898Ssam
1337116898Ssam	/* first allocate mbuf for packet storage */
1338116898Ssam	m = m_getcl(M_DONTWAIT, MT_DATA, 0);
1339116898Ssam	if (m == NULL) {
1340116898Ssam		ifp->if_ierrors++;
1341116898Ssam		return;
1342116898Ssam	}
1343116898Ssam
1344116898Ssam	m->m_pkthdr.rcvif = ifp;
1345116898Ssam
1346116898Ssam	/* now read wi_frame first so we know how much data to read */
1347116898Ssam	if (wi_read_bap(sc, fid, 0, mtod(m, caddr_t), sizeof(*rx_frame))) {
1348116898Ssam		ifp->if_ierrors++;
1349116898Ssam		goto done;
1350116898Ssam	}
1351116898Ssam
1352116898Ssam	rx_frame = mtod(m, struct wi_frame *);
1353116898Ssam
1354116898Ssam	switch ((rx_frame->wi_status & WI_STAT_MAC_PORT) >> 8) {
1355116898Ssam	case 7:
1356116898Ssam		switch (rx_frame->wi_whdr.i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1357116898Ssam		case IEEE80211_FC0_TYPE_DATA:
1358116898Ssam			hdrlen = WI_DATA_HDRLEN;
1359116898Ssam			datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1360116898Ssam			break;
1361116898Ssam		case IEEE80211_FC0_TYPE_MGT:
1362116898Ssam			hdrlen = WI_MGMT_HDRLEN;
1363116898Ssam			datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1364116898Ssam			break;
1365116898Ssam		case IEEE80211_FC0_TYPE_CTL:
1366116898Ssam			/*
1367116898Ssam			 * prism2 cards don't pass control packets
1368116898Ssam			 * down properly or consistently, so we'll only
1369116898Ssam			 * pass down the header.
1370116898Ssam			 */
1371116898Ssam			hdrlen = WI_CTL_HDRLEN;
1372116898Ssam			datlen = 0;
1373116898Ssam			break;
1374116898Ssam		default:
1375116898Ssam			if_printf(ifp, "received packet of unknown type "
1376116898Ssam				"on port 7\n");
1377116898Ssam			ifp->if_ierrors++;
1378116898Ssam			goto done;
1379116898Ssam		}
1380116898Ssam		break;
1381116898Ssam	case 0:
1382116898Ssam		hdrlen = WI_DATA_HDRLEN;
1383116898Ssam		datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1384116898Ssam		break;
1385116898Ssam	default:
1386116898Ssam		if_printf(ifp, "received packet on invalid "
1387116898Ssam		    "port (wi_status=0x%x)\n", rx_frame->wi_status);
1388116898Ssam		ifp->if_ierrors++;
1389116898Ssam		goto done;
1390116898Ssam	}
1391116898Ssam
1392116898Ssam	if (hdrlen + datlen + 2 > MCLBYTES) {
1393116898Ssam		if_printf(ifp, "oversized packet received "
1394116898Ssam		    "(wi_dat_len=%d, wi_status=0x%x)\n",
1395116898Ssam		    datlen, rx_frame->wi_status);
1396116898Ssam		ifp->if_ierrors++;
1397116898Ssam		goto done;
1398116898Ssam	}
1399116898Ssam
1400116898Ssam	if (wi_read_bap(sc, fid, hdrlen, mtod(m, caddr_t) + hdrlen,
1401116898Ssam	    datlen + 2) == 0) {
1402116898Ssam		m->m_pkthdr.len = m->m_len = hdrlen + datlen;
1403116898Ssam		ifp->if_ipackets++;
1404116898Ssam		BPF_MTAP(ifp, m);	/* Handle BPF listeners. */
1405116898Ssam	} else
1406116898Ssam		ifp->if_ierrors++;
1407116898Ssamdone:
1408116898Ssam	m_freem(m);
1409116898Ssam}
1410116898Ssam
1411116898Ssamstatic void
1412109323Ssamwi_rx_intr(struct wi_softc *sc)
1413109323Ssam{
1414109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1415138571Ssam	struct ifnet *ifp = &sc->sc_if;
1416109323Ssam	struct wi_frame frmhdr;
1417109323Ssam	struct mbuf *m;
1418109323Ssam	struct ieee80211_frame *wh;
1419119150Ssam	struct ieee80211_node *ni;
1420109323Ssam	int fid, len, off, rssi;
1421109323Ssam	u_int8_t dir;
1422109323Ssam	u_int16_t status;
1423109323Ssam	u_int32_t rstamp;
142446611Swpaul
1425109323Ssam	fid = CSR_READ_2(sc, WI_RX_FID);
142646611Swpaul
1427116898Ssam	if (sc->wi_debug.wi_monitor) {
1428116898Ssam		/*
1429116898Ssam		 * If we are in monitor mode just
1430116898Ssam		 * read the data from the device.
1431116898Ssam		 */
1432116898Ssam		wi_rx_monitor(sc, fid);
1433116898Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1434116898Ssam		return;
1435116898Ssam	}
1436116898Ssam
1437109323Ssam	/* First read in the frame header */
1438109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1439109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1440109323Ssam		ifp->if_ierrors++;
1441109323Ssam		DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1442109323Ssam		return;
1443109323Ssam	}
144491695Simp
1445109323Ssam	if (IFF_DUMPPKTS(ifp))
1446109323Ssam		wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal);
144746492Swpaul
1448109323Ssam	/*
1449109323Ssam	 * Drop undecryptable or packets with receive errors here
1450109323Ssam	 */
1451109323Ssam	status = le16toh(frmhdr.wi_status);
1452109323Ssam	if (status & WI_STAT_ERRSTAT) {
1453109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1454109323Ssam		ifp->if_ierrors++;
1455109323Ssam		DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1456109323Ssam		return;
1457109323Ssam	}
1458109323Ssam	rssi = frmhdr.wi_rx_signal;
1459109323Ssam	rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1460109323Ssam	    le16toh(frmhdr.wi_rx_tstamp1);
146146492Swpaul
1462109323Ssam	len = le16toh(frmhdr.wi_dat_len);
1463109323Ssam	off = ALIGN(sizeof(struct ieee80211_frame));
146446563Swpaul
1465117855Ssam	/*
1466117855Ssam	 * Sometimes the PRISM2.x returns bogusly large frames. Except
1467117855Ssam	 * in monitor mode, just throw them away.
1468117855Ssam	 */
1469117855Ssam	if (off + len > MCLBYTES) {
1470117855Ssam		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1471117855Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1472117855Ssam			ifp->if_ierrors++;
1473117855Ssam			DPRINTF(("wi_rx_intr: oversized packet\n"));
1474117855Ssam			return;
1475117855Ssam		} else
1476117855Ssam			len = 0;
1477117855Ssam	}
1478117855Ssam
1479111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
1480109323Ssam	if (m == NULL) {
1481109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1482109323Ssam		ifp->if_ierrors++;
1483109323Ssam		DPRINTF(("wi_rx_intr: MGET failed\n"));
1484109323Ssam		return;
1485109323Ssam	}
1486109323Ssam	if (off + len > MHLEN) {
1487111119Simp		MCLGET(m, M_DONTWAIT);
1488109323Ssam		if ((m->m_flags & M_EXT) == 0) {
1489109323Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1490109323Ssam			m_freem(m);
1491109323Ssam			ifp->if_ierrors++;
1492109323Ssam			DPRINTF(("wi_rx_intr: MCLGET failed\n"));
1493109323Ssam			return;
1494109323Ssam		}
1495109323Ssam	}
149646492Swpaul
1497109323Ssam	m->m_data += off - sizeof(struct ieee80211_frame);
1498109323Ssam	memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1499109323Ssam	wi_read_bap(sc, fid, sizeof(frmhdr),
1500109323Ssam	    m->m_data + sizeof(struct ieee80211_frame), len);
1501109323Ssam	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1502109323Ssam	m->m_pkthdr.rcvif = ifp;
150394405Simp
1504109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
150546492Swpaul
1506138952Ssam	wh = mtod(m, struct ieee80211_frame *);
1507138952Ssam	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1508138952Ssam		/*
1509138952Ssam		 * WEP is decrypted by hardware and the IV
1510138952Ssam		 * is stripped.  Clear WEP bit so we don't
1511138952Ssam		 * try to process it in ieee80211_input.
1512138952Ssam		 * XXX fix for TKIP, et. al.
1513138952Ssam		 */
1514138952Ssam		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1515138952Ssam	}
1516138952Ssam
1517109323Ssam#if NBPFILTER > 0
1518109323Ssam	if (sc->sc_drvbpf) {
1519123927Ssam		/* XXX replace divide by table */
1520123922Ssam		sc->sc_rx_th.wr_rate = frmhdr.wi_rx_rate / 5;
1521127148Sgreen		sc->sc_rx_th.wr_antsignal = frmhdr.wi_rx_signal;
1522127148Sgreen		sc->sc_rx_th.wr_antnoise = frmhdr.wi_rx_silence;
1523123927Ssam		sc->sc_rx_th.wr_flags = 0;
1524123927Ssam		if (frmhdr.wi_status & WI_STAT_PCF)
1525123927Ssam			sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_CFP;
1526138952Ssam		/* XXX IEEE80211_RADIOTAP_F_WEP */
1527123922Ssam		bpf_mtap2(sc->sc_drvbpf,
1528127698Ssam			&sc->sc_rx_th, sc->sc_rx_th_len, m);
152956965Swpaul	}
1530109323Ssam#endif
153156965Swpaul
1532109323Ssam	/* synchronize driver's BSSID with firmware's BSSID */
1533109323Ssam	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1534109323Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1535109323Ssam		wi_sync_bssid(sc, wh->i_addr3);
153646492Swpaul
1537119150Ssam	/*
1538119150Ssam	 * Locate the node for sender, track state, and
1539119150Ssam	 * then pass this node (referenced) up to the 802.11
1540138571Ssam	 * layer for its use.
1541119150Ssam	 */
1542138571Ssam	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *) wh);
1543119150Ssam	/*
1544119150Ssam	 * Send frame up for processing.
1545119150Ssam	 */
1546138571Ssam	ieee80211_input(ic, m, ni, rssi, rstamp);
1547119150Ssam	/*
1548119150Ssam	 * The frame may have caused the node to be marked for
1549119150Ssam	 * reclamation (e.g. in response to a DEAUTH message)
1550119150Ssam	 * so use free_node here instead of unref_node.
1551119150Ssam	 */
1552138571Ssam	ieee80211_free_node(ni);
1553109323Ssam}
155446492Swpaul
1555109323Ssamstatic void
1556109323Ssamwi_tx_ex_intr(struct wi_softc *sc)
1557109323Ssam{
1558138571Ssam	struct ifnet *ifp = &sc->sc_if;
1559109323Ssam	struct wi_frame frmhdr;
1560109323Ssam	int fid;
156146492Swpaul
1562109323Ssam	fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1563109323Ssam	/* Read in the frame header */
1564109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1565109323Ssam		u_int16_t status = le16toh(frmhdr.wi_status);
156646492Swpaul
1567109323Ssam		/*
1568109323Ssam		 * Spontaneous station disconnects appear as xmit
1569109323Ssam		 * errors.  Don't announce them and/or count them
1570109323Ssam		 * as an output error.
1571109323Ssam		 */
1572109323Ssam		if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1573109323Ssam			if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1574109323Ssam				if_printf(ifp, "tx failed");
1575109323Ssam				if (status & WI_TXSTAT_RET_ERR)
1576109323Ssam					printf(", retry limit exceeded");
1577109323Ssam				if (status & WI_TXSTAT_AGED_ERR)
1578109323Ssam					printf(", max transmit lifetime exceeded");
1579109323Ssam				if (status & WI_TXSTAT_DISCONNECT)
1580109323Ssam					printf(", port disconnected");
1581109323Ssam				if (status & WI_TXSTAT_FORM_ERR)
1582109323Ssam					printf(", invalid format (data len %u src %6D)",
1583109323Ssam						le16toh(frmhdr.wi_dat_len),
1584109323Ssam						frmhdr.wi_ehdr.ether_shost, ":");
1585109323Ssam				if (status & ~0xf)
1586109323Ssam					printf(", status=0x%x", status);
1587109323Ssam				printf("\n");
1588109323Ssam			}
1589109323Ssam			ifp->if_oerrors++;
1590109323Ssam		} else {
1591109323Ssam			DPRINTF(("port disconnected\n"));
1592109323Ssam			ifp->if_collisions++;	/* XXX */
1593109323Ssam		}
1594109323Ssam	} else
1595109323Ssam		DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
1596109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1597109323Ssam}
159846492Swpaul
1599109323Ssamstatic void
1600109323Ssamwi_tx_intr(struct wi_softc *sc)
1601109323Ssam{
1602138571Ssam	struct ifnet *ifp = &sc->sc_if;
1603109323Ssam	int fid, cur;
160494405Simp
1605123098Simp	if (sc->wi_gone)
1606123098Simp		return;
1607123098Simp
1608109323Ssam	fid = CSR_READ_2(sc, WI_ALLOC_FID);
1609109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
161046492Swpaul
1611109323Ssam	cur = sc->sc_txcur;
1612109323Ssam	if (sc->sc_txd[cur].d_fid != fid) {
1613109323Ssam		if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
1614109323Ssam		    fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
1615109323Ssam		return;
1616109323Ssam	}
1617109323Ssam	sc->sc_tx_timer = 0;
1618109323Ssam	sc->sc_txd[cur].d_len = 0;
1619112363Simp	sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
1620109323Ssam	if (sc->sc_txd[cur].d_len == 0)
1621109323Ssam		ifp->if_flags &= ~IFF_OACTIVE;
1622109323Ssam	else {
1623109323Ssam		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1624109323Ssam		    0, 0)) {
1625109323Ssam			if_printf(ifp, "xmit failed\n");
1626109323Ssam			sc->sc_txd[cur].d_len = 0;
1627109323Ssam		} else {
1628109323Ssam			sc->sc_tx_timer = 5;
1629109323Ssam			ifp->if_timer = 1;
1630109323Ssam		}
1631109323Ssam	}
163246492Swpaul}
163346492Swpaul
163488546Salfredstatic void
1635109323Ssamwi_info_intr(struct wi_softc *sc)
163694405Simp{
1637109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1638138571Ssam	struct ifnet *ifp = &sc->sc_if;
1639109323Ssam	int i, fid, len, off;
1640109323Ssam	u_int16_t ltbuf[2];
1641109323Ssam	u_int16_t stat;
1642109323Ssam	u_int32_t *ptr;
164394405Simp
1644109323Ssam	fid = CSR_READ_2(sc, WI_INFO_FID);
1645109323Ssam	wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
164694405Simp
1647109323Ssam	switch (le16toh(ltbuf[1])) {
164894405Simp
1649109323Ssam	case WI_INFO_LINK_STAT:
1650109323Ssam		wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1651109323Ssam		DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1652109323Ssam		switch (le16toh(stat)) {
1653109323Ssam		case WI_INFO_LINK_STAT_CONNECTED:
1654109323Ssam			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1655109323Ssam			if (ic->ic_state == IEEE80211_S_RUN &&
1656109323Ssam			    ic->ic_opmode != IEEE80211_M_IBSS)
1657109323Ssam				break;
1658109323Ssam			/* FALLTHROUGH */
1659109323Ssam		case WI_INFO_LINK_STAT_AP_CHG:
1660117812Ssam			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1661109323Ssam			break;
1662109323Ssam		case WI_INFO_LINK_STAT_AP_INR:
1663109323Ssam			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1664109323Ssam			break;
1665109323Ssam		case WI_INFO_LINK_STAT_AP_OOR:
1666109323Ssam			if (sc->sc_firmware_type == WI_SYMBOL &&
1667109323Ssam			    sc->sc_scan_timer > 0) {
1668109323Ssam				if (wi_cmd(sc, WI_CMD_INQUIRE,
1669109323Ssam				    WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
1670109323Ssam					sc->sc_scan_timer = 0;
1671109323Ssam				break;
1672109323Ssam			}
1673109323Ssam			if (ic->ic_opmode == IEEE80211_M_STA)
1674109323Ssam				sc->sc_flags |= WI_FLAGS_OUTRANGE;
1675109323Ssam			break;
1676109323Ssam		case WI_INFO_LINK_STAT_DISCONNECTED:
1677109323Ssam		case WI_INFO_LINK_STAT_ASSOC_FAILED:
1678109323Ssam			if (ic->ic_opmode == IEEE80211_M_STA)
1679117812Ssam				ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1680109323Ssam			break;
1681109323Ssam		}
1682109323Ssam		break;
168394405Simp
1684109323Ssam	case WI_INFO_COUNTERS:
1685109323Ssam		/* some card versions have a larger stats structure */
1686109323Ssam		len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1687109323Ssam		ptr = (u_int32_t *)&sc->sc_stats;
1688109323Ssam		off = sizeof(ltbuf);
1689109323Ssam		for (i = 0; i < len; i++, off += 2, ptr++) {
1690109323Ssam			wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1691109323Ssam#ifdef WI_HERMES_STATS_WAR
1692109323Ssam			if (stat & 0xf000)
1693109323Ssam				stat = ~stat;
1694109323Ssam#endif
1695109323Ssam			*ptr += stat;
1696109323Ssam		}
1697109323Ssam		ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1698109323Ssam		    sc->sc_stats.wi_tx_multi_retries +
1699109323Ssam		    sc->sc_stats.wi_tx_retry_limit;
1700109323Ssam		break;
1701109323Ssam
1702109323Ssam	case WI_INFO_SCAN_RESULTS:
1703109323Ssam	case WI_INFO_HOST_SCAN_RESULTS:
1704109323Ssam		wi_scan_result(sc, fid, le16toh(ltbuf[0]));
1705109323Ssam		break;
1706109323Ssam
1707109323Ssam	default:
1708109323Ssam		DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1709109323Ssam		    le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1710109323Ssam		break;
171194405Simp	}
1712109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1713109323Ssam}
171494405Simp
1715109323Ssamstatic int
1716109323Ssamwi_write_multi(struct wi_softc *sc)
1717109323Ssam{
1718138571Ssam	struct ifnet *ifp = &sc->sc_if;
1719109323Ssam	int n;
1720109323Ssam	struct ifmultiaddr *ifma;
1721109323Ssam	struct wi_mcast mlist;
172294472Simp
1723109323Ssam	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1724109323Ssamallmulti:
1725109323Ssam		memset(&mlist, 0, sizeof(mlist));
1726109323Ssam		return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1727109323Ssam		    sizeof(mlist));
172894405Simp	}
172994405Simp
1730109323Ssam	n = 0;
1731109323Ssam#if __FreeBSD_version < 500000
1732109323Ssam	LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1733109323Ssam#else
1734109323Ssam	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1735109323Ssam#endif
1736109323Ssam		if (ifma->ifma_addr->sa_family != AF_LINK)
1737109323Ssam			continue;
1738109323Ssam		if (n >= 16)
1739109323Ssam			goto allmulti;
1740109323Ssam		IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1741109323Ssam		    (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1742109323Ssam		n++;
174394405Simp	}
1744109323Ssam	return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1745109323Ssam	    IEEE80211_ADDR_LEN * n);
174694405Simp}
174794405Simp
174894405Simpstatic void
1749109323Ssamwi_read_nicid(struct wi_softc *sc)
175046492Swpaul{
1751109323Ssam	struct wi_card_ident *id;
1752109323Ssam	char *p;
1753109323Ssam	int len;
1754109323Ssam	u_int16_t ver[4];
175546492Swpaul
1756109323Ssam	/* getting chip identity */
1757109323Ssam	memset(ver, 0, sizeof(ver));
1758109323Ssam	len = sizeof(ver);
1759109323Ssam	wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1760109323Ssam	device_printf(sc->sc_dev, "using ");
176146492Swpaul
1762109323Ssam	sc->sc_firmware_type = WI_NOTYPE;
1763109323Ssam	for (id = wi_card_ident; id->card_name != NULL; id++) {
1764109323Ssam		if (le16toh(ver[0]) == id->card_id) {
1765109323Ssam			printf("%s", id->card_name);
1766109323Ssam			sc->sc_firmware_type = id->firm_type;
1767109323Ssam			break;
1768109323Ssam		}
176967092Swpaul	}
1770109323Ssam	if (sc->sc_firmware_type == WI_NOTYPE) {
1771109323Ssam		if (le16toh(ver[0]) & 0x8000) {
1772109323Ssam			printf("Unknown PRISM2 chip");
1773109323Ssam			sc->sc_firmware_type = WI_INTERSIL;
1774109323Ssam		} else {
1775109323Ssam			printf("Unknown Lucent chip");
1776109323Ssam			sc->sc_firmware_type = WI_LUCENT;
1777109323Ssam		}
177867092Swpaul	}
177946492Swpaul
1780109323Ssam	/* get primary firmware version (Only Prism chips) */
1781109323Ssam	if (sc->sc_firmware_type != WI_LUCENT) {
1782109323Ssam		memset(ver, 0, sizeof(ver));
1783109323Ssam		len = sizeof(ver);
1784109323Ssam		wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1785109323Ssam		sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1786109323Ssam		    le16toh(ver[3]) * 100 + le16toh(ver[1]);
178767092Swpaul	}
178846492Swpaul
1789109323Ssam	/* get station firmware version */
1790109323Ssam	memset(ver, 0, sizeof(ver));
1791109323Ssam	len = sizeof(ver);
1792109323Ssam	wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1793109323Ssam	sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1794109323Ssam	    le16toh(ver[3]) * 100 + le16toh(ver[1]);
1795109323Ssam	if (sc->sc_firmware_type == WI_INTERSIL &&
1796109323Ssam	    (sc->sc_sta_firmware_ver == 10102 ||
1797109323Ssam	     sc->sc_sta_firmware_ver == 20102)) {
1798109323Ssam		char ident[12];
1799109323Ssam		memset(ident, 0, sizeof(ident));
1800109323Ssam		len = sizeof(ident);
1801109323Ssam		/* value should be the format like "V2.00-11" */
1802109323Ssam		if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1803109323Ssam		    *(p = (char *)ident) >= 'A' &&
1804109323Ssam		    p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1805109323Ssam			sc->sc_firmware_type = WI_SYMBOL;
1806109323Ssam			sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1807109323Ssam			    (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1808109323Ssam			    (p[6] - '0') * 10 + (p[7] - '0');
180994405Simp		}
181094405Simp	}
1811109323Ssam	printf("\n");
1812109323Ssam	device_printf(sc->sc_dev, "%s Firmware: ",
1813109323Ssam	     sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
1814109323Ssam	    (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
1815109323Ssam	if (sc->sc_firmware_type != WI_LUCENT)	/* XXX */
1816109323Ssam		printf("Primary (%u.%u.%u), ",
1817109323Ssam		    sc->sc_pri_firmware_ver / 10000,
1818109323Ssam		    (sc->sc_pri_firmware_ver % 10000) / 100,
1819109323Ssam		    sc->sc_pri_firmware_ver % 100);
1820109323Ssam	printf("Station (%u.%u.%u)\n",
1821109323Ssam	    sc->sc_sta_firmware_ver / 10000,
1822109323Ssam	    (sc->sc_sta_firmware_ver % 10000) / 100,
1823109323Ssam	    sc->sc_sta_firmware_ver % 100);
1824109323Ssam}
182546492Swpaul
1826109323Ssamstatic int
1827109323Ssamwi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1828109323Ssam{
1829109323Ssam	struct wi_ssid ssid;
183046492Swpaul
1831109323Ssam	if (buflen > IEEE80211_NWID_LEN)
1832109323Ssam		return ENOBUFS;
1833109323Ssam	memset(&ssid, 0, sizeof(ssid));
1834109323Ssam	ssid.wi_len = htole16(buflen);
1835109323Ssam	memcpy(ssid.wi_ssid, buf, buflen);
1836109323Ssam	return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1837109323Ssam}
183846492Swpaul
1839109323Ssamstatic int
1840109323Ssamwi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
1841109323Ssam{
1842109323Ssam	struct wi_softc *sc = ifp->if_softc;
1843109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1844109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
1845109323Ssam	struct wi_req wreq;
1846116898Ssam	struct wi_scan_res *res;
1847122015Sgreen	size_t reslen;
1848116898Ssam	int len, n, error, mif, val, off, i;
184946492Swpaul
1850109323Ssam	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1851109323Ssam	if (error)
1852109323Ssam		return error;
1853109323Ssam	len = (wreq.wi_len - 1) * 2;
1854109323Ssam	if (len < sizeof(u_int16_t))
1855109323Ssam		return ENOSPC;
1856109323Ssam	if (len > sizeof(wreq.wi_val))
1857109323Ssam		len = sizeof(wreq.wi_val);
185846492Swpaul
1859109323Ssam	switch (wreq.wi_type) {
186046492Swpaul
1861109323Ssam	case WI_RID_IFACE_STATS:
1862109323Ssam		memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
1863109323Ssam		if (len < sizeof(sc->sc_stats))
1864109323Ssam			error = ENOSPC;
1865109323Ssam		else
1866109323Ssam			len = sizeof(sc->sc_stats);
1867109323Ssam		break;
186846492Swpaul
1869109323Ssam	case WI_RID_ENCRYPTION:
1870109323Ssam	case WI_RID_TX_CRYPT_KEY:
1871109323Ssam	case WI_RID_DEFLT_CRYPT_KEYS:
1872109323Ssam	case WI_RID_TX_RATE:
1873138571Ssam		return ieee80211_cfgget(ic, cmd, data);
187446492Swpaul
1875109323Ssam	case WI_RID_MICROWAVE_OVEN:
1876109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
1877109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1878109323Ssam			    &len);
1879109323Ssam			break;
1880109323Ssam		}
1881109323Ssam		wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
1882109323Ssam		len = sizeof(u_int16_t);
1883109323Ssam		break;
188446492Swpaul
1885109323Ssam	case WI_RID_DBM_ADJUST:
1886109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
1887109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1888109323Ssam			    &len);
1889109323Ssam			break;
1890109323Ssam		}
1891119784Ssam		wreq.wi_val[0] = htole16(sc->sc_dbm_offset);
1892109323Ssam		len = sizeof(u_int16_t);
1893109323Ssam		break;
189446492Swpaul
1895109323Ssam	case WI_RID_ROAMING_MODE:
1896109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
1897109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1898109323Ssam			    &len);
1899109323Ssam			break;
1900109323Ssam		}
1901109323Ssam		wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
1902109323Ssam		len = sizeof(u_int16_t);
1903109323Ssam		break;
190446492Swpaul
1905109323Ssam	case WI_RID_SYSTEM_SCALE:
1906109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
1907109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1908109323Ssam			    &len);
1909109323Ssam			break;
1910109323Ssam		}
1911109323Ssam		wreq.wi_val[0] = htole16(sc->sc_system_scale);
1912109323Ssam		len = sizeof(u_int16_t);
1913109323Ssam		break;
191446492Swpaul
1915109323Ssam	case WI_RID_FRAG_THRESH:
1916109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
1917109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1918109323Ssam			    &len);
1919109323Ssam			break;
1920109323Ssam		}
1921116951Ssam		wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
1922109323Ssam		len = sizeof(u_int16_t);
1923109323Ssam		break;
192446492Swpaul
1925109323Ssam	case WI_RID_READ_APS:
1926109323Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1927138571Ssam			return ieee80211_cfgget(ic, cmd, data);
1928109323Ssam		if (sc->sc_scan_timer > 0) {
1929109323Ssam			error = EINPROGRESS;
1930109323Ssam			break;
1931109323Ssam		}
1932109323Ssam		n = sc->sc_naps;
1933109323Ssam		if (len < sizeof(n)) {
1934109323Ssam			error = ENOSPC;
1935109323Ssam			break;
1936109323Ssam		}
1937109323Ssam		if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
1938109323Ssam			n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
1939109323Ssam		len = sizeof(n) + sizeof(struct wi_apinfo) * n;
1940109323Ssam		memcpy(wreq.wi_val, &n, sizeof(n));
1941109323Ssam		memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
1942109323Ssam		    sizeof(struct wi_apinfo) * n);
1943109323Ssam		break;
194446492Swpaul
1945109323Ssam	case WI_RID_PRISM2:
1946109323Ssam		wreq.wi_val[0] = sc->sc_firmware_type != WI_LUCENT;
1947109323Ssam		len = sizeof(u_int16_t);
1948109323Ssam		break;
194946492Swpaul
1950109323Ssam	case WI_RID_MIF:
1951109323Ssam		mif = wreq.wi_val[0];
1952109323Ssam		error = wi_cmd(sc, WI_CMD_READMIF, mif, 0, 0);
1953109323Ssam		val = CSR_READ_2(sc, WI_RESP0);
1954109323Ssam		wreq.wi_val[0] = val;
1955109323Ssam		len = sizeof(u_int16_t);
1956109323Ssam		break;
195746492Swpaul
1958109323Ssam	case WI_RID_ZERO_CACHE:
1959109323Ssam	case WI_RID_PROCFRAME:		/* ignore for compatibility */
1960109323Ssam		/* XXX ??? */
1961109323Ssam		break;
196246492Swpaul
1963109323Ssam	case WI_RID_READ_CACHE:
1964138571Ssam		return ieee80211_cfgget(ic, cmd, data);
196546492Swpaul
1966116898Ssam	case WI_RID_SCAN_RES:		/* compatibility interface */
1967116898Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1968138571Ssam			return ieee80211_cfgget(ic, cmd, data);
1969116898Ssam		if (sc->sc_scan_timer > 0) {
1970116898Ssam			error = EINPROGRESS;
1971116898Ssam			break;
1972116898Ssam		}
1973116898Ssam		n = sc->sc_naps;
1974122015Sgreen		if (sc->sc_firmware_type == WI_LUCENT) {
1975122015Sgreen			off = 0;
1976122015Sgreen			reslen = WI_WAVELAN_RES_SIZE;
1977122015Sgreen		} else {
1978122015Sgreen			off = sizeof(struct wi_scan_p2_hdr);
1979122015Sgreen			reslen = WI_PRISM2_RES_SIZE;
1980122015Sgreen		}
1981122015Sgreen		if (len < off + reslen * n)
1982122015Sgreen			n = (len - off) / reslen;
1983122015Sgreen		len = off + reslen * n;
1984116898Ssam		if (off != 0) {
1985116898Ssam			struct wi_scan_p2_hdr *p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1986116898Ssam			/*
1987116898Ssam			 * Prepend Prism-specific header.
1988116898Ssam			 */
1989116898Ssam			if (len < sizeof(struct wi_scan_p2_hdr)) {
1990116898Ssam				error = ENOSPC;
1991116898Ssam				break;
1992116898Ssam			}
1993116898Ssam			p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1994116898Ssam			p2->wi_rsvd = 0;
1995116898Ssam			p2->wi_reason = n;	/* XXX */
1996116898Ssam		}
1997122015Sgreen		for (i = 0; i < n; i++, off += reslen) {
1998116898Ssam			const struct wi_apinfo *ap = &sc->sc_aps[i];
1999116898Ssam
2000116898Ssam			res = (struct wi_scan_res *)((char *)wreq.wi_val + off);
2001116898Ssam			res->wi_chan = ap->channel;
2002116898Ssam			res->wi_noise = ap->noise;
2003116898Ssam			res->wi_signal = ap->signal;
2004116898Ssam			IEEE80211_ADDR_COPY(res->wi_bssid, ap->bssid);
2005116898Ssam			res->wi_interval = ap->interval;
2006116898Ssam			res->wi_capinfo = ap->capinfo;
2007116898Ssam			res->wi_ssid_len = ap->namelen;
2008116898Ssam			memcpy(res->wi_ssid, ap->name,
2009116898Ssam				IEEE80211_NWID_LEN);
2010116898Ssam			if (sc->sc_firmware_type != WI_LUCENT) {
2011116898Ssam				/* XXX not saved from Prism cards */
2012116898Ssam				memset(res->wi_srates, 0,
2013116898Ssam					sizeof(res->wi_srates));
2014116898Ssam				res->wi_rate = ap->rate;
2015116898Ssam				res->wi_rsvd = 0;
2016122015Sgreen			}
2017116898Ssam		}
2018116898Ssam		break;
2019116898Ssam
2020109323Ssam	default:
2021109323Ssam		if (sc->sc_enabled) {
2022109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2023109323Ssam			    &len);
2024109323Ssam			break;
2025109323Ssam		}
2026109323Ssam		switch (wreq.wi_type) {
2027109323Ssam		case WI_RID_MAX_DATALEN:
2028109323Ssam			wreq.wi_val[0] = htole16(sc->sc_max_datalen);
2029109323Ssam			len = sizeof(u_int16_t);
2030109323Ssam			break;
2031109323Ssam		case WI_RID_RTS_THRESH:
2032116951Ssam			wreq.wi_val[0] = htole16(ic->ic_rtsthreshold);
2033109323Ssam			len = sizeof(u_int16_t);
2034109323Ssam			break;
2035109323Ssam		case WI_RID_CNFAUTHMODE:
2036109323Ssam			wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
2037109323Ssam			len = sizeof(u_int16_t);
2038109323Ssam			break;
2039109323Ssam		case WI_RID_NODENAME:
2040109323Ssam			if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
2041109323Ssam				error = ENOSPC;
2042109323Ssam				break;
2043109323Ssam			}
2044109323Ssam			len = sc->sc_nodelen + sizeof(u_int16_t);
2045109323Ssam			wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
2046109323Ssam			memcpy(&wreq.wi_val[1], sc->sc_nodename,
2047109323Ssam			    sc->sc_nodelen);
2048109323Ssam			break;
2049109323Ssam		default:
2050138571Ssam			return ieee80211_cfgget(ic, cmd, data);
2051109323Ssam		}
2052109323Ssam		break;
205346492Swpaul	}
2054109323Ssam	if (error)
2055109323Ssam		return error;
2056109323Ssam	wreq.wi_len = (len + 1) / 2 + 1;
2057109323Ssam	return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
205846492Swpaul}
205946492Swpaul
2060109323Ssamstatic int
2061109323Ssamwi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
206246492Swpaul{
2063109323Ssam	struct wi_softc *sc = ifp->if_softc;
2064109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2065109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
2066109323Ssam	struct wi_req wreq;
2067109323Ssam	struct mbuf *m;
2068109323Ssam	int i, len, error, mif, val;
2069116951Ssam	struct ieee80211_rateset *rs;
2070138571Ssam	WI_LOCK_DECL();
207146492Swpaul
2072109323Ssam	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2073109323Ssam	if (error)
2074109323Ssam		return error;
2075116898Ssam	len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0;
2076109323Ssam	switch (wreq.wi_type) {
2077109323Ssam	case WI_RID_DBM_ADJUST:
2078109323Ssam		return ENODEV;
207967092Swpaul
2080109323Ssam	case WI_RID_NODENAME:
2081109323Ssam		if (le16toh(wreq.wi_val[0]) * 2 > len ||
2082109323Ssam		    le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
2083109323Ssam			error = ENOSPC;
2084109323Ssam			break;
2085109323Ssam		}
2086138571Ssam		WI_LOCK(sc);
2087138571Ssam		if (sc->sc_enabled)
2088109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2089109323Ssam			    len);
2090138571Ssam		if (error == 0) {
2091138571Ssam			sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
2092138571Ssam			memcpy(sc->sc_nodename, &wreq.wi_val[1],
2093138571Ssam				sc->sc_nodelen);
2094109323Ssam		}
2095138571Ssam		WI_UNLOCK(sc);
2096109323Ssam		break;
209746492Swpaul
2098109323Ssam	case WI_RID_MICROWAVE_OVEN:
2099109323Ssam	case WI_RID_ROAMING_MODE:
2100109323Ssam	case WI_RID_SYSTEM_SCALE:
2101109323Ssam	case WI_RID_FRAG_THRESH:
2102138571Ssam		/* XXX unlocked reads */
2103109323Ssam		if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
2104109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
2105109323Ssam			break;
2106109323Ssam		if (wreq.wi_type == WI_RID_ROAMING_MODE &&
2107109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
2108109323Ssam			break;
2109109323Ssam		if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
2110109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
2111109323Ssam			break;
2112109323Ssam		if (wreq.wi_type == WI_RID_FRAG_THRESH &&
2113109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
2114109323Ssam			break;
2115109323Ssam		/* FALLTHROUGH */
2116109323Ssam	case WI_RID_RTS_THRESH:
2117109323Ssam	case WI_RID_CNFAUTHMODE:
2118109323Ssam	case WI_RID_MAX_DATALEN:
2119138571Ssam		WI_LOCK(sc);
2120109323Ssam		if (sc->sc_enabled) {
2121109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2122109323Ssam			    sizeof(u_int16_t));
2123138571Ssam			if (error != 0) {
2124138571Ssam				WI_UNLOCK(sc);
2125109323Ssam				break;
2126138571Ssam			}
2127109323Ssam		}
2128109323Ssam		switch (wreq.wi_type) {
2129109323Ssam		case WI_RID_FRAG_THRESH:
2130116951Ssam			ic->ic_fragthreshold = le16toh(wreq.wi_val[0]);
2131109323Ssam			break;
2132109323Ssam		case WI_RID_RTS_THRESH:
2133116951Ssam			ic->ic_rtsthreshold = le16toh(wreq.wi_val[0]);
2134109323Ssam			break;
2135109323Ssam		case WI_RID_MICROWAVE_OVEN:
2136109323Ssam			sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
2137109323Ssam			break;
2138109323Ssam		case WI_RID_ROAMING_MODE:
2139109323Ssam			sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
2140109323Ssam			break;
2141109323Ssam		case WI_RID_SYSTEM_SCALE:
2142109323Ssam			sc->sc_system_scale = le16toh(wreq.wi_val[0]);
2143109323Ssam			break;
2144109323Ssam		case WI_RID_CNFAUTHMODE:
2145109323Ssam			sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
2146109323Ssam			break;
2147109323Ssam		case WI_RID_MAX_DATALEN:
2148109323Ssam			sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
2149109323Ssam			break;
2150109323Ssam		}
2151138571Ssam		WI_UNLOCK(sc);
2152109323Ssam		break;
215394405Simp
2154109323Ssam	case WI_RID_TX_RATE:
2155138571Ssam		WI_LOCK(sc);
2156109323Ssam		switch (le16toh(wreq.wi_val[0])) {
2157109323Ssam		case 3:
2158109323Ssam			ic->ic_fixed_rate = -1;
2159109323Ssam			break;
2160109323Ssam		default:
2161116951Ssam			rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
2162116951Ssam			for (i = 0; i < rs->rs_nrates; i++) {
2163116951Ssam				if ((rs->rs_rates[i] & IEEE80211_RATE_VAL)
2164109323Ssam				    / 2 == le16toh(wreq.wi_val[0]))
2165109323Ssam					break;
2166109323Ssam			}
2167138571Ssam			if (i == rs->rs_nrates) {
2168138571Ssam				WI_UNLOCK(sc);
2169109323Ssam				return EINVAL;
2170138571Ssam			}
2171109323Ssam			ic->ic_fixed_rate = i;
2172109323Ssam		}
2173109323Ssam		if (sc->sc_enabled)
2174109323Ssam			error = wi_write_txrate(sc);
2175138571Ssam		WI_UNLOCK(sc);
2176109323Ssam		break;
217746492Swpaul
2178109323Ssam	case WI_RID_SCAN_APS:
2179138571Ssam		WI_LOCK(sc);
2180109323Ssam		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2181116898Ssam			error = wi_scan_ap(sc, 0x3fff, 0x000f);
2182138571Ssam		WI_UNLOCK(sc);
2183109323Ssam		break;
218446492Swpaul
2185116898Ssam	case WI_RID_SCAN_REQ:		/* compatibility interface */
2186138571Ssam		WI_LOCK(sc);
2187116898Ssam		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2188116898Ssam			error = wi_scan_ap(sc, wreq.wi_val[0], wreq.wi_val[1]);
2189138571Ssam		WI_UNLOCK(sc);
2190116898Ssam		break;
2191116898Ssam
2192109323Ssam	case WI_RID_MGMT_XMIT:
2193138571Ssam		WI_LOCK(sc);
2194138571Ssam		if (!sc->sc_enabled)
2195109323Ssam			error = ENETDOWN;
2196138571Ssam		else if (ic->ic_mgtq.ifq_len > 5)
2197109323Ssam			error = EAGAIN;
2198138571Ssam		else {
2199138571Ssam			/* NB: m_devget uses M_DONTWAIT so can hold the lock */
2200138571Ssam			/* XXX wi_len looks in u_int8_t, not in u_int16_t */
2201138571Ssam			m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0,
2202138571Ssam				ifp, NULL);
2203138571Ssam			if (m != NULL)
2204138571Ssam				IF_ENQUEUE(&ic->ic_mgtq, m);
2205138571Ssam			else
2206138571Ssam				error = ENOMEM;
2207109323Ssam		}
2208138571Ssam		WI_UNLOCK(sc);
2209109323Ssam		break;
221046492Swpaul
2211109323Ssam	case WI_RID_MIF:
2212109323Ssam		mif = wreq.wi_val[0];
2213109323Ssam		val = wreq.wi_val[1];
2214138571Ssam		WI_LOCK(sc);
2215109323Ssam		error = wi_cmd(sc, WI_CMD_WRITEMIF, mif, val, 0);
2216138571Ssam		WI_UNLOCK(sc);
2217109323Ssam		break;
221846492Swpaul
2219109323Ssam	case WI_RID_PROCFRAME:		/* ignore for compatibility */
2220109323Ssam		break;
2221109323Ssam
2222116898Ssam	case WI_RID_OWN_SSID:
2223116898Ssam		if (le16toh(wreq.wi_val[0]) * 2 > len ||
2224116898Ssam		    le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) {
2225116898Ssam			error = ENOSPC;
2226116898Ssam			break;
2227116898Ssam		}
2228138571Ssam		WI_LOCK(sc);
2229116898Ssam		memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
2230116898Ssam		ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2;
2231116898Ssam		memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen);
2232138571Ssam		if (sc->sc_enabled)
2233138571Ssam			wi_init(sc);	/* XXX no error return */
2234138571Ssam		WI_UNLOCK(sc);
2235116898Ssam		break;
2236116898Ssam
2237109323Ssam	default:
2238138571Ssam		WI_LOCK(sc);
2239138571Ssam		if (sc->sc_enabled)
2240109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2241109323Ssam			    len);
2242138571Ssam		if (error == 0) {
2243138571Ssam			/* XXX ieee80211_cfgset does a copyin */
2244138571Ssam			error = ieee80211_cfgset(ic, cmd, data);
2245138571Ssam			if (error == ENETRESET) {
2246138571Ssam				if (sc->sc_enabled)
2247138571Ssam					wi_init(sc);
2248138571Ssam				error = 0;
2249138571Ssam			}
2250109323Ssam		}
2251138571Ssam		WI_UNLOCK(sc);
2252109323Ssam		break;
2253109323Ssam	}
2254109323Ssam	return error;
225546492Swpaul}
225646492Swpaul
2257109323Ssamstatic int
2258109323Ssamwi_write_txrate(struct wi_softc *sc)
225946492Swpaul{
2260109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2261109323Ssam	int i;
2262109323Ssam	u_int16_t rate;
226346492Swpaul
2264109323Ssam	if (ic->ic_fixed_rate < 0)
2265109323Ssam		rate = 0;	/* auto */
2266109323Ssam	else
2267116951Ssam		rate = (ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ic->ic_fixed_rate] &
2268109323Ssam		    IEEE80211_RATE_VAL) / 2;
226946492Swpaul
2270109323Ssam	/* rate: 0, 1, 2, 5, 11 */
227146492Swpaul
2272109323Ssam	switch (sc->sc_firmware_type) {
2273109323Ssam	case WI_LUCENT:
2274112501Simp		switch (rate) {
2275112501Simp		case 0:			/* auto == 11mbps auto */
2276112501Simp			rate = 3;
2277112501Simp			break;
2278112501Simp		/* case 1, 2 map to 1, 2*/
2279112501Simp		case 5:			/* 5.5Mbps -> 4 */
2280112501Simp			rate = 4;
2281112501Simp			break;
2282112501Simp		case 11:		/* 11mbps -> 5 */
2283112501Simp			rate = 5;
2284112501Simp			break;
2285112501Simp		default:
2286112501Simp			break;
2287112501Simp		}
2288109323Ssam		break;
2289109323Ssam	default:
2290109323Ssam		/* Choose a bit according to this table.
2291109323Ssam		 *
2292109323Ssam		 * bit | data rate
2293109323Ssam		 * ----+-------------------
2294109323Ssam		 * 0   | 1Mbps
2295109323Ssam		 * 1   | 2Mbps
2296109323Ssam		 * 2   | 5.5Mbps
2297109323Ssam		 * 3   | 11Mbps
2298109323Ssam		 */
2299109323Ssam		for (i = 8; i > 0; i >>= 1) {
2300109323Ssam			if (rate >= i)
2301109323Ssam				break;
2302109323Ssam		}
2303109323Ssam		if (i == 0)
2304109323Ssam			rate = 0xf;	/* auto */
2305109323Ssam		else
2306109323Ssam			rate = i;
2307109323Ssam		break;
2308109323Ssam	}
2309109323Ssam	return wi_write_val(sc, WI_RID_TX_RATE, rate);
2310109323Ssam}
231146492Swpaul
2312109323Ssamstatic int
2313109323Ssamwi_write_wep(struct wi_softc *sc)
2314109323Ssam{
2315109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2316109323Ssam	int error = 0;
2317109323Ssam	int i, keylen;
2318109323Ssam	u_int16_t val;
2319109323Ssam	struct wi_key wkey[IEEE80211_WEP_NKID];
232046492Swpaul
2321109323Ssam	switch (sc->sc_firmware_type) {
2322109323Ssam	case WI_LUCENT:
2323138571Ssam		val = (ic->ic_flags & IEEE80211_F_PRIVACY) ? 1 : 0;
2324109323Ssam		error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
2325109323Ssam		if (error)
2326109323Ssam			break;
2327138988Smdodd		if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0)
2328138988Smdodd			break;
2329138988Smdodd		error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_def_txkey);
2330138988Smdodd		if (error)
2331138988Smdodd			break;
2332138988Smdodd		memset(wkey, 0, sizeof(wkey));
2333138988Smdodd		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2334138988Smdodd			keylen = ic->ic_nw_keys[i].wk_keylen;
2335138988Smdodd			wkey[i].wi_keylen = htole16(keylen);
2336138988Smdodd			memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
2337138988Smdodd			    keylen);
2338109323Ssam		}
2339138988Smdodd		error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
2340138988Smdodd		    wkey, sizeof(wkey));
2341109323Ssam		break;
2342109323Ssam
2343109323Ssam	case WI_INTERSIL:
2344109323Ssam	case WI_SYMBOL:
2345138571Ssam		if (ic->ic_flags & IEEE80211_F_PRIVACY) {
2346109323Ssam			/*
2347109323Ssam			 * ONLY HWB3163 EVAL-CARD Firmware version
2348109323Ssam			 * less than 0.8 variant2
2349109323Ssam			 *
2350109323Ssam			 *   If promiscuous mode disable, Prism2 chip
2351109323Ssam			 *  does not work with WEP .
2352109323Ssam			 * It is under investigation for details.
2353109323Ssam			 * (ichiro@netbsd.org)
2354109323Ssam			 */
2355109323Ssam			if (sc->sc_firmware_type == WI_INTERSIL &&
2356109323Ssam			    sc->sc_sta_firmware_ver < 802 ) {
2357109323Ssam				/* firm ver < 0.8 variant 2 */
2358109323Ssam				wi_write_val(sc, WI_RID_PROMISC, 1);
2359109323Ssam			}
2360109323Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE,
2361109323Ssam			    sc->sc_cnfauthmode);
2362109323Ssam			val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
2363109323Ssam			/*
2364109323Ssam			 * Encryption firmware has a bug for HostAP mode.
2365109323Ssam			 */
2366109323Ssam			if (sc->sc_firmware_type == WI_INTERSIL &&
2367109323Ssam			    ic->ic_opmode == IEEE80211_M_HOSTAP)
2368109323Ssam				val |= HOST_ENCRYPT;
2369109323Ssam		} else {
2370109323Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE,
2371109323Ssam			    IEEE80211_AUTH_OPEN);
2372109323Ssam			val = HOST_ENCRYPT | HOST_DECRYPT;
2373109323Ssam		}
2374109323Ssam		error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
2375109323Ssam		if (error)
2376109323Ssam			break;
2377138988Smdodd		if ((val & PRIVACY_INVOKED) == 0)
2378138988Smdodd			break;
2379138988Smdodd		error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
2380138988Smdodd		    ic->ic_def_txkey);
2381138988Smdodd		if (error)
2382138988Smdodd			break;
2383138988Smdodd		if (val & HOST_DECRYPT)
2384138988Smdodd			break;
2385138988Smdodd		/*
2386138988Smdodd		 * It seems that the firmware accept 104bit key only if
2387138988Smdodd		 * all the keys have 104bit length.  We get the length of
2388138988Smdodd		 * the transmit key and use it for all other keys.
2389138988Smdodd		 * Perhaps we should use software WEP for such situation.
2390138988Smdodd		 */
2391138988Smdodd		if (ic->ic_def_txkey != IEEE80211_KEYIX_NONE)
2392138988Smdodd			keylen = ic->ic_nw_keys[ic->ic_def_txkey].wk_keylen;
2393138988Smdodd		else	/* XXX should not hapen */
2394138988Smdodd			keylen = IEEE80211_WEP_KEYLEN;
2395138988Smdodd		if (keylen > IEEE80211_WEP_KEYLEN)
2396138988Smdodd			keylen = 13;	/* 104bit keys */
2397138988Smdodd		else
2398138988Smdodd			keylen = IEEE80211_WEP_KEYLEN;
2399138988Smdodd		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2400138988Smdodd			error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
2401138988Smdodd			    ic->ic_nw_keys[i].wk_key, keylen);
2402109323Ssam			if (error)
2403109323Ssam				break;
2404109323Ssam		}
2405109323Ssam		break;
2406109323Ssam	}
2407109323Ssam	return error;
240846492Swpaul}
240946492Swpaul
2410109323Ssamstatic int
2411109323Ssamwi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
241246492Swpaul{
2413109323Ssam	int			i, s = 0;
2414109323Ssam	static volatile int count  = 0;
2415109323Ssam
2416123098Simp	if (sc->wi_gone)
2417123098Simp		return (ENODEV);
2418123098Simp
2419109323Ssam	if (count > 0)
2420109323Ssam		panic("Hey partner, hold on there!");
2421109323Ssam	count++;
242253702Swpaul
2423109323Ssam	/* wait for the busy bit to clear */
2424123339Simp	for (i = sc->wi_cmd_count; i > 0; i--) {	/* 500ms */
2425116206Simp		if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
2426109323Ssam			break;
2427123098Simp		DELAY(1*1000);	/* 1ms */
2428109323Ssam	}
2429109323Ssam	if (i == 0) {
2430109323Ssam		device_printf(sc->sc_dev, "wi_cmd: busy bit won't clear.\n" );
2431123098Simp		sc->wi_gone = 1;
2432109323Ssam		count--;
2433109323Ssam		return(ETIMEDOUT);
2434109323Ssam	}
243590580Sbrooks
2436109323Ssam	CSR_WRITE_2(sc, WI_PARAM0, val0);
2437109323Ssam	CSR_WRITE_2(sc, WI_PARAM1, val1);
2438109323Ssam	CSR_WRITE_2(sc, WI_PARAM2, val2);
2439109323Ssam	CSR_WRITE_2(sc, WI_COMMAND, cmd);
244090580Sbrooks
2441109323Ssam	if (cmd == WI_CMD_INI) {
2442109323Ssam		/* XXX: should sleep here. */
2443116206Simp		DELAY(100*1000);		/* 100ms delay for init */
2444109323Ssam	}
2445109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
2446109323Ssam		/*
2447109323Ssam		 * Wait for 'command complete' bit to be
2448109323Ssam		 * set in the event status register.
2449109323Ssam		 */
2450109323Ssam		s = CSR_READ_2(sc, WI_EVENT_STAT);
2451109323Ssam		if (s & WI_EV_CMD) {
2452109323Ssam			/* Ack the event and read result code. */
2453109323Ssam			s = CSR_READ_2(sc, WI_STATUS);
2454109323Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
2455109323Ssam			if (s & WI_STAT_CMD_RESULT) {
2456109323Ssam				count--;
2457109323Ssam				return(EIO);
2458109323Ssam			}
2459109323Ssam			break;
246090580Sbrooks		}
2461109323Ssam		DELAY(WI_DELAY);
2462109323Ssam	}
246390580Sbrooks
2464109323Ssam	count--;
2465109323Ssam	if (i == WI_TIMEOUT) {
2466109323Ssam		device_printf(sc->sc_dev,
2467109323Ssam		    "timeout in wi_cmd 0x%04x; event status 0x%04x\n", cmd, s);
2468123098Simp		if (s == 0xffff)
2469123098Simp			sc->wi_gone = 1;
2470109323Ssam		return(ETIMEDOUT);
247153702Swpaul	}
2472109323Ssam	return (0);
2473109323Ssam}
247453702Swpaul
2475109323Ssamstatic int
2476109323Ssamwi_seek_bap(struct wi_softc *sc, int id, int off)
2477109323Ssam{
2478109323Ssam	int i, status;
247990580Sbrooks
2480109323Ssam	CSR_WRITE_2(sc, WI_SEL0, id);
2481109323Ssam	CSR_WRITE_2(sc, WI_OFF0, off);
248290580Sbrooks
2483109323Ssam	for (i = 0; ; i++) {
2484109323Ssam		status = CSR_READ_2(sc, WI_OFF0);
2485109323Ssam		if ((status & WI_OFF_BUSY) == 0)
2486109323Ssam			break;
2487109323Ssam		if (i == WI_TIMEOUT) {
2488109323Ssam			device_printf(sc->sc_dev, "timeout in wi_seek to %x/%x\n",
2489109323Ssam			    id, off);
2490109323Ssam			sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2491123098Simp			if (status == 0xffff)
2492123098Simp				sc->wi_gone = 1;
2493109323Ssam			return ETIMEDOUT;
2494109323Ssam		}
2495109323Ssam		DELAY(1);
249653702Swpaul	}
2497109323Ssam	if (status & WI_OFF_ERR) {
2498109323Ssam		device_printf(sc->sc_dev, "failed in wi_seek to %x/%x\n", id, off);
2499109323Ssam		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2500109323Ssam		return EIO;
2501109323Ssam	}
2502109323Ssam	sc->sc_bap_id = id;
2503109323Ssam	sc->sc_bap_off = off;
2504109323Ssam	return 0;
250553702Swpaul}
250653702Swpaul
2507109323Ssamstatic int
2508109323Ssamwi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
250953702Swpaul{
2510109323Ssam	u_int16_t *ptr;
2511109323Ssam	int i, error, cnt;
251253702Swpaul
2513109323Ssam	if (buflen == 0)
2514109323Ssam		return 0;
2515109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2516109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
2517109323Ssam			return error;
251875219Salfred	}
2519109323Ssam	cnt = (buflen + 1) / 2;
2520109323Ssam	ptr = (u_int16_t *)buf;
2521109323Ssam	for (i = 0; i < cnt; i++)
2522109323Ssam		*ptr++ = CSR_READ_2(sc, WI_DATA0);
2523109323Ssam	sc->sc_bap_off += cnt * 2;
2524109323Ssam	return 0;
252553702Swpaul}
252653702Swpaul
2527109323Ssamstatic int
2528109323Ssamwi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
252953702Swpaul{
2530109323Ssam	u_int16_t *ptr;
2531109323Ssam	int i, error, cnt;
253246492Swpaul
2533109323Ssam	if (buflen == 0)
2534109323Ssam		return 0;
253546492Swpaul
2536109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
2537109323Ssam  again:
2538109323Ssam#endif
2539109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2540109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
2541109323Ssam			return error;
2542109323Ssam	}
2543109323Ssam	cnt = (buflen + 1) / 2;
2544109323Ssam	ptr = (u_int16_t *)buf;
2545109323Ssam	for (i = 0; i < cnt; i++)
2546109323Ssam		CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
2547109323Ssam	sc->sc_bap_off += cnt * 2;
2548109323Ssam
2549109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
2550109323Ssam	/*
2551109323Ssam	 * According to the comments in the HCF Light code, there is a bug
2552109323Ssam	 * in the Hermes (or possibly in certain Hermes firmware revisions)
2553109323Ssam	 * where the chip's internal autoincrement counter gets thrown off
2554109323Ssam	 * during data writes:  the autoincrement is missed, causing one
2555109323Ssam	 * data word to be overwritten and subsequent words to be written to
2556109323Ssam	 * the wrong memory locations. The end result is that we could end
2557109323Ssam	 * up transmitting bogus frames without realizing it. The workaround
2558109323Ssam	 * for this is to write a couple of extra guard words after the end
2559109323Ssam	 * of the transfer, then attempt to read then back. If we fail to
2560109323Ssam	 * locate the guard words where we expect them, we preform the
2561109323Ssam	 * transfer over again.
2562109323Ssam	 */
2563109323Ssam	if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
2564109323Ssam		CSR_WRITE_2(sc, WI_DATA0, 0x1234);
2565109323Ssam		CSR_WRITE_2(sc, WI_DATA0, 0x5678);
2566109323Ssam		wi_seek_bap(sc, id, sc->sc_bap_off);
2567109323Ssam		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2568109323Ssam		if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
2569109323Ssam		    CSR_READ_2(sc, WI_DATA0) != 0x5678) {
2570109323Ssam			device_printf(sc->sc_dev,
2571109323Ssam				"detect auto increment bug, try again\n");
2572109323Ssam			goto again;
2573109323Ssam		}
2574109323Ssam	}
2575109323Ssam#endif
2576109323Ssam	return 0;
257746492Swpaul}
257853702Swpaul
2579109323Ssamstatic int
2580109323Ssamwi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
2581109323Ssam{
2582109323Ssam	int error, len;
2583109323Ssam	struct mbuf *m;
258453702Swpaul
2585109323Ssam	for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
2586109323Ssam		if (m->m_len == 0)
2587109323Ssam			continue;
258853702Swpaul
2589109323Ssam		len = min(m->m_len, totlen);
259053702Swpaul
2591109323Ssam		if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
2592109323Ssam			m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
2593109323Ssam			return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
2594109323Ssam			    totlen);
2595109323Ssam		}
259653702Swpaul
2597109323Ssam		if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
2598109323Ssam			return error;
259953702Swpaul
2600109323Ssam		off += m->m_len;
2601109323Ssam		totlen -= len;
2602109323Ssam	}
2603109323Ssam	return 0;
2604109323Ssam}
260553702Swpaul
2606109323Ssamstatic int
2607109323Ssamwi_alloc_fid(struct wi_softc *sc, int len, int *idp)
260853702Swpaul{
260953702Swpaul	int i;
261053702Swpaul
2611109323Ssam	if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
2612109323Ssam		device_printf(sc->sc_dev, "failed to allocate %d bytes on NIC\n",
2613109323Ssam		    len);
2614109323Ssam		return ENOMEM;
261553702Swpaul	}
261653702Swpaul
2617109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
2618109323Ssam		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
2619109323Ssam			break;
2620109323Ssam		if (i == WI_TIMEOUT) {
2621109323Ssam			device_printf(sc->sc_dev, "timeout in alloc\n");
2622109323Ssam			return ETIMEDOUT;
2623109323Ssam		}
2624109323Ssam		DELAY(1);
262553702Swpaul	}
2626109323Ssam	*idp = CSR_READ_2(sc, WI_ALLOC_FID);
2627109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
2628109323Ssam	return 0;
2629109323Ssam}
263053702Swpaul
2631109323Ssamstatic int
2632109323Ssamwi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
2633109323Ssam{
2634109323Ssam	int error, len;
2635109323Ssam	u_int16_t ltbuf[2];
263653702Swpaul
2637109323Ssam	/* Tell the NIC to enter record read mode. */
2638109323Ssam	error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2639109323Ssam	if (error)
2640109323Ssam		return error;
264153702Swpaul
2642109323Ssam	error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2643109323Ssam	if (error)
2644109323Ssam		return error;
264553702Swpaul
2646109323Ssam	if (le16toh(ltbuf[1]) != rid) {
2647109323Ssam		device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n",
2648109323Ssam		    rid, le16toh(ltbuf[1]));
2649109323Ssam		return EIO;
265053702Swpaul	}
2651109323Ssam	len = (le16toh(ltbuf[0]) - 1) * 2;	 /* already got rid */
2652109323Ssam	if (*buflenp < len) {
2653109323Ssam		device_printf(sc->sc_dev, "record buffer is too small, "
2654109323Ssam		    "rid=%x, size=%d, len=%d\n",
2655109323Ssam		    rid, *buflenp, len);
2656109323Ssam		return ENOSPC;
265753702Swpaul	}
2658109323Ssam	*buflenp = len;
2659109323Ssam	return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2660109323Ssam}
266153702Swpaul
2662109323Ssamstatic int
2663109323Ssamwi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2664109323Ssam{
2665109323Ssam	int error;
2666109323Ssam	u_int16_t ltbuf[2];
266753702Swpaul
2668109323Ssam	ltbuf[0] = htole16((buflen + 1) / 2 + 1);	 /* includes rid */
2669109323Ssam	ltbuf[1] = htole16(rid);
267053702Swpaul
2671109323Ssam	error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2672109323Ssam	if (error)
2673109323Ssam		return error;
2674109323Ssam	error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2675109323Ssam	if (error)
2676109323Ssam		return error;
2677102206Simp
2678109323Ssam	return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
267953702Swpaul}
268077217Sphk
268188546Salfredstatic int
2682117812Ssamwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
268377217Sphk{
2684138571Ssam	struct ifnet *ifp = ic->ic_ifp;
2685117812Ssam	struct wi_softc *sc = ifp->if_softc;
2686139542Ssam	struct ieee80211_node *ni;
2687116951Ssam	int buflen;
2688109323Ssam	u_int16_t val;
2689109323Ssam	struct wi_ssid ssid;
2690109323Ssam	u_int8_t old_bssid[IEEE80211_ADDR_LEN];
269177217Sphk
2692117812Ssam	DPRINTF(("%s: %s -> %s\n", __func__,
2693117812Ssam		ieee80211_state_name[ic->ic_state],
2694117812Ssam		ieee80211_state_name[nstate]));
2695109323Ssam
2696139542Ssam	/*
2697139542Ssam	 * Internal to the driver the INIT and RUN states are used
2698139542Ssam	 * so bypass the net80211 state machine for other states.
2699139542Ssam	 * Beware however that this requires use to net80211 state
2700139542Ssam	 * management that otherwise would be handled for us.
2701139542Ssam	 */
2702109323Ssam	switch (nstate) {
2703109323Ssam	case IEEE80211_S_INIT:
2704109323Ssam		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2705117812Ssam		return (*sc->sc_newstate)(ic, nstate, arg);
2706109323Ssam
2707138571Ssam	case IEEE80211_S_SCAN:
2708138571Ssam	case IEEE80211_S_AUTH:
2709138571Ssam	case IEEE80211_S_ASSOC:
2710138571Ssam		ic->ic_state = nstate;	/* NB: skip normal ieee80211 handling */
2711138571Ssam		break;
2712138571Ssam
2713109323Ssam	case IEEE80211_S_RUN:
2714139542Ssam		ni = ic->ic_bss;
2715109323Ssam		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2716109323Ssam		buflen = IEEE80211_ADDR_LEN;
2717138571Ssam		IEEE80211_ADDR_COPY(old_bssid, ni->ni_bssid);
2718109323Ssam		wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen);
2719109323Ssam		IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
2720109323Ssam		buflen = sizeof(val);
2721109323Ssam		wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
2722116951Ssam		/* XXX validate channel */
2723116951Ssam		ni->ni_chan = &ic->ic_channels[le16toh(val)];
2724138951Ssam		ic->ic_ibss_chan = ni->ni_chan;
2725119784Ssam#if NBPFILTER > 0
2726119784Ssam		sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
2727119784Ssam			htole16(ni->ni_chan->ic_freq);
2728119784Ssam		sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
2729119784Ssam			htole16(ni->ni_chan->ic_flags);
2730119784Ssam#endif
2731139542Ssam		if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
2732138952Ssam			/*
2733138952Ssam			 * XXX hack; unceremoniously clear
2734138952Ssam			 * IEEE80211_F_DROPUNENC when operating with
2735138952Ssam			 * wep enabled so we don't drop unencoded frames
2736138952Ssam			 * at the 802.11 layer.  This is necessary because
2737138952Ssam			 * we must strip the WEP bit from the 802.11 header
2738138952Ssam			 * before passing frames to ieee80211_input because
2739138952Ssam			 * the card has already stripped the WEP crypto
2740138952Ssam			 * header from the packet.
2741138952Ssam			 */
2742138952Ssam			if (ic->ic_flags & IEEE80211_F_PRIVACY)
2743138952Ssam				ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
2744109323Ssam			/* XXX check return value */
2745109323Ssam			buflen = sizeof(ssid);
2746109323Ssam			wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
2747109323Ssam			ni->ni_esslen = le16toh(ssid.wi_len);
2748109323Ssam			if (ni->ni_esslen > IEEE80211_NWID_LEN)
2749109323Ssam				ni->ni_esslen = IEEE80211_NWID_LEN;	/*XXX*/
2750109323Ssam			memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
275177217Sphk		}
2752138571Ssam		return (*sc->sc_newstate)(ic, nstate, arg);
275377217Sphk	}
2754117812Ssam	return 0;
275577217Sphk}
275677217Sphk
275788546Salfredstatic int
2758116898Ssamwi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate)
275977217Sphk{
2760109323Ssam	int error = 0;
2761109323Ssam	u_int16_t val[2];
276277217Sphk
2763109323Ssam	if (!sc->sc_enabled)
2764109323Ssam		return ENXIO;
2765109323Ssam	switch (sc->sc_firmware_type) {
2766109323Ssam	case WI_LUCENT:
2767109323Ssam		(void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
276898440Simp		break;
2769109323Ssam	case WI_INTERSIL:
2770138571Ssam		val[0] = htole16(chanmask);	/* channel */
2771138571Ssam		val[1] = htole16(txrate);	/* tx rate */
2772109323Ssam		error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
277398440Simp		break;
2774109323Ssam	case WI_SYMBOL:
2775109323Ssam		/*
2776109323Ssam		 * XXX only supported on 3.x ?
2777109323Ssam		 */
2778109323Ssam		val[0] = BSCAN_BCAST | BSCAN_ONETIME;
2779109323Ssam		error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
2780109323Ssam		    val, sizeof(val[0]));
278198440Simp		break;
278298440Simp	}
2783109323Ssam	if (error == 0) {
2784109323Ssam		sc->sc_scan_timer = WI_SCAN_WAIT;
2785138571Ssam		sc->sc_if.if_timer = 1;
2786116898Ssam		DPRINTF(("wi_scan_ap: start scanning, "
2787116898Ssam			"chamask 0x%x txrate 0x%x\n", chanmask, txrate));
2788109323Ssam	}
2789109323Ssam	return error;
2790109323Ssam}
279177217Sphk
2792109323Ssamstatic void
2793109323Ssamwi_scan_result(struct wi_softc *sc, int fid, int cnt)
2794109323Ssam{
2795109323Ssam#define	N(a)	(sizeof (a) / sizeof (a[0]))
2796109323Ssam	int i, naps, off, szbuf;
2797109323Ssam	struct wi_scan_header ws_hdr;	/* Prism2 header */
2798109323Ssam	struct wi_scan_data_p2 ws_dat;	/* Prism2 scantable*/
2799109323Ssam	struct wi_apinfo *ap;
2800109323Ssam
2801109323Ssam	off = sizeof(u_int16_t) * 2;
2802109323Ssam	memset(&ws_hdr, 0, sizeof(ws_hdr));
2803109323Ssam	switch (sc->sc_firmware_type) {
2804109323Ssam	case WI_INTERSIL:
2805109323Ssam		wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
2806109323Ssam		off += sizeof(ws_hdr);
2807109323Ssam		szbuf = sizeof(struct wi_scan_data_p2);
280877217Sphk		break;
2809109323Ssam	case WI_SYMBOL:
2810109323Ssam		szbuf = sizeof(struct wi_scan_data_p2) + 6;
281177217Sphk		break;
2812109323Ssam	case WI_LUCENT:
2813109323Ssam		szbuf = sizeof(struct wi_scan_data);
281477217Sphk		break;
2815109323Ssam	default:
2816109323Ssam		device_printf(sc->sc_dev,
2817109323Ssam			"wi_scan_result: unknown firmware type %u\n",
2818109323Ssam			sc->sc_firmware_type);
2819109323Ssam		naps = 0;
2820109323Ssam		goto done;
282177217Sphk	}
2822109323Ssam	naps = (cnt * 2 + 2 - off) / szbuf;
2823109323Ssam	if (naps > N(sc->sc_aps))
2824109323Ssam		naps = N(sc->sc_aps);
2825109323Ssam	sc->sc_naps = naps;
2826109323Ssam	/* Read Data */
2827109323Ssam	ap = sc->sc_aps;
2828109323Ssam	memset(&ws_dat, 0, sizeof(ws_dat));
2829109323Ssam	for (i = 0; i < naps; i++, ap++) {
2830109323Ssam		wi_read_bap(sc, fid, off, &ws_dat,
2831109323Ssam		    (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
2832109323Ssam		DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off,
2833109323Ssam		    ether_sprintf(ws_dat.wi_bssid)));
2834109323Ssam		off += szbuf;
2835109323Ssam		ap->scanreason = le16toh(ws_hdr.wi_reason);
2836109323Ssam		memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
2837109323Ssam		ap->channel = le16toh(ws_dat.wi_chid);
2838109323Ssam		ap->signal  = le16toh(ws_dat.wi_signal);
2839109323Ssam		ap->noise   = le16toh(ws_dat.wi_noise);
2840109323Ssam		ap->quality = ap->signal - ap->noise;
2841109323Ssam		ap->capinfo = le16toh(ws_dat.wi_capinfo);
2842109323Ssam		ap->interval = le16toh(ws_dat.wi_interval);
2843109323Ssam		ap->rate    = le16toh(ws_dat.wi_rate);
2844109323Ssam		ap->namelen = le16toh(ws_dat.wi_namelen);
2845109323Ssam		if (ap->namelen > sizeof(ap->name))
2846109323Ssam			ap->namelen = sizeof(ap->name);
2847109323Ssam		memcpy(ap->name, ws_dat.wi_name, ap->namelen);
2848109323Ssam	}
2849109323Ssamdone:
2850109323Ssam	/* Done scanning */
2851109323Ssam	sc->sc_scan_timer = 0;
2852109323Ssam	DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
2853109323Ssam#undef N
285477217Sphk}
285577217Sphk
285688546Salfredstatic void
2857109323Ssamwi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi)
285877217Sphk{
2859109323Ssam	ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr),
2860116951Ssam	    ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL : -1, rssi);
2861109323Ssam	printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n",
2862109323Ssam		le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1),
2863109323Ssam		le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence);
2864109323Ssam	printf(" rx_signal %u rx_rate %u rx_flow %u\n",
2865109323Ssam		wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow);
2866109323Ssam	printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n",
2867109323Ssam		wh->wi_tx_rtry, wh->wi_tx_rate,
2868109323Ssam		le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len));
2869109323Ssam	printf(" ehdr dst %6D src %6D type 0x%x\n",
2870109323Ssam		wh->wi_ehdr.ether_dhost, ":", wh->wi_ehdr.ether_shost, ":",
2871109323Ssam		wh->wi_ehdr.ether_type);
2872109323Ssam}
287377217Sphk
2874109323Ssamint
2875109323Ssamwi_alloc(device_t dev, int rid)
2876109323Ssam{
2877109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2878109323Ssam
2879109323Ssam	if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2880109323Ssam		sc->iobase_rid = rid;
2881109323Ssam		sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2882109323Ssam		    &sc->iobase_rid, 0, ~0, (1 << 6),
2883109323Ssam		    rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2884109323Ssam		if (!sc->iobase) {
2885109323Ssam			device_printf(dev, "No I/O space?!\n");
2886109323Ssam			return (ENXIO);
288798440Simp		}
2888109323Ssam
2889109323Ssam		sc->wi_io_addr = rman_get_start(sc->iobase);
2890109323Ssam		sc->wi_btag = rman_get_bustag(sc->iobase);
2891109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2892109323Ssam	} else {
2893109323Ssam		sc->mem_rid = rid;
2894127135Snjl		sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2895127135Snjl		    &sc->mem_rid, RF_ACTIVE);
2896109323Ssam
2897109323Ssam		if (!sc->mem) {
2898109323Ssam			device_printf(dev, "No Mem space on prism2.5?\n");
2899109323Ssam			return (ENXIO);
290077217Sphk		}
2901109323Ssam
2902109323Ssam		sc->wi_btag = rman_get_bustag(sc->mem);
2903109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->mem);
290477217Sphk	}
290577217Sphk
2906109323Ssam
2907109323Ssam	sc->irq_rid = 0;
2908127135Snjl	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2909127135Snjl	    RF_ACTIVE |
2910109323Ssam	    ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2911109323Ssam
2912109323Ssam	if (!sc->irq) {
2913109323Ssam		wi_free(dev);
2914109323Ssam		device_printf(dev, "No irq?!\n");
2915109323Ssam		return (ENXIO);
291677217Sphk	}
2917109323Ssam
2918109323Ssam	sc->sc_dev = dev;
2919109323Ssam	sc->sc_unit = device_get_unit(dev);
2920109323Ssam
2921109323Ssam	return (0);
292277217Sphk}
292393359Simp
2924109323Ssamvoid
2925109323Ssamwi_free(device_t dev)
2926109323Ssam{
2927109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2928109323Ssam
2929109323Ssam	if (sc->iobase != NULL) {
2930109323Ssam		bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2931109323Ssam		sc->iobase = NULL;
2932109323Ssam	}
2933109323Ssam	if (sc->irq != NULL) {
2934109323Ssam		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2935109323Ssam		sc->irq = NULL;
2936109323Ssam	}
2937109323Ssam	if (sc->mem != NULL) {
2938109323Ssam		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
2939109323Ssam		sc->mem = NULL;
2940109323Ssam	}
2941109323Ssam
2942109323Ssam	return;
2943109323Ssam}
2944109323Ssam
294593359Simpstatic int
2946109323Ssamwi_get_debug(struct wi_softc *sc, struct wi_req *wreq)
294793359Simp{
2948109323Ssam	int error = 0;
294993359Simp
295093359Simp	wreq->wi_len = 1;
295193359Simp
295293359Simp	switch (wreq->wi_type) {
295393359Simp	case WI_DEBUG_SLEEP:
295493359Simp		wreq->wi_len++;
295593359Simp		wreq->wi_val[0] = sc->wi_debug.wi_sleep;
295693359Simp		break;
295793359Simp	case WI_DEBUG_DELAYSUPP:
295893359Simp		wreq->wi_len++;
295993359Simp		wreq->wi_val[0] = sc->wi_debug.wi_delaysupp;
296093359Simp		break;
296193359Simp	case WI_DEBUG_TXSUPP:
296293359Simp		wreq->wi_len++;
296393359Simp		wreq->wi_val[0] = sc->wi_debug.wi_txsupp;
296493359Simp		break;
296593359Simp	case WI_DEBUG_MONITOR:
296693359Simp		wreq->wi_len++;
296793359Simp		wreq->wi_val[0] = sc->wi_debug.wi_monitor;
296893359Simp		break;
296993359Simp	case WI_DEBUG_LEDTEST:
297093359Simp		wreq->wi_len += 3;
297193359Simp		wreq->wi_val[0] = sc->wi_debug.wi_ledtest;
297293359Simp		wreq->wi_val[1] = sc->wi_debug.wi_ledtest_param0;
297393359Simp		wreq->wi_val[2] = sc->wi_debug.wi_ledtest_param1;
297493359Simp		break;
297593359Simp	case WI_DEBUG_CONTTX:
297693359Simp		wreq->wi_len += 2;
297793359Simp		wreq->wi_val[0] = sc->wi_debug.wi_conttx;
297893359Simp		wreq->wi_val[1] = sc->wi_debug.wi_conttx_param0;
297993359Simp		break;
298093359Simp	case WI_DEBUG_CONTRX:
298193359Simp		wreq->wi_len++;
298293359Simp		wreq->wi_val[0] = sc->wi_debug.wi_contrx;
298393359Simp		break;
298493359Simp	case WI_DEBUG_SIGSTATE:
298593359Simp		wreq->wi_len += 2;
298693359Simp		wreq->wi_val[0] = sc->wi_debug.wi_sigstate;
298793359Simp		wreq->wi_val[1] = sc->wi_debug.wi_sigstate_param0;
298893359Simp		break;
298993359Simp	case WI_DEBUG_CONFBITS:
299093359Simp		wreq->wi_len += 2;
299193359Simp		wreq->wi_val[0] = sc->wi_debug.wi_confbits;
299293359Simp		wreq->wi_val[1] = sc->wi_debug.wi_confbits_param0;
299393359Simp		break;
299493359Simp	default:
299593359Simp		error = EIO;
299693359Simp		break;
299793359Simp	}
299893359Simp
299993359Simp	return (error);
300093359Simp}
300193359Simp
300293359Simpstatic int
3003109323Ssamwi_set_debug(struct wi_softc *sc, struct wi_req *wreq)
300493359Simp{
3005109323Ssam	int error = 0;
300693359Simp	u_int16_t		cmd, param0 = 0, param1 = 0;
300793359Simp
300893359Simp	switch (wreq->wi_type) {
300993359Simp	case WI_DEBUG_RESET:
301093359Simp	case WI_DEBUG_INIT:
301193359Simp	case WI_DEBUG_CALENABLE:
301293359Simp		break;
301393359Simp	case WI_DEBUG_SLEEP:
301493359Simp		sc->wi_debug.wi_sleep = 1;
301593359Simp		break;
301693359Simp	case WI_DEBUG_WAKE:
301793359Simp		sc->wi_debug.wi_sleep = 0;
301893359Simp		break;
301993359Simp	case WI_DEBUG_CHAN:
302093359Simp		param0 = wreq->wi_val[0];
302193359Simp		break;
302293359Simp	case WI_DEBUG_DELAYSUPP:
302393359Simp		sc->wi_debug.wi_delaysupp = 1;
302493359Simp		break;
302593359Simp	case WI_DEBUG_TXSUPP:
302693359Simp		sc->wi_debug.wi_txsupp = 1;
302793359Simp		break;
302893359Simp	case WI_DEBUG_MONITOR:
302993359Simp		sc->wi_debug.wi_monitor = 1;
303093359Simp		break;
303193359Simp	case WI_DEBUG_LEDTEST:
303293359Simp		param0 = wreq->wi_val[0];
303393359Simp		param1 = wreq->wi_val[1];
303493359Simp		sc->wi_debug.wi_ledtest = 1;
303593359Simp		sc->wi_debug.wi_ledtest_param0 = param0;
303693359Simp		sc->wi_debug.wi_ledtest_param1 = param1;
303793359Simp		break;
303893359Simp	case WI_DEBUG_CONTTX:
303993359Simp		param0 = wreq->wi_val[0];
304093359Simp		sc->wi_debug.wi_conttx = 1;
304193359Simp		sc->wi_debug.wi_conttx_param0 = param0;
304293359Simp		break;
304393359Simp	case WI_DEBUG_STOPTEST:
304493359Simp		sc->wi_debug.wi_delaysupp = 0;
304593359Simp		sc->wi_debug.wi_txsupp = 0;
304693359Simp		sc->wi_debug.wi_monitor = 0;
304793359Simp		sc->wi_debug.wi_ledtest = 0;
304893359Simp		sc->wi_debug.wi_ledtest_param0 = 0;
304993359Simp		sc->wi_debug.wi_ledtest_param1 = 0;
305093359Simp		sc->wi_debug.wi_conttx = 0;
305193359Simp		sc->wi_debug.wi_conttx_param0 = 0;
305293359Simp		sc->wi_debug.wi_contrx = 0;
305393359Simp		sc->wi_debug.wi_sigstate = 0;
305493359Simp		sc->wi_debug.wi_sigstate_param0 = 0;
305593359Simp		break;
305693359Simp	case WI_DEBUG_CONTRX:
305793359Simp		sc->wi_debug.wi_contrx = 1;
305893359Simp		break;
305993359Simp	case WI_DEBUG_SIGSTATE:
306093359Simp		param0 = wreq->wi_val[0];
306193359Simp		sc->wi_debug.wi_sigstate = 1;
306293359Simp		sc->wi_debug.wi_sigstate_param0 = param0;
306393359Simp		break;
306493359Simp	case WI_DEBUG_CONFBITS:
306593359Simp		param0 = wreq->wi_val[0];
306693359Simp		param1 = wreq->wi_val[1];
306793359Simp		sc->wi_debug.wi_confbits = param0;
306893359Simp		sc->wi_debug.wi_confbits_param0 = param1;
306993359Simp		break;
307093359Simp	default:
307193359Simp		error = EIO;
307293359Simp		break;
307393359Simp	}
307493359Simp
307593359Simp	if (error)
307693359Simp		return (error);
307793359Simp
307893359Simp	cmd = WI_CMD_DEBUG | (wreq->wi_type << 8);
307993359Simp	error = wi_cmd(sc, cmd, param0, param1, 0);
308093359Simp
308193359Simp	return (error);
308293359Simp}
3083101903Simp
3084105076Simp#if __FreeBSD_version >= 500000
3085101903Simp/*
3086101903Simp * Special routines to download firmware for Symbol CF card.
3087101903Simp * XXX: This should be modified generic into any PRISM-2 based card.
3088101903Simp */
3089101903Simp
3090101903Simp#define	WI_SBCF_PDIADDR		0x3100
3091101903Simp
3092101903Simp/* unaligned load little endian */
3093101903Simp#define	GETLE32(p)	((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24))
3094101903Simp#define	GETLE16(p)	((p)[0] | ((p)[1]<<8))
3095101903Simp
3096101903Simpint
3097101903Simpwi_symbol_load_firm(struct wi_softc *sc, const void *primsym, int primlen,
3098101903Simp    const void *secsym, int seclen)
3099101903Simp{
3100101903Simp	uint8_t ebuf[256];
3101101903Simp	int i;
3102101903Simp
3103101903Simp	/* load primary code and run it */
3104101903Simp	wi_symbol_set_hcr(sc, WI_HCR_EEHOLD);
3105101903Simp	if (wi_symbol_write_firm(sc, primsym, primlen, NULL, 0))
3106101903Simp		return EIO;
3107101903Simp	wi_symbol_set_hcr(sc, WI_HCR_RUN);
3108101903Simp	for (i = 0; ; i++) {
3109101903Simp		if (i == 10)
3110101903Simp			return ETIMEDOUT;
3111101903Simp		tsleep(sc, PWAIT, "wiinit", 1);
3112101903Simp		if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT)
3113101903Simp			break;
3114101903Simp		/* write the magic key value to unlock aux port */
3115101903Simp		CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0);
3116101903Simp		CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1);
3117101903Simp		CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2);
3118101903Simp		CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL);
3119101903Simp	}
3120101903Simp
3121101903Simp	/* issue read EEPROM command: XXX copied from wi_cmd() */
3122101903Simp	CSR_WRITE_2(sc, WI_PARAM0, 0);
3123101903Simp	CSR_WRITE_2(sc, WI_PARAM1, 0);
3124101903Simp	CSR_WRITE_2(sc, WI_PARAM2, 0);
3125101903Simp	CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE);
3126101903Simp        for (i = 0; i < WI_TIMEOUT; i++) {
3127101903Simp                if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
3128101903Simp                        break;
3129101903Simp                DELAY(1);
3130101903Simp        }
3131101903Simp        CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
3132101903Simp
3133101903Simp	CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ);
3134101903Simp	CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ);
3135101903Simp	CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA,
3136101903Simp	    (uint16_t *)ebuf, sizeof(ebuf) / 2);
3137101903Simp	if (GETLE16(ebuf) > sizeof(ebuf))
3138101903Simp		return EIO;
3139101903Simp	if (wi_symbol_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf)))
3140101903Simp		return EIO;
3141101903Simp	return 0;
3142101903Simp}
3143101903Simp
3144101903Simpstatic int
3145101903Simpwi_symbol_write_firm(struct wi_softc *sc, const void *buf, int buflen,
3146101903Simp    const void *ebuf, int ebuflen)
3147101903Simp{
3148101903Simp	const uint8_t *p, *ep, *q, *eq;
3149101903Simp	char *tp;
3150101903Simp	uint32_t addr, id, eid;
3151101903Simp	int i, len, elen, nblk, pdrlen;
3152101903Simp
3153101903Simp	/*
3154101903Simp	 * Parse the header of the firmware image.
3155101903Simp	 */
3156101903Simp	p = buf;
3157101903Simp	ep = p + buflen;
3158101903Simp	while (p < ep && *p++ != ' ');	/* FILE: */
3159101903Simp	while (p < ep && *p++ != ' ');	/* filename */
3160101903Simp	while (p < ep && *p++ != ' ');	/* type of the firmware */
3161101903Simp	nblk = strtoul(p, &tp, 10);
3162101903Simp	p = tp;
3163101903Simp	pdrlen = strtoul(p + 1, &tp, 10);
3164101903Simp	p = tp;
3165101903Simp	while (p < ep && *p++ != 0x1a);	/* skip rest of header */
3166101903Simp
3167101903Simp	/*
3168101903Simp	 * Block records: address[4], length[2], data[length];
3169101903Simp	 */
3170101903Simp	for (i = 0; i < nblk; i++) {
3171101903Simp		addr = GETLE32(p);	p += 4;
3172101903Simp		len  = GETLE16(p);	p += 2;
3173101903Simp		CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3174101903Simp		CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3175101903Simp		CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3176101903Simp		    (const uint16_t *)p, len / 2);
3177101903Simp		p += len;
3178101903Simp	}
3179101903Simp
3180101903Simp	/*
3181101903Simp	 * PDR: id[4], address[4], length[4];
3182101903Simp	 */
3183101903Simp	for (i = 0; i < pdrlen; ) {
3184101903Simp		id   = GETLE32(p);	p += 4; i += 4;
3185101903Simp		addr = GETLE32(p);	p += 4; i += 4;
3186101903Simp		len  = GETLE32(p);	p += 4; i += 4;
3187101903Simp		/* replace PDR entry with the values from EEPROM, if any */
3188101903Simp		for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) {
3189101903Simp			elen = GETLE16(q);	q += 2;
3190101903Simp			eid  = GETLE16(q);	q += 2;
3191101903Simp			elen--;		/* elen includes eid */
3192101903Simp			if (eid == 0)
3193101903Simp				break;
3194101903Simp			if (eid != id)
3195101903Simp				continue;
3196101903Simp			CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3197101903Simp			CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3198101903Simp			CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3199101903Simp			    (const uint16_t *)q, len / 2);
3200101903Simp			break;
3201101903Simp		}
3202101903Simp	}
3203101903Simp	return 0;
3204101903Simp}
3205101903Simp
3206101903Simpstatic int
3207101903Simpwi_symbol_set_hcr(struct wi_softc *sc, int mode)
3208101903Simp{
3209101903Simp	uint16_t hcr;
3210101903Simp
3211101903Simp	CSR_WRITE_2(sc, WI_COR, WI_COR_RESET);
3212101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3213101903Simp	hcr = CSR_READ_2(sc, WI_HCR);
3214101903Simp	hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE);
3215101903Simp	CSR_WRITE_2(sc, WI_HCR, hcr);
3216101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3217101903Simp	CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE);
3218101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3219101903Simp	return 0;
3220101903Simp}
3221105076Simp#endif
3222