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.
32 * $FreeBSD: head/sys/dev/wi/if_wi.c 196970 2009-09-08 13:19:05Z phk $
36 * Lucent WaveLAN/IEEE 802.11 PCMCIA driver.
38 * Original FreeBSD driver written by Bill Paul <wpaul@ctr.columbia.edu>
39 * Electrical Engineering Department
40 * Columbia University, New York City
44 * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
45 * from Lucent. Unlike the older cards, the new ones are programmed
46 * entirely via a firmware-driven controller called the Hermes.
47 * Unfortunately, Lucent will not release the Hermes programming manual
48 * without an NDA (if at all). What they do release is an API library
49 * called the HCF (Hardware Control Functions) which is supposed to
50 * do the device-specific operations of a device driver for you. The
51 * publically available version of the HCF library (the 'HCF Light') is
52 * a) extremely gross, b) lacks certain features, particularly support
53 * for 802.11 frames, and c) is contaminated by the GNU Public License.
55 * This driver does not use the HCF or HCF Light at all. Instead, it
56 * programs the Hermes controller directly, using information gleaned
57 * from the HCF Light code and corresponding documentation.
59 * This driver supports the ISA, PCMCIA and PCI versions of the Lucent
60 * WaveLan cards (based on the Hermes chipset), as well as the newer
61 * Prism 2 chipsets with firmware from Intersil and Symbol.
65 #define WI_HERMES_STATS_WAR /* Work around stats counter bug. */
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/endian.h>
70 #include <sys/sockio.h>
74 #include <sys/kernel.h>
75 #include <sys/socket.h>
76 #include <sys/module.h>
78 #include <sys/random.h>
79 #include <sys/syslog.h>
80 #include <sys/sysctl.h>
82 #include <machine/atomic.h>
86 #include <net/if_arp.h>
87 #include <net/ethernet.h>
88 #include <net/if_dl.h>
89 #include <net/if_llc.h>
90 #include <net/if_media.h>
91 #include <net/if_types.h>
92 #include <net/ifq_var.h>
94 #include <netproto/802_11/ieee80211_var.h>
95 #include <netproto/802_11/ieee80211_ioctl.h>
96 #include <netproto/802_11/ieee80211_radiotap.h>
98 #include <netinet/in.h>
99 #include <netinet/in_systm.h>
100 #include <netinet/in_var.h>
101 #include <netinet/ip.h>
102 #include <netinet/if_ether.h>
106 #include <dev/netif/wi/if_wavelan_ieee.h>
107 #include <dev/netif/wi/if_wireg.h>
108 #include <dev/netif/wi/if_wivar.h>
110 static struct ieee80211vap *wi_vap_create(struct ieee80211com *ic,
111 const char name[IFNAMSIZ], int unit,
112 enum ieee80211_opmode opmode, int flags,
113 const uint8_t bssid[IEEE80211_ADDR_LEN],
114 const uint8_t mac[IEEE80211_ADDR_LEN]);
115 static void wi_vap_delete(struct ieee80211vap *vap);
116 static void wi_stop_locked(struct wi_softc *sc, int disable);
117 static void wi_start_locked(struct ifnet *);
118 static void wi_start(struct ifnet *, struct ifaltq_subque *);
119 static int wi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr,
121 static int wi_raw_xmit(struct ieee80211_node *, struct mbuf *,
122 const struct ieee80211_bpf_params *);
123 static int wi_newstate_sta(struct ieee80211vap *, enum ieee80211_state, int);
124 static int wi_newstate_hostap(struct ieee80211vap *, enum ieee80211_state,
126 static void wi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
127 int subtype, const struct ieee80211_rx_stats *rxs,
129 static int wi_reset(struct wi_softc *);
130 static void wi_watchdog_callout(void *);
131 static int wi_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
132 static void wi_media_status(struct ifnet *, struct ifmediareq *);
134 static void wi_rx_intr(struct wi_softc *);
135 static void wi_tx_intr(struct wi_softc *);
136 static void wi_tx_ex_intr(struct wi_softc *);
138 static void wi_info_intr(struct wi_softc *);
140 static int wi_write_txrate(struct wi_softc *, struct ieee80211vap *);
141 static int wi_write_wep(struct wi_softc *, struct ieee80211vap *);
142 static int wi_write_multi(struct wi_softc *);
143 static void wi_update_mcast(struct ieee80211com *);
144 static void wi_update_promisc(struct ieee80211com *);
145 static int wi_alloc_fid(struct wi_softc *, int, int *);
146 static void wi_read_nicid(struct wi_softc *);
147 static int wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
149 static int wi_cmd(struct wi_softc *, int, int, int, int);
150 static int wi_seek_bap(struct wi_softc *, int, int);
151 static int wi_read_bap(struct wi_softc *, int, int, void *, int);
152 static int wi_write_bap(struct wi_softc *, int, int, void *, int);
153 static int wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
154 static int wi_read_rid(struct wi_softc *, int, void *, int *);
155 static int wi_write_rid(struct wi_softc *, int, void *, int);
156 static int wi_write_appie(struct wi_softc *, int, const struct ieee80211_appie *);
158 static void wi_scan_start(struct ieee80211com *);
159 static void wi_scan_end(struct ieee80211com *);
160 static void wi_set_channel(struct ieee80211com *);
163 wi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
167 return wi_write_rid(sc, rid, &val, sizeof(val));
170 SYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters");
172 static struct timeval lasttxerror; /* time of last tx error msg */
173 static int curtxeps; /* current tx error msgs/sec */
174 static int wi_txerate = 0; /* tx error rate: max msgs/sec */
175 SYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate,
176 0, "max tx error msgs/sec; 0 to disable msgs");
180 static int wi_debug = 0;
181 SYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug,
182 0, "control debugging printfs");
183 #define DPRINTF(X) if (wi_debug) kprintf X
188 #define WI_INTRS (WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
190 struct wi_card_ident wi_card_ident[] = {
191 /* CARD_ID CARD_NAME FIRM_TYPE */
192 { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT },
193 { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT },
194 { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT },
195 { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL },
196 { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL },
197 { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL },
198 { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL },
199 { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL },
200 { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL },
201 { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL },
202 { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL },
203 { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL },
204 { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
205 { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
206 { WI_NIC_3842_PCMCIA_ATL_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
207 { WI_NIC_3842_PCMCIA_ATS_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
208 { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
209 { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
210 { WI_NIC_3842_MINI_ATL_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
211 { WI_NIC_3842_MINI_ATS_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
212 { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
213 { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
214 { WI_NIC_3842_PCI_ATS_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
215 { WI_NIC_3842_PCI_ATL_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
216 { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
217 { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
218 { WI_NIC_P3_PCMCIA_ATL_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
219 { WI_NIC_P3_PCMCIA_ATS_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
220 { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
221 { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
222 { WI_NIC_P3_MINI_ATL_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
223 { WI_NIC_P3_MINI_ATS_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
227 static char *wi_firmware_names[] = { "none", "Hermes", "Intersil", "Symbol" };
229 devclass_t wi_devclass;
232 wi_attach(device_t dev)
234 struct wi_softc *sc = device_get_softc(dev);
235 struct ieee80211com *ic;
237 int i, nrates, buflen;
239 u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
240 struct ieee80211_rateset *rs;
241 struct sysctl_ctx_list *sctx;
242 struct sysctl_oid *soid;
243 static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
244 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
247 uint8_t macaddr[IEEE80211_ADDR_LEN];
249 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
251 device_printf(dev, "can not if_alloc\n");
257 sc->sc_firmware_type = WI_NOTYPE;
258 sc->wi_cmd_count = 500;
260 if (wi_reset(sc) != 0) {
262 return ENXIO; /* XXX */
265 /* Read NIC identification */
267 switch (sc->sc_firmware_type) {
269 if (sc->sc_sta_firmware_ver < 60006)
273 if (sc->sc_sta_firmware_ver < 800)
278 device_printf(dev, "Sorry, this card is not supported "
279 "(type %d, firmware ver %d)\n",
280 sc->sc_firmware_type, sc->sc_sta_firmware_ver);
285 /* Export info about the device via sysctl */
286 sctx = device_get_sysctl_ctx(sc->sc_dev);
287 soid = device_get_sysctl_tree(sc->sc_dev);
288 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
289 "firmware_type", CTLFLAG_RD,
290 wi_firmware_names[sc->sc_firmware_type], 0,
291 "Firmware type string");
292 SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "sta_version",
293 CTLFLAG_RD, &sc->sc_sta_firmware_ver, 0,
294 "Station Firmware version");
295 if (sc->sc_firmware_type == WI_INTERSIL)
296 SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
297 "pri_version", CTLFLAG_RD, &sc->sc_pri_firmware_ver, 0,
298 "Primary Firmware version");
299 SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "nic_id",
300 CTLFLAG_RD, &sc->sc_nic_id, 0, "NIC id");
301 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "nic_name",
302 CTLFLAG_RD, sc->sc_nic_name, 0, "NIC name");
304 callout_init(&sc->sc_watchdog);
307 * Read the station address.
308 * And do it twice. I've seen PRISM-based cards that return
309 * an error when trying to read it the first time, which causes
312 buflen = IEEE80211_ADDR_LEN;
313 error = wi_read_rid(sc, WI_RID_MAC_NODE, macaddr, &buflen);
315 buflen = IEEE80211_ADDR_LEN;
316 error = wi_read_rid(sc, WI_RID_MAC_NODE, macaddr, &buflen);
318 if (error || IEEE80211_ADDR_EQ(macaddr, empty_macaddr)) {
320 device_printf(dev, "mac read failed %d\n", error);
322 device_printf(dev, "mac read failed (all zeros)\n");
330 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
331 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
332 ifp->if_ioctl = wi_ioctl;
333 ifp->if_start = wi_start;
334 ifp->if_init = wi_init;
335 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
337 ifq_set_ready(&ifp->if_snd);
342 ic->ic_name = device_get_nameunit(dev);
343 ic->ic_phytype = IEEE80211_T_DS;
344 ic->ic_opmode = IEEE80211_M_STA;
345 ic->ic_caps = IEEE80211_C_STA
347 | IEEE80211_C_MONITOR
351 * Query the card for available channels and setup the
352 * channel table. We assume these are all 11b channels.
354 buflen = sizeof(val);
355 if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
356 val = htole16(0x1fff); /* assume 1-11 */
357 KASSERT(val != 0, ("wi_attach: no available channels listed!"));
359 val <<= 1; /* shift for base 1 indices */
360 for (i = 1; i < 16; i++) {
361 struct ieee80211_channel *c;
363 if (!isset((u_int8_t*)&val, i))
365 c = &ic->ic_channels[ic->ic_nchans++];
366 c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
367 c->ic_flags = IEEE80211_CHAN_B;
373 * Set flags based on firmware version.
375 switch (sc->sc_firmware_type) {
378 ic->ic_caps |= IEEE80211_C_IBSS;
380 sc->sc_ibss_port = WI_PORTTYPE_BSS;
381 sc->sc_monitor_port = WI_PORTTYPE_ADHOC;
382 sc->sc_min_rssi = WI_LUCENT_MIN_RSSI;
383 sc->sc_max_rssi = WI_LUCENT_MAX_RSSI;
384 sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
387 sc->sc_ntxbuf = WI_NTXBUF;
388 sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR
389 | WI_FLAGS_HAS_ROAMING;
391 * Old firmware are slow, so give peace a chance.
393 if (sc->sc_sta_firmware_ver < 10000)
394 sc->wi_cmd_count = 5000;
395 if (sc->sc_sta_firmware_ver > 10101)
396 sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
397 ic->ic_caps |= IEEE80211_C_IBSS;
399 * version 0.8.3 and newer are the only ones that are known
400 * to currently work. Earlier versions can be made to work,
401 * at least according to the Linux driver but we require
402 * monitor mode so this is irrelevant.
404 ic->ic_caps |= IEEE80211_C_HOSTAP;
405 if (sc->sc_sta_firmware_ver >= 10603)
406 sc->sc_flags |= WI_FLAGS_HAS_ENHSECURITY;
407 if (sc->sc_sta_firmware_ver >= 10700) {
409 * 1.7.0+ have the necessary support for sta mode WPA.
411 sc->sc_flags |= WI_FLAGS_HAS_WPASUPPORT;
412 ic->ic_caps |= IEEE80211_C_WPA;
415 sc->sc_ibss_port = WI_PORTTYPE_IBSS;
416 sc->sc_monitor_port = WI_PORTTYPE_APSILENT;
417 sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
418 sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
419 sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
424 * Find out if we support WEP on this card.
426 buflen = sizeof(val);
427 if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
429 ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
431 /* Find supported rates. */
432 buflen = sizeof(ratebuf);
433 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
434 if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
435 nrates = le16toh(*(u_int16_t *)ratebuf);
436 if (nrates > IEEE80211_RATE_MAXSIZE)
437 nrates = IEEE80211_RATE_MAXSIZE;
439 for (i = 0; i < nrates; i++)
441 rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i];
443 /* XXX fallback on error? */
446 buflen = sizeof(val);
447 if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
448 wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
449 sc->sc_dbm_offset = le16toh(val);
452 sc->sc_portnum = WI_DEFAULT_PORT;
454 ieee80211_ifattach(ic, macaddr);
455 ic->ic_raw_xmit = wi_raw_xmit;
456 ic->ic_scan_start = wi_scan_start;
457 ic->ic_scan_end = wi_scan_end;
458 ic->ic_set_channel = wi_set_channel;
460 ic->ic_vap_create = wi_vap_create;
461 ic->ic_vap_delete = wi_vap_delete;
462 ic->ic_update_mcast = wi_update_mcast;
463 ic->ic_update_promisc = wi_update_promisc;
465 ieee80211_radiotap_attach(ic,
466 &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
467 WI_TX_RADIOTAP_PRESENT,
468 &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
469 WI_RX_RADIOTAP_PRESENT);
472 ieee80211_announce(ic);
474 error = bus_setup_intr(dev, sc->irq, INTR_MPSAFE,
475 wi_intr, sc, &sc->wi_intrhand, NULL);
477 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
478 ieee80211_ifdetach(ic);
488 wi_detach(device_t dev)
490 struct wi_softc *sc = device_get_softc(dev);
491 struct ifnet *ifp = sc->sc_ifp;
492 struct ieee80211com *ic = ifp->if_l2com;
494 /* check if device was removed */
495 sc->wi_gone |= !bus_child_present(dev);
497 wi_stop_locked(sc, 0);
498 ieee80211_ifdetach(ic);
500 bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
506 static struct ieee80211vap *
507 wi_vap_create(struct ieee80211com *ic,
508 const char name[IFNAMSIZ], int unit,
509 enum ieee80211_opmode opmode, int flags,
510 const uint8_t bssid[IEEE80211_ADDR_LEN],
511 const uint8_t mac[IEEE80211_ADDR_LEN])
513 struct wi_softc *sc = ic->ic_ifp->if_softc;
515 struct ieee80211vap *vap;
517 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
519 wvp = (struct wi_vap *) kmalloc(sizeof(struct wi_vap),
520 M_80211_VAP, M_NOWAIT | M_ZERO);
525 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
527 vap->iv_max_aid = WI_MAX_AID;
530 case IEEE80211_M_STA:
531 sc->sc_porttype = WI_PORTTYPE_BSS;
532 wvp->wv_newstate = vap->iv_newstate;
533 vap->iv_newstate = wi_newstate_sta;
534 /* need to filter mgt frames to avoid confusing state machine */
535 wvp->wv_recv_mgmt = vap->iv_recv_mgmt;
536 vap->iv_recv_mgmt = wi_recv_mgmt;
538 case IEEE80211_M_IBSS:
539 sc->sc_porttype = sc->sc_ibss_port;
540 wvp->wv_newstate = vap->iv_newstate;
541 vap->iv_newstate = wi_newstate_sta;
543 case IEEE80211_M_AHDEMO:
544 sc->sc_porttype = WI_PORTTYPE_ADHOC;
546 case IEEE80211_M_HOSTAP:
547 sc->sc_porttype = WI_PORTTYPE_HOSTAP;
548 wvp->wv_newstate = vap->iv_newstate;
549 vap->iv_newstate = wi_newstate_hostap;
551 case IEEE80211_M_MONITOR:
552 sc->sc_porttype = sc->sc_monitor_port;
559 ieee80211_vap_attach(vap, ieee80211_media_change, wi_media_status);
560 ic->ic_opmode = opmode;
565 wi_vap_delete(struct ieee80211vap *vap)
567 struct wi_vap *wvp = WI_VAP(vap);
569 ieee80211_vap_detach(vap);
570 kfree(wvp, M_80211_VAP);
574 wi_shutdown(device_t dev)
576 struct wi_softc *sc = device_get_softc(dev);
585 struct wi_softc *sc = arg;
586 struct ifnet *ifp = sc->sc_ifp;
589 if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) {
590 CSR_WRITE_2(sc, WI_INT_EN, 0);
591 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
595 /* Disable interrupts. */
596 CSR_WRITE_2(sc, WI_INT_EN, 0);
598 status = CSR_READ_2(sc, WI_EVENT_STAT);
599 if (status & WI_EV_RX)
601 if (status & WI_EV_ALLOC)
603 if (status & WI_EV_TX_EXC)
605 if (status & WI_EV_INFO)
607 if (!ifq_is_oactive(&ifp->if_snd) && !ifq_is_empty(&ifp->if_snd))
608 wi_start_locked(ifp);
610 /* Re-enable interrupts. */
611 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
617 wi_enable(struct wi_softc *sc)
619 /* Enable interrupts */
620 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
623 wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
628 wi_setup_locked(struct wi_softc *sc, int porttype, int mode,
629 uint8_t mac[IEEE80211_ADDR_LEN])
635 wi_write_val(sc, WI_RID_PORTTYPE, porttype);
636 wi_write_val(sc, WI_RID_CREATE_IBSS, mode);
637 wi_write_val(sc, WI_RID_MAX_DATALEN, 2304);
638 /* XXX IEEE80211_BPF_NOACK wants 0 */
639 wi_write_val(sc, WI_RID_ALT_RETRY_CNT, 2);
640 if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
641 wi_write_val(sc, WI_RID_ROAMING_MODE, 3); /* NB: disabled */
643 wi_write_rid(sc, WI_RID_MAC_NODE, mac, IEEE80211_ADDR_LEN);
645 /* Allocate fids for the card */
646 sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
647 for (i = 0; i < sc->sc_ntxbuf; i++) {
648 int error = wi_alloc_fid(sc, sc->sc_buflen,
649 &sc->sc_txd[i].d_fid);
651 device_printf(sc->sc_dev,
652 "tx buffer allocation failed (error %u)\n",
656 sc->sc_txd[i].d_len = 0;
658 sc->sc_txcur = sc->sc_txnext = 0;
664 wi_init_locked(struct wi_softc *sc)
666 struct ifnet *ifp = sc->sc_ifp;
669 wasenabled = sc->sc_enabled;
671 wi_stop_locked(sc, 1);
673 if (wi_setup_locked(sc, sc->sc_porttype, 3, IF_LLADDR(ifp)) != 0) {
674 if_printf(ifp, "interface not running\n");
675 wi_stop_locked(sc, 1);
679 ifp->if_flags |= IFF_RUNNING;
680 ifq_clr_oactive(&ifp->if_snd);
682 callout_reset(&sc->sc_watchdog, hz, wi_watchdog_callout, sc);
684 wi_enable(sc); /* Enable desired port */
690 struct wi_softc *sc = arg;
691 struct ifnet *ifp = sc->sc_ifp;
692 struct ieee80211com *ic = ifp->if_l2com;
696 if (ifp->if_flags & IFF_RUNNING)
697 ieee80211_start_all(ic); /* start all vap's */
701 wi_stop_locked(struct wi_softc *sc, int disable)
703 struct ifnet *ifp = sc->sc_ifp;
705 if (sc->sc_enabled && !sc->wi_gone) {
706 CSR_WRITE_2(sc, WI_INT_EN, 0);
707 wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
710 } else if (sc->wi_gone && disable) /* gone --> not enabled */
713 callout_stop(&sc->sc_watchdog);
715 sc->sc_false_syns = 0;
717 ifp->if_flags &= ~IFF_RUNNING;
718 ifq_clr_oactive(&ifp->if_snd);
722 wi_stop(struct wi_softc *sc, int disable)
724 wi_stop_locked(sc, disable);
728 wi_set_channel(struct ieee80211com *ic)
730 struct ifnet *ifp = ic->ic_ifp;
731 struct wi_softc *sc = ifp->if_softc;
733 DPRINTF(("%s: channel %d, %sscanning\n", __func__,
734 ieee80211_chan2ieee(ic, ic->ic_curchan),
735 ic->ic_flags & IEEE80211_F_SCAN ? "" : "!"));
737 wi_write_val(sc, WI_RID_OWN_CHNL,
738 ieee80211_chan2ieee(ic, ic->ic_curchan));
742 wi_scan_start(struct ieee80211com *ic)
744 struct ifnet *ifp = ic->ic_ifp;
745 struct wi_softc *sc = ifp->if_softc;
746 struct ieee80211_scan_state *ss = ic->ic_scan;
748 DPRINTF(("%s\n", __func__));
751 * Switch device to monitor mode.
753 wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_monitor_port);
754 if (sc->sc_firmware_type == WI_INTERSIL) {
755 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
756 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
758 /* force full dwell time to compensate for firmware overhead */
759 ss->ss_mindwell = ss->ss_maxdwell = msecs_to_ticks(400);
764 wi_scan_end(struct ieee80211com *ic)
766 struct ifnet *ifp = ic->ic_ifp;
767 struct wi_softc *sc = ifp->if_softc;
769 DPRINTF(("%s: restore port type %d\n", __func__, sc->sc_porttype));
771 wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_porttype);
772 if (sc->sc_firmware_type == WI_INTERSIL) {
773 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
774 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
779 wi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
780 int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
782 struct ieee80211vap *vap = ni->ni_vap;
785 case IEEE80211_FC0_SUBTYPE_AUTH:
786 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
787 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
788 /* NB: filter frames that trigger state changes */
791 WI_VAP(vap)->wv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
795 wi_newstate_sta(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
797 struct ieee80211com *ic = vap->iv_ic;
798 struct ifnet *ifp = ic->ic_ifp;
799 struct ieee80211_node *bss;
800 struct wi_softc *sc = ifp->if_softc;
802 DPRINTF(("%s: %s -> %s\n", __func__,
803 ieee80211_state_name[vap->iv_state],
804 ieee80211_state_name[nstate]));
806 if (nstate == IEEE80211_S_AUTH) {
807 wi_setup_locked(sc, WI_PORTTYPE_BSS, 3, vap->iv_myaddr);
809 if (vap->iv_flags & IEEE80211_F_PMGTON) {
810 wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
811 wi_write_val(sc, WI_RID_PM_ENABLED, 1);
813 wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold);
814 if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
815 wi_write_val(sc, WI_RID_FRAG_THRESH,
816 vap->iv_fragthreshold);
817 wi_write_txrate(sc, vap);
820 wi_write_ssid(sc, WI_RID_DESIRED_SSID, bss->ni_essid, bss->ni_esslen);
821 wi_write_val(sc, WI_RID_OWN_CHNL,
822 ieee80211_chan2ieee(ic, bss->ni_chan));
825 if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP)
826 wi_write_wep(sc, vap);
828 sc->sc_encryption = 0;
830 if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) &&
831 (vap->iv_flags & IEEE80211_F_WPA)) {
832 wi_write_val(sc, WI_RID_WPA_HANDLING, 1);
833 if (vap->iv_appie_wpa != NULL)
834 wi_write_appie(sc, WI_RID_WPA_DATA,
838 wi_enable(sc); /* enable port */
840 /* Lucent firmware does not support the JOIN RID. */
841 if (sc->sc_firmware_type == WI_INTERSIL) {
842 struct wi_joinreq join;
844 memset(&join, 0, sizeof(join));
845 IEEE80211_ADDR_COPY(&join.wi_bssid, bss->ni_bssid);
846 join.wi_chan = htole16(
847 ieee80211_chan2ieee(ic, bss->ni_chan));
848 wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
852 * NB: don't go through 802.11 layer, it'll send auth frame;
853 * instead we drive the state machine from the link status
854 * notification we get on association.
856 vap->iv_state = nstate;
859 return WI_VAP(vap)->wv_newstate(vap, nstate, arg);
863 wi_newstate_hostap(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
865 struct ieee80211com *ic = vap->iv_ic;
866 struct ifnet *ifp = ic->ic_ifp;
867 struct ieee80211_node *bss;
868 struct wi_softc *sc = ifp->if_softc;
871 DPRINTF(("%s: %s -> %s\n", __func__,
872 ieee80211_state_name[vap->iv_state],
873 ieee80211_state_name[nstate]));
875 error = WI_VAP(vap)->wv_newstate(vap, nstate, arg);
876 if (error == 0 && nstate == IEEE80211_S_RUN) {
877 wi_setup_locked(sc, WI_PORTTYPE_HOSTAP, 0, vap->iv_myaddr);
880 wi_write_ssid(sc, WI_RID_OWN_SSID,
881 bss->ni_essid, bss->ni_esslen);
882 wi_write_val(sc, WI_RID_OWN_CHNL,
883 ieee80211_chan2ieee(ic, bss->ni_chan));
884 wi_write_val(sc, WI_RID_BASIC_RATE, 0x3);
885 wi_write_val(sc, WI_RID_SUPPORT_RATE, 0xf);
886 wi_write_txrate(sc, vap);
888 wi_write_val(sc, WI_RID_OWN_BEACON_INT, bss->ni_intval);
889 wi_write_val(sc, WI_RID_DTIM_PERIOD, vap->iv_dtim_period);
891 wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold);
892 if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
893 wi_write_val(sc, WI_RID_FRAG_THRESH,
894 vap->iv_fragthreshold);
896 if ((sc->sc_flags & WI_FLAGS_HAS_ENHSECURITY) &&
897 (vap->iv_flags & IEEE80211_F_HIDESSID)) {
899 * bit 0 means hide SSID in beacons,
900 * bit 1 means don't respond to bcast probe req
902 wi_write_val(sc, WI_RID_ENH_SECURITY, 0x3);
905 if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) &&
906 (vap->iv_flags & IEEE80211_F_WPA) &&
907 vap->iv_appie_wpa != NULL)
908 wi_write_appie(sc, WI_RID_WPA_DATA, vap->iv_appie_wpa);
910 wi_write_val(sc, WI_RID_PROMISC, 0);
913 if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP)
914 wi_write_wep(sc, vap);
916 sc->sc_encryption = 0;
918 wi_enable(sc); /* enable port */
924 wi_start_locked(struct ifnet *ifp)
926 struct wi_softc *sc = ifp->if_softc;
927 struct ieee80211_node *ni;
928 struct ieee80211_frame *wh;
930 struct ieee80211_key *k;
931 struct wi_frame frmhdr;
932 const struct llc *llc;
938 memset(&frmhdr, 0, sizeof(frmhdr));
941 m0 = ifq_dequeue(&ifp->if_snd);
944 if (sc->sc_txd[cur].d_len != 0) {
945 ifq_prepend(&ifp->if_snd, m0);
946 ifq_set_oactive(&ifp->if_snd);
949 ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
951 /* reconstruct 802.3 header */
952 wh = mtod(m0, struct ieee80211_frame *);
953 switch (wh->i_fc[1]) {
954 case IEEE80211_FC1_DIR_TODS:
955 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost,
957 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost,
960 case IEEE80211_FC1_DIR_NODS:
961 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost,
963 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost,
966 case IEEE80211_FC1_DIR_FROMDS:
967 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost,
969 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost,
973 llc = (const struct llc *)(
974 mtod(m0, const uint8_t *) + ieee80211_hdrsize(wh));
975 frmhdr.wi_ehdr.ether_type = llc->llc_snap.ether_type;
976 frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
977 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
978 k = ieee80211_crypto_encap(ni, m0);
980 ieee80211_free_node(ni);
984 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
987 if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
988 sc->sc_tx_th.wt_rate = ni->ni_txrate;
989 ieee80211_radiotap_tx(ni->ni_vap, m0);
992 m_copydata(m0, 0, sizeof(struct ieee80211_frame),
993 (caddr_t)&frmhdr.wi_whdr);
994 m_adj(m0, sizeof(struct ieee80211_frame));
995 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
996 ieee80211_free_node(ni);
997 if (wi_start_tx(ifp, &frmhdr, m0))
1000 sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
1001 IFNET_STAT_INC(ifp, opackets, 1);
1006 wi_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1008 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
1009 wi_start_locked(ifp);
1013 wi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr, struct mbuf *m0)
1015 struct wi_softc *sc = ifp->if_softc;
1016 int cur = sc->sc_txnext;
1017 int fid, off, error;
1019 fid = sc->sc_txd[cur].d_fid;
1020 off = sizeof(*frmhdr);
1021 error = wi_write_bap(sc, fid, 0, frmhdr, sizeof(*frmhdr)) != 0
1022 || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
1025 IFNET_STAT_INC(ifp, oerrors, 1);
1028 sc->sc_txd[cur].d_len = off;
1029 if (sc->sc_txcur == cur) {
1030 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
1031 if_printf(ifp, "xmit failed\n");
1032 sc->sc_txd[cur].d_len = 0;
1035 sc->sc_tx_timer = 5;
1041 wi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m0,
1042 const struct ieee80211_bpf_params *params)
1044 struct ieee80211com *ic = ni->ni_ic;
1045 struct ifnet *ifp = ic->ic_ifp;
1046 struct ieee80211vap *vap = ni->ni_vap;
1047 struct wi_softc *sc = ifp->if_softc;
1048 struct ieee80211_key *k;
1049 struct ieee80211_frame *wh;
1050 struct wi_frame frmhdr;
1058 memset(&frmhdr, 0, sizeof(frmhdr));
1059 cur = sc->sc_txnext;
1060 if (sc->sc_txd[cur].d_len != 0) {
1061 ifq_set_oactive(&ifp->if_snd);
1065 m0->m_pkthdr.rcvif = NULL;
1067 m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
1068 (caddr_t)&frmhdr.wi_ehdr);
1069 frmhdr.wi_ehdr.ether_type = 0;
1070 wh = mtod(m0, struct ieee80211_frame *);
1072 frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
1073 if (params && (params->ibp_flags & IEEE80211_BPF_NOACK))
1074 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_ALTRTRY);
1075 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
1076 (!params || (params && (params->ibp_flags & IEEE80211_BPF_CRYPTO)))) {
1077 k = ieee80211_crypto_encap(ni, m0);
1082 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
1084 if (ieee80211_radiotap_active_vap(vap)) {
1085 sc->sc_tx_th.wt_rate = ni->ni_txrate;
1086 ieee80211_radiotap_tx(vap, m0);
1088 m_copydata(m0, 0, sizeof(struct ieee80211_frame),
1089 (caddr_t)&frmhdr.wi_whdr);
1090 m_adj(m0, sizeof(struct ieee80211_frame));
1091 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
1092 if (wi_start_tx(ifp, &frmhdr, m0) < 0) {
1099 sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
1104 ieee80211_free_node(ni);
1109 wi_reset(struct wi_softc *sc)
1111 #define WI_INIT_TRIES 3
1114 for (i = 0; i < WI_INIT_TRIES; i++) {
1115 error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0);
1118 DELAY(WI_DELAY * 1000);
1121 if (i == WI_INIT_TRIES) {
1122 if_printf(sc->sc_ifp, "reset failed\n");
1126 CSR_WRITE_2(sc, WI_INT_EN, 0);
1127 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
1129 /* Calibrate timer. */
1130 wi_write_val(sc, WI_RID_TICK_TIME, 8);
1133 #undef WI_INIT_TRIES
1137 wi_watchdog_callout(void *arg)
1139 struct wi_softc *sc = arg;
1140 struct ifnet *ifp = sc->sc_ifp;
1142 if (!sc->sc_enabled)
1145 if (sc->sc_tx_timer && --sc->sc_tx_timer == 0) {
1146 if_printf(ifp, "device timeout\n");
1147 IFNET_STAT_INC(ifp, oerrors, 1);
1148 wi_init_locked(ifp->if_softc);
1151 callout_reset(&sc->sc_watchdog, hz, wi_watchdog_callout, sc);
1155 wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *ucred)
1157 struct wi_softc *sc = ifp->if_softc;
1158 struct ieee80211com *ic = ifp->if_l2com;
1159 struct ifreq *ifr = (struct ifreq *) data;
1160 int error = 0, startall = 0;
1165 * Can't do promisc and hostap at the same time. If all that's
1166 * changing is the promisc flag, try to short-circuit a call to
1167 * wi_init() by just setting PROMISC in the hardware.
1169 if (ifp->if_flags & IFF_UP) {
1170 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1171 ifp->if_flags & IFF_RUNNING) {
1172 if ((ifp->if_flags ^ sc->sc_if_flags) & IFF_PROMISC) {
1173 wi_write_val(sc, WI_RID_PROMISC,
1174 (ifp->if_flags & IFF_PROMISC) != 0);
1184 if (ifp->if_flags & IFF_RUNNING)
1185 wi_stop_locked(sc, 1);
1188 sc->sc_if_flags = ifp->if_flags;
1190 ieee80211_start_all(ic);
1193 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1196 error = ether_ioctl(ifp, cmd, data);
1206 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1208 struct ieee80211vap *vap = ifp->if_softc;
1209 struct ieee80211com *ic = vap->iv_ic;
1210 struct wi_softc *sc = ic->ic_ifp->if_softc;
1215 if (sc->sc_enabled &&
1216 wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) == 0 &&
1217 len == sizeof(val)) {
1218 /* convert to 802.11 rate */
1221 if (sc->sc_firmware_type == WI_LUCENT) {
1223 rate = 11; /* 5.5Mbps */
1226 rate = 11; /* 5.5Mbps */
1227 else if (rate == 8*2)
1228 rate = 22; /* 11Mbps */
1230 vap->iv_bss->ni_txrate = rate;
1232 ieee80211_media_status(ifp, imr);
1236 wi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1238 struct ifnet *ifp = sc->sc_ifp;
1239 struct ieee80211com *ic = ifp->if_l2com;
1240 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1241 struct ieee80211_node *ni = vap->iv_bss;
1242 char ethstr[ETHER_ADDRSTRLEN + 1];
1244 if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1247 DPRINTF(("wi_sync_bssid: bssid %s -> ", kether_ntoa(ni->ni_bssid, ethstr)));
1248 DPRINTF(("%s ?\n", kether_ntoa(new_bssid, ethstr)));
1250 /* In promiscuous mode, the BSSID field is not a reliable
1251 * indicator of the firmware's BSSID. Damp spurious
1252 * change-of-BSSID indications.
1254 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1255 !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns,
1259 sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1);
1262 * XXX hack; we should create a new node with the new bssid
1263 * and replace the existing ic_bss with it but since we don't
1264 * process management frames to collect state we cheat by
1265 * reusing the existing node as we know wi_newstate will be
1266 * called and it will overwrite the node state.
1268 ieee80211_sta_join(ic, ieee80211_ref_node(ni));
1272 static __noinline void
1273 wi_rx_intr(struct wi_softc *sc)
1275 struct ifnet *ifp = sc->sc_ifp;
1276 struct ieee80211com *ic = ifp->if_l2com;
1277 struct wi_frame frmhdr;
1279 struct ieee80211_frame *wh;
1280 struct ieee80211_node *ni;
1286 fid = CSR_READ_2(sc, WI_RX_FID);
1288 /* First read in the frame header */
1289 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1290 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1291 IFNET_STAT_INC(ifp, ierrors, 1);
1292 DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1297 * Drop undecryptable or packets with receive errors here
1299 status = le16toh(frmhdr.wi_status);
1300 if (status & WI_STAT_ERRSTAT) {
1301 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1302 IFNET_STAT_INC(ifp, ierrors, 1);
1303 DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1307 len = le16toh(frmhdr.wi_dat_len);
1308 off = ALIGN(sizeof(struct ieee80211_frame));
1311 * Sometimes the PRISM2.x returns bogusly large frames. Except
1312 * in monitor mode, just throw them away.
1314 if (off + len > MCLBYTES) {
1315 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1316 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1317 IFNET_STAT_INC(ifp, ierrors, 1);
1318 DPRINTF(("wi_rx_intr: oversized packet\n"));
1324 if (off + len > MHLEN)
1325 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1327 m = m_gethdr(M_NOWAIT, MT_DATA);
1329 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1330 IFNET_STAT_INC(ifp, ierrors, 1);
1331 DPRINTF(("wi_rx_intr: MGET failed\n"));
1334 m->m_data += off - sizeof(struct ieee80211_frame);
1335 memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1336 wi_read_bap(sc, fid, sizeof(frmhdr),
1337 m->m_data + sizeof(struct ieee80211_frame), len);
1338 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1339 m->m_pkthdr.rcvif = ifp;
1341 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1343 rssi = frmhdr.wi_rx_signal;
1344 nf = frmhdr.wi_rx_silence;
1345 if (ieee80211_radiotap_active(ic)) {
1346 struct wi_rx_radiotap_header *tap = &sc->sc_rx_th;
1349 rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1350 le16toh(frmhdr.wi_rx_tstamp1);
1351 tap->wr_tsf = htole64((uint64_t)rstamp);
1352 /* XXX replace divide by table */
1353 tap->wr_rate = frmhdr.wi_rx_rate / 5;
1355 if (frmhdr.wi_status & WI_STAT_PCF)
1356 tap->wr_flags |= IEEE80211_RADIOTAP_F_CFP;
1357 if (m->m_flags & M_WEP)
1358 tap->wr_flags |= IEEE80211_RADIOTAP_F_WEP;
1359 tap->wr_antsignal = rssi;
1360 tap->wr_antnoise = nf;
1363 /* synchronize driver's BSSID with firmware's BSSID */
1364 wh = mtod(m, struct ieee80211_frame *);
1365 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1366 if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1367 wi_sync_bssid(sc, wh->i_addr3);
1369 ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1371 (void) ieee80211_input(ni, m, rssi, nf);
1372 ieee80211_free_node(ni);
1374 (void) ieee80211_input_all(ic, m, rssi, nf);
1378 static __noinline void
1379 wi_tx_ex_intr(struct wi_softc *sc)
1381 struct ifnet *ifp = sc->sc_ifp;
1382 struct wi_frame frmhdr;
1384 char ethstr[ETHER_ADDRSTRLEN + 1];
1386 fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1387 /* Read in the frame header */
1388 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1389 u_int16_t status = le16toh(frmhdr.wi_status);
1391 * Spontaneous station disconnects appear as xmit
1392 * errors. Don't announce them and/or count them
1393 * as an output error.
1395 if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1396 if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1397 if_printf(ifp, "tx failed");
1398 if (status & WI_TXSTAT_RET_ERR)
1399 kprintf(", retry limit exceeded");
1400 if (status & WI_TXSTAT_AGED_ERR)
1401 kprintf(", max transmit lifetime exceeded");
1402 if (status & WI_TXSTAT_DISCONNECT)
1403 kprintf(", port disconnected");
1404 if (status & WI_TXSTAT_FORM_ERR)
1405 kprintf(", invalid format (data len %u src %s)",
1406 le16toh(frmhdr.wi_dat_len),
1407 kether_ntoa(frmhdr.wi_ehdr.ether_shost, ethstr));
1409 kprintf(", status=0x%x", status);
1412 IFNET_STAT_INC(ifp, oerrors, 1);
1414 DPRINTF(("port disconnected\n"));
1415 IFNET_STAT_INC(ifp, collisions, 1); /* XXX */
1418 DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
1419 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1422 static __noinline void
1423 wi_tx_intr(struct wi_softc *sc)
1425 struct ifnet *ifp = sc->sc_ifp;
1431 fid = CSR_READ_2(sc, WI_ALLOC_FID);
1432 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1435 if (sc->sc_txd[cur].d_fid != fid) {
1436 if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
1437 fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
1440 sc->sc_tx_timer = 0;
1441 sc->sc_txd[cur].d_len = 0;
1442 sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
1443 if (sc->sc_txd[cur].d_len == 0)
1444 ifq_clr_oactive(&ifp->if_snd);
1446 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1448 if_printf(ifp, "xmit failed\n");
1449 sc->sc_txd[cur].d_len = 0;
1451 sc->sc_tx_timer = 5;
1456 static __noinline void
1457 wi_info_intr(struct wi_softc *sc)
1459 struct ifnet *ifp = sc->sc_ifp;
1460 struct ieee80211com *ic = ifp->if_l2com;
1461 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1462 int i, fid, len, off;
1467 fid = CSR_READ_2(sc, WI_INFO_FID);
1468 wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
1470 switch (le16toh(ltbuf[1])) {
1471 case WI_INFO_LINK_STAT:
1472 wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1473 DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1474 switch (le16toh(stat)) {
1475 case WI_INFO_LINK_STAT_CONNECTED:
1476 if (vap->iv_state == IEEE80211_S_RUN &&
1477 vap->iv_opmode != IEEE80211_M_IBSS)
1480 case WI_INFO_LINK_STAT_AP_CHG:
1481 vap->iv_bss->ni_associd = 1 | 0xc000; /* NB: anything will do */
1482 ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
1484 case WI_INFO_LINK_STAT_AP_INR:
1486 case WI_INFO_LINK_STAT_DISCONNECTED:
1487 /* we dropped off the net; e.g. due to deauth/disassoc */
1488 vap->iv_bss->ni_associd = 0;
1489 vap->iv_stats.is_rx_deauth++;
1490 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
1492 case WI_INFO_LINK_STAT_AP_OOR:
1493 /* XXX does this need to be per-vap? */
1494 ieee80211_beacon_miss(ic);
1496 case WI_INFO_LINK_STAT_ASSOC_FAILED:
1497 if (vap->iv_opmode == IEEE80211_M_STA)
1498 ieee80211_new_state(vap, IEEE80211_S_SCAN,
1499 IEEE80211_SCAN_FAIL_TIMEOUT);
1503 case WI_INFO_COUNTERS:
1504 /* some card versions have a larger stats structure */
1505 len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1506 ptr = (u_int32_t *)&sc->sc_stats;
1507 off = sizeof(ltbuf);
1508 for (i = 0; i < len; i++, off += 2, ptr++) {
1509 wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1510 #ifdef WI_HERMES_STATS_WAR
1516 IFNET_STAT_SET(ifp, collisions,
1517 sc->sc_stats.wi_tx_single_retries +
1518 sc->sc_stats.wi_tx_multi_retries +
1519 sc->sc_stats.wi_tx_retry_limit);
1522 DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1523 le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1526 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1530 wi_write_multi(struct wi_softc *sc)
1532 struct ifnet *ifp = sc->sc_ifp;
1534 struct ifmultiaddr *ifma;
1535 struct wi_mcast mlist;
1537 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1539 memset(&mlist, 0, sizeof(mlist));
1540 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1546 if_maddr_rlock(ifp);
1548 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1549 if (ifma->ifma_addr->sa_family != AF_LINK)
1553 IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1554 (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1558 if_maddr_runlock(ifp);
1560 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1561 IEEE80211_ADDR_LEN * n);
1565 wi_update_mcast(struct ieee80211com *ic)
1567 wi_write_multi(ic->ic_softc);
1571 wi_update_promisc(struct ieee80211com *ic)
1573 struct wi_softc *sc = ic->ic_softc;
1575 /* XXX handle WEP special case handling? */
1576 wi_write_val(sc, WI_RID_PROMISC,
1577 (ic->ic_opmode == IEEE80211_M_MONITOR ||
1578 (ic->ic_ifp->if_flags & IFF_PROMISC)));
1582 wi_read_nicid(struct wi_softc *sc)
1584 struct wi_card_ident *id;
1589 /* getting chip identity */
1590 memset(ver, 0, sizeof(ver));
1592 wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1594 sc->sc_firmware_type = WI_NOTYPE;
1595 sc->sc_nic_id = le16toh(ver[0]);
1596 for (id = wi_card_ident; id->card_name != NULL; id++) {
1597 if (sc->sc_nic_id == id->card_id) {
1598 sc->sc_nic_name = id->card_name;
1599 sc->sc_firmware_type = id->firm_type;
1603 if (sc->sc_firmware_type == WI_NOTYPE) {
1604 if (sc->sc_nic_id & 0x8000) {
1605 sc->sc_firmware_type = WI_INTERSIL;
1606 sc->sc_nic_name = "Unknown Prism chip";
1608 sc->sc_firmware_type = WI_LUCENT;
1609 sc->sc_nic_name = "Unknown Lucent chip";
1613 device_printf(sc->sc_dev, "using %s\n", sc->sc_nic_name);
1615 /* get primary firmware version (Only Prism chips) */
1616 if (sc->sc_firmware_type != WI_LUCENT) {
1617 memset(ver, 0, sizeof(ver));
1619 wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1620 sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1621 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1624 /* get station firmware version */
1625 memset(ver, 0, sizeof(ver));
1627 wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1628 sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1629 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1630 if (sc->sc_firmware_type == WI_INTERSIL &&
1631 (sc->sc_sta_firmware_ver == 10102 ||
1632 sc->sc_sta_firmware_ver == 20102)) {
1634 memset(ident, 0, sizeof(ident));
1635 len = sizeof(ident);
1636 /* value should be the format like "V2.00-11" */
1637 if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1638 *(p = (char *)ident) >= 'A' &&
1639 p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1640 sc->sc_firmware_type = WI_SYMBOL;
1641 sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1642 (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1643 (p[6] - '0') * 10 + (p[7] - '0');
1647 device_printf(sc->sc_dev, "%s Firmware: ",
1648 wi_firmware_names[sc->sc_firmware_type]);
1649 if (sc->sc_firmware_type != WI_LUCENT) /* XXX */
1650 kprintf("Primary (%u.%u.%u), ",
1651 sc->sc_pri_firmware_ver / 10000,
1652 (sc->sc_pri_firmware_ver % 10000) / 100,
1653 sc->sc_pri_firmware_ver % 100);
1654 kprintf("Station (%u.%u.%u)\n",
1655 sc->sc_sta_firmware_ver / 10000,
1656 (sc->sc_sta_firmware_ver % 10000) / 100,
1657 sc->sc_sta_firmware_ver % 100);
1662 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1664 struct wi_ssid ssid;
1666 if (buflen > IEEE80211_NWID_LEN)
1668 memset(&ssid, 0, sizeof(ssid));
1669 ssid.wi_len = htole16(buflen);
1670 memcpy(ssid.wi_ssid, buf, buflen);
1671 return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1675 wi_write_txrate(struct wi_softc *sc, struct ieee80211vap *vap)
1677 static const uint16_t lucent_rates[12] = {
1678 [ 0] = 3, /* auto */
1679 [ 1] = 1, /* 1Mb/s */
1680 [ 2] = 2, /* 2Mb/s */
1681 [ 5] = 4, /* 5.5Mb/s */
1682 [11] = 5 /* 11Mb/s */
1684 static const uint16_t intersil_rates[12] = {
1685 [ 0] = 0xf, /* auto */
1686 [ 1] = 0, /* 1Mb/s */
1687 [ 2] = 1, /* 2Mb/s */
1688 [ 5] = 2, /* 5.5Mb/s */
1689 [11] = 3, /* 11Mb/s */
1691 const uint16_t *rates = sc->sc_firmware_type == WI_LUCENT ?
1692 lucent_rates : intersil_rates;
1693 struct ieee80211com *ic = vap->iv_ic;
1694 const struct ieee80211_txparam *tp;
1696 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
1697 return wi_write_val(sc, WI_RID_TX_RATE,
1698 (tp->ucastrate == IEEE80211_FIXED_RATE_NONE ?
1699 rates[0] : rates[tp->ucastrate / 2]));
1703 wi_write_wep(struct wi_softc *sc, struct ieee80211vap *vap)
1708 struct wi_key wkey[IEEE80211_WEP_NKID];
1710 switch (sc->sc_firmware_type) {
1712 val = (vap->iv_flags & IEEE80211_F_PRIVACY) ? 1 : 0;
1713 error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
1716 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0)
1718 error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, vap->iv_def_txkey);
1721 memset(wkey, 0, sizeof(wkey));
1722 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1723 keylen = vap->iv_nw_keys[i].wk_keylen;
1724 wkey[i].wi_keylen = htole16(keylen);
1725 memcpy(wkey[i].wi_keydat, vap->iv_nw_keys[i].wk_key,
1728 error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
1729 wkey, sizeof(wkey));
1730 sc->sc_encryption = 0;
1734 val = HOST_ENCRYPT | HOST_DECRYPT;
1735 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1737 * ONLY HWB3163 EVAL-CARD Firmware version
1738 * less than 0.8 variant2
1740 * If promiscuous mode disable, Prism2 chip
1741 * does not work with WEP .
1742 * It is under investigation for details.
1743 * (ichiro@netbsd.org)
1745 if (sc->sc_sta_firmware_ver < 802 ) {
1746 /* firm ver < 0.8 variant 2 */
1747 wi_write_val(sc, WI_RID_PROMISC, 1);
1749 wi_write_val(sc, WI_RID_CNFAUTHMODE,
1750 vap->iv_bss->ni_authmode);
1751 val |= PRIVACY_INVOKED;
1753 wi_write_val(sc, WI_RID_CNFAUTHMODE, IEEE80211_AUTH_OPEN);
1755 error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
1758 sc->sc_encryption = val;
1759 if ((val & PRIVACY_INVOKED) == 0)
1761 error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY, vap->iv_def_txkey);
1768 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
1775 /* wait for the busy bit to clear */
1776 for (i = sc->wi_cmd_count; i > 0; i--) { /* 500ms */
1777 if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
1779 DELAY(1*1000); /* 1ms */
1782 device_printf(sc->sc_dev, "%s: busy bit won't clear, cmd 0x%x\n",
1788 CSR_WRITE_2(sc, WI_PARAM0, val0);
1789 CSR_WRITE_2(sc, WI_PARAM1, val1);
1790 CSR_WRITE_2(sc, WI_PARAM2, val2);
1791 CSR_WRITE_2(sc, WI_COMMAND, cmd);
1793 if (cmd == WI_CMD_INI) {
1794 /* XXX: should sleep here. */
1795 DELAY(100*1000); /* 100ms delay for init */
1797 for (i = 0; i < WI_TIMEOUT; i++) {
1799 * Wait for 'command complete' bit to be
1800 * set in the event status register.
1802 s = CSR_READ_2(sc, WI_EVENT_STAT);
1803 if (s & WI_EV_CMD) {
1804 /* Ack the event and read result code. */
1805 s = CSR_READ_2(sc, WI_STATUS);
1806 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
1807 if (s & WI_STAT_CMD_RESULT) {
1815 if (i == WI_TIMEOUT) {
1816 device_printf(sc->sc_dev, "%s: timeout on cmd 0x%04x; "
1817 "event status 0x%04x\n", __func__, cmd, s);
1826 wi_seek_bap(struct wi_softc *sc, int id, int off)
1830 CSR_WRITE_2(sc, WI_SEL0, id);
1831 CSR_WRITE_2(sc, WI_OFF0, off);
1833 for (i = 0; ; i++) {
1834 status = CSR_READ_2(sc, WI_OFF0);
1835 if ((status & WI_OFF_BUSY) == 0)
1837 if (i == WI_TIMEOUT) {
1838 device_printf(sc->sc_dev, "%s: timeout, id %x off %x\n",
1840 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
1841 if (status == 0xffff)
1847 if (status & WI_OFF_ERR) {
1848 device_printf(sc->sc_dev, "%s: error, id %x off %x\n",
1850 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
1854 sc->sc_bap_off = off;
1859 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
1866 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1867 if ((error = wi_seek_bap(sc, id, off)) != 0)
1870 cnt = (buflen + 1) / 2;
1871 ptr = (u_int16_t *)buf;
1872 for (i = 0; i < cnt; i++)
1873 *ptr++ = CSR_READ_2(sc, WI_DATA0);
1874 sc->sc_bap_off += cnt * 2;
1879 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
1887 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1888 if ((error = wi_seek_bap(sc, id, off)) != 0)
1891 cnt = (buflen + 1) / 2;
1892 ptr = (u_int16_t *)buf;
1893 for (i = 0; i < cnt; i++)
1894 CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
1895 sc->sc_bap_off += cnt * 2;
1901 wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
1906 for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
1910 len = min(m->m_len, totlen);
1912 if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
1913 m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
1914 return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
1918 if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
1928 wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
1932 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
1933 device_printf(sc->sc_dev, "%s: failed to allocate %d bytes on NIC\n",
1938 for (i = 0; i < WI_TIMEOUT; i++) {
1939 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
1943 if (i == WI_TIMEOUT) {
1944 device_printf(sc->sc_dev, "%s: timeout in alloc\n", __func__);
1947 *idp = CSR_READ_2(sc, WI_ALLOC_FID);
1948 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1953 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
1958 /* Tell the NIC to enter record read mode. */
1959 error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
1963 error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
1967 if (le16toh(ltbuf[1]) != rid) {
1968 device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n",
1969 rid, le16toh(ltbuf[1]));
1972 len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */
1973 if (*buflenp < len) {
1974 device_printf(sc->sc_dev, "record buffer is too small, "
1975 "rid=%x, size=%d, len=%d\n",
1976 rid, *buflenp, len);
1980 return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
1984 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
1989 ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */
1990 ltbuf[1] = htole16(rid);
1992 error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
1994 device_printf(sc->sc_dev, "%s: bap0 write failure, rid 0x%x\n",
1998 error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2000 device_printf(sc->sc_dev, "%s: bap1 write failure, rid 0x%x\n",
2005 return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
2009 wi_write_appie(struct wi_softc *sc, int rid, const struct ieee80211_appie *ie)
2011 /* NB: 42 bytes is probably ok to have on the stack */
2012 char buf[sizeof(uint16_t) + 40];
2014 if (ie->ie_len > 40)
2016 /* NB: firmware requires 16-bit ie length before ie data */
2017 *(uint16_t *) buf = htole16(ie->ie_len);
2018 memcpy(buf + sizeof(uint16_t), ie->ie_data, ie->ie_len);
2019 return wi_write_rid(sc, rid, buf, ie->ie_len + sizeof(uint16_t));
2023 wi_alloc(device_t dev, int rid)
2025 struct wi_softc *sc = device_get_softc(dev);
2027 if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2028 sc->iobase_rid = rid;
2029 sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2030 &sc->iobase_rid, 0, ~0, (1 << 6),
2031 rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2032 if (sc->iobase == NULL) {
2033 device_printf(dev, "No I/O space?!\n");
2037 sc->wi_io_addr = rman_get_start(sc->iobase);
2038 sc->wi_btag = rman_get_bustag(sc->iobase);
2039 sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2042 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2043 &sc->mem_rid, RF_ACTIVE);
2044 if (sc->mem == NULL) {
2045 device_printf(dev, "No Mem space on prism2.5?\n");
2049 sc->wi_btag = rman_get_bustag(sc->mem);
2050 sc->wi_bhandle = rman_get_bushandle(sc->mem);
2054 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2056 ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2057 if (sc->irq == NULL) {
2059 device_printf(dev, "No irq?!\n");
2064 sc->sc_unit = device_get_unit(dev);
2069 wi_free(device_t dev)
2071 struct wi_softc *sc = device_get_softc(dev);
2073 if (sc->iobase != NULL) {
2074 bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2077 if (sc->irq != NULL) {
2078 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2081 if (sc->mem != NULL) {
2082 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);