if_wi.c revision 148714
1109323Ssam/* $NetBSD: wi.c,v 1.109 2003/01/09 08:52:19 dyoung Exp $ */ 2109323Ssam 3139749Simp/*- 446492Swpaul * Copyright (c) 1997, 1998, 1999 546492Swpaul * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 646492Swpaul * 746492Swpaul * Redistribution and use in source and binary forms, with or without 846492Swpaul * modification, are permitted provided that the following conditions 946492Swpaul * are met: 1046492Swpaul * 1. Redistributions of source code must retain the above copyright 1146492Swpaul * notice, this list of conditions and the following disclaimer. 1246492Swpaul * 2. Redistributions in binary form must reproduce the above copyright 1346492Swpaul * notice, this list of conditions and the following disclaimer in the 1446492Swpaul * documentation and/or other materials provided with the distribution. 1546492Swpaul * 3. All advertising materials mentioning features or use of this software 1646492Swpaul * must display the following acknowledgement: 1746492Swpaul * This product includes software developed by Bill Paul. 1846492Swpaul * 4. Neither the name of the author nor the names of any co-contributors 1946492Swpaul * may be used to endorse or promote products derived from this software 2046492Swpaul * without specific prior written permission. 2146492Swpaul * 2246492Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 2346492Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2446492Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2546492Swpaul * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 2646492Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2746492Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2846492Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2946492Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3046492Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3146492Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 3246492Swpaul * THE POSSIBILITY OF SUCH DAMAGE. 3346492Swpaul */ 3446492Swpaul 3546492Swpaul/* 36109323Ssam * Lucent WaveLAN/IEEE 802.11 PCMCIA driver. 3746492Swpaul * 38109323Ssam * Original FreeBSD driver written by Bill Paul <wpaul@ctr.columbia.edu> 3946492Swpaul * Electrical Engineering Department 4046492Swpaul * Columbia University, New York City 4146492Swpaul */ 4246492Swpaul 4346492Swpaul/* 4447401Swpaul * The WaveLAN/IEEE adapter is the second generation of the WaveLAN 4546492Swpaul * from Lucent. Unlike the older cards, the new ones are programmed 4646492Swpaul * entirely via a firmware-driven controller called the Hermes. 4746492Swpaul * Unfortunately, Lucent will not release the Hermes programming manual 4846492Swpaul * without an NDA (if at all). What they do release is an API library 4946492Swpaul * called the HCF (Hardware Control Functions) which is supposed to 5046492Swpaul * do the device-specific operations of a device driver for you. The 5146492Swpaul * publically available version of the HCF library (the 'HCF Light') is 5247401Swpaul * a) extremely gross, b) lacks certain features, particularly support 5346492Swpaul * for 802.11 frames, and c) is contaminated by the GNU Public License. 5446492Swpaul * 5546492Swpaul * This driver does not use the HCF or HCF Light at all. Instead, it 5646492Swpaul * programs the Hermes controller directly, using information gleaned 5746492Swpaul * from the HCF Light code and corresponding documentation. 5846492Swpaul * 5995534Simp * This driver supports the ISA, PCMCIA and PCI versions of the Lucent 6095534Simp * WaveLan cards (based on the Hermes chipset), as well as the newer 6195534Simp * Prism 2 chipsets with firmware from Intersil and Symbol. 6246492Swpaul */ 6346492Swpaul 64113038Sobrien#include <sys/cdefs.h> 65113038Sobrien__FBSDID("$FreeBSD: head/sys/dev/wi/if_wi.c 148714 2005-08-05 04:56:14Z imp $"); 66113038Sobrien 67109323Ssam#define WI_HERMES_AUTOINC_WAR /* Work around data write autoinc bug. */ 68109323Ssam#define WI_HERMES_STATS_WAR /* Work around stats counter bug. */ 69109323Ssam 70109323Ssam#define NBPFILTER 1 71109323Ssam 7246492Swpaul#include <sys/param.h> 7346492Swpaul#include <sys/systm.h> 7495706Simp#if __FreeBSD_version >= 500033 7595533Smike#include <sys/endian.h> 7695706Simp#endif 7746492Swpaul#include <sys/sockio.h> 7846492Swpaul#include <sys/mbuf.h> 7983366Sjulian#include <sys/proc.h> 8046492Swpaul#include <sys/kernel.h> 8146492Swpaul#include <sys/socket.h> 8253702Swpaul#include <sys/module.h> 8353702Swpaul#include <sys/bus.h> 8494486Simp#include <sys/random.h> 8553702Swpaul#include <sys/syslog.h> 8653702Swpaul#include <sys/sysctl.h> 8746492Swpaul 8853702Swpaul#include <machine/bus.h> 8953702Swpaul#include <machine/resource.h> 9095534Simp#include <machine/clock.h> 91116951Ssam#include <machine/atomic.h> 9253702Swpaul#include <sys/rman.h> 9353702Swpaul 9446492Swpaul#include <net/if.h> 9546492Swpaul#include <net/if_arp.h> 9646492Swpaul#include <net/ethernet.h> 9746492Swpaul#include <net/if_dl.h> 9846492Swpaul#include <net/if_media.h> 9946492Swpaul#include <net/if_types.h> 10046492Swpaul 101116951Ssam#include <net80211/ieee80211_var.h> 102116951Ssam#include <net80211/ieee80211_ioctl.h> 103119784Ssam#include <net80211/ieee80211_radiotap.h> 104116951Ssam 10546492Swpaul#include <netinet/in.h> 10646492Swpaul#include <netinet/in_systm.h> 10746492Swpaul#include <netinet/in_var.h> 10846492Swpaul#include <netinet/ip.h> 10946492Swpaul#include <netinet/if_ether.h> 11046492Swpaul 11146492Swpaul#include <net/bpf.h> 11246492Swpaul 11370808Speter#include <dev/wi/if_wavelan_ieee.h> 114119784Ssam#include <dev/wi/if_wireg.h> 11593611Simp#include <dev/wi/if_wivar.h> 11646492Swpaul 11791693Simpstatic void wi_start(struct ifnet *); 118109323Ssamstatic int wi_reset(struct wi_softc *); 11991693Simpstatic void wi_watchdog(struct ifnet *); 120109323Ssamstatic int wi_ioctl(struct ifnet *, u_long, caddr_t); 121109323Ssamstatic int wi_media_change(struct ifnet *); 122109323Ssamstatic void wi_media_status(struct ifnet *, struct ifmediareq *); 12346492Swpaul 124109323Ssamstatic void wi_rx_intr(struct wi_softc *); 125109323Ssamstatic void wi_tx_intr(struct wi_softc *); 126109323Ssamstatic void wi_tx_ex_intr(struct wi_softc *); 127109323Ssamstatic void wi_info_intr(struct wi_softc *); 12846492Swpaul 129109323Ssamstatic int wi_get_cfg(struct ifnet *, u_long, caddr_t); 130109323Ssamstatic int wi_set_cfg(struct ifnet *, u_long, caddr_t); 131109323Ssamstatic int wi_write_txrate(struct wi_softc *); 132109323Ssamstatic int wi_write_wep(struct wi_softc *); 133109323Ssamstatic int wi_write_multi(struct wi_softc *); 134109323Ssamstatic int wi_alloc_fid(struct wi_softc *, int, int *); 135109323Ssamstatic void wi_read_nicid(struct wi_softc *); 136109323Ssamstatic int wi_write_ssid(struct wi_softc *, int, u_int8_t *, int); 13753702Swpaul 138109323Ssamstatic int wi_cmd(struct wi_softc *, int, int, int, int); 139109323Ssamstatic int wi_seek_bap(struct wi_softc *, int, int); 140109323Ssamstatic int wi_read_bap(struct wi_softc *, int, int, void *, int); 141109323Ssamstatic int wi_write_bap(struct wi_softc *, int, int, void *, int); 142109323Ssamstatic int wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int); 143109323Ssamstatic int wi_read_rid(struct wi_softc *, int, void *, int *); 144109323Ssamstatic int wi_write_rid(struct wi_softc *, int, void *, int); 14577217Sphk 146117812Ssamstatic int wi_newstate(struct ieee80211com *, enum ieee80211_state, int); 147109323Ssam 148116898Ssamstatic int wi_scan_ap(struct wi_softc *, u_int16_t, u_int16_t); 149109323Ssamstatic void wi_scan_result(struct wi_softc *, int, int); 150109323Ssam 151109323Ssamstatic void wi_dump_pkt(struct wi_frame *, struct ieee80211_node *, int rssi); 152109323Ssam 15393359Simpstatic int wi_get_debug(struct wi_softc *, struct wi_req *); 15493359Simpstatic int wi_set_debug(struct wi_softc *, struct wi_req *); 15593359Simp 156105076Simp#if __FreeBSD_version >= 500000 157101903Simp/* support to download firmware for symbol CF card */ 158101903Simpstatic int wi_symbol_write_firm(struct wi_softc *, const void *, int, 159101903Simp const void *, int); 160101903Simpstatic int wi_symbol_set_hcr(struct wi_softc *, int); 161105076Simp#endif 162101903Simp 163109323Ssamstatic __inline int 164109323Ssamwi_write_val(struct wi_softc *sc, int rid, u_int16_t val) 165109323Ssam{ 16653702Swpaul 167109323Ssam val = htole16(val); 168109323Ssam return wi_write_rid(sc, rid, &val, sizeof(val)); 169109323Ssam} 170109323Ssam 171109592SsamSYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters"); 172109592Ssam 173109323Ssamstatic struct timeval lasttxerror; /* time of last tx error msg */ 174109323Ssamstatic int curtxeps; /* current tx error msgs/sec */ 175111559Ssamstatic int wi_txerate = 0; /* tx error rate: max msgs/sec */ 176109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate, 177111559Ssam 0, "max tx error msgs/sec; 0 to disable msgs"); 178109323Ssam 179109323Ssam#define WI_DEBUG 180109323Ssam#ifdef WI_DEBUG 181109323Ssamstatic int wi_debug = 0; 182109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug, 183109592Ssam 0, "control debugging printfs"); 184109323Ssam 185109323Ssam#define DPRINTF(X) if (wi_debug) printf X 186109323Ssam#define DPRINTF2(X) if (wi_debug > 1) printf X 187109323Ssam#define IFF_DUMPPKTS(_ifp) \ 188109323Ssam (((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2)) 189109323Ssam#else 190109323Ssam#define DPRINTF(X) 191109323Ssam#define DPRINTF2(X) 192109323Ssam#define IFF_DUMPPKTS(_ifp) 0 193109323Ssam#endif 194109323Ssam 195109323Ssam#define WI_INTRS (WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO) 196109323Ssam 19793825Simpstruct wi_card_ident wi_card_ident[] = { 19893825Simp /* CARD_ID CARD_NAME FIRM_TYPE */ 19993825Simp { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT }, 20093825Simp { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT }, 20193825Simp { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT }, 20293825Simp { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL }, 20393825Simp { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL }, 20493825Simp { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL }, 20593825Simp { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL }, 20693825Simp { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL }, 20793825Simp { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL }, 20893825Simp { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL }, 20993825Simp { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL }, 21093825Simp { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL }, 21193825Simp { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 21293825Simp { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 213101355Simp { WI_NIC_3842_PCMCIA_ATL_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 214101355Simp { WI_NIC_3842_PCMCIA_ATS_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 21593825Simp { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 21693825Simp { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 217101355Simp { WI_NIC_3842_MINI_ATL_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 218101355Simp { WI_NIC_3842_MINI_ATS_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 21993825Simp { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 22093825Simp { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 221101355Simp { WI_NIC_3842_PCI_ATS_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 222101355Simp { WI_NIC_3842_PCI_ATL_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 22393825Simp { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 22493825Simp { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 225101355Simp { WI_NIC_P3_PCMCIA_ATL_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 226101355Simp { WI_NIC_P3_PCMCIA_ATS_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 22793825Simp { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 22893825Simp { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 229101355Simp { WI_NIC_P3_MINI_ATL_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 230101355Simp { WI_NIC_P3_MINI_ATS_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 23193825Simp { 0, NULL, 0 }, 23293825Simp}; 23393825Simp 234109323Ssamdevclass_t wi_devclass; 23546492Swpaul 23693611Simpint 237109323Ssamwi_attach(device_t dev) 23874906Salfred{ 239109323Ssam struct wi_softc *sc = device_get_softc(dev); 240109323Ssam struct ieee80211com *ic = &sc->sc_ic; 241147256Sbrooks struct ifnet *ifp; 242116951Ssam int i, nrates, buflen; 243109323Ssam u_int16_t val; 244109323Ssam u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE]; 245116951Ssam struct ieee80211_rateset *rs; 246109323Ssam static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = { 247109323Ssam 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 248109323Ssam }; 249109323Ssam int error; 25074906Salfred 251147256Sbrooks ifp = sc->sc_ifp = if_alloc(IFT_ETHER); 252147256Sbrooks if (ifp == NULL) { 253147256Sbrooks device_printf(dev, "can not if_alloc\n"); 254147256Sbrooks wi_free(dev); 255147256Sbrooks return (ENOSPC); 256147256Sbrooks } 257147256Sbrooks 258109323Ssam /* 259109323Ssam * NB: no locking is needed here; don't put it here 260109323Ssam * unless you can prove it! 261109323Ssam */ 262121697Ssam error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE, 26374998Swpaul wi_intr, sc, &sc->wi_intrhand); 26453702Swpaul 26553702Swpaul if (error) { 26653702Swpaul device_printf(dev, "bus_setup_intr() failed! (%d)\n", error); 26753702Swpaul wi_free(dev); 26853702Swpaul return (error); 26953702Swpaul } 27053702Swpaul 27195534Simp#if __FreeBSD_version >= 500000 272109323Ssam mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 27393818Sjhb MTX_DEF | MTX_RECURSE); 27495534Simp#endif 27567092Swpaul 276138571Ssam sc->sc_firmware_type = WI_NOTYPE; 277123339Simp sc->wi_cmd_count = 500; 27846492Swpaul /* Reset the NIC. */ 279112096Simp if (wi_reset(sc) != 0) 280109323Ssam return ENXIO; /* XXX */ 28146492Swpaul 28276438Swpaul /* 28376438Swpaul * Read the station address. 28476438Swpaul * And do it twice. I've seen PRISM-based cards that return 28576438Swpaul * an error when trying to read it the first time, which causes 28676438Swpaul * the probe to fail. 28776438Swpaul */ 288109323Ssam buflen = IEEE80211_ADDR_LEN; 289109323Ssam error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen); 290109323Ssam if (error != 0) { 291109323Ssam buflen = IEEE80211_ADDR_LEN; 292109323Ssam error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen); 293109323Ssam } 294109323Ssam if (error || IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) { 295109323Ssam if (error != 0) 296109323Ssam device_printf(dev, "mac read failed %d\n", error); 297148714Simp else { 298109323Ssam device_printf(dev, "mac read failed (all zeros)\n"); 299148714Simp error = ENXIO; 300148714Simp } 30175149Simp wi_free(dev); 30275149Simp return (error); 30375149Simp } 30446492Swpaul 305109323Ssam /* Read NIC identification */ 306109323Ssam wi_read_nicid(sc); 30746492Swpaul 30846492Swpaul ifp->if_softc = sc; 309121816Sbrooks if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 31046492Swpaul ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 31146492Swpaul ifp->if_ioctl = wi_ioctl; 31246492Swpaul ifp->if_start = wi_start; 31346492Swpaul ifp->if_watchdog = wi_watchdog; 31446492Swpaul ifp->if_init = wi_init; 315132986Smlaier IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 316132986Smlaier ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 317132986Smlaier IFQ_SET_READY(&ifp->if_snd); 31846492Swpaul 319138571Ssam ic->ic_ifp = ifp; 320109323Ssam ic->ic_phytype = IEEE80211_T_DS; 321109323Ssam ic->ic_opmode = IEEE80211_M_STA; 322109323Ssam ic->ic_state = IEEE80211_S_INIT; 323138571Ssam ic->ic_caps = IEEE80211_C_PMGT 324138571Ssam | IEEE80211_C_WEP /* everyone supports WEP */ 325138571Ssam ; 326138571Ssam ic->ic_max_aid = WI_MAX_AID; 32746492Swpaul 328116951Ssam /* 329116951Ssam * Query the card for available channels and setup the 330116951Ssam * channel table. We assume these are all 11b channels. 331116951Ssam */ 332109323Ssam buflen = sizeof(val); 333109323Ssam if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0) 334109323Ssam val = htole16(0x1fff); /* assume 1-11 */ 335116951Ssam KASSERT(val != 0, ("wi_attach: no available channels listed!")); 336116951Ssam 337116951Ssam val <<= 1; /* shift for base 1 indices */ 338116951Ssam for (i = 1; i < 16; i++) { 339144986Smdodd if (!isset((u_int8_t*)&val, i)) 340144986Smdodd continue; 341144986Smdodd ic->ic_channels[i].ic_freq = 342144986Smdodd ieee80211_ieee2mhz(i, IEEE80211_CHAN_B); 343144986Smdodd ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B; 344109323Ssam } 34546492Swpaul 34646563Swpaul /* 34746563Swpaul * Read the default channel from the NIC. This may vary 34846563Swpaul * depending on the country where the NIC was purchased, so 34946563Swpaul * we can't hard-code a default and expect it to work for 35046563Swpaul * everyone. 351116951Ssam * 352116951Ssam * If no channel is specified, let the 802.11 code select. 35346563Swpaul */ 354109323Ssam buflen = sizeof(val); 355116951Ssam if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0) { 356116951Ssam val = le16toh(val); 357116951Ssam KASSERT(val < IEEE80211_CHAN_MAX && 358116951Ssam ic->ic_channels[val].ic_flags != 0, 359116951Ssam ("wi_attach: invalid own channel %u!", val)); 360116951Ssam ic->ic_ibss_chan = &ic->ic_channels[val]; 361117042Ssam } else { 362117042Ssam device_printf(dev, 363117042Ssam "WI_RID_OWN_CHNL failed, using first channel!\n"); 364117042Ssam ic->ic_ibss_chan = &ic->ic_channels[0]; 365109323Ssam } 36646563Swpaul 36756965Swpaul /* 36898440Simp * Set flags based on firmware version. 36998440Simp */ 37098440Simp switch (sc->sc_firmware_type) { 37198440Simp case WI_LUCENT: 372112363Simp sc->sc_ntxbuf = 1; 373109323Ssam sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE; 374109323Ssam#ifdef WI_HERMES_AUTOINC_WAR 375109323Ssam /* XXX: not confirmed, but never seen for recent firmware */ 376109323Ssam if (sc->sc_sta_firmware_ver < 40000) { 377109323Ssam sc->sc_flags |= WI_FLAGS_BUG_AUTOINC; 378109323Ssam } 379109323Ssam#endif 38098440Simp if (sc->sc_sta_firmware_ver >= 60000) 381109323Ssam sc->sc_flags |= WI_FLAGS_HAS_MOR; 382117855Ssam if (sc->sc_sta_firmware_ver >= 60006) { 383116951Ssam ic->ic_caps |= IEEE80211_C_IBSS; 384117855Ssam ic->ic_caps |= IEEE80211_C_MONITOR; 385117855Ssam } 386109323Ssam sc->sc_ibss_port = htole16(1); 387119784Ssam 388119784Ssam sc->sc_min_rssi = WI_LUCENT_MIN_RSSI; 389119784Ssam sc->sc_max_rssi = WI_LUCENT_MAX_RSSI; 390119784Ssam sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET; 39198440Simp break; 392109323Ssam 39398440Simp case WI_INTERSIL: 394112363Simp sc->sc_ntxbuf = WI_NTXBUF; 395109323Ssam sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR; 396109323Ssam sc->sc_flags |= WI_FLAGS_HAS_ROAMING; 397109323Ssam sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE; 398123339Simp /* 399123339Simp * Old firmware are slow, so give peace a chance. 400123339Simp */ 401123339Simp if (sc->sc_sta_firmware_ver < 10000) 402123339Simp sc->wi_cmd_count = 5000; 403109323Ssam if (sc->sc_sta_firmware_ver > 10101) 404109323Ssam sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST; 405117855Ssam if (sc->sc_sta_firmware_ver >= 800) { 406116951Ssam ic->ic_caps |= IEEE80211_C_IBSS; 407117855Ssam ic->ic_caps |= IEEE80211_C_MONITOR; 408117855Ssam } 409109396Simp /* 410109396Simp * version 0.8.3 and newer are the only ones that are known 411109396Simp * to currently work. Earlier versions can be made to work, 412109396Simp * at least according to the Linux driver. 413109396Simp */ 414100734Simp if (sc->sc_sta_firmware_ver >= 803) 415116951Ssam ic->ic_caps |= IEEE80211_C_HOSTAP; 416109323Ssam sc->sc_ibss_port = htole16(0); 417119784Ssam 418119784Ssam sc->sc_min_rssi = WI_PRISM_MIN_RSSI; 419119784Ssam sc->sc_max_rssi = WI_PRISM_MAX_RSSI; 420119784Ssam sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET; 42198440Simp break; 422109323Ssam 42398440Simp case WI_SYMBOL: 424112363Simp sc->sc_ntxbuf = 1; 425109323Ssam sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY; 42698440Simp if (sc->sc_sta_firmware_ver >= 25000) 427116951Ssam ic->ic_caps |= IEEE80211_C_IBSS; 428109323Ssam sc->sc_ibss_port = htole16(4); 429119784Ssam 430119784Ssam sc->sc_min_rssi = WI_PRISM_MIN_RSSI; 431119784Ssam sc->sc_max_rssi = WI_PRISM_MAX_RSSI; 432119784Ssam sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET; 43398440Simp break; 43498440Simp } 43598440Simp 43698440Simp /* 43756965Swpaul * Find out if we support WEP on this card. 43856965Swpaul */ 439109323Ssam buflen = sizeof(val); 440109323Ssam if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 && 441109323Ssam val != htole16(0)) 442116951Ssam ic->ic_caps |= IEEE80211_C_WEP; 44356965Swpaul 444109323Ssam /* Find supported rates. */ 445109323Ssam buflen = sizeof(ratebuf); 446116951Ssam rs = &ic->ic_sup_rates[IEEE80211_MODE_11B]; 447109323Ssam if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) { 448116951Ssam nrates = le16toh(*(u_int16_t *)ratebuf); 449116951Ssam if (nrates > IEEE80211_RATE_MAXSIZE) 450116951Ssam nrates = IEEE80211_RATE_MAXSIZE; 451116951Ssam rs->rs_nrates = 0; 452116951Ssam for (i = 0; i < nrates; i++) 453116951Ssam if (ratebuf[2+i]) 454116951Ssam rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i]; 455109323Ssam } else { 456109323Ssam /* XXX fallback on error? */ 457116951Ssam rs->rs_nrates = 0; 45898440Simp } 45977217Sphk 460109323Ssam buflen = sizeof(val); 461109323Ssam if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) && 462109323Ssam wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) { 463119784Ssam sc->sc_dbm_offset = le16toh(val); 464119784Ssam } 46546492Swpaul 466109323Ssam sc->sc_max_datalen = 2304; 467109323Ssam sc->sc_system_scale = 1; 468109323Ssam sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN; 469109323Ssam sc->sc_roaming_mode = 1; 47046492Swpaul 471109323Ssam sc->sc_portnum = WI_DEFAULT_PORT; 472109323Ssam sc->sc_authtype = WI_DEFAULT_AUTHTYPE; 47387383Simp 474109323Ssam bzero(sc->sc_nodename, sizeof(sc->sc_nodename)); 475109323Ssam sc->sc_nodelen = sizeof(WI_DEFAULT_NODENAME) - 1; 476109323Ssam bcopy(WI_DEFAULT_NODENAME, sc->sc_nodename, sc->sc_nodelen); 47787383Simp 478109323Ssam bzero(sc->sc_net_name, sizeof(sc->sc_net_name)); 479109323Ssam bcopy(WI_DEFAULT_NETNAME, sc->sc_net_name, 480109323Ssam sizeof(WI_DEFAULT_NETNAME) - 1); 48193733Simp 48293733Simp /* 483109323Ssam * Call MI attach routine. 48493733Simp */ 485138571Ssam ieee80211_ifattach(ic); 486117812Ssam /* override state transition method */ 487117812Ssam sc->sc_newstate = ic->ic_newstate; 488117812Ssam ic->ic_newstate = wi_newstate; 489138571Ssam ieee80211_media_init(ic, wi_media_change, wi_media_status); 490109323Ssam 491119784Ssam#if NBPFILTER > 0 492119784Ssam bpfattach2(ifp, DLT_IEEE802_11_RADIO, 493119784Ssam sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th), 494119784Ssam &sc->sc_drvbpf); 495119784Ssam /* 496119784Ssam * Initialize constant fields. 497127698Ssam * XXX make header lengths a multiple of 32-bits so subsequent 498127698Ssam * headers are properly aligned; this is a kludge to keep 499127698Ssam * certain applications happy. 500119784Ssam * 501119784Ssam * NB: the channel is setup each time we transition to the 502119784Ssam * RUN state to avoid filling it in for each frame. 503119784Ssam */ 504127698Ssam sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t)); 505127698Ssam sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len); 506127698Ssam sc->sc_tx_th.wt_ihdr.it_present = htole32(WI_TX_RADIOTAP_PRESENT); 507119784Ssam 508127698Ssam sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t)); 509127698Ssam sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len); 510127698Ssam sc->sc_rx_th.wr_ihdr.it_present = htole32(WI_RX_RADIOTAP_PRESENT); 511119784Ssam#endif 512138571Ssam 513138571Ssam if (bootverbose) 514138571Ssam ieee80211_announce(ic); 515138571Ssam 516109323Ssam return (0); 51787383Simp} 51887383Simp 519109323Ssamint 520109323Ssamwi_detach(device_t dev) 52146492Swpaul{ 522109323Ssam struct wi_softc *sc = device_get_softc(dev); 523147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 524109323Ssam WI_LOCK_DECL(); 52546492Swpaul 526109323Ssam WI_LOCK(sc); 52746492Swpaul 528109323Ssam /* check if device was removed */ 529123098Simp sc->wi_gone |= !bus_child_present(dev); 53046492Swpaul 531109323Ssam wi_stop(ifp, 0); 53246492Swpaul 533119784Ssam#if NBPFILTER > 0 534119784Ssam bpfdetach(ifp); 535119784Ssam#endif 536138571Ssam ieee80211_ifdetach(&sc->sc_ic); 537147256Sbrooks if_free(sc->sc_ifp); 538114190Simp WI_UNLOCK(sc); 539109323Ssam bus_teardown_intr(dev, sc->irq, sc->wi_intrhand); 540109323Ssam wi_free(dev); 541109323Ssam#if __FreeBSD_version >= 500000 542109323Ssam mtx_destroy(&sc->sc_mtx); 54393359Simp#endif 544109323Ssam return (0); 545109323Ssam} 54693359Simp 547109323Ssam#ifdef __NetBSD__ 548109323Ssamint 549109323Ssamwi_activate(struct device *self, enum devact act) 550109323Ssam{ 551109323Ssam struct wi_softc *sc = (struct wi_softc *)self; 552109323Ssam int rv = 0, s; 55393359Simp 554109323Ssam s = splnet(); 555109323Ssam switch (act) { 556109323Ssam case DVACT_ACTIVATE: 557109323Ssam rv = EOPNOTSUPP; 558109323Ssam break; 55993359Simp 560109323Ssam case DVACT_DEACTIVATE: 561147256Sbrooks if_deactivate(sc->sc_ifp); 562109323Ssam break; 563109323Ssam } 564109323Ssam splx(s); 565109323Ssam return rv; 566109323Ssam} 56793359Simp 568109323Ssamvoid 569109323Ssamwi_power(struct wi_softc *sc, int why) 570109323Ssam{ 571147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 572109323Ssam int s; 57393359Simp 574109323Ssam s = splnet(); 575109323Ssam switch (why) { 576109323Ssam case PWR_SUSPEND: 577109323Ssam case PWR_STANDBY: 578109323Ssam wi_stop(ifp, 1); 579109323Ssam break; 580109323Ssam case PWR_RESUME: 581109323Ssam if (ifp->if_flags & IFF_UP) { 582109323Ssam wi_init(ifp); 583109323Ssam (void)wi_intr(sc); 58494405Simp } 585109323Ssam break; 586109323Ssam case PWR_SOFTSUSPEND: 587109323Ssam case PWR_SOFTSTANDBY: 588109323Ssam case PWR_SOFTRESUME: 589109323Ssam break; 59093359Simp } 591109323Ssam splx(s); 59246492Swpaul} 593109323Ssam#endif /* __NetBSD__ */ 59446492Swpaul 595109323Ssamvoid 596109323Ssamwi_shutdown(device_t dev) 59746492Swpaul{ 598109323Ssam struct wi_softc *sc = device_get_softc(dev); 59946492Swpaul 600147256Sbrooks wi_stop(sc->sc_ifp, 1); 60146492Swpaul} 60246492Swpaul 603109323Ssamvoid 604109323Ssamwi_intr(void *arg) 60546492Swpaul{ 606109323Ssam struct wi_softc *sc = arg; 607147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 608112363Simp u_int16_t status; 609109323Ssam WI_LOCK_DECL(); 61046492Swpaul 611109323Ssam WI_LOCK(sc); 61246492Swpaul 613123098Simp if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) { 614113327Simp CSR_WRITE_2(sc, WI_INT_EN, 0); 615123098Simp CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF); 616109323Ssam WI_UNLOCK(sc); 61746492Swpaul return; 618109323Ssam } 61946492Swpaul 620112363Simp /* Disable interrupts. */ 621112363Simp CSR_WRITE_2(sc, WI_INT_EN, 0); 62246492Swpaul 623112363Simp status = CSR_READ_2(sc, WI_EVENT_STAT); 624112363Simp if (status & WI_EV_RX) 625112363Simp wi_rx_intr(sc); 626112363Simp if (status & WI_EV_ALLOC) 627112363Simp wi_tx_intr(sc); 628112363Simp if (status & WI_EV_TX_EXC) 629112363Simp wi_tx_ex_intr(sc); 630112363Simp if (status & WI_EV_INFO) 631112363Simp wi_info_intr(sc); 632112363Simp if ((ifp->if_flags & IFF_OACTIVE) == 0 && 633112363Simp (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 && 634132986Smlaier !IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 635112363Simp wi_start(ifp); 63646492Swpaul 637112363Simp /* Re-enable interrupts. */ 638112363Simp CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 63946492Swpaul 640109323Ssam WI_UNLOCK(sc); 64146492Swpaul 64246492Swpaul return; 64346492Swpaul} 64446492Swpaul 645109323Ssamvoid 646109323Ssamwi_init(void *arg) 64746492Swpaul{ 648109323Ssam struct wi_softc *sc = arg; 649147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 650109323Ssam struct ieee80211com *ic = &sc->sc_ic; 651109323Ssam struct wi_joinreq join; 652109323Ssam int i; 653109323Ssam int error = 0, wasenabled; 654109323Ssam struct ifaddr *ifa; 655109323Ssam struct sockaddr_dl *sdl; 656109323Ssam WI_LOCK_DECL(); 65746492Swpaul 658109323Ssam WI_LOCK(sc); 65967092Swpaul 660109323Ssam if (sc->wi_gone) { 661109323Ssam WI_UNLOCK(sc); 66246492Swpaul return; 66346492Swpaul } 66446492Swpaul 665112362Simp if ((wasenabled = sc->sc_enabled)) 666123098Simp wi_stop(ifp, 1); 667112362Simp wi_reset(sc); 66846492Swpaul 669109323Ssam /* common 802.11 configuration */ 670109323Ssam ic->ic_flags &= ~IEEE80211_F_IBSSON; 671109323Ssam sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 672109323Ssam switch (ic->ic_opmode) { 673109323Ssam case IEEE80211_M_STA: 674109323Ssam wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS); 675109323Ssam break; 676109323Ssam case IEEE80211_M_IBSS: 677109323Ssam wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port); 678109323Ssam ic->ic_flags |= IEEE80211_F_IBSSON; 679109323Ssam break; 680109323Ssam case IEEE80211_M_AHDEMO: 681109323Ssam wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC); 682109323Ssam break; 683109323Ssam case IEEE80211_M_HOSTAP: 684122087Sgreen /* 685122087Sgreen * For PRISM cards, override the empty SSID, because in 686122087Sgreen * HostAP mode the controller will lock up otherwise. 687122087Sgreen */ 688122087Sgreen if (sc->sc_firmware_type == WI_INTERSIL && 689122087Sgreen ic->ic_des_esslen == 0) { 690122087Sgreen ic->ic_des_essid[0] = ' '; 691122087Sgreen ic->ic_des_esslen = 1; 692122087Sgreen } 693109323Ssam wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP); 694109323Ssam break; 695117855Ssam case IEEE80211_M_MONITOR: 696117855Ssam if (sc->sc_firmware_type == WI_LUCENT) 697117855Ssam wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC); 698117855Ssam wi_cmd(sc, WI_CMD_DEBUG | (WI_TEST_MONITOR << 8), 0, 0, 0); 699117855Ssam break; 70046492Swpaul } 70146492Swpaul 702109323Ssam /* Intersil interprets this RID as joining ESS even in IBSS mode */ 703109323Ssam if (sc->sc_firmware_type == WI_LUCENT && 704109323Ssam (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0) 705109323Ssam wi_write_val(sc, WI_RID_CREATE_IBSS, 1); 706109323Ssam else 707109323Ssam wi_write_val(sc, WI_RID_CREATE_IBSS, 0); 708109323Ssam wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval); 709109323Ssam wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid, 710109323Ssam ic->ic_des_esslen); 711116951Ssam wi_write_val(sc, WI_RID_OWN_CHNL, 712116951Ssam ieee80211_chan2ieee(ic, ic->ic_ibss_chan)); 713109323Ssam wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen); 71446492Swpaul 715109323Ssam ifa = ifaddr_byindex(ifp->if_index); 716109323Ssam sdl = (struct sockaddr_dl *) ifa->ifa_addr; 717109323Ssam IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(sdl)); 718109323Ssam wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN); 71975373Salfred 720138571Ssam if (ic->ic_caps & IEEE80211_C_PMGT) 721138571Ssam wi_write_val(sc, WI_RID_PM_ENABLED, 722138571Ssam (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0); 72346492Swpaul 724109323Ssam /* not yet common 802.11 configuration */ 725109323Ssam wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen); 726116951Ssam wi_write_val(sc, WI_RID_RTS_THRESH, ic->ic_rtsthreshold); 727109323Ssam if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) 728116951Ssam wi_write_val(sc, WI_RID_FRAG_THRESH, ic->ic_fragthreshold); 72946492Swpaul 730109323Ssam /* driver specific 802.11 configuration */ 731109323Ssam if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) 732109323Ssam wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale); 733109323Ssam if (sc->sc_flags & WI_FLAGS_HAS_ROAMING) 734109323Ssam wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode); 735109323Ssam if (sc->sc_flags & WI_FLAGS_HAS_MOR) 736109323Ssam wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven); 737109323Ssam wi_write_txrate(sc); 738109323Ssam wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen); 73946492Swpaul 740109323Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP && 741109323Ssam sc->sc_firmware_type == WI_INTERSIL) { 742109323Ssam wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval); 743109323Ssam wi_write_val(sc, WI_RID_BASIC_RATE, 0x03); /* 1, 2 */ 744109323Ssam wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */ 745109323Ssam wi_write_val(sc, WI_RID_DTIM_PERIOD, 1); 74646492Swpaul } 74746492Swpaul 748109323Ssam /* 749109323Ssam * Initialize promisc mode. 750109323Ssam * Being in the Host-AP mode causes a great 751109323Ssam * deal of pain if primisc mode is set. 752109323Ssam * Therefore we avoid confusing the firmware 753109323Ssam * and always reset promisc mode in Host-AP 754109323Ssam * mode. Host-AP sees all the packets anyway. 755109323Ssam */ 756109323Ssam if (ic->ic_opmode != IEEE80211_M_HOSTAP && 757109323Ssam (ifp->if_flags & IFF_PROMISC) != 0) { 758109323Ssam wi_write_val(sc, WI_RID_PROMISC, 1); 759109323Ssam } else { 760109323Ssam wi_write_val(sc, WI_RID_PROMISC, 0); 76175373Salfred } 76246492Swpaul 763109323Ssam /* Configure WEP. */ 764146884Savatar if (ic->ic_caps & IEEE80211_C_WEP) { 765146884Savatar sc->sc_cnfauthmode = ic->ic_bss->ni_authmode; 766109323Ssam wi_write_wep(sc); 767146884Savatar } 76867092Swpaul 769109323Ssam /* Set multicast filter. */ 770109323Ssam wi_write_multi(sc); 77146492Swpaul 772114124Simp /* Allocate fids for the card */ 773109323Ssam if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) { 774109323Ssam sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame); 775109323Ssam if (sc->sc_firmware_type == WI_SYMBOL) 776109323Ssam sc->sc_buflen = 1585; /* XXX */ 777112363Simp for (i = 0; i < sc->sc_ntxbuf; i++) { 778109323Ssam error = wi_alloc_fid(sc, sc->sc_buflen, 779109323Ssam &sc->sc_txd[i].d_fid); 780109323Ssam if (error) { 781109323Ssam device_printf(sc->sc_dev, 782109323Ssam "tx buffer allocation failed (error %u)\n", 783109323Ssam error); 784109323Ssam goto out; 785109323Ssam } 786109323Ssam sc->sc_txd[i].d_len = 0; 78770073Swpaul } 78870073Swpaul } 789109323Ssam sc->sc_txcur = sc->sc_txnext = 0; 79070073Swpaul 791109323Ssam /* Enable desired port */ 792109323Ssam wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0); 79346492Swpaul 794114124Simp sc->sc_enabled = 1; 795109323Ssam ifp->if_flags |= IFF_RUNNING; 796109323Ssam ifp->if_flags &= ~IFF_OACTIVE; 797109323Ssam if (ic->ic_opmode == IEEE80211_M_AHDEMO || 798139542Ssam ic->ic_opmode == IEEE80211_M_IBSS || 799117855Ssam ic->ic_opmode == IEEE80211_M_MONITOR || 800109323Ssam ic->ic_opmode == IEEE80211_M_HOSTAP) 801139542Ssam ieee80211_create_ibss(ic, ic->ic_ibss_chan); 80246492Swpaul 803109323Ssam /* Enable interrupts */ 804109323Ssam CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 80546492Swpaul 806109323Ssam if (!wasenabled && 807109323Ssam ic->ic_opmode == IEEE80211_M_HOSTAP && 808109323Ssam sc->sc_firmware_type == WI_INTERSIL) { 809109323Ssam /* XXX: some card need to be re-enabled for hostap */ 810109323Ssam wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0); 811109323Ssam wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0); 81275149Simp } 81394397Simp 814109323Ssam if (ic->ic_opmode == IEEE80211_M_STA && 815109323Ssam ((ic->ic_flags & IEEE80211_F_DESBSSID) || 816116951Ssam ic->ic_des_chan != IEEE80211_CHAN_ANYC)) { 817109323Ssam memset(&join, 0, sizeof(join)); 818109323Ssam if (ic->ic_flags & IEEE80211_F_DESBSSID) 819109323Ssam IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid); 820116951Ssam if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) 821116951Ssam join.wi_chan = htole16( 822116951Ssam ieee80211_chan2ieee(ic, ic->ic_des_chan)); 823109323Ssam /* Lucent firmware does not support the JOIN RID. */ 824109323Ssam if (sc->sc_firmware_type != WI_LUCENT) 825109323Ssam wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join)); 82694397Simp } 82775373Salfred 828109323Ssam WI_UNLOCK(sc); 82946492Swpaul return; 830109323Ssamout: 831109323Ssam if (error) { 832109323Ssam if_printf(ifp, "interface not running\n"); 833123098Simp wi_stop(ifp, 1); 83470073Swpaul } 835110224Simp WI_UNLOCK(sc); 836109323Ssam DPRINTF(("wi_init: return %d\n", error)); 837109323Ssam return; 83846492Swpaul} 83946492Swpaul 840109323Ssamvoid 841109323Ssamwi_stop(struct ifnet *ifp, int disable) 84246492Swpaul{ 843109323Ssam struct wi_softc *sc = ifp->if_softc; 844138571Ssam struct ieee80211com *ic = &sc->sc_ic; 845109323Ssam WI_LOCK_DECL(); 84646492Swpaul 847109323Ssam WI_LOCK(sc); 84874998Swpaul 849123098Simp DELAY(100000); 850123098Simp 851117812Ssam ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 852109323Ssam if (sc->sc_enabled && !sc->wi_gone) { 853109323Ssam CSR_WRITE_2(sc, WI_INT_EN, 0); 854109323Ssam wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0); 855109323Ssam if (disable) { 856109323Ssam#ifdef __NetBSD__ 857109323Ssam if (sc->sc_disable) 858109323Ssam (*sc->sc_disable)(sc); 859109323Ssam#endif 860116951Ssam sc->sc_enabled = 0; 86170073Swpaul } 862123098Simp } else if (sc->wi_gone && disable) /* gone --> not enabled */ 863123098Simp sc->sc_enabled = 0; 86470073Swpaul 865109323Ssam sc->sc_tx_timer = 0; 866109323Ssam sc->sc_scan_timer = 0; 867109323Ssam sc->sc_false_syns = 0; 868109323Ssam sc->sc_naps = 0; 869109323Ssam ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING); 870109323Ssam ifp->if_timer = 0; 87146492Swpaul 872109323Ssam WI_UNLOCK(sc); 87346492Swpaul} 87446492Swpaul 875109323Ssamstatic void 876109323Ssamwi_start(struct ifnet *ifp) 87746492Swpaul{ 878109323Ssam struct wi_softc *sc = ifp->if_softc; 879109323Ssam struct ieee80211com *ic = &sc->sc_ic; 880119150Ssam struct ieee80211_node *ni; 881109323Ssam struct ieee80211_frame *wh; 882138571Ssam struct ether_header *eh; 883109323Ssam struct mbuf *m0; 884109323Ssam struct wi_frame frmhdr; 885119150Ssam int cur, fid, off, error; 886109323Ssam WI_LOCK_DECL(); 88746492Swpaul 888109323Ssam WI_LOCK(sc); 88946492Swpaul 890109323Ssam if (sc->wi_gone) { 891109323Ssam WI_UNLOCK(sc); 892109323Ssam return; 89346492Swpaul } 894109323Ssam if (sc->sc_flags & WI_FLAGS_OUTRANGE) { 895109323Ssam WI_UNLOCK(sc); 896109323Ssam return; 89775373Salfred } 89846492Swpaul 899109323Ssam memset(&frmhdr, 0, sizeof(frmhdr)); 900109323Ssam cur = sc->sc_txnext; 901109323Ssam for (;;) { 902109323Ssam IF_POLL(&ic->ic_mgtq, m0); 903109323Ssam if (m0 != NULL) { 904109323Ssam if (sc->sc_txd[cur].d_len != 0) { 905109323Ssam ifp->if_flags |= IFF_OACTIVE; 906109323Ssam break; 907109323Ssam } 908109323Ssam IF_DEQUEUE(&ic->ic_mgtq, m0); 909119150Ssam /* 910119150Ssam * Hack! The referenced node pointer is in the 911119150Ssam * rcvif field of the packet header. This is 912119150Ssam * placed there by ieee80211_mgmt_output because 913119150Ssam * we need to hold the reference with the frame 914119150Ssam * and there's no other way (other than packet 915119150Ssam * tags which we consider too expensive to use) 916119150Ssam * to pass it along. 917119150Ssam */ 918119150Ssam ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif; 919119150Ssam m0->m_pkthdr.rcvif = NULL; 920119150Ssam 921109323Ssam m_copydata(m0, 4, ETHER_ADDR_LEN * 2, 922109323Ssam (caddr_t)&frmhdr.wi_ehdr); 923109323Ssam frmhdr.wi_ehdr.ether_type = 0; 924109323Ssam wh = mtod(m0, struct ieee80211_frame *); 925109323Ssam } else { 926109323Ssam if (ic->ic_state != IEEE80211_S_RUN) 927109323Ssam break; 928132986Smlaier IFQ_DRV_DEQUEUE(&ifp->if_snd, m0); 929109323Ssam if (m0 == NULL) 930109323Ssam break; 931109323Ssam if (sc->sc_txd[cur].d_len != 0) { 932132986Smlaier IFQ_DRV_PREPEND(&ifp->if_snd, m0); 933109323Ssam ifp->if_flags |= IFF_OACTIVE; 934109323Ssam break; 935109323Ssam } 936138571Ssam if (m0->m_len < sizeof(struct ether_header) && 937138571Ssam (m0 = m_pullup(m0, sizeof(struct ether_header))) == NULL) { 938138571Ssam ifp->if_oerrors++; 939138571Ssam continue; 940138571Ssam } 941138571Ssam eh = mtod(m0, struct ether_header *); 942138571Ssam ni = ieee80211_find_txnode(ic, eh->ether_dhost); 943138571Ssam if (ni == NULL) { 944138571Ssam m_freem(m0); 945138571Ssam continue; 946138571Ssam } 947109323Ssam ifp->if_opackets++; 948109323Ssam m_copydata(m0, 0, ETHER_HDR_LEN, 949109323Ssam (caddr_t)&frmhdr.wi_ehdr); 950109323Ssam#if NBPFILTER > 0 951109323Ssam BPF_MTAP(ifp, m0); 952109323Ssam#endif 95346492Swpaul 954138571Ssam m0 = ieee80211_encap(ic, m0, ni); 955119150Ssam if (m0 == NULL) { 956109323Ssam ifp->if_oerrors++; 957138571Ssam ieee80211_free_node(ni); 958109323Ssam continue; 959109323Ssam } 960109323Ssam wh = mtod(m0, struct ieee80211_frame *); 961109323Ssam } 962109323Ssam#if NBPFILTER > 0 963109323Ssam if (ic->ic_rawbpf) 964109323Ssam bpf_mtap(ic->ic_rawbpf, m0); 965109323Ssam#endif 966109323Ssam frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX); 967138952Ssam /* XXX check key for SWCRYPT instead of using operating mode */ 968138952Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP && 969138952Ssam (wh->i_fc[1] & IEEE80211_FC1_WEP)) { 970138571Ssam struct ieee80211_key *k; 971138571Ssam 972138571Ssam k = ieee80211_crypto_encap(ic, ni, m0); 973138571Ssam if (k == NULL) { 974138571Ssam if (ni != NULL) 975138571Ssam ieee80211_free_node(ni); 976143299Ssam m_freem(m0); 977109323Ssam continue; 978109323Ssam } 979138952Ssam frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT); 980109323Ssam } 981109323Ssam#if NBPFILTER > 0 982109323Ssam if (sc->sc_drvbpf) { 983123927Ssam sc->sc_tx_th.wt_rate = 984123927Ssam ni->ni_rates.rs_rates[ni->ni_txrate]; 985123922Ssam bpf_mtap2(sc->sc_drvbpf, 986127698Ssam &sc->sc_tx_th, sc->sc_tx_th_len, m0); 987109323Ssam } 98846492Swpaul#endif 989127697Ssam m_copydata(m0, 0, sizeof(struct ieee80211_frame), 990127697Ssam (caddr_t)&frmhdr.wi_whdr); 991127697Ssam m_adj(m0, sizeof(struct ieee80211_frame)); 992127697Ssam frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len); 993109323Ssam if (IFF_DUMPPKTS(ifp)) 994116951Ssam wi_dump_pkt(&frmhdr, NULL, -1); 995109323Ssam fid = sc->sc_txd[cur].d_fid; 996109323Ssam off = sizeof(frmhdr); 997119150Ssam error = wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0 998119150Ssam || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0; 999119150Ssam m_freem(m0); 1000138571Ssam if (ni != NULL) 1001138571Ssam ieee80211_free_node(ni); 1002119150Ssam if (error) { 1003109323Ssam ifp->if_oerrors++; 1004109323Ssam continue; 1005109323Ssam } 1006109323Ssam sc->sc_txd[cur].d_len = off; 1007109323Ssam if (sc->sc_txcur == cur) { 1008109323Ssam if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) { 1009109323Ssam if_printf(ifp, "xmit failed\n"); 1010109323Ssam sc->sc_txd[cur].d_len = 0; 1011109323Ssam continue; 1012109323Ssam } 1013109323Ssam sc->sc_tx_timer = 5; 1014109323Ssam ifp->if_timer = 1; 1015109323Ssam } 1016112363Simp sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf; 101774838Salfred } 101846492Swpaul 1019109323Ssam WI_UNLOCK(sc); 102046492Swpaul} 102146492Swpaul 102288546Salfredstatic int 1023109323Ssamwi_reset(struct wi_softc *sc) 102446492Swpaul{ 1025147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1026114124Simp#define WI_INIT_TRIES 3 1027114124Simp int i; 1028114138Simp int error = 0; 1029114124Simp int tries; 1030114124Simp 1031114124Simp /* Symbol firmware cannot be initialized more than once */ 1032123098Simp if (sc->sc_firmware_type == WI_SYMBOL && sc->sc_reset) 1033112362Simp return (0); 1034114124Simp if (sc->sc_firmware_type == WI_SYMBOL) 1035114124Simp tries = 1; 1036114124Simp else 1037114124Simp tries = WI_INIT_TRIES; 1038112362Simp 1039114124Simp for (i = 0; i < tries; i++) { 1040109323Ssam if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0) 104146492Swpaul break; 1042109323Ssam DELAY(WI_DELAY * 1000); 104346492Swpaul } 1044114124Simp sc->sc_reset = 1; 104546492Swpaul 1046114124Simp if (i == tries) { 1047114124Simp if_printf(ifp, "init failed\n"); 1048114124Simp return (error); 104975373Salfred } 105046492Swpaul 1051109323Ssam CSR_WRITE_2(sc, WI_INT_EN, 0); 1052114124Simp CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF); 105346492Swpaul 1054109323Ssam /* Calibrate timer. */ 1055114124Simp wi_write_val(sc, WI_RID_TICK_TIME, 8); 1056114124Simp 1057114124Simp return (0); 1058109323Ssam#undef WI_INIT_TRIES 105946492Swpaul} 106046492Swpaul 106188546Salfredstatic void 1062109323Ssamwi_watchdog(struct ifnet *ifp) 106346492Swpaul{ 1064109323Ssam struct wi_softc *sc = ifp->if_softc; 106546492Swpaul 1066109323Ssam ifp->if_timer = 0; 1067109323Ssam if (!sc->sc_enabled) 1068109323Ssam return; 106946492Swpaul 1070109323Ssam if (sc->sc_tx_timer) { 1071109323Ssam if (--sc->sc_tx_timer == 0) { 1072109323Ssam if_printf(ifp, "device timeout\n"); 1073109323Ssam ifp->if_oerrors++; 1074109323Ssam wi_init(ifp->if_softc); 1075109323Ssam return; 1076109323Ssam } 1077109323Ssam ifp->if_timer = 1; 107846492Swpaul } 107946492Swpaul 1080109323Ssam if (sc->sc_scan_timer) { 1081109323Ssam if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT && 1082109323Ssam sc->sc_firmware_type == WI_INTERSIL) { 1083109323Ssam DPRINTF(("wi_watchdog: inquire scan\n")); 1084109323Ssam wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0); 108546492Swpaul } 1086109323Ssam if (sc->sc_scan_timer) 1087109323Ssam ifp->if_timer = 1; 108846492Swpaul } 108946492Swpaul 1090109323Ssam /* TODO: rate control */ 1091138571Ssam ieee80211_watchdog(&sc->sc_ic); 109246492Swpaul} 109346492Swpaul 109488546Salfredstatic int 1095109323Ssamwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 109646492Swpaul{ 1097109323Ssam struct wi_softc *sc = ifp->if_softc; 1098109323Ssam struct ieee80211com *ic = &sc->sc_ic; 1099109323Ssam struct ifreq *ifr = (struct ifreq *)data; 1100109323Ssam struct ieee80211req *ireq; 1101109323Ssam u_int8_t nodename[IEEE80211_NWID_LEN]; 1102109323Ssam int error = 0; 110395534Simp#if __FreeBSD_version >= 500000 1104109323Ssam struct thread *td = curthread; 110595534Simp#else 1106109323Ssam struct proc *td = curproc; /* Little white lie */ 110795534Simp#endif 1108109323Ssam struct wi_req wreq; 1109109323Ssam WI_LOCK_DECL(); 111046492Swpaul 1111138571Ssam if (sc->wi_gone) 1112138571Ssam return (ENODEV); 111346492Swpaul 1114109323Ssam switch (cmd) { 111546492Swpaul case SIOCSIFFLAGS: 1116100876Simp /* 1117101139Simp * Can't do promisc and hostap at the same time. If all that's 1118101139Simp * changing is the promisc flag, try to short-circuit a call to 1119101139Simp * wi_init() by just setting PROMISC in the hardware. 1120100876Simp */ 1121138571Ssam WI_LOCK(sc); 112246492Swpaul if (ifp->if_flags & IFF_UP) { 1123109323Ssam if (ic->ic_opmode != IEEE80211_M_HOSTAP && 1124101139Simp ifp->if_flags & IFF_RUNNING) { 1125101139Simp if (ifp->if_flags & IFF_PROMISC && 1126109323Ssam !(sc->sc_if_flags & IFF_PROMISC)) { 1127109323Ssam wi_write_val(sc, WI_RID_PROMISC, 1); 1128101139Simp } else if (!(ifp->if_flags & IFF_PROMISC) && 1129109323Ssam sc->sc_if_flags & IFF_PROMISC) { 1130109323Ssam wi_write_val(sc, WI_RID_PROMISC, 0); 1131101139Simp } else { 1132101139Simp wi_init(sc); 1133101139Simp } 1134100876Simp } else { 113546492Swpaul wi_init(sc); 1136100876Simp } 113746492Swpaul } else { 113846492Swpaul if (ifp->if_flags & IFF_RUNNING) { 1139123098Simp wi_stop(ifp, 1); 114046492Swpaul } 1141123098Simp sc->wi_gone = 0; 114246492Swpaul } 1143109323Ssam sc->sc_if_flags = ifp->if_flags; 1144138571Ssam WI_UNLOCK(sc); 114546492Swpaul error = 0; 114646492Swpaul break; 114746492Swpaul case SIOCADDMULTI: 114846492Swpaul case SIOCDELMULTI: 1149138571Ssam WI_LOCK(sc); 1150109323Ssam error = wi_write_multi(sc); 1151138571Ssam WI_UNLOCK(sc); 115246492Swpaul break; 1153109323Ssam case SIOCGIFGENERIC: 1154138571Ssam WI_LOCK(sc); 1155109323Ssam error = wi_get_cfg(ifp, cmd, data); 1156138571Ssam WI_UNLOCK(sc); 115746492Swpaul break; 1158109323Ssam case SIOCSIFGENERIC: 1159109323Ssam error = suser(td); 1160138571Ssam if (error == 0) 1161138571Ssam error = wi_set_cfg(ifp, cmd, data); 116246492Swpaul break; 116393359Simp case SIOCGPRISM2DEBUG: 116493359Simp error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 116593359Simp if (error) 116693359Simp break; 116793733Simp if (!(ifp->if_flags & IFF_RUNNING) || 116893733Simp sc->sc_firmware_type == WI_LUCENT) { 116993359Simp error = EIO; 117093359Simp break; 117193359Simp } 117293359Simp error = wi_get_debug(sc, &wreq); 117393359Simp if (error == 0) 117493359Simp error = copyout(&wreq, ifr->ifr_data, sizeof(wreq)); 117593359Simp break; 117693359Simp case SIOCSPRISM2DEBUG: 117793593Sjhb if ((error = suser(td))) 1178138571Ssam return (error); 117993359Simp error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 118093359Simp if (error) 118193359Simp break; 1182138571Ssam WI_LOCK(sc); 118393359Simp error = wi_set_debug(sc, &wreq); 1184138571Ssam WI_UNLOCK(sc); 118593359Simp break; 118677217Sphk case SIOCG80211: 1187109323Ssam ireq = (struct ieee80211req *) data; 1188138571Ssam if (ireq->i_type == IEEE80211_IOC_STATIONNAME) { 1189109323Ssam ireq->i_len = sc->sc_nodelen + 1; 1190109323Ssam error = copyout(sc->sc_nodename, ireq->i_data, 1191109323Ssam ireq->i_len); 119277217Sphk break; 119377217Sphk } 1194138571Ssam goto ioctl_common; 119577217Sphk case SIOCS80211: 1196109323Ssam ireq = (struct ieee80211req *) data; 1197138571Ssam if (ireq->i_type == IEEE80211_IOC_STATIONNAME) { 1198138571Ssam error = suser(td); 1199138571Ssam if (error) 1200138571Ssam break; 120177217Sphk if (ireq->i_val != 0 || 120277217Sphk ireq->i_len > IEEE80211_NWID_LEN) { 120377217Sphk error = EINVAL; 120477217Sphk break; 120577217Sphk } 1206109323Ssam memset(nodename, 0, IEEE80211_NWID_LEN); 1207109323Ssam error = copyin(ireq->i_data, nodename, ireq->i_len); 1208109323Ssam if (error) 120977217Sphk break; 1210138571Ssam WI_LOCK(sc); 1211109323Ssam if (sc->sc_enabled) { 1212109323Ssam error = wi_write_ssid(sc, WI_RID_NODENAME, 1213109323Ssam nodename, ireq->i_len); 121477217Sphk } 1215138571Ssam if (error == 0) { 1216138571Ssam memcpy(sc->sc_nodename, nodename, 1217138571Ssam IEEE80211_NWID_LEN); 1218138571Ssam sc->sc_nodelen = ireq->i_len; 1219138571Ssam } 1220138571Ssam WI_UNLOCK(sc); 122177217Sphk break; 122277217Sphk } 1223138571Ssam goto ioctl_common; 122446492Swpaul default: 1225138571Ssam ioctl_common: 1226138571Ssam WI_LOCK(sc); 1227138571Ssam error = ieee80211_ioctl(ic, cmd, data); 1228138571Ssam if (error == ENETRESET) { 1229138571Ssam if (sc->sc_enabled) 1230138571Ssam wi_init(sc); /* XXX no error return */ 1231138571Ssam error = 0; 1232138571Ssam } 1233138571Ssam WI_UNLOCK(sc); 123446492Swpaul break; 123546492Swpaul } 1236109323Ssam return (error); 123746492Swpaul} 123846492Swpaul 1239109323Ssamstatic int 1240109323Ssamwi_media_change(struct ifnet *ifp) 124146492Swpaul{ 1242109323Ssam struct wi_softc *sc = ifp->if_softc; 1243116951Ssam int error; 124446492Swpaul 1245116951Ssam error = ieee80211_media_change(ifp); 1246109323Ssam if (error == ENETRESET) { 1247109323Ssam if (sc->sc_enabled) 1248116951Ssam wi_init(sc); /* XXX no error return */ 1249109323Ssam error = 0; 1250109323Ssam } 1251109323Ssam return error; 1252109323Ssam} 125346492Swpaul 1254109323Ssamstatic void 1255109323Ssamwi_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1256109323Ssam{ 1257109323Ssam struct wi_softc *sc = ifp->if_softc; 1258109323Ssam struct ieee80211com *ic = &sc->sc_ic; 1259109323Ssam u_int16_t val; 1260109323Ssam int rate, len; 126146492Swpaul 1262109323Ssam if (sc->wi_gone || !sc->sc_enabled) { 1263109323Ssam imr->ifm_active = IFM_IEEE80211 | IFM_NONE; 1264109323Ssam imr->ifm_status = 0; 1265109323Ssam return; 1266109323Ssam } 126746492Swpaul 1268109323Ssam imr->ifm_status = IFM_AVALID; 1269109323Ssam imr->ifm_active = IFM_IEEE80211; 1270109323Ssam if (ic->ic_state == IEEE80211_S_RUN && 1271109323Ssam (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0) 1272109323Ssam imr->ifm_status |= IFM_ACTIVE; 1273109323Ssam len = sizeof(val); 1274138571Ssam if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) == 0 && 1275138571Ssam len == sizeof(val)) { 1276109323Ssam /* convert to 802.11 rate */ 1277138571Ssam val = le16toh(val); 1278109323Ssam rate = val * 2; 1279109323Ssam if (sc->sc_firmware_type == WI_LUCENT) { 1280138571Ssam if (rate == 10) 1281109323Ssam rate = 11; /* 5.5Mbps */ 1282109323Ssam } else { 1283109323Ssam if (rate == 4*2) 1284109323Ssam rate = 11; /* 5.5Mbps */ 1285109323Ssam else if (rate == 8*2) 1286109323Ssam rate = 22; /* 11Mbps */ 1287109323Ssam } 1288138571Ssam } else 1289138571Ssam rate = 0; 1290116951Ssam imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B); 1291109323Ssam switch (ic->ic_opmode) { 1292109323Ssam case IEEE80211_M_STA: 1293109323Ssam break; 1294109323Ssam case IEEE80211_M_IBSS: 1295109323Ssam imr->ifm_active |= IFM_IEEE80211_ADHOC; 1296109323Ssam break; 1297109323Ssam case IEEE80211_M_AHDEMO: 1298109323Ssam imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0; 1299109323Ssam break; 1300109323Ssam case IEEE80211_M_HOSTAP: 1301109323Ssam imr->ifm_active |= IFM_IEEE80211_HOSTAP; 1302109323Ssam break; 1303117855Ssam case IEEE80211_M_MONITOR: 1304117855Ssam imr->ifm_active |= IFM_IEEE80211_MONITOR; 1305117855Ssam break; 1306109323Ssam } 1307109323Ssam} 130846492Swpaul 1309109323Ssamstatic void 1310109323Ssamwi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN]) 1311109323Ssam{ 1312109323Ssam struct ieee80211com *ic = &sc->sc_ic; 1313116951Ssam struct ieee80211_node *ni = ic->ic_bss; 1314147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 131598440Simp 1316109323Ssam if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid)) 1317109323Ssam return; 131846492Swpaul 1319109323Ssam DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid))); 1320109323Ssam DPRINTF(("%s ?\n", ether_sprintf(new_bssid))); 132146492Swpaul 1322109323Ssam /* In promiscuous mode, the BSSID field is not a reliable 1323109323Ssam * indicator of the firmware's BSSID. Damp spurious 1324109323Ssam * change-of-BSSID indications. 1325109323Ssam */ 1326109323Ssam if ((ifp->if_flags & IFF_PROMISC) != 0 && 1327138571Ssam !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns, 1328138571Ssam WI_MAX_FALSE_SYNS)) 1329109323Ssam return; 133046492Swpaul 1331139542Ssam sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1); 1332139542Ssam /* 1333139542Ssam * XXX hack; we should create a new node with the new bssid 1334139542Ssam * and replace the existing ic_bss with it but since we don't 1335139542Ssam * process management frames to collect state we cheat by 1336139542Ssam * reusing the existing node as we know wi_newstate will be 1337139542Ssam * called and it will overwrite the node state. 1338139542Ssam */ 1339139542Ssam ieee80211_sta_join(ic, ieee80211_ref_node(ni)); 1340109323Ssam} 134146492Swpaul 1342109323Ssamstatic void 1343116898Ssamwi_rx_monitor(struct wi_softc *sc, int fid) 1344116898Ssam{ 1345147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1346116898Ssam struct wi_frame *rx_frame; 1347116898Ssam struct mbuf *m; 1348116898Ssam int datlen, hdrlen; 1349116898Ssam 1350116898Ssam /* first allocate mbuf for packet storage */ 1351116898Ssam m = m_getcl(M_DONTWAIT, MT_DATA, 0); 1352116898Ssam if (m == NULL) { 1353116898Ssam ifp->if_ierrors++; 1354116898Ssam return; 1355116898Ssam } 1356116898Ssam 1357116898Ssam m->m_pkthdr.rcvif = ifp; 1358116898Ssam 1359116898Ssam /* now read wi_frame first so we know how much data to read */ 1360116898Ssam if (wi_read_bap(sc, fid, 0, mtod(m, caddr_t), sizeof(*rx_frame))) { 1361116898Ssam ifp->if_ierrors++; 1362116898Ssam goto done; 1363116898Ssam } 1364116898Ssam 1365116898Ssam rx_frame = mtod(m, struct wi_frame *); 1366116898Ssam 1367116898Ssam switch ((rx_frame->wi_status & WI_STAT_MAC_PORT) >> 8) { 1368116898Ssam case 7: 1369116898Ssam switch (rx_frame->wi_whdr.i_fc[0] & IEEE80211_FC0_TYPE_MASK) { 1370116898Ssam case IEEE80211_FC0_TYPE_DATA: 1371116898Ssam hdrlen = WI_DATA_HDRLEN; 1372116898Ssam datlen = rx_frame->wi_dat_len + WI_FCS_LEN; 1373116898Ssam break; 1374116898Ssam case IEEE80211_FC0_TYPE_MGT: 1375116898Ssam hdrlen = WI_MGMT_HDRLEN; 1376116898Ssam datlen = rx_frame->wi_dat_len + WI_FCS_LEN; 1377116898Ssam break; 1378116898Ssam case IEEE80211_FC0_TYPE_CTL: 1379116898Ssam /* 1380116898Ssam * prism2 cards don't pass control packets 1381116898Ssam * down properly or consistently, so we'll only 1382116898Ssam * pass down the header. 1383116898Ssam */ 1384116898Ssam hdrlen = WI_CTL_HDRLEN; 1385116898Ssam datlen = 0; 1386116898Ssam break; 1387116898Ssam default: 1388116898Ssam if_printf(ifp, "received packet of unknown type " 1389116898Ssam "on port 7\n"); 1390116898Ssam ifp->if_ierrors++; 1391116898Ssam goto done; 1392116898Ssam } 1393116898Ssam break; 1394116898Ssam case 0: 1395116898Ssam hdrlen = WI_DATA_HDRLEN; 1396116898Ssam datlen = rx_frame->wi_dat_len + WI_FCS_LEN; 1397116898Ssam break; 1398116898Ssam default: 1399116898Ssam if_printf(ifp, "received packet on invalid " 1400116898Ssam "port (wi_status=0x%x)\n", rx_frame->wi_status); 1401116898Ssam ifp->if_ierrors++; 1402116898Ssam goto done; 1403116898Ssam } 1404116898Ssam 1405116898Ssam if (hdrlen + datlen + 2 > MCLBYTES) { 1406116898Ssam if_printf(ifp, "oversized packet received " 1407116898Ssam "(wi_dat_len=%d, wi_status=0x%x)\n", 1408116898Ssam datlen, rx_frame->wi_status); 1409116898Ssam ifp->if_ierrors++; 1410116898Ssam goto done; 1411116898Ssam } 1412116898Ssam 1413116898Ssam if (wi_read_bap(sc, fid, hdrlen, mtod(m, caddr_t) + hdrlen, 1414116898Ssam datlen + 2) == 0) { 1415116898Ssam m->m_pkthdr.len = m->m_len = hdrlen + datlen; 1416116898Ssam ifp->if_ipackets++; 1417116898Ssam BPF_MTAP(ifp, m); /* Handle BPF listeners. */ 1418116898Ssam } else 1419116898Ssam ifp->if_ierrors++; 1420116898Ssamdone: 1421116898Ssam m_freem(m); 1422116898Ssam} 1423116898Ssam 1424116898Ssamstatic void 1425109323Ssamwi_rx_intr(struct wi_softc *sc) 1426109323Ssam{ 1427109323Ssam struct ieee80211com *ic = &sc->sc_ic; 1428147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1429109323Ssam struct wi_frame frmhdr; 1430109323Ssam struct mbuf *m; 1431109323Ssam struct ieee80211_frame *wh; 1432119150Ssam struct ieee80211_node *ni; 1433109323Ssam int fid, len, off, rssi; 1434109323Ssam u_int8_t dir; 1435109323Ssam u_int16_t status; 1436109323Ssam u_int32_t rstamp; 143746611Swpaul 1438109323Ssam fid = CSR_READ_2(sc, WI_RX_FID); 143946611Swpaul 1440116898Ssam if (sc->wi_debug.wi_monitor) { 1441116898Ssam /* 1442116898Ssam * If we are in monitor mode just 1443116898Ssam * read the data from the device. 1444116898Ssam */ 1445116898Ssam wi_rx_monitor(sc, fid); 1446116898Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1447116898Ssam return; 1448116898Ssam } 1449116898Ssam 1450109323Ssam /* First read in the frame header */ 1451109323Ssam if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) { 1452109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1453109323Ssam ifp->if_ierrors++; 1454109323Ssam DPRINTF(("wi_rx_intr: read fid %x failed\n", fid)); 1455109323Ssam return; 1456109323Ssam } 145791695Simp 1458109323Ssam if (IFF_DUMPPKTS(ifp)) 1459109323Ssam wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal); 146046492Swpaul 1461109323Ssam /* 1462109323Ssam * Drop undecryptable or packets with receive errors here 1463109323Ssam */ 1464109323Ssam status = le16toh(frmhdr.wi_status); 1465109323Ssam if (status & WI_STAT_ERRSTAT) { 1466109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1467109323Ssam ifp->if_ierrors++; 1468109323Ssam DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status)); 1469109323Ssam return; 1470109323Ssam } 1471109323Ssam rssi = frmhdr.wi_rx_signal; 1472109323Ssam rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) | 1473109323Ssam le16toh(frmhdr.wi_rx_tstamp1); 147446492Swpaul 1475109323Ssam len = le16toh(frmhdr.wi_dat_len); 1476109323Ssam off = ALIGN(sizeof(struct ieee80211_frame)); 147746563Swpaul 1478117855Ssam /* 1479117855Ssam * Sometimes the PRISM2.x returns bogusly large frames. Except 1480117855Ssam * in monitor mode, just throw them away. 1481117855Ssam */ 1482117855Ssam if (off + len > MCLBYTES) { 1483117855Ssam if (ic->ic_opmode != IEEE80211_M_MONITOR) { 1484117855Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1485117855Ssam ifp->if_ierrors++; 1486117855Ssam DPRINTF(("wi_rx_intr: oversized packet\n")); 1487117855Ssam return; 1488117855Ssam } else 1489117855Ssam len = 0; 1490117855Ssam } 1491117855Ssam 1492111119Simp MGETHDR(m, M_DONTWAIT, MT_DATA); 1493109323Ssam if (m == NULL) { 1494109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1495109323Ssam ifp->if_ierrors++; 1496109323Ssam DPRINTF(("wi_rx_intr: MGET failed\n")); 1497109323Ssam return; 1498109323Ssam } 1499109323Ssam if (off + len > MHLEN) { 1500111119Simp MCLGET(m, M_DONTWAIT); 1501109323Ssam if ((m->m_flags & M_EXT) == 0) { 1502109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1503109323Ssam m_freem(m); 1504109323Ssam ifp->if_ierrors++; 1505109323Ssam DPRINTF(("wi_rx_intr: MCLGET failed\n")); 1506109323Ssam return; 1507109323Ssam } 1508109323Ssam } 150946492Swpaul 1510109323Ssam m->m_data += off - sizeof(struct ieee80211_frame); 1511109323Ssam memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame)); 1512109323Ssam wi_read_bap(sc, fid, sizeof(frmhdr), 1513109323Ssam m->m_data + sizeof(struct ieee80211_frame), len); 1514109323Ssam m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len; 1515109323Ssam m->m_pkthdr.rcvif = ifp; 151694405Simp 1517109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 151846492Swpaul 1519138952Ssam wh = mtod(m, struct ieee80211_frame *); 1520138952Ssam if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1521138952Ssam /* 1522138952Ssam * WEP is decrypted by hardware and the IV 1523138952Ssam * is stripped. Clear WEP bit so we don't 1524138952Ssam * try to process it in ieee80211_input. 1525138952Ssam * XXX fix for TKIP, et. al. 1526138952Ssam */ 1527138952Ssam wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 1528138952Ssam } 1529138952Ssam 1530109323Ssam#if NBPFILTER > 0 1531109323Ssam if (sc->sc_drvbpf) { 1532123927Ssam /* XXX replace divide by table */ 1533123922Ssam sc->sc_rx_th.wr_rate = frmhdr.wi_rx_rate / 5; 1534127148Sgreen sc->sc_rx_th.wr_antsignal = frmhdr.wi_rx_signal; 1535127148Sgreen sc->sc_rx_th.wr_antnoise = frmhdr.wi_rx_silence; 1536123927Ssam sc->sc_rx_th.wr_flags = 0; 1537123927Ssam if (frmhdr.wi_status & WI_STAT_PCF) 1538123927Ssam sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_CFP; 1539138952Ssam /* XXX IEEE80211_RADIOTAP_F_WEP */ 1540123922Ssam bpf_mtap2(sc->sc_drvbpf, 1541127698Ssam &sc->sc_rx_th, sc->sc_rx_th_len, m); 154256965Swpaul } 1543109323Ssam#endif 154456965Swpaul 1545109323Ssam /* synchronize driver's BSSID with firmware's BSSID */ 1546109323Ssam dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 1547109323Ssam if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS) 1548109323Ssam wi_sync_bssid(sc, wh->i_addr3); 154946492Swpaul 1550119150Ssam /* 1551119150Ssam * Locate the node for sender, track state, and 1552119150Ssam * then pass this node (referenced) up to the 802.11 1553138571Ssam * layer for its use. 1554119150Ssam */ 1555138571Ssam ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *) wh); 1556119150Ssam /* 1557119150Ssam * Send frame up for processing. 1558119150Ssam */ 1559138571Ssam ieee80211_input(ic, m, ni, rssi, rstamp); 1560119150Ssam /* 1561119150Ssam * The frame may have caused the node to be marked for 1562119150Ssam * reclamation (e.g. in response to a DEAUTH message) 1563119150Ssam * so use free_node here instead of unref_node. 1564119150Ssam */ 1565138571Ssam ieee80211_free_node(ni); 1566109323Ssam} 156746492Swpaul 1568109323Ssamstatic void 1569109323Ssamwi_tx_ex_intr(struct wi_softc *sc) 1570109323Ssam{ 1571147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1572109323Ssam struct wi_frame frmhdr; 1573109323Ssam int fid; 157446492Swpaul 1575109323Ssam fid = CSR_READ_2(sc, WI_TX_CMP_FID); 1576109323Ssam /* Read in the frame header */ 1577109323Ssam if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) { 1578109323Ssam u_int16_t status = le16toh(frmhdr.wi_status); 157946492Swpaul 1580109323Ssam /* 1581109323Ssam * Spontaneous station disconnects appear as xmit 1582109323Ssam * errors. Don't announce them and/or count them 1583109323Ssam * as an output error. 1584109323Ssam */ 1585109323Ssam if ((status & WI_TXSTAT_DISCONNECT) == 0) { 1586109323Ssam if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) { 1587109323Ssam if_printf(ifp, "tx failed"); 1588109323Ssam if (status & WI_TXSTAT_RET_ERR) 1589109323Ssam printf(", retry limit exceeded"); 1590109323Ssam if (status & WI_TXSTAT_AGED_ERR) 1591109323Ssam printf(", max transmit lifetime exceeded"); 1592109323Ssam if (status & WI_TXSTAT_DISCONNECT) 1593109323Ssam printf(", port disconnected"); 1594109323Ssam if (status & WI_TXSTAT_FORM_ERR) 1595109323Ssam printf(", invalid format (data len %u src %6D)", 1596109323Ssam le16toh(frmhdr.wi_dat_len), 1597109323Ssam frmhdr.wi_ehdr.ether_shost, ":"); 1598109323Ssam if (status & ~0xf) 1599109323Ssam printf(", status=0x%x", status); 1600109323Ssam printf("\n"); 1601109323Ssam } 1602109323Ssam ifp->if_oerrors++; 1603109323Ssam } else { 1604109323Ssam DPRINTF(("port disconnected\n")); 1605109323Ssam ifp->if_collisions++; /* XXX */ 1606109323Ssam } 1607109323Ssam } else 1608109323Ssam DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid)); 1609109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC); 1610109323Ssam} 161146492Swpaul 1612109323Ssamstatic void 1613109323Ssamwi_tx_intr(struct wi_softc *sc) 1614109323Ssam{ 1615147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1616109323Ssam int fid, cur; 161794405Simp 1618123098Simp if (sc->wi_gone) 1619123098Simp return; 1620123098Simp 1621109323Ssam fid = CSR_READ_2(sc, WI_ALLOC_FID); 1622109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC); 162346492Swpaul 1624109323Ssam cur = sc->sc_txcur; 1625109323Ssam if (sc->sc_txd[cur].d_fid != fid) { 1626109323Ssam if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n", 1627109323Ssam fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext); 1628109323Ssam return; 1629109323Ssam } 1630109323Ssam sc->sc_tx_timer = 0; 1631109323Ssam sc->sc_txd[cur].d_len = 0; 1632112363Simp sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf; 1633109323Ssam if (sc->sc_txd[cur].d_len == 0) 1634109323Ssam ifp->if_flags &= ~IFF_OACTIVE; 1635109323Ssam else { 1636109323Ssam if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid, 1637109323Ssam 0, 0)) { 1638109323Ssam if_printf(ifp, "xmit failed\n"); 1639109323Ssam sc->sc_txd[cur].d_len = 0; 1640109323Ssam } else { 1641109323Ssam sc->sc_tx_timer = 5; 1642109323Ssam ifp->if_timer = 1; 1643109323Ssam } 1644109323Ssam } 164546492Swpaul} 164646492Swpaul 164788546Salfredstatic void 1648109323Ssamwi_info_intr(struct wi_softc *sc) 164994405Simp{ 1650109323Ssam struct ieee80211com *ic = &sc->sc_ic; 1651147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1652109323Ssam int i, fid, len, off; 1653109323Ssam u_int16_t ltbuf[2]; 1654109323Ssam u_int16_t stat; 1655109323Ssam u_int32_t *ptr; 165694405Simp 1657109323Ssam fid = CSR_READ_2(sc, WI_INFO_FID); 1658109323Ssam wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf)); 165994405Simp 1660109323Ssam switch (le16toh(ltbuf[1])) { 166194405Simp 1662109323Ssam case WI_INFO_LINK_STAT: 1663109323Ssam wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat)); 1664109323Ssam DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat))); 1665109323Ssam switch (le16toh(stat)) { 1666109323Ssam case WI_INFO_LINK_STAT_CONNECTED: 1667109323Ssam sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 1668109323Ssam if (ic->ic_state == IEEE80211_S_RUN && 1669109323Ssam ic->ic_opmode != IEEE80211_M_IBSS) 1670109323Ssam break; 1671109323Ssam /* FALLTHROUGH */ 1672109323Ssam case WI_INFO_LINK_STAT_AP_CHG: 1673117812Ssam ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1674109323Ssam break; 1675109323Ssam case WI_INFO_LINK_STAT_AP_INR: 1676109323Ssam sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 1677109323Ssam break; 1678109323Ssam case WI_INFO_LINK_STAT_AP_OOR: 1679109323Ssam if (sc->sc_firmware_type == WI_SYMBOL && 1680109323Ssam sc->sc_scan_timer > 0) { 1681109323Ssam if (wi_cmd(sc, WI_CMD_INQUIRE, 1682109323Ssam WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0) 1683109323Ssam sc->sc_scan_timer = 0; 1684109323Ssam break; 1685109323Ssam } 1686109323Ssam if (ic->ic_opmode == IEEE80211_M_STA) 1687109323Ssam sc->sc_flags |= WI_FLAGS_OUTRANGE; 1688109323Ssam break; 1689109323Ssam case WI_INFO_LINK_STAT_DISCONNECTED: 1690109323Ssam case WI_INFO_LINK_STAT_ASSOC_FAILED: 1691109323Ssam if (ic->ic_opmode == IEEE80211_M_STA) 1692117812Ssam ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 1693109323Ssam break; 1694109323Ssam } 1695109323Ssam break; 169694405Simp 1697109323Ssam case WI_INFO_COUNTERS: 1698109323Ssam /* some card versions have a larger stats structure */ 1699109323Ssam len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4); 1700109323Ssam ptr = (u_int32_t *)&sc->sc_stats; 1701109323Ssam off = sizeof(ltbuf); 1702109323Ssam for (i = 0; i < len; i++, off += 2, ptr++) { 1703109323Ssam wi_read_bap(sc, fid, off, &stat, sizeof(stat)); 1704109323Ssam#ifdef WI_HERMES_STATS_WAR 1705109323Ssam if (stat & 0xf000) 1706109323Ssam stat = ~stat; 1707109323Ssam#endif 1708109323Ssam *ptr += stat; 1709109323Ssam } 1710109323Ssam ifp->if_collisions = sc->sc_stats.wi_tx_single_retries + 1711109323Ssam sc->sc_stats.wi_tx_multi_retries + 1712109323Ssam sc->sc_stats.wi_tx_retry_limit; 1713109323Ssam break; 1714109323Ssam 1715109323Ssam case WI_INFO_SCAN_RESULTS: 1716109323Ssam case WI_INFO_HOST_SCAN_RESULTS: 1717109323Ssam wi_scan_result(sc, fid, le16toh(ltbuf[0])); 1718109323Ssam break; 1719109323Ssam 1720109323Ssam default: 1721109323Ssam DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid, 1722109323Ssam le16toh(ltbuf[1]), le16toh(ltbuf[0]))); 1723109323Ssam break; 172494405Simp } 1725109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO); 1726109323Ssam} 172794405Simp 1728109323Ssamstatic int 1729109323Ssamwi_write_multi(struct wi_softc *sc) 1730109323Ssam{ 1731147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1732109323Ssam int n; 1733109323Ssam struct ifmultiaddr *ifma; 1734109323Ssam struct wi_mcast mlist; 173594472Simp 1736109323Ssam if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 1737109323Ssamallmulti: 1738109323Ssam memset(&mlist, 0, sizeof(mlist)); 1739109323Ssam return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist, 1740109323Ssam sizeof(mlist)); 174194405Simp } 174294405Simp 1743109323Ssam n = 0; 1744148654Srwatson IF_ADDR_LOCK(ifp); 1745109323Ssam#if __FreeBSD_version < 500000 1746109323Ssam LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1747109323Ssam#else 1748109323Ssam TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1749109323Ssam#endif 1750109323Ssam if (ifma->ifma_addr->sa_family != AF_LINK) 1751109323Ssam continue; 1752109323Ssam if (n >= 16) 1753109323Ssam goto allmulti; 1754109323Ssam IEEE80211_ADDR_COPY(&mlist.wi_mcast[n], 1755109323Ssam (LLADDR((struct sockaddr_dl *)ifma->ifma_addr))); 1756109323Ssam n++; 175794405Simp } 1758148654Srwatson IF_ADDR_UNLOCK(ifp); 1759109323Ssam return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist, 1760109323Ssam IEEE80211_ADDR_LEN * n); 176194405Simp} 176294405Simp 176394405Simpstatic void 1764109323Ssamwi_read_nicid(struct wi_softc *sc) 176546492Swpaul{ 1766109323Ssam struct wi_card_ident *id; 1767109323Ssam char *p; 1768109323Ssam int len; 1769109323Ssam u_int16_t ver[4]; 177046492Swpaul 1771109323Ssam /* getting chip identity */ 1772109323Ssam memset(ver, 0, sizeof(ver)); 1773109323Ssam len = sizeof(ver); 1774109323Ssam wi_read_rid(sc, WI_RID_CARD_ID, ver, &len); 1775109323Ssam device_printf(sc->sc_dev, "using "); 177646492Swpaul 1777109323Ssam sc->sc_firmware_type = WI_NOTYPE; 1778109323Ssam for (id = wi_card_ident; id->card_name != NULL; id++) { 1779109323Ssam if (le16toh(ver[0]) == id->card_id) { 1780109323Ssam printf("%s", id->card_name); 1781109323Ssam sc->sc_firmware_type = id->firm_type; 1782109323Ssam break; 1783109323Ssam } 178467092Swpaul } 1785109323Ssam if (sc->sc_firmware_type == WI_NOTYPE) { 1786109323Ssam if (le16toh(ver[0]) & 0x8000) { 1787109323Ssam printf("Unknown PRISM2 chip"); 1788109323Ssam sc->sc_firmware_type = WI_INTERSIL; 1789109323Ssam } else { 1790109323Ssam printf("Unknown Lucent chip"); 1791109323Ssam sc->sc_firmware_type = WI_LUCENT; 1792109323Ssam } 179367092Swpaul } 179446492Swpaul 1795109323Ssam /* get primary firmware version (Only Prism chips) */ 1796109323Ssam if (sc->sc_firmware_type != WI_LUCENT) { 1797109323Ssam memset(ver, 0, sizeof(ver)); 1798109323Ssam len = sizeof(ver); 1799109323Ssam wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len); 1800109323Ssam sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 + 1801109323Ssam le16toh(ver[3]) * 100 + le16toh(ver[1]); 180267092Swpaul } 180346492Swpaul 1804109323Ssam /* get station firmware version */ 1805109323Ssam memset(ver, 0, sizeof(ver)); 1806109323Ssam len = sizeof(ver); 1807109323Ssam wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len); 1808109323Ssam sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 + 1809109323Ssam le16toh(ver[3]) * 100 + le16toh(ver[1]); 1810109323Ssam if (sc->sc_firmware_type == WI_INTERSIL && 1811109323Ssam (sc->sc_sta_firmware_ver == 10102 || 1812109323Ssam sc->sc_sta_firmware_ver == 20102)) { 1813109323Ssam char ident[12]; 1814109323Ssam memset(ident, 0, sizeof(ident)); 1815109323Ssam len = sizeof(ident); 1816109323Ssam /* value should be the format like "V2.00-11" */ 1817109323Ssam if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 && 1818109323Ssam *(p = (char *)ident) >= 'A' && 1819109323Ssam p[2] == '.' && p[5] == '-' && p[8] == '\0') { 1820109323Ssam sc->sc_firmware_type = WI_SYMBOL; 1821109323Ssam sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 + 1822109323Ssam (p[3] - '0') * 1000 + (p[4] - '0') * 100 + 1823109323Ssam (p[6] - '0') * 10 + (p[7] - '0'); 182494405Simp } 182594405Simp } 1826109323Ssam printf("\n"); 1827109323Ssam device_printf(sc->sc_dev, "%s Firmware: ", 1828109323Ssam sc->sc_firmware_type == WI_LUCENT ? "Lucent" : 1829109323Ssam (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil")); 1830109323Ssam if (sc->sc_firmware_type != WI_LUCENT) /* XXX */ 1831109323Ssam printf("Primary (%u.%u.%u), ", 1832109323Ssam sc->sc_pri_firmware_ver / 10000, 1833109323Ssam (sc->sc_pri_firmware_ver % 10000) / 100, 1834109323Ssam sc->sc_pri_firmware_ver % 100); 1835109323Ssam printf("Station (%u.%u.%u)\n", 1836109323Ssam sc->sc_sta_firmware_ver / 10000, 1837109323Ssam (sc->sc_sta_firmware_ver % 10000) / 100, 1838109323Ssam sc->sc_sta_firmware_ver % 100); 1839109323Ssam} 184046492Swpaul 1841109323Ssamstatic int 1842109323Ssamwi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen) 1843109323Ssam{ 1844109323Ssam struct wi_ssid ssid; 184546492Swpaul 1846109323Ssam if (buflen > IEEE80211_NWID_LEN) 1847109323Ssam return ENOBUFS; 1848109323Ssam memset(&ssid, 0, sizeof(ssid)); 1849109323Ssam ssid.wi_len = htole16(buflen); 1850109323Ssam memcpy(ssid.wi_ssid, buf, buflen); 1851109323Ssam return wi_write_rid(sc, rid, &ssid, sizeof(ssid)); 1852109323Ssam} 185346492Swpaul 1854109323Ssamstatic int 1855109323Ssamwi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data) 1856109323Ssam{ 1857109323Ssam struct wi_softc *sc = ifp->if_softc; 1858109323Ssam struct ieee80211com *ic = &sc->sc_ic; 1859109323Ssam struct ifreq *ifr = (struct ifreq *)data; 1860109323Ssam struct wi_req wreq; 1861116898Ssam struct wi_scan_res *res; 1862122015Sgreen size_t reslen; 1863116898Ssam int len, n, error, mif, val, off, i; 186446492Swpaul 1865109323Ssam error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 1866109323Ssam if (error) 1867109323Ssam return error; 1868109323Ssam len = (wreq.wi_len - 1) * 2; 1869109323Ssam if (len < sizeof(u_int16_t)) 1870109323Ssam return ENOSPC; 1871109323Ssam if (len > sizeof(wreq.wi_val)) 1872109323Ssam len = sizeof(wreq.wi_val); 187346492Swpaul 1874109323Ssam switch (wreq.wi_type) { 187546492Swpaul 1876109323Ssam case WI_RID_IFACE_STATS: 1877109323Ssam memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats)); 1878109323Ssam if (len < sizeof(sc->sc_stats)) 1879109323Ssam error = ENOSPC; 1880109323Ssam else 1881109323Ssam len = sizeof(sc->sc_stats); 1882109323Ssam break; 188346492Swpaul 1884109323Ssam case WI_RID_ENCRYPTION: 1885109323Ssam case WI_RID_TX_CRYPT_KEY: 1886109323Ssam case WI_RID_DEFLT_CRYPT_KEYS: 1887109323Ssam case WI_RID_TX_RATE: 1888138571Ssam return ieee80211_cfgget(ic, cmd, data); 188946492Swpaul 1890109323Ssam case WI_RID_MICROWAVE_OVEN: 1891109323Ssam if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) { 1892109323Ssam error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1893109323Ssam &len); 1894109323Ssam break; 1895109323Ssam } 1896109323Ssam wreq.wi_val[0] = htole16(sc->sc_microwave_oven); 1897109323Ssam len = sizeof(u_int16_t); 1898109323Ssam break; 189946492Swpaul 1900109323Ssam case WI_RID_DBM_ADJUST: 1901109323Ssam if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) { 1902109323Ssam error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1903109323Ssam &len); 1904109323Ssam break; 1905109323Ssam } 1906119784Ssam wreq.wi_val[0] = htole16(sc->sc_dbm_offset); 1907109323Ssam len = sizeof(u_int16_t); 1908109323Ssam break; 190946492Swpaul 1910109323Ssam case WI_RID_ROAMING_MODE: 1911109323Ssam if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) { 1912109323Ssam error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1913109323Ssam &len); 1914109323Ssam break; 1915109323Ssam } 1916109323Ssam wreq.wi_val[0] = htole16(sc->sc_roaming_mode); 1917109323Ssam len = sizeof(u_int16_t); 1918109323Ssam break; 191946492Swpaul 1920109323Ssam case WI_RID_SYSTEM_SCALE: 1921109323Ssam if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) { 1922109323Ssam error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1923109323Ssam &len); 1924109323Ssam break; 1925109323Ssam } 1926109323Ssam wreq.wi_val[0] = htole16(sc->sc_system_scale); 1927109323Ssam len = sizeof(u_int16_t); 1928109323Ssam break; 192946492Swpaul 1930109323Ssam case WI_RID_FRAG_THRESH: 1931109323Ssam if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) { 1932109323Ssam error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1933109323Ssam &len); 1934109323Ssam break; 1935109323Ssam } 1936116951Ssam wreq.wi_val[0] = htole16(ic->ic_fragthreshold); 1937109323Ssam len = sizeof(u_int16_t); 1938109323Ssam break; 193946492Swpaul 1940109323Ssam case WI_RID_READ_APS: 1941109323Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1942138571Ssam return ieee80211_cfgget(ic, cmd, data); 1943109323Ssam if (sc->sc_scan_timer > 0) { 1944109323Ssam error = EINPROGRESS; 1945109323Ssam break; 1946109323Ssam } 1947109323Ssam n = sc->sc_naps; 1948109323Ssam if (len < sizeof(n)) { 1949109323Ssam error = ENOSPC; 1950109323Ssam break; 1951109323Ssam } 1952109323Ssam if (len < sizeof(n) + sizeof(struct wi_apinfo) * n) 1953109323Ssam n = (len - sizeof(n)) / sizeof(struct wi_apinfo); 1954109323Ssam len = sizeof(n) + sizeof(struct wi_apinfo) * n; 1955109323Ssam memcpy(wreq.wi_val, &n, sizeof(n)); 1956109323Ssam memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps, 1957109323Ssam sizeof(struct wi_apinfo) * n); 1958109323Ssam break; 195946492Swpaul 1960109323Ssam case WI_RID_PRISM2: 1961109323Ssam wreq.wi_val[0] = sc->sc_firmware_type != WI_LUCENT; 1962109323Ssam len = sizeof(u_int16_t); 1963109323Ssam break; 196446492Swpaul 1965109323Ssam case WI_RID_MIF: 1966109323Ssam mif = wreq.wi_val[0]; 1967109323Ssam error = wi_cmd(sc, WI_CMD_READMIF, mif, 0, 0); 1968109323Ssam val = CSR_READ_2(sc, WI_RESP0); 1969109323Ssam wreq.wi_val[0] = val; 1970109323Ssam len = sizeof(u_int16_t); 1971109323Ssam break; 197246492Swpaul 1973109323Ssam case WI_RID_ZERO_CACHE: 1974109323Ssam case WI_RID_PROCFRAME: /* ignore for compatibility */ 1975109323Ssam /* XXX ??? */ 1976109323Ssam break; 197746492Swpaul 1978109323Ssam case WI_RID_READ_CACHE: 1979138571Ssam return ieee80211_cfgget(ic, cmd, data); 198046492Swpaul 1981116898Ssam case WI_RID_SCAN_RES: /* compatibility interface */ 1982116898Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1983138571Ssam return ieee80211_cfgget(ic, cmd, data); 1984116898Ssam if (sc->sc_scan_timer > 0) { 1985116898Ssam error = EINPROGRESS; 1986116898Ssam break; 1987116898Ssam } 1988116898Ssam n = sc->sc_naps; 1989122015Sgreen if (sc->sc_firmware_type == WI_LUCENT) { 1990122015Sgreen off = 0; 1991122015Sgreen reslen = WI_WAVELAN_RES_SIZE; 1992122015Sgreen } else { 1993122015Sgreen off = sizeof(struct wi_scan_p2_hdr); 1994122015Sgreen reslen = WI_PRISM2_RES_SIZE; 1995122015Sgreen } 1996122015Sgreen if (len < off + reslen * n) 1997122015Sgreen n = (len - off) / reslen; 1998122015Sgreen len = off + reslen * n; 1999116898Ssam if (off != 0) { 2000116898Ssam struct wi_scan_p2_hdr *p2 = (struct wi_scan_p2_hdr *)wreq.wi_val; 2001116898Ssam /* 2002116898Ssam * Prepend Prism-specific header. 2003116898Ssam */ 2004116898Ssam if (len < sizeof(struct wi_scan_p2_hdr)) { 2005116898Ssam error = ENOSPC; 2006116898Ssam break; 2007116898Ssam } 2008116898Ssam p2 = (struct wi_scan_p2_hdr *)wreq.wi_val; 2009116898Ssam p2->wi_rsvd = 0; 2010116898Ssam p2->wi_reason = n; /* XXX */ 2011116898Ssam } 2012122015Sgreen for (i = 0; i < n; i++, off += reslen) { 2013116898Ssam const struct wi_apinfo *ap = &sc->sc_aps[i]; 2014116898Ssam 2015116898Ssam res = (struct wi_scan_res *)((char *)wreq.wi_val + off); 2016116898Ssam res->wi_chan = ap->channel; 2017116898Ssam res->wi_noise = ap->noise; 2018116898Ssam res->wi_signal = ap->signal; 2019116898Ssam IEEE80211_ADDR_COPY(res->wi_bssid, ap->bssid); 2020116898Ssam res->wi_interval = ap->interval; 2021116898Ssam res->wi_capinfo = ap->capinfo; 2022116898Ssam res->wi_ssid_len = ap->namelen; 2023116898Ssam memcpy(res->wi_ssid, ap->name, 2024116898Ssam IEEE80211_NWID_LEN); 2025116898Ssam if (sc->sc_firmware_type != WI_LUCENT) { 2026116898Ssam /* XXX not saved from Prism cards */ 2027116898Ssam memset(res->wi_srates, 0, 2028116898Ssam sizeof(res->wi_srates)); 2029116898Ssam res->wi_rate = ap->rate; 2030116898Ssam res->wi_rsvd = 0; 2031122015Sgreen } 2032116898Ssam } 2033116898Ssam break; 2034116898Ssam 2035109323Ssam default: 2036109323Ssam if (sc->sc_enabled) { 2037109323Ssam error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 2038109323Ssam &len); 2039109323Ssam break; 2040109323Ssam } 2041109323Ssam switch (wreq.wi_type) { 2042109323Ssam case WI_RID_MAX_DATALEN: 2043109323Ssam wreq.wi_val[0] = htole16(sc->sc_max_datalen); 2044109323Ssam len = sizeof(u_int16_t); 2045109323Ssam break; 2046109323Ssam case WI_RID_RTS_THRESH: 2047116951Ssam wreq.wi_val[0] = htole16(ic->ic_rtsthreshold); 2048109323Ssam len = sizeof(u_int16_t); 2049109323Ssam break; 2050109323Ssam case WI_RID_CNFAUTHMODE: 2051109323Ssam wreq.wi_val[0] = htole16(sc->sc_cnfauthmode); 2052109323Ssam len = sizeof(u_int16_t); 2053109323Ssam break; 2054109323Ssam case WI_RID_NODENAME: 2055109323Ssam if (len < sc->sc_nodelen + sizeof(u_int16_t)) { 2056109323Ssam error = ENOSPC; 2057109323Ssam break; 2058109323Ssam } 2059109323Ssam len = sc->sc_nodelen + sizeof(u_int16_t); 2060109323Ssam wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2); 2061109323Ssam memcpy(&wreq.wi_val[1], sc->sc_nodename, 2062109323Ssam sc->sc_nodelen); 2063109323Ssam break; 2064109323Ssam default: 2065138571Ssam return ieee80211_cfgget(ic, cmd, data); 2066109323Ssam } 2067109323Ssam break; 206846492Swpaul } 2069109323Ssam if (error) 2070109323Ssam return error; 2071109323Ssam wreq.wi_len = (len + 1) / 2 + 1; 2072109323Ssam return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2); 207346492Swpaul} 207446492Swpaul 2075109323Ssamstatic int 2076109323Ssamwi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data) 207746492Swpaul{ 2078109323Ssam struct wi_softc *sc = ifp->if_softc; 2079109323Ssam struct ieee80211com *ic = &sc->sc_ic; 2080109323Ssam struct ifreq *ifr = (struct ifreq *)data; 2081109323Ssam struct wi_req wreq; 2082109323Ssam struct mbuf *m; 2083109323Ssam int i, len, error, mif, val; 2084116951Ssam struct ieee80211_rateset *rs; 2085138571Ssam WI_LOCK_DECL(); 208646492Swpaul 2087109323Ssam error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 2088109323Ssam if (error) 2089109323Ssam return error; 2090116898Ssam len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0; 2091109323Ssam switch (wreq.wi_type) { 2092109323Ssam case WI_RID_DBM_ADJUST: 2093109323Ssam return ENODEV; 209467092Swpaul 2095109323Ssam case WI_RID_NODENAME: 2096109323Ssam if (le16toh(wreq.wi_val[0]) * 2 > len || 2097109323Ssam le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) { 2098109323Ssam error = ENOSPC; 2099109323Ssam break; 2100109323Ssam } 2101138571Ssam WI_LOCK(sc); 2102138571Ssam if (sc->sc_enabled) 2103109323Ssam error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val, 2104109323Ssam len); 2105138571Ssam if (error == 0) { 2106138571Ssam sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2; 2107138571Ssam memcpy(sc->sc_nodename, &wreq.wi_val[1], 2108138571Ssam sc->sc_nodelen); 2109109323Ssam } 2110138571Ssam WI_UNLOCK(sc); 2111109323Ssam break; 211246492Swpaul 2113109323Ssam case WI_RID_MICROWAVE_OVEN: 2114109323Ssam case WI_RID_ROAMING_MODE: 2115109323Ssam case WI_RID_SYSTEM_SCALE: 2116109323Ssam case WI_RID_FRAG_THRESH: 2117138571Ssam /* XXX unlocked reads */ 2118109323Ssam if (wreq.wi_type == WI_RID_MICROWAVE_OVEN && 2119109323Ssam (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0) 2120109323Ssam break; 2121109323Ssam if (wreq.wi_type == WI_RID_ROAMING_MODE && 2122109323Ssam (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0) 2123109323Ssam break; 2124109323Ssam if (wreq.wi_type == WI_RID_SYSTEM_SCALE && 2125109323Ssam (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0) 2126109323Ssam break; 2127109323Ssam if (wreq.wi_type == WI_RID_FRAG_THRESH && 2128109323Ssam (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0) 2129109323Ssam break; 2130109323Ssam /* FALLTHROUGH */ 2131109323Ssam case WI_RID_RTS_THRESH: 2132109323Ssam case WI_RID_CNFAUTHMODE: 2133109323Ssam case WI_RID_MAX_DATALEN: 2134138571Ssam WI_LOCK(sc); 2135109323Ssam if (sc->sc_enabled) { 2136109323Ssam error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val, 2137109323Ssam sizeof(u_int16_t)); 2138138571Ssam if (error != 0) { 2139138571Ssam WI_UNLOCK(sc); 2140109323Ssam break; 2141138571Ssam } 2142109323Ssam } 2143109323Ssam switch (wreq.wi_type) { 2144109323Ssam case WI_RID_FRAG_THRESH: 2145116951Ssam ic->ic_fragthreshold = le16toh(wreq.wi_val[0]); 2146109323Ssam break; 2147109323Ssam case WI_RID_RTS_THRESH: 2148116951Ssam ic->ic_rtsthreshold = le16toh(wreq.wi_val[0]); 2149109323Ssam break; 2150109323Ssam case WI_RID_MICROWAVE_OVEN: 2151109323Ssam sc->sc_microwave_oven = le16toh(wreq.wi_val[0]); 2152109323Ssam break; 2153109323Ssam case WI_RID_ROAMING_MODE: 2154109323Ssam sc->sc_roaming_mode = le16toh(wreq.wi_val[0]); 2155109323Ssam break; 2156109323Ssam case WI_RID_SYSTEM_SCALE: 2157109323Ssam sc->sc_system_scale = le16toh(wreq.wi_val[0]); 2158109323Ssam break; 2159109323Ssam case WI_RID_CNFAUTHMODE: 2160109323Ssam sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]); 2161109323Ssam break; 2162109323Ssam case WI_RID_MAX_DATALEN: 2163109323Ssam sc->sc_max_datalen = le16toh(wreq.wi_val[0]); 2164109323Ssam break; 2165109323Ssam } 2166138571Ssam WI_UNLOCK(sc); 2167109323Ssam break; 216894405Simp 2169109323Ssam case WI_RID_TX_RATE: 2170138571Ssam WI_LOCK(sc); 2171109323Ssam switch (le16toh(wreq.wi_val[0])) { 2172109323Ssam case 3: 2173148290Ssam ic->ic_fixed_rate = IEEE80211_FIXED_RATE_NONE; 2174109323Ssam break; 2175109323Ssam default: 2176116951Ssam rs = &ic->ic_sup_rates[IEEE80211_MODE_11B]; 2177116951Ssam for (i = 0; i < rs->rs_nrates; i++) { 2178116951Ssam if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) 2179109323Ssam / 2 == le16toh(wreq.wi_val[0])) 2180109323Ssam break; 2181109323Ssam } 2182138571Ssam if (i == rs->rs_nrates) { 2183138571Ssam WI_UNLOCK(sc); 2184109323Ssam return EINVAL; 2185138571Ssam } 2186109323Ssam ic->ic_fixed_rate = i; 2187109323Ssam } 2188109323Ssam if (sc->sc_enabled) 2189109323Ssam error = wi_write_txrate(sc); 2190138571Ssam WI_UNLOCK(sc); 2191109323Ssam break; 219246492Swpaul 2193109323Ssam case WI_RID_SCAN_APS: 2194138571Ssam WI_LOCK(sc); 2195109323Ssam if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP) 2196116898Ssam error = wi_scan_ap(sc, 0x3fff, 0x000f); 2197138571Ssam WI_UNLOCK(sc); 2198109323Ssam break; 219946492Swpaul 2200116898Ssam case WI_RID_SCAN_REQ: /* compatibility interface */ 2201138571Ssam WI_LOCK(sc); 2202116898Ssam if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP) 2203116898Ssam error = wi_scan_ap(sc, wreq.wi_val[0], wreq.wi_val[1]); 2204138571Ssam WI_UNLOCK(sc); 2205116898Ssam break; 2206116898Ssam 2207109323Ssam case WI_RID_MGMT_XMIT: 2208138571Ssam WI_LOCK(sc); 2209138571Ssam if (!sc->sc_enabled) 2210109323Ssam error = ENETDOWN; 2211138571Ssam else if (ic->ic_mgtq.ifq_len > 5) 2212109323Ssam error = EAGAIN; 2213138571Ssam else { 2214138571Ssam /* NB: m_devget uses M_DONTWAIT so can hold the lock */ 2215138571Ssam /* XXX wi_len looks in u_int8_t, not in u_int16_t */ 2216138571Ssam m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, 2217138571Ssam ifp, NULL); 2218138571Ssam if (m != NULL) 2219138571Ssam IF_ENQUEUE(&ic->ic_mgtq, m); 2220138571Ssam else 2221138571Ssam error = ENOMEM; 2222109323Ssam } 2223138571Ssam WI_UNLOCK(sc); 2224109323Ssam break; 222546492Swpaul 2226109323Ssam case WI_RID_MIF: 2227109323Ssam mif = wreq.wi_val[0]; 2228109323Ssam val = wreq.wi_val[1]; 2229138571Ssam WI_LOCK(sc); 2230109323Ssam error = wi_cmd(sc, WI_CMD_WRITEMIF, mif, val, 0); 2231138571Ssam WI_UNLOCK(sc); 2232109323Ssam break; 223346492Swpaul 2234109323Ssam case WI_RID_PROCFRAME: /* ignore for compatibility */ 2235109323Ssam break; 2236109323Ssam 2237116898Ssam case WI_RID_OWN_SSID: 2238116898Ssam if (le16toh(wreq.wi_val[0]) * 2 > len || 2239116898Ssam le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) { 2240116898Ssam error = ENOSPC; 2241116898Ssam break; 2242116898Ssam } 2243138571Ssam WI_LOCK(sc); 2244116898Ssam memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN); 2245116898Ssam ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2; 2246116898Ssam memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen); 2247138571Ssam if (sc->sc_enabled) 2248138571Ssam wi_init(sc); /* XXX no error return */ 2249138571Ssam WI_UNLOCK(sc); 2250116898Ssam break; 2251116898Ssam 2252109323Ssam default: 2253138571Ssam WI_LOCK(sc); 2254138571Ssam if (sc->sc_enabled) 2255109323Ssam error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val, 2256109323Ssam len); 2257138571Ssam if (error == 0) { 2258138571Ssam /* XXX ieee80211_cfgset does a copyin */ 2259138571Ssam error = ieee80211_cfgset(ic, cmd, data); 2260138571Ssam if (error == ENETRESET) { 2261138571Ssam if (sc->sc_enabled) 2262138571Ssam wi_init(sc); 2263138571Ssam error = 0; 2264138571Ssam } 2265109323Ssam } 2266138571Ssam WI_UNLOCK(sc); 2267109323Ssam break; 2268109323Ssam } 2269109323Ssam return error; 227046492Swpaul} 227146492Swpaul 2272109323Ssamstatic int 2273109323Ssamwi_write_txrate(struct wi_softc *sc) 227446492Swpaul{ 2275109323Ssam struct ieee80211com *ic = &sc->sc_ic; 2276109323Ssam int i; 2277109323Ssam u_int16_t rate; 227846492Swpaul 2279148290Ssam if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) 2280109323Ssam rate = 0; /* auto */ 2281109323Ssam else 2282116951Ssam rate = (ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ic->ic_fixed_rate] & 2283109323Ssam IEEE80211_RATE_VAL) / 2; 228446492Swpaul 2285109323Ssam /* rate: 0, 1, 2, 5, 11 */ 228646492Swpaul 2287109323Ssam switch (sc->sc_firmware_type) { 2288109323Ssam case WI_LUCENT: 2289112501Simp switch (rate) { 2290112501Simp case 0: /* auto == 11mbps auto */ 2291112501Simp rate = 3; 2292112501Simp break; 2293112501Simp /* case 1, 2 map to 1, 2*/ 2294112501Simp case 5: /* 5.5Mbps -> 4 */ 2295112501Simp rate = 4; 2296112501Simp break; 2297112501Simp case 11: /* 11mbps -> 5 */ 2298112501Simp rate = 5; 2299112501Simp break; 2300112501Simp default: 2301112501Simp break; 2302112501Simp } 2303109323Ssam break; 2304109323Ssam default: 2305109323Ssam /* Choose a bit according to this table. 2306109323Ssam * 2307109323Ssam * bit | data rate 2308109323Ssam * ----+------------------- 2309109323Ssam * 0 | 1Mbps 2310109323Ssam * 1 | 2Mbps 2311109323Ssam * 2 | 5.5Mbps 2312109323Ssam * 3 | 11Mbps 2313109323Ssam */ 2314109323Ssam for (i = 8; i > 0; i >>= 1) { 2315109323Ssam if (rate >= i) 2316109323Ssam break; 2317109323Ssam } 2318109323Ssam if (i == 0) 2319109323Ssam rate = 0xf; /* auto */ 2320109323Ssam else 2321109323Ssam rate = i; 2322109323Ssam break; 2323109323Ssam } 2324109323Ssam return wi_write_val(sc, WI_RID_TX_RATE, rate); 2325109323Ssam} 232646492Swpaul 2327109323Ssamstatic int 2328109323Ssamwi_write_wep(struct wi_softc *sc) 2329109323Ssam{ 2330109323Ssam struct ieee80211com *ic = &sc->sc_ic; 2331109323Ssam int error = 0; 2332109323Ssam int i, keylen; 2333109323Ssam u_int16_t val; 2334109323Ssam struct wi_key wkey[IEEE80211_WEP_NKID]; 233546492Swpaul 2336109323Ssam switch (sc->sc_firmware_type) { 2337109323Ssam case WI_LUCENT: 2338138571Ssam val = (ic->ic_flags & IEEE80211_F_PRIVACY) ? 1 : 0; 2339109323Ssam error = wi_write_val(sc, WI_RID_ENCRYPTION, val); 2340109323Ssam if (error) 2341109323Ssam break; 2342138988Smdodd if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) 2343138988Smdodd break; 2344138988Smdodd error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_def_txkey); 2345138988Smdodd if (error) 2346138988Smdodd break; 2347138988Smdodd memset(wkey, 0, sizeof(wkey)); 2348138988Smdodd for (i = 0; i < IEEE80211_WEP_NKID; i++) { 2349138988Smdodd keylen = ic->ic_nw_keys[i].wk_keylen; 2350138988Smdodd wkey[i].wi_keylen = htole16(keylen); 2351138988Smdodd memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key, 2352138988Smdodd keylen); 2353109323Ssam } 2354138988Smdodd error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS, 2355138988Smdodd wkey, sizeof(wkey)); 2356109323Ssam break; 2357109323Ssam 2358109323Ssam case WI_INTERSIL: 2359109323Ssam case WI_SYMBOL: 2360138571Ssam if (ic->ic_flags & IEEE80211_F_PRIVACY) { 2361109323Ssam /* 2362109323Ssam * ONLY HWB3163 EVAL-CARD Firmware version 2363109323Ssam * less than 0.8 variant2 2364109323Ssam * 2365109323Ssam * If promiscuous mode disable, Prism2 chip 2366109323Ssam * does not work with WEP . 2367109323Ssam * It is under investigation for details. 2368109323Ssam * (ichiro@netbsd.org) 2369109323Ssam */ 2370109323Ssam if (sc->sc_firmware_type == WI_INTERSIL && 2371109323Ssam sc->sc_sta_firmware_ver < 802 ) { 2372109323Ssam /* firm ver < 0.8 variant 2 */ 2373109323Ssam wi_write_val(sc, WI_RID_PROMISC, 1); 2374109323Ssam } 2375109323Ssam wi_write_val(sc, WI_RID_CNFAUTHMODE, 2376109323Ssam sc->sc_cnfauthmode); 2377109323Ssam val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED; 2378109323Ssam /* 2379109323Ssam * Encryption firmware has a bug for HostAP mode. 2380109323Ssam */ 2381109323Ssam if (sc->sc_firmware_type == WI_INTERSIL && 2382109323Ssam ic->ic_opmode == IEEE80211_M_HOSTAP) 2383109323Ssam val |= HOST_ENCRYPT; 2384109323Ssam } else { 2385109323Ssam wi_write_val(sc, WI_RID_CNFAUTHMODE, 2386109323Ssam IEEE80211_AUTH_OPEN); 2387109323Ssam val = HOST_ENCRYPT | HOST_DECRYPT; 2388109323Ssam } 2389109323Ssam error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val); 2390109323Ssam if (error) 2391109323Ssam break; 2392138988Smdodd if ((val & PRIVACY_INVOKED) == 0) 2393138988Smdodd break; 2394138988Smdodd error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY, 2395138988Smdodd ic->ic_def_txkey); 2396138988Smdodd if (error) 2397138988Smdodd break; 2398138988Smdodd if (val & HOST_DECRYPT) 2399138988Smdodd break; 2400138988Smdodd /* 2401138988Smdodd * It seems that the firmware accept 104bit key only if 2402138988Smdodd * all the keys have 104bit length. We get the length of 2403138988Smdodd * the transmit key and use it for all other keys. 2404138988Smdodd * Perhaps we should use software WEP for such situation. 2405138988Smdodd */ 2406138988Smdodd if (ic->ic_def_txkey != IEEE80211_KEYIX_NONE) 2407138988Smdodd keylen = ic->ic_nw_keys[ic->ic_def_txkey].wk_keylen; 2408138988Smdodd else /* XXX should not hapen */ 2409138988Smdodd keylen = IEEE80211_WEP_KEYLEN; 2410138988Smdodd if (keylen > IEEE80211_WEP_KEYLEN) 2411138988Smdodd keylen = 13; /* 104bit keys */ 2412138988Smdodd else 2413138988Smdodd keylen = IEEE80211_WEP_KEYLEN; 2414138988Smdodd for (i = 0; i < IEEE80211_WEP_NKID; i++) { 2415138988Smdodd error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i, 2416138988Smdodd ic->ic_nw_keys[i].wk_key, keylen); 2417109323Ssam if (error) 2418109323Ssam break; 2419109323Ssam } 2420109323Ssam break; 2421109323Ssam } 2422109323Ssam return error; 242346492Swpaul} 242446492Swpaul 2425109323Ssamstatic int 2426109323Ssamwi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2) 242746492Swpaul{ 2428109323Ssam int i, s = 0; 2429109323Ssam static volatile int count = 0; 2430109323Ssam 2431123098Simp if (sc->wi_gone) 2432123098Simp return (ENODEV); 2433123098Simp 2434109323Ssam if (count > 0) 2435109323Ssam panic("Hey partner, hold on there!"); 2436109323Ssam count++; 243753702Swpaul 2438109323Ssam /* wait for the busy bit to clear */ 2439123339Simp for (i = sc->wi_cmd_count; i > 0; i--) { /* 500ms */ 2440116206Simp if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY)) 2441109323Ssam break; 2442123098Simp DELAY(1*1000); /* 1ms */ 2443109323Ssam } 2444109323Ssam if (i == 0) { 2445109323Ssam device_printf(sc->sc_dev, "wi_cmd: busy bit won't clear.\n" ); 2446123098Simp sc->wi_gone = 1; 2447109323Ssam count--; 2448109323Ssam return(ETIMEDOUT); 2449109323Ssam } 245090580Sbrooks 2451109323Ssam CSR_WRITE_2(sc, WI_PARAM0, val0); 2452109323Ssam CSR_WRITE_2(sc, WI_PARAM1, val1); 2453109323Ssam CSR_WRITE_2(sc, WI_PARAM2, val2); 2454109323Ssam CSR_WRITE_2(sc, WI_COMMAND, cmd); 245590580Sbrooks 2456109323Ssam if (cmd == WI_CMD_INI) { 2457109323Ssam /* XXX: should sleep here. */ 2458116206Simp DELAY(100*1000); /* 100ms delay for init */ 2459109323Ssam } 2460109323Ssam for (i = 0; i < WI_TIMEOUT; i++) { 2461109323Ssam /* 2462109323Ssam * Wait for 'command complete' bit to be 2463109323Ssam * set in the event status register. 2464109323Ssam */ 2465109323Ssam s = CSR_READ_2(sc, WI_EVENT_STAT); 2466109323Ssam if (s & WI_EV_CMD) { 2467109323Ssam /* Ack the event and read result code. */ 2468109323Ssam s = CSR_READ_2(sc, WI_STATUS); 2469109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD); 2470109323Ssam if (s & WI_STAT_CMD_RESULT) { 2471109323Ssam count--; 2472109323Ssam return(EIO); 2473109323Ssam } 2474109323Ssam break; 247590580Sbrooks } 2476109323Ssam DELAY(WI_DELAY); 2477109323Ssam } 247890580Sbrooks 2479109323Ssam count--; 2480109323Ssam if (i == WI_TIMEOUT) { 2481109323Ssam device_printf(sc->sc_dev, 2482109323Ssam "timeout in wi_cmd 0x%04x; event status 0x%04x\n", cmd, s); 2483123098Simp if (s == 0xffff) 2484123098Simp sc->wi_gone = 1; 2485109323Ssam return(ETIMEDOUT); 248653702Swpaul } 2487109323Ssam return (0); 2488109323Ssam} 248953702Swpaul 2490109323Ssamstatic int 2491109323Ssamwi_seek_bap(struct wi_softc *sc, int id, int off) 2492109323Ssam{ 2493109323Ssam int i, status; 249490580Sbrooks 2495109323Ssam CSR_WRITE_2(sc, WI_SEL0, id); 2496109323Ssam CSR_WRITE_2(sc, WI_OFF0, off); 249790580Sbrooks 2498109323Ssam for (i = 0; ; i++) { 2499109323Ssam status = CSR_READ_2(sc, WI_OFF0); 2500109323Ssam if ((status & WI_OFF_BUSY) == 0) 2501109323Ssam break; 2502109323Ssam if (i == WI_TIMEOUT) { 2503109323Ssam device_printf(sc->sc_dev, "timeout in wi_seek to %x/%x\n", 2504109323Ssam id, off); 2505109323Ssam sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 2506123098Simp if (status == 0xffff) 2507123098Simp sc->wi_gone = 1; 2508109323Ssam return ETIMEDOUT; 2509109323Ssam } 2510109323Ssam DELAY(1); 251153702Swpaul } 2512109323Ssam if (status & WI_OFF_ERR) { 2513109323Ssam device_printf(sc->sc_dev, "failed in wi_seek to %x/%x\n", id, off); 2514109323Ssam sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 2515109323Ssam return EIO; 2516109323Ssam } 2517109323Ssam sc->sc_bap_id = id; 2518109323Ssam sc->sc_bap_off = off; 2519109323Ssam return 0; 252053702Swpaul} 252153702Swpaul 2522109323Ssamstatic int 2523109323Ssamwi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen) 252453702Swpaul{ 2525109323Ssam u_int16_t *ptr; 2526109323Ssam int i, error, cnt; 252753702Swpaul 2528109323Ssam if (buflen == 0) 2529109323Ssam return 0; 2530109323Ssam if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 2531109323Ssam if ((error = wi_seek_bap(sc, id, off)) != 0) 2532109323Ssam return error; 253375219Salfred } 2534109323Ssam cnt = (buflen + 1) / 2; 2535109323Ssam ptr = (u_int16_t *)buf; 2536109323Ssam for (i = 0; i < cnt; i++) 2537109323Ssam *ptr++ = CSR_READ_2(sc, WI_DATA0); 2538109323Ssam sc->sc_bap_off += cnt * 2; 2539109323Ssam return 0; 254053702Swpaul} 254153702Swpaul 2542109323Ssamstatic int 2543109323Ssamwi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen) 254453702Swpaul{ 2545109323Ssam u_int16_t *ptr; 2546109323Ssam int i, error, cnt; 254746492Swpaul 2548109323Ssam if (buflen == 0) 2549109323Ssam return 0; 255046492Swpaul 2551109323Ssam#ifdef WI_HERMES_AUTOINC_WAR 2552109323Ssam again: 2553109323Ssam#endif 2554109323Ssam if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 2555109323Ssam if ((error = wi_seek_bap(sc, id, off)) != 0) 2556109323Ssam return error; 2557109323Ssam } 2558109323Ssam cnt = (buflen + 1) / 2; 2559109323Ssam ptr = (u_int16_t *)buf; 2560109323Ssam for (i = 0; i < cnt; i++) 2561109323Ssam CSR_WRITE_2(sc, WI_DATA0, ptr[i]); 2562109323Ssam sc->sc_bap_off += cnt * 2; 2563109323Ssam 2564109323Ssam#ifdef WI_HERMES_AUTOINC_WAR 2565109323Ssam /* 2566109323Ssam * According to the comments in the HCF Light code, there is a bug 2567109323Ssam * in the Hermes (or possibly in certain Hermes firmware revisions) 2568109323Ssam * where the chip's internal autoincrement counter gets thrown off 2569109323Ssam * during data writes: the autoincrement is missed, causing one 2570109323Ssam * data word to be overwritten and subsequent words to be written to 2571109323Ssam * the wrong memory locations. The end result is that we could end 2572109323Ssam * up transmitting bogus frames without realizing it. The workaround 2573109323Ssam * for this is to write a couple of extra guard words after the end 2574109323Ssam * of the transfer, then attempt to read then back. If we fail to 2575109323Ssam * locate the guard words where we expect them, we preform the 2576109323Ssam * transfer over again. 2577109323Ssam */ 2578109323Ssam if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) { 2579109323Ssam CSR_WRITE_2(sc, WI_DATA0, 0x1234); 2580109323Ssam CSR_WRITE_2(sc, WI_DATA0, 0x5678); 2581109323Ssam wi_seek_bap(sc, id, sc->sc_bap_off); 2582109323Ssam sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 2583109323Ssam if (CSR_READ_2(sc, WI_DATA0) != 0x1234 || 2584109323Ssam CSR_READ_2(sc, WI_DATA0) != 0x5678) { 2585109323Ssam device_printf(sc->sc_dev, 2586109323Ssam "detect auto increment bug, try again\n"); 2587109323Ssam goto again; 2588109323Ssam } 2589109323Ssam } 2590109323Ssam#endif 2591109323Ssam return 0; 259246492Swpaul} 259353702Swpaul 2594109323Ssamstatic int 2595109323Ssamwi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen) 2596109323Ssam{ 2597109323Ssam int error, len; 2598109323Ssam struct mbuf *m; 259953702Swpaul 2600109323Ssam for (m = m0; m != NULL && totlen > 0; m = m->m_next) { 2601109323Ssam if (m->m_len == 0) 2602109323Ssam continue; 260353702Swpaul 2604109323Ssam len = min(m->m_len, totlen); 260553702Swpaul 2606109323Ssam if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) { 2607109323Ssam m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf); 2608109323Ssam return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf, 2609109323Ssam totlen); 2610109323Ssam } 261153702Swpaul 2612109323Ssam if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0) 2613109323Ssam return error; 261453702Swpaul 2615109323Ssam off += m->m_len; 2616109323Ssam totlen -= len; 2617109323Ssam } 2618109323Ssam return 0; 2619109323Ssam} 262053702Swpaul 2621109323Ssamstatic int 2622109323Ssamwi_alloc_fid(struct wi_softc *sc, int len, int *idp) 262353702Swpaul{ 262453702Swpaul int i; 262553702Swpaul 2626109323Ssam if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) { 2627109323Ssam device_printf(sc->sc_dev, "failed to allocate %d bytes on NIC\n", 2628109323Ssam len); 2629109323Ssam return ENOMEM; 263053702Swpaul } 263153702Swpaul 2632109323Ssam for (i = 0; i < WI_TIMEOUT; i++) { 2633109323Ssam if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC) 2634109323Ssam break; 2635109323Ssam DELAY(1); 263653702Swpaul } 2637144167Ssam if (i == WI_TIMEOUT) { 2638144167Ssam device_printf(sc->sc_dev, "timeout in alloc\n"); 2639144167Ssam return ETIMEDOUT; 2640144167Ssam } 2641109323Ssam *idp = CSR_READ_2(sc, WI_ALLOC_FID); 2642109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC); 2643109323Ssam return 0; 2644109323Ssam} 264553702Swpaul 2646109323Ssamstatic int 2647109323Ssamwi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp) 2648109323Ssam{ 2649109323Ssam int error, len; 2650109323Ssam u_int16_t ltbuf[2]; 265153702Swpaul 2652109323Ssam /* Tell the NIC to enter record read mode. */ 2653109323Ssam error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0); 2654109323Ssam if (error) 2655109323Ssam return error; 265653702Swpaul 2657109323Ssam error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf)); 2658109323Ssam if (error) 2659109323Ssam return error; 266053702Swpaul 2661109323Ssam if (le16toh(ltbuf[1]) != rid) { 2662109323Ssam device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n", 2663109323Ssam rid, le16toh(ltbuf[1])); 2664109323Ssam return EIO; 266553702Swpaul } 2666109323Ssam len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */ 2667109323Ssam if (*buflenp < len) { 2668109323Ssam device_printf(sc->sc_dev, "record buffer is too small, " 2669109323Ssam "rid=%x, size=%d, len=%d\n", 2670109323Ssam rid, *buflenp, len); 2671109323Ssam return ENOSPC; 267253702Swpaul } 2673109323Ssam *buflenp = len; 2674109323Ssam return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len); 2675109323Ssam} 267653702Swpaul 2677109323Ssamstatic int 2678109323Ssamwi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen) 2679109323Ssam{ 2680109323Ssam int error; 2681109323Ssam u_int16_t ltbuf[2]; 268253702Swpaul 2683109323Ssam ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */ 2684109323Ssam ltbuf[1] = htole16(rid); 268553702Swpaul 2686109323Ssam error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf)); 2687109323Ssam if (error) 2688109323Ssam return error; 2689109323Ssam error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen); 2690109323Ssam if (error) 2691109323Ssam return error; 2692102206Simp 2693109323Ssam return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0); 269453702Swpaul} 269577217Sphk 269688546Salfredstatic int 2697117812Ssamwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 269877217Sphk{ 2699138571Ssam struct ifnet *ifp = ic->ic_ifp; 2700117812Ssam struct wi_softc *sc = ifp->if_softc; 2701139542Ssam struct ieee80211_node *ni; 2702116951Ssam int buflen; 2703109323Ssam u_int16_t val; 2704109323Ssam struct wi_ssid ssid; 2705109323Ssam u_int8_t old_bssid[IEEE80211_ADDR_LEN]; 270677217Sphk 2707117812Ssam DPRINTF(("%s: %s -> %s\n", __func__, 2708117812Ssam ieee80211_state_name[ic->ic_state], 2709117812Ssam ieee80211_state_name[nstate])); 2710109323Ssam 2711139542Ssam /* 2712139542Ssam * Internal to the driver the INIT and RUN states are used 2713139542Ssam * so bypass the net80211 state machine for other states. 2714139542Ssam * Beware however that this requires use to net80211 state 2715139542Ssam * management that otherwise would be handled for us. 2716139542Ssam */ 2717109323Ssam switch (nstate) { 2718109323Ssam case IEEE80211_S_INIT: 2719109323Ssam sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 2720117812Ssam return (*sc->sc_newstate)(ic, nstate, arg); 2721109323Ssam 2722138571Ssam case IEEE80211_S_SCAN: 2723138571Ssam case IEEE80211_S_AUTH: 2724138571Ssam case IEEE80211_S_ASSOC: 2725138571Ssam ic->ic_state = nstate; /* NB: skip normal ieee80211 handling */ 2726138571Ssam break; 2727138571Ssam 2728109323Ssam case IEEE80211_S_RUN: 2729139542Ssam ni = ic->ic_bss; 2730109323Ssam sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 2731109323Ssam buflen = IEEE80211_ADDR_LEN; 2732138571Ssam IEEE80211_ADDR_COPY(old_bssid, ni->ni_bssid); 2733109323Ssam wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen); 2734109323Ssam IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid); 2735109323Ssam buflen = sizeof(val); 2736109323Ssam wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen); 2737116951Ssam /* XXX validate channel */ 2738116951Ssam ni->ni_chan = &ic->ic_channels[le16toh(val)]; 2739138951Ssam ic->ic_ibss_chan = ni->ni_chan; 2740119784Ssam#if NBPFILTER > 0 2741119784Ssam sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq = 2742119784Ssam htole16(ni->ni_chan->ic_freq); 2743119784Ssam sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags = 2744119784Ssam htole16(ni->ni_chan->ic_flags); 2745119784Ssam#endif 2746139542Ssam if (ic->ic_opmode != IEEE80211_M_HOSTAP) { 2747138952Ssam /* 2748138952Ssam * XXX hack; unceremoniously clear 2749138952Ssam * IEEE80211_F_DROPUNENC when operating with 2750138952Ssam * wep enabled so we don't drop unencoded frames 2751138952Ssam * at the 802.11 layer. This is necessary because 2752138952Ssam * we must strip the WEP bit from the 802.11 header 2753138952Ssam * before passing frames to ieee80211_input because 2754138952Ssam * the card has already stripped the WEP crypto 2755138952Ssam * header from the packet. 2756138952Ssam */ 2757138952Ssam if (ic->ic_flags & IEEE80211_F_PRIVACY) 2758138952Ssam ic->ic_flags &= ~IEEE80211_F_DROPUNENC; 2759109323Ssam /* XXX check return value */ 2760109323Ssam buflen = sizeof(ssid); 2761109323Ssam wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen); 2762109323Ssam ni->ni_esslen = le16toh(ssid.wi_len); 2763109323Ssam if (ni->ni_esslen > IEEE80211_NWID_LEN) 2764109323Ssam ni->ni_esslen = IEEE80211_NWID_LEN; /*XXX*/ 2765109323Ssam memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen); 276677217Sphk } 2767138571Ssam return (*sc->sc_newstate)(ic, nstate, arg); 276877217Sphk } 2769117812Ssam return 0; 277077217Sphk} 277177217Sphk 277288546Salfredstatic int 2773116898Ssamwi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate) 277477217Sphk{ 2775109323Ssam int error = 0; 2776109323Ssam u_int16_t val[2]; 277777217Sphk 2778109323Ssam if (!sc->sc_enabled) 2779109323Ssam return ENXIO; 2780109323Ssam switch (sc->sc_firmware_type) { 2781109323Ssam case WI_LUCENT: 2782109323Ssam (void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0); 278398440Simp break; 2784109323Ssam case WI_INTERSIL: 2785138571Ssam val[0] = htole16(chanmask); /* channel */ 2786138571Ssam val[1] = htole16(txrate); /* tx rate */ 2787109323Ssam error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val)); 278898440Simp break; 2789109323Ssam case WI_SYMBOL: 2790109323Ssam /* 2791109323Ssam * XXX only supported on 3.x ? 2792109323Ssam */ 2793109323Ssam val[0] = BSCAN_BCAST | BSCAN_ONETIME; 2794109323Ssam error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ, 2795109323Ssam val, sizeof(val[0])); 279698440Simp break; 279798440Simp } 2798109323Ssam if (error == 0) { 2799109323Ssam sc->sc_scan_timer = WI_SCAN_WAIT; 2800147256Sbrooks sc->sc_ifp->if_timer = 1; 2801116898Ssam DPRINTF(("wi_scan_ap: start scanning, " 2802116898Ssam "chamask 0x%x txrate 0x%x\n", chanmask, txrate)); 2803109323Ssam } 2804109323Ssam return error; 2805109323Ssam} 280677217Sphk 2807109323Ssamstatic void 2808109323Ssamwi_scan_result(struct wi_softc *sc, int fid, int cnt) 2809109323Ssam{ 2810109323Ssam#define N(a) (sizeof (a) / sizeof (a[0])) 2811109323Ssam int i, naps, off, szbuf; 2812109323Ssam struct wi_scan_header ws_hdr; /* Prism2 header */ 2813109323Ssam struct wi_scan_data_p2 ws_dat; /* Prism2 scantable*/ 2814109323Ssam struct wi_apinfo *ap; 2815109323Ssam 2816109323Ssam off = sizeof(u_int16_t) * 2; 2817109323Ssam memset(&ws_hdr, 0, sizeof(ws_hdr)); 2818109323Ssam switch (sc->sc_firmware_type) { 2819109323Ssam case WI_INTERSIL: 2820109323Ssam wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr)); 2821109323Ssam off += sizeof(ws_hdr); 2822109323Ssam szbuf = sizeof(struct wi_scan_data_p2); 282377217Sphk break; 2824109323Ssam case WI_SYMBOL: 2825109323Ssam szbuf = sizeof(struct wi_scan_data_p2) + 6; 282677217Sphk break; 2827109323Ssam case WI_LUCENT: 2828109323Ssam szbuf = sizeof(struct wi_scan_data); 282977217Sphk break; 2830109323Ssam default: 2831109323Ssam device_printf(sc->sc_dev, 2832109323Ssam "wi_scan_result: unknown firmware type %u\n", 2833109323Ssam sc->sc_firmware_type); 2834109323Ssam naps = 0; 2835109323Ssam goto done; 283677217Sphk } 2837109323Ssam naps = (cnt * 2 + 2 - off) / szbuf; 2838109323Ssam if (naps > N(sc->sc_aps)) 2839109323Ssam naps = N(sc->sc_aps); 2840109323Ssam sc->sc_naps = naps; 2841109323Ssam /* Read Data */ 2842109323Ssam ap = sc->sc_aps; 2843109323Ssam memset(&ws_dat, 0, sizeof(ws_dat)); 2844109323Ssam for (i = 0; i < naps; i++, ap++) { 2845109323Ssam wi_read_bap(sc, fid, off, &ws_dat, 2846109323Ssam (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf)); 2847109323Ssam DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off, 2848109323Ssam ether_sprintf(ws_dat.wi_bssid))); 2849109323Ssam off += szbuf; 2850109323Ssam ap->scanreason = le16toh(ws_hdr.wi_reason); 2851109323Ssam memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid)); 2852109323Ssam ap->channel = le16toh(ws_dat.wi_chid); 2853109323Ssam ap->signal = le16toh(ws_dat.wi_signal); 2854109323Ssam ap->noise = le16toh(ws_dat.wi_noise); 2855109323Ssam ap->quality = ap->signal - ap->noise; 2856109323Ssam ap->capinfo = le16toh(ws_dat.wi_capinfo); 2857109323Ssam ap->interval = le16toh(ws_dat.wi_interval); 2858109323Ssam ap->rate = le16toh(ws_dat.wi_rate); 2859109323Ssam ap->namelen = le16toh(ws_dat.wi_namelen); 2860109323Ssam if (ap->namelen > sizeof(ap->name)) 2861109323Ssam ap->namelen = sizeof(ap->name); 2862109323Ssam memcpy(ap->name, ws_dat.wi_name, ap->namelen); 2863109323Ssam } 2864109323Ssamdone: 2865109323Ssam /* Done scanning */ 2866109323Ssam sc->sc_scan_timer = 0; 2867109323Ssam DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps)); 2868109323Ssam#undef N 286977217Sphk} 287077217Sphk 287188546Salfredstatic void 2872109323Ssamwi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi) 287377217Sphk{ 2874109323Ssam ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr), 2875116951Ssam ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL : -1, rssi); 2876109323Ssam printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n", 2877109323Ssam le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1), 2878109323Ssam le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence); 2879109323Ssam printf(" rx_signal %u rx_rate %u rx_flow %u\n", 2880109323Ssam wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow); 2881109323Ssam printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n", 2882109323Ssam wh->wi_tx_rtry, wh->wi_tx_rate, 2883109323Ssam le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len)); 2884109323Ssam printf(" ehdr dst %6D src %6D type 0x%x\n", 2885109323Ssam wh->wi_ehdr.ether_dhost, ":", wh->wi_ehdr.ether_shost, ":", 2886109323Ssam wh->wi_ehdr.ether_type); 2887109323Ssam} 288877217Sphk 2889109323Ssamint 2890109323Ssamwi_alloc(device_t dev, int rid) 2891109323Ssam{ 2892109323Ssam struct wi_softc *sc = device_get_softc(dev); 2893109323Ssam 2894109323Ssam if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) { 2895109323Ssam sc->iobase_rid = rid; 2896109323Ssam sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT, 2897109323Ssam &sc->iobase_rid, 0, ~0, (1 << 6), 2898109323Ssam rman_make_alignment_flags(1 << 6) | RF_ACTIVE); 2899109323Ssam if (!sc->iobase) { 2900109323Ssam device_printf(dev, "No I/O space?!\n"); 2901109323Ssam return (ENXIO); 290298440Simp } 2903109323Ssam 2904109323Ssam sc->wi_io_addr = rman_get_start(sc->iobase); 2905109323Ssam sc->wi_btag = rman_get_bustag(sc->iobase); 2906109323Ssam sc->wi_bhandle = rman_get_bushandle(sc->iobase); 2907109323Ssam } else { 2908109323Ssam sc->mem_rid = rid; 2909127135Snjl sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 2910127135Snjl &sc->mem_rid, RF_ACTIVE); 2911109323Ssam 2912109323Ssam if (!sc->mem) { 2913109323Ssam device_printf(dev, "No Mem space on prism2.5?\n"); 2914109323Ssam return (ENXIO); 291577217Sphk } 2916109323Ssam 2917109323Ssam sc->wi_btag = rman_get_bustag(sc->mem); 2918109323Ssam sc->wi_bhandle = rman_get_bushandle(sc->mem); 291977217Sphk } 292077217Sphk 2921109323Ssam 2922109323Ssam sc->irq_rid = 0; 2923127135Snjl sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid, 2924127135Snjl RF_ACTIVE | 2925109323Ssam ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE)); 2926109323Ssam 2927109323Ssam if (!sc->irq) { 2928109323Ssam wi_free(dev); 2929109323Ssam device_printf(dev, "No irq?!\n"); 2930109323Ssam return (ENXIO); 293177217Sphk } 2932109323Ssam 2933109323Ssam sc->sc_dev = dev; 2934109323Ssam sc->sc_unit = device_get_unit(dev); 2935109323Ssam 2936109323Ssam return (0); 293777217Sphk} 293893359Simp 2939109323Ssamvoid 2940109323Ssamwi_free(device_t dev) 2941109323Ssam{ 2942109323Ssam struct wi_softc *sc = device_get_softc(dev); 2943109323Ssam 2944109323Ssam if (sc->iobase != NULL) { 2945109323Ssam bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase); 2946109323Ssam sc->iobase = NULL; 2947109323Ssam } 2948109323Ssam if (sc->irq != NULL) { 2949109323Ssam bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq); 2950109323Ssam sc->irq = NULL; 2951109323Ssam } 2952109323Ssam if (sc->mem != NULL) { 2953109323Ssam bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem); 2954109323Ssam sc->mem = NULL; 2955109323Ssam } 2956109323Ssam 2957109323Ssam return; 2958109323Ssam} 2959109323Ssam 296093359Simpstatic int 2961109323Ssamwi_get_debug(struct wi_softc *sc, struct wi_req *wreq) 296293359Simp{ 2963109323Ssam int error = 0; 296493359Simp 296593359Simp wreq->wi_len = 1; 296693359Simp 296793359Simp switch (wreq->wi_type) { 296893359Simp case WI_DEBUG_SLEEP: 296993359Simp wreq->wi_len++; 297093359Simp wreq->wi_val[0] = sc->wi_debug.wi_sleep; 297193359Simp break; 297293359Simp case WI_DEBUG_DELAYSUPP: 297393359Simp wreq->wi_len++; 297493359Simp wreq->wi_val[0] = sc->wi_debug.wi_delaysupp; 297593359Simp break; 297693359Simp case WI_DEBUG_TXSUPP: 297793359Simp wreq->wi_len++; 297893359Simp wreq->wi_val[0] = sc->wi_debug.wi_txsupp; 297993359Simp break; 298093359Simp case WI_DEBUG_MONITOR: 298193359Simp wreq->wi_len++; 298293359Simp wreq->wi_val[0] = sc->wi_debug.wi_monitor; 298393359Simp break; 298493359Simp case WI_DEBUG_LEDTEST: 298593359Simp wreq->wi_len += 3; 298693359Simp wreq->wi_val[0] = sc->wi_debug.wi_ledtest; 298793359Simp wreq->wi_val[1] = sc->wi_debug.wi_ledtest_param0; 298893359Simp wreq->wi_val[2] = sc->wi_debug.wi_ledtest_param1; 298993359Simp break; 299093359Simp case WI_DEBUG_CONTTX: 299193359Simp wreq->wi_len += 2; 299293359Simp wreq->wi_val[0] = sc->wi_debug.wi_conttx; 299393359Simp wreq->wi_val[1] = sc->wi_debug.wi_conttx_param0; 299493359Simp break; 299593359Simp case WI_DEBUG_CONTRX: 299693359Simp wreq->wi_len++; 299793359Simp wreq->wi_val[0] = sc->wi_debug.wi_contrx; 299893359Simp break; 299993359Simp case WI_DEBUG_SIGSTATE: 300093359Simp wreq->wi_len += 2; 300193359Simp wreq->wi_val[0] = sc->wi_debug.wi_sigstate; 300293359Simp wreq->wi_val[1] = sc->wi_debug.wi_sigstate_param0; 300393359Simp break; 300493359Simp case WI_DEBUG_CONFBITS: 300593359Simp wreq->wi_len += 2; 300693359Simp wreq->wi_val[0] = sc->wi_debug.wi_confbits; 300793359Simp wreq->wi_val[1] = sc->wi_debug.wi_confbits_param0; 300893359Simp break; 300993359Simp default: 301093359Simp error = EIO; 301193359Simp break; 301293359Simp } 301393359Simp 301493359Simp return (error); 301593359Simp} 301693359Simp 301793359Simpstatic int 3018109323Ssamwi_set_debug(struct wi_softc *sc, struct wi_req *wreq) 301993359Simp{ 3020109323Ssam int error = 0; 302193359Simp u_int16_t cmd, param0 = 0, param1 = 0; 302293359Simp 302393359Simp switch (wreq->wi_type) { 302493359Simp case WI_DEBUG_RESET: 302593359Simp case WI_DEBUG_INIT: 302693359Simp case WI_DEBUG_CALENABLE: 302793359Simp break; 302893359Simp case WI_DEBUG_SLEEP: 302993359Simp sc->wi_debug.wi_sleep = 1; 303093359Simp break; 303193359Simp case WI_DEBUG_WAKE: 303293359Simp sc->wi_debug.wi_sleep = 0; 303393359Simp break; 303493359Simp case WI_DEBUG_CHAN: 303593359Simp param0 = wreq->wi_val[0]; 303693359Simp break; 303793359Simp case WI_DEBUG_DELAYSUPP: 303893359Simp sc->wi_debug.wi_delaysupp = 1; 303993359Simp break; 304093359Simp case WI_DEBUG_TXSUPP: 304193359Simp sc->wi_debug.wi_txsupp = 1; 304293359Simp break; 304393359Simp case WI_DEBUG_MONITOR: 304493359Simp sc->wi_debug.wi_monitor = 1; 304593359Simp break; 304693359Simp case WI_DEBUG_LEDTEST: 304793359Simp param0 = wreq->wi_val[0]; 304893359Simp param1 = wreq->wi_val[1]; 304993359Simp sc->wi_debug.wi_ledtest = 1; 305093359Simp sc->wi_debug.wi_ledtest_param0 = param0; 305193359Simp sc->wi_debug.wi_ledtest_param1 = param1; 305293359Simp break; 305393359Simp case WI_DEBUG_CONTTX: 305493359Simp param0 = wreq->wi_val[0]; 305593359Simp sc->wi_debug.wi_conttx = 1; 305693359Simp sc->wi_debug.wi_conttx_param0 = param0; 305793359Simp break; 305893359Simp case WI_DEBUG_STOPTEST: 305993359Simp sc->wi_debug.wi_delaysupp = 0; 306093359Simp sc->wi_debug.wi_txsupp = 0; 306193359Simp sc->wi_debug.wi_monitor = 0; 306293359Simp sc->wi_debug.wi_ledtest = 0; 306393359Simp sc->wi_debug.wi_ledtest_param0 = 0; 306493359Simp sc->wi_debug.wi_ledtest_param1 = 0; 306593359Simp sc->wi_debug.wi_conttx = 0; 306693359Simp sc->wi_debug.wi_conttx_param0 = 0; 306793359Simp sc->wi_debug.wi_contrx = 0; 306893359Simp sc->wi_debug.wi_sigstate = 0; 306993359Simp sc->wi_debug.wi_sigstate_param0 = 0; 307093359Simp break; 307193359Simp case WI_DEBUG_CONTRX: 307293359Simp sc->wi_debug.wi_contrx = 1; 307393359Simp break; 307493359Simp case WI_DEBUG_SIGSTATE: 307593359Simp param0 = wreq->wi_val[0]; 307693359Simp sc->wi_debug.wi_sigstate = 1; 307793359Simp sc->wi_debug.wi_sigstate_param0 = param0; 307893359Simp break; 307993359Simp case WI_DEBUG_CONFBITS: 308093359Simp param0 = wreq->wi_val[0]; 308193359Simp param1 = wreq->wi_val[1]; 308293359Simp sc->wi_debug.wi_confbits = param0; 308393359Simp sc->wi_debug.wi_confbits_param0 = param1; 308493359Simp break; 308593359Simp default: 308693359Simp error = EIO; 308793359Simp break; 308893359Simp } 308993359Simp 309093359Simp if (error) 309193359Simp return (error); 309293359Simp 309393359Simp cmd = WI_CMD_DEBUG | (wreq->wi_type << 8); 309493359Simp error = wi_cmd(sc, cmd, param0, param1, 0); 309593359Simp 309693359Simp return (error); 309793359Simp} 3098101903Simp 3099105076Simp#if __FreeBSD_version >= 500000 3100101903Simp/* 3101101903Simp * Special routines to download firmware for Symbol CF card. 3102101903Simp * XXX: This should be modified generic into any PRISM-2 based card. 3103101903Simp */ 3104101903Simp 3105101903Simp#define WI_SBCF_PDIADDR 0x3100 3106101903Simp 3107101903Simp/* unaligned load little endian */ 3108101903Simp#define GETLE32(p) ((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24)) 3109101903Simp#define GETLE16(p) ((p)[0] | ((p)[1]<<8)) 3110101903Simp 3111101903Simpint 3112101903Simpwi_symbol_load_firm(struct wi_softc *sc, const void *primsym, int primlen, 3113101903Simp const void *secsym, int seclen) 3114101903Simp{ 3115101903Simp uint8_t ebuf[256]; 3116101903Simp int i; 3117101903Simp 3118101903Simp /* load primary code and run it */ 3119101903Simp wi_symbol_set_hcr(sc, WI_HCR_EEHOLD); 3120101903Simp if (wi_symbol_write_firm(sc, primsym, primlen, NULL, 0)) 3121101903Simp return EIO; 3122101903Simp wi_symbol_set_hcr(sc, WI_HCR_RUN); 3123101903Simp for (i = 0; ; i++) { 3124101903Simp if (i == 10) 3125101903Simp return ETIMEDOUT; 3126101903Simp tsleep(sc, PWAIT, "wiinit", 1); 3127101903Simp if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT) 3128101903Simp break; 3129101903Simp /* write the magic key value to unlock aux port */ 3130101903Simp CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0); 3131101903Simp CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1); 3132101903Simp CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2); 3133101903Simp CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL); 3134101903Simp } 3135101903Simp 3136101903Simp /* issue read EEPROM command: XXX copied from wi_cmd() */ 3137101903Simp CSR_WRITE_2(sc, WI_PARAM0, 0); 3138101903Simp CSR_WRITE_2(sc, WI_PARAM1, 0); 3139101903Simp CSR_WRITE_2(sc, WI_PARAM2, 0); 3140101903Simp CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE); 3141101903Simp for (i = 0; i < WI_TIMEOUT; i++) { 3142101903Simp if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD) 3143101903Simp break; 3144101903Simp DELAY(1); 3145101903Simp } 3146101903Simp CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD); 3147101903Simp 3148101903Simp CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ); 3149101903Simp CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ); 3150101903Simp CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA, 3151101903Simp (uint16_t *)ebuf, sizeof(ebuf) / 2); 3152101903Simp if (GETLE16(ebuf) > sizeof(ebuf)) 3153101903Simp return EIO; 3154101903Simp if (wi_symbol_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf))) 3155101903Simp return EIO; 3156101903Simp return 0; 3157101903Simp} 3158101903Simp 3159101903Simpstatic int 3160101903Simpwi_symbol_write_firm(struct wi_softc *sc, const void *buf, int buflen, 3161101903Simp const void *ebuf, int ebuflen) 3162101903Simp{ 3163101903Simp const uint8_t *p, *ep, *q, *eq; 3164101903Simp char *tp; 3165101903Simp uint32_t addr, id, eid; 3166101903Simp int i, len, elen, nblk, pdrlen; 3167101903Simp 3168101903Simp /* 3169101903Simp * Parse the header of the firmware image. 3170101903Simp */ 3171101903Simp p = buf; 3172101903Simp ep = p + buflen; 3173101903Simp while (p < ep && *p++ != ' '); /* FILE: */ 3174101903Simp while (p < ep && *p++ != ' '); /* filename */ 3175101903Simp while (p < ep && *p++ != ' '); /* type of the firmware */ 3176101903Simp nblk = strtoul(p, &tp, 10); 3177101903Simp p = tp; 3178101903Simp pdrlen = strtoul(p + 1, &tp, 10); 3179101903Simp p = tp; 3180101903Simp while (p < ep && *p++ != 0x1a); /* skip rest of header */ 3181101903Simp 3182101903Simp /* 3183101903Simp * Block records: address[4], length[2], data[length]; 3184101903Simp */ 3185101903Simp for (i = 0; i < nblk; i++) { 3186101903Simp addr = GETLE32(p); p += 4; 3187101903Simp len = GETLE16(p); p += 2; 3188101903Simp CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ); 3189101903Simp CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ); 3190101903Simp CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA, 3191101903Simp (const uint16_t *)p, len / 2); 3192101903Simp p += len; 3193101903Simp } 3194101903Simp 3195101903Simp /* 3196101903Simp * PDR: id[4], address[4], length[4]; 3197101903Simp */ 3198101903Simp for (i = 0; i < pdrlen; ) { 3199101903Simp id = GETLE32(p); p += 4; i += 4; 3200101903Simp addr = GETLE32(p); p += 4; i += 4; 3201101903Simp len = GETLE32(p); p += 4; i += 4; 3202101903Simp /* replace PDR entry with the values from EEPROM, if any */ 3203101903Simp for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) { 3204101903Simp elen = GETLE16(q); q += 2; 3205101903Simp eid = GETLE16(q); q += 2; 3206101903Simp elen--; /* elen includes eid */ 3207101903Simp if (eid == 0) 3208101903Simp break; 3209101903Simp if (eid != id) 3210101903Simp continue; 3211101903Simp CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ); 3212101903Simp CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ); 3213101903Simp CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA, 3214101903Simp (const uint16_t *)q, len / 2); 3215101903Simp break; 3216101903Simp } 3217101903Simp } 3218101903Simp return 0; 3219101903Simp} 3220101903Simp 3221101903Simpstatic int 3222101903Simpwi_symbol_set_hcr(struct wi_softc *sc, int mode) 3223101903Simp{ 3224101903Simp uint16_t hcr; 3225101903Simp 3226101903Simp CSR_WRITE_2(sc, WI_COR, WI_COR_RESET); 3227101903Simp tsleep(sc, PWAIT, "wiinit", 1); 3228101903Simp hcr = CSR_READ_2(sc, WI_HCR); 3229101903Simp hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE); 3230101903Simp CSR_WRITE_2(sc, WI_HCR, hcr); 3231101903Simp tsleep(sc, PWAIT, "wiinit", 1); 3232101903Simp CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE); 3233101903Simp tsleep(sc, PWAIT, "wiinit", 1); 3234101903Simp return 0; 3235101903Simp} 3236105076Simp#endif 3237