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