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/wi/if_wi.c

Version: -  FREEBSD  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  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 /*-
    2  * Copyright (c) 1997, 1998, 1999
    3  *      Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *      This product includes software developed by Bill Paul.
   16  * 4. Neither the name of the author nor the names of any co-contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
   24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
   30  * THE POSSIBILITY OF SUCH DAMAGE.
   31  */
   32 
   33 /*
   34  * Lucent WaveLAN/IEEE 802.11 PCMCIA driver.
   35  *
   36  * Original FreeBSD driver written by Bill Paul <wpaul@ctr.columbia.edu>
   37  * Electrical Engineering Department
   38  * Columbia University, New York City
   39  */
   40 
   41 /*
   42  * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
   43  * from Lucent. Unlike the older cards, the new ones are programmed
   44  * entirely via a firmware-driven controller called the Hermes.
   45  * Unfortunately, Lucent will not release the Hermes programming manual
   46  * without an NDA (if at all). What they do release is an API library
   47  * called the HCF (Hardware Control Functions) which is supposed to
   48  * do the device-specific operations of a device driver for you. The
   49  * publically available version of the HCF library (the 'HCF Light') is 
   50  * a) extremely gross, b) lacks certain features, particularly support
   51  * for 802.11 frames, and c) is contaminated by the GNU Public License.
   52  *
   53  * This driver does not use the HCF or HCF Light at all. Instead, it
   54  * programs the Hermes controller directly, using information gleaned
   55  * from the HCF Light code and corresponding documentation.
   56  *
   57  * This driver supports the ISA, PCMCIA and PCI versions of the Lucent
   58  * WaveLan cards (based on the Hermes chipset), as well as the newer
   59  * Prism 2 chipsets with firmware from Intersil and Symbol.
   60  */
   61 
   62 #include <sys/cdefs.h>
   63 __FBSDID("$FreeBSD: head/sys/dev/wi/if_wi.c 272068 2014-09-24 12:19:00Z glebius $");
   64 
   65 #include "opt_wlan.h"
   66 
   67 #define WI_HERMES_STATS_WAR     /* Work around stats counter bug. */
   68 
   69 #include <sys/param.h>
   70 #include <sys/systm.h>
   71 #include <sys/endian.h>
   72 #include <sys/sockio.h>
   73 #include <sys/mbuf.h>
   74 #include <sys/priv.h>
   75 #include <sys/proc.h>
   76 #include <sys/kernel.h>
   77 #include <sys/socket.h>
   78 #include <sys/module.h>
   79 #include <sys/bus.h>
   80 #include <sys/random.h>
   81 #include <sys/syslog.h>
   82 #include <sys/sysctl.h>
   83 
   84 #include <machine/bus.h>
   85 #include <machine/resource.h>
   86 #include <machine/atomic.h>
   87 #include <sys/rman.h>
   88 
   89 #include <net/if.h>
   90 #include <net/if_var.h>
   91 #include <net/if_arp.h>
   92 #include <net/ethernet.h>
   93 #include <net/if_dl.h>
   94 #include <net/if_llc.h>
   95 #include <net/if_media.h>
   96 #include <net/if_types.h>
   97 
   98 #include <net80211/ieee80211_var.h>
   99 #include <net80211/ieee80211_ioctl.h>
  100 #include <net80211/ieee80211_radiotap.h>
  101 
  102 #include <netinet/in.h>
  103 #include <netinet/in_systm.h>
  104 #include <netinet/in_var.h>
  105 #include <netinet/ip.h>
  106 #include <netinet/if_ether.h>
  107 
  108 #include <net/bpf.h>
  109 
  110 #include <dev/wi/if_wavelan_ieee.h>
  111 #include <dev/wi/if_wireg.h>
  112 #include <dev/wi/if_wivar.h>
  113 
  114 static struct ieee80211vap *wi_vap_create(struct ieee80211com *,
  115                     const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
  116                     const uint8_t [IEEE80211_ADDR_LEN],
  117                     const uint8_t [IEEE80211_ADDR_LEN]);
  118 static void wi_vap_delete(struct ieee80211vap *vap);
  119 static void wi_stop_locked(struct wi_softc *sc, int disable);
  120 static void wi_start_locked(struct ifnet *);
  121 static void wi_start(struct ifnet *);
  122 static int  wi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr,
  123                 struct mbuf *m0);
  124 static int  wi_raw_xmit(struct ieee80211_node *, struct mbuf *,
  125                 const struct ieee80211_bpf_params *);
  126 static int  wi_newstate_sta(struct ieee80211vap *, enum ieee80211_state, int);
  127 static int  wi_newstate_hostap(struct ieee80211vap *, enum ieee80211_state,
  128                 int);
  129 static void wi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
  130                 int subtype, int rssi, int nf);
  131 static int  wi_reset(struct wi_softc *);
  132 static void wi_watchdog(void *);
  133 static int  wi_ioctl(struct ifnet *, u_long, caddr_t);
  134 static void wi_media_status(struct ifnet *, struct ifmediareq *);
  135 static uint64_t wi_get_counter(struct ifnet *, ift_counter);
  136 
  137 static void wi_rx_intr(struct wi_softc *);
  138 static void wi_tx_intr(struct wi_softc *);
  139 static void wi_tx_ex_intr(struct wi_softc *);
  140 
  141 static void wi_info_intr(struct wi_softc *);
  142 
  143 static int  wi_write_txrate(struct wi_softc *, struct ieee80211vap *);
  144 static int  wi_write_wep(struct wi_softc *, struct ieee80211vap *);
  145 static int  wi_write_multi(struct wi_softc *);
  146 static void wi_update_mcast(struct ifnet *);
  147 static void wi_update_promisc(struct ifnet *);
  148 static int  wi_alloc_fid(struct wi_softc *, int, int *);
  149 static void wi_read_nicid(struct wi_softc *);
  150 static int  wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
  151 
  152 static int  wi_cmd(struct wi_softc *, int, int, int, int);
  153 static int  wi_seek_bap(struct wi_softc *, int, int);
  154 static int  wi_read_bap(struct wi_softc *, int, int, void *, int);
  155 static int  wi_write_bap(struct wi_softc *, int, int, void *, int);
  156 static int  wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
  157 static int  wi_read_rid(struct wi_softc *, int, void *, int *);
  158 static int  wi_write_rid(struct wi_softc *, int, void *, int);
  159 static int  wi_write_appie(struct wi_softc *, int, const struct ieee80211_appie *);
  160 
  161 static void wi_scan_start(struct ieee80211com *);
  162 static void wi_scan_end(struct ieee80211com *);
  163 static void wi_set_channel(struct ieee80211com *);
  164         
  165 static __inline int
  166 wi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
  167 {
  168 
  169         val = htole16(val);
  170         return wi_write_rid(sc, rid, &val, sizeof(val));
  171 }
  172 
  173 static SYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0,
  174             "Wireless driver parameters");
  175 
  176 static  struct timeval lasttxerror;     /* time of last tx error msg */
  177 static  int curtxeps;                   /* current tx error msgs/sec */
  178 static  int wi_txerate = 0;             /* tx error rate: max msgs/sec */
  179 SYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate,
  180             0, "max tx error msgs/sec; 0 to disable msgs");
  181 
  182 #define WI_DEBUG
  183 #ifdef WI_DEBUG
  184 static  int wi_debug = 0;
  185 SYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug,
  186             0, "control debugging printfs");
  187 #define DPRINTF(X)      if (wi_debug) printf X
  188 #else
  189 #define DPRINTF(X)
  190 #endif
  191 
  192 #define WI_INTRS        (WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
  193 
  194 struct wi_card_ident wi_card_ident[] = {
  195         /* CARD_ID                      CARD_NAME               FIRM_TYPE */
  196         { WI_NIC_LUCENT_ID,             WI_NIC_LUCENT_STR,      WI_LUCENT },
  197         { WI_NIC_SONY_ID,               WI_NIC_SONY_STR,        WI_LUCENT },
  198         { WI_NIC_LUCENT_EMB_ID,         WI_NIC_LUCENT_EMB_STR,  WI_LUCENT },
  199         { WI_NIC_EVB2_ID,               WI_NIC_EVB2_STR,        WI_INTERSIL },
  200         { WI_NIC_HWB3763_ID,            WI_NIC_HWB3763_STR,     WI_INTERSIL },
  201         { WI_NIC_HWB3163_ID,            WI_NIC_HWB3163_STR,     WI_INTERSIL },
  202         { WI_NIC_HWB3163B_ID,           WI_NIC_HWB3163B_STR,    WI_INTERSIL },
  203         { WI_NIC_EVB3_ID,               WI_NIC_EVB3_STR,        WI_INTERSIL },
  204         { WI_NIC_HWB1153_ID,            WI_NIC_HWB1153_STR,     WI_INTERSIL },
  205         { WI_NIC_P2_SST_ID,             WI_NIC_P2_SST_STR,      WI_INTERSIL },
  206         { WI_NIC_EVB2_SST_ID,           WI_NIC_EVB2_SST_STR,    WI_INTERSIL },
  207         { WI_NIC_3842_EVA_ID,           WI_NIC_3842_EVA_STR,    WI_INTERSIL },
  208         { WI_NIC_3842_PCMCIA_AMD_ID,    WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
  209         { WI_NIC_3842_PCMCIA_SST_ID,    WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
  210         { WI_NIC_3842_PCMCIA_ATL_ID,    WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
  211         { WI_NIC_3842_PCMCIA_ATS_ID,    WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
  212         { WI_NIC_3842_MINI_AMD_ID,      WI_NIC_3842_MINI_STR,   WI_INTERSIL },
  213         { WI_NIC_3842_MINI_SST_ID,      WI_NIC_3842_MINI_STR,   WI_INTERSIL },
  214         { WI_NIC_3842_MINI_ATL_ID,      WI_NIC_3842_MINI_STR,   WI_INTERSIL },
  215         { WI_NIC_3842_MINI_ATS_ID,      WI_NIC_3842_MINI_STR,   WI_INTERSIL },
  216         { WI_NIC_3842_PCI_AMD_ID,       WI_NIC_3842_PCI_STR,    WI_INTERSIL },
  217         { WI_NIC_3842_PCI_SST_ID,       WI_NIC_3842_PCI_STR,    WI_INTERSIL },
  218         { WI_NIC_3842_PCI_ATS_ID,       WI_NIC_3842_PCI_STR,    WI_INTERSIL },
  219         { WI_NIC_3842_PCI_ATL_ID,       WI_NIC_3842_PCI_STR,    WI_INTERSIL },
  220         { WI_NIC_P3_PCMCIA_AMD_ID,      WI_NIC_P3_PCMCIA_STR,   WI_INTERSIL },
  221         { WI_NIC_P3_PCMCIA_SST_ID,      WI_NIC_P3_PCMCIA_STR,   WI_INTERSIL },
  222         { WI_NIC_P3_PCMCIA_ATL_ID,      WI_NIC_P3_PCMCIA_STR,   WI_INTERSIL },
  223         { WI_NIC_P3_PCMCIA_ATS_ID,      WI_NIC_P3_PCMCIA_STR,   WI_INTERSIL },
  224         { WI_NIC_P3_MINI_AMD_ID,        WI_NIC_P3_MINI_STR,     WI_INTERSIL },
  225         { WI_NIC_P3_MINI_SST_ID,        WI_NIC_P3_MINI_STR,     WI_INTERSIL },
  226         { WI_NIC_P3_MINI_ATL_ID,        WI_NIC_P3_MINI_STR,     WI_INTERSIL },
  227         { WI_NIC_P3_MINI_ATS_ID,        WI_NIC_P3_MINI_STR,     WI_INTERSIL },
  228         { 0,    NULL,   0 },
  229 };
  230 
  231 static char *wi_firmware_names[] = { "none", "Hermes", "Intersil", "Symbol" };
  232 
  233 devclass_t wi_devclass;
  234 
  235 int
  236 wi_attach(device_t dev)
  237 {
  238         struct wi_softc *sc = device_get_softc(dev);
  239         struct ieee80211com *ic;
  240         struct ifnet *ifp;
  241         int i, nrates, buflen;
  242         u_int16_t val;
  243         u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
  244         struct ieee80211_rateset *rs;
  245         struct sysctl_ctx_list *sctx;
  246         struct sysctl_oid *soid;
  247         static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
  248                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  249         };
  250         int error;
  251         uint8_t macaddr[IEEE80211_ADDR_LEN];
  252 
  253         ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
  254         if (ifp == NULL) {
  255                 device_printf(dev, "can not if_alloc\n");
  256                 wi_free(dev);
  257                 return ENOSPC;
  258         }
  259         ic = ifp->if_l2com;
  260 
  261         sc->sc_firmware_type = WI_NOTYPE;
  262         sc->wi_cmd_count = 500;
  263         /* Reset the NIC. */
  264         if (wi_reset(sc) != 0) {
  265                 wi_free(dev);
  266                 return ENXIO;           /* XXX */
  267         }
  268 
  269         /* Read NIC identification */
  270         wi_read_nicid(sc);
  271         switch (sc->sc_firmware_type) {
  272         case WI_LUCENT:
  273                 if (sc->sc_sta_firmware_ver < 60006)
  274                         goto reject;
  275                 break;
  276         case WI_INTERSIL:
  277                 if (sc->sc_sta_firmware_ver < 800)
  278                         goto reject;
  279                 break;
  280         default:
  281         reject:
  282                 device_printf(dev, "Sorry, this card is not supported "
  283                     "(type %d, firmware ver %d)\n",
  284                     sc->sc_firmware_type, sc->sc_sta_firmware_ver);
  285                 wi_free(dev);
  286                 return EOPNOTSUPP; 
  287         }
  288 
  289         /* Export info about the device via sysctl */
  290         sctx = device_get_sysctl_ctx(dev);
  291         soid = device_get_sysctl_tree(dev);
  292         SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
  293             "firmware_type", CTLFLAG_RD,
  294             wi_firmware_names[sc->sc_firmware_type], 0,
  295             "Firmware type string");
  296         SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "sta_version",
  297             CTLFLAG_RD, &sc->sc_sta_firmware_ver, 0,
  298             "Station Firmware version");
  299         if (sc->sc_firmware_type == WI_INTERSIL)
  300                 SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
  301                     "pri_version", CTLFLAG_RD, &sc->sc_pri_firmware_ver, 0,
  302                     "Primary Firmware version");
  303         SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "nic_id",
  304             CTLFLAG_RD, &sc->sc_nic_id, 0, "NIC id");
  305         SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "nic_name",
  306             CTLFLAG_RD, sc->sc_nic_name, 0, "NIC name");
  307 
  308         mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
  309             MTX_DEF | MTX_RECURSE);
  310         callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
  311 
  312         /*
  313          * Read the station address.
  314          * And do it twice. I've seen PRISM-based cards that return
  315          * an error when trying to read it the first time, which causes
  316          * the probe to fail.
  317          */
  318         buflen = IEEE80211_ADDR_LEN;
  319         error = wi_read_rid(sc, WI_RID_MAC_NODE, macaddr, &buflen);
  320         if (error != 0) {
  321                 buflen = IEEE80211_ADDR_LEN;
  322                 error = wi_read_rid(sc, WI_RID_MAC_NODE, macaddr, &buflen);
  323         }
  324         if (error || IEEE80211_ADDR_EQ(macaddr, empty_macaddr)) {
  325                 if (error != 0)
  326                         device_printf(dev, "mac read failed %d\n", error);
  327                 else {
  328                         device_printf(dev, "mac read failed (all zeros)\n");
  329                         error = ENXIO;
  330                 }
  331                 wi_free(dev);
  332                 return (error);
  333         }
  334 
  335         ifp->if_softc = sc;
  336         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
  337         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
  338         ifp->if_ioctl = wi_ioctl;
  339         ifp->if_start = wi_start;
  340         ifp->if_init = wi_init;
  341         ifp->if_get_counter = wi_get_counter;
  342         IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
  343         ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
  344         IFQ_SET_READY(&ifp->if_snd);
  345 
  346         ic->ic_ifp = ifp;
  347         ic->ic_phytype = IEEE80211_T_DS;
  348         ic->ic_opmode = IEEE80211_M_STA;
  349         ic->ic_caps = IEEE80211_C_STA
  350                     | IEEE80211_C_PMGT
  351                     | IEEE80211_C_MONITOR
  352                     ;
  353 
  354         /*
  355          * Query the card for available channels and setup the
  356          * channel table.  We assume these are all 11b channels.
  357          */
  358         buflen = sizeof(val);
  359         if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
  360                 val = htole16(0x1fff);  /* assume 1-11 */
  361         KASSERT(val != 0, ("wi_attach: no available channels listed!"));
  362 
  363         val <<= 1;                      /* shift for base 1 indices */
  364         for (i = 1; i < 16; i++) {
  365                 struct ieee80211_channel *c;
  366 
  367                 if (!isset((u_int8_t*)&val, i))
  368                         continue;
  369                 c = &ic->ic_channels[ic->ic_nchans++];
  370                 c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
  371                 c->ic_flags = IEEE80211_CHAN_B;
  372                 c->ic_ieee = i;
  373                 /* XXX txpowers? */
  374         }
  375 
  376         /*
  377          * Set flags based on firmware version.
  378          */
  379         switch (sc->sc_firmware_type) {
  380         case WI_LUCENT:
  381                 sc->sc_ntxbuf = 1;
  382                 ic->ic_caps |= IEEE80211_C_IBSS;
  383 
  384                 sc->sc_ibss_port = WI_PORTTYPE_BSS;
  385                 sc->sc_monitor_port = WI_PORTTYPE_ADHOC;
  386                 sc->sc_min_rssi = WI_LUCENT_MIN_RSSI;
  387                 sc->sc_max_rssi = WI_LUCENT_MAX_RSSI;
  388                 sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
  389                 break;
  390         case WI_INTERSIL:
  391                 sc->sc_ntxbuf = WI_NTXBUF;
  392                 sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR
  393                              |  WI_FLAGS_HAS_ROAMING;
  394                 /*
  395                  * Old firmware are slow, so give peace a chance.
  396                  */
  397                 if (sc->sc_sta_firmware_ver < 10000)
  398                         sc->wi_cmd_count = 5000;
  399                 if (sc->sc_sta_firmware_ver > 10101)
  400                         sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
  401                 ic->ic_caps |= IEEE80211_C_IBSS;
  402                 /*
  403                  * version 0.8.3 and newer are the only ones that are known
  404                  * to currently work.  Earlier versions can be made to work,
  405                  * at least according to the Linux driver but we require
  406                  * monitor mode so this is irrelevant.
  407                  */
  408                 ic->ic_caps |= IEEE80211_C_HOSTAP;
  409                 if (sc->sc_sta_firmware_ver >= 10603)
  410                         sc->sc_flags |= WI_FLAGS_HAS_ENHSECURITY;
  411                 if (sc->sc_sta_firmware_ver >= 10700) {
  412                         /*
  413                          * 1.7.0+ have the necessary support for sta mode WPA.
  414                          */
  415                         sc->sc_flags |= WI_FLAGS_HAS_WPASUPPORT;
  416                         ic->ic_caps |= IEEE80211_C_WPA;
  417                 }
  418 
  419                 sc->sc_ibss_port = WI_PORTTYPE_IBSS;
  420                 sc->sc_monitor_port = WI_PORTTYPE_APSILENT;
  421                 sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
  422                 sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
  423                 sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
  424                 break;
  425         }
  426 
  427         /*
  428          * Find out if we support WEP on this card.
  429          */
  430         buflen = sizeof(val);
  431         if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
  432             val != htole16(0))
  433                 ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
  434 
  435         /* Find supported rates. */
  436         buflen = sizeof(ratebuf);
  437         rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
  438         if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
  439                 nrates = le16toh(*(u_int16_t *)ratebuf);
  440                 if (nrates > IEEE80211_RATE_MAXSIZE)
  441                         nrates = IEEE80211_RATE_MAXSIZE;
  442                 rs->rs_nrates = 0;
  443                 for (i = 0; i < nrates; i++)
  444                         if (ratebuf[2+i])
  445                                 rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i];
  446         } else {
  447                 /* XXX fallback on error? */
  448         }
  449 
  450         buflen = sizeof(val);
  451         if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
  452             wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
  453                 sc->sc_dbm_offset = le16toh(val);
  454         }
  455 
  456         sc->sc_portnum = WI_DEFAULT_PORT;
  457 
  458         ieee80211_ifattach(ic, macaddr);
  459         ic->ic_raw_xmit = wi_raw_xmit;
  460         ic->ic_scan_start = wi_scan_start;
  461         ic->ic_scan_end = wi_scan_end;
  462         ic->ic_set_channel = wi_set_channel;
  463 
  464         ic->ic_vap_create = wi_vap_create;
  465         ic->ic_vap_delete = wi_vap_delete;
  466         ic->ic_update_mcast = wi_update_mcast;
  467         ic->ic_update_promisc = wi_update_promisc;
  468 
  469         ieee80211_radiotap_attach(ic,
  470             &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
  471                 WI_TX_RADIOTAP_PRESENT,
  472             &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
  473                 WI_RX_RADIOTAP_PRESENT);
  474 
  475         if (bootverbose)
  476                 ieee80211_announce(ic);
  477 
  478         error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
  479             NULL, wi_intr, sc, &sc->wi_intrhand);
  480         if (error) {
  481                 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
  482                 ieee80211_ifdetach(ic);
  483                 if_free(sc->sc_ifp);
  484                 wi_free(dev);
  485                 return error;
  486         }
  487 
  488         return (0);
  489 }
  490 
  491 int
  492 wi_detach(device_t dev)
  493 {
  494         struct wi_softc *sc = device_get_softc(dev);
  495         struct ifnet *ifp = sc->sc_ifp;
  496         struct ieee80211com *ic = ifp->if_l2com;
  497 
  498         WI_LOCK(sc);
  499 
  500         /* check if device was removed */
  501         sc->wi_gone |= !bus_child_present(dev);
  502 
  503         wi_stop_locked(sc, 0);
  504         WI_UNLOCK(sc);
  505         ieee80211_ifdetach(ic);
  506 
  507         bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
  508         if_free(sc->sc_ifp);
  509         wi_free(dev);
  510         mtx_destroy(&sc->sc_mtx);
  511         return (0);
  512 }
  513 
  514 static struct ieee80211vap *
  515 wi_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
  516     enum ieee80211_opmode opmode, int flags,
  517     const uint8_t bssid[IEEE80211_ADDR_LEN],
  518     const uint8_t mac[IEEE80211_ADDR_LEN])
  519 {
  520         struct wi_softc *sc = ic->ic_ifp->if_softc;
  521         struct wi_vap *wvp;
  522         struct ieee80211vap *vap;
  523 
  524         if (!TAILQ_EMPTY(&ic->ic_vaps))         /* only one at a time */
  525                 return NULL;
  526         wvp = (struct wi_vap *) malloc(sizeof(struct wi_vap),
  527             M_80211_VAP, M_NOWAIT | M_ZERO);
  528         if (wvp == NULL)
  529                 return NULL;
  530 
  531         vap = &wvp->wv_vap;
  532         ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
  533 
  534         vap->iv_max_aid = WI_MAX_AID;
  535 
  536         switch (opmode) {
  537         case IEEE80211_M_STA:
  538                 sc->sc_porttype = WI_PORTTYPE_BSS;
  539                 wvp->wv_newstate = vap->iv_newstate;
  540                 vap->iv_newstate = wi_newstate_sta;
  541                 /* need to filter mgt frames to avoid confusing state machine */
  542                 wvp->wv_recv_mgmt = vap->iv_recv_mgmt;
  543                 vap->iv_recv_mgmt = wi_recv_mgmt;
  544                 break;
  545         case IEEE80211_M_IBSS:
  546                 sc->sc_porttype = sc->sc_ibss_port;
  547                 wvp->wv_newstate = vap->iv_newstate;
  548                 vap->iv_newstate = wi_newstate_sta;
  549                 break;
  550         case IEEE80211_M_AHDEMO:
  551                 sc->sc_porttype = WI_PORTTYPE_ADHOC;
  552                 break;
  553         case IEEE80211_M_HOSTAP:
  554                 sc->sc_porttype = WI_PORTTYPE_HOSTAP;
  555                 wvp->wv_newstate = vap->iv_newstate;
  556                 vap->iv_newstate = wi_newstate_hostap;
  557                 break;
  558         case IEEE80211_M_MONITOR:
  559                 sc->sc_porttype = sc->sc_monitor_port;
  560                 break;
  561         default:
  562                 break;
  563         }
  564 
  565         /* complete setup */
  566         ieee80211_vap_attach(vap, ieee80211_media_change, wi_media_status);
  567         ic->ic_opmode = opmode;
  568         return vap;
  569 }
  570 
  571 static void
  572 wi_vap_delete(struct ieee80211vap *vap)
  573 {
  574         struct wi_vap *wvp = WI_VAP(vap);
  575 
  576         ieee80211_vap_detach(vap);
  577         free(wvp, M_80211_VAP);
  578 }
  579 
  580 int
  581 wi_shutdown(device_t dev)
  582 {
  583         struct wi_softc *sc = device_get_softc(dev);
  584 
  585         wi_stop(sc, 1);
  586         return (0);
  587 }
  588 
  589 void
  590 wi_intr(void *arg)
  591 {
  592         struct wi_softc *sc = arg;
  593         struct ifnet *ifp = sc->sc_ifp;
  594         u_int16_t status;
  595 
  596         WI_LOCK(sc);
  597 
  598         if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) {
  599                 CSR_WRITE_2(sc, WI_INT_EN, 0);
  600                 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
  601                 WI_UNLOCK(sc);
  602                 return;
  603         }
  604 
  605         /* Disable interrupts. */
  606         CSR_WRITE_2(sc, WI_INT_EN, 0);
  607 
  608         status = CSR_READ_2(sc, WI_EVENT_STAT);
  609         if (status & WI_EV_RX)
  610                 wi_rx_intr(sc);
  611         if (status & WI_EV_ALLOC)
  612                 wi_tx_intr(sc);
  613         if (status & WI_EV_TX_EXC)
  614                 wi_tx_ex_intr(sc);
  615         if (status & WI_EV_INFO)
  616                 wi_info_intr(sc);
  617         if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
  618             !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
  619                 wi_start_locked(ifp);
  620 
  621         /* Re-enable interrupts. */
  622         CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
  623 
  624         WI_UNLOCK(sc);
  625 
  626         return;
  627 }
  628 
  629 static void
  630 wi_enable(struct wi_softc *sc)
  631 {
  632         /* Enable interrupts */
  633         CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
  634 
  635         /* enable port */
  636         wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
  637         sc->sc_enabled = 1;
  638 }
  639 
  640 static int
  641 wi_setup_locked(struct wi_softc *sc, int porttype, int mode,
  642         uint8_t mac[IEEE80211_ADDR_LEN])
  643 {
  644         int i;
  645 
  646         wi_reset(sc);
  647 
  648         wi_write_val(sc, WI_RID_PORTTYPE, porttype);
  649         wi_write_val(sc, WI_RID_CREATE_IBSS, mode);
  650         wi_write_val(sc, WI_RID_MAX_DATALEN, 2304);
  651         /* XXX IEEE80211_BPF_NOACK wants 0 */
  652         wi_write_val(sc, WI_RID_ALT_RETRY_CNT, 2);
  653         if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
  654                 wi_write_val(sc, WI_RID_ROAMING_MODE, 3); /* NB: disabled */
  655 
  656         wi_write_rid(sc, WI_RID_MAC_NODE, mac, IEEE80211_ADDR_LEN);
  657 
  658         /* Allocate fids for the card */
  659         sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
  660         for (i = 0; i < sc->sc_ntxbuf; i++) {
  661                 int error = wi_alloc_fid(sc, sc->sc_buflen,
  662                     &sc->sc_txd[i].d_fid);
  663                 if (error) {
  664                         device_printf(sc->sc_dev,
  665                             "tx buffer allocation failed (error %u)\n",
  666                             error);
  667                         return error;
  668                 }
  669                 sc->sc_txd[i].d_len = 0;
  670         }
  671         sc->sc_txcur = sc->sc_txnext = 0;
  672 
  673         return 0;
  674 }
  675 
  676 static void
  677 wi_init_locked(struct wi_softc *sc)
  678 {
  679         struct ifnet *ifp = sc->sc_ifp;
  680         int wasenabled;
  681 
  682         WI_LOCK_ASSERT(sc);
  683 
  684         wasenabled = sc->sc_enabled;
  685         if (wasenabled)
  686                 wi_stop_locked(sc, 1);
  687 
  688         if (wi_setup_locked(sc, sc->sc_porttype, 3, IF_LLADDR(ifp)) != 0) {
  689                 if_printf(ifp, "interface not running\n");
  690                 wi_stop_locked(sc, 1);
  691                 return;
  692         }
  693 
  694         ifp->if_drv_flags |= IFF_DRV_RUNNING;
  695         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
  696 
  697         callout_reset(&sc->sc_watchdog, hz, wi_watchdog, sc);
  698 
  699         wi_enable(sc);                  /* Enable desired port */
  700 }
  701 
  702 void
  703 wi_init(void *arg)
  704 {
  705         struct wi_softc *sc = arg;
  706         struct ifnet *ifp = sc->sc_ifp;
  707         struct ieee80211com *ic = ifp->if_l2com;
  708 
  709         WI_LOCK(sc);
  710         wi_init_locked(sc);
  711         WI_UNLOCK(sc);
  712 
  713         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
  714                 ieee80211_start_all(ic);                /* start all vap's */
  715 }
  716 
  717 static void
  718 wi_stop_locked(struct wi_softc *sc, int disable)
  719 {
  720         struct ifnet *ifp = sc->sc_ifp;
  721 
  722         WI_LOCK_ASSERT(sc);
  723 
  724         if (sc->sc_enabled && !sc->wi_gone) {
  725                 CSR_WRITE_2(sc, WI_INT_EN, 0);
  726                 wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
  727                 if (disable)
  728                         sc->sc_enabled = 0;
  729         } else if (sc->wi_gone && disable)      /* gone --> not enabled */
  730                 sc->sc_enabled = 0;
  731 
  732         callout_stop(&sc->sc_watchdog);
  733         sc->sc_tx_timer = 0;
  734         sc->sc_false_syns = 0;
  735 
  736         ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE | IFF_DRV_RUNNING);
  737 }
  738 
  739 void
  740 wi_stop(struct wi_softc *sc, int disable)
  741 {
  742         WI_LOCK(sc);
  743         wi_stop_locked(sc, disable);
  744         WI_UNLOCK(sc);
  745 }
  746 
  747 static void
  748 wi_set_channel(struct ieee80211com *ic)
  749 {
  750         struct ifnet *ifp = ic->ic_ifp;
  751         struct wi_softc *sc = ifp->if_softc;
  752 
  753         DPRINTF(("%s: channel %d, %sscanning\n", __func__,
  754             ieee80211_chan2ieee(ic, ic->ic_curchan),
  755             ic->ic_flags & IEEE80211_F_SCAN ? "" : "!"));
  756 
  757         WI_LOCK(sc);
  758         wi_write_val(sc, WI_RID_OWN_CHNL,
  759             ieee80211_chan2ieee(ic, ic->ic_curchan));
  760         WI_UNLOCK(sc);
  761 }
  762 
  763 static void
  764 wi_scan_start(struct ieee80211com *ic)
  765 {
  766         struct ifnet *ifp = ic->ic_ifp;
  767         struct wi_softc *sc = ifp->if_softc;
  768         struct ieee80211_scan_state *ss = ic->ic_scan;
  769 
  770         DPRINTF(("%s\n", __func__));
  771 
  772         WI_LOCK(sc);
  773         /*
  774          * Switch device to monitor mode.
  775          */
  776         wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_monitor_port);
  777         if (sc->sc_firmware_type == WI_INTERSIL) {
  778                 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
  779                 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
  780         }
  781         /* force full dwell time to compensate for firmware overhead */
  782         ss->ss_mindwell = ss->ss_maxdwell = msecs_to_ticks(400);
  783         WI_UNLOCK(sc);
  784 
  785 }
  786 
  787 static void
  788 wi_scan_end(struct ieee80211com *ic)
  789 {
  790         struct ifnet *ifp = ic->ic_ifp;
  791         struct wi_softc *sc = ifp->if_softc;
  792 
  793         DPRINTF(("%s: restore port type %d\n", __func__, sc->sc_porttype));
  794 
  795         WI_LOCK(sc);
  796         wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_porttype);
  797         if (sc->sc_firmware_type == WI_INTERSIL) {
  798                 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
  799                 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
  800         }
  801         WI_UNLOCK(sc);
  802 }
  803 
  804 static void
  805 wi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
  806         int subtype, int rssi, int nf)
  807 {
  808         struct ieee80211vap *vap = ni->ni_vap;
  809 
  810         switch (subtype) {
  811         case IEEE80211_FC0_SUBTYPE_AUTH:
  812         case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
  813         case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
  814                 /* NB: filter frames that trigger state changes */
  815                 return;
  816         }
  817         WI_VAP(vap)->wv_recv_mgmt(ni, m, subtype, rssi, nf);
  818 }
  819 
  820 static int
  821 wi_newstate_sta(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
  822 {
  823         struct ieee80211com *ic = vap->iv_ic;
  824         struct ifnet *ifp = ic->ic_ifp;
  825         struct ieee80211_node *bss;
  826         struct wi_softc *sc = ifp->if_softc;
  827 
  828         DPRINTF(("%s: %s -> %s\n", __func__,
  829                 ieee80211_state_name[vap->iv_state],
  830                 ieee80211_state_name[nstate]));
  831 
  832         if (nstate == IEEE80211_S_AUTH) {
  833                 WI_LOCK(sc);
  834                 wi_setup_locked(sc, WI_PORTTYPE_BSS, 3, vap->iv_myaddr);
  835 
  836                 if (vap->iv_flags & IEEE80211_F_PMGTON) {
  837                         wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
  838                         wi_write_val(sc, WI_RID_PM_ENABLED, 1);
  839                 }
  840                 wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold);
  841                 if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
  842                         wi_write_val(sc, WI_RID_FRAG_THRESH,
  843                             vap->iv_fragthreshold);
  844                 wi_write_txrate(sc, vap);
  845 
  846                 bss = vap->iv_bss;
  847                 wi_write_ssid(sc, WI_RID_DESIRED_SSID, bss->ni_essid, bss->ni_esslen);
  848                 wi_write_val(sc, WI_RID_OWN_CHNL,
  849                     ieee80211_chan2ieee(ic, bss->ni_chan));
  850 
  851                 /* Configure WEP. */
  852                 if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP)
  853                         wi_write_wep(sc, vap);
  854                 else
  855                         sc->sc_encryption = 0;
  856 
  857                 if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) &&
  858                     (vap->iv_flags & IEEE80211_F_WPA)) {
  859                         wi_write_val(sc, WI_RID_WPA_HANDLING, 1);
  860                         if (vap->iv_appie_wpa != NULL)
  861                                 wi_write_appie(sc, WI_RID_WPA_DATA,
  862                                     vap->iv_appie_wpa);
  863                 }
  864 
  865                 wi_enable(sc);          /* enable port */
  866 
  867                 /* Lucent firmware does not support the JOIN RID. */
  868                 if (sc->sc_firmware_type == WI_INTERSIL) {
  869                         struct wi_joinreq join;
  870 
  871                         memset(&join, 0, sizeof(join));
  872                         IEEE80211_ADDR_COPY(&join.wi_bssid, bss->ni_bssid);
  873                         join.wi_chan = htole16(
  874                             ieee80211_chan2ieee(ic, bss->ni_chan));
  875                         wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
  876                 }
  877                 WI_UNLOCK(sc);
  878 
  879                 /*
  880                  * NB: don't go through 802.11 layer, it'll send auth frame;
  881                  * instead we drive the state machine from the link status
  882                  * notification we get on association.
  883                  */
  884                 vap->iv_state = nstate;
  885                 return (0);
  886         }
  887         return WI_VAP(vap)->wv_newstate(vap, nstate, arg);
  888 }
  889 
  890 static int
  891 wi_newstate_hostap(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
  892 {
  893         struct ieee80211com *ic = vap->iv_ic;
  894         struct ifnet *ifp = ic->ic_ifp;
  895         struct ieee80211_node *bss;
  896         struct wi_softc *sc = ifp->if_softc;
  897         int error;
  898 
  899         DPRINTF(("%s: %s -> %s\n", __func__,
  900                 ieee80211_state_name[vap->iv_state],
  901                 ieee80211_state_name[nstate]));
  902 
  903         error = WI_VAP(vap)->wv_newstate(vap, nstate, arg);
  904         if (error == 0 && nstate == IEEE80211_S_RUN) {
  905                 WI_LOCK(sc);
  906                 wi_setup_locked(sc, WI_PORTTYPE_HOSTAP, 0, vap->iv_myaddr);
  907 
  908                 bss = vap->iv_bss;
  909                 wi_write_ssid(sc, WI_RID_OWN_SSID,
  910                     bss->ni_essid, bss->ni_esslen);
  911                 wi_write_val(sc, WI_RID_OWN_CHNL,
  912                     ieee80211_chan2ieee(ic, bss->ni_chan));
  913                 wi_write_val(sc, WI_RID_BASIC_RATE, 0x3);
  914                 wi_write_val(sc, WI_RID_SUPPORT_RATE, 0xf);
  915                 wi_write_txrate(sc, vap);
  916 
  917                 wi_write_val(sc, WI_RID_OWN_BEACON_INT, bss->ni_intval);
  918                 wi_write_val(sc, WI_RID_DTIM_PERIOD, vap->iv_dtim_period);
  919 
  920                 wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold);
  921                 if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
  922                         wi_write_val(sc, WI_RID_FRAG_THRESH,
  923                             vap->iv_fragthreshold);
  924 
  925                 if ((sc->sc_flags & WI_FLAGS_HAS_ENHSECURITY) &&
  926                     (vap->iv_flags & IEEE80211_F_HIDESSID)) {
  927                         /*
  928                          * bit 0 means hide SSID in beacons,
  929                          * bit 1 means don't respond to bcast probe req
  930                          */
  931                         wi_write_val(sc, WI_RID_ENH_SECURITY, 0x3);
  932                 }
  933 
  934                 if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) &&
  935                     (vap->iv_flags & IEEE80211_F_WPA) && 
  936                     vap->iv_appie_wpa != NULL)
  937                         wi_write_appie(sc, WI_RID_WPA_DATA, vap->iv_appie_wpa);
  938 
  939                 wi_write_val(sc, WI_RID_PROMISC, 0);
  940 
  941                 /* Configure WEP. */
  942                 if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP)
  943                         wi_write_wep(sc, vap);
  944                 else
  945                         sc->sc_encryption = 0;
  946 
  947                 wi_enable(sc);          /* enable port */
  948                 WI_UNLOCK(sc);
  949         }
  950         return error;
  951 }
  952 
  953 static void
  954 wi_start_locked(struct ifnet *ifp)
  955 {
  956         struct wi_softc *sc = ifp->if_softc;
  957         struct ieee80211_node *ni;
  958         struct ieee80211_frame *wh;
  959         struct mbuf *m0;
  960         struct ieee80211_key *k;
  961         struct wi_frame frmhdr;
  962         const struct llc *llc;
  963         int cur;
  964 
  965         WI_LOCK_ASSERT(sc);
  966 
  967         if (sc->wi_gone)
  968                 return;
  969 
  970         memset(&frmhdr, 0, sizeof(frmhdr));
  971         cur = sc->sc_txnext;
  972         for (;;) {
  973                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
  974                 if (m0 == NULL)
  975                         break;
  976                 if (sc->sc_txd[cur].d_len != 0) {
  977                         IFQ_DRV_PREPEND(&ifp->if_snd, m0);
  978                         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
  979                         break;
  980                 }
  981                 ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
  982 
  983                 /* reconstruct 802.3 header */
  984                 wh = mtod(m0, struct ieee80211_frame *);
  985                 switch (wh->i_fc[1]) {
  986                 case IEEE80211_FC1_DIR_TODS:
  987                         IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost,
  988                             wh->i_addr2);
  989                         IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost,
  990                             wh->i_addr3);
  991                         break;
  992                 case IEEE80211_FC1_DIR_NODS:
  993                         IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost,
  994                             wh->i_addr2);
  995                         IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost,
  996                             wh->i_addr1);
  997                         break;
  998                 case IEEE80211_FC1_DIR_FROMDS:
  999                         IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost,
 1000                             wh->i_addr3);
 1001                         IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost,
 1002                             wh->i_addr1);
 1003                         break;
 1004                 }
 1005                 llc = (const struct llc *)(
 1006                     mtod(m0, const uint8_t *) + ieee80211_hdrsize(wh));
 1007                 frmhdr.wi_ehdr.ether_type = llc->llc_snap.ether_type;
 1008                 frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
 1009                 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
 1010                         k = ieee80211_crypto_encap(ni, m0);
 1011                         if (k == NULL) {
 1012                                 ieee80211_free_node(ni);
 1013                                 m_freem(m0);
 1014                                 continue;
 1015                         }
 1016                         frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
 1017                 }
 1018 
 1019                 if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
 1020                         sc->sc_tx_th.wt_rate = ni->ni_txrate;
 1021                         ieee80211_radiotap_tx(ni->ni_vap, m0);
 1022                 }
 1023 
 1024                 m_copydata(m0, 0, sizeof(struct ieee80211_frame),
 1025                     (caddr_t)&frmhdr.wi_whdr);
 1026                 m_adj(m0, sizeof(struct ieee80211_frame));
 1027                 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
 1028                 ieee80211_free_node(ni);
 1029                 if (wi_start_tx(ifp, &frmhdr, m0))
 1030                         continue;
 1031 
 1032                 sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
 1033                 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
 1034         }
 1035 }
 1036 
 1037 static void
 1038 wi_start(struct ifnet *ifp)
 1039 {
 1040         struct wi_softc *sc = ifp->if_softc;
 1041 
 1042         WI_LOCK(sc);
 1043         wi_start_locked(ifp);
 1044         WI_UNLOCK(sc);
 1045 }
 1046 
 1047 static int
 1048 wi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr, struct mbuf *m0)
 1049 {
 1050         struct wi_softc *sc = ifp->if_softc;
 1051         int cur = sc->sc_txnext;
 1052         int fid, off, error;
 1053 
 1054         fid = sc->sc_txd[cur].d_fid;
 1055         off = sizeof(*frmhdr);
 1056         error = wi_write_bap(sc, fid, 0, frmhdr, sizeof(*frmhdr)) != 0
 1057              || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
 1058         m_freem(m0);
 1059         if (error) {
 1060                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
 1061                 return -1;
 1062         }
 1063         sc->sc_txd[cur].d_len = off;
 1064         if (sc->sc_txcur == cur) {
 1065                 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
 1066                         if_printf(ifp, "xmit failed\n");
 1067                         sc->sc_txd[cur].d_len = 0;
 1068                         return -1;
 1069                 }
 1070                 sc->sc_tx_timer = 5;
 1071         }
 1072         return 0;
 1073 }
 1074 
 1075 static int
 1076 wi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m0,
 1077             const struct ieee80211_bpf_params *params)
 1078 {
 1079         struct ieee80211com *ic = ni->ni_ic;
 1080         struct ifnet *ifp = ic->ic_ifp;
 1081         struct ieee80211vap *vap = ni->ni_vap;
 1082         struct wi_softc *sc = ifp->if_softc;
 1083         struct ieee80211_key *k;
 1084         struct ieee80211_frame *wh;
 1085         struct wi_frame frmhdr;
 1086         int cur;
 1087         int rc = 0;
 1088 
 1089         WI_LOCK(sc);
 1090 
 1091         if (sc->wi_gone) {
 1092                 rc = ENETDOWN;
 1093                 goto out;
 1094         }
 1095         memset(&frmhdr, 0, sizeof(frmhdr));
 1096         cur = sc->sc_txnext;
 1097         if (sc->sc_txd[cur].d_len != 0) {
 1098                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
 1099                 rc = ENOBUFS;
 1100                 goto out;
 1101         }
 1102         m0->m_pkthdr.rcvif = NULL;
 1103 
 1104         m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
 1105             (caddr_t)&frmhdr.wi_ehdr);
 1106         frmhdr.wi_ehdr.ether_type = 0;
 1107         wh = mtod(m0, struct ieee80211_frame *);
 1108                         
 1109         frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
 1110         if (params && (params->ibp_flags & IEEE80211_BPF_NOACK))
 1111                 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_ALTRTRY);
 1112         if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
 1113             (!params || (params && (params->ibp_flags & IEEE80211_BPF_CRYPTO)))) {
 1114                 k = ieee80211_crypto_encap(ni, m0);
 1115                 if (k == NULL) {
 1116                         rc = ENOMEM;
 1117                         goto out;
 1118                 }
 1119                 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
 1120         }
 1121         if (ieee80211_radiotap_active_vap(vap)) {
 1122                 sc->sc_tx_th.wt_rate = ni->ni_txrate;
 1123                 ieee80211_radiotap_tx(vap, m0);
 1124         }
 1125         m_copydata(m0, 0, sizeof(struct ieee80211_frame),
 1126             (caddr_t)&frmhdr.wi_whdr);
 1127         m_adj(m0, sizeof(struct ieee80211_frame));
 1128         frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
 1129         if (wi_start_tx(ifp, &frmhdr, m0) < 0) {
 1130                 m0 = NULL;
 1131                 rc = EIO;
 1132                 goto out;
 1133         }
 1134         m0 = NULL;
 1135 
 1136         sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
 1137 out:
 1138         WI_UNLOCK(sc);
 1139 
 1140         if (m0 != NULL)
 1141                 m_freem(m0);
 1142         ieee80211_free_node(ni);
 1143         return rc;
 1144 }
 1145 
 1146 static int
 1147 wi_reset(struct wi_softc *sc)
 1148 {
 1149 #define WI_INIT_TRIES 3
 1150         int i, error = 0;
 1151 
 1152         for (i = 0; i < WI_INIT_TRIES; i++) {
 1153                 error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0);
 1154                 if (error == 0)
 1155                         break;
 1156                 DELAY(WI_DELAY * 1000);
 1157         }
 1158         sc->sc_reset = 1;
 1159         if (i == WI_INIT_TRIES) {
 1160                 if_printf(sc->sc_ifp, "reset failed\n");
 1161                 return error;
 1162         }
 1163 
 1164         CSR_WRITE_2(sc, WI_INT_EN, 0);
 1165         CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
 1166 
 1167         /* Calibrate timer. */
 1168         wi_write_val(sc, WI_RID_TICK_TIME, 8);
 1169 
 1170         return 0;
 1171 #undef WI_INIT_TRIES
 1172 }
 1173 
 1174 static void
 1175 wi_watchdog(void *arg)
 1176 {
 1177         struct wi_softc *sc = arg;
 1178         struct ifnet *ifp = sc->sc_ifp;
 1179 
 1180         WI_LOCK_ASSERT(sc);
 1181 
 1182         if (!sc->sc_enabled)
 1183                 return;
 1184 
 1185         if (sc->sc_tx_timer && --sc->sc_tx_timer == 0) {
 1186                 if_printf(ifp, "device timeout\n");
 1187                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
 1188                 wi_init_locked(ifp->if_softc);
 1189                 return;
 1190         }
 1191         callout_reset(&sc->sc_watchdog, hz, wi_watchdog, sc);
 1192 }
 1193 
 1194 static int
 1195 wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
 1196 {
 1197         struct wi_softc *sc = ifp->if_softc;
 1198         struct ieee80211com *ic = ifp->if_l2com;
 1199         struct ifreq *ifr = (struct ifreq *) data;
 1200         int error = 0, startall = 0;
 1201 
 1202         switch (cmd) {
 1203         case SIOCSIFFLAGS:
 1204                 WI_LOCK(sc);
 1205                 /*
 1206                  * Can't do promisc and hostap at the same time.  If all that's
 1207                  * changing is the promisc flag, try to short-circuit a call to
 1208                  * wi_init() by just setting PROMISC in the hardware.
 1209                  */
 1210                 if (ifp->if_flags & IFF_UP) {
 1211                         if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
 1212                             ifp->if_drv_flags & IFF_DRV_RUNNING) {
 1213                                 if ((ifp->if_flags ^ sc->sc_if_flags) & IFF_PROMISC) {
 1214                                         wi_write_val(sc, WI_RID_PROMISC,
 1215                                             (ifp->if_flags & IFF_PROMISC) != 0);
 1216                                 } else {
 1217                                         wi_init_locked(sc);
 1218                                         startall = 1;
 1219                                 }
 1220                         } else {
 1221                                 wi_init_locked(sc);
 1222                                 startall = 1;
 1223                         }
 1224                 } else {
 1225                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
 1226                                 wi_stop_locked(sc, 1);
 1227                         sc->wi_gone = 0;
 1228                 }
 1229                 sc->sc_if_flags = ifp->if_flags;
 1230                 WI_UNLOCK(sc);
 1231                 if (startall)
 1232                         ieee80211_start_all(ic);
 1233                 break;
 1234         case SIOCGIFMEDIA:
 1235                 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
 1236                 break;
 1237         case SIOCGIFADDR:
 1238                 error = ether_ioctl(ifp, cmd, data);
 1239                 break;
 1240         default:
 1241                 error = EINVAL;
 1242                 break;
 1243         }
 1244         return error;
 1245 }
 1246 
 1247 static void
 1248 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
 1249 {
 1250         struct ieee80211vap *vap = ifp->if_softc;
 1251         struct ieee80211com *ic = vap->iv_ic;
 1252         struct wi_softc *sc = ic->ic_ifp->if_softc;
 1253         u_int16_t val;
 1254         int rate, len;
 1255 
 1256         len = sizeof(val);
 1257         if (sc->sc_enabled &&
 1258             wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) == 0 &&
 1259             len == sizeof(val)) {
 1260                 /* convert to 802.11 rate */
 1261                 val = le16toh(val);
 1262                 rate = val * 2;
 1263                 if (sc->sc_firmware_type == WI_LUCENT) {
 1264                         if (rate == 10)
 1265                                 rate = 11;      /* 5.5Mbps */
 1266                 } else {
 1267                         if (rate == 4*2)
 1268                                 rate = 11;      /* 5.5Mbps */
 1269                         else if (rate == 8*2)
 1270                                 rate = 22;      /* 11Mbps */
 1271                 }
 1272                 vap->iv_bss->ni_txrate = rate;
 1273         }
 1274         ieee80211_media_status(ifp, imr);
 1275 }
 1276 
 1277 static void
 1278 wi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
 1279 {
 1280         struct ifnet *ifp = sc->sc_ifp;
 1281         struct ieee80211com *ic = ifp->if_l2com;
 1282         struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
 1283         struct ieee80211_node *ni = vap->iv_bss;
 1284 
 1285         if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
 1286                 return;
 1287 
 1288         DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid)));
 1289         DPRINTF(("%s ?\n", ether_sprintf(new_bssid)));
 1290 
 1291         /* In promiscuous mode, the BSSID field is not a reliable
 1292          * indicator of the firmware's BSSID. Damp spurious
 1293          * change-of-BSSID indications.
 1294          */
 1295         if ((ifp->if_flags & IFF_PROMISC) != 0 &&
 1296             !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns,
 1297                          WI_MAX_FALSE_SYNS))
 1298                 return;
 1299 
 1300         sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1);
 1301 #if 0
 1302         /*
 1303          * XXX hack; we should create a new node with the new bssid
 1304          * and replace the existing ic_bss with it but since we don't
 1305          * process management frames to collect state we cheat by
 1306          * reusing the existing node as we know wi_newstate will be
 1307          * called and it will overwrite the node state.
 1308          */
 1309         ieee80211_sta_join(ic, ieee80211_ref_node(ni));
 1310 #endif
 1311 }
 1312 
 1313 static __noinline void
 1314 wi_rx_intr(struct wi_softc *sc)
 1315 {
 1316         struct ifnet *ifp = sc->sc_ifp;
 1317         struct ieee80211com *ic = ifp->if_l2com;
 1318         struct wi_frame frmhdr;
 1319         struct mbuf *m;
 1320         struct ieee80211_frame *wh;
 1321         struct ieee80211_node *ni;
 1322         int fid, len, off;
 1323         u_int8_t dir;
 1324         u_int16_t status;
 1325         int8_t rssi, nf;
 1326 
 1327         fid = CSR_READ_2(sc, WI_RX_FID);
 1328 
 1329         /* First read in the frame header */
 1330         if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
 1331                 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
 1332                 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
 1333                 DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
 1334                 return;
 1335         }
 1336 
 1337         /*
 1338          * Drop undecryptable or packets with receive errors here
 1339          */
 1340         status = le16toh(frmhdr.wi_status);
 1341         if (status & WI_STAT_ERRSTAT) {
 1342                 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
 1343                 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
 1344                 DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
 1345                 return;
 1346         }
 1347 
 1348         len = le16toh(frmhdr.wi_dat_len);
 1349         off = ALIGN(sizeof(struct ieee80211_frame));
 1350 
 1351         /*
 1352          * Sometimes the PRISM2.x returns bogusly large frames. Except
 1353          * in monitor mode, just throw them away.
 1354          */
 1355         if (off + len > MCLBYTES) {
 1356                 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
 1357                         CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
 1358                         if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
 1359                         DPRINTF(("wi_rx_intr: oversized packet\n"));
 1360                         return;
 1361                 } else
 1362                         len = 0;
 1363         }
 1364 
 1365         if (off + len > MHLEN)
 1366                 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
 1367         else
 1368                 m = m_gethdr(M_NOWAIT, MT_DATA);
 1369         if (m == NULL) {
 1370                 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
 1371                 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
 1372                 DPRINTF(("wi_rx_intr: MGET failed\n"));
 1373                 return;
 1374         }
 1375         m->m_data += off - sizeof(struct ieee80211_frame);
 1376         memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
 1377         wi_read_bap(sc, fid, sizeof(frmhdr),
 1378             m->m_data + sizeof(struct ieee80211_frame), len);
 1379         m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
 1380         m->m_pkthdr.rcvif = ifp;
 1381 
 1382         CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
 1383 
 1384         rssi = frmhdr.wi_rx_signal;
 1385         nf = frmhdr.wi_rx_silence;
 1386         if (ieee80211_radiotap_active(ic)) {
 1387                 struct wi_rx_radiotap_header *tap = &sc->sc_rx_th;
 1388                 uint32_t rstamp;
 1389 
 1390                 rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
 1391                     le16toh(frmhdr.wi_rx_tstamp1);
 1392                 tap->wr_tsf = htole64((uint64_t)rstamp);
 1393                 /* XXX replace divide by table */
 1394                 tap->wr_rate = frmhdr.wi_rx_rate / 5;
 1395                 tap->wr_flags = 0;
 1396                 if (frmhdr.wi_status & WI_STAT_PCF)
 1397                         tap->wr_flags |= IEEE80211_RADIOTAP_F_CFP;
 1398                 if (m->m_flags & M_WEP)
 1399                         tap->wr_flags |= IEEE80211_RADIOTAP_F_WEP;
 1400                 tap->wr_antsignal = rssi;
 1401                 tap->wr_antnoise = nf;
 1402         }
 1403 
 1404         /* synchronize driver's BSSID with firmware's BSSID */
 1405         wh = mtod(m, struct ieee80211_frame *);
 1406         dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
 1407         if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
 1408                 wi_sync_bssid(sc, wh->i_addr3);
 1409 
 1410         WI_UNLOCK(sc);
 1411 
 1412         ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
 1413         if (ni != NULL) {
 1414                 (void) ieee80211_input(ni, m, rssi, nf);
 1415                 ieee80211_free_node(ni);
 1416         } else
 1417                 (void) ieee80211_input_all(ic, m, rssi, nf);
 1418 
 1419         WI_LOCK(sc);
 1420 }
 1421 
 1422 static __noinline void
 1423 wi_tx_ex_intr(struct wi_softc *sc)
 1424 {
 1425         struct ifnet *ifp = sc->sc_ifp;
 1426         struct wi_frame frmhdr;
 1427         int fid;
 1428 
 1429         fid = CSR_READ_2(sc, WI_TX_CMP_FID);
 1430         /* Read in the frame header */
 1431         if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
 1432                 u_int16_t status = le16toh(frmhdr.wi_status);
 1433                 /*
 1434                  * Spontaneous station disconnects appear as xmit
 1435                  * errors.  Don't announce them and/or count them
 1436                  * as an output error.
 1437                  */
 1438                 if ((status & WI_TXSTAT_DISCONNECT) == 0) {
 1439                         if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
 1440                                 if_printf(ifp, "tx failed");
 1441                                 if (status & WI_TXSTAT_RET_ERR)
 1442                                         printf(", retry limit exceeded");
 1443                                 if (status & WI_TXSTAT_AGED_ERR)
 1444                                         printf(", max transmit lifetime exceeded");
 1445                                 if (status & WI_TXSTAT_DISCONNECT)
 1446                                         printf(", port disconnected");
 1447                                 if (status & WI_TXSTAT_FORM_ERR)
 1448                                         printf(", invalid format (data len %u src %6D)",
 1449                                                 le16toh(frmhdr.wi_dat_len),
 1450                                                 frmhdr.wi_ehdr.ether_shost, ":");
 1451                                 if (status & ~0xf)
 1452                                         printf(", status=0x%x", status);
 1453                                 printf("\n");
 1454                         }
 1455                         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
 1456                 } else
 1457                         DPRINTF(("port disconnected\n"));
 1458         } else
 1459                 DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
 1460         CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
 1461 }
 1462 
 1463 static __noinline void
 1464 wi_tx_intr(struct wi_softc *sc)
 1465 {
 1466         struct ifnet *ifp = sc->sc_ifp;
 1467         int fid, cur;
 1468 
 1469         if (sc->wi_gone)
 1470                 return;
 1471 
 1472         fid = CSR_READ_2(sc, WI_ALLOC_FID);
 1473         CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
 1474 
 1475         cur = sc->sc_txcur;
 1476         if (sc->sc_txd[cur].d_fid != fid) {
 1477                 if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
 1478                     fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
 1479                 return;
 1480         }
 1481         sc->sc_tx_timer = 0;
 1482         sc->sc_txd[cur].d_len = 0;
 1483         sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
 1484         if (sc->sc_txd[cur].d_len == 0)
 1485                 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
 1486         else {
 1487                 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
 1488                     0, 0)) {
 1489                         if_printf(ifp, "xmit failed\n");
 1490                         sc->sc_txd[cur].d_len = 0;
 1491                 } else {
 1492                         sc->sc_tx_timer = 5;
 1493                 }
 1494         }
 1495 }
 1496 
 1497 static __noinline void
 1498 wi_info_intr(struct wi_softc *sc)
 1499 {
 1500         struct ieee80211com *ic = sc->sc_ifp->if_l2com;
 1501         struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
 1502         int i, fid, len, off;
 1503         u_int16_t ltbuf[2];
 1504         u_int16_t stat;
 1505         u_int32_t *ptr;
 1506 
 1507         fid = CSR_READ_2(sc, WI_INFO_FID);
 1508         wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
 1509 
 1510         switch (le16toh(ltbuf[1])) {
 1511         case WI_INFO_LINK_STAT:
 1512                 wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
 1513                 DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
 1514 
 1515                 if (vap == NULL)
 1516                         goto finish;
 1517 
 1518                 switch (le16toh(stat)) {
 1519                 case WI_INFO_LINK_STAT_CONNECTED:
 1520                         if (vap->iv_state == IEEE80211_S_RUN &&
 1521                             vap->iv_opmode != IEEE80211_M_IBSS)
 1522                                 break;
 1523                         /* fall thru... */
 1524                 case WI_INFO_LINK_STAT_AP_CHG:
 1525                         IEEE80211_LOCK(ic);
 1526                         vap->iv_bss->ni_associd = 1 | 0xc000;   /* NB: anything will do */
 1527                         ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
 1528                         IEEE80211_UNLOCK(ic);
 1529                         break;
 1530                 case WI_INFO_LINK_STAT_AP_INR:
 1531                         break;
 1532                 case WI_INFO_LINK_STAT_DISCONNECTED:
 1533                         /* we dropped off the net; e.g. due to deauth/disassoc */
 1534                         IEEE80211_LOCK(ic);
 1535                         vap->iv_bss->ni_associd = 0;
 1536                         vap->iv_stats.is_rx_deauth++;
 1537                         ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
 1538                         IEEE80211_UNLOCK(ic);
 1539                         break;
 1540                 case WI_INFO_LINK_STAT_AP_OOR:
 1541                         /* XXX does this need to be per-vap? */
 1542                         ieee80211_beacon_miss(ic);
 1543                         break;
 1544                 case WI_INFO_LINK_STAT_ASSOC_FAILED:
 1545                         if (vap->iv_opmode == IEEE80211_M_STA)
 1546                                 ieee80211_new_state(vap, IEEE80211_S_SCAN,
 1547                                     IEEE80211_SCAN_FAIL_TIMEOUT);
 1548                         break;
 1549                 }
 1550                 break;
 1551         case WI_INFO_COUNTERS:
 1552                 /* some card versions have a larger stats structure */
 1553                 len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
 1554                 ptr = (u_int32_t *)&sc->sc_stats;
 1555                 off = sizeof(ltbuf);
 1556                 for (i = 0; i < len; i++, off += 2, ptr++) {
 1557                         wi_read_bap(sc, fid, off, &stat, sizeof(stat));
 1558 #ifdef WI_HERMES_STATS_WAR
 1559                         if (stat & 0xf000)
 1560                                 stat = ~stat;
 1561 #endif
 1562                         *ptr += stat;
 1563                 }
 1564                 break;
 1565         default:
 1566                 DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
 1567                     le16toh(ltbuf[1]), le16toh(ltbuf[0])));
 1568                 break;
 1569         }
 1570 finish:
 1571         CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
 1572 }
 1573 
 1574 static uint64_t
 1575 wi_get_counter(struct ifnet *ifp, ift_counter cnt)
 1576 {
 1577         struct wi_softc *sc;
 1578 
 1579         sc = if_getsoftc(ifp);
 1580 
 1581         switch (cnt) {
 1582         case IFCOUNTER_COLLISIONS:
 1583                 return (sc->sc_stats.wi_tx_single_retries +
 1584                     sc->sc_stats.wi_tx_multi_retries +
 1585                     sc->sc_stats.wi_tx_retry_limit);
 1586         default:
 1587                 return (if_get_counter_default(ifp, cnt));
 1588         }
 1589 }
 1590 
 1591 static int
 1592 wi_write_multi(struct wi_softc *sc)
 1593 {
 1594         struct ifnet *ifp = sc->sc_ifp;
 1595         int n;
 1596         struct ifmultiaddr *ifma;
 1597         struct wi_mcast mlist;
 1598 
 1599         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
 1600 allmulti:
 1601                 memset(&mlist, 0, sizeof(mlist));
 1602                 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
 1603                     sizeof(mlist));
 1604         }
 1605 
 1606         n = 0;
 1607         if_maddr_rlock(ifp);
 1608         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 1609                 if (ifma->ifma_addr->sa_family != AF_LINK)
 1610                         continue;
 1611                 if (n >= 16)
 1612                         goto allmulti;
 1613                 IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
 1614                     (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
 1615                 n++;
 1616         }
 1617         if_maddr_runlock(ifp);
 1618         return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
 1619             IEEE80211_ADDR_LEN * n);
 1620 }
 1621 
 1622 static void
 1623 wi_update_mcast(struct ifnet *ifp)
 1624 {
 1625         wi_write_multi(ifp->if_softc);
 1626 }
 1627 
 1628 static void
 1629 wi_update_promisc(struct ifnet *ifp)
 1630 {
 1631         struct wi_softc *sc = ifp->if_softc;
 1632         struct ieee80211com *ic = ifp->if_l2com;
 1633 
 1634         WI_LOCK(sc);
 1635         /* XXX handle WEP special case handling? */
 1636         wi_write_val(sc, WI_RID_PROMISC, 
 1637             (ic->ic_opmode == IEEE80211_M_MONITOR ||
 1638              (ifp->if_flags & IFF_PROMISC)));
 1639         WI_UNLOCK(sc);
 1640 }
 1641 
 1642 static void
 1643 wi_read_nicid(struct wi_softc *sc)
 1644 {
 1645         struct wi_card_ident *id;
 1646         char *p;
 1647         int len;
 1648         u_int16_t ver[4];
 1649 
 1650         /* getting chip identity */
 1651         memset(ver, 0, sizeof(ver));
 1652         len = sizeof(ver);
 1653         wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
 1654 
 1655         sc->sc_firmware_type = WI_NOTYPE;
 1656         sc->sc_nic_id = le16toh(ver[0]);
 1657         for (id = wi_card_ident; id->card_name != NULL; id++) {
 1658                 if (sc->sc_nic_id == id->card_id) {
 1659                         sc->sc_nic_name = id->card_name;
 1660                         sc->sc_firmware_type = id->firm_type;
 1661                         break;
 1662                 }
 1663         }
 1664         if (sc->sc_firmware_type == WI_NOTYPE) {
 1665                 if (sc->sc_nic_id & 0x8000) {
 1666                         sc->sc_firmware_type = WI_INTERSIL;
 1667                         sc->sc_nic_name = "Unknown Prism chip";
 1668                 } else {
 1669                         sc->sc_firmware_type = WI_LUCENT;
 1670                         sc->sc_nic_name = "Unknown Lucent chip";
 1671                 }
 1672         }
 1673         if (bootverbose)
 1674                 device_printf(sc->sc_dev, "using %s\n", sc->sc_nic_name);
 1675 
 1676         /* get primary firmware version (Only Prism chips) */
 1677         if (sc->sc_firmware_type != WI_LUCENT) {
 1678                 memset(ver, 0, sizeof(ver));
 1679                 len = sizeof(ver);
 1680                 wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
 1681                 sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
 1682                     le16toh(ver[3]) * 100 + le16toh(ver[1]);
 1683         }
 1684 
 1685         /* get station firmware version */
 1686         memset(ver, 0, sizeof(ver));
 1687         len = sizeof(ver);
 1688         wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
 1689         sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
 1690             le16toh(ver[3]) * 100 + le16toh(ver[1]);
 1691         if (sc->sc_firmware_type == WI_INTERSIL &&
 1692             (sc->sc_sta_firmware_ver == 10102 ||
 1693              sc->sc_sta_firmware_ver == 20102)) {
 1694                 char ident[12];
 1695                 memset(ident, 0, sizeof(ident));
 1696                 len = sizeof(ident);
 1697                 /* value should be the format like "V2.00-11" */
 1698                 if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
 1699                     *(p = (char *)ident) >= 'A' &&
 1700                     p[2] == '.' && p[5] == '-' && p[8] == '\0') {
 1701                         sc->sc_firmware_type = WI_SYMBOL;
 1702                         sc->sc_sta_firmware_ver = (p[1] - '') * 10000 +
 1703                             (p[3] - '') * 1000 + (p[4] - '') * 100 +
 1704                             (p[6] - '') * 10 + (p[7] - '');
 1705                 }
 1706         }
 1707         if (bootverbose) {
 1708                 device_printf(sc->sc_dev, "%s Firmware: ",
 1709                     wi_firmware_names[sc->sc_firmware_type]);
 1710                 if (sc->sc_firmware_type != WI_LUCENT)  /* XXX */
 1711                         printf("Primary (%u.%u.%u), ",
 1712                             sc->sc_pri_firmware_ver / 10000,
 1713                             (sc->sc_pri_firmware_ver % 10000) / 100,
 1714                             sc->sc_pri_firmware_ver % 100);
 1715                 printf("Station (%u.%u.%u)\n",
 1716                     sc->sc_sta_firmware_ver / 10000,
 1717                     (sc->sc_sta_firmware_ver % 10000) / 100,
 1718                     sc->sc_sta_firmware_ver % 100);
 1719         }
 1720 }
 1721 
 1722 static int
 1723 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
 1724 {
 1725         struct wi_ssid ssid;
 1726 
 1727         if (buflen > IEEE80211_NWID_LEN)
 1728                 return ENOBUFS;
 1729         memset(&ssid, 0, sizeof(ssid));
 1730         ssid.wi_len = htole16(buflen);
 1731         memcpy(ssid.wi_ssid, buf, buflen);
 1732         return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
 1733 }
 1734 
 1735 static int
 1736 wi_write_txrate(struct wi_softc *sc, struct ieee80211vap *vap)
 1737 {
 1738         static const uint16_t lucent_rates[12] = {
 1739             [ 0] = 3,   /* auto */
 1740             [ 1] = 1,   /* 1Mb/s */
 1741             [ 2] = 2,   /* 2Mb/s */
 1742             [ 5] = 4,   /* 5.5Mb/s */
 1743             [11] = 5    /* 11Mb/s */
 1744         };
 1745         static const uint16_t intersil_rates[12] = {
 1746             [ 0] = 0xf, /* auto */
 1747             [ 1] = 0,   /* 1Mb/s */
 1748             [ 2] = 1,   /* 2Mb/s */
 1749             [ 5] = 2,   /* 5.5Mb/s */
 1750             [11] = 3,   /* 11Mb/s */
 1751         };
 1752         const uint16_t *rates = sc->sc_firmware_type == WI_LUCENT ?
 1753             lucent_rates : intersil_rates;
 1754         struct ieee80211com *ic = vap->iv_ic;
 1755         const struct ieee80211_txparam *tp;
 1756 
 1757         tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
 1758         return wi_write_val(sc, WI_RID_TX_RATE,
 1759             (tp->ucastrate == IEEE80211_FIXED_RATE_NONE ?
 1760                 rates[0] : rates[tp->ucastrate / 2]));
 1761 }
 1762 
 1763 static int
 1764 wi_write_wep(struct wi_softc *sc, struct ieee80211vap *vap)
 1765 {
 1766         int error = 0;
 1767         int i, keylen;
 1768         u_int16_t val;
 1769         struct wi_key wkey[IEEE80211_WEP_NKID];
 1770 
 1771         switch (sc->sc_firmware_type) {
 1772         case WI_LUCENT:
 1773                 val = (vap->iv_flags & IEEE80211_F_PRIVACY) ? 1 : 0;
 1774                 error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
 1775                 if (error)
 1776                         break;
 1777                 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0)
 1778                         break;
 1779                 error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, vap->iv_def_txkey);
 1780                 if (error)
 1781                         break;
 1782                 memset(wkey, 0, sizeof(wkey));
 1783                 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
 1784                         keylen = vap->iv_nw_keys[i].wk_keylen;
 1785                         wkey[i].wi_keylen = htole16(keylen);
 1786                         memcpy(wkey[i].wi_keydat, vap->iv_nw_keys[i].wk_key,
 1787                             keylen);
 1788                 }
 1789                 error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
 1790                     wkey, sizeof(wkey));
 1791                 sc->sc_encryption = 0;
 1792                 break;
 1793 
 1794         case WI_INTERSIL:
 1795                 val = HOST_ENCRYPT | HOST_DECRYPT;
 1796                 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
 1797                         /*
 1798                          * ONLY HWB3163 EVAL-CARD Firmware version
 1799                          * less than 0.8 variant2
 1800                          *
 1801                          *   If promiscuous mode disable, Prism2 chip
 1802                          *  does not work with WEP .
 1803                          * It is under investigation for details.
 1804                          * (ichiro@netbsd.org)
 1805                          */
 1806                         if (sc->sc_sta_firmware_ver < 802 ) {
 1807                                 /* firm ver < 0.8 variant 2 */
 1808                                 wi_write_val(sc, WI_RID_PROMISC, 1);
 1809                         }
 1810                         wi_write_val(sc, WI_RID_CNFAUTHMODE,
 1811                             vap->iv_bss->ni_authmode);
 1812                         val |= PRIVACY_INVOKED;
 1813                 } else {
 1814                         wi_write_val(sc, WI_RID_CNFAUTHMODE, IEEE80211_AUTH_OPEN);
 1815                 }
 1816                 error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
 1817                 if (error)
 1818                         break;
 1819                 sc->sc_encryption = val;
 1820                 if ((val & PRIVACY_INVOKED) == 0)
 1821                         break;
 1822                 error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY, vap->iv_def_txkey);
 1823                 break;
 1824         }
 1825         return error;
 1826 }
 1827 
 1828 static int
 1829 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
 1830 {
 1831         int i, s = 0;
 1832 
 1833         if (sc->wi_gone)
 1834                 return (ENODEV);
 1835 
 1836         /* wait for the busy bit to clear */
 1837         for (i = sc->wi_cmd_count; i > 0; i--) {        /* 500ms */
 1838                 if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
 1839                         break;
 1840                 DELAY(1*1000);  /* 1ms */
 1841         }
 1842         if (i == 0) {
 1843                 device_printf(sc->sc_dev, "%s: busy bit won't clear, cmd 0x%x\n",
 1844                    __func__, cmd);
 1845                 sc->wi_gone = 1;
 1846                 return(ETIMEDOUT);
 1847         }
 1848 
 1849         CSR_WRITE_2(sc, WI_PARAM0, val0);
 1850         CSR_WRITE_2(sc, WI_PARAM1, val1);
 1851         CSR_WRITE_2(sc, WI_PARAM2, val2);
 1852         CSR_WRITE_2(sc, WI_COMMAND, cmd);
 1853 
 1854         if (cmd == WI_CMD_INI) {
 1855                 /* XXX: should sleep here. */
 1856                 DELAY(100*1000);                /* 100ms delay for init */
 1857         }
 1858         for (i = 0; i < WI_TIMEOUT; i++) {
 1859                 /*
 1860                  * Wait for 'command complete' bit to be
 1861                  * set in the event status register.
 1862                  */
 1863                 s = CSR_READ_2(sc, WI_EVENT_STAT);
 1864                 if (s & WI_EV_CMD) {
 1865                         /* Ack the event and read result code. */
 1866                         s = CSR_READ_2(sc, WI_STATUS);
 1867                         CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
 1868                         if (s & WI_STAT_CMD_RESULT) {
 1869                                 return(EIO);
 1870                         }
 1871                         break;
 1872                 }
 1873                 DELAY(WI_DELAY);
 1874         }
 1875 
 1876         if (i == WI_TIMEOUT) {
 1877                 device_printf(sc->sc_dev, "%s: timeout on cmd 0x%04x; "
 1878                     "event status 0x%04x\n", __func__, cmd, s);
 1879                 if (s == 0xffff)
 1880                         sc->wi_gone = 1;
 1881                 return(ETIMEDOUT);
 1882         }
 1883         return (0);
 1884 }
 1885 
 1886 static int
 1887 wi_seek_bap(struct wi_softc *sc, int id, int off)
 1888 {
 1889         int i, status;
 1890 
 1891         CSR_WRITE_2(sc, WI_SEL0, id);
 1892         CSR_WRITE_2(sc, WI_OFF0, off);
 1893 
 1894         for (i = 0; ; i++) {
 1895                 status = CSR_READ_2(sc, WI_OFF0);
 1896                 if ((status & WI_OFF_BUSY) == 0)
 1897                         break;
 1898                 if (i == WI_TIMEOUT) {
 1899                         device_printf(sc->sc_dev, "%s: timeout, id %x off %x\n",
 1900                             __func__, id, off);
 1901                         sc->sc_bap_off = WI_OFF_ERR;    /* invalidate */
 1902                         if (status == 0xffff)
 1903                                 sc->wi_gone = 1;
 1904                         return ETIMEDOUT;
 1905                 }
 1906                 DELAY(1);
 1907         }
 1908         if (status & WI_OFF_ERR) {
 1909                 device_printf(sc->sc_dev, "%s: error, id %x off %x\n",
 1910                     __func__, id, off);
 1911                 sc->sc_bap_off = WI_OFF_ERR;    /* invalidate */
 1912                 return EIO;
 1913         }
 1914         sc->sc_bap_id = id;
 1915         sc->sc_bap_off = off;
 1916         return 0;
 1917 }
 1918 
 1919 static int
 1920 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
 1921 {
 1922         int error, cnt;
 1923 
 1924         if (buflen == 0)
 1925                 return 0;
 1926         if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
 1927                 if ((error = wi_seek_bap(sc, id, off)) != 0)
 1928                         return error;
 1929         }
 1930         cnt = (buflen + 1) / 2;
 1931         CSR_READ_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
 1932         sc->sc_bap_off += cnt * 2;
 1933         return 0;
 1934 }
 1935 
 1936 static int
 1937 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
 1938 {
 1939         int error, cnt;
 1940 
 1941         if (buflen == 0)
 1942                 return 0;
 1943 
 1944         if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
 1945                 if ((error = wi_seek_bap(sc, id, off)) != 0)
 1946                         return error;
 1947         }
 1948         cnt = (buflen + 1) / 2;
 1949         CSR_WRITE_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
 1950         sc->sc_bap_off += cnt * 2;
 1951 
 1952         return 0;
 1953 }
 1954 
 1955 static int
 1956 wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
 1957 {
 1958         int error, len;
 1959         struct mbuf *m;
 1960 
 1961         for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
 1962                 if (m->m_len == 0)
 1963                         continue;
 1964 
 1965                 len = min(m->m_len, totlen);
 1966 
 1967                 if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
 1968                         m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
 1969                         return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
 1970                             totlen);
 1971                 }
 1972 
 1973                 if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
 1974                         return error;
 1975 
 1976                 off += m->m_len;
 1977                 totlen -= len;
 1978         }
 1979         return 0;
 1980 }
 1981 
 1982 static int
 1983 wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
 1984 {
 1985         int i;
 1986 
 1987         if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
 1988                 device_printf(sc->sc_dev, "%s: failed to allocate %d bytes on NIC\n",
 1989                     __func__, len);
 1990                 return ENOMEM;
 1991         }
 1992 
 1993         for (i = 0; i < WI_TIMEOUT; i++) {
 1994                 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
 1995                         break;
 1996                 DELAY(1);
 1997         }
 1998         if (i == WI_TIMEOUT) {
 1999                 device_printf(sc->sc_dev, "%s: timeout in alloc\n", __func__);
 2000                 return ETIMEDOUT;
 2001         }
 2002         *idp = CSR_READ_2(sc, WI_ALLOC_FID);
 2003         CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
 2004         return 0;
 2005 }
 2006 
 2007 static int
 2008 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
 2009 {
 2010         int error, len;
 2011         u_int16_t ltbuf[2];
 2012 
 2013         /* Tell the NIC to enter record read mode. */
 2014         error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
 2015         if (error)
 2016                 return error;
 2017 
 2018         error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
 2019         if (error)
 2020                 return error;
 2021 
 2022         if (le16toh(ltbuf[1]) != rid) {
 2023                 device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n",
 2024                     rid, le16toh(ltbuf[1]));
 2025                 return EIO;
 2026         }
 2027         len = (le16toh(ltbuf[0]) - 1) * 2;       /* already got rid */
 2028         if (*buflenp < len) {
 2029                 device_printf(sc->sc_dev, "record buffer is too small, "
 2030                     "rid=%x, size=%d, len=%d\n",
 2031                     rid, *buflenp, len);
 2032                 return ENOSPC;
 2033         }
 2034         *buflenp = len;
 2035         return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
 2036 }
 2037 
 2038 static int
 2039 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
 2040 {
 2041         int error;
 2042         u_int16_t ltbuf[2];
 2043 
 2044         ltbuf[0] = htole16((buflen + 1) / 2 + 1);        /* includes rid */
 2045         ltbuf[1] = htole16(rid);
 2046 
 2047         error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
 2048         if (error) {
 2049                 device_printf(sc->sc_dev, "%s: bap0 write failure, rid 0x%x\n",
 2050                     __func__, rid);
 2051                 return error;
 2052         }
 2053         error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
 2054         if (error) {
 2055                 device_printf(sc->sc_dev, "%s: bap1 write failure, rid 0x%x\n",
 2056                     __func__, rid);
 2057                 return error;
 2058         }
 2059 
 2060         return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
 2061 }
 2062 
 2063 static int
 2064 wi_write_appie(struct wi_softc *sc, int rid, const struct ieee80211_appie *ie)
 2065 {
 2066         /* NB: 42 bytes is probably ok to have on the stack */
 2067         char buf[sizeof(uint16_t) + 40];
 2068 
 2069         if (ie->ie_len > 40)
 2070                 return EINVAL;
 2071         /* NB: firmware requires 16-bit ie length before ie data */
 2072         *(uint16_t *) buf = htole16(ie->ie_len);
 2073         memcpy(buf + sizeof(uint16_t), ie->ie_data, ie->ie_len);
 2074         return wi_write_rid(sc, rid, buf, ie->ie_len + sizeof(uint16_t));
 2075 }
 2076 
 2077 int
 2078 wi_alloc(device_t dev, int rid)
 2079 {
 2080         struct wi_softc *sc = device_get_softc(dev);
 2081 
 2082         if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
 2083                 sc->iobase_rid = rid;
 2084                 sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
 2085                     &sc->iobase_rid, 0, ~0, (1 << 6),
 2086                     rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
 2087                 if (sc->iobase == NULL) {
 2088                         device_printf(dev, "No I/O space?!\n");
 2089                         return ENXIO;
 2090                 }
 2091 
 2092                 sc->wi_io_addr = rman_get_start(sc->iobase);
 2093                 sc->wi_btag = rman_get_bustag(sc->iobase);
 2094                 sc->wi_bhandle = rman_get_bushandle(sc->iobase);
 2095         } else {
 2096                 sc->mem_rid = rid;
 2097                 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
 2098                     &sc->mem_rid, RF_ACTIVE);
 2099                 if (sc->mem == NULL) {
 2100                         device_printf(dev, "No Mem space on prism2.5?\n");
 2101                         return ENXIO;
 2102                 }
 2103 
 2104                 sc->wi_btag = rman_get_bustag(sc->mem);
 2105                 sc->wi_bhandle = rman_get_bushandle(sc->mem);
 2106         }
 2107 
 2108         sc->irq_rid = 0;
 2109         sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
 2110             RF_ACTIVE |
 2111             ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
 2112         if (sc->irq == NULL) {
 2113                 wi_free(dev);
 2114                 device_printf(dev, "No irq?!\n");
 2115                 return ENXIO;
 2116         }
 2117 
 2118         sc->sc_dev = dev;
 2119         sc->sc_unit = device_get_unit(dev);
 2120         return 0;
 2121 }
 2122 
 2123 void
 2124 wi_free(device_t dev)
 2125 {
 2126         struct wi_softc *sc = device_get_softc(dev);
 2127 
 2128         if (sc->iobase != NULL) {
 2129                 bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
 2130                 sc->iobase = NULL;
 2131         }
 2132         if (sc->irq != NULL) {
 2133                 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
 2134                 sc->irq = NULL;
 2135         }
 2136         if (sc->mem != NULL) {
 2137                 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
 2138                 sc->mem = NULL;
 2139         }
 2140 }

Cache object: dd85b7fd75aabd229b3207fb1d0ae3f4


[ 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.