1 /*	$OpenBSD: awi.c,v 1.13 2004/05/12 06:35:10 tedu Exp $	*/
2 /*	$NetBSD: awi.c,v 1.26 2000/07/21 04:48:55 onoe Exp $	*/
3 
4 /*-
5  * Copyright (c) 1999 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Bill Sommerfeld
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 /*
40  * Driver for AMD 802.11 firmware.
41  * Uses am79c930 chip driver to talk to firmware running on the am79c930.
42  *
43  * More-or-less a generic ethernet-like if driver, with 802.11 gorp added.
44  */
45 
46 /*
47  * todo:
48  *	- flush tx queue on resynch.
49  *	- clear oactive on "down".
50  *	- rewrite copy-into-mbuf code
51  *	- mgmt state machine gets stuck retransmitting assoc requests.
52  *	- multicast filter.
53  *	- fix device reset so it's more likely to work
54  *	- show status goo through ifmedia.
55  *
56  * more todo:
57  *	- deal with more 802.11 frames.
58  *		- send reassoc request
59  *		- deal with reassoc response
60  *		- send/deal with disassociation
61  *	- deal with "full" access points (no room for me).
62  *	- power save mode
63  *
64  * later:
65  *	- SSID preferences
66  *	- need ioctls for poking at the MIBs
67  *	- implement ad-hoc mode (including bss creation).
68  *	- decide when to do "ad hoc" vs. infrastructure mode (IFF_LINK flags?)
69  *		(focus on inf. mode since that will be needed for ietf)
70  *	- deal with DH vs. FH versions of the card
71  *	- deal with faster cards (2mb/s)
72  *	- ?WEP goo (mmm, rc4) (it looks not particularly useful).
73  *	- ifmedia revision.
74  *	- common 802.11 mibish things.
75  *	- common 802.11 media layer.
76  */
77 
78 /*
79  * Driver for AMD 802.11 PCnetMobile firmware.
80  * Uses am79c930 chip driver to talk to firmware running on the am79c930.
81  *
82  * The initial version of the driver was written by
83  * Bill Sommerfeld <sommerfeld@netbsd.org>.
84  * Then the driver module completely rewritten to support cards with DS phy
85  * and to support adhoc mode by Atsushi Onoe <onoe@netbsd.org>
86  */
87 
88 #ifndef __OpenBSD__
89 #include "opt_inet.h"
90 #endif
91 #if defined(__FreeBSD__) && __FreeBSD__ >= 4
92 #define	NBPFILTER	1
93 #elif defined(__FreeBSD__) && __FreeBSD__ >= 3
94 #include "bpf.h"
95 #define	NBPFILTER	NBPF
96 #else
97 #include "bpfilter.h"
98 #endif
99 
100 #include <sys/param.h>
101 #include <sys/systm.h>
102 #include <sys/kernel.h>
103 #include <sys/mbuf.h>
104 #include <sys/malloc.h>
105 #include <sys/proc.h>
106 #include <sys/socket.h>
107 #include <sys/sockio.h>
108 #include <sys/errno.h>
109 #include <sys/syslog.h>
110 #if defined(__FreeBSD__) && __FreeBSD__ >= 4
111 #include <sys/bus.h>
112 #else
113 #include <sys/device.h>
114 #endif
115 
116 #include <net/if.h>
117 #include <net/if_dl.h>
118 #ifndef __OpenBSD__
119 #ifdef __FreeBSD__
120 #include <net/ethernet.h>
121 #else
122 #include <net/if_ether.h>
123 #endif
124 #endif
125 #include <net/if_media.h>
126 #include <net/if_llc.h>
127 
128 #ifdef INET
129 #include <netinet/in.h>
130 #include <netinet/in_systm.h>
131 #include <netinet/in_var.h>
132 #include <netinet/ip.h>
133 #ifdef __NetBSD__
134 #include <netinet/if_inarp.h>
135 #else
136 #include <netinet/if_ether.h>
137 #endif
138 #endif
139 
140 #include <net/if_ieee80211.h>
141 
142 #if NBPFILTER > 0
143 #include <net/bpf.h>
144 #endif
145 
146 #include <machine/cpu.h>
147 #include <machine/bus.h>
148 #ifdef __NetBSD__
149 #include <machine/intr.h>
150 #endif
151 #ifdef __FreeBSD__
152 #include <machine/clock.h>
153 #endif
154 
155 #if defined(__NetBSD__) || defined(__OpenBSD__)
156 #include <dev/ic/am79c930reg.h>
157 #include <dev/ic/am79c930var.h>
158 #include <dev/ic/awireg.h>
159 #include <dev/ic/awivar.h>
160 #endif
161 #ifdef __FreeBSD__
162 #include <dev/awi/am79c930reg.h>
163 #include <dev/awi/am79c930var.h>
164 #include <dev/awi/awireg.h>
165 #include <dev/awi/awivar.h>
166 #endif
167 
168 static int awi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
169 #ifdef IFM_IEEE80211
170 static int awi_media_rate2opt(struct awi_softc *sc, int rate);
171 static int awi_media_opt2rate(struct awi_softc *sc, int opt);
172 static int awi_media_change(struct ifnet *ifp);
173 static void awi_media_status(struct ifnet *ifp, struct ifmediareq *imr);
174 #endif
175 static void awi_watchdog(struct ifnet *ifp);
176 static void awi_start(struct ifnet *ifp);
177 static void awi_txint(struct awi_softc *sc);
178 static struct mbuf * awi_fix_txhdr(struct awi_softc *sc, struct mbuf *m0);
179 static struct mbuf * awi_fix_rxhdr(struct awi_softc *sc, struct mbuf *m0);
180 static void awi_input(struct awi_softc *sc, struct mbuf *m, u_int32_t rxts, u_int8_t rssi);
181 static void awi_rxint(struct awi_softc *sc);
182 static struct mbuf * awi_devget(struct awi_softc *sc, u_int32_t off, u_int16_t len);
183 static int awi_init_hw(struct awi_softc *sc);
184 static int awi_init_mibs(struct awi_softc *sc);
185 static int awi_init_txrx(struct awi_softc *sc);
186 static void awi_stop_txrx(struct awi_softc *sc);
187 static int awi_start_scan(struct awi_softc *sc);
188 static int awi_next_scan(struct awi_softc *sc);
189 static void awi_stop_scan(struct awi_softc *sc);
190 static void awi_recv_beacon(struct awi_softc *sc, struct mbuf *m0, u_int32_t rxts, u_int8_t rssi);
191 static int awi_set_ss(struct awi_softc *sc);
192 static void awi_try_sync(struct awi_softc *sc);
193 static void awi_sync_done(struct awi_softc *sc);
194 static void awi_send_deauth(struct awi_softc *sc);
195 static void awi_send_auth(struct awi_softc *sc, int seq);
196 static void awi_recv_auth(struct awi_softc *sc, struct mbuf *m0);
197 static void awi_send_asreq(struct awi_softc *sc, int reassoc);
198 static void awi_recv_asresp(struct awi_softc *sc, struct mbuf *m0);
199 static int awi_mib(struct awi_softc *sc, u_int8_t cmd, u_int8_t mib);
200 static int awi_cmd_scan(struct awi_softc *sc);
201 static int awi_cmd(struct awi_softc *sc, u_int8_t cmd);
202 static void awi_cmd_done(struct awi_softc *sc);
203 static int awi_next_txd(struct awi_softc *sc, int len, u_int32_t *framep, u_int32_t*ntxdp);
204 static int awi_lock(struct awi_softc *sc);
205 static void awi_unlock(struct awi_softc *sc);
206 static int awi_intr_lock(struct awi_softc *sc);
207 static void awi_intr_unlock(struct awi_softc *sc);
208 static int awi_cmd_wait(struct awi_softc *sc);
209 static void awi_print_essid(u_int8_t *essid);
210 
211 #ifdef AWI_DEBUG
212 static void awi_dump_pkt(struct awi_softc *sc, struct mbuf *m, int rssi);
213 int awi_verbose = 0;
214 int awi_dump = 0;
215 #define	AWI_DUMP_MASK(fc0)  (1 << (((fc0) & IEEE80211_FC0_SUBTYPE_MASK) >> 4))
216 int awi_dump_mask = AWI_DUMP_MASK(IEEE80211_FC0_SUBTYPE_BEACON);
217 int awi_dump_hdr = 0;
218 int awi_dump_len = 28;
219 #endif
220 
221 #if NBPFILTER > 0
222 #define	AWI_BPF_NORM	0
223 #define	AWI_BPF_RAW	1
224 #ifdef __FreeBSD__
225 #define	AWI_BPF_MTAP(sc, m, raw) do {					\
226 	if ((sc)->sc_ifp->if_bpf && (sc)->sc_rawbpf == (raw))		\
227 		bpf_mtap((sc)->sc_ifp, (m));				\
228 } while (0);
229 #else
230 #define	AWI_BPF_MTAP(sc, m, raw) do {					\
231 	if ((sc)->sc_ifp->if_bpf && (sc)->sc_rawbpf == (raw))		\
232 		bpf_mtap((sc)->sc_ifp->if_bpf, (m));			\
233 } while (0);
234 #endif
235 #else
236 #define	AWI_BPF_MTAP(sc, m, raw)
237 #endif
238 
239 #ifndef llc_snap
240 #define llc_snap              llc_un.type_snap
241 #endif
242 
243 #ifdef __OpenBSD__
244 struct cfdriver awi_cd = {
245 	NULL, "awi", DV_IFNET
246 };
247 #endif
248 
249 #ifdef __FreeBSD__
250 #if __FreeBSD__ >= 4
251 devclass_t awi_devclass;
252 #endif
253 
254 /* NetBSD compatible functions  */
255 static char * ether_sprintf(u_int8_t *);
256 
257 static char *
ether_sprintf(enaddr)258 ether_sprintf(enaddr)
259 	u_int8_t *enaddr;
260 {
261 	static char strbuf[18];
262 
263 	snprintf(strbuf, sizeof strbuf, "%6D", enaddr, ":");
264 	return strbuf;
265 }
266 #endif
267 
268 int
awi_attach(sc)269 awi_attach(sc)
270 	struct awi_softc *sc;
271 {
272 	struct ifnet *ifp = sc->sc_ifp;
273 	int s;
274 	int error;
275 #ifdef IFM_IEEE80211
276 	int i;
277 	u_int8_t *phy_rates;
278 	int mword;
279 	struct ifmediareq imr;
280 #endif
281 
282 	s = splnet();
283 	/*
284 	 * Even if we can sleep in initialization state,
285 	 * all other processes (e.g. ifconfig) have to wait for
286 	 * completion of attaching interface.
287 	 */
288 	sc->sc_busy = 1;
289 	sc->sc_status = AWI_ST_INIT;
290 	TAILQ_INIT(&sc->sc_scan);
291 	error = awi_init_hw(sc);
292 	if (error) {
293 		sc->sc_invalid = 1;
294 		splx(s);
295 		return error;
296 	}
297 	error = awi_init_mibs(sc);
298 	splx(s);
299 	if (error) {
300 		sc->sc_invalid = 1;
301 		return error;
302 	}
303 
304 	ifp->if_softc = sc;
305 	ifp->if_start = awi_start;
306 	ifp->if_ioctl = awi_ioctl;
307 	ifp->if_watchdog = awi_watchdog;
308 	ifp->if_mtu = ETHERMTU;
309 	ifp->if_hdrlen = sizeof(struct ieee80211_frame) +
310 	    sizeof(struct ether_header);
311 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
312 #ifdef IFF_NOTRAILERS
313 	ifp->if_flags |= IFF_NOTRAILERS;
314 #endif
315 #if defined(__NetBSD__) || defined(__OpenBSD__)
316 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
317 #endif
318 #ifdef __FreeBSD__
319 	ifp->if_output = ether_output;
320 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
321 	memcpy(sc->sc_ec.ac_enaddr, sc->sc_mib_addr.aMAC_Address,
322 	    ETHER_ADDR_LEN);
323 #endif
324 	IFQ_SET_READY(&ifp->if_snd);
325 
326 	printf("%s: IEEE802.11 %s %dMbps (firmware %s)\n",
327 	    sc->sc_dev.dv_xname,
328 	    sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH ? "FH" : "DS",
329 	    sc->sc_tx_rate / 10, sc->sc_banner);
330 	printf("%s: address %s\n",
331 	    sc->sc_dev.dv_xname,  ether_sprintf(sc->sc_mib_addr.aMAC_Address));
332 	if_attach(ifp);
333 #ifdef __OpenBSD__
334 	ether_ifattach(ifp);
335 #elif defined(__FreeBSD__)
336 	ether_ifattach(ifp);
337 #if NBPFILTER > 0
338 	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
339 #endif
340 #elif defined(__NetBSD__)
341 	ether_ifattach(ifp, sc->sc_mib_addr.aMAC_Address);
342 #endif
343 
344 #ifdef IFM_IEEE80211
345 	ifmedia_init(&sc->sc_media, 0, awi_media_change, awi_media_status);
346 	phy_rates = sc->sc_mib_phy.aSuprt_Data_Rates;
347 	for (i = 0; i < phy_rates[1]; i++) {
348 		mword = awi_media_rate2opt(sc, AWI_80211_RATE(phy_rates[2 + i]));
349 		if (mword == 0)
350 			continue;
351 		mword |= IFM_IEEE80211;
352 		ifmedia_add(&sc->sc_media, mword, 0, NULL);
353 		ifmedia_add(&sc->sc_media,
354 		    mword | IFM_IEEE80211_ADHOC, 0, NULL);
355 		if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_FH)
356 			ifmedia_add(&sc->sc_media,
357 			    mword | IFM_IEEE80211_ADHOC | IFM_FLAG0, 0, NULL);
358 	}
359 	awi_media_status(ifp, &imr);
360 	ifmedia_set(&sc->sc_media, imr.ifm_active);
361 #endif
362 
363 	/* ready to accept ioctl */
364 	awi_unlock(sc);
365 
366 	/* Attach is successful. */
367 	sc->sc_attached = 1;
368 	return 0;
369 }
370 
371 #ifndef __FreeBSD__
372 int
awi_detach(sc)373 awi_detach(sc)
374 	struct awi_softc *sc;
375 {
376 	struct ifnet *ifp = sc->sc_ifp;
377 	int s;
378 
379 	/* Succeed if there is no work to do. */
380 	if (!sc->sc_attached)
381 		return (0);
382 
383 	s = splnet();
384 	sc->sc_invalid = 1;
385 	awi_stop(sc);
386 	while (sc->sc_sleep_cnt > 0) {
387 		wakeup(sc);
388 		(void)tsleep(sc, PWAIT, "awidet", 1);
389 	}
390 	if (sc->sc_wep_ctx != NULL)
391 		free(sc->sc_wep_ctx, M_DEVBUF);
392 #if NBPFILTER > 0
393 	bpfdetach(ifp);
394 #endif
395 #ifdef IFM_IEEE80211
396 	ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY);
397 #endif
398 	ether_ifdetach(ifp);
399 	if_detach(ifp);
400 	if (sc->sc_enabled) {
401 		if (sc->sc_disable)
402 			(*sc->sc_disable)(sc);
403 		sc->sc_enabled = 0;
404 	}
405 	splx(s);
406 	return 0;
407 }
408 
409 int
awi_activate(self,act)410 awi_activate(self, act)
411 	struct device *self;
412 	enum devact act;
413 {
414 	struct awi_softc *sc = (struct awi_softc *)self;
415 	int s, error = 0;
416 
417 	s = splnet();
418 	switch (act) {
419 	case DVACT_ACTIVATE:
420 		error = EOPNOTSUPP;
421 		break;
422 
423 	case DVACT_DEACTIVATE:
424 		sc->sc_invalid = 1;
425 #ifdef __NetBSD__
426 		if (sc->sc_ifp)
427 			if_deactivate(sc->sc_ifp);
428 #endif
429 		break;
430 	}
431 	splx(s);
432 
433 	return error;
434 }
435 
436 void
awi_power(sc,why)437 awi_power(sc, why)
438 	struct awi_softc *sc;
439 	int why;
440 {
441 	int s;
442 	int ocansleep;
443 
444 	if (!sc->sc_enabled)
445 		return;
446 
447 	s = splnet();
448 	ocansleep = sc->sc_cansleep;
449 	sc->sc_cansleep = 0;
450 #ifdef needtobefixed	/*ONOE*/
451 	if (why == PWR_RESUME) {
452 		sc->sc_enabled = 0;
453 		awi_init(sc);
454 		(void)awi_intr(sc);
455 	} else {
456 		awi_stop(sc);
457 		if (sc->sc_disable)
458 			(*sc->sc_disable)(sc);
459 	}
460 #endif
461 	sc->sc_cansleep = ocansleep;
462 	splx(s);
463 }
464 #endif /* __NetBSD__ */
465 
466 static int
awi_ioctl(ifp,cmd,data)467 awi_ioctl(ifp, cmd, data)
468 	struct ifnet *ifp;
469 	u_long cmd;
470 	caddr_t data;
471 {
472 	struct awi_softc *sc = ifp->if_softc;
473 	struct ifreq *ifr = (struct ifreq *)data;
474 	struct ifaddr *ifa = (struct ifaddr *)data;
475 	int s, error;
476 	struct ieee80211_nwid nwid;
477 	u_int8_t *p;
478 
479 	s = splnet();
480 
481 #ifdef __OpenBSD__
482 	if ((error = ether_ioctl(ifp, &sc->sc_arpcom, cmd, data)) > 0) {
483 		splx(s);
484 		return (error);
485 	}
486 #endif
487 
488 	/* serialize ioctl */
489 	error = awi_lock(sc);
490 	if (error)
491 		goto cantlock;
492 	switch (cmd) {
493 	case SIOCSIFADDR:
494 		ifp->if_flags |= IFF_UP;
495 		switch (ifa->ifa_addr->sa_family) {
496 #ifdef INET
497 		case AF_INET:
498 			arp_ifinit((void *)ifp, ifa);
499 			break;
500 #endif
501 		}
502 		/* FALLTHROUGH */
503 	case SIOCSIFFLAGS:
504 		sc->sc_format_llc = !(ifp->if_flags & IFF_LINK0);
505 		if (!(ifp->if_flags & IFF_UP)) {
506 			if (sc->sc_enabled) {
507 				awi_stop(sc);
508 				if (sc->sc_disable)
509 					(*sc->sc_disable)(sc);
510 				sc->sc_enabled = 0;
511 			}
512 			break;
513 		}
514 		error = awi_init(sc);
515 		break;
516 
517 	case SIOCADDMULTI:
518 	case SIOCDELMULTI:
519 #ifdef __FreeBSD__
520 		error = ENETRESET;	/*XXX*/
521 #else
522 		error = (cmd == SIOCADDMULTI) ?
523 		    ether_addmulti(ifr, &sc->sc_arpcom) :
524 		    ether_delmulti(ifr, &sc->sc_arpcom);
525 #endif
526 		/*
527 		 * Do not rescan BSS.  Rather, just reset multicast filter.
528 		 */
529 		if (error == ENETRESET) {
530 			if (sc->sc_enabled)
531 				error = awi_init(sc);
532 			else
533 				error = 0;
534 		}
535 		break;
536 	case SIOCSIFMTU:
537 		if (ifr->ifr_mtu > ETHERMTU)
538 			error = EINVAL;
539 		else
540 			ifp->if_mtu = ifr->ifr_mtu;
541 		break;
542 	case SIOCS80211NWID:
543 		error = copyin(ifr->ifr_data, &nwid, sizeof(nwid));
544 		if (error)
545 			break;
546 		if (nwid.i_len > IEEE80211_NWID_LEN) {
547 			error = EINVAL;
548 			break;
549 		}
550 		if (sc->sc_mib_mac.aDesired_ESS_ID[1] == nwid.i_len &&
551 		    memcmp(&sc->sc_mib_mac.aDesired_ESS_ID[2], nwid.i_nwid,
552 		    nwid.i_len) == 0)
553 			break;
554 		memset(sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
555 		sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
556 		sc->sc_mib_mac.aDesired_ESS_ID[1] = nwid.i_len;
557 		memcpy(&sc->sc_mib_mac.aDesired_ESS_ID[2], nwid.i_nwid,
558 		    nwid.i_len);
559 		if (sc->sc_enabled) {
560 			awi_stop(sc);
561 			error = awi_init(sc);
562 		}
563 		break;
564 	case SIOCG80211NWID:
565 		if (ifp->if_flags & IFF_RUNNING)
566 			p = sc->sc_bss.essid;
567 		else
568 			p = sc->sc_mib_mac.aDesired_ESS_ID;
569 		error = copyout(p + 1, ifr->ifr_data, 1 + IEEE80211_NWID_LEN);
570 		break;
571 	case SIOCS80211NWKEY:
572 		error = awi_wep_setnwkey(sc, (struct ieee80211_nwkey *)data);
573 		break;
574 	case SIOCG80211NWKEY:
575 		error = awi_wep_getnwkey(sc, (struct ieee80211_nwkey *)data);
576 		break;
577 #ifdef IFM_IEEE80211
578 	case SIOCSIFMEDIA:
579 	case SIOCGIFMEDIA:
580 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
581 		break;
582 #endif
583 	default:
584 #ifdef notyet
585 		error = awi_wicfg(ifp, cmd, data);
586 #else
587 		error = EINVAL;
588 #endif
589 		break;
590 	}
591 	awi_unlock(sc);
592   cantlock:
593 	splx(s);
594 	return error;
595 }
596 
597 #ifdef IFM_IEEE80211
598 static int
awi_media_rate2opt(sc,rate)599 awi_media_rate2opt(sc, rate)
600 	struct awi_softc *sc;
601 	int rate;
602 {
603 	int mword;
604 
605 	mword = 0;
606 	switch (rate) {
607 	case 10:
608 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
609 			mword = IFM_IEEE80211_FH1;
610 		else
611 			mword = IFM_IEEE80211_DS1;
612 		break;
613 	case 20:
614 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
615 			mword = IFM_IEEE80211_FH2;
616 		else
617 			mword = IFM_IEEE80211_DS2;
618 		break;
619 	case 55:
620 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_DS)
621 			mword = IFM_IEEE80211_DS5;
622 		break;
623 	case 110:
624 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_DS)
625 			mword = IFM_IEEE80211_DS11;
626 		break;
627 	}
628 	return mword;
629 }
630 
631 static int
awi_media_opt2rate(sc,opt)632 awi_media_opt2rate(sc, opt)
633 	struct awi_softc *sc;
634 	int opt;
635 {
636 	int rate;
637 
638 	rate = 0;
639 	switch (IFM_SUBTYPE(opt)) {
640 	case IFM_IEEE80211_FH1:
641 	case IFM_IEEE80211_FH2:
642 		if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_FH)
643 			return 0;
644 		break;
645 	case IFM_IEEE80211_DS1:
646 	case IFM_IEEE80211_DS2:
647 	case IFM_IEEE80211_DS5:
648 	case IFM_IEEE80211_DS11:
649 		if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_DS)
650 			return 0;
651 		break;
652 	}
653 
654 	switch (IFM_SUBTYPE(opt)) {
655 	case IFM_IEEE80211_FH1:
656 	case IFM_IEEE80211_DS1:
657 		rate = 10;
658 		break;
659 	case IFM_IEEE80211_FH2:
660 	case IFM_IEEE80211_DS2:
661 		rate = 20;
662 		break;
663 	case IFM_IEEE80211_DS5:
664 		rate = 55;
665 		break;
666 	case IFM_IEEE80211_DS11:
667 		rate = 110;
668 		break;
669 	}
670 	return rate;
671 }
672 
673 /*
674  * Called from ifmedia_ioctl via awi_ioctl with lock obtained.
675  */
676 static int
awi_media_change(ifp)677 awi_media_change(ifp)
678 	struct ifnet *ifp;
679 {
680 	struct awi_softc *sc = ifp->if_softc;
681 	struct ifmedia_entry *ime;
682 	u_int8_t *phy_rates;
683 	int i, rate, error;
684 
685 	error = 0;
686 	ime = sc->sc_media.ifm_cur;
687 	rate = awi_media_opt2rate(sc, ime->ifm_media);
688 	if (rate == 0)
689 		return EINVAL;
690 	if (rate != sc->sc_tx_rate) {
691 		phy_rates = sc->sc_mib_phy.aSuprt_Data_Rates;
692 		for (i = 0; i < phy_rates[1]; i++) {
693 			if (rate == AWI_80211_RATE(phy_rates[2 + i]))
694 				break;
695 		}
696 		if (i == phy_rates[1])
697 			return EINVAL;
698 	}
699 	if (ime->ifm_media & IFM_IEEE80211_ADHOC) {
700 		sc->sc_mib_local.Network_Mode = 0;
701 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
702 			sc->sc_no_bssid = 0;
703 		else
704 			sc->sc_no_bssid = (ime->ifm_media & IFM_FLAG0) ? 1 : 0;
705 	} else {
706 		sc->sc_mib_local.Network_Mode = 1;
707 	}
708 	if (sc->sc_enabled) {
709 		awi_stop(sc);
710 		error = awi_init(sc);
711 	}
712 	return error;
713 }
714 
715 static void
awi_media_status(ifp,imr)716 awi_media_status(ifp, imr)
717 	struct ifnet *ifp;
718 	struct ifmediareq *imr;
719 {
720 	struct awi_softc *sc = ifp->if_softc;
721 
722 	imr->ifm_status = IFM_AVALID;
723 	if (ifp->if_flags & IFF_RUNNING)
724 		imr->ifm_status |= IFM_ACTIVE;
725 	imr->ifm_active = IFM_IEEE80211;
726 	imr->ifm_active |= awi_media_rate2opt(sc, sc->sc_tx_rate);
727 	if (sc->sc_mib_local.Network_Mode == 0) {
728 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
729 		if (sc->sc_no_bssid)
730 			imr->ifm_active |= IFM_FLAG0;
731 	}
732 }
733 #endif /* IFM_IEEE80211 */
734 
735 int
awi_intr(arg)736 awi_intr(arg)
737 	void *arg;
738 {
739 	struct awi_softc *sc = arg;
740 	u_int16_t status;
741 	int error, handled = 0, ocansleep;
742 
743 	if (!sc->sc_enabled || !sc->sc_enab_intr || sc->sc_invalid)
744 		return 0;
745 
746 	am79c930_gcr_setbits(&sc->sc_chip,
747 	    AM79C930_GCR_DISPWDN | AM79C930_GCR_ECINT);
748 	awi_write_1(sc, AWI_DIS_PWRDN, 1);
749 	ocansleep = sc->sc_cansleep;
750 	sc->sc_cansleep = 0;
751 
752 	for (;;) {
753 		error = awi_intr_lock(sc);
754 		if (error)
755 			break;
756 		status = awi_read_1(sc, AWI_INTSTAT);
757 		awi_write_1(sc, AWI_INTSTAT, 0);
758 		awi_write_1(sc, AWI_INTSTAT, 0);
759 		status |= awi_read_1(sc, AWI_INTSTAT2) << 8;
760 		awi_write_1(sc, AWI_INTSTAT2, 0);
761 		DELAY(10);
762 		awi_intr_unlock(sc);
763 		if (!sc->sc_cmd_inprog)
764 			status &= ~AWI_INT_CMD;	/* make sure */
765 		if (status == 0)
766 			break;
767 		handled = 1;
768 		if (status & AWI_INT_RX)
769 			awi_rxint(sc);
770 		if (status & AWI_INT_TX)
771 			awi_txint(sc);
772 		if (status & AWI_INT_CMD)
773 			awi_cmd_done(sc);
774 		if (status & AWI_INT_SCAN_CMPLT) {
775 			if (sc->sc_status == AWI_ST_SCAN &&
776 			    sc->sc_mgt_timer > 0)
777 				(void)awi_next_scan(sc);
778 		}
779 	}
780 	sc->sc_cansleep = ocansleep;
781 	am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_DISPWDN);
782 	awi_write_1(sc, AWI_DIS_PWRDN, 0);
783 	return handled;
784 }
785 
786 int
awi_init(sc)787 awi_init(sc)
788 	struct awi_softc *sc;
789 {
790 	int error, ostatus;
791 	int n;
792 	struct ifnet *ifp = sc->sc_ifp;
793 #ifdef __FreeBSD__
794 	struct ifmultiaddr *ifma;
795 #else
796 	struct ether_multi *enm;
797 	struct ether_multistep step;
798 #endif
799 
800 	/* reinitialize muticast filter */
801 	n = 0;
802 	ifp->if_flags |= IFF_ALLMULTI;
803 	sc->sc_mib_local.Accept_All_Multicast_Dis = 0;
804 	if (ifp->if_flags & IFF_PROMISC) {
805 		sc->sc_mib_mac.aPromiscuous_Enable = 1;
806 		goto set_mib;
807 	}
808 	sc->sc_mib_mac.aPromiscuous_Enable = 0;
809 #ifdef __FreeBSD__
810 	if (ifp->if_amcount != 0)
811 		goto set_mib;
812 	for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma != NULL;
813 	    ifma = LIST_NEXT(ifma, ifma_link)) {
814 		if (ifma->ifma_addr->sa_family != AF_LINK)
815 			continue;
816 		if (n == AWI_GROUP_ADDR_SIZE)
817 			goto set_mib;
818 		memcpy(sc->sc_mib_addr.aGroup_Addresses[n],
819 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
820 		    ETHER_ADDR_LEN);
821 		n++;
822 	}
823 #else
824 	ETHER_FIRST_MULTI(step, &sc->sc_arpcom, enm);
825 	while (enm != NULL) {
826 		if (n == AWI_GROUP_ADDR_SIZE ||
827 		    memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)
828 		    != 0)
829 			goto set_mib;
830 		memcpy(sc->sc_mib_addr.aGroup_Addresses[n], enm->enm_addrlo,
831 		    ETHER_ADDR_LEN);
832 		n++;
833 		ETHER_NEXT_MULTI(step, enm);
834 	}
835 #endif
836 	for (; n < AWI_GROUP_ADDR_SIZE; n++)
837 		memset(sc->sc_mib_addr.aGroup_Addresses[n], 0, ETHER_ADDR_LEN);
838 	ifp->if_flags &= ~IFF_ALLMULTI;
839 	sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
840 
841   set_mib:
842 #ifdef notdef	/* allow non-encrypted frame for receiving. */
843 	sc->sc_mib_mgt.Wep_Required = sc->sc_wep_algo != NULL ? 1 : 0;
844 #endif
845 	if (!sc->sc_enabled) {
846 		sc->sc_enabled = 1;
847 		if (sc->sc_enable)
848 			(*sc->sc_enable)(sc);
849 		sc->sc_status = AWI_ST_INIT;
850 		error = awi_init_hw(sc);
851 		if (error)
852 			return error;
853 	}
854 	ostatus = sc->sc_status;
855 	sc->sc_status = AWI_ST_INIT;
856 	if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_LOCAL)) != 0 ||
857 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_ADDR)) != 0 ||
858 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MAC)) != 0 ||
859 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT)) != 0 ||
860 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_PHY)) != 0) {
861 		awi_stop(sc);
862 		return error;
863 	}
864 	if (ifp->if_flags & IFF_RUNNING)
865 		sc->sc_status = AWI_ST_RUNNING;
866 	else {
867 		if (ostatus == AWI_ST_INIT) {
868 			error = awi_init_txrx(sc);
869 			if (error)
870 				return error;
871 		}
872 		error = awi_start_scan(sc);
873 	}
874 	return error;
875 }
876 
877 void
awi_stop(sc)878 awi_stop(sc)
879 	struct awi_softc *sc;
880 {
881 	struct ifnet *ifp = sc->sc_ifp;
882 	struct awi_bss *bp;
883 	struct mbuf *m;
884 
885 	sc->sc_status = AWI_ST_INIT;
886 	if (!sc->sc_invalid) {
887 		(void)awi_cmd_wait(sc);
888 		if (sc->sc_mib_local.Network_Mode &&
889 		    sc->sc_status > AWI_ST_AUTH)
890 			awi_send_deauth(sc);
891 		awi_stop_txrx(sc);
892 	}
893 	ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
894 	ifp->if_timer = 0;
895 	sc->sc_tx_timer = sc->sc_rx_timer = sc->sc_mgt_timer = 0;
896 	for (;;) {
897 		IF_DEQUEUE(&sc->sc_mgtq, m);
898 		if (m == NULL)
899 			break;
900 		m_freem(m);
901 	}
902 	IFQ_PURGE(&ifp->if_snd);
903 	while ((bp = TAILQ_FIRST(&sc->sc_scan)) != NULL) {
904 		TAILQ_REMOVE(&sc->sc_scan, bp, list);
905 		free(bp, M_DEVBUF);
906 	}
907 }
908 
909 static void
awi_watchdog(ifp)910 awi_watchdog(ifp)
911 	struct ifnet *ifp;
912 {
913 	struct awi_softc *sc = ifp->if_softc;
914 	int ocansleep;
915 
916 	if (sc->sc_invalid) {
917 		ifp->if_timer = 0;
918 		return;
919 	}
920 
921 	ocansleep = sc->sc_cansleep;
922 	sc->sc_cansleep = 0;
923 	if (sc->sc_tx_timer && --sc->sc_tx_timer == 0) {
924 		printf("%s: transmit timeout\n", sc->sc_dev.dv_xname);
925 		awi_txint(sc);
926 	}
927 	if (sc->sc_rx_timer && --sc->sc_rx_timer == 0) {
928 		if (ifp->if_flags & IFF_DEBUG) {
929 			printf("%s: no recent beacons from %s; rescanning\n",
930 			    sc->sc_dev.dv_xname,
931 			    ether_sprintf(sc->sc_bss.bssid));
932 		}
933 		ifp->if_flags &= ~IFF_RUNNING;
934 		awi_start_scan(sc);
935 	}
936 	if (sc->sc_mgt_timer && --sc->sc_mgt_timer == 0) {
937 		switch (sc->sc_status) {
938 		case AWI_ST_SCAN:
939 			awi_stop_scan(sc);
940 			break;
941 		case AWI_ST_AUTH:
942 		case AWI_ST_ASSOC:
943 			/* restart scan */
944 			awi_start_scan(sc);
945 			break;
946 		default:
947 			break;
948 		}
949 	}
950 
951 	if (sc->sc_tx_timer == 0 && sc->sc_rx_timer == 0 &&
952 	    sc->sc_mgt_timer == 0)
953 		ifp->if_timer = 0;
954 	else
955 		ifp->if_timer = 1;
956 	sc->sc_cansleep = ocansleep;
957 }
958 
959 static void
awi_start(ifp)960 awi_start(ifp)
961 	struct ifnet *ifp;
962 {
963 	struct awi_softc *sc = ifp->if_softc;
964 	struct mbuf *m0, *m;
965 	u_int32_t txd, frame, ntxd;
966 	u_int8_t rate;
967 	int len, sent = 0;
968 
969 	for (;;) {
970 		txd = sc->sc_txnext;
971 		IF_DEQUEUE(&sc->sc_mgtq, m0);
972 		if (m0 != NULL) {
973 			if (awi_next_txd(sc, m0->m_pkthdr.len, &frame, &ntxd)) {
974 				IF_PREPEND(&sc->sc_mgtq, m0);
975 				ifp->if_flags |= IFF_OACTIVE;
976 				break;
977 			}
978 		} else {
979 			if (!(ifp->if_flags & IFF_RUNNING))
980 				break;
981 			IFQ_POLL(&ifp->if_snd, m0);
982 			if (m0 == NULL)
983 				break;
984 			len = m0->m_pkthdr.len + sizeof(struct ieee80211_frame);
985 			if (sc->sc_format_llc)
986 				len += sizeof(struct llc) -
987 				    sizeof(struct ether_header);
988 			if (sc->sc_wep_algo != NULL)
989 				len += IEEE80211_WEP_IVLEN +
990 				    IEEE80211_WEP_KIDLEN + IEEE80211_WEP_CRCLEN;
991 			if (awi_next_txd(sc, len, &frame, &ntxd)) {
992 				ifp->if_flags |= IFF_OACTIVE;
993 				break;
994 			}
995 			IFQ_DEQUEUE(&ifp->if_snd, m0);
996 			AWI_BPF_MTAP(sc, m0, AWI_BPF_NORM);
997 			m0 = awi_fix_txhdr(sc, m0);
998 			if (sc->sc_wep_algo != NULL && m0 != NULL)
999 				m0 = awi_wep_encrypt(sc, m0, 1);
1000 			if (m0 == NULL) {
1001 				ifp->if_oerrors++;
1002 				continue;
1003 			}
1004 			ifp->if_opackets++;
1005 		}
1006 #ifdef AWI_DEBUG
1007 		if (awi_dump)
1008 			awi_dump_pkt(sc, m0, -1);
1009 #endif
1010 		AWI_BPF_MTAP(sc, m0, AWI_BPF_RAW);
1011 		len = 0;
1012 		for (m = m0; m != NULL; m = m->m_next) {
1013 			awi_write_bytes(sc, frame + len, mtod(m, u_int8_t *),
1014 			    m->m_len);
1015 			len += m->m_len;
1016 		}
1017 		m_freem(m0);
1018 		rate = sc->sc_tx_rate;	/*XXX*/
1019 		awi_write_1(sc, ntxd + AWI_TXD_STATE, 0);
1020 		awi_write_4(sc, txd + AWI_TXD_START, frame);
1021 		awi_write_4(sc, txd + AWI_TXD_NEXT, ntxd);
1022 		awi_write_4(sc, txd + AWI_TXD_LENGTH, len);
1023 		awi_write_1(sc, txd + AWI_TXD_RATE, rate);
1024 		awi_write_4(sc, txd + AWI_TXD_NDA, 0);
1025 		awi_write_4(sc, txd + AWI_TXD_NRA, 0);
1026 		awi_write_1(sc, txd + AWI_TXD_STATE, AWI_TXD_ST_OWN);
1027 		sc->sc_txnext = ntxd;
1028 		sent++;
1029 	}
1030 	if (sent) {
1031 		if (sc->sc_tx_timer == 0)
1032 			sc->sc_tx_timer = 5;
1033 		ifp->if_timer = 1;
1034 #ifdef AWI_DEBUG
1035 		if (awi_verbose)
1036 			printf("awi_start: sent %d txdone %d txnext %d txbase %d txend %d\n", sent, sc->sc_txdone, sc->sc_txnext, sc->sc_txbase, sc->sc_txend);
1037 #endif
1038 	}
1039 }
1040 
1041 static void
awi_txint(sc)1042 awi_txint(sc)
1043 	struct awi_softc *sc;
1044 {
1045 	struct ifnet *ifp = sc->sc_ifp;
1046 	u_int8_t flags;
1047 
1048 	while (sc->sc_txdone != sc->sc_txnext) {
1049 		flags = awi_read_1(sc, sc->sc_txdone + AWI_TXD_STATE);
1050 		if ((flags & AWI_TXD_ST_OWN) || !(flags & AWI_TXD_ST_DONE))
1051 			break;
1052 		if (flags & AWI_TXD_ST_ERROR)
1053 			ifp->if_oerrors++;
1054 		sc->sc_txdone = awi_read_4(sc, sc->sc_txdone + AWI_TXD_NEXT) &
1055 		    0x7fff;
1056 	}
1057 	sc->sc_tx_timer = 0;
1058 	ifp->if_flags &= ~IFF_OACTIVE;
1059 #ifdef AWI_DEBUG
1060 	if (awi_verbose)
1061 		printf("awi_txint: txdone %d txnext %d txbase %d txend %d\n",
1062 		    sc->sc_txdone, sc->sc_txnext, sc->sc_txbase, sc->sc_txend);
1063 #endif
1064 	awi_start(ifp);
1065 }
1066 
1067 static struct mbuf *
awi_fix_txhdr(sc,m0)1068 awi_fix_txhdr(sc, m0)
1069 	struct awi_softc *sc;
1070 	struct mbuf *m0;
1071 {
1072 	struct ether_header eh;
1073 	struct ieee80211_frame *wh;
1074 	struct llc *llc;
1075 
1076 	if (m0->m_len < sizeof(eh)) {
1077 		m0 = m_pullup(m0, sizeof(eh));
1078 		if (m0 == NULL)
1079 			return NULL;
1080 	}
1081 	memcpy(&eh, mtod(m0, caddr_t), sizeof(eh));
1082 	if (sc->sc_format_llc) {
1083 		m_adj(m0, sizeof(struct ether_header) - sizeof(struct llc));
1084 		llc = mtod(m0, struct llc *);
1085 		llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1086 		llc->llc_control = LLC_UI;
1087 		llc->llc_snap.org_code[0] = llc->llc_snap.org_code[1] =
1088 		    llc->llc_snap.org_code[2] = 0;
1089 		llc->llc_snap.ether_type = eh.ether_type;
1090 	}
1091 	M_PREPEND(m0, sizeof(struct ieee80211_frame), M_DONTWAIT);
1092 	if (m0 == NULL)
1093 		return NULL;
1094 	wh = mtod(m0, struct ieee80211_frame *);
1095 
1096 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1097 	LE_WRITE_2(wh->i_dur, 0);
1098 	LE_WRITE_2(wh->i_seq, 0);
1099 	if (sc->sc_mib_local.Network_Mode) {
1100 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1101 		memcpy(wh->i_addr1, sc->sc_bss.bssid, ETHER_ADDR_LEN);
1102 		memcpy(wh->i_addr2, eh.ether_shost, ETHER_ADDR_LEN);
1103 		memcpy(wh->i_addr3, eh.ether_dhost, ETHER_ADDR_LEN);
1104 	} else {
1105 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1106 		memcpy(wh->i_addr1, eh.ether_dhost, ETHER_ADDR_LEN);
1107 		memcpy(wh->i_addr2, eh.ether_shost, ETHER_ADDR_LEN);
1108 		memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
1109 	}
1110 	return m0;
1111 }
1112 
1113 static struct mbuf *
awi_fix_rxhdr(sc,m0)1114 awi_fix_rxhdr(sc, m0)
1115 	struct awi_softc *sc;
1116 	struct mbuf *m0;
1117 {
1118 	struct ieee80211_frame wh;
1119 	struct ether_header *eh;
1120 	struct llc *llc;
1121 
1122 	if (m0->m_len < sizeof(wh)) {
1123 		m_freem(m0);
1124 		return NULL;
1125 	}
1126 	llc = (struct llc *)(mtod(m0, caddr_t) + sizeof(wh));
1127 	if (llc->llc_dsap == LLC_SNAP_LSAP &&
1128 	    llc->llc_ssap == LLC_SNAP_LSAP &&
1129 	    llc->llc_control == LLC_UI &&
1130 	    llc->llc_snap.org_code[0] == 0 &&
1131 	    llc->llc_snap.org_code[1] == 0 &&
1132 	    llc->llc_snap.org_code[2] == 0) {
1133 		memcpy(&wh, mtod(m0, caddr_t), sizeof(wh));
1134 		m_adj(m0, sizeof(wh) + sizeof(*llc) - sizeof(*eh));
1135 		eh = mtod(m0, struct ether_header *);
1136 		switch (wh.i_fc[1] & IEEE80211_FC1_DIR_MASK) {
1137 		case IEEE80211_FC1_DIR_NODS:
1138 			memcpy(eh->ether_dhost, wh.i_addr1, ETHER_ADDR_LEN);
1139 			memcpy(eh->ether_shost, wh.i_addr2, ETHER_ADDR_LEN);
1140 			break;
1141 		case IEEE80211_FC1_DIR_TODS:
1142 			memcpy(eh->ether_dhost, wh.i_addr3, ETHER_ADDR_LEN);
1143 			memcpy(eh->ether_shost, wh.i_addr2, ETHER_ADDR_LEN);
1144 			break;
1145 		case IEEE80211_FC1_DIR_FROMDS:
1146 			memcpy(eh->ether_dhost, wh.i_addr1, ETHER_ADDR_LEN);
1147 			memcpy(eh->ether_shost, wh.i_addr3, ETHER_ADDR_LEN);
1148 			break;
1149 		case IEEE80211_FC1_DIR_DSTODS:
1150 			m_freem(m0);
1151 			return NULL;
1152 		}
1153 	} else {
1154 		/* assuming ethernet encapsulation, just strip 802.11 header */
1155 		m_adj(m0, sizeof(wh));
1156 	}
1157 	if (ALIGN(mtod(m0, caddr_t) + sizeof(struct ether_header)) !=
1158 	    (u_int)(mtod(m0, caddr_t) + sizeof(struct ether_header))) {
1159 		/* XXX: we loose to estimate the type of encapsulation */
1160 		struct mbuf *n, *n0, **np;
1161 		caddr_t newdata;
1162 		int off, oldmlen;
1163 
1164 		n0 = NULL;
1165 		np = &n0;
1166 		off = 0;
1167 		oldmlen = m0->m_pkthdr.len;
1168 		while (oldmlen > off) {
1169 			if (n0 == NULL) {
1170 				MGETHDR(n, M_DONTWAIT, MT_DATA);
1171 				if (n == NULL) {
1172 					m_freem(m0);
1173 					return NULL;
1174 				}
1175 				M_MOVE_PKTHDR(n, m0);
1176 				n->m_len = MHLEN;
1177 			} else {
1178 				MGET(n, M_DONTWAIT, MT_DATA);
1179 				if (n == NULL) {
1180 					m_freem(m0);
1181 					m_freem(n0);
1182 					return NULL;
1183 				}
1184 				n->m_len = MLEN;
1185 			}
1186 			if (oldmlen - off >= MINCLSIZE) {
1187 				MCLGET(n, M_DONTWAIT);
1188 				if (n->m_flags & M_EXT)
1189 					n->m_len = n->m_ext.ext_size;
1190 			}
1191 			if (n0 == NULL) {
1192 				newdata = (caddr_t)
1193 				    ALIGN(n->m_data
1194 				    + sizeof(struct ether_header))
1195 				    - sizeof(struct ether_header);
1196 				n->m_len -= newdata - n->m_data;
1197 				n->m_data = newdata;
1198 			}
1199 			if (n->m_len > oldmlen - off)
1200 				n->m_len = oldmlen - off;
1201 			m_copydata(m0, off, n->m_len, mtod(n, caddr_t));
1202 			off += n->m_len;
1203 			*np = n;
1204 			np = &n->m_next;
1205 		}
1206 		m_freem(m0);
1207 		m0 = n0;
1208 	}
1209 	return m0;
1210 }
1211 
1212 static void
awi_input(sc,m,rxts,rssi)1213 awi_input(sc, m, rxts, rssi)
1214 	struct awi_softc *sc;
1215 	struct mbuf *m;
1216 	u_int32_t rxts;
1217 	u_int8_t rssi;
1218 {
1219 	struct ifnet *ifp = sc->sc_ifp;
1220 	struct ieee80211_frame *wh;
1221 
1222 	/* trim CRC here for WEP can find its own CRC at the end of packet. */
1223 	m_adj(m, -ETHER_CRC_LEN);
1224 	AWI_BPF_MTAP(sc, m, AWI_BPF_RAW);
1225 	wh = mtod(m, struct ieee80211_frame *);
1226 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
1227 	    IEEE80211_FC0_VERSION_0) {
1228 		printf("%s; receive packet with wrong version: %x\n",
1229 		    sc->sc_dev.dv_xname, wh->i_fc[0]);
1230 		m_freem(m);
1231 		ifp->if_ierrors++;
1232 		return;
1233 	}
1234 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1235 		m = awi_wep_encrypt(sc, m, 0);
1236 		if (m == NULL) {
1237 			ifp->if_ierrors++;
1238 			return;
1239 		}
1240 		wh = mtod(m, struct ieee80211_frame *);
1241 	}
1242 #ifdef AWI_DEBUG
1243 	if (awi_dump)
1244 		awi_dump_pkt(sc, m, rssi);
1245 #endif
1246 
1247 	if ((sc->sc_mib_local.Network_Mode || !sc->sc_no_bssid) &&
1248 	    sc->sc_status == AWI_ST_RUNNING) {
1249 		if (memcmp(wh->i_addr2, sc->sc_bss.bssid, ETHER_ADDR_LEN) == 0) {
1250 			sc->sc_rx_timer = 10;
1251 			sc->sc_bss.rssi = rssi;
1252 		}
1253 	}
1254 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1255 	case IEEE80211_FC0_TYPE_DATA:
1256 		if (sc->sc_mib_local.Network_Mode) {
1257 			if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
1258 			    IEEE80211_FC1_DIR_FROMDS) {
1259 				m_freem(m);
1260 				return;
1261 			}
1262 		} else {
1263 			if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
1264 			    IEEE80211_FC1_DIR_NODS) {
1265 				m_freem(m);
1266 				return;
1267 			}
1268 		}
1269 		m = awi_fix_rxhdr(sc, m);
1270 		if (m == NULL) {
1271 			ifp->if_ierrors++;
1272 			break;
1273 		}
1274 		ifp->if_ipackets++;
1275 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 4)
1276 		AWI_BPF_MTAP(sc, m, AWI_BPF_NORM);
1277 #endif
1278 #ifdef __NetBSD__
1279 		(*ifp->if_input)(ifp, m);
1280 #else
1281 		ether_input_mbuf(ifp, m);
1282 #endif
1283 		break;
1284 	case IEEE80211_FC0_TYPE_MGT:
1285 		if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
1286 		   IEEE80211_FC1_DIR_NODS) {
1287 			m_freem(m);
1288 			return;
1289 		}
1290 		switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
1291 		case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1292 		case IEEE80211_FC0_SUBTYPE_BEACON:
1293 			awi_recv_beacon(sc, m, rxts, rssi);
1294 			break;
1295 		case IEEE80211_FC0_SUBTYPE_AUTH:
1296 			awi_recv_auth(sc, m);
1297 			break;
1298 		case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1299 		case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1300 			awi_recv_asresp(sc, m);
1301 			break;
1302 		case IEEE80211_FC0_SUBTYPE_DEAUTH:
1303 			if (sc->sc_mib_local.Network_Mode)
1304 				awi_send_auth(sc, 1);
1305 			break;
1306 		case IEEE80211_FC0_SUBTYPE_DISASSOC:
1307 			if (sc->sc_mib_local.Network_Mode)
1308 				awi_send_asreq(sc, 1);
1309 			break;
1310 		}
1311 		m_freem(m);
1312 		break;
1313 	case IEEE80211_FC0_TYPE_CTL:
1314 	default:
1315 		/* should not come here */
1316 		m_freem(m);
1317 		break;
1318 	}
1319 }
1320 
1321 static void
awi_rxint(sc)1322 awi_rxint(sc)
1323 	struct awi_softc *sc;
1324 {
1325 	u_int8_t state, rate, rssi;
1326 	u_int16_t len;
1327 	u_int32_t frame, next, rxts, rxoff;
1328 	struct mbuf *m;
1329 
1330 	rxoff = sc->sc_rxdoff;
1331 	for (;;) {
1332 		state = awi_read_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE);
1333 		if (state & AWI_RXD_ST_OWN)
1334 			break;
1335 		if (!(state & AWI_RXD_ST_CONSUMED)) {
1336 			if (state & AWI_RXD_ST_RXERROR)
1337 				sc->sc_ifp->if_ierrors++;
1338 			else {
1339 				len   = awi_read_2(sc, rxoff + AWI_RXD_LEN);
1340 				rate  = awi_read_1(sc, rxoff + AWI_RXD_RATE);
1341 				rssi  = awi_read_1(sc, rxoff + AWI_RXD_RSSI);
1342 				frame = awi_read_4(sc, rxoff + AWI_RXD_START_FRAME) & 0x7fff;
1343 				rxts  = awi_read_4(sc, rxoff + AWI_RXD_LOCALTIME);
1344 				m = awi_devget(sc, frame, len);
1345 				if (state & AWI_RXD_ST_LF)
1346 					awi_input(sc, m, rxts, rssi);
1347 				else
1348 					sc->sc_rxpend = m;
1349 			}
1350 			state |= AWI_RXD_ST_CONSUMED;
1351 			awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1352 		}
1353 		next  = awi_read_4(sc, rxoff + AWI_RXD_NEXT);
1354 		if (next & AWI_RXD_NEXT_LAST)
1355 			break;
1356 		/* make sure the next pointer is correct */
1357 		if (next != awi_read_4(sc, rxoff + AWI_RXD_NEXT))
1358 			break;
1359 		state |= AWI_RXD_ST_OWN;
1360 		awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1361 		rxoff = next & 0x7fff;
1362 	}
1363 	sc->sc_rxdoff = rxoff;
1364 }
1365 
1366 static struct mbuf *
awi_devget(sc,off,len)1367 awi_devget(sc, off, len)
1368 	struct awi_softc *sc;
1369 	u_int32_t off;
1370 	u_int16_t len;
1371 {
1372 	struct mbuf *m;
1373 	struct mbuf *top, **mp;
1374 	u_int tlen;
1375 
1376 	top = sc->sc_rxpend;
1377 	mp = &top;
1378 	if (top != NULL) {
1379 		sc->sc_rxpend = NULL;
1380 		top->m_pkthdr.len += len;
1381 		m = top;
1382 		while (*mp != NULL) {
1383 			m = *mp;
1384 			mp = &m->m_next;
1385 		}
1386 		if (m->m_flags & M_EXT)
1387 			tlen = m->m_ext.ext_size;
1388 		else if (m->m_flags & M_PKTHDR)
1389 			tlen = MHLEN;
1390 		else
1391 			tlen = MLEN;
1392 		tlen -= m->m_len;
1393 		if (tlen > len)
1394 			tlen = len;
1395 		awi_read_bytes(sc, off, mtod(m, u_int8_t *) + m->m_len, tlen);
1396 		off += tlen;
1397 		len -= tlen;
1398 	}
1399 
1400 	while (len > 0) {
1401 		if (top == NULL) {
1402 			MGETHDR(m, M_DONTWAIT, MT_DATA);
1403 			if (m == NULL)
1404 				return NULL;
1405 			m->m_pkthdr.rcvif = sc->sc_ifp;
1406 			m->m_pkthdr.len = len;
1407 			m->m_len = MHLEN;
1408 		} else {
1409 			MGET(m, M_DONTWAIT, MT_DATA);
1410 			if (m == NULL) {
1411 				m_freem(top);
1412 				return NULL;
1413 			}
1414 			m->m_len = MLEN;
1415 		}
1416 		if (len >= MINCLSIZE) {
1417 			MCLGET(m, M_DONTWAIT);
1418 			if (m->m_flags & M_EXT)
1419 				m->m_len = m->m_ext.ext_size;
1420 		}
1421 		if (top == NULL) {
1422 			int hdrlen = sizeof(struct ieee80211_frame) +
1423 			    (sc->sc_format_llc ? sizeof(struct llc) :
1424 			    sizeof(struct ether_header));
1425 			caddr_t newdata = (caddr_t)
1426 			    ALIGN(m->m_data + hdrlen) - hdrlen;
1427 			m->m_len -= newdata - m->m_data;
1428 			m->m_data = newdata;
1429 		}
1430 		if (m->m_len > len)
1431 			m->m_len = len;
1432 		awi_read_bytes(sc, off, mtod(m, u_int8_t *), m->m_len);
1433 		off += m->m_len;
1434 		len -= m->m_len;
1435 		*mp = m;
1436 		mp = &m->m_next;
1437 	}
1438 	return top;
1439 }
1440 
1441 /*
1442  * Initialize hardware and start firmware to accept commands.
1443  * Called everytime after power on firmware.
1444  */
1445 
1446 static int
awi_init_hw(sc)1447 awi_init_hw(sc)
1448 	struct awi_softc *sc;
1449 {
1450 	u_int8_t status;
1451 	u_int16_t intmask;
1452 	int i, error;
1453 
1454 	sc->sc_enab_intr = 0;
1455 	sc->sc_invalid = 0;	/* XXX: really? */
1456 	awi_drvstate(sc, AWI_DRV_RESET);
1457 
1458 	/* reset firmware */
1459 	am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1460 	DELAY(100);
1461 	awi_write_1(sc, AWI_SELFTEST, 0);
1462 	awi_write_1(sc, AWI_CMD, 0);
1463 	awi_write_1(sc, AWI_BANNER, 0);
1464 	am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1465 	DELAY(100);
1466 
1467 	/* wait for selftest completion */
1468 	for (i = 0; ; i++) {
1469 		if (i >= AWI_SELFTEST_TIMEOUT*hz/1000) {
1470 			printf("%s: failed to complete selftest (timeout)\n",
1471 			    sc->sc_dev.dv_xname);
1472 			return ENXIO;
1473 		}
1474 		status = awi_read_1(sc, AWI_SELFTEST);
1475 		if ((status & 0xf0) == 0xf0)
1476 			break;
1477 		if (sc->sc_cansleep) {
1478 			sc->sc_sleep_cnt++;
1479 			(void)tsleep(sc, PWAIT, "awitst", 1);
1480 			sc->sc_sleep_cnt--;
1481 		} else {
1482 			DELAY(1000*1000/hz);
1483 		}
1484 	}
1485 	if (status != AWI_SELFTEST_PASSED) {
1486 		printf("%s: failed to complete selftest (code %x)\n",
1487 		    sc->sc_dev.dv_xname, status);
1488 		return ENXIO;
1489 	}
1490 
1491 	/* check banner to confirm firmware write it */
1492 	awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
1493 	if (memcmp(sc->sc_banner, "PCnetMobile:", 12) != 0) {
1494 		printf("%s: failed to complete selftest (bad banner)\n",
1495 		    sc->sc_dev.dv_xname);
1496 		for (i = 0; i < AWI_BANNER_LEN; i++)
1497 			printf("%s%02x", i ? ":" : "\t", sc->sc_banner[i]);
1498 		printf("\n");
1499 		return ENXIO;
1500 	}
1501 
1502 	/* initializing interrupt */
1503 	sc->sc_enab_intr = 1;
1504 	error = awi_intr_lock(sc);
1505 	if (error)
1506 		return error;
1507 	intmask = AWI_INT_GROGGY | AWI_INT_SCAN_CMPLT |
1508 	    AWI_INT_TX | AWI_INT_RX | AWI_INT_CMD;
1509 	awi_write_1(sc, AWI_INTMASK, ~intmask & 0xff);
1510 	awi_write_1(sc, AWI_INTMASK2, 0);
1511 	awi_write_1(sc, AWI_INTSTAT, 0);
1512 	awi_write_1(sc, AWI_INTSTAT2, 0);
1513 	awi_intr_unlock(sc);
1514 	am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_ENECINT);
1515 
1516 	/* issueing interface test command */
1517 	error = awi_cmd(sc, AWI_CMD_NOP);
1518 	if (error) {
1519 		printf("%s: failed to complete selftest", sc->sc_dev.dv_xname);
1520 		if (error == ENXIO)
1521 			printf(" (no hardware)\n");
1522 		else if (error != EWOULDBLOCK)
1523 			printf(" (error %d)\n", error);
1524 		else if (sc->sc_cansleep)
1525 			printf(" (lost interrupt)\n");
1526 		else
1527 			printf(" (command timeout)\n");
1528 	}
1529 	return error;
1530 }
1531 
1532 /*
1533  * Extract the factory default MIB value from firmware and assign the driver
1534  * default value.
1535  * Called once at attaching the interface.
1536  */
1537 
1538 static int
awi_init_mibs(sc)1539 awi_init_mibs(sc)
1540 	struct awi_softc *sc;
1541 {
1542 	int i, error;
1543 	u_int8_t *rate;
1544 
1545 	if ((error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_LOCAL)) != 0 ||
1546 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_ADDR)) != 0 ||
1547 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MAC)) != 0 ||
1548 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MGT)) != 0 ||
1549 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_PHY)) != 0) {
1550 		printf("%s: failed to get default mib value (error %d)\n",
1551 		    sc->sc_dev.dv_xname, error);
1552 		return error;
1553 	}
1554 
1555 	rate = sc->sc_mib_phy.aSuprt_Data_Rates;
1556 	sc->sc_tx_rate = AWI_RATE_1MBIT;
1557 	for (i = 0; i < rate[1]; i++) {
1558 		if (AWI_80211_RATE(rate[2 + i]) > sc->sc_tx_rate)
1559 			sc->sc_tx_rate = AWI_80211_RATE(rate[2 + i]);
1560 	}
1561 	awi_init_region(sc);
1562 	memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
1563 	sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
1564 	sc->sc_mib_local.Fragmentation_Dis = 1;
1565 	sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
1566 	sc->sc_mib_local.Power_Saving_Mode_Dis = 1;
1567 
1568 	/* allocate buffers */
1569 	sc->sc_txbase = AWI_BUFFERS;
1570 	sc->sc_txend = sc->sc_txbase +
1571 	    (AWI_TXD_SIZE + sizeof(struct ieee80211_frame) +
1572 	    sizeof(struct ether_header) + ETHERMTU) * AWI_NTXBUFS;
1573 	LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Offset, sc->sc_txbase);
1574 	LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Size,
1575 	    sc->sc_txend - sc->sc_txbase);
1576 	LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Offset, sc->sc_txend);
1577 	LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Size,
1578 	    AWI_BUFFERS_END - sc->sc_txend);
1579 	sc->sc_mib_local.Network_Mode = 1;
1580 	sc->sc_mib_local.Acting_as_AP = 0;
1581 	return 0;
1582 }
1583 
1584 /*
1585  * Start transmitter and receiver of firmware
1586  * Called after awi_init_hw() to start operation.
1587  */
1588 
1589 static int
awi_init_txrx(sc)1590 awi_init_txrx(sc)
1591 	struct awi_softc *sc;
1592 {
1593 	int error;
1594 
1595 	/* start transmitter */
1596 	sc->sc_txdone = sc->sc_txnext = sc->sc_txbase;
1597 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_START, 0);
1598 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_NEXT, 0);
1599 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_LENGTH, 0);
1600 	awi_write_1(sc, sc->sc_txbase + AWI_TXD_RATE, 0);
1601 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_NDA, 0);
1602 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_NRA, 0);
1603 	awi_write_1(sc, sc->sc_txbase + AWI_TXD_STATE, 0);
1604 	awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_DATA, sc->sc_txbase);
1605 	awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_MGT, 0);
1606 	awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_BCAST, 0);
1607 	awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_PS, 0);
1608 	awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_CF, 0);
1609 	error = awi_cmd(sc, AWI_CMD_INIT_TX);
1610 	if (error)
1611 		return error;
1612 
1613 	/* start receiver */
1614 	if (sc->sc_rxpend) {
1615 		m_freem(sc->sc_rxpend);
1616 		sc->sc_rxpend = NULL;
1617 	}
1618 	error = awi_cmd(sc, AWI_CMD_INIT_RX);
1619 	if (error)
1620 		return error;
1621 	sc->sc_rxdoff = awi_read_4(sc, AWI_CMD_PARAMS+AWI_CA_IRX_DATA_DESC);
1622 	sc->sc_rxmoff = awi_read_4(sc, AWI_CMD_PARAMS+AWI_CA_IRX_PS_DESC);
1623 	return 0;
1624 }
1625 
1626 static void
awi_stop_txrx(sc)1627 awi_stop_txrx(sc)
1628 	struct awi_softc *sc;
1629 {
1630 
1631 	if (sc->sc_cmd_inprog)
1632 		(void)awi_cmd_wait(sc);
1633 	(void)awi_cmd(sc, AWI_CMD_KILL_RX);
1634 	(void)awi_cmd_wait(sc);
1635 	sc->sc_cmd_inprog = AWI_CMD_FLUSH_TX;
1636 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_DATA, 1);
1637 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_MGT, 0);
1638 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_BCAST, 0);
1639 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_PS, 0);
1640 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_CF, 0);
1641 	(void)awi_cmd(sc, AWI_CMD_FLUSH_TX);
1642 	(void)awi_cmd_wait(sc);
1643 }
1644 
1645 int
awi_init_region(sc)1646 awi_init_region(sc)
1647 	struct awi_softc *sc;
1648 {
1649 
1650 	if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1651 		switch (sc->sc_mib_phy.aCurrent_Reg_Domain) {
1652 		case AWI_REG_DOMAIN_US:
1653 		case AWI_REG_DOMAIN_CA:
1654 		case AWI_REG_DOMAIN_EU:
1655 			sc->sc_scan_min = 0;
1656 			sc->sc_scan_max = 77;
1657 			break;
1658 		case AWI_REG_DOMAIN_ES:
1659 			sc->sc_scan_min = 0;
1660 			sc->sc_scan_max = 26;
1661 			break;
1662 		case AWI_REG_DOMAIN_FR:
1663 			sc->sc_scan_min = 0;
1664 			sc->sc_scan_max = 32;
1665 			break;
1666 		case AWI_REG_DOMAIN_JP:
1667 			sc->sc_scan_min = 6;
1668 			sc->sc_scan_max = 17;
1669 			break;
1670 		default:
1671 			return EINVAL;
1672 		}
1673 		sc->sc_scan_set = sc->sc_scan_cur % 3 + 1;
1674 	} else {
1675 		switch (sc->sc_mib_phy.aCurrent_Reg_Domain) {
1676 		case AWI_REG_DOMAIN_US:
1677 		case AWI_REG_DOMAIN_CA:
1678 			sc->sc_scan_min = 1;
1679 			sc->sc_scan_max = 11;
1680 			sc->sc_scan_cur = 3;
1681 			break;
1682 		case AWI_REG_DOMAIN_EU:
1683 			sc->sc_scan_min = 1;
1684 			sc->sc_scan_max = 13;
1685 			sc->sc_scan_cur = 3;
1686 			break;
1687 		case AWI_REG_DOMAIN_ES:
1688 			sc->sc_scan_min = 10;
1689 			sc->sc_scan_max = 11;
1690 			sc->sc_scan_cur = 10;
1691 			break;
1692 		case AWI_REG_DOMAIN_FR:
1693 			sc->sc_scan_min = 10;
1694 			sc->sc_scan_max = 13;
1695 			sc->sc_scan_cur = 10;
1696 			break;
1697 		case AWI_REG_DOMAIN_JP:
1698 			sc->sc_scan_min = 14;
1699 			sc->sc_scan_max = 14;
1700 			sc->sc_scan_cur = 14;
1701 			break;
1702 		default:
1703 			return EINVAL;
1704 		}
1705 	}
1706 	sc->sc_ownch = sc->sc_scan_cur;
1707 	return 0;
1708 }
1709 
1710 static int
awi_start_scan(sc)1711 awi_start_scan(sc)
1712 	struct awi_softc *sc;
1713 {
1714 	int error = 0;
1715 	struct awi_bss *bp;
1716 
1717 	while ((bp = TAILQ_FIRST(&sc->sc_scan)) != NULL) {
1718 		TAILQ_REMOVE(&sc->sc_scan, bp, list);
1719 		free(bp, M_DEVBUF);
1720 	}
1721 	if (!sc->sc_mib_local.Network_Mode && sc->sc_no_bssid) {
1722 		memset(&sc->sc_bss, 0, sizeof(sc->sc_bss));
1723 		sc->sc_bss.essid[0] = IEEE80211_ELEMID_SSID;
1724 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1725 			sc->sc_bss.chanset = sc->sc_ownch % 3 + 1;
1726 			sc->sc_bss.pattern = sc->sc_ownch;
1727 			sc->sc_bss.index = 1;
1728 			sc->sc_bss.dwell_time = 200;	/*XXX*/
1729 		} else
1730 			sc->sc_bss.chanset = sc->sc_ownch;
1731 		sc->sc_status = AWI_ST_SETSS;
1732 		error = awi_set_ss(sc);
1733 	} else {
1734 		if (sc->sc_mib_local.Network_Mode)
1735 			awi_drvstate(sc, AWI_DRV_INFSC);
1736 		else
1737 			awi_drvstate(sc, AWI_DRV_ADHSC);
1738 		sc->sc_start_bss = 0;
1739 		sc->sc_active_scan = 1;
1740 		sc->sc_mgt_timer = AWI_ASCAN_WAIT / 1000;
1741 		sc->sc_ifp->if_timer = 1;
1742 		sc->sc_status = AWI_ST_SCAN;
1743 		error = awi_cmd_scan(sc);
1744 	}
1745 	return error;
1746 }
1747 
1748 static int
awi_next_scan(sc)1749 awi_next_scan(sc)
1750 	struct awi_softc *sc;
1751 {
1752 	int error;
1753 
1754 	for (;;) {
1755 		/*
1756 		 * The pattern parameter for FH phy should be incremented
1757 		 * by 3.  But BayStack 650 Access Points apparently always
1758 		 * assign hop pattern set parameter to 1 for any pattern.
1759 		 * So we try all combinations of pattern/set parameters.
1760 		 * Since this causes no error, it may be a bug of
1761 		 * PCnetMobile firmware.
1762 		 */
1763 		sc->sc_scan_cur++;
1764 		if (sc->sc_scan_cur > sc->sc_scan_max) {
1765 			sc->sc_scan_cur = sc->sc_scan_min;
1766 			if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
1767 				sc->sc_scan_set = sc->sc_scan_set % 3 + 1;
1768 		}
1769 		error = awi_cmd_scan(sc);
1770 		if (error != EINVAL)
1771 			break;
1772 	}
1773 	return error;
1774 }
1775 
1776 static void
awi_stop_scan(sc)1777 awi_stop_scan(sc)
1778 	struct awi_softc *sc;
1779 {
1780 	struct ifnet *ifp = sc->sc_ifp;
1781 	struct awi_bss *bp, *sbp;
1782 	int fail;
1783 
1784 	bp = TAILQ_FIRST(&sc->sc_scan);
1785 	if (bp == NULL) {
1786   notfound:
1787 		if (sc->sc_active_scan) {
1788 			if (ifp->if_flags & IFF_DEBUG)
1789 				printf("%s: entering passive scan mode\n",
1790 				    sc->sc_dev.dv_xname);
1791 			sc->sc_active_scan = 0;
1792 		}
1793 		sc->sc_mgt_timer = AWI_PSCAN_WAIT / 1000;
1794 		ifp->if_timer = 1;
1795 		(void)awi_next_scan(sc);
1796 		return;
1797 	}
1798 	sbp = NULL;
1799 	if (ifp->if_flags & IFF_DEBUG)
1800 		printf("%s:\tmacaddr     ch/pat   sig flag  wep  essid\n",
1801 		    sc->sc_dev.dv_xname);
1802 	for (; bp != NULL; bp = TAILQ_NEXT(bp, list)) {
1803 		if (bp->fails) {
1804 			/*
1805 			 * The configuration of the access points may change
1806 			 * during my scan.  So we retries to associate with
1807 			 * it unless there are any suitable AP.
1808 			 */
1809 			if (bp->fails++ < 3)
1810 				continue;
1811 			bp->fails = 0;
1812 		}
1813 		fail = 0;
1814 		/*
1815 		 * Since the firmware apparently scans not only the specified
1816 		 * channel of SCAN command but all available channel within
1817 		 * the region, we should filter out unnecessary responses here.
1818 		 */
1819 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1820 			if (bp->pattern < sc->sc_scan_min ||
1821 			    bp->pattern > sc->sc_scan_max)
1822 				fail |= 0x01;
1823 		} else {
1824 			if (bp->chanset < sc->sc_scan_min ||
1825 			    bp->chanset > sc->sc_scan_max)
1826 				fail |= 0x01;
1827 		}
1828 		if (sc->sc_mib_local.Network_Mode) {
1829 			if (!(bp->capinfo & IEEE80211_CAPINFO_ESS) ||
1830 			    (bp->capinfo & IEEE80211_CAPINFO_IBSS))
1831 				fail |= 0x02;
1832 		} else {
1833 			if ((bp->capinfo & IEEE80211_CAPINFO_ESS) ||
1834 			    !(bp->capinfo & IEEE80211_CAPINFO_IBSS))
1835 				fail |= 0x02;
1836 		}
1837 		if (sc->sc_wep_algo == NULL) {
1838 			if (bp->capinfo & IEEE80211_CAPINFO_PRIVACY)
1839 				fail |= 0x04;
1840 		} else {
1841 			if (!(bp->capinfo & IEEE80211_CAPINFO_PRIVACY))
1842 				fail |= 0x04;
1843 		}
1844 		if (sc->sc_mib_mac.aDesired_ESS_ID[1] != 0 &&
1845 		    memcmp(&sc->sc_mib_mac.aDesired_ESS_ID, bp->essid,
1846 		    sizeof(bp->essid)) != 0)
1847 			fail |= 0x08;
1848 		if (ifp->if_flags & IFF_DEBUG) {
1849 			printf(" %c %s", fail ? '-' : '+',
1850 			    ether_sprintf(bp->esrc));
1851 			if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
1852 				printf("  %2d/%d%c", bp->pattern, bp->chanset,
1853 				    fail & 0x01 ? '!' : ' ');
1854 			else
1855 				printf("  %4d%c", bp->chanset,
1856 				    fail & 0x01 ? '!' : ' ');
1857 			printf(" %+4d", bp->rssi);
1858 			printf(" %4s%c",
1859 			    (bp->capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
1860 			    (bp->capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
1861 			    "????",
1862 			    fail & 0x02 ? '!' : ' ');
1863 			printf(" %3s%c ",
1864 			    (bp->capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" :
1865 			    "no",
1866 			    fail & 0x04 ? '!' : ' ');
1867 			awi_print_essid(bp->essid);
1868 			printf("%s\n", fail & 0x08 ? "!" : "");
1869 		}
1870 		if (!fail) {
1871 			if (sbp == NULL || bp->rssi > sbp->rssi)
1872 				sbp = bp;
1873 		}
1874 	}
1875 	if (sbp == NULL)
1876 		goto notfound;
1877 	sc->sc_bss = *sbp;
1878 	(void)awi_set_ss(sc);
1879 }
1880 
1881 static void
awi_recv_beacon(sc,m0,rxts,rssi)1882 awi_recv_beacon(sc, m0, rxts, rssi)
1883 	struct awi_softc *sc;
1884 	struct mbuf *m0;
1885 	u_int32_t rxts;
1886 	u_int8_t rssi;
1887 {
1888 	struct ieee80211_frame *wh;
1889 	struct awi_bss *bp;
1890 	u_int8_t *frame, *eframe;
1891 	u_int8_t *tstamp, *bintval, *capinfo, *ssid, *rates, *parms;
1892 
1893 	if (sc->sc_status != AWI_ST_SCAN)
1894 		return;
1895 	wh = mtod(m0, struct ieee80211_frame *);
1896 
1897 	frame = (u_int8_t *)&wh[1];
1898 	eframe = mtod(m0, u_int8_t *) + m0->m_len;
1899 	/*
1900 	 * XXX:
1901 	 *	timestamp [8]
1902 	 *	beacon interval [2]
1903 	 *	capability information [2]
1904 	 *	ssid [tlv]
1905 	 *	supported rates [tlv]
1906 	 *	parameter set [tlv]
1907 	 *	...
1908 	 */
1909 	if (frame + 12 > eframe) {
1910 #ifdef AWI_DEBUG
1911 		if (awi_verbose)
1912 			printf("awi_recv_beacon: frame too short \n");
1913 #endif
1914 		return;
1915 	}
1916 	tstamp = frame;
1917 	frame += 8;
1918 	bintval = frame;
1919 	frame += 2;
1920 	capinfo = frame;
1921 	frame += 2;
1922 
1923 	ssid = rates = parms = NULL;
1924 	while (frame < eframe) {
1925 		switch (*frame) {
1926 		case IEEE80211_ELEMID_SSID:
1927 			ssid = frame;
1928 			break;
1929 		case IEEE80211_ELEMID_RATES:
1930 			rates = frame;
1931 			break;
1932 		case IEEE80211_ELEMID_FHPARMS:
1933 		case IEEE80211_ELEMID_DSPARMS:
1934 			parms = frame;
1935 			break;
1936 		}
1937 		frame += frame[1] + 2;
1938 	}
1939 	if (ssid == NULL || rates == NULL || parms == NULL) {
1940 #ifdef AWI_DEBUG
1941 		if (awi_verbose)
1942 			printf("awi_recv_beacon: ssid=%p, rates=%p, parms=%p\n",
1943 			    ssid, rates, parms);
1944 #endif
1945 		return;
1946 	}
1947 	if (ssid[1] > IEEE80211_NWID_LEN) {
1948 #ifdef AWI_DEBUG
1949 		if (awi_verbose)
1950 			printf("awi_recv_beacon: bad ssid len: %d from %s\n",
1951 			    ssid[1], ether_sprintf(wh->i_addr2));
1952 #endif
1953 		return;
1954 	}
1955 
1956 	for (bp = TAILQ_FIRST(&sc->sc_scan); bp != NULL;
1957 	    bp = TAILQ_NEXT(bp, list)) {
1958 		if (memcmp(bp->esrc, wh->i_addr2, ETHER_ADDR_LEN) == 0 &&
1959 		    memcmp(bp->bssid, wh->i_addr3, ETHER_ADDR_LEN) == 0)
1960 			break;
1961 	}
1962 	if (bp == NULL) {
1963 		bp = malloc(sizeof(struct awi_bss), M_DEVBUF, M_NOWAIT);
1964 		if (bp == NULL)
1965 			return;
1966 		TAILQ_INSERT_TAIL(&sc->sc_scan, bp, list);
1967 		memcpy(bp->esrc, wh->i_addr2, ETHER_ADDR_LEN);
1968 		memcpy(bp->bssid, wh->i_addr3, ETHER_ADDR_LEN);
1969 		memset(bp->essid, 0, sizeof(bp->essid));
1970 		memcpy(bp->essid, ssid, 2 + ssid[1]);
1971 	}
1972 	bp->rssi = rssi;
1973 	bp->rxtime = rxts;
1974 	memcpy(bp->timestamp, tstamp, sizeof(bp->timestamp));
1975 	bp->interval = LE_READ_2(bintval);
1976 	bp->capinfo = LE_READ_2(capinfo);
1977 	if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1978 		bp->chanset = parms[4];
1979 		bp->pattern = parms[5];
1980 		bp->index = parms[6];
1981 		bp->dwell_time = LE_READ_2(parms + 2);
1982 	} else {
1983 		bp->chanset = parms[2];
1984 		bp->pattern = 0;
1985 		bp->index = 0;
1986 		bp->dwell_time = 0;
1987 	}
1988 	if (sc->sc_mgt_timer == 0)
1989 		awi_stop_scan(sc);
1990 }
1991 
1992 static int
awi_set_ss(sc)1993 awi_set_ss(sc)
1994 	struct awi_softc *sc;
1995 {
1996 	struct ifnet *ifp = sc->sc_ifp;
1997 	struct awi_bss *bp;
1998 	int error;
1999 
2000 	sc->sc_status = AWI_ST_SETSS;
2001 	bp = &sc->sc_bss;
2002 	if (ifp->if_flags & IFF_DEBUG) {
2003 		printf("%s: ch %d pat %d id %d dw %d iv %d bss %s ssid ",
2004 		    sc->sc_dev.dv_xname, bp->chanset,
2005 		    bp->pattern, bp->index, bp->dwell_time, bp->interval,
2006 		    ether_sprintf(bp->bssid));
2007 		awi_print_essid(bp->essid);
2008 		printf("\n");
2009 	}
2010 	memcpy(&sc->sc_mib_mgt.aCurrent_BSS_ID, bp->bssid, ETHER_ADDR_LEN);
2011 	memcpy(&sc->sc_mib_mgt.aCurrent_ESS_ID, bp->essid,
2012 	    AWI_ESS_ID_SIZE);
2013 	LE_WRITE_2(&sc->sc_mib_mgt.aBeacon_Period, bp->interval);
2014 	error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT);
2015 	return error;
2016 }
2017 
2018 static void
awi_try_sync(sc)2019 awi_try_sync(sc)
2020 	struct awi_softc *sc;
2021 {
2022 	struct awi_bss *bp;
2023 
2024 	sc->sc_status = AWI_ST_SYNC;
2025 	bp = &sc->sc_bss;
2026 
2027 	if (sc->sc_cmd_inprog) {
2028 		if (awi_cmd_wait(sc))
2029 			return;
2030 	}
2031 	sc->sc_cmd_inprog = AWI_CMD_SYNC;
2032 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_SET, bp->chanset);
2033 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_PATTERN, bp->pattern);
2034 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_IDX, bp->index);
2035 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_STARTBSS,
2036 	    sc->sc_start_bss ? 1 : 0);
2037 	awi_write_2(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_DWELL, bp->dwell_time);
2038 	awi_write_2(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_MBZ, 0);
2039 	awi_write_bytes(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_TIMESTAMP,
2040 	    bp->timestamp, 8);
2041 	awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_REFTIME, bp->rxtime);
2042 	(void)awi_cmd(sc, AWI_CMD_SYNC);
2043 }
2044 
2045 static void
awi_sync_done(sc)2046 awi_sync_done(sc)
2047 	struct awi_softc *sc;
2048 {
2049 	struct ifnet *ifp = sc->sc_ifp;
2050 
2051 	if (sc->sc_mib_local.Network_Mode) {
2052 		awi_drvstate(sc, AWI_DRV_INFSY);
2053 		awi_send_auth(sc, 1);
2054 	} else {
2055 		if (ifp->if_flags & IFF_DEBUG) {
2056 			printf("%s: synced with", sc->sc_dev.dv_xname);
2057 			if (sc->sc_no_bssid)
2058 				printf(" no-bssid");
2059 			else {
2060 				printf(" %s ssid ",
2061 				    ether_sprintf(sc->sc_bss.bssid));
2062 				awi_print_essid(sc->sc_bss.essid);
2063 			}
2064 			if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
2065 				printf(" at chanset %d pattern %d\n",
2066 				    sc->sc_bss.chanset, sc->sc_bss.pattern);
2067 			else
2068 				printf(" at channel %d\n", sc->sc_bss.chanset);
2069 		}
2070 		awi_drvstate(sc, AWI_DRV_ADHSY);
2071 		sc->sc_status = AWI_ST_RUNNING;
2072 		ifp->if_flags |= IFF_RUNNING;
2073 		awi_start(ifp);
2074 	}
2075 }
2076 
2077 static void
awi_send_deauth(sc)2078 awi_send_deauth(sc)
2079 	struct awi_softc *sc;
2080 {
2081 	struct ifnet *ifp = sc->sc_ifp;
2082 	struct mbuf *m;
2083 	struct ieee80211_frame *wh;
2084 	u_int8_t *deauth;
2085 
2086 	MGETHDR(m, M_DONTWAIT, MT_DATA);
2087 	if (m == NULL)
2088 		return;
2089 	if (ifp->if_flags & IFF_DEBUG)
2090 		printf("%s: sending deauth to %s\n", sc->sc_dev.dv_xname,
2091 		    ether_sprintf(sc->sc_bss.bssid));
2092 
2093 	wh = mtod(m, struct ieee80211_frame *);
2094 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2095 	    IEEE80211_FC0_SUBTYPE_AUTH;
2096 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2097 	LE_WRITE_2(wh->i_dur, 0);
2098 	LE_WRITE_2(wh->i_seq, 0);
2099 	memcpy(wh->i_addr1, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2100 	memcpy(wh->i_addr2, sc->sc_mib_addr.aMAC_Address, ETHER_ADDR_LEN);
2101 	memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2102 
2103 	deauth = (u_int8_t *)&wh[1];
2104 	LE_WRITE_2(deauth, IEEE80211_REASON_AUTH_LEAVE);
2105 	deauth += 2;
2106 
2107 	m->m_pkthdr.len = m->m_len = deauth - mtod(m, u_int8_t *);
2108 	IF_ENQUEUE(&sc->sc_mgtq, m);
2109 	awi_start(ifp);
2110 	awi_drvstate(sc, AWI_DRV_INFTOSS);
2111 }
2112 
2113 static void
awi_send_auth(sc,seq)2114 awi_send_auth(sc, seq)
2115 	struct awi_softc *sc;
2116 	int seq;
2117 {
2118 	struct ifnet *ifp = sc->sc_ifp;
2119 	struct mbuf *m;
2120 	struct ieee80211_frame *wh;
2121 	u_int8_t *auth;
2122 
2123 	MGETHDR(m, M_DONTWAIT, MT_DATA);
2124 	if (m == NULL)
2125 		return;
2126 	sc->sc_status = AWI_ST_AUTH;
2127 	if (ifp->if_flags & IFF_DEBUG)
2128 		printf("%s: sending auth to %s\n", sc->sc_dev.dv_xname,
2129 		    ether_sprintf(sc->sc_bss.bssid));
2130 
2131 	wh = mtod(m, struct ieee80211_frame *);
2132 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2133 	    IEEE80211_FC0_SUBTYPE_AUTH;
2134 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2135 	LE_WRITE_2(wh->i_dur, 0);
2136 	LE_WRITE_2(wh->i_seq, 0);
2137 	memcpy(wh->i_addr1, sc->sc_bss.esrc, ETHER_ADDR_LEN);
2138 	memcpy(wh->i_addr2, sc->sc_mib_addr.aMAC_Address, ETHER_ADDR_LEN);
2139 	memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2140 
2141 	auth = (u_int8_t *)&wh[1];
2142 	/* algorithm number */
2143 	LE_WRITE_2(auth, IEEE80211_AUTH_ALG_OPEN);
2144 	auth += 2;
2145 	/* sequence number */
2146 	LE_WRITE_2(auth, seq);
2147 	auth += 2;
2148 	/* status */
2149 	LE_WRITE_2(auth, 0);
2150 	auth += 2;
2151 
2152 	m->m_pkthdr.len = m->m_len = auth - mtod(m, u_int8_t *);
2153 	IF_ENQUEUE(&sc->sc_mgtq, m);
2154 	awi_start(ifp);
2155 
2156 	sc->sc_mgt_timer = AWI_TRANS_TIMEOUT / 1000;
2157 	ifp->if_timer = 1;
2158 }
2159 
2160 static void
awi_recv_auth(sc,m0)2161 awi_recv_auth(sc, m0)
2162 	struct awi_softc *sc;
2163 	struct mbuf *m0;
2164 {
2165 	struct ieee80211_frame *wh;
2166 	u_int8_t *auth, *eframe;
2167 	struct awi_bss *bp;
2168 	u_int16_t status;
2169 
2170 	wh = mtod(m0, struct ieee80211_frame *);
2171 	auth = (u_int8_t *)&wh[1];
2172 	eframe = mtod(m0, u_int8_t *) + m0->m_len;
2173 	if (sc->sc_ifp->if_flags & IFF_DEBUG)
2174 		printf("%s: receive auth from %s\n", sc->sc_dev.dv_xname,
2175 		    ether_sprintf(wh->i_addr2));
2176 
2177 	/* algorithm number */
2178 	if (LE_READ_2(auth) != IEEE80211_AUTH_ALG_OPEN)
2179 		return;
2180 	auth += 2;
2181 	if (!sc->sc_mib_local.Network_Mode) {
2182 		if (sc->sc_status != AWI_ST_RUNNING)
2183 			return;
2184 		if (LE_READ_2(auth) == 1)
2185 			awi_send_auth(sc, 2);
2186 		return;
2187 	}
2188 	if (sc->sc_status != AWI_ST_AUTH)
2189 		return;
2190 	/* sequence number */
2191 	if (LE_READ_2(auth) != 2)
2192 		return;
2193 	auth += 2;
2194 	/* status */
2195 	status = LE_READ_2(auth);
2196 	if (status != 0) {
2197 		printf("%s: authentication failed (reason %d)\n",
2198 		    sc->sc_dev.dv_xname, status);
2199 		for (bp = TAILQ_FIRST(&sc->sc_scan); bp != NULL;
2200 		    bp = TAILQ_NEXT(bp, list)) {
2201 			if (memcmp(bp->esrc, sc->sc_bss.esrc, ETHER_ADDR_LEN)
2202 			    == 0) {
2203 				bp->fails++;
2204 				break;
2205 			}
2206 		}
2207 		return;
2208 	}
2209 	sc->sc_mgt_timer = 0;
2210 	awi_drvstate(sc, AWI_DRV_INFAUTH);
2211 	awi_send_asreq(sc, 0);
2212 }
2213 
2214 static void
awi_send_asreq(sc,reassoc)2215 awi_send_asreq(sc, reassoc)
2216 	struct awi_softc *sc;
2217 	int reassoc;
2218 {
2219 	struct ifnet *ifp = sc->sc_ifp;
2220 	struct mbuf *m;
2221 	struct ieee80211_frame *wh;
2222 	u_int16_t lintval;
2223 	u_int8_t *asreq;
2224 
2225 	MGETHDR(m, M_DONTWAIT, MT_DATA);
2226 	if (m == NULL)
2227 		return;
2228 	sc->sc_status = AWI_ST_ASSOC;
2229 	if (ifp->if_flags & IFF_DEBUG)
2230 		printf("%s: sending %sassoc req to %s\n", sc->sc_dev.dv_xname,
2231 		    reassoc ? "re" : "",
2232 		    ether_sprintf(sc->sc_bss.bssid));
2233 
2234 	wh = mtod(m, struct ieee80211_frame *);
2235 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT;
2236 	if (reassoc)
2237 		wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_REASSOC_REQ;
2238 	else
2239 		wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_ASSOC_REQ;
2240 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2241 	LE_WRITE_2(wh->i_dur, 0);
2242 	LE_WRITE_2(wh->i_seq, 0);
2243 	memcpy(wh->i_addr1, sc->sc_bss.esrc, ETHER_ADDR_LEN);
2244 	memcpy(wh->i_addr2, sc->sc_mib_addr.aMAC_Address, ETHER_ADDR_LEN);
2245 	memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2246 
2247 	asreq = (u_int8_t *)&wh[1];
2248 
2249 	/* capability info */
2250 	if (sc->sc_wep_algo == NULL)
2251 		LE_WRITE_2(asreq, IEEE80211_CAPINFO_CF_POLLABLE);
2252 	else
2253 		LE_WRITE_2(asreq,
2254 		    IEEE80211_CAPINFO_CF_POLLABLE | IEEE80211_CAPINFO_PRIVACY);
2255 	asreq += 2;
2256 	/* listen interval */
2257 	lintval = LE_READ_2(&sc->sc_mib_mgt.aListen_Interval);
2258 	LE_WRITE_2(asreq, lintval);
2259 	asreq += 2;
2260 	if (reassoc) {
2261 		/* current AP address */
2262 		memcpy(asreq, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2263 		asreq += ETHER_ADDR_LEN;
2264 	}
2265 	/* ssid */
2266 	memcpy(asreq, sc->sc_bss.essid, 2 + sc->sc_bss.essid[1]);
2267 	asreq += 2 + asreq[1];
2268 	/* supported rates */
2269 	memcpy(asreq, &sc->sc_mib_phy.aSuprt_Data_Rates, 4);
2270 	asreq += 2 + asreq[1];
2271 
2272 	m->m_pkthdr.len = m->m_len = asreq - mtod(m, u_int8_t *);
2273 	IF_ENQUEUE(&sc->sc_mgtq, m);
2274 	awi_start(ifp);
2275 
2276 	sc->sc_mgt_timer = AWI_TRANS_TIMEOUT / 1000;
2277 	ifp->if_timer = 1;
2278 }
2279 
2280 static void
awi_recv_asresp(sc,m0)2281 awi_recv_asresp(sc, m0)
2282 	struct awi_softc *sc;
2283 	struct mbuf *m0;
2284 {
2285 	struct ieee80211_frame *wh;
2286 	u_int8_t *asresp, *eframe;
2287 	u_int16_t status;
2288 	u_int8_t rate, *phy_rates;
2289 	struct awi_bss *bp;
2290 	int i, j;
2291 
2292 	wh = mtod(m0, struct ieee80211_frame *);
2293 	asresp = (u_int8_t *)&wh[1];
2294 	eframe = mtod(m0, u_int8_t *) + m0->m_len;
2295 	if (sc->sc_ifp->if_flags & IFF_DEBUG)
2296 		printf("%s: receive assoc resp from %s\n", sc->sc_dev.dv_xname,
2297 		    ether_sprintf(wh->i_addr2));
2298 
2299 	if (!sc->sc_mib_local.Network_Mode)
2300 		return;
2301 
2302 	if (sc->sc_status != AWI_ST_ASSOC)
2303 		return;
2304 	/* capability info */
2305 	asresp += 2;
2306 	/* status */
2307 	status = LE_READ_2(asresp);
2308 	if (status != 0) {
2309 		printf("%s: association failed (reason %d)\n",
2310 		    sc->sc_dev.dv_xname, status);
2311 		for (bp = TAILQ_FIRST(&sc->sc_scan); bp != NULL;
2312 		    bp = TAILQ_NEXT(bp, list)) {
2313 			if (memcmp(bp->esrc, sc->sc_bss.esrc, ETHER_ADDR_LEN)
2314 			    == 0) {
2315 				bp->fails++;
2316 				break;
2317 			}
2318 		}
2319 		return;
2320 	}
2321 	asresp += 2;
2322 	/* association id */
2323 	asresp += 2;
2324 	/* supported rates */
2325 	rate = AWI_RATE_1MBIT;
2326 	for (i = 0; i < asresp[1]; i++) {
2327 		if (AWI_80211_RATE(asresp[2 + i]) <= rate)
2328 			continue;
2329 		phy_rates = sc->sc_mib_phy.aSuprt_Data_Rates;
2330 		for (j = 0; j < phy_rates[1]; j++) {
2331 			if (AWI_80211_RATE(asresp[2 + i]) ==
2332 			    AWI_80211_RATE(phy_rates[2 + j]))
2333 				rate = AWI_80211_RATE(asresp[2 + i]);
2334 		}
2335 	}
2336 	if (sc->sc_ifp->if_flags & IFF_DEBUG) {
2337 		printf("%s: associated with %s ssid ",
2338 		    sc->sc_dev.dv_xname, ether_sprintf(sc->sc_bss.bssid));
2339 		awi_print_essid(sc->sc_bss.essid);
2340 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
2341 			printf(" chanset %d pattern %d\n",
2342 			    sc->sc_bss.chanset, sc->sc_bss.pattern);
2343 		else
2344 			printf(" channel %d\n", sc->sc_bss.chanset);
2345 	}
2346 	sc->sc_tx_rate = rate;
2347 	sc->sc_mgt_timer = 0;
2348 	sc->sc_rx_timer = 10;
2349 	sc->sc_ifp->if_timer = 1;
2350 	sc->sc_status = AWI_ST_RUNNING;
2351 	sc->sc_ifp->if_flags |= IFF_RUNNING;
2352 	awi_drvstate(sc, AWI_DRV_INFASSOC);
2353 	awi_start(sc->sc_ifp);
2354 }
2355 
2356 static int
awi_mib(sc,cmd,mib)2357 awi_mib(sc, cmd, mib)
2358 	struct awi_softc *sc;
2359 	u_int8_t cmd;
2360 	u_int8_t mib;
2361 {
2362 	int error;
2363 	u_int8_t size, *ptr;
2364 
2365 	switch (mib) {
2366 	case AWI_MIB_LOCAL:
2367 		ptr = (u_int8_t *)&sc->sc_mib_local;
2368 		size = sizeof(sc->sc_mib_local);
2369 		break;
2370 	case AWI_MIB_ADDR:
2371 		ptr = (u_int8_t *)&sc->sc_mib_addr;
2372 		size = sizeof(sc->sc_mib_addr);
2373 		break;
2374 	case AWI_MIB_MAC:
2375 		ptr = (u_int8_t *)&sc->sc_mib_mac;
2376 		size = sizeof(sc->sc_mib_mac);
2377 		break;
2378 	case AWI_MIB_STAT:
2379 		ptr = (u_int8_t *)&sc->sc_mib_stat;
2380 		size = sizeof(sc->sc_mib_stat);
2381 		break;
2382 	case AWI_MIB_MGT:
2383 		ptr = (u_int8_t *)&sc->sc_mib_mgt;
2384 		size = sizeof(sc->sc_mib_mgt);
2385 		break;
2386 	case AWI_MIB_PHY:
2387 		ptr = (u_int8_t *)&sc->sc_mib_phy;
2388 		size = sizeof(sc->sc_mib_phy);
2389 		break;
2390 	default:
2391 		return EINVAL;
2392 	}
2393 	if (sc->sc_cmd_inprog) {
2394 		error = awi_cmd_wait(sc);
2395 		if (error) {
2396 			if (error == EWOULDBLOCK)
2397 				printf("awi_mib: cmd %d inprog",
2398 				    sc->sc_cmd_inprog);
2399 			return error;
2400 		}
2401 	}
2402 	sc->sc_cmd_inprog = cmd;
2403 	if (cmd == AWI_CMD_SET_MIB)
2404 		awi_write_bytes(sc, AWI_CMD_PARAMS+AWI_CA_MIB_DATA, ptr, size);
2405 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_MIB_TYPE, mib);
2406 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_MIB_SIZE, size);
2407 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_MIB_INDEX, 0);
2408 	error = awi_cmd(sc, cmd);
2409 	if (error)
2410 		return error;
2411 	if (cmd == AWI_CMD_GET_MIB) {
2412 		awi_read_bytes(sc, AWI_CMD_PARAMS+AWI_CA_MIB_DATA, ptr, size);
2413 #ifdef AWI_DEBUG
2414 		if (awi_verbose) {
2415 			int i;
2416 
2417 			printf("awi_mib: #%d:", mib);
2418 			for (i = 0; i < size; i++)
2419 				printf(" %02x", ptr[i]);
2420 			printf("\n");
2421 		}
2422 #endif
2423 	}
2424 	return 0;
2425 }
2426 
2427 static int
awi_cmd_scan(sc)2428 awi_cmd_scan(sc)
2429 	struct awi_softc *sc;
2430 {
2431 	int error;
2432 	u_int8_t scan_mode;
2433 
2434 	if (sc->sc_active_scan)
2435 		scan_mode = AWI_SCAN_ACTIVE;
2436 	else
2437 		scan_mode = AWI_SCAN_PASSIVE;
2438 	if (sc->sc_mib_mgt.aScan_Mode != scan_mode) {
2439 		sc->sc_mib_mgt.aScan_Mode = scan_mode;
2440 		error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT);
2441 		return error;
2442 	}
2443 
2444 	if (sc->sc_cmd_inprog) {
2445 		error = awi_cmd_wait(sc);
2446 		if (error)
2447 			return error;
2448 	}
2449 	sc->sc_cmd_inprog = AWI_CMD_SCAN;
2450 	awi_write_2(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_DURATION,
2451 	    sc->sc_active_scan ? AWI_ASCAN_DURATION : AWI_PSCAN_DURATION);
2452 	if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
2453 		awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_SET,
2454 		    sc->sc_scan_set);
2455 		awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_PATTERN,
2456 		    sc->sc_scan_cur);
2457 		awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_IDX, 1);
2458 	} else {
2459 		awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_SET,
2460 		    sc->sc_scan_cur);
2461 		awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_PATTERN, 0);
2462 		awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_IDX, 0);
2463 	}
2464 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_SUSP, 0);
2465 	return awi_cmd(sc, AWI_CMD_SCAN);
2466 }
2467 
2468 static int
awi_cmd(sc,cmd)2469 awi_cmd(sc, cmd)
2470 	struct awi_softc *sc;
2471 	u_int8_t cmd;
2472 {
2473 	u_int8_t status;
2474 	int error = 0;
2475 
2476 	sc->sc_cmd_inprog = cmd;
2477 	awi_write_1(sc, AWI_CMD_STATUS, AWI_STAT_IDLE);
2478 	awi_write_1(sc, AWI_CMD, cmd);
2479 	if (sc->sc_status != AWI_ST_INIT)
2480 		return 0;
2481 	error = awi_cmd_wait(sc);
2482 	if (error)
2483 		return error;
2484 	status = awi_read_1(sc, AWI_CMD_STATUS);
2485 	awi_write_1(sc, AWI_CMD, 0);
2486 	switch (status) {
2487 	case AWI_STAT_OK:
2488 		break;
2489 	case AWI_STAT_BADPARM:
2490 		return EINVAL;
2491 	default:
2492 		printf("%s: command %d failed %x\n",
2493 		    sc->sc_dev.dv_xname, cmd, status);
2494 		return ENXIO;
2495 	}
2496 	return 0;
2497 }
2498 
2499 static void
awi_cmd_done(sc)2500 awi_cmd_done(sc)
2501 	struct awi_softc *sc;
2502 {
2503 	u_int8_t cmd, status;
2504 
2505 	status = awi_read_1(sc, AWI_CMD_STATUS);
2506 	if (status == AWI_STAT_IDLE)
2507 		return;		/* stray interrupt */
2508 
2509 	cmd = sc->sc_cmd_inprog;
2510 	sc->sc_cmd_inprog = 0;
2511 	if (sc->sc_status == AWI_ST_INIT) {
2512 		wakeup(sc);
2513 		return;
2514 	}
2515 	awi_write_1(sc, AWI_CMD, 0);
2516 
2517 	if (status != AWI_STAT_OK) {
2518 		printf("%s: command %d failed %x\n",
2519 		    sc->sc_dev.dv_xname, cmd, status);
2520 		return;
2521 	}
2522 	switch (sc->sc_status) {
2523 	case AWI_ST_SCAN:
2524 		if (cmd == AWI_CMD_SET_MIB)
2525 			awi_cmd_scan(sc);	/* retry */
2526 		break;
2527 	case AWI_ST_SETSS:
2528 		awi_try_sync(sc);
2529 		break;
2530 	case AWI_ST_SYNC:
2531 		awi_sync_done(sc);
2532 		break;
2533 	default:
2534 		break;
2535 	}
2536 }
2537 
2538 static int
awi_next_txd(sc,len,framep,ntxdp)2539 awi_next_txd(sc, len, framep, ntxdp)
2540 	struct awi_softc *sc;
2541 	int len;
2542 	u_int32_t *framep, *ntxdp;
2543 {
2544 	u_int32_t txd, ntxd, frame;
2545 
2546 	txd = sc->sc_txnext;
2547 	frame = txd + AWI_TXD_SIZE;
2548 	if (frame + len > sc->sc_txend)
2549 		frame = sc->sc_txbase;
2550 	ntxd = frame + len;
2551 	if (ntxd + AWI_TXD_SIZE > sc->sc_txend)
2552 		ntxd = sc->sc_txbase;
2553 	*framep = frame;
2554 	*ntxdp = ntxd;
2555 	/*
2556 	 * Determine if there are any room in ring buffer.
2557 	 *		--- send wait,  === new data,  +++ conflict (ENOBUFS)
2558 	 *   base........................end
2559 	 *	   done----txd=====ntxd		OK
2560 	 *	 --txd=====done++++ntxd--	full
2561 	 *	 --txd=====ntxd    done--	OK
2562 	 *	 ==ntxd    done----txd===	OK
2563 	 *	 ==done++++ntxd----txd===	full
2564 	 *	 ++ntxd    txd=====done++	full
2565 	 */
2566 	if (txd < ntxd) {
2567 		if (txd < sc->sc_txdone && ntxd + AWI_TXD_SIZE > sc->sc_txdone)
2568 			return ENOBUFS;
2569 	} else {
2570 		if (txd < sc->sc_txdone || ntxd + AWI_TXD_SIZE > sc->sc_txdone)
2571 			return ENOBUFS;
2572 	}
2573 	return 0;
2574 }
2575 
2576 static int
awi_lock(sc)2577 awi_lock(sc)
2578 	struct awi_softc *sc;
2579 {
2580 	int error = 0;
2581 
2582 	if (curproc == NULL) {
2583 		/*
2584 		 * XXX
2585 		 * Though driver ioctl should be called with context,
2586 		 * KAME ipv6 stack calls ioctl in interrupt for now.
2587 		 * We simply abort the request if there are other
2588 		 * ioctl requests in progress.
2589 		 */
2590 		if (sc->sc_busy) {
2591 			return EWOULDBLOCK;
2592 			if (sc->sc_invalid)
2593 				return ENXIO;
2594 		}
2595 		sc->sc_busy = 1;
2596 		sc->sc_cansleep = 0;
2597 		return 0;
2598 	}
2599 	while (sc->sc_busy) {
2600 		if (sc->sc_invalid)
2601 			return ENXIO;
2602 		sc->sc_sleep_cnt++;
2603 		error = tsleep(sc, PWAIT | PCATCH, "awilck", 0);
2604 		sc->sc_sleep_cnt--;
2605 		if (error)
2606 			return error;
2607 	}
2608 	sc->sc_busy = 1;
2609 	sc->sc_cansleep = 1;
2610 	return 0;
2611 }
2612 
2613 static void
awi_unlock(sc)2614 awi_unlock(sc)
2615 	struct awi_softc *sc;
2616 {
2617 	sc->sc_busy = 0;
2618 	sc->sc_cansleep = 0;
2619 	if (sc->sc_sleep_cnt)
2620 		wakeup(sc);
2621 }
2622 
2623 static int
awi_intr_lock(sc)2624 awi_intr_lock(sc)
2625 	struct awi_softc *sc;
2626 {
2627 	u_int8_t status;
2628 	int i, retry;
2629 
2630 	status = 1;
2631 	for (retry = 0; retry < 10; retry++) {
2632 		for (i = 0; i < AWI_LOCKOUT_TIMEOUT*1000/5; i++) {
2633 			status = awi_read_1(sc, AWI_LOCKOUT_HOST);
2634 			if (status == 0)
2635 				break;
2636 			DELAY(5);
2637 		}
2638 		if (status != 0)
2639 			break;
2640 		awi_write_1(sc, AWI_LOCKOUT_MAC, 1);
2641 		status = awi_read_1(sc, AWI_LOCKOUT_HOST);
2642 		if (status == 0)
2643 			break;
2644 		awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
2645 	}
2646 	if (status != 0) {
2647 		printf("%s: failed to lock interrupt\n",
2648 		    sc->sc_dev.dv_xname);
2649 		return ENXIO;
2650 	}
2651 	return 0;
2652 }
2653 
2654 static void
awi_intr_unlock(sc)2655 awi_intr_unlock(sc)
2656 	struct awi_softc *sc;
2657 {
2658 
2659 	awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
2660 }
2661 
2662 static int
awi_cmd_wait(sc)2663 awi_cmd_wait(sc)
2664 	struct awi_softc *sc;
2665 {
2666 	int i, error = 0;
2667 
2668 	i = 0;
2669 	while (sc->sc_cmd_inprog) {
2670 		if (sc->sc_invalid)
2671 			return ENXIO;
2672 		if (awi_read_1(sc, AWI_CMD) != sc->sc_cmd_inprog) {
2673 			printf("%s: failed to access hardware\n",
2674 			    sc->sc_dev.dv_xname);
2675 			sc->sc_invalid = 1;
2676 			return ENXIO;
2677 		}
2678 		if (sc->sc_cansleep) {
2679 			sc->sc_sleep_cnt++;
2680 			error = tsleep(sc, PWAIT, "awicmd",
2681 			    AWI_CMD_TIMEOUT*hz/1000);
2682 			sc->sc_sleep_cnt--;
2683 		} else {
2684 			if (awi_read_1(sc, AWI_CMD_STATUS) != AWI_STAT_IDLE) {
2685 				awi_cmd_done(sc);
2686 				break;
2687 			}
2688 			if (i++ >= AWI_CMD_TIMEOUT*1000/10)
2689 				error = EWOULDBLOCK;
2690 			else
2691 				DELAY(10);
2692 		}
2693 		if (error)
2694 			break;
2695 	}
2696 	return error;
2697 }
2698 
2699 static void
awi_print_essid(essid)2700 awi_print_essid(essid)
2701 	u_int8_t *essid;
2702 {
2703 	int i, len;
2704 	u_int8_t *p;
2705 
2706 	len = essid[1];
2707 	if (len > IEEE80211_NWID_LEN)
2708 		len = IEEE80211_NWID_LEN;	/*XXX*/
2709 	/* determine printable or not */
2710 	for (i = 0, p = essid + 2; i < len; i++, p++) {
2711 		if (*p < ' ' || *p > 0x7e)
2712 			break;
2713 	}
2714 	if (i == len) {
2715 		printf("\"");
2716 		for (i = 0, p = essid + 2; i < len; i++, p++)
2717 			printf("%c", *p);
2718 		printf("\"");
2719 	} else {
2720 		printf("0x");
2721 		for (i = 0, p = essid + 2; i < len; i++, p++)
2722 			printf("%02x", *p);
2723 	}
2724 }
2725 
2726 #ifdef AWI_DEBUG
2727 static void
awi_dump_pkt(sc,m,rssi)2728 awi_dump_pkt(sc, m, rssi)
2729 	struct awi_softc *sc;
2730 	struct mbuf *m;
2731 	int rssi;
2732 {
2733 	struct ieee80211_frame *wh;
2734 	int i, l;
2735 
2736 	wh = mtod(m, struct ieee80211_frame *);
2737 
2738 	if (awi_dump_mask != 0 &&
2739 	    ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK)==IEEE80211_FC1_DIR_NODS) &&
2740 	    ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK)==IEEE80211_FC0_TYPE_MGT)) {
2741 		if ((AWI_DUMP_MASK(wh->i_fc[0]) & awi_dump_mask) != 0)
2742 			return;
2743 	}
2744 	if (awi_dump_mask < 0 &&
2745 	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK)==IEEE80211_FC0_TYPE_DATA)
2746 		return;
2747 
2748 	if (rssi < 0)
2749 		printf("tx: ");
2750 	else
2751 		printf("rx: ");
2752 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
2753 	case IEEE80211_FC1_DIR_NODS:
2754 		printf("NODS %s", ether_sprintf(wh->i_addr2));
2755 		printf("->%s", ether_sprintf(wh->i_addr1));
2756 		printf("(%s)", ether_sprintf(wh->i_addr3));
2757 		break;
2758 	case IEEE80211_FC1_DIR_TODS:
2759 		printf("TODS %s", ether_sprintf(wh->i_addr2));
2760 		printf("->%s", ether_sprintf(wh->i_addr3));
2761 		printf("(%s)", ether_sprintf(wh->i_addr1));
2762 		break;
2763 	case IEEE80211_FC1_DIR_FROMDS:
2764 		printf("FRDS %s", ether_sprintf(wh->i_addr3));
2765 		printf("->%s", ether_sprintf(wh->i_addr1));
2766 		printf("(%s)", ether_sprintf(wh->i_addr2));
2767 		break;
2768 	case IEEE80211_FC1_DIR_DSTODS:
2769 		printf("DSDS %s", ether_sprintf((u_int8_t *)&wh[1]));
2770 		printf("->%s", ether_sprintf(wh->i_addr3));
2771 		printf("(%s", ether_sprintf(wh->i_addr2));
2772 		printf("->%s)", ether_sprintf(wh->i_addr1));
2773 		break;
2774 	}
2775 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
2776 	case IEEE80211_FC0_TYPE_DATA:
2777 		printf(" data");
2778 		break;
2779 	case IEEE80211_FC0_TYPE_MGT:
2780 		switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
2781 		case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2782 			printf(" probe_req");
2783 			break;
2784 		case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2785 			printf(" probe_resp");
2786 			break;
2787 		case IEEE80211_FC0_SUBTYPE_BEACON:
2788 			printf(" beacon");
2789 			break;
2790 		case IEEE80211_FC0_SUBTYPE_AUTH:
2791 			printf(" auth");
2792 			break;
2793 		case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2794 			printf(" assoc_req");
2795 			break;
2796 		case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2797 			printf(" assoc_resp");
2798 			break;
2799 		case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2800 			printf(" reassoc_req");
2801 			break;
2802 		case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2803 			printf(" reassoc_resp");
2804 			break;
2805 		case IEEE80211_FC0_SUBTYPE_DEAUTH:
2806 			printf(" deauth");
2807 			break;
2808 		case IEEE80211_FC0_SUBTYPE_DISASSOC:
2809 			printf(" disassoc");
2810 			break;
2811 		default:
2812 			printf(" mgt#%d",
2813 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
2814 			break;
2815 		}
2816 		break;
2817 	default:
2818 		printf(" type#%d",
2819 		    wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
2820 		break;
2821 	}
2822 	if (wh->i_fc[1] & IEEE80211_FC1_WEP)
2823 		printf(" WEP");
2824 	if (rssi >= 0)
2825 		printf(" +%d", rssi);
2826 	printf("\n");
2827 	if (awi_dump_len > 0) {
2828 		l = m->m_len;
2829 		if (l > awi_dump_len + sizeof(*wh))
2830 			l = awi_dump_len + sizeof(*wh);
2831 		i = sizeof(*wh);
2832 		if (awi_dump_hdr)
2833 			i = 0;
2834 		for (; i < l; i++) {
2835 			if ((i & 1) == 0)
2836 				printf(" ");
2837 			printf("%02x", mtod(m, u_int8_t *)[i]);
2838 		}
2839 		printf("\n");
2840 	}
2841 }
2842 #endif
2843