The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/ic/an.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*      $OpenBSD: an.c,v 1.56 2008/08/27 09:05:03 damien Exp $  */
    2 /*      $NetBSD: an.c,v 1.34 2005/06/20 02:49:18 atatat Exp $   */
    3 /*
    4  * Copyright (c) 1997, 1998, 1999
    5  *      Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. All advertising materials mentioning features or use of this software
   16  *    must display the following acknowledgement:
   17  *      This product includes software developed by Bill Paul.
   18  * 4. Neither the name of the author nor the names of any co-contributors
   19  *    may be used to endorse or promote products derived from this software
   20  *    without specific prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
   23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
   26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
   32  * THE POSSIBILITY OF SUCH DAMAGE.
   33  *
   34  * $FreeBSD: src/sys/dev/an/if_an.c,v 1.12 2000/11/13 23:04:12 wpaul Exp $
   35  */
   36 /*
   37  * Copyright (c) 2004, 2005 David Young.  All rights reserved.
   38  * Copyright (c) 2004, 2005 OJC Technologies.  All rights reserved.
   39  * Copyright (c) 2004, 2005 Dayton Data Center Services, LLC.  All
   40  *     rights reserved.
   41  *
   42  * Redistribution and use in source and binary forms, with or without
   43  * modification, are permitted provided that the following conditions
   44  * are met:
   45  * 1. Redistributions of source code must retain the above copyright
   46  *    notice, this list of conditions and the following disclaimer.
   47  * 2. Redistributions in binary form must reproduce the above copyright
   48  *    notice, this list of conditions and the following disclaimer in the
   49  *    documentation and/or other materials provided with the distribution.
   50  * 3. Neither the name of the author nor the names of any co-contributors
   51  *    may be used to endorse or promote products derived from this software
   52  *    without specific prior written permission.
   53  *
   54  * THIS SOFTWARE IS PROVIDED BY David Young AND CONTRIBUTORS ``AS IS'' AND
   55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   57  * ARE DISCLAIMED.  IN NO EVENT SHALL David Young AND CONTRIBUTORS
   58  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   59  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   60  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   61  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   62  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   63  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
   64  * THE POSSIBILITY OF SUCH DAMAGE.
   65  */
   66 
   67 /*
   68  * Aironet 4500/4800 802.11 PCMCIA/ISA/PCI driver for FreeBSD.
   69  *
   70  * Written by Bill Paul <wpaul@ctr.columbia.edu>
   71  * Electrical Engineering Department
   72  * Columbia University, New York City
   73  */
   74 
   75 /*
   76  * Ported to NetBSD from FreeBSD by Atsushi Onoe at the San Diego
   77  * IETF meeting.
   78  */
   79 
   80 #include <sys/cdefs.h>
   81 
   82 #include "bpfilter.h"
   83 
   84 #include <sys/param.h>
   85 #include <sys/systm.h>
   86 #include <sys/sockio.h>
   87 #include <sys/mbuf.h>
   88 #include <sys/kernel.h>
   89 #include <sys/ucred.h>
   90 #include <sys/socket.h>
   91 #include <sys/timeout.h>
   92 #include <sys/device.h>
   93 #include <sys/proc.h>
   94 #include <sys/endian.h>
   95 #include <sys/tree.h>
   96 
   97 #include <machine/bus.h>
   98 
   99 #include <net/if.h>
  100 #include <net/if_dl.h>
  101 #include <net/if_llc.h>
  102 #include <net/if_media.h>
  103 #include <net/if_types.h>
  104 
  105 #ifdef INET
  106 #include <netinet/in.h>
  107 #include <netinet/in_systm.h>
  108 #include <netinet/in_var.h>
  109 #include <netinet/ip.h>
  110 #include <netinet/if_ether.h>
  111 #endif
  112 
  113 #include <net80211/ieee80211_radiotap.h>
  114 #include <net80211/ieee80211_var.h>
  115 
  116 #if NBPFILTER > 0
  117 #include <net/bpf.h>
  118 #endif
  119 
  120 #include <dev/ic/anreg.h>
  121 #include <dev/ic/anvar.h>
  122 
  123 struct cfdriver an_cd = {
  124         NULL, "an", DV_IFNET
  125 };
  126 
  127 int     an_reset(struct an_softc *);
  128 void    an_wait(struct an_softc *);
  129 int     an_init(struct ifnet *);
  130 void    an_stop(struct ifnet *, int);
  131 void    an_start(struct ifnet *);
  132 void    an_watchdog(struct ifnet *);
  133 int     an_ioctl(struct ifnet *, u_long, caddr_t);
  134 int     an_media_change(struct ifnet *);
  135 void    an_media_status(struct ifnet *, struct ifmediareq *);
  136 
  137 int     an_set_nwkey(struct an_softc *, struct ieee80211_nwkey *);
  138 int     an_set_nwkey_wep(struct an_softc *, struct ieee80211_nwkey *);
  139 int     an_get_nwkey(struct an_softc *, struct ieee80211_nwkey *);
  140 int     an_write_wepkey(struct an_softc *, int, struct an_wepkey *,
  141                                 int);
  142 
  143 void    an_rxeof(struct an_softc *);
  144 void    an_txeof(struct an_softc *, u_int16_t);
  145 void    an_linkstat_intr(struct an_softc *);
  146 
  147 int     an_cmd(struct an_softc *, int, int);
  148 int     an_seek_bap(struct an_softc *, int, int);
  149 int     an_read_bap(struct an_softc *, int, int, void *, int, int);
  150 int     an_write_bap(struct an_softc *, int, int, void *, int);
  151 int     an_mwrite_bap(struct an_softc *, int, int, struct mbuf *, int);
  152 int     an_read_rid(struct an_softc *, int, void *, int *);
  153 int     an_write_rid(struct an_softc *, int, void *, int);
  154 
  155 int     an_alloc_nicmem(struct an_softc *, int, int *);
  156 
  157 int     an_newstate(struct ieee80211com *, enum ieee80211_state, int);
  158 
  159 #ifdef AN_DEBUG
  160 int an_debug = 0;
  161 
  162 #define DPRINTF(X)      if (an_debug) printf X
  163 #define DPRINTF2(X)     if (an_debug > 1) printf X
  164 #else
  165 #define DPRINTF(X)
  166 #define DPRINTF2(X)
  167 #endif
  168 
  169 #if BYTE_ORDER == BIG_ENDIAN
  170 static __inline void
  171 an_swap16(u_int16_t *p, int cnt)
  172 {
  173         for (; cnt--; p++)
  174                 *p = swap16(*p);
  175 }
  176 #define an_switch32(val)        (val >> 16 | (val & 0xFFFF) << 16)
  177 #else
  178 #define an_swap16(p, cnt)
  179 #define an_switch32(val)        val
  180 #endif
  181 
  182 int
  183 an_attach(struct an_softc *sc)
  184 {
  185         struct ieee80211com *ic = &sc->sc_ic;
  186         struct ifnet *ifp = &ic->ic_if;
  187         int i;
  188         struct an_rid_wepkey *akey;
  189         int buflen, kid, rid;
  190         int chan, chan_min, chan_max;
  191 
  192         sc->sc_invalid = 0;
  193 
  194         /* disable interrupts */
  195         CSR_WRITE_2(sc, AN_INT_EN, 0);
  196         CSR_WRITE_2(sc, AN_EVENT_ACK, 0xffff);
  197 
  198 //      an_wait(sc);
  199         if (an_reset(sc) != 0) {
  200                 sc->sc_invalid = 1;
  201                 return 1;
  202         }
  203 
  204         /* Load factory config */
  205         if (an_cmd(sc, AN_CMD_READCFG, 0) != 0) {
  206                 printf("%s: failed to load config data\n",
  207                     sc->sc_dev.dv_xname);
  208                 return (EIO);
  209         }
  210 
  211         /* Read the current configuration */
  212         buflen = sizeof(sc->sc_config);
  213         if (an_read_rid(sc, AN_RID_GENCONFIG, &sc->sc_config, &buflen) != 0) {
  214                 printf("%s: read config failed\n", sc->sc_dev.dv_xname);
  215                 return(EIO);
  216         }
  217 
  218         an_swap16((u_int16_t *)&sc->sc_config.an_macaddr, 3); 
  219 
  220         /* Read the card capabilities */
  221         buflen = sizeof(sc->sc_caps);
  222         if (an_read_rid(sc, AN_RID_CAPABILITIES, &sc->sc_caps, &buflen) != 0) {
  223                 printf("%s: read caps failed\n", sc->sc_dev.dv_xname);
  224                 return(EIO);
  225         }
  226 
  227         an_swap16((u_int16_t *)&sc->sc_caps.an_oemaddr, 3); 
  228         an_swap16((u_int16_t *)&sc->sc_caps.an_rates, 4);
  229 
  230         /* Read WEP settings from persistent memory */
  231         akey = &sc->sc_buf.sc_wepkey;
  232         buflen = sizeof(struct an_rid_wepkey);
  233         rid = AN_RID_WEP_VOLATILE;      /* first persistent key */
  234         while (an_read_rid(sc, rid, akey, &buflen) == 0) {
  235                 an_swap16((u_int16_t *)&akey->an_mac_addr, 3); 
  236                 an_swap16((u_int16_t *)&akey->an_key, 8); 
  237                 kid = akey->an_key_index;
  238                 DPRINTF(("an_attach: wep rid=0x%x len=%d(%d) index=0x%04x "
  239                     "mac[0]=%02x keylen=%d\n",
  240                     rid, buflen, sizeof(*akey), kid,
  241                     akey->an_mac_addr[0], akey->an_key_len));
  242                 if (kid == 0xffff) {
  243                         sc->sc_tx_perskey = akey->an_mac_addr[0];
  244                         sc->sc_tx_key = -1;
  245                         break;
  246                 }
  247                 if (kid >= IEEE80211_WEP_NKID)
  248                         break;
  249                 sc->sc_perskeylen[kid] = akey->an_key_len;
  250                 sc->sc_wepkeys[kid].an_wep_keylen = -1;
  251                 rid = AN_RID_WEP_PERSISTENT;    /* for next key */
  252                 buflen = sizeof(struct an_rid_wepkey);
  253         }
  254 
  255         IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->sc_caps.an_oemaddr);
  256         bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
  257 
  258         printf("%s: Firmware %x.%02x.%02x, Radio: ", ifp->if_xname,
  259             sc->sc_caps.an_fwrev >> 8,
  260             sc->sc_caps.an_fwrev & 0xff,
  261             sc->sc_caps.an_fwsubrev);
  262 
  263         if (sc->sc_config.an_radiotype & AN_RADIOTYPE_80211_FH)
  264                 printf("802.11 FH");
  265         else if (sc->sc_config.an_radiotype & AN_RADIOTYPE_80211_DS)
  266                 printf("802.11 DS");
  267         else if (sc->sc_config.an_radiotype & AN_RADIOTYPE_LM2000_DS)
  268                 printf("LM2000 DS");
  269         else
  270                 printf("unknown (%x)", sc->sc_config.an_radiotype);
  271 
  272         printf(", address %s\n", ether_sprintf(ic->ic_myaddr));
  273         
  274         ifp->if_softc = sc;
  275         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
  276         ifp->if_ioctl = an_ioctl;
  277         ifp->if_start = an_start;
  278         ifp->if_init = an_init;
  279         ifp->if_watchdog = an_watchdog;
  280         IFQ_SET_READY(&ifp->if_snd);
  281 
  282         ic->ic_phytype = IEEE80211_T_DS;
  283         ic->ic_opmode = IEEE80211_M_STA;
  284         ic->ic_caps = IEEE80211_C_WEP | IEEE80211_C_PMGT | IEEE80211_C_MONITOR;
  285 #ifndef IEEE80211_STA_ONLY
  286         ic->ic_caps |= IEEE80211_C_IBSS;
  287 #endif
  288         ic->ic_state = IEEE80211_S_INIT;
  289         IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->sc_caps.an_oemaddr);
  290 
  291         switch (sc->sc_caps.an_regdomain) {
  292         default:
  293         case AN_REGDOMAIN_USA:
  294         case AN_REGDOMAIN_CANADA:
  295                 chan_min = 1; chan_max = 11; break;
  296         case AN_REGDOMAIN_EUROPE:
  297         case AN_REGDOMAIN_AUSTRALIA:
  298                 chan_min = 1; chan_max = 13; break;
  299         case AN_REGDOMAIN_JAPAN:
  300                 chan_min = 14; chan_max = 14; break;
  301         case AN_REGDOMAIN_SPAIN:
  302                 chan_min = 10; chan_max = 11; break;
  303         case AN_REGDOMAIN_FRANCE:
  304                 chan_min = 10; chan_max = 13; break;
  305         case AN_REGDOMAIN_JAPANWIDE:
  306                 chan_min = 1; chan_max = 14; break;
  307         }
  308 
  309         for (chan = chan_min; chan <= chan_max; chan++) {
  310                 ic->ic_channels[chan].ic_freq =
  311                     ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ);
  312                 ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_B;
  313         }
  314         ic->ic_ibss_chan = &ic->ic_channels[chan_min];
  315 
  316         /* Find supported rate */
  317         for (i = 0; i < sizeof(sc->sc_caps.an_rates); i++) {
  318                 if (sc->sc_caps.an_rates[i] == 0)
  319                         continue;
  320                 ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[
  321                     ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates++] =
  322                     sc->sc_caps.an_rates[i];
  323         }
  324 
  325         /*
  326          * Call MI attach routine.
  327          */
  328         if_attach(ifp);
  329         ieee80211_ifattach(ifp);
  330 
  331         sc->sc_newstate = ic->ic_newstate;
  332         ic->ic_newstate = an_newstate;
  333 
  334         ieee80211_media_init(ifp, an_media_change, an_media_status);
  335 
  336 #if NBPFILTER > 0
  337         bzero(&sc->sc_rxtapu, sizeof(sc->sc_rxtapu));
  338         sc->sc_rxtap.ar_ihdr.it_len = sizeof(sc->sc_rxtapu);
  339         sc->sc_rxtap.ar_ihdr.it_present = AN_RX_RADIOTAP_PRESENT;
  340 
  341         bzero(&sc->sc_txtapu, sizeof(sc->sc_txtapu));
  342         sc->sc_txtap.at_ihdr.it_len = sizeof(sc->sc_txtapu);
  343         sc->sc_txtap.at_ihdr.it_present = AN_TX_RADIOTAP_PRESENT;
  344 
  345         bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO,
  346             sizeof(struct ieee80211_frame) + 64);
  347 #endif
  348 
  349         sc->sc_sdhook = shutdownhook_establish(an_shutdown, sc);
  350 
  351         sc->sc_attached = 1;
  352 
  353         return(0);
  354 }
  355 
  356 void
  357 an_rxeof(struct an_softc *sc)
  358 {
  359         struct ieee80211com *ic = &sc->sc_ic;
  360         struct ifnet *ifp = &ic->ic_if;
  361         struct ieee80211_frame *wh;
  362         struct ieee80211_rxinfo rxi;
  363         struct ieee80211_node *ni;
  364         struct an_rxframe frmhdr;
  365         struct mbuf *m;
  366         u_int16_t status;
  367         int fid, gaplen, len, off;
  368         uint8_t *gap;
  369 
  370         fid = CSR_READ_2(sc, AN_RX_FID);
  371 
  372         /* First read in the frame header */
  373         if (an_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr), sizeof(frmhdr)) != 0) {
  374                 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
  375                 ifp->if_ierrors++;
  376                 DPRINTF(("an_rxeof: read fid %x failed\n", fid));
  377                 return;
  378         }
  379         an_swap16((u_int16_t *)&frmhdr.an_whdr, sizeof(struct ieee80211_frame)/2);
  380 
  381         status = frmhdr.an_rx_status;
  382         if ((status & AN_STAT_ERRSTAT) != 0 &&
  383             ic->ic_opmode != IEEE80211_M_MONITOR) {
  384                 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
  385                 ifp->if_ierrors++;
  386                 DPRINTF(("an_rxeof: fid %x status %x\n", fid, status));
  387                 return;
  388         }
  389 
  390         /* the payload length field includes a 16-bit "mystery field" */
  391         len = frmhdr.an_rx_payload_len - sizeof(uint16_t);
  392         off = ALIGN(sizeof(struct ieee80211_frame));
  393 
  394         if (off + len > MCLBYTES) {
  395                 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
  396                         CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
  397                         ifp->if_ierrors++;
  398                         DPRINTF(("an_rxeof: oversized packet %d\n", len));
  399                         return;
  400                 }
  401                 len = 0;
  402         }
  403 
  404         MGETHDR(m, M_DONTWAIT, MT_DATA);
  405         if (m == NULL) {
  406                 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
  407                 ifp->if_ierrors++;
  408                 DPRINTF(("an_rxeof: MGET failed\n"));
  409                 return;
  410         }
  411         if (off + len + AN_GAPLEN_MAX > MHLEN) {
  412                 MCLGET(m, M_DONTWAIT);
  413                 if ((m->m_flags & M_EXT) == 0) {
  414                         CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
  415                         m_freem(m);
  416                         ifp->if_ierrors++;
  417                         DPRINTF(("an_rxeof: MCLGET failed\n"));
  418                         return;
  419                 }
  420         }
  421         m->m_data += off - sizeof(struct ieee80211_frame);
  422 
  423         if (ic->ic_opmode != IEEE80211_M_MONITOR) {
  424                 gaplen = frmhdr.an_gaplen;
  425                 if (gaplen > AN_GAPLEN_MAX) {
  426                         CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
  427                         m_freem(m);
  428                         ifp->if_ierrors++;
  429                         DPRINTF(("%s: gap too long\n", __func__));
  430                         return;
  431                 }
  432                 /*
  433                  * We don't need the 16-bit mystery field (payload length?),
  434                  * so read it into the region reserved for the 802.11 header.
  435                  *
  436                  * When Cisco Aironet 350 cards w/ firmware version 5 or
  437                  * greater operate with certain Cisco 350 APs,
  438                  * the "gap" is filled with the SNAP header.  Read
  439                  * it in after the 802.11 header.
  440                  */
  441                 gap = m->m_data + sizeof(struct ieee80211_frame) -
  442                     sizeof(uint16_t);
  443                 an_read_bap(sc, fid, -1, gap, gaplen + sizeof(u_int16_t),
  444                     gaplen + sizeof(u_int16_t));
  445         } else
  446                 gaplen = 0;
  447 
  448         an_read_bap(sc, fid, -1,
  449             m->m_data + sizeof(struct ieee80211_frame) + gaplen, len, len);
  450         an_swap16((u_int16_t *)(m->m_data + sizeof(struct ieee80211_frame) + gaplen), (len+1)/2);
  451         m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + gaplen +
  452             len;
  453 
  454         memcpy(m->m_data, &frmhdr.an_whdr, sizeof(struct ieee80211_frame));
  455         m->m_pkthdr.rcvif = ifp;
  456         CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
  457 
  458 #if NBPFILTER > 0
  459         if (sc->sc_drvbpf) {
  460                 struct mbuf mb;
  461                 struct an_rx_radiotap_header *tap = &sc->sc_rxtap;
  462 
  463                 tap->ar_rate = frmhdr.an_rx_rate;
  464                 tap->ar_antsignal = frmhdr.an_rx_signal_strength;
  465                 tap->ar_chan_freq = ic->ic_bss->ni_chan->ic_freq;
  466                 tap->ar_chan_flags = ic->ic_bss->ni_chan->ic_flags;
  467 
  468 
  469                 mb.m_data = (caddr_t)tap;
  470                 mb.m_len = sizeof(sc->sc_rxtapu);
  471                 mb.m_next = m;
  472                 mb.m_nextpkt = NULL;
  473                 mb.m_type = 0;
  474                 mb.m_flags = 0;
  475                 bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_IN);
  476         }
  477 #endif /* NBPFILTER > 0 */
  478 
  479         wh = mtod(m, struct ieee80211_frame *);
  480         rxi.rxi_flags = 0;
  481         if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
  482                 /*
  483                  * WEP is decrypted by hardware. Clear WEP bit
  484                  * header for ieee80211_input().
  485                  */
  486                 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
  487 
  488                 rxi.rxi_flags |= IEEE80211_RXI_HWDEC;
  489         }
  490 
  491         ni = ieee80211_find_rxnode(ic, wh);
  492         rxi.rxi_rssi = frmhdr.an_rx_signal_strength;
  493         rxi.rxi_tstamp = an_switch32(frmhdr.an_rx_time);
  494         ieee80211_input(ifp, m, ni, &rxi);
  495         ieee80211_release_node(ic, ni);
  496 }
  497 
  498 void
  499 an_txeof(struct an_softc *sc, u_int16_t status)
  500 {
  501         struct ifnet *ifp = &sc->sc_ic.ic_if;
  502         int cur, id;
  503 
  504         sc->sc_tx_timer = 0;
  505         ifp->if_flags &= ~IFF_OACTIVE;
  506 
  507         id = CSR_READ_2(sc, AN_TX_CMP_FID);
  508         CSR_WRITE_2(sc, AN_EVENT_ACK, status & (AN_EV_TX | AN_EV_TX_EXC));
  509 
  510         if (status & AN_EV_TX_EXC)
  511                 ifp->if_oerrors++;
  512         else
  513                 ifp->if_opackets++;
  514 
  515         cur = sc->sc_txcur;
  516         if (sc->sc_txd[cur].d_fid == id) {
  517                 sc->sc_txd[cur].d_inuse = 0;
  518                 DPRINTF2(("an_txeof: sent %x/%d\n", id, cur));
  519                 AN_INC(cur, AN_TX_RING_CNT);
  520                 sc->sc_txcur = cur;
  521         } else {
  522                 for (cur = 0; cur < AN_TX_RING_CNT; cur++) {
  523                         if (id == sc->sc_txd[cur].d_fid) {
  524                                 sc->sc_txd[cur].d_inuse = 0;
  525                                 break;
  526                         }
  527                 }
  528                 if (ifp->if_flags & IFF_DEBUG)
  529                         printf("%s: tx mismatch: "
  530                             "expected %x(%d), actual %x(%d)\n",
  531                             sc->sc_dev.dv_xname,
  532                             sc->sc_txd[sc->sc_txcur].d_fid, sc->sc_txcur,
  533                             id, cur);
  534         }
  535 }
  536 
  537 int
  538 an_intr(void *arg)
  539 {
  540         struct an_softc *sc = arg;
  541         struct ifnet *ifp = &sc->sc_ic.ic_if;
  542         int i;
  543         u_int16_t status;
  544 
  545         if (!sc->sc_enabled || sc->sc_invalid ||
  546             (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 ||
  547             (ifp->if_flags & IFF_RUNNING) == 0)
  548                 return 0;
  549 
  550         if ((ifp->if_flags & IFF_UP) == 0) {
  551                 CSR_WRITE_2(sc, AN_INT_EN, 0);
  552                 CSR_WRITE_2(sc, AN_EVENT_ACK, ~0);
  553                 return 1;
  554         }
  555 
  556         /* maximum 10 loops per interrupt */
  557         for (i = 0; i < 10; i++) {
  558                 if (!sc->sc_enabled || sc->sc_invalid)
  559                         return 1;
  560                 if (CSR_READ_2(sc, AN_SW0) != AN_MAGIC) {
  561                         DPRINTF(("an_intr: magic number changed: %x\n",
  562                             CSR_READ_2(sc, AN_SW0)));
  563                         sc->sc_invalid = 1;
  564                         return 1;
  565                 }
  566                 status = CSR_READ_2(sc, AN_EVENT_STAT);
  567                 CSR_WRITE_2(sc, AN_EVENT_ACK, status & ~(AN_INTRS));
  568                 if ((status & AN_INTRS) == 0)
  569                         break;
  570 
  571                 if (status & AN_EV_RX)
  572                         an_rxeof(sc);
  573 
  574                 if (status & (AN_EV_TX | AN_EV_TX_EXC))
  575                         an_txeof(sc, status);
  576 
  577                 if (status & AN_EV_LINKSTAT)
  578                         an_linkstat_intr(sc);
  579 
  580                 if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
  581                     sc->sc_ic.ic_state == IEEE80211_S_RUN &&
  582                     !IFQ_IS_EMPTY(&ifp->if_snd))
  583                         an_start(ifp);
  584         }
  585 
  586         return 1;
  587 }
  588 
  589 /* Must be called at proper protection level! */
  590 int
  591 an_cmd(struct an_softc *sc, int cmd, int val)
  592 {
  593         int i, stat;
  594 
  595         /* make sure previous command completed */
  596         if (CSR_READ_2(sc, AN_COMMAND) & AN_CMD_BUSY) {
  597                 if (sc->sc_ic.ic_if.if_flags & IFF_DEBUG)
  598                         printf("%s: command 0x%x busy\n", sc->sc_dev.dv_xname,
  599                             CSR_READ_2(sc, AN_COMMAND));
  600                 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CLR_STUCK_BUSY);
  601         }
  602 
  603         CSR_WRITE_2(sc, AN_PARAM0, val);
  604         CSR_WRITE_2(sc, AN_PARAM1, 0);
  605         CSR_WRITE_2(sc, AN_PARAM2, 0);
  606         CSR_WRITE_2(sc, AN_COMMAND, cmd);
  607 
  608         if (cmd == AN_CMD_FW_RESTART) {
  609                 /* XXX: should sleep here */
  610                 DELAY(100*1000);
  611         }
  612 
  613         for (i = 0; i < AN_TIMEOUT; i++) {
  614                 if (CSR_READ_2(sc, AN_EVENT_STAT) & AN_EV_CMD)
  615                         break;
  616                 DELAY(10);
  617         }
  618 
  619         stat = CSR_READ_2(sc, AN_STATUS);
  620 
  621         /* clear stuck command busy if necessary */
  622         if (CSR_READ_2(sc, AN_COMMAND) & AN_CMD_BUSY)
  623                 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CLR_STUCK_BUSY);
  624 
  625         /* Ack the command */
  626         CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CMD);
  627 
  628         if (i == AN_TIMEOUT) {
  629                 if (sc->sc_ic.ic_if.if_flags & IFF_DEBUG)
  630                         printf("%s: command 0x%x param 0x%x timeout\n",
  631                             sc->sc_dev.dv_xname, cmd, val);
  632                 return ETIMEDOUT;
  633         }
  634         if (stat & AN_STAT_CMD_RESULT) {
  635                 if (sc->sc_ic.ic_if.if_flags & IFF_DEBUG)
  636                         printf("%s: command 0x%x param 0x%x status 0x%x "
  637                             "resp 0x%x 0x%x 0x%x\n",
  638                             sc->sc_dev.dv_xname, cmd, val, stat,
  639                             CSR_READ_2(sc, AN_RESP0), CSR_READ_2(sc, AN_RESP1),
  640                             CSR_READ_2(sc, AN_RESP2));
  641                 return EIO;
  642         }
  643 
  644         return 0;
  645 }
  646 
  647 int
  648 an_reset(struct an_softc *sc)
  649 {
  650 
  651         DPRINTF(("an_reset\n"));
  652 
  653         if (!sc->sc_enabled)
  654                 return ENXIO;
  655 
  656         an_cmd(sc, AN_CMD_ENABLE, 0);
  657         an_cmd(sc, AN_CMD_FW_RESTART, 0);
  658         an_cmd(sc, AN_CMD_NOOP2, 0);
  659 
  660         if (an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0) == ETIMEDOUT) {
  661                 printf("%s: reset failed\n", sc->sc_dev.dv_xname);
  662                 return ETIMEDOUT;
  663         }
  664 
  665         an_cmd(sc, AN_CMD_DISABLE, 0);
  666         return 0;
  667 }
  668 
  669 void
  670 an_linkstat_intr(struct an_softc *sc)
  671 {
  672         struct ieee80211com *ic = &sc->sc_ic;
  673         u_int16_t status;
  674 
  675         status = CSR_READ_2(sc, AN_LINKSTAT);
  676         CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_LINKSTAT);
  677         DPRINTF(("an_linkstat_intr: status 0x%x\n", status));
  678 
  679         if (status == AN_LINKSTAT_ASSOCIATED) {
  680                 if (ic->ic_state != IEEE80211_S_RUN
  681 #ifndef IEEE80211_STA_ONLY
  682                     || ic->ic_opmode == IEEE80211_M_IBSS
  683 #endif
  684                     )
  685                         ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
  686         } else {
  687                 if (ic->ic_opmode == IEEE80211_M_STA)
  688                         ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
  689         }
  690 }
  691 
  692 /*
  693  * Wait for firmware come up after power enabled.
  694  */
  695 void
  696 an_wait(struct an_softc *sc)
  697 {
  698         int i;
  699 
  700         CSR_WRITE_2(sc, AN_COMMAND, AN_CMD_NOOP2);
  701         for (i = 0; i < 3*hz; i++) {
  702                 if (CSR_READ_2(sc, AN_EVENT_STAT) & AN_EV_CMD)
  703                         break;
  704                 (void)tsleep(sc, PWAIT, "anatch", 1);
  705         }
  706         CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CMD);
  707 }
  708 
  709 int
  710 an_read_bap(struct an_softc *sc, int id, int off, void *buf, int len, int blen)
  711 {
  712         int error, cnt, cnt2;
  713 
  714         if (len == 0 || blen == 0)
  715                 return 0;
  716         if (off == -1)
  717                 off = sc->sc_bap_off;
  718         if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
  719                 if ((error = an_seek_bap(sc, id, off)) != 0)
  720                         return EIO;
  721         }
  722 
  723         cnt = (blen + 1) / 2;
  724         CSR_READ_MULTI_STREAM_2(sc, AN_DATA0, (u_int16_t *)buf, cnt);
  725         for (cnt2 = (len + 1) / 2; cnt < cnt2; cnt++)
  726                 (void) CSR_READ_2(sc, AN_DATA0);
  727         sc->sc_bap_off += cnt * 2;
  728 
  729         return 0;
  730 }
  731 
  732 int
  733 an_write_bap(struct an_softc *sc, int id, int off, void *buf, int buflen)
  734 {
  735         int error, cnt;
  736 
  737         if (buflen == 0)
  738                 return 0;
  739         if (off == -1)
  740                 off = sc->sc_bap_off;
  741         if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
  742                 if ((error = an_seek_bap(sc, id, off)) != 0)
  743                         return EIO;
  744         }
  745 
  746         cnt = (buflen + 1) / 2;
  747         CSR_WRITE_MULTI_STREAM_2(sc, AN_DATA0, (u_int16_t *)buf, cnt);
  748         sc->sc_bap_off += cnt * 2;
  749         return 0;
  750 }
  751 
  752 int
  753 an_seek_bap(struct an_softc *sc, int id, int off)
  754 {
  755         int i, status;
  756 
  757         CSR_WRITE_2(sc, AN_SEL0, id);
  758         CSR_WRITE_2(sc, AN_OFF0, off);
  759 
  760         for (i = 0; ; i++) {
  761                 status = CSR_READ_2(sc, AN_OFF0);
  762                 if ((status & AN_OFF_BUSY) == 0)
  763                         break;
  764                 if (i == AN_TIMEOUT) {
  765                         printf("%s: timeout in an_seek_bap to 0x%x/0x%x\n",
  766                             sc->sc_dev.dv_xname, id, off);
  767                         sc->sc_bap_off = AN_OFF_ERR;    /* invalidate */
  768                         return ETIMEDOUT;
  769                 }
  770                 DELAY(10);
  771         }
  772         if (status & AN_OFF_ERR) {
  773                 printf("%s: failed in an_seek_bap to 0x%x/0x%x\n",
  774                     sc->sc_dev.dv_xname, id, off);
  775                 sc->sc_bap_off = AN_OFF_ERR;    /* invalidate */
  776                 return EIO;
  777         }
  778         sc->sc_bap_id = id;
  779         sc->sc_bap_off = off;
  780         return 0;
  781 }
  782 
  783 int
  784 an_mwrite_bap(struct an_softc *sc, int id, int off, struct mbuf *m, int totlen)
  785 {
  786         int error, len, cnt;
  787 
  788         if (off == -1)
  789                 off = sc->sc_bap_off;
  790         if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
  791                 if ((error = an_seek_bap(sc, id, off)) != 0)
  792                         return EIO;
  793         }
  794 
  795         for (len = 0; m != NULL; m = m->m_next) {
  796                 if (m->m_len == 0)
  797                         continue;
  798                 len = min(m->m_len, totlen);
  799 
  800                 if ((mtod(m, u_long) & 0x1) || (len & 0x1)) {
  801                         m_copydata(m, 0, totlen, (caddr_t)&sc->sc_buf.sc_txbuf);
  802                         cnt = (totlen + 1) / 2;
  803                         an_swap16((u_int16_t *)&sc->sc_buf.sc_txbuf, cnt); 
  804                         CSR_WRITE_MULTI_STREAM_2(sc, AN_DATA0,
  805                             sc->sc_buf.sc_val, cnt);
  806                         off += cnt * 2;
  807                         break;
  808                 }
  809                 cnt = len / 2;
  810                 an_swap16((u_int16_t *)mtod(m, u_int16_t *), cnt); 
  811                 CSR_WRITE_MULTI_STREAM_2(sc, AN_DATA0, mtod(m, u_int16_t *),
  812                     cnt);
  813                 off += len;
  814                 totlen -= len;
  815         }
  816         sc->sc_bap_off = off;
  817         return 0;
  818 }
  819 
  820 int
  821 an_alloc_nicmem(struct an_softc *sc, int len, int *idp)
  822 {
  823         int i;
  824 
  825         if (an_cmd(sc, AN_CMD_ALLOC_MEM, len)) {
  826                 printf("%s: failed to allocate %d bytes on NIC\n",
  827                     sc->sc_dev.dv_xname, len);
  828                 return(ENOMEM);
  829         }
  830 
  831         for (i = 0; i < AN_TIMEOUT; i++) {
  832                 if (CSR_READ_2(sc, AN_EVENT_STAT) & AN_EV_ALLOC)
  833                         break;
  834                 if (i == AN_TIMEOUT) {
  835                         printf("%s: timeout in alloc\n", sc->sc_dev.dv_xname);
  836                         return ETIMEDOUT;
  837                 }
  838                 DELAY(10);
  839         }
  840 
  841         *idp = CSR_READ_2(sc, AN_ALLOC_FID);
  842         CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_ALLOC);
  843         return 0;
  844 }
  845 
  846 int
  847 an_read_rid(struct an_softc *sc, int rid, void *buf, int *buflenp)
  848 {
  849         int error;
  850         u_int16_t len;
  851 
  852         /* Tell the NIC to enter record read mode. */
  853         error = an_cmd(sc, AN_CMD_ACCESS | AN_ACCESS_READ, rid);
  854         if (error)
  855                 return error;
  856 
  857         /* length in byte, including length itself */
  858         error = an_read_bap(sc, rid, 0, &len, sizeof(len), sizeof(len));
  859         if (error)
  860                 return error;
  861 
  862         len -= 2;
  863         return an_read_bap(sc, rid, sizeof(len), buf, len, *buflenp);
  864 }
  865 
  866 int
  867 an_write_rid(struct an_softc *sc, int rid, void *buf, int buflen)
  868 {
  869         int error;
  870         u_int16_t len;
  871 
  872         /* length in byte, including length itself */
  873         len = buflen + 2;
  874 
  875         error = an_write_bap(sc, rid, 0, &len, sizeof(len));
  876         if (error)
  877                 return error;
  878         error = an_write_bap(sc, rid, sizeof(len), buf, buflen);
  879         if (error)
  880                 return error;
  881 
  882         return an_cmd(sc, AN_CMD_ACCESS | AN_ACCESS_WRITE, rid);
  883 }
  884 
  885 int
  886 an_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
  887 {
  888         struct an_softc *sc = ifp->if_softc;
  889         struct ifaddr *ifa = (struct ifaddr *)data;
  890         int s, error = 0;
  891 
  892         if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
  893                 return ENXIO;
  894 
  895         s = splnet();
  896 
  897         switch(command) {
  898         case SIOCSIFADDR:
  899                 ifp->if_flags |= IFF_UP;
  900                 switch (ifa->ifa_addr->sa_family) {
  901 #ifdef INET
  902                 case AF_INET:
  903                         error = an_init(ifp);
  904                         arp_ifinit(&sc->sc_ic.ic_ac, ifa);
  905                         break;
  906 #endif
  907                 default:
  908                         error = an_init(ifp);
  909                         break;
  910                 }
  911                 break;
  912         case SIOCSIFFLAGS:
  913                 if (ifp->if_flags & IFF_UP) {
  914                         if (sc->sc_enabled) {
  915                                 /*
  916                                  * To avoid rescanning another access point,
  917                                  * do not call an_init() here.  Instead, only
  918                                  * reflect promisc mode settings.
  919                                  */
  920                                 error = an_cmd(sc, AN_CMD_SET_MODE,
  921                                     (ifp->if_flags & IFF_PROMISC) ? 0xffff : 0);
  922                         } else
  923                                 error = an_init(ifp);
  924                 } else if (sc->sc_enabled)
  925                         an_stop(ifp, 1);
  926                 break;
  927         case SIOCADDMULTI:
  928         case SIOCDELMULTI:
  929                 /* The Aironet has no multicast filter. */
  930                 error = 0;
  931                 break;
  932         case SIOCS80211NWKEY:
  933                 error = an_set_nwkey(sc, (struct ieee80211_nwkey *)data);
  934                         break;
  935         case SIOCG80211NWKEY:
  936                 error = an_get_nwkey(sc, (struct ieee80211_nwkey *)data);
  937                 break;
  938         default:
  939                 error = ieee80211_ioctl(ifp, command, data);
  940                 break;
  941         }
  942         if (error == ENETRESET) {
  943                 if (sc->sc_enabled)
  944                         error = an_init(ifp);
  945                 else
  946                         error = 0;
  947         }
  948         splx(s);
  949         return(error);
  950 }
  951 
  952 int
  953 an_init(struct ifnet *ifp)
  954 {
  955         struct an_softc *sc = ifp->if_softc;
  956         struct ieee80211com *ic = &sc->sc_ic;
  957         int i, error, fid;
  958 
  959         DPRINTF(("an_init: enabled %d\n", sc->sc_enabled));
  960         if (!sc->sc_enabled) {
  961                 if (sc->sc_enable)
  962                         (*sc->sc_enable)(sc);
  963                 an_wait(sc);
  964                 sc->sc_enabled = 1;
  965         } else {
  966                 an_stop(ifp, 0);
  967                 if ((error = an_reset(sc)) != 0) {
  968                         printf("%s: failed to reset\n", ifp->if_xname);
  969                         an_stop(ifp, 1);
  970                         return error;
  971                 }
  972         }
  973         CSR_WRITE_2(sc, AN_SW0, AN_MAGIC);
  974 
  975         /* Allocate the TX buffers */
  976         for (i = 0; i < AN_TX_RING_CNT; i++) {
  977                 if ((error = an_alloc_nicmem(sc, AN_TX_MAX_LEN, &fid)) != 0) {
  978                         printf("%s: failed to allocate nic memory\n",
  979                             ifp->if_xname);
  980                         an_stop(ifp, 1);
  981                         return error;
  982                 }
  983                 DPRINTF2(("an_init: txbuf %d allocated %x\n", i, fid));
  984                 sc->sc_txd[i].d_fid = fid;
  985                 sc->sc_txd[i].d_inuse = 0;
  986         }
  987         sc->sc_txcur = sc->sc_txnext = 0;
  988 
  989         IEEE80211_ADDR_COPY(sc->sc_config.an_macaddr, ic->ic_myaddr);
  990         an_swap16((u_int16_t *)&sc->sc_config.an_macaddr, 3); 
  991         sc->sc_config.an_scanmode = AN_SCANMODE_ACTIVE;
  992         sc->sc_config.an_authtype = AN_AUTHTYPE_OPEN;   /*XXX*/
  993         if (ic->ic_flags & IEEE80211_F_WEPON) {
  994                 sc->sc_config.an_authtype |=
  995                     AN_AUTHTYPE_PRIVACY_IN_USE;
  996         }
  997         sc->sc_config.an_listen_interval = ic->ic_lintval;
  998         sc->sc_config.an_beacon_period = ic->ic_lintval;
  999         if (ic->ic_flags & IEEE80211_F_PMGTON)
 1000                 sc->sc_config.an_psave_mode = AN_PSAVE_PSP;
 1001         else
 1002                 sc->sc_config.an_psave_mode = AN_PSAVE_CAM;
 1003         sc->sc_config.an_ds_channel =
 1004             ieee80211_chan2ieee(ic, ic->ic_ibss_chan);
 1005 
 1006         switch (ic->ic_opmode) {
 1007         case IEEE80211_M_STA:
 1008                 sc->sc_config.an_opmode =
 1009                     AN_OPMODE_INFRASTRUCTURE_STATION;
 1010                 sc->sc_config.an_rxmode = AN_RXMODE_BC_MC_ADDR;
 1011                 break;
 1012 #ifndef IEEE80211_STA_ONLY
 1013         case IEEE80211_M_IBSS:
 1014                 sc->sc_config.an_opmode = AN_OPMODE_IBSS_ADHOC;
 1015                 sc->sc_config.an_rxmode = AN_RXMODE_BC_MC_ADDR;
 1016                 break;
 1017 #endif
 1018         case IEEE80211_M_MONITOR:
 1019                 sc->sc_config.an_opmode =
 1020                     AN_OPMODE_INFRASTRUCTURE_STATION;
 1021                 sc->sc_config.an_rxmode =
 1022                     AN_RXMODE_80211_MONITOR_ANYBSS;
 1023                 sc->sc_config.an_authtype = AN_AUTHTYPE_NONE;
 1024                 if (ic->ic_flags & IEEE80211_F_WEPON)
 1025                         sc->sc_config.an_authtype |=
 1026                             AN_AUTHTYPE_PRIVACY_IN_USE |
 1027                             AN_AUTHTYPE_ALLOW_UNENCRYPTED;
 1028                 break;
 1029         default:
 1030                 printf("%s: bad opmode %d\n", ifp->if_xname, ic->ic_opmode);
 1031                 an_stop(ifp, 1);
 1032                 return EIO;
 1033         }
 1034         sc->sc_config.an_rxmode |= AN_RXMODE_NO_8023_HEADER;
 1035 
 1036         /* Set the ssid list */
 1037         memset(&sc->sc_buf, 0, sizeof(sc->sc_buf.sc_ssidlist));
 1038         sc->sc_buf.sc_ssidlist.an_entry[0].an_ssid_len =
 1039             ic->ic_des_esslen;
 1040         if (ic->ic_des_esslen)
 1041                 memcpy(sc->sc_buf.sc_ssidlist.an_entry[0].an_ssid,
 1042                     ic->ic_des_essid, ic->ic_des_esslen);
 1043         an_swap16((u_int16_t *)&sc->sc_buf.sc_ssidlist.an_entry[0].an_ssid, 16); 
 1044         if (an_write_rid(sc, AN_RID_SSIDLIST, &sc->sc_buf,
 1045             sizeof(sc->sc_buf.sc_ssidlist)) != 0) {
 1046                 printf("%s: failed to write ssid list\n", ifp->if_xname);
 1047                 an_stop(ifp, 1);
 1048                 return error;
 1049         }
 1050 
 1051         /* Set the AP list */
 1052         memset(&sc->sc_buf, 0, sizeof(sc->sc_buf.sc_aplist));
 1053         (void)an_write_rid(sc, AN_RID_APLIST, &sc->sc_buf,
 1054             sizeof(sc->sc_buf.sc_aplist));
 1055 
 1056         /* Set the encapsulation */
 1057         for (i = 0; i < AN_ENCAP_NENTS; i++) {
 1058                 sc->sc_buf.sc_encap.an_entry[i].an_ethertype = 0;
 1059                 sc->sc_buf.sc_encap.an_entry[i].an_action =
 1060                     AN_RXENCAP_RFC1024 | AN_TXENCAP_RFC1024;
 1061         }
 1062         (void)an_write_rid(sc, AN_RID_ENCAP, &sc->sc_buf,
 1063             sizeof(sc->sc_buf.sc_encap));
 1064 
 1065         /* Set the WEP Keys */
 1066         if (ic->ic_flags & IEEE80211_F_WEPON)
 1067                 an_write_wepkey(sc, AN_RID_WEP_VOLATILE, sc->sc_wepkeys,
 1068                     sc->sc_tx_key);
 1069 
 1070         /* Set the configuration */
 1071         if (an_write_rid(sc, AN_RID_GENCONFIG, &sc->sc_config,
 1072             sizeof(sc->sc_config)) != 0) {
 1073                 printf("%s: failed to write config\n", ifp->if_xname);
 1074                 an_stop(ifp, 1);
 1075                 return error;
 1076         }
 1077 
 1078         /* Enable the MAC */
 1079         if (an_cmd(sc, AN_CMD_ENABLE, 0)) {
 1080                 printf("%s: failed to enable MAC\n", sc->sc_dev.dv_xname);
 1081                 an_stop(ifp, 1);
 1082                 return ENXIO;
 1083         }
 1084         if (ifp->if_flags & IFF_PROMISC)
 1085                 an_cmd(sc, AN_CMD_SET_MODE, 0xffff);
 1086 
 1087         ifp->if_flags |= IFF_RUNNING;
 1088         ifp->if_flags &= ~IFF_OACTIVE;
 1089         ic->ic_state = IEEE80211_S_INIT;
 1090         if (ic->ic_opmode == IEEE80211_M_MONITOR)
 1091                 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
 1092 
 1093         /* enable interrupts */
 1094         CSR_WRITE_2(sc, AN_INT_EN, AN_INTRS);
 1095         return 0;
 1096 }
 1097 
 1098 void
 1099 an_start(struct ifnet *ifp)
 1100 {
 1101         struct an_softc *sc = (struct an_softc *)ifp->if_softc;
 1102         struct ieee80211com *ic = &sc->sc_ic;
 1103         struct ieee80211_node *ni;
 1104         struct ieee80211_frame *wh;
 1105         struct an_txframe frmhdr;
 1106         struct mbuf *m;
 1107         u_int16_t len;
 1108         int cur, fid;
 1109 
 1110         if (!sc->sc_enabled || sc->sc_invalid) {
 1111                 DPRINTF(("an_start: noop: enabled %d invalid %d\n",
 1112                     sc->sc_enabled, sc->sc_invalid));
 1113                 return;
 1114         }
 1115 
 1116         memset(&frmhdr, 0, sizeof(frmhdr));
 1117         cur = sc->sc_txnext;
 1118         for (;;) {
 1119                 if (ic->ic_state != IEEE80211_S_RUN) {
 1120                         DPRINTF(("an_start: not running %d\n", ic->ic_state));
 1121                         break;
 1122                 }
 1123                 IFQ_POLL(&ifp->if_snd, m);
 1124                 if (m == NULL) {
 1125                         DPRINTF2(("an_start: no pending mbuf\n"));
 1126                         break;
 1127                 }
 1128                 if (sc->sc_txd[cur].d_inuse) {
 1129                         DPRINTF2(("an_start: %x/%d busy\n",
 1130                             sc->sc_txd[cur].d_fid, cur));
 1131                         ifp->if_flags |= IFF_OACTIVE;
 1132                         break;
 1133                 }
 1134                 IFQ_DEQUEUE(&ifp->if_snd, m);
 1135                 ifp->if_opackets++;
 1136 #if NBPFILTER > 0
 1137                 if (ifp->if_bpf)
 1138                         bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT);
 1139 #endif
 1140                 if ((m = ieee80211_encap(ifp, m, &ni)) == NULL) {
 1141                         ifp->if_oerrors++;
 1142                         continue;
 1143                 }
 1144                 if (ni != NULL)
 1145                         ieee80211_release_node(ic, ni);
 1146 #if NBPFILTER > 0
 1147                 if (ic->ic_rawbpf)
 1148                         bpf_mtap(ic->ic_rawbpf, m, BPF_DIRECTION_OUT);
 1149 #endif
 1150 
 1151                 wh = mtod(m, struct ieee80211_frame *);
 1152                 if (ic->ic_flags & IEEE80211_F_WEPON)
 1153                         wh->i_fc[1] |= IEEE80211_FC1_WEP;
 1154                 m_copydata(m, 0, sizeof(struct ieee80211_frame),
 1155                     (caddr_t)&frmhdr.an_whdr);
 1156                 an_swap16((u_int16_t *)&frmhdr.an_whdr, sizeof(struct ieee80211_frame)/2);
 1157 
 1158                 /* insert payload length in front of llc/snap */
 1159                 len = htons(m->m_pkthdr.len - sizeof(struct ieee80211_frame));
 1160                 m_adj(m, sizeof(struct ieee80211_frame) - sizeof(len));
 1161                 if (mtod(m, u_long) & 0x01)
 1162                         memcpy(mtod(m, caddr_t), &len, sizeof(len));
 1163                 else
 1164                         *mtod(m, u_int16_t *) = len;
 1165 
 1166                 /*
 1167                  * XXX Aironet firmware apparently convert the packet
 1168                  * with longer than 1500 bytes in length into LLC/SNAP.
 1169                  * If we have 1500 bytes in ethernet payload, it is
 1170                  * 1508 bytes including LLC/SNAP and will be inserted
 1171                  * additional LLC/SNAP header with 1501-1508 in its
 1172                  * ethertype !!
 1173                  * So we skip LLC/SNAP header and force firmware to
 1174                  * convert it to LLC/SNAP again.
 1175                  */
 1176                 m_adj(m, sizeof(struct llc));
 1177 
 1178                 frmhdr.an_tx_ctl = AN_TXCTL_80211;
 1179                 frmhdr.an_tx_payload_len = m->m_pkthdr.len;
 1180                 frmhdr.an_gaplen = AN_TXGAP_802_11;
 1181 
 1182                 if (ic->ic_fixed_rate != -1)
 1183                         frmhdr.an_tx_rate =
 1184                             ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[
 1185                             ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
 1186                 else
 1187                         frmhdr.an_tx_rate = 0;
 1188 
 1189                 if (sizeof(frmhdr) + AN_TXGAP_802_11 + sizeof(len) +
 1190                     m->m_pkthdr.len > AN_TX_MAX_LEN) {
 1191                         ifp->if_oerrors++;
 1192                         m_freem(m);
 1193                         continue;
 1194                 }
 1195 
 1196 #if NBPFILTER > 0
 1197                 if (sc->sc_drvbpf) {
 1198                         struct mbuf mb;
 1199                         struct an_tx_radiotap_header *tap = &sc->sc_txtap;
 1200 
 1201                         tap->at_rate = 
 1202                             ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate];
 1203                         tap->at_chan_freq =
 1204                             ic->ic_bss->ni_chan->ic_freq;
 1205                         tap->at_chan_flags =
 1206                             ic->ic_bss->ni_chan->ic_flags;
 1207 
 1208                         mb.m_data = (caddr_t)tap;
 1209                         mb.m_len = sizeof(sc->sc_txtapu);
 1210                         mb.m_next = m;
 1211                         mb.m_nextpkt = NULL;
 1212                         mb.m_type = 0;
 1213                         mb.m_flags = 0;
 1214                         bpf_mtap(sc->sc_drvbpf, m, BPF_DIRECTION_OUT);
 1215                 }
 1216 #endif
 1217 
 1218                 fid = sc->sc_txd[cur].d_fid;
 1219                 if (an_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0) {
 1220                         ifp->if_oerrors++;
 1221                         m_freem(m);
 1222                         continue;
 1223                 }
 1224                 /* dummy write to avoid seek. */
 1225                 an_write_bap(sc, fid, -1, &frmhdr, AN_TXGAP_802_11);
 1226                 an_mwrite_bap(sc, fid, -1, m, m->m_pkthdr.len);
 1227                 m_freem(m);
 1228 
 1229                 DPRINTF2(("an_start: send %d byte via %x/%d\n",
 1230                     ntohs(len) + sizeof(struct ieee80211_frame),
 1231                     fid, cur));
 1232                 sc->sc_txd[cur].d_inuse = 1;
 1233                 if (an_cmd(sc, AN_CMD_TX, fid)) {
 1234                         printf("%s: xmit failed\n", ifp->if_xname);
 1235                         sc->sc_txd[cur].d_inuse = 0;
 1236                         continue;
 1237                 }
 1238                 sc->sc_tx_timer = 5;
 1239                 ifp->if_timer = 1;
 1240                 AN_INC(cur, AN_TX_RING_CNT);
 1241                 sc->sc_txnext = cur;
 1242         }
 1243 }
 1244 
 1245 void
 1246 an_stop(struct ifnet *ifp, int disable)
 1247 {
 1248         struct an_softc *sc = ifp->if_softc;
 1249         int i, s;
 1250 
 1251         if (!sc->sc_enabled)
 1252                 return;
 1253 
 1254         DPRINTF(("an_stop: disable %d\n", disable));
 1255 
 1256         s = splnet();
 1257         ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1);
 1258         if (!sc->sc_invalid) {
 1259                 an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0);
 1260                 CSR_WRITE_2(sc, AN_INT_EN, 0);
 1261                 an_cmd(sc, AN_CMD_DISABLE, 0);
 1262 
 1263                 for (i = 0; i < AN_TX_RING_CNT; i++)
 1264                         an_cmd(sc, AN_CMD_DEALLOC_MEM, sc->sc_txd[i].d_fid);
 1265         }
 1266 
 1267         sc->sc_tx_timer = 0;
 1268         ifp->if_timer = 0;
 1269         ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
 1270 
 1271         if (disable) {
 1272                 if (sc->sc_disable)
 1273                         (*sc->sc_disable)(sc);
 1274                 sc->sc_enabled = 0;
 1275         }
 1276         splx(s);
 1277 }
 1278 
 1279 void
 1280 an_watchdog(struct ifnet *ifp)
 1281 {
 1282         struct an_softc *sc = ifp->if_softc;
 1283 
 1284         if (!sc->sc_enabled)
 1285                 return;
 1286 
 1287         if (sc->sc_tx_timer) {
 1288                 if (--sc->sc_tx_timer == 0) {
 1289                         printf("%s: device timeout\n", ifp->if_xname);
 1290                         ifp->if_oerrors++;
 1291                         an_init(ifp);
 1292                         return;
 1293                 }
 1294                 ifp->if_timer = 1;
 1295         }
 1296         ieee80211_watchdog(ifp);
 1297 }
 1298 
 1299 void
 1300 an_shutdown(void *self)
 1301 {
 1302         struct an_softc *sc = (struct an_softc *)self;
 1303 
 1304         if (sc->sc_attached)
 1305                 an_stop(&sc->sc_ic.ic_if, 1);
 1306 }
 1307 
 1308 /* TBD factor with ieee80211_media_change */
 1309 int
 1310 an_media_change(struct ifnet *ifp)
 1311 {
 1312         struct an_softc *sc = ifp->if_softc;
 1313         struct ieee80211com *ic = &sc->sc_ic;
 1314         struct ifmedia_entry *ime;
 1315         enum ieee80211_opmode newmode;
 1316         int i, rate, error = 0;
 1317 
 1318         ime = ic->ic_media.ifm_cur;
 1319         if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) {
 1320                 i = -1;
 1321         } else {
 1322                 struct ieee80211_rateset *rs =
 1323                     &ic->ic_sup_rates[IEEE80211_MODE_11B];
 1324                 rate = ieee80211_media2rate(ime->ifm_media);
 1325                 if (rate == 0)
 1326                         return EINVAL;
 1327                 for (i = 0; i < rs->rs_nrates; i++) {
 1328                         if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
 1329                                 break;
 1330                 }
 1331                 if (i == rs->rs_nrates)
 1332                         return EINVAL;
 1333         }
 1334         if (ic->ic_fixed_rate != i) {
 1335                 ic->ic_fixed_rate = i;
 1336                 error = ENETRESET;
 1337         }
 1338 
 1339 #ifndef IEEE80211_STA_ONLY
 1340         if (ime->ifm_media & IFM_IEEE80211_ADHOC)
 1341                 newmode = IEEE80211_M_IBSS;
 1342         else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
 1343                 newmode = IEEE80211_M_HOSTAP;
 1344         else
 1345 #endif
 1346         if (ime->ifm_media & IFM_IEEE80211_MONITOR)
 1347                 newmode = IEEE80211_M_MONITOR;
 1348         else
 1349                 newmode = IEEE80211_M_STA;
 1350         if (ic->ic_opmode != newmode) {
 1351                 ic->ic_opmode = newmode;
 1352                 error = ENETRESET;
 1353         }
 1354         if (error == ENETRESET) {
 1355                 if (sc->sc_enabled)
 1356                         error = an_init(ifp);
 1357                 else
 1358                         error = 0;
 1359         }
 1360         ifp->if_baudrate = ifmedia_baudrate(ic->ic_media.ifm_cur->ifm_media);
 1361 
 1362         return error;
 1363 }
 1364 
 1365 void
 1366 an_media_status(struct ifnet *ifp, struct ifmediareq *imr)
 1367 {
 1368         struct an_softc *sc = ifp->if_softc;
 1369         struct ieee80211com *ic = &sc->sc_ic;
 1370         int rate, buflen;
 1371 
 1372         if (sc->sc_enabled == 0) {
 1373                 imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
 1374                 imr->ifm_status = 0;
 1375                 return;
 1376         }
 1377 
 1378         imr->ifm_status = IFM_AVALID;
 1379         imr->ifm_active = IFM_IEEE80211;
 1380         if (ic->ic_state == IEEE80211_S_RUN)
 1381                 imr->ifm_status |= IFM_ACTIVE;
 1382         buflen = sizeof(sc->sc_buf);
 1383         if (ic->ic_fixed_rate != -1)
 1384                 rate = ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[
 1385                     ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
 1386         else if (an_read_rid(sc, AN_RID_STATUS, &sc->sc_buf, &buflen) != 0)
 1387                 rate = 0;
 1388         else
 1389                 rate = sc->sc_buf.sc_status.an_current_tx_rate;
 1390         imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
 1391         switch (ic->ic_opmode) {
 1392         case IEEE80211_M_STA:
 1393                 break;
 1394 #ifndef IEEE80211_STA_ONLY
 1395         case IEEE80211_M_IBSS:
 1396                 imr->ifm_active |= IFM_IEEE80211_ADHOC;
 1397                 break;
 1398         case IEEE80211_M_HOSTAP:
 1399                 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
 1400                 break;
 1401 #endif
 1402         case IEEE80211_M_MONITOR:
 1403                 imr->ifm_active |= IFM_IEEE80211_MONITOR;
 1404                 break;
 1405         default:
 1406                 break;
 1407         }
 1408 }
 1409 
 1410 int
 1411 an_set_nwkey(struct an_softc *sc, struct ieee80211_nwkey *nwkey)
 1412 {
 1413         int error;
 1414         struct ieee80211com *ic = &sc->sc_ic;
 1415         u_int16_t prevauth;
 1416 
 1417         error = 0;
 1418         prevauth = sc->sc_config.an_authtype;
 1419 
 1420         switch (nwkey->i_wepon) {
 1421         case IEEE80211_NWKEY_OPEN:
 1422                 sc->sc_config.an_authtype = AN_AUTHTYPE_OPEN;
 1423                 ic->ic_flags &= ~IEEE80211_F_WEPON;
 1424                 break;
 1425 
 1426         case IEEE80211_NWKEY_WEP:
 1427         case IEEE80211_NWKEY_WEP | IEEE80211_NWKEY_PERSIST:
 1428                 error = an_set_nwkey_wep(sc, nwkey);
 1429                 if (error == 0 || error == ENETRESET) {
 1430                         sc->sc_config.an_authtype =
 1431                             AN_AUTHTYPE_OPEN | AN_AUTHTYPE_PRIVACY_IN_USE;
 1432                         ic->ic_flags |= IEEE80211_F_WEPON;
 1433                 }
 1434                 break;
 1435 
 1436         default:
 1437                 error = EINVAL;
 1438                 break;
 1439         }
 1440         if (error == 0 && prevauth != sc->sc_config.an_authtype)
 1441                 error = ENETRESET;
 1442         return error;
 1443 }
 1444 
 1445 int
 1446 an_set_nwkey_wep(struct an_softc *sc, struct ieee80211_nwkey *nwkey)
 1447 {
 1448         int i, txkey, anysetkey, needreset, error;
 1449         struct an_wepkey keys[IEEE80211_WEP_NKID];
 1450 
 1451         error = 0;
 1452         memset(keys, 0, sizeof(keys));
 1453         anysetkey = needreset = 0;
 1454 
 1455         /* load argument and sanity check */
 1456         for (i = 0; i < IEEE80211_WEP_NKID; i++) {
 1457                 keys[i].an_wep_keylen = nwkey->i_key[i].i_keylen;
 1458                 if (keys[i].an_wep_keylen < 0)
 1459                         continue;
 1460                 if (keys[i].an_wep_keylen != 0 &&
 1461                     keys[i].an_wep_keylen < IEEE80211_WEP_KEYLEN)
 1462                         return EINVAL;
 1463                 if (keys[i].an_wep_keylen > sizeof(keys[i].an_wep_key))
 1464                         return EINVAL;
 1465                 if ((error = copyin(nwkey->i_key[i].i_keydat,
 1466                     keys[i].an_wep_key, keys[i].an_wep_keylen)) != 0)
 1467                         return error;
 1468                 anysetkey++;
 1469         }
 1470         txkey = nwkey->i_defkid - 1;
 1471         if (txkey >= 0) {
 1472                 if (txkey >= IEEE80211_WEP_NKID)
 1473                         return EINVAL;
 1474                 /* default key must have a valid value */
 1475                 if (keys[txkey].an_wep_keylen == 0 ||
 1476                     (keys[txkey].an_wep_keylen < 0 &&
 1477                     sc->sc_perskeylen[txkey] == 0))
 1478                         return EINVAL;
 1479                 anysetkey++;
 1480         }
 1481         DPRINTF(("an_set_nwkey_wep: %s: %sold(%d:%d,%d,%d,%d) "
 1482             "pers(%d:%d,%d,%d,%d) new(%d:%d,%d,%d,%d)\n",
 1483             sc->sc_dev.dv_xname,
 1484             ((nwkey->i_wepon & IEEE80211_NWKEY_PERSIST) ? "persist: " : ""),
 1485             sc->sc_tx_key,
 1486             sc->sc_wepkeys[0].an_wep_keylen, sc->sc_wepkeys[1].an_wep_keylen,
 1487             sc->sc_wepkeys[2].an_wep_keylen, sc->sc_wepkeys[3].an_wep_keylen,
 1488             sc->sc_tx_perskey,
 1489             sc->sc_perskeylen[0], sc->sc_perskeylen[1],
 1490             sc->sc_perskeylen[2], sc->sc_perskeylen[3],
 1491             txkey,
 1492             keys[0].an_wep_keylen, keys[1].an_wep_keylen,
 1493             keys[2].an_wep_keylen, keys[3].an_wep_keylen));
 1494         if (!(nwkey->i_wepon & IEEE80211_NWKEY_PERSIST)) {
 1495                 /* set temporary keys */
 1496                 sc->sc_tx_key = txkey;
 1497                 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
 1498                         if (keys[i].an_wep_keylen < 0)
 1499                                 continue;
 1500                         memcpy(&sc->sc_wepkeys[i], &keys[i], sizeof(keys[i]));
 1501                 }
 1502         } else {
 1503                 /* set persist keys */
 1504                 if (anysetkey) {
 1505                         /* prepare to write nvram */
 1506                         if (!sc->sc_enabled) {
 1507                                 if (sc->sc_enable)
 1508                                         (*sc->sc_enable)(sc);
 1509                                 an_wait(sc);
 1510                                 sc->sc_enabled = 1;
 1511                                 error = an_write_wepkey(sc,
 1512                                     AN_RID_WEP_PERSISTENT, keys, txkey);
 1513                                 if (sc->sc_disable)
 1514                                         (*sc->sc_disable)(sc);
 1515                                 sc->sc_enabled = 0;
 1516                         } else {
 1517                                 an_cmd(sc, AN_CMD_DISABLE, 0);
 1518                                 error = an_write_wepkey(sc,
 1519                                     AN_RID_WEP_PERSISTENT, keys, txkey);
 1520                                 an_cmd(sc, AN_CMD_ENABLE, 0);
 1521                         }
 1522                         if (error)
 1523                                 return error;
 1524                 }
 1525                 if (txkey >= 0)
 1526                         sc->sc_tx_perskey = txkey;
 1527                 if (sc->sc_tx_key >= 0) {
 1528                         sc->sc_tx_key = -1;
 1529                         needreset++;
 1530                 }
 1531                 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
 1532                         if (sc->sc_wepkeys[i].an_wep_keylen >= 0) {
 1533                                 memset(&sc->sc_wepkeys[i].an_wep_key, 0,
 1534                                     sizeof(sc->sc_wepkeys[i].an_wep_key));
 1535                                 sc->sc_wepkeys[i].an_wep_keylen = -1;
 1536                                 needreset++;
 1537                         }
 1538                         if (keys[i].an_wep_keylen >= 0)
 1539                                 sc->sc_perskeylen[i] = keys[i].an_wep_keylen;
 1540                 }
 1541         }
 1542         if (needreset) {
 1543                 /* firmware restart to reload persistent key */
 1544                 an_reset(sc);
 1545         }
 1546         if (anysetkey || needreset)
 1547                 error = ENETRESET;
 1548         return error;
 1549 }
 1550 
 1551 int
 1552 an_get_nwkey(struct an_softc *sc, struct ieee80211_nwkey *nwkey)
 1553 {
 1554         int i, error;
 1555 
 1556         error = 0;
 1557         if (sc->sc_config.an_authtype & AN_AUTHTYPE_LEAP)
 1558                 nwkey->i_wepon = IEEE80211_NWKEY_EAP;
 1559         else if (sc->sc_config.an_authtype & AN_AUTHTYPE_PRIVACY_IN_USE)
 1560                 nwkey->i_wepon = IEEE80211_NWKEY_WEP;
 1561         else
 1562                 nwkey->i_wepon = IEEE80211_NWKEY_OPEN;
 1563         if (sc->sc_tx_key == -1)
 1564                 nwkey->i_defkid = sc->sc_tx_perskey + 1;
 1565         else
 1566                 nwkey->i_defkid = sc->sc_tx_key + 1;
 1567         if (nwkey->i_key[0].i_keydat == NULL)
 1568                 return 0;
 1569         for (i = 0; i < IEEE80211_WEP_NKID; i++) {
 1570                 if (nwkey->i_key[i].i_keydat == NULL)
 1571                         continue;
 1572                 /* do not show any keys to non-root user */
 1573                 if ((error = suser(curproc, 0)) != 0)
 1574                         break;
 1575                 nwkey->i_key[i].i_keylen = sc->sc_wepkeys[i].an_wep_keylen;
 1576                 if (nwkey->i_key[i].i_keylen < 0) {
 1577                         if (sc->sc_perskeylen[i] == 0)
 1578                                 nwkey->i_key[i].i_keylen = 0;
 1579                         continue;
 1580                 }
 1581                 if ((error = copyout(sc->sc_wepkeys[i].an_wep_key,
 1582                     nwkey->i_key[i].i_keydat,
 1583                     sc->sc_wepkeys[i].an_wep_keylen)) != 0)
 1584                         break;
 1585         }
 1586         return error;
 1587 }
 1588 
 1589 int
 1590 an_write_wepkey(struct an_softc *sc, int type, struct an_wepkey *keys, int kid)
 1591 {
 1592         int i, error;
 1593         struct an_rid_wepkey *akey;
 1594 
 1595         error = 0;
 1596         akey = &sc->sc_buf.sc_wepkey;
 1597         for (i = 0; i < IEEE80211_WEP_NKID; i++) {
 1598                 memset(akey, 0, sizeof(struct an_rid_wepkey));
 1599                 if (keys[i].an_wep_keylen < 0 ||
 1600                     keys[i].an_wep_keylen > sizeof(akey->an_key))
 1601                         continue;
 1602                 akey->an_key_len = keys[i].an_wep_keylen;
 1603                 akey->an_key_index = i;
 1604                 akey->an_mac_addr[0] = 1;       /* default mac */
 1605                 an_swap16((u_int16_t *)&akey->an_mac_addr, 3); 
 1606                 memcpy(akey->an_key, keys[i].an_wep_key, keys[i].an_wep_keylen);
 1607                 an_swap16((u_int16_t *)&akey->an_key, 8); 
 1608                 if ((error = an_write_rid(sc, type, akey, sizeof(*akey))) != 0)
 1609                         return error;
 1610         }
 1611         if (kid >= 0) {
 1612                 memset(akey, 0, sizeof(struct an_rid_wepkey));
 1613                 akey->an_key_index = 0xffff;
 1614                 akey->an_mac_addr[0] = kid;
 1615                 an_swap16((u_int16_t *)&akey->an_mac_addr, 3); 
 1616                 akey->an_key_len = 0;
 1617                 memset(akey->an_key, 0, sizeof(akey->an_key));
 1618                 error = an_write_rid(sc, type, akey, sizeof(*akey));
 1619         }
 1620         return error;
 1621 }
 1622 
 1623 int
 1624 an_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
 1625 {
 1626         struct an_softc *sc = ic->ic_softc;
 1627         struct ieee80211_node *ni = ic->ic_bss;
 1628         enum ieee80211_state ostate;
 1629         int buflen;
 1630 
 1631         ostate = ic->ic_state;
 1632         DPRINTF(("an_newstate: %s -> %s\n", ieee80211_state_name[ostate],
 1633             ieee80211_state_name[nstate]));
 1634 
 1635         switch (nstate) {
 1636         case IEEE80211_S_INIT:
 1637                 ic->ic_flags &= ~IEEE80211_F_IBSSON;
 1638                 return (*sc->sc_newstate)(ic, nstate, arg);
 1639 
 1640         case IEEE80211_S_RUN:
 1641                 buflen = sizeof(sc->sc_buf);
 1642                 an_read_rid(sc, AN_RID_STATUS, &sc->sc_buf, &buflen);
 1643                 an_swap16((u_int16_t *)&sc->sc_buf.sc_status.an_cur_bssid, 3); 
 1644                 an_swap16((u_int16_t *)&sc->sc_buf.sc_status.an_ssid, 16); 
 1645                 IEEE80211_ADDR_COPY(ni->ni_bssid,
 1646                     sc->sc_buf.sc_status.an_cur_bssid);
 1647                 IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
 1648                 ni->ni_chan = &ic->ic_channels[
 1649                     sc->sc_buf.sc_status.an_cur_channel];
 1650                 ni->ni_esslen = sc->sc_buf.sc_status.an_ssidlen;
 1651                 if (ni->ni_esslen > IEEE80211_NWID_LEN)
 1652                         ni->ni_esslen = IEEE80211_NWID_LEN;     /*XXX*/
 1653                 memcpy(ni->ni_essid, sc->sc_buf.sc_status.an_ssid,
 1654                     ni->ni_esslen);
 1655                 ni->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B];    /*XXX*/
 1656                 if (ic->ic_if.if_flags & IFF_DEBUG) {
 1657                         printf("%s: ", sc->sc_dev.dv_xname);
 1658                         if (ic->ic_opmode == IEEE80211_M_STA)
 1659                                 printf("associated ");
 1660                         else
 1661                                 printf("synchronized ");
 1662                         printf("with %s ssid ", ether_sprintf(ni->ni_bssid));
 1663                         ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
 1664                         printf(" channel %u start %uMb\n",
 1665                             sc->sc_buf.sc_status.an_cur_channel,
 1666                             sc->sc_buf.sc_status.an_current_tx_rate/2);
 1667                 }
 1668                 break;
 1669 
 1670         default:
 1671                 break;
 1672         }
 1673         ic->ic_state = nstate;
 1674         /* skip standard ieee80211 handling */
 1675         return 0;
 1676 }
 1677 
 1678 int
 1679 an_detach(struct an_softc *sc)
 1680 {
 1681         struct ifnet *ifp = &sc->sc_ic.ic_if;
 1682         int s;
 1683 
 1684         if (!sc->sc_attached)
 1685                 return 0;
 1686 
 1687         s = splnet();
 1688         sc->sc_invalid = 1;
 1689         an_stop(ifp, 1);
 1690         ifmedia_delete_instance(&sc->sc_ic.ic_media, IFM_INST_ANY);
 1691         ieee80211_ifdetach(ifp);
 1692         if_detach(ifp);
 1693         if (sc->sc_sdhook != NULL)
 1694                 shutdownhook_disestablish(sc->sc_sdhook);
 1695         splx(s);
 1696         return 0;
 1697 }
 1698 

Cache object: daf4c30650a5c513311492c2ac32d154


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.