xref: /dflybsd-src/sys/dev/netif/wi/if_wi.c (revision 0402ebbc7d4b6f34d02791995169d25c4aec3b15)
1 /*	$NetBSD: wi.c,v 1.109 2003/01/09 08:52:19 dyoung Exp $	*/
2 
3 /*
4  * Copyright (c) 1997, 1998, 1999
5  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Bill Paul.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $FreeBSD: src/sys/dev/wi/if_wi.c,v 1.166 2004/04/01 00:38:45 sam Exp $
35  * $DragonFly: src/sys/dev/netif/wi/if_wi.c,v 1.20 2005/02/11 22:25:56 joerg Exp $
36  */
37 
38 /*
39  * Lucent WaveLAN/IEEE 802.11 PCMCIA driver.
40  *
41  * Original FreeBSD driver written by Bill Paul <wpaul@ctr.columbia.edu>
42  * Electrical Engineering Department
43  * Columbia University, New York City
44  */
45 
46 /*
47  * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
48  * from Lucent. Unlike the older cards, the new ones are programmed
49  * entirely via a firmware-driven controller called the Hermes.
50  * Unfortunately, Lucent will not release the Hermes programming manual
51  * without an NDA (if at all). What they do release is an API library
52  * called the HCF (Hardware Control Functions) which is supposed to
53  * do the device-specific operations of a device driver for you. The
54  * publically available version of the HCF library (the 'HCF Light') is
55  * a) extremely gross, b) lacks certain features, particularly support
56  * for 802.11 frames, and c) is contaminated by the GNU Public License.
57  *
58  * This driver does not use the HCF or HCF Light at all. Instead, it
59  * programs the Hermes controller directly, using information gleaned
60  * from the HCF Light code and corresponding documentation.
61  *
62  * This driver supports the ISA, PCMCIA and PCI versions of the Lucent
63  * WaveLan cards (based on the Hermes chipset), as well as the newer
64  * Prism 2 chipsets with firmware from Intersil and Symbol.
65  */
66 
67 #define WI_HERMES_AUTOINC_WAR	/* Work around data write autoinc bug. */
68 #define WI_HERMES_STATS_WAR	/* Work around stats counter bug. */
69 
70 #include <sys/param.h>
71 #include <sys/endian.h>
72 #include <sys/systm.h>
73 #include <sys/sockio.h>
74 #include <sys/mbuf.h>
75 #include <sys/proc.h>
76 #include <sys/kernel.h>
77 #include <sys/socket.h>
78 #include <sys/module.h>
79 #include <sys/bus.h>
80 #include <sys/random.h>
81 #include <sys/syslog.h>
82 #include <sys/sysctl.h>
83 
84 #include <machine/bus.h>
85 #include <machine/resource.h>
86 #include <machine/clock.h>
87 #include <machine/atomic.h>
88 #include <sys/rman.h>
89 
90 #include <net/if.h>
91 #include <net/if_arp.h>
92 #include <net/ethernet.h>
93 #include <net/if_dl.h>
94 #include <net/if_media.h>
95 #include <net/if_types.h>
96 #include <net/ifq_var.h>
97 
98 #include <netproto/802_11/ieee80211_var.h>
99 #include <netproto/802_11/ieee80211_ioctl.h>
100 #include <netproto/802_11/ieee80211_radiotap.h>
101 #include <netproto/802_11/if_wavelan_ieee.h>
102 
103 #include <netinet/in.h>
104 #include <netinet/in_systm.h>
105 #include <netinet/in_var.h>
106 #include <netinet/ip.h>
107 #include <netinet/if_ether.h>
108 
109 #include <net/bpf.h>
110 
111 #include <dev/netif/wi/if_wireg.h>
112 #include <dev/netif/wi/if_wivar.h>
113 
114 static void wi_start(struct ifnet *);
115 static int  wi_reset(struct wi_softc *);
116 static void wi_watchdog(struct ifnet *);
117 static int  wi_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
118 static int  wi_media_change(struct ifnet *);
119 static void wi_media_status(struct ifnet *, struct ifmediareq *);
120 
121 static void wi_rx_intr(struct wi_softc *);
122 static void wi_tx_intr(struct wi_softc *);
123 static void wi_tx_ex_intr(struct wi_softc *);
124 static void wi_info_intr(struct wi_softc *);
125 
126 static int  wi_get_cfg(struct ifnet *, u_long, caddr_t, struct ucred *);
127 static int  wi_set_cfg(struct ifnet *, u_long, caddr_t);
128 static int  wi_write_txrate(struct wi_softc *);
129 static int  wi_write_wep(struct wi_softc *);
130 static int  wi_write_multi(struct wi_softc *);
131 static int  wi_alloc_fid(struct wi_softc *, int, int *);
132 static void wi_read_nicid(struct wi_softc *);
133 static int  wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
134 
135 static int  wi_cmd(struct wi_softc *, int, int, int, int);
136 static int  wi_seek_bap(struct wi_softc *, int, int);
137 static int  wi_read_bap(struct wi_softc *, int, int, void *, int);
138 static int  wi_write_bap(struct wi_softc *, int, int, void *, int);
139 static int  wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
140 static int  wi_read_rid(struct wi_softc *, int, void *, int *);
141 static int  wi_write_rid(struct wi_softc *, int, void *, int);
142 
143 static int  wi_newstate(struct ieee80211com *, enum ieee80211_state, int);
144 
145 static int  wi_scan_ap(struct wi_softc *, u_int16_t, u_int16_t);
146 static void wi_scan_result(struct wi_softc *, int, int);
147 
148 static void wi_dump_pkt(struct wi_frame *, struct ieee80211_node *, int rssi);
149 
150 static int wi_get_debug(struct wi_softc *, struct wi_req *);
151 static int wi_set_debug(struct wi_softc *, struct wi_req *);
152 
153 /* support to download firmware for symbol CF card */
154 static int wi_symbol_write_firm(struct wi_softc *, const void *, int,
155 		const void *, int);
156 static int wi_symbol_set_hcr(struct wi_softc *, int);
157 
158 static __inline int
159 wi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
160 {
161 
162 	val = htole16(val);
163 	return wi_write_rid(sc, rid, &val, sizeof(val));
164 }
165 
166 SYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters");
167 
168 static	struct timeval lasttxerror;	/* time of last tx error msg */
169 static	int curtxeps;			/* current tx error msgs/sec */
170 static	int wi_txerate = 0;		/* tx error rate: max msgs/sec */
171 SYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate,
172 	    0, "max tx error msgs/sec; 0 to disable msgs");
173 
174 #define	WI_DEBUG
175 #ifdef WI_DEBUG
176 static	int wi_debug = 0;
177 SYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug,
178 	    0, "control debugging printfs");
179 
180 #define	DPRINTF(X)	if (wi_debug) printf X
181 #define	DPRINTF2(X)	if (wi_debug > 1) printf X
182 #define	IFF_DUMPPKTS(_ifp) \
183 	(((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
184 #else
185 #define	DPRINTF(X)
186 #define	DPRINTF2(X)
187 #define	IFF_DUMPPKTS(_ifp)	0
188 #endif
189 
190 #define WI_INTRS	(WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
191 
192 struct wi_card_ident wi_card_ident[] = {
193 	/* CARD_ID			CARD_NAME		FIRM_TYPE */
194 	{ WI_NIC_LUCENT_ID,		WI_NIC_LUCENT_STR,	WI_LUCENT },
195 	{ WI_NIC_SONY_ID,		WI_NIC_SONY_STR,	WI_LUCENT },
196 	{ WI_NIC_LUCENT_EMB_ID,		WI_NIC_LUCENT_EMB_STR,	WI_LUCENT },
197 	{ WI_NIC_EVB2_ID,		WI_NIC_EVB2_STR,	WI_INTERSIL },
198 	{ WI_NIC_HWB3763_ID,		WI_NIC_HWB3763_STR,	WI_INTERSIL },
199 	{ WI_NIC_HWB3163_ID,		WI_NIC_HWB3163_STR,	WI_INTERSIL },
200 	{ WI_NIC_HWB3163B_ID,		WI_NIC_HWB3163B_STR,	WI_INTERSIL },
201 	{ WI_NIC_EVB3_ID,		WI_NIC_EVB3_STR,	WI_INTERSIL },
202 	{ WI_NIC_HWB1153_ID,		WI_NIC_HWB1153_STR,	WI_INTERSIL },
203 	{ WI_NIC_P2_SST_ID,		WI_NIC_P2_SST_STR,	WI_INTERSIL },
204 	{ WI_NIC_EVB2_SST_ID,		WI_NIC_EVB2_SST_STR,	WI_INTERSIL },
205 	{ WI_NIC_3842_EVA_ID,		WI_NIC_3842_EVA_STR,	WI_INTERSIL },
206 	{ WI_NIC_3842_PCMCIA_AMD_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
207 	{ WI_NIC_3842_PCMCIA_SST_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
208 	{ WI_NIC_3842_PCMCIA_ATL_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
209 	{ WI_NIC_3842_PCMCIA_ATS_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
210 	{ WI_NIC_3842_MINI_AMD_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
211 	{ WI_NIC_3842_MINI_SST_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
212 	{ WI_NIC_3842_MINI_ATL_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
213 	{ WI_NIC_3842_MINI_ATS_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
214 	{ WI_NIC_3842_PCI_AMD_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
215 	{ WI_NIC_3842_PCI_SST_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
216 	{ WI_NIC_3842_PCI_ATS_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
217 	{ WI_NIC_3842_PCI_ATL_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
218 	{ WI_NIC_P3_PCMCIA_AMD_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
219 	{ WI_NIC_P3_PCMCIA_SST_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
220 	{ WI_NIC_P3_PCMCIA_ATL_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
221 	{ WI_NIC_P3_PCMCIA_ATS_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
222 	{ WI_NIC_P3_MINI_AMD_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
223 	{ WI_NIC_P3_MINI_SST_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
224 	{ WI_NIC_P3_MINI_ATL_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
225 	{ WI_NIC_P3_MINI_ATS_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
226 	{ 0,	NULL,	0 },
227 };
228 
229 devclass_t wi_devclass;
230 
231 int
232 wi_attach(device_t dev)
233 {
234 	struct wi_softc	*sc = device_get_softc(dev);
235 	struct ieee80211com *ic = &sc->sc_ic;
236 	struct ifnet *ifp = &ic->ic_if;
237 	int i, nrates, buflen;
238 	u_int16_t val;
239 	u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
240 	struct ieee80211_rateset *rs;
241 	static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
242 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
243 	};
244 	int error;
245 
246 	/*
247 	 * NB: no locking is needed here; don't put it here
248 	 *     unless you can prove it!
249 	 */
250 	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
251 	    wi_intr, sc, &sc->wi_intrhand);
252 
253 	if (error) {
254 		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
255 		wi_free(dev);
256 		return (error);
257 	}
258 
259 	sc->wi_cmd_count = 500;
260 	/* Reset the NIC. */
261 	if (wi_reset(sc) != 0)
262 		return ENXIO;		/* XXX */
263 
264 	/*
265 	 * Read the station address.
266 	 * And do it twice. I've seen PRISM-based cards that return
267 	 * an error when trying to read it the first time, which causes
268 	 * the probe to fail.
269 	 */
270 	buflen = IEEE80211_ADDR_LEN;
271 	error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
272 	if (error != 0) {
273 		buflen = IEEE80211_ADDR_LEN;
274 		error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
275 	}
276 	if (error || IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) {
277 		if (error != 0)
278 			device_printf(dev, "mac read failed %d\n", error);
279 		else
280 			device_printf(dev, "mac read failed (all zeros)\n");
281 		wi_free(dev);
282 		return (error);
283 	}
284 
285 	/* Read NIC identification */
286 	wi_read_nicid(sc);
287 
288 	ifp->if_softc = sc;
289 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
290 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
291 	ifp->if_ioctl = wi_ioctl;
292 	ifp->if_start = wi_start;
293 	ifp->if_watchdog = wi_watchdog;
294 	ifp->if_init = wi_init;
295 	ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
296 	ifq_set_ready(&ifp->if_snd);
297 #ifdef DEVICE_POLLING
298 	ifp->if_capabilities |= IFCAP_POLLING;
299 #endif
300 	ifp->if_capenable = ifp->if_capabilities;
301 
302 	ic->ic_phytype = IEEE80211_T_DS;
303 	ic->ic_opmode = IEEE80211_M_STA;
304 	ic->ic_caps = IEEE80211_C_PMGT | IEEE80211_C_AHDEMO;
305 	ic->ic_state = IEEE80211_S_INIT;
306 
307 	/*
308 	 * Query the card for available channels and setup the
309 	 * channel table.  We assume these are all 11b channels.
310 	 */
311 	buflen = sizeof(val);
312 	if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
313 		val = htole16(0x1fff);	/* assume 1-11 */
314 	KASSERT(val != 0, ("wi_attach: no available channels listed!"));
315 
316 	val <<= 1;			/* shift for base 1 indices */
317 	for (i = 1; i < 16; i++) {
318 		if (isset((u_int8_t*)&val, i)) {
319 			ic->ic_channels[i].ic_freq =
320 				ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
321 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
322 		}
323 	}
324 
325 	/*
326 	 * Read the default channel from the NIC. This may vary
327 	 * depending on the country where the NIC was purchased, so
328 	 * we can't hard-code a default and expect it to work for
329 	 * everyone.
330 	 *
331 	 * If no channel is specified, let the 802.11 code select.
332 	 */
333 	buflen = sizeof(val);
334 	if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0) {
335 		val = le16toh(val);
336 		KASSERT(val < IEEE80211_CHAN_MAX &&
337 			ic->ic_channels[val].ic_flags != 0,
338 			("wi_attach: invalid own channel %u!", val));
339 		ic->ic_ibss_chan = &ic->ic_channels[val];
340 	} else {
341 		device_printf(dev,
342 			"WI_RID_OWN_CHNL failed, using first channel!\n");
343 		ic->ic_ibss_chan = &ic->ic_channels[0];
344 	}
345 
346 	/*
347 	 * Set flags based on firmware version.
348 	 */
349 	switch (sc->sc_firmware_type) {
350 	case WI_LUCENT:
351 		sc->sc_ntxbuf = 1;
352 		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
353 #ifdef WI_HERMES_AUTOINC_WAR
354 		/* XXX: not confirmed, but never seen for recent firmware */
355 		if (sc->sc_sta_firmware_ver <  40000) {
356 			sc->sc_flags |= WI_FLAGS_BUG_AUTOINC;
357 		}
358 #endif
359 		if (sc->sc_sta_firmware_ver >= 60000)
360 			sc->sc_flags |= WI_FLAGS_HAS_MOR;
361 		if (sc->sc_sta_firmware_ver >= 60006) {
362 			ic->ic_caps |= IEEE80211_C_IBSS;
363 			ic->ic_caps |= IEEE80211_C_MONITOR;
364 		}
365 		sc->sc_ibss_port = htole16(1);
366 
367 		sc->sc_min_rssi = WI_LUCENT_MIN_RSSI;
368 		sc->sc_max_rssi = WI_LUCENT_MAX_RSSI;
369 		sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
370 		break;
371 
372 	case WI_INTERSIL:
373 		sc->sc_ntxbuf = WI_NTXBUF;
374 		sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR;
375 		sc->sc_flags |= WI_FLAGS_HAS_ROAMING;
376 		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
377 		/*
378 		 * Old firmware are slow, so give peace a chance.
379 		 */
380 		if (sc->sc_sta_firmware_ver < 10000)
381 			sc->wi_cmd_count = 5000;
382 		if (sc->sc_sta_firmware_ver > 10101)
383 			sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
384 		if (sc->sc_sta_firmware_ver >= 800) {
385 			ic->ic_caps |= IEEE80211_C_IBSS;
386 			ic->ic_caps |= IEEE80211_C_MONITOR;
387 		}
388 		/*
389 		 * version 0.8.3 and newer are the only ones that are known
390 		 * to currently work.  Earlier versions can be made to work,
391 		 * at least according to the Linux driver.
392 		 */
393 		if (sc->sc_sta_firmware_ver >= 803)
394 			ic->ic_caps |= IEEE80211_C_HOSTAP;
395 		sc->sc_ibss_port = htole16(0);
396 
397 		sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
398 		sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
399 		sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
400 		break;
401 
402 	case WI_SYMBOL:
403 		sc->sc_ntxbuf = 1;
404 		sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY;
405 		if (sc->sc_sta_firmware_ver >= 25000)
406 			ic->ic_caps |= IEEE80211_C_IBSS;
407 		sc->sc_ibss_port = htole16(4);
408 
409 		sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
410 		sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
411 		sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
412 		break;
413 	}
414 
415 	/*
416 	 * Find out if we support WEP on this card.
417 	 */
418 	buflen = sizeof(val);
419 	if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
420 	    val != htole16(0))
421 		ic->ic_caps |= IEEE80211_C_WEP;
422 
423 	/* Find supported rates. */
424 	buflen = sizeof(ratebuf);
425 	rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
426 	if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
427 		nrates = le16toh(*(u_int16_t *)ratebuf);
428 		if (nrates > IEEE80211_RATE_MAXSIZE)
429 			nrates = IEEE80211_RATE_MAXSIZE;
430 		rs->rs_nrates = 0;
431 		for (i = 0; i < nrates; i++)
432 			if (ratebuf[2+i])
433 				rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i];
434 	} else {
435 		/* XXX fallback on error? */
436 		rs->rs_nrates = 0;
437 	}
438 
439 	buflen = sizeof(val);
440 	if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
441 	    wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
442 		sc->sc_dbm_offset = le16toh(val);
443 	}
444 
445 	sc->sc_max_datalen = 2304;
446 	sc->sc_system_scale = 1;
447 	sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN;
448 	sc->sc_roaming_mode = 1;
449 
450 	sc->sc_portnum = WI_DEFAULT_PORT;
451 	sc->sc_authtype = WI_DEFAULT_AUTHTYPE;
452 
453 	bzero(sc->sc_nodename, sizeof(sc->sc_nodename));
454 	sc->sc_nodelen = sizeof(WI_DEFAULT_NODENAME) - 1;
455 	bcopy(WI_DEFAULT_NODENAME, sc->sc_nodename, sc->sc_nodelen);
456 
457 	bzero(sc->sc_net_name, sizeof(sc->sc_net_name));
458 	bcopy(WI_DEFAULT_NETNAME, sc->sc_net_name,
459 	    sizeof(WI_DEFAULT_NETNAME) - 1);
460 
461 	/*
462 	 * Call MI attach routine.
463 	 */
464 	ieee80211_ifattach(ifp);
465 	/* override state transition method */
466 	sc->sc_newstate = ic->ic_newstate;
467 	ic->ic_newstate = wi_newstate;
468 	ieee80211_media_init(ifp, wi_media_change, wi_media_status);
469 
470 	bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
471 		sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
472 		&sc->sc_drvbpf);
473 	/*
474 	 * Initialize constant fields.
475 	 * XXX make header lengths a multiple of 32-bits so subsequent
476 	 *     headers are properly aligned; this is a kludge to keep
477 	 *     certain applications happy.
478 	 *
479 	 * NB: the channel is setup each time we transition to the
480 	 *     RUN state to avoid filling it in for each frame.
481 	 */
482 	sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t));
483 	sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len);
484 	sc->sc_tx_th.wt_ihdr.it_present = htole32(WI_TX_RADIOTAP_PRESENT);
485 
486 	sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t));
487 	sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len);
488 	sc->sc_rx_th.wr_ihdr.it_present = htole32(WI_RX_RADIOTAP_PRESENT);
489 
490 	return (0);
491 }
492 
493 int
494 wi_detach(device_t dev)
495 {
496 	struct wi_softc	*sc = device_get_softc(dev);
497 	struct ifnet *ifp = &sc->sc_ic.ic_if;
498 	WI_LOCK_DECL();
499 
500 	WI_LOCK(sc);
501 
502 	/* check if device was removed */
503 	sc->wi_gone |= !bus_child_present(dev);
504 
505 	wi_stop(ifp, 0);
506 
507 	ieee80211_ifdetach(ifp);
508 	WI_UNLOCK(sc);
509 	bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
510 	wi_free(dev);
511 	return (0);
512 }
513 
514 void
515 wi_shutdown(device_t dev)
516 {
517 	struct wi_softc *sc = device_get_softc(dev);
518 
519 	wi_stop(&sc->sc_if, 1);
520 }
521 
522 #ifdef DEVICE_POLLING
523 static void
524 wi_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
525 {
526 	struct wi_softc *sc = ifp->if_softc;
527 	uint16_t status;
528 
529 	if ((ifp->if_capenable & IFCAP_POLLING) == 0) {
530 		ether_poll_deregister(ifp);
531 		cmd = POLL_DEREGISTER;
532 	}
533 	if (cmd == POLL_DEREGISTER) {
534 		CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
535 		return;
536 	}
537 
538 	status = CSR_READ_2(sc, WI_EVENT_STAT);
539 
540 	if (status & WI_EV_RX)
541 		wi_rx_intr(sc);
542 	if (status & WI_EV_ALLOC)
543 		wi_tx_intr(sc);
544 	if (status & WI_EV_INFO)
545 		wi_info_intr(sc);
546 
547 	if (cmd == POLL_AND_CHECK_STATUS) {
548 		if (status & WI_EV_INFO)
549 			wi_info_intr(sc);
550 	}
551 
552 	if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
553 	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 && !ifq_is_empty(&ifp->if_snd))
554 		wi_start(ifp);
555 }
556 #endif /* DEVICE_POLLING */
557 
558 void
559 wi_intr(void *arg)
560 {
561 	struct wi_softc *sc = arg;
562 	struct ifnet *ifp = &sc->sc_ic.ic_if;
563 	u_int16_t status;
564 	WI_LOCK_DECL();
565 
566 #ifdef DEVICE_POLLING
567 	if (ifp->if_flags & IFF_POLLING)
568 		return;
569 	if ((ifp->if_capenable & IFCAP_POLLING) &&
570 	    (ether_poll_register(wi_poll, ifp))) {
571 		CSR_WRITE_2(sc, WI_INT_EN, 0);
572 		wi_poll(ifp, 0, 1);
573 		return;
574 	}
575 #endif /* DEVICE_POLLING */
576 
577 	if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) {
578 		CSR_WRITE_2(sc, WI_INT_EN, 0);
579 		CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
580 		return;
581 	}
582 
583 	WI_LOCK(sc);
584 
585 	/* Disable interrupts. */
586 	CSR_WRITE_2(sc, WI_INT_EN, 0);
587 
588 	status = CSR_READ_2(sc, WI_EVENT_STAT);
589 	if (status & WI_EV_RX)
590 		wi_rx_intr(sc);
591 	if (status & WI_EV_ALLOC)
592 		wi_tx_intr(sc);
593 	if (status & WI_EV_TX_EXC)
594 		wi_tx_ex_intr(sc);
595 	if (status & WI_EV_INFO)
596 		wi_info_intr(sc);
597 	if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
598 	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 &&
599 	    !ifq_is_empty(&ifp->if_snd))
600 		wi_start(ifp);
601 
602 	/* Re-enable interrupts. */
603 	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
604 
605 	WI_UNLOCK(sc);
606 
607 	return;
608 }
609 
610 void
611 wi_init(void *arg)
612 {
613 	struct wi_softc *sc = arg;
614 	struct ifnet *ifp = &sc->sc_if;
615 	struct ieee80211com *ic = &sc->sc_ic;
616 	struct wi_joinreq join;
617 	int i;
618 	int error = 0, wasenabled;
619 	struct ifaddr *ifa;
620 	struct sockaddr_dl *sdl;
621 	WI_LOCK_DECL();
622 
623 	WI_LOCK(sc);
624 
625 	if (sc->wi_gone) {
626 		WI_UNLOCK(sc);
627 		return;
628 	}
629 
630 	if ((wasenabled = sc->sc_enabled))
631 		wi_stop(ifp, 1);
632 	wi_reset(sc);
633 
634 	/* common 802.11 configuration */
635 	ic->ic_flags &= ~IEEE80211_F_IBSSON;
636 	sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
637 	switch (ic->ic_opmode) {
638 	case IEEE80211_M_STA:
639 		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS);
640 		break;
641 	case IEEE80211_M_IBSS:
642 		wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port);
643 		ic->ic_flags |= IEEE80211_F_IBSSON;
644 		break;
645 	case IEEE80211_M_AHDEMO:
646 		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
647 		break;
648 	case IEEE80211_M_HOSTAP:
649 		/*
650 		 * For PRISM cards, override the empty SSID, because in
651 		 * HostAP mode the controller will lock up otherwise.
652 		 */
653 		if (sc->sc_firmware_type == WI_INTERSIL &&
654 		    ic->ic_des_esslen == 0) {
655 			ic->ic_des_essid[0] = ' ';
656 			ic->ic_des_esslen = 1;
657 		}
658 		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP);
659 		break;
660 	case IEEE80211_M_MONITOR:
661 		if (sc->sc_firmware_type == WI_LUCENT)
662 			wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
663 		wi_cmd(sc, WI_CMD_DEBUG | (WI_TEST_MONITOR << 8), 0, 0, 0);
664 		break;
665 	}
666 
667 	/* Intersil interprets this RID as joining ESS even in IBSS mode */
668 	if (sc->sc_firmware_type == WI_LUCENT &&
669 	    (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0)
670 		wi_write_val(sc, WI_RID_CREATE_IBSS, 1);
671 	else
672 		wi_write_val(sc, WI_RID_CREATE_IBSS, 0);
673 	wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
674 	wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid,
675 	    ic->ic_des_esslen);
676 	wi_write_val(sc, WI_RID_OWN_CHNL,
677 		ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
678 	wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen);
679 
680 	ifa = ifaddr_byindex(ifp->if_index);
681 	sdl = (struct sockaddr_dl *) ifa->ifa_addr;
682 	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(sdl));
683 	wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN);
684 
685 	wi_write_val(sc, WI_RID_PM_ENABLED,
686 	    (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
687 
688 	/* not yet common 802.11 configuration */
689 	wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen);
690 	wi_write_val(sc, WI_RID_RTS_THRESH, ic->ic_rtsthreshold);
691 	if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
692 		wi_write_val(sc, WI_RID_FRAG_THRESH, ic->ic_fragthreshold);
693 
694 	/* driver specific 802.11 configuration */
695 	if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)
696 		wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale);
697 	if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
698 		wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode);
699 	if (sc->sc_flags & WI_FLAGS_HAS_MOR)
700 		wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven);
701 	wi_write_txrate(sc);
702 	wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen);
703 
704 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
705 	    sc->sc_firmware_type == WI_INTERSIL) {
706 		wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval);
707 		wi_write_val(sc, WI_RID_BASIC_RATE, 0x03);   /* 1, 2 */
708 		wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */
709 		wi_write_val(sc, WI_RID_DTIM_PERIOD, 1);
710 	}
711 
712 	/*
713 	 * Initialize promisc mode.
714 	 *	Being in the Host-AP mode causes a great
715 	 *	deal of pain if primisc mode is set.
716 	 *	Therefore we avoid confusing the firmware
717 	 *	and always reset promisc mode in Host-AP
718 	 *	mode.  Host-AP sees all the packets anyway.
719 	 */
720 	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
721 	    (ifp->if_flags & IFF_PROMISC) != 0) {
722 		wi_write_val(sc, WI_RID_PROMISC, 1);
723 	} else {
724 		wi_write_val(sc, WI_RID_PROMISC, 0);
725 	}
726 
727 	/* Configure WEP. */
728 	if (ic->ic_caps & IEEE80211_C_WEP)
729 		wi_write_wep(sc);
730 
731 	/* Set multicast filter. */
732 	wi_write_multi(sc);
733 
734 	/* Allocate fids for the card */
735 	if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
736 		sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
737 		if (sc->sc_firmware_type == WI_SYMBOL)
738 			sc->sc_buflen = 1585;	/* XXX */
739 		for (i = 0; i < sc->sc_ntxbuf; i++) {
740 			error = wi_alloc_fid(sc, sc->sc_buflen,
741 			    &sc->sc_txd[i].d_fid);
742 			if (error) {
743 				device_printf(sc->sc_dev,
744 				    "tx buffer allocation failed (error %u)\n",
745 				    error);
746 				goto out;
747 			}
748 			sc->sc_txd[i].d_len = 0;
749 		}
750 	}
751 	sc->sc_txcur = sc->sc_txnext = 0;
752 
753 	/* Enable desired port */
754 	wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
755 
756 	sc->sc_enabled = 1;
757 	ifp->if_flags |= IFF_RUNNING;
758 	ifp->if_flags &= ~IFF_OACTIVE;
759 	if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
760 	    ic->ic_opmode == IEEE80211_M_MONITOR ||
761 	    ic->ic_opmode == IEEE80211_M_HOSTAP)
762 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
763 
764 	/* Enable interrupts if not polling */
765 #ifdef DEVICE_POLLING
766 	if ((ifp->if_flags & IFF_POLLING) == 0)
767 #endif
768 		CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
769 
770 	if (!wasenabled &&
771 	    ic->ic_opmode == IEEE80211_M_HOSTAP &&
772 	    sc->sc_firmware_type == WI_INTERSIL) {
773 		/* XXX: some card need to be re-enabled for hostap */
774 		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
775 		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
776 	}
777 
778 	if (ic->ic_opmode == IEEE80211_M_STA &&
779 	    ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
780 	    ic->ic_des_chan != IEEE80211_CHAN_ANYC)) {
781 		memset(&join, 0, sizeof(join));
782 		if (ic->ic_flags & IEEE80211_F_DESBSSID)
783 			IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
784 		if (ic->ic_des_chan != IEEE80211_CHAN_ANYC)
785 			join.wi_chan = htole16(
786 				ieee80211_chan2ieee(ic, ic->ic_des_chan));
787 		/* Lucent firmware does not support the JOIN RID. */
788 		if (sc->sc_firmware_type != WI_LUCENT)
789 			wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
790 	}
791 
792 	WI_UNLOCK(sc);
793 	return;
794 out:
795 	if (error) {
796 		if_printf(ifp, "interface not running\n");
797 		wi_stop(ifp, 1);
798 	}
799 	WI_UNLOCK(sc);
800 	DPRINTF(("wi_init: return %d\n", error));
801 	return;
802 }
803 
804 void
805 wi_stop(struct ifnet *ifp, int disable)
806 {
807 	struct ieee80211com *ic = (struct ieee80211com *) ifp;
808 	struct wi_softc *sc = ifp->if_softc;
809 	WI_LOCK_DECL();
810 
811 	WI_LOCK(sc);
812 
813 	DELAY(100000);
814 
815 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
816 	if (sc->sc_enabled && !sc->wi_gone) {
817 		CSR_WRITE_2(sc, WI_INT_EN, 0);
818 		wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
819 		if (disable) {
820 #ifdef __NetBSD__
821 			if (sc->sc_disable)
822 				(*sc->sc_disable)(sc);
823 #endif
824 			sc->sc_enabled = 0;
825 		}
826 	} else if (sc->wi_gone && disable)	/* gone --> not enabled */
827 	    sc->sc_enabled = 0;
828 
829 	sc->sc_tx_timer = 0;
830 	sc->sc_scan_timer = 0;
831 	sc->sc_syn_timer = 0;
832 	sc->sc_false_syns = 0;
833 	sc->sc_naps = 0;
834 	ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
835 #ifdef DEVICE_POLLING
836 	ether_poll_deregister(ifp);
837 #endif
838 	ifp->if_timer = 0;
839 
840 	WI_UNLOCK(sc);
841 }
842 
843 static void
844 wi_start(struct ifnet *ifp)
845 {
846 	struct wi_softc	*sc = ifp->if_softc;
847 	struct ieee80211com *ic = &sc->sc_ic;
848 	struct ieee80211_node *ni;
849 	struct ieee80211_frame *wh;
850 	struct mbuf *m0;
851 	struct wi_frame frmhdr;
852 	int cur, fid, off, error;
853 	WI_LOCK_DECL();
854 
855 	WI_LOCK(sc);
856 
857 	if (sc->wi_gone) {
858 		WI_UNLOCK(sc);
859 		return;
860 	}
861 	if (sc->sc_flags & WI_FLAGS_OUTRANGE) {
862 		WI_UNLOCK(sc);
863 		return;
864 	}
865 
866 	memset(&frmhdr, 0, sizeof(frmhdr));
867 	cur = sc->sc_txnext;
868 	for (;;) {
869 		IF_POLL(&ic->ic_mgtq, m0);
870 		if (m0 != NULL) {
871 			if (sc->sc_txd[cur].d_len != 0) {
872 				ifp->if_flags |= IFF_OACTIVE;
873 				break;
874 			}
875 			IF_DEQUEUE(&ic->ic_mgtq, m0);
876 			/*
877 			 * Hack!  The referenced node pointer is in the
878 			 * rcvif field of the packet header.  This is
879 			 * placed there by ieee80211_mgmt_output because
880 			 * we need to hold the reference with the frame
881 			 * and there's no other way (other than packet
882 			 * tags which we consider too expensive to use)
883 			 * to pass it along.
884 			 */
885 			ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
886 			m0->m_pkthdr.rcvif = NULL;
887 
888 			m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
889 			    (caddr_t)&frmhdr.wi_ehdr);
890 			frmhdr.wi_ehdr.ether_type = 0;
891                         wh = mtod(m0, struct ieee80211_frame *);
892 		} else {
893 			if (ic->ic_state != IEEE80211_S_RUN)
894 				break;
895 			m0 = ifq_poll(&ifp->if_snd);
896 			if (m0 == NULL)
897 				break;
898 			if (sc->sc_txd[cur].d_len != 0) {
899 				ifp->if_flags |= IFF_OACTIVE;
900 				break;
901 			}
902 			m0 = ifq_dequeue(&ifp->if_snd);
903 			ifp->if_opackets++;
904 			m_copydata(m0, 0, ETHER_HDR_LEN,
905 			    (caddr_t)&frmhdr.wi_ehdr);
906 			BPF_MTAP(ifp, m0);
907 
908 			m0 = ieee80211_encap(ifp, m0, &ni);
909 			if (m0 == NULL) {
910 				ifp->if_oerrors++;
911 				continue;
912 			}
913                         wh = mtod(m0, struct ieee80211_frame *);
914 			if (ic->ic_flags & IEEE80211_F_WEPON)
915 				wh->i_fc[1] |= IEEE80211_FC1_WEP;
916 
917 		}
918 
919 		if (ic->ic_rawbpf != NULL)
920 			bpf_mtap(ic->ic_rawbpf, m0);
921 
922 		frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
923 		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
924 		    (wh->i_fc[1] & IEEE80211_FC1_WEP)) {
925 			if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) {
926 				ifp->if_oerrors++;
927 				if (ni && ni != ic->ic_bss)
928 					ieee80211_free_node(ic, ni);
929 				continue;
930 			}
931 			frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
932 		}
933 
934 		if (sc->sc_drvbpf) {
935 			sc->sc_tx_th.wt_rate =
936 				ni->ni_rates.rs_rates[ni->ni_txrate];
937 			bpf_ptap(sc->sc_drvbpf, m0, &sc->sc_tx_th,
938 				 sc->sc_tx_th_len);
939 		}
940 
941 		m_copydata(m0, 0, sizeof(struct ieee80211_frame),
942 		    (caddr_t)&frmhdr.wi_whdr);
943 		m_adj(m0, sizeof(struct ieee80211_frame));
944 		frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
945 		if (IFF_DUMPPKTS(ifp))
946 			wi_dump_pkt(&frmhdr, NULL, -1);
947 		fid = sc->sc_txd[cur].d_fid;
948 		off = sizeof(frmhdr);
949 		error = wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0
950 		     || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
951 		m_freem(m0);
952 		if (ni && ni != ic->ic_bss)
953 			ieee80211_free_node(ic, ni);
954 		if (error) {
955 			ifp->if_oerrors++;
956 			continue;
957 		}
958 		sc->sc_txd[cur].d_len = off;
959 		if (sc->sc_txcur == cur) {
960 			if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
961 				if_printf(ifp, "xmit failed\n");
962 				sc->sc_txd[cur].d_len = 0;
963 				continue;
964 			}
965 			sc->sc_tx_timer = 5;
966 			ifp->if_timer = 1;
967 		}
968 		sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
969 	}
970 
971 	WI_UNLOCK(sc);
972 }
973 
974 static int
975 wi_reset(struct wi_softc *sc)
976 {
977 	struct ieee80211com *ic = &sc->sc_ic;
978 	struct ifnet *ifp = &ic->ic_if;
979 #define WI_INIT_TRIES 3
980 	int i;
981 	int error = 0;
982 	int tries;
983 
984 	/* Symbol firmware cannot be initialized more than once */
985 	if (sc->sc_firmware_type == WI_SYMBOL && sc->sc_reset)
986 		return (0);
987 	if (sc->sc_firmware_type == WI_SYMBOL)
988 		tries = 1;
989 	else
990 		tries = WI_INIT_TRIES;
991 
992 	for (i = 0; i < tries; i++) {
993 		if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
994 			break;
995 		DELAY(WI_DELAY * 1000);
996 	}
997 	sc->sc_reset = 1;
998 
999 	if (i == tries) {
1000 		if_printf(ifp, "init failed\n");
1001 		return (error);
1002 	}
1003 
1004 	CSR_WRITE_2(sc, WI_INT_EN, 0);
1005 	CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
1006 
1007 	/* Calibrate timer. */
1008 	wi_write_val(sc, WI_RID_TICK_TIME, 8);
1009 
1010 	return (0);
1011 #undef WI_INIT_TRIES
1012 }
1013 
1014 static void
1015 wi_watchdog(struct ifnet *ifp)
1016 {
1017 	struct wi_softc	*sc = ifp->if_softc;
1018 
1019 	ifp->if_timer = 0;
1020 	if (!sc->sc_enabled)
1021 		return;
1022 
1023 	if (sc->sc_tx_timer) {
1024 		if (--sc->sc_tx_timer == 0) {
1025 			if_printf(ifp, "device timeout\n");
1026 			ifp->if_oerrors++;
1027 			wi_init(ifp->if_softc);
1028 			return;
1029 		}
1030 		ifp->if_timer = 1;
1031 	}
1032 
1033 	if (sc->sc_scan_timer) {
1034 		if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
1035 		    sc->sc_firmware_type == WI_INTERSIL) {
1036 			DPRINTF(("wi_watchdog: inquire scan\n"));
1037 			wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
1038 		}
1039 		if (sc->sc_scan_timer)
1040 			ifp->if_timer = 1;
1041 	}
1042 
1043 	if (sc->sc_syn_timer) {
1044 		if (--sc->sc_syn_timer == 0) {
1045 			struct ieee80211com *ic = (struct ieee80211com *) ifp;
1046 			DPRINTF2(("wi_watchdog: %d false syns\n",
1047 			    sc->sc_false_syns));
1048 			sc->sc_false_syns = 0;
1049 			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1050 			sc->sc_syn_timer = 5;
1051 		}
1052 		ifp->if_timer = 1;
1053 	}
1054 
1055 	/* TODO: rate control */
1056 	ieee80211_watchdog(ifp);
1057 }
1058 
1059 static int
1060 wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1061 {
1062 	struct wi_softc *sc = ifp->if_softc;
1063 	struct ieee80211com *ic = &sc->sc_ic;
1064 	struct ifreq *ifr = (struct ifreq *)data;
1065 	struct ieee80211req *ireq;
1066 	u_int8_t nodename[IEEE80211_NWID_LEN];
1067 	int error = 0;
1068 	struct wi_req wreq;
1069 	WI_LOCK_DECL();
1070 
1071 	WI_LOCK(sc);
1072 
1073 	if (sc->wi_gone) {
1074 		error = ENODEV;
1075 		goto out;
1076 	}
1077 
1078 	switch (cmd) {
1079 	case SIOCSIFFLAGS:
1080 		/*
1081 		 * Can't do promisc and hostap at the same time.  If all that's
1082 		 * changing is the promisc flag, try to short-circuit a call to
1083 		 * wi_init() by just setting PROMISC in the hardware.
1084 		 */
1085 		if (ifp->if_flags & IFF_UP) {
1086 			if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1087 			    ifp->if_flags & IFF_RUNNING) {
1088 				if (ifp->if_flags & IFF_PROMISC &&
1089 				    !(sc->sc_if_flags & IFF_PROMISC)) {
1090 					wi_write_val(sc, WI_RID_PROMISC, 1);
1091 				} else if (!(ifp->if_flags & IFF_PROMISC) &&
1092 				    sc->sc_if_flags & IFF_PROMISC) {
1093 					wi_write_val(sc, WI_RID_PROMISC, 0);
1094 				} else {
1095 					wi_init(sc);
1096 				}
1097 			} else {
1098 				wi_init(sc);
1099 			}
1100 		} else {
1101 			if (ifp->if_flags & IFF_RUNNING) {
1102 				wi_stop(ifp, 1);
1103 			}
1104 			sc->wi_gone = 0;
1105 		}
1106 		sc->sc_if_flags = ifp->if_flags;
1107 		error = 0;
1108 		break;
1109 	case SIOCADDMULTI:
1110 	case SIOCDELMULTI:
1111 		error = wi_write_multi(sc);
1112 		break;
1113 	case SIOCGIFGENERIC:
1114 		error = wi_get_cfg(ifp, cmd, data, cr);
1115 		break;
1116 	case SIOCSIFGENERIC:
1117 		error = suser_cred(cr, NULL_CRED_OKAY);
1118 		if (error)
1119 			break;
1120 		error = wi_set_cfg(ifp, cmd, data);
1121 		break;
1122 	case SIOCGPRISM2DEBUG:
1123 		error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1124 		if (error)
1125 			break;
1126 		if (!(ifp->if_flags & IFF_RUNNING) ||
1127 		    sc->sc_firmware_type == WI_LUCENT) {
1128 			error = EIO;
1129 			break;
1130 		}
1131 		error = wi_get_debug(sc, &wreq);
1132 		if (error == 0)
1133 			error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
1134 		break;
1135 	case SIOCSPRISM2DEBUG:
1136 		if ((error = suser_cred(cr, NULL_CRED_OKAY)))
1137 			goto out;
1138 		error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1139 		if (error)
1140 			break;
1141 		error = wi_set_debug(sc, &wreq);
1142 		break;
1143 	case SIOCG80211:
1144 		ireq = (struct ieee80211req *) data;
1145 		switch (ireq->i_type) {
1146 		case IEEE80211_IOC_STATIONNAME:
1147 			ireq->i_len = sc->sc_nodelen + 1;
1148 			error = copyout(sc->sc_nodename, ireq->i_data,
1149 					ireq->i_len);
1150 			break;
1151 		default:
1152 			error = ieee80211_ioctl(ifp, cmd, data, cr);
1153 			break;
1154 		}
1155 		break;
1156 	case SIOCS80211:
1157 		error = suser_cred(cr, NULL_CRED_OKAY);
1158 		if (error)
1159 			break;
1160 		ireq = (struct ieee80211req *) data;
1161 		switch (ireq->i_type) {
1162 		case IEEE80211_IOC_STATIONNAME:
1163 			if (ireq->i_val != 0 ||
1164 			    ireq->i_len > IEEE80211_NWID_LEN) {
1165 				error = EINVAL;
1166 				break;
1167 			}
1168 			memset(nodename, 0, IEEE80211_NWID_LEN);
1169 			error = copyin(ireq->i_data, nodename, ireq->i_len);
1170 			if (error)
1171 				break;
1172 			if (sc->sc_enabled) {
1173 				error = wi_write_ssid(sc, WI_RID_NODENAME,
1174 					nodename, ireq->i_len);
1175 				if (error)
1176 					break;
1177 			}
1178 			memcpy(sc->sc_nodename, nodename, IEEE80211_NWID_LEN);
1179 			sc->sc_nodelen = ireq->i_len;
1180 			break;
1181 		default:
1182 			error = ieee80211_ioctl(ifp, cmd, data, cr);
1183 			break;
1184 		}
1185 		break;
1186 	case SIOCSIFCAP:
1187 		ifp->if_capenable &= ~(IFCAP_POLLING);
1188 		ifp->if_capenable |= ifr->ifr_reqcap & (IFCAP_POLLING);
1189 		if (ifp->if_flags & IFF_RUNNING)
1190 			wi_init(sc);
1191 		break;
1192 	default:
1193 		error = ieee80211_ioctl(ifp, cmd, data, cr);
1194 		break;
1195 	}
1196 	if (error == ENETRESET) {
1197 		if (sc->sc_enabled)
1198 			wi_init(sc);	/* XXX no error return */
1199 		error = 0;
1200 	}
1201 out:
1202 	WI_UNLOCK(sc);
1203 
1204 	return (error);
1205 }
1206 
1207 static int
1208 wi_media_change(struct ifnet *ifp)
1209 {
1210 	struct wi_softc *sc = ifp->if_softc;
1211 	int error;
1212 
1213 	error = ieee80211_media_change(ifp);
1214 	if (error == ENETRESET) {
1215 		if (sc->sc_enabled)
1216 			wi_init(sc);	/* XXX no error return */
1217 		error = 0;
1218 	}
1219 	return error;
1220 }
1221 
1222 static void
1223 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1224 {
1225 	struct wi_softc *sc = ifp->if_softc;
1226 	struct ieee80211com *ic = &sc->sc_ic;
1227 	u_int16_t val;
1228 	int rate, len;
1229 
1230 	if (sc->wi_gone || !sc->sc_enabled) {
1231 		imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
1232 		imr->ifm_status = 0;
1233 		return;
1234 	}
1235 
1236 	imr->ifm_status = IFM_AVALID;
1237 	imr->ifm_active = IFM_IEEE80211;
1238 	if (ic->ic_state == IEEE80211_S_RUN &&
1239 	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
1240 		imr->ifm_status |= IFM_ACTIVE;
1241 	len = sizeof(val);
1242 	if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) != 0)
1243 		rate = 0;
1244 	else {
1245 		/* convert to 802.11 rate */
1246 		rate = val * 2;
1247 		if (sc->sc_firmware_type == WI_LUCENT) {
1248 			if (rate == 4 * 2)
1249 				rate = 11;	/* 5.5Mbps */
1250 			else if (rate == 5 * 2)
1251 				rate = 22;	/* 11Mbps */
1252 		} else {
1253 			if (rate == 4*2)
1254 				rate = 11;	/* 5.5Mbps */
1255 			else if (rate == 8*2)
1256 				rate = 22;	/* 11Mbps */
1257 		}
1258 	}
1259 	imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
1260 	switch (ic->ic_opmode) {
1261 	case IEEE80211_M_STA:
1262 		break;
1263 	case IEEE80211_M_IBSS:
1264 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
1265 		break;
1266 	case IEEE80211_M_AHDEMO:
1267 		imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1268 		break;
1269 	case IEEE80211_M_HOSTAP:
1270 		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1271 		break;
1272 	case IEEE80211_M_MONITOR:
1273 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
1274 		break;
1275 	}
1276 }
1277 
1278 static void
1279 wi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1280 {
1281 	struct ieee80211com *ic = &sc->sc_ic;
1282 	struct ieee80211_node *ni = ic->ic_bss;
1283 	struct ifnet *ifp = &ic->ic_if;
1284 
1285 	if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1286 		return;
1287 
1288 	DPRINTF(("wi_sync_bssid: bssid %6D -> ", ni->ni_bssid, ":"));
1289 	DPRINTF(("%6D ?\n", new_bssid, ":"));
1290 
1291 	/* In promiscuous mode, the BSSID field is not a reliable
1292 	 * indicator of the firmware's BSSID. Damp spurious
1293 	 * change-of-BSSID indications.
1294 	 */
1295 	if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1296 	    sc->sc_false_syns >= WI_MAX_FALSE_SYNS)
1297 		return;
1298 
1299 	ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1300 }
1301 
1302 static void
1303 wi_rx_monitor(struct wi_softc *sc, int fid)
1304 {
1305 	struct ieee80211com *ic = &sc->sc_ic;
1306 	struct ifnet *ifp = &ic->ic_if;
1307 	struct wi_frame *rx_frame;
1308 	struct mbuf *m;
1309 	int datlen, hdrlen;
1310 
1311 	/* first allocate mbuf for packet storage */
1312 	m = m_getcl(MB_DONTWAIT, MT_DATA, 0);
1313 	if (m == NULL) {
1314 		ifp->if_ierrors++;
1315 		return;
1316 	}
1317 
1318 	m->m_pkthdr.rcvif = ifp;
1319 
1320 	/* now read wi_frame first so we know how much data to read */
1321 	if (wi_read_bap(sc, fid, 0, mtod(m, caddr_t), sizeof(*rx_frame))) {
1322 		ifp->if_ierrors++;
1323 		goto done;
1324 	}
1325 
1326 	rx_frame = mtod(m, struct wi_frame *);
1327 
1328 	switch ((rx_frame->wi_status & WI_STAT_MAC_PORT) >> 8) {
1329 	case 7:
1330 		switch (rx_frame->wi_whdr.i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1331 		case IEEE80211_FC0_TYPE_DATA:
1332 			hdrlen = WI_DATA_HDRLEN;
1333 			datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1334 			break;
1335 		case IEEE80211_FC0_TYPE_MGT:
1336 			hdrlen = WI_MGMT_HDRLEN;
1337 			datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1338 			break;
1339 		case IEEE80211_FC0_TYPE_CTL:
1340 			/*
1341 			 * prism2 cards don't pass control packets
1342 			 * down properly or consistently, so we'll only
1343 			 * pass down the header.
1344 			 */
1345 			hdrlen = WI_CTL_HDRLEN;
1346 			datlen = 0;
1347 			break;
1348 		default:
1349 			if_printf(ifp, "received packet of unknown type "
1350 				"on port 7\n");
1351 			ifp->if_ierrors++;
1352 			goto done;
1353 		}
1354 		break;
1355 	case 0:
1356 		hdrlen = WI_DATA_HDRLEN;
1357 		datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1358 		break;
1359 	default:
1360 		if_printf(ifp, "received packet on invalid "
1361 		    "port (wi_status=0x%x)\n", rx_frame->wi_status);
1362 		ifp->if_ierrors++;
1363 		goto done;
1364 	}
1365 
1366 	if (hdrlen + datlen + 2 > MCLBYTES) {
1367 		if_printf(ifp, "oversized packet received "
1368 		    "(wi_dat_len=%d, wi_status=0x%x)\n",
1369 		    datlen, rx_frame->wi_status);
1370 		ifp->if_ierrors++;
1371 		goto done;
1372 	}
1373 
1374 	if (wi_read_bap(sc, fid, hdrlen, mtod(m, caddr_t) + hdrlen,
1375 	    datlen + 2) == 0) {
1376 		m->m_pkthdr.len = m->m_len = hdrlen + datlen;
1377 		ifp->if_ipackets++;
1378 		BPF_MTAP(ifp, m);	/* Handle BPF listeners. */
1379 	} else
1380 		ifp->if_ierrors++;
1381 done:
1382 	m_freem(m);
1383 }
1384 
1385 static void
1386 wi_rx_intr(struct wi_softc *sc)
1387 {
1388 	struct ieee80211com *ic = &sc->sc_ic;
1389 	struct ifnet *ifp = &ic->ic_if;
1390 	struct wi_frame frmhdr;
1391 	struct mbuf *m;
1392 	struct ieee80211_frame *wh;
1393 	struct ieee80211_node *ni;
1394 	int fid, len, off, rssi;
1395 	u_int8_t dir;
1396 	u_int16_t status;
1397 	u_int32_t rstamp;
1398 
1399 	fid = CSR_READ_2(sc, WI_RX_FID);
1400 
1401 	if (sc->wi_debug.wi_monitor) {
1402 		/*
1403 		 * If we are in monitor mode just
1404 		 * read the data from the device.
1405 		 */
1406 		wi_rx_monitor(sc, fid);
1407 		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1408 		return;
1409 	}
1410 
1411 	/* First read in the frame header */
1412 	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1413 		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1414 		ifp->if_ierrors++;
1415 		DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1416 		return;
1417 	}
1418 
1419 	if (IFF_DUMPPKTS(ifp))
1420 		wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal);
1421 
1422 	/*
1423 	 * Drop undecryptable or packets with receive errors here
1424 	 */
1425 	status = le16toh(frmhdr.wi_status);
1426 	if (status & WI_STAT_ERRSTAT) {
1427 		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1428 		ifp->if_ierrors++;
1429 		DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1430 		return;
1431 	}
1432 	rssi = frmhdr.wi_rx_signal;
1433 	rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1434 	    le16toh(frmhdr.wi_rx_tstamp1);
1435 
1436 	len = le16toh(frmhdr.wi_dat_len);
1437 	off = ALIGN(sizeof(struct ieee80211_frame));
1438 
1439 	/*
1440 	 * Sometimes the PRISM2.x returns bogusly large frames. Except
1441 	 * in monitor mode, just throw them away.
1442 	 */
1443 	if (off + len > MCLBYTES) {
1444 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1445 			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1446 			ifp->if_ierrors++;
1447 			DPRINTF(("wi_rx_intr: oversized packet\n"));
1448 			return;
1449 		} else
1450 			len = 0;
1451 	}
1452 
1453 	MGETHDR(m, MB_DONTWAIT, MT_DATA);
1454 	if (m == NULL) {
1455 		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1456 		ifp->if_ierrors++;
1457 		DPRINTF(("wi_rx_intr: MGET failed\n"));
1458 		return;
1459 	}
1460 	if (off + len > MHLEN) {
1461 		MCLGET(m, MB_DONTWAIT);
1462 		if ((m->m_flags & M_EXT) == 0) {
1463 			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1464 			m_freem(m);
1465 			ifp->if_ierrors++;
1466 			DPRINTF(("wi_rx_intr: MCLGET failed\n"));
1467 			return;
1468 		}
1469 	}
1470 
1471 	m->m_data += off - sizeof(struct ieee80211_frame);
1472 	memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1473 	wi_read_bap(sc, fid, sizeof(frmhdr),
1474 	    m->m_data + sizeof(struct ieee80211_frame), len);
1475 	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1476 	m->m_pkthdr.rcvif = ifp;
1477 
1478 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1479 
1480 	if (sc->sc_drvbpf) {
1481 		/* XXX replace divide by table */
1482 		sc->sc_rx_th.wr_rate = frmhdr.wi_rx_rate / 5;
1483 		sc->sc_rx_th.wr_antsignal = frmhdr.wi_rx_signal;
1484 		sc->sc_rx_th.wr_antnoise = frmhdr.wi_rx_silence;
1485 		sc->sc_rx_th.wr_flags = 0;
1486 		if (frmhdr.wi_status & WI_STAT_PCF)
1487 			sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_CFP;
1488 		bpf_ptap(sc->sc_drvbpf, m, &sc->sc_rx_th, sc->sc_rx_th_len);
1489 	}
1490 
1491 	wh = mtod(m, struct ieee80211_frame *);
1492 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1493 		/*
1494 		 * WEP is decrypted by hardware. Clear WEP bit
1495 		 * header for ieee80211_input().
1496 		 */
1497 		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1498 	}
1499 
1500 	/* synchronize driver's BSSID with firmware's BSSID */
1501 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1502 	if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1503 		wi_sync_bssid(sc, wh->i_addr3);
1504 
1505 	/*
1506 	 * Locate the node for sender, track state, and
1507 	 * then pass this node (referenced) up to the 802.11
1508 	 * layer for its use.  We are required to pass
1509 	 * something so we fallback to ic_bss when this frame
1510 	 * is from an unknown sender.
1511 	 */
1512 	if (ic->ic_opmode != IEEE80211_M_STA) {
1513 		ni = ieee80211_find_node(ic, wh->i_addr2);
1514 		if (ni == NULL)
1515 			ni = ieee80211_ref_node(ic->ic_bss);
1516 	} else
1517 		ni = ieee80211_ref_node(ic->ic_bss);
1518 	/*
1519 	 * Send frame up for processing.
1520 	 */
1521 	ieee80211_input(ifp, m, ni, rssi, rstamp);
1522 	/*
1523 	 * The frame may have caused the node to be marked for
1524 	 * reclamation (e.g. in response to a DEAUTH message)
1525 	 * so use free_node here instead of unref_node.
1526 	 */
1527 	if (ni == ic->ic_bss)
1528 		ieee80211_unref_node(&ni);
1529 	else
1530 		ieee80211_free_node(ic, ni);
1531 }
1532 
1533 static void
1534 wi_tx_ex_intr(struct wi_softc *sc)
1535 {
1536 	struct ieee80211com *ic = &sc->sc_ic;
1537 	struct ifnet *ifp = &ic->ic_if;
1538 	struct wi_frame frmhdr;
1539 	int fid;
1540 
1541 	fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1542 	/* Read in the frame header */
1543 	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1544 		u_int16_t status = le16toh(frmhdr.wi_status);
1545 
1546 		/*
1547 		 * Spontaneous station disconnects appear as xmit
1548 		 * errors.  Don't announce them and/or count them
1549 		 * as an output error.
1550 		 */
1551 		if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1552 			if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1553 				if_printf(ifp, "tx failed");
1554 				if (status & WI_TXSTAT_RET_ERR)
1555 					printf(", retry limit exceeded");
1556 				if (status & WI_TXSTAT_AGED_ERR)
1557 					printf(", max transmit lifetime exceeded");
1558 				if (status & WI_TXSTAT_DISCONNECT)
1559 					printf(", port disconnected");
1560 				if (status & WI_TXSTAT_FORM_ERR)
1561 					printf(", invalid format (data len %u src %6D)",
1562 						le16toh(frmhdr.wi_dat_len),
1563 						frmhdr.wi_ehdr.ether_shost, ":");
1564 				if (status & ~0xf)
1565 					printf(", status=0x%x", status);
1566 				printf("\n");
1567 			}
1568 			ifp->if_oerrors++;
1569 		} else {
1570 			DPRINTF(("port disconnected\n"));
1571 			ifp->if_collisions++;	/* XXX */
1572 		}
1573 	} else
1574 		DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
1575 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1576 }
1577 
1578 static void
1579 wi_tx_intr(struct wi_softc *sc)
1580 {
1581 	struct ieee80211com *ic = &sc->sc_ic;
1582 	struct ifnet *ifp = &ic->ic_if;
1583 	int fid, cur;
1584 
1585 	if (sc->wi_gone)
1586 		return;
1587 
1588 	fid = CSR_READ_2(sc, WI_ALLOC_FID);
1589 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1590 
1591 	cur = sc->sc_txcur;
1592 	if (sc->sc_txd[cur].d_fid != fid) {
1593 		if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
1594 		    fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
1595 		return;
1596 	}
1597 	sc->sc_tx_timer = 0;
1598 	sc->sc_txd[cur].d_len = 0;
1599 	sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
1600 	if (sc->sc_txd[cur].d_len == 0)
1601 		ifp->if_flags &= ~IFF_OACTIVE;
1602 	else {
1603 		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1604 		    0, 0)) {
1605 			if_printf(ifp, "xmit failed\n");
1606 			sc->sc_txd[cur].d_len = 0;
1607 		} else {
1608 			sc->sc_tx_timer = 5;
1609 			ifp->if_timer = 1;
1610 		}
1611 	}
1612 }
1613 
1614 static void
1615 wi_info_intr(struct wi_softc *sc)
1616 {
1617 	struct ieee80211com *ic = &sc->sc_ic;
1618 	struct ifnet *ifp = &ic->ic_if;
1619 	int i, fid, len, off;
1620 	u_int16_t ltbuf[2];
1621 	u_int16_t stat;
1622 	u_int32_t *ptr;
1623 
1624 	fid = CSR_READ_2(sc, WI_INFO_FID);
1625 	wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
1626 
1627 	switch (le16toh(ltbuf[1])) {
1628 
1629 	case WI_INFO_LINK_STAT:
1630 		wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1631 		DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1632 		switch (le16toh(stat)) {
1633 		case WI_INFO_LINK_STAT_CONNECTED:
1634 			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1635 			if (ic->ic_state == IEEE80211_S_RUN &&
1636 			    ic->ic_opmode != IEEE80211_M_IBSS)
1637 				break;
1638 			/* FALLTHROUGH */
1639 		case WI_INFO_LINK_STAT_AP_CHG:
1640 			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1641 			break;
1642 		case WI_INFO_LINK_STAT_AP_INR:
1643 			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1644 			break;
1645 		case WI_INFO_LINK_STAT_AP_OOR:
1646 			if (sc->sc_firmware_type == WI_SYMBOL &&
1647 			    sc->sc_scan_timer > 0) {
1648 				if (wi_cmd(sc, WI_CMD_INQUIRE,
1649 				    WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
1650 					sc->sc_scan_timer = 0;
1651 				break;
1652 			}
1653 			if (ic->ic_opmode == IEEE80211_M_STA)
1654 				sc->sc_flags |= WI_FLAGS_OUTRANGE;
1655 			break;
1656 		case WI_INFO_LINK_STAT_DISCONNECTED:
1657 		case WI_INFO_LINK_STAT_ASSOC_FAILED:
1658 			if (ic->ic_opmode == IEEE80211_M_STA)
1659 				ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1660 			break;
1661 		}
1662 		break;
1663 
1664 	case WI_INFO_COUNTERS:
1665 		/* some card versions have a larger stats structure */
1666 		len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1667 		ptr = (u_int32_t *)&sc->sc_stats;
1668 		off = sizeof(ltbuf);
1669 		for (i = 0; i < len; i++, off += 2, ptr++) {
1670 			wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1671 #ifdef WI_HERMES_STATS_WAR
1672 			if (stat & 0xf000)
1673 				stat = ~stat;
1674 #endif
1675 			*ptr += stat;
1676 		}
1677 		ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1678 		    sc->sc_stats.wi_tx_multi_retries +
1679 		    sc->sc_stats.wi_tx_retry_limit;
1680 		break;
1681 
1682 	case WI_INFO_SCAN_RESULTS:
1683 	case WI_INFO_HOST_SCAN_RESULTS:
1684 		wi_scan_result(sc, fid, le16toh(ltbuf[0]));
1685 		break;
1686 
1687 	default:
1688 		DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1689 		    le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1690 		break;
1691 	}
1692 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1693 }
1694 
1695 static int
1696 wi_write_multi(struct wi_softc *sc)
1697 {
1698 	struct ifnet *ifp = &sc->sc_ic.ic_if;
1699 	int n;
1700 	struct ifmultiaddr *ifma;
1701 	struct wi_mcast mlist;
1702 
1703 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1704 allmulti:
1705 		memset(&mlist, 0, sizeof(mlist));
1706 		return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1707 		    sizeof(mlist));
1708 	}
1709 
1710 	n = 0;
1711 	LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1712 		if (ifma->ifma_addr->sa_family != AF_LINK)
1713 			continue;
1714 		if (n >= 16)
1715 			goto allmulti;
1716 		IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1717 		    (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1718 		n++;
1719 	}
1720 	return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1721 	    IEEE80211_ADDR_LEN * n);
1722 }
1723 
1724 static void
1725 wi_read_nicid(struct wi_softc *sc)
1726 {
1727 	struct wi_card_ident *id;
1728 	char *p;
1729 	int len;
1730 	u_int16_t ver[4];
1731 
1732 	/* getting chip identity */
1733 	memset(ver, 0, sizeof(ver));
1734 	len = sizeof(ver);
1735 	wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1736 	device_printf(sc->sc_dev, "using ");
1737 
1738 	sc->sc_firmware_type = WI_NOTYPE;
1739 	for (id = wi_card_ident; id->card_name != NULL; id++) {
1740 		if (le16toh(ver[0]) == id->card_id) {
1741 			printf("%s", id->card_name);
1742 			sc->sc_firmware_type = id->firm_type;
1743 			break;
1744 		}
1745 	}
1746 	if (sc->sc_firmware_type == WI_NOTYPE) {
1747 		if (le16toh(ver[0]) & 0x8000) {
1748 			printf("Unknown PRISM2 chip");
1749 			sc->sc_firmware_type = WI_INTERSIL;
1750 		} else {
1751 			printf("Unknown Lucent chip");
1752 			sc->sc_firmware_type = WI_LUCENT;
1753 		}
1754 	}
1755 
1756 	/* get primary firmware version (Only Prism chips) */
1757 	if (sc->sc_firmware_type != WI_LUCENT) {
1758 		memset(ver, 0, sizeof(ver));
1759 		len = sizeof(ver);
1760 		wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1761 		sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1762 		    le16toh(ver[3]) * 100 + le16toh(ver[1]);
1763 	}
1764 
1765 	/* get station firmware version */
1766 	memset(ver, 0, sizeof(ver));
1767 	len = sizeof(ver);
1768 	wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1769 	sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1770 	    le16toh(ver[3]) * 100 + le16toh(ver[1]);
1771 	if (sc->sc_firmware_type == WI_INTERSIL &&
1772 	    (sc->sc_sta_firmware_ver == 10102 ||
1773 	     sc->sc_sta_firmware_ver == 20102)) {
1774 		char ident[12];
1775 		memset(ident, 0, sizeof(ident));
1776 		len = sizeof(ident);
1777 		/* value should be the format like "V2.00-11" */
1778 		if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1779 		    *(p = (char *)ident) >= 'A' &&
1780 		    p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1781 			sc->sc_firmware_type = WI_SYMBOL;
1782 			sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1783 			    (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1784 			    (p[6] - '0') * 10 + (p[7] - '0');
1785 		}
1786 	}
1787 	printf("\n");
1788 	device_printf(sc->sc_dev, "%s Firmware: ",
1789 	     sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
1790 	    (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
1791 	if (sc->sc_firmware_type != WI_LUCENT)	/* XXX */
1792 		printf("Primary (%u.%u.%u), ",
1793 		    sc->sc_pri_firmware_ver / 10000,
1794 		    (sc->sc_pri_firmware_ver % 10000) / 100,
1795 		    sc->sc_pri_firmware_ver % 100);
1796 	printf("Station (%u.%u.%u)\n",
1797 	    sc->sc_sta_firmware_ver / 10000,
1798 	    (sc->sc_sta_firmware_ver % 10000) / 100,
1799 	    sc->sc_sta_firmware_ver % 100);
1800 }
1801 
1802 static int
1803 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1804 {
1805 	struct wi_ssid ssid;
1806 
1807 	if (buflen > IEEE80211_NWID_LEN)
1808 		return ENOBUFS;
1809 	memset(&ssid, 0, sizeof(ssid));
1810 	ssid.wi_len = htole16(buflen);
1811 	memcpy(ssid.wi_ssid, buf, buflen);
1812 	return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1813 }
1814 
1815 static int
1816 wi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1817 {
1818 	struct wi_softc *sc = ifp->if_softc;
1819 	struct ieee80211com *ic = &sc->sc_ic;
1820 	struct ifreq *ifr = (struct ifreq *)data;
1821 	struct wi_req wreq;
1822 	struct wi_scan_res *res;
1823 	size_t reslen;
1824 	int len, n, error, mif, val, off, i;
1825 
1826 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1827 	if (error)
1828 		return error;
1829 	len = (wreq.wi_len - 1) * 2;
1830 	if (len < sizeof(u_int16_t))
1831 		return ENOSPC;
1832 	if (len > sizeof(wreq.wi_val))
1833 		len = sizeof(wreq.wi_val);
1834 
1835 	switch (wreq.wi_type) {
1836 
1837 	case WI_RID_IFACE_STATS:
1838 		memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
1839 		if (len < sizeof(sc->sc_stats))
1840 			error = ENOSPC;
1841 		else
1842 			len = sizeof(sc->sc_stats);
1843 		break;
1844 
1845 	case WI_RID_ENCRYPTION:
1846 	case WI_RID_TX_CRYPT_KEY:
1847 	case WI_RID_DEFLT_CRYPT_KEYS:
1848 	case WI_RID_TX_RATE:
1849 		return ieee80211_cfgget(ifp, cmd, data, cr);
1850 
1851 	case WI_RID_MICROWAVE_OVEN:
1852 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
1853 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1854 			    &len);
1855 			break;
1856 		}
1857 		wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
1858 		len = sizeof(u_int16_t);
1859 		break;
1860 
1861 	case WI_RID_DBM_ADJUST:
1862 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
1863 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1864 			    &len);
1865 			break;
1866 		}
1867 		wreq.wi_val[0] = htole16(sc->sc_dbm_offset);
1868 		len = sizeof(u_int16_t);
1869 		break;
1870 
1871 	case WI_RID_ROAMING_MODE:
1872 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
1873 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1874 			    &len);
1875 			break;
1876 		}
1877 		wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
1878 		len = sizeof(u_int16_t);
1879 		break;
1880 
1881 	case WI_RID_SYSTEM_SCALE:
1882 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
1883 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1884 			    &len);
1885 			break;
1886 		}
1887 		wreq.wi_val[0] = htole16(sc->sc_system_scale);
1888 		len = sizeof(u_int16_t);
1889 		break;
1890 
1891 	case WI_RID_FRAG_THRESH:
1892 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
1893 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1894 			    &len);
1895 			break;
1896 		}
1897 		wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
1898 		len = sizeof(u_int16_t);
1899 		break;
1900 
1901 	case WI_RID_READ_APS:
1902 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1903 			return ieee80211_cfgget(ifp, cmd, data, cr);
1904 		if (sc->sc_scan_timer > 0) {
1905 			error = EINPROGRESS;
1906 			break;
1907 		}
1908 		n = sc->sc_naps;
1909 		if (len < sizeof(n)) {
1910 			error = ENOSPC;
1911 			break;
1912 		}
1913 		if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
1914 			n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
1915 		len = sizeof(n) + sizeof(struct wi_apinfo) * n;
1916 		memcpy(wreq.wi_val, &n, sizeof(n));
1917 		memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
1918 		    sizeof(struct wi_apinfo) * n);
1919 		break;
1920 
1921 	case WI_RID_PRISM2:
1922 		wreq.wi_val[0] = sc->sc_firmware_type != WI_LUCENT;
1923 		len = sizeof(u_int16_t);
1924 		break;
1925 
1926 	case WI_RID_MIF:
1927 		mif = wreq.wi_val[0];
1928 		error = wi_cmd(sc, WI_CMD_READMIF, mif, 0, 0);
1929 		val = CSR_READ_2(sc, WI_RESP0);
1930 		wreq.wi_val[0] = val;
1931 		len = sizeof(u_int16_t);
1932 		break;
1933 
1934 	case WI_RID_ZERO_CACHE:
1935 	case WI_RID_PROCFRAME:		/* ignore for compatibility */
1936 		/* XXX ??? */
1937 		break;
1938 
1939 	case WI_RID_READ_CACHE:
1940 		return ieee80211_cfgget(ifp, cmd, data, cr);
1941 
1942 	case WI_RID_SCAN_RES:		/* compatibility interface */
1943 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1944 			return ieee80211_cfgget(ifp, cmd, data, cr);
1945 		if (sc->sc_scan_timer > 0) {
1946 			error = EINPROGRESS;
1947 			break;
1948 		}
1949 		n = sc->sc_naps;
1950 		if (sc->sc_firmware_type == WI_LUCENT) {
1951 			off = 0;
1952 			reslen = WI_WAVELAN_RES_SIZE;
1953 		} else {
1954 			off = sizeof(struct wi_scan_p2_hdr);
1955 			reslen = WI_PRISM2_RES_SIZE;
1956 		}
1957 		if (len < off + reslen * n)
1958 			n = (len - off) / reslen;
1959 		len = off + reslen * n;
1960 		if (off != 0) {
1961 			struct wi_scan_p2_hdr *p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1962 			/*
1963 			 * Prepend Prism-specific header.
1964 			 */
1965 			if (len < sizeof(struct wi_scan_p2_hdr)) {
1966 				error = ENOSPC;
1967 				break;
1968 			}
1969 			p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1970 			p2->wi_rsvd = 0;
1971 			p2->wi_reason = n;	/* XXX */
1972 		}
1973 		for (i = 0; i < n; i++, off += reslen) {
1974 			const struct wi_apinfo *ap = &sc->sc_aps[i];
1975 
1976 			res = (struct wi_scan_res *)((char *)wreq.wi_val + off);
1977 			res->wi_chan = ap->channel;
1978 			res->wi_noise = ap->noise;
1979 			res->wi_signal = ap->signal;
1980 			IEEE80211_ADDR_COPY(res->wi_bssid, ap->bssid);
1981 			res->wi_interval = ap->interval;
1982 			res->wi_capinfo = ap->capinfo;
1983 			res->wi_ssid_len = ap->namelen;
1984 			memcpy(res->wi_ssid, ap->name,
1985 				IEEE80211_NWID_LEN);
1986 			if (sc->sc_firmware_type != WI_LUCENT) {
1987 				/* XXX not saved from Prism cards */
1988 				memset(res->wi_srates, 0,
1989 					sizeof(res->wi_srates));
1990 				res->wi_rate = ap->rate;
1991 				res->wi_rsvd = 0;
1992 			}
1993 		}
1994 		break;
1995 
1996 	default:
1997 		if (sc->sc_enabled) {
1998 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1999 			    &len);
2000 			break;
2001 		}
2002 		switch (wreq.wi_type) {
2003 		case WI_RID_MAX_DATALEN:
2004 			wreq.wi_val[0] = htole16(sc->sc_max_datalen);
2005 			len = sizeof(u_int16_t);
2006 			break;
2007 		case WI_RID_RTS_THRESH:
2008 			wreq.wi_val[0] = htole16(ic->ic_rtsthreshold);
2009 			len = sizeof(u_int16_t);
2010 			break;
2011 		case WI_RID_CNFAUTHMODE:
2012 			wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
2013 			len = sizeof(u_int16_t);
2014 			break;
2015 		case WI_RID_NODENAME:
2016 			if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
2017 				error = ENOSPC;
2018 				break;
2019 			}
2020 			len = sc->sc_nodelen + sizeof(u_int16_t);
2021 			wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
2022 			memcpy(&wreq.wi_val[1], sc->sc_nodename,
2023 			    sc->sc_nodelen);
2024 			break;
2025 		default:
2026 			return ieee80211_cfgget(ifp, cmd, data, cr);
2027 		}
2028 		break;
2029 	}
2030 	if (error)
2031 		return error;
2032 	wreq.wi_len = (len + 1) / 2 + 1;
2033 	return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
2034 }
2035 
2036 static int
2037 wi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
2038 {
2039 	struct wi_softc *sc = ifp->if_softc;
2040 	struct ieee80211com *ic = &sc->sc_ic;
2041 	struct ifreq *ifr = (struct ifreq *)data;
2042 	struct wi_req wreq;
2043 	struct mbuf *m;
2044 	int i, len, error, mif, val;
2045 	struct ieee80211_rateset *rs;
2046 
2047 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2048 	if (error)
2049 		return error;
2050 	len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0;
2051 	switch (wreq.wi_type) {
2052 	case WI_RID_DBM_ADJUST:
2053 		return ENODEV;
2054 
2055 	case WI_RID_NODENAME:
2056 		if (le16toh(wreq.wi_val[0]) * 2 > len ||
2057 		    le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
2058 			error = ENOSPC;
2059 			break;
2060 		}
2061 		if (sc->sc_enabled) {
2062 			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2063 			    len);
2064 			if (error)
2065 				break;
2066 		}
2067 		sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
2068 		memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen);
2069 		break;
2070 
2071 	case WI_RID_MICROWAVE_OVEN:
2072 	case WI_RID_ROAMING_MODE:
2073 	case WI_RID_SYSTEM_SCALE:
2074 	case WI_RID_FRAG_THRESH:
2075 		if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
2076 		    (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
2077 			break;
2078 		if (wreq.wi_type == WI_RID_ROAMING_MODE &&
2079 		    (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
2080 			break;
2081 		if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
2082 		    (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
2083 			break;
2084 		if (wreq.wi_type == WI_RID_FRAG_THRESH &&
2085 		    (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
2086 			break;
2087 		/* FALLTHROUGH */
2088 	case WI_RID_RTS_THRESH:
2089 	case WI_RID_CNFAUTHMODE:
2090 	case WI_RID_MAX_DATALEN:
2091 		if (sc->sc_enabled) {
2092 			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2093 			    sizeof(u_int16_t));
2094 			if (error)
2095 				break;
2096 		}
2097 		switch (wreq.wi_type) {
2098 		case WI_RID_FRAG_THRESH:
2099 			ic->ic_fragthreshold = le16toh(wreq.wi_val[0]);
2100 			break;
2101 		case WI_RID_RTS_THRESH:
2102 			ic->ic_rtsthreshold = le16toh(wreq.wi_val[0]);
2103 			break;
2104 		case WI_RID_MICROWAVE_OVEN:
2105 			sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
2106 			break;
2107 		case WI_RID_ROAMING_MODE:
2108 			sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
2109 			break;
2110 		case WI_RID_SYSTEM_SCALE:
2111 			sc->sc_system_scale = le16toh(wreq.wi_val[0]);
2112 			break;
2113 		case WI_RID_CNFAUTHMODE:
2114 			sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
2115 			break;
2116 		case WI_RID_MAX_DATALEN:
2117 			sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
2118 			break;
2119 		}
2120 		break;
2121 
2122 	case WI_RID_TX_RATE:
2123 		switch (le16toh(wreq.wi_val[0])) {
2124 		case 3:
2125 			ic->ic_fixed_rate = -1;
2126 			break;
2127 		default:
2128 			rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
2129 			for (i = 0; i < rs->rs_nrates; i++) {
2130 				if ((rs->rs_rates[i] & IEEE80211_RATE_VAL)
2131 				    / 2 == le16toh(wreq.wi_val[0]))
2132 					break;
2133 			}
2134 			if (i == rs->rs_nrates)
2135 				return EINVAL;
2136 			ic->ic_fixed_rate = i;
2137 		}
2138 		if (sc->sc_enabled)
2139 			error = wi_write_txrate(sc);
2140 		break;
2141 
2142 	case WI_RID_SCAN_APS:
2143 		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2144 			error = wi_scan_ap(sc, 0x3fff, 0x000f);
2145 		break;
2146 
2147 	case WI_RID_SCAN_REQ:		/* compatibility interface */
2148 		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2149 			error = wi_scan_ap(sc, wreq.wi_val[0], wreq.wi_val[1]);
2150 		break;
2151 
2152 	case WI_RID_MGMT_XMIT:
2153 		if (!sc->sc_enabled) {
2154 			error = ENETDOWN;
2155 			break;
2156 		}
2157 		if (ic->ic_mgtq.ifq_len > 5) {
2158 			error = EAGAIN;
2159 			break;
2160 		}
2161 		/* XXX wi_len looks in u_int8_t, not in u_int16_t */
2162 		m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL);
2163 		if (m == NULL) {
2164 			error = ENOMEM;
2165 			break;
2166 		}
2167 		IF_ENQUEUE(&ic->ic_mgtq, m);
2168 		break;
2169 
2170 	case WI_RID_MIF:
2171 		mif = wreq.wi_val[0];
2172 		val = wreq.wi_val[1];
2173 		error = wi_cmd(sc, WI_CMD_WRITEMIF, mif, val, 0);
2174 		break;
2175 
2176 	case WI_RID_PROCFRAME:		/* ignore for compatibility */
2177 		break;
2178 
2179 	case WI_RID_OWN_SSID:
2180 		if (le16toh(wreq.wi_val[0]) * 2 > len ||
2181 		    le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) {
2182 			error = ENOSPC;
2183 			break;
2184 		}
2185 		memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
2186 		ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2;
2187 		memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen);
2188 		error = ENETRESET;
2189 		break;
2190 
2191 	default:
2192 		if (sc->sc_enabled) {
2193 			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2194 			    len);
2195 			if (error)
2196 				break;
2197 		}
2198 		error = ieee80211_cfgset(ifp, cmd, data);
2199 		break;
2200 	}
2201 	return error;
2202 }
2203 
2204 static int
2205 wi_write_txrate(struct wi_softc *sc)
2206 {
2207 	struct ieee80211com *ic = &sc->sc_ic;
2208 	int i;
2209 	u_int16_t rate;
2210 
2211 	if (ic->ic_fixed_rate < 0)
2212 		rate = 0;	/* auto */
2213 	else
2214 		rate = (ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ic->ic_fixed_rate] &
2215 		    IEEE80211_RATE_VAL) / 2;
2216 
2217 	/* rate: 0, 1, 2, 5, 11 */
2218 
2219 	switch (sc->sc_firmware_type) {
2220 	case WI_LUCENT:
2221 		switch (rate) {
2222 		case 0:			/* auto == 11mbps auto */
2223 			rate = 3;
2224 			break;
2225 		/* case 1, 2 map to 1, 2*/
2226 		case 5:			/* 5.5Mbps -> 4 */
2227 			rate = 4;
2228 			break;
2229 		case 11:		/* 11mbps -> 5 */
2230 			rate = 5;
2231 			break;
2232 		default:
2233 			break;
2234 		}
2235 		break;
2236 	default:
2237 		/* Choose a bit according to this table.
2238 		 *
2239 		 * bit | data rate
2240 		 * ----+-------------------
2241 		 * 0   | 1Mbps
2242 		 * 1   | 2Mbps
2243 		 * 2   | 5.5Mbps
2244 		 * 3   | 11Mbps
2245 		 */
2246 		for (i = 8; i > 0; i >>= 1) {
2247 			if (rate >= i)
2248 				break;
2249 		}
2250 		if (i == 0)
2251 			rate = 0xf;	/* auto */
2252 		else
2253 			rate = i;
2254 		break;
2255 	}
2256 	return wi_write_val(sc, WI_RID_TX_RATE, rate);
2257 }
2258 
2259 static int
2260 wi_write_wep(struct wi_softc *sc)
2261 {
2262 	struct ieee80211com *ic = &sc->sc_ic;
2263 	int error = 0;
2264 	int i, keylen;
2265 	u_int16_t val;
2266 	struct wi_key wkey[IEEE80211_WEP_NKID];
2267 
2268 	switch (sc->sc_firmware_type) {
2269 	case WI_LUCENT:
2270 		val = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0;
2271 		error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
2272 		if (error)
2273 			break;
2274 		error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_wep_txkey);
2275 		if (error)
2276 			break;
2277 		memset(wkey, 0, sizeof(wkey));
2278 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2279 			keylen = ic->ic_nw_keys[i].wk_len;
2280 			wkey[i].wi_keylen = htole16(keylen);
2281 			memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
2282 			    keylen);
2283 		}
2284 		error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
2285 		    wkey, sizeof(wkey));
2286 		break;
2287 
2288 	case WI_INTERSIL:
2289 	case WI_SYMBOL:
2290 		if (ic->ic_flags & IEEE80211_F_WEPON) {
2291 			/*
2292 			 * ONLY HWB3163 EVAL-CARD Firmware version
2293 			 * less than 0.8 variant2
2294 			 *
2295 			 *   If promiscuous mode disable, Prism2 chip
2296 			 *  does not work with WEP .
2297 			 * It is under investigation for details.
2298 			 * (ichiro@netbsd.org)
2299 			 */
2300 			if (sc->sc_firmware_type == WI_INTERSIL &&
2301 			    sc->sc_sta_firmware_ver < 802 ) {
2302 				/* firm ver < 0.8 variant 2 */
2303 				wi_write_val(sc, WI_RID_PROMISC, 1);
2304 			}
2305 			wi_write_val(sc, WI_RID_CNFAUTHMODE,
2306 			    sc->sc_cnfauthmode);
2307 			val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
2308 			/*
2309 			 * Encryption firmware has a bug for HostAP mode.
2310 			 */
2311 			if (sc->sc_firmware_type == WI_INTERSIL &&
2312 			    ic->ic_opmode == IEEE80211_M_HOSTAP)
2313 				val |= HOST_ENCRYPT;
2314 		} else {
2315 			wi_write_val(sc, WI_RID_CNFAUTHMODE,
2316 			    IEEE80211_AUTH_OPEN);
2317 			val = HOST_ENCRYPT | HOST_DECRYPT;
2318 		}
2319 		error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
2320 		if (error)
2321 			break;
2322 		error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
2323 		    ic->ic_wep_txkey);
2324 		if (error)
2325 			break;
2326 		/*
2327 		 * It seems that the firmware accept 104bit key only if
2328 		 * all the keys have 104bit length.  We get the length of
2329 		 * the transmit key and use it for all other keys.
2330 		 * Perhaps we should use software WEP for such situation.
2331 		 */
2332 		keylen = ic->ic_nw_keys[ic->ic_wep_txkey].wk_len;
2333 		if (keylen > IEEE80211_WEP_KEYLEN)
2334 			keylen = 13;	/* 104bit keys */
2335 		else
2336 			keylen = IEEE80211_WEP_KEYLEN;
2337 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2338 			error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
2339 			    ic->ic_nw_keys[i].wk_key, keylen);
2340 			if (error)
2341 				break;
2342 		}
2343 		break;
2344 	}
2345 	return error;
2346 }
2347 
2348 static int
2349 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
2350 {
2351 	int			i, s = 0;
2352 	static volatile int count  = 0;
2353 
2354 	if (sc->wi_gone)
2355 		return (ENODEV);
2356 
2357 	if (count > 0)
2358 		panic("Hey partner, hold on there!");
2359 	count++;
2360 
2361 	/* wait for the busy bit to clear */
2362 	for (i = sc->wi_cmd_count; i > 0; i--) {	/* 500ms */
2363 		if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
2364 			break;
2365 		DELAY(1*1000);	/* 1ms */
2366 	}
2367 	if (i == 0) {
2368 		device_printf(sc->sc_dev, "wi_cmd: busy bit won't clear.\n" );
2369 		sc->wi_gone = 1;
2370 		count--;
2371 		return(ETIMEDOUT);
2372 	}
2373 
2374 	CSR_WRITE_2(sc, WI_PARAM0, val0);
2375 	CSR_WRITE_2(sc, WI_PARAM1, val1);
2376 	CSR_WRITE_2(sc, WI_PARAM2, val2);
2377 	CSR_WRITE_2(sc, WI_COMMAND, cmd);
2378 
2379 	if (cmd == WI_CMD_INI) {
2380 		/* XXX: should sleep here. */
2381 		DELAY(100*1000);		/* 100ms delay for init */
2382 	}
2383 	for (i = 0; i < WI_TIMEOUT; i++) {
2384 		/*
2385 		 * Wait for 'command complete' bit to be
2386 		 * set in the event status register.
2387 		 */
2388 		s = CSR_READ_2(sc, WI_EVENT_STAT);
2389 		if (s & WI_EV_CMD) {
2390 			/* Ack the event and read result code. */
2391 			s = CSR_READ_2(sc, WI_STATUS);
2392 			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
2393 			if (s & WI_STAT_CMD_RESULT) {
2394 				count--;
2395 				return(EIO);
2396 			}
2397 			break;
2398 		}
2399 		DELAY(WI_DELAY);
2400 	}
2401 
2402 	count--;
2403 	if (i == WI_TIMEOUT) {
2404 		device_printf(sc->sc_dev,
2405 		    "timeout in wi_cmd 0x%04x; event status 0x%04x\n", cmd, s);
2406 		if (s == 0xffff)
2407 			sc->wi_gone = 1;
2408 		return(ETIMEDOUT);
2409 	}
2410 	return (0);
2411 }
2412 
2413 static int
2414 wi_seek_bap(struct wi_softc *sc, int id, int off)
2415 {
2416 	int i, status;
2417 
2418 	CSR_WRITE_2(sc, WI_SEL0, id);
2419 	CSR_WRITE_2(sc, WI_OFF0, off);
2420 
2421 	for (i = 0; ; i++) {
2422 		status = CSR_READ_2(sc, WI_OFF0);
2423 		if ((status & WI_OFF_BUSY) == 0)
2424 			break;
2425 		if (i == WI_TIMEOUT) {
2426 			device_printf(sc->sc_dev, "timeout in wi_seek to %x/%x\n",
2427 			    id, off);
2428 			sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2429 			if (status == 0xffff)
2430 				sc->wi_gone = 1;
2431 			return ETIMEDOUT;
2432 		}
2433 		DELAY(1);
2434 	}
2435 	if (status & WI_OFF_ERR) {
2436 		device_printf(sc->sc_dev, "failed in wi_seek to %x/%x\n", id, off);
2437 		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2438 		return EIO;
2439 	}
2440 	sc->sc_bap_id = id;
2441 	sc->sc_bap_off = off;
2442 	return 0;
2443 }
2444 
2445 static int
2446 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
2447 {
2448 	u_int16_t *ptr;
2449 	int i, error, cnt;
2450 
2451 	if (buflen == 0)
2452 		return 0;
2453 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2454 		if ((error = wi_seek_bap(sc, id, off)) != 0)
2455 			return error;
2456 	}
2457 	cnt = (buflen + 1) / 2;
2458 	ptr = (u_int16_t *)buf;
2459 	for (i = 0; i < cnt; i++)
2460 		*ptr++ = CSR_READ_2(sc, WI_DATA0);
2461 	sc->sc_bap_off += cnt * 2;
2462 	return 0;
2463 }
2464 
2465 static int
2466 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
2467 {
2468 	u_int16_t *ptr;
2469 	int i, error, cnt;
2470 
2471 	if (buflen == 0)
2472 		return 0;
2473 
2474 #ifdef WI_HERMES_AUTOINC_WAR
2475   again:
2476 #endif
2477 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2478 		if ((error = wi_seek_bap(sc, id, off)) != 0)
2479 			return error;
2480 	}
2481 	cnt = (buflen + 1) / 2;
2482 	ptr = (u_int16_t *)buf;
2483 	for (i = 0; i < cnt; i++)
2484 		CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
2485 	sc->sc_bap_off += cnt * 2;
2486 
2487 #ifdef WI_HERMES_AUTOINC_WAR
2488 	/*
2489 	 * According to the comments in the HCF Light code, there is a bug
2490 	 * in the Hermes (or possibly in certain Hermes firmware revisions)
2491 	 * where the chip's internal autoincrement counter gets thrown off
2492 	 * during data writes:  the autoincrement is missed, causing one
2493 	 * data word to be overwritten and subsequent words to be written to
2494 	 * the wrong memory locations. The end result is that we could end
2495 	 * up transmitting bogus frames without realizing it. The workaround
2496 	 * for this is to write a couple of extra guard words after the end
2497 	 * of the transfer, then attempt to read then back. If we fail to
2498 	 * locate the guard words where we expect them, we preform the
2499 	 * transfer over again.
2500 	 */
2501 	if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
2502 		CSR_WRITE_2(sc, WI_DATA0, 0x1234);
2503 		CSR_WRITE_2(sc, WI_DATA0, 0x5678);
2504 		wi_seek_bap(sc, id, sc->sc_bap_off);
2505 		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
2506 		if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
2507 		    CSR_READ_2(sc, WI_DATA0) != 0x5678) {
2508 			device_printf(sc->sc_dev,
2509 				"detect auto increment bug, try again\n");
2510 			goto again;
2511 		}
2512 	}
2513 #endif
2514 	return 0;
2515 }
2516 
2517 static int
2518 wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
2519 {
2520 	int error, len;
2521 	struct mbuf *m;
2522 
2523 	for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
2524 		if (m->m_len == 0)
2525 			continue;
2526 
2527 		len = min(m->m_len, totlen);
2528 
2529 		if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
2530 			m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
2531 			return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
2532 			    totlen);
2533 		}
2534 
2535 		if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
2536 			return error;
2537 
2538 		off += m->m_len;
2539 		totlen -= len;
2540 	}
2541 	return 0;
2542 }
2543 
2544 static int
2545 wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
2546 {
2547 	int i;
2548 
2549 	if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
2550 		device_printf(sc->sc_dev, "failed to allocate %d bytes on NIC\n",
2551 		    len);
2552 		return ENOMEM;
2553 	}
2554 
2555 	for (i = 0; i < WI_TIMEOUT; i++) {
2556 		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
2557 			break;
2558 		if (i == WI_TIMEOUT) {
2559 			device_printf(sc->sc_dev, "timeout in alloc\n");
2560 			return ETIMEDOUT;
2561 		}
2562 		DELAY(1);
2563 	}
2564 	*idp = CSR_READ_2(sc, WI_ALLOC_FID);
2565 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
2566 	return 0;
2567 }
2568 
2569 static int
2570 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
2571 {
2572 	int error, len;
2573 	u_int16_t ltbuf[2];
2574 
2575 	/* Tell the NIC to enter record read mode. */
2576 	error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2577 	if (error)
2578 		return error;
2579 
2580 	error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2581 	if (error)
2582 		return error;
2583 
2584 	if (le16toh(ltbuf[1]) != rid) {
2585 		device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n",
2586 		    rid, le16toh(ltbuf[1]));
2587 		return EIO;
2588 	}
2589 	len = (le16toh(ltbuf[0]) - 1) * 2;	 /* already got rid */
2590 	if (*buflenp < len) {
2591 		device_printf(sc->sc_dev, "record buffer is too small, "
2592 		    "rid=%x, size=%d, len=%d\n",
2593 		    rid, *buflenp, len);
2594 		return ENOSPC;
2595 	}
2596 	*buflenp = len;
2597 	return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2598 }
2599 
2600 static int
2601 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2602 {
2603 	int error;
2604 	u_int16_t ltbuf[2];
2605 
2606 	ltbuf[0] = htole16((buflen + 1) / 2 + 1);	 /* includes rid */
2607 	ltbuf[1] = htole16(rid);
2608 
2609 	error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2610 	if (error)
2611 		return error;
2612 	error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2613 	if (error)
2614 		return error;
2615 
2616 	return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
2617 }
2618 
2619 static int
2620 wi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
2621 {
2622 	struct ifnet *ifp = &ic->ic_if;
2623 	struct wi_softc *sc = ifp->if_softc;
2624 	struct ieee80211_node *ni = ic->ic_bss;
2625 	int buflen;
2626 	u_int16_t val;
2627 	struct wi_ssid ssid;
2628 	u_int8_t old_bssid[IEEE80211_ADDR_LEN];
2629 
2630 	DPRINTF(("%s: %s -> %s\n", __func__,
2631 		ieee80211_state_name[ic->ic_state],
2632 		ieee80211_state_name[nstate]));
2633 
2634 	switch (nstate) {
2635 	case IEEE80211_S_INIT:
2636 		ic->ic_flags &= ~IEEE80211_F_SIBSS;
2637 		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2638 		return (*sc->sc_newstate)(ic, nstate, arg);
2639 
2640 	case IEEE80211_S_RUN:
2641 		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2642 		buflen = IEEE80211_ADDR_LEN;
2643 		wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen);
2644 		IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
2645 		buflen = sizeof(val);
2646 		wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
2647 		/* XXX validate channel */
2648 		ni->ni_chan = &ic->ic_channels[le16toh(val)];
2649 		sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
2650 			htole16(ni->ni_chan->ic_freq);
2651 		sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
2652 			htole16(ni->ni_chan->ic_flags);
2653 
2654 		if (IEEE80211_ADDR_EQ(old_bssid, ni->ni_bssid))
2655 			sc->sc_false_syns++;
2656 		else
2657 			sc->sc_false_syns = 0;
2658 
2659 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2660 			ni->ni_esslen = ic->ic_des_esslen;
2661 			memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
2662 			ni->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B];
2663 			ni->ni_intval = ic->ic_lintval;
2664 			ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
2665 			if (ic->ic_flags & IEEE80211_F_WEPON)
2666 				ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
2667 		} else {
2668 			/* XXX check return value */
2669 			buflen = sizeof(ssid);
2670 			wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
2671 			ni->ni_esslen = le16toh(ssid.wi_len);
2672 			if (ni->ni_esslen > IEEE80211_NWID_LEN)
2673 				ni->ni_esslen = IEEE80211_NWID_LEN;	/*XXX*/
2674 			memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
2675 		}
2676 		break;
2677 
2678 	case IEEE80211_S_SCAN:
2679 	case IEEE80211_S_AUTH:
2680 	case IEEE80211_S_ASSOC:
2681 		break;
2682 	}
2683 
2684 	ic->ic_state = nstate;		/* NB: skip normal ieee80211 handling */
2685 	return 0;
2686 }
2687 
2688 static int
2689 wi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate)
2690 {
2691 	int error = 0;
2692 	u_int16_t val[2];
2693 
2694 	if (!sc->sc_enabled)
2695 		return ENXIO;
2696 	switch (sc->sc_firmware_type) {
2697 	case WI_LUCENT:
2698 		(void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
2699 		break;
2700 	case WI_INTERSIL:
2701 		val[0] = chanmask;	/* channel */
2702 		val[1] = txrate;	/* tx rate */
2703 		error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
2704 		break;
2705 	case WI_SYMBOL:
2706 		/*
2707 		 * XXX only supported on 3.x ?
2708 		 */
2709 		val[0] = BSCAN_BCAST | BSCAN_ONETIME;
2710 		error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
2711 		    val, sizeof(val[0]));
2712 		break;
2713 	}
2714 	if (error == 0) {
2715 		sc->sc_scan_timer = WI_SCAN_WAIT;
2716 		sc->sc_ic.ic_if.if_timer = 1;
2717 		DPRINTF(("wi_scan_ap: start scanning, "
2718 			"chamask 0x%x txrate 0x%x\n", chanmask, txrate));
2719 	}
2720 	return error;
2721 }
2722 
2723 static void
2724 wi_scan_result(struct wi_softc *sc, int fid, int cnt)
2725 {
2726 #define	N(a)	(sizeof (a) / sizeof (a[0]))
2727 	int i, naps, off, szbuf;
2728 	struct wi_scan_header ws_hdr;	/* Prism2 header */
2729 	struct wi_scan_data_p2 ws_dat;	/* Prism2 scantable*/
2730 	struct wi_apinfo *ap;
2731 
2732 	off = sizeof(u_int16_t) * 2;
2733 	memset(&ws_hdr, 0, sizeof(ws_hdr));
2734 	switch (sc->sc_firmware_type) {
2735 	case WI_INTERSIL:
2736 		wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
2737 		off += sizeof(ws_hdr);
2738 		szbuf = sizeof(struct wi_scan_data_p2);
2739 		break;
2740 	case WI_SYMBOL:
2741 		szbuf = sizeof(struct wi_scan_data_p2) + 6;
2742 		break;
2743 	case WI_LUCENT:
2744 		szbuf = sizeof(struct wi_scan_data);
2745 		break;
2746 	default:
2747 		device_printf(sc->sc_dev,
2748 			"wi_scan_result: unknown firmware type %u\n",
2749 			sc->sc_firmware_type);
2750 		naps = 0;
2751 		goto done;
2752 	}
2753 	naps = (cnt * 2 + 2 - off) / szbuf;
2754 	if (naps > N(sc->sc_aps))
2755 		naps = N(sc->sc_aps);
2756 	sc->sc_naps = naps;
2757 	/* Read Data */
2758 	ap = sc->sc_aps;
2759 	memset(&ws_dat, 0, sizeof(ws_dat));
2760 	for (i = 0; i < naps; i++, ap++) {
2761 		wi_read_bap(sc, fid, off, &ws_dat,
2762 		    (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
2763 		DPRINTF2(("wi_scan_result: #%d: off %d bssid %6D\n", i, off,
2764 		    ws_dat.wi_bssid, ":"));
2765 		off += szbuf;
2766 		ap->scanreason = le16toh(ws_hdr.wi_reason);
2767 		memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
2768 		ap->channel = le16toh(ws_dat.wi_chid);
2769 		ap->signal  = le16toh(ws_dat.wi_signal);
2770 		ap->noise   = le16toh(ws_dat.wi_noise);
2771 		ap->quality = ap->signal - ap->noise;
2772 		ap->capinfo = le16toh(ws_dat.wi_capinfo);
2773 		ap->interval = le16toh(ws_dat.wi_interval);
2774 		ap->rate    = le16toh(ws_dat.wi_rate);
2775 		ap->namelen = le16toh(ws_dat.wi_namelen);
2776 		if (ap->namelen > sizeof(ap->name))
2777 			ap->namelen = sizeof(ap->name);
2778 		memcpy(ap->name, ws_dat.wi_name, ap->namelen);
2779 	}
2780 done:
2781 	/* Done scanning */
2782 	sc->sc_scan_timer = 0;
2783 	DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
2784 #undef N
2785 }
2786 
2787 static void
2788 wi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi)
2789 {
2790 	ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr),
2791 	    ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL : -1, rssi);
2792 	printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n",
2793 		le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1),
2794 		le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence);
2795 	printf(" rx_signal %u rx_rate %u rx_flow %u\n",
2796 		wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow);
2797 	printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n",
2798 		wh->wi_tx_rtry, wh->wi_tx_rate,
2799 		le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len));
2800 	printf(" ehdr dst %6D src %6D type 0x%x\n",
2801 		wh->wi_ehdr.ether_dhost, ":", wh->wi_ehdr.ether_shost, ":",
2802 		wh->wi_ehdr.ether_type);
2803 }
2804 
2805 int
2806 wi_alloc(device_t dev, int rid)
2807 {
2808 	struct wi_softc	*sc = device_get_softc(dev);
2809 
2810 	if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2811 		sc->iobase_rid = rid;
2812 		sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2813 		    &sc->iobase_rid, 0, ~0, (1 << 6),
2814 		    rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2815 		if (!sc->iobase) {
2816 			device_printf(dev, "No I/O space?!\n");
2817 			return (ENXIO);
2818 		}
2819 
2820 		sc->wi_io_addr = rman_get_start(sc->iobase);
2821 		sc->wi_btag = rman_get_bustag(sc->iobase);
2822 		sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2823 	} else {
2824 		sc->mem_rid = rid;
2825 		sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2826 		    &sc->mem_rid, RF_ACTIVE);
2827 
2828 		if (!sc->mem) {
2829 			device_printf(dev, "No Mem space on prism2.5?\n");
2830 			return (ENXIO);
2831 		}
2832 
2833 		sc->wi_btag = rman_get_bustag(sc->mem);
2834 		sc->wi_bhandle = rman_get_bushandle(sc->mem);
2835 	}
2836 
2837 
2838 	sc->irq_rid = 0;
2839 	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2840 	    RF_ACTIVE |
2841 	    ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2842 
2843 	if (!sc->irq) {
2844 		wi_free(dev);
2845 		device_printf(dev, "No irq?!\n");
2846 		return (ENXIO);
2847 	}
2848 
2849 	sc->sc_dev = dev;
2850 	sc->sc_unit = device_get_unit(dev);
2851 
2852 	return (0);
2853 }
2854 
2855 void
2856 wi_free(device_t dev)
2857 {
2858 	struct wi_softc	*sc = device_get_softc(dev);
2859 
2860 	if (sc->iobase != NULL) {
2861 		bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2862 		sc->iobase = NULL;
2863 	}
2864 	if (sc->irq != NULL) {
2865 		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2866 		sc->irq = NULL;
2867 	}
2868 	if (sc->mem != NULL) {
2869 		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
2870 		sc->mem = NULL;
2871 	}
2872 
2873 	return;
2874 }
2875 
2876 static int
2877 wi_get_debug(struct wi_softc *sc, struct wi_req *wreq)
2878 {
2879 	int error = 0;
2880 
2881 	wreq->wi_len = 1;
2882 
2883 	switch (wreq->wi_type) {
2884 	case WI_DEBUG_SLEEP:
2885 		wreq->wi_len++;
2886 		wreq->wi_val[0] = sc->wi_debug.wi_sleep;
2887 		break;
2888 	case WI_DEBUG_DELAYSUPP:
2889 		wreq->wi_len++;
2890 		wreq->wi_val[0] = sc->wi_debug.wi_delaysupp;
2891 		break;
2892 	case WI_DEBUG_TXSUPP:
2893 		wreq->wi_len++;
2894 		wreq->wi_val[0] = sc->wi_debug.wi_txsupp;
2895 		break;
2896 	case WI_DEBUG_MONITOR:
2897 		wreq->wi_len++;
2898 		wreq->wi_val[0] = sc->wi_debug.wi_monitor;
2899 		break;
2900 	case WI_DEBUG_LEDTEST:
2901 		wreq->wi_len += 3;
2902 		wreq->wi_val[0] = sc->wi_debug.wi_ledtest;
2903 		wreq->wi_val[1] = sc->wi_debug.wi_ledtest_param0;
2904 		wreq->wi_val[2] = sc->wi_debug.wi_ledtest_param1;
2905 		break;
2906 	case WI_DEBUG_CONTTX:
2907 		wreq->wi_len += 2;
2908 		wreq->wi_val[0] = sc->wi_debug.wi_conttx;
2909 		wreq->wi_val[1] = sc->wi_debug.wi_conttx_param0;
2910 		break;
2911 	case WI_DEBUG_CONTRX:
2912 		wreq->wi_len++;
2913 		wreq->wi_val[0] = sc->wi_debug.wi_contrx;
2914 		break;
2915 	case WI_DEBUG_SIGSTATE:
2916 		wreq->wi_len += 2;
2917 		wreq->wi_val[0] = sc->wi_debug.wi_sigstate;
2918 		wreq->wi_val[1] = sc->wi_debug.wi_sigstate_param0;
2919 		break;
2920 	case WI_DEBUG_CONFBITS:
2921 		wreq->wi_len += 2;
2922 		wreq->wi_val[0] = sc->wi_debug.wi_confbits;
2923 		wreq->wi_val[1] = sc->wi_debug.wi_confbits_param0;
2924 		break;
2925 	default:
2926 		error = EIO;
2927 		break;
2928 	}
2929 
2930 	return (error);
2931 }
2932 
2933 static int
2934 wi_set_debug(struct wi_softc *sc, struct wi_req *wreq)
2935 {
2936 	int error = 0;
2937 	u_int16_t		cmd, param0 = 0, param1 = 0;
2938 
2939 	switch (wreq->wi_type) {
2940 	case WI_DEBUG_RESET:
2941 	case WI_DEBUG_INIT:
2942 	case WI_DEBUG_CALENABLE:
2943 		break;
2944 	case WI_DEBUG_SLEEP:
2945 		sc->wi_debug.wi_sleep = 1;
2946 		break;
2947 	case WI_DEBUG_WAKE:
2948 		sc->wi_debug.wi_sleep = 0;
2949 		break;
2950 	case WI_DEBUG_CHAN:
2951 		param0 = wreq->wi_val[0];
2952 		break;
2953 	case WI_DEBUG_DELAYSUPP:
2954 		sc->wi_debug.wi_delaysupp = 1;
2955 		break;
2956 	case WI_DEBUG_TXSUPP:
2957 		sc->wi_debug.wi_txsupp = 1;
2958 		break;
2959 	case WI_DEBUG_MONITOR:
2960 		sc->wi_debug.wi_monitor = 1;
2961 		break;
2962 	case WI_DEBUG_LEDTEST:
2963 		param0 = wreq->wi_val[0];
2964 		param1 = wreq->wi_val[1];
2965 		sc->wi_debug.wi_ledtest = 1;
2966 		sc->wi_debug.wi_ledtest_param0 = param0;
2967 		sc->wi_debug.wi_ledtest_param1 = param1;
2968 		break;
2969 	case WI_DEBUG_CONTTX:
2970 		param0 = wreq->wi_val[0];
2971 		sc->wi_debug.wi_conttx = 1;
2972 		sc->wi_debug.wi_conttx_param0 = param0;
2973 		break;
2974 	case WI_DEBUG_STOPTEST:
2975 		sc->wi_debug.wi_delaysupp = 0;
2976 		sc->wi_debug.wi_txsupp = 0;
2977 		sc->wi_debug.wi_monitor = 0;
2978 		sc->wi_debug.wi_ledtest = 0;
2979 		sc->wi_debug.wi_ledtest_param0 = 0;
2980 		sc->wi_debug.wi_ledtest_param1 = 0;
2981 		sc->wi_debug.wi_conttx = 0;
2982 		sc->wi_debug.wi_conttx_param0 = 0;
2983 		sc->wi_debug.wi_contrx = 0;
2984 		sc->wi_debug.wi_sigstate = 0;
2985 		sc->wi_debug.wi_sigstate_param0 = 0;
2986 		break;
2987 	case WI_DEBUG_CONTRX:
2988 		sc->wi_debug.wi_contrx = 1;
2989 		break;
2990 	case WI_DEBUG_SIGSTATE:
2991 		param0 = wreq->wi_val[0];
2992 		sc->wi_debug.wi_sigstate = 1;
2993 		sc->wi_debug.wi_sigstate_param0 = param0;
2994 		break;
2995 	case WI_DEBUG_CONFBITS:
2996 		param0 = wreq->wi_val[0];
2997 		param1 = wreq->wi_val[1];
2998 		sc->wi_debug.wi_confbits = param0;
2999 		sc->wi_debug.wi_confbits_param0 = param1;
3000 		break;
3001 	default:
3002 		error = EIO;
3003 		break;
3004 	}
3005 
3006 	if (error)
3007 		return (error);
3008 
3009 	cmd = WI_CMD_DEBUG | (wreq->wi_type << 8);
3010 	error = wi_cmd(sc, cmd, param0, param1, 0);
3011 
3012 	return (error);
3013 }
3014 
3015 /*
3016  * Special routines to download firmware for Symbol CF card.
3017  * XXX: This should be modified generic into any PRISM-2 based card.
3018  */
3019 
3020 #define	WI_SBCF_PDIADDR		0x3100
3021 
3022 /* unaligned load little endian */
3023 #define	GETLE32(p)	((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24))
3024 #define	GETLE16(p)	((p)[0] | ((p)[1]<<8))
3025 
3026 int
3027 wi_symbol_load_firm(struct wi_softc *sc, const void *primsym, int primlen,
3028     const void *secsym, int seclen)
3029 {
3030 	uint8_t ebuf[256];
3031 	int i;
3032 
3033 	/* load primary code and run it */
3034 	wi_symbol_set_hcr(sc, WI_HCR_EEHOLD);
3035 	if (wi_symbol_write_firm(sc, primsym, primlen, NULL, 0))
3036 		return EIO;
3037 	wi_symbol_set_hcr(sc, WI_HCR_RUN);
3038 	for (i = 0; ; i++) {
3039 		if (i == 10)
3040 			return ETIMEDOUT;
3041 		tsleep(sc, 0, "wiinit", 1);
3042 		if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT)
3043 			break;
3044 		/* write the magic key value to unlock aux port */
3045 		CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0);
3046 		CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1);
3047 		CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2);
3048 		CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL);
3049 	}
3050 
3051 	/* issue read EEPROM command: XXX copied from wi_cmd() */
3052 	CSR_WRITE_2(sc, WI_PARAM0, 0);
3053 	CSR_WRITE_2(sc, WI_PARAM1, 0);
3054 	CSR_WRITE_2(sc, WI_PARAM2, 0);
3055 	CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE);
3056         for (i = 0; i < WI_TIMEOUT; i++) {
3057                 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
3058                         break;
3059                 DELAY(1);
3060         }
3061         CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
3062 
3063 	CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ);
3064 	CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ);
3065 	CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA,
3066 	    (uint16_t *)ebuf, sizeof(ebuf) / 2);
3067 	if (GETLE16(ebuf) > sizeof(ebuf))
3068 		return EIO;
3069 	if (wi_symbol_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf)))
3070 		return EIO;
3071 	return 0;
3072 }
3073 
3074 static int
3075 wi_symbol_write_firm(struct wi_softc *sc, const void *buf, int buflen,
3076     const void *ebuf, int ebuflen)
3077 {
3078 	const uint8_t *p, *ep, *q, *eq;
3079 	char *tp;
3080 	uint32_t addr, id, eid;
3081 	int i, len, elen, nblk, pdrlen;
3082 
3083 	/*
3084 	 * Parse the header of the firmware image.
3085 	 */
3086 	p = buf;
3087 	ep = p + buflen;
3088 	while (p < ep && *p++ != ' ');	/* FILE: */
3089 	while (p < ep && *p++ != ' ');	/* filename */
3090 	while (p < ep && *p++ != ' ');	/* type of the firmware */
3091 	nblk = strtoul(p, &tp, 10);
3092 	p = tp;
3093 	pdrlen = strtoul(p + 1, &tp, 10);
3094 	p = tp;
3095 	while (p < ep && *p++ != 0x1a);	/* skip rest of header */
3096 
3097 	/*
3098 	 * Block records: address[4], length[2], data[length];
3099 	 */
3100 	for (i = 0; i < nblk; i++) {
3101 		addr = GETLE32(p);	p += 4;
3102 		len  = GETLE16(p);	p += 2;
3103 		CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3104 		CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3105 		CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3106 		    (const uint16_t *)p, len / 2);
3107 		p += len;
3108 	}
3109 
3110 	/*
3111 	 * PDR: id[4], address[4], length[4];
3112 	 */
3113 	for (i = 0; i < pdrlen; ) {
3114 		id   = GETLE32(p);	p += 4; i += 4;
3115 		addr = GETLE32(p);	p += 4; i += 4;
3116 		len  = GETLE32(p);	p += 4; i += 4;
3117 		/* replace PDR entry with the values from EEPROM, if any */
3118 		for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) {
3119 			elen = GETLE16(q);	q += 2;
3120 			eid  = GETLE16(q);	q += 2;
3121 			elen--;		/* elen includes eid */
3122 			if (eid == 0)
3123 				break;
3124 			if (eid != id)
3125 				continue;
3126 			CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3127 			CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3128 			CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3129 			    (const uint16_t *)q, len / 2);
3130 			break;
3131 		}
3132 	}
3133 	return 0;
3134 }
3135 
3136 static int
3137 wi_symbol_set_hcr(struct wi_softc *sc, int mode)
3138 {
3139 	uint16_t hcr;
3140 
3141 	CSR_WRITE_2(sc, WI_COR, WI_COR_RESET);
3142 	tsleep(sc, 0, "wiinit", 1);
3143 	hcr = CSR_READ_2(sc, WI_HCR);
3144 	hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE);
3145 	CSR_WRITE_2(sc, WI_HCR, hcr);
3146 	tsleep(sc, 0, "wiinit", 1);
3147 	CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE);
3148 	tsleep(sc, 0, "wiinit", 1);
3149 	return 0;
3150 }
3151