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