2 * Copyright (c) 1997, 1998, 1999
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
34 * Lucent WaveLAN/IEEE 802.11 PCMCIA driver.
36 * Original FreeBSD driver written by Bill Paul <wpaul@ctr.columbia.edu>
37 * Electrical Engineering Department
38 * Columbia University, New York City
42 * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
43 * from Lucent. Unlike the older cards, the new ones are programmed
44 * entirely via a firmware-driven controller called the Hermes.
45 * Unfortunately, Lucent will not release the Hermes programming manual
46 * without an NDA (if at all). What they do release is an API library
47 * called the HCF (Hardware Control Functions) which is supposed to
48 * do the device-specific operations of a device driver for you. The
49 * publically available version of the HCF library (the 'HCF Light') is
50 * a) extremely gross, b) lacks certain features, particularly support
51 * for 802.11 frames, and c) is contaminated by the GNU Public License.
53 * This driver does not use the HCF or HCF Light at all. Instead, it
54 * programs the Hermes controller directly, using information gleaned
55 * from the HCF Light code and corresponding documentation.
57 * This driver supports the ISA, PCMCIA and PCI versions of the Lucent
58 * WaveLan cards (based on the Hermes chipset), as well as the newer
59 * Prism 2 chipsets with firmware from Intersil and Symbol.
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD$");
67 #define WI_HERMES_STATS_WAR /* Work around stats counter bug. */
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/endian.h>
72 #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/atomic.h>
90 #include <net/if_var.h>
91 #include <net/if_arp.h>
92 #include <net/ethernet.h>
93 #include <net/if_dl.h>
94 #include <net/if_llc.h>
95 #include <net/if_media.h>
96 #include <net/if_types.h>
98 #include <net80211/ieee80211_var.h>
99 #include <net80211/ieee80211_ioctl.h>
100 #include <net80211/ieee80211_radiotap.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/wi/if_wavelan_ieee.h>
111 #include <dev/wi/if_wireg.h>
112 #include <dev/wi/if_wivar.h>
114 static struct ieee80211vap *wi_vap_create(struct ieee80211com *,
115 const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
116 const uint8_t [IEEE80211_ADDR_LEN],
117 const uint8_t [IEEE80211_ADDR_LEN]);
118 static void wi_vap_delete(struct ieee80211vap *vap);
119 static void wi_stop_locked(struct wi_softc *sc, int disable);
120 static void wi_start_locked(struct ifnet *);
121 static void wi_start(struct ifnet *);
122 static int wi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr,
124 static int wi_raw_xmit(struct ieee80211_node *, struct mbuf *,
125 const struct ieee80211_bpf_params *);
126 static int wi_newstate_sta(struct ieee80211vap *, enum ieee80211_state, int);
127 static int wi_newstate_hostap(struct ieee80211vap *, enum ieee80211_state,
129 static void wi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
130 int subtype, const struct ieee80211_rx_stats *rxs,
132 static int wi_reset(struct wi_softc *);
133 static void wi_watchdog(void *);
134 static int wi_ioctl(struct ifnet *, u_long, caddr_t);
135 static void wi_media_status(struct ifnet *, struct ifmediareq *);
136 static uint64_t wi_get_counter(struct ifnet *, ift_counter);
138 static void wi_rx_intr(struct wi_softc *);
139 static void wi_tx_intr(struct wi_softc *);
140 static void wi_tx_ex_intr(struct wi_softc *);
142 static void wi_info_intr(struct wi_softc *);
144 static int wi_write_txrate(struct wi_softc *, struct ieee80211vap *);
145 static int wi_write_wep(struct wi_softc *, struct ieee80211vap *);
146 static int wi_write_multi(struct wi_softc *);
147 static void wi_update_mcast(struct ieee80211com *);
148 static void wi_update_promisc(struct ieee80211com *);
149 static int wi_alloc_fid(struct wi_softc *, int, int *);
150 static void wi_read_nicid(struct wi_softc *);
151 static int wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
153 static int wi_cmd(struct wi_softc *, int, int, int, int);
154 static int wi_seek_bap(struct wi_softc *, int, int);
155 static int wi_read_bap(struct wi_softc *, int, int, void *, int);
156 static int wi_write_bap(struct wi_softc *, int, int, void *, int);
157 static int wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
158 static int wi_read_rid(struct wi_softc *, int, void *, int *);
159 static int wi_write_rid(struct wi_softc *, int, void *, int);
160 static int wi_write_appie(struct wi_softc *, int, const struct ieee80211_appie *);
162 static void wi_scan_start(struct ieee80211com *);
163 static void wi_scan_end(struct ieee80211com *);
164 static void wi_set_channel(struct ieee80211com *);
167 wi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
171 return wi_write_rid(sc, rid, &val, sizeof(val));
174 static SYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0,
175 "Wireless driver parameters");
177 static struct timeval lasttxerror; /* time of last tx error msg */
178 static int curtxeps; /* current tx error msgs/sec */
179 static int wi_txerate = 0; /* tx error rate: max msgs/sec */
180 SYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate,
181 0, "max tx error msgs/sec; 0 to disable msgs");
185 static int wi_debug = 0;
186 SYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug,
187 0, "control debugging printfs");
188 #define DPRINTF(X) if (wi_debug) printf X
193 #define WI_INTRS (WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
195 struct wi_card_ident wi_card_ident[] = {
196 /* CARD_ID CARD_NAME FIRM_TYPE */
197 { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT },
198 { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT },
199 { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT },
200 { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL },
201 { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL },
202 { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL },
203 { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL },
204 { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL },
205 { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL },
206 { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL },
207 { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL },
208 { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL },
209 { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
210 { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
211 { WI_NIC_3842_PCMCIA_ATL_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
212 { WI_NIC_3842_PCMCIA_ATS_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
213 { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
214 { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
215 { WI_NIC_3842_MINI_ATL_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
216 { WI_NIC_3842_MINI_ATS_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
217 { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
218 { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
219 { WI_NIC_3842_PCI_ATS_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
220 { WI_NIC_3842_PCI_ATL_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
221 { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
222 { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
223 { WI_NIC_P3_PCMCIA_ATL_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
224 { WI_NIC_P3_PCMCIA_ATS_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
225 { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
226 { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
227 { WI_NIC_P3_MINI_ATL_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
228 { WI_NIC_P3_MINI_ATS_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
232 static char *wi_firmware_names[] = { "none", "Hermes", "Intersil", "Symbol" };
234 devclass_t wi_devclass;
237 wi_attach(device_t dev)
239 struct wi_softc *sc = device_get_softc(dev);
240 struct ieee80211com *ic;
242 int i, nrates, buflen;
244 u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
245 struct ieee80211_rateset *rs;
246 struct sysctl_ctx_list *sctx;
247 struct sysctl_oid *soid;
248 static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
249 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
252 uint8_t macaddr[IEEE80211_ADDR_LEN];
254 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
256 device_printf(dev, "can not if_alloc\n");
262 sc->sc_firmware_type = WI_NOTYPE;
263 sc->wi_cmd_count = 500;
265 if (wi_reset(sc) != 0) {
267 return ENXIO; /* XXX */
270 /* Read NIC identification */
272 switch (sc->sc_firmware_type) {
274 if (sc->sc_sta_firmware_ver < 60006)
278 if (sc->sc_sta_firmware_ver < 800)
283 device_printf(dev, "Sorry, this card is not supported "
284 "(type %d, firmware ver %d)\n",
285 sc->sc_firmware_type, sc->sc_sta_firmware_ver);
290 /* Export info about the device via sysctl */
291 sctx = device_get_sysctl_ctx(dev);
292 soid = device_get_sysctl_tree(dev);
293 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
294 "firmware_type", CTLFLAG_RD,
295 wi_firmware_names[sc->sc_firmware_type], 0,
296 "Firmware type string");
297 SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "sta_version",
298 CTLFLAG_RD, &sc->sc_sta_firmware_ver, 0,
299 "Station Firmware version");
300 if (sc->sc_firmware_type == WI_INTERSIL)
301 SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
302 "pri_version", CTLFLAG_RD, &sc->sc_pri_firmware_ver, 0,
303 "Primary Firmware version");
304 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "nic_id",
305 CTLFLAG_RD, &sc->sc_nic_id, 0, "NIC id");
306 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "nic_name",
307 CTLFLAG_RD, sc->sc_nic_name, 0, "NIC name");
309 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
310 MTX_DEF | MTX_RECURSE);
311 callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
314 * Read the station address.
315 * And do it twice. I've seen PRISM-based cards that return
316 * an error when trying to read it the first time, which causes
319 buflen = IEEE80211_ADDR_LEN;
320 error = wi_read_rid(sc, WI_RID_MAC_NODE, macaddr, &buflen);
322 buflen = IEEE80211_ADDR_LEN;
323 error = wi_read_rid(sc, WI_RID_MAC_NODE, macaddr, &buflen);
325 if (error || IEEE80211_ADDR_EQ(macaddr, empty_macaddr)) {
327 device_printf(dev, "mac read failed %d\n", error);
329 device_printf(dev, "mac read failed (all zeros)\n");
337 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
338 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
339 ifp->if_ioctl = wi_ioctl;
340 ifp->if_start = wi_start;
341 ifp->if_init = wi_init;
342 ifp->if_get_counter = wi_get_counter;
343 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
344 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
345 IFQ_SET_READY(&ifp->if_snd);
349 ic->ic_name = device_get_nameunit(dev);
350 ic->ic_phytype = IEEE80211_T_DS;
351 ic->ic_opmode = IEEE80211_M_STA;
352 ic->ic_caps = IEEE80211_C_STA
354 | IEEE80211_C_MONITOR
358 * Query the card for available channels and setup the
359 * channel table. We assume these are all 11b channels.
361 buflen = sizeof(val);
362 if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
363 val = htole16(0x1fff); /* assume 1-11 */
364 KASSERT(val != 0, ("wi_attach: no available channels listed!"));
366 val <<= 1; /* shift for base 1 indices */
367 for (i = 1; i < 16; i++) {
368 struct ieee80211_channel *c;
370 if (!isset((u_int8_t*)&val, i))
372 c = &ic->ic_channels[ic->ic_nchans++];
373 c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
374 c->ic_flags = IEEE80211_CHAN_B;
380 * Set flags based on firmware version.
382 switch (sc->sc_firmware_type) {
385 ic->ic_caps |= IEEE80211_C_IBSS;
387 sc->sc_ibss_port = WI_PORTTYPE_BSS;
388 sc->sc_monitor_port = WI_PORTTYPE_ADHOC;
389 sc->sc_min_rssi = WI_LUCENT_MIN_RSSI;
390 sc->sc_max_rssi = WI_LUCENT_MAX_RSSI;
391 sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
394 sc->sc_ntxbuf = WI_NTXBUF;
395 sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR
396 | WI_FLAGS_HAS_ROAMING;
398 * Old firmware are slow, so give peace a chance.
400 if (sc->sc_sta_firmware_ver < 10000)
401 sc->wi_cmd_count = 5000;
402 if (sc->sc_sta_firmware_ver > 10101)
403 sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
404 ic->ic_caps |= IEEE80211_C_IBSS;
406 * version 0.8.3 and newer are the only ones that are known
407 * to currently work. Earlier versions can be made to work,
408 * at least according to the Linux driver but we require
409 * monitor mode so this is irrelevant.
411 ic->ic_caps |= IEEE80211_C_HOSTAP;
412 if (sc->sc_sta_firmware_ver >= 10603)
413 sc->sc_flags |= WI_FLAGS_HAS_ENHSECURITY;
414 if (sc->sc_sta_firmware_ver >= 10700) {
416 * 1.7.0+ have the necessary support for sta mode WPA.
418 sc->sc_flags |= WI_FLAGS_HAS_WPASUPPORT;
419 ic->ic_caps |= IEEE80211_C_WPA;
422 sc->sc_ibss_port = WI_PORTTYPE_IBSS;
423 sc->sc_monitor_port = WI_PORTTYPE_APSILENT;
424 sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
425 sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
426 sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
431 * Find out if we support WEP on this card.
433 buflen = sizeof(val);
434 if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
436 ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
438 /* Find supported rates. */
439 buflen = sizeof(ratebuf);
440 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
441 if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
442 nrates = le16toh(*(u_int16_t *)ratebuf);
443 if (nrates > IEEE80211_RATE_MAXSIZE)
444 nrates = IEEE80211_RATE_MAXSIZE;
446 for (i = 0; i < nrates; i++)
448 rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i];
450 /* XXX fallback on error? */
453 buflen = sizeof(val);
454 if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
455 wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
456 sc->sc_dbm_offset = le16toh(val);
459 sc->sc_portnum = WI_DEFAULT_PORT;
461 ieee80211_ifattach(ic, macaddr);
462 ic->ic_raw_xmit = wi_raw_xmit;
463 ic->ic_scan_start = wi_scan_start;
464 ic->ic_scan_end = wi_scan_end;
465 ic->ic_set_channel = wi_set_channel;
467 ic->ic_vap_create = wi_vap_create;
468 ic->ic_vap_delete = wi_vap_delete;
469 ic->ic_update_mcast = wi_update_mcast;
470 ic->ic_update_promisc = wi_update_promisc;
472 ieee80211_radiotap_attach(ic,
473 &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
474 WI_TX_RADIOTAP_PRESENT,
475 &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
476 WI_RX_RADIOTAP_PRESENT);
479 ieee80211_announce(ic);
481 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
482 NULL, wi_intr, sc, &sc->wi_intrhand);
484 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
485 ieee80211_ifdetach(ic);
495 wi_detach(device_t dev)
497 struct wi_softc *sc = device_get_softc(dev);
498 struct ifnet *ifp = sc->sc_ifp;
499 struct ieee80211com *ic = ifp->if_l2com;
503 /* check if device was removed */
504 sc->wi_gone |= !bus_child_present(dev);
506 wi_stop_locked(sc, 0);
508 ieee80211_ifdetach(ic);
510 bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
513 mtx_destroy(&sc->sc_mtx);
517 static struct ieee80211vap *
518 wi_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
519 enum ieee80211_opmode opmode, int flags,
520 const uint8_t bssid[IEEE80211_ADDR_LEN],
521 const uint8_t mac[IEEE80211_ADDR_LEN])
523 struct wi_softc *sc = ic->ic_softc;
525 struct ieee80211vap *vap;
527 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
529 wvp = (struct wi_vap *) malloc(sizeof(struct wi_vap),
530 M_80211_VAP, M_NOWAIT | M_ZERO);
535 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
537 vap->iv_max_aid = WI_MAX_AID;
540 case IEEE80211_M_STA:
541 sc->sc_porttype = WI_PORTTYPE_BSS;
542 wvp->wv_newstate = vap->iv_newstate;
543 vap->iv_newstate = wi_newstate_sta;
544 /* need to filter mgt frames to avoid confusing state machine */
545 wvp->wv_recv_mgmt = vap->iv_recv_mgmt;
546 vap->iv_recv_mgmt = wi_recv_mgmt;
548 case IEEE80211_M_IBSS:
549 sc->sc_porttype = sc->sc_ibss_port;
550 wvp->wv_newstate = vap->iv_newstate;
551 vap->iv_newstate = wi_newstate_sta;
553 case IEEE80211_M_AHDEMO:
554 sc->sc_porttype = WI_PORTTYPE_ADHOC;
556 case IEEE80211_M_HOSTAP:
557 sc->sc_porttype = WI_PORTTYPE_HOSTAP;
558 wvp->wv_newstate = vap->iv_newstate;
559 vap->iv_newstate = wi_newstate_hostap;
561 case IEEE80211_M_MONITOR:
562 sc->sc_porttype = sc->sc_monitor_port;
569 ieee80211_vap_attach(vap, ieee80211_media_change, wi_media_status);
570 ic->ic_opmode = opmode;
575 wi_vap_delete(struct ieee80211vap *vap)
577 struct wi_vap *wvp = WI_VAP(vap);
579 ieee80211_vap_detach(vap);
580 free(wvp, M_80211_VAP);
584 wi_shutdown(device_t dev)
586 struct wi_softc *sc = device_get_softc(dev);
595 struct wi_softc *sc = arg;
596 struct ifnet *ifp = sc->sc_ifp;
601 if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) {
602 CSR_WRITE_2(sc, WI_INT_EN, 0);
603 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
608 /* Disable interrupts. */
609 CSR_WRITE_2(sc, WI_INT_EN, 0);
611 status = CSR_READ_2(sc, WI_EVENT_STAT);
612 if (status & WI_EV_RX)
614 if (status & WI_EV_ALLOC)
616 if (status & WI_EV_TX_EXC)
618 if (status & WI_EV_INFO)
620 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
621 !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
622 wi_start_locked(ifp);
624 /* Re-enable interrupts. */
625 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
633 wi_enable(struct wi_softc *sc)
635 /* Enable interrupts */
636 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
639 wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
644 wi_setup_locked(struct wi_softc *sc, int porttype, int mode,
645 uint8_t mac[IEEE80211_ADDR_LEN])
651 wi_write_val(sc, WI_RID_PORTTYPE, porttype);
652 wi_write_val(sc, WI_RID_CREATE_IBSS, mode);
653 wi_write_val(sc, WI_RID_MAX_DATALEN, 2304);
654 /* XXX IEEE80211_BPF_NOACK wants 0 */
655 wi_write_val(sc, WI_RID_ALT_RETRY_CNT, 2);
656 if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
657 wi_write_val(sc, WI_RID_ROAMING_MODE, 3); /* NB: disabled */
659 wi_write_rid(sc, WI_RID_MAC_NODE, mac, IEEE80211_ADDR_LEN);
661 /* Allocate fids for the card */
662 sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
663 for (i = 0; i < sc->sc_ntxbuf; i++) {
664 int error = wi_alloc_fid(sc, sc->sc_buflen,
665 &sc->sc_txd[i].d_fid);
667 device_printf(sc->sc_dev,
668 "tx buffer allocation failed (error %u)\n",
672 sc->sc_txd[i].d_len = 0;
674 sc->sc_txcur = sc->sc_txnext = 0;
680 wi_init_locked(struct wi_softc *sc)
682 struct ifnet *ifp = sc->sc_ifp;
687 wasenabled = sc->sc_enabled;
689 wi_stop_locked(sc, 1);
691 if (wi_setup_locked(sc, sc->sc_porttype, 3, IF_LLADDR(ifp)) != 0) {
692 if_printf(ifp, "interface not running\n");
693 wi_stop_locked(sc, 1);
697 ifp->if_drv_flags |= IFF_DRV_RUNNING;
698 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
700 callout_reset(&sc->sc_watchdog, hz, wi_watchdog, sc);
702 wi_enable(sc); /* Enable desired port */
708 struct wi_softc *sc = arg;
709 struct ifnet *ifp = sc->sc_ifp;
710 struct ieee80211com *ic = ifp->if_l2com;
716 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
717 ieee80211_start_all(ic); /* start all vap's */
721 wi_stop_locked(struct wi_softc *sc, int disable)
723 struct ifnet *ifp = sc->sc_ifp;
727 if (sc->sc_enabled && !sc->wi_gone) {
728 CSR_WRITE_2(sc, WI_INT_EN, 0);
729 wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
732 } else if (sc->wi_gone && disable) /* gone --> not enabled */
735 callout_stop(&sc->sc_watchdog);
737 sc->sc_false_syns = 0;
739 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE | IFF_DRV_RUNNING);
743 wi_stop(struct wi_softc *sc, int disable)
746 wi_stop_locked(sc, disable);
751 wi_set_channel(struct ieee80211com *ic)
753 struct wi_softc *sc = ic->ic_softc;
755 DPRINTF(("%s: channel %d, %sscanning\n", __func__,
756 ieee80211_chan2ieee(ic, ic->ic_curchan),
757 ic->ic_flags & IEEE80211_F_SCAN ? "" : "!"));
760 wi_write_val(sc, WI_RID_OWN_CHNL,
761 ieee80211_chan2ieee(ic, ic->ic_curchan));
766 wi_scan_start(struct ieee80211com *ic)
768 struct wi_softc *sc = ic->ic_softc;
769 struct ieee80211_scan_state *ss = ic->ic_scan;
771 DPRINTF(("%s\n", __func__));
775 * Switch device to monitor mode.
777 wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_monitor_port);
778 if (sc->sc_firmware_type == WI_INTERSIL) {
779 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
780 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
782 /* force full dwell time to compensate for firmware overhead */
783 ss->ss_mindwell = ss->ss_maxdwell = msecs_to_ticks(400);
789 wi_scan_end(struct ieee80211com *ic)
791 struct wi_softc *sc = ic->ic_softc;
793 DPRINTF(("%s: restore port type %d\n", __func__, sc->sc_porttype));
796 wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_porttype);
797 if (sc->sc_firmware_type == WI_INTERSIL) {
798 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
799 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
805 wi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
806 int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
808 struct ieee80211vap *vap = ni->ni_vap;
811 case IEEE80211_FC0_SUBTYPE_AUTH:
812 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
813 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
814 /* NB: filter frames that trigger state changes */
817 WI_VAP(vap)->wv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
821 wi_newstate_sta(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
823 struct ieee80211com *ic = vap->iv_ic;
824 struct ieee80211_node *bss;
825 struct wi_softc *sc = ic->ic_softc;
827 DPRINTF(("%s: %s -> %s\n", __func__,
828 ieee80211_state_name[vap->iv_state],
829 ieee80211_state_name[nstate]));
831 if (nstate == IEEE80211_S_AUTH) {
833 wi_setup_locked(sc, WI_PORTTYPE_BSS, 3, vap->iv_myaddr);
835 if (vap->iv_flags & IEEE80211_F_PMGTON) {
836 wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
837 wi_write_val(sc, WI_RID_PM_ENABLED, 1);
839 wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold);
840 if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
841 wi_write_val(sc, WI_RID_FRAG_THRESH,
842 vap->iv_fragthreshold);
843 wi_write_txrate(sc, vap);
846 wi_write_ssid(sc, WI_RID_DESIRED_SSID, bss->ni_essid, bss->ni_esslen);
847 wi_write_val(sc, WI_RID_OWN_CHNL,
848 ieee80211_chan2ieee(ic, bss->ni_chan));
851 if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP)
852 wi_write_wep(sc, vap);
854 sc->sc_encryption = 0;
856 if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) &&
857 (vap->iv_flags & IEEE80211_F_WPA)) {
858 wi_write_val(sc, WI_RID_WPA_HANDLING, 1);
859 if (vap->iv_appie_wpa != NULL)
860 wi_write_appie(sc, WI_RID_WPA_DATA,
864 wi_enable(sc); /* enable port */
866 /* Lucent firmware does not support the JOIN RID. */
867 if (sc->sc_firmware_type == WI_INTERSIL) {
868 struct wi_joinreq join;
870 memset(&join, 0, sizeof(join));
871 IEEE80211_ADDR_COPY(&join.wi_bssid, bss->ni_bssid);
872 join.wi_chan = htole16(
873 ieee80211_chan2ieee(ic, bss->ni_chan));
874 wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
879 * NB: don't go through 802.11 layer, it'll send auth frame;
880 * instead we drive the state machine from the link status
881 * notification we get on association.
883 vap->iv_state = nstate;
886 return WI_VAP(vap)->wv_newstate(vap, nstate, arg);
890 wi_newstate_hostap(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
892 struct ieee80211com *ic = vap->iv_ic;
893 struct ieee80211_node *bss;
894 struct wi_softc *sc = ic->ic_softc;
897 DPRINTF(("%s: %s -> %s\n", __func__,
898 ieee80211_state_name[vap->iv_state],
899 ieee80211_state_name[nstate]));
901 error = WI_VAP(vap)->wv_newstate(vap, nstate, arg);
902 if (error == 0 && nstate == IEEE80211_S_RUN) {
904 wi_setup_locked(sc, WI_PORTTYPE_HOSTAP, 0, vap->iv_myaddr);
907 wi_write_ssid(sc, WI_RID_OWN_SSID,
908 bss->ni_essid, bss->ni_esslen);
909 wi_write_val(sc, WI_RID_OWN_CHNL,
910 ieee80211_chan2ieee(ic, bss->ni_chan));
911 wi_write_val(sc, WI_RID_BASIC_RATE, 0x3);
912 wi_write_val(sc, WI_RID_SUPPORT_RATE, 0xf);
913 wi_write_txrate(sc, vap);
915 wi_write_val(sc, WI_RID_OWN_BEACON_INT, bss->ni_intval);
916 wi_write_val(sc, WI_RID_DTIM_PERIOD, vap->iv_dtim_period);
918 wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold);
919 if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
920 wi_write_val(sc, WI_RID_FRAG_THRESH,
921 vap->iv_fragthreshold);
923 if ((sc->sc_flags & WI_FLAGS_HAS_ENHSECURITY) &&
924 (vap->iv_flags & IEEE80211_F_HIDESSID)) {
926 * bit 0 means hide SSID in beacons,
927 * bit 1 means don't respond to bcast probe req
929 wi_write_val(sc, WI_RID_ENH_SECURITY, 0x3);
932 if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) &&
933 (vap->iv_flags & IEEE80211_F_WPA) &&
934 vap->iv_appie_wpa != NULL)
935 wi_write_appie(sc, WI_RID_WPA_DATA, vap->iv_appie_wpa);
937 wi_write_val(sc, WI_RID_PROMISC, 0);
940 if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP)
941 wi_write_wep(sc, vap);
943 sc->sc_encryption = 0;
945 wi_enable(sc); /* enable port */
952 wi_start_locked(struct ifnet *ifp)
954 struct wi_softc *sc = ifp->if_softc;
955 struct ieee80211_node *ni;
956 struct ieee80211_frame *wh;
958 struct ieee80211_key *k;
959 struct wi_frame frmhdr;
960 const struct llc *llc;
968 memset(&frmhdr, 0, sizeof(frmhdr));
971 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
974 if (sc->sc_txd[cur].d_len != 0) {
975 IFQ_DRV_PREPEND(&ifp->if_snd, m0);
976 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
979 ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
981 /* reconstruct 802.3 header */
982 wh = mtod(m0, struct ieee80211_frame *);
983 switch (wh->i_fc[1]) {
984 case IEEE80211_FC1_DIR_TODS:
985 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost,
987 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost,
990 case IEEE80211_FC1_DIR_NODS:
991 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost,
993 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost,
996 case IEEE80211_FC1_DIR_FROMDS:
997 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost,
999 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost,
1003 llc = (const struct llc *)(
1004 mtod(m0, const uint8_t *) + ieee80211_hdrsize(wh));
1005 frmhdr.wi_ehdr.ether_type = llc->llc_snap.ether_type;
1006 frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
1007 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1008 k = ieee80211_crypto_encap(ni, m0);
1010 ieee80211_free_node(ni);
1014 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
1017 if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
1018 sc->sc_tx_th.wt_rate = ni->ni_txrate;
1019 ieee80211_radiotap_tx(ni->ni_vap, m0);
1022 m_copydata(m0, 0, sizeof(struct ieee80211_frame),
1023 (caddr_t)&frmhdr.wi_whdr);
1024 m_adj(m0, sizeof(struct ieee80211_frame));
1025 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
1026 ieee80211_free_node(ni);
1027 if (wi_start_tx(ifp, &frmhdr, m0))
1030 sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
1031 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1036 wi_start(struct ifnet *ifp)
1038 struct wi_softc *sc = ifp->if_softc;
1041 wi_start_locked(ifp);
1046 wi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr, struct mbuf *m0)
1048 struct wi_softc *sc = ifp->if_softc;
1049 int cur = sc->sc_txnext;
1050 int fid, off, error;
1052 fid = sc->sc_txd[cur].d_fid;
1053 off = sizeof(*frmhdr);
1054 error = wi_write_bap(sc, fid, 0, frmhdr, sizeof(*frmhdr)) != 0
1055 || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
1058 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1061 sc->sc_txd[cur].d_len = off;
1062 if (sc->sc_txcur == cur) {
1063 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
1064 if_printf(ifp, "xmit failed\n");
1065 sc->sc_txd[cur].d_len = 0;
1068 sc->sc_tx_timer = 5;
1074 wi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m0,
1075 const struct ieee80211_bpf_params *params)
1077 struct ieee80211com *ic = ni->ni_ic;
1078 struct ifnet *ifp = ic->ic_ifp;
1079 struct ieee80211vap *vap = ni->ni_vap;
1080 struct wi_softc *sc = ic->ic_softc;
1081 struct ieee80211_key *k;
1082 struct ieee80211_frame *wh;
1083 struct wi_frame frmhdr;
1093 memset(&frmhdr, 0, sizeof(frmhdr));
1094 cur = sc->sc_txnext;
1095 if (sc->sc_txd[cur].d_len != 0) {
1096 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1100 m0->m_pkthdr.rcvif = NULL;
1102 m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
1103 (caddr_t)&frmhdr.wi_ehdr);
1104 frmhdr.wi_ehdr.ether_type = 0;
1105 wh = mtod(m0, struct ieee80211_frame *);
1107 frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
1108 if (params && (params->ibp_flags & IEEE80211_BPF_NOACK))
1109 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_ALTRTRY);
1110 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
1111 (!params || (params && (params->ibp_flags & IEEE80211_BPF_CRYPTO)))) {
1112 k = ieee80211_crypto_encap(ni, m0);
1117 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
1119 if (ieee80211_radiotap_active_vap(vap)) {
1120 sc->sc_tx_th.wt_rate = ni->ni_txrate;
1121 ieee80211_radiotap_tx(vap, m0);
1123 m_copydata(m0, 0, sizeof(struct ieee80211_frame),
1124 (caddr_t)&frmhdr.wi_whdr);
1125 m_adj(m0, sizeof(struct ieee80211_frame));
1126 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
1127 if (wi_start_tx(ifp, &frmhdr, m0) < 0) {
1134 sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
1140 ieee80211_free_node(ni);
1145 wi_reset(struct wi_softc *sc)
1147 #define WI_INIT_TRIES 3
1150 for (i = 0; i < WI_INIT_TRIES; i++) {
1151 error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0);
1154 DELAY(WI_DELAY * 1000);
1157 if (i == WI_INIT_TRIES) {
1158 if_printf(sc->sc_ifp, "reset failed\n");
1162 CSR_WRITE_2(sc, WI_INT_EN, 0);
1163 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
1165 /* Calibrate timer. */
1166 wi_write_val(sc, WI_RID_TICK_TIME, 8);
1169 #undef WI_INIT_TRIES
1173 wi_watchdog(void *arg)
1175 struct wi_softc *sc = arg;
1176 struct ifnet *ifp = sc->sc_ifp;
1180 if (!sc->sc_enabled)
1183 if (sc->sc_tx_timer && --sc->sc_tx_timer == 0) {
1184 if_printf(ifp, "device timeout\n");
1185 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1186 wi_init_locked(ifp->if_softc);
1189 callout_reset(&sc->sc_watchdog, hz, wi_watchdog, sc);
1193 wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1195 struct wi_softc *sc = ifp->if_softc;
1196 struct ieee80211com *ic = ifp->if_l2com;
1197 struct ifreq *ifr = (struct ifreq *) data;
1198 int error = 0, startall = 0;
1204 * Can't do promisc and hostap at the same time. If all that's
1205 * changing is the promisc flag, try to short-circuit a call to
1206 * wi_init() by just setting PROMISC in the hardware.
1208 if (ifp->if_flags & IFF_UP) {
1209 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1210 ifp->if_drv_flags & IFF_DRV_RUNNING) {
1211 if ((ifp->if_flags ^ sc->sc_if_flags) & IFF_PROMISC) {
1212 wi_write_val(sc, WI_RID_PROMISC,
1213 (ifp->if_flags & IFF_PROMISC) != 0);
1223 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1224 wi_stop_locked(sc, 1);
1227 sc->sc_if_flags = ifp->if_flags;
1230 ieee80211_start_all(ic);
1233 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1236 error = ether_ioctl(ifp, cmd, data);
1246 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1248 struct ieee80211vap *vap = ifp->if_softc;
1249 struct ieee80211com *ic = vap->iv_ic;
1250 struct wi_softc *sc = ic->ic_softc;
1255 if (sc->sc_enabled &&
1256 wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) == 0 &&
1257 len == sizeof(val)) {
1258 /* convert to 802.11 rate */
1261 if (sc->sc_firmware_type == WI_LUCENT) {
1263 rate = 11; /* 5.5Mbps */
1266 rate = 11; /* 5.5Mbps */
1267 else if (rate == 8*2)
1268 rate = 22; /* 11Mbps */
1270 vap->iv_bss->ni_txrate = rate;
1272 ieee80211_media_status(ifp, imr);
1276 wi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1278 struct ifnet *ifp = sc->sc_ifp;
1279 struct ieee80211com *ic = ifp->if_l2com;
1280 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1281 struct ieee80211_node *ni = vap->iv_bss;
1283 if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1286 DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid)));
1287 DPRINTF(("%s ?\n", ether_sprintf(new_bssid)));
1289 /* In promiscuous mode, the BSSID field is not a reliable
1290 * indicator of the firmware's BSSID. Damp spurious
1291 * change-of-BSSID indications.
1293 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1294 !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns,
1298 sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1);
1301 * XXX hack; we should create a new node with the new bssid
1302 * and replace the existing ic_bss with it but since we don't
1303 * process management frames to collect state we cheat by
1304 * reusing the existing node as we know wi_newstate will be
1305 * called and it will overwrite the node state.
1307 ieee80211_sta_join(ic, ieee80211_ref_node(ni));
1311 static __noinline void
1312 wi_rx_intr(struct wi_softc *sc)
1314 struct ifnet *ifp = sc->sc_ifp;
1315 struct ieee80211com *ic = ifp->if_l2com;
1316 struct wi_frame frmhdr;
1318 struct ieee80211_frame *wh;
1319 struct ieee80211_node *ni;
1325 fid = CSR_READ_2(sc, WI_RX_FID);
1327 /* First read in the frame header */
1328 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1329 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1330 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1331 DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1336 * Drop undecryptable or packets with receive errors here
1338 status = le16toh(frmhdr.wi_status);
1339 if (status & WI_STAT_ERRSTAT) {
1340 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1341 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1342 DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1346 len = le16toh(frmhdr.wi_dat_len);
1347 off = ALIGN(sizeof(struct ieee80211_frame));
1350 * Sometimes the PRISM2.x returns bogusly large frames. Except
1351 * in monitor mode, just throw them away.
1353 if (off + len > MCLBYTES) {
1354 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1355 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1356 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1357 DPRINTF(("wi_rx_intr: oversized packet\n"));
1363 if (off + len > MHLEN)
1364 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1366 m = m_gethdr(M_NOWAIT, MT_DATA);
1368 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1369 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1370 DPRINTF(("wi_rx_intr: MGET failed\n"));
1373 m->m_data += off - sizeof(struct ieee80211_frame);
1374 memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1375 wi_read_bap(sc, fid, sizeof(frmhdr),
1376 m->m_data + sizeof(struct ieee80211_frame), len);
1377 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1378 m->m_pkthdr.rcvif = ifp;
1380 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1382 rssi = frmhdr.wi_rx_signal;
1383 nf = frmhdr.wi_rx_silence;
1384 if (ieee80211_radiotap_active(ic)) {
1385 struct wi_rx_radiotap_header *tap = &sc->sc_rx_th;
1388 rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1389 le16toh(frmhdr.wi_rx_tstamp1);
1390 tap->wr_tsf = htole64((uint64_t)rstamp);
1391 /* XXX replace divide by table */
1392 tap->wr_rate = frmhdr.wi_rx_rate / 5;
1394 if (frmhdr.wi_status & WI_STAT_PCF)
1395 tap->wr_flags |= IEEE80211_RADIOTAP_F_CFP;
1396 if (m->m_flags & M_WEP)
1397 tap->wr_flags |= IEEE80211_RADIOTAP_F_WEP;
1398 tap->wr_antsignal = rssi;
1399 tap->wr_antnoise = nf;
1402 /* synchronize driver's BSSID with firmware's BSSID */
1403 wh = mtod(m, struct ieee80211_frame *);
1404 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1405 if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1406 wi_sync_bssid(sc, wh->i_addr3);
1410 ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1412 (void) ieee80211_input(ni, m, rssi, nf);
1413 ieee80211_free_node(ni);
1415 (void) ieee80211_input_all(ic, m, rssi, nf);
1420 static __noinline void
1421 wi_tx_ex_intr(struct wi_softc *sc)
1423 struct ifnet *ifp = sc->sc_ifp;
1424 struct wi_frame frmhdr;
1427 fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1428 /* Read in the frame header */
1429 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1430 u_int16_t status = le16toh(frmhdr.wi_status);
1432 * Spontaneous station disconnects appear as xmit
1433 * errors. Don't announce them and/or count them
1434 * as an output error.
1436 if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1437 if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1438 if_printf(ifp, "tx failed");
1439 if (status & WI_TXSTAT_RET_ERR)
1440 printf(", retry limit exceeded");
1441 if (status & WI_TXSTAT_AGED_ERR)
1442 printf(", max transmit lifetime exceeded");
1443 if (status & WI_TXSTAT_DISCONNECT)
1444 printf(", port disconnected");
1445 if (status & WI_TXSTAT_FORM_ERR)
1446 printf(", invalid format (data len %u src %6D)",
1447 le16toh(frmhdr.wi_dat_len),
1448 frmhdr.wi_ehdr.ether_shost, ":");
1450 printf(", status=0x%x", status);
1453 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1455 DPRINTF(("port disconnected\n"));
1457 DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
1458 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1461 static __noinline void
1462 wi_tx_intr(struct wi_softc *sc)
1464 struct ifnet *ifp = sc->sc_ifp;
1470 fid = CSR_READ_2(sc, WI_ALLOC_FID);
1471 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1474 if (sc->sc_txd[cur].d_fid != fid) {
1475 if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
1476 fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
1479 sc->sc_tx_timer = 0;
1480 sc->sc_txd[cur].d_len = 0;
1481 sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
1482 if (sc->sc_txd[cur].d_len == 0)
1483 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1485 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1487 if_printf(ifp, "xmit failed\n");
1488 sc->sc_txd[cur].d_len = 0;
1490 sc->sc_tx_timer = 5;
1495 static __noinline void
1496 wi_info_intr(struct wi_softc *sc)
1498 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
1499 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1500 int i, fid, len, off;
1505 fid = CSR_READ_2(sc, WI_INFO_FID);
1506 wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
1508 switch (le16toh(ltbuf[1])) {
1509 case WI_INFO_LINK_STAT:
1510 wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1511 DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1516 switch (le16toh(stat)) {
1517 case WI_INFO_LINK_STAT_CONNECTED:
1518 if (vap->iv_state == IEEE80211_S_RUN &&
1519 vap->iv_opmode != IEEE80211_M_IBSS)
1522 case WI_INFO_LINK_STAT_AP_CHG:
1524 vap->iv_bss->ni_associd = 1 | 0xc000; /* NB: anything will do */
1525 ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
1526 IEEE80211_UNLOCK(ic);
1528 case WI_INFO_LINK_STAT_AP_INR:
1530 case WI_INFO_LINK_STAT_DISCONNECTED:
1531 /* we dropped off the net; e.g. due to deauth/disassoc */
1533 vap->iv_bss->ni_associd = 0;
1534 vap->iv_stats.is_rx_deauth++;
1535 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
1536 IEEE80211_UNLOCK(ic);
1538 case WI_INFO_LINK_STAT_AP_OOR:
1539 /* XXX does this need to be per-vap? */
1540 ieee80211_beacon_miss(ic);
1542 case WI_INFO_LINK_STAT_ASSOC_FAILED:
1543 if (vap->iv_opmode == IEEE80211_M_STA)
1544 ieee80211_new_state(vap, IEEE80211_S_SCAN,
1545 IEEE80211_SCAN_FAIL_TIMEOUT);
1549 case WI_INFO_COUNTERS:
1550 /* some card versions have a larger stats structure */
1551 len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1552 ptr = (u_int32_t *)&sc->sc_stats;
1553 off = sizeof(ltbuf);
1554 for (i = 0; i < len; i++, off += 2, ptr++) {
1555 wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1556 #ifdef WI_HERMES_STATS_WAR
1564 DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1565 le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1569 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1573 wi_get_counter(struct ifnet *ifp, ift_counter cnt)
1575 struct wi_softc *sc;
1577 sc = if_getsoftc(ifp);
1580 case IFCOUNTER_COLLISIONS:
1581 return (sc->sc_stats.wi_tx_single_retries +
1582 sc->sc_stats.wi_tx_multi_retries +
1583 sc->sc_stats.wi_tx_retry_limit);
1585 return (if_get_counter_default(ifp, cnt));
1590 wi_write_multi(struct wi_softc *sc)
1592 struct ifnet *ifp = sc->sc_ifp;
1594 struct ifmultiaddr *ifma;
1595 struct wi_mcast mlist;
1597 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1599 memset(&mlist, 0, sizeof(mlist));
1600 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1605 if_maddr_rlock(ifp);
1606 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1607 if (ifma->ifma_addr->sa_family != AF_LINK)
1611 IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1612 (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1615 if_maddr_runlock(ifp);
1616 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1617 IEEE80211_ADDR_LEN * n);
1621 wi_update_mcast(struct ieee80211com *ic)
1624 wi_write_multi(ic->ic_softc);
1628 wi_update_promisc(struct ieee80211com *ic)
1630 struct wi_softc *sc = ic->ic_softc;
1633 /* XXX handle WEP special case handling? */
1634 wi_write_val(sc, WI_RID_PROMISC,
1635 (ic->ic_opmode == IEEE80211_M_MONITOR ||
1636 (ic->ic_ifp->if_flags & IFF_PROMISC)));
1641 wi_read_nicid(struct wi_softc *sc)
1643 struct wi_card_ident *id;
1648 /* getting chip identity */
1649 memset(ver, 0, sizeof(ver));
1651 wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1653 sc->sc_firmware_type = WI_NOTYPE;
1654 sc->sc_nic_id = le16toh(ver[0]);
1655 for (id = wi_card_ident; id->card_name != NULL; id++) {
1656 if (sc->sc_nic_id == id->card_id) {
1657 sc->sc_nic_name = id->card_name;
1658 sc->sc_firmware_type = id->firm_type;
1662 if (sc->sc_firmware_type == WI_NOTYPE) {
1663 if (sc->sc_nic_id & 0x8000) {
1664 sc->sc_firmware_type = WI_INTERSIL;
1665 sc->sc_nic_name = "Unknown Prism chip";
1667 sc->sc_firmware_type = WI_LUCENT;
1668 sc->sc_nic_name = "Unknown Lucent chip";
1672 device_printf(sc->sc_dev, "using %s\n", sc->sc_nic_name);
1674 /* get primary firmware version (Only Prism chips) */
1675 if (sc->sc_firmware_type != WI_LUCENT) {
1676 memset(ver, 0, sizeof(ver));
1678 wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1679 sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1680 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1683 /* get station firmware version */
1684 memset(ver, 0, sizeof(ver));
1686 wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1687 sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1688 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1689 if (sc->sc_firmware_type == WI_INTERSIL &&
1690 (sc->sc_sta_firmware_ver == 10102 ||
1691 sc->sc_sta_firmware_ver == 20102)) {
1693 memset(ident, 0, sizeof(ident));
1694 len = sizeof(ident);
1695 /* value should be the format like "V2.00-11" */
1696 if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1697 *(p = (char *)ident) >= 'A' &&
1698 p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1699 sc->sc_firmware_type = WI_SYMBOL;
1700 sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1701 (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1702 (p[6] - '0') * 10 + (p[7] - '0');
1706 device_printf(sc->sc_dev, "%s Firmware: ",
1707 wi_firmware_names[sc->sc_firmware_type]);
1708 if (sc->sc_firmware_type != WI_LUCENT) /* XXX */
1709 printf("Primary (%u.%u.%u), ",
1710 sc->sc_pri_firmware_ver / 10000,
1711 (sc->sc_pri_firmware_ver % 10000) / 100,
1712 sc->sc_pri_firmware_ver % 100);
1713 printf("Station (%u.%u.%u)\n",
1714 sc->sc_sta_firmware_ver / 10000,
1715 (sc->sc_sta_firmware_ver % 10000) / 100,
1716 sc->sc_sta_firmware_ver % 100);
1721 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1723 struct wi_ssid ssid;
1725 if (buflen > IEEE80211_NWID_LEN)
1727 memset(&ssid, 0, sizeof(ssid));
1728 ssid.wi_len = htole16(buflen);
1729 memcpy(ssid.wi_ssid, buf, buflen);
1730 return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1734 wi_write_txrate(struct wi_softc *sc, struct ieee80211vap *vap)
1736 static const uint16_t lucent_rates[12] = {
1737 [ 0] = 3, /* auto */
1738 [ 1] = 1, /* 1Mb/s */
1739 [ 2] = 2, /* 2Mb/s */
1740 [ 5] = 4, /* 5.5Mb/s */
1741 [11] = 5 /* 11Mb/s */
1743 static const uint16_t intersil_rates[12] = {
1744 [ 0] = 0xf, /* auto */
1745 [ 1] = 0, /* 1Mb/s */
1746 [ 2] = 1, /* 2Mb/s */
1747 [ 5] = 2, /* 5.5Mb/s */
1748 [11] = 3, /* 11Mb/s */
1750 const uint16_t *rates = sc->sc_firmware_type == WI_LUCENT ?
1751 lucent_rates : intersil_rates;
1752 struct ieee80211com *ic = vap->iv_ic;
1753 const struct ieee80211_txparam *tp;
1755 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
1756 return wi_write_val(sc, WI_RID_TX_RATE,
1757 (tp->ucastrate == IEEE80211_FIXED_RATE_NONE ?
1758 rates[0] : rates[tp->ucastrate / 2]));
1762 wi_write_wep(struct wi_softc *sc, struct ieee80211vap *vap)
1767 struct wi_key wkey[IEEE80211_WEP_NKID];
1769 switch (sc->sc_firmware_type) {
1771 val = (vap->iv_flags & IEEE80211_F_PRIVACY) ? 1 : 0;
1772 error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
1775 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0)
1777 error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, vap->iv_def_txkey);
1780 memset(wkey, 0, sizeof(wkey));
1781 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1782 keylen = vap->iv_nw_keys[i].wk_keylen;
1783 wkey[i].wi_keylen = htole16(keylen);
1784 memcpy(wkey[i].wi_keydat, vap->iv_nw_keys[i].wk_key,
1787 error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
1788 wkey, sizeof(wkey));
1789 sc->sc_encryption = 0;
1793 val = HOST_ENCRYPT | HOST_DECRYPT;
1794 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1796 * ONLY HWB3163 EVAL-CARD Firmware version
1797 * less than 0.8 variant2
1799 * If promiscuous mode disable, Prism2 chip
1800 * does not work with WEP .
1801 * It is under investigation for details.
1802 * (ichiro@netbsd.org)
1804 if (sc->sc_sta_firmware_ver < 802 ) {
1805 /* firm ver < 0.8 variant 2 */
1806 wi_write_val(sc, WI_RID_PROMISC, 1);
1808 wi_write_val(sc, WI_RID_CNFAUTHMODE,
1809 vap->iv_bss->ni_authmode);
1810 val |= PRIVACY_INVOKED;
1812 wi_write_val(sc, WI_RID_CNFAUTHMODE, IEEE80211_AUTH_OPEN);
1814 error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
1817 sc->sc_encryption = val;
1818 if ((val & PRIVACY_INVOKED) == 0)
1820 error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY, vap->iv_def_txkey);
1827 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
1834 /* wait for the busy bit to clear */
1835 for (i = sc->wi_cmd_count; i > 0; i--) { /* 500ms */
1836 if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
1838 DELAY(1*1000); /* 1ms */
1841 device_printf(sc->sc_dev, "%s: busy bit won't clear, cmd 0x%x\n",
1847 CSR_WRITE_2(sc, WI_PARAM0, val0);
1848 CSR_WRITE_2(sc, WI_PARAM1, val1);
1849 CSR_WRITE_2(sc, WI_PARAM2, val2);
1850 CSR_WRITE_2(sc, WI_COMMAND, cmd);
1852 if (cmd == WI_CMD_INI) {
1853 /* XXX: should sleep here. */
1854 DELAY(100*1000); /* 100ms delay for init */
1856 for (i = 0; i < WI_TIMEOUT; i++) {
1858 * Wait for 'command complete' bit to be
1859 * set in the event status register.
1861 s = CSR_READ_2(sc, WI_EVENT_STAT);
1862 if (s & WI_EV_CMD) {
1863 /* Ack the event and read result code. */
1864 s = CSR_READ_2(sc, WI_STATUS);
1865 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
1866 if (s & WI_STAT_CMD_RESULT) {
1874 if (i == WI_TIMEOUT) {
1875 device_printf(sc->sc_dev, "%s: timeout on cmd 0x%04x; "
1876 "event status 0x%04x\n", __func__, cmd, s);
1885 wi_seek_bap(struct wi_softc *sc, int id, int off)
1889 CSR_WRITE_2(sc, WI_SEL0, id);
1890 CSR_WRITE_2(sc, WI_OFF0, off);
1892 for (i = 0; ; i++) {
1893 status = CSR_READ_2(sc, WI_OFF0);
1894 if ((status & WI_OFF_BUSY) == 0)
1896 if (i == WI_TIMEOUT) {
1897 device_printf(sc->sc_dev, "%s: timeout, id %x off %x\n",
1899 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
1900 if (status == 0xffff)
1906 if (status & WI_OFF_ERR) {
1907 device_printf(sc->sc_dev, "%s: error, id %x off %x\n",
1909 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
1913 sc->sc_bap_off = off;
1918 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
1924 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1925 if ((error = wi_seek_bap(sc, id, off)) != 0)
1928 cnt = (buflen + 1) / 2;
1929 CSR_READ_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
1930 sc->sc_bap_off += cnt * 2;
1935 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
1942 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1943 if ((error = wi_seek_bap(sc, id, off)) != 0)
1946 cnt = (buflen + 1) / 2;
1947 CSR_WRITE_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
1948 sc->sc_bap_off += cnt * 2;
1954 wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
1959 for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
1963 len = min(m->m_len, totlen);
1965 if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
1966 m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
1967 return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
1971 if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
1981 wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
1985 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
1986 device_printf(sc->sc_dev, "%s: failed to allocate %d bytes on NIC\n",
1991 for (i = 0; i < WI_TIMEOUT; i++) {
1992 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
1996 if (i == WI_TIMEOUT) {
1997 device_printf(sc->sc_dev, "%s: timeout in alloc\n", __func__);
2000 *idp = CSR_READ_2(sc, WI_ALLOC_FID);
2001 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
2006 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
2011 /* Tell the NIC to enter record read mode. */
2012 error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2016 error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2020 if (le16toh(ltbuf[1]) != rid) {
2021 device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n",
2022 rid, le16toh(ltbuf[1]));
2025 len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */
2026 if (*buflenp < len) {
2027 device_printf(sc->sc_dev, "record buffer is too small, "
2028 "rid=%x, size=%d, len=%d\n",
2029 rid, *buflenp, len);
2033 return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2037 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2042 ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */
2043 ltbuf[1] = htole16(rid);
2045 error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2047 device_printf(sc->sc_dev, "%s: bap0 write failure, rid 0x%x\n",
2051 error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2053 device_printf(sc->sc_dev, "%s: bap1 write failure, rid 0x%x\n",
2058 return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
2062 wi_write_appie(struct wi_softc *sc, int rid, const struct ieee80211_appie *ie)
2064 /* NB: 42 bytes is probably ok to have on the stack */
2065 char buf[sizeof(uint16_t) + 40];
2067 if (ie->ie_len > 40)
2069 /* NB: firmware requires 16-bit ie length before ie data */
2070 *(uint16_t *) buf = htole16(ie->ie_len);
2071 memcpy(buf + sizeof(uint16_t), ie->ie_data, ie->ie_len);
2072 return wi_write_rid(sc, rid, buf, ie->ie_len + sizeof(uint16_t));
2076 wi_alloc(device_t dev, int rid)
2078 struct wi_softc *sc = device_get_softc(dev);
2080 if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2081 sc->iobase_rid = rid;
2082 sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2083 &sc->iobase_rid, 0, ~0, (1 << 6),
2084 rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2085 if (sc->iobase == NULL) {
2086 device_printf(dev, "No I/O space?!\n");
2090 sc->wi_io_addr = rman_get_start(sc->iobase);
2091 sc->wi_btag = rman_get_bustag(sc->iobase);
2092 sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2095 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2096 &sc->mem_rid, RF_ACTIVE);
2097 if (sc->mem == NULL) {
2098 device_printf(dev, "No Mem space on prism2.5?\n");
2102 sc->wi_btag = rman_get_bustag(sc->mem);
2103 sc->wi_bhandle = rman_get_bushandle(sc->mem);
2107 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2109 ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2110 if (sc->irq == NULL) {
2112 device_printf(dev, "No irq?!\n");
2117 sc->sc_unit = device_get_unit(dev);
2122 wi_free(device_t dev)
2124 struct wi_softc *sc = device_get_softc(dev);
2126 if (sc->iobase != NULL) {
2127 bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2130 if (sc->irq != NULL) {
2131 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2134 if (sc->mem != NULL) {
2135 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);