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

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.