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