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