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