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

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.