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