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