1 /* $NetBSD: wi.c,v 1.109 2003/01/09 08:52:19 dyoung Exp $ */
4 * Copyright (c) 1997, 1998, 1999
5 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
34 * $FreeBSD: src/sys/dev/wi/if_wi.c,v 1.180.2.7 2005/10/05 13:16:29 avatar Exp $
35 * $DragonFly: src/sys/dev/netif/wi/if_wi.c,v 1.39 2006/12/22 23:26:22 swildner Exp $
39 * Lucent WaveLAN/IEEE 802.11 PCMCIA driver.
41 * Original FreeBSD driver written by Bill Paul <wpaul@ctr.columbia.edu>
42 * Electrical Engineering Department
43 * Columbia University, New York City
47 * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
48 * from Lucent. Unlike the older cards, the new ones are programmed
49 * entirely via a firmware-driven controller called the Hermes.
50 * Unfortunately, Lucent will not release the Hermes programming manual
51 * without an NDA (if at all). What they do release is an API library
52 * called the HCF (Hardware Control Functions) which is supposed to
53 * do the device-specific operations of a device driver for you. The
54 * publically available version of the HCF library (the 'HCF Light') is
55 * a) extremely gross, b) lacks certain features, particularly support
56 * for 802.11 frames, and c) is contaminated by the GNU Public License.
58 * This driver does not use the HCF or HCF Light at all. Instead, it
59 * programs the Hermes controller directly, using information gleaned
60 * from the HCF Light code and corresponding documentation.
62 * This driver supports the ISA, PCMCIA and PCI versions of the Lucent
63 * WaveLan cards (based on the Hermes chipset), as well as the newer
64 * Prism 2 chipsets with firmware from Intersil and Symbol.
67 #define WI_HERMES_AUTOINC_WAR /* Work around data write autoinc bug. */
68 #define WI_HERMES_STATS_WAR /* Work around stats counter bug. */
70 #include "opt_polling.h"
72 #include <sys/param.h>
73 #include <sys/endian.h>
74 #include <sys/systm.h>
75 #include <sys/sockio.h>
78 #include <sys/kernel.h>
79 #include <sys/socket.h>
80 #include <sys/module.h>
82 #include <sys/random.h>
83 #include <sys/syslog.h>
84 #include <sys/sysctl.h>
85 #include <sys/serialize.h>
87 #include <sys/thread2.h>
89 #include <machine/atomic.h>
92 #include <net/if_arp.h>
93 #include <net/ethernet.h>
94 #include <net/if_dl.h>
95 #include <net/if_media.h>
96 #include <net/if_types.h>
97 #include <net/ifq_var.h>
99 #include <netproto/802_11/ieee80211_var.h>
100 #include <netproto/802_11/ieee80211_ioctl.h>
101 #include <netproto/802_11/ieee80211_radiotap.h>
102 #include <netproto/802_11/if_wavelan_ieee.h>
104 #include <netinet/in.h>
105 #include <netinet/in_systm.h>
106 #include <netinet/in_var.h>
107 #include <netinet/ip.h>
108 #include <netinet/if_ether.h>
112 #include <dev/netif/wi/if_wireg.h>
113 #include <dev/netif/wi/if_wivar.h>
115 static void wi_start(struct ifnet *);
116 static int wi_reset(struct wi_softc *);
117 static void wi_watchdog(struct ifnet *);
118 static int wi_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
119 static int wi_media_change(struct ifnet *);
120 static void wi_media_status(struct ifnet *, struct ifmediareq *);
122 static void wi_rx_intr(struct wi_softc *);
123 static void wi_tx_intr(struct wi_softc *);
124 static void wi_tx_ex_intr(struct wi_softc *);
125 static void wi_info_intr(struct wi_softc *);
127 static int wi_get_cfg(struct ifnet *, u_long, caddr_t, struct ucred *);
128 static int wi_set_cfg(struct ifnet *, u_long, caddr_t);
129 static int wi_write_txrate(struct wi_softc *);
130 static int wi_write_wep(struct wi_softc *);
131 static int wi_write_multi(struct wi_softc *);
132 static int wi_alloc_fid(struct wi_softc *, int, int *);
133 static void wi_read_nicid(struct wi_softc *);
134 static int wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
136 static int wi_cmd(struct wi_softc *, int, int, int, int);
137 static int wi_seek_bap(struct wi_softc *, int, int);
138 static int wi_read_bap(struct wi_softc *, int, int, void *, int);
139 static int wi_write_bap(struct wi_softc *, int, int, void *, int);
140 static int wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
141 static int wi_read_rid(struct wi_softc *, int, void *, int *);
142 static int wi_write_rid(struct wi_softc *, int, void *, int);
144 static int wi_key_alloc(struct ieee80211com *, const struct ieee80211_key *,
145 ieee80211_keyix *, ieee80211_keyix *);
146 static int wi_newstate(struct ieee80211com *, enum ieee80211_state, int);
148 static int wi_scan_ap(struct wi_softc *, u_int16_t, u_int16_t);
149 static void wi_scan_result(struct wi_softc *, int, int);
151 static void wi_dump_pkt(struct wi_frame *, struct ieee80211_node *, int rssi);
153 static int wi_get_debug(struct wi_softc *, struct wi_req *);
154 static int wi_set_debug(struct wi_softc *, struct wi_req *);
156 /* support to download firmware for symbol CF card */
157 static int wi_symbol_write_firm(struct wi_softc *, const void *, int,
159 static int wi_symbol_set_hcr(struct wi_softc *, int);
160 #ifdef DEVICE_POLLING
161 static void wi_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
165 wi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
169 return wi_write_rid(sc, rid, &val, sizeof(val));
172 SYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters");
174 static struct timeval lasttxerror; /* time of last tx error msg */
175 static int curtxeps; /* current tx error msgs/sec */
176 static int wi_txerate = 0; /* tx error rate: max msgs/sec */
177 SYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate,
178 0, "max tx error msgs/sec; 0 to disable msgs");
182 static int wi_debug = 0;
183 SYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug,
184 0, "control debugging kprintfs");
186 #define DPRINTF(X) if (wi_debug) if_printf X
187 #define DPRINTF2(X) if (wi_debug > 1) if_printf X
188 #define IFF_DUMPPKTS(_ifp) \
189 (((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
193 #define IFF_DUMPPKTS(_ifp) 0
196 #define WI_INTRS (WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
198 struct wi_card_ident wi_card_ident[] = {
199 /* CARD_ID CARD_NAME FIRM_TYPE */
200 { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT },
201 { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT },
202 { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT },
203 { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL },
204 { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL },
205 { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL },
206 { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL },
207 { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL },
208 { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL },
209 { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL },
210 { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL },
211 { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL },
212 { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
213 { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
214 { WI_NIC_3842_PCMCIA_ATL_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
215 { WI_NIC_3842_PCMCIA_ATS_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
216 { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
217 { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
218 { WI_NIC_3842_MINI_ATL_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
219 { WI_NIC_3842_MINI_ATS_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
220 { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
221 { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
222 { WI_NIC_3842_PCI_ATS_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
223 { WI_NIC_3842_PCI_ATL_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
224 { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
225 { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
226 { WI_NIC_P3_PCMCIA_ATL_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
227 { WI_NIC_P3_PCMCIA_ATS_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
228 { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
229 { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
230 { WI_NIC_P3_MINI_ATL_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
231 { WI_NIC_P3_MINI_ATS_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
235 devclass_t wi_devclass;
238 wi_attach(device_t dev)
240 struct wi_softc *sc = device_get_softc(dev);
241 struct ieee80211com *ic = &sc->sc_ic;
242 struct ifnet *ifp = &ic->ic_if;
243 int i, nrates, buflen;
245 u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
246 struct ieee80211_rateset *rs;
247 static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
248 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
253 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
255 sc->sc_firmware_type = WI_NOTYPE;
256 sc->wi_cmd_count = 500;
259 error = wi_reset(sc);
264 * Read the station address.
265 * And do it twice. I've seen PRISM-based cards that return
266 * an error when trying to read it the first time, which causes
269 buflen = IEEE80211_ADDR_LEN;
270 error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
272 buflen = IEEE80211_ADDR_LEN;
273 error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
276 device_printf(dev, "mac read failed %d\n", error);
279 if (IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) {
280 device_printf(dev, "mac read failed (all zeros)\n");
285 /* Read NIC identification */
288 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
289 ifp->if_ioctl = wi_ioctl;
290 ifp->if_start = wi_start;
291 ifp->if_watchdog = wi_watchdog;
292 ifp->if_init = wi_init;
293 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
294 ifq_set_ready(&ifp->if_snd);
295 #ifdef DEVICE_POLLING
296 ifp->if_poll = wi_poll;
298 ifp->if_capenable = ifp->if_capabilities;
300 ic->ic_phytype = IEEE80211_T_DS;
301 ic->ic_opmode = IEEE80211_M_STA;
302 ic->ic_caps = IEEE80211_C_PMGT |
305 ic->ic_state = IEEE80211_S_INIT;
306 ic->ic_max_aid = WI_MAX_AID;
309 * Query the card for available channels and setup the
310 * channel table. We assume these are all 11b channels.
312 buflen = sizeof(val);
313 if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
314 val = htole16(0x1fff); /* assume 1-11 */
315 KASSERT(val != 0, ("wi_attach: no available channels listed!"));
317 val <<= 1; /* shift for base 1 indices */
318 for (i = 1; i < 16; i++) {
319 if (isset((u_int8_t*)&val, i)) {
320 ic->ic_channels[i].ic_freq =
321 ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
322 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
327 * Read the default channel from the NIC. This may vary
328 * depending on the country where the NIC was purchased, so
329 * we can't hard-code a default and expect it to work for
332 * If no channel is specified, let the 802.11 code select.
334 buflen = sizeof(val);
335 if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0) {
337 KASSERT(val < IEEE80211_CHAN_MAX &&
338 ic->ic_channels[val].ic_flags != 0,
339 ("wi_attach: invalid own channel %u!", val));
340 ic->ic_ibss_chan = &ic->ic_channels[val];
343 "WI_RID_OWN_CHNL failed, using first channel!\n");
344 ic->ic_ibss_chan = &ic->ic_channels[0];
348 * Set flags based on firmware version.
350 switch (sc->sc_firmware_type) {
353 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
354 #ifdef WI_HERMES_AUTOINC_WAR
355 /* XXX: not confirmed, but never seen for recent firmware */
356 if (sc->sc_sta_firmware_ver < 40000) {
357 sc->sc_flags |= WI_FLAGS_BUG_AUTOINC;
360 if (sc->sc_sta_firmware_ver >= 60000)
361 sc->sc_flags |= WI_FLAGS_HAS_MOR;
362 if (sc->sc_sta_firmware_ver >= 60006) {
363 ic->ic_caps |= IEEE80211_C_IBSS;
364 ic->ic_caps |= IEEE80211_C_MONITOR;
366 sc->sc_ibss_port = htole16(1);
368 sc->sc_min_rssi = WI_LUCENT_MIN_RSSI;
369 sc->sc_max_rssi = WI_LUCENT_MAX_RSSI;
370 sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
374 sc->sc_ntxbuf = WI_NTXBUF;
375 sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR;
376 sc->sc_flags |= WI_FLAGS_HAS_ROAMING;
377 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
379 * Old firmware are slow, so give peace a chance.
381 if (sc->sc_sta_firmware_ver < 10000)
382 sc->wi_cmd_count = 5000;
383 if (sc->sc_sta_firmware_ver > 10101)
384 sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
385 if (sc->sc_sta_firmware_ver >= 800) {
386 ic->ic_caps |= IEEE80211_C_IBSS;
387 ic->ic_caps |= IEEE80211_C_MONITOR;
390 * version 0.8.3 and newer are the only ones that are known
391 * to currently work. Earlier versions can be made to work,
392 * at least according to the Linux driver.
394 if (sc->sc_sta_firmware_ver >= 803)
395 ic->ic_caps |= IEEE80211_C_HOSTAP;
396 sc->sc_ibss_port = htole16(0);
398 sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
399 sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
400 sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
405 sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY;
406 if (sc->sc_sta_firmware_ver >= 25000)
407 ic->ic_caps |= IEEE80211_C_IBSS;
408 sc->sc_ibss_port = htole16(4);
410 sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
411 sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
412 sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
417 * Find out if we support WEP on this card.
419 buflen = sizeof(val);
420 if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
422 ic->ic_caps |= IEEE80211_C_WEP;
424 /* Find supported rates. */
425 buflen = sizeof(ratebuf);
426 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
427 if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
428 nrates = le16toh(*(u_int16_t *)ratebuf);
429 if (nrates > IEEE80211_RATE_MAXSIZE)
430 nrates = IEEE80211_RATE_MAXSIZE;
432 for (i = 0; i < nrates; i++)
434 rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i];
436 /* XXX fallback on error? */
440 buflen = sizeof(val);
441 if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
442 wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
443 sc->sc_dbm_offset = le16toh(val);
446 sc->sc_max_datalen = 2304;
447 sc->sc_system_scale = 1;
448 sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN;
449 sc->sc_roaming_mode = 1;
451 sc->sc_portnum = WI_DEFAULT_PORT;
452 sc->sc_authtype = WI_DEFAULT_AUTHTYPE;
454 bzero(sc->sc_nodename, sizeof(sc->sc_nodename));
455 sc->sc_nodelen = sizeof(WI_DEFAULT_NODENAME) - 1;
456 bcopy(WI_DEFAULT_NODENAME, sc->sc_nodename, sc->sc_nodelen);
458 bzero(sc->sc_net_name, sizeof(sc->sc_net_name));
459 bcopy(WI_DEFAULT_NETNAME, sc->sc_net_name,
460 sizeof(WI_DEFAULT_NETNAME) - 1);
463 * Call MI attach routine.
465 ieee80211_ifattach(ic);
466 /* override state transition method */
467 sc->sc_newstate = ic->ic_newstate;
468 sc->sc_key_alloc = ic->ic_crypto.cs_key_alloc;
469 ic->ic_crypto.cs_key_alloc = wi_key_alloc;
470 ic->ic_newstate = wi_newstate;
471 ieee80211_media_init(ic, wi_media_change, wi_media_status);
473 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
474 sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
477 * Initialize constant fields.
478 * XXX make header lengths a multiple of 32-bits so subsequent
479 * headers are properly aligned; this is a kludge to keep
480 * certain applications happy.
482 * NB: the channel is setup each time we transition to the
483 * RUN state to avoid filling it in for each frame.
485 sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t));
486 sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len);
487 sc->sc_tx_th.wt_ihdr.it_present = htole32(WI_TX_RADIOTAP_PRESENT);
489 sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t));
490 sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len);
491 sc->sc_rx_th.wr_ihdr.it_present = htole32(WI_RX_RADIOTAP_PRESENT);
493 error = bus_setup_intr(dev, sc->irq, INTR_MPSAFE,
494 wi_intr, sc, &sc->wi_intrhand,
498 ieee80211_ifdetach(ic);
499 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
504 ieee80211_announce(ic);
514 wi_detach(device_t dev)
516 struct wi_softc *sc = device_get_softc(dev);
517 struct ifnet *ifp = &sc->sc_ic.ic_if;
519 lwkt_serialize_enter(ifp->if_serializer);
521 /* check if device was removed */
522 sc->wi_gone |= !bus_child_present(dev);
524 bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
526 lwkt_serialize_exit(ifp->if_serializer);
529 ieee80211_ifdetach(&sc->sc_ic);
535 wi_shutdown(device_t dev)
537 struct wi_softc *sc = device_get_softc(dev);
538 struct ifnet *ifp = &sc->sc_if;
540 lwkt_serialize_enter(ifp->if_serializer);
542 lwkt_serialize_exit(ifp->if_serializer);
545 #ifdef DEVICE_POLLING
548 wi_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
550 struct wi_softc *sc = ifp->if_softc;
555 /* disable interruptds */
556 CSR_WRITE_2(sc, WI_INT_EN, 0);
558 case POLL_DEREGISTER:
559 /* enable interrupts */
560 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
563 status = CSR_READ_2(sc, WI_EVENT_STAT);
565 if (status & WI_EV_RX)
567 if (status & WI_EV_ALLOC)
569 if (status & WI_EV_INFO)
572 if (cmd == POLL_AND_CHECK_STATUS) {
573 if (status & WI_EV_INFO)
577 if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
578 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 && !ifq_is_empty(&ifp->if_snd)) {
584 #endif /* DEVICE_POLLING */
589 struct wi_softc *sc = arg;
590 struct ifnet *ifp = &sc->sc_ic.ic_if;
593 if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) {
594 CSR_WRITE_2(sc, WI_INT_EN, 0);
595 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
599 /* Disable interrupts. */
600 CSR_WRITE_2(sc, WI_INT_EN, 0);
602 status = CSR_READ_2(sc, WI_EVENT_STAT);
603 if (status & WI_EV_RX)
605 if (status & WI_EV_ALLOC)
607 if (status & WI_EV_TX_EXC)
609 if (status & WI_EV_INFO)
611 if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
612 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 &&
613 !ifq_is_empty(&ifp->if_snd))
616 /* Re-enable interrupts. */
617 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
625 struct wi_softc *sc = arg;
626 struct ifnet *ifp = &sc->sc_if;
627 struct ieee80211com *ic = &sc->sc_ic;
628 struct wi_joinreq join;
630 int error = 0, wasenabled;
635 if ((wasenabled = sc->sc_enabled))
639 /* common 802.11 configuration */
640 ic->ic_flags &= ~IEEE80211_F_IBSSON;
641 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
642 switch (ic->ic_opmode) {
643 case IEEE80211_M_STA:
644 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS);
646 case IEEE80211_M_IBSS:
647 wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port);
648 ic->ic_flags |= IEEE80211_F_IBSSON;
650 case IEEE80211_M_AHDEMO:
651 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
653 case IEEE80211_M_HOSTAP:
655 * For PRISM cards, override the empty SSID, because in
656 * HostAP mode the controller will lock up otherwise.
658 if (sc->sc_firmware_type == WI_INTERSIL &&
659 ic->ic_des_esslen == 0) {
660 ic->ic_des_essid[0] = ' ';
661 ic->ic_des_esslen = 1;
663 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP);
665 case IEEE80211_M_MONITOR:
666 if (sc->sc_firmware_type == WI_LUCENT)
667 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
668 wi_cmd(sc, WI_CMD_DEBUG | (WI_TEST_MONITOR << 8), 0, 0, 0);
672 /* Intersil interprets this RID as joining ESS even in IBSS mode */
673 if (sc->sc_firmware_type == WI_LUCENT &&
674 (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0)
675 wi_write_val(sc, WI_RID_CREATE_IBSS, 1);
677 wi_write_val(sc, WI_RID_CREATE_IBSS, 0);
678 wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
679 wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid,
681 wi_write_val(sc, WI_RID_OWN_CHNL,
682 ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
683 wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen);
685 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
686 wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN);
688 if (ic->ic_caps & IEEE80211_C_PMGT) {
689 wi_write_val(sc, WI_RID_PM_ENABLED,
690 (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
693 /* not yet common 802.11 configuration */
694 wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen);
695 wi_write_val(sc, WI_RID_RTS_THRESH, ic->ic_rtsthreshold);
696 if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
697 wi_write_val(sc, WI_RID_FRAG_THRESH, ic->ic_fragthreshold);
699 /* driver specific 802.11 configuration */
700 if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)
701 wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale);
702 if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
703 wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode);
704 if (sc->sc_flags & WI_FLAGS_HAS_MOR)
705 wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven);
707 wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen);
709 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
710 sc->sc_firmware_type == WI_INTERSIL) {
711 wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_bintval);
712 wi_write_val(sc, WI_RID_BASIC_RATE, 0x03); /* 1, 2 */
713 wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */
714 wi_write_val(sc, WI_RID_DTIM_PERIOD, ic->ic_dtim_period);
718 * Initialize promisc mode.
719 * Being in the Host-AP mode causes a great
720 * deal of pain if primisc mode is set.
721 * Therefore we avoid confusing the firmware
722 * and always reset promisc mode in Host-AP
723 * mode. Host-AP sees all the packets anyway.
725 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
726 (ifp->if_flags & IFF_PROMISC) != 0) {
727 wi_write_val(sc, WI_RID_PROMISC, 1);
729 wi_write_val(sc, WI_RID_PROMISC, 0);
733 if (ic->ic_caps & IEEE80211_C_WEP) {
734 sc->sc_cnfauthmode = ic->ic_bss->ni_authmode;
737 sc->sc_encryption = 0;
740 /* Set multicast filter. */
743 /* Allocate fids for the card */
744 if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
745 sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
746 if (sc->sc_firmware_type == WI_SYMBOL)
747 sc->sc_buflen = 1585; /* XXX */
748 for (i = 0; i < sc->sc_ntxbuf; i++) {
749 error = wi_alloc_fid(sc, sc->sc_buflen,
750 &sc->sc_txd[i].d_fid);
753 "tx buffer allocation failed (error %u)\n",
757 sc->sc_txd[i].d_len = 0;
760 sc->sc_txcur = sc->sc_txnext = 0;
762 /* Enable desired port */
763 wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
766 ifp->if_flags |= IFF_RUNNING;
767 ifp->if_flags &= ~IFF_OACTIVE;
768 if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
769 ic->ic_opmode == IEEE80211_M_IBSS ||
770 ic->ic_opmode == IEEE80211_M_MONITOR ||
771 ic->ic_opmode == IEEE80211_M_HOSTAP)
772 ieee80211_create_ibss(ic, ic->ic_ibss_chan);
774 /* Enable interrupts if not polling */
775 #ifdef DEVICE_POLLING
776 if ((ifp->if_flags & IFF_POLLING) == 0)
778 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
781 ic->ic_opmode == IEEE80211_M_HOSTAP &&
782 sc->sc_firmware_type == WI_INTERSIL) {
783 /* XXX: some card need to be re-enabled for hostap */
784 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
785 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
788 if (ic->ic_opmode == IEEE80211_M_STA &&
789 ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
790 ic->ic_des_chan != IEEE80211_CHAN_ANYC)) {
791 memset(&join, 0, sizeof(join));
792 if (ic->ic_flags & IEEE80211_F_DESBSSID)
793 IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
794 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC)
795 join.wi_chan = htole16(
796 ieee80211_chan2ieee(ic, ic->ic_des_chan));
797 /* Lucent firmware does not support the JOIN RID. */
798 if (sc->sc_firmware_type != WI_LUCENT)
799 wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
804 if_printf(ifp, "interface not running\n");
808 DPRINTF((ifp, "wi_init: return %d\n", error));
813 wi_stop(struct ifnet *ifp, int disable)
815 struct ieee80211com *ic = (struct ieee80211com *) ifp;
816 struct wi_softc *sc = ifp->if_softc;
820 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
821 if (sc->sc_enabled && !sc->wi_gone) {
822 CSR_WRITE_2(sc, WI_INT_EN, 0);
823 wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
827 (*sc->sc_disable)(sc);
831 } else if (sc->wi_gone && disable) /* gone --> not enabled */
835 sc->sc_scan_timer = 0;
836 sc->sc_false_syns = 0;
838 ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
843 wi_start(struct ifnet *ifp)
845 struct wi_softc *sc = ifp->if_softc;
846 struct ieee80211com *ic = &sc->sc_ic;
847 struct ieee80211_node *ni;
848 struct ieee80211_frame *wh;
850 struct wi_frame frmhdr;
851 int cur, fid, off, error;
853 if (sc->wi_gone || (sc->sc_flags & WI_FLAGS_OUTRANGE))
856 memset(&frmhdr, 0, sizeof(frmhdr));
859 IF_POLL(&ic->ic_mgtq, m0);
861 if (sc->sc_txd[cur].d_len != 0) {
862 ifp->if_flags |= IFF_OACTIVE;
865 IF_DEQUEUE(&ic->ic_mgtq, m0);
867 * Hack! The referenced node pointer is in the
868 * rcvif field of the packet header. This is
869 * placed there by ieee80211_mgmt_output because
870 * we need to hold the reference with the frame
871 * and there's no other way (other than packet
872 * tags which we consider too expensive to use)
875 ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
876 m0->m_pkthdr.rcvif = NULL;
878 m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
879 (caddr_t)&frmhdr.wi_ehdr);
880 frmhdr.wi_ehdr.ether_type = 0;
881 wh = mtod(m0, struct ieee80211_frame *);
883 struct ether_header *eh;
885 if (ic->ic_state != IEEE80211_S_RUN)
887 m0 = ifq_poll(&ifp->if_snd);
890 if (sc->sc_txd[cur].d_len != 0) {
891 ifp->if_flags |= IFF_OACTIVE;
895 ifq_dequeue(&ifp->if_snd, m0);
896 if (m0->m_len < sizeof(struct ether_header)) {
897 m0 = m_pullup(m0, sizeof(struct ether_header));
904 eh = mtod(m0, struct ether_header *);
905 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
913 m_copydata(m0, 0, ETHER_HDR_LEN,
914 (caddr_t)&frmhdr.wi_ehdr);
917 m0 = ieee80211_encap(ic, m0, ni);
919 ieee80211_free_node(ni);
923 wh = mtod(m0, struct ieee80211_frame *);
926 if (ic->ic_rawbpf != NULL)
927 bpf_mtap(ic->ic_rawbpf, m0);
929 frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
930 /* XXX check key for SWCRYPT instead of using operating mode */
931 if ((wh->i_fc[1] & IEEE80211_FC1_WEP) &&
932 (sc->sc_encryption & HOST_ENCRYPT)) {
933 if (ieee80211_crypto_encap(ic, ni, m0) == NULL) {
935 ieee80211_free_node(ni);
940 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
944 sc->sc_tx_th.wt_rate =
945 ni->ni_rates.rs_rates[ni->ni_txrate];
946 bpf_ptap(sc->sc_drvbpf, m0, &sc->sc_tx_th,
950 m_copydata(m0, 0, sizeof(struct ieee80211_frame),
951 (caddr_t)&frmhdr.wi_whdr);
952 m_adj(m0, sizeof(struct ieee80211_frame));
953 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
954 if (IFF_DUMPPKTS(ifp))
955 wi_dump_pkt(&frmhdr, NULL, -1);
956 fid = sc->sc_txd[cur].d_fid;
957 off = sizeof(frmhdr);
958 error = wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0
959 || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
962 ieee80211_free_node(ni);
967 sc->sc_txd[cur].d_len = off;
968 if (sc->sc_txcur == cur) {
969 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
970 if_printf(ifp, "xmit failed\n");
971 sc->sc_txd[cur].d_len = 0;
977 sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
982 wi_reset(struct wi_softc *sc)
984 struct ieee80211com *ic = &sc->sc_ic;
985 struct ifnet *ifp = &ic->ic_if;
986 #define WI_INIT_TRIES 3
991 /* Symbol firmware cannot be initialized more than once */
992 if (sc->sc_firmware_type == WI_SYMBOL && sc->sc_reset)
994 if (sc->sc_firmware_type == WI_SYMBOL)
997 tries = WI_INIT_TRIES;
999 for (i = 0; i < tries; i++) {
1000 if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
1002 DELAY(WI_DELAY * 1000);
1007 if_printf(ifp, "init failed\n");
1011 CSR_WRITE_2(sc, WI_INT_EN, 0);
1012 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
1014 /* Calibrate timer. */
1015 wi_write_val(sc, WI_RID_TICK_TIME, 8);
1018 #undef WI_INIT_TRIES
1022 wi_watchdog(struct ifnet *ifp)
1024 struct wi_softc *sc = ifp->if_softc;
1027 if (!sc->sc_enabled)
1030 if (sc->sc_tx_timer) {
1031 if (--sc->sc_tx_timer == 0) {
1032 if_printf(ifp, "device timeout\n");
1034 wi_init(ifp->if_softc);
1040 if (sc->sc_scan_timer) {
1041 if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
1042 sc->sc_firmware_type == WI_INTERSIL) {
1043 DPRINTF((ifp, "wi_watchdog: inquire scan\n"));
1044 wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
1046 if (sc->sc_scan_timer)
1050 /* TODO: rate control */
1051 ieee80211_watchdog(&sc->sc_ic);
1055 wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1057 struct wi_softc *sc = ifp->if_softc;
1058 struct ieee80211com *ic = &sc->sc_ic;
1059 struct ifreq *ifr = (struct ifreq *)data;
1060 struct ieee80211req *ireq;
1061 u_int8_t nodename[IEEE80211_NWID_LEN];
1073 * Can't do promisc and hostap at the same time. If all that's
1074 * changing is the promisc flag, try to short-circuit a call to
1075 * wi_init() by just setting PROMISC in the hardware.
1077 if (ifp->if_flags & IFF_UP) {
1078 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1079 ifp->if_flags & IFF_RUNNING) {
1080 if (ifp->if_flags & IFF_PROMISC &&
1081 !(sc->sc_if_flags & IFF_PROMISC)) {
1082 wi_write_val(sc, WI_RID_PROMISC, 1);
1083 } else if (!(ifp->if_flags & IFF_PROMISC) &&
1084 sc->sc_if_flags & IFF_PROMISC) {
1085 wi_write_val(sc, WI_RID_PROMISC, 0);
1093 if (ifp->if_flags & IFF_RUNNING) {
1098 sc->sc_if_flags = ifp->if_flags;
1103 error = wi_write_multi(sc);
1105 case SIOCGIFGENERIC:
1106 error = wi_get_cfg(ifp, cmd, data, cr);
1108 case SIOCSIFGENERIC:
1109 error = suser_cred(cr, NULL_CRED_OKAY);
1112 error = wi_set_cfg(ifp, cmd, data);
1114 case SIOCGPRISM2DEBUG:
1115 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1118 if (!(ifp->if_flags & IFF_RUNNING) ||
1119 sc->sc_firmware_type == WI_LUCENT) {
1123 error = wi_get_debug(sc, &wreq);
1125 error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
1127 case SIOCSPRISM2DEBUG:
1128 if ((error = suser_cred(cr, NULL_CRED_OKAY)))
1130 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1133 error = wi_set_debug(sc, &wreq);
1136 ireq = (struct ieee80211req *) data;
1137 switch (ireq->i_type) {
1138 case IEEE80211_IOC_STATIONNAME:
1139 ireq->i_len = sc->sc_nodelen + 1;
1140 error = copyout(sc->sc_nodename, ireq->i_data,
1144 error = ieee80211_ioctl(ic, cmd, data, cr);
1149 error = suser_cred(cr, NULL_CRED_OKAY);
1152 ireq = (struct ieee80211req *) data;
1153 switch (ireq->i_type) {
1154 case IEEE80211_IOC_STATIONNAME:
1155 if (ireq->i_val != 0 ||
1156 ireq->i_len > IEEE80211_NWID_LEN) {
1160 memset(nodename, 0, IEEE80211_NWID_LEN);
1161 error = copyin(ireq->i_data, nodename, ireq->i_len);
1164 if (sc->sc_enabled) {
1165 error = wi_write_ssid(sc, WI_RID_NODENAME,
1166 nodename, ireq->i_len);
1170 memcpy(sc->sc_nodename, nodename, IEEE80211_NWID_LEN);
1171 sc->sc_nodelen = ireq->i_len;
1174 error = ieee80211_ioctl(ic, cmd, data, cr);
1179 if (ifp->if_flags & IFF_RUNNING)
1183 error = ieee80211_ioctl(ic, cmd, data, cr);
1186 if (error == ENETRESET) {
1188 wi_init(sc); /* XXX no error return */
1196 wi_media_change(struct ifnet *ifp)
1198 struct wi_softc *sc = ifp->if_softc;
1201 error = ieee80211_media_change(ifp);
1202 if (error == ENETRESET) {
1204 wi_init(sc); /* XXX no error return */
1211 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1213 struct wi_softc *sc = ifp->if_softc;
1214 struct ieee80211com *ic = &sc->sc_ic;
1218 if (sc->wi_gone) { /* hardware gone (e.g. ejected) */
1219 imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
1220 imr->ifm_status = 0;
1224 imr->ifm_status = IFM_AVALID;
1225 imr->ifm_active = IFM_IEEE80211;
1226 if (!sc->sc_enabled) { /* port !enabled, have no status */
1227 imr->ifm_active |= IFM_NONE;
1230 if (ic->ic_state == IEEE80211_S_RUN &&
1231 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
1232 imr->ifm_status |= IFM_ACTIVE;
1234 if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) == 0 &&
1235 len == sizeof(val)) {
1236 /* convert to 802.11 rate */
1239 if (sc->sc_firmware_type == WI_LUCENT) {
1241 rate = 11; /* 5.5Mbps */
1244 rate = 11; /* 5.5Mbps */
1245 else if (rate == 8*2)
1246 rate = 22; /* 11Mbps */
1251 imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
1252 switch (ic->ic_opmode) {
1253 case IEEE80211_M_STA:
1255 case IEEE80211_M_IBSS:
1256 imr->ifm_active |= IFM_IEEE80211_ADHOC;
1258 case IEEE80211_M_AHDEMO:
1259 imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1261 case IEEE80211_M_HOSTAP:
1262 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1264 case IEEE80211_M_MONITOR:
1265 imr->ifm_active |= IFM_IEEE80211_MONITOR;
1271 wi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1273 struct ieee80211com *ic = &sc->sc_ic;
1274 struct ieee80211_node *ni = ic->ic_bss;
1275 struct ifnet *ifp = &ic->ic_if;
1277 if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1280 DPRINTF((ifp, "wi_sync_bssid: bssid %6D -> %6D ?\n", ni->ni_bssid, ":",
1283 /* In promiscuous mode, the BSSID field is not a reliable
1284 * indicator of the firmware's BSSID. Damp spurious
1285 * change-of-BSSID indications.
1287 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1288 !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns,
1292 sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1);
1294 * XXX hack; we should create a new node with the new bssid
1295 * and replace the existing ic_bss with it but since we don't
1296 * process management frames to collect state we cheat by
1297 * reusing the existing node as we know wi_newstate will be
1298 * called and it will overwrite the node state.
1300 ieee80211_sta_join(ic, ieee80211_ref_node(ni));
1304 wi_rx_monitor(struct wi_softc *sc, int fid)
1306 struct ieee80211com *ic = &sc->sc_ic;
1307 struct ifnet *ifp = &ic->ic_if;
1308 struct wi_frame *rx_frame;
1312 /* first allocate mbuf for packet storage */
1313 m = m_getcl(MB_DONTWAIT, MT_DATA, 0);
1319 m->m_pkthdr.rcvif = ifp;
1321 /* now read wi_frame first so we know how much data to read */
1322 if (wi_read_bap(sc, fid, 0, mtod(m, caddr_t), sizeof(*rx_frame))) {
1327 rx_frame = mtod(m, struct wi_frame *);
1329 switch ((rx_frame->wi_status & WI_STAT_MAC_PORT) >> 8) {
1331 switch (rx_frame->wi_whdr.i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1332 case IEEE80211_FC0_TYPE_DATA:
1333 hdrlen = WI_DATA_HDRLEN;
1334 datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1336 case IEEE80211_FC0_TYPE_MGT:
1337 hdrlen = WI_MGMT_HDRLEN;
1338 datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1340 case IEEE80211_FC0_TYPE_CTL:
1342 * prism2 cards don't pass control packets
1343 * down properly or consistently, so we'll only
1344 * pass down the header.
1346 hdrlen = WI_CTL_HDRLEN;
1350 if_printf(ifp, "received packet of unknown type "
1357 hdrlen = WI_DATA_HDRLEN;
1358 datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1361 if_printf(ifp, "received packet on invalid "
1362 "port (wi_status=0x%x)\n", rx_frame->wi_status);
1367 if (hdrlen + datlen + 2 > MCLBYTES) {
1368 if_printf(ifp, "oversized packet received "
1369 "(wi_dat_len=%d, wi_status=0x%x)\n",
1370 datlen, rx_frame->wi_status);
1375 if (wi_read_bap(sc, fid, hdrlen, mtod(m, caddr_t) + hdrlen,
1377 m->m_pkthdr.len = m->m_len = hdrlen + datlen;
1379 BPF_MTAP(ifp, m); /* Handle BPF listeners. */
1387 wi_rx_intr(struct wi_softc *sc)
1389 struct ieee80211com *ic = &sc->sc_ic;
1390 struct ifnet *ifp = &ic->ic_if;
1391 struct wi_frame frmhdr;
1393 struct ieee80211_frame *wh;
1394 struct ieee80211_node *ni;
1395 int fid, len, off, rssi;
1400 fid = CSR_READ_2(sc, WI_RX_FID);
1402 if (sc->wi_debug.wi_monitor) {
1404 * If we are in monitor mode just
1405 * read the data from the device.
1407 wi_rx_monitor(sc, fid);
1408 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1412 /* First read in the frame header */
1413 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1414 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1416 DPRINTF((ifp, "wi_rx_intr: read fid %x failed\n", fid));
1420 if (IFF_DUMPPKTS(ifp))
1421 wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal);
1424 * Drop undecryptable or packets with receive errors here
1426 status = le16toh(frmhdr.wi_status);
1427 if (status & WI_STAT_ERRSTAT) {
1428 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1430 DPRINTF((ifp, "wi_rx_intr: fid %x error status %x\n",
1434 rssi = frmhdr.wi_rx_signal;
1435 rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1436 le16toh(frmhdr.wi_rx_tstamp1);
1438 len = le16toh(frmhdr.wi_dat_len);
1439 off = ALIGN(sizeof(struct ieee80211_frame));
1442 * Sometimes the PRISM2.x returns bogusly large frames. Except
1443 * in monitor mode, just throw them away.
1445 if (off + len > MCLBYTES) {
1446 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1447 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1449 DPRINTF((ifp, "wi_rx_intr: oversized packet\n"));
1455 m = m_getl(off + len, MB_DONTWAIT, MT_DATA, M_PKTHDR, NULL);
1457 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1459 DPRINTF((ifp, "wi_rx_intr: m_getl failed\n"));
1463 m->m_data += off - sizeof(struct ieee80211_frame);
1464 memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1465 wi_read_bap(sc, fid, sizeof(frmhdr),
1466 m->m_data + sizeof(struct ieee80211_frame), len);
1467 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1468 m->m_pkthdr.rcvif = ifp;
1470 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1472 wh = mtod(m, struct ieee80211_frame *);
1473 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1475 * WEP is decrypted by hardware and the IV
1476 * is stripped. Clear WEP bit so we don't
1477 * try to process it in ieee80211_input.
1478 * XXX fix for TKIP, et. al.
1480 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1483 if (sc->sc_drvbpf) {
1484 /* XXX replace divide by table */
1485 sc->sc_rx_th.wr_rate = frmhdr.wi_rx_rate / 5;
1486 sc->sc_rx_th.wr_antsignal = frmhdr.wi_rx_signal;
1487 sc->sc_rx_th.wr_antnoise = frmhdr.wi_rx_silence;
1488 sc->sc_rx_th.wr_flags = 0;
1489 if (frmhdr.wi_status & WI_STAT_PCF)
1490 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_CFP;
1491 bpf_ptap(sc->sc_drvbpf, m, &sc->sc_rx_th, sc->sc_rx_th_len);
1494 /* synchronize driver's BSSID with firmware's BSSID */
1495 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1496 if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1497 wi_sync_bssid(sc, wh->i_addr3);
1500 * Locate the node for sender, track state, and
1501 * then pass this node (referenced) up to the 802.11
1502 * layer for its use.
1504 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *) wh);
1506 * Send frame up for processing.
1508 ieee80211_input(ic, m, ni, rssi, rstamp);
1510 * The frame may have caused the node to be marked for
1511 * reclamation (e.g. in response to a DEAUTH message)
1512 * so use free_node here instead of unref_node.
1514 ieee80211_free_node(ni);
1518 wi_tx_ex_intr(struct wi_softc *sc)
1520 struct ieee80211com *ic = &sc->sc_ic;
1521 struct ifnet *ifp = &ic->ic_if;
1522 struct wi_frame frmhdr;
1525 fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1526 /* Read in the frame header */
1527 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1528 u_int16_t status = le16toh(frmhdr.wi_status);
1531 * Spontaneous station disconnects appear as xmit
1532 * errors. Don't announce them and/or count them
1533 * as an output error.
1535 if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1536 if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1537 if_printf(ifp, "tx failed");
1538 if (status & WI_TXSTAT_RET_ERR)
1539 kprintf(", retry limit exceeded");
1540 if (status & WI_TXSTAT_AGED_ERR)
1541 kprintf(", max transmit lifetime exceeded");
1542 if (status & WI_TXSTAT_DISCONNECT)
1543 kprintf(", port disconnected");
1544 if (status & WI_TXSTAT_FORM_ERR)
1545 kprintf(", invalid format (data len %u src %6D)",
1546 le16toh(frmhdr.wi_dat_len),
1547 frmhdr.wi_ehdr.ether_shost, ":");
1549 kprintf(", status=0x%x", status);
1554 DPRINTF((ifp, "port disconnected\n"));
1555 ifp->if_collisions++; /* XXX */
1558 DPRINTF((ifp, "wi_tx_ex_intr: read fid %x failed\n", fid));
1559 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1563 wi_tx_intr(struct wi_softc *sc)
1565 struct ieee80211com *ic = &sc->sc_ic;
1566 struct ifnet *ifp = &ic->ic_if;
1572 fid = CSR_READ_2(sc, WI_ALLOC_FID);
1573 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1576 if (sc->sc_txd[cur].d_fid != fid) {
1577 if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
1578 fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
1581 sc->sc_tx_timer = 0;
1582 sc->sc_txd[cur].d_len = 0;
1583 sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
1584 if (sc->sc_txd[cur].d_len == 0)
1585 ifp->if_flags &= ~IFF_OACTIVE;
1587 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1589 if_printf(ifp, "xmit failed\n");
1590 sc->sc_txd[cur].d_len = 0;
1592 sc->sc_tx_timer = 5;
1599 wi_info_intr(struct wi_softc *sc)
1601 struct ieee80211com *ic = &sc->sc_ic;
1602 struct ifnet *ifp = &ic->ic_if;
1603 int i, fid, len, off;
1608 fid = CSR_READ_2(sc, WI_INFO_FID);
1609 wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
1611 switch (le16toh(ltbuf[1])) {
1613 case WI_INFO_LINK_STAT:
1614 wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1615 DPRINTF((ifp, "wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1616 switch (le16toh(stat)) {
1617 case WI_INFO_LINK_STAT_CONNECTED:
1618 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1619 if (ic->ic_state == IEEE80211_S_RUN &&
1620 ic->ic_opmode != IEEE80211_M_IBSS)
1623 case WI_INFO_LINK_STAT_AP_CHG:
1624 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1626 case WI_INFO_LINK_STAT_AP_INR:
1627 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1629 case WI_INFO_LINK_STAT_AP_OOR:
1630 if (sc->sc_firmware_type == WI_SYMBOL &&
1631 sc->sc_scan_timer > 0) {
1632 if (wi_cmd(sc, WI_CMD_INQUIRE,
1633 WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
1634 sc->sc_scan_timer = 0;
1637 if (ic->ic_opmode == IEEE80211_M_STA)
1638 sc->sc_flags |= WI_FLAGS_OUTRANGE;
1640 case WI_INFO_LINK_STAT_DISCONNECTED:
1641 case WI_INFO_LINK_STAT_ASSOC_FAILED:
1642 if (ic->ic_opmode == IEEE80211_M_STA)
1643 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1648 case WI_INFO_COUNTERS:
1649 /* some card versions have a larger stats structure */
1650 len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1651 ptr = (u_int32_t *)&sc->sc_stats;
1652 off = sizeof(ltbuf);
1653 for (i = 0; i < len; i++, off += 2, ptr++) {
1654 wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1655 #ifdef WI_HERMES_STATS_WAR
1661 ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1662 sc->sc_stats.wi_tx_multi_retries +
1663 sc->sc_stats.wi_tx_retry_limit;
1666 case WI_INFO_SCAN_RESULTS:
1667 case WI_INFO_HOST_SCAN_RESULTS:
1668 wi_scan_result(sc, fid, le16toh(ltbuf[0]));
1672 DPRINTF((ifp, "wi_info_intr: got fid %x type %x len %d\n", fid,
1673 le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1676 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1680 wi_write_multi(struct wi_softc *sc)
1682 struct ifnet *ifp = &sc->sc_ic.ic_if;
1684 struct ifmultiaddr *ifma;
1685 struct wi_mcast mlist;
1687 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1689 memset(&mlist, 0, sizeof(mlist));
1690 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1695 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1696 if (ifma->ifma_addr->sa_family != AF_LINK)
1700 IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1701 (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1704 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1705 IEEE80211_ADDR_LEN * n);
1709 wi_read_nicid(struct wi_softc *sc)
1711 struct wi_card_ident *id;
1716 /* getting chip identity */
1717 memset(ver, 0, sizeof(ver));
1719 wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1720 if_printf(&sc->sc_ic.ic_if, "using ");
1722 sc->sc_firmware_type = WI_NOTYPE;
1723 for (id = wi_card_ident; id->card_name != NULL; id++) {
1724 if (le16toh(ver[0]) == id->card_id) {
1725 kprintf("%s", id->card_name);
1726 sc->sc_firmware_type = id->firm_type;
1730 if (sc->sc_firmware_type == WI_NOTYPE) {
1731 if (le16toh(ver[0]) & 0x8000) {
1732 kprintf("Unknown PRISM2 chip");
1733 sc->sc_firmware_type = WI_INTERSIL;
1735 kprintf("Unknown Lucent chip");
1736 sc->sc_firmware_type = WI_LUCENT;
1740 /* get primary firmware version (Only Prism chips) */
1741 if (sc->sc_firmware_type != WI_LUCENT) {
1742 memset(ver, 0, sizeof(ver));
1744 wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1745 sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1746 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1749 /* get station firmware version */
1750 memset(ver, 0, sizeof(ver));
1752 wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1753 sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1754 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1755 if (sc->sc_firmware_type == WI_INTERSIL &&
1756 (sc->sc_sta_firmware_ver == 10102 ||
1757 sc->sc_sta_firmware_ver == 20102)) {
1759 memset(ident, 0, sizeof(ident));
1760 len = sizeof(ident);
1761 /* value should be the format like "V2.00-11" */
1762 if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1763 *(p = (char *)ident) >= 'A' &&
1764 p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1765 sc->sc_firmware_type = WI_SYMBOL;
1766 sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1767 (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1768 (p[6] - '0') * 10 + (p[7] - '0');
1772 if_printf(&sc->sc_ic.ic_if, "%s Firmware: ",
1773 sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
1774 (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
1775 if (sc->sc_firmware_type != WI_LUCENT) /* XXX */
1776 kprintf("Primary (%u.%u.%u), ",
1777 sc->sc_pri_firmware_ver / 10000,
1778 (sc->sc_pri_firmware_ver % 10000) / 100,
1779 sc->sc_pri_firmware_ver % 100);
1780 kprintf("Station (%u.%u.%u)\n",
1781 sc->sc_sta_firmware_ver / 10000,
1782 (sc->sc_sta_firmware_ver % 10000) / 100,
1783 sc->sc_sta_firmware_ver % 100);
1787 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1789 struct wi_ssid ssid;
1791 if (buflen > IEEE80211_NWID_LEN)
1793 memset(&ssid, 0, sizeof(ssid));
1794 ssid.wi_len = htole16(buflen);
1795 memcpy(ssid.wi_ssid, buf, buflen);
1796 return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1800 wi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1802 struct wi_softc *sc = ifp->if_softc;
1803 struct ieee80211com *ic = &sc->sc_ic;
1804 struct ifreq *ifr = (struct ifreq *)data;
1806 struct wi_scan_res *res;
1808 int len, n, error, mif, val, off, i;
1810 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1813 len = (wreq.wi_len - 1) * 2;
1814 if (len < sizeof(u_int16_t))
1816 if (len > sizeof(wreq.wi_val))
1817 len = sizeof(wreq.wi_val);
1819 switch (wreq.wi_type) {
1821 case WI_RID_IFACE_STATS:
1822 memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
1823 if (len < sizeof(sc->sc_stats))
1826 len = sizeof(sc->sc_stats);
1829 case WI_RID_ENCRYPTION:
1830 case WI_RID_TX_CRYPT_KEY:
1831 case WI_RID_DEFLT_CRYPT_KEYS:
1832 case WI_RID_TX_RATE:
1833 return ieee80211_cfgget(ic, cmd, data, cr);
1835 case WI_RID_MICROWAVE_OVEN:
1836 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
1837 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1841 wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
1842 len = sizeof(u_int16_t);
1845 case WI_RID_DBM_ADJUST:
1846 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
1847 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1851 wreq.wi_val[0] = htole16(sc->sc_dbm_offset);
1852 len = sizeof(u_int16_t);
1855 case WI_RID_ROAMING_MODE:
1856 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
1857 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1861 wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
1862 len = sizeof(u_int16_t);
1865 case WI_RID_SYSTEM_SCALE:
1866 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
1867 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1871 wreq.wi_val[0] = htole16(sc->sc_system_scale);
1872 len = sizeof(u_int16_t);
1875 case WI_RID_FRAG_THRESH:
1876 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
1877 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1881 wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
1882 len = sizeof(u_int16_t);
1885 case WI_RID_READ_APS:
1886 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1887 return ieee80211_cfgget(ic, cmd, data, cr);
1888 if (sc->sc_scan_timer > 0) {
1889 error = EINPROGRESS;
1893 if (len < sizeof(n)) {
1897 if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
1898 n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
1899 len = sizeof(n) + sizeof(struct wi_apinfo) * n;
1900 memcpy(wreq.wi_val, &n, sizeof(n));
1901 memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
1902 sizeof(struct wi_apinfo) * n);
1906 wreq.wi_val[0] = sc->sc_firmware_type != WI_LUCENT;
1907 len = sizeof(u_int16_t);
1911 mif = wreq.wi_val[0];
1912 error = wi_cmd(sc, WI_CMD_READMIF, mif, 0, 0);
1913 val = CSR_READ_2(sc, WI_RESP0);
1914 wreq.wi_val[0] = val;
1915 len = sizeof(u_int16_t);
1918 case WI_RID_ZERO_CACHE:
1919 case WI_RID_PROCFRAME: /* ignore for compatibility */
1923 case WI_RID_READ_CACHE:
1924 return ieee80211_cfgget(ic, cmd, data, cr);
1926 case WI_RID_SCAN_RES: /* compatibility interface */
1927 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1928 return ieee80211_cfgget(ic, cmd, data, cr);
1929 if (sc->sc_scan_timer > 0) {
1930 error = EINPROGRESS;
1934 if (sc->sc_firmware_type == WI_LUCENT) {
1936 reslen = WI_WAVELAN_RES_SIZE;
1938 off = sizeof(struct wi_scan_p2_hdr);
1939 reslen = WI_PRISM2_RES_SIZE;
1941 if (len < off + reslen * n)
1942 n = (len - off) / reslen;
1943 len = off + reslen * n;
1945 struct wi_scan_p2_hdr *p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1947 * Prepend Prism-specific header.
1949 if (len < sizeof(struct wi_scan_p2_hdr)) {
1953 p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1955 p2->wi_reason = n; /* XXX */
1957 for (i = 0; i < n; i++, off += reslen) {
1958 const struct wi_apinfo *ap = &sc->sc_aps[i];
1960 res = (struct wi_scan_res *)((char *)wreq.wi_val + off);
1961 res->wi_chan = ap->channel;
1962 res->wi_noise = ap->noise;
1963 res->wi_signal = ap->signal;
1964 IEEE80211_ADDR_COPY(res->wi_bssid, ap->bssid);
1965 res->wi_interval = ap->interval;
1966 res->wi_capinfo = ap->capinfo;
1967 res->wi_ssid_len = ap->namelen;
1968 memcpy(res->wi_ssid, ap->name,
1969 IEEE80211_NWID_LEN);
1970 if (sc->sc_firmware_type != WI_LUCENT) {
1971 /* XXX not saved from Prism cards */
1972 memset(res->wi_srates, 0,
1973 sizeof(res->wi_srates));
1974 res->wi_rate = ap->rate;
1981 if (sc->sc_enabled) {
1982 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1986 switch (wreq.wi_type) {
1987 case WI_RID_MAX_DATALEN:
1988 wreq.wi_val[0] = htole16(sc->sc_max_datalen);
1989 len = sizeof(u_int16_t);
1991 case WI_RID_RTS_THRESH:
1992 wreq.wi_val[0] = htole16(ic->ic_rtsthreshold);
1993 len = sizeof(u_int16_t);
1995 case WI_RID_CNFAUTHMODE:
1996 wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
1997 len = sizeof(u_int16_t);
1999 case WI_RID_NODENAME:
2000 if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
2004 len = sc->sc_nodelen + sizeof(u_int16_t);
2005 wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
2006 memcpy(&wreq.wi_val[1], sc->sc_nodename,
2010 return ieee80211_cfgget(ic, cmd, data, cr);
2016 wreq.wi_len = (len + 1) / 2 + 1;
2017 return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
2021 wi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
2023 struct wi_softc *sc = ifp->if_softc;
2024 struct ieee80211com *ic = &sc->sc_ic;
2025 struct ifreq *ifr = (struct ifreq *)data;
2028 int i, len, error, mif, val;
2029 struct ieee80211_rateset *rs;
2031 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2034 len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0;
2035 switch (wreq.wi_type) {
2036 case WI_RID_DBM_ADJUST:
2039 case WI_RID_NODENAME:
2040 if (le16toh(wreq.wi_val[0]) * 2 > len ||
2041 le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
2045 if (sc->sc_enabled) {
2046 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2051 sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
2052 memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen);
2055 case WI_RID_MICROWAVE_OVEN:
2056 case WI_RID_ROAMING_MODE:
2057 case WI_RID_SYSTEM_SCALE:
2058 case WI_RID_FRAG_THRESH:
2059 if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
2060 (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
2062 if (wreq.wi_type == WI_RID_ROAMING_MODE &&
2063 (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
2065 if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
2066 (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
2068 if (wreq.wi_type == WI_RID_FRAG_THRESH &&
2069 (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
2072 case WI_RID_RTS_THRESH:
2073 case WI_RID_CNFAUTHMODE:
2074 case WI_RID_MAX_DATALEN:
2075 if (sc->sc_enabled) {
2076 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2081 switch (wreq.wi_type) {
2082 case WI_RID_FRAG_THRESH:
2083 ic->ic_fragthreshold = le16toh(wreq.wi_val[0]);
2085 case WI_RID_RTS_THRESH:
2086 ic->ic_rtsthreshold = le16toh(wreq.wi_val[0]);
2088 case WI_RID_MICROWAVE_OVEN:
2089 sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
2091 case WI_RID_ROAMING_MODE:
2092 sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
2094 case WI_RID_SYSTEM_SCALE:
2095 sc->sc_system_scale = le16toh(wreq.wi_val[0]);
2097 case WI_RID_CNFAUTHMODE:
2098 sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
2100 case WI_RID_MAX_DATALEN:
2101 sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
2106 case WI_RID_TX_RATE:
2107 switch (le16toh(wreq.wi_val[0])) {
2109 ic->ic_fixed_rate = IEEE80211_FIXED_RATE_NONE;
2112 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
2113 for (i = 0; i < rs->rs_nrates; i++) {
2114 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL)
2115 / 2 == le16toh(wreq.wi_val[0]))
2118 if (i == rs->rs_nrates)
2120 ic->ic_fixed_rate = i;
2123 error = wi_write_txrate(sc);
2126 case WI_RID_SCAN_APS:
2127 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2128 error = wi_scan_ap(sc, 0x3fff, 0x000f);
2131 case WI_RID_SCAN_REQ: /* compatibility interface */
2132 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2133 error = wi_scan_ap(sc, wreq.wi_val[0], wreq.wi_val[1]);
2136 case WI_RID_MGMT_XMIT:
2137 if (!sc->sc_enabled) {
2141 if (ic->ic_mgtq.ifq_len > 5) {
2145 /* XXX wi_len looks in u_int8_t, not in u_int16_t */
2146 m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL);
2151 IF_ENQUEUE(&ic->ic_mgtq, m);
2155 mif = wreq.wi_val[0];
2156 val = wreq.wi_val[1];
2157 error = wi_cmd(sc, WI_CMD_WRITEMIF, mif, val, 0);
2160 case WI_RID_PROCFRAME: /* ignore for compatibility */
2163 case WI_RID_OWN_SSID:
2164 if (le16toh(wreq.wi_val[0]) * 2 > len ||
2165 le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) {
2169 memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
2170 ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2;
2171 memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen);
2176 if (sc->sc_enabled) {
2177 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2182 error = ieee80211_cfgset(ic, cmd, data);
2189 wi_write_txrate(struct wi_softc *sc)
2191 struct ieee80211com *ic = &sc->sc_ic;
2195 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
2196 rate = 0; /* auto */
2198 rate = (ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ic->ic_fixed_rate] &
2199 IEEE80211_RATE_VAL) / 2;
2201 /* rate: 0, 1, 2, 5, 11 */
2203 switch (sc->sc_firmware_type) {
2206 case 0: /* auto == 11mbps auto */
2209 /* case 1, 2 map to 1, 2*/
2210 case 5: /* 5.5Mbps -> 4 */
2213 case 11: /* 11mbps -> 5 */
2221 /* Choose a bit according to this table.
2224 * ----+-------------------
2230 for (i = 8; i > 0; i >>= 1) {
2235 rate = 0xf; /* auto */
2240 return wi_write_val(sc, WI_RID_TX_RATE, rate);
2244 wi_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k,
2245 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
2247 struct wi_softc *sc = ic->ic_ifp->if_softc;
2250 * When doing host encryption of outbound frames fail requests
2251 * for keys that are not marked w/ the SWCRYPT flag so the
2252 * net80211 layer falls back to s/w crypto. Note that we also
2253 * fixup existing keys below to handle mode changes.
2255 if ((sc->sc_encryption & HOST_ENCRYPT) &&
2256 (k->wk_flags & IEEE80211_KEY_SWCRYPT) == 0)
2258 return sc->sc_key_alloc(ic, k, keyix, rxkeyix);
2262 wi_write_wep(struct wi_softc *sc)
2264 struct ieee80211com *ic = &sc->sc_ic;
2268 struct wi_key wkey[IEEE80211_WEP_NKID];
2270 switch (sc->sc_firmware_type) {
2272 val = (ic->ic_flags & IEEE80211_F_PRIVACY) ? 1 : 0;
2273 error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
2276 if (!(ic->ic_flags & IEEE80211_F_PRIVACY))
2278 error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_def_txkey);
2281 memset(wkey, 0, sizeof(wkey));
2282 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2283 keylen = ic->ic_nw_keys[i].wk_keylen;
2284 wkey[i].wi_keylen = htole16(keylen);
2285 memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
2288 error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
2289 wkey, sizeof(wkey));
2290 sc->sc_encryption = 0;
2295 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
2297 * ONLY HWB3163 EVAL-CARD Firmware version
2298 * less than 0.8 variant2
2300 * If promiscuous mode disable, Prism2 chip
2301 * does not work with WEP .
2302 * It is under investigation for details.
2303 * (ichiro@netbsd.org)
2305 if (sc->sc_firmware_type == WI_INTERSIL &&
2306 sc->sc_sta_firmware_ver < 802 ) {
2307 /* firm ver < 0.8 variant 2 */
2308 wi_write_val(sc, WI_RID_PROMISC, 1);
2310 wi_write_val(sc, WI_RID_CNFAUTHMODE,
2311 sc->sc_cnfauthmode);
2312 /* XXX should honor IEEE80211_F_DROPUNENC */
2313 val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
2315 * Encryption firmware has a bug for HostAP mode.
2317 if (sc->sc_firmware_type == WI_INTERSIL &&
2318 ic->ic_opmode == IEEE80211_M_HOSTAP)
2319 val |= HOST_ENCRYPT;
2321 wi_write_val(sc, WI_RID_CNFAUTHMODE,
2322 IEEE80211_AUTH_OPEN);
2323 val = HOST_ENCRYPT | HOST_DECRYPT;
2325 error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
2328 sc->sc_encryption = val;
2329 if ((val & PRIVACY_INVOKED) == 0)
2331 error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
2335 if (val & HOST_DECRYPT)
2338 * It seems that the firmware accept 104bit key only if
2339 * all the keys have 104bit length. We get the length of
2340 * the transmit key and use it for all other keys.
2341 * Perhaps we should use software WEP for such situation.
2343 if (ic->ic_def_txkey != IEEE80211_KEYIX_NONE)
2344 keylen = ic->ic_nw_keys[ic->ic_def_txkey].wk_keylen;
2345 else /* XXX should not hapen */
2346 keylen = IEEE80211_WEP_KEYLEN;
2347 if (keylen > IEEE80211_WEP_KEYLEN)
2348 keylen = 13; /* 104bit keys */
2350 keylen = IEEE80211_WEP_KEYLEN;
2351 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2352 error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
2353 ic->ic_nw_keys[i].wk_key, keylen);
2360 * XXX horrible hack; insure pre-existing keys are
2361 * setup properly to do s/w crypto.
2363 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2364 struct ieee80211_key *k = &ic->ic_nw_keys[i];
2365 if (k->wk_flags & IEEE80211_KEY_XMIT) {
2366 if (sc->sc_encryption & HOST_ENCRYPT)
2367 k->wk_flags |= IEEE80211_KEY_SWCRYPT;
2369 k->wk_flags &= ~IEEE80211_KEY_SWCRYPT;
2376 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
2383 /* wait for the busy bit to clear */
2384 for (i = sc->wi_cmd_count; i > 0; i--) { /* 500ms */
2385 if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
2387 DELAY(1*1000); /* 1ms */
2390 if_printf(&sc->sc_ic.ic_if, "wi_cmd: busy bit won't clear.\n" );
2395 CSR_WRITE_2(sc, WI_PARAM0, val0);
2396 CSR_WRITE_2(sc, WI_PARAM1, val1);
2397 CSR_WRITE_2(sc, WI_PARAM2, val2);
2398 CSR_WRITE_2(sc, WI_COMMAND, cmd);
2400 if (cmd == WI_CMD_INI) {
2401 /* XXX: should sleep here. */
2402 DELAY(100*1000); /* 100ms delay for init */
2404 for (i = 0; i < WI_TIMEOUT; i++) {
2406 * Wait for 'command complete' bit to be
2407 * set in the event status register.
2409 s = CSR_READ_2(sc, WI_EVENT_STAT);
2410 if (s & WI_EV_CMD) {
2411 /* Ack the event and read result code. */
2412 s = CSR_READ_2(sc, WI_STATUS);
2413 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
2414 if (s & WI_STAT_CMD_RESULT) {
2422 if (i == WI_TIMEOUT) {
2423 if_printf(&sc->sc_ic.ic_if,
2424 "timeout in wi_cmd 0x%04x; event status 0x%04x\n", cmd, s);
2433 wi_seek_bap(struct wi_softc *sc, int id, int off)
2437 CSR_WRITE_2(sc, WI_SEL0, id);
2438 CSR_WRITE_2(sc, WI_OFF0, off);
2440 for (i = 0; ; i++) {
2441 status = CSR_READ_2(sc, WI_OFF0);
2442 if ((status & WI_OFF_BUSY) == 0)
2444 if (i == WI_TIMEOUT) {
2445 if_printf(&sc->sc_ic.ic_if,
2446 "timeout in wi_seek to %x/%x\n", id, off);
2447 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2448 if (status == 0xffff)
2454 if (status & WI_OFF_ERR) {
2455 if_printf(&sc->sc_ic.ic_if, "failed in wi_seek to %x/%x\n",
2457 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2461 sc->sc_bap_off = off;
2466 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
2473 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2474 if ((error = wi_seek_bap(sc, id, off)) != 0)
2477 cnt = (buflen + 1) / 2;
2478 ptr = (u_int16_t *)buf;
2479 for (i = 0; i < cnt; i++)
2480 *ptr++ = CSR_READ_2(sc, WI_DATA0);
2481 sc->sc_bap_off += cnt * 2;
2486 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
2494 #ifdef WI_HERMES_AUTOINC_WAR
2497 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2498 if ((error = wi_seek_bap(sc, id, off)) != 0)
2501 cnt = (buflen + 1) / 2;
2502 ptr = (u_int16_t *)buf;
2503 for (i = 0; i < cnt; i++)
2504 CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
2505 sc->sc_bap_off += cnt * 2;
2507 #ifdef WI_HERMES_AUTOINC_WAR
2509 * According to the comments in the HCF Light code, there is a bug
2510 * in the Hermes (or possibly in certain Hermes firmware revisions)
2511 * where the chip's internal autoincrement counter gets thrown off
2512 * during data writes: the autoincrement is missed, causing one
2513 * data word to be overwritten and subsequent words to be written to
2514 * the wrong memory locations. The end result is that we could end
2515 * up transmitting bogus frames without realizing it. The workaround
2516 * for this is to write a couple of extra guard words after the end
2517 * of the transfer, then attempt to read then back. If we fail to
2518 * locate the guard words where we expect them, we preform the
2519 * transfer over again.
2521 if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
2522 CSR_WRITE_2(sc, WI_DATA0, 0x1234);
2523 CSR_WRITE_2(sc, WI_DATA0, 0x5678);
2524 wi_seek_bap(sc, id, sc->sc_bap_off);
2525 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2526 if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
2527 CSR_READ_2(sc, WI_DATA0) != 0x5678) {
2528 if_printf(&sc->sc_ic.ic_if,
2529 "detect auto increment bug, try again\n");
2538 wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
2543 for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
2547 len = min(m->m_len, totlen);
2549 if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
2550 m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
2551 return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
2555 if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
2565 wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
2569 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
2570 if_printf(&sc->sc_ic.ic_if,
2571 "failed to allocate %d bytes on NIC\n", len);
2575 for (i = 0; i < WI_TIMEOUT; i++) {
2576 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
2580 if (i == WI_TIMEOUT) {
2581 if_printf(&sc->sc_ic.ic_if, "timeout in alloc\n");
2584 *idp = CSR_READ_2(sc, WI_ALLOC_FID);
2585 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
2590 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
2595 /* Tell the NIC to enter record read mode. */
2596 error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2600 error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2604 if (le16toh(ltbuf[1]) != rid) {
2605 if_printf(&sc->sc_ic.ic_if,
2606 "record read mismatch, rid=%x, got=%x\n",
2607 rid, le16toh(ltbuf[1]));
2610 len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */
2611 if (*buflenp < len) {
2612 if_printf(&sc->sc_ic.ic_if, "record buffer is too small, "
2613 "rid=%x, size=%d, len=%d\n", rid, *buflenp, len);
2617 return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2621 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2626 ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */
2627 ltbuf[1] = htole16(rid);
2629 error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2632 error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2636 return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
2640 wi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
2642 struct ifnet *ifp = &ic->ic_if;
2643 struct wi_softc *sc = ifp->if_softc;
2644 struct ieee80211_node *ni = ic->ic_bss;
2647 struct wi_ssid ssid;
2648 u_int8_t old_bssid[IEEE80211_ADDR_LEN];
2650 DPRINTF((ifp, "%s: %s -> %s\n", __func__,
2651 ieee80211_state_name[ic->ic_state],
2652 ieee80211_state_name[nstate]));
2655 * Internal to the driver the INIT and RUN states are used
2656 * so bypass the net80211 state machine for other states.
2657 * Beware however that this requires use to net80211 state
2658 * management that otherwise would be handled for us.
2661 case IEEE80211_S_INIT:
2662 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2663 return sc->sc_newstate(ic, nstate, arg);
2665 case IEEE80211_S_RUN:
2666 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2667 buflen = IEEE80211_ADDR_LEN;
2668 IEEE80211_ADDR_COPY(old_bssid, ni->ni_bssid);
2669 wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen);
2670 IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
2671 buflen = sizeof(val);
2672 wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
2673 /* XXX validate channel */
2674 ni->ni_chan = &ic->ic_channels[le16toh(val)];
2675 ic->ic_curchan = ni->ni_chan;
2676 ic->ic_ibss_chan = ni->ni_chan;
2678 sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
2679 htole16(ni->ni_chan->ic_freq);
2680 sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
2681 htole16(ni->ni_chan->ic_flags);
2684 * XXX hack; unceremoniously clear
2685 * IEEE80211_F_DROPUNENC when operating with
2686 * wep enabled so we don't drop unencoded frames
2687 * at the 802.11 layer. This is necessary because
2688 * we must strip the WEP bit from the 802.11 header
2689 * before passing frames to ieee80211_input because
2690 * the card has already stripped the WEP crypto
2691 * header from the packet.
2693 if (ic->ic_flags & IEEE80211_F_PRIVACY)
2694 ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
2695 if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
2696 /* XXX check return value */
2697 buflen = sizeof(ssid);
2698 wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
2699 ni->ni_esslen = le16toh(ssid.wi_len);
2700 if (ni->ni_esslen > IEEE80211_NWID_LEN)
2701 ni->ni_esslen = IEEE80211_NWID_LEN; /*XXX*/
2702 memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
2704 return sc->sc_newstate(ic, nstate, arg);
2706 case IEEE80211_S_SCAN:
2707 case IEEE80211_S_AUTH:
2708 case IEEE80211_S_ASSOC:
2712 ic->ic_state = nstate; /* NB: skip normal ieee80211 handling */
2717 wi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate)
2722 if (!sc->sc_enabled)
2724 switch (sc->sc_firmware_type) {
2726 wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
2729 val[0] = htole16(chanmask); /* channel */
2730 val[1] = htole16(txrate); /* tx rate */
2731 error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
2735 * XXX only supported on 3.x ?
2737 val[0] = BSCAN_BCAST | BSCAN_ONETIME;
2738 error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
2739 val, sizeof(val[0]));
2743 sc->sc_scan_timer = WI_SCAN_WAIT;
2744 sc->sc_ic.ic_if.if_timer = 1;
2745 DPRINTF((&sc->sc_ic.ic_if, "wi_scan_ap: start scanning, "
2746 "chamask 0x%x txrate 0x%x\n", chanmask, txrate));
2752 wi_scan_result(struct wi_softc *sc, int fid, int cnt)
2754 #define N(a) (sizeof (a) / sizeof (a[0]))
2755 int i, naps, off, szbuf;
2756 struct wi_scan_header ws_hdr; /* Prism2 header */
2757 struct wi_scan_data_p2 ws_dat; /* Prism2 scantable*/
2758 struct wi_apinfo *ap;
2760 off = sizeof(u_int16_t) * 2;
2761 memset(&ws_hdr, 0, sizeof(ws_hdr));
2762 switch (sc->sc_firmware_type) {
2764 wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
2765 off += sizeof(ws_hdr);
2766 szbuf = sizeof(struct wi_scan_data_p2);
2769 szbuf = sizeof(struct wi_scan_data_p2) + 6;
2772 szbuf = sizeof(struct wi_scan_data);
2775 if_printf(&sc->sc_ic.ic_if,
2776 "wi_scan_result: unknown firmware type %u\n",
2777 sc->sc_firmware_type);
2781 naps = (cnt * 2 + 2 - off) / szbuf;
2782 if (naps > N(sc->sc_aps))
2783 naps = N(sc->sc_aps);
2787 memset(&ws_dat, 0, sizeof(ws_dat));
2788 for (i = 0; i < naps; i++, ap++) {
2789 wi_read_bap(sc, fid, off, &ws_dat,
2790 (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
2791 DPRINTF2((&sc->sc_ic.ic_if,
2792 "wi_scan_result: #%d: off %d bssid %6D\n",
2793 i, off, ws_dat.wi_bssid, ":"));
2795 ap->scanreason = le16toh(ws_hdr.wi_reason);
2796 memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
2797 ap->channel = le16toh(ws_dat.wi_chid);
2798 ap->signal = le16toh(ws_dat.wi_signal);
2799 ap->noise = le16toh(ws_dat.wi_noise);
2800 ap->quality = ap->signal - ap->noise;
2801 ap->capinfo = le16toh(ws_dat.wi_capinfo);
2802 ap->interval = le16toh(ws_dat.wi_interval);
2803 ap->rate = le16toh(ws_dat.wi_rate);
2804 ap->namelen = le16toh(ws_dat.wi_namelen);
2805 if (ap->namelen > sizeof(ap->name))
2806 ap->namelen = sizeof(ap->name);
2807 memcpy(ap->name, ws_dat.wi_name, ap->namelen);
2811 sc->sc_scan_timer = 0;
2812 DPRINTF((&sc->sc_ic.ic_if, "wi_scan_result: scan complete: ap %d\n",
2818 wi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi)
2820 ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr),
2821 ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL : -1, rssi);
2822 kprintf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n",
2823 le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1),
2824 le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence);
2825 kprintf(" rx_signal %u rx_rate %u rx_flow %u\n",
2826 wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow);
2827 kprintf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n",
2828 wh->wi_tx_rtry, wh->wi_tx_rate,
2829 le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len));
2830 kprintf(" ehdr dst %6D src %6D type 0x%x\n",
2831 wh->wi_ehdr.ether_dhost, ":", wh->wi_ehdr.ether_shost, ":",
2832 wh->wi_ehdr.ether_type);
2836 wi_alloc(device_t dev, int rid)
2838 struct wi_softc *sc = device_get_softc(dev);
2840 if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2841 sc->iobase_rid = rid;
2842 sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2843 &sc->iobase_rid, 0, ~0, (1 << 6),
2844 rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2846 device_printf(dev, "No I/O space?!\n");
2850 sc->wi_io_addr = rman_get_start(sc->iobase);
2851 sc->wi_btag = rman_get_bustag(sc->iobase);
2852 sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2855 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2856 &sc->mem_rid, RF_ACTIVE);
2859 device_printf(dev, "No Mem space on prism2.5?\n");
2863 sc->wi_btag = rman_get_bustag(sc->mem);
2864 sc->wi_bhandle = rman_get_bushandle(sc->mem);
2869 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2871 ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2875 device_printf(dev, "No irq?!\n");
2883 wi_free(device_t dev)
2885 struct wi_softc *sc = device_get_softc(dev);
2887 if (sc->iobase != NULL) {
2888 bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2891 if (sc->irq != NULL) {
2892 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2895 if (sc->mem != NULL) {
2896 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
2902 wi_get_debug(struct wi_softc *sc, struct wi_req *wreq)
2908 switch (wreq->wi_type) {
2909 case WI_DEBUG_SLEEP:
2911 wreq->wi_val[0] = sc->wi_debug.wi_sleep;
2913 case WI_DEBUG_DELAYSUPP:
2915 wreq->wi_val[0] = sc->wi_debug.wi_delaysupp;
2917 case WI_DEBUG_TXSUPP:
2919 wreq->wi_val[0] = sc->wi_debug.wi_txsupp;
2921 case WI_DEBUG_MONITOR:
2923 wreq->wi_val[0] = sc->wi_debug.wi_monitor;
2925 case WI_DEBUG_LEDTEST:
2927 wreq->wi_val[0] = sc->wi_debug.wi_ledtest;
2928 wreq->wi_val[1] = sc->wi_debug.wi_ledtest_param0;
2929 wreq->wi_val[2] = sc->wi_debug.wi_ledtest_param1;
2931 case WI_DEBUG_CONTTX:
2933 wreq->wi_val[0] = sc->wi_debug.wi_conttx;
2934 wreq->wi_val[1] = sc->wi_debug.wi_conttx_param0;
2936 case WI_DEBUG_CONTRX:
2938 wreq->wi_val[0] = sc->wi_debug.wi_contrx;
2940 case WI_DEBUG_SIGSTATE:
2942 wreq->wi_val[0] = sc->wi_debug.wi_sigstate;
2943 wreq->wi_val[1] = sc->wi_debug.wi_sigstate_param0;
2945 case WI_DEBUG_CONFBITS:
2947 wreq->wi_val[0] = sc->wi_debug.wi_confbits;
2948 wreq->wi_val[1] = sc->wi_debug.wi_confbits_param0;
2959 wi_set_debug(struct wi_softc *sc, struct wi_req *wreq)
2962 u_int16_t cmd, param0 = 0, param1 = 0;
2964 switch (wreq->wi_type) {
2965 case WI_DEBUG_RESET:
2967 case WI_DEBUG_CALENABLE:
2969 case WI_DEBUG_SLEEP:
2970 sc->wi_debug.wi_sleep = 1;
2973 sc->wi_debug.wi_sleep = 0;
2976 param0 = wreq->wi_val[0];
2978 case WI_DEBUG_DELAYSUPP:
2979 sc->wi_debug.wi_delaysupp = 1;
2981 case WI_DEBUG_TXSUPP:
2982 sc->wi_debug.wi_txsupp = 1;
2984 case WI_DEBUG_MONITOR:
2985 sc->wi_debug.wi_monitor = 1;
2987 case WI_DEBUG_LEDTEST:
2988 param0 = wreq->wi_val[0];
2989 param1 = wreq->wi_val[1];
2990 sc->wi_debug.wi_ledtest = 1;
2991 sc->wi_debug.wi_ledtest_param0 = param0;
2992 sc->wi_debug.wi_ledtest_param1 = param1;
2994 case WI_DEBUG_CONTTX:
2995 param0 = wreq->wi_val[0];
2996 sc->wi_debug.wi_conttx = 1;
2997 sc->wi_debug.wi_conttx_param0 = param0;
2999 case WI_DEBUG_STOPTEST:
3000 sc->wi_debug.wi_delaysupp = 0;
3001 sc->wi_debug.wi_txsupp = 0;
3002 sc->wi_debug.wi_monitor = 0;
3003 sc->wi_debug.wi_ledtest = 0;
3004 sc->wi_debug.wi_ledtest_param0 = 0;
3005 sc->wi_debug.wi_ledtest_param1 = 0;
3006 sc->wi_debug.wi_conttx = 0;
3007 sc->wi_debug.wi_conttx_param0 = 0;
3008 sc->wi_debug.wi_contrx = 0;
3009 sc->wi_debug.wi_sigstate = 0;
3010 sc->wi_debug.wi_sigstate_param0 = 0;
3012 case WI_DEBUG_CONTRX:
3013 sc->wi_debug.wi_contrx = 1;
3015 case WI_DEBUG_SIGSTATE:
3016 param0 = wreq->wi_val[0];
3017 sc->wi_debug.wi_sigstate = 1;
3018 sc->wi_debug.wi_sigstate_param0 = param0;
3020 case WI_DEBUG_CONFBITS:
3021 param0 = wreq->wi_val[0];
3022 param1 = wreq->wi_val[1];
3023 sc->wi_debug.wi_confbits = param0;
3024 sc->wi_debug.wi_confbits_param0 = param1;
3034 cmd = WI_CMD_DEBUG | (wreq->wi_type << 8);
3035 error = wi_cmd(sc, cmd, param0, param1, 0);
3041 * Special routines to download firmware for Symbol CF card.
3042 * XXX: This should be modified generic into any PRISM-2 based card.
3045 #define WI_SBCF_PDIADDR 0x3100
3047 /* unaligned load little endian */
3048 #define GETLE32(p) ((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24))
3049 #define GETLE16(p) ((p)[0] | ((p)[1]<<8))
3052 wi_symbol_load_firm(struct wi_softc *sc, const void *primsym, int primlen,
3053 const void *secsym, int seclen)
3058 /* load primary code and run it */
3059 wi_symbol_set_hcr(sc, WI_HCR_EEHOLD);
3060 if (wi_symbol_write_firm(sc, primsym, primlen, NULL, 0))
3062 wi_symbol_set_hcr(sc, WI_HCR_RUN);
3063 for (i = 0; ; i++) {
3066 tsleep(sc, 0, "wiinit", 1);
3067 if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT)
3069 /* write the magic key value to unlock aux port */
3070 CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0);
3071 CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1);
3072 CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2);
3073 CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL);
3076 /* issue read EEPROM command: XXX copied from wi_cmd() */
3077 CSR_WRITE_2(sc, WI_PARAM0, 0);
3078 CSR_WRITE_2(sc, WI_PARAM1, 0);
3079 CSR_WRITE_2(sc, WI_PARAM2, 0);
3080 CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE);
3081 for (i = 0; i < WI_TIMEOUT; i++) {
3082 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
3086 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
3088 CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ);
3089 CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ);
3090 CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA,
3091 (uint16_t *)ebuf, sizeof(ebuf) / 2);
3092 if (GETLE16(ebuf) > sizeof(ebuf))
3094 if (wi_symbol_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf)))
3100 wi_symbol_write_firm(struct wi_softc *sc, const void *buf, int buflen,
3101 const void *ebuf, int ebuflen)
3103 const uint8_t *p, *ep, *q, *eq;
3105 uint32_t addr, id, eid;
3106 int i, len, elen, nblk, pdrlen;
3109 * Parse the header of the firmware image.
3113 while (p < ep && *p++ != ' '); /* FILE: */
3114 while (p < ep && *p++ != ' '); /* filename */
3115 while (p < ep && *p++ != ' '); /* type of the firmware */
3116 nblk = strtoul(p, &tp, 10);
3118 pdrlen = strtoul(p + 1, &tp, 10);
3120 while (p < ep && *p++ != 0x1a); /* skip rest of header */
3123 * Block records: address[4], length[2], data[length];
3125 for (i = 0; i < nblk; i++) {
3126 addr = GETLE32(p); p += 4;
3127 len = GETLE16(p); p += 2;
3128 CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3129 CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3130 CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3131 (const uint16_t *)p, len / 2);
3136 * PDR: id[4], address[4], length[4];
3138 for (i = 0; i < pdrlen; ) {
3139 id = GETLE32(p); p += 4; i += 4;
3140 addr = GETLE32(p); p += 4; i += 4;
3141 len = GETLE32(p); p += 4; i += 4;
3142 /* replace PDR entry with the values from EEPROM, if any */
3143 for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) {
3144 elen = GETLE16(q); q += 2;
3145 eid = GETLE16(q); q += 2;
3146 elen--; /* elen includes eid */
3151 CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3152 CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3153 CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3154 (const uint16_t *)q, len / 2);
3162 wi_symbol_set_hcr(struct wi_softc *sc, int mode)
3166 CSR_WRITE_2(sc, WI_COR, WI_COR_RESET);
3167 tsleep(sc, 0, "wiinit", 1);
3168 hcr = CSR_READ_2(sc, WI_HCR);
3169 hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE);
3170 CSR_WRITE_2(sc, WI_HCR, hcr);
3171 tsleep(sc, 0, "wiinit", 1);
3172 CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE);
3173 tsleep(sc, 0, "wiinit", 1);