1 /* $NetBSD: an.c,v 1.66 2017/10/23 09:24:34 msaitoh Exp $ */ 2 /* 3 * Copyright (c) 1997, 1998, 1999 4 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Bill Paul. 17 * 4. Neither the name of the author nor the names of any co-contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 31 * THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 * $FreeBSD: src/sys/dev/an/if_an.c,v 1.12 2000/11/13 23:04:12 wpaul Exp $ 34 */ 35 /* 36 * Copyright (c) 2004, 2005 David Young. All rights reserved. 37 * Copyright (c) 2004, 2005 OJC Technologies. All rights reserved. 38 * Copyright (c) 2004, 2005 Dayton Data Center Services, LLC. All 39 * rights reserved. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 3. Neither the name of the author nor the names of any co-contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY David Young AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL David Young AND CONTRIBUTORS 57 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 58 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 59 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 60 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 61 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 62 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 63 * THE POSSIBILITY OF SUCH DAMAGE. 64 */ 65 66 /* 67 * Aironet 4500/4800 802.11 PCMCIA/ISA/PCI driver for FreeBSD. 68 * 69 * Written by Bill Paul <wpaul@ctr.columbia.edu> 70 * Electrical Engineering Department 71 * Columbia University, New York City 72 */ 73 74 /* 75 * Ported to NetBSD from FreeBSD by Atsushi Onoe at the San Diego 76 * IETF meeting. 77 */ 78 79 #include <sys/cdefs.h> 80 __KERNEL_RCSID(0, "$NetBSD: an.c,v 1.66 2017/10/23 09:24:34 msaitoh Exp $"); 81 82 83 #include <sys/param.h> 84 #include <sys/callout.h> 85 #include <sys/sysctl.h> 86 #include <sys/systm.h> 87 #include <sys/sockio.h> 88 #include <sys/mbuf.h> 89 #include <sys/kernel.h> 90 #include <sys/ucred.h> 91 #include <sys/socket.h> 92 #include <sys/device.h> 93 #include <sys/proc.h> 94 #include <sys/md4.h> 95 #include <sys/endian.h> 96 #include <sys/kauth.h> 97 98 #include <sys/bus.h> 99 #include <sys/intr.h> 100 101 #include <net/if.h> 102 #include <net/if_dl.h> 103 #include <net/if_ether.h> 104 #include <net/if_llc.h> 105 #include <net/if_media.h> 106 #include <net/if_types.h> 107 108 #include <net80211/ieee80211_netbsd.h> 109 #include <net80211/ieee80211_var.h> 110 #include <net80211/ieee80211_radiotap.h> 111 112 #include <net/bpf.h> 113 #include <net/bpfdesc.h> 114 115 #include <dev/ic/anreg.h> 116 #include <dev/ic/anvar.h> 117 118 static int an_reset(struct an_softc *); 119 static void an_wait(struct an_softc *); 120 static void an_softintr(void *); 121 static int an_init(struct ifnet *); 122 static void an_stop(struct ifnet *, int); 123 static void an_start(struct ifnet *); 124 static void an_watchdog(struct ifnet *); 125 static int an_ioctl(struct ifnet *, u_long, void *); 126 static int an_media_change(struct ifnet *); 127 static void an_media_status(struct ifnet *, struct ifmediareq *); 128 129 static int an_set_nwkey(struct an_softc *, struct ieee80211_nwkey *); 130 static int an_set_nwkey_wep(struct an_softc *, struct ieee80211_nwkey *); 131 static int an_set_nwkey_eap(struct an_softc *, struct ieee80211_nwkey *); 132 static int an_get_nwkey(struct an_softc *, struct ieee80211_nwkey *); 133 static int an_write_wepkey(struct an_softc *, int, struct an_wepkey *, 134 int); 135 136 static void an_rx_intr(struct an_softc *); 137 static void an_tx_intr(struct an_softc *, int); 138 static void an_linkstat_intr(struct an_softc *); 139 140 static int an_cmd(struct an_softc *, int, int); 141 static int an_seek_bap(struct an_softc *, int, int); 142 static int an_read_bap(struct an_softc *, int, int, void *, int); 143 static int an_write_bap(struct an_softc *, int, int, void *, int); 144 static int an_mwrite_bap(struct an_softc *, int, int, struct mbuf *, int); 145 static int an_read_rid(struct an_softc *, int, void *, int *); 146 static int an_write_rid(struct an_softc *, int, void *, int); 147 148 static int an_alloc_fid(struct an_softc *, int, int *); 149 150 static int an_newstate(struct ieee80211com *, enum ieee80211_state, int); 151 152 #ifdef AN_DEBUG 153 int an_debug = 0; 154 155 #define DPRINTF(X) if (an_debug) printf X 156 #define DPRINTF2(X) if (an_debug > 1) printf X 157 static int an_sysctl_verify(SYSCTLFN_PROTO, int lower, int upper); 158 static int an_sysctl_verify_debug(SYSCTLFN_PROTO); 159 #else 160 #define DPRINTF(X) 161 #define DPRINTF2(X) 162 #endif 163 164 int 165 an_attach(struct an_softc *sc) 166 { 167 struct ieee80211com *ic = &sc->sc_ic; 168 struct ifnet *ifp = &sc->sc_if; 169 int i, s, rv = 0; 170 struct an_rid_wepkey *akey; 171 int buflen, kid, rid; 172 int chan, chan_min, chan_max; 173 174 s = splnet(); 175 176 an_wait(sc); 177 if (an_reset(sc) != 0) { 178 config_deactivate(sc->sc_dev); 179 rv = 1; 180 goto fail_1; 181 } 182 183 sc->sc_soft_ih = softint_establish(SOFTINT_NET, an_softintr, sc); 184 if (sc->sc_soft_ih == NULL) { 185 aprint_error_dev(sc->sc_dev, "failed to establish softint\n"); 186 rv = 1; 187 goto fail_1; 188 } 189 190 /* Load factory config */ 191 if (an_cmd(sc, AN_CMD_READCFG, 0) != 0) { 192 aprint_error_dev(sc->sc_dev, "failed to load config data\n"); 193 rv = 1; 194 goto fail_2; 195 } 196 197 /* Read the current configuration */ 198 buflen = sizeof(sc->sc_config); 199 if (an_read_rid(sc, AN_RID_GENCONFIG, &sc->sc_config, &buflen) != 0) { 200 aprint_error_dev(sc->sc_dev, "read config failed\n"); 201 rv = 1; 202 goto fail_2; 203 } 204 205 /* Read the card capabilities */ 206 buflen = sizeof(sc->sc_caps); 207 if (an_read_rid(sc, AN_RID_CAPABILITIES, &sc->sc_caps, &buflen) != 0) { 208 aprint_error_dev(sc->sc_dev, "read caps failed\n"); 209 rv = 1; 210 goto fail_2; 211 } 212 213 #ifdef AN_DEBUG 214 if (an_debug) { 215 static const int dumprid[] = { 216 AN_RID_GENCONFIG, AN_RID_CAPABILITIES, AN_RID_SSIDLIST, 217 AN_RID_APLIST, AN_RID_STATUS, AN_RID_ENCAP 218 }; 219 220 for (rid = 0; rid < sizeof(dumprid)/sizeof(dumprid[0]); rid++) { 221 buflen = sizeof(sc->sc_buf); 222 if (an_read_rid(sc, dumprid[rid], &sc->sc_buf, &buflen) 223 != 0) 224 continue; 225 printf("%04x (%d):\n", dumprid[rid], buflen); 226 for (i = 0; i < (buflen + 1) / 2; i++) 227 printf(" %04x", sc->sc_buf.sc_val[i]); 228 printf("\n"); 229 } 230 } 231 #endif 232 233 /* Read WEP settings from persistent memory */ 234 akey = &sc->sc_buf.sc_wepkey; 235 buflen = sizeof(struct an_rid_wepkey); 236 rid = AN_RID_WEP_VOLATILE; /* first persistent key */ 237 while (an_read_rid(sc, rid, akey, &buflen) == 0) { 238 kid = le16toh(akey->an_key_index); 239 DPRINTF(("an_attach: wep rid=0x%x len=%d(%zu) index=0x%04x " 240 "mac[0]=%02x keylen=%d\n", 241 rid, buflen, sizeof(*akey), kid, 242 akey->an_mac_addr[0], le16toh(akey->an_key_len))); 243 if (kid == 0xffff) { 244 sc->sc_tx_perskey = akey->an_mac_addr[0]; 245 sc->sc_tx_key = -1; 246 break; 247 } 248 if (kid >= IEEE80211_WEP_NKID) 249 break; 250 sc->sc_perskeylen[kid] = le16toh(akey->an_key_len); 251 sc->sc_wepkeys[kid].an_wep_keylen = -1; 252 rid = AN_RID_WEP_PERSISTENT; /* for next key */ 253 buflen = sizeof(struct an_rid_wepkey); 254 } 255 256 aprint_normal_dev(sc->sc_dev, "%s %s (firmware %s)\n", 257 sc->sc_caps.an_manufname, sc->sc_caps.an_prodname, 258 sc->sc_caps.an_prodvers); 259 260 memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ); 261 262 ifp->if_softc = sc; 263 ifp->if_flags = IFF_BROADCAST | IFF_NOTRAILERS | IFF_SIMPLEX | 264 IFF_MULTICAST | IFF_ALLMULTI; 265 ifp->if_ioctl = an_ioctl; 266 ifp->if_start = an_start; 267 ifp->if_init = an_init; 268 ifp->if_stop = an_stop; 269 ifp->if_watchdog = an_watchdog; 270 IFQ_SET_READY(&ifp->if_snd); 271 272 ic->ic_ifp = ifp; 273 ic->ic_phytype = IEEE80211_T_DS; 274 ic->ic_opmode = IEEE80211_M_STA; 275 ic->ic_caps = IEEE80211_C_WEP | IEEE80211_C_PMGT | IEEE80211_C_IBSS | 276 IEEE80211_C_MONITOR; 277 ic->ic_state = IEEE80211_S_INIT; 278 IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->sc_caps.an_oemaddr); 279 280 switch (le16toh(sc->sc_caps.an_regdomain)) { 281 default: 282 case AN_REGDOMAIN_USA: 283 case AN_REGDOMAIN_CANADA: 284 chan_min = 1; chan_max = 11; break; 285 case AN_REGDOMAIN_EUROPE: 286 case AN_REGDOMAIN_AUSTRALIA: 287 chan_min = 1; chan_max = 13; break; 288 case AN_REGDOMAIN_JAPAN: 289 chan_min = 14; chan_max = 14; break; 290 case AN_REGDOMAIN_SPAIN: 291 chan_min = 10; chan_max = 11; break; 292 case AN_REGDOMAIN_FRANCE: 293 chan_min = 10; chan_max = 13; break; 294 case AN_REGDOMAIN_JAPANWIDE: 295 chan_min = 1; chan_max = 14; break; 296 } 297 298 for (chan = chan_min; chan <= chan_max; chan++) { 299 ic->ic_channels[chan].ic_freq = 300 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ); 301 ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_B; 302 } 303 ic->ic_ibss_chan = &ic->ic_channels[chan_min]; 304 305 aprint_normal("%s: 802.11 address: %s, channel: %d-%d\n", 306 ifp->if_xname, ether_sprintf(ic->ic_myaddr), chan_min, chan_max); 307 308 /* Find supported rate */ 309 for (i = 0; i < sizeof(sc->sc_caps.an_rates); i++) { 310 if (sc->sc_caps.an_rates[i] == 0) 311 continue; 312 ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ 313 ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates++] = 314 sc->sc_caps.an_rates[i]; 315 } 316 317 /* 318 * Call MI attach routine. 319 */ 320 rv = if_initialize(ifp); 321 if (rv != 0) { 322 aprint_error_dev(sc->sc_dev, "if_initialize failed(%d)\n", rv); 323 goto fail_2; 324 } 325 ieee80211_ifattach(ic); 326 ifp->if_percpuq = if_percpuq_create(ifp); 327 if_register(ifp); 328 329 sc->sc_newstate = ic->ic_newstate; 330 ic->ic_newstate = an_newstate; 331 332 ieee80211_media_init(ic, an_media_change, an_media_status); 333 334 /* 335 * radiotap BPF device 336 */ 337 bpf_attach2(ifp, DLT_IEEE802_11_RADIO, 338 sizeof(struct ieee80211_frame) + 64, &sc->sc_drvbpf); 339 340 memset(&sc->sc_rxtapu, 0, sizeof(sc->sc_rxtapu)); 341 sc->sc_rxtap.ar_ihdr.it_len = htole16(sizeof(sc->sc_rxtapu)); 342 sc->sc_rxtap.ar_ihdr.it_present = htole32(AN_RX_RADIOTAP_PRESENT); 343 344 memset(&sc->sc_txtapu, 0, sizeof(sc->sc_txtapu)); 345 sc->sc_txtap.at_ihdr.it_len = htole16(sizeof(sc->sc_txtapu)); 346 sc->sc_txtap.at_ihdr.it_present = htole32(AN_TX_RADIOTAP_PRESENT); 347 348 sc->sc_attached = 1; 349 splx(s); 350 351 ieee80211_announce(ic); 352 return 0; 353 354 fail_2: 355 if (sc->sc_soft_ih != NULL) 356 softint_disestablish(sc->sc_soft_ih); 357 fail_1: 358 splx(s); 359 360 return rv; 361 } 362 363 #ifdef AN_DEBUG 364 /* 365 * Setup sysctl(3) MIB, hw.an.* 366 * 367 * TBD condition CTLFLAG_PERMANENT on being a module or not 368 */ 369 SYSCTL_SETUP(sysctl_an, "sysctl an(4) subtree setup") 370 { 371 int rc; 372 const struct sysctlnode *cnode, *rnode; 373 374 if ((rc = sysctl_createv(clog, 0, NULL, &rnode, 375 CTLFLAG_PERMANENT, CTLTYPE_NODE, "an", 376 "Cisco/Aironet 802.11 controls", 377 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) 378 goto err; 379 380 /* control debugging printfs */ 381 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode, 382 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, 383 "debug", SYSCTL_DESCR("Enable Cisco/Aironet debugging output"), 384 an_sysctl_verify_debug, 0, &an_debug, 0, 385 CTL_CREATE, CTL_EOL)) != 0) 386 goto err; 387 388 return; 389 err: 390 printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc); 391 } 392 393 static int 394 an_sysctl_verify(SYSCTLFN_ARGS, int lower, int upper) 395 { 396 int error, t; 397 struct sysctlnode node; 398 399 node = *rnode; 400 t = *(int*)rnode->sysctl_data; 401 node.sysctl_data = &t; 402 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 403 if (error || newp == NULL) 404 return (error); 405 406 if (t < lower || t > upper) 407 return (EINVAL); 408 409 *(int*)rnode->sysctl_data = t; 410 411 return (0); 412 } 413 414 static int 415 an_sysctl_verify_debug(SYSCTLFN_ARGS) 416 { 417 return an_sysctl_verify(SYSCTLFN_CALL(rnode), 0, 2); 418 } 419 #endif /* AN_DEBUG */ 420 421 int 422 an_detach(struct an_softc *sc) 423 { 424 struct ieee80211com *ic = &sc->sc_ic; 425 struct ifnet *ifp = &sc->sc_if; 426 int s; 427 428 if (!sc->sc_attached) 429 return 0; 430 431 s = splnet(); 432 an_stop(ifp, 1); 433 ieee80211_ifdetach(ic); 434 if_detach(ifp); 435 if (sc->sc_soft_ih != NULL) 436 softint_disestablish(sc->sc_soft_ih); 437 splx(s); 438 439 return 0; 440 } 441 442 int 443 an_activate(device_t self, enum devact act) 444 { 445 struct an_softc *sc = device_private(self); 446 447 switch (act) { 448 case DVACT_DEACTIVATE: 449 if_deactivate(&sc->sc_if); 450 return 0; 451 default: 452 return EOPNOTSUPP; 453 } 454 } 455 456 int 457 an_intr(void *arg) 458 { 459 struct an_softc *sc = arg; 460 struct ifnet *ifp = &sc->sc_if; 461 462 if (!sc->sc_enabled || !device_is_active(sc->sc_dev) || 463 (ifp->if_flags & IFF_RUNNING) == 0) 464 return 0; 465 466 if ((ifp->if_flags & IFF_UP) == 0) { 467 CSR_WRITE_2(sc, AN_INT_EN, 0); 468 CSR_WRITE_2(sc, AN_EVENT_ACK, ~0); 469 return 1; 470 } 471 472 /* Disable interrupts */ 473 CSR_WRITE_2(sc, AN_INT_EN, 0); 474 475 softint_schedule(sc->sc_soft_ih); 476 return 1; 477 } 478 479 static void 480 an_softintr(void *arg) 481 { 482 struct an_softc *sc = arg; 483 struct ifnet *ifp = &sc->sc_if; 484 int i, s; 485 uint16_t status; 486 487 if (!sc->sc_enabled || !device_is_active(sc->sc_dev) || 488 (ifp->if_flags & IFF_RUNNING) == 0) 489 return; 490 491 if ((ifp->if_flags & IFF_UP) == 0) { 492 CSR_WRITE_2(sc, AN_EVENT_ACK, ~0); 493 return; 494 } 495 496 /* maximum 10 loops per interrupt */ 497 for (i = 0; i < 10; i++) { 498 if (!sc->sc_enabled || !device_is_active(sc->sc_dev)) 499 return; 500 if (CSR_READ_2(sc, AN_SW0) != AN_MAGIC) { 501 DPRINTF(("an_intr: magic number changed: %x\n", 502 CSR_READ_2(sc, AN_SW0))); 503 config_deactivate(sc->sc_dev); 504 return; 505 } 506 status = CSR_READ_2(sc, AN_EVENT_STAT); 507 CSR_WRITE_2(sc, AN_EVENT_ACK, status & ~(AN_INTRS)); 508 if ((status & AN_INTRS) == 0) 509 break; 510 511 if (status & AN_EV_RX) 512 an_rx_intr(sc); 513 514 if (status & (AN_EV_TX | AN_EV_TX_EXC)) 515 an_tx_intr(sc, status); 516 517 if (status & AN_EV_LINKSTAT) 518 an_linkstat_intr(sc); 519 520 if ((ifp->if_flags & IFF_OACTIVE) == 0 && 521 sc->sc_ic.ic_state == IEEE80211_S_RUN && 522 !IFQ_IS_EMPTY(&ifp->if_snd)) { 523 s = splnet(); 524 an_start(ifp); /* in softint */ 525 splx(s); 526 } 527 } 528 if (i == 10) 529 softint_schedule(sc->sc_soft_ih); 530 531 /* Re-enable interrupts */ 532 CSR_WRITE_2(sc, AN_INT_EN, AN_INTRS); 533 } 534 535 static int 536 an_init(struct ifnet *ifp) 537 { 538 struct an_softc *sc = ifp->if_softc; 539 struct ieee80211com *ic = &sc->sc_ic; 540 int i, error, fid; 541 542 DPRINTF(("an_init: enabled %d\n", sc->sc_enabled)); 543 if (!sc->sc_enabled) { 544 if (sc->sc_enable) 545 (*sc->sc_enable)(sc); 546 an_wait(sc); 547 sc->sc_enabled = 1; 548 } else { 549 an_stop(ifp, 0); 550 if ((error = an_reset(sc)) != 0) { 551 printf("%s: failed to reset\n", ifp->if_xname); 552 an_stop(ifp, 1); 553 return error; 554 } 555 } 556 CSR_WRITE_2(sc, AN_SW0, AN_MAGIC); 557 558 /* Allocate the TX buffers */ 559 for (i = 0; i < AN_TX_RING_CNT; i++) { 560 if ((error = an_alloc_fid(sc, AN_TX_MAX_LEN, &fid)) != 0) { 561 printf("%s: failed to allocate nic memory\n", 562 ifp->if_xname); 563 an_stop(ifp, 1); 564 return error; 565 } 566 DPRINTF2(("an_init: txbuf %d allocated %x\n", i, fid)); 567 sc->sc_txd[i].d_fid = fid; 568 sc->sc_txd[i].d_inuse = 0; 569 } 570 sc->sc_txcur = sc->sc_txnext = 0; 571 572 IEEE80211_ADDR_COPY(sc->sc_config.an_macaddr, ic->ic_myaddr); 573 sc->sc_config.an_scanmode = htole16(AN_SCANMODE_ACTIVE); 574 sc->sc_config.an_authtype = htole16(AN_AUTHTYPE_OPEN); /*XXX*/ 575 if (ic->ic_flags & IEEE80211_F_PRIVACY) { 576 sc->sc_config.an_authtype |= 577 htole16(AN_AUTHTYPE_PRIVACY_IN_USE); 578 if (sc->sc_use_leap) 579 sc->sc_config.an_authtype |= 580 htole16(AN_AUTHTYPE_LEAP); 581 } 582 sc->sc_config.an_listen_interval = htole16(ic->ic_lintval); 583 sc->sc_config.an_beacon_period = htole16(ic->ic_lintval); 584 if (ic->ic_flags & IEEE80211_F_PMGTON) 585 sc->sc_config.an_psave_mode = htole16(AN_PSAVE_PSP); 586 else 587 sc->sc_config.an_psave_mode = htole16(AN_PSAVE_CAM); 588 sc->sc_config.an_ds_channel = 589 htole16(ieee80211_chan2ieee(ic, ic->ic_ibss_chan)); 590 591 switch (ic->ic_opmode) { 592 case IEEE80211_M_STA: 593 sc->sc_config.an_opmode = 594 htole16(AN_OPMODE_INFRASTRUCTURE_STATION); 595 sc->sc_config.an_rxmode = htole16(AN_RXMODE_BC_MC_ADDR); 596 break; 597 case IEEE80211_M_IBSS: 598 sc->sc_config.an_opmode = htole16(AN_OPMODE_IBSS_ADHOC); 599 sc->sc_config.an_rxmode = htole16(AN_RXMODE_BC_MC_ADDR); 600 break; 601 case IEEE80211_M_MONITOR: 602 sc->sc_config.an_opmode = 603 htole16(AN_OPMODE_INFRASTRUCTURE_STATION); 604 sc->sc_config.an_rxmode = 605 htole16(AN_RXMODE_80211_MONITOR_ANYBSS); 606 sc->sc_config.an_authtype = htole16(AN_AUTHTYPE_NONE); 607 if (ic->ic_flags & IEEE80211_F_PRIVACY) 608 sc->sc_config.an_authtype |= 609 htole16(AN_AUTHTYPE_PRIVACY_IN_USE | 610 AN_AUTHTYPE_ALLOW_UNENCRYPTED); 611 break; 612 default: 613 printf("%s: bad opmode %d\n", ifp->if_xname, ic->ic_opmode); 614 an_stop(ifp, 1); 615 return EIO; 616 } 617 sc->sc_config.an_rxmode |= htole16(AN_RXMODE_NO_8023_HEADER); 618 619 /* Set the ssid list */ 620 memset(&sc->sc_buf, 0, sizeof(sc->sc_buf.sc_ssidlist)); 621 sc->sc_buf.sc_ssidlist.an_entry[0].an_ssid_len = 622 htole16(ic->ic_des_esslen); 623 if (ic->ic_des_esslen) 624 memcpy(sc->sc_buf.sc_ssidlist.an_entry[0].an_ssid, 625 ic->ic_des_essid, ic->ic_des_esslen); 626 if ((error = an_write_rid(sc, AN_RID_SSIDLIST, &sc->sc_buf, 627 sizeof(sc->sc_buf.sc_ssidlist))) != 0) { 628 printf("%s: failed to write ssid list\n", ifp->if_xname); 629 an_stop(ifp, 1); 630 return error; 631 } 632 633 /* Set the AP list */ 634 memset(&sc->sc_buf, 0, sizeof(sc->sc_buf.sc_aplist)); 635 (void)an_write_rid(sc, AN_RID_APLIST, &sc->sc_buf, 636 sizeof(sc->sc_buf.sc_aplist)); 637 638 /* Set the encapsulation */ 639 for (i = 0; i < AN_ENCAP_NENTS; i++) { 640 sc->sc_buf.sc_encap.an_entry[i].an_ethertype = htole16(0); 641 sc->sc_buf.sc_encap.an_entry[i].an_action = 642 htole16(AN_RXENCAP_RFC1024 | AN_TXENCAP_RFC1024); 643 } 644 (void)an_write_rid(sc, AN_RID_ENCAP, &sc->sc_buf, 645 sizeof(sc->sc_buf.sc_encap)); 646 647 /* Set the WEP Keys */ 648 if (ic->ic_flags & IEEE80211_F_PRIVACY) 649 an_write_wepkey(sc, AN_RID_WEP_VOLATILE, sc->sc_wepkeys, 650 sc->sc_tx_key); 651 652 /* Set the configuration */ 653 #ifdef AN_DEBUG 654 if (an_debug) { 655 printf("write config:\n"); 656 for (i = 0; i < sizeof(sc->sc_config) / 2; i++) 657 printf(" %04x", ((u_int16_t *)&sc->sc_config)[i]); 658 printf("\n"); 659 } 660 #endif 661 if ((error = an_write_rid(sc, AN_RID_GENCONFIG, &sc->sc_config, 662 sizeof(sc->sc_config))) != 0) { 663 printf("%s: failed to write config\n", ifp->if_xname); 664 an_stop(ifp, 1); 665 return error; 666 } 667 668 /* Enable the MAC */ 669 if (an_cmd(sc, AN_CMD_ENABLE, 0)) { 670 aprint_error_dev(sc->sc_dev, "failed to enable MAC\n"); 671 an_stop(ifp, 1); 672 return ENXIO; 673 } 674 if (ifp->if_flags & IFF_PROMISC) 675 an_cmd(sc, AN_CMD_SET_MODE, 0xffff); 676 677 ifp->if_flags |= IFF_RUNNING; 678 ifp->if_flags &= ~IFF_OACTIVE; 679 ic->ic_state = IEEE80211_S_INIT; 680 if (ic->ic_opmode == IEEE80211_M_MONITOR) 681 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 682 683 /* enable interrupts */ 684 CSR_WRITE_2(sc, AN_INT_EN, AN_INTRS); 685 return 0; 686 } 687 688 static void 689 an_stop(struct ifnet *ifp, int disable) 690 { 691 struct an_softc *sc = ifp->if_softc; 692 int i, s; 693 694 if (!sc->sc_enabled) 695 return; 696 697 DPRINTF(("an_stop: disable %d\n", disable)); 698 699 s = splnet(); 700 ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1); 701 if (device_is_active(sc->sc_dev)) { 702 an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0); 703 CSR_WRITE_2(sc, AN_INT_EN, 0); 704 an_cmd(sc, AN_CMD_DISABLE, 0); 705 706 for (i = 0; i < AN_TX_RING_CNT; i++) 707 an_cmd(sc, AN_CMD_DEALLOC_MEM, sc->sc_txd[i].d_fid); 708 } 709 710 sc->sc_tx_timer = 0; 711 ifp->if_timer = 0; 712 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE); 713 714 if (disable) { 715 if (sc->sc_disable) 716 (*sc->sc_disable)(sc); 717 sc->sc_enabled = 0; 718 } 719 splx(s); 720 } 721 722 static void 723 an_start(struct ifnet *ifp) 724 { 725 struct an_softc *sc = (struct an_softc *)ifp->if_softc; 726 struct ieee80211com *ic = &sc->sc_ic; 727 struct ieee80211_node *ni; 728 struct ieee80211_frame *wh; 729 struct an_txframe frmhdr; 730 struct ether_header *eh; 731 struct mbuf *m; 732 u_int16_t len; 733 int cur, fid; 734 735 if (!sc->sc_enabled || !device_is_active(sc->sc_dev)) { 736 DPRINTF(("an_start: noop: enabled %d invalid %d\n", 737 sc->sc_enabled, !device_is_active(sc->sc_dev))); 738 return; 739 } 740 741 memset(&frmhdr, 0, sizeof(frmhdr)); 742 cur = sc->sc_txnext; 743 for (;;) { 744 if (ic->ic_state != IEEE80211_S_RUN) { 745 DPRINTF(("an_start: not running %d\n", ic->ic_state)); 746 break; 747 } 748 IFQ_POLL(&ifp->if_snd, m); 749 if (m == NULL) { 750 DPRINTF2(("an_start: no pending mbuf\n")); 751 break; 752 } 753 if (sc->sc_txd[cur].d_inuse) { 754 DPRINTF2(("an_start: %x/%d busy\n", 755 sc->sc_txd[cur].d_fid, cur)); 756 ifp->if_flags |= IFF_OACTIVE; 757 break; 758 } 759 IFQ_DEQUEUE(&ifp->if_snd, m); 760 ifp->if_opackets++; 761 bpf_mtap(ifp, m); 762 eh = mtod(m, struct ether_header *); 763 ni = ieee80211_find_txnode(ic, eh->ether_dhost); 764 if (ni == NULL) { 765 /* NB: ieee80211_find_txnode does stat+msg */ 766 goto bad; 767 } 768 if ((m = ieee80211_encap(ic, m, ni)) == NULL) 769 goto bad; 770 ieee80211_free_node(ni); 771 bpf_mtap3(ic->ic_rawbpf, m); 772 773 wh = mtod(m, struct ieee80211_frame *); 774 if (ic->ic_flags & IEEE80211_F_PRIVACY) 775 wh->i_fc[1] |= IEEE80211_FC1_WEP; 776 m_copydata(m, 0, sizeof(struct ieee80211_frame), 777 (void *)&frmhdr.an_whdr); 778 779 /* insert payload length in front of llc/snap */ 780 len = htons(m->m_pkthdr.len - sizeof(struct ieee80211_frame)); 781 m_adj(m, sizeof(struct ieee80211_frame) - sizeof(len)); 782 if (mtod(m, u_long) & 0x01) 783 memcpy(mtod(m, void *), &len, sizeof(len)); 784 else 785 *mtod(m, u_int16_t *) = len; 786 787 /* 788 * XXX Aironet firmware apparently convert the packet 789 * with longer than 1500 bytes in length into LLC/SNAP. 790 * If we have 1500 bytes in ethernet payload, it is 791 * 1508 bytes including LLC/SNAP and will be inserted 792 * additional LLC/SNAP header with 1501-1508 in its 793 * ethertype !! 794 * So we skip LLC/SNAP header and force firmware to 795 * convert it to LLC/SNAP again. 796 */ 797 m_adj(m, sizeof(struct llc)); 798 799 frmhdr.an_tx_ctl = htole16(AN_TXCTL_80211); 800 frmhdr.an_tx_payload_len = htole16(m->m_pkthdr.len); 801 frmhdr.an_gaplen = htole16(AN_TXGAP_802_11); 802 803 if (ic->ic_fixed_rate != -1) 804 frmhdr.an_tx_rate = 805 ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ 806 ic->ic_fixed_rate] & IEEE80211_RATE_VAL; 807 else 808 frmhdr.an_tx_rate = 0; 809 810 /* XXX radiotap for tx must be completed */ 811 if (sc->sc_drvbpf) { 812 struct an_tx_radiotap_header *tap = &sc->sc_txtap; 813 tap->at_rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate]; 814 tap->at_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq); 815 tap->at_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags); 816 /* TBD tap->wt_flags */ 817 bpf_mtap2(sc->sc_drvbpf, tap, tap->at_ihdr.it_len, m); 818 } 819 820 #ifdef AN_DEBUG 821 if ((ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) == 822 (IFF_DEBUG|IFF_LINK2)) { 823 ieee80211_dump_pkt((u_int8_t *)&frmhdr.an_whdr, 824 sizeof(struct ieee80211_frame), -1, 0); 825 printf(" txctl 0x%x plen %u\n", 826 le16toh(frmhdr.an_tx_ctl), 827 le16toh(frmhdr.an_tx_payload_len)); 828 } 829 #endif 830 if (sizeof(frmhdr) + AN_TXGAP_802_11 + sizeof(len) + 831 m->m_pkthdr.len > AN_TX_MAX_LEN) 832 goto bad; 833 834 fid = sc->sc_txd[cur].d_fid; 835 if (an_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0) 836 goto bad; 837 /* dummy write to avoid seek. */ 838 an_write_bap(sc, fid, -1, &frmhdr, AN_TXGAP_802_11); 839 an_mwrite_bap(sc, fid, -1, m, m->m_pkthdr.len); 840 m_freem(m); 841 842 DPRINTF2(("an_start: send %zu byte via %x/%d\n", 843 ntohs(len) + sizeof(struct ieee80211_frame), 844 fid, cur)); 845 sc->sc_txd[cur].d_inuse = 1; 846 if (an_cmd(sc, AN_CMD_TX, fid)) { 847 printf("%s: xmit failed\n", ifp->if_xname); 848 sc->sc_txd[cur].d_inuse = 0; 849 continue; 850 } 851 sc->sc_tx_timer = 5; 852 ifp->if_timer = 1; 853 AN_INC(cur, AN_TX_RING_CNT); 854 sc->sc_txnext = cur; 855 continue; 856 bad: 857 ifp->if_oerrors++; 858 m_freem(m); 859 } 860 } 861 862 static int 863 an_reset(struct an_softc *sc) 864 { 865 866 DPRINTF(("an_reset\n")); 867 868 if (!sc->sc_enabled) 869 return ENXIO; 870 871 an_cmd(sc, AN_CMD_ENABLE, 0); 872 an_cmd(sc, AN_CMD_FW_RESTART, 0); 873 an_cmd(sc, AN_CMD_NOOP2, 0); 874 875 if (an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0) == ETIMEDOUT) { 876 aprint_error_dev(sc->sc_dev, "reset failed\n"); 877 return ETIMEDOUT; 878 } 879 880 an_cmd(sc, AN_CMD_DISABLE, 0); 881 return 0; 882 } 883 884 static void 885 an_watchdog(struct ifnet *ifp) 886 { 887 struct an_softc *sc = ifp->if_softc; 888 889 if (!sc->sc_enabled) 890 return; 891 892 if (sc->sc_tx_timer) { 893 if (--sc->sc_tx_timer == 0) { 894 printf("%s: device timeout\n", ifp->if_xname); 895 ifp->if_oerrors++; 896 an_init(ifp); 897 return; 898 } 899 ifp->if_timer = 1; 900 } 901 ieee80211_watchdog(&sc->sc_ic); 902 } 903 904 static int 905 an_ioctl(struct ifnet *ifp, u_long command, void *data) 906 { 907 struct an_softc *sc = ifp->if_softc; 908 int s, error = 0; 909 910 if (!device_is_active(sc->sc_dev)) 911 return ENXIO; 912 913 s = splnet(); 914 915 switch (command) { 916 case SIOCSIFFLAGS: 917 if ((error = ifioctl_common(ifp, command, data)) != 0) 918 break; 919 if (ifp->if_flags & IFF_UP) { 920 if (sc->sc_enabled) { 921 /* 922 * To avoid rescanning another access point, 923 * do not call an_init() here. Instead, only 924 * reflect promisc mode settings. 925 */ 926 error = an_cmd(sc, AN_CMD_SET_MODE, 927 (ifp->if_flags & IFF_PROMISC) ? 0xffff : 0); 928 } else 929 error = an_init(ifp); 930 } else if (sc->sc_enabled) 931 an_stop(ifp, 1); 932 break; 933 case SIOCADDMULTI: 934 case SIOCDELMULTI: 935 error = ether_ioctl(ifp, command, data); 936 if (error == ENETRESET) { 937 /* we don't have multicast filter. */ 938 error = 0; 939 } 940 break; 941 case SIOCS80211NWKEY: 942 error = an_set_nwkey(sc, (struct ieee80211_nwkey *)data); 943 break; 944 case SIOCG80211NWKEY: 945 error = an_get_nwkey(sc, (struct ieee80211_nwkey *)data); 946 break; 947 default: 948 error = ieee80211_ioctl(&sc->sc_ic, command, data); 949 break; 950 } 951 if (error == ENETRESET) { 952 if (sc->sc_enabled) 953 error = an_init(ifp); 954 else 955 error = 0; 956 } 957 splx(s); 958 return error; 959 } 960 961 /* TBD factor with ieee80211_media_change */ 962 static int 963 an_media_change(struct ifnet *ifp) 964 { 965 struct an_softc *sc = ifp->if_softc; 966 struct ieee80211com *ic = &sc->sc_ic; 967 struct ifmedia_entry *ime; 968 enum ieee80211_opmode newmode; 969 int i, rate, error = 0; 970 971 ime = ic->ic_media.ifm_cur; 972 if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) { 973 i = -1; 974 } else { 975 struct ieee80211_rateset *rs = 976 &ic->ic_sup_rates[IEEE80211_MODE_11B]; 977 rate = ieee80211_media2rate(ime->ifm_media); 978 if (rate == 0) 979 return EINVAL; 980 for (i = 0; i < rs->rs_nrates; i++) { 981 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate) 982 break; 983 } 984 if (i == rs->rs_nrates) 985 return EINVAL; 986 } 987 if (ic->ic_fixed_rate != i) { 988 ic->ic_fixed_rate = i; 989 error = ENETRESET; 990 } 991 992 if (ime->ifm_media & IFM_IEEE80211_ADHOC) 993 newmode = IEEE80211_M_IBSS; 994 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP) 995 newmode = IEEE80211_M_HOSTAP; 996 else if (ime->ifm_media & IFM_IEEE80211_MONITOR) 997 newmode = IEEE80211_M_MONITOR; 998 else 999 newmode = IEEE80211_M_STA; 1000 if (ic->ic_opmode != newmode) { 1001 ic->ic_opmode = newmode; 1002 error = ENETRESET; 1003 } 1004 if (error == ENETRESET) { 1005 if (sc->sc_enabled) 1006 error = an_init(ifp); 1007 else 1008 error = 0; 1009 } 1010 ifp->if_baudrate = ifmedia_baudrate(ic->ic_media.ifm_cur->ifm_media); 1011 1012 return error; 1013 } 1014 1015 static void 1016 an_media_status(struct ifnet *ifp, struct ifmediareq *imr) 1017 { 1018 struct an_softc *sc = ifp->if_softc; 1019 struct ieee80211com *ic = &sc->sc_ic; 1020 int rate, buflen; 1021 1022 if (sc->sc_enabled == 0) { 1023 imr->ifm_active = IFM_IEEE80211 | IFM_NONE; 1024 imr->ifm_status = 0; 1025 return; 1026 } 1027 1028 imr->ifm_status = IFM_AVALID; 1029 imr->ifm_active = IFM_IEEE80211; 1030 if (ic->ic_state == IEEE80211_S_RUN) 1031 imr->ifm_status |= IFM_ACTIVE; 1032 buflen = sizeof(sc->sc_buf); 1033 if (ic->ic_fixed_rate != -1) 1034 rate = ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ 1035 ic->ic_fixed_rate] & IEEE80211_RATE_VAL; 1036 else if (an_read_rid(sc, AN_RID_STATUS, &sc->sc_buf, &buflen) != 0) 1037 rate = 0; 1038 else 1039 rate = le16toh(sc->sc_buf.sc_status.an_current_tx_rate); 1040 imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B); 1041 switch (ic->ic_opmode) { 1042 case IEEE80211_M_STA: 1043 break; 1044 case IEEE80211_M_IBSS: 1045 imr->ifm_active |= IFM_IEEE80211_ADHOC; 1046 break; 1047 case IEEE80211_M_HOSTAP: 1048 imr->ifm_active |= IFM_IEEE80211_HOSTAP; 1049 break; 1050 case IEEE80211_M_MONITOR: 1051 imr->ifm_active |= IFM_IEEE80211_MONITOR; 1052 break; 1053 default: 1054 break; 1055 } 1056 } 1057 1058 static int 1059 an_set_nwkey(struct an_softc *sc, struct ieee80211_nwkey *nwkey) 1060 { 1061 int error; 1062 struct ieee80211com *ic = &sc->sc_ic; 1063 u_int16_t prevauth; 1064 1065 error = 0; 1066 prevauth = sc->sc_config.an_authtype; 1067 1068 switch (nwkey->i_wepon) { 1069 case IEEE80211_NWKEY_OPEN: 1070 sc->sc_config.an_authtype = AN_AUTHTYPE_OPEN; 1071 ic->ic_flags &= ~IEEE80211_F_PRIVACY; 1072 break; 1073 1074 case IEEE80211_NWKEY_WEP: 1075 case IEEE80211_NWKEY_WEP | IEEE80211_NWKEY_PERSIST: 1076 error = an_set_nwkey_wep(sc, nwkey); 1077 if (error == 0 || error == ENETRESET) { 1078 sc->sc_config.an_authtype = 1079 AN_AUTHTYPE_OPEN | AN_AUTHTYPE_PRIVACY_IN_USE; 1080 ic->ic_flags |= IEEE80211_F_PRIVACY; 1081 } 1082 break; 1083 1084 case IEEE80211_NWKEY_EAP: 1085 error = an_set_nwkey_eap(sc, nwkey); 1086 if (error == 0 || error == ENETRESET) { 1087 sc->sc_config.an_authtype = AN_AUTHTYPE_OPEN | 1088 AN_AUTHTYPE_PRIVACY_IN_USE | AN_AUTHTYPE_LEAP; 1089 ic->ic_flags |= IEEE80211_F_PRIVACY; 1090 } 1091 break; 1092 default: 1093 error = EINVAL; 1094 break; 1095 } 1096 if (error == 0 && prevauth != sc->sc_config.an_authtype) 1097 error = ENETRESET; 1098 return error; 1099 } 1100 1101 static int 1102 an_set_nwkey_wep(struct an_softc *sc, struct ieee80211_nwkey *nwkey) 1103 { 1104 int i, txkey, anysetkey, needreset, error; 1105 struct an_wepkey keys[IEEE80211_WEP_NKID]; 1106 1107 error = 0; 1108 memset(keys, 0, sizeof(keys)); 1109 anysetkey = needreset = 0; 1110 1111 /* load argument and sanity check */ 1112 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 1113 keys[i].an_wep_keylen = nwkey->i_key[i].i_keylen; 1114 if (keys[i].an_wep_keylen < 0) 1115 continue; 1116 if (keys[i].an_wep_keylen != 0 && 1117 keys[i].an_wep_keylen < IEEE80211_WEP_KEYLEN) 1118 return EINVAL; 1119 if (keys[i].an_wep_keylen > sizeof(keys[i].an_wep_key)) 1120 return EINVAL; 1121 if ((error = copyin(nwkey->i_key[i].i_keydat, 1122 keys[i].an_wep_key, keys[i].an_wep_keylen)) != 0) 1123 return error; 1124 anysetkey++; 1125 } 1126 txkey = nwkey->i_defkid - 1; 1127 if (txkey >= 0) { 1128 if (txkey >= IEEE80211_WEP_NKID) 1129 return EINVAL; 1130 /* default key must have a valid value */ 1131 if (keys[txkey].an_wep_keylen == 0 || 1132 (keys[txkey].an_wep_keylen < 0 && 1133 sc->sc_perskeylen[txkey] == 0)) 1134 return EINVAL; 1135 anysetkey++; 1136 } 1137 DPRINTF(("an_set_nwkey_wep: %s: %sold(%d:%d,%d,%d,%d) " 1138 "pers(%d:%d,%d,%d,%d) new(%d:%d,%d,%d,%d)\n", 1139 device_xname(sc->sc_dev), 1140 ((nwkey->i_wepon & IEEE80211_NWKEY_PERSIST) ? "persist: " : ""), 1141 sc->sc_tx_key, 1142 sc->sc_wepkeys[0].an_wep_keylen, sc->sc_wepkeys[1].an_wep_keylen, 1143 sc->sc_wepkeys[2].an_wep_keylen, sc->sc_wepkeys[3].an_wep_keylen, 1144 sc->sc_tx_perskey, 1145 sc->sc_perskeylen[0], sc->sc_perskeylen[1], 1146 sc->sc_perskeylen[2], sc->sc_perskeylen[3], 1147 txkey, 1148 keys[0].an_wep_keylen, keys[1].an_wep_keylen, 1149 keys[2].an_wep_keylen, keys[3].an_wep_keylen)); 1150 if (!(nwkey->i_wepon & IEEE80211_NWKEY_PERSIST)) { 1151 /* set temporary keys */ 1152 sc->sc_tx_key = txkey; 1153 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 1154 if (keys[i].an_wep_keylen < 0) 1155 continue; 1156 memcpy(&sc->sc_wepkeys[i], &keys[i], sizeof(keys[i])); 1157 } 1158 } else { 1159 /* set persist keys */ 1160 if (anysetkey) { 1161 /* prepare to write nvram */ 1162 if (!sc->sc_enabled) { 1163 if (sc->sc_enable) 1164 (*sc->sc_enable)(sc); 1165 an_wait(sc); 1166 sc->sc_enabled = 1; 1167 error = an_write_wepkey(sc, 1168 AN_RID_WEP_PERSISTENT, keys, txkey); 1169 if (sc->sc_disable) 1170 (*sc->sc_disable)(sc); 1171 sc->sc_enabled = 0; 1172 } else { 1173 an_cmd(sc, AN_CMD_DISABLE, 0); 1174 error = an_write_wepkey(sc, 1175 AN_RID_WEP_PERSISTENT, keys, txkey); 1176 an_cmd(sc, AN_CMD_ENABLE, 0); 1177 } 1178 if (error) 1179 return error; 1180 } 1181 if (txkey >= 0) 1182 sc->sc_tx_perskey = txkey; 1183 if (sc->sc_tx_key >= 0) { 1184 sc->sc_tx_key = -1; 1185 needreset++; 1186 } 1187 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 1188 if (sc->sc_wepkeys[i].an_wep_keylen >= 0) { 1189 memset(&sc->sc_wepkeys[i].an_wep_key, 0, 1190 sizeof(sc->sc_wepkeys[i].an_wep_key)); 1191 sc->sc_wepkeys[i].an_wep_keylen = -1; 1192 needreset++; 1193 } 1194 if (keys[i].an_wep_keylen >= 0) 1195 sc->sc_perskeylen[i] = keys[i].an_wep_keylen; 1196 } 1197 } 1198 if (needreset) { 1199 /* firmware restart to reload persistent key */ 1200 an_reset(sc); 1201 } 1202 if (anysetkey || needreset) 1203 error = ENETRESET; 1204 return error; 1205 } 1206 1207 static int 1208 an_set_nwkey_eap(struct an_softc *sc, struct ieee80211_nwkey *nwkey) 1209 { 1210 int i, error, len; 1211 struct ifnet *ifp = &sc->sc_if; 1212 struct an_rid_leapkey *key; 1213 u_int16_t unibuf[sizeof(key->an_key)]; 1214 static const int leap_rid[] = { AN_RID_LEAP_PASS, AN_RID_LEAP_USER }; 1215 MD4_CTX ctx; 1216 1217 error = 0; 1218 1219 if (nwkey->i_key[0].i_keydat == NULL && 1220 nwkey->i_key[1].i_keydat == NULL) 1221 return 0; 1222 if (!sc->sc_enabled) 1223 return ENXIO; 1224 an_cmd(sc, AN_CMD_DISABLE, 0); 1225 key = &sc->sc_buf.sc_leapkey; 1226 for (i = 0; i < 2; i++) { 1227 if (nwkey->i_key[i].i_keydat == NULL) 1228 continue; 1229 len = nwkey->i_key[i].i_keylen; 1230 if (len > sizeof(key->an_key)) 1231 return EINVAL; 1232 memset(key, 0, sizeof(*key)); 1233 key->an_key_len = htole16(len); 1234 if ((error = copyin(nwkey->i_key[i].i_keydat, key->an_key, 1235 len)) != 0) 1236 return error; 1237 if (i == 1) { 1238 /* 1239 * Cisco seems to use PasswordHash and PasswordHashHash 1240 * in RFC-2759 (MS-CHAP-V2). 1241 */ 1242 memset(unibuf, 0, sizeof(unibuf)); 1243 /* XXX: convert password to unicode */ 1244 for (i = 0; i < len; i++) 1245 unibuf[i] = key->an_key[i]; 1246 /* set PasswordHash */ 1247 MD4Init(&ctx); 1248 MD4Update(&ctx, (u_int8_t *)unibuf, len * 2); 1249 MD4Final(key->an_key, &ctx); 1250 /* set PasswordHashHash */ 1251 MD4Init(&ctx); 1252 MD4Update(&ctx, key->an_key, 16); 1253 MD4Final(key->an_key + 16, &ctx); 1254 key->an_key_len = htole16(32); 1255 } 1256 if ((error = an_write_rid(sc, leap_rid[i], key, 1257 sizeof(*key))) != 0) { 1258 printf("%s: LEAP set failed\n", ifp->if_xname); 1259 return error; 1260 } 1261 } 1262 error = an_cmd(sc, AN_CMD_ENABLE, 0); 1263 if (error) 1264 printf("%s: an_set_nwkey: failed to enable MAC\n", 1265 ifp->if_xname); 1266 else 1267 error = ENETRESET; 1268 return error; 1269 } 1270 1271 static int 1272 an_get_nwkey(struct an_softc *sc, struct ieee80211_nwkey *nwkey) 1273 { 1274 int i, error; 1275 1276 error = 0; 1277 if (sc->sc_config.an_authtype & AN_AUTHTYPE_LEAP) 1278 nwkey->i_wepon = IEEE80211_NWKEY_EAP; 1279 else if (sc->sc_config.an_authtype & AN_AUTHTYPE_PRIVACY_IN_USE) 1280 nwkey->i_wepon = IEEE80211_NWKEY_WEP; 1281 else 1282 nwkey->i_wepon = IEEE80211_NWKEY_OPEN; 1283 if (sc->sc_tx_key == -1) 1284 nwkey->i_defkid = sc->sc_tx_perskey + 1; 1285 else 1286 nwkey->i_defkid = sc->sc_tx_key + 1; 1287 if (nwkey->i_key[0].i_keydat == NULL) 1288 return 0; 1289 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 1290 if (nwkey->i_key[i].i_keydat == NULL) 1291 continue; 1292 /* do not show any keys to non-root user */ 1293 /* XXX-elad: why is this inside a loop? */ 1294 if ((error = kauth_authorize_network(curlwp->l_cred, 1295 KAUTH_NETWORK_INTERFACE, 1296 KAUTH_REQ_NETWORK_INTERFACE_GETPRIV, sc->sc_ic.ic_ifp, 1297 KAUTH_ARG(SIOCG80211NWKEY), NULL)) != 0) 1298 break; 1299 nwkey->i_key[i].i_keylen = sc->sc_wepkeys[i].an_wep_keylen; 1300 if (nwkey->i_key[i].i_keylen < 0) { 1301 if (sc->sc_perskeylen[i] == 0) 1302 nwkey->i_key[i].i_keylen = 0; 1303 continue; 1304 } 1305 if ((error = copyout(sc->sc_wepkeys[i].an_wep_key, 1306 nwkey->i_key[i].i_keydat, 1307 sc->sc_wepkeys[i].an_wep_keylen)) != 0) 1308 break; 1309 } 1310 return error; 1311 } 1312 1313 static int 1314 an_write_wepkey(struct an_softc *sc, int type, struct an_wepkey *keys, int kid) 1315 { 1316 int i, error; 1317 struct an_rid_wepkey *akey; 1318 1319 error = 0; 1320 akey = &sc->sc_buf.sc_wepkey; 1321 memset(akey, 0, sizeof(struct an_rid_wepkey)); 1322 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 1323 if (keys[i].an_wep_keylen < 0 || 1324 keys[i].an_wep_keylen > sizeof(akey->an_key)) 1325 continue; 1326 akey->an_key_len = htole16(keys[i].an_wep_keylen); 1327 akey->an_key_index = htole16(i); 1328 akey->an_mac_addr[0] = 1; /* default mac */ 1329 memcpy(akey->an_key, keys[i].an_wep_key, keys[i].an_wep_keylen); 1330 if ((error = an_write_rid(sc, type, akey, sizeof(*akey))) != 0) 1331 return error; 1332 } 1333 if (kid >= 0) { 1334 akey->an_key_index = htole16(0xffff); 1335 akey->an_mac_addr[0] = kid; 1336 akey->an_key_len = htole16(0); 1337 memset(akey->an_key, 0, sizeof(akey->an_key)); 1338 error = an_write_rid(sc, type, akey, sizeof(*akey)); 1339 } 1340 return error; 1341 } 1342 1343 #ifdef AN_DEBUG 1344 static void 1345 an_dump_pkt(const char *devname, struct mbuf *m) 1346 { 1347 int col, col0, i; 1348 uint8_t *pkt = mtod(m, uint8_t *); 1349 const char *delim = ""; 1350 int delimw = 0; 1351 1352 printf("%s: pkt ", devname); 1353 col = col0 = strlen(devname) + strlen(": pkt "); 1354 for (i = 0; i < m->m_len; i++) { 1355 printf("%s%02x", delim, pkt[i]); 1356 delim = ":"; 1357 delimw = 1; 1358 col += delimw + 2; 1359 if (col >= 72) { 1360 printf("\n%*s", col0, ""); 1361 col = col0; 1362 delim = ""; 1363 delimw = 0; 1364 } 1365 } 1366 if (col != 0) 1367 printf("\n"); 1368 } 1369 #endif /* AN_DEBUG */ 1370 1371 /* 1372 * Low level functions 1373 */ 1374 1375 static void 1376 an_rx_intr(struct an_softc *sc) 1377 { 1378 struct ieee80211com *ic = &sc->sc_ic; 1379 struct ifnet *ifp = &sc->sc_if; 1380 struct ieee80211_frame_min *wh; 1381 struct ieee80211_node *ni; 1382 struct an_rxframe frmhdr; 1383 struct mbuf *m; 1384 u_int16_t status; 1385 int fid, gaplen, len, off, s; 1386 uint8_t *gap; 1387 1388 fid = CSR_READ_2(sc, AN_RX_FID); 1389 1390 /* First read in the frame header */ 1391 if (an_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0) { 1392 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX); 1393 ifp->if_ierrors++; 1394 DPRINTF(("an_rx_intr: read fid %x failed\n", fid)); 1395 return; 1396 } 1397 1398 #ifdef AN_DEBUG 1399 if ((ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2)) { 1400 ieee80211_dump_pkt((u_int8_t *)&frmhdr.an_whdr, 1401 sizeof(struct ieee80211_frame), frmhdr.an_rx_rate, 1402 frmhdr.an_rx_signal_strength); 1403 printf(" time 0x%x status 0x%x plen %u chan %u" 1404 " plcp %02x %02x %02x %02x gap %u\n", 1405 le32toh(frmhdr.an_rx_time), le16toh(frmhdr.an_rx_status), 1406 le16toh(frmhdr.an_rx_payload_len), frmhdr.an_rx_chan, 1407 frmhdr.an_plcp_hdr[0], frmhdr.an_plcp_hdr[1], 1408 frmhdr.an_plcp_hdr[2], frmhdr.an_plcp_hdr[3], 1409 le16toh(frmhdr.an_gaplen)); 1410 } 1411 #endif 1412 1413 status = le16toh(frmhdr.an_rx_status); 1414 if ((status & AN_STAT_ERRSTAT) != 0 && 1415 ic->ic_opmode != IEEE80211_M_MONITOR) { 1416 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX); 1417 ifp->if_ierrors++; 1418 DPRINTF(("an_rx_intr: fid %x status %x\n", fid, status)); 1419 return; 1420 } 1421 1422 /* the payload length field includes a 16-bit "mystery field" */ 1423 len = le16toh(frmhdr.an_rx_payload_len) - sizeof(uint16_t); 1424 off = ALIGN(sizeof(struct ieee80211_frame)); 1425 1426 if (off + len > MCLBYTES) { 1427 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 1428 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX); 1429 ifp->if_ierrors++; 1430 DPRINTF(("an_rx_intr: oversized packet %d\n", len)); 1431 return; 1432 } 1433 len = 0; 1434 } 1435 1436 MGETHDR(m, M_DONTWAIT, MT_DATA); 1437 if (m == NULL) { 1438 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX); 1439 ifp->if_ierrors++; 1440 DPRINTF(("an_rx_intr: MGET failed\n")); 1441 return; 1442 } 1443 if (off + len + AN_GAPLEN_MAX > MHLEN) { 1444 MCLGET(m, M_DONTWAIT); 1445 if ((m->m_flags & M_EXT) == 0) { 1446 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX); 1447 m_freem(m); 1448 ifp->if_ierrors++; 1449 DPRINTF(("an_rx_intr: MCLGET failed\n")); 1450 return; 1451 } 1452 } 1453 m->m_data += off - sizeof(struct ieee80211_frame); 1454 1455 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 1456 gaplen = le16toh(frmhdr.an_gaplen); 1457 if (gaplen > AN_GAPLEN_MAX) { 1458 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX); 1459 m_freem(m); 1460 ifp->if_ierrors++; 1461 DPRINTF(("%s: gap too long\n", __func__)); 1462 return; 1463 } 1464 /* 1465 * We don't need the 16-bit mystery field (payload length?), 1466 * so read it into the region reserved for the 802.11 header. 1467 * 1468 * When Cisco Aironet 350 cards w/ firmware version 5 or 1469 * greater operate with certain Cisco 350 APs, 1470 * the "gap" is filled with the SNAP header. Read 1471 * it in after the 802.11 header. 1472 */ 1473 gap = m->m_data + sizeof(struct ieee80211_frame) - 1474 sizeof(uint16_t); 1475 an_read_bap(sc, fid, -1, gap, gaplen + sizeof(u_int16_t)); 1476 #ifdef AN_DEBUG 1477 if ((ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) == 1478 (IFF_DEBUG|IFF_LINK2)) { 1479 int i; 1480 printf(" gap&len"); 1481 for (i = 0; i < gaplen + sizeof(u_int16_t); i++) 1482 printf(" %02x", gap[i]); 1483 printf("\n"); 1484 } 1485 #endif 1486 } else 1487 gaplen = 0; 1488 1489 an_read_bap(sc, fid, -1, 1490 m->m_data + sizeof(struct ieee80211_frame) + gaplen, len); 1491 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + gaplen + 1492 len; 1493 1494 memcpy(m->m_data, &frmhdr.an_whdr, sizeof(struct ieee80211_frame)); 1495 m_set_rcvif(m, ifp); 1496 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX); 1497 1498 s = splnet(); 1499 1500 if (sc->sc_drvbpf) { 1501 struct an_rx_radiotap_header *tap = &sc->sc_rxtap; 1502 1503 tap->ar_rate = frmhdr.an_rx_rate; 1504 tap->ar_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags); 1505 tap->ar_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq); 1506 tap->ar_antsignal = frmhdr.an_rx_signal_strength; 1507 if ((le16toh(frmhdr.an_rx_status) & AN_STAT_BADCRC) || 1508 (le16toh(frmhdr.an_rx_status) & AN_STAT_ERRSTAT) || 1509 (le16toh(frmhdr.an_rx_status) & AN_STAT_UNDECRYPTABLE)) 1510 tap->ar_flags |= IEEE80211_RADIOTAP_F_BADFCS; 1511 1512 bpf_mtap2(sc->sc_drvbpf, tap, tap->ar_ihdr.it_len, m); 1513 } 1514 wh = mtod(m, struct ieee80211_frame_min *); 1515 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1516 /* 1517 * WEP is decrypted by hardware. Clear WEP bit 1518 * header for ieee80211_input(). 1519 */ 1520 wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 1521 } 1522 1523 #ifdef AN_DEBUG 1524 if (an_debug > 1) 1525 an_dump_pkt(device_xname(sc->sc_dev), m); 1526 #endif /* AN_DEBUG */ 1527 1528 ni = ieee80211_find_rxnode(ic, wh); 1529 ieee80211_input(ic, m, ni, frmhdr.an_rx_signal_strength, 1530 le32toh(frmhdr.an_rx_time)); 1531 ieee80211_free_node(ni); 1532 1533 splx(s); 1534 } 1535 1536 static void 1537 an_tx_intr(struct an_softc *sc, int status) 1538 { 1539 struct ifnet *ifp = &sc->sc_if; 1540 int cur, fid, s; 1541 1542 s = splnet(); 1543 1544 sc->sc_tx_timer = 0; 1545 ifp->if_flags &= ~IFF_OACTIVE; 1546 1547 fid = CSR_READ_2(sc, AN_TX_CMP_FID); 1548 CSR_WRITE_2(sc, AN_EVENT_ACK, status & (AN_EV_TX | AN_EV_TX_EXC)); 1549 1550 if (status & AN_EV_TX_EXC) 1551 ifp->if_oerrors++; 1552 else 1553 ifp->if_opackets++; 1554 1555 cur = sc->sc_txcur; 1556 if (sc->sc_txd[cur].d_fid == fid) { 1557 sc->sc_txd[cur].d_inuse = 0; 1558 DPRINTF2(("an_tx_intr: sent %x/%d\n", fid, cur)); 1559 AN_INC(cur, AN_TX_RING_CNT); 1560 sc->sc_txcur = cur; 1561 } else { 1562 for (cur = 0; cur < AN_TX_RING_CNT; cur++) { 1563 if (fid == sc->sc_txd[cur].d_fid) { 1564 sc->sc_txd[cur].d_inuse = 0; 1565 break; 1566 } 1567 } 1568 if (ifp->if_flags & IFF_DEBUG) 1569 printf("%s: tx mismatch: " 1570 "expected %x(%d), actual %x(%d)\n", 1571 device_xname(sc->sc_dev), 1572 sc->sc_txd[sc->sc_txcur].d_fid, sc->sc_txcur, 1573 fid, cur); 1574 } 1575 1576 splx(s); 1577 } 1578 1579 static void 1580 an_linkstat_intr(struct an_softc *sc) 1581 { 1582 struct ieee80211com *ic = &sc->sc_ic; 1583 u_int16_t status; 1584 int s; 1585 1586 status = CSR_READ_2(sc, AN_LINKSTAT); 1587 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_LINKSTAT); 1588 DPRINTF(("an_linkstat_intr: status 0x%x\n", status)); 1589 1590 s = splnet(); 1591 if (status == AN_LINKSTAT_ASSOCIATED) { 1592 if (ic->ic_state != IEEE80211_S_RUN || 1593 ic->ic_opmode == IEEE80211_M_IBSS) 1594 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1595 } else { 1596 if (ic->ic_opmode == IEEE80211_M_STA) 1597 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 1598 } 1599 splx(s); 1600 } 1601 1602 /* Must be called at proper protection level! */ 1603 static int 1604 an_cmd(struct an_softc *sc, int cmd, int val) 1605 { 1606 int i, status; 1607 1608 /* make sure that previous command completed */ 1609 if (CSR_READ_2(sc, AN_COMMAND) & AN_CMD_BUSY) { 1610 if (sc->sc_if.if_flags & IFF_DEBUG) 1611 printf("%s: command 0x%x busy\n", device_xname(sc->sc_dev), 1612 CSR_READ_2(sc, AN_COMMAND)); 1613 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CLR_STUCK_BUSY); 1614 } 1615 1616 CSR_WRITE_2(sc, AN_PARAM0, val); 1617 CSR_WRITE_2(sc, AN_PARAM1, 0); 1618 CSR_WRITE_2(sc, AN_PARAM2, 0); 1619 CSR_WRITE_2(sc, AN_COMMAND, cmd); 1620 1621 if (cmd == AN_CMD_FW_RESTART) { 1622 /* XXX: should sleep here */ 1623 DELAY(100*1000); 1624 } 1625 1626 for (i = 0; i < AN_TIMEOUT; i++) { 1627 if (CSR_READ_2(sc, AN_EVENT_STAT) & AN_EV_CMD) 1628 break; 1629 DELAY(10); 1630 } 1631 1632 status = CSR_READ_2(sc, AN_STATUS); 1633 1634 /* clear stuck command busy if necessary */ 1635 if (CSR_READ_2(sc, AN_COMMAND) & AN_CMD_BUSY) 1636 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CLR_STUCK_BUSY); 1637 1638 /* Ack the command */ 1639 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CMD); 1640 1641 if (i == AN_TIMEOUT) { 1642 if (sc->sc_if.if_flags & IFF_DEBUG) 1643 printf("%s: command 0x%x param 0x%x timeout\n", 1644 device_xname(sc->sc_dev), cmd, val); 1645 return ETIMEDOUT; 1646 } 1647 if (status & AN_STAT_CMD_RESULT) { 1648 if (sc->sc_if.if_flags & IFF_DEBUG) 1649 printf("%s: command 0x%x param 0x%x status 0x%x " 1650 "resp 0x%x 0x%x 0x%x\n", 1651 device_xname(sc->sc_dev), cmd, val, status, 1652 CSR_READ_2(sc, AN_RESP0), CSR_READ_2(sc, AN_RESP1), 1653 CSR_READ_2(sc, AN_RESP2)); 1654 return EIO; 1655 } 1656 1657 return 0; 1658 } 1659 1660 1661 /* 1662 * Wait for firmware come up after power enabled. 1663 */ 1664 static void 1665 an_wait(struct an_softc *sc) 1666 { 1667 int i; 1668 1669 CSR_WRITE_2(sc, AN_COMMAND, AN_CMD_NOOP2); 1670 for (i = 0; i < 3*hz; i++) { 1671 if (CSR_READ_2(sc, AN_EVENT_STAT) & AN_EV_CMD) 1672 break; 1673 (void)tsleep(sc, PWAIT, "anatch", 1); 1674 } 1675 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CMD); 1676 } 1677 1678 static int 1679 an_seek_bap(struct an_softc *sc, int id, int off) 1680 { 1681 int i, status; 1682 1683 CSR_WRITE_2(sc, AN_SEL0, id); 1684 CSR_WRITE_2(sc, AN_OFF0, off); 1685 1686 for (i = 0; ; i++) { 1687 status = CSR_READ_2(sc, AN_OFF0); 1688 if ((status & AN_OFF_BUSY) == 0) 1689 break; 1690 if (i == AN_TIMEOUT) { 1691 printf("%s: timeout in an_seek_bap to 0x%x/0x%x\n", 1692 device_xname(sc->sc_dev), id, off); 1693 sc->sc_bap_off = AN_OFF_ERR; /* invalidate */ 1694 return ETIMEDOUT; 1695 } 1696 DELAY(10); 1697 } 1698 if (status & AN_OFF_ERR) { 1699 aprint_error_dev(sc->sc_dev, "failed in an_seek_bap to 0x%x/0x%x\n", 1700 id, off); 1701 sc->sc_bap_off = AN_OFF_ERR; /* invalidate */ 1702 return EIO; 1703 } 1704 sc->sc_bap_id = id; 1705 sc->sc_bap_off = off; 1706 return 0; 1707 } 1708 1709 static int 1710 an_read_bap(struct an_softc *sc, int id, int off, void *buf, int buflen) 1711 { 1712 int error, cnt; 1713 1714 if (buflen == 0) 1715 return 0; 1716 if (off == -1) 1717 off = sc->sc_bap_off; 1718 if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 1719 if ((error = an_seek_bap(sc, id, off)) != 0) 1720 return EIO; 1721 } 1722 1723 cnt = (buflen + 1) / 2; 1724 CSR_READ_MULTI_STREAM_2(sc, AN_DATA0, (u_int16_t *)buf, cnt); 1725 sc->sc_bap_off += cnt * 2; 1726 return 0; 1727 } 1728 1729 static int 1730 an_write_bap(struct an_softc *sc, int id, int off, void *buf, int buflen) 1731 { 1732 int error, cnt; 1733 1734 if (buflen == 0) 1735 return 0; 1736 if (off == -1) 1737 off = sc->sc_bap_off; 1738 if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 1739 if ((error = an_seek_bap(sc, id, off)) != 0) 1740 return EIO; 1741 } 1742 1743 cnt = (buflen + 1) / 2; 1744 CSR_WRITE_MULTI_STREAM_2(sc, AN_DATA0, (u_int16_t *)buf, cnt); 1745 sc->sc_bap_off += cnt * 2; 1746 return 0; 1747 } 1748 1749 static int 1750 an_mwrite_bap(struct an_softc *sc, int id, int off, struct mbuf *m, int totlen) 1751 { 1752 int error, len, cnt; 1753 1754 if (off == -1) 1755 off = sc->sc_bap_off; 1756 if (id != sc->sc_bap_id || off != sc->sc_bap_off) { 1757 if ((error = an_seek_bap(sc, id, off)) != 0) 1758 return EIO; 1759 } 1760 1761 for (len = 0; m != NULL; m = m->m_next) { 1762 if (m->m_len == 0) 1763 continue; 1764 len = min(m->m_len, totlen); 1765 1766 if ((mtod(m, u_long) & 0x1) || (len & 0x1)) { 1767 m_copydata(m, 0, totlen, (void *)&sc->sc_buf.sc_txbuf); 1768 cnt = (totlen + 1) / 2; 1769 CSR_WRITE_MULTI_STREAM_2(sc, AN_DATA0, 1770 sc->sc_buf.sc_val, cnt); 1771 off += cnt * 2; 1772 break; 1773 } 1774 cnt = len / 2; 1775 CSR_WRITE_MULTI_STREAM_2(sc, AN_DATA0, mtod(m, u_int16_t *), 1776 cnt); 1777 off += len; 1778 totlen -= len; 1779 } 1780 sc->sc_bap_off = off; 1781 return 0; 1782 } 1783 1784 static int 1785 an_alloc_fid(struct an_softc *sc, int len, int *idp) 1786 { 1787 int i; 1788 1789 if (an_cmd(sc, AN_CMD_ALLOC_MEM, len)) { 1790 aprint_error_dev(sc->sc_dev, "failed to allocate %d bytes on NIC\n", 1791 len); 1792 return ENOMEM; 1793 } 1794 1795 for (i = 0; i < AN_TIMEOUT; i++) { 1796 if (CSR_READ_2(sc, AN_EVENT_STAT) & AN_EV_ALLOC) 1797 break; 1798 if (i == AN_TIMEOUT) { 1799 printf("%s: timeout in alloc\n", device_xname(sc->sc_dev)); 1800 return ETIMEDOUT; 1801 } 1802 DELAY(10); 1803 } 1804 1805 *idp = CSR_READ_2(sc, AN_ALLOC_FID); 1806 CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_ALLOC); 1807 return 0; 1808 } 1809 1810 static int 1811 an_read_rid(struct an_softc *sc, int rid, void *buf, int *buflenp) 1812 { 1813 int error; 1814 u_int16_t len; 1815 1816 /* Tell the NIC to enter record read mode. */ 1817 error = an_cmd(sc, AN_CMD_ACCESS | AN_ACCESS_READ, rid); 1818 if (error) 1819 return error; 1820 1821 /* length in byte, including length itself */ 1822 error = an_read_bap(sc, rid, 0, &len, sizeof(len)); 1823 if (error) 1824 return error; 1825 1826 len = le16toh(len) - 2; 1827 if (*buflenp < len) { 1828 aprint_error_dev(sc->sc_dev, "record buffer is too small, " 1829 "rid=%x, size=%d, len=%d\n", 1830 rid, *buflenp, len); 1831 return ENOSPC; 1832 } 1833 *buflenp = len; 1834 return an_read_bap(sc, rid, sizeof(len), buf, len); 1835 } 1836 1837 static int 1838 an_write_rid(struct an_softc *sc, int rid, void *buf, int buflen) 1839 { 1840 int error; 1841 u_int16_t len; 1842 1843 /* length in byte, including length itself */ 1844 len = htole16(buflen + 2); 1845 1846 error = an_write_bap(sc, rid, 0, &len, sizeof(len)); 1847 if (error) 1848 return error; 1849 error = an_write_bap(sc, rid, sizeof(len), buf, buflen); 1850 if (error) 1851 return error; 1852 1853 return an_cmd(sc, AN_CMD_ACCESS | AN_ACCESS_WRITE, rid); 1854 } 1855 1856 static int 1857 an_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 1858 { 1859 struct an_softc *sc = (struct an_softc *)ic->ic_ifp->if_softc; 1860 struct ieee80211_node *ni = ic->ic_bss; 1861 int buflen; 1862 1863 DPRINTF(("an_newstate: %s -> %s\n", ieee80211_state_name[ic->ic_state], 1864 ieee80211_state_name[nstate])); 1865 1866 switch (nstate) { 1867 case IEEE80211_S_INIT: 1868 ic->ic_flags &= ~IEEE80211_F_IBSSON; 1869 return (*sc->sc_newstate)(ic, nstate, arg); 1870 1871 case IEEE80211_S_SCAN: 1872 case IEEE80211_S_AUTH: 1873 case IEEE80211_S_ASSOC: 1874 ic->ic_state = nstate; /* NB: skip normal ieee80211 handling */ 1875 return 0; 1876 1877 case IEEE80211_S_RUN: 1878 buflen = sizeof(sc->sc_buf); 1879 an_read_rid(sc, AN_RID_STATUS, &sc->sc_buf, &buflen); 1880 IEEE80211_ADDR_COPY(ni->ni_bssid, 1881 sc->sc_buf.sc_status.an_cur_bssid); 1882 IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid); 1883 ni->ni_chan = &ic->ic_channels[ 1884 le16toh(sc->sc_buf.sc_status.an_cur_channel)]; 1885 ni->ni_esslen = le16toh(sc->sc_buf.sc_status.an_ssidlen); 1886 if (ni->ni_esslen > IEEE80211_NWID_LEN) 1887 ni->ni_esslen = IEEE80211_NWID_LEN; /*XXX*/ 1888 memcpy(ni->ni_essid, sc->sc_buf.sc_status.an_ssid, 1889 ni->ni_esslen); 1890 ni->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B]; /*XXX*/ 1891 if (ic->ic_ifp->if_flags & IFF_DEBUG) { 1892 printf("%s: ", device_xname(sc->sc_dev)); 1893 if (ic->ic_opmode == IEEE80211_M_STA) 1894 printf("associated "); 1895 else 1896 printf("synchronized "); 1897 printf("with %s ssid ", ether_sprintf(ni->ni_bssid)); 1898 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 1899 printf(" channel %u start %uMb\n", 1900 le16toh(sc->sc_buf.sc_status.an_cur_channel), 1901 le16toh(sc->sc_buf.sc_status.an_current_tx_rate)/2); 1902 } 1903 break; 1904 1905 default: 1906 break; 1907 } 1908 return (*sc->sc_newstate)(ic, nstate, arg); 1909 } 1910