if_wi.c revision 119150
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 119150 2003-08-19 22:17:04Z sam $");
66113038Sobrien
67109323Ssam#define WI_HERMES_AUTOINC_WAR	/* Work around data write autoinc bug. */
68109323Ssam#define WI_HERMES_STATS_WAR	/* Work around stats counter bug. */
69109323Ssam
70109323Ssam#define	NBPFILTER	1
71109323Ssam
7246492Swpaul#include <sys/param.h>
7346492Swpaul#include <sys/systm.h>
7495706Simp#if __FreeBSD_version >= 500033
7595533Smike#include <sys/endian.h>
7695706Simp#endif
7746492Swpaul#include <sys/sockio.h>
7846492Swpaul#include <sys/mbuf.h>
7983366Sjulian#include <sys/proc.h>
8046492Swpaul#include <sys/kernel.h>
8146492Swpaul#include <sys/socket.h>
8253702Swpaul#include <sys/module.h>
8353702Swpaul#include <sys/bus.h>
8494486Simp#include <sys/random.h>
8553702Swpaul#include <sys/syslog.h>
8653702Swpaul#include <sys/sysctl.h>
8746492Swpaul
8853702Swpaul#include <machine/bus.h>
8953702Swpaul#include <machine/resource.h>
9095534Simp#include <machine/clock.h>
91116951Ssam#include <machine/atomic.h>
9253702Swpaul#include <sys/rman.h>
9353702Swpaul
9446492Swpaul#include <net/if.h>
9546492Swpaul#include <net/if_arp.h>
9646492Swpaul#include <net/ethernet.h>
9746492Swpaul#include <net/if_dl.h>
9846492Swpaul#include <net/if_media.h>
9946492Swpaul#include <net/if_types.h>
10046492Swpaul
101116951Ssam#include <net80211/ieee80211_var.h>
102116951Ssam#include <net80211/ieee80211_ioctl.h>
103116951Ssam
10446492Swpaul#include <netinet/in.h>
10546492Swpaul#include <netinet/in_systm.h>
10646492Swpaul#include <netinet/in_var.h>
10746492Swpaul#include <netinet/ip.h>
10846492Swpaul#include <netinet/if_ether.h>
10946492Swpaul
11046492Swpaul#include <net/bpf.h>
11146492Swpaul
11270808Speter#include <dev/wi/if_wavelan_ieee.h>
11393611Simp#include <dev/wi/if_wivar.h>
11470808Speter#include <dev/wi/if_wireg.h>
11546492Swpaul
116109323Ssam#define IF_POLL(ifq, m)		((m) = (ifq)->ifq_head)
117109323Ssam#define	IFQ_POLL(ifq, m)	IF_POLL((ifq), (m))
118109323Ssam#define IFQ_DEQUEUE(ifq, m)	IF_DEQUEUE((ifq), (m))
119109323Ssam
12091693Simpstatic void wi_start(struct ifnet *);
121109323Ssamstatic int  wi_reset(struct wi_softc *);
12291693Simpstatic void wi_watchdog(struct ifnet *);
123109323Ssamstatic int  wi_ioctl(struct ifnet *, u_long, caddr_t);
124109323Ssamstatic int  wi_media_change(struct ifnet *);
125109323Ssamstatic void wi_media_status(struct ifnet *, struct ifmediareq *);
12646492Swpaul
127109323Ssamstatic void wi_rx_intr(struct wi_softc *);
128109323Ssamstatic void wi_tx_intr(struct wi_softc *);
129109323Ssamstatic void wi_tx_ex_intr(struct wi_softc *);
130109323Ssamstatic void wi_info_intr(struct wi_softc *);
13146492Swpaul
132109323Ssamstatic int  wi_get_cfg(struct ifnet *, u_long, caddr_t);
133109323Ssamstatic int  wi_set_cfg(struct ifnet *, u_long, caddr_t);
134109323Ssamstatic int  wi_write_txrate(struct wi_softc *);
135109323Ssamstatic int  wi_write_wep(struct wi_softc *);
136109323Ssamstatic int  wi_write_multi(struct wi_softc *);
137109323Ssamstatic int  wi_alloc_fid(struct wi_softc *, int, int *);
138109323Ssamstatic void wi_read_nicid(struct wi_softc *);
139109323Ssamstatic int  wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
14053702Swpaul
141109323Ssamstatic int  wi_cmd(struct wi_softc *, int, int, int, int);
142109323Ssamstatic int  wi_seek_bap(struct wi_softc *, int, int);
143109323Ssamstatic int  wi_read_bap(struct wi_softc *, int, int, void *, int);
144109323Ssamstatic int  wi_write_bap(struct wi_softc *, int, int, void *, int);
145109323Ssamstatic int  wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
146109323Ssamstatic int  wi_read_rid(struct wi_softc *, int, void *, int *);
147109323Ssamstatic int  wi_write_rid(struct wi_softc *, int, void *, int);
14877217Sphk
149117812Ssamstatic int  wi_newstate(struct ieee80211com *, enum ieee80211_state, int);
150109323Ssam
151116898Ssamstatic int  wi_scan_ap(struct wi_softc *, u_int16_t, u_int16_t);
152109323Ssamstatic void wi_scan_result(struct wi_softc *, int, int);
153109323Ssam
154109323Ssamstatic void wi_dump_pkt(struct wi_frame *, struct ieee80211_node *, int rssi);
155109323Ssam
15693359Simpstatic int wi_get_debug(struct wi_softc *, struct wi_req *);
15793359Simpstatic int wi_set_debug(struct wi_softc *, struct wi_req *);
15893359Simp
159105076Simp#if __FreeBSD_version >= 500000
160101903Simp/* support to download firmware for symbol CF card */
161101903Simpstatic int wi_symbol_write_firm(struct wi_softc *, const void *, int,
162101903Simp		const void *, int);
163101903Simpstatic int wi_symbol_set_hcr(struct wi_softc *, int);
164105076Simp#endif
165101903Simp
166109323Ssamstatic __inline int
167109323Ssamwi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
168109323Ssam{
16953702Swpaul
170109323Ssam	val = htole16(val);
171109323Ssam	return wi_write_rid(sc, rid, &val, sizeof(val));
172109323Ssam}
173109323Ssam
174109592SsamSYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters");
175109592Ssam
176109323Ssamstatic	struct timeval lasttxerror;	/* time of last tx error msg */
177109323Ssamstatic	int curtxeps;			/* current tx error msgs/sec */
178111559Ssamstatic	int wi_txerate = 0;		/* tx error rate: max msgs/sec */
179109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate,
180111559Ssam	    0, "max tx error msgs/sec; 0 to disable msgs");
181109323Ssam
182109323Ssam#define	WI_DEBUG
183109323Ssam#ifdef WI_DEBUG
184109323Ssamstatic	int wi_debug = 0;
185109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug,
186109592Ssam	    0, "control debugging printfs");
187109323Ssam
188109323Ssam#define	DPRINTF(X)	if (wi_debug) printf X
189109323Ssam#define	DPRINTF2(X)	if (wi_debug > 1) printf X
190109323Ssam#define	IFF_DUMPPKTS(_ifp) \
191109323Ssam	(((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
192109323Ssam#else
193109323Ssam#define	DPRINTF(X)
194109323Ssam#define	DPRINTF2(X)
195109323Ssam#define	IFF_DUMPPKTS(_ifp)	0
196109323Ssam#endif
197109323Ssam
198109323Ssam#define WI_INTRS	(WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
199109323Ssam
20093825Simpstruct wi_card_ident wi_card_ident[] = {
20193825Simp	/* CARD_ID			CARD_NAME		FIRM_TYPE */
20293825Simp	{ WI_NIC_LUCENT_ID,		WI_NIC_LUCENT_STR,	WI_LUCENT },
20393825Simp	{ WI_NIC_SONY_ID,		WI_NIC_SONY_STR,	WI_LUCENT },
20493825Simp	{ WI_NIC_LUCENT_EMB_ID,		WI_NIC_LUCENT_EMB_STR,	WI_LUCENT },
20593825Simp	{ WI_NIC_EVB2_ID,		WI_NIC_EVB2_STR,	WI_INTERSIL },
20693825Simp	{ WI_NIC_HWB3763_ID,		WI_NIC_HWB3763_STR,	WI_INTERSIL },
20793825Simp	{ WI_NIC_HWB3163_ID,		WI_NIC_HWB3163_STR,	WI_INTERSIL },
20893825Simp	{ WI_NIC_HWB3163B_ID,		WI_NIC_HWB3163B_STR,	WI_INTERSIL },
20993825Simp	{ WI_NIC_EVB3_ID,		WI_NIC_EVB3_STR,	WI_INTERSIL },
21093825Simp	{ WI_NIC_HWB1153_ID,		WI_NIC_HWB1153_STR,	WI_INTERSIL },
21193825Simp	{ WI_NIC_P2_SST_ID,		WI_NIC_P2_SST_STR,	WI_INTERSIL },
21293825Simp	{ WI_NIC_EVB2_SST_ID,		WI_NIC_EVB2_SST_STR,	WI_INTERSIL },
21393825Simp	{ WI_NIC_3842_EVA_ID,		WI_NIC_3842_EVA_STR,	WI_INTERSIL },
21493825Simp	{ WI_NIC_3842_PCMCIA_AMD_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
21593825Simp	{ WI_NIC_3842_PCMCIA_SST_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
216101355Simp	{ WI_NIC_3842_PCMCIA_ATL_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
217101355Simp	{ WI_NIC_3842_PCMCIA_ATS_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
21893825Simp	{ WI_NIC_3842_MINI_AMD_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
21993825Simp	{ WI_NIC_3842_MINI_SST_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
220101355Simp	{ WI_NIC_3842_MINI_ATL_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
221101355Simp	{ WI_NIC_3842_MINI_ATS_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
22293825Simp	{ WI_NIC_3842_PCI_AMD_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
22393825Simp	{ WI_NIC_3842_PCI_SST_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
224101355Simp	{ WI_NIC_3842_PCI_ATS_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
225101355Simp	{ WI_NIC_3842_PCI_ATL_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
22693825Simp	{ WI_NIC_P3_PCMCIA_AMD_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
22793825Simp	{ WI_NIC_P3_PCMCIA_SST_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
228101355Simp	{ WI_NIC_P3_PCMCIA_ATL_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
229101355Simp	{ WI_NIC_P3_PCMCIA_ATS_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
23093825Simp	{ WI_NIC_P3_MINI_AMD_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
23193825Simp	{ WI_NIC_P3_MINI_SST_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
232101355Simp	{ WI_NIC_P3_MINI_ATL_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
233101355Simp	{ WI_NIC_P3_MINI_ATS_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
23493825Simp	{ 0,	NULL,	0 },
23593825Simp};
23693825Simp
237109323Ssamdevclass_t wi_devclass;
23846492Swpaul
23993611Simpint
240109323Ssamwi_attach(device_t dev)
24174906Salfred{
242109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
243109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
244109323Ssam	struct ifnet *ifp = &ic->ic_if;
245116951Ssam	int i, nrates, buflen;
246109323Ssam	u_int16_t val;
247109323Ssam	u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
248116951Ssam	struct ieee80211_rateset *rs;
249109323Ssam	static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
250109323Ssam		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
251109323Ssam	};
252109323Ssam	int error;
25374906Salfred
254109323Ssam	/*
255109323Ssam	 * NB: no locking is needed here; don't put it here
256109323Ssam	 *     unless you can prove it!
257109323Ssam	 */
25853702Swpaul	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
25974998Swpaul	    wi_intr, sc, &sc->wi_intrhand);
26053702Swpaul
26153702Swpaul	if (error) {
26253702Swpaul		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
26353702Swpaul		wi_free(dev);
26453702Swpaul		return (error);
26553702Swpaul	}
26653702Swpaul
26795534Simp#if __FreeBSD_version >= 500000
268109323Ssam	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
26993818Sjhb	    MTX_DEF | MTX_RECURSE);
27095534Simp#endif
27167092Swpaul
27246492Swpaul	/* Reset the NIC. */
273112096Simp	if (wi_reset(sc) != 0)
274109323Ssam		return ENXIO;		/* XXX */
27546492Swpaul
27676438Swpaul	/*
27776438Swpaul	 * Read the station address.
27876438Swpaul	 * And do it twice. I've seen PRISM-based cards that return
27976438Swpaul	 * an error when trying to read it the first time, which causes
28076438Swpaul	 * the probe to fail.
28176438Swpaul	 */
282109323Ssam	buflen = IEEE80211_ADDR_LEN;
283109323Ssam	error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
284109323Ssam	if (error != 0) {
285109323Ssam		buflen = IEEE80211_ADDR_LEN;
286109323Ssam		error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
287109323Ssam	}
288109323Ssam	if (error || IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) {
289109323Ssam		if (error != 0)
290109323Ssam			device_printf(dev, "mac read failed %d\n", error);
291109323Ssam		else
292109323Ssam			device_printf(dev, "mac read failed (all zeros)\n");
29375149Simp		wi_free(dev);
29475149Simp		return (error);
29575149Simp	}
296109323Ssam	device_printf(dev, "802.11 address: %6D\n", ic->ic_myaddr, ":");
29746492Swpaul
298109323Ssam	/* Read NIC identification */
299109323Ssam	wi_read_nicid(sc);
30046492Swpaul
30146492Swpaul	ifp->if_softc = sc;
302109323Ssam	ifp->if_unit = sc->sc_unit;
30346492Swpaul	ifp->if_name = "wi";
30446492Swpaul	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
30546492Swpaul	ifp->if_ioctl = wi_ioctl;
30646492Swpaul	ifp->if_start = wi_start;
30746492Swpaul	ifp->if_watchdog = wi_watchdog;
30846492Swpaul	ifp->if_init = wi_init;
30946492Swpaul	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
31046492Swpaul
311109323Ssam	ic->ic_phytype = IEEE80211_T_DS;
312109323Ssam	ic->ic_opmode = IEEE80211_M_STA;
313116951Ssam	ic->ic_caps = IEEE80211_C_PMGT | IEEE80211_C_AHDEMO;
314109323Ssam	ic->ic_state = IEEE80211_S_INIT;
31546492Swpaul
316116951Ssam	/*
317116951Ssam	 * Query the card for available channels and setup the
318116951Ssam	 * channel table.  We assume these are all 11b channels.
319116951Ssam	 */
320109323Ssam	buflen = sizeof(val);
321109323Ssam	if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
322109323Ssam		val = htole16(0x1fff);	/* assume 1-11 */
323116951Ssam	KASSERT(val != 0, ("wi_attach: no available channels listed!"));
324116951Ssam
325116951Ssam	val <<= 1;			/* shift for base 1 indices */
326116951Ssam	for (i = 1; i < 16; i++) {
327116951Ssam		if (isset((u_int8_t*)&val, i)) {
328116951Ssam			ic->ic_channels[i].ic_freq =
329116951Ssam				ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
330116951Ssam			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
331116951Ssam		}
332109323Ssam	}
33346492Swpaul
33446563Swpaul	/*
33546563Swpaul	 * Read the default channel from the NIC. This may vary
33646563Swpaul	 * depending on the country where the NIC was purchased, so
33746563Swpaul	 * we can't hard-code a default and expect it to work for
33846563Swpaul	 * everyone.
339116951Ssam	 *
340116951Ssam	 * If no channel is specified, let the 802.11 code select.
34146563Swpaul	 */
342109323Ssam	buflen = sizeof(val);
343116951Ssam	if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0) {
344116951Ssam		val = le16toh(val);
345116951Ssam		KASSERT(val < IEEE80211_CHAN_MAX &&
346116951Ssam			ic->ic_channels[val].ic_flags != 0,
347116951Ssam			("wi_attach: invalid own channel %u!", val));
348116951Ssam		ic->ic_ibss_chan = &ic->ic_channels[val];
349117042Ssam	} else {
350117042Ssam		device_printf(dev,
351117042Ssam			"WI_RID_OWN_CHNL failed, using first channel!\n");
352117042Ssam		ic->ic_ibss_chan = &ic->ic_channels[0];
353109323Ssam	}
35446563Swpaul
35556965Swpaul	/*
35698440Simp	 * Set flags based on firmware version.
35798440Simp	 */
35898440Simp	switch (sc->sc_firmware_type) {
35998440Simp	case WI_LUCENT:
360112363Simp		sc->sc_ntxbuf = 1;
361109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
362109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
363109323Ssam		/* XXX: not confirmed, but never seen for recent firmware */
364109323Ssam		if (sc->sc_sta_firmware_ver <  40000) {
365109323Ssam			sc->sc_flags |= WI_FLAGS_BUG_AUTOINC;
366109323Ssam		}
367109323Ssam#endif
36898440Simp		if (sc->sc_sta_firmware_ver >= 60000)
369109323Ssam			sc->sc_flags |= WI_FLAGS_HAS_MOR;
370117855Ssam		if (sc->sc_sta_firmware_ver >= 60006) {
371116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
372117855Ssam			ic->ic_caps |= IEEE80211_C_MONITOR;
373117855Ssam		}
374109323Ssam		sc->sc_ibss_port = htole16(1);
37598440Simp		break;
376109323Ssam
37798440Simp	case WI_INTERSIL:
378112363Simp		sc->sc_ntxbuf = WI_NTXBUF;
379109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR;
380109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_ROAMING;
381109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
382109323Ssam		if (sc->sc_sta_firmware_ver > 10101)
383109323Ssam			sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
384117855Ssam		if (sc->sc_sta_firmware_ver >= 800) {
385116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
386117855Ssam			ic->ic_caps |= IEEE80211_C_MONITOR;
387117855Ssam		}
388109396Simp		/*
389109396Simp		 * version 0.8.3 and newer are the only ones that are known
390109396Simp		 * to currently work.  Earlier versions can be made to work,
391109396Simp		 * at least according to the Linux driver.
392109396Simp		 */
393100734Simp		if (sc->sc_sta_firmware_ver >= 803)
394116951Ssam			ic->ic_caps |= IEEE80211_C_HOSTAP;
395109323Ssam		sc->sc_ibss_port = htole16(0);
39698440Simp		break;
397109323Ssam
39898440Simp	case WI_SYMBOL:
399112363Simp		sc->sc_ntxbuf = 1;
400109323Ssam		sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY;
40198440Simp		if (sc->sc_sta_firmware_ver >= 25000)
402116951Ssam			ic->ic_caps |= IEEE80211_C_IBSS;
403109323Ssam		sc->sc_ibss_port = htole16(4);
40498440Simp		break;
40598440Simp	}
40698440Simp
40798440Simp	/*
40856965Swpaul	 * Find out if we support WEP on this card.
40956965Swpaul	 */
410109323Ssam	buflen = sizeof(val);
411109323Ssam	if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
412109323Ssam	    val != htole16(0))
413116951Ssam		ic->ic_caps |= IEEE80211_C_WEP;
41456965Swpaul
415109323Ssam	/* Find supported rates. */
416109323Ssam	buflen = sizeof(ratebuf);
417116951Ssam	rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
418109323Ssam	if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
419116951Ssam		nrates = le16toh(*(u_int16_t *)ratebuf);
420116951Ssam		if (nrates > IEEE80211_RATE_MAXSIZE)
421116951Ssam			nrates = IEEE80211_RATE_MAXSIZE;
422116951Ssam		rs->rs_nrates = 0;
423116951Ssam		for (i = 0; i < nrates; i++)
424116951Ssam			if (ratebuf[2+i])
425116951Ssam				rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i];
426109323Ssam	} else {
427109323Ssam		/* XXX fallback on error? */
428116951Ssam		rs->rs_nrates = 0;
42998440Simp	}
43077217Sphk
431109323Ssam	buflen = sizeof(val);
432109323Ssam	if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
433109323Ssam	    wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
434109323Ssam		sc->sc_dbm_adjust = le16toh(val);
435109323Ssam	} else
436109323Ssam		sc->sc_dbm_adjust = 100;	/* default */
43746492Swpaul
438109323Ssam	sc->sc_max_datalen = 2304;
439109323Ssam	sc->sc_system_scale = 1;
440109323Ssam	sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN;
441109323Ssam	sc->sc_roaming_mode = 1;
44246492Swpaul
443109323Ssam	sc->sc_portnum = WI_DEFAULT_PORT;
444109323Ssam	sc->sc_authtype = WI_DEFAULT_AUTHTYPE;
44587383Simp
446109323Ssam	bzero(sc->sc_nodename, sizeof(sc->sc_nodename));
447109323Ssam	sc->sc_nodelen = sizeof(WI_DEFAULT_NODENAME) - 1;
448109323Ssam	bcopy(WI_DEFAULT_NODENAME, sc->sc_nodename, sc->sc_nodelen);
44987383Simp
450109323Ssam	bzero(sc->sc_net_name, sizeof(sc->sc_net_name));
451109323Ssam	bcopy(WI_DEFAULT_NETNAME, sc->sc_net_name,
452109323Ssam	    sizeof(WI_DEFAULT_NETNAME) - 1);
45393733Simp
45493733Simp	/*
455109323Ssam	 * Call MI attach routine.
45693733Simp	 */
457109323Ssam	ieee80211_ifattach(ifp);
458117812Ssam	/* override state transition method */
459117812Ssam	sc->sc_newstate = ic->ic_newstate;
460117812Ssam	ic->ic_newstate = wi_newstate;
461116951Ssam	ieee80211_media_init(ifp, wi_media_change, wi_media_status);
462109323Ssam
463109323Ssam	return (0);
46487383Simp}
46587383Simp
466109323Ssamint
467109323Ssamwi_detach(device_t dev)
46846492Swpaul{
469109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
470109323Ssam	struct ifnet *ifp = &sc->sc_ic.ic_if;
471109323Ssam	WI_LOCK_DECL();
47246492Swpaul
473109323Ssam	WI_LOCK(sc);
47446492Swpaul
475109323Ssam	/* check if device was removed */
476109323Ssam	sc->wi_gone = !bus_child_present(dev);
47746492Swpaul
478109323Ssam	wi_stop(ifp, 0);
47946492Swpaul
480109323Ssam	ieee80211_ifdetach(ifp);
481114190Simp	WI_UNLOCK(sc);
482109323Ssam	bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
483109323Ssam	wi_free(dev);
484109323Ssam#if __FreeBSD_version >= 500000
485109323Ssam	mtx_destroy(&sc->sc_mtx);
48693359Simp#endif
487109323Ssam	return (0);
488109323Ssam}
48993359Simp
490109323Ssam#ifdef __NetBSD__
491109323Ssamint
492109323Ssamwi_activate(struct device *self, enum devact act)
493109323Ssam{
494109323Ssam	struct wi_softc *sc = (struct wi_softc *)self;
495109323Ssam	int rv = 0, s;
49693359Simp
497109323Ssam	s = splnet();
498109323Ssam	switch (act) {
499109323Ssam	case DVACT_ACTIVATE:
500109323Ssam		rv = EOPNOTSUPP;
501109323Ssam		break;
50293359Simp
503109323Ssam	case DVACT_DEACTIVATE:
504109323Ssam		if_deactivate(&sc->sc_ic.ic_if);
505109323Ssam		break;
506109323Ssam	}
507109323Ssam	splx(s);
508109323Ssam	return rv;
509109323Ssam}
51093359Simp
511109323Ssamvoid
512109323Ssamwi_power(struct wi_softc *sc, int why)
513109323Ssam{
514109323Ssam	struct ifnet *ifp = &sc->sc_ic.ic_if;
515109323Ssam	int s;
51693359Simp
517109323Ssam	s = splnet();
518109323Ssam	switch (why) {
519109323Ssam	case PWR_SUSPEND:
520109323Ssam	case PWR_STANDBY:
521109323Ssam		wi_stop(ifp, 1);
522109323Ssam		break;
523109323Ssam	case PWR_RESUME:
524109323Ssam		if (ifp->if_flags & IFF_UP) {
525109323Ssam			wi_init(ifp);
526109323Ssam			(void)wi_intr(sc);
52794405Simp		}
528109323Ssam		break;
529109323Ssam	case PWR_SOFTSUSPEND:
530109323Ssam	case PWR_SOFTSTANDBY:
531109323Ssam	case PWR_SOFTRESUME:
532109323Ssam		break;
53393359Simp	}
534109323Ssam	splx(s);
53546492Swpaul}
536109323Ssam#endif /* __NetBSD__ */
53746492Swpaul
538109323Ssamvoid
539109323Ssamwi_shutdown(device_t dev)
54046492Swpaul{
541109323Ssam	struct wi_softc *sc = device_get_softc(dev);
54246492Swpaul
543109323Ssam	wi_stop(&sc->sc_if, 1);
54446492Swpaul}
54546492Swpaul
546109323Ssamvoid
547109323Ssamwi_intr(void *arg)
54846492Swpaul{
549109323Ssam	struct wi_softc *sc = arg;
550109323Ssam	struct ifnet *ifp = &sc->sc_ic.ic_if;
551112363Simp	u_int16_t status;
552109323Ssam	WI_LOCK_DECL();
55346492Swpaul
554109323Ssam	WI_LOCK(sc);
55546492Swpaul
556109323Ssam	if (sc->wi_gone || (ifp->if_flags & IFF_UP) == 0) {
557113327Simp		CSR_WRITE_2(sc, WI_INT_EN, 0);
558109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
559109323Ssam		WI_UNLOCK(sc);
56046492Swpaul		return;
561109323Ssam	}
56246492Swpaul
563112363Simp	/* Disable interrupts. */
564112363Simp	CSR_WRITE_2(sc, WI_INT_EN, 0);
56546492Swpaul
566112363Simp	status = CSR_READ_2(sc, WI_EVENT_STAT);
567112363Simp	if (status & WI_EV_RX)
568112363Simp		wi_rx_intr(sc);
569112363Simp	if (status & WI_EV_ALLOC)
570112363Simp		wi_tx_intr(sc);
571112363Simp	if (status & WI_EV_TX_EXC)
572112363Simp		wi_tx_ex_intr(sc);
573112363Simp	if (status & WI_EV_INFO)
574112363Simp		wi_info_intr(sc);
575112363Simp	if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
576112363Simp	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 &&
577112363Simp	    _IF_QLEN(&ifp->if_snd) != 0)
578112363Simp		wi_start(ifp);
57946492Swpaul
580112363Simp	/* Re-enable interrupts. */
581112363Simp	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
58246492Swpaul
583109323Ssam	WI_UNLOCK(sc);
58446492Swpaul
58546492Swpaul	return;
58646492Swpaul}
58746492Swpaul
588109323Ssamvoid
589109323Ssamwi_init(void *arg)
59046492Swpaul{
591109323Ssam	struct wi_softc *sc = arg;
592109323Ssam	struct ifnet *ifp = &sc->sc_if;
593109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
594109323Ssam	struct wi_joinreq join;
595109323Ssam	int i;
596109323Ssam	int error = 0, wasenabled;
597109323Ssam	struct ifaddr *ifa;
598109323Ssam	struct sockaddr_dl *sdl;
599109323Ssam	WI_LOCK_DECL();
60046492Swpaul
601109323Ssam	WI_LOCK(sc);
60267092Swpaul
603109323Ssam	if (sc->wi_gone) {
604109323Ssam		WI_UNLOCK(sc);
60546492Swpaul		return;
60646492Swpaul	}
60746492Swpaul
608112362Simp	if ((wasenabled = sc->sc_enabled))
609109323Ssam		wi_stop(ifp, 0);
610112362Simp	wi_reset(sc);
61146492Swpaul
612109323Ssam	/* common 802.11 configuration */
613109323Ssam	ic->ic_flags &= ~IEEE80211_F_IBSSON;
614109323Ssam	sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
615109323Ssam	switch (ic->ic_opmode) {
616109323Ssam	case IEEE80211_M_STA:
617109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS);
618109323Ssam		break;
619109323Ssam	case IEEE80211_M_IBSS:
620109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port);
621109323Ssam		ic->ic_flags |= IEEE80211_F_IBSSON;
622109323Ssam		break;
623109323Ssam	case IEEE80211_M_AHDEMO:
624109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
625109323Ssam		break;
626109323Ssam	case IEEE80211_M_HOSTAP:
627109323Ssam		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP);
628109323Ssam		break;
629117855Ssam	case IEEE80211_M_MONITOR:
630117855Ssam		if (sc->sc_firmware_type == WI_LUCENT)
631117855Ssam			wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
632117855Ssam		wi_cmd(sc, WI_CMD_DEBUG | (WI_TEST_MONITOR << 8), 0, 0, 0);
633117855Ssam		break;
63446492Swpaul	}
63546492Swpaul
636109323Ssam	/* Intersil interprets this RID as joining ESS even in IBSS mode */
637109323Ssam	if (sc->sc_firmware_type == WI_LUCENT &&
638109323Ssam	    (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0)
639109323Ssam		wi_write_val(sc, WI_RID_CREATE_IBSS, 1);
640109323Ssam	else
641109323Ssam		wi_write_val(sc, WI_RID_CREATE_IBSS, 0);
642109323Ssam	wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
643109323Ssam	wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid,
644109323Ssam	    ic->ic_des_esslen);
645116951Ssam	wi_write_val(sc, WI_RID_OWN_CHNL,
646116951Ssam		ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
647109323Ssam	wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen);
64846492Swpaul
649109323Ssam	ifa = ifaddr_byindex(ifp->if_index);
650109323Ssam	sdl = (struct sockaddr_dl *) ifa->ifa_addr;
651109323Ssam	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(sdl));
652109323Ssam	wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN);
65375373Salfred
654109323Ssam	wi_write_val(sc, WI_RID_PM_ENABLED,
655109323Ssam	    (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
65646492Swpaul
657109323Ssam	/* not yet common 802.11 configuration */
658109323Ssam	wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen);
659116951Ssam	wi_write_val(sc, WI_RID_RTS_THRESH, ic->ic_rtsthreshold);
660109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
661116951Ssam		wi_write_val(sc, WI_RID_FRAG_THRESH, ic->ic_fragthreshold);
66246492Swpaul
663109323Ssam	/* driver specific 802.11 configuration */
664109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)
665109323Ssam		wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale);
666109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
667109323Ssam		wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode);
668109323Ssam	if (sc->sc_flags & WI_FLAGS_HAS_MOR)
669109323Ssam		wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven);
670109323Ssam	wi_write_txrate(sc);
671109323Ssam	wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen);
67246492Swpaul
673109323Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
674109323Ssam	    sc->sc_firmware_type == WI_INTERSIL) {
675109323Ssam		wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval);
676109323Ssam		wi_write_val(sc, WI_RID_BASIC_RATE, 0x03);   /* 1, 2 */
677109323Ssam		wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */
678109323Ssam		wi_write_val(sc, WI_RID_DTIM_PERIOD, 1);
67946492Swpaul	}
68046492Swpaul
681109323Ssam	/*
682109323Ssam	 * Initialize promisc mode.
683109323Ssam	 *	Being in the Host-AP mode causes a great
684109323Ssam	 *	deal of pain if primisc mode is set.
685109323Ssam	 *	Therefore we avoid confusing the firmware
686109323Ssam	 *	and always reset promisc mode in Host-AP
687109323Ssam	 *	mode.  Host-AP sees all the packets anyway.
688109323Ssam	 */
689109323Ssam	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
690109323Ssam	    (ifp->if_flags & IFF_PROMISC) != 0) {
691109323Ssam		wi_write_val(sc, WI_RID_PROMISC, 1);
692109323Ssam	} else {
693109323Ssam		wi_write_val(sc, WI_RID_PROMISC, 0);
69475373Salfred	}
69546492Swpaul
696109323Ssam	/* Configure WEP. */
697116951Ssam	if (ic->ic_caps & IEEE80211_C_WEP)
698109323Ssam		wi_write_wep(sc);
69967092Swpaul
700109323Ssam	/* Set multicast filter. */
701109323Ssam	wi_write_multi(sc);
70246492Swpaul
703114124Simp	/* Allocate fids for the card */
704109323Ssam	if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
705109323Ssam		sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
706109323Ssam		if (sc->sc_firmware_type == WI_SYMBOL)
707109323Ssam			sc->sc_buflen = 1585;	/* XXX */
708112363Simp		for (i = 0; i < sc->sc_ntxbuf; i++) {
709109323Ssam			error = wi_alloc_fid(sc, sc->sc_buflen,
710109323Ssam			    &sc->sc_txd[i].d_fid);
711109323Ssam			if (error) {
712109323Ssam				device_printf(sc->sc_dev,
713109323Ssam				    "tx buffer allocation failed (error %u)\n",
714109323Ssam				    error);
715109323Ssam				goto out;
716109323Ssam			}
717109323Ssam			sc->sc_txd[i].d_len = 0;
71870073Swpaul		}
71970073Swpaul	}
720109323Ssam	sc->sc_txcur = sc->sc_txnext = 0;
72170073Swpaul
722109323Ssam	/* Enable desired port */
723109323Ssam	wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
72446492Swpaul
725114124Simp	sc->sc_enabled = 1;
726109323Ssam	ifp->if_flags |= IFF_RUNNING;
727109323Ssam	ifp->if_flags &= ~IFF_OACTIVE;
728109323Ssam	if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
729117855Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR ||
730109323Ssam	    ic->ic_opmode == IEEE80211_M_HOSTAP)
731117812Ssam		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
73246492Swpaul
733109323Ssam	/* Enable interrupts */
734109323Ssam	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
73546492Swpaul
736109323Ssam	if (!wasenabled &&
737109323Ssam	    ic->ic_opmode == IEEE80211_M_HOSTAP &&
738109323Ssam	    sc->sc_firmware_type == WI_INTERSIL) {
739109323Ssam		/* XXX: some card need to be re-enabled for hostap */
740109323Ssam		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
741109323Ssam		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
74275149Simp	}
74394397Simp
744109323Ssam	if (ic->ic_opmode == IEEE80211_M_STA &&
745109323Ssam	    ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
746116951Ssam	    ic->ic_des_chan != IEEE80211_CHAN_ANYC)) {
747109323Ssam		memset(&join, 0, sizeof(join));
748109323Ssam		if (ic->ic_flags & IEEE80211_F_DESBSSID)
749109323Ssam			IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
750116951Ssam		if (ic->ic_des_chan != IEEE80211_CHAN_ANYC)
751116951Ssam			join.wi_chan = htole16(
752116951Ssam				ieee80211_chan2ieee(ic, ic->ic_des_chan));
753109323Ssam		/* Lucent firmware does not support the JOIN RID. */
754109323Ssam		if (sc->sc_firmware_type != WI_LUCENT)
755109323Ssam			wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
75694397Simp	}
75775373Salfred
758109323Ssam	WI_UNLOCK(sc);
75946492Swpaul	return;
760109323Ssamout:
761109323Ssam	if (error) {
762109323Ssam		if_printf(ifp, "interface not running\n");
763109323Ssam		wi_stop(ifp, 0);
76470073Swpaul	}
765110224Simp	WI_UNLOCK(sc);
766109323Ssam	DPRINTF(("wi_init: return %d\n", error));
767109323Ssam	return;
76846492Swpaul}
76946492Swpaul
770109323Ssamvoid
771109323Ssamwi_stop(struct ifnet *ifp, int disable)
77246492Swpaul{
773117812Ssam	struct ieee80211com *ic = (struct ieee80211com *) ifp;
774109323Ssam	struct wi_softc *sc = ifp->if_softc;
775109323Ssam	WI_LOCK_DECL();
77646492Swpaul
777109323Ssam	WI_LOCK(sc);
77874998Swpaul
779117812Ssam	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
780109323Ssam	if (sc->sc_enabled && !sc->wi_gone) {
781109323Ssam		CSR_WRITE_2(sc, WI_INT_EN, 0);
782109323Ssam		wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
783109323Ssam		if (disable) {
784109323Ssam#ifdef __NetBSD__
785109323Ssam			if (sc->sc_disable)
786109323Ssam				(*sc->sc_disable)(sc);
787109323Ssam#endif
788116951Ssam			sc->sc_enabled = 0;
78970073Swpaul		}
79070073Swpaul	}
79170073Swpaul
792109323Ssam	sc->sc_tx_timer = 0;
793109323Ssam	sc->sc_scan_timer = 0;
794109323Ssam	sc->sc_syn_timer = 0;
795109323Ssam	sc->sc_false_syns = 0;
796109323Ssam	sc->sc_naps = 0;
797109323Ssam	ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
798109323Ssam	ifp->if_timer = 0;
79946492Swpaul
800109323Ssam	WI_UNLOCK(sc);
80146492Swpaul}
80246492Swpaul
803109323Ssamstatic void
804109323Ssamwi_start(struct ifnet *ifp)
80546492Swpaul{
806109323Ssam	struct wi_softc	*sc = ifp->if_softc;
807109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
808119150Ssam	struct ieee80211_node *ni;
809109323Ssam	struct ieee80211_frame *wh;
810109323Ssam	struct mbuf *m0;
811109323Ssam	struct wi_frame frmhdr;
812119150Ssam	int cur, fid, off, error;
813109323Ssam	WI_LOCK_DECL();
81446492Swpaul
815109323Ssam	WI_LOCK(sc);
81646492Swpaul
817109323Ssam	if (sc->wi_gone) {
818109323Ssam		WI_UNLOCK(sc);
819109323Ssam		return;
82046492Swpaul	}
821109323Ssam	if (sc->sc_flags & WI_FLAGS_OUTRANGE) {
822109323Ssam		WI_UNLOCK(sc);
823109323Ssam		return;
82475373Salfred	}
82546492Swpaul
826109323Ssam	memset(&frmhdr, 0, sizeof(frmhdr));
827109323Ssam	cur = sc->sc_txnext;
828109323Ssam	for (;;) {
829109323Ssam		IF_POLL(&ic->ic_mgtq, m0);
830109323Ssam		if (m0 != NULL) {
831109323Ssam			if (sc->sc_txd[cur].d_len != 0) {
832109323Ssam				ifp->if_flags |= IFF_OACTIVE;
833109323Ssam				break;
834109323Ssam			}
835109323Ssam			IF_DEQUEUE(&ic->ic_mgtq, m0);
836119150Ssam			/*
837119150Ssam			 * Hack!  The referenced node pointer is in the
838119150Ssam			 * rcvif field of the packet header.  This is
839119150Ssam			 * placed there by ieee80211_mgmt_output because
840119150Ssam			 * we need to hold the reference with the frame
841119150Ssam			 * and there's no other way (other than packet
842119150Ssam			 * tags which we consider too expensive to use)
843119150Ssam			 * to pass it along.
844119150Ssam			 */
845119150Ssam			ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
846119150Ssam			m0->m_pkthdr.rcvif = NULL;
847119150Ssam
848109323Ssam			m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
849109323Ssam			    (caddr_t)&frmhdr.wi_ehdr);
850109323Ssam			frmhdr.wi_ehdr.ether_type = 0;
851109323Ssam                        wh = mtod(m0, struct ieee80211_frame *);
852109323Ssam		} else {
853109323Ssam			if (ic->ic_state != IEEE80211_S_RUN)
854109323Ssam				break;
855109323Ssam			IFQ_POLL(&ifp->if_snd, m0);
856109323Ssam			if (m0 == NULL)
857109323Ssam				break;
858109323Ssam			if (sc->sc_txd[cur].d_len != 0) {
859109323Ssam				ifp->if_flags |= IFF_OACTIVE;
860109323Ssam				break;
861109323Ssam			}
862109323Ssam			IFQ_DEQUEUE(&ifp->if_snd, m0);
863109323Ssam			ifp->if_opackets++;
864109323Ssam			m_copydata(m0, 0, ETHER_HDR_LEN,
865109323Ssam			    (caddr_t)&frmhdr.wi_ehdr);
866109323Ssam#if NBPFILTER > 0
867109323Ssam			BPF_MTAP(ifp, m0);
868109323Ssam#endif
86946492Swpaul
870119150Ssam			m0 = ieee80211_encap(ifp, m0, &ni);
871119150Ssam			if (m0 == NULL) {
872109323Ssam				ifp->if_oerrors++;
873109323Ssam				continue;
874109323Ssam			}
875109323Ssam                        wh = mtod(m0, struct ieee80211_frame *);
876109323Ssam			if (ic->ic_flags & IEEE80211_F_WEPON)
877109323Ssam				wh->i_fc[1] |= IEEE80211_FC1_WEP;
87846492Swpaul
879109323Ssam		}
880109323Ssam#if NBPFILTER > 0
881109323Ssam		if (ic->ic_rawbpf)
882109323Ssam			bpf_mtap(ic->ic_rawbpf, m0);
883109323Ssam#endif
884109323Ssam		frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
885109323Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
886109323Ssam		    (wh->i_fc[1] & IEEE80211_FC1_WEP)) {
887109323Ssam			if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) {
888109323Ssam				ifp->if_oerrors++;
889119150Ssam				if (ni && ni != ic->ic_bss)
890119150Ssam					ieee80211_free_node(ic, ni);
891109323Ssam				continue;
892109323Ssam			}
893109323Ssam			frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
894109323Ssam		}
895109323Ssam		m_copydata(m0, 0, sizeof(struct ieee80211_frame),
896109323Ssam		    (caddr_t)&frmhdr.wi_whdr);
897109323Ssam		m_adj(m0, sizeof(struct ieee80211_frame));
898109323Ssam		frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
899109323Ssam#if NBPFILTER > 0
900109323Ssam		if (sc->sc_drvbpf) {
901109323Ssam			struct mbuf *mb;
90246492Swpaul
903111119Simp			MGETHDR(mb, M_DONTWAIT, m0->m_type);
904109323Ssam			if (mb != NULL) {
905111119Simp				(void) m_dup_pkthdr(mb, m0, M_DONTWAIT);
906109323Ssam				mb->m_next = m0;
907109323Ssam				mb->m_data = (caddr_t)&frmhdr;
908109323Ssam				mb->m_len = sizeof(frmhdr);
909109323Ssam				mb->m_pkthdr.len += mb->m_len;
910109323Ssam				bpf_mtap(sc->sc_drvbpf, mb);
911109323Ssam				m_free(mb);
912109323Ssam			}
913109323Ssam		}
91446492Swpaul#endif
915109323Ssam		if (IFF_DUMPPKTS(ifp))
916116951Ssam			wi_dump_pkt(&frmhdr, NULL, -1);
917109323Ssam		fid = sc->sc_txd[cur].d_fid;
918109323Ssam		off = sizeof(frmhdr);
919119150Ssam		error = wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0
920119150Ssam		     || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
921119150Ssam		m_freem(m0);
922119150Ssam		if (ni && ni != ic->ic_bss)
923119150Ssam			ieee80211_free_node(ic, ni);
924119150Ssam		if (error) {
925109323Ssam			ifp->if_oerrors++;
926109323Ssam			continue;
927109323Ssam		}
928109323Ssam		sc->sc_txd[cur].d_len = off;
929109323Ssam		if (sc->sc_txcur == cur) {
930109323Ssam			if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
931109323Ssam				if_printf(ifp, "xmit failed\n");
932109323Ssam				sc->sc_txd[cur].d_len = 0;
933109323Ssam				continue;
934109323Ssam			}
935109323Ssam			sc->sc_tx_timer = 5;
936109323Ssam			ifp->if_timer = 1;
937109323Ssam		}
938112363Simp		sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
93974838Salfred	}
94046492Swpaul
941109323Ssam	WI_UNLOCK(sc);
94246492Swpaul}
94346492Swpaul
94488546Salfredstatic int
945109323Ssamwi_reset(struct wi_softc *sc)
94646492Swpaul{
947114124Simp	struct ieee80211com *ic = &sc->sc_ic;
948114124Simp	struct ifnet *ifp = &ic->ic_if;
949114124Simp#define WI_INIT_TRIES 3
950114124Simp	int i;
951114138Simp	int error = 0;
952114124Simp	int tries;
953114124Simp
954114124Simp	/* Symbol firmware cannot be initialized more than once */
955116276Simp	if (sc->sc_firmware_type != WI_INTERSIL && sc->sc_reset)
956112362Simp		return (0);
957114124Simp	if (sc->sc_firmware_type == WI_SYMBOL)
958114124Simp		tries = 1;
959114124Simp	else
960114124Simp		tries = WI_INIT_TRIES;
961112362Simp
962114124Simp	for (i = 0; i < tries; i++) {
963109323Ssam		if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
96446492Swpaul			break;
965109323Ssam		DELAY(WI_DELAY * 1000);
96646492Swpaul	}
967114124Simp	sc->sc_reset = 1;
96846492Swpaul
969114124Simp	if (i == tries) {
970114124Simp		if_printf(ifp, "init failed\n");
971114124Simp		return (error);
97275373Salfred	}
97346492Swpaul
974109323Ssam	CSR_WRITE_2(sc, WI_INT_EN, 0);
975114124Simp	CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
97646492Swpaul
977109323Ssam	/* Calibrate timer. */
978114124Simp	wi_write_val(sc, WI_RID_TICK_TIME, 8);
979114124Simp
980114124Simp	return (0);
981109323Ssam#undef WI_INIT_TRIES
98246492Swpaul}
98346492Swpaul
98488546Salfredstatic void
985109323Ssamwi_watchdog(struct ifnet *ifp)
98646492Swpaul{
987109323Ssam	struct wi_softc	*sc = ifp->if_softc;
98846492Swpaul
989109323Ssam	ifp->if_timer = 0;
990109323Ssam	if (!sc->sc_enabled)
991109323Ssam		return;
99246492Swpaul
993109323Ssam	if (sc->sc_tx_timer) {
994109323Ssam		if (--sc->sc_tx_timer == 0) {
995109323Ssam			if_printf(ifp, "device timeout\n");
996109323Ssam			ifp->if_oerrors++;
997109323Ssam			wi_init(ifp->if_softc);
998109323Ssam			return;
999109323Ssam		}
1000109323Ssam		ifp->if_timer = 1;
100146492Swpaul	}
100246492Swpaul
1003109323Ssam	if (sc->sc_scan_timer) {
1004109323Ssam		if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
1005109323Ssam		    sc->sc_firmware_type == WI_INTERSIL) {
1006109323Ssam			DPRINTF(("wi_watchdog: inquire scan\n"));
1007109323Ssam			wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
100846492Swpaul		}
1009109323Ssam		if (sc->sc_scan_timer)
1010109323Ssam			ifp->if_timer = 1;
101146492Swpaul	}
101246492Swpaul
1013109323Ssam	if (sc->sc_syn_timer) {
1014109323Ssam		if (--sc->sc_syn_timer == 0) {
1015117812Ssam			struct ieee80211com *ic = (struct ieee80211com *) ifp;
1016109323Ssam			DPRINTF2(("wi_watchdog: %d false syns\n",
1017109323Ssam			    sc->sc_false_syns));
1018109323Ssam			sc->sc_false_syns = 0;
1019117812Ssam			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1020109323Ssam			sc->sc_syn_timer = 5;
1021109323Ssam		}
1022109323Ssam		ifp->if_timer = 1;
102346492Swpaul	}
102446492Swpaul
1025109323Ssam	/* TODO: rate control */
1026109323Ssam	ieee80211_watchdog(ifp);
102746492Swpaul}
102846492Swpaul
102988546Salfredstatic int
1030109323Ssamwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
103146492Swpaul{
1032109323Ssam	struct wi_softc *sc = ifp->if_softc;
1033109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1034109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
1035109323Ssam	struct ieee80211req *ireq;
1036109323Ssam	u_int8_t nodename[IEEE80211_NWID_LEN];
1037109323Ssam	int error = 0;
103895534Simp#if __FreeBSD_version >= 500000
1039109323Ssam	struct thread *td = curthread;
104095534Simp#else
1041109323Ssam	struct proc *td = curproc;		/* Little white lie */
104295534Simp#endif
1043109323Ssam	struct wi_req wreq;
1044109323Ssam	WI_LOCK_DECL();
104546492Swpaul
1046109323Ssam	WI_LOCK(sc);
104746492Swpaul
104861818Sroberto	if (sc->wi_gone) {
104961818Sroberto		error = ENODEV;
105061818Sroberto		goto out;
105161818Sroberto	}
105246492Swpaul
1053109323Ssam	switch (cmd) {
105446492Swpaul	case SIOCSIFFLAGS:
1055100876Simp		/*
1056101139Simp		 * Can't do promisc and hostap at the same time.  If all that's
1057101139Simp		 * changing is the promisc flag, try to short-circuit a call to
1058101139Simp		 * wi_init() by just setting PROMISC in the hardware.
1059100876Simp		 */
106046492Swpaul		if (ifp->if_flags & IFF_UP) {
1061109323Ssam			if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1062101139Simp			    ifp->if_flags & IFF_RUNNING) {
1063101139Simp				if (ifp->if_flags & IFF_PROMISC &&
1064109323Ssam				    !(sc->sc_if_flags & IFF_PROMISC)) {
1065109323Ssam					wi_write_val(sc, WI_RID_PROMISC, 1);
1066101139Simp				} else if (!(ifp->if_flags & IFF_PROMISC) &&
1067109323Ssam				    sc->sc_if_flags & IFF_PROMISC) {
1068109323Ssam					wi_write_val(sc, WI_RID_PROMISC, 0);
1069101139Simp				} else {
1070101139Simp					wi_init(sc);
1071101139Simp				}
1072100876Simp			} else {
107346492Swpaul				wi_init(sc);
1074100876Simp			}
107546492Swpaul		} else {
107646492Swpaul			if (ifp->if_flags & IFF_RUNNING) {
1077109323Ssam				wi_stop(ifp, 0);
107846492Swpaul			}
107946492Swpaul		}
1080109323Ssam		sc->sc_if_flags = ifp->if_flags;
108146492Swpaul		error = 0;
108246492Swpaul		break;
108346492Swpaul	case SIOCADDMULTI:
108446492Swpaul	case SIOCDELMULTI:
1085109323Ssam		error = wi_write_multi(sc);
108646492Swpaul		break;
1087109323Ssam	case SIOCGIFGENERIC:
1088109323Ssam		error = wi_get_cfg(ifp, cmd, data);
108946492Swpaul		break;
1090109323Ssam	case SIOCSIFGENERIC:
1091109323Ssam		error = suser(td);
109246492Swpaul		if (error)
109346492Swpaul			break;
1094109323Ssam		error = wi_set_cfg(ifp, cmd, data);
109546492Swpaul		break;
109693359Simp	case SIOCGPRISM2DEBUG:
109793359Simp		error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
109893359Simp		if (error)
109993359Simp			break;
110093733Simp		if (!(ifp->if_flags & IFF_RUNNING) ||
110193733Simp		    sc->sc_firmware_type == WI_LUCENT) {
110293359Simp			error = EIO;
110393359Simp			break;
110493359Simp		}
110593359Simp		error = wi_get_debug(sc, &wreq);
110693359Simp		if (error == 0)
110793359Simp			error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
110893359Simp		break;
110993359Simp	case SIOCSPRISM2DEBUG:
111093593Sjhb		if ((error = suser(td)))
111193359Simp			goto out;
111293359Simp		error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
111393359Simp		if (error)
111493359Simp			break;
111593359Simp		error = wi_set_debug(sc, &wreq);
111693359Simp		break;
111777217Sphk	case SIOCG80211:
1118109323Ssam		ireq = (struct ieee80211req *) data;
1119109323Ssam		switch (ireq->i_type) {
112077217Sphk		case IEEE80211_IOC_STATIONNAME:
1121109323Ssam			ireq->i_len = sc->sc_nodelen + 1;
1122109323Ssam			error = copyout(sc->sc_nodename, ireq->i_data,
1123109323Ssam					ireq->i_len);
112477217Sphk			break;
1125109323Ssam		default:
1126109323Ssam			error = ieee80211_ioctl(ifp, cmd, data);
112777217Sphk			break;
112877217Sphk		}
112977217Sphk		break;
113077217Sphk	case SIOCS80211:
1131109323Ssam		error = suser(td);
1132109323Ssam		if (error)
1133109323Ssam			break;
1134109323Ssam		ireq = (struct ieee80211req *) data;
1135109323Ssam		switch (ireq->i_type) {
1136109323Ssam		case IEEE80211_IOC_STATIONNAME:
113777217Sphk			if (ireq->i_val != 0 ||
113877217Sphk			    ireq->i_len > IEEE80211_NWID_LEN) {
113977217Sphk				error = EINVAL;
114077217Sphk				break;
114177217Sphk			}
1142109323Ssam			memset(nodename, 0, IEEE80211_NWID_LEN);
1143109323Ssam			error = copyin(ireq->i_data, nodename, ireq->i_len);
1144109323Ssam			if (error)
114577217Sphk				break;
1146109323Ssam			if (sc->sc_enabled) {
1147109323Ssam				error = wi_write_ssid(sc, WI_RID_NODENAME,
1148109323Ssam					nodename, ireq->i_len);
1149109323Ssam				if (error)
1150109323Ssam					break;
115177217Sphk			}
1152109323Ssam			memcpy(sc->sc_nodename, nodename, IEEE80211_NWID_LEN);
1153109323Ssam			sc->sc_nodelen = ireq->i_len;
115477217Sphk			break;
115577217Sphk		default:
1156109323Ssam			error = ieee80211_ioctl(ifp, cmd, data);
115777217Sphk			break;
115877217Sphk		}
115977217Sphk		break;
116046492Swpaul	default:
1161109323Ssam		error = ieee80211_ioctl(ifp, cmd, data);
116246492Swpaul		break;
116346492Swpaul	}
1164109323Ssam	if (error == ENETRESET) {
1165109323Ssam		if (sc->sc_enabled)
1166116951Ssam			wi_init(sc);	/* XXX no error return */
1167109323Ssam		error = 0;
1168109323Ssam	}
116961818Srobertoout:
1170109323Ssam	WI_UNLOCK(sc);
117146492Swpaul
1172109323Ssam	return (error);
117346492Swpaul}
117446492Swpaul
1175109323Ssamstatic int
1176109323Ssamwi_media_change(struct ifnet *ifp)
117746492Swpaul{
1178109323Ssam	struct wi_softc *sc = ifp->if_softc;
1179116951Ssam	int error;
118046492Swpaul
1181116951Ssam	error = ieee80211_media_change(ifp);
1182109323Ssam	if (error == ENETRESET) {
1183109323Ssam		if (sc->sc_enabled)
1184116951Ssam			wi_init(sc);	/* XXX no error return */
1185109323Ssam		error = 0;
1186109323Ssam	}
1187109323Ssam	return error;
1188109323Ssam}
118946492Swpaul
1190109323Ssamstatic void
1191109323Ssamwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1192109323Ssam{
1193109323Ssam	struct wi_softc *sc = ifp->if_softc;
1194109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1195109323Ssam	u_int16_t val;
1196109323Ssam	int rate, len;
119746492Swpaul
1198109323Ssam	if (sc->wi_gone || !sc->sc_enabled) {
1199109323Ssam		imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
1200109323Ssam		imr->ifm_status = 0;
1201109323Ssam		return;
1202109323Ssam	}
120346492Swpaul
1204109323Ssam	imr->ifm_status = IFM_AVALID;
1205109323Ssam	imr->ifm_active = IFM_IEEE80211;
1206109323Ssam	if (ic->ic_state == IEEE80211_S_RUN &&
1207109323Ssam	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
1208109323Ssam		imr->ifm_status |= IFM_ACTIVE;
1209109323Ssam	len = sizeof(val);
1210109323Ssam	if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) != 0)
1211109323Ssam		rate = 0;
1212109323Ssam	else {
1213109323Ssam		/* convert to 802.11 rate */
1214109323Ssam		rate = val * 2;
1215109323Ssam		if (sc->sc_firmware_type == WI_LUCENT) {
1216112501Simp			if (rate == 4 * 2)
1217109323Ssam				rate = 11;	/* 5.5Mbps */
1218112501Simp			else if (rate == 5 * 2)
1219112501Simp				rate = 22;	/* 11Mbps */
1220109323Ssam		} else {
1221109323Ssam			if (rate == 4*2)
1222109323Ssam				rate = 11;	/* 5.5Mbps */
1223109323Ssam			else if (rate == 8*2)
1224109323Ssam				rate = 22;	/* 11Mbps */
1225109323Ssam		}
1226109323Ssam	}
1227116951Ssam	imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
1228109323Ssam	switch (ic->ic_opmode) {
1229109323Ssam	case IEEE80211_M_STA:
1230109323Ssam		break;
1231109323Ssam	case IEEE80211_M_IBSS:
1232109323Ssam		imr->ifm_active |= IFM_IEEE80211_ADHOC;
1233109323Ssam		break;
1234109323Ssam	case IEEE80211_M_AHDEMO:
1235109323Ssam		imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1236109323Ssam		break;
1237109323Ssam	case IEEE80211_M_HOSTAP:
1238109323Ssam		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1239109323Ssam		break;
1240117855Ssam	case IEEE80211_M_MONITOR:
1241117855Ssam		imr->ifm_active |= IFM_IEEE80211_MONITOR;
1242117855Ssam		break;
1243109323Ssam	}
1244109323Ssam}
124546492Swpaul
1246109323Ssamstatic void
1247109323Ssamwi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1248109323Ssam{
1249109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1250116951Ssam	struct ieee80211_node *ni = ic->ic_bss;
1251109323Ssam	struct ifnet *ifp = &ic->ic_if;
125298440Simp
1253109323Ssam	if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1254109323Ssam		return;
125546492Swpaul
1256109323Ssam	DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid)));
1257109323Ssam	DPRINTF(("%s ?\n", ether_sprintf(new_bssid)));
125846492Swpaul
1259109323Ssam	/* In promiscuous mode, the BSSID field is not a reliable
1260109323Ssam	 * indicator of the firmware's BSSID. Damp spurious
1261109323Ssam	 * change-of-BSSID indications.
1262109323Ssam	 */
1263109323Ssam	if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1264109323Ssam	    sc->sc_false_syns >= WI_MAX_FALSE_SYNS)
1265109323Ssam		return;
126646492Swpaul
1267117812Ssam	ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1268109323Ssam}
126946492Swpaul
1270109323Ssamstatic void
1271116898Ssamwi_rx_monitor(struct wi_softc *sc, int fid)
1272116898Ssam{
1273116898Ssam	struct ieee80211com *ic = &sc->sc_ic;
1274116898Ssam	struct ifnet *ifp = &ic->ic_if;
1275116898Ssam	struct wi_frame *rx_frame;
1276116898Ssam	struct mbuf *m;
1277116898Ssam	int datlen, hdrlen;
1278116898Ssam
1279116898Ssam	/* first allocate mbuf for packet storage */
1280116898Ssam	m = m_getcl(M_DONTWAIT, MT_DATA, 0);
1281116898Ssam	if (m == NULL) {
1282116898Ssam		ifp->if_ierrors++;
1283116898Ssam		return;
1284116898Ssam	}
1285116898Ssam
1286116898Ssam	m->m_pkthdr.rcvif = ifp;
1287116898Ssam
1288116898Ssam	/* now read wi_frame first so we know how much data to read */
1289116898Ssam	if (wi_read_bap(sc, fid, 0, mtod(m, caddr_t), sizeof(*rx_frame))) {
1290116898Ssam		ifp->if_ierrors++;
1291116898Ssam		goto done;
1292116898Ssam	}
1293116898Ssam
1294116898Ssam	rx_frame = mtod(m, struct wi_frame *);
1295116898Ssam
1296116898Ssam	switch ((rx_frame->wi_status & WI_STAT_MAC_PORT) >> 8) {
1297116898Ssam	case 7:
1298116898Ssam		switch (rx_frame->wi_whdr.i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1299116898Ssam		case IEEE80211_FC0_TYPE_DATA:
1300116898Ssam			hdrlen = WI_DATA_HDRLEN;
1301116898Ssam			datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1302116898Ssam			break;
1303116898Ssam		case IEEE80211_FC0_TYPE_MGT:
1304116898Ssam			hdrlen = WI_MGMT_HDRLEN;
1305116898Ssam			datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1306116898Ssam			break;
1307116898Ssam		case IEEE80211_FC0_TYPE_CTL:
1308116898Ssam			/*
1309116898Ssam			 * prism2 cards don't pass control packets
1310116898Ssam			 * down properly or consistently, so we'll only
1311116898Ssam			 * pass down the header.
1312116898Ssam			 */
1313116898Ssam			hdrlen = WI_CTL_HDRLEN;
1314116898Ssam			datlen = 0;
1315116898Ssam			break;
1316116898Ssam		default:
1317116898Ssam			if_printf(ifp, "received packet of unknown type "
1318116898Ssam				"on port 7\n");
1319116898Ssam			ifp->if_ierrors++;
1320116898Ssam			goto done;
1321116898Ssam		}
1322116898Ssam		break;
1323116898Ssam	case 0:
1324116898Ssam		hdrlen = WI_DATA_HDRLEN;
1325116898Ssam		datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1326116898Ssam		break;
1327116898Ssam	default:
1328116898Ssam		if_printf(ifp, "received packet on invalid "
1329116898Ssam		    "port (wi_status=0x%x)\n", rx_frame->wi_status);
1330116898Ssam		ifp->if_ierrors++;
1331116898Ssam		goto done;
1332116898Ssam	}
1333116898Ssam
1334116898Ssam	if (hdrlen + datlen + 2 > MCLBYTES) {
1335116898Ssam		if_printf(ifp, "oversized packet received "
1336116898Ssam		    "(wi_dat_len=%d, wi_status=0x%x)\n",
1337116898Ssam		    datlen, rx_frame->wi_status);
1338116898Ssam		ifp->if_ierrors++;
1339116898Ssam		goto done;
1340116898Ssam	}
1341116898Ssam
1342116898Ssam	if (wi_read_bap(sc, fid, hdrlen, mtod(m, caddr_t) + hdrlen,
1343116898Ssam	    datlen + 2) == 0) {
1344116898Ssam		m->m_pkthdr.len = m->m_len = hdrlen + datlen;
1345116898Ssam		ifp->if_ipackets++;
1346116898Ssam		BPF_MTAP(ifp, m);	/* Handle BPF listeners. */
1347116898Ssam	} else
1348116898Ssam		ifp->if_ierrors++;
1349116898Ssamdone:
1350116898Ssam	m_freem(m);
1351116898Ssam}
1352116898Ssam
1353116898Ssamstatic void
1354109323Ssamwi_rx_intr(struct wi_softc *sc)
1355109323Ssam{
1356109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1357109323Ssam	struct ifnet *ifp = &ic->ic_if;
1358109323Ssam	struct wi_frame frmhdr;
1359109323Ssam	struct mbuf *m;
1360109323Ssam	struct ieee80211_frame *wh;
1361119150Ssam	struct ieee80211_node *ni;
1362109323Ssam	int fid, len, off, rssi;
1363109323Ssam	u_int8_t dir;
1364109323Ssam	u_int16_t status;
1365109323Ssam	u_int32_t rstamp;
136646611Swpaul
1367109323Ssam	fid = CSR_READ_2(sc, WI_RX_FID);
136846611Swpaul
1369116898Ssam	if (sc->wi_debug.wi_monitor) {
1370116898Ssam		/*
1371116898Ssam		 * If we are in monitor mode just
1372116898Ssam		 * read the data from the device.
1373116898Ssam		 */
1374116898Ssam		wi_rx_monitor(sc, fid);
1375116898Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1376116898Ssam		return;
1377116898Ssam	}
1378116898Ssam
1379109323Ssam	/* First read in the frame header */
1380109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1381109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1382109323Ssam		ifp->if_ierrors++;
1383109323Ssam		DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1384109323Ssam		return;
1385109323Ssam	}
138691695Simp
1387109323Ssam	if (IFF_DUMPPKTS(ifp))
1388109323Ssam		wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal);
138946492Swpaul
1390109323Ssam	/*
1391109323Ssam	 * Drop undecryptable or packets with receive errors here
1392109323Ssam	 */
1393109323Ssam	status = le16toh(frmhdr.wi_status);
1394109323Ssam	if (status & WI_STAT_ERRSTAT) {
1395109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1396109323Ssam		ifp->if_ierrors++;
1397109323Ssam		DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1398109323Ssam		return;
1399109323Ssam	}
1400109323Ssam	rssi = frmhdr.wi_rx_signal;
1401109323Ssam	rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1402109323Ssam	    le16toh(frmhdr.wi_rx_tstamp1);
140346492Swpaul
1404109323Ssam	len = le16toh(frmhdr.wi_dat_len);
1405109323Ssam	off = ALIGN(sizeof(struct ieee80211_frame));
140646563Swpaul
1407117855Ssam	/*
1408117855Ssam	 * Sometimes the PRISM2.x returns bogusly large frames. Except
1409117855Ssam	 * in monitor mode, just throw them away.
1410117855Ssam	 */
1411117855Ssam	if (off + len > MCLBYTES) {
1412117855Ssam		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1413117855Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1414117855Ssam			ifp->if_ierrors++;
1415117855Ssam			DPRINTF(("wi_rx_intr: oversized packet\n"));
1416117855Ssam			return;
1417117855Ssam		} else
1418117855Ssam			len = 0;
1419117855Ssam	}
1420117855Ssam
1421111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
1422109323Ssam	if (m == NULL) {
1423109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1424109323Ssam		ifp->if_ierrors++;
1425109323Ssam		DPRINTF(("wi_rx_intr: MGET failed\n"));
1426109323Ssam		return;
1427109323Ssam	}
1428109323Ssam	if (off + len > MHLEN) {
1429111119Simp		MCLGET(m, M_DONTWAIT);
1430109323Ssam		if ((m->m_flags & M_EXT) == 0) {
1431109323Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1432109323Ssam			m_freem(m);
1433109323Ssam			ifp->if_ierrors++;
1434109323Ssam			DPRINTF(("wi_rx_intr: MCLGET failed\n"));
1435109323Ssam			return;
1436109323Ssam		}
1437109323Ssam	}
143846492Swpaul
1439109323Ssam	m->m_data += off - sizeof(struct ieee80211_frame);
1440109323Ssam	memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1441109323Ssam	wi_read_bap(sc, fid, sizeof(frmhdr),
1442109323Ssam	    m->m_data + sizeof(struct ieee80211_frame), len);
1443109323Ssam	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1444109323Ssam	m->m_pkthdr.rcvif = ifp;
144594405Simp
1446109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
144746492Swpaul
1448109323Ssam#if NBPFILTER > 0
1449109323Ssam	if (sc->sc_drvbpf) {
1450109323Ssam		struct mbuf *mb;
1451100876Simp
1452111119Simp		MGETHDR(mb, M_DONTWAIT, m->m_type);
1453109323Ssam		if (mb != NULL) {
1454111119Simp			(void) m_dup_pkthdr(mb, m, M_DONTWAIT);
1455109323Ssam			mb->m_next = m;
1456109323Ssam			mb->m_data = (caddr_t)&frmhdr;
1457109323Ssam			mb->m_len = sizeof(frmhdr);
1458109323Ssam			mb->m_pkthdr.len += mb->m_len;
1459109323Ssam			bpf_mtap(sc->sc_drvbpf, mb);
1460109323Ssam			m_free(mb);
146191695Simp		}
146256965Swpaul	}
1463109323Ssam#endif
1464109323Ssam	wh = mtod(m, struct ieee80211_frame *);
1465109323Ssam	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1466109323Ssam		/*
1467109323Ssam		 * WEP is decrypted by hardware. Clear WEP bit
1468109323Ssam		 * header for ieee80211_input().
1469109323Ssam		 */
1470109323Ssam		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1471109323Ssam	}
147256965Swpaul
1473109323Ssam	/* synchronize driver's BSSID with firmware's BSSID */
1474109323Ssam	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1475109323Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1476109323Ssam		wi_sync_bssid(sc, wh->i_addr3);
147746492Swpaul
1478119150Ssam	/*
1479119150Ssam	 * Locate the node for sender, track state, and
1480119150Ssam	 * then pass this node (referenced) up to the 802.11
1481119150Ssam	 * layer for its use.  We are required to pass
1482119150Ssam	 * something so we fallback to ic_bss when this frame
1483119150Ssam	 * is from an unknown sender.
1484119150Ssam	 */
1485119150Ssam	if (ic->ic_opmode != IEEE80211_M_STA) {
1486119150Ssam		ni = ieee80211_find_node(ic, wh->i_addr2);
1487119150Ssam		if (ni == NULL)
1488119150Ssam			ni = ieee80211_ref_node(ic->ic_bss);
1489119150Ssam	} else
1490119150Ssam		ni = ieee80211_ref_node(ic->ic_bss);
1491119150Ssam	/*
1492119150Ssam	 * Send frame up for processing.
1493119150Ssam	 */
1494119150Ssam	ieee80211_input(ifp, m, ni, rssi, rstamp);
1495119150Ssam	/*
1496119150Ssam	 * The frame may have caused the node to be marked for
1497119150Ssam	 * reclamation (e.g. in response to a DEAUTH message)
1498119150Ssam	 * so use free_node here instead of unref_node.
1499119150Ssam	 */
1500119150Ssam	if (ni == ic->ic_bss)
1501119150Ssam		ieee80211_unref_node(&ni);
1502119150Ssam	else
1503119150Ssam		ieee80211_free_node(ic, ni);
1504109323Ssam}
150546492Swpaul
1506109323Ssamstatic void
1507109323Ssamwi_tx_ex_intr(struct wi_softc *sc)
1508109323Ssam{
1509109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1510109323Ssam	struct ifnet *ifp = &ic->ic_if;
1511109323Ssam	struct wi_frame frmhdr;
1512109323Ssam	int fid;
151346492Swpaul
1514109323Ssam	fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1515109323Ssam	/* Read in the frame header */
1516109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1517109323Ssam		u_int16_t status = le16toh(frmhdr.wi_status);
151846492Swpaul
1519109323Ssam		/*
1520109323Ssam		 * Spontaneous station disconnects appear as xmit
1521109323Ssam		 * errors.  Don't announce them and/or count them
1522109323Ssam		 * as an output error.
1523109323Ssam		 */
1524109323Ssam		if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1525109323Ssam			if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1526109323Ssam				if_printf(ifp, "tx failed");
1527109323Ssam				if (status & WI_TXSTAT_RET_ERR)
1528109323Ssam					printf(", retry limit exceeded");
1529109323Ssam				if (status & WI_TXSTAT_AGED_ERR)
1530109323Ssam					printf(", max transmit lifetime exceeded");
1531109323Ssam				if (status & WI_TXSTAT_DISCONNECT)
1532109323Ssam					printf(", port disconnected");
1533109323Ssam				if (status & WI_TXSTAT_FORM_ERR)
1534109323Ssam					printf(", invalid format (data len %u src %6D)",
1535109323Ssam						le16toh(frmhdr.wi_dat_len),
1536109323Ssam						frmhdr.wi_ehdr.ether_shost, ":");
1537109323Ssam				if (status & ~0xf)
1538109323Ssam					printf(", status=0x%x", status);
1539109323Ssam				printf("\n");
1540109323Ssam			}
1541109323Ssam			ifp->if_oerrors++;
1542109323Ssam		} else {
1543109323Ssam			DPRINTF(("port disconnected\n"));
1544109323Ssam			ifp->if_collisions++;	/* XXX */
1545109323Ssam		}
1546109323Ssam	} else
1547109323Ssam		DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
1548109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1549109323Ssam}
155046492Swpaul
1551109323Ssamstatic void
1552109323Ssamwi_tx_intr(struct wi_softc *sc)
1553109323Ssam{
1554109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1555109323Ssam	struct ifnet *ifp = &ic->ic_if;
1556109323Ssam	int fid, cur;
155794405Simp
1558109323Ssam	fid = CSR_READ_2(sc, WI_ALLOC_FID);
1559109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
156046492Swpaul
1561109323Ssam	cur = sc->sc_txcur;
1562109323Ssam	if (sc->sc_txd[cur].d_fid != fid) {
1563109323Ssam		if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
1564109323Ssam		    fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
1565109323Ssam		return;
1566109323Ssam	}
1567109323Ssam	sc->sc_tx_timer = 0;
1568109323Ssam	sc->sc_txd[cur].d_len = 0;
1569112363Simp	sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
1570109323Ssam	if (sc->sc_txd[cur].d_len == 0)
1571109323Ssam		ifp->if_flags &= ~IFF_OACTIVE;
1572109323Ssam	else {
1573109323Ssam		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1574109323Ssam		    0, 0)) {
1575109323Ssam			if_printf(ifp, "xmit failed\n");
1576109323Ssam			sc->sc_txd[cur].d_len = 0;
1577109323Ssam		} else {
1578109323Ssam			sc->sc_tx_timer = 5;
1579109323Ssam			ifp->if_timer = 1;
1580109323Ssam		}
1581109323Ssam	}
158246492Swpaul}
158346492Swpaul
158488546Salfredstatic void
1585109323Ssamwi_info_intr(struct wi_softc *sc)
158694405Simp{
1587109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1588109323Ssam	struct ifnet *ifp = &ic->ic_if;
1589109323Ssam	int i, fid, len, off;
1590109323Ssam	u_int16_t ltbuf[2];
1591109323Ssam	u_int16_t stat;
1592109323Ssam	u_int32_t *ptr;
159394405Simp
1594109323Ssam	fid = CSR_READ_2(sc, WI_INFO_FID);
1595109323Ssam	wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
159694405Simp
1597109323Ssam	switch (le16toh(ltbuf[1])) {
159894405Simp
1599109323Ssam	case WI_INFO_LINK_STAT:
1600109323Ssam		wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1601109323Ssam		DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1602109323Ssam		switch (le16toh(stat)) {
1603109323Ssam		case WI_INFO_LINK_STAT_CONNECTED:
1604109323Ssam			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1605109323Ssam			if (ic->ic_state == IEEE80211_S_RUN &&
1606109323Ssam			    ic->ic_opmode != IEEE80211_M_IBSS)
1607109323Ssam				break;
1608109323Ssam			/* FALLTHROUGH */
1609109323Ssam		case WI_INFO_LINK_STAT_AP_CHG:
1610117812Ssam			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1611109323Ssam			break;
1612109323Ssam		case WI_INFO_LINK_STAT_AP_INR:
1613109323Ssam			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1614109323Ssam			break;
1615109323Ssam		case WI_INFO_LINK_STAT_AP_OOR:
1616109323Ssam			if (sc->sc_firmware_type == WI_SYMBOL &&
1617109323Ssam			    sc->sc_scan_timer > 0) {
1618109323Ssam				if (wi_cmd(sc, WI_CMD_INQUIRE,
1619109323Ssam				    WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
1620109323Ssam					sc->sc_scan_timer = 0;
1621109323Ssam				break;
1622109323Ssam			}
1623109323Ssam			if (ic->ic_opmode == IEEE80211_M_STA)
1624109323Ssam				sc->sc_flags |= WI_FLAGS_OUTRANGE;
1625109323Ssam			break;
1626109323Ssam		case WI_INFO_LINK_STAT_DISCONNECTED:
1627109323Ssam		case WI_INFO_LINK_STAT_ASSOC_FAILED:
1628109323Ssam			if (ic->ic_opmode == IEEE80211_M_STA)
1629117812Ssam				ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1630109323Ssam			break;
1631109323Ssam		}
1632109323Ssam		break;
163394405Simp
1634109323Ssam	case WI_INFO_COUNTERS:
1635109323Ssam		/* some card versions have a larger stats structure */
1636109323Ssam		len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1637109323Ssam		ptr = (u_int32_t *)&sc->sc_stats;
1638109323Ssam		off = sizeof(ltbuf);
1639109323Ssam		for (i = 0; i < len; i++, off += 2, ptr++) {
1640109323Ssam			wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1641109323Ssam#ifdef WI_HERMES_STATS_WAR
1642109323Ssam			if (stat & 0xf000)
1643109323Ssam				stat = ~stat;
1644109323Ssam#endif
1645109323Ssam			*ptr += stat;
1646109323Ssam		}
1647109323Ssam		ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1648109323Ssam		    sc->sc_stats.wi_tx_multi_retries +
1649109323Ssam		    sc->sc_stats.wi_tx_retry_limit;
1650109323Ssam		break;
1651109323Ssam
1652109323Ssam	case WI_INFO_SCAN_RESULTS:
1653109323Ssam	case WI_INFO_HOST_SCAN_RESULTS:
1654109323Ssam		wi_scan_result(sc, fid, le16toh(ltbuf[0]));
1655109323Ssam		break;
1656109323Ssam
1657109323Ssam	default:
1658109323Ssam		DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1659109323Ssam		    le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1660109323Ssam		break;
166194405Simp	}
1662109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1663109323Ssam}
166494405Simp
1665109323Ssamstatic int
1666109323Ssamwi_write_multi(struct wi_softc *sc)
1667109323Ssam{
1668109323Ssam	struct ifnet *ifp = &sc->sc_ic.ic_if;
1669109323Ssam	int n;
1670109323Ssam	struct ifmultiaddr *ifma;
1671109323Ssam	struct wi_mcast mlist;
167294472Simp
1673109323Ssam	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1674109323Ssamallmulti:
1675109323Ssam		memset(&mlist, 0, sizeof(mlist));
1676109323Ssam		return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1677109323Ssam		    sizeof(mlist));
167894405Simp	}
167994405Simp
1680109323Ssam	n = 0;
1681109323Ssam#if __FreeBSD_version < 500000
1682109323Ssam	LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1683109323Ssam#else
1684109323Ssam	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1685109323Ssam#endif
1686109323Ssam		if (ifma->ifma_addr->sa_family != AF_LINK)
1687109323Ssam			continue;
1688109323Ssam		if (n >= 16)
1689109323Ssam			goto allmulti;
1690109323Ssam		IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1691109323Ssam		    (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1692109323Ssam		n++;
169394405Simp	}
1694109323Ssam	return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1695109323Ssam	    IEEE80211_ADDR_LEN * n);
169694405Simp}
169794405Simp
169894405Simpstatic void
1699109323Ssamwi_read_nicid(struct wi_softc *sc)
170046492Swpaul{
1701109323Ssam	struct wi_card_ident *id;
1702109323Ssam	char *p;
1703109323Ssam	int len;
1704109323Ssam	u_int16_t ver[4];
170546492Swpaul
1706109323Ssam	/* getting chip identity */
1707109323Ssam	memset(ver, 0, sizeof(ver));
1708109323Ssam	len = sizeof(ver);
1709109323Ssam	wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1710109323Ssam	device_printf(sc->sc_dev, "using ");
171146492Swpaul
1712109323Ssam	sc->sc_firmware_type = WI_NOTYPE;
1713109323Ssam	for (id = wi_card_ident; id->card_name != NULL; id++) {
1714109323Ssam		if (le16toh(ver[0]) == id->card_id) {
1715109323Ssam			printf("%s", id->card_name);
1716109323Ssam			sc->sc_firmware_type = id->firm_type;
1717109323Ssam			break;
1718109323Ssam		}
171967092Swpaul	}
1720109323Ssam	if (sc->sc_firmware_type == WI_NOTYPE) {
1721109323Ssam		if (le16toh(ver[0]) & 0x8000) {
1722109323Ssam			printf("Unknown PRISM2 chip");
1723109323Ssam			sc->sc_firmware_type = WI_INTERSIL;
1724109323Ssam		} else {
1725109323Ssam			printf("Unknown Lucent chip");
1726109323Ssam			sc->sc_firmware_type = WI_LUCENT;
1727109323Ssam		}
172867092Swpaul	}
172946492Swpaul
1730109323Ssam	/* get primary firmware version (Only Prism chips) */
1731109323Ssam	if (sc->sc_firmware_type != WI_LUCENT) {
1732109323Ssam		memset(ver, 0, sizeof(ver));
1733109323Ssam		len = sizeof(ver);
1734109323Ssam		wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1735109323Ssam		sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1736109323Ssam		    le16toh(ver[3]) * 100 + le16toh(ver[1]);
173767092Swpaul	}
173846492Swpaul
1739109323Ssam	/* get station firmware version */
1740109323Ssam	memset(ver, 0, sizeof(ver));
1741109323Ssam	len = sizeof(ver);
1742109323Ssam	wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1743109323Ssam	sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1744109323Ssam	    le16toh(ver[3]) * 100 + le16toh(ver[1]);
1745109323Ssam	if (sc->sc_firmware_type == WI_INTERSIL &&
1746109323Ssam	    (sc->sc_sta_firmware_ver == 10102 ||
1747109323Ssam	     sc->sc_sta_firmware_ver == 20102)) {
1748109323Ssam		char ident[12];
1749109323Ssam		memset(ident, 0, sizeof(ident));
1750109323Ssam		len = sizeof(ident);
1751109323Ssam		/* value should be the format like "V2.00-11" */
1752109323Ssam		if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1753109323Ssam		    *(p = (char *)ident) >= 'A' &&
1754109323Ssam		    p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1755109323Ssam			sc->sc_firmware_type = WI_SYMBOL;
1756109323Ssam			sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1757109323Ssam			    (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1758109323Ssam			    (p[6] - '0') * 10 + (p[7] - '0');
175994405Simp		}
176094405Simp	}
1761109323Ssam	printf("\n");
1762109323Ssam	device_printf(sc->sc_dev, "%s Firmware: ",
1763109323Ssam	     sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
1764109323Ssam	    (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
1765109323Ssam	if (sc->sc_firmware_type != WI_LUCENT)	/* XXX */
1766109323Ssam		printf("Primary (%u.%u.%u), ",
1767109323Ssam		    sc->sc_pri_firmware_ver / 10000,
1768109323Ssam		    (sc->sc_pri_firmware_ver % 10000) / 100,
1769109323Ssam		    sc->sc_pri_firmware_ver % 100);
1770109323Ssam	printf("Station (%u.%u.%u)\n",
1771109323Ssam	    sc->sc_sta_firmware_ver / 10000,
1772109323Ssam	    (sc->sc_sta_firmware_ver % 10000) / 100,
1773109323Ssam	    sc->sc_sta_firmware_ver % 100);
1774109323Ssam}
177546492Swpaul
1776109323Ssamstatic int
1777109323Ssamwi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1778109323Ssam{
1779109323Ssam	struct wi_ssid ssid;
178046492Swpaul
1781109323Ssam	if (buflen > IEEE80211_NWID_LEN)
1782109323Ssam		return ENOBUFS;
1783109323Ssam	memset(&ssid, 0, sizeof(ssid));
1784109323Ssam	ssid.wi_len = htole16(buflen);
1785109323Ssam	memcpy(ssid.wi_ssid, buf, buflen);
1786109323Ssam	return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1787109323Ssam}
178846492Swpaul
1789109323Ssamstatic int
1790109323Ssamwi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
1791109323Ssam{
1792109323Ssam	struct wi_softc *sc = ifp->if_softc;
1793109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
1794109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
1795109323Ssam	struct wi_req wreq;
1796116898Ssam	struct wi_scan_res *res;
1797116898Ssam	int len, n, error, mif, val, off, i;
179846492Swpaul
1799109323Ssam	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1800109323Ssam	if (error)
1801109323Ssam		return error;
1802109323Ssam	len = (wreq.wi_len - 1) * 2;
1803109323Ssam	if (len < sizeof(u_int16_t))
1804109323Ssam		return ENOSPC;
1805109323Ssam	if (len > sizeof(wreq.wi_val))
1806109323Ssam		len = sizeof(wreq.wi_val);
180746492Swpaul
1808109323Ssam	switch (wreq.wi_type) {
180946492Swpaul
1810109323Ssam	case WI_RID_IFACE_STATS:
1811109323Ssam		memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
1812109323Ssam		if (len < sizeof(sc->sc_stats))
1813109323Ssam			error = ENOSPC;
1814109323Ssam		else
1815109323Ssam			len = sizeof(sc->sc_stats);
1816109323Ssam		break;
181746492Swpaul
1818109323Ssam	case WI_RID_ENCRYPTION:
1819109323Ssam	case WI_RID_TX_CRYPT_KEY:
1820109323Ssam	case WI_RID_DEFLT_CRYPT_KEYS:
1821109323Ssam	case WI_RID_TX_RATE:
1822109323Ssam		return ieee80211_cfgget(ifp, cmd, data);
182346492Swpaul
1824109323Ssam	case WI_RID_MICROWAVE_OVEN:
1825109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
1826109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1827109323Ssam			    &len);
1828109323Ssam			break;
1829109323Ssam		}
1830109323Ssam		wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
1831109323Ssam		len = sizeof(u_int16_t);
1832109323Ssam		break;
183346492Swpaul
1834109323Ssam	case WI_RID_DBM_ADJUST:
1835109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
1836109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1837109323Ssam			    &len);
1838109323Ssam			break;
1839109323Ssam		}
1840109323Ssam		wreq.wi_val[0] = htole16(sc->sc_dbm_adjust);
1841109323Ssam		len = sizeof(u_int16_t);
1842109323Ssam		break;
184346492Swpaul
1844109323Ssam	case WI_RID_ROAMING_MODE:
1845109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
1846109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1847109323Ssam			    &len);
1848109323Ssam			break;
1849109323Ssam		}
1850109323Ssam		wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
1851109323Ssam		len = sizeof(u_int16_t);
1852109323Ssam		break;
185346492Swpaul
1854109323Ssam	case WI_RID_SYSTEM_SCALE:
1855109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
1856109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1857109323Ssam			    &len);
1858109323Ssam			break;
1859109323Ssam		}
1860109323Ssam		wreq.wi_val[0] = htole16(sc->sc_system_scale);
1861109323Ssam		len = sizeof(u_int16_t);
1862109323Ssam		break;
186346492Swpaul
1864109323Ssam	case WI_RID_FRAG_THRESH:
1865109323Ssam		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
1866109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1867109323Ssam			    &len);
1868109323Ssam			break;
1869109323Ssam		}
1870116951Ssam		wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
1871109323Ssam		len = sizeof(u_int16_t);
1872109323Ssam		break;
187346492Swpaul
1874109323Ssam	case WI_RID_READ_APS:
1875109323Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1876109323Ssam			return ieee80211_cfgget(ifp, cmd, data);
1877109323Ssam		if (sc->sc_scan_timer > 0) {
1878109323Ssam			error = EINPROGRESS;
1879109323Ssam			break;
1880109323Ssam		}
1881109323Ssam		n = sc->sc_naps;
1882109323Ssam		if (len < sizeof(n)) {
1883109323Ssam			error = ENOSPC;
1884109323Ssam			break;
1885109323Ssam		}
1886109323Ssam		if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
1887109323Ssam			n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
1888109323Ssam		len = sizeof(n) + sizeof(struct wi_apinfo) * n;
1889109323Ssam		memcpy(wreq.wi_val, &n, sizeof(n));
1890109323Ssam		memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
1891109323Ssam		    sizeof(struct wi_apinfo) * n);
1892109323Ssam		break;
189346492Swpaul
1894109323Ssam	case WI_RID_PRISM2:
1895109323Ssam		wreq.wi_val[0] = sc->sc_firmware_type != WI_LUCENT;
1896109323Ssam		len = sizeof(u_int16_t);
1897109323Ssam		break;
189846492Swpaul
1899109323Ssam	case WI_RID_MIF:
1900109323Ssam		mif = wreq.wi_val[0];
1901109323Ssam		error = wi_cmd(sc, WI_CMD_READMIF, mif, 0, 0);
1902109323Ssam		val = CSR_READ_2(sc, WI_RESP0);
1903109323Ssam		wreq.wi_val[0] = val;
1904109323Ssam		len = sizeof(u_int16_t);
1905109323Ssam		break;
190646492Swpaul
1907109323Ssam	case WI_RID_ZERO_CACHE:
1908109323Ssam	case WI_RID_PROCFRAME:		/* ignore for compatibility */
1909109323Ssam		/* XXX ??? */
1910109323Ssam		break;
191146492Swpaul
1912109323Ssam	case WI_RID_READ_CACHE:
1913109323Ssam		return ieee80211_cfgget(ifp, cmd, data);
191446492Swpaul
1915116898Ssam	case WI_RID_SCAN_RES:		/* compatibility interface */
1916116898Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1917116898Ssam			return ieee80211_cfgget(ifp, cmd, data);
1918116898Ssam		if (sc->sc_scan_timer > 0) {
1919116898Ssam			error = EINPROGRESS;
1920116898Ssam			break;
1921116898Ssam		}
1922116898Ssam		n = sc->sc_naps;
1923116898Ssam		off = sc->sc_firmware_type != WI_LUCENT ?
1924116898Ssam			sizeof(struct wi_scan_p2_hdr) : 0;
1925116898Ssam		if (len < off + sizeof(struct wi_scan_res) * n)
1926116898Ssam			n = (len - off) / sizeof(struct wi_scan_res);
1927116898Ssam		len = off + sizeof(struct wi_scan_res) * n;
1928116898Ssam		if (off != 0) {
1929116898Ssam			struct wi_scan_p2_hdr *p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1930116898Ssam			/*
1931116898Ssam			 * Prepend Prism-specific header.
1932116898Ssam			 */
1933116898Ssam			if (len < sizeof(struct wi_scan_p2_hdr)) {
1934116898Ssam				error = ENOSPC;
1935116898Ssam				break;
1936116898Ssam			}
1937116898Ssam			p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1938116898Ssam			p2->wi_rsvd = 0;
1939116898Ssam			p2->wi_reason = n;	/* XXX */
1940116898Ssam		}
1941116898Ssam		for (i = 0; i < n; i++) {
1942116898Ssam			const struct wi_apinfo *ap = &sc->sc_aps[i];
1943116898Ssam
1944116898Ssam			res = (struct wi_scan_res *)((char *)wreq.wi_val + off);
1945116898Ssam			res->wi_chan = ap->channel;
1946116898Ssam			res->wi_noise = ap->noise;
1947116898Ssam			res->wi_signal = ap->signal;
1948116898Ssam			IEEE80211_ADDR_COPY(res->wi_bssid, ap->bssid);
1949116898Ssam			res->wi_interval = ap->interval;
1950116898Ssam			res->wi_capinfo = ap->capinfo;
1951116898Ssam			res->wi_ssid_len = ap->namelen;
1952116898Ssam			memcpy(res->wi_ssid, ap->name,
1953116898Ssam				IEEE80211_NWID_LEN);
1954116898Ssam			if (sc->sc_firmware_type != WI_LUCENT) {
1955116898Ssam				/* XXX not saved from Prism cards */
1956116898Ssam				memset(res->wi_srates, 0,
1957116898Ssam					sizeof(res->wi_srates));
1958116898Ssam				res->wi_rate = ap->rate;
1959116898Ssam				res->wi_rsvd = 0;
1960116898Ssam				off += WI_PRISM2_RES_SIZE;
1961116898Ssam			} else
1962116898Ssam				off += WI_WAVELAN_RES_SIZE;
1963116898Ssam		}
1964116898Ssam		break;
1965116898Ssam
1966109323Ssam	default:
1967109323Ssam		if (sc->sc_enabled) {
1968109323Ssam			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1969109323Ssam			    &len);
1970109323Ssam			break;
1971109323Ssam		}
1972109323Ssam		switch (wreq.wi_type) {
1973109323Ssam		case WI_RID_MAX_DATALEN:
1974109323Ssam			wreq.wi_val[0] = htole16(sc->sc_max_datalen);
1975109323Ssam			len = sizeof(u_int16_t);
1976109323Ssam			break;
1977109323Ssam		case WI_RID_RTS_THRESH:
1978116951Ssam			wreq.wi_val[0] = htole16(ic->ic_rtsthreshold);
1979109323Ssam			len = sizeof(u_int16_t);
1980109323Ssam			break;
1981109323Ssam		case WI_RID_CNFAUTHMODE:
1982109323Ssam			wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
1983109323Ssam			len = sizeof(u_int16_t);
1984109323Ssam			break;
1985109323Ssam		case WI_RID_NODENAME:
1986109323Ssam			if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
1987109323Ssam				error = ENOSPC;
1988109323Ssam				break;
1989109323Ssam			}
1990109323Ssam			len = sc->sc_nodelen + sizeof(u_int16_t);
1991109323Ssam			wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
1992109323Ssam			memcpy(&wreq.wi_val[1], sc->sc_nodename,
1993109323Ssam			    sc->sc_nodelen);
1994109323Ssam			break;
1995109323Ssam		default:
1996109323Ssam			return ieee80211_cfgget(ifp, cmd, data);
1997109323Ssam		}
1998109323Ssam		break;
199946492Swpaul	}
2000109323Ssam	if (error)
2001109323Ssam		return error;
2002109323Ssam	wreq.wi_len = (len + 1) / 2 + 1;
2003109323Ssam	return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
200446492Swpaul}
200546492Swpaul
2006109323Ssamstatic int
2007109323Ssamwi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
200846492Swpaul{
2009109323Ssam	struct wi_softc *sc = ifp->if_softc;
2010109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2011109323Ssam	struct ifreq *ifr = (struct ifreq *)data;
2012109323Ssam	struct wi_req wreq;
2013109323Ssam	struct mbuf *m;
2014109323Ssam	int i, len, error, mif, val;
2015116951Ssam	struct ieee80211_rateset *rs;
201646492Swpaul
2017109323Ssam	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2018109323Ssam	if (error)
2019109323Ssam		return error;
2020116898Ssam	len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0;
2021109323Ssam	switch (wreq.wi_type) {
2022109323Ssam	case WI_RID_DBM_ADJUST:
2023109323Ssam		return ENODEV;
202467092Swpaul
2025109323Ssam	case WI_RID_NODENAME:
2026109323Ssam		if (le16toh(wreq.wi_val[0]) * 2 > len ||
2027109323Ssam		    le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
2028109323Ssam			error = ENOSPC;
2029109323Ssam			break;
2030109323Ssam		}
2031109323Ssam		if (sc->sc_enabled) {
2032109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2033109323Ssam			    len);
2034109323Ssam			if (error)
2035109323Ssam				break;
2036109323Ssam		}
2037109323Ssam		sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
2038109323Ssam		memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen);
2039109323Ssam		break;
204046492Swpaul
2041109323Ssam	case WI_RID_MICROWAVE_OVEN:
2042109323Ssam	case WI_RID_ROAMING_MODE:
2043109323Ssam	case WI_RID_SYSTEM_SCALE:
2044109323Ssam	case WI_RID_FRAG_THRESH:
2045109323Ssam		if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
2046109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
2047109323Ssam			break;
2048109323Ssam		if (wreq.wi_type == WI_RID_ROAMING_MODE &&
2049109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
2050109323Ssam			break;
2051109323Ssam		if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
2052109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
2053109323Ssam			break;
2054109323Ssam		if (wreq.wi_type == WI_RID_FRAG_THRESH &&
2055109323Ssam		    (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
2056109323Ssam			break;
2057109323Ssam		/* FALLTHROUGH */
2058109323Ssam	case WI_RID_RTS_THRESH:
2059109323Ssam	case WI_RID_CNFAUTHMODE:
2060109323Ssam	case WI_RID_MAX_DATALEN:
2061109323Ssam		if (sc->sc_enabled) {
2062109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2063109323Ssam			    sizeof(u_int16_t));
2064109323Ssam			if (error)
2065109323Ssam				break;
2066109323Ssam		}
2067109323Ssam		switch (wreq.wi_type) {
2068109323Ssam		case WI_RID_FRAG_THRESH:
2069116951Ssam			ic->ic_fragthreshold = le16toh(wreq.wi_val[0]);
2070109323Ssam			break;
2071109323Ssam		case WI_RID_RTS_THRESH:
2072116951Ssam			ic->ic_rtsthreshold = le16toh(wreq.wi_val[0]);
2073109323Ssam			break;
2074109323Ssam		case WI_RID_MICROWAVE_OVEN:
2075109323Ssam			sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
2076109323Ssam			break;
2077109323Ssam		case WI_RID_ROAMING_MODE:
2078109323Ssam			sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
2079109323Ssam			break;
2080109323Ssam		case WI_RID_SYSTEM_SCALE:
2081109323Ssam			sc->sc_system_scale = le16toh(wreq.wi_val[0]);
2082109323Ssam			break;
2083109323Ssam		case WI_RID_CNFAUTHMODE:
2084109323Ssam			sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
2085109323Ssam			break;
2086109323Ssam		case WI_RID_MAX_DATALEN:
2087109323Ssam			sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
2088109323Ssam			break;
2089109323Ssam		}
2090109323Ssam		break;
209194405Simp
2092109323Ssam	case WI_RID_TX_RATE:
2093109323Ssam		switch (le16toh(wreq.wi_val[0])) {
2094109323Ssam		case 3:
2095109323Ssam			ic->ic_fixed_rate = -1;
2096109323Ssam			break;
2097109323Ssam		default:
2098116951Ssam			rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
2099116951Ssam			for (i = 0; i < rs->rs_nrates; i++) {
2100116951Ssam				if ((rs->rs_rates[i] & IEEE80211_RATE_VAL)
2101109323Ssam				    / 2 == le16toh(wreq.wi_val[0]))
2102109323Ssam					break;
2103109323Ssam			}
2104116951Ssam			if (i == rs->rs_nrates)
2105109323Ssam				return EINVAL;
2106109323Ssam			ic->ic_fixed_rate = i;
2107109323Ssam		}
2108109323Ssam		if (sc->sc_enabled)
2109109323Ssam			error = wi_write_txrate(sc);
2110109323Ssam		break;
211146492Swpaul
2112109323Ssam	case WI_RID_SCAN_APS:
2113109323Ssam		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2114116898Ssam			error = wi_scan_ap(sc, 0x3fff, 0x000f);
2115109323Ssam		break;
211646492Swpaul
2117116898Ssam	case WI_RID_SCAN_REQ:		/* compatibility interface */
2118116898Ssam		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2119116898Ssam			error = wi_scan_ap(sc, wreq.wi_val[0], wreq.wi_val[1]);
2120116898Ssam		break;
2121116898Ssam
2122109323Ssam	case WI_RID_MGMT_XMIT:
2123109323Ssam		if (!sc->sc_enabled) {
2124109323Ssam			error = ENETDOWN;
2125109323Ssam			break;
2126109323Ssam		}
2127109323Ssam		if (ic->ic_mgtq.ifq_len > 5) {
2128109323Ssam			error = EAGAIN;
2129109323Ssam			break;
2130109323Ssam		}
2131109323Ssam		/* XXX wi_len looks in u_int8_t, not in u_int16_t */
2132109323Ssam		m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL);
2133109323Ssam		if (m == NULL) {
2134109323Ssam			error = ENOMEM;
2135109323Ssam			break;
2136109323Ssam		}
2137109323Ssam		IF_ENQUEUE(&ic->ic_mgtq, m);
2138109323Ssam		break;
213946492Swpaul
2140109323Ssam	case WI_RID_MIF:
2141109323Ssam		mif = wreq.wi_val[0];
2142109323Ssam		val = wreq.wi_val[1];
2143109323Ssam		error = wi_cmd(sc, WI_CMD_WRITEMIF, mif, val, 0);
2144109323Ssam		break;
214546492Swpaul
2146109323Ssam	case WI_RID_PROCFRAME:		/* ignore for compatibility */
2147109323Ssam		break;
2148109323Ssam
2149116898Ssam	case WI_RID_OWN_SSID:
2150116898Ssam		if (le16toh(wreq.wi_val[0]) * 2 > len ||
2151116898Ssam		    le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) {
2152116898Ssam			error = ENOSPC;
2153116898Ssam			break;
2154116898Ssam		}
2155116898Ssam		memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
2156116898Ssam		ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2;
2157116898Ssam		memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen);
2158116898Ssam		error = ENETRESET;
2159116898Ssam		break;
2160116898Ssam
2161109323Ssam	default:
2162109323Ssam		if (sc->sc_enabled) {
2163109323Ssam			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2164109323Ssam			    len);
2165109323Ssam			if (error)
2166109323Ssam				break;
2167109323Ssam		}
2168109323Ssam		error = ieee80211_cfgset(ifp, cmd, data);
2169109323Ssam		break;
2170109323Ssam	}
2171109323Ssam	return error;
217246492Swpaul}
217346492Swpaul
2174109323Ssamstatic int
2175109323Ssamwi_write_txrate(struct wi_softc *sc)
217646492Swpaul{
2177109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2178109323Ssam	int i;
2179109323Ssam	u_int16_t rate;
218046492Swpaul
2181109323Ssam	if (ic->ic_fixed_rate < 0)
2182109323Ssam		rate = 0;	/* auto */
2183109323Ssam	else
2184116951Ssam		rate = (ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ic->ic_fixed_rate] &
2185109323Ssam		    IEEE80211_RATE_VAL) / 2;
218646492Swpaul
2187109323Ssam	/* rate: 0, 1, 2, 5, 11 */
218846492Swpaul
2189109323Ssam	switch (sc->sc_firmware_type) {
2190109323Ssam	case WI_LUCENT:
2191112501Simp		switch (rate) {
2192112501Simp		case 0:			/* auto == 11mbps auto */
2193112501Simp			rate = 3;
2194112501Simp			break;
2195112501Simp		/* case 1, 2 map to 1, 2*/
2196112501Simp		case 5:			/* 5.5Mbps -> 4 */
2197112501Simp			rate = 4;
2198112501Simp			break;
2199112501Simp		case 11:		/* 11mbps -> 5 */
2200112501Simp			rate = 5;
2201112501Simp			break;
2202112501Simp		default:
2203112501Simp			break;
2204112501Simp		}
2205109323Ssam		break;
2206109323Ssam	default:
2207109323Ssam		/* Choose a bit according to this table.
2208109323Ssam		 *
2209109323Ssam		 * bit | data rate
2210109323Ssam		 * ----+-------------------
2211109323Ssam		 * 0   | 1Mbps
2212109323Ssam		 * 1   | 2Mbps
2213109323Ssam		 * 2   | 5.5Mbps
2214109323Ssam		 * 3   | 11Mbps
2215109323Ssam		 */
2216109323Ssam		for (i = 8; i > 0; i >>= 1) {
2217109323Ssam			if (rate >= i)
2218109323Ssam				break;
2219109323Ssam		}
2220109323Ssam		if (i == 0)
2221109323Ssam			rate = 0xf;	/* auto */
2222109323Ssam		else
2223109323Ssam			rate = i;
2224109323Ssam		break;
2225109323Ssam	}
2226109323Ssam	return wi_write_val(sc, WI_RID_TX_RATE, rate);
2227109323Ssam}
222846492Swpaul
2229109323Ssamstatic int
2230109323Ssamwi_write_wep(struct wi_softc *sc)
2231109323Ssam{
2232109323Ssam	struct ieee80211com *ic = &sc->sc_ic;
2233109323Ssam	int error = 0;
2234109323Ssam	int i, keylen;
2235109323Ssam	u_int16_t val;
2236109323Ssam	struct wi_key wkey[IEEE80211_WEP_NKID];
223746492Swpaul
2238109323Ssam	switch (sc->sc_firmware_type) {
2239109323Ssam	case WI_LUCENT:
2240109323Ssam		val = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0;
2241109323Ssam		error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
2242109323Ssam		if (error)
2243109323Ssam			break;
2244109323Ssam		error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_wep_txkey);
2245109323Ssam		if (error)
2246109323Ssam			break;
2247109323Ssam		memset(wkey, 0, sizeof(wkey));
2248109323Ssam		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2249109323Ssam			keylen = ic->ic_nw_keys[i].wk_len;
2250109323Ssam			wkey[i].wi_keylen = htole16(keylen);
2251109323Ssam			memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
2252109323Ssam			    keylen);
2253109323Ssam		}
2254109323Ssam		error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
2255109323Ssam		    wkey, sizeof(wkey));
2256109323Ssam		break;
2257109323Ssam
2258109323Ssam	case WI_INTERSIL:
2259109323Ssam	case WI_SYMBOL:
2260109323Ssam		if (ic->ic_flags & IEEE80211_F_WEPON) {
2261109323Ssam			/*
2262109323Ssam			 * ONLY HWB3163 EVAL-CARD Firmware version
2263109323Ssam			 * less than 0.8 variant2
2264109323Ssam			 *
2265109323Ssam			 *   If promiscuous mode disable, Prism2 chip
2266109323Ssam			 *  does not work with WEP .
2267109323Ssam			 * It is under investigation for details.
2268109323Ssam			 * (ichiro@netbsd.org)
2269109323Ssam			 */
2270109323Ssam			if (sc->sc_firmware_type == WI_INTERSIL &&
2271109323Ssam			    sc->sc_sta_firmware_ver < 802 ) {
2272109323Ssam				/* firm ver < 0.8 variant 2 */
2273109323Ssam				wi_write_val(sc, WI_RID_PROMISC, 1);
2274109323Ssam			}
2275109323Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE,
2276109323Ssam			    sc->sc_cnfauthmode);
2277109323Ssam			val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
2278109323Ssam			/*
2279109323Ssam			 * Encryption firmware has a bug for HostAP mode.
2280109323Ssam			 */
2281109323Ssam			if (sc->sc_firmware_type == WI_INTERSIL &&
2282109323Ssam			    ic->ic_opmode == IEEE80211_M_HOSTAP)
2283109323Ssam				val |= HOST_ENCRYPT;
2284109323Ssam		} else {
2285109323Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE,
2286109323Ssam			    IEEE80211_AUTH_OPEN);
2287109323Ssam			val = HOST_ENCRYPT | HOST_DECRYPT;
2288109323Ssam		}
2289109323Ssam		error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
2290109323Ssam		if (error)
2291109323Ssam			break;
2292109323Ssam		error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
2293109323Ssam		    ic->ic_wep_txkey);
2294109323Ssam		if (error)
2295109323Ssam			break;
2296109323Ssam		/*
2297109323Ssam		 * It seems that the firmware accept 104bit key only if
2298109323Ssam		 * all the keys have 104bit length.  We get the length of
2299109323Ssam		 * the transmit key and use it for all other keys.
2300109323Ssam		 * Perhaps we should use software WEP for such situation.
2301109323Ssam		 */
2302109323Ssam		keylen = ic->ic_nw_keys[ic->ic_wep_txkey].wk_len;
2303109323Ssam		if (keylen > IEEE80211_WEP_KEYLEN)
2304109323Ssam			keylen = 13;	/* 104bit keys */
2305109323Ssam		else
2306109323Ssam			keylen = IEEE80211_WEP_KEYLEN;
2307109323Ssam		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2308109323Ssam			error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
2309109323Ssam			    ic->ic_nw_keys[i].wk_key, keylen);
2310109323Ssam			if (error)
2311109323Ssam				break;
2312109323Ssam		}
2313109323Ssam		break;
2314109323Ssam	}
2315109323Ssam	return error;
231646492Swpaul}
231746492Swpaul
2318109323Ssamstatic int
2319109323Ssamwi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
232046492Swpaul{
2321109323Ssam	int			i, s = 0;
2322109323Ssam	static volatile int count  = 0;
2323109323Ssam
2324109323Ssam	if (count > 0)
2325109323Ssam		panic("Hey partner, hold on there!");
2326109323Ssam	count++;
232753702Swpaul
2328109323Ssam	/* wait for the busy bit to clear */
2329109323Ssam	for (i = 500; i > 0; i--) {	/* 5s */
2330116206Simp		if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
2331109323Ssam			break;
2332109323Ssam		DELAY(10*1000);	/* 10 m sec */
2333109323Ssam	}
2334109323Ssam	if (i == 0) {
2335109323Ssam		device_printf(sc->sc_dev, "wi_cmd: busy bit won't clear.\n" );
2336109323Ssam		count--;
2337109323Ssam		return(ETIMEDOUT);
2338109323Ssam	}
233990580Sbrooks
2340109323Ssam	CSR_WRITE_2(sc, WI_PARAM0, val0);
2341109323Ssam	CSR_WRITE_2(sc, WI_PARAM1, val1);
2342109323Ssam	CSR_WRITE_2(sc, WI_PARAM2, val2);
2343109323Ssam	CSR_WRITE_2(sc, WI_COMMAND, cmd);
234490580Sbrooks
2345109323Ssam	if (cmd == WI_CMD_INI) {
2346109323Ssam		/* XXX: should sleep here. */
2347116206Simp		DELAY(100*1000);		/* 100ms delay for init */
2348109323Ssam	}
2349109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
2350109323Ssam		/*
2351109323Ssam		 * Wait for 'command complete' bit to be
2352109323Ssam		 * set in the event status register.
2353109323Ssam		 */
2354109323Ssam		s = CSR_READ_2(sc, WI_EVENT_STAT);
2355109323Ssam		if (s & WI_EV_CMD) {
2356109323Ssam			/* Ack the event and read result code. */
2357109323Ssam			s = CSR_READ_2(sc, WI_STATUS);
2358109323Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
2359109323Ssam			if (s & WI_STAT_CMD_RESULT) {
2360109323Ssam				count--;
2361109323Ssam				return(EIO);
2362109323Ssam			}
2363109323Ssam			break;
236490580Sbrooks		}
2365109323Ssam		DELAY(WI_DELAY);
2366109323Ssam	}
236790580Sbrooks
2368109323Ssam	count--;
2369109323Ssam	if (i == WI_TIMEOUT) {
2370109323Ssam		device_printf(sc->sc_dev,
2371109323Ssam		    "timeout in wi_cmd 0x%04x; event status 0x%04x\n", cmd, s);
2372109323Ssam		return(ETIMEDOUT);
237353702Swpaul	}
2374109323Ssam	return (0);
2375109323Ssam}
237653702Swpaul
2377109323Ssamstatic int
2378109323Ssamwi_seek_bap(struct wi_softc *sc, int id, int off)
2379109323Ssam{
2380109323Ssam	int i, status;
238190580Sbrooks
2382109323Ssam	CSR_WRITE_2(sc, WI_SEL0, id);
2383109323Ssam	CSR_WRITE_2(sc, WI_OFF0, off);
238490580Sbrooks
2385109323Ssam	for (i = 0; ; i++) {
2386109323Ssam		status = CSR_READ_2(sc, WI_OFF0);
2387109323Ssam		if ((status & WI_OFF_BUSY) == 0)
2388109323Ssam			break;
2389109323Ssam		if (i == WI_TIMEOUT) {
2390109323Ssam			device_printf(sc->sc_dev, "timeout in wi_seek to %x/%x\n",
2391109323Ssam			    id, off);
2392109323Ssam			sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2393109323Ssam			return ETIMEDOUT;
2394109323Ssam		}
2395109323Ssam		DELAY(1);
239653702Swpaul	}
2397109323Ssam	if (status & WI_OFF_ERR) {
2398109323Ssam		device_printf(sc->sc_dev, "failed in wi_seek to %x/%x\n", id, off);
2399109323Ssam		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2400109323Ssam		return EIO;
2401109323Ssam	}
2402109323Ssam	sc->sc_bap_id = id;
2403109323Ssam	sc->sc_bap_off = off;
2404109323Ssam	return 0;
240553702Swpaul}
240653702Swpaul
2407109323Ssamstatic int
2408109323Ssamwi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
240953702Swpaul{
2410109323Ssam	u_int16_t *ptr;
2411109323Ssam	int i, error, cnt;
241253702Swpaul
2413109323Ssam	if (buflen == 0)
2414109323Ssam		return 0;
2415109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2416109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
2417109323Ssam			return error;
241875219Salfred	}
2419109323Ssam	cnt = (buflen + 1) / 2;
2420109323Ssam	ptr = (u_int16_t *)buf;
2421109323Ssam	for (i = 0; i < cnt; i++)
2422109323Ssam		*ptr++ = CSR_READ_2(sc, WI_DATA0);
2423109323Ssam	sc->sc_bap_off += cnt * 2;
2424109323Ssam	return 0;
242553702Swpaul}
242653702Swpaul
2427109323Ssamstatic int
2428109323Ssamwi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
242953702Swpaul{
2430109323Ssam	u_int16_t *ptr;
2431109323Ssam	int i, error, cnt;
243246492Swpaul
2433109323Ssam	if (buflen == 0)
2434109323Ssam		return 0;
243546492Swpaul
2436109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
2437109323Ssam  again:
2438109323Ssam#endif
2439109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2440109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
2441109323Ssam			return error;
2442109323Ssam	}
2443109323Ssam	cnt = (buflen + 1) / 2;
2444109323Ssam	ptr = (u_int16_t *)buf;
2445109323Ssam	for (i = 0; i < cnt; i++)
2446109323Ssam		CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
2447109323Ssam	sc->sc_bap_off += cnt * 2;
2448109323Ssam
2449109323Ssam#ifdef WI_HERMES_AUTOINC_WAR
2450109323Ssam	/*
2451109323Ssam	 * According to the comments in the HCF Light code, there is a bug
2452109323Ssam	 * in the Hermes (or possibly in certain Hermes firmware revisions)
2453109323Ssam	 * where the chip's internal autoincrement counter gets thrown off
2454109323Ssam	 * during data writes:  the autoincrement is missed, causing one
2455109323Ssam	 * data word to be overwritten and subsequent words to be written to
2456109323Ssam	 * the wrong memory locations. The end result is that we could end
2457109323Ssam	 * up transmitting bogus frames without realizing it. The workaround
2458109323Ssam	 * for this is to write a couple of extra guard words after the end
2459109323Ssam	 * of the transfer, then attempt to read then back. If we fail to
2460109323Ssam	 * locate the guard words where we expect them, we preform the
2461109323Ssam	 * transfer over again.
2462109323Ssam	 */
2463109323Ssam	if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
2464109323Ssam		CSR_WRITE_2(sc, WI_DATA0, 0x1234);
2465109323Ssam		CSR_WRITE_2(sc, WI_DATA0, 0x5678);
2466109323Ssam		wi_seek_bap(sc, id, sc->sc_bap_off);
2467109323Ssam		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2468109323Ssam		if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
2469109323Ssam		    CSR_READ_2(sc, WI_DATA0) != 0x5678) {
2470109323Ssam			device_printf(sc->sc_dev,
2471109323Ssam				"detect auto increment bug, try again\n");
2472109323Ssam			goto again;
2473109323Ssam		}
2474109323Ssam	}
2475109323Ssam#endif
2476109323Ssam	return 0;
247746492Swpaul}
247853702Swpaul
2479109323Ssamstatic int
2480109323Ssamwi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
2481109323Ssam{
2482109323Ssam	int error, len;
2483109323Ssam	struct mbuf *m;
248453702Swpaul
2485109323Ssam	for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
2486109323Ssam		if (m->m_len == 0)
2487109323Ssam			continue;
248853702Swpaul
2489109323Ssam		len = min(m->m_len, totlen);
249053702Swpaul
2491109323Ssam		if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
2492109323Ssam			m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
2493109323Ssam			return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
2494109323Ssam			    totlen);
2495109323Ssam		}
249653702Swpaul
2497109323Ssam		if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
2498109323Ssam			return error;
249953702Swpaul
2500109323Ssam		off += m->m_len;
2501109323Ssam		totlen -= len;
2502109323Ssam	}
2503109323Ssam	return 0;
2504109323Ssam}
250553702Swpaul
2506109323Ssamstatic int
2507109323Ssamwi_alloc_fid(struct wi_softc *sc, int len, int *idp)
250853702Swpaul{
250953702Swpaul	int i;
251053702Swpaul
2511109323Ssam	if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
2512109323Ssam		device_printf(sc->sc_dev, "failed to allocate %d bytes on NIC\n",
2513109323Ssam		    len);
2514109323Ssam		return ENOMEM;
251553702Swpaul	}
251653702Swpaul
2517109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
2518109323Ssam		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
2519109323Ssam			break;
2520109323Ssam		if (i == WI_TIMEOUT) {
2521109323Ssam			device_printf(sc->sc_dev, "timeout in alloc\n");
2522109323Ssam			return ETIMEDOUT;
2523109323Ssam		}
2524109323Ssam		DELAY(1);
252553702Swpaul	}
2526109323Ssam	*idp = CSR_READ_2(sc, WI_ALLOC_FID);
2527109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
2528109323Ssam	return 0;
2529109323Ssam}
253053702Swpaul
2531109323Ssamstatic int
2532109323Ssamwi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
2533109323Ssam{
2534109323Ssam	int error, len;
2535109323Ssam	u_int16_t ltbuf[2];
253653702Swpaul
2537109323Ssam	/* Tell the NIC to enter record read mode. */
2538109323Ssam	error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2539109323Ssam	if (error)
2540109323Ssam		return error;
254153702Swpaul
2542109323Ssam	error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2543109323Ssam	if (error)
2544109323Ssam		return error;
254553702Swpaul
2546109323Ssam	if (le16toh(ltbuf[1]) != rid) {
2547109323Ssam		device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n",
2548109323Ssam		    rid, le16toh(ltbuf[1]));
2549109323Ssam		return EIO;
255053702Swpaul	}
2551109323Ssam	len = (le16toh(ltbuf[0]) - 1) * 2;	 /* already got rid */
2552109323Ssam	if (*buflenp < len) {
2553109323Ssam		device_printf(sc->sc_dev, "record buffer is too small, "
2554109323Ssam		    "rid=%x, size=%d, len=%d\n",
2555109323Ssam		    rid, *buflenp, len);
2556109323Ssam		return ENOSPC;
255753702Swpaul	}
2558109323Ssam	*buflenp = len;
2559109323Ssam	return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2560109323Ssam}
256153702Swpaul
2562109323Ssamstatic int
2563109323Ssamwi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2564109323Ssam{
2565109323Ssam	int error;
2566109323Ssam	u_int16_t ltbuf[2];
256753702Swpaul
2568109323Ssam	ltbuf[0] = htole16((buflen + 1) / 2 + 1);	 /* includes rid */
2569109323Ssam	ltbuf[1] = htole16(rid);
257053702Swpaul
2571109323Ssam	error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2572109323Ssam	if (error)
2573109323Ssam		return error;
2574109323Ssam	error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2575109323Ssam	if (error)
2576109323Ssam		return error;
2577102206Simp
2578109323Ssam	return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
257953702Swpaul}
258077217Sphk
258188546Salfredstatic int
2582117812Ssamwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
258377217Sphk{
2584117812Ssam	struct ifnet *ifp = &ic->ic_if;
2585117812Ssam	struct wi_softc *sc = ifp->if_softc;
2586116951Ssam	struct ieee80211_node *ni = ic->ic_bss;
2587116951Ssam	int buflen;
2588109323Ssam	u_int16_t val;
2589109323Ssam	struct wi_ssid ssid;
2590109323Ssam	u_int8_t old_bssid[IEEE80211_ADDR_LEN];
259177217Sphk
2592117812Ssam	DPRINTF(("%s: %s -> %s\n", __func__,
2593117812Ssam		ieee80211_state_name[ic->ic_state],
2594117812Ssam		ieee80211_state_name[nstate]));
2595109323Ssam
2596109323Ssam	switch (nstate) {
2597109323Ssam	case IEEE80211_S_INIT:
2598109323Ssam		ic->ic_flags &= ~IEEE80211_F_SIBSS;
2599109323Ssam		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2600117812Ssam		return (*sc->sc_newstate)(ic, nstate, arg);
2601109323Ssam
2602109323Ssam	case IEEE80211_S_RUN:
2603109323Ssam		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2604109323Ssam		buflen = IEEE80211_ADDR_LEN;
2605109323Ssam		wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen);
2606109323Ssam		IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
2607109323Ssam		buflen = sizeof(val);
2608109323Ssam		wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
2609116951Ssam		/* XXX validate channel */
2610116951Ssam		ni->ni_chan = &ic->ic_channels[le16toh(val)];
2611109323Ssam
2612109323Ssam		if (IEEE80211_ADDR_EQ(old_bssid, ni->ni_bssid))
2613109323Ssam			sc->sc_false_syns++;
2614109323Ssam		else
2615109323Ssam			sc->sc_false_syns = 0;
2616109323Ssam
2617109323Ssam		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2618109323Ssam			ni->ni_esslen = ic->ic_des_esslen;
2619109323Ssam			memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
2620116951Ssam			ni->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B];
2621109323Ssam			ni->ni_intval = ic->ic_lintval;
2622109323Ssam			ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
2623109323Ssam			if (ic->ic_flags & IEEE80211_F_WEPON)
2624109323Ssam				ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
262577217Sphk		} else {
2626109323Ssam			/* XXX check return value */
2627109323Ssam			buflen = sizeof(ssid);
2628109323Ssam			wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
2629109323Ssam			ni->ni_esslen = le16toh(ssid.wi_len);
2630109323Ssam			if (ni->ni_esslen > IEEE80211_NWID_LEN)
2631109323Ssam				ni->ni_esslen = IEEE80211_NWID_LEN;	/*XXX*/
2632109323Ssam			memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
263377217Sphk		}
263477217Sphk		break;
2635109323Ssam
2636109323Ssam	case IEEE80211_S_SCAN:
2637109323Ssam	case IEEE80211_S_AUTH:
2638109323Ssam	case IEEE80211_S_ASSOC:
263977217Sphk		break;
264077217Sphk	}
264177217Sphk
2642117812Ssam	ic->ic_state = nstate;		/* NB: skip normal ieee80211 handling */
2643117812Ssam	return 0;
264477217Sphk}
264577217Sphk
264688546Salfredstatic int
2647116898Ssamwi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate)
264877217Sphk{
2649109323Ssam	int error = 0;
2650109323Ssam	u_int16_t val[2];
265177217Sphk
2652109323Ssam	if (!sc->sc_enabled)
2653109323Ssam		return ENXIO;
2654109323Ssam	switch (sc->sc_firmware_type) {
2655109323Ssam	case WI_LUCENT:
2656109323Ssam		(void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
265798440Simp		break;
2658109323Ssam	case WI_INTERSIL:
2659116898Ssam		val[0] = chanmask;	/* channel */
2660116898Ssam		val[1] = txrate;	/* tx rate */
2661109323Ssam		error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
266298440Simp		break;
2663109323Ssam	case WI_SYMBOL:
2664109323Ssam		/*
2665109323Ssam		 * XXX only supported on 3.x ?
2666109323Ssam		 */
2667109323Ssam		val[0] = BSCAN_BCAST | BSCAN_ONETIME;
2668109323Ssam		error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
2669109323Ssam		    val, sizeof(val[0]));
267098440Simp		break;
267198440Simp	}
2672109323Ssam	if (error == 0) {
2673109323Ssam		sc->sc_scan_timer = WI_SCAN_WAIT;
2674109323Ssam		sc->sc_ic.ic_if.if_timer = 1;
2675116898Ssam		DPRINTF(("wi_scan_ap: start scanning, "
2676116898Ssam			"chamask 0x%x txrate 0x%x\n", chanmask, txrate));
2677109323Ssam	}
2678109323Ssam	return error;
2679109323Ssam}
268077217Sphk
2681109323Ssamstatic void
2682109323Ssamwi_scan_result(struct wi_softc *sc, int fid, int cnt)
2683109323Ssam{
2684109323Ssam#define	N(a)	(sizeof (a) / sizeof (a[0]))
2685109323Ssam	int i, naps, off, szbuf;
2686109323Ssam	struct wi_scan_header ws_hdr;	/* Prism2 header */
2687109323Ssam	struct wi_scan_data_p2 ws_dat;	/* Prism2 scantable*/
2688109323Ssam	struct wi_apinfo *ap;
2689109323Ssam
2690109323Ssam	off = sizeof(u_int16_t) * 2;
2691109323Ssam	memset(&ws_hdr, 0, sizeof(ws_hdr));
2692109323Ssam	switch (sc->sc_firmware_type) {
2693109323Ssam	case WI_INTERSIL:
2694109323Ssam		wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
2695109323Ssam		off += sizeof(ws_hdr);
2696109323Ssam		szbuf = sizeof(struct wi_scan_data_p2);
269777217Sphk		break;
2698109323Ssam	case WI_SYMBOL:
2699109323Ssam		szbuf = sizeof(struct wi_scan_data_p2) + 6;
270077217Sphk		break;
2701109323Ssam	case WI_LUCENT:
2702109323Ssam		szbuf = sizeof(struct wi_scan_data);
270377217Sphk		break;
2704109323Ssam	default:
2705109323Ssam		device_printf(sc->sc_dev,
2706109323Ssam			"wi_scan_result: unknown firmware type %u\n",
2707109323Ssam			sc->sc_firmware_type);
2708109323Ssam		naps = 0;
2709109323Ssam		goto done;
271077217Sphk	}
2711109323Ssam	naps = (cnt * 2 + 2 - off) / szbuf;
2712109323Ssam	if (naps > N(sc->sc_aps))
2713109323Ssam		naps = N(sc->sc_aps);
2714109323Ssam	sc->sc_naps = naps;
2715109323Ssam	/* Read Data */
2716109323Ssam	ap = sc->sc_aps;
2717109323Ssam	memset(&ws_dat, 0, sizeof(ws_dat));
2718109323Ssam	for (i = 0; i < naps; i++, ap++) {
2719109323Ssam		wi_read_bap(sc, fid, off, &ws_dat,
2720109323Ssam		    (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
2721109323Ssam		DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off,
2722109323Ssam		    ether_sprintf(ws_dat.wi_bssid)));
2723109323Ssam		off += szbuf;
2724109323Ssam		ap->scanreason = le16toh(ws_hdr.wi_reason);
2725109323Ssam		memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
2726109323Ssam		ap->channel = le16toh(ws_dat.wi_chid);
2727109323Ssam		ap->signal  = le16toh(ws_dat.wi_signal);
2728109323Ssam		ap->noise   = le16toh(ws_dat.wi_noise);
2729109323Ssam		ap->quality = ap->signal - ap->noise;
2730109323Ssam		ap->capinfo = le16toh(ws_dat.wi_capinfo);
2731109323Ssam		ap->interval = le16toh(ws_dat.wi_interval);
2732109323Ssam		ap->rate    = le16toh(ws_dat.wi_rate);
2733109323Ssam		ap->namelen = le16toh(ws_dat.wi_namelen);
2734109323Ssam		if (ap->namelen > sizeof(ap->name))
2735109323Ssam			ap->namelen = sizeof(ap->name);
2736109323Ssam		memcpy(ap->name, ws_dat.wi_name, ap->namelen);
2737109323Ssam	}
2738109323Ssamdone:
2739109323Ssam	/* Done scanning */
2740109323Ssam	sc->sc_scan_timer = 0;
2741109323Ssam	DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
2742109323Ssam#undef N
274377217Sphk}
274477217Sphk
274588546Salfredstatic void
2746109323Ssamwi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi)
274777217Sphk{
2748109323Ssam	ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr),
2749116951Ssam	    ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL : -1, rssi);
2750109323Ssam	printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n",
2751109323Ssam		le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1),
2752109323Ssam		le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence);
2753109323Ssam	printf(" rx_signal %u rx_rate %u rx_flow %u\n",
2754109323Ssam		wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow);
2755109323Ssam	printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n",
2756109323Ssam		wh->wi_tx_rtry, wh->wi_tx_rate,
2757109323Ssam		le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len));
2758109323Ssam	printf(" ehdr dst %6D src %6D type 0x%x\n",
2759109323Ssam		wh->wi_ehdr.ether_dhost, ":", wh->wi_ehdr.ether_shost, ":",
2760109323Ssam		wh->wi_ehdr.ether_type);
2761109323Ssam}
276277217Sphk
2763109323Ssamint
2764109323Ssamwi_alloc(device_t dev, int rid)
2765109323Ssam{
2766109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2767109323Ssam
2768109323Ssam	if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2769109323Ssam		sc->iobase_rid = rid;
2770109323Ssam		sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2771109323Ssam		    &sc->iobase_rid, 0, ~0, (1 << 6),
2772109323Ssam		    rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2773109323Ssam		if (!sc->iobase) {
2774109323Ssam			device_printf(dev, "No I/O space?!\n");
2775109323Ssam			return (ENXIO);
277698440Simp		}
2777109323Ssam
2778109323Ssam		sc->wi_io_addr = rman_get_start(sc->iobase);
2779109323Ssam		sc->wi_btag = rman_get_bustag(sc->iobase);
2780109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2781109323Ssam	} else {
2782109323Ssam		sc->mem_rid = rid;
2783109323Ssam		sc->mem = bus_alloc_resource(dev, SYS_RES_MEMORY,
2784109323Ssam		    &sc->mem_rid, 0, ~0, 1, RF_ACTIVE);
2785109323Ssam
2786109323Ssam		if (!sc->mem) {
2787109323Ssam			device_printf(dev, "No Mem space on prism2.5?\n");
2788109323Ssam			return (ENXIO);
278977217Sphk		}
2790109323Ssam
2791109323Ssam		sc->wi_btag = rman_get_bustag(sc->mem);
2792109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->mem);
279377217Sphk	}
279477217Sphk
2795109323Ssam
2796109323Ssam	sc->irq_rid = 0;
2797109323Ssam	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
2798109323Ssam	    0, ~0, 1, RF_ACTIVE |
2799109323Ssam	    ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2800109323Ssam
2801109323Ssam	if (!sc->irq) {
2802109323Ssam		wi_free(dev);
2803109323Ssam		device_printf(dev, "No irq?!\n");
2804109323Ssam		return (ENXIO);
280577217Sphk	}
2806109323Ssam
2807109323Ssam	sc->sc_dev = dev;
2808109323Ssam	sc->sc_unit = device_get_unit(dev);
2809109323Ssam
2810109323Ssam	return (0);
281177217Sphk}
281293359Simp
2813109323Ssamvoid
2814109323Ssamwi_free(device_t dev)
2815109323Ssam{
2816109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2817109323Ssam
2818109323Ssam	if (sc->iobase != NULL) {
2819109323Ssam		bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2820109323Ssam		sc->iobase = NULL;
2821109323Ssam	}
2822109323Ssam	if (sc->irq != NULL) {
2823109323Ssam		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2824109323Ssam		sc->irq = NULL;
2825109323Ssam	}
2826109323Ssam	if (sc->mem != NULL) {
2827109323Ssam		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
2828109323Ssam		sc->mem = NULL;
2829109323Ssam	}
2830109323Ssam
2831109323Ssam	return;
2832109323Ssam}
2833109323Ssam
283493359Simpstatic int
2835109323Ssamwi_get_debug(struct wi_softc *sc, struct wi_req *wreq)
283693359Simp{
2837109323Ssam	int error = 0;
283893359Simp
283993359Simp	wreq->wi_len = 1;
284093359Simp
284193359Simp	switch (wreq->wi_type) {
284293359Simp	case WI_DEBUG_SLEEP:
284393359Simp		wreq->wi_len++;
284493359Simp		wreq->wi_val[0] = sc->wi_debug.wi_sleep;
284593359Simp		break;
284693359Simp	case WI_DEBUG_DELAYSUPP:
284793359Simp		wreq->wi_len++;
284893359Simp		wreq->wi_val[0] = sc->wi_debug.wi_delaysupp;
284993359Simp		break;
285093359Simp	case WI_DEBUG_TXSUPP:
285193359Simp		wreq->wi_len++;
285293359Simp		wreq->wi_val[0] = sc->wi_debug.wi_txsupp;
285393359Simp		break;
285493359Simp	case WI_DEBUG_MONITOR:
285593359Simp		wreq->wi_len++;
285693359Simp		wreq->wi_val[0] = sc->wi_debug.wi_monitor;
285793359Simp		break;
285893359Simp	case WI_DEBUG_LEDTEST:
285993359Simp		wreq->wi_len += 3;
286093359Simp		wreq->wi_val[0] = sc->wi_debug.wi_ledtest;
286193359Simp		wreq->wi_val[1] = sc->wi_debug.wi_ledtest_param0;
286293359Simp		wreq->wi_val[2] = sc->wi_debug.wi_ledtest_param1;
286393359Simp		break;
286493359Simp	case WI_DEBUG_CONTTX:
286593359Simp		wreq->wi_len += 2;
286693359Simp		wreq->wi_val[0] = sc->wi_debug.wi_conttx;
286793359Simp		wreq->wi_val[1] = sc->wi_debug.wi_conttx_param0;
286893359Simp		break;
286993359Simp	case WI_DEBUG_CONTRX:
287093359Simp		wreq->wi_len++;
287193359Simp		wreq->wi_val[0] = sc->wi_debug.wi_contrx;
287293359Simp		break;
287393359Simp	case WI_DEBUG_SIGSTATE:
287493359Simp		wreq->wi_len += 2;
287593359Simp		wreq->wi_val[0] = sc->wi_debug.wi_sigstate;
287693359Simp		wreq->wi_val[1] = sc->wi_debug.wi_sigstate_param0;
287793359Simp		break;
287893359Simp	case WI_DEBUG_CONFBITS:
287993359Simp		wreq->wi_len += 2;
288093359Simp		wreq->wi_val[0] = sc->wi_debug.wi_confbits;
288193359Simp		wreq->wi_val[1] = sc->wi_debug.wi_confbits_param0;
288293359Simp		break;
288393359Simp	default:
288493359Simp		error = EIO;
288593359Simp		break;
288693359Simp	}
288793359Simp
288893359Simp	return (error);
288993359Simp}
289093359Simp
289193359Simpstatic int
2892109323Ssamwi_set_debug(struct wi_softc *sc, struct wi_req *wreq)
289393359Simp{
2894109323Ssam	int error = 0;
289593359Simp	u_int16_t		cmd, param0 = 0, param1 = 0;
289693359Simp
289793359Simp	switch (wreq->wi_type) {
289893359Simp	case WI_DEBUG_RESET:
289993359Simp	case WI_DEBUG_INIT:
290093359Simp	case WI_DEBUG_CALENABLE:
290193359Simp		break;
290293359Simp	case WI_DEBUG_SLEEP:
290393359Simp		sc->wi_debug.wi_sleep = 1;
290493359Simp		break;
290593359Simp	case WI_DEBUG_WAKE:
290693359Simp		sc->wi_debug.wi_sleep = 0;
290793359Simp		break;
290893359Simp	case WI_DEBUG_CHAN:
290993359Simp		param0 = wreq->wi_val[0];
291093359Simp		break;
291193359Simp	case WI_DEBUG_DELAYSUPP:
291293359Simp		sc->wi_debug.wi_delaysupp = 1;
291393359Simp		break;
291493359Simp	case WI_DEBUG_TXSUPP:
291593359Simp		sc->wi_debug.wi_txsupp = 1;
291693359Simp		break;
291793359Simp	case WI_DEBUG_MONITOR:
291893359Simp		sc->wi_debug.wi_monitor = 1;
291993359Simp		break;
292093359Simp	case WI_DEBUG_LEDTEST:
292193359Simp		param0 = wreq->wi_val[0];
292293359Simp		param1 = wreq->wi_val[1];
292393359Simp		sc->wi_debug.wi_ledtest = 1;
292493359Simp		sc->wi_debug.wi_ledtest_param0 = param0;
292593359Simp		sc->wi_debug.wi_ledtest_param1 = param1;
292693359Simp		break;
292793359Simp	case WI_DEBUG_CONTTX:
292893359Simp		param0 = wreq->wi_val[0];
292993359Simp		sc->wi_debug.wi_conttx = 1;
293093359Simp		sc->wi_debug.wi_conttx_param0 = param0;
293193359Simp		break;
293293359Simp	case WI_DEBUG_STOPTEST:
293393359Simp		sc->wi_debug.wi_delaysupp = 0;
293493359Simp		sc->wi_debug.wi_txsupp = 0;
293593359Simp		sc->wi_debug.wi_monitor = 0;
293693359Simp		sc->wi_debug.wi_ledtest = 0;
293793359Simp		sc->wi_debug.wi_ledtest_param0 = 0;
293893359Simp		sc->wi_debug.wi_ledtest_param1 = 0;
293993359Simp		sc->wi_debug.wi_conttx = 0;
294093359Simp		sc->wi_debug.wi_conttx_param0 = 0;
294193359Simp		sc->wi_debug.wi_contrx = 0;
294293359Simp		sc->wi_debug.wi_sigstate = 0;
294393359Simp		sc->wi_debug.wi_sigstate_param0 = 0;
294493359Simp		break;
294593359Simp	case WI_DEBUG_CONTRX:
294693359Simp		sc->wi_debug.wi_contrx = 1;
294793359Simp		break;
294893359Simp	case WI_DEBUG_SIGSTATE:
294993359Simp		param0 = wreq->wi_val[0];
295093359Simp		sc->wi_debug.wi_sigstate = 1;
295193359Simp		sc->wi_debug.wi_sigstate_param0 = param0;
295293359Simp		break;
295393359Simp	case WI_DEBUG_CONFBITS:
295493359Simp		param0 = wreq->wi_val[0];
295593359Simp		param1 = wreq->wi_val[1];
295693359Simp		sc->wi_debug.wi_confbits = param0;
295793359Simp		sc->wi_debug.wi_confbits_param0 = param1;
295893359Simp		break;
295993359Simp	default:
296093359Simp		error = EIO;
296193359Simp		break;
296293359Simp	}
296393359Simp
296493359Simp	if (error)
296593359Simp		return (error);
296693359Simp
296793359Simp	cmd = WI_CMD_DEBUG | (wreq->wi_type << 8);
296893359Simp	error = wi_cmd(sc, cmd, param0, param1, 0);
296993359Simp
297093359Simp	return (error);
297193359Simp}
2972101903Simp
2973105076Simp#if __FreeBSD_version >= 500000
2974101903Simp/*
2975101903Simp * Special routines to download firmware for Symbol CF card.
2976101903Simp * XXX: This should be modified generic into any PRISM-2 based card.
2977101903Simp */
2978101903Simp
2979101903Simp#define	WI_SBCF_PDIADDR		0x3100
2980101903Simp
2981101903Simp/* unaligned load little endian */
2982101903Simp#define	GETLE32(p)	((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24))
2983101903Simp#define	GETLE16(p)	((p)[0] | ((p)[1]<<8))
2984101903Simp
2985101903Simpint
2986101903Simpwi_symbol_load_firm(struct wi_softc *sc, const void *primsym, int primlen,
2987101903Simp    const void *secsym, int seclen)
2988101903Simp{
2989101903Simp	uint8_t ebuf[256];
2990101903Simp	int i;
2991101903Simp
2992101903Simp	/* load primary code and run it */
2993101903Simp	wi_symbol_set_hcr(sc, WI_HCR_EEHOLD);
2994101903Simp	if (wi_symbol_write_firm(sc, primsym, primlen, NULL, 0))
2995101903Simp		return EIO;
2996101903Simp	wi_symbol_set_hcr(sc, WI_HCR_RUN);
2997101903Simp	for (i = 0; ; i++) {
2998101903Simp		if (i == 10)
2999101903Simp			return ETIMEDOUT;
3000101903Simp		tsleep(sc, PWAIT, "wiinit", 1);
3001101903Simp		if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT)
3002101903Simp			break;
3003101903Simp		/* write the magic key value to unlock aux port */
3004101903Simp		CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0);
3005101903Simp		CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1);
3006101903Simp		CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2);
3007101903Simp		CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL);
3008101903Simp	}
3009101903Simp
3010101903Simp	/* issue read EEPROM command: XXX copied from wi_cmd() */
3011101903Simp	CSR_WRITE_2(sc, WI_PARAM0, 0);
3012101903Simp	CSR_WRITE_2(sc, WI_PARAM1, 0);
3013101903Simp	CSR_WRITE_2(sc, WI_PARAM2, 0);
3014101903Simp	CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE);
3015101903Simp        for (i = 0; i < WI_TIMEOUT; i++) {
3016101903Simp                if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
3017101903Simp                        break;
3018101903Simp                DELAY(1);
3019101903Simp        }
3020101903Simp        CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
3021101903Simp
3022101903Simp	CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ);
3023101903Simp	CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ);
3024101903Simp	CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA,
3025101903Simp	    (uint16_t *)ebuf, sizeof(ebuf) / 2);
3026101903Simp	if (GETLE16(ebuf) > sizeof(ebuf))
3027101903Simp		return EIO;
3028101903Simp	if (wi_symbol_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf)))
3029101903Simp		return EIO;
3030101903Simp	return 0;
3031101903Simp}
3032101903Simp
3033101903Simpstatic int
3034101903Simpwi_symbol_write_firm(struct wi_softc *sc, const void *buf, int buflen,
3035101903Simp    const void *ebuf, int ebuflen)
3036101903Simp{
3037101903Simp	const uint8_t *p, *ep, *q, *eq;
3038101903Simp	char *tp;
3039101903Simp	uint32_t addr, id, eid;
3040101903Simp	int i, len, elen, nblk, pdrlen;
3041101903Simp
3042101903Simp	/*
3043101903Simp	 * Parse the header of the firmware image.
3044101903Simp	 */
3045101903Simp	p = buf;
3046101903Simp	ep = p + buflen;
3047101903Simp	while (p < ep && *p++ != ' ');	/* FILE: */
3048101903Simp	while (p < ep && *p++ != ' ');	/* filename */
3049101903Simp	while (p < ep && *p++ != ' ');	/* type of the firmware */
3050101903Simp	nblk = strtoul(p, &tp, 10);
3051101903Simp	p = tp;
3052101903Simp	pdrlen = strtoul(p + 1, &tp, 10);
3053101903Simp	p = tp;
3054101903Simp	while (p < ep && *p++ != 0x1a);	/* skip rest of header */
3055101903Simp
3056101903Simp	/*
3057101903Simp	 * Block records: address[4], length[2], data[length];
3058101903Simp	 */
3059101903Simp	for (i = 0; i < nblk; i++) {
3060101903Simp		addr = GETLE32(p);	p += 4;
3061101903Simp		len  = GETLE16(p);	p += 2;
3062101903Simp		CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3063101903Simp		CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3064101903Simp		CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3065101903Simp		    (const uint16_t *)p, len / 2);
3066101903Simp		p += len;
3067101903Simp	}
3068101903Simp
3069101903Simp	/*
3070101903Simp	 * PDR: id[4], address[4], length[4];
3071101903Simp	 */
3072101903Simp	for (i = 0; i < pdrlen; ) {
3073101903Simp		id   = GETLE32(p);	p += 4; i += 4;
3074101903Simp		addr = GETLE32(p);	p += 4; i += 4;
3075101903Simp		len  = GETLE32(p);	p += 4; i += 4;
3076101903Simp		/* replace PDR entry with the values from EEPROM, if any */
3077101903Simp		for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) {
3078101903Simp			elen = GETLE16(q);	q += 2;
3079101903Simp			eid  = GETLE16(q);	q += 2;
3080101903Simp			elen--;		/* elen includes eid */
3081101903Simp			if (eid == 0)
3082101903Simp				break;
3083101903Simp			if (eid != id)
3084101903Simp				continue;
3085101903Simp			CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3086101903Simp			CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3087101903Simp			CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3088101903Simp			    (const uint16_t *)q, len / 2);
3089101903Simp			break;
3090101903Simp		}
3091101903Simp	}
3092101903Simp	return 0;
3093101903Simp}
3094101903Simp
3095101903Simpstatic int
3096101903Simpwi_symbol_set_hcr(struct wi_softc *sc, int mode)
3097101903Simp{
3098101903Simp	uint16_t hcr;
3099101903Simp
3100101903Simp	CSR_WRITE_2(sc, WI_COR, WI_COR_RESET);
3101101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3102101903Simp	hcr = CSR_READ_2(sc, WI_HCR);
3103101903Simp	hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE);
3104101903Simp	CSR_WRITE_2(sc, WI_HCR, hcr);
3105101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3106101903Simp	CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE);
3107101903Simp	tsleep(sc, PWAIT, "wiinit", 1);
3108101903Simp	return 0;
3109101903Simp}
3110105076Simp#endif
3111