if_wi.c revision 178957
1139749Simp/*-
246492Swpaul * Copyright (c) 1997, 1998, 1999
346492Swpaul *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
446492Swpaul *
546492Swpaul * Redistribution and use in source and binary forms, with or without
646492Swpaul * modification, are permitted provided that the following conditions
746492Swpaul * are met:
846492Swpaul * 1. Redistributions of source code must retain the above copyright
946492Swpaul *    notice, this list of conditions and the following disclaimer.
1046492Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1146492Swpaul *    notice, this list of conditions and the following disclaimer in the
1246492Swpaul *    documentation and/or other materials provided with the distribution.
1346492Swpaul * 3. All advertising materials mentioning features or use of this software
1446492Swpaul *    must display the following acknowledgement:
1546492Swpaul *	This product includes software developed by Bill Paul.
1646492Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1746492Swpaul *    may be used to endorse or promote products derived from this software
1846492Swpaul *    without specific prior written permission.
1946492Swpaul *
2046492Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2146492Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2246492Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2346492Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2446492Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2546492Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2646492Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2746492Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2846492Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2946492Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3046492Swpaul * THE POSSIBILITY OF SUCH DAMAGE.
3146492Swpaul */
3246492Swpaul
3346492Swpaul/*
34109323Ssam * Lucent WaveLAN/IEEE 802.11 PCMCIA driver.
3546492Swpaul *
36109323Ssam * Original FreeBSD driver written by Bill Paul <wpaul@ctr.columbia.edu>
3746492Swpaul * Electrical Engineering Department
3846492Swpaul * Columbia University, New York City
3946492Swpaul */
4046492Swpaul
4146492Swpaul/*
4247401Swpaul * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
4346492Swpaul * from Lucent. Unlike the older cards, the new ones are programmed
4446492Swpaul * entirely via a firmware-driven controller called the Hermes.
4546492Swpaul * Unfortunately, Lucent will not release the Hermes programming manual
4646492Swpaul * without an NDA (if at all). What they do release is an API library
4746492Swpaul * called the HCF (Hardware Control Functions) which is supposed to
4846492Swpaul * do the device-specific operations of a device driver for you. The
4946492Swpaul * publically available version of the HCF library (the 'HCF Light') is
5047401Swpaul * a) extremely gross, b) lacks certain features, particularly support
5146492Swpaul * for 802.11 frames, and c) is contaminated by the GNU Public License.
5246492Swpaul *
5346492Swpaul * This driver does not use the HCF or HCF Light at all. Instead, it
5446492Swpaul * programs the Hermes controller directly, using information gleaned
5546492Swpaul * from the HCF Light code and corresponding documentation.
5646492Swpaul *
5795534Simp * This driver supports the ISA, PCMCIA and PCI versions of the Lucent
5895534Simp * WaveLan cards (based on the Hermes chipset), as well as the newer
5995534Simp * Prism 2 chipsets with firmware from Intersil and Symbol.
6046492Swpaul */
6146492Swpaul
62113038Sobrien#include <sys/cdefs.h>
63113038Sobrien__FBSDID("$FreeBSD: head/sys/dev/wi/if_wi.c 178957 2008-05-12 00:15:30Z sam $");
64113038Sobrien
65109323Ssam#define WI_HERMES_STATS_WAR	/* Work around stats counter bug. */
66109323Ssam
6746492Swpaul#include <sys/param.h>
6846492Swpaul#include <sys/systm.h>
6995533Smike#include <sys/endian.h>
7046492Swpaul#include <sys/sockio.h>
7146492Swpaul#include <sys/mbuf.h>
72164033Srwatson#include <sys/priv.h>
7383366Sjulian#include <sys/proc.h>
7446492Swpaul#include <sys/kernel.h>
7546492Swpaul#include <sys/socket.h>
7653702Swpaul#include <sys/module.h>
7753702Swpaul#include <sys/bus.h>
7894486Simp#include <sys/random.h>
7953702Swpaul#include <sys/syslog.h>
8053702Swpaul#include <sys/sysctl.h>
81178354Ssam#include <sys/taskqueue.h>
8246492Swpaul
8353702Swpaul#include <machine/bus.h>
8453702Swpaul#include <machine/resource.h>
85116951Ssam#include <machine/atomic.h>
8653702Swpaul#include <sys/rman.h>
8753702Swpaul
8846492Swpaul#include <net/if.h>
8946492Swpaul#include <net/if_arp.h>
9046492Swpaul#include <net/ethernet.h>
9146492Swpaul#include <net/if_dl.h>
9246492Swpaul#include <net/if_media.h>
9346492Swpaul#include <net/if_types.h>
9446492Swpaul
95116951Ssam#include <net80211/ieee80211_var.h>
96116951Ssam#include <net80211/ieee80211_ioctl.h>
97119784Ssam#include <net80211/ieee80211_radiotap.h>
98116951Ssam
9946492Swpaul#include <netinet/in.h>
10046492Swpaul#include <netinet/in_systm.h>
10146492Swpaul#include <netinet/in_var.h>
10246492Swpaul#include <netinet/ip.h>
10346492Swpaul#include <netinet/if_ether.h>
10446492Swpaul
10546492Swpaul#include <net/bpf.h>
10646492Swpaul
10770808Speter#include <dev/wi/if_wavelan_ieee.h>
108119784Ssam#include <dev/wi/if_wireg.h>
10993611Simp#include <dev/wi/if_wivar.h>
11046492Swpaul
111178354Ssamstatic struct ieee80211vap *wi_vap_create(struct ieee80211com *ic,
112178354Ssam		const char name[IFNAMSIZ], int unit, int opmode, int flags,
113178354Ssam		const uint8_t bssid[IEEE80211_ADDR_LEN],
114178354Ssam		const uint8_t mac[IEEE80211_ADDR_LEN]);
115178354Ssamstatic void wi_vap_delete(struct ieee80211vap *vap);
116178354Ssamstatic void wi_stop_locked(struct wi_softc *sc, int disable);
117165087Ssamstatic void wi_start_locked(struct ifnet *);
11891693Simpstatic void wi_start(struct ifnet *);
119160991Ssamstatic int  wi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr,
120160991Ssam		struct mbuf *m0);
121160991Ssamstatic int  wi_raw_xmit(struct ieee80211_node *, struct mbuf *,
122160991Ssam		const struct ieee80211_bpf_params *);
123178354Ssamstatic int  wi_newstate_sta(struct ieee80211vap *, enum ieee80211_state, int);
124178354Ssamstatic int  wi_newstate_hostap(struct ieee80211vap *, enum ieee80211_state, int);
125178354Ssamstatic void wi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
126178354Ssam		int subtype, int rssi, int noise, u_int32_t rstamp);
127178354Ssamstatic int  wi_reset(struct wi_softc *);
128165089Ssamstatic void wi_watchdog(void *);
129109323Ssamstatic int  wi_ioctl(struct ifnet *, u_long, caddr_t);
130109323Ssamstatic void wi_media_status(struct ifnet *, struct ifmediareq *);
13146492Swpaul
132109323Ssamstatic void wi_rx_intr(struct wi_softc *);
133109323Ssamstatic void wi_tx_intr(struct wi_softc *);
134109323Ssamstatic void wi_tx_ex_intr(struct wi_softc *);
135178354Ssam
136178354Ssamstatic void wi_status_connected(void *, int);
137178354Ssamstatic void wi_status_disconnected(void *, int);
138178354Ssamstatic void wi_status_oor(void *, int);
139178354Ssamstatic void wi_status_assoc_failed(void *, int);
140109323Ssamstatic void wi_info_intr(struct wi_softc *);
14146492Swpaul
142178354Ssamstatic int  wi_write_txrate(struct wi_softc *, struct ieee80211vap *);
143178354Ssamstatic int  wi_write_wep(struct wi_softc *, struct ieee80211vap *);
144109323Ssamstatic int  wi_write_multi(struct wi_softc *);
145178354Ssamstatic void wi_update_mcast(struct ifnet *);
146109323Ssamstatic int  wi_alloc_fid(struct wi_softc *, int, int *);
147109323Ssamstatic void wi_read_nicid(struct wi_softc *);
148109323Ssamstatic int  wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
14953702Swpaul
150109323Ssamstatic int  wi_cmd(struct wi_softc *, int, int, int, int);
151109323Ssamstatic int  wi_seek_bap(struct wi_softc *, int, int);
152109323Ssamstatic int  wi_read_bap(struct wi_softc *, int, int, void *, int);
153109323Ssamstatic int  wi_write_bap(struct wi_softc *, int, int, void *, int);
154109323Ssamstatic int  wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
155109323Ssamstatic int  wi_read_rid(struct wi_softc *, int, void *, int *);
156109323Ssamstatic int  wi_write_rid(struct wi_softc *, int, void *, int);
157178354Ssamstatic int  wi_write_appie(struct wi_softc *, int, const struct ieee80211_appie *);
15877217Sphk
159170530Ssamstatic void wi_scan_start(struct ieee80211com *);
160170530Ssamstatic void wi_scan_end(struct ieee80211com *);
161170530Ssamstatic void wi_set_channel(struct ieee80211com *);
162170530Ssam
163109323Ssamstatic __inline int
164109323Ssamwi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
165109323Ssam{
16653702Swpaul
167109323Ssam	val = htole16(val);
168109323Ssam	return wi_write_rid(sc, rid, &val, sizeof(val));
169109323Ssam}
170109323Ssam
171109592SsamSYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters");
172109592Ssam
173109323Ssamstatic	struct timeval lasttxerror;	/* time of last tx error msg */
174109323Ssamstatic	int curtxeps;			/* current tx error msgs/sec */
175111559Ssamstatic	int wi_txerate = 0;		/* tx error rate: max msgs/sec */
176109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate,
177111559Ssam	    0, "max tx error msgs/sec; 0 to disable msgs");
178109323Ssam
179109323Ssam#define	WI_DEBUG
180109323Ssam#ifdef WI_DEBUG
181109323Ssamstatic	int wi_debug = 0;
182109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug,
183109592Ssam	    0, "control debugging printfs");
184109323Ssam#define	DPRINTF(X)	if (wi_debug) printf X
185109323Ssam#else
186109323Ssam#define	DPRINTF(X)
187109323Ssam#endif
188109323Ssam
189109323Ssam#define WI_INTRS	(WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
190109323Ssam
19193825Simpstruct wi_card_ident wi_card_ident[] = {
19293825Simp	/* CARD_ID			CARD_NAME		FIRM_TYPE */
19393825Simp	{ WI_NIC_LUCENT_ID,		WI_NIC_LUCENT_STR,	WI_LUCENT },
19493825Simp	{ WI_NIC_SONY_ID,		WI_NIC_SONY_STR,	WI_LUCENT },
19593825Simp	{ WI_NIC_LUCENT_EMB_ID,		WI_NIC_LUCENT_EMB_STR,	WI_LUCENT },
19693825Simp	{ WI_NIC_EVB2_ID,		WI_NIC_EVB2_STR,	WI_INTERSIL },
19793825Simp	{ WI_NIC_HWB3763_ID,		WI_NIC_HWB3763_STR,	WI_INTERSIL },
19893825Simp	{ WI_NIC_HWB3163_ID,		WI_NIC_HWB3163_STR,	WI_INTERSIL },
19993825Simp	{ WI_NIC_HWB3163B_ID,		WI_NIC_HWB3163B_STR,	WI_INTERSIL },
20093825Simp	{ WI_NIC_EVB3_ID,		WI_NIC_EVB3_STR,	WI_INTERSIL },
20193825Simp	{ WI_NIC_HWB1153_ID,		WI_NIC_HWB1153_STR,	WI_INTERSIL },
20293825Simp	{ WI_NIC_P2_SST_ID,		WI_NIC_P2_SST_STR,	WI_INTERSIL },
20393825Simp	{ WI_NIC_EVB2_SST_ID,		WI_NIC_EVB2_SST_STR,	WI_INTERSIL },
20493825Simp	{ WI_NIC_3842_EVA_ID,		WI_NIC_3842_EVA_STR,	WI_INTERSIL },
20593825Simp	{ WI_NIC_3842_PCMCIA_AMD_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
20693825Simp	{ WI_NIC_3842_PCMCIA_SST_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
207101355Simp	{ WI_NIC_3842_PCMCIA_ATL_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
208101355Simp	{ WI_NIC_3842_PCMCIA_ATS_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
20993825Simp	{ WI_NIC_3842_MINI_AMD_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
21093825Simp	{ WI_NIC_3842_MINI_SST_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
211101355Simp	{ WI_NIC_3842_MINI_ATL_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
212101355Simp	{ WI_NIC_3842_MINI_ATS_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
21393825Simp	{ WI_NIC_3842_PCI_AMD_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
21493825Simp	{ WI_NIC_3842_PCI_SST_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
215101355Simp	{ WI_NIC_3842_PCI_ATS_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
216101355Simp	{ WI_NIC_3842_PCI_ATL_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
21793825Simp	{ WI_NIC_P3_PCMCIA_AMD_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
21893825Simp	{ WI_NIC_P3_PCMCIA_SST_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
219101355Simp	{ WI_NIC_P3_PCMCIA_ATL_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
220101355Simp	{ WI_NIC_P3_PCMCIA_ATS_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
22193825Simp	{ WI_NIC_P3_MINI_AMD_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
22293825Simp	{ WI_NIC_P3_MINI_SST_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
223101355Simp	{ WI_NIC_P3_MINI_ATL_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
224101355Simp	{ WI_NIC_P3_MINI_ATS_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
22593825Simp	{ 0,	NULL,	0 },
22693825Simp};
22793825Simp
228109323Ssamdevclass_t wi_devclass;
22946492Swpaul
23093611Simpint
231109323Ssamwi_attach(device_t dev)
23274906Salfred{
233109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
234178354Ssam	struct ieee80211com *ic;
235147256Sbrooks	struct ifnet *ifp;
236116951Ssam	int i, nrates, buflen;
237109323Ssam	u_int16_t val;
238109323Ssam	u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
239116951Ssam	struct ieee80211_rateset *rs;
240109323Ssam	static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
241109323Ssam		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
242109323Ssam	};
243109323Ssam	int error;
24474906Salfred
245178354Ssam	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
246147256Sbrooks	if (ifp == NULL) {
247147256Sbrooks		device_printf(dev, "can not if_alloc\n");
248147256Sbrooks		wi_free(dev);
249178354Ssam		return ENOSPC;
250147256Sbrooks	}
251178354Ssam	ic = ifp->if_l2com;
252147256Sbrooks
253109323Ssam	/*
254109323Ssam	 * NB: no locking is needed here; don't put it here
255109323Ssam	 *     unless you can prove it!
256109323Ssam	 */
257121697Ssam	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
258166901Spiso	    NULL, wi_intr, sc, &sc->wi_intrhand);
25953702Swpaul
26053702Swpaul	if (error) {
26153702Swpaul		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
26253702Swpaul		wi_free(dev);
263178354Ssam		return error;
26453702Swpaul	}
26553702Swpaul
266138571Ssam	sc->sc_firmware_type = WI_NOTYPE;
267123339Simp	sc->wi_cmd_count = 500;
26846492Swpaul	/* Reset the NIC. */
269178354Ssam	if (wi_reset(sc) != 0) {
270178354Ssam		wi_free(dev);
271109323Ssam		return ENXIO;		/* XXX */
272178354Ssam	}
27346492Swpaul
274178354Ssam	/* Read NIC identification */
275178354Ssam	wi_read_nicid(sc);
276178354Ssam	switch (sc->sc_firmware_type) {
277178354Ssam	case WI_LUCENT:
278178354Ssam		if (sc->sc_sta_firmware_ver < 60006)
279178354Ssam			goto reject;
280178354Ssam		break;
281178354Ssam	case WI_INTERSIL:
282178354Ssam		if (sc->sc_sta_firmware_ver < 800)
283178354Ssam			goto reject;
284178354Ssam		break;
285178354Ssam	default:
286178354Ssam	reject:
287178354Ssam		device_printf(dev, "Sorry, this card is not supported "
288178354Ssam		    "(type %d, firmware ver %d)\n",
289178354Ssam		    sc->sc_firmware_type, sc->sc_sta_firmware_ver);
290178354Ssam		wi_free(dev);
291178354Ssam		return EOPNOTSUPP;
292178354Ssam	}
293178354Ssam
294178354Ssam	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
295178354Ssam	    MTX_DEF | MTX_RECURSE);
296178354Ssam	callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
297178354Ssam
29876438Swpaul	/*
29976438Swpaul	 * Read the station address.
30076438Swpaul	 * And do it twice. I've seen PRISM-based cards that return
30176438Swpaul	 * an error when trying to read it the first time, which causes
30276438Swpaul	 * the probe to fail.
30376438Swpaul	 */
304109323Ssam	buflen = IEEE80211_ADDR_LEN;
305109323Ssam	error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
306109323Ssam	if (error != 0) {
307109323Ssam		buflen = IEEE80211_ADDR_LEN;
308109323Ssam		error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
309109323Ssam	}
310109323Ssam	if (error || IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) {
311109323Ssam		if (error != 0)
312109323Ssam			device_printf(dev, "mac read failed %d\n", error);
313148714Simp		else {
314109323Ssam			device_printf(dev, "mac read failed (all zeros)\n");
315148714Simp			error = ENXIO;
316148714Simp		}
31775149Simp		wi_free(dev);
31875149Simp		return (error);
31975149Simp	}
32046492Swpaul
321178354Ssam	ifp->if_softc = sc;
322121816Sbrooks	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
32346492Swpaul	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
32446492Swpaul	ifp->if_ioctl = wi_ioctl;
32546492Swpaul	ifp->if_start = wi_start;
32646492Swpaul	ifp->if_init = wi_init;
327132986Smlaier	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
328132986Smlaier	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
329132986Smlaier	IFQ_SET_READY(&ifp->if_snd);
33046492Swpaul
331138571Ssam	ic->ic_ifp = ifp;
332109323Ssam	ic->ic_phytype = IEEE80211_T_DS;
333109323Ssam	ic->ic_opmode = IEEE80211_M_STA;
334178957Ssam	ic->ic_caps = IEEE80211_C_STA
335178957Ssam		    | IEEE80211_C_PMGT
336178354Ssam		    | IEEE80211_C_MONITOR
337138571Ssam		    ;
33846492Swpaul
339116951Ssam	/*
340116951Ssam	 * Query the card for available channels and setup the
341116951Ssam	 * channel table.  We assume these are all 11b channels.
342116951Ssam	 */
343109323Ssam	buflen = sizeof(val);
344109323Ssam	if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
345109323Ssam		val = htole16(0x1fff);	/* assume 1-11 */
346116951Ssam	KASSERT(val != 0, ("wi_attach: no available channels listed!"));
347116951Ssam
348116951Ssam	val <<= 1;			/* shift for base 1 indices */
349116951Ssam	for (i = 1; i < 16; i++) {
350170530Ssam		struct ieee80211_channel *c;
351170530Ssam
352144986Smdodd		if (!isset((u_int8_t*)&val, i))
353144986Smdodd			continue;
354170530Ssam		c = &ic->ic_channels[ic->ic_nchans++];
355170530Ssam		c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
356170530Ssam		c->ic_flags = IEEE80211_CHAN_B;
357170530Ssam		c->ic_ieee = i;
358178354Ssam		/* XXX txpowers? */
359109323Ssam	}
36046492Swpaul
36146563Swpaul	/*
36298440Simp	 * Set flags based on firmware version.
36398440Simp	 */
36498440Simp	switch (sc->sc_firmware_type) {
36598440Simp	case WI_LUCENT:
366112363Simp		sc->sc_ntxbuf = 1;
367178354Ssam		ic->ic_caps |= IEEE80211_C_IBSS;
368119784Ssam
369178354Ssam		sc->sc_ibss_port = WI_PORTTYPE_BSS;
370178354Ssam		sc->sc_monitor_port = WI_PORTTYPE_ADHOC;
371119784Ssam		sc->sc_min_rssi = WI_LUCENT_MIN_RSSI;
372119784Ssam		sc->sc_max_rssi = WI_LUCENT_MAX_RSSI;
373119784Ssam		sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
37498440Simp		break;
37598440Simp	case WI_INTERSIL:
376112363Simp		sc->sc_ntxbuf = WI_NTXBUF;
377178354Ssam		sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR
378178354Ssam			     |  WI_FLAGS_HAS_ROAMING;
379123339Simp		/*
380123339Simp		 * Old firmware are slow, so give peace a chance.
381123339Simp		 */
382123339Simp		if (sc->sc_sta_firmware_ver < 10000)
383123339Simp			sc->wi_cmd_count = 5000;
384109323Ssam		if (sc->sc_sta_firmware_ver > 10101)
385109323Ssam			sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
386178354Ssam		ic->ic_caps |= IEEE80211_C_IBSS;
387109396Simp		/*
388109396Simp		 * version 0.8.3 and newer are the only ones that are known
389109396Simp		 * to currently work.  Earlier versions can be made to work,
390178354Ssam		 * at least according to the Linux driver but we require
391178354Ssam		 * monitor mode so this is irrelevant.
392109396Simp		 */
393178354Ssam		ic->ic_caps |= IEEE80211_C_HOSTAP;
394178354Ssam		if (sc->sc_sta_firmware_ver >= 10603)
395178354Ssam			sc->sc_flags |= WI_FLAGS_HAS_ENHSECURITY;
396178354Ssam		if (sc->sc_sta_firmware_ver >= 10700) {
397178354Ssam			/*
398178354Ssam			 * 1.7.0+ have the necessary support for sta mode WPA.
399178354Ssam			 */
400178354Ssam			sc->sc_flags |= WI_FLAGS_HAS_WPASUPPORT;
401178354Ssam			ic->ic_caps |= IEEE80211_C_WPA;
402178354Ssam		}
403119784Ssam
404178354Ssam		sc->sc_ibss_port = WI_PORTTYPE_IBSS;
405178354Ssam		sc->sc_monitor_port = WI_PORTTYPE_APSILENT;
406119784Ssam		sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
407119784Ssam		sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
408119784Ssam		sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
40998440Simp		break;
41098440Simp	}
41198440Simp
41298440Simp	/*
41356965Swpaul	 * Find out if we support WEP on this card.
41456965Swpaul	 */
415109323Ssam	buflen = sizeof(val);
416109323Ssam	if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
417109323Ssam	    val != htole16(0))
418178354Ssam		ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
41956965Swpaul
420109323Ssam	/* Find supported rates. */
421109323Ssam	buflen = sizeof(ratebuf);
422116951Ssam	rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
423109323Ssam	if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
424116951Ssam		nrates = le16toh(*(u_int16_t *)ratebuf);
425116951Ssam		if (nrates > IEEE80211_RATE_MAXSIZE)
426116951Ssam			nrates = IEEE80211_RATE_MAXSIZE;
427116951Ssam		rs->rs_nrates = 0;
428116951Ssam		for (i = 0; i < nrates; i++)
429116951Ssam			if (ratebuf[2+i])
430116951Ssam				rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i];
431109323Ssam	} else {
432109323Ssam		/* XXX fallback on error? */
43398440Simp	}
43477217Sphk
435109323Ssam	buflen = sizeof(val);
436109323Ssam	if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
437109323Ssam	    wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
438119784Ssam		sc->sc_dbm_offset = le16toh(val);
439119784Ssam	}
44046492Swpaul
441109323Ssam	sc->sc_portnum = WI_DEFAULT_PORT;
442178354Ssam	TASK_INIT(&sc->sc_oor_task, 0, wi_status_oor, ic);
44387383Simp
444138571Ssam	ieee80211_ifattach(ic);
445160991Ssam	ic->ic_raw_xmit = wi_raw_xmit;
446170530Ssam	ic->ic_scan_start = wi_scan_start;
447170530Ssam	ic->ic_scan_end = wi_scan_end;
448170530Ssam	ic->ic_set_channel = wi_set_channel;
449170530Ssam
450178354Ssam	ic->ic_vap_create = wi_vap_create;
451178354Ssam	ic->ic_vap_delete = wi_vap_delete;
452178354Ssam	ic->ic_update_mcast = wi_update_mcast;
453109323Ssam
454178354Ssam	bpfattach(ifp, DLT_IEEE802_11_RADIO,
455178354Ssam		sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th));
456119784Ssam	/*
457119784Ssam	 * Initialize constant fields.
458127698Ssam	 * XXX make header lengths a multiple of 32-bits so subsequent
459127698Ssam	 *     headers are properly aligned; this is a kludge to keep
460127698Ssam	 *     certain applications happy.
461119784Ssam	 *
462119784Ssam	 * NB: the channel is setup each time we transition to the
463119784Ssam	 *     RUN state to avoid filling it in for each frame.
464119784Ssam	 */
465127698Ssam	sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t));
466127698Ssam	sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len);
467127698Ssam	sc->sc_tx_th.wt_ihdr.it_present = htole32(WI_TX_RADIOTAP_PRESENT);
468119784Ssam
469127698Ssam	sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t));
470127698Ssam	sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len);
471127698Ssam	sc->sc_rx_th.wr_ihdr.it_present = htole32(WI_RX_RADIOTAP_PRESENT);
472138571Ssam
473138571Ssam	if (bootverbose)
474138571Ssam		ieee80211_announce(ic);
475138571Ssam
476109323Ssam	return (0);
47787383Simp}
47887383Simp
479109323Ssamint
480109323Ssamwi_detach(device_t dev)
48146492Swpaul{
482109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
483147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
484178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
48546492Swpaul
486109323Ssam	WI_LOCK(sc);
48746492Swpaul
488109323Ssam	/* check if device was removed */
489123098Simp	sc->wi_gone |= !bus_child_present(dev);
49046492Swpaul
491178354Ssam	wi_stop_locked(sc, 0);
492150678Sru	WI_UNLOCK(sc);
493178354Ssam	bpfdetach(ifp);
494178354Ssam	ieee80211_ifdetach(ic);
49546492Swpaul
496109323Ssam	bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
497150306Simp	if_free(sc->sc_ifp);
498109323Ssam	wi_free(dev);
499109323Ssam	mtx_destroy(&sc->sc_mtx);
500109323Ssam	return (0);
501109323Ssam}
50293359Simp
503178354Ssamstatic struct ieee80211vap *
504178354Ssamwi_vap_create(struct ieee80211com *ic,
505178354Ssam	const char name[IFNAMSIZ], int unit, int opmode, int flags,
506178354Ssam	const uint8_t bssid[IEEE80211_ADDR_LEN],
507178354Ssam	const uint8_t mac[IEEE80211_ADDR_LEN])
508109323Ssam{
509178354Ssam	struct wi_softc *sc = ic->ic_ifp->if_softc;
510178354Ssam	struct wi_vap *wvp;
511178354Ssam	struct ieee80211vap *vap;
51293359Simp
513178354Ssam	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
514178354Ssam		return NULL;
515178354Ssam	wvp = (struct wi_vap *) malloc(sizeof(struct wi_vap),
516178354Ssam	    M_80211_VAP, M_NOWAIT | M_ZERO);
517178354Ssam	if (wvp == NULL)
518178354Ssam		return NULL;
51993359Simp
520178354Ssam	vap = &wvp->wv_vap;
521178354Ssam	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
52293359Simp
523178354Ssam	vap->iv_max_aid = WI_MAX_AID;
52493359Simp
525178354Ssam	switch (opmode) {
526178354Ssam	case IEEE80211_M_STA:
527178354Ssam		sc->sc_porttype = WI_PORTTYPE_BSS;
528178354Ssam		wvp->wv_newstate = vap->iv_newstate;
529178354Ssam		vap->iv_newstate = wi_newstate_sta;
530178354Ssam		/* need to filter mgt frames to avoid confusing state machine */
531178354Ssam		wvp->wv_recv_mgmt = vap->iv_recv_mgmt;
532178354Ssam		vap->iv_recv_mgmt = wi_recv_mgmt;
533109323Ssam		break;
534178354Ssam	case IEEE80211_M_IBSS:
535178354Ssam		sc->sc_porttype = sc->sc_ibss_port;
536178354Ssam		wvp->wv_newstate = vap->iv_newstate;
537178354Ssam		vap->iv_newstate = wi_newstate_sta;
538109323Ssam		break;
539178354Ssam	case IEEE80211_M_AHDEMO:
540178354Ssam		sc->sc_porttype = WI_PORTTYPE_ADHOC;
541109323Ssam		break;
542178354Ssam	case IEEE80211_M_HOSTAP:
543178354Ssam		sc->sc_porttype = WI_PORTTYPE_HOSTAP;
544178354Ssam		wvp->wv_newstate = vap->iv_newstate;
545178354Ssam		vap->iv_newstate = wi_newstate_hostap;
546178354Ssam		break;
547178354Ssam	case IEEE80211_M_MONITOR:
548178354Ssam		sc->sc_porttype = sc->sc_monitor_port;
549178354Ssam		break;
550178354Ssam	default:
551178354Ssam		break;
55293359Simp	}
553178354Ssam
554178354Ssam	TASK_INIT(&wvp->wv_connected_task, 0, wi_status_connected, vap);
555178354Ssam	TASK_INIT(&wvp->wv_disconnected_task, 0, wi_status_disconnected, vap);
556178354Ssam	TASK_INIT(&wvp->wv_assoc_failed_task, 0, wi_status_assoc_failed, vap);
557178354Ssam
558178354Ssam	/* complete setup */
559178354Ssam	ieee80211_vap_attach(vap, ieee80211_media_change, wi_media_status);
560178354Ssam	ic->ic_opmode = opmode;
561178354Ssam	return vap;
56246492Swpaul}
56346492Swpaul
564178354Ssamstatic void
565178354Ssamwi_vap_delete(struct ieee80211vap *vap)
566178354Ssam{
567178354Ssam	struct wi_vap *wvp = WI_VAP(vap);
568178354Ssam
569178354Ssam	ieee80211_vap_detach(vap);
570178354Ssam	free(wvp, M_80211_VAP);
571178354Ssam}
572178354Ssam
573109323Ssamvoid
574109323Ssamwi_shutdown(device_t dev)
57546492Swpaul{
576109323Ssam	struct wi_softc *sc = device_get_softc(dev);
57746492Swpaul
578178354Ssam	wi_stop(sc, 1);
57946492Swpaul}
58046492Swpaul
581109323Ssamvoid
582109323Ssamwi_intr(void *arg)
58346492Swpaul{
584109323Ssam	struct wi_softc *sc = arg;
585147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
586112363Simp	u_int16_t status;
58746492Swpaul
588109323Ssam	WI_LOCK(sc);
58946492Swpaul
590123098Simp	if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) {
591113327Simp		CSR_WRITE_2(sc, WI_INT_EN, 0);
592123098Simp		CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
593109323Ssam		WI_UNLOCK(sc);
59446492Swpaul		return;
595109323Ssam	}
59646492Swpaul
597112363Simp	/* Disable interrupts. */
598112363Simp	CSR_WRITE_2(sc, WI_INT_EN, 0);
59946492Swpaul
600112363Simp	status = CSR_READ_2(sc, WI_EVENT_STAT);
601112363Simp	if (status & WI_EV_RX)
602112363Simp		wi_rx_intr(sc);
603112363Simp	if (status & WI_EV_ALLOC)
604112363Simp		wi_tx_intr(sc);
605112363Simp	if (status & WI_EV_TX_EXC)
606112363Simp		wi_tx_ex_intr(sc);
607112363Simp	if (status & WI_EV_INFO)
608112363Simp		wi_info_intr(sc);
609148887Srwatson	if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
610132986Smlaier	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
611165087Ssam		wi_start_locked(ifp);
61246492Swpaul
613112363Simp	/* Re-enable interrupts. */
614112363Simp	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
61546492Swpaul
616109323Ssam	WI_UNLOCK(sc);
61746492Swpaul
61846492Swpaul	return;
61946492Swpaul}
62046492Swpaul
621178354Ssamstatic void
622178354Ssamwi_enable(struct wi_softc *sc)
623178354Ssam{
624178354Ssam	/* Enable interrupts */
625178354Ssam	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
626178354Ssam
627178354Ssam	/* enable port */
628178354Ssam	wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
629178354Ssam	sc->sc_enabled = 1;
630178354Ssam}
631178354Ssam
632178354Ssamstatic int
633178354Ssamwi_setup_locked(struct wi_softc *sc, int porttype, int mode,
634178354Ssam	uint8_t mac[IEEE80211_ADDR_LEN])
635178354Ssam{
636178354Ssam	int i;
637178354Ssam
638178354Ssam	wi_reset(sc);
639178354Ssam
640178354Ssam	wi_write_val(sc, WI_RID_PORTTYPE, porttype);
641178354Ssam	wi_write_val(sc, WI_RID_CREATE_IBSS, mode);
642178354Ssam	wi_write_val(sc, WI_RID_MAX_DATALEN, 2304);
643178354Ssam	/* XXX IEEE80211_BPF_NOACK wants 0 */
644178354Ssam	wi_write_val(sc, WI_RID_ALT_RETRY_CNT, 2);
645178354Ssam	if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
646178354Ssam		wi_write_val(sc, WI_RID_ROAMING_MODE, 3); /* NB: disabled */
647178354Ssam
648178354Ssam	wi_write_rid(sc, WI_RID_MAC_NODE, mac, IEEE80211_ADDR_LEN);
649178354Ssam
650178354Ssam	/* Allocate fids for the card */
651178354Ssam	sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
652178354Ssam	for (i = 0; i < sc->sc_ntxbuf; i++) {
653178354Ssam		int error = wi_alloc_fid(sc, sc->sc_buflen,
654178354Ssam		    &sc->sc_txd[i].d_fid);
655178354Ssam		if (error) {
656178354Ssam			device_printf(sc->sc_dev,
657178354Ssam			    "tx buffer allocation failed (error %u)\n",
658178354Ssam			    error);
659178354Ssam			return error;
660178354Ssam		}
661178354Ssam		sc->sc_txd[i].d_len = 0;
662178354Ssam	}
663178354Ssam	sc->sc_txcur = sc->sc_txnext = 0;
664178354Ssam
665178354Ssam	return 0;
666178354Ssam}
667178354Ssam
668178354Ssamstatic void
669178354Ssamwi_init_locked(struct wi_softc *sc)
670178354Ssam{
671178354Ssam	struct ifnet *ifp = sc->sc_ifp;
672178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
673178354Ssam	int wasenabled;
674178354Ssam
675178354Ssam	WI_LOCK_ASSERT(sc);
676178354Ssam
677178354Ssam	wasenabled = sc->sc_enabled;
678178354Ssam	if (wasenabled)
679178354Ssam		wi_stop_locked(sc, 1);
680178354Ssam
681178354Ssam	IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
682178354Ssam	if (wi_setup_locked(sc, sc->sc_porttype, 3, ic->ic_myaddr) != 0) {
683178354Ssam		if_printf(ifp, "interface not running\n");
684178354Ssam		wi_stop_locked(sc, 1);
685178354Ssam		return;
686178354Ssam	}
687178354Ssam
688178354Ssam	ifp->if_drv_flags |= IFF_DRV_RUNNING;
689178354Ssam	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
690178354Ssam
691178354Ssam	callout_reset(&sc->sc_watchdog, hz, wi_watchdog, sc);
692178354Ssam
693178354Ssam	wi_enable(sc);			/* Enable desired port */
694178354Ssam}
695178354Ssam
696109323Ssamvoid
697109323Ssamwi_init(void *arg)
69846492Swpaul{
699109323Ssam	struct wi_softc *sc = arg;
700147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
701178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
70246492Swpaul
703178354Ssam	WI_LOCK(sc);
704178354Ssam	wi_init_locked(sc);
705178354Ssam	WI_UNLOCK(sc);
70667092Swpaul
707178931Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
708178931Sthompsa		ieee80211_start_all(ic);		/* start all vap's */
709178354Ssam}
710170530Ssam
711178354Ssamstatic void
712178354Ssamwi_stop_locked(struct wi_softc *sc, int disable)
713178354Ssam{
714178354Ssam	struct ifnet *ifp = sc->sc_ifp;
71546492Swpaul
716178354Ssam	WI_LOCK_ASSERT(sc);
71746492Swpaul
718178354Ssam	if (sc->sc_enabled && !sc->wi_gone) {
719178354Ssam		CSR_WRITE_2(sc, WI_INT_EN, 0);
720178354Ssam		wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
721178354Ssam		if (disable)
722178354Ssam			sc->sc_enabled = 0;
723178354Ssam	} else if (sc->wi_gone && disable)	/* gone --> not enabled */
724178354Ssam		sc->sc_enabled = 0;
725178354Ssam
726178354Ssam	callout_stop(&sc->sc_watchdog);
727178354Ssam	sc->sc_tx_timer = 0;
728178354Ssam	sc->sc_false_syns = 0;
729178354Ssam
730178354Ssam	ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE | IFF_DRV_RUNNING);
731178354Ssam}
732178354Ssam
733178354Ssamvoid
734178354Ssamwi_stop(struct wi_softc *sc, int disable)
735178354Ssam{
736170530Ssam	WI_LOCK(sc);
737178354Ssam	wi_stop_locked(sc, disable);
738178354Ssam	WI_UNLOCK(sc);
739178354Ssam}
740170530Ssam
741178354Ssamstatic void
742178354Ssamwi_set_channel(struct ieee80211com *ic)
743178354Ssam{
744178354Ssam	struct ifnet *ifp = ic->ic_ifp;
745178354Ssam	struct wi_softc *sc = ifp->if_softc;
746160991Ssam
747178354Ssam	DPRINTF(("%s: channel %d, %sscanning\n", __func__,
748178354Ssam	    ieee80211_chan2ieee(ic, ic->ic_curchan),
749178354Ssam	    ic->ic_flags & IEEE80211_F_SCAN ? "" : "!"));
75046492Swpaul
751178354Ssam	WI_LOCK(sc);
752116951Ssam	wi_write_val(sc, WI_RID_OWN_CHNL,
753178354Ssam	    ieee80211_chan2ieee(ic, ic->ic_curchan));
75446492Swpaul
755178354Ssam	sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
756178354Ssam		htole16(ic->ic_curchan->ic_freq);
757178354Ssam	sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
758178354Ssam		htole16(ic->ic_curchan->ic_flags);
759178354Ssam	WI_UNLOCK(sc);
760178354Ssam}
76175373Salfred
762178354Ssamstatic void
763178354Ssamwi_scan_start(struct ieee80211com *ic)
764178354Ssam{
765178354Ssam	struct ifnet *ifp = ic->ic_ifp;
766178354Ssam	struct wi_softc *sc = ifp->if_softc;
767178354Ssam	struct ieee80211_scan_state *ss = ic->ic_scan;
76846492Swpaul
769178354Ssam	DPRINTF(("%s\n", __func__));
77046492Swpaul
771178354Ssam	WI_LOCK(sc);
772109323Ssam	/*
773178354Ssam	 * Switch device to monitor mode.
774109323Ssam	 */
775178354Ssam	wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_monitor_port);
776178354Ssam	if (sc->sc_firmware_type == WI_INTERSIL) {
777178354Ssam		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
778178354Ssam		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
77975373Salfred	}
780178354Ssam	/* force full dwell time to compensate for firmware overhead */
781178354Ssam	ss->ss_mindwell = ss->ss_maxdwell = msecs_to_ticks(400);
782178354Ssam	WI_UNLOCK(sc);
78346492Swpaul
784178354Ssam}
78567092Swpaul
786178354Ssamstatic void
787178354Ssamwi_scan_end(struct ieee80211com *ic)
788178354Ssam{
789178354Ssam	struct ifnet *ifp = ic->ic_ifp;
790178354Ssam	struct wi_softc *sc = ifp->if_softc;
79146492Swpaul
792178354Ssam	DPRINTF(("%s: restore port type %d\n", __func__, sc->sc_porttype));
793178354Ssam
794178354Ssam	WI_LOCK(sc);
795178354Ssam	wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_porttype);
796178354Ssam	if (sc->sc_firmware_type == WI_INTERSIL) {
797178354Ssam		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
798178354Ssam		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
79970073Swpaul	}
800178354Ssam	WI_UNLOCK(sc);
801178354Ssam}
80270073Swpaul
803178354Ssamstatic void
804178354Ssamwi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
805178354Ssam	int subtype, int rssi, int noise, u_int32_t rstamp)
806178354Ssam{
807178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
80846492Swpaul
809178354Ssam	switch (subtype) {
810178354Ssam	case IEEE80211_FC0_SUBTYPE_AUTH:
811178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
812178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
813178354Ssam		/* NB: filter frames that trigger state changes */
814178354Ssam		return;
815170530Ssam	}
816178354Ssam	WI_VAP(vap)->wv_recv_mgmt(ni, m, subtype, rssi, noise, rstamp);
817178354Ssam}
81846492Swpaul
819178354Ssamstatic int
820178354Ssamwi_newstate_sta(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
821178354Ssam{
822178354Ssam	struct ieee80211com *ic = vap->iv_ic;
823178354Ssam	struct ifnet *ifp = ic->ic_ifp;
824178354Ssam	struct ieee80211_node *bss;
825178354Ssam	struct wi_softc *sc = ifp->if_softc;
82694397Simp
827178354Ssam	DPRINTF(("%s: %s -> %s\n", __func__,
828178354Ssam		ieee80211_state_name[vap->iv_state],
829178354Ssam		ieee80211_state_name[nstate]));
830178354Ssam
831178354Ssam	if (nstate == IEEE80211_S_AUTH) {
832178354Ssam		WI_LOCK(sc);
833178354Ssam		wi_setup_locked(sc, WI_PORTTYPE_BSS, 3, vap->iv_myaddr);
834178354Ssam
835178354Ssam		if (vap->iv_flags & IEEE80211_F_PMGTON) {
836178354Ssam			wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
837178354Ssam			wi_write_val(sc, WI_RID_PM_ENABLED, 1);
838178354Ssam		}
839178354Ssam		wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold);
840178354Ssam		if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
841178354Ssam			wi_write_val(sc, WI_RID_FRAG_THRESH,
842178354Ssam			    vap->iv_fragthreshold);
843178354Ssam		wi_write_txrate(sc, vap);
844178354Ssam
845178354Ssam		bss = vap->iv_bss;
846178354Ssam		wi_write_ssid(sc, WI_RID_DESIRED_SSID, bss->ni_essid, bss->ni_esslen);
847178354Ssam		wi_write_val(sc, WI_RID_OWN_CHNL,
848178354Ssam		    ieee80211_chan2ieee(ic, bss->ni_chan));
849178354Ssam
850178354Ssam		/* Configure WEP. */
851178354Ssam		if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP)
852178354Ssam			wi_write_wep(sc, vap);
853178354Ssam		else
854178354Ssam			sc->sc_encryption = 0;
855178354Ssam
856178354Ssam		if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) &&
857178354Ssam		    (vap->iv_flags & IEEE80211_F_WPA)) {
858178354Ssam			wi_write_val(sc, WI_RID_WPA_HANDLING, 1);
859178354Ssam			if (vap->iv_appie_wpa != NULL)
860178354Ssam				wi_write_appie(sc, WI_RID_WPA_DATA,
861178354Ssam				    vap->iv_appie_wpa);
862178354Ssam		}
863178354Ssam
864178354Ssam		wi_enable(sc);		/* enable port */
865178354Ssam
866178354Ssam		/* Lucent firmware does not support the JOIN RID. */
867178354Ssam		if (sc->sc_firmware_type == WI_INTERSIL) {
868178354Ssam			struct wi_joinreq join;
869178354Ssam
870178354Ssam			memset(&join, 0, sizeof(join));
871178354Ssam			IEEE80211_ADDR_COPY(&join.wi_bssid, bss->ni_bssid);
872116951Ssam			join.wi_chan = htole16(
873178354Ssam			    ieee80211_chan2ieee(ic, bss->ni_chan));
874109323Ssam			wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
875178354Ssam		}
876178354Ssam		WI_UNLOCK(sc);
87775373Salfred
878178354Ssam		/*
879178354Ssam		 * NB: don't go through 802.11 layer, it'll send auth frame;
880178354Ssam		 * instead we drive the state machine from the link status
881178354Ssam		 * notification we get on association.
882178354Ssam		 */
883178354Ssam		vap->iv_state = nstate;
884178354Ssam		return EINPROGRESS;
88570073Swpaul	}
886178354Ssam	return WI_VAP(vap)->wv_newstate(vap, nstate, arg);
88746492Swpaul}
88846492Swpaul
889178354Ssamstatic int
890178354Ssamwi_newstate_hostap(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
89146492Swpaul{
892178354Ssam	struct ieee80211com *ic = vap->iv_ic;
893178354Ssam	struct ifnet *ifp = ic->ic_ifp;
894178354Ssam	struct ieee80211_node *bss;
895109323Ssam	struct wi_softc *sc = ifp->if_softc;
896178354Ssam	int error;
89746492Swpaul
898178354Ssam	DPRINTF(("%s: %s -> %s\n", __func__,
899178354Ssam		ieee80211_state_name[vap->iv_state],
900178354Ssam		ieee80211_state_name[nstate]));
90174998Swpaul
902178354Ssam	error = WI_VAP(vap)->wv_newstate(vap, nstate, arg);
903178354Ssam	if (error == 0 && nstate == IEEE80211_S_RUN) {
904178354Ssam		WI_LOCK(sc);
905178354Ssam		wi_setup_locked(sc, WI_PORTTYPE_HOSTAP, 0, vap->iv_myaddr);
906178354Ssam
907178354Ssam		bss = vap->iv_bss;
908178354Ssam		wi_write_ssid(sc, WI_RID_OWN_SSID,
909178354Ssam		    bss->ni_essid, bss->ni_esslen);
910178354Ssam		wi_write_val(sc, WI_RID_OWN_CHNL,
911178354Ssam		    ieee80211_chan2ieee(ic, bss->ni_chan));
912178354Ssam		wi_write_val(sc, WI_RID_BASIC_RATE, 0x3);
913178354Ssam		wi_write_val(sc, WI_RID_SUPPORT_RATE, 0xf);
914178354Ssam		wi_write_txrate(sc, vap);
915178354Ssam
916178354Ssam		wi_write_val(sc, WI_RID_OWN_BEACON_INT, bss->ni_intval);
917178354Ssam		wi_write_val(sc, WI_RID_DTIM_PERIOD, vap->iv_dtim_period);
918178354Ssam
919178354Ssam		wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold);
920178354Ssam		if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
921178354Ssam			wi_write_val(sc, WI_RID_FRAG_THRESH,
922178354Ssam			    vap->iv_fragthreshold);
923178354Ssam
924178354Ssam		if ((sc->sc_flags & WI_FLAGS_HAS_ENHSECURITY) &&
925178354Ssam		    (vap->iv_flags & IEEE80211_F_HIDESSID)) {
926178354Ssam			/*
927178354Ssam			 * bit 0 means hide SSID in beacons,
928178354Ssam			 * bit 1 means don't respond to bcast probe req
929178354Ssam			 */
930178354Ssam			wi_write_val(sc, WI_RID_ENH_SECURITY, 0x3);
93170073Swpaul		}
93270073Swpaul
933178354Ssam		if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) &&
934178354Ssam		    (vap->iv_flags & IEEE80211_F_WPA) &&
935178354Ssam		    vap->iv_appie_wpa != NULL)
936178354Ssam			wi_write_appie(sc, WI_RID_WPA_DATA, vap->iv_appie_wpa);
93746492Swpaul
938178354Ssam		wi_write_val(sc, WI_RID_PROMISC, 0);
939178354Ssam
940178354Ssam		/* Configure WEP. */
941178354Ssam		if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP)
942178354Ssam			wi_write_wep(sc, vap);
943178354Ssam		else
944178354Ssam			sc->sc_encryption = 0;
945178354Ssam
946178354Ssam		wi_enable(sc);		/* enable port */
947178354Ssam		WI_UNLOCK(sc);
948178354Ssam	}
949178354Ssam	return error;
95046492Swpaul}
95146492Swpaul
952109323Ssamstatic void
953165087Ssamwi_start_locked(struct ifnet *ifp)
95446492Swpaul{
955109323Ssam	struct wi_softc	*sc = ifp->if_softc;
956119150Ssam	struct ieee80211_node *ni;
957109323Ssam	struct ieee80211_frame *wh;
958109323Ssam	struct mbuf *m0;
959178354Ssam	struct ieee80211_key *k;
960109323Ssam	struct wi_frame frmhdr;
961160991Ssam	int cur;
96246492Swpaul
963165087Ssam	WI_LOCK_ASSERT(sc);
96446492Swpaul
965165087Ssam	if (sc->wi_gone)
966109323Ssam		return;
96746492Swpaul
968109323Ssam	memset(&frmhdr, 0, sizeof(frmhdr));
969109323Ssam	cur = sc->sc_txnext;
970109323Ssam	for (;;) {
971178354Ssam		IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
972178354Ssam		if (m0 == NULL)
973178354Ssam			break;
974178354Ssam		if (sc->sc_txd[cur].d_len != 0) {
975178354Ssam			IFQ_DRV_PREPEND(&ifp->if_snd, m0);
976178354Ssam			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
977178354Ssam			break;
978178354Ssam		}
979178354Ssam		/* NB: copy before 802.11 header is prepended */
980178354Ssam		m_copydata(m0, 0, ETHER_HDR_LEN,
981178354Ssam		    (caddr_t)&frmhdr.wi_ehdr);
982119150Ssam
983178354Ssam		ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
984178354Ssam		m0 = ieee80211_encap(ni, m0);
985178354Ssam		if (m0 == NULL) {
986178354Ssam			ifp->if_oerrors++;
987178354Ssam			ieee80211_free_node(ni);
988178354Ssam			continue;
989178354Ssam		}
99046492Swpaul
991178354Ssam		wh = mtod(m0, struct ieee80211_frame *);
992109323Ssam		frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
993178354Ssam		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
994178354Ssam			k = ieee80211_crypto_encap(ni, m0);
995138571Ssam			if (k == NULL) {
996170530Ssam				ieee80211_free_node(ni);
997143299Ssam				m_freem(m0);
998109323Ssam				continue;
999109323Ssam			}
1000138952Ssam			frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
1001109323Ssam		}
1002178354Ssam
1003178354Ssam		if (bpf_peers_present(ifp->if_bpf)) {
1004178354Ssam			sc->sc_tx_th.wt_rate = ni->ni_txrate;
1005178354Ssam			bpf_mtap2(ifp->if_bpf,
1006178354Ssam			    &sc->sc_tx_th, sc->sc_tx_th_len, m0);
1007109323Ssam		}
1008178354Ssam
1009127697Ssam		m_copydata(m0, 0, sizeof(struct ieee80211_frame),
1010127697Ssam		    (caddr_t)&frmhdr.wi_whdr);
1011127697Ssam		m_adj(m0, sizeof(struct ieee80211_frame));
1012127697Ssam		frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
1013170530Ssam		ieee80211_free_node(ni);
1014160991Ssam		if (wi_start_tx(ifp, &frmhdr, m0))
1015109323Ssam			continue;
1016178354Ssam
1017160991Ssam		sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
1018178354Ssam		ifp->if_opackets++;
1019160991Ssam	}
1020165087Ssam}
1021160991Ssam
1022165087Ssamstatic void
1023165087Ssamwi_start(struct ifnet *ifp)
1024165087Ssam{
1025165087Ssam	struct wi_softc	*sc = ifp->if_softc;
1026165087Ssam
1027165087Ssam	WI_LOCK(sc);
1028165087Ssam	wi_start_locked(ifp);
1029160991Ssam	WI_UNLOCK(sc);
1030160991Ssam}
1031160991Ssam
1032160991Ssamstatic int
1033160991Ssamwi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr, struct mbuf *m0)
1034160991Ssam{
1035160991Ssam	struct wi_softc	*sc = ifp->if_softc;
1036160991Ssam	int cur = sc->sc_txnext;
1037160991Ssam	int fid, off, error;
1038160991Ssam
1039160991Ssam	fid = sc->sc_txd[cur].d_fid;
1040160991Ssam	off = sizeof(*frmhdr);
1041160991Ssam	error = wi_write_bap(sc, fid, 0, frmhdr, sizeof(*frmhdr)) != 0
1042160991Ssam	     || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
1043160991Ssam	m_freem(m0);
1044160991Ssam	if (error) {
1045160991Ssam		ifp->if_oerrors++;
1046160991Ssam		return -1;
1047160991Ssam	}
1048160991Ssam	sc->sc_txd[cur].d_len = off;
1049160991Ssam	if (sc->sc_txcur == cur) {
1050160991Ssam		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
1051160991Ssam			if_printf(ifp, "xmit failed\n");
1052160991Ssam			sc->sc_txd[cur].d_len = 0;
1053160991Ssam			return -1;
1054109323Ssam		}
1055160991Ssam		sc->sc_tx_timer = 5;
1056160991Ssam	}
1057160991Ssam	return 0;
1058160991Ssam}
1059160991Ssam
1060160991Ssamstatic int
1061160991Ssamwi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m0,
1062160991Ssam	    const struct ieee80211_bpf_params *params)
1063160991Ssam{
1064160991Ssam	struct ieee80211com *ic = ni->ni_ic;
1065160991Ssam	struct ifnet *ifp = ic->ic_ifp;
1066160991Ssam	struct wi_softc	*sc = ifp->if_softc;
1067178354Ssam	struct ieee80211_key *k;
1068160991Ssam	struct ieee80211_frame *wh;
1069160991Ssam	struct wi_frame frmhdr;
1070160991Ssam	int cur;
1071160991Ssam	int rc = 0;
1072160991Ssam
1073160991Ssam	WI_LOCK(sc);
1074160991Ssam
1075160991Ssam	if (sc->wi_gone) {
1076160991Ssam		rc = ENETDOWN;
1077160991Ssam		goto out;
1078160991Ssam	}
1079160991Ssam	memset(&frmhdr, 0, sizeof(frmhdr));
1080160991Ssam	cur = sc->sc_txnext;
1081160991Ssam	if (sc->sc_txd[cur].d_len != 0) {
1082160991Ssam		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1083160991Ssam		rc = ENOBUFS;
1084160991Ssam		goto out;
1085160991Ssam	}
1086160991Ssam	m0->m_pkthdr.rcvif = NULL;
1087160991Ssam
1088160991Ssam	m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
1089160991Ssam	    (caddr_t)&frmhdr.wi_ehdr);
1090160991Ssam	frmhdr.wi_ehdr.ether_type = 0;
1091160991Ssam	wh = mtod(m0, struct ieee80211_frame *);
1092160991Ssam
1093160991Ssam	frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
1094160991Ssam	if (params && (params->ibp_flags & IEEE80211_BPF_NOACK))
1095160991Ssam		frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_ALTRTRY);
1096160991Ssam	if ((wh->i_fc[1] & IEEE80211_FC1_WEP) &&
1097178354Ssam	    (!params || (params && (params->ibp_flags & IEEE80211_BPF_CRYPTO)))) {
1098178354Ssam		k = ieee80211_crypto_encap(ni, m0);
1099178354Ssam		if (k == NULL) {
1100178354Ssam			rc = ENOMEM;
1101178354Ssam			goto out;
1102109323Ssam		}
1103178354Ssam		frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
110474838Salfred	}
1105178354Ssam	if (bpf_peers_present(ifp->if_bpf)) {
1106178354Ssam		sc->sc_tx_th.wt_rate = ni->ni_txrate;
1107178354Ssam		bpf_mtap2(ifp->if_bpf, &sc->sc_tx_th, sc->sc_tx_th_len, m0);
1108160991Ssam	}
1109160991Ssam	m_copydata(m0, 0, sizeof(struct ieee80211_frame),
1110160991Ssam	    (caddr_t)&frmhdr.wi_whdr);
1111160991Ssam	m_adj(m0, sizeof(struct ieee80211_frame));
1112160991Ssam	frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
1113168860Ssephe	if (wi_start_tx(ifp, &frmhdr, m0) < 0) {
1114168860Ssephe		m0 = NULL;
1115168860Ssephe		rc = EIO;
1116160991Ssam		goto out;
1117168860Ssephe	}
1118168860Ssephe	m0 = NULL;
111946492Swpaul
1120160991Ssam	sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
1121160991Ssamout:
1122109323Ssam	WI_UNLOCK(sc);
1123160991Ssam
1124168860Ssephe	if (m0 != NULL)
1125168860Ssephe		m_freem(m0);
1126168860Ssephe	ieee80211_free_node(ni);
1127160991Ssam	return rc;
112846492Swpaul}
112946492Swpaul
113088546Salfredstatic int
1131178354Ssamwi_reset(struct wi_softc *sc)
113246492Swpaul{
1133114124Simp#define WI_INIT_TRIES 3
1134178354Ssam	int i, error = 0;
1135112362Simp
1136178354Ssam	for (i = 0; i < WI_INIT_TRIES; i++) {
1137178354Ssam		error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0);
1138178354Ssam		if (error == 0)
113946492Swpaul			break;
1140109323Ssam		DELAY(WI_DELAY * 1000);
114146492Swpaul	}
1142114124Simp	sc->sc_reset = 1;
1143178354Ssam	if (i == WI_INIT_TRIES) {
1144178354Ssam		if_printf(sc->sc_ifp, "reset failed\n");
1145178354Ssam		return error;
114675373Salfred	}
114746492Swpaul
1148109323Ssam	CSR_WRITE_2(sc, WI_INT_EN, 0);
1149114124Simp	CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
115046492Swpaul
1151109323Ssam	/* Calibrate timer. */
1152114124Simp	wi_write_val(sc, WI_RID_TICK_TIME, 8);
1153114124Simp
1154178354Ssam	return 0;
1155109323Ssam#undef WI_INIT_TRIES
115646492Swpaul}
115746492Swpaul
115888546Salfredstatic void
1159165089Ssamwi_watchdog(void *arg)
116046492Swpaul{
1161165089Ssam	struct wi_softc	*sc = arg;
1162165089Ssam	struct ifnet *ifp = sc->sc_ifp;
116346492Swpaul
1164178354Ssam	WI_LOCK_ASSERT(sc);
1165178354Ssam
1166109323Ssam	if (!sc->sc_enabled)
1167109323Ssam		return;
116846492Swpaul
1169178354Ssam	if (sc->sc_tx_timer && --sc->sc_tx_timer == 0) {
1170178354Ssam		if_printf(ifp, "device timeout\n");
1171178354Ssam		ifp->if_oerrors++;
1172178354Ssam		wi_init_locked(ifp->if_softc);
1173178354Ssam		return;
117446492Swpaul	}
1175165089Ssam	callout_reset(&sc->sc_watchdog, hz, wi_watchdog, sc);
117646492Swpaul}
117746492Swpaul
117888546Salfredstatic int
1179109323Ssamwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
118046492Swpaul{
1181109323Ssam	struct wi_softc *sc = ifp->if_softc;
1182178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1183178354Ssam	struct ifreq *ifr = (struct ifreq *) data;
1184178354Ssam	int error = 0, startall = 0;
118546492Swpaul
1186109323Ssam	switch (cmd) {
118746492Swpaul	case SIOCSIFFLAGS:
1188178704Sthompsa		WI_LOCK(sc);
1189100876Simp		/*
1190101139Simp		 * Can't do promisc and hostap at the same time.  If all that's
1191101139Simp		 * changing is the promisc flag, try to short-circuit a call to
1192101139Simp		 * wi_init() by just setting PROMISC in the hardware.
1193100876Simp		 */
119446492Swpaul		if (ifp->if_flags & IFF_UP) {
1195109323Ssam			if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1196148887Srwatson			    ifp->if_drv_flags & IFF_DRV_RUNNING) {
1197178354Ssam				if ((ifp->if_flags ^ sc->sc_if_flags) & IFF_PROMISC) {
1198178354Ssam					wi_write_val(sc, WI_RID_PROMISC,
1199178354Ssam					    (ifp->if_flags & IFF_PROMISC) != 0);
1200101139Simp				} else {
1201178354Ssam					wi_init_locked(sc);
1202178354Ssam					startall = 1;
1203101139Simp				}
1204100876Simp			} else {
1205178354Ssam				wi_init_locked(sc);
1206178354Ssam				startall = 1;
1207100876Simp			}
120846492Swpaul		} else {
1209178354Ssam			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1210178354Ssam				wi_stop_locked(sc, 1);
1211123098Simp			sc->wi_gone = 0;
121246492Swpaul		}
1213109323Ssam		sc->sc_if_flags = ifp->if_flags;
1214178704Sthompsa		WI_UNLOCK(sc);
1215178704Sthompsa		if (startall)
1216178704Sthompsa			ieee80211_start_all(ic);
121746492Swpaul		break;
1218178354Ssam	case SIOCGIFMEDIA:
1219178354Ssam		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
122046492Swpaul		break;
1221178704Sthompsa	case SIOCGIFADDR:
1222178354Ssam		error = ether_ioctl(ifp, cmd, data);
1223170530Ssam		break;
1224178704Sthompsa	default:
1225178704Sthompsa		error = EINVAL;
1226178704Sthompsa		break;
1227170530Ssam	}
1228109323Ssam	return error;
1229109323Ssam}
123046492Swpaul
1231109323Ssamstatic void
1232109323Ssamwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1233109323Ssam{
1234178354Ssam	struct ieee80211vap *vap = ifp->if_softc;
1235178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1236178354Ssam	struct wi_softc *sc = ic->ic_ifp->if_softc;
1237109323Ssam	u_int16_t val;
1238109323Ssam	int rate, len;
123946492Swpaul
1240109323Ssam	len = sizeof(val);
1241178354Ssam	if (sc->sc_enabled &&
1242178354Ssam	    wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) == 0 &&
1243138571Ssam	    len == sizeof(val)) {
1244109323Ssam		/* convert to 802.11 rate */
1245138571Ssam		val = le16toh(val);
1246109323Ssam		rate = val * 2;
1247109323Ssam		if (sc->sc_firmware_type == WI_LUCENT) {
1248138571Ssam			if (rate == 10)
1249109323Ssam				rate = 11;	/* 5.5Mbps */
1250109323Ssam		} else {
1251109323Ssam			if (rate == 4*2)
1252109323Ssam				rate = 11;	/* 5.5Mbps */
1253109323Ssam			else if (rate == 8*2)
1254109323Ssam				rate = 22;	/* 11Mbps */
1255109323Ssam		}
1256178354Ssam		vap->iv_bss->ni_txrate = rate;
1257109323Ssam	}
1258178354Ssam	ieee80211_media_status(ifp, imr);
1259109323Ssam}
126046492Swpaul
1261109323Ssamstatic void
1262109323Ssamwi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1263109323Ssam{
1264147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1265178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1266178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1267178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
126898440Simp
1269109323Ssam	if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1270109323Ssam		return;
127146492Swpaul
1272109323Ssam	DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid)));
1273109323Ssam	DPRINTF(("%s ?\n", ether_sprintf(new_bssid)));
127446492Swpaul
1275109323Ssam	/* In promiscuous mode, the BSSID field is not a reliable
1276109323Ssam	 * indicator of the firmware's BSSID. Damp spurious
1277109323Ssam	 * change-of-BSSID indications.
1278109323Ssam	 */
1279109323Ssam	if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1280138571Ssam	    !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns,
1281138571Ssam	                 WI_MAX_FALSE_SYNS))
1282109323Ssam		return;
128346492Swpaul
1284139542Ssam	sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1);
1285170530Ssam#if 0
1286139542Ssam	/*
1287139542Ssam	 * XXX hack; we should create a new node with the new bssid
1288139542Ssam	 * and replace the existing ic_bss with it but since we don't
1289139542Ssam	 * process management frames to collect state we cheat by
1290139542Ssam	 * reusing the existing node as we know wi_newstate will be
1291139542Ssam	 * called and it will overwrite the node state.
1292139542Ssam	 */
1293139542Ssam	ieee80211_sta_join(ic, ieee80211_ref_node(ni));
1294170530Ssam#endif
1295109323Ssam}
129646492Swpaul
1297178354Ssamstatic __noinline void
1298109323Ssamwi_rx_intr(struct wi_softc *sc)
1299109323Ssam{
1300147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1301178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1302109323Ssam	struct wi_frame frmhdr;
1303109323Ssam	struct mbuf *m;
1304109323Ssam	struct ieee80211_frame *wh;
1305119150Ssam	struct ieee80211_node *ni;
1306109323Ssam	int fid, len, off, rssi;
1307109323Ssam	u_int8_t dir;
1308109323Ssam	u_int16_t status;
1309109323Ssam	u_int32_t rstamp;
131046611Swpaul
1311109323Ssam	fid = CSR_READ_2(sc, WI_RX_FID);
131246611Swpaul
1313109323Ssam	/* First read in the frame header */
1314109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1315109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1316109323Ssam		ifp->if_ierrors++;
1317109323Ssam		DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1318109323Ssam		return;
1319109323Ssam	}
132091695Simp
1321109323Ssam	/*
1322109323Ssam	 * Drop undecryptable or packets with receive errors here
1323109323Ssam	 */
1324109323Ssam	status = le16toh(frmhdr.wi_status);
1325109323Ssam	if (status & WI_STAT_ERRSTAT) {
1326109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1327109323Ssam		ifp->if_ierrors++;
1328109323Ssam		DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1329109323Ssam		return;
1330109323Ssam	}
1331109323Ssam	rssi = frmhdr.wi_rx_signal;
1332109323Ssam	rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1333109323Ssam	    le16toh(frmhdr.wi_rx_tstamp1);
133446492Swpaul
1335109323Ssam	len = le16toh(frmhdr.wi_dat_len);
1336109323Ssam	off = ALIGN(sizeof(struct ieee80211_frame));
133746563Swpaul
1338117855Ssam	/*
1339117855Ssam	 * Sometimes the PRISM2.x returns bogusly large frames. Except
1340117855Ssam	 * in monitor mode, just throw them away.
1341117855Ssam	 */
1342117855Ssam	if (off + len > MCLBYTES) {
1343117855Ssam		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1344117855Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1345117855Ssam			ifp->if_ierrors++;
1346117855Ssam			DPRINTF(("wi_rx_intr: oversized packet\n"));
1347117855Ssam			return;
1348117855Ssam		} else
1349117855Ssam			len = 0;
1350117855Ssam	}
1351117855Ssam
1352178354Ssam	if (off + len > MHLEN)
1353178354Ssam		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1354178354Ssam	else
1355178354Ssam		m = m_gethdr(M_DONTWAIT, MT_DATA);
1356109323Ssam	if (m == NULL) {
1357109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1358109323Ssam		ifp->if_ierrors++;
1359109323Ssam		DPRINTF(("wi_rx_intr: MGET failed\n"));
1360109323Ssam		return;
1361109323Ssam	}
1362109323Ssam	m->m_data += off - sizeof(struct ieee80211_frame);
1363109323Ssam	memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1364109323Ssam	wi_read_bap(sc, fid, sizeof(frmhdr),
1365109323Ssam	    m->m_data + sizeof(struct ieee80211_frame), len);
1366109323Ssam	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1367109323Ssam	m->m_pkthdr.rcvif = ifp;
136894405Simp
1369109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
137046492Swpaul
1371178354Ssam	if (bpf_peers_present(ifp->if_bpf)) {
1372123927Ssam		/* XXX replace divide by table */
1373123922Ssam		sc->sc_rx_th.wr_rate = frmhdr.wi_rx_rate / 5;
1374127148Sgreen		sc->sc_rx_th.wr_antsignal = frmhdr.wi_rx_signal;
1375127148Sgreen		sc->sc_rx_th.wr_antnoise = frmhdr.wi_rx_silence;
1376123927Ssam		sc->sc_rx_th.wr_flags = 0;
1377123927Ssam		if (frmhdr.wi_status & WI_STAT_PCF)
1378123927Ssam			sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_CFP;
1379178354Ssam		if (m->m_flags & M_WEP)
1380178354Ssam			sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_WEP;
1381178354Ssam		bpf_mtap2(ifp->if_bpf, &sc->sc_rx_th, sc->sc_rx_th_len, m);
138256965Swpaul	}
138356965Swpaul
1384109323Ssam	/* synchronize driver's BSSID with firmware's BSSID */
1385178354Ssam	wh = mtod(m, struct ieee80211_frame *);
1386109323Ssam	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1387109323Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1388109323Ssam		wi_sync_bssid(sc, wh->i_addr3);
138946492Swpaul
1390165088Ssam	WI_UNLOCK(sc);
1391165088Ssam
1392178354Ssam	ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1393178354Ssam	if (ni != NULL) {
1394178354Ssam		(void) ieee80211_input(ni, m, rssi, -95/*XXX*/, rstamp);
1395178354Ssam		ieee80211_free_node(ni);
1396178354Ssam	} else
1397178354Ssam		(void) ieee80211_input_all(ic, m, rssi, -95/*XXX*/, rstamp);
1398178354Ssam
1399165088Ssam	WI_LOCK(sc);
1400109323Ssam}
140146492Swpaul
1402178354Ssamstatic __noinline void
1403109323Ssamwi_tx_ex_intr(struct wi_softc *sc)
1404109323Ssam{
1405147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1406109323Ssam	struct wi_frame frmhdr;
1407109323Ssam	int fid;
140846492Swpaul
1409109323Ssam	fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1410109323Ssam	/* Read in the frame header */
1411109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1412109323Ssam		u_int16_t status = le16toh(frmhdr.wi_status);
1413109323Ssam		/*
1414109323Ssam		 * Spontaneous station disconnects appear as xmit
1415109323Ssam		 * errors.  Don't announce them and/or count them
1416109323Ssam		 * as an output error.
1417109323Ssam		 */
1418109323Ssam		if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1419109323Ssam			if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1420109323Ssam				if_printf(ifp, "tx failed");
1421109323Ssam				if (status & WI_TXSTAT_RET_ERR)
1422109323Ssam					printf(", retry limit exceeded");
1423109323Ssam				if (status & WI_TXSTAT_AGED_ERR)
1424109323Ssam					printf(", max transmit lifetime exceeded");
1425109323Ssam				if (status & WI_TXSTAT_DISCONNECT)
1426109323Ssam					printf(", port disconnected");
1427109323Ssam				if (status & WI_TXSTAT_FORM_ERR)
1428109323Ssam					printf(", invalid format (data len %u src %6D)",
1429109323Ssam						le16toh(frmhdr.wi_dat_len),
1430109323Ssam						frmhdr.wi_ehdr.ether_shost, ":");
1431109323Ssam				if (status & ~0xf)
1432109323Ssam					printf(", status=0x%x", status);
1433109323Ssam				printf("\n");
1434109323Ssam			}
1435109323Ssam			ifp->if_oerrors++;
1436109323Ssam		} else {
1437109323Ssam			DPRINTF(("port disconnected\n"));
1438109323Ssam			ifp->if_collisions++;	/* XXX */
1439109323Ssam		}
1440109323Ssam	} else
1441109323Ssam		DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
1442109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1443109323Ssam}
144446492Swpaul
1445178354Ssamstatic __noinline void
1446109323Ssamwi_tx_intr(struct wi_softc *sc)
1447109323Ssam{
1448147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1449109323Ssam	int fid, cur;
145094405Simp
1451123098Simp	if (sc->wi_gone)
1452123098Simp		return;
1453123098Simp
1454109323Ssam	fid = CSR_READ_2(sc, WI_ALLOC_FID);
1455109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
145646492Swpaul
1457109323Ssam	cur = sc->sc_txcur;
1458109323Ssam	if (sc->sc_txd[cur].d_fid != fid) {
1459109323Ssam		if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
1460109323Ssam		    fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
1461109323Ssam		return;
1462109323Ssam	}
1463109323Ssam	sc->sc_tx_timer = 0;
1464109323Ssam	sc->sc_txd[cur].d_len = 0;
1465112363Simp	sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
1466109323Ssam	if (sc->sc_txd[cur].d_len == 0)
1467148887Srwatson		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1468109323Ssam	else {
1469109323Ssam		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1470109323Ssam		    0, 0)) {
1471109323Ssam			if_printf(ifp, "xmit failed\n");
1472109323Ssam			sc->sc_txd[cur].d_len = 0;
1473109323Ssam		} else {
1474109323Ssam			sc->sc_tx_timer = 5;
1475109323Ssam		}
1476109323Ssam	}
147746492Swpaul}
147846492Swpaul
147988546Salfredstatic void
1480178354Ssamwi_status_connected(void *arg, int pending)
1481178354Ssam{
1482178354Ssam	struct ieee80211vap *vap = arg;
1483178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1484178354Ssam
1485178354Ssam	IEEE80211_LOCK(ic);
1486178354Ssam	WI_VAP(vap)->wv_newstate(vap, IEEE80211_S_RUN, 0);
1487178354Ssam	if (vap->iv_newstate_cb != NULL)
1488178354Ssam		vap->iv_newstate_cb(vap, IEEE80211_S_RUN, 0);
1489178354Ssam	IEEE80211_UNLOCK(ic);
1490178354Ssam}
1491178354Ssam
1492178354Ssamstatic void
1493178354Ssamwi_status_disconnected(void *arg, int pending)
1494178354Ssam{
1495178354Ssam	struct ieee80211vap *vap = arg;
1496178354Ssam
1497178354Ssam	if (vap->iv_state == IEEE80211_S_RUN) {
1498178354Ssam		vap->iv_stats.is_rx_deauth++;
1499178354Ssam		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
1500178354Ssam	}
1501178354Ssam}
1502178354Ssam
1503178354Ssamstatic void
1504178354Ssamwi_status_oor(void *arg, int pending)
1505178354Ssam{
1506178354Ssam	struct ieee80211com *ic = arg;
1507178354Ssam
1508178354Ssam	ieee80211_beacon_miss(ic);
1509178354Ssam}
1510178354Ssam
1511178354Ssamstatic void
1512178354Ssamwi_status_assoc_failed(void *arg, int pending)
1513178354Ssam{
1514178354Ssam	struct ieee80211vap *vap = arg;
1515178354Ssam
1516178354Ssam	ieee80211_new_state(vap, IEEE80211_S_SCAN, IEEE80211_SCAN_FAIL_TIMEOUT);
1517178354Ssam}
1518178354Ssam
1519178354Ssamstatic __noinline void
1520109323Ssamwi_info_intr(struct wi_softc *sc)
152194405Simp{
1522147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1523178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1524178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1525178354Ssam	struct wi_vap *wvp = WI_VAP(vap);
1526109323Ssam	int i, fid, len, off;
1527109323Ssam	u_int16_t ltbuf[2];
1528109323Ssam	u_int16_t stat;
1529109323Ssam	u_int32_t *ptr;
153094405Simp
1531109323Ssam	fid = CSR_READ_2(sc, WI_INFO_FID);
1532109323Ssam	wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
153394405Simp
1534109323Ssam	switch (le16toh(ltbuf[1])) {
1535109323Ssam	case WI_INFO_LINK_STAT:
1536109323Ssam		wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1537109323Ssam		DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1538109323Ssam		switch (le16toh(stat)) {
1539109323Ssam		case WI_INFO_LINK_STAT_CONNECTED:
1540178354Ssam			if (vap->iv_state == IEEE80211_S_RUN &&
1541178354Ssam			    vap->iv_opmode != IEEE80211_M_IBSS)
1542109323Ssam				break;
1543178354Ssam			/* fall thru... */
1544109323Ssam		case WI_INFO_LINK_STAT_AP_CHG:
1545178354Ssam			taskqueue_enqueue(taskqueue_swi, &wvp->wv_connected_task);
1546109323Ssam			break;
1547109323Ssam		case WI_INFO_LINK_STAT_AP_INR:
1548109323Ssam			break;
1549178354Ssam		case WI_INFO_LINK_STAT_DISCONNECTED:
1550178354Ssam			/* we dropped off the net; e.g. due to deauth/disassoc */
1551178354Ssam			taskqueue_enqueue(taskqueue_swi, &wvp->wv_disconnected_task);
1552178354Ssam			break;
1553109323Ssam		case WI_INFO_LINK_STAT_AP_OOR:
1554178354Ssam			/* XXX does this need to be per-vap? */
1555178354Ssam			taskqueue_enqueue(taskqueue_swi, &sc->sc_oor_task);
1556109323Ssam			break;
1557109323Ssam		case WI_INFO_LINK_STAT_ASSOC_FAILED:
1558178354Ssam			if (vap->iv_opmode == IEEE80211_M_STA)
1559178354Ssam				taskqueue_enqueue(taskqueue_swi,
1560178354Ssam				    &wvp->wv_assoc_failed_task);
1561109323Ssam			break;
1562109323Ssam		}
1563109323Ssam		break;
1564109323Ssam	case WI_INFO_COUNTERS:
1565109323Ssam		/* some card versions have a larger stats structure */
1566109323Ssam		len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1567109323Ssam		ptr = (u_int32_t *)&sc->sc_stats;
1568109323Ssam		off = sizeof(ltbuf);
1569109323Ssam		for (i = 0; i < len; i++, off += 2, ptr++) {
1570109323Ssam			wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1571109323Ssam#ifdef WI_HERMES_STATS_WAR
1572109323Ssam			if (stat & 0xf000)
1573109323Ssam				stat = ~stat;
1574109323Ssam#endif
1575109323Ssam			*ptr += stat;
1576109323Ssam		}
1577109323Ssam		ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1578109323Ssam		    sc->sc_stats.wi_tx_multi_retries +
1579109323Ssam		    sc->sc_stats.wi_tx_retry_limit;
1580109323Ssam		break;
1581109323Ssam	default:
1582109323Ssam		DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1583109323Ssam		    le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1584109323Ssam		break;
158594405Simp	}
1586109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1587109323Ssam}
158894405Simp
1589109323Ssamstatic int
1590109323Ssamwi_write_multi(struct wi_softc *sc)
1591109323Ssam{
1592147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1593109323Ssam	int n;
1594109323Ssam	struct ifmultiaddr *ifma;
1595109323Ssam	struct wi_mcast mlist;
159694472Simp
1597109323Ssam	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1598109323Ssamallmulti:
1599109323Ssam		memset(&mlist, 0, sizeof(mlist));
1600109323Ssam		return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1601109323Ssam		    sizeof(mlist));
160294405Simp	}
160394405Simp
1604109323Ssam	n = 0;
1605148654Srwatson	IF_ADDR_LOCK(ifp);
1606109323Ssam	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1607109323Ssam		if (ifma->ifma_addr->sa_family != AF_LINK)
1608109323Ssam			continue;
1609109323Ssam		if (n >= 16)
1610109323Ssam			goto allmulti;
1611109323Ssam		IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1612109323Ssam		    (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1613109323Ssam		n++;
161494405Simp	}
1615148654Srwatson	IF_ADDR_UNLOCK(ifp);
1616109323Ssam	return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1617109323Ssam	    IEEE80211_ADDR_LEN * n);
161894405Simp}
161994405Simp
162094405Simpstatic void
1621178354Ssamwi_update_mcast(struct ifnet *ifp)
1622178354Ssam{
1623178354Ssam	wi_write_multi(ifp->if_softc);
1624178354Ssam}
1625178354Ssam
1626178354Ssamstatic void
1627109323Ssamwi_read_nicid(struct wi_softc *sc)
162846492Swpaul{
1629109323Ssam	struct wi_card_ident *id;
1630109323Ssam	char *p;
1631109323Ssam	int len;
1632109323Ssam	u_int16_t ver[4];
163346492Swpaul
1634109323Ssam	/* getting chip identity */
1635109323Ssam	memset(ver, 0, sizeof(ver));
1636109323Ssam	len = sizeof(ver);
1637109323Ssam	wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1638109323Ssam	device_printf(sc->sc_dev, "using ");
163946492Swpaul
1640109323Ssam	sc->sc_firmware_type = WI_NOTYPE;
1641109323Ssam	for (id = wi_card_ident; id->card_name != NULL; id++) {
1642109323Ssam		if (le16toh(ver[0]) == id->card_id) {
1643109323Ssam			printf("%s", id->card_name);
1644109323Ssam			sc->sc_firmware_type = id->firm_type;
1645109323Ssam			break;
1646109323Ssam		}
164767092Swpaul	}
1648109323Ssam	if (sc->sc_firmware_type == WI_NOTYPE) {
1649109323Ssam		if (le16toh(ver[0]) & 0x8000) {
1650109323Ssam			printf("Unknown PRISM2 chip");
1651109323Ssam			sc->sc_firmware_type = WI_INTERSIL;
1652109323Ssam		} else {
1653109323Ssam			printf("Unknown Lucent chip");
1654109323Ssam			sc->sc_firmware_type = WI_LUCENT;
1655109323Ssam		}
165667092Swpaul	}
165746492Swpaul
1658109323Ssam	/* get primary firmware version (Only Prism chips) */
1659109323Ssam	if (sc->sc_firmware_type != WI_LUCENT) {
1660109323Ssam		memset(ver, 0, sizeof(ver));
1661109323Ssam		len = sizeof(ver);
1662109323Ssam		wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1663109323Ssam		sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1664109323Ssam		    le16toh(ver[3]) * 100 + le16toh(ver[1]);
166567092Swpaul	}
166646492Swpaul
1667109323Ssam	/* get station firmware version */
1668109323Ssam	memset(ver, 0, sizeof(ver));
1669109323Ssam	len = sizeof(ver);
1670109323Ssam	wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1671109323Ssam	sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1672109323Ssam	    le16toh(ver[3]) * 100 + le16toh(ver[1]);
1673109323Ssam	if (sc->sc_firmware_type == WI_INTERSIL &&
1674109323Ssam	    (sc->sc_sta_firmware_ver == 10102 ||
1675109323Ssam	     sc->sc_sta_firmware_ver == 20102)) {
1676109323Ssam		char ident[12];
1677109323Ssam		memset(ident, 0, sizeof(ident));
1678109323Ssam		len = sizeof(ident);
1679109323Ssam		/* value should be the format like "V2.00-11" */
1680109323Ssam		if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1681109323Ssam		    *(p = (char *)ident) >= 'A' &&
1682109323Ssam		    p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1683109323Ssam			sc->sc_firmware_type = WI_SYMBOL;
1684109323Ssam			sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1685109323Ssam			    (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1686109323Ssam			    (p[6] - '0') * 10 + (p[7] - '0');
168794405Simp		}
168894405Simp	}
1689109323Ssam	printf("\n");
1690109323Ssam	device_printf(sc->sc_dev, "%s Firmware: ",
1691109323Ssam	     sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
1692109323Ssam	    (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
1693109323Ssam	if (sc->sc_firmware_type != WI_LUCENT)	/* XXX */
1694109323Ssam		printf("Primary (%u.%u.%u), ",
1695109323Ssam		    sc->sc_pri_firmware_ver / 10000,
1696109323Ssam		    (sc->sc_pri_firmware_ver % 10000) / 100,
1697109323Ssam		    sc->sc_pri_firmware_ver % 100);
1698109323Ssam	printf("Station (%u.%u.%u)\n",
1699109323Ssam	    sc->sc_sta_firmware_ver / 10000,
1700109323Ssam	    (sc->sc_sta_firmware_ver % 10000) / 100,
1701109323Ssam	    sc->sc_sta_firmware_ver % 100);
1702109323Ssam}
170346492Swpaul
1704109323Ssamstatic int
1705109323Ssamwi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1706109323Ssam{
1707109323Ssam	struct wi_ssid ssid;
170846492Swpaul
1709109323Ssam	if (buflen > IEEE80211_NWID_LEN)
1710109323Ssam		return ENOBUFS;
1711109323Ssam	memset(&ssid, 0, sizeof(ssid));
1712109323Ssam	ssid.wi_len = htole16(buflen);
1713109323Ssam	memcpy(ssid.wi_ssid, buf, buflen);
1714109323Ssam	return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1715109323Ssam}
171646492Swpaul
1717109323Ssamstatic int
1718178354Ssamwi_write_txrate(struct wi_softc *sc, struct ieee80211vap *vap)
1719109323Ssam{
1720178354Ssam	static const uint16_t lucent_rates[12] = {
1721178354Ssam	    [ 0] = 3,	/* auto */
1722178354Ssam	    [ 1] = 1,	/* 1Mb/s */
1723178354Ssam	    [ 2] = 2,	/* 2Mb/s */
1724178354Ssam	    [ 5] = 4,	/* 5.5Mb/s */
1725178354Ssam	    [11] = 5	/* 11Mb/s */
1726178354Ssam	};
1727178354Ssam	static const uint16_t intersil_rates[12] = {
1728178354Ssam	    [ 0] = 0xf,	/* auto */
1729178354Ssam	    [ 1] = 0,	/* 1Mb/s */
1730178354Ssam	    [ 2] = 1,	/* 2Mb/s */
1731178354Ssam	    [ 5] = 2,	/* 5.5Mb/s */
1732178354Ssam	    [11] = 3,	/* 11Mb/s */
1733178354Ssam	};
1734178354Ssam	const uint16_t *rates = sc->sc_firmware_type == WI_LUCENT ?
1735178354Ssam	    lucent_rates : intersil_rates;
1736178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1737178354Ssam	const struct ieee80211_txparam *tp;
173846492Swpaul
1739178354Ssam	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
1740178354Ssam	return wi_write_val(sc, WI_RID_TX_RATE,
1741178354Ssam	    (tp->ucastrate == IEEE80211_FIXED_RATE_NONE ?
1742178354Ssam		rates[0] : rates[tp->ucastrate / 2]));
174346492Swpaul}
174446492Swpaul
1745109323Ssamstatic int
1746178354Ssamwi_write_wep(struct wi_softc *sc, struct ieee80211vap *vap)
174746492Swpaul{
1748109323Ssam	int error = 0;
1749109323Ssam	int i, keylen;
1750109323Ssam	u_int16_t val;
1751109323Ssam	struct wi_key wkey[IEEE80211_WEP_NKID];
175246492Swpaul
1753109323Ssam	switch (sc->sc_firmware_type) {
1754109323Ssam	case WI_LUCENT:
1755178354Ssam		val = (vap->iv_flags & IEEE80211_F_PRIVACY) ? 1 : 0;
1756109323Ssam		error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
1757109323Ssam		if (error)
1758109323Ssam			break;
1759178354Ssam		if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0)
1760138988Smdodd			break;
1761178354Ssam		error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, vap->iv_def_txkey);
1762138988Smdodd		if (error)
1763138988Smdodd			break;
1764138988Smdodd		memset(wkey, 0, sizeof(wkey));
1765138988Smdodd		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1766178354Ssam			keylen = vap->iv_nw_keys[i].wk_keylen;
1767138988Smdodd			wkey[i].wi_keylen = htole16(keylen);
1768178354Ssam			memcpy(wkey[i].wi_keydat, vap->iv_nw_keys[i].wk_key,
1769138988Smdodd			    keylen);
1770109323Ssam		}
1771138988Smdodd		error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
1772138988Smdodd		    wkey, sizeof(wkey));
1773150798Savatar		sc->sc_encryption = 0;
1774109323Ssam		break;
1775109323Ssam
1776109323Ssam	case WI_INTERSIL:
1777178354Ssam		val = HOST_ENCRYPT | HOST_DECRYPT;
1778178354Ssam		if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1779109323Ssam			/*
1780109323Ssam			 * ONLY HWB3163 EVAL-CARD Firmware version
1781109323Ssam			 * less than 0.8 variant2
1782109323Ssam			 *
1783109323Ssam			 *   If promiscuous mode disable, Prism2 chip
1784109323Ssam			 *  does not work with WEP .
1785109323Ssam			 * It is under investigation for details.
1786109323Ssam			 * (ichiro@netbsd.org)
1787109323Ssam			 */
1788178354Ssam			if (sc->sc_sta_firmware_ver < 802 ) {
1789109323Ssam				/* firm ver < 0.8 variant 2 */
1790109323Ssam				wi_write_val(sc, WI_RID_PROMISC, 1);
1791109323Ssam			}
1792109323Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE,
1793178354Ssam			    vap->iv_bss->ni_authmode);
1794178354Ssam			val |= PRIVACY_INVOKED;
1795109323Ssam		} else {
1796178354Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE, IEEE80211_AUTH_OPEN);
1797109323Ssam		}
1798109323Ssam		error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
1799109323Ssam		if (error)
1800109323Ssam			break;
1801150798Savatar		sc->sc_encryption = val;
1802138988Smdodd		if ((val & PRIVACY_INVOKED) == 0)
1803138988Smdodd			break;
1804178354Ssam		error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY, vap->iv_def_txkey);
1805109323Ssam		break;
1806109323Ssam	}
1807109323Ssam	return error;
180846492Swpaul}
180946492Swpaul
1810109323Ssamstatic int
1811109323Ssamwi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
181246492Swpaul{
1813178354Ssam	int i, s = 0;
1814178354Ssam
1815123098Simp	if (sc->wi_gone)
1816123098Simp		return (ENODEV);
1817123098Simp
1818109323Ssam	/* wait for the busy bit to clear */
1819123339Simp	for (i = sc->wi_cmd_count; i > 0; i--) {	/* 500ms */
1820116206Simp		if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
1821109323Ssam			break;
1822123098Simp		DELAY(1*1000);	/* 1ms */
1823109323Ssam	}
1824109323Ssam	if (i == 0) {
1825178354Ssam		device_printf(sc->sc_dev, "%s: busy bit won't clear, cmd 0x%x\n",
1826178354Ssam		   __func__, cmd);
1827123098Simp		sc->wi_gone = 1;
1828109323Ssam		return(ETIMEDOUT);
1829109323Ssam	}
183090580Sbrooks
1831109323Ssam	CSR_WRITE_2(sc, WI_PARAM0, val0);
1832109323Ssam	CSR_WRITE_2(sc, WI_PARAM1, val1);
1833109323Ssam	CSR_WRITE_2(sc, WI_PARAM2, val2);
1834109323Ssam	CSR_WRITE_2(sc, WI_COMMAND, cmd);
183590580Sbrooks
1836109323Ssam	if (cmd == WI_CMD_INI) {
1837109323Ssam		/* XXX: should sleep here. */
1838116206Simp		DELAY(100*1000);		/* 100ms delay for init */
1839109323Ssam	}
1840109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
1841109323Ssam		/*
1842109323Ssam		 * Wait for 'command complete' bit to be
1843109323Ssam		 * set in the event status register.
1844109323Ssam		 */
1845109323Ssam		s = CSR_READ_2(sc, WI_EVENT_STAT);
1846109323Ssam		if (s & WI_EV_CMD) {
1847109323Ssam			/* Ack the event and read result code. */
1848109323Ssam			s = CSR_READ_2(sc, WI_STATUS);
1849109323Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
1850109323Ssam			if (s & WI_STAT_CMD_RESULT) {
1851109323Ssam				return(EIO);
1852109323Ssam			}
1853109323Ssam			break;
185490580Sbrooks		}
1855109323Ssam		DELAY(WI_DELAY);
1856109323Ssam	}
185790580Sbrooks
1858109323Ssam	if (i == WI_TIMEOUT) {
1859178354Ssam		device_printf(sc->sc_dev, "%s: timeout on cmd 0x%04x; "
1860178354Ssam		    "event status 0x%04x\n", __func__, cmd, s);
1861123098Simp		if (s == 0xffff)
1862123098Simp			sc->wi_gone = 1;
1863109323Ssam		return(ETIMEDOUT);
186453702Swpaul	}
1865109323Ssam	return (0);
1866109323Ssam}
186753702Swpaul
1868109323Ssamstatic int
1869109323Ssamwi_seek_bap(struct wi_softc *sc, int id, int off)
1870109323Ssam{
1871109323Ssam	int i, status;
187290580Sbrooks
1873109323Ssam	CSR_WRITE_2(sc, WI_SEL0, id);
1874109323Ssam	CSR_WRITE_2(sc, WI_OFF0, off);
187590580Sbrooks
1876109323Ssam	for (i = 0; ; i++) {
1877109323Ssam		status = CSR_READ_2(sc, WI_OFF0);
1878109323Ssam		if ((status & WI_OFF_BUSY) == 0)
1879109323Ssam			break;
1880109323Ssam		if (i == WI_TIMEOUT) {
1881178354Ssam			device_printf(sc->sc_dev, "%s: timeout, id %x off %x\n",
1882178354Ssam			    __func__, id, off);
1883109323Ssam			sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
1884123098Simp			if (status == 0xffff)
1885123098Simp				sc->wi_gone = 1;
1886109323Ssam			return ETIMEDOUT;
1887109323Ssam		}
1888109323Ssam		DELAY(1);
188953702Swpaul	}
1890109323Ssam	if (status & WI_OFF_ERR) {
1891178354Ssam		device_printf(sc->sc_dev, "%s: error, id %x off %x\n",
1892178354Ssam		    __func__, id, off);
1893109323Ssam		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
1894109323Ssam		return EIO;
1895109323Ssam	}
1896109323Ssam	sc->sc_bap_id = id;
1897109323Ssam	sc->sc_bap_off = off;
1898109323Ssam	return 0;
189953702Swpaul}
190053702Swpaul
1901109323Ssamstatic int
1902109323Ssamwi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
190353702Swpaul{
1904109323Ssam	u_int16_t *ptr;
1905109323Ssam	int i, error, cnt;
190653702Swpaul
1907109323Ssam	if (buflen == 0)
1908109323Ssam		return 0;
1909109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1910109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
1911109323Ssam			return error;
191275219Salfred	}
1913109323Ssam	cnt = (buflen + 1) / 2;
1914109323Ssam	ptr = (u_int16_t *)buf;
1915109323Ssam	for (i = 0; i < cnt; i++)
1916109323Ssam		*ptr++ = CSR_READ_2(sc, WI_DATA0);
1917109323Ssam	sc->sc_bap_off += cnt * 2;
1918109323Ssam	return 0;
191953702Swpaul}
192053702Swpaul
1921109323Ssamstatic int
1922109323Ssamwi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
192353702Swpaul{
1924109323Ssam	u_int16_t *ptr;
1925109323Ssam	int i, error, cnt;
192646492Swpaul
1927109323Ssam	if (buflen == 0)
1928109323Ssam		return 0;
192946492Swpaul
1930109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1931109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
1932109323Ssam			return error;
1933109323Ssam	}
1934109323Ssam	cnt = (buflen + 1) / 2;
1935109323Ssam	ptr = (u_int16_t *)buf;
1936109323Ssam	for (i = 0; i < cnt; i++)
1937109323Ssam		CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
1938109323Ssam	sc->sc_bap_off += cnt * 2;
1939109323Ssam
1940109323Ssam	return 0;
194146492Swpaul}
194253702Swpaul
1943109323Ssamstatic int
1944109323Ssamwi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
1945109323Ssam{
1946109323Ssam	int error, len;
1947109323Ssam	struct mbuf *m;
194853702Swpaul
1949109323Ssam	for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
1950109323Ssam		if (m->m_len == 0)
1951109323Ssam			continue;
195253702Swpaul
1953109323Ssam		len = min(m->m_len, totlen);
195453702Swpaul
1955109323Ssam		if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
1956109323Ssam			m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
1957109323Ssam			return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
1958109323Ssam			    totlen);
1959109323Ssam		}
196053702Swpaul
1961109323Ssam		if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
1962109323Ssam			return error;
196353702Swpaul
1964109323Ssam		off += m->m_len;
1965109323Ssam		totlen -= len;
1966109323Ssam	}
1967109323Ssam	return 0;
1968109323Ssam}
196953702Swpaul
1970109323Ssamstatic int
1971109323Ssamwi_alloc_fid(struct wi_softc *sc, int len, int *idp)
197253702Swpaul{
197353702Swpaul	int i;
197453702Swpaul
1975109323Ssam	if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
1976178354Ssam		device_printf(sc->sc_dev, "%s: failed to allocate %d bytes on NIC\n",
1977178354Ssam		    __func__, len);
1978109323Ssam		return ENOMEM;
197953702Swpaul	}
198053702Swpaul
1981109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
1982109323Ssam		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
1983109323Ssam			break;
1984109323Ssam		DELAY(1);
198553702Swpaul	}
1986144167Ssam	if (i == WI_TIMEOUT) {
1987178354Ssam		device_printf(sc->sc_dev, "%s: timeout in alloc\n", __func__);
1988144167Ssam		return ETIMEDOUT;
1989144167Ssam	}
1990109323Ssam	*idp = CSR_READ_2(sc, WI_ALLOC_FID);
1991109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1992109323Ssam	return 0;
1993109323Ssam}
199453702Swpaul
1995109323Ssamstatic int
1996109323Ssamwi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
1997109323Ssam{
1998109323Ssam	int error, len;
1999109323Ssam	u_int16_t ltbuf[2];
200053702Swpaul
2001109323Ssam	/* Tell the NIC to enter record read mode. */
2002109323Ssam	error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2003109323Ssam	if (error)
2004109323Ssam		return error;
200553702Swpaul
2006109323Ssam	error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2007109323Ssam	if (error)
2008109323Ssam		return error;
200953702Swpaul
2010109323Ssam	if (le16toh(ltbuf[1]) != rid) {
2011109323Ssam		device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n",
2012109323Ssam		    rid, le16toh(ltbuf[1]));
2013109323Ssam		return EIO;
201453702Swpaul	}
2015109323Ssam	len = (le16toh(ltbuf[0]) - 1) * 2;	 /* already got rid */
2016109323Ssam	if (*buflenp < len) {
2017109323Ssam		device_printf(sc->sc_dev, "record buffer is too small, "
2018109323Ssam		    "rid=%x, size=%d, len=%d\n",
2019109323Ssam		    rid, *buflenp, len);
2020109323Ssam		return ENOSPC;
202153702Swpaul	}
2022109323Ssam	*buflenp = len;
2023109323Ssam	return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2024109323Ssam}
202553702Swpaul
2026109323Ssamstatic int
2027109323Ssamwi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2028109323Ssam{
2029109323Ssam	int error;
2030109323Ssam	u_int16_t ltbuf[2];
203153702Swpaul
2032109323Ssam	ltbuf[0] = htole16((buflen + 1) / 2 + 1);	 /* includes rid */
2033109323Ssam	ltbuf[1] = htole16(rid);
203453702Swpaul
2035109323Ssam	error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2036178354Ssam	if (error) {
2037178354Ssam		device_printf(sc->sc_dev, "%s: bap0 write failure, rid 0x%x\n",
2038178354Ssam		    __func__, rid);
2039109323Ssam		return error;
2040178354Ssam	}
2041109323Ssam	error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2042178354Ssam	if (error) {
2043178354Ssam		device_printf(sc->sc_dev, "%s: bap1 write failure, rid 0x%x\n",
2044178354Ssam		    __func__, rid);
2045109323Ssam		return error;
2046178354Ssam	}
2047102206Simp
2048109323Ssam	return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
204953702Swpaul}
205077217Sphk
205188546Salfredstatic int
2052178354Ssamwi_write_appie(struct wi_softc *sc, int rid, const struct ieee80211_appie *ie)
205377217Sphk{
2054178354Ssam	/* NB: 42 bytes is probably ok to have on the stack */
2055178354Ssam	char buf[sizeof(uint16_t) + 40];
205677217Sphk
2057178354Ssam	if (ie->ie_len > 40)
2058178354Ssam		return EINVAL;
2059178354Ssam	/* NB: firmware requires 16-bit ie length before ie data */
2060178354Ssam	*(uint16_t *) buf = htole16(ie->ie_len);
2061178354Ssam	memcpy(buf + sizeof(uint16_t), ie->ie_data, ie->ie_len);
2062178354Ssam	return wi_write_rid(sc, rid, buf, ie->ie_len + sizeof(uint16_t));
206377217Sphk}
206477217Sphk
2065109323Ssamint
2066109323Ssamwi_alloc(device_t dev, int rid)
2067109323Ssam{
2068109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2069109323Ssam
2070109323Ssam	if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2071109323Ssam		sc->iobase_rid = rid;
2072109323Ssam		sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2073109323Ssam		    &sc->iobase_rid, 0, ~0, (1 << 6),
2074109323Ssam		    rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2075178354Ssam		if (sc->iobase == NULL) {
2076109323Ssam			device_printf(dev, "No I/O space?!\n");
2077178354Ssam			return ENXIO;
207898440Simp		}
2079109323Ssam
2080109323Ssam		sc->wi_io_addr = rman_get_start(sc->iobase);
2081109323Ssam		sc->wi_btag = rman_get_bustag(sc->iobase);
2082109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2083109323Ssam	} else {
2084109323Ssam		sc->mem_rid = rid;
2085127135Snjl		sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2086127135Snjl		    &sc->mem_rid, RF_ACTIVE);
2087178354Ssam		if (sc->mem == NULL) {
2088109323Ssam			device_printf(dev, "No Mem space on prism2.5?\n");
2089178354Ssam			return ENXIO;
209077217Sphk		}
2091109323Ssam
2092109323Ssam		sc->wi_btag = rman_get_bustag(sc->mem);
2093109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->mem);
209477217Sphk	}
209577217Sphk
2096109323Ssam	sc->irq_rid = 0;
2097127135Snjl	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2098127135Snjl	    RF_ACTIVE |
2099109323Ssam	    ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2100178354Ssam	if (sc->irq == NULL) {
2101109323Ssam		wi_free(dev);
2102109323Ssam		device_printf(dev, "No irq?!\n");
2103178354Ssam		return ENXIO;
210477217Sphk	}
2105109323Ssam
2106109323Ssam	sc->sc_dev = dev;
2107109323Ssam	sc->sc_unit = device_get_unit(dev);
2108178354Ssam	return 0;
210977217Sphk}
211093359Simp
2111109323Ssamvoid
2112109323Ssamwi_free(device_t dev)
2113109323Ssam{
2114109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2115109323Ssam
2116109323Ssam	if (sc->iobase != NULL) {
2117109323Ssam		bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2118109323Ssam		sc->iobase = NULL;
2119109323Ssam	}
2120109323Ssam	if (sc->irq != NULL) {
2121109323Ssam		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2122109323Ssam		sc->irq = NULL;
2123109323Ssam	}
2124109323Ssam	if (sc->mem != NULL) {
2125109323Ssam		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
2126109323Ssam		sc->mem = NULL;
2127109323Ssam	}
2128109323Ssam}
2129