if_wi.c revision 165087
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 165087 2006-12-11 00:35:51Z sam $"); 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> 7495533Smike#include <sys/endian.h> 7546492Swpaul#include <sys/sockio.h> 7646492Swpaul#include <sys/mbuf.h> 77164033Srwatson#include <sys/priv.h> 7883366Sjulian#include <sys/proc.h> 7946492Swpaul#include <sys/kernel.h> 8046492Swpaul#include <sys/socket.h> 8153702Swpaul#include <sys/module.h> 8253702Swpaul#include <sys/bus.h> 8394486Simp#include <sys/random.h> 8453702Swpaul#include <sys/syslog.h> 8553702Swpaul#include <sys/sysctl.h> 8646492Swpaul 8753702Swpaul#include <machine/bus.h> 8853702Swpaul#include <machine/resource.h> 89116951Ssam#include <machine/atomic.h> 9053702Swpaul#include <sys/rman.h> 9153702Swpaul 9246492Swpaul#include <net/if.h> 9346492Swpaul#include <net/if_arp.h> 9446492Swpaul#include <net/ethernet.h> 9546492Swpaul#include <net/if_dl.h> 9646492Swpaul#include <net/if_media.h> 9746492Swpaul#include <net/if_types.h> 9846492Swpaul 99116951Ssam#include <net80211/ieee80211_var.h> 100116951Ssam#include <net80211/ieee80211_ioctl.h> 101119784Ssam#include <net80211/ieee80211_radiotap.h> 102116951Ssam 10346492Swpaul#include <netinet/in.h> 10446492Swpaul#include <netinet/in_systm.h> 10546492Swpaul#include <netinet/in_var.h> 10646492Swpaul#include <netinet/ip.h> 10746492Swpaul#include <netinet/if_ether.h> 10846492Swpaul 10946492Swpaul#include <net/bpf.h> 11046492Swpaul 11170808Speter#include <dev/wi/if_wavelan_ieee.h> 112119784Ssam#include <dev/wi/if_wireg.h> 11393611Simp#include <dev/wi/if_wivar.h> 11446492Swpaul 115165087Ssamstatic void wi_start_locked(struct ifnet *); 11691693Simpstatic void wi_start(struct ifnet *); 117160991Ssamstatic int wi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr, 118160991Ssam struct mbuf *m0); 119160991Ssamstatic int wi_raw_xmit(struct ieee80211_node *, struct mbuf *, 120160991Ssam const struct ieee80211_bpf_params *); 121109323Ssamstatic int wi_reset(struct wi_softc *); 12291693Simpstatic void wi_watchdog(struct ifnet *); 123109323Ssamstatic int wi_ioctl(struct ifnet *, u_long, caddr_t); 124109323Ssamstatic int wi_media_change(struct ifnet *); 125109323Ssamstatic void wi_media_status(struct ifnet *, struct ifmediareq *); 12646492Swpaul 127109323Ssamstatic void wi_rx_intr(struct wi_softc *); 128109323Ssamstatic void wi_tx_intr(struct wi_softc *); 129109323Ssamstatic void wi_tx_ex_intr(struct wi_softc *); 130109323Ssamstatic void wi_info_intr(struct wi_softc *); 13146492Swpaul 132150798Savatarstatic int wi_key_alloc(struct ieee80211com *, const struct ieee80211_key *, 133150798Savatar ieee80211_keyix *, ieee80211_keyix *); 134150798Savatar 135109323Ssamstatic int wi_get_cfg(struct ifnet *, u_long, caddr_t); 136109323Ssamstatic int wi_set_cfg(struct ifnet *, u_long, caddr_t); 137109323Ssamstatic int wi_write_txrate(struct wi_softc *); 138109323Ssamstatic int wi_write_wep(struct wi_softc *); 139109323Ssamstatic int wi_write_multi(struct wi_softc *); 140109323Ssamstatic int wi_alloc_fid(struct wi_softc *, int, int *); 141109323Ssamstatic void wi_read_nicid(struct wi_softc *); 142109323Ssamstatic int wi_write_ssid(struct wi_softc *, int, u_int8_t *, int); 14353702Swpaul 144109323Ssamstatic int wi_cmd(struct wi_softc *, int, int, int, int); 145109323Ssamstatic int wi_seek_bap(struct wi_softc *, int, int); 146109323Ssamstatic int wi_read_bap(struct wi_softc *, int, int, void *, int); 147109323Ssamstatic int wi_write_bap(struct wi_softc *, int, int, void *, int); 148109323Ssamstatic int wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int); 149109323Ssamstatic int wi_read_rid(struct wi_softc *, int, void *, int *); 150109323Ssamstatic int wi_write_rid(struct wi_softc *, int, void *, int); 15177217Sphk 152117812Ssamstatic int wi_newstate(struct ieee80211com *, enum ieee80211_state, int); 153109323Ssam 154116898Ssamstatic int wi_scan_ap(struct wi_softc *, u_int16_t, u_int16_t); 155109323Ssamstatic void wi_scan_result(struct wi_softc *, int, int); 156109323Ssam 157109323Ssamstatic void wi_dump_pkt(struct wi_frame *, struct ieee80211_node *, int rssi); 158109323Ssam 15993359Simpstatic int wi_get_debug(struct wi_softc *, struct wi_req *); 16093359Simpstatic int wi_set_debug(struct wi_softc *, struct wi_req *); 16193359Simp 162101903Simp/* support to download firmware for symbol CF card */ 163101903Simpstatic int wi_symbol_write_firm(struct wi_softc *, const void *, int, 164101903Simp const void *, int); 165101903Simpstatic int wi_symbol_set_hcr(struct wi_softc *, int); 166101903Simp 167109323Ssamstatic __inline int 168109323Ssamwi_write_val(struct wi_softc *sc, int rid, u_int16_t val) 169109323Ssam{ 17053702Swpaul 171109323Ssam val = htole16(val); 172109323Ssam return wi_write_rid(sc, rid, &val, sizeof(val)); 173109323Ssam} 174109323Ssam 175109592SsamSYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters"); 176109592Ssam 177109323Ssamstatic struct timeval lasttxerror; /* time of last tx error msg */ 178109323Ssamstatic int curtxeps; /* current tx error msgs/sec */ 179111559Ssamstatic int wi_txerate = 0; /* tx error rate: max msgs/sec */ 180109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate, 181111559Ssam 0, "max tx error msgs/sec; 0 to disable msgs"); 182109323Ssam 183109323Ssam#define WI_DEBUG 184109323Ssam#ifdef WI_DEBUG 185109323Ssamstatic int wi_debug = 0; 186109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug, 187109592Ssam 0, "control debugging printfs"); 188109323Ssam 189109323Ssam#define DPRINTF(X) if (wi_debug) printf X 190109323Ssam#define DPRINTF2(X) if (wi_debug > 1) printf X 191109323Ssam#define IFF_DUMPPKTS(_ifp) \ 192109323Ssam (((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2)) 193109323Ssam#else 194109323Ssam#define DPRINTF(X) 195109323Ssam#define DPRINTF2(X) 196109323Ssam#define IFF_DUMPPKTS(_ifp) 0 197109323Ssam#endif 198109323Ssam 199109323Ssam#define WI_INTRS (WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO) 200109323Ssam 20193825Simpstruct wi_card_ident wi_card_ident[] = { 20293825Simp /* CARD_ID CARD_NAME FIRM_TYPE */ 20393825Simp { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT }, 20493825Simp { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT }, 20593825Simp { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT }, 20693825Simp { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL }, 20793825Simp { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL }, 20893825Simp { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL }, 20993825Simp { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL }, 21093825Simp { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL }, 21193825Simp { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL }, 21293825Simp { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL }, 21393825Simp { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL }, 21493825Simp { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL }, 21593825Simp { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 21693825Simp { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 217101355Simp { WI_NIC_3842_PCMCIA_ATL_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 218101355Simp { WI_NIC_3842_PCMCIA_ATS_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 21993825Simp { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 22093825Simp { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 221101355Simp { WI_NIC_3842_MINI_ATL_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 222101355Simp { WI_NIC_3842_MINI_ATS_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 22393825Simp { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 22493825Simp { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 225101355Simp { WI_NIC_3842_PCI_ATS_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 226101355Simp { WI_NIC_3842_PCI_ATL_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 22793825Simp { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 22893825Simp { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 229101355Simp { WI_NIC_P3_PCMCIA_ATL_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 230101355Simp { WI_NIC_P3_PCMCIA_ATS_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 23193825Simp { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 23293825Simp { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 233101355Simp { WI_NIC_P3_MINI_ATL_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 234101355Simp { WI_NIC_P3_MINI_ATS_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 23593825Simp { 0, NULL, 0 }, 23693825Simp}; 23793825Simp 238109323Ssamdevclass_t wi_devclass; 23946492Swpaul 24093611Simpint 241109323Ssamwi_attach(device_t dev) 24274906Salfred{ 243109323Ssam struct wi_softc *sc = device_get_softc(dev); 244109323Ssam struct ieee80211com *ic = &sc->sc_ic; 245147256Sbrooks struct ifnet *ifp; 246116951Ssam int i, nrates, buflen; 247109323Ssam u_int16_t val; 248109323Ssam u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE]; 249116951Ssam struct ieee80211_rateset *rs; 250109323Ssam static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = { 251109323Ssam 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 252109323Ssam }; 253109323Ssam int error; 25474906Salfred 255147256Sbrooks ifp = sc->sc_ifp = if_alloc(IFT_ETHER); 256147256Sbrooks if (ifp == NULL) { 257147256Sbrooks device_printf(dev, "can not if_alloc\n"); 258147256Sbrooks wi_free(dev); 259147256Sbrooks return (ENOSPC); 260147256Sbrooks } 261147256Sbrooks 262109323Ssam /* 263109323Ssam * NB: no locking is needed here; don't put it here 264109323Ssam * unless you can prove it! 265109323Ssam */ 266121697Ssam error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE, 26774998Swpaul wi_intr, sc, &sc->wi_intrhand); 26853702Swpaul 26953702Swpaul if (error) { 27053702Swpaul device_printf(dev, "bus_setup_intr() failed! (%d)\n", error); 27153702Swpaul wi_free(dev); 27253702Swpaul return (error); 27353702Swpaul } 27453702Swpaul 275109323Ssam mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 27693818Sjhb MTX_DEF | MTX_RECURSE); 27767092Swpaul 278138571Ssam sc->sc_firmware_type = WI_NOTYPE; 279123339Simp sc->wi_cmd_count = 500; 28046492Swpaul /* Reset the NIC. */ 281112096Simp if (wi_reset(sc) != 0) 282109323Ssam return ENXIO; /* XXX */ 28346492Swpaul 28476438Swpaul /* 28576438Swpaul * Read the station address. 28676438Swpaul * And do it twice. I've seen PRISM-based cards that return 28776438Swpaul * an error when trying to read it the first time, which causes 28876438Swpaul * the probe to fail. 28976438Swpaul */ 290109323Ssam buflen = IEEE80211_ADDR_LEN; 291109323Ssam error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen); 292109323Ssam if (error != 0) { 293109323Ssam buflen = IEEE80211_ADDR_LEN; 294109323Ssam error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen); 295109323Ssam } 296109323Ssam if (error || IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) { 297109323Ssam if (error != 0) 298109323Ssam device_printf(dev, "mac read failed %d\n", error); 299148714Simp else { 300109323Ssam device_printf(dev, "mac read failed (all zeros)\n"); 301148714Simp error = ENXIO; 302148714Simp } 30375149Simp wi_free(dev); 30475149Simp return (error); 30575149Simp } 30646492Swpaul 307109323Ssam /* Read NIC identification */ 308109323Ssam wi_read_nicid(sc); 30946492Swpaul 31046492Swpaul ifp->if_softc = sc; 311121816Sbrooks if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 31246492Swpaul ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 31346492Swpaul ifp->if_ioctl = wi_ioctl; 31446492Swpaul ifp->if_start = wi_start; 31546492Swpaul ifp->if_watchdog = wi_watchdog; 31646492Swpaul ifp->if_init = wi_init; 317132986Smlaier IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 318132986Smlaier ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 319132986Smlaier IFQ_SET_READY(&ifp->if_snd); 32046492Swpaul 321138571Ssam ic->ic_ifp = ifp; 322109323Ssam ic->ic_phytype = IEEE80211_T_DS; 323109323Ssam ic->ic_opmode = IEEE80211_M_STA; 324109323Ssam ic->ic_state = IEEE80211_S_INIT; 325138571Ssam ic->ic_caps = IEEE80211_C_PMGT 326138571Ssam | IEEE80211_C_WEP /* everyone supports WEP */ 327138571Ssam ; 328138571Ssam ic->ic_max_aid = WI_MAX_AID; 32946492Swpaul 330116951Ssam /* 331116951Ssam * Query the card for available channels and setup the 332116951Ssam * channel table. We assume these are all 11b channels. 333116951Ssam */ 334109323Ssam buflen = sizeof(val); 335109323Ssam if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0) 336109323Ssam val = htole16(0x1fff); /* assume 1-11 */ 337116951Ssam KASSERT(val != 0, ("wi_attach: no available channels listed!")); 338116951Ssam 339116951Ssam val <<= 1; /* shift for base 1 indices */ 340116951Ssam for (i = 1; i < 16; i++) { 341144986Smdodd if (!isset((u_int8_t*)&val, i)) 342144986Smdodd continue; 343144986Smdodd ic->ic_channels[i].ic_freq = 344144986Smdodd ieee80211_ieee2mhz(i, IEEE80211_CHAN_B); 345144986Smdodd ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B; 346109323Ssam } 34746492Swpaul 34846563Swpaul /* 34946563Swpaul * Read the default channel from the NIC. This may vary 35046563Swpaul * depending on the country where the NIC was purchased, so 35146563Swpaul * we can't hard-code a default and expect it to work for 35246563Swpaul * everyone. 353116951Ssam * 354116951Ssam * If no channel is specified, let the 802.11 code select. 35546563Swpaul */ 356109323Ssam buflen = sizeof(val); 357116951Ssam if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0) { 358116951Ssam val = le16toh(val); 359116951Ssam KASSERT(val < IEEE80211_CHAN_MAX && 360116951Ssam ic->ic_channels[val].ic_flags != 0, 361116951Ssam ("wi_attach: invalid own channel %u!", val)); 362116951Ssam ic->ic_ibss_chan = &ic->ic_channels[val]; 363117042Ssam } else { 364117042Ssam device_printf(dev, 365117042Ssam "WI_RID_OWN_CHNL failed, using first channel!\n"); 366117042Ssam ic->ic_ibss_chan = &ic->ic_channels[0]; 367109323Ssam } 36846563Swpaul 36956965Swpaul /* 37098440Simp * Set flags based on firmware version. 37198440Simp */ 37298440Simp switch (sc->sc_firmware_type) { 37398440Simp case WI_LUCENT: 374112363Simp sc->sc_ntxbuf = 1; 375109323Ssam sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE; 376109323Ssam#ifdef WI_HERMES_AUTOINC_WAR 377109323Ssam /* XXX: not confirmed, but never seen for recent firmware */ 378109323Ssam if (sc->sc_sta_firmware_ver < 40000) { 379109323Ssam sc->sc_flags |= WI_FLAGS_BUG_AUTOINC; 380109323Ssam } 381109323Ssam#endif 38298440Simp if (sc->sc_sta_firmware_ver >= 60000) 383109323Ssam sc->sc_flags |= WI_FLAGS_HAS_MOR; 384117855Ssam if (sc->sc_sta_firmware_ver >= 60006) { 385116951Ssam ic->ic_caps |= IEEE80211_C_IBSS; 386117855Ssam ic->ic_caps |= IEEE80211_C_MONITOR; 387117855Ssam } 388109323Ssam sc->sc_ibss_port = htole16(1); 389119784Ssam 390119784Ssam sc->sc_min_rssi = WI_LUCENT_MIN_RSSI; 391119784Ssam sc->sc_max_rssi = WI_LUCENT_MAX_RSSI; 392119784Ssam sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET; 39398440Simp break; 394109323Ssam 39598440Simp case WI_INTERSIL: 396112363Simp sc->sc_ntxbuf = WI_NTXBUF; 397109323Ssam sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR; 398109323Ssam sc->sc_flags |= WI_FLAGS_HAS_ROAMING; 399109323Ssam sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE; 400123339Simp /* 401123339Simp * Old firmware are slow, so give peace a chance. 402123339Simp */ 403123339Simp if (sc->sc_sta_firmware_ver < 10000) 404123339Simp sc->wi_cmd_count = 5000; 405109323Ssam if (sc->sc_sta_firmware_ver > 10101) 406109323Ssam sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST; 407117855Ssam if (sc->sc_sta_firmware_ver >= 800) { 408116951Ssam ic->ic_caps |= IEEE80211_C_IBSS; 409117855Ssam ic->ic_caps |= IEEE80211_C_MONITOR; 410117855Ssam } 411109396Simp /* 412109396Simp * version 0.8.3 and newer are the only ones that are known 413109396Simp * to currently work. Earlier versions can be made to work, 414109396Simp * at least according to the Linux driver. 415109396Simp */ 416100734Simp if (sc->sc_sta_firmware_ver >= 803) 417116951Ssam ic->ic_caps |= IEEE80211_C_HOSTAP; 418109323Ssam sc->sc_ibss_port = htole16(0); 419119784Ssam 420119784Ssam sc->sc_min_rssi = WI_PRISM_MIN_RSSI; 421119784Ssam sc->sc_max_rssi = WI_PRISM_MAX_RSSI; 422119784Ssam sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET; 42398440Simp break; 424109323Ssam 42598440Simp case WI_SYMBOL: 426112363Simp sc->sc_ntxbuf = 1; 427109323Ssam sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY; 42898440Simp if (sc->sc_sta_firmware_ver >= 25000) 429116951Ssam ic->ic_caps |= IEEE80211_C_IBSS; 430109323Ssam sc->sc_ibss_port = htole16(4); 431119784Ssam 432119784Ssam sc->sc_min_rssi = WI_PRISM_MIN_RSSI; 433119784Ssam sc->sc_max_rssi = WI_PRISM_MAX_RSSI; 434119784Ssam sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET; 43598440Simp break; 43698440Simp } 43798440Simp 43898440Simp /* 43956965Swpaul * Find out if we support WEP on this card. 44056965Swpaul */ 441109323Ssam buflen = sizeof(val); 442109323Ssam if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 && 443109323Ssam val != htole16(0)) 444116951Ssam ic->ic_caps |= IEEE80211_C_WEP; 44556965Swpaul 446109323Ssam /* Find supported rates. */ 447109323Ssam buflen = sizeof(ratebuf); 448116951Ssam rs = &ic->ic_sup_rates[IEEE80211_MODE_11B]; 449109323Ssam if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) { 450116951Ssam nrates = le16toh(*(u_int16_t *)ratebuf); 451116951Ssam if (nrates > IEEE80211_RATE_MAXSIZE) 452116951Ssam nrates = IEEE80211_RATE_MAXSIZE; 453116951Ssam rs->rs_nrates = 0; 454116951Ssam for (i = 0; i < nrates; i++) 455116951Ssam if (ratebuf[2+i]) 456116951Ssam rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i]; 457109323Ssam } else { 458109323Ssam /* XXX fallback on error? */ 459116951Ssam rs->rs_nrates = 0; 46098440Simp } 46177217Sphk 462109323Ssam buflen = sizeof(val); 463109323Ssam if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) && 464109323Ssam wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) { 465119784Ssam sc->sc_dbm_offset = le16toh(val); 466119784Ssam } 46746492Swpaul 468109323Ssam sc->sc_max_datalen = 2304; 469109323Ssam sc->sc_system_scale = 1; 470109323Ssam sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN; 471109323Ssam sc->sc_roaming_mode = 1; 47246492Swpaul 473109323Ssam sc->sc_portnum = WI_DEFAULT_PORT; 474109323Ssam sc->sc_authtype = WI_DEFAULT_AUTHTYPE; 47587383Simp 476109323Ssam bzero(sc->sc_nodename, sizeof(sc->sc_nodename)); 477109323Ssam sc->sc_nodelen = sizeof(WI_DEFAULT_NODENAME) - 1; 478109323Ssam bcopy(WI_DEFAULT_NODENAME, sc->sc_nodename, sc->sc_nodelen); 47987383Simp 480109323Ssam bzero(sc->sc_net_name, sizeof(sc->sc_net_name)); 481109323Ssam bcopy(WI_DEFAULT_NETNAME, sc->sc_net_name, 482109323Ssam sizeof(WI_DEFAULT_NETNAME) - 1); 48393733Simp 48493733Simp /* 485109323Ssam * Call MI attach routine. 48693733Simp */ 487138571Ssam ieee80211_ifattach(ic); 488117812Ssam /* override state transition method */ 489117812Ssam sc->sc_newstate = ic->ic_newstate; 490150798Savatar sc->sc_key_alloc = ic->ic_crypto.cs_key_alloc; 491150798Savatar ic->ic_crypto.cs_key_alloc = wi_key_alloc; 492117812Ssam ic->ic_newstate = wi_newstate; 493160991Ssam ic->ic_raw_xmit = wi_raw_xmit; 494138571Ssam ieee80211_media_init(ic, wi_media_change, wi_media_status); 495109323Ssam 496119784Ssam#if NBPFILTER > 0 497119784Ssam bpfattach2(ifp, DLT_IEEE802_11_RADIO, 498119784Ssam sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th), 499119784Ssam &sc->sc_drvbpf); 500119784Ssam /* 501119784Ssam * Initialize constant fields. 502127698Ssam * XXX make header lengths a multiple of 32-bits so subsequent 503127698Ssam * headers are properly aligned; this is a kludge to keep 504127698Ssam * certain applications happy. 505119784Ssam * 506119784Ssam * NB: the channel is setup each time we transition to the 507119784Ssam * RUN state to avoid filling it in for each frame. 508119784Ssam */ 509127698Ssam sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t)); 510127698Ssam sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len); 511127698Ssam sc->sc_tx_th.wt_ihdr.it_present = htole32(WI_TX_RADIOTAP_PRESENT); 512119784Ssam 513127698Ssam sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t)); 514127698Ssam sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len); 515127698Ssam sc->sc_rx_th.wr_ihdr.it_present = htole32(WI_RX_RADIOTAP_PRESENT); 516119784Ssam#endif 517138571Ssam 518138571Ssam if (bootverbose) 519138571Ssam ieee80211_announce(ic); 520138571Ssam 521109323Ssam return (0); 52287383Simp} 52387383Simp 524109323Ssamint 525109323Ssamwi_detach(device_t dev) 52646492Swpaul{ 527109323Ssam struct wi_softc *sc = device_get_softc(dev); 528147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 52946492Swpaul 530109323Ssam WI_LOCK(sc); 53146492Swpaul 532109323Ssam /* check if device was removed */ 533123098Simp sc->wi_gone |= !bus_child_present(dev); 53446492Swpaul 535109323Ssam wi_stop(ifp, 0); 536150678Sru WI_UNLOCK(sc); 53746492Swpaul 538119784Ssam#if NBPFILTER > 0 539119784Ssam bpfdetach(ifp); 540119784Ssam#endif 541138571Ssam ieee80211_ifdetach(&sc->sc_ic); 542109323Ssam bus_teardown_intr(dev, sc->irq, sc->wi_intrhand); 543150306Simp if_free(sc->sc_ifp); 544109323Ssam wi_free(dev); 545109323Ssam mtx_destroy(&sc->sc_mtx); 546109323Ssam return (0); 547109323Ssam} 54893359Simp 549109323Ssam#ifdef __NetBSD__ 550109323Ssamint 551109323Ssamwi_activate(struct device *self, enum devact act) 552109323Ssam{ 553109323Ssam struct wi_softc *sc = (struct wi_softc *)self; 554109323Ssam int rv = 0, s; 55593359Simp 556109323Ssam s = splnet(); 557109323Ssam switch (act) { 558109323Ssam case DVACT_ACTIVATE: 559109323Ssam rv = EOPNOTSUPP; 560109323Ssam break; 56193359Simp 562109323Ssam case DVACT_DEACTIVATE: 563147256Sbrooks if_deactivate(sc->sc_ifp); 564109323Ssam break; 565109323Ssam } 566109323Ssam splx(s); 567109323Ssam return rv; 568109323Ssam} 56993359Simp 570109323Ssamvoid 571109323Ssamwi_power(struct wi_softc *sc, int why) 572109323Ssam{ 573147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 574109323Ssam int s; 57593359Simp 576109323Ssam s = splnet(); 577109323Ssam switch (why) { 578109323Ssam case PWR_SUSPEND: 579109323Ssam case PWR_STANDBY: 580109323Ssam wi_stop(ifp, 1); 581109323Ssam break; 582109323Ssam case PWR_RESUME: 583109323Ssam if (ifp->if_flags & IFF_UP) { 584109323Ssam wi_init(ifp); 585109323Ssam (void)wi_intr(sc); 58694405Simp } 587109323Ssam break; 588109323Ssam case PWR_SOFTSUSPEND: 589109323Ssam case PWR_SOFTSTANDBY: 590109323Ssam case PWR_SOFTRESUME: 591109323Ssam break; 59293359Simp } 593109323Ssam splx(s); 59446492Swpaul} 595109323Ssam#endif /* __NetBSD__ */ 59646492Swpaul 597109323Ssamvoid 598109323Ssamwi_shutdown(device_t dev) 59946492Swpaul{ 600109323Ssam struct wi_softc *sc = device_get_softc(dev); 60146492Swpaul 602147256Sbrooks wi_stop(sc->sc_ifp, 1); 60346492Swpaul} 60446492Swpaul 605109323Ssamvoid 606109323Ssamwi_intr(void *arg) 60746492Swpaul{ 608109323Ssam struct wi_softc *sc = arg; 609147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 610112363Simp u_int16_t status; 61146492Swpaul 612109323Ssam WI_LOCK(sc); 61346492Swpaul 614123098Simp if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) { 615113327Simp CSR_WRITE_2(sc, WI_INT_EN, 0); 616123098Simp CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF); 617109323Ssam WI_UNLOCK(sc); 61846492Swpaul return; 619109323Ssam } 62046492Swpaul 621112363Simp /* Disable interrupts. */ 622112363Simp CSR_WRITE_2(sc, WI_INT_EN, 0); 62346492Swpaul 624112363Simp status = CSR_READ_2(sc, WI_EVENT_STAT); 625112363Simp if (status & WI_EV_RX) 626112363Simp wi_rx_intr(sc); 627112363Simp if (status & WI_EV_ALLOC) 628112363Simp wi_tx_intr(sc); 629112363Simp if (status & WI_EV_TX_EXC) 630112363Simp wi_tx_ex_intr(sc); 631112363Simp if (status & WI_EV_INFO) 632112363Simp wi_info_intr(sc); 633148887Srwatson if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 && 634112363Simp (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 && 635132986Smlaier !IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 636165087Ssam wi_start_locked(ifp); 63746492Swpaul 638112363Simp /* Re-enable interrupts. */ 639112363Simp CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 64046492Swpaul 641109323Ssam WI_UNLOCK(sc); 64246492Swpaul 64346492Swpaul return; 64446492Swpaul} 64546492Swpaul 646109323Ssamvoid 647109323Ssamwi_init(void *arg) 64846492Swpaul{ 649109323Ssam struct wi_softc *sc = arg; 650147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 651109323Ssam struct ieee80211com *ic = &sc->sc_ic; 652109323Ssam struct wi_joinreq join; 653109323Ssam int i; 654109323Ssam int error = 0, wasenabled; 65546492Swpaul 656109323Ssam WI_LOCK(sc); 65767092Swpaul 658109323Ssam if (sc->wi_gone) { 659109323Ssam WI_UNLOCK(sc); 66046492Swpaul return; 66146492Swpaul } 66246492Swpaul 663112362Simp if ((wasenabled = sc->sc_enabled)) 664123098Simp wi_stop(ifp, 1); 665112362Simp wi_reset(sc); 66646492Swpaul 667109323Ssam /* common 802.11 configuration */ 668109323Ssam ic->ic_flags &= ~IEEE80211_F_IBSSON; 669109323Ssam sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 670109323Ssam switch (ic->ic_opmode) { 671109323Ssam case IEEE80211_M_STA: 672109323Ssam wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS); 673109323Ssam break; 674109323Ssam case IEEE80211_M_IBSS: 675109323Ssam wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port); 676109323Ssam ic->ic_flags |= IEEE80211_F_IBSSON; 677109323Ssam break; 678109323Ssam case IEEE80211_M_AHDEMO: 679109323Ssam wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC); 680109323Ssam break; 681109323Ssam case IEEE80211_M_HOSTAP: 682122087Sgreen /* 683122087Sgreen * For PRISM cards, override the empty SSID, because in 684122087Sgreen * HostAP mode the controller will lock up otherwise. 685122087Sgreen */ 686122087Sgreen if (sc->sc_firmware_type == WI_INTERSIL && 687122087Sgreen ic->ic_des_esslen == 0) { 688122087Sgreen ic->ic_des_essid[0] = ' '; 689122087Sgreen ic->ic_des_esslen = 1; 690122087Sgreen } 691109323Ssam wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP); 692109323Ssam break; 693117855Ssam case IEEE80211_M_MONITOR: 694160991Ssam switch (sc->sc_firmware_type) { 695160991Ssam case WI_LUCENT: 696117855Ssam wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC); 697160991Ssam break; 698160991Ssam 699160991Ssam case WI_INTERSIL: 700160991Ssam wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_APSILENT); 701160991Ssam break; 702160991Ssam } 703160991Ssam 704117855Ssam wi_cmd(sc, WI_CMD_DEBUG | (WI_TEST_MONITOR << 8), 0, 0, 0); 705117855Ssam break; 70646492Swpaul } 70746492Swpaul 708109323Ssam /* Intersil interprets this RID as joining ESS even in IBSS mode */ 709109323Ssam if (sc->sc_firmware_type == WI_LUCENT && 710109323Ssam (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0) 711109323Ssam wi_write_val(sc, WI_RID_CREATE_IBSS, 1); 712109323Ssam else 713109323Ssam wi_write_val(sc, WI_RID_CREATE_IBSS, 0); 714109323Ssam wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval); 715109323Ssam wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid, 716109323Ssam ic->ic_des_esslen); 717116951Ssam wi_write_val(sc, WI_RID_OWN_CHNL, 718116951Ssam ieee80211_chan2ieee(ic, ic->ic_ibss_chan)); 719109323Ssam wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen); 72046492Swpaul 721152315Sru IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp)); 722109323Ssam wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN); 72375373Salfred 724138571Ssam if (ic->ic_caps & IEEE80211_C_PMGT) 725138571Ssam wi_write_val(sc, WI_RID_PM_ENABLED, 726138571Ssam (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0); 72746492Swpaul 728109323Ssam /* not yet common 802.11 configuration */ 729109323Ssam wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen); 730116951Ssam wi_write_val(sc, WI_RID_RTS_THRESH, ic->ic_rtsthreshold); 731109323Ssam if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) 732116951Ssam wi_write_val(sc, WI_RID_FRAG_THRESH, ic->ic_fragthreshold); 73346492Swpaul 734109323Ssam /* driver specific 802.11 configuration */ 735109323Ssam if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) 736109323Ssam wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale); 737109323Ssam if (sc->sc_flags & WI_FLAGS_HAS_ROAMING) 738109323Ssam wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode); 739109323Ssam if (sc->sc_flags & WI_FLAGS_HAS_MOR) 740109323Ssam wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven); 741109323Ssam wi_write_txrate(sc); 742109323Ssam wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen); 743160991Ssam wi_write_val(sc, WI_RID_ALT_RETRY_CNT, 0); /* for IEEE80211_BPF_NOACK */ 74446492Swpaul 745109323Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP && 746109323Ssam sc->sc_firmware_type == WI_INTERSIL) { 747148843Ssam wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_bintval); 748109323Ssam wi_write_val(sc, WI_RID_BASIC_RATE, 0x03); /* 1, 2 */ 749109323Ssam wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */ 750150797Savatar wi_write_val(sc, WI_RID_DTIM_PERIOD, ic->ic_dtim_period); 75146492Swpaul } 75246492Swpaul 753109323Ssam /* 754109323Ssam * Initialize promisc mode. 755109323Ssam * Being in the Host-AP mode causes a great 756109323Ssam * deal of pain if primisc mode is set. 757109323Ssam * Therefore we avoid confusing the firmware 758109323Ssam * and always reset promisc mode in Host-AP 759109323Ssam * mode. Host-AP sees all the packets anyway. 760109323Ssam */ 761109323Ssam if (ic->ic_opmode != IEEE80211_M_HOSTAP && 762109323Ssam (ifp->if_flags & IFF_PROMISC) != 0) { 763109323Ssam wi_write_val(sc, WI_RID_PROMISC, 1); 764109323Ssam } else { 765109323Ssam wi_write_val(sc, WI_RID_PROMISC, 0); 76675373Salfred } 76746492Swpaul 768109323Ssam /* Configure WEP. */ 769146884Savatar if (ic->ic_caps & IEEE80211_C_WEP) { 770146884Savatar sc->sc_cnfauthmode = ic->ic_bss->ni_authmode; 771109323Ssam wi_write_wep(sc); 772150798Savatar } else 773150798Savatar sc->sc_encryption = 0; 77467092Swpaul 775109323Ssam /* Set multicast filter. */ 776109323Ssam wi_write_multi(sc); 77746492Swpaul 778114124Simp /* Allocate fids for the card */ 779109323Ssam if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) { 780109323Ssam sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame); 781109323Ssam if (sc->sc_firmware_type == WI_SYMBOL) 782109323Ssam sc->sc_buflen = 1585; /* XXX */ 783112363Simp for (i = 0; i < sc->sc_ntxbuf; i++) { 784109323Ssam error = wi_alloc_fid(sc, sc->sc_buflen, 785109323Ssam &sc->sc_txd[i].d_fid); 786109323Ssam if (error) { 787109323Ssam device_printf(sc->sc_dev, 788109323Ssam "tx buffer allocation failed (error %u)\n", 789109323Ssam error); 790109323Ssam goto out; 791109323Ssam } 792109323Ssam sc->sc_txd[i].d_len = 0; 79370073Swpaul } 79470073Swpaul } 795109323Ssam sc->sc_txcur = sc->sc_txnext = 0; 79670073Swpaul 797109323Ssam /* Enable desired port */ 798109323Ssam wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0); 79946492Swpaul 800114124Simp sc->sc_enabled = 1; 801148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 802148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 803109323Ssam if (ic->ic_opmode == IEEE80211_M_AHDEMO || 804139542Ssam ic->ic_opmode == IEEE80211_M_IBSS || 805117855Ssam ic->ic_opmode == IEEE80211_M_MONITOR || 806109323Ssam ic->ic_opmode == IEEE80211_M_HOSTAP) 807139542Ssam ieee80211_create_ibss(ic, ic->ic_ibss_chan); 80846492Swpaul 809109323Ssam /* Enable interrupts */ 810109323Ssam CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 81146492Swpaul 812109323Ssam if (!wasenabled && 813109323Ssam ic->ic_opmode == IEEE80211_M_HOSTAP && 814109323Ssam sc->sc_firmware_type == WI_INTERSIL) { 815109323Ssam /* XXX: some card need to be re-enabled for hostap */ 816109323Ssam wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0); 817109323Ssam wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0); 81875149Simp } 81994397Simp 820109323Ssam if (ic->ic_opmode == IEEE80211_M_STA && 821109323Ssam ((ic->ic_flags & IEEE80211_F_DESBSSID) || 822116951Ssam ic->ic_des_chan != IEEE80211_CHAN_ANYC)) { 823109323Ssam memset(&join, 0, sizeof(join)); 824109323Ssam if (ic->ic_flags & IEEE80211_F_DESBSSID) 825109323Ssam IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid); 826116951Ssam if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) 827116951Ssam join.wi_chan = htole16( 828116951Ssam ieee80211_chan2ieee(ic, ic->ic_des_chan)); 829109323Ssam /* Lucent firmware does not support the JOIN RID. */ 830109323Ssam if (sc->sc_firmware_type != WI_LUCENT) 831109323Ssam wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join)); 83294397Simp } 83375373Salfred 834109323Ssam WI_UNLOCK(sc); 83546492Swpaul return; 836109323Ssamout: 837109323Ssam if (error) { 838109323Ssam if_printf(ifp, "interface not running\n"); 839123098Simp wi_stop(ifp, 1); 84070073Swpaul } 841110224Simp WI_UNLOCK(sc); 842109323Ssam DPRINTF(("wi_init: return %d\n", error)); 843109323Ssam return; 84446492Swpaul} 84546492Swpaul 846109323Ssamvoid 847109323Ssamwi_stop(struct ifnet *ifp, int disable) 84846492Swpaul{ 849109323Ssam struct wi_softc *sc = ifp->if_softc; 850138571Ssam struct ieee80211com *ic = &sc->sc_ic; 85146492Swpaul 852109323Ssam WI_LOCK(sc); 85374998Swpaul 854123098Simp DELAY(100000); 855123098Simp 856117812Ssam ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 857109323Ssam if (sc->sc_enabled && !sc->wi_gone) { 858109323Ssam CSR_WRITE_2(sc, WI_INT_EN, 0); 859109323Ssam wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0); 860109323Ssam if (disable) { 861109323Ssam#ifdef __NetBSD__ 862109323Ssam if (sc->sc_disable) 863109323Ssam (*sc->sc_disable)(sc); 864109323Ssam#endif 865116951Ssam sc->sc_enabled = 0; 86670073Swpaul } 867123098Simp } else if (sc->wi_gone && disable) /* gone --> not enabled */ 868123098Simp sc->sc_enabled = 0; 86970073Swpaul 870109323Ssam sc->sc_tx_timer = 0; 871109323Ssam sc->sc_scan_timer = 0; 872109323Ssam sc->sc_false_syns = 0; 873109323Ssam sc->sc_naps = 0; 874148887Srwatson ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE | IFF_DRV_RUNNING); 875109323Ssam ifp->if_timer = 0; 87646492Swpaul 877109323Ssam WI_UNLOCK(sc); 87846492Swpaul} 87946492Swpaul 880109323Ssamstatic void 881165087Ssamwi_start_locked(struct ifnet *ifp) 88246492Swpaul{ 883109323Ssam struct wi_softc *sc = ifp->if_softc; 884109323Ssam struct ieee80211com *ic = &sc->sc_ic; 885119150Ssam struct ieee80211_node *ni; 886109323Ssam struct ieee80211_frame *wh; 887138571Ssam struct ether_header *eh; 888109323Ssam struct mbuf *m0; 889109323Ssam struct wi_frame frmhdr; 890160991Ssam int cur; 89146492Swpaul 892165087Ssam WI_LOCK_ASSERT(sc); 89346492Swpaul 894165087Ssam if (sc->wi_gone) 895109323Ssam return; 896165087Ssam if (sc->sc_flags & WI_FLAGS_OUTRANGE) 897109323Ssam return; 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) { 905148887Srwatson ifp->if_drv_flags |= IFF_DRV_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); 933148887Srwatson ifp->if_drv_flags |= IFF_DRV_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 963159180Scsjp if (bpf_peers_present(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 */ 968150798Savatar if ((wh->i_fc[1] & IEEE80211_FC1_WEP) && 969150798Savatar (sc->sc_encryption & HOST_ENCRYPT)) { 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 982159320Savatar if (bpf_peers_present(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); 995138571Ssam if (ni != NULL) 996138571Ssam ieee80211_free_node(ni); 997160991Ssam if (wi_start_tx(ifp, &frmhdr, m0)) 998109323Ssam continue; 999160991Ssam sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf; 1000160991Ssam } 1001165087Ssam} 1002160991Ssam 1003165087Ssamstatic void 1004165087Ssamwi_start(struct ifnet *ifp) 1005165087Ssam{ 1006165087Ssam struct wi_softc *sc = ifp->if_softc; 1007165087Ssam 1008165087Ssam WI_LOCK(sc); 1009165087Ssam wi_start_locked(ifp); 1010160991Ssam WI_UNLOCK(sc); 1011160991Ssam} 1012160991Ssam 1013160991Ssamstatic int 1014160991Ssamwi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr, struct mbuf *m0) 1015160991Ssam{ 1016160991Ssam struct wi_softc *sc = ifp->if_softc; 1017160991Ssam int cur = sc->sc_txnext; 1018160991Ssam int fid, off, error; 1019160991Ssam 1020160991Ssam fid = sc->sc_txd[cur].d_fid; 1021160991Ssam off = sizeof(*frmhdr); 1022160991Ssam error = wi_write_bap(sc, fid, 0, frmhdr, sizeof(*frmhdr)) != 0 1023160991Ssam || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0; 1024160991Ssam m_freem(m0); 1025160991Ssam if (error) { 1026160991Ssam ifp->if_oerrors++; 1027160991Ssam return -1; 1028160991Ssam } 1029160991Ssam sc->sc_txd[cur].d_len = off; 1030160991Ssam if (sc->sc_txcur == cur) { 1031160991Ssam if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) { 1032160991Ssam if_printf(ifp, "xmit failed\n"); 1033160991Ssam sc->sc_txd[cur].d_len = 0; 1034160991Ssam return -1; 1035109323Ssam } 1036160991Ssam sc->sc_tx_timer = 5; 1037160991Ssam ifp->if_timer = 1; 1038160991Ssam } 1039160991Ssam return 0; 1040160991Ssam} 1041160991Ssam 1042160991Ssamstatic int 1043160991Ssamwi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m0, 1044160991Ssam const struct ieee80211_bpf_params *params) 1045160991Ssam{ 1046160991Ssam struct ieee80211com *ic = ni->ni_ic; 1047160991Ssam struct ifnet *ifp = ic->ic_ifp; 1048160991Ssam struct wi_softc *sc = ifp->if_softc; 1049160991Ssam struct ieee80211_frame *wh; 1050160991Ssam struct wi_frame frmhdr; 1051160991Ssam int cur; 1052160991Ssam int rc = 0; 1053160991Ssam 1054160991Ssam WI_LOCK(sc); 1055160991Ssam 1056160991Ssam if (sc->wi_gone) { 1057160991Ssam rc = ENETDOWN; 1058160991Ssam goto out; 1059160991Ssam } 1060160991Ssam if (sc->sc_flags & WI_FLAGS_OUTRANGE) { 1061160991Ssam rc = ENETDOWN; 1062160991Ssam goto out; 1063160991Ssam } 1064160991Ssam 1065160991Ssam memset(&frmhdr, 0, sizeof(frmhdr)); 1066160991Ssam cur = sc->sc_txnext; 1067160991Ssam if (sc->sc_txd[cur].d_len != 0) { 1068160991Ssam ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1069160991Ssam rc = ENOBUFS; 1070160991Ssam goto out; 1071160991Ssam } 1072160991Ssam m0->m_pkthdr.rcvif = NULL; 1073160991Ssam 1074160991Ssam m_copydata(m0, 4, ETHER_ADDR_LEN * 2, 1075160991Ssam (caddr_t)&frmhdr.wi_ehdr); 1076160991Ssam frmhdr.wi_ehdr.ether_type = 0; 1077160991Ssam wh = mtod(m0, struct ieee80211_frame *); 1078160991Ssam 1079160991Ssam#if NBPFILTER > 0 1080160991Ssam if (bpf_peers_present(ic->ic_rawbpf)) 1081160991Ssam bpf_mtap(ic->ic_rawbpf, m0); 1082160991Ssam#endif 1083160991Ssam frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX); 1084160991Ssam if (params && (params->ibp_flags & IEEE80211_BPF_NOACK)) 1085160991Ssam frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_ALTRTRY); 1086160991Ssam /* XXX check key for SWCRYPT instead of using operating mode */ 1087160991Ssam if ((wh->i_fc[1] & IEEE80211_FC1_WEP) && 1088160991Ssam (sc->sc_encryption & HOST_ENCRYPT)) { 1089160991Ssam if (!params || 1090160991Ssam (params && (params->ibp_flags & IEEE80211_BPF_CRYPTO))) { 1091160991Ssam struct ieee80211_key *k; 1092160991Ssam 1093160991Ssam k = ieee80211_crypto_encap(ic, ni, m0); 1094160991Ssam if (k == NULL) { 1095160991Ssam if (ni != NULL) 1096160991Ssam ieee80211_free_node(ni); 1097160991Ssam m_freem(m0); 1098160991Ssam rc = ENOMEM; 1099160991Ssam goto out; 1100109323Ssam } 1101160991Ssam frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT); 1102109323Ssam } 110374838Salfred } 1104160991Ssam#if NBPFILTER > 0 1105160991Ssam if (bpf_peers_present(sc->sc_drvbpf)) { 1106160991Ssam sc->sc_tx_th.wt_rate = 1107160991Ssam ni->ni_rates.rs_rates[ni->ni_txrate]; 1108160991Ssam bpf_mtap2(sc->sc_drvbpf, 1109160991Ssam &sc->sc_tx_th, sc->sc_tx_th_len, m0); 1110160991Ssam } 1111160991Ssam#endif 1112160991Ssam m_copydata(m0, 0, sizeof(struct ieee80211_frame), 1113160991Ssam (caddr_t)&frmhdr.wi_whdr); 1114160991Ssam m_adj(m0, sizeof(struct ieee80211_frame)); 1115160991Ssam frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len); 1116160991Ssam if (IFF_DUMPPKTS(ifp)) 1117160991Ssam wi_dump_pkt(&frmhdr, NULL, -1); 1118160991Ssam if (ni != NULL) 1119160991Ssam ieee80211_free_node(ni); 1120160991Ssam rc = wi_start_tx(ifp, &frmhdr, m0); 1121160991Ssam if (rc) 1122160991Ssam goto out; 112346492Swpaul 1124160991Ssam sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf; 1125160991Ssamout: 1126109323Ssam WI_UNLOCK(sc); 1127160991Ssam 1128160991Ssam return rc; 112946492Swpaul} 113046492Swpaul 113188546Salfredstatic int 1132109323Ssamwi_reset(struct wi_softc *sc) 113346492Swpaul{ 1134147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1135114124Simp#define WI_INIT_TRIES 3 1136114124Simp int i; 1137114138Simp int error = 0; 1138114124Simp int tries; 1139114124Simp 1140114124Simp /* Symbol firmware cannot be initialized more than once */ 1141123098Simp if (sc->sc_firmware_type == WI_SYMBOL && sc->sc_reset) 1142112362Simp return (0); 1143114124Simp if (sc->sc_firmware_type == WI_SYMBOL) 1144114124Simp tries = 1; 1145114124Simp else 1146114124Simp tries = WI_INIT_TRIES; 1147112362Simp 1148114124Simp for (i = 0; i < tries; i++) { 1149109323Ssam if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0) 115046492Swpaul break; 1151109323Ssam DELAY(WI_DELAY * 1000); 115246492Swpaul } 1153114124Simp sc->sc_reset = 1; 115446492Swpaul 1155114124Simp if (i == tries) { 1156114124Simp if_printf(ifp, "init failed\n"); 1157114124Simp return (error); 115875373Salfred } 115946492Swpaul 1160109323Ssam CSR_WRITE_2(sc, WI_INT_EN, 0); 1161114124Simp CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF); 116246492Swpaul 1163109323Ssam /* Calibrate timer. */ 1164114124Simp wi_write_val(sc, WI_RID_TICK_TIME, 8); 1165114124Simp 1166114124Simp return (0); 1167109323Ssam#undef WI_INIT_TRIES 116846492Swpaul} 116946492Swpaul 117088546Salfredstatic void 1171109323Ssamwi_watchdog(struct ifnet *ifp) 117246492Swpaul{ 1173109323Ssam struct wi_softc *sc = ifp->if_softc; 117446492Swpaul 1175109323Ssam ifp->if_timer = 0; 1176109323Ssam if (!sc->sc_enabled) 1177109323Ssam return; 117846492Swpaul 1179109323Ssam if (sc->sc_tx_timer) { 1180109323Ssam if (--sc->sc_tx_timer == 0) { 1181109323Ssam if_printf(ifp, "device timeout\n"); 1182109323Ssam ifp->if_oerrors++; 1183109323Ssam wi_init(ifp->if_softc); 1184109323Ssam return; 1185109323Ssam } 1186109323Ssam ifp->if_timer = 1; 118746492Swpaul } 118846492Swpaul 1189109323Ssam if (sc->sc_scan_timer) { 1190109323Ssam if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT && 1191109323Ssam sc->sc_firmware_type == WI_INTERSIL) { 1192109323Ssam DPRINTF(("wi_watchdog: inquire scan\n")); 1193109323Ssam wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0); 119446492Swpaul } 1195109323Ssam if (sc->sc_scan_timer) 1196109323Ssam ifp->if_timer = 1; 119746492Swpaul } 119846492Swpaul 1199109323Ssam /* TODO: rate control */ 1200138571Ssam ieee80211_watchdog(&sc->sc_ic); 120146492Swpaul} 120246492Swpaul 120388546Salfredstatic int 1204109323Ssamwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 120546492Swpaul{ 1206109323Ssam struct wi_softc *sc = ifp->if_softc; 1207109323Ssam struct ieee80211com *ic = &sc->sc_ic; 1208109323Ssam struct ifreq *ifr = (struct ifreq *)data; 1209109323Ssam struct ieee80211req *ireq; 1210109323Ssam u_int8_t nodename[IEEE80211_NWID_LEN]; 1211109323Ssam int error = 0; 1212109323Ssam struct thread *td = curthread; 1213109323Ssam struct wi_req wreq; 121446492Swpaul 1215138571Ssam if (sc->wi_gone) 1216138571Ssam return (ENODEV); 121746492Swpaul 1218109323Ssam switch (cmd) { 121946492Swpaul case SIOCSIFFLAGS: 1220100876Simp /* 1221101139Simp * Can't do promisc and hostap at the same time. If all that's 1222101139Simp * changing is the promisc flag, try to short-circuit a call to 1223101139Simp * wi_init() by just setting PROMISC in the hardware. 1224100876Simp */ 1225138571Ssam WI_LOCK(sc); 122646492Swpaul if (ifp->if_flags & IFF_UP) { 1227109323Ssam if (ic->ic_opmode != IEEE80211_M_HOSTAP && 1228148887Srwatson ifp->if_drv_flags & IFF_DRV_RUNNING) { 1229101139Simp if (ifp->if_flags & IFF_PROMISC && 1230109323Ssam !(sc->sc_if_flags & IFF_PROMISC)) { 1231109323Ssam wi_write_val(sc, WI_RID_PROMISC, 1); 1232101139Simp } else if (!(ifp->if_flags & IFF_PROMISC) && 1233109323Ssam sc->sc_if_flags & IFF_PROMISC) { 1234109323Ssam wi_write_val(sc, WI_RID_PROMISC, 0); 1235101139Simp } else { 1236101139Simp wi_init(sc); 1237101139Simp } 1238100876Simp } else { 123946492Swpaul wi_init(sc); 1240100876Simp } 124146492Swpaul } else { 1242148887Srwatson if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 1243123098Simp wi_stop(ifp, 1); 124446492Swpaul } 1245123098Simp sc->wi_gone = 0; 124646492Swpaul } 1247109323Ssam sc->sc_if_flags = ifp->if_flags; 1248138571Ssam WI_UNLOCK(sc); 124946492Swpaul error = 0; 125046492Swpaul break; 125146492Swpaul case SIOCADDMULTI: 125246492Swpaul case SIOCDELMULTI: 1253138571Ssam WI_LOCK(sc); 1254109323Ssam error = wi_write_multi(sc); 1255138571Ssam WI_UNLOCK(sc); 125646492Swpaul break; 1257109323Ssam case SIOCGIFGENERIC: 1258138571Ssam WI_LOCK(sc); 1259109323Ssam error = wi_get_cfg(ifp, cmd, data); 1260138571Ssam WI_UNLOCK(sc); 126146492Swpaul break; 1262109323Ssam case SIOCSIFGENERIC: 1263164033Srwatson error = priv_check(td, PRIV_DRIVER); 1264138571Ssam if (error == 0) 1265138571Ssam error = wi_set_cfg(ifp, cmd, data); 126646492Swpaul break; 126793359Simp case SIOCGPRISM2DEBUG: 126893359Simp error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 126993359Simp if (error) 127093359Simp break; 1271148887Srwatson if (!(ifp->if_drv_flags & IFF_DRV_RUNNING) || 127293733Simp sc->sc_firmware_type == WI_LUCENT) { 127393359Simp error = EIO; 127493359Simp break; 127593359Simp } 127693359Simp error = wi_get_debug(sc, &wreq); 127793359Simp if (error == 0) 127893359Simp error = copyout(&wreq, ifr->ifr_data, sizeof(wreq)); 127993359Simp break; 128093359Simp case SIOCSPRISM2DEBUG: 1281164033Srwatson if ((error = priv_check(td, PRIV_DRIVER))) 1282138571Ssam return (error); 128393359Simp error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 128493359Simp if (error) 128593359Simp break; 1286138571Ssam WI_LOCK(sc); 128793359Simp error = wi_set_debug(sc, &wreq); 1288138571Ssam WI_UNLOCK(sc); 128993359Simp break; 129077217Sphk case SIOCG80211: 1291109323Ssam ireq = (struct ieee80211req *) data; 1292138571Ssam if (ireq->i_type == IEEE80211_IOC_STATIONNAME) { 1293109323Ssam ireq->i_len = sc->sc_nodelen + 1; 1294109323Ssam error = copyout(sc->sc_nodename, ireq->i_data, 1295109323Ssam ireq->i_len); 129677217Sphk break; 129777217Sphk } 1298138571Ssam goto ioctl_common; 129977217Sphk case SIOCS80211: 1300109323Ssam ireq = (struct ieee80211req *) data; 1301138571Ssam if (ireq->i_type == IEEE80211_IOC_STATIONNAME) { 1302164033Srwatson error = priv_check(td, PRIV_NET80211_MANAGE); 1303138571Ssam if (error) 1304138571Ssam break; 130577217Sphk if (ireq->i_val != 0 || 130677217Sphk ireq->i_len > IEEE80211_NWID_LEN) { 130777217Sphk error = EINVAL; 130877217Sphk break; 130977217Sphk } 1310109323Ssam memset(nodename, 0, IEEE80211_NWID_LEN); 1311109323Ssam error = copyin(ireq->i_data, nodename, ireq->i_len); 1312109323Ssam if (error) 131377217Sphk break; 1314138571Ssam WI_LOCK(sc); 1315109323Ssam if (sc->sc_enabled) { 1316109323Ssam error = wi_write_ssid(sc, WI_RID_NODENAME, 1317109323Ssam nodename, ireq->i_len); 131877217Sphk } 1319138571Ssam if (error == 0) { 1320138571Ssam memcpy(sc->sc_nodename, nodename, 1321138571Ssam IEEE80211_NWID_LEN); 1322138571Ssam sc->sc_nodelen = ireq->i_len; 1323138571Ssam } 1324138571Ssam WI_UNLOCK(sc); 132577217Sphk break; 132677217Sphk } 1327138571Ssam goto ioctl_common; 132846492Swpaul default: 1329138571Ssam ioctl_common: 1330138571Ssam WI_LOCK(sc); 1331138571Ssam error = ieee80211_ioctl(ic, cmd, data); 1332138571Ssam if (error == ENETRESET) { 1333138571Ssam if (sc->sc_enabled) 1334138571Ssam wi_init(sc); /* XXX no error return */ 1335138571Ssam error = 0; 1336138571Ssam } 1337138571Ssam WI_UNLOCK(sc); 133846492Swpaul break; 133946492Swpaul } 1340109323Ssam return (error); 134146492Swpaul} 134246492Swpaul 1343109323Ssamstatic int 1344109323Ssamwi_media_change(struct ifnet *ifp) 134546492Swpaul{ 1346109323Ssam struct wi_softc *sc = ifp->if_softc; 1347116951Ssam int error; 134846492Swpaul 1349116951Ssam error = ieee80211_media_change(ifp); 1350109323Ssam if (error == ENETRESET) { 1351109323Ssam if (sc->sc_enabled) 1352116951Ssam wi_init(sc); /* XXX no error return */ 1353109323Ssam error = 0; 1354109323Ssam } 1355109323Ssam return error; 1356109323Ssam} 135746492Swpaul 1358109323Ssamstatic void 1359109323Ssamwi_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1360109323Ssam{ 1361109323Ssam struct wi_softc *sc = ifp->if_softc; 1362109323Ssam struct ieee80211com *ic = &sc->sc_ic; 1363109323Ssam u_int16_t val; 1364109323Ssam int rate, len; 136546492Swpaul 1366149007Ssam if (sc->wi_gone) { /* hardware gone (e.g. ejected) */ 1367109323Ssam imr->ifm_active = IFM_IEEE80211 | IFM_NONE; 1368109323Ssam imr->ifm_status = 0; 1369109323Ssam return; 1370109323Ssam } 137146492Swpaul 1372109323Ssam imr->ifm_status = IFM_AVALID; 1373109323Ssam imr->ifm_active = IFM_IEEE80211; 1374149007Ssam if (!sc->sc_enabled) { /* port !enabled, have no status */ 1375149007Ssam imr->ifm_active |= IFM_NONE; 1376149007Ssam imr->ifm_status = IFM_AVALID; 1377149007Ssam return; 1378149007Ssam } 1379109323Ssam if (ic->ic_state == IEEE80211_S_RUN && 1380109323Ssam (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0) 1381109323Ssam imr->ifm_status |= IFM_ACTIVE; 1382109323Ssam len = sizeof(val); 1383138571Ssam if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) == 0 && 1384138571Ssam len == sizeof(val)) { 1385109323Ssam /* convert to 802.11 rate */ 1386138571Ssam val = le16toh(val); 1387109323Ssam rate = val * 2; 1388109323Ssam if (sc->sc_firmware_type == WI_LUCENT) { 1389138571Ssam if (rate == 10) 1390109323Ssam rate = 11; /* 5.5Mbps */ 1391109323Ssam } else { 1392109323Ssam if (rate == 4*2) 1393109323Ssam rate = 11; /* 5.5Mbps */ 1394109323Ssam else if (rate == 8*2) 1395109323Ssam rate = 22; /* 11Mbps */ 1396109323Ssam } 1397138571Ssam } else 1398138571Ssam rate = 0; 1399116951Ssam imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B); 1400109323Ssam switch (ic->ic_opmode) { 1401109323Ssam case IEEE80211_M_STA: 1402109323Ssam break; 1403109323Ssam case IEEE80211_M_IBSS: 1404109323Ssam imr->ifm_active |= IFM_IEEE80211_ADHOC; 1405109323Ssam break; 1406109323Ssam case IEEE80211_M_AHDEMO: 1407109323Ssam imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0; 1408109323Ssam break; 1409109323Ssam case IEEE80211_M_HOSTAP: 1410109323Ssam imr->ifm_active |= IFM_IEEE80211_HOSTAP; 1411109323Ssam break; 1412117855Ssam case IEEE80211_M_MONITOR: 1413117855Ssam imr->ifm_active |= IFM_IEEE80211_MONITOR; 1414117855Ssam break; 1415109323Ssam } 1416109323Ssam} 141746492Swpaul 1418109323Ssamstatic void 1419109323Ssamwi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN]) 1420109323Ssam{ 1421109323Ssam struct ieee80211com *ic = &sc->sc_ic; 1422116951Ssam struct ieee80211_node *ni = ic->ic_bss; 1423147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 142498440Simp 1425109323Ssam if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid)) 1426109323Ssam return; 142746492Swpaul 1428109323Ssam DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid))); 1429109323Ssam DPRINTF(("%s ?\n", ether_sprintf(new_bssid))); 143046492Swpaul 1431109323Ssam /* In promiscuous mode, the BSSID field is not a reliable 1432109323Ssam * indicator of the firmware's BSSID. Damp spurious 1433109323Ssam * change-of-BSSID indications. 1434109323Ssam */ 1435109323Ssam if ((ifp->if_flags & IFF_PROMISC) != 0 && 1436138571Ssam !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns, 1437138571Ssam WI_MAX_FALSE_SYNS)) 1438109323Ssam return; 143946492Swpaul 1440139542Ssam sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1); 1441139542Ssam /* 1442139542Ssam * XXX hack; we should create a new node with the new bssid 1443139542Ssam * and replace the existing ic_bss with it but since we don't 1444139542Ssam * process management frames to collect state we cheat by 1445139542Ssam * reusing the existing node as we know wi_newstate will be 1446139542Ssam * called and it will overwrite the node state. 1447139542Ssam */ 1448139542Ssam ieee80211_sta_join(ic, ieee80211_ref_node(ni)); 1449109323Ssam} 145046492Swpaul 1451109323Ssamstatic void 1452116898Ssamwi_rx_monitor(struct wi_softc *sc, int fid) 1453116898Ssam{ 1454147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1455116898Ssam struct wi_frame *rx_frame; 1456116898Ssam struct mbuf *m; 1457116898Ssam int datlen, hdrlen; 1458116898Ssam 1459116898Ssam /* first allocate mbuf for packet storage */ 1460116898Ssam m = m_getcl(M_DONTWAIT, MT_DATA, 0); 1461116898Ssam if (m == NULL) { 1462116898Ssam ifp->if_ierrors++; 1463116898Ssam return; 1464116898Ssam } 1465116898Ssam 1466116898Ssam m->m_pkthdr.rcvif = ifp; 1467116898Ssam 1468116898Ssam /* now read wi_frame first so we know how much data to read */ 1469116898Ssam if (wi_read_bap(sc, fid, 0, mtod(m, caddr_t), sizeof(*rx_frame))) { 1470116898Ssam ifp->if_ierrors++; 1471116898Ssam goto done; 1472116898Ssam } 1473116898Ssam 1474116898Ssam rx_frame = mtod(m, struct wi_frame *); 1475116898Ssam 1476116898Ssam switch ((rx_frame->wi_status & WI_STAT_MAC_PORT) >> 8) { 1477116898Ssam case 7: 1478116898Ssam switch (rx_frame->wi_whdr.i_fc[0] & IEEE80211_FC0_TYPE_MASK) { 1479116898Ssam case IEEE80211_FC0_TYPE_DATA: 1480116898Ssam hdrlen = WI_DATA_HDRLEN; 1481116898Ssam datlen = rx_frame->wi_dat_len + WI_FCS_LEN; 1482116898Ssam break; 1483116898Ssam case IEEE80211_FC0_TYPE_MGT: 1484116898Ssam hdrlen = WI_MGMT_HDRLEN; 1485116898Ssam datlen = rx_frame->wi_dat_len + WI_FCS_LEN; 1486116898Ssam break; 1487116898Ssam case IEEE80211_FC0_TYPE_CTL: 1488116898Ssam /* 1489116898Ssam * prism2 cards don't pass control packets 1490116898Ssam * down properly or consistently, so we'll only 1491116898Ssam * pass down the header. 1492116898Ssam */ 1493116898Ssam hdrlen = WI_CTL_HDRLEN; 1494116898Ssam datlen = 0; 1495116898Ssam break; 1496116898Ssam default: 1497116898Ssam if_printf(ifp, "received packet of unknown type " 1498116898Ssam "on port 7\n"); 1499116898Ssam ifp->if_ierrors++; 1500116898Ssam goto done; 1501116898Ssam } 1502116898Ssam break; 1503116898Ssam case 0: 1504116898Ssam hdrlen = WI_DATA_HDRLEN; 1505116898Ssam datlen = rx_frame->wi_dat_len + WI_FCS_LEN; 1506116898Ssam break; 1507116898Ssam default: 1508116898Ssam if_printf(ifp, "received packet on invalid " 1509116898Ssam "port (wi_status=0x%x)\n", rx_frame->wi_status); 1510116898Ssam ifp->if_ierrors++; 1511116898Ssam goto done; 1512116898Ssam } 1513116898Ssam 1514116898Ssam if (hdrlen + datlen + 2 > MCLBYTES) { 1515116898Ssam if_printf(ifp, "oversized packet received " 1516116898Ssam "(wi_dat_len=%d, wi_status=0x%x)\n", 1517116898Ssam datlen, rx_frame->wi_status); 1518116898Ssam ifp->if_ierrors++; 1519116898Ssam goto done; 1520116898Ssam } 1521116898Ssam 1522116898Ssam if (wi_read_bap(sc, fid, hdrlen, mtod(m, caddr_t) + hdrlen, 1523116898Ssam datlen + 2) == 0) { 1524116898Ssam m->m_pkthdr.len = m->m_len = hdrlen + datlen; 1525116898Ssam ifp->if_ipackets++; 1526116898Ssam BPF_MTAP(ifp, m); /* Handle BPF listeners. */ 1527116898Ssam } else 1528116898Ssam ifp->if_ierrors++; 1529116898Ssamdone: 1530116898Ssam m_freem(m); 1531116898Ssam} 1532116898Ssam 1533116898Ssamstatic void 1534109323Ssamwi_rx_intr(struct wi_softc *sc) 1535109323Ssam{ 1536109323Ssam struct ieee80211com *ic = &sc->sc_ic; 1537147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1538109323Ssam struct wi_frame frmhdr; 1539109323Ssam struct mbuf *m; 1540109323Ssam struct ieee80211_frame *wh; 1541119150Ssam struct ieee80211_node *ni; 1542109323Ssam int fid, len, off, rssi; 1543109323Ssam u_int8_t dir; 1544109323Ssam u_int16_t status; 1545109323Ssam u_int32_t rstamp; 154646611Swpaul 1547109323Ssam fid = CSR_READ_2(sc, WI_RX_FID); 154846611Swpaul 1549116898Ssam if (sc->wi_debug.wi_monitor) { 1550116898Ssam /* 1551116898Ssam * If we are in monitor mode just 1552116898Ssam * read the data from the device. 1553116898Ssam */ 1554116898Ssam wi_rx_monitor(sc, fid); 1555116898Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1556116898Ssam return; 1557116898Ssam } 1558116898Ssam 1559109323Ssam /* First read in the frame header */ 1560109323Ssam if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) { 1561109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1562109323Ssam ifp->if_ierrors++; 1563109323Ssam DPRINTF(("wi_rx_intr: read fid %x failed\n", fid)); 1564109323Ssam return; 1565109323Ssam } 156691695Simp 1567109323Ssam if (IFF_DUMPPKTS(ifp)) 1568109323Ssam wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal); 156946492Swpaul 1570109323Ssam /* 1571109323Ssam * Drop undecryptable or packets with receive errors here 1572109323Ssam */ 1573109323Ssam status = le16toh(frmhdr.wi_status); 1574109323Ssam if (status & WI_STAT_ERRSTAT) { 1575109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1576109323Ssam ifp->if_ierrors++; 1577109323Ssam DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status)); 1578109323Ssam return; 1579109323Ssam } 1580109323Ssam rssi = frmhdr.wi_rx_signal; 1581109323Ssam rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) | 1582109323Ssam le16toh(frmhdr.wi_rx_tstamp1); 158346492Swpaul 1584109323Ssam len = le16toh(frmhdr.wi_dat_len); 1585109323Ssam off = ALIGN(sizeof(struct ieee80211_frame)); 158646563Swpaul 1587117855Ssam /* 1588117855Ssam * Sometimes the PRISM2.x returns bogusly large frames. Except 1589117855Ssam * in monitor mode, just throw them away. 1590117855Ssam */ 1591117855Ssam if (off + len > MCLBYTES) { 1592117855Ssam if (ic->ic_opmode != IEEE80211_M_MONITOR) { 1593117855Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1594117855Ssam ifp->if_ierrors++; 1595117855Ssam DPRINTF(("wi_rx_intr: oversized packet\n")); 1596117855Ssam return; 1597117855Ssam } else 1598117855Ssam len = 0; 1599117855Ssam } 1600117855Ssam 1601111119Simp MGETHDR(m, M_DONTWAIT, MT_DATA); 1602109323Ssam if (m == NULL) { 1603109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1604109323Ssam ifp->if_ierrors++; 1605109323Ssam DPRINTF(("wi_rx_intr: MGET failed\n")); 1606109323Ssam return; 1607109323Ssam } 1608109323Ssam if (off + len > MHLEN) { 1609111119Simp MCLGET(m, M_DONTWAIT); 1610109323Ssam if ((m->m_flags & M_EXT) == 0) { 1611109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1612109323Ssam m_freem(m); 1613109323Ssam ifp->if_ierrors++; 1614109323Ssam DPRINTF(("wi_rx_intr: MCLGET failed\n")); 1615109323Ssam return; 1616109323Ssam } 1617109323Ssam } 161846492Swpaul 1619109323Ssam m->m_data += off - sizeof(struct ieee80211_frame); 1620109323Ssam memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame)); 1621109323Ssam wi_read_bap(sc, fid, sizeof(frmhdr), 1622109323Ssam m->m_data + sizeof(struct ieee80211_frame), len); 1623109323Ssam m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len; 1624109323Ssam m->m_pkthdr.rcvif = ifp; 162594405Simp 1626109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 162746492Swpaul 1628109323Ssam#if NBPFILTER > 0 1629159180Scsjp if (bpf_peers_present(sc->sc_drvbpf)) { 1630123927Ssam /* XXX replace divide by table */ 1631123922Ssam sc->sc_rx_th.wr_rate = frmhdr.wi_rx_rate / 5; 1632127148Sgreen sc->sc_rx_th.wr_antsignal = frmhdr.wi_rx_signal; 1633127148Sgreen sc->sc_rx_th.wr_antnoise = frmhdr.wi_rx_silence; 1634123927Ssam sc->sc_rx_th.wr_flags = 0; 1635123927Ssam if (frmhdr.wi_status & WI_STAT_PCF) 1636123927Ssam sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_CFP; 1637138952Ssam /* XXX IEEE80211_RADIOTAP_F_WEP */ 1638123922Ssam bpf_mtap2(sc->sc_drvbpf, 1639127698Ssam &sc->sc_rx_th, sc->sc_rx_th_len, m); 164056965Swpaul } 1641109323Ssam#endif 1642160991Ssam wh = mtod(m, struct ieee80211_frame *); 1643160991Ssam if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1644160991Ssam /* 1645160991Ssam * WEP is decrypted by hardware and the IV 1646160991Ssam * is stripped. Clear WEP bit so we don't 1647160991Ssam * try to process it in ieee80211_input. 1648160991Ssam * XXX fix for TKIP, et. al. 1649160991Ssam */ 1650160991Ssam wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 1651160991Ssam } 165256965Swpaul 1653109323Ssam /* synchronize driver's BSSID with firmware's BSSID */ 1654109323Ssam dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 1655109323Ssam if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS) 1656109323Ssam wi_sync_bssid(sc, wh->i_addr3); 165746492Swpaul 1658119150Ssam /* 1659119150Ssam * Locate the node for sender, track state, and 1660119150Ssam * then pass this node (referenced) up to the 802.11 1661138571Ssam * layer for its use. 1662119150Ssam */ 1663138571Ssam ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *) wh); 1664119150Ssam /* 1665119150Ssam * Send frame up for processing. 1666119150Ssam */ 1667138571Ssam ieee80211_input(ic, m, ni, rssi, rstamp); 1668119150Ssam /* 1669119150Ssam * The frame may have caused the node to be marked for 1670119150Ssam * reclamation (e.g. in response to a DEAUTH message) 1671119150Ssam * so use free_node here instead of unref_node. 1672119150Ssam */ 1673138571Ssam ieee80211_free_node(ni); 1674109323Ssam} 167546492Swpaul 1676109323Ssamstatic void 1677109323Ssamwi_tx_ex_intr(struct wi_softc *sc) 1678109323Ssam{ 1679147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1680109323Ssam struct wi_frame frmhdr; 1681109323Ssam int fid; 168246492Swpaul 1683109323Ssam fid = CSR_READ_2(sc, WI_TX_CMP_FID); 1684109323Ssam /* Read in the frame header */ 1685109323Ssam if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) { 1686109323Ssam u_int16_t status = le16toh(frmhdr.wi_status); 168746492Swpaul 1688109323Ssam /* 1689109323Ssam * Spontaneous station disconnects appear as xmit 1690109323Ssam * errors. Don't announce them and/or count them 1691109323Ssam * as an output error. 1692109323Ssam */ 1693109323Ssam if ((status & WI_TXSTAT_DISCONNECT) == 0) { 1694109323Ssam if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) { 1695109323Ssam if_printf(ifp, "tx failed"); 1696109323Ssam if (status & WI_TXSTAT_RET_ERR) 1697109323Ssam printf(", retry limit exceeded"); 1698109323Ssam if (status & WI_TXSTAT_AGED_ERR) 1699109323Ssam printf(", max transmit lifetime exceeded"); 1700109323Ssam if (status & WI_TXSTAT_DISCONNECT) 1701109323Ssam printf(", port disconnected"); 1702109323Ssam if (status & WI_TXSTAT_FORM_ERR) 1703109323Ssam printf(", invalid format (data len %u src %6D)", 1704109323Ssam le16toh(frmhdr.wi_dat_len), 1705109323Ssam frmhdr.wi_ehdr.ether_shost, ":"); 1706109323Ssam if (status & ~0xf) 1707109323Ssam printf(", status=0x%x", status); 1708109323Ssam printf("\n"); 1709109323Ssam } 1710109323Ssam ifp->if_oerrors++; 1711109323Ssam } else { 1712109323Ssam DPRINTF(("port disconnected\n")); 1713109323Ssam ifp->if_collisions++; /* XXX */ 1714109323Ssam } 1715109323Ssam } else 1716109323Ssam DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid)); 1717109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC); 1718109323Ssam} 171946492Swpaul 1720109323Ssamstatic void 1721109323Ssamwi_tx_intr(struct wi_softc *sc) 1722109323Ssam{ 1723147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1724109323Ssam int fid, cur; 172594405Simp 1726123098Simp if (sc->wi_gone) 1727123098Simp return; 1728123098Simp 1729109323Ssam fid = CSR_READ_2(sc, WI_ALLOC_FID); 1730109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC); 173146492Swpaul 1732109323Ssam cur = sc->sc_txcur; 1733109323Ssam if (sc->sc_txd[cur].d_fid != fid) { 1734109323Ssam if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n", 1735109323Ssam fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext); 1736109323Ssam return; 1737109323Ssam } 1738109323Ssam sc->sc_tx_timer = 0; 1739109323Ssam sc->sc_txd[cur].d_len = 0; 1740112363Simp sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf; 1741109323Ssam if (sc->sc_txd[cur].d_len == 0) 1742148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1743109323Ssam else { 1744109323Ssam if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid, 1745109323Ssam 0, 0)) { 1746109323Ssam if_printf(ifp, "xmit failed\n"); 1747109323Ssam sc->sc_txd[cur].d_len = 0; 1748109323Ssam } else { 1749109323Ssam sc->sc_tx_timer = 5; 1750109323Ssam ifp->if_timer = 1; 1751109323Ssam } 1752109323Ssam } 175346492Swpaul} 175446492Swpaul 175588546Salfredstatic void 1756109323Ssamwi_info_intr(struct wi_softc *sc) 175794405Simp{ 1758109323Ssam struct ieee80211com *ic = &sc->sc_ic; 1759147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1760109323Ssam int i, fid, len, off; 1761109323Ssam u_int16_t ltbuf[2]; 1762109323Ssam u_int16_t stat; 1763109323Ssam u_int32_t *ptr; 176494405Simp 1765109323Ssam fid = CSR_READ_2(sc, WI_INFO_FID); 1766109323Ssam wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf)); 176794405Simp 1768109323Ssam switch (le16toh(ltbuf[1])) { 176994405Simp 1770109323Ssam case WI_INFO_LINK_STAT: 1771109323Ssam wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat)); 1772109323Ssam DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat))); 1773109323Ssam switch (le16toh(stat)) { 1774109323Ssam case WI_INFO_LINK_STAT_CONNECTED: 1775109323Ssam sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 1776109323Ssam if (ic->ic_state == IEEE80211_S_RUN && 1777109323Ssam ic->ic_opmode != IEEE80211_M_IBSS) 1778109323Ssam break; 1779109323Ssam /* FALLTHROUGH */ 1780109323Ssam case WI_INFO_LINK_STAT_AP_CHG: 1781117812Ssam ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1782109323Ssam break; 1783109323Ssam case WI_INFO_LINK_STAT_AP_INR: 1784109323Ssam sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 1785109323Ssam break; 1786109323Ssam case WI_INFO_LINK_STAT_AP_OOR: 1787109323Ssam if (sc->sc_firmware_type == WI_SYMBOL && 1788109323Ssam sc->sc_scan_timer > 0) { 1789109323Ssam if (wi_cmd(sc, WI_CMD_INQUIRE, 1790109323Ssam WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0) 1791109323Ssam sc->sc_scan_timer = 0; 1792109323Ssam break; 1793109323Ssam } 1794109323Ssam if (ic->ic_opmode == IEEE80211_M_STA) 1795109323Ssam sc->sc_flags |= WI_FLAGS_OUTRANGE; 1796109323Ssam break; 1797109323Ssam case WI_INFO_LINK_STAT_DISCONNECTED: 1798109323Ssam case WI_INFO_LINK_STAT_ASSOC_FAILED: 1799109323Ssam if (ic->ic_opmode == IEEE80211_M_STA) 1800117812Ssam ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 1801109323Ssam break; 1802109323Ssam } 1803109323Ssam break; 180494405Simp 1805109323Ssam case WI_INFO_COUNTERS: 1806109323Ssam /* some card versions have a larger stats structure */ 1807109323Ssam len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4); 1808109323Ssam ptr = (u_int32_t *)&sc->sc_stats; 1809109323Ssam off = sizeof(ltbuf); 1810109323Ssam for (i = 0; i < len; i++, off += 2, ptr++) { 1811109323Ssam wi_read_bap(sc, fid, off, &stat, sizeof(stat)); 1812109323Ssam#ifdef WI_HERMES_STATS_WAR 1813109323Ssam if (stat & 0xf000) 1814109323Ssam stat = ~stat; 1815109323Ssam#endif 1816109323Ssam *ptr += stat; 1817109323Ssam } 1818109323Ssam ifp->if_collisions = sc->sc_stats.wi_tx_single_retries + 1819109323Ssam sc->sc_stats.wi_tx_multi_retries + 1820109323Ssam sc->sc_stats.wi_tx_retry_limit; 1821109323Ssam break; 1822109323Ssam 1823109323Ssam case WI_INFO_SCAN_RESULTS: 1824109323Ssam case WI_INFO_HOST_SCAN_RESULTS: 1825109323Ssam wi_scan_result(sc, fid, le16toh(ltbuf[0])); 1826109323Ssam break; 1827109323Ssam 1828109323Ssam default: 1829109323Ssam DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid, 1830109323Ssam le16toh(ltbuf[1]), le16toh(ltbuf[0]))); 1831109323Ssam break; 183294405Simp } 1833109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO); 1834109323Ssam} 183594405Simp 1836109323Ssamstatic int 1837109323Ssamwi_write_multi(struct wi_softc *sc) 1838109323Ssam{ 1839147256Sbrooks struct ifnet *ifp = sc->sc_ifp; 1840109323Ssam int n; 1841109323Ssam struct ifmultiaddr *ifma; 1842109323Ssam struct wi_mcast mlist; 184394472Simp 1844109323Ssam if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 1845109323Ssamallmulti: 1846109323Ssam memset(&mlist, 0, sizeof(mlist)); 1847109323Ssam return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist, 1848109323Ssam sizeof(mlist)); 184994405Simp } 185094405Simp 1851109323Ssam n = 0; 1852148654Srwatson IF_ADDR_LOCK(ifp); 1853109323Ssam TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1854109323Ssam if (ifma->ifma_addr->sa_family != AF_LINK) 1855109323Ssam continue; 1856109323Ssam if (n >= 16) 1857109323Ssam goto allmulti; 1858109323Ssam IEEE80211_ADDR_COPY(&mlist.wi_mcast[n], 1859109323Ssam (LLADDR((struct sockaddr_dl *)ifma->ifma_addr))); 1860109323Ssam n++; 186194405Simp } 1862148654Srwatson IF_ADDR_UNLOCK(ifp); 1863109323Ssam return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist, 1864109323Ssam IEEE80211_ADDR_LEN * n); 186594405Simp} 186694405Simp 186794405Simpstatic void 1868109323Ssamwi_read_nicid(struct wi_softc *sc) 186946492Swpaul{ 1870109323Ssam struct wi_card_ident *id; 1871109323Ssam char *p; 1872109323Ssam int len; 1873109323Ssam u_int16_t ver[4]; 187446492Swpaul 1875109323Ssam /* getting chip identity */ 1876109323Ssam memset(ver, 0, sizeof(ver)); 1877109323Ssam len = sizeof(ver); 1878109323Ssam wi_read_rid(sc, WI_RID_CARD_ID, ver, &len); 1879109323Ssam device_printf(sc->sc_dev, "using "); 188046492Swpaul 1881109323Ssam sc->sc_firmware_type = WI_NOTYPE; 1882109323Ssam for (id = wi_card_ident; id->card_name != NULL; id++) { 1883109323Ssam if (le16toh(ver[0]) == id->card_id) { 1884109323Ssam printf("%s", id->card_name); 1885109323Ssam sc->sc_firmware_type = id->firm_type; 1886109323Ssam break; 1887109323Ssam } 188867092Swpaul } 1889109323Ssam if (sc->sc_firmware_type == WI_NOTYPE) { 1890109323Ssam if (le16toh(ver[0]) & 0x8000) { 1891109323Ssam printf("Unknown PRISM2 chip"); 1892109323Ssam sc->sc_firmware_type = WI_INTERSIL; 1893109323Ssam } else { 1894109323Ssam printf("Unknown Lucent chip"); 1895109323Ssam sc->sc_firmware_type = WI_LUCENT; 1896109323Ssam } 189767092Swpaul } 189846492Swpaul 1899109323Ssam /* get primary firmware version (Only Prism chips) */ 1900109323Ssam if (sc->sc_firmware_type != WI_LUCENT) { 1901109323Ssam memset(ver, 0, sizeof(ver)); 1902109323Ssam len = sizeof(ver); 1903109323Ssam wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len); 1904109323Ssam sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 + 1905109323Ssam le16toh(ver[3]) * 100 + le16toh(ver[1]); 190667092Swpaul } 190746492Swpaul 1908109323Ssam /* get station firmware version */ 1909109323Ssam memset(ver, 0, sizeof(ver)); 1910109323Ssam len = sizeof(ver); 1911109323Ssam wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len); 1912109323Ssam sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 + 1913109323Ssam le16toh(ver[3]) * 100 + le16toh(ver[1]); 1914109323Ssam if (sc->sc_firmware_type == WI_INTERSIL && 1915109323Ssam (sc->sc_sta_firmware_ver == 10102 || 1916109323Ssam sc->sc_sta_firmware_ver == 20102)) { 1917109323Ssam char ident[12]; 1918109323Ssam memset(ident, 0, sizeof(ident)); 1919109323Ssam len = sizeof(ident); 1920109323Ssam /* value should be the format like "V2.00-11" */ 1921109323Ssam if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 && 1922109323Ssam *(p = (char *)ident) >= 'A' && 1923109323Ssam p[2] == '.' && p[5] == '-' && p[8] == '\0') { 1924109323Ssam sc->sc_firmware_type = WI_SYMBOL; 1925109323Ssam sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 + 1926109323Ssam (p[3] - '0') * 1000 + (p[4] - '0') * 100 + 1927109323Ssam (p[6] - '0') * 10 + (p[7] - '0'); 192894405Simp } 192994405Simp } 1930109323Ssam printf("\n"); 1931109323Ssam device_printf(sc->sc_dev, "%s Firmware: ", 1932109323Ssam sc->sc_firmware_type == WI_LUCENT ? "Lucent" : 1933109323Ssam (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil")); 1934109323Ssam if (sc->sc_firmware_type != WI_LUCENT) /* XXX */ 1935109323Ssam printf("Primary (%u.%u.%u), ", 1936109323Ssam sc->sc_pri_firmware_ver / 10000, 1937109323Ssam (sc->sc_pri_firmware_ver % 10000) / 100, 1938109323Ssam sc->sc_pri_firmware_ver % 100); 1939109323Ssam printf("Station (%u.%u.%u)\n", 1940109323Ssam sc->sc_sta_firmware_ver / 10000, 1941109323Ssam (sc->sc_sta_firmware_ver % 10000) / 100, 1942109323Ssam sc->sc_sta_firmware_ver % 100); 1943109323Ssam} 194446492Swpaul 1945109323Ssamstatic int 1946109323Ssamwi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen) 1947109323Ssam{ 1948109323Ssam struct wi_ssid ssid; 194946492Swpaul 1950109323Ssam if (buflen > IEEE80211_NWID_LEN) 1951109323Ssam return ENOBUFS; 1952109323Ssam memset(&ssid, 0, sizeof(ssid)); 1953109323Ssam ssid.wi_len = htole16(buflen); 1954109323Ssam memcpy(ssid.wi_ssid, buf, buflen); 1955109323Ssam return wi_write_rid(sc, rid, &ssid, sizeof(ssid)); 1956109323Ssam} 195746492Swpaul 1958109323Ssamstatic int 1959109323Ssamwi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data) 1960109323Ssam{ 1961109323Ssam struct wi_softc *sc = ifp->if_softc; 1962109323Ssam struct ieee80211com *ic = &sc->sc_ic; 1963109323Ssam struct ifreq *ifr = (struct ifreq *)data; 1964109323Ssam struct wi_req wreq; 1965116898Ssam struct wi_scan_res *res; 1966122015Sgreen size_t reslen; 1967116898Ssam int len, n, error, mif, val, off, i; 196846492Swpaul 1969109323Ssam error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 1970109323Ssam if (error) 1971109323Ssam return error; 1972109323Ssam len = (wreq.wi_len - 1) * 2; 1973109323Ssam if (len < sizeof(u_int16_t)) 1974109323Ssam return ENOSPC; 1975109323Ssam if (len > sizeof(wreq.wi_val)) 1976109323Ssam len = sizeof(wreq.wi_val); 197746492Swpaul 1978109323Ssam switch (wreq.wi_type) { 197946492Swpaul 1980109323Ssam case WI_RID_IFACE_STATS: 1981109323Ssam memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats)); 1982109323Ssam if (len < sizeof(sc->sc_stats)) 1983109323Ssam error = ENOSPC; 1984109323Ssam else 1985109323Ssam len = sizeof(sc->sc_stats); 1986109323Ssam break; 198746492Swpaul 1988109323Ssam case WI_RID_ENCRYPTION: 1989109323Ssam case WI_RID_TX_CRYPT_KEY: 1990109323Ssam case WI_RID_DEFLT_CRYPT_KEYS: 1991109323Ssam case WI_RID_TX_RATE: 1992138571Ssam return ieee80211_cfgget(ic, cmd, data); 199346492Swpaul 1994109323Ssam case WI_RID_MICROWAVE_OVEN: 1995109323Ssam if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) { 1996109323Ssam error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1997109323Ssam &len); 1998109323Ssam break; 1999109323Ssam } 2000109323Ssam wreq.wi_val[0] = htole16(sc->sc_microwave_oven); 2001109323Ssam len = sizeof(u_int16_t); 2002109323Ssam break; 200346492Swpaul 2004109323Ssam case WI_RID_DBM_ADJUST: 2005109323Ssam if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) { 2006109323Ssam error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 2007109323Ssam &len); 2008109323Ssam break; 2009109323Ssam } 2010119784Ssam wreq.wi_val[0] = htole16(sc->sc_dbm_offset); 2011109323Ssam len = sizeof(u_int16_t); 2012109323Ssam break; 201346492Swpaul 2014109323Ssam case WI_RID_ROAMING_MODE: 2015109323Ssam if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) { 2016109323Ssam error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 2017109323Ssam &len); 2018109323Ssam break; 2019109323Ssam } 2020109323Ssam wreq.wi_val[0] = htole16(sc->sc_roaming_mode); 2021109323Ssam len = sizeof(u_int16_t); 2022109323Ssam break; 202346492Swpaul 2024109323Ssam case WI_RID_SYSTEM_SCALE: 2025109323Ssam if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) { 2026109323Ssam error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 2027109323Ssam &len); 2028109323Ssam break; 2029109323Ssam } 2030109323Ssam wreq.wi_val[0] = htole16(sc->sc_system_scale); 2031109323Ssam len = sizeof(u_int16_t); 2032109323Ssam break; 203346492Swpaul 2034109323Ssam case WI_RID_FRAG_THRESH: 2035109323Ssam if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) { 2036109323Ssam error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 2037109323Ssam &len); 2038109323Ssam break; 2039109323Ssam } 2040116951Ssam wreq.wi_val[0] = htole16(ic->ic_fragthreshold); 2041109323Ssam len = sizeof(u_int16_t); 2042109323Ssam break; 204346492Swpaul 2044109323Ssam case WI_RID_READ_APS: 2045109323Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP) 2046138571Ssam return ieee80211_cfgget(ic, cmd, data); 2047109323Ssam if (sc->sc_scan_timer > 0) { 2048109323Ssam error = EINPROGRESS; 2049109323Ssam break; 2050109323Ssam } 2051109323Ssam n = sc->sc_naps; 2052109323Ssam if (len < sizeof(n)) { 2053109323Ssam error = ENOSPC; 2054109323Ssam break; 2055109323Ssam } 2056109323Ssam if (len < sizeof(n) + sizeof(struct wi_apinfo) * n) 2057109323Ssam n = (len - sizeof(n)) / sizeof(struct wi_apinfo); 2058109323Ssam len = sizeof(n) + sizeof(struct wi_apinfo) * n; 2059109323Ssam memcpy(wreq.wi_val, &n, sizeof(n)); 2060109323Ssam memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps, 2061109323Ssam sizeof(struct wi_apinfo) * n); 2062109323Ssam break; 206346492Swpaul 2064109323Ssam case WI_RID_PRISM2: 2065109323Ssam wreq.wi_val[0] = sc->sc_firmware_type != WI_LUCENT; 2066109323Ssam len = sizeof(u_int16_t); 2067109323Ssam break; 206846492Swpaul 2069109323Ssam case WI_RID_MIF: 2070109323Ssam mif = wreq.wi_val[0]; 2071109323Ssam error = wi_cmd(sc, WI_CMD_READMIF, mif, 0, 0); 2072109323Ssam val = CSR_READ_2(sc, WI_RESP0); 2073109323Ssam wreq.wi_val[0] = val; 2074109323Ssam len = sizeof(u_int16_t); 2075109323Ssam break; 207646492Swpaul 2077109323Ssam case WI_RID_ZERO_CACHE: 2078109323Ssam case WI_RID_PROCFRAME: /* ignore for compatibility */ 2079109323Ssam /* XXX ??? */ 2080109323Ssam break; 208146492Swpaul 2082109323Ssam case WI_RID_READ_CACHE: 2083138571Ssam return ieee80211_cfgget(ic, cmd, data); 208446492Swpaul 2085116898Ssam case WI_RID_SCAN_RES: /* compatibility interface */ 2086116898Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP) 2087138571Ssam return ieee80211_cfgget(ic, cmd, data); 2088116898Ssam if (sc->sc_scan_timer > 0) { 2089116898Ssam error = EINPROGRESS; 2090116898Ssam break; 2091116898Ssam } 2092116898Ssam n = sc->sc_naps; 2093122015Sgreen if (sc->sc_firmware_type == WI_LUCENT) { 2094122015Sgreen off = 0; 2095122015Sgreen reslen = WI_WAVELAN_RES_SIZE; 2096122015Sgreen } else { 2097122015Sgreen off = sizeof(struct wi_scan_p2_hdr); 2098122015Sgreen reslen = WI_PRISM2_RES_SIZE; 2099122015Sgreen } 2100122015Sgreen if (len < off + reslen * n) 2101122015Sgreen n = (len - off) / reslen; 2102122015Sgreen len = off + reslen * n; 2103116898Ssam if (off != 0) { 2104116898Ssam struct wi_scan_p2_hdr *p2 = (struct wi_scan_p2_hdr *)wreq.wi_val; 2105116898Ssam /* 2106116898Ssam * Prepend Prism-specific header. 2107116898Ssam */ 2108116898Ssam if (len < sizeof(struct wi_scan_p2_hdr)) { 2109116898Ssam error = ENOSPC; 2110116898Ssam break; 2111116898Ssam } 2112116898Ssam p2 = (struct wi_scan_p2_hdr *)wreq.wi_val; 2113116898Ssam p2->wi_rsvd = 0; 2114116898Ssam p2->wi_reason = n; /* XXX */ 2115116898Ssam } 2116122015Sgreen for (i = 0; i < n; i++, off += reslen) { 2117116898Ssam const struct wi_apinfo *ap = &sc->sc_aps[i]; 2118116898Ssam 2119116898Ssam res = (struct wi_scan_res *)((char *)wreq.wi_val + off); 2120116898Ssam res->wi_chan = ap->channel; 2121116898Ssam res->wi_noise = ap->noise; 2122116898Ssam res->wi_signal = ap->signal; 2123116898Ssam IEEE80211_ADDR_COPY(res->wi_bssid, ap->bssid); 2124116898Ssam res->wi_interval = ap->interval; 2125116898Ssam res->wi_capinfo = ap->capinfo; 2126116898Ssam res->wi_ssid_len = ap->namelen; 2127116898Ssam memcpy(res->wi_ssid, ap->name, 2128116898Ssam IEEE80211_NWID_LEN); 2129116898Ssam if (sc->sc_firmware_type != WI_LUCENT) { 2130116898Ssam /* XXX not saved from Prism cards */ 2131116898Ssam memset(res->wi_srates, 0, 2132116898Ssam sizeof(res->wi_srates)); 2133116898Ssam res->wi_rate = ap->rate; 2134116898Ssam res->wi_rsvd = 0; 2135122015Sgreen } 2136116898Ssam } 2137116898Ssam break; 2138116898Ssam 2139109323Ssam default: 2140109323Ssam if (sc->sc_enabled) { 2141109323Ssam error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 2142109323Ssam &len); 2143109323Ssam break; 2144109323Ssam } 2145109323Ssam switch (wreq.wi_type) { 2146109323Ssam case WI_RID_MAX_DATALEN: 2147109323Ssam wreq.wi_val[0] = htole16(sc->sc_max_datalen); 2148109323Ssam len = sizeof(u_int16_t); 2149109323Ssam break; 2150109323Ssam case WI_RID_RTS_THRESH: 2151116951Ssam wreq.wi_val[0] = htole16(ic->ic_rtsthreshold); 2152109323Ssam len = sizeof(u_int16_t); 2153109323Ssam break; 2154109323Ssam case WI_RID_CNFAUTHMODE: 2155109323Ssam wreq.wi_val[0] = htole16(sc->sc_cnfauthmode); 2156109323Ssam len = sizeof(u_int16_t); 2157109323Ssam break; 2158109323Ssam case WI_RID_NODENAME: 2159109323Ssam if (len < sc->sc_nodelen + sizeof(u_int16_t)) { 2160109323Ssam error = ENOSPC; 2161109323Ssam break; 2162109323Ssam } 2163109323Ssam len = sc->sc_nodelen + sizeof(u_int16_t); 2164109323Ssam wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2); 2165109323Ssam memcpy(&wreq.wi_val[1], sc->sc_nodename, 2166109323Ssam sc->sc_nodelen); 2167109323Ssam break; 2168109323Ssam default: 2169138571Ssam return ieee80211_cfgget(ic, cmd, data); 2170109323Ssam } 2171109323Ssam break; 217246492Swpaul } 2173109323Ssam if (error) 2174109323Ssam return error; 2175109323Ssam wreq.wi_len = (len + 1) / 2 + 1; 2176109323Ssam return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2); 217746492Swpaul} 217846492Swpaul 2179109323Ssamstatic int 2180109323Ssamwi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data) 218146492Swpaul{ 2182109323Ssam struct wi_softc *sc = ifp->if_softc; 2183109323Ssam struct ieee80211com *ic = &sc->sc_ic; 2184109323Ssam struct ifreq *ifr = (struct ifreq *)data; 2185109323Ssam struct wi_req wreq; 2186109323Ssam struct mbuf *m; 2187109323Ssam int i, len, error, mif, val; 2188116951Ssam struct ieee80211_rateset *rs; 218946492Swpaul 2190109323Ssam error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 2191109323Ssam if (error) 2192109323Ssam return error; 2193116898Ssam len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0; 2194109323Ssam switch (wreq.wi_type) { 2195109323Ssam case WI_RID_DBM_ADJUST: 2196109323Ssam return ENODEV; 219767092Swpaul 2198109323Ssam case WI_RID_NODENAME: 2199109323Ssam if (le16toh(wreq.wi_val[0]) * 2 > len || 2200109323Ssam le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) { 2201109323Ssam error = ENOSPC; 2202109323Ssam break; 2203109323Ssam } 2204138571Ssam WI_LOCK(sc); 2205138571Ssam if (sc->sc_enabled) 2206109323Ssam error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val, 2207109323Ssam len); 2208138571Ssam if (error == 0) { 2209138571Ssam sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2; 2210138571Ssam memcpy(sc->sc_nodename, &wreq.wi_val[1], 2211138571Ssam sc->sc_nodelen); 2212109323Ssam } 2213138571Ssam WI_UNLOCK(sc); 2214109323Ssam break; 221546492Swpaul 2216109323Ssam case WI_RID_MICROWAVE_OVEN: 2217109323Ssam case WI_RID_ROAMING_MODE: 2218109323Ssam case WI_RID_SYSTEM_SCALE: 2219109323Ssam case WI_RID_FRAG_THRESH: 2220138571Ssam /* XXX unlocked reads */ 2221109323Ssam if (wreq.wi_type == WI_RID_MICROWAVE_OVEN && 2222109323Ssam (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0) 2223109323Ssam break; 2224109323Ssam if (wreq.wi_type == WI_RID_ROAMING_MODE && 2225109323Ssam (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0) 2226109323Ssam break; 2227109323Ssam if (wreq.wi_type == WI_RID_SYSTEM_SCALE && 2228109323Ssam (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0) 2229109323Ssam break; 2230109323Ssam if (wreq.wi_type == WI_RID_FRAG_THRESH && 2231109323Ssam (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0) 2232109323Ssam break; 2233109323Ssam /* FALLTHROUGH */ 2234109323Ssam case WI_RID_RTS_THRESH: 2235109323Ssam case WI_RID_CNFAUTHMODE: 2236109323Ssam case WI_RID_MAX_DATALEN: 2237138571Ssam WI_LOCK(sc); 2238109323Ssam if (sc->sc_enabled) { 2239109323Ssam error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val, 2240109323Ssam sizeof(u_int16_t)); 2241138571Ssam if (error != 0) { 2242138571Ssam WI_UNLOCK(sc); 2243109323Ssam break; 2244138571Ssam } 2245109323Ssam } 2246109323Ssam switch (wreq.wi_type) { 2247109323Ssam case WI_RID_FRAG_THRESH: 2248116951Ssam ic->ic_fragthreshold = le16toh(wreq.wi_val[0]); 2249109323Ssam break; 2250109323Ssam case WI_RID_RTS_THRESH: 2251116951Ssam ic->ic_rtsthreshold = le16toh(wreq.wi_val[0]); 2252109323Ssam break; 2253109323Ssam case WI_RID_MICROWAVE_OVEN: 2254109323Ssam sc->sc_microwave_oven = le16toh(wreq.wi_val[0]); 2255109323Ssam break; 2256109323Ssam case WI_RID_ROAMING_MODE: 2257109323Ssam sc->sc_roaming_mode = le16toh(wreq.wi_val[0]); 2258109323Ssam break; 2259109323Ssam case WI_RID_SYSTEM_SCALE: 2260109323Ssam sc->sc_system_scale = le16toh(wreq.wi_val[0]); 2261109323Ssam break; 2262109323Ssam case WI_RID_CNFAUTHMODE: 2263109323Ssam sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]); 2264109323Ssam break; 2265109323Ssam case WI_RID_MAX_DATALEN: 2266109323Ssam sc->sc_max_datalen = le16toh(wreq.wi_val[0]); 2267109323Ssam break; 2268109323Ssam } 2269138571Ssam WI_UNLOCK(sc); 2270109323Ssam break; 227194405Simp 2272109323Ssam case WI_RID_TX_RATE: 2273138571Ssam WI_LOCK(sc); 2274109323Ssam switch (le16toh(wreq.wi_val[0])) { 2275109323Ssam case 3: 2276148290Ssam ic->ic_fixed_rate = IEEE80211_FIXED_RATE_NONE; 2277109323Ssam break; 2278109323Ssam default: 2279116951Ssam rs = &ic->ic_sup_rates[IEEE80211_MODE_11B]; 2280116951Ssam for (i = 0; i < rs->rs_nrates; i++) { 2281116951Ssam if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) 2282109323Ssam / 2 == le16toh(wreq.wi_val[0])) 2283109323Ssam break; 2284109323Ssam } 2285138571Ssam if (i == rs->rs_nrates) { 2286138571Ssam WI_UNLOCK(sc); 2287109323Ssam return EINVAL; 2288138571Ssam } 2289109323Ssam ic->ic_fixed_rate = i; 2290109323Ssam } 2291109323Ssam if (sc->sc_enabled) 2292109323Ssam error = wi_write_txrate(sc); 2293138571Ssam WI_UNLOCK(sc); 2294109323Ssam break; 229546492Swpaul 2296109323Ssam case WI_RID_SCAN_APS: 2297138571Ssam WI_LOCK(sc); 2298109323Ssam if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP) 2299116898Ssam error = wi_scan_ap(sc, 0x3fff, 0x000f); 2300138571Ssam WI_UNLOCK(sc); 2301109323Ssam break; 230246492Swpaul 2303116898Ssam case WI_RID_SCAN_REQ: /* compatibility interface */ 2304138571Ssam WI_LOCK(sc); 2305116898Ssam if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP) 2306116898Ssam error = wi_scan_ap(sc, wreq.wi_val[0], wreq.wi_val[1]); 2307138571Ssam WI_UNLOCK(sc); 2308116898Ssam break; 2309116898Ssam 2310109323Ssam case WI_RID_MGMT_XMIT: 2311138571Ssam WI_LOCK(sc); 2312138571Ssam if (!sc->sc_enabled) 2313109323Ssam error = ENETDOWN; 2314138571Ssam else if (ic->ic_mgtq.ifq_len > 5) 2315109323Ssam error = EAGAIN; 2316138571Ssam else { 2317138571Ssam /* NB: m_devget uses M_DONTWAIT so can hold the lock */ 2318138571Ssam /* XXX wi_len looks in u_int8_t, not in u_int16_t */ 2319138571Ssam m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, 2320138571Ssam ifp, NULL); 2321138571Ssam if (m != NULL) 2322138571Ssam IF_ENQUEUE(&ic->ic_mgtq, m); 2323138571Ssam else 2324138571Ssam error = ENOMEM; 2325109323Ssam } 2326138571Ssam WI_UNLOCK(sc); 2327109323Ssam break; 232846492Swpaul 2329109323Ssam case WI_RID_MIF: 2330109323Ssam mif = wreq.wi_val[0]; 2331109323Ssam val = wreq.wi_val[1]; 2332138571Ssam WI_LOCK(sc); 2333109323Ssam error = wi_cmd(sc, WI_CMD_WRITEMIF, mif, val, 0); 2334138571Ssam WI_UNLOCK(sc); 2335109323Ssam break; 233646492Swpaul 2337109323Ssam case WI_RID_PROCFRAME: /* ignore for compatibility */ 2338109323Ssam break; 2339109323Ssam 2340116898Ssam case WI_RID_OWN_SSID: 2341116898Ssam if (le16toh(wreq.wi_val[0]) * 2 > len || 2342116898Ssam le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) { 2343116898Ssam error = ENOSPC; 2344116898Ssam break; 2345116898Ssam } 2346138571Ssam WI_LOCK(sc); 2347116898Ssam memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN); 2348116898Ssam ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2; 2349116898Ssam memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen); 2350138571Ssam if (sc->sc_enabled) 2351138571Ssam wi_init(sc); /* XXX no error return */ 2352138571Ssam WI_UNLOCK(sc); 2353116898Ssam break; 2354116898Ssam 2355109323Ssam default: 2356138571Ssam WI_LOCK(sc); 2357138571Ssam if (sc->sc_enabled) 2358109323Ssam error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val, 2359109323Ssam len); 2360138571Ssam if (error == 0) { 2361138571Ssam /* XXX ieee80211_cfgset does a copyin */ 2362138571Ssam error = ieee80211_cfgset(ic, cmd, data); 2363138571Ssam if (error == ENETRESET) { 2364138571Ssam if (sc->sc_enabled) 2365138571Ssam wi_init(sc); 2366138571Ssam error = 0; 2367138571Ssam } 2368109323Ssam } 2369138571Ssam WI_UNLOCK(sc); 2370109323Ssam break; 2371109323Ssam } 2372109323Ssam return error; 237346492Swpaul} 237446492Swpaul 2375109323Ssamstatic int 2376109323Ssamwi_write_txrate(struct wi_softc *sc) 237746492Swpaul{ 2378109323Ssam struct ieee80211com *ic = &sc->sc_ic; 2379109323Ssam int i; 2380109323Ssam u_int16_t rate; 238146492Swpaul 2382148290Ssam if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) 2383109323Ssam rate = 0; /* auto */ 2384109323Ssam else 2385116951Ssam rate = (ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ic->ic_fixed_rate] & 2386109323Ssam IEEE80211_RATE_VAL) / 2; 238746492Swpaul 2388109323Ssam /* rate: 0, 1, 2, 5, 11 */ 238946492Swpaul 2390109323Ssam switch (sc->sc_firmware_type) { 2391109323Ssam case WI_LUCENT: 2392112501Simp switch (rate) { 2393112501Simp case 0: /* auto == 11mbps auto */ 2394112501Simp rate = 3; 2395112501Simp break; 2396112501Simp /* case 1, 2 map to 1, 2*/ 2397112501Simp case 5: /* 5.5Mbps -> 4 */ 2398112501Simp rate = 4; 2399112501Simp break; 2400112501Simp case 11: /* 11mbps -> 5 */ 2401112501Simp rate = 5; 2402112501Simp break; 2403112501Simp default: 2404112501Simp break; 2405112501Simp } 2406109323Ssam break; 2407109323Ssam default: 2408109323Ssam /* Choose a bit according to this table. 2409109323Ssam * 2410109323Ssam * bit | data rate 2411109323Ssam * ----+------------------- 2412109323Ssam * 0 | 1Mbps 2413109323Ssam * 1 | 2Mbps 2414109323Ssam * 2 | 5.5Mbps 2415109323Ssam * 3 | 11Mbps 2416109323Ssam */ 2417109323Ssam for (i = 8; i > 0; i >>= 1) { 2418109323Ssam if (rate >= i) 2419109323Ssam break; 2420109323Ssam } 2421109323Ssam if (i == 0) 2422109323Ssam rate = 0xf; /* auto */ 2423109323Ssam else 2424109323Ssam rate = i; 2425109323Ssam break; 2426109323Ssam } 2427109323Ssam return wi_write_val(sc, WI_RID_TX_RATE, rate); 2428109323Ssam} 242946492Swpaul 2430109323Ssamstatic int 2431150798Savatarwi_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k, 2432150798Savatar ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 2433150798Savatar{ 2434150798Savatar struct wi_softc *sc = ic->ic_ifp->if_softc; 2435150798Savatar 2436150798Savatar /* 2437150798Savatar * When doing host encryption of outbound frames fail requests 2438150798Savatar * for keys that are not marked w/ the SWCRYPT flag so the 2439150798Savatar * net80211 layer falls back to s/w crypto. Note that we also 2440150798Savatar * fixup existing keys below to handle mode changes. 2441150798Savatar */ 2442150798Savatar if ((sc->sc_encryption & HOST_ENCRYPT) && 2443150798Savatar (k->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) 2444150798Savatar return 0; 2445150798Savatar return sc->sc_key_alloc(ic, k, keyix, rxkeyix); 2446150798Savatar} 2447150798Savatar 2448150798Savatarstatic int 2449109323Ssamwi_write_wep(struct wi_softc *sc) 2450109323Ssam{ 2451109323Ssam struct ieee80211com *ic = &sc->sc_ic; 2452109323Ssam int error = 0; 2453109323Ssam int i, keylen; 2454109323Ssam u_int16_t val; 2455109323Ssam struct wi_key wkey[IEEE80211_WEP_NKID]; 245646492Swpaul 2457109323Ssam switch (sc->sc_firmware_type) { 2458109323Ssam case WI_LUCENT: 2459138571Ssam val = (ic->ic_flags & IEEE80211_F_PRIVACY) ? 1 : 0; 2460109323Ssam error = wi_write_val(sc, WI_RID_ENCRYPTION, val); 2461109323Ssam if (error) 2462109323Ssam break; 2463138988Smdodd if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) 2464138988Smdodd break; 2465138988Smdodd error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_def_txkey); 2466138988Smdodd if (error) 2467138988Smdodd break; 2468138988Smdodd memset(wkey, 0, sizeof(wkey)); 2469138988Smdodd for (i = 0; i < IEEE80211_WEP_NKID; i++) { 2470138988Smdodd keylen = ic->ic_nw_keys[i].wk_keylen; 2471138988Smdodd wkey[i].wi_keylen = htole16(keylen); 2472138988Smdodd memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key, 2473138988Smdodd keylen); 2474109323Ssam } 2475138988Smdodd error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS, 2476138988Smdodd wkey, sizeof(wkey)); 2477150798Savatar sc->sc_encryption = 0; 2478109323Ssam break; 2479109323Ssam 2480109323Ssam case WI_INTERSIL: 2481109323Ssam case WI_SYMBOL: 2482138571Ssam if (ic->ic_flags & IEEE80211_F_PRIVACY) { 2483109323Ssam /* 2484109323Ssam * ONLY HWB3163 EVAL-CARD Firmware version 2485109323Ssam * less than 0.8 variant2 2486109323Ssam * 2487109323Ssam * If promiscuous mode disable, Prism2 chip 2488109323Ssam * does not work with WEP . 2489109323Ssam * It is under investigation for details. 2490109323Ssam * (ichiro@netbsd.org) 2491109323Ssam */ 2492109323Ssam if (sc->sc_firmware_type == WI_INTERSIL && 2493109323Ssam sc->sc_sta_firmware_ver < 802 ) { 2494109323Ssam /* firm ver < 0.8 variant 2 */ 2495109323Ssam wi_write_val(sc, WI_RID_PROMISC, 1); 2496109323Ssam } 2497109323Ssam wi_write_val(sc, WI_RID_CNFAUTHMODE, 2498109323Ssam sc->sc_cnfauthmode); 2499150798Savatar /* XXX should honor IEEE80211_F_DROPUNENC */ 2500109323Ssam val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED; 2501109323Ssam /* 2502109323Ssam * Encryption firmware has a bug for HostAP mode. 2503109323Ssam */ 2504109323Ssam if (sc->sc_firmware_type == WI_INTERSIL && 2505109323Ssam ic->ic_opmode == IEEE80211_M_HOSTAP) 2506109323Ssam val |= HOST_ENCRYPT; 2507109323Ssam } else { 2508109323Ssam wi_write_val(sc, WI_RID_CNFAUTHMODE, 2509109323Ssam IEEE80211_AUTH_OPEN); 2510109323Ssam val = HOST_ENCRYPT | HOST_DECRYPT; 2511109323Ssam } 2512109323Ssam error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val); 2513109323Ssam if (error) 2514109323Ssam break; 2515150798Savatar sc->sc_encryption = val; 2516138988Smdodd if ((val & PRIVACY_INVOKED) == 0) 2517138988Smdodd break; 2518138988Smdodd error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY, 2519138988Smdodd ic->ic_def_txkey); 2520138988Smdodd if (error) 2521138988Smdodd break; 2522138988Smdodd if (val & HOST_DECRYPT) 2523138988Smdodd break; 2524138988Smdodd /* 2525138988Smdodd * It seems that the firmware accept 104bit key only if 2526138988Smdodd * all the keys have 104bit length. We get the length of 2527138988Smdodd * the transmit key and use it for all other keys. 2528138988Smdodd * Perhaps we should use software WEP for such situation. 2529138988Smdodd */ 2530138988Smdodd if (ic->ic_def_txkey != IEEE80211_KEYIX_NONE) 2531138988Smdodd keylen = ic->ic_nw_keys[ic->ic_def_txkey].wk_keylen; 2532138988Smdodd else /* XXX should not hapen */ 2533138988Smdodd keylen = IEEE80211_WEP_KEYLEN; 2534138988Smdodd if (keylen > IEEE80211_WEP_KEYLEN) 2535138988Smdodd keylen = 13; /* 104bit keys */ 2536138988Smdodd else 2537138988Smdodd keylen = IEEE80211_WEP_KEYLEN; 2538138988Smdodd for (i = 0; i < IEEE80211_WEP_NKID; i++) { 2539138988Smdodd error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i, 2540138988Smdodd ic->ic_nw_keys[i].wk_key, keylen); 2541109323Ssam if (error) 2542109323Ssam break; 2543109323Ssam } 2544109323Ssam break; 2545109323Ssam } 2546150798Savatar /* 2547150798Savatar * XXX horrible hack; insure pre-existing keys are 2548150798Savatar * setup properly to do s/w crypto. 2549150798Savatar */ 2550150798Savatar for (i = 0; i < IEEE80211_WEP_NKID; i++) { 2551150798Savatar struct ieee80211_key *k = &ic->ic_nw_keys[i]; 2552150798Savatar if (k->wk_flags & IEEE80211_KEY_XMIT) { 2553150798Savatar if (sc->sc_encryption & HOST_ENCRYPT) 2554150798Savatar k->wk_flags |= IEEE80211_KEY_SWCRYPT; 2555150798Savatar else 2556150798Savatar k->wk_flags &= ~IEEE80211_KEY_SWCRYPT; 2557150798Savatar } 2558150798Savatar } 2559109323Ssam return error; 256046492Swpaul} 256146492Swpaul 2562109323Ssamstatic int 2563109323Ssamwi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2) 256446492Swpaul{ 2565109323Ssam int i, s = 0; 2566109323Ssam 2567123098Simp if (sc->wi_gone) 2568123098Simp return (ENODEV); 2569123098Simp 2570109323Ssam /* wait for the busy bit to clear */ 2571123339Simp for (i = sc->wi_cmd_count; i > 0; i--) { /* 500ms */ 2572116206Simp if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY)) 2573109323Ssam break; 2574123098Simp DELAY(1*1000); /* 1ms */ 2575109323Ssam } 2576109323Ssam if (i == 0) { 2577109323Ssam device_printf(sc->sc_dev, "wi_cmd: busy bit won't clear.\n" ); 2578123098Simp sc->wi_gone = 1; 2579109323Ssam return(ETIMEDOUT); 2580109323Ssam } 258190580Sbrooks 2582109323Ssam CSR_WRITE_2(sc, WI_PARAM0, val0); 2583109323Ssam CSR_WRITE_2(sc, WI_PARAM1, val1); 2584109323Ssam CSR_WRITE_2(sc, WI_PARAM2, val2); 2585109323Ssam CSR_WRITE_2(sc, WI_COMMAND, cmd); 258690580Sbrooks 2587109323Ssam if (cmd == WI_CMD_INI) { 2588109323Ssam /* XXX: should sleep here. */ 2589116206Simp DELAY(100*1000); /* 100ms delay for init */ 2590109323Ssam } 2591109323Ssam for (i = 0; i < WI_TIMEOUT; i++) { 2592109323Ssam /* 2593109323Ssam * Wait for 'command complete' bit to be 2594109323Ssam * set in the event status register. 2595109323Ssam */ 2596109323Ssam s = CSR_READ_2(sc, WI_EVENT_STAT); 2597109323Ssam if (s & WI_EV_CMD) { 2598109323Ssam /* Ack the event and read result code. */ 2599109323Ssam s = CSR_READ_2(sc, WI_STATUS); 2600109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD); 2601109323Ssam if (s & WI_STAT_CMD_RESULT) { 2602109323Ssam return(EIO); 2603109323Ssam } 2604109323Ssam break; 260590580Sbrooks } 2606109323Ssam DELAY(WI_DELAY); 2607109323Ssam } 260890580Sbrooks 2609109323Ssam if (i == WI_TIMEOUT) { 2610109323Ssam device_printf(sc->sc_dev, 2611109323Ssam "timeout in wi_cmd 0x%04x; event status 0x%04x\n", cmd, s); 2612123098Simp if (s == 0xffff) 2613123098Simp sc->wi_gone = 1; 2614109323Ssam return(ETIMEDOUT); 261553702Swpaul } 2616109323Ssam return (0); 2617109323Ssam} 261853702Swpaul 2619109323Ssamstatic int 2620109323Ssamwi_seek_bap(struct wi_softc *sc, int id, int off) 2621109323Ssam{ 2622109323Ssam int i, status; 262390580Sbrooks 2624109323Ssam CSR_WRITE_2(sc, WI_SEL0, id); 2625109323Ssam CSR_WRITE_2(sc, WI_OFF0, off); 262690580Sbrooks 2627109323Ssam for (i = 0; ; i++) { 2628109323Ssam status = CSR_READ_2(sc, WI_OFF0); 2629109323Ssam if ((status & WI_OFF_BUSY) == 0) 2630109323Ssam break; 2631109323Ssam if (i == WI_TIMEOUT) { 2632109323Ssam device_printf(sc->sc_dev, "timeout in wi_seek to %x/%x\n", 2633109323Ssam id, off); 2634109323Ssam sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 2635123098Simp if (status == 0xffff) 2636123098Simp sc->wi_gone = 1; 2637109323Ssam return ETIMEDOUT; 2638109323Ssam } 2639109323Ssam DELAY(1); 264053702Swpaul } 2641109323Ssam if (status & WI_OFF_ERR) { 2642109323Ssam device_printf(sc->sc_dev, "failed in wi_seek to %x/%x\n", id, off); 2643109323Ssam sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 2644109323Ssam return EIO; 2645109323Ssam } 2646109323Ssam sc->sc_bap_id = id; 2647109323Ssam sc->sc_bap_off = off; 2648109323Ssam return 0; 264953702Swpaul} 265053702Swpaul 2651109323Ssamstatic int 2652109323Ssamwi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen) 265353702Swpaul{ 2654109323Ssam u_int16_t *ptr; 2655109323Ssam int i, error, cnt; 265653702Swpaul 2657109323Ssam if (buflen == 0) 2658109323Ssam return 0; 2659109323Ssam if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 2660109323Ssam if ((error = wi_seek_bap(sc, id, off)) != 0) 2661109323Ssam return error; 266275219Salfred } 2663109323Ssam cnt = (buflen + 1) / 2; 2664109323Ssam ptr = (u_int16_t *)buf; 2665109323Ssam for (i = 0; i < cnt; i++) 2666109323Ssam *ptr++ = CSR_READ_2(sc, WI_DATA0); 2667109323Ssam sc->sc_bap_off += cnt * 2; 2668109323Ssam return 0; 266953702Swpaul} 267053702Swpaul 2671109323Ssamstatic int 2672109323Ssamwi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen) 267353702Swpaul{ 2674109323Ssam u_int16_t *ptr; 2675109323Ssam int i, error, cnt; 267646492Swpaul 2677109323Ssam if (buflen == 0) 2678109323Ssam return 0; 267946492Swpaul 2680109323Ssam#ifdef WI_HERMES_AUTOINC_WAR 2681109323Ssam again: 2682109323Ssam#endif 2683109323Ssam if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 2684109323Ssam if ((error = wi_seek_bap(sc, id, off)) != 0) 2685109323Ssam return error; 2686109323Ssam } 2687109323Ssam cnt = (buflen + 1) / 2; 2688109323Ssam ptr = (u_int16_t *)buf; 2689109323Ssam for (i = 0; i < cnt; i++) 2690109323Ssam CSR_WRITE_2(sc, WI_DATA0, ptr[i]); 2691109323Ssam sc->sc_bap_off += cnt * 2; 2692109323Ssam 2693109323Ssam#ifdef WI_HERMES_AUTOINC_WAR 2694109323Ssam /* 2695109323Ssam * According to the comments in the HCF Light code, there is a bug 2696109323Ssam * in the Hermes (or possibly in certain Hermes firmware revisions) 2697109323Ssam * where the chip's internal autoincrement counter gets thrown off 2698109323Ssam * during data writes: the autoincrement is missed, causing one 2699109323Ssam * data word to be overwritten and subsequent words to be written to 2700109323Ssam * the wrong memory locations. The end result is that we could end 2701109323Ssam * up transmitting bogus frames without realizing it. The workaround 2702109323Ssam * for this is to write a couple of extra guard words after the end 2703109323Ssam * of the transfer, then attempt to read then back. If we fail to 2704109323Ssam * locate the guard words where we expect them, we preform the 2705109323Ssam * transfer over again. 2706109323Ssam */ 2707109323Ssam if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) { 2708109323Ssam CSR_WRITE_2(sc, WI_DATA0, 0x1234); 2709109323Ssam CSR_WRITE_2(sc, WI_DATA0, 0x5678); 2710109323Ssam wi_seek_bap(sc, id, sc->sc_bap_off); 2711109323Ssam sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 2712109323Ssam if (CSR_READ_2(sc, WI_DATA0) != 0x1234 || 2713109323Ssam CSR_READ_2(sc, WI_DATA0) != 0x5678) { 2714109323Ssam device_printf(sc->sc_dev, 2715109323Ssam "detect auto increment bug, try again\n"); 2716109323Ssam goto again; 2717109323Ssam } 2718109323Ssam } 2719109323Ssam#endif 2720109323Ssam return 0; 272146492Swpaul} 272253702Swpaul 2723109323Ssamstatic int 2724109323Ssamwi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen) 2725109323Ssam{ 2726109323Ssam int error, len; 2727109323Ssam struct mbuf *m; 272853702Swpaul 2729109323Ssam for (m = m0; m != NULL && totlen > 0; m = m->m_next) { 2730109323Ssam if (m->m_len == 0) 2731109323Ssam continue; 273253702Swpaul 2733109323Ssam len = min(m->m_len, totlen); 273453702Swpaul 2735109323Ssam if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) { 2736109323Ssam m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf); 2737109323Ssam return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf, 2738109323Ssam totlen); 2739109323Ssam } 274053702Swpaul 2741109323Ssam if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0) 2742109323Ssam return error; 274353702Swpaul 2744109323Ssam off += m->m_len; 2745109323Ssam totlen -= len; 2746109323Ssam } 2747109323Ssam return 0; 2748109323Ssam} 274953702Swpaul 2750109323Ssamstatic int 2751109323Ssamwi_alloc_fid(struct wi_softc *sc, int len, int *idp) 275253702Swpaul{ 275353702Swpaul int i; 275453702Swpaul 2755109323Ssam if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) { 2756109323Ssam device_printf(sc->sc_dev, "failed to allocate %d bytes on NIC\n", 2757109323Ssam len); 2758109323Ssam return ENOMEM; 275953702Swpaul } 276053702Swpaul 2761109323Ssam for (i = 0; i < WI_TIMEOUT; i++) { 2762109323Ssam if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC) 2763109323Ssam break; 2764109323Ssam DELAY(1); 276553702Swpaul } 2766144167Ssam if (i == WI_TIMEOUT) { 2767144167Ssam device_printf(sc->sc_dev, "timeout in alloc\n"); 2768144167Ssam return ETIMEDOUT; 2769144167Ssam } 2770109323Ssam *idp = CSR_READ_2(sc, WI_ALLOC_FID); 2771109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC); 2772109323Ssam return 0; 2773109323Ssam} 277453702Swpaul 2775109323Ssamstatic int 2776109323Ssamwi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp) 2777109323Ssam{ 2778109323Ssam int error, len; 2779109323Ssam u_int16_t ltbuf[2]; 278053702Swpaul 2781109323Ssam /* Tell the NIC to enter record read mode. */ 2782109323Ssam error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0); 2783109323Ssam if (error) 2784109323Ssam return error; 278553702Swpaul 2786109323Ssam error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf)); 2787109323Ssam if (error) 2788109323Ssam return error; 278953702Swpaul 2790109323Ssam if (le16toh(ltbuf[1]) != rid) { 2791109323Ssam device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n", 2792109323Ssam rid, le16toh(ltbuf[1])); 2793109323Ssam return EIO; 279453702Swpaul } 2795109323Ssam len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */ 2796109323Ssam if (*buflenp < len) { 2797109323Ssam device_printf(sc->sc_dev, "record buffer is too small, " 2798109323Ssam "rid=%x, size=%d, len=%d\n", 2799109323Ssam rid, *buflenp, len); 2800109323Ssam return ENOSPC; 280153702Swpaul } 2802109323Ssam *buflenp = len; 2803109323Ssam return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len); 2804109323Ssam} 280553702Swpaul 2806109323Ssamstatic int 2807109323Ssamwi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen) 2808109323Ssam{ 2809109323Ssam int error; 2810109323Ssam u_int16_t ltbuf[2]; 281153702Swpaul 2812109323Ssam ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */ 2813109323Ssam ltbuf[1] = htole16(rid); 281453702Swpaul 2815109323Ssam error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf)); 2816109323Ssam if (error) 2817109323Ssam return error; 2818109323Ssam error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen); 2819109323Ssam if (error) 2820109323Ssam return error; 2821102206Simp 2822109323Ssam return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0); 282353702Swpaul} 282477217Sphk 282588546Salfredstatic int 2826117812Ssamwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 282777217Sphk{ 2828138571Ssam struct ifnet *ifp = ic->ic_ifp; 2829117812Ssam struct wi_softc *sc = ifp->if_softc; 2830139542Ssam struct ieee80211_node *ni; 2831116951Ssam int buflen; 2832109323Ssam u_int16_t val; 2833109323Ssam struct wi_ssid ssid; 2834109323Ssam u_int8_t old_bssid[IEEE80211_ADDR_LEN]; 283577217Sphk 2836117812Ssam DPRINTF(("%s: %s -> %s\n", __func__, 2837117812Ssam ieee80211_state_name[ic->ic_state], 2838117812Ssam ieee80211_state_name[nstate])); 2839109323Ssam 2840139542Ssam /* 2841139542Ssam * Internal to the driver the INIT and RUN states are used 2842139542Ssam * so bypass the net80211 state machine for other states. 2843139542Ssam * Beware however that this requires use to net80211 state 2844139542Ssam * management that otherwise would be handled for us. 2845139542Ssam */ 2846109323Ssam switch (nstate) { 2847109323Ssam case IEEE80211_S_INIT: 2848109323Ssam sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 2849117812Ssam return (*sc->sc_newstate)(ic, nstate, arg); 2850109323Ssam 2851138571Ssam case IEEE80211_S_SCAN: 2852138571Ssam case IEEE80211_S_AUTH: 2853138571Ssam case IEEE80211_S_ASSOC: 2854138571Ssam ic->ic_state = nstate; /* NB: skip normal ieee80211 handling */ 2855138571Ssam break; 2856138571Ssam 2857109323Ssam case IEEE80211_S_RUN: 2858139542Ssam ni = ic->ic_bss; 2859109323Ssam sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 2860109323Ssam buflen = IEEE80211_ADDR_LEN; 2861138571Ssam IEEE80211_ADDR_COPY(old_bssid, ni->ni_bssid); 2862109323Ssam wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen); 2863109323Ssam IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid); 2864109323Ssam buflen = sizeof(val); 2865109323Ssam wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen); 2866116951Ssam /* XXX validate channel */ 2867116951Ssam ni->ni_chan = &ic->ic_channels[le16toh(val)]; 2868148936Ssam ic->ic_curchan = ni->ni_chan; 2869138951Ssam ic->ic_ibss_chan = ni->ni_chan; 2870119784Ssam#if NBPFILTER > 0 2871119784Ssam sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq = 2872119784Ssam htole16(ni->ni_chan->ic_freq); 2873119784Ssam sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags = 2874119784Ssam htole16(ni->ni_chan->ic_flags); 2875119784Ssam#endif 2876150798Savatar /* 2877150798Savatar * XXX hack; unceremoniously clear 2878150798Savatar * IEEE80211_F_DROPUNENC when operating with 2879150798Savatar * wep enabled so we don't drop unencoded frames 2880150798Savatar * at the 802.11 layer. This is necessary because 2881150798Savatar * we must strip the WEP bit from the 802.11 header 2882150798Savatar * before passing frames to ieee80211_input because 2883150798Savatar * the card has already stripped the WEP crypto 2884150798Savatar * header from the packet. 2885150798Savatar */ 2886150798Savatar if (ic->ic_flags & IEEE80211_F_PRIVACY) 2887150798Savatar ic->ic_flags &= ~IEEE80211_F_DROPUNENC; 2888139542Ssam if (ic->ic_opmode != IEEE80211_M_HOSTAP) { 2889109323Ssam /* XXX check return value */ 2890109323Ssam buflen = sizeof(ssid); 2891109323Ssam wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen); 2892109323Ssam ni->ni_esslen = le16toh(ssid.wi_len); 2893109323Ssam if (ni->ni_esslen > IEEE80211_NWID_LEN) 2894109323Ssam ni->ni_esslen = IEEE80211_NWID_LEN; /*XXX*/ 2895109323Ssam memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen); 289677217Sphk } 2897138571Ssam return (*sc->sc_newstate)(ic, nstate, arg); 289877217Sphk } 2899117812Ssam return 0; 290077217Sphk} 290177217Sphk 290288546Salfredstatic int 2903116898Ssamwi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate) 290477217Sphk{ 2905109323Ssam int error = 0; 2906109323Ssam u_int16_t val[2]; 290777217Sphk 2908109323Ssam if (!sc->sc_enabled) 2909109323Ssam return ENXIO; 2910109323Ssam switch (sc->sc_firmware_type) { 2911109323Ssam case WI_LUCENT: 2912109323Ssam (void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0); 291398440Simp break; 2914109323Ssam case WI_INTERSIL: 2915138571Ssam val[0] = htole16(chanmask); /* channel */ 2916138571Ssam val[1] = htole16(txrate); /* tx rate */ 2917109323Ssam error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val)); 291898440Simp break; 2919109323Ssam case WI_SYMBOL: 2920109323Ssam /* 2921109323Ssam * XXX only supported on 3.x ? 2922109323Ssam */ 2923109323Ssam val[0] = BSCAN_BCAST | BSCAN_ONETIME; 2924109323Ssam error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ, 2925109323Ssam val, sizeof(val[0])); 292698440Simp break; 292798440Simp } 2928109323Ssam if (error == 0) { 2929109323Ssam sc->sc_scan_timer = WI_SCAN_WAIT; 2930147256Sbrooks sc->sc_ifp->if_timer = 1; 2931116898Ssam DPRINTF(("wi_scan_ap: start scanning, " 2932116898Ssam "chamask 0x%x txrate 0x%x\n", chanmask, txrate)); 2933109323Ssam } 2934109323Ssam return error; 2935109323Ssam} 293677217Sphk 2937109323Ssamstatic void 2938109323Ssamwi_scan_result(struct wi_softc *sc, int fid, int cnt) 2939109323Ssam{ 2940109323Ssam#define N(a) (sizeof (a) / sizeof (a[0])) 2941109323Ssam int i, naps, off, szbuf; 2942109323Ssam struct wi_scan_header ws_hdr; /* Prism2 header */ 2943109323Ssam struct wi_scan_data_p2 ws_dat; /* Prism2 scantable*/ 2944109323Ssam struct wi_apinfo *ap; 2945109323Ssam 2946109323Ssam off = sizeof(u_int16_t) * 2; 2947109323Ssam memset(&ws_hdr, 0, sizeof(ws_hdr)); 2948109323Ssam switch (sc->sc_firmware_type) { 2949109323Ssam case WI_INTERSIL: 2950109323Ssam wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr)); 2951109323Ssam off += sizeof(ws_hdr); 2952109323Ssam szbuf = sizeof(struct wi_scan_data_p2); 295377217Sphk break; 2954109323Ssam case WI_SYMBOL: 2955109323Ssam szbuf = sizeof(struct wi_scan_data_p2) + 6; 295677217Sphk break; 2957109323Ssam case WI_LUCENT: 2958109323Ssam szbuf = sizeof(struct wi_scan_data); 295977217Sphk break; 2960109323Ssam default: 2961109323Ssam device_printf(sc->sc_dev, 2962109323Ssam "wi_scan_result: unknown firmware type %u\n", 2963109323Ssam sc->sc_firmware_type); 2964109323Ssam naps = 0; 2965109323Ssam goto done; 296677217Sphk } 2967109323Ssam naps = (cnt * 2 + 2 - off) / szbuf; 2968109323Ssam if (naps > N(sc->sc_aps)) 2969109323Ssam naps = N(sc->sc_aps); 2970109323Ssam sc->sc_naps = naps; 2971109323Ssam /* Read Data */ 2972109323Ssam ap = sc->sc_aps; 2973109323Ssam memset(&ws_dat, 0, sizeof(ws_dat)); 2974109323Ssam for (i = 0; i < naps; i++, ap++) { 2975109323Ssam wi_read_bap(sc, fid, off, &ws_dat, 2976109323Ssam (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf)); 2977109323Ssam DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off, 2978109323Ssam ether_sprintf(ws_dat.wi_bssid))); 2979109323Ssam off += szbuf; 2980109323Ssam ap->scanreason = le16toh(ws_hdr.wi_reason); 2981109323Ssam memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid)); 2982109323Ssam ap->channel = le16toh(ws_dat.wi_chid); 2983109323Ssam ap->signal = le16toh(ws_dat.wi_signal); 2984109323Ssam ap->noise = le16toh(ws_dat.wi_noise); 2985109323Ssam ap->quality = ap->signal - ap->noise; 2986109323Ssam ap->capinfo = le16toh(ws_dat.wi_capinfo); 2987109323Ssam ap->interval = le16toh(ws_dat.wi_interval); 2988109323Ssam ap->rate = le16toh(ws_dat.wi_rate); 2989109323Ssam ap->namelen = le16toh(ws_dat.wi_namelen); 2990109323Ssam if (ap->namelen > sizeof(ap->name)) 2991109323Ssam ap->namelen = sizeof(ap->name); 2992109323Ssam memcpy(ap->name, ws_dat.wi_name, ap->namelen); 2993109323Ssam } 2994109323Ssamdone: 2995109323Ssam /* Done scanning */ 2996109323Ssam sc->sc_scan_timer = 0; 2997109323Ssam DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps)); 2998109323Ssam#undef N 299977217Sphk} 300077217Sphk 300188546Salfredstatic void 3002109323Ssamwi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi) 300377217Sphk{ 3004109323Ssam ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr), 3005116951Ssam ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL : -1, rssi); 3006109323Ssam printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n", 3007109323Ssam le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1), 3008109323Ssam le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence); 3009109323Ssam printf(" rx_signal %u rx_rate %u rx_flow %u\n", 3010109323Ssam wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow); 3011109323Ssam printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n", 3012109323Ssam wh->wi_tx_rtry, wh->wi_tx_rate, 3013109323Ssam le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len)); 3014109323Ssam printf(" ehdr dst %6D src %6D type 0x%x\n", 3015109323Ssam wh->wi_ehdr.ether_dhost, ":", wh->wi_ehdr.ether_shost, ":", 3016109323Ssam wh->wi_ehdr.ether_type); 3017109323Ssam} 301877217Sphk 3019109323Ssamint 3020109323Ssamwi_alloc(device_t dev, int rid) 3021109323Ssam{ 3022109323Ssam struct wi_softc *sc = device_get_softc(dev); 3023109323Ssam 3024109323Ssam if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) { 3025109323Ssam sc->iobase_rid = rid; 3026109323Ssam sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT, 3027109323Ssam &sc->iobase_rid, 0, ~0, (1 << 6), 3028109323Ssam rman_make_alignment_flags(1 << 6) | RF_ACTIVE); 3029109323Ssam if (!sc->iobase) { 3030109323Ssam device_printf(dev, "No I/O space?!\n"); 3031109323Ssam return (ENXIO); 303298440Simp } 3033109323Ssam 3034109323Ssam sc->wi_io_addr = rman_get_start(sc->iobase); 3035109323Ssam sc->wi_btag = rman_get_bustag(sc->iobase); 3036109323Ssam sc->wi_bhandle = rman_get_bushandle(sc->iobase); 3037109323Ssam } else { 3038109323Ssam sc->mem_rid = rid; 3039127135Snjl sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 3040127135Snjl &sc->mem_rid, RF_ACTIVE); 3041109323Ssam 3042109323Ssam if (!sc->mem) { 3043109323Ssam device_printf(dev, "No Mem space on prism2.5?\n"); 3044109323Ssam return (ENXIO); 304577217Sphk } 3046109323Ssam 3047109323Ssam sc->wi_btag = rman_get_bustag(sc->mem); 3048109323Ssam sc->wi_bhandle = rman_get_bushandle(sc->mem); 304977217Sphk } 305077217Sphk 3051109323Ssam 3052109323Ssam sc->irq_rid = 0; 3053127135Snjl sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid, 3054127135Snjl RF_ACTIVE | 3055109323Ssam ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE)); 3056109323Ssam 3057109323Ssam if (!sc->irq) { 3058109323Ssam wi_free(dev); 3059109323Ssam device_printf(dev, "No irq?!\n"); 3060109323Ssam return (ENXIO); 306177217Sphk } 3062109323Ssam 3063109323Ssam sc->sc_dev = dev; 3064109323Ssam sc->sc_unit = device_get_unit(dev); 3065109323Ssam 3066109323Ssam return (0); 306777217Sphk} 306893359Simp 3069109323Ssamvoid 3070109323Ssamwi_free(device_t dev) 3071109323Ssam{ 3072109323Ssam struct wi_softc *sc = device_get_softc(dev); 3073109323Ssam 3074109323Ssam if (sc->iobase != NULL) { 3075109323Ssam bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase); 3076109323Ssam sc->iobase = NULL; 3077109323Ssam } 3078109323Ssam if (sc->irq != NULL) { 3079109323Ssam bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq); 3080109323Ssam sc->irq = NULL; 3081109323Ssam } 3082109323Ssam if (sc->mem != NULL) { 3083109323Ssam bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem); 3084109323Ssam sc->mem = NULL; 3085109323Ssam } 3086109323Ssam 3087109323Ssam return; 3088109323Ssam} 3089109323Ssam 309093359Simpstatic int 3091109323Ssamwi_get_debug(struct wi_softc *sc, struct wi_req *wreq) 309293359Simp{ 3093109323Ssam int error = 0; 309493359Simp 309593359Simp wreq->wi_len = 1; 309693359Simp 309793359Simp switch (wreq->wi_type) { 309893359Simp case WI_DEBUG_SLEEP: 309993359Simp wreq->wi_len++; 310093359Simp wreq->wi_val[0] = sc->wi_debug.wi_sleep; 310193359Simp break; 310293359Simp case WI_DEBUG_DELAYSUPP: 310393359Simp wreq->wi_len++; 310493359Simp wreq->wi_val[0] = sc->wi_debug.wi_delaysupp; 310593359Simp break; 310693359Simp case WI_DEBUG_TXSUPP: 310793359Simp wreq->wi_len++; 310893359Simp wreq->wi_val[0] = sc->wi_debug.wi_txsupp; 310993359Simp break; 311093359Simp case WI_DEBUG_MONITOR: 311193359Simp wreq->wi_len++; 311293359Simp wreq->wi_val[0] = sc->wi_debug.wi_monitor; 311393359Simp break; 311493359Simp case WI_DEBUG_LEDTEST: 311593359Simp wreq->wi_len += 3; 311693359Simp wreq->wi_val[0] = sc->wi_debug.wi_ledtest; 311793359Simp wreq->wi_val[1] = sc->wi_debug.wi_ledtest_param0; 311893359Simp wreq->wi_val[2] = sc->wi_debug.wi_ledtest_param1; 311993359Simp break; 312093359Simp case WI_DEBUG_CONTTX: 312193359Simp wreq->wi_len += 2; 312293359Simp wreq->wi_val[0] = sc->wi_debug.wi_conttx; 312393359Simp wreq->wi_val[1] = sc->wi_debug.wi_conttx_param0; 312493359Simp break; 312593359Simp case WI_DEBUG_CONTRX: 312693359Simp wreq->wi_len++; 312793359Simp wreq->wi_val[0] = sc->wi_debug.wi_contrx; 312893359Simp break; 312993359Simp case WI_DEBUG_SIGSTATE: 313093359Simp wreq->wi_len += 2; 313193359Simp wreq->wi_val[0] = sc->wi_debug.wi_sigstate; 313293359Simp wreq->wi_val[1] = sc->wi_debug.wi_sigstate_param0; 313393359Simp break; 313493359Simp case WI_DEBUG_CONFBITS: 313593359Simp wreq->wi_len += 2; 313693359Simp wreq->wi_val[0] = sc->wi_debug.wi_confbits; 313793359Simp wreq->wi_val[1] = sc->wi_debug.wi_confbits_param0; 313893359Simp break; 313993359Simp default: 314093359Simp error = EIO; 314193359Simp break; 314293359Simp } 314393359Simp 314493359Simp return (error); 314593359Simp} 314693359Simp 314793359Simpstatic int 3148109323Ssamwi_set_debug(struct wi_softc *sc, struct wi_req *wreq) 314993359Simp{ 3150109323Ssam int error = 0; 315193359Simp u_int16_t cmd, param0 = 0, param1 = 0; 315293359Simp 315393359Simp switch (wreq->wi_type) { 315493359Simp case WI_DEBUG_RESET: 315593359Simp case WI_DEBUG_INIT: 315693359Simp case WI_DEBUG_CALENABLE: 315793359Simp break; 315893359Simp case WI_DEBUG_SLEEP: 315993359Simp sc->wi_debug.wi_sleep = 1; 316093359Simp break; 316193359Simp case WI_DEBUG_WAKE: 316293359Simp sc->wi_debug.wi_sleep = 0; 316393359Simp break; 316493359Simp case WI_DEBUG_CHAN: 316593359Simp param0 = wreq->wi_val[0]; 316693359Simp break; 316793359Simp case WI_DEBUG_DELAYSUPP: 316893359Simp sc->wi_debug.wi_delaysupp = 1; 316993359Simp break; 317093359Simp case WI_DEBUG_TXSUPP: 317193359Simp sc->wi_debug.wi_txsupp = 1; 317293359Simp break; 317393359Simp case WI_DEBUG_MONITOR: 317493359Simp sc->wi_debug.wi_monitor = 1; 317593359Simp break; 317693359Simp case WI_DEBUG_LEDTEST: 317793359Simp param0 = wreq->wi_val[0]; 317893359Simp param1 = wreq->wi_val[1]; 317993359Simp sc->wi_debug.wi_ledtest = 1; 318093359Simp sc->wi_debug.wi_ledtest_param0 = param0; 318193359Simp sc->wi_debug.wi_ledtest_param1 = param1; 318293359Simp break; 318393359Simp case WI_DEBUG_CONTTX: 318493359Simp param0 = wreq->wi_val[0]; 318593359Simp sc->wi_debug.wi_conttx = 1; 318693359Simp sc->wi_debug.wi_conttx_param0 = param0; 318793359Simp break; 318893359Simp case WI_DEBUG_STOPTEST: 318993359Simp sc->wi_debug.wi_delaysupp = 0; 319093359Simp sc->wi_debug.wi_txsupp = 0; 319193359Simp sc->wi_debug.wi_monitor = 0; 319293359Simp sc->wi_debug.wi_ledtest = 0; 319393359Simp sc->wi_debug.wi_ledtest_param0 = 0; 319493359Simp sc->wi_debug.wi_ledtest_param1 = 0; 319593359Simp sc->wi_debug.wi_conttx = 0; 319693359Simp sc->wi_debug.wi_conttx_param0 = 0; 319793359Simp sc->wi_debug.wi_contrx = 0; 319893359Simp sc->wi_debug.wi_sigstate = 0; 319993359Simp sc->wi_debug.wi_sigstate_param0 = 0; 320093359Simp break; 320193359Simp case WI_DEBUG_CONTRX: 320293359Simp sc->wi_debug.wi_contrx = 1; 320393359Simp break; 320493359Simp case WI_DEBUG_SIGSTATE: 320593359Simp param0 = wreq->wi_val[0]; 320693359Simp sc->wi_debug.wi_sigstate = 1; 320793359Simp sc->wi_debug.wi_sigstate_param0 = param0; 320893359Simp break; 320993359Simp case WI_DEBUG_CONFBITS: 321093359Simp param0 = wreq->wi_val[0]; 321193359Simp param1 = wreq->wi_val[1]; 321293359Simp sc->wi_debug.wi_confbits = param0; 321393359Simp sc->wi_debug.wi_confbits_param0 = param1; 321493359Simp break; 321593359Simp default: 321693359Simp error = EIO; 321793359Simp break; 321893359Simp } 321993359Simp 322093359Simp if (error) 322193359Simp return (error); 322293359Simp 322393359Simp cmd = WI_CMD_DEBUG | (wreq->wi_type << 8); 322493359Simp error = wi_cmd(sc, cmd, param0, param1, 0); 322593359Simp 322693359Simp return (error); 322793359Simp} 3228101903Simp 3229101903Simp/* 3230101903Simp * Special routines to download firmware for Symbol CF card. 3231101903Simp * XXX: This should be modified generic into any PRISM-2 based card. 3232101903Simp */ 3233101903Simp 3234101903Simp#define WI_SBCF_PDIADDR 0x3100 3235101903Simp 3236101903Simp/* unaligned load little endian */ 3237101903Simp#define GETLE32(p) ((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24)) 3238101903Simp#define GETLE16(p) ((p)[0] | ((p)[1]<<8)) 3239101903Simp 3240101903Simpint 3241101903Simpwi_symbol_load_firm(struct wi_softc *sc, const void *primsym, int primlen, 3242101903Simp const void *secsym, int seclen) 3243101903Simp{ 3244101903Simp uint8_t ebuf[256]; 3245101903Simp int i; 3246101903Simp 3247101903Simp /* load primary code and run it */ 3248101903Simp wi_symbol_set_hcr(sc, WI_HCR_EEHOLD); 3249101903Simp if (wi_symbol_write_firm(sc, primsym, primlen, NULL, 0)) 3250101903Simp return EIO; 3251101903Simp wi_symbol_set_hcr(sc, WI_HCR_RUN); 3252101903Simp for (i = 0; ; i++) { 3253101903Simp if (i == 10) 3254101903Simp return ETIMEDOUT; 3255101903Simp tsleep(sc, PWAIT, "wiinit", 1); 3256101903Simp if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT) 3257101903Simp break; 3258101903Simp /* write the magic key value to unlock aux port */ 3259101903Simp CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0); 3260101903Simp CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1); 3261101903Simp CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2); 3262101903Simp CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL); 3263101903Simp } 3264101903Simp 3265101903Simp /* issue read EEPROM command: XXX copied from wi_cmd() */ 3266101903Simp CSR_WRITE_2(sc, WI_PARAM0, 0); 3267101903Simp CSR_WRITE_2(sc, WI_PARAM1, 0); 3268101903Simp CSR_WRITE_2(sc, WI_PARAM2, 0); 3269101903Simp CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE); 3270101903Simp for (i = 0; i < WI_TIMEOUT; i++) { 3271101903Simp if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD) 3272101903Simp break; 3273101903Simp DELAY(1); 3274101903Simp } 3275101903Simp CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD); 3276101903Simp 3277101903Simp CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ); 3278101903Simp CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ); 3279101903Simp CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA, 3280101903Simp (uint16_t *)ebuf, sizeof(ebuf) / 2); 3281101903Simp if (GETLE16(ebuf) > sizeof(ebuf)) 3282101903Simp return EIO; 3283101903Simp if (wi_symbol_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf))) 3284101903Simp return EIO; 3285101903Simp return 0; 3286101903Simp} 3287101903Simp 3288101903Simpstatic int 3289101903Simpwi_symbol_write_firm(struct wi_softc *sc, const void *buf, int buflen, 3290101903Simp const void *ebuf, int ebuflen) 3291101903Simp{ 3292101903Simp const uint8_t *p, *ep, *q, *eq; 3293101903Simp char *tp; 3294101903Simp uint32_t addr, id, eid; 3295101903Simp int i, len, elen, nblk, pdrlen; 3296101903Simp 3297101903Simp /* 3298101903Simp * Parse the header of the firmware image. 3299101903Simp */ 3300101903Simp p = buf; 3301101903Simp ep = p + buflen; 3302101903Simp while (p < ep && *p++ != ' '); /* FILE: */ 3303101903Simp while (p < ep && *p++ != ' '); /* filename */ 3304101903Simp while (p < ep && *p++ != ' '); /* type of the firmware */ 3305101903Simp nblk = strtoul(p, &tp, 10); 3306101903Simp p = tp; 3307101903Simp pdrlen = strtoul(p + 1, &tp, 10); 3308101903Simp p = tp; 3309101903Simp while (p < ep && *p++ != 0x1a); /* skip rest of header */ 3310101903Simp 3311101903Simp /* 3312101903Simp * Block records: address[4], length[2], data[length]; 3313101903Simp */ 3314101903Simp for (i = 0; i < nblk; i++) { 3315101903Simp addr = GETLE32(p); p += 4; 3316101903Simp len = GETLE16(p); p += 2; 3317101903Simp CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ); 3318101903Simp CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ); 3319101903Simp CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA, 3320101903Simp (const uint16_t *)p, len / 2); 3321101903Simp p += len; 3322101903Simp } 3323101903Simp 3324101903Simp /* 3325101903Simp * PDR: id[4], address[4], length[4]; 3326101903Simp */ 3327101903Simp for (i = 0; i < pdrlen; ) { 3328101903Simp id = GETLE32(p); p += 4; i += 4; 3329101903Simp addr = GETLE32(p); p += 4; i += 4; 3330101903Simp len = GETLE32(p); p += 4; i += 4; 3331101903Simp /* replace PDR entry with the values from EEPROM, if any */ 3332101903Simp for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) { 3333101903Simp elen = GETLE16(q); q += 2; 3334101903Simp eid = GETLE16(q); q += 2; 3335101903Simp elen--; /* elen includes eid */ 3336101903Simp if (eid == 0) 3337101903Simp break; 3338101903Simp if (eid != id) 3339101903Simp continue; 3340101903Simp CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ); 3341101903Simp CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ); 3342101903Simp CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA, 3343101903Simp (const uint16_t *)q, len / 2); 3344101903Simp break; 3345101903Simp } 3346101903Simp } 3347101903Simp return 0; 3348101903Simp} 3349101903Simp 3350101903Simpstatic int 3351101903Simpwi_symbol_set_hcr(struct wi_softc *sc, int mode) 3352101903Simp{ 3353101903Simp uint16_t hcr; 3354101903Simp 3355101903Simp CSR_WRITE_2(sc, WI_COR, WI_COR_RESET); 3356101903Simp tsleep(sc, PWAIT, "wiinit", 1); 3357101903Simp hcr = CSR_READ_2(sc, WI_HCR); 3358101903Simp hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE); 3359101903Simp CSR_WRITE_2(sc, WI_HCR, hcr); 3360101903Simp tsleep(sc, PWAIT, "wiinit", 1); 3361101903Simp CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE); 3362101903Simp tsleep(sc, PWAIT, "wiinit", 1); 3363101903Simp return 0; 3364101903Simp} 3365