awi.c revision 1.15 1 /* $NetBSD: awi.c,v 1.15 2000/03/27 12:54:59 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 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 sc->sc_enab_intr = 0;
1494 awi_drvstate(sc, AWI_DRV_RESET);
1495
1496 /* reset firmware */
1497 am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1498 DELAY(100);
1499 awi_write_1(sc, AWI_SELFTEST, 0);
1500 am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1501 DELAY(100);
1502
1503 /* wait for selftest completion */
1504 for (i = 0; ; i++) {
1505 if (i >= AWI_SELFTEST_TIMEOUT*hz/1000) {
1506 printf("%s: failed to complete selftest (timeout)\n",
1507 sc->sc_dev.dv_xname);
1508 return ENXIO;
1509 }
1510 status = awi_read_1(sc, AWI_SELFTEST);
1511 if ((status & 0xf0) == 0xf0)
1512 break;
1513 if (sc->sc_cansleep) {
1514 sc->sc_sleep_cnt++;
1515 (void)tsleep(sc, PWAIT, "awitst", 1);
1516 sc->sc_sleep_cnt--;
1517 } else {
1518 DELAY(1000*1000/hz);
1519 }
1520 }
1521 if (status != AWI_SELFTEST_PASSED) {
1522 printf("%s: failed to complete selftest (code %x)\n",
1523 sc->sc_dev.dv_xname, status);
1524 return ENXIO;
1525 }
1526
1527 /* check banner to confirm firmware write it */
1528 awi_read_bytes(sc, AWI_BANNER, banner, AWI_BANNER_LEN);
1529 if (memcmp(banner, "PCnetMobile:", 12) != 0) {
1530 printf("%s: failed to complete selftest (bad banner)\n",
1531 sc->sc_dev.dv_xname);
1532 for (i = 0; i < AWI_BANNER_LEN; i++)
1533 printf("%s%02x", i ? ":" : "\t", banner[i]);
1534 printf("\n");
1535 return ENXIO;
1536 }
1537
1538 /* initializing interrupt */
1539 sc->sc_enab_intr = 1;
1540 error = awi_intr_lock(sc);
1541 if (error)
1542 return error;
1543 intmask = AWI_INT_GROGGY | AWI_INT_SCAN_CMPLT |
1544 AWI_INT_TX | AWI_INT_RX | AWI_INT_CMD;
1545 awi_write_1(sc, AWI_INTMASK, ~intmask & 0xff);
1546 awi_write_1(sc, AWI_INTMASK2, 0);
1547 awi_write_1(sc, AWI_INTSTAT, 0);
1548 awi_write_1(sc, AWI_INTSTAT2, 0);
1549 awi_intr_unlock(sc);
1550 am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_ENECINT);
1551
1552 /* issueing interface test command */
1553 error = awi_cmd(sc, AWI_CMD_NOP);
1554 if (error) {
1555 printf("%s: failed to complete selftest", sc->sc_dev.dv_xname);
1556 if (error != EWOULDBLOCK)
1557 printf(" (error %d)\n", error);
1558 else if (sc->sc_cansleep)
1559 printf(" (lost interrupt)\n");
1560 else
1561 printf(" (command timeout)\n");
1562 }
1563 return error;
1564 }
1565
1566 /*
1567 * Extract the factory default MIB value from firmware and assign the driver
1568 * default value.
1569 * Called once at attaching the interface.
1570 */
1571
1572 static int
1573 awi_init_mibs(sc)
1574 struct awi_softc *sc;
1575 {
1576 int i, error;
1577 u_int8_t *rate;
1578
1579 if ((error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_LOCAL)) != 0 ||
1580 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_ADDR)) != 0 ||
1581 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MAC)) != 0 ||
1582 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MGT)) != 0 ||
1583 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_PHY)) != 0) {
1584 printf("%s: failed to get default mib value (error %d)\n",
1585 sc->sc_dev.dv_xname, error);
1586 return error;
1587 }
1588
1589 rate = sc->sc_mib_phy.aSuprt_Data_Rates;
1590 sc->sc_tx_rate = AWI_RATE_1MBIT;
1591 for (i = 0; i < rate[1]; i++) {
1592 if (AWI_80211_RATE(rate[2 + i]) > sc->sc_tx_rate)
1593 sc->sc_tx_rate = AWI_80211_RATE(rate[2 + i]);
1594 }
1595 awi_init_region(sc);
1596 memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
1597 sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
1598 sc->sc_mib_local.Fragmentation_Dis = 1;
1599 sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
1600
1601 /* allocate buffers */
1602 sc->sc_txbase = AWI_BUFFERS;
1603 sc->sc_txend = sc->sc_txbase +
1604 (AWI_TXD_SIZE + sizeof(struct ieee80211_frame) +
1605 sizeof(struct ether_header) + ETHERMTU) * AWI_NTXBUFS;
1606 LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Offset, sc->sc_txbase);
1607 LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Size,
1608 sc->sc_txend - sc->sc_txbase);
1609 LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Offset, sc->sc_txend);
1610 LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Size,
1611 AWI_BUFFERS_END - sc->sc_txend);
1612 sc->sc_mib_local.Network_Mode = 1;
1613 sc->sc_mib_local.Acting_as_AP = 0;
1614 return 0;
1615 }
1616
1617 /*
1618 * Start transmitter and receiver of firmware
1619 * Called after awi_init_hw() to start operation.
1620 */
1621
1622 static int
1623 awi_init_txrx(sc)
1624 struct awi_softc *sc;
1625 {
1626 int error;
1627
1628 /* start transmitter */
1629 sc->sc_txdone = sc->sc_txnext = sc->sc_txbase;
1630 awi_write_4(sc, sc->sc_txbase + AWI_TXD_START, 0);
1631 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NEXT, 0);
1632 awi_write_4(sc, sc->sc_txbase + AWI_TXD_LENGTH, 0);
1633 awi_write_1(sc, sc->sc_txbase + AWI_TXD_RATE, 0);
1634 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NDA, 0);
1635 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NRA, 0);
1636 awi_write_1(sc, sc->sc_txbase + AWI_TXD_STATE, 0);
1637 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_DATA, sc->sc_txbase);
1638 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_MGT, 0);
1639 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_BCAST, 0);
1640 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_PS, 0);
1641 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_CF, 0);
1642 error = awi_cmd(sc, AWI_CMD_INIT_TX);
1643 if (error)
1644 return error;
1645
1646 /* start receiver */
1647 if (sc->sc_rxpend) {
1648 m_freem(sc->sc_rxpend);
1649 sc->sc_rxpend = NULL;
1650 }
1651 error = awi_cmd(sc, AWI_CMD_INIT_RX);
1652 if (error)
1653 return error;
1654 sc->sc_rxdoff = awi_read_4(sc, AWI_CMD_PARAMS+AWI_CA_IRX_DATA_DESC);
1655 sc->sc_rxmoff = awi_read_4(sc, AWI_CMD_PARAMS+AWI_CA_IRX_PS_DESC);
1656 return 0;
1657 }
1658
1659 static void
1660 awi_stop_txrx(sc)
1661 struct awi_softc *sc;
1662 {
1663
1664 (void)awi_cmd(sc, AWI_CMD_KILL_RX);
1665 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_DATA, 1);
1666 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_MGT, 0);
1667 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_BCAST, 0);
1668 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_PS, 0);
1669 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_CF, 0);
1670 (void)awi_cmd(sc, AWI_CMD_FLUSH_TX);
1671 }
1672
1673 static int
1674 awi_init_region(sc)
1675 struct awi_softc *sc;
1676 {
1677
1678 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1679 switch (sc->sc_mib_phy.aCurrent_Reg_Domain) {
1680 case AWI_REG_DOMAIN_US:
1681 case AWI_REG_DOMAIN_CA:
1682 case AWI_REG_DOMAIN_EU:
1683 sc->sc_scan_min = 0;
1684 sc->sc_scan_max = 77;
1685 break;
1686 case AWI_REG_DOMAIN_ES:
1687 sc->sc_scan_min = 0;
1688 sc->sc_scan_max = 26;
1689 break;
1690 case AWI_REG_DOMAIN_FR:
1691 sc->sc_scan_min = 0;
1692 sc->sc_scan_max = 32;
1693 break;
1694 case AWI_REG_DOMAIN_JP:
1695 sc->sc_scan_min = 6;
1696 sc->sc_scan_max = 17;
1697 break;
1698 default:
1699 return EINVAL;
1700 }
1701 sc->sc_scan_cur = sc->sc_scan_min;
1702 sc->sc_scan_set = sc->sc_scan_cur % 3 + 1;
1703 } else {
1704 switch (sc->sc_mib_phy.aCurrent_Reg_Domain) {
1705 case AWI_REG_DOMAIN_US:
1706 case AWI_REG_DOMAIN_CA:
1707 sc->sc_scan_min = 1;
1708 sc->sc_scan_max = 11;
1709 sc->sc_scan_cur = 3;
1710 break;
1711 case AWI_REG_DOMAIN_EU:
1712 sc->sc_scan_min = 1;
1713 sc->sc_scan_max = 13;
1714 sc->sc_scan_cur = 3;
1715 break;
1716 case AWI_REG_DOMAIN_ES:
1717 sc->sc_scan_min = 10;
1718 sc->sc_scan_max = 11;
1719 sc->sc_scan_cur = 10;
1720 break;
1721 case AWI_REG_DOMAIN_FR:
1722 sc->sc_scan_min = 10;
1723 sc->sc_scan_max = 13;
1724 sc->sc_scan_cur = 10;
1725 break;
1726 case AWI_REG_DOMAIN_JP:
1727 sc->sc_scan_min = 14;
1728 sc->sc_scan_max = 14;
1729 sc->sc_scan_cur = 14;
1730 break;
1731 default:
1732 return EINVAL;
1733 }
1734 }
1735 return 0;
1736 }
1737
1738 static int
1739 awi_start_scan(sc)
1740 struct awi_softc *sc;
1741 {
1742 int error = 0;
1743
1744 if (!sc->sc_mib_local.Network_Mode && sc->sc_no_bssid) {
1745 memset(&sc->sc_bss, 0, sizeof(sc->sc_bss));
1746 sc->sc_bss.rxtime = 0;
1747 memcpy(sc->sc_bss.essid, &sc->sc_mib_mac.aDesired_ESS_ID,
1748 sizeof(sc->sc_bss.essid));
1749 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1750 sc->sc_bss.chanset = sc->sc_scan_set;
1751 sc->sc_bss.pattern = sc->sc_scan_cur;
1752 sc->sc_bss.index = 1;
1753 sc->sc_bss.dwell_time = 19; /*XXX*/
1754 } else
1755 sc->sc_bss.chanset = sc->sc_scan_cur;
1756 sc->sc_status = AWI_ST_SETSS;
1757 error = awi_set_ss(sc);
1758 } else {
1759 if (sc->sc_mib_local.Network_Mode)
1760 awi_drvstate(sc, AWI_DRV_INFSC);
1761 else
1762 awi_drvstate(sc, AWI_DRV_ADHSC);
1763 sc->sc_start_bss = 0;
1764 sc->sc_active_scan = 1;
1765 sc->sc_mgt_timer = AWI_ASCAN_WAIT / 1000;
1766 sc->sc_ifp->if_timer = 1;
1767 sc->sc_status = AWI_ST_SCAN;
1768 error = awi_cmd_scan(sc);
1769 }
1770 return error;
1771 }
1772
1773 static int
1774 awi_next_scan(sc)
1775 struct awi_softc *sc;
1776 {
1777 int error;
1778
1779 for (;;) {
1780 /*
1781 * The pattern parameter for FH phy should be incremented
1782 * by 3. But BayStack 650 Access Points apparently always
1783 * assign hop pattern set parameter to 1 for any pattern.
1784 * So we try all combinations of pattern/set parameters.
1785 * Since this causes no error, it may be a bug of
1786 * PCnetMobile firmware.
1787 */
1788 sc->sc_scan_cur++;
1789 if (sc->sc_scan_cur > sc->sc_scan_max) {
1790 sc->sc_scan_cur = sc->sc_scan_min;
1791 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
1792 sc->sc_scan_set = (sc->sc_scan_set + 1) % 3;
1793 }
1794 error = awi_cmd_scan(sc);
1795 if (error != EINVAL)
1796 break;
1797 }
1798 return error;
1799 }
1800
1801 static void
1802 awi_stop_scan(sc)
1803 struct awi_softc *sc;
1804 {
1805 struct ifnet *ifp = sc->sc_ifp;
1806 struct awi_bss *bp, *sbp;
1807
1808 bp = TAILQ_FIRST(&sc->sc_scan);
1809 if (bp == NULL) {
1810 notfound:
1811 if (sc->sc_active_scan) {
1812 if (ifp->if_flags & IFF_DEBUG)
1813 printf("%s: entering passive scan mode\n",
1814 sc->sc_dev.dv_xname);
1815 sc->sc_active_scan = 0;
1816 }
1817 sc->sc_mgt_timer = AWI_PSCAN_WAIT / 1000;
1818 ifp->if_timer = 1;
1819 (void)awi_next_scan(sc);
1820 return;
1821 }
1822 sbp = NULL;
1823 for (; bp != NULL; bp = TAILQ_NEXT(bp, list)) {
1824 if (bp->fails) {
1825 /*
1826 * The configuration of the access points may change
1827 * during my scan. So we retries to associate with
1828 * it unless there are any suitable AP.
1829 */
1830 if (bp->fails < 3)
1831 continue;
1832 bp->fails = 0;
1833 }
1834 if (sc->sc_mib_mac.aDesired_ESS_ID[1] != 0 &&
1835 memcmp(&sc->sc_mib_mac.aDesired_ESS_ID, bp->essid,
1836 sizeof(bp->essid) != 0))
1837 continue;
1838 /*
1839 * Since the firmware apparently scans not only the specified
1840 * channel of SCAN command but all available channel within
1841 * the region, we should filter out unnecessary responses here.
1842 */
1843 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1844 if (bp->pattern < sc->sc_scan_min ||
1845 bp->pattern > sc->sc_scan_max)
1846 continue;
1847 } else {
1848 if (bp->chanset < sc->sc_scan_min ||
1849 bp->chanset > sc->sc_scan_max)
1850 continue;
1851 }
1852 if (sbp == NULL || bp->rssi > sbp->rssi)
1853 sbp = bp;
1854 }
1855 if (sbp == NULL)
1856 goto notfound;
1857 sc->sc_bss = *sbp;
1858 (void)awi_set_ss(sc);
1859 }
1860
1861 static void
1862 awi_recv_beacon(sc, m0, rxts, rssi)
1863 struct awi_softc *sc;
1864 struct mbuf *m0;
1865 u_int32_t rxts;
1866 u_int8_t rssi;
1867 {
1868 struct ieee80211_frame *wh;
1869 struct awi_bss *bp;
1870 u_int8_t *frame, *eframe;
1871 u_int8_t *tstamp, *capinfo, *ssid, *rates, *parms;
1872 u_int16_t bintval;
1873
1874 if (sc->sc_status != AWI_ST_SCAN)
1875 return;
1876 wh = mtod(m0, struct ieee80211_frame *);
1877
1878 frame = (u_int8_t *)&wh[1];
1879 eframe = mtod(m0, u_int8_t *) + m0->m_len;
1880 /*
1881 * XXX:
1882 * timestamp [8]
1883 * beacon interval [2]
1884 * capability information [2]
1885 * ssid [tlv]
1886 * supported rates [tlv]
1887 * parameter set [tlv]
1888 * ...
1889 */
1890 if (frame + 12 > eframe) {
1891 #ifdef AWI_DEBUG
1892 if (awi_verbose)
1893 printf("awi_recv_beacon: frame too short \n");
1894 #endif
1895 return;
1896 }
1897 tstamp = frame;
1898 frame += 8;
1899 bintval = LE_READ_2(frame);
1900 frame += 2;
1901 capinfo = frame;
1902 frame += 2;
1903
1904 if (sc->sc_mib_local.Network_Mode) {
1905 if (!(capinfo[0] & IEEE80211_CAPINFO_ESS) ||
1906 (capinfo[0] & IEEE80211_CAPINFO_IBSS)) {
1907 #ifdef AWI_DEBUG
1908 if (awi_verbose)
1909 printf("awi_recv_beacon: non ESS \n");
1910 #endif
1911 return;
1912 }
1913 } else {
1914 if ((capinfo[0] & IEEE80211_CAPINFO_ESS) ||
1915 !(capinfo[0] & IEEE80211_CAPINFO_IBSS)) {
1916 #ifdef AWI_DEBUG
1917 if (awi_verbose)
1918 printf("awi_recv_beacon: non IBSS \n");
1919 #endif
1920 return;
1921 }
1922 }
1923
1924 ssid = rates = parms = NULL;
1925 while (frame < eframe) {
1926 switch (*frame) {
1927 case IEEE80211_ELEMID_SSID:
1928 ssid = frame;
1929 break;
1930 case IEEE80211_ELEMID_RATES:
1931 rates = frame;
1932 break;
1933 case IEEE80211_ELEMID_FHPARMS:
1934 case IEEE80211_ELEMID_DSPARMS:
1935 parms = frame;
1936 break;
1937 }
1938 frame += frame[1] + 2;
1939 }
1940 if (ssid == NULL || rates == NULL || parms == NULL) {
1941 #ifdef AWI_DEBUG
1942 if (awi_verbose)
1943 printf("awi_recv_beacon: ssid=%p, rates=%p, parms=%p\n",
1944 ssid, rates, parms);
1945 #endif
1946 return;
1947 }
1948 if (ssid[1] > IEEE80211_NWID_LEN) {
1949 #ifdef AWI_DEBUG
1950 if (awi_verbose)
1951 printf("awi_recv_beacon: bad ssid len: %d from %s\n",
1952 ssid[1], ether_sprintf(wh->i_addr2));
1953 #endif
1954 return;
1955 }
1956
1957 for (bp = TAILQ_FIRST(&sc->sc_scan); bp != NULL;
1958 bp = TAILQ_NEXT(bp, list)) {
1959 if (memcmp(bp->esrc, wh->i_addr2, ETHER_ADDR_LEN) == 0 &&
1960 memcmp(bp->bssid, wh->i_addr3, ETHER_ADDR_LEN) == 0)
1961 break;
1962 }
1963 if (bp == NULL) {
1964 bp = malloc(sizeof(struct awi_bss), M_DEVBUF, M_NOWAIT);
1965 if (bp == NULL)
1966 return;
1967 TAILQ_INSERT_TAIL(&sc->sc_scan, bp, list);
1968 memcpy(bp->esrc, wh->i_addr2, ETHER_ADDR_LEN);
1969 memcpy(bp->bssid, wh->i_addr3, ETHER_ADDR_LEN);
1970 memset(bp->essid, 0, sizeof(bp->essid));
1971 memcpy(bp->essid, ssid, 2 + ssid[1]);
1972 }
1973 bp->rssi = rssi;
1974 bp->rxtime = rxts;
1975 memcpy(bp->timestamp, tstamp, sizeof(bp->timestamp));
1976 bp->interval = bintval;
1977 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1978 bp->chanset = parms[4];
1979 bp->pattern = parms[5];
1980 bp->index = parms[6];
1981 bp->dwell_time = LE_READ_2(parms + 2);
1982 } else {
1983 bp->chanset = parms[2];
1984 bp->pattern = 0;
1985 bp->index = 0;
1986 bp->dwell_time = 0;
1987 }
1988 if (sc->sc_mgt_timer == 0)
1989 awi_stop_scan(sc);
1990 }
1991
1992 static int
1993 awi_set_ss(sc)
1994 struct awi_softc *sc;
1995 {
1996 struct ifnet *ifp = sc->sc_ifp;
1997 struct awi_bss *bp;
1998 int error;
1999
2000 sc->sc_status = AWI_ST_SETSS;
2001 bp = &sc->sc_bss;
2002 if (ifp->if_flags & IFF_DEBUG) {
2003 printf("%s: ch %d pat %d id %d dw %d iv %d bss %s ssid \"%s\"\n",
2004 sc->sc_dev.dv_xname, bp->chanset,
2005 bp->pattern, bp->index, bp->dwell_time, bp->interval,
2006 ether_sprintf(bp->bssid), bp->essid + 2);
2007 }
2008 memcpy(&sc->sc_mib_mgt.aCurrent_BSS_ID, bp->bssid, ETHER_ADDR_LEN);
2009 memcpy(&sc->sc_mib_mgt.aCurrent_ESS_ID, bp->essid,
2010 AWI_ESS_ID_SIZE);
2011 LE_WRITE_2(&sc->sc_mib_mgt.aBeacon_Period, bp->interval);
2012 error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT);
2013 return error;
2014 }
2015
2016 static void
2017 awi_try_sync(sc)
2018 struct awi_softc *sc;
2019 {
2020 struct awi_bss *bp;
2021
2022 sc->sc_status = AWI_ST_SYNC;
2023 bp = &sc->sc_bss;
2024
2025 if (sc->sc_cmd_inprog) {
2026 if (awi_cmd_wait(sc))
2027 return;
2028 }
2029 sc->sc_cmd_inprog = 1;
2030 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_SET, bp->chanset);
2031 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_PATTERN, bp->pattern);
2032 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_IDX, bp->index);
2033 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_STARTBSS,
2034 sc->sc_start_bss ? 1 : 0);
2035 awi_write_2(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_DWELL, bp->dwell_time);
2036 awi_write_2(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_MBZ, 0);
2037 awi_write_bytes(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_TIMESTAMP,
2038 bp->timestamp, 8);
2039 awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_REFTIME, bp->rxtime);
2040 (void)awi_cmd(sc, AWI_CMD_SYNC);
2041 }
2042
2043 static void
2044 awi_sync_done(sc)
2045 struct awi_softc *sc;
2046 {
2047 struct ifnet *ifp = sc->sc_ifp;
2048
2049 if (sc->sc_mib_local.Network_Mode) {
2050 awi_drvstate(sc, AWI_DRV_INFSY);
2051 awi_send_auth(sc);
2052 } else {
2053 printf("%s: synced with %s ssid \"%s\" at chanset %d\n",
2054 sc->sc_dev.dv_xname, ether_sprintf(sc->sc_bss.bssid),
2055 sc->sc_bss.essid + 2, sc->sc_bss.chanset);
2056 awi_drvstate(sc, AWI_DRV_ADHSY);
2057 sc->sc_status = AWI_ST_RUNNING;
2058 ifp->if_flags |= IFF_RUNNING;
2059 awi_start(ifp);
2060 }
2061 }
2062
2063 static void
2064 awi_send_deauth(sc)
2065 struct awi_softc *sc;
2066 {
2067 struct ifnet *ifp = sc->sc_ifp;
2068 struct mbuf *m;
2069 struct ieee80211_frame *wh;
2070 u_int8_t *deauth;
2071
2072 MGETHDR(m, M_DONTWAIT, MT_DATA);
2073 if (m == NULL)
2074 return;
2075 if (ifp->if_flags & IFF_DEBUG)
2076 printf("%s: sending deauth to %s\n", sc->sc_dev.dv_xname,
2077 ether_sprintf(sc->sc_bss.bssid));
2078
2079 wh = mtod(m, struct ieee80211_frame *);
2080 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2081 IEEE80211_FC0_SUBTYPE_AUTH;
2082 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2083 LE_WRITE_2(wh->i_dur, 0);
2084 LE_WRITE_2(wh->i_seq, 0);
2085 memcpy(wh->i_addr1, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2086 memcpy(wh->i_addr2, sc->sc_mib_addr.aMAC_Address, ETHER_ADDR_LEN);
2087 memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2088
2089 deauth = (u_int8_t *)&wh[1];
2090 LE_WRITE_2(deauth, IEEE80211_REASON_AUTH_LEAVE);
2091 deauth += 2;
2092
2093 m->m_pkthdr.len = m->m_len = deauth - mtod(m, u_int8_t *);
2094 IF_ENQUEUE(&sc->sc_mgtq, m);
2095 awi_start(ifp);
2096 awi_drvstate(sc, AWI_DRV_INFTOSS);
2097 }
2098
2099 static void
2100 awi_send_auth(sc)
2101 struct awi_softc *sc;
2102 {
2103 struct ifnet *ifp = sc->sc_ifp;
2104 struct mbuf *m;
2105 struct ieee80211_frame *wh;
2106 u_int8_t *auth;
2107
2108 MGETHDR(m, M_DONTWAIT, MT_DATA);
2109 if (m == NULL)
2110 return;
2111 sc->sc_status = AWI_ST_AUTH;
2112 if (ifp->if_flags & IFF_DEBUG)
2113 printf("%s: sending auth to %s\n", sc->sc_dev.dv_xname,
2114 ether_sprintf(sc->sc_bss.bssid));
2115
2116 wh = mtod(m, struct ieee80211_frame *);
2117 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2118 IEEE80211_FC0_SUBTYPE_AUTH;
2119 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2120 LE_WRITE_2(wh->i_dur, 0);
2121 LE_WRITE_2(wh->i_seq, 0);
2122 memcpy(wh->i_addr1, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2123 memcpy(wh->i_addr2, sc->sc_mib_addr.aMAC_Address, ETHER_ADDR_LEN);
2124 memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2125
2126 auth = (u_int8_t *)&wh[1];
2127 /* algorithm number */
2128 LE_WRITE_2(auth, IEEE80211_AUTH_ALG_OPEN);
2129 auth += 2;
2130 /* sequence number */
2131 LE_WRITE_2(auth, 1);
2132 auth += 2;
2133 /* status */
2134 LE_WRITE_2(auth, 0);
2135 auth += 2;
2136
2137 m->m_pkthdr.len = m->m_len = auth - mtod(m, u_int8_t *);
2138 IF_ENQUEUE(&sc->sc_mgtq, m);
2139 awi_start(ifp);
2140
2141 sc->sc_mgt_timer = AWI_TRANS_TIMEOUT / 1000;
2142 ifp->if_timer = 1;
2143 }
2144
2145 static void
2146 awi_recv_auth(sc, m0)
2147 struct awi_softc *sc;
2148 struct mbuf *m0;
2149 {
2150 struct ieee80211_frame *wh;
2151 u_int8_t *auth, *eframe;
2152 struct awi_bss *bp;
2153 u_int16_t status;
2154
2155 wh = mtod(m0, struct ieee80211_frame *);
2156 auth = (u_int8_t *)&wh[1];
2157 eframe = mtod(m0, u_int8_t *) + m0->m_len;
2158 if (sc->sc_ifp->if_flags & IFF_DEBUG)
2159 printf("%s: receive auth from %s\n", sc->sc_dev.dv_xname,
2160 ether_sprintf(wh->i_addr2));
2161
2162 if (!sc->sc_mib_local.Network_Mode) {
2163 /* XXX: 802.11 allow auth for IBSS */
2164 return;
2165 }
2166 if (sc->sc_status != AWI_ST_AUTH)
2167 return;
2168 /* algorithm number */
2169 if (LE_READ_2(auth) != IEEE80211_AUTH_ALG_OPEN)
2170 return;
2171 auth += 2;
2172 /* sequence number */
2173 if (LE_READ_2(auth) != 2)
2174 return;
2175 auth += 2;
2176 /* status */
2177 status = LE_READ_2(auth);
2178 if (status != 0) {
2179 printf("%s: authentication failed (reason %d)\n",
2180 sc->sc_dev.dv_xname, status);
2181 for (bp = TAILQ_FIRST(&sc->sc_scan); bp != NULL;
2182 bp = TAILQ_NEXT(bp, list)) {
2183 if (memcmp(bp->esrc, sc->sc_bss.esrc, ETHER_ADDR_LEN)
2184 == 0) {
2185 bp->fails++;
2186 break;
2187 }
2188 }
2189 return;
2190 }
2191 sc->sc_mgt_timer = 0;
2192 awi_drvstate(sc, AWI_DRV_INFAUTH);
2193 awi_send_asreq(sc, 0);
2194 }
2195
2196 static void
2197 awi_send_asreq(sc, reassoc)
2198 struct awi_softc *sc;
2199 int reassoc;
2200 {
2201 struct ifnet *ifp = sc->sc_ifp;
2202 struct mbuf *m;
2203 struct ieee80211_frame *wh;
2204 u_int16_t lintval;
2205 u_int8_t *asreq;
2206
2207 MGETHDR(m, M_DONTWAIT, MT_DATA);
2208 if (m == NULL)
2209 return;
2210 sc->sc_status = AWI_ST_ASSOC;
2211 if (ifp->if_flags & IFF_DEBUG)
2212 printf("%s: sending %sassoc req to %s\n", sc->sc_dev.dv_xname,
2213 reassoc ? "re" : "",
2214 ether_sprintf(sc->sc_bss.bssid));
2215
2216 wh = mtod(m, struct ieee80211_frame *);
2217 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT;
2218 if (reassoc)
2219 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_REASSOC_REQ;
2220 else
2221 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_ASSOC_REQ;
2222 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2223 LE_WRITE_2(wh->i_dur, 0);
2224 LE_WRITE_2(wh->i_seq, 0);
2225 memcpy(wh->i_addr1, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2226 memcpy(wh->i_addr2, sc->sc_mib_addr.aMAC_Address, ETHER_ADDR_LEN);
2227 memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2228
2229 asreq = (u_int8_t *)&wh[1];
2230
2231 /* capability info */
2232 LE_WRITE_2(asreq, IEEE80211_CAPINFO_CF_POLLABLE);
2233 asreq += 2;
2234 /* listen interval */
2235 lintval = LE_READ_2(&sc->sc_mib_mgt.aListen_Interval);
2236 LE_WRITE_2(asreq, lintval);
2237 asreq += 2;
2238 if (reassoc) {
2239 /* current AP address */
2240 memcpy(asreq, sc->sc_bss.bssid, ETHER_ADDR_LEN);
2241 asreq += ETHER_ADDR_LEN;
2242 }
2243 /* ssid */
2244 memcpy(asreq, sc->sc_bss.essid, 2 + sc->sc_bss.essid[1]);
2245 asreq += 2 + asreq[1];
2246 /* supported rates */
2247 memcpy(asreq, &sc->sc_mib_phy.aSuprt_Data_Rates, 4);
2248 asreq += 2 + asreq[1];
2249
2250 m->m_pkthdr.len = m->m_len = asreq - mtod(m, u_int8_t *);
2251 IF_ENQUEUE(&sc->sc_mgtq, m);
2252 awi_start(ifp);
2253
2254 sc->sc_mgt_timer = AWI_TRANS_TIMEOUT / 1000;
2255 ifp->if_timer = 1;
2256 }
2257
2258 static void
2259 awi_recv_asresp(sc, m0)
2260 struct awi_softc *sc;
2261 struct mbuf *m0;
2262 {
2263 struct ieee80211_frame *wh;
2264 u_int8_t *asresp, *eframe;
2265 u_int16_t status;
2266 u_int8_t rate, *phy_rates;
2267 struct awi_bss *bp;
2268 int i, j;
2269
2270 wh = mtod(m0, struct ieee80211_frame *);
2271 asresp = (u_int8_t *)&wh[1];
2272 eframe = mtod(m0, u_int8_t *) + m0->m_len;
2273 if (sc->sc_ifp->if_flags & IFF_DEBUG)
2274 printf("%s: receive assoc resp from %s\n", sc->sc_dev.dv_xname,
2275 ether_sprintf(wh->i_addr2));
2276
2277 if (!sc->sc_mib_local.Network_Mode)
2278 return;
2279
2280 if (sc->sc_status != AWI_ST_ASSOC)
2281 return;
2282 /* capability info */
2283 asresp += 2;
2284 /* status */
2285 status = LE_READ_2(asresp);
2286 if (status != 0) {
2287 printf("%s: association failed (reason %d)\n",
2288 sc->sc_dev.dv_xname, status);
2289 for (bp = TAILQ_FIRST(&sc->sc_scan); bp != NULL;
2290 bp = TAILQ_NEXT(bp, list)) {
2291 if (memcmp(bp->esrc, sc->sc_bss.esrc, ETHER_ADDR_LEN)
2292 == 0) {
2293 bp->fails++;
2294 break;
2295 }
2296 }
2297 return;
2298 }
2299 asresp += 2;
2300 /* association id */
2301 asresp += 2;
2302 /* supported rates */
2303 rate = AWI_RATE_1MBIT;
2304 for (i = 0; i < asresp[1]; i++) {
2305 if (AWI_80211_RATE(asresp[2 + i]) <= rate)
2306 continue;
2307 phy_rates = sc->sc_mib_phy.aSuprt_Data_Rates;
2308 for (j = 0; j < phy_rates[1]; j++) {
2309 if (AWI_80211_RATE(asresp[2 + i]) ==
2310 AWI_80211_RATE(phy_rates[2 + j]))
2311 rate = AWI_80211_RATE(asresp[2 + i]);
2312 }
2313 }
2314 printf("%s: associated with %s ssid \"%s\"",
2315 sc->sc_dev.dv_xname, ether_sprintf(sc->sc_bss.bssid),
2316 sc->sc_bss.essid + 2);
2317 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
2318 printf(" chanset %d pattern %d",
2319 sc->sc_bss.chanset, sc->sc_bss.pattern);
2320 else
2321 printf(" channel %d", sc->sc_bss.chanset);
2322 printf(" signal %d\n", sc->sc_bss.rssi);
2323 sc->sc_tx_rate = rate;
2324 sc->sc_mgt_timer = 0;
2325 sc->sc_rx_timer = 10;
2326 sc->sc_ifp->if_timer = 1;
2327 sc->sc_status = AWI_ST_RUNNING;
2328 sc->sc_ifp->if_flags |= IFF_RUNNING;
2329 awi_drvstate(sc, AWI_DRV_INFASSOC);
2330 while ((bp = TAILQ_FIRST(&sc->sc_scan)) != NULL)
2331 TAILQ_REMOVE(&sc->sc_scan, bp, list);
2332 awi_start(sc->sc_ifp);
2333 }
2334
2335 static int
2336 awi_mib(sc, cmd, mib)
2337 struct awi_softc *sc;
2338 u_int8_t cmd;
2339 u_int8_t mib;
2340 {
2341 int error;
2342 u_int8_t size, *ptr;
2343
2344 switch (mib) {
2345 case AWI_MIB_LOCAL:
2346 ptr = (u_int8_t *)&sc->sc_mib_local;
2347 size = sizeof(sc->sc_mib_local);
2348 break;
2349 case AWI_MIB_ADDR:
2350 ptr = (u_int8_t *)&sc->sc_mib_addr;
2351 size = sizeof(sc->sc_mib_addr);
2352 break;
2353 case AWI_MIB_MAC:
2354 ptr = (u_int8_t *)&sc->sc_mib_mac;
2355 size = sizeof(sc->sc_mib_mac);
2356 break;
2357 case AWI_MIB_STAT:
2358 ptr = (u_int8_t *)&sc->sc_mib_stat;
2359 size = sizeof(sc->sc_mib_stat);
2360 break;
2361 case AWI_MIB_MGT:
2362 ptr = (u_int8_t *)&sc->sc_mib_mgt;
2363 size = sizeof(sc->sc_mib_mgt);
2364 break;
2365 case AWI_MIB_PHY:
2366 ptr = (u_int8_t *)&sc->sc_mib_phy;
2367 size = sizeof(sc->sc_mib_phy);
2368 break;
2369 default:
2370 return EINVAL;
2371 }
2372 if (sc->sc_cmd_inprog) {
2373 error = awi_cmd_wait(sc);
2374 if (error) {
2375 printf("awi_mib: cmd %d inprog\n",
2376 awi_read_1(sc, AWI_CMD));
2377 return error;
2378 }
2379 }
2380 sc->sc_cmd_inprog = 1;
2381 if (cmd == AWI_CMD_SET_MIB)
2382 awi_write_bytes(sc, AWI_CMD_PARAMS+AWI_CA_MIB_DATA, ptr, size);
2383 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_MIB_TYPE, mib);
2384 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_MIB_SIZE, size);
2385 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_MIB_INDEX, 0);
2386 error = awi_cmd(sc, cmd);
2387 if (error)
2388 return error;
2389 if (cmd == AWI_CMD_GET_MIB) {
2390 awi_read_bytes(sc, AWI_CMD_PARAMS+AWI_CA_MIB_DATA, ptr, size);
2391 #ifdef AWI_DEBUG
2392 if (awi_verbose) {
2393 int i;
2394
2395 printf("awi_mib: #%d:", mib);
2396 for (i = 0; i < size; i++)
2397 printf(" %02x", ptr[i]);
2398 printf("\n");
2399 }
2400 #endif
2401 }
2402 return 0;
2403 }
2404
2405 static int
2406 awi_cmd_scan(sc)
2407 struct awi_softc *sc;
2408 {
2409 int error;
2410 u_int8_t scan_mode;
2411
2412 if (sc->sc_active_scan)
2413 scan_mode = AWI_SCAN_ACTIVE;
2414 else
2415 scan_mode = AWI_SCAN_PASSIVE;
2416 if (sc->sc_mib_mgt.aScan_Mode != scan_mode) {
2417 sc->sc_mib_mgt.aScan_Mode = scan_mode;
2418 error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT);
2419 return error;
2420 }
2421
2422 if (sc->sc_cmd_inprog) {
2423 error = awi_cmd_wait(sc);
2424 if (error)
2425 return error;
2426 }
2427 sc->sc_cmd_inprog = 1;
2428 awi_write_2(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_DURATION,
2429 sc->sc_active_scan ? AWI_ASCAN_DURATION : AWI_PSCAN_DURATION);
2430 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
2431 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_SET,
2432 sc->sc_scan_set);
2433 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_PATTERN,
2434 sc->sc_scan_cur);
2435 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_IDX, 1);
2436 } else {
2437 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_SET,
2438 sc->sc_scan_cur);
2439 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_PATTERN, 0);
2440 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_IDX, 0);
2441 }
2442 awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_SUSP, 0);
2443 return awi_cmd(sc, AWI_CMD_SCAN);
2444 }
2445
2446 static int
2447 awi_cmd(sc, cmd)
2448 struct awi_softc *sc;
2449 u_int8_t cmd;
2450 {
2451 u_int8_t status;
2452 int error = 0;
2453
2454 sc->sc_cmd_inprog = 1;
2455 awi_write_1(sc, AWI_CMD_STATUS, AWI_STAT_IDLE);
2456 awi_write_1(sc, AWI_CMD, cmd);
2457 if (sc->sc_status != AWI_ST_INIT)
2458 return 0;
2459 error = awi_cmd_wait(sc);
2460 if (error)
2461 return error;
2462 status = awi_read_1(sc, AWI_CMD_STATUS);
2463 awi_write_1(sc, AWI_CMD, 0);
2464 switch (status) {
2465 case AWI_STAT_OK:
2466 break;
2467 case AWI_STAT_BADPARM:
2468 return EINVAL;
2469 default:
2470 printf("%s: command %d failed %x\n",
2471 sc->sc_dev.dv_xname, cmd, status);
2472 return ENXIO;
2473 }
2474 return 0;
2475 }
2476
2477 static void
2478 awi_cmd_done(sc)
2479 struct awi_softc *sc;
2480 {
2481 u_int8_t cmd, status;
2482
2483 status = awi_read_1(sc, AWI_CMD_STATUS);
2484 if (status == AWI_STAT_IDLE)
2485 return; /* stray interrupt */
2486
2487 sc->sc_cmd_inprog = 0;
2488 if (sc->sc_status == AWI_ST_INIT) {
2489 wakeup(sc);
2490 return;
2491 }
2492 cmd = awi_read_1(sc, AWI_CMD);
2493 awi_write_1(sc, AWI_CMD, 0);
2494
2495 if (status != AWI_STAT_OK) {
2496 printf("%s: command %d failed %x\n",
2497 sc->sc_dev.dv_xname, cmd, status);
2498 return;
2499 }
2500 switch (sc->sc_status) {
2501 case AWI_ST_SCAN:
2502 if (cmd == AWI_CMD_SET_MIB)
2503 awi_cmd_scan(sc); /* retry */
2504 break;
2505 case AWI_ST_SETSS:
2506 awi_try_sync(sc);
2507 break;
2508 case AWI_ST_SYNC:
2509 awi_sync_done(sc);
2510 break;
2511 default:
2512 break;
2513 }
2514 }
2515
2516 static int
2517 awi_next_txd(sc, len, framep, ntxdp)
2518 struct awi_softc *sc;
2519 int len;
2520 u_int32_t *framep, *ntxdp;
2521 {
2522 u_int32_t txd, ntxd, frame;
2523
2524 txd = sc->sc_txnext;
2525 frame = txd + AWI_TXD_SIZE;
2526 if (frame + len > sc->sc_txend)
2527 frame = sc->sc_txbase;
2528 ntxd = frame + len;
2529 if (ntxd + AWI_TXD_SIZE > sc->sc_txend)
2530 ntxd = sc->sc_txbase;
2531 *framep = frame;
2532 *ntxdp = ntxd;
2533 /*
2534 * Determine if there are any room in ring buffer.
2535 * --- send wait, === new data, +++ conflict (ENOBUFS)
2536 * base........................end
2537 * done----txd=====ntxd OK
2538 * --txd=====done++++ntxd-- full
2539 * --txd=====ntxd done-- OK
2540 * ==ntxd done----txd=== OK
2541 * ==done++++ntxd----txd=== full
2542 * ++ntxd txd=====done++ full
2543 */
2544 if (txd < ntxd) {
2545 if (txd < sc->sc_txdone && ntxd + AWI_TXD_SIZE > sc->sc_txdone)
2546 return ENOBUFS;
2547 } else {
2548 if (txd < sc->sc_txdone || ntxd + AWI_TXD_SIZE > sc->sc_txdone)
2549 return ENOBUFS;
2550 }
2551 return 0;
2552 }
2553
2554 static int
2555 awi_lock(sc)
2556 struct awi_softc *sc;
2557 {
2558 int error = 0;
2559
2560 if (sc->sc_invalid)
2561 return ENXIO;
2562 if (curproc == NULL) {
2563 /*
2564 * XXX
2565 * Though driver ioctl should be called with context,
2566 * KAME ipv6 stack calls ioctl in interrupt for now.
2567 * We simply abort the request if there are other
2568 * ioctl requests in progress.
2569 */
2570 if (sc->sc_busy)
2571 return EWOULDBLOCK;
2572 sc->sc_busy = 1;
2573 sc->sc_cansleep = 0;
2574 return 0;
2575 }
2576 while (sc->sc_busy) {
2577 sc->sc_sleep_cnt++;
2578 error = tsleep(sc, PWAIT | PCATCH, "awilck", 0);
2579 sc->sc_sleep_cnt--;
2580 if (error)
2581 return error;
2582 if (sc->sc_invalid)
2583 return ENXIO;
2584 }
2585 sc->sc_busy = 1;
2586 sc->sc_cansleep = 1;
2587 return 0;
2588 }
2589
2590 static void
2591 awi_unlock(sc)
2592 struct awi_softc *sc;
2593 {
2594 sc->sc_busy = 0;
2595 sc->sc_cansleep = 0;
2596 if (sc->sc_sleep_cnt)
2597 wakeup(sc);
2598 }
2599
2600 static int
2601 awi_intr_lock(sc)
2602 struct awi_softc *sc;
2603 {
2604 u_int8_t status;
2605 int i, retry;
2606
2607 status = 1;
2608 for (retry = 0; retry < 10; retry++) {
2609 for (i = 0; i < AWI_LOCKOUT_TIMEOUT*1000/5; i++) {
2610 status = awi_read_1(sc, AWI_LOCKOUT_HOST);
2611 if (status == 0)
2612 break;
2613 DELAY(5);
2614 }
2615 if (status != 0)
2616 break;
2617 awi_write_1(sc, AWI_LOCKOUT_MAC, 1);
2618 status = awi_read_1(sc, AWI_LOCKOUT_HOST);
2619 if (status == 0)
2620 break;
2621 awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
2622 }
2623 if (status != 0) {
2624 printf("%s: failed to lock interrupt\n",
2625 sc->sc_dev.dv_xname);
2626 return ENXIO;
2627 }
2628 return 0;
2629 }
2630
2631 static void
2632 awi_intr_unlock(sc)
2633 struct awi_softc *sc;
2634 {
2635
2636 awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
2637 }
2638
2639 static int
2640 awi_cmd_wait(sc)
2641 struct awi_softc *sc;
2642 {
2643 int i, error = 0;
2644
2645 i = 0;
2646 while (sc->sc_cmd_inprog) {
2647 if (sc->sc_invalid)
2648 return ENXIO;
2649 if (sc->sc_cansleep) {
2650 sc->sc_sleep_cnt++;
2651 error = tsleep(sc, PWAIT, "awicmd",
2652 AWI_CMD_TIMEOUT*hz/1000);
2653 sc->sc_sleep_cnt--;
2654 } else {
2655 if (awi_read_1(sc, AWI_CMD_STATUS) != AWI_STAT_IDLE) {
2656 awi_cmd_done(sc);
2657 break;
2658 }
2659 if (i++ >= AWI_CMD_TIMEOUT*1000/10)
2660 error = EWOULDBLOCK;
2661 else
2662 DELAY(10);
2663 }
2664 if (error)
2665 break;
2666 }
2667 return error;
2668 }
2669
2670 #ifdef AWI_DEBUG
2671 static void
2672 awi_dump_pkt(sc, m, rssi)
2673 struct awi_softc *sc;
2674 struct mbuf *m;
2675 u_int8_t rssi;
2676 {
2677 struct ieee80211_frame *wh;
2678 int i, l;
2679
2680 wh = mtod(m, struct ieee80211_frame *);
2681
2682 if (awi_dump_mask != 0 &&
2683 ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK)==IEEE80211_FC1_DIR_NODS) &&
2684 ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK)==IEEE80211_FC0_TYPE_MGT)) {
2685 if ((AWI_DUMP_MASK(wh->i_fc[0]) & awi_dump_mask) != 0)
2686 return;
2687 }
2688 if (awi_dump_mask < 0 &&
2689 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK)==IEEE80211_FC0_TYPE_DATA)
2690 return;
2691
2692 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
2693 case IEEE80211_FC1_DIR_NODS:
2694 printf("rx: NODS %s", ether_sprintf(wh->i_addr2));
2695 printf("->%s", ether_sprintf(wh->i_addr1));
2696 printf("(%s)", ether_sprintf(wh->i_addr3));
2697 break;
2698 case IEEE80211_FC1_DIR_TODS:
2699 printf("rx: TODS %s", ether_sprintf(wh->i_addr2));
2700 printf("->%s", ether_sprintf(wh->i_addr3));
2701 printf("(%s)", ether_sprintf(wh->i_addr1));
2702 break;
2703 case IEEE80211_FC1_DIR_FROMDS:
2704 printf("rx: FRDS %s", ether_sprintf(wh->i_addr3));
2705 printf("->%s", ether_sprintf(wh->i_addr1));
2706 printf("(%s)", ether_sprintf(wh->i_addr2));
2707 break;
2708 case IEEE80211_FC1_DIR_DSTODS:
2709 printf("rx: DSDS %s", ether_sprintf((u_int8_t *)&wh[1]));
2710 printf("->%s", ether_sprintf(wh->i_addr3));
2711 printf("(%s", ether_sprintf(wh->i_addr2));
2712 printf("->%s)", ether_sprintf(wh->i_addr1));
2713 break;
2714 }
2715 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
2716 case IEEE80211_FC0_TYPE_DATA:
2717 printf(" data");
2718 break;
2719 case IEEE80211_FC0_TYPE_MGT:
2720 switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
2721 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2722 printf(" probe_req");
2723 break;
2724 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2725 printf(" probe_resp");
2726 break;
2727 case IEEE80211_FC0_SUBTYPE_BEACON:
2728 printf(" beacon");
2729 break;
2730 case IEEE80211_FC0_SUBTYPE_AUTH:
2731 printf(" auth");
2732 break;
2733 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2734 printf(" assoc_req");
2735 break;
2736 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2737 printf(" assoc_resp");
2738 break;
2739 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2740 printf(" reassoc_req");
2741 break;
2742 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2743 printf(" reassoc_resp");
2744 break;
2745 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2746 printf(" deauth");
2747 break;
2748 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2749 printf(" disassoc");
2750 break;
2751 default:
2752 printf(" mgt#%d",
2753 wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
2754 break;
2755 }
2756 break;
2757 default:
2758 printf(" type#%d",
2759 wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
2760 break;
2761 }
2762 printf(" +%d\n", rssi);
2763 if (awi_dump_len > 0) {
2764 l = m->m_len;
2765 if (l > awi_dump_len + sizeof(*wh))
2766 l = awi_dump_len + sizeof(*wh);
2767 i = sizeof(*wh);
2768 if (awi_dump_hdr)
2769 i = 0;
2770 for (; i < l; i++) {
2771 if ((i & 1) == 0)
2772 printf(" ");
2773 printf("%02x", mtod(m, u_int8_t *)[i]);
2774 }
2775 printf("\n");
2776 }
2777 }
2778 #endif
2779