xref: /netbsd-src/sys/dev/ic/an.c (revision 7f21db1c0118155e0dd40b75182e30c589d9f63e)
1 /*	$NetBSD: an.c,v 1.58 2010/01/19 22:06:24 pooka Exp $	*/
2 /*
3  * Copyright (c) 1997, 1998, 1999
4  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Bill Paul.
17  * 4. Neither the name of the author nor the names of any co-contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $FreeBSD: src/sys/dev/an/if_an.c,v 1.12 2000/11/13 23:04:12 wpaul Exp $
34  */
35 /*
36  * Copyright (c) 2004, 2005 David Young.  All rights reserved.
37  * Copyright (c) 2004, 2005 OJC Technologies.  All rights reserved.
38  * Copyright (c) 2004, 2005 Dayton Data Center Services, LLC.  All
39  *     rights reserved.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. Neither the name of the author nor the names of any co-contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY David Young AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL David Young AND CONTRIBUTORS
57  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
63  * THE POSSIBILITY OF SUCH DAMAGE.
64  */
65 
66 /*
67  * Aironet 4500/4800 802.11 PCMCIA/ISA/PCI driver for FreeBSD.
68  *
69  * Written by Bill Paul <wpaul@ctr.columbia.edu>
70  * Electrical Engineering Department
71  * Columbia University, New York City
72  */
73 
74 /*
75  * Ported to NetBSD from FreeBSD by Atsushi Onoe at the San Diego
76  * IETF meeting.
77  */
78 
79 #include <sys/cdefs.h>
80 __KERNEL_RCSID(0, "$NetBSD: an.c,v 1.58 2010/01/19 22:06:24 pooka Exp $");
81 
82 
83 #include <sys/param.h>
84 #include <sys/callout.h>
85 #include <sys/sysctl.h>
86 #include <sys/systm.h>
87 #include <sys/sockio.h>
88 #include <sys/mbuf.h>
89 #include <sys/kernel.h>
90 #include <sys/ucred.h>
91 #include <sys/socket.h>
92 #include <sys/device.h>
93 #include <sys/proc.h>
94 #include <sys/md4.h>
95 #include <sys/endian.h>
96 #include <sys/kauth.h>
97 
98 #include <sys/bus.h>
99 
100 #include <net/if.h>
101 #include <net/if_dl.h>
102 #include <net/if_ether.h>
103 #include <net/if_llc.h>
104 #include <net/if_media.h>
105 #include <net/if_types.h>
106 
107 #include <net80211/ieee80211_netbsd.h>
108 #include <net80211/ieee80211_var.h>
109 #include <net80211/ieee80211_radiotap.h>
110 
111 #include <net/bpf.h>
112 #include <net/bpfdesc.h>
113 
114 #include <dev/ic/anreg.h>
115 #include <dev/ic/anvar.h>
116 
117 static int	an_reset(struct an_softc *);
118 static void	an_wait(struct an_softc *);
119 static int	an_init(struct ifnet *);
120 static void	an_stop(struct ifnet *, int);
121 static void	an_start(struct ifnet *);
122 static void	an_watchdog(struct ifnet *);
123 static int	an_ioctl(struct ifnet *, u_long, void *);
124 static int	an_media_change(struct ifnet *);
125 static void	an_media_status(struct ifnet *, struct ifmediareq *);
126 
127 static int	an_set_nwkey(struct an_softc *, struct ieee80211_nwkey *);
128 static int	an_set_nwkey_wep(struct an_softc *, struct ieee80211_nwkey *);
129 static int	an_set_nwkey_eap(struct an_softc *, struct ieee80211_nwkey *);
130 static int	an_get_nwkey(struct an_softc *, struct ieee80211_nwkey *);
131 static int	an_write_wepkey(struct an_softc *, int, struct an_wepkey *,
132 				int);
133 
134 static void	an_rx_intr(struct an_softc *);
135 static void	an_tx_intr(struct an_softc *, int);
136 static void	an_linkstat_intr(struct an_softc *);
137 
138 static int	an_cmd(struct an_softc *, int, int);
139 static int	an_seek_bap(struct an_softc *, int, int);
140 static int	an_read_bap(struct an_softc *, int, int, void *, int);
141 static int	an_write_bap(struct an_softc *, int, int, void *, int);
142 static int	an_mwrite_bap(struct an_softc *, int, int, struct mbuf *, int);
143 static int	an_read_rid(struct an_softc *, int, void *, int *);
144 static int	an_write_rid(struct an_softc *, int, void *, int);
145 
146 static int	an_alloc_fid(struct an_softc *, int, int *);
147 
148 static int	an_newstate(struct ieee80211com *, enum ieee80211_state, int);
149 
150 #ifdef AN_DEBUG
151 int an_debug = 0;
152 
153 #define	DPRINTF(X)	if (an_debug) printf X
154 #define	DPRINTF2(X)	if (an_debug > 1) printf X
155 static int an_sysctl_verify(SYSCTLFN_PROTO, int lower, int upper);
156 static int an_sysctl_verify_debug(SYSCTLFN_PROTO);
157 #else
158 #define	DPRINTF(X)
159 #define	DPRINTF2(X)
160 #endif
161 
162 int
163 an_attach(struct an_softc *sc)
164 {
165 	struct ieee80211com *ic = &sc->sc_ic;
166 	struct ifnet *ifp = &sc->sc_if;
167 	int i, s;
168 	struct an_rid_wepkey *akey;
169 	int buflen, kid, rid;
170 	int chan, chan_min, chan_max;
171 
172 	s = splnet();
173 
174 	an_wait(sc);
175 	if (an_reset(sc) != 0) {
176 		config_deactivate(sc->sc_dev);
177 		splx(s);
178 		return 1;
179 	}
180 
181 	/* Load factory config */
182 	if (an_cmd(sc, AN_CMD_READCFG, 0) != 0) {
183 		splx(s);
184 		aprint_error_dev(sc->sc_dev, "failed to load config data\n");
185 		return 1;
186 	}
187 
188 	/* Read the current configuration */
189 	buflen = sizeof(sc->sc_config);
190 	if (an_read_rid(sc, AN_RID_GENCONFIG, &sc->sc_config, &buflen) != 0) {
191 		splx(s);
192 		aprint_error_dev(sc->sc_dev, "read config failed\n");
193 		return 1;
194 	}
195 
196 	/* Read the card capabilities */
197 	buflen = sizeof(sc->sc_caps);
198 	if (an_read_rid(sc, AN_RID_CAPABILITIES, &sc->sc_caps, &buflen) != 0) {
199 		splx(s);
200 		aprint_error_dev(sc->sc_dev, "read caps failed\n");
201 		return 1;
202 	}
203 
204 #ifdef AN_DEBUG
205 	if (an_debug) {
206 		static const int dumprid[] = {
207 		    AN_RID_GENCONFIG, AN_RID_CAPABILITIES, AN_RID_SSIDLIST,
208 		    AN_RID_APLIST, AN_RID_STATUS, AN_RID_ENCAP
209 		};
210 
211 		for (rid = 0; rid < sizeof(dumprid)/sizeof(dumprid[0]); rid++) {
212 			buflen = sizeof(sc->sc_buf);
213 			if (an_read_rid(sc, dumprid[rid], &sc->sc_buf, &buflen)
214 			    != 0)
215 				continue;
216 			printf("%04x (%d):\n", dumprid[rid], buflen);
217 			for (i = 0; i < (buflen + 1) / 2; i++)
218 				printf(" %04x", sc->sc_buf.sc_val[i]);
219 			printf("\n");
220 		}
221 	}
222 #endif
223 
224 	/* Read WEP settings from persistent memory */
225 	akey = &sc->sc_buf.sc_wepkey;
226 	buflen = sizeof(struct an_rid_wepkey);
227 	rid = AN_RID_WEP_VOLATILE;	/* first persistent key */
228 	while (an_read_rid(sc, rid, akey, &buflen) == 0) {
229 		kid = le16toh(akey->an_key_index);
230 		DPRINTF(("an_attach: wep rid=0x%x len=%d(%zu) index=0x%04x "
231 		    "mac[0]=%02x keylen=%d\n",
232 		    rid, buflen, sizeof(*akey), kid,
233 		    akey->an_mac_addr[0], le16toh(akey->an_key_len)));
234 		if (kid == 0xffff) {
235 			sc->sc_tx_perskey = akey->an_mac_addr[0];
236 			sc->sc_tx_key = -1;
237 			break;
238 		}
239 		if (kid >= IEEE80211_WEP_NKID)
240 			break;
241 		sc->sc_perskeylen[kid] = le16toh(akey->an_key_len);
242 		sc->sc_wepkeys[kid].an_wep_keylen = -1;
243 		rid = AN_RID_WEP_PERSISTENT;	/* for next key */
244 		buflen = sizeof(struct an_rid_wepkey);
245 	}
246 
247 	aprint_normal_dev(sc->sc_dev, "%s %s (firmware %s)\n",
248 	    sc->sc_caps.an_manufname, sc->sc_caps.an_prodname,
249 	    sc->sc_caps.an_prodvers);
250 
251 	memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
252 
253 	ifp->if_softc = sc;
254 	ifp->if_flags = IFF_BROADCAST | IFF_NOTRAILERS | IFF_SIMPLEX |
255 	    IFF_MULTICAST | IFF_ALLMULTI;
256 	ifp->if_ioctl = an_ioctl;
257 	ifp->if_start = an_start;
258 	ifp->if_init = an_init;
259 	ifp->if_stop = an_stop;
260 	ifp->if_watchdog = an_watchdog;
261 	IFQ_SET_READY(&ifp->if_snd);
262 
263 	ic->ic_ifp = ifp;
264 	ic->ic_phytype = IEEE80211_T_DS;
265 	ic->ic_opmode = IEEE80211_M_STA;
266 	ic->ic_caps = IEEE80211_C_WEP | IEEE80211_C_PMGT | IEEE80211_C_IBSS |
267 	    IEEE80211_C_MONITOR;
268 	ic->ic_state = IEEE80211_S_INIT;
269 	IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->sc_caps.an_oemaddr);
270 
271 	switch (le16toh(sc->sc_caps.an_regdomain)) {
272 	default:
273 	case AN_REGDOMAIN_USA:
274 	case AN_REGDOMAIN_CANADA:
275 		chan_min = 1; chan_max = 11; break;
276 	case AN_REGDOMAIN_EUROPE:
277 	case AN_REGDOMAIN_AUSTRALIA:
278 		chan_min = 1; chan_max = 13; break;
279 	case AN_REGDOMAIN_JAPAN:
280 		chan_min = 14; chan_max = 14; break;
281 	case AN_REGDOMAIN_SPAIN:
282 		chan_min = 10; chan_max = 11; break;
283 	case AN_REGDOMAIN_FRANCE:
284 		chan_min = 10; chan_max = 13; break;
285 	case AN_REGDOMAIN_JAPANWIDE:
286 		chan_min = 1; chan_max = 14; break;
287 	}
288 
289 	for (chan = chan_min; chan <= chan_max; chan++) {
290 		ic->ic_channels[chan].ic_freq =
291 		    ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ);
292 		ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_B;
293 	}
294 	ic->ic_ibss_chan = &ic->ic_channels[chan_min];
295 
296 	aprint_normal("%s: 802.11 address: %s, channel: %d-%d\n",
297 	    ifp->if_xname, ether_sprintf(ic->ic_myaddr), chan_min, chan_max);
298 
299 	/* Find supported rate */
300 	for (i = 0; i < sizeof(sc->sc_caps.an_rates); i++) {
301 		if (sc->sc_caps.an_rates[i] == 0)
302 			continue;
303 		ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[
304 		    ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates++] =
305 		    sc->sc_caps.an_rates[i];
306 	}
307 
308 	/*
309 	 * Call MI attach routine.
310 	 */
311 	if_attach(ifp);
312 	ieee80211_ifattach(ic);
313 
314 	sc->sc_newstate = ic->ic_newstate;
315 	ic->ic_newstate = an_newstate;
316 
317 	ieee80211_media_init(ic, an_media_change, an_media_status);
318 
319 	/*
320 	 * radiotap BPF device
321 	 */
322 	bpf_ops->bpf_attach(ifp, DLT_IEEE802_11_RADIO,
323 	    sizeof(struct ieee80211_frame) + 64, &sc->sc_drvbpf);
324 
325 	memset(&sc->sc_rxtapu, 0, sizeof(sc->sc_rxtapu));
326 	sc->sc_rxtap.ar_ihdr.it_len = htole16(sizeof(sc->sc_rxtapu));
327 	sc->sc_rxtap.ar_ihdr.it_present = htole32(AN_RX_RADIOTAP_PRESENT);
328 
329 	memset(&sc->sc_txtapu, 0, sizeof(sc->sc_txtapu));
330 	sc->sc_txtap.at_ihdr.it_len = htole16(sizeof(sc->sc_txtapu));
331 	sc->sc_txtap.at_ihdr.it_present = htole32(AN_TX_RADIOTAP_PRESENT);
332 
333 	sc->sc_attached = 1;
334 	splx(s);
335 
336 	ieee80211_announce(ic);
337 	return 0;
338 }
339 
340 #ifdef AN_DEBUG
341 /*
342  * Setup sysctl(3) MIB, hw.an.*
343  *
344  * TBD condition CTLFLAG_PERMANENT on being a module or not
345  */
346 SYSCTL_SETUP(sysctl_an, "sysctl an(4) subtree setup")
347 {
348 	int rc;
349 	const struct sysctlnode *cnode, *rnode;
350 
351 	if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
352 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
353 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
354 		goto err;
355 
356 	if ((rc = sysctl_createv(clog, 0, &rnode, &rnode,
357 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "an",
358 	    "Cisco/Aironet 802.11 controls",
359 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
360 		goto err;
361 
362 	/* control debugging printfs */
363 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
364 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
365 	    "debug", SYSCTL_DESCR("Enable Cisco/Aironet debugging output"),
366 	    an_sysctl_verify_debug, 0, &an_debug, 0,
367 	    CTL_CREATE, CTL_EOL)) != 0)
368 		goto err;
369 
370 	return;
371 err:
372 	printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
373 }
374 
375 static int
376 an_sysctl_verify(SYSCTLFN_ARGS, int lower, int upper)
377 {
378 	int error, t;
379 	struct sysctlnode node;
380 
381 	node = *rnode;
382 	t = *(int*)rnode->sysctl_data;
383 	node.sysctl_data = &t;
384 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
385 	if (error || newp == NULL)
386 		return (error);
387 
388 	if (t < lower || t > upper)
389 		return (EINVAL);
390 
391 	*(int*)rnode->sysctl_data = t;
392 
393 	return (0);
394 }
395 
396 static int
397 an_sysctl_verify_debug(SYSCTLFN_ARGS)
398 {
399 	return an_sysctl_verify(SYSCTLFN_CALL(rnode), 0, 2);
400 }
401 #endif /* AN_DEBUG */
402 
403 int
404 an_detach(struct an_softc *sc)
405 {
406 	struct ieee80211com *ic = &sc->sc_ic;
407 	struct ifnet *ifp = &sc->sc_if;
408 	int s;
409 
410 	if (!sc->sc_attached)
411 		return 0;
412 
413 	s = splnet();
414 	an_stop(ifp, 1);
415 	ieee80211_ifdetach(ic);
416 	if_detach(ifp);
417 	splx(s);
418 	return 0;
419 }
420 
421 int
422 an_activate(device_t self, enum devact act)
423 {
424 	struct an_softc *sc = device_private(self);
425 
426 	switch (act) {
427 	case DVACT_DEACTIVATE:
428 		if_deactivate(&sc->sc_if);
429 		return 0;
430 	default:
431 		return EOPNOTSUPP;
432 	}
433 }
434 
435 int
436 an_intr(void *arg)
437 {
438 	struct an_softc *sc = arg;
439 	struct ifnet *ifp = &sc->sc_if;
440 	int i;
441 	u_int16_t status;
442 
443 	if (!sc->sc_enabled || !device_is_active(sc->sc_dev) ||
444 	    (ifp->if_flags & IFF_RUNNING) == 0)
445 		return 0;
446 
447 	if ((ifp->if_flags & IFF_UP) == 0) {
448 		CSR_WRITE_2(sc, AN_INT_EN, 0);
449 		CSR_WRITE_2(sc, AN_EVENT_ACK, ~0);
450 		return 1;
451 	}
452 
453 	/* maximum 10 loops per interrupt */
454 	for (i = 0; i < 10; i++) {
455 		if (!sc->sc_enabled || !device_is_active(sc->sc_dev))
456 			return 1;
457 		if (CSR_READ_2(sc, AN_SW0) != AN_MAGIC) {
458 			DPRINTF(("an_intr: magic number changed: %x\n",
459 			    CSR_READ_2(sc, AN_SW0)));
460 			config_deactivate(sc->sc_dev);
461 			return 1;
462 		}
463 		status = CSR_READ_2(sc, AN_EVENT_STAT);
464 		CSR_WRITE_2(sc, AN_EVENT_ACK, status & ~(AN_INTRS));
465 		if ((status & AN_INTRS) == 0)
466 			break;
467 
468 		if (status & AN_EV_RX)
469 			an_rx_intr(sc);
470 
471 		if (status & (AN_EV_TX | AN_EV_TX_EXC))
472 			an_tx_intr(sc, status);
473 
474 		if (status & AN_EV_LINKSTAT)
475 			an_linkstat_intr(sc);
476 
477 		if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
478 		    sc->sc_ic.ic_state == IEEE80211_S_RUN &&
479 		    !IFQ_IS_EMPTY(&ifp->if_snd))
480 			an_start(ifp);
481 	}
482 
483 	return 1;
484 }
485 
486 static int
487 an_init(struct ifnet *ifp)
488 {
489 	struct an_softc *sc = ifp->if_softc;
490 	struct ieee80211com *ic = &sc->sc_ic;
491 	int i, error, fid;
492 
493 	DPRINTF(("an_init: enabled %d\n", sc->sc_enabled));
494 	if (!sc->sc_enabled) {
495 		if (sc->sc_enable)
496 			(*sc->sc_enable)(sc);
497 		an_wait(sc);
498 		sc->sc_enabled = 1;
499 	} else {
500 		an_stop(ifp, 0);
501 		if ((error = an_reset(sc)) != 0) {
502 			printf("%s: failed to reset\n", ifp->if_xname);
503 			an_stop(ifp, 1);
504 			return error;
505 		}
506 	}
507 	CSR_WRITE_2(sc, AN_SW0, AN_MAGIC);
508 
509 	/* Allocate the TX buffers */
510 	for (i = 0; i < AN_TX_RING_CNT; i++) {
511 		if ((error = an_alloc_fid(sc, AN_TX_MAX_LEN, &fid)) != 0) {
512 			printf("%s: failed to allocate nic memory\n",
513 			    ifp->if_xname);
514 			an_stop(ifp, 1);
515 			return error;
516 		}
517 		DPRINTF2(("an_init: txbuf %d allocated %x\n", i, fid));
518 		sc->sc_txd[i].d_fid = fid;
519 		sc->sc_txd[i].d_inuse = 0;
520 	}
521 	sc->sc_txcur = sc->sc_txnext = 0;
522 
523 	IEEE80211_ADDR_COPY(sc->sc_config.an_macaddr, ic->ic_myaddr);
524 	sc->sc_config.an_scanmode = htole16(AN_SCANMODE_ACTIVE);
525 	sc->sc_config.an_authtype = htole16(AN_AUTHTYPE_OPEN);	/*XXX*/
526 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
527 		sc->sc_config.an_authtype |=
528 		    htole16(AN_AUTHTYPE_PRIVACY_IN_USE);
529 		if (sc->sc_use_leap)
530 			sc->sc_config.an_authtype |=
531 			    htole16(AN_AUTHTYPE_LEAP);
532 	}
533 	sc->sc_config.an_listen_interval = htole16(ic->ic_lintval);
534 	sc->sc_config.an_beacon_period = htole16(ic->ic_lintval);
535 	if (ic->ic_flags & IEEE80211_F_PMGTON)
536 		sc->sc_config.an_psave_mode = htole16(AN_PSAVE_PSP);
537 	else
538 		sc->sc_config.an_psave_mode = htole16(AN_PSAVE_CAM);
539 	sc->sc_config.an_ds_channel =
540 	    htole16(ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
541 
542 	switch (ic->ic_opmode) {
543 	case IEEE80211_M_STA:
544 		sc->sc_config.an_opmode =
545 		    htole16(AN_OPMODE_INFRASTRUCTURE_STATION);
546 		sc->sc_config.an_rxmode = htole16(AN_RXMODE_BC_MC_ADDR);
547 		break;
548 	case IEEE80211_M_IBSS:
549 		sc->sc_config.an_opmode = htole16(AN_OPMODE_IBSS_ADHOC);
550 		sc->sc_config.an_rxmode = htole16(AN_RXMODE_BC_MC_ADDR);
551 		break;
552 	case IEEE80211_M_MONITOR:
553 		sc->sc_config.an_opmode =
554 		    htole16(AN_OPMODE_INFRASTRUCTURE_STATION);
555 		sc->sc_config.an_rxmode =
556 		    htole16(AN_RXMODE_80211_MONITOR_ANYBSS);
557 		sc->sc_config.an_authtype = htole16(AN_AUTHTYPE_NONE);
558 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
559 			sc->sc_config.an_authtype |=
560 			    htole16(AN_AUTHTYPE_PRIVACY_IN_USE |
561 		            AN_AUTHTYPE_ALLOW_UNENCRYPTED);
562 		break;
563 	default:
564 		printf("%s: bad opmode %d\n", ifp->if_xname, ic->ic_opmode);
565 		an_stop(ifp, 1);
566 		return EIO;
567 	}
568 	sc->sc_config.an_rxmode |= htole16(AN_RXMODE_NO_8023_HEADER);
569 
570 	/* Set the ssid list */
571 	memset(&sc->sc_buf, 0, sizeof(sc->sc_buf.sc_ssidlist));
572 	sc->sc_buf.sc_ssidlist.an_entry[0].an_ssid_len =
573 	    htole16(ic->ic_des_esslen);
574 	if (ic->ic_des_esslen)
575 		memcpy(sc->sc_buf.sc_ssidlist.an_entry[0].an_ssid,
576 		    ic->ic_des_essid, ic->ic_des_esslen);
577 	if (an_write_rid(sc, AN_RID_SSIDLIST, &sc->sc_buf,
578 	    sizeof(sc->sc_buf.sc_ssidlist)) != 0) {
579 		printf("%s: failed to write ssid list\n", ifp->if_xname);
580 		an_stop(ifp, 1);
581 		return error;
582 	}
583 
584 	/* Set the AP list */
585 	memset(&sc->sc_buf, 0, sizeof(sc->sc_buf.sc_aplist));
586 	(void)an_write_rid(sc, AN_RID_APLIST, &sc->sc_buf,
587 	    sizeof(sc->sc_buf.sc_aplist));
588 
589 	/* Set the encapsulation */
590 	for (i = 0; i < AN_ENCAP_NENTS; i++) {
591 		sc->sc_buf.sc_encap.an_entry[i].an_ethertype = htole16(0);
592 		sc->sc_buf.sc_encap.an_entry[i].an_action =
593 		    htole16(AN_RXENCAP_RFC1024 | AN_TXENCAP_RFC1024);
594 	}
595 	(void)an_write_rid(sc, AN_RID_ENCAP, &sc->sc_buf,
596 	    sizeof(sc->sc_buf.sc_encap));
597 
598 	/* Set the WEP Keys */
599 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
600 		an_write_wepkey(sc, AN_RID_WEP_VOLATILE, sc->sc_wepkeys,
601 		    sc->sc_tx_key);
602 
603 	/* Set the configuration */
604 #ifdef AN_DEBUG
605 	if (an_debug) {
606 		printf("write config:\n");
607 		for (i = 0; i < sizeof(sc->sc_config) / 2; i++)
608 			printf(" %04x", ((u_int16_t *)&sc->sc_config)[i]);
609 		printf("\n");
610 	}
611 #endif
612 	if (an_write_rid(sc, AN_RID_GENCONFIG, &sc->sc_config,
613 	    sizeof(sc->sc_config)) != 0) {
614 		printf("%s: failed to write config\n", ifp->if_xname);
615 		an_stop(ifp, 1);
616 		return error;
617 	}
618 
619 	/* Enable the MAC */
620 	if (an_cmd(sc, AN_CMD_ENABLE, 0)) {
621 		aprint_error_dev(sc->sc_dev, "failed to enable MAC\n");
622 		an_stop(ifp, 1);
623 		return ENXIO;
624 	}
625 	if (ifp->if_flags & IFF_PROMISC)
626 		an_cmd(sc, AN_CMD_SET_MODE, 0xffff);
627 
628 	ifp->if_flags |= IFF_RUNNING;
629 	ifp->if_flags &= ~IFF_OACTIVE;
630 	ic->ic_state = IEEE80211_S_INIT;
631 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
632 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
633 
634 	/* enable interrupts */
635 	CSR_WRITE_2(sc, AN_INT_EN, AN_INTRS);
636 	return 0;
637 }
638 
639 static void
640 an_stop(struct ifnet *ifp, int disable)
641 {
642 	struct an_softc *sc = ifp->if_softc;
643 	int i, s;
644 
645 	if (!sc->sc_enabled)
646 		return;
647 
648 	DPRINTF(("an_stop: disable %d\n", disable));
649 
650 	s = splnet();
651 	ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1);
652 	if (device_is_active(sc->sc_dev)) {
653 		an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0);
654 		CSR_WRITE_2(sc, AN_INT_EN, 0);
655 		an_cmd(sc, AN_CMD_DISABLE, 0);
656 
657 		for (i = 0; i < AN_TX_RING_CNT; i++)
658 			an_cmd(sc, AN_CMD_DEALLOC_MEM, sc->sc_txd[i].d_fid);
659 	}
660 
661 	sc->sc_tx_timer = 0;
662 	ifp->if_timer = 0;
663 	ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
664 
665 	if (disable) {
666 		if (sc->sc_disable)
667 			(*sc->sc_disable)(sc);
668 		sc->sc_enabled = 0;
669 	}
670 	splx(s);
671 }
672 
673 static void
674 an_start(struct ifnet *ifp)
675 {
676 	struct an_softc *sc = (struct an_softc *)ifp->if_softc;
677 	struct ieee80211com *ic = &sc->sc_ic;
678 	struct ieee80211_node *ni;
679 	struct ieee80211_frame *wh;
680 	struct an_txframe frmhdr;
681 	struct ether_header *eh;
682 	struct mbuf *m;
683 	u_int16_t len;
684 	int cur, fid;
685 
686 	if (!sc->sc_enabled || !device_is_active(sc->sc_dev)) {
687 		DPRINTF(("an_start: noop: enabled %d invalid %d\n",
688 		    sc->sc_enabled, !device_is_active(sc->sc_dev)));
689 		return;
690 	}
691 
692 	memset(&frmhdr, 0, sizeof(frmhdr));
693 	cur = sc->sc_txnext;
694 	for (;;) {
695 		if (ic->ic_state != IEEE80211_S_RUN) {
696 			DPRINTF(("an_start: not running %d\n", ic->ic_state));
697 			break;
698 		}
699 		IFQ_POLL(&ifp->if_snd, m);
700 		if (m == NULL) {
701 			DPRINTF2(("an_start: no pending mbuf\n"));
702 			break;
703 		}
704 		if (sc->sc_txd[cur].d_inuse) {
705 			DPRINTF2(("an_start: %x/%d busy\n",
706 			    sc->sc_txd[cur].d_fid, cur));
707 			ifp->if_flags |= IFF_OACTIVE;
708 			break;
709 		}
710 		IFQ_DEQUEUE(&ifp->if_snd, m);
711 		ifp->if_opackets++;
712 		if (ifp->if_bpf)
713 			bpf_ops->bpf_mtap(ifp->if_bpf, m);
714 		eh = mtod(m, struct ether_header *);
715 		ni = ieee80211_find_txnode(ic, eh->ether_dhost);
716 		if (ni == NULL) {
717 			/* NB: ieee80211_find_txnode does stat+msg */
718 			goto bad;
719 		}
720 		if ((m = ieee80211_encap(ic, m, ni)) == NULL)
721 			goto bad;
722 		ieee80211_free_node(ni);
723 		if (ic->ic_rawbpf)
724 			bpf_ops->bpf_mtap(ic->ic_rawbpf, m);
725 
726 		wh = mtod(m, struct ieee80211_frame *);
727 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
728 			wh->i_fc[1] |= IEEE80211_FC1_WEP;
729 		m_copydata(m, 0, sizeof(struct ieee80211_frame),
730 		    (void *)&frmhdr.an_whdr);
731 
732 		/* insert payload length in front of llc/snap */
733 		len = htons(m->m_pkthdr.len - sizeof(struct ieee80211_frame));
734 		m_adj(m, sizeof(struct ieee80211_frame) - sizeof(len));
735 		if (mtod(m, u_long) & 0x01)
736 			memcpy(mtod(m, void *), &len, sizeof(len));
737 		else
738 			*mtod(m, u_int16_t *) = len;
739 
740 		/*
741 		 * XXX Aironet firmware apparently convert the packet
742 		 * with longer than 1500 bytes in length into LLC/SNAP.
743 		 * If we have 1500 bytes in ethernet payload, it is
744 		 * 1508 bytes including LLC/SNAP and will be inserted
745 		 * additional LLC/SNAP header with 1501-1508 in its
746 		 * ethertype !!
747 		 * So we skip LLC/SNAP header and force firmware to
748 		 * convert it to LLC/SNAP again.
749 		 */
750 		m_adj(m, sizeof(struct llc));
751 
752 		frmhdr.an_tx_ctl = htole16(AN_TXCTL_80211);
753 		frmhdr.an_tx_payload_len = htole16(m->m_pkthdr.len);
754 		frmhdr.an_gaplen = htole16(AN_TXGAP_802_11);
755 
756 		if (ic->ic_fixed_rate != -1)
757 			frmhdr.an_tx_rate =
758 			    ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[
759 			    ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
760 		else
761 			frmhdr.an_tx_rate = 0;
762 
763 		/* XXX radiotap for tx must be completed */
764 		if (sc->sc_drvbpf) {
765 			struct an_tx_radiotap_header *tap = &sc->sc_txtap;
766 			tap->at_rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate];
767 			tap->at_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
768 			tap->at_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
769 			/* TBD tap->wt_flags */
770 			bpf_ops->bpf_mtap2(sc->sc_drvbpf, tap, tap->at_ihdr.it_len, m);
771 		}
772 
773 #ifdef AN_DEBUG
774 		if ((ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) ==
775 		    (IFF_DEBUG|IFF_LINK2)) {
776 			ieee80211_dump_pkt((u_int8_t *)&frmhdr.an_whdr,
777 			    sizeof(struct ieee80211_frame), -1, 0);
778 			printf(" txctl 0x%x plen %u\n",
779 			    le16toh(frmhdr.an_tx_ctl),
780 			    le16toh(frmhdr.an_tx_payload_len));
781 		}
782 #endif
783 		if (sizeof(frmhdr) + AN_TXGAP_802_11 + sizeof(len) +
784 		    m->m_pkthdr.len > AN_TX_MAX_LEN)
785 			goto bad;
786 
787 		fid = sc->sc_txd[cur].d_fid;
788 		if (an_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0)
789 			goto bad;
790 		/* dummy write to avoid seek. */
791 		an_write_bap(sc, fid, -1, &frmhdr, AN_TXGAP_802_11);
792 		an_mwrite_bap(sc, fid, -1, m, m->m_pkthdr.len);
793 		m_freem(m);
794 
795 		DPRINTF2(("an_start: send %zu byte via %x/%d\n",
796 		    ntohs(len) + sizeof(struct ieee80211_frame),
797 		    fid, cur));
798 		sc->sc_txd[cur].d_inuse = 1;
799 		if (an_cmd(sc, AN_CMD_TX, fid)) {
800 			printf("%s: xmit failed\n", ifp->if_xname);
801 			sc->sc_txd[cur].d_inuse = 0;
802 			continue;
803 		}
804 		sc->sc_tx_timer = 5;
805 		ifp->if_timer = 1;
806 		AN_INC(cur, AN_TX_RING_CNT);
807 		sc->sc_txnext = cur;
808 		continue;
809 bad:
810 		ifp->if_oerrors++;
811 		m_freem(m);
812 	}
813 }
814 
815 static int
816 an_reset(struct an_softc *sc)
817 {
818 
819 	DPRINTF(("an_reset\n"));
820 
821 	if (!sc->sc_enabled)
822 		return ENXIO;
823 
824 	an_cmd(sc, AN_CMD_ENABLE, 0);
825 	an_cmd(sc, AN_CMD_FW_RESTART, 0);
826 	an_cmd(sc, AN_CMD_NOOP2, 0);
827 
828 	if (an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0) == ETIMEDOUT) {
829 		aprint_error_dev(sc->sc_dev, "reset failed\n");
830 		return ETIMEDOUT;
831 	}
832 
833 	an_cmd(sc, AN_CMD_DISABLE, 0);
834 	return 0;
835 }
836 
837 static void
838 an_watchdog(struct ifnet *ifp)
839 {
840 	struct an_softc *sc = ifp->if_softc;
841 
842 	if (!sc->sc_enabled)
843 		return;
844 
845 	if (sc->sc_tx_timer) {
846 		if (--sc->sc_tx_timer == 0) {
847 			printf("%s: device timeout\n", ifp->if_xname);
848 			ifp->if_oerrors++;
849 			an_init(ifp);
850 			return;
851 		}
852 		ifp->if_timer = 1;
853 	}
854 	ieee80211_watchdog(&sc->sc_ic);
855 }
856 
857 static int
858 an_ioctl(struct ifnet *ifp, u_long command, void *data)
859 {
860 	struct an_softc *sc = ifp->if_softc;
861 	int s, error = 0;
862 
863 	if (!device_is_active(sc->sc_dev))
864 		return ENXIO;
865 
866 	s = splnet();
867 
868 	switch (command) {
869 	case SIOCSIFFLAGS:
870 		if ((error = ifioctl_common(ifp, command, data)) != 0)
871 			break;
872 		if (ifp->if_flags & IFF_UP) {
873 			if (sc->sc_enabled) {
874 				/*
875 				 * To avoid rescanning another access point,
876 				 * do not call an_init() here.  Instead, only
877 				 * reflect promisc mode settings.
878 				 */
879 				error = an_cmd(sc, AN_CMD_SET_MODE,
880 				    (ifp->if_flags & IFF_PROMISC) ? 0xffff : 0);
881 			} else
882 				error = an_init(ifp);
883 		} else if (sc->sc_enabled)
884 			an_stop(ifp, 1);
885 		break;
886 	case SIOCADDMULTI:
887 	case SIOCDELMULTI:
888 		error = ether_ioctl(ifp, command, data);
889 		if (error == ENETRESET) {
890 			/* we don't have multicast filter. */
891 			error = 0;
892 		}
893 		break;
894 	case SIOCS80211NWKEY:
895 		error = an_set_nwkey(sc, (struct ieee80211_nwkey *)data);
896 			break;
897 	case SIOCG80211NWKEY:
898 		error = an_get_nwkey(sc, (struct ieee80211_nwkey *)data);
899 		break;
900 	default:
901 		error = ieee80211_ioctl(&sc->sc_ic, command, data);
902 		break;
903 	}
904 	if (error == ENETRESET) {
905 		if (sc->sc_enabled)
906 			error = an_init(ifp);
907 		else
908 			error = 0;
909 	}
910 	splx(s);
911 	return error;
912 }
913 
914 /* TBD factor with ieee80211_media_change */
915 static int
916 an_media_change(struct ifnet *ifp)
917 {
918 	struct an_softc *sc = ifp->if_softc;
919 	struct ieee80211com *ic = &sc->sc_ic;
920 	struct ifmedia_entry *ime;
921 	enum ieee80211_opmode newmode;
922 	int i, rate, error = 0;
923 
924 	ime = ic->ic_media.ifm_cur;
925 	if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) {
926 		i = -1;
927 	} else {
928 		struct ieee80211_rateset *rs =
929 		    &ic->ic_sup_rates[IEEE80211_MODE_11B];
930 		rate = ieee80211_media2rate(ime->ifm_media);
931 		if (rate == 0)
932 			return EINVAL;
933 		for (i = 0; i < rs->rs_nrates; i++) {
934 			if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
935 				break;
936 		}
937 		if (i == rs->rs_nrates)
938 			return EINVAL;
939 	}
940 	if (ic->ic_fixed_rate != i) {
941 		ic->ic_fixed_rate = i;
942 		error = ENETRESET;
943 	}
944 
945 	if (ime->ifm_media & IFM_IEEE80211_ADHOC)
946 		newmode = IEEE80211_M_IBSS;
947 	else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
948 		newmode = IEEE80211_M_HOSTAP;
949 	else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
950 		newmode = IEEE80211_M_MONITOR;
951 	else
952 		newmode = IEEE80211_M_STA;
953 	if (ic->ic_opmode != newmode) {
954 		ic->ic_opmode = newmode;
955 		error = ENETRESET;
956 	}
957 	if (error == ENETRESET) {
958 		if (sc->sc_enabled)
959 			error = an_init(ifp);
960 		else
961 			error = 0;
962 	}
963 	ifp->if_baudrate = ifmedia_baudrate(ic->ic_media.ifm_cur->ifm_media);
964 
965 	return error;
966 }
967 
968 static void
969 an_media_status(struct ifnet *ifp, struct ifmediareq *imr)
970 {
971 	struct an_softc *sc = ifp->if_softc;
972 	struct ieee80211com *ic = &sc->sc_ic;
973 	int rate, buflen;
974 
975 	if (sc->sc_enabled == 0) {
976 		imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
977 		imr->ifm_status = 0;
978 		return;
979 	}
980 
981 	imr->ifm_status = IFM_AVALID;
982 	imr->ifm_active = IFM_IEEE80211;
983 	if (ic->ic_state == IEEE80211_S_RUN)
984 		imr->ifm_status |= IFM_ACTIVE;
985 	buflen = sizeof(sc->sc_buf);
986 	if (ic->ic_fixed_rate != -1)
987 		rate = ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[
988 		    ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
989 	else if (an_read_rid(sc, AN_RID_STATUS, &sc->sc_buf, &buflen) != 0)
990 		rate = 0;
991 	else
992 		rate = le16toh(sc->sc_buf.sc_status.an_current_tx_rate);
993 	imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
994 	switch (ic->ic_opmode) {
995 	case IEEE80211_M_STA:
996 		break;
997 	case IEEE80211_M_IBSS:
998 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
999 		break;
1000 	case IEEE80211_M_HOSTAP:
1001 		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1002 		break;
1003 	case IEEE80211_M_MONITOR:
1004 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
1005 		break;
1006 	default:
1007 		break;
1008 	}
1009 }
1010 
1011 static int
1012 an_set_nwkey(struct an_softc *sc, struct ieee80211_nwkey *nwkey)
1013 {
1014 	int error;
1015 	struct ieee80211com *ic = &sc->sc_ic;
1016 	u_int16_t prevauth;
1017 
1018 	error = 0;
1019 	prevauth = sc->sc_config.an_authtype;
1020 
1021 	switch (nwkey->i_wepon) {
1022 	case IEEE80211_NWKEY_OPEN:
1023 		sc->sc_config.an_authtype = AN_AUTHTYPE_OPEN;
1024 		ic->ic_flags &= ~IEEE80211_F_PRIVACY;
1025 		break;
1026 
1027 	case IEEE80211_NWKEY_WEP:
1028 	case IEEE80211_NWKEY_WEP | IEEE80211_NWKEY_PERSIST:
1029 		error = an_set_nwkey_wep(sc, nwkey);
1030 		if (error == 0 || error == ENETRESET) {
1031 			sc->sc_config.an_authtype =
1032 			    AN_AUTHTYPE_OPEN | AN_AUTHTYPE_PRIVACY_IN_USE;
1033 			ic->ic_flags |= IEEE80211_F_PRIVACY;
1034 		}
1035 		break;
1036 
1037 	case IEEE80211_NWKEY_EAP:
1038 		error = an_set_nwkey_eap(sc, nwkey);
1039 		if (error == 0 || error == ENETRESET) {
1040 			sc->sc_config.an_authtype = AN_AUTHTYPE_OPEN |
1041 			    AN_AUTHTYPE_PRIVACY_IN_USE | AN_AUTHTYPE_LEAP;
1042 			ic->ic_flags |= IEEE80211_F_PRIVACY;
1043 		}
1044 		break;
1045 	default:
1046 		error = EINVAL;
1047 		break;
1048 	}
1049 	if (error == 0 && prevauth != sc->sc_config.an_authtype)
1050 		error = ENETRESET;
1051 	return error;
1052 }
1053 
1054 static int
1055 an_set_nwkey_wep(struct an_softc *sc, struct ieee80211_nwkey *nwkey)
1056 {
1057 	int i, txkey, anysetkey, needreset, error;
1058 	struct an_wepkey keys[IEEE80211_WEP_NKID];
1059 
1060 	error = 0;
1061 	memset(keys, 0, sizeof(keys));
1062 	anysetkey = needreset = 0;
1063 
1064 	/* load argument and sanity check */
1065 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1066 		keys[i].an_wep_keylen = nwkey->i_key[i].i_keylen;
1067 		if (keys[i].an_wep_keylen < 0)
1068 			continue;
1069 		if (keys[i].an_wep_keylen != 0 &&
1070 		    keys[i].an_wep_keylen < IEEE80211_WEP_KEYLEN)
1071 			return EINVAL;
1072 		if (keys[i].an_wep_keylen > sizeof(keys[i].an_wep_key))
1073 			return EINVAL;
1074 		if ((error = copyin(nwkey->i_key[i].i_keydat,
1075 		    keys[i].an_wep_key, keys[i].an_wep_keylen)) != 0)
1076 			return error;
1077 		anysetkey++;
1078 	}
1079 	txkey = nwkey->i_defkid - 1;
1080 	if (txkey >= 0) {
1081 		if (txkey >= IEEE80211_WEP_NKID)
1082 			return EINVAL;
1083 		/* default key must have a valid value */
1084 		if (keys[txkey].an_wep_keylen == 0 ||
1085 		    (keys[txkey].an_wep_keylen < 0 &&
1086 		    sc->sc_perskeylen[txkey] == 0))
1087 			return EINVAL;
1088 		anysetkey++;
1089 	}
1090 	DPRINTF(("an_set_nwkey_wep: %s: %sold(%d:%d,%d,%d,%d) "
1091 	    "pers(%d:%d,%d,%d,%d) new(%d:%d,%d,%d,%d)\n",
1092 	    device_xname(sc->sc_dev),
1093 	    ((nwkey->i_wepon & IEEE80211_NWKEY_PERSIST) ? "persist: " : ""),
1094 	    sc->sc_tx_key,
1095 	    sc->sc_wepkeys[0].an_wep_keylen, sc->sc_wepkeys[1].an_wep_keylen,
1096 	    sc->sc_wepkeys[2].an_wep_keylen, sc->sc_wepkeys[3].an_wep_keylen,
1097 	    sc->sc_tx_perskey,
1098 	    sc->sc_perskeylen[0], sc->sc_perskeylen[1],
1099 	    sc->sc_perskeylen[2], sc->sc_perskeylen[3],
1100 	    txkey,
1101 	    keys[0].an_wep_keylen, keys[1].an_wep_keylen,
1102 	    keys[2].an_wep_keylen, keys[3].an_wep_keylen));
1103 	if (!(nwkey->i_wepon & IEEE80211_NWKEY_PERSIST)) {
1104 		/* set temporary keys */
1105 		sc->sc_tx_key = txkey;
1106 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1107 			if (keys[i].an_wep_keylen < 0)
1108 				continue;
1109 			memcpy(&sc->sc_wepkeys[i], &keys[i], sizeof(keys[i]));
1110 		}
1111 	} else {
1112 		/* set persist keys */
1113 		if (anysetkey) {
1114 			/* prepare to write nvram */
1115 			if (!sc->sc_enabled) {
1116 				if (sc->sc_enable)
1117 					(*sc->sc_enable)(sc);
1118 				an_wait(sc);
1119 				sc->sc_enabled = 1;
1120 				error = an_write_wepkey(sc,
1121 				    AN_RID_WEP_PERSISTENT, keys, txkey);
1122 				if (sc->sc_disable)
1123 					(*sc->sc_disable)(sc);
1124 				sc->sc_enabled = 0;
1125 			} else {
1126 				an_cmd(sc, AN_CMD_DISABLE, 0);
1127 				error = an_write_wepkey(sc,
1128 				    AN_RID_WEP_PERSISTENT, keys, txkey);
1129 				an_cmd(sc, AN_CMD_ENABLE, 0);
1130 			}
1131 			if (error)
1132 				return error;
1133 		}
1134 		if (txkey >= 0)
1135 			sc->sc_tx_perskey = txkey;
1136 		if (sc->sc_tx_key >= 0) {
1137 			sc->sc_tx_key = -1;
1138 			needreset++;
1139 		}
1140 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1141 			if (sc->sc_wepkeys[i].an_wep_keylen >= 0) {
1142 				memset(&sc->sc_wepkeys[i].an_wep_key, 0,
1143 				    sizeof(sc->sc_wepkeys[i].an_wep_key));
1144 				sc->sc_wepkeys[i].an_wep_keylen = -1;
1145 				needreset++;
1146 			}
1147 			if (keys[i].an_wep_keylen >= 0)
1148 				sc->sc_perskeylen[i] = keys[i].an_wep_keylen;
1149 		}
1150 	}
1151 	if (needreset) {
1152 		/* firmware restart to reload persistent key */
1153 		an_reset(sc);
1154 	}
1155 	if (anysetkey || needreset)
1156 		error = ENETRESET;
1157 	return error;
1158 }
1159 
1160 static int
1161 an_set_nwkey_eap(struct an_softc *sc, struct ieee80211_nwkey *nwkey)
1162 {
1163 	int i, error, len;
1164 	struct ifnet *ifp = &sc->sc_if;
1165 	struct an_rid_leapkey *key;
1166 	u_int16_t unibuf[sizeof(key->an_key)];
1167 	static const int leap_rid[] = { AN_RID_LEAP_PASS, AN_RID_LEAP_USER };
1168 	MD4_CTX ctx;
1169 
1170 	error = 0;
1171 
1172 	if (nwkey->i_key[0].i_keydat == NULL &&
1173 	    nwkey->i_key[1].i_keydat == NULL)
1174 		return 0;
1175 	if (!sc->sc_enabled)
1176 		return ENXIO;
1177 	an_cmd(sc, AN_CMD_DISABLE, 0);
1178 	key = &sc->sc_buf.sc_leapkey;
1179 	for (i = 0; i < 2; i++) {
1180 		if (nwkey->i_key[i].i_keydat == NULL)
1181 			continue;
1182 		len = nwkey->i_key[i].i_keylen;
1183 		if (len > sizeof(key->an_key))
1184 			return EINVAL;
1185 		memset(key, 0, sizeof(*key));
1186 		key->an_key_len = htole16(len);
1187 		if ((error = copyin(nwkey->i_key[i].i_keydat, key->an_key,
1188 		    len)) != 0)
1189 			return error;
1190 		if (i == 1) {
1191 			/*
1192 			 * Cisco seems to use PasswordHash and PasswordHashHash
1193 			 * in RFC-2759 (MS-CHAP-V2).
1194 			 */
1195 			memset(unibuf, 0, sizeof(unibuf));
1196 			/* XXX: convert password to unicode */
1197 			for (i = 0; i < len; i++)
1198 				unibuf[i] = key->an_key[i];
1199 			/* set PasswordHash */
1200 			MD4Init(&ctx);
1201 			MD4Update(&ctx, (u_int8_t *)unibuf, len * 2);
1202 			MD4Final(key->an_key, &ctx);
1203 			/* set PasswordHashHash */
1204 			MD4Init(&ctx);
1205 			MD4Update(&ctx, key->an_key, 16);
1206 			MD4Final(key->an_key + 16, &ctx);
1207 			key->an_key_len = htole16(32);
1208 		}
1209 		if ((error = an_write_rid(sc, leap_rid[i], key,
1210 		    sizeof(*key))) != 0) {
1211 			printf("%s: LEAP set failed\n", ifp->if_xname);
1212 			return error;
1213 		}
1214 	}
1215 	error = an_cmd(sc, AN_CMD_ENABLE, 0);
1216 	if (error)
1217 		printf("%s: an_set_nwkey: failed to enable MAC\n",
1218 		    ifp->if_xname);
1219 	else
1220 		error = ENETRESET;
1221 	return error;
1222 }
1223 
1224 static int
1225 an_get_nwkey(struct an_softc *sc, struct ieee80211_nwkey *nwkey)
1226 {
1227 	int i, error;
1228 
1229 	error = 0;
1230 	if (sc->sc_config.an_authtype & AN_AUTHTYPE_LEAP)
1231 		nwkey->i_wepon = IEEE80211_NWKEY_EAP;
1232 	else if (sc->sc_config.an_authtype & AN_AUTHTYPE_PRIVACY_IN_USE)
1233 		nwkey->i_wepon = IEEE80211_NWKEY_WEP;
1234 	else
1235 		nwkey->i_wepon = IEEE80211_NWKEY_OPEN;
1236 	if (sc->sc_tx_key == -1)
1237 		nwkey->i_defkid = sc->sc_tx_perskey + 1;
1238 	else
1239 		nwkey->i_defkid = sc->sc_tx_key + 1;
1240 	if (nwkey->i_key[0].i_keydat == NULL)
1241 		return 0;
1242 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1243 		if (nwkey->i_key[i].i_keydat == NULL)
1244 			continue;
1245 		/* do not show any keys to non-root user */
1246 		/* XXX-elad: why is this inside a loop? */
1247 		if ((error = kauth_authorize_network(curlwp->l_cred,
1248 		    KAUTH_NETWORK_INTERFACE,
1249 		    KAUTH_REQ_NETWORK_INTERFACE_GETPRIV, sc->sc_ic.ic_ifp,
1250 		    KAUTH_ARG(SIOCG80211NWKEY), NULL)) != 0)
1251 			break;
1252 		nwkey->i_key[i].i_keylen = sc->sc_wepkeys[i].an_wep_keylen;
1253 		if (nwkey->i_key[i].i_keylen < 0) {
1254 			if (sc->sc_perskeylen[i] == 0)
1255 				nwkey->i_key[i].i_keylen = 0;
1256 			continue;
1257 		}
1258 		if ((error = copyout(sc->sc_wepkeys[i].an_wep_key,
1259 		    nwkey->i_key[i].i_keydat,
1260 		    sc->sc_wepkeys[i].an_wep_keylen)) != 0)
1261 			break;
1262 	}
1263 	return error;
1264 }
1265 
1266 static int
1267 an_write_wepkey(struct an_softc *sc, int type, struct an_wepkey *keys, int kid)
1268 {
1269 	int i, error;
1270 	struct an_rid_wepkey *akey;
1271 
1272 	error = 0;
1273 	akey = &sc->sc_buf.sc_wepkey;
1274 	memset(akey, 0, sizeof(struct an_rid_wepkey));
1275 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1276 		if (keys[i].an_wep_keylen < 0 ||
1277 		    keys[i].an_wep_keylen > sizeof(akey->an_key))
1278 			continue;
1279 		akey->an_key_len = htole16(keys[i].an_wep_keylen);
1280 		akey->an_key_index = htole16(i);
1281 		akey->an_mac_addr[0] = 1;	/* default mac */
1282 		memcpy(akey->an_key, keys[i].an_wep_key, keys[i].an_wep_keylen);
1283 		if ((error = an_write_rid(sc, type, akey, sizeof(*akey))) != 0)
1284 			return error;
1285 	}
1286 	if (kid >= 0) {
1287 		akey->an_key_index = htole16(0xffff);
1288 		akey->an_mac_addr[0] = kid;
1289 		akey->an_key_len = htole16(0);
1290 		memset(akey->an_key, 0, sizeof(akey->an_key));
1291 		error = an_write_rid(sc, type, akey, sizeof(*akey));
1292 	}
1293 	return error;
1294 }
1295 
1296 #ifdef AN_DEBUG
1297 static void
1298 an_dump_pkt(const char *devname, struct mbuf *m)
1299 {
1300 	int col, col0, i;
1301 	uint8_t *pkt = mtod(m, uint8_t *);
1302 	const char *delim = "";
1303 	int delimw = 0;
1304 
1305 	printf("%s: pkt ", devname);
1306 	col = col0 = strlen(devname) + strlen(": pkt ");
1307 	for (i = 0; i < m->m_len; i++) {
1308 		printf("%s%02x", delim, pkt[i]);
1309 		delim = ":";
1310 		delimw = 1;
1311 		col += delimw + 2;
1312 		if (col >= 72) {
1313 			printf("\n%*s", col0, "");
1314 			col = col0;
1315 			delim = "";
1316 			delimw = 0;
1317 		}
1318 	}
1319 	if (col != 0)
1320 		printf("\n");
1321 }
1322 #endif /* AN_DEBUG */
1323 
1324 /*
1325  * Low level functions
1326  */
1327 
1328 static void
1329 an_rx_intr(struct an_softc *sc)
1330 {
1331 	struct ieee80211com *ic = &sc->sc_ic;
1332 	struct ifnet *ifp = &sc->sc_if;
1333 	struct ieee80211_frame_min *wh;
1334 	struct ieee80211_node *ni;
1335 	struct an_rxframe frmhdr;
1336 	struct mbuf *m;
1337 	u_int16_t status;
1338 	int fid, gaplen, len, off;
1339 	uint8_t *gap;
1340 
1341 	fid = CSR_READ_2(sc, AN_RX_FID);
1342 
1343 	/* First read in the frame header */
1344 	if (an_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0) {
1345 		CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
1346 		ifp->if_ierrors++;
1347 		DPRINTF(("an_rx_intr: read fid %x failed\n", fid));
1348 		return;
1349 	}
1350 
1351 #ifdef AN_DEBUG
1352 	if ((ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2)) {
1353 		ieee80211_dump_pkt((u_int8_t *)&frmhdr.an_whdr,
1354 		    sizeof(struct ieee80211_frame), frmhdr.an_rx_rate,
1355 		    frmhdr.an_rx_signal_strength);
1356 		printf(" time 0x%x status 0x%x plen %u chan %u"
1357 		    " plcp %02x %02x %02x %02x gap %u\n",
1358 		    le32toh(frmhdr.an_rx_time), le16toh(frmhdr.an_rx_status),
1359 		    le16toh(frmhdr.an_rx_payload_len), frmhdr.an_rx_chan,
1360 		    frmhdr.an_plcp_hdr[0], frmhdr.an_plcp_hdr[1],
1361 		    frmhdr.an_plcp_hdr[2], frmhdr.an_plcp_hdr[3],
1362 		    le16toh(frmhdr.an_gaplen));
1363 	}
1364 #endif
1365 
1366 	status = le16toh(frmhdr.an_rx_status);
1367 	if ((status & AN_STAT_ERRSTAT) != 0 &&
1368 	    ic->ic_opmode != IEEE80211_M_MONITOR) {
1369 		CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
1370 		ifp->if_ierrors++;
1371 		DPRINTF(("an_rx_intr: fid %x status %x\n", fid, status));
1372 		return;
1373 	}
1374 
1375 	/* the payload length field includes a 16-bit "mystery field" */
1376 	len = le16toh(frmhdr.an_rx_payload_len) - sizeof(uint16_t);
1377 	off = ALIGN(sizeof(struct ieee80211_frame));
1378 
1379 	if (off + len > MCLBYTES) {
1380 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1381 			CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
1382 			ifp->if_ierrors++;
1383 			DPRINTF(("an_rx_intr: oversized packet %d\n", len));
1384 			return;
1385 		}
1386 		len = 0;
1387 	}
1388 
1389 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1390 	if (m == NULL) {
1391 		CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
1392 		ifp->if_ierrors++;
1393 		DPRINTF(("an_rx_intr: MGET failed\n"));
1394 		return;
1395 	}
1396 	if (off + len + AN_GAPLEN_MAX > MHLEN) {
1397 		MCLGET(m, M_DONTWAIT);
1398 		if ((m->m_flags & M_EXT) == 0) {
1399 			CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
1400 			m_freem(m);
1401 			ifp->if_ierrors++;
1402 			DPRINTF(("an_rx_intr: MCLGET failed\n"));
1403 			return;
1404 		}
1405 	}
1406 	m->m_data += off - sizeof(struct ieee80211_frame);
1407 
1408 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1409 		gaplen = le16toh(frmhdr.an_gaplen);
1410 		if (gaplen > AN_GAPLEN_MAX) {
1411 			CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
1412 			m_freem(m);
1413 			ifp->if_ierrors++;
1414 			DPRINTF(("%s: gap too long\n", __func__));
1415 			return;
1416 		}
1417 		/*
1418 		 * We don't need the 16-bit mystery field (payload length?),
1419 		 * so read it into the region reserved for the 802.11 header.
1420 		 *
1421 		 * When Cisco Aironet 350 cards w/ firmware version 5 or
1422 		 * greater operate with certain Cisco 350 APs,
1423 		 * the "gap" is filled with the SNAP header.  Read
1424 		 * it in after the 802.11 header.
1425 		 */
1426 		gap = m->m_data + sizeof(struct ieee80211_frame) -
1427 		    sizeof(uint16_t);
1428 		an_read_bap(sc, fid, -1, gap, gaplen + sizeof(u_int16_t));
1429 #ifdef AN_DEBUG
1430 		if ((ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) ==
1431 		    (IFF_DEBUG|IFF_LINK2)) {
1432 			int i;
1433 			printf(" gap&len");
1434 			for (i = 0; i < gaplen + sizeof(u_int16_t); i++)
1435 				printf(" %02x", gap[i]);
1436 			printf("\n");
1437 		}
1438 #endif
1439 	} else
1440 		gaplen = 0;
1441 
1442 	an_read_bap(sc, fid, -1,
1443 	    m->m_data + sizeof(struct ieee80211_frame) + gaplen, len);
1444 	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + gaplen +
1445 	    len;
1446 
1447 	memcpy(m->m_data, &frmhdr.an_whdr, sizeof(struct ieee80211_frame));
1448 	m->m_pkthdr.rcvif = ifp;
1449 	CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
1450 
1451 	if (sc->sc_drvbpf) {
1452 		struct an_rx_radiotap_header *tap = &sc->sc_rxtap;
1453 
1454 		tap->ar_rate = frmhdr.an_rx_rate;
1455 		tap->ar_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1456 		tap->ar_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1457 		tap->ar_antsignal = frmhdr.an_rx_signal_strength;
1458 		if ((le16toh(frmhdr.an_rx_status) & AN_STAT_BADCRC) ||
1459 		    (le16toh(frmhdr.an_rx_status) & AN_STAT_ERRSTAT) ||
1460 		    (le16toh(frmhdr.an_rx_status) & AN_STAT_UNDECRYPTABLE))
1461 		    tap->ar_flags |= IEEE80211_RADIOTAP_F_BADFCS;
1462 
1463 		bpf_ops->bpf_mtap2(sc->sc_drvbpf, tap, tap->ar_ihdr.it_len, m);
1464 	}
1465 	wh = mtod(m, struct ieee80211_frame_min *);
1466 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1467 		/*
1468 		 * WEP is decrypted by hardware. Clear WEP bit
1469 		 * header for ieee80211_input().
1470 		 */
1471 		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1472 	}
1473 
1474 #ifdef AN_DEBUG
1475 	if (an_debug > 1)
1476 		an_dump_pkt(device_xname(sc->sc_dev), m);
1477 #endif /* AN_DEBUG */
1478 
1479 	ni = ieee80211_find_rxnode(ic, wh);
1480 	ieee80211_input(ic, m, ni, frmhdr.an_rx_signal_strength,
1481 	    le32toh(frmhdr.an_rx_time));
1482 	ieee80211_free_node(ni);
1483 }
1484 
1485 static void
1486 an_tx_intr(struct an_softc *sc, int status)
1487 {
1488 	struct ifnet *ifp = &sc->sc_if;
1489 	int cur, fid;
1490 
1491 	sc->sc_tx_timer = 0;
1492 	ifp->if_flags &= ~IFF_OACTIVE;
1493 
1494 	fid = CSR_READ_2(sc, AN_TX_CMP_FID);
1495 	CSR_WRITE_2(sc, AN_EVENT_ACK, status & (AN_EV_TX | AN_EV_TX_EXC));
1496 
1497 	if (status & AN_EV_TX_EXC)
1498 		ifp->if_oerrors++;
1499 	else
1500 		ifp->if_opackets++;
1501 
1502 	cur = sc->sc_txcur;
1503 	if (sc->sc_txd[cur].d_fid == fid) {
1504 		sc->sc_txd[cur].d_inuse = 0;
1505 		DPRINTF2(("an_tx_intr: sent %x/%d\n", fid, cur));
1506 		AN_INC(cur, AN_TX_RING_CNT);
1507 		sc->sc_txcur = cur;
1508 	} else {
1509 		for (cur = 0; cur < AN_TX_RING_CNT; cur++) {
1510 			if (fid == sc->sc_txd[cur].d_fid) {
1511 				sc->sc_txd[cur].d_inuse = 0;
1512 				break;
1513 			}
1514 		}
1515 		if (ifp->if_flags & IFF_DEBUG)
1516 			printf("%s: tx mismatch: "
1517 			    "expected %x(%d), actual %x(%d)\n",
1518 			    device_xname(sc->sc_dev),
1519 			    sc->sc_txd[sc->sc_txcur].d_fid, sc->sc_txcur,
1520 			    fid, cur);
1521 	}
1522 
1523 	return;
1524 }
1525 
1526 static void
1527 an_linkstat_intr(struct an_softc *sc)
1528 {
1529 	struct ieee80211com *ic = &sc->sc_ic;
1530 	u_int16_t status;
1531 
1532 	status = CSR_READ_2(sc, AN_LINKSTAT);
1533 	CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_LINKSTAT);
1534 	DPRINTF(("an_linkstat_intr: status 0x%x\n", status));
1535 
1536 	if (status == AN_LINKSTAT_ASSOCIATED) {
1537 		if (ic->ic_state != IEEE80211_S_RUN ||
1538 		    ic->ic_opmode == IEEE80211_M_IBSS)
1539 			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1540 	} else {
1541 		if (ic->ic_opmode == IEEE80211_M_STA)
1542 			ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1543 	}
1544 }
1545 
1546 /* Must be called at proper protection level! */
1547 static int
1548 an_cmd(struct an_softc *sc, int cmd, int val)
1549 {
1550 	int i, status;
1551 
1552 	/* make sure that previous command completed */
1553 	if (CSR_READ_2(sc, AN_COMMAND) & AN_CMD_BUSY) {
1554 		if (sc->sc_if.if_flags & IFF_DEBUG)
1555 			printf("%s: command 0x%x busy\n", device_xname(sc->sc_dev),
1556 			    CSR_READ_2(sc, AN_COMMAND));
1557 		CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CLR_STUCK_BUSY);
1558 	}
1559 
1560 	CSR_WRITE_2(sc, AN_PARAM0, val);
1561 	CSR_WRITE_2(sc, AN_PARAM1, 0);
1562 	CSR_WRITE_2(sc, AN_PARAM2, 0);
1563 	CSR_WRITE_2(sc, AN_COMMAND, cmd);
1564 
1565 	if (cmd == AN_CMD_FW_RESTART) {
1566 		/* XXX: should sleep here */
1567 		DELAY(100*1000);
1568 	}
1569 
1570 	for (i = 0; i < AN_TIMEOUT; i++) {
1571 		if (CSR_READ_2(sc, AN_EVENT_STAT) & AN_EV_CMD)
1572 			break;
1573 		DELAY(10);
1574 	}
1575 
1576 	status = CSR_READ_2(sc, AN_STATUS);
1577 
1578 	/* clear stuck command busy if necessary */
1579 	if (CSR_READ_2(sc, AN_COMMAND) & AN_CMD_BUSY)
1580 		CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CLR_STUCK_BUSY);
1581 
1582 	/* Ack the command */
1583 	CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CMD);
1584 
1585 	if (i == AN_TIMEOUT) {
1586 		if (sc->sc_if.if_flags & IFF_DEBUG)
1587 			printf("%s: command 0x%x param 0x%x timeout\n",
1588 			    device_xname(sc->sc_dev), cmd, val);
1589 		return ETIMEDOUT;
1590 	}
1591 	if (status & AN_STAT_CMD_RESULT) {
1592 		if (sc->sc_if.if_flags & IFF_DEBUG)
1593 			printf("%s: command 0x%x param 0x%x status 0x%x "
1594 			    "resp 0x%x 0x%x 0x%x\n",
1595 			    device_xname(sc->sc_dev), cmd, val, status,
1596 			    CSR_READ_2(sc, AN_RESP0), CSR_READ_2(sc, AN_RESP1),
1597 			    CSR_READ_2(sc, AN_RESP2));
1598 		return EIO;
1599 	}
1600 
1601 	return 0;
1602 }
1603 
1604 
1605 /*
1606  * Wait for firmware come up after power enabled.
1607  */
1608 static void
1609 an_wait(struct an_softc *sc)
1610 {
1611 	int i;
1612 
1613 	CSR_WRITE_2(sc, AN_COMMAND, AN_CMD_NOOP2);
1614 	for (i = 0; i < 3*hz; i++) {
1615 		if (CSR_READ_2(sc, AN_EVENT_STAT) & AN_EV_CMD)
1616 			break;
1617 		(void)tsleep(sc, PWAIT, "anatch", 1);
1618 	}
1619 	CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CMD);
1620 }
1621 
1622 static int
1623 an_seek_bap(struct an_softc *sc, int id, int off)
1624 {
1625 	int i, status;
1626 
1627 	CSR_WRITE_2(sc, AN_SEL0, id);
1628 	CSR_WRITE_2(sc, AN_OFF0, off);
1629 
1630 	for (i = 0; ; i++) {
1631 		status = CSR_READ_2(sc, AN_OFF0);
1632 		if ((status & AN_OFF_BUSY) == 0)
1633 			break;
1634 		if (i == AN_TIMEOUT) {
1635 			printf("%s: timeout in an_seek_bap to 0x%x/0x%x\n",
1636 			    device_xname(sc->sc_dev), id, off);
1637 			sc->sc_bap_off = AN_OFF_ERR;	/* invalidate */
1638 			return ETIMEDOUT;
1639 		}
1640 		DELAY(10);
1641 	}
1642 	if (status & AN_OFF_ERR) {
1643 		aprint_error_dev(sc->sc_dev, "failed in an_seek_bap to 0x%x/0x%x\n",
1644 		    id, off);
1645 		sc->sc_bap_off = AN_OFF_ERR;	/* invalidate */
1646 		return EIO;
1647 	}
1648 	sc->sc_bap_id = id;
1649 	sc->sc_bap_off = off;
1650 	return 0;
1651 }
1652 
1653 static int
1654 an_read_bap(struct an_softc *sc, int id, int off, void *buf, int buflen)
1655 {
1656 	int error, cnt;
1657 
1658 	if (buflen == 0)
1659 		return 0;
1660 	if (off == -1)
1661 		off = sc->sc_bap_off;
1662 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1663 		if ((error = an_seek_bap(sc, id, off)) != 0)
1664 			return EIO;
1665 	}
1666 
1667 	cnt = (buflen + 1) / 2;
1668 	CSR_READ_MULTI_STREAM_2(sc, AN_DATA0, (u_int16_t *)buf, cnt);
1669 	sc->sc_bap_off += cnt * 2;
1670 	return 0;
1671 }
1672 
1673 static int
1674 an_write_bap(struct an_softc *sc, int id, int off, void *buf, int buflen)
1675 {
1676 	int error, cnt;
1677 
1678 	if (buflen == 0)
1679 		return 0;
1680 	if (off == -1)
1681 		off = sc->sc_bap_off;
1682 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1683 		if ((error = an_seek_bap(sc, id, off)) != 0)
1684 			return EIO;
1685 	}
1686 
1687 	cnt = (buflen + 1) / 2;
1688 	CSR_WRITE_MULTI_STREAM_2(sc, AN_DATA0, (u_int16_t *)buf, cnt);
1689 	sc->sc_bap_off += cnt * 2;
1690 	return 0;
1691 }
1692 
1693 static int
1694 an_mwrite_bap(struct an_softc *sc, int id, int off, struct mbuf *m, int totlen)
1695 {
1696 	int error, len, cnt;
1697 
1698 	if (off == -1)
1699 		off = sc->sc_bap_off;
1700 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1701 		if ((error = an_seek_bap(sc, id, off)) != 0)
1702 			return EIO;
1703 	}
1704 
1705 	for (len = 0; m != NULL; m = m->m_next) {
1706 		if (m->m_len == 0)
1707 			continue;
1708 		len = min(m->m_len, totlen);
1709 
1710 		if ((mtod(m, u_long) & 0x1) || (len & 0x1)) {
1711 			m_copydata(m, 0, totlen, (void *)&sc->sc_buf.sc_txbuf);
1712 			cnt = (totlen + 1) / 2;
1713 			CSR_WRITE_MULTI_STREAM_2(sc, AN_DATA0,
1714 			    sc->sc_buf.sc_val, cnt);
1715 			off += cnt * 2;
1716 			break;
1717 		}
1718 		cnt = len / 2;
1719 		CSR_WRITE_MULTI_STREAM_2(sc, AN_DATA0, mtod(m, u_int16_t *),
1720 		    cnt);
1721 		off += len;
1722 		totlen -= len;
1723 	}
1724 	sc->sc_bap_off = off;
1725 	return 0;
1726 }
1727 
1728 static int
1729 an_alloc_fid(struct an_softc *sc, int len, int *idp)
1730 {
1731 	int i;
1732 
1733 	if (an_cmd(sc, AN_CMD_ALLOC_MEM, len)) {
1734 		aprint_error_dev(sc->sc_dev, "failed to allocate %d bytes on NIC\n",
1735 		    len);
1736 		return ENOMEM;
1737 	}
1738 
1739 	for (i = 0; i < AN_TIMEOUT; i++) {
1740 		if (CSR_READ_2(sc, AN_EVENT_STAT) & AN_EV_ALLOC)
1741 			break;
1742 		if (i == AN_TIMEOUT) {
1743 			printf("%s: timeout in alloc\n", device_xname(sc->sc_dev));
1744 			return ETIMEDOUT;
1745 		}
1746 		DELAY(10);
1747 	}
1748 
1749 	*idp = CSR_READ_2(sc, AN_ALLOC_FID);
1750 	CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_ALLOC);
1751 	return 0;
1752 }
1753 
1754 static int
1755 an_read_rid(struct an_softc *sc, int rid, void *buf, int *buflenp)
1756 {
1757 	int error;
1758 	u_int16_t len;
1759 
1760 	/* Tell the NIC to enter record read mode. */
1761 	error = an_cmd(sc, AN_CMD_ACCESS | AN_ACCESS_READ, rid);
1762 	if (error)
1763 		return error;
1764 
1765 	/* length in byte, including length itself */
1766 	error = an_read_bap(sc, rid, 0, &len, sizeof(len));
1767 	if (error)
1768 		return error;
1769 
1770 	len = le16toh(len) - 2;
1771 	if (*buflenp < len) {
1772 		aprint_error_dev(sc->sc_dev, "record buffer is too small, "
1773 		    "rid=%x, size=%d, len=%d\n",
1774 		    rid, *buflenp, len);
1775 		return ENOSPC;
1776 	}
1777 	*buflenp = len;
1778 	return an_read_bap(sc, rid, sizeof(len), buf, len);
1779 }
1780 
1781 static int
1782 an_write_rid(struct an_softc *sc, int rid, void *buf, int buflen)
1783 {
1784 	int error;
1785 	u_int16_t len;
1786 
1787 	/* length in byte, including length itself */
1788 	len = htole16(buflen + 2);
1789 
1790 	error = an_write_bap(sc, rid, 0, &len, sizeof(len));
1791 	if (error)
1792 		return error;
1793 	error = an_write_bap(sc, rid, sizeof(len), buf, buflen);
1794 	if (error)
1795 		return error;
1796 
1797 	return an_cmd(sc, AN_CMD_ACCESS | AN_ACCESS_WRITE, rid);
1798 }
1799 
1800 static int
1801 an_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1802 {
1803 	struct an_softc *sc = (struct an_softc *)ic->ic_ifp->if_softc;
1804 	struct ieee80211_node *ni = ic->ic_bss;
1805 	enum ieee80211_state ostate;
1806 	int buflen;
1807 
1808 	ostate = ic->ic_state;
1809 	DPRINTF(("an_newstate: %s -> %s\n", ieee80211_state_name[ostate],
1810 	    ieee80211_state_name[nstate]));
1811 
1812 	switch (nstate) {
1813 	case IEEE80211_S_INIT:
1814 		ic->ic_flags &= ~IEEE80211_F_IBSSON;
1815 		return (*sc->sc_newstate)(ic, nstate, arg);
1816 
1817 	case IEEE80211_S_SCAN:
1818 	case IEEE80211_S_AUTH:
1819 	case IEEE80211_S_ASSOC:
1820 		ic->ic_state = nstate; /* NB: skip normal ieee80211 handling */
1821 		return 0;
1822 
1823 	case IEEE80211_S_RUN:
1824 		buflen = sizeof(sc->sc_buf);
1825 		an_read_rid(sc, AN_RID_STATUS, &sc->sc_buf, &buflen);
1826 		IEEE80211_ADDR_COPY(ni->ni_bssid,
1827 		    sc->sc_buf.sc_status.an_cur_bssid);
1828 		IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
1829 		ni->ni_chan = &ic->ic_channels[
1830 		    le16toh(sc->sc_buf.sc_status.an_cur_channel)];
1831 		ni->ni_esslen = le16toh(sc->sc_buf.sc_status.an_ssidlen);
1832 		if (ni->ni_esslen > IEEE80211_NWID_LEN)
1833 			ni->ni_esslen = IEEE80211_NWID_LEN;	/*XXX*/
1834 		memcpy(ni->ni_essid, sc->sc_buf.sc_status.an_ssid,
1835 		    ni->ni_esslen);
1836 		ni->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B];	/*XXX*/
1837 		if (ic->ic_ifp->if_flags & IFF_DEBUG) {
1838 			printf("%s: ", device_xname(sc->sc_dev));
1839 			if (ic->ic_opmode == IEEE80211_M_STA)
1840 				printf("associated ");
1841 			else
1842 				printf("synchronized ");
1843 			printf("with %s ssid ", ether_sprintf(ni->ni_bssid));
1844 			ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
1845 			printf(" channel %u start %uMb\n",
1846 			    le16toh(sc->sc_buf.sc_status.an_cur_channel),
1847 			    le16toh(sc->sc_buf.sc_status.an_current_tx_rate)/2);
1848 		}
1849 		break;
1850 
1851 	default:
1852 		break;
1853 	}
1854 	return (*sc->sc_newstate)(ic, nstate, arg);
1855 }
1856