1 /* $NetBSD: wi.c,v 1.104 2002/11/18 15:10:22 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 35 /* 36 * Lucent WaveLAN/IEEE 802.11 PCMCIA driver for NetBSD. 37 * 38 * Original FreeBSD driver written by Bill Paul <wpaul@ctr.columbia.edu> 39 * Electrical Engineering Department 40 * Columbia University, New York City 41 */ 42 43 /* 44 * The WaveLAN/IEEE adapter is the second generation of the WaveLAN 45 * from Lucent. Unlike the older cards, the new ones are programmed 46 * entirely via a firmware-driven controller called the Hermes. 47 * Unfortunately, Lucent will not release the Hermes programming manual 48 * without an NDA (if at all). What they do release is an API library 49 * called the HCF (Hardware Control Functions) which is supposed to 50 * do the device-specific operations of a device driver for you. The 51 * publically available version of the HCF library (the 'HCF Light') is 52 * a) extremely gross, b) lacks certain features, particularly support 53 * for 802.11 frames, and c) is contaminated by the GNU Public License. 54 * 55 * This driver does not use the HCF or HCF Light at all. Instead, it 56 * programs the Hermes controller directly, using information gleaned 57 * from the HCF Light code and corresponding documentation. 58 * 59 * This driver supports both the PCMCIA and ISA versions of the 60 * WaveLAN/IEEE cards. Note however that the ISA card isn't really 61 * anything of the sort: it's actually a PCMCIA bridge adapter 62 * that fits into an ISA slot, into which a PCMCIA WaveLAN card is 63 * inserted. Consequently, you need to use the pccard support for 64 * both the ISA and PCMCIA adapters. 65 */ 66 67 /* 68 * FreeBSD driver ported to NetBSD by Bill Sommerfeld in the back of the 69 * Oslo IETF plenary meeting. 70 */ 71 72 #include <sys/cdefs.h> 73 __KERNEL_RCSID(0, "$NetBSD: wi.c,v 1.104 2002/11/18 15:10:22 dyoung Exp $"); 74 75 #define WI_HERMES_AUTOINC_WAR /* Work around data write autoinc bug. */ 76 #define WI_HERMES_STATS_WAR /* Work around stats counter bug. */ 77 78 #include "bpfilter.h" 79 80 #include <sys/param.h> 81 #include <sys/systm.h> 82 #include <sys/callout.h> 83 #include <sys/device.h> 84 #include <sys/socket.h> 85 #include <sys/mbuf.h> 86 #include <sys/ioctl.h> 87 #include <sys/kernel.h> /* for hz */ 88 #include <sys/proc.h> 89 90 #include <net/if.h> 91 #include <net/if_dl.h> 92 #include <net/if_media.h> 93 #include <net/if_ether.h> 94 #include <net/if_ieee80211.h> 95 96 #if NBPFILTER > 0 97 #include <net/bpf.h> 98 #include <net/bpfdesc.h> 99 #endif 100 101 #include <machine/bus.h> 102 103 #include <dev/ic/wi_ieee.h> 104 #include <dev/ic/wireg.h> 105 #include <dev/ic/wivar.h> 106 107 static int wi_init(struct ifnet *); 108 static void wi_stop(struct ifnet *, int); 109 static void wi_start(struct ifnet *); 110 static int wi_reset(struct wi_softc *); 111 static void wi_watchdog(struct ifnet *); 112 static int wi_ioctl(struct ifnet *, u_long, caddr_t); 113 static int wi_media_change(struct ifnet *); 114 static void wi_media_status(struct ifnet *, struct ifmediareq *); 115 116 static void wi_rx_intr(struct wi_softc *); 117 static void wi_tx_intr(struct wi_softc *); 118 static void wi_info_intr(struct wi_softc *); 119 120 static int wi_get_cfg(struct ifnet *, u_long, caddr_t); 121 static int wi_set_cfg(struct ifnet *, u_long, caddr_t); 122 static int wi_write_txrate(struct wi_softc *); 123 static int wi_write_wep(struct wi_softc *); 124 static int wi_write_multi(struct wi_softc *); 125 static int wi_alloc_fid(struct wi_softc *, int, int *); 126 static void wi_read_nicid(struct wi_softc *); 127 static int wi_write_ssid(struct wi_softc *, int, u_int8_t *, int); 128 129 static int wi_cmd(struct wi_softc *, int, int, int, int); 130 static int wi_seek_bap(struct wi_softc *, int, int); 131 static int wi_read_bap(struct wi_softc *, int, int, void *, int); 132 static int wi_write_bap(struct wi_softc *, int, int, void *, int); 133 static int wi_read_rid(struct wi_softc *, int, void *, int *); 134 static int wi_write_rid(struct wi_softc *, int, void *, int); 135 136 static int wi_newstate(void *, enum ieee80211_state); 137 138 static int wi_scan_ap(struct wi_softc *); 139 static void wi_scan_result(struct wi_softc *, int, int); 140 141 static inline int 142 wi_write_val(struct wi_softc *sc, int rid, u_int16_t val) 143 { 144 145 val = htole16(val); 146 return wi_write_rid(sc, rid, &val, sizeof(val)); 147 } 148 149 #ifdef WI_DEBUG 150 int wi_debug = 0; 151 152 #define DPRINTF(X) if (wi_debug) printf X 153 #define DPRINTF2(X) if (wi_debug > 1) printf X 154 #else 155 #define DPRINTF(X) 156 #define DPRINTF2(X) 157 #endif 158 159 #define WI_INTRS (WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO) 160 161 struct wi_card_ident 162 wi_card_ident[] = { 163 /* CARD_ID CARD_NAME FIRM_TYPE */ 164 { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT }, 165 { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT }, 166 { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT }, 167 { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL }, 168 { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL }, 169 { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL }, 170 { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL }, 171 { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL }, 172 { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL }, 173 { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL }, 174 { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL }, 175 { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL }, 176 { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 177 { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 178 { WI_NIC_3842_PCMCIA_ATM_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL }, 179 { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 180 { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 181 { WI_NIC_3842_MINI_ATM_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL }, 182 { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 183 { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 184 { WI_NIC_3842_PCI_ATM_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL }, 185 { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 186 { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL }, 187 { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 188 { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL }, 189 { 0, NULL, 0 }, 190 }; 191 192 int 193 wi_attach(struct wi_softc *sc) 194 { 195 struct ieee80211com *ic = &sc->sc_ic; 196 struct ifnet *ifp = &ic->ic_if; 197 int i, nrate, mword, buflen; 198 u_int8_t r; 199 u_int16_t val; 200 u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE]; 201 static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = { 202 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 203 }; 204 int s; 205 206 s = splnet(); 207 208 /* Make sure interrupts are disabled. */ 209 CSR_WRITE_2(sc, WI_INT_EN, 0); 210 CSR_WRITE_2(sc, WI_EVENT_ACK, ~0); 211 212 /* Reset the NIC. */ 213 if (wi_reset(sc) != 0) { 214 splx(s); 215 return 1; 216 } 217 218 buflen = IEEE80211_ADDR_LEN; 219 if (wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen) != 0 || 220 IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) { 221 printf(" could not get mac address, attach failed\n"); 222 splx(s); 223 return 1; 224 } 225 226 printf(" 802.11 address %s\n", ether_sprintf(ic->ic_myaddr)); 227 228 /* Read NIC identification */ 229 wi_read_nicid(sc); 230 231 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ); 232 ifp->if_softc = sc; 233 ifp->if_start = wi_start; 234 ifp->if_ioctl = wi_ioctl; 235 ifp->if_watchdog = wi_watchdog; 236 ifp->if_init = wi_init; 237 ifp->if_stop = wi_stop; 238 ifp->if_flags = 239 IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST | IFF_NOTRAILERS; 240 IFQ_SET_READY(&ifp->if_snd); 241 242 ic->ic_phytype = IEEE80211_T_DS; 243 ic->ic_opmode = IEEE80211_M_STA; 244 ic->ic_flags = IEEE80211_F_HASPMGT | IEEE80211_F_HASAHDEMO; 245 ic->ic_state = IEEE80211_S_INIT; 246 ic->ic_newstate = wi_newstate; 247 248 /* Find available channel */ 249 buflen = sizeof(val); 250 if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0) 251 val = htole16(0x1fff); /* assume 1-11 */ 252 for (i = 0; i < 16; i++) { 253 if (isset((u_int8_t*)&val, i)) 254 setbit(ic->ic_chan_avail, i + 1); 255 } 256 257 sc->sc_dbm_adjust = 100; /* default */ 258 259 if (sc->sc_firmware_type == WI_INTERSIL && 260 wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) { 261 sc->sc_dbm_adjust = le16toh(val); 262 } 263 264 /* Find default IBSS channel */ 265 buflen = sizeof(val); 266 if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0) 267 ic->ic_ibss_chan = le16toh(val); 268 else { 269 /* use lowest available channel */ 270 for (i = 0; i < 16; i++) { 271 if (isset(ic->ic_chan_avail, i)) 272 break; 273 } 274 ic->ic_ibss_chan = i; 275 } 276 277 /* 278 * Set flags based on firmware version. 279 */ 280 switch (sc->sc_firmware_type) { 281 case WI_LUCENT: 282 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE; 283 #ifdef WI_HERMES_AUTOINC_WAR 284 /* XXX: not confirmed, but never seen for recent firmware */ 285 if (sc->sc_sta_firmware_ver < 40000) { 286 sc->sc_flags |= WI_FLAGS_BUG_AUTOINC; 287 } 288 #endif 289 if (sc->sc_sta_firmware_ver >= 60000) 290 sc->sc_flags |= WI_FLAGS_HAS_MOR; 291 if (sc->sc_sta_firmware_ver >= 60006) 292 ic->ic_flags |= IEEE80211_F_HASIBSS; 293 sc->sc_ibss_port = 1; 294 break; 295 296 case WI_INTERSIL: 297 sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR; 298 sc->sc_flags |= WI_FLAGS_HAS_ROAMING; 299 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE; 300 if (sc->sc_sta_firmware_ver >= 800) { 301 ic->ic_flags |= IEEE80211_F_HASHOSTAP; 302 ic->ic_flags |= IEEE80211_F_HASIBSS; 303 } 304 sc->sc_ibss_port = 0; 305 break; 306 307 case WI_SYMBOL: 308 sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY; 309 if (sc->sc_sta_firmware_ver >= 20000) 310 ic->ic_flags |= IEEE80211_F_HASIBSS; 311 sc->sc_ibss_port = 4; 312 break; 313 } 314 315 /* 316 * Find out if we support WEP on this card. 317 */ 318 buflen = sizeof(val); 319 if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 && 320 val != htole16(0)) 321 ic->ic_flags |= IEEE80211_F_HASWEP; 322 323 /* Find supported rates. */ 324 buflen = sizeof(ratebuf); 325 if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) { 326 nrate = le16toh(*(u_int16_t *)ratebuf); 327 if (nrate > IEEE80211_RATE_SIZE) 328 nrate = IEEE80211_RATE_SIZE; 329 memcpy(ic->ic_sup_rates, ratebuf + 2, nrate); 330 } 331 buflen = sizeof(val); 332 333 sc->sc_max_datalen = 2304; 334 sc->sc_rts_thresh = 2347; 335 sc->sc_frag_thresh = 2346; 336 sc->sc_system_scale = 1; 337 sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN; 338 sc->sc_roaming_mode = 1; 339 340 ifmedia_init(&sc->sc_media, 0, wi_media_change, wi_media_status); 341 printf("%s: supported rates: ", sc->sc_dev.dv_xname); 342 #define ADD(s, o) ifmedia_add(&sc->sc_media, \ 343 IFM_MAKEWORD(IFM_IEEE80211, (s), (o), 0), 0, NULL) 344 ADD(IFM_AUTO, 0); 345 if (ic->ic_flags & IEEE80211_F_HASHOSTAP) 346 ADD(IFM_AUTO, IFM_IEEE80211_HOSTAP); 347 if (ic->ic_flags & IEEE80211_F_HASIBSS) 348 ADD(IFM_AUTO, IFM_IEEE80211_ADHOC); 349 ADD(IFM_AUTO, IFM_IEEE80211_ADHOC | IFM_FLAG0); 350 for (i = 0; i < nrate; i++) { 351 r = ic->ic_sup_rates[i]; 352 mword = ieee80211_rate2media(r, IEEE80211_T_DS); 353 if (mword == 0) 354 continue; 355 printf("%s%d%sMbps", (i != 0 ? " " : ""), 356 (r & IEEE80211_RATE_VAL) / 2, ((r & 0x1) != 0 ? ".5" : "")); 357 ADD(mword, 0); 358 if (ic->ic_flags & IEEE80211_F_HASHOSTAP) 359 ADD(mword, IFM_IEEE80211_HOSTAP); 360 if (ic->ic_flags & IEEE80211_F_HASIBSS) 361 ADD(mword, IFM_IEEE80211_ADHOC); 362 ADD(mword, IFM_IEEE80211_ADHOC | IFM_FLAG0); 363 } 364 printf("\n"); 365 ifmedia_set(&sc->sc_media, IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO, 0, 0)); 366 #undef ADD 367 368 /* 369 * Call MI attach routines. 370 */ 371 372 if_attach(ifp); 373 ieee80211_ifattach(ifp); 374 375 /* Attach is successful. */ 376 sc->sc_attached = 1; 377 378 splx(s); 379 return 0; 380 } 381 382 int 383 wi_detach(struct wi_softc *sc) 384 { 385 struct ifnet *ifp = &sc->sc_ic.ic_if; 386 int s; 387 388 if (!sc->sc_attached) 389 return 0; 390 391 s = splnet(); 392 393 /* Delete all remaining media. */ 394 ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY); 395 396 ieee80211_ifdetach(ifp); 397 if_detach(ifp); 398 if (sc->sc_enabled) { 399 if (sc->sc_disable) 400 (*sc->sc_disable)(sc); 401 sc->sc_enabled = 0; 402 } 403 splx(s); 404 return 0; 405 } 406 407 int 408 wi_activate(struct device *self, enum devact act) 409 { 410 struct wi_softc *sc = (struct wi_softc *)self; 411 int rv = 0, s; 412 413 s = splnet(); 414 switch (act) { 415 case DVACT_ACTIVATE: 416 rv = EOPNOTSUPP; 417 break; 418 419 case DVACT_DEACTIVATE: 420 if_deactivate(&sc->sc_ic.ic_if); 421 break; 422 } 423 splx(s); 424 return rv; 425 } 426 427 void 428 wi_power(struct wi_softc *sc, int why) 429 { 430 struct ifnet *ifp = &sc->sc_ic.ic_if; 431 int s; 432 433 s = splnet(); 434 switch (why) { 435 case PWR_SUSPEND: 436 case PWR_STANDBY: 437 wi_stop(ifp, 1); 438 break; 439 case PWR_RESUME: 440 if (ifp->if_flags & IFF_UP) { 441 wi_init(ifp); 442 (void)wi_intr(sc); 443 } 444 break; 445 case PWR_SOFTSUSPEND: 446 case PWR_SOFTSTANDBY: 447 case PWR_SOFTRESUME: 448 break; 449 } 450 splx(s); 451 } 452 453 void 454 wi_shutdown(struct wi_softc *sc) 455 { 456 struct ifnet *ifp = &sc->sc_ic.ic_if; 457 458 if (sc->sc_attached) 459 wi_stop(ifp, 1); 460 } 461 462 int 463 wi_intr(void *arg) 464 { 465 int i; 466 struct wi_softc *sc = arg; 467 struct ifnet *ifp = &sc->sc_ic.ic_if; 468 u_int16_t status, raw_status, last_status; 469 470 if (sc->sc_enabled == 0 || 471 (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 || 472 (ifp->if_flags & IFF_RUNNING) == 0) 473 return 0; 474 475 if ((ifp->if_flags & IFF_UP) == 0) { 476 CSR_WRITE_2(sc, WI_EVENT_ACK, ~0); 477 CSR_WRITE_2(sc, WI_INT_EN, 0); 478 return 1; 479 } 480 481 /* maximum 10 loops per interrupt */ 482 last_status = 0; 483 for (i = 0; i < 10; i++) { 484 /* 485 * Only believe a status bit when we enter wi_intr, or when 486 * the bit was "off" the last time through the loop. This is 487 * my strategy to avoid racing the hardware/firmware if I 488 * can re-read the event status register more quickly than 489 * it is updated. 490 */ 491 raw_status = CSR_READ_2(sc, WI_EVENT_STAT); 492 status = raw_status & ~last_status; 493 if ((status & WI_INTRS) == 0) 494 break; 495 last_status = raw_status; 496 497 if (status & WI_EV_RX) 498 wi_rx_intr(sc); 499 500 if (status & WI_EV_ALLOC) 501 wi_tx_intr(sc); 502 503 if (status & WI_EV_INFO) 504 wi_info_intr(sc); 505 506 if ((ifp->if_flags & IFF_OACTIVE) == 0 && 507 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 && 508 !IFQ_IS_EMPTY(&ifp->if_snd)) 509 wi_start(ifp); 510 } 511 512 return 1; 513 } 514 515 static int 516 wi_init(struct ifnet *ifp) 517 { 518 struct wi_softc *sc = ifp->if_softc; 519 struct ieee80211com *ic = &sc->sc_ic; 520 struct wi_joinreq join; 521 int i; 522 int error = 0, wasenabled; 523 524 DPRINTF(("wi_init: enabled %d\n", sc->sc_enabled)); 525 wasenabled = sc->sc_enabled; 526 if (!sc->sc_enabled) { 527 if ((error = (*sc->sc_enable)(sc)) != 0) 528 goto out; 529 sc->sc_enabled = 1; 530 } else 531 wi_stop(ifp, 0); 532 533 /* Symbol firmware cannot be initialized more than once */ 534 if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) { 535 if ((error = wi_reset(sc)) != 0) 536 goto out; 537 } 538 539 /* common 802.11 configuration */ 540 ic->ic_flags &= ~IEEE80211_F_IBSSON; 541 sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 542 switch (ic->ic_opmode) { 543 case IEEE80211_M_STA: 544 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS); 545 break; 546 case IEEE80211_M_IBSS: 547 wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port); 548 ic->ic_flags |= IEEE80211_F_IBSSON; 549 break; 550 case IEEE80211_M_AHDEMO: 551 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC); 552 break; 553 case IEEE80211_M_HOSTAP: 554 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP); 555 break; 556 } 557 558 /* Intersil interprets this RID as joining ESS even in IBSS mode */ 559 if (sc->sc_firmware_type == WI_LUCENT && 560 (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0) 561 wi_write_val(sc, WI_RID_CREATE_IBSS, 1); 562 else 563 wi_write_val(sc, WI_RID_CREATE_IBSS, 0); 564 wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval); 565 wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid, 566 ic->ic_des_esslen); 567 wi_write_val(sc, WI_RID_OWN_CHNL, ic->ic_ibss_chan); 568 wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen); 569 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl)); 570 wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN); 571 wi_write_val(sc, WI_RID_PM_ENABLED, 572 (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0); 573 574 /* not yet common 802.11 configuration */ 575 wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen); 576 wi_write_val(sc, WI_RID_RTS_THRESH, sc->sc_rts_thresh); 577 if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) 578 wi_write_val(sc, WI_RID_FRAG_THRESH, sc->sc_frag_thresh); 579 580 /* driver specific 802.11 configuration */ 581 if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) 582 wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale); 583 if (sc->sc_flags & WI_FLAGS_HAS_ROAMING) 584 wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode); 585 if (sc->sc_flags & WI_FLAGS_HAS_MOR) 586 wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven); 587 wi_write_txrate(sc); 588 wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen); 589 590 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 591 sc->sc_firmware_type == WI_INTERSIL) { 592 wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval); 593 wi_write_val(sc, WI_RID_BASIC_RATE, 0x03); /* 1, 2 */ 594 wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */ 595 wi_write_val(sc, WI_RID_DTIM_PERIOD, 1); 596 } 597 598 /* 599 * Initialize promisc mode. 600 * Being in the Host-AP mode causes a great 601 * deal of pain if primisc mode is set. 602 * Therefore we avoid confusing the firmware 603 * and always reset promisc mode in Host-AP 604 * mode. Host-AP sees all the packets anyway. 605 */ 606 if (ic->ic_opmode != IEEE80211_M_HOSTAP && 607 (ifp->if_flags & IFF_PROMISC) != 0) { 608 wi_write_val(sc, WI_RID_PROMISC, 1); 609 } else { 610 wi_write_val(sc, WI_RID_PROMISC, 0); 611 } 612 613 /* Configure WEP. */ 614 if (ic->ic_flags & IEEE80211_F_HASWEP) 615 wi_write_wep(sc); 616 617 /* Set multicast filter. */ 618 wi_write_multi(sc); 619 620 if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) { 621 sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame); 622 if (sc->sc_firmware_type == WI_SYMBOL) 623 sc->sc_buflen = 1585; /* XXX */ 624 for (i = 0; i < WI_NTXBUF; i++) { 625 error = wi_alloc_fid(sc, sc->sc_buflen, 626 &sc->sc_txd[i].d_fid); 627 if (error) { 628 printf("%s: tx buffer allocation failed\n", 629 sc->sc_dev.dv_xname); 630 goto out; 631 } 632 DPRINTF2(("wi_init: txbuf %d allocated %x\n", i, 633 sc->sc_txd[i].d_fid)); 634 sc->sc_txd[i].d_len = 0; 635 } 636 } 637 sc->sc_txcur = sc->sc_txnext = 0; 638 639 /* Enable port 0 */ 640 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0); 641 ifp->if_flags |= IFF_RUNNING; 642 ifp->if_flags &= ~IFF_OACTIVE; 643 if (ic->ic_opmode == IEEE80211_M_AHDEMO || 644 ic->ic_opmode == IEEE80211_M_HOSTAP) 645 wi_newstate(sc, IEEE80211_S_RUN); 646 647 /* Enable interrupts */ 648 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS); 649 650 if (!wasenabled && 651 ic->ic_opmode == IEEE80211_M_HOSTAP && 652 sc->sc_firmware_type == WI_INTERSIL) { 653 /* XXX: some card need to be re-enabled for hostap */ 654 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0); 655 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0); 656 } 657 658 if (ic->ic_opmode == IEEE80211_M_STA && 659 ((ic->ic_flags & IEEE80211_F_DESBSSID) || 660 ic->ic_des_chan != IEEE80211_CHAN_ANY)) { 661 memset(&join, 0, sizeof(join)); 662 if (ic->ic_flags & IEEE80211_F_DESBSSID) 663 IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid); 664 if (ic->ic_des_chan != IEEE80211_CHAN_ANY) 665 join.wi_chan = htole16(ic->ic_des_chan); 666 wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join)); 667 } 668 669 out: 670 if (error) { 671 printf("%s: interface not running\n", sc->sc_dev.dv_xname); 672 wi_stop(ifp, 0); 673 } 674 DPRINTF(("wi_init: return %d\n", error)); 675 return error; 676 } 677 678 static void 679 wi_stop(struct ifnet *ifp, int disable) 680 { 681 struct wi_softc *sc = ifp->if_softc; 682 683 DPRINTF(("wi_stop: disable %d\n", disable)); 684 ieee80211_new_state(ifp, IEEE80211_S_INIT, -1); 685 if (sc->sc_enabled) { 686 CSR_WRITE_2(sc, WI_INT_EN, 0); 687 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0); 688 if (disable) { 689 if (sc->sc_disable) 690 (*sc->sc_disable)(sc); 691 sc->sc_enabled = 0; 692 } 693 } 694 695 sc->sc_tx_timer = 0; 696 sc->sc_scan_timer = 0; 697 sc->sc_naps = 0; 698 ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING); 699 ifp->if_timer = 0; 700 } 701 702 static void 703 wi_start(struct ifnet *ifp) 704 { 705 struct wi_softc *sc = ifp->if_softc; 706 struct ieee80211com *ic = &sc->sc_ic; 707 struct ieee80211_node *ni; 708 struct ieee80211_frame *wh; 709 struct mbuf *m0, *m; 710 struct wi_frame frmhdr; 711 int cur, fid, off; 712 713 if (ifp->if_flags & IFF_OACTIVE) 714 return; 715 if (sc->sc_flags & WI_FLAGS_OUTRANGE) 716 return; 717 718 memset(&frmhdr, 0, sizeof(frmhdr)); 719 cur = sc->sc_txnext; 720 for (;;) { 721 IF_POLL(&ic->ic_mgtq, m0); 722 if (m0 != NULL) { 723 if (sc->sc_txd[cur].d_len != 0) { 724 ifp->if_flags |= IFF_OACTIVE; 725 break; 726 } 727 IF_DEQUEUE(&ic->ic_mgtq, m0); 728 m_copydata(m0, 4, ETHER_ADDR_LEN * 2, 729 (caddr_t)&frmhdr.wi_ehdr); 730 frmhdr.wi_ehdr.ether_type = 0; 731 wh = mtod(m0, struct ieee80211_frame *); 732 } else { 733 if (ic->ic_state != IEEE80211_S_RUN) 734 break; 735 IFQ_POLL(&ifp->if_snd, m0); 736 if (m0 == NULL) 737 break; 738 if (sc->sc_txd[cur].d_len != 0) { 739 ifp->if_flags |= IFF_OACTIVE; 740 break; 741 } 742 IFQ_DEQUEUE(&ifp->if_snd, m0); 743 ifp->if_opackets++; 744 m_copydata(m0, 0, ETHER_HDR_LEN, 745 (caddr_t)&frmhdr.wi_ehdr); 746 #if NBPFILTER > 0 747 if (ifp->if_bpf) 748 bpf_mtap(ifp->if_bpf, m0); 749 #endif 750 751 if ((m0 = ieee80211_encap(ifp, m0)) == NULL) { 752 ifp->if_oerrors++; 753 continue; 754 } 755 wh = mtod(m0, struct ieee80211_frame *); 756 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 757 !IEEE80211_IS_MULTICAST(wh->i_addr1) && 758 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 759 IEEE80211_FC0_TYPE_DATA && 760 ((ni = ieee80211_find_node(ic, wh->i_addr1)) == 761 NULL || ni->ni_associd == 0)) { 762 m_freem(m0); 763 ifp->if_oerrors++; 764 continue; 765 } 766 if (ic->ic_flags & IEEE80211_F_WEPON) 767 wh->i_fc[1] |= IEEE80211_FC1_WEP; 768 769 } 770 #if NBPFILTER > 0 771 if (ic->ic_rawbpf) 772 bpf_mtap(ic->ic_rawbpf, m0); 773 #endif 774 frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11); 775 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 776 (wh->i_fc[1] & IEEE80211_FC1_WEP)) { 777 if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) { 778 ifp->if_oerrors++; 779 continue; 780 } 781 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT); 782 } 783 m_copydata(m0, 0, sizeof(struct ieee80211_frame), 784 (caddr_t)&frmhdr.wi_whdr); 785 m_adj(m0, sizeof(struct ieee80211_frame)); 786 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len); 787 #if NBPFILTER > 0 788 if (sc->sc_drvbpf) { 789 struct mbuf mb; 790 791 M_COPY_PKTHDR(&mb, m0); 792 mb.m_data = (caddr_t)&frmhdr; 793 mb.m_len = sizeof(frmhdr); 794 mb.m_next = m0; 795 mb.m_pkthdr.len += mb.m_len; 796 bpf_mtap(sc->sc_drvbpf, &mb); 797 } 798 #endif 799 fid = sc->sc_txd[cur].d_fid; 800 wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)); 801 off = sizeof(frmhdr); 802 for (m = m0; m != NULL; m = m->m_next) { 803 if (m->m_len == 0) 804 continue; 805 wi_write_bap(sc, fid, off, m->m_data, m->m_len); 806 off += m->m_len; 807 } 808 m_freem(m0); 809 sc->sc_txd[cur].d_len = off; 810 if (sc->sc_txcur == cur) { 811 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) { 812 printf("%s: xmit failed\n", 813 sc->sc_dev.dv_xname); 814 sc->sc_txd[cur].d_len = 0; 815 continue; 816 } 817 sc->sc_tx_timer = 5; 818 ifp->if_timer = 1; 819 } 820 sc->sc_txnext = cur = (cur + 1) % WI_NTXBUF; 821 } 822 } 823 824 825 static int 826 wi_reset(struct wi_softc *sc) 827 { 828 int i, error; 829 830 DPRINTF(("wi_reset\n")); 831 error = 0; 832 for (i = 0; i < 5; i++) { 833 DELAY(20*1000); /* XXX: way too long! */ 834 if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0) 835 break; 836 } 837 if (error) { 838 printf("%s: init failed\n", sc->sc_dev.dv_xname); 839 return error; 840 } 841 CSR_WRITE_2(sc, WI_INT_EN, 0); 842 CSR_WRITE_2(sc, WI_EVENT_ACK, ~0); 843 844 /* Calibrate timer. */ 845 wi_write_val(sc, WI_RID_TICK_TIME, 0); 846 return 0; 847 } 848 849 static void 850 wi_watchdog(struct ifnet *ifp) 851 { 852 struct wi_softc *sc = ifp->if_softc; 853 854 ifp->if_timer = 0; 855 if (!sc->sc_enabled) 856 return; 857 858 if (sc->sc_tx_timer) { 859 if (--sc->sc_tx_timer == 0) { 860 printf("%s: device timeout\n", ifp->if_xname); 861 ifp->if_oerrors++; 862 wi_init(ifp); 863 return; 864 } 865 ifp->if_timer = 1; 866 } 867 868 if (sc->sc_scan_timer) { 869 if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT && 870 sc->sc_firmware_type == WI_INTERSIL) { 871 DPRINTF(("wi_watchdog: inquire scan\n")); 872 wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0); 873 } 874 if (sc->sc_scan_timer) 875 ifp->if_timer = 1; 876 } 877 878 /* TODO: rate control */ 879 ieee80211_watchdog(ifp); 880 } 881 882 static int 883 wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 884 { 885 struct wi_softc *sc = ifp->if_softc; 886 struct ieee80211com *ic = &sc->sc_ic; 887 struct ifreq *ifr = (struct ifreq *)data; 888 int s, error = 0; 889 890 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0) 891 return ENXIO; 892 893 s = splnet(); 894 895 switch (cmd) { 896 case SIOCSIFFLAGS: 897 if (ifp->if_flags & IFF_UP) { 898 if (sc->sc_enabled) { 899 /* 900 * To avoid rescanning another access point, 901 * do not call wi_init() here. Instead, 902 * only reflect promisc mode settings. 903 */ 904 if (ic->ic_opmode != IEEE80211_M_HOSTAP && 905 (ifp->if_flags & IFF_PROMISC) != 0) 906 wi_write_val(sc, WI_RID_PROMISC, 1); 907 else 908 wi_write_val(sc, WI_RID_PROMISC, 0); 909 } else 910 error = wi_init(ifp); 911 } else if (sc->sc_enabled) 912 wi_stop(ifp, 1); 913 break; 914 case SIOCSIFMEDIA: 915 case SIOCGIFMEDIA: 916 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd); 917 break; 918 case SIOCADDMULTI: 919 case SIOCDELMULTI: 920 error = (cmd == SIOCADDMULTI) ? 921 ether_addmulti(ifr, &sc->sc_ic.ic_ec) : 922 ether_delmulti(ifr, &sc->sc_ic.ic_ec); 923 if (error == ENETRESET) { 924 if (sc->sc_enabled) { 925 /* do not rescan */ 926 error = wi_write_multi(sc); 927 } else 928 error = 0; 929 } 930 break; 931 case SIOCGIFGENERIC: 932 error = wi_get_cfg(ifp, cmd, data); 933 break; 934 case SIOCSIFGENERIC: 935 error = suser(curproc->p_ucred, &curproc->p_acflag); 936 if (error) 937 break; 938 error = wi_set_cfg(ifp, cmd, data); 939 if (error == ENETRESET) { 940 if (sc->sc_enabled) 941 error = wi_init(ifp); 942 else 943 error = 0; 944 } 945 break; 946 default: 947 error = ieee80211_ioctl(ifp, cmd, data); 948 if (error == ENETRESET) { 949 if (sc->sc_enabled) 950 error = wi_init(ifp); 951 else 952 error = 0; 953 } 954 break; 955 } 956 splx(s); 957 return error; 958 } 959 960 static int 961 wi_media_change(struct ifnet *ifp) 962 { 963 struct wi_softc *sc = ifp->if_softc; 964 struct ieee80211com *ic = &sc->sc_ic; 965 struct ifmedia_entry *ime; 966 enum ieee80211_opmode newmode; 967 int i, rate, error = 0; 968 969 ime = sc->sc_media.ifm_cur; 970 if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) { 971 i = -1; 972 } else { 973 rate = ieee80211_media2rate(ime->ifm_media, IEEE80211_T_DS); 974 if (rate == 0) 975 return EINVAL; 976 for (i = 0; i < IEEE80211_RATE_SIZE; i++) { 977 if ((ic->ic_sup_rates[i] & IEEE80211_RATE_VAL) == rate) 978 break; 979 } 980 if (i == IEEE80211_RATE_SIZE) 981 return EINVAL; 982 } 983 if (ic->ic_fixed_rate != i) { 984 ic->ic_fixed_rate = i; 985 error = ENETRESET; 986 } 987 988 if ((ime->ifm_media & IFM_IEEE80211_ADHOC) && 989 (ime->ifm_media & IFM_FLAG0)) 990 newmode = IEEE80211_M_AHDEMO; 991 else if (ime->ifm_media & IFM_IEEE80211_ADHOC) 992 newmode = IEEE80211_M_IBSS; 993 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP) 994 newmode = IEEE80211_M_HOSTAP; 995 else 996 newmode = IEEE80211_M_STA; 997 if (ic->ic_opmode != newmode) { 998 ic->ic_opmode = newmode; 999 error = ENETRESET; 1000 } 1001 if (error == ENETRESET) { 1002 if (sc->sc_enabled) 1003 error = wi_init(ifp); 1004 else 1005 error = 0; 1006 } 1007 ifp->if_baudrate = ifmedia_baudrate(sc->sc_media.ifm_cur->ifm_media); 1008 1009 return error; 1010 } 1011 1012 static void 1013 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1014 { 1015 struct wi_softc *sc = ifp->if_softc; 1016 struct ieee80211com *ic = &sc->sc_ic; 1017 u_int16_t val; 1018 int rate, len; 1019 1020 if (sc->sc_enabled == 0) { 1021 imr->ifm_active = IFM_IEEE80211 | IFM_NONE; 1022 imr->ifm_status = 0; 1023 return; 1024 } 1025 1026 imr->ifm_status = IFM_AVALID; 1027 imr->ifm_active = IFM_IEEE80211; 1028 if (ic->ic_state == IEEE80211_S_RUN && 1029 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0) 1030 imr->ifm_status |= IFM_ACTIVE; 1031 len = sizeof(val); 1032 if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) != 0) 1033 rate = 0; 1034 else { 1035 /* convert to 802.11 rate */ 1036 rate = val * 2; 1037 if (sc->sc_firmware_type == WI_LUCENT) { 1038 if (rate == 10) 1039 rate = 11; /* 5.5Mbps */ 1040 } else { 1041 if (rate == 4*2) 1042 rate = 11; /* 5.5Mbps */ 1043 else if (rate == 8*2) 1044 rate = 22; /* 11Mbps */ 1045 } 1046 } 1047 imr->ifm_active |= ieee80211_rate2media(rate, IEEE80211_T_DS); 1048 switch (ic->ic_opmode) { 1049 case IEEE80211_M_STA: 1050 break; 1051 case IEEE80211_M_IBSS: 1052 imr->ifm_active |= IFM_IEEE80211_ADHOC; 1053 break; 1054 case IEEE80211_M_AHDEMO: 1055 imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0; 1056 break; 1057 case IEEE80211_M_HOSTAP: 1058 imr->ifm_active |= IFM_IEEE80211_HOSTAP; 1059 break; 1060 } 1061 } 1062 1063 static void 1064 wi_rx_intr(struct wi_softc *sc) 1065 { 1066 struct ieee80211com *ic = &sc->sc_ic; 1067 struct ifnet *ifp = &ic->ic_if; 1068 struct wi_frame frmhdr; 1069 struct mbuf *m; 1070 struct ieee80211_frame *wh; 1071 int fid, len, off, rssi; 1072 u_int16_t status; 1073 u_int32_t rstamp; 1074 1075 fid = CSR_READ_2(sc, WI_RX_FID); 1076 1077 /* First read in the frame header */ 1078 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) { 1079 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1080 ifp->if_ierrors++; 1081 DPRINTF(("wi_rx_intr: read fid %x failed\n", fid)); 1082 return; 1083 } 1084 1085 /* 1086 * Drop undecryptable or packets with receive errors here 1087 */ 1088 status = le16toh(frmhdr.wi_status); 1089 if (status & WI_STAT_ERRSTAT) { 1090 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1091 ifp->if_ierrors++; 1092 DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status)); 1093 return; 1094 } 1095 rssi = frmhdr.wi_rx_signal; 1096 rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) | 1097 le16toh(frmhdr.wi_rx_tstamp1); 1098 1099 len = le16toh(frmhdr.wi_dat_len); 1100 off = ALIGN(sizeof(struct ieee80211_frame)); 1101 1102 MGETHDR(m, M_DONTWAIT, MT_DATA); 1103 if (m == NULL) { 1104 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1105 ifp->if_ierrors++; 1106 DPRINTF(("wi_rx_intr: MGET failed\n")); 1107 return; 1108 } 1109 if (off + len > MHLEN) { 1110 MCLGET(m, M_DONTWAIT); 1111 if ((m->m_flags & M_EXT) == 0) { 1112 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1113 m_freem(m); 1114 ifp->if_ierrors++; 1115 DPRINTF(("wi_rx_intr: MCLGET failed\n")); 1116 return; 1117 } 1118 } 1119 1120 m->m_data += off - sizeof(struct ieee80211_frame); 1121 memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame)); 1122 wi_read_bap(sc, fid, sizeof(frmhdr), 1123 m->m_data + sizeof(struct ieee80211_frame), len); 1124 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len; 1125 m->m_pkthdr.rcvif = ifp; 1126 1127 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX); 1128 1129 #if NBPFILTER > 0 1130 if (sc->sc_drvbpf) { 1131 struct mbuf mb; 1132 1133 M_COPY_PKTHDR(&mb, m); 1134 mb.m_data = (caddr_t)&frmhdr; 1135 mb.m_len = sizeof(frmhdr); 1136 mb.m_next = m; 1137 mb.m_pkthdr.len += mb.m_len; 1138 bpf_mtap(sc->sc_drvbpf, &mb); 1139 } 1140 #endif 1141 wh = mtod(m, struct ieee80211_frame *); 1142 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1143 /* 1144 * WEP is decrypted by hardware. Clear WEP bit 1145 * header for ieee80211_input(). 1146 */ 1147 wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 1148 } 1149 ieee80211_input(ifp, m, rssi, rstamp); 1150 } 1151 1152 static void 1153 wi_tx_intr(struct wi_softc *sc) 1154 { 1155 struct ieee80211com *ic = &sc->sc_ic; 1156 struct ifnet *ifp = &ic->ic_if; 1157 int fid, cur; 1158 1159 fid = CSR_READ_2(sc, WI_ALLOC_FID); 1160 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC); 1161 1162 cur = sc->sc_txcur; 1163 if (sc->sc_txd[cur].d_fid != fid) { 1164 printf("%s: bad alloc %x != %x, cur %d nxt %d\n", 1165 sc->sc_dev.dv_xname, fid, sc->sc_txd[cur].d_fid, cur, 1166 sc->sc_txnext); 1167 return; 1168 } 1169 sc->sc_tx_timer = 0; 1170 sc->sc_txd[cur].d_len = 0; 1171 sc->sc_txcur = cur = (cur + 1) % WI_NTXBUF; 1172 if (sc->sc_txd[cur].d_len == 0) 1173 ifp->if_flags &= ~IFF_OACTIVE; 1174 else { 1175 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid, 1176 0, 0)) { 1177 printf("%s: xmit failed\n", sc->sc_dev.dv_xname); 1178 sc->sc_txd[cur].d_len = 0; 1179 } else { 1180 sc->sc_tx_timer = 5; 1181 ifp->if_timer = 1; 1182 } 1183 } 1184 } 1185 1186 static void 1187 wi_info_intr(struct wi_softc *sc) 1188 { 1189 struct ieee80211com *ic = &sc->sc_ic; 1190 struct ifnet *ifp = &ic->ic_if; 1191 int i, fid, len, off; 1192 u_int16_t ltbuf[2]; 1193 u_int16_t stat; 1194 u_int32_t *ptr; 1195 1196 fid = CSR_READ_2(sc, WI_INFO_FID); 1197 wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf)); 1198 1199 switch (le16toh(ltbuf[1])) { 1200 1201 case WI_INFO_LINK_STAT: 1202 wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat)); 1203 DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat))); 1204 switch (le16toh(stat)) { 1205 case CONNECTED: 1206 sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 1207 if (ic->ic_state == IEEE80211_S_RUN && 1208 ic->ic_opmode != IEEE80211_M_IBSS) 1209 break; 1210 /* FALLTHROUGH */ 1211 case AP_CHANGE: 1212 ieee80211_new_state(ifp, IEEE80211_S_RUN, -1); 1213 break; 1214 case AP_IN_RANGE: 1215 sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 1216 break; 1217 case AP_OUT_OF_RANGE: 1218 if (sc->sc_firmware_type == WI_SYMBOL && 1219 sc->sc_scan_timer > 0) { 1220 if (wi_cmd(sc, WI_CMD_INQUIRE, 1221 WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0) 1222 sc->sc_scan_timer = 0; 1223 break; 1224 } 1225 if (ic->ic_opmode == IEEE80211_M_STA) 1226 sc->sc_flags |= WI_FLAGS_OUTRANGE; 1227 break; 1228 case DISCONNECTED: 1229 case ASSOC_FAILED: 1230 if (ic->ic_opmode == IEEE80211_M_STA) 1231 ieee80211_new_state(ifp, IEEE80211_S_INIT, -1); 1232 break; 1233 } 1234 break; 1235 1236 case WI_INFO_COUNTERS: 1237 /* some card versions have a larger stats structure */ 1238 len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4); 1239 ptr = (u_int32_t *)&sc->sc_stats; 1240 off = sizeof(ltbuf); 1241 for (i = 0; i < len; i++, off += 2, ptr++) { 1242 wi_read_bap(sc, fid, off, &stat, sizeof(stat)); 1243 #ifdef WI_HERMES_STATS_WAR 1244 if (stat & 0xf000) 1245 stat = ~stat; 1246 #endif 1247 *ptr += stat; 1248 } 1249 ifp->if_collisions = sc->sc_stats.wi_tx_single_retries + 1250 sc->sc_stats.wi_tx_multi_retries + 1251 sc->sc_stats.wi_tx_retry_limit; 1252 break; 1253 1254 case WI_INFO_SCAN_RESULTS: 1255 case WI_INFO_HOST_SCAN_RESULTS: 1256 wi_scan_result(sc, fid, le16toh(ltbuf[0])); 1257 break; 1258 1259 default: 1260 DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid, 1261 le16toh(ltbuf[1]), le16toh(ltbuf[0]))); 1262 break; 1263 } 1264 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO); 1265 } 1266 1267 /* 1268 * Allocate a region of memory inside the NIC and zero 1269 * it out. 1270 */ 1271 static int 1272 wi_write_multi(struct wi_softc *sc) 1273 { 1274 struct ifnet *ifp = &sc->sc_ic.ic_if; 1275 int n = 0; 1276 struct wi_mcast mlist; 1277 struct ether_multi *enm; 1278 struct ether_multistep estep; 1279 1280 if ((ifp->if_flags & IFF_PROMISC) != 0) { 1281 allmulti: 1282 ifp->if_flags |= IFF_ALLMULTI; 1283 memset(&mlist, 0, sizeof(mlist)); 1284 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist, 1285 sizeof(mlist)); 1286 } 1287 1288 n = 0; 1289 ETHER_FIRST_MULTI(estep, &sc->sc_ic.ic_ec, enm); 1290 while (enm != NULL) { 1291 /* Punt on ranges or too many multicast addresses. */ 1292 if (!IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi) || 1293 n >= sizeof(mlist) / sizeof(mlist.wi_mcast[0])) 1294 goto allmulti; 1295 1296 IEEE80211_ADDR_COPY(&mlist.wi_mcast[n], enm->enm_addrlo); 1297 n++; 1298 ETHER_NEXT_MULTI(estep, enm); 1299 } 1300 ifp->if_flags &= ~IFF_ALLMULTI; 1301 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist, 1302 IEEE80211_ADDR_LEN * n); 1303 } 1304 1305 1306 static void 1307 wi_read_nicid(sc) 1308 struct wi_softc *sc; 1309 { 1310 struct wi_card_ident *id; 1311 char *p; 1312 int len; 1313 u_int16_t ver[4]; 1314 1315 /* getting chip identity */ 1316 memset(ver, 0, sizeof(ver)); 1317 len = sizeof(ver); 1318 wi_read_rid(sc, WI_RID_CARD_ID, ver, &len); 1319 printf("%s: using ", sc->sc_dev.dv_xname); 1320 DPRINTF2(("wi_read_nicid: CARD_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3]))); 1321 1322 sc->sc_firmware_type = WI_NOTYPE; 1323 for (id = wi_card_ident; id->card_name != NULL; id++) { 1324 if (le16toh(ver[0]) == id->card_id) { 1325 printf("%s", id->card_name); 1326 sc->sc_firmware_type = id->firm_type; 1327 break; 1328 } 1329 } 1330 if (sc->sc_firmware_type == WI_NOTYPE) { 1331 if (le16toh(ver[0]) & 0x8000) { 1332 printf("Unknown PRISM2 chip"); 1333 sc->sc_firmware_type = WI_INTERSIL; 1334 } else { 1335 printf("Unknown Lucent chip"); 1336 sc->sc_firmware_type = WI_LUCENT; 1337 } 1338 } 1339 1340 /* get primary firmware version (Only Prism chips) */ 1341 if (sc->sc_firmware_type != WI_LUCENT) { 1342 memset(ver, 0, sizeof(ver)); 1343 len = sizeof(ver); 1344 wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len); 1345 sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 + 1346 le16toh(ver[3]) * 100 + le16toh(ver[1]); 1347 DPRINTF2(("wi_read_nicid: PRI_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3]))); 1348 } 1349 1350 /* get station firmware version */ 1351 memset(ver, 0, sizeof(ver)); 1352 len = sizeof(ver); 1353 wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len); 1354 sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 + 1355 le16toh(ver[3]) * 100 + le16toh(ver[1]); 1356 DPRINTF2(("wi_read_nicid: STA_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3]))); 1357 if (sc->sc_firmware_type == WI_INTERSIL && 1358 (sc->sc_sta_firmware_ver == 10102 || 1359 sc->sc_sta_firmware_ver == 20102)) { 1360 char ident[12]; 1361 memset(ident, 0, sizeof(ident)); 1362 len = sizeof(ident); 1363 /* value should be the format like "V2.00-11" */ 1364 if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 && 1365 *(p = (char *)ident) >= 'A' && 1366 p[2] == '.' && p[5] == '-' && p[8] == '\0') { 1367 sc->sc_firmware_type = WI_SYMBOL; 1368 sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 + 1369 (p[3] - '0') * 1000 + (p[4] - '0') * 100 + 1370 (p[6] - '0') * 10 + (p[7] - '0'); 1371 } 1372 DPRINTF2(("wi_read_nicid: SYMBOL_ID: %x %x %x %x\n", le16toh(ident[0]), le16toh(ident[1]), le16toh(ident[2]), le16toh(ident[3]))); 1373 } 1374 1375 printf("\n%s: %s Firmware: ", sc->sc_dev.dv_xname, 1376 sc->sc_firmware_type == WI_LUCENT ? "Lucent" : 1377 (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil")); 1378 if (sc->sc_firmware_type != WI_LUCENT) /* XXX */ 1379 printf("Primary (%u.%u.%u), ", 1380 sc->sc_pri_firmware_ver / 10000, 1381 (sc->sc_pri_firmware_ver % 10000) / 100, 1382 sc->sc_pri_firmware_ver % 100); 1383 printf("Station (%u.%u.%u)\n", 1384 sc->sc_sta_firmware_ver / 10000, 1385 (sc->sc_sta_firmware_ver % 10000) / 100, 1386 sc->sc_sta_firmware_ver % 100); 1387 } 1388 1389 static int 1390 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen) 1391 { 1392 struct wi_ssid ssid; 1393 1394 if (buflen > IEEE80211_NWID_LEN) 1395 return ENOBUFS; 1396 memset(&ssid, 0, sizeof(ssid)); 1397 ssid.wi_len = htole16(buflen); 1398 memcpy(ssid.wi_ssid, buf, buflen); 1399 return wi_write_rid(sc, rid, &ssid, sizeof(ssid)); 1400 } 1401 1402 static int 1403 wi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data) 1404 { 1405 struct wi_softc *sc = ifp->if_softc; 1406 struct ieee80211com *ic = &sc->sc_ic; 1407 struct ifreq *ifr = (struct ifreq *)data; 1408 struct wi_req wreq; 1409 int len, n, error; 1410 1411 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 1412 if (error) 1413 return error; 1414 len = (wreq.wi_len - 1) * 2; 1415 if (len < sizeof(u_int16_t)) 1416 return ENOSPC; 1417 if (len > sizeof(wreq.wi_val)) 1418 len = sizeof(wreq.wi_val); 1419 1420 switch (wreq.wi_type) { 1421 1422 case WI_RID_IFACE_STATS: 1423 memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats)); 1424 if (len < sizeof(sc->sc_stats)) 1425 error = ENOSPC; 1426 else 1427 len = sizeof(sc->sc_stats); 1428 break; 1429 1430 case WI_RID_ENCRYPTION: 1431 case WI_RID_TX_CRYPT_KEY: 1432 case WI_RID_DEFLT_CRYPT_KEYS: 1433 case WI_RID_TX_RATE: 1434 return ieee80211_cfgget(ifp, cmd, data); 1435 1436 case WI_RID_MICROWAVE_OVEN: 1437 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) { 1438 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1439 &len); 1440 break; 1441 } 1442 wreq.wi_val[0] = htole16(sc->sc_microwave_oven); 1443 len = sizeof(u_int16_t); 1444 break; 1445 1446 case WI_RID_ROAMING_MODE: 1447 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) { 1448 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1449 &len); 1450 break; 1451 } 1452 wreq.wi_val[0] = htole16(sc->sc_roaming_mode); 1453 len = sizeof(u_int16_t); 1454 break; 1455 1456 case WI_RID_SYSTEM_SCALE: 1457 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) { 1458 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1459 &len); 1460 break; 1461 } 1462 wreq.wi_val[0] = htole16(sc->sc_system_scale); 1463 len = sizeof(u_int16_t); 1464 break; 1465 1466 case WI_RID_FRAG_THRESH: 1467 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) { 1468 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1469 &len); 1470 break; 1471 } 1472 wreq.wi_val[0] = htole16(sc->sc_frag_thresh); 1473 len = sizeof(u_int16_t); 1474 break; 1475 1476 case WI_RID_READ_APS: 1477 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1478 return ieee80211_cfgget(ifp, cmd, data); 1479 if (sc->sc_scan_timer > 0) { 1480 error = EINPROGRESS; 1481 break; 1482 } 1483 n = sc->sc_naps; 1484 if (len < sizeof(n)) { 1485 error = ENOSPC; 1486 break; 1487 } 1488 if (len < sizeof(n) + sizeof(struct wi_apinfo) * n) 1489 n = (len - sizeof(n)) / sizeof(struct wi_apinfo); 1490 len = sizeof(n) + sizeof(struct wi_apinfo) * n; 1491 memcpy(wreq.wi_val, &n, sizeof(n)); 1492 memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps, 1493 sizeof(struct wi_apinfo) * n); 1494 break; 1495 1496 default: 1497 if (sc->sc_enabled) { 1498 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val, 1499 &len); 1500 break; 1501 } 1502 switch (wreq.wi_type) { 1503 case WI_RID_MAX_DATALEN: 1504 wreq.wi_val[0] = htole16(sc->sc_max_datalen); 1505 len = sizeof(u_int16_t); 1506 break; 1507 case WI_RID_RTS_THRESH: 1508 wreq.wi_val[0] = htole16(sc->sc_rts_thresh); 1509 len = sizeof(u_int16_t); 1510 break; 1511 case WI_RID_CNFAUTHMODE: 1512 wreq.wi_val[0] = htole16(sc->sc_cnfauthmode); 1513 len = sizeof(u_int16_t); 1514 break; 1515 case WI_RID_NODENAME: 1516 if (len < sc->sc_nodelen + sizeof(u_int16_t)) { 1517 error = ENOSPC; 1518 break; 1519 } 1520 len = sc->sc_nodelen + sizeof(u_int16_t); 1521 wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2); 1522 memcpy(&wreq.wi_val[1], sc->sc_nodename, 1523 sc->sc_nodelen); 1524 break; 1525 default: 1526 return ieee80211_cfgget(ifp, cmd, data); 1527 } 1528 break; 1529 } 1530 if (error) 1531 return error; 1532 wreq.wi_len = (len + 1) / 2 + 1; 1533 return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2); 1534 } 1535 1536 static int 1537 wi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data) 1538 { 1539 struct wi_softc *sc = ifp->if_softc; 1540 struct ieee80211com *ic = &sc->sc_ic; 1541 struct ifreq *ifr = (struct ifreq *)data; 1542 struct wi_req wreq; 1543 struct mbuf *m; 1544 int i, len, error; 1545 1546 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq)); 1547 if (error) 1548 return error; 1549 len = (wreq.wi_len - 1) * 2; 1550 switch (wreq.wi_type) { 1551 case WI_RID_NODENAME: 1552 if (le16toh(wreq.wi_val[0]) * 2 > len || 1553 le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) { 1554 error = ENOSPC; 1555 break; 1556 } 1557 if (sc->sc_enabled) { 1558 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val, 1559 len); 1560 if (error) 1561 break; 1562 } 1563 sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2; 1564 memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen); 1565 break; 1566 1567 case WI_RID_MICROWAVE_OVEN: 1568 case WI_RID_ROAMING_MODE: 1569 case WI_RID_SYSTEM_SCALE: 1570 case WI_RID_FRAG_THRESH: 1571 if (wreq.wi_type == WI_RID_MICROWAVE_OVEN && 1572 (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0) 1573 break; 1574 if (wreq.wi_type == WI_RID_ROAMING_MODE && 1575 (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0) 1576 break; 1577 if (wreq.wi_type == WI_RID_SYSTEM_SCALE && 1578 (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0) 1579 break; 1580 if (wreq.wi_type == WI_RID_FRAG_THRESH && 1581 (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0) 1582 break; 1583 /* FALLTHROUGH */ 1584 case WI_RID_RTS_THRESH: 1585 case WI_RID_CNFAUTHMODE: 1586 case WI_RID_MAX_DATALEN: 1587 if (sc->sc_enabled) { 1588 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val, 1589 sizeof(u_int16_t)); 1590 if (error) 1591 break; 1592 } 1593 switch (wreq.wi_type) { 1594 case WI_RID_FRAG_THRESH: 1595 sc->sc_frag_thresh = le16toh(wreq.wi_val[0]); 1596 break; 1597 case WI_RID_RTS_THRESH: 1598 sc->sc_rts_thresh = le16toh(wreq.wi_val[0]); 1599 break; 1600 case WI_RID_MICROWAVE_OVEN: 1601 sc->sc_microwave_oven = le16toh(wreq.wi_val[0]); 1602 break; 1603 case WI_RID_ROAMING_MODE: 1604 sc->sc_roaming_mode = le16toh(wreq.wi_val[0]); 1605 break; 1606 case WI_RID_SYSTEM_SCALE: 1607 sc->sc_system_scale = le16toh(wreq.wi_val[0]); 1608 break; 1609 case WI_RID_CNFAUTHMODE: 1610 sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]); 1611 break; 1612 case WI_RID_MAX_DATALEN: 1613 sc->sc_max_datalen = le16toh(wreq.wi_val[0]); 1614 break; 1615 } 1616 break; 1617 1618 case WI_RID_TX_RATE: 1619 switch (le16toh(wreq.wi_val[0])) { 1620 case 3: 1621 ic->ic_fixed_rate = -1; 1622 break; 1623 default: 1624 for (i = 0; i < IEEE80211_RATE_SIZE; i++) { 1625 if ((ic->ic_sup_rates[i] & IEEE80211_RATE_VAL) 1626 / 2 == le16toh(wreq.wi_val[0])) 1627 break; 1628 } 1629 if (i == IEEE80211_RATE_SIZE) 1630 return EINVAL; 1631 ic->ic_fixed_rate = i; 1632 } 1633 if (sc->sc_enabled) 1634 error = wi_write_txrate(sc); 1635 break; 1636 1637 case WI_RID_SCAN_APS: 1638 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP) 1639 error = wi_scan_ap(sc); 1640 break; 1641 1642 case WI_RID_MGMT_XMIT: 1643 if (!sc->sc_enabled) { 1644 error = ENETDOWN; 1645 break; 1646 } 1647 if (ic->ic_mgtq.ifq_len > 5) { 1648 error = EAGAIN; 1649 break; 1650 } 1651 /* XXX wi_len looks in u_int8_t, not in u_int16_t */ 1652 m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL); 1653 if (m == NULL) { 1654 error = ENOMEM; 1655 break; 1656 } 1657 IF_ENQUEUE(&ic->ic_mgtq, m); 1658 break; 1659 1660 default: 1661 if (sc->sc_enabled) { 1662 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val, 1663 len); 1664 if (error) 1665 break; 1666 } 1667 error = ieee80211_cfgset(ifp, cmd, data); 1668 break; 1669 } 1670 return error; 1671 } 1672 1673 static int 1674 wi_write_txrate(struct wi_softc *sc) 1675 { 1676 struct ieee80211com *ic = &sc->sc_ic; 1677 int i; 1678 u_int16_t rate; 1679 1680 if (ic->ic_fixed_rate < 0) 1681 rate = 0; /* auto */ 1682 else 1683 rate = (ic->ic_sup_rates[ic->ic_fixed_rate] & 1684 IEEE80211_RATE_VAL) / 2; 1685 1686 /* rate: 0, 1, 2, 5, 11 */ 1687 1688 switch (sc->sc_firmware_type) { 1689 case WI_LUCENT: 1690 if (rate == 0) 1691 rate = 3; /* auto */ 1692 break; 1693 default: 1694 /* Choose a bit according to this table. 1695 * 1696 * bit | data rate 1697 * ----+------------------- 1698 * 0 | 1Mbps 1699 * 1 | 2Mbps 1700 * 2 | 5.5Mbps 1701 * 3 | 11Mbps 1702 */ 1703 for (i = 8; i > 0; i >>= 1) { 1704 if (rate >= i) 1705 break; 1706 } 1707 if (i == 0) 1708 rate = 0xf; /* auto */ 1709 else 1710 rate = i; 1711 break; 1712 } 1713 return wi_write_val(sc, WI_RID_TX_RATE, rate); 1714 } 1715 1716 static int 1717 wi_write_wep(struct wi_softc *sc) 1718 { 1719 struct ieee80211com *ic = &sc->sc_ic; 1720 int error = 0; 1721 int i, keylen; 1722 u_int16_t val; 1723 struct wi_key wkey[IEEE80211_WEP_NKID]; 1724 1725 switch (sc->sc_firmware_type) { 1726 case WI_LUCENT: 1727 val = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0; 1728 error = wi_write_val(sc, WI_RID_ENCRYPTION, val); 1729 if (error) 1730 break; 1731 error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_wep_txkey); 1732 if (error) 1733 break; 1734 memset(wkey, 0, sizeof(wkey)); 1735 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 1736 keylen = ic->ic_nw_keys[i].wk_len; 1737 wkey[i].wi_keylen = htole16(keylen); 1738 memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key, 1739 keylen); 1740 } 1741 error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS, 1742 wkey, sizeof(wkey)); 1743 break; 1744 1745 case WI_INTERSIL: 1746 case WI_SYMBOL: 1747 if (ic->ic_flags & IEEE80211_F_WEPON) { 1748 /* 1749 * ONLY HWB3163 EVAL-CARD Firmware version 1750 * less than 0.8 variant2 1751 * 1752 * If promiscuous mode disable, Prism2 chip 1753 * does not work with WEP . 1754 * It is under investigation for details. 1755 * (ichiro@netbsd.org) 1756 */ 1757 if (sc->sc_firmware_type == WI_INTERSIL && 1758 sc->sc_sta_firmware_ver < 802 ) { 1759 /* firm ver < 0.8 variant 2 */ 1760 wi_write_val(sc, WI_RID_PROMISC, 1); 1761 } 1762 wi_write_val(sc, WI_RID_CNFAUTHMODE, 1763 sc->sc_cnfauthmode); 1764 val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED; 1765 /* 1766 * Encryption firmware has a bug for HostAP mode. 1767 */ 1768 if (sc->sc_firmware_type == WI_INTERSIL && 1769 ic->ic_opmode == IEEE80211_M_HOSTAP) 1770 val |= HOST_ENCRYPT; 1771 } else { 1772 wi_write_val(sc, WI_RID_CNFAUTHMODE, 1773 IEEE80211_AUTH_OPEN); 1774 val = HOST_ENCRYPT | HOST_DECRYPT; 1775 } 1776 error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val); 1777 if (error) 1778 break; 1779 error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY, 1780 ic->ic_wep_txkey); 1781 if (error) 1782 break; 1783 /* 1784 * It seems that the firmware accept 104bit key only if 1785 * all the keys have 104bit length. We get the length of 1786 * the transmit key and use it for all other keys. 1787 * Perhaps we should use software WEP for such situation. 1788 */ 1789 keylen = ic->ic_nw_keys[ic->ic_wep_txkey].wk_len; 1790 if (keylen > IEEE80211_WEP_KEYLEN) 1791 keylen = 13; /* 104bit keys */ 1792 else 1793 keylen = IEEE80211_WEP_KEYLEN; 1794 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 1795 error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i, 1796 ic->ic_nw_keys[i].wk_key, keylen); 1797 if (error) 1798 break; 1799 } 1800 break; 1801 } 1802 return error; 1803 } 1804 1805 /* Must be called at proper protection level! */ 1806 static int 1807 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2) 1808 { 1809 int i, status; 1810 1811 /* wait for the busy bit to clear */ 1812 for (i = 0; ; i++) { 1813 if ((CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY) == 0) 1814 break; 1815 if (i == WI_TIMEOUT) { 1816 printf("%s: wi_cmd: BUSY did not clear, " 1817 "cmd=0x%x, prev=0x%x\n", sc->sc_dev.dv_xname, 1818 cmd, CSR_READ_2(sc, WI_COMMAND)); 1819 return EIO; 1820 } 1821 DELAY(1); 1822 } 1823 1824 CSR_WRITE_2(sc, WI_PARAM0, val0); 1825 CSR_WRITE_2(sc, WI_PARAM1, val1); 1826 CSR_WRITE_2(sc, WI_PARAM2, val2); 1827 CSR_WRITE_2(sc, WI_COMMAND, cmd); 1828 1829 if (cmd == WI_CMD_INI) { 1830 /* XXX: should sleep here. */ 1831 DELAY(100*1000); 1832 } 1833 /* wait for the cmd completed bit */ 1834 for (i = 0; i < WI_TIMEOUT; i++) { 1835 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD) 1836 break; 1837 DELAY(1); 1838 } 1839 1840 status = CSR_READ_2(sc, WI_STATUS); 1841 1842 /* Ack the command */ 1843 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD); 1844 1845 if (i == WI_TIMEOUT) { 1846 printf("%s: command timed out, cmd=0x%x, arg=0x%x\n", 1847 sc->sc_dev.dv_xname, cmd, val0); 1848 return ETIMEDOUT; 1849 } 1850 1851 if (status & WI_STAT_CMD_RESULT) { 1852 printf("%s: command failed, cmd=0x%x, arg=0x%x\n", 1853 sc->sc_dev.dv_xname, cmd, val0); 1854 return EIO; 1855 } 1856 return 0; 1857 } 1858 1859 static int 1860 wi_seek_bap(struct wi_softc *sc, int id, int off) 1861 { 1862 int i, status; 1863 1864 CSR_WRITE_2(sc, WI_SEL0, id); 1865 CSR_WRITE_2(sc, WI_OFF0, off); 1866 1867 for (i = 0; ; i++) { 1868 status = CSR_READ_2(sc, WI_OFF0); 1869 if ((status & WI_OFF_BUSY) == 0) 1870 break; 1871 if (i == WI_TIMEOUT) { 1872 printf("%s: timeout in wi_seek to %x/%x\n", 1873 sc->sc_dev.dv_xname, id, off); 1874 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 1875 return ETIMEDOUT; 1876 } 1877 DELAY(1); 1878 } 1879 if (status & WI_OFF_ERR) { 1880 printf("%s: failed in wi_seek to %x/%x\n", 1881 sc->sc_dev.dv_xname, id, off); 1882 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 1883 return EIO; 1884 } 1885 sc->sc_bap_id = id; 1886 sc->sc_bap_off = off; 1887 return 0; 1888 } 1889 1890 static int 1891 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen) 1892 { 1893 int error, cnt; 1894 1895 if (buflen == 0) 1896 return 0; 1897 if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 1898 if ((error = wi_seek_bap(sc, id, off)) != 0) 1899 return error; 1900 } 1901 cnt = (buflen + 1) / 2; 1902 CSR_READ_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt); 1903 sc->sc_bap_off += cnt * 2; 1904 return 0; 1905 } 1906 1907 static int 1908 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen) 1909 { 1910 int error, cnt; 1911 1912 if (buflen == 0) 1913 return 0; 1914 1915 #ifdef WI_HERMES_AUTOINC_WAR 1916 again: 1917 #endif 1918 if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 1919 if ((error = wi_seek_bap(sc, id, off)) != 0) 1920 return error; 1921 } 1922 cnt = (buflen + 1) / 2; 1923 CSR_WRITE_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt); 1924 sc->sc_bap_off += cnt * 2; 1925 1926 #ifdef WI_HERMES_AUTOINC_WAR 1927 /* 1928 * According to the comments in the HCF Light code, there is a bug 1929 * in the Hermes (or possibly in certain Hermes firmware revisions) 1930 * where the chip's internal autoincrement counter gets thrown off 1931 * during data writes: the autoincrement is missed, causing one 1932 * data word to be overwritten and subsequent words to be written to 1933 * the wrong memory locations. The end result is that we could end 1934 * up transmitting bogus frames without realizing it. The workaround 1935 * for this is to write a couple of extra guard words after the end 1936 * of the transfer, then attempt to read then back. If we fail to 1937 * locate the guard words where we expect them, we preform the 1938 * transfer over again. 1939 */ 1940 if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) { 1941 CSR_WRITE_2(sc, WI_DATA0, 0x1234); 1942 CSR_WRITE_2(sc, WI_DATA0, 0x5678); 1943 wi_seek_bap(sc, id, sc->sc_bap_off); 1944 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */ 1945 if (CSR_READ_2(sc, WI_DATA0) != 0x1234 || 1946 CSR_READ_2(sc, WI_DATA0) != 0x5678) { 1947 printf("%s: detect auto increment bug, try again\n", 1948 sc->sc_dev.dv_xname); 1949 goto again; 1950 } 1951 } 1952 #endif 1953 return 0; 1954 } 1955 1956 static int 1957 wi_alloc_fid(struct wi_softc *sc, int len, int *idp) 1958 { 1959 int i; 1960 1961 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) { 1962 printf("%s: failed to allocate %d bytes on NIC\n", 1963 sc->sc_dev.dv_xname, len); 1964 return ENOMEM; 1965 } 1966 1967 for (i = 0; i < WI_TIMEOUT; i++) { 1968 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC) 1969 break; 1970 if (i == WI_TIMEOUT) { 1971 printf("%s: timeout in alloc\n", sc->sc_dev.dv_xname); 1972 return ETIMEDOUT; 1973 } 1974 DELAY(1); 1975 } 1976 *idp = CSR_READ_2(sc, WI_ALLOC_FID); 1977 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC); 1978 return 0; 1979 } 1980 1981 static int 1982 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp) 1983 { 1984 int error, len; 1985 u_int16_t ltbuf[2]; 1986 1987 /* Tell the NIC to enter record read mode. */ 1988 error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0); 1989 if (error) 1990 return error; 1991 1992 error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf)); 1993 if (error) 1994 return error; 1995 1996 if (le16toh(ltbuf[1]) != rid) { 1997 printf("%s: record read mismatch, rid=%x, got=%x\n", 1998 sc->sc_dev.dv_xname, rid, le16toh(ltbuf[1])); 1999 return EIO; 2000 } 2001 len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */ 2002 if (*buflenp < len) { 2003 printf("%s: record buffer is too small, " 2004 "rid=%x, size=%d, len=%d\n", 2005 sc->sc_dev.dv_xname, rid, *buflenp, len); 2006 return ENOSPC; 2007 } 2008 *buflenp = len; 2009 return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len); 2010 } 2011 2012 static int 2013 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen) 2014 { 2015 int error; 2016 u_int16_t ltbuf[2]; 2017 2018 ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */ 2019 ltbuf[1] = htole16(rid); 2020 2021 error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf)); 2022 if (error) 2023 return error; 2024 error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen); 2025 if (error) 2026 return error; 2027 2028 return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0); 2029 } 2030 2031 static int 2032 wi_newstate(void *arg, enum ieee80211_state nstate) 2033 { 2034 struct wi_softc *sc = arg; 2035 struct ieee80211com *ic = &sc->sc_ic; 2036 struct ieee80211_node *ni = &ic->ic_bss; 2037 int i, buflen; 2038 u_int16_t val; 2039 struct wi_ssid ssid; 2040 enum ieee80211_state ostate; 2041 #ifdef WI_DEBUG 2042 static const char *stname[] = 2043 { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" }; 2044 #endif /* WI_DEBUG */ 2045 2046 ostate = ic->ic_state; 2047 DPRINTF(("wi_newstate: %s -> %s\n", stname[ostate], stname[nstate])); 2048 2049 ic->ic_state = nstate; 2050 switch (nstate) { 2051 case IEEE80211_S_INIT: 2052 ic->ic_flags &= ~IEEE80211_F_SIBSS; 2053 sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 2054 return 0; 2055 2056 case IEEE80211_S_RUN: 2057 sc->sc_flags &= ~WI_FLAGS_OUTRANGE; 2058 buflen = IEEE80211_ADDR_LEN; 2059 wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen); 2060 IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid); 2061 buflen = sizeof(val); 2062 wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen); 2063 ni->ni_chan = le16toh(val); 2064 2065 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 2066 ni->ni_esslen = ic->ic_des_esslen; 2067 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen); 2068 ni->ni_nrate = 0; 2069 for (i = 0; i < IEEE80211_RATE_SIZE; i++) { 2070 if (ic->ic_sup_rates[i]) 2071 ni->ni_rates[ni->ni_nrate++] = 2072 ic->ic_sup_rates[i]; 2073 } 2074 ni->ni_intval = ic->ic_lintval; 2075 ni->ni_capinfo = IEEE80211_CAPINFO_ESS; 2076 if (ic->ic_flags & IEEE80211_F_WEPON) 2077 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY; 2078 } else { 2079 buflen = sizeof(ssid); 2080 wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen); 2081 ni->ni_esslen = le16toh(ssid.wi_len); 2082 if (ni->ni_esslen > IEEE80211_NWID_LEN) 2083 ni->ni_esslen = IEEE80211_NWID_LEN; /*XXX*/ 2084 memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen); 2085 } 2086 break; 2087 2088 case IEEE80211_S_SCAN: 2089 case IEEE80211_S_AUTH: 2090 case IEEE80211_S_ASSOC: 2091 break; 2092 } 2093 2094 /* skip standard ieee80211 handling */ 2095 return EINPROGRESS; 2096 } 2097 2098 static int 2099 wi_scan_ap(struct wi_softc *sc) 2100 { 2101 int error = 0; 2102 u_int16_t val[2]; 2103 2104 if (!sc->sc_enabled) 2105 return ENXIO; 2106 switch (sc->sc_firmware_type) { 2107 case WI_LUCENT: 2108 (void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0); 2109 break; 2110 case WI_INTERSIL: 2111 val[0] = 0x3fff; /* channel */ 2112 val[1] = 0x000f; /* tx rate */ 2113 error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val)); 2114 break; 2115 case WI_SYMBOL: 2116 /* 2117 * XXX only supported on 3.x ? 2118 */ 2119 val[0] = BSCAN_BCAST | BSCAN_ONETIME; 2120 error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ, 2121 val, sizeof(val[0])); 2122 break; 2123 } 2124 if (error == 0) { 2125 sc->sc_scan_timer = WI_SCAN_WAIT; 2126 sc->sc_ic.ic_if.if_timer = 1; 2127 DPRINTF(("wi_scan_ap: start scanning\n")); 2128 } 2129 return error; 2130 } 2131 2132 static void 2133 wi_scan_result(struct wi_softc *sc, int fid, int cnt) 2134 { 2135 int i, naps, off, szbuf; 2136 struct wi_scan_header ws_hdr; /* Prism2 header */ 2137 struct wi_scan_data_p2 ws_dat; /* Prism2 scantable*/ 2138 struct wi_apinfo *ap; 2139 2140 off = sizeof(u_int16_t) * 2; 2141 memset(&ws_hdr, 0, sizeof(ws_hdr)); 2142 switch (sc->sc_firmware_type) { 2143 case WI_INTERSIL: 2144 wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr)); 2145 off += sizeof(ws_hdr); 2146 szbuf = sizeof(struct wi_scan_data_p2); 2147 break; 2148 case WI_SYMBOL: 2149 szbuf = sizeof(struct wi_scan_data_p2) + 6; 2150 break; 2151 case WI_LUCENT: 2152 szbuf = sizeof(struct wi_scan_data); 2153 break; 2154 } 2155 naps = (cnt * 2 + 2 - off) / szbuf; 2156 if (naps > MAXAPINFO) 2157 naps = MAXAPINFO; 2158 sc->sc_naps = naps; 2159 /* Read Data */ 2160 ap = sc->sc_aps; 2161 memset(&ws_dat, 0, sizeof(ws_dat)); 2162 for (i = 0; i < naps; i++, ap++) { 2163 wi_read_bap(sc, fid, off, &ws_dat, 2164 (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf)); 2165 DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off, 2166 ether_sprintf(ws_dat.wi_bssid))); 2167 off += szbuf; 2168 ap->scanreason = le16toh(ws_hdr.wi_reason); 2169 memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid)); 2170 ap->channel = le16toh(ws_dat.wi_chid); 2171 ap->signal = le16toh(ws_dat.wi_signal); 2172 ap->noise = le16toh(ws_dat.wi_noise); 2173 ap->quality = ap->signal - ap->noise; 2174 ap->capinfo = le16toh(ws_dat.wi_capinfo); 2175 ap->interval = le16toh(ws_dat.wi_interval); 2176 ap->rate = le16toh(ws_dat.wi_rate); 2177 ap->namelen = le16toh(ws_dat.wi_namelen); 2178 if (ap->namelen > sizeof(ap->name)) 2179 ap->namelen = sizeof(ap->name); 2180 memcpy(ap->name, ws_dat.wi_name, ap->namelen); 2181 } 2182 /* Done scanning */ 2183 sc->sc_scan_timer = 0; 2184 DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps)); 2185 } 2186