awi.c revision 1.38 1 /* $NetBSD: awi.c,v 1.38 2001/09/18 23:19:08 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 (at) netbsd.org>.
83 * Then the driver module completely rewritten to support cards with DS phy
84 * and to support adhoc mode by Atsushi Onoe <onoe (at) netbsd.org>
85 */
86
87 #include "opt_inet.h"
88 #include "bpfilter.h"
89
90 #include <sys/param.h>
91 #include <sys/systm.h>
92 #include <sys/kernel.h>
93 #include <sys/mbuf.h>
94 #include <sys/malloc.h>
95 #include <sys/proc.h>
96 #include <sys/socket.h>
97 #include <sys/sockio.h>
98 #include <sys/errno.h>
99 #include <sys/device.h>
100
101 #include <net/if.h>
102 #include <net/if_dl.h>
103 #include <net/if_ether.h>
104 #include <net/if_media.h>
105 #include <net/if_llc.h>
106 #include <net/if_ieee80211.h>
107
108 #ifdef INET
109 #include <netinet/in.h>
110 #include <netinet/in_systm.h>
111 #ifdef __NetBSD__
112 #include <netinet/if_inarp.h>
113 #else
114 #include <netinet/if_ether.h>
115 #endif
116 #endif
117
118 #if NBPFILTER > 0
119 #include <net/bpf.h>
120 #endif
121
122 #include <machine/cpu.h>
123 #include <machine/bus.h>
124 #include <machine/intr.h>
125
126 #include <dev/ic/am79c930reg.h>
127 #include <dev/ic/am79c930var.h>
128 #include <dev/ic/awireg.h>
129 #include <dev/ic/awivar.h>
130
131 static int awi_init(struct ifnet *);
132 static void awi_stop(struct ifnet *, int);
133 static void awi_start(struct ifnet *);
134 static void awi_watchdog(struct ifnet *);
135 static int awi_ioctl(struct ifnet *, u_long, caddr_t);
136 static int awi_media_change(struct ifnet *);
137 static void awi_media_status(struct ifnet *, struct ifmediareq *);
138 static int awi_mode_init(struct awi_softc *);
139 static int awi_media_rate2opt(struct awi_softc *, int);
140 static int awi_media_opt2rate(struct awi_softc *, int);
141 static void awi_rx_int(struct awi_softc *);
142 static void awi_tx_int(struct awi_softc *);
143 static struct mbuf *awi_devget(struct awi_softc *, u_int32_t, u_int16_t);
144 static int awi_hw_init(struct awi_softc *);
145 static int awi_init_mibs(struct awi_softc *);
146 static int awi_chan_check(void *, u_char *);
147 static int awi_mib(struct awi_softc *, u_int8_t, u_int8_t, int);
148 static int awi_cmd(struct awi_softc *, u_int8_t, int);
149 static int awi_cmd_wait(struct awi_softc *);
150 static void awi_cmd_done(struct awi_softc *);
151 static int awi_next_txd(struct awi_softc *, int, u_int32_t *, u_int32_t *);
152 static int awi_lock(struct awi_softc *);
153 static void awi_unlock(struct awi_softc *);
154 static int awi_intr_lock(struct awi_softc *);
155 static void awi_intr_unlock(struct awi_softc *);
156 static int awi_newstate(void *, enum ieee80211_state);
157 static struct mbuf *awi_ether_encap(struct awi_softc *, struct mbuf *);
158 static struct mbuf *awi_ether_modcap(struct awi_softc *, struct mbuf *);
159
160 /* unalligned little endian access */
161 #define LE_READ_2(p) \
162 ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8))
163 #define LE_READ_4(p) \
164 ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8) | \
165 (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24))
166 #define LE_WRITE_2(p, v) \
167 ((((u_int8_t *)(p))[0] = (((u_int32_t)(v) ) & 0xff)), \
168 (((u_int8_t *)(p))[1] = (((u_int32_t)(v) >> 8) & 0xff)))
169 #define LE_WRITE_4(p, v) \
170 ((((u_int8_t *)(p))[0] = (((u_int32_t)(v) ) & 0xff)), \
171 (((u_int8_t *)(p))[1] = (((u_int32_t)(v) >> 8) & 0xff)), \
172 (((u_int8_t *)(p))[2] = (((u_int32_t)(v) >> 16) & 0xff)), \
173 (((u_int8_t *)(p))[3] = (((u_int32_t)(v) >> 24) & 0xff)))
174
175 struct awi_chanset awi_chanset[] = {
176 /* PHY type domain min max def */
177 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_JP, 6, 17, 6 },
178 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_ES, 0, 26, 1 },
179 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_FR, 0, 32, 1 },
180 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_US, 0, 77, 1 },
181 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_CA, 0, 77, 1 },
182 { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_EU, 0, 77, 1 },
183 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_JP, 14, 14, 14 },
184 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_ES, 10, 11, 10 },
185 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_FR, 10, 13, 10 },
186 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_US, 1, 11, 3 },
187 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_CA, 1, 11, 3 },
188 { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_EU, 1, 13, 3 },
189 { 0, 0 }
190 };
191
192 #ifdef AWI_DEBUG
193 int awi_debug;
194
195 #define DPRINTF(X) if (awi_debug) printf X
196 #define DPRINTF2(X) if (awi_debug > 1) printf X
197 #else
198 #define DPRINTF(X)
199 #define DPRINTF2(X)
200 #endif
201
202 int
203 awi_attach(struct awi_softc *sc)
204 {
205 struct ieee80211com *ic = &sc->sc_ic;
206 struct ifnet *ifp = &ic->ic_if;
207 int s, i, error, nrate;
208 int mword;
209 struct ifmediareq imr;
210
211 s = splnet();
212 sc->sc_busy = 1;
213 ic->ic_state = IEEE80211_S_INIT;
214 sc->sc_substate = AWI_ST_NONE;
215 if ((error = awi_hw_init(sc)) != 0) {
216 sc->sc_invalid = 1;
217 splx(s);
218 return error;
219 }
220 error = awi_init_mibs(sc);
221 if (error != 0) {
222 sc->sc_invalid = 1;
223 splx(s);
224 return error;
225 }
226 ifp->if_softc = sc;
227 ifp->if_flags =
228 IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST | IFF_NOTRAILERS;
229 ifp->if_ioctl = awi_ioctl;
230 ifp->if_start = awi_start;
231 ifp->if_init = awi_init;
232 ifp->if_stop = awi_stop;
233 ifp->if_watchdog = awi_watchdog;
234 IFQ_SET_READY(&ifp->if_snd);
235 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
236
237 ic->ic_flags = IEEE80211_F_HASWEP | IEEE80211_F_HASIBSS;
238 ic->ic_newstate = awi_newstate;
239 ic->ic_chancheck = awi_chan_check;
240 nrate = sc->sc_mib_phy.aSuprt_Data_Rates[1];
241 memcpy(ic->ic_sup_rates, sc->sc_mib_phy.aSuprt_Data_Rates + 2, nrate);
242 memcpy(ic->ic_myaddr, sc->sc_mib_addr.aMAC_Address, IEEE80211_ADDR_LEN);
243
244 printf("%s: IEEE802.11 %s %dMbps (firmware %s)\n",
245 sc->sc_dev.dv_xname,
246 sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH ? "FH" : "DS",
247 (ic->ic_sup_rates[nrate - 1] & IEEE80211_RATE_VAL) / 2,
248 sc->sc_banner);
249 printf("%s: 802.11 address: %s\n", sc->sc_dev.dv_xname,
250 ether_sprintf(ic->ic_myaddr));
251
252 if_attach(ifp);
253 ieee80211_ifattach(ifp);
254
255 ifmedia_init(&sc->sc_media, 0, awi_media_change, awi_media_status);
256 mword = IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO, 0, 0);
257 ifmedia_add(&sc->sc_media, mword, 0, NULL);
258 ifmedia_add(&sc->sc_media, mword | IFM_FLAG0, 0, NULL);
259 mword |= IFM_IEEE80211_ADHOC;
260 ifmedia_add(&sc->sc_media, mword, 0, NULL);
261 ifmedia_add(&sc->sc_media, mword | IFM_FLAG0, 0, NULL);
262 for (i = 0; i < nrate; i++) {
263 mword = awi_media_rate2opt(sc, ic->ic_sup_rates[i]);
264 if (mword == 0)
265 continue;
266 mword |= IFM_IEEE80211;
267 ifmedia_add(&sc->sc_media, mword, 0, NULL);
268 ifmedia_add(&sc->sc_media, mword | IFM_FLAG0, 0, NULL);
269 mword |= IFM_IEEE80211_ADHOC;
270 ifmedia_add(&sc->sc_media, mword, 0, NULL);
271 if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_FH)
272 ifmedia_add(&sc->sc_media, mword | IFM_FLAG0, 0, NULL);
273 }
274 awi_media_status(ifp, &imr);
275 ifmedia_set(&sc->sc_media, imr.ifm_active);
276
277 if ((sc->sc_sdhook = shutdownhook_establish(awi_shutdown, sc)) == NULL)
278 printf("%s: WARNING: unable to establish shutdown hook\n",
279 sc->sc_dev.dv_xname);
280 if ((sc->sc_powerhook = powerhook_establish(awi_power, sc)) == NULL)
281 printf("%s: WARNING: unable to establish power hook\n",
282 sc->sc_dev.dv_xname);
283 sc->sc_attached = 1;
284 splx(s);
285
286 /* ready to accept ioctl */
287 awi_unlock(sc);
288
289 return 0;
290 }
291
292 int
293 awi_detach(struct awi_softc *sc)
294 {
295 struct ifnet *ifp = &sc->sc_ic.ic_if;
296 int s;
297
298 if (!sc->sc_attached)
299 return 0;
300
301 s = splnet();
302 sc->sc_invalid = 1;
303 awi_stop(ifp, 1);
304 while (sc->sc_sleep_cnt > 0) {
305 wakeup(sc);
306 (void)tsleep(sc, PWAIT, "awidet", 1);
307 }
308 ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY);
309 ieee80211_ifdetach(ifp);
310 if_detach(ifp);
311 shutdownhook_disestablish(sc->sc_sdhook);
312 powerhook_disestablish(sc->sc_powerhook);
313 splx(s);
314 return 0;
315 }
316
317 int
318 awi_activate(struct device *self, enum devact act)
319 {
320 struct awi_softc *sc = (struct awi_softc *)self;
321 struct ifnet *ifp = &sc->sc_ic.ic_if;
322 int s, error = 0;
323
324 s = splnet();
325 switch (act) {
326 case DVACT_ACTIVATE:
327 error = EOPNOTSUPP;
328 break;
329 case DVACT_DEACTIVATE:
330 sc->sc_invalid = 1;
331 if_deactivate(ifp);
332 break;
333 }
334 splx(s);
335 return error;
336 }
337
338 void
339 awi_power(int why, void *arg)
340 {
341 struct awi_softc *sc = arg;
342 struct ifnet *ifp = &sc->sc_ic.ic_if;
343 int s;
344 int ocansleep;
345
346 DPRINTF(("awi_power: %d\n", why));
347 s = splnet();
348 ocansleep = sc->sc_cansleep;
349 sc->sc_cansleep = 0;
350 switch (why) {
351 case PWR_SUSPEND:
352 case PWR_STANDBY:
353 awi_stop(ifp, 1);
354 break;
355 case PWR_RESUME:
356 if (ifp->if_flags & IFF_UP) {
357 awi_init(ifp);
358 (void)awi_intr(sc); /* make sure */
359 }
360 break;
361 case PWR_SOFTSUSPEND:
362 case PWR_SOFTSTANDBY:
363 case PWR_SOFTRESUME:
364 break;
365 }
366 sc->sc_cansleep = ocansleep;
367 splx(s);
368 }
369
370 void
371 awi_shutdown(void *arg)
372 {
373 struct awi_softc *sc = arg;
374 struct ifnet *ifp = &sc->sc_ic.ic_if;
375
376 if (sc->sc_attached)
377 awi_stop(ifp, 1);
378 }
379
380 int
381 awi_intr(void *arg)
382 {
383 struct awi_softc *sc = arg;
384 u_int16_t status;
385 int error, handled = 0, ocansleep;
386 #ifdef AWI_DEBUG
387 static const char *intname[] = {
388 "CMD", "RX", "TX", "SCAN_CMPLT",
389 "CFP_START", "DTIM", "CFP_ENDING", "GROGGY",
390 "TXDATA", "TXBCAST", "TXPS", "TXCF",
391 "TXMGT", "#13", "RXDATA", "RXMGT"
392 };
393 #endif
394
395 if (!sc->sc_enabled || !sc->sc_enab_intr || sc->sc_invalid)
396 return 0;
397
398 am79c930_gcr_setbits(&sc->sc_chip,
399 AM79C930_GCR_DISPWDN | AM79C930_GCR_ECINT);
400 awi_write_1(sc, AWI_DIS_PWRDN, 1);
401 ocansleep = sc->sc_cansleep;
402 sc->sc_cansleep = 0;
403
404 for (;;) {
405 if ((error = awi_intr_lock(sc)) != 0)
406 break;
407 status = awi_read_1(sc, AWI_INTSTAT);
408 awi_write_1(sc, AWI_INTSTAT, 0);
409 awi_write_1(sc, AWI_INTSTAT, 0);
410 status |= awi_read_1(sc, AWI_INTSTAT2) << 8;
411 awi_write_1(sc, AWI_INTSTAT2, 0);
412 DELAY(10);
413 awi_intr_unlock(sc);
414 if (!sc->sc_cmd_inprog)
415 status &= ~AWI_INT_CMD; /* make sure */
416 if (status == 0)
417 break;
418 #ifdef AWI_DEBUG
419 if (awi_debug > 1) {
420 int i;
421
422 printf("awi_intr: status 0x%04x", status);
423 for (i = 0; i < sizeof(intname)/sizeof(intname[0]);
424 i++) {
425 if (status & (1 << i))
426 printf(" %s", intname[i]);
427 }
428 printf("\n");
429 }
430 #endif
431 handled = 1;
432 if (status & AWI_INT_RX)
433 awi_rx_int(sc);
434 if (status & AWI_INT_TX)
435 awi_tx_int(sc);
436 if (status & AWI_INT_CMD)
437 awi_cmd_done(sc);
438 if (status & AWI_INT_SCAN_CMPLT) {
439 if (sc->sc_ic.ic_state == IEEE80211_S_SCAN)
440 ieee80211_next_scan(&sc->sc_ic.ic_if);
441 }
442 }
443 sc->sc_cansleep = ocansleep;
444 am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_DISPWDN);
445 awi_write_1(sc, AWI_DIS_PWRDN, 0);
446 return handled;
447 }
448
449 static int
450 awi_init(struct ifnet *ifp)
451 {
452 struct awi_softc *sc = ifp->if_softc;
453 struct ieee80211com *ic = &sc->sc_ic;
454 struct ieee80211_bss *bs = &ic->ic_bss;
455 int i, error;
456
457 DPRINTF(("awi_init: enabled=%d\n", sc->sc_enabled));
458 if (sc->sc_enabled) {
459 awi_stop(ifp, 0);
460 } else {
461 if (sc->sc_enable)
462 (*sc->sc_enable)(sc);
463 sc->sc_enabled = 1;
464 if ((error = awi_hw_init(sc)) != 0) {
465 awi_stop(ifp, 1);
466 return error;
467 }
468 }
469 ic->ic_state = IEEE80211_S_INIT;
470
471 sc->sc_mib_local.Network_Mode =
472 (ic->ic_flags & IEEE80211_F_ADHOC) ? 0 : 1;
473
474 if ((error = awi_mode_init(sc)) != 0) {
475 DPRINTF(("awi_init: awi_mode_init failed %d\n", error));
476 awi_stop(ifp, 1);
477 return error;
478 }
479
480 /* start transmitter */
481 sc->sc_txdone = sc->sc_txnext = sc->sc_txbase;
482 awi_write_4(sc, sc->sc_txbase + AWI_TXD_START, 0);
483 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NEXT, 0);
484 awi_write_4(sc, sc->sc_txbase + AWI_TXD_LENGTH, 0);
485 awi_write_1(sc, sc->sc_txbase + AWI_TXD_RATE, 0);
486 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NDA, 0);
487 awi_write_4(sc, sc->sc_txbase + AWI_TXD_NRA, 0);
488 awi_write_1(sc, sc->sc_txbase + AWI_TXD_STATE, 0);
489 awi_write_4(sc, AWI_CA_TX_DATA, sc->sc_txbase);
490 awi_write_4(sc, AWI_CA_TX_MGT, 0);
491 awi_write_4(sc, AWI_CA_TX_BCAST, 0);
492 awi_write_4(sc, AWI_CA_TX_PS, 0);
493 awi_write_4(sc, AWI_CA_TX_CF, 0);
494 if ((error = awi_cmd(sc, AWI_CMD_INIT_TX, AWI_WAIT)) != 0) {
495 DPRINTF(("awi_init: failed to start transmitter: %d\n", error));
496 awi_stop(ifp, 1);
497 return error;
498 }
499
500 /* start receiver */
501 if ((error = awi_cmd(sc, AWI_CMD_INIT_RX, AWI_WAIT)) != 0) {
502 DPRINTF(("awi_init: failed to start receiver: %d\n", error));
503 awi_stop(ifp, 1);
504 return error;
505 }
506 sc->sc_rxdoff = awi_read_4(sc, AWI_CA_IRX_DATA_DESC);
507 sc->sc_rxmoff = awi_read_4(sc, AWI_CA_IRX_PS_DESC);
508
509 ifp->if_flags |= IFF_RUNNING;
510 ifp->if_flags &= ~IFF_OACTIVE;
511
512 if ((sc->sc_ic.ic_flags & IEEE80211_F_ADHOC) && sc->sc_no_bssid) {
513 bs->bs_chan = ic->ic_ibss_chan;
514 bs->bs_intval = ic->ic_lintval;
515 bs->bs_nrate = 0;
516 for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
517 if (ic->ic_sup_rates[i])
518 bs->bs_rates[bs->bs_nrate++] =
519 ic->ic_sup_rates[i];
520 }
521 memcpy(bs->bs_macaddr, ic->ic_myaddr, IEEE80211_ADDR_LEN);
522 memset(bs->bs_bssid, 0, IEEE80211_ADDR_LEN);
523 bs->bs_esslen = 0;
524 ic->ic_flags |= IEEE80211_F_SIBSS;
525 ic->ic_state = IEEE80211_S_SCAN; /*XXX*/
526 sc->sc_substate = AWI_ST_NONE;
527 ieee80211_new_state(&ic->ic_if, IEEE80211_S_RUN, -1);
528 } else {
529 bs->bs_chan = sc->sc_cur_chan;
530 ieee80211_new_state(&ic->ic_if, IEEE80211_S_SCAN, -1);
531 }
532 return 0;
533 }
534
535 static void
536 awi_stop(struct ifnet *ifp, int disable)
537 {
538 struct awi_softc *sc = ifp->if_softc;
539
540 if (!sc->sc_enabled)
541 return;
542
543 DPRINTF(("awi_stop(%d)\n", disable));
544
545 ieee80211_new_state(&sc->sc_ic.ic_if, IEEE80211_S_INIT, -1);
546
547 if (!sc->sc_invalid) {
548 if (sc->sc_cmd_inprog)
549 (void)awi_cmd_wait(sc);
550 (void)awi_cmd(sc, AWI_CMD_KILL_RX, AWI_WAIT);
551 sc->sc_cmd_inprog = AWI_CMD_FLUSH_TX;
552 awi_write_1(sc, AWI_CA_FTX_DATA, 1);
553 awi_write_1(sc, AWI_CA_FTX_MGT, 0);
554 awi_write_1(sc, AWI_CA_FTX_BCAST, 0);
555 awi_write_1(sc, AWI_CA_FTX_PS, 0);
556 awi_write_1(sc, AWI_CA_FTX_CF, 0);
557 (void)awi_cmd(sc, AWI_CMD_FLUSH_TX, AWI_WAIT);
558 }
559 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
560 ifp->if_timer = 0;
561 sc->sc_tx_timer = sc->sc_rx_timer = 0;
562 if (sc->sc_rxpend != NULL) {
563 m_freem(sc->sc_rxpend);
564 sc->sc_rxpend = NULL;
565 }
566 IFQ_PURGE(&ifp->if_snd);
567
568 if (disable) {
569 if (sc->sc_disable)
570 (*sc->sc_disable)(sc);
571 sc->sc_enabled = 0;
572 }
573 }
574
575 static void
576 awi_start(struct ifnet *ifp)
577 {
578 struct awi_softc *sc = ifp->if_softc;
579 struct ieee80211com *ic = &sc->sc_ic;
580 struct mbuf *m, *m0;
581 int len;
582 u_int32_t txd, frame, ntxd;
583 u_int8_t rate;
584
585 if (!sc->sc_enabled || sc->sc_invalid)
586 return;
587
588 for (;;) {
589 txd = sc->sc_txnext;
590 IF_POLL(&ic->ic_mgtq, m0);
591 if (m0 != NULL) {
592 if (awi_next_txd(sc, m0->m_pkthdr.len, &frame, &ntxd)) {
593 ifp->if_flags |= IFF_OACTIVE;
594 break;
595 }
596 IF_DEQUEUE(&ic->ic_mgtq, m0);
597 } else {
598 if (ic->ic_state != IEEE80211_S_RUN)
599 break;
600 IFQ_POLL(&ifp->if_snd, m0);
601 if (m0 == NULL)
602 break;
603 /*
604 * Need to calculate the real length to determine
605 * if the transmit buffer has a room for the packet.
606 */
607 len = m0->m_pkthdr.len + sizeof(struct ieee80211_frame);
608 if (!(ifp->if_flags & IFF_LINK0) && !sc->sc_adhoc_ap)
609 len += sizeof(struct llc) -
610 sizeof(struct ether_header);
611 if (ic->ic_flags & IEEE80211_F_WEPON)
612 len += IEEE80211_WEP_IVLEN +
613 IEEE80211_WEP_KIDLEN + IEEE80211_WEP_CRCLEN;
614 if (awi_next_txd(sc, len, &frame, &ntxd)) {
615 ifp->if_flags |= IFF_OACTIVE;
616 break;
617 }
618 IFQ_DEQUEUE(&ifp->if_snd, m0);
619 ifp->if_opackets++;
620 #if NBPFILTER > 0
621 if (ifp->if_bpf)
622 bpf_mtap(ifp->if_bpf, m0);
623 #endif
624 if ((ifp->if_flags & IFF_LINK0) || sc->sc_adhoc_ap)
625 m0 = awi_ether_encap(sc, m0);
626 else
627 m0 = ieee80211_encap(ifp, m0);
628 if ((ic->ic_flags & IEEE80211_F_WEPON) && m0 != NULL)
629 m0 = ieee80211_wep_crypt(ifp, m0, 1);
630 if (m0 == NULL) {
631 ifp->if_oerrors++;
632 continue;
633 }
634 #ifdef DIAGNOSTIC
635 if (m0->m_pkthdr.len != len) {
636 printf("%s: length %d should be %d\n",
637 ifp->if_xname, m0->m_pkthdr.len, len);
638 m_freem(m0);
639 ifp->if_oerrors++;
640 continue;
641 }
642 #endif
643 }
644
645 if ((ifp->if_flags & IFF_DEBUG) && (ifp->if_flags & IFF_LINK2))
646 ieee80211_dump_pkt(m0->m_data, m0->m_len,
647 ic->ic_bss.bs_rates[ic->ic_bss.bs_txrate] &
648 IEEE80211_RATE_VAL, -1);
649
650 for (m = m0, len = 0; m != NULL; m = m->m_next) {
651 awi_write_bytes(sc, frame + len, mtod(m, u_int8_t *),
652 m->m_len);
653 len += m->m_len;
654 }
655 m_freem(m0);
656 rate = (ic->ic_bss.bs_rates[ic->ic_bss.bs_txrate] &
657 IEEE80211_RATE_VAL) * 5;
658 awi_write_1(sc, ntxd + AWI_TXD_STATE, 0);
659 awi_write_4(sc, txd + AWI_TXD_START, frame);
660 awi_write_4(sc, txd + AWI_TXD_NEXT, ntxd);
661 awi_write_4(sc, txd + AWI_TXD_LENGTH, len);
662 awi_write_1(sc, txd + AWI_TXD_RATE, rate);
663 awi_write_4(sc, txd + AWI_TXD_NDA, 0);
664 awi_write_4(sc, txd + AWI_TXD_NRA, 0);
665 awi_write_1(sc, txd + AWI_TXD_STATE, AWI_TXD_ST_OWN);
666 sc->sc_txnext = ntxd;
667
668 sc->sc_tx_timer = 5;
669 ifp->if_timer = 1;
670 }
671 }
672
673 static void
674 awi_watchdog(struct ifnet *ifp)
675 {
676 struct awi_softc *sc = ifp->if_softc;
677 u_int32_t prevdone;
678 int ocansleep;
679
680 ifp->if_timer = 0;
681 if (!sc->sc_enabled || sc->sc_invalid)
682 return;
683
684 ocansleep = sc->sc_cansleep;
685 sc->sc_cansleep = 0;
686 if (sc->sc_tx_timer) {
687 if (--sc->sc_tx_timer == 0) {
688 printf("%s: device timeout\n", ifp->if_xname);
689 prevdone = sc->sc_txdone;
690 awi_tx_int(sc);
691 if (sc->sc_txdone == prevdone) {
692 ifp->if_oerrors++;
693 awi_init(ifp);
694 goto out;
695 }
696 }
697 ifp->if_timer = 1;
698 }
699 if (sc->sc_rx_timer) {
700 if (--sc->sc_rx_timer == 0) {
701 if (sc->sc_ic.ic_state == IEEE80211_S_RUN) {
702 ieee80211_new_state(ifp, IEEE80211_S_SCAN, -1);
703 goto out;
704 }
705 } else
706 ifp->if_timer = 1;
707 }
708 /* TODO: rate control */
709 ieee80211_watchdog(ifp);
710 out:
711 sc->sc_cansleep = ocansleep;
712 }
713
714 static int
715 awi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
716 {
717 struct awi_softc *sc = ifp->if_softc;
718 struct ifreq *ifr = (struct ifreq *)data;
719 int s, error;
720
721 s = splnet();
722 /* serialize ioctl, since we may sleep */
723 if ((error = awi_lock(sc)) != 0)
724 goto cantlock;
725
726 switch (cmd) {
727 case SIOCSIFFLAGS:
728 if (ifp->if_flags & IFF_UP) {
729 if (sc->sc_enabled) {
730 /*
731 * To avoid rescanning another access point,
732 * do not call awi_init() here. Instead,
733 * only reflect promisc mode settings.
734 */
735 error = awi_mode_init(sc);
736 } else
737 error = awi_init(ifp);
738 } else if (sc->sc_enabled)
739 awi_stop(ifp, 1);
740 break;
741 case SIOCSIFMEDIA:
742 case SIOCGIFMEDIA:
743 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
744 break;
745 case SIOCADDMULTI:
746 case SIOCDELMULTI:
747 error = (cmd == SIOCADDMULTI) ?
748 ether_addmulti(ifr, &sc->sc_ic.ic_ec) :
749 ether_delmulti(ifr, &sc->sc_ic.ic_ec);
750 if (error == ENETRESET) {
751 /* do not rescan */
752 if (sc->sc_enabled)
753 error = awi_mode_init(sc);
754 else
755 error = 0;
756 }
757 break;
758 default:
759 error = ieee80211_ioctl(ifp, cmd, data);
760 if (error == ENETRESET) {
761 if (sc->sc_enabled)
762 error = awi_init(ifp);
763 else
764 error = 0;
765 }
766 break;
767 }
768 awi_unlock(sc);
769 cantlock:
770 splx(s);
771 return error;
772 }
773
774 /*
775 * Called from ifmedia_ioctl via awi_ioctl with lock obtained.
776 */
777 static int
778 awi_media_change(struct ifnet *ifp)
779 {
780 struct awi_softc *sc = ifp->if_softc;
781 struct ieee80211com *ic = &sc->sc_ic;
782 struct ifmedia_entry *ime;
783 int i, rate, error = 0;
784
785 ime = sc->sc_media.ifm_cur;
786 if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) {
787 ic->ic_fixed_rate = -1;
788 } else {
789 rate = awi_media_opt2rate(sc, ime->ifm_media);
790 if (rate == 0)
791 return EINVAL;
792 for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
793 if ((ic->ic_sup_rates[i] & IEEE80211_RATE_VAL) == rate)
794 break;
795 }
796 if (i == IEEE80211_RATE_SIZE)
797 return EINVAL;
798 ic->ic_fixed_rate = i;
799 }
800
801 /*
802 * ADHOC,-FLAG0 ADHOC, !no_bssid, !adhoc_ap IBSS
803 * ADHOC, FLAG0 ADHOC no_bssid, !adhoc_ap WaveLAN adhoc
804 * -ADHOC,-FLAG0 ~ADHOC, !no_bssid, !adhoc_ap Infra
805 * -ADHOC, FLAG0 ADHOC, !no_bssid, adhoc_ap Melco old AP
806 * also LINK0
807 */
808 if (ime->ifm_media & IFM_IEEE80211_ADHOC) {
809 if ((ic->ic_flags & IEEE80211_F_ADHOC) == 0) {
810 ic->ic_flags |= IEEE80211_F_ADHOC;
811 error = ENETRESET;
812 }
813 ic->ic_flags |= IEEE80211_F_IBSSON;
814 if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_FH &&
815 (ime->ifm_media & IFM_FLAG0)) {
816 if (sc->sc_no_bssid == 0) {
817 sc->sc_no_bssid = 1;
818 error = ENETRESET;
819 }
820 } else {
821 if (sc->sc_no_bssid) {
822 sc->sc_no_bssid = 0;
823 error = ENETRESET;
824 }
825 }
826 if (sc->sc_adhoc_ap) {
827 sc->sc_adhoc_ap = 0;
828 error = ENETRESET;
829 }
830 } else {
831 ic->ic_flags &= ~IEEE80211_F_IBSSON;
832 if (sc->sc_no_bssid) {
833 sc->sc_no_bssid = 0;
834 error = ENETRESET;
835 }
836 if (ime->ifm_media & IFM_FLAG0) {
837 if ((ic->ic_flags & IEEE80211_F_ADHOC) == 0) {
838 ic->ic_flags |= IEEE80211_F_ADHOC;
839 error = ENETRESET;
840 }
841 if (!sc->sc_adhoc_ap) {
842 sc->sc_adhoc_ap = 1;
843 error = ENETRESET;
844 }
845 } else {
846 if (ic->ic_flags & IEEE80211_F_ADHOC) {
847 ic->ic_flags &= ~IEEE80211_F_ADHOC;
848 error = ENETRESET;
849 }
850 if (sc->sc_adhoc_ap) {
851 sc->sc_adhoc_ap = 0;
852 error = ENETRESET;
853 }
854 }
855 }
856 if (error == ENETRESET) {
857 if (sc->sc_enabled)
858 error = awi_init(ifp);
859 else
860 error = 0;
861 }
862 return error;
863 }
864
865 static void
866 awi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
867 {
868 struct awi_softc *sc = ifp->if_softc;
869 struct ieee80211com *ic = &sc->sc_ic;
870 int rate;
871
872 imr->ifm_status = IFM_AVALID;
873 if (ic->ic_state == IEEE80211_S_RUN)
874 imr->ifm_status |= IFM_ACTIVE;
875 imr->ifm_active = IFM_IEEE80211;
876 if (ic->ic_state == IEEE80211_S_RUN)
877 rate = ic->ic_bss.bs_rates[ic->ic_bss.bs_txrate] &
878 IEEE80211_RATE_VAL;
879 else {
880 if (ic->ic_fixed_rate == -1)
881 rate = 0;
882 else
883 rate = ic->ic_sup_rates[ic->ic_fixed_rate] &
884 IEEE80211_RATE_VAL;
885 }
886 imr->ifm_active |= awi_media_rate2opt(sc, rate);
887 if (ic->ic_flags & IEEE80211_F_ADHOC) {
888 if (sc->sc_adhoc_ap)
889 imr->ifm_active |= IFM_FLAG0;
890 else {
891 imr->ifm_active |= IFM_IEEE80211_ADHOC;
892 if (sc->sc_no_bssid)
893 imr->ifm_active |= IFM_FLAG0;
894 }
895 }
896 }
897
898 static int
899 awi_mode_init(struct awi_softc *sc)
900 {
901 struct ifnet *ifp = &sc->sc_ic.ic_if;
902 int n, error;
903 struct ether_multi *enm;
904 struct ether_multistep step;
905
906 /* reinitialize muticast filter */
907 n = 0;
908 sc->sc_mib_local.Accept_All_Multicast_Dis = 0;
909 if (ifp->if_flags & IFF_PROMISC) {
910 sc->sc_mib_mac.aPromiscuous_Enable = 1;
911 goto set_mib;
912 }
913 sc->sc_mib_mac.aPromiscuous_Enable = 0;
914 ETHER_FIRST_MULTI(step, &sc->sc_ic.ic_ec, enm);
915 while (enm != NULL) {
916 if (n == AWI_GROUP_ADDR_SIZE ||
917 memcmp(enm->enm_addrlo, enm->enm_addrhi, IEEE80211_ADDR_LEN)
918 != 0)
919 goto set_mib;
920 memcpy(sc->sc_mib_addr.aGroup_Addresses[n], enm->enm_addrlo,
921 IEEE80211_ADDR_LEN);
922 n++;
923 ETHER_NEXT_MULTI(step, enm);
924 }
925 for (; n < AWI_GROUP_ADDR_SIZE; n++)
926 memset(sc->sc_mib_addr.aGroup_Addresses[n], 0, IEEE80211_ADDR_LEN);
927 sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
928
929 set_mib:
930 if (sc->sc_mib_local.Accept_All_Multicast_Dis)
931 ifp->if_flags &= ~IFF_ALLMULTI;
932 else
933 ifp->if_flags |= IFF_ALLMULTI;
934 sc->sc_mib_mgt.Wep_Required =
935 (sc->sc_ic.ic_flags & IEEE80211_F_WEPON) ? 1 : 0;
936
937 if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) ||
938 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_ADDR, AWI_WAIT)) ||
939 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MAC, AWI_WAIT)) ||
940 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT, AWI_WAIT)) ||
941 (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_PHY, AWI_WAIT))) {
942 DPRINTF(("awi_mode_init: MIB set failed: %d\n", error));
943 return error;
944 }
945 return 0;
946 }
947
948 /* XXX should be moved to if_ieee80211subr.c ? */
949 static int
950 awi_media_rate2opt(struct awi_softc *sc, int rate)
951 {
952 int mword;
953
954 mword = 0;
955 switch (rate & IEEE80211_RATE_VAL) {
956 case 2:
957 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
958 mword = IFM_IEEE80211_FH1;
959 else
960 mword = IFM_IEEE80211_DS1;
961 break;
962 case 4:
963 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
964 mword = IFM_IEEE80211_FH2;
965 else
966 mword = IFM_IEEE80211_DS2;
967 break;
968 case 11:
969 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_DS)
970 mword = IFM_IEEE80211_DS5;
971 break;
972 case 22:
973 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_DS)
974 mword = IFM_IEEE80211_DS11;
975 break;
976 }
977 return mword;
978 }
979
980 static int
981 awi_media_opt2rate(struct awi_softc *sc, int opt)
982 {
983 int rate;
984
985 rate = 0;
986 switch (IFM_SUBTYPE(opt)) {
987 case IFM_IEEE80211_FH1:
988 case IFM_IEEE80211_FH2:
989 if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_FH)
990 return 0;
991 break;
992 case IFM_IEEE80211_DS1:
993 case IFM_IEEE80211_DS2:
994 case IFM_IEEE80211_DS5:
995 case IFM_IEEE80211_DS11:
996 if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_DS)
997 return 0;
998 break;
999 }
1000
1001 switch (IFM_SUBTYPE(opt)) {
1002 case IFM_IEEE80211_FH1:
1003 case IFM_IEEE80211_DS1:
1004 rate = 2;
1005 break;
1006 case IFM_IEEE80211_FH2:
1007 case IFM_IEEE80211_DS2:
1008 rate = 4;
1009 break;
1010 case IFM_IEEE80211_DS5:
1011 rate = 11;
1012 break;
1013 case IFM_IEEE80211_DS11:
1014 rate = 22;
1015 break;
1016 }
1017 return rate;
1018 }
1019
1020 static void
1021 awi_rx_int(struct awi_softc *sc)
1022 {
1023 struct ifnet *ifp = &sc->sc_ic.ic_if;
1024 u_int8_t state, rate, rssi;
1025 u_int16_t len;
1026 u_int32_t frame, next, timoff, rxoff;
1027 struct mbuf *m;
1028
1029 rxoff = sc->sc_rxdoff;
1030 for (;;) {
1031 state = awi_read_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE);
1032 if (state & AWI_RXD_ST_OWN)
1033 break;
1034 if (!(state & AWI_RXD_ST_CONSUMED)) {
1035 if (state & AWI_RXD_ST_RXERROR) {
1036 ifp->if_ierrors++;
1037 goto rx_next;
1038 }
1039 len = awi_read_2(sc, rxoff + AWI_RXD_LEN);
1040 rate = awi_read_1(sc, rxoff + AWI_RXD_RATE);
1041 rssi = awi_read_1(sc, rxoff + AWI_RXD_RSSI);
1042 frame = awi_read_4(sc, rxoff + AWI_RXD_START_FRAME) &
1043 0x7fff;
1044 timoff = awi_read_4(sc, rxoff + AWI_RXD_LOCALTIME);
1045 m = awi_devget(sc, frame, len);
1046 if (m == NULL) {
1047 ifp->if_ierrors++;
1048 goto rx_next;
1049 }
1050 if (state & AWI_RXD_ST_LF) {
1051 /* TODO check my bss */
1052 if (!(sc->sc_ic.ic_flags & IEEE80211_F_SIBSS) &&
1053 sc->sc_ic.ic_state == IEEE80211_S_RUN) {
1054 sc->sc_rx_timer = 10;
1055 ifp->if_timer = 1;
1056 }
1057 if ((ifp->if_flags & IFF_DEBUG) &&
1058 (ifp->if_flags & IFF_LINK2))
1059 ieee80211_dump_pkt(m->m_data, m->m_len,
1060 rate / 5, rssi);
1061 if ((ifp->if_flags & IFF_LINK0) ||
1062 sc->sc_adhoc_ap)
1063 m = awi_ether_modcap(sc, m);
1064 if (m == NULL)
1065 ifp->if_ierrors++;
1066 else
1067 ieee80211_input(ifp, m, rssi, timoff);
1068 } else
1069 sc->sc_rxpend = m;
1070 rx_next:
1071 state |= AWI_RXD_ST_CONSUMED;
1072 awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1073 }
1074 next = awi_read_4(sc, rxoff + AWI_RXD_NEXT);
1075 if (next & AWI_RXD_NEXT_LAST)
1076 break;
1077 /* make sure the next pointer is correct */
1078 if (next != awi_read_4(sc, rxoff + AWI_RXD_NEXT))
1079 break;
1080 state |= AWI_RXD_ST_OWN;
1081 awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1082 rxoff = next & 0x7fff;
1083 }
1084 sc->sc_rxdoff = rxoff;
1085 }
1086
1087 static void
1088 awi_tx_int(struct awi_softc *sc)
1089 {
1090 struct ifnet *ifp = &sc->sc_ic.ic_if;
1091 u_int8_t flags;
1092
1093 while (sc->sc_txdone != sc->sc_txnext) {
1094 flags = awi_read_1(sc, sc->sc_txdone + AWI_TXD_STATE);
1095 if ((flags & AWI_TXD_ST_OWN) || !(flags & AWI_TXD_ST_DONE))
1096 break;
1097 if (flags & AWI_TXD_ST_ERROR)
1098 ifp->if_oerrors++;
1099 sc->sc_txdone = awi_read_4(sc, sc->sc_txdone + AWI_TXD_NEXT) &
1100 0x7fff;
1101 }
1102 DPRINTF2(("awi_txint: txdone %d txnext %d txbase %d txend %d\n",
1103 sc->sc_txdone, sc->sc_txnext, sc->sc_txbase, sc->sc_txend));
1104 sc->sc_tx_timer = 0;
1105 ifp->if_flags &= ~IFF_OACTIVE;
1106 awi_start(ifp);
1107 }
1108
1109 static struct mbuf *
1110 awi_devget(struct awi_softc *sc, u_int32_t off, u_int16_t len)
1111 {
1112 struct ifnet *ifp = &sc->sc_ic.ic_if;
1113 struct mbuf *m;
1114 struct mbuf *top, **mp;
1115 u_int tlen;
1116
1117 top = sc->sc_rxpend;
1118 mp = ⊤
1119 if (top != NULL) {
1120 sc->sc_rxpend = NULL;
1121 top->m_pkthdr.len += len;
1122 m = top;
1123 while (*mp != NULL) {
1124 m = *mp;
1125 mp = &m->m_next;
1126 }
1127 if (m->m_flags & M_EXT)
1128 tlen = m->m_ext.ext_size;
1129 else if (m->m_flags & M_PKTHDR)
1130 tlen = MHLEN;
1131 else
1132 tlen = MLEN;
1133 tlen -= m->m_len;
1134 if (tlen > len)
1135 tlen = len;
1136 awi_read_bytes(sc, off, mtod(m, u_int8_t *) + m->m_len, tlen);
1137 off += tlen;
1138 len -= tlen;
1139 }
1140
1141 while (len > 0) {
1142 if (top == NULL) {
1143 MGETHDR(m, M_DONTWAIT, MT_DATA);
1144 if (m == NULL)
1145 return NULL;
1146 m->m_pkthdr.rcvif = ifp;
1147 m->m_pkthdr.len = len;
1148 m->m_len = MHLEN;
1149 m->m_flags |= M_HASFCS;
1150 } else {
1151 MGET(m, M_DONTWAIT, MT_DATA);
1152 if (m == NULL) {
1153 m_freem(top);
1154 return NULL;
1155 }
1156 m->m_len = MLEN;
1157 }
1158 if (len >= MINCLSIZE) {
1159 MCLGET(m, M_DONTWAIT);
1160 if (m->m_flags & M_EXT)
1161 m->m_len = m->m_ext.ext_size;
1162 }
1163 if (top == NULL) {
1164 int hdrlen = sizeof(struct ieee80211_frame) +
1165 sizeof(struct llc);
1166 caddr_t newdata = (caddr_t)
1167 ALIGN(m->m_data + hdrlen) - hdrlen;
1168 m->m_len -= newdata - m->m_data;
1169 m->m_data = newdata;
1170 }
1171 if (m->m_len > len)
1172 m->m_len = len;
1173 awi_read_bytes(sc, off, mtod(m, u_int8_t *), m->m_len);
1174 off += m->m_len;
1175 len -= m->m_len;
1176 *mp = m;
1177 mp = &m->m_next;
1178 }
1179 return top;
1180 }
1181
1182 /*
1183 * Initialize hardware and start firmware to accept commands.
1184 * Called everytime after power on firmware.
1185 */
1186
1187 static int
1188 awi_hw_init(struct awi_softc *sc)
1189 {
1190 u_int8_t status;
1191 u_int16_t intmask;
1192 int i, error;
1193
1194 sc->sc_enab_intr = 0;
1195 sc->sc_invalid = 0; /* XXX: really? */
1196 awi_drvstate(sc, AWI_DRV_RESET);
1197
1198 /* reset firmware */
1199 am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1200 DELAY(100);
1201 awi_write_1(sc, AWI_SELFTEST, 0);
1202 awi_write_1(sc, AWI_CMD, 0);
1203 awi_write_1(sc, AWI_BANNER, 0);
1204 am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1205 DELAY(100);
1206
1207 /* wait for selftest completion */
1208 for (i = 0; ; i++) {
1209 if (i >= AWI_SELFTEST_TIMEOUT*hz/1000) {
1210 printf("%s: failed to complete selftest (timeout)\n",
1211 sc->sc_dev.dv_xname);
1212 return ENXIO;
1213 }
1214 status = awi_read_1(sc, AWI_SELFTEST);
1215 if ((status & 0xf0) == 0xf0)
1216 break;
1217 if (sc->sc_cansleep) {
1218 sc->sc_sleep_cnt++;
1219 (void)tsleep(sc, PWAIT, "awitst", 1);
1220 sc->sc_sleep_cnt--;
1221 } else {
1222 DELAY(1000*1000/hz);
1223 }
1224 }
1225 if (status != AWI_SELFTEST_PASSED) {
1226 printf("%s: failed to complete selftest (code %x)\n",
1227 sc->sc_dev.dv_xname, status);
1228 return ENXIO;
1229 }
1230
1231 /* check banner to confirm firmware write it */
1232 awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
1233 if (memcmp(sc->sc_banner, "PCnetMobile:", 12) != 0) {
1234 printf("%s: failed to complete selftest (bad banner)\n",
1235 sc->sc_dev.dv_xname);
1236 for (i = 0; i < AWI_BANNER_LEN; i++)
1237 printf("%s%02x", i ? ":" : "\t", sc->sc_banner[i]);
1238 printf("\n");
1239 return ENXIO;
1240 }
1241
1242 /* initializing interrupt */
1243 sc->sc_enab_intr = 1;
1244 error = awi_intr_lock(sc);
1245 if (error)
1246 return error;
1247 intmask = AWI_INT_GROGGY | AWI_INT_SCAN_CMPLT |
1248 AWI_INT_TX | AWI_INT_RX | AWI_INT_CMD;
1249 awi_write_1(sc, AWI_INTMASK, ~intmask & 0xff);
1250 awi_write_1(sc, AWI_INTMASK2, 0);
1251 awi_write_1(sc, AWI_INTSTAT, 0);
1252 awi_write_1(sc, AWI_INTSTAT2, 0);
1253 awi_intr_unlock(sc);
1254 am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_ENECINT);
1255
1256 /* issuing interface test command */
1257 error = awi_cmd(sc, AWI_CMD_NOP, AWI_WAIT);
1258 if (error) {
1259 printf("%s: failed to complete selftest", sc->sc_dev.dv_xname);
1260 if (error == ENXIO)
1261 printf(" (no hardware)\n");
1262 else if (error != EWOULDBLOCK)
1263 printf(" (error %d)\n", error);
1264 else if (sc->sc_cansleep)
1265 printf(" (lost interrupt)\n");
1266 else
1267 printf(" (command timeout)\n");
1268 }
1269 return error;
1270 }
1271
1272 /*
1273 * Extract the factory default MIB value from firmware and assign the driver
1274 * default value.
1275 * Called once at attaching the interface.
1276 */
1277
1278 static int
1279 awi_init_mibs(struct awi_softc *sc)
1280 {
1281 int i, error;
1282 struct awi_chanset *cs;
1283
1284 if ((error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) ||
1285 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_ADDR, AWI_WAIT)) ||
1286 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MAC, AWI_WAIT)) ||
1287 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MGT, AWI_WAIT)) ||
1288 (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_PHY, AWI_WAIT))) {
1289 printf("%s: failed to get default mib value (error %d)\n",
1290 sc->sc_dev.dv_xname, error);
1291 return error;
1292 }
1293
1294 memset(&sc->sc_ic.ic_chan_avail, 0, sizeof(sc->sc_ic.ic_chan_avail));
1295 for (cs = awi_chanset; ; cs++) {
1296 if (cs->cs_type == 0) {
1297 printf("%s: failed to set available channel\n",
1298 sc->sc_dev.dv_xname);
1299 return ENXIO;
1300 }
1301 if (cs->cs_type == sc->sc_mib_phy.IEEE_PHY_Type &&
1302 cs->cs_region == sc->sc_mib_phy.aCurrent_Reg_Domain)
1303 break;
1304 }
1305 for (i = cs->cs_min; i <= cs->cs_max; i++)
1306 setbit(sc->sc_ic.ic_chan_avail, i);
1307 sc->sc_cur_chan = cs->cs_def;
1308
1309 memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
1310 sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
1311 sc->sc_mib_local.Fragmentation_Dis = 1;
1312 sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
1313 sc->sc_mib_local.Power_Saving_Mode_Dis = 1;
1314
1315 /* allocate buffers */
1316 sc->sc_txbase = AWI_BUFFERS;
1317 sc->sc_txend = sc->sc_txbase +
1318 (AWI_TXD_SIZE + sizeof(struct ieee80211_frame) +
1319 sizeof(struct ether_header) + ETHERMTU) * AWI_NTXBUFS;
1320 LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Offset, sc->sc_txbase);
1321 LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Size,
1322 sc->sc_txend - sc->sc_txbase);
1323 LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Offset, sc->sc_txend);
1324 LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Size,
1325 AWI_BUFFERS_END - sc->sc_txend);
1326 sc->sc_mib_local.Network_Mode = 1;
1327 sc->sc_mib_local.Acting_as_AP = 0;
1328 return 0;
1329 }
1330
1331 static int
1332 awi_chan_check(void *arg, u_char *chanreq)
1333 {
1334 struct awi_softc *sc = arg;
1335 int i;
1336 struct awi_chanset *cs;
1337 u_char chanlist[(IEEE80211_CHAN_MAX+1)/NBBY];
1338
1339 for (cs = awi_chanset; cs->cs_type != 0; cs++) {
1340 if (cs->cs_type != sc->sc_mib_phy.IEEE_PHY_Type)
1341 continue;
1342 memset(chanlist, 0, sizeof(chanlist));
1343 for (i = 0; ; i++) {
1344 if (i == IEEE80211_CHAN_MAX) {
1345 sc->sc_mib_phy.aCurrent_Reg_Domain =
1346 cs->cs_region;
1347 memcpy(sc->sc_ic.ic_chan_avail, chanlist,
1348 sizeof(sc->sc_ic.ic_chan_avail));
1349 sc->sc_cur_chan = cs->cs_def;
1350 return 0;
1351 }
1352 if (i >= cs->cs_min && i <= cs->cs_max)
1353 setbit(chanlist, i);
1354 else if (isset(chanreq, i))
1355 break;
1356 }
1357 }
1358 return EINVAL;
1359 }
1360
1361 static int
1362 awi_mib(struct awi_softc *sc, u_int8_t cmd, u_int8_t mib, int wflag)
1363 {
1364 int error;
1365 u_int8_t size, *ptr;
1366
1367 switch (mib) {
1368 case AWI_MIB_LOCAL:
1369 ptr = (u_int8_t *)&sc->sc_mib_local;
1370 size = sizeof(sc->sc_mib_local);
1371 break;
1372 case AWI_MIB_ADDR:
1373 ptr = (u_int8_t *)&sc->sc_mib_addr;
1374 size = sizeof(sc->sc_mib_addr);
1375 break;
1376 case AWI_MIB_MAC:
1377 ptr = (u_int8_t *)&sc->sc_mib_mac;
1378 size = sizeof(sc->sc_mib_mac);
1379 break;
1380 case AWI_MIB_STAT:
1381 ptr = (u_int8_t *)&sc->sc_mib_stat;
1382 size = sizeof(sc->sc_mib_stat);
1383 break;
1384 case AWI_MIB_MGT:
1385 ptr = (u_int8_t *)&sc->sc_mib_mgt;
1386 size = sizeof(sc->sc_mib_mgt);
1387 break;
1388 case AWI_MIB_PHY:
1389 ptr = (u_int8_t *)&sc->sc_mib_phy;
1390 size = sizeof(sc->sc_mib_phy);
1391 break;
1392 default:
1393 return EINVAL;
1394 }
1395 if (sc->sc_cmd_inprog) {
1396 if ((error = awi_cmd_wait(sc)) != 0) {
1397 if (error == EWOULDBLOCK)
1398 DPRINTF(("awi_mib: cmd %d inprog",
1399 sc->sc_cmd_inprog));
1400 return error;
1401 }
1402 }
1403 sc->sc_cmd_inprog = cmd;
1404 if (cmd == AWI_CMD_SET_MIB)
1405 awi_write_bytes(sc, AWI_CA_MIB_DATA, ptr, size);
1406 awi_write_1(sc, AWI_CA_MIB_TYPE, mib);
1407 awi_write_1(sc, AWI_CA_MIB_SIZE, size);
1408 awi_write_1(sc, AWI_CA_MIB_INDEX, 0);
1409 if ((error = awi_cmd(sc, cmd, wflag)) != 0)
1410 return error;
1411 if (cmd == AWI_CMD_GET_MIB) {
1412 awi_read_bytes(sc, AWI_CA_MIB_DATA, ptr, size);
1413 #ifdef AWI_DEBUG
1414 if (awi_debug) {
1415 int i;
1416
1417 printf("awi_mib: #%d:", mib);
1418 for (i = 0; i < size; i++)
1419 printf(" %02x", ptr[i]);
1420 printf("\n");
1421 }
1422 #endif
1423 }
1424 return 0;
1425 }
1426
1427 static int
1428 awi_cmd(struct awi_softc *sc, u_int8_t cmd, int wflag)
1429 {
1430 u_int8_t status;
1431 int error = 0;
1432 #ifdef AWI_DEBUG
1433 static const char *cmdname[] = {
1434 "IDLE", "NOP", "SET_MIB", "INIT_TX", "FLUSH_TX", "INIT_RX",
1435 "KILL_RX", "SLEEP", "WAKE", "GET_MIB", "SCAN", "SYNC", "RESUME"
1436 };
1437 #endif
1438
1439 #ifdef AWI_DEBUG
1440 if (awi_debug > 1) {
1441 if (cmd >= sizeof(cmdname)/sizeof(cmdname[0]))
1442 printf("awi_cmd: #%d", cmd);
1443 else
1444 printf("awi_cmd: %s", cmdname[cmd]);
1445 printf(" %s\n", wflag == AWI_NOWAIT ? "nowait" : "wait");
1446 }
1447 #endif
1448 sc->sc_cmd_inprog = cmd;
1449 awi_write_1(sc, AWI_CMD_STATUS, AWI_STAT_IDLE);
1450 awi_write_1(sc, AWI_CMD, cmd);
1451 if (wflag == AWI_NOWAIT)
1452 return EINPROGRESS;
1453 if ((error = awi_cmd_wait(sc)) != 0)
1454 return error;
1455 status = awi_read_1(sc, AWI_CMD_STATUS);
1456 awi_write_1(sc, AWI_CMD, 0);
1457 switch (status) {
1458 case AWI_STAT_OK:
1459 break;
1460 case AWI_STAT_BADPARM:
1461 return EINVAL;
1462 default:
1463 printf("%s: command %d failed %x\n",
1464 sc->sc_dev.dv_xname, cmd, status);
1465 return ENXIO;
1466 }
1467 return 0;
1468 }
1469
1470 static int
1471 awi_cmd_wait(struct awi_softc *sc)
1472 {
1473 int i, error = 0;
1474
1475 i = 0;
1476 while (sc->sc_cmd_inprog) {
1477 if (sc->sc_invalid)
1478 return ENXIO;
1479 if (awi_read_1(sc, AWI_CMD) != sc->sc_cmd_inprog) {
1480 printf("%s: failed to access hardware\n",
1481 sc->sc_dev.dv_xname);
1482 sc->sc_invalid = 1;
1483 return ENXIO;
1484 }
1485 if (sc->sc_cansleep) {
1486 sc->sc_sleep_cnt++;
1487 error = tsleep(sc, PWAIT, "awicmd",
1488 AWI_CMD_TIMEOUT*hz/1000);
1489 sc->sc_sleep_cnt--;
1490 } else {
1491 if (awi_read_1(sc, AWI_CMD_STATUS) != AWI_STAT_IDLE) {
1492 awi_cmd_done(sc);
1493 break;
1494 }
1495 if (i++ >= AWI_CMD_TIMEOUT*1000/10)
1496 error = EWOULDBLOCK;
1497 else
1498 DELAY(10);
1499 }
1500 if (error)
1501 break;
1502 }
1503 if (error) {
1504 DPRINTF(("awi_cmd_wait: cmd 0x%x, error %d\n",
1505 sc->sc_cmd_inprog, error));
1506 }
1507 return error;
1508 }
1509
1510 static void
1511 awi_cmd_done(struct awi_softc *sc)
1512 {
1513 u_int8_t cmd, status;
1514
1515 status = awi_read_1(sc, AWI_CMD_STATUS);
1516 if (status == AWI_STAT_IDLE)
1517 return; /* stray interrupt */
1518
1519 cmd = sc->sc_cmd_inprog;
1520 sc->sc_cmd_inprog = 0;
1521 wakeup(sc);
1522 awi_write_1(sc, AWI_CMD, 0);
1523
1524 if (status != AWI_STAT_OK) {
1525 printf("%s: command %d failed %x\n",
1526 sc->sc_dev.dv_xname, cmd, status);
1527 sc->sc_substate = AWI_ST_NONE;
1528 return;
1529 }
1530 if (sc->sc_substate != AWI_ST_NONE)
1531 (void)ieee80211_new_state(&sc->sc_ic.ic_if, sc->sc_nstate, -1);
1532 }
1533
1534 static int
1535 awi_next_txd(struct awi_softc *sc, int len, u_int32_t *framep, u_int32_t *ntxdp)
1536 {
1537 u_int32_t txd, ntxd, frame;
1538
1539 txd = sc->sc_txnext;
1540 frame = txd + AWI_TXD_SIZE;
1541 if (frame + len > sc->sc_txend)
1542 frame = sc->sc_txbase;
1543 ntxd = frame + len;
1544 if (ntxd + AWI_TXD_SIZE > sc->sc_txend)
1545 ntxd = sc->sc_txbase;
1546 *framep = frame;
1547 *ntxdp = ntxd;
1548 /*
1549 * Determine if there are any room in ring buffer.
1550 * --- send wait, === new data, +++ conflict (ENOBUFS)
1551 * base........................end
1552 * done----txd=====ntxd OK
1553 * --txd=====done++++ntxd-- full
1554 * --txd=====ntxd done-- OK
1555 * ==ntxd done----txd=== OK
1556 * ==done++++ntxd----txd=== full
1557 * ++ntxd txd=====done++ full
1558 */
1559 if (txd < ntxd) {
1560 if (txd < sc->sc_txdone && ntxd + AWI_TXD_SIZE > sc->sc_txdone)
1561 return ENOBUFS;
1562 } else {
1563 if (txd < sc->sc_txdone || ntxd + AWI_TXD_SIZE > sc->sc_txdone)
1564 return ENOBUFS;
1565 }
1566 return 0;
1567 }
1568
1569 static int
1570 awi_lock(struct awi_softc *sc)
1571 {
1572 int error = 0;
1573
1574 if (curproc == NULL) {
1575 /*
1576 * XXX
1577 * Though driver ioctl should be called with context,
1578 * KAME ipv6 stack calls ioctl in interrupt for now.
1579 * We simply abort the request if there are other
1580 * ioctl requests in progress.
1581 */
1582 if (sc->sc_busy) {
1583 return EWOULDBLOCK;
1584 if (sc->sc_invalid)
1585 return ENXIO;
1586 }
1587 sc->sc_busy = 1;
1588 sc->sc_cansleep = 0;
1589 return 0;
1590 }
1591 while (sc->sc_busy) {
1592 if (sc->sc_invalid)
1593 return ENXIO;
1594 sc->sc_sleep_cnt++;
1595 error = tsleep(sc, PWAIT | PCATCH, "awilck", 0);
1596 sc->sc_sleep_cnt--;
1597 if (error)
1598 return error;
1599 }
1600 sc->sc_busy = 1;
1601 sc->sc_cansleep = 1;
1602 return 0;
1603 }
1604
1605 static void
1606 awi_unlock(struct awi_softc *sc)
1607 {
1608 sc->sc_busy = 0;
1609 sc->sc_cansleep = 0;
1610 if (sc->sc_sleep_cnt)
1611 wakeup(sc);
1612 }
1613
1614 static int
1615 awi_intr_lock(struct awi_softc *sc)
1616 {
1617 u_int8_t status;
1618 int i, retry;
1619
1620 status = 1;
1621 for (retry = 0; retry < 10; retry++) {
1622 for (i = 0; i < AWI_LOCKOUT_TIMEOUT*1000/5; i++) {
1623 if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0)
1624 break;
1625 DELAY(5);
1626 }
1627 if (status != 0)
1628 break;
1629 awi_write_1(sc, AWI_LOCKOUT_MAC, 1);
1630 if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0)
1631 break;
1632 awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
1633 }
1634 if (status != 0) {
1635 printf("%s: failed to lock interrupt\n",
1636 sc->sc_dev.dv_xname);
1637 return ENXIO;
1638 }
1639 return 0;
1640 }
1641
1642 static void
1643 awi_intr_unlock(struct awi_softc *sc)
1644 {
1645
1646 awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
1647 }
1648
1649 static int
1650 awi_newstate(void *arg, enum ieee80211_state nstate)
1651 {
1652 struct awi_softc *sc = arg;
1653 struct ieee80211com *ic = &sc->sc_ic;
1654 struct ieee80211_bss *bs = &ic->ic_bss;
1655 struct ifnet *ifp = &ic->ic_if;
1656 int i, error;
1657 u_int8_t newmode;
1658 enum ieee80211_state ostate;
1659 #ifdef AWI_DEBUG
1660 static const char *stname[] =
1661 { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" };
1662 static const char *substname[] =
1663 { "NONE", "SCAN_INIT", "SCAN_SETMIB", "SCAN_SCCMD",
1664 "SUB_INIT", "SUB_SETSS", "SUB_SYNC" };
1665 #endif /* AWI_DEBUG */
1666
1667 ostate = ic->ic_state;
1668 DPRINTF(("awi_newstate: %s (%s/%s) -> %s\n", stname[ostate],
1669 stname[sc->sc_nstate], substname[sc->sc_substate], stname[nstate]));
1670
1671 /* set LED */
1672 switch (nstate) {
1673 case IEEE80211_S_INIT:
1674 awi_drvstate(sc, AWI_DRV_RESET);
1675 break;
1676 case IEEE80211_S_SCAN:
1677 if (ic->ic_flags & IEEE80211_F_ADHOC)
1678 awi_drvstate(sc, AWI_DRV_ADHSC);
1679 else
1680 awi_drvstate(sc, AWI_DRV_INFSY);
1681 break;
1682 case IEEE80211_S_AUTH:
1683 awi_drvstate(sc, AWI_DRV_INFSY);
1684 break;
1685 case IEEE80211_S_ASSOC:
1686 awi_drvstate(sc, AWI_DRV_INFAUTH);
1687 break;
1688 case IEEE80211_S_RUN:
1689 if (ic->ic_flags & IEEE80211_F_ADHOC)
1690 awi_drvstate(sc, AWI_DRV_ADHSY);
1691 else
1692 awi_drvstate(sc, AWI_DRV_INFASSOC);
1693 break;
1694 }
1695
1696 if (nstate == IEEE80211_S_INIT) {
1697 sc->sc_substate = AWI_ST_NONE;
1698 ic->ic_flags &= ~IEEE80211_F_SIBSS;
1699 return 0;
1700 }
1701
1702 /* state transition */
1703 if (nstate == IEEE80211_S_SCAN) {
1704 if (ostate == IEEE80211_S_SCAN &&
1705 (ic->ic_flags & IEEE80211_F_ASCAN) == 0 &&
1706 (ic->ic_flags & IEEE80211_F_ADHOC) != 0 &&
1707 (ic->ic_flags & IEEE80211_F_IBSSON) != 0 &&
1708 sc->sc_adhoc_ap == 0 &&
1709 ic->ic_des_esslen != 0) {
1710 /* create IBSS */
1711 if (ifp->if_flags & IFF_DEBUG)
1712 printf("%s: creating ibss\n", ifp->if_xname);
1713 ic->ic_flags |= IEEE80211_F_SIBSS;
1714 bs->bs_chan = ic->ic_ibss_chan;
1715 bs->bs_intval = ic->ic_lintval;
1716 bs->bs_nrate = 0;
1717 for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
1718 if (ic->ic_sup_rates[i])
1719 bs->bs_rates[bs->bs_nrate++] =
1720 ic->ic_sup_rates[i];
1721 }
1722 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
1723 bs->bs_dwell = 200; /* XXX */
1724 memcpy(bs->bs_macaddr, ic->ic_myaddr,
1725 IEEE80211_ADDR_LEN);
1726 bs->bs_esslen = ic->ic_des_esslen;
1727 memcpy(bs->bs_essid, ic->ic_des_essid, bs->bs_esslen);
1728 nstate = IEEE80211_S_RUN;
1729 sc->sc_substate = AWI_ST_NONE;
1730 /* TODO */
1731 goto set_ssid;
1732 }
1733 ic->ic_flags &= ~IEEE80211_F_SIBSS;
1734
1735 /* SCAN substate */
1736 if (sc->sc_substate == AWI_ST_NONE) {
1737 sc->sc_nstate = nstate; /* next state in transition */
1738 sc->sc_substate = AWI_ST_SCAN_INIT;
1739 }
1740 switch (sc->sc_substate) {
1741 case AWI_ST_SCAN_INIT:
1742 sc->sc_substate = AWI_ST_SCAN_SETMIB;
1743 switch (ostate) {
1744 case IEEE80211_S_RUN:
1745 /* beacon miss */
1746 if (ifp->if_flags & IFF_DEBUG)
1747 printf("%s: no recent beacons from %s;"
1748 " rescanning\n",
1749 ifp->if_xname,
1750 ether_sprintf(ic->ic_bss.bs_bssid));
1751 /* FALLTHRU */
1752 case IEEE80211_S_AUTH:
1753 case IEEE80211_S_ASSOC:
1754 /* timeout restart scan */
1755 ieee80211_free_scan(ifp);
1756 /* FALLTHRU */
1757 case IEEE80211_S_INIT:
1758 ic->ic_flags |= IEEE80211_F_ASCAN;
1759 ic->ic_scan_timer = 0;
1760 /* FALLTHRU */
1761 case IEEE80211_S_SCAN:
1762 /* scan next */
1763 break;
1764 }
1765 if (ic->ic_flags & IEEE80211_F_ASCAN)
1766 newmode = AWI_SCAN_ACTIVE;
1767 else
1768 newmode = AWI_SCAN_PASSIVE;
1769 if (sc->sc_mib_mgt.aScan_Mode != newmode) {
1770 sc->sc_mib_mgt.aScan_Mode = newmode;
1771 if ((error = awi_mib(sc, AWI_CMD_SET_MIB,
1772 AWI_MIB_MGT, AWI_NOWAIT)) != 0)
1773 break;
1774 }
1775 /* FALLTHRU */
1776 case AWI_ST_SCAN_SETMIB:
1777 sc->sc_substate = AWI_ST_SCAN_SCCMD;
1778 if (sc->sc_cmd_inprog) {
1779 if ((error = awi_cmd_wait(sc)) != 0)
1780 break;
1781 }
1782 sc->sc_cmd_inprog = AWI_CMD_SCAN;
1783 awi_write_2(sc, AWI_CA_SCAN_DURATION,
1784 (ic->ic_flags & IEEE80211_F_ASCAN) ?
1785 AWI_ASCAN_DURATION : AWI_PSCAN_DURATION);
1786 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1787 if (bs->bs_chan < sc->sc_cur_chan)
1788 sc->sc_fhset_one = ~sc->sc_fhset_one;
1789 if (sc->sc_fhset_one)
1790 awi_write_1(sc, AWI_CA_SCAN_SET, 1);
1791 else
1792 awi_write_1(sc, AWI_CA_SCAN_SET,
1793 bs->bs_chan % 3 + 1);
1794 awi_write_1(sc, AWI_CA_SCAN_PATTERN,
1795 bs->bs_chan);
1796 awi_write_1(sc, AWI_CA_SCAN_IDX, 1);
1797 } else {
1798 awi_write_1(sc, AWI_CA_SCAN_SET, bs->bs_chan);
1799 awi_write_1(sc, AWI_CA_SCAN_PATTERN, 0);
1800 awi_write_1(sc, AWI_CA_SCAN_IDX, 0);
1801 }
1802 awi_write_1(sc, AWI_CA_SCAN_SUSP, 0);
1803 sc->sc_cur_chan = bs->bs_chan;
1804 if ((error = awi_cmd(sc, AWI_CMD_SCAN, AWI_NOWAIT))
1805 != 0)
1806 break;
1807 /* FALLTHRU */
1808 case AWI_ST_SCAN_SCCMD:
1809 if (ic->ic_scan_timer == 0)
1810 ic->ic_scan_timer =
1811 (ic->ic_flags & IEEE80211_F_ASCAN) ?
1812 IEEE80211_ASCAN_WAIT : IEEE80211_PSCAN_WAIT;
1813 ifp->if_timer = 1;
1814 ic->ic_state = nstate;
1815 sc->sc_substate = AWI_ST_NONE;
1816 error = EINPROGRESS;
1817 break;
1818 default:
1819 DPRINTF(("awi_newstate: unexpected state %s/%s\n",
1820 stname[nstate], substname[sc->sc_substate]));
1821 sc->sc_substate = AWI_ST_NONE;
1822 error = EIO;
1823 break;
1824 }
1825 return error;
1826 }
1827
1828 if (ostate == IEEE80211_S_SCAN) {
1829 /* set SSID and channel */
1830 set_ssid:
1831 /* substate */
1832 if (sc->sc_substate == AWI_ST_NONE) {
1833 sc->sc_nstate = nstate; /* next state in transition */
1834 sc->sc_substate = AWI_ST_SUB_INIT;
1835 }
1836 switch (sc->sc_substate) {
1837 case AWI_ST_SUB_INIT:
1838 sc->sc_substate = AWI_ST_SUB_SETSS;
1839 memcpy(&sc->sc_mib_mgt.aCurrent_BSS_ID, bs->bs_bssid,
1840 IEEE80211_ADDR_LEN);
1841 memset(&sc->sc_mib_mgt.aCurrent_ESS_ID, 0,
1842 AWI_ESS_ID_SIZE);
1843 sc->sc_mib_mgt.aCurrent_ESS_ID[0] =
1844 IEEE80211_ELEMID_SSID;
1845 sc->sc_mib_mgt.aCurrent_ESS_ID[1] = bs->bs_esslen;
1846 memcpy(&sc->sc_mib_mgt.aCurrent_ESS_ID[2],
1847 bs->bs_essid, bs->bs_esslen);
1848 LE_WRITE_2(&sc->sc_mib_mgt.aBeacon_Period,
1849 bs->bs_intval);
1850 if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT,
1851 AWI_NOWAIT)) != 0)
1852 break;
1853 /* FALLTHRU */
1854 case AWI_ST_SUB_SETSS:
1855 sc->sc_substate = AWI_ST_SUB_SYNC;
1856 if (sc->sc_cmd_inprog) {
1857 if (awi_cmd_wait(sc))
1858 break;
1859 }
1860 sc->sc_cmd_inprog = AWI_CMD_SYNC;
1861 if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1862 if (sc->sc_fhset_one)
1863 awi_write_1(sc, AWI_CA_SYNC_SET, 1);
1864 else
1865 awi_write_1(sc, AWI_CA_SYNC_SET,
1866 bs->bs_chan % 3 + 1);
1867 awi_write_1(sc, AWI_CA_SYNC_IDX, 1);
1868 } else {
1869 awi_write_1(sc, AWI_CA_SYNC_SET, bs->bs_chan);
1870 awi_write_1(sc, AWI_CA_SYNC_PATTERN, 0);
1871 awi_write_1(sc, AWI_CA_SYNC_IDX, 0);
1872 }
1873 if ((ic->ic_flags & IEEE80211_F_SIBSS) &&
1874 !sc->sc_no_bssid)
1875 awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 1);
1876 else
1877 awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 0);
1878 awi_write_2(sc, AWI_CA_SYNC_DWELL, bs->bs_dwell);
1879 awi_write_2(sc, AWI_CA_SYNC_MBZ, 0);
1880 awi_write_bytes(sc, AWI_CA_SYNC_TIMESTAMP,
1881 bs->bs_tstamp, 8);
1882 awi_write_4(sc, AWI_CA_SYNC_REFTIME, bs->bs_timoff);
1883 sc->sc_cur_chan = bs->bs_chan;
1884 if ((error = awi_cmd(sc, AWI_CMD_SYNC, AWI_NOWAIT))
1885 != 0)
1886 break;
1887 /* FALLTHRU */
1888 case AWI_ST_SUB_SYNC:
1889 sc->sc_substate = AWI_ST_NONE;
1890 if (ic->ic_flags & IEEE80211_F_SIBSS) {
1891 if ((error = awi_mib(sc, AWI_CMD_GET_MIB,
1892 AWI_MIB_MGT, AWI_WAIT)) != 0)
1893 break;
1894 memcpy(bs->bs_bssid,
1895 &sc->sc_mib_mgt.aCurrent_BSS_ID,
1896 IEEE80211_ADDR_LEN);
1897 } else {
1898 if (nstate == IEEE80211_S_RUN) {
1899 sc->sc_rx_timer = 10;
1900 ifp->if_timer = 1;
1901 }
1902 }
1903 error = 0;
1904 break;
1905 default:
1906 DPRINTF(("awi_newstate: unexpected state %s/%s\n",
1907 stname[nstate], substname[sc->sc_substate]));
1908 sc->sc_substate = AWI_ST_NONE;
1909 error = EIO;
1910 break;
1911 }
1912 return error;
1913 }
1914
1915 sc->sc_substate = AWI_ST_NONE;
1916
1917 return 0;
1918 }
1919
1920 static struct mbuf *
1921 awi_ether_encap(struct awi_softc *sc, struct mbuf *m)
1922 {
1923 struct ieee80211com *ic = &sc->sc_ic;
1924 struct ieee80211_bss *bs = &ic->ic_bss;
1925 struct ether_header *eh;
1926 struct ieee80211_frame *wh;
1927
1928 if (m->m_len < sizeof(struct ether_header)) {
1929 m = m_pullup(m, sizeof(struct ether_header));
1930 if (m == NULL)
1931 return NULL;
1932 }
1933 eh = mtod(m, struct ether_header *);
1934 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1935 if (m == NULL)
1936 return NULL;
1937 wh = mtod(m, struct ieee80211_frame *);
1938 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1939 *(u_int16_t *)wh->i_dur = 0;
1940 *(u_int16_t *)wh->i_seq =
1941 htole16(bs->bs_txseq << IEEE80211_SEQ_SEQ_SHIFT);
1942 bs->bs_txseq++;
1943 if (ic->ic_flags & IEEE80211_F_ADHOC) {
1944 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1945 if (sc->sc_adhoc_ap)
1946 memcpy(wh->i_addr1, bs->bs_macaddr, IEEE80211_ADDR_LEN);
1947 else
1948 memcpy(wh->i_addr1, eh->ether_dhost,
1949 IEEE80211_ADDR_LEN);
1950 memcpy(wh->i_addr2, eh->ether_shost, IEEE80211_ADDR_LEN);
1951 memcpy(wh->i_addr3, bs->bs_bssid, IEEE80211_ADDR_LEN);
1952 } else {
1953 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1954 memcpy(wh->i_addr1, bs->bs_bssid, IEEE80211_ADDR_LEN);
1955 memcpy(wh->i_addr2, eh->ether_shost, IEEE80211_ADDR_LEN);
1956 memcpy(wh->i_addr3, eh->ether_dhost, IEEE80211_ADDR_LEN);
1957 }
1958 return m;
1959 }
1960
1961 static struct mbuf *
1962 awi_ether_modcap(struct awi_softc *sc, struct mbuf *m)
1963 {
1964 struct ieee80211com *ic = &sc->sc_ic;
1965 struct ether_header eh;
1966 struct ieee80211_frame wh;
1967 struct llc *llc;
1968
1969 if (m->m_len < sizeof(wh) + sizeof(eh)) {
1970 m = m_pullup(m, sizeof(wh) + sizeof(eh));
1971 if (m == NULL)
1972 return NULL;
1973 }
1974 memcpy(&wh, mtod(m, caddr_t), sizeof(wh));
1975 if (wh.i_fc[0] != (IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA))
1976 return m;
1977 memcpy(&eh, mtod(m, caddr_t) + sizeof(wh), sizeof(eh));
1978 m_adj(m, sizeof(eh) - sizeof(*llc));
1979 if (ic->ic_flags & IEEE80211_F_ADHOC)
1980 memcpy(wh.i_addr2, eh.ether_shost, IEEE80211_ADDR_LEN);
1981 memcpy(mtod(m, caddr_t), &wh, sizeof(wh));
1982 llc = (struct llc *)(mtod(m, caddr_t) + sizeof(wh));
1983 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1984 llc->llc_control = LLC_UI;
1985 llc->llc_snap.org_code[0] = 0;
1986 llc->llc_snap.org_code[1] = 0;
1987 llc->llc_snap.org_code[2] = 0;
1988 llc->llc_snap.ether_type = eh.ether_type;
1989 return m;
1990 }
1991