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.26 2005/06/11 04:26:53 hsu 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>
83 #include <sys/thread2.h>
85 #include <machine/bus.h>
86 #include <machine/resource.h>
87 #include <machine/clock.h>
88 #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_newstate(struct ieee80211com *, enum ieee80211_state, int);
146 static int wi_scan_ap(struct wi_softc *, u_int16_t, u_int16_t);
147 static void wi_scan_result(struct wi_softc *, int, int);
149 static void wi_dump_pkt(struct wi_frame *, struct ieee80211_node *, int rssi);
151 static int wi_get_debug(struct wi_softc *, struct wi_req *);
152 static int wi_set_debug(struct wi_softc *, struct wi_req *);
154 /* support to download firmware for symbol CF card */
155 static int wi_symbol_write_firm(struct wi_softc *, const void *, int,
157 static int wi_symbol_set_hcr(struct wi_softc *, int);
158 #ifdef DEVICE_POLLING
159 static void wi_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
163 wi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
167 return wi_write_rid(sc, rid, &val, sizeof(val));
170 SYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters");
172 static struct timeval lasttxerror; /* time of last tx error msg */
173 static int curtxeps; /* current tx error msgs/sec */
174 static int wi_txerate = 0; /* tx error rate: max msgs/sec */
175 SYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate,
176 0, "max tx error msgs/sec; 0 to disable msgs");
180 static int wi_debug = 0;
181 SYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug,
182 0, "control debugging printfs");
184 #define DPRINTF(X) if (wi_debug) printf X
185 #define DPRINTF2(X) if (wi_debug > 1) printf X
186 #define IFF_DUMPPKTS(_ifp) \
187 (((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
191 #define IFF_DUMPPKTS(_ifp) 0
194 #define WI_INTRS (WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
196 struct wi_card_ident wi_card_ident[] = {
197 /* CARD_ID CARD_NAME FIRM_TYPE */
198 { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT },
199 { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT },
200 { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT },
201 { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL },
202 { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL },
203 { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL },
204 { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL },
205 { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL },
206 { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL },
207 { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL },
208 { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL },
209 { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL },
210 { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
211 { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
212 { WI_NIC_3842_PCMCIA_ATL_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
213 { WI_NIC_3842_PCMCIA_ATS_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
214 { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
215 { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
216 { WI_NIC_3842_MINI_ATL_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
217 { WI_NIC_3842_MINI_ATS_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
218 { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
219 { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
220 { WI_NIC_3842_PCI_ATS_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
221 { WI_NIC_3842_PCI_ATL_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
222 { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
223 { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
224 { WI_NIC_P3_PCMCIA_ATL_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
225 { WI_NIC_P3_PCMCIA_ATS_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
226 { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
227 { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
228 { WI_NIC_P3_MINI_ATL_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
229 { WI_NIC_P3_MINI_ATS_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
233 devclass_t wi_devclass;
236 wi_attach(device_t dev)
238 struct wi_softc *sc = device_get_softc(dev);
239 struct ieee80211com *ic = &sc->sc_ic;
240 struct ifnet *ifp = &ic->ic_if;
241 int i, nrates, buflen;
243 u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
244 struct ieee80211_rateset *rs;
245 static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
246 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
251 * NB: no locking is needed here; don't put it here
252 * unless you can prove it!
254 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
255 wi_intr, sc, &sc->wi_intrhand, NULL);
258 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
263 sc->wi_cmd_count = 500;
265 if (wi_reset(sc) != 0)
266 return ENXIO; /* XXX */
269 * Read the station address.
270 * And do it twice. I've seen PRISM-based cards that return
271 * an error when trying to read it the first time, which causes
274 buflen = IEEE80211_ADDR_LEN;
275 error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
277 buflen = IEEE80211_ADDR_LEN;
278 error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
280 if (error || IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) {
282 device_printf(dev, "mac read failed %d\n", error);
284 device_printf(dev, "mac read failed (all zeros)\n");
289 /* Read NIC identification */
293 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
294 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
295 ifp->if_ioctl = wi_ioctl;
296 ifp->if_start = wi_start;
297 ifp->if_watchdog = wi_watchdog;
298 ifp->if_init = wi_init;
299 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
300 ifq_set_ready(&ifp->if_snd);
301 #ifdef DEVICE_POLLING
302 ifp->if_poll = wi_poll;
304 ifp->if_capenable = ifp->if_capabilities;
306 ic->ic_phytype = IEEE80211_T_DS;
307 ic->ic_opmode = IEEE80211_M_STA;
308 ic->ic_caps = IEEE80211_C_PMGT | IEEE80211_C_AHDEMO;
309 ic->ic_state = IEEE80211_S_INIT;
312 * Query the card for available channels and setup the
313 * channel table. We assume these are all 11b channels.
315 buflen = sizeof(val);
316 if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
317 val = htole16(0x1fff); /* assume 1-11 */
318 KASSERT(val != 0, ("wi_attach: no available channels listed!"));
320 val <<= 1; /* shift for base 1 indices */
321 for (i = 1; i < 16; i++) {
322 if (isset((u_int8_t*)&val, i)) {
323 ic->ic_channels[i].ic_freq =
324 ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
325 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
330 * Read the default channel from the NIC. This may vary
331 * depending on the country where the NIC was purchased, so
332 * we can't hard-code a default and expect it to work for
335 * If no channel is specified, let the 802.11 code select.
337 buflen = sizeof(val);
338 if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0) {
340 KASSERT(val < IEEE80211_CHAN_MAX &&
341 ic->ic_channels[val].ic_flags != 0,
342 ("wi_attach: invalid own channel %u!", val));
343 ic->ic_ibss_chan = &ic->ic_channels[val];
346 "WI_RID_OWN_CHNL failed, using first channel!\n");
347 ic->ic_ibss_chan = &ic->ic_channels[0];
351 * Set flags based on firmware version.
353 switch (sc->sc_firmware_type) {
356 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
357 #ifdef WI_HERMES_AUTOINC_WAR
358 /* XXX: not confirmed, but never seen for recent firmware */
359 if (sc->sc_sta_firmware_ver < 40000) {
360 sc->sc_flags |= WI_FLAGS_BUG_AUTOINC;
363 if (sc->sc_sta_firmware_ver >= 60000)
364 sc->sc_flags |= WI_FLAGS_HAS_MOR;
365 if (sc->sc_sta_firmware_ver >= 60006) {
366 ic->ic_caps |= IEEE80211_C_IBSS;
367 ic->ic_caps |= IEEE80211_C_MONITOR;
369 sc->sc_ibss_port = htole16(1);
371 sc->sc_min_rssi = WI_LUCENT_MIN_RSSI;
372 sc->sc_max_rssi = WI_LUCENT_MAX_RSSI;
373 sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
377 sc->sc_ntxbuf = WI_NTXBUF;
378 sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR;
379 sc->sc_flags |= WI_FLAGS_HAS_ROAMING;
380 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
382 * Old firmware are slow, so give peace a chance.
384 if (sc->sc_sta_firmware_ver < 10000)
385 sc->wi_cmd_count = 5000;
386 if (sc->sc_sta_firmware_ver > 10101)
387 sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
388 if (sc->sc_sta_firmware_ver >= 800) {
389 ic->ic_caps |= IEEE80211_C_IBSS;
390 ic->ic_caps |= IEEE80211_C_MONITOR;
393 * version 0.8.3 and newer are the only ones that are known
394 * to currently work. Earlier versions can be made to work,
395 * at least according to the Linux driver.
397 if (sc->sc_sta_firmware_ver >= 803)
398 ic->ic_caps |= IEEE80211_C_HOSTAP;
399 sc->sc_ibss_port = htole16(0);
401 sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
402 sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
403 sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
408 sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY;
409 if (sc->sc_sta_firmware_ver >= 25000)
410 ic->ic_caps |= IEEE80211_C_IBSS;
411 sc->sc_ibss_port = htole16(4);
413 sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
414 sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
415 sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
420 * Find out if we support WEP on this card.
422 buflen = sizeof(val);
423 if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
425 ic->ic_caps |= IEEE80211_C_WEP;
427 /* Find supported rates. */
428 buflen = sizeof(ratebuf);
429 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
430 if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
431 nrates = le16toh(*(u_int16_t *)ratebuf);
432 if (nrates > IEEE80211_RATE_MAXSIZE)
433 nrates = IEEE80211_RATE_MAXSIZE;
435 for (i = 0; i < nrates; i++)
437 rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i];
439 /* XXX fallback on error? */
443 buflen = sizeof(val);
444 if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
445 wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
446 sc->sc_dbm_offset = le16toh(val);
449 sc->sc_max_datalen = 2304;
450 sc->sc_system_scale = 1;
451 sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN;
452 sc->sc_roaming_mode = 1;
454 sc->sc_portnum = WI_DEFAULT_PORT;
455 sc->sc_authtype = WI_DEFAULT_AUTHTYPE;
457 bzero(sc->sc_nodename, sizeof(sc->sc_nodename));
458 sc->sc_nodelen = sizeof(WI_DEFAULT_NODENAME) - 1;
459 bcopy(WI_DEFAULT_NODENAME, sc->sc_nodename, sc->sc_nodelen);
461 bzero(sc->sc_net_name, sizeof(sc->sc_net_name));
462 bcopy(WI_DEFAULT_NETNAME, sc->sc_net_name,
463 sizeof(WI_DEFAULT_NETNAME) - 1);
466 * Call MI attach routine.
468 ieee80211_ifattach(ifp);
469 /* override state transition method */
470 sc->sc_newstate = ic->ic_newstate;
471 ic->ic_newstate = wi_newstate;
472 ieee80211_media_init(ifp, wi_media_change, wi_media_status);
474 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
475 sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
478 * Initialize constant fields.
479 * XXX make header lengths a multiple of 32-bits so subsequent
480 * headers are properly aligned; this is a kludge to keep
481 * certain applications happy.
483 * NB: the channel is setup each time we transition to the
484 * RUN state to avoid filling it in for each frame.
486 sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t));
487 sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len);
488 sc->sc_tx_th.wt_ihdr.it_present = htole32(WI_TX_RADIOTAP_PRESENT);
490 sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t));
491 sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len);
492 sc->sc_rx_th.wr_ihdr.it_present = htole32(WI_RX_RADIOTAP_PRESENT);
498 wi_detach(device_t dev)
500 struct wi_softc *sc = device_get_softc(dev);
501 struct ifnet *ifp = &sc->sc_ic.ic_if;
506 /* check if device was removed */
507 sc->wi_gone |= !bus_child_present(dev);
511 ieee80211_ifdetach(ifp);
512 bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
519 wi_shutdown(device_t dev)
521 struct wi_softc *sc = device_get_softc(dev);
523 wi_stop(&sc->sc_if, 1);
526 #ifdef DEVICE_POLLING
529 wi_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
531 struct wi_softc *sc = ifp->if_softc;
536 /* disable interruptds */
537 CSR_WRITE_2(sc, WI_INT_EN, 0);
539 case POLL_DEREGISTER:
540 /* enable interrupts */
541 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
544 status = CSR_READ_2(sc, WI_EVENT_STAT);
546 if (status & WI_EV_RX)
548 if (status & WI_EV_ALLOC)
550 if (status & WI_EV_INFO)
553 if (cmd == POLL_AND_CHECK_STATUS) {
554 if (status & WI_EV_INFO)
558 if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
559 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 && !ifq_is_empty(&ifp->if_snd)) {
565 #endif /* DEVICE_POLLING */
570 struct wi_softc *sc = arg;
571 struct ifnet *ifp = &sc->sc_ic.ic_if;
575 if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) {
576 CSR_WRITE_2(sc, WI_INT_EN, 0);
577 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
583 /* Disable interrupts. */
584 CSR_WRITE_2(sc, WI_INT_EN, 0);
586 status = CSR_READ_2(sc, WI_EVENT_STAT);
587 if (status & WI_EV_RX)
589 if (status & WI_EV_ALLOC)
591 if (status & WI_EV_TX_EXC)
593 if (status & WI_EV_INFO)
595 if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
596 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 &&
597 !ifq_is_empty(&ifp->if_snd))
600 /* Re-enable interrupts. */
601 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
611 struct wi_softc *sc = arg;
612 struct ifnet *ifp = &sc->sc_if;
613 struct ieee80211com *ic = &sc->sc_ic;
614 struct wi_joinreq join;
616 int error = 0, wasenabled;
626 if ((wasenabled = sc->sc_enabled))
630 /* common 802.11 configuration */
631 ic->ic_flags &= ~IEEE80211_F_IBSSON;
632 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
633 switch (ic->ic_opmode) {
634 case IEEE80211_M_STA:
635 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS);
637 case IEEE80211_M_IBSS:
638 wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port);
639 ic->ic_flags |= IEEE80211_F_IBSSON;
641 case IEEE80211_M_AHDEMO:
642 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
644 case IEEE80211_M_HOSTAP:
646 * For PRISM cards, override the empty SSID, because in
647 * HostAP mode the controller will lock up otherwise.
649 if (sc->sc_firmware_type == WI_INTERSIL &&
650 ic->ic_des_esslen == 0) {
651 ic->ic_des_essid[0] = ' ';
652 ic->ic_des_esslen = 1;
654 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP);
656 case IEEE80211_M_MONITOR:
657 if (sc->sc_firmware_type == WI_LUCENT)
658 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
659 wi_cmd(sc, WI_CMD_DEBUG | (WI_TEST_MONITOR << 8), 0, 0, 0);
663 /* Intersil interprets this RID as joining ESS even in IBSS mode */
664 if (sc->sc_firmware_type == WI_LUCENT &&
665 (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0)
666 wi_write_val(sc, WI_RID_CREATE_IBSS, 1);
668 wi_write_val(sc, WI_RID_CREATE_IBSS, 0);
669 wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
670 wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid,
672 wi_write_val(sc, WI_RID_OWN_CHNL,
673 ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
674 wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen);
676 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
677 wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN);
679 wi_write_val(sc, WI_RID_PM_ENABLED,
680 (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
682 /* not yet common 802.11 configuration */
683 wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen);
684 wi_write_val(sc, WI_RID_RTS_THRESH, ic->ic_rtsthreshold);
685 if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
686 wi_write_val(sc, WI_RID_FRAG_THRESH, ic->ic_fragthreshold);
688 /* driver specific 802.11 configuration */
689 if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)
690 wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale);
691 if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
692 wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode);
693 if (sc->sc_flags & WI_FLAGS_HAS_MOR)
694 wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven);
696 wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen);
698 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
699 sc->sc_firmware_type == WI_INTERSIL) {
700 wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval);
701 wi_write_val(sc, WI_RID_BASIC_RATE, 0x03); /* 1, 2 */
702 wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */
703 wi_write_val(sc, WI_RID_DTIM_PERIOD, 1);
707 * Initialize promisc mode.
708 * Being in the Host-AP mode causes a great
709 * deal of pain if primisc mode is set.
710 * Therefore we avoid confusing the firmware
711 * and always reset promisc mode in Host-AP
712 * mode. Host-AP sees all the packets anyway.
714 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
715 (ifp->if_flags & IFF_PROMISC) != 0) {
716 wi_write_val(sc, WI_RID_PROMISC, 1);
718 wi_write_val(sc, WI_RID_PROMISC, 0);
722 if (ic->ic_caps & IEEE80211_C_WEP)
725 /* Set multicast filter. */
728 /* Allocate fids for the card */
729 if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
730 sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
731 if (sc->sc_firmware_type == WI_SYMBOL)
732 sc->sc_buflen = 1585; /* XXX */
733 for (i = 0; i < sc->sc_ntxbuf; i++) {
734 error = wi_alloc_fid(sc, sc->sc_buflen,
735 &sc->sc_txd[i].d_fid);
737 device_printf(sc->sc_dev,
738 "tx buffer allocation failed (error %u)\n",
742 sc->sc_txd[i].d_len = 0;
745 sc->sc_txcur = sc->sc_txnext = 0;
747 /* Enable desired port */
748 wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
751 ifp->if_flags |= IFF_RUNNING;
752 ifp->if_flags &= ~IFF_OACTIVE;
753 if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
754 ic->ic_opmode == IEEE80211_M_MONITOR ||
755 ic->ic_opmode == IEEE80211_M_HOSTAP)
756 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
758 /* Enable interrupts if not polling */
759 #ifdef DEVICE_POLLING
760 if ((ifp->if_flags & IFF_POLLING) == 0)
762 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
765 ic->ic_opmode == IEEE80211_M_HOSTAP &&
766 sc->sc_firmware_type == WI_INTERSIL) {
767 /* XXX: some card need to be re-enabled for hostap */
768 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
769 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
772 if (ic->ic_opmode == IEEE80211_M_STA &&
773 ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
774 ic->ic_des_chan != IEEE80211_CHAN_ANYC)) {
775 memset(&join, 0, sizeof(join));
776 if (ic->ic_flags & IEEE80211_F_DESBSSID)
777 IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
778 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC)
779 join.wi_chan = htole16(
780 ieee80211_chan2ieee(ic, ic->ic_des_chan));
781 /* Lucent firmware does not support the JOIN RID. */
782 if (sc->sc_firmware_type != WI_LUCENT)
783 wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
790 if_printf(ifp, "interface not running\n");
794 DPRINTF(("wi_init: return %d\n", error));
799 wi_stop(struct ifnet *ifp, int disable)
801 struct ieee80211com *ic = (struct ieee80211com *) ifp;
802 struct wi_softc *sc = ifp->if_softc;
809 ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
810 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
811 if (sc->sc_enabled && !sc->wi_gone) {
812 CSR_WRITE_2(sc, WI_INT_EN, 0);
813 wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
817 (*sc->sc_disable)(sc);
821 } else if (sc->wi_gone && disable) /* gone --> not enabled */
825 sc->sc_scan_timer = 0;
826 sc->sc_syn_timer = 0;
827 sc->sc_false_syns = 0;
835 wi_start(struct ifnet *ifp)
837 struct wi_softc *sc = ifp->if_softc;
838 struct ieee80211com *ic = &sc->sc_ic;
839 struct ieee80211_node *ni;
840 struct ieee80211_frame *wh;
842 struct wi_frame frmhdr;
843 int cur, fid, off, error;
852 if (sc->sc_flags & WI_FLAGS_OUTRANGE) {
857 memset(&frmhdr, 0, sizeof(frmhdr));
860 IF_POLL(&ic->ic_mgtq, m0);
862 if (sc->sc_txd[cur].d_len != 0) {
863 ifp->if_flags |= IFF_OACTIVE;
866 IF_DEQUEUE(&ic->ic_mgtq, m0);
868 * Hack! The referenced node pointer is in the
869 * rcvif field of the packet header. This is
870 * placed there by ieee80211_mgmt_output because
871 * we need to hold the reference with the frame
872 * and there's no other way (other than packet
873 * tags which we consider too expensive to use)
876 ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
877 m0->m_pkthdr.rcvif = NULL;
879 m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
880 (caddr_t)&frmhdr.wi_ehdr);
881 frmhdr.wi_ehdr.ether_type = 0;
882 wh = mtod(m0, struct ieee80211_frame *);
884 if (ic->ic_state != IEEE80211_S_RUN)
886 m0 = ifq_poll(&ifp->if_snd);
889 if (sc->sc_txd[cur].d_len != 0) {
890 ifp->if_flags |= IFF_OACTIVE;
893 m0 = ifq_dequeue(&ifp->if_snd);
895 m_copydata(m0, 0, ETHER_HDR_LEN,
896 (caddr_t)&frmhdr.wi_ehdr);
899 m0 = ieee80211_encap(ifp, m0, &ni);
904 wh = mtod(m0, struct ieee80211_frame *);
905 if (ic->ic_flags & IEEE80211_F_WEPON)
906 wh->i_fc[1] |= IEEE80211_FC1_WEP;
910 if (ic->ic_rawbpf != NULL)
911 bpf_mtap(ic->ic_rawbpf, m0);
913 frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
914 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
915 (wh->i_fc[1] & IEEE80211_FC1_WEP)) {
916 if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) {
918 if (ni && ni != ic->ic_bss)
919 ieee80211_free_node(ic, ni);
922 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
926 sc->sc_tx_th.wt_rate =
927 ni->ni_rates.rs_rates[ni->ni_txrate];
928 bpf_ptap(sc->sc_drvbpf, m0, &sc->sc_tx_th,
932 m_copydata(m0, 0, sizeof(struct ieee80211_frame),
933 (caddr_t)&frmhdr.wi_whdr);
934 m_adj(m0, sizeof(struct ieee80211_frame));
935 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
936 if (IFF_DUMPPKTS(ifp))
937 wi_dump_pkt(&frmhdr, NULL, -1);
938 fid = sc->sc_txd[cur].d_fid;
939 off = sizeof(frmhdr);
940 error = wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0
941 || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
943 if (ni && ni != ic->ic_bss)
944 ieee80211_free_node(ic, ni);
949 sc->sc_txd[cur].d_len = off;
950 if (sc->sc_txcur == cur) {
951 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
952 if_printf(ifp, "xmit failed\n");
953 sc->sc_txd[cur].d_len = 0;
959 sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
966 wi_reset(struct wi_softc *sc)
968 struct ieee80211com *ic = &sc->sc_ic;
969 struct ifnet *ifp = &ic->ic_if;
970 #define WI_INIT_TRIES 3
975 /* Symbol firmware cannot be initialized more than once */
976 if (sc->sc_firmware_type == WI_SYMBOL && sc->sc_reset)
978 if (sc->sc_firmware_type == WI_SYMBOL)
981 tries = WI_INIT_TRIES;
983 for (i = 0; i < tries; i++) {
984 if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
986 DELAY(WI_DELAY * 1000);
991 if_printf(ifp, "init failed\n");
995 CSR_WRITE_2(sc, WI_INT_EN, 0);
996 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
998 /* Calibrate timer. */
999 wi_write_val(sc, WI_RID_TICK_TIME, 8);
1002 #undef WI_INIT_TRIES
1006 wi_watchdog(struct ifnet *ifp)
1008 struct wi_softc *sc = ifp->if_softc;
1011 if (!sc->sc_enabled)
1014 if (sc->sc_tx_timer) {
1015 if (--sc->sc_tx_timer == 0) {
1016 if_printf(ifp, "device timeout\n");
1018 wi_init(ifp->if_softc);
1024 if (sc->sc_scan_timer) {
1025 if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
1026 sc->sc_firmware_type == WI_INTERSIL) {
1027 DPRINTF(("wi_watchdog: inquire scan\n"));
1028 wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
1030 if (sc->sc_scan_timer)
1034 if (sc->sc_syn_timer) {
1035 if (--sc->sc_syn_timer == 0) {
1036 struct ieee80211com *ic = (struct ieee80211com *) ifp;
1037 DPRINTF2(("wi_watchdog: %d false syns\n",
1038 sc->sc_false_syns));
1039 sc->sc_false_syns = 0;
1040 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1041 sc->sc_syn_timer = 5;
1046 /* TODO: rate control */
1047 ieee80211_watchdog(ifp);
1051 wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1053 struct wi_softc *sc = ifp->if_softc;
1054 struct ieee80211com *ic = &sc->sc_ic;
1055 struct ifreq *ifr = (struct ifreq *)data;
1056 struct ieee80211req *ireq;
1057 u_int8_t nodename[IEEE80211_NWID_LEN];
1072 * Can't do promisc and hostap at the same time. If all that's
1073 * changing is the promisc flag, try to short-circuit a call to
1074 * wi_init() by just setting PROMISC in the hardware.
1076 if (ifp->if_flags & IFF_UP) {
1077 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1078 ifp->if_flags & IFF_RUNNING) {
1079 if (ifp->if_flags & IFF_PROMISC &&
1080 !(sc->sc_if_flags & IFF_PROMISC)) {
1081 wi_write_val(sc, WI_RID_PROMISC, 1);
1082 } else if (!(ifp->if_flags & IFF_PROMISC) &&
1083 sc->sc_if_flags & IFF_PROMISC) {
1084 wi_write_val(sc, WI_RID_PROMISC, 0);
1092 if (ifp->if_flags & IFF_RUNNING) {
1097 sc->sc_if_flags = ifp->if_flags;
1102 error = wi_write_multi(sc);
1104 case SIOCGIFGENERIC:
1105 error = wi_get_cfg(ifp, cmd, data, cr);
1107 case SIOCSIFGENERIC:
1108 error = suser_cred(cr, NULL_CRED_OKAY);
1111 error = wi_set_cfg(ifp, cmd, data);
1113 case SIOCGPRISM2DEBUG:
1114 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1117 if (!(ifp->if_flags & IFF_RUNNING) ||
1118 sc->sc_firmware_type == WI_LUCENT) {
1122 error = wi_get_debug(sc, &wreq);
1124 error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
1126 case SIOCSPRISM2DEBUG:
1127 if ((error = suser_cred(cr, NULL_CRED_OKAY)))
1129 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1132 error = wi_set_debug(sc, &wreq);
1135 ireq = (struct ieee80211req *) data;
1136 switch (ireq->i_type) {
1137 case IEEE80211_IOC_STATIONNAME:
1138 ireq->i_len = sc->sc_nodelen + 1;
1139 error = copyout(sc->sc_nodename, ireq->i_data,
1143 error = ieee80211_ioctl(ifp, cmd, data, cr);
1148 error = suser_cred(cr, NULL_CRED_OKAY);
1151 ireq = (struct ieee80211req *) data;
1152 switch (ireq->i_type) {
1153 case IEEE80211_IOC_STATIONNAME:
1154 if (ireq->i_val != 0 ||
1155 ireq->i_len > IEEE80211_NWID_LEN) {
1159 memset(nodename, 0, IEEE80211_NWID_LEN);
1160 error = copyin(ireq->i_data, nodename, ireq->i_len);
1163 if (sc->sc_enabled) {
1164 error = wi_write_ssid(sc, WI_RID_NODENAME,
1165 nodename, ireq->i_len);
1169 memcpy(sc->sc_nodename, nodename, IEEE80211_NWID_LEN);
1170 sc->sc_nodelen = ireq->i_len;
1173 error = ieee80211_ioctl(ifp, cmd, data, cr);
1178 if (ifp->if_flags & IFF_RUNNING)
1182 error = ieee80211_ioctl(ifp, cmd, data, cr);
1185 if (error == ENETRESET) {
1187 wi_init(sc); /* XXX no error return */
1197 wi_media_change(struct ifnet *ifp)
1199 struct wi_softc *sc = ifp->if_softc;
1202 error = ieee80211_media_change(ifp);
1203 if (error == ENETRESET) {
1205 wi_init(sc); /* XXX no error return */
1212 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1214 struct wi_softc *sc = ifp->if_softc;
1215 struct ieee80211com *ic = &sc->sc_ic;
1219 if (sc->wi_gone || !sc->sc_enabled) {
1220 imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
1221 imr->ifm_status = 0;
1225 imr->ifm_status = IFM_AVALID;
1226 imr->ifm_active = IFM_IEEE80211;
1227 if (ic->ic_state == IEEE80211_S_RUN &&
1228 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
1229 imr->ifm_status |= IFM_ACTIVE;
1231 if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) != 0)
1234 /* convert to 802.11 rate */
1236 if (sc->sc_firmware_type == WI_LUCENT) {
1238 rate = 11; /* 5.5Mbps */
1239 else if (rate == 5 * 2)
1240 rate = 22; /* 11Mbps */
1243 rate = 11; /* 5.5Mbps */
1244 else if (rate == 8*2)
1245 rate = 22; /* 11Mbps */
1248 imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
1249 switch (ic->ic_opmode) {
1250 case IEEE80211_M_STA:
1252 case IEEE80211_M_IBSS:
1253 imr->ifm_active |= IFM_IEEE80211_ADHOC;
1255 case IEEE80211_M_AHDEMO:
1256 imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1258 case IEEE80211_M_HOSTAP:
1259 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1261 case IEEE80211_M_MONITOR:
1262 imr->ifm_active |= IFM_IEEE80211_MONITOR;
1268 wi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1270 struct ieee80211com *ic = &sc->sc_ic;
1271 struct ieee80211_node *ni = ic->ic_bss;
1272 struct ifnet *ifp = &ic->ic_if;
1274 if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1277 DPRINTF(("wi_sync_bssid: bssid %6D -> ", ni->ni_bssid, ":"));
1278 DPRINTF(("%6D ?\n", new_bssid, ":"));
1280 /* In promiscuous mode, the BSSID field is not a reliable
1281 * indicator of the firmware's BSSID. Damp spurious
1282 * change-of-BSSID indications.
1284 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1285 sc->sc_false_syns >= WI_MAX_FALSE_SYNS)
1288 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1292 wi_rx_monitor(struct wi_softc *sc, int fid)
1294 struct ieee80211com *ic = &sc->sc_ic;
1295 struct ifnet *ifp = &ic->ic_if;
1296 struct wi_frame *rx_frame;
1300 /* first allocate mbuf for packet storage */
1301 m = m_getcl(MB_DONTWAIT, MT_DATA, 0);
1307 m->m_pkthdr.rcvif = ifp;
1309 /* now read wi_frame first so we know how much data to read */
1310 if (wi_read_bap(sc, fid, 0, mtod(m, caddr_t), sizeof(*rx_frame))) {
1315 rx_frame = mtod(m, struct wi_frame *);
1317 switch ((rx_frame->wi_status & WI_STAT_MAC_PORT) >> 8) {
1319 switch (rx_frame->wi_whdr.i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1320 case IEEE80211_FC0_TYPE_DATA:
1321 hdrlen = WI_DATA_HDRLEN;
1322 datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1324 case IEEE80211_FC0_TYPE_MGT:
1325 hdrlen = WI_MGMT_HDRLEN;
1326 datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1328 case IEEE80211_FC0_TYPE_CTL:
1330 * prism2 cards don't pass control packets
1331 * down properly or consistently, so we'll only
1332 * pass down the header.
1334 hdrlen = WI_CTL_HDRLEN;
1338 if_printf(ifp, "received packet of unknown type "
1345 hdrlen = WI_DATA_HDRLEN;
1346 datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1349 if_printf(ifp, "received packet on invalid "
1350 "port (wi_status=0x%x)\n", rx_frame->wi_status);
1355 if (hdrlen + datlen + 2 > MCLBYTES) {
1356 if_printf(ifp, "oversized packet received "
1357 "(wi_dat_len=%d, wi_status=0x%x)\n",
1358 datlen, rx_frame->wi_status);
1363 if (wi_read_bap(sc, fid, hdrlen, mtod(m, caddr_t) + hdrlen,
1365 m->m_pkthdr.len = m->m_len = hdrlen + datlen;
1367 BPF_MTAP(ifp, m); /* Handle BPF listeners. */
1375 wi_rx_intr(struct wi_softc *sc)
1377 struct ieee80211com *ic = &sc->sc_ic;
1378 struct ifnet *ifp = &ic->ic_if;
1379 struct wi_frame frmhdr;
1381 struct ieee80211_frame *wh;
1382 struct ieee80211_node *ni;
1383 int fid, len, off, rssi;
1388 fid = CSR_READ_2(sc, WI_RX_FID);
1390 if (sc->wi_debug.wi_monitor) {
1392 * If we are in monitor mode just
1393 * read the data from the device.
1395 wi_rx_monitor(sc, fid);
1396 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1400 /* First read in the frame header */
1401 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1402 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1404 DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1408 if (IFF_DUMPPKTS(ifp))
1409 wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal);
1412 * Drop undecryptable or packets with receive errors here
1414 status = le16toh(frmhdr.wi_status);
1415 if (status & WI_STAT_ERRSTAT) {
1416 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1418 DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1421 rssi = frmhdr.wi_rx_signal;
1422 rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1423 le16toh(frmhdr.wi_rx_tstamp1);
1425 len = le16toh(frmhdr.wi_dat_len);
1426 off = ALIGN(sizeof(struct ieee80211_frame));
1429 * Sometimes the PRISM2.x returns bogusly large frames. Except
1430 * in monitor mode, just throw them away.
1432 if (off + len > MCLBYTES) {
1433 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1434 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1436 DPRINTF(("wi_rx_intr: oversized packet\n"));
1442 m = m_getl(off + len, MB_DONTWAIT, MT_DATA, M_PKTHDR, NULL);
1444 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1446 DPRINTF(("wi_rx_intr: m_getl failed\n"));
1450 m->m_data += off - sizeof(struct ieee80211_frame);
1451 memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1452 wi_read_bap(sc, fid, sizeof(frmhdr),
1453 m->m_data + sizeof(struct ieee80211_frame), len);
1454 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1455 m->m_pkthdr.rcvif = ifp;
1457 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1459 if (sc->sc_drvbpf) {
1460 /* XXX replace divide by table */
1461 sc->sc_rx_th.wr_rate = frmhdr.wi_rx_rate / 5;
1462 sc->sc_rx_th.wr_antsignal = frmhdr.wi_rx_signal;
1463 sc->sc_rx_th.wr_antnoise = frmhdr.wi_rx_silence;
1464 sc->sc_rx_th.wr_flags = 0;
1465 if (frmhdr.wi_status & WI_STAT_PCF)
1466 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_CFP;
1467 bpf_ptap(sc->sc_drvbpf, m, &sc->sc_rx_th, sc->sc_rx_th_len);
1470 wh = mtod(m, struct ieee80211_frame *);
1471 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1473 * WEP is decrypted by hardware. Clear WEP bit
1474 * header for ieee80211_input().
1476 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1479 /* synchronize driver's BSSID with firmware's BSSID */
1480 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1481 if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1482 wi_sync_bssid(sc, wh->i_addr3);
1485 * Locate the node for sender, track state, and
1486 * then pass this node (referenced) up to the 802.11
1487 * layer for its use. We are required to pass
1488 * something so we fallback to ic_bss when this frame
1489 * is from an unknown sender.
1491 if (ic->ic_opmode != IEEE80211_M_STA) {
1492 ni = ieee80211_find_node(ic, wh->i_addr2);
1494 ni = ieee80211_ref_node(ic->ic_bss);
1496 ni = ieee80211_ref_node(ic->ic_bss);
1498 * Send frame up for processing.
1500 ieee80211_input(ifp, m, ni, rssi, rstamp);
1502 * The frame may have caused the node to be marked for
1503 * reclamation (e.g. in response to a DEAUTH message)
1504 * so use free_node here instead of unref_node.
1506 if (ni == ic->ic_bss)
1507 ieee80211_unref_node(&ni);
1509 ieee80211_free_node(ic, ni);
1513 wi_tx_ex_intr(struct wi_softc *sc)
1515 struct ieee80211com *ic = &sc->sc_ic;
1516 struct ifnet *ifp = &ic->ic_if;
1517 struct wi_frame frmhdr;
1520 fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1521 /* Read in the frame header */
1522 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1523 u_int16_t status = le16toh(frmhdr.wi_status);
1526 * Spontaneous station disconnects appear as xmit
1527 * errors. Don't announce them and/or count them
1528 * as an output error.
1530 if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1531 if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1532 if_printf(ifp, "tx failed");
1533 if (status & WI_TXSTAT_RET_ERR)
1534 printf(", retry limit exceeded");
1535 if (status & WI_TXSTAT_AGED_ERR)
1536 printf(", max transmit lifetime exceeded");
1537 if (status & WI_TXSTAT_DISCONNECT)
1538 printf(", port disconnected");
1539 if (status & WI_TXSTAT_FORM_ERR)
1540 printf(", invalid format (data len %u src %6D)",
1541 le16toh(frmhdr.wi_dat_len),
1542 frmhdr.wi_ehdr.ether_shost, ":");
1544 printf(", status=0x%x", status);
1549 DPRINTF(("port disconnected\n"));
1550 ifp->if_collisions++; /* XXX */
1553 DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
1554 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1558 wi_tx_intr(struct wi_softc *sc)
1560 struct ieee80211com *ic = &sc->sc_ic;
1561 struct ifnet *ifp = &ic->ic_if;
1567 fid = CSR_READ_2(sc, WI_ALLOC_FID);
1568 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1571 if (sc->sc_txd[cur].d_fid != fid) {
1572 if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
1573 fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
1576 sc->sc_tx_timer = 0;
1577 sc->sc_txd[cur].d_len = 0;
1578 sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
1579 if (sc->sc_txd[cur].d_len == 0)
1580 ifp->if_flags &= ~IFF_OACTIVE;
1582 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1584 if_printf(ifp, "xmit failed\n");
1585 sc->sc_txd[cur].d_len = 0;
1587 sc->sc_tx_timer = 5;
1594 wi_info_intr(struct wi_softc *sc)
1596 struct ieee80211com *ic = &sc->sc_ic;
1597 struct ifnet *ifp = &ic->ic_if;
1598 int i, fid, len, off;
1603 fid = CSR_READ_2(sc, WI_INFO_FID);
1604 wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
1606 switch (le16toh(ltbuf[1])) {
1608 case WI_INFO_LINK_STAT:
1609 wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1610 DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1611 switch (le16toh(stat)) {
1612 case WI_INFO_LINK_STAT_CONNECTED:
1613 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1614 if (ic->ic_state == IEEE80211_S_RUN &&
1615 ic->ic_opmode != IEEE80211_M_IBSS)
1618 case WI_INFO_LINK_STAT_AP_CHG:
1619 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1621 case WI_INFO_LINK_STAT_AP_INR:
1622 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1624 case WI_INFO_LINK_STAT_AP_OOR:
1625 if (sc->sc_firmware_type == WI_SYMBOL &&
1626 sc->sc_scan_timer > 0) {
1627 if (wi_cmd(sc, WI_CMD_INQUIRE,
1628 WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
1629 sc->sc_scan_timer = 0;
1632 if (ic->ic_opmode == IEEE80211_M_STA)
1633 sc->sc_flags |= WI_FLAGS_OUTRANGE;
1635 case WI_INFO_LINK_STAT_DISCONNECTED:
1636 case WI_INFO_LINK_STAT_ASSOC_FAILED:
1637 if (ic->ic_opmode == IEEE80211_M_STA)
1638 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1643 case WI_INFO_COUNTERS:
1644 /* some card versions have a larger stats structure */
1645 len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1646 ptr = (u_int32_t *)&sc->sc_stats;
1647 off = sizeof(ltbuf);
1648 for (i = 0; i < len; i++, off += 2, ptr++) {
1649 wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1650 #ifdef WI_HERMES_STATS_WAR
1656 ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1657 sc->sc_stats.wi_tx_multi_retries +
1658 sc->sc_stats.wi_tx_retry_limit;
1661 case WI_INFO_SCAN_RESULTS:
1662 case WI_INFO_HOST_SCAN_RESULTS:
1663 wi_scan_result(sc, fid, le16toh(ltbuf[0]));
1667 DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1668 le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1671 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1675 wi_write_multi(struct wi_softc *sc)
1677 struct ifnet *ifp = &sc->sc_ic.ic_if;
1679 struct ifmultiaddr *ifma;
1680 struct wi_mcast mlist;
1682 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1684 memset(&mlist, 0, sizeof(mlist));
1685 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1690 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1691 if (ifma->ifma_addr->sa_family != AF_LINK)
1695 IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1696 (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1699 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1700 IEEE80211_ADDR_LEN * n);
1704 wi_read_nicid(struct wi_softc *sc)
1706 struct wi_card_ident *id;
1711 /* getting chip identity */
1712 memset(ver, 0, sizeof(ver));
1714 wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1715 device_printf(sc->sc_dev, "using ");
1717 sc->sc_firmware_type = WI_NOTYPE;
1718 for (id = wi_card_ident; id->card_name != NULL; id++) {
1719 if (le16toh(ver[0]) == id->card_id) {
1720 printf("%s", id->card_name);
1721 sc->sc_firmware_type = id->firm_type;
1725 if (sc->sc_firmware_type == WI_NOTYPE) {
1726 if (le16toh(ver[0]) & 0x8000) {
1727 printf("Unknown PRISM2 chip");
1728 sc->sc_firmware_type = WI_INTERSIL;
1730 printf("Unknown Lucent chip");
1731 sc->sc_firmware_type = WI_LUCENT;
1735 /* get primary firmware version (Only Prism chips) */
1736 if (sc->sc_firmware_type != WI_LUCENT) {
1737 memset(ver, 0, sizeof(ver));
1739 wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1740 sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1741 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1744 /* get station firmware version */
1745 memset(ver, 0, sizeof(ver));
1747 wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1748 sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1749 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1750 if (sc->sc_firmware_type == WI_INTERSIL &&
1751 (sc->sc_sta_firmware_ver == 10102 ||
1752 sc->sc_sta_firmware_ver == 20102)) {
1754 memset(ident, 0, sizeof(ident));
1755 len = sizeof(ident);
1756 /* value should be the format like "V2.00-11" */
1757 if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1758 *(p = (char *)ident) >= 'A' &&
1759 p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1760 sc->sc_firmware_type = WI_SYMBOL;
1761 sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1762 (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1763 (p[6] - '0') * 10 + (p[7] - '0');
1767 device_printf(sc->sc_dev, "%s Firmware: ",
1768 sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
1769 (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
1770 if (sc->sc_firmware_type != WI_LUCENT) /* XXX */
1771 printf("Primary (%u.%u.%u), ",
1772 sc->sc_pri_firmware_ver / 10000,
1773 (sc->sc_pri_firmware_ver % 10000) / 100,
1774 sc->sc_pri_firmware_ver % 100);
1775 printf("Station (%u.%u.%u)\n",
1776 sc->sc_sta_firmware_ver / 10000,
1777 (sc->sc_sta_firmware_ver % 10000) / 100,
1778 sc->sc_sta_firmware_ver % 100);
1782 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1784 struct wi_ssid ssid;
1786 if (buflen > IEEE80211_NWID_LEN)
1788 memset(&ssid, 0, sizeof(ssid));
1789 ssid.wi_len = htole16(buflen);
1790 memcpy(ssid.wi_ssid, buf, buflen);
1791 return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1795 wi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1797 struct wi_softc *sc = ifp->if_softc;
1798 struct ieee80211com *ic = &sc->sc_ic;
1799 struct ifreq *ifr = (struct ifreq *)data;
1801 struct wi_scan_res *res;
1803 int len, n, error, mif, val, off, i;
1805 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1808 len = (wreq.wi_len - 1) * 2;
1809 if (len < sizeof(u_int16_t))
1811 if (len > sizeof(wreq.wi_val))
1812 len = sizeof(wreq.wi_val);
1814 switch (wreq.wi_type) {
1816 case WI_RID_IFACE_STATS:
1817 memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
1818 if (len < sizeof(sc->sc_stats))
1821 len = sizeof(sc->sc_stats);
1824 case WI_RID_ENCRYPTION:
1825 case WI_RID_TX_CRYPT_KEY:
1826 case WI_RID_DEFLT_CRYPT_KEYS:
1827 case WI_RID_TX_RATE:
1828 return ieee80211_cfgget(ifp, cmd, data, cr);
1830 case WI_RID_MICROWAVE_OVEN:
1831 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
1832 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1836 wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
1837 len = sizeof(u_int16_t);
1840 case WI_RID_DBM_ADJUST:
1841 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
1842 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1846 wreq.wi_val[0] = htole16(sc->sc_dbm_offset);
1847 len = sizeof(u_int16_t);
1850 case WI_RID_ROAMING_MODE:
1851 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
1852 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1856 wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
1857 len = sizeof(u_int16_t);
1860 case WI_RID_SYSTEM_SCALE:
1861 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
1862 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1866 wreq.wi_val[0] = htole16(sc->sc_system_scale);
1867 len = sizeof(u_int16_t);
1870 case WI_RID_FRAG_THRESH:
1871 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
1872 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1876 wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
1877 len = sizeof(u_int16_t);
1880 case WI_RID_READ_APS:
1881 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1882 return ieee80211_cfgget(ifp, cmd, data, cr);
1883 if (sc->sc_scan_timer > 0) {
1884 error = EINPROGRESS;
1888 if (len < sizeof(n)) {
1892 if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
1893 n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
1894 len = sizeof(n) + sizeof(struct wi_apinfo) * n;
1895 memcpy(wreq.wi_val, &n, sizeof(n));
1896 memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
1897 sizeof(struct wi_apinfo) * n);
1901 wreq.wi_val[0] = sc->sc_firmware_type != WI_LUCENT;
1902 len = sizeof(u_int16_t);
1906 mif = wreq.wi_val[0];
1907 error = wi_cmd(sc, WI_CMD_READMIF, mif, 0, 0);
1908 val = CSR_READ_2(sc, WI_RESP0);
1909 wreq.wi_val[0] = val;
1910 len = sizeof(u_int16_t);
1913 case WI_RID_ZERO_CACHE:
1914 case WI_RID_PROCFRAME: /* ignore for compatibility */
1918 case WI_RID_READ_CACHE:
1919 return ieee80211_cfgget(ifp, cmd, data, cr);
1921 case WI_RID_SCAN_RES: /* compatibility interface */
1922 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1923 return ieee80211_cfgget(ifp, cmd, data, cr);
1924 if (sc->sc_scan_timer > 0) {
1925 error = EINPROGRESS;
1929 if (sc->sc_firmware_type == WI_LUCENT) {
1931 reslen = WI_WAVELAN_RES_SIZE;
1933 off = sizeof(struct wi_scan_p2_hdr);
1934 reslen = WI_PRISM2_RES_SIZE;
1936 if (len < off + reslen * n)
1937 n = (len - off) / reslen;
1938 len = off + reslen * n;
1940 struct wi_scan_p2_hdr *p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1942 * Prepend Prism-specific header.
1944 if (len < sizeof(struct wi_scan_p2_hdr)) {
1948 p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1950 p2->wi_reason = n; /* XXX */
1952 for (i = 0; i < n; i++, off += reslen) {
1953 const struct wi_apinfo *ap = &sc->sc_aps[i];
1955 res = (struct wi_scan_res *)((char *)wreq.wi_val + off);
1956 res->wi_chan = ap->channel;
1957 res->wi_noise = ap->noise;
1958 res->wi_signal = ap->signal;
1959 IEEE80211_ADDR_COPY(res->wi_bssid, ap->bssid);
1960 res->wi_interval = ap->interval;
1961 res->wi_capinfo = ap->capinfo;
1962 res->wi_ssid_len = ap->namelen;
1963 memcpy(res->wi_ssid, ap->name,
1964 IEEE80211_NWID_LEN);
1965 if (sc->sc_firmware_type != WI_LUCENT) {
1966 /* XXX not saved from Prism cards */
1967 memset(res->wi_srates, 0,
1968 sizeof(res->wi_srates));
1969 res->wi_rate = ap->rate;
1976 if (sc->sc_enabled) {
1977 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1981 switch (wreq.wi_type) {
1982 case WI_RID_MAX_DATALEN:
1983 wreq.wi_val[0] = htole16(sc->sc_max_datalen);
1984 len = sizeof(u_int16_t);
1986 case WI_RID_RTS_THRESH:
1987 wreq.wi_val[0] = htole16(ic->ic_rtsthreshold);
1988 len = sizeof(u_int16_t);
1990 case WI_RID_CNFAUTHMODE:
1991 wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
1992 len = sizeof(u_int16_t);
1994 case WI_RID_NODENAME:
1995 if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
1999 len = sc->sc_nodelen + sizeof(u_int16_t);
2000 wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
2001 memcpy(&wreq.wi_val[1], sc->sc_nodename,
2005 return ieee80211_cfgget(ifp, cmd, data, cr);
2011 wreq.wi_len = (len + 1) / 2 + 1;
2012 return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
2016 wi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
2018 struct wi_softc *sc = ifp->if_softc;
2019 struct ieee80211com *ic = &sc->sc_ic;
2020 struct ifreq *ifr = (struct ifreq *)data;
2023 int i, len, error, mif, val;
2024 struct ieee80211_rateset *rs;
2026 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2029 len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0;
2030 switch (wreq.wi_type) {
2031 case WI_RID_DBM_ADJUST:
2034 case WI_RID_NODENAME:
2035 if (le16toh(wreq.wi_val[0]) * 2 > len ||
2036 le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
2040 if (sc->sc_enabled) {
2041 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2046 sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
2047 memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen);
2050 case WI_RID_MICROWAVE_OVEN:
2051 case WI_RID_ROAMING_MODE:
2052 case WI_RID_SYSTEM_SCALE:
2053 case WI_RID_FRAG_THRESH:
2054 if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
2055 (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
2057 if (wreq.wi_type == WI_RID_ROAMING_MODE &&
2058 (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
2060 if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
2061 (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
2063 if (wreq.wi_type == WI_RID_FRAG_THRESH &&
2064 (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
2067 case WI_RID_RTS_THRESH:
2068 case WI_RID_CNFAUTHMODE:
2069 case WI_RID_MAX_DATALEN:
2070 if (sc->sc_enabled) {
2071 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2076 switch (wreq.wi_type) {
2077 case WI_RID_FRAG_THRESH:
2078 ic->ic_fragthreshold = le16toh(wreq.wi_val[0]);
2080 case WI_RID_RTS_THRESH:
2081 ic->ic_rtsthreshold = le16toh(wreq.wi_val[0]);
2083 case WI_RID_MICROWAVE_OVEN:
2084 sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
2086 case WI_RID_ROAMING_MODE:
2087 sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
2089 case WI_RID_SYSTEM_SCALE:
2090 sc->sc_system_scale = le16toh(wreq.wi_val[0]);
2092 case WI_RID_CNFAUTHMODE:
2093 sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
2095 case WI_RID_MAX_DATALEN:
2096 sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
2101 case WI_RID_TX_RATE:
2102 switch (le16toh(wreq.wi_val[0])) {
2104 ic->ic_fixed_rate = -1;
2107 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
2108 for (i = 0; i < rs->rs_nrates; i++) {
2109 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL)
2110 / 2 == le16toh(wreq.wi_val[0]))
2113 if (i == rs->rs_nrates)
2115 ic->ic_fixed_rate = i;
2118 error = wi_write_txrate(sc);
2121 case WI_RID_SCAN_APS:
2122 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2123 error = wi_scan_ap(sc, 0x3fff, 0x000f);
2126 case WI_RID_SCAN_REQ: /* compatibility interface */
2127 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2128 error = wi_scan_ap(sc, wreq.wi_val[0], wreq.wi_val[1]);
2131 case WI_RID_MGMT_XMIT:
2132 if (!sc->sc_enabled) {
2136 if (ic->ic_mgtq.ifq_len > 5) {
2140 /* XXX wi_len looks in u_int8_t, not in u_int16_t */
2141 m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL);
2146 IF_ENQUEUE(&ic->ic_mgtq, m);
2150 mif = wreq.wi_val[0];
2151 val = wreq.wi_val[1];
2152 error = wi_cmd(sc, WI_CMD_WRITEMIF, mif, val, 0);
2155 case WI_RID_PROCFRAME: /* ignore for compatibility */
2158 case WI_RID_OWN_SSID:
2159 if (le16toh(wreq.wi_val[0]) * 2 > len ||
2160 le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) {
2164 memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
2165 ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2;
2166 memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen);
2171 if (sc->sc_enabled) {
2172 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2177 error = ieee80211_cfgset(ifp, cmd, data);
2184 wi_write_txrate(struct wi_softc *sc)
2186 struct ieee80211com *ic = &sc->sc_ic;
2190 if (ic->ic_fixed_rate < 0)
2191 rate = 0; /* auto */
2193 rate = (ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ic->ic_fixed_rate] &
2194 IEEE80211_RATE_VAL) / 2;
2196 /* rate: 0, 1, 2, 5, 11 */
2198 switch (sc->sc_firmware_type) {
2201 case 0: /* auto == 11mbps auto */
2204 /* case 1, 2 map to 1, 2*/
2205 case 5: /* 5.5Mbps -> 4 */
2208 case 11: /* 11mbps -> 5 */
2216 /* Choose a bit according to this table.
2219 * ----+-------------------
2225 for (i = 8; i > 0; i >>= 1) {
2230 rate = 0xf; /* auto */
2235 return wi_write_val(sc, WI_RID_TX_RATE, rate);
2239 wi_write_wep(struct wi_softc *sc)
2241 struct ieee80211com *ic = &sc->sc_ic;
2245 struct wi_key wkey[IEEE80211_WEP_NKID];
2247 switch (sc->sc_firmware_type) {
2249 val = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0;
2250 error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
2253 error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_wep_txkey);
2256 memset(wkey, 0, sizeof(wkey));
2257 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2258 keylen = ic->ic_nw_keys[i].wk_len;
2259 wkey[i].wi_keylen = htole16(keylen);
2260 memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
2263 error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
2264 wkey, sizeof(wkey));
2269 if (ic->ic_flags & IEEE80211_F_WEPON) {
2271 * ONLY HWB3163 EVAL-CARD Firmware version
2272 * less than 0.8 variant2
2274 * If promiscuous mode disable, Prism2 chip
2275 * does not work with WEP .
2276 * It is under investigation for details.
2277 * (ichiro@netbsd.org)
2279 if (sc->sc_firmware_type == WI_INTERSIL &&
2280 sc->sc_sta_firmware_ver < 802 ) {
2281 /* firm ver < 0.8 variant 2 */
2282 wi_write_val(sc, WI_RID_PROMISC, 1);
2284 wi_write_val(sc, WI_RID_CNFAUTHMODE,
2285 sc->sc_cnfauthmode);
2286 val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
2288 * Encryption firmware has a bug for HostAP mode.
2290 if (sc->sc_firmware_type == WI_INTERSIL &&
2291 ic->ic_opmode == IEEE80211_M_HOSTAP)
2292 val |= HOST_ENCRYPT;
2294 wi_write_val(sc, WI_RID_CNFAUTHMODE,
2295 IEEE80211_AUTH_OPEN);
2296 val = HOST_ENCRYPT | HOST_DECRYPT;
2298 error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
2301 error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
2306 * It seems that the firmware accept 104bit key only if
2307 * all the keys have 104bit length. We get the length of
2308 * the transmit key and use it for all other keys.
2309 * Perhaps we should use software WEP for such situation.
2311 keylen = ic->ic_nw_keys[ic->ic_wep_txkey].wk_len;
2312 if (keylen > IEEE80211_WEP_KEYLEN)
2313 keylen = 13; /* 104bit keys */
2315 keylen = IEEE80211_WEP_KEYLEN;
2316 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2317 error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
2318 ic->ic_nw_keys[i].wk_key, keylen);
2328 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
2331 static volatile int count = 0;
2337 panic("Hey partner, hold on there!");
2340 /* wait for the busy bit to clear */
2341 for (i = sc->wi_cmd_count; i > 0; i--) { /* 500ms */
2342 if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
2344 DELAY(1*1000); /* 1ms */
2347 device_printf(sc->sc_dev, "wi_cmd: busy bit won't clear.\n" );
2353 CSR_WRITE_2(sc, WI_PARAM0, val0);
2354 CSR_WRITE_2(sc, WI_PARAM1, val1);
2355 CSR_WRITE_2(sc, WI_PARAM2, val2);
2356 CSR_WRITE_2(sc, WI_COMMAND, cmd);
2358 if (cmd == WI_CMD_INI) {
2359 /* XXX: should sleep here. */
2360 DELAY(100*1000); /* 100ms delay for init */
2362 for (i = 0; i < WI_TIMEOUT; i++) {
2364 * Wait for 'command complete' bit to be
2365 * set in the event status register.
2367 s = CSR_READ_2(sc, WI_EVENT_STAT);
2368 if (s & WI_EV_CMD) {
2369 /* Ack the event and read result code. */
2370 s = CSR_READ_2(sc, WI_STATUS);
2371 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
2372 if (s & WI_STAT_CMD_RESULT) {
2382 if (i == WI_TIMEOUT) {
2383 device_printf(sc->sc_dev,
2384 "timeout in wi_cmd 0x%04x; event status 0x%04x\n", cmd, s);
2393 wi_seek_bap(struct wi_softc *sc, int id, int off)
2397 CSR_WRITE_2(sc, WI_SEL0, id);
2398 CSR_WRITE_2(sc, WI_OFF0, off);
2400 for (i = 0; ; i++) {
2401 status = CSR_READ_2(sc, WI_OFF0);
2402 if ((status & WI_OFF_BUSY) == 0)
2404 if (i == WI_TIMEOUT) {
2405 device_printf(sc->sc_dev, "timeout in wi_seek to %x/%x\n",
2407 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2408 if (status == 0xffff)
2414 if (status & WI_OFF_ERR) {
2415 device_printf(sc->sc_dev, "failed in wi_seek to %x/%x\n", id, off);
2416 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2420 sc->sc_bap_off = off;
2425 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
2432 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2433 if ((error = wi_seek_bap(sc, id, off)) != 0)
2436 cnt = (buflen + 1) / 2;
2437 ptr = (u_int16_t *)buf;
2438 for (i = 0; i < cnt; i++)
2439 *ptr++ = CSR_READ_2(sc, WI_DATA0);
2440 sc->sc_bap_off += cnt * 2;
2445 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
2453 #ifdef WI_HERMES_AUTOINC_WAR
2456 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2457 if ((error = wi_seek_bap(sc, id, off)) != 0)
2460 cnt = (buflen + 1) / 2;
2461 ptr = (u_int16_t *)buf;
2462 for (i = 0; i < cnt; i++)
2463 CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
2464 sc->sc_bap_off += cnt * 2;
2466 #ifdef WI_HERMES_AUTOINC_WAR
2468 * According to the comments in the HCF Light code, there is a bug
2469 * in the Hermes (or possibly in certain Hermes firmware revisions)
2470 * where the chip's internal autoincrement counter gets thrown off
2471 * during data writes: the autoincrement is missed, causing one
2472 * data word to be overwritten and subsequent words to be written to
2473 * the wrong memory locations. The end result is that we could end
2474 * up transmitting bogus frames without realizing it. The workaround
2475 * for this is to write a couple of extra guard words after the end
2476 * of the transfer, then attempt to read then back. If we fail to
2477 * locate the guard words where we expect them, we preform the
2478 * transfer over again.
2480 if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
2481 CSR_WRITE_2(sc, WI_DATA0, 0x1234);
2482 CSR_WRITE_2(sc, WI_DATA0, 0x5678);
2483 wi_seek_bap(sc, id, sc->sc_bap_off);
2484 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2485 if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
2486 CSR_READ_2(sc, WI_DATA0) != 0x5678) {
2487 device_printf(sc->sc_dev,
2488 "detect auto increment bug, try again\n");
2497 wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
2502 for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
2506 len = min(m->m_len, totlen);
2508 if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
2509 m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
2510 return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
2514 if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
2524 wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
2528 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
2529 device_printf(sc->sc_dev, "failed to allocate %d bytes on NIC\n",
2534 for (i = 0; i < WI_TIMEOUT; i++) {
2535 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
2537 if (i == WI_TIMEOUT) {
2538 device_printf(sc->sc_dev, "timeout in alloc\n");
2543 *idp = CSR_READ_2(sc, WI_ALLOC_FID);
2544 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
2549 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
2554 /* Tell the NIC to enter record read mode. */
2555 error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2559 error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2563 if (le16toh(ltbuf[1]) != rid) {
2564 device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n",
2565 rid, le16toh(ltbuf[1]));
2568 len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */
2569 if (*buflenp < len) {
2570 device_printf(sc->sc_dev, "record buffer is too small, "
2571 "rid=%x, size=%d, len=%d\n",
2572 rid, *buflenp, len);
2576 return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2580 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2585 ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */
2586 ltbuf[1] = htole16(rid);
2588 error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2591 error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2595 return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
2599 wi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
2601 struct ifnet *ifp = &ic->ic_if;
2602 struct wi_softc *sc = ifp->if_softc;
2603 struct ieee80211_node *ni = ic->ic_bss;
2606 struct wi_ssid ssid;
2607 u_int8_t old_bssid[IEEE80211_ADDR_LEN];
2609 DPRINTF(("%s: %s -> %s\n", __func__,
2610 ieee80211_state_name[ic->ic_state],
2611 ieee80211_state_name[nstate]));
2614 case IEEE80211_S_INIT:
2615 ic->ic_flags &= ~IEEE80211_F_SIBSS;
2616 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2617 return (*sc->sc_newstate)(ic, nstate, arg);
2619 case IEEE80211_S_RUN:
2620 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2621 buflen = IEEE80211_ADDR_LEN;
2622 wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen);
2623 IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
2624 buflen = sizeof(val);
2625 wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
2626 /* XXX validate channel */
2627 ni->ni_chan = &ic->ic_channels[le16toh(val)];
2628 sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
2629 htole16(ni->ni_chan->ic_freq);
2630 sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
2631 htole16(ni->ni_chan->ic_flags);
2633 if (IEEE80211_ADDR_EQ(old_bssid, ni->ni_bssid))
2634 sc->sc_false_syns++;
2636 sc->sc_false_syns = 0;
2638 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2639 ni->ni_esslen = ic->ic_des_esslen;
2640 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
2641 ni->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B];
2642 ni->ni_intval = ic->ic_lintval;
2643 ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
2644 if (ic->ic_flags & IEEE80211_F_WEPON)
2645 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
2647 /* XXX check return value */
2648 buflen = sizeof(ssid);
2649 wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
2650 ni->ni_esslen = le16toh(ssid.wi_len);
2651 if (ni->ni_esslen > IEEE80211_NWID_LEN)
2652 ni->ni_esslen = IEEE80211_NWID_LEN; /*XXX*/
2653 memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
2657 case IEEE80211_S_SCAN:
2658 case IEEE80211_S_AUTH:
2659 case IEEE80211_S_ASSOC:
2663 ic->ic_state = nstate; /* NB: skip normal ieee80211 handling */
2668 wi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate)
2673 if (!sc->sc_enabled)
2675 switch (sc->sc_firmware_type) {
2677 (void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
2680 val[0] = chanmask; /* channel */
2681 val[1] = txrate; /* tx rate */
2682 error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
2686 * XXX only supported on 3.x ?
2688 val[0] = BSCAN_BCAST | BSCAN_ONETIME;
2689 error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
2690 val, sizeof(val[0]));
2694 sc->sc_scan_timer = WI_SCAN_WAIT;
2695 sc->sc_ic.ic_if.if_timer = 1;
2696 DPRINTF(("wi_scan_ap: start scanning, "
2697 "chamask 0x%x txrate 0x%x\n", chanmask, txrate));
2703 wi_scan_result(struct wi_softc *sc, int fid, int cnt)
2705 #define N(a) (sizeof (a) / sizeof (a[0]))
2706 int i, naps, off, szbuf;
2707 struct wi_scan_header ws_hdr; /* Prism2 header */
2708 struct wi_scan_data_p2 ws_dat; /* Prism2 scantable*/
2709 struct wi_apinfo *ap;
2711 off = sizeof(u_int16_t) * 2;
2712 memset(&ws_hdr, 0, sizeof(ws_hdr));
2713 switch (sc->sc_firmware_type) {
2715 wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
2716 off += sizeof(ws_hdr);
2717 szbuf = sizeof(struct wi_scan_data_p2);
2720 szbuf = sizeof(struct wi_scan_data_p2) + 6;
2723 szbuf = sizeof(struct wi_scan_data);
2726 device_printf(sc->sc_dev,
2727 "wi_scan_result: unknown firmware type %u\n",
2728 sc->sc_firmware_type);
2732 naps = (cnt * 2 + 2 - off) / szbuf;
2733 if (naps > N(sc->sc_aps))
2734 naps = N(sc->sc_aps);
2738 memset(&ws_dat, 0, sizeof(ws_dat));
2739 for (i = 0; i < naps; i++, ap++) {
2740 wi_read_bap(sc, fid, off, &ws_dat,
2741 (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
2742 DPRINTF2(("wi_scan_result: #%d: off %d bssid %6D\n", i, off,
2743 ws_dat.wi_bssid, ":"));
2745 ap->scanreason = le16toh(ws_hdr.wi_reason);
2746 memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
2747 ap->channel = le16toh(ws_dat.wi_chid);
2748 ap->signal = le16toh(ws_dat.wi_signal);
2749 ap->noise = le16toh(ws_dat.wi_noise);
2750 ap->quality = ap->signal - ap->noise;
2751 ap->capinfo = le16toh(ws_dat.wi_capinfo);
2752 ap->interval = le16toh(ws_dat.wi_interval);
2753 ap->rate = le16toh(ws_dat.wi_rate);
2754 ap->namelen = le16toh(ws_dat.wi_namelen);
2755 if (ap->namelen > sizeof(ap->name))
2756 ap->namelen = sizeof(ap->name);
2757 memcpy(ap->name, ws_dat.wi_name, ap->namelen);
2761 sc->sc_scan_timer = 0;
2762 DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
2767 wi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi)
2769 ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr),
2770 ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL : -1, rssi);
2771 printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n",
2772 le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1),
2773 le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence);
2774 printf(" rx_signal %u rx_rate %u rx_flow %u\n",
2775 wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow);
2776 printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n",
2777 wh->wi_tx_rtry, wh->wi_tx_rate,
2778 le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len));
2779 printf(" ehdr dst %6D src %6D type 0x%x\n",
2780 wh->wi_ehdr.ether_dhost, ":", wh->wi_ehdr.ether_shost, ":",
2781 wh->wi_ehdr.ether_type);
2785 wi_alloc(device_t dev, int rid)
2787 struct wi_softc *sc = device_get_softc(dev);
2789 if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2790 sc->iobase_rid = rid;
2791 sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2792 &sc->iobase_rid, 0, ~0, (1 << 6),
2793 rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2795 device_printf(dev, "No I/O space?!\n");
2799 sc->wi_io_addr = rman_get_start(sc->iobase);
2800 sc->wi_btag = rman_get_bustag(sc->iobase);
2801 sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2804 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2805 &sc->mem_rid, RF_ACTIVE);
2808 device_printf(dev, "No Mem space on prism2.5?\n");
2812 sc->wi_btag = rman_get_bustag(sc->mem);
2813 sc->wi_bhandle = rman_get_bushandle(sc->mem);
2818 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2820 ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2824 device_printf(dev, "No irq?!\n");
2829 sc->sc_unit = device_get_unit(dev);
2835 wi_free(device_t dev)
2837 struct wi_softc *sc = device_get_softc(dev);
2839 if (sc->iobase != NULL) {
2840 bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2843 if (sc->irq != NULL) {
2844 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2847 if (sc->mem != NULL) {
2848 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
2856 wi_get_debug(struct wi_softc *sc, struct wi_req *wreq)
2862 switch (wreq->wi_type) {
2863 case WI_DEBUG_SLEEP:
2865 wreq->wi_val[0] = sc->wi_debug.wi_sleep;
2867 case WI_DEBUG_DELAYSUPP:
2869 wreq->wi_val[0] = sc->wi_debug.wi_delaysupp;
2871 case WI_DEBUG_TXSUPP:
2873 wreq->wi_val[0] = sc->wi_debug.wi_txsupp;
2875 case WI_DEBUG_MONITOR:
2877 wreq->wi_val[0] = sc->wi_debug.wi_monitor;
2879 case WI_DEBUG_LEDTEST:
2881 wreq->wi_val[0] = sc->wi_debug.wi_ledtest;
2882 wreq->wi_val[1] = sc->wi_debug.wi_ledtest_param0;
2883 wreq->wi_val[2] = sc->wi_debug.wi_ledtest_param1;
2885 case WI_DEBUG_CONTTX:
2887 wreq->wi_val[0] = sc->wi_debug.wi_conttx;
2888 wreq->wi_val[1] = sc->wi_debug.wi_conttx_param0;
2890 case WI_DEBUG_CONTRX:
2892 wreq->wi_val[0] = sc->wi_debug.wi_contrx;
2894 case WI_DEBUG_SIGSTATE:
2896 wreq->wi_val[0] = sc->wi_debug.wi_sigstate;
2897 wreq->wi_val[1] = sc->wi_debug.wi_sigstate_param0;
2899 case WI_DEBUG_CONFBITS:
2901 wreq->wi_val[0] = sc->wi_debug.wi_confbits;
2902 wreq->wi_val[1] = sc->wi_debug.wi_confbits_param0;
2913 wi_set_debug(struct wi_softc *sc, struct wi_req *wreq)
2916 u_int16_t cmd, param0 = 0, param1 = 0;
2918 switch (wreq->wi_type) {
2919 case WI_DEBUG_RESET:
2921 case WI_DEBUG_CALENABLE:
2923 case WI_DEBUG_SLEEP:
2924 sc->wi_debug.wi_sleep = 1;
2927 sc->wi_debug.wi_sleep = 0;
2930 param0 = wreq->wi_val[0];
2932 case WI_DEBUG_DELAYSUPP:
2933 sc->wi_debug.wi_delaysupp = 1;
2935 case WI_DEBUG_TXSUPP:
2936 sc->wi_debug.wi_txsupp = 1;
2938 case WI_DEBUG_MONITOR:
2939 sc->wi_debug.wi_monitor = 1;
2941 case WI_DEBUG_LEDTEST:
2942 param0 = wreq->wi_val[0];
2943 param1 = wreq->wi_val[1];
2944 sc->wi_debug.wi_ledtest = 1;
2945 sc->wi_debug.wi_ledtest_param0 = param0;
2946 sc->wi_debug.wi_ledtest_param1 = param1;
2948 case WI_DEBUG_CONTTX:
2949 param0 = wreq->wi_val[0];
2950 sc->wi_debug.wi_conttx = 1;
2951 sc->wi_debug.wi_conttx_param0 = param0;
2953 case WI_DEBUG_STOPTEST:
2954 sc->wi_debug.wi_delaysupp = 0;
2955 sc->wi_debug.wi_txsupp = 0;
2956 sc->wi_debug.wi_monitor = 0;
2957 sc->wi_debug.wi_ledtest = 0;
2958 sc->wi_debug.wi_ledtest_param0 = 0;
2959 sc->wi_debug.wi_ledtest_param1 = 0;
2960 sc->wi_debug.wi_conttx = 0;
2961 sc->wi_debug.wi_conttx_param0 = 0;
2962 sc->wi_debug.wi_contrx = 0;
2963 sc->wi_debug.wi_sigstate = 0;
2964 sc->wi_debug.wi_sigstate_param0 = 0;
2966 case WI_DEBUG_CONTRX:
2967 sc->wi_debug.wi_contrx = 1;
2969 case WI_DEBUG_SIGSTATE:
2970 param0 = wreq->wi_val[0];
2971 sc->wi_debug.wi_sigstate = 1;
2972 sc->wi_debug.wi_sigstate_param0 = param0;
2974 case WI_DEBUG_CONFBITS:
2975 param0 = wreq->wi_val[0];
2976 param1 = wreq->wi_val[1];
2977 sc->wi_debug.wi_confbits = param0;
2978 sc->wi_debug.wi_confbits_param0 = param1;
2988 cmd = WI_CMD_DEBUG | (wreq->wi_type << 8);
2989 error = wi_cmd(sc, cmd, param0, param1, 0);
2995 * Special routines to download firmware for Symbol CF card.
2996 * XXX: This should be modified generic into any PRISM-2 based card.
2999 #define WI_SBCF_PDIADDR 0x3100
3001 /* unaligned load little endian */
3002 #define GETLE32(p) ((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24))
3003 #define GETLE16(p) ((p)[0] | ((p)[1]<<8))
3006 wi_symbol_load_firm(struct wi_softc *sc, const void *primsym, int primlen,
3007 const void *secsym, int seclen)
3012 /* load primary code and run it */
3013 wi_symbol_set_hcr(sc, WI_HCR_EEHOLD);
3014 if (wi_symbol_write_firm(sc, primsym, primlen, NULL, 0))
3016 wi_symbol_set_hcr(sc, WI_HCR_RUN);
3017 for (i = 0; ; i++) {
3020 tsleep(sc, 0, "wiinit", 1);
3021 if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT)
3023 /* write the magic key value to unlock aux port */
3024 CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0);
3025 CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1);
3026 CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2);
3027 CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL);
3030 /* issue read EEPROM command: XXX copied from wi_cmd() */
3031 CSR_WRITE_2(sc, WI_PARAM0, 0);
3032 CSR_WRITE_2(sc, WI_PARAM1, 0);
3033 CSR_WRITE_2(sc, WI_PARAM2, 0);
3034 CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE);
3035 for (i = 0; i < WI_TIMEOUT; i++) {
3036 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
3040 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
3042 CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ);
3043 CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ);
3044 CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA,
3045 (uint16_t *)ebuf, sizeof(ebuf) / 2);
3046 if (GETLE16(ebuf) > sizeof(ebuf))
3048 if (wi_symbol_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf)))
3054 wi_symbol_write_firm(struct wi_softc *sc, const void *buf, int buflen,
3055 const void *ebuf, int ebuflen)
3057 const uint8_t *p, *ep, *q, *eq;
3059 uint32_t addr, id, eid;
3060 int i, len, elen, nblk, pdrlen;
3063 * Parse the header of the firmware image.
3067 while (p < ep && *p++ != ' '); /* FILE: */
3068 while (p < ep && *p++ != ' '); /* filename */
3069 while (p < ep && *p++ != ' '); /* type of the firmware */
3070 nblk = strtoul(p, &tp, 10);
3072 pdrlen = strtoul(p + 1, &tp, 10);
3074 while (p < ep && *p++ != 0x1a); /* skip rest of header */
3077 * Block records: address[4], length[2], data[length];
3079 for (i = 0; i < nblk; i++) {
3080 addr = GETLE32(p); p += 4;
3081 len = GETLE16(p); p += 2;
3082 CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3083 CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3084 CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3085 (const uint16_t *)p, len / 2);
3090 * PDR: id[4], address[4], length[4];
3092 for (i = 0; i < pdrlen; ) {
3093 id = GETLE32(p); p += 4; i += 4;
3094 addr = GETLE32(p); p += 4; i += 4;
3095 len = GETLE32(p); p += 4; i += 4;
3096 /* replace PDR entry with the values from EEPROM, if any */
3097 for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) {
3098 elen = GETLE16(q); q += 2;
3099 eid = GETLE16(q); q += 2;
3100 elen--; /* elen includes eid */
3105 CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3106 CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3107 CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3108 (const uint16_t *)q, len / 2);
3116 wi_symbol_set_hcr(struct wi_softc *sc, int mode)
3120 CSR_WRITE_2(sc, WI_COR, WI_COR_RESET);
3121 tsleep(sc, 0, "wiinit", 1);
3122 hcr = CSR_READ_2(sc, WI_HCR);
3123 hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE);
3124 CSR_WRITE_2(sc, WI_HCR, hcr);
3125 tsleep(sc, 0, "wiinit", 1);
3126 CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE);
3127 tsleep(sc, 0, "wiinit", 1);