wi.c revision 1.98 1 /* $NetBSD: wi.c,v 1.98 2002/10/04 04:23:20 onoe Exp $ */
2
3 /*
4 * Copyright (c) 1997, 1998, 1999
5 * Bill Paul <wpaul (at) ctr.columbia.edu>. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /*
36 * Lucent WaveLAN/IEEE 802.11 PCMCIA driver for NetBSD.
37 *
38 * Original FreeBSD driver written by Bill Paul <wpaul (at) ctr.columbia.edu>
39 * Electrical Engineering Department
40 * Columbia University, New York City
41 */
42
43 /*
44 * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
45 * from Lucent. Unlike the older cards, the new ones are programmed
46 * entirely via a firmware-driven controller called the Hermes.
47 * Unfortunately, Lucent will not release the Hermes programming manual
48 * without an NDA (if at all). What they do release is an API library
49 * called the HCF (Hardware Control Functions) which is supposed to
50 * do the device-specific operations of a device driver for you. The
51 * publically available version of the HCF library (the 'HCF Light') is
52 * a) extremely gross, b) lacks certain features, particularly support
53 * for 802.11 frames, and c) is contaminated by the GNU Public License.
54 *
55 * This driver does not use the HCF or HCF Light at all. Instead, it
56 * programs the Hermes controller directly, using information gleaned
57 * from the HCF Light code and corresponding documentation.
58 *
59 * This driver supports both the PCMCIA and ISA versions of the
60 * WaveLAN/IEEE cards. Note however that the ISA card isn't really
61 * anything of the sort: it's actually a PCMCIA bridge adapter
62 * that fits into an ISA slot, into which a PCMCIA WaveLAN card is
63 * inserted. Consequently, you need to use the pccard support for
64 * both the ISA and PCMCIA adapters.
65 */
66
67 /*
68 * FreeBSD driver ported to NetBSD by Bill Sommerfeld in the back of the
69 * Oslo IETF plenary meeting.
70 */
71
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: wi.c,v 1.98 2002/10/04 04:23:20 onoe Exp $");
74
75 #define WI_HERMES_AUTOINC_WAR /* Work around data write autoinc bug. */
76 #define WI_HERMES_STATS_WAR /* Work around stats counter bug. */
77
78 #include "bpfilter.h"
79
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/callout.h>
83 #include <sys/device.h>
84 #include <sys/socket.h>
85 #include <sys/mbuf.h>
86 #include <sys/ioctl.h>
87 #include <sys/kernel.h> /* for hz */
88 #include <sys/proc.h>
89
90 #include <net/if.h>
91 #include <net/if_dl.h>
92 #include <net/if_media.h>
93 #include <net/if_ether.h>
94 #include <net/if_ieee80211.h>
95
96 #if NBPFILTER > 0
97 #include <net/bpf.h>
98 #include <net/bpfdesc.h>
99 #endif
100
101 #include <machine/bus.h>
102
103 #include <dev/ic/wi_ieee.h>
104 #include <dev/ic/wireg.h>
105 #include <dev/ic/wivar.h>
106
107 static int wi_init(struct ifnet *);
108 static void wi_stop(struct ifnet *, int);
109 static void wi_start(struct ifnet *);
110 static int wi_reset(struct wi_softc *);
111 static void wi_watchdog(struct ifnet *);
112 static int wi_ioctl(struct ifnet *, u_long, caddr_t);
113 static int wi_media_change(struct ifnet *);
114 static void wi_media_status(struct ifnet *, struct ifmediareq *);
115
116 static void wi_rx_intr(struct wi_softc *);
117 static void wi_tx_intr(struct wi_softc *);
118 static void wi_info_intr(struct wi_softc *);
119
120 static int wi_get_cfg(struct ifnet *, u_long, caddr_t);
121 static int wi_set_cfg(struct ifnet *, u_long, caddr_t);
122 static int wi_write_txrate(struct wi_softc *);
123 static int wi_write_wep(struct wi_softc *);
124 static int wi_write_multi(struct wi_softc *);
125 static int wi_alloc_fid(struct wi_softc *, int, int *);
126 static void wi_read_nicid(struct wi_softc *);
127 static int wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
128
129 static int wi_cmd(struct wi_softc *, int, int, int, int);
130 static int wi_seek_bap(struct wi_softc *, int, int);
131 static int wi_read_bap(struct wi_softc *, int, int, void *, int);
132 static int wi_write_bap(struct wi_softc *, int, int, void *, int);
133 static int wi_read_rid(struct wi_softc *, int, void *, int *);
134 static int wi_write_rid(struct wi_softc *, int, void *, int);
135
136 static int wi_newstate(void *, enum ieee80211_state);
137
138 static int wi_scan_ap(struct wi_softc *);
139 static void wi_scan_result(struct wi_softc *, int, int);
140
141 static inline int
142 wi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
143 {
144
145 val = htole16(val);
146 return wi_write_rid(sc, rid, &val, sizeof(val));
147 }
148
149 #ifdef WI_DEBUG
150 int wi_debug = 0;
151
152 #define DPRINTF(X) if (wi_debug) printf X
153 #define DPRINTF2(X) if (wi_debug > 1) printf X
154 #else
155 #define DPRINTF(X)
156 #define DPRINTF2(X)
157 #endif
158
159 #define WI_INTRS (WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
160
161 struct wi_card_ident
162 wi_card_ident[] = {
163 /* CARD_ID CARD_NAME FIRM_TYPE */
164 { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT },
165 { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT },
166 { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT },
167 { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL },
168 { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL },
169 { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL },
170 { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL },
171 { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL },
172 { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL },
173 { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL },
174 { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL },
175 { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL },
176 { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
177 { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
178 { WI_NIC_3842_PCMCIA_ATM_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
179 { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
180 { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
181 { WI_NIC_3842_MINI_ATM_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
182 { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
183 { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
184 { WI_NIC_3842_PCI_ATM_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
185 { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
186 { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
187 { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
188 { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
189 { 0, NULL, 0 },
190 };
191
192 int
193 wi_attach(struct wi_softc *sc)
194 {
195 struct ieee80211com *ic = &sc->sc_ic;
196 struct ifnet *ifp = &ic->ic_if;
197 int i, nrate, mword, buflen;
198 u_int8_t r;
199 u_int16_t val;
200 u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
201 static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
202 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
203 };
204 int s;
205
206 s = splnet();
207
208 /* Make sure interrupts are disabled. */
209 CSR_WRITE_2(sc, WI_INT_EN, 0);
210 CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
211
212 /* Reset the NIC. */
213 if (wi_reset(sc) != 0) {
214 splx(s);
215 return 1;
216 }
217
218 buflen = IEEE80211_ADDR_LEN;
219 if (wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen) != 0 ||
220 IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) {
221 printf(" could not get mac address, attach failed\n");
222 splx(s);
223 return 1;
224 }
225
226 printf(" 802.11 address %s\n", ether_sprintf(ic->ic_myaddr));
227
228 /* Read NIC identification */
229 wi_read_nicid(sc);
230
231 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
232 ifp->if_softc = sc;
233 ifp->if_start = wi_start;
234 ifp->if_ioctl = wi_ioctl;
235 ifp->if_watchdog = wi_watchdog;
236 ifp->if_init = wi_init;
237 ifp->if_stop = wi_stop;
238 ifp->if_flags =
239 IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST | IFF_NOTRAILERS;
240 IFQ_SET_READY(&ifp->if_snd);
241
242 ic->ic_phytype = IEEE80211_T_DS;
243 ic->ic_opmode = IEEE80211_M_STA;
244 ic->ic_flags = IEEE80211_F_HASPMGT | IEEE80211_F_HASAHDEMO;
245 ic->ic_state = IEEE80211_S_INIT;
246 ic->ic_newstate = wi_newstate;
247
248 /* Find available channel */
249 buflen = sizeof(val);
250 if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
251 val = htole16(0x1fff); /* assume 1-11 */
252 for (i = 0; i < 16; i++) {
253 if (isset((u_int8_t*)&val, i))
254 setbit(ic->ic_chan_avail, i + 1);
255 }
256
257 /* Find default IBSS channel */
258 buflen = sizeof(val);
259 if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0)
260 ic->ic_ibss_chan = le16toh(val);
261 else {
262 /* use lowest available channel */
263 for (i = 0; i < 16; i++) {
264 if (isset(ic->ic_chan_avail, i))
265 break;
266 }
267 ic->ic_ibss_chan = i;
268 }
269
270 /*
271 * Set flags based on firmware version.
272 */
273 switch (sc->sc_firmware_type) {
274 case WI_LUCENT:
275 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
276 #ifdef WI_HERMES_AUTOINC_WAR
277 /* XXX: not confirmed, but never seen for recent firmware */
278 if (sc->sc_sta_firmware_ver < 40000) {
279 sc->sc_flags |= WI_FLAGS_BUG_AUTOINC;
280 }
281 #endif
282 if (sc->sc_sta_firmware_ver >= 60000)
283 sc->sc_flags |= WI_FLAGS_HAS_MOR;
284 if (sc->sc_sta_firmware_ver >= 60006)
285 ic->ic_flags |= IEEE80211_F_HASIBSS;
286 sc->sc_ibss_port = 1;
287 break;
288
289 case WI_INTERSIL:
290 sc->sc_flags |= WI_FLAGS_HAS_ROAMING;
291 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
292 if (sc->sc_sta_firmware_ver >= 800) {
293 ic->ic_flags |= IEEE80211_F_HASHOSTAP;
294 ic->ic_flags |= IEEE80211_F_HASIBSS;
295 }
296 sc->sc_ibss_port = 0;
297 break;
298
299 case WI_SYMBOL:
300 sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY;
301 if (sc->sc_sta_firmware_ver >= 25000)
302 ic->ic_flags |= IEEE80211_F_HASIBSS;
303 sc->sc_ibss_port = 4;
304 break;
305 }
306
307 /*
308 * Find out if we support WEP on this card.
309 */
310 buflen = sizeof(val);
311 if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
312 val != htole16(0))
313 ic->ic_flags |= IEEE80211_F_HASWEP;
314
315 /* Find supported rates. */
316 buflen = sizeof(ratebuf);
317 if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
318 nrate = le16toh(*(u_int16_t *)ratebuf);
319 if (nrate > IEEE80211_RATE_SIZE)
320 nrate = IEEE80211_RATE_SIZE;
321 memcpy(ic->ic_sup_rates, ratebuf + 2, nrate);
322 }
323 buflen = sizeof(val);
324
325 sc->sc_max_datalen = 2304;
326 sc->sc_rts_thresh = 2347;
327 sc->sc_system_scale = 1;
328 sc->sc_cnfauthmode = 1;
329 sc->sc_roaming_mode = 1;
330
331 ifmedia_init(&sc->sc_media, 0, wi_media_change, wi_media_status);
332 printf("%s: supported rates: ", sc->sc_dev.dv_xname);
333 #define ADD(s, o) ifmedia_add(&sc->sc_media, \
334 IFM_MAKEWORD(IFM_IEEE80211, (s), (o), 0), 0, NULL)
335 ADD(IFM_AUTO, 0);
336 if (ic->ic_flags & IEEE80211_F_HASHOSTAP)
337 ADD(IFM_AUTO, IFM_IEEE80211_HOSTAP);
338 if (ic->ic_flags & IEEE80211_F_HASIBSS)
339 ADD(IFM_AUTO, IFM_IEEE80211_ADHOC);
340 ADD(IFM_AUTO, IFM_IEEE80211_ADHOC | IFM_FLAG0);
341 for (i = 0; i < nrate; i++) {
342 r = ic->ic_sup_rates[i];
343 mword = ieee80211_rate2media(r, IEEE80211_T_DS);
344 if (mword == 0)
345 continue;
346 printf("%s%d%sMbps", (i != 0 ? " " : ""),
347 (r & IEEE80211_RATE_VAL) / 2, ((r & 0x1) != 0 ? ".5" : ""));
348 ADD(mword, 0);
349 if (ic->ic_flags & IEEE80211_F_HASHOSTAP)
350 ADD(mword, IFM_IEEE80211_HOSTAP);
351 if (ic->ic_flags & IEEE80211_F_HASIBSS)
352 ADD(mword, IFM_IEEE80211_ADHOC);
353 ADD(mword, IFM_IEEE80211_ADHOC | IFM_FLAG0);
354 }
355 printf("\n");
356 ifmedia_set(&sc->sc_media, IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO, 0, 0));
357 #undef ADD
358
359 /*
360 * Call MI attach routines.
361 */
362
363 if_attach(ifp);
364 ieee80211_ifattach(ifp);
365
366 /* Attach is successful. */
367 sc->sc_attached = 1;
368
369 splx(s);
370 return 0;
371 }
372
373 int
374 wi_detach(struct wi_softc *sc)
375 {
376 struct ifnet *ifp = &sc->sc_ic.ic_if;
377 int s;
378
379 if (!sc->sc_attached)
380 return 0;
381
382 s = splnet();
383
384 /* Delete all remaining media. */
385 ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY);
386
387 ieee80211_ifdetach(ifp);
388 if_detach(ifp);
389 if (sc->sc_enabled) {
390 if (sc->sc_disable)
391 (*sc->sc_disable)(sc);
392 sc->sc_enabled = 0;
393 }
394 splx(s);
395 return 0;
396 }
397
398 int
399 wi_activate(struct device *self, enum devact act)
400 {
401 struct wi_softc *sc = (struct wi_softc *)self;
402 int rv = 0, s;
403
404 s = splnet();
405 switch (act) {
406 case DVACT_ACTIVATE:
407 rv = EOPNOTSUPP;
408 break;
409
410 case DVACT_DEACTIVATE:
411 if_deactivate(&sc->sc_ic.ic_if);
412 break;
413 }
414 splx(s);
415 return rv;
416 }
417
418 void
419 wi_power(struct wi_softc *sc, int why)
420 {
421 struct ifnet *ifp = &sc->sc_ic.ic_if;
422 int s;
423
424 s = splnet();
425 switch (why) {
426 case PWR_SUSPEND:
427 case PWR_STANDBY:
428 wi_stop(ifp, 1);
429 break;
430 case PWR_RESUME:
431 if (ifp->if_flags & IFF_UP) {
432 wi_init(ifp);
433 (void)wi_intr(sc);
434 }
435 break;
436 case PWR_SOFTSUSPEND:
437 case PWR_SOFTSTANDBY:
438 case PWR_SOFTRESUME:
439 break;
440 }
441 splx(s);
442 }
443
444 void
445 wi_shutdown(struct wi_softc *sc)
446 {
447 struct ifnet *ifp = &sc->sc_ic.ic_if;
448
449 if (sc->sc_attached)
450 wi_stop(ifp, 1);
451 }
452
453 int
454 wi_intr(void *arg)
455 {
456 int i;
457 struct wi_softc *sc = arg;
458 struct ifnet *ifp = &sc->sc_ic.ic_if;
459 u_int16_t status, raw_status, last_status;
460
461 if (sc->sc_enabled == 0 ||
462 (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 ||
463 (ifp->if_flags & IFF_RUNNING) == 0)
464 return 0;
465
466 if ((ifp->if_flags & IFF_UP) == 0) {
467 CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
468 CSR_WRITE_2(sc, WI_INT_EN, 0);
469 return 1;
470 }
471
472 /* maximum 10 loops per interrupt */
473 last_status = 0;
474 for (i = 0; i < 10; i++) {
475 /*
476 * Only believe a status bit when we enter wi_intr, or when
477 * the bit was "off" the last time through the loop. This is
478 * my strategy to avoid racing the hardware/firmware if I
479 * can re-read the event status register more quickly than
480 * it is updated.
481 */
482 raw_status = CSR_READ_2(sc, WI_EVENT_STAT);
483 status = raw_status & ~last_status;
484 if ((status & WI_INTRS) == 0)
485 break;
486 last_status = raw_status;
487
488 if (status & WI_EV_RX)
489 wi_rx_intr(sc);
490
491 if (status & WI_EV_ALLOC)
492 wi_tx_intr(sc);
493
494 if (status & WI_EV_INFO)
495 wi_info_intr(sc);
496
497 if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
498 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 &&
499 !IFQ_IS_EMPTY(&ifp->if_snd))
500 wi_start(ifp);
501 }
502
503 return 1;
504 }
505
506 static int
507 wi_init(struct ifnet *ifp)
508 {
509 struct wi_softc *sc = ifp->if_softc;
510 struct ieee80211com *ic = &sc->sc_ic;
511 struct wi_joinreq join;
512 int i;
513 int error = 0, wasenabled;
514
515 DPRINTF(("wi_init: enabled %d\n", sc->sc_enabled));
516 wasenabled = sc->sc_enabled;
517 if (!sc->sc_enabled) {
518 if ((error = (*sc->sc_enable)(sc)) != 0)
519 goto out;
520 sc->sc_enabled = 1;
521 } else
522 wi_stop(ifp, 0);
523
524 /* Symbol firmware cannot be initialized more than once */
525 if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
526 if ((error = wi_reset(sc)) != 0)
527 goto out;
528 }
529
530 /* common 802.11 configuration */
531 ic->ic_flags &= ~IEEE80211_F_IBSSON;
532 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
533 switch (ic->ic_opmode) {
534 case IEEE80211_M_STA:
535 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS);
536 break;
537 case IEEE80211_M_IBSS:
538 wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port);
539 ic->ic_flags |= IEEE80211_F_IBSSON;
540 break;
541 case IEEE80211_M_AHDEMO:
542 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
543 break;
544 case IEEE80211_M_HOSTAP:
545 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP);
546 break;
547 }
548
549 /* Intersil interprets this RID as joining ESS even in IBSS mode */
550 if (sc->sc_firmware_type == WI_LUCENT &&
551 (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0)
552 wi_write_val(sc, WI_RID_CREATE_IBSS, 1);
553 else
554 wi_write_val(sc, WI_RID_CREATE_IBSS, 0);
555 wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
556 wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid,
557 ic->ic_des_esslen);
558 wi_write_val(sc, WI_RID_OWN_CHNL, ic->ic_ibss_chan);
559 wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen);
560 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
561 wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN);
562 wi_write_val(sc, WI_RID_PM_ENABLED,
563 (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
564
565 /* not yet common 802.11 configuration */
566 wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen);
567 wi_write_val(sc, WI_RID_RTS_THRESH, sc->sc_rts_thresh);
568
569 /* driver specific 802.11 configuration */
570 if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)
571 wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale);
572 if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
573 wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode);
574 if (sc->sc_flags & WI_FLAGS_HAS_MOR)
575 wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven);
576 wi_write_txrate(sc);
577 wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen);
578
579 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
580 sc->sc_firmware_type == WI_INTERSIL) {
581 wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval);
582 wi_write_val(sc, WI_RID_BASIC_RATE, 0x03); /* 1, 2 */
583 wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */
584 wi_write_val(sc, WI_RID_DTIM_PERIOD, 1);
585 }
586
587 /*
588 * Initialize promisc mode.
589 * Being in the Host-AP mode causes a great
590 * deal of pain if primisc mode is set.
591 * Therefore we avoid confusing the firmware
592 * and always reset promisc mode in Host-AP
593 * mode. Host-AP sees all the packets anyway.
594 */
595 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
596 (ifp->if_flags & IFF_PROMISC) != 0) {
597 wi_write_val(sc, WI_RID_PROMISC, 1);
598 } else {
599 wi_write_val(sc, WI_RID_PROMISC, 0);
600 }
601
602 /* Configure WEP. */
603 if (ic->ic_flags & IEEE80211_F_HASWEP)
604 wi_write_wep(sc);
605
606 /* Set multicast filter. */
607 wi_write_multi(sc);
608
609 if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
610 sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
611 if (sc->sc_firmware_type == WI_SYMBOL)
612 sc->sc_buflen = 1585; /* XXX */
613 for (i = 0; i < WI_NTXBUF; i++) {
614 error = wi_alloc_fid(sc, sc->sc_buflen,
615 &sc->sc_txd[i].d_fid);
616 if (error) {
617 printf("%s: tx buffer allocation failed\n",
618 sc->sc_dev.dv_xname);
619 goto out;
620 }
621 DPRINTF2(("wi_init: txbuf %d allocated %x\n", i,
622 sc->sc_txd[i].d_fid));
623 sc->sc_txd[i].d_len = 0;
624 }
625 }
626 sc->sc_txcur = sc->sc_txnext = 0;
627
628 /* Enable port 0 */
629 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
630 ifp->if_flags |= IFF_RUNNING;
631 ifp->if_flags &= ~IFF_OACTIVE;
632 if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
633 ic->ic_opmode == IEEE80211_M_HOSTAP)
634 wi_newstate(sc, IEEE80211_S_RUN);
635
636 /* Enable interrupts */
637 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
638
639 if (!wasenabled &&
640 ic->ic_opmode == IEEE80211_M_HOSTAP &&
641 sc->sc_firmware_type == WI_INTERSIL) {
642 /* XXX: some card need to be re-enabled for hostap */
643 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
644 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
645 }
646
647 if (ic->ic_opmode == IEEE80211_M_STA &&
648 ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
649 ic->ic_des_chan != IEEE80211_CHAN_ANY)) {
650 memset(&join, 0, sizeof(join));
651 if (ic->ic_flags & IEEE80211_F_DESBSSID)
652 IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
653 if (ic->ic_des_chan != IEEE80211_CHAN_ANY)
654 join.wi_chan = htole16(ic->ic_des_chan);
655 wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
656 }
657
658 out:
659 if (error) {
660 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
661 wi_stop(ifp, 0);
662 }
663 DPRINTF(("wi_init: return %d\n", error));
664 return error;
665 }
666
667 static void
668 wi_stop(struct ifnet *ifp, int disable)
669 {
670 struct wi_softc *sc = ifp->if_softc;
671
672 DPRINTF(("wi_stop: disable %d\n", disable));
673 ieee80211_new_state(ifp, IEEE80211_S_INIT, -1);
674 if (sc->sc_enabled) {
675 CSR_WRITE_2(sc, WI_INT_EN, 0);
676 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
677 if (disable) {
678 if (sc->sc_disable)
679 (*sc->sc_disable)(sc);
680 sc->sc_enabled = 0;
681 }
682 }
683
684 sc->sc_tx_timer = 0;
685 sc->sc_scan_timer = 0;
686 sc->sc_naps = 0;
687 ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
688 ifp->if_timer = 0;
689 }
690
691 static void
692 wi_start(struct ifnet *ifp)
693 {
694 struct wi_softc *sc = ifp->if_softc;
695 struct ieee80211com *ic = &sc->sc_ic;
696 struct ieee80211_node *ni;
697 struct ieee80211_frame *wh;
698 struct mbuf *m0, *m;
699 struct wi_frame frmhdr;
700 int cur, fid, off;
701
702 if (ifp->if_flags & IFF_OACTIVE)
703 return;
704 if (sc->sc_flags & WI_FLAGS_OUTRANGE)
705 return;
706
707 memset(&frmhdr, 0, sizeof(frmhdr));
708 cur = sc->sc_txnext;
709 for (;;) {
710 IF_POLL(&ic->ic_mgtq, m0);
711 if (m0 != NULL) {
712 if (sc->sc_txd[cur].d_len != 0) {
713 ifp->if_flags |= IFF_OACTIVE;
714 break;
715 }
716 IF_DEQUEUE(&ic->ic_mgtq, m0);
717 m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
718 (caddr_t)&frmhdr.wi_ehdr);
719 frmhdr.wi_ehdr.ether_type = 0;
720 wh = mtod(m0, struct ieee80211_frame *);
721 } else {
722 if (ic->ic_state != IEEE80211_S_RUN)
723 break;
724 IFQ_POLL(&ifp->if_snd, m0);
725 if (m0 == NULL)
726 break;
727 if (sc->sc_txd[cur].d_len != 0) {
728 ifp->if_flags |= IFF_OACTIVE;
729 break;
730 }
731 IFQ_DEQUEUE(&ifp->if_snd, m0);
732 ifp->if_opackets++;
733 m_copydata(m0, 0, ETHER_HDR_LEN,
734 (caddr_t)&frmhdr.wi_ehdr);
735 #if NBPFILTER > 0
736 if (ifp->if_bpf)
737 bpf_mtap(ifp->if_bpf, m0);
738 #endif
739
740 if ((m0 = ieee80211_encap(ifp, m0)) == NULL) {
741 ifp->if_oerrors++;
742 continue;
743 }
744 wh = mtod(m0, struct ieee80211_frame *);
745 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
746 !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
747 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
748 IEEE80211_FC0_TYPE_DATA &&
749 ((ni = ieee80211_find_node(ic, wh->i_addr1)) ==
750 NULL || ni->ni_associd == 0)) {
751 m_freem(m0);
752 ifp->if_oerrors++;
753 continue;
754 }
755 if (ic->ic_flags & IEEE80211_F_WEPON)
756 wh->i_fc[1] |= IEEE80211_FC1_WEP;
757
758 }
759 #if NBPFILTER > 0
760 if (ic->ic_rawbpf)
761 bpf_mtap(ic->ic_rawbpf, m0);
762 #endif
763 frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11);
764 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
765 (wh->i_fc[1] & IEEE80211_FC1_WEP)) {
766 if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) {
767 ifp->if_oerrors++;
768 continue;
769 }
770 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
771 }
772 m_copydata(m0, 0, sizeof(struct ieee80211_frame),
773 (caddr_t)&frmhdr.wi_whdr);
774 m_adj(m0, sizeof(struct ieee80211_frame));
775 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
776 #if NBPFILTER > 0
777 if (sc->sc_drvbpf) {
778 struct mbuf mb;
779
780 M_COPY_PKTHDR(&mb, m0);
781 mb.m_data = (caddr_t)&frmhdr;
782 mb.m_len = sizeof(frmhdr);
783 mb.m_next = m0;
784 mb.m_pkthdr.len += mb.m_len;
785 bpf_mtap(sc->sc_drvbpf, &mb);
786 }
787 #endif
788 fid = sc->sc_txd[cur].d_fid;
789 wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr));
790 off = sizeof(frmhdr);
791 for (m = m0; m != NULL; m = m->m_next) {
792 if (m->m_len == 0)
793 continue;
794 wi_write_bap(sc, fid, off, m->m_data, m->m_len);
795 off += m->m_len;
796 }
797 m_freem(m0);
798 sc->sc_txd[cur].d_len = off;
799 if (sc->sc_txcur == cur) {
800 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
801 printf("%s: xmit failed\n",
802 sc->sc_dev.dv_xname);
803 sc->sc_txd[cur].d_len = 0;
804 continue;
805 }
806 sc->sc_tx_timer = 5;
807 ifp->if_timer = 1;
808 }
809 sc->sc_txnext = cur = (cur + 1) % WI_NTXBUF;
810 }
811 }
812
813
814 static int
815 wi_reset(struct wi_softc *sc)
816 {
817 int i, error;
818
819 DPRINTF(("wi_reset\n"));
820 error = 0;
821 for (i = 0; i < 5; i++) {
822 DELAY(20*1000); /* XXX: way too long! */
823 if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
824 break;
825 }
826 if (error) {
827 printf("%s: init failed\n", sc->sc_dev.dv_xname);
828 return error;
829 }
830 CSR_WRITE_2(sc, WI_INT_EN, 0);
831 CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
832
833 /* Calibrate timer. */
834 wi_write_val(sc, WI_RID_TICK_TIME, 0);
835 return 0;
836 }
837
838 static void
839 wi_watchdog(struct ifnet *ifp)
840 {
841 struct wi_softc *sc = ifp->if_softc;
842
843 ifp->if_timer = 0;
844 if (!sc->sc_enabled)
845 return;
846
847 if (sc->sc_tx_timer) {
848 if (--sc->sc_tx_timer == 0) {
849 printf("%s: device timeout\n", ifp->if_xname);
850 ifp->if_oerrors++;
851 wi_init(ifp);
852 return;
853 }
854 ifp->if_timer = 1;
855 }
856
857 if (sc->sc_scan_timer) {
858 if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
859 sc->sc_firmware_type == WI_INTERSIL) {
860 DPRINTF(("wi_watchdog: inquire scan\n"));
861 wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
862 }
863 if (sc->sc_scan_timer)
864 ifp->if_timer = 1;
865 }
866
867 /* TODO: rate control */
868 ieee80211_watchdog(ifp);
869 }
870
871 static int
872 wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
873 {
874 struct wi_softc *sc = ifp->if_softc;
875 struct ieee80211com *ic = &sc->sc_ic;
876 struct ifreq *ifr = (struct ifreq *)data;
877 int s, error = 0;
878
879 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
880 return ENXIO;
881
882 s = splnet();
883
884 switch (cmd) {
885 case SIOCSIFFLAGS:
886 if (ifp->if_flags & IFF_UP) {
887 if (sc->sc_enabled) {
888 /*
889 * To avoid rescanning another access point,
890 * do not call wi_init() here. Instead,
891 * only reflect promisc mode settings.
892 */
893 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
894 (ifp->if_flags & IFF_PROMISC) != 0)
895 wi_write_val(sc, WI_RID_PROMISC, 1);
896 else
897 wi_write_val(sc, WI_RID_PROMISC, 0);
898 } else
899 error = wi_init(ifp);
900 } else if (sc->sc_enabled)
901 wi_stop(ifp, 1);
902 break;
903 case SIOCSIFMEDIA:
904 case SIOCGIFMEDIA:
905 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
906 break;
907 case SIOCADDMULTI:
908 case SIOCDELMULTI:
909 error = (cmd == SIOCADDMULTI) ?
910 ether_addmulti(ifr, &sc->sc_ic.ic_ec) :
911 ether_delmulti(ifr, &sc->sc_ic.ic_ec);
912 if (error == ENETRESET) {
913 if (sc->sc_enabled) {
914 /* do not rescan */
915 error = wi_write_multi(sc);
916 } else
917 error = 0;
918 }
919 break;
920 case SIOCGIFGENERIC:
921 error = wi_get_cfg(ifp, cmd, data);
922 break;
923 case SIOCSIFGENERIC:
924 error = suser(curproc->p_ucred, &curproc->p_acflag);
925 if (error)
926 break;
927 error = wi_set_cfg(ifp, cmd, data);
928 if (error == ENETRESET) {
929 if (sc->sc_enabled)
930 error = wi_init(ifp);
931 else
932 error = 0;
933 }
934 break;
935 default:
936 error = ieee80211_ioctl(ifp, cmd, data);
937 if (error == ENETRESET) {
938 if (sc->sc_enabled)
939 error = wi_init(ifp);
940 else
941 error = 0;
942 }
943 break;
944 }
945 splx(s);
946 return error;
947 }
948
949 static int
950 wi_media_change(struct ifnet *ifp)
951 {
952 struct wi_softc *sc = ifp->if_softc;
953 struct ieee80211com *ic = &sc->sc_ic;
954 struct ifmedia_entry *ime;
955 enum ieee80211_opmode newmode;
956 int i, rate, error = 0;
957
958 ime = sc->sc_media.ifm_cur;
959 if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) {
960 i = -1;
961 } else {
962 rate = ieee80211_media2rate(ime->ifm_media, IEEE80211_T_DS);
963 if (rate == 0)
964 return EINVAL;
965 for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
966 if ((ic->ic_sup_rates[i] & IEEE80211_RATE_VAL) == rate)
967 break;
968 }
969 if (i == IEEE80211_RATE_SIZE)
970 return EINVAL;
971 }
972 if (ic->ic_fixed_rate != i) {
973 ic->ic_fixed_rate = i;
974 error = ENETRESET;
975 }
976
977 if ((ime->ifm_media & IFM_IEEE80211_ADHOC) &&
978 (ime->ifm_media & IFM_FLAG0))
979 newmode = IEEE80211_M_AHDEMO;
980 else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
981 newmode = IEEE80211_M_IBSS;
982 else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
983 newmode = IEEE80211_M_HOSTAP;
984 else
985 newmode = IEEE80211_M_STA;
986 if (ic->ic_opmode != newmode) {
987 ic->ic_opmode = newmode;
988 error = ENETRESET;
989 }
990 if (error == ENETRESET) {
991 if (sc->sc_enabled)
992 error = wi_init(ifp);
993 else
994 error = 0;
995 }
996 ifp->if_baudrate = ifmedia_baudrate(sc->sc_media.ifm_cur->ifm_media);
997
998 return error;
999 }
1000
1001 static void
1002 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1003 {
1004 struct wi_softc *sc = ifp->if_softc;
1005 struct ieee80211com *ic = &sc->sc_ic;
1006 u_int16_t val;
1007 int rate, len;
1008
1009 if (sc->sc_enabled == 0) {
1010 imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
1011 imr->ifm_status = 0;
1012 return;
1013 }
1014
1015 imr->ifm_status = IFM_AVALID;
1016 imr->ifm_active = IFM_IEEE80211;
1017 if (ic->ic_state == IEEE80211_S_RUN &&
1018 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
1019 imr->ifm_status |= IFM_ACTIVE;
1020 len = sizeof(val);
1021 if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) != 0)
1022 rate = 0;
1023 else {
1024 /* convert to 802.11 rate */
1025 rate = val * 2;
1026 if (sc->sc_firmware_type == WI_LUCENT) {
1027 if (rate == 10)
1028 rate = 11; /* 5.5Mbps */
1029 } else {
1030 if (rate == 4*2)
1031 rate = 11; /* 5.5Mbps */
1032 else if (rate == 8*2)
1033 rate = 22; /* 11Mbps */
1034 }
1035 }
1036 imr->ifm_active |= ieee80211_rate2media(rate, IEEE80211_T_DS);
1037 switch (ic->ic_opmode) {
1038 case IEEE80211_M_STA:
1039 break;
1040 case IEEE80211_M_IBSS:
1041 imr->ifm_active |= IFM_IEEE80211_ADHOC;
1042 break;
1043 case IEEE80211_M_AHDEMO:
1044 imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1045 break;
1046 case IEEE80211_M_HOSTAP:
1047 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1048 break;
1049 }
1050 }
1051
1052 static void
1053 wi_rx_intr(struct wi_softc *sc)
1054 {
1055 struct ieee80211com *ic = &sc->sc_ic;
1056 struct ifnet *ifp = &ic->ic_if;
1057 struct wi_frame frmhdr;
1058 struct mbuf *m;
1059 struct ieee80211_frame *wh;
1060 int fid, len, off, rssi;
1061 u_int16_t status;
1062 u_int32_t rstamp;
1063
1064 fid = CSR_READ_2(sc, WI_RX_FID);
1065
1066 /* First read in the frame header */
1067 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1068 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1069 ifp->if_ierrors++;
1070 DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1071 return;
1072 }
1073
1074 /*
1075 * Drop undecryptable or packets with receive errors here
1076 */
1077 status = le16toh(frmhdr.wi_status);
1078 if (status & WI_STAT_ERRSTAT) {
1079 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1080 ifp->if_ierrors++;
1081 DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1082 return;
1083 }
1084 rssi = frmhdr.wi_rx_signal;
1085 rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1086 le16toh(frmhdr.wi_rx_tstamp1);
1087
1088 len = le16toh(frmhdr.wi_dat_len);
1089 off = ALIGN(sizeof(struct ieee80211_frame));
1090
1091 MGETHDR(m, M_DONTWAIT, MT_DATA);
1092 if (m == NULL) {
1093 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1094 ifp->if_ierrors++;
1095 DPRINTF(("wi_rx_intr: MGET failed\n"));
1096 return;
1097 }
1098 if (off + len > MHLEN) {
1099 MCLGET(m, M_DONTWAIT);
1100 if ((m->m_flags & M_EXT) == 0) {
1101 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1102 m_freem(m);
1103 ifp->if_ierrors++;
1104 DPRINTF(("wi_rx_intr: MCLGET failed\n"));
1105 return;
1106 }
1107 }
1108
1109 m->m_data += off - sizeof(struct ieee80211_frame);
1110 memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1111 wi_read_bap(sc, fid, sizeof(frmhdr),
1112 m->m_data + sizeof(struct ieee80211_frame), len);
1113 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1114 m->m_pkthdr.rcvif = ifp;
1115
1116 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1117
1118 #if NBPFILTER > 0
1119 if (sc->sc_drvbpf) {
1120 struct mbuf mb;
1121
1122 M_COPY_PKTHDR(&mb, m);
1123 mb.m_data = (caddr_t)&frmhdr;
1124 mb.m_len = sizeof(frmhdr);
1125 mb.m_next = m;
1126 mb.m_pkthdr.len += mb.m_len;
1127 bpf_mtap(sc->sc_drvbpf, &mb);
1128 }
1129 #endif
1130 wh = mtod(m, struct ieee80211_frame *);
1131 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1132 /*
1133 * WEP is decrypted by hardware. Clear WEP bit
1134 * header for ieee80211_input().
1135 */
1136 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1137 }
1138 ieee80211_input(ifp, m, rssi, rstamp);
1139 }
1140
1141 static void
1142 wi_tx_intr(struct wi_softc *sc)
1143 {
1144 struct ieee80211com *ic = &sc->sc_ic;
1145 struct ifnet *ifp = &ic->ic_if;
1146 int fid, cur;
1147
1148 fid = CSR_READ_2(sc, WI_ALLOC_FID);
1149 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1150
1151 cur = sc->sc_txcur;
1152 if (sc->sc_txd[cur].d_fid != fid) {
1153 printf("%s: bad alloc %x != %x, cur %d nxt %d\n",
1154 sc->sc_dev.dv_xname, fid, sc->sc_txd[cur].d_fid, cur,
1155 sc->sc_txnext);
1156 return;
1157 }
1158 sc->sc_tx_timer = 0;
1159 sc->sc_txd[cur].d_len = 0;
1160 sc->sc_txcur = cur = (cur + 1) % WI_NTXBUF;
1161 if (sc->sc_txd[cur].d_len == 0)
1162 ifp->if_flags &= ~IFF_OACTIVE;
1163 else {
1164 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1165 0, 0)) {
1166 printf("%s: xmit failed\n", sc->sc_dev.dv_xname);
1167 sc->sc_txd[cur].d_len = 0;
1168 } else {
1169 sc->sc_tx_timer = 5;
1170 ifp->if_timer = 1;
1171 }
1172 }
1173 }
1174
1175 static void
1176 wi_info_intr(struct wi_softc *sc)
1177 {
1178 struct ieee80211com *ic = &sc->sc_ic;
1179 struct ifnet *ifp = &ic->ic_if;
1180 int i, fid, len, off;
1181 u_int16_t ltbuf[2];
1182 u_int16_t stat;
1183 u_int32_t *ptr;
1184
1185 fid = CSR_READ_2(sc, WI_INFO_FID);
1186 wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
1187
1188 switch (le16toh(ltbuf[1])) {
1189
1190 case WI_INFO_LINK_STAT:
1191 wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1192 DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1193 switch (le16toh(stat)) {
1194 case CONNECTED:
1195 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1196 if (ic->ic_state == IEEE80211_S_RUN)
1197 break;
1198 /* FALLTHROUGH */
1199 case AP_CHANGE:
1200 ieee80211_new_state(ifp, IEEE80211_S_RUN, -1);
1201 break;
1202 case AP_IN_RANGE:
1203 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1204 break;
1205 case AP_OUT_OF_RANGE:
1206 if (sc->sc_firmware_type == WI_SYMBOL &&
1207 sc->sc_scan_timer > 0) {
1208 if (wi_cmd(sc, WI_CMD_INQUIRE,
1209 WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
1210 sc->sc_scan_timer = 0;
1211 break;
1212 }
1213 if (ic->ic_opmode == IEEE80211_M_STA)
1214 sc->sc_flags |= WI_FLAGS_OUTRANGE;
1215 break;
1216 case DISCONNECTED:
1217 case ASSOC_FAILED:
1218 if (ic->ic_opmode == IEEE80211_M_STA)
1219 ieee80211_new_state(ifp, IEEE80211_S_INIT, -1);
1220 break;
1221 }
1222 break;
1223
1224 case WI_INFO_COUNTERS:
1225 /* some card versions have a larger stats structure */
1226 len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1227 ptr = (u_int32_t *)&sc->sc_stats;
1228 off = sizeof(ltbuf);
1229 for (i = 0; i < len; i++, off += 2, ptr++) {
1230 wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1231 #ifdef WI_HERMES_STATS_WAR
1232 if (stat & 0xf000)
1233 stat = ~stat;
1234 #endif
1235 *ptr += stat;
1236 }
1237 ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1238 sc->sc_stats.wi_tx_multi_retries +
1239 sc->sc_stats.wi_tx_retry_limit;
1240 break;
1241
1242 case WI_INFO_SCAN_RESULTS:
1243 case WI_INFO_HOST_SCAN_RESULTS:
1244 wi_scan_result(sc, fid, le16toh(ltbuf[0]));
1245 break;
1246
1247 default:
1248 DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1249 le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1250 break;
1251 }
1252 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1253 }
1254
1255 /*
1256 * Allocate a region of memory inside the NIC and zero
1257 * it out.
1258 */
1259 static int
1260 wi_write_multi(struct wi_softc *sc)
1261 {
1262 struct ifnet *ifp = &sc->sc_ic.ic_if;
1263 int n = 0;
1264 struct wi_mcast mlist;
1265 struct ether_multi *enm;
1266 struct ether_multistep estep;
1267
1268 if ((ifp->if_flags & IFF_PROMISC) != 0) {
1269 allmulti:
1270 ifp->if_flags |= IFF_ALLMULTI;
1271 memset(&mlist, 0, sizeof(mlist));
1272 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1273 sizeof(mlist));
1274 }
1275
1276 n = 0;
1277 ETHER_FIRST_MULTI(estep, &sc->sc_ic.ic_ec, enm);
1278 while (enm != NULL) {
1279 /* Punt on ranges or too many multicast addresses. */
1280 if (!IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi) ||
1281 n >= sizeof(mlist) / sizeof(mlist.wi_mcast[0]))
1282 goto allmulti;
1283
1284 IEEE80211_ADDR_COPY(&mlist.wi_mcast[n], enm->enm_addrlo);
1285 n++;
1286 ETHER_NEXT_MULTI(estep, enm);
1287 }
1288 ifp->if_flags &= ~IFF_ALLMULTI;
1289 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1290 IEEE80211_ADDR_LEN * n);
1291 }
1292
1293
1294 static void
1295 wi_read_nicid(sc)
1296 struct wi_softc *sc;
1297 {
1298 struct wi_card_ident *id;
1299 char *p;
1300 int len;
1301 u_int16_t ver[4];
1302
1303 /* getting chip identity */
1304 memset(ver, 0, sizeof(ver));
1305 len = sizeof(ver);
1306 wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1307 printf("%s: using ", sc->sc_dev.dv_xname);
1308 DPRINTF2(("wi_read_nicid: CARD_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3])));
1309
1310 sc->sc_firmware_type = WI_NOTYPE;
1311 for (id = wi_card_ident; id->card_name != NULL; id++) {
1312 if (le16toh(ver[0]) == id->card_id) {
1313 printf("%s", id->card_name);
1314 sc->sc_firmware_type = id->firm_type;
1315 break;
1316 }
1317 }
1318 if (sc->sc_firmware_type == WI_NOTYPE) {
1319 if (le16toh(ver[0]) & 0x8000) {
1320 printf("Unknown PRISM2 chip");
1321 sc->sc_firmware_type = WI_INTERSIL;
1322 } else {
1323 printf("Unknown Lucent chip");
1324 sc->sc_firmware_type = WI_LUCENT;
1325 }
1326 }
1327
1328 /* get primary firmware version (Only Prism chips) */
1329 if (sc->sc_firmware_type != WI_LUCENT) {
1330 memset(ver, 0, sizeof(ver));
1331 len = sizeof(ver);
1332 wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1333 sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1334 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1335 DPRINTF2(("wi_read_nicid: PRI_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3])));
1336 }
1337
1338 /* get station firmware version */
1339 memset(ver, 0, sizeof(ver));
1340 len = sizeof(ver);
1341 wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1342 sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1343 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1344 DPRINTF2(("wi_read_nicid: STA_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3])));
1345 if (sc->sc_firmware_type == WI_INTERSIL &&
1346 (sc->sc_sta_firmware_ver == 10102 ||
1347 sc->sc_sta_firmware_ver == 20102)) {
1348 char ident[12];
1349 memset(ident, 0, sizeof(ident));
1350 len = sizeof(ident);
1351 /* value should be the format like "V2.00-11" */
1352 if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1353 *(p = (char *)ident) >= 'A' &&
1354 p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1355 sc->sc_firmware_type = WI_SYMBOL;
1356 sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1357 (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1358 (p[6] - '0') * 10 + (p[7] - '0');
1359 }
1360 DPRINTF2(("wi_read_nicid: SYMBOL_ID: %x %x %x %x\n", le16toh(ident[0]), le16toh(ident[1]), le16toh(ident[2]), le16toh(ident[3])));
1361 }
1362
1363 printf("\n%s: %s Firmware: ", sc->sc_dev.dv_xname,
1364 sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
1365 (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
1366 if (sc->sc_firmware_type != WI_LUCENT) /* XXX */
1367 printf("Primary (%u.%u.%u), ",
1368 sc->sc_pri_firmware_ver / 10000,
1369 (sc->sc_pri_firmware_ver % 10000) / 100,
1370 sc->sc_pri_firmware_ver % 100);
1371 printf("Station (%u.%u.%u)\n",
1372 sc->sc_sta_firmware_ver / 10000,
1373 (sc->sc_sta_firmware_ver % 10000) / 100,
1374 sc->sc_sta_firmware_ver % 100);
1375 }
1376
1377 static int
1378 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1379 {
1380 struct wi_ssid ssid;
1381
1382 if (buflen > IEEE80211_NWID_LEN)
1383 return ENOBUFS;
1384 memset(&ssid, 0, sizeof(ssid));
1385 ssid.wi_len = htole16(buflen);
1386 memcpy(ssid.wi_ssid, buf, buflen);
1387 return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1388 }
1389
1390 static int
1391 wi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
1392 {
1393 struct wi_softc *sc = ifp->if_softc;
1394 struct ieee80211com *ic = &sc->sc_ic;
1395 struct ifreq *ifr = (struct ifreq *)data;
1396 struct wi_req wreq;
1397 int len, n, error;
1398
1399 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1400 if (error)
1401 return error;
1402 len = (wreq.wi_len - 1) * 2;
1403 if (len < sizeof(u_int16_t))
1404 return ENOSPC;
1405 if (len > sizeof(wreq.wi_val))
1406 len = sizeof(wreq.wi_val);
1407
1408 switch (wreq.wi_type) {
1409
1410 case WI_RID_IFACE_STATS:
1411 memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
1412 if (len < sizeof(sc->sc_stats))
1413 error = ENOSPC;
1414 else
1415 len = sizeof(sc->sc_stats);
1416 break;
1417
1418 case WI_RID_ENCRYPTION:
1419 case WI_RID_TX_CRYPT_KEY:
1420 case WI_RID_DEFLT_CRYPT_KEYS:
1421 return ieee80211_cfgget(ifp, cmd, data);
1422
1423 case WI_RID_TX_RATE:
1424 if (ic->ic_fixed_rate < 0)
1425 wreq.wi_val[0] = htole16(3); /*XXX*/
1426 else
1427 wreq.wi_val[0] = htole16(
1428 (ic->ic_sup_rates[ic->ic_fixed_rate] &
1429 IEEE80211_RATE_VAL) / 2);
1430 len = sizeof(u_int16_t);
1431 break;
1432
1433 case WI_RID_MICROWAVE_OVEN:
1434 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
1435 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1436 &len);
1437 break;
1438 }
1439 wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
1440 len = sizeof(u_int16_t);
1441 break;
1442
1443 case WI_RID_ROAMING_MODE:
1444 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
1445 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1446 &len);
1447 break;
1448 }
1449 wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
1450 len = sizeof(u_int16_t);
1451 break;
1452
1453 case WI_RID_SYSTEM_SCALE:
1454 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
1455 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1456 &len);
1457 break;
1458 }
1459 wreq.wi_val[0] = htole16(sc->sc_system_scale);
1460 len = sizeof(u_int16_t);
1461 break;
1462
1463 case WI_RID_READ_APS:
1464 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1465 return ieee80211_cfgget(ifp, cmd, data);
1466 if (sc->sc_scan_timer > 0) {
1467 error = EINPROGRESS;
1468 break;
1469 }
1470 n = sc->sc_naps;
1471 if (len < sizeof(n)) {
1472 error = ENOSPC;
1473 break;
1474 }
1475 if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
1476 n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
1477 len = sizeof(n) + sizeof(struct wi_apinfo) * n;
1478 memcpy(wreq.wi_val, &n, sizeof(n));
1479 memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
1480 sizeof(struct wi_apinfo) * n);
1481 break;
1482
1483 default:
1484 if (sc->sc_enabled) {
1485 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1486 &len);
1487 break;
1488 }
1489 switch (wreq.wi_type) {
1490 case WI_RID_MAX_DATALEN:
1491 wreq.wi_val[0] = htole16(sc->sc_max_datalen);
1492 len = sizeof(u_int16_t);
1493 break;
1494 case WI_RID_RTS_THRESH:
1495 wreq.wi_val[0] = htole16(sc->sc_rts_thresh);
1496 len = sizeof(u_int16_t);
1497 break;
1498 case WI_RID_CNFAUTHMODE:
1499 wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
1500 len = sizeof(u_int16_t);
1501 break;
1502 case WI_RID_NODENAME:
1503 if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
1504 error = ENOSPC;
1505 break;
1506 }
1507 len = sc->sc_nodelen + sizeof(u_int16_t);
1508 wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
1509 memcpy(&wreq.wi_val[1], sc->sc_nodename,
1510 sc->sc_nodelen);
1511 break;
1512 default:
1513 return ieee80211_cfgget(ifp, cmd, data);
1514 }
1515 break;
1516 }
1517 if (error)
1518 return error;
1519 wreq.wi_len = (len + 1) / 2 + 1;
1520 return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
1521 }
1522
1523 static int
1524 wi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
1525 {
1526 struct wi_softc *sc = ifp->if_softc;
1527 struct ieee80211com *ic = &sc->sc_ic;
1528 struct ifreq *ifr = (struct ifreq *)data;
1529 struct wi_req wreq;
1530 struct mbuf *m;
1531 int i, len, error;
1532
1533 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1534 if (error)
1535 return error;
1536 len = (wreq.wi_len - 1) * 2;
1537 switch (wreq.wi_type) {
1538 case WI_RID_NODENAME:
1539 if (le16toh(wreq.wi_val[0]) * 2 > len ||
1540 le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
1541 error = ENOSPC;
1542 break;
1543 }
1544 if (sc->sc_enabled) {
1545 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
1546 len);
1547 if (error)
1548 break;
1549 }
1550 sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
1551 memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen);
1552 break;
1553
1554 case WI_RID_MICROWAVE_OVEN:
1555 case WI_RID_ROAMING_MODE:
1556 case WI_RID_SYSTEM_SCALE:
1557 if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
1558 (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
1559 break;
1560 if (wreq.wi_type == WI_RID_ROAMING_MODE &&
1561 (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
1562 break;
1563 if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
1564 (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
1565 break;
1566 /* FALLTHROUGH */
1567 case WI_RID_RTS_THRESH:
1568 case WI_RID_CNFAUTHMODE:
1569 case WI_RID_MAX_DATALEN:
1570 if (sc->sc_enabled) {
1571 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
1572 sizeof(u_int16_t));
1573 if (error)
1574 break;
1575 }
1576 switch (wreq.wi_type) {
1577 case WI_RID_RTS_THRESH:
1578 sc->sc_rts_thresh = le16toh(wreq.wi_val[0]);
1579 break;
1580 case WI_RID_MICROWAVE_OVEN:
1581 sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
1582 break;
1583 case WI_RID_ROAMING_MODE:
1584 sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
1585 break;
1586 case WI_RID_SYSTEM_SCALE:
1587 sc->sc_system_scale = le16toh(wreq.wi_val[0]);
1588 break;
1589 case WI_RID_CNFAUTHMODE:
1590 sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
1591 break;
1592 case WI_RID_MAX_DATALEN:
1593 sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
1594 break;
1595 }
1596 break;
1597
1598 case WI_RID_TX_RATE:
1599 switch (le16toh(wreq.wi_val[0])) {
1600 case 3:
1601 ic->ic_fixed_rate = -1;
1602 break;
1603 default:
1604 for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
1605 if ((ic->ic_sup_rates[i] & IEEE80211_RATE_VAL)
1606 / 2 == le16toh(wreq.wi_val[0]))
1607 break;
1608 }
1609 if (i == IEEE80211_RATE_SIZE)
1610 return EINVAL;
1611 ic->ic_fixed_rate = i;
1612 }
1613 if (sc->sc_enabled)
1614 error = wi_write_txrate(sc);
1615 break;
1616
1617 case WI_RID_SCAN_APS:
1618 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
1619 error = wi_scan_ap(sc);
1620 break;
1621
1622 case WI_RID_MGMT_XMIT:
1623 if (!sc->sc_enabled) {
1624 error = ENETDOWN;
1625 break;
1626 }
1627 if (ic->ic_mgtq.ifq_len > 5) {
1628 error = EAGAIN;
1629 break;
1630 }
1631 /* XXX wi_len looks in u_int8_t, not in u_int16_t */
1632 m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL);
1633 if (m == NULL) {
1634 error = ENOMEM;
1635 break;
1636 }
1637 IF_ENQUEUE(&ic->ic_mgtq, m);
1638 break;
1639
1640 default:
1641 if (sc->sc_enabled) {
1642 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
1643 len);
1644 if (error)
1645 break;
1646 }
1647 error = ieee80211_cfgset(ifp, cmd, data);
1648 break;
1649 }
1650 return error;
1651 }
1652
1653 static int
1654 wi_write_txrate(struct wi_softc *sc)
1655 {
1656 struct ieee80211com *ic = &sc->sc_ic;
1657 int i;
1658 u_int16_t rate;
1659
1660 if (ic->ic_fixed_rate < 0)
1661 rate = 0; /* auto */
1662 else
1663 rate = (ic->ic_sup_rates[ic->ic_fixed_rate] &
1664 IEEE80211_RATE_VAL) / 2;
1665
1666 /* rate: 0, 1, 2, 5, 11 */
1667
1668 switch (sc->sc_firmware_type) {
1669 case WI_LUCENT:
1670 if (rate == 0)
1671 rate = 3; /* auto */
1672 break;
1673 default:
1674 for (i = 8; i > 0; i >>= 1) {
1675 if (rate >= i)
1676 break;
1677 }
1678 if (i == 0)
1679 rate = 0xf; /* auto */
1680 else
1681 rate = i;
1682 break;
1683 }
1684 return wi_write_val(sc, WI_RID_TX_RATE, rate);
1685 }
1686
1687 static int
1688 wi_write_wep(struct wi_softc *sc)
1689 {
1690 struct ieee80211com *ic = &sc->sc_ic;
1691 int error = 0;
1692 int i, keylen;
1693 u_int16_t val;
1694 struct wi_key wkey[IEEE80211_WEP_NKID];
1695
1696 switch (sc->sc_firmware_type) {
1697 case WI_LUCENT:
1698 val = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0;
1699 error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
1700 if (error)
1701 break;
1702 error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_wep_txkey);
1703 if (error)
1704 break;
1705 memset(wkey, 0, sizeof(wkey));
1706 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1707 keylen = ic->ic_nw_keys[i].wk_len;
1708 wkey[i].wi_keylen = htole16(keylen);
1709 memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
1710 keylen);
1711 }
1712 error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
1713 wkey, sizeof(wkey));
1714 break;
1715
1716 case WI_INTERSIL:
1717 case WI_SYMBOL:
1718 if (ic->ic_flags & IEEE80211_F_WEPON) {
1719 /*
1720 * ONLY HWB3163 EVAL-CARD Firmware version
1721 * less than 0.8 variant2
1722 *
1723 * If promiscuous mode disable, Prism2 chip
1724 * does not work with WEP .
1725 * It is under investigation for details.
1726 * (ichiro (at) netbsd.org)
1727 */
1728 if (sc->sc_firmware_type == WI_INTERSIL &&
1729 sc->sc_sta_firmware_ver < 802 ) {
1730 /* firm ver < 0.8 variant 2 */
1731 wi_write_val(sc, WI_RID_PROMISC, 1);
1732 }
1733 wi_write_val(sc, WI_RID_CNFAUTHMODE,
1734 sc->sc_cnfauthmode);
1735 val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
1736 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1737 val |= HOST_ENCRYPT;
1738 } else {
1739 wi_write_val(sc, WI_RID_CNFAUTHMODE, 1); /* open */
1740 val = HOST_ENCRYPT | HOST_DECRYPT;
1741 }
1742 error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
1743 if (error)
1744 break;
1745 error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
1746 ic->ic_wep_txkey);
1747 if (error)
1748 break;
1749 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1750 keylen = ic->ic_nw_keys[i].wk_len;
1751 if (keylen > IEEE80211_WEP_KEYLEN)
1752 keylen = 13; /* 104bit keys */
1753 else
1754 keylen = IEEE80211_WEP_KEYLEN;
1755 error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
1756 ic->ic_nw_keys[i].wk_key, keylen);
1757 if (error)
1758 break;
1759 }
1760 break;
1761 }
1762 return error;
1763 }
1764
1765 /* Must be called at proper protection level! */
1766 static int
1767 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
1768 {
1769 int i, status;
1770
1771 /* wait for the busy bit to clear */
1772 for (i = 0; ; i++) {
1773 if ((CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY) == 0)
1774 break;
1775 if (i == WI_TIMEOUT) {
1776 printf("%s: wi_cmd: BUSY did not clear, "
1777 "cmd=0x%x, prev=0x%x\n", sc->sc_dev.dv_xname,
1778 cmd, CSR_READ_2(sc, WI_COMMAND));
1779 return EIO;
1780 }
1781 DELAY(1);
1782 }
1783
1784 CSR_WRITE_2(sc, WI_PARAM0, val0);
1785 CSR_WRITE_2(sc, WI_PARAM1, val1);
1786 CSR_WRITE_2(sc, WI_PARAM2, val2);
1787 CSR_WRITE_2(sc, WI_COMMAND, cmd);
1788
1789 if (cmd == WI_CMD_INI) {
1790 /* XXX: should sleep here. */
1791 DELAY(100*1000);
1792 }
1793 /* wait for the cmd completed bit */
1794 for (i = 0; i < WI_TIMEOUT; i++) {
1795 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
1796 break;
1797 DELAY(1);
1798 }
1799
1800 status = CSR_READ_2(sc, WI_STATUS);
1801
1802 /* Ack the command */
1803 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
1804
1805 if (i == WI_TIMEOUT) {
1806 printf("%s: command timed out, cmd=0x%x, arg=0x%x\n",
1807 sc->sc_dev.dv_xname, cmd, val0);
1808 return ETIMEDOUT;
1809 }
1810
1811 if (status & WI_STAT_CMD_RESULT) {
1812 printf("%s: command failed, cmd=0x%x, arg=0x%x\n",
1813 sc->sc_dev.dv_xname, cmd, val0);
1814 return EIO;
1815 }
1816 return 0;
1817 }
1818
1819 static int
1820 wi_seek_bap(struct wi_softc *sc, int id, int off)
1821 {
1822 int i, status;
1823
1824 CSR_WRITE_2(sc, WI_SEL0, id);
1825 CSR_WRITE_2(sc, WI_OFF0, off);
1826
1827 for (i = 0; ; i++) {
1828 status = CSR_READ_2(sc, WI_OFF0);
1829 if ((status & WI_OFF_BUSY) == 0)
1830 break;
1831 if (i == WI_TIMEOUT) {
1832 printf("%s: timeout in wi_seek to %x/%x\n",
1833 sc->sc_dev.dv_xname, id, off);
1834 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
1835 return ETIMEDOUT;
1836 }
1837 DELAY(1);
1838 }
1839 if (status & WI_OFF_ERR) {
1840 printf("%s: failed in wi_seek to %x/%x\n",
1841 sc->sc_dev.dv_xname, id, off);
1842 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
1843 return EIO;
1844 }
1845 sc->sc_bap_id = id;
1846 sc->sc_bap_off = off;
1847 return 0;
1848 }
1849
1850 static int
1851 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
1852 {
1853 int error, cnt;
1854
1855 if (buflen == 0)
1856 return 0;
1857 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1858 if ((error = wi_seek_bap(sc, id, off)) != 0)
1859 return error;
1860 }
1861 cnt = (buflen + 1) / 2;
1862 CSR_READ_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
1863 sc->sc_bap_off += cnt * 2;
1864 return 0;
1865 }
1866
1867 static int
1868 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
1869 {
1870 int error, cnt;
1871
1872 if (buflen == 0)
1873 return 0;
1874
1875 #ifdef WI_HERMES_AUTOINC_WAR
1876 again:
1877 #endif
1878 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1879 if ((error = wi_seek_bap(sc, id, off)) != 0)
1880 return error;
1881 }
1882 cnt = (buflen + 1) / 2;
1883 CSR_WRITE_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
1884 sc->sc_bap_off += cnt * 2;
1885
1886 #ifdef WI_HERMES_AUTOINC_WAR
1887 /*
1888 * According to the comments in the HCF Light code, there is a bug
1889 * in the Hermes (or possibly in certain Hermes firmware revisions)
1890 * where the chip's internal autoincrement counter gets thrown off
1891 * during data writes: the autoincrement is missed, causing one
1892 * data word to be overwritten and subsequent words to be written to
1893 * the wrong memory locations. The end result is that we could end
1894 * up transmitting bogus frames without realizing it. The workaround
1895 * for this is to write a couple of extra guard words after the end
1896 * of the transfer, then attempt to read then back. If we fail to
1897 * locate the guard words where we expect them, we preform the
1898 * transfer over again.
1899 */
1900 if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
1901 CSR_WRITE_2(sc, WI_DATA0, 0x1234);
1902 CSR_WRITE_2(sc, WI_DATA0, 0x5678);
1903 wi_seek_bap(sc, id, sc->sc_bap_off);
1904 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
1905 if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
1906 CSR_READ_2(sc, WI_DATA0) != 0x5678) {
1907 printf("%s: detect auto increment bug, try again\n",
1908 sc->sc_dev.dv_xname);
1909 goto again;
1910 }
1911 }
1912 #endif
1913 return 0;
1914 }
1915
1916 static int
1917 wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
1918 {
1919 int i;
1920
1921 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
1922 printf("%s: failed to allocate %d bytes on NIC\n",
1923 sc->sc_dev.dv_xname, len);
1924 return ENOMEM;
1925 }
1926
1927 for (i = 0; i < WI_TIMEOUT; i++) {
1928 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
1929 break;
1930 if (i == WI_TIMEOUT) {
1931 printf("%s: timeout in alloc\n", sc->sc_dev.dv_xname);
1932 return ETIMEDOUT;
1933 }
1934 DELAY(1);
1935 }
1936 *idp = CSR_READ_2(sc, WI_ALLOC_FID);
1937 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1938 return 0;
1939 }
1940
1941 static int
1942 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
1943 {
1944 int error, len;
1945 u_int16_t ltbuf[2];
1946
1947 /* Tell the NIC to enter record read mode. */
1948 error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
1949 if (error)
1950 return error;
1951
1952 error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
1953 if (error)
1954 return error;
1955
1956 if (le16toh(ltbuf[1]) != rid) {
1957 printf("%s: record read mismatch, rid=%x, got=%x\n",
1958 sc->sc_dev.dv_xname, rid, le16toh(ltbuf[1]));
1959 return EIO;
1960 }
1961 len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */
1962 if (*buflenp < len) {
1963 printf("%s: record buffer is too small, "
1964 "rid=%x, size=%d, len=%d\n",
1965 sc->sc_dev.dv_xname, rid, *buflenp, len);
1966 return ENOSPC;
1967 }
1968 *buflenp = len;
1969 return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
1970 }
1971
1972 static int
1973 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
1974 {
1975 int error;
1976 u_int16_t ltbuf[2];
1977
1978 ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */
1979 ltbuf[1] = htole16(rid);
1980
1981 error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
1982 if (error)
1983 return error;
1984 error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
1985 if (error)
1986 return error;
1987
1988 return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
1989 }
1990
1991 static int
1992 wi_newstate(void *arg, enum ieee80211_state nstate)
1993 {
1994 struct wi_softc *sc = arg;
1995 struct ieee80211com *ic = &sc->sc_ic;
1996 struct ieee80211_node *ni = &ic->ic_bss;
1997 int i, buflen;
1998 u_int16_t val;
1999 struct wi_ssid ssid;
2000 enum ieee80211_state ostate;
2001 #ifdef WI_DEBUG
2002 static const char *stname[] =
2003 { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" };
2004 #endif /* WI_DEBUG */
2005
2006 ostate = ic->ic_state;
2007 DPRINTF(("wi_newstate: %s -> %s\n", stname[ostate], stname[nstate]));
2008
2009 ic->ic_state = nstate;
2010 switch (nstate) {
2011 case IEEE80211_S_INIT:
2012 ic->ic_flags &= ~IEEE80211_F_SIBSS;
2013 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2014 return 0;
2015
2016 case IEEE80211_S_RUN:
2017 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2018 buflen = IEEE80211_ADDR_LEN;
2019 wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen);
2020 IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
2021 buflen = sizeof(val);
2022 wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
2023 ni->ni_chan = le16toh(val);
2024
2025 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2026 ni->ni_esslen = ic->ic_des_esslen;
2027 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
2028 ni->ni_nrate = 0;
2029 for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
2030 if (ic->ic_sup_rates[i])
2031 ni->ni_rates[ni->ni_nrate++] =
2032 ic->ic_sup_rates[i];
2033 }
2034 ni->ni_intval = ic->ic_lintval;
2035 ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
2036 if (ic->ic_flags & IEEE80211_F_WEPON)
2037 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
2038 } else {
2039 buflen = sizeof(ssid);
2040 wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
2041 ni->ni_esslen = le16toh(ssid.wi_len);
2042 if (ni->ni_esslen > IEEE80211_NWID_LEN)
2043 ni->ni_esslen = IEEE80211_NWID_LEN; /*XXX*/
2044 memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
2045 }
2046 break;
2047
2048 case IEEE80211_S_SCAN:
2049 case IEEE80211_S_AUTH:
2050 case IEEE80211_S_ASSOC:
2051 break;
2052 }
2053
2054 /* skip standard ieee80211 handling */
2055 return EINPROGRESS;
2056 }
2057
2058 static int
2059 wi_scan_ap(struct wi_softc *sc)
2060 {
2061 int error = 0;
2062 u_int16_t val[2];
2063
2064 if (!sc->sc_enabled)
2065 return ENXIO;
2066 switch (sc->sc_firmware_type) {
2067 case WI_LUCENT:
2068 (void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
2069 break;
2070 case WI_INTERSIL:
2071 val[0] = 0x3fff; /* channel */
2072 val[1] = 0x000f; /* tx rate */
2073 error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
2074 break;
2075 case WI_SYMBOL:
2076 /*
2077 * XXX only supported on 3.x ?
2078 */
2079 val[0] = BSCAN_BCAST | BSCAN_ONETIME;
2080 error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
2081 val, sizeof(val[0]));
2082 break;
2083 }
2084 if (error == 0) {
2085 sc->sc_scan_timer = WI_SCAN_WAIT;
2086 sc->sc_ic.ic_if.if_timer = 1;
2087 DPRINTF(("wi_scan_ap: start scanning\n"));
2088 }
2089 return error;
2090 }
2091
2092 static void
2093 wi_scan_result(struct wi_softc *sc, int fid, int cnt)
2094 {
2095 int i, naps, off, szbuf;
2096 struct wi_scan_header ws_hdr; /* Prism2 header */
2097 struct wi_scan_data_p2 ws_dat; /* Prism2 scantable*/
2098 struct wi_apinfo *ap;
2099
2100 off = sizeof(u_int16_t) * 2;
2101 memset(&ws_hdr, 0, sizeof(ws_hdr));
2102 switch (sc->sc_firmware_type) {
2103 case WI_INTERSIL:
2104 wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
2105 off += sizeof(ws_hdr);
2106 szbuf = sizeof(struct wi_scan_data_p2);
2107 break;
2108 case WI_SYMBOL:
2109 szbuf = sizeof(struct wi_scan_data_p2) + 6;
2110 break;
2111 case WI_LUCENT:
2112 szbuf = sizeof(struct wi_scan_data);
2113 break;
2114 }
2115 naps = (cnt * 2 + 2 - off) / szbuf;
2116 if (naps > MAXAPINFO)
2117 naps = MAXAPINFO;
2118 sc->sc_naps = naps;
2119 /* Read Data */
2120 ap = sc->sc_aps;
2121 memset(&ws_dat, 0, sizeof(ws_dat));
2122 for (i = 0; i < naps; i++, ap++) {
2123 wi_read_bap(sc, fid, off, &ws_dat,
2124 (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
2125 DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off,
2126 ether_sprintf(ws_dat.wi_bssid)));
2127 off += szbuf;
2128 ap->scanreason = le16toh(ws_hdr.wi_reason);
2129 memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
2130 ap->channel = le16toh(ws_dat.wi_chid);
2131 ap->signal = le16toh(ws_dat.wi_signal);
2132 ap->noise = le16toh(ws_dat.wi_noise);
2133 ap->quality = ap->signal - ap->noise;
2134 ap->capinfo = le16toh(ws_dat.wi_capinfo);
2135 ap->interval = le16toh(ws_dat.wi_interval);
2136 ap->rate = le16toh(ws_dat.wi_rate);
2137 ap->namelen = le16toh(ws_dat.wi_namelen);
2138 if (ap->namelen > sizeof(ap->name))
2139 ap->namelen = sizeof(ap->name);
2140 memcpy(ap->name, ws_dat.wi_name, ap->namelen);
2141 }
2142 /* Done scanning */
2143 sc->sc_scan_timer = 0;
2144 DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
2145 }
2146