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

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

Cache object: 73afacd18385ecaf843627832a660a06


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