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