1 /* $NetBSD: awi.c,v 1.26 2000/07/21 04:48:55 onoe Exp $ */
2 /* $FreeBSD: src/sys/dev/awi/awi.c,v 1.10.2.2 2003/01/23 21:06:42 sam Exp $ */
3 /* $DragonFly: src/sys/dev/netif/awi/Attic/awi.c,v 1.2 2003/06/17 04:28:22 dillon Exp $ */
6 * Copyright (c) 1999 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
41 * Driver for AMD 802.11 firmware.
42 * Uses am79c930 chip driver to talk to firmware running on the am79c930.
44 * More-or-less a generic ethernet-like if driver, with 802.11 gorp added.
49 * - flush tx queue on resynch.
50 * - clear oactive on "down".
51 * - rewrite copy-into-mbuf code
52 * - mgmt state machine gets stuck retransmitting assoc requests.
54 * - fix device reset so it's more likely to work
55 * - show status goo through ifmedia.
58 * - deal with more 802.11 frames.
59 * - send reassoc request
60 * - deal with reassoc response
61 * - send/deal with disassociation
62 * - deal with "full" access points (no room for me).
67 * - need ioctls for poking at the MIBs
68 * - implement ad-hoc mode (including bss creation).
69 * - decide when to do "ad hoc" vs. infrastructure mode (IFF_LINK flags?)
70 * (focus on inf. mode since that will be needed for ietf)
71 * - deal with DH vs. FH versions of the card
72 * - deal with faster cards (2mb/s)
73 * - ?WEP goo (mmm, rc4) (it looks not particularly useful).
75 * - common 802.11 mibish things.
76 * - common 802.11 media layer.
80 * Driver for AMD 802.11 PCnetMobile firmware.
81 * Uses am79c930 chip driver to talk to firmware running on the am79c930.
83 * The initial version of the driver was written by
84 * Bill Sommerfeld <sommerfeld@netbsd.org>.
85 * Then the driver module completely rewritten to support cards with DS phy
86 * and to support adhoc mode by Atsushi Onoe <onoe@netbsd.org>
90 #if defined(__FreeBSD__) && __FreeBSD__ >= 4
92 #elif defined(__FreeBSD__) && __FreeBSD__ >= 3
94 #define NBPFILTER NBPF
99 #include <sys/param.h>
100 #include <sys/systm.h>
101 #include <sys/kernel.h>
102 #include <sys/mbuf.h>
103 #include <sys/malloc.h>
104 #include <sys/proc.h>
105 #include <sys/socket.h>
106 #include <sys/sockio.h>
107 #include <sys/errno.h>
108 #include <sys/syslog.h>
109 #if defined(__FreeBSD__) && __FreeBSD__ >= 4
112 #include <sys/device.h>
116 #include <net/if_dl.h>
118 #include <net/ethernet.h>
120 #include <net/if_ether.h>
122 #include <net/if_media.h>
123 #include <net/if_llc.h>
124 #include <net/if_ieee80211.h>
127 #include <netinet/in.h>
128 #include <netinet/in_systm.h>
129 #include <netinet/in_var.h>
130 #include <netinet/ip.h>
132 #include <netinet/if_inarp.h>
134 #include <netinet/if_ether.h>
140 #include <net/bpfdesc.h>
143 #include <machine/cpu.h>
144 #include <machine/bus.h>
146 #include <machine/intr.h>
149 #include <machine/clock.h>
153 #include <dev/ic/am79c930reg.h>
154 #include <dev/ic/am79c930var.h>
155 #include <dev/ic/awireg.h>
156 #include <dev/ic/awivar.h>
159 #include <dev/awi/am79c930reg.h>
160 #include <dev/awi/am79c930var.h>
161 #include <dev/awi/awireg.h>
162 #include <dev/awi/awivar.h>
165 static int awi_ioctl __P((struct ifnet *ifp, u_long cmd, caddr_t data));
167 static int awi_media_rate2opt __P((struct awi_softc *sc, int rate));
168 static int awi_media_opt2rate __P((struct awi_softc *sc, int opt));
169 static int awi_media_change __P((struct ifnet *ifp));
170 static void awi_media_status __P((struct ifnet *ifp, struct ifmediareq *imr));
172 static void awi_watchdog __P((struct ifnet *ifp));
173 static void awi_start __P((struct ifnet *ifp));
174 static void awi_txint __P((struct awi_softc *sc));
175 static struct mbuf * awi_fix_txhdr __P((struct awi_softc *sc, struct mbuf *m0));
176 static struct mbuf * awi_fix_rxhdr __P((struct awi_softc *sc, struct mbuf *m0));
177 static void awi_input __P((struct awi_softc *sc, struct mbuf *m, u_int32_t rxts, u_int8_t rssi));
178 static void awi_rxint __P((struct awi_softc *sc));
179 static struct mbuf * awi_devget __P((struct awi_softc *sc, u_int32_t off, u_int16_t len));
180 static int awi_init_hw __P((struct awi_softc *sc));
181 static int awi_init_mibs __P((struct awi_softc *sc));
182 static int awi_init_txrx __P((struct awi_softc *sc));
183 static void awi_stop_txrx __P((struct awi_softc *sc));
184 static int awi_start_scan __P((struct awi_softc *sc));
185 static int awi_next_scan __P((struct awi_softc *sc));
186 static void awi_stop_scan __P((struct awi_softc *sc));
187 static void awi_recv_beacon __P((struct awi_softc *sc, struct mbuf *m0, u_int32_t rxts, u_int8_t rssi));
188 static int awi_set_ss __P((struct awi_softc *sc));
189 static void awi_try_sync __P((struct awi_softc *sc));
190 static void awi_sync_done __P((struct awi_softc *sc));
191 static void awi_send_deauth __P((struct awi_softc *sc));
192 static void awi_send_auth __P((struct awi_softc *sc, int seq));
193 static void awi_recv_auth __P((struct awi_softc *sc, struct mbuf *m0));
194 static void awi_send_asreq __P((struct awi_softc *sc, int reassoc));
195 static void awi_recv_asresp __P((struct awi_softc *sc, struct mbuf *m0));
196 static int awi_mib __P((struct awi_softc *sc, u_int8_t cmd, u_int8_t mib));
197 static int awi_cmd_scan __P((struct awi_softc *sc));
198 static int awi_cmd __P((struct awi_softc *sc, u_int8_t cmd));
199 static void awi_cmd_done __P((struct awi_softc *sc));
200 static int awi_next_txd __P((struct awi_softc *sc, int len, u_int32_t *framep, u_int32_t*ntxdp));
201 static int awi_lock __P((struct awi_softc *sc));
202 static void awi_unlock __P((struct awi_softc *sc));
203 static int awi_intr_lock __P((struct awi_softc *sc));
204 static void awi_intr_unlock __P((struct awi_softc *sc));
205 static int awi_cmd_wait __P((struct awi_softc *sc));
206 static void awi_print_essid __P((u_int8_t *essid));
209 static void awi_dump_pkt __P((struct awi_softc *sc, struct mbuf *m, int rssi));
212 #define AWI_DUMP_MASK(fc0) (1 << (((fc0) & IEEE80211_FC0_SUBTYPE_MASK) >> 4))
213 int awi_dump_mask = AWI_DUMP_MASK(IEEE80211_FC0_SUBTYPE_BEACON);
214 int awi_dump_hdr = 0;
215 int awi_dump_len = 28;
219 #define AWI_BPF_NORM 0
220 #define AWI_BPF_RAW 1
222 #define AWI_BPF_MTAP(sc, m, raw) do { \
223 if ((sc)->sc_ifp->if_bpf && (sc)->sc_rawbpf == (raw)) \
224 bpf_mtap((sc)->sc_ifp, (m)); \
227 #define AWI_BPF_MTAP(sc, m, raw) do { \
228 if ((sc)->sc_ifp->if_bpf && (sc)->sc_rawbpf == (raw)) \
229 bpf_mtap((sc)->sc_ifp->if_bpf, (m)); \
233 #define AWI_BPF_MTAP(sc, m, raw)
237 #define llc_snap llc_un.type_snap
242 devclass_t awi_devclass;
245 /* NetBSD compatible functions */
246 static char * ether_sprintf __P((u_int8_t *));
249 ether_sprintf(enaddr)
252 static char strbuf[18];
254 sprintf(strbuf, "%6D", enaddr, ":");
261 struct awi_softc *sc;
263 struct ifnet *ifp = sc->sc_ifp;
270 struct ifmediareq imr;
275 * Even if we can sleep in initialization state,
276 * all other processes (e.g. ifconfig) have to wait for
277 * completion of attaching interface.
280 sc->sc_status = AWI_ST_INIT;
281 TAILQ_INIT(&sc->sc_scan);
282 error = awi_init_hw(sc);
288 error = awi_init_mibs(sc);
296 ifp->if_start = awi_start;
297 ifp->if_ioctl = awi_ioctl;
298 ifp->if_watchdog = awi_watchdog;
299 ifp->if_mtu = ETHERMTU;
300 ifp->if_hdrlen = sizeof(struct ieee80211_frame) +
301 sizeof(struct ether_header);
302 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
303 #ifdef IFF_NOTRAILERS
304 ifp->if_flags |= IFF_NOTRAILERS;
307 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
310 ifp->if_output = ether_output;
311 ifp->if_snd.ifq_maxlen = ifqmaxlen;
312 memcpy(sc->sc_ec.ac_enaddr, sc->sc_mib_addr.aMAC_Address,
316 printf("%s: IEEE802.11 %s %dMbps (firmware %s)\n",
318 sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH ? "FH" : "DS",
319 sc->sc_tx_rate / 10, sc->sc_banner);
320 printf("%s: address %s\n",
321 sc->sc_dev.dv_xname, ether_sprintf(sc->sc_mib_addr.aMAC_Address));
323 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
326 ether_ifattach(ifp, sc->sc_mib_addr.aMAC_Address);
328 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
333 ifmedia_init(&sc->sc_media, 0, awi_media_change, awi_media_status);
334 phy_rates = sc->sc_mib_phy.aSuprt_Data_Rates;
335 for (i = 0; i < phy_rates[1]; i++) {
336 mword = awi_media_rate2opt(sc, AWI_80211_RATE(phy_rates[2 + i]));
339 mword |= IFM_IEEE80211;
340 ifmedia_add(&sc->sc_media, mword, 0, NULL);
341 ifmedia_add(&sc->sc_media,
342 mword | IFM_IEEE80211_ADHOC, 0, NULL);
343 if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_FH)
344 ifmedia_add(&sc->sc_media,
345 mword | IFM_IEEE80211_ADHOC | IFM_FLAG0, 0, NULL);
347 awi_media_status(ifp, &imr);
348 ifmedia_set(&sc->sc_media, imr.ifm_active);
351 /* ready to accept ioctl */
354 /* Attach is successful. */
362 struct awi_softc *sc;
364 struct ifnet *ifp = sc->sc_ifp;
367 /* Succeed if there is no work to do. */
368 if (!sc->sc_attached)
374 while (sc->sc_sleep_cnt > 0) {
376 (void)tsleep(sc, PWAIT, "awidet", 1);
378 if (sc->sc_wep_ctx != NULL)
379 free(sc->sc_wep_ctx, M_DEVBUF);
384 ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY);
388 if (sc->sc_enabled) {
390 (*sc->sc_disable)(sc);
398 awi_activate(self, act)
402 struct awi_softc *sc = (struct awi_softc *)self;
411 case DVACT_DEACTIVATE:
414 if_deactivate(sc->sc_ifp);
424 struct awi_softc *sc;
434 ocansleep = sc->sc_cansleep;
436 #ifdef needtobefixed /*ONOE*/
437 if (why == PWR_RESUME) {
444 (*sc->sc_disable)(sc);
447 sc->sc_cansleep = ocansleep;
450 #endif /* __NetBSD__ */
453 awi_ioctl(ifp, cmd, data)
458 struct awi_softc *sc = ifp->if_softc;
459 struct ifreq *ifr = (struct ifreq *)data;
460 struct ifaddr *ifa = (struct ifaddr *)data;
462 struct ieee80211_nwid nwid;
467 /* serialize ioctl */
468 error = awi_lock(sc);
473 ifp->if_flags |= IFF_UP;
474 switch (ifa->ifa_addr->sa_family) {
477 arp_ifinit((void *)ifp, ifa);
483 sc->sc_format_llc = !(ifp->if_flags & IFF_LINK0);
484 if (!(ifp->if_flags & IFF_UP)) {
485 if (sc->sc_enabled) {
488 (*sc->sc_disable)(sc);
493 error = awi_init(sc);
499 error = ENETRESET; /*XXX*/
501 error = (cmd == SIOCADDMULTI) ?
502 ether_addmulti(ifr, &sc->sc_ec) :
503 ether_delmulti(ifr, &sc->sc_ec);
506 * Do not rescan BSS. Rather, just reset multicast filter.
508 if (error == ENETRESET) {
510 error = awi_init(sc);
516 if (ifr->ifr_mtu > ETHERMTU)
519 ifp->if_mtu = ifr->ifr_mtu;
523 error = suser(curproc);
527 error = copyin(ifr->ifr_data, &nwid, sizeof(nwid));
530 if (nwid.i_len > IEEE80211_NWID_LEN) {
534 if (sc->sc_mib_mac.aDesired_ESS_ID[1] == nwid.i_len &&
535 memcmp(&sc->sc_mib_mac.aDesired_ESS_ID[2], nwid.i_nwid,
538 memset(sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
539 sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
540 sc->sc_mib_mac.aDesired_ESS_ID[1] = nwid.i_len;
541 memcpy(&sc->sc_mib_mac.aDesired_ESS_ID[2], nwid.i_nwid,
543 if (sc->sc_enabled) {
545 error = awi_init(sc);
549 if (ifp->if_flags & IFF_RUNNING)
550 p = sc->sc_bss.essid;
552 p = sc->sc_mib_mac.aDesired_ESS_ID;
553 error = copyout(p + 1, ifr->ifr_data, 1 + IEEE80211_NWID_LEN);
555 case SIOCS80211NWKEY:
557 error = suser(curproc);
561 error = awi_wep_setnwkey(sc, (struct ieee80211_nwkey *)data);
563 case SIOCG80211NWKEY:
564 error = awi_wep_getnwkey(sc, (struct ieee80211_nwkey *)data);
569 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
573 error = awi_wicfg(ifp, cmd, data);
584 awi_media_rate2opt(sc, rate)
585 struct awi_softc *sc;
593 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
594 mword = IFM_IEEE80211_FH1;
596 mword = IFM_IEEE80211_DS1;
599 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
600 mword = IFM_IEEE80211_FH2;
602 mword = IFM_IEEE80211_DS2;
605 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_DS)
606 mword = IFM_IEEE80211_DS5;
609 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_DS)
610 mword = IFM_IEEE80211_DS11;
617 awi_media_opt2rate(sc, opt)
618 struct awi_softc *sc;
624 switch (IFM_SUBTYPE(opt)) {
625 case IFM_IEEE80211_FH1:
626 case IFM_IEEE80211_FH2:
627 if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_FH)
630 case IFM_IEEE80211_DS1:
631 case IFM_IEEE80211_DS2:
632 case IFM_IEEE80211_DS5:
633 case IFM_IEEE80211_DS11:
634 if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_DS)
639 switch (IFM_SUBTYPE(opt)) {
640 case IFM_IEEE80211_FH1:
641 case IFM_IEEE80211_DS1:
644 case IFM_IEEE80211_FH2:
645 case IFM_IEEE80211_DS2:
648 case IFM_IEEE80211_DS5:
651 case IFM_IEEE80211_DS11:
659 * Called from ifmedia_ioctl via awi_ioctl with lock obtained.
662 awi_media_change(ifp)
665 struct awi_softc *sc = ifp->if_softc;
666 struct ifmedia_entry *ime;
671 ime = sc->sc_media.ifm_cur;
672 rate = awi_media_opt2rate(sc, ime->ifm_media);
675 if (rate != sc->sc_tx_rate) {
676 phy_rates = sc->sc_mib_phy.aSuprt_Data_Rates;
677 for (i = 0; i < phy_rates[1]; i++) {
678 if (rate == AWI_80211_RATE(phy_rates[2 + i]))
681 if (i == phy_rates[1])
684 if (ime->ifm_media & IFM_IEEE80211_ADHOC) {
685 sc->sc_mib_local.Network_Mode = 0;
686 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
689 sc->sc_no_bssid = (ime->ifm_media & IFM_FLAG0) ? 1 : 0;
691 sc->sc_mib_local.Network_Mode = 1;
693 if (sc->sc_enabled) {
695 error = awi_init(sc);
701 awi_media_status(ifp, imr)
703 struct ifmediareq *imr;
705 struct awi_softc *sc = ifp->if_softc;
707 imr->ifm_status = IFM_AVALID;
708 if (ifp->if_flags & IFF_RUNNING)
709 imr->ifm_status |= IFM_ACTIVE;
710 imr->ifm_active = IFM_IEEE80211;
711 imr->ifm_active |= awi_media_rate2opt(sc, sc->sc_tx_rate);
712 if (sc->sc_mib_local.Network_Mode == 0) {
713 imr->ifm_active |= IFM_IEEE80211_ADHOC;
715 imr->ifm_active |= IFM_FLAG0;
718 #endif /* IFM_IEEE80211 */
724 struct awi_softc *sc = arg;
726 int error, handled = 0, ocansleep;
728 if (!sc->sc_enabled || !sc->sc_enab_intr || sc->sc_invalid)
731 am79c930_gcr_setbits(&sc->sc_chip,
732 AM79C930_GCR_DISPWDN | AM79C930_GCR_ECINT);
733 awi_write_1(sc, AWI_DIS_PWRDN, 1);
734 ocansleep = sc->sc_cansleep;
738 error = awi_intr_lock(sc);
741 status = awi_read_1(sc, AWI_INTSTAT);
742 awi_write_1(sc, AWI_INTSTAT, 0);
743 awi_write_1(sc, AWI_INTSTAT, 0);
744 status |= awi_read_1(sc, AWI_INTSTAT2) << 8;
745 awi_write_1(sc, AWI_INTSTAT2, 0);
748 if (!sc->sc_cmd_inprog)
749 status &= ~AWI_INT_CMD; /* make sure */
753 if (status & AWI_INT_RX)
755 if (status & AWI_INT_TX)
757 if (status & AWI_INT_CMD)
759 if (status & AWI_INT_SCAN_CMPLT) {
760 if (sc->sc_status == AWI_ST_SCAN &&
761 sc->sc_mgt_timer > 0)
762 (void)awi_next_scan(sc);
765 sc->sc_cansleep = ocansleep;
766 am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_DISPWDN);
767 awi_write_1(sc, AWI_DIS_PWRDN, 0);
773 struct awi_softc *sc;
777 struct ifnet *ifp = sc->sc_ifp;
779 struct ifmultiaddr *ifma;
781 struct ether_multi *enm;
782 struct ether_multistep step;
785 /* reinitialize muticast filter */
787 ifp->if_flags |= IFF_ALLMULTI;
788 sc->sc_mib_local.Accept_All_Multicast_Dis = 0;
789 if (ifp->if_flags & IFF_PROMISC) {
790 sc->sc_mib_mac.aPromiscuous_Enable = 1;
793 sc->sc_mib_mac.aPromiscuous_Enable = 0;
795 if (ifp->if_amcount != 0)
797 for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma != NULL;
798 ifma = LIST_NEXT(ifma, ifma_link)) {
799 if (ifma->ifma_addr->sa_family != AF_LINK)
801 if (n == AWI_GROUP_ADDR_SIZE)
803 memcpy(sc->sc_mib_addr.aGroup_Addresses[n],
804 LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
809 ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
810 while (enm != NULL) {
811 if (n == AWI_GROUP_ADDR_SIZE ||
812 memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)
815 memcpy(sc->sc_mib_addr.aGroup_Addresses[n], enm->enm_addrlo,
818 ETHER_NEXT_MULTI(step, enm);
821 for (; n < AWI_GROUP_ADDR_SIZE; n++)
822 memset(sc->sc_mib_addr.aGroup_Addresses[n], 0, ETHER_ADDR_LEN);
823 ifp->if_flags &= ~IFF_ALLMULTI;
824 sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
827 #ifdef notdef /* allow non-encrypted frame for receiving. */
828 sc->sc_mib_mgt.Wep_Required = sc->sc_wep_algo != NULL ? 1 : 0;
830 if (!sc->sc_enabled) {
833 (*sc->sc_enable)(sc);
834 sc->sc_status = AWI_ST_INIT;
835 error = awi_init_hw(sc);
839 ostatus = sc->sc_status;
840 sc->sc_status = AWI_ST_INIT;
841 if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_LOCAL)) != 0 ||
842 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_ADDR)) != 0 ||
843 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MAC)) != 0 ||
844 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT)) != 0 ||
845 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_PHY)) != 0) {
849 if (ifp->if_flags & IFF_RUNNING)
850 sc->sc_status = AWI_ST_RUNNING;
852 if (ostatus == AWI_ST_INIT) {
853 error = awi_init_txrx(sc);
857 error = awi_start_scan(sc);
864 struct awi_softc *sc;
866 struct ifnet *ifp = sc->sc_ifp;
870 sc->sc_status = AWI_ST_INIT;
871 if (!sc->sc_invalid) {
872 (void)awi_cmd_wait(sc);
873 if (sc->sc_mib_local.Network_Mode &&
874 sc->sc_status > AWI_ST_AUTH)
878 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
880 sc->sc_tx_timer = sc->sc_rx_timer = sc->sc_mgt_timer = 0;
882 IF_DEQUEUE(&sc->sc_mgtq, m);
888 IF_DEQUEUE(&ifp->if_snd, m);
893 while ((bp = TAILQ_FIRST(&sc->sc_scan)) != NULL) {
894 TAILQ_REMOVE(&sc->sc_scan, bp, list);
903 struct awi_softc *sc = ifp->if_softc;
906 if (sc->sc_invalid) {
911 ocansleep = sc->sc_cansleep;
913 if (sc->sc_tx_timer && --sc->sc_tx_timer == 0) {
914 printf("%s: transmit timeout\n", sc->sc_dev.dv_xname);
917 if (sc->sc_rx_timer && --sc->sc_rx_timer == 0) {
918 if (ifp->if_flags & IFF_DEBUG) {
919 printf("%s: no recent beacons from %s; rescanning\n",
921 ether_sprintf(sc->sc_bss.bssid));
923 ifp->if_flags &= ~IFF_RUNNING;
926 if (sc->sc_mgt_timer && --sc->sc_mgt_timer == 0) {
927 switch (sc->sc_status) {
941 if (sc->sc_tx_timer == 0 && sc->sc_rx_timer == 0 &&
942 sc->sc_mgt_timer == 0)
946 sc->sc_cansleep = ocansleep;
953 struct awi_softc *sc = ifp->if_softc;
955 u_int32_t txd, frame, ntxd;
961 IF_DEQUEUE(&sc->sc_mgtq, m0);
963 if (awi_next_txd(sc, m0->m_pkthdr.len, &frame, &ntxd)) {
964 IF_PREPEND(&sc->sc_mgtq, m0);
965 ifp->if_flags |= IFF_OACTIVE;
969 if (!(ifp->if_flags & IFF_RUNNING))
971 IF_DEQUEUE(&ifp->if_snd, m0);
974 len = m0->m_pkthdr.len + sizeof(struct ieee80211_frame);
975 if (sc->sc_format_llc)
976 len += sizeof(struct llc) -
977 sizeof(struct ether_header);
978 if (sc->sc_wep_algo != NULL)
979 len += IEEE80211_WEP_IVLEN +
980 IEEE80211_WEP_KIDLEN + IEEE80211_WEP_CRCLEN;
981 if (awi_next_txd(sc, len, &frame, &ntxd)) {
982 IF_PREPEND(&ifp->if_snd, m0);
983 ifp->if_flags |= IFF_OACTIVE;
986 AWI_BPF_MTAP(sc, m0, AWI_BPF_NORM);
987 m0 = awi_fix_txhdr(sc, m0);
988 if (sc->sc_wep_algo != NULL && m0 != NULL)
989 m0 = awi_wep_encrypt(sc, m0, 1);
998 awi_dump_pkt(sc, m0, -1);
1000 AWI_BPF_MTAP(sc, m0, AWI_BPF_RAW);
1002 for (m = m0; m != NULL; m = m->m_next) {
1003 awi_write_bytes(sc, frame + len, mtod(m, u_int8_t *),
1008 rate = sc->sc_tx_rate; /*XXX*/
1009 awi_write_1(sc, ntxd + AWI_TXD_STATE, 0);
1010 awi_write_4(sc, txd + AWI_TXD_START, frame);
1011 awi_write_4(sc, txd + AWI_TXD_NEXT, ntxd);
1012 awi_write_4(sc, txd + AWI_TXD_LENGTH, len);
1013 awi_write_1(sc, txd + AWI_TXD_RATE, rate);
1014 awi_write_4(sc, txd + AWI_TXD_NDA, 0);
1015 awi_write_4(sc, txd + AWI_TXD_NRA, 0);
1016 awi_write_1(sc, txd + AWI_TXD_STATE, AWI_TXD_ST_OWN);
1017 sc->sc_txnext = ntxd;
1021 if (sc->sc_tx_timer == 0)
1022 sc->sc_tx_timer = 5;
1026 printf("awi_start: sent %d txdone %d txnext %d txbase %d txend %d\n", sent, sc->sc_txdone, sc->sc_txnext, sc->sc_txbase, sc->sc_txend);
1033 struct awi_softc *sc;
1035 struct ifnet *ifp = sc->sc_ifp;
1038 while (sc->sc_txdone != sc->sc_txnext) {
1039 flags = awi_read_1(sc, sc->sc_txdone + AWI_TXD_STATE);
1040 if ((flags & AWI_TXD_ST_OWN) || !(flags & AWI_TXD_ST_DONE))
1042 if (flags & AWI_TXD_ST_ERROR)
1044 sc->sc_txdone = awi_read_4(sc, sc->sc_txdone + AWI_TXD_NEXT) &
1047 sc->sc_tx_timer = 0;
1048 ifp->if_flags &= ~IFF_OACTIVE;
1051 printf("awi_txint: txdone %d txnext %d txbase %d txend %d\n",
1052 sc->sc_txdone, sc->sc_txnext, sc->sc_txbase, sc->sc_txend);
1057 static struct mbuf *
1058 awi_fix_txhdr(sc, m0)
1059 struct awi_softc *sc;
1062 struct ether_header eh;
1063 struct ieee80211_frame *wh;
1066 if (m0->m_len < sizeof(eh)) {
1067 m0 = m_pullup(m0, sizeof(eh));
1071 memcpy(&eh, mtod(m0, caddr_t), sizeof(eh));
1072 if (sc->sc_format_llc) {
1073 m_adj(m0, sizeof(struct ether_header) - sizeof(struct llc));
1074 llc = mtod(m0, struct llc *);
1075 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1076 llc->llc_control = LLC_UI;
1077 llc->llc_snap.org_code[0] = llc->llc_snap.org_code[1] =
1078 llc->llc_snap.org_code[2] = 0;
1079 llc->llc_snap.ether_type = eh.ether_type;
1081 M_PREPEND(m0, sizeof(struct ieee80211_frame), M_DONTWAIT);
1084 wh = mtod(m0, struct ieee80211_frame *);
1086 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1087 LE_WRITE_2(wh->i_dur, 0);
1088 LE_WRITE_2(wh->i_seq, 0);
1089 if (sc->sc_mib_local.Network_Mode) {
1090 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1091 memcpy(wh->i_addr1, sc->sc_bss.bssid, ETHER_ADDR_LEN);
1092 memcpy(wh->i_addr2, eh.ether_shost, ETHER_ADDR_LEN);
1093 memcpy(wh->i_addr3, eh.ether_dhost, ETHER_ADDR_LEN);
1095 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1096 memcpy(wh->i_addr1, eh.ether_dhost, ETHER_ADDR_LEN);
1097 memcpy(wh->i_addr2, eh.ether_shost, ETHER_ADDR_LEN);
1098 memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
1103 static struct mbuf *
1104 awi_fix_rxhdr(sc, m0)
1105 struct awi_softc *sc;
1108 struct ieee80211_frame wh;
1109 struct ether_header *eh;
1112 if (m0->m_len < sizeof(wh)) {
1116 llc = (struct llc *)(mtod(m0, caddr_t) + sizeof(wh));
1117 if (llc->llc_dsap == LLC_SNAP_LSAP &&
1118 llc->llc_ssap == LLC_SNAP_LSAP &&
1119 llc->llc_control == LLC_UI &&
1120 llc->llc_snap.org_code[0] == 0 &&
1121 llc->llc_snap.org_code[1] == 0 &&
1122 llc->llc_snap.org_code[2] == 0) {
1123 memcpy(&wh, mtod(m0, caddr_t), sizeof(wh));
1124 m_adj(m0, sizeof(wh) + sizeof(*llc) - sizeof(*eh));
1125 eh = mtod(m0, struct ether_header *);
1126 switch (wh.i_fc[1] & IEEE80211_FC1_DIR_MASK) {
1127 case IEEE80211_FC1_DIR_NODS:
1128 memcpy(eh->ether_dhost, wh.i_addr1, ETHER_ADDR_LEN);
1129 memcpy(eh->ether_shost, wh.i_addr2, ETHER_ADDR_LEN);
1131 case IEEE80211_FC1_DIR_TODS:
1132 memcpy(eh->ether_dhost, wh.i_addr3, ETHER_ADDR_LEN);
1133 memcpy(eh->ether_shost, wh.i_addr2, ETHER_ADDR_LEN);
1135 case IEEE80211_FC1_DIR_FROMDS:
1136 memcpy(eh->ether_dhost, wh.i_addr1, ETHER_ADDR_LEN);
1137 memcpy(eh->ether_shost, wh.i_addr3, ETHER_ADDR_LEN);
1139 case IEEE80211_FC1_DIR_DSTODS:
1144 /* assuming ethernet encapsulation, just strip 802.11 header */
1145 m_adj(m0, sizeof(wh));
1147 if (ALIGN(mtod(m0, caddr_t) + sizeof(struct ether_header)) !=
1148 (u_int)(mtod(m0, caddr_t) + sizeof(struct ether_header))) {
1149 /* XXX: we loose to estimate the type of encapsulation */
1150 struct mbuf *n, *n0, **np;
1157 while (m0->m_pkthdr.len > off) {
1159 MGETHDR(n, M_DONTWAIT, MT_DATA);
1164 M_MOVE_PKTHDR(n, m0);
1167 MGET(n, M_DONTWAIT, MT_DATA);
1175 if (m0->m_pkthdr.len - off >= MINCLSIZE) {
1176 MCLGET(n, M_DONTWAIT);
1177 if (n->m_flags & M_EXT)
1178 n->m_len = n->m_ext.ext_size;
1183 + sizeof(struct ether_header))
1184 - sizeof(struct ether_header);
1185 n->m_len -= newdata - n->m_data;
1186 n->m_data = newdata;
1188 if (n->m_len > m0->m_pkthdr.len - off)
1189 n->m_len = m0->m_pkthdr.len - off;
1190 m_copydata(m0, off, n->m_len, mtod(n, caddr_t));
1202 awi_input(sc, m, rxts, rssi)
1203 struct awi_softc *sc;
1208 struct ifnet *ifp = sc->sc_ifp;
1209 struct ieee80211_frame *wh;
1211 struct ether_header *eh;
1214 /* trim CRC here for WEP can find its own CRC at the end of packet. */
1215 m_adj(m, -ETHER_CRC_LEN);
1216 AWI_BPF_MTAP(sc, m, AWI_BPF_RAW);
1217 wh = mtod(m, struct ieee80211_frame *);
1218 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
1219 IEEE80211_FC0_VERSION_0) {
1220 printf("%s; receive packet with wrong version: %x\n",
1221 sc->sc_dev.dv_xname, wh->i_fc[0]);
1226 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1227 m = awi_wep_encrypt(sc, m, 0);
1232 wh = mtod(m, struct ieee80211_frame *);
1236 awi_dump_pkt(sc, m, rssi);
1239 if ((sc->sc_mib_local.Network_Mode || !sc->sc_no_bssid) &&
1240 sc->sc_status == AWI_ST_RUNNING) {
1241 if (memcmp(wh->i_addr2, sc->sc_bss.bssid, ETHER_ADDR_LEN) == 0) {
1242 sc->sc_rx_timer = 10;
1243 sc->sc_bss.rssi = rssi;
1246 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1247 case IEEE80211_FC0_TYPE_DATA:
1248 if (sc->sc_mib_local.Network_Mode) {
1249 if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
1250 IEEE80211_FC1_DIR_FROMDS) {
1255 if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
1256 IEEE80211_FC1_DIR_NODS) {
1261 m = awi_fix_rxhdr(sc, m);
1267 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 4)
1268 AWI_BPF_MTAP(sc, m, AWI_BPF_NORM);
1271 (*ifp->if_input)(ifp, m);
1273 eh = mtod(m, struct ether_header *);
1274 m_adj(m, sizeof(*eh));
1275 ether_input(ifp, eh, m);
1278 case IEEE80211_FC0_TYPE_MGT:
1279 if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
1280 IEEE80211_FC1_DIR_NODS) {
1284 switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
1285 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1286 case IEEE80211_FC0_SUBTYPE_BEACON:
1287 awi_recv_beacon(sc, m, rxts, rssi);
1289 case IEEE80211_FC0_SUBTYPE_AUTH:
1290 awi_recv_auth(sc, m);
1292 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1293 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1294 awi_recv_asresp(sc, m);
1296 case IEEE80211_FC0_SUBTYPE_DEAUTH:
1297 if (sc->sc_mib_local.Network_Mode)
1298 awi_send_auth(sc, 1);
1300 case IEEE80211_FC0_SUBTYPE_DISASSOC:
1301 if (sc->sc_mib_local.Network_Mode)
1302 awi_send_asreq(sc, 1);
1307 case IEEE80211_FC0_TYPE_CTL:
1309 /* should not come here */
1317 struct awi_softc *sc;
1319 u_int8_t state, rate, rssi;
1321 u_int32_t frame, next, rxts, rxoff;
1324 rxoff = sc->sc_rxdoff;
1326 state = awi_read_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE);
1327 if (state & AWI_RXD_ST_OWN)
1329 if (!(state & AWI_RXD_ST_CONSUMED)) {
1330 if (state & AWI_RXD_ST_RXERROR)
1331 sc->sc_ifp->if_ierrors++;
1333 len = awi_read_2(sc, rxoff + AWI_RXD_LEN);
1334 rate = awi_read_1(sc, rxoff + AWI_RXD_RATE);
1335 rssi = awi_read_1(sc, rxoff + AWI_RXD_RSSI);
1336 frame = awi_read_4(sc, rxoff + AWI_RXD_START_FRAME) & 0x7fff;
1337 rxts = awi_read_4(sc, rxoff + AWI_RXD_LOCALTIME);
1338 m = awi_devget(sc, frame, len);
1339 if (state & AWI_RXD_ST_LF)
1340 awi_input(sc, m, rxts, rssi);
1344 state |= AWI_RXD_ST_CONSUMED;
1345 awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1347 next = awi_read_4(sc, rxoff + AWI_RXD_NEXT);
1348 if (next & AWI_RXD_NEXT_LAST)
1350 /* make sure the next pointer is correct */
1351 if (next != awi_read_4(sc, rxoff + AWI_RXD_NEXT))
1353 state |= AWI_RXD_ST_OWN;
1354 awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1355 rxoff = next & 0x7fff;
1357 sc->sc_rxdoff = rxoff;
1360 static struct mbuf *
1361 awi_devget(sc, off, len)
1362 struct awi_softc *sc;
1367 struct mbuf *top, **mp;
1370 top = sc->sc_rxpend;
1373 sc->sc_rxpend = NULL;
1374 top->m_pkthdr.len += len;
1376 while (*mp != NULL) {
1380 if (m->m_flags & M_EXT)
1381 tlen = m->m_ext.ext_size;
1382 else if (m->m_flags & M_PKTHDR)
1389 awi_read_bytes(sc, off, mtod(m, u_int8_t *) + m->m_len, tlen);
1396 MGETHDR(m, M_DONTWAIT, MT_DATA);
1399 m->m_pkthdr.rcvif = sc->sc_ifp;
1400 m->m_pkthdr.len = len;
1403 MGET(m, M_DONTWAIT, MT_DATA);
1410 if (len >= MINCLSIZE) {
1411 MCLGET(m, M_DONTWAIT);
1412 if (m->m_flags & M_EXT)
1413 m->m_len = m->m_ext.ext_size;
1416 int hdrlen = sizeof(struct ieee80211_frame) +
1417 (sc->sc_format_llc ? sizeof(struct llc) :
1418 sizeof(struct ether_header));
1419 caddr_t newdata = (caddr_t)
1420 ALIGN(m->m_data + hdrlen) - hdrlen;
1421 m->m_len -= newdata - m->m_data;
1422 m->m_data = newdata;
1426 awi_read_bytes(sc, off, mtod(m, u_int8_t *), m->m_len);
1436 * Initialize hardware and start firmware to accept commands.
1437 * Called everytime after power on firmware.
1442 struct awi_softc *sc;
1448 sc->sc_enab_intr = 0;
1449 sc->sc_invalid = 0; /* XXX: really? */
1450 awi_drvstate(sc, AWI_DRV_RESET);
1452 /* reset firmware */
1453 am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1455 awi_write_1(sc, AWI_SELFTEST, 0);
1456 awi_write_1(sc, AWI_CMD, 0);
1457 awi_write_1(sc, AWI_BANNER, 0);
1458 am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1461 /* wait for selftest completion */
1462 for (i = 0; ; i++) {
1463 if (i >= AWI_SELFTEST_TIMEOUT*hz/1000) {
1464 printf("%s: failed to complete selftest (timeout)\n",
1465 sc->sc_dev.dv_xname);
1468 status = awi_read_1(sc, AWI_SELFTEST);
1469 if ((status & 0xf0) == 0xf0)
1471 if (sc->sc_cansleep) {
1473 (void)tsleep(sc, PWAIT, "awitst", 1);
1476 DELAY(1000*1000/hz);
1479 if (status != AWI_SELFTEST_PASSED) {
1480 printf("%s: failed to complete selftest (code %x)\n",
1481 sc->sc_dev.dv_xname, status);
1485 /* check banner to confirm firmware write it */
1486 awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
1487 if (memcmp(sc->sc_banner, "PCnetMobile:", 12) != 0) {
1488 printf("%s: failed to complete selftest (bad banner)\n",
1489 sc->sc_dev.dv_xname);
1490 for (i = 0; i < AWI_BANNER_LEN; i++)
1491 printf("%s%02x", i ? ":" : "\t", sc->sc_banner[i]);
1496 /* initializing interrupt */
1497 sc->sc_enab_intr = 1;
1498 error = awi_intr_lock(sc);
1501 intmask = AWI_INT_GROGGY | AWI_INT_SCAN_CMPLT |
1502 AWI_INT_TX | AWI_INT_RX | AWI_INT_CMD;
1503 awi_write_1(sc, AWI_INTMASK, ~intmask & 0xff);
1504 awi_write_1(sc, AWI_INTMASK2, 0);
1505 awi_write_1(sc, AWI_INTSTAT, 0);
1506 awi_write_1(sc, AWI_INTSTAT2, 0);
1507 awi_intr_unlock(sc);
1508 am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_ENECINT);
1510 /* issueing interface test command */
1511 error = awi_cmd(sc, AWI_CMD_NOP);
1513 printf("%s: failed to complete selftest", sc->sc_dev.dv_xname);
1515 printf(" (no hardware)\n");
1516 else if (error != EWOULDBLOCK)
1517 printf(" (error %d)\n", error);
1518 else if (sc->sc_cansleep)
1519 printf(" (lost interrupt)\n");
1521 printf(" (command timeout)\n");
1527 * Extract the factory default MIB value from firmware and assign the driver
1529 * Called once at attaching the interface.
1534 struct awi_softc *sc;
1539 if ((error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_LOCAL)) != 0 ||
1540 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_ADDR)) != 0 ||
1541 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MAC)) != 0 ||
1542 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MGT)) != 0 ||
1543 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_PHY)) != 0) {
1544 printf("%s: failed to get default mib value (error %d)\n",
1545 sc->sc_dev.dv_xname, error);
1549 rate = sc->sc_mib_phy.aSuprt_Data_Rates;
1550 sc->sc_tx_rate = AWI_RATE_1MBIT;
1551 for (i = 0; i < rate[1]; i++) {
1552 if (AWI_80211_RATE(rate[2 + i]) > sc->sc_tx_rate)
1553 sc->sc_tx_rate = AWI_80211_RATE(rate[2 + i]);
1555 awi_init_region(sc);
1556 memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
1557 sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
1558 sc->sc_mib_local.Fragmentation_Dis = 1;
1559 sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
1560 sc->sc_mib_local.Power_Saving_Mode_Dis = 1;
1562 /* allocate buffers */
1563 sc->sc_txbase = AWI_BUFFERS;
1564 sc->sc_txend = sc->sc_txbase +
1565 (AWI_TXD_SIZE + sizeof(struct ieee80211_frame) +
1566 sizeof(struct ether_header) + ETHERMTU) * AWI_NTXBUFS;
1567 LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Offset, sc->sc_txbase);
1568 LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Size,
1569 sc->sc_txend - sc->sc_txbase);
1570 LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Offset, sc->sc_txend);
1571 LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Size,
1572 AWI_BUFFERS_END - sc->sc_txend);
1573 sc->sc_mib_local.Network_Mode = 1;
1574 sc->sc_mib_local.Acting_as_AP = 0;
1579 * Start transmitter and receiver of firmware
1580 * Called after awi_init_hw() to start operation.
1585 struct awi_softc *sc;
1589 /* start transmitter */
1590 sc->sc_txdone = sc->sc_txnext = sc->sc_txbase;
1591 awi_write_4(sc, sc->sc_txbase + AWI_TXD_START, 0);
1592 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NEXT, 0);
1593 awi_write_4(sc, sc->sc_txbase + AWI_TXD_LENGTH, 0);
1594 awi_write_1(sc, sc->sc_txbase + AWI_TXD_RATE, 0);
1595 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NDA, 0);
1596 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NRA, 0);
1597 awi_write_1(sc, sc->sc_txbase + AWI_TXD_STATE, 0);
1598 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_DATA, sc->sc_txbase);
1599 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_MGT, 0);
1600 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_BCAST, 0);
1601 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_PS, 0);
1602 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_CF, 0);
1603 error = awi_cmd(sc, AWI_CMD_INIT_TX);
1607 /* start receiver */
1608 if (sc->sc_rxpend) {
1609 m_freem(sc->sc_rxpend);
1610 sc->sc_rxpend = NULL;
1612 error = awi_cmd(sc, AWI_CMD_INIT_RX);
1615 sc->sc_rxdoff = awi_read_4(sc, AWI_CMD_PARAMS+AWI_CA_IRX_DATA_DESC);
1616 sc->sc_rxmoff = awi_read_4(sc, AWI_CMD_PARAMS+AWI_CA_IRX_PS_DESC);
1622 struct awi_softc *sc;
1625 if (sc->sc_cmd_inprog)
1626 (void)awi_cmd_wait(sc);
1627 (void)awi_cmd(sc, AWI_CMD_KILL_RX);
1628 (void)awi_cmd_wait(sc);
1629 sc->sc_cmd_inprog = AWI_CMD_FLUSH_TX;
1630 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_DATA, 1);
1631 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_MGT, 0);
1632 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_BCAST, 0);
1633 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_PS, 0);
1634 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_CF, 0);
1635 (void)awi_cmd(sc, AWI_CMD_FLUSH_TX);
1636 (void)awi_cmd_wait(sc);
1641 struct awi_softc *sc;
1644 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1645 switch (sc->sc_mib_phy.aCurrent_Reg_Domain) {
1646 case AWI_REG_DOMAIN_US:
1647 case AWI_REG_DOMAIN_CA:
1648 case AWI_REG_DOMAIN_EU:
1649 sc->sc_scan_min = 0;
1650 sc->sc_scan_max = 77;
1652 case AWI_REG_DOMAIN_ES:
1653 sc->sc_scan_min = 0;
1654 sc->sc_scan_max = 26;
1656 case AWI_REG_DOMAIN_FR:
1657 sc->sc_scan_min = 0;
1658 sc->sc_scan_max = 32;
1660 case AWI_REG_DOMAIN_JP:
1661 sc->sc_scan_min = 6;
1662 sc->sc_scan_max = 17;
1667 sc->sc_scan_set = sc->sc_scan_cur % 3 + 1;
1669 switch (sc->sc_mib_phy.aCurrent_Reg_Domain) {
1670 case AWI_REG_DOMAIN_US:
1671 case AWI_REG_DOMAIN_CA:
1672 sc->sc_scan_min = 1;
1673 sc->sc_scan_max = 11;
1674 sc->sc_scan_cur = 3;
1676 case AWI_REG_DOMAIN_EU:
1677 sc->sc_scan_min = 1;
1678 sc->sc_scan_max = 13;
1679 sc->sc_scan_cur = 3;
1681 case AWI_REG_DOMAIN_ES:
1682 sc->sc_scan_min = 10;
1683 sc->sc_scan_max = 11;
1684 sc->sc_scan_cur = 10;
1686 case AWI_REG_DOMAIN_FR:
1687 sc->sc_scan_min = 10;
1688 sc->sc_scan_max = 13;
1689 sc->sc_scan_cur = 10;
1691 case AWI_REG_DOMAIN_JP:
1692 sc->sc_scan_min = 14;
1693 sc->sc_scan_max = 14;
1694 sc->sc_scan_cur = 14;
1700 sc->sc_ownch = sc->sc_scan_cur;
1706 struct awi_softc *sc;
1711 while ((bp = TAILQ_FIRST(&sc->sc_scan)) != NULL) {
1712 TAILQ_REMOVE(&sc->sc_scan, bp, list);
1715 if (!sc->sc_mib_local.Network_Mode && sc->sc_no_bssid) {
1716 memset(&sc->sc_bss, 0, sizeof(sc->sc_bss));
1717 sc->sc_bss.essid[0] = IEEE80211_ELEMID_SSID;
1718 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1719 sc->sc_bss.chanset = sc->sc_ownch % 3 + 1;
1720 sc->sc_bss.pattern = sc->sc_ownch;
1721 sc->sc_bss.index = 1;
1722 sc->sc_bss.dwell_time = 200; /*XXX*/
1724 sc->sc_bss.chanset = sc->sc_ownch;
1725 sc->sc_status = AWI_ST_SETSS;
1726 error = awi_set_ss(sc);
1728 if (sc->sc_mib_local.Network_Mode)
1729 awi_drvstate(sc, AWI_DRV_INFSC);
1731 awi_drvstate(sc, AWI_DRV_ADHSC);
1732 sc->sc_start_bss = 0;
1733 sc->sc_active_scan = 1;
1734 sc->sc_mgt_timer = AWI_ASCAN_WAIT / 1000;
1735 sc->sc_ifp->if_timer = 1;
1736 sc->sc_status = AWI_ST_SCAN;
1737 error = awi_cmd_scan(sc);
1744 struct awi_softc *sc;
1750 * The pattern parameter for FH phy should be incremented
1751 * by 3. But BayStack 650 Access Points apparently always
1752 * assign hop pattern set parameter to 1 for any pattern.
1753 * So we try all combinations of pattern/set parameters.
1754 * Since this causes no error, it may be a bug of
1755 * PCnetMobile firmware.
1758 if (sc->sc_scan_cur > sc->sc_scan_max) {
1759 sc->sc_scan_cur = sc->sc_scan_min;
1760 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
1761 sc->sc_scan_set = sc->sc_scan_set % 3 + 1;
1763 error = awi_cmd_scan(sc);
1764 if (error != EINVAL)
1772 struct awi_softc *sc;
1774 struct ifnet *ifp = sc->sc_ifp;
1775 struct awi_bss *bp, *sbp;
1778 bp = TAILQ_FIRST(&sc->sc_scan);
1781 if (sc->sc_active_scan) {
1782 if (ifp->if_flags & IFF_DEBUG)
1783 printf("%s: entering passive scan mode\n",
1784 sc->sc_dev.dv_xname);
1785 sc->sc_active_scan = 0;
1787 sc->sc_mgt_timer = AWI_PSCAN_WAIT / 1000;
1789 (void)awi_next_scan(sc);
1793 if (ifp->if_flags & IFF_DEBUG)
1794 printf("%s:\tmacaddr ch/pat sig flag wep essid\n",
1795 sc->sc_dev.dv_xname);
1796 for (; bp != NULL; bp = TAILQ_NEXT(bp, list)) {
1799 * The configuration of the access points may change
1800 * during my scan. So we retries to associate with
1801 * it unless there are any suitable AP.
1803 if (bp->fails++ < 3)
1809 * Since the firmware apparently scans not only the specified
1810 * channel of SCAN command but all available channel within
1811 * the region, we should filter out unnecessary responses here.
1813 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1814 if (bp->pattern < sc->sc_scan_min ||
1815 bp->pattern > sc->sc_scan_max)
1818 if (bp->chanset < sc->sc_scan_min ||
1819 bp->chanset > sc->sc_scan_max)
1822 if (sc->sc_mib_local.Network_Mode) {
1823 if (!(bp->capinfo & IEEE80211_CAPINFO_ESS) ||
1824 (bp->capinfo & IEEE80211_CAPINFO_IBSS))
1827 if ((bp->capinfo & IEEE80211_CAPINFO_ESS) ||
1828 !(bp->capinfo & IEEE80211_CAPINFO_IBSS))
1831 if (sc->sc_wep_algo == NULL) {
1832 if (bp->capinfo & IEEE80211_CAPINFO_PRIVACY)
1835 if (!(bp->capinfo & IEEE80211_CAPINFO_PRIVACY))
1838 if (sc->sc_mib_mac.aDesired_ESS_ID[1] != 0 &&
1839 memcmp(&sc->sc_mib_mac.aDesired_ESS_ID, bp->essid,
1840 sizeof(bp->essid)) != 0)
1842 if (ifp->if_flags & IFF_DEBUG) {
1843 printf(" %c %s", fail ? '-' : '+',
1844 ether_sprintf(bp->esrc));
1845 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
1846 printf(" %2d/%d%c", bp->pattern, bp->chanset,
1847 fail & 0x01 ? '!' : ' ');
1849 printf(" %4d%c", bp->chanset,
1850 fail & 0x01 ? '!' : ' ');
1851 printf(" %+4d", bp->rssi);
1853 (bp->capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
1854 (bp->capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
1856 fail & 0x02 ? '!' : ' ');
1858 (bp->capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" :
1860 fail & 0x04 ? '!' : ' ');
1861 awi_print_essid(bp->essid);
1862 printf("%s\n", fail & 0x08 ? "!" : "");
1865 if (sbp == NULL || bp->rssi > sbp->rssi)
1872 (void)awi_set_ss(sc);
1876 awi_recv_beacon(sc, m0, rxts, rssi)
1877 struct awi_softc *sc;
1882 struct ieee80211_frame *wh;
1884 u_int8_t *frame, *eframe;
1885 u_int8_t *tstamp, *bintval, *capinfo, *ssid, *rates, *parms;
1887 if (sc->sc_status != AWI_ST_SCAN)
1889 wh = mtod(m0, struct ieee80211_frame *);
1891 frame = (u_int8_t *)&wh[1];
1892 eframe = mtod(m0, u_int8_t *) + m0->m_len;
1896 * beacon interval [2]
1897 * capability information [2]
1899 * supported rates [tlv]
1900 * parameter set [tlv]
1903 if (frame + 12 > eframe) {
1906 printf("awi_recv_beacon: frame too short \n");
1917 ssid = rates = parms = NULL;
1918 while (frame < eframe) {
1920 case IEEE80211_ELEMID_SSID:
1923 case IEEE80211_ELEMID_RATES:
1926 case IEEE80211_ELEMID_FHPARMS:
1927 case IEEE80211_ELEMID_DSPARMS:
1931 frame += frame[1] + 2;
1933 if (ssid == NULL || rates == NULL || parms == NULL) {
1936 printf("awi_recv_beacon: ssid=%p, rates=%p, parms=%p\n",
1937 ssid, rates, parms);
1941 if (ssid[1] > IEEE80211_NWID_LEN) {
1944 printf("awi_recv_beacon: bad ssid len: %d from %s\n",
1945 ssid[1], ether_sprintf(wh->i_addr2));
1950 for (bp = TAILQ_FIRST(&sc->sc_scan); bp != NULL;
1951 bp = TAILQ_NEXT(bp, list)) {
1952 if (memcmp(bp->esrc, wh->i_addr2, ETHER_ADDR_LEN) == 0 &&
1953 memcmp(bp->bssid, wh->i_addr3, ETHER_ADDR_LEN) == 0)
1957 bp = malloc(sizeof(struct awi_bss), M_DEVBUF, M_NOWAIT);
1960 TAILQ_INSERT_TAIL(&sc->sc_scan, bp, list);
1961 memcpy(bp->esrc, wh->i_addr2, ETHER_ADDR_LEN);
1962 memcpy(bp->bssid, wh->i_addr3, ETHER_ADDR_LEN);
1963 memset(bp->essid, 0, sizeof(bp->essid));
1964 memcpy(bp->essid, ssid, 2 + ssid[1]);
1968 memcpy(bp->timestamp, tstamp, sizeof(bp->timestamp));
1969 bp->interval = LE_READ_2(bintval);
1970 bp->capinfo = LE_READ_2(capinfo);
1971 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1972 bp->chanset = parms[4];
1973 bp->pattern = parms[5];
1974 bp->index = parms[6];
1975 bp->dwell_time = LE_READ_2(parms + 2);
1977 bp->chanset = parms[2];
1982 if (sc->sc_mgt_timer == 0)
1988 struct awi_softc *sc;
1990 struct ifnet *ifp = sc->sc_ifp;
1994 sc->sc_status = AWI_ST_SETSS;
1996 if (ifp->if_flags & IFF_DEBUG) {
1997 printf("%s: ch %d pat %d id %d dw %d iv %d bss %s ssid ",
1998 sc->sc_dev.dv_xname, bp->chanset,
1999 bp->pattern, bp->index, bp->dwell_time, bp->interval,
2000 ether_sprintf(bp->bssid));
2001 awi_print_essid(bp->essid);
2004 memcpy(&sc->sc_mib_mgt.aCurrent_BSS_ID, bp->bssid, ETHER_ADDR_LEN);
2005 memcpy(&sc->sc_mib_mgt.aCurrent_ESS_ID, bp->essid,
2007 LE_WRITE_2(&sc->sc_mib_mgt.aBeacon_Period, bp->interval);
2008 error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT);
2014 struct awi_softc *sc;
2018 sc->sc_status = AWI_ST_SYNC;
2021 if (sc->sc_cmd_inprog) {
2022 if (awi_cmd_wait(sc))
2025 sc->sc_cmd_inprog = AWI_CMD_SYNC;
2026 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_SET, bp->chanset);
2027 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_PATTERN, bp->pattern);
2028 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_IDX, bp->index);
2029 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_STARTBSS,
2030 sc->sc_start_bss ? 1 : 0);
2031 awi_write_2(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_DWELL, bp->dwell_time);
2032 awi_write_2(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_MBZ, 0);
2033 awi_write_bytes(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_TIMESTAMP,
2035 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_REFTIME, bp->rxtime);
2036 (void)awi_cmd(sc, AWI_CMD_SYNC);
2041 struct awi_softc *sc;
2043 struct ifnet *ifp = sc->sc_ifp;
2045 if (sc->sc_mib_local.Network_Mode) {
2046 awi_drvstate(sc, AWI_DRV_INFSY);
2047 awi_send_auth(sc, 1);
2049 if (ifp->if_flags & IFF_DEBUG) {
2050 printf("%s: synced with", sc->sc_dev.dv_xname);
2051 if (sc->sc_no_bssid)
2052 printf(" no-bssid");
2055 ether_sprintf(sc->sc_bss.bssid));
2056 awi_print_essid(sc->sc_bss.essid);
2058 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
2059 printf(" at chanset %d pattern %d\n",
2060 sc->sc_bss.chanset, sc->sc_bss.pattern);
2062 printf(" at channel %d\n", sc->sc_bss.chanset);
2064 awi_drvstate(sc, AWI_DRV_ADHSY);
2065 sc->sc_status = AWI_ST_RUNNING;
2066 ifp->if_flags |= IFF_RUNNING;
2073 struct awi_softc *sc;
2075 struct ifnet *ifp = sc->sc_ifp;
2077 struct ieee80211_frame *wh;
2080 MGETHDR(m, M_DONTWAIT, MT_DATA);
2083 if (ifp->if_flags & IFF_DEBUG)
2084 printf("%s: sending deauth to %s\n", sc->sc_dev.dv_xname,
2085 ether_sprintf(sc->sc_bss.bssid));
2087 wh = mtod(m, struct ieee80211_frame *);
2088 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2089 IEEE80211_FC0_SUBTYPE_AUTH;
2090 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2091 LE_WRITE_2(wh->i_dur, 0);
2092 LE_WRITE_2(wh->i_seq, 0);
2093 memcpy(wh->i_addr1, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2094 memcpy(wh->i_addr2, sc->sc_mib_addr.aMAC_Address, ETHER_ADDR_LEN);
2095 memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2097 deauth = (u_int8_t *)&wh[1];
2098 LE_WRITE_2(deauth, IEEE80211_REASON_AUTH_LEAVE);
2101 m->m_pkthdr.len = m->m_len = deauth - mtod(m, u_int8_t *);
2102 IF_ENQUEUE(&sc->sc_mgtq, m);
2104 awi_drvstate(sc, AWI_DRV_INFTOSS);
2108 awi_send_auth(sc, seq)
2109 struct awi_softc *sc;
2112 struct ifnet *ifp = sc->sc_ifp;
2114 struct ieee80211_frame *wh;
2117 MGETHDR(m, M_DONTWAIT, MT_DATA);
2120 sc->sc_status = AWI_ST_AUTH;
2121 if (ifp->if_flags & IFF_DEBUG)
2122 printf("%s: sending auth to %s\n", sc->sc_dev.dv_xname,
2123 ether_sprintf(sc->sc_bss.bssid));
2125 wh = mtod(m, struct ieee80211_frame *);
2126 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2127 IEEE80211_FC0_SUBTYPE_AUTH;
2128 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2129 LE_WRITE_2(wh->i_dur, 0);
2130 LE_WRITE_2(wh->i_seq, 0);
2131 memcpy(wh->i_addr1, sc->sc_bss.esrc, ETHER_ADDR_LEN);
2132 memcpy(wh->i_addr2, sc->sc_mib_addr.aMAC_Address, ETHER_ADDR_LEN);
2133 memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2135 auth = (u_int8_t *)&wh[1];
2136 /* algorithm number */
2137 LE_WRITE_2(auth, IEEE80211_AUTH_ALG_OPEN);
2139 /* sequence number */
2140 LE_WRITE_2(auth, seq);
2143 LE_WRITE_2(auth, 0);
2146 m->m_pkthdr.len = m->m_len = auth - mtod(m, u_int8_t *);
2147 IF_ENQUEUE(&sc->sc_mgtq, m);
2150 sc->sc_mgt_timer = AWI_TRANS_TIMEOUT / 1000;
2155 awi_recv_auth(sc, m0)
2156 struct awi_softc *sc;
2159 struct ieee80211_frame *wh;
2160 u_int8_t *auth, *eframe;
2164 wh = mtod(m0, struct ieee80211_frame *);
2165 auth = (u_int8_t *)&wh[1];
2166 eframe = mtod(m0, u_int8_t *) + m0->m_len;
2167 if (sc->sc_ifp->if_flags & IFF_DEBUG)
2168 printf("%s: receive auth from %s\n", sc->sc_dev.dv_xname,
2169 ether_sprintf(wh->i_addr2));
2171 /* algorithm number */
2172 if (LE_READ_2(auth) != IEEE80211_AUTH_ALG_OPEN)
2175 if (!sc->sc_mib_local.Network_Mode) {
2176 if (sc->sc_status != AWI_ST_RUNNING)
2178 if (LE_READ_2(auth) == 1)
2179 awi_send_auth(sc, 2);
2182 if (sc->sc_status != AWI_ST_AUTH)
2184 /* sequence number */
2185 if (LE_READ_2(auth) != 2)
2189 status = LE_READ_2(auth);
2191 printf("%s: authentication failed (reason %d)\n",
2192 sc->sc_dev.dv_xname, status);
2193 for (bp = TAILQ_FIRST(&sc->sc_scan); bp != NULL;
2194 bp = TAILQ_NEXT(bp, list)) {
2195 if (memcmp(bp->esrc, sc->sc_bss.esrc, ETHER_ADDR_LEN)
2203 sc->sc_mgt_timer = 0;
2204 awi_drvstate(sc, AWI_DRV_INFAUTH);
2205 awi_send_asreq(sc, 0);
2209 awi_send_asreq(sc, reassoc)
2210 struct awi_softc *sc;
2213 struct ifnet *ifp = sc->sc_ifp;
2215 struct ieee80211_frame *wh;
2219 MGETHDR(m, M_DONTWAIT, MT_DATA);
2222 sc->sc_status = AWI_ST_ASSOC;
2223 if (ifp->if_flags & IFF_DEBUG)
2224 printf("%s: sending %sassoc req to %s\n", sc->sc_dev.dv_xname,
2225 reassoc ? "re" : "",
2226 ether_sprintf(sc->sc_bss.bssid));
2228 wh = mtod(m, struct ieee80211_frame *);
2229 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT;
2231 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_REASSOC_REQ;
2233 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_ASSOC_REQ;
2234 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2235 LE_WRITE_2(wh->i_dur, 0);
2236 LE_WRITE_2(wh->i_seq, 0);
2237 memcpy(wh->i_addr1, sc->sc_bss.esrc, ETHER_ADDR_LEN);
2238 memcpy(wh->i_addr2, sc->sc_mib_addr.aMAC_Address, ETHER_ADDR_LEN);
2239 memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2241 asreq = (u_int8_t *)&wh[1];
2243 /* capability info */
2244 if (sc->sc_wep_algo == NULL)
2245 LE_WRITE_2(asreq, IEEE80211_CAPINFO_CF_POLLABLE);
2248 IEEE80211_CAPINFO_CF_POLLABLE | IEEE80211_CAPINFO_PRIVACY);
2250 /* listen interval */
2251 lintval = LE_READ_2(&sc->sc_mib_mgt.aListen_Interval);
2252 LE_WRITE_2(asreq, lintval);
2255 /* current AP address */
2256 memcpy(asreq, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2257 asreq += ETHER_ADDR_LEN;
2260 memcpy(asreq, sc->sc_bss.essid, 2 + sc->sc_bss.essid[1]);
2261 asreq += 2 + asreq[1];
2262 /* supported rates */
2263 memcpy(asreq, &sc->sc_mib_phy.aSuprt_Data_Rates, 4);
2264 asreq += 2 + asreq[1];
2266 m->m_pkthdr.len = m->m_len = asreq - mtod(m, u_int8_t *);
2267 IF_ENQUEUE(&sc->sc_mgtq, m);
2270 sc->sc_mgt_timer = AWI_TRANS_TIMEOUT / 1000;
2275 awi_recv_asresp(sc, m0)
2276 struct awi_softc *sc;
2279 struct ieee80211_frame *wh;
2280 u_int8_t *asresp, *eframe;
2282 u_int8_t rate, *phy_rates;
2286 wh = mtod(m0, struct ieee80211_frame *);
2287 asresp = (u_int8_t *)&wh[1];
2288 eframe = mtod(m0, u_int8_t *) + m0->m_len;
2289 if (sc->sc_ifp->if_flags & IFF_DEBUG)
2290 printf("%s: receive assoc resp from %s\n", sc->sc_dev.dv_xname,
2291 ether_sprintf(wh->i_addr2));
2293 if (!sc->sc_mib_local.Network_Mode)
2296 if (sc->sc_status != AWI_ST_ASSOC)
2298 /* capability info */
2301 status = LE_READ_2(asresp);
2303 printf("%s: association failed (reason %d)\n",
2304 sc->sc_dev.dv_xname, status);
2305 for (bp = TAILQ_FIRST(&sc->sc_scan); bp != NULL;
2306 bp = TAILQ_NEXT(bp, list)) {
2307 if (memcmp(bp->esrc, sc->sc_bss.esrc, ETHER_ADDR_LEN)
2316 /* association id */
2318 /* supported rates */
2319 rate = AWI_RATE_1MBIT;
2320 for (i = 0; i < asresp[1]; i++) {
2321 if (AWI_80211_RATE(asresp[2 + i]) <= rate)
2323 phy_rates = sc->sc_mib_phy.aSuprt_Data_Rates;
2324 for (j = 0; j < phy_rates[1]; j++) {
2325 if (AWI_80211_RATE(asresp[2 + i]) ==
2326 AWI_80211_RATE(phy_rates[2 + j]))
2327 rate = AWI_80211_RATE(asresp[2 + i]);
2330 if (sc->sc_ifp->if_flags & IFF_DEBUG) {
2331 printf("%s: associated with %s ssid ",
2332 sc->sc_dev.dv_xname, ether_sprintf(sc->sc_bss.bssid));
2333 awi_print_essid(sc->sc_bss.essid);
2334 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
2335 printf(" chanset %d pattern %d\n",
2336 sc->sc_bss.chanset, sc->sc_bss.pattern);
2338 printf(" channel %d\n", sc->sc_bss.chanset);
2340 sc->sc_tx_rate = rate;
2341 sc->sc_mgt_timer = 0;
2342 sc->sc_rx_timer = 10;
2343 sc->sc_ifp->if_timer = 1;
2344 sc->sc_status = AWI_ST_RUNNING;
2345 sc->sc_ifp->if_flags |= IFF_RUNNING;
2346 awi_drvstate(sc, AWI_DRV_INFASSOC);
2347 awi_start(sc->sc_ifp);
2351 awi_mib(sc, cmd, mib)
2352 struct awi_softc *sc;
2357 u_int8_t size, *ptr;
2361 ptr = (u_int8_t *)&sc->sc_mib_local;
2362 size = sizeof(sc->sc_mib_local);
2365 ptr = (u_int8_t *)&sc->sc_mib_addr;
2366 size = sizeof(sc->sc_mib_addr);
2369 ptr = (u_int8_t *)&sc->sc_mib_mac;
2370 size = sizeof(sc->sc_mib_mac);
2373 ptr = (u_int8_t *)&sc->sc_mib_stat;
2374 size = sizeof(sc->sc_mib_stat);
2377 ptr = (u_int8_t *)&sc->sc_mib_mgt;
2378 size = sizeof(sc->sc_mib_mgt);
2381 ptr = (u_int8_t *)&sc->sc_mib_phy;
2382 size = sizeof(sc->sc_mib_phy);
2387 if (sc->sc_cmd_inprog) {
2388 error = awi_cmd_wait(sc);
2390 if (error == EWOULDBLOCK)
2391 printf("awi_mib: cmd %d inprog",
2396 sc->sc_cmd_inprog = cmd;
2397 if (cmd == AWI_CMD_SET_MIB)
2398 awi_write_bytes(sc, AWI_CMD_PARAMS+AWI_CA_MIB_DATA, ptr, size);
2399 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_MIB_TYPE, mib);
2400 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_MIB_SIZE, size);
2401 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_MIB_INDEX, 0);
2402 error = awi_cmd(sc, cmd);
2405 if (cmd == AWI_CMD_GET_MIB) {
2406 awi_read_bytes(sc, AWI_CMD_PARAMS+AWI_CA_MIB_DATA, ptr, size);
2411 printf("awi_mib: #%d:", mib);
2412 for (i = 0; i < size; i++)
2413 printf(" %02x", ptr[i]);
2423 struct awi_softc *sc;
2428 if (sc->sc_active_scan)
2429 scan_mode = AWI_SCAN_ACTIVE;
2431 scan_mode = AWI_SCAN_PASSIVE;
2432 if (sc->sc_mib_mgt.aScan_Mode != scan_mode) {
2433 sc->sc_mib_mgt.aScan_Mode = scan_mode;
2434 error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT);
2438 if (sc->sc_cmd_inprog) {
2439 error = awi_cmd_wait(sc);
2443 sc->sc_cmd_inprog = AWI_CMD_SCAN;
2444 awi_write_2(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_DURATION,
2445 sc->sc_active_scan ? AWI_ASCAN_DURATION : AWI_PSCAN_DURATION);
2446 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
2447 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_SET,
2449 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_PATTERN,
2451 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_IDX, 1);
2453 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_SET,
2455 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_PATTERN, 0);
2456 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_IDX, 0);
2458 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_SUSP, 0);
2459 return awi_cmd(sc, AWI_CMD_SCAN);
2464 struct awi_softc *sc;
2470 sc->sc_cmd_inprog = cmd;
2471 awi_write_1(sc, AWI_CMD_STATUS, AWI_STAT_IDLE);
2472 awi_write_1(sc, AWI_CMD, cmd);
2473 if (sc->sc_status != AWI_ST_INIT)
2475 error = awi_cmd_wait(sc);
2478 status = awi_read_1(sc, AWI_CMD_STATUS);
2479 awi_write_1(sc, AWI_CMD, 0);
2483 case AWI_STAT_BADPARM:
2486 printf("%s: command %d failed %x\n",
2487 sc->sc_dev.dv_xname, cmd, status);
2495 struct awi_softc *sc;
2497 u_int8_t cmd, status;
2499 status = awi_read_1(sc, AWI_CMD_STATUS);
2500 if (status == AWI_STAT_IDLE)
2501 return; /* stray interrupt */
2503 cmd = sc->sc_cmd_inprog;
2504 sc->sc_cmd_inprog = 0;
2505 if (sc->sc_status == AWI_ST_INIT) {
2509 awi_write_1(sc, AWI_CMD, 0);
2511 if (status != AWI_STAT_OK) {
2512 printf("%s: command %d failed %x\n",
2513 sc->sc_dev.dv_xname, cmd, status);
2516 switch (sc->sc_status) {
2518 if (cmd == AWI_CMD_SET_MIB)
2519 awi_cmd_scan(sc); /* retry */
2533 awi_next_txd(sc, len, framep, ntxdp)
2534 struct awi_softc *sc;
2536 u_int32_t *framep, *ntxdp;
2538 u_int32_t txd, ntxd, frame;
2540 txd = sc->sc_txnext;
2541 frame = txd + AWI_TXD_SIZE;
2542 if (frame + len > sc->sc_txend)
2543 frame = sc->sc_txbase;
2545 if (ntxd + AWI_TXD_SIZE > sc->sc_txend)
2546 ntxd = sc->sc_txbase;
2550 * Determine if there are any room in ring buffer.
2551 * --- send wait, === new data, +++ conflict (ENOBUFS)
2552 * base........................end
2553 * done----txd=====ntxd OK
2554 * --txd=====done++++ntxd-- full
2555 * --txd=====ntxd done-- OK
2556 * ==ntxd done----txd=== OK
2557 * ==done++++ntxd----txd=== full
2558 * ++ntxd txd=====done++ full
2561 if (txd < sc->sc_txdone && ntxd + AWI_TXD_SIZE > sc->sc_txdone)
2564 if (txd < sc->sc_txdone || ntxd + AWI_TXD_SIZE > sc->sc_txdone)
2572 struct awi_softc *sc;
2576 if (curproc == NULL) {
2579 * Though driver ioctl should be called with context,
2580 * KAME ipv6 stack calls ioctl in interrupt for now.
2581 * We simply abort the request if there are other
2582 * ioctl requests in progress.
2590 sc->sc_cansleep = 0;
2593 while (sc->sc_busy) {
2597 error = tsleep(sc, PWAIT | PCATCH, "awilck", 0);
2603 sc->sc_cansleep = 1;
2609 struct awi_softc *sc;
2612 sc->sc_cansleep = 0;
2613 if (sc->sc_sleep_cnt)
2619 struct awi_softc *sc;
2625 for (retry = 0; retry < 10; retry++) {
2626 for (i = 0; i < AWI_LOCKOUT_TIMEOUT*1000/5; i++) {
2627 status = awi_read_1(sc, AWI_LOCKOUT_HOST);
2634 awi_write_1(sc, AWI_LOCKOUT_MAC, 1);
2635 status = awi_read_1(sc, AWI_LOCKOUT_HOST);
2638 awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
2641 printf("%s: failed to lock interrupt\n",
2642 sc->sc_dev.dv_xname);
2650 struct awi_softc *sc;
2653 awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
2658 struct awi_softc *sc;
2663 while (sc->sc_cmd_inprog) {
2666 if (awi_read_1(sc, AWI_CMD) != sc->sc_cmd_inprog) {
2667 printf("%s: failed to access hardware\n",
2668 sc->sc_dev.dv_xname);
2672 if (sc->sc_cansleep) {
2674 error = tsleep(sc, PWAIT, "awicmd",
2675 AWI_CMD_TIMEOUT*hz/1000);
2678 if (awi_read_1(sc, AWI_CMD_STATUS) != AWI_STAT_IDLE) {
2682 if (i++ >= AWI_CMD_TIMEOUT*1000/10)
2683 error = EWOULDBLOCK;
2694 awi_print_essid(essid)
2701 if (len > IEEE80211_NWID_LEN)
2702 len = IEEE80211_NWID_LEN; /*XXX*/
2703 /* determine printable or not */
2704 for (i = 0, p = essid + 2; i < len; i++, p++) {
2705 if (*p < ' ' || *p > 0x7e)
2710 for (i = 0, p = essid + 2; i < len; i++, p++)
2715 for (i = 0, p = essid + 2; i < len; i++, p++)
2722 awi_dump_pkt(sc, m, rssi)
2723 struct awi_softc *sc;
2727 struct ieee80211_frame *wh;
2730 wh = mtod(m, struct ieee80211_frame *);
2732 if (awi_dump_mask != 0 &&
2733 ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK)==IEEE80211_FC1_DIR_NODS) &&
2734 ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK)==IEEE80211_FC0_TYPE_MGT)) {
2735 if ((AWI_DUMP_MASK(wh->i_fc[0]) & awi_dump_mask) != 0)
2738 if (awi_dump_mask < 0 &&
2739 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK)==IEEE80211_FC0_TYPE_DATA)
2746 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
2747 case IEEE80211_FC1_DIR_NODS:
2748 printf("NODS %s", ether_sprintf(wh->i_addr2));
2749 printf("->%s", ether_sprintf(wh->i_addr1));
2750 printf("(%s)", ether_sprintf(wh->i_addr3));
2752 case IEEE80211_FC1_DIR_TODS:
2753 printf("TODS %s", ether_sprintf(wh->i_addr2));
2754 printf("->%s", ether_sprintf(wh->i_addr3));
2755 printf("(%s)", ether_sprintf(wh->i_addr1));
2757 case IEEE80211_FC1_DIR_FROMDS:
2758 printf("FRDS %s", ether_sprintf(wh->i_addr3));
2759 printf("->%s", ether_sprintf(wh->i_addr1));
2760 printf("(%s)", ether_sprintf(wh->i_addr2));
2762 case IEEE80211_FC1_DIR_DSTODS:
2763 printf("DSDS %s", ether_sprintf((u_int8_t *)&wh[1]));
2764 printf("->%s", ether_sprintf(wh->i_addr3));
2765 printf("(%s", ether_sprintf(wh->i_addr2));
2766 printf("->%s)", ether_sprintf(wh->i_addr1));
2769 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
2770 case IEEE80211_FC0_TYPE_DATA:
2773 case IEEE80211_FC0_TYPE_MGT:
2774 switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
2775 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2776 printf(" probe_req");
2778 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2779 printf(" probe_resp");
2781 case IEEE80211_FC0_SUBTYPE_BEACON:
2784 case IEEE80211_FC0_SUBTYPE_AUTH:
2787 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2788 printf(" assoc_req");
2790 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2791 printf(" assoc_resp");
2793 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2794 printf(" reassoc_req");
2796 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2797 printf(" reassoc_resp");
2799 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2802 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2803 printf(" disassoc");
2807 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
2813 wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
2816 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
2819 printf(" +%d", rssi);
2821 if (awi_dump_len > 0) {
2823 if (l > awi_dump_len + sizeof(*wh))
2824 l = awi_dump_len + sizeof(*wh);
2828 for (; i < l; i++) {
2831 printf("%02x", mtod(m, u_int8_t *)[i]);