xref: /netbsd-src/sys/dev/ic/awi.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: awi.c,v 1.69 2005/12/11 12:21:26 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999,2000,2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Bill Sommerfeld
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 /*
39  * Driver for AMD 802.11 firmware.
40  * Uses am79c930 chip driver to talk to firmware running on the am79c930.
41  *
42  * More-or-less a generic ethernet-like if driver, with 802.11 gorp added.
43  */
44 
45 /*
46  * todo:
47  *	- flush tx queue on resynch.
48  *	- clear oactive on "down".
49  *	- rewrite copy-into-mbuf code
50  *	- mgmt state machine gets stuck retransmitting assoc requests.
51  *	- multicast filter.
52  *	- fix device reset so it's more likely to work
53  *	- show status goo through ifmedia.
54  *
55  * more todo:
56  *	- deal with more 802.11 frames.
57  *		- send reassoc request
58  *		- deal with reassoc response
59  *		- send/deal with disassociation
60  *	- deal with "full" access points (no room for me).
61  *	- power save mode
62  *
63  * later:
64  *	- SSID preferences
65  *	- need ioctls for poking at the MIBs
66  *	- implement ad-hoc mode (including bss creation).
67  *	- decide when to do "ad hoc" vs. infrastructure mode (IFF_LINK flags?)
68  *		(focus on inf. mode since that will be needed for ietf)
69  *	- deal with DH vs. FH versions of the card
70  *	- deal with faster cards (2mb/s)
71  *	- ?WEP goo (mmm, rc4) (it looks not particularly useful).
72  *	- ifmedia revision.
73  *	- common 802.11 mibish things.
74  *	- common 802.11 media layer.
75  */
76 
77 /*
78  * Driver for AMD 802.11 PCnetMobile firmware.
79  * Uses am79c930 chip driver to talk to firmware running on the am79c930.
80  *
81  * The initial version of the driver was written by
82  * Bill Sommerfeld <sommerfeld@NetBSD.org>.
83  * Then the driver module completely rewritten to support cards with DS phy
84  * and to support adhoc mode by Atsushi Onoe <onoe@NetBSD.org>
85  */
86 
87 #include <sys/cdefs.h>
88 #ifdef __NetBSD__
89 __KERNEL_RCSID(0, "$NetBSD: awi.c,v 1.69 2005/12/11 12:21:26 christos Exp $");
90 #endif
91 #ifdef __FreeBSD__
92 __FBSDID("$FreeBSD: src/sys/dev/awi/awi.c,v 1.30 2004/01/15 13:30:06 onoe Exp $");
93 #endif
94 
95 #include "opt_inet.h"
96 #ifdef __NetBSD__
97 #include "bpfilter.h"
98 #endif
99 #ifdef __FreeBSD__
100 #define	NBPFILTER	1
101 #endif
102 
103 #include <sys/param.h>
104 #include <sys/systm.h>
105 #include <sys/kernel.h>
106 #include <sys/mbuf.h>
107 #include <sys/malloc.h>
108 #include <sys/proc.h>
109 #include <sys/socket.h>
110 #include <sys/sockio.h>
111 #include <sys/errno.h>
112 #include <sys/endian.h>
113 #ifdef __FreeBSD__
114 #include <sys/bus.h>
115 #endif
116 #ifdef __NetBSD__
117 #include <sys/device.h>
118 #endif
119 
120 #include <net/if.h>
121 #include <net/if_dl.h>
122 #ifdef __NetBSD__
123 #include <net/if_ether.h>
124 #endif
125 #ifdef __FreeBSD__
126 #include <net/ethernet.h>
127 #include <net/if_arp.h>
128 #endif
129 #include <net/if_media.h>
130 #include <net/if_llc.h>
131 
132 #include <net80211/ieee80211_netbsd.h>
133 #include <net80211/ieee80211_var.h>
134 
135 #if NBPFILTER > 0
136 #include <net/bpf.h>
137 #endif
138 
139 #include <machine/cpu.h>
140 #include <machine/bus.h>
141 
142 #ifdef __NetBSD__
143 #include <dev/ic/am79c930reg.h>
144 #include <dev/ic/am79c930var.h>
145 #include <dev/ic/awireg.h>
146 #include <dev/ic/awivar.h>
147 #endif
148 #ifdef __FreeBSD__
149 #include <dev/awi/am79c930reg.h>
150 #include <dev/awi/am79c930var.h>
151 #include <dev/awi/awireg.h>
152 #include <dev/awi/awivar.h>
153 #endif
154 
155 #ifdef __FreeBSD__
156 static void awi_init0(void *);
157 #endif
158 static int  awi_init(struct ifnet *);
159 static void awi_stop(struct ifnet *, int);
160 static void awi_start(struct ifnet *);
161 static void awi_watchdog(struct ifnet *);
162 static int  awi_ioctl(struct ifnet *, u_long, caddr_t);
163 static int  awi_media_change(struct ifnet *);
164 static void awi_media_status(struct ifnet *, struct ifmediareq *);
165 static int  awi_mode_init(struct awi_softc *);
166 static void awi_rx_int(struct awi_softc *);
167 static void awi_tx_int(struct awi_softc *);
168 static struct mbuf *awi_devget(struct awi_softc *, u_int32_t, u_int16_t);
169 static int  awi_hw_init(struct awi_softc *);
170 static int  awi_init_mibs(struct awi_softc *);
171 static int  awi_mib(struct awi_softc *, u_int8_t, u_int8_t, int);
172 static int  awi_cmd(struct awi_softc *, u_int8_t, int);
173 static int  awi_cmd_wait(struct awi_softc *);
174 static void awi_cmd_done(struct awi_softc *);
175 static int  awi_next_txd(struct awi_softc *, int, u_int32_t *, u_int32_t *);
176 static int  awi_lock(struct awi_softc *);
177 static void awi_unlock(struct awi_softc *);
178 static int  awi_intr_lock(struct awi_softc *);
179 static void awi_intr_unlock(struct awi_softc *);
180 static int  awi_newstate(struct ieee80211com *, enum ieee80211_state, int);
181 static void awi_recv_mgmt(struct ieee80211com *, struct mbuf *,
182     struct ieee80211_node *, int, int, u_int32_t);
183 static int  awi_send_mgmt(struct ieee80211com *, struct ieee80211_node *, int,
184     int);
185 static struct mbuf *awi_ether_encap(struct awi_softc *, struct mbuf *);
186 static struct mbuf *awi_ether_modcap(struct awi_softc *, struct mbuf *);
187 
188 /* unaligned little endian access */
189 #define LE_READ_2(p)							\
190 	((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8))
191 #define LE_READ_4(p)							\
192 	((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8) |	\
193 	 (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24))
194 #define LE_WRITE_2(p, v)						\
195 	((((u_int8_t *)(p))[0] = (((u_int32_t)(v)      ) & 0xff)),	\
196 	 (((u_int8_t *)(p))[1] = (((u_int32_t)(v) >>  8) & 0xff)))
197 #define LE_WRITE_4(p, v)						\
198 	((((u_int8_t *)(p))[0] = (((u_int32_t)(v)      ) & 0xff)),	\
199 	 (((u_int8_t *)(p))[1] = (((u_int32_t)(v) >>  8) & 0xff)),	\
200 	 (((u_int8_t *)(p))[2] = (((u_int32_t)(v) >> 16) & 0xff)),	\
201 	 (((u_int8_t *)(p))[3] = (((u_int32_t)(v) >> 24) & 0xff)))
202 
203 struct awi_chanset awi_chanset[] = {
204     /* PHY type        domain            min max def */
205     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_JP,  6, 17,  6 },
206     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_ES,  0, 26,  1 },
207     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_FR,  0, 32,  1 },
208     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_US,  0, 77,  1 },
209     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_CA,  0, 77,  1 },
210     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_EU,  0, 77,  1 },
211     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_JP, 14, 14, 14 },
212     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_ES, 10, 11, 10 },
213     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_FR, 10, 13, 10 },
214     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_US,  1, 11,  3 },
215     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_CA,  1, 11,  3 },
216     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_EU,  1, 13,  3 },
217     { 0, 0 }
218 };
219 
220 #ifdef __FreeBSD__
221 devclass_t awi_devclass;
222 
223 #if __FreeBSD_version < 500043
224 static char *ether_sprintf(u_int8_t *);
225 
226 static char *
227 ether_sprintf(u_int8_t *enaddr)
228 {
229 	static char strbuf[18];
230 
231 	sprintf(strbuf, "%6D", enaddr, ":");
232 	return strbuf;
233 }
234 #endif
235 
236 #define	IFQ_PURGE(ifq)		IF_DRAIN(ifq)
237 #define IF_POLL(ifq, m)		((m) = (ifq)->ifq_head)
238 #define IFQ_POLL(ifq, m)	IF_POLL((ifq), (m))
239 #define IFQ_DEQUEUE(ifq, m)	IF_DEQUEUE((ifq), (m))
240 
241 #endif
242 
243 #ifdef AWI_DEBUG
244 int awi_debug = 0;
245 
246 #define	DPRINTF(X)	if (awi_debug) printf X
247 #define	DPRINTF2(X)	if (awi_debug > 1) printf X
248 #else
249 #define	DPRINTF(X)
250 #define	DPRINTF2(X)
251 #endif
252 
253 int
254 awi_attach(struct awi_softc *sc)
255 {
256 	struct ieee80211com *ic = &sc->sc_ic;
257 	struct ifnet *ifp = &sc->sc_if;
258 	int s, i, error, nrate;
259 	int mword;
260 	enum ieee80211_phymode mode;
261 
262 	s = splnet();
263 	sc->sc_busy = 1;
264 	sc->sc_attached = 0;
265 	sc->sc_substate = AWI_ST_NONE;
266 	if ((error = awi_hw_init(sc)) != 0) {
267 		sc->sc_invalid = 1;
268 		splx(s);
269 		return error;
270 	}
271 	error = awi_init_mibs(sc);
272 	if (error != 0) {
273 		sc->sc_invalid = 1;
274 		splx(s);
275 		return error;
276 	}
277 	ifp->if_softc = sc;
278 	ifp->if_flags =
279 #ifdef IFF_NOTRAILERS
280 	    IFF_NOTRAILERS |
281 #endif
282 	    IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
283 	ifp->if_ioctl = awi_ioctl;
284 	ifp->if_start = awi_start;
285 	ifp->if_watchdog = awi_watchdog;
286 #ifdef __NetBSD__
287 	ifp->if_init = awi_init;
288 	ifp->if_stop = awi_stop;
289 	IFQ_SET_READY(&ifp->if_snd);
290 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
291 #endif
292 #ifdef __FreeBSD__
293 	ifp->if_init = awi_init0;
294 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
295 	if_initname(ifp, device_get_name(sc->sc_dev),
296 	    device_get_unit(sc->sc_dev));
297 #endif
298 
299 	ic->ic_ifp = ifp;
300 	ic->ic_caps = IEEE80211_C_WEP | IEEE80211_C_IBSS | IEEE80211_C_HOSTAP;
301 	if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
302 		ic->ic_phytype = IEEE80211_T_FH;
303 		mode = IEEE80211_MODE_FH;
304 	} else {
305 		ic->ic_phytype = IEEE80211_T_DS;
306 		ic->ic_caps |= IEEE80211_C_AHDEMO;
307 		mode = IEEE80211_MODE_11B;
308 	}
309 	ic->ic_opmode = IEEE80211_M_STA;
310 	nrate = sc->sc_mib_phy.aSuprt_Data_Rates[1];
311 	memcpy(ic->ic_sup_rates[mode].rs_rates,
312 	    sc->sc_mib_phy.aSuprt_Data_Rates + 2, nrate);
313 	ic->ic_sup_rates[mode].rs_nrates = nrate;
314 	IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->sc_mib_addr.aMAC_Address);
315 
316 	printf("%s: IEEE802.11 %s (firmware %s)\n", ifp->if_xname,
317 	    (ic->ic_phytype == IEEE80211_T_FH) ? "FH" : "DS", sc->sc_banner);
318 	printf("%s: 802.11 address: %s\n", ifp->if_xname,
319 	    ether_sprintf(ic->ic_myaddr));
320 
321 #ifdef __NetBSD__
322 	if_attach(ifp);
323 #endif
324 	ieee80211_ifattach(ic);
325 
326 	sc->sc_newstate = ic->ic_newstate;
327 	ic->ic_newstate = awi_newstate;
328 
329 	sc->sc_recv_mgmt = ic->ic_recv_mgmt;
330 	ic->ic_recv_mgmt = awi_recv_mgmt;
331 
332 	sc->sc_send_mgmt = ic->ic_send_mgmt;
333 	ic->ic_send_mgmt = awi_send_mgmt;
334 
335 	ieee80211_media_init(ic, awi_media_change, awi_media_status);
336 
337 	/* Melco compatibility mode. */
338 #define	ADD(s, o)	ifmedia_add(&ic->ic_media, \
339 	IFM_MAKEWORD(IFM_IEEE80211, (s), (o), 0), 0, NULL)
340 	ADD(IFM_AUTO, IFM_FLAG0);
341 
342 	for (i = 0; i < nrate; i++) {
343 		mword = ieee80211_rate2media(ic,
344 		    ic->ic_sup_rates[mode].rs_rates[i], mode);
345 		if (mword == 0)
346 			continue;
347 		ADD(mword, IFM_FLAG0);
348 	}
349 #undef	ADD
350 
351 #ifdef __NetBSD__
352 	if ((sc->sc_sdhook = shutdownhook_establish(awi_shutdown, sc)) == NULL)
353 		printf("%s: WARNING: unable to establish shutdown hook\n",
354 		    ifp->if_xname);
355 	if ((sc->sc_powerhook = powerhook_establish(awi_power, sc)) == NULL)
356 		printf("%s: WARNING: unable to establish power hook\n",
357 		    ifp->if_xname);
358 #endif
359 	sc->sc_attached = 1;
360 	splx(s);
361 
362 	/* ready to accept ioctl */
363 	awi_unlock(sc);
364 
365 	return 0;
366 }
367 
368 int
369 awi_detach(struct awi_softc *sc)
370 {
371 	struct ieee80211com *ic = &sc->sc_ic;
372 	struct ifnet *ifp = &sc->sc_if;
373 	int s;
374 
375 	if (!sc->sc_attached)
376 		return 0;
377 
378 	s = splnet();
379 	sc->sc_invalid = 1;
380 	awi_stop(ifp, 1);
381 
382 	while (sc->sc_sleep_cnt > 0) {
383 		wakeup(sc);
384 		(void)tsleep(sc, PWAIT, "awidet", 1);
385 	}
386 	sc->sc_attached = 0;
387 	ieee80211_ifdetach(ic);
388 #ifdef __NetBSD__
389 	if_detach(ifp);
390 	shutdownhook_disestablish(sc->sc_sdhook);
391 	powerhook_disestablish(sc->sc_powerhook);
392 #endif
393 	splx(s);
394 	return 0;
395 }
396 
397 #ifdef __NetBSD__
398 int
399 awi_activate(struct device *self, enum devact act)
400 {
401 	struct awi_softc *sc = (struct awi_softc *)self;
402 	struct ifnet *ifp = &sc->sc_if;
403 	int s, error = 0;
404 
405 	s = splnet();
406 	switch (act) {
407 	case DVACT_ACTIVATE:
408 		error = EOPNOTSUPP;
409 		break;
410 	case DVACT_DEACTIVATE:
411 		sc->sc_invalid = 1;
412 		if_deactivate(ifp);
413 		break;
414 	}
415 	splx(s);
416 	return error;
417 }
418 
419 void
420 awi_power(int why, void *arg)
421 {
422 	struct awi_softc *sc = arg;
423 	struct ifnet *ifp = &sc->sc_if;
424 	int s;
425 	int ocansleep;
426 
427 	DPRINTF(("awi_power: %d\n", why));
428 	s = splnet();
429 	ocansleep = sc->sc_cansleep;
430 	sc->sc_cansleep = 0;
431 	switch (why) {
432 	case PWR_SUSPEND:
433 	case PWR_STANDBY:
434 		awi_stop(ifp, 1);
435 		break;
436 	case PWR_RESUME:
437 		if (ifp->if_flags & IFF_UP) {
438 			awi_init(ifp);
439 			(void)awi_intr(sc);	/* make sure */
440 		}
441 		break;
442 	case PWR_SOFTSUSPEND:
443 	case PWR_SOFTSTANDBY:
444 	case PWR_SOFTRESUME:
445 		break;
446 	}
447 	sc->sc_cansleep = ocansleep;
448 	splx(s);
449 }
450 #endif /* __NetBSD__ */
451 
452 void
453 awi_shutdown(void *arg)
454 {
455 	struct awi_softc *sc = arg;
456 	struct ifnet *ifp = &sc->sc_if;
457 
458 	if (sc->sc_attached)
459 		awi_stop(ifp, 1);
460 }
461 
462 int
463 awi_intr(void *arg)
464 {
465 	struct awi_softc *sc = arg;
466 	u_int16_t status;
467 	int handled = 0, ocansleep;
468 #ifdef AWI_DEBUG
469 	static const char *intname[] = {
470 	    "CMD", "RX", "TX", "SCAN_CMPLT",
471 	    "CFP_START", "DTIM", "CFP_ENDING", "GROGGY",
472 	    "TXDATA", "TXBCAST", "TXPS", "TXCF",
473 	    "TXMGT", "#13", "RXDATA", "RXMGT"
474 	};
475 #endif
476 
477 	if (!sc->sc_enabled || !sc->sc_enab_intr || sc->sc_invalid) {
478 		DPRINTF(("awi_intr: stray interrupt: "
479 		    "enabled %d enab_intr %d invalid %d\n",
480 		    sc->sc_enabled, sc->sc_enab_intr, sc->sc_invalid));
481 		return 0;
482 	}
483 
484 	am79c930_gcr_setbits(&sc->sc_chip,
485 	    AM79C930_GCR_DISPWDN | AM79C930_GCR_ECINT);
486 	awi_write_1(sc, AWI_DIS_PWRDN, 1);
487 	ocansleep = sc->sc_cansleep;
488 	sc->sc_cansleep = 0;
489 
490 	for (;;) {
491 		if (awi_intr_lock(sc) != 0)
492 			break;
493 		status = awi_read_1(sc, AWI_INTSTAT);
494 		awi_write_1(sc, AWI_INTSTAT, 0);
495 		awi_write_1(sc, AWI_INTSTAT, 0);
496 		status |= awi_read_1(sc, AWI_INTSTAT2) << 8;
497 		awi_write_1(sc, AWI_INTSTAT2, 0);
498 		DELAY(10);
499 		awi_intr_unlock(sc);
500 		if (!sc->sc_cmd_inprog)
501 			status &= ~AWI_INT_CMD;	/* make sure */
502 		if (status == 0)
503 			break;
504 #ifdef AWI_DEBUG
505 		if (awi_debug > 1) {
506 			int i;
507 
508 			printf("awi_intr: status 0x%04x", status);
509 			for (i = 0; i < sizeof(intname)/sizeof(intname[0]);
510 			    i++) {
511 				if (status & (1 << i))
512 					printf(" %s", intname[i]);
513 			}
514 			printf("\n");
515 		}
516 #endif
517 		handled = 1;
518 		if (status & AWI_INT_RX)
519 			awi_rx_int(sc);
520 		if (status & AWI_INT_TX)
521 			awi_tx_int(sc);
522 		if (status & AWI_INT_CMD)
523 			awi_cmd_done(sc);
524 		if (status & AWI_INT_SCAN_CMPLT) {
525 			if (sc->sc_ic.ic_state == IEEE80211_S_SCAN &&
526 			    sc->sc_substate == AWI_ST_NONE)
527 				ieee80211_next_scan(&sc->sc_ic);
528 		}
529 	}
530 	sc->sc_cansleep = ocansleep;
531 	am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_DISPWDN);
532 	awi_write_1(sc, AWI_DIS_PWRDN, 0);
533 	return handled;
534 }
535 
536 #ifdef __FreeBSD__
537 static void
538 awi_init0(void *arg)
539 {
540 	struct awi_softc *sc = arg;
541 
542 	(void)awi_init(&sc->sc_if);
543 }
544 #endif
545 
546 static int
547 awi_init(struct ifnet *ifp)
548 {
549 	struct awi_softc *sc = ifp->if_softc;
550 	struct ieee80211com *ic = &sc->sc_ic;
551 	struct ieee80211_node *ni = ic->ic_bss;
552 	struct ieee80211_rateset *rs;
553 	int error, rate, i;
554 
555 	DPRINTF(("awi_init: enabled=%d\n", sc->sc_enabled));
556 	if (sc->sc_enabled) {
557 		awi_stop(ifp, 0);
558 	} else {
559 		if (sc->sc_enable)
560 			(*sc->sc_enable)(sc);
561 		sc->sc_enabled = 1;
562 		if ((error = awi_hw_init(sc)) != 0) {
563 			if (sc->sc_disable)
564 				(*sc->sc_disable)(sc);
565 			sc->sc_enabled = 0;
566 			return error;
567 		}
568 	}
569 	ic->ic_state = IEEE80211_S_INIT;
570 
571 	ic->ic_flags &= ~IEEE80211_F_IBSSON;
572 	switch (ic->ic_opmode) {
573 	case IEEE80211_M_STA:
574 		sc->sc_mib_local.Network_Mode = 1;
575 		sc->sc_mib_local.Acting_as_AP = 0;
576 		break;
577 	case IEEE80211_M_IBSS:
578 		ic->ic_flags |= IEEE80211_F_IBSSON;
579 		/* FALLTHRU */
580 	case IEEE80211_M_AHDEMO:
581 		sc->sc_mib_local.Network_Mode = 0;
582 		sc->sc_mib_local.Acting_as_AP = 0;
583 		break;
584 	case IEEE80211_M_HOSTAP:
585 		sc->sc_mib_local.Network_Mode = 1;
586 		sc->sc_mib_local.Acting_as_AP = 1;
587 		break;
588 	case IEEE80211_M_MONITOR:
589 		return ENODEV;
590 	}
591 #if 0
592 	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
593 #endif
594 	memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
595 	sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
596 	sc->sc_mib_mac.aDesired_ESS_ID[1] = ic->ic_des_esslen;
597 	memcpy(&sc->sc_mib_mac.aDesired_ESS_ID[2], ic->ic_des_essid,
598 	    ic->ic_des_esslen);
599 
600 	/* configure basic rate */
601 	if (ic->ic_phytype == IEEE80211_T_FH)
602 		rs = &ic->ic_sup_rates[IEEE80211_MODE_FH];
603 	else
604 		rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
605 	if (ic->ic_fixed_rate != -1) {
606 		rate = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
607 	} else {
608 		rate = 0;
609 		for (i = 0; i < rs->rs_nrates; i++) {
610 			if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
611 			    rate < (rs->rs_rates[i] & IEEE80211_RATE_VAL))
612 				rate = rs->rs_rates[i] & IEEE80211_RATE_VAL;
613 		}
614 	}
615 	rate *= 5;
616 	LE_WRITE_2(&sc->sc_mib_mac.aStation_Basic_Rate, rate);
617 
618 	if ((error = awi_mode_init(sc)) != 0) {
619 		DPRINTF(("awi_init: awi_mode_init failed %d\n", error));
620 		awi_stop(ifp, 1);
621 		return error;
622 	}
623 
624 	/* start transmitter */
625 	sc->sc_txdone = sc->sc_txnext = sc->sc_txbase;
626 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_START, 0);
627 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_NEXT, 0);
628 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_LENGTH, 0);
629 	awi_write_1(sc, sc->sc_txbase + AWI_TXD_RATE, 0);
630 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_NDA, 0);
631 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_NRA, 0);
632 	awi_write_1(sc, sc->sc_txbase + AWI_TXD_STATE, 0);
633 	awi_write_4(sc, AWI_CA_TX_DATA, sc->sc_txbase);
634 	awi_write_4(sc, AWI_CA_TX_MGT, 0);
635 	awi_write_4(sc, AWI_CA_TX_BCAST, 0);
636 	awi_write_4(sc, AWI_CA_TX_PS, 0);
637 	awi_write_4(sc, AWI_CA_TX_CF, 0);
638 	if ((error = awi_cmd(sc, AWI_CMD_INIT_TX, AWI_WAIT)) != 0) {
639 		DPRINTF(("awi_init: failed to start transmitter: %d\n", error));
640 		awi_stop(ifp, 1);
641 		return error;
642 	}
643 
644 	/* start receiver */
645 	if ((error = awi_cmd(sc, AWI_CMD_INIT_RX, AWI_WAIT)) != 0) {
646 		DPRINTF(("awi_init: failed to start receiver: %d\n", error));
647 		awi_stop(ifp, 1);
648 		return error;
649 	}
650 	sc->sc_rxdoff = awi_read_4(sc, AWI_CA_IRX_DATA_DESC);
651 	sc->sc_rxmoff = awi_read_4(sc, AWI_CA_IRX_PS_DESC);
652 
653 	ifp->if_flags |= IFF_RUNNING;
654 	ifp->if_flags &= ~IFF_OACTIVE;
655 	ic->ic_state = IEEE80211_S_INIT;
656 
657 	if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
658 	    ic->ic_opmode == IEEE80211_M_HOSTAP) {
659 		ni->ni_chan = ic->ic_ibss_chan;
660 		ni->ni_intval = ic->ic_lintval;
661 		ni->ni_rssi = 0;
662 		ni->ni_rstamp = 0;
663 		memset(&ni->ni_tstamp, 0, sizeof(ni->ni_tstamp));
664 		ni->ni_rates =
665 		    ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
666 		IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr);
667 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
668 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
669 			ni->ni_esslen = ic->ic_des_esslen;
670 			memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
671 			ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
672 			if (ic->ic_phytype == IEEE80211_T_FH) {
673 				ni->ni_fhdwell = 200;   /* XXX */
674 				ni->ni_fhindex = 1;
675 			}
676 		} else {
677 			ni->ni_capinfo = IEEE80211_CAPINFO_IBSS;
678 			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
679 			ni->ni_esslen = 0;
680 		}
681 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
682 			ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
683 		if (ic->ic_opmode != IEEE80211_M_AHDEMO)
684 			ic->ic_flags |= IEEE80211_F_SIBSS;
685 		ic->ic_state = IEEE80211_S_SCAN;	/*XXX*/
686 		sc->sc_substate = AWI_ST_NONE;
687 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
688 	} else {
689 		/* XXX check sc->sc_cur_chan */
690 		ni->ni_chan = &ic->ic_channels[sc->sc_cur_chan];
691 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
692 	}
693 	return 0;
694 }
695 
696 static void
697 awi_stop(struct ifnet *ifp, int disable)
698 {
699 	struct awi_softc *sc = ifp->if_softc;
700 
701 	if (!sc->sc_enabled)
702 		return;
703 
704 	DPRINTF(("awi_stop(%d)\n", disable));
705 
706 	ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1);
707 
708 	if (!sc->sc_invalid) {
709 		if (sc->sc_cmd_inprog)
710 			(void)awi_cmd_wait(sc);
711 		(void)awi_cmd(sc, AWI_CMD_KILL_RX, AWI_WAIT);
712 		sc->sc_cmd_inprog = AWI_CMD_FLUSH_TX;
713 		awi_write_1(sc, AWI_CA_FTX_DATA, 1);
714 		awi_write_1(sc, AWI_CA_FTX_MGT, 0);
715 		awi_write_1(sc, AWI_CA_FTX_BCAST, 0);
716 		awi_write_1(sc, AWI_CA_FTX_PS, 0);
717 		awi_write_1(sc, AWI_CA_FTX_CF, 0);
718 		(void)awi_cmd(sc, AWI_CMD_FLUSH_TX, AWI_WAIT);
719 	}
720 	ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
721 	ifp->if_timer = 0;
722 	sc->sc_tx_timer = sc->sc_rx_timer = 0;
723 	if (sc->sc_rxpend != NULL) {
724 		m_freem(sc->sc_rxpend);
725 		sc->sc_rxpend = NULL;
726 	}
727 	IFQ_PURGE(&ifp->if_snd);
728 
729 	if (disable) {
730 		if (!sc->sc_invalid)
731 			am79c930_gcr_setbits(&sc->sc_chip,
732 			    AM79C930_GCR_CORESET);
733 		if (sc->sc_disable)
734 			(*sc->sc_disable)(sc);
735 		sc->sc_enabled = 0;
736 	}
737 }
738 
739 static void
740 awi_start(struct ifnet *ifp)
741 {
742 	struct awi_softc *sc = ifp->if_softc;
743 	struct ieee80211com *ic = &sc->sc_ic;
744 	struct ether_header *eh;
745 	struct ieee80211_node *ni;
746 	struct ieee80211_frame *wh;
747 	struct mbuf *m, *m0;
748 	int len, dowep;
749 	u_int32_t txd, frame, ntxd;
750 	u_int8_t rate;
751 
752 	if (!sc->sc_enabled || sc->sc_invalid)
753 		return;
754 
755 	for (;;) {
756 		txd = sc->sc_txnext;
757 		IF_POLL(&ic->ic_mgtq, m0);
758 		dowep = 0;
759 		if (m0 != NULL) {
760 			len = m0->m_pkthdr.len;
761 			if (awi_next_txd(sc, len, &frame, &ntxd)) {
762 				ifp->if_flags |= IFF_OACTIVE;
763 				break;
764 			}
765 			IF_DEQUEUE(&ic->ic_mgtq, m0);
766 			ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
767 		} else {
768 			if (ic->ic_state != IEEE80211_S_RUN)
769 				break;
770 			IFQ_POLL(&ifp->if_snd, m0);
771 			if (m0 == NULL)
772 				break;
773 			/*
774 			 * Need to calculate the real length to determine
775 			 * if the transmit buffer has a room for the packet.
776 			 */
777 			len = m0->m_pkthdr.len + sizeof(struct ieee80211_frame);
778 			if (!(ifp->if_flags & IFF_LINK0) && !sc->sc_adhoc_ap)
779 				len += sizeof(struct llc) -
780 				    sizeof(struct ether_header);
781 			if (ic->ic_flags & IEEE80211_F_PRIVACY) {
782 				dowep = 1;
783 				len += IEEE80211_WEP_IVLEN +
784 				    IEEE80211_WEP_KIDLEN + IEEE80211_WEP_CRCLEN;
785 			}
786 			if (awi_next_txd(sc, len, &frame, &ntxd)) {
787 				ifp->if_flags |= IFF_OACTIVE;
788 				break;
789 			}
790 			IFQ_DEQUEUE(&ifp->if_snd, m0);
791 			ifp->if_opackets++;
792 #if NBPFILTER > 0
793 			if (ifp->if_bpf)
794 				bpf_mtap(ifp->if_bpf, m0);
795 #endif
796 			eh = mtod(m0, struct ether_header *);
797 			ni = ieee80211_find_txnode(ic, eh->ether_dhost);
798 			if (ni == NULL) {
799 				ifp->if_oerrors++;
800 				continue;
801 			}
802 			if ((ifp->if_flags & IFF_LINK0) || sc->sc_adhoc_ap)
803 				m0 = awi_ether_encap(sc, m0);
804 			else {
805 				m0 = ieee80211_encap(ic, m0, ni);
806 			}
807 			if (m0 == NULL) {
808 				ieee80211_free_node(ni);
809 				ifp->if_oerrors++;
810 				continue;
811 			}
812 			wh = mtod(m0, struct ieee80211_frame *);
813 			if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
814 			    (ic->ic_opmode == IEEE80211_M_HOSTAP ||
815 			     ic->ic_opmode == IEEE80211_M_IBSS) &&
816 			    sc->sc_adhoc_ap == 0 &&
817 			    (ifp->if_flags & IFF_LINK0) == 0 &&
818 			    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
819 			    IEEE80211_FC0_TYPE_DATA && ni == NULL) {
820 				m_freem(m0);
821 				ieee80211_free_node(ni);
822 				ifp->if_oerrors++;
823 				continue;
824 			}
825 		}
826 #if NBPFILTER > 0
827 		if (ic->ic_rawbpf)
828 			bpf_mtap(ic->ic_rawbpf, m0);
829 #endif
830 		if (dowep) {
831 			if ((ieee80211_crypto_encap(ic, ni, m0)) == NULL) {
832 				m_freem(m0);
833 				ieee80211_free_node(ni);
834 				ifp->if_oerrors++;
835 				continue;
836 			}
837 		}
838 		ieee80211_free_node(ni);
839 #ifdef DIAGNOSTIC
840 		if (m0->m_pkthdr.len != len) {
841 			printf("%s: length %d should be %d\n",
842 			    sc->sc_if.if_xname, m0->m_pkthdr.len, len);
843 			m_freem(m0);
844 			ifp->if_oerrors++;
845 			continue;
846 		}
847 #endif
848 
849 		if ((ifp->if_flags & IFF_DEBUG) && (ifp->if_flags & IFF_LINK2))
850 			ieee80211_dump_pkt(m0->m_data, m0->m_len,
851 			    ic->ic_bss->ni_rates.
852 			        rs_rates[ic->ic_bss->ni_txrate] &
853 			    IEEE80211_RATE_VAL, -1);
854 
855 		for (m = m0, len = 0; m != NULL; m = m->m_next) {
856 			awi_write_bytes(sc, frame + len, mtod(m, u_int8_t *),
857 			    m->m_len);
858 			len += m->m_len;
859 		}
860 		m_freem(m0);
861 		rate = (ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate] &
862 		    IEEE80211_RATE_VAL) * 5;
863 		awi_write_1(sc, ntxd + AWI_TXD_STATE, 0);
864 		awi_write_4(sc, txd + AWI_TXD_START, frame);
865 		awi_write_4(sc, txd + AWI_TXD_NEXT, ntxd);
866 		awi_write_4(sc, txd + AWI_TXD_LENGTH, len);
867 		awi_write_1(sc, txd + AWI_TXD_RATE, rate);
868 		awi_write_4(sc, txd + AWI_TXD_NDA, 0);
869 		awi_write_4(sc, txd + AWI_TXD_NRA, 0);
870 		awi_write_1(sc, txd + AWI_TXD_STATE, AWI_TXD_ST_OWN);
871 		sc->sc_txnext = ntxd;
872 
873 		sc->sc_tx_timer = 5;
874 		ifp->if_timer = 1;
875 	}
876 }
877 
878 static void
879 awi_watchdog(struct ifnet *ifp)
880 {
881 	struct awi_softc *sc = ifp->if_softc;
882 	u_int32_t prevdone;
883 	int ocansleep;
884 
885 	ifp->if_timer = 0;
886 	if (!sc->sc_enabled || sc->sc_invalid)
887 		return;
888 
889 	ocansleep = sc->sc_cansleep;
890 	sc->sc_cansleep = 0;
891 	if (sc->sc_tx_timer) {
892 		if (--sc->sc_tx_timer == 0) {
893 			printf("%s: device timeout\n", ifp->if_xname);
894 			prevdone = sc->sc_txdone;
895 			awi_tx_int(sc);
896 			if (sc->sc_txdone == prevdone) {
897 				ifp->if_oerrors++;
898 				awi_init(ifp);
899 				goto out;
900 			}
901 		}
902 		ifp->if_timer = 1;
903 	}
904 	if (sc->sc_rx_timer) {
905 		if (--sc->sc_rx_timer == 0) {
906 			if (sc->sc_ic.ic_state == IEEE80211_S_RUN) {
907 				ieee80211_new_state(&sc->sc_ic,
908 				    IEEE80211_S_SCAN, -1);
909 				goto out;
910 			}
911 		} else
912 			ifp->if_timer = 1;
913 	}
914 	/* TODO: rate control */
915 	ieee80211_watchdog(&sc->sc_ic);
916   out:
917 	sc->sc_cansleep = ocansleep;
918 }
919 
920 static int
921 awi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
922 {
923 	struct awi_softc *sc = ifp->if_softc;
924 	struct ifreq *ifr = (struct ifreq *)data;
925 	int s, error;
926 
927 	s = splnet();
928 	/* serialize ioctl, since we may sleep */
929 	if ((error = awi_lock(sc)) != 0)
930 		goto cantlock;
931 
932 	switch (cmd) {
933 	case SIOCSIFFLAGS:
934 		if (ifp->if_flags & IFF_UP) {
935 			if (sc->sc_enabled) {
936 				/*
937 				 * To avoid rescanning another access point,
938 				 * do not call awi_init() here.  Instead,
939 				 * only reflect promisc mode settings.
940 				 */
941 				error = awi_mode_init(sc);
942 			} else
943 				error = awi_init(ifp);
944 		} else if (sc->sc_enabled)
945 			awi_stop(ifp, 1);
946 		break;
947 	case SIOCSIFMEDIA:
948 	case SIOCGIFMEDIA:
949 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_ic.ic_media, cmd);
950 		break;
951 	case SIOCADDMULTI:
952 	case SIOCDELMULTI:
953 #ifdef __FreeBSD__
954 		error = ENETRESET;	/* XXX */
955 #else
956 		error = (cmd == SIOCADDMULTI) ?
957 		    ether_addmulti(ifr, &sc->sc_ec) :
958 		    ether_delmulti(ifr, &sc->sc_ec);
959 #endif
960 		if (error == ENETRESET) {
961 			/* do not rescan */
962 			if (ifp->if_flags & IFF_RUNNING)
963 				error = awi_mode_init(sc);
964 			else
965 				error = 0;
966 		}
967 		break;
968 	default:
969 		error = ieee80211_ioctl(&sc->sc_ic, cmd, data);
970 		if (error == ENETRESET) {
971 			if (sc->sc_enabled)
972 				error = awi_init(ifp);
973 			else
974 				error = 0;
975 		}
976 		break;
977 	}
978 	awi_unlock(sc);
979   cantlock:
980 	splx(s);
981 	return error;
982 }
983 
984 /*
985  * Called from ifmedia_ioctl via awi_ioctl with lock obtained.
986  *
987  * TBD factor with ieee80211_media_change
988  */
989 static int
990 awi_media_change(struct ifnet *ifp)
991 {
992 	struct awi_softc *sc = ifp->if_softc;
993 	struct ieee80211com *ic = &sc->sc_ic;
994 	struct ifmedia_entry *ime;
995 	enum ieee80211_opmode newmode;
996 	int i, rate, newadhoc_ap, error = 0;
997 
998 	ime = ic->ic_media.ifm_cur;
999 	if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) {
1000 		i = -1;
1001 	} else {
1002 		struct ieee80211_rateset *rs =
1003 		    &ic->ic_sup_rates[(ic->ic_phytype == IEEE80211_T_FH)
1004 		    ? IEEE80211_MODE_FH : IEEE80211_MODE_11B];
1005 		rate = ieee80211_media2rate(ime->ifm_media);
1006 		if (rate == 0)
1007 			return EINVAL;
1008 		for (i = 0; i < rs->rs_nrates; i++) {
1009 			if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
1010 				break;
1011 		}
1012 		if (i == rs->rs_nrates)
1013 			return EINVAL;
1014 	}
1015 	if (ic->ic_fixed_rate != i) {
1016 		ic->ic_fixed_rate = i;
1017 		error = ENETRESET;
1018 	}
1019 
1020 	/*
1021 	 * combination of mediaopt
1022 	 *
1023 	 * hostap adhoc flag0	opmode  adhoc_ap	comment
1024 	 *   +      -     -	HOSTAP      0		HostAP
1025 	 *   -      +     -	IBSS        0		IBSS
1026 	 *   -      +     +	AHDEMO      0		WaveLAN adhoc
1027 	 *   -      -     +	IBSS        1		Melco old Sta
1028 	 *							also LINK0
1029 	 *   -      -     -	STA         0		Infra Station
1030 	 */
1031 	newadhoc_ap = 0;
1032 	if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
1033 		newmode = IEEE80211_M_HOSTAP;
1034 	else if (ime->ifm_media & IFM_IEEE80211_ADHOC) {
1035 		if (ic->ic_phytype == IEEE80211_T_DS &&
1036 		    (ime->ifm_media & IFM_FLAG0))
1037 			newmode = IEEE80211_M_AHDEMO;
1038 		else
1039 			newmode = IEEE80211_M_IBSS;
1040 	} else if (ime->ifm_media & IFM_FLAG0) {
1041 		newmode = IEEE80211_M_IBSS;
1042 		newadhoc_ap = 1;
1043 	} else
1044 		newmode = IEEE80211_M_STA;
1045 	if (ic->ic_opmode != newmode || sc->sc_adhoc_ap != newadhoc_ap) {
1046 		ic->ic_opmode = newmode;
1047 		sc->sc_adhoc_ap = newadhoc_ap;
1048 		error = ENETRESET;
1049 	}
1050 
1051 	if (error == ENETRESET) {
1052 		if (sc->sc_enabled)
1053 			error = awi_init(ifp);
1054 		else
1055 			error = 0;
1056 	}
1057 	return error;
1058 }
1059 
1060 static void
1061 awi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1062 {
1063 	struct awi_softc *sc = ifp->if_softc;
1064 	struct ieee80211com *ic = &sc->sc_ic;
1065 	int rate;
1066 	enum ieee80211_phymode mode;
1067 
1068 	imr->ifm_status = IFM_AVALID;
1069 	if (ic->ic_state == IEEE80211_S_RUN)
1070 		imr->ifm_status |= IFM_ACTIVE;
1071 	imr->ifm_active = IFM_IEEE80211;
1072 	if (ic->ic_phytype == IEEE80211_T_FH)
1073 		mode = IEEE80211_MODE_FH;
1074 	else
1075 		mode = IEEE80211_MODE_11B;
1076 	if (ic->ic_state == IEEE80211_S_RUN) {
1077 		rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate] &
1078 		    IEEE80211_RATE_VAL;
1079 	} else {
1080 		if (ic->ic_fixed_rate == -1)
1081 			rate = 0;
1082 		else
1083 			rate = ic->ic_sup_rates[mode].
1084 			    rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
1085 	}
1086 	imr->ifm_active |= ieee80211_rate2media(ic, rate, mode);
1087 	switch (ic->ic_opmode) {
1088 	case IEEE80211_M_MONITOR: /* we should never reach here */
1089 		break;
1090 	case IEEE80211_M_STA:
1091 		break;
1092 	case IEEE80211_M_IBSS:
1093 		if (sc->sc_adhoc_ap)
1094 			imr->ifm_active |= IFM_FLAG0;
1095 		else
1096 			imr->ifm_active |= IFM_IEEE80211_ADHOC;
1097 		break;
1098 	case IEEE80211_M_AHDEMO:
1099 		imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1100 		break;
1101 	case IEEE80211_M_HOSTAP:
1102 		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1103 		break;
1104 	}
1105 }
1106 
1107 static int
1108 awi_mode_init(struct awi_softc *sc)
1109 {
1110 	struct ifnet *ifp = &sc->sc_if;
1111 	int n, error;
1112 #ifdef __FreeBSD__
1113 	struct ifmultiaddr *ifma;
1114 #else
1115 	struct ether_multi *enm;
1116 	struct ether_multistep step;
1117 #endif
1118 
1119 	/* reinitialize muticast filter */
1120 	n = 0;
1121 	sc->sc_mib_local.Accept_All_Multicast_Dis = 0;
1122 	if (sc->sc_ic.ic_opmode != IEEE80211_M_HOSTAP &&
1123 	    (ifp->if_flags & IFF_PROMISC)) {
1124 		sc->sc_mib_mac.aPromiscuous_Enable = 1;
1125 		goto set_mib;
1126 	}
1127 	sc->sc_mib_mac.aPromiscuous_Enable = 0;
1128 #ifdef __FreeBSD__
1129 	if (ifp->if_amcount != 0)
1130 		goto set_mib;
1131 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1132 		if (ifma->ifma_addr->sa_family != AF_LINK)
1133 			continue;
1134 		if (n == AWI_GROUP_ADDR_SIZE)
1135 			goto set_mib;
1136 		IEEE80211_ADDR_COPY(sc->sc_mib_addr.aGroup_Addresses[n],
1137 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1138 		n++;
1139 	}
1140 #else
1141 	ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
1142 	while (enm != NULL) {
1143 		if (n == AWI_GROUP_ADDR_SIZE ||
1144 		    !IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi))
1145 			goto set_mib;
1146 		IEEE80211_ADDR_COPY(sc->sc_mib_addr.aGroup_Addresses[n],
1147 		    enm->enm_addrlo);
1148 		n++;
1149 		ETHER_NEXT_MULTI(step, enm);
1150 	}
1151 #endif
1152 	for (; n < AWI_GROUP_ADDR_SIZE; n++)
1153 		memset(sc->sc_mib_addr.aGroup_Addresses[n], 0,
1154 		    IEEE80211_ADDR_LEN);
1155 	sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
1156 
1157   set_mib:
1158 	if (sc->sc_mib_local.Accept_All_Multicast_Dis)
1159 		ifp->if_flags &= ~IFF_ALLMULTI;
1160 	else
1161 		ifp->if_flags |= IFF_ALLMULTI;
1162 	sc->sc_mib_mgt.Wep_Required =
1163 	    (sc->sc_ic.ic_flags & IEEE80211_F_PRIVACY) ? AWI_WEP_ON : AWI_WEP_OFF;
1164 
1165 	if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) ||
1166 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_ADDR, AWI_WAIT)) ||
1167 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MAC, AWI_WAIT)) ||
1168 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT, AWI_WAIT)) ||
1169 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_PHY, AWI_WAIT))) {
1170 		DPRINTF(("awi_mode_init: MIB set failed: %d\n", error));
1171 		return error;
1172 	}
1173 	return 0;
1174 }
1175 
1176 static void
1177 awi_rx_int(struct awi_softc *sc)
1178 {
1179 	struct ieee80211com *ic = &sc->sc_ic;
1180 	struct ifnet *ifp = &sc->sc_if;
1181 	struct ieee80211_frame_min *wh;
1182 	struct ieee80211_node *ni;
1183 	u_int8_t state, rate, rssi;
1184 	u_int16_t len;
1185 	u_int32_t frame, next, rstamp, rxoff;
1186 	struct mbuf *m;
1187 
1188 	rxoff = sc->sc_rxdoff;
1189 	for (;;) {
1190 		state = awi_read_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE);
1191 		if (state & AWI_RXD_ST_OWN)
1192 			break;
1193 		if (!(state & AWI_RXD_ST_CONSUMED)) {
1194 			if (sc->sc_substate != AWI_ST_NONE)
1195 				goto rx_next;
1196 			if (state & AWI_RXD_ST_RXERROR) {
1197 				ifp->if_ierrors++;
1198 				goto rx_next;
1199 			}
1200 			len    = awi_read_2(sc, rxoff + AWI_RXD_LEN);
1201 			rate   = awi_read_1(sc, rxoff + AWI_RXD_RATE);
1202 			rssi   = awi_read_1(sc, rxoff + AWI_RXD_RSSI);
1203 			frame  = awi_read_4(sc, rxoff + AWI_RXD_START_FRAME) &
1204 			    0x7fff;
1205 			rstamp = awi_read_4(sc, rxoff + AWI_RXD_LOCALTIME);
1206 			m = awi_devget(sc, frame, len);
1207 			if (m == NULL) {
1208 				ifp->if_ierrors++;
1209 				goto rx_next;
1210 			}
1211 			if (state & AWI_RXD_ST_LF) {
1212 				/* TODO check my bss */
1213 				if (!(sc->sc_ic.ic_flags & IEEE80211_F_SIBSS) &&
1214 				    sc->sc_ic.ic_state == IEEE80211_S_RUN) {
1215 					sc->sc_rx_timer = 10;
1216 					ifp->if_timer = 1;
1217 				}
1218 				if ((ifp->if_flags & IFF_DEBUG) &&
1219 				    (ifp->if_flags & IFF_LINK2))
1220 					ieee80211_dump_pkt(m->m_data, m->m_len,
1221 					    rate / 5, rssi);
1222 				if ((ifp->if_flags & IFF_LINK0) ||
1223 				    sc->sc_adhoc_ap)
1224 					m = awi_ether_modcap(sc, m);
1225 				else
1226 					m = m_pullup(m, sizeof(*wh));
1227 				if (m == NULL) {
1228 					ifp->if_ierrors++;
1229 					goto rx_next;
1230 				}
1231 				wh = mtod(m, struct ieee80211_frame_min *);
1232 				ni = ieee80211_find_rxnode(ic, wh);
1233 				ieee80211_input(ic, m, ni, rssi, rstamp);
1234 				/*
1235 				 * The frame may have caused the
1236 				 * node to be marked for reclamation
1237 				 * (e.g. in response to a DEAUTH
1238 				 * message) so use release_node here
1239 				 * instead of unref_node.
1240 				 */
1241 				ieee80211_free_node(ni);
1242 			} else
1243 				sc->sc_rxpend = m;
1244   rx_next:
1245 			state |= AWI_RXD_ST_CONSUMED;
1246 			awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1247 		}
1248 		next = awi_read_4(sc, rxoff + AWI_RXD_NEXT);
1249 		if (next & AWI_RXD_NEXT_LAST)
1250 			break;
1251 		/* make sure the next pointer is correct */
1252 		if (next != awi_read_4(sc, rxoff + AWI_RXD_NEXT))
1253 			break;
1254 		state |= AWI_RXD_ST_OWN;
1255 		awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1256 		rxoff = next & 0x7fff;
1257 	}
1258 	sc->sc_rxdoff = rxoff;
1259 }
1260 
1261 static void
1262 awi_tx_int(struct awi_softc *sc)
1263 {
1264 	struct ifnet *ifp = &sc->sc_if;
1265 	u_int8_t flags;
1266 
1267 	while (sc->sc_txdone != sc->sc_txnext) {
1268 		flags = awi_read_1(sc, sc->sc_txdone + AWI_TXD_STATE);
1269 		if ((flags & AWI_TXD_ST_OWN) || !(flags & AWI_TXD_ST_DONE))
1270 			break;
1271 		if (flags & AWI_TXD_ST_ERROR)
1272 			ifp->if_oerrors++;
1273 		sc->sc_txdone = awi_read_4(sc, sc->sc_txdone + AWI_TXD_NEXT) &
1274 		    0x7fff;
1275 	}
1276 	DPRINTF2(("awi_txint: txdone %d txnext %d txbase %d txend %d\n",
1277 	    sc->sc_txdone, sc->sc_txnext, sc->sc_txbase, sc->sc_txend));
1278 	sc->sc_tx_timer = 0;
1279 	ifp->if_flags &= ~IFF_OACTIVE;
1280 	awi_start(ifp);
1281 }
1282 
1283 static struct mbuf *
1284 awi_devget(struct awi_softc *sc, u_int32_t off, u_int16_t len)
1285 {
1286 	struct ifnet *ifp = &sc->sc_if;
1287 	struct mbuf *m;
1288 	struct mbuf *top, **mp;
1289 	u_int tlen;
1290 
1291 	top = sc->sc_rxpend;
1292 	mp = &top;
1293 	if (top != NULL) {
1294 		sc->sc_rxpend = NULL;
1295 		top->m_pkthdr.len += len;
1296 		m = top;
1297 		while (*mp != NULL) {
1298 			m = *mp;
1299 			mp = &m->m_next;
1300 		}
1301 		if (m->m_flags & M_EXT)
1302 			tlen = m->m_ext.ext_size;
1303 		else if (m->m_flags & M_PKTHDR)
1304 			tlen = MHLEN;
1305 		else
1306 			tlen = MLEN;
1307 		tlen -= m->m_len;
1308 		if (tlen > len)
1309 			tlen = len;
1310 		awi_read_bytes(sc, off, mtod(m, u_int8_t *) + m->m_len, tlen);
1311 		off += tlen;
1312 		len -= tlen;
1313 	}
1314 
1315 	while (len > 0) {
1316 		if (top == NULL) {
1317 			MGETHDR(m, M_DONTWAIT, MT_DATA);
1318 			if (m == NULL)
1319 				return NULL;
1320 			m->m_pkthdr.rcvif = ifp;
1321 			m->m_pkthdr.len = len;
1322 			m->m_len = MHLEN;
1323 			m->m_flags |= M_HASFCS;
1324 		} else {
1325 			MGET(m, M_DONTWAIT, MT_DATA);
1326 			if (m == NULL) {
1327 				m_freem(top);
1328 				return NULL;
1329 			}
1330 			m->m_len = MLEN;
1331 		}
1332 		if (len >= MINCLSIZE) {
1333 			MCLGET(m, M_DONTWAIT);
1334 			if (m->m_flags & M_EXT)
1335 				m->m_len = m->m_ext.ext_size;
1336 		}
1337 		if (top == NULL) {
1338 			int hdrlen = sizeof(struct ieee80211_frame) +
1339 			    sizeof(struct llc);
1340 			caddr_t newdata = (caddr_t)
1341 			    ALIGN(m->m_data + hdrlen) - hdrlen;
1342 			m->m_len -= newdata - m->m_data;
1343 			m->m_data = newdata;
1344 		}
1345 		if (m->m_len > len)
1346 			m->m_len = len;
1347 		awi_read_bytes(sc, off, mtod(m, u_int8_t *), m->m_len);
1348 		off += m->m_len;
1349 		len -= m->m_len;
1350 		*mp = m;
1351 		mp = &m->m_next;
1352 	}
1353 	return top;
1354 }
1355 
1356 /*
1357  * Initialize hardware and start firmware to accept commands.
1358  * Called everytime after power on firmware.
1359  */
1360 
1361 static int
1362 awi_hw_init(struct awi_softc *sc)
1363 {
1364 	u_int8_t status;
1365 	u_int16_t intmask;
1366 	int i, error;
1367 
1368 	sc->sc_enab_intr = 0;
1369 	sc->sc_invalid = 0;	/* XXX: really? */
1370 	awi_drvstate(sc, AWI_DRV_RESET);
1371 
1372 	/* reset firmware */
1373 	am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1374 	DELAY(100);
1375 	awi_write_1(sc, AWI_SELFTEST, 0);
1376 	awi_write_1(sc, AWI_CMD, 0);
1377 	awi_write_1(sc, AWI_BANNER, 0);
1378 	am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1379 	DELAY(100);
1380 
1381 	/* wait for selftest completion */
1382 	for (i = 0; ; i++) {
1383 		if (sc->sc_invalid)
1384 			return ENXIO;
1385 		if (i >= AWI_SELFTEST_TIMEOUT*hz/1000) {
1386 			printf("%s: failed to complete selftest (timeout)\n",
1387 			    sc->sc_if.if_xname);
1388 			return ENXIO;
1389 		}
1390 		status = awi_read_1(sc, AWI_SELFTEST);
1391 		if ((status & 0xf0) == 0xf0)
1392 			break;
1393 		if (sc->sc_cansleep) {
1394 			sc->sc_sleep_cnt++;
1395 			(void)tsleep(sc, PWAIT, "awitst", 1);
1396 			sc->sc_sleep_cnt--;
1397 		} else {
1398 			DELAY(1000*1000/hz);
1399 		}
1400 	}
1401 	if (status != AWI_SELFTEST_PASSED) {
1402 		printf("%s: failed to complete selftest (code %x)\n",
1403 		    sc->sc_if.if_xname, status);
1404 		return ENXIO;
1405 	}
1406 
1407 	/* check banner to confirm firmware write it */
1408 	awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
1409 	if (memcmp(sc->sc_banner, "PCnetMobile:", 12) != 0) {
1410 		printf("%s: failed to complete selftest (bad banner)\n",
1411 		    sc->sc_if.if_xname);
1412 		for (i = 0; i < AWI_BANNER_LEN; i++)
1413 			printf("%s%02x", i ? ":" : "\t", sc->sc_banner[i]);
1414 		printf("\n");
1415 		return ENXIO;
1416 	}
1417 
1418 	/* initializing interrupt */
1419 	sc->sc_enab_intr = 1;
1420 	error = awi_intr_lock(sc);
1421 	if (error)
1422 		return error;
1423 	intmask = AWI_INT_GROGGY | AWI_INT_SCAN_CMPLT |
1424 	    AWI_INT_TX | AWI_INT_RX | AWI_INT_CMD;
1425 	awi_write_1(sc, AWI_INTMASK, ~intmask & 0xff);
1426 	awi_write_1(sc, AWI_INTMASK2, 0);
1427 	awi_write_1(sc, AWI_INTSTAT, 0);
1428 	awi_write_1(sc, AWI_INTSTAT2, 0);
1429 	awi_intr_unlock(sc);
1430 	am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_ENECINT);
1431 
1432 	/* issuing interface test command */
1433 	error = awi_cmd(sc, AWI_CMD_NOP, AWI_WAIT);
1434 	if (error) {
1435 		printf("%s: failed to complete selftest",
1436 		    sc->sc_if.if_xname);
1437 		if (error == ENXIO)
1438 			printf(" (no hardware)\n");
1439 		else if (error != EWOULDBLOCK)
1440 			printf(" (error %d)\n", error);
1441 		else if (sc->sc_cansleep)
1442 			printf(" (lost interrupt)\n");
1443 		else
1444 			printf(" (command timeout)\n");
1445 		return error;
1446 	}
1447 
1448 	/* Initialize VBM */
1449 	awi_write_1(sc, AWI_VBM_OFFSET, 0);
1450 	awi_write_1(sc, AWI_VBM_LENGTH, 1);
1451 	awi_write_1(sc, AWI_VBM_BITMAP, 0);
1452 	return 0;
1453 }
1454 
1455 /*
1456  * Extract the factory default MIB value from firmware and assign the driver
1457  * default value.
1458  * Called once at attaching the interface.
1459  */
1460 
1461 static int
1462 awi_init_mibs(struct awi_softc *sc)
1463 {
1464 	int chan, i, error;
1465 	struct ieee80211com *ic = &sc->sc_ic;
1466 	struct awi_chanset *cs;
1467 
1468 	if ((error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) ||
1469 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_ADDR, AWI_WAIT)) ||
1470 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MAC, AWI_WAIT)) ||
1471 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MGT, AWI_WAIT)) ||
1472 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_PHY, AWI_WAIT))) {
1473 		printf("%s: failed to get default mib value (error %d)\n",
1474 		    sc->sc_if.if_xname, error);
1475 		return error;
1476 	}
1477 
1478 	memset(&sc->sc_ic.ic_chan_avail, 0, sizeof(sc->sc_ic.ic_chan_avail));
1479 	for (cs = awi_chanset; ; cs++) {
1480 		if (cs->cs_type == 0) {
1481 			printf("%s: failed to set available channel\n",
1482 			    sc->sc_if.if_xname);
1483 			return ENXIO;
1484 		}
1485 		if (cs->cs_type == sc->sc_mib_phy.IEEE_PHY_Type &&
1486 		    cs->cs_region == sc->sc_mib_phy.aCurrent_Reg_Domain)
1487 			break;
1488 	}
1489 	if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1490 		for (i = cs->cs_min; i <= cs->cs_max; i++) {
1491 			chan = IEEE80211_FH_CHAN(i % 3 + 1, i);
1492 			setbit(sc->sc_ic.ic_chan_avail, chan);
1493 			/* XXX for FHSS, does frequency matter? */
1494 			ic->ic_channels[chan].ic_freq = 0;
1495 			ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_FHSS;
1496 			/*
1497 			 * According to the IEEE 802.11 specification,
1498 			 * hop pattern parameter for FH phy should be
1499 			 * incremented by 3 for given hop chanset, i.e.,
1500 			 * the chanset parameter is calculated for given
1501 			 * hop patter.  However, BayStack 650 Access Points
1502 			 * apparently use fixed hop chanset parameter value
1503 			 * 1 for any hop pattern.  So we also try this
1504 			 * combination of hop chanset and pattern.
1505 			 */
1506 			chan = IEEE80211_FH_CHAN(1, i);
1507 			setbit(sc->sc_ic.ic_chan_avail, chan);
1508 			ic->ic_channels[chan].ic_freq = 0; /* XXX */
1509 			ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_FHSS;
1510 		}
1511 	} else {
1512 		for (i = cs->cs_min; i <= cs->cs_max; i++) {
1513 			setbit(sc->sc_ic.ic_chan_avail, i);
1514 			ic->ic_channels[i].ic_freq =
1515 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
1516 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
1517 		}
1518 	}
1519 	sc->sc_cur_chan = cs->cs_def;
1520 	ic->ic_ibss_chan = &ic->ic_channels[cs->cs_def];
1521 
1522 	sc->sc_mib_local.Fragmentation_Dis = 1;
1523 	sc->sc_mib_local.Add_PLCP_Dis = 0;
1524 	sc->sc_mib_local.MAC_Hdr_Prsv = 0;
1525 	sc->sc_mib_local.Rx_Mgmt_Que_En = 0;
1526 	sc->sc_mib_local.Re_Assembly_Dis = 1;
1527 	sc->sc_mib_local.Strip_PLCP_Dis = 0;
1528 	sc->sc_mib_local.Power_Saving_Mode_Dis = 1;
1529 	sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
1530 	sc->sc_mib_local.Check_Seq_Cntl_Dis = 0;
1531 	sc->sc_mib_local.Flush_CFP_Queue_On_CF_End = 0;
1532 	sc->sc_mib_local.Network_Mode = 1;
1533 	sc->sc_mib_local.PWD_Lvl = 0;
1534 	sc->sc_mib_local.CFP_Mode = 0;
1535 
1536 	/* allocate buffers */
1537 	sc->sc_txbase = AWI_BUFFERS;
1538 	sc->sc_txend = sc->sc_txbase +
1539 	    (AWI_TXD_SIZE + sizeof(struct ieee80211_frame) +
1540 	    sizeof(struct ether_header) + ETHERMTU) * AWI_NTXBUFS;
1541 	LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Offset, sc->sc_txbase);
1542 	LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Size,
1543 	    sc->sc_txend - sc->sc_txbase);
1544 	LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Offset, sc->sc_txend);
1545 	LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Size,
1546 	    AWI_BUFFERS_END - sc->sc_txend);
1547 	sc->sc_mib_local.Acting_as_AP = 0;
1548 	sc->sc_mib_local.Fill_CFP = 0;
1549 
1550 	memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
1551 	sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
1552 
1553 	sc->sc_mib_mgt.aPower_Mgt_Mode = 0;
1554 	sc->sc_mib_mgt.aDTIM_Period = 1;
1555 	LE_WRITE_2(&sc->sc_mib_mgt.aATIM_Window, 0);
1556 	return 0;
1557 }
1558 
1559 static int
1560 awi_mib(struct awi_softc *sc, u_int8_t cmd, u_int8_t mib, int wflag)
1561 {
1562 	int error;
1563 	u_int8_t size, *ptr;
1564 
1565 	switch (mib) {
1566 	case AWI_MIB_LOCAL:
1567 		ptr = (u_int8_t *)&sc->sc_mib_local;
1568 		size = sizeof(sc->sc_mib_local);
1569 		break;
1570 	case AWI_MIB_ADDR:
1571 		ptr = (u_int8_t *)&sc->sc_mib_addr;
1572 		size = sizeof(sc->sc_mib_addr);
1573 		break;
1574 	case AWI_MIB_MAC:
1575 		ptr = (u_int8_t *)&sc->sc_mib_mac;
1576 		size = sizeof(sc->sc_mib_mac);
1577 		break;
1578 	case AWI_MIB_STAT:
1579 		ptr = (u_int8_t *)&sc->sc_mib_stat;
1580 		size = sizeof(sc->sc_mib_stat);
1581 		break;
1582 	case AWI_MIB_MGT:
1583 		ptr = (u_int8_t *)&sc->sc_mib_mgt;
1584 		size = sizeof(sc->sc_mib_mgt);
1585 		break;
1586 	case AWI_MIB_PHY:
1587 		ptr = (u_int8_t *)&sc->sc_mib_phy;
1588 		size = sizeof(sc->sc_mib_phy);
1589 		break;
1590 	default:
1591 		return EINVAL;
1592 	}
1593 	if (sc->sc_cmd_inprog) {
1594 		if ((error = awi_cmd_wait(sc)) != 0) {
1595 			if (error == EWOULDBLOCK)
1596 				DPRINTF(("awi_mib: cmd %d inprog",
1597 				    sc->sc_cmd_inprog));
1598 			return error;
1599 		}
1600 	}
1601 	sc->sc_cmd_inprog = cmd;
1602 	if (cmd == AWI_CMD_SET_MIB)
1603 		awi_write_bytes(sc, AWI_CA_MIB_DATA, ptr, size);
1604 	awi_write_1(sc, AWI_CA_MIB_TYPE, mib);
1605 	awi_write_1(sc, AWI_CA_MIB_SIZE, size);
1606 	awi_write_1(sc, AWI_CA_MIB_INDEX, 0);
1607 	if ((error = awi_cmd(sc, cmd, wflag)) != 0)
1608 		return error;
1609 	if (cmd == AWI_CMD_GET_MIB) {
1610 		awi_read_bytes(sc, AWI_CA_MIB_DATA, ptr, size);
1611 #ifdef AWI_DEBUG
1612 		if (awi_debug) {
1613 			int i;
1614 
1615 			printf("awi_mib: #%d:", mib);
1616 			for (i = 0; i < size; i++)
1617 				printf(" %02x", ptr[i]);
1618 			printf("\n");
1619 		}
1620 #endif
1621 	}
1622 	return 0;
1623 }
1624 
1625 static int
1626 awi_cmd(struct awi_softc *sc, u_int8_t cmd, int wflag)
1627 {
1628 	u_int8_t status;
1629 	int error = 0;
1630 #ifdef AWI_DEBUG
1631 	static const char *cmdname[] = {
1632 	    "IDLE", "NOP", "SET_MIB", "INIT_TX", "FLUSH_TX", "INIT_RX",
1633 	    "KILL_RX", "SLEEP", "WAKE", "GET_MIB", "SCAN", "SYNC", "RESUME"
1634 	};
1635 #endif
1636 
1637 #ifdef AWI_DEBUG
1638 	if (awi_debug > 1) {
1639 		if (cmd >= sizeof(cmdname)/sizeof(cmdname[0]))
1640 			printf("awi_cmd: #%d", cmd);
1641 		else
1642 			printf("awi_cmd: %s", cmdname[cmd]);
1643 		printf(" %s\n", wflag == AWI_NOWAIT ? "nowait" : "wait");
1644 	}
1645 #endif
1646 	sc->sc_cmd_inprog = cmd;
1647 	awi_write_1(sc, AWI_CMD_STATUS, AWI_STAT_IDLE);
1648 	awi_write_1(sc, AWI_CMD, cmd);
1649 	if (wflag == AWI_NOWAIT)
1650 		return EINPROGRESS;
1651 	if ((error = awi_cmd_wait(sc)) != 0)
1652 		return error;
1653 	status = awi_read_1(sc, AWI_CMD_STATUS);
1654 	awi_write_1(sc, AWI_CMD, 0);
1655 	switch (status) {
1656 	case AWI_STAT_OK:
1657 		break;
1658 	case AWI_STAT_BADPARM:
1659 		return EINVAL;
1660 	default:
1661 		printf("%s: command %d failed %x\n",
1662 		    sc->sc_if.if_xname, cmd, status);
1663 		return ENXIO;
1664 	}
1665 	return 0;
1666 }
1667 
1668 static int
1669 awi_cmd_wait(struct awi_softc *sc)
1670 {
1671 	int i, error = 0;
1672 
1673 	i = 0;
1674 	while (sc->sc_cmd_inprog) {
1675 		if (sc->sc_invalid)
1676 			return ENXIO;
1677 		if (awi_read_1(sc, AWI_CMD) != sc->sc_cmd_inprog) {
1678 			printf("%s: failed to access hardware\n",
1679 			    sc->sc_if.if_xname);
1680 			sc->sc_invalid = 1;
1681 			return ENXIO;
1682 		}
1683 		if (sc->sc_cansleep) {
1684 			sc->sc_sleep_cnt++;
1685 			error = tsleep(sc, PWAIT, "awicmd",
1686 			    AWI_CMD_TIMEOUT*hz/1000);
1687 			sc->sc_sleep_cnt--;
1688 		} else {
1689 			if (awi_read_1(sc, AWI_CMD_STATUS) != AWI_STAT_IDLE) {
1690 				awi_cmd_done(sc);
1691 				break;
1692 			}
1693 			if (i++ >= AWI_CMD_TIMEOUT*1000/10)
1694 				error = EWOULDBLOCK;
1695 			else
1696 				DELAY(10);
1697 		}
1698 		if (error)
1699 			break;
1700 	}
1701 	if (error) {
1702 		DPRINTF(("awi_cmd_wait: cmd 0x%x, error %d\n",
1703 		    sc->sc_cmd_inprog, error));
1704 	}
1705 	return error;
1706 }
1707 
1708 static void
1709 awi_cmd_done(struct awi_softc *sc)
1710 {
1711 	u_int8_t cmd, status;
1712 
1713 	status = awi_read_1(sc, AWI_CMD_STATUS);
1714 	if (status == AWI_STAT_IDLE)
1715 		return;		/* stray interrupt */
1716 
1717 	cmd = sc->sc_cmd_inprog;
1718 	sc->sc_cmd_inprog = 0;
1719 	wakeup(sc);
1720 	awi_write_1(sc, AWI_CMD, 0);
1721 
1722 	if (status != AWI_STAT_OK) {
1723 		printf("%s: command %d failed %x\n",
1724 		    sc->sc_if.if_xname, cmd, status);
1725 		sc->sc_substate = AWI_ST_NONE;
1726 		return;
1727 	}
1728 	if (sc->sc_substate != AWI_ST_NONE)
1729 		(void)ieee80211_new_state(&sc->sc_ic, sc->sc_nstate, -1);
1730 }
1731 
1732 static int
1733 awi_next_txd(struct awi_softc *sc, int len, u_int32_t *framep, u_int32_t *ntxdp)
1734 {
1735 	u_int32_t txd, ntxd, frame;
1736 
1737 	txd = sc->sc_txnext;
1738 	frame = txd + AWI_TXD_SIZE;
1739 	if (frame + len > sc->sc_txend)
1740 		frame = sc->sc_txbase;
1741 	ntxd = frame + len;
1742 	if (ntxd + AWI_TXD_SIZE > sc->sc_txend)
1743 		ntxd = sc->sc_txbase;
1744 	*framep = frame;
1745 	*ntxdp = ntxd;
1746 	/*
1747 	 * Determine if there are any room in ring buffer.
1748 	 *		--- send wait,  === new data,  +++ conflict (ENOBUFS)
1749 	 *   base........................end
1750 	 *	   done----txd=====ntxd		OK
1751 	 *	 --txd=====done++++ntxd--	full
1752 	 *	 --txd=====ntxd    done--	OK
1753 	 *	 ==ntxd    done----txd===	OK
1754 	 *	 ==done++++ntxd----txd===	full
1755 	 *	 ++ntxd    txd=====done++	full
1756 	 */
1757 	if (txd < ntxd) {
1758 		if (txd < sc->sc_txdone && ntxd + AWI_TXD_SIZE > sc->sc_txdone)
1759 			return ENOBUFS;
1760 	} else {
1761 		if (txd < sc->sc_txdone || ntxd + AWI_TXD_SIZE > sc->sc_txdone)
1762 			return ENOBUFS;
1763 	}
1764 	return 0;
1765 }
1766 
1767 static int
1768 awi_lock(struct awi_softc *sc)
1769 {
1770 	int error = 0;
1771 
1772 #ifdef __NetBSD__
1773 	if (curlwp == NULL)
1774 #else
1775 	if (curproc == NULL)
1776 #endif
1777 	{
1778 		/*
1779 		 * XXX
1780 		 * Though driver ioctl should be called with context,
1781 		 * KAME ipv6 stack calls ioctl in interrupt for now.
1782 		 * We simply abort the request if there are other
1783 		 * ioctl requests in progress.
1784 		 */
1785 		if (sc->sc_busy) {
1786 			if (sc->sc_invalid)
1787 				return ENXIO;
1788 			return EWOULDBLOCK;
1789 		}
1790 		sc->sc_busy = 1;
1791 		sc->sc_cansleep = 0;
1792 		return 0;
1793 	}
1794 	while (sc->sc_busy) {
1795 		if (sc->sc_invalid)
1796 			return ENXIO;
1797 		sc->sc_sleep_cnt++;
1798 		error = tsleep(sc, PWAIT | PCATCH, "awilck", 0);
1799 		sc->sc_sleep_cnt--;
1800 		if (error)
1801 			return error;
1802 	}
1803 	sc->sc_busy = 1;
1804 	sc->sc_cansleep = 1;
1805 	return 0;
1806 }
1807 
1808 static void
1809 awi_unlock(struct awi_softc *sc)
1810 {
1811 	sc->sc_busy = 0;
1812 	sc->sc_cansleep = 0;
1813 	if (sc->sc_sleep_cnt)
1814 		wakeup(sc);
1815 }
1816 
1817 static int
1818 awi_intr_lock(struct awi_softc *sc)
1819 {
1820 	u_int8_t status;
1821 	int i, retry;
1822 
1823 	status = 1;
1824 	for (retry = 0; retry < 10; retry++) {
1825 		for (i = 0; i < AWI_LOCKOUT_TIMEOUT*1000/5; i++) {
1826 			if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0)
1827 				break;
1828 			DELAY(5);
1829 		}
1830 		if (status != 0)
1831 			break;
1832 		awi_write_1(sc, AWI_LOCKOUT_MAC, 1);
1833 		if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0)
1834 			break;
1835 		awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
1836 	}
1837 	if (status != 0) {
1838 		printf("%s: failed to lock interrupt\n",
1839 		    sc->sc_if.if_xname);
1840 		return ENXIO;
1841 	}
1842 	return 0;
1843 }
1844 
1845 static void
1846 awi_intr_unlock(struct awi_softc *sc)
1847 {
1848 
1849 	awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
1850 }
1851 
1852 static int
1853 awi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1854 {
1855 	struct ifnet *ifp = ic->ic_ifp;
1856 	struct awi_softc *sc = ifp->if_softc;
1857 	struct ieee80211_node *ni;
1858 	int error;
1859 	u_int8_t newmode;
1860 	enum ieee80211_state ostate;
1861 #ifdef AWI_DEBUG
1862 	static const char *stname[] =
1863 	    { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" };
1864 	static const char *substname[] =
1865 	    { "NONE", "SCAN_INIT", "SCAN_SETMIB", "SCAN_SCCMD",
1866 	      "SUB_INIT", "SUB_SETSS", "SUB_SYNC" };
1867 #endif /* AWI_DEBUG */
1868 
1869 	ostate = ic->ic_state;
1870 	DPRINTF(("awi_newstate: %s (%s/%s) -> %s\n", stname[ostate],
1871 	    stname[sc->sc_nstate], substname[sc->sc_substate], stname[nstate]));
1872 
1873 	/* set LED */
1874 	switch (nstate) {
1875 	case IEEE80211_S_INIT:
1876 		awi_drvstate(sc, AWI_DRV_RESET);
1877 		break;
1878 	case IEEE80211_S_SCAN:
1879 		if (ic->ic_opmode == IEEE80211_M_IBSS ||
1880 		    ic->ic_opmode == IEEE80211_M_AHDEMO)
1881 			awi_drvstate(sc, AWI_DRV_ADHSC);
1882 		else
1883 			awi_drvstate(sc, AWI_DRV_INFSY);
1884 		break;
1885 	case IEEE80211_S_AUTH:
1886 		awi_drvstate(sc, AWI_DRV_INFSY);
1887 		break;
1888 	case IEEE80211_S_ASSOC:
1889 		awi_drvstate(sc, AWI_DRV_INFAUTH);
1890 		break;
1891 	case IEEE80211_S_RUN:
1892 		if (ic->ic_opmode == IEEE80211_M_IBSS ||
1893 		    ic->ic_opmode == IEEE80211_M_AHDEMO)
1894 			awi_drvstate(sc, AWI_DRV_ADHSY);
1895 		else
1896 			awi_drvstate(sc, AWI_DRV_INFASSOC);
1897 		break;
1898 	}
1899 
1900 	if (nstate == IEEE80211_S_INIT) {
1901 		sc->sc_substate = AWI_ST_NONE;
1902 		ic->ic_flags &= ~IEEE80211_F_SIBSS;
1903 		return (*sc->sc_newstate)(ic, nstate, arg);
1904 	}
1905 
1906 	/* state transition */
1907 	if (nstate == IEEE80211_S_SCAN) {
1908 		/* SCAN substate */
1909 		if (sc->sc_substate == AWI_ST_NONE) {
1910 			sc->sc_nstate = nstate;	/* next state in transition */
1911 			sc->sc_substate = AWI_ST_SCAN_INIT;
1912 		}
1913 		switch (sc->sc_substate) {
1914 		case AWI_ST_SCAN_INIT:
1915 			sc->sc_substate = AWI_ST_SCAN_SETMIB;
1916 			switch (ostate) {
1917 			case IEEE80211_S_RUN:
1918 				/* beacon miss */
1919 				if (ifp->if_flags & IFF_DEBUG)
1920 					printf("%s: no recent beacons from %s;"
1921 					    " rescanning\n",
1922 					    ifp->if_xname,
1923 					    ether_sprintf(ic->ic_bss->ni_bssid));
1924 				/* FALLTHRU */
1925 			case IEEE80211_S_AUTH:
1926 			case IEEE80211_S_ASSOC:
1927 			case IEEE80211_S_INIT:
1928 				ieee80211_begin_scan(ic, 1);
1929 				/* FALLTHRU */
1930 			case IEEE80211_S_SCAN:
1931 				/* scan next */
1932 				break;
1933 			}
1934 			if (ic->ic_flags & IEEE80211_F_ASCAN)
1935 				newmode = AWI_SCAN_ACTIVE;
1936 			else
1937 				newmode = AWI_SCAN_PASSIVE;
1938 			if (sc->sc_mib_mgt.aScan_Mode != newmode) {
1939 				sc->sc_mib_mgt.aScan_Mode = newmode;
1940 				if ((error = awi_mib(sc, AWI_CMD_SET_MIB,
1941 				    AWI_MIB_MGT, AWI_NOWAIT)) != 0)
1942 					break;
1943 			}
1944 			/* FALLTHRU */
1945 		case AWI_ST_SCAN_SETMIB:
1946 			sc->sc_substate = AWI_ST_SCAN_SCCMD;
1947 			if (sc->sc_cmd_inprog) {
1948 				if ((error = awi_cmd_wait(sc)) != 0)
1949 					break;
1950 			}
1951 			sc->sc_cmd_inprog = AWI_CMD_SCAN;
1952 			ni = ic->ic_bss;
1953 			awi_write_2(sc, AWI_CA_SCAN_DURATION,
1954 			    (ic->ic_flags & IEEE80211_F_ASCAN) ?
1955 			    AWI_ASCAN_DURATION : AWI_PSCAN_DURATION);
1956 			if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1957 				awi_write_1(sc, AWI_CA_SCAN_SET,
1958 				    IEEE80211_FH_CHANSET(
1959 				        ieee80211_chan2ieee(ic, ni->ni_chan)));
1960 				awi_write_1(sc, AWI_CA_SCAN_PATTERN,
1961 				    IEEE80211_FH_CHANPAT(
1962 				        ieee80211_chan2ieee(ic, ni->ni_chan)));
1963 				awi_write_1(sc, AWI_CA_SCAN_IDX, 1);
1964 			} else {
1965 				awi_write_1(sc, AWI_CA_SCAN_SET,
1966 				    ieee80211_chan2ieee(ic, ni->ni_chan));
1967 				awi_write_1(sc, AWI_CA_SCAN_PATTERN, 0);
1968 				awi_write_1(sc, AWI_CA_SCAN_IDX, 0);
1969 			}
1970 			awi_write_1(sc, AWI_CA_SCAN_SUSP, 0);
1971 			sc->sc_cur_chan = ieee80211_chan2ieee(ic, ni->ni_chan);
1972 			if ((error = awi_cmd(sc, AWI_CMD_SCAN, AWI_NOWAIT))
1973 			    != 0)
1974 				break;
1975 			/* FALLTHRU */
1976 		case AWI_ST_SCAN_SCCMD:
1977 			ic->ic_state = nstate;
1978 			sc->sc_substate = AWI_ST_NONE;
1979 			error = EINPROGRESS;
1980 			break;
1981 		default:
1982 			DPRINTF(("awi_newstate: unexpected state %s/%s\n",
1983 			    stname[nstate], substname[sc->sc_substate]));
1984 			sc->sc_substate = AWI_ST_NONE;
1985 			error = EIO;
1986 			break;
1987 		}
1988 		goto out;
1989 	}
1990 
1991 	if (ostate == IEEE80211_S_SCAN) {
1992 		/* set SSID and channel */
1993 		/* substate */
1994 		if (sc->sc_substate == AWI_ST_NONE) {
1995 			sc->sc_nstate = nstate;	/* next state in transition */
1996 			sc->sc_substate = AWI_ST_SUB_INIT;
1997 		}
1998 		ni = ic->ic_bss;
1999 		switch (sc->sc_substate) {
2000 		case AWI_ST_SUB_INIT:
2001 			sc->sc_substate = AWI_ST_SUB_SETSS;
2002 			IEEE80211_ADDR_COPY(&sc->sc_mib_mgt.aCurrent_BSS_ID,
2003 			    ni->ni_bssid);
2004 			memset(&sc->sc_mib_mgt.aCurrent_ESS_ID, 0,
2005 			    AWI_ESS_ID_SIZE);
2006 			sc->sc_mib_mgt.aCurrent_ESS_ID[0] =
2007 			    IEEE80211_ELEMID_SSID;
2008 			sc->sc_mib_mgt.aCurrent_ESS_ID[1] = ni->ni_esslen;
2009 			memcpy(&sc->sc_mib_mgt.aCurrent_ESS_ID[2],
2010 			    ni->ni_essid, ni->ni_esslen);
2011 			LE_WRITE_2(&sc->sc_mib_mgt.aBeacon_Period,
2012 			    ni->ni_intval);
2013 			if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT,
2014 			    AWI_NOWAIT)) != 0)
2015 				break;
2016 			/* FALLTHRU */
2017 		case AWI_ST_SUB_SETSS:
2018 			sc->sc_substate = AWI_ST_SUB_SYNC;
2019 			if (sc->sc_cmd_inprog) {
2020 				if ((error = awi_cmd_wait(sc)) != 0)
2021 					break;
2022 			}
2023 			sc->sc_cmd_inprog = AWI_CMD_SYNC;
2024 			if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
2025 				awi_write_1(sc, AWI_CA_SYNC_SET,
2026 				    IEEE80211_FH_CHANSET(
2027 				        ieee80211_chan2ieee(ic, ni->ni_chan)));
2028 				awi_write_1(sc, AWI_CA_SYNC_PATTERN,
2029 				    IEEE80211_FH_CHANPAT(
2030 				        ieee80211_chan2ieee(ic, ni->ni_chan)));
2031 				awi_write_1(sc, AWI_CA_SYNC_IDX,
2032 				    ni->ni_fhindex);
2033 				awi_write_2(sc, AWI_CA_SYNC_DWELL,
2034 				    ni->ni_fhdwell);
2035 			} else {
2036 				awi_write_1(sc, AWI_CA_SYNC_SET,
2037 				    ieee80211_chan2ieee(ic, ni->ni_chan));
2038 				awi_write_1(sc, AWI_CA_SYNC_PATTERN, 0);
2039 				awi_write_1(sc, AWI_CA_SYNC_IDX, 0);
2040 				awi_write_2(sc, AWI_CA_SYNC_DWELL, 0);
2041 			}
2042 			if (ic->ic_flags & IEEE80211_F_SIBSS) {
2043 				memset(&ni->ni_tstamp, 0,
2044 				    sizeof(ni->ni_tstamp));
2045 				ni->ni_rstamp = 0;
2046 				awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 1);
2047 			} else
2048 				awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 0);
2049 			awi_write_2(sc, AWI_CA_SYNC_MBZ, 0);
2050 			awi_write_bytes(sc, AWI_CA_SYNC_TIMESTAMP,
2051 			    ni->ni_tstamp.data, sizeof(ni->ni_tstamp.data));
2052 			awi_write_4(sc, AWI_CA_SYNC_REFTIME, ni->ni_rstamp);
2053 			sc->sc_cur_chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2054 			if ((error = awi_cmd(sc, AWI_CMD_SYNC, AWI_NOWAIT))
2055 			    != 0)
2056 				break;
2057 			/* FALLTHRU */
2058 		case AWI_ST_SUB_SYNC:
2059 			sc->sc_substate = AWI_ST_NONE;
2060 			if (ic->ic_flags & IEEE80211_F_SIBSS) {
2061 				if ((error = awi_mib(sc, AWI_CMD_GET_MIB,
2062 				    AWI_MIB_MGT, AWI_WAIT)) != 0)
2063 					break;
2064 				IEEE80211_ADDR_COPY(ni->ni_bssid,
2065 				    &sc->sc_mib_mgt.aCurrent_BSS_ID);
2066 			} else {
2067 				if (nstate == IEEE80211_S_RUN) {
2068 					sc->sc_rx_timer = 10;
2069 					ifp->if_timer = 1;
2070 				}
2071 			}
2072 			error = 0;
2073 			break;
2074 		default:
2075 			DPRINTF(("awi_newstate: unexpected state %s/%s\n",
2076 			    stname[nstate], substname[sc->sc_substate]));
2077 			sc->sc_substate = AWI_ST_NONE;
2078 			error = EIO;
2079 			break;
2080 		}
2081 		goto out;
2082 	}
2083 
2084 	sc->sc_substate = AWI_ST_NONE;
2085 
2086 	return (*sc->sc_newstate)(ic, nstate, arg);
2087 out:
2088 	if (error != 0) {
2089 		if (error == EINPROGRESS)
2090 			error = 0;
2091 		return error;
2092 	}
2093 	return (*sc->sc_newstate)(ic, nstate, arg);
2094 }
2095 
2096 static void
2097 awi_recv_mgmt(struct ieee80211com *ic, struct mbuf *m0,
2098 	struct ieee80211_node *ni,
2099 	int subtype, int rssi, u_int32_t rstamp)
2100 {
2101 	struct awi_softc *sc = ic->ic_ifp->if_softc;
2102 
2103 	/* probe request is handled by hardware */
2104 	if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_REQ)
2105 		return;
2106 	(*sc->sc_recv_mgmt)(ic, m0, ni, subtype, rssi, rstamp);
2107 }
2108 
2109 static int
2110 awi_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
2111 	int type, int arg)
2112 {
2113 	struct awi_softc *sc = ic->ic_ifp->if_softc;
2114 
2115 	/* probe request is handled by hardware */
2116 	if (type == IEEE80211_FC0_SUBTYPE_PROBE_REQ)
2117 		return 0;
2118 	return (*sc->sc_send_mgmt)(ic, ni, type, arg);
2119 }
2120 
2121 static struct mbuf *
2122 awi_ether_encap(struct awi_softc *sc, struct mbuf *m)
2123 {
2124 	struct ieee80211com *ic = &sc->sc_ic;
2125 	struct ieee80211_node *ni = ic->ic_bss;
2126 	struct ether_header *eh;
2127 	struct ieee80211_frame *wh;
2128 
2129 	if (m->m_len < sizeof(struct ether_header)) {
2130 		m = m_pullup(m, sizeof(struct ether_header));
2131 		if (m == NULL)
2132 			return NULL;
2133 	}
2134 	eh = mtod(m, struct ether_header *);
2135 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
2136 	if (m == NULL)
2137 		return NULL;
2138 	wh = mtod(m, struct ieee80211_frame *);
2139 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
2140 	*(u_int16_t *)wh->i_dur = 0;
2141 	*(u_int16_t *)wh->i_seq =
2142 	    htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
2143 	ni->ni_txseqs[0]++;
2144 	if (ic->ic_opmode == IEEE80211_M_IBSS ||
2145 	    ic->ic_opmode == IEEE80211_M_AHDEMO) {
2146 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2147 		if (sc->sc_adhoc_ap)
2148 			IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
2149 		else
2150 			IEEE80211_ADDR_COPY(wh->i_addr1, eh->ether_dhost);
2151 		IEEE80211_ADDR_COPY(wh->i_addr2, eh->ether_shost);
2152 		IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
2153 	} else {
2154 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
2155 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
2156 		IEEE80211_ADDR_COPY(wh->i_addr2, eh->ether_shost);
2157 		IEEE80211_ADDR_COPY(wh->i_addr3, eh->ether_dhost);
2158 	}
2159 	return m;
2160 }
2161 
2162 static struct mbuf *
2163 awi_ether_modcap(struct awi_softc *sc, struct mbuf *m)
2164 {
2165 	struct ieee80211com *ic = &sc->sc_ic;
2166 	struct ether_header eh;
2167 	struct ieee80211_frame wh;
2168 	struct llc *llc;
2169 
2170 	if (m->m_len < sizeof(wh) + sizeof(eh)) {
2171 		m = m_pullup(m, sizeof(wh) + sizeof(eh));
2172 		if (m == NULL)
2173 			return NULL;
2174 	}
2175 	memcpy(&wh, mtod(m, caddr_t), sizeof(wh));
2176 	if (wh.i_fc[0] != (IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA))
2177 		return m;
2178 	memcpy(&eh, mtod(m, caddr_t) + sizeof(wh), sizeof(eh));
2179 	m_adj(m, sizeof(eh) - sizeof(*llc));
2180 	if (ic->ic_opmode == IEEE80211_M_IBSS ||
2181 	    ic->ic_opmode == IEEE80211_M_AHDEMO)
2182 		IEEE80211_ADDR_COPY(wh.i_addr2, eh.ether_shost);
2183 	memcpy(mtod(m, caddr_t), &wh, sizeof(wh));
2184 	llc = (struct llc *)(mtod(m, caddr_t) + sizeof(wh));
2185 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
2186 	llc->llc_control = LLC_UI;
2187 	llc->llc_snap.org_code[0] = 0;
2188 	llc->llc_snap.org_code[1] = 0;
2189 	llc->llc_snap.org_code[2] = 0;
2190 	llc->llc_snap.ether_type = eh.ether_type;
2191 	return m;
2192 }
2193