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