if_wi.c revision 228621
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 228621 2011-12-17 10:23:17Z bschmidt $");
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>
8146492Swpaul
8253702Swpaul#include <machine/bus.h>
8353702Swpaul#include <machine/resource.h>
84116951Ssam#include <machine/atomic.h>
8553702Swpaul#include <sys/rman.h>
8653702Swpaul
8746492Swpaul#include <net/if.h>
8846492Swpaul#include <net/if_arp.h>
8946492Swpaul#include <net/ethernet.h>
9046492Swpaul#include <net/if_dl.h>
91190579Ssam#include <net/if_llc.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
111228621Sbschmidtstatic struct ieee80211vap *wi_vap_create(struct ieee80211com *,
112228621Sbschmidt		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
113228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN],
114228621Sbschmidt		    const uint8_t [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);
124192468Ssamstatic int  wi_newstate_hostap(struct ieee80211vap *, enum ieee80211_state,
125192468Ssam		int);
126178354Ssamstatic void wi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
127192468Ssam		int subtype, int rssi, int nf);
128178354Ssamstatic int  wi_reset(struct wi_softc *);
129165089Ssamstatic void wi_watchdog(void *);
130109323Ssamstatic int  wi_ioctl(struct ifnet *, u_long, caddr_t);
131109323Ssamstatic void wi_media_status(struct ifnet *, struct ifmediareq *);
13246492Swpaul
133109323Ssamstatic void wi_rx_intr(struct wi_softc *);
134109323Ssamstatic void wi_tx_intr(struct wi_softc *);
135109323Ssamstatic void wi_tx_ex_intr(struct wi_softc *);
136178354Ssam
137109323Ssamstatic void wi_info_intr(struct wi_softc *);
13846492Swpaul
139178354Ssamstatic int  wi_write_txrate(struct wi_softc *, struct ieee80211vap *);
140178354Ssamstatic int  wi_write_wep(struct wi_softc *, struct ieee80211vap *);
141109323Ssamstatic int  wi_write_multi(struct wi_softc *);
142178354Ssamstatic void wi_update_mcast(struct ifnet *);
143192468Ssamstatic void wi_update_promisc(struct ifnet *);
144109323Ssamstatic int  wi_alloc_fid(struct wi_softc *, int, int *);
145109323Ssamstatic void wi_read_nicid(struct wi_softc *);
146109323Ssamstatic int  wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
14753702Swpaul
148192492Simpstatic int  wi_cmd(struct wi_softc *, int, int, int, int);
149109323Ssamstatic int  wi_seek_bap(struct wi_softc *, int, int);
150109323Ssamstatic int  wi_read_bap(struct wi_softc *, int, int, void *, int);
151109323Ssamstatic int  wi_write_bap(struct wi_softc *, int, int, void *, int);
152109323Ssamstatic int  wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
153109323Ssamstatic int  wi_read_rid(struct wi_softc *, int, void *, int *);
154109323Ssamstatic int  wi_write_rid(struct wi_softc *, int, void *, int);
155178354Ssamstatic int  wi_write_appie(struct wi_softc *, int, const struct ieee80211_appie *);
15677217Sphk
157170530Ssamstatic void wi_scan_start(struct ieee80211com *);
158170530Ssamstatic void wi_scan_end(struct ieee80211com *);
159170530Ssamstatic void wi_set_channel(struct ieee80211com *);
160170530Ssam
161109323Ssamstatic __inline int
162109323Ssamwi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
163109323Ssam{
16453702Swpaul
165109323Ssam	val = htole16(val);
166109323Ssam	return wi_write_rid(sc, rid, &val, sizeof(val));
167109323Ssam}
168109323Ssam
169227309Sedstatic SYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0,
170227309Sed	    "Wireless driver parameters");
171109592Ssam
172109323Ssamstatic	struct timeval lasttxerror;	/* time of last tx error msg */
173109323Ssamstatic	int curtxeps;			/* current tx error msgs/sec */
174111559Ssamstatic	int wi_txerate = 0;		/* tx error rate: max msgs/sec */
175109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate,
176111559Ssam	    0, "max tx error msgs/sec; 0 to disable msgs");
177109323Ssam
178109323Ssam#define	WI_DEBUG
179109323Ssam#ifdef WI_DEBUG
180109323Ssamstatic	int wi_debug = 0;
181109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug,
182109592Ssam	    0, "control debugging printfs");
183109323Ssam#define	DPRINTF(X)	if (wi_debug) printf X
184109323Ssam#else
185109323Ssam#define	DPRINTF(X)
186109323Ssam#endif
187109323Ssam
188109323Ssam#define WI_INTRS	(WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
189109323Ssam
19093825Simpstruct wi_card_ident wi_card_ident[] = {
19193825Simp	/* CARD_ID			CARD_NAME		FIRM_TYPE */
19293825Simp	{ WI_NIC_LUCENT_ID,		WI_NIC_LUCENT_STR,	WI_LUCENT },
19393825Simp	{ WI_NIC_SONY_ID,		WI_NIC_SONY_STR,	WI_LUCENT },
19493825Simp	{ WI_NIC_LUCENT_EMB_ID,		WI_NIC_LUCENT_EMB_STR,	WI_LUCENT },
19593825Simp	{ WI_NIC_EVB2_ID,		WI_NIC_EVB2_STR,	WI_INTERSIL },
19693825Simp	{ WI_NIC_HWB3763_ID,		WI_NIC_HWB3763_STR,	WI_INTERSIL },
19793825Simp	{ WI_NIC_HWB3163_ID,		WI_NIC_HWB3163_STR,	WI_INTERSIL },
19893825Simp	{ WI_NIC_HWB3163B_ID,		WI_NIC_HWB3163B_STR,	WI_INTERSIL },
19993825Simp	{ WI_NIC_EVB3_ID,		WI_NIC_EVB3_STR,	WI_INTERSIL },
20093825Simp	{ WI_NIC_HWB1153_ID,		WI_NIC_HWB1153_STR,	WI_INTERSIL },
20193825Simp	{ WI_NIC_P2_SST_ID,		WI_NIC_P2_SST_STR,	WI_INTERSIL },
20293825Simp	{ WI_NIC_EVB2_SST_ID,		WI_NIC_EVB2_SST_STR,	WI_INTERSIL },
20393825Simp	{ WI_NIC_3842_EVA_ID,		WI_NIC_3842_EVA_STR,	WI_INTERSIL },
20493825Simp	{ WI_NIC_3842_PCMCIA_AMD_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
20593825Simp	{ WI_NIC_3842_PCMCIA_SST_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
206101355Simp	{ WI_NIC_3842_PCMCIA_ATL_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
207101355Simp	{ WI_NIC_3842_PCMCIA_ATS_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
20893825Simp	{ WI_NIC_3842_MINI_AMD_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
20993825Simp	{ WI_NIC_3842_MINI_SST_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
210101355Simp	{ WI_NIC_3842_MINI_ATL_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
211101355Simp	{ WI_NIC_3842_MINI_ATS_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
21293825Simp	{ WI_NIC_3842_PCI_AMD_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
21393825Simp	{ WI_NIC_3842_PCI_SST_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
214101355Simp	{ WI_NIC_3842_PCI_ATS_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
215101355Simp	{ WI_NIC_3842_PCI_ATL_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
21693825Simp	{ WI_NIC_P3_PCMCIA_AMD_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
21793825Simp	{ WI_NIC_P3_PCMCIA_SST_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
218101355Simp	{ WI_NIC_P3_PCMCIA_ATL_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
219101355Simp	{ WI_NIC_P3_PCMCIA_ATS_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
22093825Simp	{ WI_NIC_P3_MINI_AMD_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
22193825Simp	{ WI_NIC_P3_MINI_SST_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
222101355Simp	{ WI_NIC_P3_MINI_ATL_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
223101355Simp	{ WI_NIC_P3_MINI_ATS_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
22493825Simp	{ 0,	NULL,	0 },
22593825Simp};
22693825Simp
227180919Simpstatic char *wi_firmware_names[] = { "none", "Hermes", "Intersil", "Symbol" };
228180919Simp
229109323Ssamdevclass_t wi_devclass;
23046492Swpaul
23193611Simpint
232109323Ssamwi_attach(device_t dev)
23374906Salfred{
234109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
235178354Ssam	struct ieee80211com *ic;
236147256Sbrooks	struct ifnet *ifp;
237116951Ssam	int i, nrates, buflen;
238109323Ssam	u_int16_t val;
239109323Ssam	u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
240116951Ssam	struct ieee80211_rateset *rs;
241180919Simp	struct sysctl_ctx_list *sctx;
242180919Simp	struct sysctl_oid *soid;
243109323Ssam	static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
244109323Ssam		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
245109323Ssam	};
246109323Ssam	int error;
247190526Ssam	uint8_t macaddr[IEEE80211_ADDR_LEN];
24874906Salfred
249178354Ssam	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
250147256Sbrooks	if (ifp == NULL) {
251147256Sbrooks		device_printf(dev, "can not if_alloc\n");
252147256Sbrooks		wi_free(dev);
253178354Ssam		return ENOSPC;
254147256Sbrooks	}
255178354Ssam	ic = ifp->if_l2com;
256147256Sbrooks
257138571Ssam	sc->sc_firmware_type = WI_NOTYPE;
258123339Simp	sc->wi_cmd_count = 500;
25946492Swpaul	/* Reset the NIC. */
260178354Ssam	if (wi_reset(sc) != 0) {
261178354Ssam		wi_free(dev);
262109323Ssam		return ENXIO;		/* XXX */
263178354Ssam	}
26446492Swpaul
265178354Ssam	/* Read NIC identification */
266178354Ssam	wi_read_nicid(sc);
267178354Ssam	switch (sc->sc_firmware_type) {
268178354Ssam	case WI_LUCENT:
269178354Ssam		if (sc->sc_sta_firmware_ver < 60006)
270178354Ssam			goto reject;
271178354Ssam		break;
272178354Ssam	case WI_INTERSIL:
273178354Ssam		if (sc->sc_sta_firmware_ver < 800)
274178354Ssam			goto reject;
275178354Ssam		break;
276178354Ssam	default:
277178354Ssam	reject:
278178354Ssam		device_printf(dev, "Sorry, this card is not supported "
279178354Ssam		    "(type %d, firmware ver %d)\n",
280178354Ssam		    sc->sc_firmware_type, sc->sc_sta_firmware_ver);
281178354Ssam		wi_free(dev);
282178354Ssam		return EOPNOTSUPP;
283178354Ssam	}
284178354Ssam
285180919Simp	/* Export info about the device via sysctl */
286180919Simp	sctx = device_get_sysctl_ctx(dev);
287180919Simp	soid = device_get_sysctl_tree(dev);
288180919Simp	SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
289180919Simp	    "firmware_type", CTLFLAG_RD,
290180919Simp	    wi_firmware_names[sc->sc_firmware_type], 0,
291180919Simp	    "Firmware type string");
292180919Simp	SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "sta_version",
293180919Simp	    CTLFLAG_RD, &sc->sc_sta_firmware_ver, 0,
294180919Simp	    "Station Firmware version");
295180919Simp	if (sc->sc_firmware_type == WI_INTERSIL)
296180919Simp		SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
297180919Simp		    "pri_version", CTLFLAG_RD, &sc->sc_pri_firmware_ver, 0,
298180919Simp		    "Primary Firmware version");
299217586Smdf	SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "nic_id",
300180919Simp	    CTLFLAG_RD, &sc->sc_nic_id, 0, "NIC id");
301180919Simp	SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "nic_name",
302180919Simp	    CTLFLAG_RD, sc->sc_nic_name, 0, "NIC name");
303180919Simp
304178354Ssam	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
305178354Ssam	    MTX_DEF | MTX_RECURSE);
306178354Ssam	callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
307178354Ssam
30876438Swpaul	/*
30976438Swpaul	 * Read the station address.
31076438Swpaul	 * And do it twice. I've seen PRISM-based cards that return
31176438Swpaul	 * an error when trying to read it the first time, which causes
31276438Swpaul	 * the probe to fail.
31376438Swpaul	 */
314109323Ssam	buflen = IEEE80211_ADDR_LEN;
315190526Ssam	error = wi_read_rid(sc, WI_RID_MAC_NODE, macaddr, &buflen);
316109323Ssam	if (error != 0) {
317109323Ssam		buflen = IEEE80211_ADDR_LEN;
318190526Ssam		error = wi_read_rid(sc, WI_RID_MAC_NODE, macaddr, &buflen);
319109323Ssam	}
320190526Ssam	if (error || IEEE80211_ADDR_EQ(macaddr, empty_macaddr)) {
321109323Ssam		if (error != 0)
322109323Ssam			device_printf(dev, "mac read failed %d\n", error);
323148714Simp		else {
324109323Ssam			device_printf(dev, "mac read failed (all zeros)\n");
325148714Simp			error = ENXIO;
326148714Simp		}
32775149Simp		wi_free(dev);
32875149Simp		return (error);
32975149Simp	}
33046492Swpaul
331178354Ssam	ifp->if_softc = sc;
332121816Sbrooks	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
33346492Swpaul	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
33446492Swpaul	ifp->if_ioctl = wi_ioctl;
33546492Swpaul	ifp->if_start = wi_start;
33646492Swpaul	ifp->if_init = wi_init;
337207554Ssobomax	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
338207554Ssobomax	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
339132986Smlaier	IFQ_SET_READY(&ifp->if_snd);
34046492Swpaul
341138571Ssam	ic->ic_ifp = ifp;
342109323Ssam	ic->ic_phytype = IEEE80211_T_DS;
343109323Ssam	ic->ic_opmode = IEEE80211_M_STA;
344178957Ssam	ic->ic_caps = IEEE80211_C_STA
345178957Ssam		    | IEEE80211_C_PMGT
346178354Ssam		    | IEEE80211_C_MONITOR
347138571Ssam		    ;
34846492Swpaul
349116951Ssam	/*
350116951Ssam	 * Query the card for available channels and setup the
351116951Ssam	 * channel table.  We assume these are all 11b channels.
352116951Ssam	 */
353109323Ssam	buflen = sizeof(val);
354109323Ssam	if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
355109323Ssam		val = htole16(0x1fff);	/* assume 1-11 */
356116951Ssam	KASSERT(val != 0, ("wi_attach: no available channels listed!"));
357116951Ssam
358116951Ssam	val <<= 1;			/* shift for base 1 indices */
359116951Ssam	for (i = 1; i < 16; i++) {
360170530Ssam		struct ieee80211_channel *c;
361170530Ssam
362144986Smdodd		if (!isset((u_int8_t*)&val, i))
363144986Smdodd			continue;
364170530Ssam		c = &ic->ic_channels[ic->ic_nchans++];
365170530Ssam		c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
366170530Ssam		c->ic_flags = IEEE80211_CHAN_B;
367170530Ssam		c->ic_ieee = i;
368178354Ssam		/* XXX txpowers? */
369109323Ssam	}
37046492Swpaul
37146563Swpaul	/*
37298440Simp	 * Set flags based on firmware version.
37398440Simp	 */
37498440Simp	switch (sc->sc_firmware_type) {
37598440Simp	case WI_LUCENT:
376112363Simp		sc->sc_ntxbuf = 1;
377178354Ssam		ic->ic_caps |= IEEE80211_C_IBSS;
378119784Ssam
379178354Ssam		sc->sc_ibss_port = WI_PORTTYPE_BSS;
380178354Ssam		sc->sc_monitor_port = WI_PORTTYPE_ADHOC;
381119784Ssam		sc->sc_min_rssi = WI_LUCENT_MIN_RSSI;
382119784Ssam		sc->sc_max_rssi = WI_LUCENT_MAX_RSSI;
383119784Ssam		sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
38498440Simp		break;
38598440Simp	case WI_INTERSIL:
386112363Simp		sc->sc_ntxbuf = WI_NTXBUF;
387178354Ssam		sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR
388178354Ssam			     |  WI_FLAGS_HAS_ROAMING;
389123339Simp		/*
390123339Simp		 * Old firmware are slow, so give peace a chance.
391123339Simp		 */
392123339Simp		if (sc->sc_sta_firmware_ver < 10000)
393123339Simp			sc->wi_cmd_count = 5000;
394109323Ssam		if (sc->sc_sta_firmware_ver > 10101)
395109323Ssam			sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
396178354Ssam		ic->ic_caps |= IEEE80211_C_IBSS;
397109396Simp		/*
398109396Simp		 * version 0.8.3 and newer are the only ones that are known
399109396Simp		 * to currently work.  Earlier versions can be made to work,
400178354Ssam		 * at least according to the Linux driver but we require
401178354Ssam		 * monitor mode so this is irrelevant.
402109396Simp		 */
403178354Ssam		ic->ic_caps |= IEEE80211_C_HOSTAP;
404178354Ssam		if (sc->sc_sta_firmware_ver >= 10603)
405178354Ssam			sc->sc_flags |= WI_FLAGS_HAS_ENHSECURITY;
406178354Ssam		if (sc->sc_sta_firmware_ver >= 10700) {
407178354Ssam			/*
408178354Ssam			 * 1.7.0+ have the necessary support for sta mode WPA.
409178354Ssam			 */
410178354Ssam			sc->sc_flags |= WI_FLAGS_HAS_WPASUPPORT;
411178354Ssam			ic->ic_caps |= IEEE80211_C_WPA;
412178354Ssam		}
413119784Ssam
414178354Ssam		sc->sc_ibss_port = WI_PORTTYPE_IBSS;
415178354Ssam		sc->sc_monitor_port = WI_PORTTYPE_APSILENT;
416119784Ssam		sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
417119784Ssam		sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
418119784Ssam		sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
41998440Simp		break;
42098440Simp	}
42198440Simp
42298440Simp	/*
42356965Swpaul	 * Find out if we support WEP on this card.
42456965Swpaul	 */
425109323Ssam	buflen = sizeof(val);
426109323Ssam	if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
427109323Ssam	    val != htole16(0))
428178354Ssam		ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
42956965Swpaul
430109323Ssam	/* Find supported rates. */
431109323Ssam	buflen = sizeof(ratebuf);
432116951Ssam	rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
433109323Ssam	if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
434116951Ssam		nrates = le16toh(*(u_int16_t *)ratebuf);
435116951Ssam		if (nrates > IEEE80211_RATE_MAXSIZE)
436116951Ssam			nrates = IEEE80211_RATE_MAXSIZE;
437116951Ssam		rs->rs_nrates = 0;
438116951Ssam		for (i = 0; i < nrates; i++)
439116951Ssam			if (ratebuf[2+i])
440116951Ssam				rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i];
441109323Ssam	} else {
442109323Ssam		/* XXX fallback on error? */
44398440Simp	}
44477217Sphk
445109323Ssam	buflen = sizeof(val);
446109323Ssam	if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
447109323Ssam	    wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
448119784Ssam		sc->sc_dbm_offset = le16toh(val);
449119784Ssam	}
45046492Swpaul
451109323Ssam	sc->sc_portnum = WI_DEFAULT_PORT;
45287383Simp
453190526Ssam	ieee80211_ifattach(ic, macaddr);
454160991Ssam	ic->ic_raw_xmit = wi_raw_xmit;
455170530Ssam	ic->ic_scan_start = wi_scan_start;
456170530Ssam	ic->ic_scan_end = wi_scan_end;
457170530Ssam	ic->ic_set_channel = wi_set_channel;
458170530Ssam
459178354Ssam	ic->ic_vap_create = wi_vap_create;
460178354Ssam	ic->ic_vap_delete = wi_vap_delete;
461178354Ssam	ic->ic_update_mcast = wi_update_mcast;
462192468Ssam	ic->ic_update_promisc = wi_update_promisc;
463109323Ssam
464192468Ssam	ieee80211_radiotap_attach(ic,
465192468Ssam	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
466192468Ssam		WI_TX_RADIOTAP_PRESENT,
467192468Ssam	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
468192468Ssam		WI_RX_RADIOTAP_PRESENT);
469119784Ssam
470138571Ssam	if (bootverbose)
471138571Ssam		ieee80211_announce(ic);
472138571Ssam
473180826Simp	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
474180826Simp	    NULL, wi_intr, sc, &sc->wi_intrhand);
475180826Simp	if (error) {
476180826Simp		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
477180826Simp		ieee80211_ifdetach(ic);
478180826Simp		if_free(sc->sc_ifp);
479180826Simp		wi_free(dev);
480180826Simp		return error;
481180826Simp	}
482180826Simp
483109323Ssam	return (0);
48487383Simp}
48587383Simp
486109323Ssamint
487109323Ssamwi_detach(device_t dev)
48846492Swpaul{
489109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
490147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
491178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
49246492Swpaul
493109323Ssam	WI_LOCK(sc);
49446492Swpaul
495109323Ssam	/* check if device was removed */
496123098Simp	sc->wi_gone |= !bus_child_present(dev);
49746492Swpaul
498178354Ssam	wi_stop_locked(sc, 0);
499150678Sru	WI_UNLOCK(sc);
500178354Ssam	ieee80211_ifdetach(ic);
50146492Swpaul
502109323Ssam	bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
503150306Simp	if_free(sc->sc_ifp);
504109323Ssam	wi_free(dev);
505109323Ssam	mtx_destroy(&sc->sc_mtx);
506109323Ssam	return (0);
507109323Ssam}
50893359Simp
509178354Ssamstatic struct ieee80211vap *
510228621Sbschmidtwi_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
511228621Sbschmidt    enum ieee80211_opmode opmode, int flags,
512228621Sbschmidt    const uint8_t bssid[IEEE80211_ADDR_LEN],
513228621Sbschmidt    const uint8_t mac[IEEE80211_ADDR_LEN])
514109323Ssam{
515178354Ssam	struct wi_softc *sc = ic->ic_ifp->if_softc;
516178354Ssam	struct wi_vap *wvp;
517178354Ssam	struct ieee80211vap *vap;
51893359Simp
519178354Ssam	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
520178354Ssam		return NULL;
521178354Ssam	wvp = (struct wi_vap *) malloc(sizeof(struct wi_vap),
522178354Ssam	    M_80211_VAP, M_NOWAIT | M_ZERO);
523178354Ssam	if (wvp == NULL)
524178354Ssam		return NULL;
52593359Simp
526178354Ssam	vap = &wvp->wv_vap;
527178354Ssam	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
52893359Simp
529178354Ssam	vap->iv_max_aid = WI_MAX_AID;
53093359Simp
531178354Ssam	switch (opmode) {
532178354Ssam	case IEEE80211_M_STA:
533178354Ssam		sc->sc_porttype = WI_PORTTYPE_BSS;
534178354Ssam		wvp->wv_newstate = vap->iv_newstate;
535178354Ssam		vap->iv_newstate = wi_newstate_sta;
536178354Ssam		/* need to filter mgt frames to avoid confusing state machine */
537178354Ssam		wvp->wv_recv_mgmt = vap->iv_recv_mgmt;
538178354Ssam		vap->iv_recv_mgmt = wi_recv_mgmt;
539109323Ssam		break;
540178354Ssam	case IEEE80211_M_IBSS:
541178354Ssam		sc->sc_porttype = sc->sc_ibss_port;
542178354Ssam		wvp->wv_newstate = vap->iv_newstate;
543178354Ssam		vap->iv_newstate = wi_newstate_sta;
544109323Ssam		break;
545178354Ssam	case IEEE80211_M_AHDEMO:
546178354Ssam		sc->sc_porttype = WI_PORTTYPE_ADHOC;
547109323Ssam		break;
548178354Ssam	case IEEE80211_M_HOSTAP:
549178354Ssam		sc->sc_porttype = WI_PORTTYPE_HOSTAP;
550178354Ssam		wvp->wv_newstate = vap->iv_newstate;
551178354Ssam		vap->iv_newstate = wi_newstate_hostap;
552178354Ssam		break;
553178354Ssam	case IEEE80211_M_MONITOR:
554178354Ssam		sc->sc_porttype = sc->sc_monitor_port;
555178354Ssam		break;
556178354Ssam	default:
557178354Ssam		break;
55893359Simp	}
559178354Ssam
560178354Ssam	/* complete setup */
561178354Ssam	ieee80211_vap_attach(vap, ieee80211_media_change, wi_media_status);
562178354Ssam	ic->ic_opmode = opmode;
563178354Ssam	return vap;
56446492Swpaul}
56546492Swpaul
566178354Ssamstatic void
567178354Ssamwi_vap_delete(struct ieee80211vap *vap)
568178354Ssam{
569178354Ssam	struct wi_vap *wvp = WI_VAP(vap);
570178354Ssam
571178354Ssam	ieee80211_vap_detach(vap);
572178354Ssam	free(wvp, M_80211_VAP);
573178354Ssam}
574178354Ssam
575194023Savgint
576109323Ssamwi_shutdown(device_t dev)
57746492Swpaul{
578109323Ssam	struct wi_softc *sc = device_get_softc(dev);
57946492Swpaul
580178354Ssam	wi_stop(sc, 1);
581194023Savg	return (0);
58246492Swpaul}
58346492Swpaul
584109323Ssamvoid
585109323Ssamwi_intr(void *arg)
58646492Swpaul{
587109323Ssam	struct wi_softc *sc = arg;
588147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
589112363Simp	u_int16_t status;
59046492Swpaul
591109323Ssam	WI_LOCK(sc);
59246492Swpaul
593123098Simp	if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) {
594113327Simp		CSR_WRITE_2(sc, WI_INT_EN, 0);
595123098Simp		CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
596109323Ssam		WI_UNLOCK(sc);
59746492Swpaul		return;
598109323Ssam	}
59946492Swpaul
600112363Simp	/* Disable interrupts. */
601112363Simp	CSR_WRITE_2(sc, WI_INT_EN, 0);
60246492Swpaul
603112363Simp	status = CSR_READ_2(sc, WI_EVENT_STAT);
604112363Simp	if (status & WI_EV_RX)
605112363Simp		wi_rx_intr(sc);
606112363Simp	if (status & WI_EV_ALLOC)
607112363Simp		wi_tx_intr(sc);
608112363Simp	if (status & WI_EV_TX_EXC)
609112363Simp		wi_tx_ex_intr(sc);
610112363Simp	if (status & WI_EV_INFO)
611112363Simp		wi_info_intr(sc);
612148887Srwatson	if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
613132986Smlaier	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
614165087Ssam		wi_start_locked(ifp);
61546492Swpaul
616112363Simp	/* Re-enable interrupts. */
617112363Simp	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
61846492Swpaul
619109323Ssam	WI_UNLOCK(sc);
62046492Swpaul
62146492Swpaul	return;
62246492Swpaul}
62346492Swpaul
624178354Ssamstatic void
625178354Ssamwi_enable(struct wi_softc *sc)
626178354Ssam{
627178354Ssam	/* Enable interrupts */
628178354Ssam	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
629178354Ssam
630178354Ssam	/* enable port */
631178354Ssam	wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
632178354Ssam	sc->sc_enabled = 1;
633178354Ssam}
634178354Ssam
635178354Ssamstatic int
636178354Ssamwi_setup_locked(struct wi_softc *sc, int porttype, int mode,
637178354Ssam	uint8_t mac[IEEE80211_ADDR_LEN])
638178354Ssam{
639178354Ssam	int i;
640178354Ssam
641178354Ssam	wi_reset(sc);
642178354Ssam
643178354Ssam	wi_write_val(sc, WI_RID_PORTTYPE, porttype);
644178354Ssam	wi_write_val(sc, WI_RID_CREATE_IBSS, mode);
645178354Ssam	wi_write_val(sc, WI_RID_MAX_DATALEN, 2304);
646178354Ssam	/* XXX IEEE80211_BPF_NOACK wants 0 */
647178354Ssam	wi_write_val(sc, WI_RID_ALT_RETRY_CNT, 2);
648178354Ssam	if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
649178354Ssam		wi_write_val(sc, WI_RID_ROAMING_MODE, 3); /* NB: disabled */
650178354Ssam
651178354Ssam	wi_write_rid(sc, WI_RID_MAC_NODE, mac, IEEE80211_ADDR_LEN);
652178354Ssam
653178354Ssam	/* Allocate fids for the card */
654178354Ssam	sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
655178354Ssam	for (i = 0; i < sc->sc_ntxbuf; i++) {
656178354Ssam		int error = wi_alloc_fid(sc, sc->sc_buflen,
657178354Ssam		    &sc->sc_txd[i].d_fid);
658178354Ssam		if (error) {
659178354Ssam			device_printf(sc->sc_dev,
660178354Ssam			    "tx buffer allocation failed (error %u)\n",
661178354Ssam			    error);
662178354Ssam			return error;
663178354Ssam		}
664178354Ssam		sc->sc_txd[i].d_len = 0;
665178354Ssam	}
666178354Ssam	sc->sc_txcur = sc->sc_txnext = 0;
667178354Ssam
668178354Ssam	return 0;
669178354Ssam}
670178354Ssam
671178354Ssamstatic void
672178354Ssamwi_init_locked(struct wi_softc *sc)
673178354Ssam{
674178354Ssam	struct ifnet *ifp = sc->sc_ifp;
675178354Ssam	int wasenabled;
676178354Ssam
677178354Ssam	WI_LOCK_ASSERT(sc);
678178354Ssam
679178354Ssam	wasenabled = sc->sc_enabled;
680178354Ssam	if (wasenabled)
681178354Ssam		wi_stop_locked(sc, 1);
682178354Ssam
683190526Ssam	if (wi_setup_locked(sc, sc->sc_porttype, 3, IF_LLADDR(ifp)) != 0) {
684178354Ssam		if_printf(ifp, "interface not running\n");
685178354Ssam		wi_stop_locked(sc, 1);
686178354Ssam		return;
687178354Ssam	}
688178354Ssam
689178354Ssam	ifp->if_drv_flags |= IFF_DRV_RUNNING;
690178354Ssam	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
691178354Ssam
692178354Ssam	callout_reset(&sc->sc_watchdog, hz, wi_watchdog, sc);
693178354Ssam
694178354Ssam	wi_enable(sc);			/* Enable desired port */
695178354Ssam}
696178354Ssam
697109323Ssamvoid
698109323Ssamwi_init(void *arg)
69946492Swpaul{
700109323Ssam	struct wi_softc *sc = arg;
701147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
702178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
70346492Swpaul
704178354Ssam	WI_LOCK(sc);
705178354Ssam	wi_init_locked(sc);
706178354Ssam	WI_UNLOCK(sc);
70767092Swpaul
708178931Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
709178931Sthompsa		ieee80211_start_all(ic);		/* start all vap's */
710178354Ssam}
711170530Ssam
712178354Ssamstatic void
713178354Ssamwi_stop_locked(struct wi_softc *sc, int disable)
714178354Ssam{
715178354Ssam	struct ifnet *ifp = sc->sc_ifp;
71646492Swpaul
717178354Ssam	WI_LOCK_ASSERT(sc);
71846492Swpaul
719178354Ssam	if (sc->sc_enabled && !sc->wi_gone) {
720178354Ssam		CSR_WRITE_2(sc, WI_INT_EN, 0);
721178354Ssam		wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
722178354Ssam		if (disable)
723178354Ssam			sc->sc_enabled = 0;
724178354Ssam	} else if (sc->wi_gone && disable)	/* gone --> not enabled */
725178354Ssam		sc->sc_enabled = 0;
726178354Ssam
727178354Ssam	callout_stop(&sc->sc_watchdog);
728178354Ssam	sc->sc_tx_timer = 0;
729178354Ssam	sc->sc_false_syns = 0;
730178354Ssam
731178354Ssam	ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE | IFF_DRV_RUNNING);
732178354Ssam}
733178354Ssam
734178354Ssamvoid
735178354Ssamwi_stop(struct wi_softc *sc, int disable)
736178354Ssam{
737170530Ssam	WI_LOCK(sc);
738178354Ssam	wi_stop_locked(sc, disable);
739178354Ssam	WI_UNLOCK(sc);
740178354Ssam}
741170530Ssam
742178354Ssamstatic void
743178354Ssamwi_set_channel(struct ieee80211com *ic)
744178354Ssam{
745178354Ssam	struct ifnet *ifp = ic->ic_ifp;
746178354Ssam	struct wi_softc *sc = ifp->if_softc;
747160991Ssam
748178354Ssam	DPRINTF(("%s: channel %d, %sscanning\n", __func__,
749178354Ssam	    ieee80211_chan2ieee(ic, ic->ic_curchan),
750178354Ssam	    ic->ic_flags & IEEE80211_F_SCAN ? "" : "!"));
75146492Swpaul
752178354Ssam	WI_LOCK(sc);
753116951Ssam	wi_write_val(sc, WI_RID_OWN_CHNL,
754178354Ssam	    ieee80211_chan2ieee(ic, ic->ic_curchan));
755178354Ssam	WI_UNLOCK(sc);
756178354Ssam}
75775373Salfred
758178354Ssamstatic void
759178354Ssamwi_scan_start(struct ieee80211com *ic)
760178354Ssam{
761178354Ssam	struct ifnet *ifp = ic->ic_ifp;
762178354Ssam	struct wi_softc *sc = ifp->if_softc;
763178354Ssam	struct ieee80211_scan_state *ss = ic->ic_scan;
76446492Swpaul
765178354Ssam	DPRINTF(("%s\n", __func__));
76646492Swpaul
767178354Ssam	WI_LOCK(sc);
768109323Ssam	/*
769178354Ssam	 * Switch device to monitor mode.
770109323Ssam	 */
771178354Ssam	wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_monitor_port);
772178354Ssam	if (sc->sc_firmware_type == WI_INTERSIL) {
773178354Ssam		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
774178354Ssam		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
77575373Salfred	}
776178354Ssam	/* force full dwell time to compensate for firmware overhead */
777178354Ssam	ss->ss_mindwell = ss->ss_maxdwell = msecs_to_ticks(400);
778178354Ssam	WI_UNLOCK(sc);
77946492Swpaul
780178354Ssam}
78167092Swpaul
782178354Ssamstatic void
783178354Ssamwi_scan_end(struct ieee80211com *ic)
784178354Ssam{
785178354Ssam	struct ifnet *ifp = ic->ic_ifp;
786178354Ssam	struct wi_softc *sc = ifp->if_softc;
78746492Swpaul
788178354Ssam	DPRINTF(("%s: restore port type %d\n", __func__, sc->sc_porttype));
789178354Ssam
790178354Ssam	WI_LOCK(sc);
791178354Ssam	wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_porttype);
792178354Ssam	if (sc->sc_firmware_type == WI_INTERSIL) {
793178354Ssam		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
794178354Ssam		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
79570073Swpaul	}
796178354Ssam	WI_UNLOCK(sc);
797178354Ssam}
79870073Swpaul
799178354Ssamstatic void
800178354Ssamwi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
801192468Ssam	int subtype, int rssi, int nf)
802178354Ssam{
803178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
80446492Swpaul
805178354Ssam	switch (subtype) {
806178354Ssam	case IEEE80211_FC0_SUBTYPE_AUTH:
807178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
808178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
809178354Ssam		/* NB: filter frames that trigger state changes */
810178354Ssam		return;
811170530Ssam	}
812192468Ssam	WI_VAP(vap)->wv_recv_mgmt(ni, m, subtype, rssi, nf);
813178354Ssam}
81446492Swpaul
815178354Ssamstatic int
816178354Ssamwi_newstate_sta(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
817178354Ssam{
818178354Ssam	struct ieee80211com *ic = vap->iv_ic;
819178354Ssam	struct ifnet *ifp = ic->ic_ifp;
820178354Ssam	struct ieee80211_node *bss;
821178354Ssam	struct wi_softc *sc = ifp->if_softc;
82294397Simp
823178354Ssam	DPRINTF(("%s: %s -> %s\n", __func__,
824178354Ssam		ieee80211_state_name[vap->iv_state],
825178354Ssam		ieee80211_state_name[nstate]));
826178354Ssam
827178354Ssam	if (nstate == IEEE80211_S_AUTH) {
828178354Ssam		WI_LOCK(sc);
829178354Ssam		wi_setup_locked(sc, WI_PORTTYPE_BSS, 3, vap->iv_myaddr);
830178354Ssam
831178354Ssam		if (vap->iv_flags & IEEE80211_F_PMGTON) {
832178354Ssam			wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
833178354Ssam			wi_write_val(sc, WI_RID_PM_ENABLED, 1);
834178354Ssam		}
835178354Ssam		wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold);
836178354Ssam		if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
837178354Ssam			wi_write_val(sc, WI_RID_FRAG_THRESH,
838178354Ssam			    vap->iv_fragthreshold);
839178354Ssam		wi_write_txrate(sc, vap);
840178354Ssam
841178354Ssam		bss = vap->iv_bss;
842178354Ssam		wi_write_ssid(sc, WI_RID_DESIRED_SSID, bss->ni_essid, bss->ni_esslen);
843178354Ssam		wi_write_val(sc, WI_RID_OWN_CHNL,
844178354Ssam		    ieee80211_chan2ieee(ic, bss->ni_chan));
845178354Ssam
846178354Ssam		/* Configure WEP. */
847178354Ssam		if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP)
848178354Ssam			wi_write_wep(sc, vap);
849178354Ssam		else
850178354Ssam			sc->sc_encryption = 0;
851178354Ssam
852178354Ssam		if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) &&
853178354Ssam		    (vap->iv_flags & IEEE80211_F_WPA)) {
854178354Ssam			wi_write_val(sc, WI_RID_WPA_HANDLING, 1);
855178354Ssam			if (vap->iv_appie_wpa != NULL)
856178354Ssam				wi_write_appie(sc, WI_RID_WPA_DATA,
857178354Ssam				    vap->iv_appie_wpa);
858178354Ssam		}
859178354Ssam
860178354Ssam		wi_enable(sc);		/* enable port */
861178354Ssam
862178354Ssam		/* Lucent firmware does not support the JOIN RID. */
863178354Ssam		if (sc->sc_firmware_type == WI_INTERSIL) {
864178354Ssam			struct wi_joinreq join;
865178354Ssam
866178354Ssam			memset(&join, 0, sizeof(join));
867178354Ssam			IEEE80211_ADDR_COPY(&join.wi_bssid, bss->ni_bssid);
868116951Ssam			join.wi_chan = htole16(
869178354Ssam			    ieee80211_chan2ieee(ic, bss->ni_chan));
870109323Ssam			wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
871178354Ssam		}
872178354Ssam		WI_UNLOCK(sc);
87375373Salfred
874178354Ssam		/*
875178354Ssam		 * NB: don't go through 802.11 layer, it'll send auth frame;
876178354Ssam		 * instead we drive the state machine from the link status
877178354Ssam		 * notification we get on association.
878178354Ssam		 */
879178354Ssam		vap->iv_state = nstate;
880191746Sthompsa		return (0);
88170073Swpaul	}
882178354Ssam	return WI_VAP(vap)->wv_newstate(vap, nstate, arg);
88346492Swpaul}
88446492Swpaul
885178354Ssamstatic int
886178354Ssamwi_newstate_hostap(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
88746492Swpaul{
888178354Ssam	struct ieee80211com *ic = vap->iv_ic;
889178354Ssam	struct ifnet *ifp = ic->ic_ifp;
890178354Ssam	struct ieee80211_node *bss;
891109323Ssam	struct wi_softc *sc = ifp->if_softc;
892178354Ssam	int error;
89346492Swpaul
894178354Ssam	DPRINTF(("%s: %s -> %s\n", __func__,
895178354Ssam		ieee80211_state_name[vap->iv_state],
896178354Ssam		ieee80211_state_name[nstate]));
89774998Swpaul
898178354Ssam	error = WI_VAP(vap)->wv_newstate(vap, nstate, arg);
899178354Ssam	if (error == 0 && nstate == IEEE80211_S_RUN) {
900178354Ssam		WI_LOCK(sc);
901178354Ssam		wi_setup_locked(sc, WI_PORTTYPE_HOSTAP, 0, vap->iv_myaddr);
902178354Ssam
903178354Ssam		bss = vap->iv_bss;
904178354Ssam		wi_write_ssid(sc, WI_RID_OWN_SSID,
905178354Ssam		    bss->ni_essid, bss->ni_esslen);
906178354Ssam		wi_write_val(sc, WI_RID_OWN_CHNL,
907178354Ssam		    ieee80211_chan2ieee(ic, bss->ni_chan));
908178354Ssam		wi_write_val(sc, WI_RID_BASIC_RATE, 0x3);
909178354Ssam		wi_write_val(sc, WI_RID_SUPPORT_RATE, 0xf);
910178354Ssam		wi_write_txrate(sc, vap);
911178354Ssam
912178354Ssam		wi_write_val(sc, WI_RID_OWN_BEACON_INT, bss->ni_intval);
913178354Ssam		wi_write_val(sc, WI_RID_DTIM_PERIOD, vap->iv_dtim_period);
914178354Ssam
915178354Ssam		wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold);
916178354Ssam		if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
917178354Ssam			wi_write_val(sc, WI_RID_FRAG_THRESH,
918178354Ssam			    vap->iv_fragthreshold);
919178354Ssam
920178354Ssam		if ((sc->sc_flags & WI_FLAGS_HAS_ENHSECURITY) &&
921178354Ssam		    (vap->iv_flags & IEEE80211_F_HIDESSID)) {
922178354Ssam			/*
923178354Ssam			 * bit 0 means hide SSID in beacons,
924178354Ssam			 * bit 1 means don't respond to bcast probe req
925178354Ssam			 */
926178354Ssam			wi_write_val(sc, WI_RID_ENH_SECURITY, 0x3);
92770073Swpaul		}
92870073Swpaul
929178354Ssam		if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) &&
930178354Ssam		    (vap->iv_flags & IEEE80211_F_WPA) &&
931178354Ssam		    vap->iv_appie_wpa != NULL)
932178354Ssam			wi_write_appie(sc, WI_RID_WPA_DATA, vap->iv_appie_wpa);
93346492Swpaul
934178354Ssam		wi_write_val(sc, WI_RID_PROMISC, 0);
935178354Ssam
936178354Ssam		/* Configure WEP. */
937178354Ssam		if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP)
938178354Ssam			wi_write_wep(sc, vap);
939178354Ssam		else
940178354Ssam			sc->sc_encryption = 0;
941178354Ssam
942178354Ssam		wi_enable(sc);		/* enable port */
943178354Ssam		WI_UNLOCK(sc);
944178354Ssam	}
945178354Ssam	return error;
94646492Swpaul}
94746492Swpaul
948109323Ssamstatic void
949165087Ssamwi_start_locked(struct ifnet *ifp)
95046492Swpaul{
951109323Ssam	struct wi_softc	*sc = ifp->if_softc;
952119150Ssam	struct ieee80211_node *ni;
953109323Ssam	struct ieee80211_frame *wh;
954109323Ssam	struct mbuf *m0;
955178354Ssam	struct ieee80211_key *k;
956109323Ssam	struct wi_frame frmhdr;
957190579Ssam	const struct llc *llc;
958160991Ssam	int cur;
95946492Swpaul
960165087Ssam	WI_LOCK_ASSERT(sc);
96146492Swpaul
962165087Ssam	if (sc->wi_gone)
963109323Ssam		return;
96446492Swpaul
965109323Ssam	memset(&frmhdr, 0, sizeof(frmhdr));
966109323Ssam	cur = sc->sc_txnext;
967109323Ssam	for (;;) {
968178354Ssam		IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
969178354Ssam		if (m0 == NULL)
970178354Ssam			break;
971178354Ssam		if (sc->sc_txd[cur].d_len != 0) {
972178354Ssam			IFQ_DRV_PREPEND(&ifp->if_snd, m0);
973178354Ssam			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
974178354Ssam			break;
975178354Ssam		}
976178354Ssam		ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
97746492Swpaul
978190579Ssam		/* reconstruct 802.3 header */
979178354Ssam		wh = mtod(m0, struct ieee80211_frame *);
980190579Ssam		switch (wh->i_fc[1]) {
981190579Ssam		case IEEE80211_FC1_DIR_TODS:
982190579Ssam			IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost,
983190579Ssam			    wh->i_addr2);
984190579Ssam			IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost,
985190579Ssam			    wh->i_addr3);
986190579Ssam			break;
987190579Ssam		case IEEE80211_FC1_DIR_NODS:
988190579Ssam			IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost,
989190579Ssam			    wh->i_addr2);
990190579Ssam			IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost,
991190579Ssam			    wh->i_addr1);
992190579Ssam			break;
993190579Ssam		case IEEE80211_FC1_DIR_FROMDS:
994190579Ssam			IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost,
995190579Ssam			    wh->i_addr3);
996190579Ssam			IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost,
997190579Ssam			    wh->i_addr1);
998190579Ssam			break;
999190579Ssam		}
1000190579Ssam		llc = (const struct llc *)(
1001190579Ssam		    mtod(m0, const uint8_t *) + ieee80211_hdrsize(wh));
1002190579Ssam		frmhdr.wi_ehdr.ether_type = llc->llc_snap.ether_type;
1003109323Ssam		frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
1004178354Ssam		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1005178354Ssam			k = ieee80211_crypto_encap(ni, m0);
1006138571Ssam			if (k == NULL) {
1007170530Ssam				ieee80211_free_node(ni);
1008143299Ssam				m_freem(m0);
1009109323Ssam				continue;
1010109323Ssam			}
1011138952Ssam			frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
1012109323Ssam		}
1013178354Ssam
1014192468Ssam		if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
1015178354Ssam			sc->sc_tx_th.wt_rate = ni->ni_txrate;
1016192468Ssam			ieee80211_radiotap_tx(ni->ni_vap, m0);
1017109323Ssam		}
1018178354Ssam
1019127697Ssam		m_copydata(m0, 0, sizeof(struct ieee80211_frame),
1020127697Ssam		    (caddr_t)&frmhdr.wi_whdr);
1021127697Ssam		m_adj(m0, sizeof(struct ieee80211_frame));
1022127697Ssam		frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
1023170530Ssam		ieee80211_free_node(ni);
1024160991Ssam		if (wi_start_tx(ifp, &frmhdr, m0))
1025109323Ssam			continue;
1026178354Ssam
1027160991Ssam		sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
1028178354Ssam		ifp->if_opackets++;
1029160991Ssam	}
1030165087Ssam}
1031160991Ssam
1032165087Ssamstatic void
1033165087Ssamwi_start(struct ifnet *ifp)
1034165087Ssam{
1035165087Ssam	struct wi_softc	*sc = ifp->if_softc;
1036165087Ssam
1037165087Ssam	WI_LOCK(sc);
1038165087Ssam	wi_start_locked(ifp);
1039160991Ssam	WI_UNLOCK(sc);
1040160991Ssam}
1041160991Ssam
1042160991Ssamstatic int
1043160991Ssamwi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr, struct mbuf *m0)
1044160991Ssam{
1045160991Ssam	struct wi_softc	*sc = ifp->if_softc;
1046160991Ssam	int cur = sc->sc_txnext;
1047160991Ssam	int fid, off, error;
1048160991Ssam
1049160991Ssam	fid = sc->sc_txd[cur].d_fid;
1050160991Ssam	off = sizeof(*frmhdr);
1051160991Ssam	error = wi_write_bap(sc, fid, 0, frmhdr, sizeof(*frmhdr)) != 0
1052160991Ssam	     || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
1053160991Ssam	m_freem(m0);
1054160991Ssam	if (error) {
1055160991Ssam		ifp->if_oerrors++;
1056160991Ssam		return -1;
1057160991Ssam	}
1058160991Ssam	sc->sc_txd[cur].d_len = off;
1059160991Ssam	if (sc->sc_txcur == cur) {
1060160991Ssam		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
1061160991Ssam			if_printf(ifp, "xmit failed\n");
1062160991Ssam			sc->sc_txd[cur].d_len = 0;
1063160991Ssam			return -1;
1064109323Ssam		}
1065160991Ssam		sc->sc_tx_timer = 5;
1066160991Ssam	}
1067160991Ssam	return 0;
1068160991Ssam}
1069160991Ssam
1070160991Ssamstatic int
1071160991Ssamwi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m0,
1072160991Ssam	    const struct ieee80211_bpf_params *params)
1073160991Ssam{
1074160991Ssam	struct ieee80211com *ic = ni->ni_ic;
1075160991Ssam	struct ifnet *ifp = ic->ic_ifp;
1076192468Ssam	struct ieee80211vap *vap = ni->ni_vap;
1077160991Ssam	struct wi_softc	*sc = ifp->if_softc;
1078178354Ssam	struct ieee80211_key *k;
1079160991Ssam	struct ieee80211_frame *wh;
1080160991Ssam	struct wi_frame frmhdr;
1081160991Ssam	int cur;
1082160991Ssam	int rc = 0;
1083160991Ssam
1084160991Ssam	WI_LOCK(sc);
1085160991Ssam
1086160991Ssam	if (sc->wi_gone) {
1087160991Ssam		rc = ENETDOWN;
1088160991Ssam		goto out;
1089160991Ssam	}
1090160991Ssam	memset(&frmhdr, 0, sizeof(frmhdr));
1091160991Ssam	cur = sc->sc_txnext;
1092160991Ssam	if (sc->sc_txd[cur].d_len != 0) {
1093160991Ssam		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1094160991Ssam		rc = ENOBUFS;
1095160991Ssam		goto out;
1096160991Ssam	}
1097160991Ssam	m0->m_pkthdr.rcvif = NULL;
1098160991Ssam
1099160991Ssam	m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
1100160991Ssam	    (caddr_t)&frmhdr.wi_ehdr);
1101160991Ssam	frmhdr.wi_ehdr.ether_type = 0;
1102160991Ssam	wh = mtod(m0, struct ieee80211_frame *);
1103160991Ssam
1104160991Ssam	frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
1105160991Ssam	if (params && (params->ibp_flags & IEEE80211_BPF_NOACK))
1106160991Ssam		frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_ALTRTRY);
1107160991Ssam	if ((wh->i_fc[1] & IEEE80211_FC1_WEP) &&
1108178354Ssam	    (!params || (params && (params->ibp_flags & IEEE80211_BPF_CRYPTO)))) {
1109178354Ssam		k = ieee80211_crypto_encap(ni, m0);
1110178354Ssam		if (k == NULL) {
1111178354Ssam			rc = ENOMEM;
1112178354Ssam			goto out;
1113109323Ssam		}
1114178354Ssam		frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
111574838Salfred	}
1116192468Ssam	if (ieee80211_radiotap_active_vap(vap)) {
1117178354Ssam		sc->sc_tx_th.wt_rate = ni->ni_txrate;
1118192468Ssam		ieee80211_radiotap_tx(vap, m0);
1119160991Ssam	}
1120160991Ssam	m_copydata(m0, 0, sizeof(struct ieee80211_frame),
1121160991Ssam	    (caddr_t)&frmhdr.wi_whdr);
1122160991Ssam	m_adj(m0, sizeof(struct ieee80211_frame));
1123160991Ssam	frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
1124168860Ssephe	if (wi_start_tx(ifp, &frmhdr, m0) < 0) {
1125168860Ssephe		m0 = NULL;
1126168860Ssephe		rc = EIO;
1127160991Ssam		goto out;
1128168860Ssephe	}
1129168860Ssephe	m0 = NULL;
113046492Swpaul
1131160991Ssam	sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
1132160991Ssamout:
1133109323Ssam	WI_UNLOCK(sc);
1134160991Ssam
1135168860Ssephe	if (m0 != NULL)
1136168860Ssephe		m_freem(m0);
1137168860Ssephe	ieee80211_free_node(ni);
1138160991Ssam	return rc;
113946492Swpaul}
114046492Swpaul
114188546Salfredstatic int
1142178354Ssamwi_reset(struct wi_softc *sc)
114346492Swpaul{
1144114124Simp#define WI_INIT_TRIES 3
1145178354Ssam	int i, error = 0;
1146112362Simp
1147178354Ssam	for (i = 0; i < WI_INIT_TRIES; i++) {
1148178354Ssam		error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0);
1149178354Ssam		if (error == 0)
115046492Swpaul			break;
1151109323Ssam		DELAY(WI_DELAY * 1000);
115246492Swpaul	}
1153114124Simp	sc->sc_reset = 1;
1154178354Ssam	if (i == WI_INIT_TRIES) {
1155178354Ssam		if_printf(sc->sc_ifp, "reset failed\n");
1156178354Ssam		return error;
115775373Salfred	}
115846492Swpaul
1159109323Ssam	CSR_WRITE_2(sc, WI_INT_EN, 0);
1160114124Simp	CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
116146492Swpaul
1162109323Ssam	/* Calibrate timer. */
1163114124Simp	wi_write_val(sc, WI_RID_TICK_TIME, 8);
1164114124Simp
1165178354Ssam	return 0;
1166109323Ssam#undef WI_INIT_TRIES
116746492Swpaul}
116846492Swpaul
116988546Salfredstatic void
1170165089Ssamwi_watchdog(void *arg)
117146492Swpaul{
1172165089Ssam	struct wi_softc	*sc = arg;
1173165089Ssam	struct ifnet *ifp = sc->sc_ifp;
117446492Swpaul
1175178354Ssam	WI_LOCK_ASSERT(sc);
1176178354Ssam
1177109323Ssam	if (!sc->sc_enabled)
1178109323Ssam		return;
117946492Swpaul
1180178354Ssam	if (sc->sc_tx_timer && --sc->sc_tx_timer == 0) {
1181178354Ssam		if_printf(ifp, "device timeout\n");
1182178354Ssam		ifp->if_oerrors++;
1183178354Ssam		wi_init_locked(ifp->if_softc);
1184178354Ssam		return;
118546492Swpaul	}
1186165089Ssam	callout_reset(&sc->sc_watchdog, hz, wi_watchdog, sc);
118746492Swpaul}
118846492Swpaul
118988546Salfredstatic int
1190109323Ssamwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
119146492Swpaul{
1192109323Ssam	struct wi_softc *sc = ifp->if_softc;
1193178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1194178354Ssam	struct ifreq *ifr = (struct ifreq *) data;
1195178354Ssam	int error = 0, startall = 0;
119646492Swpaul
1197109323Ssam	switch (cmd) {
119846492Swpaul	case SIOCSIFFLAGS:
1199178704Sthompsa		WI_LOCK(sc);
1200100876Simp		/*
1201101139Simp		 * Can't do promisc and hostap at the same time.  If all that's
1202101139Simp		 * changing is the promisc flag, try to short-circuit a call to
1203101139Simp		 * wi_init() by just setting PROMISC in the hardware.
1204100876Simp		 */
120546492Swpaul		if (ifp->if_flags & IFF_UP) {
1206109323Ssam			if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1207148887Srwatson			    ifp->if_drv_flags & IFF_DRV_RUNNING) {
1208178354Ssam				if ((ifp->if_flags ^ sc->sc_if_flags) & IFF_PROMISC) {
1209178354Ssam					wi_write_val(sc, WI_RID_PROMISC,
1210178354Ssam					    (ifp->if_flags & IFF_PROMISC) != 0);
1211101139Simp				} else {
1212178354Ssam					wi_init_locked(sc);
1213178354Ssam					startall = 1;
1214101139Simp				}
1215100876Simp			} else {
1216178354Ssam				wi_init_locked(sc);
1217178354Ssam				startall = 1;
1218100876Simp			}
121946492Swpaul		} else {
1220178354Ssam			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1221178354Ssam				wi_stop_locked(sc, 1);
1222123098Simp			sc->wi_gone = 0;
122346492Swpaul		}
1224109323Ssam		sc->sc_if_flags = ifp->if_flags;
1225178704Sthompsa		WI_UNLOCK(sc);
1226178704Sthompsa		if (startall)
1227178704Sthompsa			ieee80211_start_all(ic);
122846492Swpaul		break;
1229178354Ssam	case SIOCGIFMEDIA:
1230178354Ssam		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
123146492Swpaul		break;
1232178704Sthompsa	case SIOCGIFADDR:
1233178354Ssam		error = ether_ioctl(ifp, cmd, data);
1234170530Ssam		break;
1235178704Sthompsa	default:
1236178704Sthompsa		error = EINVAL;
1237178704Sthompsa		break;
1238170530Ssam	}
1239109323Ssam	return error;
1240109323Ssam}
124146492Swpaul
1242109323Ssamstatic void
1243109323Ssamwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1244109323Ssam{
1245178354Ssam	struct ieee80211vap *vap = ifp->if_softc;
1246178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1247178354Ssam	struct wi_softc *sc = ic->ic_ifp->if_softc;
1248109323Ssam	u_int16_t val;
1249109323Ssam	int rate, len;
125046492Swpaul
1251109323Ssam	len = sizeof(val);
1252178354Ssam	if (sc->sc_enabled &&
1253178354Ssam	    wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) == 0 &&
1254138571Ssam	    len == sizeof(val)) {
1255109323Ssam		/* convert to 802.11 rate */
1256138571Ssam		val = le16toh(val);
1257109323Ssam		rate = val * 2;
1258109323Ssam		if (sc->sc_firmware_type == WI_LUCENT) {
1259138571Ssam			if (rate == 10)
1260109323Ssam				rate = 11;	/* 5.5Mbps */
1261109323Ssam		} else {
1262109323Ssam			if (rate == 4*2)
1263109323Ssam				rate = 11;	/* 5.5Mbps */
1264109323Ssam			else if (rate == 8*2)
1265109323Ssam				rate = 22;	/* 11Mbps */
1266109323Ssam		}
1267178354Ssam		vap->iv_bss->ni_txrate = rate;
1268109323Ssam	}
1269178354Ssam	ieee80211_media_status(ifp, imr);
1270109323Ssam}
127146492Swpaul
1272109323Ssamstatic void
1273109323Ssamwi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1274109323Ssam{
1275147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1276178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1277178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1278178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
127998440Simp
1280109323Ssam	if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1281109323Ssam		return;
128246492Swpaul
1283109323Ssam	DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid)));
1284109323Ssam	DPRINTF(("%s ?\n", ether_sprintf(new_bssid)));
128546492Swpaul
1286109323Ssam	/* In promiscuous mode, the BSSID field is not a reliable
1287109323Ssam	 * indicator of the firmware's BSSID. Damp spurious
1288109323Ssam	 * change-of-BSSID indications.
1289109323Ssam	 */
1290109323Ssam	if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1291138571Ssam	    !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns,
1292138571Ssam	                 WI_MAX_FALSE_SYNS))
1293109323Ssam		return;
129446492Swpaul
1295139542Ssam	sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1);
1296170530Ssam#if 0
1297139542Ssam	/*
1298139542Ssam	 * XXX hack; we should create a new node with the new bssid
1299139542Ssam	 * and replace the existing ic_bss with it but since we don't
1300139542Ssam	 * process management frames to collect state we cheat by
1301139542Ssam	 * reusing the existing node as we know wi_newstate will be
1302139542Ssam	 * called and it will overwrite the node state.
1303139542Ssam	 */
1304139542Ssam	ieee80211_sta_join(ic, ieee80211_ref_node(ni));
1305170530Ssam#endif
1306109323Ssam}
130746492Swpaul
1308178354Ssamstatic __noinline void
1309109323Ssamwi_rx_intr(struct wi_softc *sc)
1310109323Ssam{
1311147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1312178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1313109323Ssam	struct wi_frame frmhdr;
1314109323Ssam	struct mbuf *m;
1315109323Ssam	struct ieee80211_frame *wh;
1316119150Ssam	struct ieee80211_node *ni;
1317192468Ssam	int fid, len, off;
1318109323Ssam	u_int8_t dir;
1319109323Ssam	u_int16_t status;
1320192468Ssam	int8_t rssi, nf;
132146611Swpaul
1322109323Ssam	fid = CSR_READ_2(sc, WI_RX_FID);
132346611Swpaul
1324109323Ssam	/* First read in the frame header */
1325109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1326109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1327109323Ssam		ifp->if_ierrors++;
1328109323Ssam		DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1329109323Ssam		return;
1330109323Ssam	}
133191695Simp
1332109323Ssam	/*
1333109323Ssam	 * Drop undecryptable or packets with receive errors here
1334109323Ssam	 */
1335109323Ssam	status = le16toh(frmhdr.wi_status);
1336109323Ssam	if (status & WI_STAT_ERRSTAT) {
1337109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1338109323Ssam		ifp->if_ierrors++;
1339109323Ssam		DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1340109323Ssam		return;
1341109323Ssam	}
134246492Swpaul
1343109323Ssam	len = le16toh(frmhdr.wi_dat_len);
1344109323Ssam	off = ALIGN(sizeof(struct ieee80211_frame));
134546563Swpaul
1346117855Ssam	/*
1347117855Ssam	 * Sometimes the PRISM2.x returns bogusly large frames. Except
1348117855Ssam	 * in monitor mode, just throw them away.
1349117855Ssam	 */
1350117855Ssam	if (off + len > MCLBYTES) {
1351117855Ssam		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1352117855Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1353117855Ssam			ifp->if_ierrors++;
1354117855Ssam			DPRINTF(("wi_rx_intr: oversized packet\n"));
1355117855Ssam			return;
1356117855Ssam		} else
1357117855Ssam			len = 0;
1358117855Ssam	}
1359117855Ssam
1360178354Ssam	if (off + len > MHLEN)
1361178354Ssam		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1362178354Ssam	else
1363178354Ssam		m = m_gethdr(M_DONTWAIT, MT_DATA);
1364109323Ssam	if (m == NULL) {
1365109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1366109323Ssam		ifp->if_ierrors++;
1367109323Ssam		DPRINTF(("wi_rx_intr: MGET failed\n"));
1368109323Ssam		return;
1369109323Ssam	}
1370109323Ssam	m->m_data += off - sizeof(struct ieee80211_frame);
1371109323Ssam	memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1372109323Ssam	wi_read_bap(sc, fid, sizeof(frmhdr),
1373109323Ssam	    m->m_data + sizeof(struct ieee80211_frame), len);
1374109323Ssam	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1375109323Ssam	m->m_pkthdr.rcvif = ifp;
137694405Simp
1377109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
137846492Swpaul
1379192468Ssam	rssi = frmhdr.wi_rx_signal;
1380192468Ssam	nf = frmhdr.wi_rx_silence;
1381192468Ssam	if (ieee80211_radiotap_active(ic)) {
1382192468Ssam		struct wi_rx_radiotap_header *tap = &sc->sc_rx_th;
1383192468Ssam		uint32_t rstamp;
1384192468Ssam
1385192468Ssam		rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1386192468Ssam		    le16toh(frmhdr.wi_rx_tstamp1);
1387192517Ssam		tap->wr_tsf = htole64((uint64_t)rstamp);
1388123927Ssam		/* XXX replace divide by table */
1389192468Ssam		tap->wr_rate = frmhdr.wi_rx_rate / 5;
1390192468Ssam		tap->wr_flags = 0;
1391123927Ssam		if (frmhdr.wi_status & WI_STAT_PCF)
1392192468Ssam			tap->wr_flags |= IEEE80211_RADIOTAP_F_CFP;
1393178354Ssam		if (m->m_flags & M_WEP)
1394192468Ssam			tap->wr_flags |= IEEE80211_RADIOTAP_F_WEP;
1395192468Ssam		tap->wr_antsignal = rssi;
1396192468Ssam		tap->wr_antnoise = nf;
139756965Swpaul	}
139856965Swpaul
1399109323Ssam	/* synchronize driver's BSSID with firmware's BSSID */
1400178354Ssam	wh = mtod(m, struct ieee80211_frame *);
1401109323Ssam	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1402109323Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1403109323Ssam		wi_sync_bssid(sc, wh->i_addr3);
140446492Swpaul
1405165088Ssam	WI_UNLOCK(sc);
1406165088Ssam
1407178354Ssam	ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1408178354Ssam	if (ni != NULL) {
1409192468Ssam		(void) ieee80211_input(ni, m, rssi, nf);
1410178354Ssam		ieee80211_free_node(ni);
1411178354Ssam	} else
1412192468Ssam		(void) ieee80211_input_all(ic, m, rssi, nf);
1413178354Ssam
1414165088Ssam	WI_LOCK(sc);
1415109323Ssam}
141646492Swpaul
1417178354Ssamstatic __noinline void
1418109323Ssamwi_tx_ex_intr(struct wi_softc *sc)
1419109323Ssam{
1420147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1421109323Ssam	struct wi_frame frmhdr;
1422109323Ssam	int fid;
142346492Swpaul
1424109323Ssam	fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1425109323Ssam	/* Read in the frame header */
1426109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1427109323Ssam		u_int16_t status = le16toh(frmhdr.wi_status);
1428109323Ssam		/*
1429109323Ssam		 * Spontaneous station disconnects appear as xmit
1430109323Ssam		 * errors.  Don't announce them and/or count them
1431109323Ssam		 * as an output error.
1432109323Ssam		 */
1433109323Ssam		if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1434109323Ssam			if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1435109323Ssam				if_printf(ifp, "tx failed");
1436109323Ssam				if (status & WI_TXSTAT_RET_ERR)
1437109323Ssam					printf(", retry limit exceeded");
1438109323Ssam				if (status & WI_TXSTAT_AGED_ERR)
1439109323Ssam					printf(", max transmit lifetime exceeded");
1440109323Ssam				if (status & WI_TXSTAT_DISCONNECT)
1441109323Ssam					printf(", port disconnected");
1442109323Ssam				if (status & WI_TXSTAT_FORM_ERR)
1443109323Ssam					printf(", invalid format (data len %u src %6D)",
1444109323Ssam						le16toh(frmhdr.wi_dat_len),
1445109323Ssam						frmhdr.wi_ehdr.ether_shost, ":");
1446109323Ssam				if (status & ~0xf)
1447109323Ssam					printf(", status=0x%x", status);
1448109323Ssam				printf("\n");
1449109323Ssam			}
1450109323Ssam			ifp->if_oerrors++;
1451109323Ssam		} else {
1452109323Ssam			DPRINTF(("port disconnected\n"));
1453109323Ssam			ifp->if_collisions++;	/* XXX */
1454109323Ssam		}
1455109323Ssam	} else
1456109323Ssam		DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
1457109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1458109323Ssam}
145946492Swpaul
1460178354Ssamstatic __noinline void
1461109323Ssamwi_tx_intr(struct wi_softc *sc)
1462109323Ssam{
1463147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1464109323Ssam	int fid, cur;
146594405Simp
1466123098Simp	if (sc->wi_gone)
1467123098Simp		return;
1468123098Simp
1469109323Ssam	fid = CSR_READ_2(sc, WI_ALLOC_FID);
1470109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
147146492Swpaul
1472109323Ssam	cur = sc->sc_txcur;
1473109323Ssam	if (sc->sc_txd[cur].d_fid != fid) {
1474109323Ssam		if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
1475109323Ssam		    fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
1476109323Ssam		return;
1477109323Ssam	}
1478109323Ssam	sc->sc_tx_timer = 0;
1479109323Ssam	sc->sc_txd[cur].d_len = 0;
1480112363Simp	sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
1481109323Ssam	if (sc->sc_txd[cur].d_len == 0)
1482148887Srwatson		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1483109323Ssam	else {
1484109323Ssam		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1485109323Ssam		    0, 0)) {
1486109323Ssam			if_printf(ifp, "xmit failed\n");
1487109323Ssam			sc->sc_txd[cur].d_len = 0;
1488109323Ssam		} else {
1489109323Ssam			sc->sc_tx_timer = 5;
1490109323Ssam		}
1491109323Ssam	}
149246492Swpaul}
149346492Swpaul
1494178354Ssamstatic __noinline void
1495109323Ssamwi_info_intr(struct wi_softc *sc)
149694405Simp{
1497147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1498178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1499178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1500109323Ssam	int i, fid, len, off;
1501109323Ssam	u_int16_t ltbuf[2];
1502109323Ssam	u_int16_t stat;
1503109323Ssam	u_int32_t *ptr;
150494405Simp
1505109323Ssam	fid = CSR_READ_2(sc, WI_INFO_FID);
1506109323Ssam	wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
150794405Simp
1508109323Ssam	switch (le16toh(ltbuf[1])) {
1509109323Ssam	case WI_INFO_LINK_STAT:
1510109323Ssam		wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1511109323Ssam		DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1512109323Ssam		switch (le16toh(stat)) {
1513109323Ssam		case WI_INFO_LINK_STAT_CONNECTED:
1514178354Ssam			if (vap->iv_state == IEEE80211_S_RUN &&
1515178354Ssam			    vap->iv_opmode != IEEE80211_M_IBSS)
1516109323Ssam				break;
1517178354Ssam			/* fall thru... */
1518109323Ssam		case WI_INFO_LINK_STAT_AP_CHG:
1519191746Sthompsa			IEEE80211_LOCK(ic);
1520191746Sthompsa			vap->iv_bss->ni_associd = 1 | 0xc000;	/* NB: anything will do */
1521191746Sthompsa			ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
1522191746Sthompsa			IEEE80211_UNLOCK(ic);
1523109323Ssam			break;
1524109323Ssam		case WI_INFO_LINK_STAT_AP_INR:
1525109323Ssam			break;
1526178354Ssam		case WI_INFO_LINK_STAT_DISCONNECTED:
1527178354Ssam			/* we dropped off the net; e.g. due to deauth/disassoc */
1528191746Sthompsa			IEEE80211_LOCK(ic);
1529191746Sthompsa			vap->iv_bss->ni_associd = 0;
1530191746Sthompsa			vap->iv_stats.is_rx_deauth++;
1531191746Sthompsa			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
1532191746Sthompsa			IEEE80211_UNLOCK(ic);
1533178354Ssam			break;
1534109323Ssam		case WI_INFO_LINK_STAT_AP_OOR:
1535178354Ssam			/* XXX does this need to be per-vap? */
1536191746Sthompsa			ieee80211_beacon_miss(ic);
1537109323Ssam			break;
1538109323Ssam		case WI_INFO_LINK_STAT_ASSOC_FAILED:
1539178354Ssam			if (vap->iv_opmode == IEEE80211_M_STA)
1540191746Sthompsa				ieee80211_new_state(vap, IEEE80211_S_SCAN,
1541191746Sthompsa				    IEEE80211_SCAN_FAIL_TIMEOUT);
1542109323Ssam			break;
1543109323Ssam		}
1544109323Ssam		break;
1545109323Ssam	case WI_INFO_COUNTERS:
1546109323Ssam		/* some card versions have a larger stats structure */
1547109323Ssam		len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1548109323Ssam		ptr = (u_int32_t *)&sc->sc_stats;
1549109323Ssam		off = sizeof(ltbuf);
1550109323Ssam		for (i = 0; i < len; i++, off += 2, ptr++) {
1551109323Ssam			wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1552109323Ssam#ifdef WI_HERMES_STATS_WAR
1553109323Ssam			if (stat & 0xf000)
1554109323Ssam				stat = ~stat;
1555109323Ssam#endif
1556109323Ssam			*ptr += stat;
1557109323Ssam		}
1558109323Ssam		ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1559109323Ssam		    sc->sc_stats.wi_tx_multi_retries +
1560109323Ssam		    sc->sc_stats.wi_tx_retry_limit;
1561109323Ssam		break;
1562109323Ssam	default:
1563109323Ssam		DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1564109323Ssam		    le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1565109323Ssam		break;
156694405Simp	}
1567109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1568109323Ssam}
156994405Simp
1570109323Ssamstatic int
1571109323Ssamwi_write_multi(struct wi_softc *sc)
1572109323Ssam{
1573147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1574109323Ssam	int n;
1575109323Ssam	struct ifmultiaddr *ifma;
1576109323Ssam	struct wi_mcast mlist;
157794472Simp
1578109323Ssam	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1579109323Ssamallmulti:
1580109323Ssam		memset(&mlist, 0, sizeof(mlist));
1581109323Ssam		return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1582109323Ssam		    sizeof(mlist));
158394405Simp	}
158494405Simp
1585109323Ssam	n = 0;
1586195049Srwatson	if_maddr_rlock(ifp);
1587109323Ssam	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1588109323Ssam		if (ifma->ifma_addr->sa_family != AF_LINK)
1589109323Ssam			continue;
1590109323Ssam		if (n >= 16)
1591109323Ssam			goto allmulti;
1592109323Ssam		IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1593109323Ssam		    (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1594109323Ssam		n++;
159594405Simp	}
1596195049Srwatson	if_maddr_runlock(ifp);
1597109323Ssam	return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1598109323Ssam	    IEEE80211_ADDR_LEN * n);
159994405Simp}
160094405Simp
160194405Simpstatic void
1602178354Ssamwi_update_mcast(struct ifnet *ifp)
1603178354Ssam{
1604178354Ssam	wi_write_multi(ifp->if_softc);
1605178354Ssam}
1606178354Ssam
1607178354Ssamstatic void
1608192468Ssamwi_update_promisc(struct ifnet *ifp)
1609192468Ssam{
1610192468Ssam	struct wi_softc *sc = ifp->if_softc;
1611192468Ssam	struct ieee80211com *ic = ifp->if_l2com;
1612192468Ssam
1613192468Ssam	WI_LOCK(sc);
1614192468Ssam	/* XXX handle WEP special case handling? */
1615192468Ssam	wi_write_val(sc, WI_RID_PROMISC,
1616192468Ssam	    (ic->ic_opmode == IEEE80211_M_MONITOR ||
1617192468Ssam	     (ifp->if_flags & IFF_PROMISC)));
1618192468Ssam	WI_UNLOCK(sc);
1619192468Ssam}
1620192468Ssam
1621192468Ssamstatic void
1622109323Ssamwi_read_nicid(struct wi_softc *sc)
162346492Swpaul{
1624109323Ssam	struct wi_card_ident *id;
1625109323Ssam	char *p;
1626109323Ssam	int len;
1627109323Ssam	u_int16_t ver[4];
162846492Swpaul
1629109323Ssam	/* getting chip identity */
1630109323Ssam	memset(ver, 0, sizeof(ver));
1631109323Ssam	len = sizeof(ver);
1632109323Ssam	wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
163346492Swpaul
1634109323Ssam	sc->sc_firmware_type = WI_NOTYPE;
1635180919Simp	sc->sc_nic_id = le16toh(ver[0]);
1636109323Ssam	for (id = wi_card_ident; id->card_name != NULL; id++) {
1637180919Simp		if (sc->sc_nic_id == id->card_id) {
1638180919Simp			sc->sc_nic_name = id->card_name;
1639109323Ssam			sc->sc_firmware_type = id->firm_type;
1640109323Ssam			break;
1641109323Ssam		}
164267092Swpaul	}
1643109323Ssam	if (sc->sc_firmware_type == WI_NOTYPE) {
1644180919Simp		if (sc->sc_nic_id & 0x8000) {
1645109323Ssam			sc->sc_firmware_type = WI_INTERSIL;
1646180919Simp			sc->sc_nic_name = "Unknown Prism chip";
1647109323Ssam		} else {
1648109323Ssam			sc->sc_firmware_type = WI_LUCENT;
1649180919Simp			sc->sc_nic_name = "Unknown Lucent chip";
1650109323Ssam		}
165167092Swpaul	}
1652181210Simp	if (bootverbose)
1653181210Simp		device_printf(sc->sc_dev, "using %s\n", sc->sc_nic_name);
165446492Swpaul
1655109323Ssam	/* get primary firmware version (Only Prism chips) */
1656109323Ssam	if (sc->sc_firmware_type != WI_LUCENT) {
1657109323Ssam		memset(ver, 0, sizeof(ver));
1658109323Ssam		len = sizeof(ver);
1659109323Ssam		wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1660109323Ssam		sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1661109323Ssam		    le16toh(ver[3]) * 100 + le16toh(ver[1]);
166267092Swpaul	}
166346492Swpaul
1664109323Ssam	/* get station firmware version */
1665109323Ssam	memset(ver, 0, sizeof(ver));
1666109323Ssam	len = sizeof(ver);
1667109323Ssam	wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1668109323Ssam	sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1669109323Ssam	    le16toh(ver[3]) * 100 + le16toh(ver[1]);
1670109323Ssam	if (sc->sc_firmware_type == WI_INTERSIL &&
1671109323Ssam	    (sc->sc_sta_firmware_ver == 10102 ||
1672109323Ssam	     sc->sc_sta_firmware_ver == 20102)) {
1673109323Ssam		char ident[12];
1674109323Ssam		memset(ident, 0, sizeof(ident));
1675109323Ssam		len = sizeof(ident);
1676109323Ssam		/* value should be the format like "V2.00-11" */
1677109323Ssam		if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1678109323Ssam		    *(p = (char *)ident) >= 'A' &&
1679109323Ssam		    p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1680109323Ssam			sc->sc_firmware_type = WI_SYMBOL;
1681109323Ssam			sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1682109323Ssam			    (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1683109323Ssam			    (p[6] - '0') * 10 + (p[7] - '0');
168494405Simp		}
168594405Simp	}
1686180919Simp	if (bootverbose) {
1687180919Simp		device_printf(sc->sc_dev, "%s Firmware: ",
1688180919Simp		    wi_firmware_names[sc->sc_firmware_type]);
1689180919Simp		if (sc->sc_firmware_type != WI_LUCENT)	/* XXX */
1690180919Simp			printf("Primary (%u.%u.%u), ",
1691180919Simp			    sc->sc_pri_firmware_ver / 10000,
1692180919Simp			    (sc->sc_pri_firmware_ver % 10000) / 100,
1693180919Simp			    sc->sc_pri_firmware_ver % 100);
1694180919Simp		printf("Station (%u.%u.%u)\n",
1695180919Simp		    sc->sc_sta_firmware_ver / 10000,
1696180919Simp		    (sc->sc_sta_firmware_ver % 10000) / 100,
1697180919Simp		    sc->sc_sta_firmware_ver % 100);
1698180919Simp	}
1699109323Ssam}
170046492Swpaul
1701109323Ssamstatic int
1702109323Ssamwi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1703109323Ssam{
1704109323Ssam	struct wi_ssid ssid;
170546492Swpaul
1706109323Ssam	if (buflen > IEEE80211_NWID_LEN)
1707109323Ssam		return ENOBUFS;
1708109323Ssam	memset(&ssid, 0, sizeof(ssid));
1709109323Ssam	ssid.wi_len = htole16(buflen);
1710109323Ssam	memcpy(ssid.wi_ssid, buf, buflen);
1711109323Ssam	return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1712109323Ssam}
171346492Swpaul
1714109323Ssamstatic int
1715178354Ssamwi_write_txrate(struct wi_softc *sc, struct ieee80211vap *vap)
1716109323Ssam{
1717178354Ssam	static const uint16_t lucent_rates[12] = {
1718178354Ssam	    [ 0] = 3,	/* auto */
1719178354Ssam	    [ 1] = 1,	/* 1Mb/s */
1720178354Ssam	    [ 2] = 2,	/* 2Mb/s */
1721178354Ssam	    [ 5] = 4,	/* 5.5Mb/s */
1722178354Ssam	    [11] = 5	/* 11Mb/s */
1723178354Ssam	};
1724178354Ssam	static const uint16_t intersil_rates[12] = {
1725178354Ssam	    [ 0] = 0xf,	/* auto */
1726178354Ssam	    [ 1] = 0,	/* 1Mb/s */
1727178354Ssam	    [ 2] = 1,	/* 2Mb/s */
1728178354Ssam	    [ 5] = 2,	/* 5.5Mb/s */
1729178354Ssam	    [11] = 3,	/* 11Mb/s */
1730178354Ssam	};
1731178354Ssam	const uint16_t *rates = sc->sc_firmware_type == WI_LUCENT ?
1732178354Ssam	    lucent_rates : intersil_rates;
1733178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1734178354Ssam	const struct ieee80211_txparam *tp;
173546492Swpaul
1736178354Ssam	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
1737178354Ssam	return wi_write_val(sc, WI_RID_TX_RATE,
1738178354Ssam	    (tp->ucastrate == IEEE80211_FIXED_RATE_NONE ?
1739178354Ssam		rates[0] : rates[tp->ucastrate / 2]));
174046492Swpaul}
174146492Swpaul
1742109323Ssamstatic int
1743178354Ssamwi_write_wep(struct wi_softc *sc, struct ieee80211vap *vap)
174446492Swpaul{
1745109323Ssam	int error = 0;
1746109323Ssam	int i, keylen;
1747109323Ssam	u_int16_t val;
1748109323Ssam	struct wi_key wkey[IEEE80211_WEP_NKID];
174946492Swpaul
1750109323Ssam	switch (sc->sc_firmware_type) {
1751109323Ssam	case WI_LUCENT:
1752178354Ssam		val = (vap->iv_flags & IEEE80211_F_PRIVACY) ? 1 : 0;
1753109323Ssam		error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
1754109323Ssam		if (error)
1755109323Ssam			break;
1756178354Ssam		if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0)
1757138988Smdodd			break;
1758178354Ssam		error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, vap->iv_def_txkey);
1759138988Smdodd		if (error)
1760138988Smdodd			break;
1761138988Smdodd		memset(wkey, 0, sizeof(wkey));
1762138988Smdodd		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1763178354Ssam			keylen = vap->iv_nw_keys[i].wk_keylen;
1764138988Smdodd			wkey[i].wi_keylen = htole16(keylen);
1765178354Ssam			memcpy(wkey[i].wi_keydat, vap->iv_nw_keys[i].wk_key,
1766138988Smdodd			    keylen);
1767109323Ssam		}
1768138988Smdodd		error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
1769138988Smdodd		    wkey, sizeof(wkey));
1770150798Savatar		sc->sc_encryption = 0;
1771109323Ssam		break;
1772109323Ssam
1773109323Ssam	case WI_INTERSIL:
1774178354Ssam		val = HOST_ENCRYPT | HOST_DECRYPT;
1775178354Ssam		if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1776109323Ssam			/*
1777109323Ssam			 * ONLY HWB3163 EVAL-CARD Firmware version
1778109323Ssam			 * less than 0.8 variant2
1779109323Ssam			 *
1780109323Ssam			 *   If promiscuous mode disable, Prism2 chip
1781109323Ssam			 *  does not work with WEP .
1782109323Ssam			 * It is under investigation for details.
1783109323Ssam			 * (ichiro@netbsd.org)
1784109323Ssam			 */
1785178354Ssam			if (sc->sc_sta_firmware_ver < 802 ) {
1786109323Ssam				/* firm ver < 0.8 variant 2 */
1787109323Ssam				wi_write_val(sc, WI_RID_PROMISC, 1);
1788109323Ssam			}
1789109323Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE,
1790178354Ssam			    vap->iv_bss->ni_authmode);
1791178354Ssam			val |= PRIVACY_INVOKED;
1792109323Ssam		} else {
1793178354Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE, IEEE80211_AUTH_OPEN);
1794109323Ssam		}
1795109323Ssam		error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
1796109323Ssam		if (error)
1797109323Ssam			break;
1798150798Savatar		sc->sc_encryption = val;
1799138988Smdodd		if ((val & PRIVACY_INVOKED) == 0)
1800138988Smdodd			break;
1801178354Ssam		error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY, vap->iv_def_txkey);
1802109323Ssam		break;
1803109323Ssam	}
1804109323Ssam	return error;
180546492Swpaul}
180646492Swpaul
1807192492Simpstatic int
1808109323Ssamwi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
180946492Swpaul{
1810178354Ssam	int i, s = 0;
1811178354Ssam
1812123098Simp	if (sc->wi_gone)
1813123098Simp		return (ENODEV);
1814123098Simp
1815109323Ssam	/* wait for the busy bit to clear */
1816123339Simp	for (i = sc->wi_cmd_count; i > 0; i--) {	/* 500ms */
1817116206Simp		if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
1818109323Ssam			break;
1819123098Simp		DELAY(1*1000);	/* 1ms */
1820109323Ssam	}
1821109323Ssam	if (i == 0) {
1822178354Ssam		device_printf(sc->sc_dev, "%s: busy bit won't clear, cmd 0x%x\n",
1823178354Ssam		   __func__, cmd);
1824123098Simp		sc->wi_gone = 1;
1825109323Ssam		return(ETIMEDOUT);
1826109323Ssam	}
182790580Sbrooks
1828109323Ssam	CSR_WRITE_2(sc, WI_PARAM0, val0);
1829109323Ssam	CSR_WRITE_2(sc, WI_PARAM1, val1);
1830109323Ssam	CSR_WRITE_2(sc, WI_PARAM2, val2);
1831109323Ssam	CSR_WRITE_2(sc, WI_COMMAND, cmd);
183290580Sbrooks
1833109323Ssam	if (cmd == WI_CMD_INI) {
1834109323Ssam		/* XXX: should sleep here. */
1835116206Simp		DELAY(100*1000);		/* 100ms delay for init */
1836109323Ssam	}
1837109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
1838109323Ssam		/*
1839109323Ssam		 * Wait for 'command complete' bit to be
1840109323Ssam		 * set in the event status register.
1841109323Ssam		 */
1842109323Ssam		s = CSR_READ_2(sc, WI_EVENT_STAT);
1843109323Ssam		if (s & WI_EV_CMD) {
1844109323Ssam			/* Ack the event and read result code. */
1845109323Ssam			s = CSR_READ_2(sc, WI_STATUS);
1846109323Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
1847109323Ssam			if (s & WI_STAT_CMD_RESULT) {
1848109323Ssam				return(EIO);
1849109323Ssam			}
1850109323Ssam			break;
185190580Sbrooks		}
1852109323Ssam		DELAY(WI_DELAY);
1853109323Ssam	}
185490580Sbrooks
1855109323Ssam	if (i == WI_TIMEOUT) {
1856178354Ssam		device_printf(sc->sc_dev, "%s: timeout on cmd 0x%04x; "
1857178354Ssam		    "event status 0x%04x\n", __func__, cmd, s);
1858123098Simp		if (s == 0xffff)
1859123098Simp			sc->wi_gone = 1;
1860109323Ssam		return(ETIMEDOUT);
186153702Swpaul	}
1862109323Ssam	return (0);
1863109323Ssam}
186453702Swpaul
1865109323Ssamstatic int
1866109323Ssamwi_seek_bap(struct wi_softc *sc, int id, int off)
1867109323Ssam{
1868109323Ssam	int i, status;
186990580Sbrooks
1870109323Ssam	CSR_WRITE_2(sc, WI_SEL0, id);
1871109323Ssam	CSR_WRITE_2(sc, WI_OFF0, off);
187290580Sbrooks
1873109323Ssam	for (i = 0; ; i++) {
1874109323Ssam		status = CSR_READ_2(sc, WI_OFF0);
1875109323Ssam		if ((status & WI_OFF_BUSY) == 0)
1876109323Ssam			break;
1877109323Ssam		if (i == WI_TIMEOUT) {
1878178354Ssam			device_printf(sc->sc_dev, "%s: timeout, id %x off %x\n",
1879178354Ssam			    __func__, id, off);
1880109323Ssam			sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
1881123098Simp			if (status == 0xffff)
1882123098Simp				sc->wi_gone = 1;
1883109323Ssam			return ETIMEDOUT;
1884109323Ssam		}
1885109323Ssam		DELAY(1);
188653702Swpaul	}
1887109323Ssam	if (status & WI_OFF_ERR) {
1888178354Ssam		device_printf(sc->sc_dev, "%s: error, id %x off %x\n",
1889178354Ssam		    __func__, id, off);
1890109323Ssam		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
1891109323Ssam		return EIO;
1892109323Ssam	}
1893109323Ssam	sc->sc_bap_id = id;
1894109323Ssam	sc->sc_bap_off = off;
1895109323Ssam	return 0;
189653702Swpaul}
189753702Swpaul
1898109323Ssamstatic int
1899109323Ssamwi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
190053702Swpaul{
1901109323Ssam	u_int16_t *ptr;
1902109323Ssam	int i, error, cnt;
190353702Swpaul
1904109323Ssam	if (buflen == 0)
1905109323Ssam		return 0;
1906109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1907109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
1908109323Ssam			return error;
190975219Salfred	}
1910109323Ssam	cnt = (buflen + 1) / 2;
1911109323Ssam	ptr = (u_int16_t *)buf;
1912109323Ssam	for (i = 0; i < cnt; i++)
1913109323Ssam		*ptr++ = CSR_READ_2(sc, WI_DATA0);
1914109323Ssam	sc->sc_bap_off += cnt * 2;
1915109323Ssam	return 0;
191653702Swpaul}
191753702Swpaul
1918109323Ssamstatic int
1919109323Ssamwi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
192053702Swpaul{
1921109323Ssam	u_int16_t *ptr;
1922109323Ssam	int i, error, cnt;
192346492Swpaul
1924109323Ssam	if (buflen == 0)
1925109323Ssam		return 0;
192646492Swpaul
1927109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1928109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
1929109323Ssam			return error;
1930109323Ssam	}
1931109323Ssam	cnt = (buflen + 1) / 2;
1932109323Ssam	ptr = (u_int16_t *)buf;
1933109323Ssam	for (i = 0; i < cnt; i++)
1934109323Ssam		CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
1935109323Ssam	sc->sc_bap_off += cnt * 2;
1936109323Ssam
1937109323Ssam	return 0;
193846492Swpaul}
193953702Swpaul
1940109323Ssamstatic int
1941109323Ssamwi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
1942109323Ssam{
1943109323Ssam	int error, len;
1944109323Ssam	struct mbuf *m;
194553702Swpaul
1946109323Ssam	for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
1947109323Ssam		if (m->m_len == 0)
1948109323Ssam			continue;
194953702Swpaul
1950109323Ssam		len = min(m->m_len, totlen);
195153702Swpaul
1952109323Ssam		if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
1953109323Ssam			m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
1954109323Ssam			return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
1955109323Ssam			    totlen);
1956109323Ssam		}
195753702Swpaul
1958109323Ssam		if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
1959109323Ssam			return error;
196053702Swpaul
1961109323Ssam		off += m->m_len;
1962109323Ssam		totlen -= len;
1963109323Ssam	}
1964109323Ssam	return 0;
1965109323Ssam}
196653702Swpaul
1967109323Ssamstatic int
1968109323Ssamwi_alloc_fid(struct wi_softc *sc, int len, int *idp)
196953702Swpaul{
197053702Swpaul	int i;
197153702Swpaul
1972109323Ssam	if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
1973178354Ssam		device_printf(sc->sc_dev, "%s: failed to allocate %d bytes on NIC\n",
1974178354Ssam		    __func__, len);
1975109323Ssam		return ENOMEM;
197653702Swpaul	}
197753702Swpaul
1978109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
1979109323Ssam		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
1980109323Ssam			break;
1981109323Ssam		DELAY(1);
198253702Swpaul	}
1983144167Ssam	if (i == WI_TIMEOUT) {
1984178354Ssam		device_printf(sc->sc_dev, "%s: timeout in alloc\n", __func__);
1985144167Ssam		return ETIMEDOUT;
1986144167Ssam	}
1987109323Ssam	*idp = CSR_READ_2(sc, WI_ALLOC_FID);
1988109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1989109323Ssam	return 0;
1990109323Ssam}
199153702Swpaul
1992109323Ssamstatic int
1993109323Ssamwi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
1994109323Ssam{
1995109323Ssam	int error, len;
1996109323Ssam	u_int16_t ltbuf[2];
199753702Swpaul
1998109323Ssam	/* Tell the NIC to enter record read mode. */
1999109323Ssam	error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2000109323Ssam	if (error)
2001109323Ssam		return error;
200253702Swpaul
2003109323Ssam	error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2004109323Ssam	if (error)
2005109323Ssam		return error;
200653702Swpaul
2007109323Ssam	if (le16toh(ltbuf[1]) != rid) {
2008109323Ssam		device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n",
2009109323Ssam		    rid, le16toh(ltbuf[1]));
2010109323Ssam		return EIO;
201153702Swpaul	}
2012109323Ssam	len = (le16toh(ltbuf[0]) - 1) * 2;	 /* already got rid */
2013109323Ssam	if (*buflenp < len) {
2014109323Ssam		device_printf(sc->sc_dev, "record buffer is too small, "
2015109323Ssam		    "rid=%x, size=%d, len=%d\n",
2016109323Ssam		    rid, *buflenp, len);
2017109323Ssam		return ENOSPC;
201853702Swpaul	}
2019109323Ssam	*buflenp = len;
2020109323Ssam	return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2021109323Ssam}
202253702Swpaul
2023109323Ssamstatic int
2024109323Ssamwi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2025109323Ssam{
2026109323Ssam	int error;
2027109323Ssam	u_int16_t ltbuf[2];
202853702Swpaul
2029109323Ssam	ltbuf[0] = htole16((buflen + 1) / 2 + 1);	 /* includes rid */
2030109323Ssam	ltbuf[1] = htole16(rid);
203153702Swpaul
2032109323Ssam	error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2033178354Ssam	if (error) {
2034178354Ssam		device_printf(sc->sc_dev, "%s: bap0 write failure, rid 0x%x\n",
2035178354Ssam		    __func__, rid);
2036109323Ssam		return error;
2037178354Ssam	}
2038109323Ssam	error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2039178354Ssam	if (error) {
2040178354Ssam		device_printf(sc->sc_dev, "%s: bap1 write failure, rid 0x%x\n",
2041178354Ssam		    __func__, rid);
2042109323Ssam		return error;
2043178354Ssam	}
2044102206Simp
2045109323Ssam	return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
204653702Swpaul}
204777217Sphk
204888546Salfredstatic int
2049178354Ssamwi_write_appie(struct wi_softc *sc, int rid, const struct ieee80211_appie *ie)
205077217Sphk{
2051178354Ssam	/* NB: 42 bytes is probably ok to have on the stack */
2052178354Ssam	char buf[sizeof(uint16_t) + 40];
205377217Sphk
2054178354Ssam	if (ie->ie_len > 40)
2055178354Ssam		return EINVAL;
2056178354Ssam	/* NB: firmware requires 16-bit ie length before ie data */
2057178354Ssam	*(uint16_t *) buf = htole16(ie->ie_len);
2058178354Ssam	memcpy(buf + sizeof(uint16_t), ie->ie_data, ie->ie_len);
2059178354Ssam	return wi_write_rid(sc, rid, buf, ie->ie_len + sizeof(uint16_t));
206077217Sphk}
206177217Sphk
2062109323Ssamint
2063109323Ssamwi_alloc(device_t dev, int rid)
2064109323Ssam{
2065109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2066109323Ssam
2067109323Ssam	if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2068109323Ssam		sc->iobase_rid = rid;
2069109323Ssam		sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2070109323Ssam		    &sc->iobase_rid, 0, ~0, (1 << 6),
2071109323Ssam		    rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2072178354Ssam		if (sc->iobase == NULL) {
2073109323Ssam			device_printf(dev, "No I/O space?!\n");
2074178354Ssam			return ENXIO;
207598440Simp		}
2076109323Ssam
2077109323Ssam		sc->wi_io_addr = rman_get_start(sc->iobase);
2078109323Ssam		sc->wi_btag = rman_get_bustag(sc->iobase);
2079109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2080109323Ssam	} else {
2081109323Ssam		sc->mem_rid = rid;
2082127135Snjl		sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2083127135Snjl		    &sc->mem_rid, RF_ACTIVE);
2084178354Ssam		if (sc->mem == NULL) {
2085109323Ssam			device_printf(dev, "No Mem space on prism2.5?\n");
2086178354Ssam			return ENXIO;
208777217Sphk		}
2088109323Ssam
2089109323Ssam		sc->wi_btag = rman_get_bustag(sc->mem);
2090109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->mem);
209177217Sphk	}
209277217Sphk
2093109323Ssam	sc->irq_rid = 0;
2094127135Snjl	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2095127135Snjl	    RF_ACTIVE |
2096109323Ssam	    ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2097178354Ssam	if (sc->irq == NULL) {
2098109323Ssam		wi_free(dev);
2099109323Ssam		device_printf(dev, "No irq?!\n");
2100178354Ssam		return ENXIO;
210177217Sphk	}
2102109323Ssam
2103109323Ssam	sc->sc_dev = dev;
2104109323Ssam	sc->sc_unit = device_get_unit(dev);
2105178354Ssam	return 0;
210677217Sphk}
210793359Simp
2108109323Ssamvoid
2109109323Ssamwi_free(device_t dev)
2110109323Ssam{
2111109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2112109323Ssam
2113109323Ssam	if (sc->iobase != NULL) {
2114109323Ssam		bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2115109323Ssam		sc->iobase = NULL;
2116109323Ssam	}
2117109323Ssam	if (sc->irq != NULL) {
2118109323Ssam		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2119109323Ssam		sc->irq = NULL;
2120109323Ssam	}
2121109323Ssam	if (sc->mem != NULL) {
2122109323Ssam		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
2123109323Ssam		sc->mem = NULL;
2124109323Ssam	}
2125109323Ssam}
2126