]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/awi/awi.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / awi / awi.c
1 /*      $NetBSD: awi.c,v 1.62 2004/01/16 14:13:15 onoe Exp $    */
2
3 /*-
4  * Copyright (c) 1999,2000,2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Bill Sommerfeld
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 /*
39  * Driver for AMD 802.11 firmware.
40  * Uses am79c930 chip driver to talk to firmware running on the am79c930.
41  *
42  * More-or-less a generic ethernet-like if driver, with 802.11 gorp added.
43  */
44
45 /*
46  * todo:
47  *      - flush tx queue on resynch.
48  *      - clear oactive on "down".
49  *      - rewrite copy-into-mbuf code
50  *      - mgmt state machine gets stuck retransmitting assoc requests.
51  *      - multicast filter.
52  *      - fix device reset so it's more likely to work
53  *      - show status goo through ifmedia.
54  *
55  * more todo:
56  *      - deal with more 802.11 frames.
57  *              - send reassoc request
58  *              - deal with reassoc response
59  *              - send/deal with disassociation
60  *      - deal with "full" access points (no room for me).
61  *      - power save mode
62  *
63  * later:
64  *      - SSID preferences
65  *      - need ioctls for poking at the MIBs
66  *      - implement ad-hoc mode (including bss creation).
67  *      - decide when to do "ad hoc" vs. infrastructure mode (IFF_LINK flags?)
68  *              (focus on inf. mode since that will be needed for ietf)
69  *      - deal with DH vs. FH versions of the card
70  *      - deal with faster cards (2mb/s)
71  *      - ?WEP goo (mmm, rc4) (it looks not particularly useful).
72  *      - ifmedia revision.
73  *      - common 802.11 mibish things.
74  *      - common 802.11 media layer.
75  */
76
77 /*
78  * Driver for AMD 802.11 PCnetMobile firmware.
79  * Uses am79c930 chip driver to talk to firmware running on the am79c930.
80  *
81  * The initial version of the driver was written by
82  * Bill Sommerfeld <sommerfeld@NetBSD.org>.
83  * Then the driver module completely rewritten to support cards with DS phy
84  * and to support adhoc mode by Atsushi Onoe <onoe@NetBSD.org>
85  */
86
87 #include <sys/cdefs.h>
88 #ifdef __NetBSD__
89 __KERNEL_RCSID(0, "$NetBSD: awi.c,v 1.62 2004/01/16 14:13:15 onoe Exp $");
90 #endif
91 #ifdef __FreeBSD__
92 __FBSDID("$FreeBSD$");
93 #endif
94
95 #include "opt_inet.h"
96 #ifdef __NetBSD__
97 #include "bpfilter.h"
98 #endif
99 #ifdef __FreeBSD__
100 #define NBPFILTER       1
101 #endif
102
103 #include <sys/param.h>
104 #include <sys/systm.h>
105 #include <sys/kernel.h>
106 #include <sys/mbuf.h>
107 #include <sys/malloc.h>
108 #include <sys/proc.h>
109 #include <sys/socket.h>
110 #include <sys/sockio.h>
111 #include <sys/errno.h>
112 #include <sys/endian.h>
113 #ifdef __FreeBSD__
114 #include <sys/bus.h>
115 #endif
116 #ifdef __NetBSD__
117 #include <sys/device.h>
118 #endif
119
120 #include <net/if.h>
121 #include <net/if_dl.h>
122 #ifdef __NetBSD__
123 #include <net/if_ether.h>
124 #endif
125 #ifdef __FreeBSD__
126 #include <net/ethernet.h>
127 #include <net/if_arp.h>
128 #endif
129 #include <net/if_media.h>
130 #include <net/if_llc.h>
131
132 #include <net80211/ieee80211_var.h>
133 #ifdef __NetBSD__
134 #include <net80211/ieee80211_compat.h>
135 #endif
136
137 #if NBPFILTER > 0
138 #include <net/bpf.h>
139 #endif
140
141 #include <machine/cpu.h>
142 #include <machine/bus.h>
143
144 #ifdef __NetBSD__
145 #include <dev/ic/am79c930reg.h>
146 #include <dev/ic/am79c930var.h>
147 #include <dev/ic/awireg.h>
148 #include <dev/ic/awivar.h>
149 #endif
150 #ifdef __FreeBSD__
151 #include <dev/awi/am79c930reg.h>
152 #include <dev/awi/am79c930var.h>
153 #include <dev/awi/awireg.h>
154 #include <dev/awi/awivar.h>
155 #endif
156
157 #ifdef __FreeBSD__
158 static void awi_init0(void *);
159 #endif
160 static int  awi_init(struct ifnet *);
161 static void awi_stop(struct ifnet *, int);
162 static void awi_start(struct ifnet *);
163 static void awi_watchdog(struct ifnet *);
164 static int  awi_ioctl(struct ifnet *, u_long, caddr_t);
165 static int  awi_media_change(struct ifnet *);
166 static void awi_media_status(struct ifnet *, struct ifmediareq *);
167 static int  awi_mode_init(struct awi_softc *);
168 static void awi_rx_int(struct awi_softc *);
169 static void awi_tx_int(struct awi_softc *);
170 static struct mbuf *awi_devget(struct awi_softc *, u_int32_t, u_int16_t);
171 static int  awi_hw_init(struct awi_softc *);
172 static int  awi_init_mibs(struct awi_softc *);
173 static int  awi_mib(struct awi_softc *, u_int8_t, u_int8_t, int);
174 static int  awi_cmd(struct awi_softc *, u_int8_t, int);
175 static int  awi_cmd_wait(struct awi_softc *);
176 static void awi_cmd_done(struct awi_softc *);
177 static int  awi_next_txd(struct awi_softc *, int, u_int32_t *, u_int32_t *);
178 static int  awi_lock(struct awi_softc *);
179 static void awi_unlock(struct awi_softc *);
180 static int  awi_intr_lock(struct awi_softc *);
181 static void awi_intr_unlock(struct awi_softc *);
182 static int  awi_newstate(struct ieee80211com *, enum ieee80211_state, int);
183 static void awi_recv_mgmt(struct ieee80211com *, struct mbuf *,
184     struct ieee80211_node *, int, int, int, u_int32_t);
185 static int  awi_send_mgmt(struct ieee80211com *, struct ieee80211_node *, int,
186     int);
187 static struct mbuf *awi_ether_encap(struct awi_softc *, struct mbuf *);
188 static struct mbuf *awi_ether_modcap(struct awi_softc *, struct mbuf *);
189
190 /* unaligned little endian access */     
191 #define LE_READ_2(p)                                                    \
192         ((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8))
193 #define LE_READ_4(p)                                                    \
194         ((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8) |  \
195          (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24))
196 #define LE_WRITE_2(p, v)                                                \
197         ((((u_int8_t *)(p))[0] = (((u_int32_t)(v)      ) & 0xff)),      \
198          (((u_int8_t *)(p))[1] = (((u_int32_t)(v) >>  8) & 0xff)))
199 #define LE_WRITE_4(p, v)                                                \
200         ((((u_int8_t *)(p))[0] = (((u_int32_t)(v)      ) & 0xff)),      \
201          (((u_int8_t *)(p))[1] = (((u_int32_t)(v) >>  8) & 0xff)),      \
202          (((u_int8_t *)(p))[2] = (((u_int32_t)(v) >> 16) & 0xff)),      \
203          (((u_int8_t *)(p))[3] = (((u_int32_t)(v) >> 24) & 0xff)))
204
205 struct awi_chanset awi_chanset[] = {
206     /* PHY type        domain            min max def */
207     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_JP,  6, 17,  6 },
208     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_ES,  0, 26,  1 },
209     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_FR,  0, 32,  1 },
210     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_US,  0, 77,  1 },
211     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_CA,  0, 77,  1 },
212     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_EU,  0, 77,  1 },
213     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_JP, 14, 14, 14 },
214     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_ES, 10, 11, 10 },
215     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_FR, 10, 13, 10 },
216     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_US,  1, 11,  3 },
217     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_CA,  1, 11,  3 },
218     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_EU,  1, 13,  3 },
219     { 0, 0 }
220 };
221
222 #ifdef __FreeBSD__
223 devclass_t awi_devclass;
224
225 #if __FreeBSD_version < 500043
226 static char *ether_sprintf(u_int8_t *);
227
228 static char *
229 ether_sprintf(u_int8_t *enaddr)
230 {
231         static char strbuf[18];
232
233         sprintf(strbuf, "%6D", enaddr, ":");
234         return strbuf;
235 }
236 #endif
237
238 #if 0 /* ALTQ */
239 #define IFQ_PURGE(ifq)          IF_DRAIN(ifq)
240 #define IF_POLL(ifq, m)         ((m) = (ifq)->ifq_head)
241 #define IFQ_POLL(ifq, m)        IF_POLL((ifq), (m))
242 #define IFQ_DEQUEUE(ifq, m)     IF_DEQUEUE((ifq), (m))
243 #endif
244
245 #endif
246
247 #ifdef AWI_DEBUG
248 int awi_debug = 0;
249
250 #define DPRINTF(X)      if (awi_debug) printf X
251 #define DPRINTF2(X)     if (awi_debug > 1) printf X
252 #else
253 #define DPRINTF(X)
254 #define DPRINTF2(X)
255 #endif
256
257 int
258 awi_attach(struct awi_softc *sc)
259 {
260         struct ieee80211com *ic = &sc->sc_ic;
261         struct ifnet *ifp = AC2IFP(&sc->sc_arp);
262         int s, i, error, nrate;
263         int mword;
264         enum ieee80211_phymode mode;
265
266         s = splnet();
267         sc->sc_busy = 1;
268         sc->sc_attached = 0;
269         sc->sc_substate = AWI_ST_NONE;
270         if ((error = awi_hw_init(sc)) != 0) {
271                 sc->sc_invalid = 1;
272                 splx(s);
273                 return error;
274         }
275         error = awi_init_mibs(sc);
276         if (error != 0) {
277                 sc->sc_invalid = 1;
278                 splx(s);
279                 return error;
280         }
281         ifp->if_softc = sc;
282         ifp->if_flags =
283 #ifdef IFF_NOTRAILERS
284             IFF_NOTRAILERS |
285 #endif
286             IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST | IFF_NEEDSGIANT;
287         ifp->if_ioctl = awi_ioctl;
288         ifp->if_start = awi_start;
289         ifp->if_watchdog = awi_watchdog;
290 #ifdef __NetBSD__
291         ifp->if_init = awi_init;
292         ifp->if_stop = awi_stop;
293         IFQ_SET_READY(&ifp->if_snd);
294         memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
295 #endif
296 #ifdef __FreeBSD__
297         ifp->if_init = awi_init0;
298         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
299         if_initname(ifp, device_get_name(sc->sc_dev),
300             device_get_unit(sc->sc_dev));
301 #endif
302
303         ic->ic_ifp = ifp;
304         ic->ic_caps = IEEE80211_C_WEP | IEEE80211_C_IBSS | IEEE80211_C_HOSTAP;
305         if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
306                 ic->ic_phytype = IEEE80211_T_FH;
307                 mode = IEEE80211_MODE_FH;
308         } else {
309                 ic->ic_phytype = IEEE80211_T_DS;
310                 ic->ic_caps |= IEEE80211_C_AHDEMO;
311                 mode = IEEE80211_MODE_11B;
312         }
313         ic->ic_opmode = IEEE80211_M_STA;
314         nrate = sc->sc_mib_phy.aSuprt_Data_Rates[1];
315         memcpy(ic->ic_sup_rates[mode].rs_rates,
316             sc->sc_mib_phy.aSuprt_Data_Rates + 2, nrate);
317         ic->ic_sup_rates[mode].rs_nrates = nrate;
318         IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->sc_mib_addr.aMAC_Address);
319
320         printf("%s: IEEE802.11 %s (firmware %s)\n", ifp->if_xname,
321             (ic->ic_phytype == IEEE80211_T_FH) ? "FH" : "DS", sc->sc_banner);
322
323 #ifdef __NetBSD__
324         if_attach(ifp);
325 #endif
326         ieee80211_ifattach(ic);
327
328         sc->sc_newstate = ic->ic_newstate;
329         ic->ic_newstate = awi_newstate;
330
331         sc->sc_recv_mgmt = ic->ic_recv_mgmt;
332         ic->ic_recv_mgmt = awi_recv_mgmt;
333
334         sc->sc_send_mgmt = ic->ic_send_mgmt;
335         ic->ic_send_mgmt = awi_send_mgmt;
336
337         ieee80211_media_init(ic, awi_media_change, awi_media_status);
338
339         /* Melco compatibility mode. */
340 #define ADD(s, o)       ifmedia_add(&ic->ic_media, \
341         IFM_MAKEWORD(IFM_IEEE80211, (s), (o), 0), 0, NULL)
342         ADD(IFM_AUTO, IFM_FLAG0);
343
344         for (i = 0; i < nrate; i++) {
345                 mword = ieee80211_rate2media(ic,
346                     ic->ic_sup_rates[mode].rs_rates[i], mode);
347                 if (mword == 0)
348                         continue;
349                 ADD(mword, IFM_FLAG0);
350         }
351 #undef  ADD
352
353 #ifdef __NetBSD__
354         if ((sc->sc_sdhook = shutdownhook_establish(awi_shutdown, sc)) == NULL)
355                 printf("%s: WARNING: unable to establish shutdown hook\n",
356                     ifp->if_xname);
357         if ((sc->sc_powerhook = powerhook_establish(awi_power, sc)) == NULL)
358                 printf("%s: WARNING: unable to establish power hook\n",
359                     ifp->if_xname);
360 #endif
361         sc->sc_attached = 1;
362         splx(s);
363
364         /* ready to accept ioctl */
365         awi_unlock(sc);
366
367         ieee80211_announce(ic);
368
369         return 0;
370 }
371
372 int
373 awi_detach(struct awi_softc *sc)
374 {
375         struct ifnet *ifp = AC2IFP(&sc->sc_arp);
376         int s;
377
378         if (!sc->sc_attached)
379                 return 0;
380
381         s = splnet();
382         sc->sc_invalid = 1;
383         awi_stop(ifp, 1);
384
385         while (sc->sc_sleep_cnt > 0) {
386                 wakeup(sc);
387                 (void)tsleep(sc, PWAIT, "awidet", 1);
388         }
389         sc->sc_attached = 0;
390         ieee80211_ifdetach(&sc->sc_ic);
391 #ifdef __NetBSD__
392         if_detach(ifp);
393         shutdownhook_disestablish(sc->sc_sdhook);
394         powerhook_disestablish(sc->sc_powerhook);
395 #endif
396         splx(s);
397         return 0;
398 }
399
400 #ifdef __NetBSD__
401 int
402 awi_activate(struct device *self, enum devact act)
403 {
404         struct awi_softc *sc = (struct awi_softc *)self;
405         struct ifnet *ifp = AC2IFP(&sc->sc_arp);
406         int s, error = 0;
407
408         s = splnet();
409         switch (act) {
410         case DVACT_ACTIVATE:
411                 error = EOPNOTSUPP;
412                 break;
413         case DVACT_DEACTIVATE:
414                 sc->sc_invalid = 1;
415                 if_deactivate(ifp);
416                 break;
417         }
418         splx(s);
419         return error;
420 }
421
422 void
423 awi_power(int why, void *arg)
424 {
425         struct awi_softc *sc = arg;
426         struct ifnet *ifp = AC2IFP(&sc->sc_arp);
427         int s;
428         int ocansleep;
429
430         DPRINTF(("awi_power: %d\n", why));
431         s = splnet();
432         ocansleep = sc->sc_cansleep;
433         sc->sc_cansleep = 0;
434         switch (why) {
435         case PWR_SUSPEND:
436         case PWR_STANDBY:
437                 awi_stop(ifp, 1);
438                 break;
439         case PWR_RESUME:
440                 if (ifp->if_flags & IFF_UP) {
441                         awi_init(ifp);
442                         (void)awi_intr(sc);     /* make sure */
443                 }
444                 break;
445         case PWR_SOFTSUSPEND:
446         case PWR_SOFTSTANDBY:
447         case PWR_SOFTRESUME:
448                 break;
449         }
450         sc->sc_cansleep = ocansleep;
451         splx(s);
452 }
453 #endif /* __NetBSD__ */
454
455 void
456 awi_shutdown(void *arg)
457 {
458         struct awi_softc *sc = arg;
459         struct ifnet *ifp = AC2IFP(&sc->sc_arp);
460
461         if (sc->sc_attached)
462                 awi_stop(ifp, 1);
463 }
464
465 int
466 awi_intr(void *arg)
467 {
468         struct awi_softc *sc = arg;
469         u_int16_t status;
470         int handled = 0, ocansleep;
471 #ifdef AWI_DEBUG
472         static const char *intname[] = {
473             "CMD", "RX", "TX", "SCAN_CMPLT",
474             "CFP_START", "DTIM", "CFP_ENDING", "GROGGY",
475             "TXDATA", "TXBCAST", "TXPS", "TXCF",
476             "TXMGT", "#13", "RXDATA", "RXMGT"
477         };
478 #endif
479
480         if (!sc->sc_enabled || !sc->sc_enab_intr || sc->sc_invalid) {
481                 DPRINTF(("awi_intr: stray interrupt: "
482                     "enabled %d enab_intr %d invalid %d\n",
483                     sc->sc_enabled, sc->sc_enab_intr, sc->sc_invalid));
484                 return 0;
485         }
486
487         am79c930_gcr_setbits(&sc->sc_chip,
488             AM79C930_GCR_DISPWDN | AM79C930_GCR_ECINT);
489         awi_write_1(sc, AWI_DIS_PWRDN, 1);
490         ocansleep = sc->sc_cansleep;
491         sc->sc_cansleep = 0;
492
493         for (;;) {
494                 if (awi_intr_lock(sc) != 0)
495                         break;
496                 status = awi_read_1(sc, AWI_INTSTAT);
497                 awi_write_1(sc, AWI_INTSTAT, 0);
498                 awi_write_1(sc, AWI_INTSTAT, 0);
499                 status |= awi_read_1(sc, AWI_INTSTAT2) << 8;
500                 awi_write_1(sc, AWI_INTSTAT2, 0);
501                 DELAY(10);
502                 awi_intr_unlock(sc);
503                 if (!sc->sc_cmd_inprog)
504                         status &= ~AWI_INT_CMD; /* make sure */
505                 if (status == 0)
506                         break;
507 #ifdef AWI_DEBUG
508                 if (awi_debug > 1) {
509                         int i;
510
511                         printf("awi_intr: status 0x%04x", status);
512                         for (i = 0; i < sizeof(intname)/sizeof(intname[0]);
513                             i++) {
514                                 if (status & (1 << i))
515                                         printf(" %s", intname[i]);
516                         }
517                         printf("\n");
518                 }
519 #endif
520                 handled = 1;
521                 if (status & AWI_INT_RX)
522                         awi_rx_int(sc);
523                 if (status & AWI_INT_TX)
524                         awi_tx_int(sc);
525                 if (status & AWI_INT_CMD)
526                         awi_cmd_done(sc);
527                 if (status & AWI_INT_SCAN_CMPLT) {
528                   /* XXX revisit scanning */
529                         if (sc->sc_ic.ic_state == IEEE80211_S_SCAN &&
530                             sc->sc_substate == AWI_ST_NONE)
531                           ;
532                 }
533         }
534         sc->sc_cansleep = ocansleep;
535         am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_DISPWDN);
536         awi_write_1(sc, AWI_DIS_PWRDN, 0);
537         return handled;
538 }
539
540 #ifdef __FreeBSD__
541 static void
542 awi_init0(void *arg)
543 {
544         struct awi_softc *sc = arg;
545
546         (void)awi_init(AC2IFP(&sc->sc_arp));
547 }
548 #endif
549
550 static int
551 awi_init(struct ifnet *ifp)
552 {
553         struct awi_softc *sc = ifp->if_softc;
554         struct ieee80211com *ic = &sc->sc_ic;
555         struct ieee80211_node *ni = ic->ic_bss;
556         struct ieee80211_rateset *rs;
557         int error, rate, i;
558
559         DPRINTF(("awi_init: enabled=%d\n", sc->sc_enabled));
560         if (sc->sc_enabled) {
561                 awi_stop(ifp, 0);
562         } else {
563                 if (sc->sc_enable)
564                         (*sc->sc_enable)(sc);
565                 sc->sc_enabled = 1;
566                 if ((error = awi_hw_init(sc)) != 0) {
567                         if (sc->sc_disable)
568                                 (*sc->sc_disable)(sc);
569                         sc->sc_enabled = 0;
570                         return error;
571                 }
572         }
573         ic->ic_state = IEEE80211_S_INIT;
574
575         ic->ic_flags &= ~IEEE80211_F_IBSSON;
576         switch (ic->ic_opmode) {
577         case IEEE80211_M_STA:
578                 sc->sc_mib_local.Network_Mode = 1;
579                 sc->sc_mib_local.Acting_as_AP = 0;
580                 break;
581         case IEEE80211_M_IBSS:
582                 ic->ic_flags |= IEEE80211_F_IBSSON;
583                 /* FALLTHRU */
584         case IEEE80211_M_AHDEMO:
585                 sc->sc_mib_local.Network_Mode = 0;
586                 sc->sc_mib_local.Acting_as_AP = 0;
587                 break;
588         case IEEE80211_M_HOSTAP:
589                 sc->sc_mib_local.Network_Mode = 1;
590                 sc->sc_mib_local.Acting_as_AP = 1;
591                 break;
592         case IEEE80211_M_MONITOR:
593         case IEEE80211_M_WDS:
594                 return ENODEV;
595         }
596 #if 0
597         IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
598 #endif
599         memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
600         sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
601         sc->sc_mib_mac.aDesired_ESS_ID[1] = ic->ic_des_ssid[0].len;
602         memcpy(&sc->sc_mib_mac.aDesired_ESS_ID[2], ic->ic_des_ssid[0].ssid,
603             ic->ic_des_ssid[0].len);
604
605         /* configure basic rate */
606         if (ic->ic_phytype == IEEE80211_T_FH)
607                 rs = &ic->ic_sup_rates[IEEE80211_MODE_FH];
608         else
609                 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
610         if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
611                 rate = ic->ic_fixed_rate;
612         } else {
613                 rate = 0;
614                 for (i = 0; i < rs->rs_nrates; i++) {
615                         if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
616                             rate < (rs->rs_rates[i] & IEEE80211_RATE_VAL))
617                                 rate = rs->rs_rates[i] & IEEE80211_RATE_VAL;
618                 }
619         }
620         rate *= 5;
621         LE_WRITE_2(&sc->sc_mib_mac.aStation_Basic_Rate, rate);
622
623         if ((error = awi_mode_init(sc)) != 0) {
624                 DPRINTF(("awi_init: awi_mode_init failed %d\n", error));
625                 awi_stop(ifp, 1);
626                 return error;
627         }
628
629         /* start transmitter */
630         sc->sc_txdone = sc->sc_txnext = sc->sc_txbase;
631         awi_write_4(sc, sc->sc_txbase + AWI_TXD_START, 0);
632         awi_write_4(sc, sc->sc_txbase + AWI_TXD_NEXT, 0);
633         awi_write_4(sc, sc->sc_txbase + AWI_TXD_LENGTH, 0);
634         awi_write_1(sc, sc->sc_txbase + AWI_TXD_RATE, 0);
635         awi_write_4(sc, sc->sc_txbase + AWI_TXD_NDA, 0);
636         awi_write_4(sc, sc->sc_txbase + AWI_TXD_NRA, 0);
637         awi_write_1(sc, sc->sc_txbase + AWI_TXD_STATE, 0);
638         awi_write_4(sc, AWI_CA_TX_DATA, sc->sc_txbase);
639         awi_write_4(sc, AWI_CA_TX_MGT, 0);
640         awi_write_4(sc, AWI_CA_TX_BCAST, 0);
641         awi_write_4(sc, AWI_CA_TX_PS, 0);
642         awi_write_4(sc, AWI_CA_TX_CF, 0);
643         if ((error = awi_cmd(sc, AWI_CMD_INIT_TX, AWI_WAIT)) != 0) {
644                 DPRINTF(("awi_init: failed to start transmitter: %d\n", error));
645                 awi_stop(ifp, 1);
646                 return error;
647         }
648
649         /* start receiver */
650         if ((error = awi_cmd(sc, AWI_CMD_INIT_RX, AWI_WAIT)) != 0) {
651                 DPRINTF(("awi_init: failed to start receiver: %d\n", error));
652                 awi_stop(ifp, 1);
653                 return error;
654         }
655         sc->sc_rxdoff = awi_read_4(sc, AWI_CA_IRX_DATA_DESC);
656         sc->sc_rxmoff = awi_read_4(sc, AWI_CA_IRX_PS_DESC);
657
658         ifp->if_drv_flags |= IFF_DRV_RUNNING;
659         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
660         ic->ic_state = IEEE80211_S_INIT;
661
662         if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
663             ic->ic_opmode == IEEE80211_M_HOSTAP) {
664                 ni->ni_chan = ic->ic_des_chan;  /* XXX? */
665                 ni->ni_intval = ic->ic_bintval;
666                 ni->ni_rssi = 0;
667                 ni->ni_rstamp = 0;
668                 memset(&ni->ni_tstamp, 0, sizeof(ni->ni_tstamp));
669                 ni->ni_rates =
670                     ic->ic_sup_rates[ieee80211_chan2mode(ni->ni_chan)];
671                 IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr);
672                 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
673                         IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
674                         ni->ni_esslen = ic->ic_des_ssid[0].len;
675                         memcpy(ni->ni_essid, ic->ic_des_ssid[0].ssid, ni->ni_esslen);
676                         ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
677                         if (ic->ic_phytype == IEEE80211_T_FH) {
678                                 ni->ni_fhdwell = 200;   /* XXX */
679                                 ni->ni_fhindex = 1;
680                         }
681                 } else {
682                         ni->ni_capinfo = IEEE80211_CAPINFO_IBSS;
683                         memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
684                         ni->ni_esslen = 0;
685                 }
686                 if (ic->ic_flags & IEEE80211_F_PRIVACY)
687                         ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
688                 if (ic->ic_opmode != IEEE80211_M_AHDEMO)
689                         ic->ic_flags |= IEEE80211_F_SIBSS;
690                 ic->ic_state = IEEE80211_S_SCAN;        /*XXX*/
691                 sc->sc_substate = AWI_ST_NONE;
692                 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
693         } else {
694                 /* XXX check sc->sc_cur_chan */
695                 ni->ni_chan = &ic->ic_channels[sc->sc_cur_chan];
696                 ic->ic_curchan = ni->ni_chan;
697                 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
698         }
699         return 0;
700 }
701
702 static void
703 awi_stop(struct ifnet *ifp, int disable)
704 {
705         struct awi_softc *sc = ifp->if_softc;
706
707         if (!sc->sc_enabled)
708                 return;
709
710         DPRINTF(("awi_stop(%d)\n", disable));
711
712         ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1);
713
714         if (!sc->sc_invalid) {
715                 if (sc->sc_cmd_inprog)
716                         (void)awi_cmd_wait(sc);
717                 (void)awi_cmd(sc, AWI_CMD_KILL_RX, AWI_WAIT);
718                 sc->sc_cmd_inprog = AWI_CMD_FLUSH_TX;
719                 awi_write_1(sc, AWI_CA_FTX_DATA, 1);
720                 awi_write_1(sc, AWI_CA_FTX_MGT, 0);
721                 awi_write_1(sc, AWI_CA_FTX_BCAST, 0);
722                 awi_write_1(sc, AWI_CA_FTX_PS, 0);
723                 awi_write_1(sc, AWI_CA_FTX_CF, 0);
724                 (void)awi_cmd(sc, AWI_CMD_FLUSH_TX, AWI_WAIT);
725         }
726         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING|IFF_DRV_OACTIVE);
727         ifp->if_timer = 0;
728         sc->sc_tx_timer = sc->sc_rx_timer = 0;
729         if (sc->sc_rxpend != NULL) {
730                 m_freem(sc->sc_rxpend);
731                 sc->sc_rxpend = NULL;
732         }
733         IFQ_PURGE(&ifp->if_snd);
734
735         if (disable) {
736                 if (!sc->sc_invalid)
737                         am79c930_gcr_setbits(&sc->sc_chip,
738                             AM79C930_GCR_CORESET);
739                 if (sc->sc_disable)
740                         (*sc->sc_disable)(sc);
741                 sc->sc_enabled = 0;
742         }
743 }
744
745 static void
746 awi_start(struct ifnet *ifp)
747 {
748         struct awi_softc *sc = ifp->if_softc;
749         struct ieee80211com *ic = &sc->sc_ic;
750         struct ieee80211_node *ni;
751         struct ieee80211_frame *wh;
752         struct ether_header *eh;
753         struct mbuf *m, *m0;
754         int len, dowep;
755         u_int32_t txd, frame, ntxd;
756         u_int8_t rate;
757
758         if (!sc->sc_enabled || sc->sc_invalid)
759                 return;
760
761         for (;;) {
762                 txd = sc->sc_txnext;
763                 IF_POLL(&ic->ic_mgtq, m0);
764                 dowep = 0;
765                 ni = NULL;
766                 if (m0 != NULL) {
767                         len = m0->m_pkthdr.len;
768                         if (awi_next_txd(sc, len, &frame, &ntxd)) {
769                                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
770                                 break;
771                         }
772                         IF_DEQUEUE(&ic->ic_mgtq, m0);
773                         ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
774                         m0->m_pkthdr.rcvif = NULL;
775                 } else {
776                         if (ic->ic_state != IEEE80211_S_RUN)
777                                 break;
778                         IFQ_POLL(&ifp->if_snd, m0);
779                         if (m0 == NULL)
780                                 break;
781                         /*
782                          * Need to calculate the real length to determine
783                          * if the transmit buffer has a room for the packet.
784                          */
785                         len = m0->m_pkthdr.len + sizeof(struct ieee80211_frame);
786                         if (!(ifp->if_flags & IFF_LINK0) && !sc->sc_adhoc_ap)
787                                 len += sizeof(struct llc) -
788                                     sizeof(struct ether_header);
789                         if (ic->ic_flags & IEEE80211_F_PRIVACY) {
790                                 /* XXX other crypto */
791                                 dowep = 1;
792                                 len += IEEE80211_WEP_IVLEN +
793                                     IEEE80211_WEP_KIDLEN + IEEE80211_WEP_CRCLEN;
794                         }
795                         if (awi_next_txd(sc, len, &frame, &ntxd)) {
796                                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
797                                 break;
798                         }
799                         IFQ_DEQUEUE(&ifp->if_snd, m0);
800 #if NBPFILTER > 0
801                         BPF_MTAP(ifp, m0);
802 #endif
803                         if ((ifp->if_flags & IFF_LINK0) || sc->sc_adhoc_ap)
804                                 m0 = awi_ether_encap(sc, m0);
805                         else {
806                                 if (m0->m_len < sizeof(struct ether_header) &&
807                                     ((m0 = m_pullup(m0, sizeof(struct ether_header)))) == NULL) {
808                                         ifp->if_oerrors++;
809                                         continue;
810                                 }
811                                 eh = mtod(m0, struct ether_header *);
812                                 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
813                                 if (ni == NULL)
814                                         goto bad;
815                                 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
816                                     (m0->m_flags & M_PWR_SAV) == 0) {
817                                         ieee80211_pwrsave(ni, m0);
818                                         continue;
819                                 }
820                                 m0 = ieee80211_encap(ic, m0, ni);
821                         }
822                         if (m0 == NULL)
823                                 goto bad;
824                         wh = mtod(m0, struct ieee80211_frame *);
825                         if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
826                             (ic->ic_opmode == IEEE80211_M_HOSTAP ||
827                              ic->ic_opmode == IEEE80211_M_IBSS) &&
828                             sc->sc_adhoc_ap == 0 &&
829                             (ifp->if_flags & IFF_LINK0) == 0 &&
830                             (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
831                             IEEE80211_FC0_TYPE_DATA && ni == NULL) {
832                         bad:
833                                 if (m0 != NULL)
834                                         m_freem(m0);
835                                 ifp->if_oerrors++;
836                                 if (ni != NULL)
837                                         ieee80211_free_node(ni);
838                                 continue;
839                         }
840                         ifp->if_opackets++;
841                 }
842 #if NBPFILTER > 0
843                 if (bpf_peers_present(ic->ic_rawbpf))
844                         bpf_mtap(ic->ic_rawbpf, m0);
845 #endif
846                 if (dowep) {
847                         struct ieee80211_key *k;
848
849                         k = ieee80211_crypto_encap(ic, ni, m0);
850                         if (k == NULL) {
851                                 if (ni != NULL)
852                                         ieee80211_free_node(ni);
853                                 m_freem(m0);
854                                 continue;
855                         }
856                 }
857 #ifdef DIAGNOSTIC
858                 if (m0->m_pkthdr.len != len) {
859                         if_printf(ifp, "length %d should be %d\n",
860                             m0->m_pkthdr.len, len);
861                         m_freem(m0);
862                         ifp->if_oerrors++;
863                         if (ni != NULL)
864                                 ieee80211_free_node(ni);
865                         continue;
866                 }
867 #endif
868
869                 if ((ifp->if_flags & IFF_DEBUG) && (ifp->if_flags & IFF_LINK2))
870                         ieee80211_dump_pkt(ic, m0->m_data, m0->m_len,
871                             ic->ic_bss->ni_rates.
872                                 rs_rates[ic->ic_bss->ni_txrate] &
873                             IEEE80211_RATE_VAL, -1);
874
875                 for (m = m0, len = 0; m != NULL; m = m->m_next) {
876                         awi_write_bytes(sc, frame + len, mtod(m, u_int8_t *),
877                             m->m_len);
878                         len += m->m_len;
879                 }
880                 m_freem(m0);
881                 rate = (ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate] &
882                     IEEE80211_RATE_VAL) * 5;
883                 awi_write_1(sc, ntxd + AWI_TXD_STATE, 0);
884                 awi_write_4(sc, txd + AWI_TXD_START, frame);
885                 awi_write_4(sc, txd + AWI_TXD_NEXT, ntxd);
886                 awi_write_4(sc, txd + AWI_TXD_LENGTH, len);
887                 awi_write_1(sc, txd + AWI_TXD_RATE, rate);
888                 awi_write_4(sc, txd + AWI_TXD_NDA, 0);
889                 awi_write_4(sc, txd + AWI_TXD_NRA, 0);
890                 awi_write_1(sc, txd + AWI_TXD_STATE, AWI_TXD_ST_OWN);
891                 sc->sc_txnext = ntxd;
892
893                 sc->sc_tx_timer = 5;
894                 ifp->if_timer = 1;
895         }
896 }
897
898 static void
899 awi_watchdog(struct ifnet *ifp)
900 {
901         struct awi_softc *sc = ifp->if_softc;
902         u_int32_t prevdone;
903         int ocansleep;
904
905         ifp->if_timer = 0;
906         if (!sc->sc_enabled || sc->sc_invalid)
907                 return;
908
909         ocansleep = sc->sc_cansleep;
910         sc->sc_cansleep = 0;
911         if (sc->sc_tx_timer) {
912                 if (--sc->sc_tx_timer == 0) {
913                         printf("%s: device timeout\n", ifp->if_xname);
914                         prevdone = sc->sc_txdone;
915                         awi_tx_int(sc);
916                         if (sc->sc_txdone == prevdone) {
917                                 ifp->if_oerrors++;
918                                 awi_init(ifp);
919                                 goto out;
920                         }
921                 }
922                 ifp->if_timer = 1;
923         }
924         if (sc->sc_rx_timer) {
925                 if (--sc->sc_rx_timer == 0) {
926                         if (sc->sc_ic.ic_state == IEEE80211_S_RUN) {
927                                 ieee80211_new_state(&sc->sc_ic,
928                                     IEEE80211_S_SCAN, -1);
929                                 goto out;
930                         }
931                 } else
932                         ifp->if_timer = 1;
933         }
934         /* TODO: rate control */
935   out:
936         sc->sc_cansleep = ocansleep;
937 }
938
939 static int
940 awi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
941 {
942         struct awi_softc *sc = ifp->if_softc;
943         struct ifreq *ifr = (struct ifreq *)data;
944         int s, error;
945
946         s = splnet();
947         /* serialize ioctl, since we may sleep */
948         if ((error = awi_lock(sc)) != 0)
949                 goto cantlock;
950
951         switch (cmd) {
952         case SIOCSIFFLAGS:
953                 if (ifp->if_flags & IFF_UP) {
954                         if (sc->sc_enabled) {
955                                 /*
956                                  * To avoid rescanning another access point,
957                                  * do not call awi_init() here.  Instead,
958                                  * only reflect promisc mode settings.
959                                  */
960                                 error = awi_mode_init(sc);
961                         } else
962                                 error = awi_init(ifp);
963                 } else if (sc->sc_enabled)
964                         awi_stop(ifp, 1);
965                 break;
966         case SIOCSIFMEDIA:
967         case SIOCGIFMEDIA:
968                 error = ifmedia_ioctl(ifp, ifr, &sc->sc_ic.ic_media, cmd);
969                 break;
970         case SIOCADDMULTI:
971         case SIOCDELMULTI:
972 #ifdef __FreeBSD__
973                 error = ENETRESET;      /* XXX */
974 #else
975                 error = (cmd == SIOCADDMULTI) ?
976                     ether_addmulti(ifr, &sc->sc_ic.ic_ec) :
977                     ether_delmulti(ifr, &sc->sc_ic.ic_ec);
978 #endif
979                 if (error == ENETRESET) {
980                         /* do not rescan */
981                         if (sc->sc_enabled)
982                                 error = awi_mode_init(sc);
983                         else
984                                 error = 0;
985                 }
986                 break;
987         default:
988                 error = ieee80211_ioctl(&sc->sc_ic, cmd, data);
989                 if (error == ENETRESET) {
990                         if (sc->sc_enabled)
991                                 error = awi_init(ifp);
992                         else
993                                 error = 0;
994                 }
995                 break;
996         }
997         awi_unlock(sc);
998   cantlock:
999         splx(s);
1000         return error;
1001 }
1002
1003 /*
1004  * Called from ifmedia_ioctl via awi_ioctl with lock obtained.
1005  *
1006  * TBD factor with ieee80211_media_change
1007  */
1008 static int
1009 awi_media_change(struct ifnet *ifp)
1010 {
1011         struct awi_softc *sc = ifp->if_softc;
1012         struct ieee80211com *ic = &sc->sc_ic;
1013         struct ifmedia_entry *ime;
1014         enum ieee80211_opmode newmode;
1015         int i, rate, newadhoc_ap, error = 0;
1016
1017         ime = ic->ic_media.ifm_cur;
1018         if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) {
1019                 i = -1;
1020                 rate = ic->ic_fixed_rate;
1021         } else {
1022                 struct ieee80211_rateset *rs =
1023                     &ic->ic_sup_rates[(ic->ic_phytype == IEEE80211_T_FH)
1024                     ? IEEE80211_MODE_FH : IEEE80211_MODE_11B];
1025                 rate = ieee80211_media2rate(ime->ifm_media);
1026                 if (rate == 0)
1027                         return EINVAL;
1028                 for (i = 0; i < rs->rs_nrates; i++) {
1029                         if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
1030                                 break;
1031                 }
1032                 if (i == rs->rs_nrates)
1033                         return EINVAL;
1034         }
1035         if (ic->ic_fixed_rate != rate) {
1036                 ic->ic_fixed_rate = rate;
1037                 error = ENETRESET;
1038         }
1039
1040         /*
1041          * combination of mediaopt
1042          *
1043          * hostap adhoc flag0   opmode  adhoc_ap        comment
1044          *   +      -     -     HOSTAP      0           HostAP
1045          *   -      +     -     IBSS        0           IBSS
1046          *   -      +     +     AHDEMO      0           WaveLAN adhoc
1047          *   -      -     +     IBSS        1           Melco old Sta
1048          *                                                      also LINK0
1049          *   -      -     -     STA         0           Infra Station
1050          */
1051         newadhoc_ap = 0;
1052         if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
1053                 newmode = IEEE80211_M_HOSTAP;
1054         else if (ime->ifm_media & IFM_IEEE80211_ADHOC) {
1055                 if (ic->ic_phytype == IEEE80211_T_DS &&
1056                     (ime->ifm_media & IFM_FLAG0))
1057                         newmode = IEEE80211_M_AHDEMO;
1058                 else
1059                         newmode = IEEE80211_M_IBSS;
1060         } else if (ime->ifm_media & IFM_FLAG0) {
1061                 newmode = IEEE80211_M_IBSS;
1062                 newadhoc_ap = 1;
1063         } else
1064                 newmode = IEEE80211_M_STA;
1065         if (ic->ic_opmode != newmode || sc->sc_adhoc_ap != newadhoc_ap) {
1066                 ic->ic_opmode = newmode;
1067                 sc->sc_adhoc_ap = newadhoc_ap;
1068                 error = ENETRESET;
1069         }
1070
1071         if (error == ENETRESET) {
1072                 if (sc->sc_enabled)
1073                         error = awi_init(ifp);
1074                 else
1075                         error = 0;
1076         }
1077         return error;
1078 }
1079
1080 static void
1081 awi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1082 {
1083         struct awi_softc *sc = ifp->if_softc;
1084         struct ieee80211com *ic = &sc->sc_ic;
1085         int rate;
1086         enum ieee80211_phymode mode;
1087
1088         imr->ifm_status = IFM_AVALID;
1089         if (ic->ic_state == IEEE80211_S_RUN)
1090                 imr->ifm_status |= IFM_ACTIVE;
1091         imr->ifm_active = IFM_IEEE80211;
1092         if (ic->ic_phytype == IEEE80211_T_FH)
1093                 mode = IEEE80211_MODE_FH;
1094         else
1095                 mode = IEEE80211_MODE_11B;
1096         if (ic->ic_state == IEEE80211_S_RUN) {
1097                 rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate] &
1098                     IEEE80211_RATE_VAL;
1099         } else {
1100                 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
1101                         rate = 0;
1102                 else
1103                         rate = ic->ic_fixed_rate;
1104         }
1105         imr->ifm_active |= ieee80211_rate2media(ic, rate, mode);
1106         switch (ic->ic_opmode) {
1107         case IEEE80211_M_MONITOR: /* we should never reach here */
1108         case IEEE80211_M_WDS:
1109                 break;
1110         case IEEE80211_M_STA:
1111                 break;
1112         case IEEE80211_M_IBSS:
1113                 if (sc->sc_adhoc_ap)
1114                         imr->ifm_active |= IFM_FLAG0;
1115                 else
1116                         imr->ifm_active |= IFM_IEEE80211_ADHOC;
1117                 break;
1118         case IEEE80211_M_AHDEMO:
1119                 imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1120                 break;
1121         case IEEE80211_M_HOSTAP:
1122                 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1123                 break;
1124         }
1125 }
1126
1127 static int
1128 awi_mode_init(struct awi_softc *sc)
1129 {
1130         struct ifnet *ifp = AC2IFP(&sc->sc_arp);
1131         int n, error;
1132 #ifdef __FreeBSD__
1133         struct ifmultiaddr *ifma;
1134 #else
1135         struct ether_multi *enm;
1136         struct ether_multistep step;
1137 #endif
1138
1139         /* reinitialize muticast filter */
1140         n = 0;
1141         sc->sc_mib_local.Accept_All_Multicast_Dis = 0;
1142         if (sc->sc_ic.ic_opmode != IEEE80211_M_HOSTAP &&
1143             (ifp->if_flags & IFF_PROMISC)) {
1144                 sc->sc_mib_mac.aPromiscuous_Enable = 1;
1145                 goto set_mib;
1146         }
1147         sc->sc_mib_mac.aPromiscuous_Enable = 0;
1148 #ifdef __FreeBSD__
1149         if (ifp->if_flags & IFF_ALLMULTI)
1150                 goto set_mib;
1151         IF_ADDR_LOCK(ifp);
1152         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1153                 if (ifma->ifma_addr->sa_family != AF_LINK)
1154                         continue;
1155                 if (n == AWI_GROUP_ADDR_SIZE) {
1156                         IF_ADDR_UNLOCK(ifp);
1157                         goto set_mib;
1158                 }
1159                 IEEE80211_ADDR_COPY(sc->sc_mib_addr.aGroup_Addresses[n],
1160                     LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1161                 n++;
1162         }
1163         IF_ADDR_UNLOCK(ifp);
1164 #else
1165         ETHER_FIRST_MULTI(step, &sc->sc_ic.ic_ec, enm);
1166         while (enm != NULL) {
1167                 if (n == AWI_GROUP_ADDR_SIZE ||
1168                     !IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi))
1169                         goto set_mib;
1170                 IEEE80211_ADDR_COPY(sc->sc_mib_addr.aGroup_Addresses[n],
1171                     enm->enm_addrlo);
1172                 n++;
1173                 ETHER_NEXT_MULTI(step, enm);
1174         }
1175 #endif
1176         for (; n < AWI_GROUP_ADDR_SIZE; n++)
1177                 memset(sc->sc_mib_addr.aGroup_Addresses[n], 0,
1178                     IEEE80211_ADDR_LEN);
1179         sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
1180
1181   set_mib:
1182 #ifndef __FreeBSD__
1183         if (sc->sc_mib_local.Accept_All_Multicast_Dis)
1184                 ifp->if_flags &= ~IFF_ALLMULTI;
1185         else
1186                 ifp->if_flags |= IFF_ALLMULTI;
1187 #endif
1188         sc->sc_mib_mgt.Wep_Required =
1189             (sc->sc_ic.ic_flags & IEEE80211_F_PRIVACY) ? AWI_WEP_ON : AWI_WEP_OFF;
1190
1191         if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) ||
1192             (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_ADDR, AWI_WAIT)) ||
1193             (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MAC, AWI_WAIT)) ||
1194             (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT, AWI_WAIT)) ||
1195             (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_PHY, AWI_WAIT))) {
1196                 DPRINTF(("awi_mode_init: MIB set failed: %d\n", error));
1197                 return error;
1198         }
1199         return 0;
1200 }
1201
1202 static void
1203 awi_rx_int(struct awi_softc *sc)
1204 {
1205         struct ieee80211com *ic = &sc->sc_ic;
1206         struct ifnet *ifp = AC2IFP(&sc->sc_arp);
1207         struct ieee80211_node *ni;
1208         u_int8_t state, rate, rssi;
1209         u_int16_t len;
1210         u_int32_t frame, next, rstamp, rxoff;
1211         struct mbuf *m;
1212
1213         rxoff = sc->sc_rxdoff;
1214         for (;;) {
1215                 state = awi_read_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE);
1216                 if (state & AWI_RXD_ST_OWN)
1217                         break;
1218                 if (!(state & AWI_RXD_ST_CONSUMED)) {
1219                         if (sc->sc_substate != AWI_ST_NONE)
1220                                 goto rx_next;
1221                         if (state & AWI_RXD_ST_RXERROR) {
1222                                 ifp->if_ierrors++;
1223                                 goto rx_next;
1224                         }
1225                         len    = awi_read_2(sc, rxoff + AWI_RXD_LEN);
1226                         rate   = awi_read_1(sc, rxoff + AWI_RXD_RATE);
1227                         rssi   = awi_read_1(sc, rxoff + AWI_RXD_RSSI);
1228                         frame  = awi_read_4(sc, rxoff + AWI_RXD_START_FRAME) &
1229                             0x7fff;
1230                         rstamp = awi_read_4(sc, rxoff + AWI_RXD_LOCALTIME);
1231                         m = awi_devget(sc, frame, len);
1232                         if (m == NULL) {
1233                                 ifp->if_ierrors++;
1234                                 goto rx_next;
1235                         }
1236                         if (state & AWI_RXD_ST_LF) {
1237                                 /* TODO check my bss */
1238                                 if (!(sc->sc_ic.ic_flags & IEEE80211_F_SIBSS) &&
1239                                     sc->sc_ic.ic_state == IEEE80211_S_RUN) {
1240                                         sc->sc_rx_timer = 10;
1241                                         ifp->if_timer = 1;
1242                                 }
1243                                 if ((ifp->if_flags & IFF_DEBUG) &&
1244                                     (ifp->if_flags & IFF_LINK2))
1245                                         ieee80211_dump_pkt(ic,
1246                                             m->m_data, m->m_len,
1247                                             rate / 5, rssi);
1248                                 if ((ifp->if_flags & IFF_LINK0) ||
1249                                     sc->sc_adhoc_ap)
1250                                         m = awi_ether_modcap(sc, m);
1251                                 else
1252                                         m = m_pullup(m,
1253                                             sizeof(struct ieee80211_frame_min));
1254                                 if (m == NULL) {
1255                                         ifp->if_ierrors++;
1256                                         goto rx_next;
1257                                 }
1258                                 ni = ieee80211_find_rxnode(ic,
1259                                         mtod(m, struct ieee80211_frame_min *));
1260                                 /* XXX 0 for noise floor */
1261                                 ieee80211_input(ic, m, ni, rssi, 0, rstamp);
1262                                 ieee80211_free_node(ni);
1263                         } else
1264                                 sc->sc_rxpend = m;
1265   rx_next:
1266                         state |= AWI_RXD_ST_CONSUMED;
1267                         awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1268                 }
1269                 next = awi_read_4(sc, rxoff + AWI_RXD_NEXT);
1270                 if (next & AWI_RXD_NEXT_LAST)
1271                         break;
1272                 /* make sure the next pointer is correct */
1273                 if (next != awi_read_4(sc, rxoff + AWI_RXD_NEXT))
1274                         break;
1275                 state |= AWI_RXD_ST_OWN;
1276                 awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1277                 rxoff = next & 0x7fff;
1278         }
1279         sc->sc_rxdoff = rxoff;
1280 }
1281
1282 static void
1283 awi_tx_int(struct awi_softc *sc)
1284 {
1285         struct ifnet *ifp = AC2IFP(&sc->sc_arp);
1286         u_int8_t flags;
1287
1288         while (sc->sc_txdone != sc->sc_txnext) {
1289                 flags = awi_read_1(sc, sc->sc_txdone + AWI_TXD_STATE);
1290                 if ((flags & AWI_TXD_ST_OWN) || !(flags & AWI_TXD_ST_DONE))
1291                         break;
1292                 if (flags & AWI_TXD_ST_ERROR)
1293                         ifp->if_oerrors++;
1294                 sc->sc_txdone = awi_read_4(sc, sc->sc_txdone + AWI_TXD_NEXT) &
1295                     0x7fff;
1296         }
1297         DPRINTF2(("awi_txint: txdone %d txnext %d txbase %d txend %d\n",
1298             sc->sc_txdone, sc->sc_txnext, sc->sc_txbase, sc->sc_txend));
1299         sc->sc_tx_timer = 0;
1300         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1301         awi_start(ifp);
1302 }
1303
1304 static struct mbuf *
1305 awi_devget(struct awi_softc *sc, u_int32_t off, u_int16_t len)
1306 {
1307         struct ifnet *ifp = AC2IFP(&sc->sc_arp);
1308         struct mbuf *m;
1309         struct mbuf *top, **mp;
1310         u_int tlen;
1311
1312         top = sc->sc_rxpend;
1313         mp = &top;
1314         if (top != NULL) {
1315                 sc->sc_rxpend = NULL;
1316                 top->m_pkthdr.len += len;
1317                 m = top;
1318                 while (*mp != NULL) {
1319                         m = *mp;
1320                         mp = &m->m_next;
1321                 }
1322                 if (m->m_flags & M_EXT)
1323                         tlen = m->m_ext.ext_size;
1324                 else if (m->m_flags & M_PKTHDR)
1325                         tlen = MHLEN;
1326                 else
1327                         tlen = MLEN;
1328                 tlen -= m->m_len;
1329                 if (tlen > len)
1330                         tlen = len;
1331                 awi_read_bytes(sc, off, mtod(m, u_int8_t *) + m->m_len, tlen);
1332                 off += tlen;
1333                 len -= tlen;
1334         }
1335
1336         while (len > 0) {
1337                 if (top == NULL) {
1338                         MGETHDR(m, M_DONTWAIT, MT_DATA);
1339                         if (m == NULL)
1340                                 return NULL;
1341                         m->m_pkthdr.rcvif = ifp;
1342                         m->m_pkthdr.len = len;
1343                         m->m_len = MHLEN;
1344                 } else {
1345                         MGET(m, M_DONTWAIT, MT_DATA);
1346                         if (m == NULL) {
1347                                 m_freem(top);
1348                                 return NULL;
1349                         }
1350                         m->m_len = MLEN;
1351                 }
1352                 if (len >= MINCLSIZE) {
1353                         MCLGET(m, M_DONTWAIT);
1354                         if (m->m_flags & M_EXT)
1355                                 m->m_len = m->m_ext.ext_size;
1356                 }
1357                 if (top == NULL) {
1358                         int hdrlen = sizeof(struct ieee80211_frame) +
1359                             sizeof(struct llc);
1360                         caddr_t newdata = (caddr_t)
1361                             ALIGN(m->m_data + hdrlen) - hdrlen;
1362                         m->m_len -= newdata - m->m_data;
1363                         m->m_data = newdata;
1364                 }
1365                 if (m->m_len > len)
1366                         m->m_len = len;
1367                 awi_read_bytes(sc, off, mtod(m, u_int8_t *), m->m_len);
1368                 off += m->m_len;
1369                 len -= m->m_len;
1370                 *mp = m;
1371                 mp = &m->m_next;
1372         }
1373         if (top != NULL) {
1374                 /* Strip trailing 802.11 MAC FCS. */
1375                 m_adj(top, -IEEE80211_CRC_LEN);
1376         }
1377         return top;
1378 }
1379
1380 /*
1381  * Initialize hardware and start firmware to accept commands.
1382  * Called everytime after power on firmware.
1383  */
1384
1385 static int
1386 awi_hw_init(struct awi_softc *sc)
1387 {
1388         u_int8_t status;
1389         u_int16_t intmask;
1390         int i, error;
1391
1392         sc->sc_enab_intr = 0;
1393         sc->sc_invalid = 0;     /* XXX: really? */
1394         awi_drvstate(sc, AWI_DRV_RESET);
1395
1396         /* reset firmware */
1397         am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1398         DELAY(100);
1399         awi_write_1(sc, AWI_SELFTEST, 0);
1400         awi_write_1(sc, AWI_CMD, 0);
1401         awi_write_1(sc, AWI_BANNER, 0);
1402         am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1403         DELAY(100);
1404
1405         /* wait for selftest completion */
1406         for (i = 0; ; i++) {
1407                 if (sc->sc_invalid)
1408                         return ENXIO;
1409                 if (i >= AWI_SELFTEST_TIMEOUT*hz/1000) {
1410                         printf("%s: failed to complete selftest (timeout)\n",
1411                             AC2IFP(&sc->sc_arp)->if_xname);
1412                         return ENXIO;
1413                 }
1414                 status = awi_read_1(sc, AWI_SELFTEST);
1415                 if ((status & 0xf0) == 0xf0)
1416                         break;
1417                 if (sc->sc_cansleep) {
1418                         sc->sc_sleep_cnt++;
1419                         (void)tsleep(sc, PWAIT, "awitst", 1);
1420                         sc->sc_sleep_cnt--;
1421                 } else {
1422                         DELAY(1000*1000/hz);
1423                 }
1424         }
1425         if (status != AWI_SELFTEST_PASSED) {
1426                 printf("%s: failed to complete selftest (code %x)\n",
1427                     AC2IFP(&sc->sc_arp)->if_xname, status);
1428                 return ENXIO;
1429         }
1430
1431         /* check banner to confirm firmware write it */
1432         awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
1433         if (memcmp(sc->sc_banner, "PCnetMobile:", 12) != 0) {
1434                 printf("%s: failed to complete selftest (bad banner)\n",
1435                     AC2IFP(&sc->sc_arp)->if_xname);
1436                 for (i = 0; i < AWI_BANNER_LEN; i++)
1437                         printf("%s%02x", i ? ":" : "\t", sc->sc_banner[i]);
1438                 printf("\n");
1439                 return ENXIO;
1440         }
1441
1442         /* initializing interrupt */
1443         sc->sc_enab_intr = 1;
1444         error = awi_intr_lock(sc);
1445         if (error)
1446                 return error;
1447         intmask = AWI_INT_GROGGY | AWI_INT_SCAN_CMPLT |
1448             AWI_INT_TX | AWI_INT_RX | AWI_INT_CMD;
1449         awi_write_1(sc, AWI_INTMASK, ~intmask & 0xff);
1450         awi_write_1(sc, AWI_INTMASK2, 0);
1451         awi_write_1(sc, AWI_INTSTAT, 0);
1452         awi_write_1(sc, AWI_INTSTAT2, 0);
1453         awi_intr_unlock(sc);
1454         am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_ENECINT);
1455
1456         /* issuing interface test command */
1457         error = awi_cmd(sc, AWI_CMD_NOP, AWI_WAIT);
1458         if (error) {
1459                 printf("%s: failed to complete selftest",
1460                     AC2IFP(&sc->sc_arp)->if_xname);
1461                 if (error == ENXIO)
1462                         printf(" (no hardware)\n");
1463                 else if (error != EWOULDBLOCK)
1464                         printf(" (error %d)\n", error);
1465                 else if (sc->sc_cansleep)
1466                         printf(" (lost interrupt)\n");
1467                 else
1468                         printf(" (command timeout)\n");
1469                 return error;
1470         }
1471
1472         /* Initialize VBM */
1473         awi_write_1(sc, AWI_VBM_OFFSET, 0);
1474         awi_write_1(sc, AWI_VBM_LENGTH, 1);
1475         awi_write_1(sc, AWI_VBM_BITMAP, 0);
1476         return 0;
1477 }
1478
1479 /*
1480  * Extract the factory default MIB value from firmware and assign the driver
1481  * default value.
1482  * Called once at attaching the interface.
1483  */
1484
1485 static int
1486 awi_init_mibs(struct awi_softc *sc)
1487 {
1488         int chan, i, error;
1489         struct ieee80211com *ic = &sc->sc_ic;
1490         struct awi_chanset *cs;
1491
1492         if ((error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) ||
1493             (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_ADDR, AWI_WAIT)) ||
1494             (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MAC, AWI_WAIT)) ||
1495             (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MGT, AWI_WAIT)) ||
1496             (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_PHY, AWI_WAIT))) {
1497                 printf("%s: failed to get default mib value (error %d)\n",
1498                     AC2IFP(&sc->sc_arp)->if_xname, error);
1499                 return error;
1500         }
1501
1502         memset(&sc->sc_ic.ic_chan_avail, 0, sizeof(sc->sc_ic.ic_chan_avail));
1503         for (cs = awi_chanset; ; cs++) {
1504                 if (cs->cs_type == 0) {
1505                         printf("%s: failed to set available channel\n",
1506                             AC2IFP(&sc->sc_arp)->if_xname);
1507                         return ENXIO;
1508                 }
1509                 if (cs->cs_type == sc->sc_mib_phy.IEEE_PHY_Type &&
1510                     cs->cs_region == sc->sc_mib_phy.aCurrent_Reg_Domain)
1511                         break;
1512         }
1513         if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1514                 for (i = cs->cs_min; i <= cs->cs_max; i++) {
1515                         chan = IEEE80211_FH_CHAN(i % 3 + 1, i);
1516                         setbit(sc->sc_ic.ic_chan_avail, chan);
1517                         /* XXX for FHSS, does frequency matter? */
1518                         ic->ic_channels[chan].ic_freq = 0;
1519                         ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_FHSS;
1520                         /*
1521                          * According to the IEEE 802.11 specification,
1522                          * hop pattern parameter for FH phy should be
1523                          * incremented by 3 for given hop chanset, i.e.,
1524                          * the chanset parameter is calculated for given
1525                          * hop patter.  However, BayStack 650 Access Points
1526                          * apparently use fixed hop chanset parameter value
1527                          * 1 for any hop pattern.  So we also try this
1528                          * combination of hop chanset and pattern.
1529                          */
1530                         chan = IEEE80211_FH_CHAN(1, i);
1531                         setbit(sc->sc_ic.ic_chan_avail, chan);
1532                         ic->ic_channels[chan].ic_freq = 0; /* XXX */
1533                         ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_FHSS;
1534                 }
1535         } else {
1536                 for (i = cs->cs_min; i <= cs->cs_max; i++) {
1537                         setbit(sc->sc_ic.ic_chan_avail, i);
1538                         ic->ic_channels[i].ic_freq =
1539                             ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
1540                         ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
1541                 }
1542         }
1543         sc->sc_cur_chan = cs->cs_def;
1544         ic->ic_curchan = &ic->ic_channels[cs->cs_def];  /* XXX? */
1545
1546         sc->sc_mib_local.Fragmentation_Dis = 1;
1547         sc->sc_mib_local.Add_PLCP_Dis = 0;
1548         sc->sc_mib_local.MAC_Hdr_Prsv = 0;
1549         sc->sc_mib_local.Rx_Mgmt_Que_En = 0;
1550         sc->sc_mib_local.Re_Assembly_Dis = 1;
1551         sc->sc_mib_local.Strip_PLCP_Dis = 0;
1552         sc->sc_mib_local.Power_Saving_Mode_Dis = 1;
1553         sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
1554         sc->sc_mib_local.Check_Seq_Cntl_Dis = 0;
1555         sc->sc_mib_local.Flush_CFP_Queue_On_CF_End = 0;
1556         sc->sc_mib_local.Network_Mode = 1;
1557         sc->sc_mib_local.PWD_Lvl = 0;
1558         sc->sc_mib_local.CFP_Mode = 0;
1559
1560         /* allocate buffers */
1561         sc->sc_txbase = AWI_BUFFERS;
1562         sc->sc_txend = sc->sc_txbase +
1563             (AWI_TXD_SIZE + sizeof(struct ieee80211_frame) +
1564             sizeof(struct ether_header) + ETHERMTU) * AWI_NTXBUFS;
1565         LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Offset, sc->sc_txbase);
1566         LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Size,
1567             sc->sc_txend - sc->sc_txbase);
1568         LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Offset, sc->sc_txend);
1569         LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Size,
1570             AWI_BUFFERS_END - sc->sc_txend);
1571         sc->sc_mib_local.Acting_as_AP = 0;
1572         sc->sc_mib_local.Fill_CFP = 0;
1573
1574         memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
1575         sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
1576
1577         sc->sc_mib_mgt.aPower_Mgt_Mode = 0;
1578         sc->sc_mib_mgt.aDTIM_Period = 1;
1579         LE_WRITE_2(&sc->sc_mib_mgt.aATIM_Window, 0);
1580         return 0;
1581 }
1582
1583 static int
1584 awi_mib(struct awi_softc *sc, u_int8_t cmd, u_int8_t mib, int wflag)
1585 {
1586         int error;
1587         u_int8_t size, *ptr;
1588
1589         switch (mib) {
1590         case AWI_MIB_LOCAL:
1591                 ptr = (u_int8_t *)&sc->sc_mib_local;
1592                 size = sizeof(sc->sc_mib_local);
1593                 break;
1594         case AWI_MIB_ADDR:
1595                 ptr = (u_int8_t *)&sc->sc_mib_addr;
1596                 size = sizeof(sc->sc_mib_addr);
1597                 break;
1598         case AWI_MIB_MAC:
1599                 ptr = (u_int8_t *)&sc->sc_mib_mac;
1600                 size = sizeof(sc->sc_mib_mac);
1601                 break;
1602         case AWI_MIB_STAT:
1603                 ptr = (u_int8_t *)&sc->sc_mib_stat;
1604                 size = sizeof(sc->sc_mib_stat);
1605                 break;
1606         case AWI_MIB_MGT:
1607                 ptr = (u_int8_t *)&sc->sc_mib_mgt;
1608                 size = sizeof(sc->sc_mib_mgt);
1609                 break;
1610         case AWI_MIB_PHY:
1611                 ptr = (u_int8_t *)&sc->sc_mib_phy;
1612                 size = sizeof(sc->sc_mib_phy);
1613                 break;
1614         default:
1615                 return EINVAL;
1616         }
1617         if (sc->sc_cmd_inprog) {
1618                 if ((error = awi_cmd_wait(sc)) != 0) {
1619                         if (error == EWOULDBLOCK)
1620                                 DPRINTF(("awi_mib: cmd %d inprog",
1621                                     sc->sc_cmd_inprog));
1622                         return error;
1623                 }
1624         }
1625         sc->sc_cmd_inprog = cmd;
1626         if (cmd == AWI_CMD_SET_MIB)
1627                 awi_write_bytes(sc, AWI_CA_MIB_DATA, ptr, size);
1628         awi_write_1(sc, AWI_CA_MIB_TYPE, mib);
1629         awi_write_1(sc, AWI_CA_MIB_SIZE, size);
1630         awi_write_1(sc, AWI_CA_MIB_INDEX, 0);
1631         if ((error = awi_cmd(sc, cmd, wflag)) != 0)
1632                 return error;
1633         if (cmd == AWI_CMD_GET_MIB) {
1634                 awi_read_bytes(sc, AWI_CA_MIB_DATA, ptr, size);
1635 #ifdef AWI_DEBUG
1636                 if (awi_debug) {
1637                         int i;
1638
1639                         printf("awi_mib: #%d:", mib);
1640                         for (i = 0; i < size; i++)
1641                                 printf(" %02x", ptr[i]);
1642                         printf("\n");
1643                 }
1644 #endif
1645         }
1646         return 0;
1647 }
1648
1649 static int
1650 awi_cmd(struct awi_softc *sc, u_int8_t cmd, int wflag)
1651 {
1652         u_int8_t status;
1653         int error = 0;
1654 #ifdef AWI_DEBUG
1655         static const char *cmdname[] = {
1656             "IDLE", "NOP", "SET_MIB", "INIT_TX", "FLUSH_TX", "INIT_RX",
1657             "KILL_RX", "SLEEP", "WAKE", "GET_MIB", "SCAN", "SYNC", "RESUME"
1658         };
1659 #endif
1660
1661 #ifdef AWI_DEBUG
1662         if (awi_debug > 1) {
1663                 if (cmd >= sizeof(cmdname)/sizeof(cmdname[0]))
1664                         printf("awi_cmd: #%d", cmd);
1665                 else
1666                         printf("awi_cmd: %s", cmdname[cmd]);
1667                 printf(" %s\n", wflag == AWI_NOWAIT ? "nowait" : "wait");
1668         }
1669 #endif
1670         sc->sc_cmd_inprog = cmd;
1671         awi_write_1(sc, AWI_CMD_STATUS, AWI_STAT_IDLE);
1672         awi_write_1(sc, AWI_CMD, cmd);
1673         if (wflag == AWI_NOWAIT)
1674                 return EINPROGRESS;
1675         if ((error = awi_cmd_wait(sc)) != 0)
1676                 return error;
1677         status = awi_read_1(sc, AWI_CMD_STATUS);
1678         awi_write_1(sc, AWI_CMD, 0);
1679         switch (status) {
1680         case AWI_STAT_OK:
1681                 break;
1682         case AWI_STAT_BADPARM:
1683                 return EINVAL;
1684         default:
1685                 printf("%s: command %d failed %x\n",
1686                     AC2IFP(&sc->sc_arp)->if_xname, cmd, status);
1687                 return ENXIO;
1688         }
1689         return 0;
1690 }
1691
1692 static int
1693 awi_cmd_wait(struct awi_softc *sc)
1694 {
1695         int i, error = 0;
1696
1697         i = 0;
1698         while (sc->sc_cmd_inprog) {
1699                 if (sc->sc_invalid)
1700                         return ENXIO;
1701                 if (awi_read_1(sc, AWI_CMD) != sc->sc_cmd_inprog) {
1702                         printf("%s: failed to access hardware\n",
1703                             AC2IFP(&sc->sc_arp)->if_xname);
1704                         sc->sc_invalid = 1;
1705                         return ENXIO;
1706                 }
1707                 if (sc->sc_cansleep) {
1708                         sc->sc_sleep_cnt++;
1709                         error = tsleep(sc, PWAIT, "awicmd",
1710                             AWI_CMD_TIMEOUT*hz/1000);
1711                         sc->sc_sleep_cnt--;
1712                 } else {
1713                         if (awi_read_1(sc, AWI_CMD_STATUS) != AWI_STAT_IDLE) {
1714                                 awi_cmd_done(sc);
1715                                 break;
1716                         }
1717                         if (i++ >= AWI_CMD_TIMEOUT*1000/10)
1718                                 error = EWOULDBLOCK;
1719                         else
1720                                 DELAY(10);
1721                 }
1722                 if (error)
1723                         break;
1724         }
1725         if (error) {
1726                 DPRINTF(("awi_cmd_wait: cmd 0x%x, error %d\n",
1727                     sc->sc_cmd_inprog, error));
1728         }
1729         return error;
1730 }
1731
1732 static void
1733 awi_cmd_done(struct awi_softc *sc)
1734 {
1735         u_int8_t cmd, status;
1736
1737         status = awi_read_1(sc, AWI_CMD_STATUS);
1738         if (status == AWI_STAT_IDLE)
1739                 return;         /* stray interrupt */
1740
1741         cmd = sc->sc_cmd_inprog;
1742         sc->sc_cmd_inprog = 0;
1743         wakeup(sc);
1744         awi_write_1(sc, AWI_CMD, 0);
1745
1746         if (status != AWI_STAT_OK) {
1747                 printf("%s: command %d failed %x\n",
1748                     AC2IFP(&sc->sc_arp)->if_xname, cmd, status);
1749                 sc->sc_substate = AWI_ST_NONE;
1750                 return;
1751         }
1752         if (sc->sc_substate != AWI_ST_NONE)
1753                 (void)ieee80211_new_state(&sc->sc_ic, sc->sc_nstate, -1);
1754 }
1755
1756 static int
1757 awi_next_txd(struct awi_softc *sc, int len, u_int32_t *framep, u_int32_t *ntxdp)
1758 {
1759         u_int32_t txd, ntxd, frame;
1760
1761         txd = sc->sc_txnext;
1762         frame = txd + AWI_TXD_SIZE;
1763         if (frame + len > sc->sc_txend)
1764                 frame = sc->sc_txbase;
1765         ntxd = frame + len;
1766         if (ntxd + AWI_TXD_SIZE > sc->sc_txend)
1767                 ntxd = sc->sc_txbase;
1768         *framep = frame;
1769         *ntxdp = ntxd;
1770         /*
1771          * Determine if there are any room in ring buffer.
1772          *              --- send wait,  === new data,  +++ conflict (ENOBUFS)
1773          *   base........................end
1774          *         done----txd=====ntxd         OK
1775          *       --txd=====done++++ntxd--       full
1776          *       --txd=====ntxd    done--       OK
1777          *       ==ntxd    done----txd===       OK
1778          *       ==done++++ntxd----txd===       full
1779          *       ++ntxd    txd=====done++       full
1780          */
1781         if (txd < ntxd) {
1782                 if (txd < sc->sc_txdone && ntxd + AWI_TXD_SIZE > sc->sc_txdone)
1783                         return ENOBUFS;
1784         } else {
1785                 if (txd < sc->sc_txdone || ntxd + AWI_TXD_SIZE > sc->sc_txdone)
1786                         return ENOBUFS;
1787         }
1788         return 0;
1789 }
1790
1791 static int
1792 awi_lock(struct awi_softc *sc)
1793 {
1794         int error = 0;
1795
1796 #ifdef __NetBSD__
1797         if (curlwp == NULL)
1798 #else
1799         if (curproc == NULL)
1800 #endif
1801         {
1802                 /*
1803                  * XXX
1804                  * Though driver ioctl should be called with context,
1805                  * KAME ipv6 stack calls ioctl in interrupt for now.
1806                  * We simply abort the request if there are other
1807                  * ioctl requests in progress.
1808                  */
1809                 if (sc->sc_busy) {
1810                         if (sc->sc_invalid)
1811                                 return ENXIO;
1812                         return EWOULDBLOCK;
1813                 }
1814                 sc->sc_busy = 1;
1815                 sc->sc_cansleep = 0;
1816                 return 0;
1817         }
1818         while (sc->sc_busy) {
1819                 if (sc->sc_invalid)
1820                         return ENXIO;
1821                 sc->sc_sleep_cnt++;
1822                 error = tsleep(sc, PWAIT | PCATCH, "awilck", 0);
1823                 sc->sc_sleep_cnt--;
1824                 if (error)
1825                         return error;
1826         }
1827         sc->sc_busy = 1;
1828         sc->sc_cansleep = 1;
1829         return 0;
1830 }
1831
1832 static void
1833 awi_unlock(struct awi_softc *sc)
1834 {
1835         sc->sc_busy = 0;
1836         sc->sc_cansleep = 0;
1837         if (sc->sc_sleep_cnt)
1838                 wakeup(sc);
1839 }
1840
1841 static int
1842 awi_intr_lock(struct awi_softc *sc)
1843 {
1844         u_int8_t status;
1845         int i, retry;
1846
1847         status = 1;
1848         for (retry = 0; retry < 10; retry++) {
1849                 for (i = 0; i < AWI_LOCKOUT_TIMEOUT*1000/5; i++) {
1850                         if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0)
1851                                 break;
1852                         DELAY(5);
1853                 }
1854                 if (status != 0)
1855                         break;
1856                 awi_write_1(sc, AWI_LOCKOUT_MAC, 1);
1857                 if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0)
1858                         break;
1859                 awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
1860         }
1861         if (status != 0) {
1862                 printf("%s: failed to lock interrupt\n",
1863                     AC2IFP(&sc->sc_arp)->if_xname);
1864                 return ENXIO;
1865         }
1866         return 0;
1867 }
1868
1869 static void
1870 awi_intr_unlock(struct awi_softc *sc)
1871 {
1872
1873         awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
1874 }
1875
1876 static int
1877 awi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1878 {
1879         struct ifnet *ifp = ic->ic_ifp;
1880         struct awi_softc *sc = ifp->if_softc;
1881         struct ieee80211_node *ni;
1882         int error;
1883         u_int8_t newmode;
1884         enum ieee80211_state ostate;
1885 #ifdef AWI_DEBUG
1886         static const char *stname[] =
1887             { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" };
1888         static const char *substname[] =
1889             { "NONE", "SCAN_INIT", "SCAN_SETMIB", "SCAN_SCCMD",
1890               "SUB_INIT", "SUB_SETSS", "SUB_SYNC" };
1891 #endif /* AWI_DEBUG */
1892
1893         ostate = ic->ic_state;
1894         DPRINTF(("awi_newstate: %s (%s/%s) -> %s\n", stname[ostate],
1895             stname[sc->sc_nstate], substname[sc->sc_substate], stname[nstate]));
1896
1897         /* set LED */
1898         switch (nstate) {
1899         case IEEE80211_S_INIT:
1900                 awi_drvstate(sc, AWI_DRV_RESET);
1901                 break;
1902         case IEEE80211_S_SCAN:
1903                 if (ic->ic_opmode == IEEE80211_M_IBSS ||
1904                     ic->ic_opmode == IEEE80211_M_AHDEMO)
1905                         awi_drvstate(sc, AWI_DRV_ADHSC);
1906                 else
1907                         awi_drvstate(sc, AWI_DRV_INFSY);
1908                 break;
1909         case IEEE80211_S_AUTH:
1910                 awi_drvstate(sc, AWI_DRV_INFSY);
1911                 break;
1912         case IEEE80211_S_ASSOC:
1913                 awi_drvstate(sc, AWI_DRV_INFAUTH);
1914                 break;
1915         case IEEE80211_S_RUN:
1916                 if (ic->ic_opmode == IEEE80211_M_IBSS ||
1917                     ic->ic_opmode == IEEE80211_M_AHDEMO)
1918                         awi_drvstate(sc, AWI_DRV_ADHSY);
1919                 else
1920                         awi_drvstate(sc, AWI_DRV_INFASSOC);
1921                 break;
1922         default:
1923                 break;
1924         }
1925
1926         if (nstate == IEEE80211_S_INIT) {
1927                 sc->sc_substate = AWI_ST_NONE;
1928                 ic->ic_flags &= ~IEEE80211_F_SIBSS;
1929                 return (*sc->sc_newstate)(ic, nstate, arg);
1930         }
1931
1932         /* state transition */
1933         if (nstate == IEEE80211_S_SCAN) {
1934                 /* SCAN substate */
1935                 if (sc->sc_substate == AWI_ST_NONE) {
1936                         sc->sc_nstate = nstate; /* next state in transition */
1937                         sc->sc_substate = AWI_ST_SCAN_INIT;
1938                 }
1939                 switch (sc->sc_substate) {
1940                 case AWI_ST_SCAN_INIT:
1941                         sc->sc_substate = AWI_ST_SCAN_SETMIB;
1942                         switch (ostate) {
1943                         case IEEE80211_S_RUN:
1944                                 /* beacon miss */
1945                                 if (ifp->if_flags & IFF_DEBUG)
1946                                         printf("%s: no recent beacons from %s;"
1947                                             " rescanning\n",
1948                                             ifp->if_xname,
1949                                             ether_sprintf(ic->ic_bss->ni_bssid));
1950                                 /* FALLTHRU */
1951                         case IEEE80211_S_AUTH:
1952                         case IEEE80211_S_ASSOC:
1953                         case IEEE80211_S_INIT:
1954                           /* XXX revisit scanning */;
1955                                 break;
1956                         case IEEE80211_S_SCAN:
1957                                 /* scan next */
1958                                 break;
1959                         default:
1960                                 break;
1961                         }
1962                         if (ic->ic_flags & IEEE80211_F_ASCAN)
1963                                 newmode = AWI_SCAN_ACTIVE;
1964                         else
1965                                 newmode = AWI_SCAN_PASSIVE;
1966                         if (sc->sc_mib_mgt.aScan_Mode != newmode) {
1967                                 sc->sc_mib_mgt.aScan_Mode = newmode;
1968                                 if ((error = awi_mib(sc, AWI_CMD_SET_MIB,
1969                                     AWI_MIB_MGT, AWI_NOWAIT)) != 0)
1970                                         break;
1971                         }
1972                         /* FALLTHRU */
1973                 case AWI_ST_SCAN_SETMIB:
1974                         sc->sc_substate = AWI_ST_SCAN_SCCMD;
1975                         if (sc->sc_cmd_inprog) {
1976                                 if ((error = awi_cmd_wait(sc)) != 0)
1977                                         break;
1978                         }
1979                         sc->sc_cmd_inprog = AWI_CMD_SCAN;
1980                         ni = ic->ic_bss;
1981                         awi_write_2(sc, AWI_CA_SCAN_DURATION,
1982                             (ic->ic_flags & IEEE80211_F_ASCAN) ?
1983                             AWI_ASCAN_DURATION : AWI_PSCAN_DURATION);
1984                         if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1985                                 awi_write_1(sc, AWI_CA_SCAN_SET,
1986                                     IEEE80211_FH_CHANSET(
1987                                         ieee80211_chan2ieee(ic, ic->ic_curchan)));
1988                                 awi_write_1(sc, AWI_CA_SCAN_PATTERN,
1989                                     IEEE80211_FH_CHANPAT(
1990                                         ieee80211_chan2ieee(ic, ic->ic_curchan)));
1991                                 awi_write_1(sc, AWI_CA_SCAN_IDX, 1);
1992                         } else {
1993                                 awi_write_1(sc, AWI_CA_SCAN_SET,
1994                                     ieee80211_chan2ieee(ic, ic->ic_curchan));
1995                                 awi_write_1(sc, AWI_CA_SCAN_PATTERN, 0);
1996                                 awi_write_1(sc, AWI_CA_SCAN_IDX, 0);
1997                         }
1998                         awi_write_1(sc, AWI_CA_SCAN_SUSP, 0);
1999                         sc->sc_cur_chan = ieee80211_chan2ieee(ic, ic->ic_curchan);
2000                         if ((error = awi_cmd(sc, AWI_CMD_SCAN, AWI_NOWAIT))
2001                             != 0)
2002                                 break;
2003                         /* FALLTHRU */
2004                 case AWI_ST_SCAN_SCCMD:
2005                         ic->ic_state = nstate;
2006                         sc->sc_substate = AWI_ST_NONE;
2007                         error = EINPROGRESS;
2008                         break;
2009                 default:
2010                         DPRINTF(("awi_newstate: unexpected state %s/%s\n",
2011                             stname[nstate], substname[sc->sc_substate]));
2012                         sc->sc_substate = AWI_ST_NONE;
2013                         error = EIO;
2014                         break;
2015                 }
2016                 goto out;
2017         }
2018
2019         if (ostate == IEEE80211_S_SCAN) {
2020                 /* set SSID and channel */
2021                 /* substate */
2022                 if (sc->sc_substate == AWI_ST_NONE) {
2023                         sc->sc_nstate = nstate; /* next state in transition */
2024                         sc->sc_substate = AWI_ST_SUB_INIT;
2025                 }
2026                 ni = ic->ic_bss;
2027                 switch (sc->sc_substate) {
2028                 case AWI_ST_SUB_INIT:
2029                         sc->sc_substate = AWI_ST_SUB_SETSS;
2030                         IEEE80211_ADDR_COPY(&sc->sc_mib_mgt.aCurrent_BSS_ID,
2031                             ni->ni_bssid);
2032                         memset(&sc->sc_mib_mgt.aCurrent_ESS_ID, 0,
2033                             AWI_ESS_ID_SIZE);
2034                         sc->sc_mib_mgt.aCurrent_ESS_ID[0] =
2035                             IEEE80211_ELEMID_SSID;
2036                         sc->sc_mib_mgt.aCurrent_ESS_ID[1] = ni->ni_esslen;
2037                         memcpy(&sc->sc_mib_mgt.aCurrent_ESS_ID[2],
2038                             ni->ni_essid, ni->ni_esslen);
2039                         LE_WRITE_2(&sc->sc_mib_mgt.aBeacon_Period,
2040                             ni->ni_intval);
2041                         if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT,
2042                             AWI_NOWAIT)) != 0)
2043                                 break;
2044                         /* FALLTHRU */
2045                 case AWI_ST_SUB_SETSS:
2046                         sc->sc_substate = AWI_ST_SUB_SYNC;
2047                         if (sc->sc_cmd_inprog) {
2048                                 if ((error = awi_cmd_wait(sc)) != 0)
2049                                         break;
2050                         }
2051                         sc->sc_cmd_inprog = AWI_CMD_SYNC;
2052                         if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
2053                                 awi_write_1(sc, AWI_CA_SYNC_SET,
2054                                     IEEE80211_FH_CHANSET(
2055                                         ieee80211_chan2ieee(ic, ni->ni_chan)));
2056                                 awi_write_1(sc, AWI_CA_SYNC_PATTERN,
2057                                     IEEE80211_FH_CHANPAT(
2058                                         ieee80211_chan2ieee(ic, ni->ni_chan)));
2059                                 awi_write_1(sc, AWI_CA_SYNC_IDX,
2060                                     ni->ni_fhindex);
2061                                 awi_write_2(sc, AWI_CA_SYNC_DWELL,
2062                                     ni->ni_fhdwell);
2063                         } else {
2064                                 awi_write_1(sc, AWI_CA_SYNC_SET,
2065                                     ieee80211_chan2ieee(ic, ni->ni_chan));
2066                                 awi_write_1(sc, AWI_CA_SYNC_PATTERN, 0);
2067                                 awi_write_1(sc, AWI_CA_SYNC_IDX, 0);
2068                                 awi_write_2(sc, AWI_CA_SYNC_DWELL, 0);
2069                         }
2070                         if (ic->ic_flags & IEEE80211_F_SIBSS) {
2071                                 memset(&ni->ni_tstamp, 0, sizeof(ni->ni_tstamp));
2072                                 ni->ni_rstamp = 0;
2073                                 awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 1);
2074                         } else
2075                                 awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 0);
2076                         awi_write_2(sc, AWI_CA_SYNC_MBZ, 0);
2077                         awi_write_bytes(sc, AWI_CA_SYNC_TIMESTAMP,
2078                             ni->ni_tstamp.data, 8);
2079                         awi_write_4(sc, AWI_CA_SYNC_REFTIME, ni->ni_rstamp);
2080                         sc->sc_cur_chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2081                         if ((error = awi_cmd(sc, AWI_CMD_SYNC, AWI_NOWAIT))
2082                             != 0)
2083                                 break;
2084                         /* FALLTHRU */
2085                 case AWI_ST_SUB_SYNC:
2086                         sc->sc_substate = AWI_ST_NONE;
2087                         if (ic->ic_flags & IEEE80211_F_SIBSS) {
2088                                 if ((error = awi_mib(sc, AWI_CMD_GET_MIB,
2089                                     AWI_MIB_MGT, AWI_WAIT)) != 0)
2090                                         break;
2091                                 IEEE80211_ADDR_COPY(ni->ni_bssid,
2092                                     &sc->sc_mib_mgt.aCurrent_BSS_ID);
2093                         } else {
2094                                 if (nstate == IEEE80211_S_RUN) {
2095                                         sc->sc_rx_timer = 10;
2096                                         ifp->if_timer = 1;
2097                                 }
2098                         }
2099                         error = 0;
2100                         break;
2101                 default:
2102                         DPRINTF(("awi_newstate: unexpected state %s/%s\n",
2103                             stname[nstate], substname[sc->sc_substate]));
2104                         sc->sc_substate = AWI_ST_NONE;
2105                         error = EIO;
2106                         break;
2107                 }
2108                 goto out;
2109         }
2110
2111         sc->sc_substate = AWI_ST_NONE;
2112
2113         return (*sc->sc_newstate)(ic, nstate, arg);
2114 out:
2115         if (error != 0) {
2116                 if (error == EINPROGRESS)
2117                         error = 0;
2118                 return error;
2119         }
2120         return (*sc->sc_newstate)(ic, nstate, arg);
2121 }
2122
2123 static void
2124 awi_recv_mgmt(struct ieee80211com *ic, struct mbuf *m0,
2125         struct ieee80211_node *ni,
2126         int subtype, int rssi, int nf, u_int32_t rstamp)
2127 {
2128         struct awi_softc *sc = ic->ic_ifp->if_softc;
2129
2130         /* probe request is handled by hardware */
2131         if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_REQ)
2132                 return;
2133         (*sc->sc_recv_mgmt)(ic, m0, ni, subtype, rssi, nf, rstamp);
2134 }
2135
2136 static int
2137 awi_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
2138         int type, int arg)
2139 {
2140         struct awi_softc *sc = ic->ic_ifp->if_softc;
2141
2142         /* probe request is handled by hardware */
2143         if (type == IEEE80211_FC0_SUBTYPE_PROBE_REQ)
2144                 return 0;
2145         return (*sc->sc_send_mgmt)(ic, ni, type, arg);
2146 }
2147
2148 static struct mbuf *
2149 awi_ether_encap(struct awi_softc *sc, struct mbuf *m)
2150 {
2151         struct ieee80211com *ic = &sc->sc_ic;
2152         struct ieee80211_node *ni = ic->ic_bss;
2153         struct ether_header *eh;
2154         struct ieee80211_frame *wh;
2155
2156         if (m->m_len < sizeof(struct ether_header)) {
2157                 m = m_pullup(m, sizeof(struct ether_header));
2158                 if (m == NULL)
2159                         return NULL;
2160         }
2161         eh = mtod(m, struct ether_header *);
2162         M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
2163         if (m == NULL)
2164                 return NULL;
2165         wh = mtod(m, struct ieee80211_frame *);
2166         wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
2167         *(u_int16_t *)wh->i_dur = 0;
2168         *(u_int16_t *)wh->i_seq =
2169             htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
2170         ni->ni_txseqs[0]++;
2171         if (ic->ic_opmode == IEEE80211_M_IBSS ||
2172             ic->ic_opmode == IEEE80211_M_AHDEMO) {
2173                 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2174                 if (sc->sc_adhoc_ap)
2175                         IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
2176                 else
2177                         IEEE80211_ADDR_COPY(wh->i_addr1, eh->ether_dhost);
2178                 IEEE80211_ADDR_COPY(wh->i_addr2, eh->ether_shost);
2179                 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
2180         } else {
2181                 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
2182                 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
2183                 IEEE80211_ADDR_COPY(wh->i_addr2, eh->ether_shost);
2184                 IEEE80211_ADDR_COPY(wh->i_addr3, eh->ether_dhost);
2185         }
2186         return m;
2187 }
2188
2189 static struct mbuf *
2190 awi_ether_modcap(struct awi_softc *sc, struct mbuf *m)
2191 {
2192         struct ieee80211com *ic = &sc->sc_ic;
2193         struct ether_header eh;
2194         struct ieee80211_frame wh;
2195         struct llc *llc;
2196
2197         if (m->m_len < sizeof(wh) + sizeof(eh)) {
2198                 m = m_pullup(m, sizeof(wh) + sizeof(eh));
2199                 if (m == NULL)
2200                         return NULL;
2201         }
2202         memcpy(&wh, mtod(m, caddr_t), sizeof(wh));
2203         if (wh.i_fc[0] != (IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA))
2204                 return m;
2205         memcpy(&eh, mtod(m, caddr_t) + sizeof(wh), sizeof(eh));
2206         m_adj(m, sizeof(eh) - sizeof(*llc));
2207         if (ic->ic_opmode == IEEE80211_M_IBSS ||
2208             ic->ic_opmode == IEEE80211_M_AHDEMO)
2209                 IEEE80211_ADDR_COPY(wh.i_addr2, eh.ether_shost);
2210         memcpy(mtod(m, caddr_t), &wh, sizeof(wh));
2211         llc = (struct llc *)(mtod(m, caddr_t) + sizeof(wh));
2212         llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
2213         llc->llc_control = LLC_UI;
2214         llc->llc_snap.org_code[0] = 0;
2215         llc->llc_snap.org_code[1] = 0;
2216         llc->llc_snap.org_code[2] = 0;
2217         llc->llc_snap.ether_type = eh.ether_type;
2218         return m;
2219 }