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