if_wi.c revision 117855
1109323Ssam/* $NetBSD: wi.c,v 1.109 2003/01/09 08:52:19 dyoung Exp $ */ 2109323Ssam 346492Swpaul/* 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 117855 2003-07-21 23:20:40Z 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> 7495706Simp#if __FreeBSD_version >= 500033 7595533Smike#include <sys/endian.h> 7695706Simp#endif 7746492Swpaul#include <sys/sockio.h> 7846492Swpaul#include <sys/mbuf.h> 7983366Sjulian#include <sys/proc.h> 8046492Swpaul#include <sys/kernel.h> 8146492Swpaul#include <sys/socket.h> 8253702Swpaul#include <sys/module.h> 8353702Swpaul#include <sys/bus.h> 8494486Simp#include <sys/random.h> 8553702Swpaul#include <sys/syslog.h> 8653702Swpaul#include <sys/sysctl.h> 8746492Swpaul 8853702Swpaul#include <machine/bus.h> 8953702Swpaul#include <machine/resource.h> 9095534Simp#include <machine/clock.h> 91116951Ssam#include <machine/atomic.h> 9253702Swpaul#include <sys/rman.h> 9353702Swpaul 9446492Swpaul#include <net/if.h> 9546492Swpaul#include <net/if_arp.h> 9646492Swpaul#include <net/ethernet.h> 9746492Swpaul#include <net/if_dl.h> 9846492Swpaul#include <net/if_media.h> 9946492Swpaul#include <net/if_types.h> 10046492Swpaul 101116951Ssam#include <net80211/ieee80211_var.h> 102116951Ssam#include <net80211/ieee80211_ioctl.h> 103116951Ssam 10446492Swpaul#include <netinet/in.h> 10546492Swpaul#include <netinet/in_systm.h> 10646492Swpaul#include <netinet/in_var.h> 10746492Swpaul#include <netinet/ip.h> 10846492Swpaul#include <netinet/if_ether.h> 10946492Swpaul 11046492Swpaul#include <net/bpf.h> 11146492Swpaul 11270808Speter#include <dev/wi/if_wavelan_ieee.h> 11393611Simp#include <dev/wi/if_wivar.h> 11470808Speter#include <dev/wi/if_wireg.h> 11546492Swpaul 116109323Ssam#define IF_POLL(ifq, m) ((m) = (ifq)->ifq_head) 117109323Ssam#define IFQ_POLL(ifq, m) IF_POLL((ifq), (m)) 118109323Ssam#define IFQ_DEQUEUE(ifq, m) IF_DEQUEUE((ifq), (m)) 119109323Ssam 12091693Simpstatic void wi_start(struct ifnet *); 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 132109323Ssamstatic int wi_get_cfg(struct ifnet *, u_long, caddr_t); 133109323Ssamstatic int wi_set_cfg(struct ifnet *, u_long, caddr_t); 134109323Ssamstatic int wi_write_txrate(struct wi_softc *); 135109323Ssamstatic int wi_write_wep(struct wi_softc *); 136109323Ssamstatic int wi_write_multi(struct wi_softc *); 137109323Ssamstatic int wi_alloc_fid(struct wi_softc *, int, int *); 138109323Ssamstatic void wi_read_nicid(struct wi_softc *); 139109323Ssamstatic int wi_write_ssid(struct wi_softc *, int, u_int8_t *, int); 14053702Swpaul 141109323Ssamstatic int wi_cmd(struct wi_softc *, int, int, int, int); 142109323Ssamstatic int wi_seek_bap(struct wi_softc *, int, int); 143109323Ssamstatic int wi_read_bap(struct wi_softc *, int, int, void *, int); 144109323Ssamstatic int wi_write_bap(struct wi_softc *, int, int, void *, int); 145109323Ssamstatic int wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int); 146109323Ssamstatic int wi_read_rid(struct wi_softc *, int, void *, int *); 147109323Ssamstatic int wi_write_rid(struct wi_softc *, int, void *, int); 14877217Sphk 149117812Ssamstatic int wi_newstate(struct ieee80211com *, enum ieee80211_state, int); 150109323Ssam 151116898Ssamstatic int wi_scan_ap(struct wi_softc *, u_int16_t, u_int16_t); 152109323Ssamstatic void wi_scan_result(struct wi_softc *, int, int); 153109323Ssam 154109323Ssamstatic void wi_dump_pkt(struct wi_frame *, struct ieee80211_node *, int rssi); 155109323Ssam 15693359Simpstatic int wi_get_debug(struct wi_softc *, struct wi_req *); 15793359Simpstatic int wi_set_debug(struct wi_softc *, struct wi_req *); 15893359Simp 159105076Simp#if __FreeBSD_version >= 500000 160101903Simp/* support to download firmware for symbol CF card */ 161101903Simpstatic int wi_symbol_write_firm(struct wi_softc *, const void *, int, 162101903Simp const void *, int); 163101903Simpstatic int wi_symbol_set_hcr(struct wi_softc *, int); 164105076Simp#endif 165101903Simp 166109323Ssamstatic __inline int 167109323Ssamwi_write_val(struct wi_softc *sc, int rid, u_int16_t val) 168109323Ssam{ 16953702Swpaul 170109323Ssam val = htole16(val); 171109323Ssam return wi_write_rid(sc, rid, &val, sizeof(val)); 172109323Ssam} 173109323Ssam 174109592SsamSYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters"); 175109592Ssam 176109323Ssamstatic struct timeval lasttxerror; /* time of last tx error msg */ 177109323Ssamstatic int curtxeps; /* current tx error msgs/sec */ 178111559Ssamstatic int wi_txerate = 0; /* tx error rate: max msgs/sec */ 179109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate, 180111559Ssam 0, "max tx error msgs/sec; 0 to disable msgs"); 181109323Ssam 182109323Ssam#define WI_DEBUG 183109323Ssam#ifdef WI_DEBUG 184109323Ssamstatic int wi_debug = 0; 185109592SsamSYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug, 186109592Ssam 0, "control debugging printfs"); 187109323Ssam 188109323Ssam#define DPRINTF(X) if (wi_debug) printf X 189109323Ssam#define DPRINTF2(X) if (wi_debug > 1) printf X 190109323Ssam#define IFF_DUMPPKTS(_ifp) \ 191109323Ssam (((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2)) 192109323Ssam#else 193109323Ssam#define DPRINTF(X) 194109323Ssam#define DPRINTF2(X) 195109323Ssam#define IFF_DUMPPKTS(_ifp) 0 196109323Ssam#endif 197109323Ssam 198109323Ssam#define WI_INTRS (WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO) 199109323Ssam 20093825Simpstruct wi_card_ident wi_card_ident[] = { 20193825Simp /* CARD_ID CARD_NAME FIRM_TYPE */ 20293825Simp { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT }, 20393825Simp { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT }, 20493825Simp { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT }, 20593825Simp { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL }, 20693825Simp { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL }, 20793825Simp { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL }, 20893825Simp { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL }, 20993825Simp { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL }, 21093825Simp { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL }, 21193825Simp { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL }, 21293825Simp { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL }, 21393825Simp { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL }, 21493825Simp { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 21593825Simp { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 216101355Simp { WI_NIC_3842_PCMCIA_ATL_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 217101355Simp { WI_NIC_3842_PCMCIA_ATS_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 21893825Simp { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 21993825Simp { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 220101355Simp { WI_NIC_3842_MINI_ATL_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 221101355Simp { WI_NIC_3842_MINI_ATS_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 22293825Simp { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 22393825Simp { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 224101355Simp { WI_NIC_3842_PCI_ATS_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 225101355Simp { WI_NIC_3842_PCI_ATL_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 22693825Simp { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 22793825Simp { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 228101355Simp { WI_NIC_P3_PCMCIA_ATL_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 229101355Simp { WI_NIC_P3_PCMCIA_ATS_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 23093825Simp { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 23193825Simp { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 232101355Simp { WI_NIC_P3_MINI_ATL_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 233101355Simp { WI_NIC_P3_MINI_ATS_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 23493825Simp { 0, NULL, 0 }, 23593825Simp}; 23693825Simp 237109323Ssamdevclass_t wi_devclass; 23846492Swpaul 23993611Simpint 240109323Ssamwi_attach(device_t dev) 24174906Salfred{ 242109323Ssam struct wi_softc *sc = device_get_softc(dev); 243109323Ssam struct ieee80211com *ic = &sc->sc_ic; 244109323Ssam struct ifnet *ifp = &ic->ic_if; 245116951Ssam int i, nrates, buflen; 246109323Ssam u_int16_t val; 247109323Ssam u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE]; 248116951Ssam struct ieee80211_rateset *rs; 249109323Ssam static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = { 250109323Ssam 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 251109323Ssam }; 252109323Ssam int error; 25374906Salfred 254109323Ssam /* 255109323Ssam * NB: no locking is needed here; don't put it here 256109323Ssam * unless you can prove it! 257109323Ssam */ 25853702Swpaul error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, 25974998Swpaul wi_intr, sc, &sc->wi_intrhand); 26053702Swpaul 26153702Swpaul if (error) { 26253702Swpaul device_printf(dev, "bus_setup_intr() failed! (%d)\n", error); 26353702Swpaul wi_free(dev); 26453702Swpaul return (error); 26553702Swpaul } 26653702Swpaul 26795534Simp#if __FreeBSD_version >= 500000 268109323Ssam mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 26993818Sjhb MTX_DEF | MTX_RECURSE); 27095534Simp#endif 27167092Swpaul 27246492Swpaul /* Reset the NIC. */ 273112096Simp if (wi_reset(sc) != 0) 274109323Ssam return ENXIO; /* XXX */ 27546492Swpaul 27676438Swpaul /* 27776438Swpaul * Read the station address. 27876438Swpaul * And do it twice. I've seen PRISM-based cards that return 27976438Swpaul * an error when trying to read it the first time, which causes 28076438Swpaul * the probe to fail. 28176438Swpaul */ 282109323Ssam buflen = IEEE80211_ADDR_LEN; 283109323Ssam error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen); 284109323Ssam if (error != 0) { 285109323Ssam buflen = IEEE80211_ADDR_LEN; 286109323Ssam error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen); 287109323Ssam } 288109323Ssam if (error || IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) { 289109323Ssam if (error != 0) 290109323Ssam device_printf(dev, "mac read failed %d\n", error); 291109323Ssam else 292109323Ssam device_printf(dev, "mac read failed (all zeros)\n"); 29375149Simp wi_free(dev); 29475149Simp return (error); 29575149Simp } 296109323Ssam device_printf(dev, "802.11 address: %6D\n", ic->ic_myaddr, ":"); 29746492Swpaul 298109323Ssam /* Read NIC identification */ 299109323Ssam wi_read_nicid(sc); 30046492Swpaul 30146492Swpaul ifp->if_softc = sc; 302109323Ssam ifp->if_unit = sc->sc_unit; 30346492Swpaul ifp->if_name = "wi"; 30446492Swpaul ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 30546492Swpaul ifp->if_ioctl = wi_ioctl; 30646492Swpaul ifp->if_start = wi_start; 30746492Swpaul ifp->if_watchdog = wi_watchdog; 30846492Swpaul ifp->if_init = wi_init; 30946492Swpaul ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 31046492Swpaul 311109323Ssam ic->ic_phytype = IEEE80211_T_DS; 312109323Ssam ic->ic_opmode = IEEE80211_M_STA; 313116951Ssam ic->ic_caps = IEEE80211_C_PMGT | IEEE80211_C_AHDEMO; 314109323Ssam ic->ic_state = IEEE80211_S_INIT; 31546492Swpaul 316116951Ssam /* 317116951Ssam * Query the card for available channels and setup the 318116951Ssam * channel table. We assume these are all 11b channels. 319116951Ssam */ 320109323Ssam buflen = sizeof(val); 321109323Ssam if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0) 322109323Ssam val = htole16(0x1fff); /* assume 1-11 */ 323116951Ssam KASSERT(val != 0, ("wi_attach: no available channels listed!")); 324116951Ssam 325116951Ssam val <<= 1; /* shift for base 1 indices */ 326116951Ssam for (i = 1; i < 16; i++) { 327116951Ssam if (isset((u_int8_t*)&val, i)) { 328116951Ssam ic->ic_channels[i].ic_freq = 329116951Ssam ieee80211_ieee2mhz(i, IEEE80211_CHAN_B); 330116951Ssam ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B; 331116951Ssam } 332109323Ssam } 33346492Swpaul 33446563Swpaul /* 33546563Swpaul * Read the default channel from the NIC. This may vary 33646563Swpaul * depending on the country where the NIC was purchased, so 33746563Swpaul * we can't hard-code a default and expect it to work for 33846563Swpaul * everyone. 339116951Ssam * 340116951Ssam * If no channel is specified, let the 802.11 code select. 34146563Swpaul */ 342109323Ssam buflen = sizeof(val); 343116951Ssam if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0) { 344116951Ssam val = le16toh(val); 345116951Ssam KASSERT(val < IEEE80211_CHAN_MAX && 346116951Ssam ic->ic_channels[val].ic_flags != 0, 347116951Ssam ("wi_attach: invalid own channel %u!", val)); 348116951Ssam ic->ic_ibss_chan = &ic->ic_channels[val]; 349117042Ssam } else { 350117042Ssam device_printf(dev, 351117042Ssam "WI_RID_OWN_CHNL failed, using first channel!\n"); 352117042Ssam ic->ic_ibss_chan = &ic->ic_channels[0]; 353109323Ssam } 35446563Swpaul 35556965Swpaul /* 35698440Simp * Set flags based on firmware version. 35798440Simp */ 35898440Simp switch (sc->sc_firmware_type) { 35998440Simp case WI_LUCENT: 360112363Simp sc->sc_ntxbuf = 1; 361109323Ssam sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE; 362109323Ssam#ifdef WI_HERMES_AUTOINC_WAR 363109323Ssam /* XXX: not confirmed, but never seen for recent firmware */ 364109323Ssam if (sc->sc_sta_firmware_ver < 40000) { 365109323Ssam sc->sc_flags |= WI_FLAGS_BUG_AUTOINC; 366109323Ssam } 367109323Ssam#endif 36898440Simp if (sc->sc_sta_firmware_ver >= 60000) 369109323Ssam sc->sc_flags |= WI_FLAGS_HAS_MOR; 370117855Ssam if (sc->sc_sta_firmware_ver >= 60006) { 371116951Ssam ic->ic_caps |= IEEE80211_C_IBSS; 372117855Ssam ic->ic_caps |= IEEE80211_C_MONITOR; 373117855Ssam } 374109323Ssam sc->sc_ibss_port = htole16(1); 37598440Simp break; 376109323Ssam 37798440Simp case WI_INTERSIL: 378112363Simp sc->sc_ntxbuf = WI_NTXBUF; 379109323Ssam sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR; 380109323Ssam sc->sc_flags |= WI_FLAGS_HAS_ROAMING; 381109323Ssam sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE; 382109323Ssam if (sc->sc_sta_firmware_ver > 10101) 383109323Ssam sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST; 384117855Ssam if (sc->sc_sta_firmware_ver >= 800) { 385116951Ssam ic->ic_caps |= IEEE80211_C_IBSS; 386117855Ssam ic->ic_caps |= IEEE80211_C_MONITOR; 387117855Ssam } 388109396Simp /* 389109396Simp * version 0.8.3 and newer are the only ones that are known 390109396Simp * to currently work. Earlier versions can be made to work, 391109396Simp * at least according to the Linux driver. 392109396Simp */ 393100734Simp if (sc->sc_sta_firmware_ver >= 803) 394116951Ssam ic->ic_caps |= IEEE80211_C_HOSTAP; 395109323Ssam sc->sc_ibss_port = htole16(0); 39698440Simp break; 397109323Ssam 39898440Simp case WI_SYMBOL: 399112363Simp sc->sc_ntxbuf = 1; 400109323Ssam sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY; 40198440Simp if (sc->sc_sta_firmware_ver >= 25000) 402116951Ssam ic->ic_caps |= IEEE80211_C_IBSS; 403109323Ssam sc->sc_ibss_port = htole16(4); 40498440Simp break; 40598440Simp } 40698440Simp 40798440Simp /* 40856965Swpaul * Find out if we support WEP on this card. 40956965Swpaul */ 410109323Ssam buflen = sizeof(val); 411109323Ssam if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 && 412109323Ssam val != htole16(0)) 413116951Ssam ic->ic_caps |= IEEE80211_C_WEP; 41456965Swpaul 415109323Ssam /* Find supported rates. */ 416109323Ssam buflen = sizeof(ratebuf); 417116951Ssam rs = &ic->ic_sup_rates[IEEE80211_MODE_11B]; 418109323Ssam if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) { 419116951Ssam nrates = le16toh(*(u_int16_t *)ratebuf); 420116951Ssam if (nrates > IEEE80211_RATE_MAXSIZE) 421116951Ssam nrates = IEEE80211_RATE_MAXSIZE; 422116951Ssam rs->rs_nrates = 0; 423116951Ssam for (i = 0; i < nrates; i++) 424116951Ssam if (ratebuf[2+i]) 425116951Ssam rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i]; 426109323Ssam } else { 427109323Ssam /* XXX fallback on error? */ 428116951Ssam rs->rs_nrates = 0; 42998440Simp } 43077217Sphk 431109323Ssam buflen = sizeof(val); 432109323Ssam if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) && 433109323Ssam wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) { 434109323Ssam sc->sc_dbm_adjust = le16toh(val); 435109323Ssam } else 436109323Ssam sc->sc_dbm_adjust = 100; /* default */ 43746492Swpaul 438109323Ssam sc->sc_max_datalen = 2304; 439109323Ssam sc->sc_system_scale = 1; 440109323Ssam sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN; 441109323Ssam sc->sc_roaming_mode = 1; 44246492Swpaul 443109323Ssam sc->sc_portnum = WI_DEFAULT_PORT; 444109323Ssam sc->sc_authtype = WI_DEFAULT_AUTHTYPE; 44587383Simp 446109323Ssam bzero(sc->sc_nodename, sizeof(sc->sc_nodename)); 447109323Ssam sc->sc_nodelen = sizeof(WI_DEFAULT_NODENAME) - 1; 448109323Ssam bcopy(WI_DEFAULT_NODENAME, sc->sc_nodename, sc->sc_nodelen); 44987383Simp 450109323Ssam bzero(sc->sc_net_name, sizeof(sc->sc_net_name)); 451109323Ssam bcopy(WI_DEFAULT_NETNAME, sc->sc_net_name, 452109323Ssam sizeof(WI_DEFAULT_NETNAME) - 1); 45393733Simp 45493733Simp /* 455109323Ssam * Call MI attach routine. 45693733Simp */ 457109323Ssam ieee80211_ifattach(ifp); 458117812Ssam /* override state transition method */ 459117812Ssam sc->sc_newstate = ic->ic_newstate; 460117812Ssam ic->ic_newstate = wi_newstate; 461116951Ssam ieee80211_media_init(ifp, wi_media_change, wi_media_status); 462109323Ssam 463109323Ssam return (0); 46487383Simp} 46587383Simp 466109323Ssamint 467109323Ssamwi_detach(device_t dev) 46846492Swpaul{ 469109323Ssam struct wi_softc *sc = device_get_softc(dev); 470109323Ssam struct ifnet *ifp = &sc->sc_ic.ic_if; 471109323Ssam WI_LOCK_DECL(); 47246492Swpaul 473109323Ssam WI_LOCK(sc); 47446492Swpaul 475109323Ssam /* check if device was removed */ 476109323Ssam sc->wi_gone = !bus_child_present(dev); 47746492Swpaul 478109323Ssam wi_stop(ifp, 0); 47946492Swpaul 480109323Ssam ieee80211_ifdetach(ifp); 481114190Simp WI_UNLOCK(sc); 482109323Ssam bus_teardown_intr(dev, sc->irq, sc->wi_intrhand); 483109323Ssam wi_free(dev); 484109323Ssam#if __FreeBSD_version >= 500000 485109323Ssam mtx_destroy(&sc->sc_mtx); 48693359Simp#endif 487109323Ssam return (0); 488109323Ssam} 48993359Simp 490109323Ssam#ifdef __NetBSD__ 491109323Ssamint 492109323Ssamwi_activate(struct device *self, enum devact act) 493109323Ssam{ 494109323Ssam struct wi_softc *sc = (struct wi_softc *)self; 495109323Ssam int rv = 0, s; 49693359Simp 497109323Ssam s = splnet(); 498109323Ssam switch (act) { 499109323Ssam case DVACT_ACTIVATE: 500109323Ssam rv = EOPNOTSUPP; 501109323Ssam break; 50293359Simp 503109323Ssam case DVACT_DEACTIVATE: 504109323Ssam if_deactivate(&sc->sc_ic.ic_if); 505109323Ssam break; 506109323Ssam } 507109323Ssam splx(s); 508109323Ssam return rv; 509109323Ssam} 51093359Simp 511109323Ssamvoid 512109323Ssamwi_power(struct wi_softc *sc, int why) 513109323Ssam{ 514109323Ssam struct ifnet *ifp = &sc->sc_ic.ic_if; 515109323Ssam int s; 51693359Simp 517109323Ssam s = splnet(); 518109323Ssam switch (why) { 519109323Ssam case PWR_SUSPEND: 520109323Ssam case PWR_STANDBY: 521109323Ssam wi_stop(ifp, 1); 522109323Ssam break; 523109323Ssam case PWR_RESUME: 524109323Ssam if (ifp->if_flags & IFF_UP) { 525109323Ssam wi_init(ifp); 526109323Ssam (void)wi_intr(sc); 52794405Simp } 528109323Ssam break; 529109323Ssam case PWR_SOFTSUSPEND: 530109323Ssam case PWR_SOFTSTANDBY: 531109323Ssam case PWR_SOFTRESUME: 532109323Ssam break; 53393359Simp } 534109323Ssam splx(s); 53546492Swpaul} 536109323Ssam#endif /* __NetBSD__ */ 53746492Swpaul 538109323Ssamvoid 539109323Ssamwi_shutdown(device_t dev) 54046492Swpaul{ 541109323Ssam struct wi_softc *sc = device_get_softc(dev); 54246492Swpaul 543109323Ssam wi_stop(&sc->sc_if, 1); 54446492Swpaul} 54546492Swpaul 546109323Ssamvoid 547109323Ssamwi_intr(void *arg) 54846492Swpaul{ 549109323Ssam struct wi_softc *sc = arg; 550109323Ssam struct ifnet *ifp = &sc->sc_ic.ic_if; 551112363Simp u_int16_t status; 552109323Ssam WI_LOCK_DECL(); 55346492Swpaul 554109323Ssam WI_LOCK(sc); 55546492Swpaul 556109323Ssam if (sc->wi_gone || (ifp->if_flags & IFF_UP) == 0) { 557113327Simp CSR_WRITE_2(sc, WI_INT_EN, 0); 558109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, ~0); 559109323Ssam WI_UNLOCK(sc); 56046492Swpaul return; 561109323Ssam } 56246492Swpaul 563112363Simp /* Disable interrupts. */ 564112363Simp CSR_WRITE_2(sc, WI_INT_EN, 0); 56546492Swpaul 566112363Simp status = CSR_READ_2(sc, WI_EVENT_STAT); 567112363Simp if (status & WI_EV_RX) 568112363Simp wi_rx_intr(sc); 569112363Simp if (status & WI_EV_ALLOC) 570112363Simp wi_tx_intr(sc); 571112363Simp if (status & WI_EV_TX_EXC) 572112363Simp wi_tx_ex_intr(sc); 573112363Simp if (status & WI_EV_INFO) 574112363Simp wi_info_intr(sc); 575112363Simp if ((ifp->if_flags & IFF_OACTIVE) == 0 && 576112363Simp (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 && 577112363Simp _IF_QLEN(&ifp->if_snd) != 0) 578112363Simp wi_start(ifp); 57946492Swpaul 580112363Simp /* Re-enable interrupts. */ 581112363Simp CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 58246492Swpaul 583109323Ssam WI_UNLOCK(sc); 58446492Swpaul 58546492Swpaul return; 58646492Swpaul} 58746492Swpaul 588109323Ssamvoid 589109323Ssamwi_init(void *arg) 59046492Swpaul{ 591109323Ssam struct wi_softc *sc = arg; 592109323Ssam struct ifnet *ifp = &sc->sc_if; 593109323Ssam struct ieee80211com *ic = &sc->sc_ic; 594109323Ssam struct wi_joinreq join; 595109323Ssam int i; 596109323Ssam int error = 0, wasenabled; 597109323Ssam struct ifaddr *ifa; 598109323Ssam struct sockaddr_dl *sdl; 599109323Ssam WI_LOCK_DECL(); 60046492Swpaul 601109323Ssam WI_LOCK(sc); 60267092Swpaul 603109323Ssam if (sc->wi_gone) { 604109323Ssam WI_UNLOCK(sc); 60546492Swpaul return; 60646492Swpaul } 60746492Swpaul 608112362Simp if ((wasenabled = sc->sc_enabled)) 609109323Ssam wi_stop(ifp, 0); 610112362Simp wi_reset(sc); 61146492Swpaul 612109323Ssam /* common 802.11 configuration */ 613109323Ssam ic->ic_flags &= ~IEEE80211_F_IBSSON; 614109323Ssam sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 615109323Ssam switch (ic->ic_opmode) { 616109323Ssam case IEEE80211_M_STA: 617109323Ssam wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS); 618109323Ssam break; 619109323Ssam case IEEE80211_M_IBSS: 620109323Ssam wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port); 621109323Ssam ic->ic_flags |= IEEE80211_F_IBSSON; 622109323Ssam break; 623109323Ssam case IEEE80211_M_AHDEMO: 624109323Ssam wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC); 625109323Ssam break; 626109323Ssam case IEEE80211_M_HOSTAP: 627109323Ssam wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP); 628109323Ssam break; 629117855Ssam case IEEE80211_M_MONITOR: 630117855Ssam if (sc->sc_firmware_type == WI_LUCENT) 631117855Ssam wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC); 632117855Ssam wi_cmd(sc, WI_CMD_DEBUG | (WI_TEST_MONITOR << 8), 0, 0, 0); 633117855Ssam break; 63446492Swpaul } 63546492Swpaul 636109323Ssam /* Intersil interprets this RID as joining ESS even in IBSS mode */ 637109323Ssam if (sc->sc_firmware_type == WI_LUCENT && 638109323Ssam (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0) 639109323Ssam wi_write_val(sc, WI_RID_CREATE_IBSS, 1); 640109323Ssam else 641109323Ssam wi_write_val(sc, WI_RID_CREATE_IBSS, 0); 642109323Ssam wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval); 643109323Ssam wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid, 644109323Ssam ic->ic_des_esslen); 645116951Ssam wi_write_val(sc, WI_RID_OWN_CHNL, 646116951Ssam ieee80211_chan2ieee(ic, ic->ic_ibss_chan)); 647109323Ssam wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen); 64846492Swpaul 649109323Ssam ifa = ifaddr_byindex(ifp->if_index); 650109323Ssam sdl = (struct sockaddr_dl *) ifa->ifa_addr; 651109323Ssam IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(sdl)); 652109323Ssam wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN); 65375373Salfred 654109323Ssam wi_write_val(sc, WI_RID_PM_ENABLED, 655109323Ssam (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0); 65646492Swpaul 657109323Ssam /* not yet common 802.11 configuration */ 658109323Ssam wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen); 659116951Ssam wi_write_val(sc, WI_RID_RTS_THRESH, ic->ic_rtsthreshold); 660109323Ssam if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) 661116951Ssam wi_write_val(sc, WI_RID_FRAG_THRESH, ic->ic_fragthreshold); 66246492Swpaul 663109323Ssam /* driver specific 802.11 configuration */ 664109323Ssam if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) 665109323Ssam wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale); 666109323Ssam if (sc->sc_flags & WI_FLAGS_HAS_ROAMING) 667109323Ssam wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode); 668109323Ssam if (sc->sc_flags & WI_FLAGS_HAS_MOR) 669109323Ssam wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven); 670109323Ssam wi_write_txrate(sc); 671109323Ssam wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen); 67246492Swpaul 673109323Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP && 674109323Ssam sc->sc_firmware_type == WI_INTERSIL) { 675109323Ssam wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval); 676109323Ssam wi_write_val(sc, WI_RID_BASIC_RATE, 0x03); /* 1, 2 */ 677109323Ssam wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */ 678109323Ssam wi_write_val(sc, WI_RID_DTIM_PERIOD, 1); 67946492Swpaul } 68046492Swpaul 681109323Ssam /* 682109323Ssam * Initialize promisc mode. 683109323Ssam * Being in the Host-AP mode causes a great 684109323Ssam * deal of pain if primisc mode is set. 685109323Ssam * Therefore we avoid confusing the firmware 686109323Ssam * and always reset promisc mode in Host-AP 687109323Ssam * mode. Host-AP sees all the packets anyway. 688109323Ssam */ 689109323Ssam if (ic->ic_opmode != IEEE80211_M_HOSTAP && 690109323Ssam (ifp->if_flags & IFF_PROMISC) != 0) { 691109323Ssam wi_write_val(sc, WI_RID_PROMISC, 1); 692109323Ssam } else { 693109323Ssam wi_write_val(sc, WI_RID_PROMISC, 0); 69475373Salfred } 69546492Swpaul 696109323Ssam /* Configure WEP. */ 697116951Ssam if (ic->ic_caps & IEEE80211_C_WEP) 698109323Ssam wi_write_wep(sc); 69967092Swpaul 700109323Ssam /* Set multicast filter. */ 701109323Ssam wi_write_multi(sc); 70246492Swpaul 703114124Simp /* Allocate fids for the card */ 704109323Ssam if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) { 705109323Ssam sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame); 706109323Ssam if (sc->sc_firmware_type == WI_SYMBOL) 707109323Ssam sc->sc_buflen = 1585; /* XXX */ 708112363Simp for (i = 0; i < sc->sc_ntxbuf; i++) { 709109323Ssam error = wi_alloc_fid(sc, sc->sc_buflen, 710109323Ssam &sc->sc_txd[i].d_fid); 711109323Ssam if (error) { 712109323Ssam device_printf(sc->sc_dev, 713109323Ssam "tx buffer allocation failed (error %u)\n", 714109323Ssam error); 715109323Ssam goto out; 716109323Ssam } 717109323Ssam sc->sc_txd[i].d_len = 0; 71870073Swpaul } 71970073Swpaul } 720109323Ssam sc->sc_txcur = sc->sc_txnext = 0; 72170073Swpaul 722109323Ssam /* Enable desired port */ 723109323Ssam wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0); 72446492Swpaul 725114124Simp sc->sc_enabled = 1; 726109323Ssam ifp->if_flags |= IFF_RUNNING; 727109323Ssam ifp->if_flags &= ~IFF_OACTIVE; 728109323Ssam if (ic->ic_opmode == IEEE80211_M_AHDEMO || 729117855Ssam ic->ic_opmode == IEEE80211_M_MONITOR || 730109323Ssam ic->ic_opmode == IEEE80211_M_HOSTAP) 731117812Ssam ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 73246492Swpaul 733109323Ssam /* Enable interrupts */ 734109323Ssam CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 73546492Swpaul 736109323Ssam if (!wasenabled && 737109323Ssam ic->ic_opmode == IEEE80211_M_HOSTAP && 738109323Ssam sc->sc_firmware_type == WI_INTERSIL) { 739109323Ssam /* XXX: some card need to be re-enabled for hostap */ 740109323Ssam wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0); 741109323Ssam wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0); 74275149Simp } 74394397Simp 744109323Ssam if (ic->ic_opmode == IEEE80211_M_STA && 745109323Ssam ((ic->ic_flags & IEEE80211_F_DESBSSID) || 746116951Ssam ic->ic_des_chan != IEEE80211_CHAN_ANYC)) { 747109323Ssam memset(&join, 0, sizeof(join)); 748109323Ssam if (ic->ic_flags & IEEE80211_F_DESBSSID) 749109323Ssam IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid); 750116951Ssam if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) 751116951Ssam join.wi_chan = htole16( 752116951Ssam ieee80211_chan2ieee(ic, ic->ic_des_chan)); 753109323Ssam /* Lucent firmware does not support the JOIN RID. */ 754109323Ssam if (sc->sc_firmware_type != WI_LUCENT) 755109323Ssam wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join)); 75694397Simp } 75775373Salfred 758109323Ssam WI_UNLOCK(sc); 75946492Swpaul return; 760109323Ssamout: 761109323Ssam if (error) { 762109323Ssam if_printf(ifp, "interface not running\n"); 763109323Ssam wi_stop(ifp, 0); 76470073Swpaul } 765110224Simp WI_UNLOCK(sc); 766109323Ssam DPRINTF(("wi_init: return %d\n", error)); 767109323Ssam return; 76846492Swpaul} 76946492Swpaul 770109323Ssamvoid 771109323Ssamwi_stop(struct ifnet *ifp, int disable) 77246492Swpaul{ 773117812Ssam struct ieee80211com *ic = (struct ieee80211com *) ifp; 774109323Ssam struct wi_softc *sc = ifp->if_softc; 775109323Ssam WI_LOCK_DECL(); 77646492Swpaul 777109323Ssam WI_LOCK(sc); 77874998Swpaul 779117812Ssam ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 780109323Ssam if (sc->sc_enabled && !sc->wi_gone) { 781109323Ssam CSR_WRITE_2(sc, WI_INT_EN, 0); 782109323Ssam wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0); 783109323Ssam if (disable) { 784109323Ssam#ifdef __NetBSD__ 785109323Ssam if (sc->sc_disable) 786109323Ssam (*sc->sc_disable)(sc); 787109323Ssam#endif 788116951Ssam sc->sc_enabled = 0; 78970073Swpaul } 79070073Swpaul } 79170073Swpaul 792109323Ssam sc->sc_tx_timer = 0; 793109323Ssam sc->sc_scan_timer = 0; 794109323Ssam sc->sc_syn_timer = 0; 795109323Ssam sc->sc_false_syns = 0; 796109323Ssam sc->sc_naps = 0; 797109323Ssam ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING); 798109323Ssam ifp->if_timer = 0; 79946492Swpaul 800109323Ssam WI_UNLOCK(sc); 80146492Swpaul} 80246492Swpaul 803109323Ssamstatic void 804109323Ssamwi_start(struct ifnet *ifp) 80546492Swpaul{ 806109323Ssam struct wi_softc *sc = ifp->if_softc; 807109323Ssam struct ieee80211com *ic = &sc->sc_ic; 808109323Ssam struct ieee80211_frame *wh; 809109323Ssam struct mbuf *m0; 810109323Ssam struct wi_frame frmhdr; 811109323Ssam int cur, fid, off; 812109323Ssam WI_LOCK_DECL(); 81346492Swpaul 814109323Ssam WI_LOCK(sc); 81546492Swpaul 816109323Ssam if (sc->wi_gone) { 817109323Ssam WI_UNLOCK(sc); 818109323Ssam return; 81946492Swpaul } 820109323Ssam if (sc->sc_flags & WI_FLAGS_OUTRANGE) { 821109323Ssam WI_UNLOCK(sc); 822109323Ssam return; 82375373Salfred } 82446492Swpaul 825109323Ssam memset(&frmhdr, 0, sizeof(frmhdr)); 826109323Ssam cur = sc->sc_txnext; 827109323Ssam for (;;) { 828109323Ssam IF_POLL(&ic->ic_mgtq, m0); 829109323Ssam if (m0 != NULL) { 830109323Ssam if (sc->sc_txd[cur].d_len != 0) { 831109323Ssam ifp->if_flags |= IFF_OACTIVE; 832109323Ssam break; 833109323Ssam } 834109323Ssam IF_DEQUEUE(&ic->ic_mgtq, m0); 835109323Ssam m_copydata(m0, 4, ETHER_ADDR_LEN * 2, 836109323Ssam (caddr_t)&frmhdr.wi_ehdr); 837109323Ssam frmhdr.wi_ehdr.ether_type = 0; 838109323Ssam wh = mtod(m0, struct ieee80211_frame *); 839109323Ssam } else { 840109323Ssam if (ic->ic_state != IEEE80211_S_RUN) 841109323Ssam break; 842109323Ssam IFQ_POLL(&ifp->if_snd, m0); 843109323Ssam if (m0 == NULL) 844109323Ssam break; 845109323Ssam if (sc->sc_txd[cur].d_len != 0) { 846109323Ssam ifp->if_flags |= IFF_OACTIVE; 847109323Ssam break; 848109323Ssam } 849109323Ssam IFQ_DEQUEUE(&ifp->if_snd, m0); 850109323Ssam ifp->if_opackets++; 851109323Ssam m_copydata(m0, 0, ETHER_HDR_LEN, 852109323Ssam (caddr_t)&frmhdr.wi_ehdr); 853109323Ssam#if NBPFILTER > 0 854109323Ssam BPF_MTAP(ifp, m0); 855109323Ssam#endif 85646492Swpaul 857109323Ssam if ((m0 = ieee80211_encap(ifp, m0)) == NULL) { 858109323Ssam ifp->if_oerrors++; 859109323Ssam continue; 860109323Ssam } 861109323Ssam wh = mtod(m0, struct ieee80211_frame *); 862109323Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP && 863109323Ssam !IEEE80211_IS_MULTICAST(wh->i_addr1) && 864109323Ssam (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 865116951Ssam IEEE80211_FC0_TYPE_DATA) { 866116951Ssam struct ieee80211_node *ni = 867116951Ssam ieee80211_find_node(ic, wh->i_addr1); 868116951Ssam int err = (ni == NULL || ni->ni_associd == 0); 869116951Ssam if (ni != NULL) 870116951Ssam ieee80211_unref_node(&ni); 871116951Ssam if (err) { 872116951Ssam m_freem(m0); 873116951Ssam ifp->if_oerrors++; 874116951Ssam continue; 875116951Ssam } 876109323Ssam } 877109323Ssam if (ic->ic_flags & IEEE80211_F_WEPON) 878109323Ssam wh->i_fc[1] |= IEEE80211_FC1_WEP; 87946492Swpaul 880109323Ssam } 881109323Ssam#if NBPFILTER > 0 882109323Ssam if (ic->ic_rawbpf) 883109323Ssam bpf_mtap(ic->ic_rawbpf, m0); 884109323Ssam#endif 885109323Ssam frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX); 886109323Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP && 887109323Ssam (wh->i_fc[1] & IEEE80211_FC1_WEP)) { 888109323Ssam if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) { 889109323Ssam ifp->if_oerrors++; 890109323Ssam continue; 891109323Ssam } 892109323Ssam frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT); 893109323Ssam } 894109323Ssam m_copydata(m0, 0, sizeof(struct ieee80211_frame), 895109323Ssam (caddr_t)&frmhdr.wi_whdr); 896109323Ssam m_adj(m0, sizeof(struct ieee80211_frame)); 897109323Ssam frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len); 898109323Ssam#if NBPFILTER > 0 899109323Ssam if (sc->sc_drvbpf) { 900109323Ssam struct mbuf *mb; 90146492Swpaul 902111119Simp MGETHDR(mb, M_DONTWAIT, m0->m_type); 903109323Ssam if (mb != NULL) { 904111119Simp (void) m_dup_pkthdr(mb, m0, M_DONTWAIT); 905109323Ssam mb->m_next = m0; 906109323Ssam mb->m_data = (caddr_t)&frmhdr; 907109323Ssam mb->m_len = sizeof(frmhdr); 908109323Ssam mb->m_pkthdr.len += mb->m_len; 909109323Ssam bpf_mtap(sc->sc_drvbpf, mb); 910109323Ssam m_free(mb); 911109323Ssam } 912109323Ssam } 91346492Swpaul#endif 914109323Ssam if (IFF_DUMPPKTS(ifp)) 915116951Ssam wi_dump_pkt(&frmhdr, NULL, -1); 916109323Ssam fid = sc->sc_txd[cur].d_fid; 917109323Ssam off = sizeof(frmhdr); 918109323Ssam if (wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0 || 919109323Ssam wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0) { 920109323Ssam ifp->if_oerrors++; 921109323Ssam m_freem(m0); 922109323Ssam continue; 923109323Ssam } 924109323Ssam m_freem(m0); 925109323Ssam sc->sc_txd[cur].d_len = off; 926109323Ssam if (sc->sc_txcur == cur) { 927109323Ssam if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) { 928109323Ssam if_printf(ifp, "xmit failed\n"); 929109323Ssam sc->sc_txd[cur].d_len = 0; 930109323Ssam continue; 931109323Ssam } 932109323Ssam sc->sc_tx_timer = 5; 933109323Ssam ifp->if_timer = 1; 934109323Ssam } 935112363Simp sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf; 93674838Salfred } 93746492Swpaul 938109323Ssam WI_UNLOCK(sc); 93946492Swpaul} 94046492Swpaul 94188546Salfredstatic int 942109323Ssamwi_reset(struct wi_softc *sc) 94346492Swpaul{ 944114124Simp struct ieee80211com *ic = &sc->sc_ic; 945114124Simp struct ifnet *ifp = &ic->ic_if; 946114124Simp#define WI_INIT_TRIES 3 947114124Simp int i; 948114138Simp int error = 0; 949114124Simp int tries; 950114124Simp 951114124Simp /* Symbol firmware cannot be initialized more than once */ 952116276Simp if (sc->sc_firmware_type != WI_INTERSIL && sc->sc_reset) 953112362Simp return (0); 954114124Simp if (sc->sc_firmware_type == WI_SYMBOL) 955114124Simp tries = 1; 956114124Simp else 957114124Simp tries = WI_INIT_TRIES; 958112362Simp 959114124Simp for (i = 0; i < tries; i++) { 960109323Ssam if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0) 96146492Swpaul break; 962109323Ssam DELAY(WI_DELAY * 1000); 96346492Swpaul } 964114124Simp sc->sc_reset = 1; 96546492Swpaul 966114124Simp if (i == tries) { 967114124Simp if_printf(ifp, "init failed\n"); 968114124Simp return (error); 96975373Salfred } 97046492Swpaul 971109323Ssam CSR_WRITE_2(sc, WI_INT_EN, 0); 972114124Simp CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF); 97346492Swpaul 974109323Ssam /* Calibrate timer. */ 975114124Simp wi_write_val(sc, WI_RID_TICK_TIME, 8); 976114124Simp 977114124Simp return (0); 978109323Ssam#undef WI_INIT_TRIES 97946492Swpaul} 98046492Swpaul 98188546Salfredstatic void 982109323Ssamwi_watchdog(struct ifnet *ifp) 98346492Swpaul{ 984109323Ssam struct wi_softc *sc = ifp->if_softc; 98546492Swpaul 986109323Ssam ifp->if_timer = 0; 987109323Ssam if (!sc->sc_enabled) 988109323Ssam return; 98946492Swpaul 990109323Ssam if (sc->sc_tx_timer) { 991109323Ssam if (--sc->sc_tx_timer == 0) { 992109323Ssam if_printf(ifp, "device timeout\n"); 993109323Ssam ifp->if_oerrors++; 994109323Ssam wi_init(ifp->if_softc); 995109323Ssam return; 996109323Ssam } 997109323Ssam ifp->if_timer = 1; 99846492Swpaul } 99946492Swpaul 1000109323Ssam if (sc->sc_scan_timer) { 1001109323Ssam if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT && 1002109323Ssam sc->sc_firmware_type == WI_INTERSIL) { 1003109323Ssam DPRINTF(("wi_watchdog: inquire scan\n")); 1004109323Ssam wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0); 100546492Swpaul } 1006109323Ssam if (sc->sc_scan_timer) 1007109323Ssam ifp->if_timer = 1; 100846492Swpaul } 100946492Swpaul 1010109323Ssam if (sc->sc_syn_timer) { 1011109323Ssam if (--sc->sc_syn_timer == 0) { 1012117812Ssam struct ieee80211com *ic = (struct ieee80211com *) ifp; 1013109323Ssam DPRINTF2(("wi_watchdog: %d false syns\n", 1014109323Ssam sc->sc_false_syns)); 1015109323Ssam sc->sc_false_syns = 0; 1016117812Ssam ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1017109323Ssam sc->sc_syn_timer = 5; 1018109323Ssam } 1019109323Ssam ifp->if_timer = 1; 102046492Swpaul } 102146492Swpaul 1022109323Ssam /* TODO: rate control */ 1023109323Ssam ieee80211_watchdog(ifp); 102446492Swpaul} 102546492Swpaul 102688546Salfredstatic int 1027109323Ssamwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 102846492Swpaul{ 1029109323Ssam struct wi_softc *sc = ifp->if_softc; 1030109323Ssam struct ieee80211com *ic = &sc->sc_ic; 1031109323Ssam struct ifreq *ifr = (struct ifreq *)data; 1032109323Ssam struct ieee80211req *ireq; 1033109323Ssam u_int8_t nodename[IEEE80211_NWID_LEN]; 1034109323Ssam int error = 0; 103595534Simp#if __FreeBSD_version >= 500000 1036109323Ssam struct thread *td = curthread; 103795534Simp#else 1038109323Ssam struct proc *td = curproc; /* Little white lie */ 103995534Simp#endif 1040109323Ssam struct wi_req wreq; 1041109323Ssam WI_LOCK_DECL(); 104246492Swpaul 1043109323Ssam WI_LOCK(sc); 104446492Swpaul 104561818Sroberto if (sc->wi_gone) { 104661818Sroberto error = ENODEV; 104761818Sroberto goto out; 104861818Sroberto } 104946492Swpaul 1050109323Ssam switch (cmd) { 105146492Swpaul case SIOCSIFFLAGS: 1052100876Simp /* 1053101139Simp * Can't do promisc and hostap at the same time. If all that's 1054101139Simp * changing is the promisc flag, try to short-circuit a call to 1055101139Simp * wi_init() by just setting PROMISC in the hardware. 1056100876Simp */ 105746492Swpaul if (ifp->if_flags & IFF_UP) { 1058109323Ssam if (ic->ic_opmode != IEEE80211_M_HOSTAP && 1059101139Simp ifp->if_flags & IFF_RUNNING) { 1060101139Simp if (ifp->if_flags & IFF_PROMISC && 1061109323Ssam !(sc->sc_if_flags & IFF_PROMISC)) { 1062109323Ssam wi_write_val(sc, WI_RID_PROMISC, 1); 1063101139Simp } else if (!(ifp->if_flags & IFF_PROMISC) && 1064109323Ssam sc->sc_if_flags & IFF_PROMISC) { 1065109323Ssam wi_write_val(sc, WI_RID_PROMISC, 0); 1066101139Simp } else { 1067101139Simp wi_init(sc); 1068101139Simp } 1069100876Simp } else { 107046492Swpaul wi_init(sc); 1071100876Simp } 107246492Swpaul } else { 107346492Swpaul if (ifp->if_flags & IFF_RUNNING) { 1074109323Ssam wi_stop(ifp, 0); 107546492Swpaul } 107646492Swpaul } 1077109323Ssam sc->sc_if_flags = ifp->if_flags; 107846492Swpaul error = 0; 107946492Swpaul break; 108046492Swpaul case SIOCADDMULTI: 108146492Swpaul case SIOCDELMULTI: 1082109323Ssam error = wi_write_multi(sc); 108346492Swpaul break; 1084109323Ssam case SIOCGIFGENERIC: 1085109323Ssam error = wi_get_cfg(ifp, cmd, data); 108646492Swpaul break; 1087109323Ssam case SIOCSIFGENERIC: 1088109323Ssam error = suser(td); 108946492Swpaul if (error) 109046492Swpaul break; 1091109323Ssam error = wi_set_cfg(ifp, cmd, data); 109246492Swpaul break; 109393359Simp case SIOCGPRISM2DEBUG: 109493359Simp error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 109593359Simp if (error) 109693359Simp break; 109793733Simp if (!(ifp->if_flags & IFF_RUNNING) || 109893733Simp sc->sc_firmware_type == WI_LUCENT) { 109993359Simp error = EIO; 110093359Simp break; 110193359Simp } 110293359Simp error = wi_get_debug(sc, &wreq); 110393359Simp if (error == 0) 110493359Simp error = copyout(&wreq, ifr->ifr_data, sizeof(wreq)); 110593359Simp break; 110693359Simp case SIOCSPRISM2DEBUG: 110793593Sjhb if ((error = suser(td))) 110893359Simp goto out; 110993359Simp error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 111093359Simp if (error) 111193359Simp break; 111293359Simp error = wi_set_debug(sc, &wreq); 111393359Simp break; 111477217Sphk case SIOCG80211: 1115109323Ssam ireq = (struct ieee80211req *) data; 1116109323Ssam switch (ireq->i_type) { 111777217Sphk case IEEE80211_IOC_STATIONNAME: 1118109323Ssam ireq->i_len = sc->sc_nodelen + 1; 1119109323Ssam error = copyout(sc->sc_nodename, ireq->i_data, 1120109323Ssam ireq->i_len); 112177217Sphk break; 1122109323Ssam default: 1123109323Ssam error = ieee80211_ioctl(ifp, cmd, data); 112477217Sphk break; 112577217Sphk } 112677217Sphk break; 112777217Sphk case SIOCS80211: 1128109323Ssam error = suser(td); 1129109323Ssam if (error) 1130109323Ssam break; 1131109323Ssam ireq = (struct ieee80211req *) data; 1132109323Ssam switch (ireq->i_type) { 1133109323Ssam case IEEE80211_IOC_STATIONNAME: 113477217Sphk if (ireq->i_val != 0 || 113577217Sphk ireq->i_len > IEEE80211_NWID_LEN) { 113677217Sphk error = EINVAL; 113777217Sphk break; 113877217Sphk } 1139109323Ssam memset(nodename, 0, IEEE80211_NWID_LEN); 1140109323Ssam error = copyin(ireq->i_data, nodename, ireq->i_len); 1141109323Ssam if (error) 114277217Sphk break; 1143109323Ssam if (sc->sc_enabled) { 1144109323Ssam error = wi_write_ssid(sc, WI_RID_NODENAME, 1145109323Ssam nodename, ireq->i_len); 1146109323Ssam if (error) 1147109323Ssam break; 114877217Sphk } 1149109323Ssam memcpy(sc->sc_nodename, nodename, IEEE80211_NWID_LEN); 1150109323Ssam sc->sc_nodelen = ireq->i_len; 115177217Sphk break; 115277217Sphk default: 1153109323Ssam error = ieee80211_ioctl(ifp, cmd, data); 115477217Sphk break; 115577217Sphk } 115677217Sphk break; 115746492Swpaul default: 1158109323Ssam error = ieee80211_ioctl(ifp, cmd, data); 115946492Swpaul break; 116046492Swpaul } 1161109323Ssam if (error == ENETRESET) { 1162109323Ssam if (sc->sc_enabled) 1163116951Ssam wi_init(sc); /* XXX no error return */ 1164109323Ssam error = 0; 1165109323Ssam } 116661818Srobertoout: 1167109323Ssam WI_UNLOCK(sc); 116846492Swpaul 1169109323Ssam return (error); 117046492Swpaul} 117146492Swpaul 1172109323Ssamstatic int 1173109323Ssamwi_media_change(struct ifnet *ifp) 117446492Swpaul{ 1175109323Ssam struct wi_softc *sc = ifp->if_softc; 1176116951Ssam int error; 117746492Swpaul 1178116951Ssam error = ieee80211_media_change(ifp); 1179109323Ssam if (error == ENETRESET) { 1180109323Ssam if (sc->sc_enabled) 1181116951Ssam wi_init(sc); /* XXX no error return */ 1182109323Ssam error = 0; 1183109323Ssam } 1184109323Ssam return error; 1185109323Ssam} 118646492Swpaul 1187109323Ssamstatic void 1188109323Ssamwi_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1189109323Ssam{ 1190109323Ssam struct wi_softc *sc = ifp->if_softc; 1191109323Ssam struct ieee80211com *ic = &sc->sc_ic; 1192109323Ssam u_int16_t val; 1193109323Ssam int rate, len; 119446492Swpaul 1195109323Ssam if (sc->wi_gone || !sc->sc_enabled) { 1196109323Ssam imr->ifm_active = IFM_IEEE80211 | IFM_NONE; 1197109323Ssam imr->ifm_status = 0; 1198109323Ssam return; 1199109323Ssam } 120046492Swpaul 1201109323Ssam imr->ifm_status = IFM_AVALID; 1202109323Ssam imr->ifm_active = IFM_IEEE80211; 1203109323Ssam if (ic->ic_state == IEEE80211_S_RUN && 1204109323Ssam (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0) 1205109323Ssam imr->ifm_status |= IFM_ACTIVE; 1206109323Ssam len = sizeof(val); 1207109323Ssam if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) != 0) 1208109323Ssam rate = 0; 1209109323Ssam else { 1210109323Ssam /* convert to 802.11 rate */ 1211109323Ssam rate = val * 2; 1212109323Ssam if (sc->sc_firmware_type == WI_LUCENT) { 1213112501Simp if (rate == 4 * 2) 1214109323Ssam rate = 11; /* 5.5Mbps */ 1215112501Simp else if (rate == 5 * 2) 1216112501Simp rate = 22; /* 11Mbps */ 1217109323Ssam } else { 1218109323Ssam if (rate == 4*2) 1219109323Ssam rate = 11; /* 5.5Mbps */ 1220109323Ssam else if (rate == 8*2) 1221109323Ssam rate = 22; /* 11Mbps */ 1222109323Ssam } 1223109323Ssam } 1224116951Ssam imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B); 1225109323Ssam switch (ic->ic_opmode) { 1226109323Ssam case IEEE80211_M_STA: 1227109323Ssam break; 1228109323Ssam case IEEE80211_M_IBSS: 1229109323Ssam imr->ifm_active |= IFM_IEEE80211_ADHOC; 1230109323Ssam break; 1231109323Ssam case IEEE80211_M_AHDEMO: 1232109323Ssam imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0; 1233109323Ssam break; 1234109323Ssam case IEEE80211_M_HOSTAP: 1235109323Ssam imr->ifm_active |= IFM_IEEE80211_HOSTAP; 1236109323Ssam break; 1237117855Ssam case IEEE80211_M_MONITOR: 1238117855Ssam imr->ifm_active |= IFM_IEEE80211_MONITOR; 1239117855Ssam break; 1240109323Ssam } 1241109323Ssam} 124246492Swpaul 1243109323Ssamstatic void 1244109323Ssamwi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN]) 1245109323Ssam{ 1246109323Ssam struct ieee80211com *ic = &sc->sc_ic; 1247116951Ssam struct ieee80211_node *ni = ic->ic_bss; 1248109323Ssam struct ifnet *ifp = &ic->ic_if; 124998440Simp 1250109323Ssam if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid)) 1251109323Ssam return; 125246492Swpaul 1253109323Ssam DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid))); 1254109323Ssam DPRINTF(("%s ?\n", ether_sprintf(new_bssid))); 125546492Swpaul 1256109323Ssam /* In promiscuous mode, the BSSID field is not a reliable 1257109323Ssam * indicator of the firmware's BSSID. Damp spurious 1258109323Ssam * change-of-BSSID indications. 1259109323Ssam */ 1260109323Ssam if ((ifp->if_flags & IFF_PROMISC) != 0 && 1261109323Ssam sc->sc_false_syns >= WI_MAX_FALSE_SYNS) 1262109323Ssam return; 126346492Swpaul 1264117812Ssam ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1265109323Ssam} 126646492Swpaul 1267109323Ssamstatic void 1268116898Ssamwi_rx_monitor(struct wi_softc *sc, int fid) 1269116898Ssam{ 1270116898Ssam struct ieee80211com *ic = &sc->sc_ic; 1271116898Ssam struct ifnet *ifp = &ic->ic_if; 1272116898Ssam struct wi_frame *rx_frame; 1273116898Ssam struct mbuf *m; 1274116898Ssam int datlen, hdrlen; 1275116898Ssam 1276116898Ssam /* first allocate mbuf for packet storage */ 1277116898Ssam m = m_getcl(M_DONTWAIT, MT_DATA, 0); 1278116898Ssam if (m == NULL) { 1279116898Ssam ifp->if_ierrors++; 1280116898Ssam return; 1281116898Ssam } 1282116898Ssam 1283116898Ssam m->m_pkthdr.rcvif = ifp; 1284116898Ssam 1285116898Ssam /* now read wi_frame first so we know how much data to read */ 1286116898Ssam if (wi_read_bap(sc, fid, 0, mtod(m, caddr_t), sizeof(*rx_frame))) { 1287116898Ssam ifp->if_ierrors++; 1288116898Ssam goto done; 1289116898Ssam } 1290116898Ssam 1291116898Ssam rx_frame = mtod(m, struct wi_frame *); 1292116898Ssam 1293116898Ssam switch ((rx_frame->wi_status & WI_STAT_MAC_PORT) >> 8) { 1294116898Ssam case 7: 1295116898Ssam switch (rx_frame->wi_whdr.i_fc[0] & IEEE80211_FC0_TYPE_MASK) { 1296116898Ssam case IEEE80211_FC0_TYPE_DATA: 1297116898Ssam hdrlen = WI_DATA_HDRLEN; 1298116898Ssam datlen = rx_frame->wi_dat_len + WI_FCS_LEN; 1299116898Ssam break; 1300116898Ssam case IEEE80211_FC0_TYPE_MGT: 1301116898Ssam hdrlen = WI_MGMT_HDRLEN; 1302116898Ssam datlen = rx_frame->wi_dat_len + WI_FCS_LEN; 1303116898Ssam break; 1304116898Ssam case IEEE80211_FC0_TYPE_CTL: 1305116898Ssam /* 1306116898Ssam * prism2 cards don't pass control packets 1307116898Ssam * down properly or consistently, so we'll only 1308116898Ssam * pass down the header. 1309116898Ssam */ 1310116898Ssam hdrlen = WI_CTL_HDRLEN; 1311116898Ssam datlen = 0; 1312116898Ssam break; 1313116898Ssam default: 1314116898Ssam if_printf(ifp, "received packet of unknown type " 1315116898Ssam "on port 7\n"); 1316116898Ssam ifp->if_ierrors++; 1317116898Ssam goto done; 1318116898Ssam } 1319116898Ssam break; 1320116898Ssam case 0: 1321116898Ssam hdrlen = WI_DATA_HDRLEN; 1322116898Ssam datlen = rx_frame->wi_dat_len + WI_FCS_LEN; 1323116898Ssam break; 1324116898Ssam default: 1325116898Ssam if_printf(ifp, "received packet on invalid " 1326116898Ssam "port (wi_status=0x%x)\n", rx_frame->wi_status); 1327116898Ssam ifp->if_ierrors++; 1328116898Ssam goto done; 1329116898Ssam } 1330116898Ssam 1331116898Ssam if (hdrlen + datlen + 2 > MCLBYTES) { 1332116898Ssam if_printf(ifp, "oversized packet received " 1333116898Ssam "(wi_dat_len=%d, wi_status=0x%x)\n", 1334116898Ssam datlen, rx_frame->wi_status); 1335116898Ssam ifp->if_ierrors++; 1336116898Ssam goto done; 1337116898Ssam } 1338116898Ssam 1339116898Ssam if (wi_read_bap(sc, fid, hdrlen, mtod(m, caddr_t) + hdrlen, 1340116898Ssam datlen + 2) == 0) { 1341116898Ssam m->m_pkthdr.len = m->m_len = hdrlen + datlen; 1342116898Ssam ifp->if_ipackets++; 1343116898Ssam BPF_MTAP(ifp, m); /* Handle BPF listeners. */ 1344116898Ssam } else 1345116898Ssam ifp->if_ierrors++; 1346116898Ssamdone: 1347116898Ssam m_freem(m); 1348116898Ssam} 1349116898Ssam 1350116898Ssamstatic void 1351109323Ssamwi_rx_intr(struct wi_softc *sc) 1352109323Ssam{ 1353109323Ssam struct ieee80211com *ic = &sc->sc_ic; 1354109323Ssam struct ifnet *ifp = &ic->ic_if; 1355109323Ssam struct wi_frame frmhdr; 1356109323Ssam struct mbuf *m; 1357109323Ssam struct ieee80211_frame *wh; 1358109323Ssam int fid, len, off, rssi; 1359109323Ssam u_int8_t dir; 1360109323Ssam u_int16_t status; 1361109323Ssam u_int32_t rstamp; 136246611Swpaul 1363109323Ssam fid = CSR_READ_2(sc, WI_RX_FID); 136446611Swpaul 1365116898Ssam if (sc->wi_debug.wi_monitor) { 1366116898Ssam /* 1367116898Ssam * If we are in monitor mode just 1368116898Ssam * read the data from the device. 1369116898Ssam */ 1370116898Ssam wi_rx_monitor(sc, fid); 1371116898Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1372116898Ssam return; 1373116898Ssam } 1374116898Ssam 1375109323Ssam /* First read in the frame header */ 1376109323Ssam if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) { 1377109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1378109323Ssam ifp->if_ierrors++; 1379109323Ssam DPRINTF(("wi_rx_intr: read fid %x failed\n", fid)); 1380109323Ssam return; 1381109323Ssam } 138291695Simp 1383109323Ssam if (IFF_DUMPPKTS(ifp)) 1384109323Ssam wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal); 138546492Swpaul 1386109323Ssam /* 1387109323Ssam * Drop undecryptable or packets with receive errors here 1388109323Ssam */ 1389109323Ssam status = le16toh(frmhdr.wi_status); 1390109323Ssam if (status & WI_STAT_ERRSTAT) { 1391109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1392109323Ssam ifp->if_ierrors++; 1393109323Ssam DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status)); 1394109323Ssam return; 1395109323Ssam } 1396109323Ssam rssi = frmhdr.wi_rx_signal; 1397109323Ssam rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) | 1398109323Ssam le16toh(frmhdr.wi_rx_tstamp1); 139946492Swpaul 1400109323Ssam len = le16toh(frmhdr.wi_dat_len); 1401109323Ssam off = ALIGN(sizeof(struct ieee80211_frame)); 140246563Swpaul 1403117855Ssam /* 1404117855Ssam * Sometimes the PRISM2.x returns bogusly large frames. Except 1405117855Ssam * in monitor mode, just throw them away. 1406117855Ssam */ 1407117855Ssam if (off + len > MCLBYTES) { 1408117855Ssam if (ic->ic_opmode != IEEE80211_M_MONITOR) { 1409117855Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1410117855Ssam ifp->if_ierrors++; 1411117855Ssam DPRINTF(("wi_rx_intr: oversized packet\n")); 1412117855Ssam return; 1413117855Ssam } else 1414117855Ssam len = 0; 1415117855Ssam } 1416117855Ssam 1417111119Simp MGETHDR(m, M_DONTWAIT, MT_DATA); 1418109323Ssam if (m == NULL) { 1419109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1420109323Ssam ifp->if_ierrors++; 1421109323Ssam DPRINTF(("wi_rx_intr: MGET failed\n")); 1422109323Ssam return; 1423109323Ssam } 1424109323Ssam if (off + len > MHLEN) { 1425111119Simp MCLGET(m, M_DONTWAIT); 1426109323Ssam if ((m->m_flags & M_EXT) == 0) { 1427109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1428109323Ssam m_freem(m); 1429109323Ssam ifp->if_ierrors++; 1430109323Ssam DPRINTF(("wi_rx_intr: MCLGET failed\n")); 1431109323Ssam return; 1432109323Ssam } 1433109323Ssam } 143446492Swpaul 1435109323Ssam m->m_data += off - sizeof(struct ieee80211_frame); 1436109323Ssam memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame)); 1437109323Ssam wi_read_bap(sc, fid, sizeof(frmhdr), 1438109323Ssam m->m_data + sizeof(struct ieee80211_frame), len); 1439109323Ssam m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len; 1440109323Ssam m->m_pkthdr.rcvif = ifp; 144194405Simp 1442109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 144346492Swpaul 1444109323Ssam#if NBPFILTER > 0 1445109323Ssam if (sc->sc_drvbpf) { 1446109323Ssam struct mbuf *mb; 1447100876Simp 1448111119Simp MGETHDR(mb, M_DONTWAIT, m->m_type); 1449109323Ssam if (mb != NULL) { 1450111119Simp (void) m_dup_pkthdr(mb, m, M_DONTWAIT); 1451109323Ssam mb->m_next = m; 1452109323Ssam mb->m_data = (caddr_t)&frmhdr; 1453109323Ssam mb->m_len = sizeof(frmhdr); 1454109323Ssam mb->m_pkthdr.len += mb->m_len; 1455109323Ssam bpf_mtap(sc->sc_drvbpf, mb); 1456109323Ssam m_free(mb); 145791695Simp } 145856965Swpaul } 1459109323Ssam#endif 1460109323Ssam wh = mtod(m, struct ieee80211_frame *); 1461109323Ssam if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1462109323Ssam /* 1463109323Ssam * WEP is decrypted by hardware. Clear WEP bit 1464109323Ssam * header for ieee80211_input(). 1465109323Ssam */ 1466109323Ssam wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 1467109323Ssam } 146856965Swpaul 1469109323Ssam /* synchronize driver's BSSID with firmware's BSSID */ 1470109323Ssam dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 1471109323Ssam if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS) 1472109323Ssam wi_sync_bssid(sc, wh->i_addr3); 147346492Swpaul 1474116951Ssam ieee80211_input(ifp, m, rssi, rstamp, 0); 1475109323Ssam} 147646492Swpaul 1477109323Ssamstatic void 1478109323Ssamwi_tx_ex_intr(struct wi_softc *sc) 1479109323Ssam{ 1480109323Ssam struct ieee80211com *ic = &sc->sc_ic; 1481109323Ssam struct ifnet *ifp = &ic->ic_if; 1482109323Ssam struct wi_frame frmhdr; 1483109323Ssam int fid; 148446492Swpaul 1485109323Ssam fid = CSR_READ_2(sc, WI_TX_CMP_FID); 1486109323Ssam /* Read in the frame header */ 1487109323Ssam if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) { 1488109323Ssam u_int16_t status = le16toh(frmhdr.wi_status); 148946492Swpaul 1490109323Ssam /* 1491109323Ssam * Spontaneous station disconnects appear as xmit 1492109323Ssam * errors. Don't announce them and/or count them 1493109323Ssam * as an output error. 1494109323Ssam */ 1495109323Ssam if ((status & WI_TXSTAT_DISCONNECT) == 0) { 1496109323Ssam if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) { 1497109323Ssam if_printf(ifp, "tx failed"); 1498109323Ssam if (status & WI_TXSTAT_RET_ERR) 1499109323Ssam printf(", retry limit exceeded"); 1500109323Ssam if (status & WI_TXSTAT_AGED_ERR) 1501109323Ssam printf(", max transmit lifetime exceeded"); 1502109323Ssam if (status & WI_TXSTAT_DISCONNECT) 1503109323Ssam printf(", port disconnected"); 1504109323Ssam if (status & WI_TXSTAT_FORM_ERR) 1505109323Ssam printf(", invalid format (data len %u src %6D)", 1506109323Ssam le16toh(frmhdr.wi_dat_len), 1507109323Ssam frmhdr.wi_ehdr.ether_shost, ":"); 1508109323Ssam if (status & ~0xf) 1509109323Ssam printf(", status=0x%x", status); 1510109323Ssam printf("\n"); 1511109323Ssam } 1512109323Ssam ifp->if_oerrors++; 1513109323Ssam } else { 1514109323Ssam DPRINTF(("port disconnected\n")); 1515109323Ssam ifp->if_collisions++; /* XXX */ 1516109323Ssam } 1517109323Ssam } else 1518109323Ssam DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid)); 1519109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC); 1520109323Ssam} 152146492Swpaul 1522109323Ssamstatic void 1523109323Ssamwi_tx_intr(struct wi_softc *sc) 1524109323Ssam{ 1525109323Ssam struct ieee80211com *ic = &sc->sc_ic; 1526109323Ssam struct ifnet *ifp = &ic->ic_if; 1527109323Ssam int fid, cur; 152894405Simp 1529109323Ssam fid = CSR_READ_2(sc, WI_ALLOC_FID); 1530109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC); 153146492Swpaul 1532109323Ssam cur = sc->sc_txcur; 1533109323Ssam if (sc->sc_txd[cur].d_fid != fid) { 1534109323Ssam if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n", 1535109323Ssam fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext); 1536109323Ssam return; 1537109323Ssam } 1538109323Ssam sc->sc_tx_timer = 0; 1539109323Ssam sc->sc_txd[cur].d_len = 0; 1540112363Simp sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf; 1541109323Ssam if (sc->sc_txd[cur].d_len == 0) 1542109323Ssam ifp->if_flags &= ~IFF_OACTIVE; 1543109323Ssam else { 1544109323Ssam if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid, 1545109323Ssam 0, 0)) { 1546109323Ssam if_printf(ifp, "xmit failed\n"); 1547109323Ssam sc->sc_txd[cur].d_len = 0; 1548109323Ssam } else { 1549109323Ssam sc->sc_tx_timer = 5; 1550109323Ssam ifp->if_timer = 1; 1551109323Ssam } 1552109323Ssam } 155346492Swpaul} 155446492Swpaul 155588546Salfredstatic void 1556109323Ssamwi_info_intr(struct wi_softc *sc) 155794405Simp{ 1558109323Ssam struct ieee80211com *ic = &sc->sc_ic; 1559109323Ssam struct ifnet *ifp = &ic->ic_if; 1560109323Ssam int i, fid, len, off; 1561109323Ssam u_int16_t ltbuf[2]; 1562109323Ssam u_int16_t stat; 1563109323Ssam u_int32_t *ptr; 156494405Simp 1565109323Ssam fid = CSR_READ_2(sc, WI_INFO_FID); 1566109323Ssam wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf)); 156794405Simp 1568109323Ssam switch (le16toh(ltbuf[1])) { 156994405Simp 1570109323Ssam case WI_INFO_LINK_STAT: 1571109323Ssam wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat)); 1572109323Ssam DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat))); 1573109323Ssam switch (le16toh(stat)) { 1574109323Ssam case WI_INFO_LINK_STAT_CONNECTED: 1575109323Ssam sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 1576109323Ssam if (ic->ic_state == IEEE80211_S_RUN && 1577109323Ssam ic->ic_opmode != IEEE80211_M_IBSS) 1578109323Ssam break; 1579109323Ssam /* FALLTHROUGH */ 1580109323Ssam case WI_INFO_LINK_STAT_AP_CHG: 1581117812Ssam ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1582109323Ssam break; 1583109323Ssam case WI_INFO_LINK_STAT_AP_INR: 1584109323Ssam sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 1585109323Ssam break; 1586109323Ssam case WI_INFO_LINK_STAT_AP_OOR: 1587109323Ssam if (sc->sc_firmware_type == WI_SYMBOL && 1588109323Ssam sc->sc_scan_timer > 0) { 1589109323Ssam if (wi_cmd(sc, WI_CMD_INQUIRE, 1590109323Ssam WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0) 1591109323Ssam sc->sc_scan_timer = 0; 1592109323Ssam break; 1593109323Ssam } 1594109323Ssam if (ic->ic_opmode == IEEE80211_M_STA) 1595109323Ssam sc->sc_flags |= WI_FLAGS_OUTRANGE; 1596109323Ssam break; 1597109323Ssam case WI_INFO_LINK_STAT_DISCONNECTED: 1598109323Ssam case WI_INFO_LINK_STAT_ASSOC_FAILED: 1599109323Ssam if (ic->ic_opmode == IEEE80211_M_STA) 1600117812Ssam ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 1601109323Ssam break; 1602109323Ssam } 1603109323Ssam break; 160494405Simp 1605109323Ssam case WI_INFO_COUNTERS: 1606109323Ssam /* some card versions have a larger stats structure */ 1607109323Ssam len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4); 1608109323Ssam ptr = (u_int32_t *)&sc->sc_stats; 1609109323Ssam off = sizeof(ltbuf); 1610109323Ssam for (i = 0; i < len; i++, off += 2, ptr++) { 1611109323Ssam wi_read_bap(sc, fid, off, &stat, sizeof(stat)); 1612109323Ssam#ifdef WI_HERMES_STATS_WAR 1613109323Ssam if (stat & 0xf000) 1614109323Ssam stat = ~stat; 1615109323Ssam#endif 1616109323Ssam *ptr += stat; 1617109323Ssam } 1618109323Ssam ifp->if_collisions = sc->sc_stats.wi_tx_single_retries + 1619109323Ssam sc->sc_stats.wi_tx_multi_retries + 1620109323Ssam sc->sc_stats.wi_tx_retry_limit; 1621109323Ssam break; 1622109323Ssam 1623109323Ssam case WI_INFO_SCAN_RESULTS: 1624109323Ssam case WI_INFO_HOST_SCAN_RESULTS: 1625109323Ssam wi_scan_result(sc, fid, le16toh(ltbuf[0])); 1626109323Ssam break; 1627109323Ssam 1628109323Ssam default: 1629109323Ssam DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid, 1630109323Ssam le16toh(ltbuf[1]), le16toh(ltbuf[0]))); 1631109323Ssam break; 163294405Simp } 1633109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO); 1634109323Ssam} 163594405Simp 1636109323Ssamstatic int 1637109323Ssamwi_write_multi(struct wi_softc *sc) 1638109323Ssam{ 1639109323Ssam struct ifnet *ifp = &sc->sc_ic.ic_if; 1640109323Ssam int n; 1641109323Ssam struct ifmultiaddr *ifma; 1642109323Ssam struct wi_mcast mlist; 164394472Simp 1644109323Ssam if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 1645109323Ssamallmulti: 1646109323Ssam memset(&mlist, 0, sizeof(mlist)); 1647109323Ssam return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist, 1648109323Ssam sizeof(mlist)); 164994405Simp } 165094405Simp 1651109323Ssam n = 0; 1652109323Ssam#if __FreeBSD_version < 500000 1653109323Ssam LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1654109323Ssam#else 1655109323Ssam TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1656109323Ssam#endif 1657109323Ssam if (ifma->ifma_addr->sa_family != AF_LINK) 1658109323Ssam continue; 1659109323Ssam if (n >= 16) 1660109323Ssam goto allmulti; 1661109323Ssam IEEE80211_ADDR_COPY(&mlist.wi_mcast[n], 1662109323Ssam (LLADDR((struct sockaddr_dl *)ifma->ifma_addr))); 1663109323Ssam n++; 166494405Simp } 1665109323Ssam return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist, 1666109323Ssam IEEE80211_ADDR_LEN * n); 166794405Simp} 166894405Simp 166994405Simpstatic void 1670109323Ssamwi_read_nicid(struct wi_softc *sc) 167146492Swpaul{ 1672109323Ssam struct wi_card_ident *id; 1673109323Ssam char *p; 1674109323Ssam int len; 1675109323Ssam u_int16_t ver[4]; 167646492Swpaul 1677109323Ssam /* getting chip identity */ 1678109323Ssam memset(ver, 0, sizeof(ver)); 1679109323Ssam len = sizeof(ver); 1680109323Ssam wi_read_rid(sc, WI_RID_CARD_ID, ver, &len); 1681109323Ssam device_printf(sc->sc_dev, "using "); 168246492Swpaul 1683109323Ssam sc->sc_firmware_type = WI_NOTYPE; 1684109323Ssam for (id = wi_card_ident; id->card_name != NULL; id++) { 1685109323Ssam if (le16toh(ver[0]) == id->card_id) { 1686109323Ssam printf("%s", id->card_name); 1687109323Ssam sc->sc_firmware_type = id->firm_type; 1688109323Ssam break; 1689109323Ssam } 169067092Swpaul } 1691109323Ssam if (sc->sc_firmware_type == WI_NOTYPE) { 1692109323Ssam if (le16toh(ver[0]) & 0x8000) { 1693109323Ssam printf("Unknown PRISM2 chip"); 1694109323Ssam sc->sc_firmware_type = WI_INTERSIL; 1695109323Ssam } else { 1696109323Ssam printf("Unknown Lucent chip"); 1697109323Ssam sc->sc_firmware_type = WI_LUCENT; 1698109323Ssam } 169967092Swpaul } 170046492Swpaul 1701109323Ssam /* get primary firmware version (Only Prism chips) */ 1702109323Ssam if (sc->sc_firmware_type != WI_LUCENT) { 1703109323Ssam memset(ver, 0, sizeof(ver)); 1704109323Ssam len = sizeof(ver); 1705109323Ssam wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len); 1706109323Ssam sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 + 1707109323Ssam le16toh(ver[3]) * 100 + le16toh(ver[1]); 170867092Swpaul } 170946492Swpaul 1710109323Ssam /* get station firmware version */ 1711109323Ssam memset(ver, 0, sizeof(ver)); 1712109323Ssam len = sizeof(ver); 1713109323Ssam wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len); 1714109323Ssam sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 + 1715109323Ssam le16toh(ver[3]) * 100 + le16toh(ver[1]); 1716109323Ssam if (sc->sc_firmware_type == WI_INTERSIL && 1717109323Ssam (sc->sc_sta_firmware_ver == 10102 || 1718109323Ssam sc->sc_sta_firmware_ver == 20102)) { 1719109323Ssam char ident[12]; 1720109323Ssam memset(ident, 0, sizeof(ident)); 1721109323Ssam len = sizeof(ident); 1722109323Ssam /* value should be the format like "V2.00-11" */ 1723109323Ssam if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 && 1724109323Ssam *(p = (char *)ident) >= 'A' && 1725109323Ssam p[2] == '.' && p[5] == '-' && p[8] == '\0') { 1726109323Ssam sc->sc_firmware_type = WI_SYMBOL; 1727109323Ssam sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 + 1728109323Ssam (p[3] - '0') * 1000 + (p[4] - '0') * 100 + 1729109323Ssam (p[6] - '0') * 10 + (p[7] - '0'); 173094405Simp } 173194405Simp } 1732109323Ssam printf("\n"); 1733109323Ssam device_printf(sc->sc_dev, "%s Firmware: ", 1734109323Ssam sc->sc_firmware_type == WI_LUCENT ? "Lucent" : 1735109323Ssam (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil")); 1736109323Ssam if (sc->sc_firmware_type != WI_LUCENT) /* XXX */ 1737109323Ssam printf("Primary (%u.%u.%u), ", 1738109323Ssam sc->sc_pri_firmware_ver / 10000, 1739109323Ssam (sc->sc_pri_firmware_ver % 10000) / 100, 1740109323Ssam sc->sc_pri_firmware_ver % 100); 1741109323Ssam printf("Station (%u.%u.%u)\n", 1742109323Ssam sc->sc_sta_firmware_ver / 10000, 1743109323Ssam (sc->sc_sta_firmware_ver % 10000) / 100, 1744109323Ssam sc->sc_sta_firmware_ver % 100); 1745109323Ssam} 174646492Swpaul 1747109323Ssamstatic int 1748109323Ssamwi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen) 1749109323Ssam{ 1750109323Ssam struct wi_ssid ssid; 175146492Swpaul 1752109323Ssam if (buflen > IEEE80211_NWID_LEN) 1753109323Ssam return ENOBUFS; 1754109323Ssam memset(&ssid, 0, sizeof(ssid)); 1755109323Ssam ssid.wi_len = htole16(buflen); 1756109323Ssam memcpy(ssid.wi_ssid, buf, buflen); 1757109323Ssam return wi_write_rid(sc, rid, &ssid, sizeof(ssid)); 1758109323Ssam} 175946492Swpaul 1760109323Ssamstatic int 1761109323Ssamwi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data) 1762109323Ssam{ 1763109323Ssam struct wi_softc *sc = ifp->if_softc; 1764109323Ssam struct ieee80211com *ic = &sc->sc_ic; 1765109323Ssam struct ifreq *ifr = (struct ifreq *)data; 1766109323Ssam struct wi_req wreq; 1767116898Ssam struct wi_scan_res *res; 1768116898Ssam int len, n, error, mif, val, off, i; 176946492Swpaul 1770109323Ssam error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 1771109323Ssam if (error) 1772109323Ssam return error; 1773109323Ssam len = (wreq.wi_len - 1) * 2; 1774109323Ssam if (len < sizeof(u_int16_t)) 1775109323Ssam return ENOSPC; 1776109323Ssam if (len > sizeof(wreq.wi_val)) 1777109323Ssam len = sizeof(wreq.wi_val); 177846492Swpaul 1779109323Ssam switch (wreq.wi_type) { 178046492Swpaul 1781109323Ssam case WI_RID_IFACE_STATS: 1782109323Ssam memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats)); 1783109323Ssam if (len < sizeof(sc->sc_stats)) 1784109323Ssam error = ENOSPC; 1785109323Ssam else 1786109323Ssam len = sizeof(sc->sc_stats); 1787109323Ssam break; 178846492Swpaul 1789109323Ssam case WI_RID_ENCRYPTION: 1790109323Ssam case WI_RID_TX_CRYPT_KEY: 1791109323Ssam case WI_RID_DEFLT_CRYPT_KEYS: 1792109323Ssam case WI_RID_TX_RATE: 1793109323Ssam return ieee80211_cfgget(ifp, cmd, data); 179446492Swpaul 1795109323Ssam case WI_RID_MICROWAVE_OVEN: 1796109323Ssam if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) { 1797109323Ssam error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1798109323Ssam &len); 1799109323Ssam break; 1800109323Ssam } 1801109323Ssam wreq.wi_val[0] = htole16(sc->sc_microwave_oven); 1802109323Ssam len = sizeof(u_int16_t); 1803109323Ssam break; 180446492Swpaul 1805109323Ssam case WI_RID_DBM_ADJUST: 1806109323Ssam if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) { 1807109323Ssam error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1808109323Ssam &len); 1809109323Ssam break; 1810109323Ssam } 1811109323Ssam wreq.wi_val[0] = htole16(sc->sc_dbm_adjust); 1812109323Ssam len = sizeof(u_int16_t); 1813109323Ssam break; 181446492Swpaul 1815109323Ssam case WI_RID_ROAMING_MODE: 1816109323Ssam if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) { 1817109323Ssam error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1818109323Ssam &len); 1819109323Ssam break; 1820109323Ssam } 1821109323Ssam wreq.wi_val[0] = htole16(sc->sc_roaming_mode); 1822109323Ssam len = sizeof(u_int16_t); 1823109323Ssam break; 182446492Swpaul 1825109323Ssam case WI_RID_SYSTEM_SCALE: 1826109323Ssam if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) { 1827109323Ssam error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1828109323Ssam &len); 1829109323Ssam break; 1830109323Ssam } 1831109323Ssam wreq.wi_val[0] = htole16(sc->sc_system_scale); 1832109323Ssam len = sizeof(u_int16_t); 1833109323Ssam break; 183446492Swpaul 1835109323Ssam case WI_RID_FRAG_THRESH: 1836109323Ssam if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) { 1837109323Ssam error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1838109323Ssam &len); 1839109323Ssam break; 1840109323Ssam } 1841116951Ssam wreq.wi_val[0] = htole16(ic->ic_fragthreshold); 1842109323Ssam len = sizeof(u_int16_t); 1843109323Ssam break; 184446492Swpaul 1845109323Ssam case WI_RID_READ_APS: 1846109323Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1847109323Ssam return ieee80211_cfgget(ifp, cmd, data); 1848109323Ssam if (sc->sc_scan_timer > 0) { 1849109323Ssam error = EINPROGRESS; 1850109323Ssam break; 1851109323Ssam } 1852109323Ssam n = sc->sc_naps; 1853109323Ssam if (len < sizeof(n)) { 1854109323Ssam error = ENOSPC; 1855109323Ssam break; 1856109323Ssam } 1857109323Ssam if (len < sizeof(n) + sizeof(struct wi_apinfo) * n) 1858109323Ssam n = (len - sizeof(n)) / sizeof(struct wi_apinfo); 1859109323Ssam len = sizeof(n) + sizeof(struct wi_apinfo) * n; 1860109323Ssam memcpy(wreq.wi_val, &n, sizeof(n)); 1861109323Ssam memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps, 1862109323Ssam sizeof(struct wi_apinfo) * n); 1863109323Ssam break; 186446492Swpaul 1865109323Ssam case WI_RID_PRISM2: 1866109323Ssam wreq.wi_val[0] = sc->sc_firmware_type != WI_LUCENT; 1867109323Ssam len = sizeof(u_int16_t); 1868109323Ssam break; 186946492Swpaul 1870109323Ssam case WI_RID_MIF: 1871109323Ssam mif = wreq.wi_val[0]; 1872109323Ssam error = wi_cmd(sc, WI_CMD_READMIF, mif, 0, 0); 1873109323Ssam val = CSR_READ_2(sc, WI_RESP0); 1874109323Ssam wreq.wi_val[0] = val; 1875109323Ssam len = sizeof(u_int16_t); 1876109323Ssam break; 187746492Swpaul 1878109323Ssam case WI_RID_ZERO_CACHE: 1879109323Ssam case WI_RID_PROCFRAME: /* ignore for compatibility */ 1880109323Ssam /* XXX ??? */ 1881109323Ssam break; 188246492Swpaul 1883109323Ssam case WI_RID_READ_CACHE: 1884109323Ssam return ieee80211_cfgget(ifp, cmd, data); 188546492Swpaul 1886116898Ssam case WI_RID_SCAN_RES: /* compatibility interface */ 1887116898Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1888116898Ssam return ieee80211_cfgget(ifp, cmd, data); 1889116898Ssam if (sc->sc_scan_timer > 0) { 1890116898Ssam error = EINPROGRESS; 1891116898Ssam break; 1892116898Ssam } 1893116898Ssam n = sc->sc_naps; 1894116898Ssam off = sc->sc_firmware_type != WI_LUCENT ? 1895116898Ssam sizeof(struct wi_scan_p2_hdr) : 0; 1896116898Ssam if (len < off + sizeof(struct wi_scan_res) * n) 1897116898Ssam n = (len - off) / sizeof(struct wi_scan_res); 1898116898Ssam len = off + sizeof(struct wi_scan_res) * n; 1899116898Ssam if (off != 0) { 1900116898Ssam struct wi_scan_p2_hdr *p2 = (struct wi_scan_p2_hdr *)wreq.wi_val; 1901116898Ssam /* 1902116898Ssam * Prepend Prism-specific header. 1903116898Ssam */ 1904116898Ssam if (len < sizeof(struct wi_scan_p2_hdr)) { 1905116898Ssam error = ENOSPC; 1906116898Ssam break; 1907116898Ssam } 1908116898Ssam p2 = (struct wi_scan_p2_hdr *)wreq.wi_val; 1909116898Ssam p2->wi_rsvd = 0; 1910116898Ssam p2->wi_reason = n; /* XXX */ 1911116898Ssam } 1912116898Ssam for (i = 0; i < n; i++) { 1913116898Ssam const struct wi_apinfo *ap = &sc->sc_aps[i]; 1914116898Ssam 1915116898Ssam res = (struct wi_scan_res *)((char *)wreq.wi_val + off); 1916116898Ssam res->wi_chan = ap->channel; 1917116898Ssam res->wi_noise = ap->noise; 1918116898Ssam res->wi_signal = ap->signal; 1919116898Ssam IEEE80211_ADDR_COPY(res->wi_bssid, ap->bssid); 1920116898Ssam res->wi_interval = ap->interval; 1921116898Ssam res->wi_capinfo = ap->capinfo; 1922116898Ssam res->wi_ssid_len = ap->namelen; 1923116898Ssam memcpy(res->wi_ssid, ap->name, 1924116898Ssam IEEE80211_NWID_LEN); 1925116898Ssam if (sc->sc_firmware_type != WI_LUCENT) { 1926116898Ssam /* XXX not saved from Prism cards */ 1927116898Ssam memset(res->wi_srates, 0, 1928116898Ssam sizeof(res->wi_srates)); 1929116898Ssam res->wi_rate = ap->rate; 1930116898Ssam res->wi_rsvd = 0; 1931116898Ssam off += WI_PRISM2_RES_SIZE; 1932116898Ssam } else 1933116898Ssam off += WI_WAVELAN_RES_SIZE; 1934116898Ssam } 1935116898Ssam break; 1936116898Ssam 1937109323Ssam default: 1938109323Ssam if (sc->sc_enabled) { 1939109323Ssam error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1940109323Ssam &len); 1941109323Ssam break; 1942109323Ssam } 1943109323Ssam switch (wreq.wi_type) { 1944109323Ssam case WI_RID_MAX_DATALEN: 1945109323Ssam wreq.wi_val[0] = htole16(sc->sc_max_datalen); 1946109323Ssam len = sizeof(u_int16_t); 1947109323Ssam break; 1948109323Ssam case WI_RID_RTS_THRESH: 1949116951Ssam wreq.wi_val[0] = htole16(ic->ic_rtsthreshold); 1950109323Ssam len = sizeof(u_int16_t); 1951109323Ssam break; 1952109323Ssam case WI_RID_CNFAUTHMODE: 1953109323Ssam wreq.wi_val[0] = htole16(sc->sc_cnfauthmode); 1954109323Ssam len = sizeof(u_int16_t); 1955109323Ssam break; 1956109323Ssam case WI_RID_NODENAME: 1957109323Ssam if (len < sc->sc_nodelen + sizeof(u_int16_t)) { 1958109323Ssam error = ENOSPC; 1959109323Ssam break; 1960109323Ssam } 1961109323Ssam len = sc->sc_nodelen + sizeof(u_int16_t); 1962109323Ssam wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2); 1963109323Ssam memcpy(&wreq.wi_val[1], sc->sc_nodename, 1964109323Ssam sc->sc_nodelen); 1965109323Ssam break; 1966109323Ssam default: 1967109323Ssam return ieee80211_cfgget(ifp, cmd, data); 1968109323Ssam } 1969109323Ssam break; 197046492Swpaul } 1971109323Ssam if (error) 1972109323Ssam return error; 1973109323Ssam wreq.wi_len = (len + 1) / 2 + 1; 1974109323Ssam return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2); 197546492Swpaul} 197646492Swpaul 1977109323Ssamstatic int 1978109323Ssamwi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data) 197946492Swpaul{ 1980109323Ssam struct wi_softc *sc = ifp->if_softc; 1981109323Ssam struct ieee80211com *ic = &sc->sc_ic; 1982109323Ssam struct ifreq *ifr = (struct ifreq *)data; 1983109323Ssam struct wi_req wreq; 1984109323Ssam struct mbuf *m; 1985109323Ssam int i, len, error, mif, val; 1986116951Ssam struct ieee80211_rateset *rs; 198746492Swpaul 1988109323Ssam error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 1989109323Ssam if (error) 1990109323Ssam return error; 1991116898Ssam len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0; 1992109323Ssam switch (wreq.wi_type) { 1993109323Ssam case WI_RID_DBM_ADJUST: 1994109323Ssam return ENODEV; 199567092Swpaul 1996109323Ssam case WI_RID_NODENAME: 1997109323Ssam if (le16toh(wreq.wi_val[0]) * 2 > len || 1998109323Ssam le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) { 1999109323Ssam error = ENOSPC; 2000109323Ssam break; 2001109323Ssam } 2002109323Ssam if (sc->sc_enabled) { 2003109323Ssam error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val, 2004109323Ssam len); 2005109323Ssam if (error) 2006109323Ssam break; 2007109323Ssam } 2008109323Ssam sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2; 2009109323Ssam memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen); 2010109323Ssam break; 201146492Swpaul 2012109323Ssam case WI_RID_MICROWAVE_OVEN: 2013109323Ssam case WI_RID_ROAMING_MODE: 2014109323Ssam case WI_RID_SYSTEM_SCALE: 2015109323Ssam case WI_RID_FRAG_THRESH: 2016109323Ssam if (wreq.wi_type == WI_RID_MICROWAVE_OVEN && 2017109323Ssam (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0) 2018109323Ssam break; 2019109323Ssam if (wreq.wi_type == WI_RID_ROAMING_MODE && 2020109323Ssam (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0) 2021109323Ssam break; 2022109323Ssam if (wreq.wi_type == WI_RID_SYSTEM_SCALE && 2023109323Ssam (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0) 2024109323Ssam break; 2025109323Ssam if (wreq.wi_type == WI_RID_FRAG_THRESH && 2026109323Ssam (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0) 2027109323Ssam break; 2028109323Ssam /* FALLTHROUGH */ 2029109323Ssam case WI_RID_RTS_THRESH: 2030109323Ssam case WI_RID_CNFAUTHMODE: 2031109323Ssam case WI_RID_MAX_DATALEN: 2032109323Ssam if (sc->sc_enabled) { 2033109323Ssam error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val, 2034109323Ssam sizeof(u_int16_t)); 2035109323Ssam if (error) 2036109323Ssam break; 2037109323Ssam } 2038109323Ssam switch (wreq.wi_type) { 2039109323Ssam case WI_RID_FRAG_THRESH: 2040116951Ssam ic->ic_fragthreshold = le16toh(wreq.wi_val[0]); 2041109323Ssam break; 2042109323Ssam case WI_RID_RTS_THRESH: 2043116951Ssam ic->ic_rtsthreshold = le16toh(wreq.wi_val[0]); 2044109323Ssam break; 2045109323Ssam case WI_RID_MICROWAVE_OVEN: 2046109323Ssam sc->sc_microwave_oven = le16toh(wreq.wi_val[0]); 2047109323Ssam break; 2048109323Ssam case WI_RID_ROAMING_MODE: 2049109323Ssam sc->sc_roaming_mode = le16toh(wreq.wi_val[0]); 2050109323Ssam break; 2051109323Ssam case WI_RID_SYSTEM_SCALE: 2052109323Ssam sc->sc_system_scale = le16toh(wreq.wi_val[0]); 2053109323Ssam break; 2054109323Ssam case WI_RID_CNFAUTHMODE: 2055109323Ssam sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]); 2056109323Ssam break; 2057109323Ssam case WI_RID_MAX_DATALEN: 2058109323Ssam sc->sc_max_datalen = le16toh(wreq.wi_val[0]); 2059109323Ssam break; 2060109323Ssam } 2061109323Ssam break; 206294405Simp 2063109323Ssam case WI_RID_TX_RATE: 2064109323Ssam switch (le16toh(wreq.wi_val[0])) { 2065109323Ssam case 3: 2066109323Ssam ic->ic_fixed_rate = -1; 2067109323Ssam break; 2068109323Ssam default: 2069116951Ssam rs = &ic->ic_sup_rates[IEEE80211_MODE_11B]; 2070116951Ssam for (i = 0; i < rs->rs_nrates; i++) { 2071116951Ssam if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) 2072109323Ssam / 2 == le16toh(wreq.wi_val[0])) 2073109323Ssam break; 2074109323Ssam } 2075116951Ssam if (i == rs->rs_nrates) 2076109323Ssam return EINVAL; 2077109323Ssam ic->ic_fixed_rate = i; 2078109323Ssam } 2079109323Ssam if (sc->sc_enabled) 2080109323Ssam error = wi_write_txrate(sc); 2081109323Ssam break; 208246492Swpaul 2083109323Ssam case WI_RID_SCAN_APS: 2084109323Ssam if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP) 2085116898Ssam error = wi_scan_ap(sc, 0x3fff, 0x000f); 2086109323Ssam break; 208746492Swpaul 2088116898Ssam case WI_RID_SCAN_REQ: /* compatibility interface */ 2089116898Ssam if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP) 2090116898Ssam error = wi_scan_ap(sc, wreq.wi_val[0], wreq.wi_val[1]); 2091116898Ssam break; 2092116898Ssam 2093109323Ssam case WI_RID_MGMT_XMIT: 2094109323Ssam if (!sc->sc_enabled) { 2095109323Ssam error = ENETDOWN; 2096109323Ssam break; 2097109323Ssam } 2098109323Ssam if (ic->ic_mgtq.ifq_len > 5) { 2099109323Ssam error = EAGAIN; 2100109323Ssam break; 2101109323Ssam } 2102109323Ssam /* XXX wi_len looks in u_int8_t, not in u_int16_t */ 2103109323Ssam m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL); 2104109323Ssam if (m == NULL) { 2105109323Ssam error = ENOMEM; 2106109323Ssam break; 2107109323Ssam } 2108109323Ssam IF_ENQUEUE(&ic->ic_mgtq, m); 2109109323Ssam break; 211046492Swpaul 2111109323Ssam case WI_RID_MIF: 2112109323Ssam mif = wreq.wi_val[0]; 2113109323Ssam val = wreq.wi_val[1]; 2114109323Ssam error = wi_cmd(sc, WI_CMD_WRITEMIF, mif, val, 0); 2115109323Ssam break; 211646492Swpaul 2117109323Ssam case WI_RID_PROCFRAME: /* ignore for compatibility */ 2118109323Ssam break; 2119109323Ssam 2120116898Ssam case WI_RID_OWN_SSID: 2121116898Ssam if (le16toh(wreq.wi_val[0]) * 2 > len || 2122116898Ssam le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) { 2123116898Ssam error = ENOSPC; 2124116898Ssam break; 2125116898Ssam } 2126116898Ssam memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN); 2127116898Ssam ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2; 2128116898Ssam memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen); 2129116898Ssam error = ENETRESET; 2130116898Ssam break; 2131116898Ssam 2132109323Ssam default: 2133109323Ssam if (sc->sc_enabled) { 2134109323Ssam error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val, 2135109323Ssam len); 2136109323Ssam if (error) 2137109323Ssam break; 2138109323Ssam } 2139109323Ssam error = ieee80211_cfgset(ifp, cmd, data); 2140109323Ssam break; 2141109323Ssam } 2142109323Ssam return error; 214346492Swpaul} 214446492Swpaul 2145109323Ssamstatic int 2146109323Ssamwi_write_txrate(struct wi_softc *sc) 214746492Swpaul{ 2148109323Ssam struct ieee80211com *ic = &sc->sc_ic; 2149109323Ssam int i; 2150109323Ssam u_int16_t rate; 215146492Swpaul 2152109323Ssam if (ic->ic_fixed_rate < 0) 2153109323Ssam rate = 0; /* auto */ 2154109323Ssam else 2155116951Ssam rate = (ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ic->ic_fixed_rate] & 2156109323Ssam IEEE80211_RATE_VAL) / 2; 215746492Swpaul 2158109323Ssam /* rate: 0, 1, 2, 5, 11 */ 215946492Swpaul 2160109323Ssam switch (sc->sc_firmware_type) { 2161109323Ssam case WI_LUCENT: 2162112501Simp switch (rate) { 2163112501Simp case 0: /* auto == 11mbps auto */ 2164112501Simp rate = 3; 2165112501Simp break; 2166112501Simp /* case 1, 2 map to 1, 2*/ 2167112501Simp case 5: /* 5.5Mbps -> 4 */ 2168112501Simp rate = 4; 2169112501Simp break; 2170112501Simp case 11: /* 11mbps -> 5 */ 2171112501Simp rate = 5; 2172112501Simp break; 2173112501Simp default: 2174112501Simp break; 2175112501Simp } 2176109323Ssam break; 2177109323Ssam default: 2178109323Ssam /* Choose a bit according to this table. 2179109323Ssam * 2180109323Ssam * bit | data rate 2181109323Ssam * ----+------------------- 2182109323Ssam * 0 | 1Mbps 2183109323Ssam * 1 | 2Mbps 2184109323Ssam * 2 | 5.5Mbps 2185109323Ssam * 3 | 11Mbps 2186109323Ssam */ 2187109323Ssam for (i = 8; i > 0; i >>= 1) { 2188109323Ssam if (rate >= i) 2189109323Ssam break; 2190109323Ssam } 2191109323Ssam if (i == 0) 2192109323Ssam rate = 0xf; /* auto */ 2193109323Ssam else 2194109323Ssam rate = i; 2195109323Ssam break; 2196109323Ssam } 2197109323Ssam return wi_write_val(sc, WI_RID_TX_RATE, rate); 2198109323Ssam} 219946492Swpaul 2200109323Ssamstatic int 2201109323Ssamwi_write_wep(struct wi_softc *sc) 2202109323Ssam{ 2203109323Ssam struct ieee80211com *ic = &sc->sc_ic; 2204109323Ssam int error = 0; 2205109323Ssam int i, keylen; 2206109323Ssam u_int16_t val; 2207109323Ssam struct wi_key wkey[IEEE80211_WEP_NKID]; 220846492Swpaul 2209109323Ssam switch (sc->sc_firmware_type) { 2210109323Ssam case WI_LUCENT: 2211109323Ssam val = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0; 2212109323Ssam error = wi_write_val(sc, WI_RID_ENCRYPTION, val); 2213109323Ssam if (error) 2214109323Ssam break; 2215109323Ssam error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_wep_txkey); 2216109323Ssam if (error) 2217109323Ssam break; 2218109323Ssam memset(wkey, 0, sizeof(wkey)); 2219109323Ssam for (i = 0; i < IEEE80211_WEP_NKID; i++) { 2220109323Ssam keylen = ic->ic_nw_keys[i].wk_len; 2221109323Ssam wkey[i].wi_keylen = htole16(keylen); 2222109323Ssam memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key, 2223109323Ssam keylen); 2224109323Ssam } 2225109323Ssam error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS, 2226109323Ssam wkey, sizeof(wkey)); 2227109323Ssam break; 2228109323Ssam 2229109323Ssam case WI_INTERSIL: 2230109323Ssam case WI_SYMBOL: 2231109323Ssam if (ic->ic_flags & IEEE80211_F_WEPON) { 2232109323Ssam /* 2233109323Ssam * ONLY HWB3163 EVAL-CARD Firmware version 2234109323Ssam * less than 0.8 variant2 2235109323Ssam * 2236109323Ssam * If promiscuous mode disable, Prism2 chip 2237109323Ssam * does not work with WEP . 2238109323Ssam * It is under investigation for details. 2239109323Ssam * (ichiro@netbsd.org) 2240109323Ssam */ 2241109323Ssam if (sc->sc_firmware_type == WI_INTERSIL && 2242109323Ssam sc->sc_sta_firmware_ver < 802 ) { 2243109323Ssam /* firm ver < 0.8 variant 2 */ 2244109323Ssam wi_write_val(sc, WI_RID_PROMISC, 1); 2245109323Ssam } 2246109323Ssam wi_write_val(sc, WI_RID_CNFAUTHMODE, 2247109323Ssam sc->sc_cnfauthmode); 2248109323Ssam val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED; 2249109323Ssam /* 2250109323Ssam * Encryption firmware has a bug for HostAP mode. 2251109323Ssam */ 2252109323Ssam if (sc->sc_firmware_type == WI_INTERSIL && 2253109323Ssam ic->ic_opmode == IEEE80211_M_HOSTAP) 2254109323Ssam val |= HOST_ENCRYPT; 2255109323Ssam } else { 2256109323Ssam wi_write_val(sc, WI_RID_CNFAUTHMODE, 2257109323Ssam IEEE80211_AUTH_OPEN); 2258109323Ssam val = HOST_ENCRYPT | HOST_DECRYPT; 2259109323Ssam } 2260109323Ssam error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val); 2261109323Ssam if (error) 2262109323Ssam break; 2263109323Ssam error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY, 2264109323Ssam ic->ic_wep_txkey); 2265109323Ssam if (error) 2266109323Ssam break; 2267109323Ssam /* 2268109323Ssam * It seems that the firmware accept 104bit key only if 2269109323Ssam * all the keys have 104bit length. We get the length of 2270109323Ssam * the transmit key and use it for all other keys. 2271109323Ssam * Perhaps we should use software WEP for such situation. 2272109323Ssam */ 2273109323Ssam keylen = ic->ic_nw_keys[ic->ic_wep_txkey].wk_len; 2274109323Ssam if (keylen > IEEE80211_WEP_KEYLEN) 2275109323Ssam keylen = 13; /* 104bit keys */ 2276109323Ssam else 2277109323Ssam keylen = IEEE80211_WEP_KEYLEN; 2278109323Ssam for (i = 0; i < IEEE80211_WEP_NKID; i++) { 2279109323Ssam error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i, 2280109323Ssam ic->ic_nw_keys[i].wk_key, keylen); 2281109323Ssam if (error) 2282109323Ssam break; 2283109323Ssam } 2284109323Ssam break; 2285109323Ssam } 2286109323Ssam return error; 228746492Swpaul} 228846492Swpaul 2289109323Ssamstatic int 2290109323Ssamwi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2) 229146492Swpaul{ 2292109323Ssam int i, s = 0; 2293109323Ssam static volatile int count = 0; 2294109323Ssam 2295109323Ssam if (count > 0) 2296109323Ssam panic("Hey partner, hold on there!"); 2297109323Ssam count++; 229853702Swpaul 2299109323Ssam /* wait for the busy bit to clear */ 2300109323Ssam for (i = 500; i > 0; i--) { /* 5s */ 2301116206Simp if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY)) 2302109323Ssam break; 2303109323Ssam DELAY(10*1000); /* 10 m sec */ 2304109323Ssam } 2305109323Ssam if (i == 0) { 2306109323Ssam device_printf(sc->sc_dev, "wi_cmd: busy bit won't clear.\n" ); 2307109323Ssam count--; 2308109323Ssam return(ETIMEDOUT); 2309109323Ssam } 231090580Sbrooks 2311109323Ssam CSR_WRITE_2(sc, WI_PARAM0, val0); 2312109323Ssam CSR_WRITE_2(sc, WI_PARAM1, val1); 2313109323Ssam CSR_WRITE_2(sc, WI_PARAM2, val2); 2314109323Ssam CSR_WRITE_2(sc, WI_COMMAND, cmd); 231590580Sbrooks 2316109323Ssam if (cmd == WI_CMD_INI) { 2317109323Ssam /* XXX: should sleep here. */ 2318116206Simp DELAY(100*1000); /* 100ms delay for init */ 2319109323Ssam } 2320109323Ssam for (i = 0; i < WI_TIMEOUT; i++) { 2321109323Ssam /* 2322109323Ssam * Wait for 'command complete' bit to be 2323109323Ssam * set in the event status register. 2324109323Ssam */ 2325109323Ssam s = CSR_READ_2(sc, WI_EVENT_STAT); 2326109323Ssam if (s & WI_EV_CMD) { 2327109323Ssam /* Ack the event and read result code. */ 2328109323Ssam s = CSR_READ_2(sc, WI_STATUS); 2329109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD); 2330109323Ssam if (s & WI_STAT_CMD_RESULT) { 2331109323Ssam count--; 2332109323Ssam return(EIO); 2333109323Ssam } 2334109323Ssam break; 233590580Sbrooks } 2336109323Ssam DELAY(WI_DELAY); 2337109323Ssam } 233890580Sbrooks 2339109323Ssam count--; 2340109323Ssam if (i == WI_TIMEOUT) { 2341109323Ssam device_printf(sc->sc_dev, 2342109323Ssam "timeout in wi_cmd 0x%04x; event status 0x%04x\n", cmd, s); 2343109323Ssam return(ETIMEDOUT); 234453702Swpaul } 2345109323Ssam return (0); 2346109323Ssam} 234753702Swpaul 2348109323Ssamstatic int 2349109323Ssamwi_seek_bap(struct wi_softc *sc, int id, int off) 2350109323Ssam{ 2351109323Ssam int i, status; 235290580Sbrooks 2353109323Ssam CSR_WRITE_2(sc, WI_SEL0, id); 2354109323Ssam CSR_WRITE_2(sc, WI_OFF0, off); 235590580Sbrooks 2356109323Ssam for (i = 0; ; i++) { 2357109323Ssam status = CSR_READ_2(sc, WI_OFF0); 2358109323Ssam if ((status & WI_OFF_BUSY) == 0) 2359109323Ssam break; 2360109323Ssam if (i == WI_TIMEOUT) { 2361109323Ssam device_printf(sc->sc_dev, "timeout in wi_seek to %x/%x\n", 2362109323Ssam id, off); 2363109323Ssam sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 2364109323Ssam return ETIMEDOUT; 2365109323Ssam } 2366109323Ssam DELAY(1); 236753702Swpaul } 2368109323Ssam if (status & WI_OFF_ERR) { 2369109323Ssam device_printf(sc->sc_dev, "failed in wi_seek to %x/%x\n", id, off); 2370109323Ssam sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 2371109323Ssam return EIO; 2372109323Ssam } 2373109323Ssam sc->sc_bap_id = id; 2374109323Ssam sc->sc_bap_off = off; 2375109323Ssam return 0; 237653702Swpaul} 237753702Swpaul 2378109323Ssamstatic int 2379109323Ssamwi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen) 238053702Swpaul{ 2381109323Ssam u_int16_t *ptr; 2382109323Ssam int i, error, cnt; 238353702Swpaul 2384109323Ssam if (buflen == 0) 2385109323Ssam return 0; 2386109323Ssam if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 2387109323Ssam if ((error = wi_seek_bap(sc, id, off)) != 0) 2388109323Ssam return error; 238975219Salfred } 2390109323Ssam cnt = (buflen + 1) / 2; 2391109323Ssam ptr = (u_int16_t *)buf; 2392109323Ssam for (i = 0; i < cnt; i++) 2393109323Ssam *ptr++ = CSR_READ_2(sc, WI_DATA0); 2394109323Ssam sc->sc_bap_off += cnt * 2; 2395109323Ssam return 0; 239653702Swpaul} 239753702Swpaul 2398109323Ssamstatic int 2399109323Ssamwi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen) 240053702Swpaul{ 2401109323Ssam u_int16_t *ptr; 2402109323Ssam int i, error, cnt; 240346492Swpaul 2404109323Ssam if (buflen == 0) 2405109323Ssam return 0; 240646492Swpaul 2407109323Ssam#ifdef WI_HERMES_AUTOINC_WAR 2408109323Ssam again: 2409109323Ssam#endif 2410109323Ssam if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 2411109323Ssam if ((error = wi_seek_bap(sc, id, off)) != 0) 2412109323Ssam return error; 2413109323Ssam } 2414109323Ssam cnt = (buflen + 1) / 2; 2415109323Ssam ptr = (u_int16_t *)buf; 2416109323Ssam for (i = 0; i < cnt; i++) 2417109323Ssam CSR_WRITE_2(sc, WI_DATA0, ptr[i]); 2418109323Ssam sc->sc_bap_off += cnt * 2; 2419109323Ssam 2420109323Ssam#ifdef WI_HERMES_AUTOINC_WAR 2421109323Ssam /* 2422109323Ssam * According to the comments in the HCF Light code, there is a bug 2423109323Ssam * in the Hermes (or possibly in certain Hermes firmware revisions) 2424109323Ssam * where the chip's internal autoincrement counter gets thrown off 2425109323Ssam * during data writes: the autoincrement is missed, causing one 2426109323Ssam * data word to be overwritten and subsequent words to be written to 2427109323Ssam * the wrong memory locations. The end result is that we could end 2428109323Ssam * up transmitting bogus frames without realizing it. The workaround 2429109323Ssam * for this is to write a couple of extra guard words after the end 2430109323Ssam * of the transfer, then attempt to read then back. If we fail to 2431109323Ssam * locate the guard words where we expect them, we preform the 2432109323Ssam * transfer over again. 2433109323Ssam */ 2434109323Ssam if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) { 2435109323Ssam CSR_WRITE_2(sc, WI_DATA0, 0x1234); 2436109323Ssam CSR_WRITE_2(sc, WI_DATA0, 0x5678); 2437109323Ssam wi_seek_bap(sc, id, sc->sc_bap_off); 2438109323Ssam sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 2439109323Ssam if (CSR_READ_2(sc, WI_DATA0) != 0x1234 || 2440109323Ssam CSR_READ_2(sc, WI_DATA0) != 0x5678) { 2441109323Ssam device_printf(sc->sc_dev, 2442109323Ssam "detect auto increment bug, try again\n"); 2443109323Ssam goto again; 2444109323Ssam } 2445109323Ssam } 2446109323Ssam#endif 2447109323Ssam return 0; 244846492Swpaul} 244953702Swpaul 2450109323Ssamstatic int 2451109323Ssamwi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen) 2452109323Ssam{ 2453109323Ssam int error, len; 2454109323Ssam struct mbuf *m; 245553702Swpaul 2456109323Ssam for (m = m0; m != NULL && totlen > 0; m = m->m_next) { 2457109323Ssam if (m->m_len == 0) 2458109323Ssam continue; 245953702Swpaul 2460109323Ssam len = min(m->m_len, totlen); 246153702Swpaul 2462109323Ssam if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) { 2463109323Ssam m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf); 2464109323Ssam return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf, 2465109323Ssam totlen); 2466109323Ssam } 246753702Swpaul 2468109323Ssam if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0) 2469109323Ssam return error; 247053702Swpaul 2471109323Ssam off += m->m_len; 2472109323Ssam totlen -= len; 2473109323Ssam } 2474109323Ssam return 0; 2475109323Ssam} 247653702Swpaul 2477109323Ssamstatic int 2478109323Ssamwi_alloc_fid(struct wi_softc *sc, int len, int *idp) 247953702Swpaul{ 248053702Swpaul int i; 248153702Swpaul 2482109323Ssam if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) { 2483109323Ssam device_printf(sc->sc_dev, "failed to allocate %d bytes on NIC\n", 2484109323Ssam len); 2485109323Ssam return ENOMEM; 248653702Swpaul } 248753702Swpaul 2488109323Ssam for (i = 0; i < WI_TIMEOUT; i++) { 2489109323Ssam if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC) 2490109323Ssam break; 2491109323Ssam if (i == WI_TIMEOUT) { 2492109323Ssam device_printf(sc->sc_dev, "timeout in alloc\n"); 2493109323Ssam return ETIMEDOUT; 2494109323Ssam } 2495109323Ssam DELAY(1); 249653702Swpaul } 2497109323Ssam *idp = CSR_READ_2(sc, WI_ALLOC_FID); 2498109323Ssam CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC); 2499109323Ssam return 0; 2500109323Ssam} 250153702Swpaul 2502109323Ssamstatic int 2503109323Ssamwi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp) 2504109323Ssam{ 2505109323Ssam int error, len; 2506109323Ssam u_int16_t ltbuf[2]; 250753702Swpaul 2508109323Ssam /* Tell the NIC to enter record read mode. */ 2509109323Ssam error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0); 2510109323Ssam if (error) 2511109323Ssam return error; 251253702Swpaul 2513109323Ssam error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf)); 2514109323Ssam if (error) 2515109323Ssam return error; 251653702Swpaul 2517109323Ssam if (le16toh(ltbuf[1]) != rid) { 2518109323Ssam device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n", 2519109323Ssam rid, le16toh(ltbuf[1])); 2520109323Ssam return EIO; 252153702Swpaul } 2522109323Ssam len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */ 2523109323Ssam if (*buflenp < len) { 2524109323Ssam device_printf(sc->sc_dev, "record buffer is too small, " 2525109323Ssam "rid=%x, size=%d, len=%d\n", 2526109323Ssam rid, *buflenp, len); 2527109323Ssam return ENOSPC; 252853702Swpaul } 2529109323Ssam *buflenp = len; 2530109323Ssam return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len); 2531109323Ssam} 253253702Swpaul 2533109323Ssamstatic int 2534109323Ssamwi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen) 2535109323Ssam{ 2536109323Ssam int error; 2537109323Ssam u_int16_t ltbuf[2]; 253853702Swpaul 2539109323Ssam ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */ 2540109323Ssam ltbuf[1] = htole16(rid); 254153702Swpaul 2542109323Ssam error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf)); 2543109323Ssam if (error) 2544109323Ssam return error; 2545109323Ssam error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen); 2546109323Ssam if (error) 2547109323Ssam return error; 2548102206Simp 2549109323Ssam return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0); 255053702Swpaul} 255177217Sphk 255288546Salfredstatic int 2553117812Ssamwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 255477217Sphk{ 2555117812Ssam struct ifnet *ifp = &ic->ic_if; 2556117812Ssam struct wi_softc *sc = ifp->if_softc; 2557116951Ssam struct ieee80211_node *ni = ic->ic_bss; 2558116951Ssam int buflen; 2559109323Ssam u_int16_t val; 2560109323Ssam struct wi_ssid ssid; 2561109323Ssam u_int8_t old_bssid[IEEE80211_ADDR_LEN]; 256277217Sphk 2563117812Ssam DPRINTF(("%s: %s -> %s\n", __func__, 2564117812Ssam ieee80211_state_name[ic->ic_state], 2565117812Ssam ieee80211_state_name[nstate])); 2566109323Ssam 2567109323Ssam switch (nstate) { 2568109323Ssam case IEEE80211_S_INIT: 2569109323Ssam ic->ic_flags &= ~IEEE80211_F_SIBSS; 2570109323Ssam sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 2571117812Ssam return (*sc->sc_newstate)(ic, nstate, arg); 2572109323Ssam 2573109323Ssam case IEEE80211_S_RUN: 2574109323Ssam sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 2575109323Ssam buflen = IEEE80211_ADDR_LEN; 2576109323Ssam wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen); 2577109323Ssam IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid); 2578109323Ssam buflen = sizeof(val); 2579109323Ssam wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen); 2580116951Ssam /* XXX validate channel */ 2581116951Ssam ni->ni_chan = &ic->ic_channels[le16toh(val)]; 2582109323Ssam 2583109323Ssam if (IEEE80211_ADDR_EQ(old_bssid, ni->ni_bssid)) 2584109323Ssam sc->sc_false_syns++; 2585109323Ssam else 2586109323Ssam sc->sc_false_syns = 0; 2587109323Ssam 2588109323Ssam if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 2589109323Ssam ni->ni_esslen = ic->ic_des_esslen; 2590109323Ssam memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen); 2591116951Ssam ni->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B]; 2592109323Ssam ni->ni_intval = ic->ic_lintval; 2593109323Ssam ni->ni_capinfo = IEEE80211_CAPINFO_ESS; 2594109323Ssam if (ic->ic_flags & IEEE80211_F_WEPON) 2595109323Ssam ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 259677217Sphk } else { 2597109323Ssam /* XXX check return value */ 2598109323Ssam buflen = sizeof(ssid); 2599109323Ssam wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen); 2600109323Ssam ni->ni_esslen = le16toh(ssid.wi_len); 2601109323Ssam if (ni->ni_esslen > IEEE80211_NWID_LEN) 2602109323Ssam ni->ni_esslen = IEEE80211_NWID_LEN; /*XXX*/ 2603109323Ssam memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen); 260477217Sphk } 260577217Sphk break; 2606109323Ssam 2607109323Ssam case IEEE80211_S_SCAN: 2608109323Ssam case IEEE80211_S_AUTH: 2609109323Ssam case IEEE80211_S_ASSOC: 261077217Sphk break; 261177217Sphk } 261277217Sphk 2613117812Ssam ic->ic_state = nstate; /* NB: skip normal ieee80211 handling */ 2614117812Ssam return 0; 261577217Sphk} 261677217Sphk 261788546Salfredstatic int 2618116898Ssamwi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate) 261977217Sphk{ 2620109323Ssam int error = 0; 2621109323Ssam u_int16_t val[2]; 262277217Sphk 2623109323Ssam if (!sc->sc_enabled) 2624109323Ssam return ENXIO; 2625109323Ssam switch (sc->sc_firmware_type) { 2626109323Ssam case WI_LUCENT: 2627109323Ssam (void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0); 262898440Simp break; 2629109323Ssam case WI_INTERSIL: 2630116898Ssam val[0] = chanmask; /* channel */ 2631116898Ssam val[1] = txrate; /* tx rate */ 2632109323Ssam error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val)); 263398440Simp break; 2634109323Ssam case WI_SYMBOL: 2635109323Ssam /* 2636109323Ssam * XXX only supported on 3.x ? 2637109323Ssam */ 2638109323Ssam val[0] = BSCAN_BCAST | BSCAN_ONETIME; 2639109323Ssam error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ, 2640109323Ssam val, sizeof(val[0])); 264198440Simp break; 264298440Simp } 2643109323Ssam if (error == 0) { 2644109323Ssam sc->sc_scan_timer = WI_SCAN_WAIT; 2645109323Ssam sc->sc_ic.ic_if.if_timer = 1; 2646116898Ssam DPRINTF(("wi_scan_ap: start scanning, " 2647116898Ssam "chamask 0x%x txrate 0x%x\n", chanmask, txrate)); 2648109323Ssam } 2649109323Ssam return error; 2650109323Ssam} 265177217Sphk 2652109323Ssamstatic void 2653109323Ssamwi_scan_result(struct wi_softc *sc, int fid, int cnt) 2654109323Ssam{ 2655109323Ssam#define N(a) (sizeof (a) / sizeof (a[0])) 2656109323Ssam int i, naps, off, szbuf; 2657109323Ssam struct wi_scan_header ws_hdr; /* Prism2 header */ 2658109323Ssam struct wi_scan_data_p2 ws_dat; /* Prism2 scantable*/ 2659109323Ssam struct wi_apinfo *ap; 2660109323Ssam 2661109323Ssam off = sizeof(u_int16_t) * 2; 2662109323Ssam memset(&ws_hdr, 0, sizeof(ws_hdr)); 2663109323Ssam switch (sc->sc_firmware_type) { 2664109323Ssam case WI_INTERSIL: 2665109323Ssam wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr)); 2666109323Ssam off += sizeof(ws_hdr); 2667109323Ssam szbuf = sizeof(struct wi_scan_data_p2); 266877217Sphk break; 2669109323Ssam case WI_SYMBOL: 2670109323Ssam szbuf = sizeof(struct wi_scan_data_p2) + 6; 267177217Sphk break; 2672109323Ssam case WI_LUCENT: 2673109323Ssam szbuf = sizeof(struct wi_scan_data); 267477217Sphk break; 2675109323Ssam default: 2676109323Ssam device_printf(sc->sc_dev, 2677109323Ssam "wi_scan_result: unknown firmware type %u\n", 2678109323Ssam sc->sc_firmware_type); 2679109323Ssam naps = 0; 2680109323Ssam goto done; 268177217Sphk } 2682109323Ssam naps = (cnt * 2 + 2 - off) / szbuf; 2683109323Ssam if (naps > N(sc->sc_aps)) 2684109323Ssam naps = N(sc->sc_aps); 2685109323Ssam sc->sc_naps = naps; 2686109323Ssam /* Read Data */ 2687109323Ssam ap = sc->sc_aps; 2688109323Ssam memset(&ws_dat, 0, sizeof(ws_dat)); 2689109323Ssam for (i = 0; i < naps; i++, ap++) { 2690109323Ssam wi_read_bap(sc, fid, off, &ws_dat, 2691109323Ssam (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf)); 2692109323Ssam DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off, 2693109323Ssam ether_sprintf(ws_dat.wi_bssid))); 2694109323Ssam off += szbuf; 2695109323Ssam ap->scanreason = le16toh(ws_hdr.wi_reason); 2696109323Ssam memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid)); 2697109323Ssam ap->channel = le16toh(ws_dat.wi_chid); 2698109323Ssam ap->signal = le16toh(ws_dat.wi_signal); 2699109323Ssam ap->noise = le16toh(ws_dat.wi_noise); 2700109323Ssam ap->quality = ap->signal - ap->noise; 2701109323Ssam ap->capinfo = le16toh(ws_dat.wi_capinfo); 2702109323Ssam ap->interval = le16toh(ws_dat.wi_interval); 2703109323Ssam ap->rate = le16toh(ws_dat.wi_rate); 2704109323Ssam ap->namelen = le16toh(ws_dat.wi_namelen); 2705109323Ssam if (ap->namelen > sizeof(ap->name)) 2706109323Ssam ap->namelen = sizeof(ap->name); 2707109323Ssam memcpy(ap->name, ws_dat.wi_name, ap->namelen); 2708109323Ssam } 2709109323Ssamdone: 2710109323Ssam /* Done scanning */ 2711109323Ssam sc->sc_scan_timer = 0; 2712109323Ssam DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps)); 2713109323Ssam#undef N 271477217Sphk} 271577217Sphk 271688546Salfredstatic void 2717109323Ssamwi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi) 271877217Sphk{ 2719109323Ssam ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr), 2720116951Ssam ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL : -1, rssi); 2721109323Ssam printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n", 2722109323Ssam le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1), 2723109323Ssam le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence); 2724109323Ssam printf(" rx_signal %u rx_rate %u rx_flow %u\n", 2725109323Ssam wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow); 2726109323Ssam printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n", 2727109323Ssam wh->wi_tx_rtry, wh->wi_tx_rate, 2728109323Ssam le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len)); 2729109323Ssam printf(" ehdr dst %6D src %6D type 0x%x\n", 2730109323Ssam wh->wi_ehdr.ether_dhost, ":", wh->wi_ehdr.ether_shost, ":", 2731109323Ssam wh->wi_ehdr.ether_type); 2732109323Ssam} 273377217Sphk 2734109323Ssamint 2735109323Ssamwi_alloc(device_t dev, int rid) 2736109323Ssam{ 2737109323Ssam struct wi_softc *sc = device_get_softc(dev); 2738109323Ssam 2739109323Ssam if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) { 2740109323Ssam sc->iobase_rid = rid; 2741109323Ssam sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT, 2742109323Ssam &sc->iobase_rid, 0, ~0, (1 << 6), 2743109323Ssam rman_make_alignment_flags(1 << 6) | RF_ACTIVE); 2744109323Ssam if (!sc->iobase) { 2745109323Ssam device_printf(dev, "No I/O space?!\n"); 2746109323Ssam return (ENXIO); 274798440Simp } 2748109323Ssam 2749109323Ssam sc->wi_io_addr = rman_get_start(sc->iobase); 2750109323Ssam sc->wi_btag = rman_get_bustag(sc->iobase); 2751109323Ssam sc->wi_bhandle = rman_get_bushandle(sc->iobase); 2752109323Ssam } else { 2753109323Ssam sc->mem_rid = rid; 2754109323Ssam sc->mem = bus_alloc_resource(dev, SYS_RES_MEMORY, 2755109323Ssam &sc->mem_rid, 0, ~0, 1, RF_ACTIVE); 2756109323Ssam 2757109323Ssam if (!sc->mem) { 2758109323Ssam device_printf(dev, "No Mem space on prism2.5?\n"); 2759109323Ssam return (ENXIO); 276077217Sphk } 2761109323Ssam 2762109323Ssam sc->wi_btag = rman_get_bustag(sc->mem); 2763109323Ssam sc->wi_bhandle = rman_get_bushandle(sc->mem); 276477217Sphk } 276577217Sphk 2766109323Ssam 2767109323Ssam sc->irq_rid = 0; 2768109323Ssam sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid, 2769109323Ssam 0, ~0, 1, RF_ACTIVE | 2770109323Ssam ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE)); 2771109323Ssam 2772109323Ssam if (!sc->irq) { 2773109323Ssam wi_free(dev); 2774109323Ssam device_printf(dev, "No irq?!\n"); 2775109323Ssam return (ENXIO); 277677217Sphk } 2777109323Ssam 2778109323Ssam sc->sc_dev = dev; 2779109323Ssam sc->sc_unit = device_get_unit(dev); 2780109323Ssam 2781109323Ssam return (0); 278277217Sphk} 278393359Simp 2784109323Ssamvoid 2785109323Ssamwi_free(device_t dev) 2786109323Ssam{ 2787109323Ssam struct wi_softc *sc = device_get_softc(dev); 2788109323Ssam 2789109323Ssam if (sc->iobase != NULL) { 2790109323Ssam bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase); 2791109323Ssam sc->iobase = NULL; 2792109323Ssam } 2793109323Ssam if (sc->irq != NULL) { 2794109323Ssam bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq); 2795109323Ssam sc->irq = NULL; 2796109323Ssam } 2797109323Ssam if (sc->mem != NULL) { 2798109323Ssam bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem); 2799109323Ssam sc->mem = NULL; 2800109323Ssam } 2801109323Ssam 2802109323Ssam return; 2803109323Ssam} 2804109323Ssam 280593359Simpstatic int 2806109323Ssamwi_get_debug(struct wi_softc *sc, struct wi_req *wreq) 280793359Simp{ 2808109323Ssam int error = 0; 280993359Simp 281093359Simp wreq->wi_len = 1; 281193359Simp 281293359Simp switch (wreq->wi_type) { 281393359Simp case WI_DEBUG_SLEEP: 281493359Simp wreq->wi_len++; 281593359Simp wreq->wi_val[0] = sc->wi_debug.wi_sleep; 281693359Simp break; 281793359Simp case WI_DEBUG_DELAYSUPP: 281893359Simp wreq->wi_len++; 281993359Simp wreq->wi_val[0] = sc->wi_debug.wi_delaysupp; 282093359Simp break; 282193359Simp case WI_DEBUG_TXSUPP: 282293359Simp wreq->wi_len++; 282393359Simp wreq->wi_val[0] = sc->wi_debug.wi_txsupp; 282493359Simp break; 282593359Simp case WI_DEBUG_MONITOR: 282693359Simp wreq->wi_len++; 282793359Simp wreq->wi_val[0] = sc->wi_debug.wi_monitor; 282893359Simp break; 282993359Simp case WI_DEBUG_LEDTEST: 283093359Simp wreq->wi_len += 3; 283193359Simp wreq->wi_val[0] = sc->wi_debug.wi_ledtest; 283293359Simp wreq->wi_val[1] = sc->wi_debug.wi_ledtest_param0; 283393359Simp wreq->wi_val[2] = sc->wi_debug.wi_ledtest_param1; 283493359Simp break; 283593359Simp case WI_DEBUG_CONTTX: 283693359Simp wreq->wi_len += 2; 283793359Simp wreq->wi_val[0] = sc->wi_debug.wi_conttx; 283893359Simp wreq->wi_val[1] = sc->wi_debug.wi_conttx_param0; 283993359Simp break; 284093359Simp case WI_DEBUG_CONTRX: 284193359Simp wreq->wi_len++; 284293359Simp wreq->wi_val[0] = sc->wi_debug.wi_contrx; 284393359Simp break; 284493359Simp case WI_DEBUG_SIGSTATE: 284593359Simp wreq->wi_len += 2; 284693359Simp wreq->wi_val[0] = sc->wi_debug.wi_sigstate; 284793359Simp wreq->wi_val[1] = sc->wi_debug.wi_sigstate_param0; 284893359Simp break; 284993359Simp case WI_DEBUG_CONFBITS: 285093359Simp wreq->wi_len += 2; 285193359Simp wreq->wi_val[0] = sc->wi_debug.wi_confbits; 285293359Simp wreq->wi_val[1] = sc->wi_debug.wi_confbits_param0; 285393359Simp break; 285493359Simp default: 285593359Simp error = EIO; 285693359Simp break; 285793359Simp } 285893359Simp 285993359Simp return (error); 286093359Simp} 286193359Simp 286293359Simpstatic int 2863109323Ssamwi_set_debug(struct wi_softc *sc, struct wi_req *wreq) 286493359Simp{ 2865109323Ssam int error = 0; 286693359Simp u_int16_t cmd, param0 = 0, param1 = 0; 286793359Simp 286893359Simp switch (wreq->wi_type) { 286993359Simp case WI_DEBUG_RESET: 287093359Simp case WI_DEBUG_INIT: 287193359Simp case WI_DEBUG_CALENABLE: 287293359Simp break; 287393359Simp case WI_DEBUG_SLEEP: 287493359Simp sc->wi_debug.wi_sleep = 1; 287593359Simp break; 287693359Simp case WI_DEBUG_WAKE: 287793359Simp sc->wi_debug.wi_sleep = 0; 287893359Simp break; 287993359Simp case WI_DEBUG_CHAN: 288093359Simp param0 = wreq->wi_val[0]; 288193359Simp break; 288293359Simp case WI_DEBUG_DELAYSUPP: 288393359Simp sc->wi_debug.wi_delaysupp = 1; 288493359Simp break; 288593359Simp case WI_DEBUG_TXSUPP: 288693359Simp sc->wi_debug.wi_txsupp = 1; 288793359Simp break; 288893359Simp case WI_DEBUG_MONITOR: 288993359Simp sc->wi_debug.wi_monitor = 1; 289093359Simp break; 289193359Simp case WI_DEBUG_LEDTEST: 289293359Simp param0 = wreq->wi_val[0]; 289393359Simp param1 = wreq->wi_val[1]; 289493359Simp sc->wi_debug.wi_ledtest = 1; 289593359Simp sc->wi_debug.wi_ledtest_param0 = param0; 289693359Simp sc->wi_debug.wi_ledtest_param1 = param1; 289793359Simp break; 289893359Simp case WI_DEBUG_CONTTX: 289993359Simp param0 = wreq->wi_val[0]; 290093359Simp sc->wi_debug.wi_conttx = 1; 290193359Simp sc->wi_debug.wi_conttx_param0 = param0; 290293359Simp break; 290393359Simp case WI_DEBUG_STOPTEST: 290493359Simp sc->wi_debug.wi_delaysupp = 0; 290593359Simp sc->wi_debug.wi_txsupp = 0; 290693359Simp sc->wi_debug.wi_monitor = 0; 290793359Simp sc->wi_debug.wi_ledtest = 0; 290893359Simp sc->wi_debug.wi_ledtest_param0 = 0; 290993359Simp sc->wi_debug.wi_ledtest_param1 = 0; 291093359Simp sc->wi_debug.wi_conttx = 0; 291193359Simp sc->wi_debug.wi_conttx_param0 = 0; 291293359Simp sc->wi_debug.wi_contrx = 0; 291393359Simp sc->wi_debug.wi_sigstate = 0; 291493359Simp sc->wi_debug.wi_sigstate_param0 = 0; 291593359Simp break; 291693359Simp case WI_DEBUG_CONTRX: 291793359Simp sc->wi_debug.wi_contrx = 1; 291893359Simp break; 291993359Simp case WI_DEBUG_SIGSTATE: 292093359Simp param0 = wreq->wi_val[0]; 292193359Simp sc->wi_debug.wi_sigstate = 1; 292293359Simp sc->wi_debug.wi_sigstate_param0 = param0; 292393359Simp break; 292493359Simp case WI_DEBUG_CONFBITS: 292593359Simp param0 = wreq->wi_val[0]; 292693359Simp param1 = wreq->wi_val[1]; 292793359Simp sc->wi_debug.wi_confbits = param0; 292893359Simp sc->wi_debug.wi_confbits_param0 = param1; 292993359Simp break; 293093359Simp default: 293193359Simp error = EIO; 293293359Simp break; 293393359Simp } 293493359Simp 293593359Simp if (error) 293693359Simp return (error); 293793359Simp 293893359Simp cmd = WI_CMD_DEBUG | (wreq->wi_type << 8); 293993359Simp error = wi_cmd(sc, cmd, param0, param1, 0); 294093359Simp 294193359Simp return (error); 294293359Simp} 2943101903Simp 2944105076Simp#if __FreeBSD_version >= 500000 2945101903Simp/* 2946101903Simp * Special routines to download firmware for Symbol CF card. 2947101903Simp * XXX: This should be modified generic into any PRISM-2 based card. 2948101903Simp */ 2949101903Simp 2950101903Simp#define WI_SBCF_PDIADDR 0x3100 2951101903Simp 2952101903Simp/* unaligned load little endian */ 2953101903Simp#define GETLE32(p) ((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24)) 2954101903Simp#define GETLE16(p) ((p)[0] | ((p)[1]<<8)) 2955101903Simp 2956101903Simpint 2957101903Simpwi_symbol_load_firm(struct wi_softc *sc, const void *primsym, int primlen, 2958101903Simp const void *secsym, int seclen) 2959101903Simp{ 2960101903Simp uint8_t ebuf[256]; 2961101903Simp int i; 2962101903Simp 2963101903Simp /* load primary code and run it */ 2964101903Simp wi_symbol_set_hcr(sc, WI_HCR_EEHOLD); 2965101903Simp if (wi_symbol_write_firm(sc, primsym, primlen, NULL, 0)) 2966101903Simp return EIO; 2967101903Simp wi_symbol_set_hcr(sc, WI_HCR_RUN); 2968101903Simp for (i = 0; ; i++) { 2969101903Simp if (i == 10) 2970101903Simp return ETIMEDOUT; 2971101903Simp tsleep(sc, PWAIT, "wiinit", 1); 2972101903Simp if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT) 2973101903Simp break; 2974101903Simp /* write the magic key value to unlock aux port */ 2975101903Simp CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0); 2976101903Simp CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1); 2977101903Simp CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2); 2978101903Simp CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL); 2979101903Simp } 2980101903Simp 2981101903Simp /* issue read EEPROM command: XXX copied from wi_cmd() */ 2982101903Simp CSR_WRITE_2(sc, WI_PARAM0, 0); 2983101903Simp CSR_WRITE_2(sc, WI_PARAM1, 0); 2984101903Simp CSR_WRITE_2(sc, WI_PARAM2, 0); 2985101903Simp CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE); 2986101903Simp for (i = 0; i < WI_TIMEOUT; i++) { 2987101903Simp if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD) 2988101903Simp break; 2989101903Simp DELAY(1); 2990101903Simp } 2991101903Simp CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD); 2992101903Simp 2993101903Simp CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ); 2994101903Simp CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ); 2995101903Simp CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA, 2996101903Simp (uint16_t *)ebuf, sizeof(ebuf) / 2); 2997101903Simp if (GETLE16(ebuf) > sizeof(ebuf)) 2998101903Simp return EIO; 2999101903Simp if (wi_symbol_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf))) 3000101903Simp return EIO; 3001101903Simp return 0; 3002101903Simp} 3003101903Simp 3004101903Simpstatic int 3005101903Simpwi_symbol_write_firm(struct wi_softc *sc, const void *buf, int buflen, 3006101903Simp const void *ebuf, int ebuflen) 3007101903Simp{ 3008101903Simp const uint8_t *p, *ep, *q, *eq; 3009101903Simp char *tp; 3010101903Simp uint32_t addr, id, eid; 3011101903Simp int i, len, elen, nblk, pdrlen; 3012101903Simp 3013101903Simp /* 3014101903Simp * Parse the header of the firmware image. 3015101903Simp */ 3016101903Simp p = buf; 3017101903Simp ep = p + buflen; 3018101903Simp while (p < ep && *p++ != ' '); /* FILE: */ 3019101903Simp while (p < ep && *p++ != ' '); /* filename */ 3020101903Simp while (p < ep && *p++ != ' '); /* type of the firmware */ 3021101903Simp nblk = strtoul(p, &tp, 10); 3022101903Simp p = tp; 3023101903Simp pdrlen = strtoul(p + 1, &tp, 10); 3024101903Simp p = tp; 3025101903Simp while (p < ep && *p++ != 0x1a); /* skip rest of header */ 3026101903Simp 3027101903Simp /* 3028101903Simp * Block records: address[4], length[2], data[length]; 3029101903Simp */ 3030101903Simp for (i = 0; i < nblk; i++) { 3031101903Simp addr = GETLE32(p); p += 4; 3032101903Simp len = GETLE16(p); p += 2; 3033101903Simp CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ); 3034101903Simp CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ); 3035101903Simp CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA, 3036101903Simp (const uint16_t *)p, len / 2); 3037101903Simp p += len; 3038101903Simp } 3039101903Simp 3040101903Simp /* 3041101903Simp * PDR: id[4], address[4], length[4]; 3042101903Simp */ 3043101903Simp for (i = 0; i < pdrlen; ) { 3044101903Simp id = GETLE32(p); p += 4; i += 4; 3045101903Simp addr = GETLE32(p); p += 4; i += 4; 3046101903Simp len = GETLE32(p); p += 4; i += 4; 3047101903Simp /* replace PDR entry with the values from EEPROM, if any */ 3048101903Simp for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) { 3049101903Simp elen = GETLE16(q); q += 2; 3050101903Simp eid = GETLE16(q); q += 2; 3051101903Simp elen--; /* elen includes eid */ 3052101903Simp if (eid == 0) 3053101903Simp break; 3054101903Simp if (eid != id) 3055101903Simp continue; 3056101903Simp CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ); 3057101903Simp CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ); 3058101903Simp CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA, 3059101903Simp (const uint16_t *)q, len / 2); 3060101903Simp break; 3061101903Simp } 3062101903Simp } 3063101903Simp return 0; 3064101903Simp} 3065101903Simp 3066101903Simpstatic int 3067101903Simpwi_symbol_set_hcr(struct wi_softc *sc, int mode) 3068101903Simp{ 3069101903Simp uint16_t hcr; 3070101903Simp 3071101903Simp CSR_WRITE_2(sc, WI_COR, WI_COR_RESET); 3072101903Simp tsleep(sc, PWAIT, "wiinit", 1); 3073101903Simp hcr = CSR_READ_2(sc, WI_HCR); 3074101903Simp hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE); 3075101903Simp CSR_WRITE_2(sc, WI_HCR, hcr); 3076101903Simp tsleep(sc, PWAIT, "wiinit", 1); 3077101903Simp CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE); 3078101903Simp tsleep(sc, PWAIT, "wiinit", 1); 3079101903Simp return 0; 3080101903Simp} 3081105076Simp#endif 3082