1 /* $NetBSD: wi.c,v 1.109 2003/01/09 08:52:19 dyoung Exp $ */ 2 3 /* 4 * Copyright (c) 1997, 1998, 1999 5 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Bill Paul. 18 * 4. Neither the name of the author nor the names of any co-contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 32 * THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 * $FreeBSD: src/sys/dev/wi/if_wi.c,v 1.166 2004/04/01 00:38:45 sam Exp $ 35 * $DragonFly: src/sys/dev/netif/wi/if_wi.c,v 1.18 2005/01/26 00:37:39 joerg Exp $ 36 */ 37 38 /* 39 * Lucent WaveLAN/IEEE 802.11 PCMCIA driver. 40 * 41 * Original FreeBSD driver written by Bill Paul <wpaul@ctr.columbia.edu> 42 * Electrical Engineering Department 43 * Columbia University, New York City 44 */ 45 46 /* 47 * The WaveLAN/IEEE adapter is the second generation of the WaveLAN 48 * from Lucent. Unlike the older cards, the new ones are programmed 49 * entirely via a firmware-driven controller called the Hermes. 50 * Unfortunately, Lucent will not release the Hermes programming manual 51 * without an NDA (if at all). What they do release is an API library 52 * called the HCF (Hardware Control Functions) which is supposed to 53 * do the device-specific operations of a device driver for you. The 54 * publically available version of the HCF library (the 'HCF Light') is 55 * a) extremely gross, b) lacks certain features, particularly support 56 * for 802.11 frames, and c) is contaminated by the GNU Public License. 57 * 58 * This driver does not use the HCF or HCF Light at all. Instead, it 59 * programs the Hermes controller directly, using information gleaned 60 * from the HCF Light code and corresponding documentation. 61 * 62 * This driver supports the ISA, PCMCIA and PCI versions of the Lucent 63 * WaveLan cards (based on the Hermes chipset), as well as the newer 64 * Prism 2 chipsets with firmware from Intersil and Symbol. 65 */ 66 67 #define WI_HERMES_AUTOINC_WAR /* Work around data write autoinc bug. */ 68 #define WI_HERMES_STATS_WAR /* Work around stats counter bug. */ 69 70 #include <sys/param.h> 71 #include <sys/endian.h> 72 #include <sys/systm.h> 73 #include <sys/sockio.h> 74 #include <sys/mbuf.h> 75 #include <sys/proc.h> 76 #include <sys/kernel.h> 77 #include <sys/socket.h> 78 #include <sys/module.h> 79 #include <sys/bus.h> 80 #include <sys/random.h> 81 #include <sys/syslog.h> 82 #include <sys/sysctl.h> 83 84 #include <machine/bus.h> 85 #include <machine/resource.h> 86 #include <machine/clock.h> 87 #include <machine/atomic.h> 88 #include <sys/rman.h> 89 90 #include <net/if.h> 91 #include <net/if_arp.h> 92 #include <net/ethernet.h> 93 #include <net/if_dl.h> 94 #include <net/if_media.h> 95 #include <net/if_types.h> 96 97 #include <netproto/802_11/ieee80211_var.h> 98 #include <netproto/802_11/ieee80211_ioctl.h> 99 #include <netproto/802_11/ieee80211_radiotap.h> 100 #include <netproto/802_11/if_wavelan_ieee.h> 101 102 #include <netinet/in.h> 103 #include <netinet/in_systm.h> 104 #include <netinet/in_var.h> 105 #include <netinet/ip.h> 106 #include <netinet/if_ether.h> 107 108 #include <net/bpf.h> 109 110 #include <dev/netif/wi/if_wireg.h> 111 #include <dev/netif/wi/if_wivar.h> 112 113 static void wi_start(struct ifnet *); 114 static int wi_reset(struct wi_softc *); 115 static void wi_watchdog(struct ifnet *); 116 static int wi_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *); 117 static int wi_media_change(struct ifnet *); 118 static void wi_media_status(struct ifnet *, struct ifmediareq *); 119 120 static void wi_rx_intr(struct wi_softc *); 121 static void wi_tx_intr(struct wi_softc *); 122 static void wi_tx_ex_intr(struct wi_softc *); 123 static void wi_info_intr(struct wi_softc *); 124 125 static int wi_get_cfg(struct ifnet *, u_long, caddr_t, struct ucred *); 126 static int wi_set_cfg(struct ifnet *, u_long, caddr_t); 127 static int wi_write_txrate(struct wi_softc *); 128 static int wi_write_wep(struct wi_softc *); 129 static int wi_write_multi(struct wi_softc *); 130 static int wi_alloc_fid(struct wi_softc *, int, int *); 131 static void wi_read_nicid(struct wi_softc *); 132 static int wi_write_ssid(struct wi_softc *, int, u_int8_t *, int); 133 134 static int wi_cmd(struct wi_softc *, int, int, int, int); 135 static int wi_seek_bap(struct wi_softc *, int, int); 136 static int wi_read_bap(struct wi_softc *, int, int, void *, int); 137 static int wi_write_bap(struct wi_softc *, int, int, void *, int); 138 static int wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int); 139 static int wi_read_rid(struct wi_softc *, int, void *, int *); 140 static int wi_write_rid(struct wi_softc *, int, void *, int); 141 142 static int wi_newstate(struct ieee80211com *, enum ieee80211_state, int); 143 144 static int wi_scan_ap(struct wi_softc *, u_int16_t, u_int16_t); 145 static void wi_scan_result(struct wi_softc *, int, int); 146 147 static void wi_dump_pkt(struct wi_frame *, struct ieee80211_node *, int rssi); 148 149 static int wi_get_debug(struct wi_softc *, struct wi_req *); 150 static int wi_set_debug(struct wi_softc *, struct wi_req *); 151 152 /* support to download firmware for symbol CF card */ 153 static int wi_symbol_write_firm(struct wi_softc *, const void *, int, 154 const void *, int); 155 static int wi_symbol_set_hcr(struct wi_softc *, int); 156 157 static __inline int 158 wi_write_val(struct wi_softc *sc, int rid, u_int16_t val) 159 { 160 161 val = htole16(val); 162 return wi_write_rid(sc, rid, &val, sizeof(val)); 163 } 164 165 SYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters"); 166 167 static struct timeval lasttxerror; /* time of last tx error msg */ 168 static int curtxeps; /* current tx error msgs/sec */ 169 static int wi_txerate = 0; /* tx error rate: max msgs/sec */ 170 SYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate, 171 0, "max tx error msgs/sec; 0 to disable msgs"); 172 173 #define WI_DEBUG 174 #ifdef WI_DEBUG 175 static int wi_debug = 0; 176 SYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug, 177 0, "control debugging printfs"); 178 179 #define DPRINTF(X) if (wi_debug) printf X 180 #define DPRINTF2(X) if (wi_debug > 1) printf X 181 #define IFF_DUMPPKTS(_ifp) \ 182 (((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2)) 183 #else 184 #define DPRINTF(X) 185 #define DPRINTF2(X) 186 #define IFF_DUMPPKTS(_ifp) 0 187 #endif 188 189 #define WI_INTRS (WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO) 190 191 struct wi_card_ident wi_card_ident[] = { 192 /* CARD_ID CARD_NAME FIRM_TYPE */ 193 { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT }, 194 { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT }, 195 { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT }, 196 { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL }, 197 { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL }, 198 { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL }, 199 { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL }, 200 { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL }, 201 { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL }, 202 { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL }, 203 { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL }, 204 { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL }, 205 { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 206 { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 207 { WI_NIC_3842_PCMCIA_ATL_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 208 { WI_NIC_3842_PCMCIA_ATS_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 209 { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 210 { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 211 { WI_NIC_3842_MINI_ATL_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 212 { WI_NIC_3842_MINI_ATS_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 213 { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 214 { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 215 { WI_NIC_3842_PCI_ATS_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 216 { WI_NIC_3842_PCI_ATL_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 217 { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 218 { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 219 { WI_NIC_P3_PCMCIA_ATL_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 220 { WI_NIC_P3_PCMCIA_ATS_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 221 { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 222 { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 223 { WI_NIC_P3_MINI_ATL_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 224 { WI_NIC_P3_MINI_ATS_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 225 { 0, NULL, 0 }, 226 }; 227 228 devclass_t wi_devclass; 229 230 int 231 wi_attach(device_t dev) 232 { 233 struct wi_softc *sc = device_get_softc(dev); 234 struct ieee80211com *ic = &sc->sc_ic; 235 struct ifnet *ifp = &ic->ic_if; 236 int i, nrates, buflen; 237 u_int16_t val; 238 u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE]; 239 struct ieee80211_rateset *rs; 240 static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = { 241 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 242 }; 243 int error; 244 245 /* 246 * NB: no locking is needed here; don't put it here 247 * unless you can prove it! 248 */ 249 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE, 250 wi_intr, sc, &sc->wi_intrhand); 251 252 if (error) { 253 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error); 254 wi_free(dev); 255 return (error); 256 } 257 258 sc->wi_cmd_count = 500; 259 /* Reset the NIC. */ 260 if (wi_reset(sc) != 0) 261 return ENXIO; /* XXX */ 262 263 /* 264 * Read the station address. 265 * And do it twice. I've seen PRISM-based cards that return 266 * an error when trying to read it the first time, which causes 267 * the probe to fail. 268 */ 269 buflen = IEEE80211_ADDR_LEN; 270 error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen); 271 if (error != 0) { 272 buflen = IEEE80211_ADDR_LEN; 273 error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen); 274 } 275 if (error || IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) { 276 if (error != 0) 277 device_printf(dev, "mac read failed %d\n", error); 278 else 279 device_printf(dev, "mac read failed (all zeros)\n"); 280 wi_free(dev); 281 return (error); 282 } 283 284 /* Read NIC identification */ 285 wi_read_nicid(sc); 286 287 ifp->if_softc = sc; 288 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 289 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 290 ifp->if_ioctl = wi_ioctl; 291 ifp->if_start = wi_start; 292 ifp->if_watchdog = wi_watchdog; 293 ifp->if_init = wi_init; 294 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 295 #ifdef DEVICE_POLLING 296 ifp->if_capabilities |= IFCAP_POLLING; 297 #endif 298 ifp->if_capenable = ifp->if_capabilities; 299 300 ic->ic_phytype = IEEE80211_T_DS; 301 ic->ic_opmode = IEEE80211_M_STA; 302 ic->ic_caps = IEEE80211_C_PMGT | IEEE80211_C_AHDEMO; 303 ic->ic_state = IEEE80211_S_INIT; 304 305 /* 306 * Query the card for available channels and setup the 307 * channel table. We assume these are all 11b channels. 308 */ 309 buflen = sizeof(val); 310 if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0) 311 val = htole16(0x1fff); /* assume 1-11 */ 312 KASSERT(val != 0, ("wi_attach: no available channels listed!")); 313 314 val <<= 1; /* shift for base 1 indices */ 315 for (i = 1; i < 16; i++) { 316 if (isset((u_int8_t*)&val, i)) { 317 ic->ic_channels[i].ic_freq = 318 ieee80211_ieee2mhz(i, IEEE80211_CHAN_B); 319 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B; 320 } 321 } 322 323 /* 324 * Read the default channel from the NIC. This may vary 325 * depending on the country where the NIC was purchased, so 326 * we can't hard-code a default and expect it to work for 327 * everyone. 328 * 329 * If no channel is specified, let the 802.11 code select. 330 */ 331 buflen = sizeof(val); 332 if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0) { 333 val = le16toh(val); 334 KASSERT(val < IEEE80211_CHAN_MAX && 335 ic->ic_channels[val].ic_flags != 0, 336 ("wi_attach: invalid own channel %u!", val)); 337 ic->ic_ibss_chan = &ic->ic_channels[val]; 338 } else { 339 device_printf(dev, 340 "WI_RID_OWN_CHNL failed, using first channel!\n"); 341 ic->ic_ibss_chan = &ic->ic_channels[0]; 342 } 343 344 /* 345 * Set flags based on firmware version. 346 */ 347 switch (sc->sc_firmware_type) { 348 case WI_LUCENT: 349 sc->sc_ntxbuf = 1; 350 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE; 351 #ifdef WI_HERMES_AUTOINC_WAR 352 /* XXX: not confirmed, but never seen for recent firmware */ 353 if (sc->sc_sta_firmware_ver < 40000) { 354 sc->sc_flags |= WI_FLAGS_BUG_AUTOINC; 355 } 356 #endif 357 if (sc->sc_sta_firmware_ver >= 60000) 358 sc->sc_flags |= WI_FLAGS_HAS_MOR; 359 if (sc->sc_sta_firmware_ver >= 60006) { 360 ic->ic_caps |= IEEE80211_C_IBSS; 361 ic->ic_caps |= IEEE80211_C_MONITOR; 362 } 363 sc->sc_ibss_port = htole16(1); 364 365 sc->sc_min_rssi = WI_LUCENT_MIN_RSSI; 366 sc->sc_max_rssi = WI_LUCENT_MAX_RSSI; 367 sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET; 368 break; 369 370 case WI_INTERSIL: 371 sc->sc_ntxbuf = WI_NTXBUF; 372 sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR; 373 sc->sc_flags |= WI_FLAGS_HAS_ROAMING; 374 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE; 375 /* 376 * Old firmware are slow, so give peace a chance. 377 */ 378 if (sc->sc_sta_firmware_ver < 10000) 379 sc->wi_cmd_count = 5000; 380 if (sc->sc_sta_firmware_ver > 10101) 381 sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST; 382 if (sc->sc_sta_firmware_ver >= 800) { 383 ic->ic_caps |= IEEE80211_C_IBSS; 384 ic->ic_caps |= IEEE80211_C_MONITOR; 385 } 386 /* 387 * version 0.8.3 and newer are the only ones that are known 388 * to currently work. Earlier versions can be made to work, 389 * at least according to the Linux driver. 390 */ 391 if (sc->sc_sta_firmware_ver >= 803) 392 ic->ic_caps |= IEEE80211_C_HOSTAP; 393 sc->sc_ibss_port = htole16(0); 394 395 sc->sc_min_rssi = WI_PRISM_MIN_RSSI; 396 sc->sc_max_rssi = WI_PRISM_MAX_RSSI; 397 sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET; 398 break; 399 400 case WI_SYMBOL: 401 sc->sc_ntxbuf = 1; 402 sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY; 403 if (sc->sc_sta_firmware_ver >= 25000) 404 ic->ic_caps |= IEEE80211_C_IBSS; 405 sc->sc_ibss_port = htole16(4); 406 407 sc->sc_min_rssi = WI_PRISM_MIN_RSSI; 408 sc->sc_max_rssi = WI_PRISM_MAX_RSSI; 409 sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET; 410 break; 411 } 412 413 /* 414 * Find out if we support WEP on this card. 415 */ 416 buflen = sizeof(val); 417 if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 && 418 val != htole16(0)) 419 ic->ic_caps |= IEEE80211_C_WEP; 420 421 /* Find supported rates. */ 422 buflen = sizeof(ratebuf); 423 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B]; 424 if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) { 425 nrates = le16toh(*(u_int16_t *)ratebuf); 426 if (nrates > IEEE80211_RATE_MAXSIZE) 427 nrates = IEEE80211_RATE_MAXSIZE; 428 rs->rs_nrates = 0; 429 for (i = 0; i < nrates; i++) 430 if (ratebuf[2+i]) 431 rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i]; 432 } else { 433 /* XXX fallback on error? */ 434 rs->rs_nrates = 0; 435 } 436 437 buflen = sizeof(val); 438 if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) && 439 wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) { 440 sc->sc_dbm_offset = le16toh(val); 441 } 442 443 sc->sc_max_datalen = 2304; 444 sc->sc_system_scale = 1; 445 sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN; 446 sc->sc_roaming_mode = 1; 447 448 sc->sc_portnum = WI_DEFAULT_PORT; 449 sc->sc_authtype = WI_DEFAULT_AUTHTYPE; 450 451 bzero(sc->sc_nodename, sizeof(sc->sc_nodename)); 452 sc->sc_nodelen = sizeof(WI_DEFAULT_NODENAME) - 1; 453 bcopy(WI_DEFAULT_NODENAME, sc->sc_nodename, sc->sc_nodelen); 454 455 bzero(sc->sc_net_name, sizeof(sc->sc_net_name)); 456 bcopy(WI_DEFAULT_NETNAME, sc->sc_net_name, 457 sizeof(WI_DEFAULT_NETNAME) - 1); 458 459 /* 460 * Call MI attach routine. 461 */ 462 ieee80211_ifattach(ifp); 463 /* override state transition method */ 464 sc->sc_newstate = ic->ic_newstate; 465 ic->ic_newstate = wi_newstate; 466 ieee80211_media_init(ifp, wi_media_change, wi_media_status); 467 468 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO, 469 sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th), 470 &sc->sc_drvbpf); 471 /* 472 * Initialize constant fields. 473 * XXX make header lengths a multiple of 32-bits so subsequent 474 * headers are properly aligned; this is a kludge to keep 475 * certain applications happy. 476 * 477 * NB: the channel is setup each time we transition to the 478 * RUN state to avoid filling it in for each frame. 479 */ 480 sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t)); 481 sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len); 482 sc->sc_tx_th.wt_ihdr.it_present = htole32(WI_TX_RADIOTAP_PRESENT); 483 484 sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t)); 485 sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len); 486 sc->sc_rx_th.wr_ihdr.it_present = htole32(WI_RX_RADIOTAP_PRESENT); 487 488 return (0); 489 } 490 491 int 492 wi_detach(device_t dev) 493 { 494 struct wi_softc *sc = device_get_softc(dev); 495 struct ifnet *ifp = &sc->sc_ic.ic_if; 496 WI_LOCK_DECL(); 497 498 WI_LOCK(sc); 499 500 /* check if device was removed */ 501 sc->wi_gone |= !bus_child_present(dev); 502 503 wi_stop(ifp, 0); 504 505 ieee80211_ifdetach(ifp); 506 WI_UNLOCK(sc); 507 bus_teardown_intr(dev, sc->irq, sc->wi_intrhand); 508 wi_free(dev); 509 return (0); 510 } 511 512 void 513 wi_shutdown(device_t dev) 514 { 515 struct wi_softc *sc = device_get_softc(dev); 516 517 wi_stop(&sc->sc_if, 1); 518 } 519 520 #ifdef DEVICE_POLLING 521 static void 522 wi_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 523 { 524 struct wi_softc *sc = ifp->if_softc; 525 uint16_t status; 526 527 if ((ifp->if_capenable & IFCAP_POLLING) == 0) { 528 ether_poll_deregister(ifp); 529 cmd = POLL_DEREGISTER; 530 } 531 if (cmd == POLL_DEREGISTER) { 532 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 533 return; 534 } 535 536 status = CSR_READ_2(sc, WI_EVENT_STAT); 537 538 if (status & WI_EV_RX) 539 wi_rx_intr(sc); 540 if (status & WI_EV_ALLOC) 541 wi_tx_intr(sc); 542 if (status & WI_EV_INFO) 543 wi_info_intr(sc); 544 545 if (cmd == POLL_AND_CHECK_STATUS) { 546 if (status & WI_EV_INFO) 547 wi_info_intr(sc); 548 } 549 550 if ((ifp->if_flags & IFF_OACTIVE) == 0 && 551 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 && 552 IF_QLEN(&ifp->if_snd) != NULL) 553 wi_start(ifp); 554 } 555 #endif /* DEVICE_POLLING */ 556 557 void 558 wi_intr(void *arg) 559 { 560 struct wi_softc *sc = arg; 561 struct ifnet *ifp = &sc->sc_ic.ic_if; 562 u_int16_t status; 563 WI_LOCK_DECL(); 564 565 #ifdef DEVICE_POLLING 566 if (ifp->if_flags & IFF_POLLING) 567 return; 568 if ((ifp->if_capenable & IFCAP_POLLING) && 569 (ether_poll_register(wi_poll, ifp))) { 570 CSR_WRITE_2(sc, WI_INT_EN, 0); 571 wi_poll(ifp, 0, 1); 572 return; 573 } 574 #endif DEVICE_POLLING 575 576 if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) { 577 CSR_WRITE_2(sc, WI_INT_EN, 0); 578 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF); 579 return; 580 } 581 582 WI_LOCK(sc); 583 584 /* Disable interrupts. */ 585 CSR_WRITE_2(sc, WI_INT_EN, 0); 586 587 status = CSR_READ_2(sc, WI_EVENT_STAT); 588 if (status & WI_EV_RX) 589 wi_rx_intr(sc); 590 if (status & WI_EV_ALLOC) 591 wi_tx_intr(sc); 592 if (status & WI_EV_TX_EXC) 593 wi_tx_ex_intr(sc); 594 if (status & WI_EV_INFO) 595 wi_info_intr(sc); 596 if ((ifp->if_flags & IFF_OACTIVE) == 0 && 597 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 && 598 IF_QLEN(&ifp->if_snd) != 0) 599 wi_start(ifp); 600 601 /* Re-enable interrupts. */ 602 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 603 604 WI_UNLOCK(sc); 605 606 return; 607 } 608 609 void 610 wi_init(void *arg) 611 { 612 struct wi_softc *sc = arg; 613 struct ifnet *ifp = &sc->sc_if; 614 struct ieee80211com *ic = &sc->sc_ic; 615 struct wi_joinreq join; 616 int i; 617 int error = 0, wasenabled; 618 struct ifaddr *ifa; 619 struct sockaddr_dl *sdl; 620 WI_LOCK_DECL(); 621 622 WI_LOCK(sc); 623 624 if (sc->wi_gone) { 625 WI_UNLOCK(sc); 626 return; 627 } 628 629 if ((wasenabled = sc->sc_enabled)) 630 wi_stop(ifp, 1); 631 wi_reset(sc); 632 633 /* common 802.11 configuration */ 634 ic->ic_flags &= ~IEEE80211_F_IBSSON; 635 sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 636 switch (ic->ic_opmode) { 637 case IEEE80211_M_STA: 638 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS); 639 break; 640 case IEEE80211_M_IBSS: 641 wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port); 642 ic->ic_flags |= IEEE80211_F_IBSSON; 643 break; 644 case IEEE80211_M_AHDEMO: 645 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC); 646 break; 647 case IEEE80211_M_HOSTAP: 648 /* 649 * For PRISM cards, override the empty SSID, because in 650 * HostAP mode the controller will lock up otherwise. 651 */ 652 if (sc->sc_firmware_type == WI_INTERSIL && 653 ic->ic_des_esslen == 0) { 654 ic->ic_des_essid[0] = ' '; 655 ic->ic_des_esslen = 1; 656 } 657 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP); 658 break; 659 case IEEE80211_M_MONITOR: 660 if (sc->sc_firmware_type == WI_LUCENT) 661 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC); 662 wi_cmd(sc, WI_CMD_DEBUG | (WI_TEST_MONITOR << 8), 0, 0, 0); 663 break; 664 } 665 666 /* Intersil interprets this RID as joining ESS even in IBSS mode */ 667 if (sc->sc_firmware_type == WI_LUCENT && 668 (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0) 669 wi_write_val(sc, WI_RID_CREATE_IBSS, 1); 670 else 671 wi_write_val(sc, WI_RID_CREATE_IBSS, 0); 672 wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval); 673 wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid, 674 ic->ic_des_esslen); 675 wi_write_val(sc, WI_RID_OWN_CHNL, 676 ieee80211_chan2ieee(ic, ic->ic_ibss_chan)); 677 wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen); 678 679 ifa = ifaddr_byindex(ifp->if_index); 680 sdl = (struct sockaddr_dl *) ifa->ifa_addr; 681 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(sdl)); 682 wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN); 683 684 wi_write_val(sc, WI_RID_PM_ENABLED, 685 (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0); 686 687 /* not yet common 802.11 configuration */ 688 wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen); 689 wi_write_val(sc, WI_RID_RTS_THRESH, ic->ic_rtsthreshold); 690 if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) 691 wi_write_val(sc, WI_RID_FRAG_THRESH, ic->ic_fragthreshold); 692 693 /* driver specific 802.11 configuration */ 694 if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) 695 wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale); 696 if (sc->sc_flags & WI_FLAGS_HAS_ROAMING) 697 wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode); 698 if (sc->sc_flags & WI_FLAGS_HAS_MOR) 699 wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven); 700 wi_write_txrate(sc); 701 wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen); 702 703 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 704 sc->sc_firmware_type == WI_INTERSIL) { 705 wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval); 706 wi_write_val(sc, WI_RID_BASIC_RATE, 0x03); /* 1, 2 */ 707 wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */ 708 wi_write_val(sc, WI_RID_DTIM_PERIOD, 1); 709 } 710 711 /* 712 * Initialize promisc mode. 713 * Being in the Host-AP mode causes a great 714 * deal of pain if primisc mode is set. 715 * Therefore we avoid confusing the firmware 716 * and always reset promisc mode in Host-AP 717 * mode. Host-AP sees all the packets anyway. 718 */ 719 if (ic->ic_opmode != IEEE80211_M_HOSTAP && 720 (ifp->if_flags & IFF_PROMISC) != 0) { 721 wi_write_val(sc, WI_RID_PROMISC, 1); 722 } else { 723 wi_write_val(sc, WI_RID_PROMISC, 0); 724 } 725 726 /* Configure WEP. */ 727 if (ic->ic_caps & IEEE80211_C_WEP) 728 wi_write_wep(sc); 729 730 /* Set multicast filter. */ 731 wi_write_multi(sc); 732 733 /* Allocate fids for the card */ 734 if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) { 735 sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame); 736 if (sc->sc_firmware_type == WI_SYMBOL) 737 sc->sc_buflen = 1585; /* XXX */ 738 for (i = 0; i < sc->sc_ntxbuf; i++) { 739 error = wi_alloc_fid(sc, sc->sc_buflen, 740 &sc->sc_txd[i].d_fid); 741 if (error) { 742 device_printf(sc->sc_dev, 743 "tx buffer allocation failed (error %u)\n", 744 error); 745 goto out; 746 } 747 sc->sc_txd[i].d_len = 0; 748 } 749 } 750 sc->sc_txcur = sc->sc_txnext = 0; 751 752 /* Enable desired port */ 753 wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0); 754 755 sc->sc_enabled = 1; 756 ifp->if_flags |= IFF_RUNNING; 757 ifp->if_flags &= ~IFF_OACTIVE; 758 if (ic->ic_opmode == IEEE80211_M_AHDEMO || 759 ic->ic_opmode == IEEE80211_M_MONITOR || 760 ic->ic_opmode == IEEE80211_M_HOSTAP) 761 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 762 763 /* Enable interrupts if not polling */ 764 #ifdef DEVICE_POLLING 765 if ((ifp->if_flags & IFF_POLLING) == 0) 766 #endif 767 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 768 769 if (!wasenabled && 770 ic->ic_opmode == IEEE80211_M_HOSTAP && 771 sc->sc_firmware_type == WI_INTERSIL) { 772 /* XXX: some card need to be re-enabled for hostap */ 773 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0); 774 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0); 775 } 776 777 if (ic->ic_opmode == IEEE80211_M_STA && 778 ((ic->ic_flags & IEEE80211_F_DESBSSID) || 779 ic->ic_des_chan != IEEE80211_CHAN_ANYC)) { 780 memset(&join, 0, sizeof(join)); 781 if (ic->ic_flags & IEEE80211_F_DESBSSID) 782 IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid); 783 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC) 784 join.wi_chan = htole16( 785 ieee80211_chan2ieee(ic, ic->ic_des_chan)); 786 /* Lucent firmware does not support the JOIN RID. */ 787 if (sc->sc_firmware_type != WI_LUCENT) 788 wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join)); 789 } 790 791 WI_UNLOCK(sc); 792 return; 793 out: 794 if (error) { 795 if_printf(ifp, "interface not running\n"); 796 wi_stop(ifp, 1); 797 } 798 WI_UNLOCK(sc); 799 DPRINTF(("wi_init: return %d\n", error)); 800 return; 801 } 802 803 void 804 wi_stop(struct ifnet *ifp, int disable) 805 { 806 struct ieee80211com *ic = (struct ieee80211com *) ifp; 807 struct wi_softc *sc = ifp->if_softc; 808 WI_LOCK_DECL(); 809 810 WI_LOCK(sc); 811 812 DELAY(100000); 813 814 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 815 if (sc->sc_enabled && !sc->wi_gone) { 816 CSR_WRITE_2(sc, WI_INT_EN, 0); 817 wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0); 818 if (disable) { 819 #ifdef __NetBSD__ 820 if (sc->sc_disable) 821 (*sc->sc_disable)(sc); 822 #endif 823 sc->sc_enabled = 0; 824 } 825 } else if (sc->wi_gone && disable) /* gone --> not enabled */ 826 sc->sc_enabled = 0; 827 828 sc->sc_tx_timer = 0; 829 sc->sc_scan_timer = 0; 830 sc->sc_syn_timer = 0; 831 sc->sc_false_syns = 0; 832 sc->sc_naps = 0; 833 ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING); 834 #ifdef DEVICE_POLLING 835 ether_poll_deregister(ifp); 836 #endif 837 ifp->if_timer = 0; 838 839 WI_UNLOCK(sc); 840 } 841 842 static void 843 wi_start(struct ifnet *ifp) 844 { 845 struct wi_softc *sc = ifp->if_softc; 846 struct ieee80211com *ic = &sc->sc_ic; 847 struct ieee80211_node *ni; 848 struct ieee80211_frame *wh; 849 struct mbuf *m0; 850 struct wi_frame frmhdr; 851 int cur, fid, off, error; 852 WI_LOCK_DECL(); 853 854 WI_LOCK(sc); 855 856 if (sc->wi_gone) { 857 WI_UNLOCK(sc); 858 return; 859 } 860 if (sc->sc_flags & WI_FLAGS_OUTRANGE) { 861 WI_UNLOCK(sc); 862 return; 863 } 864 865 memset(&frmhdr, 0, sizeof(frmhdr)); 866 cur = sc->sc_txnext; 867 for (;;) { 868 IF_POLL(&ic->ic_mgtq, m0); 869 if (m0 != NULL) { 870 if (sc->sc_txd[cur].d_len != 0) { 871 ifp->if_flags |= IFF_OACTIVE; 872 break; 873 } 874 IF_DEQUEUE(&ic->ic_mgtq, m0); 875 /* 876 * Hack! The referenced node pointer is in the 877 * rcvif field of the packet header. This is 878 * placed there by ieee80211_mgmt_output because 879 * we need to hold the reference with the frame 880 * and there's no other way (other than packet 881 * tags which we consider too expensive to use) 882 * to pass it along. 883 */ 884 ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif; 885 m0->m_pkthdr.rcvif = NULL; 886 887 m_copydata(m0, 4, ETHER_ADDR_LEN * 2, 888 (caddr_t)&frmhdr.wi_ehdr); 889 frmhdr.wi_ehdr.ether_type = 0; 890 wh = mtod(m0, struct ieee80211_frame *); 891 } else { 892 if (ic->ic_state != IEEE80211_S_RUN) 893 break; 894 IFQ_POLL(&ifp->if_snd, m0); 895 if (m0 == NULL) 896 break; 897 if (sc->sc_txd[cur].d_len != 0) { 898 ifp->if_flags |= IFF_OACTIVE; 899 break; 900 } 901 IFQ_DEQUEUE(&ifp->if_snd, m0); 902 ifp->if_opackets++; 903 m_copydata(m0, 0, ETHER_HDR_LEN, 904 (caddr_t)&frmhdr.wi_ehdr); 905 BPF_MTAP(ifp, m0); 906 907 m0 = ieee80211_encap(ifp, m0, &ni); 908 if (m0 == NULL) { 909 ifp->if_oerrors++; 910 continue; 911 } 912 wh = mtod(m0, struct ieee80211_frame *); 913 if (ic->ic_flags & IEEE80211_F_WEPON) 914 wh->i_fc[1] |= IEEE80211_FC1_WEP; 915 916 } 917 918 if (ic->ic_rawbpf != NULL) 919 bpf_mtap(ic->ic_rawbpf, m0); 920 921 frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX); 922 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 923 (wh->i_fc[1] & IEEE80211_FC1_WEP)) { 924 if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) { 925 ifp->if_oerrors++; 926 if (ni && ni != ic->ic_bss) 927 ieee80211_free_node(ic, ni); 928 continue; 929 } 930 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT); 931 } 932 933 if (sc->sc_drvbpf) { 934 sc->sc_tx_th.wt_rate = 935 ni->ni_rates.rs_rates[ni->ni_txrate]; 936 bpf_ptap(sc->sc_drvbpf, m0, &sc->sc_tx_th, 937 sc->sc_tx_th_len); 938 } 939 940 m_copydata(m0, 0, sizeof(struct ieee80211_frame), 941 (caddr_t)&frmhdr.wi_whdr); 942 m_adj(m0, sizeof(struct ieee80211_frame)); 943 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len); 944 if (IFF_DUMPPKTS(ifp)) 945 wi_dump_pkt(&frmhdr, NULL, -1); 946 fid = sc->sc_txd[cur].d_fid; 947 off = sizeof(frmhdr); 948 error = wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0 949 || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0; 950 m_freem(m0); 951 if (ni && ni != ic->ic_bss) 952 ieee80211_free_node(ic, ni); 953 if (error) { 954 ifp->if_oerrors++; 955 continue; 956 } 957 sc->sc_txd[cur].d_len = off; 958 if (sc->sc_txcur == cur) { 959 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) { 960 if_printf(ifp, "xmit failed\n"); 961 sc->sc_txd[cur].d_len = 0; 962 continue; 963 } 964 sc->sc_tx_timer = 5; 965 ifp->if_timer = 1; 966 } 967 sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf; 968 } 969 970 WI_UNLOCK(sc); 971 } 972 973 static int 974 wi_reset(struct wi_softc *sc) 975 { 976 struct ieee80211com *ic = &sc->sc_ic; 977 struct ifnet *ifp = &ic->ic_if; 978 #define WI_INIT_TRIES 3 979 int i; 980 int error = 0; 981 int tries; 982 983 /* Symbol firmware cannot be initialized more than once */ 984 if (sc->sc_firmware_type == WI_SYMBOL && sc->sc_reset) 985 return (0); 986 if (sc->sc_firmware_type == WI_SYMBOL) 987 tries = 1; 988 else 989 tries = WI_INIT_TRIES; 990 991 for (i = 0; i < tries; i++) { 992 if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0) 993 break; 994 DELAY(WI_DELAY * 1000); 995 } 996 sc->sc_reset = 1; 997 998 if (i == tries) { 999 if_printf(ifp, "init failed\n"); 1000 return (error); 1001 } 1002 1003 CSR_WRITE_2(sc, WI_INT_EN, 0); 1004 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF); 1005 1006 /* Calibrate timer. */ 1007 wi_write_val(sc, WI_RID_TICK_TIME, 8); 1008 1009 return (0); 1010 #undef WI_INIT_TRIES 1011 } 1012 1013 static void 1014 wi_watchdog(struct ifnet *ifp) 1015 { 1016 struct wi_softc *sc = ifp->if_softc; 1017 1018 ifp->if_timer = 0; 1019 if (!sc->sc_enabled) 1020 return; 1021 1022 if (sc->sc_tx_timer) { 1023 if (--sc->sc_tx_timer == 0) { 1024 if_printf(ifp, "device timeout\n"); 1025 ifp->if_oerrors++; 1026 wi_init(ifp->if_softc); 1027 return; 1028 } 1029 ifp->if_timer = 1; 1030 } 1031 1032 if (sc->sc_scan_timer) { 1033 if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT && 1034 sc->sc_firmware_type == WI_INTERSIL) { 1035 DPRINTF(("wi_watchdog: inquire scan\n")); 1036 wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0); 1037 } 1038 if (sc->sc_scan_timer) 1039 ifp->if_timer = 1; 1040 } 1041 1042 if (sc->sc_syn_timer) { 1043 if (--sc->sc_syn_timer == 0) { 1044 struct ieee80211com *ic = (struct ieee80211com *) ifp; 1045 DPRINTF2(("wi_watchdog: %d false syns\n", 1046 sc->sc_false_syns)); 1047 sc->sc_false_syns = 0; 1048 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1049 sc->sc_syn_timer = 5; 1050 } 1051 ifp->if_timer = 1; 1052 } 1053 1054 /* TODO: rate control */ 1055 ieee80211_watchdog(ifp); 1056 } 1057 1058 static int 1059 wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr) 1060 { 1061 struct wi_softc *sc = ifp->if_softc; 1062 struct ieee80211com *ic = &sc->sc_ic; 1063 struct ifreq *ifr = (struct ifreq *)data; 1064 struct ieee80211req *ireq; 1065 u_int8_t nodename[IEEE80211_NWID_LEN]; 1066 int error = 0; 1067 struct wi_req wreq; 1068 WI_LOCK_DECL(); 1069 1070 WI_LOCK(sc); 1071 1072 if (sc->wi_gone) { 1073 error = ENODEV; 1074 goto out; 1075 } 1076 1077 switch (cmd) { 1078 case SIOCSIFFLAGS: 1079 /* 1080 * Can't do promisc and hostap at the same time. If all that's 1081 * changing is the promisc flag, try to short-circuit a call to 1082 * wi_init() by just setting PROMISC in the hardware. 1083 */ 1084 if (ifp->if_flags & IFF_UP) { 1085 if (ic->ic_opmode != IEEE80211_M_HOSTAP && 1086 ifp->if_flags & IFF_RUNNING) { 1087 if (ifp->if_flags & IFF_PROMISC && 1088 !(sc->sc_if_flags & IFF_PROMISC)) { 1089 wi_write_val(sc, WI_RID_PROMISC, 1); 1090 } else if (!(ifp->if_flags & IFF_PROMISC) && 1091 sc->sc_if_flags & IFF_PROMISC) { 1092 wi_write_val(sc, WI_RID_PROMISC, 0); 1093 } else { 1094 wi_init(sc); 1095 } 1096 } else { 1097 wi_init(sc); 1098 } 1099 } else { 1100 if (ifp->if_flags & IFF_RUNNING) { 1101 wi_stop(ifp, 1); 1102 } 1103 sc->wi_gone = 0; 1104 } 1105 sc->sc_if_flags = ifp->if_flags; 1106 error = 0; 1107 break; 1108 case SIOCADDMULTI: 1109 case SIOCDELMULTI: 1110 error = wi_write_multi(sc); 1111 break; 1112 case SIOCGIFGENERIC: 1113 error = wi_get_cfg(ifp, cmd, data, cr); 1114 break; 1115 case SIOCSIFGENERIC: 1116 error = suser_cred(cr, NULL_CRED_OKAY); 1117 if (error) 1118 break; 1119 error = wi_set_cfg(ifp, cmd, data); 1120 break; 1121 case SIOCGPRISM2DEBUG: 1122 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 1123 if (error) 1124 break; 1125 if (!(ifp->if_flags & IFF_RUNNING) || 1126 sc->sc_firmware_type == WI_LUCENT) { 1127 error = EIO; 1128 break; 1129 } 1130 error = wi_get_debug(sc, &wreq); 1131 if (error == 0) 1132 error = copyout(&wreq, ifr->ifr_data, sizeof(wreq)); 1133 break; 1134 case SIOCSPRISM2DEBUG: 1135 if ((error = suser_cred(cr, NULL_CRED_OKAY))) 1136 goto out; 1137 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 1138 if (error) 1139 break; 1140 error = wi_set_debug(sc, &wreq); 1141 break; 1142 case SIOCG80211: 1143 ireq = (struct ieee80211req *) data; 1144 switch (ireq->i_type) { 1145 case IEEE80211_IOC_STATIONNAME: 1146 ireq->i_len = sc->sc_nodelen + 1; 1147 error = copyout(sc->sc_nodename, ireq->i_data, 1148 ireq->i_len); 1149 break; 1150 default: 1151 error = ieee80211_ioctl(ifp, cmd, data, cr); 1152 break; 1153 } 1154 break; 1155 case SIOCS80211: 1156 error = suser_cred(cr, NULL_CRED_OKAY); 1157 if (error) 1158 break; 1159 ireq = (struct ieee80211req *) data; 1160 switch (ireq->i_type) { 1161 case IEEE80211_IOC_STATIONNAME: 1162 if (ireq->i_val != 0 || 1163 ireq->i_len > IEEE80211_NWID_LEN) { 1164 error = EINVAL; 1165 break; 1166 } 1167 memset(nodename, 0, IEEE80211_NWID_LEN); 1168 error = copyin(ireq->i_data, nodename, ireq->i_len); 1169 if (error) 1170 break; 1171 if (sc->sc_enabled) { 1172 error = wi_write_ssid(sc, WI_RID_NODENAME, 1173 nodename, ireq->i_len); 1174 if (error) 1175 break; 1176 } 1177 memcpy(sc->sc_nodename, nodename, IEEE80211_NWID_LEN); 1178 sc->sc_nodelen = ireq->i_len; 1179 break; 1180 default: 1181 error = ieee80211_ioctl(ifp, cmd, data, cr); 1182 break; 1183 } 1184 break; 1185 case SIOCSIFCAP: 1186 ifp->if_capenable &= ~(IFCAP_POLLING); 1187 ifp->if_capenable |= ifr->ifr_reqcap & (IFCAP_POLLING); 1188 if (ifp->if_flags & IFF_RUNNING) 1189 wi_init(sc); 1190 break; 1191 default: 1192 error = ieee80211_ioctl(ifp, cmd, data, cr); 1193 break; 1194 } 1195 if (error == ENETRESET) { 1196 if (sc->sc_enabled) 1197 wi_init(sc); /* XXX no error return */ 1198 error = 0; 1199 } 1200 out: 1201 WI_UNLOCK(sc); 1202 1203 return (error); 1204 } 1205 1206 static int 1207 wi_media_change(struct ifnet *ifp) 1208 { 1209 struct wi_softc *sc = ifp->if_softc; 1210 int error; 1211 1212 error = ieee80211_media_change(ifp); 1213 if (error == ENETRESET) { 1214 if (sc->sc_enabled) 1215 wi_init(sc); /* XXX no error return */ 1216 error = 0; 1217 } 1218 return error; 1219 } 1220 1221 static void 1222 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1223 { 1224 struct wi_softc *sc = ifp->if_softc; 1225 struct ieee80211com *ic = &sc->sc_ic; 1226 u_int16_t val; 1227 int rate, len; 1228 1229 if (sc->wi_gone || !sc->sc_enabled) { 1230 imr->ifm_active = IFM_IEEE80211 | IFM_NONE; 1231 imr->ifm_status = 0; 1232 return; 1233 } 1234 1235 imr->ifm_status = IFM_AVALID; 1236 imr->ifm_active = IFM_IEEE80211; 1237 if (ic->ic_state == IEEE80211_S_RUN && 1238 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0) 1239 imr->ifm_status |= IFM_ACTIVE; 1240 len = sizeof(val); 1241 if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) != 0) 1242 rate = 0; 1243 else { 1244 /* convert to 802.11 rate */ 1245 rate = val * 2; 1246 if (sc->sc_firmware_type == WI_LUCENT) { 1247 if (rate == 4 * 2) 1248 rate = 11; /* 5.5Mbps */ 1249 else if (rate == 5 * 2) 1250 rate = 22; /* 11Mbps */ 1251 } else { 1252 if (rate == 4*2) 1253 rate = 11; /* 5.5Mbps */ 1254 else if (rate == 8*2) 1255 rate = 22; /* 11Mbps */ 1256 } 1257 } 1258 imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B); 1259 switch (ic->ic_opmode) { 1260 case IEEE80211_M_STA: 1261 break; 1262 case IEEE80211_M_IBSS: 1263 imr->ifm_active |= IFM_IEEE80211_ADHOC; 1264 break; 1265 case IEEE80211_M_AHDEMO: 1266 imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0; 1267 break; 1268 case IEEE80211_M_HOSTAP: 1269 imr->ifm_active |= IFM_IEEE80211_HOSTAP; 1270 break; 1271 case IEEE80211_M_MONITOR: 1272 imr->ifm_active |= IFM_IEEE80211_MONITOR; 1273 break; 1274 } 1275 } 1276 1277 static void 1278 wi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN]) 1279 { 1280 struct ieee80211com *ic = &sc->sc_ic; 1281 struct ieee80211_node *ni = ic->ic_bss; 1282 struct ifnet *ifp = &ic->ic_if; 1283 1284 if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid)) 1285 return; 1286 1287 DPRINTF(("wi_sync_bssid: bssid %6D -> ", ni->ni_bssid, ":")); 1288 DPRINTF(("%6D ?\n", new_bssid, ":")); 1289 1290 /* In promiscuous mode, the BSSID field is not a reliable 1291 * indicator of the firmware's BSSID. Damp spurious 1292 * change-of-BSSID indications. 1293 */ 1294 if ((ifp->if_flags & IFF_PROMISC) != 0 && 1295 sc->sc_false_syns >= WI_MAX_FALSE_SYNS) 1296 return; 1297 1298 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1299 } 1300 1301 static void 1302 wi_rx_monitor(struct wi_softc *sc, int fid) 1303 { 1304 struct ieee80211com *ic = &sc->sc_ic; 1305 struct ifnet *ifp = &ic->ic_if; 1306 struct wi_frame *rx_frame; 1307 struct mbuf *m; 1308 int datlen, hdrlen; 1309 1310 /* first allocate mbuf for packet storage */ 1311 m = m_getcl(MB_DONTWAIT, MT_DATA, 0); 1312 if (m == NULL) { 1313 ifp->if_ierrors++; 1314 return; 1315 } 1316 1317 m->m_pkthdr.rcvif = ifp; 1318 1319 /* now read wi_frame first so we know how much data to read */ 1320 if (wi_read_bap(sc, fid, 0, mtod(m, caddr_t), sizeof(*rx_frame))) { 1321 ifp->if_ierrors++; 1322 goto done; 1323 } 1324 1325 rx_frame = mtod(m, struct wi_frame *); 1326 1327 switch ((rx_frame->wi_status & WI_STAT_MAC_PORT) >> 8) { 1328 case 7: 1329 switch (rx_frame->wi_whdr.i_fc[0] & IEEE80211_FC0_TYPE_MASK) { 1330 case IEEE80211_FC0_TYPE_DATA: 1331 hdrlen = WI_DATA_HDRLEN; 1332 datlen = rx_frame->wi_dat_len + WI_FCS_LEN; 1333 break; 1334 case IEEE80211_FC0_TYPE_MGT: 1335 hdrlen = WI_MGMT_HDRLEN; 1336 datlen = rx_frame->wi_dat_len + WI_FCS_LEN; 1337 break; 1338 case IEEE80211_FC0_TYPE_CTL: 1339 /* 1340 * prism2 cards don't pass control packets 1341 * down properly or consistently, so we'll only 1342 * pass down the header. 1343 */ 1344 hdrlen = WI_CTL_HDRLEN; 1345 datlen = 0; 1346 break; 1347 default: 1348 if_printf(ifp, "received packet of unknown type " 1349 "on port 7\n"); 1350 ifp->if_ierrors++; 1351 goto done; 1352 } 1353 break; 1354 case 0: 1355 hdrlen = WI_DATA_HDRLEN; 1356 datlen = rx_frame->wi_dat_len + WI_FCS_LEN; 1357 break; 1358 default: 1359 if_printf(ifp, "received packet on invalid " 1360 "port (wi_status=0x%x)\n", rx_frame->wi_status); 1361 ifp->if_ierrors++; 1362 goto done; 1363 } 1364 1365 if (hdrlen + datlen + 2 > MCLBYTES) { 1366 if_printf(ifp, "oversized packet received " 1367 "(wi_dat_len=%d, wi_status=0x%x)\n", 1368 datlen, rx_frame->wi_status); 1369 ifp->if_ierrors++; 1370 goto done; 1371 } 1372 1373 if (wi_read_bap(sc, fid, hdrlen, mtod(m, caddr_t) + hdrlen, 1374 datlen + 2) == 0) { 1375 m->m_pkthdr.len = m->m_len = hdrlen + datlen; 1376 ifp->if_ipackets++; 1377 BPF_MTAP(ifp, m); /* Handle BPF listeners. */ 1378 } else 1379 ifp->if_ierrors++; 1380 done: 1381 m_freem(m); 1382 } 1383 1384 static void 1385 wi_rx_intr(struct wi_softc *sc) 1386 { 1387 struct ieee80211com *ic = &sc->sc_ic; 1388 struct ifnet *ifp = &ic->ic_if; 1389 struct wi_frame frmhdr; 1390 struct mbuf *m; 1391 struct ieee80211_frame *wh; 1392 struct ieee80211_node *ni; 1393 int fid, len, off, rssi; 1394 u_int8_t dir; 1395 u_int16_t status; 1396 u_int32_t rstamp; 1397 1398 fid = CSR_READ_2(sc, WI_RX_FID); 1399 1400 if (sc->wi_debug.wi_monitor) { 1401 /* 1402 * If we are in monitor mode just 1403 * read the data from the device. 1404 */ 1405 wi_rx_monitor(sc, fid); 1406 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1407 return; 1408 } 1409 1410 /* First read in the frame header */ 1411 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) { 1412 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1413 ifp->if_ierrors++; 1414 DPRINTF(("wi_rx_intr: read fid %x failed\n", fid)); 1415 return; 1416 } 1417 1418 if (IFF_DUMPPKTS(ifp)) 1419 wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal); 1420 1421 /* 1422 * Drop undecryptable or packets with receive errors here 1423 */ 1424 status = le16toh(frmhdr.wi_status); 1425 if (status & WI_STAT_ERRSTAT) { 1426 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1427 ifp->if_ierrors++; 1428 DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status)); 1429 return; 1430 } 1431 rssi = frmhdr.wi_rx_signal; 1432 rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) | 1433 le16toh(frmhdr.wi_rx_tstamp1); 1434 1435 len = le16toh(frmhdr.wi_dat_len); 1436 off = ALIGN(sizeof(struct ieee80211_frame)); 1437 1438 /* 1439 * Sometimes the PRISM2.x returns bogusly large frames. Except 1440 * in monitor mode, just throw them away. 1441 */ 1442 if (off + len > MCLBYTES) { 1443 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 1444 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1445 ifp->if_ierrors++; 1446 DPRINTF(("wi_rx_intr: oversized packet\n")); 1447 return; 1448 } else 1449 len = 0; 1450 } 1451 1452 MGETHDR(m, MB_DONTWAIT, MT_DATA); 1453 if (m == NULL) { 1454 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1455 ifp->if_ierrors++; 1456 DPRINTF(("wi_rx_intr: MGET failed\n")); 1457 return; 1458 } 1459 if (off + len > MHLEN) { 1460 MCLGET(m, MB_DONTWAIT); 1461 if ((m->m_flags & M_EXT) == 0) { 1462 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1463 m_freem(m); 1464 ifp->if_ierrors++; 1465 DPRINTF(("wi_rx_intr: MCLGET failed\n")); 1466 return; 1467 } 1468 } 1469 1470 m->m_data += off - sizeof(struct ieee80211_frame); 1471 memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame)); 1472 wi_read_bap(sc, fid, sizeof(frmhdr), 1473 m->m_data + sizeof(struct ieee80211_frame), len); 1474 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len; 1475 m->m_pkthdr.rcvif = ifp; 1476 1477 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1478 1479 if (sc->sc_drvbpf) { 1480 /* XXX replace divide by table */ 1481 sc->sc_rx_th.wr_rate = frmhdr.wi_rx_rate / 5; 1482 sc->sc_rx_th.wr_antsignal = frmhdr.wi_rx_signal; 1483 sc->sc_rx_th.wr_antnoise = frmhdr.wi_rx_silence; 1484 sc->sc_rx_th.wr_flags = 0; 1485 if (frmhdr.wi_status & WI_STAT_PCF) 1486 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_CFP; 1487 bpf_ptap(sc->sc_drvbpf, m, &sc->sc_rx_th, sc->sc_rx_th_len); 1488 } 1489 1490 wh = mtod(m, struct ieee80211_frame *); 1491 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1492 /* 1493 * WEP is decrypted by hardware. Clear WEP bit 1494 * header for ieee80211_input(). 1495 */ 1496 wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 1497 } 1498 1499 /* synchronize driver's BSSID with firmware's BSSID */ 1500 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 1501 if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS) 1502 wi_sync_bssid(sc, wh->i_addr3); 1503 1504 /* 1505 * Locate the node for sender, track state, and 1506 * then pass this node (referenced) up to the 802.11 1507 * layer for its use. We are required to pass 1508 * something so we fallback to ic_bss when this frame 1509 * is from an unknown sender. 1510 */ 1511 if (ic->ic_opmode != IEEE80211_M_STA) { 1512 ni = ieee80211_find_node(ic, wh->i_addr2); 1513 if (ni == NULL) 1514 ni = ieee80211_ref_node(ic->ic_bss); 1515 } else 1516 ni = ieee80211_ref_node(ic->ic_bss); 1517 /* 1518 * Send frame up for processing. 1519 */ 1520 ieee80211_input(ifp, m, ni, rssi, rstamp); 1521 /* 1522 * The frame may have caused the node to be marked for 1523 * reclamation (e.g. in response to a DEAUTH message) 1524 * so use free_node here instead of unref_node. 1525 */ 1526 if (ni == ic->ic_bss) 1527 ieee80211_unref_node(&ni); 1528 else 1529 ieee80211_free_node(ic, ni); 1530 } 1531 1532 static void 1533 wi_tx_ex_intr(struct wi_softc *sc) 1534 { 1535 struct ieee80211com *ic = &sc->sc_ic; 1536 struct ifnet *ifp = &ic->ic_if; 1537 struct wi_frame frmhdr; 1538 int fid; 1539 1540 fid = CSR_READ_2(sc, WI_TX_CMP_FID); 1541 /* Read in the frame header */ 1542 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) { 1543 u_int16_t status = le16toh(frmhdr.wi_status); 1544 1545 /* 1546 * Spontaneous station disconnects appear as xmit 1547 * errors. Don't announce them and/or count them 1548 * as an output error. 1549 */ 1550 if ((status & WI_TXSTAT_DISCONNECT) == 0) { 1551 if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) { 1552 if_printf(ifp, "tx failed"); 1553 if (status & WI_TXSTAT_RET_ERR) 1554 printf(", retry limit exceeded"); 1555 if (status & WI_TXSTAT_AGED_ERR) 1556 printf(", max transmit lifetime exceeded"); 1557 if (status & WI_TXSTAT_DISCONNECT) 1558 printf(", port disconnected"); 1559 if (status & WI_TXSTAT_FORM_ERR) 1560 printf(", invalid format (data len %u src %6D)", 1561 le16toh(frmhdr.wi_dat_len), 1562 frmhdr.wi_ehdr.ether_shost, ":"); 1563 if (status & ~0xf) 1564 printf(", status=0x%x", status); 1565 printf("\n"); 1566 } 1567 ifp->if_oerrors++; 1568 } else { 1569 DPRINTF(("port disconnected\n")); 1570 ifp->if_collisions++; /* XXX */ 1571 } 1572 } else 1573 DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid)); 1574 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC); 1575 } 1576 1577 static void 1578 wi_tx_intr(struct wi_softc *sc) 1579 { 1580 struct ieee80211com *ic = &sc->sc_ic; 1581 struct ifnet *ifp = &ic->ic_if; 1582 int fid, cur; 1583 1584 if (sc->wi_gone) 1585 return; 1586 1587 fid = CSR_READ_2(sc, WI_ALLOC_FID); 1588 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC); 1589 1590 cur = sc->sc_txcur; 1591 if (sc->sc_txd[cur].d_fid != fid) { 1592 if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n", 1593 fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext); 1594 return; 1595 } 1596 sc->sc_tx_timer = 0; 1597 sc->sc_txd[cur].d_len = 0; 1598 sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf; 1599 if (sc->sc_txd[cur].d_len == 0) 1600 ifp->if_flags &= ~IFF_OACTIVE; 1601 else { 1602 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid, 1603 0, 0)) { 1604 if_printf(ifp, "xmit failed\n"); 1605 sc->sc_txd[cur].d_len = 0; 1606 } else { 1607 sc->sc_tx_timer = 5; 1608 ifp->if_timer = 1; 1609 } 1610 } 1611 } 1612 1613 static void 1614 wi_info_intr(struct wi_softc *sc) 1615 { 1616 struct ieee80211com *ic = &sc->sc_ic; 1617 struct ifnet *ifp = &ic->ic_if; 1618 int i, fid, len, off; 1619 u_int16_t ltbuf[2]; 1620 u_int16_t stat; 1621 u_int32_t *ptr; 1622 1623 fid = CSR_READ_2(sc, WI_INFO_FID); 1624 wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf)); 1625 1626 switch (le16toh(ltbuf[1])) { 1627 1628 case WI_INFO_LINK_STAT: 1629 wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat)); 1630 DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat))); 1631 switch (le16toh(stat)) { 1632 case WI_INFO_LINK_STAT_CONNECTED: 1633 sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 1634 if (ic->ic_state == IEEE80211_S_RUN && 1635 ic->ic_opmode != IEEE80211_M_IBSS) 1636 break; 1637 /* FALLTHROUGH */ 1638 case WI_INFO_LINK_STAT_AP_CHG: 1639 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1640 break; 1641 case WI_INFO_LINK_STAT_AP_INR: 1642 sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 1643 break; 1644 case WI_INFO_LINK_STAT_AP_OOR: 1645 if (sc->sc_firmware_type == WI_SYMBOL && 1646 sc->sc_scan_timer > 0) { 1647 if (wi_cmd(sc, WI_CMD_INQUIRE, 1648 WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0) 1649 sc->sc_scan_timer = 0; 1650 break; 1651 } 1652 if (ic->ic_opmode == IEEE80211_M_STA) 1653 sc->sc_flags |= WI_FLAGS_OUTRANGE; 1654 break; 1655 case WI_INFO_LINK_STAT_DISCONNECTED: 1656 case WI_INFO_LINK_STAT_ASSOC_FAILED: 1657 if (ic->ic_opmode == IEEE80211_M_STA) 1658 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 1659 break; 1660 } 1661 break; 1662 1663 case WI_INFO_COUNTERS: 1664 /* some card versions have a larger stats structure */ 1665 len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4); 1666 ptr = (u_int32_t *)&sc->sc_stats; 1667 off = sizeof(ltbuf); 1668 for (i = 0; i < len; i++, off += 2, ptr++) { 1669 wi_read_bap(sc, fid, off, &stat, sizeof(stat)); 1670 #ifdef WI_HERMES_STATS_WAR 1671 if (stat & 0xf000) 1672 stat = ~stat; 1673 #endif 1674 *ptr += stat; 1675 } 1676 ifp->if_collisions = sc->sc_stats.wi_tx_single_retries + 1677 sc->sc_stats.wi_tx_multi_retries + 1678 sc->sc_stats.wi_tx_retry_limit; 1679 break; 1680 1681 case WI_INFO_SCAN_RESULTS: 1682 case WI_INFO_HOST_SCAN_RESULTS: 1683 wi_scan_result(sc, fid, le16toh(ltbuf[0])); 1684 break; 1685 1686 default: 1687 DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid, 1688 le16toh(ltbuf[1]), le16toh(ltbuf[0]))); 1689 break; 1690 } 1691 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO); 1692 } 1693 1694 static int 1695 wi_write_multi(struct wi_softc *sc) 1696 { 1697 struct ifnet *ifp = &sc->sc_ic.ic_if; 1698 int n; 1699 struct ifmultiaddr *ifma; 1700 struct wi_mcast mlist; 1701 1702 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 1703 allmulti: 1704 memset(&mlist, 0, sizeof(mlist)); 1705 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist, 1706 sizeof(mlist)); 1707 } 1708 1709 n = 0; 1710 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1711 if (ifma->ifma_addr->sa_family != AF_LINK) 1712 continue; 1713 if (n >= 16) 1714 goto allmulti; 1715 IEEE80211_ADDR_COPY(&mlist.wi_mcast[n], 1716 (LLADDR((struct sockaddr_dl *)ifma->ifma_addr))); 1717 n++; 1718 } 1719 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist, 1720 IEEE80211_ADDR_LEN * n); 1721 } 1722 1723 static void 1724 wi_read_nicid(struct wi_softc *sc) 1725 { 1726 struct wi_card_ident *id; 1727 char *p; 1728 int len; 1729 u_int16_t ver[4]; 1730 1731 /* getting chip identity */ 1732 memset(ver, 0, sizeof(ver)); 1733 len = sizeof(ver); 1734 wi_read_rid(sc, WI_RID_CARD_ID, ver, &len); 1735 device_printf(sc->sc_dev, "using "); 1736 1737 sc->sc_firmware_type = WI_NOTYPE; 1738 for (id = wi_card_ident; id->card_name != NULL; id++) { 1739 if (le16toh(ver[0]) == id->card_id) { 1740 printf("%s", id->card_name); 1741 sc->sc_firmware_type = id->firm_type; 1742 break; 1743 } 1744 } 1745 if (sc->sc_firmware_type == WI_NOTYPE) { 1746 if (le16toh(ver[0]) & 0x8000) { 1747 printf("Unknown PRISM2 chip"); 1748 sc->sc_firmware_type = WI_INTERSIL; 1749 } else { 1750 printf("Unknown Lucent chip"); 1751 sc->sc_firmware_type = WI_LUCENT; 1752 } 1753 } 1754 1755 /* get primary firmware version (Only Prism chips) */ 1756 if (sc->sc_firmware_type != WI_LUCENT) { 1757 memset(ver, 0, sizeof(ver)); 1758 len = sizeof(ver); 1759 wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len); 1760 sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 + 1761 le16toh(ver[3]) * 100 + le16toh(ver[1]); 1762 } 1763 1764 /* get station firmware version */ 1765 memset(ver, 0, sizeof(ver)); 1766 len = sizeof(ver); 1767 wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len); 1768 sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 + 1769 le16toh(ver[3]) * 100 + le16toh(ver[1]); 1770 if (sc->sc_firmware_type == WI_INTERSIL && 1771 (sc->sc_sta_firmware_ver == 10102 || 1772 sc->sc_sta_firmware_ver == 20102)) { 1773 char ident[12]; 1774 memset(ident, 0, sizeof(ident)); 1775 len = sizeof(ident); 1776 /* value should be the format like "V2.00-11" */ 1777 if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 && 1778 *(p = (char *)ident) >= 'A' && 1779 p[2] == '.' && p[5] == '-' && p[8] == '\0') { 1780 sc->sc_firmware_type = WI_SYMBOL; 1781 sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 + 1782 (p[3] - '0') * 1000 + (p[4] - '0') * 100 + 1783 (p[6] - '0') * 10 + (p[7] - '0'); 1784 } 1785 } 1786 printf("\n"); 1787 device_printf(sc->sc_dev, "%s Firmware: ", 1788 sc->sc_firmware_type == WI_LUCENT ? "Lucent" : 1789 (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil")); 1790 if (sc->sc_firmware_type != WI_LUCENT) /* XXX */ 1791 printf("Primary (%u.%u.%u), ", 1792 sc->sc_pri_firmware_ver / 10000, 1793 (sc->sc_pri_firmware_ver % 10000) / 100, 1794 sc->sc_pri_firmware_ver % 100); 1795 printf("Station (%u.%u.%u)\n", 1796 sc->sc_sta_firmware_ver / 10000, 1797 (sc->sc_sta_firmware_ver % 10000) / 100, 1798 sc->sc_sta_firmware_ver % 100); 1799 } 1800 1801 static int 1802 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen) 1803 { 1804 struct wi_ssid ssid; 1805 1806 if (buflen > IEEE80211_NWID_LEN) 1807 return ENOBUFS; 1808 memset(&ssid, 0, sizeof(ssid)); 1809 ssid.wi_len = htole16(buflen); 1810 memcpy(ssid.wi_ssid, buf, buflen); 1811 return wi_write_rid(sc, rid, &ssid, sizeof(ssid)); 1812 } 1813 1814 static int 1815 wi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr) 1816 { 1817 struct wi_softc *sc = ifp->if_softc; 1818 struct ieee80211com *ic = &sc->sc_ic; 1819 struct ifreq *ifr = (struct ifreq *)data; 1820 struct wi_req wreq; 1821 struct wi_scan_res *res; 1822 size_t reslen; 1823 int len, n, error, mif, val, off, i; 1824 1825 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 1826 if (error) 1827 return error; 1828 len = (wreq.wi_len - 1) * 2; 1829 if (len < sizeof(u_int16_t)) 1830 return ENOSPC; 1831 if (len > sizeof(wreq.wi_val)) 1832 len = sizeof(wreq.wi_val); 1833 1834 switch (wreq.wi_type) { 1835 1836 case WI_RID_IFACE_STATS: 1837 memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats)); 1838 if (len < sizeof(sc->sc_stats)) 1839 error = ENOSPC; 1840 else 1841 len = sizeof(sc->sc_stats); 1842 break; 1843 1844 case WI_RID_ENCRYPTION: 1845 case WI_RID_TX_CRYPT_KEY: 1846 case WI_RID_DEFLT_CRYPT_KEYS: 1847 case WI_RID_TX_RATE: 1848 return ieee80211_cfgget(ifp, cmd, data, cr); 1849 1850 case WI_RID_MICROWAVE_OVEN: 1851 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) { 1852 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1853 &len); 1854 break; 1855 } 1856 wreq.wi_val[0] = htole16(sc->sc_microwave_oven); 1857 len = sizeof(u_int16_t); 1858 break; 1859 1860 case WI_RID_DBM_ADJUST: 1861 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) { 1862 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1863 &len); 1864 break; 1865 } 1866 wreq.wi_val[0] = htole16(sc->sc_dbm_offset); 1867 len = sizeof(u_int16_t); 1868 break; 1869 1870 case WI_RID_ROAMING_MODE: 1871 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) { 1872 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1873 &len); 1874 break; 1875 } 1876 wreq.wi_val[0] = htole16(sc->sc_roaming_mode); 1877 len = sizeof(u_int16_t); 1878 break; 1879 1880 case WI_RID_SYSTEM_SCALE: 1881 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) { 1882 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1883 &len); 1884 break; 1885 } 1886 wreq.wi_val[0] = htole16(sc->sc_system_scale); 1887 len = sizeof(u_int16_t); 1888 break; 1889 1890 case WI_RID_FRAG_THRESH: 1891 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) { 1892 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1893 &len); 1894 break; 1895 } 1896 wreq.wi_val[0] = htole16(ic->ic_fragthreshold); 1897 len = sizeof(u_int16_t); 1898 break; 1899 1900 case WI_RID_READ_APS: 1901 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1902 return ieee80211_cfgget(ifp, cmd, data, cr); 1903 if (sc->sc_scan_timer > 0) { 1904 error = EINPROGRESS; 1905 break; 1906 } 1907 n = sc->sc_naps; 1908 if (len < sizeof(n)) { 1909 error = ENOSPC; 1910 break; 1911 } 1912 if (len < sizeof(n) + sizeof(struct wi_apinfo) * n) 1913 n = (len - sizeof(n)) / sizeof(struct wi_apinfo); 1914 len = sizeof(n) + sizeof(struct wi_apinfo) * n; 1915 memcpy(wreq.wi_val, &n, sizeof(n)); 1916 memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps, 1917 sizeof(struct wi_apinfo) * n); 1918 break; 1919 1920 case WI_RID_PRISM2: 1921 wreq.wi_val[0] = sc->sc_firmware_type != WI_LUCENT; 1922 len = sizeof(u_int16_t); 1923 break; 1924 1925 case WI_RID_MIF: 1926 mif = wreq.wi_val[0]; 1927 error = wi_cmd(sc, WI_CMD_READMIF, mif, 0, 0); 1928 val = CSR_READ_2(sc, WI_RESP0); 1929 wreq.wi_val[0] = val; 1930 len = sizeof(u_int16_t); 1931 break; 1932 1933 case WI_RID_ZERO_CACHE: 1934 case WI_RID_PROCFRAME: /* ignore for compatibility */ 1935 /* XXX ??? */ 1936 break; 1937 1938 case WI_RID_READ_CACHE: 1939 return ieee80211_cfgget(ifp, cmd, data, cr); 1940 1941 case WI_RID_SCAN_RES: /* compatibility interface */ 1942 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1943 return ieee80211_cfgget(ifp, cmd, data, cr); 1944 if (sc->sc_scan_timer > 0) { 1945 error = EINPROGRESS; 1946 break; 1947 } 1948 n = sc->sc_naps; 1949 if (sc->sc_firmware_type == WI_LUCENT) { 1950 off = 0; 1951 reslen = WI_WAVELAN_RES_SIZE; 1952 } else { 1953 off = sizeof(struct wi_scan_p2_hdr); 1954 reslen = WI_PRISM2_RES_SIZE; 1955 } 1956 if (len < off + reslen * n) 1957 n = (len - off) / reslen; 1958 len = off + reslen * n; 1959 if (off != 0) { 1960 struct wi_scan_p2_hdr *p2 = (struct wi_scan_p2_hdr *)wreq.wi_val; 1961 /* 1962 * Prepend Prism-specific header. 1963 */ 1964 if (len < sizeof(struct wi_scan_p2_hdr)) { 1965 error = ENOSPC; 1966 break; 1967 } 1968 p2 = (struct wi_scan_p2_hdr *)wreq.wi_val; 1969 p2->wi_rsvd = 0; 1970 p2->wi_reason = n; /* XXX */ 1971 } 1972 for (i = 0; i < n; i++, off += reslen) { 1973 const struct wi_apinfo *ap = &sc->sc_aps[i]; 1974 1975 res = (struct wi_scan_res *)((char *)wreq.wi_val + off); 1976 res->wi_chan = ap->channel; 1977 res->wi_noise = ap->noise; 1978 res->wi_signal = ap->signal; 1979 IEEE80211_ADDR_COPY(res->wi_bssid, ap->bssid); 1980 res->wi_interval = ap->interval; 1981 res->wi_capinfo = ap->capinfo; 1982 res->wi_ssid_len = ap->namelen; 1983 memcpy(res->wi_ssid, ap->name, 1984 IEEE80211_NWID_LEN); 1985 if (sc->sc_firmware_type != WI_LUCENT) { 1986 /* XXX not saved from Prism cards */ 1987 memset(res->wi_srates, 0, 1988 sizeof(res->wi_srates)); 1989 res->wi_rate = ap->rate; 1990 res->wi_rsvd = 0; 1991 } 1992 } 1993 break; 1994 1995 default: 1996 if (sc->sc_enabled) { 1997 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1998 &len); 1999 break; 2000 } 2001 switch (wreq.wi_type) { 2002 case WI_RID_MAX_DATALEN: 2003 wreq.wi_val[0] = htole16(sc->sc_max_datalen); 2004 len = sizeof(u_int16_t); 2005 break; 2006 case WI_RID_RTS_THRESH: 2007 wreq.wi_val[0] = htole16(ic->ic_rtsthreshold); 2008 len = sizeof(u_int16_t); 2009 break; 2010 case WI_RID_CNFAUTHMODE: 2011 wreq.wi_val[0] = htole16(sc->sc_cnfauthmode); 2012 len = sizeof(u_int16_t); 2013 break; 2014 case WI_RID_NODENAME: 2015 if (len < sc->sc_nodelen + sizeof(u_int16_t)) { 2016 error = ENOSPC; 2017 break; 2018 } 2019 len = sc->sc_nodelen + sizeof(u_int16_t); 2020 wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2); 2021 memcpy(&wreq.wi_val[1], sc->sc_nodename, 2022 sc->sc_nodelen); 2023 break; 2024 default: 2025 return ieee80211_cfgget(ifp, cmd, data, cr); 2026 } 2027 break; 2028 } 2029 if (error) 2030 return error; 2031 wreq.wi_len = (len + 1) / 2 + 1; 2032 return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2); 2033 } 2034 2035 static int 2036 wi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data) 2037 { 2038 struct wi_softc *sc = ifp->if_softc; 2039 struct ieee80211com *ic = &sc->sc_ic; 2040 struct ifreq *ifr = (struct ifreq *)data; 2041 struct wi_req wreq; 2042 struct mbuf *m; 2043 int i, len, error, mif, val; 2044 struct ieee80211_rateset *rs; 2045 2046 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 2047 if (error) 2048 return error; 2049 len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0; 2050 switch (wreq.wi_type) { 2051 case WI_RID_DBM_ADJUST: 2052 return ENODEV; 2053 2054 case WI_RID_NODENAME: 2055 if (le16toh(wreq.wi_val[0]) * 2 > len || 2056 le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) { 2057 error = ENOSPC; 2058 break; 2059 } 2060 if (sc->sc_enabled) { 2061 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val, 2062 len); 2063 if (error) 2064 break; 2065 } 2066 sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2; 2067 memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen); 2068 break; 2069 2070 case WI_RID_MICROWAVE_OVEN: 2071 case WI_RID_ROAMING_MODE: 2072 case WI_RID_SYSTEM_SCALE: 2073 case WI_RID_FRAG_THRESH: 2074 if (wreq.wi_type == WI_RID_MICROWAVE_OVEN && 2075 (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0) 2076 break; 2077 if (wreq.wi_type == WI_RID_ROAMING_MODE && 2078 (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0) 2079 break; 2080 if (wreq.wi_type == WI_RID_SYSTEM_SCALE && 2081 (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0) 2082 break; 2083 if (wreq.wi_type == WI_RID_FRAG_THRESH && 2084 (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0) 2085 break; 2086 /* FALLTHROUGH */ 2087 case WI_RID_RTS_THRESH: 2088 case WI_RID_CNFAUTHMODE: 2089 case WI_RID_MAX_DATALEN: 2090 if (sc->sc_enabled) { 2091 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val, 2092 sizeof(u_int16_t)); 2093 if (error) 2094 break; 2095 } 2096 switch (wreq.wi_type) { 2097 case WI_RID_FRAG_THRESH: 2098 ic->ic_fragthreshold = le16toh(wreq.wi_val[0]); 2099 break; 2100 case WI_RID_RTS_THRESH: 2101 ic->ic_rtsthreshold = le16toh(wreq.wi_val[0]); 2102 break; 2103 case WI_RID_MICROWAVE_OVEN: 2104 sc->sc_microwave_oven = le16toh(wreq.wi_val[0]); 2105 break; 2106 case WI_RID_ROAMING_MODE: 2107 sc->sc_roaming_mode = le16toh(wreq.wi_val[0]); 2108 break; 2109 case WI_RID_SYSTEM_SCALE: 2110 sc->sc_system_scale = le16toh(wreq.wi_val[0]); 2111 break; 2112 case WI_RID_CNFAUTHMODE: 2113 sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]); 2114 break; 2115 case WI_RID_MAX_DATALEN: 2116 sc->sc_max_datalen = le16toh(wreq.wi_val[0]); 2117 break; 2118 } 2119 break; 2120 2121 case WI_RID_TX_RATE: 2122 switch (le16toh(wreq.wi_val[0])) { 2123 case 3: 2124 ic->ic_fixed_rate = -1; 2125 break; 2126 default: 2127 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B]; 2128 for (i = 0; i < rs->rs_nrates; i++) { 2129 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) 2130 / 2 == le16toh(wreq.wi_val[0])) 2131 break; 2132 } 2133 if (i == rs->rs_nrates) 2134 return EINVAL; 2135 ic->ic_fixed_rate = i; 2136 } 2137 if (sc->sc_enabled) 2138 error = wi_write_txrate(sc); 2139 break; 2140 2141 case WI_RID_SCAN_APS: 2142 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP) 2143 error = wi_scan_ap(sc, 0x3fff, 0x000f); 2144 break; 2145 2146 case WI_RID_SCAN_REQ: /* compatibility interface */ 2147 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP) 2148 error = wi_scan_ap(sc, wreq.wi_val[0], wreq.wi_val[1]); 2149 break; 2150 2151 case WI_RID_MGMT_XMIT: 2152 if (!sc->sc_enabled) { 2153 error = ENETDOWN; 2154 break; 2155 } 2156 if (ic->ic_mgtq.ifq_len > 5) { 2157 error = EAGAIN; 2158 break; 2159 } 2160 /* XXX wi_len looks in u_int8_t, not in u_int16_t */ 2161 m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL); 2162 if (m == NULL) { 2163 error = ENOMEM; 2164 break; 2165 } 2166 IF_ENQUEUE(&ic->ic_mgtq, m); 2167 break; 2168 2169 case WI_RID_MIF: 2170 mif = wreq.wi_val[0]; 2171 val = wreq.wi_val[1]; 2172 error = wi_cmd(sc, WI_CMD_WRITEMIF, mif, val, 0); 2173 break; 2174 2175 case WI_RID_PROCFRAME: /* ignore for compatibility */ 2176 break; 2177 2178 case WI_RID_OWN_SSID: 2179 if (le16toh(wreq.wi_val[0]) * 2 > len || 2180 le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) { 2181 error = ENOSPC; 2182 break; 2183 } 2184 memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN); 2185 ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2; 2186 memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen); 2187 error = ENETRESET; 2188 break; 2189 2190 default: 2191 if (sc->sc_enabled) { 2192 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val, 2193 len); 2194 if (error) 2195 break; 2196 } 2197 error = ieee80211_cfgset(ifp, cmd, data); 2198 break; 2199 } 2200 return error; 2201 } 2202 2203 static int 2204 wi_write_txrate(struct wi_softc *sc) 2205 { 2206 struct ieee80211com *ic = &sc->sc_ic; 2207 int i; 2208 u_int16_t rate; 2209 2210 if (ic->ic_fixed_rate < 0) 2211 rate = 0; /* auto */ 2212 else 2213 rate = (ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ic->ic_fixed_rate] & 2214 IEEE80211_RATE_VAL) / 2; 2215 2216 /* rate: 0, 1, 2, 5, 11 */ 2217 2218 switch (sc->sc_firmware_type) { 2219 case WI_LUCENT: 2220 switch (rate) { 2221 case 0: /* auto == 11mbps auto */ 2222 rate = 3; 2223 break; 2224 /* case 1, 2 map to 1, 2*/ 2225 case 5: /* 5.5Mbps -> 4 */ 2226 rate = 4; 2227 break; 2228 case 11: /* 11mbps -> 5 */ 2229 rate = 5; 2230 break; 2231 default: 2232 break; 2233 } 2234 break; 2235 default: 2236 /* Choose a bit according to this table. 2237 * 2238 * bit | data rate 2239 * ----+------------------- 2240 * 0 | 1Mbps 2241 * 1 | 2Mbps 2242 * 2 | 5.5Mbps 2243 * 3 | 11Mbps 2244 */ 2245 for (i = 8; i > 0; i >>= 1) { 2246 if (rate >= i) 2247 break; 2248 } 2249 if (i == 0) 2250 rate = 0xf; /* auto */ 2251 else 2252 rate = i; 2253 break; 2254 } 2255 return wi_write_val(sc, WI_RID_TX_RATE, rate); 2256 } 2257 2258 static int 2259 wi_write_wep(struct wi_softc *sc) 2260 { 2261 struct ieee80211com *ic = &sc->sc_ic; 2262 int error = 0; 2263 int i, keylen; 2264 u_int16_t val; 2265 struct wi_key wkey[IEEE80211_WEP_NKID]; 2266 2267 switch (sc->sc_firmware_type) { 2268 case WI_LUCENT: 2269 val = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0; 2270 error = wi_write_val(sc, WI_RID_ENCRYPTION, val); 2271 if (error) 2272 break; 2273 error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_wep_txkey); 2274 if (error) 2275 break; 2276 memset(wkey, 0, sizeof(wkey)); 2277 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 2278 keylen = ic->ic_nw_keys[i].wk_len; 2279 wkey[i].wi_keylen = htole16(keylen); 2280 memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key, 2281 keylen); 2282 } 2283 error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS, 2284 wkey, sizeof(wkey)); 2285 break; 2286 2287 case WI_INTERSIL: 2288 case WI_SYMBOL: 2289 if (ic->ic_flags & IEEE80211_F_WEPON) { 2290 /* 2291 * ONLY HWB3163 EVAL-CARD Firmware version 2292 * less than 0.8 variant2 2293 * 2294 * If promiscuous mode disable, Prism2 chip 2295 * does not work with WEP . 2296 * It is under investigation for details. 2297 * (ichiro@netbsd.org) 2298 */ 2299 if (sc->sc_firmware_type == WI_INTERSIL && 2300 sc->sc_sta_firmware_ver < 802 ) { 2301 /* firm ver < 0.8 variant 2 */ 2302 wi_write_val(sc, WI_RID_PROMISC, 1); 2303 } 2304 wi_write_val(sc, WI_RID_CNFAUTHMODE, 2305 sc->sc_cnfauthmode); 2306 val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED; 2307 /* 2308 * Encryption firmware has a bug for HostAP mode. 2309 */ 2310 if (sc->sc_firmware_type == WI_INTERSIL && 2311 ic->ic_opmode == IEEE80211_M_HOSTAP) 2312 val |= HOST_ENCRYPT; 2313 } else { 2314 wi_write_val(sc, WI_RID_CNFAUTHMODE, 2315 IEEE80211_AUTH_OPEN); 2316 val = HOST_ENCRYPT | HOST_DECRYPT; 2317 } 2318 error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val); 2319 if (error) 2320 break; 2321 error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY, 2322 ic->ic_wep_txkey); 2323 if (error) 2324 break; 2325 /* 2326 * It seems that the firmware accept 104bit key only if 2327 * all the keys have 104bit length. We get the length of 2328 * the transmit key and use it for all other keys. 2329 * Perhaps we should use software WEP for such situation. 2330 */ 2331 keylen = ic->ic_nw_keys[ic->ic_wep_txkey].wk_len; 2332 if (keylen > IEEE80211_WEP_KEYLEN) 2333 keylen = 13; /* 104bit keys */ 2334 else 2335 keylen = IEEE80211_WEP_KEYLEN; 2336 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 2337 error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i, 2338 ic->ic_nw_keys[i].wk_key, keylen); 2339 if (error) 2340 break; 2341 } 2342 break; 2343 } 2344 return error; 2345 } 2346 2347 static int 2348 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2) 2349 { 2350 int i, s = 0; 2351 static volatile int count = 0; 2352 2353 if (sc->wi_gone) 2354 return (ENODEV); 2355 2356 if (count > 0) 2357 panic("Hey partner, hold on there!"); 2358 count++; 2359 2360 /* wait for the busy bit to clear */ 2361 for (i = sc->wi_cmd_count; i > 0; i--) { /* 500ms */ 2362 if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY)) 2363 break; 2364 DELAY(1*1000); /* 1ms */ 2365 } 2366 if (i == 0) { 2367 device_printf(sc->sc_dev, "wi_cmd: busy bit won't clear.\n" ); 2368 sc->wi_gone = 1; 2369 count--; 2370 return(ETIMEDOUT); 2371 } 2372 2373 CSR_WRITE_2(sc, WI_PARAM0, val0); 2374 CSR_WRITE_2(sc, WI_PARAM1, val1); 2375 CSR_WRITE_2(sc, WI_PARAM2, val2); 2376 CSR_WRITE_2(sc, WI_COMMAND, cmd); 2377 2378 if (cmd == WI_CMD_INI) { 2379 /* XXX: should sleep here. */ 2380 DELAY(100*1000); /* 100ms delay for init */ 2381 } 2382 for (i = 0; i < WI_TIMEOUT; i++) { 2383 /* 2384 * Wait for 'command complete' bit to be 2385 * set in the event status register. 2386 */ 2387 s = CSR_READ_2(sc, WI_EVENT_STAT); 2388 if (s & WI_EV_CMD) { 2389 /* Ack the event and read result code. */ 2390 s = CSR_READ_2(sc, WI_STATUS); 2391 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD); 2392 if (s & WI_STAT_CMD_RESULT) { 2393 count--; 2394 return(EIO); 2395 } 2396 break; 2397 } 2398 DELAY(WI_DELAY); 2399 } 2400 2401 count--; 2402 if (i == WI_TIMEOUT) { 2403 device_printf(sc->sc_dev, 2404 "timeout in wi_cmd 0x%04x; event status 0x%04x\n", cmd, s); 2405 if (s == 0xffff) 2406 sc->wi_gone = 1; 2407 return(ETIMEDOUT); 2408 } 2409 return (0); 2410 } 2411 2412 static int 2413 wi_seek_bap(struct wi_softc *sc, int id, int off) 2414 { 2415 int i, status; 2416 2417 CSR_WRITE_2(sc, WI_SEL0, id); 2418 CSR_WRITE_2(sc, WI_OFF0, off); 2419 2420 for (i = 0; ; i++) { 2421 status = CSR_READ_2(sc, WI_OFF0); 2422 if ((status & WI_OFF_BUSY) == 0) 2423 break; 2424 if (i == WI_TIMEOUT) { 2425 device_printf(sc->sc_dev, "timeout in wi_seek to %x/%x\n", 2426 id, off); 2427 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 2428 if (status == 0xffff) 2429 sc->wi_gone = 1; 2430 return ETIMEDOUT; 2431 } 2432 DELAY(1); 2433 } 2434 if (status & WI_OFF_ERR) { 2435 device_printf(sc->sc_dev, "failed in wi_seek to %x/%x\n", id, off); 2436 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 2437 return EIO; 2438 } 2439 sc->sc_bap_id = id; 2440 sc->sc_bap_off = off; 2441 return 0; 2442 } 2443 2444 static int 2445 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen) 2446 { 2447 u_int16_t *ptr; 2448 int i, error, cnt; 2449 2450 if (buflen == 0) 2451 return 0; 2452 if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 2453 if ((error = wi_seek_bap(sc, id, off)) != 0) 2454 return error; 2455 } 2456 cnt = (buflen + 1) / 2; 2457 ptr = (u_int16_t *)buf; 2458 for (i = 0; i < cnt; i++) 2459 *ptr++ = CSR_READ_2(sc, WI_DATA0); 2460 sc->sc_bap_off += cnt * 2; 2461 return 0; 2462 } 2463 2464 static int 2465 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen) 2466 { 2467 u_int16_t *ptr; 2468 int i, error, cnt; 2469 2470 if (buflen == 0) 2471 return 0; 2472 2473 #ifdef WI_HERMES_AUTOINC_WAR 2474 again: 2475 #endif 2476 if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 2477 if ((error = wi_seek_bap(sc, id, off)) != 0) 2478 return error; 2479 } 2480 cnt = (buflen + 1) / 2; 2481 ptr = (u_int16_t *)buf; 2482 for (i = 0; i < cnt; i++) 2483 CSR_WRITE_2(sc, WI_DATA0, ptr[i]); 2484 sc->sc_bap_off += cnt * 2; 2485 2486 #ifdef WI_HERMES_AUTOINC_WAR 2487 /* 2488 * According to the comments in the HCF Light code, there is a bug 2489 * in the Hermes (or possibly in certain Hermes firmware revisions) 2490 * where the chip's internal autoincrement counter gets thrown off 2491 * during data writes: the autoincrement is missed, causing one 2492 * data word to be overwritten and subsequent words to be written to 2493 * the wrong memory locations. The end result is that we could end 2494 * up transmitting bogus frames without realizing it. The workaround 2495 * for this is to write a couple of extra guard words after the end 2496 * of the transfer, then attempt to read then back. If we fail to 2497 * locate the guard words where we expect them, we preform the 2498 * transfer over again. 2499 */ 2500 if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) { 2501 CSR_WRITE_2(sc, WI_DATA0, 0x1234); 2502 CSR_WRITE_2(sc, WI_DATA0, 0x5678); 2503 wi_seek_bap(sc, id, sc->sc_bap_off); 2504 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 2505 if (CSR_READ_2(sc, WI_DATA0) != 0x1234 || 2506 CSR_READ_2(sc, WI_DATA0) != 0x5678) { 2507 device_printf(sc->sc_dev, 2508 "detect auto increment bug, try again\n"); 2509 goto again; 2510 } 2511 } 2512 #endif 2513 return 0; 2514 } 2515 2516 static int 2517 wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen) 2518 { 2519 int error, len; 2520 struct mbuf *m; 2521 2522 for (m = m0; m != NULL && totlen > 0; m = m->m_next) { 2523 if (m->m_len == 0) 2524 continue; 2525 2526 len = min(m->m_len, totlen); 2527 2528 if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) { 2529 m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf); 2530 return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf, 2531 totlen); 2532 } 2533 2534 if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0) 2535 return error; 2536 2537 off += m->m_len; 2538 totlen -= len; 2539 } 2540 return 0; 2541 } 2542 2543 static int 2544 wi_alloc_fid(struct wi_softc *sc, int len, int *idp) 2545 { 2546 int i; 2547 2548 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) { 2549 device_printf(sc->sc_dev, "failed to allocate %d bytes on NIC\n", 2550 len); 2551 return ENOMEM; 2552 } 2553 2554 for (i = 0; i < WI_TIMEOUT; i++) { 2555 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC) 2556 break; 2557 if (i == WI_TIMEOUT) { 2558 device_printf(sc->sc_dev, "timeout in alloc\n"); 2559 return ETIMEDOUT; 2560 } 2561 DELAY(1); 2562 } 2563 *idp = CSR_READ_2(sc, WI_ALLOC_FID); 2564 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC); 2565 return 0; 2566 } 2567 2568 static int 2569 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp) 2570 { 2571 int error, len; 2572 u_int16_t ltbuf[2]; 2573 2574 /* Tell the NIC to enter record read mode. */ 2575 error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0); 2576 if (error) 2577 return error; 2578 2579 error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf)); 2580 if (error) 2581 return error; 2582 2583 if (le16toh(ltbuf[1]) != rid) { 2584 device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n", 2585 rid, le16toh(ltbuf[1])); 2586 return EIO; 2587 } 2588 len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */ 2589 if (*buflenp < len) { 2590 device_printf(sc->sc_dev, "record buffer is too small, " 2591 "rid=%x, size=%d, len=%d\n", 2592 rid, *buflenp, len); 2593 return ENOSPC; 2594 } 2595 *buflenp = len; 2596 return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len); 2597 } 2598 2599 static int 2600 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen) 2601 { 2602 int error; 2603 u_int16_t ltbuf[2]; 2604 2605 ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */ 2606 ltbuf[1] = htole16(rid); 2607 2608 error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf)); 2609 if (error) 2610 return error; 2611 error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen); 2612 if (error) 2613 return error; 2614 2615 return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0); 2616 } 2617 2618 static int 2619 wi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 2620 { 2621 struct ifnet *ifp = &ic->ic_if; 2622 struct wi_softc *sc = ifp->if_softc; 2623 struct ieee80211_node *ni = ic->ic_bss; 2624 int buflen; 2625 u_int16_t val; 2626 struct wi_ssid ssid; 2627 u_int8_t old_bssid[IEEE80211_ADDR_LEN]; 2628 2629 DPRINTF(("%s: %s -> %s\n", __func__, 2630 ieee80211_state_name[ic->ic_state], 2631 ieee80211_state_name[nstate])); 2632 2633 switch (nstate) { 2634 case IEEE80211_S_INIT: 2635 ic->ic_flags &= ~IEEE80211_F_SIBSS; 2636 sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 2637 return (*sc->sc_newstate)(ic, nstate, arg); 2638 2639 case IEEE80211_S_RUN: 2640 sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 2641 buflen = IEEE80211_ADDR_LEN; 2642 wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen); 2643 IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid); 2644 buflen = sizeof(val); 2645 wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen); 2646 /* XXX validate channel */ 2647 ni->ni_chan = &ic->ic_channels[le16toh(val)]; 2648 sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq = 2649 htole16(ni->ni_chan->ic_freq); 2650 sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags = 2651 htole16(ni->ni_chan->ic_flags); 2652 2653 if (IEEE80211_ADDR_EQ(old_bssid, ni->ni_bssid)) 2654 sc->sc_false_syns++; 2655 else 2656 sc->sc_false_syns = 0; 2657 2658 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 2659 ni->ni_esslen = ic->ic_des_esslen; 2660 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen); 2661 ni->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B]; 2662 ni->ni_intval = ic->ic_lintval; 2663 ni->ni_capinfo = IEEE80211_CAPINFO_ESS; 2664 if (ic->ic_flags & IEEE80211_F_WEPON) 2665 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 2666 } else { 2667 /* XXX check return value */ 2668 buflen = sizeof(ssid); 2669 wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen); 2670 ni->ni_esslen = le16toh(ssid.wi_len); 2671 if (ni->ni_esslen > IEEE80211_NWID_LEN) 2672 ni->ni_esslen = IEEE80211_NWID_LEN; /*XXX*/ 2673 memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen); 2674 } 2675 break; 2676 2677 case IEEE80211_S_SCAN: 2678 case IEEE80211_S_AUTH: 2679 case IEEE80211_S_ASSOC: 2680 break; 2681 } 2682 2683 ic->ic_state = nstate; /* NB: skip normal ieee80211 handling */ 2684 return 0; 2685 } 2686 2687 static int 2688 wi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate) 2689 { 2690 int error = 0; 2691 u_int16_t val[2]; 2692 2693 if (!sc->sc_enabled) 2694 return ENXIO; 2695 switch (sc->sc_firmware_type) { 2696 case WI_LUCENT: 2697 (void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0); 2698 break; 2699 case WI_INTERSIL: 2700 val[0] = chanmask; /* channel */ 2701 val[1] = txrate; /* tx rate */ 2702 error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val)); 2703 break; 2704 case WI_SYMBOL: 2705 /* 2706 * XXX only supported on 3.x ? 2707 */ 2708 val[0] = BSCAN_BCAST | BSCAN_ONETIME; 2709 error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ, 2710 val, sizeof(val[0])); 2711 break; 2712 } 2713 if (error == 0) { 2714 sc->sc_scan_timer = WI_SCAN_WAIT; 2715 sc->sc_ic.ic_if.if_timer = 1; 2716 DPRINTF(("wi_scan_ap: start scanning, " 2717 "chamask 0x%x txrate 0x%x\n", chanmask, txrate)); 2718 } 2719 return error; 2720 } 2721 2722 static void 2723 wi_scan_result(struct wi_softc *sc, int fid, int cnt) 2724 { 2725 #define N(a) (sizeof (a) / sizeof (a[0])) 2726 int i, naps, off, szbuf; 2727 struct wi_scan_header ws_hdr; /* Prism2 header */ 2728 struct wi_scan_data_p2 ws_dat; /* Prism2 scantable*/ 2729 struct wi_apinfo *ap; 2730 2731 off = sizeof(u_int16_t) * 2; 2732 memset(&ws_hdr, 0, sizeof(ws_hdr)); 2733 switch (sc->sc_firmware_type) { 2734 case WI_INTERSIL: 2735 wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr)); 2736 off += sizeof(ws_hdr); 2737 szbuf = sizeof(struct wi_scan_data_p2); 2738 break; 2739 case WI_SYMBOL: 2740 szbuf = sizeof(struct wi_scan_data_p2) + 6; 2741 break; 2742 case WI_LUCENT: 2743 szbuf = sizeof(struct wi_scan_data); 2744 break; 2745 default: 2746 device_printf(sc->sc_dev, 2747 "wi_scan_result: unknown firmware type %u\n", 2748 sc->sc_firmware_type); 2749 naps = 0; 2750 goto done; 2751 } 2752 naps = (cnt * 2 + 2 - off) / szbuf; 2753 if (naps > N(sc->sc_aps)) 2754 naps = N(sc->sc_aps); 2755 sc->sc_naps = naps; 2756 /* Read Data */ 2757 ap = sc->sc_aps; 2758 memset(&ws_dat, 0, sizeof(ws_dat)); 2759 for (i = 0; i < naps; i++, ap++) { 2760 wi_read_bap(sc, fid, off, &ws_dat, 2761 (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf)); 2762 DPRINTF2(("wi_scan_result: #%d: off %d bssid %6D\n", i, off, 2763 ws_dat.wi_bssid, ":")); 2764 off += szbuf; 2765 ap->scanreason = le16toh(ws_hdr.wi_reason); 2766 memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid)); 2767 ap->channel = le16toh(ws_dat.wi_chid); 2768 ap->signal = le16toh(ws_dat.wi_signal); 2769 ap->noise = le16toh(ws_dat.wi_noise); 2770 ap->quality = ap->signal - ap->noise; 2771 ap->capinfo = le16toh(ws_dat.wi_capinfo); 2772 ap->interval = le16toh(ws_dat.wi_interval); 2773 ap->rate = le16toh(ws_dat.wi_rate); 2774 ap->namelen = le16toh(ws_dat.wi_namelen); 2775 if (ap->namelen > sizeof(ap->name)) 2776 ap->namelen = sizeof(ap->name); 2777 memcpy(ap->name, ws_dat.wi_name, ap->namelen); 2778 } 2779 done: 2780 /* Done scanning */ 2781 sc->sc_scan_timer = 0; 2782 DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps)); 2783 #undef N 2784 } 2785 2786 static void 2787 wi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi) 2788 { 2789 ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr), 2790 ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL : -1, rssi); 2791 printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n", 2792 le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1), 2793 le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence); 2794 printf(" rx_signal %u rx_rate %u rx_flow %u\n", 2795 wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow); 2796 printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n", 2797 wh->wi_tx_rtry, wh->wi_tx_rate, 2798 le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len)); 2799 printf(" ehdr dst %6D src %6D type 0x%x\n", 2800 wh->wi_ehdr.ether_dhost, ":", wh->wi_ehdr.ether_shost, ":", 2801 wh->wi_ehdr.ether_type); 2802 } 2803 2804 int 2805 wi_alloc(device_t dev, int rid) 2806 { 2807 struct wi_softc *sc = device_get_softc(dev); 2808 2809 if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) { 2810 sc->iobase_rid = rid; 2811 sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT, 2812 &sc->iobase_rid, 0, ~0, (1 << 6), 2813 rman_make_alignment_flags(1 << 6) | RF_ACTIVE); 2814 if (!sc->iobase) { 2815 device_printf(dev, "No I/O space?!\n"); 2816 return (ENXIO); 2817 } 2818 2819 sc->wi_io_addr = rman_get_start(sc->iobase); 2820 sc->wi_btag = rman_get_bustag(sc->iobase); 2821 sc->wi_bhandle = rman_get_bushandle(sc->iobase); 2822 } else { 2823 sc->mem_rid = rid; 2824 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 2825 &sc->mem_rid, RF_ACTIVE); 2826 2827 if (!sc->mem) { 2828 device_printf(dev, "No Mem space on prism2.5?\n"); 2829 return (ENXIO); 2830 } 2831 2832 sc->wi_btag = rman_get_bustag(sc->mem); 2833 sc->wi_bhandle = rman_get_bushandle(sc->mem); 2834 } 2835 2836 2837 sc->irq_rid = 0; 2838 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid, 2839 RF_ACTIVE | 2840 ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE)); 2841 2842 if (!sc->irq) { 2843 wi_free(dev); 2844 device_printf(dev, "No irq?!\n"); 2845 return (ENXIO); 2846 } 2847 2848 sc->sc_dev = dev; 2849 sc->sc_unit = device_get_unit(dev); 2850 2851 return (0); 2852 } 2853 2854 void 2855 wi_free(device_t dev) 2856 { 2857 struct wi_softc *sc = device_get_softc(dev); 2858 2859 if (sc->iobase != NULL) { 2860 bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase); 2861 sc->iobase = NULL; 2862 } 2863 if (sc->irq != NULL) { 2864 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq); 2865 sc->irq = NULL; 2866 } 2867 if (sc->mem != NULL) { 2868 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem); 2869 sc->mem = NULL; 2870 } 2871 2872 return; 2873 } 2874 2875 static int 2876 wi_get_debug(struct wi_softc *sc, struct wi_req *wreq) 2877 { 2878 int error = 0; 2879 2880 wreq->wi_len = 1; 2881 2882 switch (wreq->wi_type) { 2883 case WI_DEBUG_SLEEP: 2884 wreq->wi_len++; 2885 wreq->wi_val[0] = sc->wi_debug.wi_sleep; 2886 break; 2887 case WI_DEBUG_DELAYSUPP: 2888 wreq->wi_len++; 2889 wreq->wi_val[0] = sc->wi_debug.wi_delaysupp; 2890 break; 2891 case WI_DEBUG_TXSUPP: 2892 wreq->wi_len++; 2893 wreq->wi_val[0] = sc->wi_debug.wi_txsupp; 2894 break; 2895 case WI_DEBUG_MONITOR: 2896 wreq->wi_len++; 2897 wreq->wi_val[0] = sc->wi_debug.wi_monitor; 2898 break; 2899 case WI_DEBUG_LEDTEST: 2900 wreq->wi_len += 3; 2901 wreq->wi_val[0] = sc->wi_debug.wi_ledtest; 2902 wreq->wi_val[1] = sc->wi_debug.wi_ledtest_param0; 2903 wreq->wi_val[2] = sc->wi_debug.wi_ledtest_param1; 2904 break; 2905 case WI_DEBUG_CONTTX: 2906 wreq->wi_len += 2; 2907 wreq->wi_val[0] = sc->wi_debug.wi_conttx; 2908 wreq->wi_val[1] = sc->wi_debug.wi_conttx_param0; 2909 break; 2910 case WI_DEBUG_CONTRX: 2911 wreq->wi_len++; 2912 wreq->wi_val[0] = sc->wi_debug.wi_contrx; 2913 break; 2914 case WI_DEBUG_SIGSTATE: 2915 wreq->wi_len += 2; 2916 wreq->wi_val[0] = sc->wi_debug.wi_sigstate; 2917 wreq->wi_val[1] = sc->wi_debug.wi_sigstate_param0; 2918 break; 2919 case WI_DEBUG_CONFBITS: 2920 wreq->wi_len += 2; 2921 wreq->wi_val[0] = sc->wi_debug.wi_confbits; 2922 wreq->wi_val[1] = sc->wi_debug.wi_confbits_param0; 2923 break; 2924 default: 2925 error = EIO; 2926 break; 2927 } 2928 2929 return (error); 2930 } 2931 2932 static int 2933 wi_set_debug(struct wi_softc *sc, struct wi_req *wreq) 2934 { 2935 int error = 0; 2936 u_int16_t cmd, param0 = 0, param1 = 0; 2937 2938 switch (wreq->wi_type) { 2939 case WI_DEBUG_RESET: 2940 case WI_DEBUG_INIT: 2941 case WI_DEBUG_CALENABLE: 2942 break; 2943 case WI_DEBUG_SLEEP: 2944 sc->wi_debug.wi_sleep = 1; 2945 break; 2946 case WI_DEBUG_WAKE: 2947 sc->wi_debug.wi_sleep = 0; 2948 break; 2949 case WI_DEBUG_CHAN: 2950 param0 = wreq->wi_val[0]; 2951 break; 2952 case WI_DEBUG_DELAYSUPP: 2953 sc->wi_debug.wi_delaysupp = 1; 2954 break; 2955 case WI_DEBUG_TXSUPP: 2956 sc->wi_debug.wi_txsupp = 1; 2957 break; 2958 case WI_DEBUG_MONITOR: 2959 sc->wi_debug.wi_monitor = 1; 2960 break; 2961 case WI_DEBUG_LEDTEST: 2962 param0 = wreq->wi_val[0]; 2963 param1 = wreq->wi_val[1]; 2964 sc->wi_debug.wi_ledtest = 1; 2965 sc->wi_debug.wi_ledtest_param0 = param0; 2966 sc->wi_debug.wi_ledtest_param1 = param1; 2967 break; 2968 case WI_DEBUG_CONTTX: 2969 param0 = wreq->wi_val[0]; 2970 sc->wi_debug.wi_conttx = 1; 2971 sc->wi_debug.wi_conttx_param0 = param0; 2972 break; 2973 case WI_DEBUG_STOPTEST: 2974 sc->wi_debug.wi_delaysupp = 0; 2975 sc->wi_debug.wi_txsupp = 0; 2976 sc->wi_debug.wi_monitor = 0; 2977 sc->wi_debug.wi_ledtest = 0; 2978 sc->wi_debug.wi_ledtest_param0 = 0; 2979 sc->wi_debug.wi_ledtest_param1 = 0; 2980 sc->wi_debug.wi_conttx = 0; 2981 sc->wi_debug.wi_conttx_param0 = 0; 2982 sc->wi_debug.wi_contrx = 0; 2983 sc->wi_debug.wi_sigstate = 0; 2984 sc->wi_debug.wi_sigstate_param0 = 0; 2985 break; 2986 case WI_DEBUG_CONTRX: 2987 sc->wi_debug.wi_contrx = 1; 2988 break; 2989 case WI_DEBUG_SIGSTATE: 2990 param0 = wreq->wi_val[0]; 2991 sc->wi_debug.wi_sigstate = 1; 2992 sc->wi_debug.wi_sigstate_param0 = param0; 2993 break; 2994 case WI_DEBUG_CONFBITS: 2995 param0 = wreq->wi_val[0]; 2996 param1 = wreq->wi_val[1]; 2997 sc->wi_debug.wi_confbits = param0; 2998 sc->wi_debug.wi_confbits_param0 = param1; 2999 break; 3000 default: 3001 error = EIO; 3002 break; 3003 } 3004 3005 if (error) 3006 return (error); 3007 3008 cmd = WI_CMD_DEBUG | (wreq->wi_type << 8); 3009 error = wi_cmd(sc, cmd, param0, param1, 0); 3010 3011 return (error); 3012 } 3013 3014 /* 3015 * Special routines to download firmware for Symbol CF card. 3016 * XXX: This should be modified generic into any PRISM-2 based card. 3017 */ 3018 3019 #define WI_SBCF_PDIADDR 0x3100 3020 3021 /* unaligned load little endian */ 3022 #define GETLE32(p) ((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24)) 3023 #define GETLE16(p) ((p)[0] | ((p)[1]<<8)) 3024 3025 int 3026 wi_symbol_load_firm(struct wi_softc *sc, const void *primsym, int primlen, 3027 const void *secsym, int seclen) 3028 { 3029 uint8_t ebuf[256]; 3030 int i; 3031 3032 /* load primary code and run it */ 3033 wi_symbol_set_hcr(sc, WI_HCR_EEHOLD); 3034 if (wi_symbol_write_firm(sc, primsym, primlen, NULL, 0)) 3035 return EIO; 3036 wi_symbol_set_hcr(sc, WI_HCR_RUN); 3037 for (i = 0; ; i++) { 3038 if (i == 10) 3039 return ETIMEDOUT; 3040 tsleep(sc, 0, "wiinit", 1); 3041 if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT) 3042 break; 3043 /* write the magic key value to unlock aux port */ 3044 CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0); 3045 CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1); 3046 CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2); 3047 CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL); 3048 } 3049 3050 /* issue read EEPROM command: XXX copied from wi_cmd() */ 3051 CSR_WRITE_2(sc, WI_PARAM0, 0); 3052 CSR_WRITE_2(sc, WI_PARAM1, 0); 3053 CSR_WRITE_2(sc, WI_PARAM2, 0); 3054 CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE); 3055 for (i = 0; i < WI_TIMEOUT; i++) { 3056 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD) 3057 break; 3058 DELAY(1); 3059 } 3060 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD); 3061 3062 CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ); 3063 CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ); 3064 CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA, 3065 (uint16_t *)ebuf, sizeof(ebuf) / 2); 3066 if (GETLE16(ebuf) > sizeof(ebuf)) 3067 return EIO; 3068 if (wi_symbol_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf))) 3069 return EIO; 3070 return 0; 3071 } 3072 3073 static int 3074 wi_symbol_write_firm(struct wi_softc *sc, const void *buf, int buflen, 3075 const void *ebuf, int ebuflen) 3076 { 3077 const uint8_t *p, *ep, *q, *eq; 3078 char *tp; 3079 uint32_t addr, id, eid; 3080 int i, len, elen, nblk, pdrlen; 3081 3082 /* 3083 * Parse the header of the firmware image. 3084 */ 3085 p = buf; 3086 ep = p + buflen; 3087 while (p < ep && *p++ != ' '); /* FILE: */ 3088 while (p < ep && *p++ != ' '); /* filename */ 3089 while (p < ep && *p++ != ' '); /* type of the firmware */ 3090 nblk = strtoul(p, &tp, 10); 3091 p = tp; 3092 pdrlen = strtoul(p + 1, &tp, 10); 3093 p = tp; 3094 while (p < ep && *p++ != 0x1a); /* skip rest of header */ 3095 3096 /* 3097 * Block records: address[4], length[2], data[length]; 3098 */ 3099 for (i = 0; i < nblk; i++) { 3100 addr = GETLE32(p); p += 4; 3101 len = GETLE16(p); p += 2; 3102 CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ); 3103 CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ); 3104 CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA, 3105 (const uint16_t *)p, len / 2); 3106 p += len; 3107 } 3108 3109 /* 3110 * PDR: id[4], address[4], length[4]; 3111 */ 3112 for (i = 0; i < pdrlen; ) { 3113 id = GETLE32(p); p += 4; i += 4; 3114 addr = GETLE32(p); p += 4; i += 4; 3115 len = GETLE32(p); p += 4; i += 4; 3116 /* replace PDR entry with the values from EEPROM, if any */ 3117 for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) { 3118 elen = GETLE16(q); q += 2; 3119 eid = GETLE16(q); q += 2; 3120 elen--; /* elen includes eid */ 3121 if (eid == 0) 3122 break; 3123 if (eid != id) 3124 continue; 3125 CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ); 3126 CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ); 3127 CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA, 3128 (const uint16_t *)q, len / 2); 3129 break; 3130 } 3131 } 3132 return 0; 3133 } 3134 3135 static int 3136 wi_symbol_set_hcr(struct wi_softc *sc, int mode) 3137 { 3138 uint16_t hcr; 3139 3140 CSR_WRITE_2(sc, WI_COR, WI_COR_RESET); 3141 tsleep(sc, 0, "wiinit", 1); 3142 hcr = CSR_READ_2(sc, WI_HCR); 3143 hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE); 3144 CSR_WRITE_2(sc, WI_HCR, hcr); 3145 tsleep(sc, 0, "wiinit", 1); 3146 CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE); 3147 tsleep(sc, 0, "wiinit", 1); 3148 return 0; 3149 } 3150