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.166 2004/04/01 00:38:45 sam Exp $
35 * $DragonFly: src/sys/dev/netif/wi/if_wi.c,v 1.19 2005/02/02 14:14:20 joerg 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 <sys/param.h>
71 #include <sys/endian.h>
72 #include <sys/systm.h>
73 #include <sys/sockio.h>
76 #include <sys/kernel.h>
77 #include <sys/socket.h>
78 #include <sys/module.h>
80 #include <sys/random.h>
81 #include <sys/syslog.h>
82 #include <sys/sysctl.h>
84 #include <machine/bus.h>
85 #include <machine/resource.h>
86 #include <machine/clock.h>
87 #include <machine/atomic.h>
91 #include <net/if_arp.h>
92 #include <net/ethernet.h>
93 #include <net/if_dl.h>
94 #include <net/if_media.h>
95 #include <net/if_types.h>
97 #include <netproto/802_11/ieee80211_var.h>
98 #include <netproto/802_11/ieee80211_ioctl.h>
99 #include <netproto/802_11/ieee80211_radiotap.h>
100 #include <netproto/802_11/if_wavelan_ieee.h>
102 #include <netinet/in.h>
103 #include <netinet/in_systm.h>
104 #include <netinet/in_var.h>
105 #include <netinet/ip.h>
106 #include <netinet/if_ether.h>
110 #include <dev/netif/wi/if_wireg.h>
111 #include <dev/netif/wi/if_wivar.h>
113 static void wi_start(struct ifnet *);
114 static int wi_reset(struct wi_softc *);
115 static void wi_watchdog(struct ifnet *);
116 static int wi_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
117 static int wi_media_change(struct ifnet *);
118 static void wi_media_status(struct ifnet *, struct ifmediareq *);
120 static void wi_rx_intr(struct wi_softc *);
121 static void wi_tx_intr(struct wi_softc *);
122 static void wi_tx_ex_intr(struct wi_softc *);
123 static void wi_info_intr(struct wi_softc *);
125 static int wi_get_cfg(struct ifnet *, u_long, caddr_t, struct ucred *);
126 static int wi_set_cfg(struct ifnet *, u_long, caddr_t);
127 static int wi_write_txrate(struct wi_softc *);
128 static int wi_write_wep(struct wi_softc *);
129 static int wi_write_multi(struct wi_softc *);
130 static int wi_alloc_fid(struct wi_softc *, int, int *);
131 static void wi_read_nicid(struct wi_softc *);
132 static int wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
134 static int wi_cmd(struct wi_softc *, int, int, int, int);
135 static int wi_seek_bap(struct wi_softc *, int, int);
136 static int wi_read_bap(struct wi_softc *, int, int, void *, int);
137 static int wi_write_bap(struct wi_softc *, int, int, void *, int);
138 static int wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
139 static int wi_read_rid(struct wi_softc *, int, void *, int *);
140 static int wi_write_rid(struct wi_softc *, int, void *, int);
142 static int wi_newstate(struct ieee80211com *, enum ieee80211_state, int);
144 static int wi_scan_ap(struct wi_softc *, u_int16_t, u_int16_t);
145 static void wi_scan_result(struct wi_softc *, int, int);
147 static void wi_dump_pkt(struct wi_frame *, struct ieee80211_node *, int rssi);
149 static int wi_get_debug(struct wi_softc *, struct wi_req *);
150 static int wi_set_debug(struct wi_softc *, struct wi_req *);
152 /* support to download firmware for symbol CF card */
153 static int wi_symbol_write_firm(struct wi_softc *, const void *, int,
155 static int wi_symbol_set_hcr(struct wi_softc *, int);
158 wi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
162 return wi_write_rid(sc, rid, &val, sizeof(val));
165 SYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters");
167 static struct timeval lasttxerror; /* time of last tx error msg */
168 static int curtxeps; /* current tx error msgs/sec */
169 static int wi_txerate = 0; /* tx error rate: max msgs/sec */
170 SYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate,
171 0, "max tx error msgs/sec; 0 to disable msgs");
175 static int wi_debug = 0;
176 SYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug,
177 0, "control debugging printfs");
179 #define DPRINTF(X) if (wi_debug) printf X
180 #define DPRINTF2(X) if (wi_debug > 1) printf X
181 #define IFF_DUMPPKTS(_ifp) \
182 (((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
186 #define IFF_DUMPPKTS(_ifp) 0
189 #define WI_INTRS (WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
191 struct wi_card_ident wi_card_ident[] = {
192 /* CARD_ID CARD_NAME FIRM_TYPE */
193 { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT },
194 { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT },
195 { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT },
196 { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL },
197 { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL },
198 { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL },
199 { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL },
200 { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL },
201 { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL },
202 { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL },
203 { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL },
204 { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL },
205 { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
206 { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
207 { WI_NIC_3842_PCMCIA_ATL_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
208 { WI_NIC_3842_PCMCIA_ATS_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
209 { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
210 { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
211 { WI_NIC_3842_MINI_ATL_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
212 { WI_NIC_3842_MINI_ATS_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
213 { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
214 { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
215 { WI_NIC_3842_PCI_ATS_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
216 { WI_NIC_3842_PCI_ATL_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
217 { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
218 { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
219 { WI_NIC_P3_PCMCIA_ATL_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
220 { WI_NIC_P3_PCMCIA_ATS_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
221 { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
222 { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
223 { WI_NIC_P3_MINI_ATL_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
224 { WI_NIC_P3_MINI_ATS_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
228 devclass_t wi_devclass;
231 wi_attach(device_t dev)
233 struct wi_softc *sc = device_get_softc(dev);
234 struct ieee80211com *ic = &sc->sc_ic;
235 struct ifnet *ifp = &ic->ic_if;
236 int i, nrates, buflen;
238 u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
239 struct ieee80211_rateset *rs;
240 static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
241 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
246 * NB: no locking is needed here; don't put it here
247 * unless you can prove it!
249 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
250 wi_intr, sc, &sc->wi_intrhand);
253 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
258 sc->wi_cmd_count = 500;
260 if (wi_reset(sc) != 0)
261 return ENXIO; /* XXX */
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);
275 if (error || IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) {
277 device_printf(dev, "mac read failed %d\n", error);
279 device_printf(dev, "mac read failed (all zeros)\n");
284 /* Read NIC identification */
288 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
289 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
290 ifp->if_ioctl = wi_ioctl;
291 ifp->if_start = wi_start;
292 ifp->if_watchdog = wi_watchdog;
293 ifp->if_init = wi_init;
294 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
295 #ifdef DEVICE_POLLING
296 ifp->if_capabilities |= IFCAP_POLLING;
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 | IEEE80211_C_AHDEMO;
303 ic->ic_state = IEEE80211_S_INIT;
306 * Query the card for available channels and setup the
307 * channel table. We assume these are all 11b channels.
309 buflen = sizeof(val);
310 if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
311 val = htole16(0x1fff); /* assume 1-11 */
312 KASSERT(val != 0, ("wi_attach: no available channels listed!"));
314 val <<= 1; /* shift for base 1 indices */
315 for (i = 1; i < 16; i++) {
316 if (isset((u_int8_t*)&val, i)) {
317 ic->ic_channels[i].ic_freq =
318 ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
319 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
324 * Read the default channel from the NIC. This may vary
325 * depending on the country where the NIC was purchased, so
326 * we can't hard-code a default and expect it to work for
329 * If no channel is specified, let the 802.11 code select.
331 buflen = sizeof(val);
332 if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0) {
334 KASSERT(val < IEEE80211_CHAN_MAX &&
335 ic->ic_channels[val].ic_flags != 0,
336 ("wi_attach: invalid own channel %u!", val));
337 ic->ic_ibss_chan = &ic->ic_channels[val];
340 "WI_RID_OWN_CHNL failed, using first channel!\n");
341 ic->ic_ibss_chan = &ic->ic_channels[0];
345 * Set flags based on firmware version.
347 switch (sc->sc_firmware_type) {
350 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
351 #ifdef WI_HERMES_AUTOINC_WAR
352 /* XXX: not confirmed, but never seen for recent firmware */
353 if (sc->sc_sta_firmware_ver < 40000) {
354 sc->sc_flags |= WI_FLAGS_BUG_AUTOINC;
357 if (sc->sc_sta_firmware_ver >= 60000)
358 sc->sc_flags |= WI_FLAGS_HAS_MOR;
359 if (sc->sc_sta_firmware_ver >= 60006) {
360 ic->ic_caps |= IEEE80211_C_IBSS;
361 ic->ic_caps |= IEEE80211_C_MONITOR;
363 sc->sc_ibss_port = htole16(1);
365 sc->sc_min_rssi = WI_LUCENT_MIN_RSSI;
366 sc->sc_max_rssi = WI_LUCENT_MAX_RSSI;
367 sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
371 sc->sc_ntxbuf = WI_NTXBUF;
372 sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR;
373 sc->sc_flags |= WI_FLAGS_HAS_ROAMING;
374 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
376 * Old firmware are slow, so give peace a chance.
378 if (sc->sc_sta_firmware_ver < 10000)
379 sc->wi_cmd_count = 5000;
380 if (sc->sc_sta_firmware_ver > 10101)
381 sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
382 if (sc->sc_sta_firmware_ver >= 800) {
383 ic->ic_caps |= IEEE80211_C_IBSS;
384 ic->ic_caps |= IEEE80211_C_MONITOR;
387 * version 0.8.3 and newer are the only ones that are known
388 * to currently work. Earlier versions can be made to work,
389 * at least according to the Linux driver.
391 if (sc->sc_sta_firmware_ver >= 803)
392 ic->ic_caps |= IEEE80211_C_HOSTAP;
393 sc->sc_ibss_port = htole16(0);
395 sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
396 sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
397 sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
402 sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY;
403 if (sc->sc_sta_firmware_ver >= 25000)
404 ic->ic_caps |= IEEE80211_C_IBSS;
405 sc->sc_ibss_port = htole16(4);
407 sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
408 sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
409 sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
414 * Find out if we support WEP on this card.
416 buflen = sizeof(val);
417 if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
419 ic->ic_caps |= IEEE80211_C_WEP;
421 /* Find supported rates. */
422 buflen = sizeof(ratebuf);
423 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
424 if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
425 nrates = le16toh(*(u_int16_t *)ratebuf);
426 if (nrates > IEEE80211_RATE_MAXSIZE)
427 nrates = IEEE80211_RATE_MAXSIZE;
429 for (i = 0; i < nrates; i++)
431 rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i];
433 /* XXX fallback on error? */
437 buflen = sizeof(val);
438 if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
439 wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
440 sc->sc_dbm_offset = le16toh(val);
443 sc->sc_max_datalen = 2304;
444 sc->sc_system_scale = 1;
445 sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN;
446 sc->sc_roaming_mode = 1;
448 sc->sc_portnum = WI_DEFAULT_PORT;
449 sc->sc_authtype = WI_DEFAULT_AUTHTYPE;
451 bzero(sc->sc_nodename, sizeof(sc->sc_nodename));
452 sc->sc_nodelen = sizeof(WI_DEFAULT_NODENAME) - 1;
453 bcopy(WI_DEFAULT_NODENAME, sc->sc_nodename, sc->sc_nodelen);
455 bzero(sc->sc_net_name, sizeof(sc->sc_net_name));
456 bcopy(WI_DEFAULT_NETNAME, sc->sc_net_name,
457 sizeof(WI_DEFAULT_NETNAME) - 1);
460 * Call MI attach routine.
462 ieee80211_ifattach(ifp);
463 /* override state transition method */
464 sc->sc_newstate = ic->ic_newstate;
465 ic->ic_newstate = wi_newstate;
466 ieee80211_media_init(ifp, wi_media_change, wi_media_status);
468 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
469 sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
472 * Initialize constant fields.
473 * XXX make header lengths a multiple of 32-bits so subsequent
474 * headers are properly aligned; this is a kludge to keep
475 * certain applications happy.
477 * NB: the channel is setup each time we transition to the
478 * RUN state to avoid filling it in for each frame.
480 sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t));
481 sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len);
482 sc->sc_tx_th.wt_ihdr.it_present = htole32(WI_TX_RADIOTAP_PRESENT);
484 sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t));
485 sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len);
486 sc->sc_rx_th.wr_ihdr.it_present = htole32(WI_RX_RADIOTAP_PRESENT);
492 wi_detach(device_t dev)
494 struct wi_softc *sc = device_get_softc(dev);
495 struct ifnet *ifp = &sc->sc_ic.ic_if;
500 /* check if device was removed */
501 sc->wi_gone |= !bus_child_present(dev);
505 ieee80211_ifdetach(ifp);
507 bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
513 wi_shutdown(device_t dev)
515 struct wi_softc *sc = device_get_softc(dev);
517 wi_stop(&sc->sc_if, 1);
520 #ifdef DEVICE_POLLING
522 wi_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
524 struct wi_softc *sc = ifp->if_softc;
527 if ((ifp->if_capenable & IFCAP_POLLING) == 0) {
528 ether_poll_deregister(ifp);
529 cmd = POLL_DEREGISTER;
531 if (cmd == POLL_DEREGISTER) {
532 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
536 status = CSR_READ_2(sc, WI_EVENT_STAT);
538 if (status & WI_EV_RX)
540 if (status & WI_EV_ALLOC)
542 if (status & WI_EV_INFO)
545 if (cmd == POLL_AND_CHECK_STATUS) {
546 if (status & WI_EV_INFO)
550 if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
551 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 &&
552 IF_QLEN(&ifp->if_snd) != NULL)
555 #endif /* DEVICE_POLLING */
560 struct wi_softc *sc = arg;
561 struct ifnet *ifp = &sc->sc_ic.ic_if;
565 #ifdef DEVICE_POLLING
566 if (ifp->if_flags & IFF_POLLING)
568 if ((ifp->if_capenable & IFCAP_POLLING) &&
569 (ether_poll_register(wi_poll, ifp))) {
570 CSR_WRITE_2(sc, WI_INT_EN, 0);
574 #endif /* DEVICE_POLLING */
576 if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) {
577 CSR_WRITE_2(sc, WI_INT_EN, 0);
578 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
584 /* Disable interrupts. */
585 CSR_WRITE_2(sc, WI_INT_EN, 0);
587 status = CSR_READ_2(sc, WI_EVENT_STAT);
588 if (status & WI_EV_RX)
590 if (status & WI_EV_ALLOC)
592 if (status & WI_EV_TX_EXC)
594 if (status & WI_EV_INFO)
596 if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
597 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 &&
598 IF_QLEN(&ifp->if_snd) != 0)
601 /* Re-enable interrupts. */
602 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
612 struct wi_softc *sc = arg;
613 struct ifnet *ifp = &sc->sc_if;
614 struct ieee80211com *ic = &sc->sc_ic;
615 struct wi_joinreq join;
617 int error = 0, wasenabled;
619 struct sockaddr_dl *sdl;
629 if ((wasenabled = sc->sc_enabled))
633 /* common 802.11 configuration */
634 ic->ic_flags &= ~IEEE80211_F_IBSSON;
635 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
636 switch (ic->ic_opmode) {
637 case IEEE80211_M_STA:
638 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS);
640 case IEEE80211_M_IBSS:
641 wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port);
642 ic->ic_flags |= IEEE80211_F_IBSSON;
644 case IEEE80211_M_AHDEMO:
645 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
647 case IEEE80211_M_HOSTAP:
649 * For PRISM cards, override the empty SSID, because in
650 * HostAP mode the controller will lock up otherwise.
652 if (sc->sc_firmware_type == WI_INTERSIL &&
653 ic->ic_des_esslen == 0) {
654 ic->ic_des_essid[0] = ' ';
655 ic->ic_des_esslen = 1;
657 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP);
659 case IEEE80211_M_MONITOR:
660 if (sc->sc_firmware_type == WI_LUCENT)
661 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
662 wi_cmd(sc, WI_CMD_DEBUG | (WI_TEST_MONITOR << 8), 0, 0, 0);
666 /* Intersil interprets this RID as joining ESS even in IBSS mode */
667 if (sc->sc_firmware_type == WI_LUCENT &&
668 (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0)
669 wi_write_val(sc, WI_RID_CREATE_IBSS, 1);
671 wi_write_val(sc, WI_RID_CREATE_IBSS, 0);
672 wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
673 wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid,
675 wi_write_val(sc, WI_RID_OWN_CHNL,
676 ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
677 wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen);
679 ifa = ifaddr_byindex(ifp->if_index);
680 sdl = (struct sockaddr_dl *) ifa->ifa_addr;
681 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(sdl));
682 wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN);
684 wi_write_val(sc, WI_RID_PM_ENABLED,
685 (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
687 /* not yet common 802.11 configuration */
688 wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen);
689 wi_write_val(sc, WI_RID_RTS_THRESH, ic->ic_rtsthreshold);
690 if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
691 wi_write_val(sc, WI_RID_FRAG_THRESH, ic->ic_fragthreshold);
693 /* driver specific 802.11 configuration */
694 if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)
695 wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale);
696 if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
697 wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode);
698 if (sc->sc_flags & WI_FLAGS_HAS_MOR)
699 wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven);
701 wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen);
703 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
704 sc->sc_firmware_type == WI_INTERSIL) {
705 wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval);
706 wi_write_val(sc, WI_RID_BASIC_RATE, 0x03); /* 1, 2 */
707 wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */
708 wi_write_val(sc, WI_RID_DTIM_PERIOD, 1);
712 * Initialize promisc mode.
713 * Being in the Host-AP mode causes a great
714 * deal of pain if primisc mode is set.
715 * Therefore we avoid confusing the firmware
716 * and always reset promisc mode in Host-AP
717 * mode. Host-AP sees all the packets anyway.
719 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
720 (ifp->if_flags & IFF_PROMISC) != 0) {
721 wi_write_val(sc, WI_RID_PROMISC, 1);
723 wi_write_val(sc, WI_RID_PROMISC, 0);
727 if (ic->ic_caps & IEEE80211_C_WEP)
730 /* Set multicast filter. */
733 /* Allocate fids for the card */
734 if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
735 sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
736 if (sc->sc_firmware_type == WI_SYMBOL)
737 sc->sc_buflen = 1585; /* XXX */
738 for (i = 0; i < sc->sc_ntxbuf; i++) {
739 error = wi_alloc_fid(sc, sc->sc_buflen,
740 &sc->sc_txd[i].d_fid);
742 device_printf(sc->sc_dev,
743 "tx buffer allocation failed (error %u)\n",
747 sc->sc_txd[i].d_len = 0;
750 sc->sc_txcur = sc->sc_txnext = 0;
752 /* Enable desired port */
753 wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
756 ifp->if_flags |= IFF_RUNNING;
757 ifp->if_flags &= ~IFF_OACTIVE;
758 if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
759 ic->ic_opmode == IEEE80211_M_MONITOR ||
760 ic->ic_opmode == IEEE80211_M_HOSTAP)
761 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
763 /* Enable interrupts if not polling */
764 #ifdef DEVICE_POLLING
765 if ((ifp->if_flags & IFF_POLLING) == 0)
767 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
770 ic->ic_opmode == IEEE80211_M_HOSTAP &&
771 sc->sc_firmware_type == WI_INTERSIL) {
772 /* XXX: some card need to be re-enabled for hostap */
773 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
774 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
777 if (ic->ic_opmode == IEEE80211_M_STA &&
778 ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
779 ic->ic_des_chan != IEEE80211_CHAN_ANYC)) {
780 memset(&join, 0, sizeof(join));
781 if (ic->ic_flags & IEEE80211_F_DESBSSID)
782 IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
783 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC)
784 join.wi_chan = htole16(
785 ieee80211_chan2ieee(ic, ic->ic_des_chan));
786 /* Lucent firmware does not support the JOIN RID. */
787 if (sc->sc_firmware_type != WI_LUCENT)
788 wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
795 if_printf(ifp, "interface not running\n");
799 DPRINTF(("wi_init: return %d\n", error));
804 wi_stop(struct ifnet *ifp, int disable)
806 struct ieee80211com *ic = (struct ieee80211com *) ifp;
807 struct wi_softc *sc = ifp->if_softc;
814 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
815 if (sc->sc_enabled && !sc->wi_gone) {
816 CSR_WRITE_2(sc, WI_INT_EN, 0);
817 wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
821 (*sc->sc_disable)(sc);
825 } else if (sc->wi_gone && disable) /* gone --> not enabled */
829 sc->sc_scan_timer = 0;
830 sc->sc_syn_timer = 0;
831 sc->sc_false_syns = 0;
833 ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
834 #ifdef DEVICE_POLLING
835 ether_poll_deregister(ifp);
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;
860 if (sc->sc_flags & WI_FLAGS_OUTRANGE) {
865 memset(&frmhdr, 0, sizeof(frmhdr));
868 IF_POLL(&ic->ic_mgtq, m0);
870 if (sc->sc_txd[cur].d_len != 0) {
871 ifp->if_flags |= IFF_OACTIVE;
874 IF_DEQUEUE(&ic->ic_mgtq, m0);
876 * Hack! The referenced node pointer is in the
877 * rcvif field of the packet header. This is
878 * placed there by ieee80211_mgmt_output because
879 * we need to hold the reference with the frame
880 * and there's no other way (other than packet
881 * tags which we consider too expensive to use)
884 ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
885 m0->m_pkthdr.rcvif = NULL;
887 m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
888 (caddr_t)&frmhdr.wi_ehdr);
889 frmhdr.wi_ehdr.ether_type = 0;
890 wh = mtod(m0, struct ieee80211_frame *);
892 if (ic->ic_state != IEEE80211_S_RUN)
894 IFQ_POLL(&ifp->if_snd, m0);
897 if (sc->sc_txd[cur].d_len != 0) {
898 ifp->if_flags |= IFF_OACTIVE;
901 IFQ_DEQUEUE(&ifp->if_snd, m0);
903 m_copydata(m0, 0, ETHER_HDR_LEN,
904 (caddr_t)&frmhdr.wi_ehdr);
907 m0 = ieee80211_encap(ifp, m0, &ni);
912 wh = mtod(m0, struct ieee80211_frame *);
913 if (ic->ic_flags & IEEE80211_F_WEPON)
914 wh->i_fc[1] |= IEEE80211_FC1_WEP;
918 if (ic->ic_rawbpf != NULL)
919 bpf_mtap(ic->ic_rawbpf, m0);
921 frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
922 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
923 (wh->i_fc[1] & IEEE80211_FC1_WEP)) {
924 if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) {
926 if (ni && ni != ic->ic_bss)
927 ieee80211_free_node(ic, ni);
930 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
934 sc->sc_tx_th.wt_rate =
935 ni->ni_rates.rs_rates[ni->ni_txrate];
936 bpf_ptap(sc->sc_drvbpf, m0, &sc->sc_tx_th,
940 m_copydata(m0, 0, sizeof(struct ieee80211_frame),
941 (caddr_t)&frmhdr.wi_whdr);
942 m_adj(m0, sizeof(struct ieee80211_frame));
943 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
944 if (IFF_DUMPPKTS(ifp))
945 wi_dump_pkt(&frmhdr, NULL, -1);
946 fid = sc->sc_txd[cur].d_fid;
947 off = sizeof(frmhdr);
948 error = wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0
949 || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
951 if (ni && ni != ic->ic_bss)
952 ieee80211_free_node(ic, ni);
957 sc->sc_txd[cur].d_len = off;
958 if (sc->sc_txcur == cur) {
959 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
960 if_printf(ifp, "xmit failed\n");
961 sc->sc_txd[cur].d_len = 0;
967 sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
974 wi_reset(struct wi_softc *sc)
976 struct ieee80211com *ic = &sc->sc_ic;
977 struct ifnet *ifp = &ic->ic_if;
978 #define WI_INIT_TRIES 3
983 /* Symbol firmware cannot be initialized more than once */
984 if (sc->sc_firmware_type == WI_SYMBOL && sc->sc_reset)
986 if (sc->sc_firmware_type == WI_SYMBOL)
989 tries = WI_INIT_TRIES;
991 for (i = 0; i < tries; i++) {
992 if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
994 DELAY(WI_DELAY * 1000);
999 if_printf(ifp, "init failed\n");
1003 CSR_WRITE_2(sc, WI_INT_EN, 0);
1004 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
1006 /* Calibrate timer. */
1007 wi_write_val(sc, WI_RID_TICK_TIME, 8);
1010 #undef WI_INIT_TRIES
1014 wi_watchdog(struct ifnet *ifp)
1016 struct wi_softc *sc = ifp->if_softc;
1019 if (!sc->sc_enabled)
1022 if (sc->sc_tx_timer) {
1023 if (--sc->sc_tx_timer == 0) {
1024 if_printf(ifp, "device timeout\n");
1026 wi_init(ifp->if_softc);
1032 if (sc->sc_scan_timer) {
1033 if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
1034 sc->sc_firmware_type == WI_INTERSIL) {
1035 DPRINTF(("wi_watchdog: inquire scan\n"));
1036 wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
1038 if (sc->sc_scan_timer)
1042 if (sc->sc_syn_timer) {
1043 if (--sc->sc_syn_timer == 0) {
1044 struct ieee80211com *ic = (struct ieee80211com *) ifp;
1045 DPRINTF2(("wi_watchdog: %d false syns\n",
1046 sc->sc_false_syns));
1047 sc->sc_false_syns = 0;
1048 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1049 sc->sc_syn_timer = 5;
1054 /* TODO: rate control */
1055 ieee80211_watchdog(ifp);
1059 wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1061 struct wi_softc *sc = ifp->if_softc;
1062 struct ieee80211com *ic = &sc->sc_ic;
1063 struct ifreq *ifr = (struct ifreq *)data;
1064 struct ieee80211req *ireq;
1065 u_int8_t nodename[IEEE80211_NWID_LEN];
1080 * Can't do promisc and hostap at the same time. If all that's
1081 * changing is the promisc flag, try to short-circuit a call to
1082 * wi_init() by just setting PROMISC in the hardware.
1084 if (ifp->if_flags & IFF_UP) {
1085 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1086 ifp->if_flags & IFF_RUNNING) {
1087 if (ifp->if_flags & IFF_PROMISC &&
1088 !(sc->sc_if_flags & IFF_PROMISC)) {
1089 wi_write_val(sc, WI_RID_PROMISC, 1);
1090 } else if (!(ifp->if_flags & IFF_PROMISC) &&
1091 sc->sc_if_flags & IFF_PROMISC) {
1092 wi_write_val(sc, WI_RID_PROMISC, 0);
1100 if (ifp->if_flags & IFF_RUNNING) {
1105 sc->sc_if_flags = ifp->if_flags;
1110 error = wi_write_multi(sc);
1112 case SIOCGIFGENERIC:
1113 error = wi_get_cfg(ifp, cmd, data, cr);
1115 case SIOCSIFGENERIC:
1116 error = suser_cred(cr, NULL_CRED_OKAY);
1119 error = wi_set_cfg(ifp, cmd, data);
1121 case SIOCGPRISM2DEBUG:
1122 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1125 if (!(ifp->if_flags & IFF_RUNNING) ||
1126 sc->sc_firmware_type == WI_LUCENT) {
1130 error = wi_get_debug(sc, &wreq);
1132 error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
1134 case SIOCSPRISM2DEBUG:
1135 if ((error = suser_cred(cr, NULL_CRED_OKAY)))
1137 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1140 error = wi_set_debug(sc, &wreq);
1143 ireq = (struct ieee80211req *) data;
1144 switch (ireq->i_type) {
1145 case IEEE80211_IOC_STATIONNAME:
1146 ireq->i_len = sc->sc_nodelen + 1;
1147 error = copyout(sc->sc_nodename, ireq->i_data,
1151 error = ieee80211_ioctl(ifp, cmd, data, cr);
1156 error = suser_cred(cr, NULL_CRED_OKAY);
1159 ireq = (struct ieee80211req *) data;
1160 switch (ireq->i_type) {
1161 case IEEE80211_IOC_STATIONNAME:
1162 if (ireq->i_val != 0 ||
1163 ireq->i_len > IEEE80211_NWID_LEN) {
1167 memset(nodename, 0, IEEE80211_NWID_LEN);
1168 error = copyin(ireq->i_data, nodename, ireq->i_len);
1171 if (sc->sc_enabled) {
1172 error = wi_write_ssid(sc, WI_RID_NODENAME,
1173 nodename, ireq->i_len);
1177 memcpy(sc->sc_nodename, nodename, IEEE80211_NWID_LEN);
1178 sc->sc_nodelen = ireq->i_len;
1181 error = ieee80211_ioctl(ifp, cmd, data, cr);
1186 ifp->if_capenable &= ~(IFCAP_POLLING);
1187 ifp->if_capenable |= ifr->ifr_reqcap & (IFCAP_POLLING);
1188 if (ifp->if_flags & IFF_RUNNING)
1192 error = ieee80211_ioctl(ifp, cmd, data, cr);
1195 if (error == ENETRESET) {
1197 wi_init(sc); /* XXX no error return */
1207 wi_media_change(struct ifnet *ifp)
1209 struct wi_softc *sc = ifp->if_softc;
1212 error = ieee80211_media_change(ifp);
1213 if (error == ENETRESET) {
1215 wi_init(sc); /* XXX no error return */
1222 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1224 struct wi_softc *sc = ifp->if_softc;
1225 struct ieee80211com *ic = &sc->sc_ic;
1229 if (sc->wi_gone || !sc->sc_enabled) {
1230 imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
1231 imr->ifm_status = 0;
1235 imr->ifm_status = IFM_AVALID;
1236 imr->ifm_active = IFM_IEEE80211;
1237 if (ic->ic_state == IEEE80211_S_RUN &&
1238 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
1239 imr->ifm_status |= IFM_ACTIVE;
1241 if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) != 0)
1244 /* convert to 802.11 rate */
1246 if (sc->sc_firmware_type == WI_LUCENT) {
1248 rate = 11; /* 5.5Mbps */
1249 else if (rate == 5 * 2)
1250 rate = 22; /* 11Mbps */
1253 rate = 11; /* 5.5Mbps */
1254 else if (rate == 8*2)
1255 rate = 22; /* 11Mbps */
1258 imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
1259 switch (ic->ic_opmode) {
1260 case IEEE80211_M_STA:
1262 case IEEE80211_M_IBSS:
1263 imr->ifm_active |= IFM_IEEE80211_ADHOC;
1265 case IEEE80211_M_AHDEMO:
1266 imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1268 case IEEE80211_M_HOSTAP:
1269 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1271 case IEEE80211_M_MONITOR:
1272 imr->ifm_active |= IFM_IEEE80211_MONITOR;
1278 wi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1280 struct ieee80211com *ic = &sc->sc_ic;
1281 struct ieee80211_node *ni = ic->ic_bss;
1282 struct ifnet *ifp = &ic->ic_if;
1284 if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1287 DPRINTF(("wi_sync_bssid: bssid %6D -> ", ni->ni_bssid, ":"));
1288 DPRINTF(("%6D ?\n", new_bssid, ":"));
1290 /* In promiscuous mode, the BSSID field is not a reliable
1291 * indicator of the firmware's BSSID. Damp spurious
1292 * change-of-BSSID indications.
1294 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1295 sc->sc_false_syns >= WI_MAX_FALSE_SYNS)
1298 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1302 wi_rx_monitor(struct wi_softc *sc, int fid)
1304 struct ieee80211com *ic = &sc->sc_ic;
1305 struct ifnet *ifp = &ic->ic_if;
1306 struct wi_frame *rx_frame;
1310 /* first allocate mbuf for packet storage */
1311 m = m_getcl(MB_DONTWAIT, MT_DATA, 0);
1317 m->m_pkthdr.rcvif = ifp;
1319 /* now read wi_frame first so we know how much data to read */
1320 if (wi_read_bap(sc, fid, 0, mtod(m, caddr_t), sizeof(*rx_frame))) {
1325 rx_frame = mtod(m, struct wi_frame *);
1327 switch ((rx_frame->wi_status & WI_STAT_MAC_PORT) >> 8) {
1329 switch (rx_frame->wi_whdr.i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1330 case IEEE80211_FC0_TYPE_DATA:
1331 hdrlen = WI_DATA_HDRLEN;
1332 datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1334 case IEEE80211_FC0_TYPE_MGT:
1335 hdrlen = WI_MGMT_HDRLEN;
1336 datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1338 case IEEE80211_FC0_TYPE_CTL:
1340 * prism2 cards don't pass control packets
1341 * down properly or consistently, so we'll only
1342 * pass down the header.
1344 hdrlen = WI_CTL_HDRLEN;
1348 if_printf(ifp, "received packet of unknown type "
1355 hdrlen = WI_DATA_HDRLEN;
1356 datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1359 if_printf(ifp, "received packet on invalid "
1360 "port (wi_status=0x%x)\n", rx_frame->wi_status);
1365 if (hdrlen + datlen + 2 > MCLBYTES) {
1366 if_printf(ifp, "oversized packet received "
1367 "(wi_dat_len=%d, wi_status=0x%x)\n",
1368 datlen, rx_frame->wi_status);
1373 if (wi_read_bap(sc, fid, hdrlen, mtod(m, caddr_t) + hdrlen,
1375 m->m_pkthdr.len = m->m_len = hdrlen + datlen;
1377 BPF_MTAP(ifp, m); /* Handle BPF listeners. */
1385 wi_rx_intr(struct wi_softc *sc)
1387 struct ieee80211com *ic = &sc->sc_ic;
1388 struct ifnet *ifp = &ic->ic_if;
1389 struct wi_frame frmhdr;
1391 struct ieee80211_frame *wh;
1392 struct ieee80211_node *ni;
1393 int fid, len, off, rssi;
1398 fid = CSR_READ_2(sc, WI_RX_FID);
1400 if (sc->wi_debug.wi_monitor) {
1402 * If we are in monitor mode just
1403 * read the data from the device.
1405 wi_rx_monitor(sc, fid);
1406 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1410 /* First read in the frame header */
1411 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1412 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1414 DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1418 if (IFF_DUMPPKTS(ifp))
1419 wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal);
1422 * Drop undecryptable or packets with receive errors here
1424 status = le16toh(frmhdr.wi_status);
1425 if (status & WI_STAT_ERRSTAT) {
1426 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1428 DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1431 rssi = frmhdr.wi_rx_signal;
1432 rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1433 le16toh(frmhdr.wi_rx_tstamp1);
1435 len = le16toh(frmhdr.wi_dat_len);
1436 off = ALIGN(sizeof(struct ieee80211_frame));
1439 * Sometimes the PRISM2.x returns bogusly large frames. Except
1440 * in monitor mode, just throw them away.
1442 if (off + len > MCLBYTES) {
1443 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1444 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1446 DPRINTF(("wi_rx_intr: oversized packet\n"));
1452 MGETHDR(m, MB_DONTWAIT, MT_DATA);
1454 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1456 DPRINTF(("wi_rx_intr: MGET failed\n"));
1459 if (off + len > MHLEN) {
1460 MCLGET(m, MB_DONTWAIT);
1461 if ((m->m_flags & M_EXT) == 0) {
1462 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1465 DPRINTF(("wi_rx_intr: MCLGET failed\n"));
1470 m->m_data += off - sizeof(struct ieee80211_frame);
1471 memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1472 wi_read_bap(sc, fid, sizeof(frmhdr),
1473 m->m_data + sizeof(struct ieee80211_frame), len);
1474 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1475 m->m_pkthdr.rcvif = ifp;
1477 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1479 if (sc->sc_drvbpf) {
1480 /* XXX replace divide by table */
1481 sc->sc_rx_th.wr_rate = frmhdr.wi_rx_rate / 5;
1482 sc->sc_rx_th.wr_antsignal = frmhdr.wi_rx_signal;
1483 sc->sc_rx_th.wr_antnoise = frmhdr.wi_rx_silence;
1484 sc->sc_rx_th.wr_flags = 0;
1485 if (frmhdr.wi_status & WI_STAT_PCF)
1486 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_CFP;
1487 bpf_ptap(sc->sc_drvbpf, m, &sc->sc_rx_th, sc->sc_rx_th_len);
1490 wh = mtod(m, struct ieee80211_frame *);
1491 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1493 * WEP is decrypted by hardware. Clear WEP bit
1494 * header for ieee80211_input().
1496 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1499 /* synchronize driver's BSSID with firmware's BSSID */
1500 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1501 if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1502 wi_sync_bssid(sc, wh->i_addr3);
1505 * Locate the node for sender, track state, and
1506 * then pass this node (referenced) up to the 802.11
1507 * layer for its use. We are required to pass
1508 * something so we fallback to ic_bss when this frame
1509 * is from an unknown sender.
1511 if (ic->ic_opmode != IEEE80211_M_STA) {
1512 ni = ieee80211_find_node(ic, wh->i_addr2);
1514 ni = ieee80211_ref_node(ic->ic_bss);
1516 ni = ieee80211_ref_node(ic->ic_bss);
1518 * Send frame up for processing.
1520 ieee80211_input(ifp, m, ni, rssi, rstamp);
1522 * The frame may have caused the node to be marked for
1523 * reclamation (e.g. in response to a DEAUTH message)
1524 * so use free_node here instead of unref_node.
1526 if (ni == ic->ic_bss)
1527 ieee80211_unref_node(&ni);
1529 ieee80211_free_node(ic, ni);
1533 wi_tx_ex_intr(struct wi_softc *sc)
1535 struct ieee80211com *ic = &sc->sc_ic;
1536 struct ifnet *ifp = &ic->ic_if;
1537 struct wi_frame frmhdr;
1540 fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1541 /* Read in the frame header */
1542 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1543 u_int16_t status = le16toh(frmhdr.wi_status);
1546 * Spontaneous station disconnects appear as xmit
1547 * errors. Don't announce them and/or count them
1548 * as an output error.
1550 if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1551 if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1552 if_printf(ifp, "tx failed");
1553 if (status & WI_TXSTAT_RET_ERR)
1554 printf(", retry limit exceeded");
1555 if (status & WI_TXSTAT_AGED_ERR)
1556 printf(", max transmit lifetime exceeded");
1557 if (status & WI_TXSTAT_DISCONNECT)
1558 printf(", port disconnected");
1559 if (status & WI_TXSTAT_FORM_ERR)
1560 printf(", invalid format (data len %u src %6D)",
1561 le16toh(frmhdr.wi_dat_len),
1562 frmhdr.wi_ehdr.ether_shost, ":");
1564 printf(", status=0x%x", status);
1569 DPRINTF(("port disconnected\n"));
1570 ifp->if_collisions++; /* XXX */
1573 DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
1574 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1578 wi_tx_intr(struct wi_softc *sc)
1580 struct ieee80211com *ic = &sc->sc_ic;
1581 struct ifnet *ifp = &ic->ic_if;
1587 fid = CSR_READ_2(sc, WI_ALLOC_FID);
1588 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1591 if (sc->sc_txd[cur].d_fid != fid) {
1592 if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
1593 fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
1596 sc->sc_tx_timer = 0;
1597 sc->sc_txd[cur].d_len = 0;
1598 sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
1599 if (sc->sc_txd[cur].d_len == 0)
1600 ifp->if_flags &= ~IFF_OACTIVE;
1602 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1604 if_printf(ifp, "xmit failed\n");
1605 sc->sc_txd[cur].d_len = 0;
1607 sc->sc_tx_timer = 5;
1614 wi_info_intr(struct wi_softc *sc)
1616 struct ieee80211com *ic = &sc->sc_ic;
1617 struct ifnet *ifp = &ic->ic_if;
1618 int i, fid, len, off;
1623 fid = CSR_READ_2(sc, WI_INFO_FID);
1624 wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
1626 switch (le16toh(ltbuf[1])) {
1628 case WI_INFO_LINK_STAT:
1629 wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1630 DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1631 switch (le16toh(stat)) {
1632 case WI_INFO_LINK_STAT_CONNECTED:
1633 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1634 if (ic->ic_state == IEEE80211_S_RUN &&
1635 ic->ic_opmode != IEEE80211_M_IBSS)
1638 case WI_INFO_LINK_STAT_AP_CHG:
1639 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1641 case WI_INFO_LINK_STAT_AP_INR:
1642 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1644 case WI_INFO_LINK_STAT_AP_OOR:
1645 if (sc->sc_firmware_type == WI_SYMBOL &&
1646 sc->sc_scan_timer > 0) {
1647 if (wi_cmd(sc, WI_CMD_INQUIRE,
1648 WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
1649 sc->sc_scan_timer = 0;
1652 if (ic->ic_opmode == IEEE80211_M_STA)
1653 sc->sc_flags |= WI_FLAGS_OUTRANGE;
1655 case WI_INFO_LINK_STAT_DISCONNECTED:
1656 case WI_INFO_LINK_STAT_ASSOC_FAILED:
1657 if (ic->ic_opmode == IEEE80211_M_STA)
1658 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1663 case WI_INFO_COUNTERS:
1664 /* some card versions have a larger stats structure */
1665 len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1666 ptr = (u_int32_t *)&sc->sc_stats;
1667 off = sizeof(ltbuf);
1668 for (i = 0; i < len; i++, off += 2, ptr++) {
1669 wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1670 #ifdef WI_HERMES_STATS_WAR
1676 ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1677 sc->sc_stats.wi_tx_multi_retries +
1678 sc->sc_stats.wi_tx_retry_limit;
1681 case WI_INFO_SCAN_RESULTS:
1682 case WI_INFO_HOST_SCAN_RESULTS:
1683 wi_scan_result(sc, fid, le16toh(ltbuf[0]));
1687 DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1688 le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1691 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1695 wi_write_multi(struct wi_softc *sc)
1697 struct ifnet *ifp = &sc->sc_ic.ic_if;
1699 struct ifmultiaddr *ifma;
1700 struct wi_mcast mlist;
1702 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1704 memset(&mlist, 0, sizeof(mlist));
1705 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1710 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1711 if (ifma->ifma_addr->sa_family != AF_LINK)
1715 IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1716 (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1719 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1720 IEEE80211_ADDR_LEN * n);
1724 wi_read_nicid(struct wi_softc *sc)
1726 struct wi_card_ident *id;
1731 /* getting chip identity */
1732 memset(ver, 0, sizeof(ver));
1734 wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1735 device_printf(sc->sc_dev, "using ");
1737 sc->sc_firmware_type = WI_NOTYPE;
1738 for (id = wi_card_ident; id->card_name != NULL; id++) {
1739 if (le16toh(ver[0]) == id->card_id) {
1740 printf("%s", id->card_name);
1741 sc->sc_firmware_type = id->firm_type;
1745 if (sc->sc_firmware_type == WI_NOTYPE) {
1746 if (le16toh(ver[0]) & 0x8000) {
1747 printf("Unknown PRISM2 chip");
1748 sc->sc_firmware_type = WI_INTERSIL;
1750 printf("Unknown Lucent chip");
1751 sc->sc_firmware_type = WI_LUCENT;
1755 /* get primary firmware version (Only Prism chips) */
1756 if (sc->sc_firmware_type != WI_LUCENT) {
1757 memset(ver, 0, sizeof(ver));
1759 wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1760 sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1761 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1764 /* get station firmware version */
1765 memset(ver, 0, sizeof(ver));
1767 wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1768 sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1769 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1770 if (sc->sc_firmware_type == WI_INTERSIL &&
1771 (sc->sc_sta_firmware_ver == 10102 ||
1772 sc->sc_sta_firmware_ver == 20102)) {
1774 memset(ident, 0, sizeof(ident));
1775 len = sizeof(ident);
1776 /* value should be the format like "V2.00-11" */
1777 if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1778 *(p = (char *)ident) >= 'A' &&
1779 p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1780 sc->sc_firmware_type = WI_SYMBOL;
1781 sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1782 (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1783 (p[6] - '0') * 10 + (p[7] - '0');
1787 device_printf(sc->sc_dev, "%s Firmware: ",
1788 sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
1789 (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
1790 if (sc->sc_firmware_type != WI_LUCENT) /* XXX */
1791 printf("Primary (%u.%u.%u), ",
1792 sc->sc_pri_firmware_ver / 10000,
1793 (sc->sc_pri_firmware_ver % 10000) / 100,
1794 sc->sc_pri_firmware_ver % 100);
1795 printf("Station (%u.%u.%u)\n",
1796 sc->sc_sta_firmware_ver / 10000,
1797 (sc->sc_sta_firmware_ver % 10000) / 100,
1798 sc->sc_sta_firmware_ver % 100);
1802 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1804 struct wi_ssid ssid;
1806 if (buflen > IEEE80211_NWID_LEN)
1808 memset(&ssid, 0, sizeof(ssid));
1809 ssid.wi_len = htole16(buflen);
1810 memcpy(ssid.wi_ssid, buf, buflen);
1811 return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1815 wi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1817 struct wi_softc *sc = ifp->if_softc;
1818 struct ieee80211com *ic = &sc->sc_ic;
1819 struct ifreq *ifr = (struct ifreq *)data;
1821 struct wi_scan_res *res;
1823 int len, n, error, mif, val, off, i;
1825 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1828 len = (wreq.wi_len - 1) * 2;
1829 if (len < sizeof(u_int16_t))
1831 if (len > sizeof(wreq.wi_val))
1832 len = sizeof(wreq.wi_val);
1834 switch (wreq.wi_type) {
1836 case WI_RID_IFACE_STATS:
1837 memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
1838 if (len < sizeof(sc->sc_stats))
1841 len = sizeof(sc->sc_stats);
1844 case WI_RID_ENCRYPTION:
1845 case WI_RID_TX_CRYPT_KEY:
1846 case WI_RID_DEFLT_CRYPT_KEYS:
1847 case WI_RID_TX_RATE:
1848 return ieee80211_cfgget(ifp, cmd, data, cr);
1850 case WI_RID_MICROWAVE_OVEN:
1851 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
1852 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1856 wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
1857 len = sizeof(u_int16_t);
1860 case WI_RID_DBM_ADJUST:
1861 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
1862 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1866 wreq.wi_val[0] = htole16(sc->sc_dbm_offset);
1867 len = sizeof(u_int16_t);
1870 case WI_RID_ROAMING_MODE:
1871 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
1872 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1876 wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
1877 len = sizeof(u_int16_t);
1880 case WI_RID_SYSTEM_SCALE:
1881 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
1882 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1886 wreq.wi_val[0] = htole16(sc->sc_system_scale);
1887 len = sizeof(u_int16_t);
1890 case WI_RID_FRAG_THRESH:
1891 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
1892 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1896 wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
1897 len = sizeof(u_int16_t);
1900 case WI_RID_READ_APS:
1901 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1902 return ieee80211_cfgget(ifp, cmd, data, cr);
1903 if (sc->sc_scan_timer > 0) {
1904 error = EINPROGRESS;
1908 if (len < sizeof(n)) {
1912 if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
1913 n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
1914 len = sizeof(n) + sizeof(struct wi_apinfo) * n;
1915 memcpy(wreq.wi_val, &n, sizeof(n));
1916 memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
1917 sizeof(struct wi_apinfo) * n);
1921 wreq.wi_val[0] = sc->sc_firmware_type != WI_LUCENT;
1922 len = sizeof(u_int16_t);
1926 mif = wreq.wi_val[0];
1927 error = wi_cmd(sc, WI_CMD_READMIF, mif, 0, 0);
1928 val = CSR_READ_2(sc, WI_RESP0);
1929 wreq.wi_val[0] = val;
1930 len = sizeof(u_int16_t);
1933 case WI_RID_ZERO_CACHE:
1934 case WI_RID_PROCFRAME: /* ignore for compatibility */
1938 case WI_RID_READ_CACHE:
1939 return ieee80211_cfgget(ifp, cmd, data, cr);
1941 case WI_RID_SCAN_RES: /* compatibility interface */
1942 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1943 return ieee80211_cfgget(ifp, cmd, data, cr);
1944 if (sc->sc_scan_timer > 0) {
1945 error = EINPROGRESS;
1949 if (sc->sc_firmware_type == WI_LUCENT) {
1951 reslen = WI_WAVELAN_RES_SIZE;
1953 off = sizeof(struct wi_scan_p2_hdr);
1954 reslen = WI_PRISM2_RES_SIZE;
1956 if (len < off + reslen * n)
1957 n = (len - off) / reslen;
1958 len = off + reslen * n;
1960 struct wi_scan_p2_hdr *p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1962 * Prepend Prism-specific header.
1964 if (len < sizeof(struct wi_scan_p2_hdr)) {
1968 p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1970 p2->wi_reason = n; /* XXX */
1972 for (i = 0; i < n; i++, off += reslen) {
1973 const struct wi_apinfo *ap = &sc->sc_aps[i];
1975 res = (struct wi_scan_res *)((char *)wreq.wi_val + off);
1976 res->wi_chan = ap->channel;
1977 res->wi_noise = ap->noise;
1978 res->wi_signal = ap->signal;
1979 IEEE80211_ADDR_COPY(res->wi_bssid, ap->bssid);
1980 res->wi_interval = ap->interval;
1981 res->wi_capinfo = ap->capinfo;
1982 res->wi_ssid_len = ap->namelen;
1983 memcpy(res->wi_ssid, ap->name,
1984 IEEE80211_NWID_LEN);
1985 if (sc->sc_firmware_type != WI_LUCENT) {
1986 /* XXX not saved from Prism cards */
1987 memset(res->wi_srates, 0,
1988 sizeof(res->wi_srates));
1989 res->wi_rate = ap->rate;
1996 if (sc->sc_enabled) {
1997 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2001 switch (wreq.wi_type) {
2002 case WI_RID_MAX_DATALEN:
2003 wreq.wi_val[0] = htole16(sc->sc_max_datalen);
2004 len = sizeof(u_int16_t);
2006 case WI_RID_RTS_THRESH:
2007 wreq.wi_val[0] = htole16(ic->ic_rtsthreshold);
2008 len = sizeof(u_int16_t);
2010 case WI_RID_CNFAUTHMODE:
2011 wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
2012 len = sizeof(u_int16_t);
2014 case WI_RID_NODENAME:
2015 if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
2019 len = sc->sc_nodelen + sizeof(u_int16_t);
2020 wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
2021 memcpy(&wreq.wi_val[1], sc->sc_nodename,
2025 return ieee80211_cfgget(ifp, cmd, data, cr);
2031 wreq.wi_len = (len + 1) / 2 + 1;
2032 return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
2036 wi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
2038 struct wi_softc *sc = ifp->if_softc;
2039 struct ieee80211com *ic = &sc->sc_ic;
2040 struct ifreq *ifr = (struct ifreq *)data;
2043 int i, len, error, mif, val;
2044 struct ieee80211_rateset *rs;
2046 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2049 len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0;
2050 switch (wreq.wi_type) {
2051 case WI_RID_DBM_ADJUST:
2054 case WI_RID_NODENAME:
2055 if (le16toh(wreq.wi_val[0]) * 2 > len ||
2056 le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
2060 if (sc->sc_enabled) {
2061 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2066 sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
2067 memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen);
2070 case WI_RID_MICROWAVE_OVEN:
2071 case WI_RID_ROAMING_MODE:
2072 case WI_RID_SYSTEM_SCALE:
2073 case WI_RID_FRAG_THRESH:
2074 if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
2075 (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
2077 if (wreq.wi_type == WI_RID_ROAMING_MODE &&
2078 (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
2080 if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
2081 (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
2083 if (wreq.wi_type == WI_RID_FRAG_THRESH &&
2084 (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
2087 case WI_RID_RTS_THRESH:
2088 case WI_RID_CNFAUTHMODE:
2089 case WI_RID_MAX_DATALEN:
2090 if (sc->sc_enabled) {
2091 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2096 switch (wreq.wi_type) {
2097 case WI_RID_FRAG_THRESH:
2098 ic->ic_fragthreshold = le16toh(wreq.wi_val[0]);
2100 case WI_RID_RTS_THRESH:
2101 ic->ic_rtsthreshold = le16toh(wreq.wi_val[0]);
2103 case WI_RID_MICROWAVE_OVEN:
2104 sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
2106 case WI_RID_ROAMING_MODE:
2107 sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
2109 case WI_RID_SYSTEM_SCALE:
2110 sc->sc_system_scale = le16toh(wreq.wi_val[0]);
2112 case WI_RID_CNFAUTHMODE:
2113 sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
2115 case WI_RID_MAX_DATALEN:
2116 sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
2121 case WI_RID_TX_RATE:
2122 switch (le16toh(wreq.wi_val[0])) {
2124 ic->ic_fixed_rate = -1;
2127 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
2128 for (i = 0; i < rs->rs_nrates; i++) {
2129 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL)
2130 / 2 == le16toh(wreq.wi_val[0]))
2133 if (i == rs->rs_nrates)
2135 ic->ic_fixed_rate = i;
2138 error = wi_write_txrate(sc);
2141 case WI_RID_SCAN_APS:
2142 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2143 error = wi_scan_ap(sc, 0x3fff, 0x000f);
2146 case WI_RID_SCAN_REQ: /* compatibility interface */
2147 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2148 error = wi_scan_ap(sc, wreq.wi_val[0], wreq.wi_val[1]);
2151 case WI_RID_MGMT_XMIT:
2152 if (!sc->sc_enabled) {
2156 if (ic->ic_mgtq.ifq_len > 5) {
2160 /* XXX wi_len looks in u_int8_t, not in u_int16_t */
2161 m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL);
2166 IF_ENQUEUE(&ic->ic_mgtq, m);
2170 mif = wreq.wi_val[0];
2171 val = wreq.wi_val[1];
2172 error = wi_cmd(sc, WI_CMD_WRITEMIF, mif, val, 0);
2175 case WI_RID_PROCFRAME: /* ignore for compatibility */
2178 case WI_RID_OWN_SSID:
2179 if (le16toh(wreq.wi_val[0]) * 2 > len ||
2180 le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) {
2184 memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
2185 ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2;
2186 memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen);
2191 if (sc->sc_enabled) {
2192 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2197 error = ieee80211_cfgset(ifp, cmd, data);
2204 wi_write_txrate(struct wi_softc *sc)
2206 struct ieee80211com *ic = &sc->sc_ic;
2210 if (ic->ic_fixed_rate < 0)
2211 rate = 0; /* auto */
2213 rate = (ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ic->ic_fixed_rate] &
2214 IEEE80211_RATE_VAL) / 2;
2216 /* rate: 0, 1, 2, 5, 11 */
2218 switch (sc->sc_firmware_type) {
2221 case 0: /* auto == 11mbps auto */
2224 /* case 1, 2 map to 1, 2*/
2225 case 5: /* 5.5Mbps -> 4 */
2228 case 11: /* 11mbps -> 5 */
2236 /* Choose a bit according to this table.
2239 * ----+-------------------
2245 for (i = 8; i > 0; i >>= 1) {
2250 rate = 0xf; /* auto */
2255 return wi_write_val(sc, WI_RID_TX_RATE, rate);
2259 wi_write_wep(struct wi_softc *sc)
2261 struct ieee80211com *ic = &sc->sc_ic;
2265 struct wi_key wkey[IEEE80211_WEP_NKID];
2267 switch (sc->sc_firmware_type) {
2269 val = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0;
2270 error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
2273 error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_wep_txkey);
2276 memset(wkey, 0, sizeof(wkey));
2277 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2278 keylen = ic->ic_nw_keys[i].wk_len;
2279 wkey[i].wi_keylen = htole16(keylen);
2280 memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
2283 error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
2284 wkey, sizeof(wkey));
2289 if (ic->ic_flags & IEEE80211_F_WEPON) {
2291 * ONLY HWB3163 EVAL-CARD Firmware version
2292 * less than 0.8 variant2
2294 * If promiscuous mode disable, Prism2 chip
2295 * does not work with WEP .
2296 * It is under investigation for details.
2297 * (ichiro@netbsd.org)
2299 if (sc->sc_firmware_type == WI_INTERSIL &&
2300 sc->sc_sta_firmware_ver < 802 ) {
2301 /* firm ver < 0.8 variant 2 */
2302 wi_write_val(sc, WI_RID_PROMISC, 1);
2304 wi_write_val(sc, WI_RID_CNFAUTHMODE,
2305 sc->sc_cnfauthmode);
2306 val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
2308 * Encryption firmware has a bug for HostAP mode.
2310 if (sc->sc_firmware_type == WI_INTERSIL &&
2311 ic->ic_opmode == IEEE80211_M_HOSTAP)
2312 val |= HOST_ENCRYPT;
2314 wi_write_val(sc, WI_RID_CNFAUTHMODE,
2315 IEEE80211_AUTH_OPEN);
2316 val = HOST_ENCRYPT | HOST_DECRYPT;
2318 error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
2321 error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
2326 * It seems that the firmware accept 104bit key only if
2327 * all the keys have 104bit length. We get the length of
2328 * the transmit key and use it for all other keys.
2329 * Perhaps we should use software WEP for such situation.
2331 keylen = ic->ic_nw_keys[ic->ic_wep_txkey].wk_len;
2332 if (keylen > IEEE80211_WEP_KEYLEN)
2333 keylen = 13; /* 104bit keys */
2335 keylen = IEEE80211_WEP_KEYLEN;
2336 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2337 error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
2338 ic->ic_nw_keys[i].wk_key, keylen);
2348 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
2351 static volatile int count = 0;
2357 panic("Hey partner, hold on there!");
2360 /* wait for the busy bit to clear */
2361 for (i = sc->wi_cmd_count; i > 0; i--) { /* 500ms */
2362 if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
2364 DELAY(1*1000); /* 1ms */
2367 device_printf(sc->sc_dev, "wi_cmd: busy bit won't clear.\n" );
2373 CSR_WRITE_2(sc, WI_PARAM0, val0);
2374 CSR_WRITE_2(sc, WI_PARAM1, val1);
2375 CSR_WRITE_2(sc, WI_PARAM2, val2);
2376 CSR_WRITE_2(sc, WI_COMMAND, cmd);
2378 if (cmd == WI_CMD_INI) {
2379 /* XXX: should sleep here. */
2380 DELAY(100*1000); /* 100ms delay for init */
2382 for (i = 0; i < WI_TIMEOUT; i++) {
2384 * Wait for 'command complete' bit to be
2385 * set in the event status register.
2387 s = CSR_READ_2(sc, WI_EVENT_STAT);
2388 if (s & WI_EV_CMD) {
2389 /* Ack the event and read result code. */
2390 s = CSR_READ_2(sc, WI_STATUS);
2391 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
2392 if (s & WI_STAT_CMD_RESULT) {
2402 if (i == WI_TIMEOUT) {
2403 device_printf(sc->sc_dev,
2404 "timeout in wi_cmd 0x%04x; event status 0x%04x\n", cmd, s);
2413 wi_seek_bap(struct wi_softc *sc, int id, int off)
2417 CSR_WRITE_2(sc, WI_SEL0, id);
2418 CSR_WRITE_2(sc, WI_OFF0, off);
2420 for (i = 0; ; i++) {
2421 status = CSR_READ_2(sc, WI_OFF0);
2422 if ((status & WI_OFF_BUSY) == 0)
2424 if (i == WI_TIMEOUT) {
2425 device_printf(sc->sc_dev, "timeout in wi_seek to %x/%x\n",
2427 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2428 if (status == 0xffff)
2434 if (status & WI_OFF_ERR) {
2435 device_printf(sc->sc_dev, "failed in wi_seek to %x/%x\n", id, off);
2436 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2440 sc->sc_bap_off = off;
2445 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
2452 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2453 if ((error = wi_seek_bap(sc, id, off)) != 0)
2456 cnt = (buflen + 1) / 2;
2457 ptr = (u_int16_t *)buf;
2458 for (i = 0; i < cnt; i++)
2459 *ptr++ = CSR_READ_2(sc, WI_DATA0);
2460 sc->sc_bap_off += cnt * 2;
2465 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
2473 #ifdef WI_HERMES_AUTOINC_WAR
2476 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2477 if ((error = wi_seek_bap(sc, id, off)) != 0)
2480 cnt = (buflen + 1) / 2;
2481 ptr = (u_int16_t *)buf;
2482 for (i = 0; i < cnt; i++)
2483 CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
2484 sc->sc_bap_off += cnt * 2;
2486 #ifdef WI_HERMES_AUTOINC_WAR
2488 * According to the comments in the HCF Light code, there is a bug
2489 * in the Hermes (or possibly in certain Hermes firmware revisions)
2490 * where the chip's internal autoincrement counter gets thrown off
2491 * during data writes: the autoincrement is missed, causing one
2492 * data word to be overwritten and subsequent words to be written to
2493 * the wrong memory locations. The end result is that we could end
2494 * up transmitting bogus frames without realizing it. The workaround
2495 * for this is to write a couple of extra guard words after the end
2496 * of the transfer, then attempt to read then back. If we fail to
2497 * locate the guard words where we expect them, we preform the
2498 * transfer over again.
2500 if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
2501 CSR_WRITE_2(sc, WI_DATA0, 0x1234);
2502 CSR_WRITE_2(sc, WI_DATA0, 0x5678);
2503 wi_seek_bap(sc, id, sc->sc_bap_off);
2504 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2505 if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
2506 CSR_READ_2(sc, WI_DATA0) != 0x5678) {
2507 device_printf(sc->sc_dev,
2508 "detect auto increment bug, try again\n");
2517 wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
2522 for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
2526 len = min(m->m_len, totlen);
2528 if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
2529 m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
2530 return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
2534 if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
2544 wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
2548 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
2549 device_printf(sc->sc_dev, "failed to allocate %d bytes on NIC\n",
2554 for (i = 0; i < WI_TIMEOUT; i++) {
2555 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
2557 if (i == WI_TIMEOUT) {
2558 device_printf(sc->sc_dev, "timeout in alloc\n");
2563 *idp = CSR_READ_2(sc, WI_ALLOC_FID);
2564 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
2569 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
2574 /* Tell the NIC to enter record read mode. */
2575 error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2579 error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2583 if (le16toh(ltbuf[1]) != rid) {
2584 device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n",
2585 rid, le16toh(ltbuf[1]));
2588 len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */
2589 if (*buflenp < len) {
2590 device_printf(sc->sc_dev, "record buffer is too small, "
2591 "rid=%x, size=%d, len=%d\n",
2592 rid, *buflenp, len);
2596 return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2600 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2605 ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */
2606 ltbuf[1] = htole16(rid);
2608 error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2611 error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2615 return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
2619 wi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
2621 struct ifnet *ifp = &ic->ic_if;
2622 struct wi_softc *sc = ifp->if_softc;
2623 struct ieee80211_node *ni = ic->ic_bss;
2626 struct wi_ssid ssid;
2627 u_int8_t old_bssid[IEEE80211_ADDR_LEN];
2629 DPRINTF(("%s: %s -> %s\n", __func__,
2630 ieee80211_state_name[ic->ic_state],
2631 ieee80211_state_name[nstate]));
2634 case IEEE80211_S_INIT:
2635 ic->ic_flags &= ~IEEE80211_F_SIBSS;
2636 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2637 return (*sc->sc_newstate)(ic, nstate, arg);
2639 case IEEE80211_S_RUN:
2640 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2641 buflen = IEEE80211_ADDR_LEN;
2642 wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen);
2643 IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
2644 buflen = sizeof(val);
2645 wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
2646 /* XXX validate channel */
2647 ni->ni_chan = &ic->ic_channels[le16toh(val)];
2648 sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
2649 htole16(ni->ni_chan->ic_freq);
2650 sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
2651 htole16(ni->ni_chan->ic_flags);
2653 if (IEEE80211_ADDR_EQ(old_bssid, ni->ni_bssid))
2654 sc->sc_false_syns++;
2656 sc->sc_false_syns = 0;
2658 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2659 ni->ni_esslen = ic->ic_des_esslen;
2660 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
2661 ni->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B];
2662 ni->ni_intval = ic->ic_lintval;
2663 ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
2664 if (ic->ic_flags & IEEE80211_F_WEPON)
2665 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
2667 /* XXX check return value */
2668 buflen = sizeof(ssid);
2669 wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
2670 ni->ni_esslen = le16toh(ssid.wi_len);
2671 if (ni->ni_esslen > IEEE80211_NWID_LEN)
2672 ni->ni_esslen = IEEE80211_NWID_LEN; /*XXX*/
2673 memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
2677 case IEEE80211_S_SCAN:
2678 case IEEE80211_S_AUTH:
2679 case IEEE80211_S_ASSOC:
2683 ic->ic_state = nstate; /* NB: skip normal ieee80211 handling */
2688 wi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate)
2693 if (!sc->sc_enabled)
2695 switch (sc->sc_firmware_type) {
2697 (void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
2700 val[0] = chanmask; /* channel */
2701 val[1] = txrate; /* tx rate */
2702 error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
2706 * XXX only supported on 3.x ?
2708 val[0] = BSCAN_BCAST | BSCAN_ONETIME;
2709 error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
2710 val, sizeof(val[0]));
2714 sc->sc_scan_timer = WI_SCAN_WAIT;
2715 sc->sc_ic.ic_if.if_timer = 1;
2716 DPRINTF(("wi_scan_ap: start scanning, "
2717 "chamask 0x%x txrate 0x%x\n", chanmask, txrate));
2723 wi_scan_result(struct wi_softc *sc, int fid, int cnt)
2725 #define N(a) (sizeof (a) / sizeof (a[0]))
2726 int i, naps, off, szbuf;
2727 struct wi_scan_header ws_hdr; /* Prism2 header */
2728 struct wi_scan_data_p2 ws_dat; /* Prism2 scantable*/
2729 struct wi_apinfo *ap;
2731 off = sizeof(u_int16_t) * 2;
2732 memset(&ws_hdr, 0, sizeof(ws_hdr));
2733 switch (sc->sc_firmware_type) {
2735 wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
2736 off += sizeof(ws_hdr);
2737 szbuf = sizeof(struct wi_scan_data_p2);
2740 szbuf = sizeof(struct wi_scan_data_p2) + 6;
2743 szbuf = sizeof(struct wi_scan_data);
2746 device_printf(sc->sc_dev,
2747 "wi_scan_result: unknown firmware type %u\n",
2748 sc->sc_firmware_type);
2752 naps = (cnt * 2 + 2 - off) / szbuf;
2753 if (naps > N(sc->sc_aps))
2754 naps = N(sc->sc_aps);
2758 memset(&ws_dat, 0, sizeof(ws_dat));
2759 for (i = 0; i < naps; i++, ap++) {
2760 wi_read_bap(sc, fid, off, &ws_dat,
2761 (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
2762 DPRINTF2(("wi_scan_result: #%d: off %d bssid %6D\n", i, off,
2763 ws_dat.wi_bssid, ":"));
2765 ap->scanreason = le16toh(ws_hdr.wi_reason);
2766 memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
2767 ap->channel = le16toh(ws_dat.wi_chid);
2768 ap->signal = le16toh(ws_dat.wi_signal);
2769 ap->noise = le16toh(ws_dat.wi_noise);
2770 ap->quality = ap->signal - ap->noise;
2771 ap->capinfo = le16toh(ws_dat.wi_capinfo);
2772 ap->interval = le16toh(ws_dat.wi_interval);
2773 ap->rate = le16toh(ws_dat.wi_rate);
2774 ap->namelen = le16toh(ws_dat.wi_namelen);
2775 if (ap->namelen > sizeof(ap->name))
2776 ap->namelen = sizeof(ap->name);
2777 memcpy(ap->name, ws_dat.wi_name, ap->namelen);
2781 sc->sc_scan_timer = 0;
2782 DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
2787 wi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi)
2789 ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr),
2790 ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL : -1, rssi);
2791 printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n",
2792 le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1),
2793 le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence);
2794 printf(" rx_signal %u rx_rate %u rx_flow %u\n",
2795 wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow);
2796 printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n",
2797 wh->wi_tx_rtry, wh->wi_tx_rate,
2798 le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len));
2799 printf(" ehdr dst %6D src %6D type 0x%x\n",
2800 wh->wi_ehdr.ether_dhost, ":", wh->wi_ehdr.ether_shost, ":",
2801 wh->wi_ehdr.ether_type);
2805 wi_alloc(device_t dev, int rid)
2807 struct wi_softc *sc = device_get_softc(dev);
2809 if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2810 sc->iobase_rid = rid;
2811 sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2812 &sc->iobase_rid, 0, ~0, (1 << 6),
2813 rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2815 device_printf(dev, "No I/O space?!\n");
2819 sc->wi_io_addr = rman_get_start(sc->iobase);
2820 sc->wi_btag = rman_get_bustag(sc->iobase);
2821 sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2824 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2825 &sc->mem_rid, RF_ACTIVE);
2828 device_printf(dev, "No Mem space on prism2.5?\n");
2832 sc->wi_btag = rman_get_bustag(sc->mem);
2833 sc->wi_bhandle = rman_get_bushandle(sc->mem);
2838 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2840 ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2844 device_printf(dev, "No irq?!\n");
2849 sc->sc_unit = device_get_unit(dev);
2855 wi_free(device_t dev)
2857 struct wi_softc *sc = device_get_softc(dev);
2859 if (sc->iobase != NULL) {
2860 bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2863 if (sc->irq != NULL) {
2864 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2867 if (sc->mem != NULL) {
2868 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
2876 wi_get_debug(struct wi_softc *sc, struct wi_req *wreq)
2882 switch (wreq->wi_type) {
2883 case WI_DEBUG_SLEEP:
2885 wreq->wi_val[0] = sc->wi_debug.wi_sleep;
2887 case WI_DEBUG_DELAYSUPP:
2889 wreq->wi_val[0] = sc->wi_debug.wi_delaysupp;
2891 case WI_DEBUG_TXSUPP:
2893 wreq->wi_val[0] = sc->wi_debug.wi_txsupp;
2895 case WI_DEBUG_MONITOR:
2897 wreq->wi_val[0] = sc->wi_debug.wi_monitor;
2899 case WI_DEBUG_LEDTEST:
2901 wreq->wi_val[0] = sc->wi_debug.wi_ledtest;
2902 wreq->wi_val[1] = sc->wi_debug.wi_ledtest_param0;
2903 wreq->wi_val[2] = sc->wi_debug.wi_ledtest_param1;
2905 case WI_DEBUG_CONTTX:
2907 wreq->wi_val[0] = sc->wi_debug.wi_conttx;
2908 wreq->wi_val[1] = sc->wi_debug.wi_conttx_param0;
2910 case WI_DEBUG_CONTRX:
2912 wreq->wi_val[0] = sc->wi_debug.wi_contrx;
2914 case WI_DEBUG_SIGSTATE:
2916 wreq->wi_val[0] = sc->wi_debug.wi_sigstate;
2917 wreq->wi_val[1] = sc->wi_debug.wi_sigstate_param0;
2919 case WI_DEBUG_CONFBITS:
2921 wreq->wi_val[0] = sc->wi_debug.wi_confbits;
2922 wreq->wi_val[1] = sc->wi_debug.wi_confbits_param0;
2933 wi_set_debug(struct wi_softc *sc, struct wi_req *wreq)
2936 u_int16_t cmd, param0 = 0, param1 = 0;
2938 switch (wreq->wi_type) {
2939 case WI_DEBUG_RESET:
2941 case WI_DEBUG_CALENABLE:
2943 case WI_DEBUG_SLEEP:
2944 sc->wi_debug.wi_sleep = 1;
2947 sc->wi_debug.wi_sleep = 0;
2950 param0 = wreq->wi_val[0];
2952 case WI_DEBUG_DELAYSUPP:
2953 sc->wi_debug.wi_delaysupp = 1;
2955 case WI_DEBUG_TXSUPP:
2956 sc->wi_debug.wi_txsupp = 1;
2958 case WI_DEBUG_MONITOR:
2959 sc->wi_debug.wi_monitor = 1;
2961 case WI_DEBUG_LEDTEST:
2962 param0 = wreq->wi_val[0];
2963 param1 = wreq->wi_val[1];
2964 sc->wi_debug.wi_ledtest = 1;
2965 sc->wi_debug.wi_ledtest_param0 = param0;
2966 sc->wi_debug.wi_ledtest_param1 = param1;
2968 case WI_DEBUG_CONTTX:
2969 param0 = wreq->wi_val[0];
2970 sc->wi_debug.wi_conttx = 1;
2971 sc->wi_debug.wi_conttx_param0 = param0;
2973 case WI_DEBUG_STOPTEST:
2974 sc->wi_debug.wi_delaysupp = 0;
2975 sc->wi_debug.wi_txsupp = 0;
2976 sc->wi_debug.wi_monitor = 0;
2977 sc->wi_debug.wi_ledtest = 0;
2978 sc->wi_debug.wi_ledtest_param0 = 0;
2979 sc->wi_debug.wi_ledtest_param1 = 0;
2980 sc->wi_debug.wi_conttx = 0;
2981 sc->wi_debug.wi_conttx_param0 = 0;
2982 sc->wi_debug.wi_contrx = 0;
2983 sc->wi_debug.wi_sigstate = 0;
2984 sc->wi_debug.wi_sigstate_param0 = 0;
2986 case WI_DEBUG_CONTRX:
2987 sc->wi_debug.wi_contrx = 1;
2989 case WI_DEBUG_SIGSTATE:
2990 param0 = wreq->wi_val[0];
2991 sc->wi_debug.wi_sigstate = 1;
2992 sc->wi_debug.wi_sigstate_param0 = param0;
2994 case WI_DEBUG_CONFBITS:
2995 param0 = wreq->wi_val[0];
2996 param1 = wreq->wi_val[1];
2997 sc->wi_debug.wi_confbits = param0;
2998 sc->wi_debug.wi_confbits_param0 = param1;
3008 cmd = WI_CMD_DEBUG | (wreq->wi_type << 8);
3009 error = wi_cmd(sc, cmd, param0, param1, 0);
3015 * Special routines to download firmware for Symbol CF card.
3016 * XXX: This should be modified generic into any PRISM-2 based card.
3019 #define WI_SBCF_PDIADDR 0x3100
3021 /* unaligned load little endian */
3022 #define GETLE32(p) ((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24))
3023 #define GETLE16(p) ((p)[0] | ((p)[1]<<8))
3026 wi_symbol_load_firm(struct wi_softc *sc, const void *primsym, int primlen,
3027 const void *secsym, int seclen)
3032 /* load primary code and run it */
3033 wi_symbol_set_hcr(sc, WI_HCR_EEHOLD);
3034 if (wi_symbol_write_firm(sc, primsym, primlen, NULL, 0))
3036 wi_symbol_set_hcr(sc, WI_HCR_RUN);
3037 for (i = 0; ; i++) {
3040 tsleep(sc, 0, "wiinit", 1);
3041 if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT)
3043 /* write the magic key value to unlock aux port */
3044 CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0);
3045 CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1);
3046 CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2);
3047 CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL);
3050 /* issue read EEPROM command: XXX copied from wi_cmd() */
3051 CSR_WRITE_2(sc, WI_PARAM0, 0);
3052 CSR_WRITE_2(sc, WI_PARAM1, 0);
3053 CSR_WRITE_2(sc, WI_PARAM2, 0);
3054 CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE);
3055 for (i = 0; i < WI_TIMEOUT; i++) {
3056 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
3060 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
3062 CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ);
3063 CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ);
3064 CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA,
3065 (uint16_t *)ebuf, sizeof(ebuf) / 2);
3066 if (GETLE16(ebuf) > sizeof(ebuf))
3068 if (wi_symbol_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf)))
3074 wi_symbol_write_firm(struct wi_softc *sc, const void *buf, int buflen,
3075 const void *ebuf, int ebuflen)
3077 const uint8_t *p, *ep, *q, *eq;
3079 uint32_t addr, id, eid;
3080 int i, len, elen, nblk, pdrlen;
3083 * Parse the header of the firmware image.
3087 while (p < ep && *p++ != ' '); /* FILE: */
3088 while (p < ep && *p++ != ' '); /* filename */
3089 while (p < ep && *p++ != ' '); /* type of the firmware */
3090 nblk = strtoul(p, &tp, 10);
3092 pdrlen = strtoul(p + 1, &tp, 10);
3094 while (p < ep && *p++ != 0x1a); /* skip rest of header */
3097 * Block records: address[4], length[2], data[length];
3099 for (i = 0; i < nblk; i++) {
3100 addr = GETLE32(p); p += 4;
3101 len = GETLE16(p); p += 2;
3102 CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3103 CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3104 CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3105 (const uint16_t *)p, len / 2);
3110 * PDR: id[4], address[4], length[4];
3112 for (i = 0; i < pdrlen; ) {
3113 id = GETLE32(p); p += 4; i += 4;
3114 addr = GETLE32(p); p += 4; i += 4;
3115 len = GETLE32(p); p += 4; i += 4;
3116 /* replace PDR entry with the values from EEPROM, if any */
3117 for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) {
3118 elen = GETLE16(q); q += 2;
3119 eid = GETLE16(q); q += 2;
3120 elen--; /* elen includes eid */
3125 CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3126 CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3127 CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3128 (const uint16_t *)q, len / 2);
3136 wi_symbol_set_hcr(struct wi_softc *sc, int mode)
3140 CSR_WRITE_2(sc, WI_COR, WI_COR_RESET);
3141 tsleep(sc, 0, "wiinit", 1);
3142 hcr = CSR_READ_2(sc, WI_HCR);
3143 hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE);
3144 CSR_WRITE_2(sc, WI_HCR, hcr);
3145 tsleep(sc, 0, "wiinit", 1);
3146 CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE);
3147 tsleep(sc, 0, "wiinit", 1);