if_wi.c revision 283537
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 283537 2015-05-25 18:50:26Z glebius $");
64113038Sobrien
65230562Sadrian#include "opt_wlan.h"
66230562Sadrian
67109323Ssam#define WI_HERMES_STATS_WAR	/* Work around stats counter bug. */
68109323Ssam
6946492Swpaul#include <sys/param.h>
7046492Swpaul#include <sys/systm.h>
7195533Smike#include <sys/endian.h>
7246492Swpaul#include <sys/sockio.h>
7346492Swpaul#include <sys/mbuf.h>
74164033Srwatson#include <sys/priv.h>
7583366Sjulian#include <sys/proc.h>
7646492Swpaul#include <sys/kernel.h>
7746492Swpaul#include <sys/socket.h>
7853702Swpaul#include <sys/module.h>
7953702Swpaul#include <sys/bus.h>
8094486Simp#include <sys/random.h>
8153702Swpaul#include <sys/syslog.h>
8253702Swpaul#include <sys/sysctl.h>
8346492Swpaul
8453702Swpaul#include <machine/bus.h>
8553702Swpaul#include <machine/resource.h>
86116951Ssam#include <machine/atomic.h>
8753702Swpaul#include <sys/rman.h>
8853702Swpaul
8946492Swpaul#include <net/if.h>
90257176Sglebius#include <net/if_var.h>
9146492Swpaul#include <net/if_arp.h>
9246492Swpaul#include <net/ethernet.h>
9346492Swpaul#include <net/if_dl.h>
94190579Ssam#include <net/if_llc.h>
9546492Swpaul#include <net/if_media.h>
9646492Swpaul#include <net/if_types.h>
9746492Swpaul
98116951Ssam#include <net80211/ieee80211_var.h>
99116951Ssam#include <net80211/ieee80211_ioctl.h>
100119784Ssam#include <net80211/ieee80211_radiotap.h>
101116951Ssam
10246492Swpaul#include <netinet/in.h>
10346492Swpaul#include <netinet/in_systm.h>
10446492Swpaul#include <netinet/in_var.h>
10546492Swpaul#include <netinet/ip.h>
10646492Swpaul#include <netinet/if_ether.h>
10746492Swpaul
10846492Swpaul#include <net/bpf.h>
10946492Swpaul
11070808Speter#include <dev/wi/if_wavelan_ieee.h>
111119784Ssam#include <dev/wi/if_wireg.h>
11293611Simp#include <dev/wi/if_wivar.h>
11346492Swpaul
114228621Sbschmidtstatic struct ieee80211vap *wi_vap_create(struct ieee80211com *,
115228621Sbschmidt		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
116228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN],
117228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN]);
118178354Ssamstatic void wi_vap_delete(struct ieee80211vap *vap);
119178354Ssamstatic void wi_stop_locked(struct wi_softc *sc, int disable);
120165087Ssamstatic void wi_start_locked(struct ifnet *);
12191693Simpstatic void wi_start(struct ifnet *);
122160991Ssamstatic int  wi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr,
123160991Ssam		struct mbuf *m0);
124160991Ssamstatic int  wi_raw_xmit(struct ieee80211_node *, struct mbuf *,
125160991Ssam		const struct ieee80211_bpf_params *);
126178354Ssamstatic int  wi_newstate_sta(struct ieee80211vap *, enum ieee80211_state, int);
127192468Ssamstatic int  wi_newstate_hostap(struct ieee80211vap *, enum ieee80211_state,
128192468Ssam		int);
129178354Ssamstatic void wi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
130283535Sadrian		int subtype, const struct ieee80211_rx_stats *rxs,
131283535Sadrian		int rssi, int nf);
132178354Ssamstatic int  wi_reset(struct wi_softc *);
133165089Ssamstatic void wi_watchdog(void *);
134109323Ssamstatic int  wi_ioctl(struct ifnet *, u_long, caddr_t);
135109323Ssamstatic void wi_media_status(struct ifnet *, struct ifmediareq *);
136272068Sglebiusstatic uint64_t wi_get_counter(struct ifnet *, ift_counter);
13746492Swpaul
138109323Ssamstatic void wi_rx_intr(struct wi_softc *);
139109323Ssamstatic void wi_tx_intr(struct wi_softc *);
140109323Ssamstatic void wi_tx_ex_intr(struct wi_softc *);
141178354Ssam
142109323Ssamstatic void wi_info_intr(struct wi_softc *);
14346492Swpaul
144178354Ssamstatic int  wi_write_txrate(struct wi_softc *, struct ieee80211vap *);
145178354Ssamstatic int  wi_write_wep(struct wi_softc *, struct ieee80211vap *);
146109323Ssamstatic int  wi_write_multi(struct wi_softc *);
147178354Ssamstatic void wi_update_mcast(struct ifnet *);
148192468Ssamstatic void wi_update_promisc(struct ifnet *);
149109323Ssamstatic int  wi_alloc_fid(struct wi_softc *, int, int *);
150109323Ssamstatic void wi_read_nicid(struct wi_softc *);
151109323Ssamstatic int  wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
15253702Swpaul
153192492Simpstatic int  wi_cmd(struct wi_softc *, int, int, int, int);
154109323Ssamstatic int  wi_seek_bap(struct wi_softc *, int, int);
155109323Ssamstatic int  wi_read_bap(struct wi_softc *, int, int, void *, int);
156109323Ssamstatic int  wi_write_bap(struct wi_softc *, int, int, void *, int);
157109323Ssamstatic int  wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
158109323Ssamstatic int  wi_read_rid(struct wi_softc *, int, void *, int *);
159109323Ssamstatic int  wi_write_rid(struct wi_softc *, int, void *, int);
160178354Ssamstatic int  wi_write_appie(struct wi_softc *, int, const struct ieee80211_appie *);
16177217Sphk
162170530Ssamstatic void wi_scan_start(struct ieee80211com *);
163170530Ssamstatic void wi_scan_end(struct ieee80211com *);
164170530Ssamstatic void wi_set_channel(struct ieee80211com *);
165170530Ssam
166109323Ssamstatic __inline int
167109323Ssamwi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
168109323Ssam{
16953702Swpaul
170109323Ssam	val = htole16(val);
171109323Ssam	return wi_write_rid(sc, rid, &val, sizeof(val));
172109323Ssam}
173109323Ssam
174227309Sedstatic SYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0,
175227309Sed	    "Wireless driver parameters");
176109592Ssam
177109323Ssamstatic	struct timeval lasttxerror;	/* time of last tx error msg */
178109323Ssamstatic	int curtxeps;			/* current tx error msgs/sec */
179111559Ssamstatic	int wi_txerate = 0;		/* tx error rate: max msgs/sec */
180109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate,
181111559Ssam	    0, "max tx error msgs/sec; 0 to disable msgs");
182109323Ssam
183109323Ssam#define	WI_DEBUG
184109323Ssam#ifdef WI_DEBUG
185109323Ssamstatic	int wi_debug = 0;
186109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug,
187109592Ssam	    0, "control debugging printfs");
188109323Ssam#define	DPRINTF(X)	if (wi_debug) printf X
189109323Ssam#else
190109323Ssam#define	DPRINTF(X)
191109323Ssam#endif
192109323Ssam
193109323Ssam#define WI_INTRS	(WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
194109323Ssam
19593825Simpstruct wi_card_ident wi_card_ident[] = {
19693825Simp	/* CARD_ID			CARD_NAME		FIRM_TYPE */
19793825Simp	{ WI_NIC_LUCENT_ID,		WI_NIC_LUCENT_STR,	WI_LUCENT },
19893825Simp	{ WI_NIC_SONY_ID,		WI_NIC_SONY_STR,	WI_LUCENT },
19993825Simp	{ WI_NIC_LUCENT_EMB_ID,		WI_NIC_LUCENT_EMB_STR,	WI_LUCENT },
20093825Simp	{ WI_NIC_EVB2_ID,		WI_NIC_EVB2_STR,	WI_INTERSIL },
20193825Simp	{ WI_NIC_HWB3763_ID,		WI_NIC_HWB3763_STR,	WI_INTERSIL },
20293825Simp	{ WI_NIC_HWB3163_ID,		WI_NIC_HWB3163_STR,	WI_INTERSIL },
20393825Simp	{ WI_NIC_HWB3163B_ID,		WI_NIC_HWB3163B_STR,	WI_INTERSIL },
20493825Simp	{ WI_NIC_EVB3_ID,		WI_NIC_EVB3_STR,	WI_INTERSIL },
20593825Simp	{ WI_NIC_HWB1153_ID,		WI_NIC_HWB1153_STR,	WI_INTERSIL },
20693825Simp	{ WI_NIC_P2_SST_ID,		WI_NIC_P2_SST_STR,	WI_INTERSIL },
20793825Simp	{ WI_NIC_EVB2_SST_ID,		WI_NIC_EVB2_SST_STR,	WI_INTERSIL },
20893825Simp	{ WI_NIC_3842_EVA_ID,		WI_NIC_3842_EVA_STR,	WI_INTERSIL },
20993825Simp	{ WI_NIC_3842_PCMCIA_AMD_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
21093825Simp	{ WI_NIC_3842_PCMCIA_SST_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
211101355Simp	{ WI_NIC_3842_PCMCIA_ATL_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
212101355Simp	{ WI_NIC_3842_PCMCIA_ATS_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
21393825Simp	{ WI_NIC_3842_MINI_AMD_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
21493825Simp	{ WI_NIC_3842_MINI_SST_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
215101355Simp	{ WI_NIC_3842_MINI_ATL_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
216101355Simp	{ WI_NIC_3842_MINI_ATS_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
21793825Simp	{ WI_NIC_3842_PCI_AMD_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
21893825Simp	{ WI_NIC_3842_PCI_SST_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
219101355Simp	{ WI_NIC_3842_PCI_ATS_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
220101355Simp	{ WI_NIC_3842_PCI_ATL_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
22193825Simp	{ WI_NIC_P3_PCMCIA_AMD_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
22293825Simp	{ WI_NIC_P3_PCMCIA_SST_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
223101355Simp	{ WI_NIC_P3_PCMCIA_ATL_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
224101355Simp	{ WI_NIC_P3_PCMCIA_ATS_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
22593825Simp	{ WI_NIC_P3_MINI_AMD_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
22693825Simp	{ WI_NIC_P3_MINI_SST_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
227101355Simp	{ WI_NIC_P3_MINI_ATL_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
228101355Simp	{ WI_NIC_P3_MINI_ATS_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
22993825Simp	{ 0,	NULL,	0 },
23093825Simp};
23193825Simp
232180919Simpstatic char *wi_firmware_names[] = { "none", "Hermes", "Intersil", "Symbol" };
233180919Simp
234109323Ssamdevclass_t wi_devclass;
23546492Swpaul
23693611Simpint
237109323Ssamwi_attach(device_t dev)
23874906Salfred{
239109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
240178354Ssam	struct ieee80211com *ic;
241147256Sbrooks	struct ifnet *ifp;
242116951Ssam	int i, nrates, buflen;
243109323Ssam	u_int16_t val;
244109323Ssam	u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
245116951Ssam	struct ieee80211_rateset *rs;
246180919Simp	struct sysctl_ctx_list *sctx;
247180919Simp	struct sysctl_oid *soid;
248109323Ssam	static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
249109323Ssam		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
250109323Ssam	};
251109323Ssam	int error;
252190526Ssam	uint8_t macaddr[IEEE80211_ADDR_LEN];
25374906Salfred
254178354Ssam	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
255147256Sbrooks	if (ifp == NULL) {
256147256Sbrooks		device_printf(dev, "can not if_alloc\n");
257147256Sbrooks		wi_free(dev);
258178354Ssam		return ENOSPC;
259147256Sbrooks	}
260178354Ssam	ic = ifp->if_l2com;
261147256Sbrooks
262138571Ssam	sc->sc_firmware_type = WI_NOTYPE;
263123339Simp	sc->wi_cmd_count = 500;
26446492Swpaul	/* Reset the NIC. */
265178354Ssam	if (wi_reset(sc) != 0) {
266178354Ssam		wi_free(dev);
267109323Ssam		return ENXIO;		/* XXX */
268178354Ssam	}
26946492Swpaul
270178354Ssam	/* Read NIC identification */
271178354Ssam	wi_read_nicid(sc);
272178354Ssam	switch (sc->sc_firmware_type) {
273178354Ssam	case WI_LUCENT:
274178354Ssam		if (sc->sc_sta_firmware_ver < 60006)
275178354Ssam			goto reject;
276178354Ssam		break;
277178354Ssam	case WI_INTERSIL:
278178354Ssam		if (sc->sc_sta_firmware_ver < 800)
279178354Ssam			goto reject;
280178354Ssam		break;
281178354Ssam	default:
282178354Ssam	reject:
283178354Ssam		device_printf(dev, "Sorry, this card is not supported "
284178354Ssam		    "(type %d, firmware ver %d)\n",
285178354Ssam		    sc->sc_firmware_type, sc->sc_sta_firmware_ver);
286178354Ssam		wi_free(dev);
287178354Ssam		return EOPNOTSUPP;
288178354Ssam	}
289178354Ssam
290180919Simp	/* Export info about the device via sysctl */
291180919Simp	sctx = device_get_sysctl_ctx(dev);
292180919Simp	soid = device_get_sysctl_tree(dev);
293180919Simp	SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
294180919Simp	    "firmware_type", CTLFLAG_RD,
295180919Simp	    wi_firmware_names[sc->sc_firmware_type], 0,
296180919Simp	    "Firmware type string");
297180919Simp	SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "sta_version",
298180919Simp	    CTLFLAG_RD, &sc->sc_sta_firmware_ver, 0,
299180919Simp	    "Station Firmware version");
300180919Simp	if (sc->sc_firmware_type == WI_INTERSIL)
301180919Simp		SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
302180919Simp		    "pri_version", CTLFLAG_RD, &sc->sc_pri_firmware_ver, 0,
303180919Simp		    "Primary Firmware version");
304217586Smdf	SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "nic_id",
305180919Simp	    CTLFLAG_RD, &sc->sc_nic_id, 0, "NIC id");
306180919Simp	SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "nic_name",
307180919Simp	    CTLFLAG_RD, sc->sc_nic_name, 0, "NIC name");
308180919Simp
309178354Ssam	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
310178354Ssam	    MTX_DEF | MTX_RECURSE);
311178354Ssam	callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
312178354Ssam
31376438Swpaul	/*
31476438Swpaul	 * Read the station address.
31576438Swpaul	 * And do it twice. I've seen PRISM-based cards that return
31676438Swpaul	 * an error when trying to read it the first time, which causes
31776438Swpaul	 * the probe to fail.
31876438Swpaul	 */
319109323Ssam	buflen = IEEE80211_ADDR_LEN;
320190526Ssam	error = wi_read_rid(sc, WI_RID_MAC_NODE, macaddr, &buflen);
321109323Ssam	if (error != 0) {
322109323Ssam		buflen = IEEE80211_ADDR_LEN;
323190526Ssam		error = wi_read_rid(sc, WI_RID_MAC_NODE, macaddr, &buflen);
324109323Ssam	}
325190526Ssam	if (error || IEEE80211_ADDR_EQ(macaddr, empty_macaddr)) {
326109323Ssam		if (error != 0)
327109323Ssam			device_printf(dev, "mac read failed %d\n", error);
328148714Simp		else {
329109323Ssam			device_printf(dev, "mac read failed (all zeros)\n");
330148714Simp			error = ENXIO;
331148714Simp		}
33275149Simp		wi_free(dev);
33375149Simp		return (error);
33475149Simp	}
33546492Swpaul
336178354Ssam	ifp->if_softc = sc;
337121816Sbrooks	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
33846492Swpaul	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
33946492Swpaul	ifp->if_ioctl = wi_ioctl;
34046492Swpaul	ifp->if_start = wi_start;
34146492Swpaul	ifp->if_init = wi_init;
342272068Sglebius	ifp->if_get_counter = wi_get_counter;
343207554Ssobomax	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
344207554Ssobomax	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
345132986Smlaier	IFQ_SET_READY(&ifp->if_snd);
34646492Swpaul
347138571Ssam	ic->ic_ifp = ifp;
348283537Sglebius	ic->ic_softc = sc;
349283527Sglebius	ic->ic_name = device_get_nameunit(dev);
350109323Ssam	ic->ic_phytype = IEEE80211_T_DS;
351109323Ssam	ic->ic_opmode = IEEE80211_M_STA;
352178957Ssam	ic->ic_caps = IEEE80211_C_STA
353178957Ssam		    | IEEE80211_C_PMGT
354178354Ssam		    | IEEE80211_C_MONITOR
355138571Ssam		    ;
35646492Swpaul
357116951Ssam	/*
358116951Ssam	 * Query the card for available channels and setup the
359116951Ssam	 * channel table.  We assume these are all 11b channels.
360116951Ssam	 */
361109323Ssam	buflen = sizeof(val);
362109323Ssam	if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
363109323Ssam		val = htole16(0x1fff);	/* assume 1-11 */
364116951Ssam	KASSERT(val != 0, ("wi_attach: no available channels listed!"));
365116951Ssam
366116951Ssam	val <<= 1;			/* shift for base 1 indices */
367116951Ssam	for (i = 1; i < 16; i++) {
368170530Ssam		struct ieee80211_channel *c;
369170530Ssam
370144986Smdodd		if (!isset((u_int8_t*)&val, i))
371144986Smdodd			continue;
372170530Ssam		c = &ic->ic_channels[ic->ic_nchans++];
373170530Ssam		c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
374170530Ssam		c->ic_flags = IEEE80211_CHAN_B;
375170530Ssam		c->ic_ieee = i;
376178354Ssam		/* XXX txpowers? */
377109323Ssam	}
37846492Swpaul
37946563Swpaul	/*
38098440Simp	 * Set flags based on firmware version.
38198440Simp	 */
38298440Simp	switch (sc->sc_firmware_type) {
38398440Simp	case WI_LUCENT:
384112363Simp		sc->sc_ntxbuf = 1;
385178354Ssam		ic->ic_caps |= IEEE80211_C_IBSS;
386119784Ssam
387178354Ssam		sc->sc_ibss_port = WI_PORTTYPE_BSS;
388178354Ssam		sc->sc_monitor_port = WI_PORTTYPE_ADHOC;
389119784Ssam		sc->sc_min_rssi = WI_LUCENT_MIN_RSSI;
390119784Ssam		sc->sc_max_rssi = WI_LUCENT_MAX_RSSI;
391119784Ssam		sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
39298440Simp		break;
39398440Simp	case WI_INTERSIL:
394112363Simp		sc->sc_ntxbuf = WI_NTXBUF;
395178354Ssam		sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR
396178354Ssam			     |  WI_FLAGS_HAS_ROAMING;
397123339Simp		/*
398123339Simp		 * Old firmware are slow, so give peace a chance.
399123339Simp		 */
400123339Simp		if (sc->sc_sta_firmware_ver < 10000)
401123339Simp			sc->wi_cmd_count = 5000;
402109323Ssam		if (sc->sc_sta_firmware_ver > 10101)
403109323Ssam			sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
404178354Ssam		ic->ic_caps |= IEEE80211_C_IBSS;
405109396Simp		/*
406109396Simp		 * version 0.8.3 and newer are the only ones that are known
407109396Simp		 * to currently work.  Earlier versions can be made to work,
408178354Ssam		 * at least according to the Linux driver but we require
409178354Ssam		 * monitor mode so this is irrelevant.
410109396Simp		 */
411178354Ssam		ic->ic_caps |= IEEE80211_C_HOSTAP;
412178354Ssam		if (sc->sc_sta_firmware_ver >= 10603)
413178354Ssam			sc->sc_flags |= WI_FLAGS_HAS_ENHSECURITY;
414178354Ssam		if (sc->sc_sta_firmware_ver >= 10700) {
415178354Ssam			/*
416178354Ssam			 * 1.7.0+ have the necessary support for sta mode WPA.
417178354Ssam			 */
418178354Ssam			sc->sc_flags |= WI_FLAGS_HAS_WPASUPPORT;
419178354Ssam			ic->ic_caps |= IEEE80211_C_WPA;
420178354Ssam		}
421119784Ssam
422178354Ssam		sc->sc_ibss_port = WI_PORTTYPE_IBSS;
423178354Ssam		sc->sc_monitor_port = WI_PORTTYPE_APSILENT;
424119784Ssam		sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
425119784Ssam		sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
426119784Ssam		sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
42798440Simp		break;
42898440Simp	}
42998440Simp
43098440Simp	/*
43156965Swpaul	 * Find out if we support WEP on this card.
43256965Swpaul	 */
433109323Ssam	buflen = sizeof(val);
434109323Ssam	if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
435109323Ssam	    val != htole16(0))
436178354Ssam		ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
43756965Swpaul
438109323Ssam	/* Find supported rates. */
439109323Ssam	buflen = sizeof(ratebuf);
440116951Ssam	rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
441109323Ssam	if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
442116951Ssam		nrates = le16toh(*(u_int16_t *)ratebuf);
443116951Ssam		if (nrates > IEEE80211_RATE_MAXSIZE)
444116951Ssam			nrates = IEEE80211_RATE_MAXSIZE;
445116951Ssam		rs->rs_nrates = 0;
446116951Ssam		for (i = 0; i < nrates; i++)
447116951Ssam			if (ratebuf[2+i])
448116951Ssam				rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i];
449109323Ssam	} else {
450109323Ssam		/* XXX fallback on error? */
45198440Simp	}
45277217Sphk
453109323Ssam	buflen = sizeof(val);
454109323Ssam	if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
455109323Ssam	    wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
456119784Ssam		sc->sc_dbm_offset = le16toh(val);
457119784Ssam	}
45846492Swpaul
459109323Ssam	sc->sc_portnum = WI_DEFAULT_PORT;
46087383Simp
461190526Ssam	ieee80211_ifattach(ic, macaddr);
462160991Ssam	ic->ic_raw_xmit = wi_raw_xmit;
463170530Ssam	ic->ic_scan_start = wi_scan_start;
464170530Ssam	ic->ic_scan_end = wi_scan_end;
465170530Ssam	ic->ic_set_channel = wi_set_channel;
466170530Ssam
467178354Ssam	ic->ic_vap_create = wi_vap_create;
468178354Ssam	ic->ic_vap_delete = wi_vap_delete;
469178354Ssam	ic->ic_update_mcast = wi_update_mcast;
470192468Ssam	ic->ic_update_promisc = wi_update_promisc;
471109323Ssam
472192468Ssam	ieee80211_radiotap_attach(ic,
473192468Ssam	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
474192468Ssam		WI_TX_RADIOTAP_PRESENT,
475192468Ssam	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
476192468Ssam		WI_RX_RADIOTAP_PRESENT);
477119784Ssam
478138571Ssam	if (bootverbose)
479138571Ssam		ieee80211_announce(ic);
480138571Ssam
481180826Simp	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
482180826Simp	    NULL, wi_intr, sc, &sc->wi_intrhand);
483180826Simp	if (error) {
484180826Simp		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
485180826Simp		ieee80211_ifdetach(ic);
486180826Simp		if_free(sc->sc_ifp);
487180826Simp		wi_free(dev);
488180826Simp		return error;
489180826Simp	}
490180826Simp
491109323Ssam	return (0);
49287383Simp}
49387383Simp
494109323Ssamint
495109323Ssamwi_detach(device_t dev)
49646492Swpaul{
497109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
498147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
499178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
50046492Swpaul
501109323Ssam	WI_LOCK(sc);
50246492Swpaul
503109323Ssam	/* check if device was removed */
504123098Simp	sc->wi_gone |= !bus_child_present(dev);
50546492Swpaul
506178354Ssam	wi_stop_locked(sc, 0);
507150678Sru	WI_UNLOCK(sc);
508178354Ssam	ieee80211_ifdetach(ic);
50946492Swpaul
510109323Ssam	bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
511150306Simp	if_free(sc->sc_ifp);
512109323Ssam	wi_free(dev);
513109323Ssam	mtx_destroy(&sc->sc_mtx);
514109323Ssam	return (0);
515109323Ssam}
51693359Simp
517178354Ssamstatic struct ieee80211vap *
518228621Sbschmidtwi_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
519228621Sbschmidt    enum ieee80211_opmode opmode, int flags,
520228621Sbschmidt    const uint8_t bssid[IEEE80211_ADDR_LEN],
521228621Sbschmidt    const uint8_t mac[IEEE80211_ADDR_LEN])
522109323Ssam{
523178354Ssam	struct wi_softc *sc = ic->ic_ifp->if_softc;
524178354Ssam	struct wi_vap *wvp;
525178354Ssam	struct ieee80211vap *vap;
52693359Simp
527178354Ssam	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
528178354Ssam		return NULL;
529178354Ssam	wvp = (struct wi_vap *) malloc(sizeof(struct wi_vap),
530178354Ssam	    M_80211_VAP, M_NOWAIT | M_ZERO);
531178354Ssam	if (wvp == NULL)
532178354Ssam		return NULL;
53393359Simp
534178354Ssam	vap = &wvp->wv_vap;
535178354Ssam	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
53693359Simp
537178354Ssam	vap->iv_max_aid = WI_MAX_AID;
53893359Simp
539178354Ssam	switch (opmode) {
540178354Ssam	case IEEE80211_M_STA:
541178354Ssam		sc->sc_porttype = WI_PORTTYPE_BSS;
542178354Ssam		wvp->wv_newstate = vap->iv_newstate;
543178354Ssam		vap->iv_newstate = wi_newstate_sta;
544178354Ssam		/* need to filter mgt frames to avoid confusing state machine */
545178354Ssam		wvp->wv_recv_mgmt = vap->iv_recv_mgmt;
546178354Ssam		vap->iv_recv_mgmt = wi_recv_mgmt;
547109323Ssam		break;
548178354Ssam	case IEEE80211_M_IBSS:
549178354Ssam		sc->sc_porttype = sc->sc_ibss_port;
550178354Ssam		wvp->wv_newstate = vap->iv_newstate;
551178354Ssam		vap->iv_newstate = wi_newstate_sta;
552109323Ssam		break;
553178354Ssam	case IEEE80211_M_AHDEMO:
554178354Ssam		sc->sc_porttype = WI_PORTTYPE_ADHOC;
555109323Ssam		break;
556178354Ssam	case IEEE80211_M_HOSTAP:
557178354Ssam		sc->sc_porttype = WI_PORTTYPE_HOSTAP;
558178354Ssam		wvp->wv_newstate = vap->iv_newstate;
559178354Ssam		vap->iv_newstate = wi_newstate_hostap;
560178354Ssam		break;
561178354Ssam	case IEEE80211_M_MONITOR:
562178354Ssam		sc->sc_porttype = sc->sc_monitor_port;
563178354Ssam		break;
564178354Ssam	default:
565178354Ssam		break;
56693359Simp	}
567178354Ssam
568178354Ssam	/* complete setup */
569178354Ssam	ieee80211_vap_attach(vap, ieee80211_media_change, wi_media_status);
570178354Ssam	ic->ic_opmode = opmode;
571178354Ssam	return vap;
57246492Swpaul}
57346492Swpaul
574178354Ssamstatic void
575178354Ssamwi_vap_delete(struct ieee80211vap *vap)
576178354Ssam{
577178354Ssam	struct wi_vap *wvp = WI_VAP(vap);
578178354Ssam
579178354Ssam	ieee80211_vap_detach(vap);
580178354Ssam	free(wvp, M_80211_VAP);
581178354Ssam}
582178354Ssam
583194023Savgint
584109323Ssamwi_shutdown(device_t dev)
58546492Swpaul{
586109323Ssam	struct wi_softc *sc = device_get_softc(dev);
58746492Swpaul
588178354Ssam	wi_stop(sc, 1);
589194023Savg	return (0);
59046492Swpaul}
59146492Swpaul
592109323Ssamvoid
593109323Ssamwi_intr(void *arg)
59446492Swpaul{
595109323Ssam	struct wi_softc *sc = arg;
596147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
597112363Simp	u_int16_t status;
59846492Swpaul
599109323Ssam	WI_LOCK(sc);
60046492Swpaul
601123098Simp	if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) {
602113327Simp		CSR_WRITE_2(sc, WI_INT_EN, 0);
603123098Simp		CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
604109323Ssam		WI_UNLOCK(sc);
60546492Swpaul		return;
606109323Ssam	}
60746492Swpaul
608112363Simp	/* Disable interrupts. */
609112363Simp	CSR_WRITE_2(sc, WI_INT_EN, 0);
61046492Swpaul
611112363Simp	status = CSR_READ_2(sc, WI_EVENT_STAT);
612112363Simp	if (status & WI_EV_RX)
613112363Simp		wi_rx_intr(sc);
614112363Simp	if (status & WI_EV_ALLOC)
615112363Simp		wi_tx_intr(sc);
616112363Simp	if (status & WI_EV_TX_EXC)
617112363Simp		wi_tx_ex_intr(sc);
618112363Simp	if (status & WI_EV_INFO)
619112363Simp		wi_info_intr(sc);
620148887Srwatson	if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
621132986Smlaier	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
622165087Ssam		wi_start_locked(ifp);
62346492Swpaul
624112363Simp	/* Re-enable interrupts. */
625112363Simp	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
62646492Swpaul
627109323Ssam	WI_UNLOCK(sc);
62846492Swpaul
62946492Swpaul	return;
63046492Swpaul}
63146492Swpaul
632178354Ssamstatic void
633178354Ssamwi_enable(struct wi_softc *sc)
634178354Ssam{
635178354Ssam	/* Enable interrupts */
636178354Ssam	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
637178354Ssam
638178354Ssam	/* enable port */
639178354Ssam	wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
640178354Ssam	sc->sc_enabled = 1;
641178354Ssam}
642178354Ssam
643178354Ssamstatic int
644178354Ssamwi_setup_locked(struct wi_softc *sc, int porttype, int mode,
645178354Ssam	uint8_t mac[IEEE80211_ADDR_LEN])
646178354Ssam{
647178354Ssam	int i;
648178354Ssam
649178354Ssam	wi_reset(sc);
650178354Ssam
651178354Ssam	wi_write_val(sc, WI_RID_PORTTYPE, porttype);
652178354Ssam	wi_write_val(sc, WI_RID_CREATE_IBSS, mode);
653178354Ssam	wi_write_val(sc, WI_RID_MAX_DATALEN, 2304);
654178354Ssam	/* XXX IEEE80211_BPF_NOACK wants 0 */
655178354Ssam	wi_write_val(sc, WI_RID_ALT_RETRY_CNT, 2);
656178354Ssam	if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
657178354Ssam		wi_write_val(sc, WI_RID_ROAMING_MODE, 3); /* NB: disabled */
658178354Ssam
659178354Ssam	wi_write_rid(sc, WI_RID_MAC_NODE, mac, IEEE80211_ADDR_LEN);
660178354Ssam
661178354Ssam	/* Allocate fids for the card */
662178354Ssam	sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
663178354Ssam	for (i = 0; i < sc->sc_ntxbuf; i++) {
664178354Ssam		int error = wi_alloc_fid(sc, sc->sc_buflen,
665178354Ssam		    &sc->sc_txd[i].d_fid);
666178354Ssam		if (error) {
667178354Ssam			device_printf(sc->sc_dev,
668178354Ssam			    "tx buffer allocation failed (error %u)\n",
669178354Ssam			    error);
670178354Ssam			return error;
671178354Ssam		}
672178354Ssam		sc->sc_txd[i].d_len = 0;
673178354Ssam	}
674178354Ssam	sc->sc_txcur = sc->sc_txnext = 0;
675178354Ssam
676178354Ssam	return 0;
677178354Ssam}
678178354Ssam
679178354Ssamstatic void
680178354Ssamwi_init_locked(struct wi_softc *sc)
681178354Ssam{
682178354Ssam	struct ifnet *ifp = sc->sc_ifp;
683178354Ssam	int wasenabled;
684178354Ssam
685178354Ssam	WI_LOCK_ASSERT(sc);
686178354Ssam
687178354Ssam	wasenabled = sc->sc_enabled;
688178354Ssam	if (wasenabled)
689178354Ssam		wi_stop_locked(sc, 1);
690178354Ssam
691190526Ssam	if (wi_setup_locked(sc, sc->sc_porttype, 3, IF_LLADDR(ifp)) != 0) {
692178354Ssam		if_printf(ifp, "interface not running\n");
693178354Ssam		wi_stop_locked(sc, 1);
694178354Ssam		return;
695178354Ssam	}
696178354Ssam
697178354Ssam	ifp->if_drv_flags |= IFF_DRV_RUNNING;
698178354Ssam	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
699178354Ssam
700178354Ssam	callout_reset(&sc->sc_watchdog, hz, wi_watchdog, sc);
701178354Ssam
702178354Ssam	wi_enable(sc);			/* Enable desired port */
703178354Ssam}
704178354Ssam
705109323Ssamvoid
706109323Ssamwi_init(void *arg)
70746492Swpaul{
708109323Ssam	struct wi_softc *sc = arg;
709147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
710178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
71146492Swpaul
712178354Ssam	WI_LOCK(sc);
713178354Ssam	wi_init_locked(sc);
714178354Ssam	WI_UNLOCK(sc);
71567092Swpaul
716178931Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
717178931Sthompsa		ieee80211_start_all(ic);		/* start all vap's */
718178354Ssam}
719170530Ssam
720178354Ssamstatic void
721178354Ssamwi_stop_locked(struct wi_softc *sc, int disable)
722178354Ssam{
723178354Ssam	struct ifnet *ifp = sc->sc_ifp;
72446492Swpaul
725178354Ssam	WI_LOCK_ASSERT(sc);
72646492Swpaul
727178354Ssam	if (sc->sc_enabled && !sc->wi_gone) {
728178354Ssam		CSR_WRITE_2(sc, WI_INT_EN, 0);
729178354Ssam		wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
730178354Ssam		if (disable)
731178354Ssam			sc->sc_enabled = 0;
732178354Ssam	} else if (sc->wi_gone && disable)	/* gone --> not enabled */
733178354Ssam		sc->sc_enabled = 0;
734178354Ssam
735178354Ssam	callout_stop(&sc->sc_watchdog);
736178354Ssam	sc->sc_tx_timer = 0;
737178354Ssam	sc->sc_false_syns = 0;
738178354Ssam
739178354Ssam	ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE | IFF_DRV_RUNNING);
740178354Ssam}
741178354Ssam
742178354Ssamvoid
743178354Ssamwi_stop(struct wi_softc *sc, int disable)
744178354Ssam{
745170530Ssam	WI_LOCK(sc);
746178354Ssam	wi_stop_locked(sc, disable);
747178354Ssam	WI_UNLOCK(sc);
748178354Ssam}
749170530Ssam
750178354Ssamstatic void
751178354Ssamwi_set_channel(struct ieee80211com *ic)
752178354Ssam{
753178354Ssam	struct ifnet *ifp = ic->ic_ifp;
754178354Ssam	struct wi_softc *sc = ifp->if_softc;
755160991Ssam
756178354Ssam	DPRINTF(("%s: channel %d, %sscanning\n", __func__,
757178354Ssam	    ieee80211_chan2ieee(ic, ic->ic_curchan),
758178354Ssam	    ic->ic_flags & IEEE80211_F_SCAN ? "" : "!"));
75946492Swpaul
760178354Ssam	WI_LOCK(sc);
761116951Ssam	wi_write_val(sc, WI_RID_OWN_CHNL,
762178354Ssam	    ieee80211_chan2ieee(ic, ic->ic_curchan));
763178354Ssam	WI_UNLOCK(sc);
764178354Ssam}
76575373Salfred
766178354Ssamstatic void
767178354Ssamwi_scan_start(struct ieee80211com *ic)
768178354Ssam{
769178354Ssam	struct ifnet *ifp = ic->ic_ifp;
770178354Ssam	struct wi_softc *sc = ifp->if_softc;
771178354Ssam	struct ieee80211_scan_state *ss = ic->ic_scan;
77246492Swpaul
773178354Ssam	DPRINTF(("%s\n", __func__));
77446492Swpaul
775178354Ssam	WI_LOCK(sc);
776109323Ssam	/*
777178354Ssam	 * Switch device to monitor mode.
778109323Ssam	 */
779178354Ssam	wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_monitor_port);
780178354Ssam	if (sc->sc_firmware_type == WI_INTERSIL) {
781178354Ssam		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
782178354Ssam		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
78375373Salfred	}
784178354Ssam	/* force full dwell time to compensate for firmware overhead */
785178354Ssam	ss->ss_mindwell = ss->ss_maxdwell = msecs_to_ticks(400);
786178354Ssam	WI_UNLOCK(sc);
78746492Swpaul
788178354Ssam}
78967092Swpaul
790178354Ssamstatic void
791178354Ssamwi_scan_end(struct ieee80211com *ic)
792178354Ssam{
793178354Ssam	struct ifnet *ifp = ic->ic_ifp;
794178354Ssam	struct wi_softc *sc = ifp->if_softc;
79546492Swpaul
796178354Ssam	DPRINTF(("%s: restore port type %d\n", __func__, sc->sc_porttype));
797178354Ssam
798178354Ssam	WI_LOCK(sc);
799178354Ssam	wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_porttype);
800178354Ssam	if (sc->sc_firmware_type == WI_INTERSIL) {
801178354Ssam		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
802178354Ssam		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
80370073Swpaul	}
804178354Ssam	WI_UNLOCK(sc);
805178354Ssam}
80670073Swpaul
807178354Ssamstatic void
808178354Ssamwi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
809283535Sadrian	int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
810178354Ssam{
811178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
81246492Swpaul
813178354Ssam	switch (subtype) {
814178354Ssam	case IEEE80211_FC0_SUBTYPE_AUTH:
815178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
816178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
817178354Ssam		/* NB: filter frames that trigger state changes */
818178354Ssam		return;
819170530Ssam	}
820283535Sadrian	WI_VAP(vap)->wv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
821178354Ssam}
82246492Swpaul
823178354Ssamstatic int
824178354Ssamwi_newstate_sta(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
825178354Ssam{
826178354Ssam	struct ieee80211com *ic = vap->iv_ic;
827178354Ssam	struct ifnet *ifp = ic->ic_ifp;
828178354Ssam	struct ieee80211_node *bss;
829178354Ssam	struct wi_softc *sc = ifp->if_softc;
83094397Simp
831178354Ssam	DPRINTF(("%s: %s -> %s\n", __func__,
832178354Ssam		ieee80211_state_name[vap->iv_state],
833178354Ssam		ieee80211_state_name[nstate]));
834178354Ssam
835178354Ssam	if (nstate == IEEE80211_S_AUTH) {
836178354Ssam		WI_LOCK(sc);
837178354Ssam		wi_setup_locked(sc, WI_PORTTYPE_BSS, 3, vap->iv_myaddr);
838178354Ssam
839178354Ssam		if (vap->iv_flags & IEEE80211_F_PMGTON) {
840178354Ssam			wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
841178354Ssam			wi_write_val(sc, WI_RID_PM_ENABLED, 1);
842178354Ssam		}
843178354Ssam		wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold);
844178354Ssam		if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
845178354Ssam			wi_write_val(sc, WI_RID_FRAG_THRESH,
846178354Ssam			    vap->iv_fragthreshold);
847178354Ssam		wi_write_txrate(sc, vap);
848178354Ssam
849178354Ssam		bss = vap->iv_bss;
850178354Ssam		wi_write_ssid(sc, WI_RID_DESIRED_SSID, bss->ni_essid, bss->ni_esslen);
851178354Ssam		wi_write_val(sc, WI_RID_OWN_CHNL,
852178354Ssam		    ieee80211_chan2ieee(ic, bss->ni_chan));
853178354Ssam
854178354Ssam		/* Configure WEP. */
855178354Ssam		if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP)
856178354Ssam			wi_write_wep(sc, vap);
857178354Ssam		else
858178354Ssam			sc->sc_encryption = 0;
859178354Ssam
860178354Ssam		if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) &&
861178354Ssam		    (vap->iv_flags & IEEE80211_F_WPA)) {
862178354Ssam			wi_write_val(sc, WI_RID_WPA_HANDLING, 1);
863178354Ssam			if (vap->iv_appie_wpa != NULL)
864178354Ssam				wi_write_appie(sc, WI_RID_WPA_DATA,
865178354Ssam				    vap->iv_appie_wpa);
866178354Ssam		}
867178354Ssam
868178354Ssam		wi_enable(sc);		/* enable port */
869178354Ssam
870178354Ssam		/* Lucent firmware does not support the JOIN RID. */
871178354Ssam		if (sc->sc_firmware_type == WI_INTERSIL) {
872178354Ssam			struct wi_joinreq join;
873178354Ssam
874178354Ssam			memset(&join, 0, sizeof(join));
875178354Ssam			IEEE80211_ADDR_COPY(&join.wi_bssid, bss->ni_bssid);
876116951Ssam			join.wi_chan = htole16(
877178354Ssam			    ieee80211_chan2ieee(ic, bss->ni_chan));
878109323Ssam			wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
879178354Ssam		}
880178354Ssam		WI_UNLOCK(sc);
88175373Salfred
882178354Ssam		/*
883178354Ssam		 * NB: don't go through 802.11 layer, it'll send auth frame;
884178354Ssam		 * instead we drive the state machine from the link status
885178354Ssam		 * notification we get on association.
886178354Ssam		 */
887178354Ssam		vap->iv_state = nstate;
888191746Sthompsa		return (0);
88970073Swpaul	}
890178354Ssam	return WI_VAP(vap)->wv_newstate(vap, nstate, arg);
89146492Swpaul}
89246492Swpaul
893178354Ssamstatic int
894178354Ssamwi_newstate_hostap(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
89546492Swpaul{
896178354Ssam	struct ieee80211com *ic = vap->iv_ic;
897178354Ssam	struct ifnet *ifp = ic->ic_ifp;
898178354Ssam	struct ieee80211_node *bss;
899109323Ssam	struct wi_softc *sc = ifp->if_softc;
900178354Ssam	int error;
90146492Swpaul
902178354Ssam	DPRINTF(("%s: %s -> %s\n", __func__,
903178354Ssam		ieee80211_state_name[vap->iv_state],
904178354Ssam		ieee80211_state_name[nstate]));
90574998Swpaul
906178354Ssam	error = WI_VAP(vap)->wv_newstate(vap, nstate, arg);
907178354Ssam	if (error == 0 && nstate == IEEE80211_S_RUN) {
908178354Ssam		WI_LOCK(sc);
909178354Ssam		wi_setup_locked(sc, WI_PORTTYPE_HOSTAP, 0, vap->iv_myaddr);
910178354Ssam
911178354Ssam		bss = vap->iv_bss;
912178354Ssam		wi_write_ssid(sc, WI_RID_OWN_SSID,
913178354Ssam		    bss->ni_essid, bss->ni_esslen);
914178354Ssam		wi_write_val(sc, WI_RID_OWN_CHNL,
915178354Ssam		    ieee80211_chan2ieee(ic, bss->ni_chan));
916178354Ssam		wi_write_val(sc, WI_RID_BASIC_RATE, 0x3);
917178354Ssam		wi_write_val(sc, WI_RID_SUPPORT_RATE, 0xf);
918178354Ssam		wi_write_txrate(sc, vap);
919178354Ssam
920178354Ssam		wi_write_val(sc, WI_RID_OWN_BEACON_INT, bss->ni_intval);
921178354Ssam		wi_write_val(sc, WI_RID_DTIM_PERIOD, vap->iv_dtim_period);
922178354Ssam
923178354Ssam		wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold);
924178354Ssam		if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
925178354Ssam			wi_write_val(sc, WI_RID_FRAG_THRESH,
926178354Ssam			    vap->iv_fragthreshold);
927178354Ssam
928178354Ssam		if ((sc->sc_flags & WI_FLAGS_HAS_ENHSECURITY) &&
929178354Ssam		    (vap->iv_flags & IEEE80211_F_HIDESSID)) {
930178354Ssam			/*
931178354Ssam			 * bit 0 means hide SSID in beacons,
932178354Ssam			 * bit 1 means don't respond to bcast probe req
933178354Ssam			 */
934178354Ssam			wi_write_val(sc, WI_RID_ENH_SECURITY, 0x3);
93570073Swpaul		}
93670073Swpaul
937178354Ssam		if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) &&
938178354Ssam		    (vap->iv_flags & IEEE80211_F_WPA) &&
939178354Ssam		    vap->iv_appie_wpa != NULL)
940178354Ssam			wi_write_appie(sc, WI_RID_WPA_DATA, vap->iv_appie_wpa);
94146492Swpaul
942178354Ssam		wi_write_val(sc, WI_RID_PROMISC, 0);
943178354Ssam
944178354Ssam		/* Configure WEP. */
945178354Ssam		if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP)
946178354Ssam			wi_write_wep(sc, vap);
947178354Ssam		else
948178354Ssam			sc->sc_encryption = 0;
949178354Ssam
950178354Ssam		wi_enable(sc);		/* enable port */
951178354Ssam		WI_UNLOCK(sc);
952178354Ssam	}
953178354Ssam	return error;
95446492Swpaul}
95546492Swpaul
956109323Ssamstatic void
957165087Ssamwi_start_locked(struct ifnet *ifp)
95846492Swpaul{
959109323Ssam	struct wi_softc	*sc = ifp->if_softc;
960119150Ssam	struct ieee80211_node *ni;
961109323Ssam	struct ieee80211_frame *wh;
962109323Ssam	struct mbuf *m0;
963178354Ssam	struct ieee80211_key *k;
964109323Ssam	struct wi_frame frmhdr;
965190579Ssam	const struct llc *llc;
966160991Ssam	int cur;
96746492Swpaul
968165087Ssam	WI_LOCK_ASSERT(sc);
96946492Swpaul
970165087Ssam	if (sc->wi_gone)
971109323Ssam		return;
97246492Swpaul
973109323Ssam	memset(&frmhdr, 0, sizeof(frmhdr));
974109323Ssam	cur = sc->sc_txnext;
975109323Ssam	for (;;) {
976178354Ssam		IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
977178354Ssam		if (m0 == NULL)
978178354Ssam			break;
979178354Ssam		if (sc->sc_txd[cur].d_len != 0) {
980178354Ssam			IFQ_DRV_PREPEND(&ifp->if_snd, m0);
981178354Ssam			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
982178354Ssam			break;
983178354Ssam		}
984178354Ssam		ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
98546492Swpaul
986190579Ssam		/* reconstruct 802.3 header */
987178354Ssam		wh = mtod(m0, struct ieee80211_frame *);
988190579Ssam		switch (wh->i_fc[1]) {
989190579Ssam		case IEEE80211_FC1_DIR_TODS:
990190579Ssam			IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost,
991190579Ssam			    wh->i_addr2);
992190579Ssam			IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost,
993190579Ssam			    wh->i_addr3);
994190579Ssam			break;
995190579Ssam		case IEEE80211_FC1_DIR_NODS:
996190579Ssam			IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost,
997190579Ssam			    wh->i_addr2);
998190579Ssam			IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost,
999190579Ssam			    wh->i_addr1);
1000190579Ssam			break;
1001190579Ssam		case IEEE80211_FC1_DIR_FROMDS:
1002190579Ssam			IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost,
1003190579Ssam			    wh->i_addr3);
1004190579Ssam			IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost,
1005190579Ssam			    wh->i_addr1);
1006190579Ssam			break;
1007190579Ssam		}
1008190579Ssam		llc = (const struct llc *)(
1009190579Ssam		    mtod(m0, const uint8_t *) + ieee80211_hdrsize(wh));
1010190579Ssam		frmhdr.wi_ehdr.ether_type = llc->llc_snap.ether_type;
1011109323Ssam		frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
1012260444Skevlo		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1013178354Ssam			k = ieee80211_crypto_encap(ni, m0);
1014138571Ssam			if (k == NULL) {
1015170530Ssam				ieee80211_free_node(ni);
1016143299Ssam				m_freem(m0);
1017109323Ssam				continue;
1018109323Ssam			}
1019138952Ssam			frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
1020109323Ssam		}
1021178354Ssam
1022192468Ssam		if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
1023178354Ssam			sc->sc_tx_th.wt_rate = ni->ni_txrate;
1024192468Ssam			ieee80211_radiotap_tx(ni->ni_vap, m0);
1025109323Ssam		}
1026178354Ssam
1027127697Ssam		m_copydata(m0, 0, sizeof(struct ieee80211_frame),
1028127697Ssam		    (caddr_t)&frmhdr.wi_whdr);
1029127697Ssam		m_adj(m0, sizeof(struct ieee80211_frame));
1030127697Ssam		frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
1031170530Ssam		ieee80211_free_node(ni);
1032160991Ssam		if (wi_start_tx(ifp, &frmhdr, m0))
1033109323Ssam			continue;
1034178354Ssam
1035160991Ssam		sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
1036272068Sglebius		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1037160991Ssam	}
1038165087Ssam}
1039160991Ssam
1040165087Ssamstatic void
1041165087Ssamwi_start(struct ifnet *ifp)
1042165087Ssam{
1043165087Ssam	struct wi_softc	*sc = ifp->if_softc;
1044165087Ssam
1045165087Ssam	WI_LOCK(sc);
1046165087Ssam	wi_start_locked(ifp);
1047160991Ssam	WI_UNLOCK(sc);
1048160991Ssam}
1049160991Ssam
1050160991Ssamstatic int
1051160991Ssamwi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr, struct mbuf *m0)
1052160991Ssam{
1053160991Ssam	struct wi_softc	*sc = ifp->if_softc;
1054160991Ssam	int cur = sc->sc_txnext;
1055160991Ssam	int fid, off, error;
1056160991Ssam
1057160991Ssam	fid = sc->sc_txd[cur].d_fid;
1058160991Ssam	off = sizeof(*frmhdr);
1059160991Ssam	error = wi_write_bap(sc, fid, 0, frmhdr, sizeof(*frmhdr)) != 0
1060160991Ssam	     || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
1061160991Ssam	m_freem(m0);
1062160991Ssam	if (error) {
1063272068Sglebius		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1064160991Ssam		return -1;
1065160991Ssam	}
1066160991Ssam	sc->sc_txd[cur].d_len = off;
1067160991Ssam	if (sc->sc_txcur == cur) {
1068160991Ssam		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
1069160991Ssam			if_printf(ifp, "xmit failed\n");
1070160991Ssam			sc->sc_txd[cur].d_len = 0;
1071160991Ssam			return -1;
1072109323Ssam		}
1073160991Ssam		sc->sc_tx_timer = 5;
1074160991Ssam	}
1075160991Ssam	return 0;
1076160991Ssam}
1077160991Ssam
1078160991Ssamstatic int
1079160991Ssamwi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m0,
1080160991Ssam	    const struct ieee80211_bpf_params *params)
1081160991Ssam{
1082160991Ssam	struct ieee80211com *ic = ni->ni_ic;
1083160991Ssam	struct ifnet *ifp = ic->ic_ifp;
1084192468Ssam	struct ieee80211vap *vap = ni->ni_vap;
1085160991Ssam	struct wi_softc	*sc = ifp->if_softc;
1086178354Ssam	struct ieee80211_key *k;
1087160991Ssam	struct ieee80211_frame *wh;
1088160991Ssam	struct wi_frame frmhdr;
1089160991Ssam	int cur;
1090160991Ssam	int rc = 0;
1091160991Ssam
1092160991Ssam	WI_LOCK(sc);
1093160991Ssam
1094160991Ssam	if (sc->wi_gone) {
1095160991Ssam		rc = ENETDOWN;
1096160991Ssam		goto out;
1097160991Ssam	}
1098160991Ssam	memset(&frmhdr, 0, sizeof(frmhdr));
1099160991Ssam	cur = sc->sc_txnext;
1100160991Ssam	if (sc->sc_txd[cur].d_len != 0) {
1101160991Ssam		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1102160991Ssam		rc = ENOBUFS;
1103160991Ssam		goto out;
1104160991Ssam	}
1105160991Ssam	m0->m_pkthdr.rcvif = NULL;
1106160991Ssam
1107160991Ssam	m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
1108160991Ssam	    (caddr_t)&frmhdr.wi_ehdr);
1109160991Ssam	frmhdr.wi_ehdr.ether_type = 0;
1110160991Ssam	wh = mtod(m0, struct ieee80211_frame *);
1111160991Ssam
1112160991Ssam	frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
1113160991Ssam	if (params && (params->ibp_flags & IEEE80211_BPF_NOACK))
1114160991Ssam		frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_ALTRTRY);
1115260444Skevlo	if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
1116178354Ssam	    (!params || (params && (params->ibp_flags & IEEE80211_BPF_CRYPTO)))) {
1117178354Ssam		k = ieee80211_crypto_encap(ni, m0);
1118178354Ssam		if (k == NULL) {
1119178354Ssam			rc = ENOMEM;
1120178354Ssam			goto out;
1121109323Ssam		}
1122178354Ssam		frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
112374838Salfred	}
1124192468Ssam	if (ieee80211_radiotap_active_vap(vap)) {
1125178354Ssam		sc->sc_tx_th.wt_rate = ni->ni_txrate;
1126192468Ssam		ieee80211_radiotap_tx(vap, m0);
1127160991Ssam	}
1128160991Ssam	m_copydata(m0, 0, sizeof(struct ieee80211_frame),
1129160991Ssam	    (caddr_t)&frmhdr.wi_whdr);
1130160991Ssam	m_adj(m0, sizeof(struct ieee80211_frame));
1131160991Ssam	frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
1132168860Ssephe	if (wi_start_tx(ifp, &frmhdr, m0) < 0) {
1133168860Ssephe		m0 = NULL;
1134168860Ssephe		rc = EIO;
1135160991Ssam		goto out;
1136168860Ssephe	}
1137168860Ssephe	m0 = NULL;
113846492Swpaul
1139160991Ssam	sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
1140160991Ssamout:
1141109323Ssam	WI_UNLOCK(sc);
1142160991Ssam
1143168860Ssephe	if (m0 != NULL)
1144168860Ssephe		m_freem(m0);
1145168860Ssephe	ieee80211_free_node(ni);
1146160991Ssam	return rc;
114746492Swpaul}
114846492Swpaul
114988546Salfredstatic int
1150178354Ssamwi_reset(struct wi_softc *sc)
115146492Swpaul{
1152114124Simp#define WI_INIT_TRIES 3
1153178354Ssam	int i, error = 0;
1154112362Simp
1155178354Ssam	for (i = 0; i < WI_INIT_TRIES; i++) {
1156178354Ssam		error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0);
1157178354Ssam		if (error == 0)
115846492Swpaul			break;
1159109323Ssam		DELAY(WI_DELAY * 1000);
116046492Swpaul	}
1161114124Simp	sc->sc_reset = 1;
1162178354Ssam	if (i == WI_INIT_TRIES) {
1163178354Ssam		if_printf(sc->sc_ifp, "reset failed\n");
1164178354Ssam		return error;
116575373Salfred	}
116646492Swpaul
1167109323Ssam	CSR_WRITE_2(sc, WI_INT_EN, 0);
1168114124Simp	CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
116946492Swpaul
1170109323Ssam	/* Calibrate timer. */
1171114124Simp	wi_write_val(sc, WI_RID_TICK_TIME, 8);
1172114124Simp
1173178354Ssam	return 0;
1174109323Ssam#undef WI_INIT_TRIES
117546492Swpaul}
117646492Swpaul
117788546Salfredstatic void
1178165089Ssamwi_watchdog(void *arg)
117946492Swpaul{
1180165089Ssam	struct wi_softc	*sc = arg;
1181165089Ssam	struct ifnet *ifp = sc->sc_ifp;
118246492Swpaul
1183178354Ssam	WI_LOCK_ASSERT(sc);
1184178354Ssam
1185109323Ssam	if (!sc->sc_enabled)
1186109323Ssam		return;
118746492Swpaul
1188178354Ssam	if (sc->sc_tx_timer && --sc->sc_tx_timer == 0) {
1189178354Ssam		if_printf(ifp, "device timeout\n");
1190272068Sglebius		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1191178354Ssam		wi_init_locked(ifp->if_softc);
1192178354Ssam		return;
119346492Swpaul	}
1194165089Ssam	callout_reset(&sc->sc_watchdog, hz, wi_watchdog, sc);
119546492Swpaul}
119646492Swpaul
119788546Salfredstatic int
1198109323Ssamwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
119946492Swpaul{
1200109323Ssam	struct wi_softc *sc = ifp->if_softc;
1201178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1202178354Ssam	struct ifreq *ifr = (struct ifreq *) data;
1203178354Ssam	int error = 0, startall = 0;
120446492Swpaul
1205109323Ssam	switch (cmd) {
120646492Swpaul	case SIOCSIFFLAGS:
1207178704Sthompsa		WI_LOCK(sc);
1208100876Simp		/*
1209101139Simp		 * Can't do promisc and hostap at the same time.  If all that's
1210101139Simp		 * changing is the promisc flag, try to short-circuit a call to
1211101139Simp		 * wi_init() by just setting PROMISC in the hardware.
1212100876Simp		 */
121346492Swpaul		if (ifp->if_flags & IFF_UP) {
1214109323Ssam			if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1215148887Srwatson			    ifp->if_drv_flags & IFF_DRV_RUNNING) {
1216178354Ssam				if ((ifp->if_flags ^ sc->sc_if_flags) & IFF_PROMISC) {
1217178354Ssam					wi_write_val(sc, WI_RID_PROMISC,
1218178354Ssam					    (ifp->if_flags & IFF_PROMISC) != 0);
1219101139Simp				} else {
1220178354Ssam					wi_init_locked(sc);
1221178354Ssam					startall = 1;
1222101139Simp				}
1223100876Simp			} else {
1224178354Ssam				wi_init_locked(sc);
1225178354Ssam				startall = 1;
1226100876Simp			}
122746492Swpaul		} else {
1228178354Ssam			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1229178354Ssam				wi_stop_locked(sc, 1);
1230123098Simp			sc->wi_gone = 0;
123146492Swpaul		}
1232109323Ssam		sc->sc_if_flags = ifp->if_flags;
1233178704Sthompsa		WI_UNLOCK(sc);
1234178704Sthompsa		if (startall)
1235178704Sthompsa			ieee80211_start_all(ic);
123646492Swpaul		break;
1237178354Ssam	case SIOCGIFMEDIA:
1238178354Ssam		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
123946492Swpaul		break;
1240178704Sthompsa	case SIOCGIFADDR:
1241178354Ssam		error = ether_ioctl(ifp, cmd, data);
1242170530Ssam		break;
1243178704Sthompsa	default:
1244178704Sthompsa		error = EINVAL;
1245178704Sthompsa		break;
1246170530Ssam	}
1247109323Ssam	return error;
1248109323Ssam}
124946492Swpaul
1250109323Ssamstatic void
1251109323Ssamwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1252109323Ssam{
1253178354Ssam	struct ieee80211vap *vap = ifp->if_softc;
1254178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1255178354Ssam	struct wi_softc *sc = ic->ic_ifp->if_softc;
1256109323Ssam	u_int16_t val;
1257109323Ssam	int rate, len;
125846492Swpaul
1259109323Ssam	len = sizeof(val);
1260178354Ssam	if (sc->sc_enabled &&
1261178354Ssam	    wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) == 0 &&
1262138571Ssam	    len == sizeof(val)) {
1263109323Ssam		/* convert to 802.11 rate */
1264138571Ssam		val = le16toh(val);
1265109323Ssam		rate = val * 2;
1266109323Ssam		if (sc->sc_firmware_type == WI_LUCENT) {
1267138571Ssam			if (rate == 10)
1268109323Ssam				rate = 11;	/* 5.5Mbps */
1269109323Ssam		} else {
1270109323Ssam			if (rate == 4*2)
1271109323Ssam				rate = 11;	/* 5.5Mbps */
1272109323Ssam			else if (rate == 8*2)
1273109323Ssam				rate = 22;	/* 11Mbps */
1274109323Ssam		}
1275178354Ssam		vap->iv_bss->ni_txrate = rate;
1276109323Ssam	}
1277178354Ssam	ieee80211_media_status(ifp, imr);
1278109323Ssam}
127946492Swpaul
1280109323Ssamstatic void
1281109323Ssamwi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1282109323Ssam{
1283147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1284178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1285178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1286178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
128798440Simp
1288109323Ssam	if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1289109323Ssam		return;
129046492Swpaul
1291109323Ssam	DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid)));
1292109323Ssam	DPRINTF(("%s ?\n", ether_sprintf(new_bssid)));
129346492Swpaul
1294109323Ssam	/* In promiscuous mode, the BSSID field is not a reliable
1295109323Ssam	 * indicator of the firmware's BSSID. Damp spurious
1296109323Ssam	 * change-of-BSSID indications.
1297109323Ssam	 */
1298109323Ssam	if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1299138571Ssam	    !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns,
1300138571Ssam	                 WI_MAX_FALSE_SYNS))
1301109323Ssam		return;
130246492Swpaul
1303139542Ssam	sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1);
1304170530Ssam#if 0
1305139542Ssam	/*
1306139542Ssam	 * XXX hack; we should create a new node with the new bssid
1307139542Ssam	 * and replace the existing ic_bss with it but since we don't
1308139542Ssam	 * process management frames to collect state we cheat by
1309139542Ssam	 * reusing the existing node as we know wi_newstate will be
1310139542Ssam	 * called and it will overwrite the node state.
1311139542Ssam	 */
1312139542Ssam	ieee80211_sta_join(ic, ieee80211_ref_node(ni));
1313170530Ssam#endif
1314109323Ssam}
131546492Swpaul
1316178354Ssamstatic __noinline void
1317109323Ssamwi_rx_intr(struct wi_softc *sc)
1318109323Ssam{
1319147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1320178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1321109323Ssam	struct wi_frame frmhdr;
1322109323Ssam	struct mbuf *m;
1323109323Ssam	struct ieee80211_frame *wh;
1324119150Ssam	struct ieee80211_node *ni;
1325192468Ssam	int fid, len, off;
1326109323Ssam	u_int8_t dir;
1327109323Ssam	u_int16_t status;
1328192468Ssam	int8_t rssi, nf;
132946611Swpaul
1330109323Ssam	fid = CSR_READ_2(sc, WI_RX_FID);
133146611Swpaul
1332109323Ssam	/* First read in the frame header */
1333109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1334109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1335272068Sglebius		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1336109323Ssam		DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1337109323Ssam		return;
1338109323Ssam	}
133991695Simp
1340109323Ssam	/*
1341109323Ssam	 * Drop undecryptable or packets with receive errors here
1342109323Ssam	 */
1343109323Ssam	status = le16toh(frmhdr.wi_status);
1344109323Ssam	if (status & WI_STAT_ERRSTAT) {
1345109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1346272068Sglebius		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1347109323Ssam		DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1348109323Ssam		return;
1349109323Ssam	}
135046492Swpaul
1351109323Ssam	len = le16toh(frmhdr.wi_dat_len);
1352109323Ssam	off = ALIGN(sizeof(struct ieee80211_frame));
135346563Swpaul
1354117855Ssam	/*
1355117855Ssam	 * Sometimes the PRISM2.x returns bogusly large frames. Except
1356117855Ssam	 * in monitor mode, just throw them away.
1357117855Ssam	 */
1358117855Ssam	if (off + len > MCLBYTES) {
1359117855Ssam		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1360117855Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1361272068Sglebius			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1362117855Ssam			DPRINTF(("wi_rx_intr: oversized packet\n"));
1363117855Ssam			return;
1364117855Ssam		} else
1365117855Ssam			len = 0;
1366117855Ssam	}
1367117855Ssam
1368178354Ssam	if (off + len > MHLEN)
1369243857Sglebius		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1370178354Ssam	else
1371243857Sglebius		m = m_gethdr(M_NOWAIT, MT_DATA);
1372109323Ssam	if (m == NULL) {
1373109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1374272068Sglebius		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1375109323Ssam		DPRINTF(("wi_rx_intr: MGET failed\n"));
1376109323Ssam		return;
1377109323Ssam	}
1378109323Ssam	m->m_data += off - sizeof(struct ieee80211_frame);
1379109323Ssam	memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1380109323Ssam	wi_read_bap(sc, fid, sizeof(frmhdr),
1381109323Ssam	    m->m_data + sizeof(struct ieee80211_frame), len);
1382109323Ssam	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1383109323Ssam	m->m_pkthdr.rcvif = ifp;
138494405Simp
1385109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
138646492Swpaul
1387192468Ssam	rssi = frmhdr.wi_rx_signal;
1388192468Ssam	nf = frmhdr.wi_rx_silence;
1389192468Ssam	if (ieee80211_radiotap_active(ic)) {
1390192468Ssam		struct wi_rx_radiotap_header *tap = &sc->sc_rx_th;
1391192468Ssam		uint32_t rstamp;
1392192468Ssam
1393192468Ssam		rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1394192468Ssam		    le16toh(frmhdr.wi_rx_tstamp1);
1395192517Ssam		tap->wr_tsf = htole64((uint64_t)rstamp);
1396123927Ssam		/* XXX replace divide by table */
1397192468Ssam		tap->wr_rate = frmhdr.wi_rx_rate / 5;
1398192468Ssam		tap->wr_flags = 0;
1399123927Ssam		if (frmhdr.wi_status & WI_STAT_PCF)
1400192468Ssam			tap->wr_flags |= IEEE80211_RADIOTAP_F_CFP;
1401178354Ssam		if (m->m_flags & M_WEP)
1402192468Ssam			tap->wr_flags |= IEEE80211_RADIOTAP_F_WEP;
1403192468Ssam		tap->wr_antsignal = rssi;
1404192468Ssam		tap->wr_antnoise = nf;
140556965Swpaul	}
140656965Swpaul
1407109323Ssam	/* synchronize driver's BSSID with firmware's BSSID */
1408178354Ssam	wh = mtod(m, struct ieee80211_frame *);
1409109323Ssam	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1410109323Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1411109323Ssam		wi_sync_bssid(sc, wh->i_addr3);
141246492Swpaul
1413165088Ssam	WI_UNLOCK(sc);
1414165088Ssam
1415178354Ssam	ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1416178354Ssam	if (ni != NULL) {
1417192468Ssam		(void) ieee80211_input(ni, m, rssi, nf);
1418178354Ssam		ieee80211_free_node(ni);
1419178354Ssam	} else
1420192468Ssam		(void) ieee80211_input_all(ic, m, rssi, nf);
1421178354Ssam
1422165088Ssam	WI_LOCK(sc);
1423109323Ssam}
142446492Swpaul
1425178354Ssamstatic __noinline void
1426109323Ssamwi_tx_ex_intr(struct wi_softc *sc)
1427109323Ssam{
1428147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1429109323Ssam	struct wi_frame frmhdr;
1430109323Ssam	int fid;
143146492Swpaul
1432109323Ssam	fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1433109323Ssam	/* Read in the frame header */
1434109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1435109323Ssam		u_int16_t status = le16toh(frmhdr.wi_status);
1436109323Ssam		/*
1437109323Ssam		 * Spontaneous station disconnects appear as xmit
1438109323Ssam		 * errors.  Don't announce them and/or count them
1439109323Ssam		 * as an output error.
1440109323Ssam		 */
1441109323Ssam		if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1442109323Ssam			if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1443109323Ssam				if_printf(ifp, "tx failed");
1444109323Ssam				if (status & WI_TXSTAT_RET_ERR)
1445109323Ssam					printf(", retry limit exceeded");
1446109323Ssam				if (status & WI_TXSTAT_AGED_ERR)
1447109323Ssam					printf(", max transmit lifetime exceeded");
1448109323Ssam				if (status & WI_TXSTAT_DISCONNECT)
1449109323Ssam					printf(", port disconnected");
1450109323Ssam				if (status & WI_TXSTAT_FORM_ERR)
1451109323Ssam					printf(", invalid format (data len %u src %6D)",
1452109323Ssam						le16toh(frmhdr.wi_dat_len),
1453109323Ssam						frmhdr.wi_ehdr.ether_shost, ":");
1454109323Ssam				if (status & ~0xf)
1455109323Ssam					printf(", status=0x%x", status);
1456109323Ssam				printf("\n");
1457109323Ssam			}
1458272068Sglebius			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1459272068Sglebius		} else
1460109323Ssam			DPRINTF(("port disconnected\n"));
1461109323Ssam	} else
1462109323Ssam		DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
1463109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1464109323Ssam}
146546492Swpaul
1466178354Ssamstatic __noinline void
1467109323Ssamwi_tx_intr(struct wi_softc *sc)
1468109323Ssam{
1469147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1470109323Ssam	int fid, cur;
147194405Simp
1472123098Simp	if (sc->wi_gone)
1473123098Simp		return;
1474123098Simp
1475109323Ssam	fid = CSR_READ_2(sc, WI_ALLOC_FID);
1476109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
147746492Swpaul
1478109323Ssam	cur = sc->sc_txcur;
1479109323Ssam	if (sc->sc_txd[cur].d_fid != fid) {
1480109323Ssam		if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
1481109323Ssam		    fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
1482109323Ssam		return;
1483109323Ssam	}
1484109323Ssam	sc->sc_tx_timer = 0;
1485109323Ssam	sc->sc_txd[cur].d_len = 0;
1486112363Simp	sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
1487109323Ssam	if (sc->sc_txd[cur].d_len == 0)
1488148887Srwatson		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1489109323Ssam	else {
1490109323Ssam		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1491109323Ssam		    0, 0)) {
1492109323Ssam			if_printf(ifp, "xmit failed\n");
1493109323Ssam			sc->sc_txd[cur].d_len = 0;
1494109323Ssam		} else {
1495109323Ssam			sc->sc_tx_timer = 5;
1496109323Ssam		}
1497109323Ssam	}
149846492Swpaul}
149946492Swpaul
1500178354Ssamstatic __noinline void
1501109323Ssamwi_info_intr(struct wi_softc *sc)
150294405Simp{
1503272068Sglebius	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
1504178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1505109323Ssam	int i, fid, len, off;
1506109323Ssam	u_int16_t ltbuf[2];
1507109323Ssam	u_int16_t stat;
1508109323Ssam	u_int32_t *ptr;
150994405Simp
1510109323Ssam	fid = CSR_READ_2(sc, WI_INFO_FID);
1511109323Ssam	wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
151294405Simp
1513109323Ssam	switch (le16toh(ltbuf[1])) {
1514109323Ssam	case WI_INFO_LINK_STAT:
1515109323Ssam		wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1516109323Ssam		DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1517232147Sadrian
1518232147Sadrian		if (vap == NULL)
1519232147Sadrian			goto finish;
1520232147Sadrian
1521109323Ssam		switch (le16toh(stat)) {
1522109323Ssam		case WI_INFO_LINK_STAT_CONNECTED:
1523178354Ssam			if (vap->iv_state == IEEE80211_S_RUN &&
1524178354Ssam			    vap->iv_opmode != IEEE80211_M_IBSS)
1525109323Ssam				break;
1526178354Ssam			/* fall thru... */
1527109323Ssam		case WI_INFO_LINK_STAT_AP_CHG:
1528191746Sthompsa			IEEE80211_LOCK(ic);
1529191746Sthompsa			vap->iv_bss->ni_associd = 1 | 0xc000;	/* NB: anything will do */
1530191746Sthompsa			ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
1531191746Sthompsa			IEEE80211_UNLOCK(ic);
1532109323Ssam			break;
1533109323Ssam		case WI_INFO_LINK_STAT_AP_INR:
1534109323Ssam			break;
1535178354Ssam		case WI_INFO_LINK_STAT_DISCONNECTED:
1536178354Ssam			/* we dropped off the net; e.g. due to deauth/disassoc */
1537191746Sthompsa			IEEE80211_LOCK(ic);
1538191746Sthompsa			vap->iv_bss->ni_associd = 0;
1539191746Sthompsa			vap->iv_stats.is_rx_deauth++;
1540191746Sthompsa			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
1541191746Sthompsa			IEEE80211_UNLOCK(ic);
1542178354Ssam			break;
1543109323Ssam		case WI_INFO_LINK_STAT_AP_OOR:
1544178354Ssam			/* XXX does this need to be per-vap? */
1545191746Sthompsa			ieee80211_beacon_miss(ic);
1546109323Ssam			break;
1547109323Ssam		case WI_INFO_LINK_STAT_ASSOC_FAILED:
1548178354Ssam			if (vap->iv_opmode == IEEE80211_M_STA)
1549191746Sthompsa				ieee80211_new_state(vap, IEEE80211_S_SCAN,
1550191746Sthompsa				    IEEE80211_SCAN_FAIL_TIMEOUT);
1551109323Ssam			break;
1552109323Ssam		}
1553109323Ssam		break;
1554109323Ssam	case WI_INFO_COUNTERS:
1555109323Ssam		/* some card versions have a larger stats structure */
1556109323Ssam		len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1557109323Ssam		ptr = (u_int32_t *)&sc->sc_stats;
1558109323Ssam		off = sizeof(ltbuf);
1559109323Ssam		for (i = 0; i < len; i++, off += 2, ptr++) {
1560109323Ssam			wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1561109323Ssam#ifdef WI_HERMES_STATS_WAR
1562109323Ssam			if (stat & 0xf000)
1563109323Ssam				stat = ~stat;
1564109323Ssam#endif
1565109323Ssam			*ptr += stat;
1566109323Ssam		}
1567109323Ssam		break;
1568109323Ssam	default:
1569109323Ssam		DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1570109323Ssam		    le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1571109323Ssam		break;
157294405Simp	}
1573232147Sadrianfinish:
1574109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1575109323Ssam}
157694405Simp
1577272068Sglebiusstatic uint64_t
1578272068Sglebiuswi_get_counter(struct ifnet *ifp, ift_counter cnt)
1579272068Sglebius{
1580272068Sglebius	struct wi_softc *sc;
1581272068Sglebius
1582272068Sglebius	sc = if_getsoftc(ifp);
1583272068Sglebius
1584272068Sglebius	switch (cnt) {
1585272068Sglebius	case IFCOUNTER_COLLISIONS:
1586272068Sglebius		return (sc->sc_stats.wi_tx_single_retries +
1587272068Sglebius		    sc->sc_stats.wi_tx_multi_retries +
1588272068Sglebius		    sc->sc_stats.wi_tx_retry_limit);
1589272068Sglebius	default:
1590272068Sglebius		return (if_get_counter_default(ifp, cnt));
1591272068Sglebius	}
1592272068Sglebius}
1593272068Sglebius
1594109323Ssamstatic int
1595109323Ssamwi_write_multi(struct wi_softc *sc)
1596109323Ssam{
1597147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1598109323Ssam	int n;
1599109323Ssam	struct ifmultiaddr *ifma;
1600109323Ssam	struct wi_mcast mlist;
160194472Simp
1602109323Ssam	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1603109323Ssamallmulti:
1604109323Ssam		memset(&mlist, 0, sizeof(mlist));
1605109323Ssam		return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1606109323Ssam		    sizeof(mlist));
160794405Simp	}
160894405Simp
1609109323Ssam	n = 0;
1610195049Srwatson	if_maddr_rlock(ifp);
1611109323Ssam	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1612109323Ssam		if (ifma->ifma_addr->sa_family != AF_LINK)
1613109323Ssam			continue;
1614109323Ssam		if (n >= 16)
1615109323Ssam			goto allmulti;
1616109323Ssam		IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1617109323Ssam		    (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1618109323Ssam		n++;
161994405Simp	}
1620195049Srwatson	if_maddr_runlock(ifp);
1621109323Ssam	return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1622109323Ssam	    IEEE80211_ADDR_LEN * n);
162394405Simp}
162494405Simp
162594405Simpstatic void
1626178354Ssamwi_update_mcast(struct ifnet *ifp)
1627178354Ssam{
1628178354Ssam	wi_write_multi(ifp->if_softc);
1629178354Ssam}
1630178354Ssam
1631178354Ssamstatic void
1632192468Ssamwi_update_promisc(struct ifnet *ifp)
1633192468Ssam{
1634192468Ssam	struct wi_softc *sc = ifp->if_softc;
1635192468Ssam	struct ieee80211com *ic = ifp->if_l2com;
1636192468Ssam
1637192468Ssam	WI_LOCK(sc);
1638192468Ssam	/* XXX handle WEP special case handling? */
1639192468Ssam	wi_write_val(sc, WI_RID_PROMISC,
1640192468Ssam	    (ic->ic_opmode == IEEE80211_M_MONITOR ||
1641192468Ssam	     (ifp->if_flags & IFF_PROMISC)));
1642192468Ssam	WI_UNLOCK(sc);
1643192468Ssam}
1644192468Ssam
1645192468Ssamstatic void
1646109323Ssamwi_read_nicid(struct wi_softc *sc)
164746492Swpaul{
1648109323Ssam	struct wi_card_ident *id;
1649109323Ssam	char *p;
1650109323Ssam	int len;
1651109323Ssam	u_int16_t ver[4];
165246492Swpaul
1653109323Ssam	/* getting chip identity */
1654109323Ssam	memset(ver, 0, sizeof(ver));
1655109323Ssam	len = sizeof(ver);
1656109323Ssam	wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
165746492Swpaul
1658109323Ssam	sc->sc_firmware_type = WI_NOTYPE;
1659180919Simp	sc->sc_nic_id = le16toh(ver[0]);
1660109323Ssam	for (id = wi_card_ident; id->card_name != NULL; id++) {
1661180919Simp		if (sc->sc_nic_id == id->card_id) {
1662180919Simp			sc->sc_nic_name = id->card_name;
1663109323Ssam			sc->sc_firmware_type = id->firm_type;
1664109323Ssam			break;
1665109323Ssam		}
166667092Swpaul	}
1667109323Ssam	if (sc->sc_firmware_type == WI_NOTYPE) {
1668180919Simp		if (sc->sc_nic_id & 0x8000) {
1669109323Ssam			sc->sc_firmware_type = WI_INTERSIL;
1670180919Simp			sc->sc_nic_name = "Unknown Prism chip";
1671109323Ssam		} else {
1672109323Ssam			sc->sc_firmware_type = WI_LUCENT;
1673180919Simp			sc->sc_nic_name = "Unknown Lucent chip";
1674109323Ssam		}
167567092Swpaul	}
1676181210Simp	if (bootverbose)
1677181210Simp		device_printf(sc->sc_dev, "using %s\n", sc->sc_nic_name);
167846492Swpaul
1679109323Ssam	/* get primary firmware version (Only Prism chips) */
1680109323Ssam	if (sc->sc_firmware_type != WI_LUCENT) {
1681109323Ssam		memset(ver, 0, sizeof(ver));
1682109323Ssam		len = sizeof(ver);
1683109323Ssam		wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1684109323Ssam		sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1685109323Ssam		    le16toh(ver[3]) * 100 + le16toh(ver[1]);
168667092Swpaul	}
168746492Swpaul
1688109323Ssam	/* get station firmware version */
1689109323Ssam	memset(ver, 0, sizeof(ver));
1690109323Ssam	len = sizeof(ver);
1691109323Ssam	wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1692109323Ssam	sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1693109323Ssam	    le16toh(ver[3]) * 100 + le16toh(ver[1]);
1694109323Ssam	if (sc->sc_firmware_type == WI_INTERSIL &&
1695109323Ssam	    (sc->sc_sta_firmware_ver == 10102 ||
1696109323Ssam	     sc->sc_sta_firmware_ver == 20102)) {
1697109323Ssam		char ident[12];
1698109323Ssam		memset(ident, 0, sizeof(ident));
1699109323Ssam		len = sizeof(ident);
1700109323Ssam		/* value should be the format like "V2.00-11" */
1701109323Ssam		if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1702109323Ssam		    *(p = (char *)ident) >= 'A' &&
1703109323Ssam		    p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1704109323Ssam			sc->sc_firmware_type = WI_SYMBOL;
1705109323Ssam			sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1706109323Ssam			    (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1707109323Ssam			    (p[6] - '0') * 10 + (p[7] - '0');
170894405Simp		}
170994405Simp	}
1710180919Simp	if (bootverbose) {
1711180919Simp		device_printf(sc->sc_dev, "%s Firmware: ",
1712180919Simp		    wi_firmware_names[sc->sc_firmware_type]);
1713180919Simp		if (sc->sc_firmware_type != WI_LUCENT)	/* XXX */
1714180919Simp			printf("Primary (%u.%u.%u), ",
1715180919Simp			    sc->sc_pri_firmware_ver / 10000,
1716180919Simp			    (sc->sc_pri_firmware_ver % 10000) / 100,
1717180919Simp			    sc->sc_pri_firmware_ver % 100);
1718180919Simp		printf("Station (%u.%u.%u)\n",
1719180919Simp		    sc->sc_sta_firmware_ver / 10000,
1720180919Simp		    (sc->sc_sta_firmware_ver % 10000) / 100,
1721180919Simp		    sc->sc_sta_firmware_ver % 100);
1722180919Simp	}
1723109323Ssam}
172446492Swpaul
1725109323Ssamstatic int
1726109323Ssamwi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1727109323Ssam{
1728109323Ssam	struct wi_ssid ssid;
172946492Swpaul
1730109323Ssam	if (buflen > IEEE80211_NWID_LEN)
1731109323Ssam		return ENOBUFS;
1732109323Ssam	memset(&ssid, 0, sizeof(ssid));
1733109323Ssam	ssid.wi_len = htole16(buflen);
1734109323Ssam	memcpy(ssid.wi_ssid, buf, buflen);
1735109323Ssam	return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1736109323Ssam}
173746492Swpaul
1738109323Ssamstatic int
1739178354Ssamwi_write_txrate(struct wi_softc *sc, struct ieee80211vap *vap)
1740109323Ssam{
1741178354Ssam	static const uint16_t lucent_rates[12] = {
1742178354Ssam	    [ 0] = 3,	/* auto */
1743178354Ssam	    [ 1] = 1,	/* 1Mb/s */
1744178354Ssam	    [ 2] = 2,	/* 2Mb/s */
1745178354Ssam	    [ 5] = 4,	/* 5.5Mb/s */
1746178354Ssam	    [11] = 5	/* 11Mb/s */
1747178354Ssam	};
1748178354Ssam	static const uint16_t intersil_rates[12] = {
1749178354Ssam	    [ 0] = 0xf,	/* auto */
1750178354Ssam	    [ 1] = 0,	/* 1Mb/s */
1751178354Ssam	    [ 2] = 1,	/* 2Mb/s */
1752178354Ssam	    [ 5] = 2,	/* 5.5Mb/s */
1753178354Ssam	    [11] = 3,	/* 11Mb/s */
1754178354Ssam	};
1755178354Ssam	const uint16_t *rates = sc->sc_firmware_type == WI_LUCENT ?
1756178354Ssam	    lucent_rates : intersil_rates;
1757178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1758178354Ssam	const struct ieee80211_txparam *tp;
175946492Swpaul
1760178354Ssam	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
1761178354Ssam	return wi_write_val(sc, WI_RID_TX_RATE,
1762178354Ssam	    (tp->ucastrate == IEEE80211_FIXED_RATE_NONE ?
1763178354Ssam		rates[0] : rates[tp->ucastrate / 2]));
176446492Swpaul}
176546492Swpaul
1766109323Ssamstatic int
1767178354Ssamwi_write_wep(struct wi_softc *sc, struct ieee80211vap *vap)
176846492Swpaul{
1769109323Ssam	int error = 0;
1770109323Ssam	int i, keylen;
1771109323Ssam	u_int16_t val;
1772109323Ssam	struct wi_key wkey[IEEE80211_WEP_NKID];
177346492Swpaul
1774109323Ssam	switch (sc->sc_firmware_type) {
1775109323Ssam	case WI_LUCENT:
1776178354Ssam		val = (vap->iv_flags & IEEE80211_F_PRIVACY) ? 1 : 0;
1777109323Ssam		error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
1778109323Ssam		if (error)
1779109323Ssam			break;
1780178354Ssam		if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0)
1781138988Smdodd			break;
1782178354Ssam		error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, vap->iv_def_txkey);
1783138988Smdodd		if (error)
1784138988Smdodd			break;
1785138988Smdodd		memset(wkey, 0, sizeof(wkey));
1786138988Smdodd		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1787178354Ssam			keylen = vap->iv_nw_keys[i].wk_keylen;
1788138988Smdodd			wkey[i].wi_keylen = htole16(keylen);
1789178354Ssam			memcpy(wkey[i].wi_keydat, vap->iv_nw_keys[i].wk_key,
1790138988Smdodd			    keylen);
1791109323Ssam		}
1792138988Smdodd		error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
1793138988Smdodd		    wkey, sizeof(wkey));
1794150798Savatar		sc->sc_encryption = 0;
1795109323Ssam		break;
1796109323Ssam
1797109323Ssam	case WI_INTERSIL:
1798178354Ssam		val = HOST_ENCRYPT | HOST_DECRYPT;
1799178354Ssam		if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1800109323Ssam			/*
1801109323Ssam			 * ONLY HWB3163 EVAL-CARD Firmware version
1802109323Ssam			 * less than 0.8 variant2
1803109323Ssam			 *
1804109323Ssam			 *   If promiscuous mode disable, Prism2 chip
1805109323Ssam			 *  does not work with WEP .
1806109323Ssam			 * It is under investigation for details.
1807109323Ssam			 * (ichiro@netbsd.org)
1808109323Ssam			 */
1809178354Ssam			if (sc->sc_sta_firmware_ver < 802 ) {
1810109323Ssam				/* firm ver < 0.8 variant 2 */
1811109323Ssam				wi_write_val(sc, WI_RID_PROMISC, 1);
1812109323Ssam			}
1813109323Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE,
1814178354Ssam			    vap->iv_bss->ni_authmode);
1815178354Ssam			val |= PRIVACY_INVOKED;
1816109323Ssam		} else {
1817178354Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE, IEEE80211_AUTH_OPEN);
1818109323Ssam		}
1819109323Ssam		error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
1820109323Ssam		if (error)
1821109323Ssam			break;
1822150798Savatar		sc->sc_encryption = val;
1823138988Smdodd		if ((val & PRIVACY_INVOKED) == 0)
1824138988Smdodd			break;
1825178354Ssam		error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY, vap->iv_def_txkey);
1826109323Ssam		break;
1827109323Ssam	}
1828109323Ssam	return error;
182946492Swpaul}
183046492Swpaul
1831192492Simpstatic int
1832109323Ssamwi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
183346492Swpaul{
1834178354Ssam	int i, s = 0;
1835178354Ssam
1836123098Simp	if (sc->wi_gone)
1837123098Simp		return (ENODEV);
1838123098Simp
1839109323Ssam	/* wait for the busy bit to clear */
1840123339Simp	for (i = sc->wi_cmd_count; i > 0; i--) {	/* 500ms */
1841116206Simp		if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
1842109323Ssam			break;
1843123098Simp		DELAY(1*1000);	/* 1ms */
1844109323Ssam	}
1845109323Ssam	if (i == 0) {
1846178354Ssam		device_printf(sc->sc_dev, "%s: busy bit won't clear, cmd 0x%x\n",
1847178354Ssam		   __func__, cmd);
1848123098Simp		sc->wi_gone = 1;
1849109323Ssam		return(ETIMEDOUT);
1850109323Ssam	}
185190580Sbrooks
1852109323Ssam	CSR_WRITE_2(sc, WI_PARAM0, val0);
1853109323Ssam	CSR_WRITE_2(sc, WI_PARAM1, val1);
1854109323Ssam	CSR_WRITE_2(sc, WI_PARAM2, val2);
1855109323Ssam	CSR_WRITE_2(sc, WI_COMMAND, cmd);
185690580Sbrooks
1857109323Ssam	if (cmd == WI_CMD_INI) {
1858109323Ssam		/* XXX: should sleep here. */
1859116206Simp		DELAY(100*1000);		/* 100ms delay for init */
1860109323Ssam	}
1861109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
1862109323Ssam		/*
1863109323Ssam		 * Wait for 'command complete' bit to be
1864109323Ssam		 * set in the event status register.
1865109323Ssam		 */
1866109323Ssam		s = CSR_READ_2(sc, WI_EVENT_STAT);
1867109323Ssam		if (s & WI_EV_CMD) {
1868109323Ssam			/* Ack the event and read result code. */
1869109323Ssam			s = CSR_READ_2(sc, WI_STATUS);
1870109323Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
1871109323Ssam			if (s & WI_STAT_CMD_RESULT) {
1872109323Ssam				return(EIO);
1873109323Ssam			}
1874109323Ssam			break;
187590580Sbrooks		}
1876109323Ssam		DELAY(WI_DELAY);
1877109323Ssam	}
187890580Sbrooks
1879109323Ssam	if (i == WI_TIMEOUT) {
1880178354Ssam		device_printf(sc->sc_dev, "%s: timeout on cmd 0x%04x; "
1881178354Ssam		    "event status 0x%04x\n", __func__, cmd, s);
1882123098Simp		if (s == 0xffff)
1883123098Simp			sc->wi_gone = 1;
1884109323Ssam		return(ETIMEDOUT);
188553702Swpaul	}
1886109323Ssam	return (0);
1887109323Ssam}
188853702Swpaul
1889109323Ssamstatic int
1890109323Ssamwi_seek_bap(struct wi_softc *sc, int id, int off)
1891109323Ssam{
1892109323Ssam	int i, status;
189390580Sbrooks
1894109323Ssam	CSR_WRITE_2(sc, WI_SEL0, id);
1895109323Ssam	CSR_WRITE_2(sc, WI_OFF0, off);
189690580Sbrooks
1897109323Ssam	for (i = 0; ; i++) {
1898109323Ssam		status = CSR_READ_2(sc, WI_OFF0);
1899109323Ssam		if ((status & WI_OFF_BUSY) == 0)
1900109323Ssam			break;
1901109323Ssam		if (i == WI_TIMEOUT) {
1902178354Ssam			device_printf(sc->sc_dev, "%s: timeout, id %x off %x\n",
1903178354Ssam			    __func__, id, off);
1904109323Ssam			sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
1905123098Simp			if (status == 0xffff)
1906123098Simp				sc->wi_gone = 1;
1907109323Ssam			return ETIMEDOUT;
1908109323Ssam		}
1909109323Ssam		DELAY(1);
191053702Swpaul	}
1911109323Ssam	if (status & WI_OFF_ERR) {
1912178354Ssam		device_printf(sc->sc_dev, "%s: error, id %x off %x\n",
1913178354Ssam		    __func__, id, off);
1914109323Ssam		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
1915109323Ssam		return EIO;
1916109323Ssam	}
1917109323Ssam	sc->sc_bap_id = id;
1918109323Ssam	sc->sc_bap_off = off;
1919109323Ssam	return 0;
192053702Swpaul}
192153702Swpaul
1922109323Ssamstatic int
1923109323Ssamwi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
192453702Swpaul{
1925253756Sjhibbits	int error, cnt;
192653702Swpaul
1927109323Ssam	if (buflen == 0)
1928109323Ssam		return 0;
1929109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1930109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
1931109323Ssam			return error;
193275219Salfred	}
1933109323Ssam	cnt = (buflen + 1) / 2;
1934253756Sjhibbits	CSR_READ_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
1935109323Ssam	sc->sc_bap_off += cnt * 2;
1936109323Ssam	return 0;
193753702Swpaul}
193853702Swpaul
1939109323Ssamstatic int
1940109323Ssamwi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
194153702Swpaul{
1942253756Sjhibbits	int error, cnt;
194346492Swpaul
1944109323Ssam	if (buflen == 0)
1945109323Ssam		return 0;
194646492Swpaul
1947109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1948109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
1949109323Ssam			return error;
1950109323Ssam	}
1951109323Ssam	cnt = (buflen + 1) / 2;
1952253756Sjhibbits	CSR_WRITE_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
1953109323Ssam	sc->sc_bap_off += cnt * 2;
1954109323Ssam
1955109323Ssam	return 0;
195646492Swpaul}
195753702Swpaul
1958109323Ssamstatic int
1959109323Ssamwi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
1960109323Ssam{
1961109323Ssam	int error, len;
1962109323Ssam	struct mbuf *m;
196353702Swpaul
1964109323Ssam	for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
1965109323Ssam		if (m->m_len == 0)
1966109323Ssam			continue;
196753702Swpaul
1968109323Ssam		len = min(m->m_len, totlen);
196953702Swpaul
1970109323Ssam		if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
1971109323Ssam			m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
1972109323Ssam			return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
1973109323Ssam			    totlen);
1974109323Ssam		}
197553702Swpaul
1976109323Ssam		if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
1977109323Ssam			return error;
197853702Swpaul
1979109323Ssam		off += m->m_len;
1980109323Ssam		totlen -= len;
1981109323Ssam	}
1982109323Ssam	return 0;
1983109323Ssam}
198453702Swpaul
1985109323Ssamstatic int
1986109323Ssamwi_alloc_fid(struct wi_softc *sc, int len, int *idp)
198753702Swpaul{
198853702Swpaul	int i;
198953702Swpaul
1990109323Ssam	if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
1991178354Ssam		device_printf(sc->sc_dev, "%s: failed to allocate %d bytes on NIC\n",
1992178354Ssam		    __func__, len);
1993109323Ssam		return ENOMEM;
199453702Swpaul	}
199553702Swpaul
1996109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
1997109323Ssam		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
1998109323Ssam			break;
1999109323Ssam		DELAY(1);
200053702Swpaul	}
2001144167Ssam	if (i == WI_TIMEOUT) {
2002178354Ssam		device_printf(sc->sc_dev, "%s: timeout in alloc\n", __func__);
2003144167Ssam		return ETIMEDOUT;
2004144167Ssam	}
2005109323Ssam	*idp = CSR_READ_2(sc, WI_ALLOC_FID);
2006109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
2007109323Ssam	return 0;
2008109323Ssam}
200953702Swpaul
2010109323Ssamstatic int
2011109323Ssamwi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
2012109323Ssam{
2013109323Ssam	int error, len;
2014109323Ssam	u_int16_t ltbuf[2];
201553702Swpaul
2016109323Ssam	/* Tell the NIC to enter record read mode. */
2017109323Ssam	error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2018109323Ssam	if (error)
2019109323Ssam		return error;
202053702Swpaul
2021109323Ssam	error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2022109323Ssam	if (error)
2023109323Ssam		return error;
202453702Swpaul
2025109323Ssam	if (le16toh(ltbuf[1]) != rid) {
2026109323Ssam		device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n",
2027109323Ssam		    rid, le16toh(ltbuf[1]));
2028109323Ssam		return EIO;
202953702Swpaul	}
2030109323Ssam	len = (le16toh(ltbuf[0]) - 1) * 2;	 /* already got rid */
2031109323Ssam	if (*buflenp < len) {
2032109323Ssam		device_printf(sc->sc_dev, "record buffer is too small, "
2033109323Ssam		    "rid=%x, size=%d, len=%d\n",
2034109323Ssam		    rid, *buflenp, len);
2035109323Ssam		return ENOSPC;
203653702Swpaul	}
2037109323Ssam	*buflenp = len;
2038109323Ssam	return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2039109323Ssam}
204053702Swpaul
2041109323Ssamstatic int
2042109323Ssamwi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2043109323Ssam{
2044109323Ssam	int error;
2045109323Ssam	u_int16_t ltbuf[2];
204653702Swpaul
2047109323Ssam	ltbuf[0] = htole16((buflen + 1) / 2 + 1);	 /* includes rid */
2048109323Ssam	ltbuf[1] = htole16(rid);
204953702Swpaul
2050109323Ssam	error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2051178354Ssam	if (error) {
2052178354Ssam		device_printf(sc->sc_dev, "%s: bap0 write failure, rid 0x%x\n",
2053178354Ssam		    __func__, rid);
2054109323Ssam		return error;
2055178354Ssam	}
2056109323Ssam	error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2057178354Ssam	if (error) {
2058178354Ssam		device_printf(sc->sc_dev, "%s: bap1 write failure, rid 0x%x\n",
2059178354Ssam		    __func__, rid);
2060109323Ssam		return error;
2061178354Ssam	}
2062102206Simp
2063109323Ssam	return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
206453702Swpaul}
206577217Sphk
206688546Salfredstatic int
2067178354Ssamwi_write_appie(struct wi_softc *sc, int rid, const struct ieee80211_appie *ie)
206877217Sphk{
2069178354Ssam	/* NB: 42 bytes is probably ok to have on the stack */
2070178354Ssam	char buf[sizeof(uint16_t) + 40];
207177217Sphk
2072178354Ssam	if (ie->ie_len > 40)
2073178354Ssam		return EINVAL;
2074178354Ssam	/* NB: firmware requires 16-bit ie length before ie data */
2075178354Ssam	*(uint16_t *) buf = htole16(ie->ie_len);
2076178354Ssam	memcpy(buf + sizeof(uint16_t), ie->ie_data, ie->ie_len);
2077178354Ssam	return wi_write_rid(sc, rid, buf, ie->ie_len + sizeof(uint16_t));
207877217Sphk}
207977217Sphk
2080109323Ssamint
2081109323Ssamwi_alloc(device_t dev, int rid)
2082109323Ssam{
2083109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2084109323Ssam
2085109323Ssam	if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2086109323Ssam		sc->iobase_rid = rid;
2087109323Ssam		sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2088109323Ssam		    &sc->iobase_rid, 0, ~0, (1 << 6),
2089109323Ssam		    rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2090178354Ssam		if (sc->iobase == NULL) {
2091109323Ssam			device_printf(dev, "No I/O space?!\n");
2092178354Ssam			return ENXIO;
209398440Simp		}
2094109323Ssam
2095109323Ssam		sc->wi_io_addr = rman_get_start(sc->iobase);
2096109323Ssam		sc->wi_btag = rman_get_bustag(sc->iobase);
2097109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2098109323Ssam	} else {
2099109323Ssam		sc->mem_rid = rid;
2100127135Snjl		sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2101127135Snjl		    &sc->mem_rid, RF_ACTIVE);
2102178354Ssam		if (sc->mem == NULL) {
2103109323Ssam			device_printf(dev, "No Mem space on prism2.5?\n");
2104178354Ssam			return ENXIO;
210577217Sphk		}
2106109323Ssam
2107109323Ssam		sc->wi_btag = rman_get_bustag(sc->mem);
2108109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->mem);
210977217Sphk	}
211077217Sphk
2111109323Ssam	sc->irq_rid = 0;
2112127135Snjl	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2113127135Snjl	    RF_ACTIVE |
2114109323Ssam	    ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2115178354Ssam	if (sc->irq == NULL) {
2116109323Ssam		wi_free(dev);
2117109323Ssam		device_printf(dev, "No irq?!\n");
2118178354Ssam		return ENXIO;
211977217Sphk	}
2120109323Ssam
2121109323Ssam	sc->sc_dev = dev;
2122109323Ssam	sc->sc_unit = device_get_unit(dev);
2123178354Ssam	return 0;
212477217Sphk}
212593359Simp
2126109323Ssamvoid
2127109323Ssamwi_free(device_t dev)
2128109323Ssam{
2129109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2130109323Ssam
2131109323Ssam	if (sc->iobase != NULL) {
2132109323Ssam		bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2133109323Ssam		sc->iobase = NULL;
2134109323Ssam	}
2135109323Ssam	if (sc->irq != NULL) {
2136109323Ssam		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2137109323Ssam		sc->irq = NULL;
2138109323Ssam	}
2139109323Ssam	if (sc->mem != NULL) {
2140109323Ssam		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
2141109323Ssam		sc->mem = NULL;
2142109323Ssam	}
2143109323Ssam}
2144