if_wi.c revision 127135
1109323Ssam/*	$NetBSD: wi.c,v 1.109 2003/01/09 08:52:19 dyoung Exp $	*/
2109323Ssam
346492Swpaul/*
446492Swpaul * Copyright (c) 1997, 1998, 1999
546492Swpaul *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
646492Swpaul *
746492Swpaul * Redistribution and use in source and binary forms, with or without
846492Swpaul * modification, are permitted provided that the following conditions
946492Swpaul * are met:
1046492Swpaul * 1. Redistributions of source code must retain the above copyright
1146492Swpaul *    notice, this list of conditions and the following disclaimer.
1246492Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1346492Swpaul *    notice, this list of conditions and the following disclaimer in the
1446492Swpaul *    documentation and/or other materials provided with the distribution.
1546492Swpaul * 3. All advertising materials mentioning features or use of this software
1646492Swpaul *    must display the following acknowledgement:
1746492Swpaul *	This product includes software developed by Bill Paul.
1846492Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1946492Swpaul *    may be used to endorse or promote products derived from this software
2046492Swpaul *    without specific prior written permission.
2146492Swpaul *
2246492Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2346492Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2446492Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2546492Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2646492Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2746492Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2846492Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2946492Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3046492Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3146492Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3246492Swpaul * THE POSSIBILITY OF SUCH DAMAGE.
3346492Swpaul */
3446492Swpaul
3546492Swpaul/*
36109323Ssam * Lucent WaveLAN/IEEE 802.11 PCMCIA driver.
3746492Swpaul *
38109323Ssam * Original FreeBSD driver written by Bill Paul <wpaul@ctr.columbia.edu>
3946492Swpaul * Electrical Engineering Department
4046492Swpaul * Columbia University, New York City
4146492Swpaul */
4246492Swpaul
4346492Swpaul/*
4447401Swpaul * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
4546492Swpaul * from Lucent. Unlike the older cards, the new ones are programmed
4646492Swpaul * entirely via a firmware-driven controller called the Hermes.
4746492Swpaul * Unfortunately, Lucent will not release the Hermes programming manual
4846492Swpaul * without an NDA (if at all). What they do release is an API library
4946492Swpaul * called the HCF (Hardware Control Functions) which is supposed to
5046492Swpaul * do the device-specific operations of a device driver for you. The
5146492Swpaul * publically available version of the HCF library (the 'HCF Light') is
5247401Swpaul * a) extremely gross, b) lacks certain features, particularly support
5346492Swpaul * for 802.11 frames, and c) is contaminated by the GNU Public License.
5446492Swpaul *
5546492Swpaul * This driver does not use the HCF or HCF Light at all. Instead, it
5646492Swpaul * programs the Hermes controller directly, using information gleaned
5746492Swpaul * from the HCF Light code and corresponding documentation.
5846492Swpaul *
5995534Simp * This driver supports the ISA, PCMCIA and PCI versions of the Lucent
6095534Simp * WaveLan cards (based on the Hermes chipset), as well as the newer
6195534Simp * Prism 2 chipsets with firmware from Intersil and Symbol.
6246492Swpaul */
6346492Swpaul
64113038Sobrien#include <sys/cdefs.h>
65113038Sobrien__FBSDID("$FreeBSD: head/sys/dev/wi/if_wi.c 127135 2004-03-17 17:50:55Z njl $");
66113038Sobrien
67109323Ssam#define WI_HERMES_AUTOINC_WAR	/* Work around data write autoinc bug. */
68109323Ssam#define WI_HERMES_STATS_WAR	/* Work around stats counter bug. */
69109323Ssam
70109323Ssam#define	NBPFILTER	1
71109323Ssam
7246492Swpaul#include <sys/param.h>
7346492Swpaul#include <sys/systm.h>
7495706Simp#if __FreeBSD_version >= 500033
7595533Smike#include <sys/endian.h>
7695706Simp#endif
7746492Swpaul#include <sys/sockio.h>
7846492Swpaul#include <sys/mbuf.h>
7983366Sjulian#include <sys/proc.h>
8046492Swpaul#include <sys/kernel.h>
8146492Swpaul#include <sys/socket.h>
8253702Swpaul#include <sys/module.h>
8353702Swpaul#include <sys/bus.h>
8494486Simp#include <sys/random.h>
8553702Swpaul#include <sys/syslog.h>
8653702Swpaul#include <sys/sysctl.h>
8746492Swpaul
8853702Swpaul#include <machine/bus.h>
8953702Swpaul#include <machine/resource.h>
9095534Simp#include <machine/clock.h>
91116951Ssam#include <machine/atomic.h>
9253702Swpaul#include <sys/rman.h>
9353702Swpaul
9446492Swpaul#include <net/if.h>
9546492Swpaul#include <net/if_arp.h>
9646492Swpaul#include <net/ethernet.h>
9746492Swpaul#include <net/if_dl.h>
9846492Swpaul#include <net/if_media.h>
9946492Swpaul#include <net/if_types.h>
10046492Swpaul
101116951Ssam#include <net80211/ieee80211_var.h>
102116951Ssam#include <net80211/ieee80211_ioctl.h>
103119784Ssam#include <net80211/ieee80211_radiotap.h>
104116951Ssam
10546492Swpaul#include <netinet/in.h>
10646492Swpaul#include <netinet/in_systm.h>
10746492Swpaul#include <netinet/in_var.h>
10846492Swpaul#include <netinet/ip.h>
10946492Swpaul#include <netinet/if_ether.h>
11046492Swpaul
11146492Swpaul#include <net/bpf.h>
11246492Swpaul
11370808Speter#include <dev/wi/if_wavelan_ieee.h>
114119784Ssam#include <dev/wi/if_wireg.h>
11593611Simp#include <dev/wi/if_wivar.h>
11646492Swpaul
117109323Ssam#define IF_POLL(ifq, m)		((m) = (ifq)->ifq_head)
118109323Ssam#define	IFQ_POLL(ifq, m)	IF_POLL((ifq), (m))
119109323Ssam#define IFQ_DEQUEUE(ifq, m)	IF_DEQUEUE((ifq), (m))
120109323Ssam
12191693Simpstatic void wi_start(struct ifnet *);
122109323Ssamstatic int  wi_reset(struct wi_softc *);
12391693Simpstatic void wi_watchdog(struct ifnet *);
124109323Ssamstatic int  wi_ioctl(struct ifnet *, u_long, caddr_t);
125109323Ssamstatic int  wi_media_change(struct ifnet *);
126109323Ssamstatic void wi_media_status(struct ifnet *, struct ifmediareq *);
12746492Swpaul
128109323Ssamstatic void wi_rx_intr(struct wi_softc *);
129109323Ssamstatic void wi_tx_intr(struct wi_softc *);
130109323Ssamstatic void wi_tx_ex_intr(struct wi_softc *);
131109323Ssamstatic void wi_info_intr(struct wi_softc *);
13246492Swpaul
133109323Ssamstatic int  wi_get_cfg(struct ifnet *, u_long, caddr_t);
134109323Ssamstatic int  wi_set_cfg(struct ifnet *, u_long, caddr_t);
135109323Ssamstatic int  wi_write_txrate(struct wi_softc *);
136109323Ssamstatic int  wi_write_wep(struct wi_softc *);
137109323Ssamstatic int  wi_write_multi(struct wi_softc *);
138109323Ssamstatic int  wi_alloc_fid(struct wi_softc *, int, int *);
139109323Ssamstatic void wi_read_nicid(struct wi_softc *);
140109323Ssamstatic int  wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
14153702Swpaul
142109323Ssamstatic int  wi_cmd(struct wi_softc *, int, int, int, int);
143109323Ssamstatic int  wi_seek_bap(struct wi_softc *, int, int);
144109323Ssamstatic int  wi_read_bap(struct wi_softc *, int, int, void *, int);
145109323Ssamstatic int  wi_write_bap(struct wi_softc *, int, int, void *, int);
146109323Ssamstatic int  wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
147109323Ssamstatic int  wi_read_rid(struct wi_softc *, int, void *, int *);
148109323Ssamstatic int  wi_write_rid(struct wi_softc *, int, void *, int);
14977217Sphk
150117812Ssamstatic int  wi_newstate(struct ieee80211com *, enum ieee80211_state, int);
151109323Ssam
152116898Ssamstatic int  wi_scan_ap(struct wi_softc *, u_int16_t, u_int16_t);
153109323Ssamstatic void wi_scan_result(struct wi_softc *, int, int);
154109323Ssam
155109323Ssamstatic void wi_dump_pkt(struct wi_frame *, struct ieee80211_node *, int rssi);
156109323Ssam
15793359Simpstatic int wi_get_debug(struct wi_softc *, struct wi_req *);
15893359Simpstatic int wi_set_debug(struct wi_softc *, struct wi_req *);
15993359Simp
160105076Simp#if __FreeBSD_version >= 500000
161101903Simp/* support to download firmware for symbol CF card */
162101903Simpstatic int wi_symbol_write_firm(struct wi_softc *, const void *, int,
163101903Simp		const void *, int);
164101903Simpstatic int wi_symbol_set_hcr(struct wi_softc *, int);
165105076Simp#endif
166101903Simp
167109323Ssamstatic __inline int
168109323Ssamwi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
169109323Ssam{
17053702Swpaul
171109323Ssam	val = htole16(val);
172109323Ssam	return wi_write_rid(sc, rid, &val, sizeof(val));
173109323Ssam}
174109323Ssam
175109592SsamSYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters");
176109592Ssam
177109323Ssamstatic	struct timeval lasttxerror;	/* time of last tx error msg */
178109323Ssamstatic	int curtxeps;			/* current tx error msgs/sec */
179111559Ssamstatic	int wi_txerate = 0;		/* tx error rate: max msgs/sec */
180109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate,
181111559Ssam	    0, "max tx error msgs/sec; 0 to disable msgs");
182109323Ssam
183109323Ssam#define	WI_DEBUG
184109323Ssam#ifdef WI_DEBUG
185109323Ssamstatic	int wi_debug = 0;
186109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug,
187109592Ssam	    0, "control debugging printfs");
188109323Ssam
189109323Ssam#define	DPRINTF(X)	if (wi_debug) printf X
190109323Ssam#define	DPRINTF2(X)	if (wi_debug > 1) printf X
191109323Ssam#define	IFF_DUMPPKTS(_ifp) \
192109323Ssam	(((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
193109323Ssam#else
194109323Ssam#define	DPRINTF(X)
195109323Ssam#define	DPRINTF2(X)
196109323Ssam#define	IFF_DUMPPKTS(_ifp)	0
197109323Ssam#endif
198109323Ssam
199109323Ssam#define WI_INTRS	(WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
200109323Ssam
20193825Simpstruct wi_card_ident wi_card_ident[] = {
20293825Simp	/* CARD_ID			CARD_NAME		FIRM_TYPE */
20393825Simp	{ WI_NIC_LUCENT_ID,		WI_NIC_LUCENT_STR,	WI_LUCENT },
20493825Simp	{ WI_NIC_SONY_ID,		WI_NIC_SONY_STR,	WI_LUCENT },
20593825Simp	{ WI_NIC_LUCENT_EMB_ID,		WI_NIC_LUCENT_EMB_STR,	WI_LUCENT },
20693825Simp	{ WI_NIC_EVB2_ID,		WI_NIC_EVB2_STR,	WI_INTERSIL },
20793825Simp	{ WI_NIC_HWB3763_ID,		WI_NIC_HWB3763_STR,	WI_INTERSIL },
20893825Simp	{ WI_NIC_HWB3163_ID,		WI_NIC_HWB3163_STR,	WI_INTERSIL },
20993825Simp	{ WI_NIC_HWB3163B_ID,		WI_NIC_HWB3163B_STR,	WI_INTERSIL },
21093825Simp	{ WI_NIC_EVB3_ID,		WI_NIC_EVB3_STR,	WI_INTERSIL },
21193825Simp	{ WI_NIC_HWB1153_ID,		WI_NIC_HWB1153_STR,	WI_INTERSIL },
21293825Simp	{ WI_NIC_P2_SST_ID,		WI_NIC_P2_SST_STR,	WI_INTERSIL },
21393825Simp	{ WI_NIC_EVB2_SST_ID,		WI_NIC_EVB2_SST_STR,	WI_INTERSIL },
21493825Simp	{ WI_NIC_3842_EVA_ID,		WI_NIC_3842_EVA_STR,	WI_INTERSIL },
21593825Simp	{ WI_NIC_3842_PCMCIA_AMD_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
21693825Simp	{ WI_NIC_3842_PCMCIA_SST_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
217101355Simp	{ WI_NIC_3842_PCMCIA_ATL_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
218101355Simp	{ WI_NIC_3842_PCMCIA_ATS_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
21993825Simp	{ WI_NIC_3842_MINI_AMD_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
22093825Simp	{ WI_NIC_3842_MINI_SST_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
221101355Simp	{ WI_NIC_3842_MINI_ATL_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
222101355Simp	{ WI_NIC_3842_MINI_ATS_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
22393825Simp	{ WI_NIC_3842_PCI_AMD_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
22493825Simp	{ WI_NIC_3842_PCI_SST_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
225101355Simp	{ WI_NIC_3842_PCI_ATS_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
226101355Simp	{ WI_NIC_3842_PCI_ATL_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
22793825Simp	{ WI_NIC_P3_PCMCIA_AMD_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
22893825Simp	{ WI_NIC_P3_PCMCIA_SST_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
229101355Simp	{ WI_NIC_P3_PCMCIA_ATL_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
230101355Simp	{ WI_NIC_P3_PCMCIA_ATS_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
23193825Simp	{ WI_NIC_P3_MINI_AMD_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
23293825Simp	{ WI_NIC_P3_MINI_SST_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
233101355Simp	{ WI_NIC_P3_MINI_ATL_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
234101355Simp	{ WI_NIC_P3_MINI_ATS_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
23593825Simp	{ 0,	NULL,	0 },
23693825Simp};
23793825Simp
238109323Ssamdevclass_t wi_devclass;
23946492Swpaul
24093611Simpint
241109323Ssamwi_attach(device_t dev)
24274906Salfred{
243109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
244109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
245109323Ssam	struct ifnet *ifp = &ic->ic_if;
246116951Ssam	int i, nrates, buflen;
247109323Ssam	u_int16_t val;
248109323Ssam	u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
249116951Ssam	struct ieee80211_rateset *rs;
250109323Ssam	static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
251109323Ssam		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
252109323Ssam	};
253109323Ssam	int error;
25474906Salfred
255109323Ssam	/*
256109323Ssam	 * NB: no locking is needed here; don't put it here
257109323Ssam	 *     unless you can prove it!
258109323Ssam	 */
259121697Ssam	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
26074998Swpaul	    wi_intr, sc, &sc->wi_intrhand);
26153702Swpaul
26253702Swpaul	if (error) {
26353702Swpaul		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
26453702Swpaul		wi_free(dev);
26553702Swpaul		return (error);
26653702Swpaul	}
26753702Swpaul
26895534Simp#if __FreeBSD_version >= 500000
269109323Ssam	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
27093818Sjhb	    MTX_DEF | MTX_RECURSE);
27195534Simp#endif
27267092Swpaul
273123339Simp	sc->wi_cmd_count = 500;
27446492Swpaul	/* Reset the NIC. */
275112096Simp	if (wi_reset(sc) != 0)
276109323Ssam		return ENXIO;		/* XXX */
27746492Swpaul
27876438Swpaul	/*
27976438Swpaul	 * Read the station address.
28076438Swpaul	 * And do it twice. I've seen PRISM-based cards that return
28176438Swpaul	 * an error when trying to read it the first time, which causes
28276438Swpaul	 * the probe to fail.
28376438Swpaul	 */
284109323Ssam	buflen = IEEE80211_ADDR_LEN;
285109323Ssam	error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
286109323Ssam	if (error != 0) {
287109323Ssam		buflen = IEEE80211_ADDR_LEN;
288109323Ssam		error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
289109323Ssam	}
290109323Ssam	if (error || IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) {
291109323Ssam		if (error != 0)
292109323Ssam			device_printf(dev, "mac read failed %d\n", error);
293109323Ssam		else
294109323Ssam			device_printf(dev, "mac read failed (all zeros)\n");
29575149Simp		wi_free(dev);
29675149Simp		return (error);
29775149Simp	}
298109323Ssam	device_printf(dev, "802.11 address: %6D\n", ic->ic_myaddr, ":");
29946492Swpaul
300109323Ssam	/* Read NIC identification */
301109323Ssam	wi_read_nicid(sc);
30246492Swpaul
30346492Swpaul	ifp->if_softc = sc;
304121816Sbrooks	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
30546492Swpaul	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
30646492Swpaul	ifp->if_ioctl = wi_ioctl;
30746492Swpaul	ifp->if_start = wi_start;
30846492Swpaul	ifp->if_watchdog = wi_watchdog;
30946492Swpaul	ifp->if_init = wi_init;
31046492Swpaul	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
31146492Swpaul
312109323Ssam	ic->ic_phytype = IEEE80211_T_DS;
313109323Ssam	ic->ic_opmode = IEEE80211_M_STA;
314116951Ssam	ic->ic_caps = IEEE80211_C_PMGT | IEEE80211_C_AHDEMO;
315109323Ssam	ic->ic_state = IEEE80211_S_INIT;
31646492Swpaul
317116951Ssam	/*
318116951Ssam	 * Query the card for available channels and setup the
319116951Ssam	 * channel table.  We assume these are all 11b channels.
320116951Ssam	 */
321109323Ssam	buflen = sizeof(val);
322109323Ssam	if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
323109323Ssam		val = htole16(0x1fff);	/* assume 1-11 */
324116951Ssam	KASSERT(val != 0, ("wi_attach: no available channels listed!"));
325116951Ssam
326116951Ssam	val <<= 1;			/* shift for base 1 indices */
327116951Ssam	for (i = 1; i < 16; i++) {
328116951Ssam		if (isset((u_int8_t*)&val, i)) {
329116951Ssam			ic->ic_channels[i].ic_freq =
330116951Ssam				ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
331116951Ssam			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
332116951Ssam		}
333109323Ssam	}
33446492Swpaul
33546563Swpaul	/*
33646563Swpaul	 * Read the default channel from the NIC. This may vary
33746563Swpaul	 * depending on the country where the NIC was purchased, so
33846563Swpaul	 * we can't hard-code a default and expect it to work for
33946563Swpaul	 * everyone.
340116951Ssam	 *
341116951Ssam	 * If no channel is specified, let the 802.11 code select.
34246563Swpaul	 */
343109323Ssam	buflen = sizeof(val);
344116951Ssam	if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0) {
345116951Ssam		val = le16toh(val);
346116951Ssam		KASSERT(val < IEEE80211_CHAN_MAX &&
347116951Ssam			ic->ic_channels[val].ic_flags != 0,
348116951Ssam			("wi_attach: invalid own channel %u!", val));
349116951Ssam		ic->ic_ibss_chan = &ic->ic_channels[val];
350117042Ssam	} else {
351117042Ssam		device_printf(dev,
352117042Ssam			"WI_RID_OWN_CHNL failed, using first channel!\n");
353117042Ssam		ic->ic_ibss_chan = &ic->ic_channels[0];
354109323Ssam	}
35546563Swpaul
35656965Swpaul	/*
35798440Simp	 * Set flags based on firmware version.
35898440Simp	 */
35998440Simp	switch (sc->sc_firmware_type) {
36098440Simp	case WI_LUCENT:
361112363Simp		sc->sc_ntxbuf = 1;
362109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
363109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
364109323Ssam		/* XXX: not confirmed, but never seen for recent firmware */
365109323Ssam		if (sc->sc_sta_firmware_ver <  40000) {
366109323Ssam			sc->sc_flags |= WI_FLAGS_BUG_AUTOINC;
367109323Ssam		}
368109323Ssam#endif
36998440Simp		if (sc->sc_sta_firmware_ver >= 60000)
370109323Ssam			sc->sc_flags |= WI_FLAGS_HAS_MOR;
371117855Ssam		if (sc->sc_sta_firmware_ver >= 60006) {
372116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
373117855Ssam			ic->ic_caps |= IEEE80211_C_MONITOR;
374117855Ssam		}
375109323Ssam		sc->sc_ibss_port = htole16(1);
376119784Ssam
377119784Ssam		sc->sc_min_rssi = WI_LUCENT_MIN_RSSI;
378119784Ssam		sc->sc_max_rssi = WI_LUCENT_MAX_RSSI;
379119784Ssam		sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
38098440Simp		break;
381109323Ssam
38298440Simp	case WI_INTERSIL:
383112363Simp		sc->sc_ntxbuf = WI_NTXBUF;
384109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR;
385109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_ROAMING;
386109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
387123339Simp		/*
388123339Simp		 * Old firmware are slow, so give peace a chance.
389123339Simp		 */
390123339Simp		if (sc->sc_sta_firmware_ver < 10000)
391123339Simp			sc->wi_cmd_count = 5000;
392109323Ssam		if (sc->sc_sta_firmware_ver > 10101)
393109323Ssam			sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
394117855Ssam		if (sc->sc_sta_firmware_ver >= 800) {
395116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
396117855Ssam			ic->ic_caps |= IEEE80211_C_MONITOR;
397117855Ssam		}
398109396Simp		/*
399109396Simp		 * version 0.8.3 and newer are the only ones that are known
400109396Simp		 * to currently work.  Earlier versions can be made to work,
401109396Simp		 * at least according to the Linux driver.
402109396Simp		 */
403100734Simp		if (sc->sc_sta_firmware_ver >= 803)
404116951Ssam			ic->ic_caps |= IEEE80211_C_HOSTAP;
405109323Ssam		sc->sc_ibss_port = htole16(0);
406119784Ssam
407119784Ssam		sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
408119784Ssam		sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
409119784Ssam		sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
41098440Simp		break;
411109323Ssam
41298440Simp	case WI_SYMBOL:
413112363Simp		sc->sc_ntxbuf = 1;
414109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY;
41598440Simp		if (sc->sc_sta_firmware_ver >= 25000)
416116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
417109323Ssam		sc->sc_ibss_port = htole16(4);
418119784Ssam
419119784Ssam		sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
420119784Ssam		sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
421119784Ssam		sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
42298440Simp		break;
42398440Simp	}
42498440Simp
42598440Simp	/*
42656965Swpaul	 * Find out if we support WEP on this card.
42756965Swpaul	 */
428109323Ssam	buflen = sizeof(val);
429109323Ssam	if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
430109323Ssam	    val != htole16(0))
431116951Ssam		ic->ic_caps |= IEEE80211_C_WEP;
43256965Swpaul
433109323Ssam	/* Find supported rates. */
434109323Ssam	buflen = sizeof(ratebuf);
435116951Ssam	rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
436109323Ssam	if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
437116951Ssam		nrates = le16toh(*(u_int16_t *)ratebuf);
438116951Ssam		if (nrates > IEEE80211_RATE_MAXSIZE)
439116951Ssam			nrates = IEEE80211_RATE_MAXSIZE;
440116951Ssam		rs->rs_nrates = 0;
441116951Ssam		for (i = 0; i < nrates; i++)
442116951Ssam			if (ratebuf[2+i])
443116951Ssam				rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i];
444109323Ssam	} else {
445109323Ssam		/* XXX fallback on error? */
446116951Ssam		rs->rs_nrates = 0;
44798440Simp	}
44877217Sphk
449109323Ssam	buflen = sizeof(val);
450109323Ssam	if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
451109323Ssam	    wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
452119784Ssam		sc->sc_dbm_offset = le16toh(val);
453119784Ssam	}
45446492Swpaul
455109323Ssam	sc->sc_max_datalen = 2304;
456109323Ssam	sc->sc_system_scale = 1;
457109323Ssam	sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN;
458109323Ssam	sc->sc_roaming_mode = 1;
45946492Swpaul
460109323Ssam	sc->sc_portnum = WI_DEFAULT_PORT;
461109323Ssam	sc->sc_authtype = WI_DEFAULT_AUTHTYPE;
46287383Simp
463109323Ssam	bzero(sc->sc_nodename, sizeof(sc->sc_nodename));
464109323Ssam	sc->sc_nodelen = sizeof(WI_DEFAULT_NODENAME) - 1;
465109323Ssam	bcopy(WI_DEFAULT_NODENAME, sc->sc_nodename, sc->sc_nodelen);
46687383Simp
467109323Ssam	bzero(sc->sc_net_name, sizeof(sc->sc_net_name));
468109323Ssam	bcopy(WI_DEFAULT_NETNAME, sc->sc_net_name,
469109323Ssam	    sizeof(WI_DEFAULT_NETNAME) - 1);
47093733Simp
47193733Simp	/*
472109323Ssam	 * Call MI attach routine.
47393733Simp	 */
474109323Ssam	ieee80211_ifattach(ifp);
475117812Ssam	/* override state transition method */
476117812Ssam	sc->sc_newstate = ic->ic_newstate;
477117812Ssam	ic->ic_newstate = wi_newstate;
478116951Ssam	ieee80211_media_init(ifp, wi_media_change, wi_media_status);
479109323Ssam
480119784Ssam#if NBPFILTER > 0
481119784Ssam	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
482119784Ssam		sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
483119784Ssam		&sc->sc_drvbpf);
484119784Ssam	/*
485119784Ssam	 * Initialize constant fields.
486119784Ssam	 *
487119784Ssam	 * NB: the channel is setup each time we transition to the
488119784Ssam	 *     RUN state to avoid filling it in for each frame.
489119784Ssam	 */
490119784Ssam	sc->sc_tx_th.wt_ihdr.it_len = sizeof(sc->sc_tx_th);
491119784Ssam	sc->sc_tx_th.wt_ihdr.it_present = WI_TX_RADIOTAP_PRESENT;
492119784Ssam
493119784Ssam	sc->sc_rx_th.wr_ihdr.it_len = sizeof(sc->sc_rx_th);
494123927Ssam	sc->sc_rx_th.wr_ihdr.it_present = WI_RX_RADIOTAP_PRESENT;
495119784Ssam#endif
496109323Ssam	return (0);
49787383Simp}
49887383Simp
499109323Ssamint
500109323Ssamwi_detach(device_t dev)
50146492Swpaul{
502109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
503109323Ssam	struct ifnet *ifp = &sc->sc_ic.ic_if;
504109323Ssam	WI_LOCK_DECL();
50546492Swpaul
506109323Ssam	WI_LOCK(sc);
50746492Swpaul
508109323Ssam	/* check if device was removed */
509123098Simp	sc->wi_gone |= !bus_child_present(dev);
51046492Swpaul
511109323Ssam	wi_stop(ifp, 0);
51246492Swpaul
513119784Ssam#if NBPFILTER > 0
514119784Ssam	bpfdetach(ifp);
515119784Ssam#endif
516109323Ssam	ieee80211_ifdetach(ifp);
517114190Simp	WI_UNLOCK(sc);
518109323Ssam	bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
519109323Ssam	wi_free(dev);
520109323Ssam#if __FreeBSD_version >= 500000
521109323Ssam	mtx_destroy(&sc->sc_mtx);
52293359Simp#endif
523109323Ssam	return (0);
524109323Ssam}
52593359Simp
526109323Ssam#ifdef __NetBSD__
527109323Ssamint
528109323Ssamwi_activate(struct device *self, enum devact act)
529109323Ssam{
530109323Ssam	struct wi_softc *sc = (struct wi_softc *)self;
531109323Ssam	int rv = 0, s;
53293359Simp
533109323Ssam	s = splnet();
534109323Ssam	switch (act) {
535109323Ssam	case DVACT_ACTIVATE:
536109323Ssam		rv = EOPNOTSUPP;
537109323Ssam		break;
53893359Simp
539109323Ssam	case DVACT_DEACTIVATE:
540109323Ssam		if_deactivate(&sc->sc_ic.ic_if);
541109323Ssam		break;
542109323Ssam	}
543109323Ssam	splx(s);
544109323Ssam	return rv;
545109323Ssam}
54693359Simp
547109323Ssamvoid
548109323Ssamwi_power(struct wi_softc *sc, int why)
549109323Ssam{
550109323Ssam	struct ifnet *ifp = &sc->sc_ic.ic_if;
551109323Ssam	int s;
55293359Simp
553109323Ssam	s = splnet();
554109323Ssam	switch (why) {
555109323Ssam	case PWR_SUSPEND:
556109323Ssam	case PWR_STANDBY:
557109323Ssam		wi_stop(ifp, 1);
558109323Ssam		break;
559109323Ssam	case PWR_RESUME:
560109323Ssam		if (ifp->if_flags & IFF_UP) {
561109323Ssam			wi_init(ifp);
562109323Ssam			(void)wi_intr(sc);
56394405Simp		}
564109323Ssam		break;
565109323Ssam	case PWR_SOFTSUSPEND:
566109323Ssam	case PWR_SOFTSTANDBY:
567109323Ssam	case PWR_SOFTRESUME:
568109323Ssam		break;
56993359Simp	}
570109323Ssam	splx(s);
57146492Swpaul}
572109323Ssam#endif /* __NetBSD__ */
57346492Swpaul
574109323Ssamvoid
575109323Ssamwi_shutdown(device_t dev)
57646492Swpaul{
577109323Ssam	struct wi_softc *sc = device_get_softc(dev);
57846492Swpaul
579109323Ssam	wi_stop(&sc->sc_if, 1);
58046492Swpaul}
58146492Swpaul
582109323Ssamvoid
583109323Ssamwi_intr(void *arg)
58446492Swpaul{
585109323Ssam	struct wi_softc *sc = arg;
586109323Ssam	struct ifnet *ifp = &sc->sc_ic.ic_if;
587112363Simp	u_int16_t status;
588109323Ssam	WI_LOCK_DECL();
58946492Swpaul
590109323Ssam	WI_LOCK(sc);
59146492Swpaul
592123098Simp	if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) {
593113327Simp		CSR_WRITE_2(sc, WI_INT_EN, 0);
594123098Simp		CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
595109323Ssam		WI_UNLOCK(sc);
59646492Swpaul		return;
597109323Ssam	}
59846492Swpaul
599112363Simp	/* Disable interrupts. */
600112363Simp	CSR_WRITE_2(sc, WI_INT_EN, 0);
60146492Swpaul
602112363Simp	status = CSR_READ_2(sc, WI_EVENT_STAT);
603112363Simp	if (status & WI_EV_RX)
604112363Simp		wi_rx_intr(sc);
605112363Simp	if (status & WI_EV_ALLOC)
606112363Simp		wi_tx_intr(sc);
607112363Simp	if (status & WI_EV_TX_EXC)
608112363Simp		wi_tx_ex_intr(sc);
609112363Simp	if (status & WI_EV_INFO)
610112363Simp		wi_info_intr(sc);
611112363Simp	if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
612112363Simp	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 &&
613112363Simp	    _IF_QLEN(&ifp->if_snd) != 0)
614112363Simp		wi_start(ifp);
61546492Swpaul
616112363Simp	/* Re-enable interrupts. */
617112363Simp	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
61846492Swpaul
619109323Ssam	WI_UNLOCK(sc);
62046492Swpaul
62146492Swpaul	return;
62246492Swpaul}
62346492Swpaul
624109323Ssamvoid
625109323Ssamwi_init(void *arg)
62646492Swpaul{
627109323Ssam	struct wi_softc *sc = arg;
628109323Ssam	struct ifnet *ifp = &sc->sc_if;
629109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
630109323Ssam	struct wi_joinreq join;
631109323Ssam	int i;
632109323Ssam	int error = 0, wasenabled;
633109323Ssam	struct ifaddr *ifa;
634109323Ssam	struct sockaddr_dl *sdl;
635109323Ssam	WI_LOCK_DECL();
63646492Swpaul
637109323Ssam	WI_LOCK(sc);
63867092Swpaul
639109323Ssam	if (sc->wi_gone) {
640109323Ssam		WI_UNLOCK(sc);
64146492Swpaul		return;
64246492Swpaul	}
64346492Swpaul
644112362Simp	if ((wasenabled = sc->sc_enabled))
645123098Simp		wi_stop(ifp, 1);
646112362Simp	wi_reset(sc);
64746492Swpaul
648109323Ssam	/* common 802.11 configuration */
649109323Ssam	ic->ic_flags &= ~IEEE80211_F_IBSSON;
650109323Ssam	sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
651109323Ssam	switch (ic->ic_opmode) {
652109323Ssam	case IEEE80211_M_STA:
653109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS);
654109323Ssam		break;
655109323Ssam	case IEEE80211_M_IBSS:
656109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port);
657109323Ssam		ic->ic_flags |= IEEE80211_F_IBSSON;
658109323Ssam		break;
659109323Ssam	case IEEE80211_M_AHDEMO:
660109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
661109323Ssam		break;
662109323Ssam	case IEEE80211_M_HOSTAP:
663122087Sgreen		/*
664122087Sgreen		 * For PRISM cards, override the empty SSID, because in
665122087Sgreen		 * HostAP mode the controller will lock up otherwise.
666122087Sgreen		 */
667122087Sgreen		if (sc->sc_firmware_type == WI_INTERSIL &&
668122087Sgreen		    ic->ic_des_esslen == 0) {
669122087Sgreen			ic->ic_des_essid[0] = ' ';
670122087Sgreen			ic->ic_des_esslen = 1;
671122087Sgreen		}
672109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP);
673109323Ssam		break;
674117855Ssam	case IEEE80211_M_MONITOR:
675117855Ssam		if (sc->sc_firmware_type == WI_LUCENT)
676117855Ssam			wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
677117855Ssam		wi_cmd(sc, WI_CMD_DEBUG | (WI_TEST_MONITOR << 8), 0, 0, 0);
678117855Ssam		break;
67946492Swpaul	}
68046492Swpaul
681109323Ssam	/* Intersil interprets this RID as joining ESS even in IBSS mode */
682109323Ssam	if (sc->sc_firmware_type == WI_LUCENT &&
683109323Ssam	    (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0)
684109323Ssam		wi_write_val(sc, WI_RID_CREATE_IBSS, 1);
685109323Ssam	else
686109323Ssam		wi_write_val(sc, WI_RID_CREATE_IBSS, 0);
687109323Ssam	wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
688109323Ssam	wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid,
689109323Ssam	    ic->ic_des_esslen);
690116951Ssam	wi_write_val(sc, WI_RID_OWN_CHNL,
691116951Ssam		ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
692109323Ssam	wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen);
69346492Swpaul
694109323Ssam	ifa = ifaddr_byindex(ifp->if_index);
695109323Ssam	sdl = (struct sockaddr_dl *) ifa->ifa_addr;
696109323Ssam	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(sdl));
697109323Ssam	wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN);
69875373Salfred
699109323Ssam	wi_write_val(sc, WI_RID_PM_ENABLED,
700109323Ssam	    (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
70146492Swpaul
702109323Ssam	/* not yet common 802.11 configuration */
703109323Ssam	wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen);
704116951Ssam	wi_write_val(sc, WI_RID_RTS_THRESH, ic->ic_rtsthreshold);
705109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
706116951Ssam		wi_write_val(sc, WI_RID_FRAG_THRESH, ic->ic_fragthreshold);
70746492Swpaul
708109323Ssam	/* driver specific 802.11 configuration */
709109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)
710109323Ssam		wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale);
711109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
712109323Ssam		wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode);
713109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_MOR)
714109323Ssam		wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven);
715109323Ssam	wi_write_txrate(sc);
716109323Ssam	wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen);
71746492Swpaul
718109323Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
719109323Ssam	    sc->sc_firmware_type == WI_INTERSIL) {
720109323Ssam		wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval);
721109323Ssam		wi_write_val(sc, WI_RID_BASIC_RATE, 0x03);   /* 1, 2 */
722109323Ssam		wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */
723109323Ssam		wi_write_val(sc, WI_RID_DTIM_PERIOD, 1);
72446492Swpaul	}
72546492Swpaul
726109323Ssam	/*
727109323Ssam	 * Initialize promisc mode.
728109323Ssam	 *	Being in the Host-AP mode causes a great
729109323Ssam	 *	deal of pain if primisc mode is set.
730109323Ssam	 *	Therefore we avoid confusing the firmware
731109323Ssam	 *	and always reset promisc mode in Host-AP
732109323Ssam	 *	mode.  Host-AP sees all the packets anyway.
733109323Ssam	 */
734109323Ssam	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
735109323Ssam	    (ifp->if_flags & IFF_PROMISC) != 0) {
736109323Ssam		wi_write_val(sc, WI_RID_PROMISC, 1);
737109323Ssam	} else {
738109323Ssam		wi_write_val(sc, WI_RID_PROMISC, 0);
73975373Salfred	}
74046492Swpaul
741109323Ssam	/* Configure WEP. */
742116951Ssam	if (ic->ic_caps & IEEE80211_C_WEP)
743109323Ssam		wi_write_wep(sc);
74467092Swpaul
745109323Ssam	/* Set multicast filter. */
746109323Ssam	wi_write_multi(sc);
74746492Swpaul
748114124Simp	/* Allocate fids for the card */
749109323Ssam	if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
750109323Ssam		sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
751109323Ssam		if (sc->sc_firmware_type == WI_SYMBOL)
752109323Ssam			sc->sc_buflen = 1585;	/* XXX */
753112363Simp		for (i = 0; i < sc->sc_ntxbuf; i++) {
754109323Ssam			error = wi_alloc_fid(sc, sc->sc_buflen,
755109323Ssam			    &sc->sc_txd[i].d_fid);
756109323Ssam			if (error) {
757109323Ssam				device_printf(sc->sc_dev,
758109323Ssam				    "tx buffer allocation failed (error %u)\n",
759109323Ssam				    error);
760109323Ssam				goto out;
761109323Ssam			}
762109323Ssam			sc->sc_txd[i].d_len = 0;
76370073Swpaul		}
76470073Swpaul	}
765109323Ssam	sc->sc_txcur = sc->sc_txnext = 0;
76670073Swpaul
767109323Ssam	/* Enable desired port */
768109323Ssam	wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
76946492Swpaul
770114124Simp	sc->sc_enabled = 1;
771109323Ssam	ifp->if_flags |= IFF_RUNNING;
772109323Ssam	ifp->if_flags &= ~IFF_OACTIVE;
773109323Ssam	if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
774117855Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR ||
775109323Ssam	    ic->ic_opmode == IEEE80211_M_HOSTAP)
776117812Ssam		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
77746492Swpaul
778109323Ssam	/* Enable interrupts */
779109323Ssam	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
78046492Swpaul
781109323Ssam	if (!wasenabled &&
782109323Ssam	    ic->ic_opmode == IEEE80211_M_HOSTAP &&
783109323Ssam	    sc->sc_firmware_type == WI_INTERSIL) {
784109323Ssam		/* XXX: some card need to be re-enabled for hostap */
785109323Ssam		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
786109323Ssam		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
78775149Simp	}
78894397Simp
789109323Ssam	if (ic->ic_opmode == IEEE80211_M_STA &&
790109323Ssam	    ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
791116951Ssam	    ic->ic_des_chan != IEEE80211_CHAN_ANYC)) {
792109323Ssam		memset(&join, 0, sizeof(join));
793109323Ssam		if (ic->ic_flags & IEEE80211_F_DESBSSID)
794109323Ssam			IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
795116951Ssam		if (ic->ic_des_chan != IEEE80211_CHAN_ANYC)
796116951Ssam			join.wi_chan = htole16(
797116951Ssam				ieee80211_chan2ieee(ic, ic->ic_des_chan));
798109323Ssam		/* Lucent firmware does not support the JOIN RID. */
799109323Ssam		if (sc->sc_firmware_type != WI_LUCENT)
800109323Ssam			wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
80194397Simp	}
80275373Salfred
803109323Ssam	WI_UNLOCK(sc);
80446492Swpaul	return;
805109323Ssamout:
806109323Ssam	if (error) {
807109323Ssam		if_printf(ifp, "interface not running\n");
808123098Simp		wi_stop(ifp, 1);
80970073Swpaul	}
810110224Simp	WI_UNLOCK(sc);
811109323Ssam	DPRINTF(("wi_init: return %d\n", error));
812109323Ssam	return;
81346492Swpaul}
81446492Swpaul
815109323Ssamvoid
816109323Ssamwi_stop(struct ifnet *ifp, int disable)
81746492Swpaul{
818117812Ssam	struct ieee80211com *ic = (struct ieee80211com *) ifp;
819109323Ssam	struct wi_softc *sc = ifp->if_softc;
820109323Ssam	WI_LOCK_DECL();
82146492Swpaul
822109323Ssam	WI_LOCK(sc);
82374998Swpaul
824123098Simp	DELAY(100000);
825123098Simp
826117812Ssam	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
827109323Ssam	if (sc->sc_enabled && !sc->wi_gone) {
828109323Ssam		CSR_WRITE_2(sc, WI_INT_EN, 0);
829109323Ssam		wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
830109323Ssam		if (disable) {
831109323Ssam#ifdef __NetBSD__
832109323Ssam			if (sc->sc_disable)
833109323Ssam				(*sc->sc_disable)(sc);
834109323Ssam#endif
835116951Ssam			sc->sc_enabled = 0;
83670073Swpaul		}
837123098Simp	} else if (sc->wi_gone && disable)	/* gone --> not enabled */
838123098Simp	    sc->sc_enabled = 0;
83970073Swpaul
840109323Ssam	sc->sc_tx_timer = 0;
841109323Ssam	sc->sc_scan_timer = 0;
842109323Ssam	sc->sc_syn_timer = 0;
843109323Ssam	sc->sc_false_syns = 0;
844109323Ssam	sc->sc_naps = 0;
845109323Ssam	ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
846109323Ssam	ifp->if_timer = 0;
84746492Swpaul
848109323Ssam	WI_UNLOCK(sc);
84946492Swpaul}
85046492Swpaul
851109323Ssamstatic void
852109323Ssamwi_start(struct ifnet *ifp)
85346492Swpaul{
854109323Ssam	struct wi_softc	*sc = ifp->if_softc;
855109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
856119150Ssam	struct ieee80211_node *ni;
857109323Ssam	struct ieee80211_frame *wh;
858109323Ssam	struct mbuf *m0;
859109323Ssam	struct wi_frame frmhdr;
860119150Ssam	int cur, fid, off, error;
861109323Ssam	WI_LOCK_DECL();
86246492Swpaul
863109323Ssam	WI_LOCK(sc);
86446492Swpaul
865109323Ssam	if (sc->wi_gone) {
866109323Ssam		WI_UNLOCK(sc);
867109323Ssam		return;
86846492Swpaul	}
869109323Ssam	if (sc->sc_flags & WI_FLAGS_OUTRANGE) {
870109323Ssam		WI_UNLOCK(sc);
871109323Ssam		return;
87275373Salfred	}
87346492Swpaul
874109323Ssam	memset(&frmhdr, 0, sizeof(frmhdr));
875109323Ssam	cur = sc->sc_txnext;
876109323Ssam	for (;;) {
877109323Ssam		IF_POLL(&ic->ic_mgtq, m0);
878109323Ssam		if (m0 != NULL) {
879109323Ssam			if (sc->sc_txd[cur].d_len != 0) {
880109323Ssam				ifp->if_flags |= IFF_OACTIVE;
881109323Ssam				break;
882109323Ssam			}
883109323Ssam			IF_DEQUEUE(&ic->ic_mgtq, m0);
884119150Ssam			/*
885119150Ssam			 * Hack!  The referenced node pointer is in the
886119150Ssam			 * rcvif field of the packet header.  This is
887119150Ssam			 * placed there by ieee80211_mgmt_output because
888119150Ssam			 * we need to hold the reference with the frame
889119150Ssam			 * and there's no other way (other than packet
890119150Ssam			 * tags which we consider too expensive to use)
891119150Ssam			 * to pass it along.
892119150Ssam			 */
893119150Ssam			ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
894119150Ssam			m0->m_pkthdr.rcvif = NULL;
895119150Ssam
896109323Ssam			m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
897109323Ssam			    (caddr_t)&frmhdr.wi_ehdr);
898109323Ssam			frmhdr.wi_ehdr.ether_type = 0;
899109323Ssam                        wh = mtod(m0, struct ieee80211_frame *);
900109323Ssam		} else {
901109323Ssam			if (ic->ic_state != IEEE80211_S_RUN)
902109323Ssam				break;
903109323Ssam			IFQ_POLL(&ifp->if_snd, m0);
904109323Ssam			if (m0 == NULL)
905109323Ssam				break;
906109323Ssam			if (sc->sc_txd[cur].d_len != 0) {
907109323Ssam				ifp->if_flags |= IFF_OACTIVE;
908109323Ssam				break;
909109323Ssam			}
910109323Ssam			IFQ_DEQUEUE(&ifp->if_snd, m0);
911109323Ssam			ifp->if_opackets++;
912109323Ssam			m_copydata(m0, 0, ETHER_HDR_LEN,
913109323Ssam			    (caddr_t)&frmhdr.wi_ehdr);
914109323Ssam#if NBPFILTER > 0
915109323Ssam			BPF_MTAP(ifp, m0);
916109323Ssam#endif
91746492Swpaul
918119150Ssam			m0 = ieee80211_encap(ifp, m0, &ni);
919119150Ssam			if (m0 == NULL) {
920109323Ssam				ifp->if_oerrors++;
921109323Ssam				continue;
922109323Ssam			}
923109323Ssam                        wh = mtod(m0, struct ieee80211_frame *);
924109323Ssam			if (ic->ic_flags & IEEE80211_F_WEPON)
925109323Ssam				wh->i_fc[1] |= IEEE80211_FC1_WEP;
92646492Swpaul
927109323Ssam		}
928109323Ssam#if NBPFILTER > 0
929109323Ssam		if (ic->ic_rawbpf)
930109323Ssam			bpf_mtap(ic->ic_rawbpf, m0);
931109323Ssam#endif
932109323Ssam		frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
933109323Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
934109323Ssam		    (wh->i_fc[1] & IEEE80211_FC1_WEP)) {
935109323Ssam			if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) {
936109323Ssam				ifp->if_oerrors++;
937119150Ssam				if (ni && ni != ic->ic_bss)
938119150Ssam					ieee80211_free_node(ic, ni);
939109323Ssam				continue;
940109323Ssam			}
941109323Ssam			frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
942109323Ssam		}
943109323Ssam		m_copydata(m0, 0, sizeof(struct ieee80211_frame),
944109323Ssam		    (caddr_t)&frmhdr.wi_whdr);
945109323Ssam		m_adj(m0, sizeof(struct ieee80211_frame));
946109323Ssam		frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
947109323Ssam#if NBPFILTER > 0
948109323Ssam		if (sc->sc_drvbpf) {
949123927Ssam			sc->sc_tx_th.wt_rate =
950123927Ssam				ni->ni_rates.rs_rates[ni->ni_txrate];
951123922Ssam			bpf_mtap2(sc->sc_drvbpf,
952123922Ssam				&sc->sc_tx_th, sizeof(sc->sc_tx_th), m0);
953109323Ssam		}
95446492Swpaul#endif
955109323Ssam		if (IFF_DUMPPKTS(ifp))
956116951Ssam			wi_dump_pkt(&frmhdr, NULL, -1);
957109323Ssam		fid = sc->sc_txd[cur].d_fid;
958109323Ssam		off = sizeof(frmhdr);
959119150Ssam		error = wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0
960119150Ssam		     || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
961119150Ssam		m_freem(m0);
962119150Ssam		if (ni && ni != ic->ic_bss)
963119150Ssam			ieee80211_free_node(ic, ni);
964119150Ssam		if (error) {
965109323Ssam			ifp->if_oerrors++;
966109323Ssam			continue;
967109323Ssam		}
968109323Ssam		sc->sc_txd[cur].d_len = off;
969109323Ssam		if (sc->sc_txcur == cur) {
970109323Ssam			if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
971109323Ssam				if_printf(ifp, "xmit failed\n");
972109323Ssam				sc->sc_txd[cur].d_len = 0;
973109323Ssam				continue;
974109323Ssam			}
975109323Ssam			sc->sc_tx_timer = 5;
976109323Ssam			ifp->if_timer = 1;
977109323Ssam		}
978112363Simp		sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
97974838Salfred	}
98046492Swpaul
981109323Ssam	WI_UNLOCK(sc);
98246492Swpaul}
98346492Swpaul
98488546Salfredstatic int
985109323Ssamwi_reset(struct wi_softc *sc)
98646492Swpaul{
987114124Simp	struct ieee80211com *ic = &sc->sc_ic;
988114124Simp	struct ifnet *ifp = &ic->ic_if;
989114124Simp#define WI_INIT_TRIES 3
990114124Simp	int i;
991114138Simp	int error = 0;
992114124Simp	int tries;
993114124Simp
994114124Simp	/* Symbol firmware cannot be initialized more than once */
995123098Simp	if (sc->sc_firmware_type == WI_SYMBOL && sc->sc_reset)
996112362Simp		return (0);
997114124Simp	if (sc->sc_firmware_type == WI_SYMBOL)
998114124Simp		tries = 1;
999114124Simp	else
1000114124Simp		tries = WI_INIT_TRIES;
1001112362Simp
1002114124Simp	for (i = 0; i < tries; i++) {
1003109323Ssam		if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
100446492Swpaul			break;
1005109323Ssam		DELAY(WI_DELAY * 1000);
100646492Swpaul	}
1007114124Simp	sc->sc_reset = 1;
100846492Swpaul
1009114124Simp	if (i == tries) {
1010114124Simp		if_printf(ifp, "init failed\n");
1011114124Simp		return (error);
101275373Salfred	}
101346492Swpaul
1014109323Ssam	CSR_WRITE_2(sc, WI_INT_EN, 0);
1015114124Simp	CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
101646492Swpaul
1017109323Ssam	/* Calibrate timer. */
1018114124Simp	wi_write_val(sc, WI_RID_TICK_TIME, 8);
1019114124Simp
1020114124Simp	return (0);
1021109323Ssam#undef WI_INIT_TRIES
102246492Swpaul}
102346492Swpaul
102488546Salfredstatic void
1025109323Ssamwi_watchdog(struct ifnet *ifp)
102646492Swpaul{
1027109323Ssam	struct wi_softc	*sc = ifp->if_softc;
102846492Swpaul
1029109323Ssam	ifp->if_timer = 0;
1030109323Ssam	if (!sc->sc_enabled)
1031109323Ssam		return;
103246492Swpaul
1033109323Ssam	if (sc->sc_tx_timer) {
1034109323Ssam		if (--sc->sc_tx_timer == 0) {
1035109323Ssam			if_printf(ifp, "device timeout\n");
1036109323Ssam			ifp->if_oerrors++;
1037109323Ssam			wi_init(ifp->if_softc);
1038109323Ssam			return;
1039109323Ssam		}
1040109323Ssam		ifp->if_timer = 1;
104146492Swpaul	}
104246492Swpaul
1043109323Ssam	if (sc->sc_scan_timer) {
1044109323Ssam		if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
1045109323Ssam		    sc->sc_firmware_type == WI_INTERSIL) {
1046109323Ssam			DPRINTF(("wi_watchdog: inquire scan\n"));
1047109323Ssam			wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
104846492Swpaul		}
1049109323Ssam		if (sc->sc_scan_timer)
1050109323Ssam			ifp->if_timer = 1;
105146492Swpaul	}
105246492Swpaul
1053109323Ssam	if (sc->sc_syn_timer) {
1054109323Ssam		if (--sc->sc_syn_timer == 0) {
1055117812Ssam			struct ieee80211com *ic = (struct ieee80211com *) ifp;
1056109323Ssam			DPRINTF2(("wi_watchdog: %d false syns\n",
1057109323Ssam			    sc->sc_false_syns));
1058109323Ssam			sc->sc_false_syns = 0;
1059117812Ssam			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1060109323Ssam			sc->sc_syn_timer = 5;
1061109323Ssam		}
1062109323Ssam		ifp->if_timer = 1;
106346492Swpaul	}
106446492Swpaul
1065109323Ssam	/* TODO: rate control */
1066109323Ssam	ieee80211_watchdog(ifp);
106746492Swpaul}
106846492Swpaul
106988546Salfredstatic int
1070109323Ssamwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
107146492Swpaul{
1072109323Ssam	struct wi_softc *sc = ifp->if_softc;
1073109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1074109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
1075109323Ssam	struct ieee80211req *ireq;
1076109323Ssam	u_int8_t nodename[IEEE80211_NWID_LEN];
1077109323Ssam	int error = 0;
107895534Simp#if __FreeBSD_version >= 500000
1079109323Ssam	struct thread *td = curthread;
108095534Simp#else
1081109323Ssam	struct proc *td = curproc;		/* Little white lie */
108295534Simp#endif
1083109323Ssam	struct wi_req wreq;
1084109323Ssam	WI_LOCK_DECL();
108546492Swpaul
1086109323Ssam	WI_LOCK(sc);
108746492Swpaul
108861818Sroberto	if (sc->wi_gone) {
108961818Sroberto		error = ENODEV;
109061818Sroberto		goto out;
109161818Sroberto	}
109246492Swpaul
1093109323Ssam	switch (cmd) {
109446492Swpaul	case SIOCSIFFLAGS:
1095100876Simp		/*
1096101139Simp		 * Can't do promisc and hostap at the same time.  If all that's
1097101139Simp		 * changing is the promisc flag, try to short-circuit a call to
1098101139Simp		 * wi_init() by just setting PROMISC in the hardware.
1099100876Simp		 */
110046492Swpaul		if (ifp->if_flags & IFF_UP) {
1101109323Ssam			if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1102101139Simp			    ifp->if_flags & IFF_RUNNING) {
1103101139Simp				if (ifp->if_flags & IFF_PROMISC &&
1104109323Ssam				    !(sc->sc_if_flags & IFF_PROMISC)) {
1105109323Ssam					wi_write_val(sc, WI_RID_PROMISC, 1);
1106101139Simp				} else if (!(ifp->if_flags & IFF_PROMISC) &&
1107109323Ssam				    sc->sc_if_flags & IFF_PROMISC) {
1108109323Ssam					wi_write_val(sc, WI_RID_PROMISC, 0);
1109101139Simp				} else {
1110101139Simp					wi_init(sc);
1111101139Simp				}
1112100876Simp			} else {
111346492Swpaul				wi_init(sc);
1114100876Simp			}
111546492Swpaul		} else {
111646492Swpaul			if (ifp->if_flags & IFF_RUNNING) {
1117123098Simp				wi_stop(ifp, 1);
111846492Swpaul			}
1119123098Simp			sc->wi_gone = 0;
112046492Swpaul		}
1121109323Ssam		sc->sc_if_flags = ifp->if_flags;
112246492Swpaul		error = 0;
112346492Swpaul		break;
112446492Swpaul	case SIOCADDMULTI:
112546492Swpaul	case SIOCDELMULTI:
1126109323Ssam		error = wi_write_multi(sc);
112746492Swpaul		break;
1128109323Ssam	case SIOCGIFGENERIC:
1129109323Ssam		error = wi_get_cfg(ifp, cmd, data);
113046492Swpaul		break;
1131109323Ssam	case SIOCSIFGENERIC:
1132109323Ssam		error = suser(td);
113346492Swpaul		if (error)
113446492Swpaul			break;
1135109323Ssam		error = wi_set_cfg(ifp, cmd, data);
113646492Swpaul		break;
113793359Simp	case SIOCGPRISM2DEBUG:
113893359Simp		error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
113993359Simp		if (error)
114093359Simp			break;
114193733Simp		if (!(ifp->if_flags & IFF_RUNNING) ||
114293733Simp		    sc->sc_firmware_type == WI_LUCENT) {
114393359Simp			error = EIO;
114493359Simp			break;
114593359Simp		}
114693359Simp		error = wi_get_debug(sc, &wreq);
114793359Simp		if (error == 0)
114893359Simp			error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
114993359Simp		break;
115093359Simp	case SIOCSPRISM2DEBUG:
115193593Sjhb		if ((error = suser(td)))
115293359Simp			goto out;
115393359Simp		error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
115493359Simp		if (error)
115593359Simp			break;
115693359Simp		error = wi_set_debug(sc, &wreq);
115793359Simp		break;
115877217Sphk	case SIOCG80211:
1159109323Ssam		ireq = (struct ieee80211req *) data;
1160109323Ssam		switch (ireq->i_type) {
116177217Sphk		case IEEE80211_IOC_STATIONNAME:
1162109323Ssam			ireq->i_len = sc->sc_nodelen + 1;
1163109323Ssam			error = copyout(sc->sc_nodename, ireq->i_data,
1164109323Ssam					ireq->i_len);
116577217Sphk			break;
1166109323Ssam		default:
1167109323Ssam			error = ieee80211_ioctl(ifp, cmd, data);
116877217Sphk			break;
116977217Sphk		}
117077217Sphk		break;
117177217Sphk	case SIOCS80211:
1172109323Ssam		error = suser(td);
1173109323Ssam		if (error)
1174109323Ssam			break;
1175109323Ssam		ireq = (struct ieee80211req *) data;
1176109323Ssam		switch (ireq->i_type) {
1177109323Ssam		case IEEE80211_IOC_STATIONNAME:
117877217Sphk			if (ireq->i_val != 0 ||
117977217Sphk			    ireq->i_len > IEEE80211_NWID_LEN) {
118077217Sphk				error = EINVAL;
118177217Sphk				break;
118277217Sphk			}
1183109323Ssam			memset(nodename, 0, IEEE80211_NWID_LEN);
1184109323Ssam			error = copyin(ireq->i_data, nodename, ireq->i_len);
1185109323Ssam			if (error)
118677217Sphk				break;
1187109323Ssam			if (sc->sc_enabled) {
1188109323Ssam				error = wi_write_ssid(sc, WI_RID_NODENAME,
1189109323Ssam					nodename, ireq->i_len);
1190109323Ssam				if (error)
1191109323Ssam					break;
119277217Sphk			}
1193109323Ssam			memcpy(sc->sc_nodename, nodename, IEEE80211_NWID_LEN);
1194109323Ssam			sc->sc_nodelen = ireq->i_len;
119577217Sphk			break;
119677217Sphk		default:
1197109323Ssam			error = ieee80211_ioctl(ifp, cmd, data);
119877217Sphk			break;
119977217Sphk		}
120077217Sphk		break;
120146492Swpaul	default:
1202109323Ssam		error = ieee80211_ioctl(ifp, cmd, data);
120346492Swpaul		break;
120446492Swpaul	}
1205109323Ssam	if (error == ENETRESET) {
1206109323Ssam		if (sc->sc_enabled)
1207116951Ssam			wi_init(sc);	/* XXX no error return */
1208109323Ssam		error = 0;
1209109323Ssam	}
121061818Srobertoout:
1211109323Ssam	WI_UNLOCK(sc);
121246492Swpaul
1213109323Ssam	return (error);
121446492Swpaul}
121546492Swpaul
1216109323Ssamstatic int
1217109323Ssamwi_media_change(struct ifnet *ifp)
121846492Swpaul{
1219109323Ssam	struct wi_softc *sc = ifp->if_softc;
1220116951Ssam	int error;
122146492Swpaul
1222116951Ssam	error = ieee80211_media_change(ifp);
1223109323Ssam	if (error == ENETRESET) {
1224109323Ssam		if (sc->sc_enabled)
1225116951Ssam			wi_init(sc);	/* XXX no error return */
1226109323Ssam		error = 0;
1227109323Ssam	}
1228109323Ssam	return error;
1229109323Ssam}
123046492Swpaul
1231109323Ssamstatic void
1232109323Ssamwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1233109323Ssam{
1234109323Ssam	struct wi_softc *sc = ifp->if_softc;
1235109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1236109323Ssam	u_int16_t val;
1237109323Ssam	int rate, len;
123846492Swpaul
1239109323Ssam	if (sc->wi_gone || !sc->sc_enabled) {
1240109323Ssam		imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
1241109323Ssam		imr->ifm_status = 0;
1242109323Ssam		return;
1243109323Ssam	}
124446492Swpaul
1245109323Ssam	imr->ifm_status = IFM_AVALID;
1246109323Ssam	imr->ifm_active = IFM_IEEE80211;
1247109323Ssam	if (ic->ic_state == IEEE80211_S_RUN &&
1248109323Ssam	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
1249109323Ssam		imr->ifm_status |= IFM_ACTIVE;
1250109323Ssam	len = sizeof(val);
1251109323Ssam	if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) != 0)
1252109323Ssam		rate = 0;
1253109323Ssam	else {
1254109323Ssam		/* convert to 802.11 rate */
1255109323Ssam		rate = val * 2;
1256109323Ssam		if (sc->sc_firmware_type == WI_LUCENT) {
1257112501Simp			if (rate == 4 * 2)
1258109323Ssam				rate = 11;	/* 5.5Mbps */
1259112501Simp			else if (rate == 5 * 2)
1260112501Simp				rate = 22;	/* 11Mbps */
1261109323Ssam		} else {
1262109323Ssam			if (rate == 4*2)
1263109323Ssam				rate = 11;	/* 5.5Mbps */
1264109323Ssam			else if (rate == 8*2)
1265109323Ssam				rate = 22;	/* 11Mbps */
1266109323Ssam		}
1267109323Ssam	}
1268116951Ssam	imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
1269109323Ssam	switch (ic->ic_opmode) {
1270109323Ssam	case IEEE80211_M_STA:
1271109323Ssam		break;
1272109323Ssam	case IEEE80211_M_IBSS:
1273109323Ssam		imr->ifm_active |= IFM_IEEE80211_ADHOC;
1274109323Ssam		break;
1275109323Ssam	case IEEE80211_M_AHDEMO:
1276109323Ssam		imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1277109323Ssam		break;
1278109323Ssam	case IEEE80211_M_HOSTAP:
1279109323Ssam		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1280109323Ssam		break;
1281117855Ssam	case IEEE80211_M_MONITOR:
1282117855Ssam		imr->ifm_active |= IFM_IEEE80211_MONITOR;
1283117855Ssam		break;
1284109323Ssam	}
1285109323Ssam}
128646492Swpaul
1287109323Ssamstatic void
1288109323Ssamwi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1289109323Ssam{
1290109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1291116951Ssam	struct ieee80211_node *ni = ic->ic_bss;
1292109323Ssam	struct ifnet *ifp = &ic->ic_if;
129398440Simp
1294109323Ssam	if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1295109323Ssam		return;
129646492Swpaul
1297109323Ssam	DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid)));
1298109323Ssam	DPRINTF(("%s ?\n", ether_sprintf(new_bssid)));
129946492Swpaul
1300109323Ssam	/* In promiscuous mode, the BSSID field is not a reliable
1301109323Ssam	 * indicator of the firmware's BSSID. Damp spurious
1302109323Ssam	 * change-of-BSSID indications.
1303109323Ssam	 */
1304109323Ssam	if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1305109323Ssam	    sc->sc_false_syns >= WI_MAX_FALSE_SYNS)
1306109323Ssam		return;
130746492Swpaul
1308117812Ssam	ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1309109323Ssam}
131046492Swpaul
1311109323Ssamstatic void
1312116898Ssamwi_rx_monitor(struct wi_softc *sc, int fid)
1313116898Ssam{
1314116898Ssam	struct ieee80211com *ic = &sc->sc_ic;
1315116898Ssam	struct ifnet *ifp = &ic->ic_if;
1316116898Ssam	struct wi_frame *rx_frame;
1317116898Ssam	struct mbuf *m;
1318116898Ssam	int datlen, hdrlen;
1319116898Ssam
1320116898Ssam	/* first allocate mbuf for packet storage */
1321116898Ssam	m = m_getcl(M_DONTWAIT, MT_DATA, 0);
1322116898Ssam	if (m == NULL) {
1323116898Ssam		ifp->if_ierrors++;
1324116898Ssam		return;
1325116898Ssam	}
1326116898Ssam
1327116898Ssam	m->m_pkthdr.rcvif = ifp;
1328116898Ssam
1329116898Ssam	/* now read wi_frame first so we know how much data to read */
1330116898Ssam	if (wi_read_bap(sc, fid, 0, mtod(m, caddr_t), sizeof(*rx_frame))) {
1331116898Ssam		ifp->if_ierrors++;
1332116898Ssam		goto done;
1333116898Ssam	}
1334116898Ssam
1335116898Ssam	rx_frame = mtod(m, struct wi_frame *);
1336116898Ssam
1337116898Ssam	switch ((rx_frame->wi_status & WI_STAT_MAC_PORT) >> 8) {
1338116898Ssam	case 7:
1339116898Ssam		switch (rx_frame->wi_whdr.i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1340116898Ssam		case IEEE80211_FC0_TYPE_DATA:
1341116898Ssam			hdrlen = WI_DATA_HDRLEN;
1342116898Ssam			datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1343116898Ssam			break;
1344116898Ssam		case IEEE80211_FC0_TYPE_MGT:
1345116898Ssam			hdrlen = WI_MGMT_HDRLEN;
1346116898Ssam			datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1347116898Ssam			break;
1348116898Ssam		case IEEE80211_FC0_TYPE_CTL:
1349116898Ssam			/*
1350116898Ssam			 * prism2 cards don't pass control packets
1351116898Ssam			 * down properly or consistently, so we'll only
1352116898Ssam			 * pass down the header.
1353116898Ssam			 */
1354116898Ssam			hdrlen = WI_CTL_HDRLEN;
1355116898Ssam			datlen = 0;
1356116898Ssam			break;
1357116898Ssam		default:
1358116898Ssam			if_printf(ifp, "received packet of unknown type "
1359116898Ssam				"on port 7\n");
1360116898Ssam			ifp->if_ierrors++;
1361116898Ssam			goto done;
1362116898Ssam		}
1363116898Ssam		break;
1364116898Ssam	case 0:
1365116898Ssam		hdrlen = WI_DATA_HDRLEN;
1366116898Ssam		datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1367116898Ssam		break;
1368116898Ssam	default:
1369116898Ssam		if_printf(ifp, "received packet on invalid "
1370116898Ssam		    "port (wi_status=0x%x)\n", rx_frame->wi_status);
1371116898Ssam		ifp->if_ierrors++;
1372116898Ssam		goto done;
1373116898Ssam	}
1374116898Ssam
1375116898Ssam	if (hdrlen + datlen + 2 > MCLBYTES) {
1376116898Ssam		if_printf(ifp, "oversized packet received "
1377116898Ssam		    "(wi_dat_len=%d, wi_status=0x%x)\n",
1378116898Ssam		    datlen, rx_frame->wi_status);
1379116898Ssam		ifp->if_ierrors++;
1380116898Ssam		goto done;
1381116898Ssam	}
1382116898Ssam
1383116898Ssam	if (wi_read_bap(sc, fid, hdrlen, mtod(m, caddr_t) + hdrlen,
1384116898Ssam	    datlen + 2) == 0) {
1385116898Ssam		m->m_pkthdr.len = m->m_len = hdrlen + datlen;
1386116898Ssam		ifp->if_ipackets++;
1387116898Ssam		BPF_MTAP(ifp, m);	/* Handle BPF listeners. */
1388116898Ssam	} else
1389116898Ssam		ifp->if_ierrors++;
1390116898Ssamdone:
1391116898Ssam	m_freem(m);
1392116898Ssam}
1393116898Ssam
1394116898Ssamstatic void
1395109323Ssamwi_rx_intr(struct wi_softc *sc)
1396109323Ssam{
1397109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1398109323Ssam	struct ifnet *ifp = &ic->ic_if;
1399109323Ssam	struct wi_frame frmhdr;
1400109323Ssam	struct mbuf *m;
1401109323Ssam	struct ieee80211_frame *wh;
1402119150Ssam	struct ieee80211_node *ni;
1403109323Ssam	int fid, len, off, rssi;
1404109323Ssam	u_int8_t dir;
1405109323Ssam	u_int16_t status;
1406109323Ssam	u_int32_t rstamp;
140746611Swpaul
1408109323Ssam	fid = CSR_READ_2(sc, WI_RX_FID);
140946611Swpaul
1410116898Ssam	if (sc->wi_debug.wi_monitor) {
1411116898Ssam		/*
1412116898Ssam		 * If we are in monitor mode just
1413116898Ssam		 * read the data from the device.
1414116898Ssam		 */
1415116898Ssam		wi_rx_monitor(sc, fid);
1416116898Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1417116898Ssam		return;
1418116898Ssam	}
1419116898Ssam
1420109323Ssam	/* First read in the frame header */
1421109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1422109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1423109323Ssam		ifp->if_ierrors++;
1424109323Ssam		DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1425109323Ssam		return;
1426109323Ssam	}
142791695Simp
1428109323Ssam	if (IFF_DUMPPKTS(ifp))
1429109323Ssam		wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal);
143046492Swpaul
1431109323Ssam	/*
1432109323Ssam	 * Drop undecryptable or packets with receive errors here
1433109323Ssam	 */
1434109323Ssam	status = le16toh(frmhdr.wi_status);
1435109323Ssam	if (status & WI_STAT_ERRSTAT) {
1436109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1437109323Ssam		ifp->if_ierrors++;
1438109323Ssam		DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1439109323Ssam		return;
1440109323Ssam	}
1441109323Ssam	rssi = frmhdr.wi_rx_signal;
1442109323Ssam	rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1443109323Ssam	    le16toh(frmhdr.wi_rx_tstamp1);
144446492Swpaul
1445109323Ssam	len = le16toh(frmhdr.wi_dat_len);
1446109323Ssam	off = ALIGN(sizeof(struct ieee80211_frame));
144746563Swpaul
1448117855Ssam	/*
1449117855Ssam	 * Sometimes the PRISM2.x returns bogusly large frames. Except
1450117855Ssam	 * in monitor mode, just throw them away.
1451117855Ssam	 */
1452117855Ssam	if (off + len > MCLBYTES) {
1453117855Ssam		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1454117855Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1455117855Ssam			ifp->if_ierrors++;
1456117855Ssam			DPRINTF(("wi_rx_intr: oversized packet\n"));
1457117855Ssam			return;
1458117855Ssam		} else
1459117855Ssam			len = 0;
1460117855Ssam	}
1461117855Ssam
1462111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
1463109323Ssam	if (m == NULL) {
1464109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1465109323Ssam		ifp->if_ierrors++;
1466109323Ssam		DPRINTF(("wi_rx_intr: MGET failed\n"));
1467109323Ssam		return;
1468109323Ssam	}
1469109323Ssam	if (off + len > MHLEN) {
1470111119Simp		MCLGET(m, M_DONTWAIT);
1471109323Ssam		if ((m->m_flags & M_EXT) == 0) {
1472109323Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1473109323Ssam			m_freem(m);
1474109323Ssam			ifp->if_ierrors++;
1475109323Ssam			DPRINTF(("wi_rx_intr: MCLGET failed\n"));
1476109323Ssam			return;
1477109323Ssam		}
1478109323Ssam	}
147946492Swpaul
1480109323Ssam	m->m_data += off - sizeof(struct ieee80211_frame);
1481109323Ssam	memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1482109323Ssam	wi_read_bap(sc, fid, sizeof(frmhdr),
1483109323Ssam	    m->m_data + sizeof(struct ieee80211_frame), len);
1484109323Ssam	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1485109323Ssam	m->m_pkthdr.rcvif = ifp;
148694405Simp
1487109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
148846492Swpaul
1489109323Ssam#if NBPFILTER > 0
1490109323Ssam	if (sc->sc_drvbpf) {
1491123927Ssam		/* XXX replace divide by table */
1492123922Ssam		sc->sc_rx_th.wr_rate = frmhdr.wi_rx_rate / 5;
1493123922Ssam		sc->sc_rx_th.wr_antsignal =
1494123922Ssam			WI_RSSI_TO_DBM(sc, frmhdr.wi_rx_signal);
1495123922Ssam		sc->sc_rx_th.wr_antnoise =
1496123922Ssam			WI_RSSI_TO_DBM(sc, frmhdr.wi_rx_silence);
1497123927Ssam		sc->sc_rx_th.wr_flags = 0;
1498123927Ssam		if (frmhdr.wi_status & WI_STAT_PCF)
1499123927Ssam			sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_CFP;
1500123922Ssam		bpf_mtap2(sc->sc_drvbpf,
1501123922Ssam			&sc->sc_rx_th, sizeof(sc->sc_rx_th), m);
150256965Swpaul	}
1503109323Ssam#endif
1504109323Ssam	wh = mtod(m, struct ieee80211_frame *);
1505109323Ssam	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1506109323Ssam		/*
1507109323Ssam		 * WEP is decrypted by hardware. Clear WEP bit
1508109323Ssam		 * header for ieee80211_input().
1509109323Ssam		 */
1510109323Ssam		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1511109323Ssam	}
151256965Swpaul
1513109323Ssam	/* synchronize driver's BSSID with firmware's BSSID */
1514109323Ssam	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1515109323Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1516109323Ssam		wi_sync_bssid(sc, wh->i_addr3);
151746492Swpaul
1518119150Ssam	/*
1519119150Ssam	 * Locate the node for sender, track state, and
1520119150Ssam	 * then pass this node (referenced) up to the 802.11
1521119150Ssam	 * layer for its use.  We are required to pass
1522119150Ssam	 * something so we fallback to ic_bss when this frame
1523119150Ssam	 * is from an unknown sender.
1524119150Ssam	 */
1525119150Ssam	if (ic->ic_opmode != IEEE80211_M_STA) {
1526119150Ssam		ni = ieee80211_find_node(ic, wh->i_addr2);
1527119150Ssam		if (ni == NULL)
1528119150Ssam			ni = ieee80211_ref_node(ic->ic_bss);
1529119150Ssam	} else
1530119150Ssam		ni = ieee80211_ref_node(ic->ic_bss);
1531119150Ssam	/*
1532119150Ssam	 * Send frame up for processing.
1533119150Ssam	 */
1534119150Ssam	ieee80211_input(ifp, m, ni, rssi, rstamp);
1535119150Ssam	/*
1536119150Ssam	 * The frame may have caused the node to be marked for
1537119150Ssam	 * reclamation (e.g. in response to a DEAUTH message)
1538119150Ssam	 * so use free_node here instead of unref_node.
1539119150Ssam	 */
1540119150Ssam	if (ni == ic->ic_bss)
1541119150Ssam		ieee80211_unref_node(&ni);
1542119150Ssam	else
1543119150Ssam		ieee80211_free_node(ic, ni);
1544109323Ssam}
154546492Swpaul
1546109323Ssamstatic void
1547109323Ssamwi_tx_ex_intr(struct wi_softc *sc)
1548109323Ssam{
1549109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1550109323Ssam	struct ifnet *ifp = &ic->ic_if;
1551109323Ssam	struct wi_frame frmhdr;
1552109323Ssam	int fid;
155346492Swpaul
1554109323Ssam	fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1555109323Ssam	/* Read in the frame header */
1556109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1557109323Ssam		u_int16_t status = le16toh(frmhdr.wi_status);
155846492Swpaul
1559109323Ssam		/*
1560109323Ssam		 * Spontaneous station disconnects appear as xmit
1561109323Ssam		 * errors.  Don't announce them and/or count them
1562109323Ssam		 * as an output error.
1563109323Ssam		 */
1564109323Ssam		if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1565109323Ssam			if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1566109323Ssam				if_printf(ifp, "tx failed");
1567109323Ssam				if (status & WI_TXSTAT_RET_ERR)
1568109323Ssam					printf(", retry limit exceeded");
1569109323Ssam				if (status & WI_TXSTAT_AGED_ERR)
1570109323Ssam					printf(", max transmit lifetime exceeded");
1571109323Ssam				if (status & WI_TXSTAT_DISCONNECT)
1572109323Ssam					printf(", port disconnected");
1573109323Ssam				if (status & WI_TXSTAT_FORM_ERR)
1574109323Ssam					printf(", invalid format (data len %u src %6D)",
1575109323Ssam						le16toh(frmhdr.wi_dat_len),
1576109323Ssam						frmhdr.wi_ehdr.ether_shost, ":");
1577109323Ssam				if (status & ~0xf)
1578109323Ssam					printf(", status=0x%x", status);
1579109323Ssam				printf("\n");
1580109323Ssam			}
1581109323Ssam			ifp->if_oerrors++;
1582109323Ssam		} else {
1583109323Ssam			DPRINTF(("port disconnected\n"));
1584109323Ssam			ifp->if_collisions++;	/* XXX */
1585109323Ssam		}
1586109323Ssam	} else
1587109323Ssam		DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
1588109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1589109323Ssam}
159046492Swpaul
1591109323Ssamstatic void
1592109323Ssamwi_tx_intr(struct wi_softc *sc)
1593109323Ssam{
1594109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1595109323Ssam	struct ifnet *ifp = &ic->ic_if;
1596109323Ssam	int fid, cur;
159794405Simp
1598123098Simp	if (sc->wi_gone)
1599123098Simp		return;
1600123098Simp
1601109323Ssam	fid = CSR_READ_2(sc, WI_ALLOC_FID);
1602109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
160346492Swpaul
1604109323Ssam	cur = sc->sc_txcur;
1605109323Ssam	if (sc->sc_txd[cur].d_fid != fid) {
1606109323Ssam		if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
1607109323Ssam		    fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
1608109323Ssam		return;
1609109323Ssam	}
1610109323Ssam	sc->sc_tx_timer = 0;
1611109323Ssam	sc->sc_txd[cur].d_len = 0;
1612112363Simp	sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
1613109323Ssam	if (sc->sc_txd[cur].d_len == 0)
1614109323Ssam		ifp->if_flags &= ~IFF_OACTIVE;
1615109323Ssam	else {
1616109323Ssam		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1617109323Ssam		    0, 0)) {
1618109323Ssam			if_printf(ifp, "xmit failed\n");
1619109323Ssam			sc->sc_txd[cur].d_len = 0;
1620109323Ssam		} else {
1621109323Ssam			sc->sc_tx_timer = 5;
1622109323Ssam			ifp->if_timer = 1;
1623109323Ssam		}
1624109323Ssam	}
162546492Swpaul}
162646492Swpaul
162788546Salfredstatic void
1628109323Ssamwi_info_intr(struct wi_softc *sc)
162994405Simp{
1630109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1631109323Ssam	struct ifnet *ifp = &ic->ic_if;
1632109323Ssam	int i, fid, len, off;
1633109323Ssam	u_int16_t ltbuf[2];
1634109323Ssam	u_int16_t stat;
1635109323Ssam	u_int32_t *ptr;
163694405Simp
1637109323Ssam	fid = CSR_READ_2(sc, WI_INFO_FID);
1638109323Ssam	wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
163994405Simp
1640109323Ssam	switch (le16toh(ltbuf[1])) {
164194405Simp
1642109323Ssam	case WI_INFO_LINK_STAT:
1643109323Ssam		wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1644109323Ssam		DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1645109323Ssam		switch (le16toh(stat)) {
1646109323Ssam		case WI_INFO_LINK_STAT_CONNECTED:
1647109323Ssam			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1648109323Ssam			if (ic->ic_state == IEEE80211_S_RUN &&
1649109323Ssam			    ic->ic_opmode != IEEE80211_M_IBSS)
1650109323Ssam				break;
1651109323Ssam			/* FALLTHROUGH */
1652109323Ssam		case WI_INFO_LINK_STAT_AP_CHG:
1653117812Ssam			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1654109323Ssam			break;
1655109323Ssam		case WI_INFO_LINK_STAT_AP_INR:
1656109323Ssam			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1657109323Ssam			break;
1658109323Ssam		case WI_INFO_LINK_STAT_AP_OOR:
1659109323Ssam			if (sc->sc_firmware_type == WI_SYMBOL &&
1660109323Ssam			    sc->sc_scan_timer > 0) {
1661109323Ssam				if (wi_cmd(sc, WI_CMD_INQUIRE,
1662109323Ssam				    WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
1663109323Ssam					sc->sc_scan_timer = 0;
1664109323Ssam				break;
1665109323Ssam			}
1666109323Ssam			if (ic->ic_opmode == IEEE80211_M_STA)
1667109323Ssam				sc->sc_flags |= WI_FLAGS_OUTRANGE;
1668109323Ssam			break;
1669109323Ssam		case WI_INFO_LINK_STAT_DISCONNECTED:
1670109323Ssam		case WI_INFO_LINK_STAT_ASSOC_FAILED:
1671109323Ssam			if (ic->ic_opmode == IEEE80211_M_STA)
1672117812Ssam				ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1673109323Ssam			break;
1674109323Ssam		}
1675109323Ssam		break;
167694405Simp
1677109323Ssam	case WI_INFO_COUNTERS:
1678109323Ssam		/* some card versions have a larger stats structure */
1679109323Ssam		len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1680109323Ssam		ptr = (u_int32_t *)&sc->sc_stats;
1681109323Ssam		off = sizeof(ltbuf);
1682109323Ssam		for (i = 0; i < len; i++, off += 2, ptr++) {
1683109323Ssam			wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1684109323Ssam#ifdef WI_HERMES_STATS_WAR
1685109323Ssam			if (stat & 0xf000)
1686109323Ssam				stat = ~stat;
1687109323Ssam#endif
1688109323Ssam			*ptr += stat;
1689109323Ssam		}
1690109323Ssam		ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1691109323Ssam		    sc->sc_stats.wi_tx_multi_retries +
1692109323Ssam		    sc->sc_stats.wi_tx_retry_limit;
1693109323Ssam		break;
1694109323Ssam
1695109323Ssam	case WI_INFO_SCAN_RESULTS:
1696109323Ssam	case WI_INFO_HOST_SCAN_RESULTS:
1697109323Ssam		wi_scan_result(sc, fid, le16toh(ltbuf[0]));
1698109323Ssam		break;
1699109323Ssam
1700109323Ssam	default:
1701109323Ssam		DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1702109323Ssam		    le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1703109323Ssam		break;
170494405Simp	}
1705109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1706109323Ssam}
170794405Simp
1708109323Ssamstatic int
1709109323Ssamwi_write_multi(struct wi_softc *sc)
1710109323Ssam{
1711109323Ssam	struct ifnet *ifp = &sc->sc_ic.ic_if;
1712109323Ssam	int n;
1713109323Ssam	struct ifmultiaddr *ifma;
1714109323Ssam	struct wi_mcast mlist;
171594472Simp
1716109323Ssam	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1717109323Ssamallmulti:
1718109323Ssam		memset(&mlist, 0, sizeof(mlist));
1719109323Ssam		return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1720109323Ssam		    sizeof(mlist));
172194405Simp	}
172294405Simp
1723109323Ssam	n = 0;
1724109323Ssam#if __FreeBSD_version < 500000
1725109323Ssam	LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1726109323Ssam#else
1727109323Ssam	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1728109323Ssam#endif
1729109323Ssam		if (ifma->ifma_addr->sa_family != AF_LINK)
1730109323Ssam			continue;
1731109323Ssam		if (n >= 16)
1732109323Ssam			goto allmulti;
1733109323Ssam		IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1734109323Ssam		    (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1735109323Ssam		n++;
173694405Simp	}
1737109323Ssam	return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1738109323Ssam	    IEEE80211_ADDR_LEN * n);
173994405Simp}
174094405Simp
174194405Simpstatic void
1742109323Ssamwi_read_nicid(struct wi_softc *sc)
174346492Swpaul{
1744109323Ssam	struct wi_card_ident *id;
1745109323Ssam	char *p;
1746109323Ssam	int len;
1747109323Ssam	u_int16_t ver[4];
174846492Swpaul
1749109323Ssam	/* getting chip identity */
1750109323Ssam	memset(ver, 0, sizeof(ver));
1751109323Ssam	len = sizeof(ver);
1752109323Ssam	wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1753109323Ssam	device_printf(sc->sc_dev, "using ");
175446492Swpaul
1755109323Ssam	sc->sc_firmware_type = WI_NOTYPE;
1756109323Ssam	for (id = wi_card_ident; id->card_name != NULL; id++) {
1757109323Ssam		if (le16toh(ver[0]) == id->card_id) {
1758109323Ssam			printf("%s", id->card_name);
1759109323Ssam			sc->sc_firmware_type = id->firm_type;
1760109323Ssam			break;
1761109323Ssam		}
176267092Swpaul	}
1763109323Ssam	if (sc->sc_firmware_type == WI_NOTYPE) {
1764109323Ssam		if (le16toh(ver[0]) & 0x8000) {
1765109323Ssam			printf("Unknown PRISM2 chip");
1766109323Ssam			sc->sc_firmware_type = WI_INTERSIL;
1767109323Ssam		} else {
1768109323Ssam			printf("Unknown Lucent chip");
1769109323Ssam			sc->sc_firmware_type = WI_LUCENT;
1770109323Ssam		}
177167092Swpaul	}
177246492Swpaul
1773109323Ssam	/* get primary firmware version (Only Prism chips) */
1774109323Ssam	if (sc->sc_firmware_type != WI_LUCENT) {
1775109323Ssam		memset(ver, 0, sizeof(ver));
1776109323Ssam		len = sizeof(ver);
1777109323Ssam		wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1778109323Ssam		sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1779109323Ssam		    le16toh(ver[3]) * 100 + le16toh(ver[1]);
178067092Swpaul	}
178146492Swpaul
1782109323Ssam	/* get station firmware version */
1783109323Ssam	memset(ver, 0, sizeof(ver));
1784109323Ssam	len = sizeof(ver);
1785109323Ssam	wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1786109323Ssam	sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1787109323Ssam	    le16toh(ver[3]) * 100 + le16toh(ver[1]);
1788109323Ssam	if (sc->sc_firmware_type == WI_INTERSIL &&
1789109323Ssam	    (sc->sc_sta_firmware_ver == 10102 ||
1790109323Ssam	     sc->sc_sta_firmware_ver == 20102)) {
1791109323Ssam		char ident[12];
1792109323Ssam		memset(ident, 0, sizeof(ident));
1793109323Ssam		len = sizeof(ident);
1794109323Ssam		/* value should be the format like "V2.00-11" */
1795109323Ssam		if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1796109323Ssam		    *(p = (char *)ident) >= 'A' &&
1797109323Ssam		    p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1798109323Ssam			sc->sc_firmware_type = WI_SYMBOL;
1799109323Ssam			sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1800109323Ssam			    (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1801109323Ssam			    (p[6] - '0') * 10 + (p[7] - '0');
180294405Simp		}
180394405Simp	}
1804109323Ssam	printf("\n");
1805109323Ssam	device_printf(sc->sc_dev, "%s Firmware: ",
1806109323Ssam	     sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
1807109323Ssam	    (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
1808109323Ssam	if (sc->sc_firmware_type != WI_LUCENT)	/* XXX */
1809109323Ssam		printf("Primary (%u.%u.%u), ",
1810109323Ssam		    sc->sc_pri_firmware_ver / 10000,
1811109323Ssam		    (sc->sc_pri_firmware_ver % 10000) / 100,
1812109323Ssam		    sc->sc_pri_firmware_ver % 100);
1813109323Ssam	printf("Station (%u.%u.%u)\n",
1814109323Ssam	    sc->sc_sta_firmware_ver / 10000,
1815109323Ssam	    (sc->sc_sta_firmware_ver % 10000) / 100,
1816109323Ssam	    sc->sc_sta_firmware_ver % 100);
1817109323Ssam}
181846492Swpaul
1819109323Ssamstatic int
1820109323Ssamwi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1821109323Ssam{
1822109323Ssam	struct wi_ssid ssid;
182346492Swpaul
1824109323Ssam	if (buflen > IEEE80211_NWID_LEN)
1825109323Ssam		return ENOBUFS;
1826109323Ssam	memset(&ssid, 0, sizeof(ssid));
1827109323Ssam	ssid.wi_len = htole16(buflen);
1828109323Ssam	memcpy(ssid.wi_ssid, buf, buflen);
1829109323Ssam	return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1830109323Ssam}
183146492Swpaul
1832109323Ssamstatic int
1833109323Ssamwi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
1834109323Ssam{
1835109323Ssam	struct wi_softc *sc = ifp->if_softc;
1836109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1837109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
1838109323Ssam	struct wi_req wreq;
1839116898Ssam	struct wi_scan_res *res;
1840122015Sgreen	size_t reslen;
1841116898Ssam	int len, n, error, mif, val, off, i;
184246492Swpaul
1843109323Ssam	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1844109323Ssam	if (error)
1845109323Ssam		return error;
1846109323Ssam	len = (wreq.wi_len - 1) * 2;
1847109323Ssam	if (len < sizeof(u_int16_t))
1848109323Ssam		return ENOSPC;
1849109323Ssam	if (len > sizeof(wreq.wi_val))
1850109323Ssam		len = sizeof(wreq.wi_val);
185146492Swpaul
1852109323Ssam	switch (wreq.wi_type) {
185346492Swpaul
1854109323Ssam	case WI_RID_IFACE_STATS:
1855109323Ssam		memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
1856109323Ssam		if (len < sizeof(sc->sc_stats))
1857109323Ssam			error = ENOSPC;
1858109323Ssam		else
1859109323Ssam			len = sizeof(sc->sc_stats);
1860109323Ssam		break;
186146492Swpaul
1862109323Ssam	case WI_RID_ENCRYPTION:
1863109323Ssam	case WI_RID_TX_CRYPT_KEY:
1864109323Ssam	case WI_RID_DEFLT_CRYPT_KEYS:
1865109323Ssam	case WI_RID_TX_RATE:
1866109323Ssam		return ieee80211_cfgget(ifp, cmd, data);
186746492Swpaul
1868109323Ssam	case WI_RID_MICROWAVE_OVEN:
1869109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
1870109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1871109323Ssam			    &len);
1872109323Ssam			break;
1873109323Ssam		}
1874109323Ssam		wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
1875109323Ssam		len = sizeof(u_int16_t);
1876109323Ssam		break;
187746492Swpaul
1878109323Ssam	case WI_RID_DBM_ADJUST:
1879109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
1880109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1881109323Ssam			    &len);
1882109323Ssam			break;
1883109323Ssam		}
1884119784Ssam		wreq.wi_val[0] = htole16(sc->sc_dbm_offset);
1885109323Ssam		len = sizeof(u_int16_t);
1886109323Ssam		break;
188746492Swpaul
1888109323Ssam	case WI_RID_ROAMING_MODE:
1889109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
1890109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1891109323Ssam			    &len);
1892109323Ssam			break;
1893109323Ssam		}
1894109323Ssam		wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
1895109323Ssam		len = sizeof(u_int16_t);
1896109323Ssam		break;
189746492Swpaul
1898109323Ssam	case WI_RID_SYSTEM_SCALE:
1899109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
1900109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1901109323Ssam			    &len);
1902109323Ssam			break;
1903109323Ssam		}
1904109323Ssam		wreq.wi_val[0] = htole16(sc->sc_system_scale);
1905109323Ssam		len = sizeof(u_int16_t);
1906109323Ssam		break;
190746492Swpaul
1908109323Ssam	case WI_RID_FRAG_THRESH:
1909109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
1910109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1911109323Ssam			    &len);
1912109323Ssam			break;
1913109323Ssam		}
1914116951Ssam		wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
1915109323Ssam		len = sizeof(u_int16_t);
1916109323Ssam		break;
191746492Swpaul
1918109323Ssam	case WI_RID_READ_APS:
1919109323Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1920109323Ssam			return ieee80211_cfgget(ifp, cmd, data);
1921109323Ssam		if (sc->sc_scan_timer > 0) {
1922109323Ssam			error = EINPROGRESS;
1923109323Ssam			break;
1924109323Ssam		}
1925109323Ssam		n = sc->sc_naps;
1926109323Ssam		if (len < sizeof(n)) {
1927109323Ssam			error = ENOSPC;
1928109323Ssam			break;
1929109323Ssam		}
1930109323Ssam		if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
1931109323Ssam			n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
1932109323Ssam		len = sizeof(n) + sizeof(struct wi_apinfo) * n;
1933109323Ssam		memcpy(wreq.wi_val, &n, sizeof(n));
1934109323Ssam		memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
1935109323Ssam		    sizeof(struct wi_apinfo) * n);
1936109323Ssam		break;
193746492Swpaul
1938109323Ssam	case WI_RID_PRISM2:
1939109323Ssam		wreq.wi_val[0] = sc->sc_firmware_type != WI_LUCENT;
1940109323Ssam		len = sizeof(u_int16_t);
1941109323Ssam		break;
194246492Swpaul
1943109323Ssam	case WI_RID_MIF:
1944109323Ssam		mif = wreq.wi_val[0];
1945109323Ssam		error = wi_cmd(sc, WI_CMD_READMIF, mif, 0, 0);
1946109323Ssam		val = CSR_READ_2(sc, WI_RESP0);
1947109323Ssam		wreq.wi_val[0] = val;
1948109323Ssam		len = sizeof(u_int16_t);
1949109323Ssam		break;
195046492Swpaul
1951109323Ssam	case WI_RID_ZERO_CACHE:
1952109323Ssam	case WI_RID_PROCFRAME:		/* ignore for compatibility */
1953109323Ssam		/* XXX ??? */
1954109323Ssam		break;
195546492Swpaul
1956109323Ssam	case WI_RID_READ_CACHE:
1957109323Ssam		return ieee80211_cfgget(ifp, cmd, data);
195846492Swpaul
1959116898Ssam	case WI_RID_SCAN_RES:		/* compatibility interface */
1960116898Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1961116898Ssam			return ieee80211_cfgget(ifp, cmd, data);
1962116898Ssam		if (sc->sc_scan_timer > 0) {
1963116898Ssam			error = EINPROGRESS;
1964116898Ssam			break;
1965116898Ssam		}
1966116898Ssam		n = sc->sc_naps;
1967122015Sgreen		if (sc->sc_firmware_type == WI_LUCENT) {
1968122015Sgreen			off = 0;
1969122015Sgreen			reslen = WI_WAVELAN_RES_SIZE;
1970122015Sgreen		} else {
1971122015Sgreen			off = sizeof(struct wi_scan_p2_hdr);
1972122015Sgreen			reslen = WI_PRISM2_RES_SIZE;
1973122015Sgreen		}
1974122015Sgreen		if (len < off + reslen * n)
1975122015Sgreen			n = (len - off) / reslen;
1976122015Sgreen		len = off + reslen * n;
1977116898Ssam		if (off != 0) {
1978116898Ssam			struct wi_scan_p2_hdr *p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1979116898Ssam			/*
1980116898Ssam			 * Prepend Prism-specific header.
1981116898Ssam			 */
1982116898Ssam			if (len < sizeof(struct wi_scan_p2_hdr)) {
1983116898Ssam				error = ENOSPC;
1984116898Ssam				break;
1985116898Ssam			}
1986116898Ssam			p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1987116898Ssam			p2->wi_rsvd = 0;
1988116898Ssam			p2->wi_reason = n;	/* XXX */
1989116898Ssam		}
1990122015Sgreen		for (i = 0; i < n; i++, off += reslen) {
1991116898Ssam			const struct wi_apinfo *ap = &sc->sc_aps[i];
1992116898Ssam
1993116898Ssam			res = (struct wi_scan_res *)((char *)wreq.wi_val + off);
1994116898Ssam			res->wi_chan = ap->channel;
1995116898Ssam			res->wi_noise = ap->noise;
1996116898Ssam			res->wi_signal = ap->signal;
1997116898Ssam			IEEE80211_ADDR_COPY(res->wi_bssid, ap->bssid);
1998116898Ssam			res->wi_interval = ap->interval;
1999116898Ssam			res->wi_capinfo = ap->capinfo;
2000116898Ssam			res->wi_ssid_len = ap->namelen;
2001116898Ssam			memcpy(res->wi_ssid, ap->name,
2002116898Ssam				IEEE80211_NWID_LEN);
2003116898Ssam			if (sc->sc_firmware_type != WI_LUCENT) {
2004116898Ssam				/* XXX not saved from Prism cards */
2005116898Ssam				memset(res->wi_srates, 0,
2006116898Ssam					sizeof(res->wi_srates));
2007116898Ssam				res->wi_rate = ap->rate;
2008116898Ssam				res->wi_rsvd = 0;
2009122015Sgreen			}
2010116898Ssam		}
2011116898Ssam		break;
2012116898Ssam
2013109323Ssam	default:
2014109323Ssam		if (sc->sc_enabled) {
2015109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2016109323Ssam			    &len);
2017109323Ssam			break;
2018109323Ssam		}
2019109323Ssam		switch (wreq.wi_type) {
2020109323Ssam		case WI_RID_MAX_DATALEN:
2021109323Ssam			wreq.wi_val[0] = htole16(sc->sc_max_datalen);
2022109323Ssam			len = sizeof(u_int16_t);
2023109323Ssam			break;
2024109323Ssam		case WI_RID_RTS_THRESH:
2025116951Ssam			wreq.wi_val[0] = htole16(ic->ic_rtsthreshold);
2026109323Ssam			len = sizeof(u_int16_t);
2027109323Ssam			break;
2028109323Ssam		case WI_RID_CNFAUTHMODE:
2029109323Ssam			wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
2030109323Ssam			len = sizeof(u_int16_t);
2031109323Ssam			break;
2032109323Ssam		case WI_RID_NODENAME:
2033109323Ssam			if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
2034109323Ssam				error = ENOSPC;
2035109323Ssam				break;
2036109323Ssam			}
2037109323Ssam			len = sc->sc_nodelen + sizeof(u_int16_t);
2038109323Ssam			wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
2039109323Ssam			memcpy(&wreq.wi_val[1], sc->sc_nodename,
2040109323Ssam			    sc->sc_nodelen);
2041109323Ssam			break;
2042109323Ssam		default:
2043109323Ssam			return ieee80211_cfgget(ifp, cmd, data);
2044109323Ssam		}
2045109323Ssam		break;
204646492Swpaul	}
2047109323Ssam	if (error)
2048109323Ssam		return error;
2049109323Ssam	wreq.wi_len = (len + 1) / 2 + 1;
2050109323Ssam	return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
205146492Swpaul}
205246492Swpaul
2053109323Ssamstatic int
2054109323Ssamwi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
205546492Swpaul{
2056109323Ssam	struct wi_softc *sc = ifp->if_softc;
2057109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2058109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
2059109323Ssam	struct wi_req wreq;
2060109323Ssam	struct mbuf *m;
2061109323Ssam	int i, len, error, mif, val;
2062116951Ssam	struct ieee80211_rateset *rs;
206346492Swpaul
2064109323Ssam	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2065109323Ssam	if (error)
2066109323Ssam		return error;
2067116898Ssam	len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0;
2068109323Ssam	switch (wreq.wi_type) {
2069109323Ssam	case WI_RID_DBM_ADJUST:
2070109323Ssam		return ENODEV;
207167092Swpaul
2072109323Ssam	case WI_RID_NODENAME:
2073109323Ssam		if (le16toh(wreq.wi_val[0]) * 2 > len ||
2074109323Ssam		    le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
2075109323Ssam			error = ENOSPC;
2076109323Ssam			break;
2077109323Ssam		}
2078109323Ssam		if (sc->sc_enabled) {
2079109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2080109323Ssam			    len);
2081109323Ssam			if (error)
2082109323Ssam				break;
2083109323Ssam		}
2084109323Ssam		sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
2085109323Ssam		memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen);
2086109323Ssam		break;
208746492Swpaul
2088109323Ssam	case WI_RID_MICROWAVE_OVEN:
2089109323Ssam	case WI_RID_ROAMING_MODE:
2090109323Ssam	case WI_RID_SYSTEM_SCALE:
2091109323Ssam	case WI_RID_FRAG_THRESH:
2092109323Ssam		if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
2093109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
2094109323Ssam			break;
2095109323Ssam		if (wreq.wi_type == WI_RID_ROAMING_MODE &&
2096109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
2097109323Ssam			break;
2098109323Ssam		if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
2099109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
2100109323Ssam			break;
2101109323Ssam		if (wreq.wi_type == WI_RID_FRAG_THRESH &&
2102109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
2103109323Ssam			break;
2104109323Ssam		/* FALLTHROUGH */
2105109323Ssam	case WI_RID_RTS_THRESH:
2106109323Ssam	case WI_RID_CNFAUTHMODE:
2107109323Ssam	case WI_RID_MAX_DATALEN:
2108109323Ssam		if (sc->sc_enabled) {
2109109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2110109323Ssam			    sizeof(u_int16_t));
2111109323Ssam			if (error)
2112109323Ssam				break;
2113109323Ssam		}
2114109323Ssam		switch (wreq.wi_type) {
2115109323Ssam		case WI_RID_FRAG_THRESH:
2116116951Ssam			ic->ic_fragthreshold = le16toh(wreq.wi_val[0]);
2117109323Ssam			break;
2118109323Ssam		case WI_RID_RTS_THRESH:
2119116951Ssam			ic->ic_rtsthreshold = le16toh(wreq.wi_val[0]);
2120109323Ssam			break;
2121109323Ssam		case WI_RID_MICROWAVE_OVEN:
2122109323Ssam			sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
2123109323Ssam			break;
2124109323Ssam		case WI_RID_ROAMING_MODE:
2125109323Ssam			sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
2126109323Ssam			break;
2127109323Ssam		case WI_RID_SYSTEM_SCALE:
2128109323Ssam			sc->sc_system_scale = le16toh(wreq.wi_val[0]);
2129109323Ssam			break;
2130109323Ssam		case WI_RID_CNFAUTHMODE:
2131109323Ssam			sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
2132109323Ssam			break;
2133109323Ssam		case WI_RID_MAX_DATALEN:
2134109323Ssam			sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
2135109323Ssam			break;
2136109323Ssam		}
2137109323Ssam		break;
213894405Simp
2139109323Ssam	case WI_RID_TX_RATE:
2140109323Ssam		switch (le16toh(wreq.wi_val[0])) {
2141109323Ssam		case 3:
2142109323Ssam			ic->ic_fixed_rate = -1;
2143109323Ssam			break;
2144109323Ssam		default:
2145116951Ssam			rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
2146116951Ssam			for (i = 0; i < rs->rs_nrates; i++) {
2147116951Ssam				if ((rs->rs_rates[i] & IEEE80211_RATE_VAL)
2148109323Ssam				    / 2 == le16toh(wreq.wi_val[0]))
2149109323Ssam					break;
2150109323Ssam			}
2151116951Ssam			if (i == rs->rs_nrates)
2152109323Ssam				return EINVAL;
2153109323Ssam			ic->ic_fixed_rate = i;
2154109323Ssam		}
2155109323Ssam		if (sc->sc_enabled)
2156109323Ssam			error = wi_write_txrate(sc);
2157109323Ssam		break;
215846492Swpaul
2159109323Ssam	case WI_RID_SCAN_APS:
2160109323Ssam		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2161116898Ssam			error = wi_scan_ap(sc, 0x3fff, 0x000f);
2162109323Ssam		break;
216346492Swpaul
2164116898Ssam	case WI_RID_SCAN_REQ:		/* compatibility interface */
2165116898Ssam		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2166116898Ssam			error = wi_scan_ap(sc, wreq.wi_val[0], wreq.wi_val[1]);
2167116898Ssam		break;
2168116898Ssam
2169109323Ssam	case WI_RID_MGMT_XMIT:
2170109323Ssam		if (!sc->sc_enabled) {
2171109323Ssam			error = ENETDOWN;
2172109323Ssam			break;
2173109323Ssam		}
2174109323Ssam		if (ic->ic_mgtq.ifq_len > 5) {
2175109323Ssam			error = EAGAIN;
2176109323Ssam			break;
2177109323Ssam		}
2178109323Ssam		/* XXX wi_len looks in u_int8_t, not in u_int16_t */
2179109323Ssam		m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL);
2180109323Ssam		if (m == NULL) {
2181109323Ssam			error = ENOMEM;
2182109323Ssam			break;
2183109323Ssam		}
2184109323Ssam		IF_ENQUEUE(&ic->ic_mgtq, m);
2185109323Ssam		break;
218646492Swpaul
2187109323Ssam	case WI_RID_MIF:
2188109323Ssam		mif = wreq.wi_val[0];
2189109323Ssam		val = wreq.wi_val[1];
2190109323Ssam		error = wi_cmd(sc, WI_CMD_WRITEMIF, mif, val, 0);
2191109323Ssam		break;
219246492Swpaul
2193109323Ssam	case WI_RID_PROCFRAME:		/* ignore for compatibility */
2194109323Ssam		break;
2195109323Ssam
2196116898Ssam	case WI_RID_OWN_SSID:
2197116898Ssam		if (le16toh(wreq.wi_val[0]) * 2 > len ||
2198116898Ssam		    le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) {
2199116898Ssam			error = ENOSPC;
2200116898Ssam			break;
2201116898Ssam		}
2202116898Ssam		memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
2203116898Ssam		ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2;
2204116898Ssam		memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen);
2205116898Ssam		error = ENETRESET;
2206116898Ssam		break;
2207116898Ssam
2208109323Ssam	default:
2209109323Ssam		if (sc->sc_enabled) {
2210109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2211109323Ssam			    len);
2212109323Ssam			if (error)
2213109323Ssam				break;
2214109323Ssam		}
2215109323Ssam		error = ieee80211_cfgset(ifp, cmd, data);
2216109323Ssam		break;
2217109323Ssam	}
2218109323Ssam	return error;
221946492Swpaul}
222046492Swpaul
2221109323Ssamstatic int
2222109323Ssamwi_write_txrate(struct wi_softc *sc)
222346492Swpaul{
2224109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2225109323Ssam	int i;
2226109323Ssam	u_int16_t rate;
222746492Swpaul
2228109323Ssam	if (ic->ic_fixed_rate < 0)
2229109323Ssam		rate = 0;	/* auto */
2230109323Ssam	else
2231116951Ssam		rate = (ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ic->ic_fixed_rate] &
2232109323Ssam		    IEEE80211_RATE_VAL) / 2;
223346492Swpaul
2234109323Ssam	/* rate: 0, 1, 2, 5, 11 */
223546492Swpaul
2236109323Ssam	switch (sc->sc_firmware_type) {
2237109323Ssam	case WI_LUCENT:
2238112501Simp		switch (rate) {
2239112501Simp		case 0:			/* auto == 11mbps auto */
2240112501Simp			rate = 3;
2241112501Simp			break;
2242112501Simp		/* case 1, 2 map to 1, 2*/
2243112501Simp		case 5:			/* 5.5Mbps -> 4 */
2244112501Simp			rate = 4;
2245112501Simp			break;
2246112501Simp		case 11:		/* 11mbps -> 5 */
2247112501Simp			rate = 5;
2248112501Simp			break;
2249112501Simp		default:
2250112501Simp			break;
2251112501Simp		}
2252109323Ssam		break;
2253109323Ssam	default:
2254109323Ssam		/* Choose a bit according to this table.
2255109323Ssam		 *
2256109323Ssam		 * bit | data rate
2257109323Ssam		 * ----+-------------------
2258109323Ssam		 * 0   | 1Mbps
2259109323Ssam		 * 1   | 2Mbps
2260109323Ssam		 * 2   | 5.5Mbps
2261109323Ssam		 * 3   | 11Mbps
2262109323Ssam		 */
2263109323Ssam		for (i = 8; i > 0; i >>= 1) {
2264109323Ssam			if (rate >= i)
2265109323Ssam				break;
2266109323Ssam		}
2267109323Ssam		if (i == 0)
2268109323Ssam			rate = 0xf;	/* auto */
2269109323Ssam		else
2270109323Ssam			rate = i;
2271109323Ssam		break;
2272109323Ssam	}
2273109323Ssam	return wi_write_val(sc, WI_RID_TX_RATE, rate);
2274109323Ssam}
227546492Swpaul
2276109323Ssamstatic int
2277109323Ssamwi_write_wep(struct wi_softc *sc)
2278109323Ssam{
2279109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2280109323Ssam	int error = 0;
2281109323Ssam	int i, keylen;
2282109323Ssam	u_int16_t val;
2283109323Ssam	struct wi_key wkey[IEEE80211_WEP_NKID];
228446492Swpaul
2285109323Ssam	switch (sc->sc_firmware_type) {
2286109323Ssam	case WI_LUCENT:
2287109323Ssam		val = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0;
2288109323Ssam		error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
2289109323Ssam		if (error)
2290109323Ssam			break;
2291109323Ssam		error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_wep_txkey);
2292109323Ssam		if (error)
2293109323Ssam			break;
2294109323Ssam		memset(wkey, 0, sizeof(wkey));
2295109323Ssam		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2296109323Ssam			keylen = ic->ic_nw_keys[i].wk_len;
2297109323Ssam			wkey[i].wi_keylen = htole16(keylen);
2298109323Ssam			memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
2299109323Ssam			    keylen);
2300109323Ssam		}
2301109323Ssam		error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
2302109323Ssam		    wkey, sizeof(wkey));
2303109323Ssam		break;
2304109323Ssam
2305109323Ssam	case WI_INTERSIL:
2306109323Ssam	case WI_SYMBOL:
2307109323Ssam		if (ic->ic_flags & IEEE80211_F_WEPON) {
2308109323Ssam			/*
2309109323Ssam			 * ONLY HWB3163 EVAL-CARD Firmware version
2310109323Ssam			 * less than 0.8 variant2
2311109323Ssam			 *
2312109323Ssam			 *   If promiscuous mode disable, Prism2 chip
2313109323Ssam			 *  does not work with WEP .
2314109323Ssam			 * It is under investigation for details.
2315109323Ssam			 * (ichiro@netbsd.org)
2316109323Ssam			 */
2317109323Ssam			if (sc->sc_firmware_type == WI_INTERSIL &&
2318109323Ssam			    sc->sc_sta_firmware_ver < 802 ) {
2319109323Ssam				/* firm ver < 0.8 variant 2 */
2320109323Ssam				wi_write_val(sc, WI_RID_PROMISC, 1);
2321109323Ssam			}
2322109323Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE,
2323109323Ssam			    sc->sc_cnfauthmode);
2324109323Ssam			val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
2325109323Ssam			/*
2326109323Ssam			 * Encryption firmware has a bug for HostAP mode.
2327109323Ssam			 */
2328109323Ssam			if (sc->sc_firmware_type == WI_INTERSIL &&
2329109323Ssam			    ic->ic_opmode == IEEE80211_M_HOSTAP)
2330109323Ssam				val |= HOST_ENCRYPT;
2331109323Ssam		} else {
2332109323Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE,
2333109323Ssam			    IEEE80211_AUTH_OPEN);
2334109323Ssam			val = HOST_ENCRYPT | HOST_DECRYPT;
2335109323Ssam		}
2336109323Ssam		error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
2337109323Ssam		if (error)
2338109323Ssam			break;
2339109323Ssam		error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
2340109323Ssam		    ic->ic_wep_txkey);
2341109323Ssam		if (error)
2342109323Ssam			break;
2343109323Ssam		/*
2344109323Ssam		 * It seems that the firmware accept 104bit key only if
2345109323Ssam		 * all the keys have 104bit length.  We get the length of
2346109323Ssam		 * the transmit key and use it for all other keys.
2347109323Ssam		 * Perhaps we should use software WEP for such situation.
2348109323Ssam		 */
2349109323Ssam		keylen = ic->ic_nw_keys[ic->ic_wep_txkey].wk_len;
2350109323Ssam		if (keylen > IEEE80211_WEP_KEYLEN)
2351109323Ssam			keylen = 13;	/* 104bit keys */
2352109323Ssam		else
2353109323Ssam			keylen = IEEE80211_WEP_KEYLEN;
2354109323Ssam		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2355109323Ssam			error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
2356109323Ssam			    ic->ic_nw_keys[i].wk_key, keylen);
2357109323Ssam			if (error)
2358109323Ssam				break;
2359109323Ssam		}
2360109323Ssam		break;
2361109323Ssam	}
2362109323Ssam	return error;
236346492Swpaul}
236446492Swpaul
2365109323Ssamstatic int
2366109323Ssamwi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
236746492Swpaul{
2368109323Ssam	int			i, s = 0;
2369109323Ssam	static volatile int count  = 0;
2370109323Ssam
2371123098Simp	if (sc->wi_gone)
2372123098Simp		return (ENODEV);
2373123098Simp
2374109323Ssam	if (count > 0)
2375109323Ssam		panic("Hey partner, hold on there!");
2376109323Ssam	count++;
237753702Swpaul
2378109323Ssam	/* wait for the busy bit to clear */
2379123339Simp	for (i = sc->wi_cmd_count; i > 0; i--) {	/* 500ms */
2380116206Simp		if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
2381109323Ssam			break;
2382123098Simp		DELAY(1*1000);	/* 1ms */
2383109323Ssam	}
2384109323Ssam	if (i == 0) {
2385109323Ssam		device_printf(sc->sc_dev, "wi_cmd: busy bit won't clear.\n" );
2386123098Simp		sc->wi_gone = 1;
2387109323Ssam		count--;
2388109323Ssam		return(ETIMEDOUT);
2389109323Ssam	}
239090580Sbrooks
2391109323Ssam	CSR_WRITE_2(sc, WI_PARAM0, val0);
2392109323Ssam	CSR_WRITE_2(sc, WI_PARAM1, val1);
2393109323Ssam	CSR_WRITE_2(sc, WI_PARAM2, val2);
2394109323Ssam	CSR_WRITE_2(sc, WI_COMMAND, cmd);
239590580Sbrooks
2396109323Ssam	if (cmd == WI_CMD_INI) {
2397109323Ssam		/* XXX: should sleep here. */
2398116206Simp		DELAY(100*1000);		/* 100ms delay for init */
2399109323Ssam	}
2400109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
2401109323Ssam		/*
2402109323Ssam		 * Wait for 'command complete' bit to be
2403109323Ssam		 * set in the event status register.
2404109323Ssam		 */
2405109323Ssam		s = CSR_READ_2(sc, WI_EVENT_STAT);
2406109323Ssam		if (s & WI_EV_CMD) {
2407109323Ssam			/* Ack the event and read result code. */
2408109323Ssam			s = CSR_READ_2(sc, WI_STATUS);
2409109323Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
2410109323Ssam			if (s & WI_STAT_CMD_RESULT) {
2411109323Ssam				count--;
2412109323Ssam				return(EIO);
2413109323Ssam			}
2414109323Ssam			break;
241590580Sbrooks		}
2416109323Ssam		DELAY(WI_DELAY);
2417109323Ssam	}
241890580Sbrooks
2419109323Ssam	count--;
2420109323Ssam	if (i == WI_TIMEOUT) {
2421109323Ssam		device_printf(sc->sc_dev,
2422109323Ssam		    "timeout in wi_cmd 0x%04x; event status 0x%04x\n", cmd, s);
2423123098Simp		if (s == 0xffff)
2424123098Simp			sc->wi_gone = 1;
2425109323Ssam		return(ETIMEDOUT);
242653702Swpaul	}
2427109323Ssam	return (0);
2428109323Ssam}
242953702Swpaul
2430109323Ssamstatic int
2431109323Ssamwi_seek_bap(struct wi_softc *sc, int id, int off)
2432109323Ssam{
2433109323Ssam	int i, status;
243490580Sbrooks
2435109323Ssam	CSR_WRITE_2(sc, WI_SEL0, id);
2436109323Ssam	CSR_WRITE_2(sc, WI_OFF0, off);
243790580Sbrooks
2438109323Ssam	for (i = 0; ; i++) {
2439109323Ssam		status = CSR_READ_2(sc, WI_OFF0);
2440109323Ssam		if ((status & WI_OFF_BUSY) == 0)
2441109323Ssam			break;
2442109323Ssam		if (i == WI_TIMEOUT) {
2443109323Ssam			device_printf(sc->sc_dev, "timeout in wi_seek to %x/%x\n",
2444109323Ssam			    id, off);
2445109323Ssam			sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2446123098Simp			if (status == 0xffff)
2447123098Simp				sc->wi_gone = 1;
2448109323Ssam			return ETIMEDOUT;
2449109323Ssam		}
2450109323Ssam		DELAY(1);
245153702Swpaul	}
2452109323Ssam	if (status & WI_OFF_ERR) {
2453109323Ssam		device_printf(sc->sc_dev, "failed in wi_seek to %x/%x\n", id, off);
2454109323Ssam		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2455109323Ssam		return EIO;
2456109323Ssam	}
2457109323Ssam	sc->sc_bap_id = id;
2458109323Ssam	sc->sc_bap_off = off;
2459109323Ssam	return 0;
246053702Swpaul}
246153702Swpaul
2462109323Ssamstatic int
2463109323Ssamwi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
246453702Swpaul{
2465109323Ssam	u_int16_t *ptr;
2466109323Ssam	int i, error, cnt;
246753702Swpaul
2468109323Ssam	if (buflen == 0)
2469109323Ssam		return 0;
2470109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2471109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
2472109323Ssam			return error;
247375219Salfred	}
2474109323Ssam	cnt = (buflen + 1) / 2;
2475109323Ssam	ptr = (u_int16_t *)buf;
2476109323Ssam	for (i = 0; i < cnt; i++)
2477109323Ssam		*ptr++ = CSR_READ_2(sc, WI_DATA0);
2478109323Ssam	sc->sc_bap_off += cnt * 2;
2479109323Ssam	return 0;
248053702Swpaul}
248153702Swpaul
2482109323Ssamstatic int
2483109323Ssamwi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
248453702Swpaul{
2485109323Ssam	u_int16_t *ptr;
2486109323Ssam	int i, error, cnt;
248746492Swpaul
2488109323Ssam	if (buflen == 0)
2489109323Ssam		return 0;
249046492Swpaul
2491109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
2492109323Ssam  again:
2493109323Ssam#endif
2494109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2495109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
2496109323Ssam			return error;
2497109323Ssam	}
2498109323Ssam	cnt = (buflen + 1) / 2;
2499109323Ssam	ptr = (u_int16_t *)buf;
2500109323Ssam	for (i = 0; i < cnt; i++)
2501109323Ssam		CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
2502109323Ssam	sc->sc_bap_off += cnt * 2;
2503109323Ssam
2504109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
2505109323Ssam	/*
2506109323Ssam	 * According to the comments in the HCF Light code, there is a bug
2507109323Ssam	 * in the Hermes (or possibly in certain Hermes firmware revisions)
2508109323Ssam	 * where the chip's internal autoincrement counter gets thrown off
2509109323Ssam	 * during data writes:  the autoincrement is missed, causing one
2510109323Ssam	 * data word to be overwritten and subsequent words to be written to
2511109323Ssam	 * the wrong memory locations. The end result is that we could end
2512109323Ssam	 * up transmitting bogus frames without realizing it. The workaround
2513109323Ssam	 * for this is to write a couple of extra guard words after the end
2514109323Ssam	 * of the transfer, then attempt to read then back. If we fail to
2515109323Ssam	 * locate the guard words where we expect them, we preform the
2516109323Ssam	 * transfer over again.
2517109323Ssam	 */
2518109323Ssam	if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
2519109323Ssam		CSR_WRITE_2(sc, WI_DATA0, 0x1234);
2520109323Ssam		CSR_WRITE_2(sc, WI_DATA0, 0x5678);
2521109323Ssam		wi_seek_bap(sc, id, sc->sc_bap_off);
2522109323Ssam		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2523109323Ssam		if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
2524109323Ssam		    CSR_READ_2(sc, WI_DATA0) != 0x5678) {
2525109323Ssam			device_printf(sc->sc_dev,
2526109323Ssam				"detect auto increment bug, try again\n");
2527109323Ssam			goto again;
2528109323Ssam		}
2529109323Ssam	}
2530109323Ssam#endif
2531109323Ssam	return 0;
253246492Swpaul}
253353702Swpaul
2534109323Ssamstatic int
2535109323Ssamwi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
2536109323Ssam{
2537109323Ssam	int error, len;
2538109323Ssam	struct mbuf *m;
253953702Swpaul
2540109323Ssam	for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
2541109323Ssam		if (m->m_len == 0)
2542109323Ssam			continue;
254353702Swpaul
2544109323Ssam		len = min(m->m_len, totlen);
254553702Swpaul
2546109323Ssam		if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
2547109323Ssam			m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
2548109323Ssam			return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
2549109323Ssam			    totlen);
2550109323Ssam		}
255153702Swpaul
2552109323Ssam		if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
2553109323Ssam			return error;
255453702Swpaul
2555109323Ssam		off += m->m_len;
2556109323Ssam		totlen -= len;
2557109323Ssam	}
2558109323Ssam	return 0;
2559109323Ssam}
256053702Swpaul
2561109323Ssamstatic int
2562109323Ssamwi_alloc_fid(struct wi_softc *sc, int len, int *idp)
256353702Swpaul{
256453702Swpaul	int i;
256553702Swpaul
2566109323Ssam	if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
2567109323Ssam		device_printf(sc->sc_dev, "failed to allocate %d bytes on NIC\n",
2568109323Ssam		    len);
2569109323Ssam		return ENOMEM;
257053702Swpaul	}
257153702Swpaul
2572109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
2573109323Ssam		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
2574109323Ssam			break;
2575109323Ssam		if (i == WI_TIMEOUT) {
2576109323Ssam			device_printf(sc->sc_dev, "timeout in alloc\n");
2577109323Ssam			return ETIMEDOUT;
2578109323Ssam		}
2579109323Ssam		DELAY(1);
258053702Swpaul	}
2581109323Ssam	*idp = CSR_READ_2(sc, WI_ALLOC_FID);
2582109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
2583109323Ssam	return 0;
2584109323Ssam}
258553702Swpaul
2586109323Ssamstatic int
2587109323Ssamwi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
2588109323Ssam{
2589109323Ssam	int error, len;
2590109323Ssam	u_int16_t ltbuf[2];
259153702Swpaul
2592109323Ssam	/* Tell the NIC to enter record read mode. */
2593109323Ssam	error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2594109323Ssam	if (error)
2595109323Ssam		return error;
259653702Swpaul
2597109323Ssam	error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2598109323Ssam	if (error)
2599109323Ssam		return error;
260053702Swpaul
2601109323Ssam	if (le16toh(ltbuf[1]) != rid) {
2602109323Ssam		device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n",
2603109323Ssam		    rid, le16toh(ltbuf[1]));
2604109323Ssam		return EIO;
260553702Swpaul	}
2606109323Ssam	len = (le16toh(ltbuf[0]) - 1) * 2;	 /* already got rid */
2607109323Ssam	if (*buflenp < len) {
2608109323Ssam		device_printf(sc->sc_dev, "record buffer is too small, "
2609109323Ssam		    "rid=%x, size=%d, len=%d\n",
2610109323Ssam		    rid, *buflenp, len);
2611109323Ssam		return ENOSPC;
261253702Swpaul	}
2613109323Ssam	*buflenp = len;
2614109323Ssam	return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2615109323Ssam}
261653702Swpaul
2617109323Ssamstatic int
2618109323Ssamwi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2619109323Ssam{
2620109323Ssam	int error;
2621109323Ssam	u_int16_t ltbuf[2];
262253702Swpaul
2623109323Ssam	ltbuf[0] = htole16((buflen + 1) / 2 + 1);	 /* includes rid */
2624109323Ssam	ltbuf[1] = htole16(rid);
262553702Swpaul
2626109323Ssam	error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2627109323Ssam	if (error)
2628109323Ssam		return error;
2629109323Ssam	error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2630109323Ssam	if (error)
2631109323Ssam		return error;
2632102206Simp
2633109323Ssam	return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
263453702Swpaul}
263577217Sphk
263688546Salfredstatic int
2637117812Ssamwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
263877217Sphk{
2639117812Ssam	struct ifnet *ifp = &ic->ic_if;
2640117812Ssam	struct wi_softc *sc = ifp->if_softc;
2641116951Ssam	struct ieee80211_node *ni = ic->ic_bss;
2642116951Ssam	int buflen;
2643109323Ssam	u_int16_t val;
2644109323Ssam	struct wi_ssid ssid;
2645109323Ssam	u_int8_t old_bssid[IEEE80211_ADDR_LEN];
264677217Sphk
2647117812Ssam	DPRINTF(("%s: %s -> %s\n", __func__,
2648117812Ssam		ieee80211_state_name[ic->ic_state],
2649117812Ssam		ieee80211_state_name[nstate]));
2650109323Ssam
2651109323Ssam	switch (nstate) {
2652109323Ssam	case IEEE80211_S_INIT:
2653109323Ssam		ic->ic_flags &= ~IEEE80211_F_SIBSS;
2654109323Ssam		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2655117812Ssam		return (*sc->sc_newstate)(ic, nstate, arg);
2656109323Ssam
2657109323Ssam	case IEEE80211_S_RUN:
2658109323Ssam		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2659109323Ssam		buflen = IEEE80211_ADDR_LEN;
2660109323Ssam		wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen);
2661109323Ssam		IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
2662109323Ssam		buflen = sizeof(val);
2663109323Ssam		wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
2664116951Ssam		/* XXX validate channel */
2665116951Ssam		ni->ni_chan = &ic->ic_channels[le16toh(val)];
2666119784Ssam#if NBPFILTER > 0
2667119784Ssam		sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
2668119784Ssam			htole16(ni->ni_chan->ic_freq);
2669119784Ssam		sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
2670119784Ssam			htole16(ni->ni_chan->ic_flags);
2671119784Ssam#endif
2672109323Ssam
2673109323Ssam		if (IEEE80211_ADDR_EQ(old_bssid, ni->ni_bssid))
2674109323Ssam			sc->sc_false_syns++;
2675109323Ssam		else
2676109323Ssam			sc->sc_false_syns = 0;
2677109323Ssam
2678109323Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2679109323Ssam			ni->ni_esslen = ic->ic_des_esslen;
2680109323Ssam			memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
2681116951Ssam			ni->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B];
2682109323Ssam			ni->ni_intval = ic->ic_lintval;
2683109323Ssam			ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
2684109323Ssam			if (ic->ic_flags & IEEE80211_F_WEPON)
2685109323Ssam				ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
268677217Sphk		} else {
2687109323Ssam			/* XXX check return value */
2688109323Ssam			buflen = sizeof(ssid);
2689109323Ssam			wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
2690109323Ssam			ni->ni_esslen = le16toh(ssid.wi_len);
2691109323Ssam			if (ni->ni_esslen > IEEE80211_NWID_LEN)
2692109323Ssam				ni->ni_esslen = IEEE80211_NWID_LEN;	/*XXX*/
2693109323Ssam			memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
269477217Sphk		}
269577217Sphk		break;
2696109323Ssam
2697109323Ssam	case IEEE80211_S_SCAN:
2698109323Ssam	case IEEE80211_S_AUTH:
2699109323Ssam	case IEEE80211_S_ASSOC:
270077217Sphk		break;
270177217Sphk	}
270277217Sphk
2703117812Ssam	ic->ic_state = nstate;		/* NB: skip normal ieee80211 handling */
2704117812Ssam	return 0;
270577217Sphk}
270677217Sphk
270788546Salfredstatic int
2708116898Ssamwi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate)
270977217Sphk{
2710109323Ssam	int error = 0;
2711109323Ssam	u_int16_t val[2];
271277217Sphk
2713109323Ssam	if (!sc->sc_enabled)
2714109323Ssam		return ENXIO;
2715109323Ssam	switch (sc->sc_firmware_type) {
2716109323Ssam	case WI_LUCENT:
2717109323Ssam		(void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
271898440Simp		break;
2719109323Ssam	case WI_INTERSIL:
2720116898Ssam		val[0] = chanmask;	/* channel */
2721116898Ssam		val[1] = txrate;	/* tx rate */
2722109323Ssam		error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
272398440Simp		break;
2724109323Ssam	case WI_SYMBOL:
2725109323Ssam		/*
2726109323Ssam		 * XXX only supported on 3.x ?
2727109323Ssam		 */
2728109323Ssam		val[0] = BSCAN_BCAST | BSCAN_ONETIME;
2729109323Ssam		error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
2730109323Ssam		    val, sizeof(val[0]));
273198440Simp		break;
273298440Simp	}
2733109323Ssam	if (error == 0) {
2734109323Ssam		sc->sc_scan_timer = WI_SCAN_WAIT;
2735109323Ssam		sc->sc_ic.ic_if.if_timer = 1;
2736116898Ssam		DPRINTF(("wi_scan_ap: start scanning, "
2737116898Ssam			"chamask 0x%x txrate 0x%x\n", chanmask, txrate));
2738109323Ssam	}
2739109323Ssam	return error;
2740109323Ssam}
274177217Sphk
2742109323Ssamstatic void
2743109323Ssamwi_scan_result(struct wi_softc *sc, int fid, int cnt)
2744109323Ssam{
2745109323Ssam#define	N(a)	(sizeof (a) / sizeof (a[0]))
2746109323Ssam	int i, naps, off, szbuf;
2747109323Ssam	struct wi_scan_header ws_hdr;	/* Prism2 header */
2748109323Ssam	struct wi_scan_data_p2 ws_dat;	/* Prism2 scantable*/
2749109323Ssam	struct wi_apinfo *ap;
2750109323Ssam
2751109323Ssam	off = sizeof(u_int16_t) * 2;
2752109323Ssam	memset(&ws_hdr, 0, sizeof(ws_hdr));
2753109323Ssam	switch (sc->sc_firmware_type) {
2754109323Ssam	case WI_INTERSIL:
2755109323Ssam		wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
2756109323Ssam		off += sizeof(ws_hdr);
2757109323Ssam		szbuf = sizeof(struct wi_scan_data_p2);
275877217Sphk		break;
2759109323Ssam	case WI_SYMBOL:
2760109323Ssam		szbuf = sizeof(struct wi_scan_data_p2) + 6;
276177217Sphk		break;
2762109323Ssam	case WI_LUCENT:
2763109323Ssam		szbuf = sizeof(struct wi_scan_data);
276477217Sphk		break;
2765109323Ssam	default:
2766109323Ssam		device_printf(sc->sc_dev,
2767109323Ssam			"wi_scan_result: unknown firmware type %u\n",
2768109323Ssam			sc->sc_firmware_type);
2769109323Ssam		naps = 0;
2770109323Ssam		goto done;
277177217Sphk	}
2772109323Ssam	naps = (cnt * 2 + 2 - off) / szbuf;
2773109323Ssam	if (naps > N(sc->sc_aps))
2774109323Ssam		naps = N(sc->sc_aps);
2775109323Ssam	sc->sc_naps = naps;
2776109323Ssam	/* Read Data */
2777109323Ssam	ap = sc->sc_aps;
2778109323Ssam	memset(&ws_dat, 0, sizeof(ws_dat));
2779109323Ssam	for (i = 0; i < naps; i++, ap++) {
2780109323Ssam		wi_read_bap(sc, fid, off, &ws_dat,
2781109323Ssam		    (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
2782109323Ssam		DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off,
2783109323Ssam		    ether_sprintf(ws_dat.wi_bssid)));
2784109323Ssam		off += szbuf;
2785109323Ssam		ap->scanreason = le16toh(ws_hdr.wi_reason);
2786109323Ssam		memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
2787109323Ssam		ap->channel = le16toh(ws_dat.wi_chid);
2788109323Ssam		ap->signal  = le16toh(ws_dat.wi_signal);
2789109323Ssam		ap->noise   = le16toh(ws_dat.wi_noise);
2790109323Ssam		ap->quality = ap->signal - ap->noise;
2791109323Ssam		ap->capinfo = le16toh(ws_dat.wi_capinfo);
2792109323Ssam		ap->interval = le16toh(ws_dat.wi_interval);
2793109323Ssam		ap->rate    = le16toh(ws_dat.wi_rate);
2794109323Ssam		ap->namelen = le16toh(ws_dat.wi_namelen);
2795109323Ssam		if (ap->namelen > sizeof(ap->name))
2796109323Ssam			ap->namelen = sizeof(ap->name);
2797109323Ssam		memcpy(ap->name, ws_dat.wi_name, ap->namelen);
2798109323Ssam	}
2799109323Ssamdone:
2800109323Ssam	/* Done scanning */
2801109323Ssam	sc->sc_scan_timer = 0;
2802109323Ssam	DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
2803109323Ssam#undef N
280477217Sphk}
280577217Sphk
280688546Salfredstatic void
2807109323Ssamwi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi)
280877217Sphk{
2809109323Ssam	ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr),
2810116951Ssam	    ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL : -1, rssi);
2811109323Ssam	printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n",
2812109323Ssam		le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1),
2813109323Ssam		le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence);
2814109323Ssam	printf(" rx_signal %u rx_rate %u rx_flow %u\n",
2815109323Ssam		wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow);
2816109323Ssam	printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n",
2817109323Ssam		wh->wi_tx_rtry, wh->wi_tx_rate,
2818109323Ssam		le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len));
2819109323Ssam	printf(" ehdr dst %6D src %6D type 0x%x\n",
2820109323Ssam		wh->wi_ehdr.ether_dhost, ":", wh->wi_ehdr.ether_shost, ":",
2821109323Ssam		wh->wi_ehdr.ether_type);
2822109323Ssam}
282377217Sphk
2824109323Ssamint
2825109323Ssamwi_alloc(device_t dev, int rid)
2826109323Ssam{
2827109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2828109323Ssam
2829109323Ssam	if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2830109323Ssam		sc->iobase_rid = rid;
2831109323Ssam		sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2832109323Ssam		    &sc->iobase_rid, 0, ~0, (1 << 6),
2833109323Ssam		    rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2834109323Ssam		if (!sc->iobase) {
2835109323Ssam			device_printf(dev, "No I/O space?!\n");
2836109323Ssam			return (ENXIO);
283798440Simp		}
2838109323Ssam
2839109323Ssam		sc->wi_io_addr = rman_get_start(sc->iobase);
2840109323Ssam		sc->wi_btag = rman_get_bustag(sc->iobase);
2841109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2842109323Ssam	} else {
2843109323Ssam		sc->mem_rid = rid;
2844127135Snjl		sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2845127135Snjl		    &sc->mem_rid, RF_ACTIVE);
2846109323Ssam
2847109323Ssam		if (!sc->mem) {
2848109323Ssam			device_printf(dev, "No Mem space on prism2.5?\n");
2849109323Ssam			return (ENXIO);
285077217Sphk		}
2851109323Ssam
2852109323Ssam		sc->wi_btag = rman_get_bustag(sc->mem);
2853109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->mem);
285477217Sphk	}
285577217Sphk
2856109323Ssam
2857109323Ssam	sc->irq_rid = 0;
2858127135Snjl	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2859127135Snjl	    RF_ACTIVE |
2860109323Ssam	    ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2861109323Ssam
2862109323Ssam	if (!sc->irq) {
2863109323Ssam		wi_free(dev);
2864109323Ssam		device_printf(dev, "No irq?!\n");
2865109323Ssam		return (ENXIO);
286677217Sphk	}
2867109323Ssam
2868109323Ssam	sc->sc_dev = dev;
2869109323Ssam	sc->sc_unit = device_get_unit(dev);
2870109323Ssam
2871109323Ssam	return (0);
287277217Sphk}
287393359Simp
2874109323Ssamvoid
2875109323Ssamwi_free(device_t dev)
2876109323Ssam{
2877109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2878109323Ssam
2879109323Ssam	if (sc->iobase != NULL) {
2880109323Ssam		bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2881109323Ssam		sc->iobase = NULL;
2882109323Ssam	}
2883109323Ssam	if (sc->irq != NULL) {
2884109323Ssam		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2885109323Ssam		sc->irq = NULL;
2886109323Ssam	}
2887109323Ssam	if (sc->mem != NULL) {
2888109323Ssam		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
2889109323Ssam		sc->mem = NULL;
2890109323Ssam	}
2891109323Ssam
2892109323Ssam	return;
2893109323Ssam}
2894109323Ssam
289593359Simpstatic int
2896109323Ssamwi_get_debug(struct wi_softc *sc, struct wi_req *wreq)
289793359Simp{
2898109323Ssam	int error = 0;
289993359Simp
290093359Simp	wreq->wi_len = 1;
290193359Simp
290293359Simp	switch (wreq->wi_type) {
290393359Simp	case WI_DEBUG_SLEEP:
290493359Simp		wreq->wi_len++;
290593359Simp		wreq->wi_val[0] = sc->wi_debug.wi_sleep;
290693359Simp		break;
290793359Simp	case WI_DEBUG_DELAYSUPP:
290893359Simp		wreq->wi_len++;
290993359Simp		wreq->wi_val[0] = sc->wi_debug.wi_delaysupp;
291093359Simp		break;
291193359Simp	case WI_DEBUG_TXSUPP:
291293359Simp		wreq->wi_len++;
291393359Simp		wreq->wi_val[0] = sc->wi_debug.wi_txsupp;
291493359Simp		break;
291593359Simp	case WI_DEBUG_MONITOR:
291693359Simp		wreq->wi_len++;
291793359Simp		wreq->wi_val[0] = sc->wi_debug.wi_monitor;
291893359Simp		break;
291993359Simp	case WI_DEBUG_LEDTEST:
292093359Simp		wreq->wi_len += 3;
292193359Simp		wreq->wi_val[0] = sc->wi_debug.wi_ledtest;
292293359Simp		wreq->wi_val[1] = sc->wi_debug.wi_ledtest_param0;
292393359Simp		wreq->wi_val[2] = sc->wi_debug.wi_ledtest_param1;
292493359Simp		break;
292593359Simp	case WI_DEBUG_CONTTX:
292693359Simp		wreq->wi_len += 2;
292793359Simp		wreq->wi_val[0] = sc->wi_debug.wi_conttx;
292893359Simp		wreq->wi_val[1] = sc->wi_debug.wi_conttx_param0;
292993359Simp		break;
293093359Simp	case WI_DEBUG_CONTRX:
293193359Simp		wreq->wi_len++;
293293359Simp		wreq->wi_val[0] = sc->wi_debug.wi_contrx;
293393359Simp		break;
293493359Simp	case WI_DEBUG_SIGSTATE:
293593359Simp		wreq->wi_len += 2;
293693359Simp		wreq->wi_val[0] = sc->wi_debug.wi_sigstate;
293793359Simp		wreq->wi_val[1] = sc->wi_debug.wi_sigstate_param0;
293893359Simp		break;
293993359Simp	case WI_DEBUG_CONFBITS:
294093359Simp		wreq->wi_len += 2;
294193359Simp		wreq->wi_val[0] = sc->wi_debug.wi_confbits;
294293359Simp		wreq->wi_val[1] = sc->wi_debug.wi_confbits_param0;
294393359Simp		break;
294493359Simp	default:
294593359Simp		error = EIO;
294693359Simp		break;
294793359Simp	}
294893359Simp
294993359Simp	return (error);
295093359Simp}
295193359Simp
295293359Simpstatic int
2953109323Ssamwi_set_debug(struct wi_softc *sc, struct wi_req *wreq)
295493359Simp{
2955109323Ssam	int error = 0;
295693359Simp	u_int16_t		cmd, param0 = 0, param1 = 0;
295793359Simp
295893359Simp	switch (wreq->wi_type) {
295993359Simp	case WI_DEBUG_RESET:
296093359Simp	case WI_DEBUG_INIT:
296193359Simp	case WI_DEBUG_CALENABLE:
296293359Simp		break;
296393359Simp	case WI_DEBUG_SLEEP:
296493359Simp		sc->wi_debug.wi_sleep = 1;
296593359Simp		break;
296693359Simp	case WI_DEBUG_WAKE:
296793359Simp		sc->wi_debug.wi_sleep = 0;
296893359Simp		break;
296993359Simp	case WI_DEBUG_CHAN:
297093359Simp		param0 = wreq->wi_val[0];
297193359Simp		break;
297293359Simp	case WI_DEBUG_DELAYSUPP:
297393359Simp		sc->wi_debug.wi_delaysupp = 1;
297493359Simp		break;
297593359Simp	case WI_DEBUG_TXSUPP:
297693359Simp		sc->wi_debug.wi_txsupp = 1;
297793359Simp		break;
297893359Simp	case WI_DEBUG_MONITOR:
297993359Simp		sc->wi_debug.wi_monitor = 1;
298093359Simp		break;
298193359Simp	case WI_DEBUG_LEDTEST:
298293359Simp		param0 = wreq->wi_val[0];
298393359Simp		param1 = wreq->wi_val[1];
298493359Simp		sc->wi_debug.wi_ledtest = 1;
298593359Simp		sc->wi_debug.wi_ledtest_param0 = param0;
298693359Simp		sc->wi_debug.wi_ledtest_param1 = param1;
298793359Simp		break;
298893359Simp	case WI_DEBUG_CONTTX:
298993359Simp		param0 = wreq->wi_val[0];
299093359Simp		sc->wi_debug.wi_conttx = 1;
299193359Simp		sc->wi_debug.wi_conttx_param0 = param0;
299293359Simp		break;
299393359Simp	case WI_DEBUG_STOPTEST:
299493359Simp		sc->wi_debug.wi_delaysupp = 0;
299593359Simp		sc->wi_debug.wi_txsupp = 0;
299693359Simp		sc->wi_debug.wi_monitor = 0;
299793359Simp		sc->wi_debug.wi_ledtest = 0;
299893359Simp		sc->wi_debug.wi_ledtest_param0 = 0;
299993359Simp		sc->wi_debug.wi_ledtest_param1 = 0;
300093359Simp		sc->wi_debug.wi_conttx = 0;
300193359Simp		sc->wi_debug.wi_conttx_param0 = 0;
300293359Simp		sc->wi_debug.wi_contrx = 0;
300393359Simp		sc->wi_debug.wi_sigstate = 0;
300493359Simp		sc->wi_debug.wi_sigstate_param0 = 0;
300593359Simp		break;
300693359Simp	case WI_DEBUG_CONTRX:
300793359Simp		sc->wi_debug.wi_contrx = 1;
300893359Simp		break;
300993359Simp	case WI_DEBUG_SIGSTATE:
301093359Simp		param0 = wreq->wi_val[0];
301193359Simp		sc->wi_debug.wi_sigstate = 1;
301293359Simp		sc->wi_debug.wi_sigstate_param0 = param0;
301393359Simp		break;
301493359Simp	case WI_DEBUG_CONFBITS:
301593359Simp		param0 = wreq->wi_val[0];
301693359Simp		param1 = wreq->wi_val[1];
301793359Simp		sc->wi_debug.wi_confbits = param0;
301893359Simp		sc->wi_debug.wi_confbits_param0 = param1;
301993359Simp		break;
302093359Simp	default:
302193359Simp		error = EIO;
302293359Simp		break;
302393359Simp	}
302493359Simp
302593359Simp	if (error)
302693359Simp		return (error);
302793359Simp
302893359Simp	cmd = WI_CMD_DEBUG | (wreq->wi_type << 8);
302993359Simp	error = wi_cmd(sc, cmd, param0, param1, 0);
303093359Simp
303193359Simp	return (error);
303293359Simp}
3033101903Simp
3034105076Simp#if __FreeBSD_version >= 500000
3035101903Simp/*
3036101903Simp * Special routines to download firmware for Symbol CF card.
3037101903Simp * XXX: This should be modified generic into any PRISM-2 based card.
3038101903Simp */
3039101903Simp
3040101903Simp#define	WI_SBCF_PDIADDR		0x3100
3041101903Simp
3042101903Simp/* unaligned load little endian */
3043101903Simp#define	GETLE32(p)	((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24))
3044101903Simp#define	GETLE16(p)	((p)[0] | ((p)[1]<<8))
3045101903Simp
3046101903Simpint
3047101903Simpwi_symbol_load_firm(struct wi_softc *sc, const void *primsym, int primlen,
3048101903Simp    const void *secsym, int seclen)
3049101903Simp{
3050101903Simp	uint8_t ebuf[256];
3051101903Simp	int i;
3052101903Simp
3053101903Simp	/* load primary code and run it */
3054101903Simp	wi_symbol_set_hcr(sc, WI_HCR_EEHOLD);
3055101903Simp	if (wi_symbol_write_firm(sc, primsym, primlen, NULL, 0))
3056101903Simp		return EIO;
3057101903Simp	wi_symbol_set_hcr(sc, WI_HCR_RUN);
3058101903Simp	for (i = 0; ; i++) {
3059101903Simp		if (i == 10)
3060101903Simp			return ETIMEDOUT;
3061101903Simp		tsleep(sc, PWAIT, "wiinit", 1);
3062101903Simp		if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT)
3063101903Simp			break;
3064101903Simp		/* write the magic key value to unlock aux port */
3065101903Simp		CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0);
3066101903Simp		CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1);
3067101903Simp		CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2);
3068101903Simp		CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL);
3069101903Simp	}
3070101903Simp
3071101903Simp	/* issue read EEPROM command: XXX copied from wi_cmd() */
3072101903Simp	CSR_WRITE_2(sc, WI_PARAM0, 0);
3073101903Simp	CSR_WRITE_2(sc, WI_PARAM1, 0);
3074101903Simp	CSR_WRITE_2(sc, WI_PARAM2, 0);
3075101903Simp	CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE);
3076101903Simp        for (i = 0; i < WI_TIMEOUT; i++) {
3077101903Simp                if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
3078101903Simp                        break;
3079101903Simp                DELAY(1);
3080101903Simp        }
3081101903Simp        CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
3082101903Simp
3083101903Simp	CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ);
3084101903Simp	CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ);
3085101903Simp	CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA,
3086101903Simp	    (uint16_t *)ebuf, sizeof(ebuf) / 2);
3087101903Simp	if (GETLE16(ebuf) > sizeof(ebuf))
3088101903Simp		return EIO;
3089101903Simp	if (wi_symbol_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf)))
3090101903Simp		return EIO;
3091101903Simp	return 0;
3092101903Simp}
3093101903Simp
3094101903Simpstatic int
3095101903Simpwi_symbol_write_firm(struct wi_softc *sc, const void *buf, int buflen,
3096101903Simp    const void *ebuf, int ebuflen)
3097101903Simp{
3098101903Simp	const uint8_t *p, *ep, *q, *eq;
3099101903Simp	char *tp;
3100101903Simp	uint32_t addr, id, eid;
3101101903Simp	int i, len, elen, nblk, pdrlen;
3102101903Simp
3103101903Simp	/*
3104101903Simp	 * Parse the header of the firmware image.
3105101903Simp	 */
3106101903Simp	p = buf;
3107101903Simp	ep = p + buflen;
3108101903Simp	while (p < ep && *p++ != ' ');	/* FILE: */
3109101903Simp	while (p < ep && *p++ != ' ');	/* filename */
3110101903Simp	while (p < ep && *p++ != ' ');	/* type of the firmware */
3111101903Simp	nblk = strtoul(p, &tp, 10);
3112101903Simp	p = tp;
3113101903Simp	pdrlen = strtoul(p + 1, &tp, 10);
3114101903Simp	p = tp;
3115101903Simp	while (p < ep && *p++ != 0x1a);	/* skip rest of header */
3116101903Simp
3117101903Simp	/*
3118101903Simp	 * Block records: address[4], length[2], data[length];
3119101903Simp	 */
3120101903Simp	for (i = 0; i < nblk; i++) {
3121101903Simp		addr = GETLE32(p);	p += 4;
3122101903Simp		len  = GETLE16(p);	p += 2;
3123101903Simp		CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3124101903Simp		CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3125101903Simp		CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3126101903Simp		    (const uint16_t *)p, len / 2);
3127101903Simp		p += len;
3128101903Simp	}
3129101903Simp
3130101903Simp	/*
3131101903Simp	 * PDR: id[4], address[4], length[4];
3132101903Simp	 */
3133101903Simp	for (i = 0; i < pdrlen; ) {
3134101903Simp		id   = GETLE32(p);	p += 4; i += 4;
3135101903Simp		addr = GETLE32(p);	p += 4; i += 4;
3136101903Simp		len  = GETLE32(p);	p += 4; i += 4;
3137101903Simp		/* replace PDR entry with the values from EEPROM, if any */
3138101903Simp		for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) {
3139101903Simp			elen = GETLE16(q);	q += 2;
3140101903Simp			eid  = GETLE16(q);	q += 2;
3141101903Simp			elen--;		/* elen includes eid */
3142101903Simp			if (eid == 0)
3143101903Simp				break;
3144101903Simp			if (eid != id)
3145101903Simp				continue;
3146101903Simp			CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3147101903Simp			CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3148101903Simp			CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3149101903Simp			    (const uint16_t *)q, len / 2);
3150101903Simp			break;
3151101903Simp		}
3152101903Simp	}
3153101903Simp	return 0;
3154101903Simp}
3155101903Simp
3156101903Simpstatic int
3157101903Simpwi_symbol_set_hcr(struct wi_softc *sc, int mode)
3158101903Simp{
3159101903Simp	uint16_t hcr;
3160101903Simp
3161101903Simp	CSR_WRITE_2(sc, WI_COR, WI_COR_RESET);
3162101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3163101903Simp	hcr = CSR_READ_2(sc, WI_HCR);
3164101903Simp	hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE);
3165101903Simp	CSR_WRITE_2(sc, WI_HCR, hcr);
3166101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3167101903Simp	CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE);
3168101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3169101903Simp	return 0;
3170101903Simp}
3171105076Simp#endif
3172