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.17 2005/01/14 02:35:09 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);
469 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
470 sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
473 * Initialize constant fields.
474 * XXX make header lengths a multiple of 32-bits so subsequent
475 * headers are properly aligned; this is a kludge to keep
476 * certain applications happy.
478 * NB: the channel is setup each time we transition to the
479 * RUN state to avoid filling it in for each frame.
481 sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t));
482 sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len);
483 sc->sc_tx_th.wt_ihdr.it_present = htole32(WI_TX_RADIOTAP_PRESENT);
485 sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t));
486 sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len);
487 sc->sc_rx_th.wr_ihdr.it_present = htole32(WI_RX_RADIOTAP_PRESENT);
493 wi_detach(device_t dev)
495 struct wi_softc *sc = device_get_softc(dev);
496 struct ifnet *ifp = &sc->sc_ic.ic_if;
501 /* check if device was removed */
502 sc->wi_gone |= !bus_child_present(dev);
506 ieee80211_ifdetach(ifp);
508 bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
514 wi_shutdown(device_t dev)
516 struct wi_softc *sc = device_get_softc(dev);
518 wi_stop(&sc->sc_if, 1);
521 #ifdef DEVICE_POLLING
523 wi_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
525 struct wi_softc *sc = ifp->if_softc;
528 if ((ifp->if_capenable & IFCAP_POLLING) == 0) {
529 ether_poll_deregister(ifp);
530 cmd = POLL_DEREGISTER;
532 if (cmd == POLL_DEREGISTER) {
533 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
537 status = CSR_READ_2(sc, WI_EVENT_STAT);
539 if (status & WI_EV_RX)
541 if (status & WI_EV_ALLOC)
543 if (status & WI_EV_INFO)
546 if (cmd == POLL_AND_CHECK_STATUS) {
547 if (status & WI_EV_INFO)
551 if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
552 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 &&
553 IF_QLEN(&ifp->if_snd) != NULL)
556 #endif /* DEVICE_POLLING */
561 struct wi_softc *sc = arg;
562 struct ifnet *ifp = &sc->sc_ic.ic_if;
566 #ifdef DEVICE_POLLING
567 if (ifp->if_flags & IFF_POLLING)
569 if ((ifp->if_capenable & IFCAP_POLLING) &&
570 (ether_poll_register(wi_poll, ifp))) {
571 CSR_WRITE_2(sc, WI_INT_EN, 0);
575 #endif DEVICE_POLLING
577 if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) {
578 CSR_WRITE_2(sc, WI_INT_EN, 0);
579 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
585 /* Disable interrupts. */
586 CSR_WRITE_2(sc, WI_INT_EN, 0);
588 status = CSR_READ_2(sc, WI_EVENT_STAT);
589 if (status & WI_EV_RX)
591 if (status & WI_EV_ALLOC)
593 if (status & WI_EV_TX_EXC)
595 if (status & WI_EV_INFO)
597 if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
598 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 &&
599 IF_QLEN(&ifp->if_snd) != 0)
602 /* Re-enable interrupts. */
603 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
613 struct wi_softc *sc = arg;
614 struct ifnet *ifp = &sc->sc_if;
615 struct ieee80211com *ic = &sc->sc_ic;
616 struct wi_joinreq join;
618 int error = 0, wasenabled;
620 struct sockaddr_dl *sdl;
630 if ((wasenabled = sc->sc_enabled))
634 /* common 802.11 configuration */
635 ic->ic_flags &= ~IEEE80211_F_IBSSON;
636 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
637 switch (ic->ic_opmode) {
638 case IEEE80211_M_STA:
639 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS);
641 case IEEE80211_M_IBSS:
642 wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port);
643 ic->ic_flags |= IEEE80211_F_IBSSON;
645 case IEEE80211_M_AHDEMO:
646 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
648 case IEEE80211_M_HOSTAP:
650 * For PRISM cards, override the empty SSID, because in
651 * HostAP mode the controller will lock up otherwise.
653 if (sc->sc_firmware_type == WI_INTERSIL &&
654 ic->ic_des_esslen == 0) {
655 ic->ic_des_essid[0] = ' ';
656 ic->ic_des_esslen = 1;
658 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP);
660 case IEEE80211_M_MONITOR:
661 if (sc->sc_firmware_type == WI_LUCENT)
662 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
663 wi_cmd(sc, WI_CMD_DEBUG | (WI_TEST_MONITOR << 8), 0, 0, 0);
667 /* Intersil interprets this RID as joining ESS even in IBSS mode */
668 if (sc->sc_firmware_type == WI_LUCENT &&
669 (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0)
670 wi_write_val(sc, WI_RID_CREATE_IBSS, 1);
672 wi_write_val(sc, WI_RID_CREATE_IBSS, 0);
673 wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
674 wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid,
676 wi_write_val(sc, WI_RID_OWN_CHNL,
677 ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
678 wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen);
680 ifa = ifaddr_byindex(ifp->if_index);
681 sdl = (struct sockaddr_dl *) ifa->ifa_addr;
682 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(sdl));
683 wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN);
685 wi_write_val(sc, WI_RID_PM_ENABLED,
686 (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
688 /* not yet common 802.11 configuration */
689 wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen);
690 wi_write_val(sc, WI_RID_RTS_THRESH, ic->ic_rtsthreshold);
691 if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
692 wi_write_val(sc, WI_RID_FRAG_THRESH, ic->ic_fragthreshold);
694 /* driver specific 802.11 configuration */
695 if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)
696 wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale);
697 if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
698 wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode);
699 if (sc->sc_flags & WI_FLAGS_HAS_MOR)
700 wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven);
702 wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen);
704 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
705 sc->sc_firmware_type == WI_INTERSIL) {
706 wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval);
707 wi_write_val(sc, WI_RID_BASIC_RATE, 0x03); /* 1, 2 */
708 wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */
709 wi_write_val(sc, WI_RID_DTIM_PERIOD, 1);
713 * Initialize promisc mode.
714 * Being in the Host-AP mode causes a great
715 * deal of pain if primisc mode is set.
716 * Therefore we avoid confusing the firmware
717 * and always reset promisc mode in Host-AP
718 * mode. Host-AP sees all the packets anyway.
720 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
721 (ifp->if_flags & IFF_PROMISC) != 0) {
722 wi_write_val(sc, WI_RID_PROMISC, 1);
724 wi_write_val(sc, WI_RID_PROMISC, 0);
728 if (ic->ic_caps & IEEE80211_C_WEP)
731 /* Set multicast filter. */
734 /* Allocate fids for the card */
735 if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
736 sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
737 if (sc->sc_firmware_type == WI_SYMBOL)
738 sc->sc_buflen = 1585; /* XXX */
739 for (i = 0; i < sc->sc_ntxbuf; i++) {
740 error = wi_alloc_fid(sc, sc->sc_buflen,
741 &sc->sc_txd[i].d_fid);
743 device_printf(sc->sc_dev,
744 "tx buffer allocation failed (error %u)\n",
748 sc->sc_txd[i].d_len = 0;
751 sc->sc_txcur = sc->sc_txnext = 0;
753 /* Enable desired port */
754 wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
757 ifp->if_flags |= IFF_RUNNING;
758 ifp->if_flags &= ~IFF_OACTIVE;
759 if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
760 ic->ic_opmode == IEEE80211_M_MONITOR ||
761 ic->ic_opmode == IEEE80211_M_HOSTAP)
762 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
764 /* Enable interrupts if not polling */
765 #ifdef DEVICE_POLLING
766 if ((ifp->if_flags & IFF_POLLING) == 0)
768 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
771 ic->ic_opmode == IEEE80211_M_HOSTAP &&
772 sc->sc_firmware_type == WI_INTERSIL) {
773 /* XXX: some card need to be re-enabled for hostap */
774 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
775 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
778 if (ic->ic_opmode == IEEE80211_M_STA &&
779 ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
780 ic->ic_des_chan != IEEE80211_CHAN_ANYC)) {
781 memset(&join, 0, sizeof(join));
782 if (ic->ic_flags & IEEE80211_F_DESBSSID)
783 IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
784 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC)
785 join.wi_chan = htole16(
786 ieee80211_chan2ieee(ic, ic->ic_des_chan));
787 /* Lucent firmware does not support the JOIN RID. */
788 if (sc->sc_firmware_type != WI_LUCENT)
789 wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
796 if_printf(ifp, "interface not running\n");
800 DPRINTF(("wi_init: return %d\n", error));
805 wi_stop(struct ifnet *ifp, int disable)
807 struct ieee80211com *ic = (struct ieee80211com *) ifp;
808 struct wi_softc *sc = ifp->if_softc;
815 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
816 if (sc->sc_enabled && !sc->wi_gone) {
817 CSR_WRITE_2(sc, WI_INT_EN, 0);
818 wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
822 (*sc->sc_disable)(sc);
826 } else if (sc->wi_gone && disable) /* gone --> not enabled */
830 sc->sc_scan_timer = 0;
831 sc->sc_syn_timer = 0;
832 sc->sc_false_syns = 0;
834 ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
835 #ifdef DEVICE_POLLING
836 ether_poll_deregister(ifp);
844 wi_start(struct ifnet *ifp)
846 struct wi_softc *sc = ifp->if_softc;
847 struct ieee80211com *ic = &sc->sc_ic;
848 struct ieee80211_node *ni;
849 struct ieee80211_frame *wh;
851 struct wi_frame frmhdr;
852 int cur, fid, off, error;
861 if (sc->sc_flags & WI_FLAGS_OUTRANGE) {
866 memset(&frmhdr, 0, sizeof(frmhdr));
869 IF_POLL(&ic->ic_mgtq, m0);
871 if (sc->sc_txd[cur].d_len != 0) {
872 ifp->if_flags |= IFF_OACTIVE;
875 IF_DEQUEUE(&ic->ic_mgtq, m0);
877 * Hack! The referenced node pointer is in the
878 * rcvif field of the packet header. This is
879 * placed there by ieee80211_mgmt_output because
880 * we need to hold the reference with the frame
881 * and there's no other way (other than packet
882 * tags which we consider too expensive to use)
885 ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
886 m0->m_pkthdr.rcvif = NULL;
888 m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
889 (caddr_t)&frmhdr.wi_ehdr);
890 frmhdr.wi_ehdr.ether_type = 0;
891 wh = mtod(m0, struct ieee80211_frame *);
893 if (ic->ic_state != IEEE80211_S_RUN)
895 IFQ_POLL(&ifp->if_snd, m0);
898 if (sc->sc_txd[cur].d_len != 0) {
899 ifp->if_flags |= IFF_OACTIVE;
902 IFQ_DEQUEUE(&ifp->if_snd, m0);
904 m_copydata(m0, 0, ETHER_HDR_LEN,
905 (caddr_t)&frmhdr.wi_ehdr);
908 m0 = ieee80211_encap(ifp, m0, &ni);
913 wh = mtod(m0, struct ieee80211_frame *);
914 if (ic->ic_flags & IEEE80211_F_WEPON)
915 wh->i_fc[1] |= IEEE80211_FC1_WEP;
918 #ifdef IEEE80211_RAWBPF
920 bpf_mtap(ic->ic_rawbpf, m0);
922 frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
923 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
924 (wh->i_fc[1] & IEEE80211_FC1_WEP)) {
925 if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) {
927 if (ni && ni != ic->ic_bss)
928 ieee80211_free_node(ic, ni);
931 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
935 sc->sc_tx_th.wt_rate =
936 ni->ni_rates.rs_rates[ni->ni_txrate];
937 bpf_mtap2(sc->sc_drvbpf,
938 &sc->sc_tx_th, sc->sc_tx_th_len, m0);
941 m_copydata(m0, 0, sizeof(struct ieee80211_frame),
942 (caddr_t)&frmhdr.wi_whdr);
943 m_adj(m0, sizeof(struct ieee80211_frame));
944 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
945 if (IFF_DUMPPKTS(ifp))
946 wi_dump_pkt(&frmhdr, NULL, -1);
947 fid = sc->sc_txd[cur].d_fid;
948 off = sizeof(frmhdr);
949 error = wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0
950 || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
952 if (ni && ni != ic->ic_bss)
953 ieee80211_free_node(ic, ni);
958 sc->sc_txd[cur].d_len = off;
959 if (sc->sc_txcur == cur) {
960 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
961 if_printf(ifp, "xmit failed\n");
962 sc->sc_txd[cur].d_len = 0;
968 sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
975 wi_reset(struct wi_softc *sc)
977 struct ieee80211com *ic = &sc->sc_ic;
978 struct ifnet *ifp = &ic->ic_if;
979 #define WI_INIT_TRIES 3
984 /* Symbol firmware cannot be initialized more than once */
985 if (sc->sc_firmware_type == WI_SYMBOL && sc->sc_reset)
987 if (sc->sc_firmware_type == WI_SYMBOL)
990 tries = WI_INIT_TRIES;
992 for (i = 0; i < tries; i++) {
993 if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
995 DELAY(WI_DELAY * 1000);
1000 if_printf(ifp, "init failed\n");
1004 CSR_WRITE_2(sc, WI_INT_EN, 0);
1005 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
1007 /* Calibrate timer. */
1008 wi_write_val(sc, WI_RID_TICK_TIME, 8);
1011 #undef WI_INIT_TRIES
1015 wi_watchdog(struct ifnet *ifp)
1017 struct wi_softc *sc = ifp->if_softc;
1020 if (!sc->sc_enabled)
1023 if (sc->sc_tx_timer) {
1024 if (--sc->sc_tx_timer == 0) {
1025 if_printf(ifp, "device timeout\n");
1027 wi_init(ifp->if_softc);
1033 if (sc->sc_scan_timer) {
1034 if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
1035 sc->sc_firmware_type == WI_INTERSIL) {
1036 DPRINTF(("wi_watchdog: inquire scan\n"));
1037 wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
1039 if (sc->sc_scan_timer)
1043 if (sc->sc_syn_timer) {
1044 if (--sc->sc_syn_timer == 0) {
1045 struct ieee80211com *ic = (struct ieee80211com *) ifp;
1046 DPRINTF2(("wi_watchdog: %d false syns\n",
1047 sc->sc_false_syns));
1048 sc->sc_false_syns = 0;
1049 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1050 sc->sc_syn_timer = 5;
1055 /* TODO: rate control */
1056 ieee80211_watchdog(ifp);
1060 wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1062 struct wi_softc *sc = ifp->if_softc;
1063 struct ieee80211com *ic = &sc->sc_ic;
1064 struct ifreq *ifr = (struct ifreq *)data;
1065 struct ieee80211req *ireq;
1066 u_int8_t nodename[IEEE80211_NWID_LEN];
1081 * Can't do promisc and hostap at the same time. If all that's
1082 * changing is the promisc flag, try to short-circuit a call to
1083 * wi_init() by just setting PROMISC in the hardware.
1085 if (ifp->if_flags & IFF_UP) {
1086 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1087 ifp->if_flags & IFF_RUNNING) {
1088 if (ifp->if_flags & IFF_PROMISC &&
1089 !(sc->sc_if_flags & IFF_PROMISC)) {
1090 wi_write_val(sc, WI_RID_PROMISC, 1);
1091 } else if (!(ifp->if_flags & IFF_PROMISC) &&
1092 sc->sc_if_flags & IFF_PROMISC) {
1093 wi_write_val(sc, WI_RID_PROMISC, 0);
1101 if (ifp->if_flags & IFF_RUNNING) {
1106 sc->sc_if_flags = ifp->if_flags;
1111 error = wi_write_multi(sc);
1113 case SIOCGIFGENERIC:
1114 error = wi_get_cfg(ifp, cmd, data, cr);
1116 case SIOCSIFGENERIC:
1117 error = suser_cred(cr, NULL_CRED_OKAY);
1120 error = wi_set_cfg(ifp, cmd, data);
1122 case SIOCGPRISM2DEBUG:
1123 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1126 if (!(ifp->if_flags & IFF_RUNNING) ||
1127 sc->sc_firmware_type == WI_LUCENT) {
1131 error = wi_get_debug(sc, &wreq);
1133 error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
1135 case SIOCSPRISM2DEBUG:
1136 if ((error = suser_cred(cr, NULL_CRED_OKAY)))
1138 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1141 error = wi_set_debug(sc, &wreq);
1144 ireq = (struct ieee80211req *) data;
1145 switch (ireq->i_type) {
1146 case IEEE80211_IOC_STATIONNAME:
1147 ireq->i_len = sc->sc_nodelen + 1;
1148 error = copyout(sc->sc_nodename, ireq->i_data,
1152 error = ieee80211_ioctl(ifp, cmd, data, cr);
1157 error = suser_cred(cr, NULL_CRED_OKAY);
1160 ireq = (struct ieee80211req *) data;
1161 switch (ireq->i_type) {
1162 case IEEE80211_IOC_STATIONNAME:
1163 if (ireq->i_val != 0 ||
1164 ireq->i_len > IEEE80211_NWID_LEN) {
1168 memset(nodename, 0, IEEE80211_NWID_LEN);
1169 error = copyin(ireq->i_data, nodename, ireq->i_len);
1172 if (sc->sc_enabled) {
1173 error = wi_write_ssid(sc, WI_RID_NODENAME,
1174 nodename, ireq->i_len);
1178 memcpy(sc->sc_nodename, nodename, IEEE80211_NWID_LEN);
1179 sc->sc_nodelen = ireq->i_len;
1182 error = ieee80211_ioctl(ifp, cmd, data, cr);
1187 ifp->if_capenable &= ~(IFCAP_POLLING);
1188 ifp->if_capenable |= ifr->ifr_reqcap & (IFCAP_POLLING);
1189 if (ifp->if_flags & IFF_RUNNING)
1193 error = ieee80211_ioctl(ifp, cmd, data, cr);
1196 if (error == ENETRESET) {
1198 wi_init(sc); /* XXX no error return */
1208 wi_media_change(struct ifnet *ifp)
1210 struct wi_softc *sc = ifp->if_softc;
1213 error = ieee80211_media_change(ifp);
1214 if (error == ENETRESET) {
1216 wi_init(sc); /* XXX no error return */
1223 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1225 struct wi_softc *sc = ifp->if_softc;
1226 struct ieee80211com *ic = &sc->sc_ic;
1230 if (sc->wi_gone || !sc->sc_enabled) {
1231 imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
1232 imr->ifm_status = 0;
1236 imr->ifm_status = IFM_AVALID;
1237 imr->ifm_active = IFM_IEEE80211;
1238 if (ic->ic_state == IEEE80211_S_RUN &&
1239 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
1240 imr->ifm_status |= IFM_ACTIVE;
1242 if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) != 0)
1245 /* convert to 802.11 rate */
1247 if (sc->sc_firmware_type == WI_LUCENT) {
1249 rate = 11; /* 5.5Mbps */
1250 else if (rate == 5 * 2)
1251 rate = 22; /* 11Mbps */
1254 rate = 11; /* 5.5Mbps */
1255 else if (rate == 8*2)
1256 rate = 22; /* 11Mbps */
1259 imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
1260 switch (ic->ic_opmode) {
1261 case IEEE80211_M_STA:
1263 case IEEE80211_M_IBSS:
1264 imr->ifm_active |= IFM_IEEE80211_ADHOC;
1266 case IEEE80211_M_AHDEMO:
1267 imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1269 case IEEE80211_M_HOSTAP:
1270 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1272 case IEEE80211_M_MONITOR:
1273 imr->ifm_active |= IFM_IEEE80211_MONITOR;
1279 wi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1281 struct ieee80211com *ic = &sc->sc_ic;
1282 struct ieee80211_node *ni = ic->ic_bss;
1283 struct ifnet *ifp = &ic->ic_if;
1285 if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1288 DPRINTF(("wi_sync_bssid: bssid %6D -> ", ni->ni_bssid, ":"));
1289 DPRINTF(("%6D ?\n", new_bssid, ":"));
1291 /* In promiscuous mode, the BSSID field is not a reliable
1292 * indicator of the firmware's BSSID. Damp spurious
1293 * change-of-BSSID indications.
1295 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1296 sc->sc_false_syns >= WI_MAX_FALSE_SYNS)
1299 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1303 wi_rx_monitor(struct wi_softc *sc, int fid)
1305 struct ieee80211com *ic = &sc->sc_ic;
1306 struct ifnet *ifp = &ic->ic_if;
1307 struct wi_frame *rx_frame;
1311 /* first allocate mbuf for packet storage */
1312 m = m_getcl(MB_DONTWAIT, MT_DATA, 0);
1318 m->m_pkthdr.rcvif = ifp;
1320 /* now read wi_frame first so we know how much data to read */
1321 if (wi_read_bap(sc, fid, 0, mtod(m, caddr_t), sizeof(*rx_frame))) {
1326 rx_frame = mtod(m, struct wi_frame *);
1328 switch ((rx_frame->wi_status & WI_STAT_MAC_PORT) >> 8) {
1330 switch (rx_frame->wi_whdr.i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1331 case IEEE80211_FC0_TYPE_DATA:
1332 hdrlen = WI_DATA_HDRLEN;
1333 datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1335 case IEEE80211_FC0_TYPE_MGT:
1336 hdrlen = WI_MGMT_HDRLEN;
1337 datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1339 case IEEE80211_FC0_TYPE_CTL:
1341 * prism2 cards don't pass control packets
1342 * down properly or consistently, so we'll only
1343 * pass down the header.
1345 hdrlen = WI_CTL_HDRLEN;
1349 if_printf(ifp, "received packet of unknown type "
1356 hdrlen = WI_DATA_HDRLEN;
1357 datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1360 if_printf(ifp, "received packet on invalid "
1361 "port (wi_status=0x%x)\n", rx_frame->wi_status);
1366 if (hdrlen + datlen + 2 > MCLBYTES) {
1367 if_printf(ifp, "oversized packet received "
1368 "(wi_dat_len=%d, wi_status=0x%x)\n",
1369 datlen, rx_frame->wi_status);
1374 if (wi_read_bap(sc, fid, hdrlen, mtod(m, caddr_t) + hdrlen,
1376 m->m_pkthdr.len = m->m_len = hdrlen + datlen;
1378 BPF_MTAP(ifp, m); /* Handle BPF listeners. */
1386 wi_rx_intr(struct wi_softc *sc)
1388 struct ieee80211com *ic = &sc->sc_ic;
1389 struct ifnet *ifp = &ic->ic_if;
1390 struct wi_frame frmhdr;
1392 struct ieee80211_frame *wh;
1393 struct ieee80211_node *ni;
1394 int fid, len, off, rssi;
1399 fid = CSR_READ_2(sc, WI_RX_FID);
1401 if (sc->wi_debug.wi_monitor) {
1403 * If we are in monitor mode just
1404 * read the data from the device.
1406 wi_rx_monitor(sc, fid);
1407 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1411 /* First read in the frame header */
1412 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1413 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1415 DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1419 if (IFF_DUMPPKTS(ifp))
1420 wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal);
1423 * Drop undecryptable or packets with receive errors here
1425 status = le16toh(frmhdr.wi_status);
1426 if (status & WI_STAT_ERRSTAT) {
1427 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1429 DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1432 rssi = frmhdr.wi_rx_signal;
1433 rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1434 le16toh(frmhdr.wi_rx_tstamp1);
1436 len = le16toh(frmhdr.wi_dat_len);
1437 off = ALIGN(sizeof(struct ieee80211_frame));
1440 * Sometimes the PRISM2.x returns bogusly large frames. Except
1441 * in monitor mode, just throw them away.
1443 if (off + len > MCLBYTES) {
1444 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1445 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1447 DPRINTF(("wi_rx_intr: oversized packet\n"));
1453 MGETHDR(m, MB_DONTWAIT, MT_DATA);
1455 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1457 DPRINTF(("wi_rx_intr: MGET failed\n"));
1460 if (off + len > MHLEN) {
1461 MCLGET(m, MB_DONTWAIT);
1462 if ((m->m_flags & M_EXT) == 0) {
1463 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1466 DPRINTF(("wi_rx_intr: MCLGET failed\n"));
1471 m->m_data += off - sizeof(struct ieee80211_frame);
1472 memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1473 wi_read_bap(sc, fid, sizeof(frmhdr),
1474 m->m_data + sizeof(struct ieee80211_frame), len);
1475 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1476 m->m_pkthdr.rcvif = ifp;
1478 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1481 if (sc->sc_drvbpf) {
1482 /* XXX replace divide by table */
1483 sc->sc_rx_th.wr_rate = frmhdr.wi_rx_rate / 5;
1484 sc->sc_rx_th.wr_antsignal = frmhdr.wi_rx_signal;
1485 sc->sc_rx_th.wr_antnoise = frmhdr.wi_rx_silence;
1486 sc->sc_rx_th.wr_flags = 0;
1487 if (frmhdr.wi_status & WI_STAT_PCF)
1488 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_CFP;
1489 bpf_mtap2(sc->sc_drvbpf,
1490 &sc->sc_rx_th, sc->sc_rx_th_len, m);
1493 wh = mtod(m, struct ieee80211_frame *);
1494 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1496 * WEP is decrypted by hardware. Clear WEP bit
1497 * header for ieee80211_input().
1499 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1502 /* synchronize driver's BSSID with firmware's BSSID */
1503 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1504 if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1505 wi_sync_bssid(sc, wh->i_addr3);
1508 * Locate the node for sender, track state, and
1509 * then pass this node (referenced) up to the 802.11
1510 * layer for its use. We are required to pass
1511 * something so we fallback to ic_bss when this frame
1512 * is from an unknown sender.
1514 if (ic->ic_opmode != IEEE80211_M_STA) {
1515 ni = ieee80211_find_node(ic, wh->i_addr2);
1517 ni = ieee80211_ref_node(ic->ic_bss);
1519 ni = ieee80211_ref_node(ic->ic_bss);
1521 * Send frame up for processing.
1523 ieee80211_input(ifp, m, ni, rssi, rstamp);
1525 * The frame may have caused the node to be marked for
1526 * reclamation (e.g. in response to a DEAUTH message)
1527 * so use free_node here instead of unref_node.
1529 if (ni == ic->ic_bss)
1530 ieee80211_unref_node(&ni);
1532 ieee80211_free_node(ic, ni);
1536 wi_tx_ex_intr(struct wi_softc *sc)
1538 struct ieee80211com *ic = &sc->sc_ic;
1539 struct ifnet *ifp = &ic->ic_if;
1540 struct wi_frame frmhdr;
1543 fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1544 /* Read in the frame header */
1545 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1546 u_int16_t status = le16toh(frmhdr.wi_status);
1549 * Spontaneous station disconnects appear as xmit
1550 * errors. Don't announce them and/or count them
1551 * as an output error.
1553 if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1554 if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1555 if_printf(ifp, "tx failed");
1556 if (status & WI_TXSTAT_RET_ERR)
1557 printf(", retry limit exceeded");
1558 if (status & WI_TXSTAT_AGED_ERR)
1559 printf(", max transmit lifetime exceeded");
1560 if (status & WI_TXSTAT_DISCONNECT)
1561 printf(", port disconnected");
1562 if (status & WI_TXSTAT_FORM_ERR)
1563 printf(", invalid format (data len %u src %6D)",
1564 le16toh(frmhdr.wi_dat_len),
1565 frmhdr.wi_ehdr.ether_shost, ":");
1567 printf(", status=0x%x", status);
1572 DPRINTF(("port disconnected\n"));
1573 ifp->if_collisions++; /* XXX */
1576 DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
1577 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1581 wi_tx_intr(struct wi_softc *sc)
1583 struct ieee80211com *ic = &sc->sc_ic;
1584 struct ifnet *ifp = &ic->ic_if;
1590 fid = CSR_READ_2(sc, WI_ALLOC_FID);
1591 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1594 if (sc->sc_txd[cur].d_fid != fid) {
1595 if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
1596 fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
1599 sc->sc_tx_timer = 0;
1600 sc->sc_txd[cur].d_len = 0;
1601 sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
1602 if (sc->sc_txd[cur].d_len == 0)
1603 ifp->if_flags &= ~IFF_OACTIVE;
1605 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1607 if_printf(ifp, "xmit failed\n");
1608 sc->sc_txd[cur].d_len = 0;
1610 sc->sc_tx_timer = 5;
1617 wi_info_intr(struct wi_softc *sc)
1619 struct ieee80211com *ic = &sc->sc_ic;
1620 struct ifnet *ifp = &ic->ic_if;
1621 int i, fid, len, off;
1626 fid = CSR_READ_2(sc, WI_INFO_FID);
1627 wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
1629 switch (le16toh(ltbuf[1])) {
1631 case WI_INFO_LINK_STAT:
1632 wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1633 DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1634 switch (le16toh(stat)) {
1635 case WI_INFO_LINK_STAT_CONNECTED:
1636 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1637 if (ic->ic_state == IEEE80211_S_RUN &&
1638 ic->ic_opmode != IEEE80211_M_IBSS)
1641 case WI_INFO_LINK_STAT_AP_CHG:
1642 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1644 case WI_INFO_LINK_STAT_AP_INR:
1645 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1647 case WI_INFO_LINK_STAT_AP_OOR:
1648 if (sc->sc_firmware_type == WI_SYMBOL &&
1649 sc->sc_scan_timer > 0) {
1650 if (wi_cmd(sc, WI_CMD_INQUIRE,
1651 WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
1652 sc->sc_scan_timer = 0;
1655 if (ic->ic_opmode == IEEE80211_M_STA)
1656 sc->sc_flags |= WI_FLAGS_OUTRANGE;
1658 case WI_INFO_LINK_STAT_DISCONNECTED:
1659 case WI_INFO_LINK_STAT_ASSOC_FAILED:
1660 if (ic->ic_opmode == IEEE80211_M_STA)
1661 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1666 case WI_INFO_COUNTERS:
1667 /* some card versions have a larger stats structure */
1668 len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1669 ptr = (u_int32_t *)&sc->sc_stats;
1670 off = sizeof(ltbuf);
1671 for (i = 0; i < len; i++, off += 2, ptr++) {
1672 wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1673 #ifdef WI_HERMES_STATS_WAR
1679 ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1680 sc->sc_stats.wi_tx_multi_retries +
1681 sc->sc_stats.wi_tx_retry_limit;
1684 case WI_INFO_SCAN_RESULTS:
1685 case WI_INFO_HOST_SCAN_RESULTS:
1686 wi_scan_result(sc, fid, le16toh(ltbuf[0]));
1690 DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1691 le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1694 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1698 wi_write_multi(struct wi_softc *sc)
1700 struct ifnet *ifp = &sc->sc_ic.ic_if;
1702 struct ifmultiaddr *ifma;
1703 struct wi_mcast mlist;
1705 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1707 memset(&mlist, 0, sizeof(mlist));
1708 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1713 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1714 if (ifma->ifma_addr->sa_family != AF_LINK)
1718 IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1719 (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1722 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1723 IEEE80211_ADDR_LEN * n);
1727 wi_read_nicid(struct wi_softc *sc)
1729 struct wi_card_ident *id;
1734 /* getting chip identity */
1735 memset(ver, 0, sizeof(ver));
1737 wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1738 device_printf(sc->sc_dev, "using ");
1740 sc->sc_firmware_type = WI_NOTYPE;
1741 for (id = wi_card_ident; id->card_name != NULL; id++) {
1742 if (le16toh(ver[0]) == id->card_id) {
1743 printf("%s", id->card_name);
1744 sc->sc_firmware_type = id->firm_type;
1748 if (sc->sc_firmware_type == WI_NOTYPE) {
1749 if (le16toh(ver[0]) & 0x8000) {
1750 printf("Unknown PRISM2 chip");
1751 sc->sc_firmware_type = WI_INTERSIL;
1753 printf("Unknown Lucent chip");
1754 sc->sc_firmware_type = WI_LUCENT;
1758 /* get primary firmware version (Only Prism chips) */
1759 if (sc->sc_firmware_type != WI_LUCENT) {
1760 memset(ver, 0, sizeof(ver));
1762 wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1763 sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1764 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1767 /* get station firmware version */
1768 memset(ver, 0, sizeof(ver));
1770 wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1771 sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1772 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1773 if (sc->sc_firmware_type == WI_INTERSIL &&
1774 (sc->sc_sta_firmware_ver == 10102 ||
1775 sc->sc_sta_firmware_ver == 20102)) {
1777 memset(ident, 0, sizeof(ident));
1778 len = sizeof(ident);
1779 /* value should be the format like "V2.00-11" */
1780 if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1781 *(p = (char *)ident) >= 'A' &&
1782 p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1783 sc->sc_firmware_type = WI_SYMBOL;
1784 sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1785 (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1786 (p[6] - '0') * 10 + (p[7] - '0');
1790 device_printf(sc->sc_dev, "%s Firmware: ",
1791 sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
1792 (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
1793 if (sc->sc_firmware_type != WI_LUCENT) /* XXX */
1794 printf("Primary (%u.%u.%u), ",
1795 sc->sc_pri_firmware_ver / 10000,
1796 (sc->sc_pri_firmware_ver % 10000) / 100,
1797 sc->sc_pri_firmware_ver % 100);
1798 printf("Station (%u.%u.%u)\n",
1799 sc->sc_sta_firmware_ver / 10000,
1800 (sc->sc_sta_firmware_ver % 10000) / 100,
1801 sc->sc_sta_firmware_ver % 100);
1805 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1807 struct wi_ssid ssid;
1809 if (buflen > IEEE80211_NWID_LEN)
1811 memset(&ssid, 0, sizeof(ssid));
1812 ssid.wi_len = htole16(buflen);
1813 memcpy(ssid.wi_ssid, buf, buflen);
1814 return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1818 wi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1820 struct wi_softc *sc = ifp->if_softc;
1821 struct ieee80211com *ic = &sc->sc_ic;
1822 struct ifreq *ifr = (struct ifreq *)data;
1824 struct wi_scan_res *res;
1826 int len, n, error, mif, val, off, i;
1828 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1831 len = (wreq.wi_len - 1) * 2;
1832 if (len < sizeof(u_int16_t))
1834 if (len > sizeof(wreq.wi_val))
1835 len = sizeof(wreq.wi_val);
1837 switch (wreq.wi_type) {
1839 case WI_RID_IFACE_STATS:
1840 memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
1841 if (len < sizeof(sc->sc_stats))
1844 len = sizeof(sc->sc_stats);
1847 case WI_RID_ENCRYPTION:
1848 case WI_RID_TX_CRYPT_KEY:
1849 case WI_RID_DEFLT_CRYPT_KEYS:
1850 case WI_RID_TX_RATE:
1851 return ieee80211_cfgget(ifp, cmd, data, cr);
1853 case WI_RID_MICROWAVE_OVEN:
1854 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
1855 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1859 wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
1860 len = sizeof(u_int16_t);
1863 case WI_RID_DBM_ADJUST:
1864 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
1865 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1869 wreq.wi_val[0] = htole16(sc->sc_dbm_offset);
1870 len = sizeof(u_int16_t);
1873 case WI_RID_ROAMING_MODE:
1874 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
1875 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1879 wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
1880 len = sizeof(u_int16_t);
1883 case WI_RID_SYSTEM_SCALE:
1884 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
1885 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1889 wreq.wi_val[0] = htole16(sc->sc_system_scale);
1890 len = sizeof(u_int16_t);
1893 case WI_RID_FRAG_THRESH:
1894 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
1895 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1899 wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
1900 len = sizeof(u_int16_t);
1903 case WI_RID_READ_APS:
1904 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1905 return ieee80211_cfgget(ifp, cmd, data, cr);
1906 if (sc->sc_scan_timer > 0) {
1907 error = EINPROGRESS;
1911 if (len < sizeof(n)) {
1915 if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
1916 n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
1917 len = sizeof(n) + sizeof(struct wi_apinfo) * n;
1918 memcpy(wreq.wi_val, &n, sizeof(n));
1919 memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
1920 sizeof(struct wi_apinfo) * n);
1924 wreq.wi_val[0] = sc->sc_firmware_type != WI_LUCENT;
1925 len = sizeof(u_int16_t);
1929 mif = wreq.wi_val[0];
1930 error = wi_cmd(sc, WI_CMD_READMIF, mif, 0, 0);
1931 val = CSR_READ_2(sc, WI_RESP0);
1932 wreq.wi_val[0] = val;
1933 len = sizeof(u_int16_t);
1936 case WI_RID_ZERO_CACHE:
1937 case WI_RID_PROCFRAME: /* ignore for compatibility */
1941 case WI_RID_READ_CACHE:
1942 return ieee80211_cfgget(ifp, cmd, data, cr);
1944 case WI_RID_SCAN_RES: /* compatibility interface */
1945 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1946 return ieee80211_cfgget(ifp, cmd, data, cr);
1947 if (sc->sc_scan_timer > 0) {
1948 error = EINPROGRESS;
1952 if (sc->sc_firmware_type == WI_LUCENT) {
1954 reslen = WI_WAVELAN_RES_SIZE;
1956 off = sizeof(struct wi_scan_p2_hdr);
1957 reslen = WI_PRISM2_RES_SIZE;
1959 if (len < off + reslen * n)
1960 n = (len - off) / reslen;
1961 len = off + reslen * n;
1963 struct wi_scan_p2_hdr *p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1965 * Prepend Prism-specific header.
1967 if (len < sizeof(struct wi_scan_p2_hdr)) {
1971 p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1973 p2->wi_reason = n; /* XXX */
1975 for (i = 0; i < n; i++, off += reslen) {
1976 const struct wi_apinfo *ap = &sc->sc_aps[i];
1978 res = (struct wi_scan_res *)((char *)wreq.wi_val + off);
1979 res->wi_chan = ap->channel;
1980 res->wi_noise = ap->noise;
1981 res->wi_signal = ap->signal;
1982 IEEE80211_ADDR_COPY(res->wi_bssid, ap->bssid);
1983 res->wi_interval = ap->interval;
1984 res->wi_capinfo = ap->capinfo;
1985 res->wi_ssid_len = ap->namelen;
1986 memcpy(res->wi_ssid, ap->name,
1987 IEEE80211_NWID_LEN);
1988 if (sc->sc_firmware_type != WI_LUCENT) {
1989 /* XXX not saved from Prism cards */
1990 memset(res->wi_srates, 0,
1991 sizeof(res->wi_srates));
1992 res->wi_rate = ap->rate;
1999 if (sc->sc_enabled) {
2000 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2004 switch (wreq.wi_type) {
2005 case WI_RID_MAX_DATALEN:
2006 wreq.wi_val[0] = htole16(sc->sc_max_datalen);
2007 len = sizeof(u_int16_t);
2009 case WI_RID_RTS_THRESH:
2010 wreq.wi_val[0] = htole16(ic->ic_rtsthreshold);
2011 len = sizeof(u_int16_t);
2013 case WI_RID_CNFAUTHMODE:
2014 wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
2015 len = sizeof(u_int16_t);
2017 case WI_RID_NODENAME:
2018 if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
2022 len = sc->sc_nodelen + sizeof(u_int16_t);
2023 wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
2024 memcpy(&wreq.wi_val[1], sc->sc_nodename,
2028 return ieee80211_cfgget(ifp, cmd, data, cr);
2034 wreq.wi_len = (len + 1) / 2 + 1;
2035 return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
2039 wi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
2041 struct wi_softc *sc = ifp->if_softc;
2042 struct ieee80211com *ic = &sc->sc_ic;
2043 struct ifreq *ifr = (struct ifreq *)data;
2046 int i, len, error, mif, val;
2047 struct ieee80211_rateset *rs;
2049 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2052 len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0;
2053 switch (wreq.wi_type) {
2054 case WI_RID_DBM_ADJUST:
2057 case WI_RID_NODENAME:
2058 if (le16toh(wreq.wi_val[0]) * 2 > len ||
2059 le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
2063 if (sc->sc_enabled) {
2064 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2069 sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
2070 memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen);
2073 case WI_RID_MICROWAVE_OVEN:
2074 case WI_RID_ROAMING_MODE:
2075 case WI_RID_SYSTEM_SCALE:
2076 case WI_RID_FRAG_THRESH:
2077 if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
2078 (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
2080 if (wreq.wi_type == WI_RID_ROAMING_MODE &&
2081 (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
2083 if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
2084 (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
2086 if (wreq.wi_type == WI_RID_FRAG_THRESH &&
2087 (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
2090 case WI_RID_RTS_THRESH:
2091 case WI_RID_CNFAUTHMODE:
2092 case WI_RID_MAX_DATALEN:
2093 if (sc->sc_enabled) {
2094 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2099 switch (wreq.wi_type) {
2100 case WI_RID_FRAG_THRESH:
2101 ic->ic_fragthreshold = le16toh(wreq.wi_val[0]);
2103 case WI_RID_RTS_THRESH:
2104 ic->ic_rtsthreshold = le16toh(wreq.wi_val[0]);
2106 case WI_RID_MICROWAVE_OVEN:
2107 sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
2109 case WI_RID_ROAMING_MODE:
2110 sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
2112 case WI_RID_SYSTEM_SCALE:
2113 sc->sc_system_scale = le16toh(wreq.wi_val[0]);
2115 case WI_RID_CNFAUTHMODE:
2116 sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
2118 case WI_RID_MAX_DATALEN:
2119 sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
2124 case WI_RID_TX_RATE:
2125 switch (le16toh(wreq.wi_val[0])) {
2127 ic->ic_fixed_rate = -1;
2130 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
2131 for (i = 0; i < rs->rs_nrates; i++) {
2132 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL)
2133 / 2 == le16toh(wreq.wi_val[0]))
2136 if (i == rs->rs_nrates)
2138 ic->ic_fixed_rate = i;
2141 error = wi_write_txrate(sc);
2144 case WI_RID_SCAN_APS:
2145 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2146 error = wi_scan_ap(sc, 0x3fff, 0x000f);
2149 case WI_RID_SCAN_REQ: /* compatibility interface */
2150 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2151 error = wi_scan_ap(sc, wreq.wi_val[0], wreq.wi_val[1]);
2154 case WI_RID_MGMT_XMIT:
2155 if (!sc->sc_enabled) {
2159 if (ic->ic_mgtq.ifq_len > 5) {
2163 /* XXX wi_len looks in u_int8_t, not in u_int16_t */
2164 m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL);
2169 IF_ENQUEUE(&ic->ic_mgtq, m);
2173 mif = wreq.wi_val[0];
2174 val = wreq.wi_val[1];
2175 error = wi_cmd(sc, WI_CMD_WRITEMIF, mif, val, 0);
2178 case WI_RID_PROCFRAME: /* ignore for compatibility */
2181 case WI_RID_OWN_SSID:
2182 if (le16toh(wreq.wi_val[0]) * 2 > len ||
2183 le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) {
2187 memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
2188 ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2;
2189 memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen);
2194 if (sc->sc_enabled) {
2195 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2200 error = ieee80211_cfgset(ifp, cmd, data);
2207 wi_write_txrate(struct wi_softc *sc)
2209 struct ieee80211com *ic = &sc->sc_ic;
2213 if (ic->ic_fixed_rate < 0)
2214 rate = 0; /* auto */
2216 rate = (ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ic->ic_fixed_rate] &
2217 IEEE80211_RATE_VAL) / 2;
2219 /* rate: 0, 1, 2, 5, 11 */
2221 switch (sc->sc_firmware_type) {
2224 case 0: /* auto == 11mbps auto */
2227 /* case 1, 2 map to 1, 2*/
2228 case 5: /* 5.5Mbps -> 4 */
2231 case 11: /* 11mbps -> 5 */
2239 /* Choose a bit according to this table.
2242 * ----+-------------------
2248 for (i = 8; i > 0; i >>= 1) {
2253 rate = 0xf; /* auto */
2258 return wi_write_val(sc, WI_RID_TX_RATE, rate);
2262 wi_write_wep(struct wi_softc *sc)
2264 struct ieee80211com *ic = &sc->sc_ic;
2268 struct wi_key wkey[IEEE80211_WEP_NKID];
2270 switch (sc->sc_firmware_type) {
2272 val = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0;
2273 error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
2276 error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_wep_txkey);
2279 memset(wkey, 0, sizeof(wkey));
2280 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2281 keylen = ic->ic_nw_keys[i].wk_len;
2282 wkey[i].wi_keylen = htole16(keylen);
2283 memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
2286 error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
2287 wkey, sizeof(wkey));
2292 if (ic->ic_flags & IEEE80211_F_WEPON) {
2294 * ONLY HWB3163 EVAL-CARD Firmware version
2295 * less than 0.8 variant2
2297 * If promiscuous mode disable, Prism2 chip
2298 * does not work with WEP .
2299 * It is under investigation for details.
2300 * (ichiro@netbsd.org)
2302 if (sc->sc_firmware_type == WI_INTERSIL &&
2303 sc->sc_sta_firmware_ver < 802 ) {
2304 /* firm ver < 0.8 variant 2 */
2305 wi_write_val(sc, WI_RID_PROMISC, 1);
2307 wi_write_val(sc, WI_RID_CNFAUTHMODE,
2308 sc->sc_cnfauthmode);
2309 val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
2311 * Encryption firmware has a bug for HostAP mode.
2313 if (sc->sc_firmware_type == WI_INTERSIL &&
2314 ic->ic_opmode == IEEE80211_M_HOSTAP)
2315 val |= HOST_ENCRYPT;
2317 wi_write_val(sc, WI_RID_CNFAUTHMODE,
2318 IEEE80211_AUTH_OPEN);
2319 val = HOST_ENCRYPT | HOST_DECRYPT;
2321 error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
2324 error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
2329 * It seems that the firmware accept 104bit key only if
2330 * all the keys have 104bit length. We get the length of
2331 * the transmit key and use it for all other keys.
2332 * Perhaps we should use software WEP for such situation.
2334 keylen = ic->ic_nw_keys[ic->ic_wep_txkey].wk_len;
2335 if (keylen > IEEE80211_WEP_KEYLEN)
2336 keylen = 13; /* 104bit keys */
2338 keylen = IEEE80211_WEP_KEYLEN;
2339 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2340 error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
2341 ic->ic_nw_keys[i].wk_key, keylen);
2351 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
2354 static volatile int count = 0;
2360 panic("Hey partner, hold on there!");
2363 /* wait for the busy bit to clear */
2364 for (i = sc->wi_cmd_count; i > 0; i--) { /* 500ms */
2365 if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
2367 DELAY(1*1000); /* 1ms */
2370 device_printf(sc->sc_dev, "wi_cmd: busy bit won't clear.\n" );
2376 CSR_WRITE_2(sc, WI_PARAM0, val0);
2377 CSR_WRITE_2(sc, WI_PARAM1, val1);
2378 CSR_WRITE_2(sc, WI_PARAM2, val2);
2379 CSR_WRITE_2(sc, WI_COMMAND, cmd);
2381 if (cmd == WI_CMD_INI) {
2382 /* XXX: should sleep here. */
2383 DELAY(100*1000); /* 100ms delay for init */
2385 for (i = 0; i < WI_TIMEOUT; i++) {
2387 * Wait for 'command complete' bit to be
2388 * set in the event status register.
2390 s = CSR_READ_2(sc, WI_EVENT_STAT);
2391 if (s & WI_EV_CMD) {
2392 /* Ack the event and read result code. */
2393 s = CSR_READ_2(sc, WI_STATUS);
2394 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
2395 if (s & WI_STAT_CMD_RESULT) {
2405 if (i == WI_TIMEOUT) {
2406 device_printf(sc->sc_dev,
2407 "timeout in wi_cmd 0x%04x; event status 0x%04x\n", cmd, s);
2416 wi_seek_bap(struct wi_softc *sc, int id, int off)
2420 CSR_WRITE_2(sc, WI_SEL0, id);
2421 CSR_WRITE_2(sc, WI_OFF0, off);
2423 for (i = 0; ; i++) {
2424 status = CSR_READ_2(sc, WI_OFF0);
2425 if ((status & WI_OFF_BUSY) == 0)
2427 if (i == WI_TIMEOUT) {
2428 device_printf(sc->sc_dev, "timeout in wi_seek to %x/%x\n",
2430 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2431 if (status == 0xffff)
2437 if (status & WI_OFF_ERR) {
2438 device_printf(sc->sc_dev, "failed in wi_seek to %x/%x\n", id, off);
2439 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2443 sc->sc_bap_off = off;
2448 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
2455 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2456 if ((error = wi_seek_bap(sc, id, off)) != 0)
2459 cnt = (buflen + 1) / 2;
2460 ptr = (u_int16_t *)buf;
2461 for (i = 0; i < cnt; i++)
2462 *ptr++ = CSR_READ_2(sc, WI_DATA0);
2463 sc->sc_bap_off += cnt * 2;
2468 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
2476 #ifdef WI_HERMES_AUTOINC_WAR
2479 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2480 if ((error = wi_seek_bap(sc, id, off)) != 0)
2483 cnt = (buflen + 1) / 2;
2484 ptr = (u_int16_t *)buf;
2485 for (i = 0; i < cnt; i++)
2486 CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
2487 sc->sc_bap_off += cnt * 2;
2489 #ifdef WI_HERMES_AUTOINC_WAR
2491 * According to the comments in the HCF Light code, there is a bug
2492 * in the Hermes (or possibly in certain Hermes firmware revisions)
2493 * where the chip's internal autoincrement counter gets thrown off
2494 * during data writes: the autoincrement is missed, causing one
2495 * data word to be overwritten and subsequent words to be written to
2496 * the wrong memory locations. The end result is that we could end
2497 * up transmitting bogus frames without realizing it. The workaround
2498 * for this is to write a couple of extra guard words after the end
2499 * of the transfer, then attempt to read then back. If we fail to
2500 * locate the guard words where we expect them, we preform the
2501 * transfer over again.
2503 if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
2504 CSR_WRITE_2(sc, WI_DATA0, 0x1234);
2505 CSR_WRITE_2(sc, WI_DATA0, 0x5678);
2506 wi_seek_bap(sc, id, sc->sc_bap_off);
2507 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2508 if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
2509 CSR_READ_2(sc, WI_DATA0) != 0x5678) {
2510 device_printf(sc->sc_dev,
2511 "detect auto increment bug, try again\n");
2520 wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
2525 for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
2529 len = min(m->m_len, totlen);
2531 if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
2532 m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
2533 return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
2537 if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
2547 wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
2551 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
2552 device_printf(sc->sc_dev, "failed to allocate %d bytes on NIC\n",
2557 for (i = 0; i < WI_TIMEOUT; i++) {
2558 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
2560 if (i == WI_TIMEOUT) {
2561 device_printf(sc->sc_dev, "timeout in alloc\n");
2566 *idp = CSR_READ_2(sc, WI_ALLOC_FID);
2567 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
2572 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
2577 /* Tell the NIC to enter record read mode. */
2578 error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2582 error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2586 if (le16toh(ltbuf[1]) != rid) {
2587 device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n",
2588 rid, le16toh(ltbuf[1]));
2591 len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */
2592 if (*buflenp < len) {
2593 device_printf(sc->sc_dev, "record buffer is too small, "
2594 "rid=%x, size=%d, len=%d\n",
2595 rid, *buflenp, len);
2599 return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2603 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2608 ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */
2609 ltbuf[1] = htole16(rid);
2611 error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2614 error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2618 return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
2622 wi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
2624 struct ifnet *ifp = &ic->ic_if;
2625 struct wi_softc *sc = ifp->if_softc;
2626 struct ieee80211_node *ni = ic->ic_bss;
2629 struct wi_ssid ssid;
2630 u_int8_t old_bssid[IEEE80211_ADDR_LEN];
2632 DPRINTF(("%s: %s -> %s\n", __func__,
2633 ieee80211_state_name[ic->ic_state],
2634 ieee80211_state_name[nstate]));
2637 case IEEE80211_S_INIT:
2638 ic->ic_flags &= ~IEEE80211_F_SIBSS;
2639 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2640 return (*sc->sc_newstate)(ic, nstate, arg);
2642 case IEEE80211_S_RUN:
2643 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2644 buflen = IEEE80211_ADDR_LEN;
2645 wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen);
2646 IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
2647 buflen = sizeof(val);
2648 wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
2649 /* XXX validate channel */
2650 ni->ni_chan = &ic->ic_channels[le16toh(val)];
2652 sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
2653 htole16(ni->ni_chan->ic_freq);
2654 sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
2655 htole16(ni->ni_chan->ic_flags);
2658 if (IEEE80211_ADDR_EQ(old_bssid, ni->ni_bssid))
2659 sc->sc_false_syns++;
2661 sc->sc_false_syns = 0;
2663 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2664 ni->ni_esslen = ic->ic_des_esslen;
2665 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
2666 ni->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B];
2667 ni->ni_intval = ic->ic_lintval;
2668 ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
2669 if (ic->ic_flags & IEEE80211_F_WEPON)
2670 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
2672 /* XXX check return value */
2673 buflen = sizeof(ssid);
2674 wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
2675 ni->ni_esslen = le16toh(ssid.wi_len);
2676 if (ni->ni_esslen > IEEE80211_NWID_LEN)
2677 ni->ni_esslen = IEEE80211_NWID_LEN; /*XXX*/
2678 memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
2682 case IEEE80211_S_SCAN:
2683 case IEEE80211_S_AUTH:
2684 case IEEE80211_S_ASSOC:
2688 ic->ic_state = nstate; /* NB: skip normal ieee80211 handling */
2693 wi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate)
2698 if (!sc->sc_enabled)
2700 switch (sc->sc_firmware_type) {
2702 (void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
2705 val[0] = chanmask; /* channel */
2706 val[1] = txrate; /* tx rate */
2707 error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
2711 * XXX only supported on 3.x ?
2713 val[0] = BSCAN_BCAST | BSCAN_ONETIME;
2714 error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
2715 val, sizeof(val[0]));
2719 sc->sc_scan_timer = WI_SCAN_WAIT;
2720 sc->sc_ic.ic_if.if_timer = 1;
2721 DPRINTF(("wi_scan_ap: start scanning, "
2722 "chamask 0x%x txrate 0x%x\n", chanmask, txrate));
2728 wi_scan_result(struct wi_softc *sc, int fid, int cnt)
2730 #define N(a) (sizeof (a) / sizeof (a[0]))
2731 int i, naps, off, szbuf;
2732 struct wi_scan_header ws_hdr; /* Prism2 header */
2733 struct wi_scan_data_p2 ws_dat; /* Prism2 scantable*/
2734 struct wi_apinfo *ap;
2736 off = sizeof(u_int16_t) * 2;
2737 memset(&ws_hdr, 0, sizeof(ws_hdr));
2738 switch (sc->sc_firmware_type) {
2740 wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
2741 off += sizeof(ws_hdr);
2742 szbuf = sizeof(struct wi_scan_data_p2);
2745 szbuf = sizeof(struct wi_scan_data_p2) + 6;
2748 szbuf = sizeof(struct wi_scan_data);
2751 device_printf(sc->sc_dev,
2752 "wi_scan_result: unknown firmware type %u\n",
2753 sc->sc_firmware_type);
2757 naps = (cnt * 2 + 2 - off) / szbuf;
2758 if (naps > N(sc->sc_aps))
2759 naps = N(sc->sc_aps);
2763 memset(&ws_dat, 0, sizeof(ws_dat));
2764 for (i = 0; i < naps; i++, ap++) {
2765 wi_read_bap(sc, fid, off, &ws_dat,
2766 (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
2767 DPRINTF2(("wi_scan_result: #%d: off %d bssid %6D\n", i, off,
2768 ws_dat.wi_bssid, ":"));
2770 ap->scanreason = le16toh(ws_hdr.wi_reason);
2771 memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
2772 ap->channel = le16toh(ws_dat.wi_chid);
2773 ap->signal = le16toh(ws_dat.wi_signal);
2774 ap->noise = le16toh(ws_dat.wi_noise);
2775 ap->quality = ap->signal - ap->noise;
2776 ap->capinfo = le16toh(ws_dat.wi_capinfo);
2777 ap->interval = le16toh(ws_dat.wi_interval);
2778 ap->rate = le16toh(ws_dat.wi_rate);
2779 ap->namelen = le16toh(ws_dat.wi_namelen);
2780 if (ap->namelen > sizeof(ap->name))
2781 ap->namelen = sizeof(ap->name);
2782 memcpy(ap->name, ws_dat.wi_name, ap->namelen);
2786 sc->sc_scan_timer = 0;
2787 DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
2792 wi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi)
2794 ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr),
2795 ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL : -1, rssi);
2796 printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n",
2797 le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1),
2798 le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence);
2799 printf(" rx_signal %u rx_rate %u rx_flow %u\n",
2800 wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow);
2801 printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n",
2802 wh->wi_tx_rtry, wh->wi_tx_rate,
2803 le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len));
2804 printf(" ehdr dst %6D src %6D type 0x%x\n",
2805 wh->wi_ehdr.ether_dhost, ":", wh->wi_ehdr.ether_shost, ":",
2806 wh->wi_ehdr.ether_type);
2810 wi_alloc(device_t dev, int rid)
2812 struct wi_softc *sc = device_get_softc(dev);
2814 if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2815 sc->iobase_rid = rid;
2816 sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2817 &sc->iobase_rid, 0, ~0, (1 << 6),
2818 rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2820 device_printf(dev, "No I/O space?!\n");
2824 sc->wi_io_addr = rman_get_start(sc->iobase);
2825 sc->wi_btag = rman_get_bustag(sc->iobase);
2826 sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2829 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2830 &sc->mem_rid, RF_ACTIVE);
2833 device_printf(dev, "No Mem space on prism2.5?\n");
2837 sc->wi_btag = rman_get_bustag(sc->mem);
2838 sc->wi_bhandle = rman_get_bushandle(sc->mem);
2843 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2845 ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2849 device_printf(dev, "No irq?!\n");
2854 sc->sc_unit = device_get_unit(dev);
2860 wi_free(device_t dev)
2862 struct wi_softc *sc = device_get_softc(dev);
2864 if (sc->iobase != NULL) {
2865 bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2868 if (sc->irq != NULL) {
2869 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2872 if (sc->mem != NULL) {
2873 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
2881 wi_get_debug(struct wi_softc *sc, struct wi_req *wreq)
2887 switch (wreq->wi_type) {
2888 case WI_DEBUG_SLEEP:
2890 wreq->wi_val[0] = sc->wi_debug.wi_sleep;
2892 case WI_DEBUG_DELAYSUPP:
2894 wreq->wi_val[0] = sc->wi_debug.wi_delaysupp;
2896 case WI_DEBUG_TXSUPP:
2898 wreq->wi_val[0] = sc->wi_debug.wi_txsupp;
2900 case WI_DEBUG_MONITOR:
2902 wreq->wi_val[0] = sc->wi_debug.wi_monitor;
2904 case WI_DEBUG_LEDTEST:
2906 wreq->wi_val[0] = sc->wi_debug.wi_ledtest;
2907 wreq->wi_val[1] = sc->wi_debug.wi_ledtest_param0;
2908 wreq->wi_val[2] = sc->wi_debug.wi_ledtest_param1;
2910 case WI_DEBUG_CONTTX:
2912 wreq->wi_val[0] = sc->wi_debug.wi_conttx;
2913 wreq->wi_val[1] = sc->wi_debug.wi_conttx_param0;
2915 case WI_DEBUG_CONTRX:
2917 wreq->wi_val[0] = sc->wi_debug.wi_contrx;
2919 case WI_DEBUG_SIGSTATE:
2921 wreq->wi_val[0] = sc->wi_debug.wi_sigstate;
2922 wreq->wi_val[1] = sc->wi_debug.wi_sigstate_param0;
2924 case WI_DEBUG_CONFBITS:
2926 wreq->wi_val[0] = sc->wi_debug.wi_confbits;
2927 wreq->wi_val[1] = sc->wi_debug.wi_confbits_param0;
2938 wi_set_debug(struct wi_softc *sc, struct wi_req *wreq)
2941 u_int16_t cmd, param0 = 0, param1 = 0;
2943 switch (wreq->wi_type) {
2944 case WI_DEBUG_RESET:
2946 case WI_DEBUG_CALENABLE:
2948 case WI_DEBUG_SLEEP:
2949 sc->wi_debug.wi_sleep = 1;
2952 sc->wi_debug.wi_sleep = 0;
2955 param0 = wreq->wi_val[0];
2957 case WI_DEBUG_DELAYSUPP:
2958 sc->wi_debug.wi_delaysupp = 1;
2960 case WI_DEBUG_TXSUPP:
2961 sc->wi_debug.wi_txsupp = 1;
2963 case WI_DEBUG_MONITOR:
2964 sc->wi_debug.wi_monitor = 1;
2966 case WI_DEBUG_LEDTEST:
2967 param0 = wreq->wi_val[0];
2968 param1 = wreq->wi_val[1];
2969 sc->wi_debug.wi_ledtest = 1;
2970 sc->wi_debug.wi_ledtest_param0 = param0;
2971 sc->wi_debug.wi_ledtest_param1 = param1;
2973 case WI_DEBUG_CONTTX:
2974 param0 = wreq->wi_val[0];
2975 sc->wi_debug.wi_conttx = 1;
2976 sc->wi_debug.wi_conttx_param0 = param0;
2978 case WI_DEBUG_STOPTEST:
2979 sc->wi_debug.wi_delaysupp = 0;
2980 sc->wi_debug.wi_txsupp = 0;
2981 sc->wi_debug.wi_monitor = 0;
2982 sc->wi_debug.wi_ledtest = 0;
2983 sc->wi_debug.wi_ledtest_param0 = 0;
2984 sc->wi_debug.wi_ledtest_param1 = 0;
2985 sc->wi_debug.wi_conttx = 0;
2986 sc->wi_debug.wi_conttx_param0 = 0;
2987 sc->wi_debug.wi_contrx = 0;
2988 sc->wi_debug.wi_sigstate = 0;
2989 sc->wi_debug.wi_sigstate_param0 = 0;
2991 case WI_DEBUG_CONTRX:
2992 sc->wi_debug.wi_contrx = 1;
2994 case WI_DEBUG_SIGSTATE:
2995 param0 = wreq->wi_val[0];
2996 sc->wi_debug.wi_sigstate = 1;
2997 sc->wi_debug.wi_sigstate_param0 = param0;
2999 case WI_DEBUG_CONFBITS:
3000 param0 = wreq->wi_val[0];
3001 param1 = wreq->wi_val[1];
3002 sc->wi_debug.wi_confbits = param0;
3003 sc->wi_debug.wi_confbits_param0 = param1;
3013 cmd = WI_CMD_DEBUG | (wreq->wi_type << 8);
3014 error = wi_cmd(sc, cmd, param0, param1, 0);
3020 * Special routines to download firmware for Symbol CF card.
3021 * XXX: This should be modified generic into any PRISM-2 based card.
3024 #define WI_SBCF_PDIADDR 0x3100
3026 /* unaligned load little endian */
3027 #define GETLE32(p) ((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24))
3028 #define GETLE16(p) ((p)[0] | ((p)[1]<<8))
3031 wi_symbol_load_firm(struct wi_softc *sc, const void *primsym, int primlen,
3032 const void *secsym, int seclen)
3037 /* load primary code and run it */
3038 wi_symbol_set_hcr(sc, WI_HCR_EEHOLD);
3039 if (wi_symbol_write_firm(sc, primsym, primlen, NULL, 0))
3041 wi_symbol_set_hcr(sc, WI_HCR_RUN);
3042 for (i = 0; ; i++) {
3045 tsleep(sc, 0, "wiinit", 1);
3046 if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT)
3048 /* write the magic key value to unlock aux port */
3049 CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0);
3050 CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1);
3051 CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2);
3052 CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL);
3055 /* issue read EEPROM command: XXX copied from wi_cmd() */
3056 CSR_WRITE_2(sc, WI_PARAM0, 0);
3057 CSR_WRITE_2(sc, WI_PARAM1, 0);
3058 CSR_WRITE_2(sc, WI_PARAM2, 0);
3059 CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE);
3060 for (i = 0; i < WI_TIMEOUT; i++) {
3061 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
3065 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
3067 CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ);
3068 CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ);
3069 CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA,
3070 (uint16_t *)ebuf, sizeof(ebuf) / 2);
3071 if (GETLE16(ebuf) > sizeof(ebuf))
3073 if (wi_symbol_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf)))
3079 wi_symbol_write_firm(struct wi_softc *sc, const void *buf, int buflen,
3080 const void *ebuf, int ebuflen)
3082 const uint8_t *p, *ep, *q, *eq;
3084 uint32_t addr, id, eid;
3085 int i, len, elen, nblk, pdrlen;
3088 * Parse the header of the firmware image.
3092 while (p < ep && *p++ != ' '); /* FILE: */
3093 while (p < ep && *p++ != ' '); /* filename */
3094 while (p < ep && *p++ != ' '); /* type of the firmware */
3095 nblk = strtoul(p, &tp, 10);
3097 pdrlen = strtoul(p + 1, &tp, 10);
3099 while (p < ep && *p++ != 0x1a); /* skip rest of header */
3102 * Block records: address[4], length[2], data[length];
3104 for (i = 0; i < nblk; i++) {
3105 addr = GETLE32(p); p += 4;
3106 len = GETLE16(p); p += 2;
3107 CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3108 CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3109 CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3110 (const uint16_t *)p, len / 2);
3115 * PDR: id[4], address[4], length[4];
3117 for (i = 0; i < pdrlen; ) {
3118 id = GETLE32(p); p += 4; i += 4;
3119 addr = GETLE32(p); p += 4; i += 4;
3120 len = GETLE32(p); p += 4; i += 4;
3121 /* replace PDR entry with the values from EEPROM, if any */
3122 for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) {
3123 elen = GETLE16(q); q += 2;
3124 eid = GETLE16(q); q += 2;
3125 elen--; /* elen includes eid */
3130 CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3131 CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3132 CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3133 (const uint16_t *)q, len / 2);
3141 wi_symbol_set_hcr(struct wi_softc *sc, int mode)
3145 CSR_WRITE_2(sc, WI_COR, WI_COR_RESET);
3146 tsleep(sc, 0, "wiinit", 1);
3147 hcr = CSR_READ_2(sc, WI_HCR);
3148 hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE);
3149 CSR_WRITE_2(sc, WI_HCR, hcr);
3150 tsleep(sc, 0, "wiinit", 1);
3151 CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE);
3152 tsleep(sc, 0, "wiinit", 1);