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