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