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