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