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