awi.c revision 1.10 1 /* $NetBSD: awi.c,v 1.10 2000/03/22 11:22:22 onoe Exp $ */
2
3 /*
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Atsushi Onoe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Driver for AMD 802.11 PCnetMobile firmware.
41 * Uses am79c930 chip driver to talk to firmware running on the am79c930.
42 *
43 * The awi device driver first appeared in NetBSD 1.5.
44 *
45 * The initial version of the driver was written by
46 * Bill Sommerfeld <sommerfeld (at) netbsd.org>.
47 * Then the driver module completely rewritten to support cards with DS phy
48 * and to support adhoc mode by Atsushi Onoe <onoe (at) netbsd.org>
49 */
50
51 #ifdef __NetBSD__
52 #include "opt_inet.h"
53 #include "opt_ns.h"
54 #include "bpfilter.h"
55 #include "rnd.h"
56 #endif
57 #ifdef __FreeBSD__
58 #if __FreeBSD__ >= 3
59 #include "opt_inet.h"
60 #endif
61 #if __FreeBSD__ >= 4
62 #include "bpf.h"
63 #define NBPFILTER NBPF
64 #else
65 #include "bpfilter.h"
66 #endif
67 #endif
68
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/mbuf.h>
73 #include <sys/malloc.h>
74 #include <sys/proc.h>
75 #include <sys/socket.h>
76 #ifdef __FreeBSD__
77 #include <sys/sockio.h>
78 #else
79 #include <sys/ioctl.h>
80 #endif
81 #include <sys/errno.h>
82 #include <sys/syslog.h>
83 #include <sys/select.h>
84 #if defined(__FreeBSD__) && __FreeBSD__ >= 4
85 #include <sys/bus.h>
86 #else
87 #include <sys/device.h>
88 #endif
89 #if NRND > 0
90 #include <sys/rnd.h>
91 #endif
92
93 #include <net/if.h>
94 #include <net/if_dl.h>
95 #ifdef __FreeBSD__
96 #include <net/ethernet.h>
97 #else
98 #include <net/if_ether.h>
99 #endif
100 #include <net/if_media.h>
101 #include <net/if_llc.h>
102 #include <net/if_ieee80211.h>
103
104 #ifdef INET
105 #include <netinet/in.h>
106 #include <netinet/in_systm.h>
107 #include <netinet/in_var.h>
108 #include <netinet/ip.h>
109 #ifdef __NetBSD__
110 #include <netinet/if_inarp.h>
111 #else
112 #include <netinet/if_ether.h>
113 #endif
114 #endif
115
116 #ifdef NS
117 #include <netns/ns.h>
118 #include <netns/ns_if.h>
119 #endif
120
121 #if NBPFILTER > 0
122 #include <net/bpf.h>
123 #include <net/bpfdesc.h>
124 #endif
125
126 #include <machine/cpu.h>
127 #include <machine/bus.h>
128 #ifdef __NetBSD__
129 #include <machine/intr.h>
130 #endif
131 #ifdef __FreeBSD__
132 #include <machine/clock.h>
133 #endif
134
135 #ifdef __NetBSD__
136 #include <dev/ic/am79c930reg.h>
137 #include <dev/ic/am79c930var.h>
138 #include <dev/ic/awireg.h>
139 #include <dev/ic/awivar.h>
140 #include <dev/ic/awictl.h>
141 #endif
142 #ifdef __FreeBSD__
143 #include <dev/awi/am79c930reg.h>
144 #include <dev/awi/am79c930var.h>
145 #include <dev/awi/awireg.h>
146 #include <dev/awi/awivar.h>
147 #include <dev/awi/awictl.h>
148 #endif
149
150 static int awi_ioctl __P((struct ifnet *ifp, u_long cmd, caddr_t data));
151 #ifdef IFM_IEEE80211
152 static int awi_media_rate2opt __P((struct awi_softc *sc, int rate));
153 static int awi_media_opt2rate __P((struct awi_softc *sc, int opt));
154 static int awi_media_change __P((struct ifnet *ifp));
155 static void awi_media_status __P((struct ifnet *ifp, struct ifmediareq *imr));
156 #endif
157 static int awi_drvget __P((struct ifnet *ifp, u_long cmd, caddr_t data));
158 static int awi_drvset __P((struct ifnet *ifp, u_long cmd, caddr_t data));
159 static int awi_init __P((struct awi_softc *sc));
160 static void awi_stop __P((struct awi_softc *sc));
161 static void awi_watchdog __P((struct ifnet *ifp));
162 static void awi_start __P((struct ifnet *ifp));
163 static void awi_txint __P((struct awi_softc *sc));
164 static struct mbuf * awi_fix_txhdr __P((struct awi_softc *sc, struct mbuf *m0));
165 static struct mbuf * awi_fix_rxhdr __P((struct awi_softc *sc, struct mbuf *m0));
166 static void awi_input __P((struct awi_softc *sc, struct mbuf *m, u_int32_t rxts, u_int8_t rssi));
167 static void awi_rxint __P((struct awi_softc *sc));
168 struct mbuf * awi_devget __P((struct awi_softc *sc, u_int32_t off, u_int16_t len));
169 static int awi_init_hw __P((struct awi_softc *sc));
170 static int awi_init_mibs __P((struct awi_softc *sc));
171 static int awi_init_txrx __P((struct awi_softc *sc));
172 static void awi_stop_txrx __P((struct awi_softc *sc));
173 static int awi_init_region __P((struct awi_softc *sc));
174 static int awi_start_scan __P((struct awi_softc *sc));
175 static int awi_next_scan __P((struct awi_softc *sc));
176 static void awi_stop_scan __P((struct awi_softc *sc));
177 static void awi_recv_beacon __P((struct awi_softc *sc, struct mbuf *m0, u_int32_t rxts, u_int8_t rssi));
178 static int awi_set_ss __P((struct awi_softc *sc));
179 static void awi_try_sync __P((struct awi_softc *sc));
180 static void awi_sync_done __P((struct awi_softc *sc));
181 static void awi_send_deauth __P((struct awi_softc *sc));
182 static void awi_send_auth __P((struct awi_softc *sc));
183 static void awi_recv_auth __P((struct awi_softc *sc, struct mbuf *m0));
184 static void awi_send_asreq __P((struct awi_softc *sc, int reassoc));
185 static void awi_recv_asresp __P((struct awi_softc *sc, struct mbuf *m0));
186 static int awi_mib __P((struct awi_softc *sc, u_int8_t cmd, u_int8_t mib));
187 static int awi_cmd_scan __P((struct awi_softc *sc));
188 static int awi_cmd __P((struct awi_softc *sc, u_int8_t cmd));
189 static void awi_cmd_done __P((struct awi_softc *sc));
190 static int awi_next_txd __P((struct awi_softc *sc, int len, u_int32_t *framep, u_int32_t*ntxdp));
191 static int awi_lock __P((struct awi_softc *sc));
192 static void awi_unlock __P((struct awi_softc *sc));
193 static int awi_intr_lock __P((struct awi_softc *sc));
194 static void awi_intr_unlock __P((struct awi_softc *sc));
195 static int awi_cmd_wait __P((struct awi_softc *sc));
196
197 #ifdef AWI_DEBUG
198 static void awi_dump_pkt __P((struct awi_softc *sc, struct mbuf *m, u_int8_t rssi));
199 int awi_verbose = 0;
200 int awi_dump = 0;
201 #define AWI_DUMP_MASK(fc0) (1 << (((fc0) & IEEE80211_FC0_SUBTYPE_MASK) >> 4))
202 int awi_dump_mask = AWI_DUMP_MASK(IEEE80211_FC0_SUBTYPE_BEACON);
203 int awi_dump_hdr = 0;
204 int awi_dump_len = 28;
205 #endif
206
207 #if NBPFILTER > 0
208 #define AWI_BPF_NORM 0
209 #define AWI_BPF_RAW 1
210 #ifdef __FreeBSD__
211 #define AWI_BPF_MTAP(sc, m, raw) do { \
212 if ((sc)->sc_ifp->if_bpf && (sc)->sc_rawbpf == (raw)) \
213 bpf_mtap((sc)->sc_ifp, (m)); \
214 } while (0);
215 #else
216 #define AWI_BPF_MTAP(sc, m, raw) do { \
217 if ((sc)->sc_ifp->if_bpf && (sc)->sc_rawbpf == (raw)) \
218 bpf_mtap((sc)->sc_ifp->if_bpf, (m)); \
219 } while (0);
220 #endif
221 #else
222 #define AWI_BPF_MTAP(sc, m, raw)
223 #endif
224
225 #ifndef llc_snap
226 #define llc_snap llc_un.type_snap
227 #endif
228
229 #ifdef __FreeBSD__
230 #if __FreeBSD__ < 4
231 #define memset(p, v, n) bzero(p, n) /*XXX*/
232 #endif
233
234 #if __FreeBSD__ >= 4
235 devclass_t awi_devclass;
236 #endif
237
238 /* NetBSD compatible functions */
239 static char * ether_sprintf __P((u_int8_t *));
240
241 static char *
242 ether_sprintf(enaddr)
243 u_int8_t *enaddr;
244 {
245 static char strbuf[18];
246
247 sprintf(strbuf, "%6D", enaddr, ":");
248 return strbuf;
249 }
250 #endif
251
252 int
253 awi_attach(sc)
254 struct awi_softc *sc;
255 {
256 struct ifnet *ifp = sc->sc_ifp;
257 #ifdef IFM_IEEE80211
258 int i;
259 u_int8_t *phy_rates;
260 int mword;
261 struct ifmediareq imr;
262 #endif
263 int s;
264 int error;
265
266 s = splnet();
267
268 /*
269 * Even if we can sleep in initialization state,
270 * all other processes (e.g. ifconfig) have to wait for
271 * completion of attaching interface.
272 */
273 sc->sc_busy = 1;
274 sc->sc_status = AWI_ST_INIT;
275 TAILQ_INIT(&sc->sc_scan);
276 error = awi_init_hw(sc);
277 if (error) {
278 sc->sc_invalid = 1;
279 splx(s);
280 return error;
281 }
282 error = awi_init_mibs(sc);
283 splx(s);
284 if (error) {
285 sc->sc_invalid = 1;
286 return error;
287 }
288
289 ifp->if_softc = sc;
290 ifp->if_start = awi_start;
291 ifp->if_ioctl = awi_ioctl;
292 ifp->if_watchdog = awi_watchdog;
293 ifp->if_mtu = ETHERMTU;
294 ifp->if_hdrlen = sizeof(struct ieee80211_frame) +
295 sizeof(struct ether_header);
296 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
297 #ifdef IFF_NOTRAILERS
298 ifp->if_flags |= IFF_NOTRAILERS;
299 #endif
300 #ifdef __NetBSD__
301 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
302 #endif
303 #ifdef __FreeBSD__
304 ifp->if_output = ether_output;
305 ifp->if_snd.ifq_maxlen = ifqmaxlen;
306 memcpy(sc->sc_ec.ac_enaddr, sc->sc_mib_addr.aMAC_Address,
307 ETHER_ADDR_LEN);
308 #endif
309
310 printf("%s: IEEE802.11 (%s %dMbps) address %s\n",
311 sc->sc_dev.dv_xname,
312 sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH ? "FH" : "DS",
313 sc->sc_tx_rate / 10, ether_sprintf(sc->sc_mib_addr.aMAC_Address));
314 if_attach(ifp);
315 #ifdef __FreeBSD__
316 ether_ifattach(ifp);
317 #if NBPFILTER > 0
318 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
319 #endif
320 #else
321 ether_ifattach(ifp, sc->sc_mib_addr.aMAC_Address);
322 #if NBPFILTER > 0
323 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
324 #endif
325 #endif
326
327 #ifdef IFM_IEEE80211
328 ifmedia_init(&sc->sc_media, 0, awi_media_change, awi_media_status);
329 phy_rates = sc->sc_mib_phy.aSuprt_Data_Rates;
330 for (i = 0; i < phy_rates[1]; i++) {
331 mword = awi_media_rate2opt(sc, AWI_80211_RATE(phy_rates[2 + i]));
332 if (mword == 0)
333 continue;
334 mword |= IFM_IEEE80211;
335 ifmedia_add(&sc->sc_media, mword, 0, NULL);
336 ifmedia_add(&sc->sc_media,
337 mword | IFM_IEEE80211_ADHOC, 0, NULL);
338 ifmedia_add(&sc->sc_media,
339 mword | IFM_IEEE80211_ADHOC | IFM_FLAG0, 0, NULL);
340 }
341 awi_media_status(ifp, &imr);
342 ifmedia_set(&sc->sc_media, imr.ifm_active);
343 #endif
344
345 /* ready to accept ioctl */
346 awi_unlock(sc);
347 return 0;
348 }
349
350 #ifdef __NetBSD__
351 int
352 awi_detach(sc)
353 struct awi_softc *sc;
354 {
355 struct ifnet *ifp = sc->sc_ifp;
356 int s;
357
358 s = splnet();
359 sc->sc_invalid = 1;
360 awi_stop(sc);
361 while (sc->sc_sleep_cnt > 0) {
362 wakeup(sc);
363 (void)tsleep(sc, PWAIT, "awidet", 1);
364 }
365 #if NBPFILTER > 0
366 bpfdetach(ifp);
367 #endif
368 #ifdef IFM_IEEE80211
369 ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY);
370 #endif
371 ether_ifdetach(ifp);
372 if_detach(ifp);
373 if (sc->sc_enabled) {
374 if (sc->sc_disable)
375 (*sc->sc_disable)(sc);
376 sc->sc_enabled = 0;
377 }
378 splx(s);
379 return 0;
380 }
381
382 int
383 awi_activate(self, act)
384 struct device *self;
385 enum devact act;
386 {
387 struct awi_softc *sc = (struct awi_softc *)self;
388 int s, error = 0;
389
390 s = splnet();
391 switch (act) {
392 case DVACT_ACTIVATE:
393 error = EOPNOTSUPP;
394 break;
395
396 case DVACT_DEACTIVATE:
397 sc->sc_invalid = 1;
398 if_deactivate(sc->sc_ifp);
399 break;
400 }
401 splx(s);
402
403 return error;
404 }
405 #endif /* __NetBSD__ */
406
407 void
408 awi_reset(sc)
409 struct awi_softc *sc;
410 {
411 int s;
412
413 if (!sc->sc_enabled)
414 return;
415 s = splnet();
416 sc->sc_invalid = 1;
417 awi_stop(sc);
418 if (sc->sc_disable)
419 (*sc->sc_disable)(sc);
420 sc->sc_enabled = 0;
421 DELAY(1000);
422 sc->sc_invalid = 0;
423 (void)awi_init(sc);
424 splx(s);
425 }
426
427 static int
428 awi_ioctl(ifp, cmd, data)
429 struct ifnet *ifp;
430 u_long cmd;
431 caddr_t data;
432 {
433 struct awi_softc *sc = ifp->if_softc;
434 struct ifreq *ifr = (struct ifreq *)data;
435 struct ifaddr *ifa = (struct ifaddr *)data;
436 int s, error;
437 size_t nwidlen;
438 u_int8_t nwid[IEEE80211_NWID_LEN + 1];
439 u_int8_t *p;
440
441 s = splnet();
442
443 /* serialize ioctl */
444 error = awi_lock(sc);
445 if (error)
446 goto cantlock;
447 switch (cmd) {
448 case SIOCSIFADDR:
449 ifp->if_flags |= IFF_UP;
450 switch (ifa->ifa_addr->sa_family) {
451 #ifdef INET
452 case AF_INET:
453 arp_ifinit((void *)ifp, ifa);
454 break;
455 #endif
456 }
457 /* FALLTHROUGH */
458 case SIOCSIFFLAGS:
459 sc->sc_format_llc = !(ifp->if_flags & IFF_LINK0);
460 if (!(ifp->if_flags & IFF_UP)) {
461 if (sc->sc_enabled) {
462 awi_stop(sc);
463 if (sc->sc_disable)
464 (*sc->sc_disable)(sc);
465 sc->sc_enabled = 0;
466 }
467 break;
468 }
469 error = awi_init(sc);
470 break;
471
472 case SIOCADDMULTI:
473 case SIOCDELMULTI:
474 #ifdef __FreeBSD__
475 error = ENETRESET; /*XXX*/
476 #else
477 error = (cmd == SIOCADDMULTI) ?
478 ether_addmulti(ifr, &sc->sc_ec) :
479 ether_delmulti(ifr, &sc->sc_ec);
480 #endif
481 if (error == ENETRESET) {
482 if (sc->sc_enabled)
483 error = awi_init(sc);
484 else
485 error = 0;
486 }
487 break;
488 case SIOCSIFMTU:
489 if (ifr->ifr_mtu > ETHERMTU)
490 error = EINVAL;
491 else
492 ifp->if_mtu = ifr->ifr_mtu;
493 break;
494 case SIOCS80211NWID:
495 error = copyinstr(ifr->ifr_data, nwid, sizeof(nwid), &nwidlen);
496 if (error)
497 break;
498 nwidlen--; /* eliminate trailing '\0' */
499 if (nwidlen > IEEE80211_NWID_LEN) {
500 error = EINVAL;
501 break;
502 }
503 if (sc->sc_mib_mac.aDesired_ESS_ID[1] == nwidlen &&
504 memcmp(&sc->sc_mib_mac.aDesired_ESS_ID[2], nwid,
505 nwidlen) == 0)
506 break;
507 memset(sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
508 sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
509 sc->sc_mib_mac.aDesired_ESS_ID[1] = nwidlen;
510 memcpy(&sc->sc_mib_mac.aDesired_ESS_ID[2], nwid, nwidlen);
511 if (sc->sc_enabled) {
512 awi_stop(sc);
513 error = awi_init(sc);
514 }
515 break;
516 case SIOCG80211NWID:
517 if (ifp->if_flags & IFF_RUNNING)
518 p = sc->sc_bss.essid;
519 else
520 p = sc->sc_mib_mac.aDesired_ESS_ID;
521 error = copyout(p + 2, ifr->ifr_data, IEEE80211_NWID_LEN);
522 break;
523 #ifdef IFM_IEEE80211
524 case SIOCSIFMEDIA:
525 case SIOCGIFMEDIA:
526 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
527 break;
528 #endif
529 case SIOCGDRVSPEC:
530 error = awi_drvget(ifp, cmd, data);
531 break;
532 case SIOCSDRVSPEC:
533 error = awi_drvset(ifp, cmd, data);
534 break;
535 default:
536 error = EINVAL;
537 break;
538 }
539 awi_unlock(sc);
540 cantlock:
541 splx(s);
542 return error;
543 }
544
545 #ifdef IFM_IEEE80211
546 static int
547 awi_media_rate2opt(sc, rate)
548 struct awi_softc *sc;
549 int rate;
550 {
551 int mword;
552
553 mword = 0;
554 switch (rate) {
555 case 10:
556 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
557 mword = IFM_IEEE80211_FH1;
558 else
559 mword = IFM_IEEE80211_DS1;
560 break;
561 case 20:
562 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
563 mword = IFM_IEEE80211_FH2;
564 else
565 mword = IFM_IEEE80211_DS2;
566 break;
567 case 55:
568 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_DS)
569 mword = IFM_IEEE80211_DS5;
570 break;
571 case 110:
572 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_DS)
573 mword = IFM_IEEE80211_DS11;
574 break;
575 }
576 return mword;
577 }
578
579 static int
580 awi_media_opt2rate(sc, opt)
581 struct awi_softc *sc;
582 int opt;
583 {
584 int rate;
585
586 rate = 0;
587 switch (IFM_SUBTYPE(opt)) {
588 case IFM_IEEE80211_FH1:
589 case IFM_IEEE80211_FH2:
590 if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_FH)
591 return 0;
592 break;
593 case IFM_IEEE80211_DS1:
594 case IFM_IEEE80211_DS2:
595 case IFM_IEEE80211_DS5:
596 case IFM_IEEE80211_DS11:
597 if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_DS)
598 return 0;
599 break;
600 }
601
602 switch (IFM_SUBTYPE(opt)) {
603 case IFM_IEEE80211_FH1:
604 case IFM_IEEE80211_DS1:
605 rate = 10;
606 break;
607 case IFM_IEEE80211_FH2:
608 case IFM_IEEE80211_DS2:
609 rate = 20;
610 break;
611 case IFM_IEEE80211_DS5:
612 rate = 55;
613 break;
614 case IFM_IEEE80211_DS11:
615 rate = 110;
616 break;
617 }
618 return rate;
619 }
620
621 /*
622 * Called from ifmedia_ioctl via awi_ioctl with lock obtained.
623 */
624 static int
625 awi_media_change(ifp)
626 struct ifnet *ifp;
627 {
628 struct awi_softc *sc = ifp->if_softc;
629 struct ifmedia_entry *ime;
630 u_int8_t *phy_rates;
631 int i, rate, error;
632
633 error = 0;
634 ime = sc->sc_media.ifm_cur;
635 rate = awi_media_opt2rate(sc, ime->ifm_media);
636 if (rate == 0)
637 return EINVAL;
638 if (rate != sc->sc_tx_rate) {
639 phy_rates = sc->sc_mib_phy.aSuprt_Data_Rates;
640 for (i = 0; i < phy_rates[1]; i++) {
641 if (rate == AWI_80211_RATE(phy_rates[2 + i]))
642 break;
643 }
644 if (i == phy_rates[1])
645 return EINVAL;
646 }
647 if (ime->ifm_media & IFM_IEEE80211_ADHOC) {
648 sc->sc_mib_local.Network_Mode = 0;
649 sc->sc_no_bssid = (ime->ifm_media & IFM_FLAG0) ? 1 : 0;
650 } else {
651 sc->sc_mib_local.Network_Mode = 1;
652 }
653 if (sc->sc_enabled) {
654 awi_stop(sc);
655 error = awi_init(sc);
656 }
657 return error;
658 }
659
660 static void
661 awi_media_status(ifp, imr)
662 struct ifnet *ifp;
663 struct ifmediareq *imr;
664 {
665 struct awi_softc *sc = ifp->if_softc;
666
667 imr->ifm_status = IFM_AVALID;
668 if (ifp->if_flags & IFF_RUNNING)
669 imr->ifm_status |= IFM_ACTIVE;
670 imr->ifm_active = IFM_IEEE80211;
671 imr->ifm_active |= awi_media_rate2opt(sc, sc->sc_tx_rate);
672 if (sc->sc_mib_local.Network_Mode == 0) {
673 imr->ifm_active |= IFM_IEEE80211_ADHOC;
674 if (sc->sc_no_bssid)
675 imr->ifm_active |= IFM_FLAG0;
676 }
677 }
678 #endif /* IFM_IEEE80211 */
679
680 static int
681 awi_drvget(ifp, cmd, data)
682 struct ifnet *ifp;
683 u_long cmd;
684 caddr_t data;
685 {
686 struct awi_softc *sc = ifp->if_softc;
687 struct ifdrv *ifd = (struct ifdrv *)data;
688 u_int8_t buf[AWICTL_BUFSIZE];
689 u_int8_t *essid;
690 int error = 0;
691
692 switch (ifd->ifd_cmd) {
693 case AWICTL_REGION:
694 if (ifd->ifd_len < 1)
695 return ENOSPC;
696 ifd->ifd_len = 1;
697 buf[0] = sc->sc_mib_phy.aCurrent_Reg_Domain;
698 break;
699 case AWICTL_CHANSET:
700 if (ifd->ifd_len < 3)
701 return ENOSPC;
702 ifd->ifd_len = 3;
703 buf[0] = sc->sc_bss.chanset;
704 buf[1] = sc->sc_scan_min;
705 buf[2] = sc->sc_scan_max;
706 break;
707 case AWICTL_RAWBPF:
708 if (ifd->ifd_len < 1)
709 return ENOSPC;
710 ifd->ifd_len = 1;
711 buf[0] = sc->sc_rawbpf;
712 break;
713 case AWICTL_DESSID:
714 case AWICTL_CESSID:
715 if (ifd->ifd_cmd == AWICTL_DESSID)
716 essid = sc->sc_mib_mac.aDesired_ESS_ID;
717 else
718 essid = sc->sc_bss.essid;
719 if (ifd->ifd_len < essid[1])
720 return ENOSPC;
721 ifd->ifd_len = essid[1];
722 if (ifd->ifd_len > 0)
723 memcpy(buf, essid, ifd->ifd_len);
724 break;
725 case AWICTL_MODE:
726 if (ifd->ifd_len < 1)
727 return ENOSPC;
728 ifd->ifd_len = 1;
729 if (sc->sc_mib_local.Network_Mode == 0) {
730 if (sc->sc_no_bssid)
731 buf[0] = AWICTL_MODE_NOBSSID;
732 else
733 buf[0] = AWICTL_MODE_ADHOC;
734 } else
735 buf[0] = AWICTL_MODE_INFRA;
736 break;
737 default:
738 error = EINVAL;
739 break;
740 }
741 if (error == 0 && ifd->ifd_len > 0)
742 error = copyout(ifd->ifd_data, buf, ifd->ifd_len);
743 return error;
744 }
745
746 static int
747 awi_drvset(ifp, cmd, data)
748 struct ifnet *ifp;
749 u_long cmd;
750 caddr_t data;
751 {
752 struct awi_softc *sc = ifp->if_softc;
753 struct ifdrv *ifd = (struct ifdrv *)data;
754 u_int8_t buf[AWICTL_BUFSIZE];
755 u_int8_t oregion;
756 int error = 0;
757
758 if (ifd->ifd_len > sizeof(buf))
759 return EINVAL;
760 error = copyin(ifd->ifd_data, buf, ifd->ifd_len);
761 if (error)
762 return error;
763
764 switch (ifd->ifd_cmd) {
765 case AWICTL_REGION:
766 if (ifd->ifd_len != 1)
767 return EINVAL;
768 oregion = sc->sc_mib_phy.aCurrent_Reg_Domain;
769 if (buf[0] == oregion)
770 break;
771 sc->sc_mib_phy.aCurrent_Reg_Domain = buf[0];
772 error = awi_init_region(sc);
773 if (error) {
774 sc->sc_mib_phy.aCurrent_Reg_Domain = oregion;
775 break;
776 }
777 if (sc->sc_enabled) {
778 awi_stop(sc);
779 error = awi_init(sc);
780 }
781 break;
782 case AWICTL_CHANSET:
783 if (ifd->ifd_len != 3)
784 return EINVAL;
785 /* reset scan min/max */
786 awi_init_region(sc);
787 if (buf[0] < sc->sc_scan_min || buf[0] > sc->sc_scan_max ||
788 buf[1] < sc->sc_scan_min || buf[1] > sc->sc_scan_max ||
789 buf[2] < sc->sc_scan_min || buf[2] > sc->sc_scan_max)
790 return EINVAL;
791 sc->sc_scan_cur = buf[0];
792 sc->sc_scan_min = buf[1];
793 sc->sc_scan_max = buf[2];
794 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
795 sc->sc_scan_set = sc->sc_scan_cur % 3 + 1;
796 if (sc->sc_enabled) {
797 awi_stop(sc);
798 error = awi_init(sc);
799 }
800 break;
801 case AWICTL_RAWBPF:
802 if (ifd->ifd_len != 1)
803 return EINVAL;
804 sc->sc_rawbpf = buf[0];
805 break;
806 case AWICTL_DESSID:
807 if (ifd->ifd_len > IEEE80211_NWID_LEN)
808 return EINVAL;
809 if (sc->sc_mib_mac.aDesired_ESS_ID[1] == ifd->ifd_len &&
810 memcmp(&sc->sc_mib_mac.aDesired_ESS_ID[2], buf,
811 ifd->ifd_len) == 0)
812 break;
813 memset(sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
814 sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
815 sc->sc_mib_mac.aDesired_ESS_ID[1] = ifd->ifd_len;
816 memcpy(&sc->sc_mib_mac.aDesired_ESS_ID[2], buf, ifd->ifd_len);
817 if (sc->sc_enabled) {
818 awi_stop(sc);
819 error = awi_init(sc);
820 }
821 break;
822 case AWICTL_CESSID:
823 error = EINVAL;
824 break;
825 case AWICTL_MODE:
826 switch (buf[0]) {
827 case AWICTL_MODE_INFRA:
828 sc->sc_mib_local.Network_Mode = 1;
829 sc->sc_no_bssid = 0;
830 break;
831 case AWICTL_MODE_ADHOC:
832 sc->sc_mib_local.Network_Mode = 0;
833 sc->sc_no_bssid = 0;
834 break;
835 case AWICTL_MODE_NOBSSID:
836 sc->sc_mib_local.Network_Mode = 0;
837 sc->sc_no_bssid = 1;
838 break;
839 default:
840 return EINVAL;
841 }
842 if (sc->sc_enabled) {
843 awi_stop(sc);
844 error = awi_init(sc);
845 }
846 break;
847 default:
848 error = EINVAL;
849 break;
850 }
851 return error;
852 }
853
854 int
855 awi_intr(arg)
856 void *arg;
857 {
858 struct awi_softc *sc = arg;
859 u_int16_t status;
860 int error, handled = 0, ocansleep;
861
862 if (sc->sc_invalid)
863 return 0;
864
865 am79c930_gcr_setbits(&sc->sc_chip,
866 AM79C930_GCR_DISPWDN | AM79C930_GCR_ECINT);
867 awi_write_1(sc, AWI_DIS_PWRDN, 1);
868 ocansleep = sc->sc_cansleep;
869 sc->sc_cansleep = 0;
870
871 for (;;) {
872 error = awi_intr_lock(sc);
873 if (error)
874 break;
875 status = awi_read_1(sc, AWI_INTSTAT);
876 awi_write_1(sc, AWI_INTSTAT, 0);
877 awi_write_1(sc, AWI_INTSTAT, 0);
878 status |= awi_read_1(sc, AWI_INTSTAT2) << 8;
879 awi_write_1(sc, AWI_INTSTAT2, 0);
880 DELAY(10);
881 awi_intr_unlock(sc);
882 if (!sc->sc_cmd_inprog)
883 status &= ~AWI_INT_CMD; /* make sure */
884 if (status == 0)
885 break;
886 handled = 1;
887 if (status & AWI_INT_RX)
888 awi_rxint(sc);
889 if (status & AWI_INT_TX)
890 awi_txint(sc);
891 if (status & AWI_INT_CMD)
892 awi_cmd_done(sc);
893 if (status & AWI_INT_SCAN_CMPLT) {
894 if (sc->sc_status == AWI_ST_SCAN &&
895 sc->sc_mgt_timer > 0)
896 (void)awi_next_scan(sc);
897 }
898 }
899 sc->sc_cansleep = ocansleep;
900 am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_DISPWDN);
901 awi_write_1(sc, AWI_DIS_PWRDN, 0);
902 return handled;
903 }
904
905 static int
906 awi_init(sc)
907 struct awi_softc *sc;
908 {
909 int error, ostatus;
910 int n;
911 struct ifnet *ifp = sc->sc_ifp;
912 #ifdef __FreeBSD__
913 struct ifmultiaddr *ifma;
914 #else
915 struct ether_multi *enm;
916 struct ether_multistep step;
917 #endif
918
919 n = 0;
920 ifp->if_flags |= IFF_ALLMULTI;
921 sc->sc_mib_local.Accept_All_Multicast_Dis = 0;
922 if (ifp->if_flags & IFF_PROMISC) {
923 sc->sc_mib_mac.aPromiscuous_Enable = 1;
924 goto set_mib;
925 }
926 sc->sc_mib_mac.aPromiscuous_Enable = 0;
927 ifp->if_flags &= ~IFF_ALLMULTI;
928 #ifdef __FreeBSD__
929 if (ifp->if_amcount != 0)
930 goto set_mib;
931 for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma != NULL;
932 ifma = LIST_NEXT(ifma, ifma_link)) {
933 if (ifma->ifma_addr->sa_family != AF_LINK)
934 continue;
935 if (n == AWI_GROUP_ADDR_SIZE)
936 goto set_mib;
937 memcpy(sc->sc_mib_addr.aGroup_Addresses[n],
938 LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
939 ETHER_ADDR_LEN);
940 n++;
941 }
942 #else
943 ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
944 while (enm != NULL) {
945 if (n == AWI_GROUP_ADDR_SIZE ||
946 memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)
947 != 0)
948 goto set_mib;
949 memcpy(sc->sc_mib_addr.aGroup_Addresses[n], enm->enm_addrlo,
950 ETHER_ADDR_LEN);
951 n++;
952 ETHER_NEXT_MULTI(step, enm);
953 }
954 #endif
955 ifp->if_flags &= ~IFF_ALLMULTI;
956 sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
957
958 set_mib:
959 if (!sc->sc_enabled) {
960 sc->sc_enabled = 1;
961 if (sc->sc_enable)
962 (*sc->sc_enable)(sc);
963 sc->sc_status = AWI_ST_INIT;
964 error = awi_init_hw(sc);
965 if (error)
966 return error;
967 }
968 ostatus = sc->sc_status;
969 sc->sc_status = AWI_ST_INIT;
970 if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_LOCAL)) != 0 ||
971 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_ADDR)) != 0 ||
972 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MAC)) != 0 ||
973 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT)) != 0 ||
974 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_PHY)) != 0) {
975 awi_stop(sc);
976 return error;
977 }
978 if (ifp->if_flags & IFF_RUNNING)
979 sc->sc_status = AWI_ST_RUNNING;
980 else {
981 if (ostatus == AWI_ST_INIT) {
982 error = awi_init_txrx(sc);
983 if (error)
984 return error;
985 }
986 error = awi_start_scan(sc);
987 }
988 return error;
989 }
990
991 static void
992 awi_stop(sc)
993 struct awi_softc *sc;
994 {
995 struct ifnet *ifp = sc->sc_ifp;
996 struct awi_bss *bp;
997 struct mbuf *m;
998
999 sc->sc_status = AWI_ST_INIT;
1000 if (!sc->sc_invalid) {
1001 (void)awi_cmd_wait(sc);
1002 if (sc->sc_mib_local.Network_Mode &&
1003 sc->sc_status > AWI_ST_AUTH)
1004 awi_send_deauth(sc);
1005 awi_stop_txrx(sc);
1006 }
1007 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
1008 ifp->if_timer = 0;
1009 sc->sc_tx_timer = sc->sc_rx_timer = sc->sc_mgt_timer = 0;
1010 for (;;) {
1011 IF_DEQUEUE(&sc->sc_mgtq, m);
1012 if (m == NULL)
1013 break;
1014 m_freem(m);
1015 }
1016 for (;;) {
1017 IF_DEQUEUE(&ifp->if_snd, m);
1018 if (m == NULL)
1019 break;
1020 m_freem(m);
1021 }
1022 while ((bp = TAILQ_FIRST(&sc->sc_scan)) != NULL)
1023 TAILQ_REMOVE(&sc->sc_scan, bp, list);
1024 }
1025
1026 static void
1027 awi_watchdog(ifp)
1028 struct ifnet *ifp;
1029 {
1030 struct awi_softc *sc = ifp->if_softc;
1031 int ocansleep;
1032
1033 if (sc->sc_invalid) {
1034 ifp->if_timer = 0;
1035 return;
1036 }
1037
1038 ocansleep = sc->sc_cansleep;
1039 sc->sc_cansleep = 0;
1040 if (sc->sc_tx_timer && --sc->sc_tx_timer == 0) {
1041 printf("%s: transmit timeout\n", sc->sc_dev.dv_xname);
1042 awi_txint(sc);
1043 }
1044 if (sc->sc_rx_timer && --sc->sc_rx_timer == 0) {
1045 printf("%s: no recent beacons from %s; rescanning\n",
1046 sc->sc_dev.dv_xname, ether_sprintf(sc->sc_bss.bssid));
1047 awi_start_scan(sc);
1048 }
1049 if (sc->sc_mgt_timer && --sc->sc_mgt_timer == 0) {
1050 switch (sc->sc_status) {
1051 case AWI_ST_SCAN:
1052 awi_stop_scan(sc);
1053 break;
1054 case AWI_ST_AUTH:
1055 case AWI_ST_ASSOC:
1056 /* restart scan */
1057 awi_start_scan(sc);
1058 break;
1059 default:
1060 break;
1061 }
1062 }
1063
1064 if (sc->sc_tx_timer == 0 && sc->sc_rx_timer == 0 &&
1065 sc->sc_mgt_timer == 0)
1066 ifp->if_timer = 0;
1067 else
1068 ifp->if_timer = 1;
1069 sc->sc_cansleep = ocansleep;
1070 }
1071
1072 static void
1073 awi_start(ifp)
1074 struct ifnet *ifp;
1075 {
1076 struct awi_softc *sc = ifp->if_softc;
1077 struct mbuf *m0, *m;
1078 u_int32_t txd, frame, ntxd;
1079 u_int8_t rate;
1080 int len, sent = 0;
1081
1082 for (;;) {
1083 txd = sc->sc_txnext;
1084 IF_DEQUEUE(&sc->sc_mgtq, m0);
1085 if (m0 != NULL) {
1086 if (awi_next_txd(sc, m0->m_pkthdr.len, &frame, &ntxd)) {
1087 IF_PREPEND(&sc->sc_mgtq, m0);
1088 ifp->if_flags |= IFF_OACTIVE;
1089 break;
1090 }
1091 } else {
1092 if (!(ifp->if_flags & IFF_RUNNING))
1093 break;
1094 IF_DEQUEUE(&ifp->if_snd, m0);
1095 if (m0 == NULL)
1096 break;
1097 if (awi_next_txd(sc, m0->m_pkthdr.len +
1098 sizeof(struct ieee80211_frame), &frame, &ntxd)) {
1099 IF_PREPEND(&ifp->if_snd, m0);
1100 ifp->if_flags |= IFF_OACTIVE;
1101 break;
1102 }
1103 AWI_BPF_MTAP(sc, m0, AWI_BPF_NORM);
1104 m0 = awi_fix_txhdr(sc, m0);
1105 if (m0 == NULL) {
1106 ifp->if_oerrors++;
1107 continue;
1108 }
1109 ifp->if_opackets++;
1110 }
1111 AWI_BPF_MTAP(sc, m0, AWI_BPF_RAW);
1112 len = 0;
1113 for (m = m0; m != NULL; m = m->m_next) {
1114 awi_write_bytes(sc, frame + len, mtod(m, u_int8_t *),
1115 m->m_len);
1116 len += m->m_len;
1117 }
1118 m_freem(m0);
1119 rate = sc->sc_tx_rate; /*XXX*/
1120 awi_write_1(sc, ntxd + AWI_TXD_STATE, 0);
1121 awi_write_4(sc, txd + AWI_TXD_START, frame);
1122 awi_write_4(sc, txd + AWI_TXD_NEXT, ntxd);
1123 awi_write_4(sc, txd + AWI_TXD_LENGTH, len);
1124 awi_write_1(sc, txd + AWI_TXD_RATE, rate);
1125 awi_write_4(sc, txd + AWI_TXD_NDA, 0);
1126 awi_write_4(sc, txd + AWI_TXD_NRA, 0);
1127 awi_write_1(sc, txd + AWI_TXD_STATE, AWI_TXD_ST_OWN);
1128 sc->sc_txnext = ntxd;
1129 sent++;
1130 }
1131 if (sent) {
1132 if (sc->sc_tx_timer == 0)
1133 sc->sc_tx_timer = 5;
1134 ifp->if_timer = 1;
1135 #ifdef AWI_DEBUG
1136 if (awi_verbose)
1137 printf("awi_start: sent %d txdone %d txnext %d txbase %d txend %d\n", sent, sc->sc_txdone, sc->sc_txnext, sc->sc_txbase, sc->sc_txend);
1138 #endif
1139 }
1140 }
1141
1142 static void
1143 awi_txint(sc)
1144 struct awi_softc *sc;
1145 {
1146 struct ifnet *ifp = sc->sc_ifp;
1147 u_int8_t flags;
1148
1149 while (sc->sc_txdone != sc->sc_txnext) {
1150 flags = awi_read_1(sc, sc->sc_txdone + AWI_TXD_STATE);
1151 if ((flags & AWI_TXD_ST_OWN) || !(flags & AWI_TXD_ST_DONE))
1152 break;
1153 if (flags & AWI_TXD_ST_ERROR)
1154 ifp->if_oerrors++;
1155 sc->sc_txdone = awi_read_4(sc, sc->sc_txdone + AWI_TXD_NEXT) &
1156 0x7fff;
1157 }
1158 sc->sc_tx_timer = 0;
1159 ifp->if_flags &= ~IFF_OACTIVE;
1160 #ifdef AWI_DEBUG
1161 if (awi_verbose)
1162 printf("awi_txint: txdone %d txnext %d txbase %d txend %d\n",
1163 sc->sc_txdone, sc->sc_txnext, sc->sc_txbase, sc->sc_txend);
1164 #endif
1165 awi_start(ifp);
1166 }
1167
1168 static struct mbuf *
1169 awi_fix_txhdr(sc, m0)
1170 struct awi_softc *sc;
1171 struct mbuf *m0;
1172 {
1173 struct ether_header eh;
1174 struct ieee80211_frame *wh;
1175 struct llc *llc;
1176
1177 if (m0->m_len < sizeof(eh)) {
1178 m0 = m_pullup(m0, sizeof(eh));
1179 if (m0 == NULL)
1180 return NULL;
1181 }
1182 memcpy(&eh, mtod(m0, caddr_t), sizeof(eh));
1183 if (sc->sc_format_llc) {
1184 m_adj(m0, sizeof(struct ether_header) - sizeof(struct llc));
1185 llc = mtod(m0, struct llc *);
1186 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1187 llc->llc_control = LLC_UI;
1188 llc->llc_snap.org_code[0] = llc->llc_snap.org_code[1] =
1189 llc->llc_snap.org_code[2] = 0;
1190 llc->llc_snap.ether_type = eh.ether_type;
1191 }
1192 M_PREPEND(m0, sizeof(struct ieee80211_frame), M_DONTWAIT);
1193 if (m0 == NULL)
1194 return NULL;
1195 wh = mtod(m0, struct ieee80211_frame *);
1196
1197 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1198 LE_WRITE_2(wh->i_dur, 0);
1199 LE_WRITE_2(wh->i_seq, 0);
1200 if (sc->sc_mib_local.Network_Mode) {
1201 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1202 memcpy(wh->i_addr1, sc->sc_bss.bssid, ETHER_ADDR_LEN);
1203 memcpy(wh->i_addr2, eh.ether_shost, ETHER_ADDR_LEN);
1204 memcpy(wh->i_addr3, eh.ether_dhost, ETHER_ADDR_LEN);
1205 } else {
1206 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1207 memcpy(wh->i_addr1, eh.ether_dhost, ETHER_ADDR_LEN);
1208 memcpy(wh->i_addr2, eh.ether_shost, ETHER_ADDR_LEN);
1209 memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
1210 }
1211 return m0;
1212 }
1213
1214 static struct mbuf *
1215 awi_fix_rxhdr(sc, m0)
1216 struct awi_softc *sc;
1217 struct mbuf *m0;
1218 {
1219 struct ieee80211_frame wh;
1220 struct ether_header *eh;
1221 struct llc *llc;
1222
1223 if (m0->m_len < sizeof(wh)) {
1224 m_freem(m0);
1225 return NULL;
1226 }
1227 llc = (struct llc *)(mtod(m0, caddr_t) + sizeof(wh));
1228 if (llc->llc_dsap == LLC_SNAP_LSAP &&
1229 llc->llc_ssap == LLC_SNAP_LSAP &&
1230 llc->llc_control == LLC_UI &&
1231 llc->llc_snap.org_code[0] == 0 &&
1232 llc->llc_snap.org_code[1] == 0 &&
1233 llc->llc_snap.org_code[2] == 0) {
1234 memcpy(&wh, mtod(m0, caddr_t), sizeof(wh));
1235 m_adj(m0, sizeof(wh) + sizeof(*llc) - sizeof(*eh));
1236 eh = mtod(m0, struct ether_header *);
1237 switch (wh.i_fc[1] & IEEE80211_FC1_DIR_MASK) {
1238 case IEEE80211_FC1_DIR_NODS:
1239 memcpy(eh->ether_dhost, wh.i_addr1, ETHER_ADDR_LEN);
1240 memcpy(eh->ether_shost, wh.i_addr2, ETHER_ADDR_LEN);
1241 break;
1242 case IEEE80211_FC1_DIR_TODS:
1243 memcpy(eh->ether_dhost, wh.i_addr3, ETHER_ADDR_LEN);
1244 memcpy(eh->ether_shost, wh.i_addr2, ETHER_ADDR_LEN);
1245 break;
1246 case IEEE80211_FC1_DIR_FROMDS:
1247 memcpy(eh->ether_dhost, wh.i_addr1, ETHER_ADDR_LEN);
1248 memcpy(eh->ether_shost, wh.i_addr3, ETHER_ADDR_LEN);
1249 break;
1250 case IEEE80211_FC1_DIR_DSTODS:
1251 m_freem(m0);
1252 return NULL;
1253 }
1254 } else {
1255 /* assuming ethernet encapsulation, just strip 802.11 header */
1256 m_adj(m0, sizeof(wh));
1257 }
1258 return m0;
1259 }
1260
1261 static void
1262 awi_input(sc, m, rxts, rssi)
1263 struct awi_softc *sc;
1264 struct mbuf *m;
1265 u_int32_t rxts;
1266 u_int8_t rssi;
1267 {
1268 struct ifnet *ifp = sc->sc_ifp;
1269 struct ieee80211_frame *wh;
1270 #ifndef __NetBSD__
1271 struct ether_header *eh;
1272 #endif
1273
1274 AWI_BPF_MTAP(sc, m, AWI_BPF_RAW);
1275 wh = mtod(m, struct ieee80211_frame *);
1276 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
1277 IEEE80211_FC0_VERSION_0) {
1278 printf("%s; receive packet with wrong version: %x\n",
1279 sc->sc_dev.dv_xname, wh->i_fc[0]);
1280 m_freem(m);
1281 return;
1282 }
1283 #ifdef AWI_DEBUG
1284 if (awi_dump)
1285 awi_dump_pkt(sc, m, rssi);
1286 #endif
1287
1288 if ((sc->sc_mib_local.Network_Mode || !sc->sc_no_bssid) &&
1289 sc->sc_status == AWI_ST_RUNNING) {
1290 if (memcmp(wh->i_addr2, sc->sc_bss.bssid, ETHER_ADDR_LEN) == 0) {
1291 sc->sc_rx_timer = 10;
1292 sc->sc_bss.rssi = rssi;
1293 }
1294 }
1295 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1296 case IEEE80211_FC0_TYPE_DATA:
1297 if (sc->sc_mib_local.Network_Mode) {
1298 if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
1299 IEEE80211_FC1_DIR_FROMDS) {
1300 m_freem(m);
1301 return;
1302 }
1303 } else {
1304 if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
1305 IEEE80211_FC1_DIR_NODS) {
1306 m_freem(m);
1307 return;
1308 }
1309 }
1310 m = awi_fix_rxhdr(sc, m);
1311 if (m == NULL) {
1312 ifp->if_ierrors++;
1313 break;
1314 }
1315 ifp->if_ipackets++;
1316 AWI_BPF_MTAP(sc, m, AWI_BPF_NORM);
1317 #ifdef __NetBSD__
1318 m->m_flags |= M_HASFCS;
1319 (*ifp->if_input)(ifp, m);
1320 #else
1321 eh = mtod(m, struct ether_header *);
1322 m_adj(m, sizeof(*eh));
1323 m_adj(m, -ETHER_CRC_LEN);
1324 ether_input(ifp, eh, m);
1325 #endif
1326 break;
1327 case IEEE80211_FC0_TYPE_MGT:
1328 if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
1329 IEEE80211_FC1_DIR_NODS) {
1330 m_freem(m);
1331 return;
1332 }
1333 switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
1334 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1335 case IEEE80211_FC0_SUBTYPE_BEACON:
1336 awi_recv_beacon(sc, m, rxts, rssi);
1337 break;
1338 case IEEE80211_FC0_SUBTYPE_AUTH:
1339 awi_recv_auth(sc, m);
1340 break;
1341 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1342 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1343 awi_recv_asresp(sc, m);
1344 break;
1345 case IEEE80211_FC0_SUBTYPE_DEAUTH:
1346 if (sc->sc_mib_local.Network_Mode)
1347 awi_send_auth(sc);
1348 break;
1349 case IEEE80211_FC0_SUBTYPE_DISASSOC:
1350 if (sc->sc_mib_local.Network_Mode)
1351 awi_send_asreq(sc, 1);
1352 break;
1353 }
1354 m_freem(m);
1355 break;
1356 case IEEE80211_FC0_TYPE_CTL:
1357 default:
1358 /* should not come here */
1359 m_freem(m);
1360 break;
1361 }
1362 }
1363
1364 static void
1365 awi_rxint(sc)
1366 struct awi_softc *sc;
1367 {
1368 u_int8_t state, rate, rssi;
1369 u_int16_t len;
1370 u_int32_t frame, next, rxts, rxoff;
1371 struct mbuf *m;
1372
1373 rxoff = sc->sc_rxdoff;
1374 for (;;) {
1375 state = awi_read_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE);
1376 if (state & AWI_RXD_ST_OWN)
1377 break;
1378 if (!(state & AWI_RXD_ST_CONSUMED)) {
1379 if (state & AWI_RXD_ST_RXERROR)
1380 sc->sc_ifp->if_ierrors++;
1381 else {
1382 len = awi_read_2(sc, rxoff + AWI_RXD_LEN);
1383 rate = awi_read_1(sc, rxoff + AWI_RXD_RATE);
1384 rssi = awi_read_1(sc, rxoff + AWI_RXD_RSSI);
1385 frame = awi_read_4(sc, rxoff + AWI_RXD_START_FRAME) & 0x7fff;
1386 rxts = awi_read_4(sc, rxoff + AWI_RXD_LOCALTIME);
1387 m = awi_devget(sc, frame, len);
1388 if (state & AWI_RXD_ST_LF)
1389 awi_input(sc, m, rxts, rssi);
1390 else
1391 sc->sc_rxpend = m;
1392 }
1393 state |= AWI_RXD_ST_CONSUMED;
1394 awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1395 }
1396 next = awi_read_4(sc, rxoff + AWI_RXD_NEXT);
1397 if (next & AWI_RXD_NEXT_LAST)
1398 break;
1399 /* make sure the next pointer is correct */
1400 if (next != awi_read_4(sc, rxoff + AWI_RXD_NEXT))
1401 break;
1402 state |= AWI_RXD_ST_OWN;
1403 awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1404 rxoff = next & 0x7fff;
1405 }
1406 sc->sc_rxdoff = rxoff;
1407 }
1408
1409 struct mbuf *
1410 awi_devget(sc, off, len)
1411 struct awi_softc *sc;
1412 u_int32_t off;
1413 u_int16_t len;
1414 {
1415 struct mbuf *m;
1416 struct mbuf *top, **mp = ⊤
1417 u_int tlen;
1418
1419 top = sc->sc_rxpend;
1420 if (top != NULL) {
1421 sc->sc_rxpend = NULL;
1422 top->m_pkthdr.len += len;
1423 while ((m = *mp) != NULL)
1424 mp = &m->m_next;
1425 if (m->m_flags & M_EXT)
1426 tlen = m->m_ext.ext_size;
1427 else if (m->m_flags & M_PKTHDR)
1428 tlen = MHLEN;
1429 else
1430 tlen = MLEN;
1431 tlen -= m->m_len;
1432 if (tlen > len)
1433 tlen = len;
1434 awi_read_bytes(sc, off, mtod(m, u_int8_t *) + m->m_len, tlen);
1435 off += tlen;
1436 len -= tlen;
1437 }
1438
1439 while (len > 0) {
1440 if (top == NULL) {
1441 MGETHDR(m, M_DONTWAIT, MT_DATA);
1442 if (m == NULL)
1443 return NULL;
1444 m->m_pkthdr.rcvif = sc->sc_ifp;
1445 m->m_pkthdr.len = len;
1446 m->m_len = MHLEN;
1447 } else {
1448 MGET(m, M_DONTWAIT, MT_DATA);
1449 if (m == NULL) {
1450 m_freem(top);
1451 return NULL;
1452 }
1453 m->m_len = MLEN;
1454 }
1455 if (len >= MINCLSIZE) {
1456 MCLGET(m, M_DONTWAIT);
1457 if (m->m_flags & M_EXT)
1458 m->m_len = m->m_ext.ext_size;
1459 }
1460 if (m->m_len > len)
1461 m->m_len = len;
1462 awi_read_bytes(sc, off, mtod(m, u_int8_t *), m->m_len);
1463 off += m->m_len;
1464 len -= m->m_len;
1465 *mp = m;
1466 mp = &m->m_next;
1467 }
1468 return top;
1469 }
1470
1471 /*
1472 * Initialize hardware and start firmware to accept commands.
1473 * Called everytime after power on firmware.
1474 */
1475
1476 static int
1477 awi_init_hw(sc)
1478 struct awi_softc *sc;
1479 {
1480 u_int8_t status;
1481 u_int16_t intmask;
1482 int i, error;
1483 u_int8_t banner[AWI_BANNER_LEN];
1484
1485 awi_drvstate(sc, AWI_DRV_RESET);
1486
1487 /* reset firmware */
1488 am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1489 DELAY(100);
1490 awi_write_1(sc, AWI_SELFTEST, 0);
1491 am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1492 DELAY(100);
1493
1494 /* wait for selftest completion */
1495 for (i = 0; ; i++) {
1496 if (i >= AWI_SELFTEST_TIMEOUT*hz/1000) {
1497 printf("%s: failed to complete selftest (timeout)\n",
1498 sc->sc_dev.dv_xname);
1499 return ENXIO;
1500 }
1501 status = awi_read_1(sc, AWI_SELFTEST);
1502 if ((status & 0xf0) == 0xf0)
1503 break;
1504 if (sc->sc_cansleep) {
1505 sc->sc_sleep_cnt++;
1506 (void)tsleep(sc, PWAIT, "awitst", 1);
1507 sc->sc_sleep_cnt--;
1508 } else {
1509 DELAY(1000*1000/hz);
1510 }
1511 }
1512 if (status != AWI_SELFTEST_PASSED) {
1513 printf("%s: failed to complete selftest (code %x)\n",
1514 sc->sc_dev.dv_xname, status);
1515 return ENXIO;
1516 }
1517
1518 /* check banner to confirm firmware write it */
1519 awi_read_bytes(sc, AWI_BANNER, banner, AWI_BANNER_LEN);
1520 if (memcmp(banner, "PCnetMobile:", 12) != 0) {
1521 printf("%s: failed to complete selftest (bad banner)\n",
1522 sc->sc_dev.dv_xname);
1523 for (i = 0; i < AWI_BANNER_LEN; i++)
1524 printf("%s%02x", i ? ":" : "\t", banner[i]);
1525 printf("\n");
1526 return ENXIO;
1527 }
1528
1529 /* initializing interrupt */
1530 error = awi_intr_lock(sc);
1531 if (error)
1532 return error;
1533 intmask = AWI_INT_GROGGY | AWI_INT_SCAN_CMPLT |
1534 AWI_INT_TX | AWI_INT_RX | AWI_INT_CMD;
1535 awi_write_1(sc, AWI_INTMASK, ~intmask & 0xff);
1536 awi_write_1(sc, AWI_INTMASK2, 0);
1537 awi_write_1(sc, AWI_INTSTAT, 0);
1538 awi_write_1(sc, AWI_INTSTAT2, 0);
1539 awi_intr_unlock(sc);
1540 am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_ENECINT);
1541
1542 /* issueing interface test command */
1543 error = awi_cmd(sc, AWI_CMD_NOP);
1544 if (error) {
1545 printf("%s: failed to complete selftest", sc->sc_dev.dv_xname);
1546 if (error != EWOULDBLOCK)
1547 printf(" (error %d)\n", error);
1548 else if (sc->sc_cansleep)
1549 printf(" (lost interrupt)\n");
1550 else
1551 printf(" (command timeout)\n");
1552 }
1553 return error;
1554 }
1555
1556 /*
1557 * Extract the factory default MIB value from firmware and assign the driver
1558 * default value.
1559 * Called once at attaching the interface.
1560 */
1561
1562 static int
1563 awi_init_mibs(sc)
1564 struct awi_softc *sc;
1565 {
1566 int i, error;
1567 u_int8_t *rate;
1568
1569 if ((error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_LOCAL)) != 0 ||
1570 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_ADDR)) != 0 ||
1571 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MAC)) != 0 ||
1572 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MGT)) != 0 ||
1573 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_PHY)) != 0) {
1574 printf("%s: failed to get default mib value (error %d)\n",
1575 sc->sc_dev.dv_xname, error);
1576 return error;
1577 }
1578
1579 rate = sc->sc_mib_phy.aSuprt_Data_Rates;
1580 sc->sc_tx_rate = AWI_RATE_1MBIT;
1581 for (i = 0; i < rate[1]; i++) {
1582 if (AWI_80211_RATE(rate[2 + i]) > sc->sc_tx_rate)
1583 sc->sc_tx_rate = AWI_80211_RATE(rate[2 + i]);
1584 }
1585 awi_init_region(sc);
1586 memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
1587 sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
1588 sc->sc_mib_local.Fragmentation_Dis = 1;
1589 sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
1590
1591 /* allocate buffers */
1592 sc->sc_txbase = AWI_BUFFERS;
1593 sc->sc_txend = sc->sc_txbase +
1594 (AWI_TXD_SIZE + sizeof(struct ieee80211_frame) +
1595 sizeof(struct ether_header) + ETHERMTU) * AWI_NTXBUFS;
1596 LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Offset, sc->sc_txbase);
1597 LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Size,
1598 sc->sc_txend - sc->sc_txbase);
1599 LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Offset, sc->sc_txend);
1600 LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Size,
1601 AWI_BUFFERS_END - sc->sc_txend);
1602 sc->sc_mib_local.Network_Mode = 1;
1603 sc->sc_mib_local.Acting_as_AP = 0;
1604 return 0;
1605 }
1606
1607 /*
1608 * Start transmitter and receiver of firmware
1609 * Called after awi_init_hw() to start operation.
1610 */
1611
1612 static int
1613 awi_init_txrx(sc)
1614 struct awi_softc *sc;
1615 {
1616 int error;
1617
1618 /* start transmitter */
1619 sc->sc_txdone = sc->sc_txnext = sc->sc_txbase;
1620 awi_write_4(sc, sc->sc_txbase + AWI_TXD_START, 0);
1621 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NEXT, 0);
1622 awi_write_4(sc, sc->sc_txbase + AWI_TXD_LENGTH, 0);
1623 awi_write_1(sc, sc->sc_txbase + AWI_TXD_RATE, 0);
1624 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NDA, 0);
1625 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NRA, 0);
1626 awi_write_1(sc, sc->sc_txbase + AWI_TXD_STATE, 0);
1627 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_DATA, sc->sc_txbase);
1628 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_MGT, 0);
1629 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_BCAST, 0);
1630 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_PS, 0);
1631 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_CF, 0);
1632 error = awi_cmd(sc, AWI_CMD_INIT_TX);
1633 if (error)
1634 return error;
1635
1636 /* start receiver */
1637 if (sc->sc_rxpend) {
1638 m_freem(sc->sc_rxpend);
1639 sc->sc_rxpend = NULL;
1640 }
1641 error = awi_cmd(sc, AWI_CMD_INIT_RX);
1642 if (error)
1643 return error;
1644 sc->sc_rxdoff = awi_read_4(sc, AWI_CMD_PARAMS+AWI_CA_IRX_DATA_DESC);
1645 sc->sc_rxmoff = awi_read_4(sc, AWI_CMD_PARAMS+AWI_CA_IRX_PS_DESC);
1646 return 0;
1647 }
1648
1649 static void
1650 awi_stop_txrx(sc)
1651 struct awi_softc *sc;
1652 {
1653
1654 (void)awi_cmd(sc, AWI_CMD_KILL_RX);
1655 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_DATA, 1);
1656 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_MGT, 0);
1657 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_BCAST, 0);
1658 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_PS, 0);
1659 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_CF, 0);
1660 (void)awi_cmd(sc, AWI_CMD_FLUSH_TX);
1661 }
1662
1663 static int
1664 awi_init_region(sc)
1665 struct awi_softc *sc;
1666 {
1667
1668 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1669 switch (sc->sc_mib_phy.aCurrent_Reg_Domain) {
1670 case AWI_REG_DOMAIN_US:
1671 case AWI_REG_DOMAIN_CA:
1672 case AWI_REG_DOMAIN_EU:
1673 sc->sc_scan_min = 0;
1674 sc->sc_scan_max = 77;
1675 break;
1676 case AWI_REG_DOMAIN_ES:
1677 sc->sc_scan_min = 0;
1678 sc->sc_scan_max = 26;
1679 break;
1680 case AWI_REG_DOMAIN_FR:
1681 sc->sc_scan_min = 0;
1682 sc->sc_scan_max = 32;
1683 break;
1684 case AWI_REG_DOMAIN_JP:
1685 sc->sc_scan_min = 6;
1686 sc->sc_scan_max = 17;
1687 break;
1688 default:
1689 return EINVAL;
1690 }
1691 sc->sc_scan_cur = sc->sc_scan_min;
1692 sc->sc_scan_set = sc->sc_scan_cur % 3 + 1;
1693 } else {
1694 switch (sc->sc_mib_phy.aCurrent_Reg_Domain) {
1695 case AWI_REG_DOMAIN_US:
1696 case AWI_REG_DOMAIN_CA:
1697 sc->sc_scan_min = 1;
1698 sc->sc_scan_max = 11;
1699 sc->sc_scan_cur = 3;
1700 break;
1701 case AWI_REG_DOMAIN_EU:
1702 sc->sc_scan_min = 1;
1703 sc->sc_scan_max = 13;
1704 sc->sc_scan_cur = 3;
1705 break;
1706 case AWI_REG_DOMAIN_ES:
1707 sc->sc_scan_min = 10;
1708 sc->sc_scan_max = 11;
1709 sc->sc_scan_cur = 10;
1710 break;
1711 case AWI_REG_DOMAIN_FR:
1712 sc->sc_scan_min = 10;
1713 sc->sc_scan_max = 13;
1714 sc->sc_scan_cur = 10;
1715 break;
1716 case AWI_REG_DOMAIN_JP:
1717 sc->sc_scan_min = 14;
1718 sc->sc_scan_max = 14;
1719 sc->sc_scan_cur = 14;
1720 break;
1721 default:
1722 return EINVAL;
1723 }
1724 }
1725 return 0;
1726 }
1727
1728 static int
1729 awi_start_scan(sc)
1730 struct awi_softc *sc;
1731 {
1732 int error = 0;
1733
1734 if (!sc->sc_mib_local.Network_Mode && sc->sc_no_bssid) {
1735 memset(&sc->sc_bss, 0, sizeof(sc->sc_bss));
1736 sc->sc_bss.rxtime = 0;
1737 memcpy(sc->sc_bss.essid, &sc->sc_mib_mac.aDesired_ESS_ID,
1738 sizeof(sc->sc_bss.essid));
1739 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1740 sc->sc_bss.chanset = sc->sc_scan_set;
1741 sc->sc_bss.pattern = sc->sc_scan_cur;
1742 sc->sc_bss.index = 1;
1743 sc->sc_bss.dwell_time = 19; /*XXX*/
1744 } else
1745 sc->sc_bss.chanset = sc->sc_scan_cur;
1746 sc->sc_status = AWI_ST_SETSS;
1747 error = awi_set_ss(sc);
1748 } else {
1749 if (sc->sc_mib_local.Network_Mode)
1750 awi_drvstate(sc, AWI_DRV_INFSC);
1751 else
1752 awi_drvstate(sc, AWI_DRV_ADHSC);
1753 sc->sc_start_bss = 0;
1754 sc->sc_active_scan = 1;
1755 sc->sc_mgt_timer = AWI_ASCAN_WAIT / 1000;
1756 sc->sc_ifp->if_timer = 1;
1757 sc->sc_status = AWI_ST_SCAN;
1758 error = awi_cmd_scan(sc);
1759 }
1760 return error;
1761 }
1762
1763 static int
1764 awi_next_scan(sc)
1765 struct awi_softc *sc;
1766 {
1767 int error;
1768
1769 for (;;) {
1770 /*
1771 * The pattern parameter for FH phy should be incremented
1772 * by 3. But BayStack 650 Access Points apparently always
1773 * assign hop pattern set parameter to 1 for any pattern.
1774 * So we try all combinations of pattern/set parameters.
1775 * Since this causes no error, it may be a bug of
1776 * PCnetMobile firmware.
1777 */
1778 sc->sc_scan_cur++;
1779 if (sc->sc_scan_cur > sc->sc_scan_max) {
1780 sc->sc_scan_cur = sc->sc_scan_min;
1781 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
1782 sc->sc_scan_set = (sc->sc_scan_set + 1) % 3;
1783 }
1784 error = awi_cmd_scan(sc);
1785 if (error != EINVAL)
1786 break;
1787 }
1788 return error;
1789 }
1790
1791 static void
1792 awi_stop_scan(sc)
1793 struct awi_softc *sc;
1794 {
1795 struct ifnet *ifp = sc->sc_ifp;
1796 struct awi_bss *bp, *sbp;
1797
1798 bp = TAILQ_FIRST(&sc->sc_scan);
1799 if (bp == NULL) {
1800 notfound:
1801 if (sc->sc_active_scan) {
1802 if (ifp->if_flags & IFF_DEBUG)
1803 printf("%s: entering passive scan mode\n",
1804 sc->sc_dev.dv_xname);
1805 sc->sc_active_scan = 0;
1806 }
1807 sc->sc_mgt_timer = AWI_PSCAN_WAIT / 1000;
1808 ifp->if_timer = 1;
1809 (void)awi_next_scan(sc);
1810 return;
1811 }
1812 sbp = NULL;
1813 for (; bp != NULL; bp = TAILQ_NEXT(bp, list)) {
1814 if (bp->fails) {
1815 /*
1816 * The configuration of the access points may change
1817 * during my scan. So we retries to associate with
1818 * it unless there are any suitable AP.
1819 */
1820 if (bp->fails < 3)
1821 continue;
1822 bp->fails = 0;
1823 }
1824 if (sc->sc_mib_mac.aDesired_ESS_ID[1] != 0 &&
1825 memcmp(&sc->sc_mib_mac.aDesired_ESS_ID, bp->essid,
1826 sizeof(bp->essid) != 0))
1827 continue;
1828 /*
1829 * Since the firmware apparently scans not only the specified
1830 * channel of SCAN command but all available channel within
1831 * the region, we should filter out unnecessary responses here.
1832 */
1833 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1834 if (bp->pattern < sc->sc_scan_min ||
1835 bp->pattern > sc->sc_scan_max)
1836 continue;
1837 } else {
1838 if (bp->chanset < sc->sc_scan_min ||
1839 bp->chanset > sc->sc_scan_max)
1840 continue;
1841 }
1842 if (sbp == NULL || bp->rssi > sbp->rssi)
1843 sbp = bp;
1844 }
1845 if (sbp == NULL)
1846 goto notfound;
1847 sc->sc_bss = *sbp;
1848 (void)awi_set_ss(sc);
1849 }
1850
1851 static void
1852 awi_recv_beacon(sc, m0, rxts, rssi)
1853 struct awi_softc *sc;
1854 struct mbuf *m0;
1855 u_int32_t rxts;
1856 u_int8_t rssi;
1857 {
1858 struct ieee80211_frame *wh;
1859 struct awi_bss *bp;
1860 u_int8_t *frame, *eframe;
1861 u_int8_t *tstamp, *capinfo, *ssid, *rates, *parms;
1862 u_int16_t bintval;
1863
1864 if (sc->sc_status != AWI_ST_SCAN)
1865 return;
1866 wh = mtod(m0, struct ieee80211_frame *);
1867
1868 frame = (u_int8_t *)&wh[1];
1869 eframe = mtod(m0, u_int8_t *) + m0->m_len;
1870 /*
1871 * XXX:
1872 * timestamp [8]
1873 * beacon interval [2]
1874 * capability information [2]
1875 * ssid [tlv]
1876 * supported rates [tlv]
1877 * parameter set [tlv]
1878 * ...
1879 */
1880 if (frame + 12 > eframe) {
1881 #ifdef AWI_DEBUG
1882 if (awi_verbose)
1883 printf("awi_recv_beacon: frame too short \n");
1884 #endif
1885 return;
1886 }
1887 tstamp = frame;
1888 frame += 8;
1889 bintval = LE_READ_2(frame);
1890 frame += 2;
1891 capinfo = frame;
1892 frame += 2;
1893
1894 if (sc->sc_mib_local.Network_Mode) {
1895 if (!(capinfo[0] & IEEE80211_CAPINFO_ESS) ||
1896 (capinfo[0] & IEEE80211_CAPINFO_IBSS)) {
1897 #ifdef AWI_DEBUG
1898 if (awi_verbose)
1899 printf("awi_recv_beacon: non ESS \n");
1900 #endif
1901 return;
1902 }
1903 } else {
1904 if ((capinfo[0] & IEEE80211_CAPINFO_ESS) ||
1905 !(capinfo[0] & IEEE80211_CAPINFO_IBSS)) {
1906 #ifdef AWI_DEBUG
1907 if (awi_verbose)
1908 printf("awi_recv_beacon: non IBSS \n");
1909 #endif
1910 return;
1911 }
1912 }
1913
1914 ssid = rates = parms = NULL;
1915 while (frame < eframe) {
1916 switch (*frame) {
1917 case IEEE80211_ELEMID_SSID:
1918 ssid = frame;
1919 break;
1920 case IEEE80211_ELEMID_RATES:
1921 rates = frame;
1922 break;
1923 case IEEE80211_ELEMID_FHPARMS:
1924 case IEEE80211_ELEMID_DSPARMS:
1925 parms = frame;
1926 break;
1927 }
1928 frame += frame[1] + 2;
1929 }
1930 if (ssid == NULL || rates == NULL || parms == NULL) {
1931 #ifdef AWI_DEBUG
1932 if (awi_verbose)
1933 printf("awi_recv_beacon: ssid=%p, rates=%p, parms=%p\n",
1934 ssid, rates, parms);
1935 #endif
1936 return;
1937 }
1938 if (ssid[1] > IEEE80211_NWID_LEN) {
1939 #ifdef AWI_DEBUG
1940 if (awi_verbose)
1941 printf("awi_recv_beacon: bad ssid len: %d from %s\n",
1942 ssid[1], ether_sprintf(wh->i_addr2));
1943 #endif
1944 return;
1945 }
1946
1947 for (bp = TAILQ_FIRST(&sc->sc_scan); bp != NULL;
1948 bp = TAILQ_NEXT(bp, list)) {
1949 if (memcmp(bp->esrc, wh->i_addr2, ETHER_ADDR_LEN) == 0 &&
1950 memcmp(bp->bssid, wh->i_addr3, ETHER_ADDR_LEN) == 0)
1951 break;
1952 }
1953 if (bp == NULL) {
1954 bp = malloc(sizeof(struct awi_bss), M_DEVBUF, M_NOWAIT);
1955 if (bp == NULL)
1956 return;
1957 TAILQ_INSERT_TAIL(&sc->sc_scan, bp, list);
1958 memcpy(bp->esrc, wh->i_addr2, ETHER_ADDR_LEN);
1959 memcpy(bp->bssid, wh->i_addr3, ETHER_ADDR_LEN);
1960 memset(bp->essid, 0, sizeof(bp->essid));
1961 memcpy(bp->essid, ssid, 2 + ssid[1]);
1962 }
1963 bp->rssi = rssi;
1964 bp->rxtime = rxts;
1965 memcpy(bp->timestamp, tstamp, sizeof(bp->timestamp));
1966 bp->interval = bintval;
1967 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1968 bp->chanset = parms[4];
1969 bp->pattern = parms[5];
1970 bp->index = parms[6];
1971 bp->dwell_time = LE_READ_2(parms + 2);
1972 } else {
1973 bp->chanset = parms[2];
1974 bp->pattern = 0;
1975 bp->index = 0;
1976 bp->dwell_time = 0;
1977 }
1978 if (sc->sc_mgt_timer == 0)
1979 awi_stop_scan(sc);
1980 }
1981
1982 static int
1983 awi_set_ss(sc)
1984 struct awi_softc *sc;
1985 {
1986 struct ifnet *ifp = sc->sc_ifp;
1987 struct awi_bss *bp;
1988 int error;
1989
1990 sc->sc_status = AWI_ST_SETSS;
1991 bp = &sc->sc_bss;
1992 if (ifp->if_flags & IFF_DEBUG) {
1993 printf("%s: ch %d pat %d id %d dw %d iv %d bss %s ssid \"%s\"\n",
1994 sc->sc_dev.dv_xname, bp->chanset,
1995 bp->pattern, bp->index, bp->dwell_time, bp->interval,
1996 ether_sprintf(bp->bssid), bp->essid + 2);
1997 }
1998 memcpy(&sc->sc_mib_mgt.aCurrent_BSS_ID, bp->bssid, ETHER_ADDR_LEN);
1999 memcpy(&sc->sc_mib_mgt.aCurrent_ESS_ID, bp->essid,
2000 AWI_ESS_ID_SIZE);
2001 LE_WRITE_2(&sc->sc_mib_mgt.aBeacon_Period, bp->interval);
2002 error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT);
2003 return error;
2004 }
2005
2006 static void
2007 awi_try_sync(sc)
2008 struct awi_softc *sc;
2009 {
2010 struct awi_bss *bp;
2011
2012 sc->sc_status = AWI_ST_SYNC;
2013 bp = &sc->sc_bss;
2014
2015 if (sc->sc_cmd_inprog) {
2016 if (awi_cmd_wait(sc))
2017 return;
2018 }
2019 sc->sc_cmd_inprog = 1;
2020 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_SET, bp->chanset);
2021 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_PATTERN, bp->pattern);
2022 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_IDX, bp->index);
2023 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_STARTBSS,
2024 sc->sc_start_bss ? 1 : 0);
2025 awi_write_2(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_DWELL, bp->dwell_time);
2026 awi_write_2(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_MBZ, 0);
2027 awi_write_bytes(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_TIMESTAMP,
2028 bp->timestamp, 8);
2029 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_REFTIME, bp->rxtime);
2030 (void)awi_cmd(sc, AWI_CMD_SYNC);
2031 }
2032
2033 static void
2034 awi_sync_done(sc)
2035 struct awi_softc *sc;
2036 {
2037 struct ifnet *ifp = sc->sc_ifp;
2038
2039 if (sc->sc_mib_local.Network_Mode) {
2040 awi_drvstate(sc, AWI_DRV_INFSY);
2041 awi_send_auth(sc);
2042 } else {
2043 printf("%s: synced with %s ssid \"%s\" at chanset %d\n",
2044 sc->sc_dev.dv_xname, ether_sprintf(sc->sc_bss.bssid),
2045 sc->sc_bss.essid + 2, sc->sc_bss.chanset);
2046 awi_drvstate(sc, AWI_DRV_ADHSY);
2047 sc->sc_status = AWI_ST_RUNNING;
2048 ifp->if_flags |= IFF_RUNNING;
2049 awi_start(ifp);
2050 }
2051 }
2052
2053 static void
2054 awi_send_deauth(sc)
2055 struct awi_softc *sc;
2056 {
2057 struct ifnet *ifp = sc->sc_ifp;
2058 struct mbuf *m;
2059 struct ieee80211_frame *wh;
2060 u_int8_t *deauth;
2061
2062 MGETHDR(m, M_DONTWAIT, MT_DATA);
2063 if (m == NULL)
2064 return;
2065 if (ifp->if_flags & IFF_DEBUG)
2066 printf("%s: sending deauth to %s\n", sc->sc_dev.dv_xname,
2067 ether_sprintf(sc->sc_bss.bssid));
2068
2069 wh = mtod(m, struct ieee80211_frame *);
2070 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2071 IEEE80211_FC0_SUBTYPE_AUTH;
2072 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2073 LE_WRITE_2(wh->i_dur, 0);
2074 LE_WRITE_2(wh->i_seq, 0);
2075 memcpy(wh->i_addr1, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2076 memcpy(wh->i_addr2, sc->sc_mib_addr.aMAC_Address, ETHER_ADDR_LEN);
2077 memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2078
2079 deauth = (u_int8_t *)&wh[1];
2080 LE_WRITE_2(deauth, IEEE80211_REASON_AUTH_LEAVE);
2081 deauth += 2;
2082
2083 m->m_pkthdr.len = m->m_len = deauth - mtod(m, u_int8_t *);
2084 IF_ENQUEUE(&sc->sc_mgtq, m);
2085 awi_start(ifp);
2086 awi_drvstate(sc, AWI_DRV_INFTOSS);
2087 }
2088
2089 static void
2090 awi_send_auth(sc)
2091 struct awi_softc *sc;
2092 {
2093 struct ifnet *ifp = sc->sc_ifp;
2094 struct mbuf *m;
2095 struct ieee80211_frame *wh;
2096 u_int8_t *auth;
2097
2098 MGETHDR(m, M_DONTWAIT, MT_DATA);
2099 if (m == NULL)
2100 return;
2101 sc->sc_status = AWI_ST_AUTH;
2102 if (ifp->if_flags & IFF_DEBUG)
2103 printf("%s: sending auth to %s\n", sc->sc_dev.dv_xname,
2104 ether_sprintf(sc->sc_bss.bssid));
2105
2106 wh = mtod(m, struct ieee80211_frame *);
2107 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2108 IEEE80211_FC0_SUBTYPE_AUTH;
2109 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2110 LE_WRITE_2(wh->i_dur, 0);
2111 LE_WRITE_2(wh->i_seq, 0);
2112 memcpy(wh->i_addr1, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2113 memcpy(wh->i_addr2, sc->sc_mib_addr.aMAC_Address, ETHER_ADDR_LEN);
2114 memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2115
2116 auth = (u_int8_t *)&wh[1];
2117 /* algorithm number */
2118 LE_WRITE_2(auth, IEEE80211_AUTH_ALG_OPEN);
2119 auth += 2;
2120 /* sequence number */
2121 LE_WRITE_2(auth, 1);
2122 auth += 2;
2123 /* status */
2124 LE_WRITE_2(auth, 0);
2125 auth += 2;
2126
2127 m->m_pkthdr.len = m->m_len = auth - mtod(m, u_int8_t *);
2128 IF_ENQUEUE(&sc->sc_mgtq, m);
2129 awi_start(ifp);
2130
2131 sc->sc_mgt_timer = AWI_TRANS_TIMEOUT / 1000;
2132 ifp->if_timer = 1;
2133 }
2134
2135 static void
2136 awi_recv_auth(sc, m0)
2137 struct awi_softc *sc;
2138 struct mbuf *m0;
2139 {
2140 struct ieee80211_frame *wh;
2141 u_int8_t *auth, *eframe;
2142 struct awi_bss *bp;
2143 u_int16_t status;
2144
2145 wh = mtod(m0, struct ieee80211_frame *);
2146 auth = (u_int8_t *)&wh[1];
2147 eframe = mtod(m0, u_int8_t *) + m0->m_len;
2148 if (sc->sc_ifp->if_flags & IFF_DEBUG)
2149 printf("%s: receive auth from %s\n", sc->sc_dev.dv_xname,
2150 ether_sprintf(wh->i_addr2));
2151
2152 if (!sc->sc_mib_local.Network_Mode) {
2153 /* XXX: 802.11 allow auth for IBSS */
2154 return;
2155 }
2156 if (sc->sc_status != AWI_ST_AUTH)
2157 return;
2158 /* algorithm number */
2159 if (LE_READ_2(auth) != IEEE80211_AUTH_ALG_OPEN)
2160 return;
2161 auth += 2;
2162 /* sequence number */
2163 if (LE_READ_2(auth) != 2)
2164 return;
2165 auth += 2;
2166 /* status */
2167 status = LE_READ_2(auth);
2168 if (status != 0) {
2169 printf("%s: authentication failed (reason %d)\n",
2170 sc->sc_dev.dv_xname, status);
2171 for (bp = TAILQ_FIRST(&sc->sc_scan); bp != NULL;
2172 bp = TAILQ_NEXT(bp, list)) {
2173 if (memcmp(bp->esrc, sc->sc_bss.esrc, ETHER_ADDR_LEN)
2174 == 0) {
2175 bp->fails++;
2176 break;
2177 }
2178 }
2179 return;
2180 }
2181 sc->sc_mgt_timer = 0;
2182 awi_drvstate(sc, AWI_DRV_INFAUTH);
2183 awi_send_asreq(sc, 0);
2184 }
2185
2186 static void
2187 awi_send_asreq(sc, reassoc)
2188 struct awi_softc *sc;
2189 int reassoc;
2190 {
2191 struct ifnet *ifp = sc->sc_ifp;
2192 struct mbuf *m;
2193 struct ieee80211_frame *wh;
2194 u_int16_t lintval;
2195 u_int8_t *asreq;
2196
2197 MGETHDR(m, M_DONTWAIT, MT_DATA);
2198 if (m == NULL)
2199 return;
2200 sc->sc_status = AWI_ST_ASSOC;
2201 if (ifp->if_flags & IFF_DEBUG)
2202 printf("%s: sending %sassoc req to %s\n", sc->sc_dev.dv_xname,
2203 reassoc ? "re" : "",
2204 ether_sprintf(sc->sc_bss.bssid));
2205
2206 wh = mtod(m, struct ieee80211_frame *);
2207 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT;
2208 if (reassoc)
2209 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_REASSOC_REQ;
2210 else
2211 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_ASSOC_REQ;
2212 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2213 LE_WRITE_2(wh->i_dur, 0);
2214 LE_WRITE_2(wh->i_seq, 0);
2215 memcpy(wh->i_addr1, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2216 memcpy(wh->i_addr2, sc->sc_mib_addr.aMAC_Address, ETHER_ADDR_LEN);
2217 memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2218
2219 asreq = (u_int8_t *)&wh[1];
2220
2221 /* capability info */
2222 LE_WRITE_2(asreq, IEEE80211_CAPINFO_CF_POLLABLE);
2223 asreq += 2;
2224 /* listen interval */
2225 lintval = LE_READ_2(&sc->sc_mib_mgt.aListen_Interval);
2226 LE_WRITE_2(asreq, lintval);
2227 asreq += 2;
2228 if (reassoc) {
2229 /* current AP address */
2230 memcpy(asreq, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2231 asreq += ETHER_ADDR_LEN;
2232 }
2233 /* ssid */
2234 memcpy(asreq, sc->sc_bss.essid, 2 + sc->sc_bss.essid[1]);
2235 asreq += 2 + asreq[1];
2236 /* supported rates */
2237 memcpy(asreq, &sc->sc_mib_phy.aSuprt_Data_Rates, 4);
2238 asreq += 2 + asreq[1];
2239
2240 m->m_pkthdr.len = m->m_len = asreq - mtod(m, u_int8_t *);
2241 IF_ENQUEUE(&sc->sc_mgtq, m);
2242 awi_start(ifp);
2243
2244 sc->sc_mgt_timer = AWI_TRANS_TIMEOUT / 1000;
2245 ifp->if_timer = 1;
2246 }
2247
2248 static void
2249 awi_recv_asresp(sc, m0)
2250 struct awi_softc *sc;
2251 struct mbuf *m0;
2252 {
2253 struct ieee80211_frame *wh;
2254 u_int8_t *asresp, *eframe;
2255 u_int16_t status;
2256 u_int8_t rate, *phy_rates;
2257 struct awi_bss *bp;
2258 int i, j;
2259
2260 wh = mtod(m0, struct ieee80211_frame *);
2261 asresp = (u_int8_t *)&wh[1];
2262 eframe = mtod(m0, u_int8_t *) + m0->m_len;
2263 if (sc->sc_ifp->if_flags & IFF_DEBUG)
2264 printf("%s: receive assoc resp from %s\n", sc->sc_dev.dv_xname,
2265 ether_sprintf(wh->i_addr2));
2266
2267 if (!sc->sc_mib_local.Network_Mode)
2268 return;
2269
2270 if (sc->sc_status != AWI_ST_ASSOC)
2271 return;
2272 /* capability info */
2273 asresp += 2;
2274 /* status */
2275 status = LE_READ_2(asresp);
2276 if (status != 0) {
2277 printf("%s: association failed (reason %d)\n",
2278 sc->sc_dev.dv_xname, status);
2279 for (bp = TAILQ_FIRST(&sc->sc_scan); bp != NULL;
2280 bp = TAILQ_NEXT(bp, list)) {
2281 if (memcmp(bp->esrc, sc->sc_bss.esrc, ETHER_ADDR_LEN)
2282 == 0) {
2283 bp->fails++;
2284 break;
2285 }
2286 }
2287 return;
2288 }
2289 asresp += 2;
2290 /* association id */
2291 asresp += 2;
2292 /* supported rates */
2293 rate = AWI_RATE_1MBIT;
2294 for (i = 0; i < asresp[1]; i++) {
2295 if (AWI_80211_RATE(asresp[2 + i]) <= rate)
2296 continue;
2297 phy_rates = sc->sc_mib_phy.aSuprt_Data_Rates;
2298 for (j = 0; j < phy_rates[1]; j++) {
2299 if (AWI_80211_RATE(asresp[2 + i]) ==
2300 AWI_80211_RATE(phy_rates[2 + j]))
2301 rate = AWI_80211_RATE(asresp[2 + i]);
2302 }
2303 }
2304 printf("%s: associated with %s ssid \"%s\"",
2305 sc->sc_dev.dv_xname, ether_sprintf(sc->sc_bss.bssid),
2306 sc->sc_bss.essid + 2);
2307 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
2308 printf(" chanset %d pattern %d",
2309 sc->sc_bss.chanset, sc->sc_bss.pattern);
2310 else
2311 printf(" channel %d", sc->sc_bss.chanset);
2312 printf(" signal %d\n", sc->sc_bss.rssi);
2313 sc->sc_tx_rate = rate;
2314 sc->sc_mgt_timer = 0;
2315 sc->sc_rx_timer = 10;
2316 sc->sc_ifp->if_timer = 1;
2317 sc->sc_status = AWI_ST_RUNNING;
2318 sc->sc_ifp->if_flags |= IFF_RUNNING;
2319 awi_drvstate(sc, AWI_DRV_INFASSOC);
2320 while ((bp = TAILQ_FIRST(&sc->sc_scan)) != NULL)
2321 TAILQ_REMOVE(&sc->sc_scan, bp, list);
2322 awi_start(sc->sc_ifp);
2323 }
2324
2325 static int
2326 awi_mib(sc, cmd, mib)
2327 struct awi_softc *sc;
2328 u_int8_t cmd;
2329 u_int8_t mib;
2330 {
2331 int error;
2332 u_int8_t size, *ptr;
2333
2334 switch (mib) {
2335 case AWI_MIB_LOCAL:
2336 ptr = (u_int8_t *)&sc->sc_mib_local;
2337 size = sizeof(sc->sc_mib_local);
2338 break;
2339 case AWI_MIB_ADDR:
2340 ptr = (u_int8_t *)&sc->sc_mib_addr;
2341 size = sizeof(sc->sc_mib_addr);
2342 break;
2343 case AWI_MIB_MAC:
2344 ptr = (u_int8_t *)&sc->sc_mib_mac;
2345 size = sizeof(sc->sc_mib_mac);
2346 break;
2347 case AWI_MIB_STAT:
2348 ptr = (u_int8_t *)&sc->sc_mib_stat;
2349 size = sizeof(sc->sc_mib_stat);
2350 break;
2351 case AWI_MIB_MGT:
2352 ptr = (u_int8_t *)&sc->sc_mib_mgt;
2353 size = sizeof(sc->sc_mib_mgt);
2354 break;
2355 case AWI_MIB_PHY:
2356 ptr = (u_int8_t *)&sc->sc_mib_phy;
2357 size = sizeof(sc->sc_mib_phy);
2358 break;
2359 default:
2360 return EINVAL;
2361 }
2362 if (sc->sc_cmd_inprog) {
2363 error = awi_cmd_wait(sc);
2364 if (error) {
2365 printf("awi_mib: cmd %d inprog\n",
2366 awi_read_1(sc, AWI_CMD));
2367 return error;
2368 }
2369 }
2370 sc->sc_cmd_inprog = 1;
2371 if (cmd == AWI_CMD_SET_MIB)
2372 awi_write_bytes(sc, AWI_CMD_PARAMS+AWI_CA_MIB_DATA, ptr, size);
2373 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_MIB_TYPE, mib);
2374 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_MIB_SIZE, size);
2375 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_MIB_INDEX, 0);
2376 error = awi_cmd(sc, cmd);
2377 if (error)
2378 return error;
2379 if (cmd == AWI_CMD_GET_MIB) {
2380 awi_read_bytes(sc, AWI_CMD_PARAMS+AWI_CA_MIB_DATA, ptr, size);
2381 #ifdef AWI_DEBUG
2382 if (awi_verbose) {
2383 int i;
2384
2385 printf("awi_mib: #%d:", mib);
2386 for (i = 0; i < size; i++)
2387 printf(" %02x", ptr[i]);
2388 printf("\n");
2389 }
2390 #endif
2391 }
2392 return 0;
2393 }
2394
2395 static int
2396 awi_cmd_scan(sc)
2397 struct awi_softc *sc;
2398 {
2399 int error;
2400 u_int8_t scan_mode;
2401
2402 if (sc->sc_active_scan)
2403 scan_mode = AWI_SCAN_ACTIVE;
2404 else
2405 scan_mode = AWI_SCAN_PASSIVE;
2406 if (sc->sc_mib_mgt.aScan_Mode != scan_mode) {
2407 sc->sc_mib_mgt.aScan_Mode = scan_mode;
2408 error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT);
2409 return error;
2410 }
2411
2412 if (sc->sc_cmd_inprog) {
2413 error = awi_cmd_wait(sc);
2414 if (error)
2415 return error;
2416 }
2417 sc->sc_cmd_inprog = 1;
2418 awi_write_2(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_DURATION,
2419 sc->sc_active_scan ? AWI_ASCAN_DURATION : AWI_PSCAN_DURATION);
2420 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
2421 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_SET,
2422 sc->sc_scan_set);
2423 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_PATTERN,
2424 sc->sc_scan_cur);
2425 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_IDX, 1);
2426 } else {
2427 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_SET,
2428 sc->sc_scan_cur);
2429 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_PATTERN, 0);
2430 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_IDX, 0);
2431 }
2432 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_SUSP, 0);
2433 return awi_cmd(sc, AWI_CMD_SCAN);
2434 }
2435
2436 static int
2437 awi_cmd(sc, cmd)
2438 struct awi_softc *sc;
2439 u_int8_t cmd;
2440 {
2441 u_int8_t status;
2442 int error = 0;
2443
2444 sc->sc_cmd_inprog = 1;
2445 awi_write_1(sc, AWI_CMD_STATUS, AWI_STAT_IDLE);
2446 awi_write_1(sc, AWI_CMD, cmd);
2447 if (sc->sc_status != AWI_ST_INIT)
2448 return 0;
2449 error = awi_cmd_wait(sc);
2450 if (error)
2451 return error;
2452 status = awi_read_1(sc, AWI_CMD_STATUS);
2453 awi_write_1(sc, AWI_CMD, 0);
2454 switch (status) {
2455 case AWI_STAT_OK:
2456 break;
2457 case AWI_STAT_BADPARM:
2458 return EINVAL;
2459 default:
2460 printf("%s: command %d failed %x\n",
2461 sc->sc_dev.dv_xname, cmd, status);
2462 return ENXIO;
2463 }
2464 return 0;
2465 }
2466
2467 static void
2468 awi_cmd_done(sc)
2469 struct awi_softc *sc;
2470 {
2471 u_int8_t cmd, status;
2472
2473 status = awi_read_1(sc, AWI_CMD_STATUS);
2474 if (status == AWI_STAT_IDLE)
2475 return; /* stray interrupt */
2476
2477 sc->sc_cmd_inprog = 0;
2478 if (sc->sc_status == AWI_ST_INIT) {
2479 wakeup(sc);
2480 return;
2481 }
2482 cmd = awi_read_1(sc, AWI_CMD);
2483 awi_write_1(sc, AWI_CMD, 0);
2484
2485 if (status != AWI_STAT_OK) {
2486 printf("%s: command %d failed %x\n",
2487 sc->sc_dev.dv_xname, cmd, status);
2488 return;
2489 }
2490 switch (sc->sc_status) {
2491 case AWI_ST_SCAN:
2492 if (cmd == AWI_CMD_SET_MIB)
2493 awi_cmd_scan(sc); /* retry */
2494 break;
2495 case AWI_ST_SETSS:
2496 awi_try_sync(sc);
2497 break;
2498 case AWI_ST_SYNC:
2499 awi_sync_done(sc);
2500 break;
2501 default:
2502 break;
2503 }
2504 }
2505
2506 static int
2507 awi_next_txd(sc, len, framep, ntxdp)
2508 struct awi_softc *sc;
2509 int len;
2510 u_int32_t *framep, *ntxdp;
2511 {
2512 u_int32_t txd, ntxd, frame;
2513
2514 txd = sc->sc_txnext;
2515 frame = txd + AWI_TXD_SIZE;
2516 if (frame + len > sc->sc_txend)
2517 frame = sc->sc_txbase;
2518 ntxd = frame + len;
2519 if (ntxd + AWI_TXD_SIZE > sc->sc_txend)
2520 ntxd = sc->sc_txbase;
2521 *framep = frame;
2522 *ntxdp = ntxd;
2523 /*
2524 * Determine if there are any room in ring buffer.
2525 * --- send wait, === new data, +++ conflict (ENOBUFS)
2526 * base........................end
2527 * done----txd=====ntxd OK
2528 * --txd=====done++++ntxd-- full
2529 * --txd=====ntxd done-- OK
2530 * ==ntxd done----txd=== OK
2531 * ==done++++ntxd----txd=== full
2532 * ++ntxd txd=====done++ full
2533 */
2534 if (txd < ntxd) {
2535 if (txd < sc->sc_txdone && ntxd + AWI_TXD_SIZE > sc->sc_txdone)
2536 return ENOBUFS;
2537 } else {
2538 if (txd < sc->sc_txdone || ntxd + AWI_TXD_SIZE > sc->sc_txdone)
2539 return ENOBUFS;
2540 }
2541 return 0;
2542 }
2543
2544 static int
2545 awi_lock(sc)
2546 struct awi_softc *sc;
2547 {
2548 int error = 0;
2549
2550 if (sc->sc_invalid)
2551 return ENXIO;
2552 if (curproc == NULL) {
2553 /*
2554 * XXX
2555 * Though driver ioctl should be called with context,
2556 * KAME ipv6 stack calls ioctl in interrupt for now.
2557 * We simply abort the request if there are other
2558 * ioctl requests in progress.
2559 */
2560 if (sc->sc_busy)
2561 return EWOULDBLOCK;
2562 sc->sc_busy = 1;
2563 sc->sc_cansleep = 0;
2564 return 0;
2565 }
2566 while (sc->sc_busy) {
2567 sc->sc_sleep_cnt++;
2568 error = tsleep(sc, PWAIT | PCATCH, "awilck", 0);
2569 sc->sc_sleep_cnt--;
2570 if (error)
2571 return error;
2572 if (sc->sc_invalid)
2573 return ENXIO;
2574 }
2575 sc->sc_busy = 1;
2576 sc->sc_cansleep = 1;
2577 return 0;
2578 }
2579
2580 static void
2581 awi_unlock(sc)
2582 struct awi_softc *sc;
2583 {
2584 sc->sc_busy = 0;
2585 sc->sc_cansleep = 0;
2586 if (sc->sc_sleep_cnt)
2587 wakeup(sc);
2588 }
2589
2590 static int
2591 awi_intr_lock(sc)
2592 struct awi_softc *sc;
2593 {
2594 u_int8_t status;
2595 int i, retry;
2596
2597 status = 1;
2598 for (retry = 0; retry < 10; retry++) {
2599 for (i = 0; i < AWI_LOCKOUT_TIMEOUT*1000/5; i++) {
2600 status = awi_read_1(sc, AWI_LOCKOUT_HOST);
2601 if (status == 0)
2602 break;
2603 DELAY(5);
2604 }
2605 if (status != 0)
2606 break;
2607 awi_write_1(sc, AWI_LOCKOUT_MAC, 1);
2608 status = awi_read_1(sc, AWI_LOCKOUT_HOST);
2609 if (status == 0)
2610 break;
2611 awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
2612 }
2613 if (status != 0) {
2614 printf("%s: failed to lock interrupt\n",
2615 sc->sc_dev.dv_xname);
2616 return ENXIO;
2617 }
2618 return 0;
2619 }
2620
2621 static void
2622 awi_intr_unlock(sc)
2623 struct awi_softc *sc;
2624 {
2625
2626 awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
2627 }
2628
2629 static int
2630 awi_cmd_wait(sc)
2631 struct awi_softc *sc;
2632 {
2633 int i, error = 0;
2634
2635 i = 0;
2636 while (sc->sc_cmd_inprog) {
2637 if (sc->sc_invalid)
2638 return ENXIO;
2639 if (sc->sc_cansleep) {
2640 sc->sc_sleep_cnt++;
2641 error = tsleep(sc, PWAIT, "awicmd",
2642 AWI_CMD_TIMEOUT*hz/1000);
2643 sc->sc_sleep_cnt--;
2644 } else {
2645 if (awi_read_1(sc, AWI_CMD_STATUS) != AWI_STAT_IDLE) {
2646 awi_cmd_done(sc);
2647 break;
2648 }
2649 if (i++ >= AWI_CMD_TIMEOUT*1000/10)
2650 error = EWOULDBLOCK;
2651 else
2652 DELAY(10);
2653 }
2654 if (error)
2655 break;
2656 }
2657 return error;
2658 }
2659
2660 #ifdef AWI_DEBUG
2661 static void
2662 awi_dump_pkt(sc, m, rssi)
2663 struct awi_softc *sc;
2664 struct mbuf *m;
2665 u_int8_t rssi;
2666 {
2667 struct ieee80211_frame *wh;
2668 int i, l;
2669
2670 wh = mtod(m, struct ieee80211_frame *);
2671
2672 if (awi_dump_mask != 0 &&
2673 ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK)==IEEE80211_FC1_DIR_NODS) &&
2674 ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK)==IEEE80211_FC0_TYPE_MGT)) {
2675 if ((AWI_DUMP_MASK(wh->i_fc[0]) & awi_dump_mask) != 0)
2676 return;
2677 }
2678 if (awi_dump_mask < 0 &&
2679 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK)==IEEE80211_FC0_TYPE_DATA)
2680 return;
2681
2682 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
2683 case IEEE80211_FC1_DIR_NODS:
2684 printf("rx: NODS %s", ether_sprintf(wh->i_addr2));
2685 printf("->%s", ether_sprintf(wh->i_addr1));
2686 printf("(%s)", ether_sprintf(wh->i_addr3));
2687 break;
2688 case IEEE80211_FC1_DIR_TODS:
2689 printf("rx: TODS %s", ether_sprintf(wh->i_addr2));
2690 printf("->%s", ether_sprintf(wh->i_addr3));
2691 printf("(%s)", ether_sprintf(wh->i_addr1));
2692 break;
2693 case IEEE80211_FC1_DIR_FROMDS:
2694 printf("rx: FRDS %s", ether_sprintf(wh->i_addr3));
2695 printf("->%s", ether_sprintf(wh->i_addr1));
2696 printf("(%s)", ether_sprintf(wh->i_addr2));
2697 break;
2698 case IEEE80211_FC1_DIR_DSTODS:
2699 printf("rx: DSDS %s", ether_sprintf((u_int8_t *)&wh[1]));
2700 printf("->%s", ether_sprintf(wh->i_addr3));
2701 printf("(%s", ether_sprintf(wh->i_addr2));
2702 printf("->%s)", ether_sprintf(wh->i_addr1));
2703 break;
2704 }
2705 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
2706 case IEEE80211_FC0_TYPE_DATA:
2707 printf(" data");
2708 break;
2709 case IEEE80211_FC0_TYPE_MGT:
2710 switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
2711 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2712 printf(" probe_req");
2713 break;
2714 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2715 printf(" probe_resp");
2716 break;
2717 case IEEE80211_FC0_SUBTYPE_BEACON:
2718 printf(" beacon");
2719 break;
2720 case IEEE80211_FC0_SUBTYPE_AUTH:
2721 printf(" auth");
2722 break;
2723 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2724 printf(" assoc_req");
2725 break;
2726 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2727 printf(" assoc_resp");
2728 break;
2729 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2730 printf(" reassoc_req");
2731 break;
2732 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2733 printf(" reassoc_resp");
2734 break;
2735 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2736 printf(" deauth");
2737 break;
2738 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2739 printf(" disassoc");
2740 break;
2741 default:
2742 printf(" mgt#%d",
2743 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
2744 break;
2745 }
2746 break;
2747 default:
2748 printf(" type#%d",
2749 wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
2750 break;
2751 }
2752 printf(" +%d\n", rssi);
2753 if (awi_dump_len > 0) {
2754 l = m->m_len;
2755 if (l > awi_dump_len + sizeof(*wh))
2756 l = awi_dump_len + sizeof(*wh);
2757 i = sizeof(*wh);
2758 if (awi_dump_hdr)
2759 i = 0;
2760 for (; i < l; i++) {
2761 if ((i & 1) == 0)
2762 printf(" ");
2763 printf("%02x", mtod(m, u_int8_t *)[i]);
2764 }
2765 printf("\n");
2766 }
2767 }
2768 #endif
2769