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