if_wi.c revision 147256
1109323Ssam/*	$NetBSD: wi.c,v 1.109 2003/01/09 08:52:19 dyoung Exp $	*/
2109323Ssam
3139749Simp/*-
446492Swpaul * Copyright (c) 1997, 1998, 1999
546492Swpaul *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
646492Swpaul *
746492Swpaul * Redistribution and use in source and binary forms, with or without
846492Swpaul * modification, are permitted provided that the following conditions
946492Swpaul * are met:
1046492Swpaul * 1. Redistributions of source code must retain the above copyright
1146492Swpaul *    notice, this list of conditions and the following disclaimer.
1246492Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1346492Swpaul *    notice, this list of conditions and the following disclaimer in the
1446492Swpaul *    documentation and/or other materials provided with the distribution.
1546492Swpaul * 3. All advertising materials mentioning features or use of this software
1646492Swpaul *    must display the following acknowledgement:
1746492Swpaul *	This product includes software developed by Bill Paul.
1846492Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1946492Swpaul *    may be used to endorse or promote products derived from this software
2046492Swpaul *    without specific prior written permission.
2146492Swpaul *
2246492Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2346492Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2446492Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2546492Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2646492Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2746492Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2846492Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2946492Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3046492Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3146492Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3246492Swpaul * THE POSSIBILITY OF SUCH DAMAGE.
3346492Swpaul */
3446492Swpaul
3546492Swpaul/*
36109323Ssam * Lucent WaveLAN/IEEE 802.11 PCMCIA driver.
3746492Swpaul *
38109323Ssam * Original FreeBSD driver written by Bill Paul <wpaul@ctr.columbia.edu>
3946492Swpaul * Electrical Engineering Department
4046492Swpaul * Columbia University, New York City
4146492Swpaul */
4246492Swpaul
4346492Swpaul/*
4447401Swpaul * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
4546492Swpaul * from Lucent. Unlike the older cards, the new ones are programmed
4646492Swpaul * entirely via a firmware-driven controller called the Hermes.
4746492Swpaul * Unfortunately, Lucent will not release the Hermes programming manual
4846492Swpaul * without an NDA (if at all). What they do release is an API library
4946492Swpaul * called the HCF (Hardware Control Functions) which is supposed to
5046492Swpaul * do the device-specific operations of a device driver for you. The
5146492Swpaul * publically available version of the HCF library (the 'HCF Light') is
5247401Swpaul * a) extremely gross, b) lacks certain features, particularly support
5346492Swpaul * for 802.11 frames, and c) is contaminated by the GNU Public License.
5446492Swpaul *
5546492Swpaul * This driver does not use the HCF or HCF Light at all. Instead, it
5646492Swpaul * programs the Hermes controller directly, using information gleaned
5746492Swpaul * from the HCF Light code and corresponding documentation.
5846492Swpaul *
5995534Simp * This driver supports the ISA, PCMCIA and PCI versions of the Lucent
6095534Simp * WaveLan cards (based on the Hermes chipset), as well as the newer
6195534Simp * Prism 2 chipsets with firmware from Intersil and Symbol.
6246492Swpaul */
6346492Swpaul
64113038Sobrien#include <sys/cdefs.h>
65113038Sobrien__FBSDID("$FreeBSD: head/sys/dev/wi/if_wi.c 147256 2005-06-10 16:49:24Z brooks $");
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;
241147256Sbrooks	struct ifnet *ifp;
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
251147256Sbrooks	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
252147256Sbrooks	if (ifp == NULL) {
253147256Sbrooks		device_printf(dev, "can not if_alloc\n");
254147256Sbrooks		wi_free(dev);
255147256Sbrooks		return (ENOSPC);
256147256Sbrooks	}
257147256Sbrooks
258109323Ssam	/*
259109323Ssam	 * NB: no locking is needed here; don't put it here
260109323Ssam	 *     unless you can prove it!
261109323Ssam	 */
262121697Ssam	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
26374998Swpaul	    wi_intr, sc, &sc->wi_intrhand);
26453702Swpaul
26553702Swpaul	if (error) {
26653702Swpaul		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
26753702Swpaul		wi_free(dev);
26853702Swpaul		return (error);
26953702Swpaul	}
27053702Swpaul
27195534Simp#if __FreeBSD_version >= 500000
272109323Ssam	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
27393818Sjhb	    MTX_DEF | MTX_RECURSE);
27495534Simp#endif
27567092Swpaul
276138571Ssam	sc->sc_firmware_type = WI_NOTYPE;
277123339Simp	sc->wi_cmd_count = 500;
27846492Swpaul	/* Reset the NIC. */
279112096Simp	if (wi_reset(sc) != 0)
280109323Ssam		return ENXIO;		/* XXX */
28146492Swpaul
28276438Swpaul	/*
28376438Swpaul	 * Read the station address.
28476438Swpaul	 * And do it twice. I've seen PRISM-based cards that return
28576438Swpaul	 * an error when trying to read it the first time, which causes
28676438Swpaul	 * the probe to fail.
28776438Swpaul	 */
288109323Ssam	buflen = IEEE80211_ADDR_LEN;
289109323Ssam	error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
290109323Ssam	if (error != 0) {
291109323Ssam		buflen = IEEE80211_ADDR_LEN;
292109323Ssam		error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
293109323Ssam	}
294109323Ssam	if (error || IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) {
295109323Ssam		if (error != 0)
296109323Ssam			device_printf(dev, "mac read failed %d\n", error);
297109323Ssam		else
298109323Ssam			device_printf(dev, "mac read failed (all zeros)\n");
29975149Simp		wi_free(dev);
30075149Simp		return (error);
30175149Simp	}
30246492Swpaul
303109323Ssam	/* Read NIC identification */
304109323Ssam	wi_read_nicid(sc);
30546492Swpaul
30646492Swpaul	ifp->if_softc = sc;
307121816Sbrooks	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
30846492Swpaul	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
30946492Swpaul	ifp->if_ioctl = wi_ioctl;
31046492Swpaul	ifp->if_start = wi_start;
31146492Swpaul	ifp->if_watchdog = wi_watchdog;
31246492Swpaul	ifp->if_init = wi_init;
313132986Smlaier	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
314132986Smlaier	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
315132986Smlaier	IFQ_SET_READY(&ifp->if_snd);
31646492Swpaul
317138571Ssam	ic->ic_ifp = ifp;
318109323Ssam	ic->ic_phytype = IEEE80211_T_DS;
319109323Ssam	ic->ic_opmode = IEEE80211_M_STA;
320109323Ssam	ic->ic_state = IEEE80211_S_INIT;
321138571Ssam	ic->ic_caps = IEEE80211_C_PMGT
322138571Ssam		    | IEEE80211_C_WEP		/* everyone supports WEP */
323138571Ssam		    ;
324138571Ssam	ic->ic_max_aid = WI_MAX_AID;
32546492Swpaul
326116951Ssam	/*
327116951Ssam	 * Query the card for available channels and setup the
328116951Ssam	 * channel table.  We assume these are all 11b channels.
329116951Ssam	 */
330109323Ssam	buflen = sizeof(val);
331109323Ssam	if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
332109323Ssam		val = htole16(0x1fff);	/* assume 1-11 */
333116951Ssam	KASSERT(val != 0, ("wi_attach: no available channels listed!"));
334116951Ssam
335116951Ssam	val <<= 1;			/* shift for base 1 indices */
336116951Ssam	for (i = 1; i < 16; i++) {
337144986Smdodd		if (!isset((u_int8_t*)&val, i))
338144986Smdodd			continue;
339144986Smdodd		ic->ic_channels[i].ic_freq =
340144986Smdodd			ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
341144986Smdodd		ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
342109323Ssam	}
34346492Swpaul
34446563Swpaul	/*
34546563Swpaul	 * Read the default channel from the NIC. This may vary
34646563Swpaul	 * depending on the country where the NIC was purchased, so
34746563Swpaul	 * we can't hard-code a default and expect it to work for
34846563Swpaul	 * everyone.
349116951Ssam	 *
350116951Ssam	 * If no channel is specified, let the 802.11 code select.
35146563Swpaul	 */
352109323Ssam	buflen = sizeof(val);
353116951Ssam	if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0) {
354116951Ssam		val = le16toh(val);
355116951Ssam		KASSERT(val < IEEE80211_CHAN_MAX &&
356116951Ssam			ic->ic_channels[val].ic_flags != 0,
357116951Ssam			("wi_attach: invalid own channel %u!", val));
358116951Ssam		ic->ic_ibss_chan = &ic->ic_channels[val];
359117042Ssam	} else {
360117042Ssam		device_printf(dev,
361117042Ssam			"WI_RID_OWN_CHNL failed, using first channel!\n");
362117042Ssam		ic->ic_ibss_chan = &ic->ic_channels[0];
363109323Ssam	}
36446563Swpaul
36556965Swpaul	/*
36698440Simp	 * Set flags based on firmware version.
36798440Simp	 */
36898440Simp	switch (sc->sc_firmware_type) {
36998440Simp	case WI_LUCENT:
370112363Simp		sc->sc_ntxbuf = 1;
371109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
372109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
373109323Ssam		/* XXX: not confirmed, but never seen for recent firmware */
374109323Ssam		if (sc->sc_sta_firmware_ver <  40000) {
375109323Ssam			sc->sc_flags |= WI_FLAGS_BUG_AUTOINC;
376109323Ssam		}
377109323Ssam#endif
37898440Simp		if (sc->sc_sta_firmware_ver >= 60000)
379109323Ssam			sc->sc_flags |= WI_FLAGS_HAS_MOR;
380117855Ssam		if (sc->sc_sta_firmware_ver >= 60006) {
381116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
382117855Ssam			ic->ic_caps |= IEEE80211_C_MONITOR;
383117855Ssam		}
384109323Ssam		sc->sc_ibss_port = htole16(1);
385119784Ssam
386119784Ssam		sc->sc_min_rssi = WI_LUCENT_MIN_RSSI;
387119784Ssam		sc->sc_max_rssi = WI_LUCENT_MAX_RSSI;
388119784Ssam		sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
38998440Simp		break;
390109323Ssam
39198440Simp	case WI_INTERSIL:
392112363Simp		sc->sc_ntxbuf = WI_NTXBUF;
393109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR;
394109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_ROAMING;
395109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
396123339Simp		/*
397123339Simp		 * Old firmware are slow, so give peace a chance.
398123339Simp		 */
399123339Simp		if (sc->sc_sta_firmware_ver < 10000)
400123339Simp			sc->wi_cmd_count = 5000;
401109323Ssam		if (sc->sc_sta_firmware_ver > 10101)
402109323Ssam			sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
403117855Ssam		if (sc->sc_sta_firmware_ver >= 800) {
404116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
405117855Ssam			ic->ic_caps |= IEEE80211_C_MONITOR;
406117855Ssam		}
407109396Simp		/*
408109396Simp		 * version 0.8.3 and newer are the only ones that are known
409109396Simp		 * to currently work.  Earlier versions can be made to work,
410109396Simp		 * at least according to the Linux driver.
411109396Simp		 */
412100734Simp		if (sc->sc_sta_firmware_ver >= 803)
413116951Ssam			ic->ic_caps |= IEEE80211_C_HOSTAP;
414109323Ssam		sc->sc_ibss_port = htole16(0);
415119784Ssam
416119784Ssam		sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
417119784Ssam		sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
418119784Ssam		sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
41998440Simp		break;
420109323Ssam
42198440Simp	case WI_SYMBOL:
422112363Simp		sc->sc_ntxbuf = 1;
423109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY;
42498440Simp		if (sc->sc_sta_firmware_ver >= 25000)
425116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
426109323Ssam		sc->sc_ibss_port = htole16(4);
427119784Ssam
428119784Ssam		sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
429119784Ssam		sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
430119784Ssam		sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
43198440Simp		break;
43298440Simp	}
43398440Simp
43498440Simp	/*
43556965Swpaul	 * Find out if we support WEP on this card.
43656965Swpaul	 */
437109323Ssam	buflen = sizeof(val);
438109323Ssam	if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
439109323Ssam	    val != htole16(0))
440116951Ssam		ic->ic_caps |= IEEE80211_C_WEP;
44156965Swpaul
442109323Ssam	/* Find supported rates. */
443109323Ssam	buflen = sizeof(ratebuf);
444116951Ssam	rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
445109323Ssam	if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
446116951Ssam		nrates = le16toh(*(u_int16_t *)ratebuf);
447116951Ssam		if (nrates > IEEE80211_RATE_MAXSIZE)
448116951Ssam			nrates = IEEE80211_RATE_MAXSIZE;
449116951Ssam		rs->rs_nrates = 0;
450116951Ssam		for (i = 0; i < nrates; i++)
451116951Ssam			if (ratebuf[2+i])
452116951Ssam				rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i];
453109323Ssam	} else {
454109323Ssam		/* XXX fallback on error? */
455116951Ssam		rs->rs_nrates = 0;
45698440Simp	}
45777217Sphk
458109323Ssam	buflen = sizeof(val);
459109323Ssam	if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
460109323Ssam	    wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
461119784Ssam		sc->sc_dbm_offset = le16toh(val);
462119784Ssam	}
46346492Swpaul
464109323Ssam	sc->sc_max_datalen = 2304;
465109323Ssam	sc->sc_system_scale = 1;
466109323Ssam	sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN;
467109323Ssam	sc->sc_roaming_mode = 1;
46846492Swpaul
469109323Ssam	sc->sc_portnum = WI_DEFAULT_PORT;
470109323Ssam	sc->sc_authtype = WI_DEFAULT_AUTHTYPE;
47187383Simp
472109323Ssam	bzero(sc->sc_nodename, sizeof(sc->sc_nodename));
473109323Ssam	sc->sc_nodelen = sizeof(WI_DEFAULT_NODENAME) - 1;
474109323Ssam	bcopy(WI_DEFAULT_NODENAME, sc->sc_nodename, sc->sc_nodelen);
47587383Simp
476109323Ssam	bzero(sc->sc_net_name, sizeof(sc->sc_net_name));
477109323Ssam	bcopy(WI_DEFAULT_NETNAME, sc->sc_net_name,
478109323Ssam	    sizeof(WI_DEFAULT_NETNAME) - 1);
47993733Simp
48093733Simp	/*
481109323Ssam	 * Call MI attach routine.
48293733Simp	 */
483138571Ssam	ieee80211_ifattach(ic);
484117812Ssam	/* override state transition method */
485117812Ssam	sc->sc_newstate = ic->ic_newstate;
486117812Ssam	ic->ic_newstate = wi_newstate;
487138571Ssam	ieee80211_media_init(ic, wi_media_change, wi_media_status);
488109323Ssam
489119784Ssam#if NBPFILTER > 0
490119784Ssam	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
491119784Ssam		sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
492119784Ssam		&sc->sc_drvbpf);
493119784Ssam	/*
494119784Ssam	 * Initialize constant fields.
495127698Ssam	 * XXX make header lengths a multiple of 32-bits so subsequent
496127698Ssam	 *     headers are properly aligned; this is a kludge to keep
497127698Ssam	 *     certain applications happy.
498119784Ssam	 *
499119784Ssam	 * NB: the channel is setup each time we transition to the
500119784Ssam	 *     RUN state to avoid filling it in for each frame.
501119784Ssam	 */
502127698Ssam	sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t));
503127698Ssam	sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len);
504127698Ssam	sc->sc_tx_th.wt_ihdr.it_present = htole32(WI_TX_RADIOTAP_PRESENT);
505119784Ssam
506127698Ssam	sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t));
507127698Ssam	sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len);
508127698Ssam	sc->sc_rx_th.wr_ihdr.it_present = htole32(WI_RX_RADIOTAP_PRESENT);
509119784Ssam#endif
510138571Ssam
511138571Ssam	if (bootverbose)
512138571Ssam		ieee80211_announce(ic);
513138571Ssam
514109323Ssam	return (0);
51587383Simp}
51687383Simp
517109323Ssamint
518109323Ssamwi_detach(device_t dev)
51946492Swpaul{
520109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
521147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
522109323Ssam	WI_LOCK_DECL();
52346492Swpaul
524109323Ssam	WI_LOCK(sc);
52546492Swpaul
526109323Ssam	/* check if device was removed */
527123098Simp	sc->wi_gone |= !bus_child_present(dev);
52846492Swpaul
529109323Ssam	wi_stop(ifp, 0);
53046492Swpaul
531119784Ssam#if NBPFILTER > 0
532119784Ssam	bpfdetach(ifp);
533119784Ssam#endif
534138571Ssam	ieee80211_ifdetach(&sc->sc_ic);
535147256Sbrooks	if_free(sc->sc_ifp);
536114190Simp	WI_UNLOCK(sc);
537109323Ssam	bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
538109323Ssam	wi_free(dev);
539109323Ssam#if __FreeBSD_version >= 500000
540109323Ssam	mtx_destroy(&sc->sc_mtx);
54193359Simp#endif
542109323Ssam	return (0);
543109323Ssam}
54493359Simp
545109323Ssam#ifdef __NetBSD__
546109323Ssamint
547109323Ssamwi_activate(struct device *self, enum devact act)
548109323Ssam{
549109323Ssam	struct wi_softc *sc = (struct wi_softc *)self;
550109323Ssam	int rv = 0, s;
55193359Simp
552109323Ssam	s = splnet();
553109323Ssam	switch (act) {
554109323Ssam	case DVACT_ACTIVATE:
555109323Ssam		rv = EOPNOTSUPP;
556109323Ssam		break;
55793359Simp
558109323Ssam	case DVACT_DEACTIVATE:
559147256Sbrooks		if_deactivate(sc->sc_ifp);
560109323Ssam		break;
561109323Ssam	}
562109323Ssam	splx(s);
563109323Ssam	return rv;
564109323Ssam}
56593359Simp
566109323Ssamvoid
567109323Ssamwi_power(struct wi_softc *sc, int why)
568109323Ssam{
569147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
570109323Ssam	int s;
57193359Simp
572109323Ssam	s = splnet();
573109323Ssam	switch (why) {
574109323Ssam	case PWR_SUSPEND:
575109323Ssam	case PWR_STANDBY:
576109323Ssam		wi_stop(ifp, 1);
577109323Ssam		break;
578109323Ssam	case PWR_RESUME:
579109323Ssam		if (ifp->if_flags & IFF_UP) {
580109323Ssam			wi_init(ifp);
581109323Ssam			(void)wi_intr(sc);
58294405Simp		}
583109323Ssam		break;
584109323Ssam	case PWR_SOFTSUSPEND:
585109323Ssam	case PWR_SOFTSTANDBY:
586109323Ssam	case PWR_SOFTRESUME:
587109323Ssam		break;
58893359Simp	}
589109323Ssam	splx(s);
59046492Swpaul}
591109323Ssam#endif /* __NetBSD__ */
59246492Swpaul
593109323Ssamvoid
594109323Ssamwi_shutdown(device_t dev)
59546492Swpaul{
596109323Ssam	struct wi_softc *sc = device_get_softc(dev);
59746492Swpaul
598147256Sbrooks	wi_stop(sc->sc_ifp, 1);
59946492Swpaul}
60046492Swpaul
601109323Ssamvoid
602109323Ssamwi_intr(void *arg)
60346492Swpaul{
604109323Ssam	struct wi_softc *sc = arg;
605147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
606112363Simp	u_int16_t status;
607109323Ssam	WI_LOCK_DECL();
60846492Swpaul
609109323Ssam	WI_LOCK(sc);
61046492Swpaul
611123098Simp	if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) {
612113327Simp		CSR_WRITE_2(sc, WI_INT_EN, 0);
613123098Simp		CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
614109323Ssam		WI_UNLOCK(sc);
61546492Swpaul		return;
616109323Ssam	}
61746492Swpaul
618112363Simp	/* Disable interrupts. */
619112363Simp	CSR_WRITE_2(sc, WI_INT_EN, 0);
62046492Swpaul
621112363Simp	status = CSR_READ_2(sc, WI_EVENT_STAT);
622112363Simp	if (status & WI_EV_RX)
623112363Simp		wi_rx_intr(sc);
624112363Simp	if (status & WI_EV_ALLOC)
625112363Simp		wi_tx_intr(sc);
626112363Simp	if (status & WI_EV_TX_EXC)
627112363Simp		wi_tx_ex_intr(sc);
628112363Simp	if (status & WI_EV_INFO)
629112363Simp		wi_info_intr(sc);
630112363Simp	if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
631112363Simp	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 &&
632132986Smlaier	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
633112363Simp		wi_start(ifp);
63446492Swpaul
635112363Simp	/* Re-enable interrupts. */
636112363Simp	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
63746492Swpaul
638109323Ssam	WI_UNLOCK(sc);
63946492Swpaul
64046492Swpaul	return;
64146492Swpaul}
64246492Swpaul
643109323Ssamvoid
644109323Ssamwi_init(void *arg)
64546492Swpaul{
646109323Ssam	struct wi_softc *sc = arg;
647147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
648109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
649109323Ssam	struct wi_joinreq join;
650109323Ssam	int i;
651109323Ssam	int error = 0, wasenabled;
652109323Ssam	struct ifaddr *ifa;
653109323Ssam	struct sockaddr_dl *sdl;
654109323Ssam	WI_LOCK_DECL();
65546492Swpaul
656109323Ssam	WI_LOCK(sc);
65767092Swpaul
658109323Ssam	if (sc->wi_gone) {
659109323Ssam		WI_UNLOCK(sc);
66046492Swpaul		return;
66146492Swpaul	}
66246492Swpaul
663112362Simp	if ((wasenabled = sc->sc_enabled))
664123098Simp		wi_stop(ifp, 1);
665112362Simp	wi_reset(sc);
66646492Swpaul
667109323Ssam	/* common 802.11 configuration */
668109323Ssam	ic->ic_flags &= ~IEEE80211_F_IBSSON;
669109323Ssam	sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
670109323Ssam	switch (ic->ic_opmode) {
671109323Ssam	case IEEE80211_M_STA:
672109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS);
673109323Ssam		break;
674109323Ssam	case IEEE80211_M_IBSS:
675109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port);
676109323Ssam		ic->ic_flags |= IEEE80211_F_IBSSON;
677109323Ssam		break;
678109323Ssam	case IEEE80211_M_AHDEMO:
679109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
680109323Ssam		break;
681109323Ssam	case IEEE80211_M_HOSTAP:
682122087Sgreen		/*
683122087Sgreen		 * For PRISM cards, override the empty SSID, because in
684122087Sgreen		 * HostAP mode the controller will lock up otherwise.
685122087Sgreen		 */
686122087Sgreen		if (sc->sc_firmware_type == WI_INTERSIL &&
687122087Sgreen		    ic->ic_des_esslen == 0) {
688122087Sgreen			ic->ic_des_essid[0] = ' ';
689122087Sgreen			ic->ic_des_esslen = 1;
690122087Sgreen		}
691109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP);
692109323Ssam		break;
693117855Ssam	case IEEE80211_M_MONITOR:
694117855Ssam		if (sc->sc_firmware_type == WI_LUCENT)
695117855Ssam			wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
696117855Ssam		wi_cmd(sc, WI_CMD_DEBUG | (WI_TEST_MONITOR << 8), 0, 0, 0);
697117855Ssam		break;
69846492Swpaul	}
69946492Swpaul
700109323Ssam	/* Intersil interprets this RID as joining ESS even in IBSS mode */
701109323Ssam	if (sc->sc_firmware_type == WI_LUCENT &&
702109323Ssam	    (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0)
703109323Ssam		wi_write_val(sc, WI_RID_CREATE_IBSS, 1);
704109323Ssam	else
705109323Ssam		wi_write_val(sc, WI_RID_CREATE_IBSS, 0);
706109323Ssam	wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
707109323Ssam	wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid,
708109323Ssam	    ic->ic_des_esslen);
709116951Ssam	wi_write_val(sc, WI_RID_OWN_CHNL,
710116951Ssam		ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
711109323Ssam	wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen);
71246492Swpaul
713109323Ssam	ifa = ifaddr_byindex(ifp->if_index);
714109323Ssam	sdl = (struct sockaddr_dl *) ifa->ifa_addr;
715109323Ssam	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(sdl));
716109323Ssam	wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN);
71775373Salfred
718138571Ssam	if (ic->ic_caps & IEEE80211_C_PMGT)
719138571Ssam		wi_write_val(sc, WI_RID_PM_ENABLED,
720138571Ssam		    (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
72146492Swpaul
722109323Ssam	/* not yet common 802.11 configuration */
723109323Ssam	wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen);
724116951Ssam	wi_write_val(sc, WI_RID_RTS_THRESH, ic->ic_rtsthreshold);
725109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
726116951Ssam		wi_write_val(sc, WI_RID_FRAG_THRESH, ic->ic_fragthreshold);
72746492Swpaul
728109323Ssam	/* driver specific 802.11 configuration */
729109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)
730109323Ssam		wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale);
731109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
732109323Ssam		wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode);
733109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_MOR)
734109323Ssam		wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven);
735109323Ssam	wi_write_txrate(sc);
736109323Ssam	wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen);
73746492Swpaul
738109323Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
739109323Ssam	    sc->sc_firmware_type == WI_INTERSIL) {
740109323Ssam		wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval);
741109323Ssam		wi_write_val(sc, WI_RID_BASIC_RATE, 0x03);   /* 1, 2 */
742109323Ssam		wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */
743109323Ssam		wi_write_val(sc, WI_RID_DTIM_PERIOD, 1);
74446492Swpaul	}
74546492Swpaul
746109323Ssam	/*
747109323Ssam	 * Initialize promisc mode.
748109323Ssam	 *	Being in the Host-AP mode causes a great
749109323Ssam	 *	deal of pain if primisc mode is set.
750109323Ssam	 *	Therefore we avoid confusing the firmware
751109323Ssam	 *	and always reset promisc mode in Host-AP
752109323Ssam	 *	mode.  Host-AP sees all the packets anyway.
753109323Ssam	 */
754109323Ssam	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
755109323Ssam	    (ifp->if_flags & IFF_PROMISC) != 0) {
756109323Ssam		wi_write_val(sc, WI_RID_PROMISC, 1);
757109323Ssam	} else {
758109323Ssam		wi_write_val(sc, WI_RID_PROMISC, 0);
75975373Salfred	}
76046492Swpaul
761109323Ssam	/* Configure WEP. */
762146884Savatar	if (ic->ic_caps & IEEE80211_C_WEP) {
763146884Savatar		sc->sc_cnfauthmode = ic->ic_bss->ni_authmode;
764109323Ssam		wi_write_wep(sc);
765146884Savatar	}
76667092Swpaul
767109323Ssam	/* Set multicast filter. */
768109323Ssam	wi_write_multi(sc);
76946492Swpaul
770114124Simp	/* Allocate fids for the card */
771109323Ssam	if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
772109323Ssam		sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
773109323Ssam		if (sc->sc_firmware_type == WI_SYMBOL)
774109323Ssam			sc->sc_buflen = 1585;	/* XXX */
775112363Simp		for (i = 0; i < sc->sc_ntxbuf; i++) {
776109323Ssam			error = wi_alloc_fid(sc, sc->sc_buflen,
777109323Ssam			    &sc->sc_txd[i].d_fid);
778109323Ssam			if (error) {
779109323Ssam				device_printf(sc->sc_dev,
780109323Ssam				    "tx buffer allocation failed (error %u)\n",
781109323Ssam				    error);
782109323Ssam				goto out;
783109323Ssam			}
784109323Ssam			sc->sc_txd[i].d_len = 0;
78570073Swpaul		}
78670073Swpaul	}
787109323Ssam	sc->sc_txcur = sc->sc_txnext = 0;
78870073Swpaul
789109323Ssam	/* Enable desired port */
790109323Ssam	wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
79146492Swpaul
792114124Simp	sc->sc_enabled = 1;
793109323Ssam	ifp->if_flags |= IFF_RUNNING;
794109323Ssam	ifp->if_flags &= ~IFF_OACTIVE;
795109323Ssam	if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
796139542Ssam	    ic->ic_opmode == IEEE80211_M_IBSS ||
797117855Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR ||
798109323Ssam	    ic->ic_opmode == IEEE80211_M_HOSTAP)
799139542Ssam		ieee80211_create_ibss(ic, ic->ic_ibss_chan);
80046492Swpaul
801109323Ssam	/* Enable interrupts */
802109323Ssam	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
80346492Swpaul
804109323Ssam	if (!wasenabled &&
805109323Ssam	    ic->ic_opmode == IEEE80211_M_HOSTAP &&
806109323Ssam	    sc->sc_firmware_type == WI_INTERSIL) {
807109323Ssam		/* XXX: some card need to be re-enabled for hostap */
808109323Ssam		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
809109323Ssam		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
81075149Simp	}
81194397Simp
812109323Ssam	if (ic->ic_opmode == IEEE80211_M_STA &&
813109323Ssam	    ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
814116951Ssam	    ic->ic_des_chan != IEEE80211_CHAN_ANYC)) {
815109323Ssam		memset(&join, 0, sizeof(join));
816109323Ssam		if (ic->ic_flags & IEEE80211_F_DESBSSID)
817109323Ssam			IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
818116951Ssam		if (ic->ic_des_chan != IEEE80211_CHAN_ANYC)
819116951Ssam			join.wi_chan = htole16(
820116951Ssam				ieee80211_chan2ieee(ic, ic->ic_des_chan));
821109323Ssam		/* Lucent firmware does not support the JOIN RID. */
822109323Ssam		if (sc->sc_firmware_type != WI_LUCENT)
823109323Ssam			wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
82494397Simp	}
82575373Salfred
826109323Ssam	WI_UNLOCK(sc);
82746492Swpaul	return;
828109323Ssamout:
829109323Ssam	if (error) {
830109323Ssam		if_printf(ifp, "interface not running\n");
831123098Simp		wi_stop(ifp, 1);
83270073Swpaul	}
833110224Simp	WI_UNLOCK(sc);
834109323Ssam	DPRINTF(("wi_init: return %d\n", error));
835109323Ssam	return;
83646492Swpaul}
83746492Swpaul
838109323Ssamvoid
839109323Ssamwi_stop(struct ifnet *ifp, int disable)
84046492Swpaul{
841109323Ssam	struct wi_softc *sc = ifp->if_softc;
842138571Ssam	struct ieee80211com *ic = &sc->sc_ic;
843109323Ssam	WI_LOCK_DECL();
84446492Swpaul
845109323Ssam	WI_LOCK(sc);
84674998Swpaul
847123098Simp	DELAY(100000);
848123098Simp
849117812Ssam	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
850109323Ssam	if (sc->sc_enabled && !sc->wi_gone) {
851109323Ssam		CSR_WRITE_2(sc, WI_INT_EN, 0);
852109323Ssam		wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
853109323Ssam		if (disable) {
854109323Ssam#ifdef __NetBSD__
855109323Ssam			if (sc->sc_disable)
856109323Ssam				(*sc->sc_disable)(sc);
857109323Ssam#endif
858116951Ssam			sc->sc_enabled = 0;
85970073Swpaul		}
860123098Simp	} else if (sc->wi_gone && disable)	/* gone --> not enabled */
861123098Simp	    sc->sc_enabled = 0;
86270073Swpaul
863109323Ssam	sc->sc_tx_timer = 0;
864109323Ssam	sc->sc_scan_timer = 0;
865109323Ssam	sc->sc_false_syns = 0;
866109323Ssam	sc->sc_naps = 0;
867109323Ssam	ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
868109323Ssam	ifp->if_timer = 0;
86946492Swpaul
870109323Ssam	WI_UNLOCK(sc);
87146492Swpaul}
87246492Swpaul
873109323Ssamstatic void
874109323Ssamwi_start(struct ifnet *ifp)
87546492Swpaul{
876109323Ssam	struct wi_softc	*sc = ifp->if_softc;
877109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
878119150Ssam	struct ieee80211_node *ni;
879109323Ssam	struct ieee80211_frame *wh;
880138571Ssam	struct ether_header *eh;
881109323Ssam	struct mbuf *m0;
882109323Ssam	struct wi_frame frmhdr;
883119150Ssam	int cur, fid, off, error;
884109323Ssam	WI_LOCK_DECL();
88546492Swpaul
886109323Ssam	WI_LOCK(sc);
88746492Swpaul
888109323Ssam	if (sc->wi_gone) {
889109323Ssam		WI_UNLOCK(sc);
890109323Ssam		return;
89146492Swpaul	}
892109323Ssam	if (sc->sc_flags & WI_FLAGS_OUTRANGE) {
893109323Ssam		WI_UNLOCK(sc);
894109323Ssam		return;
89575373Salfred	}
89646492Swpaul
897109323Ssam	memset(&frmhdr, 0, sizeof(frmhdr));
898109323Ssam	cur = sc->sc_txnext;
899109323Ssam	for (;;) {
900109323Ssam		IF_POLL(&ic->ic_mgtq, m0);
901109323Ssam		if (m0 != NULL) {
902109323Ssam			if (sc->sc_txd[cur].d_len != 0) {
903109323Ssam				ifp->if_flags |= IFF_OACTIVE;
904109323Ssam				break;
905109323Ssam			}
906109323Ssam			IF_DEQUEUE(&ic->ic_mgtq, m0);
907119150Ssam			/*
908119150Ssam			 * Hack!  The referenced node pointer is in the
909119150Ssam			 * rcvif field of the packet header.  This is
910119150Ssam			 * placed there by ieee80211_mgmt_output because
911119150Ssam			 * we need to hold the reference with the frame
912119150Ssam			 * and there's no other way (other than packet
913119150Ssam			 * tags which we consider too expensive to use)
914119150Ssam			 * to pass it along.
915119150Ssam			 */
916119150Ssam			ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
917119150Ssam			m0->m_pkthdr.rcvif = NULL;
918119150Ssam
919109323Ssam			m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
920109323Ssam			    (caddr_t)&frmhdr.wi_ehdr);
921109323Ssam			frmhdr.wi_ehdr.ether_type = 0;
922109323Ssam                        wh = mtod(m0, struct ieee80211_frame *);
923109323Ssam		} else {
924109323Ssam			if (ic->ic_state != IEEE80211_S_RUN)
925109323Ssam				break;
926132986Smlaier			IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
927109323Ssam			if (m0 == NULL)
928109323Ssam				break;
929109323Ssam			if (sc->sc_txd[cur].d_len != 0) {
930132986Smlaier				IFQ_DRV_PREPEND(&ifp->if_snd, m0);
931109323Ssam				ifp->if_flags |= IFF_OACTIVE;
932109323Ssam				break;
933109323Ssam			}
934138571Ssam			if (m0->m_len < sizeof(struct ether_header) &&
935138571Ssam			    (m0 = m_pullup(m0, sizeof(struct ether_header))) == NULL) {
936138571Ssam				ifp->if_oerrors++;
937138571Ssam				continue;
938138571Ssam			}
939138571Ssam			eh = mtod(m0, struct ether_header *);
940138571Ssam			ni = ieee80211_find_txnode(ic, eh->ether_dhost);
941138571Ssam			if (ni == NULL) {
942138571Ssam				m_freem(m0);
943138571Ssam				continue;
944138571Ssam			}
945109323Ssam			ifp->if_opackets++;
946109323Ssam			m_copydata(m0, 0, ETHER_HDR_LEN,
947109323Ssam			    (caddr_t)&frmhdr.wi_ehdr);
948109323Ssam#if NBPFILTER > 0
949109323Ssam			BPF_MTAP(ifp, m0);
950109323Ssam#endif
95146492Swpaul
952138571Ssam			m0 = ieee80211_encap(ic, m0, ni);
953119150Ssam			if (m0 == NULL) {
954109323Ssam				ifp->if_oerrors++;
955138571Ssam				ieee80211_free_node(ni);
956109323Ssam				continue;
957109323Ssam			}
958109323Ssam                        wh = mtod(m0, struct ieee80211_frame *);
959109323Ssam		}
960109323Ssam#if NBPFILTER > 0
961109323Ssam		if (ic->ic_rawbpf)
962109323Ssam			bpf_mtap(ic->ic_rawbpf, m0);
963109323Ssam#endif
964109323Ssam		frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
965138952Ssam		/* XXX check key for SWCRYPT instead of using operating mode */
966138952Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
967138952Ssam		    (wh->i_fc[1] & IEEE80211_FC1_WEP)) {
968138571Ssam			struct ieee80211_key *k;
969138571Ssam
970138571Ssam			k = ieee80211_crypto_encap(ic, ni, m0);
971138571Ssam			if (k == NULL) {
972138571Ssam				if (ni != NULL)
973138571Ssam					ieee80211_free_node(ni);
974143299Ssam				m_freem(m0);
975109323Ssam				continue;
976109323Ssam			}
977138952Ssam			frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
978109323Ssam		}
979109323Ssam#if NBPFILTER > 0
980109323Ssam		if (sc->sc_drvbpf) {
981123927Ssam			sc->sc_tx_th.wt_rate =
982123927Ssam				ni->ni_rates.rs_rates[ni->ni_txrate];
983123922Ssam			bpf_mtap2(sc->sc_drvbpf,
984127698Ssam				&sc->sc_tx_th, sc->sc_tx_th_len, m0);
985109323Ssam		}
98646492Swpaul#endif
987127697Ssam		m_copydata(m0, 0, sizeof(struct ieee80211_frame),
988127697Ssam		    (caddr_t)&frmhdr.wi_whdr);
989127697Ssam		m_adj(m0, sizeof(struct ieee80211_frame));
990127697Ssam		frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
991109323Ssam		if (IFF_DUMPPKTS(ifp))
992116951Ssam			wi_dump_pkt(&frmhdr, NULL, -1);
993109323Ssam		fid = sc->sc_txd[cur].d_fid;
994109323Ssam		off = sizeof(frmhdr);
995119150Ssam		error = wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0
996119150Ssam		     || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
997119150Ssam		m_freem(m0);
998138571Ssam		if (ni != NULL)
999138571Ssam			ieee80211_free_node(ni);
1000119150Ssam		if (error) {
1001109323Ssam			ifp->if_oerrors++;
1002109323Ssam			continue;
1003109323Ssam		}
1004109323Ssam		sc->sc_txd[cur].d_len = off;
1005109323Ssam		if (sc->sc_txcur == cur) {
1006109323Ssam			if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
1007109323Ssam				if_printf(ifp, "xmit failed\n");
1008109323Ssam				sc->sc_txd[cur].d_len = 0;
1009109323Ssam				continue;
1010109323Ssam			}
1011109323Ssam			sc->sc_tx_timer = 5;
1012109323Ssam			ifp->if_timer = 1;
1013109323Ssam		}
1014112363Simp		sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
101574838Salfred	}
101646492Swpaul
1017109323Ssam	WI_UNLOCK(sc);
101846492Swpaul}
101946492Swpaul
102088546Salfredstatic int
1021109323Ssamwi_reset(struct wi_softc *sc)
102246492Swpaul{
1023147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1024114124Simp#define WI_INIT_TRIES 3
1025114124Simp	int i;
1026114138Simp	int error = 0;
1027114124Simp	int tries;
1028114124Simp
1029114124Simp	/* Symbol firmware cannot be initialized more than once */
1030123098Simp	if (sc->sc_firmware_type == WI_SYMBOL && sc->sc_reset)
1031112362Simp		return (0);
1032114124Simp	if (sc->sc_firmware_type == WI_SYMBOL)
1033114124Simp		tries = 1;
1034114124Simp	else
1035114124Simp		tries = WI_INIT_TRIES;
1036112362Simp
1037114124Simp	for (i = 0; i < tries; i++) {
1038109323Ssam		if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
103946492Swpaul			break;
1040109323Ssam		DELAY(WI_DELAY * 1000);
104146492Swpaul	}
1042114124Simp	sc->sc_reset = 1;
104346492Swpaul
1044114124Simp	if (i == tries) {
1045114124Simp		if_printf(ifp, "init failed\n");
1046114124Simp		return (error);
104775373Salfred	}
104846492Swpaul
1049109323Ssam	CSR_WRITE_2(sc, WI_INT_EN, 0);
1050114124Simp	CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
105146492Swpaul
1052109323Ssam	/* Calibrate timer. */
1053114124Simp	wi_write_val(sc, WI_RID_TICK_TIME, 8);
1054114124Simp
1055114124Simp	return (0);
1056109323Ssam#undef WI_INIT_TRIES
105746492Swpaul}
105846492Swpaul
105988546Salfredstatic void
1060109323Ssamwi_watchdog(struct ifnet *ifp)
106146492Swpaul{
1062109323Ssam	struct wi_softc	*sc = ifp->if_softc;
106346492Swpaul
1064109323Ssam	ifp->if_timer = 0;
1065109323Ssam	if (!sc->sc_enabled)
1066109323Ssam		return;
106746492Swpaul
1068109323Ssam	if (sc->sc_tx_timer) {
1069109323Ssam		if (--sc->sc_tx_timer == 0) {
1070109323Ssam			if_printf(ifp, "device timeout\n");
1071109323Ssam			ifp->if_oerrors++;
1072109323Ssam			wi_init(ifp->if_softc);
1073109323Ssam			return;
1074109323Ssam		}
1075109323Ssam		ifp->if_timer = 1;
107646492Swpaul	}
107746492Swpaul
1078109323Ssam	if (sc->sc_scan_timer) {
1079109323Ssam		if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
1080109323Ssam		    sc->sc_firmware_type == WI_INTERSIL) {
1081109323Ssam			DPRINTF(("wi_watchdog: inquire scan\n"));
1082109323Ssam			wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
108346492Swpaul		}
1084109323Ssam		if (sc->sc_scan_timer)
1085109323Ssam			ifp->if_timer = 1;
108646492Swpaul	}
108746492Swpaul
1088109323Ssam	/* TODO: rate control */
1089138571Ssam	ieee80211_watchdog(&sc->sc_ic);
109046492Swpaul}
109146492Swpaul
109288546Salfredstatic int
1093109323Ssamwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
109446492Swpaul{
1095109323Ssam	struct wi_softc *sc = ifp->if_softc;
1096109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1097109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
1098109323Ssam	struct ieee80211req *ireq;
1099109323Ssam	u_int8_t nodename[IEEE80211_NWID_LEN];
1100109323Ssam	int error = 0;
110195534Simp#if __FreeBSD_version >= 500000
1102109323Ssam	struct thread *td = curthread;
110395534Simp#else
1104109323Ssam	struct proc *td = curproc;		/* Little white lie */
110595534Simp#endif
1106109323Ssam	struct wi_req wreq;
1107109323Ssam	WI_LOCK_DECL();
110846492Swpaul
1109138571Ssam	if (sc->wi_gone)
1110138571Ssam		return (ENODEV);
111146492Swpaul
1112109323Ssam	switch (cmd) {
111346492Swpaul	case SIOCSIFFLAGS:
1114100876Simp		/*
1115101139Simp		 * Can't do promisc and hostap at the same time.  If all that's
1116101139Simp		 * changing is the promisc flag, try to short-circuit a call to
1117101139Simp		 * wi_init() by just setting PROMISC in the hardware.
1118100876Simp		 */
1119138571Ssam		WI_LOCK(sc);
112046492Swpaul		if (ifp->if_flags & IFF_UP) {
1121109323Ssam			if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1122101139Simp			    ifp->if_flags & IFF_RUNNING) {
1123101139Simp				if (ifp->if_flags & IFF_PROMISC &&
1124109323Ssam				    !(sc->sc_if_flags & IFF_PROMISC)) {
1125109323Ssam					wi_write_val(sc, WI_RID_PROMISC, 1);
1126101139Simp				} else if (!(ifp->if_flags & IFF_PROMISC) &&
1127109323Ssam				    sc->sc_if_flags & IFF_PROMISC) {
1128109323Ssam					wi_write_val(sc, WI_RID_PROMISC, 0);
1129101139Simp				} else {
1130101139Simp					wi_init(sc);
1131101139Simp				}
1132100876Simp			} else {
113346492Swpaul				wi_init(sc);
1134100876Simp			}
113546492Swpaul		} else {
113646492Swpaul			if (ifp->if_flags & IFF_RUNNING) {
1137123098Simp				wi_stop(ifp, 1);
113846492Swpaul			}
1139123098Simp			sc->wi_gone = 0;
114046492Swpaul		}
1141109323Ssam		sc->sc_if_flags = ifp->if_flags;
1142138571Ssam		WI_UNLOCK(sc);
114346492Swpaul		error = 0;
114446492Swpaul		break;
114546492Swpaul	case SIOCADDMULTI:
114646492Swpaul	case SIOCDELMULTI:
1147138571Ssam		WI_LOCK(sc);
1148109323Ssam		error = wi_write_multi(sc);
1149138571Ssam		WI_UNLOCK(sc);
115046492Swpaul		break;
1151109323Ssam	case SIOCGIFGENERIC:
1152138571Ssam		WI_LOCK(sc);
1153109323Ssam		error = wi_get_cfg(ifp, cmd, data);
1154138571Ssam		WI_UNLOCK(sc);
115546492Swpaul		break;
1156109323Ssam	case SIOCSIFGENERIC:
1157109323Ssam		error = suser(td);
1158138571Ssam		if (error == 0)
1159138571Ssam			error = wi_set_cfg(ifp, cmd, data);
116046492Swpaul		break;
116193359Simp	case SIOCGPRISM2DEBUG:
116293359Simp		error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
116393359Simp		if (error)
116493359Simp			break;
116593733Simp		if (!(ifp->if_flags & IFF_RUNNING) ||
116693733Simp		    sc->sc_firmware_type == WI_LUCENT) {
116793359Simp			error = EIO;
116893359Simp			break;
116993359Simp		}
117093359Simp		error = wi_get_debug(sc, &wreq);
117193359Simp		if (error == 0)
117293359Simp			error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
117393359Simp		break;
117493359Simp	case SIOCSPRISM2DEBUG:
117593593Sjhb		if ((error = suser(td)))
1176138571Ssam			return (error);
117793359Simp		error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
117893359Simp		if (error)
117993359Simp			break;
1180138571Ssam		WI_LOCK(sc);
118193359Simp		error = wi_set_debug(sc, &wreq);
1182138571Ssam		WI_UNLOCK(sc);
118393359Simp		break;
118477217Sphk	case SIOCG80211:
1185109323Ssam		ireq = (struct ieee80211req *) data;
1186138571Ssam		if (ireq->i_type == IEEE80211_IOC_STATIONNAME) {
1187109323Ssam			ireq->i_len = sc->sc_nodelen + 1;
1188109323Ssam			error = copyout(sc->sc_nodename, ireq->i_data,
1189109323Ssam					ireq->i_len);
119077217Sphk			break;
119177217Sphk		}
1192138571Ssam		goto ioctl_common;
119377217Sphk	case SIOCS80211:
1194109323Ssam		ireq = (struct ieee80211req *) data;
1195138571Ssam		if (ireq->i_type == IEEE80211_IOC_STATIONNAME) {
1196138571Ssam			error = suser(td);
1197138571Ssam			if (error)
1198138571Ssam				break;
119977217Sphk			if (ireq->i_val != 0 ||
120077217Sphk			    ireq->i_len > IEEE80211_NWID_LEN) {
120177217Sphk				error = EINVAL;
120277217Sphk				break;
120377217Sphk			}
1204109323Ssam			memset(nodename, 0, IEEE80211_NWID_LEN);
1205109323Ssam			error = copyin(ireq->i_data, nodename, ireq->i_len);
1206109323Ssam			if (error)
120777217Sphk				break;
1208138571Ssam			WI_LOCK(sc);
1209109323Ssam			if (sc->sc_enabled) {
1210109323Ssam				error = wi_write_ssid(sc, WI_RID_NODENAME,
1211109323Ssam					nodename, ireq->i_len);
121277217Sphk			}
1213138571Ssam			if (error == 0) {
1214138571Ssam				memcpy(sc->sc_nodename, nodename,
1215138571Ssam					IEEE80211_NWID_LEN);
1216138571Ssam				sc->sc_nodelen = ireq->i_len;
1217138571Ssam			}
1218138571Ssam			WI_UNLOCK(sc);
121977217Sphk			break;
122077217Sphk		}
1221138571Ssam		goto ioctl_common;
122246492Swpaul	default:
1223138571Ssam	ioctl_common:
1224138571Ssam		WI_LOCK(sc);
1225138571Ssam		error = ieee80211_ioctl(ic, cmd, data);
1226138571Ssam		if (error == ENETRESET) {
1227138571Ssam			if (sc->sc_enabled)
1228138571Ssam				wi_init(sc);	/* XXX no error return */
1229138571Ssam			error = 0;
1230138571Ssam		}
1231138571Ssam		WI_UNLOCK(sc);
123246492Swpaul		break;
123346492Swpaul	}
1234109323Ssam	return (error);
123546492Swpaul}
123646492Swpaul
1237109323Ssamstatic int
1238109323Ssamwi_media_change(struct ifnet *ifp)
123946492Swpaul{
1240109323Ssam	struct wi_softc *sc = ifp->if_softc;
1241116951Ssam	int error;
124246492Swpaul
1243116951Ssam	error = ieee80211_media_change(ifp);
1244109323Ssam	if (error == ENETRESET) {
1245109323Ssam		if (sc->sc_enabled)
1246116951Ssam			wi_init(sc);	/* XXX no error return */
1247109323Ssam		error = 0;
1248109323Ssam	}
1249109323Ssam	return error;
1250109323Ssam}
125146492Swpaul
1252109323Ssamstatic void
1253109323Ssamwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1254109323Ssam{
1255109323Ssam	struct wi_softc *sc = ifp->if_softc;
1256109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1257109323Ssam	u_int16_t val;
1258109323Ssam	int rate, len;
125946492Swpaul
1260109323Ssam	if (sc->wi_gone || !sc->sc_enabled) {
1261109323Ssam		imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
1262109323Ssam		imr->ifm_status = 0;
1263109323Ssam		return;
1264109323Ssam	}
126546492Swpaul
1266109323Ssam	imr->ifm_status = IFM_AVALID;
1267109323Ssam	imr->ifm_active = IFM_IEEE80211;
1268109323Ssam	if (ic->ic_state == IEEE80211_S_RUN &&
1269109323Ssam	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
1270109323Ssam		imr->ifm_status |= IFM_ACTIVE;
1271109323Ssam	len = sizeof(val);
1272138571Ssam	if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) == 0 &&
1273138571Ssam	    len == sizeof(val)) {
1274109323Ssam		/* convert to 802.11 rate */
1275138571Ssam		val = le16toh(val);
1276109323Ssam		rate = val * 2;
1277109323Ssam		if (sc->sc_firmware_type == WI_LUCENT) {
1278138571Ssam			if (rate == 10)
1279109323Ssam				rate = 11;	/* 5.5Mbps */
1280109323Ssam		} else {
1281109323Ssam			if (rate == 4*2)
1282109323Ssam				rate = 11;	/* 5.5Mbps */
1283109323Ssam			else if (rate == 8*2)
1284109323Ssam				rate = 22;	/* 11Mbps */
1285109323Ssam		}
1286138571Ssam	} else
1287138571Ssam		rate = 0;
1288116951Ssam	imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
1289109323Ssam	switch (ic->ic_opmode) {
1290109323Ssam	case IEEE80211_M_STA:
1291109323Ssam		break;
1292109323Ssam	case IEEE80211_M_IBSS:
1293109323Ssam		imr->ifm_active |= IFM_IEEE80211_ADHOC;
1294109323Ssam		break;
1295109323Ssam	case IEEE80211_M_AHDEMO:
1296109323Ssam		imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1297109323Ssam		break;
1298109323Ssam	case IEEE80211_M_HOSTAP:
1299109323Ssam		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1300109323Ssam		break;
1301117855Ssam	case IEEE80211_M_MONITOR:
1302117855Ssam		imr->ifm_active |= IFM_IEEE80211_MONITOR;
1303117855Ssam		break;
1304109323Ssam	}
1305109323Ssam}
130646492Swpaul
1307109323Ssamstatic void
1308109323Ssamwi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1309109323Ssam{
1310109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1311116951Ssam	struct ieee80211_node *ni = ic->ic_bss;
1312147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
131398440Simp
1314109323Ssam	if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1315109323Ssam		return;
131646492Swpaul
1317109323Ssam	DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid)));
1318109323Ssam	DPRINTF(("%s ?\n", ether_sprintf(new_bssid)));
131946492Swpaul
1320109323Ssam	/* In promiscuous mode, the BSSID field is not a reliable
1321109323Ssam	 * indicator of the firmware's BSSID. Damp spurious
1322109323Ssam	 * change-of-BSSID indications.
1323109323Ssam	 */
1324109323Ssam	if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1325138571Ssam	    !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns,
1326138571Ssam	                 WI_MAX_FALSE_SYNS))
1327109323Ssam		return;
132846492Swpaul
1329139542Ssam	sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1);
1330139542Ssam	/*
1331139542Ssam	 * XXX hack; we should create a new node with the new bssid
1332139542Ssam	 * and replace the existing ic_bss with it but since we don't
1333139542Ssam	 * process management frames to collect state we cheat by
1334139542Ssam	 * reusing the existing node as we know wi_newstate will be
1335139542Ssam	 * called and it will overwrite the node state.
1336139542Ssam	 */
1337139542Ssam	ieee80211_sta_join(ic, ieee80211_ref_node(ni));
1338109323Ssam}
133946492Swpaul
1340109323Ssamstatic void
1341116898Ssamwi_rx_monitor(struct wi_softc *sc, int fid)
1342116898Ssam{
1343147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1344116898Ssam	struct wi_frame *rx_frame;
1345116898Ssam	struct mbuf *m;
1346116898Ssam	int datlen, hdrlen;
1347116898Ssam
1348116898Ssam	/* first allocate mbuf for packet storage */
1349116898Ssam	m = m_getcl(M_DONTWAIT, MT_DATA, 0);
1350116898Ssam	if (m == NULL) {
1351116898Ssam		ifp->if_ierrors++;
1352116898Ssam		return;
1353116898Ssam	}
1354116898Ssam
1355116898Ssam	m->m_pkthdr.rcvif = ifp;
1356116898Ssam
1357116898Ssam	/* now read wi_frame first so we know how much data to read */
1358116898Ssam	if (wi_read_bap(sc, fid, 0, mtod(m, caddr_t), sizeof(*rx_frame))) {
1359116898Ssam		ifp->if_ierrors++;
1360116898Ssam		goto done;
1361116898Ssam	}
1362116898Ssam
1363116898Ssam	rx_frame = mtod(m, struct wi_frame *);
1364116898Ssam
1365116898Ssam	switch ((rx_frame->wi_status & WI_STAT_MAC_PORT) >> 8) {
1366116898Ssam	case 7:
1367116898Ssam		switch (rx_frame->wi_whdr.i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1368116898Ssam		case IEEE80211_FC0_TYPE_DATA:
1369116898Ssam			hdrlen = WI_DATA_HDRLEN;
1370116898Ssam			datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1371116898Ssam			break;
1372116898Ssam		case IEEE80211_FC0_TYPE_MGT:
1373116898Ssam			hdrlen = WI_MGMT_HDRLEN;
1374116898Ssam			datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1375116898Ssam			break;
1376116898Ssam		case IEEE80211_FC0_TYPE_CTL:
1377116898Ssam			/*
1378116898Ssam			 * prism2 cards don't pass control packets
1379116898Ssam			 * down properly or consistently, so we'll only
1380116898Ssam			 * pass down the header.
1381116898Ssam			 */
1382116898Ssam			hdrlen = WI_CTL_HDRLEN;
1383116898Ssam			datlen = 0;
1384116898Ssam			break;
1385116898Ssam		default:
1386116898Ssam			if_printf(ifp, "received packet of unknown type "
1387116898Ssam				"on port 7\n");
1388116898Ssam			ifp->if_ierrors++;
1389116898Ssam			goto done;
1390116898Ssam		}
1391116898Ssam		break;
1392116898Ssam	case 0:
1393116898Ssam		hdrlen = WI_DATA_HDRLEN;
1394116898Ssam		datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1395116898Ssam		break;
1396116898Ssam	default:
1397116898Ssam		if_printf(ifp, "received packet on invalid "
1398116898Ssam		    "port (wi_status=0x%x)\n", rx_frame->wi_status);
1399116898Ssam		ifp->if_ierrors++;
1400116898Ssam		goto done;
1401116898Ssam	}
1402116898Ssam
1403116898Ssam	if (hdrlen + datlen + 2 > MCLBYTES) {
1404116898Ssam		if_printf(ifp, "oversized packet received "
1405116898Ssam		    "(wi_dat_len=%d, wi_status=0x%x)\n",
1406116898Ssam		    datlen, rx_frame->wi_status);
1407116898Ssam		ifp->if_ierrors++;
1408116898Ssam		goto done;
1409116898Ssam	}
1410116898Ssam
1411116898Ssam	if (wi_read_bap(sc, fid, hdrlen, mtod(m, caddr_t) + hdrlen,
1412116898Ssam	    datlen + 2) == 0) {
1413116898Ssam		m->m_pkthdr.len = m->m_len = hdrlen + datlen;
1414116898Ssam		ifp->if_ipackets++;
1415116898Ssam		BPF_MTAP(ifp, m);	/* Handle BPF listeners. */
1416116898Ssam	} else
1417116898Ssam		ifp->if_ierrors++;
1418116898Ssamdone:
1419116898Ssam	m_freem(m);
1420116898Ssam}
1421116898Ssam
1422116898Ssamstatic void
1423109323Ssamwi_rx_intr(struct wi_softc *sc)
1424109323Ssam{
1425109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1426147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1427109323Ssam	struct wi_frame frmhdr;
1428109323Ssam	struct mbuf *m;
1429109323Ssam	struct ieee80211_frame *wh;
1430119150Ssam	struct ieee80211_node *ni;
1431109323Ssam	int fid, len, off, rssi;
1432109323Ssam	u_int8_t dir;
1433109323Ssam	u_int16_t status;
1434109323Ssam	u_int32_t rstamp;
143546611Swpaul
1436109323Ssam	fid = CSR_READ_2(sc, WI_RX_FID);
143746611Swpaul
1438116898Ssam	if (sc->wi_debug.wi_monitor) {
1439116898Ssam		/*
1440116898Ssam		 * If we are in monitor mode just
1441116898Ssam		 * read the data from the device.
1442116898Ssam		 */
1443116898Ssam		wi_rx_monitor(sc, fid);
1444116898Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1445116898Ssam		return;
1446116898Ssam	}
1447116898Ssam
1448109323Ssam	/* First read in the frame header */
1449109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1450109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1451109323Ssam		ifp->if_ierrors++;
1452109323Ssam		DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1453109323Ssam		return;
1454109323Ssam	}
145591695Simp
1456109323Ssam	if (IFF_DUMPPKTS(ifp))
1457109323Ssam		wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal);
145846492Swpaul
1459109323Ssam	/*
1460109323Ssam	 * Drop undecryptable or packets with receive errors here
1461109323Ssam	 */
1462109323Ssam	status = le16toh(frmhdr.wi_status);
1463109323Ssam	if (status & WI_STAT_ERRSTAT) {
1464109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1465109323Ssam		ifp->if_ierrors++;
1466109323Ssam		DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1467109323Ssam		return;
1468109323Ssam	}
1469109323Ssam	rssi = frmhdr.wi_rx_signal;
1470109323Ssam	rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1471109323Ssam	    le16toh(frmhdr.wi_rx_tstamp1);
147246492Swpaul
1473109323Ssam	len = le16toh(frmhdr.wi_dat_len);
1474109323Ssam	off = ALIGN(sizeof(struct ieee80211_frame));
147546563Swpaul
1476117855Ssam	/*
1477117855Ssam	 * Sometimes the PRISM2.x returns bogusly large frames. Except
1478117855Ssam	 * in monitor mode, just throw them away.
1479117855Ssam	 */
1480117855Ssam	if (off + len > MCLBYTES) {
1481117855Ssam		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1482117855Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1483117855Ssam			ifp->if_ierrors++;
1484117855Ssam			DPRINTF(("wi_rx_intr: oversized packet\n"));
1485117855Ssam			return;
1486117855Ssam		} else
1487117855Ssam			len = 0;
1488117855Ssam	}
1489117855Ssam
1490111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
1491109323Ssam	if (m == NULL) {
1492109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1493109323Ssam		ifp->if_ierrors++;
1494109323Ssam		DPRINTF(("wi_rx_intr: MGET failed\n"));
1495109323Ssam		return;
1496109323Ssam	}
1497109323Ssam	if (off + len > MHLEN) {
1498111119Simp		MCLGET(m, M_DONTWAIT);
1499109323Ssam		if ((m->m_flags & M_EXT) == 0) {
1500109323Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1501109323Ssam			m_freem(m);
1502109323Ssam			ifp->if_ierrors++;
1503109323Ssam			DPRINTF(("wi_rx_intr: MCLGET failed\n"));
1504109323Ssam			return;
1505109323Ssam		}
1506109323Ssam	}
150746492Swpaul
1508109323Ssam	m->m_data += off - sizeof(struct ieee80211_frame);
1509109323Ssam	memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1510109323Ssam	wi_read_bap(sc, fid, sizeof(frmhdr),
1511109323Ssam	    m->m_data + sizeof(struct ieee80211_frame), len);
1512109323Ssam	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1513109323Ssam	m->m_pkthdr.rcvif = ifp;
151494405Simp
1515109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
151646492Swpaul
1517138952Ssam	wh = mtod(m, struct ieee80211_frame *);
1518138952Ssam	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1519138952Ssam		/*
1520138952Ssam		 * WEP is decrypted by hardware and the IV
1521138952Ssam		 * is stripped.  Clear WEP bit so we don't
1522138952Ssam		 * try to process it in ieee80211_input.
1523138952Ssam		 * XXX fix for TKIP, et. al.
1524138952Ssam		 */
1525138952Ssam		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1526138952Ssam	}
1527138952Ssam
1528109323Ssam#if NBPFILTER > 0
1529109323Ssam	if (sc->sc_drvbpf) {
1530123927Ssam		/* XXX replace divide by table */
1531123922Ssam		sc->sc_rx_th.wr_rate = frmhdr.wi_rx_rate / 5;
1532127148Sgreen		sc->sc_rx_th.wr_antsignal = frmhdr.wi_rx_signal;
1533127148Sgreen		sc->sc_rx_th.wr_antnoise = frmhdr.wi_rx_silence;
1534123927Ssam		sc->sc_rx_th.wr_flags = 0;
1535123927Ssam		if (frmhdr.wi_status & WI_STAT_PCF)
1536123927Ssam			sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_CFP;
1537138952Ssam		/* XXX IEEE80211_RADIOTAP_F_WEP */
1538123922Ssam		bpf_mtap2(sc->sc_drvbpf,
1539127698Ssam			&sc->sc_rx_th, sc->sc_rx_th_len, m);
154056965Swpaul	}
1541109323Ssam#endif
154256965Swpaul
1543109323Ssam	/* synchronize driver's BSSID with firmware's BSSID */
1544109323Ssam	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1545109323Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1546109323Ssam		wi_sync_bssid(sc, wh->i_addr3);
154746492Swpaul
1548119150Ssam	/*
1549119150Ssam	 * Locate the node for sender, track state, and
1550119150Ssam	 * then pass this node (referenced) up to the 802.11
1551138571Ssam	 * layer for its use.
1552119150Ssam	 */
1553138571Ssam	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *) wh);
1554119150Ssam	/*
1555119150Ssam	 * Send frame up for processing.
1556119150Ssam	 */
1557138571Ssam	ieee80211_input(ic, m, ni, rssi, rstamp);
1558119150Ssam	/*
1559119150Ssam	 * The frame may have caused the node to be marked for
1560119150Ssam	 * reclamation (e.g. in response to a DEAUTH message)
1561119150Ssam	 * so use free_node here instead of unref_node.
1562119150Ssam	 */
1563138571Ssam	ieee80211_free_node(ni);
1564109323Ssam}
156546492Swpaul
1566109323Ssamstatic void
1567109323Ssamwi_tx_ex_intr(struct wi_softc *sc)
1568109323Ssam{
1569147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1570109323Ssam	struct wi_frame frmhdr;
1571109323Ssam	int fid;
157246492Swpaul
1573109323Ssam	fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1574109323Ssam	/* Read in the frame header */
1575109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1576109323Ssam		u_int16_t status = le16toh(frmhdr.wi_status);
157746492Swpaul
1578109323Ssam		/*
1579109323Ssam		 * Spontaneous station disconnects appear as xmit
1580109323Ssam		 * errors.  Don't announce them and/or count them
1581109323Ssam		 * as an output error.
1582109323Ssam		 */
1583109323Ssam		if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1584109323Ssam			if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1585109323Ssam				if_printf(ifp, "tx failed");
1586109323Ssam				if (status & WI_TXSTAT_RET_ERR)
1587109323Ssam					printf(", retry limit exceeded");
1588109323Ssam				if (status & WI_TXSTAT_AGED_ERR)
1589109323Ssam					printf(", max transmit lifetime exceeded");
1590109323Ssam				if (status & WI_TXSTAT_DISCONNECT)
1591109323Ssam					printf(", port disconnected");
1592109323Ssam				if (status & WI_TXSTAT_FORM_ERR)
1593109323Ssam					printf(", invalid format (data len %u src %6D)",
1594109323Ssam						le16toh(frmhdr.wi_dat_len),
1595109323Ssam						frmhdr.wi_ehdr.ether_shost, ":");
1596109323Ssam				if (status & ~0xf)
1597109323Ssam					printf(", status=0x%x", status);
1598109323Ssam				printf("\n");
1599109323Ssam			}
1600109323Ssam			ifp->if_oerrors++;
1601109323Ssam		} else {
1602109323Ssam			DPRINTF(("port disconnected\n"));
1603109323Ssam			ifp->if_collisions++;	/* XXX */
1604109323Ssam		}
1605109323Ssam	} else
1606109323Ssam		DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
1607109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1608109323Ssam}
160946492Swpaul
1610109323Ssamstatic void
1611109323Ssamwi_tx_intr(struct wi_softc *sc)
1612109323Ssam{
1613147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1614109323Ssam	int fid, cur;
161594405Simp
1616123098Simp	if (sc->wi_gone)
1617123098Simp		return;
1618123098Simp
1619109323Ssam	fid = CSR_READ_2(sc, WI_ALLOC_FID);
1620109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
162146492Swpaul
1622109323Ssam	cur = sc->sc_txcur;
1623109323Ssam	if (sc->sc_txd[cur].d_fid != fid) {
1624109323Ssam		if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
1625109323Ssam		    fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
1626109323Ssam		return;
1627109323Ssam	}
1628109323Ssam	sc->sc_tx_timer = 0;
1629109323Ssam	sc->sc_txd[cur].d_len = 0;
1630112363Simp	sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
1631109323Ssam	if (sc->sc_txd[cur].d_len == 0)
1632109323Ssam		ifp->if_flags &= ~IFF_OACTIVE;
1633109323Ssam	else {
1634109323Ssam		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1635109323Ssam		    0, 0)) {
1636109323Ssam			if_printf(ifp, "xmit failed\n");
1637109323Ssam			sc->sc_txd[cur].d_len = 0;
1638109323Ssam		} else {
1639109323Ssam			sc->sc_tx_timer = 5;
1640109323Ssam			ifp->if_timer = 1;
1641109323Ssam		}
1642109323Ssam	}
164346492Swpaul}
164446492Swpaul
164588546Salfredstatic void
1646109323Ssamwi_info_intr(struct wi_softc *sc)
164794405Simp{
1648109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1649147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1650109323Ssam	int i, fid, len, off;
1651109323Ssam	u_int16_t ltbuf[2];
1652109323Ssam	u_int16_t stat;
1653109323Ssam	u_int32_t *ptr;
165494405Simp
1655109323Ssam	fid = CSR_READ_2(sc, WI_INFO_FID);
1656109323Ssam	wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
165794405Simp
1658109323Ssam	switch (le16toh(ltbuf[1])) {
165994405Simp
1660109323Ssam	case WI_INFO_LINK_STAT:
1661109323Ssam		wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1662109323Ssam		DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1663109323Ssam		switch (le16toh(stat)) {
1664109323Ssam		case WI_INFO_LINK_STAT_CONNECTED:
1665109323Ssam			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1666109323Ssam			if (ic->ic_state == IEEE80211_S_RUN &&
1667109323Ssam			    ic->ic_opmode != IEEE80211_M_IBSS)
1668109323Ssam				break;
1669109323Ssam			/* FALLTHROUGH */
1670109323Ssam		case WI_INFO_LINK_STAT_AP_CHG:
1671117812Ssam			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1672109323Ssam			break;
1673109323Ssam		case WI_INFO_LINK_STAT_AP_INR:
1674109323Ssam			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1675109323Ssam			break;
1676109323Ssam		case WI_INFO_LINK_STAT_AP_OOR:
1677109323Ssam			if (sc->sc_firmware_type == WI_SYMBOL &&
1678109323Ssam			    sc->sc_scan_timer > 0) {
1679109323Ssam				if (wi_cmd(sc, WI_CMD_INQUIRE,
1680109323Ssam				    WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
1681109323Ssam					sc->sc_scan_timer = 0;
1682109323Ssam				break;
1683109323Ssam			}
1684109323Ssam			if (ic->ic_opmode == IEEE80211_M_STA)
1685109323Ssam				sc->sc_flags |= WI_FLAGS_OUTRANGE;
1686109323Ssam			break;
1687109323Ssam		case WI_INFO_LINK_STAT_DISCONNECTED:
1688109323Ssam		case WI_INFO_LINK_STAT_ASSOC_FAILED:
1689109323Ssam			if (ic->ic_opmode == IEEE80211_M_STA)
1690117812Ssam				ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1691109323Ssam			break;
1692109323Ssam		}
1693109323Ssam		break;
169494405Simp
1695109323Ssam	case WI_INFO_COUNTERS:
1696109323Ssam		/* some card versions have a larger stats structure */
1697109323Ssam		len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1698109323Ssam		ptr = (u_int32_t *)&sc->sc_stats;
1699109323Ssam		off = sizeof(ltbuf);
1700109323Ssam		for (i = 0; i < len; i++, off += 2, ptr++) {
1701109323Ssam			wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1702109323Ssam#ifdef WI_HERMES_STATS_WAR
1703109323Ssam			if (stat & 0xf000)
1704109323Ssam				stat = ~stat;
1705109323Ssam#endif
1706109323Ssam			*ptr += stat;
1707109323Ssam		}
1708109323Ssam		ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1709109323Ssam		    sc->sc_stats.wi_tx_multi_retries +
1710109323Ssam		    sc->sc_stats.wi_tx_retry_limit;
1711109323Ssam		break;
1712109323Ssam
1713109323Ssam	case WI_INFO_SCAN_RESULTS:
1714109323Ssam	case WI_INFO_HOST_SCAN_RESULTS:
1715109323Ssam		wi_scan_result(sc, fid, le16toh(ltbuf[0]));
1716109323Ssam		break;
1717109323Ssam
1718109323Ssam	default:
1719109323Ssam		DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1720109323Ssam		    le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1721109323Ssam		break;
172294405Simp	}
1723109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1724109323Ssam}
172594405Simp
1726109323Ssamstatic int
1727109323Ssamwi_write_multi(struct wi_softc *sc)
1728109323Ssam{
1729147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1730109323Ssam	int n;
1731109323Ssam	struct ifmultiaddr *ifma;
1732109323Ssam	struct wi_mcast mlist;
173394472Simp
1734109323Ssam	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1735109323Ssamallmulti:
1736109323Ssam		memset(&mlist, 0, sizeof(mlist));
1737109323Ssam		return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1738109323Ssam		    sizeof(mlist));
173994405Simp	}
174094405Simp
1741109323Ssam	n = 0;
1742109323Ssam#if __FreeBSD_version < 500000
1743109323Ssam	LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1744109323Ssam#else
1745109323Ssam	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1746109323Ssam#endif
1747109323Ssam		if (ifma->ifma_addr->sa_family != AF_LINK)
1748109323Ssam			continue;
1749109323Ssam		if (n >= 16)
1750109323Ssam			goto allmulti;
1751109323Ssam		IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1752109323Ssam		    (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1753109323Ssam		n++;
175494405Simp	}
1755109323Ssam	return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1756109323Ssam	    IEEE80211_ADDR_LEN * n);
175794405Simp}
175894405Simp
175994405Simpstatic void
1760109323Ssamwi_read_nicid(struct wi_softc *sc)
176146492Swpaul{
1762109323Ssam	struct wi_card_ident *id;
1763109323Ssam	char *p;
1764109323Ssam	int len;
1765109323Ssam	u_int16_t ver[4];
176646492Swpaul
1767109323Ssam	/* getting chip identity */
1768109323Ssam	memset(ver, 0, sizeof(ver));
1769109323Ssam	len = sizeof(ver);
1770109323Ssam	wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1771109323Ssam	device_printf(sc->sc_dev, "using ");
177246492Swpaul
1773109323Ssam	sc->sc_firmware_type = WI_NOTYPE;
1774109323Ssam	for (id = wi_card_ident; id->card_name != NULL; id++) {
1775109323Ssam		if (le16toh(ver[0]) == id->card_id) {
1776109323Ssam			printf("%s", id->card_name);
1777109323Ssam			sc->sc_firmware_type = id->firm_type;
1778109323Ssam			break;
1779109323Ssam		}
178067092Swpaul	}
1781109323Ssam	if (sc->sc_firmware_type == WI_NOTYPE) {
1782109323Ssam		if (le16toh(ver[0]) & 0x8000) {
1783109323Ssam			printf("Unknown PRISM2 chip");
1784109323Ssam			sc->sc_firmware_type = WI_INTERSIL;
1785109323Ssam		} else {
1786109323Ssam			printf("Unknown Lucent chip");
1787109323Ssam			sc->sc_firmware_type = WI_LUCENT;
1788109323Ssam		}
178967092Swpaul	}
179046492Swpaul
1791109323Ssam	/* get primary firmware version (Only Prism chips) */
1792109323Ssam	if (sc->sc_firmware_type != WI_LUCENT) {
1793109323Ssam		memset(ver, 0, sizeof(ver));
1794109323Ssam		len = sizeof(ver);
1795109323Ssam		wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1796109323Ssam		sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1797109323Ssam		    le16toh(ver[3]) * 100 + le16toh(ver[1]);
179867092Swpaul	}
179946492Swpaul
1800109323Ssam	/* get station firmware version */
1801109323Ssam	memset(ver, 0, sizeof(ver));
1802109323Ssam	len = sizeof(ver);
1803109323Ssam	wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1804109323Ssam	sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1805109323Ssam	    le16toh(ver[3]) * 100 + le16toh(ver[1]);
1806109323Ssam	if (sc->sc_firmware_type == WI_INTERSIL &&
1807109323Ssam	    (sc->sc_sta_firmware_ver == 10102 ||
1808109323Ssam	     sc->sc_sta_firmware_ver == 20102)) {
1809109323Ssam		char ident[12];
1810109323Ssam		memset(ident, 0, sizeof(ident));
1811109323Ssam		len = sizeof(ident);
1812109323Ssam		/* value should be the format like "V2.00-11" */
1813109323Ssam		if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1814109323Ssam		    *(p = (char *)ident) >= 'A' &&
1815109323Ssam		    p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1816109323Ssam			sc->sc_firmware_type = WI_SYMBOL;
1817109323Ssam			sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1818109323Ssam			    (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1819109323Ssam			    (p[6] - '0') * 10 + (p[7] - '0');
182094405Simp		}
182194405Simp	}
1822109323Ssam	printf("\n");
1823109323Ssam	device_printf(sc->sc_dev, "%s Firmware: ",
1824109323Ssam	     sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
1825109323Ssam	    (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
1826109323Ssam	if (sc->sc_firmware_type != WI_LUCENT)	/* XXX */
1827109323Ssam		printf("Primary (%u.%u.%u), ",
1828109323Ssam		    sc->sc_pri_firmware_ver / 10000,
1829109323Ssam		    (sc->sc_pri_firmware_ver % 10000) / 100,
1830109323Ssam		    sc->sc_pri_firmware_ver % 100);
1831109323Ssam	printf("Station (%u.%u.%u)\n",
1832109323Ssam	    sc->sc_sta_firmware_ver / 10000,
1833109323Ssam	    (sc->sc_sta_firmware_ver % 10000) / 100,
1834109323Ssam	    sc->sc_sta_firmware_ver % 100);
1835109323Ssam}
183646492Swpaul
1837109323Ssamstatic int
1838109323Ssamwi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1839109323Ssam{
1840109323Ssam	struct wi_ssid ssid;
184146492Swpaul
1842109323Ssam	if (buflen > IEEE80211_NWID_LEN)
1843109323Ssam		return ENOBUFS;
1844109323Ssam	memset(&ssid, 0, sizeof(ssid));
1845109323Ssam	ssid.wi_len = htole16(buflen);
1846109323Ssam	memcpy(ssid.wi_ssid, buf, buflen);
1847109323Ssam	return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1848109323Ssam}
184946492Swpaul
1850109323Ssamstatic int
1851109323Ssamwi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
1852109323Ssam{
1853109323Ssam	struct wi_softc *sc = ifp->if_softc;
1854109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1855109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
1856109323Ssam	struct wi_req wreq;
1857116898Ssam	struct wi_scan_res *res;
1858122015Sgreen	size_t reslen;
1859116898Ssam	int len, n, error, mif, val, off, i;
186046492Swpaul
1861109323Ssam	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1862109323Ssam	if (error)
1863109323Ssam		return error;
1864109323Ssam	len = (wreq.wi_len - 1) * 2;
1865109323Ssam	if (len < sizeof(u_int16_t))
1866109323Ssam		return ENOSPC;
1867109323Ssam	if (len > sizeof(wreq.wi_val))
1868109323Ssam		len = sizeof(wreq.wi_val);
186946492Swpaul
1870109323Ssam	switch (wreq.wi_type) {
187146492Swpaul
1872109323Ssam	case WI_RID_IFACE_STATS:
1873109323Ssam		memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
1874109323Ssam		if (len < sizeof(sc->sc_stats))
1875109323Ssam			error = ENOSPC;
1876109323Ssam		else
1877109323Ssam			len = sizeof(sc->sc_stats);
1878109323Ssam		break;
187946492Swpaul
1880109323Ssam	case WI_RID_ENCRYPTION:
1881109323Ssam	case WI_RID_TX_CRYPT_KEY:
1882109323Ssam	case WI_RID_DEFLT_CRYPT_KEYS:
1883109323Ssam	case WI_RID_TX_RATE:
1884138571Ssam		return ieee80211_cfgget(ic, cmd, data);
188546492Swpaul
1886109323Ssam	case WI_RID_MICROWAVE_OVEN:
1887109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
1888109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1889109323Ssam			    &len);
1890109323Ssam			break;
1891109323Ssam		}
1892109323Ssam		wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
1893109323Ssam		len = sizeof(u_int16_t);
1894109323Ssam		break;
189546492Swpaul
1896109323Ssam	case WI_RID_DBM_ADJUST:
1897109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
1898109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1899109323Ssam			    &len);
1900109323Ssam			break;
1901109323Ssam		}
1902119784Ssam		wreq.wi_val[0] = htole16(sc->sc_dbm_offset);
1903109323Ssam		len = sizeof(u_int16_t);
1904109323Ssam		break;
190546492Swpaul
1906109323Ssam	case WI_RID_ROAMING_MODE:
1907109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
1908109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1909109323Ssam			    &len);
1910109323Ssam			break;
1911109323Ssam		}
1912109323Ssam		wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
1913109323Ssam		len = sizeof(u_int16_t);
1914109323Ssam		break;
191546492Swpaul
1916109323Ssam	case WI_RID_SYSTEM_SCALE:
1917109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
1918109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1919109323Ssam			    &len);
1920109323Ssam			break;
1921109323Ssam		}
1922109323Ssam		wreq.wi_val[0] = htole16(sc->sc_system_scale);
1923109323Ssam		len = sizeof(u_int16_t);
1924109323Ssam		break;
192546492Swpaul
1926109323Ssam	case WI_RID_FRAG_THRESH:
1927109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
1928109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1929109323Ssam			    &len);
1930109323Ssam			break;
1931109323Ssam		}
1932116951Ssam		wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
1933109323Ssam		len = sizeof(u_int16_t);
1934109323Ssam		break;
193546492Swpaul
1936109323Ssam	case WI_RID_READ_APS:
1937109323Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1938138571Ssam			return ieee80211_cfgget(ic, cmd, data);
1939109323Ssam		if (sc->sc_scan_timer > 0) {
1940109323Ssam			error = EINPROGRESS;
1941109323Ssam			break;
1942109323Ssam		}
1943109323Ssam		n = sc->sc_naps;
1944109323Ssam		if (len < sizeof(n)) {
1945109323Ssam			error = ENOSPC;
1946109323Ssam			break;
1947109323Ssam		}
1948109323Ssam		if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
1949109323Ssam			n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
1950109323Ssam		len = sizeof(n) + sizeof(struct wi_apinfo) * n;
1951109323Ssam		memcpy(wreq.wi_val, &n, sizeof(n));
1952109323Ssam		memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
1953109323Ssam		    sizeof(struct wi_apinfo) * n);
1954109323Ssam		break;
195546492Swpaul
1956109323Ssam	case WI_RID_PRISM2:
1957109323Ssam		wreq.wi_val[0] = sc->sc_firmware_type != WI_LUCENT;
1958109323Ssam		len = sizeof(u_int16_t);
1959109323Ssam		break;
196046492Swpaul
1961109323Ssam	case WI_RID_MIF:
1962109323Ssam		mif = wreq.wi_val[0];
1963109323Ssam		error = wi_cmd(sc, WI_CMD_READMIF, mif, 0, 0);
1964109323Ssam		val = CSR_READ_2(sc, WI_RESP0);
1965109323Ssam		wreq.wi_val[0] = val;
1966109323Ssam		len = sizeof(u_int16_t);
1967109323Ssam		break;
196846492Swpaul
1969109323Ssam	case WI_RID_ZERO_CACHE:
1970109323Ssam	case WI_RID_PROCFRAME:		/* ignore for compatibility */
1971109323Ssam		/* XXX ??? */
1972109323Ssam		break;
197346492Swpaul
1974109323Ssam	case WI_RID_READ_CACHE:
1975138571Ssam		return ieee80211_cfgget(ic, cmd, data);
197646492Swpaul
1977116898Ssam	case WI_RID_SCAN_RES:		/* compatibility interface */
1978116898Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1979138571Ssam			return ieee80211_cfgget(ic, cmd, data);
1980116898Ssam		if (sc->sc_scan_timer > 0) {
1981116898Ssam			error = EINPROGRESS;
1982116898Ssam			break;
1983116898Ssam		}
1984116898Ssam		n = sc->sc_naps;
1985122015Sgreen		if (sc->sc_firmware_type == WI_LUCENT) {
1986122015Sgreen			off = 0;
1987122015Sgreen			reslen = WI_WAVELAN_RES_SIZE;
1988122015Sgreen		} else {
1989122015Sgreen			off = sizeof(struct wi_scan_p2_hdr);
1990122015Sgreen			reslen = WI_PRISM2_RES_SIZE;
1991122015Sgreen		}
1992122015Sgreen		if (len < off + reslen * n)
1993122015Sgreen			n = (len - off) / reslen;
1994122015Sgreen		len = off + reslen * n;
1995116898Ssam		if (off != 0) {
1996116898Ssam			struct wi_scan_p2_hdr *p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1997116898Ssam			/*
1998116898Ssam			 * Prepend Prism-specific header.
1999116898Ssam			 */
2000116898Ssam			if (len < sizeof(struct wi_scan_p2_hdr)) {
2001116898Ssam				error = ENOSPC;
2002116898Ssam				break;
2003116898Ssam			}
2004116898Ssam			p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
2005116898Ssam			p2->wi_rsvd = 0;
2006116898Ssam			p2->wi_reason = n;	/* XXX */
2007116898Ssam		}
2008122015Sgreen		for (i = 0; i < n; i++, off += reslen) {
2009116898Ssam			const struct wi_apinfo *ap = &sc->sc_aps[i];
2010116898Ssam
2011116898Ssam			res = (struct wi_scan_res *)((char *)wreq.wi_val + off);
2012116898Ssam			res->wi_chan = ap->channel;
2013116898Ssam			res->wi_noise = ap->noise;
2014116898Ssam			res->wi_signal = ap->signal;
2015116898Ssam			IEEE80211_ADDR_COPY(res->wi_bssid, ap->bssid);
2016116898Ssam			res->wi_interval = ap->interval;
2017116898Ssam			res->wi_capinfo = ap->capinfo;
2018116898Ssam			res->wi_ssid_len = ap->namelen;
2019116898Ssam			memcpy(res->wi_ssid, ap->name,
2020116898Ssam				IEEE80211_NWID_LEN);
2021116898Ssam			if (sc->sc_firmware_type != WI_LUCENT) {
2022116898Ssam				/* XXX not saved from Prism cards */
2023116898Ssam				memset(res->wi_srates, 0,
2024116898Ssam					sizeof(res->wi_srates));
2025116898Ssam				res->wi_rate = ap->rate;
2026116898Ssam				res->wi_rsvd = 0;
2027122015Sgreen			}
2028116898Ssam		}
2029116898Ssam		break;
2030116898Ssam
2031109323Ssam	default:
2032109323Ssam		if (sc->sc_enabled) {
2033109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2034109323Ssam			    &len);
2035109323Ssam			break;
2036109323Ssam		}
2037109323Ssam		switch (wreq.wi_type) {
2038109323Ssam		case WI_RID_MAX_DATALEN:
2039109323Ssam			wreq.wi_val[0] = htole16(sc->sc_max_datalen);
2040109323Ssam			len = sizeof(u_int16_t);
2041109323Ssam			break;
2042109323Ssam		case WI_RID_RTS_THRESH:
2043116951Ssam			wreq.wi_val[0] = htole16(ic->ic_rtsthreshold);
2044109323Ssam			len = sizeof(u_int16_t);
2045109323Ssam			break;
2046109323Ssam		case WI_RID_CNFAUTHMODE:
2047109323Ssam			wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
2048109323Ssam			len = sizeof(u_int16_t);
2049109323Ssam			break;
2050109323Ssam		case WI_RID_NODENAME:
2051109323Ssam			if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
2052109323Ssam				error = ENOSPC;
2053109323Ssam				break;
2054109323Ssam			}
2055109323Ssam			len = sc->sc_nodelen + sizeof(u_int16_t);
2056109323Ssam			wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
2057109323Ssam			memcpy(&wreq.wi_val[1], sc->sc_nodename,
2058109323Ssam			    sc->sc_nodelen);
2059109323Ssam			break;
2060109323Ssam		default:
2061138571Ssam			return ieee80211_cfgget(ic, cmd, data);
2062109323Ssam		}
2063109323Ssam		break;
206446492Swpaul	}
2065109323Ssam	if (error)
2066109323Ssam		return error;
2067109323Ssam	wreq.wi_len = (len + 1) / 2 + 1;
2068109323Ssam	return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
206946492Swpaul}
207046492Swpaul
2071109323Ssamstatic int
2072109323Ssamwi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
207346492Swpaul{
2074109323Ssam	struct wi_softc *sc = ifp->if_softc;
2075109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2076109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
2077109323Ssam	struct wi_req wreq;
2078109323Ssam	struct mbuf *m;
2079109323Ssam	int i, len, error, mif, val;
2080116951Ssam	struct ieee80211_rateset *rs;
2081138571Ssam	WI_LOCK_DECL();
208246492Swpaul
2083109323Ssam	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2084109323Ssam	if (error)
2085109323Ssam		return error;
2086116898Ssam	len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0;
2087109323Ssam	switch (wreq.wi_type) {
2088109323Ssam	case WI_RID_DBM_ADJUST:
2089109323Ssam		return ENODEV;
209067092Swpaul
2091109323Ssam	case WI_RID_NODENAME:
2092109323Ssam		if (le16toh(wreq.wi_val[0]) * 2 > len ||
2093109323Ssam		    le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
2094109323Ssam			error = ENOSPC;
2095109323Ssam			break;
2096109323Ssam		}
2097138571Ssam		WI_LOCK(sc);
2098138571Ssam		if (sc->sc_enabled)
2099109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2100109323Ssam			    len);
2101138571Ssam		if (error == 0) {
2102138571Ssam			sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
2103138571Ssam			memcpy(sc->sc_nodename, &wreq.wi_val[1],
2104138571Ssam				sc->sc_nodelen);
2105109323Ssam		}
2106138571Ssam		WI_UNLOCK(sc);
2107109323Ssam		break;
210846492Swpaul
2109109323Ssam	case WI_RID_MICROWAVE_OVEN:
2110109323Ssam	case WI_RID_ROAMING_MODE:
2111109323Ssam	case WI_RID_SYSTEM_SCALE:
2112109323Ssam	case WI_RID_FRAG_THRESH:
2113138571Ssam		/* XXX unlocked reads */
2114109323Ssam		if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
2115109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
2116109323Ssam			break;
2117109323Ssam		if (wreq.wi_type == WI_RID_ROAMING_MODE &&
2118109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
2119109323Ssam			break;
2120109323Ssam		if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
2121109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
2122109323Ssam			break;
2123109323Ssam		if (wreq.wi_type == WI_RID_FRAG_THRESH &&
2124109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
2125109323Ssam			break;
2126109323Ssam		/* FALLTHROUGH */
2127109323Ssam	case WI_RID_RTS_THRESH:
2128109323Ssam	case WI_RID_CNFAUTHMODE:
2129109323Ssam	case WI_RID_MAX_DATALEN:
2130138571Ssam		WI_LOCK(sc);
2131109323Ssam		if (sc->sc_enabled) {
2132109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2133109323Ssam			    sizeof(u_int16_t));
2134138571Ssam			if (error != 0) {
2135138571Ssam				WI_UNLOCK(sc);
2136109323Ssam				break;
2137138571Ssam			}
2138109323Ssam		}
2139109323Ssam		switch (wreq.wi_type) {
2140109323Ssam		case WI_RID_FRAG_THRESH:
2141116951Ssam			ic->ic_fragthreshold = le16toh(wreq.wi_val[0]);
2142109323Ssam			break;
2143109323Ssam		case WI_RID_RTS_THRESH:
2144116951Ssam			ic->ic_rtsthreshold = le16toh(wreq.wi_val[0]);
2145109323Ssam			break;
2146109323Ssam		case WI_RID_MICROWAVE_OVEN:
2147109323Ssam			sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
2148109323Ssam			break;
2149109323Ssam		case WI_RID_ROAMING_MODE:
2150109323Ssam			sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
2151109323Ssam			break;
2152109323Ssam		case WI_RID_SYSTEM_SCALE:
2153109323Ssam			sc->sc_system_scale = le16toh(wreq.wi_val[0]);
2154109323Ssam			break;
2155109323Ssam		case WI_RID_CNFAUTHMODE:
2156109323Ssam			sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
2157109323Ssam			break;
2158109323Ssam		case WI_RID_MAX_DATALEN:
2159109323Ssam			sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
2160109323Ssam			break;
2161109323Ssam		}
2162138571Ssam		WI_UNLOCK(sc);
2163109323Ssam		break;
216494405Simp
2165109323Ssam	case WI_RID_TX_RATE:
2166138571Ssam		WI_LOCK(sc);
2167109323Ssam		switch (le16toh(wreq.wi_val[0])) {
2168109323Ssam		case 3:
2169109323Ssam			ic->ic_fixed_rate = -1;
2170109323Ssam			break;
2171109323Ssam		default:
2172116951Ssam			rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
2173116951Ssam			for (i = 0; i < rs->rs_nrates; i++) {
2174116951Ssam				if ((rs->rs_rates[i] & IEEE80211_RATE_VAL)
2175109323Ssam				    / 2 == le16toh(wreq.wi_val[0]))
2176109323Ssam					break;
2177109323Ssam			}
2178138571Ssam			if (i == rs->rs_nrates) {
2179138571Ssam				WI_UNLOCK(sc);
2180109323Ssam				return EINVAL;
2181138571Ssam			}
2182109323Ssam			ic->ic_fixed_rate = i;
2183109323Ssam		}
2184109323Ssam		if (sc->sc_enabled)
2185109323Ssam			error = wi_write_txrate(sc);
2186138571Ssam		WI_UNLOCK(sc);
2187109323Ssam		break;
218846492Swpaul
2189109323Ssam	case WI_RID_SCAN_APS:
2190138571Ssam		WI_LOCK(sc);
2191109323Ssam		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2192116898Ssam			error = wi_scan_ap(sc, 0x3fff, 0x000f);
2193138571Ssam		WI_UNLOCK(sc);
2194109323Ssam		break;
219546492Swpaul
2196116898Ssam	case WI_RID_SCAN_REQ:		/* compatibility interface */
2197138571Ssam		WI_LOCK(sc);
2198116898Ssam		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2199116898Ssam			error = wi_scan_ap(sc, wreq.wi_val[0], wreq.wi_val[1]);
2200138571Ssam		WI_UNLOCK(sc);
2201116898Ssam		break;
2202116898Ssam
2203109323Ssam	case WI_RID_MGMT_XMIT:
2204138571Ssam		WI_LOCK(sc);
2205138571Ssam		if (!sc->sc_enabled)
2206109323Ssam			error = ENETDOWN;
2207138571Ssam		else if (ic->ic_mgtq.ifq_len > 5)
2208109323Ssam			error = EAGAIN;
2209138571Ssam		else {
2210138571Ssam			/* NB: m_devget uses M_DONTWAIT so can hold the lock */
2211138571Ssam			/* XXX wi_len looks in u_int8_t, not in u_int16_t */
2212138571Ssam			m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0,
2213138571Ssam				ifp, NULL);
2214138571Ssam			if (m != NULL)
2215138571Ssam				IF_ENQUEUE(&ic->ic_mgtq, m);
2216138571Ssam			else
2217138571Ssam				error = ENOMEM;
2218109323Ssam		}
2219138571Ssam		WI_UNLOCK(sc);
2220109323Ssam		break;
222146492Swpaul
2222109323Ssam	case WI_RID_MIF:
2223109323Ssam		mif = wreq.wi_val[0];
2224109323Ssam		val = wreq.wi_val[1];
2225138571Ssam		WI_LOCK(sc);
2226109323Ssam		error = wi_cmd(sc, WI_CMD_WRITEMIF, mif, val, 0);
2227138571Ssam		WI_UNLOCK(sc);
2228109323Ssam		break;
222946492Swpaul
2230109323Ssam	case WI_RID_PROCFRAME:		/* ignore for compatibility */
2231109323Ssam		break;
2232109323Ssam
2233116898Ssam	case WI_RID_OWN_SSID:
2234116898Ssam		if (le16toh(wreq.wi_val[0]) * 2 > len ||
2235116898Ssam		    le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) {
2236116898Ssam			error = ENOSPC;
2237116898Ssam			break;
2238116898Ssam		}
2239138571Ssam		WI_LOCK(sc);
2240116898Ssam		memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
2241116898Ssam		ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2;
2242116898Ssam		memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen);
2243138571Ssam		if (sc->sc_enabled)
2244138571Ssam			wi_init(sc);	/* XXX no error return */
2245138571Ssam		WI_UNLOCK(sc);
2246116898Ssam		break;
2247116898Ssam
2248109323Ssam	default:
2249138571Ssam		WI_LOCK(sc);
2250138571Ssam		if (sc->sc_enabled)
2251109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2252109323Ssam			    len);
2253138571Ssam		if (error == 0) {
2254138571Ssam			/* XXX ieee80211_cfgset does a copyin */
2255138571Ssam			error = ieee80211_cfgset(ic, cmd, data);
2256138571Ssam			if (error == ENETRESET) {
2257138571Ssam				if (sc->sc_enabled)
2258138571Ssam					wi_init(sc);
2259138571Ssam				error = 0;
2260138571Ssam			}
2261109323Ssam		}
2262138571Ssam		WI_UNLOCK(sc);
2263109323Ssam		break;
2264109323Ssam	}
2265109323Ssam	return error;
226646492Swpaul}
226746492Swpaul
2268109323Ssamstatic int
2269109323Ssamwi_write_txrate(struct wi_softc *sc)
227046492Swpaul{
2271109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2272109323Ssam	int i;
2273109323Ssam	u_int16_t rate;
227446492Swpaul
2275109323Ssam	if (ic->ic_fixed_rate < 0)
2276109323Ssam		rate = 0;	/* auto */
2277109323Ssam	else
2278116951Ssam		rate = (ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ic->ic_fixed_rate] &
2279109323Ssam		    IEEE80211_RATE_VAL) / 2;
228046492Swpaul
2281109323Ssam	/* rate: 0, 1, 2, 5, 11 */
228246492Swpaul
2283109323Ssam	switch (sc->sc_firmware_type) {
2284109323Ssam	case WI_LUCENT:
2285112501Simp		switch (rate) {
2286112501Simp		case 0:			/* auto == 11mbps auto */
2287112501Simp			rate = 3;
2288112501Simp			break;
2289112501Simp		/* case 1, 2 map to 1, 2*/
2290112501Simp		case 5:			/* 5.5Mbps -> 4 */
2291112501Simp			rate = 4;
2292112501Simp			break;
2293112501Simp		case 11:		/* 11mbps -> 5 */
2294112501Simp			rate = 5;
2295112501Simp			break;
2296112501Simp		default:
2297112501Simp			break;
2298112501Simp		}
2299109323Ssam		break;
2300109323Ssam	default:
2301109323Ssam		/* Choose a bit according to this table.
2302109323Ssam		 *
2303109323Ssam		 * bit | data rate
2304109323Ssam		 * ----+-------------------
2305109323Ssam		 * 0   | 1Mbps
2306109323Ssam		 * 1   | 2Mbps
2307109323Ssam		 * 2   | 5.5Mbps
2308109323Ssam		 * 3   | 11Mbps
2309109323Ssam		 */
2310109323Ssam		for (i = 8; i > 0; i >>= 1) {
2311109323Ssam			if (rate >= i)
2312109323Ssam				break;
2313109323Ssam		}
2314109323Ssam		if (i == 0)
2315109323Ssam			rate = 0xf;	/* auto */
2316109323Ssam		else
2317109323Ssam			rate = i;
2318109323Ssam		break;
2319109323Ssam	}
2320109323Ssam	return wi_write_val(sc, WI_RID_TX_RATE, rate);
2321109323Ssam}
232246492Swpaul
2323109323Ssamstatic int
2324109323Ssamwi_write_wep(struct wi_softc *sc)
2325109323Ssam{
2326109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2327109323Ssam	int error = 0;
2328109323Ssam	int i, keylen;
2329109323Ssam	u_int16_t val;
2330109323Ssam	struct wi_key wkey[IEEE80211_WEP_NKID];
233146492Swpaul
2332109323Ssam	switch (sc->sc_firmware_type) {
2333109323Ssam	case WI_LUCENT:
2334138571Ssam		val = (ic->ic_flags & IEEE80211_F_PRIVACY) ? 1 : 0;
2335109323Ssam		error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
2336109323Ssam		if (error)
2337109323Ssam			break;
2338138988Smdodd		if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0)
2339138988Smdodd			break;
2340138988Smdodd		error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_def_txkey);
2341138988Smdodd		if (error)
2342138988Smdodd			break;
2343138988Smdodd		memset(wkey, 0, sizeof(wkey));
2344138988Smdodd		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2345138988Smdodd			keylen = ic->ic_nw_keys[i].wk_keylen;
2346138988Smdodd			wkey[i].wi_keylen = htole16(keylen);
2347138988Smdodd			memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
2348138988Smdodd			    keylen);
2349109323Ssam		}
2350138988Smdodd		error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
2351138988Smdodd		    wkey, sizeof(wkey));
2352109323Ssam		break;
2353109323Ssam
2354109323Ssam	case WI_INTERSIL:
2355109323Ssam	case WI_SYMBOL:
2356138571Ssam		if (ic->ic_flags & IEEE80211_F_PRIVACY) {
2357109323Ssam			/*
2358109323Ssam			 * ONLY HWB3163 EVAL-CARD Firmware version
2359109323Ssam			 * less than 0.8 variant2
2360109323Ssam			 *
2361109323Ssam			 *   If promiscuous mode disable, Prism2 chip
2362109323Ssam			 *  does not work with WEP .
2363109323Ssam			 * It is under investigation for details.
2364109323Ssam			 * (ichiro@netbsd.org)
2365109323Ssam			 */
2366109323Ssam			if (sc->sc_firmware_type == WI_INTERSIL &&
2367109323Ssam			    sc->sc_sta_firmware_ver < 802 ) {
2368109323Ssam				/* firm ver < 0.8 variant 2 */
2369109323Ssam				wi_write_val(sc, WI_RID_PROMISC, 1);
2370109323Ssam			}
2371109323Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE,
2372109323Ssam			    sc->sc_cnfauthmode);
2373109323Ssam			val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
2374109323Ssam			/*
2375109323Ssam			 * Encryption firmware has a bug for HostAP mode.
2376109323Ssam			 */
2377109323Ssam			if (sc->sc_firmware_type == WI_INTERSIL &&
2378109323Ssam			    ic->ic_opmode == IEEE80211_M_HOSTAP)
2379109323Ssam				val |= HOST_ENCRYPT;
2380109323Ssam		} else {
2381109323Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE,
2382109323Ssam			    IEEE80211_AUTH_OPEN);
2383109323Ssam			val = HOST_ENCRYPT | HOST_DECRYPT;
2384109323Ssam		}
2385109323Ssam		error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
2386109323Ssam		if (error)
2387109323Ssam			break;
2388138988Smdodd		if ((val & PRIVACY_INVOKED) == 0)
2389138988Smdodd			break;
2390138988Smdodd		error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
2391138988Smdodd		    ic->ic_def_txkey);
2392138988Smdodd		if (error)
2393138988Smdodd			break;
2394138988Smdodd		if (val & HOST_DECRYPT)
2395138988Smdodd			break;
2396138988Smdodd		/*
2397138988Smdodd		 * It seems that the firmware accept 104bit key only if
2398138988Smdodd		 * all the keys have 104bit length.  We get the length of
2399138988Smdodd		 * the transmit key and use it for all other keys.
2400138988Smdodd		 * Perhaps we should use software WEP for such situation.
2401138988Smdodd		 */
2402138988Smdodd		if (ic->ic_def_txkey != IEEE80211_KEYIX_NONE)
2403138988Smdodd			keylen = ic->ic_nw_keys[ic->ic_def_txkey].wk_keylen;
2404138988Smdodd		else	/* XXX should not hapen */
2405138988Smdodd			keylen = IEEE80211_WEP_KEYLEN;
2406138988Smdodd		if (keylen > IEEE80211_WEP_KEYLEN)
2407138988Smdodd			keylen = 13;	/* 104bit keys */
2408138988Smdodd		else
2409138988Smdodd			keylen = IEEE80211_WEP_KEYLEN;
2410138988Smdodd		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2411138988Smdodd			error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
2412138988Smdodd			    ic->ic_nw_keys[i].wk_key, keylen);
2413109323Ssam			if (error)
2414109323Ssam				break;
2415109323Ssam		}
2416109323Ssam		break;
2417109323Ssam	}
2418109323Ssam	return error;
241946492Swpaul}
242046492Swpaul
2421109323Ssamstatic int
2422109323Ssamwi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
242346492Swpaul{
2424109323Ssam	int			i, s = 0;
2425109323Ssam	static volatile int count  = 0;
2426109323Ssam
2427123098Simp	if (sc->wi_gone)
2428123098Simp		return (ENODEV);
2429123098Simp
2430109323Ssam	if (count > 0)
2431109323Ssam		panic("Hey partner, hold on there!");
2432109323Ssam	count++;
243353702Swpaul
2434109323Ssam	/* wait for the busy bit to clear */
2435123339Simp	for (i = sc->wi_cmd_count; i > 0; i--) {	/* 500ms */
2436116206Simp		if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
2437109323Ssam			break;
2438123098Simp		DELAY(1*1000);	/* 1ms */
2439109323Ssam	}
2440109323Ssam	if (i == 0) {
2441109323Ssam		device_printf(sc->sc_dev, "wi_cmd: busy bit won't clear.\n" );
2442123098Simp		sc->wi_gone = 1;
2443109323Ssam		count--;
2444109323Ssam		return(ETIMEDOUT);
2445109323Ssam	}
244690580Sbrooks
2447109323Ssam	CSR_WRITE_2(sc, WI_PARAM0, val0);
2448109323Ssam	CSR_WRITE_2(sc, WI_PARAM1, val1);
2449109323Ssam	CSR_WRITE_2(sc, WI_PARAM2, val2);
2450109323Ssam	CSR_WRITE_2(sc, WI_COMMAND, cmd);
245190580Sbrooks
2452109323Ssam	if (cmd == WI_CMD_INI) {
2453109323Ssam		/* XXX: should sleep here. */
2454116206Simp		DELAY(100*1000);		/* 100ms delay for init */
2455109323Ssam	}
2456109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
2457109323Ssam		/*
2458109323Ssam		 * Wait for 'command complete' bit to be
2459109323Ssam		 * set in the event status register.
2460109323Ssam		 */
2461109323Ssam		s = CSR_READ_2(sc, WI_EVENT_STAT);
2462109323Ssam		if (s & WI_EV_CMD) {
2463109323Ssam			/* Ack the event and read result code. */
2464109323Ssam			s = CSR_READ_2(sc, WI_STATUS);
2465109323Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
2466109323Ssam			if (s & WI_STAT_CMD_RESULT) {
2467109323Ssam				count--;
2468109323Ssam				return(EIO);
2469109323Ssam			}
2470109323Ssam			break;
247190580Sbrooks		}
2472109323Ssam		DELAY(WI_DELAY);
2473109323Ssam	}
247490580Sbrooks
2475109323Ssam	count--;
2476109323Ssam	if (i == WI_TIMEOUT) {
2477109323Ssam		device_printf(sc->sc_dev,
2478109323Ssam		    "timeout in wi_cmd 0x%04x; event status 0x%04x\n", cmd, s);
2479123098Simp		if (s == 0xffff)
2480123098Simp			sc->wi_gone = 1;
2481109323Ssam		return(ETIMEDOUT);
248253702Swpaul	}
2483109323Ssam	return (0);
2484109323Ssam}
248553702Swpaul
2486109323Ssamstatic int
2487109323Ssamwi_seek_bap(struct wi_softc *sc, int id, int off)
2488109323Ssam{
2489109323Ssam	int i, status;
249090580Sbrooks
2491109323Ssam	CSR_WRITE_2(sc, WI_SEL0, id);
2492109323Ssam	CSR_WRITE_2(sc, WI_OFF0, off);
249390580Sbrooks
2494109323Ssam	for (i = 0; ; i++) {
2495109323Ssam		status = CSR_READ_2(sc, WI_OFF0);
2496109323Ssam		if ((status & WI_OFF_BUSY) == 0)
2497109323Ssam			break;
2498109323Ssam		if (i == WI_TIMEOUT) {
2499109323Ssam			device_printf(sc->sc_dev, "timeout in wi_seek to %x/%x\n",
2500109323Ssam			    id, off);
2501109323Ssam			sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2502123098Simp			if (status == 0xffff)
2503123098Simp				sc->wi_gone = 1;
2504109323Ssam			return ETIMEDOUT;
2505109323Ssam		}
2506109323Ssam		DELAY(1);
250753702Swpaul	}
2508109323Ssam	if (status & WI_OFF_ERR) {
2509109323Ssam		device_printf(sc->sc_dev, "failed in wi_seek to %x/%x\n", id, off);
2510109323Ssam		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2511109323Ssam		return EIO;
2512109323Ssam	}
2513109323Ssam	sc->sc_bap_id = id;
2514109323Ssam	sc->sc_bap_off = off;
2515109323Ssam	return 0;
251653702Swpaul}
251753702Swpaul
2518109323Ssamstatic int
2519109323Ssamwi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
252053702Swpaul{
2521109323Ssam	u_int16_t *ptr;
2522109323Ssam	int i, error, cnt;
252353702Swpaul
2524109323Ssam	if (buflen == 0)
2525109323Ssam		return 0;
2526109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2527109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
2528109323Ssam			return error;
252975219Salfred	}
2530109323Ssam	cnt = (buflen + 1) / 2;
2531109323Ssam	ptr = (u_int16_t *)buf;
2532109323Ssam	for (i = 0; i < cnt; i++)
2533109323Ssam		*ptr++ = CSR_READ_2(sc, WI_DATA0);
2534109323Ssam	sc->sc_bap_off += cnt * 2;
2535109323Ssam	return 0;
253653702Swpaul}
253753702Swpaul
2538109323Ssamstatic int
2539109323Ssamwi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
254053702Swpaul{
2541109323Ssam	u_int16_t *ptr;
2542109323Ssam	int i, error, cnt;
254346492Swpaul
2544109323Ssam	if (buflen == 0)
2545109323Ssam		return 0;
254646492Swpaul
2547109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
2548109323Ssam  again:
2549109323Ssam#endif
2550109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2551109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
2552109323Ssam			return error;
2553109323Ssam	}
2554109323Ssam	cnt = (buflen + 1) / 2;
2555109323Ssam	ptr = (u_int16_t *)buf;
2556109323Ssam	for (i = 0; i < cnt; i++)
2557109323Ssam		CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
2558109323Ssam	sc->sc_bap_off += cnt * 2;
2559109323Ssam
2560109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
2561109323Ssam	/*
2562109323Ssam	 * According to the comments in the HCF Light code, there is a bug
2563109323Ssam	 * in the Hermes (or possibly in certain Hermes firmware revisions)
2564109323Ssam	 * where the chip's internal autoincrement counter gets thrown off
2565109323Ssam	 * during data writes:  the autoincrement is missed, causing one
2566109323Ssam	 * data word to be overwritten and subsequent words to be written to
2567109323Ssam	 * the wrong memory locations. The end result is that we could end
2568109323Ssam	 * up transmitting bogus frames without realizing it. The workaround
2569109323Ssam	 * for this is to write a couple of extra guard words after the end
2570109323Ssam	 * of the transfer, then attempt to read then back. If we fail to
2571109323Ssam	 * locate the guard words where we expect them, we preform the
2572109323Ssam	 * transfer over again.
2573109323Ssam	 */
2574109323Ssam	if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
2575109323Ssam		CSR_WRITE_2(sc, WI_DATA0, 0x1234);
2576109323Ssam		CSR_WRITE_2(sc, WI_DATA0, 0x5678);
2577109323Ssam		wi_seek_bap(sc, id, sc->sc_bap_off);
2578109323Ssam		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2579109323Ssam		if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
2580109323Ssam		    CSR_READ_2(sc, WI_DATA0) != 0x5678) {
2581109323Ssam			device_printf(sc->sc_dev,
2582109323Ssam				"detect auto increment bug, try again\n");
2583109323Ssam			goto again;
2584109323Ssam		}
2585109323Ssam	}
2586109323Ssam#endif
2587109323Ssam	return 0;
258846492Swpaul}
258953702Swpaul
2590109323Ssamstatic int
2591109323Ssamwi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
2592109323Ssam{
2593109323Ssam	int error, len;
2594109323Ssam	struct mbuf *m;
259553702Swpaul
2596109323Ssam	for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
2597109323Ssam		if (m->m_len == 0)
2598109323Ssam			continue;
259953702Swpaul
2600109323Ssam		len = min(m->m_len, totlen);
260153702Swpaul
2602109323Ssam		if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
2603109323Ssam			m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
2604109323Ssam			return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
2605109323Ssam			    totlen);
2606109323Ssam		}
260753702Swpaul
2608109323Ssam		if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
2609109323Ssam			return error;
261053702Swpaul
2611109323Ssam		off += m->m_len;
2612109323Ssam		totlen -= len;
2613109323Ssam	}
2614109323Ssam	return 0;
2615109323Ssam}
261653702Swpaul
2617109323Ssamstatic int
2618109323Ssamwi_alloc_fid(struct wi_softc *sc, int len, int *idp)
261953702Swpaul{
262053702Swpaul	int i;
262153702Swpaul
2622109323Ssam	if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
2623109323Ssam		device_printf(sc->sc_dev, "failed to allocate %d bytes on NIC\n",
2624109323Ssam		    len);
2625109323Ssam		return ENOMEM;
262653702Swpaul	}
262753702Swpaul
2628109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
2629109323Ssam		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
2630109323Ssam			break;
2631109323Ssam		DELAY(1);
263253702Swpaul	}
2633144167Ssam	if (i == WI_TIMEOUT) {
2634144167Ssam		device_printf(sc->sc_dev, "timeout in alloc\n");
2635144167Ssam		return ETIMEDOUT;
2636144167Ssam	}
2637109323Ssam	*idp = CSR_READ_2(sc, WI_ALLOC_FID);
2638109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
2639109323Ssam	return 0;
2640109323Ssam}
264153702Swpaul
2642109323Ssamstatic int
2643109323Ssamwi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
2644109323Ssam{
2645109323Ssam	int error, len;
2646109323Ssam	u_int16_t ltbuf[2];
264753702Swpaul
2648109323Ssam	/* Tell the NIC to enter record read mode. */
2649109323Ssam	error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2650109323Ssam	if (error)
2651109323Ssam		return error;
265253702Swpaul
2653109323Ssam	error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2654109323Ssam	if (error)
2655109323Ssam		return error;
265653702Swpaul
2657109323Ssam	if (le16toh(ltbuf[1]) != rid) {
2658109323Ssam		device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n",
2659109323Ssam		    rid, le16toh(ltbuf[1]));
2660109323Ssam		return EIO;
266153702Swpaul	}
2662109323Ssam	len = (le16toh(ltbuf[0]) - 1) * 2;	 /* already got rid */
2663109323Ssam	if (*buflenp < len) {
2664109323Ssam		device_printf(sc->sc_dev, "record buffer is too small, "
2665109323Ssam		    "rid=%x, size=%d, len=%d\n",
2666109323Ssam		    rid, *buflenp, len);
2667109323Ssam		return ENOSPC;
266853702Swpaul	}
2669109323Ssam	*buflenp = len;
2670109323Ssam	return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2671109323Ssam}
267253702Swpaul
2673109323Ssamstatic int
2674109323Ssamwi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2675109323Ssam{
2676109323Ssam	int error;
2677109323Ssam	u_int16_t ltbuf[2];
267853702Swpaul
2679109323Ssam	ltbuf[0] = htole16((buflen + 1) / 2 + 1);	 /* includes rid */
2680109323Ssam	ltbuf[1] = htole16(rid);
268153702Swpaul
2682109323Ssam	error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2683109323Ssam	if (error)
2684109323Ssam		return error;
2685109323Ssam	error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2686109323Ssam	if (error)
2687109323Ssam		return error;
2688102206Simp
2689109323Ssam	return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
269053702Swpaul}
269177217Sphk
269288546Salfredstatic int
2693117812Ssamwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
269477217Sphk{
2695138571Ssam	struct ifnet *ifp = ic->ic_ifp;
2696117812Ssam	struct wi_softc *sc = ifp->if_softc;
2697139542Ssam	struct ieee80211_node *ni;
2698116951Ssam	int buflen;
2699109323Ssam	u_int16_t val;
2700109323Ssam	struct wi_ssid ssid;
2701109323Ssam	u_int8_t old_bssid[IEEE80211_ADDR_LEN];
270277217Sphk
2703117812Ssam	DPRINTF(("%s: %s -> %s\n", __func__,
2704117812Ssam		ieee80211_state_name[ic->ic_state],
2705117812Ssam		ieee80211_state_name[nstate]));
2706109323Ssam
2707139542Ssam	/*
2708139542Ssam	 * Internal to the driver the INIT and RUN states are used
2709139542Ssam	 * so bypass the net80211 state machine for other states.
2710139542Ssam	 * Beware however that this requires use to net80211 state
2711139542Ssam	 * management that otherwise would be handled for us.
2712139542Ssam	 */
2713109323Ssam	switch (nstate) {
2714109323Ssam	case IEEE80211_S_INIT:
2715109323Ssam		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2716117812Ssam		return (*sc->sc_newstate)(ic, nstate, arg);
2717109323Ssam
2718138571Ssam	case IEEE80211_S_SCAN:
2719138571Ssam	case IEEE80211_S_AUTH:
2720138571Ssam	case IEEE80211_S_ASSOC:
2721138571Ssam		ic->ic_state = nstate;	/* NB: skip normal ieee80211 handling */
2722138571Ssam		break;
2723138571Ssam
2724109323Ssam	case IEEE80211_S_RUN:
2725139542Ssam		ni = ic->ic_bss;
2726109323Ssam		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2727109323Ssam		buflen = IEEE80211_ADDR_LEN;
2728138571Ssam		IEEE80211_ADDR_COPY(old_bssid, ni->ni_bssid);
2729109323Ssam		wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen);
2730109323Ssam		IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
2731109323Ssam		buflen = sizeof(val);
2732109323Ssam		wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
2733116951Ssam		/* XXX validate channel */
2734116951Ssam		ni->ni_chan = &ic->ic_channels[le16toh(val)];
2735138951Ssam		ic->ic_ibss_chan = ni->ni_chan;
2736119784Ssam#if NBPFILTER > 0
2737119784Ssam		sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
2738119784Ssam			htole16(ni->ni_chan->ic_freq);
2739119784Ssam		sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
2740119784Ssam			htole16(ni->ni_chan->ic_flags);
2741119784Ssam#endif
2742139542Ssam		if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
2743138952Ssam			/*
2744138952Ssam			 * XXX hack; unceremoniously clear
2745138952Ssam			 * IEEE80211_F_DROPUNENC when operating with
2746138952Ssam			 * wep enabled so we don't drop unencoded frames
2747138952Ssam			 * at the 802.11 layer.  This is necessary because
2748138952Ssam			 * we must strip the WEP bit from the 802.11 header
2749138952Ssam			 * before passing frames to ieee80211_input because
2750138952Ssam			 * the card has already stripped the WEP crypto
2751138952Ssam			 * header from the packet.
2752138952Ssam			 */
2753138952Ssam			if (ic->ic_flags & IEEE80211_F_PRIVACY)
2754138952Ssam				ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
2755109323Ssam			/* XXX check return value */
2756109323Ssam			buflen = sizeof(ssid);
2757109323Ssam			wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
2758109323Ssam			ni->ni_esslen = le16toh(ssid.wi_len);
2759109323Ssam			if (ni->ni_esslen > IEEE80211_NWID_LEN)
2760109323Ssam				ni->ni_esslen = IEEE80211_NWID_LEN;	/*XXX*/
2761109323Ssam			memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
276277217Sphk		}
2763138571Ssam		return (*sc->sc_newstate)(ic, nstate, arg);
276477217Sphk	}
2765117812Ssam	return 0;
276677217Sphk}
276777217Sphk
276888546Salfredstatic int
2769116898Ssamwi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate)
277077217Sphk{
2771109323Ssam	int error = 0;
2772109323Ssam	u_int16_t val[2];
277377217Sphk
2774109323Ssam	if (!sc->sc_enabled)
2775109323Ssam		return ENXIO;
2776109323Ssam	switch (sc->sc_firmware_type) {
2777109323Ssam	case WI_LUCENT:
2778109323Ssam		(void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
277998440Simp		break;
2780109323Ssam	case WI_INTERSIL:
2781138571Ssam		val[0] = htole16(chanmask);	/* channel */
2782138571Ssam		val[1] = htole16(txrate);	/* tx rate */
2783109323Ssam		error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
278498440Simp		break;
2785109323Ssam	case WI_SYMBOL:
2786109323Ssam		/*
2787109323Ssam		 * XXX only supported on 3.x ?
2788109323Ssam		 */
2789109323Ssam		val[0] = BSCAN_BCAST | BSCAN_ONETIME;
2790109323Ssam		error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
2791109323Ssam		    val, sizeof(val[0]));
279298440Simp		break;
279398440Simp	}
2794109323Ssam	if (error == 0) {
2795109323Ssam		sc->sc_scan_timer = WI_SCAN_WAIT;
2796147256Sbrooks		sc->sc_ifp->if_timer = 1;
2797116898Ssam		DPRINTF(("wi_scan_ap: start scanning, "
2798116898Ssam			"chamask 0x%x txrate 0x%x\n", chanmask, txrate));
2799109323Ssam	}
2800109323Ssam	return error;
2801109323Ssam}
280277217Sphk
2803109323Ssamstatic void
2804109323Ssamwi_scan_result(struct wi_softc *sc, int fid, int cnt)
2805109323Ssam{
2806109323Ssam#define	N(a)	(sizeof (a) / sizeof (a[0]))
2807109323Ssam	int i, naps, off, szbuf;
2808109323Ssam	struct wi_scan_header ws_hdr;	/* Prism2 header */
2809109323Ssam	struct wi_scan_data_p2 ws_dat;	/* Prism2 scantable*/
2810109323Ssam	struct wi_apinfo *ap;
2811109323Ssam
2812109323Ssam	off = sizeof(u_int16_t) * 2;
2813109323Ssam	memset(&ws_hdr, 0, sizeof(ws_hdr));
2814109323Ssam	switch (sc->sc_firmware_type) {
2815109323Ssam	case WI_INTERSIL:
2816109323Ssam		wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
2817109323Ssam		off += sizeof(ws_hdr);
2818109323Ssam		szbuf = sizeof(struct wi_scan_data_p2);
281977217Sphk		break;
2820109323Ssam	case WI_SYMBOL:
2821109323Ssam		szbuf = sizeof(struct wi_scan_data_p2) + 6;
282277217Sphk		break;
2823109323Ssam	case WI_LUCENT:
2824109323Ssam		szbuf = sizeof(struct wi_scan_data);
282577217Sphk		break;
2826109323Ssam	default:
2827109323Ssam		device_printf(sc->sc_dev,
2828109323Ssam			"wi_scan_result: unknown firmware type %u\n",
2829109323Ssam			sc->sc_firmware_type);
2830109323Ssam		naps = 0;
2831109323Ssam		goto done;
283277217Sphk	}
2833109323Ssam	naps = (cnt * 2 + 2 - off) / szbuf;
2834109323Ssam	if (naps > N(sc->sc_aps))
2835109323Ssam		naps = N(sc->sc_aps);
2836109323Ssam	sc->sc_naps = naps;
2837109323Ssam	/* Read Data */
2838109323Ssam	ap = sc->sc_aps;
2839109323Ssam	memset(&ws_dat, 0, sizeof(ws_dat));
2840109323Ssam	for (i = 0; i < naps; i++, ap++) {
2841109323Ssam		wi_read_bap(sc, fid, off, &ws_dat,
2842109323Ssam		    (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
2843109323Ssam		DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off,
2844109323Ssam		    ether_sprintf(ws_dat.wi_bssid)));
2845109323Ssam		off += szbuf;
2846109323Ssam		ap->scanreason = le16toh(ws_hdr.wi_reason);
2847109323Ssam		memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
2848109323Ssam		ap->channel = le16toh(ws_dat.wi_chid);
2849109323Ssam		ap->signal  = le16toh(ws_dat.wi_signal);
2850109323Ssam		ap->noise   = le16toh(ws_dat.wi_noise);
2851109323Ssam		ap->quality = ap->signal - ap->noise;
2852109323Ssam		ap->capinfo = le16toh(ws_dat.wi_capinfo);
2853109323Ssam		ap->interval = le16toh(ws_dat.wi_interval);
2854109323Ssam		ap->rate    = le16toh(ws_dat.wi_rate);
2855109323Ssam		ap->namelen = le16toh(ws_dat.wi_namelen);
2856109323Ssam		if (ap->namelen > sizeof(ap->name))
2857109323Ssam			ap->namelen = sizeof(ap->name);
2858109323Ssam		memcpy(ap->name, ws_dat.wi_name, ap->namelen);
2859109323Ssam	}
2860109323Ssamdone:
2861109323Ssam	/* Done scanning */
2862109323Ssam	sc->sc_scan_timer = 0;
2863109323Ssam	DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
2864109323Ssam#undef N
286577217Sphk}
286677217Sphk
286788546Salfredstatic void
2868109323Ssamwi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi)
286977217Sphk{
2870109323Ssam	ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr),
2871116951Ssam	    ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL : -1, rssi);
2872109323Ssam	printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n",
2873109323Ssam		le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1),
2874109323Ssam		le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence);
2875109323Ssam	printf(" rx_signal %u rx_rate %u rx_flow %u\n",
2876109323Ssam		wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow);
2877109323Ssam	printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n",
2878109323Ssam		wh->wi_tx_rtry, wh->wi_tx_rate,
2879109323Ssam		le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len));
2880109323Ssam	printf(" ehdr dst %6D src %6D type 0x%x\n",
2881109323Ssam		wh->wi_ehdr.ether_dhost, ":", wh->wi_ehdr.ether_shost, ":",
2882109323Ssam		wh->wi_ehdr.ether_type);
2883109323Ssam}
288477217Sphk
2885109323Ssamint
2886109323Ssamwi_alloc(device_t dev, int rid)
2887109323Ssam{
2888109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2889109323Ssam
2890109323Ssam	if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2891109323Ssam		sc->iobase_rid = rid;
2892109323Ssam		sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2893109323Ssam		    &sc->iobase_rid, 0, ~0, (1 << 6),
2894109323Ssam		    rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2895109323Ssam		if (!sc->iobase) {
2896109323Ssam			device_printf(dev, "No I/O space?!\n");
2897109323Ssam			return (ENXIO);
289898440Simp		}
2899109323Ssam
2900109323Ssam		sc->wi_io_addr = rman_get_start(sc->iobase);
2901109323Ssam		sc->wi_btag = rman_get_bustag(sc->iobase);
2902109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2903109323Ssam	} else {
2904109323Ssam		sc->mem_rid = rid;
2905127135Snjl		sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2906127135Snjl		    &sc->mem_rid, RF_ACTIVE);
2907109323Ssam
2908109323Ssam		if (!sc->mem) {
2909109323Ssam			device_printf(dev, "No Mem space on prism2.5?\n");
2910109323Ssam			return (ENXIO);
291177217Sphk		}
2912109323Ssam
2913109323Ssam		sc->wi_btag = rman_get_bustag(sc->mem);
2914109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->mem);
291577217Sphk	}
291677217Sphk
2917109323Ssam
2918109323Ssam	sc->irq_rid = 0;
2919127135Snjl	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2920127135Snjl	    RF_ACTIVE |
2921109323Ssam	    ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2922109323Ssam
2923109323Ssam	if (!sc->irq) {
2924109323Ssam		wi_free(dev);
2925109323Ssam		device_printf(dev, "No irq?!\n");
2926109323Ssam		return (ENXIO);
292777217Sphk	}
2928109323Ssam
2929109323Ssam	sc->sc_dev = dev;
2930109323Ssam	sc->sc_unit = device_get_unit(dev);
2931109323Ssam
2932109323Ssam	return (0);
293377217Sphk}
293493359Simp
2935109323Ssamvoid
2936109323Ssamwi_free(device_t dev)
2937109323Ssam{
2938109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2939109323Ssam
2940109323Ssam	if (sc->iobase != NULL) {
2941109323Ssam		bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2942109323Ssam		sc->iobase = NULL;
2943109323Ssam	}
2944109323Ssam	if (sc->irq != NULL) {
2945109323Ssam		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2946109323Ssam		sc->irq = NULL;
2947109323Ssam	}
2948109323Ssam	if (sc->mem != NULL) {
2949109323Ssam		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
2950109323Ssam		sc->mem = NULL;
2951109323Ssam	}
2952109323Ssam
2953109323Ssam	return;
2954109323Ssam}
2955109323Ssam
295693359Simpstatic int
2957109323Ssamwi_get_debug(struct wi_softc *sc, struct wi_req *wreq)
295893359Simp{
2959109323Ssam	int error = 0;
296093359Simp
296193359Simp	wreq->wi_len = 1;
296293359Simp
296393359Simp	switch (wreq->wi_type) {
296493359Simp	case WI_DEBUG_SLEEP:
296593359Simp		wreq->wi_len++;
296693359Simp		wreq->wi_val[0] = sc->wi_debug.wi_sleep;
296793359Simp		break;
296893359Simp	case WI_DEBUG_DELAYSUPP:
296993359Simp		wreq->wi_len++;
297093359Simp		wreq->wi_val[0] = sc->wi_debug.wi_delaysupp;
297193359Simp		break;
297293359Simp	case WI_DEBUG_TXSUPP:
297393359Simp		wreq->wi_len++;
297493359Simp		wreq->wi_val[0] = sc->wi_debug.wi_txsupp;
297593359Simp		break;
297693359Simp	case WI_DEBUG_MONITOR:
297793359Simp		wreq->wi_len++;
297893359Simp		wreq->wi_val[0] = sc->wi_debug.wi_monitor;
297993359Simp		break;
298093359Simp	case WI_DEBUG_LEDTEST:
298193359Simp		wreq->wi_len += 3;
298293359Simp		wreq->wi_val[0] = sc->wi_debug.wi_ledtest;
298393359Simp		wreq->wi_val[1] = sc->wi_debug.wi_ledtest_param0;
298493359Simp		wreq->wi_val[2] = sc->wi_debug.wi_ledtest_param1;
298593359Simp		break;
298693359Simp	case WI_DEBUG_CONTTX:
298793359Simp		wreq->wi_len += 2;
298893359Simp		wreq->wi_val[0] = sc->wi_debug.wi_conttx;
298993359Simp		wreq->wi_val[1] = sc->wi_debug.wi_conttx_param0;
299093359Simp		break;
299193359Simp	case WI_DEBUG_CONTRX:
299293359Simp		wreq->wi_len++;
299393359Simp		wreq->wi_val[0] = sc->wi_debug.wi_contrx;
299493359Simp		break;
299593359Simp	case WI_DEBUG_SIGSTATE:
299693359Simp		wreq->wi_len += 2;
299793359Simp		wreq->wi_val[0] = sc->wi_debug.wi_sigstate;
299893359Simp		wreq->wi_val[1] = sc->wi_debug.wi_sigstate_param0;
299993359Simp		break;
300093359Simp	case WI_DEBUG_CONFBITS:
300193359Simp		wreq->wi_len += 2;
300293359Simp		wreq->wi_val[0] = sc->wi_debug.wi_confbits;
300393359Simp		wreq->wi_val[1] = sc->wi_debug.wi_confbits_param0;
300493359Simp		break;
300593359Simp	default:
300693359Simp		error = EIO;
300793359Simp		break;
300893359Simp	}
300993359Simp
301093359Simp	return (error);
301193359Simp}
301293359Simp
301393359Simpstatic int
3014109323Ssamwi_set_debug(struct wi_softc *sc, struct wi_req *wreq)
301593359Simp{
3016109323Ssam	int error = 0;
301793359Simp	u_int16_t		cmd, param0 = 0, param1 = 0;
301893359Simp
301993359Simp	switch (wreq->wi_type) {
302093359Simp	case WI_DEBUG_RESET:
302193359Simp	case WI_DEBUG_INIT:
302293359Simp	case WI_DEBUG_CALENABLE:
302393359Simp		break;
302493359Simp	case WI_DEBUG_SLEEP:
302593359Simp		sc->wi_debug.wi_sleep = 1;
302693359Simp		break;
302793359Simp	case WI_DEBUG_WAKE:
302893359Simp		sc->wi_debug.wi_sleep = 0;
302993359Simp		break;
303093359Simp	case WI_DEBUG_CHAN:
303193359Simp		param0 = wreq->wi_val[0];
303293359Simp		break;
303393359Simp	case WI_DEBUG_DELAYSUPP:
303493359Simp		sc->wi_debug.wi_delaysupp = 1;
303593359Simp		break;
303693359Simp	case WI_DEBUG_TXSUPP:
303793359Simp		sc->wi_debug.wi_txsupp = 1;
303893359Simp		break;
303993359Simp	case WI_DEBUG_MONITOR:
304093359Simp		sc->wi_debug.wi_monitor = 1;
304193359Simp		break;
304293359Simp	case WI_DEBUG_LEDTEST:
304393359Simp		param0 = wreq->wi_val[0];
304493359Simp		param1 = wreq->wi_val[1];
304593359Simp		sc->wi_debug.wi_ledtest = 1;
304693359Simp		sc->wi_debug.wi_ledtest_param0 = param0;
304793359Simp		sc->wi_debug.wi_ledtest_param1 = param1;
304893359Simp		break;
304993359Simp	case WI_DEBUG_CONTTX:
305093359Simp		param0 = wreq->wi_val[0];
305193359Simp		sc->wi_debug.wi_conttx = 1;
305293359Simp		sc->wi_debug.wi_conttx_param0 = param0;
305393359Simp		break;
305493359Simp	case WI_DEBUG_STOPTEST:
305593359Simp		sc->wi_debug.wi_delaysupp = 0;
305693359Simp		sc->wi_debug.wi_txsupp = 0;
305793359Simp		sc->wi_debug.wi_monitor = 0;
305893359Simp		sc->wi_debug.wi_ledtest = 0;
305993359Simp		sc->wi_debug.wi_ledtest_param0 = 0;
306093359Simp		sc->wi_debug.wi_ledtest_param1 = 0;
306193359Simp		sc->wi_debug.wi_conttx = 0;
306293359Simp		sc->wi_debug.wi_conttx_param0 = 0;
306393359Simp		sc->wi_debug.wi_contrx = 0;
306493359Simp		sc->wi_debug.wi_sigstate = 0;
306593359Simp		sc->wi_debug.wi_sigstate_param0 = 0;
306693359Simp		break;
306793359Simp	case WI_DEBUG_CONTRX:
306893359Simp		sc->wi_debug.wi_contrx = 1;
306993359Simp		break;
307093359Simp	case WI_DEBUG_SIGSTATE:
307193359Simp		param0 = wreq->wi_val[0];
307293359Simp		sc->wi_debug.wi_sigstate = 1;
307393359Simp		sc->wi_debug.wi_sigstate_param0 = param0;
307493359Simp		break;
307593359Simp	case WI_DEBUG_CONFBITS:
307693359Simp		param0 = wreq->wi_val[0];
307793359Simp		param1 = wreq->wi_val[1];
307893359Simp		sc->wi_debug.wi_confbits = param0;
307993359Simp		sc->wi_debug.wi_confbits_param0 = param1;
308093359Simp		break;
308193359Simp	default:
308293359Simp		error = EIO;
308393359Simp		break;
308493359Simp	}
308593359Simp
308693359Simp	if (error)
308793359Simp		return (error);
308893359Simp
308993359Simp	cmd = WI_CMD_DEBUG | (wreq->wi_type << 8);
309093359Simp	error = wi_cmd(sc, cmd, param0, param1, 0);
309193359Simp
309293359Simp	return (error);
309393359Simp}
3094101903Simp
3095105076Simp#if __FreeBSD_version >= 500000
3096101903Simp/*
3097101903Simp * Special routines to download firmware for Symbol CF card.
3098101903Simp * XXX: This should be modified generic into any PRISM-2 based card.
3099101903Simp */
3100101903Simp
3101101903Simp#define	WI_SBCF_PDIADDR		0x3100
3102101903Simp
3103101903Simp/* unaligned load little endian */
3104101903Simp#define	GETLE32(p)	((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24))
3105101903Simp#define	GETLE16(p)	((p)[0] | ((p)[1]<<8))
3106101903Simp
3107101903Simpint
3108101903Simpwi_symbol_load_firm(struct wi_softc *sc, const void *primsym, int primlen,
3109101903Simp    const void *secsym, int seclen)
3110101903Simp{
3111101903Simp	uint8_t ebuf[256];
3112101903Simp	int i;
3113101903Simp
3114101903Simp	/* load primary code and run it */
3115101903Simp	wi_symbol_set_hcr(sc, WI_HCR_EEHOLD);
3116101903Simp	if (wi_symbol_write_firm(sc, primsym, primlen, NULL, 0))
3117101903Simp		return EIO;
3118101903Simp	wi_symbol_set_hcr(sc, WI_HCR_RUN);
3119101903Simp	for (i = 0; ; i++) {
3120101903Simp		if (i == 10)
3121101903Simp			return ETIMEDOUT;
3122101903Simp		tsleep(sc, PWAIT, "wiinit", 1);
3123101903Simp		if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT)
3124101903Simp			break;
3125101903Simp		/* write the magic key value to unlock aux port */
3126101903Simp		CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0);
3127101903Simp		CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1);
3128101903Simp		CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2);
3129101903Simp		CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL);
3130101903Simp	}
3131101903Simp
3132101903Simp	/* issue read EEPROM command: XXX copied from wi_cmd() */
3133101903Simp	CSR_WRITE_2(sc, WI_PARAM0, 0);
3134101903Simp	CSR_WRITE_2(sc, WI_PARAM1, 0);
3135101903Simp	CSR_WRITE_2(sc, WI_PARAM2, 0);
3136101903Simp	CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE);
3137101903Simp        for (i = 0; i < WI_TIMEOUT; i++) {
3138101903Simp                if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
3139101903Simp                        break;
3140101903Simp                DELAY(1);
3141101903Simp        }
3142101903Simp        CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
3143101903Simp
3144101903Simp	CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ);
3145101903Simp	CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ);
3146101903Simp	CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA,
3147101903Simp	    (uint16_t *)ebuf, sizeof(ebuf) / 2);
3148101903Simp	if (GETLE16(ebuf) > sizeof(ebuf))
3149101903Simp		return EIO;
3150101903Simp	if (wi_symbol_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf)))
3151101903Simp		return EIO;
3152101903Simp	return 0;
3153101903Simp}
3154101903Simp
3155101903Simpstatic int
3156101903Simpwi_symbol_write_firm(struct wi_softc *sc, const void *buf, int buflen,
3157101903Simp    const void *ebuf, int ebuflen)
3158101903Simp{
3159101903Simp	const uint8_t *p, *ep, *q, *eq;
3160101903Simp	char *tp;
3161101903Simp	uint32_t addr, id, eid;
3162101903Simp	int i, len, elen, nblk, pdrlen;
3163101903Simp
3164101903Simp	/*
3165101903Simp	 * Parse the header of the firmware image.
3166101903Simp	 */
3167101903Simp	p = buf;
3168101903Simp	ep = p + buflen;
3169101903Simp	while (p < ep && *p++ != ' ');	/* FILE: */
3170101903Simp	while (p < ep && *p++ != ' ');	/* filename */
3171101903Simp	while (p < ep && *p++ != ' ');	/* type of the firmware */
3172101903Simp	nblk = strtoul(p, &tp, 10);
3173101903Simp	p = tp;
3174101903Simp	pdrlen = strtoul(p + 1, &tp, 10);
3175101903Simp	p = tp;
3176101903Simp	while (p < ep && *p++ != 0x1a);	/* skip rest of header */
3177101903Simp
3178101903Simp	/*
3179101903Simp	 * Block records: address[4], length[2], data[length];
3180101903Simp	 */
3181101903Simp	for (i = 0; i < nblk; i++) {
3182101903Simp		addr = GETLE32(p);	p += 4;
3183101903Simp		len  = GETLE16(p);	p += 2;
3184101903Simp		CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3185101903Simp		CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3186101903Simp		CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3187101903Simp		    (const uint16_t *)p, len / 2);
3188101903Simp		p += len;
3189101903Simp	}
3190101903Simp
3191101903Simp	/*
3192101903Simp	 * PDR: id[4], address[4], length[4];
3193101903Simp	 */
3194101903Simp	for (i = 0; i < pdrlen; ) {
3195101903Simp		id   = GETLE32(p);	p += 4; i += 4;
3196101903Simp		addr = GETLE32(p);	p += 4; i += 4;
3197101903Simp		len  = GETLE32(p);	p += 4; i += 4;
3198101903Simp		/* replace PDR entry with the values from EEPROM, if any */
3199101903Simp		for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) {
3200101903Simp			elen = GETLE16(q);	q += 2;
3201101903Simp			eid  = GETLE16(q);	q += 2;
3202101903Simp			elen--;		/* elen includes eid */
3203101903Simp			if (eid == 0)
3204101903Simp				break;
3205101903Simp			if (eid != id)
3206101903Simp				continue;
3207101903Simp			CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3208101903Simp			CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3209101903Simp			CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3210101903Simp			    (const uint16_t *)q, len / 2);
3211101903Simp			break;
3212101903Simp		}
3213101903Simp	}
3214101903Simp	return 0;
3215101903Simp}
3216101903Simp
3217101903Simpstatic int
3218101903Simpwi_symbol_set_hcr(struct wi_softc *sc, int mode)
3219101903Simp{
3220101903Simp	uint16_t hcr;
3221101903Simp
3222101903Simp	CSR_WRITE_2(sc, WI_COR, WI_COR_RESET);
3223101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3224101903Simp	hcr = CSR_READ_2(sc, WI_HCR);
3225101903Simp	hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE);
3226101903Simp	CSR_WRITE_2(sc, WI_HCR, hcr);
3227101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3228101903Simp	CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE);
3229101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3230101903Simp	return 0;
3231101903Simp}
3232105076Simp#endif
3233