1 /* $NetBSD: awi.c,v 1.62 2004/01/16 14:13:15 onoe Exp $ */
4 * Copyright (c) 1999,2000,2001 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
39 * Driver for AMD 802.11 firmware.
40 * Uses am79c930 chip driver to talk to firmware running on the am79c930.
42 * More-or-less a generic ethernet-like if driver, with 802.11 gorp added.
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.
52 * - fix device reset so it's more likely to work
53 * - show status goo through ifmedia.
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).
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).
73 * - common 802.11 mibish things.
74 * - common 802.11 media layer.
78 * Driver for AMD 802.11 PCnetMobile firmware.
79 * Uses am79c930 chip driver to talk to firmware running on the am79c930.
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>
87 #include <sys/cdefs.h>
89 __KERNEL_RCSID(0, "$NetBSD: awi.c,v 1.62 2004/01/16 14:13:15 onoe Exp $");
92 __FBSDID("$FreeBSD$");
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>
117 #include <sys/device.h>
121 #include <net/if_dl.h>
123 #include <net/if_ether.h>
126 #include <net/ethernet.h>
127 #include <net/if_arp.h>
129 #include <net/if_media.h>
130 #include <net/if_llc.h>
132 #include <net80211/ieee80211_var.h>
134 #include <net80211/ieee80211_compat.h>
141 #include <machine/cpu.h>
142 #include <machine/bus.h>
145 #include <dev/ic/am79c930reg.h>
146 #include <dev/ic/am79c930var.h>
147 #include <dev/ic/awireg.h>
148 #include <dev/ic/awivar.h>
151 #include <dev/awi/am79c930reg.h>
152 #include <dev/awi/am79c930var.h>
153 #include <dev/awi/awireg.h>
154 #include <dev/awi/awivar.h>
158 static void awi_init0(void *);
160 static int awi_init(struct ifnet *);
161 static void awi_stop(struct ifnet *, int);
162 static void awi_start(struct ifnet *);
163 static void awi_watchdog(struct ifnet *);
164 static int awi_ioctl(struct ifnet *, u_long, caddr_t);
165 static int awi_media_change(struct ifnet *);
166 static void awi_media_status(struct ifnet *, struct ifmediareq *);
167 static int awi_mode_init(struct awi_softc *);
168 static void awi_rx_int(struct awi_softc *);
169 static void awi_tx_int(struct awi_softc *);
170 static struct mbuf *awi_devget(struct awi_softc *, u_int32_t, u_int16_t);
171 static int awi_hw_init(struct awi_softc *);
172 static int awi_init_mibs(struct awi_softc *);
173 static int awi_mib(struct awi_softc *, u_int8_t, u_int8_t, int);
174 static int awi_cmd(struct awi_softc *, u_int8_t, int);
175 static int awi_cmd_wait(struct awi_softc *);
176 static void awi_cmd_done(struct awi_softc *);
177 static int awi_next_txd(struct awi_softc *, int, u_int32_t *, u_int32_t *);
178 static int awi_lock(struct awi_softc *);
179 static void awi_unlock(struct awi_softc *);
180 static int awi_intr_lock(struct awi_softc *);
181 static void awi_intr_unlock(struct awi_softc *);
182 static int awi_newstate(struct ieee80211com *, enum ieee80211_state, int);
183 static void awi_recv_mgmt(struct ieee80211com *, struct mbuf *,
184 struct ieee80211_node *, int, int, u_int32_t);
185 static int awi_send_mgmt(struct ieee80211com *, struct ieee80211_node *, int,
187 static struct mbuf *awi_ether_encap(struct awi_softc *, struct mbuf *);
188 static struct mbuf *awi_ether_modcap(struct awi_softc *, struct mbuf *);
190 /* unaligned little endian access */
191 #define LE_READ_2(p) \
192 ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8))
193 #define LE_READ_4(p) \
194 ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8) | \
195 (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24))
196 #define LE_WRITE_2(p, v) \
197 ((((u_int8_t *)(p))[0] = (((u_int32_t)(v) ) & 0xff)), \
198 (((u_int8_t *)(p))[1] = (((u_int32_t)(v) >> 8) & 0xff)))
199 #define LE_WRITE_4(p, v) \
200 ((((u_int8_t *)(p))[0] = (((u_int32_t)(v) ) & 0xff)), \
201 (((u_int8_t *)(p))[1] = (((u_int32_t)(v) >> 8) & 0xff)), \
202 (((u_int8_t *)(p))[2] = (((u_int32_t)(v) >> 16) & 0xff)), \
203 (((u_int8_t *)(p))[3] = (((u_int32_t)(v) >> 24) & 0xff)))
205 struct awi_chanset awi_chanset[] = {
206 /* PHY type domain min max def */
207 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_JP, 6, 17, 6 },
208 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_ES, 0, 26, 1 },
209 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_FR, 0, 32, 1 },
210 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_US, 0, 77, 1 },
211 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_CA, 0, 77, 1 },
212 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_EU, 0, 77, 1 },
213 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_JP, 14, 14, 14 },
214 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_ES, 10, 11, 10 },
215 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_FR, 10, 13, 10 },
216 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_US, 1, 11, 3 },
217 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_CA, 1, 11, 3 },
218 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_EU, 1, 13, 3 },
223 devclass_t awi_devclass;
225 #if __FreeBSD_version < 500043
226 static char *ether_sprintf(u_int8_t *);
229 ether_sprintf(u_int8_t *enaddr)
231 static char strbuf[18];
233 sprintf(strbuf, "%6D", enaddr, ":");
239 #define IFQ_PURGE(ifq) IF_DRAIN(ifq)
240 #define IF_POLL(ifq, m) ((m) = (ifq)->ifq_head)
241 #define IFQ_POLL(ifq, m) IF_POLL((ifq), (m))
242 #define IFQ_DEQUEUE(ifq, m) IF_DEQUEUE((ifq), (m))
250 #define DPRINTF(X) if (awi_debug) printf X
251 #define DPRINTF2(X) if (awi_debug > 1) printf X
258 awi_attach(struct awi_softc *sc)
260 struct ieee80211com *ic = &sc->sc_ic;
261 struct ifnet *ifp = AC2IFP(&sc->sc_arp);
262 int s, i, error, nrate;
264 enum ieee80211_phymode mode;
269 sc->sc_substate = AWI_ST_NONE;
270 if ((error = awi_hw_init(sc)) != 0) {
275 error = awi_init_mibs(sc);
283 #ifdef IFF_NOTRAILERS
286 IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST | IFF_NEEDSGIANT;
287 ifp->if_ioctl = awi_ioctl;
288 ifp->if_start = awi_start;
289 ifp->if_watchdog = awi_watchdog;
291 ifp->if_init = awi_init;
292 ifp->if_stop = awi_stop;
293 IFQ_SET_READY(&ifp->if_snd);
294 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
297 ifp->if_init = awi_init0;
298 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
299 if_initname(ifp, device_get_name(sc->sc_dev),
300 device_get_unit(sc->sc_dev));
304 ic->ic_caps = IEEE80211_C_WEP | IEEE80211_C_IBSS | IEEE80211_C_HOSTAP;
305 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
306 ic->ic_phytype = IEEE80211_T_FH;
307 mode = IEEE80211_MODE_FH;
309 ic->ic_phytype = IEEE80211_T_DS;
310 ic->ic_caps |= IEEE80211_C_AHDEMO;
311 mode = IEEE80211_MODE_11B;
313 ic->ic_opmode = IEEE80211_M_STA;
314 nrate = sc->sc_mib_phy.aSuprt_Data_Rates[1];
315 memcpy(ic->ic_sup_rates[mode].rs_rates,
316 sc->sc_mib_phy.aSuprt_Data_Rates + 2, nrate);
317 ic->ic_sup_rates[mode].rs_nrates = nrate;
318 IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->sc_mib_addr.aMAC_Address);
320 printf("%s: IEEE802.11 %s (firmware %s)\n", ifp->if_xname,
321 (ic->ic_phytype == IEEE80211_T_FH) ? "FH" : "DS", sc->sc_banner);
326 ieee80211_ifattach(ic);
328 sc->sc_newstate = ic->ic_newstate;
329 ic->ic_newstate = awi_newstate;
331 sc->sc_recv_mgmt = ic->ic_recv_mgmt;
332 ic->ic_recv_mgmt = awi_recv_mgmt;
334 sc->sc_send_mgmt = ic->ic_send_mgmt;
335 ic->ic_send_mgmt = awi_send_mgmt;
337 ieee80211_media_init(ic, awi_media_change, awi_media_status);
339 /* Melco compatibility mode. */
340 #define ADD(s, o) ifmedia_add(&ic->ic_media, \
341 IFM_MAKEWORD(IFM_IEEE80211, (s), (o), 0), 0, NULL)
342 ADD(IFM_AUTO, IFM_FLAG0);
344 for (i = 0; i < nrate; i++) {
345 mword = ieee80211_rate2media(ic,
346 ic->ic_sup_rates[mode].rs_rates[i], mode);
349 ADD(mword, IFM_FLAG0);
354 if ((sc->sc_sdhook = shutdownhook_establish(awi_shutdown, sc)) == NULL)
355 printf("%s: WARNING: unable to establish shutdown hook\n",
357 if ((sc->sc_powerhook = powerhook_establish(awi_power, sc)) == NULL)
358 printf("%s: WARNING: unable to establish power hook\n",
364 /* ready to accept ioctl */
367 ieee80211_announce(ic);
373 awi_detach(struct awi_softc *sc)
375 struct ifnet *ifp = AC2IFP(&sc->sc_arp);
378 if (!sc->sc_attached)
385 while (sc->sc_sleep_cnt > 0) {
387 (void)tsleep(sc, PWAIT, "awidet", 1);
390 ieee80211_ifdetach(&sc->sc_ic);
393 shutdownhook_disestablish(sc->sc_sdhook);
394 powerhook_disestablish(sc->sc_powerhook);
402 awi_activate(struct device *self, enum devact act)
404 struct awi_softc *sc = (struct awi_softc *)self;
405 struct ifnet *ifp = AC2IFP(&sc->sc_arp);
413 case DVACT_DEACTIVATE:
423 awi_power(int why, void *arg)
425 struct awi_softc *sc = arg;
426 struct ifnet *ifp = AC2IFP(&sc->sc_arp);
430 DPRINTF(("awi_power: %d\n", why));
432 ocansleep = sc->sc_cansleep;
440 if (ifp->if_flags & IFF_UP) {
442 (void)awi_intr(sc); /* make sure */
445 case PWR_SOFTSUSPEND:
446 case PWR_SOFTSTANDBY:
450 sc->sc_cansleep = ocansleep;
453 #endif /* __NetBSD__ */
456 awi_shutdown(void *arg)
458 struct awi_softc *sc = arg;
459 struct ifnet *ifp = AC2IFP(&sc->sc_arp);
468 struct awi_softc *sc = arg;
470 int handled = 0, ocansleep;
472 static const char *intname[] = {
473 "CMD", "RX", "TX", "SCAN_CMPLT",
474 "CFP_START", "DTIM", "CFP_ENDING", "GROGGY",
475 "TXDATA", "TXBCAST", "TXPS", "TXCF",
476 "TXMGT", "#13", "RXDATA", "RXMGT"
480 if (!sc->sc_enabled || !sc->sc_enab_intr || sc->sc_invalid) {
481 DPRINTF(("awi_intr: stray interrupt: "
482 "enabled %d enab_intr %d invalid %d\n",
483 sc->sc_enabled, sc->sc_enab_intr, sc->sc_invalid));
487 am79c930_gcr_setbits(&sc->sc_chip,
488 AM79C930_GCR_DISPWDN | AM79C930_GCR_ECINT);
489 awi_write_1(sc, AWI_DIS_PWRDN, 1);
490 ocansleep = sc->sc_cansleep;
494 if (awi_intr_lock(sc) != 0)
496 status = awi_read_1(sc, AWI_INTSTAT);
497 awi_write_1(sc, AWI_INTSTAT, 0);
498 awi_write_1(sc, AWI_INTSTAT, 0);
499 status |= awi_read_1(sc, AWI_INTSTAT2) << 8;
500 awi_write_1(sc, AWI_INTSTAT2, 0);
503 if (!sc->sc_cmd_inprog)
504 status &= ~AWI_INT_CMD; /* make sure */
511 printf("awi_intr: status 0x%04x", status);
512 for (i = 0; i < sizeof(intname)/sizeof(intname[0]);
514 if (status & (1 << i))
515 printf(" %s", intname[i]);
521 if (status & AWI_INT_RX)
523 if (status & AWI_INT_TX)
525 if (status & AWI_INT_CMD)
527 if (status & AWI_INT_SCAN_CMPLT) {
528 if (sc->sc_ic.ic_state == IEEE80211_S_SCAN &&
529 sc->sc_substate == AWI_ST_NONE)
530 ieee80211_next_scan(&sc->sc_ic);
533 sc->sc_cansleep = ocansleep;
534 am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_DISPWDN);
535 awi_write_1(sc, AWI_DIS_PWRDN, 0);
543 struct awi_softc *sc = arg;
545 (void)awi_init(AC2IFP(&sc->sc_arp));
550 awi_init(struct ifnet *ifp)
552 struct awi_softc *sc = ifp->if_softc;
553 struct ieee80211com *ic = &sc->sc_ic;
554 struct ieee80211_node *ni = ic->ic_bss;
555 struct ieee80211_rateset *rs;
558 DPRINTF(("awi_init: enabled=%d\n", sc->sc_enabled));
559 if (sc->sc_enabled) {
563 (*sc->sc_enable)(sc);
565 if ((error = awi_hw_init(sc)) != 0) {
567 (*sc->sc_disable)(sc);
572 ic->ic_state = IEEE80211_S_INIT;
574 ic->ic_flags &= ~IEEE80211_F_IBSSON;
575 switch (ic->ic_opmode) {
576 case IEEE80211_M_STA:
577 sc->sc_mib_local.Network_Mode = 1;
578 sc->sc_mib_local.Acting_as_AP = 0;
580 case IEEE80211_M_IBSS:
581 ic->ic_flags |= IEEE80211_F_IBSSON;
583 case IEEE80211_M_AHDEMO:
584 sc->sc_mib_local.Network_Mode = 0;
585 sc->sc_mib_local.Acting_as_AP = 0;
587 case IEEE80211_M_HOSTAP:
588 sc->sc_mib_local.Network_Mode = 1;
589 sc->sc_mib_local.Acting_as_AP = 1;
591 case IEEE80211_M_MONITOR:
595 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
597 memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
598 sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
599 sc->sc_mib_mac.aDesired_ESS_ID[1] = ic->ic_des_esslen;
600 memcpy(&sc->sc_mib_mac.aDesired_ESS_ID[2], ic->ic_des_essid,
603 /* configure basic rate */
604 if (ic->ic_phytype == IEEE80211_T_FH)
605 rs = &ic->ic_sup_rates[IEEE80211_MODE_FH];
607 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
608 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
609 rate = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
612 for (i = 0; i < rs->rs_nrates; i++) {
613 if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
614 rate < (rs->rs_rates[i] & IEEE80211_RATE_VAL))
615 rate = rs->rs_rates[i] & IEEE80211_RATE_VAL;
619 LE_WRITE_2(&sc->sc_mib_mac.aStation_Basic_Rate, rate);
621 if ((error = awi_mode_init(sc)) != 0) {
622 DPRINTF(("awi_init: awi_mode_init failed %d\n", error));
627 /* start transmitter */
628 sc->sc_txdone = sc->sc_txnext = sc->sc_txbase;
629 awi_write_4(sc, sc->sc_txbase + AWI_TXD_START, 0);
630 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NEXT, 0);
631 awi_write_4(sc, sc->sc_txbase + AWI_TXD_LENGTH, 0);
632 awi_write_1(sc, sc->sc_txbase + AWI_TXD_RATE, 0);
633 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NDA, 0);
634 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NRA, 0);
635 awi_write_1(sc, sc->sc_txbase + AWI_TXD_STATE, 0);
636 awi_write_4(sc, AWI_CA_TX_DATA, sc->sc_txbase);
637 awi_write_4(sc, AWI_CA_TX_MGT, 0);
638 awi_write_4(sc, AWI_CA_TX_BCAST, 0);
639 awi_write_4(sc, AWI_CA_TX_PS, 0);
640 awi_write_4(sc, AWI_CA_TX_CF, 0);
641 if ((error = awi_cmd(sc, AWI_CMD_INIT_TX, AWI_WAIT)) != 0) {
642 DPRINTF(("awi_init: failed to start transmitter: %d\n", error));
648 if ((error = awi_cmd(sc, AWI_CMD_INIT_RX, AWI_WAIT)) != 0) {
649 DPRINTF(("awi_init: failed to start receiver: %d\n", error));
653 sc->sc_rxdoff = awi_read_4(sc, AWI_CA_IRX_DATA_DESC);
654 sc->sc_rxmoff = awi_read_4(sc, AWI_CA_IRX_PS_DESC);
656 ifp->if_drv_flags |= IFF_DRV_RUNNING;
657 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
658 ic->ic_state = IEEE80211_S_INIT;
660 if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
661 ic->ic_opmode == IEEE80211_M_HOSTAP) {
662 ni->ni_chan = ic->ic_ibss_chan;
663 ni->ni_intval = ic->ic_bintval;
666 memset(&ni->ni_tstamp, 0, sizeof(ni->ni_tstamp));
668 ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
669 IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr);
670 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
671 IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
672 ni->ni_esslen = ic->ic_des_esslen;
673 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
674 ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
675 if (ic->ic_phytype == IEEE80211_T_FH) {
676 ni->ni_fhdwell = 200; /* XXX */
680 ni->ni_capinfo = IEEE80211_CAPINFO_IBSS;
681 memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
684 if (ic->ic_flags & IEEE80211_F_PRIVACY)
685 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
686 if (ic->ic_opmode != IEEE80211_M_AHDEMO)
687 ic->ic_flags |= IEEE80211_F_SIBSS;
688 ic->ic_state = IEEE80211_S_SCAN; /*XXX*/
689 sc->sc_substate = AWI_ST_NONE;
690 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
692 /* XXX check sc->sc_cur_chan */
693 ni->ni_chan = &ic->ic_channels[sc->sc_cur_chan];
694 ic->ic_curchan = ni->ni_chan;
695 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
701 awi_stop(struct ifnet *ifp, int disable)
703 struct awi_softc *sc = ifp->if_softc;
708 DPRINTF(("awi_stop(%d)\n", disable));
710 ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1);
712 if (!sc->sc_invalid) {
713 if (sc->sc_cmd_inprog)
714 (void)awi_cmd_wait(sc);
715 (void)awi_cmd(sc, AWI_CMD_KILL_RX, AWI_WAIT);
716 sc->sc_cmd_inprog = AWI_CMD_FLUSH_TX;
717 awi_write_1(sc, AWI_CA_FTX_DATA, 1);
718 awi_write_1(sc, AWI_CA_FTX_MGT, 0);
719 awi_write_1(sc, AWI_CA_FTX_BCAST, 0);
720 awi_write_1(sc, AWI_CA_FTX_PS, 0);
721 awi_write_1(sc, AWI_CA_FTX_CF, 0);
722 (void)awi_cmd(sc, AWI_CMD_FLUSH_TX, AWI_WAIT);
724 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING|IFF_DRV_OACTIVE);
726 sc->sc_tx_timer = sc->sc_rx_timer = 0;
727 if (sc->sc_rxpend != NULL) {
728 m_freem(sc->sc_rxpend);
729 sc->sc_rxpend = NULL;
731 IFQ_PURGE(&ifp->if_snd);
735 am79c930_gcr_setbits(&sc->sc_chip,
736 AM79C930_GCR_CORESET);
738 (*sc->sc_disable)(sc);
744 awi_start(struct ifnet *ifp)
746 struct awi_softc *sc = ifp->if_softc;
747 struct ieee80211com *ic = &sc->sc_ic;
748 struct ieee80211_node *ni;
749 struct ieee80211_frame *wh;
750 struct ether_header *eh;
753 u_int32_t txd, frame, ntxd;
756 if (!sc->sc_enabled || sc->sc_invalid)
761 IF_POLL(&ic->ic_mgtq, m0);
765 len = m0->m_pkthdr.len;
766 if (awi_next_txd(sc, len, &frame, &ntxd)) {
767 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
770 IF_DEQUEUE(&ic->ic_mgtq, m0);
771 ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
772 m0->m_pkthdr.rcvif = NULL;
774 if (ic->ic_state != IEEE80211_S_RUN)
776 IFQ_POLL(&ifp->if_snd, m0);
780 * Need to calculate the real length to determine
781 * if the transmit buffer has a room for the packet.
783 len = m0->m_pkthdr.len + sizeof(struct ieee80211_frame);
784 if (!(ifp->if_flags & IFF_LINK0) && !sc->sc_adhoc_ap)
785 len += sizeof(struct llc) -
786 sizeof(struct ether_header);
787 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
788 /* XXX other crypto */
790 len += IEEE80211_WEP_IVLEN +
791 IEEE80211_WEP_KIDLEN + IEEE80211_WEP_CRCLEN;
793 if (awi_next_txd(sc, len, &frame, &ntxd)) {
794 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
797 IFQ_DEQUEUE(&ifp->if_snd, m0);
799 if (bpf_peers_present(ifp->if_bpf))
800 bpf_mtap(ifp->if_bpf, m0);
802 if ((ifp->if_flags & IFF_LINK0) || sc->sc_adhoc_ap)
803 m0 = awi_ether_encap(sc, m0);
805 if (m0->m_len < sizeof(struct ether_header) &&
806 ((m0 = m_pullup(m0, sizeof(struct ether_header)))) == NULL) {
810 eh = mtod(m0, struct ether_header *);
811 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
814 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
815 (m0->m_flags & M_PWR_SAV) == 0) {
816 ieee80211_pwrsave(ic, ni, m0);
819 m0 = ieee80211_encap(ic, m0, ni);
823 wh = mtod(m0, struct ieee80211_frame *);
824 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
825 (ic->ic_opmode == IEEE80211_M_HOSTAP ||
826 ic->ic_opmode == IEEE80211_M_IBSS) &&
827 sc->sc_adhoc_ap == 0 &&
828 (ifp->if_flags & IFF_LINK0) == 0 &&
829 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
830 IEEE80211_FC0_TYPE_DATA && ni == NULL) {
836 ieee80211_free_node(ni);
842 if (bpf_peers_present(ic->ic_rawbpf))
843 bpf_mtap(ic->ic_rawbpf, m0);
846 struct ieee80211_key *k;
848 k = ieee80211_crypto_encap(ic, ni, m0);
851 ieee80211_free_node(ni);
857 if (m0->m_pkthdr.len != len) {
858 if_printf(ifp, "length %d should be %d\n",
859 m0->m_pkthdr.len, len);
863 ieee80211_free_node(ni);
868 if ((ifp->if_flags & IFF_DEBUG) && (ifp->if_flags & IFF_LINK2))
869 ieee80211_dump_pkt(m0->m_data, m0->m_len,
870 ic->ic_bss->ni_rates.
871 rs_rates[ic->ic_bss->ni_txrate] &
872 IEEE80211_RATE_VAL, -1);
874 for (m = m0, len = 0; m != NULL; m = m->m_next) {
875 awi_write_bytes(sc, frame + len, mtod(m, u_int8_t *),
880 rate = (ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate] &
881 IEEE80211_RATE_VAL) * 5;
882 awi_write_1(sc, ntxd + AWI_TXD_STATE, 0);
883 awi_write_4(sc, txd + AWI_TXD_START, frame);
884 awi_write_4(sc, txd + AWI_TXD_NEXT, ntxd);
885 awi_write_4(sc, txd + AWI_TXD_LENGTH, len);
886 awi_write_1(sc, txd + AWI_TXD_RATE, rate);
887 awi_write_4(sc, txd + AWI_TXD_NDA, 0);
888 awi_write_4(sc, txd + AWI_TXD_NRA, 0);
889 awi_write_1(sc, txd + AWI_TXD_STATE, AWI_TXD_ST_OWN);
890 sc->sc_txnext = ntxd;
898 awi_watchdog(struct ifnet *ifp)
900 struct awi_softc *sc = ifp->if_softc;
905 if (!sc->sc_enabled || sc->sc_invalid)
908 ocansleep = sc->sc_cansleep;
910 if (sc->sc_tx_timer) {
911 if (--sc->sc_tx_timer == 0) {
912 printf("%s: device timeout\n", ifp->if_xname);
913 prevdone = sc->sc_txdone;
915 if (sc->sc_txdone == prevdone) {
923 if (sc->sc_rx_timer) {
924 if (--sc->sc_rx_timer == 0) {
925 if (sc->sc_ic.ic_state == IEEE80211_S_RUN) {
926 ieee80211_new_state(&sc->sc_ic,
927 IEEE80211_S_SCAN, -1);
933 /* TODO: rate control */
934 ieee80211_watchdog(&sc->sc_ic);
936 sc->sc_cansleep = ocansleep;
940 awi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
942 struct awi_softc *sc = ifp->if_softc;
943 struct ifreq *ifr = (struct ifreq *)data;
947 /* serialize ioctl, since we may sleep */
948 if ((error = awi_lock(sc)) != 0)
953 if (ifp->if_flags & IFF_UP) {
954 if (sc->sc_enabled) {
956 * To avoid rescanning another access point,
957 * do not call awi_init() here. Instead,
958 * only reflect promisc mode settings.
960 error = awi_mode_init(sc);
962 error = awi_init(ifp);
963 } else if (sc->sc_enabled)
968 error = ifmedia_ioctl(ifp, ifr, &sc->sc_ic.ic_media, cmd);
973 error = ENETRESET; /* XXX */
975 error = (cmd == SIOCADDMULTI) ?
976 ether_addmulti(ifr, &sc->sc_ic.ic_ec) :
977 ether_delmulti(ifr, &sc->sc_ic.ic_ec);
979 if (error == ENETRESET) {
982 error = awi_mode_init(sc);
988 error = ieee80211_ioctl(&sc->sc_ic, cmd, data);
989 if (error == ENETRESET) {
991 error = awi_init(ifp);
1004 * Called from ifmedia_ioctl via awi_ioctl with lock obtained.
1006 * TBD factor with ieee80211_media_change
1009 awi_media_change(struct ifnet *ifp)
1011 struct awi_softc *sc = ifp->if_softc;
1012 struct ieee80211com *ic = &sc->sc_ic;
1013 struct ifmedia_entry *ime;
1014 enum ieee80211_opmode newmode;
1015 int i, rate, newadhoc_ap, error = 0;
1017 ime = ic->ic_media.ifm_cur;
1018 if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) {
1021 struct ieee80211_rateset *rs =
1022 &ic->ic_sup_rates[(ic->ic_phytype == IEEE80211_T_FH)
1023 ? IEEE80211_MODE_FH : IEEE80211_MODE_11B];
1024 rate = ieee80211_media2rate(ime->ifm_media);
1027 for (i = 0; i < rs->rs_nrates; i++) {
1028 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
1031 if (i == rs->rs_nrates)
1034 if (ic->ic_fixed_rate != i) {
1035 ic->ic_fixed_rate = i;
1040 * combination of mediaopt
1042 * hostap adhoc flag0 opmode adhoc_ap comment
1043 * + - - HOSTAP 0 HostAP
1045 * - + + AHDEMO 0 WaveLAN adhoc
1046 * - - + IBSS 1 Melco old Sta
1048 * - - - STA 0 Infra Station
1051 if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
1052 newmode = IEEE80211_M_HOSTAP;
1053 else if (ime->ifm_media & IFM_IEEE80211_ADHOC) {
1054 if (ic->ic_phytype == IEEE80211_T_DS &&
1055 (ime->ifm_media & IFM_FLAG0))
1056 newmode = IEEE80211_M_AHDEMO;
1058 newmode = IEEE80211_M_IBSS;
1059 } else if (ime->ifm_media & IFM_FLAG0) {
1060 newmode = IEEE80211_M_IBSS;
1063 newmode = IEEE80211_M_STA;
1064 if (ic->ic_opmode != newmode || sc->sc_adhoc_ap != newadhoc_ap) {
1065 ic->ic_opmode = newmode;
1066 sc->sc_adhoc_ap = newadhoc_ap;
1070 if (error == ENETRESET) {
1072 error = awi_init(ifp);
1080 awi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1082 struct awi_softc *sc = ifp->if_softc;
1083 struct ieee80211com *ic = &sc->sc_ic;
1085 enum ieee80211_phymode mode;
1087 imr->ifm_status = IFM_AVALID;
1088 if (ic->ic_state == IEEE80211_S_RUN)
1089 imr->ifm_status |= IFM_ACTIVE;
1090 imr->ifm_active = IFM_IEEE80211;
1091 if (ic->ic_phytype == IEEE80211_T_FH)
1092 mode = IEEE80211_MODE_FH;
1094 mode = IEEE80211_MODE_11B;
1095 if (ic->ic_state == IEEE80211_S_RUN) {
1096 rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate] &
1099 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
1102 rate = ic->ic_sup_rates[mode].
1103 rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
1105 imr->ifm_active |= ieee80211_rate2media(ic, rate, mode);
1106 switch (ic->ic_opmode) {
1107 case IEEE80211_M_MONITOR: /* we should never reach here */
1109 case IEEE80211_M_STA:
1111 case IEEE80211_M_IBSS:
1112 if (sc->sc_adhoc_ap)
1113 imr->ifm_active |= IFM_FLAG0;
1115 imr->ifm_active |= IFM_IEEE80211_ADHOC;
1117 case IEEE80211_M_AHDEMO:
1118 imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1120 case IEEE80211_M_HOSTAP:
1121 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1127 awi_mode_init(struct awi_softc *sc)
1129 struct ifnet *ifp = AC2IFP(&sc->sc_arp);
1132 struct ifmultiaddr *ifma;
1134 struct ether_multi *enm;
1135 struct ether_multistep step;
1138 /* reinitialize muticast filter */
1140 sc->sc_mib_local.Accept_All_Multicast_Dis = 0;
1141 if (sc->sc_ic.ic_opmode != IEEE80211_M_HOSTAP &&
1142 (ifp->if_flags & IFF_PROMISC)) {
1143 sc->sc_mib_mac.aPromiscuous_Enable = 1;
1146 sc->sc_mib_mac.aPromiscuous_Enable = 0;
1148 if (ifp->if_flags & IFF_ALLMULTI)
1151 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1152 if (ifma->ifma_addr->sa_family != AF_LINK)
1154 if (n == AWI_GROUP_ADDR_SIZE) {
1155 IF_ADDR_UNLOCK(ifp);
1158 IEEE80211_ADDR_COPY(sc->sc_mib_addr.aGroup_Addresses[n],
1159 LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1162 IF_ADDR_UNLOCK(ifp);
1164 ETHER_FIRST_MULTI(step, &sc->sc_ic.ic_ec, enm);
1165 while (enm != NULL) {
1166 if (n == AWI_GROUP_ADDR_SIZE ||
1167 !IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi))
1169 IEEE80211_ADDR_COPY(sc->sc_mib_addr.aGroup_Addresses[n],
1172 ETHER_NEXT_MULTI(step, enm);
1175 for (; n < AWI_GROUP_ADDR_SIZE; n++)
1176 memset(sc->sc_mib_addr.aGroup_Addresses[n], 0,
1177 IEEE80211_ADDR_LEN);
1178 sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
1182 if (sc->sc_mib_local.Accept_All_Multicast_Dis)
1183 ifp->if_flags &= ~IFF_ALLMULTI;
1185 ifp->if_flags |= IFF_ALLMULTI;
1187 sc->sc_mib_mgt.Wep_Required =
1188 (sc->sc_ic.ic_flags & IEEE80211_F_PRIVACY) ? AWI_WEP_ON : AWI_WEP_OFF;
1190 if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) ||
1191 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_ADDR, AWI_WAIT)) ||
1192 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MAC, AWI_WAIT)) ||
1193 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT, AWI_WAIT)) ||
1194 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_PHY, AWI_WAIT))) {
1195 DPRINTF(("awi_mode_init: MIB set failed: %d\n", error));
1202 awi_rx_int(struct awi_softc *sc)
1204 struct ieee80211com *ic = &sc->sc_ic;
1205 struct ifnet *ifp = AC2IFP(&sc->sc_arp);
1206 struct ieee80211_node *ni;
1207 u_int8_t state, rate, rssi;
1209 u_int32_t frame, next, rstamp, rxoff;
1212 rxoff = sc->sc_rxdoff;
1214 state = awi_read_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE);
1215 if (state & AWI_RXD_ST_OWN)
1217 if (!(state & AWI_RXD_ST_CONSUMED)) {
1218 if (sc->sc_substate != AWI_ST_NONE)
1220 if (state & AWI_RXD_ST_RXERROR) {
1224 len = awi_read_2(sc, rxoff + AWI_RXD_LEN);
1225 rate = awi_read_1(sc, rxoff + AWI_RXD_RATE);
1226 rssi = awi_read_1(sc, rxoff + AWI_RXD_RSSI);
1227 frame = awi_read_4(sc, rxoff + AWI_RXD_START_FRAME) &
1229 rstamp = awi_read_4(sc, rxoff + AWI_RXD_LOCALTIME);
1230 m = awi_devget(sc, frame, len);
1235 if (state & AWI_RXD_ST_LF) {
1236 /* TODO check my bss */
1237 if (!(sc->sc_ic.ic_flags & IEEE80211_F_SIBSS) &&
1238 sc->sc_ic.ic_state == IEEE80211_S_RUN) {
1239 sc->sc_rx_timer = 10;
1242 if ((ifp->if_flags & IFF_DEBUG) &&
1243 (ifp->if_flags & IFF_LINK2))
1244 ieee80211_dump_pkt(m->m_data, m->m_len,
1246 if ((ifp->if_flags & IFF_LINK0) ||
1248 m = awi_ether_modcap(sc, m);
1251 sizeof(struct ieee80211_frame_min));
1256 ni = ieee80211_find_rxnode(ic,
1257 mtod(m, struct ieee80211_frame_min *));
1258 ieee80211_input(ic, m, ni, rssi, rstamp);
1259 ieee80211_free_node(ni);
1263 state |= AWI_RXD_ST_CONSUMED;
1264 awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1266 next = awi_read_4(sc, rxoff + AWI_RXD_NEXT);
1267 if (next & AWI_RXD_NEXT_LAST)
1269 /* make sure the next pointer is correct */
1270 if (next != awi_read_4(sc, rxoff + AWI_RXD_NEXT))
1272 state |= AWI_RXD_ST_OWN;
1273 awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1274 rxoff = next & 0x7fff;
1276 sc->sc_rxdoff = rxoff;
1280 awi_tx_int(struct awi_softc *sc)
1282 struct ifnet *ifp = AC2IFP(&sc->sc_arp);
1285 while (sc->sc_txdone != sc->sc_txnext) {
1286 flags = awi_read_1(sc, sc->sc_txdone + AWI_TXD_STATE);
1287 if ((flags & AWI_TXD_ST_OWN) || !(flags & AWI_TXD_ST_DONE))
1289 if (flags & AWI_TXD_ST_ERROR)
1291 sc->sc_txdone = awi_read_4(sc, sc->sc_txdone + AWI_TXD_NEXT) &
1294 DPRINTF2(("awi_txint: txdone %d txnext %d txbase %d txend %d\n",
1295 sc->sc_txdone, sc->sc_txnext, sc->sc_txbase, sc->sc_txend));
1296 sc->sc_tx_timer = 0;
1297 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1301 static struct mbuf *
1302 awi_devget(struct awi_softc *sc, u_int32_t off, u_int16_t len)
1304 struct ifnet *ifp = AC2IFP(&sc->sc_arp);
1306 struct mbuf *top, **mp;
1309 top = sc->sc_rxpend;
1312 sc->sc_rxpend = NULL;
1313 top->m_pkthdr.len += len;
1315 while (*mp != NULL) {
1319 if (m->m_flags & M_EXT)
1320 tlen = m->m_ext.ext_size;
1321 else if (m->m_flags & M_PKTHDR)
1328 awi_read_bytes(sc, off, mtod(m, u_int8_t *) + m->m_len, tlen);
1335 MGETHDR(m, M_DONTWAIT, MT_DATA);
1338 m->m_pkthdr.rcvif = ifp;
1339 m->m_pkthdr.len = len;
1341 m->m_flags |= M_HASFCS;
1343 MGET(m, M_DONTWAIT, MT_DATA);
1350 if (len >= MINCLSIZE) {
1351 MCLGET(m, M_DONTWAIT);
1352 if (m->m_flags & M_EXT)
1353 m->m_len = m->m_ext.ext_size;
1356 int hdrlen = sizeof(struct ieee80211_frame) +
1358 caddr_t newdata = (caddr_t)
1359 ALIGN(m->m_data + hdrlen) - hdrlen;
1360 m->m_len -= newdata - m->m_data;
1361 m->m_data = newdata;
1365 awi_read_bytes(sc, off, mtod(m, u_int8_t *), m->m_len);
1375 * Initialize hardware and start firmware to accept commands.
1376 * Called everytime after power on firmware.
1380 awi_hw_init(struct awi_softc *sc)
1386 sc->sc_enab_intr = 0;
1387 sc->sc_invalid = 0; /* XXX: really? */
1388 awi_drvstate(sc, AWI_DRV_RESET);
1390 /* reset firmware */
1391 am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1393 awi_write_1(sc, AWI_SELFTEST, 0);
1394 awi_write_1(sc, AWI_CMD, 0);
1395 awi_write_1(sc, AWI_BANNER, 0);
1396 am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1399 /* wait for selftest completion */
1400 for (i = 0; ; i++) {
1403 if (i >= AWI_SELFTEST_TIMEOUT*hz/1000) {
1404 printf("%s: failed to complete selftest (timeout)\n",
1405 AC2IFP(&sc->sc_arp)->if_xname);
1408 status = awi_read_1(sc, AWI_SELFTEST);
1409 if ((status & 0xf0) == 0xf0)
1411 if (sc->sc_cansleep) {
1413 (void)tsleep(sc, PWAIT, "awitst", 1);
1416 DELAY(1000*1000/hz);
1419 if (status != AWI_SELFTEST_PASSED) {
1420 printf("%s: failed to complete selftest (code %x)\n",
1421 AC2IFP(&sc->sc_arp)->if_xname, status);
1425 /* check banner to confirm firmware write it */
1426 awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
1427 if (memcmp(sc->sc_banner, "PCnetMobile:", 12) != 0) {
1428 printf("%s: failed to complete selftest (bad banner)\n",
1429 AC2IFP(&sc->sc_arp)->if_xname);
1430 for (i = 0; i < AWI_BANNER_LEN; i++)
1431 printf("%s%02x", i ? ":" : "\t", sc->sc_banner[i]);
1436 /* initializing interrupt */
1437 sc->sc_enab_intr = 1;
1438 error = awi_intr_lock(sc);
1441 intmask = AWI_INT_GROGGY | AWI_INT_SCAN_CMPLT |
1442 AWI_INT_TX | AWI_INT_RX | AWI_INT_CMD;
1443 awi_write_1(sc, AWI_INTMASK, ~intmask & 0xff);
1444 awi_write_1(sc, AWI_INTMASK2, 0);
1445 awi_write_1(sc, AWI_INTSTAT, 0);
1446 awi_write_1(sc, AWI_INTSTAT2, 0);
1447 awi_intr_unlock(sc);
1448 am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_ENECINT);
1450 /* issuing interface test command */
1451 error = awi_cmd(sc, AWI_CMD_NOP, AWI_WAIT);
1453 printf("%s: failed to complete selftest",
1454 AC2IFP(&sc->sc_arp)->if_xname);
1456 printf(" (no hardware)\n");
1457 else if (error != EWOULDBLOCK)
1458 printf(" (error %d)\n", error);
1459 else if (sc->sc_cansleep)
1460 printf(" (lost interrupt)\n");
1462 printf(" (command timeout)\n");
1466 /* Initialize VBM */
1467 awi_write_1(sc, AWI_VBM_OFFSET, 0);
1468 awi_write_1(sc, AWI_VBM_LENGTH, 1);
1469 awi_write_1(sc, AWI_VBM_BITMAP, 0);
1474 * Extract the factory default MIB value from firmware and assign the driver
1476 * Called once at attaching the interface.
1480 awi_init_mibs(struct awi_softc *sc)
1483 struct ieee80211com *ic = &sc->sc_ic;
1484 struct awi_chanset *cs;
1486 if ((error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) ||
1487 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_ADDR, AWI_WAIT)) ||
1488 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MAC, AWI_WAIT)) ||
1489 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MGT, AWI_WAIT)) ||
1490 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_PHY, AWI_WAIT))) {
1491 printf("%s: failed to get default mib value (error %d)\n",
1492 AC2IFP(&sc->sc_arp)->if_xname, error);
1496 memset(&sc->sc_ic.ic_chan_avail, 0, sizeof(sc->sc_ic.ic_chan_avail));
1497 for (cs = awi_chanset; ; cs++) {
1498 if (cs->cs_type == 0) {
1499 printf("%s: failed to set available channel\n",
1500 AC2IFP(&sc->sc_arp)->if_xname);
1503 if (cs->cs_type == sc->sc_mib_phy.IEEE_PHY_Type &&
1504 cs->cs_region == sc->sc_mib_phy.aCurrent_Reg_Domain)
1507 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1508 for (i = cs->cs_min; i <= cs->cs_max; i++) {
1509 chan = IEEE80211_FH_CHAN(i % 3 + 1, i);
1510 setbit(sc->sc_ic.ic_chan_avail, chan);
1511 /* XXX for FHSS, does frequency matter? */
1512 ic->ic_channels[chan].ic_freq = 0;
1513 ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_FHSS;
1515 * According to the IEEE 802.11 specification,
1516 * hop pattern parameter for FH phy should be
1517 * incremented by 3 for given hop chanset, i.e.,
1518 * the chanset parameter is calculated for given
1519 * hop patter. However, BayStack 650 Access Points
1520 * apparently use fixed hop chanset parameter value
1521 * 1 for any hop pattern. So we also try this
1522 * combination of hop chanset and pattern.
1524 chan = IEEE80211_FH_CHAN(1, i);
1525 setbit(sc->sc_ic.ic_chan_avail, chan);
1526 ic->ic_channels[chan].ic_freq = 0; /* XXX */
1527 ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_FHSS;
1530 for (i = cs->cs_min; i <= cs->cs_max; i++) {
1531 setbit(sc->sc_ic.ic_chan_avail, i);
1532 ic->ic_channels[i].ic_freq =
1533 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
1534 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
1537 sc->sc_cur_chan = cs->cs_def;
1538 ic->ic_ibss_chan = &ic->ic_channels[cs->cs_def];
1540 sc->sc_mib_local.Fragmentation_Dis = 1;
1541 sc->sc_mib_local.Add_PLCP_Dis = 0;
1542 sc->sc_mib_local.MAC_Hdr_Prsv = 0;
1543 sc->sc_mib_local.Rx_Mgmt_Que_En = 0;
1544 sc->sc_mib_local.Re_Assembly_Dis = 1;
1545 sc->sc_mib_local.Strip_PLCP_Dis = 0;
1546 sc->sc_mib_local.Power_Saving_Mode_Dis = 1;
1547 sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
1548 sc->sc_mib_local.Check_Seq_Cntl_Dis = 0;
1549 sc->sc_mib_local.Flush_CFP_Queue_On_CF_End = 0;
1550 sc->sc_mib_local.Network_Mode = 1;
1551 sc->sc_mib_local.PWD_Lvl = 0;
1552 sc->sc_mib_local.CFP_Mode = 0;
1554 /* allocate buffers */
1555 sc->sc_txbase = AWI_BUFFERS;
1556 sc->sc_txend = sc->sc_txbase +
1557 (AWI_TXD_SIZE + sizeof(struct ieee80211_frame) +
1558 sizeof(struct ether_header) + ETHERMTU) * AWI_NTXBUFS;
1559 LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Offset, sc->sc_txbase);
1560 LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Size,
1561 sc->sc_txend - sc->sc_txbase);
1562 LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Offset, sc->sc_txend);
1563 LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Size,
1564 AWI_BUFFERS_END - sc->sc_txend);
1565 sc->sc_mib_local.Acting_as_AP = 0;
1566 sc->sc_mib_local.Fill_CFP = 0;
1568 memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
1569 sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
1571 sc->sc_mib_mgt.aPower_Mgt_Mode = 0;
1572 sc->sc_mib_mgt.aDTIM_Period = 1;
1573 LE_WRITE_2(&sc->sc_mib_mgt.aATIM_Window, 0);
1578 awi_mib(struct awi_softc *sc, u_int8_t cmd, u_int8_t mib, int wflag)
1581 u_int8_t size, *ptr;
1585 ptr = (u_int8_t *)&sc->sc_mib_local;
1586 size = sizeof(sc->sc_mib_local);
1589 ptr = (u_int8_t *)&sc->sc_mib_addr;
1590 size = sizeof(sc->sc_mib_addr);
1593 ptr = (u_int8_t *)&sc->sc_mib_mac;
1594 size = sizeof(sc->sc_mib_mac);
1597 ptr = (u_int8_t *)&sc->sc_mib_stat;
1598 size = sizeof(sc->sc_mib_stat);
1601 ptr = (u_int8_t *)&sc->sc_mib_mgt;
1602 size = sizeof(sc->sc_mib_mgt);
1605 ptr = (u_int8_t *)&sc->sc_mib_phy;
1606 size = sizeof(sc->sc_mib_phy);
1611 if (sc->sc_cmd_inprog) {
1612 if ((error = awi_cmd_wait(sc)) != 0) {
1613 if (error == EWOULDBLOCK)
1614 DPRINTF(("awi_mib: cmd %d inprog",
1615 sc->sc_cmd_inprog));
1619 sc->sc_cmd_inprog = cmd;
1620 if (cmd == AWI_CMD_SET_MIB)
1621 awi_write_bytes(sc, AWI_CA_MIB_DATA, ptr, size);
1622 awi_write_1(sc, AWI_CA_MIB_TYPE, mib);
1623 awi_write_1(sc, AWI_CA_MIB_SIZE, size);
1624 awi_write_1(sc, AWI_CA_MIB_INDEX, 0);
1625 if ((error = awi_cmd(sc, cmd, wflag)) != 0)
1627 if (cmd == AWI_CMD_GET_MIB) {
1628 awi_read_bytes(sc, AWI_CA_MIB_DATA, ptr, size);
1633 printf("awi_mib: #%d:", mib);
1634 for (i = 0; i < size; i++)
1635 printf(" %02x", ptr[i]);
1644 awi_cmd(struct awi_softc *sc, u_int8_t cmd, int wflag)
1649 static const char *cmdname[] = {
1650 "IDLE", "NOP", "SET_MIB", "INIT_TX", "FLUSH_TX", "INIT_RX",
1651 "KILL_RX", "SLEEP", "WAKE", "GET_MIB", "SCAN", "SYNC", "RESUME"
1656 if (awi_debug > 1) {
1657 if (cmd >= sizeof(cmdname)/sizeof(cmdname[0]))
1658 printf("awi_cmd: #%d", cmd);
1660 printf("awi_cmd: %s", cmdname[cmd]);
1661 printf(" %s\n", wflag == AWI_NOWAIT ? "nowait" : "wait");
1664 sc->sc_cmd_inprog = cmd;
1665 awi_write_1(sc, AWI_CMD_STATUS, AWI_STAT_IDLE);
1666 awi_write_1(sc, AWI_CMD, cmd);
1667 if (wflag == AWI_NOWAIT)
1669 if ((error = awi_cmd_wait(sc)) != 0)
1671 status = awi_read_1(sc, AWI_CMD_STATUS);
1672 awi_write_1(sc, AWI_CMD, 0);
1676 case AWI_STAT_BADPARM:
1679 printf("%s: command %d failed %x\n",
1680 AC2IFP(&sc->sc_arp)->if_xname, cmd, status);
1687 awi_cmd_wait(struct awi_softc *sc)
1692 while (sc->sc_cmd_inprog) {
1695 if (awi_read_1(sc, AWI_CMD) != sc->sc_cmd_inprog) {
1696 printf("%s: failed to access hardware\n",
1697 AC2IFP(&sc->sc_arp)->if_xname);
1701 if (sc->sc_cansleep) {
1703 error = tsleep(sc, PWAIT, "awicmd",
1704 AWI_CMD_TIMEOUT*hz/1000);
1707 if (awi_read_1(sc, AWI_CMD_STATUS) != AWI_STAT_IDLE) {
1711 if (i++ >= AWI_CMD_TIMEOUT*1000/10)
1712 error = EWOULDBLOCK;
1720 DPRINTF(("awi_cmd_wait: cmd 0x%x, error %d\n",
1721 sc->sc_cmd_inprog, error));
1727 awi_cmd_done(struct awi_softc *sc)
1729 u_int8_t cmd, status;
1731 status = awi_read_1(sc, AWI_CMD_STATUS);
1732 if (status == AWI_STAT_IDLE)
1733 return; /* stray interrupt */
1735 cmd = sc->sc_cmd_inprog;
1736 sc->sc_cmd_inprog = 0;
1738 awi_write_1(sc, AWI_CMD, 0);
1740 if (status != AWI_STAT_OK) {
1741 printf("%s: command %d failed %x\n",
1742 AC2IFP(&sc->sc_arp)->if_xname, cmd, status);
1743 sc->sc_substate = AWI_ST_NONE;
1746 if (sc->sc_substate != AWI_ST_NONE)
1747 (void)ieee80211_new_state(&sc->sc_ic, sc->sc_nstate, -1);
1751 awi_next_txd(struct awi_softc *sc, int len, u_int32_t *framep, u_int32_t *ntxdp)
1753 u_int32_t txd, ntxd, frame;
1755 txd = sc->sc_txnext;
1756 frame = txd + AWI_TXD_SIZE;
1757 if (frame + len > sc->sc_txend)
1758 frame = sc->sc_txbase;
1760 if (ntxd + AWI_TXD_SIZE > sc->sc_txend)
1761 ntxd = sc->sc_txbase;
1765 * Determine if there are any room in ring buffer.
1766 * --- send wait, === new data, +++ conflict (ENOBUFS)
1767 * base........................end
1768 * done----txd=====ntxd OK
1769 * --txd=====done++++ntxd-- full
1770 * --txd=====ntxd done-- OK
1771 * ==ntxd done----txd=== OK
1772 * ==done++++ntxd----txd=== full
1773 * ++ntxd txd=====done++ full
1776 if (txd < sc->sc_txdone && ntxd + AWI_TXD_SIZE > sc->sc_txdone)
1779 if (txd < sc->sc_txdone || ntxd + AWI_TXD_SIZE > sc->sc_txdone)
1786 awi_lock(struct awi_softc *sc)
1793 if (curproc == NULL)
1798 * Though driver ioctl should be called with context,
1799 * KAME ipv6 stack calls ioctl in interrupt for now.
1800 * We simply abort the request if there are other
1801 * ioctl requests in progress.
1809 sc->sc_cansleep = 0;
1812 while (sc->sc_busy) {
1816 error = tsleep(sc, PWAIT | PCATCH, "awilck", 0);
1822 sc->sc_cansleep = 1;
1827 awi_unlock(struct awi_softc *sc)
1830 sc->sc_cansleep = 0;
1831 if (sc->sc_sleep_cnt)
1836 awi_intr_lock(struct awi_softc *sc)
1842 for (retry = 0; retry < 10; retry++) {
1843 for (i = 0; i < AWI_LOCKOUT_TIMEOUT*1000/5; i++) {
1844 if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0)
1850 awi_write_1(sc, AWI_LOCKOUT_MAC, 1);
1851 if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0)
1853 awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
1856 printf("%s: failed to lock interrupt\n",
1857 AC2IFP(&sc->sc_arp)->if_xname);
1864 awi_intr_unlock(struct awi_softc *sc)
1867 awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
1871 awi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1873 struct ifnet *ifp = ic->ic_ifp;
1874 struct awi_softc *sc = ifp->if_softc;
1875 struct ieee80211_node *ni;
1878 enum ieee80211_state ostate;
1880 static const char *stname[] =
1881 { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" };
1882 static const char *substname[] =
1883 { "NONE", "SCAN_INIT", "SCAN_SETMIB", "SCAN_SCCMD",
1884 "SUB_INIT", "SUB_SETSS", "SUB_SYNC" };
1885 #endif /* AWI_DEBUG */
1887 ostate = ic->ic_state;
1888 DPRINTF(("awi_newstate: %s (%s/%s) -> %s\n", stname[ostate],
1889 stname[sc->sc_nstate], substname[sc->sc_substate], stname[nstate]));
1893 case IEEE80211_S_INIT:
1894 awi_drvstate(sc, AWI_DRV_RESET);
1896 case IEEE80211_S_SCAN:
1897 if (ic->ic_opmode == IEEE80211_M_IBSS ||
1898 ic->ic_opmode == IEEE80211_M_AHDEMO)
1899 awi_drvstate(sc, AWI_DRV_ADHSC);
1901 awi_drvstate(sc, AWI_DRV_INFSY);
1903 case IEEE80211_S_AUTH:
1904 awi_drvstate(sc, AWI_DRV_INFSY);
1906 case IEEE80211_S_ASSOC:
1907 awi_drvstate(sc, AWI_DRV_INFAUTH);
1909 case IEEE80211_S_RUN:
1910 if (ic->ic_opmode == IEEE80211_M_IBSS ||
1911 ic->ic_opmode == IEEE80211_M_AHDEMO)
1912 awi_drvstate(sc, AWI_DRV_ADHSY);
1914 awi_drvstate(sc, AWI_DRV_INFASSOC);
1918 if (nstate == IEEE80211_S_INIT) {
1919 sc->sc_substate = AWI_ST_NONE;
1920 ic->ic_flags &= ~IEEE80211_F_SIBSS;
1921 return (*sc->sc_newstate)(ic, nstate, arg);
1924 /* state transition */
1925 if (nstate == IEEE80211_S_SCAN) {
1927 if (sc->sc_substate == AWI_ST_NONE) {
1928 sc->sc_nstate = nstate; /* next state in transition */
1929 sc->sc_substate = AWI_ST_SCAN_INIT;
1931 switch (sc->sc_substate) {
1932 case AWI_ST_SCAN_INIT:
1933 sc->sc_substate = AWI_ST_SCAN_SETMIB;
1935 case IEEE80211_S_RUN:
1937 if (ifp->if_flags & IFF_DEBUG)
1938 printf("%s: no recent beacons from %s;"
1941 ether_sprintf(ic->ic_bss->ni_bssid));
1943 case IEEE80211_S_AUTH:
1944 case IEEE80211_S_ASSOC:
1945 case IEEE80211_S_INIT:
1946 ieee80211_begin_scan(ic, 0);
1948 case IEEE80211_S_SCAN:
1952 if (ic->ic_flags & IEEE80211_F_ASCAN)
1953 newmode = AWI_SCAN_ACTIVE;
1955 newmode = AWI_SCAN_PASSIVE;
1956 if (sc->sc_mib_mgt.aScan_Mode != newmode) {
1957 sc->sc_mib_mgt.aScan_Mode = newmode;
1958 if ((error = awi_mib(sc, AWI_CMD_SET_MIB,
1959 AWI_MIB_MGT, AWI_NOWAIT)) != 0)
1963 case AWI_ST_SCAN_SETMIB:
1964 sc->sc_substate = AWI_ST_SCAN_SCCMD;
1965 if (sc->sc_cmd_inprog) {
1966 if ((error = awi_cmd_wait(sc)) != 0)
1969 sc->sc_cmd_inprog = AWI_CMD_SCAN;
1971 awi_write_2(sc, AWI_CA_SCAN_DURATION,
1972 (ic->ic_flags & IEEE80211_F_ASCAN) ?
1973 AWI_ASCAN_DURATION : AWI_PSCAN_DURATION);
1974 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1975 awi_write_1(sc, AWI_CA_SCAN_SET,
1976 IEEE80211_FH_CHANSET(
1977 ieee80211_chan2ieee(ic, ic->ic_curchan)));
1978 awi_write_1(sc, AWI_CA_SCAN_PATTERN,
1979 IEEE80211_FH_CHANPAT(
1980 ieee80211_chan2ieee(ic, ic->ic_curchan)));
1981 awi_write_1(sc, AWI_CA_SCAN_IDX, 1);
1983 awi_write_1(sc, AWI_CA_SCAN_SET,
1984 ieee80211_chan2ieee(ic, ic->ic_curchan));
1985 awi_write_1(sc, AWI_CA_SCAN_PATTERN, 0);
1986 awi_write_1(sc, AWI_CA_SCAN_IDX, 0);
1988 awi_write_1(sc, AWI_CA_SCAN_SUSP, 0);
1989 sc->sc_cur_chan = ieee80211_chan2ieee(ic, ic->ic_curchan);
1990 if ((error = awi_cmd(sc, AWI_CMD_SCAN, AWI_NOWAIT))
1994 case AWI_ST_SCAN_SCCMD:
1995 ic->ic_state = nstate;
1996 sc->sc_substate = AWI_ST_NONE;
1997 error = EINPROGRESS;
2000 DPRINTF(("awi_newstate: unexpected state %s/%s\n",
2001 stname[nstate], substname[sc->sc_substate]));
2002 sc->sc_substate = AWI_ST_NONE;
2009 if (ostate == IEEE80211_S_SCAN) {
2010 /* set SSID and channel */
2012 if (sc->sc_substate == AWI_ST_NONE) {
2013 sc->sc_nstate = nstate; /* next state in transition */
2014 sc->sc_substate = AWI_ST_SUB_INIT;
2017 switch (sc->sc_substate) {
2018 case AWI_ST_SUB_INIT:
2019 sc->sc_substate = AWI_ST_SUB_SETSS;
2020 IEEE80211_ADDR_COPY(&sc->sc_mib_mgt.aCurrent_BSS_ID,
2022 memset(&sc->sc_mib_mgt.aCurrent_ESS_ID, 0,
2024 sc->sc_mib_mgt.aCurrent_ESS_ID[0] =
2025 IEEE80211_ELEMID_SSID;
2026 sc->sc_mib_mgt.aCurrent_ESS_ID[1] = ni->ni_esslen;
2027 memcpy(&sc->sc_mib_mgt.aCurrent_ESS_ID[2],
2028 ni->ni_essid, ni->ni_esslen);
2029 LE_WRITE_2(&sc->sc_mib_mgt.aBeacon_Period,
2031 if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT,
2035 case AWI_ST_SUB_SETSS:
2036 sc->sc_substate = AWI_ST_SUB_SYNC;
2037 if (sc->sc_cmd_inprog) {
2038 if ((error = awi_cmd_wait(sc)) != 0)
2041 sc->sc_cmd_inprog = AWI_CMD_SYNC;
2042 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
2043 awi_write_1(sc, AWI_CA_SYNC_SET,
2044 IEEE80211_FH_CHANSET(
2045 ieee80211_chan2ieee(ic, ni->ni_chan)));
2046 awi_write_1(sc, AWI_CA_SYNC_PATTERN,
2047 IEEE80211_FH_CHANPAT(
2048 ieee80211_chan2ieee(ic, ni->ni_chan)));
2049 awi_write_1(sc, AWI_CA_SYNC_IDX,
2051 awi_write_2(sc, AWI_CA_SYNC_DWELL,
2054 awi_write_1(sc, AWI_CA_SYNC_SET,
2055 ieee80211_chan2ieee(ic, ni->ni_chan));
2056 awi_write_1(sc, AWI_CA_SYNC_PATTERN, 0);
2057 awi_write_1(sc, AWI_CA_SYNC_IDX, 0);
2058 awi_write_2(sc, AWI_CA_SYNC_DWELL, 0);
2060 if (ic->ic_flags & IEEE80211_F_SIBSS) {
2061 memset(&ni->ni_tstamp, 0, sizeof(ni->ni_tstamp));
2063 awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 1);
2065 awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 0);
2066 awi_write_2(sc, AWI_CA_SYNC_MBZ, 0);
2067 awi_write_bytes(sc, AWI_CA_SYNC_TIMESTAMP,
2068 ni->ni_tstamp.data, 8);
2069 awi_write_4(sc, AWI_CA_SYNC_REFTIME, ni->ni_rstamp);
2070 sc->sc_cur_chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2071 if ((error = awi_cmd(sc, AWI_CMD_SYNC, AWI_NOWAIT))
2075 case AWI_ST_SUB_SYNC:
2076 sc->sc_substate = AWI_ST_NONE;
2077 if (ic->ic_flags & IEEE80211_F_SIBSS) {
2078 if ((error = awi_mib(sc, AWI_CMD_GET_MIB,
2079 AWI_MIB_MGT, AWI_WAIT)) != 0)
2081 IEEE80211_ADDR_COPY(ni->ni_bssid,
2082 &sc->sc_mib_mgt.aCurrent_BSS_ID);
2084 if (nstate == IEEE80211_S_RUN) {
2085 sc->sc_rx_timer = 10;
2092 DPRINTF(("awi_newstate: unexpected state %s/%s\n",
2093 stname[nstate], substname[sc->sc_substate]));
2094 sc->sc_substate = AWI_ST_NONE;
2101 sc->sc_substate = AWI_ST_NONE;
2103 return (*sc->sc_newstate)(ic, nstate, arg);
2106 if (error == EINPROGRESS)
2110 return (*sc->sc_newstate)(ic, nstate, arg);
2114 awi_recv_mgmt(struct ieee80211com *ic, struct mbuf *m0,
2115 struct ieee80211_node *ni,
2116 int subtype, int rssi, u_int32_t rstamp)
2118 struct awi_softc *sc = ic->ic_ifp->if_softc;
2120 /* probe request is handled by hardware */
2121 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_REQ)
2123 (*sc->sc_recv_mgmt)(ic, m0, ni, subtype, rssi, rstamp);
2127 awi_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
2130 struct awi_softc *sc = ic->ic_ifp->if_softc;
2132 /* probe request is handled by hardware */
2133 if (type == IEEE80211_FC0_SUBTYPE_PROBE_REQ)
2135 return (*sc->sc_send_mgmt)(ic, ni, type, arg);
2138 static struct mbuf *
2139 awi_ether_encap(struct awi_softc *sc, struct mbuf *m)
2141 struct ieee80211com *ic = &sc->sc_ic;
2142 struct ieee80211_node *ni = ic->ic_bss;
2143 struct ether_header *eh;
2144 struct ieee80211_frame *wh;
2146 if (m->m_len < sizeof(struct ether_header)) {
2147 m = m_pullup(m, sizeof(struct ether_header));
2151 eh = mtod(m, struct ether_header *);
2152 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
2155 wh = mtod(m, struct ieee80211_frame *);
2156 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
2157 *(u_int16_t *)wh->i_dur = 0;
2158 *(u_int16_t *)wh->i_seq =
2159 htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
2161 if (ic->ic_opmode == IEEE80211_M_IBSS ||
2162 ic->ic_opmode == IEEE80211_M_AHDEMO) {
2163 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2164 if (sc->sc_adhoc_ap)
2165 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
2167 IEEE80211_ADDR_COPY(wh->i_addr1, eh->ether_dhost);
2168 IEEE80211_ADDR_COPY(wh->i_addr2, eh->ether_shost);
2169 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
2171 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
2172 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
2173 IEEE80211_ADDR_COPY(wh->i_addr2, eh->ether_shost);
2174 IEEE80211_ADDR_COPY(wh->i_addr3, eh->ether_dhost);
2179 static struct mbuf *
2180 awi_ether_modcap(struct awi_softc *sc, struct mbuf *m)
2182 struct ieee80211com *ic = &sc->sc_ic;
2183 struct ether_header eh;
2184 struct ieee80211_frame wh;
2187 if (m->m_len < sizeof(wh) + sizeof(eh)) {
2188 m = m_pullup(m, sizeof(wh) + sizeof(eh));
2192 memcpy(&wh, mtod(m, caddr_t), sizeof(wh));
2193 if (wh.i_fc[0] != (IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA))
2195 memcpy(&eh, mtod(m, caddr_t) + sizeof(wh), sizeof(eh));
2196 m_adj(m, sizeof(eh) - sizeof(*llc));
2197 if (ic->ic_opmode == IEEE80211_M_IBSS ||
2198 ic->ic_opmode == IEEE80211_M_AHDEMO)
2199 IEEE80211_ADDR_COPY(wh.i_addr2, eh.ether_shost);
2200 memcpy(mtod(m, caddr_t), &wh, sizeof(wh));
2201 llc = (struct llc *)(mtod(m, caddr_t) + sizeof(wh));
2202 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
2203 llc->llc_control = LLC_UI;
2204 llc->llc_snap.org_code[0] = 0;
2205 llc->llc_snap.org_code[1] = 0;
2206 llc->llc_snap.org_code[2] = 0;
2207 llc->llc_snap.ether_type = eh.ether_type;