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