Do not try to write sparse files when using zlib.
[dragonfly.git] / sys / dev / netif / wi / if_wi.c
CommitLineData
acffed4b
JS
1/* $NetBSD: wi.c,v 1.109 2003/01/09 08:52:19 dyoung Exp $ */
2
984263bc
MD
3/*
4 * Copyright (c) 1997, 1998, 1999
5 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
1de703da 33 *
acffed4b 34 * $FreeBSD: src/sys/dev/wi/if_wi.c,v 1.166 2004/04/01 00:38:45 sam Exp $
d2c71fa0 35 * $DragonFly: src/sys/dev/netif/wi/if_wi.c,v 1.32 2005/11/22 00:24:34 dillon Exp $
984263bc
MD
36 */
37
38/*
acffed4b 39 * Lucent WaveLAN/IEEE 802.11 PCMCIA driver.
984263bc 40 *
acffed4b 41 * Original FreeBSD driver written by Bill Paul <wpaul@ctr.columbia.edu>
984263bc
MD
42 * Electrical Engineering Department
43 * Columbia University, New York City
44 */
45
46/*
47 * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
48 * from Lucent. Unlike the older cards, the new ones are programmed
49 * entirely via a firmware-driven controller called the Hermes.
50 * Unfortunately, Lucent will not release the Hermes programming manual
51 * without an NDA (if at all). What they do release is an API library
52 * called the HCF (Hardware Control Functions) which is supposed to
53 * do the device-specific operations of a device driver for you. The
54 * publically available version of the HCF library (the 'HCF Light') is
55 * a) extremely gross, b) lacks certain features, particularly support
56 * for 802.11 frames, and c) is contaminated by the GNU Public License.
57 *
58 * This driver does not use the HCF or HCF Light at all. Instead, it
59 * programs the Hermes controller directly, using information gleaned
60 * from the HCF Light code and corresponding documentation.
61 *
62 * This driver supports the ISA, PCMCIA and PCI versions of the Lucent
63 * WaveLan cards (based on the Hermes chipset), as well as the newer
64 * Prism 2 chipsets with firmware from Intersil and Symbol.
65 */
66
acffed4b
JS
67#define WI_HERMES_AUTOINC_WAR /* Work around data write autoinc bug. */
68#define WI_HERMES_STATS_WAR /* Work around stats counter bug. */
69
2b71c8f1
SZ
70#include "opt_polling.h"
71
984263bc 72#include <sys/param.h>
984263bc 73#include <sys/endian.h>
acffed4b 74#include <sys/systm.h>
984263bc
MD
75#include <sys/sockio.h>
76#include <sys/mbuf.h>
77#include <sys/proc.h>
78#include <sys/kernel.h>
79#include <sys/socket.h>
80#include <sys/module.h>
81#include <sys/bus.h>
82#include <sys/random.h>
83#include <sys/syslog.h>
84#include <sys/sysctl.h>
d33a8e8f 85#include <sys/thread2.h>
984263bc
MD
86
87#include <machine/bus.h>
88#include <machine/resource.h>
acffed4b 89#include <machine/atomic.h>
984263bc
MD
90#include <sys/rman.h>
91
92#include <net/if.h>
93#include <net/if_arp.h>
94#include <net/ethernet.h>
95#include <net/if_dl.h>
96#include <net/if_media.h>
97#include <net/if_types.h>
4d723e5a 98#include <net/ifq_var.h>
acffed4b
JS
99
100#include <netproto/802_11/ieee80211_var.h>
3ee50d77 101#include <netproto/802_11/ieee80211_ioctl.h>
acffed4b 102#include <netproto/802_11/ieee80211_radiotap.h>
3ee50d77 103#include <netproto/802_11/if_wavelan_ieee.h>
984263bc
MD
104
105#include <netinet/in.h>
106#include <netinet/in_systm.h>
107#include <netinet/in_var.h>
108#include <netinet/ip.h>
109#include <netinet/if_ether.h>
110
111#include <net/bpf.h>
112
acffed4b
JS
113#include <dev/netif/wi/if_wireg.h>
114#include <dev/netif/wi/if_wivar.h>
984263bc 115
984263bc 116static void wi_start(struct ifnet *);
acffed4b 117static int wi_reset(struct wi_softc *);
984263bc 118static void wi_watchdog(struct ifnet *);
acffed4b
JS
119static int wi_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
120static int wi_media_change(struct ifnet *);
984263bc
MD
121static void wi_media_status(struct ifnet *, struct ifmediareq *);
122
acffed4b
JS
123static void wi_rx_intr(struct wi_softc *);
124static void wi_tx_intr(struct wi_softc *);
125static void wi_tx_ex_intr(struct wi_softc *);
126static void wi_info_intr(struct wi_softc *);
127
128static int wi_get_cfg(struct ifnet *, u_long, caddr_t, struct ucred *);
129static int wi_set_cfg(struct ifnet *, u_long, caddr_t);
130static int wi_write_txrate(struct wi_softc *);
131static int wi_write_wep(struct wi_softc *);
132static int wi_write_multi(struct wi_softc *);
133static int wi_alloc_fid(struct wi_softc *, int, int *);
134static void wi_read_nicid(struct wi_softc *);
135static int wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
136
137static int wi_cmd(struct wi_softc *, int, int, int, int);
138static int wi_seek_bap(struct wi_softc *, int, int);
139static int wi_read_bap(struct wi_softc *, int, int, void *, int);
140static int wi_write_bap(struct wi_softc *, int, int, void *, int);
141static int wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
142static int wi_read_rid(struct wi_softc *, int, void *, int *);
143static int wi_write_rid(struct wi_softc *, int, void *, int);
144
145static int wi_newstate(struct ieee80211com *, enum ieee80211_state, int);
146
147static int wi_scan_ap(struct wi_softc *, u_int16_t, u_int16_t);
148static void wi_scan_result(struct wi_softc *, int, int);
149
150static void wi_dump_pkt(struct wi_frame *, struct ieee80211_node *, int rssi);
151
984263bc
MD
152static int wi_get_debug(struct wi_softc *, struct wi_req *);
153static int wi_set_debug(struct wi_softc *, struct wi_req *);
154
acffed4b
JS
155/* support to download firmware for symbol CF card */
156static int wi_symbol_write_firm(struct wi_softc *, const void *, int,
157 const void *, int);
158static int wi_symbol_set_hcr(struct wi_softc *, int);
9c095379
MD
159#ifdef DEVICE_POLLING
160static void wi_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
161#endif
32832096 162
acffed4b
JS
163static __inline int
164wi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
165{
166
167 val = htole16(val);
168 return wi_write_rid(sc, rid, &val, sizeof(val));
169}
170
171SYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters");
172
173static struct timeval lasttxerror; /* time of last tx error msg */
174static int curtxeps; /* current tx error msgs/sec */
175static int wi_txerate = 0; /* tx error rate: max msgs/sec */
176SYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate,
177 0, "max tx error msgs/sec; 0 to disable msgs");
178
179#define WI_DEBUG
180#ifdef WI_DEBUG
181static int wi_debug = 0;
182SYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug,
183 0, "control debugging printfs");
184
4c30eb3b
JS
185#define DPRINTF(X) if (wi_debug) if_printf X
186#define DPRINTF2(X) if (wi_debug > 1) if_printf X
acffed4b
JS
187#define IFF_DUMPPKTS(_ifp) \
188 (((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
189#else
190#define DPRINTF(X)
191#define DPRINTF2(X)
192#define IFF_DUMPPKTS(_ifp) 0
193#endif
194
195#define WI_INTRS (WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
984263bc
MD
196
197struct wi_card_ident wi_card_ident[] = {
198 /* CARD_ID CARD_NAME FIRM_TYPE */
199 { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT },
200 { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT },
201 { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT },
202 { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL },
203 { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL },
204 { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL },
205 { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL },
206 { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL },
207 { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL },
208 { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL },
209 { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL },
210 { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL },
211 { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
212 { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
acffed4b
JS
213 { WI_NIC_3842_PCMCIA_ATL_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
214 { WI_NIC_3842_PCMCIA_ATS_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
984263bc
MD
215 { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
216 { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
acffed4b
JS
217 { WI_NIC_3842_MINI_ATL_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
218 { WI_NIC_3842_MINI_ATS_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
984263bc
MD
219 { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
220 { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
acffed4b
JS
221 { WI_NIC_3842_PCI_ATS_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
222 { WI_NIC_3842_PCI_ATL_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
984263bc
MD
223 { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
224 { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
acffed4b
JS
225 { WI_NIC_P3_PCMCIA_ATL_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
226 { WI_NIC_P3_PCMCIA_ATS_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
984263bc
MD
227 { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
228 { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
acffed4b
JS
229 { WI_NIC_P3_MINI_ATL_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
230 { WI_NIC_P3_MINI_ATS_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
984263bc
MD
231 { 0, NULL, 0 },
232};
233
acffed4b 234devclass_t wi_devclass;
984263bc
MD
235
236int
acffed4b 237wi_attach(device_t dev)
984263bc 238{
acffed4b
JS
239 struct wi_softc *sc = device_get_softc(dev);
240 struct ieee80211com *ic = &sc->sc_ic;
241 struct ifnet *ifp = &ic->ic_if;
242 int i, nrates, buflen;
243 u_int16_t val;
244 u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
245 struct ieee80211_rateset *rs;
246 static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
247 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
248 };
249 int error;
250
4c30eb3b
JS
251 ifp->if_softc = sc;
252 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
253
acffed4b 254 sc->wi_cmd_count = 500;
984263bc 255 /* Reset the NIC. */
0d71884b
JS
256 error = wi_reset(sc);
257 if (error)
258 goto fail;
984263bc
MD
259
260 /*
261 * Read the station address.
262 * And do it twice. I've seen PRISM-based cards that return
263 * an error when trying to read it the first time, which causes
264 * the probe to fail.
265 */
acffed4b
JS
266 buflen = IEEE80211_ADDR_LEN;
267 error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
268 if (error != 0) {
269 buflen = IEEE80211_ADDR_LEN;
270 error = wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen);
271 }
0d71884b
JS
272 if (error) {
273 device_printf(dev, "mac read failed %d\n", error);
274 goto fail;
275 }
276 if (IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) {
277 device_printf(dev, "mac read failed (all zeros)\n");
278 error = ENXIO;
279 goto fail;
984263bc 280 }
984263bc 281
acffed4b
JS
282 /* Read NIC identification */
283 wi_read_nicid(sc);
984263bc 284
984263bc
MD
285 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
286 ifp->if_ioctl = wi_ioctl;
984263bc
MD
287 ifp->if_start = wi_start;
288 ifp->if_watchdog = wi_watchdog;
289 ifp->if_init = wi_init;
4d723e5a
JS
290 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
291 ifq_set_ready(&ifp->if_snd);
f35cfef5 292#ifdef DEVICE_POLLING
9c095379 293 ifp->if_poll = wi_poll;
f35cfef5
JS
294#endif
295 ifp->if_capenable = ifp->if_capabilities;
984263bc 296
acffed4b
JS
297 ic->ic_phytype = IEEE80211_T_DS;
298 ic->ic_opmode = IEEE80211_M_STA;
299 ic->ic_caps = IEEE80211_C_PMGT | IEEE80211_C_AHDEMO;
300 ic->ic_state = IEEE80211_S_INIT;
984263bc 301
acffed4b
JS
302 /*
303 * Query the card for available channels and setup the
304 * channel table. We assume these are all 11b channels.
305 */
306 buflen = sizeof(val);
307 if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
308 val = htole16(0x1fff); /* assume 1-11 */
309 KASSERT(val != 0, ("wi_attach: no available channels listed!"));
310
311 val <<= 1; /* shift for base 1 indices */
312 for (i = 1; i < 16; i++) {
313 if (isset((u_int8_t*)&val, i)) {
314 ic->ic_channels[i].ic_freq =
315 ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
316 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
317 }
318 }
984263bc
MD
319
320 /*
321 * Read the default channel from the NIC. This may vary
322 * depending on the country where the NIC was purchased, so
323 * we can't hard-code a default and expect it to work for
324 * everyone.
acffed4b
JS
325 *
326 * If no channel is specified, let the 802.11 code select.
984263bc 327 */
acffed4b
JS
328 buflen = sizeof(val);
329 if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0) {
330 val = le16toh(val);
331 KASSERT(val < IEEE80211_CHAN_MAX &&
332 ic->ic_channels[val].ic_flags != 0,
333 ("wi_attach: invalid own channel %u!", val));
334 ic->ic_ibss_chan = &ic->ic_channels[val];
335 } else {
336 device_printf(dev,
337 "WI_RID_OWN_CHNL failed, using first channel!\n");
338 ic->ic_ibss_chan = &ic->ic_channels[0];
339 }
984263bc
MD
340
341 /*
342 * Set flags based on firmware version.
343 */
344 switch (sc->sc_firmware_type) {
345 case WI_LUCENT:
acffed4b
JS
346 sc->sc_ntxbuf = 1;
347 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
348#ifdef WI_HERMES_AUTOINC_WAR
349 /* XXX: not confirmed, but never seen for recent firmware */
350 if (sc->sc_sta_firmware_ver < 40000) {
351 sc->sc_flags |= WI_FLAGS_BUG_AUTOINC;
352 }
353#endif
984263bc 354 if (sc->sc_sta_firmware_ver >= 60000)
acffed4b 355 sc->sc_flags |= WI_FLAGS_HAS_MOR;
984263bc 356 if (sc->sc_sta_firmware_ver >= 60006) {
acffed4b
JS
357 ic->ic_caps |= IEEE80211_C_IBSS;
358 ic->ic_caps |= IEEE80211_C_MONITOR;
984263bc 359 }
acffed4b
JS
360 sc->sc_ibss_port = htole16(1);
361
362 sc->sc_min_rssi = WI_LUCENT_MIN_RSSI;
363 sc->sc_max_rssi = WI_LUCENT_MAX_RSSI;
364 sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
984263bc 365 break;
acffed4b 366
984263bc 367 case WI_INTERSIL:
acffed4b
JS
368 sc->sc_ntxbuf = WI_NTXBUF;
369 sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR;
370 sc->sc_flags |= WI_FLAGS_HAS_ROAMING;
371 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
372 /*
373 * Old firmware are slow, so give peace a chance.
374 */
375 if (sc->sc_sta_firmware_ver < 10000)
376 sc->wi_cmd_count = 5000;
377 if (sc->sc_sta_firmware_ver > 10101)
378 sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
984263bc 379 if (sc->sc_sta_firmware_ver >= 800) {
acffed4b
JS
380 ic->ic_caps |= IEEE80211_C_IBSS;
381 ic->ic_caps |= IEEE80211_C_MONITOR;
984263bc
MD
382 }
383 /*
384 * version 0.8.3 and newer are the only ones that are known
385 * to currently work. Earlier versions can be made to work,
386 * at least according to the Linux driver.
387 */
388 if (sc->sc_sta_firmware_ver >= 803)
acffed4b
JS
389 ic->ic_caps |= IEEE80211_C_HOSTAP;
390 sc->sc_ibss_port = htole16(0);
391
392 sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
393 sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
394 sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
984263bc 395 break;
acffed4b 396
984263bc 397 case WI_SYMBOL:
acffed4b
JS
398 sc->sc_ntxbuf = 1;
399 sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY;
984263bc 400 if (sc->sc_sta_firmware_ver >= 25000)
acffed4b
JS
401 ic->ic_caps |= IEEE80211_C_IBSS;
402 sc->sc_ibss_port = htole16(4);
403
404 sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
405 sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
406 sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
984263bc
MD
407 break;
408 }
409
410 /*
411 * Find out if we support WEP on this card.
412 */
acffed4b
JS
413 buflen = sizeof(val);
414 if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
415 val != htole16(0))
416 ic->ic_caps |= IEEE80211_C_WEP;
417
418 /* Find supported rates. */
419 buflen = sizeof(ratebuf);
420 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
421 if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
422 nrates = le16toh(*(u_int16_t *)ratebuf);
423 if (nrates > IEEE80211_RATE_MAXSIZE)
424 nrates = IEEE80211_RATE_MAXSIZE;
425 rs->rs_nrates = 0;
426 for (i = 0; i < nrates; i++)
427 if (ratebuf[2+i])
428 rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i];
429 } else {
430 /* XXX fallback on error? */
431 rs->rs_nrates = 0;
432 }
433
434 buflen = sizeof(val);
435 if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
436 wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
437 sc->sc_dbm_offset = le16toh(val);
438 }
984263bc 439
acffed4b
JS
440 sc->sc_max_datalen = 2304;
441 sc->sc_system_scale = 1;
442 sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN;
443 sc->sc_roaming_mode = 1;
984263bc 444
acffed4b
JS
445 sc->sc_portnum = WI_DEFAULT_PORT;
446 sc->sc_authtype = WI_DEFAULT_AUTHTYPE;
447
448 bzero(sc->sc_nodename, sizeof(sc->sc_nodename));
449 sc->sc_nodelen = sizeof(WI_DEFAULT_NODENAME) - 1;
450 bcopy(WI_DEFAULT_NODENAME, sc->sc_nodename, sc->sc_nodelen);
451
452 bzero(sc->sc_net_name, sizeof(sc->sc_net_name));
453 bcopy(WI_DEFAULT_NETNAME, sc->sc_net_name,
454 sizeof(WI_DEFAULT_NETNAME) - 1);
984263bc
MD
455
456 /*
457 * Call MI attach routine.
458 */
acffed4b
JS
459 ieee80211_ifattach(ifp);
460 /* override state transition method */
461 sc->sc_newstate = ic->ic_newstate;
462 ic->ic_newstate = wi_newstate;
463 ieee80211_media_init(ifp, wi_media_change, wi_media_status);
464
1f8e62c9 465 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
acffed4b
JS
466 sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
467 &sc->sc_drvbpf);
468 /*
469 * Initialize constant fields.
470 * XXX make header lengths a multiple of 32-bits so subsequent
471 * headers are properly aligned; this is a kludge to keep
472 * certain applications happy.
473 *
474 * NB: the channel is setup each time we transition to the
475 * RUN state to avoid filling it in for each frame.
476 */
477 sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t));
478 sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len);
479 sc->sc_tx_th.wt_ihdr.it_present = htole32(WI_TX_RADIOTAP_PRESENT);
984263bc 480
acffed4b
JS
481 sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t));
482 sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len);
483 sc->sc_rx_th.wr_ihdr.it_present = htole32(WI_RX_RADIOTAP_PRESENT);
1f8e62c9 484
0d71884b 485
ee61f228 486 error = bus_setup_intr(dev, sc->irq, INTR_MPSAFE,
bf853d22
JS
487 wi_intr, sc, &sc->wi_intrhand, NULL);
488 if (error) {
489 ieee80211_ifdetach(ifp);
490 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
491 goto fail;
492 }
493
0d71884b
JS
494 return(0);
495
496fail:
497 wi_free(dev);
498 return(error);
984263bc
MD
499}
500
acffed4b
JS
501int
502wi_detach(device_t dev)
984263bc 503{
acffed4b
JS
504 struct wi_softc *sc = device_get_softc(dev);
505 struct ifnet *ifp = &sc->sc_ic.ic_if;
506 WI_LOCK_DECL();
984263bc 507
acffed4b 508 WI_LOCK(sc);
984263bc 509
acffed4b
JS
510 /* check if device was removed */
511 sc->wi_gone |= !bus_child_present(dev);
984263bc 512
acffed4b 513 wi_stop(ifp, 0);
984263bc 514
acffed4b 515 ieee80211_ifdetach(ifp);
acffed4b 516 wi_free(dev);
bf853d22 517 WI_UNLOCK(sc);
acffed4b
JS
518 return (0);
519}
984263bc 520
acffed4b
JS
521void
522wi_shutdown(device_t dev)
523{
524 struct wi_softc *sc = device_get_softc(dev);
525
526 wi_stop(&sc->sc_if, 1);
984263bc
MD
527}
528
f35cfef5 529#ifdef DEVICE_POLLING
9c095379 530
f35cfef5
JS
531static void
532wi_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
533{
534 struct wi_softc *sc = ifp->if_softc;
535 uint16_t status;
536
9c095379
MD
537 switch(cmd) {
538 case POLL_REGISTER:
539 /* disable interruptds */
540 CSR_WRITE_2(sc, WI_INT_EN, 0);
541 break;
542 case POLL_DEREGISTER:
543 /* enable interrupts */
f35cfef5 544 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
9c095379
MD
545 break;
546 default:
547 status = CSR_READ_2(sc, WI_EVENT_STAT);
f35cfef5 548
9c095379
MD
549 if (status & WI_EV_RX)
550 wi_rx_intr(sc);
551 if (status & WI_EV_ALLOC)
552 wi_tx_intr(sc);
f35cfef5
JS
553 if (status & WI_EV_INFO)
554 wi_info_intr(sc);
f35cfef5 555
9c095379
MD
556 if (cmd == POLL_AND_CHECK_STATUS) {
557 if (status & WI_EV_INFO)
558 wi_info_intr(sc);
559 }
560
561 if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
562 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 && !ifq_is_empty(&ifp->if_snd)) {
563 wi_start(ifp);
564 }
565 break;
566 }
f35cfef5
JS
567}
568#endif /* DEVICE_POLLING */
569
acffed4b
JS
570void
571wi_intr(void *arg)
984263bc 572{
acffed4b
JS
573 struct wi_softc *sc = arg;
574 struct ifnet *ifp = &sc->sc_ic.ic_if;
575 u_int16_t status;
576 WI_LOCK_DECL();
984263bc 577
acffed4b
JS
578 if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) {
579 CSR_WRITE_2(sc, WI_INT_EN, 0);
580 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
acffed4b
JS
581 return;
582 }
984263bc 583
f35cfef5
JS
584 WI_LOCK(sc);
585
acffed4b
JS
586 /* Disable interrupts. */
587 CSR_WRITE_2(sc, WI_INT_EN, 0);
984263bc 588
acffed4b
JS
589 status = CSR_READ_2(sc, WI_EVENT_STAT);
590 if (status & WI_EV_RX)
591 wi_rx_intr(sc);
592 if (status & WI_EV_ALLOC)
593 wi_tx_intr(sc);
594 if (status & WI_EV_TX_EXC)
595 wi_tx_ex_intr(sc);
596 if (status & WI_EV_INFO)
597 wi_info_intr(sc);
598 if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
599 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 &&
4d723e5a 600 !ifq_is_empty(&ifp->if_snd))
acffed4b 601 wi_start(ifp);
984263bc 602
acffed4b
JS
603 /* Re-enable interrupts. */
604 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
984263bc 605
acffed4b 606 WI_UNLOCK(sc);
984263bc 607
acffed4b
JS
608 return;
609}
984263bc 610
acffed4b
JS
611void
612wi_init(void *arg)
613{
614 struct wi_softc *sc = arg;
615 struct ifnet *ifp = &sc->sc_if;
616 struct ieee80211com *ic = &sc->sc_ic;
617 struct wi_joinreq join;
618 int i;
619 int error = 0, wasenabled;
acffed4b 620 WI_LOCK_DECL();
984263bc 621
acffed4b 622 WI_LOCK(sc);
984263bc 623
acffed4b
JS
624 if (sc->wi_gone) {
625 WI_UNLOCK(sc);
626 return;
627 }
984263bc 628
acffed4b
JS
629 if ((wasenabled = sc->sc_enabled))
630 wi_stop(ifp, 1);
631 wi_reset(sc);
984263bc 632
acffed4b
JS
633 /* common 802.11 configuration */
634 ic->ic_flags &= ~IEEE80211_F_IBSSON;
635 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
636 switch (ic->ic_opmode) {
637 case IEEE80211_M_STA:
638 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS);
639 break;
640 case IEEE80211_M_IBSS:
641 wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port);
642 ic->ic_flags |= IEEE80211_F_IBSSON;
643 break;
644 case IEEE80211_M_AHDEMO:
645 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
646 break;
647 case IEEE80211_M_HOSTAP:
648 /*
649 * For PRISM cards, override the empty SSID, because in
650 * HostAP mode the controller will lock up otherwise.
651 */
652 if (sc->sc_firmware_type == WI_INTERSIL &&
653 ic->ic_des_esslen == 0) {
654 ic->ic_des_essid[0] = ' ';
655 ic->ic_des_esslen = 1;
656 }
657 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP);
658 break;
659 case IEEE80211_M_MONITOR:
660 if (sc->sc_firmware_type == WI_LUCENT)
661 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
662 wi_cmd(sc, WI_CMD_DEBUG | (WI_TEST_MONITOR << 8), 0, 0, 0);
663 break;
664 }
984263bc 665
acffed4b
JS
666 /* Intersil interprets this RID as joining ESS even in IBSS mode */
667 if (sc->sc_firmware_type == WI_LUCENT &&
668 (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0)
669 wi_write_val(sc, WI_RID_CREATE_IBSS, 1);
670 else
671 wi_write_val(sc, WI_RID_CREATE_IBSS, 0);
672 wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
673 wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid,
674 ic->ic_des_esslen);
675 wi_write_val(sc, WI_RID_OWN_CHNL,
676 ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
677 wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen);
678
f2682cb9 679 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
acffed4b
JS
680 wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN);
681
682 wi_write_val(sc, WI_RID_PM_ENABLED,
683 (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
684
685 /* not yet common 802.11 configuration */
686 wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen);
687 wi_write_val(sc, WI_RID_RTS_THRESH, ic->ic_rtsthreshold);
688 if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
689 wi_write_val(sc, WI_RID_FRAG_THRESH, ic->ic_fragthreshold);
690
691 /* driver specific 802.11 configuration */
692 if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)
693 wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale);
694 if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
695 wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode);
696 if (sc->sc_flags & WI_FLAGS_HAS_MOR)
697 wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven);
698 wi_write_txrate(sc);
699 wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen);
700
701 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
702 sc->sc_firmware_type == WI_INTERSIL) {
703 wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval);
704 wi_write_val(sc, WI_RID_BASIC_RATE, 0x03); /* 1, 2 */
705 wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */
706 wi_write_val(sc, WI_RID_DTIM_PERIOD, 1);
707 }
984263bc 708
acffed4b
JS
709 /*
710 * Initialize promisc mode.
711 * Being in the Host-AP mode causes a great
712 * deal of pain if primisc mode is set.
713 * Therefore we avoid confusing the firmware
714 * and always reset promisc mode in Host-AP
715 * mode. Host-AP sees all the packets anyway.
716 */
717 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
718 (ifp->if_flags & IFF_PROMISC) != 0) {
719 wi_write_val(sc, WI_RID_PROMISC, 1);
984263bc 720 } else {
acffed4b
JS
721 wi_write_val(sc, WI_RID_PROMISC, 0);
722 }
984263bc 723
acffed4b
JS
724 /* Configure WEP. */
725 if (ic->ic_caps & IEEE80211_C_WEP)
726 wi_write_wep(sc);
984263bc 727
acffed4b
JS
728 /* Set multicast filter. */
729 wi_write_multi(sc);
730
731 /* Allocate fids for the card */
732 if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
733 sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
734 if (sc->sc_firmware_type == WI_SYMBOL)
735 sc->sc_buflen = 1585; /* XXX */
736 for (i = 0; i < sc->sc_ntxbuf; i++) {
737 error = wi_alloc_fid(sc, sc->sc_buflen,
738 &sc->sc_txd[i].d_fid);
739 if (error) {
4c30eb3b 740 if_printf(ifp,
acffed4b
JS
741 "tx buffer allocation failed (error %u)\n",
742 error);
743 goto out;
744 }
745 sc->sc_txd[i].d_len = 0;
984263bc 746 }
acffed4b
JS
747 }
748 sc->sc_txcur = sc->sc_txnext = 0;
984263bc 749
acffed4b
JS
750 /* Enable desired port */
751 wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
984263bc 752
acffed4b
JS
753 sc->sc_enabled = 1;
754 ifp->if_flags |= IFF_RUNNING;
755 ifp->if_flags &= ~IFF_OACTIVE;
756 if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
757 ic->ic_opmode == IEEE80211_M_MONITOR ||
758 ic->ic_opmode == IEEE80211_M_HOSTAP)
759 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
984263bc 760
f35cfef5
JS
761 /* Enable interrupts if not polling */
762#ifdef DEVICE_POLLING
763 if ((ifp->if_flags & IFF_POLLING) == 0)
764#endif
765 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
984263bc 766
acffed4b
JS
767 if (!wasenabled &&
768 ic->ic_opmode == IEEE80211_M_HOSTAP &&
769 sc->sc_firmware_type == WI_INTERSIL) {
770 /* XXX: some card need to be re-enabled for hostap */
771 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
772 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
773 }
774
775 if (ic->ic_opmode == IEEE80211_M_STA &&
776 ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
777 ic->ic_des_chan != IEEE80211_CHAN_ANYC)) {
778 memset(&join, 0, sizeof(join));
779 if (ic->ic_flags & IEEE80211_F_DESBSSID)
780 IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
781 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC)
782 join.wi_chan = htole16(
783 ieee80211_chan2ieee(ic, ic->ic_des_chan));
784 /* Lucent firmware does not support the JOIN RID. */
785 if (sc->sc_firmware_type != WI_LUCENT)
786 wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
787 }
788
789 WI_UNLOCK(sc);
790 return;
791out:
792 if (error) {
793 if_printf(ifp, "interface not running\n");
794 wi_stop(ifp, 1);
795 }
796 WI_UNLOCK(sc);
4c30eb3b 797 DPRINTF((ifp, "wi_init: return %d\n", error));
acffed4b
JS
798 return;
799}
984263bc 800
acffed4b
JS
801void
802wi_stop(struct ifnet *ifp, int disable)
803{
804 struct ieee80211com *ic = (struct ieee80211com *) ifp;
805 struct wi_softc *sc = ifp->if_softc;
806 WI_LOCK_DECL();
984263bc 807
acffed4b 808 WI_LOCK(sc);
984263bc 809
acffed4b 810 DELAY(100000);
984263bc 811
9c095379 812 ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
acffed4b
JS
813 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
814 if (sc->sc_enabled && !sc->wi_gone) {
815 CSR_WRITE_2(sc, WI_INT_EN, 0);
816 wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
817 if (disable) {
818#ifdef __NetBSD__
819 if (sc->sc_disable)
820 (*sc->sc_disable)(sc);
821#endif
822 sc->sc_enabled = 0;
984263bc 823 }
acffed4b
JS
824 } else if (sc->wi_gone && disable) /* gone --> not enabled */
825 sc->sc_enabled = 0;
826
827 sc->sc_tx_timer = 0;
828 sc->sc_scan_timer = 0;
829 sc->sc_syn_timer = 0;
830 sc->sc_false_syns = 0;
831 sc->sc_naps = 0;
acffed4b
JS
832 ifp->if_timer = 0;
833
834 WI_UNLOCK(sc);
984263bc
MD
835}
836
837static void
acffed4b 838wi_start(struct ifnet *ifp)
984263bc 839{
acffed4b
JS
840 struct wi_softc *sc = ifp->if_softc;
841 struct ieee80211com *ic = &sc->sc_ic;
842 struct ieee80211_node *ni;
843 struct ieee80211_frame *wh;
844 struct mbuf *m0;
845 struct wi_frame frmhdr;
846 int cur, fid, off, error;
847 WI_LOCK_DECL();
984263bc 848
acffed4b 849 WI_LOCK(sc);
984263bc 850
acffed4b
JS
851 if (sc->wi_gone) {
852 WI_UNLOCK(sc);
984263bc
MD
853 return;
854 }
acffed4b
JS
855 if (sc->sc_flags & WI_FLAGS_OUTRANGE) {
856 WI_UNLOCK(sc);
984263bc 857 return;
984263bc
MD
858 }
859
acffed4b
JS
860 memset(&frmhdr, 0, sizeof(frmhdr));
861 cur = sc->sc_txnext;
862 for (;;) {
863 IF_POLL(&ic->ic_mgtq, m0);
864 if (m0 != NULL) {
865 if (sc->sc_txd[cur].d_len != 0) {
866 ifp->if_flags |= IFF_OACTIVE;
867 break;
868 }
869 IF_DEQUEUE(&ic->ic_mgtq, m0);
870 /*
871 * Hack! The referenced node pointer is in the
872 * rcvif field of the packet header. This is
873 * placed there by ieee80211_mgmt_output because
874 * we need to hold the reference with the frame
875 * and there's no other way (other than packet
876 * tags which we consider too expensive to use)
877 * to pass it along.
878 */
879 ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
880 m0->m_pkthdr.rcvif = NULL;
984263bc 881
acffed4b
JS
882 m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
883 (caddr_t)&frmhdr.wi_ehdr);
884 frmhdr.wi_ehdr.ether_type = 0;
885 wh = mtod(m0, struct ieee80211_frame *);
886 } else {
887 if (ic->ic_state != IEEE80211_S_RUN)
888 break;
4d723e5a 889 m0 = ifq_poll(&ifp->if_snd);
acffed4b
JS
890 if (m0 == NULL)
891 break;
892 if (sc->sc_txd[cur].d_len != 0) {
893 ifp->if_flags |= IFF_OACTIVE;
894 break;
895 }
d2c71fa0 896 ifq_dequeue(&ifp->if_snd, m0);
acffed4b
JS
897 ifp->if_opackets++;
898 m_copydata(m0, 0, ETHER_HDR_LEN,
899 (caddr_t)&frmhdr.wi_ehdr);
900 BPF_MTAP(ifp, m0);
901
902 m0 = ieee80211_encap(ifp, m0, &ni);
903 if (m0 == NULL) {
904 ifp->if_oerrors++;
905 continue;
906 }
907 wh = mtod(m0, struct ieee80211_frame *);
908 if (ic->ic_flags & IEEE80211_F_WEPON)
909 wh->i_fc[1] |= IEEE80211_FC1_WEP;
984263bc 910
984263bc 911 }
1f8e62c9
JS
912
913 if (ic->ic_rawbpf != NULL)
acffed4b 914 bpf_mtap(ic->ic_rawbpf, m0);
1f8e62c9 915
acffed4b
JS
916 frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
917 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
918 (wh->i_fc[1] & IEEE80211_FC1_WEP)) {
919 if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) {
920 ifp->if_oerrors++;
921 if (ni && ni != ic->ic_bss)
922 ieee80211_free_node(ic, ni);
923 continue;
984263bc 924 }
acffed4b 925 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
984263bc 926 }
1f8e62c9 927
acffed4b
JS
928 if (sc->sc_drvbpf) {
929 sc->sc_tx_th.wt_rate =
930 ni->ni_rates.rs_rates[ni->ni_txrate];
1f8e62c9
JS
931 bpf_ptap(sc->sc_drvbpf, m0, &sc->sc_tx_th,
932 sc->sc_tx_th_len);
acffed4b 933 }
1f8e62c9 934
acffed4b
JS
935 m_copydata(m0, 0, sizeof(struct ieee80211_frame),
936 (caddr_t)&frmhdr.wi_whdr);
937 m_adj(m0, sizeof(struct ieee80211_frame));
938 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
939 if (IFF_DUMPPKTS(ifp))
940 wi_dump_pkt(&frmhdr, NULL, -1);
941 fid = sc->sc_txd[cur].d_fid;
942 off = sizeof(frmhdr);
943 error = wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0
944 || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
945 m_freem(m0);
946 if (ni && ni != ic->ic_bss)
947 ieee80211_free_node(ic, ni);
948 if (error) {
949 ifp->if_oerrors++;
950 continue;
951 }
952 sc->sc_txd[cur].d_len = off;
953 if (sc->sc_txcur == cur) {
954 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
955 if_printf(ifp, "xmit failed\n");
956 sc->sc_txd[cur].d_len = 0;
957 continue;
958 }
959 sc->sc_tx_timer = 5;
960 ifp->if_timer = 1;
961 }
962 sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
984263bc
MD
963 }
964
acffed4b 965 WI_UNLOCK(sc);
984263bc
MD
966}
967
acffed4b
JS
968static int
969wi_reset(struct wi_softc *sc)
984263bc 970{
acffed4b
JS
971 struct ieee80211com *ic = &sc->sc_ic;
972 struct ifnet *ifp = &ic->ic_if;
984263bc
MD
973#define WI_INIT_TRIES 3
974 int i;
acffed4b 975 int error = 0;
984263bc
MD
976 int tries;
977
978 /* Symbol firmware cannot be initialized more than once */
acffed4b
JS
979 if (sc->sc_firmware_type == WI_SYMBOL && sc->sc_reset)
980 return (0);
984263bc
MD
981 if (sc->sc_firmware_type == WI_SYMBOL)
982 tries = 1;
983 else
984 tries = WI_INIT_TRIES;
985
986 for (i = 0; i < tries; i++) {
acffed4b 987 if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
984263bc
MD
988 break;
989 DELAY(WI_DELAY * 1000);
990 }
acffed4b 991 sc->sc_reset = 1;
984263bc
MD
992
993 if (i == tries) {
acffed4b
JS
994 if_printf(ifp, "init failed\n");
995 return (error);
984263bc
MD
996 }
997
998 CSR_WRITE_2(sc, WI_INT_EN, 0);
999 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
1000
1001 /* Calibrate timer. */
acffed4b 1002 wi_write_val(sc, WI_RID_TICK_TIME, 8);
984263bc 1003
acffed4b
JS
1004 return (0);
1005#undef WI_INIT_TRIES
984263bc
MD
1006}
1007
acffed4b
JS
1008static void
1009wi_watchdog(struct ifnet *ifp)
984263bc 1010{
acffed4b 1011 struct wi_softc *sc = ifp->if_softc;
984263bc 1012
acffed4b
JS
1013 ifp->if_timer = 0;
1014 if (!sc->sc_enabled)
1015 return;
1016
1017 if (sc->sc_tx_timer) {
1018 if (--sc->sc_tx_timer == 0) {
1019 if_printf(ifp, "device timeout\n");
1020 ifp->if_oerrors++;
1021 wi_init(ifp->if_softc);
1022 return;
984263bc 1023 }
acffed4b 1024 ifp->if_timer = 1;
984263bc
MD
1025 }
1026
acffed4b
JS
1027 if (sc->sc_scan_timer) {
1028 if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
1029 sc->sc_firmware_type == WI_INTERSIL) {
4c30eb3b 1030 DPRINTF((ifp, "wi_watchdog: inquire scan\n"));
acffed4b
JS
1031 wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
1032 }
1033 if (sc->sc_scan_timer)
1034 ifp->if_timer = 1;
1035 }
1036
1037 if (sc->sc_syn_timer) {
1038 if (--sc->sc_syn_timer == 0) {
1039 struct ieee80211com *ic = (struct ieee80211com *) ifp;
4c30eb3b 1040 DPRINTF2((ifp, "wi_watchdog: %d false syns\n",
acffed4b
JS
1041 sc->sc_false_syns));
1042 sc->sc_false_syns = 0;
1043 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1044 sc->sc_syn_timer = 5;
1045 }
1046 ifp->if_timer = 1;
1047 }
984263bc 1048
acffed4b
JS
1049 /* TODO: rate control */
1050 ieee80211_watchdog(ifp);
1051}
984263bc 1052
acffed4b
JS
1053static int
1054wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1055{
1056 struct wi_softc *sc = ifp->if_softc;
1057 struct ieee80211com *ic = &sc->sc_ic;
1058 struct ifreq *ifr = (struct ifreq *)data;
1059 struct ieee80211req *ireq;
1060 u_int8_t nodename[IEEE80211_NWID_LEN];
1061 int error = 0;
1062 struct wi_req wreq;
1063 WI_LOCK_DECL();
1064
1065 WI_LOCK(sc);
1066
1067 if (sc->wi_gone) {
1068 error = ENODEV;
1069 goto out;
1070 }
1071
1072 switch (cmd) {
1073 case SIOCSIFFLAGS:
984263bc 1074 /*
acffed4b
JS
1075 * Can't do promisc and hostap at the same time. If all that's
1076 * changing is the promisc flag, try to short-circuit a call to
1077 * wi_init() by just setting PROMISC in the hardware.
984263bc 1078 */
acffed4b
JS
1079 if (ifp->if_flags & IFF_UP) {
1080 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1081 ifp->if_flags & IFF_RUNNING) {
1082 if (ifp->if_flags & IFF_PROMISC &&
1083 !(sc->sc_if_flags & IFF_PROMISC)) {
1084 wi_write_val(sc, WI_RID_PROMISC, 1);
1085 } else if (!(ifp->if_flags & IFF_PROMISC) &&
1086 sc->sc_if_flags & IFF_PROMISC) {
1087 wi_write_val(sc, WI_RID_PROMISC, 0);
1088 } else {
1089 wi_init(sc);
1090 }
1091 } else {
1092 wi_init(sc);
984263bc 1093 }
acffed4b
JS
1094 } else {
1095 if (ifp->if_flags & IFF_RUNNING) {
1096 wi_stop(ifp, 1);
1097 }
1098 sc->wi_gone = 0;
1099 }
1100 sc->sc_if_flags = ifp->if_flags;
1101 error = 0;
1102 break;
1103 case SIOCADDMULTI:
1104 case SIOCDELMULTI:
1105 error = wi_write_multi(sc);
1106 break;
1107 case SIOCGIFGENERIC:
1108 error = wi_get_cfg(ifp, cmd, data, cr);
1109 break;
1110 case SIOCSIFGENERIC:
1111 error = suser_cred(cr, NULL_CRED_OKAY);
1112 if (error)
984263bc 1113 break;
acffed4b
JS
1114 error = wi_set_cfg(ifp, cmd, data);
1115 break;
1116 case SIOCGPRISM2DEBUG:
1117 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1118 if (error)
984263bc 1119 break;
acffed4b
JS
1120 if (!(ifp->if_flags & IFF_RUNNING) ||
1121 sc->sc_firmware_type == WI_LUCENT) {
1122 error = EIO;
984263bc
MD
1123 break;
1124 }
acffed4b
JS
1125 error = wi_get_debug(sc, &wreq);
1126 if (error == 0)
1127 error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
1128 break;
1129 case SIOCSPRISM2DEBUG:
1130 if ((error = suser_cred(cr, NULL_CRED_OKAY)))
1131 goto out;
1132 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1133 if (error)
984263bc 1134 break;
acffed4b
JS
1135 error = wi_set_debug(sc, &wreq);
1136 break;
1137 case SIOCG80211:
1138 ireq = (struct ieee80211req *) data;
1139 switch (ireq->i_type) {
1140 case IEEE80211_IOC_STATIONNAME:
1141 ireq->i_len = sc->sc_nodelen + 1;
1142 error = copyout(sc->sc_nodename, ireq->i_data,
1143 ireq->i_len);
984263bc 1144 break;
acffed4b
JS
1145 default:
1146 error = ieee80211_ioctl(ifp, cmd, data, cr);
984263bc 1147 break;
acffed4b
JS
1148 }
1149 break;
1150 case SIOCS80211:
1151 error = suser_cred(cr, NULL_CRED_OKAY);
1152 if (error)
1153 break;
1154 ireq = (struct ieee80211req *) data;
1155 switch (ireq->i_type) {
1156 case IEEE80211_IOC_STATIONNAME:
1157 if (ireq->i_val != 0 ||
1158 ireq->i_len > IEEE80211_NWID_LEN) {
1159 error = EINVAL;
1160 break;
1161 }
1162 memset(nodename, 0, IEEE80211_NWID_LEN);
1163 error = copyin(ireq->i_data, nodename, ireq->i_len);
1164 if (error)
1165 break;
1166 if (sc->sc_enabled) {
1167 error = wi_write_ssid(sc, WI_RID_NODENAME,
1168 nodename, ireq->i_len);
984263bc 1169 if (error)
acffed4b 1170 break;
984263bc 1171 }
acffed4b
JS
1172 memcpy(sc->sc_nodename, nodename, IEEE80211_NWID_LEN);
1173 sc->sc_nodelen = ireq->i_len;
1174 break;
1175 default:
1176 error = ieee80211_ioctl(ifp, cmd, data, cr);
984263bc 1177 break;
984263bc 1178 }
acffed4b 1179 break;
f35cfef5 1180 case SIOCSIFCAP:
f35cfef5
JS
1181 if (ifp->if_flags & IFF_RUNNING)
1182 wi_init(sc);
1183 break;
acffed4b
JS
1184 default:
1185 error = ieee80211_ioctl(ifp, cmd, data, cr);
1186 break;
1187 }
1188 if (error == ENETRESET) {
1189 if (sc->sc_enabled)
1190 wi_init(sc); /* XXX no error return */
1191 error = 0;
1192 }
1193out:
1194 WI_UNLOCK(sc);
1195
1196 return (error);
1197}
1198
1199static int
1200wi_media_change(struct ifnet *ifp)
1201{
1202 struct wi_softc *sc = ifp->if_softc;
1203 int error;
1204
1205 error = ieee80211_media_change(ifp);
1206 if (error == ENETRESET) {
1207 if (sc->sc_enabled)
1208 wi_init(sc); /* XXX no error return */
1209 error = 0;
1210 }
1211 return error;
1212}
1213
1214static void
1215wi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1216{
1217 struct wi_softc *sc = ifp->if_softc;
1218 struct ieee80211com *ic = &sc->sc_ic;
1219 u_int16_t val;
1220 int rate, len;
1221
1222 if (sc->wi_gone || !sc->sc_enabled) {
1223 imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
1224 imr->ifm_status = 0;
1225 return;
1226 }
1227
1228 imr->ifm_status = IFM_AVALID;
1229 imr->ifm_active = IFM_IEEE80211;
1230 if (ic->ic_state == IEEE80211_S_RUN &&
1231 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
1232 imr->ifm_status |= IFM_ACTIVE;
1233 len = sizeof(val);
1234 if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) != 0)
1235 rate = 0;
1236 else {
1237 /* convert to 802.11 rate */
1238 rate = val * 2;
1239 if (sc->sc_firmware_type == WI_LUCENT) {
1240 if (rate == 4 * 2)
1241 rate = 11; /* 5.5Mbps */
1242 else if (rate == 5 * 2)
1243 rate = 22; /* 11Mbps */
1244 } else {
1245 if (rate == 4*2)
1246 rate = 11; /* 5.5Mbps */
1247 else if (rate == 8*2)
1248 rate = 22; /* 11Mbps */
984263bc
MD
1249 }
1250 }
acffed4b
JS
1251 imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
1252 switch (ic->ic_opmode) {
1253 case IEEE80211_M_STA:
1254 break;
1255 case IEEE80211_M_IBSS:
1256 imr->ifm_active |= IFM_IEEE80211_ADHOC;
1257 break;
1258 case IEEE80211_M_AHDEMO:
1259 imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1260 break;
1261 case IEEE80211_M_HOSTAP:
1262 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1263 break;
1264 case IEEE80211_M_MONITOR:
1265 imr->ifm_active |= IFM_IEEE80211_MONITOR;
1266 break;
1267 }
1268}
984263bc 1269
acffed4b
JS
1270static void
1271wi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1272{
1273 struct ieee80211com *ic = &sc->sc_ic;
1274 struct ieee80211_node *ni = ic->ic_bss;
1275 struct ifnet *ifp = &ic->ic_if;
984263bc 1276
acffed4b
JS
1277 if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1278 return;
984263bc 1279
4c30eb3b
JS
1280 DPRINTF((ifp, "wi_sync_bssid: bssid %6D -> %6D ?\n", ni->ni_bssid, ":",
1281 new_bssid, ":"));
984263bc 1282
acffed4b
JS
1283 /* In promiscuous mode, the BSSID field is not a reliable
1284 * indicator of the firmware's BSSID. Damp spurious
1285 * change-of-BSSID indications.
1286 */
1287 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1288 sc->sc_false_syns >= WI_MAX_FALSE_SYNS)
1289 return;
984263bc 1290
acffed4b 1291 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
984263bc
MD
1292}
1293
acffed4b
JS
1294static void
1295wi_rx_monitor(struct wi_softc *sc, int fid)
984263bc 1296{
acffed4b
JS
1297 struct ieee80211com *ic = &sc->sc_ic;
1298 struct ifnet *ifp = &ic->ic_if;
1299 struct wi_frame *rx_frame;
1300 struct mbuf *m;
1301 int datlen, hdrlen;
1302
1303 /* first allocate mbuf for packet storage */
1304 m = m_getcl(MB_DONTWAIT, MT_DATA, 0);
1305 if (m == NULL) {
1306 ifp->if_ierrors++;
1307 return;
1308 }
984263bc 1309
acffed4b
JS
1310 m->m_pkthdr.rcvif = ifp;
1311
1312 /* now read wi_frame first so we know how much data to read */
1313 if (wi_read_bap(sc, fid, 0, mtod(m, caddr_t), sizeof(*rx_frame))) {
1314 ifp->if_ierrors++;
1315 goto done;
984263bc
MD
1316 }
1317
acffed4b 1318 rx_frame = mtod(m, struct wi_frame *);
984263bc 1319
acffed4b
JS
1320 switch ((rx_frame->wi_status & WI_STAT_MAC_PORT) >> 8) {
1321 case 7:
1322 switch (rx_frame->wi_whdr.i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1323 case IEEE80211_FC0_TYPE_DATA:
1324 hdrlen = WI_DATA_HDRLEN;
1325 datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
984263bc 1326 break;
acffed4b
JS
1327 case IEEE80211_FC0_TYPE_MGT:
1328 hdrlen = WI_MGMT_HDRLEN;
1329 datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1330 break;
1331 case IEEE80211_FC0_TYPE_CTL:
1332 /*
1333 * prism2 cards don't pass control packets
1334 * down properly or consistently, so we'll only
1335 * pass down the header.
1336 */
1337 hdrlen = WI_CTL_HDRLEN;
1338 datlen = 0;
1339 break;
1340 default:
1341 if_printf(ifp, "received packet of unknown type "
1342 "on port 7\n");
1343 ifp->if_ierrors++;
1344 goto done;
1345 }
1346 break;
1347 case 0:
1348 hdrlen = WI_DATA_HDRLEN;
1349 datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
1350 break;
1351 default:
1352 if_printf(ifp, "received packet on invalid "
1353 "port (wi_status=0x%x)\n", rx_frame->wi_status);
1354 ifp->if_ierrors++;
1355 goto done;
984263bc
MD
1356 }
1357
acffed4b
JS
1358 if (hdrlen + datlen + 2 > MCLBYTES) {
1359 if_printf(ifp, "oversized packet received "
1360 "(wi_dat_len=%d, wi_status=0x%x)\n",
1361 datlen, rx_frame->wi_status);
1362 ifp->if_ierrors++;
1363 goto done;
984263bc
MD
1364 }
1365
acffed4b
JS
1366 if (wi_read_bap(sc, fid, hdrlen, mtod(m, caddr_t) + hdrlen,
1367 datlen + 2) == 0) {
1368 m->m_pkthdr.len = m->m_len = hdrlen + datlen;
1369 ifp->if_ipackets++;
1370 BPF_MTAP(ifp, m); /* Handle BPF listeners. */
1371 } else
1372 ifp->if_ierrors++;
1373done:
1374 m_freem(m);
984263bc
MD
1375}
1376
acffed4b
JS
1377static void
1378wi_rx_intr(struct wi_softc *sc)
984263bc 1379{
acffed4b
JS
1380 struct ieee80211com *ic = &sc->sc_ic;
1381 struct ifnet *ifp = &ic->ic_if;
1382 struct wi_frame frmhdr;
1383 struct mbuf *m;
1384 struct ieee80211_frame *wh;
1385 struct ieee80211_node *ni;
1386 int fid, len, off, rssi;
1387 u_int8_t dir;
1388 u_int16_t status;
1389 u_int32_t rstamp;
1390
1391 fid = CSR_READ_2(sc, WI_RX_FID);
1392
1393 if (sc->wi_debug.wi_monitor) {
1394 /*
1395 * If we are in monitor mode just
1396 * read the data from the device.
1397 */
1398 wi_rx_monitor(sc, fid);
1399 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1400 return;
1401 }
984263bc 1402
acffed4b
JS
1403 /* First read in the frame header */
1404 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1405 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1406 ifp->if_ierrors++;
4c30eb3b 1407 DPRINTF((ifp, "wi_rx_intr: read fid %x failed\n", fid));
acffed4b
JS
1408 return;
1409 }
984263bc 1410
acffed4b
JS
1411 if (IFF_DUMPPKTS(ifp))
1412 wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal);
984263bc 1413
acffed4b
JS
1414 /*
1415 * Drop undecryptable or packets with receive errors here
1416 */
1417 status = le16toh(frmhdr.wi_status);
1418 if (status & WI_STAT_ERRSTAT) {
1419 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1420 ifp->if_ierrors++;
4c30eb3b
JS
1421 DPRINTF((ifp, "wi_rx_intr: fid %x error status %x\n",
1422 fid, status));
acffed4b
JS
1423 return;
1424 }
1425 rssi = frmhdr.wi_rx_signal;
1426 rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1427 le16toh(frmhdr.wi_rx_tstamp1);
984263bc 1428
acffed4b
JS
1429 len = le16toh(frmhdr.wi_dat_len);
1430 off = ALIGN(sizeof(struct ieee80211_frame));
984263bc 1431
acffed4b
JS
1432 /*
1433 * Sometimes the PRISM2.x returns bogusly large frames. Except
1434 * in monitor mode, just throw them away.
1435 */
1436 if (off + len > MCLBYTES) {
1437 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1438 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1439 ifp->if_ierrors++;
4c30eb3b 1440 DPRINTF((ifp, "wi_rx_intr: oversized packet\n"));
acffed4b
JS
1441 return;
1442 } else
1443 len = 0;
1444 }
984263bc 1445
17b71a59 1446 m = m_getl(off + len, MB_DONTWAIT, MT_DATA, M_PKTHDR, NULL);
acffed4b
JS
1447 if (m == NULL) {
1448 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1449 ifp->if_ierrors++;
4c30eb3b 1450 DPRINTF((ifp, "wi_rx_intr: m_getl failed\n"));
acffed4b
JS
1451 return;
1452 }
984263bc 1453
acffed4b
JS
1454 m->m_data += off - sizeof(struct ieee80211_frame);
1455 memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1456 wi_read_bap(sc, fid, sizeof(frmhdr),
1457 m->m_data + sizeof(struct ieee80211_frame), len);
1458 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1459 m->m_pkthdr.rcvif = ifp;
984263bc 1460
acffed4b 1461 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
984263bc 1462
acffed4b
JS
1463 if (sc->sc_drvbpf) {
1464 /* XXX replace divide by table */
1465 sc->sc_rx_th.wr_rate = frmhdr.wi_rx_rate / 5;
1466 sc->sc_rx_th.wr_antsignal = frmhdr.wi_rx_signal;
1467 sc->sc_rx_th.wr_antnoise = frmhdr.wi_rx_silence;
1468 sc->sc_rx_th.wr_flags = 0;
1469 if (frmhdr.wi_status & WI_STAT_PCF)
1470 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_CFP;
1f8e62c9 1471 bpf_ptap(sc->sc_drvbpf, m, &sc->sc_rx_th, sc->sc_rx_th_len);
984263bc 1472 }
1f8e62c9 1473
acffed4b
JS
1474 wh = mtod(m, struct ieee80211_frame *);
1475 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1476 /*
1477 * WEP is decrypted by hardware. Clear WEP bit
1478 * header for ieee80211_input().
1479 */
1480 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1481 }
984263bc 1482
acffed4b
JS
1483 /* synchronize driver's BSSID with firmware's BSSID */
1484 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1485 if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1486 wi_sync_bssid(sc, wh->i_addr3);
1487
1488 /*
1489 * Locate the node for sender, track state, and
1490 * then pass this node (referenced) up to the 802.11
1491 * layer for its use. We are required to pass
1492 * something so we fallback to ic_bss when this frame
1493 * is from an unknown sender.
1494 */
1495 if (ic->ic_opmode != IEEE80211_M_STA) {
1496 ni = ieee80211_find_node(ic, wh->i_addr2);
1497 if (ni == NULL)
1498 ni = ieee80211_ref_node(ic->ic_bss);
1499 } else
1500 ni = ieee80211_ref_node(ic->ic_bss);
1501 /*
1502 * Send frame up for processing.
1503 */
1504 ieee80211_input(ifp, m, ni, rssi, rstamp);
1505 /*
1506 * The frame may have caused the node to be marked for
1507 * reclamation (e.g. in response to a DEAUTH message)
1508 * so use free_node here instead of unref_node.
1509 */
1510 if (ni == ic->ic_bss)
1511 ieee80211_unref_node(&ni);
1512 else
1513 ieee80211_free_node(ic, ni);
984263bc
MD
1514}
1515
acffed4b
JS
1516static void
1517wi_tx_ex_intr(struct wi_softc *sc)
984263bc 1518{
acffed4b
JS
1519 struct ieee80211com *ic = &sc->sc_ic;
1520 struct ifnet *ifp = &ic->ic_if;
1521 struct wi_frame frmhdr;
1522 int fid;
984263bc 1523
acffed4b
JS
1524 fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1525 /* Read in the frame header */
1526 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1527 u_int16_t status = le16toh(frmhdr.wi_status);
984263bc 1528
acffed4b
JS
1529 /*
1530 * Spontaneous station disconnects appear as xmit
1531 * errors. Don't announce them and/or count them
1532 * as an output error.
1533 */
1534 if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1535 if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1536 if_printf(ifp, "tx failed");
1537 if (status & WI_TXSTAT_RET_ERR)
1538 printf(", retry limit exceeded");
1539 if (status & WI_TXSTAT_AGED_ERR)
1540 printf(", max transmit lifetime exceeded");
1541 if (status & WI_TXSTAT_DISCONNECT)
1542 printf(", port disconnected");
1543 if (status & WI_TXSTAT_FORM_ERR)
1544 printf(", invalid format (data len %u src %6D)",
1545 le16toh(frmhdr.wi_dat_len),
1546 frmhdr.wi_ehdr.ether_shost, ":");
1547 if (status & ~0xf)
1548 printf(", status=0x%x", status);
1549 printf("\n");
1550 }
1551 ifp->if_oerrors++;
1552 } else {
4c30eb3b 1553 DPRINTF((ifp, "port disconnected\n"));
acffed4b
JS
1554 ifp->if_collisions++; /* XXX */
1555 }
1556 } else
4c30eb3b 1557 DPRINTF((ifp, "wi_tx_ex_intr: read fid %x failed\n", fid));
acffed4b
JS
1558 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1559}
984263bc 1560
acffed4b
JS
1561static void
1562wi_tx_intr(struct wi_softc *sc)
1563{
1564 struct ieee80211com *ic = &sc->sc_ic;
1565 struct ifnet *ifp = &ic->ic_if;
1566 int fid, cur;
1567
1568 if (sc->wi_gone)
1569 return;
984263bc 1570
acffed4b 1571 fid = CSR_READ_2(sc, WI_ALLOC_FID);
984263bc 1572 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
984263bc 1573
acffed4b
JS
1574 cur = sc->sc_txcur;
1575 if (sc->sc_txd[cur].d_fid != fid) {
1576 if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
1577 fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
1578 return;
1579 }
1580 sc->sc_tx_timer = 0;
1581 sc->sc_txd[cur].d_len = 0;
1582 sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
1583 if (sc->sc_txd[cur].d_len == 0)
1584 ifp->if_flags &= ~IFF_OACTIVE;
1585 else {
1586 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1587 0, 0)) {
1588 if_printf(ifp, "xmit failed\n");
1589 sc->sc_txd[cur].d_len = 0;
1590 } else {
1591 sc->sc_tx_timer = 5;
1592 ifp->if_timer = 1;
1593 }
984263bc 1594 }
984263bc
MD
1595}
1596
1597static void
acffed4b 1598wi_info_intr(struct wi_softc *sc)
984263bc 1599{
acffed4b
JS
1600 struct ieee80211com *ic = &sc->sc_ic;
1601 struct ifnet *ifp = &ic->ic_if;
1602 int i, fid, len, off;
1603 u_int16_t ltbuf[2];
1604 u_int16_t stat;
1605 u_int32_t *ptr;
1606
1607 fid = CSR_READ_2(sc, WI_INFO_FID);
1608 wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
1609
1610 switch (le16toh(ltbuf[1])) {
1611
1612 case WI_INFO_LINK_STAT:
1613 wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
4c30eb3b 1614 DPRINTF((ifp, "wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
acffed4b
JS
1615 switch (le16toh(stat)) {
1616 case WI_INFO_LINK_STAT_CONNECTED:
1617 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1618 if (ic->ic_state == IEEE80211_S_RUN &&
1619 ic->ic_opmode != IEEE80211_M_IBSS)
1620 break;
1621 /* FALLTHROUGH */
1622 case WI_INFO_LINK_STAT_AP_CHG:
1623 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1624 break;
1625 case WI_INFO_LINK_STAT_AP_INR:
1626 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1627 break;
1628 case WI_INFO_LINK_STAT_AP_OOR:
1629 if (sc->sc_firmware_type == WI_SYMBOL &&
1630 sc->sc_scan_timer > 0) {
1631 if (wi_cmd(sc, WI_CMD_INQUIRE,
1632 WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
1633 sc->sc_scan_timer = 0;
1634 break;
1635 }
1636 if (ic->ic_opmode == IEEE80211_M_STA)
1637 sc->sc_flags |= WI_FLAGS_OUTRANGE;
1638 break;
1639 case WI_INFO_LINK_STAT_DISCONNECTED:
1640 case WI_INFO_LINK_STAT_ASSOC_FAILED:
1641 if (ic->ic_opmode == IEEE80211_M_STA)
1642 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1643 break;
1644 }
1645 break;
1646
1647 case WI_INFO_COUNTERS:
1648 /* some card versions have a larger stats structure */
1649 len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1650 ptr = (u_int32_t *)&sc->sc_stats;
1651 off = sizeof(ltbuf);
1652 for (i = 0; i < len; i++, off += 2, ptr++) {
1653 wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1654#ifdef WI_HERMES_STATS_WAR
1655 if (stat & 0xf000)
1656 stat = ~stat;
1657#endif
1658 *ptr += stat;
1659 }
1660 ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1661 sc->sc_stats.wi_tx_multi_retries +
1662 sc->sc_stats.wi_tx_retry_limit;
1663 break;
984263bc 1664
acffed4b
JS
1665 case WI_INFO_SCAN_RESULTS:
1666 case WI_INFO_HOST_SCAN_RESULTS:
1667 wi_scan_result(sc, fid, le16toh(ltbuf[0]));
1668 break;
984263bc 1669
acffed4b 1670 default:
4c30eb3b 1671 DPRINTF((ifp, "wi_info_intr: got fid %x type %x len %d\n", fid,
acffed4b
JS
1672 le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1673 break;
1674 }
1675 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1676}
984263bc 1677
acffed4b
JS
1678static int
1679wi_write_multi(struct wi_softc *sc)
1680{
1681 struct ifnet *ifp = &sc->sc_ic.ic_if;
1682 int n;
1683 struct ifmultiaddr *ifma;
1684 struct wi_mcast mlist;
984263bc
MD
1685
1686 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
acffed4b
JS
1687allmulti:
1688 memset(&mlist, 0, sizeof(mlist));
1689 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1690 sizeof(mlist));
984263bc
MD
1691 }
1692
acffed4b 1693 n = 0;
984263bc 1694 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
984263bc
MD
1695 if (ifma->ifma_addr->sa_family != AF_LINK)
1696 continue;
acffed4b
JS
1697 if (n >= 16)
1698 goto allmulti;
1699 IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1700 (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1701 n++;
1702 }
1703 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1704 IEEE80211_ADDR_LEN * n);
1705}
1706
1707static void
1708wi_read_nicid(struct wi_softc *sc)
1709{
1710 struct wi_card_ident *id;
1711 char *p;
1712 int len;
1713 u_int16_t ver[4];
1714
1715 /* getting chip identity */
1716 memset(ver, 0, sizeof(ver));
1717 len = sizeof(ver);
1718 wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
4c30eb3b 1719 if_printf(&sc->sc_ic.ic_if, "using ");
acffed4b
JS
1720
1721 sc->sc_firmware_type = WI_NOTYPE;
1722 for (id = wi_card_ident; id->card_name != NULL; id++) {
1723 if (le16toh(ver[0]) == id->card_id) {
1724 printf("%s", id->card_name);
1725 sc->sc_firmware_type = id->firm_type;
984263bc
MD
1726 break;
1727 }
1728 }
acffed4b
JS
1729 if (sc->sc_firmware_type == WI_NOTYPE) {
1730 if (le16toh(ver[0]) & 0x8000) {
1731 printf("Unknown PRISM2 chip");
1732 sc->sc_firmware_type = WI_INTERSIL;
1733 } else {
1734 printf("Unknown Lucent chip");
1735 sc->sc_firmware_type = WI_LUCENT;
1736 }
1737 }
984263bc 1738
acffed4b
JS
1739 /* get primary firmware version (Only Prism chips) */
1740 if (sc->sc_firmware_type != WI_LUCENT) {
1741 memset(ver, 0, sizeof(ver));
1742 len = sizeof(ver);
1743 wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1744 sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1745 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1746 }
984263bc 1747
acffed4b
JS
1748 /* get station firmware version */
1749 memset(ver, 0, sizeof(ver));
1750 len = sizeof(ver);
1751 wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1752 sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1753 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1754 if (sc->sc_firmware_type == WI_INTERSIL &&
1755 (sc->sc_sta_firmware_ver == 10102 ||
1756 sc->sc_sta_firmware_ver == 20102)) {
1757 char ident[12];
1758 memset(ident, 0, sizeof(ident));
1759 len = sizeof(ident);
1760 /* value should be the format like "V2.00-11" */
1761 if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1762 *(p = (char *)ident) >= 'A' &&
1763 p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1764 sc->sc_firmware_type = WI_SYMBOL;
1765 sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1766 (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1767 (p[6] - '0') * 10 + (p[7] - '0');
1768 }
1769 }
1770 printf("\n");
4c30eb3b 1771 if_printf(&sc->sc_ic.ic_if, "%s Firmware: ",
acffed4b
JS
1772 sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
1773 (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
1774 if (sc->sc_firmware_type != WI_LUCENT) /* XXX */
1775 printf("Primary (%u.%u.%u), ",
1776 sc->sc_pri_firmware_ver / 10000,
1777 (sc->sc_pri_firmware_ver % 10000) / 100,
1778 sc->sc_pri_firmware_ver % 100);
1779 printf("Station (%u.%u.%u)\n",
1780 sc->sc_sta_firmware_ver / 10000,
1781 (sc->sc_sta_firmware_ver % 10000) / 100,
1782 sc->sc_sta_firmware_ver % 100);
984263bc
MD
1783}
1784
acffed4b
JS
1785static int
1786wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
984263bc 1787{
acffed4b
JS
1788 struct wi_ssid ssid;
1789
1790 if (buflen > IEEE80211_NWID_LEN)
1791 return ENOBUFS;
1792 memset(&ssid, 0, sizeof(ssid));
1793 ssid.wi_len = htole16(buflen);
1794 memcpy(ssid.wi_ssid, buf, buflen);
1795 return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1796}
984263bc 1797
acffed4b
JS
1798static int
1799wi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1800{
1801 struct wi_softc *sc = ifp->if_softc;
1802 struct ieee80211com *ic = &sc->sc_ic;
1803 struct ifreq *ifr = (struct ifreq *)data;
1804 struct wi_req wreq;
1805 struct wi_scan_res *res;
1806 size_t reslen;
1807 int len, n, error, mif, val, off, i;
1808
1809 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1810 if (error)
1811 return error;
1812 len = (wreq.wi_len - 1) * 2;
1813 if (len < sizeof(u_int16_t))
1814 return ENOSPC;
1815 if (len > sizeof(wreq.wi_val))
1816 len = sizeof(wreq.wi_val);
1817
1818 switch (wreq.wi_type) {
1819
1820 case WI_RID_IFACE_STATS:
1821 memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
1822 if (len < sizeof(sc->sc_stats))
1823 error = ENOSPC;
1824 else
1825 len = sizeof(sc->sc_stats);
984263bc 1826 break;
acffed4b
JS
1827
1828 case WI_RID_ENCRYPTION:
1829 case WI_RID_TX_CRYPT_KEY:
1830 case WI_RID_DEFLT_CRYPT_KEYS:
984263bc 1831 case WI_RID_TX_RATE:
acffed4b
JS
1832 return ieee80211_cfgget(ifp, cmd, data, cr);
1833
984263bc 1834 case WI_RID_MICROWAVE_OVEN:
acffed4b
JS
1835 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
1836 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1837 &len);
1838 break;
1839 }
1840 wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
1841 len = sizeof(u_int16_t);
984263bc 1842 break;
acffed4b
JS
1843
1844 case WI_RID_DBM_ADJUST:
1845 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
1846 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1847 &len);
1848 break;
1849 }
1850 wreq.wi_val[0] = htole16(sc->sc_dbm_offset);
1851 len = sizeof(u_int16_t);
984263bc 1852 break;
acffed4b 1853
984263bc 1854 case WI_RID_ROAMING_MODE:
acffed4b
JS
1855 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
1856 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1857 &len);
1858 break;
1859 }
1860 wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
1861 len = sizeof(u_int16_t);
984263bc 1862 break;
acffed4b
JS
1863
1864 case WI_RID_SYSTEM_SCALE:
1865 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
1866 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1867 &len);
1868 break;
1869 }
1870 wreq.wi_val[0] = htole16(sc->sc_system_scale);
1871 len = sizeof(u_int16_t);
984263bc 1872 break;
acffed4b
JS
1873
1874 case WI_RID_FRAG_THRESH:
1875 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
1876 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1877 &len);
1878 break;
1879 }
1880 wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
1881 len = sizeof(u_int16_t);
984263bc 1882 break;
acffed4b
JS
1883
1884 case WI_RID_READ_APS:
1885 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1886 return ieee80211_cfgget(ifp, cmd, data, cr);
1887 if (sc->sc_scan_timer > 0) {
1888 error = EINPROGRESS;
1889 break;
1890 }
1891 n = sc->sc_naps;
1892 if (len < sizeof(n)) {
1893 error = ENOSPC;
1894 break;
1895 }
1896 if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
1897 n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
1898 len = sizeof(n) + sizeof(struct wi_apinfo) * n;
1899 memcpy(wreq.wi_val, &n, sizeof(n));
1900 memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
1901 sizeof(struct wi_apinfo) * n);
984263bc 1902 break;
984263bc 1903
acffed4b
JS
1904 case WI_RID_PRISM2:
1905 wreq.wi_val[0] = sc->sc_firmware_type != WI_LUCENT;
1906 len = sizeof(u_int16_t);
1907 break;
984263bc 1908
acffed4b
JS
1909 case WI_RID_MIF:
1910 mif = wreq.wi_val[0];
1911 error = wi_cmd(sc, WI_CMD_READMIF, mif, 0, 0);
1912 val = CSR_READ_2(sc, WI_RESP0);
1913 wreq.wi_val[0] = val;
1914 len = sizeof(u_int16_t);
1915 break;
984263bc 1916
acffed4b
JS
1917 case WI_RID_ZERO_CACHE:
1918 case WI_RID_PROCFRAME: /* ignore for compatibility */
1919 /* XXX ??? */
1920 break;
984263bc 1921
acffed4b
JS
1922 case WI_RID_READ_CACHE:
1923 return ieee80211_cfgget(ifp, cmd, data, cr);
984263bc 1924
acffed4b
JS
1925 case WI_RID_SCAN_RES: /* compatibility interface */
1926 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1927 return ieee80211_cfgget(ifp, cmd, data, cr);
1928 if (sc->sc_scan_timer > 0) {
1929 error = EINPROGRESS;
984263bc 1930 break;
984263bc 1931 }
acffed4b
JS
1932 n = sc->sc_naps;
1933 if (sc->sc_firmware_type == WI_LUCENT) {
1934 off = 0;
1935 reslen = WI_WAVELAN_RES_SIZE;
984263bc 1936 } else {
acffed4b
JS
1937 off = sizeof(struct wi_scan_p2_hdr);
1938 reslen = WI_PRISM2_RES_SIZE;
1939 }
1940 if (len < off + reslen * n)
1941 n = (len - off) / reslen;
1942 len = off + reslen * n;
1943 if (off != 0) {
1944 struct wi_scan_p2_hdr *p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1945 /*
1946 * Prepend Prism-specific header.
1947 */
1948 if (len < sizeof(struct wi_scan_p2_hdr)) {
1949 error = ENOSPC;
984263bc
MD
1950 break;
1951 }
acffed4b
JS
1952 p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
1953 p2->wi_rsvd = 0;
1954 p2->wi_reason = n; /* XXX */
1955 }
1956 for (i = 0; i < n; i++, off += reslen) {
1957 const struct wi_apinfo *ap = &sc->sc_aps[i];
1958
1959 res = (struct wi_scan_res *)((char *)wreq.wi_val + off);
1960 res->wi_chan = ap->channel;
1961 res->wi_noise = ap->noise;
1962 res->wi_signal = ap->signal;
1963 IEEE80211_ADDR_COPY(res->wi_bssid, ap->bssid);
1964 res->wi_interval = ap->interval;
1965 res->wi_capinfo = ap->capinfo;
1966 res->wi_ssid_len = ap->namelen;
1967 memcpy(res->wi_ssid, ap->name,
1968 IEEE80211_NWID_LEN);
1969 if (sc->sc_firmware_type != WI_LUCENT) {
1970 /* XXX not saved from Prism cards */
1971 memset(res->wi_srates, 0,
1972 sizeof(res->wi_srates));
1973 res->wi_rate = ap->rate;
1974 res->wi_rsvd = 0;
1975 }
984263bc 1976 }
984263bc 1977 break;
acffed4b
JS
1978
1979 default:
1980 if (sc->sc_enabled) {
1981 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
1982 &len);
984263bc
MD
1983 break;
1984 }
acffed4b
JS
1985 switch (wreq.wi_type) {
1986 case WI_RID_MAX_DATALEN:
1987 wreq.wi_val[0] = htole16(sc->sc_max_datalen);
1988 len = sizeof(u_int16_t);
984263bc 1989 break;
acffed4b
JS
1990 case WI_RID_RTS_THRESH:
1991 wreq.wi_val[0] = htole16(ic->ic_rtsthreshold);
1992 len = sizeof(u_int16_t);
984263bc 1993 break;
acffed4b
JS
1994 case WI_RID_CNFAUTHMODE:
1995 wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
1996 len = sizeof(u_int16_t);
1997 break;
1998 case WI_RID_NODENAME:
1999 if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
2000 error = ENOSPC;
2001 break;
2002 }
2003 len = sc->sc_nodelen + sizeof(u_int16_t);
2004 wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
2005 memcpy(&wreq.wi_val[1], sc->sc_nodename,
2006 sc->sc_nodelen);
984263bc 2007 break;
acffed4b
JS
2008 default:
2009 return ieee80211_cfgget(ifp, cmd, data, cr);
984263bc 2010 }
984263bc 2011 break;
acffed4b
JS
2012 }
2013 if (error)
2014 return error;
2015 wreq.wi_len = (len + 1) / 2 + 1;
2016 return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
2017}
2018
2019static int
2020wi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
2021{
2022 struct wi_softc *sc = ifp->if_softc;
2023 struct ieee80211com *ic = &sc->sc_ic;
2024 struct ifreq *ifr = (struct ifreq *)data;
2025 struct wi_req wreq;
2026 struct mbuf *m;
2027 int i, len, error, mif, val;
2028 struct ieee80211_rateset *rs;
2029
2030 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2031 if (error)
2032 return error;
2033 len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0;
2034 switch (wreq.wi_type) {
2035 case WI_RID_DBM_ADJUST:
2036 return ENODEV;
2037
2038 case WI_RID_NODENAME:
2039 if (le16toh(wreq.wi_val[0]) * 2 > len ||
2040 le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
2041 error = ENOSPC;
984263bc 2042 break;
acffed4b
JS
2043 }
2044 if (sc->sc_enabled) {
2045 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2046 len);
2047 if (error)
2048 break;
2049 }
2050 sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
2051 memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen);
984263bc 2052 break;
acffed4b
JS
2053
2054 case WI_RID_MICROWAVE_OVEN:
2055 case WI_RID_ROAMING_MODE:
2056 case WI_RID_SYSTEM_SCALE:
2057 case WI_RID_FRAG_THRESH:
2058 if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
2059 (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
984263bc 2060 break;
acffed4b
JS
2061 if (wreq.wi_type == WI_RID_ROAMING_MODE &&
2062 (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
984263bc 2063 break;
acffed4b
JS
2064 if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
2065 (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
984263bc 2066 break;
acffed4b
JS
2067 if (wreq.wi_type == WI_RID_FRAG_THRESH &&
2068 (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
984263bc 2069 break;
acffed4b
JS
2070 /* FALLTHROUGH */
2071 case WI_RID_RTS_THRESH:
2072 case WI_RID_CNFAUTHMODE:
2073 case WI_RID_MAX_DATALEN:
2074 if (sc->sc_enabled) {
2075 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2076 sizeof(u_int16_t));
2077 if (error)
2078 break;
2079 }
2080 switch (wreq.wi_type) {
2081 case WI_RID_FRAG_THRESH:
2082 ic->ic_fragthreshold = le16toh(wreq.wi_val[0]);
984263bc 2083 break;
acffed4b
JS
2084 case WI_RID_RTS_THRESH:
2085 ic->ic_rtsthreshold = le16toh(wreq.wi_val[0]);
984263bc 2086 break;
acffed4b
JS
2087 case WI_RID_MICROWAVE_OVEN:
2088 sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
984263bc 2089 break;
acffed4b
JS
2090 case WI_RID_ROAMING_MODE:
2091 sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
984263bc 2092 break;
acffed4b
JS
2093 case WI_RID_SYSTEM_SCALE:
2094 sc->sc_system_scale = le16toh(wreq.wi_val[0]);
984263bc 2095 break;
acffed4b
JS
2096 case WI_RID_CNFAUTHMODE:
2097 sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
984263bc 2098 break;
acffed4b
JS
2099 case WI_RID_MAX_DATALEN:
2100 sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
984263bc 2101 break;
984263bc
MD
2102 }
2103 break;
acffed4b
JS
2104
2105 case WI_RID_TX_RATE:
2106 switch (le16toh(wreq.wi_val[0])) {
2107 case 3:
2108 ic->ic_fixed_rate = -1;
984263bc 2109 break;
acffed4b
JS
2110 default:
2111 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
2112 for (i = 0; i < rs->rs_nrates; i++) {
2113 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL)
2114 / 2 == le16toh(wreq.wi_val[0]))
2115 break;
984263bc 2116 }
acffed4b
JS
2117 if (i == rs->rs_nrates)
2118 return EINVAL;
2119 ic->ic_fixed_rate = i;
2120 }
2121 if (sc->sc_enabled)
2122 error = wi_write_txrate(sc);
2123 break;
2124
2125 case WI_RID_SCAN_APS:
2126 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2127 error = wi_scan_ap(sc, 0x3fff, 0x000f);
2128 break;
2129
2130 case WI_RID_SCAN_REQ: /* compatibility interface */
2131 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2132 error = wi_scan_ap(sc, wreq.wi_val[0], wreq.wi_val[1]);
2133 break;
2134
2135 case WI_RID_MGMT_XMIT:
2136 if (!sc->sc_enabled) {
2137 error = ENETDOWN;
984263bc 2138 break;
acffed4b
JS
2139 }
2140 if (ic->ic_mgtq.ifq_len > 5) {
2141 error = EAGAIN;
984263bc 2142 break;
acffed4b
JS
2143 }
2144 /* XXX wi_len looks in u_int8_t, not in u_int16_t */
2145 m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL);
2146 if (m == NULL) {
2147 error = ENOMEM;
984263bc 2148 break;
acffed4b
JS
2149 }
2150 IF_ENQUEUE(&ic->ic_mgtq, m);
2151 break;
2152
2153 case WI_RID_MIF:
2154 mif = wreq.wi_val[0];
2155 val = wreq.wi_val[1];
2156 error = wi_cmd(sc, WI_CMD_WRITEMIF, mif, val, 0);
2157 break;
2158
2159 case WI_RID_PROCFRAME: /* ignore for compatibility */
2160 break;
2161
2162 case WI_RID_OWN_SSID:
2163 if (le16toh(wreq.wi_val[0]) * 2 > len ||
2164 le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) {
2165 error = ENOSPC;
984263bc 2166 break;
acffed4b
JS
2167 }
2168 memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
2169 ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2;
2170 memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen);
2171 error = ENETRESET;
2172 break;
2173
2174 default:
2175 if (sc->sc_enabled) {
2176 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2177 len);
2178 if (error)
984263bc 2179 break;
acffed4b
JS
2180 }
2181 error = ieee80211_cfgset(ifp, cmd, data);
2182 break;
2183 }
2184 return error;
2185}
2186
2187static int
2188wi_write_txrate(struct wi_softc *sc)
2189{
2190 struct ieee80211com *ic = &sc->sc_ic;
2191 int i;
2192 u_int16_t rate;
2193
2194 if (ic->ic_fixed_rate < 0)
2195 rate = 0; /* auto */
2196 else
2197 rate = (ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[ic->ic_fixed_rate] &
2198 IEEE80211_RATE_VAL) / 2;
2199
2200 /* rate: 0, 1, 2, 5, 11 */
2201
2202 switch (sc->sc_firmware_type) {
2203 case WI_LUCENT:
2204 switch (rate) {
2205 case 0: /* auto == 11mbps auto */
2206 rate = 3;
984263bc 2207 break;
acffed4b
JS
2208 /* case 1, 2 map to 1, 2*/
2209 case 5: /* 5.5Mbps -> 4 */
2210 rate = 4;
984263bc 2211 break;
acffed4b
JS
2212 case 11: /* 11mbps -> 5 */
2213 rate = 5;
984263bc
MD
2214 break;
2215 default:
984263bc
MD
2216 break;
2217 }
984263bc
MD
2218 break;
2219 default:
acffed4b
JS
2220 /* Choose a bit according to this table.
2221 *
2222 * bit | data rate
2223 * ----+-------------------
2224 * 0 | 1Mbps
2225 * 1 | 2Mbps
2226 * 2 | 5.5Mbps
2227 * 3 | 11Mbps
2228 */
2229 for (i = 8; i > 0; i >>= 1) {
2230 if (rate >= i)
2231 break;
2232 }
2233 if (i == 0)
2234 rate = 0xf; /* auto */
2235 else
2236 rate = i;
984263bc
MD
2237 break;
2238 }
acffed4b 2239 return wi_write_val(sc, WI_RID_TX_RATE, rate);
984263bc
MD
2240}
2241
acffed4b
JS
2242static int
2243wi_write_wep(struct wi_softc *sc)
984263bc 2244{
acffed4b
JS
2245 struct ieee80211com *ic = &sc->sc_ic;
2246 int error = 0;
2247 int i, keylen;
2248 u_int16_t val;
2249 struct wi_key wkey[IEEE80211_WEP_NKID];
984263bc 2250
acffed4b
JS
2251 switch (sc->sc_firmware_type) {
2252 case WI_LUCENT:
2253 val = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0;
2254 error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
2255 if (error)
2256 break;
2257 error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_wep_txkey);
2258 if (error)
2259 break;
2260 memset(wkey, 0, sizeof(wkey));
2261 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2262 keylen = ic->ic_nw_keys[i].wk_len;
2263 wkey[i].wi_keylen = htole16(keylen);
2264 memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
2265 keylen);
2266 }
2267 error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
2268 wkey, sizeof(wkey));
2269 break;
984263bc 2270
acffed4b
JS
2271 case WI_INTERSIL:
2272 case WI_SYMBOL:
2273 if (ic->ic_flags & IEEE80211_F_WEPON) {
984263bc
MD
2274 /*
2275 * ONLY HWB3163 EVAL-CARD Firmware version
2276 * less than 0.8 variant2
2277 *
acffed4b
JS
2278 * If promiscuous mode disable, Prism2 chip
2279 * does not work with WEP .
984263bc
MD
2280 * It is under investigation for details.
2281 * (ichiro@netbsd.org)
984263bc 2282 */
acffed4b 2283 if (sc->sc_firmware_type == WI_INTERSIL &&
984263bc
MD
2284 sc->sc_sta_firmware_ver < 802 ) {
2285 /* firm ver < 0.8 variant 2 */
acffed4b 2286 wi_write_val(sc, WI_RID_PROMISC, 1);
984263bc 2287 }
acffed4b
JS
2288 wi_write_val(sc, WI_RID_CNFAUTHMODE,
2289 sc->sc_cnfauthmode);
2290 val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
2291 /*
2292 * Encryption firmware has a bug for HostAP mode.
2293 */
2294 if (sc->sc_firmware_type == WI_INTERSIL &&
2295 ic->ic_opmode == IEEE80211_M_HOSTAP)
2296 val |= HOST_ENCRYPT;
2297 } else {
2298 wi_write_val(sc, WI_RID_CNFAUTHMODE,
2299 IEEE80211_AUTH_OPEN);
2300 val = HOST_ENCRYPT | HOST_DECRYPT;
2301 }
2302 error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
2303 if (error)
2304 break;
2305 error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
2306 ic->ic_wep_txkey);
2307 if (error)
2308 break;
2309 /*
2310 * It seems that the firmware accept 104bit key only if
2311 * all the keys have 104bit length. We get the length of
2312 * the transmit key and use it for all other keys.
2313 * Perhaps we should use software WEP for such situation.
2314 */
2315 keylen = ic->ic_nw_keys[ic->ic_wep_txkey].wk_len;
2316 if (keylen > IEEE80211_WEP_KEYLEN)
2317 keylen = 13; /* 104bit keys */
2318 else
2319 keylen = IEEE80211_WEP_KEYLEN;
2320 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2321 error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
2322 ic->ic_nw_keys[i].wk_key, keylen);
2323 if (error)
2324 break;
984263bc 2325 }
acffed4b 2326 break;
984263bc 2327 }
acffed4b
JS
2328 return error;
2329}
984263bc 2330
acffed4b
JS
2331static int
2332wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
2333{
2334 int i, s = 0;
2335 static volatile int count = 0;
2336
2337 if (sc->wi_gone)
2338 return (ENODEV);
984263bc 2339
acffed4b
JS
2340 if (count > 0)
2341 panic("Hey partner, hold on there!");
2342 count++;
984263bc 2343
acffed4b
JS
2344 /* wait for the busy bit to clear */
2345 for (i = sc->wi_cmd_count; i > 0; i--) { /* 500ms */
2346 if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
2347 break;
2348 DELAY(1*1000); /* 1ms */
2349 }
2350 if (i == 0) {
4c30eb3b 2351 if_printf(&sc->sc_ic.ic_if, "wi_cmd: busy bit won't clear.\n" );
acffed4b
JS
2352 sc->wi_gone = 1;
2353 count--;
2354 return(ETIMEDOUT);
2355 }
984263bc 2356
acffed4b
JS
2357 CSR_WRITE_2(sc, WI_PARAM0, val0);
2358 CSR_WRITE_2(sc, WI_PARAM1, val1);
2359 CSR_WRITE_2(sc, WI_PARAM2, val2);
2360 CSR_WRITE_2(sc, WI_COMMAND, cmd);
984263bc 2361
acffed4b
JS
2362 if (cmd == WI_CMD_INI) {
2363 /* XXX: should sleep here. */
2364 DELAY(100*1000); /* 100ms delay for init */
2365 }
2366 for (i = 0; i < WI_TIMEOUT; i++) {
2367 /*
2368 * Wait for 'command complete' bit to be
2369 * set in the event status register.
2370 */
2371 s = CSR_READ_2(sc, WI_EVENT_STAT);
2372 if (s & WI_EV_CMD) {
2373 /* Ack the event and read result code. */
2374 s = CSR_READ_2(sc, WI_STATUS);
2375 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
2376 if (s & WI_STAT_CMD_RESULT) {
2377 count--;
2378 return(EIO);
2379 }
2380 break;
2381 }
2382 DELAY(WI_DELAY);
2383 }
984263bc 2384
acffed4b
JS
2385 count--;
2386 if (i == WI_TIMEOUT) {
4c30eb3b 2387 if_printf(&sc->sc_ic.ic_if,
acffed4b
JS
2388 "timeout in wi_cmd 0x%04x; event status 0x%04x\n", cmd, s);
2389 if (s == 0xffff)
2390 sc->wi_gone = 1;
2391 return(ETIMEDOUT);
2392 }
2393 return (0);
2394}
984263bc 2395
acffed4b
JS
2396static int
2397wi_seek_bap(struct wi_softc *sc, int id, int off)
2398{
2399 int i, status;
984263bc 2400
acffed4b
JS
2401 CSR_WRITE_2(sc, WI_SEL0, id);
2402 CSR_WRITE_2(sc, WI_OFF0, off);
984263bc 2403
acffed4b
JS
2404 for (i = 0; ; i++) {
2405 status = CSR_READ_2(sc, WI_OFF0);
2406 if ((status & WI_OFF_BUSY) == 0)
2407 break;
2408 if (i == WI_TIMEOUT) {
4c30eb3b
JS
2409 if_printf(&sc->sc_ic.ic_if,
2410 "timeout in wi_seek to %x/%x\n", id, off);
acffed4b
JS
2411 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2412 if (status == 0xffff)
2413 sc->wi_gone = 1;
2414 return ETIMEDOUT;
2415 }
2416 DELAY(1);
2417 }
2418 if (status & WI_OFF_ERR) {
4c30eb3b
JS
2419 if_printf(&sc->sc_ic.ic_if, "failed in wi_seek to %x/%x\n",
2420 id, off);
acffed4b
JS
2421 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2422 return EIO;
2423 }
2424 sc->sc_bap_id = id;
2425 sc->sc_bap_off = off;
2426 return 0;
984263bc
MD
2427}
2428
acffed4b
JS
2429static int
2430wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
984263bc 2431{
acffed4b
JS
2432 u_int16_t *ptr;
2433 int i, error, cnt;
2434
2435 if (buflen == 0)
2436 return 0;
2437 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2438 if ((error = wi_seek_bap(sc, id, off)) != 0)
2439 return error;
984263bc 2440 }
acffed4b
JS
2441 cnt = (buflen + 1) / 2;
2442 ptr = (u_int16_t *)buf;
2443 for (i = 0; i < cnt; i++)
2444 *ptr++ = CSR_READ_2(sc, WI_DATA0);
2445 sc->sc_bap_off += cnt * 2;
2446 return 0;
984263bc
MD
2447}
2448
acffed4b
JS
2449static int
2450wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
984263bc 2451{
acffed4b
JS
2452 u_int16_t *ptr;
2453 int i, error, cnt;
984263bc 2454
acffed4b
JS
2455 if (buflen == 0)
2456 return 0;
984263bc 2457
acffed4b
JS
2458#ifdef WI_HERMES_AUTOINC_WAR
2459 again:
2460#endif
2461 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2462 if ((error = wi_seek_bap(sc, id, off)) != 0)
2463 return error;
984263bc 2464 }
acffed4b
JS
2465 cnt = (buflen + 1) / 2;
2466 ptr = (u_int16_t *)buf;
2467 for (i = 0; i < cnt; i++)
2468 CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
2469 sc->sc_bap_off += cnt * 2;
984263bc 2470
acffed4b 2471#ifdef WI_HERMES_AUTOINC_WAR
984263bc 2472 /*
acffed4b
JS
2473 * According to the comments in the HCF Light code, there is a bug
2474 * in the Hermes (or possibly in certain Hermes firmware revisions)
2475 * where the chip's internal autoincrement counter gets thrown off
2476 * during data writes: the autoincrement is missed, causing one
2477 * data word to be overwritten and subsequent words to be written to
2478 * the wrong memory locations. The end result is that we could end
2479 * up transmitting bogus frames without realizing it. The workaround
2480 * for this is to write a couple of extra guard words after the end
2481 * of the transfer, then attempt to read then back. If we fail to
2482 * locate the guard words where we expect them, we preform the
2483 * transfer over again.
2484 */
2485 if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
2486 CSR_WRITE_2(sc, WI_DATA0, 0x1234);
2487 CSR_WRITE_2(sc, WI_DATA0, 0x5678);
2488 wi_seek_bap(sc, id, sc->sc_bap_off);
2489 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2490 if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
2491 CSR_READ_2(sc, WI_DATA0) != 0x5678) {
4c30eb3b
JS
2492 if_printf(&sc->sc_ic.ic_if,
2493 "detect auto increment bug, try again\n");
acffed4b 2494 goto again;
984263bc
MD
2495 }
2496 }
acffed4b
JS
2497#endif
2498 return 0;
2499}
984263bc 2500
acffed4b
JS
2501static int
2502wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
2503{
2504 int error, len;
2505 struct mbuf *m;
984263bc 2506
acffed4b
JS
2507 for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
2508 if (m->m_len == 0)
2509 continue;
984263bc 2510
acffed4b 2511 len = min(m->m_len, totlen);
984263bc 2512
acffed4b
JS
2513 if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
2514 m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
2515 return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
2516 totlen);
2517 }
984263bc 2518
acffed4b
JS
2519 if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
2520 return error;
984263bc 2521
acffed4b
JS
2522 off += m->m_len;
2523 totlen -= len;
2524 }
2525 return 0;
984263bc
MD
2526}
2527
acffed4b
JS
2528static int
2529wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
984263bc 2530{
acffed4b 2531 int i;
984263bc 2532
acffed4b 2533 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
4c30eb3b
JS
2534 if_printf(&sc->sc_ic.ic_if,
2535 "failed to allocate %d bytes on NIC\n", len);
acffed4b
JS
2536 return ENOMEM;
2537 }
2538
2539 for (i = 0; i < WI_TIMEOUT; i++) {
2540 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
2541 break;
2542 if (i == WI_TIMEOUT) {
4c30eb3b 2543 if_printf(&sc->sc_ic.ic_if, "timeout in alloc\n");
acffed4b
JS
2544 return ETIMEDOUT;
2545 }
2546 DELAY(1);
2547 }
2548 *idp = CSR_READ_2(sc, WI_ALLOC_FID);
2549 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
2550 return 0;
2551}
984263bc 2552
acffed4b
JS
2553static int
2554wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
2555{
2556 int error, len;
2557 u_int16_t ltbuf[2];
984263bc 2558
acffed4b
JS
2559 /* Tell the NIC to enter record read mode. */
2560 error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2561 if (error)
2562 return error;
984263bc 2563
acffed4b
JS
2564 error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2565 if (error)
2566 return error;
2567
2568 if (le16toh(ltbuf[1]) != rid) {
4c30eb3b
JS
2569 if_printf(&sc->sc_ic.ic_if,
2570 "record read mismatch, rid=%x, got=%x\n",
2571 rid, le16toh(ltbuf[1]));
acffed4b
JS
2572 return EIO;
2573 }
2574 len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */
2575 if (*buflenp < len) {
4c30eb3b
JS
2576 if_printf(&sc->sc_ic.ic_if, "record buffer is too small, "
2577 "rid=%x, size=%d, len=%d\n", rid, *buflenp, len);
acffed4b
JS
2578 return ENOSPC;
2579 }
2580 *buflenp = len;
2581 return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2582}
984263bc 2583
acffed4b
JS
2584static int
2585wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2586{
2587 int error;
2588 u_int16_t ltbuf[2];
984263bc 2589
acffed4b
JS
2590 ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */
2591 ltbuf[1] = htole16(rid);
984263bc 2592
acffed4b
JS
2593 error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2594 if (error)
2595 return error;
2596 error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2597 if (error)
2598 return error;
984263bc 2599
acffed4b 2600 return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
984263bc
MD
2601}
2602
acffed4b
JS
2603static int
2604wi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
984263bc 2605{
acffed4b
JS
2606 struct ifnet *ifp = &ic->ic_if;
2607 struct wi_softc *sc = ifp->if_softc;
2608 struct ieee80211_node *ni = ic->ic_bss;
2609 int buflen;
2610 u_int16_t val;
2611 struct wi_ssid ssid;
2612 u_int8_t old_bssid[IEEE80211_ADDR_LEN];
2613
4c30eb3b 2614 DPRINTF((ifp, "%s: %s -> %s\n", __func__,
acffed4b
JS
2615 ieee80211_state_name[ic->ic_state],
2616 ieee80211_state_name[nstate]));
2617
2618 switch (nstate) {
2619 case IEEE80211_S_INIT:
2620 ic->ic_flags &= ~IEEE80211_F_SIBSS;
2621 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2622 return (*sc->sc_newstate)(ic, nstate, arg);
2623
2624 case IEEE80211_S_RUN:
2625 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
2626 buflen = IEEE80211_ADDR_LEN;
2627 wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen);
2628 IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
2629 buflen = sizeof(val);
2630 wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
2631 /* XXX validate channel */
2632 ni->ni_chan = &ic->ic_channels[le16toh(val)];
acffed4b
JS
2633 sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
2634 htole16(ni->ni_chan->ic_freq);
2635 sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
2636 htole16(ni->ni_chan->ic_flags);
984263bc 2637
acffed4b
JS
2638 if (IEEE80211_ADDR_EQ(old_bssid, ni->ni_bssid))
2639 sc->sc_false_syns++;
2640 else
2641 sc->sc_false_syns = 0;
2642
2643 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2644 ni->ni_esslen = ic->ic_des_esslen;
2645 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
2646 ni->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B];
2647 ni->ni_intval = ic->ic_lintval;
2648 ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
2649 if (ic->ic_flags & IEEE80211_F_WEPON)
2650 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
2651 } else {
2652 /* XXX check return value */
2653 buflen = sizeof(ssid);
2654 wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
2655 ni->ni_esslen = le16toh(ssid.wi_len);
2656 if (ni->ni_esslen > IEEE80211_NWID_LEN)
2657 ni->ni_esslen = IEEE80211_NWID_LEN; /*XXX*/
2658 memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
2659 }
2660 break;
984263bc 2661
acffed4b
JS
2662 case IEEE80211_S_SCAN:
2663 case IEEE80211_S_AUTH:
2664 case IEEE80211_S_ASSOC:
2665 break;
984263bc
MD
2666 }
2667
acffed4b
JS
2668 ic->ic_state = nstate; /* NB: skip normal ieee80211 handling */
2669 return 0;
2670}
984263bc 2671
acffed4b
JS
2672static int
2673wi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate)
2674{
2675 int error = 0;
2676 u_int16_t val[2];
984263bc 2677
acffed4b
JS
2678 if (!sc->sc_enabled)
2679 return ENXIO;
2680 switch (sc->sc_firmware_type) {
2681 case WI_LUCENT:
2682 (void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
2683 break;
2684 case WI_INTERSIL:
2685 val[0] = chanmask; /* channel */
2686 val[1] = txrate; /* tx rate */
2687 error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
2688 break;
2689 case WI_SYMBOL:
2690 /*
2691 * XXX only supported on 3.x ?
2692 */
2693 val[0] = BSCAN_BCAST | BSCAN_ONETIME;
2694 error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
2695 val, sizeof(val[0]));
2696 break;
984263bc 2697 }
acffed4b
JS
2698 if (error == 0) {
2699 sc->sc_scan_timer = WI_SCAN_WAIT;
2700 sc->sc_ic.ic_if.if_timer = 1;
4c30eb3b 2701 DPRINTF((&sc->sc_ic.ic_if, "wi_scan_ap: start scanning, "
acffed4b
JS
2702 "chamask 0x%x txrate 0x%x\n", chanmask, txrate));
2703 }
2704 return error;
984263bc
MD
2705}
2706
2707static void
acffed4b 2708wi_scan_result(struct wi_softc *sc, int fid, int cnt)
984263bc 2709{
acffed4b
JS
2710#define N(a) (sizeof (a) / sizeof (a[0]))
2711 int i, naps, off, szbuf;
2712 struct wi_scan_header ws_hdr; /* Prism2 header */
2713 struct wi_scan_data_p2 ws_dat; /* Prism2 scantable*/
2714 struct wi_apinfo *ap;
2715
2716 off = sizeof(u_int16_t) * 2;
2717 memset(&ws_hdr, 0, sizeof(ws_hdr));
2718 switch (sc->sc_firmware_type) {
2719 case WI_INTERSIL:
2720 wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
2721 off += sizeof(ws_hdr);
2722 szbuf = sizeof(struct wi_scan_data_p2);
2723 break;
2724 case WI_SYMBOL:
2725 szbuf = sizeof(struct wi_scan_data_p2) + 6;
2726 break;
2727 case WI_LUCENT:
2728 szbuf = sizeof(struct wi_scan_data);
2729 break;
2730 default:
4c30eb3b
JS
2731 if_printf(&sc->sc_ic.ic_if,
2732 "wi_scan_result: unknown firmware type %u\n",
2733 sc->sc_firmware_type);
acffed4b
JS
2734 naps = 0;
2735 goto done;
2736 }
2737 naps = (cnt * 2 + 2 - off) / szbuf;
2738 if (naps > N(sc->sc_aps))
2739 naps = N(sc->sc_aps);
2740 sc->sc_naps = naps;
2741 /* Read Data */
2742 ap = sc->sc_aps;
2743 memset(&ws_dat, 0, sizeof(ws_dat));
2744 for (i = 0; i < naps; i++, ap++) {
2745 wi_read_bap(sc, fid, off, &ws_dat,
2746 (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
4c30eb3b
JS
2747 DPRINTF2((&sc->sc_ic.ic_if,
2748 "wi_scan_result: #%d: off %d bssid %6D\n",
2749 i, off, ws_dat.wi_bssid, ":"));
acffed4b
JS
2750 off += szbuf;
2751 ap->scanreason = le16toh(ws_hdr.wi_reason);
2752 memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
2753 ap->channel = le16toh(ws_dat.wi_chid);
2754 ap->signal = le16toh(ws_dat.wi_signal);
2755 ap->noise = le16toh(ws_dat.wi_noise);
2756 ap->quality = ap->signal - ap->noise;
2757 ap->capinfo = le16toh(ws_dat.wi_capinfo);
2758 ap->interval = le16toh(ws_dat.wi_interval);
2759 ap->rate = le16toh(ws_dat.wi_rate);
2760 ap->namelen = le16toh(ws_dat.wi_namelen);
2761 if (ap->namelen > sizeof(ap->name))
2762 ap->namelen = sizeof(ap->name);
2763 memcpy(ap->name, ws_dat.wi_name, ap->namelen);
2764 }
2765done:
2766 /* Done scanning */
2767 sc->sc_scan_timer = 0;
4c30eb3b
JS
2768 DPRINTF((&sc->sc_ic.ic_if, "wi_scan_result: scan complete: ap %d\n",
2769 naps));
acffed4b
JS
2770#undef N
2771}
984263bc 2772
acffed4b
JS
2773static void
2774wi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi)
2775{
2776 ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr),
2777 ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL : -1, rssi);
2778 printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n",
2779 le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1),
2780 le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence);
2781 printf(" rx_signal %u rx_rate %u rx_flow %u\n",
2782 wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow);
2783 printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n",
2784 wh->wi_tx_rtry, wh->wi_tx_rate,
2785 le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len));
2786 printf(" ehdr dst %6D src %6D type 0x%x\n",
2787 wh->wi_ehdr.ether_dhost, ":", wh->wi_ehdr.ether_shost, ":",
2788 wh->wi_ehdr.ether_type);
984263bc
MD
2789}
2790
2791int
acffed4b 2792wi_alloc(device_t dev, int rid)
984263bc 2793{
acffed4b 2794 struct wi_softc *sc = device_get_softc(dev);
984263bc
MD
2795
2796 if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2797 sc->iobase_rid = rid;
2798 sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2799 &sc->iobase_rid, 0, ~0, (1 << 6),
2800 rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2801 if (!sc->iobase) {
2802 device_printf(dev, "No I/O space?!\n");
2803 return (ENXIO);
2804 }
2805
2806 sc->wi_io_addr = rman_get_start(sc->iobase);
2807 sc->wi_btag = rman_get_bustag(sc->iobase);
2808 sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2809 } else {
2810 sc->mem_rid = rid;
acffed4b
JS
2811 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2812 &sc->mem_rid, RF_ACTIVE);
984263bc
MD
2813
2814 if (!sc->mem) {
2815 device_printf(dev, "No Mem space on prism2.5?\n");
2816 return (ENXIO);
2817 }
2818
2819 sc->wi_btag = rman_get_bustag(sc->mem);
2820 sc->wi_bhandle = rman_get_bushandle(sc->mem);
2821 }
2822
2823
2824 sc->irq_rid = 0;
acffed4b
JS
2825 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2826 RF_ACTIVE |
984263bc
MD
2827 ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2828
2829 if (!sc->irq) {
2830 wi_free(dev);
2831 device_printf(dev, "No irq?!\n");
2832 return (ENXIO);
2833 }
2834
984263bc
MD
2835 return (0);
2836}
2837
2838void
acffed4b 2839wi_free(device_t dev)
984263bc 2840{
acffed4b 2841 struct wi_softc *sc = device_get_softc(dev);
984263bc 2842
0d71884b
JS
2843 if (sc->wi_intrhand != NULL) {
2844 bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
2845 sc->wi_intrhand = NULL;
2846 }
984263bc
MD
2847 if (sc->iobase != NULL) {
2848 bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2849 sc->iobase = NULL;
2850 }
2851 if (sc->irq != NULL) {
2852 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2853 sc->irq = NULL;
2854 }
2855 if (sc->mem != NULL) {
2856 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
2857 sc->mem = NULL;
2858 }
984263bc
MD
2859}
2860
984263bc 2861static int
acffed4b 2862wi_get_debug(struct wi_softc *sc, struct wi_req *wreq)
984263bc 2863{
acffed4b 2864 int error = 0;
984263bc
MD
2865
2866 wreq->wi_len = 1;
2867
2868 switch (wreq->wi_type) {
2869 case WI_DEBUG_SLEEP:
2870 wreq->wi_len++;
2871 wreq->wi_val[0] = sc->wi_debug.wi_sleep;
2872 break;
2873 case WI_DEBUG_DELAYSUPP:
2874 wreq->wi_len++;
2875 wreq->wi_val[0] = sc->wi_debug.wi_delaysupp;
2876 break;
2877 case WI_DEBUG_TXSUPP:
2878 wreq->wi_len++;
2879 wreq->wi_val[0] = sc->wi_debug.wi_txsupp;
2880 break;
2881 case WI_DEBUG_MONITOR:
2882 wreq->wi_len++;
2883 wreq->wi_val[0] = sc->wi_debug.wi_monitor;
2884 break;
2885 case WI_DEBUG_LEDTEST:
2886 wreq->wi_len += 3;
2887 wreq->wi_val[0] = sc->wi_debug.wi_ledtest;
2888 wreq->wi_val[1] = sc->wi_debug.wi_ledtest_param0;
2889 wreq->wi_val[2] = sc->wi_debug.wi_ledtest_param1;
2890 break;
2891 case WI_DEBUG_CONTTX:
2892 wreq->wi_len += 2;
2893 wreq->wi_val[0] = sc->wi_debug.wi_conttx;
2894 wreq->wi_val[1] = sc->wi_debug.wi_conttx_param0;
2895 break;
2896 case WI_DEBUG_CONTRX:
2897 wreq->wi_len++;
2898 wreq->wi_val[0] = sc->wi_debug.wi_contrx;
2899 break;
2900 case WI_DEBUG_SIGSTATE:
2901 wreq->wi_len += 2;
2902 wreq->wi_val[0] = sc->wi_debug.wi_sigstate;
2903 wreq->wi_val[1] = sc->wi_debug.wi_sigstate_param0;
2904 break;
2905 case WI_DEBUG_CONFBITS:
2906 wreq->wi_len += 2;
2907 wreq->wi_val[0] = sc->wi_debug.wi_confbits;
2908 wreq->wi_val[1] = sc->wi_debug.wi_confbits_param0;
2909 break;
2910 default:
2911 error = EIO;
2912 break;
2913 }
2914
2915 return (error);
2916}
2917
2918static int
acffed4b 2919wi_set_debug(struct wi_softc *sc, struct wi_req *wreq)
984263bc 2920{
acffed4b 2921 int error = 0;
984263bc
MD
2922 u_int16_t cmd, param0 = 0, param1 = 0;
2923
2924 switch (wreq->wi_type) {
2925 case WI_DEBUG_RESET:
2926 case WI_DEBUG_INIT:
2927 case WI_DEBUG_CALENABLE:
2928 break;
2929 case WI_DEBUG_SLEEP:
2930 sc->wi_debug.wi_sleep = 1;
2931 break;
2932 case WI_DEBUG_WAKE:
2933 sc->wi_debug.wi_sleep = 0;
2934 break;
2935 case WI_DEBUG_CHAN:
2936 param0 = wreq->wi_val[0];
2937 break;
2938 case WI_DEBUG_DELAYSUPP:
2939 sc->wi_debug.wi_delaysupp = 1;
2940 break;
2941 case WI_DEBUG_TXSUPP:
2942 sc->wi_debug.wi_txsupp = 1;
2943 break;
2944 case WI_DEBUG_MONITOR:
2945 sc->wi_debug.wi_monitor = 1;
2946 break;
2947 case WI_DEBUG_LEDTEST:
2948 param0 = wreq->wi_val[0];
2949 param1 = wreq->wi_val[1];
2950 sc->wi_debug.wi_ledtest = 1;
2951 sc->wi_debug.wi_ledtest_param0 = param0;
2952 sc->wi_debug.wi_ledtest_param1 = param1;
2953 break;
2954 case WI_DEBUG_CONTTX:
2955 param0 = wreq->wi_val[0];
2956 sc->wi_debug.wi_conttx = 1;
2957 sc->wi_debug.wi_conttx_param0 = param0;
2958 break;
2959 case WI_DEBUG_STOPTEST:
2960 sc->wi_debug.wi_delaysupp = 0;
2961 sc->wi_debug.wi_txsupp = 0;
2962 sc->wi_debug.wi_monitor = 0;
2963 sc->wi_debug.wi_ledtest = 0;
2964 sc->wi_debug.wi_ledtest_param0 = 0;
2965 sc->wi_debug.wi_ledtest_param1 = 0;
2966 sc->wi_debug.wi_conttx = 0;
2967 sc->wi_debug.wi_conttx_param0 = 0;
2968 sc->wi_debug.wi_contrx = 0;
2969 sc->wi_debug.wi_sigstate = 0;
2970 sc->wi_debug.wi_sigstate_param0 = 0;
2971 break;
2972 case WI_DEBUG_CONTRX:
2973 sc->wi_debug.wi_contrx = 1;
2974 break;
2975 case WI_DEBUG_SIGSTATE:
2976 param0 = wreq->wi_val[0];
2977 sc->wi_debug.wi_sigstate = 1;
2978 sc->wi_debug.wi_sigstate_param0 = param0;
2979 break;
2980 case WI_DEBUG_CONFBITS:
2981 param0 = wreq->wi_val[0];
2982 param1 = wreq->wi_val[1];
2983 sc->wi_debug.wi_confbits = param0;
2984 sc->wi_debug.wi_confbits_param0 = param1;
2985 break;
2986 default:
2987 error = EIO;
2988 break;
2989 }
2990
2991 if (error)
2992 return (error);
2993
2994 cmd = WI_CMD_DEBUG | (wreq->wi_type << 8);
2995 error = wi_cmd(sc, cmd, param0, param1, 0);
2996
2997 return (error);
2998}
acffed4b
JS
2999
3000/*
3001 * Special routines to download firmware for Symbol CF card.
3002 * XXX: This should be modified generic into any PRISM-2 based card.
3003 */
3004
3005#define WI_SBCF_PDIADDR 0x3100
3006
3007/* unaligned load little endian */
3008#define GETLE32(p) ((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24))
3009#define GETLE16(p) ((p)[0] | ((p)[1]<<8))
3010
3011int
3012wi_symbol_load_firm(struct wi_softc *sc, const void *primsym, int primlen,
3013 const void *secsym, int seclen)
3014{
3015 uint8_t ebuf[256];
3016 int i;
3017
3018 /* load primary code and run it */
3019 wi_symbol_set_hcr(sc, WI_HCR_EEHOLD);
3020 if (wi_symbol_write_firm(sc, primsym, primlen, NULL, 0))
3021 return EIO;
3022 wi_symbol_set_hcr(sc, WI_HCR_RUN);
3023 for (i = 0; ; i++) {
3024 if (i == 10)
3025 return ETIMEDOUT;
3026 tsleep(sc, 0, "wiinit", 1);
3027 if (CSR_READ_2(sc, WI_CNTL) == WI_CNTL_AUX_ENA_STAT)
3028 break;
3029 /* write the magic key value to unlock aux port */
3030 CSR_WRITE_2(sc, WI_PARAM0, WI_AUX_KEY0);
3031 CSR_WRITE_2(sc, WI_PARAM1, WI_AUX_KEY1);
3032 CSR_WRITE_2(sc, WI_PARAM2, WI_AUX_KEY2);
3033 CSR_WRITE_2(sc, WI_CNTL, WI_CNTL_AUX_ENA_CNTL);
3034 }
3035
3036 /* issue read EEPROM command: XXX copied from wi_cmd() */
3037 CSR_WRITE_2(sc, WI_PARAM0, 0);
3038 CSR_WRITE_2(sc, WI_PARAM1, 0);
3039 CSR_WRITE_2(sc, WI_PARAM2, 0);
3040 CSR_WRITE_2(sc, WI_COMMAND, WI_CMD_READEE);
3041 for (i = 0; i < WI_TIMEOUT; i++) {
3042 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
3043 break;
3044 DELAY(1);
3045 }
3046 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
3047
3048 CSR_WRITE_2(sc, WI_AUX_PAGE, WI_SBCF_PDIADDR / WI_AUX_PGSZ);
3049 CSR_WRITE_2(sc, WI_AUX_OFFSET, WI_SBCF_PDIADDR % WI_AUX_PGSZ);
3050 CSR_READ_MULTI_STREAM_2(sc, WI_AUX_DATA,
3051 (uint16_t *)ebuf, sizeof(ebuf) / 2);
3052 if (GETLE16(ebuf) > sizeof(ebuf))
3053 return EIO;
3054 if (wi_symbol_write_firm(sc, secsym, seclen, ebuf + 4, GETLE16(ebuf)))
3055 return EIO;
3056 return 0;
3057}
3058
3059static int
3060wi_symbol_write_firm(struct wi_softc *sc, const void *buf, int buflen,
3061 const void *ebuf, int ebuflen)
3062{
3063 const uint8_t *p, *ep, *q, *eq;
3064 char *tp;
3065 uint32_t addr, id, eid;
3066 int i, len, elen, nblk, pdrlen;
3067
3068 /*
3069 * Parse the header of the firmware image.
3070 */
3071 p = buf;
3072 ep = p + buflen;
3073 while (p < ep && *p++ != ' '); /* FILE: */
3074 while (p < ep && *p++ != ' '); /* filename */
3075 while (p < ep && *p++ != ' '); /* type of the firmware */
3076 nblk = strtoul(p, &tp, 10);
3077 p = tp;
3078 pdrlen = strtoul(p + 1, &tp, 10);
3079 p = tp;
3080 while (p < ep && *p++ != 0x1a); /* skip rest of header */
3081
3082 /*
3083 * Block records: address[4], length[2], data[length];
3084 */
3085 for (i = 0; i < nblk; i++) {
3086 addr = GETLE32(p); p += 4;
3087 len = GETLE16(p); p += 2;
3088 CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3089 CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3090 CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3091 (const uint16_t *)p, len / 2);
3092 p += len;
3093 }
3094
3095 /*
3096 * PDR: id[4], address[4], length[4];
3097 */
3098 for (i = 0; i < pdrlen; ) {
3099 id = GETLE32(p); p += 4; i += 4;
3100 addr = GETLE32(p); p += 4; i += 4;
3101 len = GETLE32(p); p += 4; i += 4;
3102 /* replace PDR entry with the values from EEPROM, if any */
3103 for (q = ebuf, eq = q + ebuflen; q < eq; q += elen * 2) {
3104 elen = GETLE16(q); q += 2;
3105 eid = GETLE16(q); q += 2;
3106 elen--; /* elen includes eid */
3107 if (eid == 0)
3108 break;
3109 if (eid != id)
3110 continue;
3111 CSR_WRITE_2(sc, WI_AUX_PAGE, addr / WI_AUX_PGSZ);
3112 CSR_WRITE_2(sc, WI_AUX_OFFSET, addr % WI_AUX_PGSZ);
3113 CSR_WRITE_MULTI_STREAM_2(sc, WI_AUX_DATA,
3114 (const uint16_t *)q, len / 2);
3115 break;
3116 }
3117 }
3118 return 0;
3119}
3120
3121static int
3122wi_symbol_set_hcr(struct wi_softc *sc, int mode)
3123{
3124 uint16_t hcr;
3125
3126 CSR_WRITE_2(sc, WI_COR, WI_COR_RESET);
3127 tsleep(sc, 0, "wiinit", 1);
3128 hcr = CSR_READ_2(sc, WI_HCR);
3129 hcr = (hcr & WI_HCR_4WIRE) | (mode & ~WI_HCR_4WIRE);
3130 CSR_WRITE_2(sc, WI_HCR, hcr);
3131 tsleep(sc, 0, "wiinit", 1);
3132 CSR_WRITE_2(sc, WI_COR, WI_COR_IOMODE);
3133 tsleep(sc, 0, "wiinit", 1);
3134 return 0;
3135}