awi.c revision 1.16 1 /* $NetBSD: awi.c,v 1.16 2000/03/27 12:58:01 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_enabled || !sc->sc_enab_intr || 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 /* reinitialize muticast filter */
928 n = 0;
929 ifp->if_flags |= IFF_ALLMULTI;
930 sc->sc_mib_local.Accept_All_Multicast_Dis = 0;
931 if (ifp->if_flags & IFF_PROMISC) {
932 sc->sc_mib_mac.aPromiscuous_Enable = 1;
933 goto set_mib;
934 }
935 sc->sc_mib_mac.aPromiscuous_Enable = 0;
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 for (; n < AWI_GROUP_ADDR_SIZE; n++)
964 memset(sc->sc_mib_addr.aGroup_Addresses[n], 0, ETHER_ADDR_LEN);
965 ifp->if_flags &= ~IFF_ALLMULTI;
966 sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
967
968 set_mib:
969 if (!sc->sc_enabled) {
970 sc->sc_enabled = 1;
971 if (sc->sc_enable)
972 (*sc->sc_enable)(sc);
973 sc->sc_status = AWI_ST_INIT;
974 error = awi_init_hw(sc);
975 if (error)
976 return error;
977 }
978 ostatus = sc->sc_status;
979 sc->sc_status = AWI_ST_INIT;
980 if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_LOCAL)) != 0 ||
981 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_ADDR)) != 0 ||
982 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MAC)) != 0 ||
983 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT)) != 0 ||
984 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_PHY)) != 0) {
985 awi_stop(sc);
986 return error;
987 }
988 if (ifp->if_flags & IFF_RUNNING)
989 sc->sc_status = AWI_ST_RUNNING;
990 else {
991 if (ostatus == AWI_ST_INIT) {
992 error = awi_init_txrx(sc);
993 if (error)
994 return error;
995 }
996 error = awi_start_scan(sc);
997 }
998 return error;
999 }
1000
1001 static void
1002 awi_stop(sc)
1003 struct awi_softc *sc;
1004 {
1005 struct ifnet *ifp = sc->sc_ifp;
1006 struct awi_bss *bp;
1007 struct mbuf *m;
1008
1009 sc->sc_status = AWI_ST_INIT;
1010 if (!sc->sc_invalid) {
1011 (void)awi_cmd_wait(sc);
1012 if (sc->sc_mib_local.Network_Mode &&
1013 sc->sc_status > AWI_ST_AUTH)
1014 awi_send_deauth(sc);
1015 awi_stop_txrx(sc);
1016 }
1017 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
1018 ifp->if_timer = 0;
1019 sc->sc_tx_timer = sc->sc_rx_timer = sc->sc_mgt_timer = 0;
1020 for (;;) {
1021 IF_DEQUEUE(&sc->sc_mgtq, m);
1022 if (m == NULL)
1023 break;
1024 m_freem(m);
1025 }
1026 for (;;) {
1027 IF_DEQUEUE(&ifp->if_snd, m);
1028 if (m == NULL)
1029 break;
1030 m_freem(m);
1031 }
1032 while ((bp = TAILQ_FIRST(&sc->sc_scan)) != NULL)
1033 TAILQ_REMOVE(&sc->sc_scan, bp, list);
1034 }
1035
1036 static void
1037 awi_watchdog(ifp)
1038 struct ifnet *ifp;
1039 {
1040 struct awi_softc *sc = ifp->if_softc;
1041 int ocansleep;
1042
1043 if (sc->sc_invalid) {
1044 ifp->if_timer = 0;
1045 return;
1046 }
1047
1048 ocansleep = sc->sc_cansleep;
1049 sc->sc_cansleep = 0;
1050 if (sc->sc_tx_timer && --sc->sc_tx_timer == 0) {
1051 printf("%s: transmit timeout\n", sc->sc_dev.dv_xname);
1052 awi_txint(sc);
1053 }
1054 if (sc->sc_rx_timer && --sc->sc_rx_timer == 0) {
1055 printf("%s: no recent beacons from %s; rescanning\n",
1056 sc->sc_dev.dv_xname, ether_sprintf(sc->sc_bss.bssid));
1057 awi_start_scan(sc);
1058 }
1059 if (sc->sc_mgt_timer && --sc->sc_mgt_timer == 0) {
1060 switch (sc->sc_status) {
1061 case AWI_ST_SCAN:
1062 awi_stop_scan(sc);
1063 break;
1064 case AWI_ST_AUTH:
1065 case AWI_ST_ASSOC:
1066 /* restart scan */
1067 awi_start_scan(sc);
1068 break;
1069 default:
1070 break;
1071 }
1072 }
1073
1074 if (sc->sc_tx_timer == 0 && sc->sc_rx_timer == 0 &&
1075 sc->sc_mgt_timer == 0)
1076 ifp->if_timer = 0;
1077 else
1078 ifp->if_timer = 1;
1079 sc->sc_cansleep = ocansleep;
1080 }
1081
1082 static void
1083 awi_start(ifp)
1084 struct ifnet *ifp;
1085 {
1086 struct awi_softc *sc = ifp->if_softc;
1087 struct mbuf *m0, *m;
1088 u_int32_t txd, frame, ntxd;
1089 u_int8_t rate;
1090 int len, sent = 0;
1091
1092 for (;;) {
1093 txd = sc->sc_txnext;
1094 IF_DEQUEUE(&sc->sc_mgtq, m0);
1095 if (m0 != NULL) {
1096 if (awi_next_txd(sc, m0->m_pkthdr.len, &frame, &ntxd)) {
1097 IF_PREPEND(&sc->sc_mgtq, m0);
1098 ifp->if_flags |= IFF_OACTIVE;
1099 break;
1100 }
1101 } else {
1102 if (!(ifp->if_flags & IFF_RUNNING))
1103 break;
1104 IF_DEQUEUE(&ifp->if_snd, m0);
1105 if (m0 == NULL)
1106 break;
1107 if (awi_next_txd(sc, m0->m_pkthdr.len +
1108 sizeof(struct ieee80211_frame), &frame, &ntxd)) {
1109 IF_PREPEND(&ifp->if_snd, m0);
1110 ifp->if_flags |= IFF_OACTIVE;
1111 break;
1112 }
1113 AWI_BPF_MTAP(sc, m0, AWI_BPF_NORM);
1114 m0 = awi_fix_txhdr(sc, m0);
1115 if (m0 == NULL) {
1116 ifp->if_oerrors++;
1117 continue;
1118 }
1119 ifp->if_opackets++;
1120 }
1121 AWI_BPF_MTAP(sc, m0, AWI_BPF_RAW);
1122 len = 0;
1123 for (m = m0; m != NULL; m = m->m_next) {
1124 awi_write_bytes(sc, frame + len, mtod(m, u_int8_t *),
1125 m->m_len);
1126 len += m->m_len;
1127 }
1128 m_freem(m0);
1129 rate = sc->sc_tx_rate; /*XXX*/
1130 awi_write_1(sc, ntxd + AWI_TXD_STATE, 0);
1131 awi_write_4(sc, txd + AWI_TXD_START, frame);
1132 awi_write_4(sc, txd + AWI_TXD_NEXT, ntxd);
1133 awi_write_4(sc, txd + AWI_TXD_LENGTH, len);
1134 awi_write_1(sc, txd + AWI_TXD_RATE, rate);
1135 awi_write_4(sc, txd + AWI_TXD_NDA, 0);
1136 awi_write_4(sc, txd + AWI_TXD_NRA, 0);
1137 awi_write_1(sc, txd + AWI_TXD_STATE, AWI_TXD_ST_OWN);
1138 sc->sc_txnext = ntxd;
1139 sent++;
1140 }
1141 if (sent) {
1142 if (sc->sc_tx_timer == 0)
1143 sc->sc_tx_timer = 5;
1144 ifp->if_timer = 1;
1145 #ifdef AWI_DEBUG
1146 if (awi_verbose)
1147 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);
1148 #endif
1149 }
1150 }
1151
1152 static void
1153 awi_txint(sc)
1154 struct awi_softc *sc;
1155 {
1156 struct ifnet *ifp = sc->sc_ifp;
1157 u_int8_t flags;
1158
1159 while (sc->sc_txdone != sc->sc_txnext) {
1160 flags = awi_read_1(sc, sc->sc_txdone + AWI_TXD_STATE);
1161 if ((flags & AWI_TXD_ST_OWN) || !(flags & AWI_TXD_ST_DONE))
1162 break;
1163 if (flags & AWI_TXD_ST_ERROR)
1164 ifp->if_oerrors++;
1165 sc->sc_txdone = awi_read_4(sc, sc->sc_txdone + AWI_TXD_NEXT) &
1166 0x7fff;
1167 }
1168 sc->sc_tx_timer = 0;
1169 ifp->if_flags &= ~IFF_OACTIVE;
1170 #ifdef AWI_DEBUG
1171 if (awi_verbose)
1172 printf("awi_txint: txdone %d txnext %d txbase %d txend %d\n",
1173 sc->sc_txdone, sc->sc_txnext, sc->sc_txbase, sc->sc_txend);
1174 #endif
1175 awi_start(ifp);
1176 }
1177
1178 static struct mbuf *
1179 awi_fix_txhdr(sc, m0)
1180 struct awi_softc *sc;
1181 struct mbuf *m0;
1182 {
1183 struct ether_header eh;
1184 struct ieee80211_frame *wh;
1185 struct llc *llc;
1186
1187 if (m0->m_len < sizeof(eh)) {
1188 m0 = m_pullup(m0, sizeof(eh));
1189 if (m0 == NULL)
1190 return NULL;
1191 }
1192 memcpy(&eh, mtod(m0, caddr_t), sizeof(eh));
1193 if (sc->sc_format_llc) {
1194 m_adj(m0, sizeof(struct ether_header) - sizeof(struct llc));
1195 llc = mtod(m0, struct llc *);
1196 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1197 llc->llc_control = LLC_UI;
1198 llc->llc_snap.org_code[0] = llc->llc_snap.org_code[1] =
1199 llc->llc_snap.org_code[2] = 0;
1200 llc->llc_snap.ether_type = eh.ether_type;
1201 }
1202 M_PREPEND(m0, sizeof(struct ieee80211_frame), M_DONTWAIT);
1203 if (m0 == NULL)
1204 return NULL;
1205 wh = mtod(m0, struct ieee80211_frame *);
1206
1207 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1208 LE_WRITE_2(wh->i_dur, 0);
1209 LE_WRITE_2(wh->i_seq, 0);
1210 if (sc->sc_mib_local.Network_Mode) {
1211 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1212 memcpy(wh->i_addr1, sc->sc_bss.bssid, ETHER_ADDR_LEN);
1213 memcpy(wh->i_addr2, eh.ether_shost, ETHER_ADDR_LEN);
1214 memcpy(wh->i_addr3, eh.ether_dhost, ETHER_ADDR_LEN);
1215 } else {
1216 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1217 memcpy(wh->i_addr1, eh.ether_dhost, ETHER_ADDR_LEN);
1218 memcpy(wh->i_addr2, eh.ether_shost, ETHER_ADDR_LEN);
1219 memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
1220 }
1221 return m0;
1222 }
1223
1224 static struct mbuf *
1225 awi_fix_rxhdr(sc, m0)
1226 struct awi_softc *sc;
1227 struct mbuf *m0;
1228 {
1229 struct ieee80211_frame wh;
1230 struct ether_header *eh;
1231 struct llc *llc;
1232
1233 if (m0->m_len < sizeof(wh)) {
1234 m_freem(m0);
1235 return NULL;
1236 }
1237 llc = (struct llc *)(mtod(m0, caddr_t) + sizeof(wh));
1238 if (llc->llc_dsap == LLC_SNAP_LSAP &&
1239 llc->llc_ssap == LLC_SNAP_LSAP &&
1240 llc->llc_control == LLC_UI &&
1241 llc->llc_snap.org_code[0] == 0 &&
1242 llc->llc_snap.org_code[1] == 0 &&
1243 llc->llc_snap.org_code[2] == 0) {
1244 memcpy(&wh, mtod(m0, caddr_t), sizeof(wh));
1245 m_adj(m0, sizeof(wh) + sizeof(*llc) - sizeof(*eh));
1246 eh = mtod(m0, struct ether_header *);
1247 switch (wh.i_fc[1] & IEEE80211_FC1_DIR_MASK) {
1248 case IEEE80211_FC1_DIR_NODS:
1249 memcpy(eh->ether_dhost, wh.i_addr1, ETHER_ADDR_LEN);
1250 memcpy(eh->ether_shost, wh.i_addr2, ETHER_ADDR_LEN);
1251 break;
1252 case IEEE80211_FC1_DIR_TODS:
1253 memcpy(eh->ether_dhost, wh.i_addr3, ETHER_ADDR_LEN);
1254 memcpy(eh->ether_shost, wh.i_addr2, ETHER_ADDR_LEN);
1255 break;
1256 case IEEE80211_FC1_DIR_FROMDS:
1257 memcpy(eh->ether_dhost, wh.i_addr1, ETHER_ADDR_LEN);
1258 memcpy(eh->ether_shost, wh.i_addr3, ETHER_ADDR_LEN);
1259 break;
1260 case IEEE80211_FC1_DIR_DSTODS:
1261 m_freem(m0);
1262 return NULL;
1263 }
1264 } else {
1265 /* assuming ethernet encapsulation, just strip 802.11 header */
1266 m_adj(m0, sizeof(wh));
1267 }
1268 return m0;
1269 }
1270
1271 static void
1272 awi_input(sc, m, rxts, rssi)
1273 struct awi_softc *sc;
1274 struct mbuf *m;
1275 u_int32_t rxts;
1276 u_int8_t rssi;
1277 {
1278 struct ifnet *ifp = sc->sc_ifp;
1279 struct ieee80211_frame *wh;
1280 #ifndef __NetBSD__
1281 struct ether_header *eh;
1282 #endif
1283
1284 AWI_BPF_MTAP(sc, m, AWI_BPF_RAW);
1285 wh = mtod(m, struct ieee80211_frame *);
1286 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
1287 IEEE80211_FC0_VERSION_0) {
1288 printf("%s; receive packet with wrong version: %x\n",
1289 sc->sc_dev.dv_xname, wh->i_fc[0]);
1290 m_freem(m);
1291 return;
1292 }
1293 #ifdef AWI_DEBUG
1294 if (awi_dump)
1295 awi_dump_pkt(sc, m, rssi);
1296 #endif
1297
1298 if ((sc->sc_mib_local.Network_Mode || !sc->sc_no_bssid) &&
1299 sc->sc_status == AWI_ST_RUNNING) {
1300 if (memcmp(wh->i_addr2, sc->sc_bss.bssid, ETHER_ADDR_LEN) == 0) {
1301 sc->sc_rx_timer = 10;
1302 sc->sc_bss.rssi = rssi;
1303 }
1304 }
1305 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1306 case IEEE80211_FC0_TYPE_DATA:
1307 if (sc->sc_mib_local.Network_Mode) {
1308 if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
1309 IEEE80211_FC1_DIR_FROMDS) {
1310 m_freem(m);
1311 return;
1312 }
1313 } else {
1314 if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
1315 IEEE80211_FC1_DIR_NODS) {
1316 m_freem(m);
1317 return;
1318 }
1319 }
1320 m = awi_fix_rxhdr(sc, m);
1321 if (m == NULL) {
1322 ifp->if_ierrors++;
1323 break;
1324 }
1325 ifp->if_ipackets++;
1326 AWI_BPF_MTAP(sc, m, AWI_BPF_NORM);
1327 #ifdef __NetBSD__
1328 m->m_flags |= M_HASFCS;
1329 (*ifp->if_input)(ifp, m);
1330 #else
1331 eh = mtod(m, struct ether_header *);
1332 m_adj(m, sizeof(*eh));
1333 m_adj(m, -ETHER_CRC_LEN);
1334 ether_input(ifp, eh, m);
1335 #endif
1336 break;
1337 case IEEE80211_FC0_TYPE_MGT:
1338 if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
1339 IEEE80211_FC1_DIR_NODS) {
1340 m_freem(m);
1341 return;
1342 }
1343 switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
1344 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1345 case IEEE80211_FC0_SUBTYPE_BEACON:
1346 awi_recv_beacon(sc, m, rxts, rssi);
1347 break;
1348 case IEEE80211_FC0_SUBTYPE_AUTH:
1349 awi_recv_auth(sc, m);
1350 break;
1351 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1352 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1353 awi_recv_asresp(sc, m);
1354 break;
1355 case IEEE80211_FC0_SUBTYPE_DEAUTH:
1356 if (sc->sc_mib_local.Network_Mode)
1357 awi_send_auth(sc);
1358 break;
1359 case IEEE80211_FC0_SUBTYPE_DISASSOC:
1360 if (sc->sc_mib_local.Network_Mode)
1361 awi_send_asreq(sc, 1);
1362 break;
1363 }
1364 m_freem(m);
1365 break;
1366 case IEEE80211_FC0_TYPE_CTL:
1367 default:
1368 /* should not come here */
1369 m_freem(m);
1370 break;
1371 }
1372 }
1373
1374 static void
1375 awi_rxint(sc)
1376 struct awi_softc *sc;
1377 {
1378 u_int8_t state, rate, rssi;
1379 u_int16_t len;
1380 u_int32_t frame, next, rxts, rxoff;
1381 struct mbuf *m;
1382
1383 rxoff = sc->sc_rxdoff;
1384 for (;;) {
1385 state = awi_read_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE);
1386 if (state & AWI_RXD_ST_OWN)
1387 break;
1388 if (!(state & AWI_RXD_ST_CONSUMED)) {
1389 if (state & AWI_RXD_ST_RXERROR)
1390 sc->sc_ifp->if_ierrors++;
1391 else {
1392 len = awi_read_2(sc, rxoff + AWI_RXD_LEN);
1393 rate = awi_read_1(sc, rxoff + AWI_RXD_RATE);
1394 rssi = awi_read_1(sc, rxoff + AWI_RXD_RSSI);
1395 frame = awi_read_4(sc, rxoff + AWI_RXD_START_FRAME) & 0x7fff;
1396 rxts = awi_read_4(sc, rxoff + AWI_RXD_LOCALTIME);
1397 m = awi_devget(sc, frame, len);
1398 if (state & AWI_RXD_ST_LF)
1399 awi_input(sc, m, rxts, rssi);
1400 else
1401 sc->sc_rxpend = m;
1402 }
1403 state |= AWI_RXD_ST_CONSUMED;
1404 awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1405 }
1406 next = awi_read_4(sc, rxoff + AWI_RXD_NEXT);
1407 if (next & AWI_RXD_NEXT_LAST)
1408 break;
1409 /* make sure the next pointer is correct */
1410 if (next != awi_read_4(sc, rxoff + AWI_RXD_NEXT))
1411 break;
1412 state |= AWI_RXD_ST_OWN;
1413 awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1414 rxoff = next & 0x7fff;
1415 }
1416 sc->sc_rxdoff = rxoff;
1417 }
1418
1419 struct mbuf *
1420 awi_devget(sc, off, len)
1421 struct awi_softc *sc;
1422 u_int32_t off;
1423 u_int16_t len;
1424 {
1425 struct mbuf *m;
1426 struct mbuf *top, **mp = ⊤
1427 u_int tlen;
1428
1429 top = sc->sc_rxpend;
1430 if (top != NULL) {
1431 sc->sc_rxpend = NULL;
1432 top->m_pkthdr.len += len;
1433 while ((m = *mp) != NULL)
1434 mp = &m->m_next;
1435 if (m->m_flags & M_EXT)
1436 tlen = m->m_ext.ext_size;
1437 else if (m->m_flags & M_PKTHDR)
1438 tlen = MHLEN;
1439 else
1440 tlen = MLEN;
1441 tlen -= m->m_len;
1442 if (tlen > len)
1443 tlen = len;
1444 awi_read_bytes(sc, off, mtod(m, u_int8_t *) + m->m_len, tlen);
1445 off += tlen;
1446 len -= tlen;
1447 }
1448
1449 while (len > 0) {
1450 if (top == NULL) {
1451 MGETHDR(m, M_DONTWAIT, MT_DATA);
1452 if (m == NULL)
1453 return NULL;
1454 m->m_pkthdr.rcvif = sc->sc_ifp;
1455 m->m_pkthdr.len = len;
1456 m->m_len = MHLEN;
1457 } else {
1458 MGET(m, M_DONTWAIT, MT_DATA);
1459 if (m == NULL) {
1460 m_freem(top);
1461 return NULL;
1462 }
1463 m->m_len = MLEN;
1464 }
1465 if (len >= MINCLSIZE) {
1466 MCLGET(m, M_DONTWAIT);
1467 if (m->m_flags & M_EXT)
1468 m->m_len = m->m_ext.ext_size;
1469 }
1470 if (m->m_len > len)
1471 m->m_len = len;
1472 awi_read_bytes(sc, off, mtod(m, u_int8_t *), m->m_len);
1473 off += m->m_len;
1474 len -= m->m_len;
1475 *mp = m;
1476 mp = &m->m_next;
1477 }
1478 return top;
1479 }
1480
1481 /*
1482 * Initialize hardware and start firmware to accept commands.
1483 * Called everytime after power on firmware.
1484 */
1485
1486 static int
1487 awi_init_hw(sc)
1488 struct awi_softc *sc;
1489 {
1490 u_int8_t status;
1491 u_int16_t intmask;
1492 int i, error;
1493 u_int8_t banner[AWI_BANNER_LEN];
1494
1495 sc->sc_enab_intr = 0;
1496 awi_drvstate(sc, AWI_DRV_RESET);
1497
1498 /* reset firmware */
1499 am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1500 DELAY(100);
1501 awi_write_1(sc, AWI_SELFTEST, 0);
1502 am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1503 DELAY(100);
1504
1505 /* wait for selftest completion */
1506 for (i = 0; ; i++) {
1507 if (i >= AWI_SELFTEST_TIMEOUT*hz/1000) {
1508 printf("%s: failed to complete selftest (timeout)\n",
1509 sc->sc_dev.dv_xname);
1510 return ENXIO;
1511 }
1512 status = awi_read_1(sc, AWI_SELFTEST);
1513 if ((status & 0xf0) == 0xf0)
1514 break;
1515 if (sc->sc_cansleep) {
1516 sc->sc_sleep_cnt++;
1517 (void)tsleep(sc, PWAIT, "awitst", 1);
1518 sc->sc_sleep_cnt--;
1519 } else {
1520 DELAY(1000*1000/hz);
1521 }
1522 }
1523 if (status != AWI_SELFTEST_PASSED) {
1524 printf("%s: failed to complete selftest (code %x)\n",
1525 sc->sc_dev.dv_xname, status);
1526 return ENXIO;
1527 }
1528
1529 /* check banner to confirm firmware write it */
1530 awi_read_bytes(sc, AWI_BANNER, banner, AWI_BANNER_LEN);
1531 if (memcmp(banner, "PCnetMobile:", 12) != 0) {
1532 printf("%s: failed to complete selftest (bad banner)\n",
1533 sc->sc_dev.dv_xname);
1534 for (i = 0; i < AWI_BANNER_LEN; i++)
1535 printf("%s%02x", i ? ":" : "\t", banner[i]);
1536 printf("\n");
1537 return ENXIO;
1538 }
1539
1540 /* initializing interrupt */
1541 sc->sc_enab_intr = 1;
1542 error = awi_intr_lock(sc);
1543 if (error)
1544 return error;
1545 intmask = AWI_INT_GROGGY | AWI_INT_SCAN_CMPLT |
1546 AWI_INT_TX | AWI_INT_RX | AWI_INT_CMD;
1547 awi_write_1(sc, AWI_INTMASK, ~intmask & 0xff);
1548 awi_write_1(sc, AWI_INTMASK2, 0);
1549 awi_write_1(sc, AWI_INTSTAT, 0);
1550 awi_write_1(sc, AWI_INTSTAT2, 0);
1551 awi_intr_unlock(sc);
1552 am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_ENECINT);
1553
1554 /* issueing interface test command */
1555 error = awi_cmd(sc, AWI_CMD_NOP);
1556 if (error) {
1557 printf("%s: failed to complete selftest", sc->sc_dev.dv_xname);
1558 if (error != EWOULDBLOCK)
1559 printf(" (error %d)\n", error);
1560 else if (sc->sc_cansleep)
1561 printf(" (lost interrupt)\n");
1562 else
1563 printf(" (command timeout)\n");
1564 }
1565 return error;
1566 }
1567
1568 /*
1569 * Extract the factory default MIB value from firmware and assign the driver
1570 * default value.
1571 * Called once at attaching the interface.
1572 */
1573
1574 static int
1575 awi_init_mibs(sc)
1576 struct awi_softc *sc;
1577 {
1578 int i, error;
1579 u_int8_t *rate;
1580
1581 if ((error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_LOCAL)) != 0 ||
1582 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_ADDR)) != 0 ||
1583 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MAC)) != 0 ||
1584 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MGT)) != 0 ||
1585 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_PHY)) != 0) {
1586 printf("%s: failed to get default mib value (error %d)\n",
1587 sc->sc_dev.dv_xname, error);
1588 return error;
1589 }
1590
1591 rate = sc->sc_mib_phy.aSuprt_Data_Rates;
1592 sc->sc_tx_rate = AWI_RATE_1MBIT;
1593 for (i = 0; i < rate[1]; i++) {
1594 if (AWI_80211_RATE(rate[2 + i]) > sc->sc_tx_rate)
1595 sc->sc_tx_rate = AWI_80211_RATE(rate[2 + i]);
1596 }
1597 awi_init_region(sc);
1598 memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
1599 sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
1600 sc->sc_mib_local.Fragmentation_Dis = 1;
1601 sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
1602
1603 /* allocate buffers */
1604 sc->sc_txbase = AWI_BUFFERS;
1605 sc->sc_txend = sc->sc_txbase +
1606 (AWI_TXD_SIZE + sizeof(struct ieee80211_frame) +
1607 sizeof(struct ether_header) + ETHERMTU) * AWI_NTXBUFS;
1608 LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Offset, sc->sc_txbase);
1609 LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Size,
1610 sc->sc_txend - sc->sc_txbase);
1611 LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Offset, sc->sc_txend);
1612 LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Size,
1613 AWI_BUFFERS_END - sc->sc_txend);
1614 sc->sc_mib_local.Network_Mode = 1;
1615 sc->sc_mib_local.Acting_as_AP = 0;
1616 return 0;
1617 }
1618
1619 /*
1620 * Start transmitter and receiver of firmware
1621 * Called after awi_init_hw() to start operation.
1622 */
1623
1624 static int
1625 awi_init_txrx(sc)
1626 struct awi_softc *sc;
1627 {
1628 int error;
1629
1630 /* start transmitter */
1631 sc->sc_txdone = sc->sc_txnext = sc->sc_txbase;
1632 awi_write_4(sc, sc->sc_txbase + AWI_TXD_START, 0);
1633 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NEXT, 0);
1634 awi_write_4(sc, sc->sc_txbase + AWI_TXD_LENGTH, 0);
1635 awi_write_1(sc, sc->sc_txbase + AWI_TXD_RATE, 0);
1636 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NDA, 0);
1637 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NRA, 0);
1638 awi_write_1(sc, sc->sc_txbase + AWI_TXD_STATE, 0);
1639 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_DATA, sc->sc_txbase);
1640 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_MGT, 0);
1641 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_BCAST, 0);
1642 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_PS, 0);
1643 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_CF, 0);
1644 error = awi_cmd(sc, AWI_CMD_INIT_TX);
1645 if (error)
1646 return error;
1647
1648 /* start receiver */
1649 if (sc->sc_rxpend) {
1650 m_freem(sc->sc_rxpend);
1651 sc->sc_rxpend = NULL;
1652 }
1653 error = awi_cmd(sc, AWI_CMD_INIT_RX);
1654 if (error)
1655 return error;
1656 sc->sc_rxdoff = awi_read_4(sc, AWI_CMD_PARAMS+AWI_CA_IRX_DATA_DESC);
1657 sc->sc_rxmoff = awi_read_4(sc, AWI_CMD_PARAMS+AWI_CA_IRX_PS_DESC);
1658 return 0;
1659 }
1660
1661 static void
1662 awi_stop_txrx(sc)
1663 struct awi_softc *sc;
1664 {
1665
1666 (void)awi_cmd(sc, AWI_CMD_KILL_RX);
1667 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_DATA, 1);
1668 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_MGT, 0);
1669 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_BCAST, 0);
1670 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_PS, 0);
1671 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_CF, 0);
1672 (void)awi_cmd(sc, AWI_CMD_FLUSH_TX);
1673 }
1674
1675 static int
1676 awi_init_region(sc)
1677 struct awi_softc *sc;
1678 {
1679
1680 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1681 switch (sc->sc_mib_phy.aCurrent_Reg_Domain) {
1682 case AWI_REG_DOMAIN_US:
1683 case AWI_REG_DOMAIN_CA:
1684 case AWI_REG_DOMAIN_EU:
1685 sc->sc_scan_min = 0;
1686 sc->sc_scan_max = 77;
1687 break;
1688 case AWI_REG_DOMAIN_ES:
1689 sc->sc_scan_min = 0;
1690 sc->sc_scan_max = 26;
1691 break;
1692 case AWI_REG_DOMAIN_FR:
1693 sc->sc_scan_min = 0;
1694 sc->sc_scan_max = 32;
1695 break;
1696 case AWI_REG_DOMAIN_JP:
1697 sc->sc_scan_min = 6;
1698 sc->sc_scan_max = 17;
1699 break;
1700 default:
1701 return EINVAL;
1702 }
1703 sc->sc_scan_cur = sc->sc_scan_min;
1704 sc->sc_scan_set = sc->sc_scan_cur % 3 + 1;
1705 } else {
1706 switch (sc->sc_mib_phy.aCurrent_Reg_Domain) {
1707 case AWI_REG_DOMAIN_US:
1708 case AWI_REG_DOMAIN_CA:
1709 sc->sc_scan_min = 1;
1710 sc->sc_scan_max = 11;
1711 sc->sc_scan_cur = 3;
1712 break;
1713 case AWI_REG_DOMAIN_EU:
1714 sc->sc_scan_min = 1;
1715 sc->sc_scan_max = 13;
1716 sc->sc_scan_cur = 3;
1717 break;
1718 case AWI_REG_DOMAIN_ES:
1719 sc->sc_scan_min = 10;
1720 sc->sc_scan_max = 11;
1721 sc->sc_scan_cur = 10;
1722 break;
1723 case AWI_REG_DOMAIN_FR:
1724 sc->sc_scan_min = 10;
1725 sc->sc_scan_max = 13;
1726 sc->sc_scan_cur = 10;
1727 break;
1728 case AWI_REG_DOMAIN_JP:
1729 sc->sc_scan_min = 14;
1730 sc->sc_scan_max = 14;
1731 sc->sc_scan_cur = 14;
1732 break;
1733 default:
1734 return EINVAL;
1735 }
1736 }
1737 return 0;
1738 }
1739
1740 static int
1741 awi_start_scan(sc)
1742 struct awi_softc *sc;
1743 {
1744 int error = 0;
1745
1746 if (!sc->sc_mib_local.Network_Mode && sc->sc_no_bssid) {
1747 memset(&sc->sc_bss, 0, sizeof(sc->sc_bss));
1748 sc->sc_bss.rxtime = 0;
1749 memcpy(sc->sc_bss.essid, &sc->sc_mib_mac.aDesired_ESS_ID,
1750 sizeof(sc->sc_bss.essid));
1751 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1752 sc->sc_bss.chanset = sc->sc_scan_set;
1753 sc->sc_bss.pattern = sc->sc_scan_cur;
1754 sc->sc_bss.index = 1;
1755 sc->sc_bss.dwell_time = 19; /*XXX*/
1756 } else
1757 sc->sc_bss.chanset = sc->sc_scan_cur;
1758 sc->sc_status = AWI_ST_SETSS;
1759 error = awi_set_ss(sc);
1760 } else {
1761 if (sc->sc_mib_local.Network_Mode)
1762 awi_drvstate(sc, AWI_DRV_INFSC);
1763 else
1764 awi_drvstate(sc, AWI_DRV_ADHSC);
1765 sc->sc_start_bss = 0;
1766 sc->sc_active_scan = 1;
1767 sc->sc_mgt_timer = AWI_ASCAN_WAIT / 1000;
1768 sc->sc_ifp->if_timer = 1;
1769 sc->sc_status = AWI_ST_SCAN;
1770 error = awi_cmd_scan(sc);
1771 }
1772 return error;
1773 }
1774
1775 static int
1776 awi_next_scan(sc)
1777 struct awi_softc *sc;
1778 {
1779 int error;
1780
1781 for (;;) {
1782 /*
1783 * The pattern parameter for FH phy should be incremented
1784 * by 3. But BayStack 650 Access Points apparently always
1785 * assign hop pattern set parameter to 1 for any pattern.
1786 * So we try all combinations of pattern/set parameters.
1787 * Since this causes no error, it may be a bug of
1788 * PCnetMobile firmware.
1789 */
1790 sc->sc_scan_cur++;
1791 if (sc->sc_scan_cur > sc->sc_scan_max) {
1792 sc->sc_scan_cur = sc->sc_scan_min;
1793 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
1794 sc->sc_scan_set = (sc->sc_scan_set + 1) % 3;
1795 }
1796 error = awi_cmd_scan(sc);
1797 if (error != EINVAL)
1798 break;
1799 }
1800 return error;
1801 }
1802
1803 static void
1804 awi_stop_scan(sc)
1805 struct awi_softc *sc;
1806 {
1807 struct ifnet *ifp = sc->sc_ifp;
1808 struct awi_bss *bp, *sbp;
1809
1810 bp = TAILQ_FIRST(&sc->sc_scan);
1811 if (bp == NULL) {
1812 notfound:
1813 if (sc->sc_active_scan) {
1814 if (ifp->if_flags & IFF_DEBUG)
1815 printf("%s: entering passive scan mode\n",
1816 sc->sc_dev.dv_xname);
1817 sc->sc_active_scan = 0;
1818 }
1819 sc->sc_mgt_timer = AWI_PSCAN_WAIT / 1000;
1820 ifp->if_timer = 1;
1821 (void)awi_next_scan(sc);
1822 return;
1823 }
1824 sbp = NULL;
1825 for (; bp != NULL; bp = TAILQ_NEXT(bp, list)) {
1826 if (bp->fails) {
1827 /*
1828 * The configuration of the access points may change
1829 * during my scan. So we retries to associate with
1830 * it unless there are any suitable AP.
1831 */
1832 if (bp->fails < 3)
1833 continue;
1834 bp->fails = 0;
1835 }
1836 if (sc->sc_mib_mac.aDesired_ESS_ID[1] != 0 &&
1837 memcmp(&sc->sc_mib_mac.aDesired_ESS_ID, bp->essid,
1838 sizeof(bp->essid) != 0))
1839 continue;
1840 /*
1841 * Since the firmware apparently scans not only the specified
1842 * channel of SCAN command but all available channel within
1843 * the region, we should filter out unnecessary responses here.
1844 */
1845 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1846 if (bp->pattern < sc->sc_scan_min ||
1847 bp->pattern > sc->sc_scan_max)
1848 continue;
1849 } else {
1850 if (bp->chanset < sc->sc_scan_min ||
1851 bp->chanset > sc->sc_scan_max)
1852 continue;
1853 }
1854 if (sbp == NULL || bp->rssi > sbp->rssi)
1855 sbp = bp;
1856 }
1857 if (sbp == NULL)
1858 goto notfound;
1859 sc->sc_bss = *sbp;
1860 (void)awi_set_ss(sc);
1861 }
1862
1863 static void
1864 awi_recv_beacon(sc, m0, rxts, rssi)
1865 struct awi_softc *sc;
1866 struct mbuf *m0;
1867 u_int32_t rxts;
1868 u_int8_t rssi;
1869 {
1870 struct ieee80211_frame *wh;
1871 struct awi_bss *bp;
1872 u_int8_t *frame, *eframe;
1873 u_int8_t *tstamp, *capinfo, *ssid, *rates, *parms;
1874 u_int16_t bintval;
1875
1876 if (sc->sc_status != AWI_ST_SCAN)
1877 return;
1878 wh = mtod(m0, struct ieee80211_frame *);
1879
1880 frame = (u_int8_t *)&wh[1];
1881 eframe = mtod(m0, u_int8_t *) + m0->m_len;
1882 /*
1883 * XXX:
1884 * timestamp [8]
1885 * beacon interval [2]
1886 * capability information [2]
1887 * ssid [tlv]
1888 * supported rates [tlv]
1889 * parameter set [tlv]
1890 * ...
1891 */
1892 if (frame + 12 > eframe) {
1893 #ifdef AWI_DEBUG
1894 if (awi_verbose)
1895 printf("awi_recv_beacon: frame too short \n");
1896 #endif
1897 return;
1898 }
1899 tstamp = frame;
1900 frame += 8;
1901 bintval = LE_READ_2(frame);
1902 frame += 2;
1903 capinfo = frame;
1904 frame += 2;
1905
1906 if (sc->sc_mib_local.Network_Mode) {
1907 if (!(capinfo[0] & IEEE80211_CAPINFO_ESS) ||
1908 (capinfo[0] & IEEE80211_CAPINFO_IBSS)) {
1909 #ifdef AWI_DEBUG
1910 if (awi_verbose)
1911 printf("awi_recv_beacon: non ESS \n");
1912 #endif
1913 return;
1914 }
1915 } else {
1916 if ((capinfo[0] & IEEE80211_CAPINFO_ESS) ||
1917 !(capinfo[0] & IEEE80211_CAPINFO_IBSS)) {
1918 #ifdef AWI_DEBUG
1919 if (awi_verbose)
1920 printf("awi_recv_beacon: non IBSS \n");
1921 #endif
1922 return;
1923 }
1924 }
1925
1926 ssid = rates = parms = NULL;
1927 while (frame < eframe) {
1928 switch (*frame) {
1929 case IEEE80211_ELEMID_SSID:
1930 ssid = frame;
1931 break;
1932 case IEEE80211_ELEMID_RATES:
1933 rates = frame;
1934 break;
1935 case IEEE80211_ELEMID_FHPARMS:
1936 case IEEE80211_ELEMID_DSPARMS:
1937 parms = frame;
1938 break;
1939 }
1940 frame += frame[1] + 2;
1941 }
1942 if (ssid == NULL || rates == NULL || parms == NULL) {
1943 #ifdef AWI_DEBUG
1944 if (awi_verbose)
1945 printf("awi_recv_beacon: ssid=%p, rates=%p, parms=%p\n",
1946 ssid, rates, parms);
1947 #endif
1948 return;
1949 }
1950 if (ssid[1] > IEEE80211_NWID_LEN) {
1951 #ifdef AWI_DEBUG
1952 if (awi_verbose)
1953 printf("awi_recv_beacon: bad ssid len: %d from %s\n",
1954 ssid[1], ether_sprintf(wh->i_addr2));
1955 #endif
1956 return;
1957 }
1958
1959 for (bp = TAILQ_FIRST(&sc->sc_scan); bp != NULL;
1960 bp = TAILQ_NEXT(bp, list)) {
1961 if (memcmp(bp->esrc, wh->i_addr2, ETHER_ADDR_LEN) == 0 &&
1962 memcmp(bp->bssid, wh->i_addr3, ETHER_ADDR_LEN) == 0)
1963 break;
1964 }
1965 if (bp == NULL) {
1966 bp = malloc(sizeof(struct awi_bss), M_DEVBUF, M_NOWAIT);
1967 if (bp == NULL)
1968 return;
1969 TAILQ_INSERT_TAIL(&sc->sc_scan, bp, list);
1970 memcpy(bp->esrc, wh->i_addr2, ETHER_ADDR_LEN);
1971 memcpy(bp->bssid, wh->i_addr3, ETHER_ADDR_LEN);
1972 memset(bp->essid, 0, sizeof(bp->essid));
1973 memcpy(bp->essid, ssid, 2 + ssid[1]);
1974 }
1975 bp->rssi = rssi;
1976 bp->rxtime = rxts;
1977 memcpy(bp->timestamp, tstamp, sizeof(bp->timestamp));
1978 bp->interval = bintval;
1979 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1980 bp->chanset = parms[4];
1981 bp->pattern = parms[5];
1982 bp->index = parms[6];
1983 bp->dwell_time = LE_READ_2(parms + 2);
1984 } else {
1985 bp->chanset = parms[2];
1986 bp->pattern = 0;
1987 bp->index = 0;
1988 bp->dwell_time = 0;
1989 }
1990 if (sc->sc_mgt_timer == 0)
1991 awi_stop_scan(sc);
1992 }
1993
1994 static int
1995 awi_set_ss(sc)
1996 struct awi_softc *sc;
1997 {
1998 struct ifnet *ifp = sc->sc_ifp;
1999 struct awi_bss *bp;
2000 int error;
2001
2002 sc->sc_status = AWI_ST_SETSS;
2003 bp = &sc->sc_bss;
2004 if (ifp->if_flags & IFF_DEBUG) {
2005 printf("%s: ch %d pat %d id %d dw %d iv %d bss %s ssid \"%s\"\n",
2006 sc->sc_dev.dv_xname, bp->chanset,
2007 bp->pattern, bp->index, bp->dwell_time, bp->interval,
2008 ether_sprintf(bp->bssid), bp->essid + 2);
2009 }
2010 memcpy(&sc->sc_mib_mgt.aCurrent_BSS_ID, bp->bssid, ETHER_ADDR_LEN);
2011 memcpy(&sc->sc_mib_mgt.aCurrent_ESS_ID, bp->essid,
2012 AWI_ESS_ID_SIZE);
2013 LE_WRITE_2(&sc->sc_mib_mgt.aBeacon_Period, bp->interval);
2014 error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT);
2015 return error;
2016 }
2017
2018 static void
2019 awi_try_sync(sc)
2020 struct awi_softc *sc;
2021 {
2022 struct awi_bss *bp;
2023
2024 sc->sc_status = AWI_ST_SYNC;
2025 bp = &sc->sc_bss;
2026
2027 if (sc->sc_cmd_inprog) {
2028 if (awi_cmd_wait(sc))
2029 return;
2030 }
2031 sc->sc_cmd_inprog = 1;
2032 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_SET, bp->chanset);
2033 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_PATTERN, bp->pattern);
2034 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_IDX, bp->index);
2035 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_STARTBSS,
2036 sc->sc_start_bss ? 1 : 0);
2037 awi_write_2(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_DWELL, bp->dwell_time);
2038 awi_write_2(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_MBZ, 0);
2039 awi_write_bytes(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_TIMESTAMP,
2040 bp->timestamp, 8);
2041 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_REFTIME, bp->rxtime);
2042 (void)awi_cmd(sc, AWI_CMD_SYNC);
2043 }
2044
2045 static void
2046 awi_sync_done(sc)
2047 struct awi_softc *sc;
2048 {
2049 struct ifnet *ifp = sc->sc_ifp;
2050
2051 if (sc->sc_mib_local.Network_Mode) {
2052 awi_drvstate(sc, AWI_DRV_INFSY);
2053 awi_send_auth(sc);
2054 } else {
2055 printf("%s: synced with %s ssid \"%s\" at chanset %d\n",
2056 sc->sc_dev.dv_xname, ether_sprintf(sc->sc_bss.bssid),
2057 sc->sc_bss.essid + 2, sc->sc_bss.chanset);
2058 awi_drvstate(sc, AWI_DRV_ADHSY);
2059 sc->sc_status = AWI_ST_RUNNING;
2060 ifp->if_flags |= IFF_RUNNING;
2061 awi_start(ifp);
2062 }
2063 }
2064
2065 static void
2066 awi_send_deauth(sc)
2067 struct awi_softc *sc;
2068 {
2069 struct ifnet *ifp = sc->sc_ifp;
2070 struct mbuf *m;
2071 struct ieee80211_frame *wh;
2072 u_int8_t *deauth;
2073
2074 MGETHDR(m, M_DONTWAIT, MT_DATA);
2075 if (m == NULL)
2076 return;
2077 if (ifp->if_flags & IFF_DEBUG)
2078 printf("%s: sending deauth to %s\n", sc->sc_dev.dv_xname,
2079 ether_sprintf(sc->sc_bss.bssid));
2080
2081 wh = mtod(m, struct ieee80211_frame *);
2082 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2083 IEEE80211_FC0_SUBTYPE_AUTH;
2084 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2085 LE_WRITE_2(wh->i_dur, 0);
2086 LE_WRITE_2(wh->i_seq, 0);
2087 memcpy(wh->i_addr1, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2088 memcpy(wh->i_addr2, sc->sc_mib_addr.aMAC_Address, ETHER_ADDR_LEN);
2089 memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2090
2091 deauth = (u_int8_t *)&wh[1];
2092 LE_WRITE_2(deauth, IEEE80211_REASON_AUTH_LEAVE);
2093 deauth += 2;
2094
2095 m->m_pkthdr.len = m->m_len = deauth - mtod(m, u_int8_t *);
2096 IF_ENQUEUE(&sc->sc_mgtq, m);
2097 awi_start(ifp);
2098 awi_drvstate(sc, AWI_DRV_INFTOSS);
2099 }
2100
2101 static void
2102 awi_send_auth(sc)
2103 struct awi_softc *sc;
2104 {
2105 struct ifnet *ifp = sc->sc_ifp;
2106 struct mbuf *m;
2107 struct ieee80211_frame *wh;
2108 u_int8_t *auth;
2109
2110 MGETHDR(m, M_DONTWAIT, MT_DATA);
2111 if (m == NULL)
2112 return;
2113 sc->sc_status = AWI_ST_AUTH;
2114 if (ifp->if_flags & IFF_DEBUG)
2115 printf("%s: sending auth to %s\n", sc->sc_dev.dv_xname,
2116 ether_sprintf(sc->sc_bss.bssid));
2117
2118 wh = mtod(m, struct ieee80211_frame *);
2119 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2120 IEEE80211_FC0_SUBTYPE_AUTH;
2121 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2122 LE_WRITE_2(wh->i_dur, 0);
2123 LE_WRITE_2(wh->i_seq, 0);
2124 memcpy(wh->i_addr1, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2125 memcpy(wh->i_addr2, sc->sc_mib_addr.aMAC_Address, ETHER_ADDR_LEN);
2126 memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2127
2128 auth = (u_int8_t *)&wh[1];
2129 /* algorithm number */
2130 LE_WRITE_2(auth, IEEE80211_AUTH_ALG_OPEN);
2131 auth += 2;
2132 /* sequence number */
2133 LE_WRITE_2(auth, 1);
2134 auth += 2;
2135 /* status */
2136 LE_WRITE_2(auth, 0);
2137 auth += 2;
2138
2139 m->m_pkthdr.len = m->m_len = auth - mtod(m, u_int8_t *);
2140 IF_ENQUEUE(&sc->sc_mgtq, m);
2141 awi_start(ifp);
2142
2143 sc->sc_mgt_timer = AWI_TRANS_TIMEOUT / 1000;
2144 ifp->if_timer = 1;
2145 }
2146
2147 static void
2148 awi_recv_auth(sc, m0)
2149 struct awi_softc *sc;
2150 struct mbuf *m0;
2151 {
2152 struct ieee80211_frame *wh;
2153 u_int8_t *auth, *eframe;
2154 struct awi_bss *bp;
2155 u_int16_t status;
2156
2157 wh = mtod(m0, struct ieee80211_frame *);
2158 auth = (u_int8_t *)&wh[1];
2159 eframe = mtod(m0, u_int8_t *) + m0->m_len;
2160 if (sc->sc_ifp->if_flags & IFF_DEBUG)
2161 printf("%s: receive auth from %s\n", sc->sc_dev.dv_xname,
2162 ether_sprintf(wh->i_addr2));
2163
2164 if (!sc->sc_mib_local.Network_Mode) {
2165 /* XXX: 802.11 allow auth for IBSS */
2166 return;
2167 }
2168 if (sc->sc_status != AWI_ST_AUTH)
2169 return;
2170 /* algorithm number */
2171 if (LE_READ_2(auth) != IEEE80211_AUTH_ALG_OPEN)
2172 return;
2173 auth += 2;
2174 /* sequence number */
2175 if (LE_READ_2(auth) != 2)
2176 return;
2177 auth += 2;
2178 /* status */
2179 status = LE_READ_2(auth);
2180 if (status != 0) {
2181 printf("%s: authentication failed (reason %d)\n",
2182 sc->sc_dev.dv_xname, status);
2183 for (bp = TAILQ_FIRST(&sc->sc_scan); bp != NULL;
2184 bp = TAILQ_NEXT(bp, list)) {
2185 if (memcmp(bp->esrc, sc->sc_bss.esrc, ETHER_ADDR_LEN)
2186 == 0) {
2187 bp->fails++;
2188 break;
2189 }
2190 }
2191 return;
2192 }
2193 sc->sc_mgt_timer = 0;
2194 awi_drvstate(sc, AWI_DRV_INFAUTH);
2195 awi_send_asreq(sc, 0);
2196 }
2197
2198 static void
2199 awi_send_asreq(sc, reassoc)
2200 struct awi_softc *sc;
2201 int reassoc;
2202 {
2203 struct ifnet *ifp = sc->sc_ifp;
2204 struct mbuf *m;
2205 struct ieee80211_frame *wh;
2206 u_int16_t lintval;
2207 u_int8_t *asreq;
2208
2209 MGETHDR(m, M_DONTWAIT, MT_DATA);
2210 if (m == NULL)
2211 return;
2212 sc->sc_status = AWI_ST_ASSOC;
2213 if (ifp->if_flags & IFF_DEBUG)
2214 printf("%s: sending %sassoc req to %s\n", sc->sc_dev.dv_xname,
2215 reassoc ? "re" : "",
2216 ether_sprintf(sc->sc_bss.bssid));
2217
2218 wh = mtod(m, struct ieee80211_frame *);
2219 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT;
2220 if (reassoc)
2221 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_REASSOC_REQ;
2222 else
2223 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_ASSOC_REQ;
2224 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2225 LE_WRITE_2(wh->i_dur, 0);
2226 LE_WRITE_2(wh->i_seq, 0);
2227 memcpy(wh->i_addr1, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2228 memcpy(wh->i_addr2, sc->sc_mib_addr.aMAC_Address, ETHER_ADDR_LEN);
2229 memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2230
2231 asreq = (u_int8_t *)&wh[1];
2232
2233 /* capability info */
2234 LE_WRITE_2(asreq, IEEE80211_CAPINFO_CF_POLLABLE);
2235 asreq += 2;
2236 /* listen interval */
2237 lintval = LE_READ_2(&sc->sc_mib_mgt.aListen_Interval);
2238 LE_WRITE_2(asreq, lintval);
2239 asreq += 2;
2240 if (reassoc) {
2241 /* current AP address */
2242 memcpy(asreq, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2243 asreq += ETHER_ADDR_LEN;
2244 }
2245 /* ssid */
2246 memcpy(asreq, sc->sc_bss.essid, 2 + sc->sc_bss.essid[1]);
2247 asreq += 2 + asreq[1];
2248 /* supported rates */
2249 memcpy(asreq, &sc->sc_mib_phy.aSuprt_Data_Rates, 4);
2250 asreq += 2 + asreq[1];
2251
2252 m->m_pkthdr.len = m->m_len = asreq - mtod(m, u_int8_t *);
2253 IF_ENQUEUE(&sc->sc_mgtq, m);
2254 awi_start(ifp);
2255
2256 sc->sc_mgt_timer = AWI_TRANS_TIMEOUT / 1000;
2257 ifp->if_timer = 1;
2258 }
2259
2260 static void
2261 awi_recv_asresp(sc, m0)
2262 struct awi_softc *sc;
2263 struct mbuf *m0;
2264 {
2265 struct ieee80211_frame *wh;
2266 u_int8_t *asresp, *eframe;
2267 u_int16_t status;
2268 u_int8_t rate, *phy_rates;
2269 struct awi_bss *bp;
2270 int i, j;
2271
2272 wh = mtod(m0, struct ieee80211_frame *);
2273 asresp = (u_int8_t *)&wh[1];
2274 eframe = mtod(m0, u_int8_t *) + m0->m_len;
2275 if (sc->sc_ifp->if_flags & IFF_DEBUG)
2276 printf("%s: receive assoc resp from %s\n", sc->sc_dev.dv_xname,
2277 ether_sprintf(wh->i_addr2));
2278
2279 if (!sc->sc_mib_local.Network_Mode)
2280 return;
2281
2282 if (sc->sc_status != AWI_ST_ASSOC)
2283 return;
2284 /* capability info */
2285 asresp += 2;
2286 /* status */
2287 status = LE_READ_2(asresp);
2288 if (status != 0) {
2289 printf("%s: association failed (reason %d)\n",
2290 sc->sc_dev.dv_xname, status);
2291 for (bp = TAILQ_FIRST(&sc->sc_scan); bp != NULL;
2292 bp = TAILQ_NEXT(bp, list)) {
2293 if (memcmp(bp->esrc, sc->sc_bss.esrc, ETHER_ADDR_LEN)
2294 == 0) {
2295 bp->fails++;
2296 break;
2297 }
2298 }
2299 return;
2300 }
2301 asresp += 2;
2302 /* association id */
2303 asresp += 2;
2304 /* supported rates */
2305 rate = AWI_RATE_1MBIT;
2306 for (i = 0; i < asresp[1]; i++) {
2307 if (AWI_80211_RATE(asresp[2 + i]) <= rate)
2308 continue;
2309 phy_rates = sc->sc_mib_phy.aSuprt_Data_Rates;
2310 for (j = 0; j < phy_rates[1]; j++) {
2311 if (AWI_80211_RATE(asresp[2 + i]) ==
2312 AWI_80211_RATE(phy_rates[2 + j]))
2313 rate = AWI_80211_RATE(asresp[2 + i]);
2314 }
2315 }
2316 printf("%s: associated with %s ssid \"%s\"",
2317 sc->sc_dev.dv_xname, ether_sprintf(sc->sc_bss.bssid),
2318 sc->sc_bss.essid + 2);
2319 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
2320 printf(" chanset %d pattern %d",
2321 sc->sc_bss.chanset, sc->sc_bss.pattern);
2322 else
2323 printf(" channel %d", sc->sc_bss.chanset);
2324 printf(" signal %d\n", sc->sc_bss.rssi);
2325 sc->sc_tx_rate = rate;
2326 sc->sc_mgt_timer = 0;
2327 sc->sc_rx_timer = 10;
2328 sc->sc_ifp->if_timer = 1;
2329 sc->sc_status = AWI_ST_RUNNING;
2330 sc->sc_ifp->if_flags |= IFF_RUNNING;
2331 awi_drvstate(sc, AWI_DRV_INFASSOC);
2332 while ((bp = TAILQ_FIRST(&sc->sc_scan)) != NULL)
2333 TAILQ_REMOVE(&sc->sc_scan, bp, list);
2334 awi_start(sc->sc_ifp);
2335 }
2336
2337 static int
2338 awi_mib(sc, cmd, mib)
2339 struct awi_softc *sc;
2340 u_int8_t cmd;
2341 u_int8_t mib;
2342 {
2343 int error;
2344 u_int8_t size, *ptr;
2345
2346 switch (mib) {
2347 case AWI_MIB_LOCAL:
2348 ptr = (u_int8_t *)&sc->sc_mib_local;
2349 size = sizeof(sc->sc_mib_local);
2350 break;
2351 case AWI_MIB_ADDR:
2352 ptr = (u_int8_t *)&sc->sc_mib_addr;
2353 size = sizeof(sc->sc_mib_addr);
2354 break;
2355 case AWI_MIB_MAC:
2356 ptr = (u_int8_t *)&sc->sc_mib_mac;
2357 size = sizeof(sc->sc_mib_mac);
2358 break;
2359 case AWI_MIB_STAT:
2360 ptr = (u_int8_t *)&sc->sc_mib_stat;
2361 size = sizeof(sc->sc_mib_stat);
2362 break;
2363 case AWI_MIB_MGT:
2364 ptr = (u_int8_t *)&sc->sc_mib_mgt;
2365 size = sizeof(sc->sc_mib_mgt);
2366 break;
2367 case AWI_MIB_PHY:
2368 ptr = (u_int8_t *)&sc->sc_mib_phy;
2369 size = sizeof(sc->sc_mib_phy);
2370 break;
2371 default:
2372 return EINVAL;
2373 }
2374 if (sc->sc_cmd_inprog) {
2375 error = awi_cmd_wait(sc);
2376 if (error) {
2377 printf("awi_mib: cmd %d inprog\n",
2378 awi_read_1(sc, AWI_CMD));
2379 return error;
2380 }
2381 }
2382 sc->sc_cmd_inprog = 1;
2383 if (cmd == AWI_CMD_SET_MIB)
2384 awi_write_bytes(sc, AWI_CMD_PARAMS+AWI_CA_MIB_DATA, ptr, size);
2385 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_MIB_TYPE, mib);
2386 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_MIB_SIZE, size);
2387 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_MIB_INDEX, 0);
2388 error = awi_cmd(sc, cmd);
2389 if (error)
2390 return error;
2391 if (cmd == AWI_CMD_GET_MIB) {
2392 awi_read_bytes(sc, AWI_CMD_PARAMS+AWI_CA_MIB_DATA, ptr, size);
2393 #ifdef AWI_DEBUG
2394 if (awi_verbose) {
2395 int i;
2396
2397 printf("awi_mib: #%d:", mib);
2398 for (i = 0; i < size; i++)
2399 printf(" %02x", ptr[i]);
2400 printf("\n");
2401 }
2402 #endif
2403 }
2404 return 0;
2405 }
2406
2407 static int
2408 awi_cmd_scan(sc)
2409 struct awi_softc *sc;
2410 {
2411 int error;
2412 u_int8_t scan_mode;
2413
2414 if (sc->sc_active_scan)
2415 scan_mode = AWI_SCAN_ACTIVE;
2416 else
2417 scan_mode = AWI_SCAN_PASSIVE;
2418 if (sc->sc_mib_mgt.aScan_Mode != scan_mode) {
2419 sc->sc_mib_mgt.aScan_Mode = scan_mode;
2420 error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT);
2421 return error;
2422 }
2423
2424 if (sc->sc_cmd_inprog) {
2425 error = awi_cmd_wait(sc);
2426 if (error)
2427 return error;
2428 }
2429 sc->sc_cmd_inprog = 1;
2430 awi_write_2(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_DURATION,
2431 sc->sc_active_scan ? AWI_ASCAN_DURATION : AWI_PSCAN_DURATION);
2432 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
2433 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_SET,
2434 sc->sc_scan_set);
2435 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_PATTERN,
2436 sc->sc_scan_cur);
2437 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_IDX, 1);
2438 } else {
2439 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_SET,
2440 sc->sc_scan_cur);
2441 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_PATTERN, 0);
2442 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_IDX, 0);
2443 }
2444 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_SUSP, 0);
2445 return awi_cmd(sc, AWI_CMD_SCAN);
2446 }
2447
2448 static int
2449 awi_cmd(sc, cmd)
2450 struct awi_softc *sc;
2451 u_int8_t cmd;
2452 {
2453 u_int8_t status;
2454 int error = 0;
2455
2456 sc->sc_cmd_inprog = 1;
2457 awi_write_1(sc, AWI_CMD_STATUS, AWI_STAT_IDLE);
2458 awi_write_1(sc, AWI_CMD, cmd);
2459 if (sc->sc_status != AWI_ST_INIT)
2460 return 0;
2461 error = awi_cmd_wait(sc);
2462 if (error)
2463 return error;
2464 status = awi_read_1(sc, AWI_CMD_STATUS);
2465 awi_write_1(sc, AWI_CMD, 0);
2466 switch (status) {
2467 case AWI_STAT_OK:
2468 break;
2469 case AWI_STAT_BADPARM:
2470 return EINVAL;
2471 default:
2472 printf("%s: command %d failed %x\n",
2473 sc->sc_dev.dv_xname, cmd, status);
2474 return ENXIO;
2475 }
2476 return 0;
2477 }
2478
2479 static void
2480 awi_cmd_done(sc)
2481 struct awi_softc *sc;
2482 {
2483 u_int8_t cmd, status;
2484
2485 status = awi_read_1(sc, AWI_CMD_STATUS);
2486 if (status == AWI_STAT_IDLE)
2487 return; /* stray interrupt */
2488
2489 sc->sc_cmd_inprog = 0;
2490 if (sc->sc_status == AWI_ST_INIT) {
2491 wakeup(sc);
2492 return;
2493 }
2494 cmd = awi_read_1(sc, AWI_CMD);
2495 awi_write_1(sc, AWI_CMD, 0);
2496
2497 if (status != AWI_STAT_OK) {
2498 printf("%s: command %d failed %x\n",
2499 sc->sc_dev.dv_xname, cmd, status);
2500 return;
2501 }
2502 switch (sc->sc_status) {
2503 case AWI_ST_SCAN:
2504 if (cmd == AWI_CMD_SET_MIB)
2505 awi_cmd_scan(sc); /* retry */
2506 break;
2507 case AWI_ST_SETSS:
2508 awi_try_sync(sc);
2509 break;
2510 case AWI_ST_SYNC:
2511 awi_sync_done(sc);
2512 break;
2513 default:
2514 break;
2515 }
2516 }
2517
2518 static int
2519 awi_next_txd(sc, len, framep, ntxdp)
2520 struct awi_softc *sc;
2521 int len;
2522 u_int32_t *framep, *ntxdp;
2523 {
2524 u_int32_t txd, ntxd, frame;
2525
2526 txd = sc->sc_txnext;
2527 frame = txd + AWI_TXD_SIZE;
2528 if (frame + len > sc->sc_txend)
2529 frame = sc->sc_txbase;
2530 ntxd = frame + len;
2531 if (ntxd + AWI_TXD_SIZE > sc->sc_txend)
2532 ntxd = sc->sc_txbase;
2533 *framep = frame;
2534 *ntxdp = ntxd;
2535 /*
2536 * Determine if there are any room in ring buffer.
2537 * --- send wait, === new data, +++ conflict (ENOBUFS)
2538 * base........................end
2539 * done----txd=====ntxd OK
2540 * --txd=====done++++ntxd-- full
2541 * --txd=====ntxd done-- OK
2542 * ==ntxd done----txd=== OK
2543 * ==done++++ntxd----txd=== full
2544 * ++ntxd txd=====done++ full
2545 */
2546 if (txd < ntxd) {
2547 if (txd < sc->sc_txdone && ntxd + AWI_TXD_SIZE > sc->sc_txdone)
2548 return ENOBUFS;
2549 } else {
2550 if (txd < sc->sc_txdone || ntxd + AWI_TXD_SIZE > sc->sc_txdone)
2551 return ENOBUFS;
2552 }
2553 return 0;
2554 }
2555
2556 static int
2557 awi_lock(sc)
2558 struct awi_softc *sc;
2559 {
2560 int error = 0;
2561
2562 if (sc->sc_invalid)
2563 return ENXIO;
2564 if (curproc == NULL) {
2565 /*
2566 * XXX
2567 * Though driver ioctl should be called with context,
2568 * KAME ipv6 stack calls ioctl in interrupt for now.
2569 * We simply abort the request if there are other
2570 * ioctl requests in progress.
2571 */
2572 if (sc->sc_busy)
2573 return EWOULDBLOCK;
2574 sc->sc_busy = 1;
2575 sc->sc_cansleep = 0;
2576 return 0;
2577 }
2578 while (sc->sc_busy) {
2579 sc->sc_sleep_cnt++;
2580 error = tsleep(sc, PWAIT | PCATCH, "awilck", 0);
2581 sc->sc_sleep_cnt--;
2582 if (error)
2583 return error;
2584 if (sc->sc_invalid)
2585 return ENXIO;
2586 }
2587 sc->sc_busy = 1;
2588 sc->sc_cansleep = 1;
2589 return 0;
2590 }
2591
2592 static void
2593 awi_unlock(sc)
2594 struct awi_softc *sc;
2595 {
2596 sc->sc_busy = 0;
2597 sc->sc_cansleep = 0;
2598 if (sc->sc_sleep_cnt)
2599 wakeup(sc);
2600 }
2601
2602 static int
2603 awi_intr_lock(sc)
2604 struct awi_softc *sc;
2605 {
2606 u_int8_t status;
2607 int i, retry;
2608
2609 status = 1;
2610 for (retry = 0; retry < 10; retry++) {
2611 for (i = 0; i < AWI_LOCKOUT_TIMEOUT*1000/5; i++) {
2612 status = awi_read_1(sc, AWI_LOCKOUT_HOST);
2613 if (status == 0)
2614 break;
2615 DELAY(5);
2616 }
2617 if (status != 0)
2618 break;
2619 awi_write_1(sc, AWI_LOCKOUT_MAC, 1);
2620 status = awi_read_1(sc, AWI_LOCKOUT_HOST);
2621 if (status == 0)
2622 break;
2623 awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
2624 }
2625 if (status != 0) {
2626 printf("%s: failed to lock interrupt\n",
2627 sc->sc_dev.dv_xname);
2628 return ENXIO;
2629 }
2630 return 0;
2631 }
2632
2633 static void
2634 awi_intr_unlock(sc)
2635 struct awi_softc *sc;
2636 {
2637
2638 awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
2639 }
2640
2641 static int
2642 awi_cmd_wait(sc)
2643 struct awi_softc *sc;
2644 {
2645 int i, error = 0;
2646
2647 i = 0;
2648 while (sc->sc_cmd_inprog) {
2649 if (sc->sc_invalid)
2650 return ENXIO;
2651 if (sc->sc_cansleep) {
2652 sc->sc_sleep_cnt++;
2653 error = tsleep(sc, PWAIT, "awicmd",
2654 AWI_CMD_TIMEOUT*hz/1000);
2655 sc->sc_sleep_cnt--;
2656 } else {
2657 if (awi_read_1(sc, AWI_CMD_STATUS) != AWI_STAT_IDLE) {
2658 awi_cmd_done(sc);
2659 break;
2660 }
2661 if (i++ >= AWI_CMD_TIMEOUT*1000/10)
2662 error = EWOULDBLOCK;
2663 else
2664 DELAY(10);
2665 }
2666 if (error)
2667 break;
2668 }
2669 return error;
2670 }
2671
2672 #ifdef AWI_DEBUG
2673 static void
2674 awi_dump_pkt(sc, m, rssi)
2675 struct awi_softc *sc;
2676 struct mbuf *m;
2677 u_int8_t rssi;
2678 {
2679 struct ieee80211_frame *wh;
2680 int i, l;
2681
2682 wh = mtod(m, struct ieee80211_frame *);
2683
2684 if (awi_dump_mask != 0 &&
2685 ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK)==IEEE80211_FC1_DIR_NODS) &&
2686 ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK)==IEEE80211_FC0_TYPE_MGT)) {
2687 if ((AWI_DUMP_MASK(wh->i_fc[0]) & awi_dump_mask) != 0)
2688 return;
2689 }
2690 if (awi_dump_mask < 0 &&
2691 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK)==IEEE80211_FC0_TYPE_DATA)
2692 return;
2693
2694 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
2695 case IEEE80211_FC1_DIR_NODS:
2696 printf("rx: NODS %s", ether_sprintf(wh->i_addr2));
2697 printf("->%s", ether_sprintf(wh->i_addr1));
2698 printf("(%s)", ether_sprintf(wh->i_addr3));
2699 break;
2700 case IEEE80211_FC1_DIR_TODS:
2701 printf("rx: TODS %s", ether_sprintf(wh->i_addr2));
2702 printf("->%s", ether_sprintf(wh->i_addr3));
2703 printf("(%s)", ether_sprintf(wh->i_addr1));
2704 break;
2705 case IEEE80211_FC1_DIR_FROMDS:
2706 printf("rx: FRDS %s", ether_sprintf(wh->i_addr3));
2707 printf("->%s", ether_sprintf(wh->i_addr1));
2708 printf("(%s)", ether_sprintf(wh->i_addr2));
2709 break;
2710 case IEEE80211_FC1_DIR_DSTODS:
2711 printf("rx: DSDS %s", ether_sprintf((u_int8_t *)&wh[1]));
2712 printf("->%s", ether_sprintf(wh->i_addr3));
2713 printf("(%s", ether_sprintf(wh->i_addr2));
2714 printf("->%s)", ether_sprintf(wh->i_addr1));
2715 break;
2716 }
2717 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
2718 case IEEE80211_FC0_TYPE_DATA:
2719 printf(" data");
2720 break;
2721 case IEEE80211_FC0_TYPE_MGT:
2722 switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
2723 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2724 printf(" probe_req");
2725 break;
2726 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2727 printf(" probe_resp");
2728 break;
2729 case IEEE80211_FC0_SUBTYPE_BEACON:
2730 printf(" beacon");
2731 break;
2732 case IEEE80211_FC0_SUBTYPE_AUTH:
2733 printf(" auth");
2734 break;
2735 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2736 printf(" assoc_req");
2737 break;
2738 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2739 printf(" assoc_resp");
2740 break;
2741 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2742 printf(" reassoc_req");
2743 break;
2744 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2745 printf(" reassoc_resp");
2746 break;
2747 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2748 printf(" deauth");
2749 break;
2750 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2751 printf(" disassoc");
2752 break;
2753 default:
2754 printf(" mgt#%d",
2755 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
2756 break;
2757 }
2758 break;
2759 default:
2760 printf(" type#%d",
2761 wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
2762 break;
2763 }
2764 printf(" +%d\n", rssi);
2765 if (awi_dump_len > 0) {
2766 l = m->m_len;
2767 if (l > awi_dump_len + sizeof(*wh))
2768 l = awi_dump_len + sizeof(*wh);
2769 i = sizeof(*wh);
2770 if (awi_dump_hdr)
2771 i = 0;
2772 for (; i < l; i++) {
2773 if ((i & 1) == 0)
2774 printf(" ");
2775 printf("%02x", mtod(m, u_int8_t *)[i]);
2776 }
2777 printf("\n");
2778 }
2779 }
2780 #endif
2781