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