if_wi.c revision 181210
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 181210 2008-08-02 20:49:01Z imp $");
64113038Sobrien
65109323Ssam#define WI_HERMES_STATS_WAR	/* Work around stats counter bug. */
66109323Ssam
6746492Swpaul#include <sys/param.h>
6846492Swpaul#include <sys/systm.h>
6995533Smike#include <sys/endian.h>
7046492Swpaul#include <sys/sockio.h>
7146492Swpaul#include <sys/mbuf.h>
72164033Srwatson#include <sys/priv.h>
7383366Sjulian#include <sys/proc.h>
7446492Swpaul#include <sys/kernel.h>
7546492Swpaul#include <sys/socket.h>
7653702Swpaul#include <sys/module.h>
7753702Swpaul#include <sys/bus.h>
7894486Simp#include <sys/random.h>
7953702Swpaul#include <sys/syslog.h>
8053702Swpaul#include <sys/sysctl.h>
81178354Ssam#include <sys/taskqueue.h>
8246492Swpaul
8353702Swpaul#include <machine/bus.h>
8453702Swpaul#include <machine/resource.h>
85116951Ssam#include <machine/atomic.h>
8653702Swpaul#include <sys/rman.h>
8753702Swpaul
8846492Swpaul#include <net/if.h>
8946492Swpaul#include <net/if_arp.h>
9046492Swpaul#include <net/ethernet.h>
9146492Swpaul#include <net/if_dl.h>
9246492Swpaul#include <net/if_media.h>
9346492Swpaul#include <net/if_types.h>
9446492Swpaul
95116951Ssam#include <net80211/ieee80211_var.h>
96116951Ssam#include <net80211/ieee80211_ioctl.h>
97119784Ssam#include <net80211/ieee80211_radiotap.h>
98116951Ssam
9946492Swpaul#include <netinet/in.h>
10046492Swpaul#include <netinet/in_systm.h>
10146492Swpaul#include <netinet/in_var.h>
10246492Swpaul#include <netinet/ip.h>
10346492Swpaul#include <netinet/if_ether.h>
10446492Swpaul
10546492Swpaul#include <net/bpf.h>
10646492Swpaul
10770808Speter#include <dev/wi/if_wavelan_ieee.h>
108119784Ssam#include <dev/wi/if_wireg.h>
10993611Simp#include <dev/wi/if_wivar.h>
11046492Swpaul
111178354Ssamstatic struct ieee80211vap *wi_vap_create(struct ieee80211com *ic,
112178354Ssam		const char name[IFNAMSIZ], int unit, int opmode, int flags,
113178354Ssam		const uint8_t bssid[IEEE80211_ADDR_LEN],
114178354Ssam		const uint8_t mac[IEEE80211_ADDR_LEN]);
115178354Ssamstatic void wi_vap_delete(struct ieee80211vap *vap);
116178354Ssamstatic void wi_stop_locked(struct wi_softc *sc, int disable);
117165087Ssamstatic void wi_start_locked(struct ifnet *);
11891693Simpstatic void wi_start(struct ifnet *);
119160991Ssamstatic int  wi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr,
120160991Ssam		struct mbuf *m0);
121160991Ssamstatic int  wi_raw_xmit(struct ieee80211_node *, struct mbuf *,
122160991Ssam		const struct ieee80211_bpf_params *);
123178354Ssamstatic int  wi_newstate_sta(struct ieee80211vap *, enum ieee80211_state, int);
124178354Ssamstatic int  wi_newstate_hostap(struct ieee80211vap *, enum ieee80211_state, int);
125178354Ssamstatic void wi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
126178354Ssam		int subtype, int rssi, int noise, u_int32_t rstamp);
127178354Ssamstatic int  wi_reset(struct wi_softc *);
128165089Ssamstatic void wi_watchdog(void *);
129109323Ssamstatic int  wi_ioctl(struct ifnet *, u_long, caddr_t);
130109323Ssamstatic void wi_media_status(struct ifnet *, struct ifmediareq *);
13146492Swpaul
132109323Ssamstatic void wi_rx_intr(struct wi_softc *);
133109323Ssamstatic void wi_tx_intr(struct wi_softc *);
134109323Ssamstatic void wi_tx_ex_intr(struct wi_softc *);
135178354Ssam
136178354Ssamstatic void wi_status_connected(void *, int);
137178354Ssamstatic void wi_status_disconnected(void *, int);
138178354Ssamstatic void wi_status_oor(void *, int);
139178354Ssamstatic void wi_status_assoc_failed(void *, int);
140109323Ssamstatic void wi_info_intr(struct wi_softc *);
14146492Swpaul
142178354Ssamstatic int  wi_write_txrate(struct wi_softc *, struct ieee80211vap *);
143178354Ssamstatic int  wi_write_wep(struct wi_softc *, struct ieee80211vap *);
144109323Ssamstatic int  wi_write_multi(struct wi_softc *);
145178354Ssamstatic void wi_update_mcast(struct ifnet *);
146109323Ssamstatic int  wi_alloc_fid(struct wi_softc *, int, int *);
147109323Ssamstatic void wi_read_nicid(struct wi_softc *);
148109323Ssamstatic int  wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
14953702Swpaul
150109323Ssamstatic int  wi_cmd(struct wi_softc *, int, int, int, int);
151109323Ssamstatic int  wi_seek_bap(struct wi_softc *, int, int);
152109323Ssamstatic int  wi_read_bap(struct wi_softc *, int, int, void *, int);
153109323Ssamstatic int  wi_write_bap(struct wi_softc *, int, int, void *, int);
154109323Ssamstatic int  wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
155109323Ssamstatic int  wi_read_rid(struct wi_softc *, int, void *, int *);
156109323Ssamstatic int  wi_write_rid(struct wi_softc *, int, void *, int);
157178354Ssamstatic int  wi_write_appie(struct wi_softc *, int, const struct ieee80211_appie *);
15877217Sphk
159170530Ssamstatic void wi_scan_start(struct ieee80211com *);
160170530Ssamstatic void wi_scan_end(struct ieee80211com *);
161170530Ssamstatic void wi_set_channel(struct ieee80211com *);
162170530Ssam
163109323Ssamstatic __inline int
164109323Ssamwi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
165109323Ssam{
16653702Swpaul
167109323Ssam	val = htole16(val);
168109323Ssam	return wi_write_rid(sc, rid, &val, sizeof(val));
169109323Ssam}
170109323Ssam
171109592SsamSYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters");
172109592Ssam
173109323Ssamstatic	struct timeval lasttxerror;	/* time of last tx error msg */
174109323Ssamstatic	int curtxeps;			/* current tx error msgs/sec */
175111559Ssamstatic	int wi_txerate = 0;		/* tx error rate: max msgs/sec */
176109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate,
177111559Ssam	    0, "max tx error msgs/sec; 0 to disable msgs");
178109323Ssam
179109323Ssam#define	WI_DEBUG
180109323Ssam#ifdef WI_DEBUG
181109323Ssamstatic	int wi_debug = 0;
182109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug,
183109592Ssam	    0, "control debugging printfs");
184109323Ssam#define	DPRINTF(X)	if (wi_debug) printf X
185109323Ssam#else
186109323Ssam#define	DPRINTF(X)
187109323Ssam#endif
188109323Ssam
189109323Ssam#define WI_INTRS	(WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
190109323Ssam
19193825Simpstruct wi_card_ident wi_card_ident[] = {
19293825Simp	/* CARD_ID			CARD_NAME		FIRM_TYPE */
19393825Simp	{ WI_NIC_LUCENT_ID,		WI_NIC_LUCENT_STR,	WI_LUCENT },
19493825Simp	{ WI_NIC_SONY_ID,		WI_NIC_SONY_STR,	WI_LUCENT },
19593825Simp	{ WI_NIC_LUCENT_EMB_ID,		WI_NIC_LUCENT_EMB_STR,	WI_LUCENT },
19693825Simp	{ WI_NIC_EVB2_ID,		WI_NIC_EVB2_STR,	WI_INTERSIL },
19793825Simp	{ WI_NIC_HWB3763_ID,		WI_NIC_HWB3763_STR,	WI_INTERSIL },
19893825Simp	{ WI_NIC_HWB3163_ID,		WI_NIC_HWB3163_STR,	WI_INTERSIL },
19993825Simp	{ WI_NIC_HWB3163B_ID,		WI_NIC_HWB3163B_STR,	WI_INTERSIL },
20093825Simp	{ WI_NIC_EVB3_ID,		WI_NIC_EVB3_STR,	WI_INTERSIL },
20193825Simp	{ WI_NIC_HWB1153_ID,		WI_NIC_HWB1153_STR,	WI_INTERSIL },
20293825Simp	{ WI_NIC_P2_SST_ID,		WI_NIC_P2_SST_STR,	WI_INTERSIL },
20393825Simp	{ WI_NIC_EVB2_SST_ID,		WI_NIC_EVB2_SST_STR,	WI_INTERSIL },
20493825Simp	{ WI_NIC_3842_EVA_ID,		WI_NIC_3842_EVA_STR,	WI_INTERSIL },
20593825Simp	{ WI_NIC_3842_PCMCIA_AMD_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
20693825Simp	{ WI_NIC_3842_PCMCIA_SST_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
207101355Simp	{ WI_NIC_3842_PCMCIA_ATL_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
208101355Simp	{ WI_NIC_3842_PCMCIA_ATS_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
20993825Simp	{ WI_NIC_3842_MINI_AMD_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
21093825Simp	{ WI_NIC_3842_MINI_SST_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
211101355Simp	{ WI_NIC_3842_MINI_ATL_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
212101355Simp	{ WI_NIC_3842_MINI_ATS_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
21393825Simp	{ WI_NIC_3842_PCI_AMD_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
21493825Simp	{ WI_NIC_3842_PCI_SST_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
215101355Simp	{ WI_NIC_3842_PCI_ATS_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
216101355Simp	{ WI_NIC_3842_PCI_ATL_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
21793825Simp	{ WI_NIC_P3_PCMCIA_AMD_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
21893825Simp	{ WI_NIC_P3_PCMCIA_SST_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
219101355Simp	{ WI_NIC_P3_PCMCIA_ATL_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
220101355Simp	{ WI_NIC_P3_PCMCIA_ATS_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
22193825Simp	{ WI_NIC_P3_MINI_AMD_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
22293825Simp	{ WI_NIC_P3_MINI_SST_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
223101355Simp	{ WI_NIC_P3_MINI_ATL_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
224101355Simp	{ WI_NIC_P3_MINI_ATS_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
22593825Simp	{ 0,	NULL,	0 },
22693825Simp};
22793825Simp
228180919Simpstatic char *wi_firmware_names[] = { "none", "Hermes", "Intersil", "Symbol" };
229180919Simp
230109323Ssamdevclass_t wi_devclass;
23146492Swpaul
23293611Simpint
233109323Ssamwi_attach(device_t dev)
23474906Salfred{
235109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
236178354Ssam	struct ieee80211com *ic;
237147256Sbrooks	struct ifnet *ifp;
238116951Ssam	int i, nrates, buflen;
239109323Ssam	u_int16_t val;
240109323Ssam	u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
241116951Ssam	struct ieee80211_rateset *rs;
242180919Simp	struct sysctl_ctx_list *sctx;
243180919Simp	struct sysctl_oid *soid;
244109323Ssam	static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
245109323Ssam		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
246109323Ssam	};
247109323Ssam	int error;
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");
299180919Simp	SYSCTL_ADD_XINT(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;
315109323Ssam	error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
316109323Ssam	if (error != 0) {
317109323Ssam		buflen = IEEE80211_ADDR_LEN;
318109323Ssam		error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
319109323Ssam	}
320109323Ssam	if (error || IEEE80211_ADDR_EQ(ic->ic_myaddr, 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;
337132986Smlaier	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
338132986Smlaier	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
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;
452178354Ssam	TASK_INIT(&sc->sc_oor_task, 0, wi_status_oor, ic);
45387383Simp
454138571Ssam	ieee80211_ifattach(ic);
455160991Ssam	ic->ic_raw_xmit = wi_raw_xmit;
456170530Ssam	ic->ic_scan_start = wi_scan_start;
457170530Ssam	ic->ic_scan_end = wi_scan_end;
458170530Ssam	ic->ic_set_channel = wi_set_channel;
459170530Ssam
460178354Ssam	ic->ic_vap_create = wi_vap_create;
461178354Ssam	ic->ic_vap_delete = wi_vap_delete;
462178354Ssam	ic->ic_update_mcast = wi_update_mcast;
463109323Ssam
464178354Ssam	bpfattach(ifp, DLT_IEEE802_11_RADIO,
465178354Ssam		sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th));
466119784Ssam	/*
467119784Ssam	 * Initialize constant fields.
468127698Ssam	 * XXX make header lengths a multiple of 32-bits so subsequent
469127698Ssam	 *     headers are properly aligned; this is a kludge to keep
470127698Ssam	 *     certain applications happy.
471119784Ssam	 *
472119784Ssam	 * NB: the channel is setup each time we transition to the
473119784Ssam	 *     RUN state to avoid filling it in for each frame.
474119784Ssam	 */
475127698Ssam	sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t));
476127698Ssam	sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len);
477127698Ssam	sc->sc_tx_th.wt_ihdr.it_present = htole32(WI_TX_RADIOTAP_PRESENT);
478119784Ssam
479127698Ssam	sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t));
480127698Ssam	sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len);
481127698Ssam	sc->sc_rx_th.wr_ihdr.it_present = htole32(WI_RX_RADIOTAP_PRESENT);
482138571Ssam
483138571Ssam	if (bootverbose)
484138571Ssam		ieee80211_announce(ic);
485138571Ssam
486180826Simp	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
487180826Simp	    NULL, wi_intr, sc, &sc->wi_intrhand);
488180826Simp	if (error) {
489180826Simp		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
490180826Simp		bpfdetach(ifp);
491180826Simp		ieee80211_ifdetach(ic);
492180826Simp		if_free(sc->sc_ifp);
493180826Simp		wi_free(dev);
494180826Simp		return error;
495180826Simp	}
496180826Simp
497109323Ssam	return (0);
49887383Simp}
49987383Simp
500109323Ssamint
501109323Ssamwi_detach(device_t dev)
50246492Swpaul{
503109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
504147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
505178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
50646492Swpaul
507109323Ssam	WI_LOCK(sc);
50846492Swpaul
509109323Ssam	/* check if device was removed */
510123098Simp	sc->wi_gone |= !bus_child_present(dev);
51146492Swpaul
512178354Ssam	wi_stop_locked(sc, 0);
513150678Sru	WI_UNLOCK(sc);
514178354Ssam	bpfdetach(ifp);
515178354Ssam	ieee80211_ifdetach(ic);
51646492Swpaul
517109323Ssam	bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
518150306Simp	if_free(sc->sc_ifp);
519109323Ssam	wi_free(dev);
520109323Ssam	mtx_destroy(&sc->sc_mtx);
521109323Ssam	return (0);
522109323Ssam}
52393359Simp
524178354Ssamstatic struct ieee80211vap *
525178354Ssamwi_vap_create(struct ieee80211com *ic,
526178354Ssam	const char name[IFNAMSIZ], int unit, int opmode, int flags,
527178354Ssam	const uint8_t bssid[IEEE80211_ADDR_LEN],
528178354Ssam	const uint8_t mac[IEEE80211_ADDR_LEN])
529109323Ssam{
530178354Ssam	struct wi_softc *sc = ic->ic_ifp->if_softc;
531178354Ssam	struct wi_vap *wvp;
532178354Ssam	struct ieee80211vap *vap;
53393359Simp
534178354Ssam	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
535178354Ssam		return NULL;
536178354Ssam	wvp = (struct wi_vap *) malloc(sizeof(struct wi_vap),
537178354Ssam	    M_80211_VAP, M_NOWAIT | M_ZERO);
538178354Ssam	if (wvp == NULL)
539178354Ssam		return NULL;
54093359Simp
541178354Ssam	vap = &wvp->wv_vap;
542178354Ssam	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
54393359Simp
544178354Ssam	vap->iv_max_aid = WI_MAX_AID;
54593359Simp
546178354Ssam	switch (opmode) {
547178354Ssam	case IEEE80211_M_STA:
548178354Ssam		sc->sc_porttype = WI_PORTTYPE_BSS;
549178354Ssam		wvp->wv_newstate = vap->iv_newstate;
550178354Ssam		vap->iv_newstate = wi_newstate_sta;
551178354Ssam		/* need to filter mgt frames to avoid confusing state machine */
552178354Ssam		wvp->wv_recv_mgmt = vap->iv_recv_mgmt;
553178354Ssam		vap->iv_recv_mgmt = wi_recv_mgmt;
554109323Ssam		break;
555178354Ssam	case IEEE80211_M_IBSS:
556178354Ssam		sc->sc_porttype = sc->sc_ibss_port;
557178354Ssam		wvp->wv_newstate = vap->iv_newstate;
558178354Ssam		vap->iv_newstate = wi_newstate_sta;
559109323Ssam		break;
560178354Ssam	case IEEE80211_M_AHDEMO:
561178354Ssam		sc->sc_porttype = WI_PORTTYPE_ADHOC;
562109323Ssam		break;
563178354Ssam	case IEEE80211_M_HOSTAP:
564178354Ssam		sc->sc_porttype = WI_PORTTYPE_HOSTAP;
565178354Ssam		wvp->wv_newstate = vap->iv_newstate;
566178354Ssam		vap->iv_newstate = wi_newstate_hostap;
567178354Ssam		break;
568178354Ssam	case IEEE80211_M_MONITOR:
569178354Ssam		sc->sc_porttype = sc->sc_monitor_port;
570178354Ssam		break;
571178354Ssam	default:
572178354Ssam		break;
57393359Simp	}
574178354Ssam
575178354Ssam	TASK_INIT(&wvp->wv_connected_task, 0, wi_status_connected, vap);
576178354Ssam	TASK_INIT(&wvp->wv_disconnected_task, 0, wi_status_disconnected, vap);
577178354Ssam	TASK_INIT(&wvp->wv_assoc_failed_task, 0, wi_status_assoc_failed, vap);
578178354Ssam
579178354Ssam	/* complete setup */
580178354Ssam	ieee80211_vap_attach(vap, ieee80211_media_change, wi_media_status);
581178354Ssam	ic->ic_opmode = opmode;
582178354Ssam	return vap;
58346492Swpaul}
58446492Swpaul
585178354Ssamstatic void
586178354Ssamwi_vap_delete(struct ieee80211vap *vap)
587178354Ssam{
588178354Ssam	struct wi_vap *wvp = WI_VAP(vap);
589178354Ssam
590178354Ssam	ieee80211_vap_detach(vap);
591178354Ssam	free(wvp, M_80211_VAP);
592178354Ssam}
593178354Ssam
594109323Ssamvoid
595109323Ssamwi_shutdown(device_t dev)
59646492Swpaul{
597109323Ssam	struct wi_softc *sc = device_get_softc(dev);
59846492Swpaul
599178354Ssam	wi_stop(sc, 1);
60046492Swpaul}
60146492Swpaul
602109323Ssamvoid
603109323Ssamwi_intr(void *arg)
60446492Swpaul{
605109323Ssam	struct wi_softc *sc = arg;
606147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
607112363Simp	u_int16_t status;
60846492Swpaul
609109323Ssam	WI_LOCK(sc);
61046492Swpaul
611123098Simp	if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) {
612113327Simp		CSR_WRITE_2(sc, WI_INT_EN, 0);
613123098Simp		CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
614109323Ssam		WI_UNLOCK(sc);
61546492Swpaul		return;
616109323Ssam	}
61746492Swpaul
618112363Simp	/* Disable interrupts. */
619112363Simp	CSR_WRITE_2(sc, WI_INT_EN, 0);
62046492Swpaul
621112363Simp	status = CSR_READ_2(sc, WI_EVENT_STAT);
622112363Simp	if (status & WI_EV_RX)
623112363Simp		wi_rx_intr(sc);
624112363Simp	if (status & WI_EV_ALLOC)
625112363Simp		wi_tx_intr(sc);
626112363Simp	if (status & WI_EV_TX_EXC)
627112363Simp		wi_tx_ex_intr(sc);
628112363Simp	if (status & WI_EV_INFO)
629112363Simp		wi_info_intr(sc);
630148887Srwatson	if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
631132986Smlaier	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
632165087Ssam		wi_start_locked(ifp);
63346492Swpaul
634112363Simp	/* Re-enable interrupts. */
635112363Simp	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
63646492Swpaul
637109323Ssam	WI_UNLOCK(sc);
63846492Swpaul
63946492Swpaul	return;
64046492Swpaul}
64146492Swpaul
642178354Ssamstatic void
643178354Ssamwi_enable(struct wi_softc *sc)
644178354Ssam{
645178354Ssam	/* Enable interrupts */
646178354Ssam	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
647178354Ssam
648178354Ssam	/* enable port */
649178354Ssam	wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
650178354Ssam	sc->sc_enabled = 1;
651178354Ssam}
652178354Ssam
653178354Ssamstatic int
654178354Ssamwi_setup_locked(struct wi_softc *sc, int porttype, int mode,
655178354Ssam	uint8_t mac[IEEE80211_ADDR_LEN])
656178354Ssam{
657178354Ssam	int i;
658178354Ssam
659178354Ssam	wi_reset(sc);
660178354Ssam
661178354Ssam	wi_write_val(sc, WI_RID_PORTTYPE, porttype);
662178354Ssam	wi_write_val(sc, WI_RID_CREATE_IBSS, mode);
663178354Ssam	wi_write_val(sc, WI_RID_MAX_DATALEN, 2304);
664178354Ssam	/* XXX IEEE80211_BPF_NOACK wants 0 */
665178354Ssam	wi_write_val(sc, WI_RID_ALT_RETRY_CNT, 2);
666178354Ssam	if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
667178354Ssam		wi_write_val(sc, WI_RID_ROAMING_MODE, 3); /* NB: disabled */
668178354Ssam
669178354Ssam	wi_write_rid(sc, WI_RID_MAC_NODE, mac, IEEE80211_ADDR_LEN);
670178354Ssam
671178354Ssam	/* Allocate fids for the card */
672178354Ssam	sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
673178354Ssam	for (i = 0; i < sc->sc_ntxbuf; i++) {
674178354Ssam		int error = wi_alloc_fid(sc, sc->sc_buflen,
675178354Ssam		    &sc->sc_txd[i].d_fid);
676178354Ssam		if (error) {
677178354Ssam			device_printf(sc->sc_dev,
678178354Ssam			    "tx buffer allocation failed (error %u)\n",
679178354Ssam			    error);
680178354Ssam			return error;
681178354Ssam		}
682178354Ssam		sc->sc_txd[i].d_len = 0;
683178354Ssam	}
684178354Ssam	sc->sc_txcur = sc->sc_txnext = 0;
685178354Ssam
686178354Ssam	return 0;
687178354Ssam}
688178354Ssam
689178354Ssamstatic void
690178354Ssamwi_init_locked(struct wi_softc *sc)
691178354Ssam{
692178354Ssam	struct ifnet *ifp = sc->sc_ifp;
693178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
694178354Ssam	int wasenabled;
695178354Ssam
696178354Ssam	WI_LOCK_ASSERT(sc);
697178354Ssam
698178354Ssam	wasenabled = sc->sc_enabled;
699178354Ssam	if (wasenabled)
700178354Ssam		wi_stop_locked(sc, 1);
701178354Ssam
702178354Ssam	IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
703178354Ssam	if (wi_setup_locked(sc, sc->sc_porttype, 3, ic->ic_myaddr) != 0) {
704178354Ssam		if_printf(ifp, "interface not running\n");
705178354Ssam		wi_stop_locked(sc, 1);
706178354Ssam		return;
707178354Ssam	}
708178354Ssam
709178354Ssam	ifp->if_drv_flags |= IFF_DRV_RUNNING;
710178354Ssam	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
711178354Ssam
712178354Ssam	callout_reset(&sc->sc_watchdog, hz, wi_watchdog, sc);
713178354Ssam
714178354Ssam	wi_enable(sc);			/* Enable desired port */
715178354Ssam}
716178354Ssam
717109323Ssamvoid
718109323Ssamwi_init(void *arg)
71946492Swpaul{
720109323Ssam	struct wi_softc *sc = arg;
721147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
722178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
72346492Swpaul
724178354Ssam	WI_LOCK(sc);
725178354Ssam	wi_init_locked(sc);
726178354Ssam	WI_UNLOCK(sc);
72767092Swpaul
728178931Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
729178931Sthompsa		ieee80211_start_all(ic);		/* start all vap's */
730178354Ssam}
731170530Ssam
732178354Ssamstatic void
733178354Ssamwi_stop_locked(struct wi_softc *sc, int disable)
734178354Ssam{
735178354Ssam	struct ifnet *ifp = sc->sc_ifp;
73646492Swpaul
737178354Ssam	WI_LOCK_ASSERT(sc);
73846492Swpaul
739178354Ssam	if (sc->sc_enabled && !sc->wi_gone) {
740178354Ssam		CSR_WRITE_2(sc, WI_INT_EN, 0);
741178354Ssam		wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
742178354Ssam		if (disable)
743178354Ssam			sc->sc_enabled = 0;
744178354Ssam	} else if (sc->wi_gone && disable)	/* gone --> not enabled */
745178354Ssam		sc->sc_enabled = 0;
746178354Ssam
747178354Ssam	callout_stop(&sc->sc_watchdog);
748178354Ssam	sc->sc_tx_timer = 0;
749178354Ssam	sc->sc_false_syns = 0;
750178354Ssam
751178354Ssam	ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE | IFF_DRV_RUNNING);
752178354Ssam}
753178354Ssam
754178354Ssamvoid
755178354Ssamwi_stop(struct wi_softc *sc, int disable)
756178354Ssam{
757170530Ssam	WI_LOCK(sc);
758178354Ssam	wi_stop_locked(sc, disable);
759178354Ssam	WI_UNLOCK(sc);
760178354Ssam}
761170530Ssam
762178354Ssamstatic void
763178354Ssamwi_set_channel(struct ieee80211com *ic)
764178354Ssam{
765178354Ssam	struct ifnet *ifp = ic->ic_ifp;
766178354Ssam	struct wi_softc *sc = ifp->if_softc;
767160991Ssam
768178354Ssam	DPRINTF(("%s: channel %d, %sscanning\n", __func__,
769178354Ssam	    ieee80211_chan2ieee(ic, ic->ic_curchan),
770178354Ssam	    ic->ic_flags & IEEE80211_F_SCAN ? "" : "!"));
77146492Swpaul
772178354Ssam	WI_LOCK(sc);
773116951Ssam	wi_write_val(sc, WI_RID_OWN_CHNL,
774178354Ssam	    ieee80211_chan2ieee(ic, ic->ic_curchan));
77546492Swpaul
776178354Ssam	sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
777178354Ssam		htole16(ic->ic_curchan->ic_freq);
778178354Ssam	sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
779178354Ssam		htole16(ic->ic_curchan->ic_flags);
780178354Ssam	WI_UNLOCK(sc);
781178354Ssam}
78275373Salfred
783178354Ssamstatic void
784178354Ssamwi_scan_start(struct ieee80211com *ic)
785178354Ssam{
786178354Ssam	struct ifnet *ifp = ic->ic_ifp;
787178354Ssam	struct wi_softc *sc = ifp->if_softc;
788178354Ssam	struct ieee80211_scan_state *ss = ic->ic_scan;
78946492Swpaul
790178354Ssam	DPRINTF(("%s\n", __func__));
79146492Swpaul
792178354Ssam	WI_LOCK(sc);
793109323Ssam	/*
794178354Ssam	 * Switch device to monitor mode.
795109323Ssam	 */
796178354Ssam	wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_monitor_port);
797178354Ssam	if (sc->sc_firmware_type == WI_INTERSIL) {
798178354Ssam		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
799178354Ssam		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
80075373Salfred	}
801178354Ssam	/* force full dwell time to compensate for firmware overhead */
802178354Ssam	ss->ss_mindwell = ss->ss_maxdwell = msecs_to_ticks(400);
803178354Ssam	WI_UNLOCK(sc);
80446492Swpaul
805178354Ssam}
80667092Swpaul
807178354Ssamstatic void
808178354Ssamwi_scan_end(struct ieee80211com *ic)
809178354Ssam{
810178354Ssam	struct ifnet *ifp = ic->ic_ifp;
811178354Ssam	struct wi_softc *sc = ifp->if_softc;
81246492Swpaul
813178354Ssam	DPRINTF(("%s: restore port type %d\n", __func__, sc->sc_porttype));
814178354Ssam
815178354Ssam	WI_LOCK(sc);
816178354Ssam	wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_porttype);
817178354Ssam	if (sc->sc_firmware_type == WI_INTERSIL) {
818178354Ssam		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
819178354Ssam		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
82070073Swpaul	}
821178354Ssam	WI_UNLOCK(sc);
822178354Ssam}
82370073Swpaul
824178354Ssamstatic void
825178354Ssamwi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
826178354Ssam	int subtype, int rssi, int noise, u_int32_t rstamp)
827178354Ssam{
828178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
82946492Swpaul
830178354Ssam	switch (subtype) {
831178354Ssam	case IEEE80211_FC0_SUBTYPE_AUTH:
832178354Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
833178354Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
834178354Ssam		/* NB: filter frames that trigger state changes */
835178354Ssam		return;
836170530Ssam	}
837178354Ssam	WI_VAP(vap)->wv_recv_mgmt(ni, m, subtype, rssi, noise, rstamp);
838178354Ssam}
83946492Swpaul
840178354Ssamstatic int
841178354Ssamwi_newstate_sta(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
842178354Ssam{
843178354Ssam	struct ieee80211com *ic = vap->iv_ic;
844178354Ssam	struct ifnet *ifp = ic->ic_ifp;
845178354Ssam	struct ieee80211_node *bss;
846178354Ssam	struct wi_softc *sc = ifp->if_softc;
84794397Simp
848178354Ssam	DPRINTF(("%s: %s -> %s\n", __func__,
849178354Ssam		ieee80211_state_name[vap->iv_state],
850178354Ssam		ieee80211_state_name[nstate]));
851178354Ssam
852178354Ssam	if (nstate == IEEE80211_S_AUTH) {
853178354Ssam		WI_LOCK(sc);
854178354Ssam		wi_setup_locked(sc, WI_PORTTYPE_BSS, 3, vap->iv_myaddr);
855178354Ssam
856178354Ssam		if (vap->iv_flags & IEEE80211_F_PMGTON) {
857178354Ssam			wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
858178354Ssam			wi_write_val(sc, WI_RID_PM_ENABLED, 1);
859178354Ssam		}
860178354Ssam		wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold);
861178354Ssam		if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
862178354Ssam			wi_write_val(sc, WI_RID_FRAG_THRESH,
863178354Ssam			    vap->iv_fragthreshold);
864178354Ssam		wi_write_txrate(sc, vap);
865178354Ssam
866178354Ssam		bss = vap->iv_bss;
867178354Ssam		wi_write_ssid(sc, WI_RID_DESIRED_SSID, bss->ni_essid, bss->ni_esslen);
868178354Ssam		wi_write_val(sc, WI_RID_OWN_CHNL,
869178354Ssam		    ieee80211_chan2ieee(ic, bss->ni_chan));
870178354Ssam
871178354Ssam		/* Configure WEP. */
872178354Ssam		if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP)
873178354Ssam			wi_write_wep(sc, vap);
874178354Ssam		else
875178354Ssam			sc->sc_encryption = 0;
876178354Ssam
877178354Ssam		if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) &&
878178354Ssam		    (vap->iv_flags & IEEE80211_F_WPA)) {
879178354Ssam			wi_write_val(sc, WI_RID_WPA_HANDLING, 1);
880178354Ssam			if (vap->iv_appie_wpa != NULL)
881178354Ssam				wi_write_appie(sc, WI_RID_WPA_DATA,
882178354Ssam				    vap->iv_appie_wpa);
883178354Ssam		}
884178354Ssam
885178354Ssam		wi_enable(sc);		/* enable port */
886178354Ssam
887178354Ssam		/* Lucent firmware does not support the JOIN RID. */
888178354Ssam		if (sc->sc_firmware_type == WI_INTERSIL) {
889178354Ssam			struct wi_joinreq join;
890178354Ssam
891178354Ssam			memset(&join, 0, sizeof(join));
892178354Ssam			IEEE80211_ADDR_COPY(&join.wi_bssid, bss->ni_bssid);
893116951Ssam			join.wi_chan = htole16(
894178354Ssam			    ieee80211_chan2ieee(ic, bss->ni_chan));
895109323Ssam			wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
896178354Ssam		}
897178354Ssam		WI_UNLOCK(sc);
89875373Salfred
899178354Ssam		/*
900178354Ssam		 * NB: don't go through 802.11 layer, it'll send auth frame;
901178354Ssam		 * instead we drive the state machine from the link status
902178354Ssam		 * notification we get on association.
903178354Ssam		 */
904178354Ssam		vap->iv_state = nstate;
905178354Ssam		return EINPROGRESS;
90670073Swpaul	}
907178354Ssam	return WI_VAP(vap)->wv_newstate(vap, nstate, arg);
90846492Swpaul}
90946492Swpaul
910178354Ssamstatic int
911178354Ssamwi_newstate_hostap(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
91246492Swpaul{
913178354Ssam	struct ieee80211com *ic = vap->iv_ic;
914178354Ssam	struct ifnet *ifp = ic->ic_ifp;
915178354Ssam	struct ieee80211_node *bss;
916109323Ssam	struct wi_softc *sc = ifp->if_softc;
917178354Ssam	int error;
91846492Swpaul
919178354Ssam	DPRINTF(("%s: %s -> %s\n", __func__,
920178354Ssam		ieee80211_state_name[vap->iv_state],
921178354Ssam		ieee80211_state_name[nstate]));
92274998Swpaul
923178354Ssam	error = WI_VAP(vap)->wv_newstate(vap, nstate, arg);
924178354Ssam	if (error == 0 && nstate == IEEE80211_S_RUN) {
925178354Ssam		WI_LOCK(sc);
926178354Ssam		wi_setup_locked(sc, WI_PORTTYPE_HOSTAP, 0, vap->iv_myaddr);
927178354Ssam
928178354Ssam		bss = vap->iv_bss;
929178354Ssam		wi_write_ssid(sc, WI_RID_OWN_SSID,
930178354Ssam		    bss->ni_essid, bss->ni_esslen);
931178354Ssam		wi_write_val(sc, WI_RID_OWN_CHNL,
932178354Ssam		    ieee80211_chan2ieee(ic, bss->ni_chan));
933178354Ssam		wi_write_val(sc, WI_RID_BASIC_RATE, 0x3);
934178354Ssam		wi_write_val(sc, WI_RID_SUPPORT_RATE, 0xf);
935178354Ssam		wi_write_txrate(sc, vap);
936178354Ssam
937178354Ssam		wi_write_val(sc, WI_RID_OWN_BEACON_INT, bss->ni_intval);
938178354Ssam		wi_write_val(sc, WI_RID_DTIM_PERIOD, vap->iv_dtim_period);
939178354Ssam
940178354Ssam		wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold);
941178354Ssam		if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
942178354Ssam			wi_write_val(sc, WI_RID_FRAG_THRESH,
943178354Ssam			    vap->iv_fragthreshold);
944178354Ssam
945178354Ssam		if ((sc->sc_flags & WI_FLAGS_HAS_ENHSECURITY) &&
946178354Ssam		    (vap->iv_flags & IEEE80211_F_HIDESSID)) {
947178354Ssam			/*
948178354Ssam			 * bit 0 means hide SSID in beacons,
949178354Ssam			 * bit 1 means don't respond to bcast probe req
950178354Ssam			 */
951178354Ssam			wi_write_val(sc, WI_RID_ENH_SECURITY, 0x3);
95270073Swpaul		}
95370073Swpaul
954178354Ssam		if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) &&
955178354Ssam		    (vap->iv_flags & IEEE80211_F_WPA) &&
956178354Ssam		    vap->iv_appie_wpa != NULL)
957178354Ssam			wi_write_appie(sc, WI_RID_WPA_DATA, vap->iv_appie_wpa);
95846492Swpaul
959178354Ssam		wi_write_val(sc, WI_RID_PROMISC, 0);
960178354Ssam
961178354Ssam		/* Configure WEP. */
962178354Ssam		if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP)
963178354Ssam			wi_write_wep(sc, vap);
964178354Ssam		else
965178354Ssam			sc->sc_encryption = 0;
966178354Ssam
967178354Ssam		wi_enable(sc);		/* enable port */
968178354Ssam		WI_UNLOCK(sc);
969178354Ssam	}
970178354Ssam	return error;
97146492Swpaul}
97246492Swpaul
973109323Ssamstatic void
974165087Ssamwi_start_locked(struct ifnet *ifp)
97546492Swpaul{
976109323Ssam	struct wi_softc	*sc = ifp->if_softc;
977119150Ssam	struct ieee80211_node *ni;
978109323Ssam	struct ieee80211_frame *wh;
979109323Ssam	struct mbuf *m0;
980178354Ssam	struct ieee80211_key *k;
981109323Ssam	struct wi_frame frmhdr;
982160991Ssam	int cur;
98346492Swpaul
984165087Ssam	WI_LOCK_ASSERT(sc);
98546492Swpaul
986165087Ssam	if (sc->wi_gone)
987109323Ssam		return;
98846492Swpaul
989109323Ssam	memset(&frmhdr, 0, sizeof(frmhdr));
990109323Ssam	cur = sc->sc_txnext;
991109323Ssam	for (;;) {
992178354Ssam		IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
993178354Ssam		if (m0 == NULL)
994178354Ssam			break;
995178354Ssam		if (sc->sc_txd[cur].d_len != 0) {
996178354Ssam			IFQ_DRV_PREPEND(&ifp->if_snd, m0);
997178354Ssam			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
998178354Ssam			break;
999178354Ssam		}
1000178354Ssam		/* NB: copy before 802.11 header is prepended */
1001178354Ssam		m_copydata(m0, 0, ETHER_HDR_LEN,
1002178354Ssam		    (caddr_t)&frmhdr.wi_ehdr);
1003119150Ssam
1004178354Ssam		ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
1005178354Ssam		m0 = ieee80211_encap(ni, m0);
1006178354Ssam		if (m0 == NULL) {
1007178354Ssam			ifp->if_oerrors++;
1008178354Ssam			ieee80211_free_node(ni);
1009178354Ssam			continue;
1010178354Ssam		}
101146492Swpaul
1012178354Ssam		wh = mtod(m0, struct ieee80211_frame *);
1013109323Ssam		frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
1014178354Ssam		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1015178354Ssam			k = ieee80211_crypto_encap(ni, m0);
1016138571Ssam			if (k == NULL) {
1017170530Ssam				ieee80211_free_node(ni);
1018143299Ssam				m_freem(m0);
1019109323Ssam				continue;
1020109323Ssam			}
1021138952Ssam			frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
1022109323Ssam		}
1023178354Ssam
1024178354Ssam		if (bpf_peers_present(ifp->if_bpf)) {
1025178354Ssam			sc->sc_tx_th.wt_rate = ni->ni_txrate;
1026178354Ssam			bpf_mtap2(ifp->if_bpf,
1027178354Ssam			    &sc->sc_tx_th, sc->sc_tx_th_len, m0);
1028109323Ssam		}
1029178354Ssam
1030127697Ssam		m_copydata(m0, 0, sizeof(struct ieee80211_frame),
1031127697Ssam		    (caddr_t)&frmhdr.wi_whdr);
1032127697Ssam		m_adj(m0, sizeof(struct ieee80211_frame));
1033127697Ssam		frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
1034170530Ssam		ieee80211_free_node(ni);
1035160991Ssam		if (wi_start_tx(ifp, &frmhdr, m0))
1036109323Ssam			continue;
1037178354Ssam
1038160991Ssam		sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
1039178354Ssam		ifp->if_opackets++;
1040160991Ssam	}
1041165087Ssam}
1042160991Ssam
1043165087Ssamstatic void
1044165087Ssamwi_start(struct ifnet *ifp)
1045165087Ssam{
1046165087Ssam	struct wi_softc	*sc = ifp->if_softc;
1047165087Ssam
1048165087Ssam	WI_LOCK(sc);
1049165087Ssam	wi_start_locked(ifp);
1050160991Ssam	WI_UNLOCK(sc);
1051160991Ssam}
1052160991Ssam
1053160991Ssamstatic int
1054160991Ssamwi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr, struct mbuf *m0)
1055160991Ssam{
1056160991Ssam	struct wi_softc	*sc = ifp->if_softc;
1057160991Ssam	int cur = sc->sc_txnext;
1058160991Ssam	int fid, off, error;
1059160991Ssam
1060160991Ssam	fid = sc->sc_txd[cur].d_fid;
1061160991Ssam	off = sizeof(*frmhdr);
1062160991Ssam	error = wi_write_bap(sc, fid, 0, frmhdr, sizeof(*frmhdr)) != 0
1063160991Ssam	     || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
1064160991Ssam	m_freem(m0);
1065160991Ssam	if (error) {
1066160991Ssam		ifp->if_oerrors++;
1067160991Ssam		return -1;
1068160991Ssam	}
1069160991Ssam	sc->sc_txd[cur].d_len = off;
1070160991Ssam	if (sc->sc_txcur == cur) {
1071160991Ssam		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
1072160991Ssam			if_printf(ifp, "xmit failed\n");
1073160991Ssam			sc->sc_txd[cur].d_len = 0;
1074160991Ssam			return -1;
1075109323Ssam		}
1076160991Ssam		sc->sc_tx_timer = 5;
1077160991Ssam	}
1078160991Ssam	return 0;
1079160991Ssam}
1080160991Ssam
1081160991Ssamstatic int
1082160991Ssamwi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m0,
1083160991Ssam	    const struct ieee80211_bpf_params *params)
1084160991Ssam{
1085160991Ssam	struct ieee80211com *ic = ni->ni_ic;
1086160991Ssam	struct ifnet *ifp = ic->ic_ifp;
1087160991Ssam	struct wi_softc	*sc = ifp->if_softc;
1088178354Ssam	struct ieee80211_key *k;
1089160991Ssam	struct ieee80211_frame *wh;
1090160991Ssam	struct wi_frame frmhdr;
1091160991Ssam	int cur;
1092160991Ssam	int rc = 0;
1093160991Ssam
1094160991Ssam	WI_LOCK(sc);
1095160991Ssam
1096160991Ssam	if (sc->wi_gone) {
1097160991Ssam		rc = ENETDOWN;
1098160991Ssam		goto out;
1099160991Ssam	}
1100160991Ssam	memset(&frmhdr, 0, sizeof(frmhdr));
1101160991Ssam	cur = sc->sc_txnext;
1102160991Ssam	if (sc->sc_txd[cur].d_len != 0) {
1103160991Ssam		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1104160991Ssam		rc = ENOBUFS;
1105160991Ssam		goto out;
1106160991Ssam	}
1107160991Ssam	m0->m_pkthdr.rcvif = NULL;
1108160991Ssam
1109160991Ssam	m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
1110160991Ssam	    (caddr_t)&frmhdr.wi_ehdr);
1111160991Ssam	frmhdr.wi_ehdr.ether_type = 0;
1112160991Ssam	wh = mtod(m0, struct ieee80211_frame *);
1113160991Ssam
1114160991Ssam	frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
1115160991Ssam	if (params && (params->ibp_flags & IEEE80211_BPF_NOACK))
1116160991Ssam		frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_ALTRTRY);
1117160991Ssam	if ((wh->i_fc[1] & IEEE80211_FC1_WEP) &&
1118178354Ssam	    (!params || (params && (params->ibp_flags & IEEE80211_BPF_CRYPTO)))) {
1119178354Ssam		k = ieee80211_crypto_encap(ni, m0);
1120178354Ssam		if (k == NULL) {
1121178354Ssam			rc = ENOMEM;
1122178354Ssam			goto out;
1123109323Ssam		}
1124178354Ssam		frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
112574838Salfred	}
1126178354Ssam	if (bpf_peers_present(ifp->if_bpf)) {
1127178354Ssam		sc->sc_tx_th.wt_rate = ni->ni_txrate;
1128178354Ssam		bpf_mtap2(ifp->if_bpf, &sc->sc_tx_th, sc->sc_tx_th_len, m0);
1129160991Ssam	}
1130160991Ssam	m_copydata(m0, 0, sizeof(struct ieee80211_frame),
1131160991Ssam	    (caddr_t)&frmhdr.wi_whdr);
1132160991Ssam	m_adj(m0, sizeof(struct ieee80211_frame));
1133160991Ssam	frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
1134168860Ssephe	if (wi_start_tx(ifp, &frmhdr, m0) < 0) {
1135168860Ssephe		m0 = NULL;
1136168860Ssephe		rc = EIO;
1137160991Ssam		goto out;
1138168860Ssephe	}
1139168860Ssephe	m0 = NULL;
114046492Swpaul
1141160991Ssam	sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
1142160991Ssamout:
1143109323Ssam	WI_UNLOCK(sc);
1144160991Ssam
1145168860Ssephe	if (m0 != NULL)
1146168860Ssephe		m_freem(m0);
1147168860Ssephe	ieee80211_free_node(ni);
1148160991Ssam	return rc;
114946492Swpaul}
115046492Swpaul
115188546Salfredstatic int
1152178354Ssamwi_reset(struct wi_softc *sc)
115346492Swpaul{
1154114124Simp#define WI_INIT_TRIES 3
1155178354Ssam	int i, error = 0;
1156112362Simp
1157178354Ssam	for (i = 0; i < WI_INIT_TRIES; i++) {
1158178354Ssam		error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0);
1159178354Ssam		if (error == 0)
116046492Swpaul			break;
1161109323Ssam		DELAY(WI_DELAY * 1000);
116246492Swpaul	}
1163114124Simp	sc->sc_reset = 1;
1164178354Ssam	if (i == WI_INIT_TRIES) {
1165178354Ssam		if_printf(sc->sc_ifp, "reset failed\n");
1166178354Ssam		return error;
116775373Salfred	}
116846492Swpaul
1169109323Ssam	CSR_WRITE_2(sc, WI_INT_EN, 0);
1170114124Simp	CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
117146492Swpaul
1172109323Ssam	/* Calibrate timer. */
1173114124Simp	wi_write_val(sc, WI_RID_TICK_TIME, 8);
1174114124Simp
1175178354Ssam	return 0;
1176109323Ssam#undef WI_INIT_TRIES
117746492Swpaul}
117846492Swpaul
117988546Salfredstatic void
1180165089Ssamwi_watchdog(void *arg)
118146492Swpaul{
1182165089Ssam	struct wi_softc	*sc = arg;
1183165089Ssam	struct ifnet *ifp = sc->sc_ifp;
118446492Swpaul
1185178354Ssam	WI_LOCK_ASSERT(sc);
1186178354Ssam
1187109323Ssam	if (!sc->sc_enabled)
1188109323Ssam		return;
118946492Swpaul
1190178354Ssam	if (sc->sc_tx_timer && --sc->sc_tx_timer == 0) {
1191178354Ssam		if_printf(ifp, "device timeout\n");
1192178354Ssam		ifp->if_oerrors++;
1193178354Ssam		wi_init_locked(ifp->if_softc);
1194178354Ssam		return;
119546492Swpaul	}
1196165089Ssam	callout_reset(&sc->sc_watchdog, hz, wi_watchdog, sc);
119746492Swpaul}
119846492Swpaul
119988546Salfredstatic int
1200109323Ssamwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
120146492Swpaul{
1202109323Ssam	struct wi_softc *sc = ifp->if_softc;
1203178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1204178354Ssam	struct ifreq *ifr = (struct ifreq *) data;
1205178354Ssam	int error = 0, startall = 0;
120646492Swpaul
1207109323Ssam	switch (cmd) {
120846492Swpaul	case SIOCSIFFLAGS:
1209178704Sthompsa		WI_LOCK(sc);
1210100876Simp		/*
1211101139Simp		 * Can't do promisc and hostap at the same time.  If all that's
1212101139Simp		 * changing is the promisc flag, try to short-circuit a call to
1213101139Simp		 * wi_init() by just setting PROMISC in the hardware.
1214100876Simp		 */
121546492Swpaul		if (ifp->if_flags & IFF_UP) {
1216109323Ssam			if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1217148887Srwatson			    ifp->if_drv_flags & IFF_DRV_RUNNING) {
1218178354Ssam				if ((ifp->if_flags ^ sc->sc_if_flags) & IFF_PROMISC) {
1219178354Ssam					wi_write_val(sc, WI_RID_PROMISC,
1220178354Ssam					    (ifp->if_flags & IFF_PROMISC) != 0);
1221101139Simp				} else {
1222178354Ssam					wi_init_locked(sc);
1223178354Ssam					startall = 1;
1224101139Simp				}
1225100876Simp			} else {
1226178354Ssam				wi_init_locked(sc);
1227178354Ssam				startall = 1;
1228100876Simp			}
122946492Swpaul		} else {
1230178354Ssam			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1231178354Ssam				wi_stop_locked(sc, 1);
1232123098Simp			sc->wi_gone = 0;
123346492Swpaul		}
1234109323Ssam		sc->sc_if_flags = ifp->if_flags;
1235178704Sthompsa		WI_UNLOCK(sc);
1236178704Sthompsa		if (startall)
1237178704Sthompsa			ieee80211_start_all(ic);
123846492Swpaul		break;
1239178354Ssam	case SIOCGIFMEDIA:
1240178354Ssam		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
124146492Swpaul		break;
1242178704Sthompsa	case SIOCGIFADDR:
1243178354Ssam		error = ether_ioctl(ifp, cmd, data);
1244170530Ssam		break;
1245178704Sthompsa	default:
1246178704Sthompsa		error = EINVAL;
1247178704Sthompsa		break;
1248170530Ssam	}
1249109323Ssam	return error;
1250109323Ssam}
125146492Swpaul
1252109323Ssamstatic void
1253109323Ssamwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1254109323Ssam{
1255178354Ssam	struct ieee80211vap *vap = ifp->if_softc;
1256178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1257178354Ssam	struct wi_softc *sc = ic->ic_ifp->if_softc;
1258109323Ssam	u_int16_t val;
1259109323Ssam	int rate, len;
126046492Swpaul
1261109323Ssam	len = sizeof(val);
1262178354Ssam	if (sc->sc_enabled &&
1263178354Ssam	    wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) == 0 &&
1264138571Ssam	    len == sizeof(val)) {
1265109323Ssam		/* convert to 802.11 rate */
1266138571Ssam		val = le16toh(val);
1267109323Ssam		rate = val * 2;
1268109323Ssam		if (sc->sc_firmware_type == WI_LUCENT) {
1269138571Ssam			if (rate == 10)
1270109323Ssam				rate = 11;	/* 5.5Mbps */
1271109323Ssam		} else {
1272109323Ssam			if (rate == 4*2)
1273109323Ssam				rate = 11;	/* 5.5Mbps */
1274109323Ssam			else if (rate == 8*2)
1275109323Ssam				rate = 22;	/* 11Mbps */
1276109323Ssam		}
1277178354Ssam		vap->iv_bss->ni_txrate = rate;
1278109323Ssam	}
1279178354Ssam	ieee80211_media_status(ifp, imr);
1280109323Ssam}
128146492Swpaul
1282109323Ssamstatic void
1283109323Ssamwi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1284109323Ssam{
1285147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1286178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1287178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1288178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
128998440Simp
1290109323Ssam	if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1291109323Ssam		return;
129246492Swpaul
1293109323Ssam	DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid)));
1294109323Ssam	DPRINTF(("%s ?\n", ether_sprintf(new_bssid)));
129546492Swpaul
1296109323Ssam	/* In promiscuous mode, the BSSID field is not a reliable
1297109323Ssam	 * indicator of the firmware's BSSID. Damp spurious
1298109323Ssam	 * change-of-BSSID indications.
1299109323Ssam	 */
1300109323Ssam	if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1301138571Ssam	    !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns,
1302138571Ssam	                 WI_MAX_FALSE_SYNS))
1303109323Ssam		return;
130446492Swpaul
1305139542Ssam	sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1);
1306170530Ssam#if 0
1307139542Ssam	/*
1308139542Ssam	 * XXX hack; we should create a new node with the new bssid
1309139542Ssam	 * and replace the existing ic_bss with it but since we don't
1310139542Ssam	 * process management frames to collect state we cheat by
1311139542Ssam	 * reusing the existing node as we know wi_newstate will be
1312139542Ssam	 * called and it will overwrite the node state.
1313139542Ssam	 */
1314139542Ssam	ieee80211_sta_join(ic, ieee80211_ref_node(ni));
1315170530Ssam#endif
1316109323Ssam}
131746492Swpaul
1318178354Ssamstatic __noinline void
1319109323Ssamwi_rx_intr(struct wi_softc *sc)
1320109323Ssam{
1321147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1322178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1323109323Ssam	struct wi_frame frmhdr;
1324109323Ssam	struct mbuf *m;
1325109323Ssam	struct ieee80211_frame *wh;
1326119150Ssam	struct ieee80211_node *ni;
1327109323Ssam	int fid, len, off, rssi;
1328109323Ssam	u_int8_t dir;
1329109323Ssam	u_int16_t status;
1330109323Ssam	u_int32_t rstamp;
133146611Swpaul
1332109323Ssam	fid = CSR_READ_2(sc, WI_RX_FID);
133346611Swpaul
1334109323Ssam	/* First read in the frame header */
1335109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1336109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1337109323Ssam		ifp->if_ierrors++;
1338109323Ssam		DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1339109323Ssam		return;
1340109323Ssam	}
134191695Simp
1342109323Ssam	/*
1343109323Ssam	 * Drop undecryptable or packets with receive errors here
1344109323Ssam	 */
1345109323Ssam	status = le16toh(frmhdr.wi_status);
1346109323Ssam	if (status & WI_STAT_ERRSTAT) {
1347109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1348109323Ssam		ifp->if_ierrors++;
1349109323Ssam		DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1350109323Ssam		return;
1351109323Ssam	}
1352109323Ssam	rssi = frmhdr.wi_rx_signal;
1353109323Ssam	rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1354109323Ssam	    le16toh(frmhdr.wi_rx_tstamp1);
135546492Swpaul
1356109323Ssam	len = le16toh(frmhdr.wi_dat_len);
1357109323Ssam	off = ALIGN(sizeof(struct ieee80211_frame));
135846563Swpaul
1359117855Ssam	/*
1360117855Ssam	 * Sometimes the PRISM2.x returns bogusly large frames. Except
1361117855Ssam	 * in monitor mode, just throw them away.
1362117855Ssam	 */
1363117855Ssam	if (off + len > MCLBYTES) {
1364117855Ssam		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1365117855Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1366117855Ssam			ifp->if_ierrors++;
1367117855Ssam			DPRINTF(("wi_rx_intr: oversized packet\n"));
1368117855Ssam			return;
1369117855Ssam		} else
1370117855Ssam			len = 0;
1371117855Ssam	}
1372117855Ssam
1373178354Ssam	if (off + len > MHLEN)
1374178354Ssam		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1375178354Ssam	else
1376178354Ssam		m = m_gethdr(M_DONTWAIT, MT_DATA);
1377109323Ssam	if (m == NULL) {
1378109323Ssam		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1379109323Ssam		ifp->if_ierrors++;
1380109323Ssam		DPRINTF(("wi_rx_intr: MGET failed\n"));
1381109323Ssam		return;
1382109323Ssam	}
1383109323Ssam	m->m_data += off - sizeof(struct ieee80211_frame);
1384109323Ssam	memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1385109323Ssam	wi_read_bap(sc, fid, sizeof(frmhdr),
1386109323Ssam	    m->m_data + sizeof(struct ieee80211_frame), len);
1387109323Ssam	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1388109323Ssam	m->m_pkthdr.rcvif = ifp;
138994405Simp
1390109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
139146492Swpaul
1392178354Ssam	if (bpf_peers_present(ifp->if_bpf)) {
1393123927Ssam		/* XXX replace divide by table */
1394123922Ssam		sc->sc_rx_th.wr_rate = frmhdr.wi_rx_rate / 5;
1395127148Sgreen		sc->sc_rx_th.wr_antsignal = frmhdr.wi_rx_signal;
1396127148Sgreen		sc->sc_rx_th.wr_antnoise = frmhdr.wi_rx_silence;
1397123927Ssam		sc->sc_rx_th.wr_flags = 0;
1398123927Ssam		if (frmhdr.wi_status & WI_STAT_PCF)
1399123927Ssam			sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_CFP;
1400178354Ssam		if (m->m_flags & M_WEP)
1401178354Ssam			sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_WEP;
1402178354Ssam		bpf_mtap2(ifp->if_bpf, &sc->sc_rx_th, sc->sc_rx_th_len, m);
140356965Swpaul	}
140456965Swpaul
1405109323Ssam	/* synchronize driver's BSSID with firmware's BSSID */
1406178354Ssam	wh = mtod(m, struct ieee80211_frame *);
1407109323Ssam	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1408109323Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1409109323Ssam		wi_sync_bssid(sc, wh->i_addr3);
141046492Swpaul
1411165088Ssam	WI_UNLOCK(sc);
1412165088Ssam
1413178354Ssam	ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1414178354Ssam	if (ni != NULL) {
1415178354Ssam		(void) ieee80211_input(ni, m, rssi, -95/*XXX*/, rstamp);
1416178354Ssam		ieee80211_free_node(ni);
1417178354Ssam	} else
1418178354Ssam		(void) ieee80211_input_all(ic, m, rssi, -95/*XXX*/, rstamp);
1419178354Ssam
1420165088Ssam	WI_LOCK(sc);
1421109323Ssam}
142246492Swpaul
1423178354Ssamstatic __noinline void
1424109323Ssamwi_tx_ex_intr(struct wi_softc *sc)
1425109323Ssam{
1426147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1427109323Ssam	struct wi_frame frmhdr;
1428109323Ssam	int fid;
142946492Swpaul
1430109323Ssam	fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1431109323Ssam	/* Read in the frame header */
1432109323Ssam	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1433109323Ssam		u_int16_t status = le16toh(frmhdr.wi_status);
1434109323Ssam		/*
1435109323Ssam		 * Spontaneous station disconnects appear as xmit
1436109323Ssam		 * errors.  Don't announce them and/or count them
1437109323Ssam		 * as an output error.
1438109323Ssam		 */
1439109323Ssam		if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1440109323Ssam			if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1441109323Ssam				if_printf(ifp, "tx failed");
1442109323Ssam				if (status & WI_TXSTAT_RET_ERR)
1443109323Ssam					printf(", retry limit exceeded");
1444109323Ssam				if (status & WI_TXSTAT_AGED_ERR)
1445109323Ssam					printf(", max transmit lifetime exceeded");
1446109323Ssam				if (status & WI_TXSTAT_DISCONNECT)
1447109323Ssam					printf(", port disconnected");
1448109323Ssam				if (status & WI_TXSTAT_FORM_ERR)
1449109323Ssam					printf(", invalid format (data len %u src %6D)",
1450109323Ssam						le16toh(frmhdr.wi_dat_len),
1451109323Ssam						frmhdr.wi_ehdr.ether_shost, ":");
1452109323Ssam				if (status & ~0xf)
1453109323Ssam					printf(", status=0x%x", status);
1454109323Ssam				printf("\n");
1455109323Ssam			}
1456109323Ssam			ifp->if_oerrors++;
1457109323Ssam		} else {
1458109323Ssam			DPRINTF(("port disconnected\n"));
1459109323Ssam			ifp->if_collisions++;	/* XXX */
1460109323Ssam		}
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
150088546Salfredstatic void
1501178354Ssamwi_status_connected(void *arg, int pending)
1502178354Ssam{
1503178354Ssam	struct ieee80211vap *vap = arg;
1504178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1505178354Ssam
1506178354Ssam	IEEE80211_LOCK(ic);
1507178354Ssam	WI_VAP(vap)->wv_newstate(vap, IEEE80211_S_RUN, 0);
1508178354Ssam	if (vap->iv_newstate_cb != NULL)
1509178354Ssam		vap->iv_newstate_cb(vap, IEEE80211_S_RUN, 0);
1510178354Ssam	IEEE80211_UNLOCK(ic);
1511178354Ssam}
1512178354Ssam
1513178354Ssamstatic void
1514178354Ssamwi_status_disconnected(void *arg, int pending)
1515178354Ssam{
1516178354Ssam	struct ieee80211vap *vap = arg;
1517178354Ssam
1518178354Ssam	if (vap->iv_state == IEEE80211_S_RUN) {
1519178354Ssam		vap->iv_stats.is_rx_deauth++;
1520178354Ssam		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
1521178354Ssam	}
1522178354Ssam}
1523178354Ssam
1524178354Ssamstatic void
1525178354Ssamwi_status_oor(void *arg, int pending)
1526178354Ssam{
1527178354Ssam	struct ieee80211com *ic = arg;
1528178354Ssam
1529178354Ssam	ieee80211_beacon_miss(ic);
1530178354Ssam}
1531178354Ssam
1532178354Ssamstatic void
1533178354Ssamwi_status_assoc_failed(void *arg, int pending)
1534178354Ssam{
1535178354Ssam	struct ieee80211vap *vap = arg;
1536178354Ssam
1537178354Ssam	ieee80211_new_state(vap, IEEE80211_S_SCAN, IEEE80211_SCAN_FAIL_TIMEOUT);
1538178354Ssam}
1539178354Ssam
1540178354Ssamstatic __noinline void
1541109323Ssamwi_info_intr(struct wi_softc *sc)
154294405Simp{
1543147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1544178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1545178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1546178354Ssam	struct wi_vap *wvp = WI_VAP(vap);
1547109323Ssam	int i, fid, len, off;
1548109323Ssam	u_int16_t ltbuf[2];
1549109323Ssam	u_int16_t stat;
1550109323Ssam	u_int32_t *ptr;
155194405Simp
1552109323Ssam	fid = CSR_READ_2(sc, WI_INFO_FID);
1553109323Ssam	wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
155494405Simp
1555109323Ssam	switch (le16toh(ltbuf[1])) {
1556109323Ssam	case WI_INFO_LINK_STAT:
1557109323Ssam		wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1558109323Ssam		DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1559109323Ssam		switch (le16toh(stat)) {
1560109323Ssam		case WI_INFO_LINK_STAT_CONNECTED:
1561178354Ssam			if (vap->iv_state == IEEE80211_S_RUN &&
1562178354Ssam			    vap->iv_opmode != IEEE80211_M_IBSS)
1563109323Ssam				break;
1564178354Ssam			/* fall thru... */
1565109323Ssam		case WI_INFO_LINK_STAT_AP_CHG:
1566178354Ssam			taskqueue_enqueue(taskqueue_swi, &wvp->wv_connected_task);
1567109323Ssam			break;
1568109323Ssam		case WI_INFO_LINK_STAT_AP_INR:
1569109323Ssam			break;
1570178354Ssam		case WI_INFO_LINK_STAT_DISCONNECTED:
1571178354Ssam			/* we dropped off the net; e.g. due to deauth/disassoc */
1572178354Ssam			taskqueue_enqueue(taskqueue_swi, &wvp->wv_disconnected_task);
1573178354Ssam			break;
1574109323Ssam		case WI_INFO_LINK_STAT_AP_OOR:
1575178354Ssam			/* XXX does this need to be per-vap? */
1576178354Ssam			taskqueue_enqueue(taskqueue_swi, &sc->sc_oor_task);
1577109323Ssam			break;
1578109323Ssam		case WI_INFO_LINK_STAT_ASSOC_FAILED:
1579178354Ssam			if (vap->iv_opmode == IEEE80211_M_STA)
1580178354Ssam				taskqueue_enqueue(taskqueue_swi,
1581178354Ssam				    &wvp->wv_assoc_failed_task);
1582109323Ssam			break;
1583109323Ssam		}
1584109323Ssam		break;
1585109323Ssam	case WI_INFO_COUNTERS:
1586109323Ssam		/* some card versions have a larger stats structure */
1587109323Ssam		len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1588109323Ssam		ptr = (u_int32_t *)&sc->sc_stats;
1589109323Ssam		off = sizeof(ltbuf);
1590109323Ssam		for (i = 0; i < len; i++, off += 2, ptr++) {
1591109323Ssam			wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1592109323Ssam#ifdef WI_HERMES_STATS_WAR
1593109323Ssam			if (stat & 0xf000)
1594109323Ssam				stat = ~stat;
1595109323Ssam#endif
1596109323Ssam			*ptr += stat;
1597109323Ssam		}
1598109323Ssam		ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1599109323Ssam		    sc->sc_stats.wi_tx_multi_retries +
1600109323Ssam		    sc->sc_stats.wi_tx_retry_limit;
1601109323Ssam		break;
1602109323Ssam	default:
1603109323Ssam		DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1604109323Ssam		    le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1605109323Ssam		break;
160694405Simp	}
1607109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1608109323Ssam}
160994405Simp
1610109323Ssamstatic int
1611109323Ssamwi_write_multi(struct wi_softc *sc)
1612109323Ssam{
1613147256Sbrooks	struct ifnet *ifp = sc->sc_ifp;
1614109323Ssam	int n;
1615109323Ssam	struct ifmultiaddr *ifma;
1616109323Ssam	struct wi_mcast mlist;
161794472Simp
1618109323Ssam	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1619109323Ssamallmulti:
1620109323Ssam		memset(&mlist, 0, sizeof(mlist));
1621109323Ssam		return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1622109323Ssam		    sizeof(mlist));
162394405Simp	}
162494405Simp
1625109323Ssam	n = 0;
1626148654Srwatson	IF_ADDR_LOCK(ifp);
1627109323Ssam	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1628109323Ssam		if (ifma->ifma_addr->sa_family != AF_LINK)
1629109323Ssam			continue;
1630109323Ssam		if (n >= 16)
1631109323Ssam			goto allmulti;
1632109323Ssam		IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1633109323Ssam		    (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1634109323Ssam		n++;
163594405Simp	}
1636148654Srwatson	IF_ADDR_UNLOCK(ifp);
1637109323Ssam	return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1638109323Ssam	    IEEE80211_ADDR_LEN * n);
163994405Simp}
164094405Simp
164194405Simpstatic void
1642178354Ssamwi_update_mcast(struct ifnet *ifp)
1643178354Ssam{
1644178354Ssam	wi_write_multi(ifp->if_softc);
1645178354Ssam}
1646178354Ssam
1647178354Ssamstatic void
1648109323Ssamwi_read_nicid(struct wi_softc *sc)
164946492Swpaul{
1650109323Ssam	struct wi_card_ident *id;
1651109323Ssam	char *p;
1652109323Ssam	int len;
1653109323Ssam	u_int16_t ver[4];
165446492Swpaul
1655109323Ssam	/* getting chip identity */
1656109323Ssam	memset(ver, 0, sizeof(ver));
1657109323Ssam	len = sizeof(ver);
1658109323Ssam	wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
165946492Swpaul
1660109323Ssam	sc->sc_firmware_type = WI_NOTYPE;
1661180919Simp	sc->sc_nic_id = le16toh(ver[0]);
1662109323Ssam	for (id = wi_card_ident; id->card_name != NULL; id++) {
1663180919Simp		if (sc->sc_nic_id == id->card_id) {
1664180919Simp			sc->sc_nic_name = id->card_name;
1665109323Ssam			sc->sc_firmware_type = id->firm_type;
1666109323Ssam			break;
1667109323Ssam		}
166867092Swpaul	}
1669109323Ssam	if (sc->sc_firmware_type == WI_NOTYPE) {
1670180919Simp		if (sc->sc_nic_id & 0x8000) {
1671109323Ssam			sc->sc_firmware_type = WI_INTERSIL;
1672180919Simp			sc->sc_nic_name = "Unknown Prism chip";
1673109323Ssam		} else {
1674109323Ssam			sc->sc_firmware_type = WI_LUCENT;
1675180919Simp			sc->sc_nic_name = "Unknown Lucent chip";
1676109323Ssam		}
167767092Swpaul	}
1678181210Simp	if (bootverbose)
1679181210Simp		device_printf(sc->sc_dev, "using %s\n", sc->sc_nic_name);
168046492Swpaul
1681109323Ssam	/* get primary firmware version (Only Prism chips) */
1682109323Ssam	if (sc->sc_firmware_type != WI_LUCENT) {
1683109323Ssam		memset(ver, 0, sizeof(ver));
1684109323Ssam		len = sizeof(ver);
1685109323Ssam		wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1686109323Ssam		sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1687109323Ssam		    le16toh(ver[3]) * 100 + le16toh(ver[1]);
168867092Swpaul	}
168946492Swpaul
1690109323Ssam	/* get station firmware version */
1691109323Ssam	memset(ver, 0, sizeof(ver));
1692109323Ssam	len = sizeof(ver);
1693109323Ssam	wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1694109323Ssam	sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1695109323Ssam	    le16toh(ver[3]) * 100 + le16toh(ver[1]);
1696109323Ssam	if (sc->sc_firmware_type == WI_INTERSIL &&
1697109323Ssam	    (sc->sc_sta_firmware_ver == 10102 ||
1698109323Ssam	     sc->sc_sta_firmware_ver == 20102)) {
1699109323Ssam		char ident[12];
1700109323Ssam		memset(ident, 0, sizeof(ident));
1701109323Ssam		len = sizeof(ident);
1702109323Ssam		/* value should be the format like "V2.00-11" */
1703109323Ssam		if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1704109323Ssam		    *(p = (char *)ident) >= 'A' &&
1705109323Ssam		    p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1706109323Ssam			sc->sc_firmware_type = WI_SYMBOL;
1707109323Ssam			sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1708109323Ssam			    (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1709109323Ssam			    (p[6] - '0') * 10 + (p[7] - '0');
171094405Simp		}
171194405Simp	}
1712180919Simp	if (bootverbose) {
1713180919Simp		device_printf(sc->sc_dev, "%s Firmware: ",
1714180919Simp		    wi_firmware_names[sc->sc_firmware_type]);
1715180919Simp		if (sc->sc_firmware_type != WI_LUCENT)	/* XXX */
1716180919Simp			printf("Primary (%u.%u.%u), ",
1717180919Simp			    sc->sc_pri_firmware_ver / 10000,
1718180919Simp			    (sc->sc_pri_firmware_ver % 10000) / 100,
1719180919Simp			    sc->sc_pri_firmware_ver % 100);
1720180919Simp		printf("Station (%u.%u.%u)\n",
1721180919Simp		    sc->sc_sta_firmware_ver / 10000,
1722180919Simp		    (sc->sc_sta_firmware_ver % 10000) / 100,
1723180919Simp		    sc->sc_sta_firmware_ver % 100);
1724180919Simp	}
1725109323Ssam}
172646492Swpaul
1727109323Ssamstatic int
1728109323Ssamwi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1729109323Ssam{
1730109323Ssam	struct wi_ssid ssid;
173146492Swpaul
1732109323Ssam	if (buflen > IEEE80211_NWID_LEN)
1733109323Ssam		return ENOBUFS;
1734109323Ssam	memset(&ssid, 0, sizeof(ssid));
1735109323Ssam	ssid.wi_len = htole16(buflen);
1736109323Ssam	memcpy(ssid.wi_ssid, buf, buflen);
1737109323Ssam	return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1738109323Ssam}
173946492Swpaul
1740109323Ssamstatic int
1741178354Ssamwi_write_txrate(struct wi_softc *sc, struct ieee80211vap *vap)
1742109323Ssam{
1743178354Ssam	static const uint16_t lucent_rates[12] = {
1744178354Ssam	    [ 0] = 3,	/* auto */
1745178354Ssam	    [ 1] = 1,	/* 1Mb/s */
1746178354Ssam	    [ 2] = 2,	/* 2Mb/s */
1747178354Ssam	    [ 5] = 4,	/* 5.5Mb/s */
1748178354Ssam	    [11] = 5	/* 11Mb/s */
1749178354Ssam	};
1750178354Ssam	static const uint16_t intersil_rates[12] = {
1751178354Ssam	    [ 0] = 0xf,	/* auto */
1752178354Ssam	    [ 1] = 0,	/* 1Mb/s */
1753178354Ssam	    [ 2] = 1,	/* 2Mb/s */
1754178354Ssam	    [ 5] = 2,	/* 5.5Mb/s */
1755178354Ssam	    [11] = 3,	/* 11Mb/s */
1756178354Ssam	};
1757178354Ssam	const uint16_t *rates = sc->sc_firmware_type == WI_LUCENT ?
1758178354Ssam	    lucent_rates : intersil_rates;
1759178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1760178354Ssam	const struct ieee80211_txparam *tp;
176146492Swpaul
1762178354Ssam	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
1763178354Ssam	return wi_write_val(sc, WI_RID_TX_RATE,
1764178354Ssam	    (tp->ucastrate == IEEE80211_FIXED_RATE_NONE ?
1765178354Ssam		rates[0] : rates[tp->ucastrate / 2]));
176646492Swpaul}
176746492Swpaul
1768109323Ssamstatic int
1769178354Ssamwi_write_wep(struct wi_softc *sc, struct ieee80211vap *vap)
177046492Swpaul{
1771109323Ssam	int error = 0;
1772109323Ssam	int i, keylen;
1773109323Ssam	u_int16_t val;
1774109323Ssam	struct wi_key wkey[IEEE80211_WEP_NKID];
177546492Swpaul
1776109323Ssam	switch (sc->sc_firmware_type) {
1777109323Ssam	case WI_LUCENT:
1778178354Ssam		val = (vap->iv_flags & IEEE80211_F_PRIVACY) ? 1 : 0;
1779109323Ssam		error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
1780109323Ssam		if (error)
1781109323Ssam			break;
1782178354Ssam		if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0)
1783138988Smdodd			break;
1784178354Ssam		error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, vap->iv_def_txkey);
1785138988Smdodd		if (error)
1786138988Smdodd			break;
1787138988Smdodd		memset(wkey, 0, sizeof(wkey));
1788138988Smdodd		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1789178354Ssam			keylen = vap->iv_nw_keys[i].wk_keylen;
1790138988Smdodd			wkey[i].wi_keylen = htole16(keylen);
1791178354Ssam			memcpy(wkey[i].wi_keydat, vap->iv_nw_keys[i].wk_key,
1792138988Smdodd			    keylen);
1793109323Ssam		}
1794138988Smdodd		error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
1795138988Smdodd		    wkey, sizeof(wkey));
1796150798Savatar		sc->sc_encryption = 0;
1797109323Ssam		break;
1798109323Ssam
1799109323Ssam	case WI_INTERSIL:
1800178354Ssam		val = HOST_ENCRYPT | HOST_DECRYPT;
1801178354Ssam		if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1802109323Ssam			/*
1803109323Ssam			 * ONLY HWB3163 EVAL-CARD Firmware version
1804109323Ssam			 * less than 0.8 variant2
1805109323Ssam			 *
1806109323Ssam			 *   If promiscuous mode disable, Prism2 chip
1807109323Ssam			 *  does not work with WEP .
1808109323Ssam			 * It is under investigation for details.
1809109323Ssam			 * (ichiro@netbsd.org)
1810109323Ssam			 */
1811178354Ssam			if (sc->sc_sta_firmware_ver < 802 ) {
1812109323Ssam				/* firm ver < 0.8 variant 2 */
1813109323Ssam				wi_write_val(sc, WI_RID_PROMISC, 1);
1814109323Ssam			}
1815109323Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE,
1816178354Ssam			    vap->iv_bss->ni_authmode);
1817178354Ssam			val |= PRIVACY_INVOKED;
1818109323Ssam		} else {
1819178354Ssam			wi_write_val(sc, WI_RID_CNFAUTHMODE, IEEE80211_AUTH_OPEN);
1820109323Ssam		}
1821109323Ssam		error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
1822109323Ssam		if (error)
1823109323Ssam			break;
1824150798Savatar		sc->sc_encryption = val;
1825138988Smdodd		if ((val & PRIVACY_INVOKED) == 0)
1826138988Smdodd			break;
1827178354Ssam		error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY, vap->iv_def_txkey);
1828109323Ssam		break;
1829109323Ssam	}
1830109323Ssam	return error;
183146492Swpaul}
183246492Swpaul
1833109323Ssamstatic int
1834109323Ssamwi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
183546492Swpaul{
1836178354Ssam	int i, s = 0;
1837178354Ssam
1838123098Simp	if (sc->wi_gone)
1839123098Simp		return (ENODEV);
1840123098Simp
1841109323Ssam	/* wait for the busy bit to clear */
1842123339Simp	for (i = sc->wi_cmd_count; i > 0; i--) {	/* 500ms */
1843116206Simp		if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
1844109323Ssam			break;
1845123098Simp		DELAY(1*1000);	/* 1ms */
1846109323Ssam	}
1847109323Ssam	if (i == 0) {
1848178354Ssam		device_printf(sc->sc_dev, "%s: busy bit won't clear, cmd 0x%x\n",
1849178354Ssam		   __func__, cmd);
1850123098Simp		sc->wi_gone = 1;
1851109323Ssam		return(ETIMEDOUT);
1852109323Ssam	}
185390580Sbrooks
1854109323Ssam	CSR_WRITE_2(sc, WI_PARAM0, val0);
1855109323Ssam	CSR_WRITE_2(sc, WI_PARAM1, val1);
1856109323Ssam	CSR_WRITE_2(sc, WI_PARAM2, val2);
1857109323Ssam	CSR_WRITE_2(sc, WI_COMMAND, cmd);
185890580Sbrooks
1859109323Ssam	if (cmd == WI_CMD_INI) {
1860109323Ssam		/* XXX: should sleep here. */
1861116206Simp		DELAY(100*1000);		/* 100ms delay for init */
1862109323Ssam	}
1863109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
1864109323Ssam		/*
1865109323Ssam		 * Wait for 'command complete' bit to be
1866109323Ssam		 * set in the event status register.
1867109323Ssam		 */
1868109323Ssam		s = CSR_READ_2(sc, WI_EVENT_STAT);
1869109323Ssam		if (s & WI_EV_CMD) {
1870109323Ssam			/* Ack the event and read result code. */
1871109323Ssam			s = CSR_READ_2(sc, WI_STATUS);
1872109323Ssam			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
1873109323Ssam			if (s & WI_STAT_CMD_RESULT) {
1874109323Ssam				return(EIO);
1875109323Ssam			}
1876109323Ssam			break;
187790580Sbrooks		}
1878109323Ssam		DELAY(WI_DELAY);
1879109323Ssam	}
188090580Sbrooks
1881109323Ssam	if (i == WI_TIMEOUT) {
1882178354Ssam		device_printf(sc->sc_dev, "%s: timeout on cmd 0x%04x; "
1883178354Ssam		    "event status 0x%04x\n", __func__, cmd, s);
1884123098Simp		if (s == 0xffff)
1885123098Simp			sc->wi_gone = 1;
1886109323Ssam		return(ETIMEDOUT);
188753702Swpaul	}
1888109323Ssam	return (0);
1889109323Ssam}
189053702Swpaul
1891109323Ssamstatic int
1892109323Ssamwi_seek_bap(struct wi_softc *sc, int id, int off)
1893109323Ssam{
1894109323Ssam	int i, status;
189590580Sbrooks
1896109323Ssam	CSR_WRITE_2(sc, WI_SEL0, id);
1897109323Ssam	CSR_WRITE_2(sc, WI_OFF0, off);
189890580Sbrooks
1899109323Ssam	for (i = 0; ; i++) {
1900109323Ssam		status = CSR_READ_2(sc, WI_OFF0);
1901109323Ssam		if ((status & WI_OFF_BUSY) == 0)
1902109323Ssam			break;
1903109323Ssam		if (i == WI_TIMEOUT) {
1904178354Ssam			device_printf(sc->sc_dev, "%s: timeout, id %x off %x\n",
1905178354Ssam			    __func__, id, off);
1906109323Ssam			sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
1907123098Simp			if (status == 0xffff)
1908123098Simp				sc->wi_gone = 1;
1909109323Ssam			return ETIMEDOUT;
1910109323Ssam		}
1911109323Ssam		DELAY(1);
191253702Swpaul	}
1913109323Ssam	if (status & WI_OFF_ERR) {
1914178354Ssam		device_printf(sc->sc_dev, "%s: error, id %x off %x\n",
1915178354Ssam		    __func__, id, off);
1916109323Ssam		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
1917109323Ssam		return EIO;
1918109323Ssam	}
1919109323Ssam	sc->sc_bap_id = id;
1920109323Ssam	sc->sc_bap_off = off;
1921109323Ssam	return 0;
192253702Swpaul}
192353702Swpaul
1924109323Ssamstatic int
1925109323Ssamwi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
192653702Swpaul{
1927109323Ssam	u_int16_t *ptr;
1928109323Ssam	int i, error, cnt;
192953702Swpaul
1930109323Ssam	if (buflen == 0)
1931109323Ssam		return 0;
1932109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1933109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
1934109323Ssam			return error;
193575219Salfred	}
1936109323Ssam	cnt = (buflen + 1) / 2;
1937109323Ssam	ptr = (u_int16_t *)buf;
1938109323Ssam	for (i = 0; i < cnt; i++)
1939109323Ssam		*ptr++ = CSR_READ_2(sc, WI_DATA0);
1940109323Ssam	sc->sc_bap_off += cnt * 2;
1941109323Ssam	return 0;
194253702Swpaul}
194353702Swpaul
1944109323Ssamstatic int
1945109323Ssamwi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
194653702Swpaul{
1947109323Ssam	u_int16_t *ptr;
1948109323Ssam	int i, error, cnt;
194946492Swpaul
1950109323Ssam	if (buflen == 0)
1951109323Ssam		return 0;
195246492Swpaul
1953109323Ssam	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1954109323Ssam		if ((error = wi_seek_bap(sc, id, off)) != 0)
1955109323Ssam			return error;
1956109323Ssam	}
1957109323Ssam	cnt = (buflen + 1) / 2;
1958109323Ssam	ptr = (u_int16_t *)buf;
1959109323Ssam	for (i = 0; i < cnt; i++)
1960109323Ssam		CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
1961109323Ssam	sc->sc_bap_off += cnt * 2;
1962109323Ssam
1963109323Ssam	return 0;
196446492Swpaul}
196553702Swpaul
1966109323Ssamstatic int
1967109323Ssamwi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
1968109323Ssam{
1969109323Ssam	int error, len;
1970109323Ssam	struct mbuf *m;
197153702Swpaul
1972109323Ssam	for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
1973109323Ssam		if (m->m_len == 0)
1974109323Ssam			continue;
197553702Swpaul
1976109323Ssam		len = min(m->m_len, totlen);
197753702Swpaul
1978109323Ssam		if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
1979109323Ssam			m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
1980109323Ssam			return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
1981109323Ssam			    totlen);
1982109323Ssam		}
198353702Swpaul
1984109323Ssam		if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
1985109323Ssam			return error;
198653702Swpaul
1987109323Ssam		off += m->m_len;
1988109323Ssam		totlen -= len;
1989109323Ssam	}
1990109323Ssam	return 0;
1991109323Ssam}
199253702Swpaul
1993109323Ssamstatic int
1994109323Ssamwi_alloc_fid(struct wi_softc *sc, int len, int *idp)
199553702Swpaul{
199653702Swpaul	int i;
199753702Swpaul
1998109323Ssam	if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
1999178354Ssam		device_printf(sc->sc_dev, "%s: failed to allocate %d bytes on NIC\n",
2000178354Ssam		    __func__, len);
2001109323Ssam		return ENOMEM;
200253702Swpaul	}
200353702Swpaul
2004109323Ssam	for (i = 0; i < WI_TIMEOUT; i++) {
2005109323Ssam		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
2006109323Ssam			break;
2007109323Ssam		DELAY(1);
200853702Swpaul	}
2009144167Ssam	if (i == WI_TIMEOUT) {
2010178354Ssam		device_printf(sc->sc_dev, "%s: timeout in alloc\n", __func__);
2011144167Ssam		return ETIMEDOUT;
2012144167Ssam	}
2013109323Ssam	*idp = CSR_READ_2(sc, WI_ALLOC_FID);
2014109323Ssam	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
2015109323Ssam	return 0;
2016109323Ssam}
201753702Swpaul
2018109323Ssamstatic int
2019109323Ssamwi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
2020109323Ssam{
2021109323Ssam	int error, len;
2022109323Ssam	u_int16_t ltbuf[2];
202353702Swpaul
2024109323Ssam	/* Tell the NIC to enter record read mode. */
2025109323Ssam	error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2026109323Ssam	if (error)
2027109323Ssam		return error;
202853702Swpaul
2029109323Ssam	error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2030109323Ssam	if (error)
2031109323Ssam		return error;
203253702Swpaul
2033109323Ssam	if (le16toh(ltbuf[1]) != rid) {
2034109323Ssam		device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n",
2035109323Ssam		    rid, le16toh(ltbuf[1]));
2036109323Ssam		return EIO;
203753702Swpaul	}
2038109323Ssam	len = (le16toh(ltbuf[0]) - 1) * 2;	 /* already got rid */
2039109323Ssam	if (*buflenp < len) {
2040109323Ssam		device_printf(sc->sc_dev, "record buffer is too small, "
2041109323Ssam		    "rid=%x, size=%d, len=%d\n",
2042109323Ssam		    rid, *buflenp, len);
2043109323Ssam		return ENOSPC;
204453702Swpaul	}
2045109323Ssam	*buflenp = len;
2046109323Ssam	return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2047109323Ssam}
204853702Swpaul
2049109323Ssamstatic int
2050109323Ssamwi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2051109323Ssam{
2052109323Ssam	int error;
2053109323Ssam	u_int16_t ltbuf[2];
205453702Swpaul
2055109323Ssam	ltbuf[0] = htole16((buflen + 1) / 2 + 1);	 /* includes rid */
2056109323Ssam	ltbuf[1] = htole16(rid);
205753702Swpaul
2058109323Ssam	error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2059178354Ssam	if (error) {
2060178354Ssam		device_printf(sc->sc_dev, "%s: bap0 write failure, rid 0x%x\n",
2061178354Ssam		    __func__, rid);
2062109323Ssam		return error;
2063178354Ssam	}
2064109323Ssam	error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2065178354Ssam	if (error) {
2066178354Ssam		device_printf(sc->sc_dev, "%s: bap1 write failure, rid 0x%x\n",
2067178354Ssam		    __func__, rid);
2068109323Ssam		return error;
2069178354Ssam	}
2070102206Simp
2071109323Ssam	return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
207253702Swpaul}
207377217Sphk
207488546Salfredstatic int
2075178354Ssamwi_write_appie(struct wi_softc *sc, int rid, const struct ieee80211_appie *ie)
207677217Sphk{
2077178354Ssam	/* NB: 42 bytes is probably ok to have on the stack */
2078178354Ssam	char buf[sizeof(uint16_t) + 40];
207977217Sphk
2080178354Ssam	if (ie->ie_len > 40)
2081178354Ssam		return EINVAL;
2082178354Ssam	/* NB: firmware requires 16-bit ie length before ie data */
2083178354Ssam	*(uint16_t *) buf = htole16(ie->ie_len);
2084178354Ssam	memcpy(buf + sizeof(uint16_t), ie->ie_data, ie->ie_len);
2085178354Ssam	return wi_write_rid(sc, rid, buf, ie->ie_len + sizeof(uint16_t));
208677217Sphk}
208777217Sphk
2088109323Ssamint
2089109323Ssamwi_alloc(device_t dev, int rid)
2090109323Ssam{
2091109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2092109323Ssam
2093109323Ssam	if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2094109323Ssam		sc->iobase_rid = rid;
2095109323Ssam		sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2096109323Ssam		    &sc->iobase_rid, 0, ~0, (1 << 6),
2097109323Ssam		    rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2098178354Ssam		if (sc->iobase == NULL) {
2099109323Ssam			device_printf(dev, "No I/O space?!\n");
2100178354Ssam			return ENXIO;
210198440Simp		}
2102109323Ssam
2103109323Ssam		sc->wi_io_addr = rman_get_start(sc->iobase);
2104109323Ssam		sc->wi_btag = rman_get_bustag(sc->iobase);
2105109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2106109323Ssam	} else {
2107109323Ssam		sc->mem_rid = rid;
2108127135Snjl		sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2109127135Snjl		    &sc->mem_rid, RF_ACTIVE);
2110178354Ssam		if (sc->mem == NULL) {
2111109323Ssam			device_printf(dev, "No Mem space on prism2.5?\n");
2112178354Ssam			return ENXIO;
211377217Sphk		}
2114109323Ssam
2115109323Ssam		sc->wi_btag = rman_get_bustag(sc->mem);
2116109323Ssam		sc->wi_bhandle = rman_get_bushandle(sc->mem);
211777217Sphk	}
211877217Sphk
2119109323Ssam	sc->irq_rid = 0;
2120127135Snjl	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2121127135Snjl	    RF_ACTIVE |
2122109323Ssam	    ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2123178354Ssam	if (sc->irq == NULL) {
2124109323Ssam		wi_free(dev);
2125109323Ssam		device_printf(dev, "No irq?!\n");
2126178354Ssam		return ENXIO;
212777217Sphk	}
2128109323Ssam
2129109323Ssam	sc->sc_dev = dev;
2130109323Ssam	sc->sc_unit = device_get_unit(dev);
2131178354Ssam	return 0;
213277217Sphk}
213393359Simp
2134109323Ssamvoid
2135109323Ssamwi_free(device_t dev)
2136109323Ssam{
2137109323Ssam	struct wi_softc	*sc = device_get_softc(dev);
2138109323Ssam
2139109323Ssam	if (sc->iobase != NULL) {
2140109323Ssam		bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2141109323Ssam		sc->iobase = NULL;
2142109323Ssam	}
2143109323Ssam	if (sc->irq != NULL) {
2144109323Ssam		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2145109323Ssam		sc->irq = NULL;
2146109323Ssam	}
2147109323Ssam	if (sc->mem != NULL) {
2148109323Ssam		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
2149109323Ssam		sc->mem = NULL;
2150109323Ssam	}
2151109323Ssam}
2152