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.8 2003/11/20 22:07:26 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 "am79c930reg.h"
160 #include "am79c930var.h"
165 static int awi_ioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
167 static int awi_media_rate2opt (struct awi_softc *sc, int rate);
168 static int awi_media_opt2rate (struct awi_softc *sc, int opt);
169 static int awi_media_change (struct ifnet *ifp);
170 static void awi_media_status (struct ifnet *ifp, struct ifmediareq *imr);
172 static void awi_watchdog (struct ifnet *ifp);
173 static void awi_start (struct ifnet *ifp);
174 static void awi_txint (struct awi_softc *sc);
175 static struct mbuf * awi_fix_txhdr (struct awi_softc *sc, struct mbuf *m0);
176 static struct mbuf * awi_fix_rxhdr (struct awi_softc *sc, struct mbuf *m0);
177 static void awi_input (struct awi_softc *sc, struct mbuf *m, u_int32_t rxts, u_int8_t rssi);
178 static void awi_rxint (struct awi_softc *sc);
179 static struct mbuf * awi_devget (struct awi_softc *sc, u_int32_t off, u_int16_t len);
180 static int awi_init_hw (struct awi_softc *sc);
181 static int awi_init_mibs (struct awi_softc *sc);
182 static int awi_init_txrx (struct awi_softc *sc);
183 static void awi_stop_txrx (struct awi_softc *sc);
184 static int awi_start_scan (struct awi_softc *sc);
185 static int awi_next_scan (struct awi_softc *sc);
186 static void awi_stop_scan (struct awi_softc *sc);
187 static void awi_recv_beacon (struct awi_softc *sc, struct mbuf *m0, u_int32_t rxts, u_int8_t rssi);
188 static int awi_set_ss (struct awi_softc *sc);
189 static void awi_try_sync (struct awi_softc *sc);
190 static void awi_sync_done (struct awi_softc *sc);
191 static void awi_send_deauth (struct awi_softc *sc);
192 static void awi_send_auth (struct awi_softc *sc, int seq);
193 static void awi_recv_auth (struct awi_softc *sc, struct mbuf *m0);
194 static void awi_send_asreq (struct awi_softc *sc, int reassoc);
195 static void awi_recv_asresp (struct awi_softc *sc, struct mbuf *m0);
196 static int awi_mib (struct awi_softc *sc, u_int8_t cmd, u_int8_t mib);
197 static int awi_cmd_scan (struct awi_softc *sc);
198 static int awi_cmd (struct awi_softc *sc, u_int8_t cmd);
199 static void awi_cmd_done (struct awi_softc *sc);
200 static int awi_next_txd (struct awi_softc *sc, int len, u_int32_t *framep, u_int32_t*ntxdp);
201 static int awi_lock (struct awi_softc *sc);
202 static void awi_unlock (struct awi_softc *sc);
203 static int awi_intr_lock (struct awi_softc *sc);
204 static void awi_intr_unlock (struct awi_softc *sc);
205 static int awi_cmd_wait (struct awi_softc *sc);
206 static void awi_print_essid (u_int8_t *essid);
208 DECLARE_DUMMY_MODULE(if_awi);
211 static void awi_dump_pkt (struct awi_softc *sc, struct mbuf *m, int rssi);
214 #define AWI_DUMP_MASK(fc0) (1 << (((fc0) & IEEE80211_FC0_SUBTYPE_MASK) >> 4))
215 int awi_dump_mask = AWI_DUMP_MASK(IEEE80211_FC0_SUBTYPE_BEACON);
216 int awi_dump_hdr = 0;
217 int awi_dump_len = 28;
221 #define AWI_BPF_NORM 0
222 #define AWI_BPF_RAW 1
224 #define AWI_BPF_MTAP(sc, m, raw) do { \
225 if ((sc)->sc_ifp->if_bpf && (sc)->sc_rawbpf == (raw)) \
226 bpf_mtap((sc)->sc_ifp, (m)); \
229 #define AWI_BPF_MTAP(sc, m, raw) do { \
230 if ((sc)->sc_ifp->if_bpf && (sc)->sc_rawbpf == (raw)) \
231 bpf_mtap((sc)->sc_ifp->if_bpf, (m)); \
235 #define AWI_BPF_MTAP(sc, m, raw)
239 #define llc_snap llc_un.type_snap
244 devclass_t awi_devclass;
247 /* NetBSD compatible functions */
248 static char * ether_sprintf (u_int8_t *);
251 ether_sprintf(enaddr)
254 static char strbuf[18];
256 sprintf(strbuf, "%6D", enaddr, ":");
263 struct awi_softc *sc;
265 struct ifnet *ifp = sc->sc_ifp;
272 struct ifmediareq imr;
277 * Even if we can sleep in initialization state,
278 * all other processes (e.g. ifconfig) have to wait for
279 * completion of attaching interface.
282 sc->sc_status = AWI_ST_INIT;
283 TAILQ_INIT(&sc->sc_scan);
284 error = awi_init_hw(sc);
290 error = awi_init_mibs(sc);
298 ifp->if_start = awi_start;
299 ifp->if_ioctl = awi_ioctl;
300 ifp->if_watchdog = awi_watchdog;
301 ifp->if_mtu = ETHERMTU;
302 ifp->if_hdrlen = sizeof(struct ieee80211_frame) +
303 sizeof(struct ether_header);
304 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
305 #ifdef IFF_NOTRAILERS
306 ifp->if_flags |= IFF_NOTRAILERS;
309 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
312 ifp->if_output = ether_output;
313 ifp->if_snd.ifq_maxlen = ifqmaxlen;
314 memcpy(sc->sc_ec.ac_enaddr, sc->sc_mib_addr.aMAC_Address,
318 printf("%s: IEEE802.11 %s %dMbps (firmware %s)\n",
320 sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH ? "FH" : "DS",
321 sc->sc_tx_rate / 10, sc->sc_banner);
322 printf("%s: address %s\n",
323 sc->sc_dev.dv_xname, ether_sprintf(sc->sc_mib_addr.aMAC_Address));
325 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
328 ether_ifattach(ifp, sc->sc_mib_addr.aMAC_Address);
330 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
335 ifmedia_init(&sc->sc_media, 0, awi_media_change, awi_media_status);
336 phy_rates = sc->sc_mib_phy.aSuprt_Data_Rates;
337 for (i = 0; i < phy_rates[1]; i++) {
338 mword = awi_media_rate2opt(sc, AWI_80211_RATE(phy_rates[2 + i]));
341 mword |= IFM_IEEE80211;
342 ifmedia_add(&sc->sc_media, mword, 0, NULL);
343 ifmedia_add(&sc->sc_media,
344 mword | IFM_IEEE80211_ADHOC, 0, NULL);
345 if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_FH)
346 ifmedia_add(&sc->sc_media,
347 mword | IFM_IEEE80211_ADHOC | IFM_FLAG0, 0, NULL);
349 awi_media_status(ifp, &imr);
350 ifmedia_set(&sc->sc_media, imr.ifm_active);
353 /* ready to accept ioctl */
356 /* Attach is successful. */
364 struct awi_softc *sc;
366 struct ifnet *ifp = sc->sc_ifp;
369 /* Succeed if there is no work to do. */
370 if (!sc->sc_attached)
376 while (sc->sc_sleep_cnt > 0) {
378 (void)tsleep(sc, 0, "awidet", 1);
380 if (sc->sc_wep_ctx != NULL)
381 free(sc->sc_wep_ctx, M_DEVBUF);
386 ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY);
390 if (sc->sc_enabled) {
392 (*sc->sc_disable)(sc);
400 awi_activate(self, act)
404 struct awi_softc *sc = (struct awi_softc *)self;
413 case DVACT_DEACTIVATE:
416 if_deactivate(sc->sc_ifp);
426 struct awi_softc *sc;
436 ocansleep = sc->sc_cansleep;
438 #ifdef needtobefixed /*ONOE*/
439 if (why == PWR_RESUME) {
446 (*sc->sc_disable)(sc);
449 sc->sc_cansleep = ocansleep;
452 #endif /* __NetBSD__ */
455 awi_ioctl(ifp, cmd, data)
460 struct thread *td = curthread;
461 struct awi_softc *sc = ifp->if_softc;
462 struct ifreq *ifr = (struct ifreq *)data;
463 struct ifaddr *ifa = (struct ifaddr *)data;
465 struct ieee80211_nwid nwid;
470 /* serialize ioctl */
471 error = awi_lock(sc);
476 ifp->if_flags |= IFF_UP;
477 switch (ifa->ifa_addr->sa_family) {
480 arp_ifinit((void *)ifp, ifa);
486 sc->sc_format_llc = !(ifp->if_flags & IFF_LINK0);
487 if (!(ifp->if_flags & IFF_UP)) {
488 if (sc->sc_enabled) {
491 (*sc->sc_disable)(sc);
496 error = awi_init(sc);
502 error = ENETRESET; /*XXX*/
504 error = (cmd == SIOCADDMULTI) ?
505 ether_addmulti(ifr, &sc->sc_ec) :
506 ether_delmulti(ifr, &sc->sc_ec);
509 * Do not rescan BSS. Rather, just reset multicast filter.
511 if (error == ENETRESET) {
513 error = awi_init(sc);
519 if (ifr->ifr_mtu > ETHERMTU)
522 ifp->if_mtu = ifr->ifr_mtu;
526 error = suser(td); /* EPERM if no proc */
530 error = copyin(ifr->ifr_data, &nwid, sizeof(nwid));
533 if (nwid.i_len > IEEE80211_NWID_LEN) {
537 if (sc->sc_mib_mac.aDesired_ESS_ID[1] == nwid.i_len &&
538 memcmp(&sc->sc_mib_mac.aDesired_ESS_ID[2], nwid.i_nwid,
541 memset(sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
542 sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
543 sc->sc_mib_mac.aDesired_ESS_ID[1] = nwid.i_len;
544 memcpy(&sc->sc_mib_mac.aDesired_ESS_ID[2], nwid.i_nwid,
546 if (sc->sc_enabled) {
548 error = awi_init(sc);
552 if (ifp->if_flags & IFF_RUNNING)
553 p = sc->sc_bss.essid;
555 p = sc->sc_mib_mac.aDesired_ESS_ID;
556 error = copyout(p + 1, ifr->ifr_data, 1 + IEEE80211_NWID_LEN);
558 case SIOCS80211NWKEY:
560 error = suser(td); /* EPERM if no proc */
564 error = awi_wep_setnwkey(sc, (struct ieee80211_nwkey *)data);
566 case SIOCG80211NWKEY:
567 error = awi_wep_getnwkey(sc, (struct ieee80211_nwkey *)data);
572 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
576 error = awi_wicfg(ifp, cmd, data);
587 awi_media_rate2opt(sc, rate)
588 struct awi_softc *sc;
596 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
597 mword = IFM_IEEE80211_FH1;
599 mword = IFM_IEEE80211_DS1;
602 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
603 mword = IFM_IEEE80211_FH2;
605 mword = IFM_IEEE80211_DS2;
608 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_DS)
609 mword = IFM_IEEE80211_DS5;
612 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_DS)
613 mword = IFM_IEEE80211_DS11;
620 awi_media_opt2rate(sc, opt)
621 struct awi_softc *sc;
627 switch (IFM_SUBTYPE(opt)) {
628 case IFM_IEEE80211_FH1:
629 case IFM_IEEE80211_FH2:
630 if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_FH)
633 case IFM_IEEE80211_DS1:
634 case IFM_IEEE80211_DS2:
635 case IFM_IEEE80211_DS5:
636 case IFM_IEEE80211_DS11:
637 if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_DS)
642 switch (IFM_SUBTYPE(opt)) {
643 case IFM_IEEE80211_FH1:
644 case IFM_IEEE80211_DS1:
647 case IFM_IEEE80211_FH2:
648 case IFM_IEEE80211_DS2:
651 case IFM_IEEE80211_DS5:
654 case IFM_IEEE80211_DS11:
662 * Called from ifmedia_ioctl via awi_ioctl with lock obtained.
665 awi_media_change(ifp)
668 struct awi_softc *sc = ifp->if_softc;
669 struct ifmedia_entry *ime;
674 ime = sc->sc_media.ifm_cur;
675 rate = awi_media_opt2rate(sc, ime->ifm_media);
678 if (rate != sc->sc_tx_rate) {
679 phy_rates = sc->sc_mib_phy.aSuprt_Data_Rates;
680 for (i = 0; i < phy_rates[1]; i++) {
681 if (rate == AWI_80211_RATE(phy_rates[2 + i]))
684 if (i == phy_rates[1])
687 if (ime->ifm_media & IFM_IEEE80211_ADHOC) {
688 sc->sc_mib_local.Network_Mode = 0;
689 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
692 sc->sc_no_bssid = (ime->ifm_media & IFM_FLAG0) ? 1 : 0;
694 sc->sc_mib_local.Network_Mode = 1;
696 if (sc->sc_enabled) {
698 error = awi_init(sc);
704 awi_media_status(ifp, imr)
706 struct ifmediareq *imr;
708 struct awi_softc *sc = ifp->if_softc;
710 imr->ifm_status = IFM_AVALID;
711 if (ifp->if_flags & IFF_RUNNING)
712 imr->ifm_status |= IFM_ACTIVE;
713 imr->ifm_active = IFM_IEEE80211;
714 imr->ifm_active |= awi_media_rate2opt(sc, sc->sc_tx_rate);
715 if (sc->sc_mib_local.Network_Mode == 0) {
716 imr->ifm_active |= IFM_IEEE80211_ADHOC;
718 imr->ifm_active |= IFM_FLAG0;
721 #endif /* IFM_IEEE80211 */
727 struct awi_softc *sc = arg;
729 int error, handled = 0, ocansleep;
731 if (!sc->sc_enabled || !sc->sc_enab_intr || sc->sc_invalid)
734 am79c930_gcr_setbits(&sc->sc_chip,
735 AM79C930_GCR_DISPWDN | AM79C930_GCR_ECINT);
736 awi_write_1(sc, AWI_DIS_PWRDN, 1);
737 ocansleep = sc->sc_cansleep;
741 error = awi_intr_lock(sc);
744 status = awi_read_1(sc, AWI_INTSTAT);
745 awi_write_1(sc, AWI_INTSTAT, 0);
746 awi_write_1(sc, AWI_INTSTAT, 0);
747 status |= awi_read_1(sc, AWI_INTSTAT2) << 8;
748 awi_write_1(sc, AWI_INTSTAT2, 0);
751 if (!sc->sc_cmd_inprog)
752 status &= ~AWI_INT_CMD; /* make sure */
756 if (status & AWI_INT_RX)
758 if (status & AWI_INT_TX)
760 if (status & AWI_INT_CMD)
762 if (status & AWI_INT_SCAN_CMPLT) {
763 if (sc->sc_status == AWI_ST_SCAN &&
764 sc->sc_mgt_timer > 0)
765 (void)awi_next_scan(sc);
768 sc->sc_cansleep = ocansleep;
769 am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_DISPWDN);
770 awi_write_1(sc, AWI_DIS_PWRDN, 0);
776 struct awi_softc *sc;
780 struct ifnet *ifp = sc->sc_ifp;
782 struct ifmultiaddr *ifma;
784 struct ether_multi *enm;
785 struct ether_multistep step;
788 /* reinitialize muticast filter */
790 ifp->if_flags |= IFF_ALLMULTI;
791 sc->sc_mib_local.Accept_All_Multicast_Dis = 0;
792 if (ifp->if_flags & IFF_PROMISC) {
793 sc->sc_mib_mac.aPromiscuous_Enable = 1;
796 sc->sc_mib_mac.aPromiscuous_Enable = 0;
798 if (ifp->if_amcount != 0)
800 for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma != NULL;
801 ifma = LIST_NEXT(ifma, ifma_link)) {
802 if (ifma->ifma_addr->sa_family != AF_LINK)
804 if (n == AWI_GROUP_ADDR_SIZE)
806 memcpy(sc->sc_mib_addr.aGroup_Addresses[n],
807 LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
812 ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
813 while (enm != NULL) {
814 if (n == AWI_GROUP_ADDR_SIZE ||
815 memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)
818 memcpy(sc->sc_mib_addr.aGroup_Addresses[n], enm->enm_addrlo,
821 ETHER_NEXT_MULTI(step, enm);
824 for (; n < AWI_GROUP_ADDR_SIZE; n++)
825 memset(sc->sc_mib_addr.aGroup_Addresses[n], 0, ETHER_ADDR_LEN);
826 ifp->if_flags &= ~IFF_ALLMULTI;
827 sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
830 #ifdef notdef /* allow non-encrypted frame for receiving. */
831 sc->sc_mib_mgt.Wep_Required = sc->sc_wep_algo != NULL ? 1 : 0;
833 if (!sc->sc_enabled) {
836 (*sc->sc_enable)(sc);
837 sc->sc_status = AWI_ST_INIT;
838 error = awi_init_hw(sc);
842 ostatus = sc->sc_status;
843 sc->sc_status = AWI_ST_INIT;
844 if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_LOCAL)) != 0 ||
845 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_ADDR)) != 0 ||
846 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MAC)) != 0 ||
847 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT)) != 0 ||
848 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_PHY)) != 0) {
852 if (ifp->if_flags & IFF_RUNNING)
853 sc->sc_status = AWI_ST_RUNNING;
855 if (ostatus == AWI_ST_INIT) {
856 error = awi_init_txrx(sc);
860 error = awi_start_scan(sc);
867 struct awi_softc *sc;
869 struct ifnet *ifp = sc->sc_ifp;
873 sc->sc_status = AWI_ST_INIT;
874 if (!sc->sc_invalid) {
875 (void)awi_cmd_wait(sc);
876 if (sc->sc_mib_local.Network_Mode &&
877 sc->sc_status > AWI_ST_AUTH)
881 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
883 sc->sc_tx_timer = sc->sc_rx_timer = sc->sc_mgt_timer = 0;
885 IF_DEQUEUE(&sc->sc_mgtq, m);
891 IF_DEQUEUE(&ifp->if_snd, m);
896 while ((bp = TAILQ_FIRST(&sc->sc_scan)) != NULL) {
897 TAILQ_REMOVE(&sc->sc_scan, bp, list);
906 struct awi_softc *sc = ifp->if_softc;
909 if (sc->sc_invalid) {
914 ocansleep = sc->sc_cansleep;
916 if (sc->sc_tx_timer && --sc->sc_tx_timer == 0) {
917 printf("%s: transmit timeout\n", sc->sc_dev.dv_xname);
920 if (sc->sc_rx_timer && --sc->sc_rx_timer == 0) {
921 if (ifp->if_flags & IFF_DEBUG) {
922 printf("%s: no recent beacons from %s; rescanning\n",
924 ether_sprintf(sc->sc_bss.bssid));
926 ifp->if_flags &= ~IFF_RUNNING;
929 if (sc->sc_mgt_timer && --sc->sc_mgt_timer == 0) {
930 switch (sc->sc_status) {
944 if (sc->sc_tx_timer == 0 && sc->sc_rx_timer == 0 &&
945 sc->sc_mgt_timer == 0)
949 sc->sc_cansleep = ocansleep;
956 struct awi_softc *sc = ifp->if_softc;
958 u_int32_t txd, frame, ntxd;
964 IF_DEQUEUE(&sc->sc_mgtq, m0);
966 if (awi_next_txd(sc, m0->m_pkthdr.len, &frame, &ntxd)) {
967 IF_PREPEND(&sc->sc_mgtq, m0);
968 ifp->if_flags |= IFF_OACTIVE;
972 if (!(ifp->if_flags & IFF_RUNNING))
974 IF_DEQUEUE(&ifp->if_snd, m0);
977 len = m0->m_pkthdr.len + sizeof(struct ieee80211_frame);
978 if (sc->sc_format_llc)
979 len += sizeof(struct llc) -
980 sizeof(struct ether_header);
981 if (sc->sc_wep_algo != NULL)
982 len += IEEE80211_WEP_IVLEN +
983 IEEE80211_WEP_KIDLEN + IEEE80211_WEP_CRCLEN;
984 if (awi_next_txd(sc, len, &frame, &ntxd)) {
985 IF_PREPEND(&ifp->if_snd, m0);
986 ifp->if_flags |= IFF_OACTIVE;
989 AWI_BPF_MTAP(sc, m0, AWI_BPF_NORM);
990 m0 = awi_fix_txhdr(sc, m0);
991 if (sc->sc_wep_algo != NULL && m0 != NULL)
992 m0 = awi_wep_encrypt(sc, m0, 1);
1001 awi_dump_pkt(sc, m0, -1);
1003 AWI_BPF_MTAP(sc, m0, AWI_BPF_RAW);
1005 for (m = m0; m != NULL; m = m->m_next) {
1006 awi_write_bytes(sc, frame + len, mtod(m, u_int8_t *),
1011 rate = sc->sc_tx_rate; /*XXX*/
1012 awi_write_1(sc, ntxd + AWI_TXD_STATE, 0);
1013 awi_write_4(sc, txd + AWI_TXD_START, frame);
1014 awi_write_4(sc, txd + AWI_TXD_NEXT, ntxd);
1015 awi_write_4(sc, txd + AWI_TXD_LENGTH, len);
1016 awi_write_1(sc, txd + AWI_TXD_RATE, rate);
1017 awi_write_4(sc, txd + AWI_TXD_NDA, 0);
1018 awi_write_4(sc, txd + AWI_TXD_NRA, 0);
1019 awi_write_1(sc, txd + AWI_TXD_STATE, AWI_TXD_ST_OWN);
1020 sc->sc_txnext = ntxd;
1024 if (sc->sc_tx_timer == 0)
1025 sc->sc_tx_timer = 5;
1029 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);
1036 struct awi_softc *sc;
1038 struct ifnet *ifp = sc->sc_ifp;
1041 while (sc->sc_txdone != sc->sc_txnext) {
1042 flags = awi_read_1(sc, sc->sc_txdone + AWI_TXD_STATE);
1043 if ((flags & AWI_TXD_ST_OWN) || !(flags & AWI_TXD_ST_DONE))
1045 if (flags & AWI_TXD_ST_ERROR)
1047 sc->sc_txdone = awi_read_4(sc, sc->sc_txdone + AWI_TXD_NEXT) &
1050 sc->sc_tx_timer = 0;
1051 ifp->if_flags &= ~IFF_OACTIVE;
1054 printf("awi_txint: txdone %d txnext %d txbase %d txend %d\n",
1055 sc->sc_txdone, sc->sc_txnext, sc->sc_txbase, sc->sc_txend);
1060 static struct mbuf *
1061 awi_fix_txhdr(sc, m0)
1062 struct awi_softc *sc;
1065 struct ether_header eh;
1066 struct ieee80211_frame *wh;
1069 if (m0->m_len < sizeof(eh)) {
1070 m0 = m_pullup(m0, sizeof(eh));
1074 memcpy(&eh, mtod(m0, caddr_t), sizeof(eh));
1075 if (sc->sc_format_llc) {
1076 m_adj(m0, sizeof(struct ether_header) - sizeof(struct llc));
1077 llc = mtod(m0, struct llc *);
1078 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1079 llc->llc_control = LLC_UI;
1080 llc->llc_snap.org_code[0] = llc->llc_snap.org_code[1] =
1081 llc->llc_snap.org_code[2] = 0;
1082 llc->llc_snap.ether_type = eh.ether_type;
1084 M_PREPEND(m0, sizeof(struct ieee80211_frame), M_DONTWAIT);
1087 wh = mtod(m0, struct ieee80211_frame *);
1089 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1090 LE_WRITE_2(wh->i_dur, 0);
1091 LE_WRITE_2(wh->i_seq, 0);
1092 if (sc->sc_mib_local.Network_Mode) {
1093 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1094 memcpy(wh->i_addr1, sc->sc_bss.bssid, ETHER_ADDR_LEN);
1095 memcpy(wh->i_addr2, eh.ether_shost, ETHER_ADDR_LEN);
1096 memcpy(wh->i_addr3, eh.ether_dhost, ETHER_ADDR_LEN);
1098 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1099 memcpy(wh->i_addr1, eh.ether_dhost, ETHER_ADDR_LEN);
1100 memcpy(wh->i_addr2, eh.ether_shost, ETHER_ADDR_LEN);
1101 memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
1106 static struct mbuf *
1107 awi_fix_rxhdr(sc, m0)
1108 struct awi_softc *sc;
1111 struct ieee80211_frame wh;
1112 struct ether_header *eh;
1115 if (m0->m_len < sizeof(wh)) {
1119 llc = (struct llc *)(mtod(m0, caddr_t) + sizeof(wh));
1120 if (llc->llc_dsap == LLC_SNAP_LSAP &&
1121 llc->llc_ssap == LLC_SNAP_LSAP &&
1122 llc->llc_control == LLC_UI &&
1123 llc->llc_snap.org_code[0] == 0 &&
1124 llc->llc_snap.org_code[1] == 0 &&
1125 llc->llc_snap.org_code[2] == 0) {
1126 memcpy(&wh, mtod(m0, caddr_t), sizeof(wh));
1127 m_adj(m0, sizeof(wh) + sizeof(*llc) - sizeof(*eh));
1128 eh = mtod(m0, struct ether_header *);
1129 switch (wh.i_fc[1] & IEEE80211_FC1_DIR_MASK) {
1130 case IEEE80211_FC1_DIR_NODS:
1131 memcpy(eh->ether_dhost, wh.i_addr1, ETHER_ADDR_LEN);
1132 memcpy(eh->ether_shost, wh.i_addr2, ETHER_ADDR_LEN);
1134 case IEEE80211_FC1_DIR_TODS:
1135 memcpy(eh->ether_dhost, wh.i_addr3, ETHER_ADDR_LEN);
1136 memcpy(eh->ether_shost, wh.i_addr2, ETHER_ADDR_LEN);
1138 case IEEE80211_FC1_DIR_FROMDS:
1139 memcpy(eh->ether_dhost, wh.i_addr1, ETHER_ADDR_LEN);
1140 memcpy(eh->ether_shost, wh.i_addr3, ETHER_ADDR_LEN);
1142 case IEEE80211_FC1_DIR_DSTODS:
1147 /* assuming ethernet encapsulation, just strip 802.11 header */
1148 m_adj(m0, sizeof(wh));
1150 if (ALIGN(mtod(m0, caddr_t) + sizeof(struct ether_header)) !=
1151 (u_int)(mtod(m0, caddr_t) + sizeof(struct ether_header))) {
1152 /* XXX: we loose to estimate the type of encapsulation */
1153 struct mbuf *n, *n0, **np;
1160 while (m0->m_pkthdr.len > off) {
1162 MGETHDR(n, M_DONTWAIT, MT_DATA);
1167 M_MOVE_PKTHDR(n, m0);
1170 MGET(n, M_DONTWAIT, MT_DATA);
1178 if (m0->m_pkthdr.len - off >= MINCLSIZE) {
1179 MCLGET(n, M_DONTWAIT);
1180 if (n->m_flags & M_EXT)
1181 n->m_len = n->m_ext.ext_size;
1186 + sizeof(struct ether_header))
1187 - sizeof(struct ether_header);
1188 n->m_len -= newdata - n->m_data;
1189 n->m_data = newdata;
1191 if (n->m_len > m0->m_pkthdr.len - off)
1192 n->m_len = m0->m_pkthdr.len - off;
1193 m_copydata(m0, off, n->m_len, mtod(n, caddr_t));
1205 awi_input(sc, m, rxts, rssi)
1206 struct awi_softc *sc;
1211 struct ifnet *ifp = sc->sc_ifp;
1212 struct ieee80211_frame *wh;
1214 struct ether_header *eh;
1217 /* trim CRC here for WEP can find its own CRC at the end of packet. */
1218 m_adj(m, -ETHER_CRC_LEN);
1219 AWI_BPF_MTAP(sc, m, AWI_BPF_RAW);
1220 wh = mtod(m, struct ieee80211_frame *);
1221 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
1222 IEEE80211_FC0_VERSION_0) {
1223 printf("%s; receive packet with wrong version: %x\n",
1224 sc->sc_dev.dv_xname, wh->i_fc[0]);
1229 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1230 m = awi_wep_encrypt(sc, m, 0);
1235 wh = mtod(m, struct ieee80211_frame *);
1239 awi_dump_pkt(sc, m, rssi);
1242 if ((sc->sc_mib_local.Network_Mode || !sc->sc_no_bssid) &&
1243 sc->sc_status == AWI_ST_RUNNING) {
1244 if (memcmp(wh->i_addr2, sc->sc_bss.bssid, ETHER_ADDR_LEN) == 0) {
1245 sc->sc_rx_timer = 10;
1246 sc->sc_bss.rssi = rssi;
1249 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1250 case IEEE80211_FC0_TYPE_DATA:
1251 if (sc->sc_mib_local.Network_Mode) {
1252 if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
1253 IEEE80211_FC1_DIR_FROMDS) {
1258 if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
1259 IEEE80211_FC1_DIR_NODS) {
1264 m = awi_fix_rxhdr(sc, m);
1270 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 4)
1271 AWI_BPF_MTAP(sc, m, AWI_BPF_NORM);
1274 (*ifp->if_input)(ifp, m);
1276 eh = mtod(m, struct ether_header *);
1277 m_adj(m, sizeof(*eh));
1278 ether_input(ifp, eh, m);
1281 case IEEE80211_FC0_TYPE_MGT:
1282 if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
1283 IEEE80211_FC1_DIR_NODS) {
1287 switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
1288 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1289 case IEEE80211_FC0_SUBTYPE_BEACON:
1290 awi_recv_beacon(sc, m, rxts, rssi);
1292 case IEEE80211_FC0_SUBTYPE_AUTH:
1293 awi_recv_auth(sc, m);
1295 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1296 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1297 awi_recv_asresp(sc, m);
1299 case IEEE80211_FC0_SUBTYPE_DEAUTH:
1300 if (sc->sc_mib_local.Network_Mode)
1301 awi_send_auth(sc, 1);
1303 case IEEE80211_FC0_SUBTYPE_DISASSOC:
1304 if (sc->sc_mib_local.Network_Mode)
1305 awi_send_asreq(sc, 1);
1310 case IEEE80211_FC0_TYPE_CTL:
1312 /* should not come here */
1320 struct awi_softc *sc;
1322 u_int8_t state, rate, rssi;
1324 u_int32_t frame, next, rxts, rxoff;
1327 rxoff = sc->sc_rxdoff;
1329 state = awi_read_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE);
1330 if (state & AWI_RXD_ST_OWN)
1332 if (!(state & AWI_RXD_ST_CONSUMED)) {
1333 if (state & AWI_RXD_ST_RXERROR)
1334 sc->sc_ifp->if_ierrors++;
1336 len = awi_read_2(sc, rxoff + AWI_RXD_LEN);
1337 rate = awi_read_1(sc, rxoff + AWI_RXD_RATE);
1338 rssi = awi_read_1(sc, rxoff + AWI_RXD_RSSI);
1339 frame = awi_read_4(sc, rxoff + AWI_RXD_START_FRAME) & 0x7fff;
1340 rxts = awi_read_4(sc, rxoff + AWI_RXD_LOCALTIME);
1341 m = awi_devget(sc, frame, len);
1342 if (state & AWI_RXD_ST_LF)
1343 awi_input(sc, m, rxts, rssi);
1347 state |= AWI_RXD_ST_CONSUMED;
1348 awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1350 next = awi_read_4(sc, rxoff + AWI_RXD_NEXT);
1351 if (next & AWI_RXD_NEXT_LAST)
1353 /* make sure the next pointer is correct */
1354 if (next != awi_read_4(sc, rxoff + AWI_RXD_NEXT))
1356 state |= AWI_RXD_ST_OWN;
1357 awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1358 rxoff = next & 0x7fff;
1360 sc->sc_rxdoff = rxoff;
1363 static struct mbuf *
1364 awi_devget(sc, off, len)
1365 struct awi_softc *sc;
1370 struct mbuf *top, **mp;
1373 top = sc->sc_rxpend;
1376 sc->sc_rxpend = NULL;
1377 top->m_pkthdr.len += len;
1379 while (*mp != NULL) {
1383 if (m->m_flags & M_EXT)
1384 tlen = m->m_ext.ext_size;
1385 else if (m->m_flags & M_PKTHDR)
1392 awi_read_bytes(sc, off, mtod(m, u_int8_t *) + m->m_len, tlen);
1399 MGETHDR(m, M_DONTWAIT, MT_DATA);
1402 m->m_pkthdr.rcvif = sc->sc_ifp;
1403 m->m_pkthdr.len = len;
1406 MGET(m, M_DONTWAIT, MT_DATA);
1413 if (len >= MINCLSIZE) {
1414 MCLGET(m, M_DONTWAIT);
1415 if (m->m_flags & M_EXT)
1416 m->m_len = m->m_ext.ext_size;
1419 int hdrlen = sizeof(struct ieee80211_frame) +
1420 (sc->sc_format_llc ? sizeof(struct llc) :
1421 sizeof(struct ether_header));
1422 caddr_t newdata = (caddr_t)
1423 ALIGN(m->m_data + hdrlen) - hdrlen;
1424 m->m_len -= newdata - m->m_data;
1425 m->m_data = newdata;
1429 awi_read_bytes(sc, off, mtod(m, u_int8_t *), m->m_len);
1439 * Initialize hardware and start firmware to accept commands.
1440 * Called everytime after power on firmware.
1445 struct awi_softc *sc;
1451 sc->sc_enab_intr = 0;
1452 sc->sc_invalid = 0; /* XXX: really? */
1453 awi_drvstate(sc, AWI_DRV_RESET);
1455 /* reset firmware */
1456 am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1458 awi_write_1(sc, AWI_SELFTEST, 0);
1459 awi_write_1(sc, AWI_CMD, 0);
1460 awi_write_1(sc, AWI_BANNER, 0);
1461 am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1464 /* wait for selftest completion */
1465 for (i = 0; ; i++) {
1466 if (i >= AWI_SELFTEST_TIMEOUT*hz/1000) {
1467 printf("%s: failed to complete selftest (timeout)\n",
1468 sc->sc_dev.dv_xname);
1471 status = awi_read_1(sc, AWI_SELFTEST);
1472 if ((status & 0xf0) == 0xf0)
1474 if (sc->sc_cansleep) {
1476 (void)tsleep(sc, 0, "awitst", 1);
1479 DELAY(1000*1000/hz);
1482 if (status != AWI_SELFTEST_PASSED) {
1483 printf("%s: failed to complete selftest (code %x)\n",
1484 sc->sc_dev.dv_xname, status);
1488 /* check banner to confirm firmware write it */
1489 awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
1490 if (memcmp(sc->sc_banner, "PCnetMobile:", 12) != 0) {
1491 printf("%s: failed to complete selftest (bad banner)\n",
1492 sc->sc_dev.dv_xname);
1493 for (i = 0; i < AWI_BANNER_LEN; i++)
1494 printf("%s%02x", i ? ":" : "\t", sc->sc_banner[i]);
1499 /* initializing interrupt */
1500 sc->sc_enab_intr = 1;
1501 error = awi_intr_lock(sc);
1504 intmask = AWI_INT_GROGGY | AWI_INT_SCAN_CMPLT |
1505 AWI_INT_TX | AWI_INT_RX | AWI_INT_CMD;
1506 awi_write_1(sc, AWI_INTMASK, ~intmask & 0xff);
1507 awi_write_1(sc, AWI_INTMASK2, 0);
1508 awi_write_1(sc, AWI_INTSTAT, 0);
1509 awi_write_1(sc, AWI_INTSTAT2, 0);
1510 awi_intr_unlock(sc);
1511 am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_ENECINT);
1513 /* issueing interface test command */
1514 error = awi_cmd(sc, AWI_CMD_NOP);
1516 printf("%s: failed to complete selftest", sc->sc_dev.dv_xname);
1518 printf(" (no hardware)\n");
1519 else if (error != EWOULDBLOCK)
1520 printf(" (error %d)\n", error);
1521 else if (sc->sc_cansleep)
1522 printf(" (lost interrupt)\n");
1524 printf(" (command timeout)\n");
1530 * Extract the factory default MIB value from firmware and assign the driver
1532 * Called once at attaching the interface.
1537 struct awi_softc *sc;
1542 if ((error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_LOCAL)) != 0 ||
1543 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_ADDR)) != 0 ||
1544 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MAC)) != 0 ||
1545 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MGT)) != 0 ||
1546 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_PHY)) != 0) {
1547 printf("%s: failed to get default mib value (error %d)\n",
1548 sc->sc_dev.dv_xname, error);
1552 rate = sc->sc_mib_phy.aSuprt_Data_Rates;
1553 sc->sc_tx_rate = AWI_RATE_1MBIT;
1554 for (i = 0; i < rate[1]; i++) {
1555 if (AWI_80211_RATE(rate[2 + i]) > sc->sc_tx_rate)
1556 sc->sc_tx_rate = AWI_80211_RATE(rate[2 + i]);
1558 awi_init_region(sc);
1559 memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
1560 sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
1561 sc->sc_mib_local.Fragmentation_Dis = 1;
1562 sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
1563 sc->sc_mib_local.Power_Saving_Mode_Dis = 1;
1565 /* allocate buffers */
1566 sc->sc_txbase = AWI_BUFFERS;
1567 sc->sc_txend = sc->sc_txbase +
1568 (AWI_TXD_SIZE + sizeof(struct ieee80211_frame) +
1569 sizeof(struct ether_header) + ETHERMTU) * AWI_NTXBUFS;
1570 LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Offset, sc->sc_txbase);
1571 LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Size,
1572 sc->sc_txend - sc->sc_txbase);
1573 LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Offset, sc->sc_txend);
1574 LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Size,
1575 AWI_BUFFERS_END - sc->sc_txend);
1576 sc->sc_mib_local.Network_Mode = 1;
1577 sc->sc_mib_local.Acting_as_AP = 0;
1582 * Start transmitter and receiver of firmware
1583 * Called after awi_init_hw() to start operation.
1588 struct awi_softc *sc;
1592 /* start transmitter */
1593 sc->sc_txdone = sc->sc_txnext = sc->sc_txbase;
1594 awi_write_4(sc, sc->sc_txbase + AWI_TXD_START, 0);
1595 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NEXT, 0);
1596 awi_write_4(sc, sc->sc_txbase + AWI_TXD_LENGTH, 0);
1597 awi_write_1(sc, sc->sc_txbase + AWI_TXD_RATE, 0);
1598 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NDA, 0);
1599 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NRA, 0);
1600 awi_write_1(sc, sc->sc_txbase + AWI_TXD_STATE, 0);
1601 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_DATA, sc->sc_txbase);
1602 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_MGT, 0);
1603 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_BCAST, 0);
1604 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_PS, 0);
1605 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_CF, 0);
1606 error = awi_cmd(sc, AWI_CMD_INIT_TX);
1610 /* start receiver */
1611 if (sc->sc_rxpend) {
1612 m_freem(sc->sc_rxpend);
1613 sc->sc_rxpend = NULL;
1615 error = awi_cmd(sc, AWI_CMD_INIT_RX);
1618 sc->sc_rxdoff = awi_read_4(sc, AWI_CMD_PARAMS+AWI_CA_IRX_DATA_DESC);
1619 sc->sc_rxmoff = awi_read_4(sc, AWI_CMD_PARAMS+AWI_CA_IRX_PS_DESC);
1625 struct awi_softc *sc;
1628 if (sc->sc_cmd_inprog)
1629 (void)awi_cmd_wait(sc);
1630 (void)awi_cmd(sc, AWI_CMD_KILL_RX);
1631 (void)awi_cmd_wait(sc);
1632 sc->sc_cmd_inprog = AWI_CMD_FLUSH_TX;
1633 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_DATA, 1);
1634 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_MGT, 0);
1635 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_BCAST, 0);
1636 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_PS, 0);
1637 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_CF, 0);
1638 (void)awi_cmd(sc, AWI_CMD_FLUSH_TX);
1639 (void)awi_cmd_wait(sc);
1644 struct awi_softc *sc;
1647 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1648 switch (sc->sc_mib_phy.aCurrent_Reg_Domain) {
1649 case AWI_REG_DOMAIN_US:
1650 case AWI_REG_DOMAIN_CA:
1651 case AWI_REG_DOMAIN_EU:
1652 sc->sc_scan_min = 0;
1653 sc->sc_scan_max = 77;
1655 case AWI_REG_DOMAIN_ES:
1656 sc->sc_scan_min = 0;
1657 sc->sc_scan_max = 26;
1659 case AWI_REG_DOMAIN_FR:
1660 sc->sc_scan_min = 0;
1661 sc->sc_scan_max = 32;
1663 case AWI_REG_DOMAIN_JP:
1664 sc->sc_scan_min = 6;
1665 sc->sc_scan_max = 17;
1670 sc->sc_scan_set = sc->sc_scan_cur % 3 + 1;
1672 switch (sc->sc_mib_phy.aCurrent_Reg_Domain) {
1673 case AWI_REG_DOMAIN_US:
1674 case AWI_REG_DOMAIN_CA:
1675 sc->sc_scan_min = 1;
1676 sc->sc_scan_max = 11;
1677 sc->sc_scan_cur = 3;
1679 case AWI_REG_DOMAIN_EU:
1680 sc->sc_scan_min = 1;
1681 sc->sc_scan_max = 13;
1682 sc->sc_scan_cur = 3;
1684 case AWI_REG_DOMAIN_ES:
1685 sc->sc_scan_min = 10;
1686 sc->sc_scan_max = 11;
1687 sc->sc_scan_cur = 10;
1689 case AWI_REG_DOMAIN_FR:
1690 sc->sc_scan_min = 10;
1691 sc->sc_scan_max = 13;
1692 sc->sc_scan_cur = 10;
1694 case AWI_REG_DOMAIN_JP:
1695 sc->sc_scan_min = 14;
1696 sc->sc_scan_max = 14;
1697 sc->sc_scan_cur = 14;
1703 sc->sc_ownch = sc->sc_scan_cur;
1709 struct awi_softc *sc;
1714 while ((bp = TAILQ_FIRST(&sc->sc_scan)) != NULL) {
1715 TAILQ_REMOVE(&sc->sc_scan, bp, list);
1718 if (!sc->sc_mib_local.Network_Mode && sc->sc_no_bssid) {
1719 memset(&sc->sc_bss, 0, sizeof(sc->sc_bss));
1720 sc->sc_bss.essid[0] = IEEE80211_ELEMID_SSID;
1721 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1722 sc->sc_bss.chanset = sc->sc_ownch % 3 + 1;
1723 sc->sc_bss.pattern = sc->sc_ownch;
1724 sc->sc_bss.index = 1;
1725 sc->sc_bss.dwell_time = 200; /*XXX*/
1727 sc->sc_bss.chanset = sc->sc_ownch;
1728 sc->sc_status = AWI_ST_SETSS;
1729 error = awi_set_ss(sc);
1731 if (sc->sc_mib_local.Network_Mode)
1732 awi_drvstate(sc, AWI_DRV_INFSC);
1734 awi_drvstate(sc, AWI_DRV_ADHSC);
1735 sc->sc_start_bss = 0;
1736 sc->sc_active_scan = 1;
1737 sc->sc_mgt_timer = AWI_ASCAN_WAIT / 1000;
1738 sc->sc_ifp->if_timer = 1;
1739 sc->sc_status = AWI_ST_SCAN;
1740 error = awi_cmd_scan(sc);
1747 struct awi_softc *sc;
1753 * The pattern parameter for FH phy should be incremented
1754 * by 3. But BayStack 650 Access Points apparently always
1755 * assign hop pattern set parameter to 1 for any pattern.
1756 * So we try all combinations of pattern/set parameters.
1757 * Since this causes no error, it may be a bug of
1758 * PCnetMobile firmware.
1761 if (sc->sc_scan_cur > sc->sc_scan_max) {
1762 sc->sc_scan_cur = sc->sc_scan_min;
1763 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
1764 sc->sc_scan_set = sc->sc_scan_set % 3 + 1;
1766 error = awi_cmd_scan(sc);
1767 if (error != EINVAL)
1775 struct awi_softc *sc;
1777 struct ifnet *ifp = sc->sc_ifp;
1778 struct awi_bss *bp, *sbp;
1781 bp = TAILQ_FIRST(&sc->sc_scan);
1784 if (sc->sc_active_scan) {
1785 if (ifp->if_flags & IFF_DEBUG)
1786 printf("%s: entering passive scan mode\n",
1787 sc->sc_dev.dv_xname);
1788 sc->sc_active_scan = 0;
1790 sc->sc_mgt_timer = AWI_PSCAN_WAIT / 1000;
1792 (void)awi_next_scan(sc);
1796 if (ifp->if_flags & IFF_DEBUG)
1797 printf("%s:\tmacaddr ch/pat sig flag wep essid\n",
1798 sc->sc_dev.dv_xname);
1799 for (; bp != NULL; bp = TAILQ_NEXT(bp, list)) {
1802 * The configuration of the access points may change
1803 * during my scan. So we retries to associate with
1804 * it unless there are any suitable AP.
1806 if (bp->fails++ < 3)
1812 * Since the firmware apparently scans not only the specified
1813 * channel of SCAN command but all available channel within
1814 * the region, we should filter out unnecessary responses here.
1816 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1817 if (bp->pattern < sc->sc_scan_min ||
1818 bp->pattern > sc->sc_scan_max)
1821 if (bp->chanset < sc->sc_scan_min ||
1822 bp->chanset > sc->sc_scan_max)
1825 if (sc->sc_mib_local.Network_Mode) {
1826 if (!(bp->capinfo & IEEE80211_CAPINFO_ESS) ||
1827 (bp->capinfo & IEEE80211_CAPINFO_IBSS))
1830 if ((bp->capinfo & IEEE80211_CAPINFO_ESS) ||
1831 !(bp->capinfo & IEEE80211_CAPINFO_IBSS))
1834 if (sc->sc_wep_algo == NULL) {
1835 if (bp->capinfo & IEEE80211_CAPINFO_PRIVACY)
1838 if (!(bp->capinfo & IEEE80211_CAPINFO_PRIVACY))
1841 if (sc->sc_mib_mac.aDesired_ESS_ID[1] != 0 &&
1842 memcmp(&sc->sc_mib_mac.aDesired_ESS_ID, bp->essid,
1843 sizeof(bp->essid)) != 0)
1845 if (ifp->if_flags & IFF_DEBUG) {
1846 printf(" %c %s", fail ? '-' : '+',
1847 ether_sprintf(bp->esrc));
1848 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
1849 printf(" %2d/%d%c", bp->pattern, bp->chanset,
1850 fail & 0x01 ? '!' : ' ');
1852 printf(" %4d%c", bp->chanset,
1853 fail & 0x01 ? '!' : ' ');
1854 printf(" %+4d", bp->rssi);
1856 (bp->capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
1857 (bp->capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
1859 fail & 0x02 ? '!' : ' ');
1861 (bp->capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" :
1863 fail & 0x04 ? '!' : ' ');
1864 awi_print_essid(bp->essid);
1865 printf("%s\n", fail & 0x08 ? "!" : "");
1868 if (sbp == NULL || bp->rssi > sbp->rssi)
1875 (void)awi_set_ss(sc);
1879 awi_recv_beacon(sc, m0, rxts, rssi)
1880 struct awi_softc *sc;
1885 struct ieee80211_frame *wh;
1887 u_int8_t *frame, *eframe;
1888 u_int8_t *tstamp, *bintval, *capinfo, *ssid, *rates, *parms;
1890 if (sc->sc_status != AWI_ST_SCAN)
1892 wh = mtod(m0, struct ieee80211_frame *);
1894 frame = (u_int8_t *)&wh[1];
1895 eframe = mtod(m0, u_int8_t *) + m0->m_len;
1899 * beacon interval [2]
1900 * capability information [2]
1902 * supported rates [tlv]
1903 * parameter set [tlv]
1906 if (frame + 12 > eframe) {
1909 printf("awi_recv_beacon: frame too short \n");
1920 ssid = rates = parms = NULL;
1921 while (frame < eframe) {
1923 case IEEE80211_ELEMID_SSID:
1926 case IEEE80211_ELEMID_RATES:
1929 case IEEE80211_ELEMID_FHPARMS:
1930 case IEEE80211_ELEMID_DSPARMS:
1934 frame += frame[1] + 2;
1936 if (ssid == NULL || rates == NULL || parms == NULL) {
1939 printf("awi_recv_beacon: ssid=%p, rates=%p, parms=%p\n",
1940 ssid, rates, parms);
1944 if (ssid[1] > IEEE80211_NWID_LEN) {
1947 printf("awi_recv_beacon: bad ssid len: %d from %s\n",
1948 ssid[1], ether_sprintf(wh->i_addr2));
1953 for (bp = TAILQ_FIRST(&sc->sc_scan); bp != NULL;
1954 bp = TAILQ_NEXT(bp, list)) {
1955 if (memcmp(bp->esrc, wh->i_addr2, ETHER_ADDR_LEN) == 0 &&
1956 memcmp(bp->bssid, wh->i_addr3, ETHER_ADDR_LEN) == 0)
1960 bp = malloc(sizeof(struct awi_bss), M_DEVBUF, M_NOWAIT);
1963 TAILQ_INSERT_TAIL(&sc->sc_scan, bp, list);
1964 memcpy(bp->esrc, wh->i_addr2, ETHER_ADDR_LEN);
1965 memcpy(bp->bssid, wh->i_addr3, ETHER_ADDR_LEN);
1966 memset(bp->essid, 0, sizeof(bp->essid));
1967 memcpy(bp->essid, ssid, 2 + ssid[1]);
1971 memcpy(bp->timestamp, tstamp, sizeof(bp->timestamp));
1972 bp->interval = LE_READ_2(bintval);
1973 bp->capinfo = LE_READ_2(capinfo);
1974 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1975 bp->chanset = parms[4];
1976 bp->pattern = parms[5];
1977 bp->index = parms[6];
1978 bp->dwell_time = LE_READ_2(parms + 2);
1980 bp->chanset = parms[2];
1985 if (sc->sc_mgt_timer == 0)
1991 struct awi_softc *sc;
1993 struct ifnet *ifp = sc->sc_ifp;
1997 sc->sc_status = AWI_ST_SETSS;
1999 if (ifp->if_flags & IFF_DEBUG) {
2000 printf("%s: ch %d pat %d id %d dw %d iv %d bss %s ssid ",
2001 sc->sc_dev.dv_xname, bp->chanset,
2002 bp->pattern, bp->index, bp->dwell_time, bp->interval,
2003 ether_sprintf(bp->bssid));
2004 awi_print_essid(bp->essid);
2007 memcpy(&sc->sc_mib_mgt.aCurrent_BSS_ID, bp->bssid, ETHER_ADDR_LEN);
2008 memcpy(&sc->sc_mib_mgt.aCurrent_ESS_ID, bp->essid,
2010 LE_WRITE_2(&sc->sc_mib_mgt.aBeacon_Period, bp->interval);
2011 error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT);
2017 struct awi_softc *sc;
2021 sc->sc_status = AWI_ST_SYNC;
2024 if (sc->sc_cmd_inprog) {
2025 if (awi_cmd_wait(sc))
2028 sc->sc_cmd_inprog = AWI_CMD_SYNC;
2029 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_SET, bp->chanset);
2030 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_PATTERN, bp->pattern);
2031 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_IDX, bp->index);
2032 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_STARTBSS,
2033 sc->sc_start_bss ? 1 : 0);
2034 awi_write_2(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_DWELL, bp->dwell_time);
2035 awi_write_2(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_MBZ, 0);
2036 awi_write_bytes(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_TIMESTAMP,
2038 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_REFTIME, bp->rxtime);
2039 (void)awi_cmd(sc, AWI_CMD_SYNC);
2044 struct awi_softc *sc;
2046 struct ifnet *ifp = sc->sc_ifp;
2048 if (sc->sc_mib_local.Network_Mode) {
2049 awi_drvstate(sc, AWI_DRV_INFSY);
2050 awi_send_auth(sc, 1);
2052 if (ifp->if_flags & IFF_DEBUG) {
2053 printf("%s: synced with", sc->sc_dev.dv_xname);
2054 if (sc->sc_no_bssid)
2055 printf(" no-bssid");
2058 ether_sprintf(sc->sc_bss.bssid));
2059 awi_print_essid(sc->sc_bss.essid);
2061 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
2062 printf(" at chanset %d pattern %d\n",
2063 sc->sc_bss.chanset, sc->sc_bss.pattern);
2065 printf(" at channel %d\n", sc->sc_bss.chanset);
2067 awi_drvstate(sc, AWI_DRV_ADHSY);
2068 sc->sc_status = AWI_ST_RUNNING;
2069 ifp->if_flags |= IFF_RUNNING;
2076 struct awi_softc *sc;
2078 struct ifnet *ifp = sc->sc_ifp;
2080 struct ieee80211_frame *wh;
2083 MGETHDR(m, M_DONTWAIT, MT_DATA);
2086 if (ifp->if_flags & IFF_DEBUG)
2087 printf("%s: sending deauth to %s\n", sc->sc_dev.dv_xname,
2088 ether_sprintf(sc->sc_bss.bssid));
2090 wh = mtod(m, struct ieee80211_frame *);
2091 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2092 IEEE80211_FC0_SUBTYPE_AUTH;
2093 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2094 LE_WRITE_2(wh->i_dur, 0);
2095 LE_WRITE_2(wh->i_seq, 0);
2096 memcpy(wh->i_addr1, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2097 memcpy(wh->i_addr2, sc->sc_mib_addr.aMAC_Address, ETHER_ADDR_LEN);
2098 memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2100 deauth = (u_int8_t *)&wh[1];
2101 LE_WRITE_2(deauth, IEEE80211_REASON_AUTH_LEAVE);
2104 m->m_pkthdr.len = m->m_len = deauth - mtod(m, u_int8_t *);
2105 IF_ENQUEUE(&sc->sc_mgtq, m);
2107 awi_drvstate(sc, AWI_DRV_INFTOSS);
2111 awi_send_auth(sc, seq)
2112 struct awi_softc *sc;
2115 struct ifnet *ifp = sc->sc_ifp;
2117 struct ieee80211_frame *wh;
2120 MGETHDR(m, M_DONTWAIT, MT_DATA);
2123 sc->sc_status = AWI_ST_AUTH;
2124 if (ifp->if_flags & IFF_DEBUG)
2125 printf("%s: sending auth to %s\n", sc->sc_dev.dv_xname,
2126 ether_sprintf(sc->sc_bss.bssid));
2128 wh = mtod(m, struct ieee80211_frame *);
2129 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2130 IEEE80211_FC0_SUBTYPE_AUTH;
2131 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2132 LE_WRITE_2(wh->i_dur, 0);
2133 LE_WRITE_2(wh->i_seq, 0);
2134 memcpy(wh->i_addr1, sc->sc_bss.esrc, ETHER_ADDR_LEN);
2135 memcpy(wh->i_addr2, sc->sc_mib_addr.aMAC_Address, ETHER_ADDR_LEN);
2136 memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2138 auth = (u_int8_t *)&wh[1];
2139 /* algorithm number */
2140 LE_WRITE_2(auth, IEEE80211_AUTH_ALG_OPEN);
2142 /* sequence number */
2143 LE_WRITE_2(auth, seq);
2146 LE_WRITE_2(auth, 0);
2149 m->m_pkthdr.len = m->m_len = auth - mtod(m, u_int8_t *);
2150 IF_ENQUEUE(&sc->sc_mgtq, m);
2153 sc->sc_mgt_timer = AWI_TRANS_TIMEOUT / 1000;
2158 awi_recv_auth(sc, m0)
2159 struct awi_softc *sc;
2162 struct ieee80211_frame *wh;
2163 u_int8_t *auth, *eframe;
2167 wh = mtod(m0, struct ieee80211_frame *);
2168 auth = (u_int8_t *)&wh[1];
2169 eframe = mtod(m0, u_int8_t *) + m0->m_len;
2170 if (sc->sc_ifp->if_flags & IFF_DEBUG)
2171 printf("%s: receive auth from %s\n", sc->sc_dev.dv_xname,
2172 ether_sprintf(wh->i_addr2));
2174 /* algorithm number */
2175 if (LE_READ_2(auth) != IEEE80211_AUTH_ALG_OPEN)
2178 if (!sc->sc_mib_local.Network_Mode) {
2179 if (sc->sc_status != AWI_ST_RUNNING)
2181 if (LE_READ_2(auth) == 1)
2182 awi_send_auth(sc, 2);
2185 if (sc->sc_status != AWI_ST_AUTH)
2187 /* sequence number */
2188 if (LE_READ_2(auth) != 2)
2192 status = LE_READ_2(auth);
2194 printf("%s: authentication failed (reason %d)\n",
2195 sc->sc_dev.dv_xname, status);
2196 for (bp = TAILQ_FIRST(&sc->sc_scan); bp != NULL;
2197 bp = TAILQ_NEXT(bp, list)) {
2198 if (memcmp(bp->esrc, sc->sc_bss.esrc, ETHER_ADDR_LEN)
2206 sc->sc_mgt_timer = 0;
2207 awi_drvstate(sc, AWI_DRV_INFAUTH);
2208 awi_send_asreq(sc, 0);
2212 awi_send_asreq(sc, reassoc)
2213 struct awi_softc *sc;
2216 struct ifnet *ifp = sc->sc_ifp;
2218 struct ieee80211_frame *wh;
2222 MGETHDR(m, M_DONTWAIT, MT_DATA);
2225 sc->sc_status = AWI_ST_ASSOC;
2226 if (ifp->if_flags & IFF_DEBUG)
2227 printf("%s: sending %sassoc req to %s\n", sc->sc_dev.dv_xname,
2228 reassoc ? "re" : "",
2229 ether_sprintf(sc->sc_bss.bssid));
2231 wh = mtod(m, struct ieee80211_frame *);
2232 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT;
2234 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_REASSOC_REQ;
2236 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_ASSOC_REQ;
2237 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2238 LE_WRITE_2(wh->i_dur, 0);
2239 LE_WRITE_2(wh->i_seq, 0);
2240 memcpy(wh->i_addr1, sc->sc_bss.esrc, ETHER_ADDR_LEN);
2241 memcpy(wh->i_addr2, sc->sc_mib_addr.aMAC_Address, ETHER_ADDR_LEN);
2242 memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2244 asreq = (u_int8_t *)&wh[1];
2246 /* capability info */
2247 if (sc->sc_wep_algo == NULL)
2248 LE_WRITE_2(asreq, IEEE80211_CAPINFO_CF_POLLABLE);
2251 IEEE80211_CAPINFO_CF_POLLABLE | IEEE80211_CAPINFO_PRIVACY);
2253 /* listen interval */
2254 lintval = LE_READ_2(&sc->sc_mib_mgt.aListen_Interval);
2255 LE_WRITE_2(asreq, lintval);
2258 /* current AP address */
2259 memcpy(asreq, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2260 asreq += ETHER_ADDR_LEN;
2263 memcpy(asreq, sc->sc_bss.essid, 2 + sc->sc_bss.essid[1]);
2264 asreq += 2 + asreq[1];
2265 /* supported rates */
2266 memcpy(asreq, &sc->sc_mib_phy.aSuprt_Data_Rates, 4);
2267 asreq += 2 + asreq[1];
2269 m->m_pkthdr.len = m->m_len = asreq - mtod(m, u_int8_t *);
2270 IF_ENQUEUE(&sc->sc_mgtq, m);
2273 sc->sc_mgt_timer = AWI_TRANS_TIMEOUT / 1000;
2278 awi_recv_asresp(sc, m0)
2279 struct awi_softc *sc;
2282 struct ieee80211_frame *wh;
2283 u_int8_t *asresp, *eframe;
2285 u_int8_t rate, *phy_rates;
2289 wh = mtod(m0, struct ieee80211_frame *);
2290 asresp = (u_int8_t *)&wh[1];
2291 eframe = mtod(m0, u_int8_t *) + m0->m_len;
2292 if (sc->sc_ifp->if_flags & IFF_DEBUG)
2293 printf("%s: receive assoc resp from %s\n", sc->sc_dev.dv_xname,
2294 ether_sprintf(wh->i_addr2));
2296 if (!sc->sc_mib_local.Network_Mode)
2299 if (sc->sc_status != AWI_ST_ASSOC)
2301 /* capability info */
2304 status = LE_READ_2(asresp);
2306 printf("%s: association failed (reason %d)\n",
2307 sc->sc_dev.dv_xname, status);
2308 for (bp = TAILQ_FIRST(&sc->sc_scan); bp != NULL;
2309 bp = TAILQ_NEXT(bp, list)) {
2310 if (memcmp(bp->esrc, sc->sc_bss.esrc, ETHER_ADDR_LEN)
2319 /* association id */
2321 /* supported rates */
2322 rate = AWI_RATE_1MBIT;
2323 for (i = 0; i < asresp[1]; i++) {
2324 if (AWI_80211_RATE(asresp[2 + i]) <= rate)
2326 phy_rates = sc->sc_mib_phy.aSuprt_Data_Rates;
2327 for (j = 0; j < phy_rates[1]; j++) {
2328 if (AWI_80211_RATE(asresp[2 + i]) ==
2329 AWI_80211_RATE(phy_rates[2 + j]))
2330 rate = AWI_80211_RATE(asresp[2 + i]);
2333 if (sc->sc_ifp->if_flags & IFF_DEBUG) {
2334 printf("%s: associated with %s ssid ",
2335 sc->sc_dev.dv_xname, ether_sprintf(sc->sc_bss.bssid));
2336 awi_print_essid(sc->sc_bss.essid);
2337 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
2338 printf(" chanset %d pattern %d\n",
2339 sc->sc_bss.chanset, sc->sc_bss.pattern);
2341 printf(" channel %d\n", sc->sc_bss.chanset);
2343 sc->sc_tx_rate = rate;
2344 sc->sc_mgt_timer = 0;
2345 sc->sc_rx_timer = 10;
2346 sc->sc_ifp->if_timer = 1;
2347 sc->sc_status = AWI_ST_RUNNING;
2348 sc->sc_ifp->if_flags |= IFF_RUNNING;
2349 awi_drvstate(sc, AWI_DRV_INFASSOC);
2350 awi_start(sc->sc_ifp);
2354 awi_mib(sc, cmd, mib)
2355 struct awi_softc *sc;
2360 u_int8_t size, *ptr;
2364 ptr = (u_int8_t *)&sc->sc_mib_local;
2365 size = sizeof(sc->sc_mib_local);
2368 ptr = (u_int8_t *)&sc->sc_mib_addr;
2369 size = sizeof(sc->sc_mib_addr);
2372 ptr = (u_int8_t *)&sc->sc_mib_mac;
2373 size = sizeof(sc->sc_mib_mac);
2376 ptr = (u_int8_t *)&sc->sc_mib_stat;
2377 size = sizeof(sc->sc_mib_stat);
2380 ptr = (u_int8_t *)&sc->sc_mib_mgt;
2381 size = sizeof(sc->sc_mib_mgt);
2384 ptr = (u_int8_t *)&sc->sc_mib_phy;
2385 size = sizeof(sc->sc_mib_phy);
2390 if (sc->sc_cmd_inprog) {
2391 error = awi_cmd_wait(sc);
2393 if (error == EWOULDBLOCK)
2394 printf("awi_mib: cmd %d inprog",
2399 sc->sc_cmd_inprog = cmd;
2400 if (cmd == AWI_CMD_SET_MIB)
2401 awi_write_bytes(sc, AWI_CMD_PARAMS+AWI_CA_MIB_DATA, ptr, size);
2402 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_MIB_TYPE, mib);
2403 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_MIB_SIZE, size);
2404 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_MIB_INDEX, 0);
2405 error = awi_cmd(sc, cmd);
2408 if (cmd == AWI_CMD_GET_MIB) {
2409 awi_read_bytes(sc, AWI_CMD_PARAMS+AWI_CA_MIB_DATA, ptr, size);
2414 printf("awi_mib: #%d:", mib);
2415 for (i = 0; i < size; i++)
2416 printf(" %02x", ptr[i]);
2426 struct awi_softc *sc;
2431 if (sc->sc_active_scan)
2432 scan_mode = AWI_SCAN_ACTIVE;
2434 scan_mode = AWI_SCAN_PASSIVE;
2435 if (sc->sc_mib_mgt.aScan_Mode != scan_mode) {
2436 sc->sc_mib_mgt.aScan_Mode = scan_mode;
2437 error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT);
2441 if (sc->sc_cmd_inprog) {
2442 error = awi_cmd_wait(sc);
2446 sc->sc_cmd_inprog = AWI_CMD_SCAN;
2447 awi_write_2(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_DURATION,
2448 sc->sc_active_scan ? AWI_ASCAN_DURATION : AWI_PSCAN_DURATION);
2449 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
2450 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_SET,
2452 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_PATTERN,
2454 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_IDX, 1);
2456 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_SET,
2458 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_PATTERN, 0);
2459 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_IDX, 0);
2461 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_SUSP, 0);
2462 return awi_cmd(sc, AWI_CMD_SCAN);
2467 struct awi_softc *sc;
2473 sc->sc_cmd_inprog = cmd;
2474 awi_write_1(sc, AWI_CMD_STATUS, AWI_STAT_IDLE);
2475 awi_write_1(sc, AWI_CMD, cmd);
2476 if (sc->sc_status != AWI_ST_INIT)
2478 error = awi_cmd_wait(sc);
2481 status = awi_read_1(sc, AWI_CMD_STATUS);
2482 awi_write_1(sc, AWI_CMD, 0);
2486 case AWI_STAT_BADPARM:
2489 printf("%s: command %d failed %x\n",
2490 sc->sc_dev.dv_xname, cmd, status);
2498 struct awi_softc *sc;
2500 u_int8_t cmd, status;
2502 status = awi_read_1(sc, AWI_CMD_STATUS);
2503 if (status == AWI_STAT_IDLE)
2504 return; /* stray interrupt */
2506 cmd = sc->sc_cmd_inprog;
2507 sc->sc_cmd_inprog = 0;
2508 if (sc->sc_status == AWI_ST_INIT) {
2512 awi_write_1(sc, AWI_CMD, 0);
2514 if (status != AWI_STAT_OK) {
2515 printf("%s: command %d failed %x\n",
2516 sc->sc_dev.dv_xname, cmd, status);
2519 switch (sc->sc_status) {
2521 if (cmd == AWI_CMD_SET_MIB)
2522 awi_cmd_scan(sc); /* retry */
2536 awi_next_txd(sc, len, framep, ntxdp)
2537 struct awi_softc *sc;
2539 u_int32_t *framep, *ntxdp;
2541 u_int32_t txd, ntxd, frame;
2543 txd = sc->sc_txnext;
2544 frame = txd + AWI_TXD_SIZE;
2545 if (frame + len > sc->sc_txend)
2546 frame = sc->sc_txbase;
2548 if (ntxd + AWI_TXD_SIZE > sc->sc_txend)
2549 ntxd = sc->sc_txbase;
2553 * Determine if there are any room in ring buffer.
2554 * --- send wait, === new data, +++ conflict (ENOBUFS)
2555 * base........................end
2556 * done----txd=====ntxd OK
2557 * --txd=====done++++ntxd-- full
2558 * --txd=====ntxd done-- OK
2559 * ==ntxd done----txd=== OK
2560 * ==done++++ntxd----txd=== full
2561 * ++ntxd txd=====done++ full
2564 if (txd < sc->sc_txdone && ntxd + AWI_TXD_SIZE > sc->sc_txdone)
2567 if (txd < sc->sc_txdone || ntxd + AWI_TXD_SIZE > sc->sc_txdone)
2575 struct awi_softc *sc;
2579 if (curproc == NULL) {
2582 * Though driver ioctl should be called with context,
2583 * KAME ipv6 stack calls ioctl in interrupt for now.
2584 * We simply abort the request if there are other
2585 * ioctl requests in progress.
2593 sc->sc_cansleep = 0;
2596 while (sc->sc_busy) {
2600 error = tsleep(sc, PCATCH, "awilck", 0);
2606 sc->sc_cansleep = 1;
2612 struct awi_softc *sc;
2615 sc->sc_cansleep = 0;
2616 if (sc->sc_sleep_cnt)
2622 struct awi_softc *sc;
2628 for (retry = 0; retry < 10; retry++) {
2629 for (i = 0; i < AWI_LOCKOUT_TIMEOUT*1000/5; i++) {
2630 status = awi_read_1(sc, AWI_LOCKOUT_HOST);
2637 awi_write_1(sc, AWI_LOCKOUT_MAC, 1);
2638 status = awi_read_1(sc, AWI_LOCKOUT_HOST);
2641 awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
2644 printf("%s: failed to lock interrupt\n",
2645 sc->sc_dev.dv_xname);
2653 struct awi_softc *sc;
2656 awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
2661 struct awi_softc *sc;
2666 while (sc->sc_cmd_inprog) {
2669 if (awi_read_1(sc, AWI_CMD) != sc->sc_cmd_inprog) {
2670 printf("%s: failed to access hardware\n",
2671 sc->sc_dev.dv_xname);
2675 if (sc->sc_cansleep) {
2677 error = tsleep(sc, 0, "awicmd",
2678 AWI_CMD_TIMEOUT*hz/1000);
2681 if (awi_read_1(sc, AWI_CMD_STATUS) != AWI_STAT_IDLE) {
2685 if (i++ >= AWI_CMD_TIMEOUT*1000/10)
2686 error = EWOULDBLOCK;
2697 awi_print_essid(essid)
2704 if (len > IEEE80211_NWID_LEN)
2705 len = IEEE80211_NWID_LEN; /*XXX*/
2706 /* determine printable or not */
2707 for (i = 0, p = essid + 2; i < len; i++, p++) {
2708 if (*p < ' ' || *p > 0x7e)
2713 for (i = 0, p = essid + 2; i < len; i++, p++)
2718 for (i = 0, p = essid + 2; i < len; i++, p++)
2725 awi_dump_pkt(sc, m, rssi)
2726 struct awi_softc *sc;
2730 struct ieee80211_frame *wh;
2733 wh = mtod(m, struct ieee80211_frame *);
2735 if (awi_dump_mask != 0 &&
2736 ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK)==IEEE80211_FC1_DIR_NODS) &&
2737 ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK)==IEEE80211_FC0_TYPE_MGT)) {
2738 if ((AWI_DUMP_MASK(wh->i_fc[0]) & awi_dump_mask) != 0)
2741 if (awi_dump_mask < 0 &&
2742 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK)==IEEE80211_FC0_TYPE_DATA)
2749 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
2750 case IEEE80211_FC1_DIR_NODS:
2751 printf("NODS %s", ether_sprintf(wh->i_addr2));
2752 printf("->%s", ether_sprintf(wh->i_addr1));
2753 printf("(%s)", ether_sprintf(wh->i_addr3));
2755 case IEEE80211_FC1_DIR_TODS:
2756 printf("TODS %s", ether_sprintf(wh->i_addr2));
2757 printf("->%s", ether_sprintf(wh->i_addr3));
2758 printf("(%s)", ether_sprintf(wh->i_addr1));
2760 case IEEE80211_FC1_DIR_FROMDS:
2761 printf("FRDS %s", ether_sprintf(wh->i_addr3));
2762 printf("->%s", ether_sprintf(wh->i_addr1));
2763 printf("(%s)", ether_sprintf(wh->i_addr2));
2765 case IEEE80211_FC1_DIR_DSTODS:
2766 printf("DSDS %s", ether_sprintf((u_int8_t *)&wh[1]));
2767 printf("->%s", ether_sprintf(wh->i_addr3));
2768 printf("(%s", ether_sprintf(wh->i_addr2));
2769 printf("->%s)", ether_sprintf(wh->i_addr1));
2772 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
2773 case IEEE80211_FC0_TYPE_DATA:
2776 case IEEE80211_FC0_TYPE_MGT:
2777 switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
2778 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2779 printf(" probe_req");
2781 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2782 printf(" probe_resp");
2784 case IEEE80211_FC0_SUBTYPE_BEACON:
2787 case IEEE80211_FC0_SUBTYPE_AUTH:
2790 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2791 printf(" assoc_req");
2793 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2794 printf(" assoc_resp");
2796 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2797 printf(" reassoc_req");
2799 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2800 printf(" reassoc_resp");
2802 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2805 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2806 printf(" disassoc");
2810 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
2816 wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
2819 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
2822 printf(" +%d", rssi);
2824 if (awi_dump_len > 0) {
2826 if (l > awi_dump_len + sizeof(*wh))
2827 l = awi_dump_len + sizeof(*wh);
2831 for (; i < l; i++) {
2834 printf("%02x", mtod(m, u_int8_t *)[i]);