Add const for argument of argmatch to fix warnings.
[dragonfly.git] / sys / dev / netif / wi / if_wi.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1997, 1998, 1999
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
1de703da
MD
31 *
32 * $FreeBSD: src/sys/dev/wi/if_wi.c,v 1.103.2.2 2002/08/02 07:11:34 imp Exp $
3ee50d77 33 * $DragonFly: src/sys/dev/netif/wi/if_wi.c,v 1.15 2004/07/27 14:30:10 joerg Exp $
984263bc
MD
34 */
35
36/*
37 * Lucent WaveLAN/IEEE 802.11 PCMCIA driver for FreeBSD.
38 *
39 * Written by Bill Paul <wpaul@ctr.columbia.edu>
40 * Electrical Engineering Department
41 * Columbia University, New York City
42 */
43
44/*
45 * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
46 * from Lucent. Unlike the older cards, the new ones are programmed
47 * entirely via a firmware-driven controller called the Hermes.
48 * Unfortunately, Lucent will not release the Hermes programming manual
49 * without an NDA (if at all). What they do release is an API library
50 * called the HCF (Hardware Control Functions) which is supposed to
51 * do the device-specific operations of a device driver for you. The
52 * publically available version of the HCF library (the 'HCF Light') is
53 * a) extremely gross, b) lacks certain features, particularly support
54 * for 802.11 frames, and c) is contaminated by the GNU Public License.
55 *
56 * This driver does not use the HCF or HCF Light at all. Instead, it
57 * programs the Hermes controller directly, using information gleaned
58 * from the HCF Light code and corresponding documentation.
59 *
60 * This driver supports the ISA, PCMCIA and PCI versions of the Lucent
61 * WaveLan cards (based on the Hermes chipset), as well as the newer
62 * Prism 2 chipsets with firmware from Intersil and Symbol.
63 */
64
65#include <sys/param.h>
66#include <sys/systm.h>
929783d0 67#if defined(__FreeBSD__) && __FreeBSD_version >= 500033
984263bc
MD
68#include <sys/endian.h>
69#endif
70#include <sys/sockio.h>
71#include <sys/mbuf.h>
72#include <sys/proc.h>
73#include <sys/kernel.h>
74#include <sys/socket.h>
75#include <sys/module.h>
76#include <sys/bus.h>
77#include <sys/random.h>
78#include <sys/syslog.h>
79#include <sys/sysctl.h>
80
81#include <machine/bus.h>
82#include <machine/resource.h>
83#include <machine/clock.h>
84#include <sys/rman.h>
85
86#include <net/if.h>
87#include <net/if_arp.h>
88#include <net/ethernet.h>
89#include <net/if_dl.h>
90#include <net/if_media.h>
91#include <net/if_types.h>
3ee50d77
JS
92#include <netproto/802_11/ieee80211.h>
93#include <netproto/802_11/ieee80211_ioctl.h>
94#include <netproto/802_11/if_wavelan_ieee.h>
984263bc
MD
95
96#include <netinet/in.h>
97#include <netinet/in_systm.h>
98#include <netinet/in_var.h>
99#include <netinet/ip.h>
100#include <netinet/if_ether.h>
101
102#include <net/bpf.h>
103
1f2de5d4
MD
104#include "wi_hostap.h"
105#include "if_wivar.h"
106#include "if_wireg.h"
984263bc 107
3ee50d77
JS
108#define WI_CMD_DEBUG 0x0038 /* prism2 debug */
109
984263bc
MD
110static void wi_intr(void *);
111static void wi_reset(struct wi_softc *);
bd4539cc 112static int wi_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
984263bc
MD
113static void wi_init(void *);
114static void wi_start(struct ifnet *);
115static void wi_stop(struct wi_softc *);
116static void wi_watchdog(struct ifnet *);
117static void wi_rxeof(struct wi_softc *);
118static void wi_txeof(struct wi_softc *, int);
119static void wi_update_stats(struct wi_softc *);
120static void wi_setmulti(struct wi_softc *);
121
122static int wi_cmd(struct wi_softc *, int, int, int, int);
123static int wi_read_record(struct wi_softc *, struct wi_ltv_gen *);
124static int wi_write_record(struct wi_softc *, struct wi_ltv_gen *);
125static int wi_read_data(struct wi_softc *, int, int, caddr_t, int);
126static int wi_write_data(struct wi_softc *, int, int, caddr_t, int);
127static int wi_seek(struct wi_softc *, int, int, int);
128static int wi_alloc_nicmem(struct wi_softc *, int, int *);
129static void wi_inquire(void *);
130static void wi_setdef(struct wi_softc *, struct wi_req *);
131
132#ifdef WICACHE
133static
3013ac0e 134void wi_cache_store(struct wi_softc *, struct mbuf *, unsigned short);
984263bc
MD
135#endif
136
137static int wi_get_cur_ssid(struct wi_softc *, char *, int *);
138static void wi_get_id(struct wi_softc *);
139static int wi_media_change(struct ifnet *);
140static void wi_media_status(struct ifnet *, struct ifmediareq *);
141
142static int wi_get_debug(struct wi_softc *, struct wi_req *);
143static int wi_set_debug(struct wi_softc *, struct wi_req *);
144
32832096
MD
145DECLARE_DUMMY_MODULE(if_wi);
146
984263bc
MD
147devclass_t wi_devclass;
148
149struct wi_card_ident wi_card_ident[] = {
150 /* CARD_ID CARD_NAME FIRM_TYPE */
151 { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT },
152 { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT },
153 { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT },
154 { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL },
155 { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL },
156 { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL },
157 { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL },
158 { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL },
159 { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL },
160 { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL },
161 { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL },
162 { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL },
163 { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
164 { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
165 { WI_NIC_3842_PCMCIA_ATM_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
166 { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
167 { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
168 { WI_NIC_3842_MINI_ATM_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
169 { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
170 { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
171 { WI_NIC_3842_PCI_ATM_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
172 { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
173 { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
174 { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
175 { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
176 { 0, NULL, 0 },
177};
178
179int
180wi_generic_detach(dev)
181 device_t dev;
182{
183 struct wi_softc *sc;
184 struct ifnet *ifp;
185 int s;
186
187 sc = device_get_softc(dev);
188 WI_LOCK(sc, s);
189 ifp = &sc->arpcom.ac_if;
190
191 if (sc->wi_gone) {
192 device_printf(dev, "already unloaded\n");
193 WI_UNLOCK(sc, s);
194 return(ENODEV);
195 }
196
197 wi_stop(sc);
198
199 /* Delete all remaining media. */
200 ifmedia_removeall(&sc->ifmedia);
201
0a8b5977 202 ether_ifdetach(ifp);
984263bc
MD
203 bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
204 wi_free(dev);
205 sc->wi_gone = 1;
206
207 WI_UNLOCK(sc, s);
929783d0 208#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
984263bc
MD
209 mtx_destroy(&sc->wi_mtx);
210#endif
211
212 return(0);
213}
214
215int
216wi_generic_attach(device_t dev)
217{
218 struct wi_softc *sc;
219 struct wi_ltv_macaddr mac;
220 struct wi_ltv_gen gen;
221 struct ifnet *ifp;
222 int error;
223 int s;
224
225 /* XXX maybe we need the splimp stuff here XXX */
226 sc = device_get_softc(dev);
227 ifp = &sc->arpcom.ac_if;
228
229 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
230 wi_intr, sc, &sc->wi_intrhand);
231
232 if (error) {
233 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
234 wi_free(dev);
235 return (error);
236 }
237
929783d0 238#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
984263bc
MD
239 mtx_init(&sc->wi_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
240 MTX_DEF | MTX_RECURSE);
241#endif
242 WI_LOCK(sc, s);
243
244 /* Reset the NIC. */
245 wi_reset(sc);
246
247 /*
248 * Read the station address.
249 * And do it twice. I've seen PRISM-based cards that return
250 * an error when trying to read it the first time, which causes
251 * the probe to fail.
252 */
253 mac.wi_type = WI_RID_MAC_NODE;
254 mac.wi_len = 4;
255 wi_read_record(sc, (struct wi_ltv_gen *)&mac);
256 if ((error = wi_read_record(sc, (struct wi_ltv_gen *)&mac)) != 0) {
257 device_printf(dev, "mac read failed %d\n", error);
258 wi_free(dev);
55b3ee0d 259 WI_UNLOCK(sc, s);
984263bc
MD
260 return (error);
261 }
262 bcopy((char *)&mac.wi_mac_addr,
263 (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
264
984263bc
MD
265 wi_get_id(sc);
266
267 ifp->if_softc = sc;
cdb7d804 268 if_initname(ifp, "wi", sc->wi_unit);
984263bc
MD
269 ifp->if_mtu = ETHERMTU;
270 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
271 ifp->if_ioctl = wi_ioctl;
984263bc
MD
272 ifp->if_start = wi_start;
273 ifp->if_watchdog = wi_watchdog;
274 ifp->if_init = wi_init;
275 ifp->if_baudrate = 10000000;
276 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
277
278 bzero(sc->wi_node_name, sizeof(sc->wi_node_name));
279 bcopy(WI_DEFAULT_NODENAME, sc->wi_node_name,
280 sizeof(WI_DEFAULT_NODENAME) - 1);
281
282 bzero(sc->wi_net_name, sizeof(sc->wi_net_name));
283 bcopy(WI_DEFAULT_NETNAME, sc->wi_net_name,
284 sizeof(WI_DEFAULT_NETNAME) - 1);
285
286 bzero(sc->wi_ibss_name, sizeof(sc->wi_ibss_name));
287 bcopy(WI_DEFAULT_IBSS, sc->wi_ibss_name,
288 sizeof(WI_DEFAULT_IBSS) - 1);
289
290 sc->wi_portnum = WI_DEFAULT_PORT;
291 sc->wi_ptype = WI_PORTTYPE_BSS;
292 sc->wi_ap_density = WI_DEFAULT_AP_DENSITY;
293 sc->wi_rts_thresh = WI_DEFAULT_RTS_THRESH;
294 sc->wi_tx_rate = WI_DEFAULT_TX_RATE;
295 sc->wi_max_data_len = WI_DEFAULT_DATALEN;
296 sc->wi_create_ibss = WI_DEFAULT_CREATE_IBSS;
297 sc->wi_pm_enabled = WI_DEFAULT_PM_ENABLED;
298 sc->wi_max_sleep = WI_DEFAULT_MAX_SLEEP;
299 sc->wi_roaming = WI_DEFAULT_ROAMING;
300 sc->wi_authtype = WI_DEFAULT_AUTHTYPE;
301 sc->wi_authmode = IEEE80211_AUTH_OPEN;
302
303 /*
304 * Read the default channel from the NIC. This may vary
305 * depending on the country where the NIC was purchased, so
306 * we can't hard-code a default and expect it to work for
307 * everyone.
308 */
309 gen.wi_type = WI_RID_OWN_CHNL;
310 gen.wi_len = 2;
311 wi_read_record(sc, &gen);
312 sc->wi_channel = gen.wi_val;
313
314 /*
315 * Set flags based on firmware version.
316 */
317 switch (sc->sc_firmware_type) {
318 case WI_LUCENT:
319 sc->wi_flags |= WI_FLAGS_HAS_ROAMING;
320 if (sc->sc_sta_firmware_ver >= 60000)
321 sc->wi_flags |= WI_FLAGS_HAS_MOR;
322 if (sc->sc_sta_firmware_ver >= 60006) {
323 sc->wi_flags |= WI_FLAGS_HAS_IBSS;
324 sc->wi_flags |= WI_FLAGS_HAS_CREATE_IBSS;
325 }
326 sc->wi_ibss_port = htole16(1);
327 break;
328 case WI_INTERSIL:
329 sc->wi_flags |= WI_FLAGS_HAS_ROAMING;
330 if (sc->sc_sta_firmware_ver >= 800) {
331 sc->wi_flags |= WI_FLAGS_HAS_IBSS;
332 sc->wi_flags |= WI_FLAGS_HAS_CREATE_IBSS;
333 }
334 /*
335 * version 0.8.3 and newer are the only ones that are known
336 * to currently work. Earlier versions can be made to work,
337 * at least according to the Linux driver.
338 */
339 if (sc->sc_sta_firmware_ver >= 803)
340 sc->wi_flags |= WI_FLAGS_HAS_HOSTAP;
341 sc->wi_ibss_port = htole16(0);
342 break;
343 case WI_SYMBOL:
344 sc->wi_flags |= WI_FLAGS_HAS_DIVERSITY;
345 if (sc->sc_sta_firmware_ver >= 20000)
346 sc->wi_flags |= WI_FLAGS_HAS_IBSS;
347 /* Older Symbol firmware does not support IBSS creation. */
348 if (sc->sc_sta_firmware_ver >= 25000)
349 sc->wi_flags |= WI_FLAGS_HAS_CREATE_IBSS;
350 sc->wi_ibss_port = htole16(4);
351 break;
352 }
353
354 /*
355 * Find out if we support WEP on this card.
356 */
357 gen.wi_type = WI_RID_WEP_AVAIL;
358 gen.wi_len = 2;
359 wi_read_record(sc, &gen);
360 sc->wi_has_wep = gen.wi_val;
361
362 if (bootverbose)
363 device_printf(sc->dev, "wi_has_wep = %d\n", sc->wi_has_wep);
364
365 /*
366 * Find supported rates.
367 */
368 gen.wi_type = WI_RID_DATA_RATES;
369 gen.wi_len = 2;
370 if (wi_read_record(sc, &gen))
371 sc->wi_supprates = WI_SUPPRATES_1M | WI_SUPPRATES_2M |
372 WI_SUPPRATES_5M | WI_SUPPRATES_11M;
373 else
374 sc->wi_supprates = gen.wi_val;
375
376 bzero((char *)&sc->wi_stats, sizeof(sc->wi_stats));
377
378 wi_init(sc);
379 wi_stop(sc);
380
381 ifmedia_init(&sc->ifmedia, 0, wi_media_change, wi_media_status);
382#define ADD(m, c) ifmedia_add(&sc->ifmedia, (m), (c), NULL)
383 if (sc->wi_supprates & WI_SUPPRATES_1M) {
384 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS1, 0, 0), 0);
385 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS1,
386 IFM_IEEE80211_ADHOC, 0), 0);
387 if (sc->wi_flags & WI_FLAGS_HAS_IBSS)
388 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS1,
389 IFM_IEEE80211_IBSS, 0), 0);
390 if (sc->wi_flags & WI_FLAGS_HAS_CREATE_IBSS)
391 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS1,
392 IFM_IEEE80211_IBSSMASTER, 0), 0);
393 if (sc->wi_flags & WI_FLAGS_HAS_HOSTAP)
394 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS1,
395 IFM_IEEE80211_HOSTAP, 0), 0);
396 }
397 if (sc->wi_supprates & WI_SUPPRATES_2M) {
398 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS2, 0, 0), 0);
399 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS2,
400 IFM_IEEE80211_ADHOC, 0), 0);
401 if (sc->wi_flags & WI_FLAGS_HAS_IBSS)
402 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS2,
403 IFM_IEEE80211_IBSS, 0), 0);
404 if (sc->wi_flags & WI_FLAGS_HAS_CREATE_IBSS)
405 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS2,
406 IFM_IEEE80211_IBSSMASTER, 0), 0);
407 if (sc->wi_flags & WI_FLAGS_HAS_HOSTAP)
408 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS2,
409 IFM_IEEE80211_HOSTAP, 0), 0);
410 }
411 if (sc->wi_supprates & WI_SUPPRATES_5M) {
412 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS5, 0, 0), 0);
413 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS5,
414 IFM_IEEE80211_ADHOC, 0), 0);
415 if (sc->wi_flags & WI_FLAGS_HAS_IBSS)
416 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS5,
417 IFM_IEEE80211_IBSS, 0), 0);
418 if (sc->wi_flags & WI_FLAGS_HAS_CREATE_IBSS)
419 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS5,
420 IFM_IEEE80211_IBSSMASTER, 0), 0);
421 if (sc->wi_flags & WI_FLAGS_HAS_HOSTAP)
422 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS5,
423 IFM_IEEE80211_HOSTAP, 0), 0);
424 }
425 if (sc->wi_supprates & WI_SUPPRATES_11M) {
426 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS11, 0, 0), 0);
427 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS11,
428 IFM_IEEE80211_ADHOC, 0), 0);
429 if (sc->wi_flags & WI_FLAGS_HAS_IBSS)
430 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS11,
431 IFM_IEEE80211_IBSS, 0), 0);
432 if (sc->wi_flags & WI_FLAGS_HAS_CREATE_IBSS)
433 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS11,
434 IFM_IEEE80211_IBSSMASTER, 0), 0);
435 if (sc->wi_flags & WI_FLAGS_HAS_HOSTAP)
436 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS11,
437 IFM_IEEE80211_HOSTAP, 0), 0);
438 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_MANUAL, 0, 0), 0);
439 }
440 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO, IFM_IEEE80211_ADHOC, 0), 0);
441 if (sc->wi_flags & WI_FLAGS_HAS_IBSS)
442 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO, IFM_IEEE80211_IBSS,
443 0), 0);
444 if (sc->wi_flags & WI_FLAGS_HAS_CREATE_IBSS)
445 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO,
446 IFM_IEEE80211_IBSSMASTER, 0), 0);
447 if (sc->wi_flags & WI_FLAGS_HAS_HOSTAP)
448 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO,
449 IFM_IEEE80211_HOSTAP, 0), 0);
450 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO, 0, 0), 0);
451#undef ADD
452 ifmedia_set(&sc->ifmedia, IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO, 0, 0));
453
454 /*
455 * Call MI attach routine.
456 */
0a8b5977 457 ether_ifattach(ifp, sc->arpcom.ac_enaddr);
984263bc
MD
458 callout_handle_init(&sc->wi_stat_ch);
459 WI_UNLOCK(sc, s);
460
461 return(0);
462}
463
464static void
465wi_get_id(sc)
466 struct wi_softc *sc;
467{
468 struct wi_ltv_ver ver;
469 struct wi_card_ident *id;
470
471 /* getting chip identity */
472 memset(&ver, 0, sizeof(ver));
473 ver.wi_type = WI_RID_CARD_ID;
474 ver.wi_len = 5;
475 wi_read_record(sc, (struct wi_ltv_gen *)&ver);
476 device_printf(sc->dev, "using ");
477 sc->sc_firmware_type = WI_NOTYPE;
478 for (id = wi_card_ident; id->card_name != NULL; id++) {
479 if (le16toh(ver.wi_ver[0]) == id->card_id) {
480 printf("%s", id->card_name);
481 sc->sc_firmware_type = id->firm_type;
482 break;
483 }
484 }
485 if (sc->sc_firmware_type == WI_NOTYPE) {
486 if (le16toh(ver.wi_ver[0]) & 0x8000) {
487 printf("Unknown PRISM2 chip");
488 sc->sc_firmware_type = WI_INTERSIL;
489 } else {
490 printf("Unknown Lucent chip");
491 sc->sc_firmware_type = WI_LUCENT;
492 }
493 }
494
495 if (sc->sc_firmware_type != WI_LUCENT) {
496 /* get primary firmware version */
497 memset(&ver, 0, sizeof(ver));
498 ver.wi_type = WI_RID_PRI_IDENTITY;
499 ver.wi_len = 5;
500 wi_read_record(sc, (struct wi_ltv_gen *)&ver);
501 ver.wi_ver[1] = le16toh(ver.wi_ver[1]);
502 ver.wi_ver[2] = le16toh(ver.wi_ver[2]);
503 ver.wi_ver[3] = le16toh(ver.wi_ver[3]);
504 sc->sc_pri_firmware_ver = ver.wi_ver[2] * 10000 +
505 ver.wi_ver[3] * 100 + ver.wi_ver[1];
506 }
507
508 /* get station firmware version */
509 memset(&ver, 0, sizeof(ver));
510 ver.wi_type = WI_RID_STA_IDENTITY;
511 ver.wi_len = 5;
512 wi_read_record(sc, (struct wi_ltv_gen *)&ver);
513 ver.wi_ver[1] = le16toh(ver.wi_ver[1]);
514 ver.wi_ver[2] = le16toh(ver.wi_ver[2]);
515 ver.wi_ver[3] = le16toh(ver.wi_ver[3]);
516 sc->sc_sta_firmware_ver = ver.wi_ver[2] * 10000 +
517 ver.wi_ver[3] * 100 + ver.wi_ver[1];
518 if (sc->sc_firmware_type == WI_INTERSIL &&
519 (sc->sc_sta_firmware_ver == 10102 ||
520 sc->sc_sta_firmware_ver == 20102)) {
521 struct wi_ltv_str sver;
522 char *p;
523
524 memset(&sver, 0, sizeof(sver));
525 sver.wi_type = WI_RID_SYMBOL_IDENTITY;
526 sver.wi_len = 7;
527 /* value should be the format like "V2.00-11" */
528 if (wi_read_record(sc, (struct wi_ltv_gen *)&sver) == 0 &&
529 *(p = (char *)sver.wi_str) >= 'A' &&
530 p[2] == '.' && p[5] == '-' && p[8] == '\0') {
531 sc->sc_firmware_type = WI_SYMBOL;
532 sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
533 (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
534 (p[6] - '0') * 10 + (p[7] - '0');
535 }
536 }
537 printf("\n");
538 device_printf(sc->dev, "%s Firmware: ",
539 sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
540 (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
541
542 /*
543 * The primary firmware is only valid on Prism based chipsets
544 * (INTERSIL or SYMBOL).
545 */
546 if (sc->sc_firmware_type != WI_LUCENT)
547 printf("Primary %u.%02u.%02u, ", sc->sc_pri_firmware_ver / 10000,
548 (sc->sc_pri_firmware_ver % 10000) / 100,
549 sc->sc_pri_firmware_ver % 100);
550 printf("Station %u.%02u.%02u\n",
551 sc->sc_sta_firmware_ver / 10000, (sc->sc_sta_firmware_ver % 10000) / 100,
552 sc->sc_sta_firmware_ver % 100);
553 return;
554}
555
556static void
557wi_rxeof(sc)
558 struct wi_softc *sc;
559{
560 struct ifnet *ifp;
561 struct ether_header *eh;
562 struct mbuf *m;
563 int id;
564
565 ifp = &sc->arpcom.ac_if;
566
567 id = CSR_READ_2(sc, WI_RX_FID);
568
569 /*
570 * if we have the procframe flag set, disregard all this and just
571 * read the data from the device.
572 */
573 if (sc->wi_procframe || sc->wi_debug.wi_monitor) {
574 struct wi_frame *rx_frame;
575 int datlen, hdrlen;
576
577 /* first allocate mbuf for packet storage */
74f1caca 578 MGETHDR(m, MB_DONTWAIT, MT_DATA);
984263bc
MD
579 if (m == NULL) {
580 ifp->if_ierrors++;
581 return;
582 }
74f1caca 583 MCLGET(m, MB_DONTWAIT);
984263bc
MD
584 if (!(m->m_flags & M_EXT)) {
585 m_freem(m);
586 ifp->if_ierrors++;
587 return;
588 }
589
590 m->m_pkthdr.rcvif = ifp;
591
592 /* now read wi_frame first so we know how much data to read */
593 if (wi_read_data(sc, id, 0, mtod(m, caddr_t),
594 sizeof(struct wi_frame))) {
595 m_freem(m);
596 ifp->if_ierrors++;
597 return;
598 }
599
600 rx_frame = mtod(m, struct wi_frame *);
601
602 switch ((rx_frame->wi_status & WI_STAT_MAC_PORT) >> 8) {
603 case 7:
604 switch (rx_frame->wi_frame_ctl & WI_FCTL_FTYPE) {
605 case WI_FTYPE_DATA:
606 hdrlen = WI_DATA_HDRLEN;
607 datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
608 break;
609 case WI_FTYPE_MGMT:
610 hdrlen = WI_MGMT_HDRLEN;
611 datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
612 break;
613 case WI_FTYPE_CTL:
614 /*
615 * prism2 cards don't pass control packets
616 * down properly or consistently, so we'll only
617 * pass down the header.
618 */
619 hdrlen = WI_CTL_HDRLEN;
620 datlen = 0;
621 break;
622 default:
623 device_printf(sc->dev, "received packet of "
624 "unknown type on port 7\n");
625 m_freem(m);
626 ifp->if_ierrors++;
627 return;
628 }
629 break;
630 case 0:
631 hdrlen = WI_DATA_HDRLEN;
632 datlen = rx_frame->wi_dat_len + WI_FCS_LEN;
633 break;
634 default:
635 device_printf(sc->dev, "received packet on invalid "
636 "port (wi_status=0x%x)\n", rx_frame->wi_status);
637 m_freem(m);
638 ifp->if_ierrors++;
639 return;
640 }
641
642 if ((hdrlen + datlen + 2) > MCLBYTES) {
643 device_printf(sc->dev, "oversized packet received "
644 "(wi_dat_len=%d, wi_status=0x%x)\n",
645 datlen, rx_frame->wi_status);
646 m_freem(m);
647 ifp->if_ierrors++;
648 return;
649 }
650
651 if (wi_read_data(sc, id, hdrlen, mtod(m, caddr_t) + hdrlen,
652 datlen + 2)) {
653 m_freem(m);
654 ifp->if_ierrors++;
655 return;
656 }
657
658 m->m_pkthdr.len = m->m_len = hdrlen + datlen;
659
660 ifp->if_ipackets++;
661
662 /* Handle BPF listeners. */
663 if (ifp->if_bpf)
664 bpf_mtap(ifp, m);
665
666 m_freem(m);
667 } else {
668 struct wi_frame rx_frame;
669
670 /* First read in the frame header */
671 if (wi_read_data(sc, id, 0, (caddr_t)&rx_frame,
672 sizeof(rx_frame))) {
673 ifp->if_ierrors++;
674 return;
675 }
676
677 if (rx_frame.wi_status & WI_STAT_ERRSTAT) {
678 ifp->if_ierrors++;
679 return;
680 }
681
74f1caca 682 MGETHDR(m, MB_DONTWAIT, MT_DATA);
984263bc
MD
683 if (m == NULL) {
684 ifp->if_ierrors++;
685 return;
686 }
74f1caca 687 MCLGET(m, MB_DONTWAIT);
984263bc
MD
688 if (!(m->m_flags & M_EXT)) {
689 m_freem(m);
690 ifp->if_ierrors++;
691 return;
692 }
693
694 eh = mtod(m, struct ether_header *);
695 m->m_pkthdr.rcvif = ifp;
696
697 if (rx_frame.wi_status == WI_STAT_MGMT &&
698 sc->wi_ptype == WI_PORTTYPE_AP) {
699 if ((WI_802_11_OFFSET_RAW + rx_frame.wi_dat_len + 2) >
700 MCLBYTES) {
701 device_printf(sc->dev, "oversized mgmt packet "
702 "received in hostap mode "
703 "(wi_dat_len=%d, wi_status=0x%x)\n",
704 rx_frame.wi_dat_len, rx_frame.wi_status);
705 m_freem(m);
706 ifp->if_ierrors++;
707 return;
708 }
709
710 /* Put the whole header in there. */
711 bcopy(&rx_frame, mtod(m, void *),
712 sizeof(struct wi_frame));
713 if (wi_read_data(sc, id, WI_802_11_OFFSET_RAW,
714 mtod(m, caddr_t) + WI_802_11_OFFSET_RAW,
715 rx_frame.wi_dat_len + 2)) {
716 m_freem(m);
717 ifp->if_ierrors++;
718 return;
719 }
720 m->m_pkthdr.len = m->m_len =
721 WI_802_11_OFFSET_RAW + rx_frame.wi_dat_len;
722 /* XXX: consider giving packet to bhp? */
723 wihap_mgmt_input(sc, &rx_frame, m);
724 return;
725 }
726
727 if (rx_frame.wi_status == WI_STAT_1042 ||
728 rx_frame.wi_status == WI_STAT_TUNNEL ||
729 rx_frame.wi_status == WI_STAT_WMP_MSG) {
730 if((rx_frame.wi_dat_len + WI_SNAPHDR_LEN) > MCLBYTES) {
731 device_printf(sc->dev,
732 "oversized packet received "
733 "(wi_dat_len=%d, wi_status=0x%x)\n",
734 rx_frame.wi_dat_len, rx_frame.wi_status);
735 m_freem(m);
736 ifp->if_ierrors++;
737 return;
738 }
739 m->m_pkthdr.len = m->m_len =
740 rx_frame.wi_dat_len + WI_SNAPHDR_LEN;
741
742#if 0
743 bcopy((char *)&rx_frame.wi_addr1,
744 (char *)&eh->ether_dhost, ETHER_ADDR_LEN);
745 if (sc->wi_ptype == WI_PORTTYPE_ADHOC) {
746 bcopy((char *)&rx_frame.wi_addr2,
747 (char *)&eh->ether_shost, ETHER_ADDR_LEN);
748 } else {
749 bcopy((char *)&rx_frame.wi_addr3,
750 (char *)&eh->ether_shost, ETHER_ADDR_LEN);
751 }
752#else
753 bcopy((char *)&rx_frame.wi_dst_addr,
754 (char *)&eh->ether_dhost, ETHER_ADDR_LEN);
755 bcopy((char *)&rx_frame.wi_src_addr,
756 (char *)&eh->ether_shost, ETHER_ADDR_LEN);
757#endif
758
759 bcopy((char *)&rx_frame.wi_type,
760 (char *)&eh->ether_type, ETHER_TYPE_LEN);
761
762 if (wi_read_data(sc, id, WI_802_11_OFFSET,
763 mtod(m, caddr_t) + sizeof(struct ether_header),
764 m->m_len + 2)) {
765 m_freem(m);
766 ifp->if_ierrors++;
767 return;
768 }
769 } else {
770 if((rx_frame.wi_dat_len +
771 sizeof(struct ether_header)) > MCLBYTES) {
772 device_printf(sc->dev,
773 "oversized packet received "
774 "(wi_dat_len=%d, wi_status=0x%x)\n",
775 rx_frame.wi_dat_len, rx_frame.wi_status);
776 m_freem(m);
777 ifp->if_ierrors++;
778 return;
779 }
780 m->m_pkthdr.len = m->m_len =
781 rx_frame.wi_dat_len + sizeof(struct ether_header);
782
783 if (wi_read_data(sc, id, WI_802_3_OFFSET,
784 mtod(m, caddr_t), m->m_len + 2)) {
785 m_freem(m);
786 ifp->if_ierrors++;
787 return;
788 }
789 }
790
791 ifp->if_ipackets++;
792
793 if (sc->wi_ptype == WI_PORTTYPE_AP) {
794 /*
795 * Give host AP code first crack at data
796 * packets. If it decides to handle it (or
797 * drop it), it will return a non-zero.
798 * Otherwise, it is destined for this host.
799 */
800 if (wihap_data_input(sc, &rx_frame, m))
801 return;
802 }
803 /* Receive packet. */
984263bc 804#ifdef WICACHE
3013ac0e 805 wi_cache_store(sc, m, rx_frame.wi_q_info);
984263bc 806#endif
3013ac0e 807 (*ifp->if_input)(ifp, m);
984263bc
MD
808 }
809}
810
811static void
812wi_txeof(sc, status)
813 struct wi_softc *sc;
814 int status;
815{
816 struct ifnet *ifp;
817
818 ifp = &sc->arpcom.ac_if;
819
820 ifp->if_timer = 0;
821 ifp->if_flags &= ~IFF_OACTIVE;
822
823 if (status & WI_EV_TX_EXC)
824 ifp->if_oerrors++;
825 else
826 ifp->if_opackets++;
827
828 return;
829}
830
831void
832wi_inquire(xsc)
833 void *xsc;
834{
835 struct wi_softc *sc;
836 struct ifnet *ifp;
837 int s;
838
839 sc = xsc;
840 ifp = &sc->arpcom.ac_if;
841
842 sc->wi_stat_ch = timeout(wi_inquire, sc, hz * 60);
843
844 /* Don't do this while we're transmitting */
845 if (ifp->if_flags & IFF_OACTIVE)
846 return;
847
848 WI_LOCK(sc, s);
849 wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_COUNTERS, 0, 0);
850 WI_UNLOCK(sc, s);
851
852 return;
853}
854
855void
856wi_update_stats(sc)
857 struct wi_softc *sc;
858{
859 struct wi_ltv_gen gen;
860 u_int16_t id;
861 struct ifnet *ifp;
862 u_int32_t *ptr;
863 int len, i;
864 u_int16_t t;
865
866 ifp = &sc->arpcom.ac_if;
867
868 id = CSR_READ_2(sc, WI_INFO_FID);
869
870 wi_read_data(sc, id, 0, (char *)&gen, 4);
871
872 /*
873 * if we just got our scan results, copy it over into the scan buffer
874 * so we can return it to anyone that asks for it. (add a little
875 * compatibility with the prism2 scanning mechanism)
876 */
877 if (gen.wi_type == WI_INFO_SCAN_RESULTS)
878 {
879 sc->wi_scanbuf_len = gen.wi_len;
880 wi_read_data(sc, id, 4, (char *)sc->wi_scanbuf,
881 sc->wi_scanbuf_len * 2);
882
883 return;
884 }
885 else if (gen.wi_type != WI_INFO_COUNTERS)
886 return;
887
888 len = (gen.wi_len - 1 < sizeof(sc->wi_stats) / 4) ?
889 gen.wi_len - 1 : sizeof(sc->wi_stats) / 4;
890 ptr = (u_int32_t *)&sc->wi_stats;
891
892 for (i = 0; i < len - 1; i++) {
893 t = CSR_READ_2(sc, WI_DATA1);
894#ifdef WI_HERMES_STATS_WAR
895 if (t > 0xF000)
896 t = ~t & 0xFFFF;
897#endif
898 ptr[i] += t;
899 }
900
901 ifp->if_collisions = sc->wi_stats.wi_tx_single_retries +
902 sc->wi_stats.wi_tx_multi_retries +
903 sc->wi_stats.wi_tx_retry_limit;
904
905 return;
906}
907
908static void
909wi_intr(xsc)
910 void *xsc;
911{
912 struct wi_softc *sc = xsc;
913 struct ifnet *ifp;
914 u_int16_t status;
915 int s;
916
917 WI_LOCK(sc, s);
918
919 ifp = &sc->arpcom.ac_if;
920
921 if (sc->wi_gone || !(ifp->if_flags & IFF_UP)) {
922 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
923 CSR_WRITE_2(sc, WI_INT_EN, 0);
924 WI_UNLOCK(sc, s);
925 return;
926 }
927
928 /* Disable interrupts. */
929 CSR_WRITE_2(sc, WI_INT_EN, 0);
930
931 status = CSR_READ_2(sc, WI_EVENT_STAT);
932 CSR_WRITE_2(sc, WI_EVENT_ACK, ~WI_INTRS);
933
934 if (status & WI_EV_RX) {
935 wi_rxeof(sc);
936 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
937 }
938
939 if (status & WI_EV_TX) {
940 wi_txeof(sc, status);
941 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX);
942 }
943
944 if (status & WI_EV_ALLOC) {
945 int id;
946
947 id = CSR_READ_2(sc, WI_ALLOC_FID);
948 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
949 if (id == sc->wi_tx_data_id)
950 wi_txeof(sc, status);
951 }
952
953 if (status & WI_EV_INFO) {
954 wi_update_stats(sc);
955 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
956 }
957
958 if (status & WI_EV_TX_EXC) {
959 wi_txeof(sc, status);
960 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
961 }
962
963 if (status & WI_EV_INFO_DROP) {
964 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO_DROP);
965 }
966
967 /* Re-enable interrupts. */
968 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
969
970 if (ifp->if_snd.ifq_head != NULL) {
971 wi_start(ifp);
972 }
973
974 WI_UNLOCK(sc, s);
975
976 return;
977}
978
979static int
980wi_cmd(sc, cmd, val0, val1, val2)
981 struct wi_softc *sc;
982 int cmd;
983 int val0;
984 int val1;
985 int val2;
986{
987 int i, s = 0;
988 static volatile int count = 0;
989
990 if (count > 1)
991 panic("Hey partner, hold on there!");
992 count++;
993
994 /* wait for the busy bit to clear */
995 for (i = 500; i > 0; i--) { /* 5s */
996 if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY)) {
997 break;
998 }
999 DELAY(10*1000); /* 10 m sec */
1000 }
1001 if (i == 0) {
1002 device_printf(sc->dev, "wi_cmd: busy bit won't clear.\n" );
1003 count--;
1004 return(ETIMEDOUT);
1005 }
1006
1007 CSR_WRITE_2(sc, WI_PARAM0, val0);
1008 CSR_WRITE_2(sc, WI_PARAM1, val1);
1009 CSR_WRITE_2(sc, WI_PARAM2, val2);
1010 CSR_WRITE_2(sc, WI_COMMAND, cmd);
1011
1012 for (i = 0; i < WI_TIMEOUT; i++) {
1013 /*
1014 * Wait for 'command complete' bit to be
1015 * set in the event status register.
1016 */
1017 s = CSR_READ_2(sc, WI_EVENT_STAT);
1018 if (s & WI_EV_CMD) {
1019 /* Ack the event and read result code. */
1020 s = CSR_READ_2(sc, WI_STATUS);
1021 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
1022#ifdef foo
1023 if ((s & WI_CMD_CODE_MASK) != (cmd & WI_CMD_CODE_MASK))
1024 return(EIO);
1025#endif
1026 if (s & WI_STAT_CMD_RESULT) {
1027 count--;
1028 return(EIO);
1029 }
1030 break;
1031 }
1032 DELAY(WI_DELAY);
1033 }
1034
1035 count--;
1036 if (i == WI_TIMEOUT) {
1037 device_printf(sc->dev,
1038 "timeout in wi_cmd 0x%04x; event status 0x%04x\n", cmd, s);
1039 return(ETIMEDOUT);
1040 }
1041 return(0);
1042}
1043
1044static void
1045wi_reset(sc)
1046 struct wi_softc *sc;
1047{
1048#define WI_INIT_TRIES 3
1049 int i;
1050 int tries;
1051
1052 /* Symbol firmware cannot be initialized more than once */
1053 if (sc->sc_firmware_type == WI_SYMBOL && sc->sc_enabled)
1054 return;
1055 if (sc->sc_firmware_type == WI_SYMBOL)
1056 tries = 1;
1057 else
1058 tries = WI_INIT_TRIES;
1059
1060 for (i = 0; i < tries; i++) {
1061 if (wi_cmd(sc, WI_CMD_INI, 0, 0, 0) == 0)
1062 break;
1063 DELAY(WI_DELAY * 1000);
1064 }
1065 sc->sc_enabled = 1;
1066
1067 if (i == tries) {
1068 device_printf(sc->dev, "init failed\n");
1069 return;
1070 }
1071
1072 CSR_WRITE_2(sc, WI_INT_EN, 0);
1073 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
1074
1075 /* Calibrate timer. */
1076 WI_SETVAL(WI_RID_TICK_TIME, 8);
1077
1078 return;
1079}
1080
1081/*
1082 * Read an LTV record from the NIC.
1083 */
1084static int
1085wi_read_record(sc, ltv)
1086 struct wi_softc *sc;
1087 struct wi_ltv_gen *ltv;
1088{
1089 u_int16_t *ptr;
1090 int i, len, code;
1091 struct wi_ltv_gen *oltv, p2ltv;
1092
1093 oltv = ltv;
1094 if (sc->sc_firmware_type != WI_LUCENT) {
1095 switch (ltv->wi_type) {
1096 case WI_RID_ENCRYPTION:
1097 p2ltv.wi_type = WI_RID_P2_ENCRYPTION;
1098 p2ltv.wi_len = 2;
1099 ltv = &p2ltv;
1100 break;
1101 case WI_RID_TX_CRYPT_KEY:
1102 p2ltv.wi_type = WI_RID_P2_TX_CRYPT_KEY;
1103 p2ltv.wi_len = 2;
1104 ltv = &p2ltv;
1105 break;
1106 case WI_RID_ROAMING_MODE:
1107 if (sc->sc_firmware_type == WI_INTERSIL)
1108 break;
1109 /* not supported */
1110 ltv->wi_len = 1;
1111 return 0;
1112 case WI_RID_MICROWAVE_OVEN:
1113 /* not supported */
1114 ltv->wi_len = 1;
1115 return 0;
1116 }
1117 }
1118
1119 /* Tell the NIC to enter record read mode. */
1120 if (wi_cmd(sc, WI_CMD_ACCESS|WI_ACCESS_READ, ltv->wi_type, 0, 0))
1121 return(EIO);
1122
1123 /* Seek to the record. */
1124 if (wi_seek(sc, ltv->wi_type, 0, WI_BAP1))
1125 return(EIO);
1126
1127 /*
1128 * Read the length and record type and make sure they
1129 * match what we expect (this verifies that we have enough
1130 * room to hold all of the returned data).
1131 */
1132 len = CSR_READ_2(sc, WI_DATA1);
1133 if (len > ltv->wi_len)
1134 return(ENOSPC);
1135 code = CSR_READ_2(sc, WI_DATA1);
1136 if (code != ltv->wi_type)
1137 return(EIO);
1138
1139 ltv->wi_len = len;
1140 ltv->wi_type = code;
1141
1142 /* Now read the data. */
1143 ptr = &ltv->wi_val;
1144 for (i = 0; i < ltv->wi_len - 1; i++)
1145 ptr[i] = CSR_READ_2(sc, WI_DATA1);
1146
1147 if (ltv->wi_type == WI_RID_PORTTYPE && sc->wi_ptype == WI_PORTTYPE_IBSS
1148 && ltv->wi_val == sc->wi_ibss_port) {
1149 /*
1150 * Convert vendor IBSS port type to WI_PORTTYPE_IBSS.
1151 * Since Lucent uses port type 1 for BSS *and* IBSS we
1152 * have to rely on wi_ptype to distinguish this for us.
1153 */
1154 ltv->wi_val = htole16(WI_PORTTYPE_IBSS);
1155 } else if (sc->sc_firmware_type != WI_LUCENT) {
1156 switch (oltv->wi_type) {
1157 case WI_RID_TX_RATE:
1158 case WI_RID_CUR_TX_RATE:
1159 switch (ltv->wi_val) {
1160 case 1: oltv->wi_val = 1; break;
1161 case 2: oltv->wi_val = 2; break;
1162 case 3: oltv->wi_val = 6; break;
1163 case 4: oltv->wi_val = 5; break;
1164 case 7: oltv->wi_val = 7; break;
1165 case 8: oltv->wi_val = 11; break;
1166 case 15: oltv->wi_val = 3; break;
1167 default: oltv->wi_val = 0x100 + ltv->wi_val; break;
1168 }
1169 break;
1170 case WI_RID_ENCRYPTION:
1171 oltv->wi_len = 2;
1172 if (ltv->wi_val & 0x01)
1173 oltv->wi_val = 1;
1174 else
1175 oltv->wi_val = 0;
1176 break;
1177 case WI_RID_TX_CRYPT_KEY:
1178 oltv->wi_len = 2;
1179 oltv->wi_val = ltv->wi_val;
1180 break;
1181 case WI_RID_CNFAUTHMODE:
1182 oltv->wi_len = 2;
1183 if (le16toh(ltv->wi_val) & 0x01)
1184 oltv->wi_val = htole16(1);
1185 else if (le16toh(ltv->wi_val) & 0x02)
1186 oltv->wi_val = htole16(2);
1187 break;
1188 }
1189 }
1190
1191 return(0);
1192}
1193
1194/*
1195 * Same as read, except we inject data instead of reading it.
1196 */
1197static int
1198wi_write_record(sc, ltv)
1199 struct wi_softc *sc;
1200 struct wi_ltv_gen *ltv;
1201{
1202 u_int16_t *ptr;
1203 int i;
1204 struct wi_ltv_gen p2ltv;
1205
1206 if (ltv->wi_type == WI_RID_PORTTYPE &&
1207 le16toh(ltv->wi_val) == WI_PORTTYPE_IBSS) {
1208 /* Convert WI_PORTTYPE_IBSS to vendor IBSS port type. */
1209 p2ltv.wi_type = WI_RID_PORTTYPE;
1210 p2ltv.wi_len = 2;
1211 p2ltv.wi_val = sc->wi_ibss_port;
1212 ltv = &p2ltv;
1213 } else if (sc->sc_firmware_type != WI_LUCENT) {
1214 switch (ltv->wi_type) {
1215 case WI_RID_TX_RATE:
1216 p2ltv.wi_type = WI_RID_TX_RATE;
1217 p2ltv.wi_len = 2;
1218 switch (ltv->wi_val) {
1219 case 1: p2ltv.wi_val = 1; break;
1220 case 2: p2ltv.wi_val = 2; break;
1221 case 3: p2ltv.wi_val = 15; break;
1222 case 5: p2ltv.wi_val = 4; break;
1223 case 6: p2ltv.wi_val = 3; break;
1224 case 7: p2ltv.wi_val = 7; break;
1225 case 11: p2ltv.wi_val = 8; break;
1226 default: return EINVAL;
1227 }
1228 ltv = &p2ltv;
1229 break;
1230 case WI_RID_ENCRYPTION:
1231 p2ltv.wi_type = WI_RID_P2_ENCRYPTION;
1232 p2ltv.wi_len = 2;
1233 if (le16toh(ltv->wi_val)) {
1234 p2ltv.wi_val =htole16(PRIVACY_INVOKED |
1235 EXCLUDE_UNENCRYPTED);
1236 if (sc->wi_ptype == WI_PORTTYPE_AP)
1237 /*
1238 * Disable tx encryption...
1239 * it's broken.
1240 */
1241 p2ltv.wi_val |= htole16(HOST_ENCRYPT);
1242 } else
1243 p2ltv.wi_val =
1244 htole16(HOST_ENCRYPT | HOST_DECRYPT);
1245 ltv = &p2ltv;
1246 break;
1247 case WI_RID_TX_CRYPT_KEY:
1248 p2ltv.wi_type = WI_RID_P2_TX_CRYPT_KEY;
1249 p2ltv.wi_len = 2;
1250 p2ltv.wi_val = ltv->wi_val;
1251 ltv = &p2ltv;
1252 break;
1253 case WI_RID_DEFLT_CRYPT_KEYS:
1254 {
1255 int error;
1256 int keylen;
1257 struct wi_ltv_str ws;
1258 struct wi_ltv_keys *wk =
1259 (struct wi_ltv_keys *)ltv;
1260
1261 keylen = wk->wi_keys[sc->wi_tx_key].wi_keylen;
1262
1263 for (i = 0; i < 4; i++) {
1264 bzero(&ws, sizeof(ws));
1265 ws.wi_len = (keylen > 5) ? 8 : 4;
1266 ws.wi_type = WI_RID_P2_CRYPT_KEY0 + i;
1267 memcpy(ws.wi_str,
1268 &wk->wi_keys[i].wi_keydat, keylen);
1269 error = wi_write_record(sc,
1270 (struct wi_ltv_gen *)&ws);
1271 if (error)
1272 return error;
1273 }
1274 return 0;
1275 }
1276 case WI_RID_CNFAUTHMODE:
1277 p2ltv.wi_type = WI_RID_CNFAUTHMODE;
1278 p2ltv.wi_len = 2;
1279 if (le16toh(ltv->wi_val) == 1)
1280 p2ltv.wi_val = htole16(0x01);
1281 else if (le16toh(ltv->wi_val) == 2)
1282 p2ltv.wi_val = htole16(0x02);
1283 ltv = &p2ltv;
1284 break;
1285 case WI_RID_ROAMING_MODE:
1286 if (sc->sc_firmware_type == WI_INTERSIL)
1287 break;
1288 /* not supported */
1289 return 0;
1290 case WI_RID_MICROWAVE_OVEN:
1291 /* not supported */
1292 return 0;
1293 }
1294 } else {
1295 /* LUCENT */
1296 switch (ltv->wi_type) {
1297 case WI_RID_TX_RATE:
1298 switch (ltv->wi_val) {
1299 case 1: ltv->wi_val = 1; break; /* 1Mb/s fixed */
1300 case 2: ltv->wi_val = 2; break; /* 2Mb/s fixed */
1301 case 3: ltv->wi_val = 3; break; /* 11Mb/s auto */
1302 case 5: ltv->wi_val = 4; break; /* 5.5Mb/s fixed */
1303 case 6: ltv->wi_val = 6; break; /* 2Mb/s auto */
1304 case 7: ltv->wi_val = 7; break; /* 5.5Mb/s auto */
1305 case 11: ltv->wi_val = 5; break; /* 11Mb/s fixed */
1306 default: return EINVAL;
1307 }
1308 }
1309 }
1310
1311 if (wi_seek(sc, ltv->wi_type, 0, WI_BAP1))
1312 return(EIO);
1313
1314 CSR_WRITE_2(sc, WI_DATA1, ltv->wi_len);
1315 CSR_WRITE_2(sc, WI_DATA1, ltv->wi_type);
1316
1317 ptr = &ltv->wi_val;
1318 for (i = 0; i < ltv->wi_len - 1; i++)
1319 CSR_WRITE_2(sc, WI_DATA1, ptr[i]);
1320
1321 if (wi_cmd(sc, WI_CMD_ACCESS|WI_ACCESS_WRITE, ltv->wi_type, 0, 0))
1322 return(EIO);
1323
1324 return(0);
1325}
1326
1327static int
1328wi_seek(sc, id, off, chan)
1329 struct wi_softc *sc;
1330 int id, off, chan;
1331{
1332 int i;
1333 int selreg, offreg;
1334 int status;
1335
1336 switch (chan) {
1337 case WI_BAP0:
1338 selreg = WI_SEL0;
1339 offreg = WI_OFF0;
1340 break;
1341 case WI_BAP1:
1342 selreg = WI_SEL1;
1343 offreg = WI_OFF1;
1344 break;
1345 default:
1346 device_printf(sc->dev, "invalid data path: %x\n", chan);
1347 return(EIO);
1348 }
1349
1350 CSR_WRITE_2(sc, selreg, id);
1351 CSR_WRITE_2(sc, offreg, off);
1352
1353 for (i = 0; i < WI_TIMEOUT; i++) {
1354 status = CSR_READ_2(sc, offreg);
1355 if (!(status & (WI_OFF_BUSY|WI_OFF_ERR)))
1356 break;
1357 DELAY(WI_DELAY);
1358 }
1359
1360 if (i == WI_TIMEOUT) {
1361 device_printf(sc->dev, "timeout in wi_seek to %x/%x; last status %x\n",
1362 id, off, status);
1363 return(ETIMEDOUT);
1364 }
1365
1366 return(0);
1367}
1368
1369static int
1370wi_read_data(sc, id, off, buf, len)
1371 struct wi_softc *sc;
1372 int id, off;
1373 caddr_t buf;
1374 int len;
1375{
1376 int i;
1377 u_int16_t *ptr;
1378
1379 if (wi_seek(sc, id, off, WI_BAP1))
1380 return(EIO);
1381
1382 ptr = (u_int16_t *)buf;
1383 for (i = 0; i < len / 2; i++)
1384 ptr[i] = CSR_READ_2(sc, WI_DATA1);
1385
1386 return(0);
1387}
1388
1389/*
1390 * According to the comments in the HCF Light code, there is a bug in
1391 * the Hermes (or possibly in certain Hermes firmware revisions) where
1392 * the chip's internal autoincrement counter gets thrown off during
1393 * data writes: the autoincrement is missed, causing one data word to
1394 * be overwritten and subsequent words to be written to the wrong memory
1395 * locations. The end result is that we could end up transmitting bogus
1396 * frames without realizing it. The workaround for this is to write a
1397 * couple of extra guard words after the end of the transfer, then
1398 * attempt to read then back. If we fail to locate the guard words where
1399 * we expect them, we preform the transfer over again.
1400 */
1401static int
1402wi_write_data(sc, id, off, buf, len)
1403 struct wi_softc *sc;
1404 int id, off;
1405 caddr_t buf;
1406 int len;
1407{
1408 int i;
1409 u_int16_t *ptr;
1410#ifdef WI_HERMES_AUTOINC_WAR
1411 int retries;
1412
1413 retries = 512;
1414again:
1415#endif
1416
1417 if (wi_seek(sc, id, off, WI_BAP0))
1418 return(EIO);
1419
1420 ptr = (u_int16_t *)buf;
1421 for (i = 0; i < (len / 2); i++)
1422 CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
1423
1424#ifdef WI_HERMES_AUTOINC_WAR
1425 CSR_WRITE_2(sc, WI_DATA0, 0x1234);
1426 CSR_WRITE_2(sc, WI_DATA0, 0x5678);
1427
1428 if (wi_seek(sc, id, off + len, WI_BAP0))
1429 return(EIO);
1430
1431 if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
1432 CSR_READ_2(sc, WI_DATA0) != 0x5678) {
1433 if (--retries >= 0)
1434 goto again;
1435 device_printf(sc->dev, "wi_write_data device timeout\n");
1436 return (EIO);
1437 }
1438#endif
1439
1440 return(0);
1441}
1442
1443/*
1444 * Allocate a region of memory inside the NIC and zero
1445 * it out.
1446 */
1447static int
1448wi_alloc_nicmem(sc, len, id)
1449 struct wi_softc *sc;
1450 int len;
1451 int *id;
1452{
1453 int i;
1454
1455 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
1456 device_printf(sc->dev,
1457 "failed to allocate %d bytes on NIC\n", len);
1458 return(ENOMEM);
1459 }
1460
1461 for (i = 0; i < WI_TIMEOUT; i++) {
1462 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
1463 break;
1464 DELAY(WI_DELAY);
1465 }
1466
1467 if (i == WI_TIMEOUT) {
1468 device_printf(sc->dev, "time out allocating memory on card\n");
1469 return(ETIMEDOUT);
1470 }
1471
1472 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1473 *id = CSR_READ_2(sc, WI_ALLOC_FID);
1474
1475 if (wi_seek(sc, *id, 0, WI_BAP0)) {
1476 device_printf(sc->dev, "seek failed while allocating memory on card\n");
1477 return(EIO);
1478 }
1479
1480 for (i = 0; i < len / 2; i++)
1481 CSR_WRITE_2(sc, WI_DATA0, 0);
1482
1483 return(0);
1484}
1485
1486static void
1487wi_setmulti(sc)
1488 struct wi_softc *sc;
1489{
1490 struct ifnet *ifp;
1491 int i = 0;
1492 struct ifmultiaddr *ifma;
1493 struct wi_ltv_mcast mcast;
1494
1495 ifp = &sc->arpcom.ac_if;
1496
1497 bzero((char *)&mcast, sizeof(mcast));
1498
1499 mcast.wi_type = WI_RID_MCAST_LIST;
1500 mcast.wi_len = (3 * 16) + 1;
1501
1502 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1503 wi_write_record(sc, (struct wi_ltv_gen *)&mcast);
1504 return;
1505 }
1506
929783d0 1507#if defined(__DragonFly__) || __FreeBSD_version < 500000
984263bc
MD
1508 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1509#else
1510 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1511#endif
1512 if (ifma->ifma_addr->sa_family != AF_LINK)
1513 continue;
1514 if (i < 16) {
1515 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1516 (char *)&mcast.wi_mcast[i], ETHER_ADDR_LEN);
1517 i++;
1518 } else {
1519 bzero((char *)&mcast, sizeof(mcast));
1520 break;
1521 }
1522 }
1523
1524 mcast.wi_len = (i * 3) + 1;
1525 wi_write_record(sc, (struct wi_ltv_gen *)&mcast);
1526
1527 return;
1528}
1529
1530static void
1531wi_setdef(sc, wreq)
1532 struct wi_softc *sc;
1533 struct wi_req *wreq;
1534{
1535 struct sockaddr_dl *sdl;
1536 struct ifaddr *ifa;
1537 struct ifnet *ifp;
1538
1539 ifp = &sc->arpcom.ac_if;
1540
1541 switch(wreq->wi_type) {
1542 case WI_RID_MAC_NODE:
1543 ifa = ifaddr_byindex(ifp->if_index);
1544 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1545 bcopy((char *)&wreq->wi_val, (char *)&sc->arpcom.ac_enaddr,
1546 ETHER_ADDR_LEN);
1547 bcopy((char *)&wreq->wi_val, LLADDR(sdl), ETHER_ADDR_LEN);
1548 break;
1549 case WI_RID_PORTTYPE:
1550 sc->wi_ptype = le16toh(wreq->wi_val[0]);
1551 break;
1552 case WI_RID_TX_RATE:
1553 sc->wi_tx_rate = le16toh(wreq->wi_val[0]);
1554 break;
1555 case WI_RID_MAX_DATALEN:
1556 sc->wi_max_data_len = le16toh(wreq->wi_val[0]);
1557 break;
1558 case WI_RID_RTS_THRESH:
1559 sc->wi_rts_thresh = le16toh(wreq->wi_val[0]);
1560 break;
1561 case WI_RID_SYSTEM_SCALE:
1562 sc->wi_ap_density = le16toh(wreq->wi_val[0]);
1563 break;
1564 case WI_RID_CREATE_IBSS:
1565 sc->wi_create_ibss = le16toh(wreq->wi_val[0]);
1566 break;
1567 case WI_RID_OWN_CHNL:
1568 sc->wi_channel = le16toh(wreq->wi_val[0]);
1569 break;
1570 case WI_RID_NODENAME:
1571 bzero(sc->wi_node_name, sizeof(sc->wi_node_name));
1572 bcopy((char *)&wreq->wi_val[1], sc->wi_node_name, 30);
1573 break;
1574 case WI_RID_DESIRED_SSID:
1575 bzero(sc->wi_net_name, sizeof(sc->wi_net_name));
1576 bcopy((char *)&wreq->wi_val[1], sc->wi_net_name, 30);
1577 break;
1578 case WI_RID_OWN_SSID:
1579 bzero(sc->wi_ibss_name, sizeof(sc->wi_ibss_name));
1580 bcopy((char *)&wreq->wi_val[1], sc->wi_ibss_name, 30);
1581 break;
1582 case WI_RID_PM_ENABLED:
1583 sc->wi_pm_enabled = le16toh(wreq->wi_val[0]);
1584 break;
1585 case WI_RID_MICROWAVE_OVEN:
1586 sc->wi_mor_enabled = le16toh(wreq->wi_val[0]);
1587 break;
1588 case WI_RID_MAX_SLEEP:
1589 sc->wi_max_sleep = le16toh(wreq->wi_val[0]);
1590 break;
1591 case WI_RID_CNFAUTHMODE:
1592 sc->wi_authtype = le16toh(wreq->wi_val[0]);
1593 break;
1594 case WI_RID_ROAMING_MODE:
1595 sc->wi_roaming = le16toh(wreq->wi_val[0]);
1596 break;
1597 case WI_RID_ENCRYPTION:
1598 sc->wi_use_wep = le16toh(wreq->wi_val[0]);
1599 break;
1600 case WI_RID_TX_CRYPT_KEY:
1601 sc->wi_tx_key = le16toh(wreq->wi_val[0]);
1602 break;
1603 case WI_RID_DEFLT_CRYPT_KEYS:
1604 bcopy((char *)wreq, (char *)&sc->wi_keys,
1605 sizeof(struct wi_ltv_keys));
1606 break;
1607 default:
1608 break;
1609 }
1610
1611 /* Reinitialize WaveLAN. */
1612 wi_init(sc);
1613
1614 return;
1615}
1616
1617static int
bd4539cc 1618wi_ioctl(ifp, command, data, cr)
984263bc
MD
1619 struct ifnet *ifp;
1620 u_long command;
1621 caddr_t data;
bd4539cc 1622 struct ucred *cr;
984263bc
MD
1623{
1624 int error = 0;
1625 int len;
1626 u_int8_t tmpkey[14];
1627 char tmpssid[IEEE80211_NWID_LEN];
1628 struct wi_softc *sc;
1629 struct wi_req wreq;
1630 struct ifreq *ifr;
1631 struct ieee80211req *ireq;
984263bc
MD
1632 int s;
1633
1634 sc = ifp->if_softc;
1635 WI_LOCK(sc, s);
1636 ifr = (struct ifreq *)data;
1637 ireq = (struct ieee80211req *)data;
1638
1639 if (sc->wi_gone) {
1640 error = ENODEV;
1641 goto out;
1642 }
1643
1644 switch(command) {
1645 case SIOCSIFADDR:
1646 case SIOCGIFADDR:
1647 case SIOCSIFMTU:
1648 error = ether_ioctl(ifp, command, data);
1649 break;
1650 case SIOCSIFFLAGS:
1651 /*
1652 * Can't do promisc and hostap at the same time. If all that's
1653 * changing is the promisc flag, try to short-circuit a call to
1654 * wi_init() by just setting PROMISC in the hardware.
1655 */
1656 if (ifp->if_flags & IFF_UP) {
1657 if (sc->wi_ptype != WI_PORTTYPE_AP &&
1658 ifp->if_flags & IFF_RUNNING) {
1659 if (ifp->if_flags & IFF_PROMISC &&
1660 !(sc->wi_if_flags & IFF_PROMISC)) {
1661 WI_SETVAL(WI_RID_PROMISC, 1);
1662 } else if (!(ifp->if_flags & IFF_PROMISC) &&
1663 sc->wi_if_flags & IFF_PROMISC) {
1664 WI_SETVAL(WI_RID_PROMISC, 0);
1665 } else {
1666 wi_init(sc);
1667 }
1668 } else {
1669 wi_init(sc);
1670 }
1671 } else {
1672 if (ifp->if_flags & IFF_RUNNING) {
1673 wi_stop(sc);
1674 }
1675 }
1676 sc->wi_if_flags = ifp->if_flags;
1677 error = 0;
1678 break;
1679 case SIOCSIFMEDIA:
1680 case SIOCGIFMEDIA:
1681 error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
1682 break;
1683 case SIOCADDMULTI:
1684 case SIOCDELMULTI:
1685 wi_setmulti(sc);
1686 error = 0;
1687 break;
1688 case SIOCGWAVELAN:
1689 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1690 if (error)
1691 break;
1692 if (wreq.wi_len > WI_MAX_DATALEN) {
1693 error = EINVAL;
1694 break;
1695 }
1696 /* Don't show WEP keys to non-root users. */
bd4539cc
JH
1697 if (wreq.wi_type == WI_RID_DEFLT_CRYPT_KEYS &&
1698 suser_cred(cr, NULL_CRED_OKAY))
984263bc
MD
1699 break;
1700 if (wreq.wi_type == WI_RID_IFACE_STATS) {
1701 bcopy((char *)&sc->wi_stats, (char *)&wreq.wi_val,
1702 sizeof(sc->wi_stats));
1703 wreq.wi_len = (sizeof(sc->wi_stats) / 2) + 1;
1704 } else if (wreq.wi_type == WI_RID_DEFLT_CRYPT_KEYS) {
1705 bcopy((char *)&sc->wi_keys, (char *)&wreq,
1706 sizeof(struct wi_ltv_keys));
1707 }
1708#ifdef WICACHE
1709 else if (wreq.wi_type == WI_RID_ZERO_CACHE) {
1710 sc->wi_sigitems = sc->wi_nextitem = 0;
1711 } else if (wreq.wi_type == WI_RID_READ_CACHE) {
1712 char *pt = (char *)&wreq.wi_val;
1713 bcopy((char *)&sc->wi_sigitems,
1714 (char *)pt, sizeof(int));
1715 pt += (sizeof (int));
1716 wreq.wi_len = sizeof(int) / 2;
1717 bcopy((char *)&sc->wi_sigcache, (char *)pt,
1718 sizeof(struct wi_sigcache) * sc->wi_sigitems);
1719 wreq.wi_len += ((sizeof(struct wi_sigcache) *
1720 sc->wi_sigitems) / 2) + 1;
1721 }
1722#endif
1723 else if (wreq.wi_type == WI_RID_PROCFRAME) {
1724 wreq.wi_len = 2;
1725 wreq.wi_val[0] = sc->wi_procframe;
1726 } else if (wreq.wi_type == WI_RID_PRISM2) {
1727 wreq.wi_len = 2;
1728 wreq.wi_val[0] = sc->sc_firmware_type != WI_LUCENT;
1729 } else if (wreq.wi_type == WI_RID_SCAN_RES &&
1730 sc->sc_firmware_type == WI_LUCENT) {
1731 memcpy((char *)wreq.wi_val, (char *)sc->wi_scanbuf,
1732 sc->wi_scanbuf_len * 2);
1733 wreq.wi_len = sc->wi_scanbuf_len;
1734 } else {
1735 if (wi_read_record(sc, (struct wi_ltv_gen *)&wreq)) {
1736 error = EINVAL;
1737 break;
1738 }
1739 }
1740 error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
1741 break;
1742 case SIOCSWAVELAN:
bd4539cc 1743 if ((error = suser_cred(cr, NULL_CRED_OKAY)))
984263bc
MD
1744 goto out;
1745 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1746 if (error)
1747 break;
1748 if (wreq.wi_len > WI_MAX_DATALEN) {
1749 error = EINVAL;
1750 break;
1751 }
1752 if (wreq.wi_type == WI_RID_IFACE_STATS) {
1753 error = EINVAL;
1754 break;
1755 } else if (wreq.wi_type == WI_RID_MGMT_XMIT) {
1756 error = wi_mgmt_xmit(sc, (caddr_t)&wreq.wi_val,
1757 wreq.wi_len);
1758 } else if (wreq.wi_type == WI_RID_PROCFRAME) {
1759 sc->wi_procframe = wreq.wi_val[0];
1760 /*
1761 * if we're getting a scan request from a wavelan card
1762 * (non-prism2), send out a cmd_inquire to the card to scan
1763 * results for the scan will be received through the info
1764 * interrupt handler. otherwise the scan request can be
1765 * directly handled by a prism2 card's rid interface.
1766 */
1767 } else if (wreq.wi_type == WI_RID_SCAN_REQ &&
1768 sc->sc_firmware_type == WI_LUCENT) {
1769 wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
1770 } else {
1771 error = wi_write_record(sc, (struct wi_ltv_gen *)&wreq);
1772 if (!error)
1773 wi_setdef(sc, &wreq);
1774 }
1775 break;
1776 case SIOCGPRISM2DEBUG:
1777 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1778 if (error)
1779 break;
1780 if (!(ifp->if_flags & IFF_RUNNING) ||
1781 sc->sc_firmware_type == WI_LUCENT) {
1782 error = EIO;
1783 break;
1784 }
1785 error = wi_get_debug(sc, &wreq);
1786 if (error == 0)
1787 error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
1788 break;
1789 case SIOCSPRISM2DEBUG:
bd4539cc 1790 if ((error = suser_cred(cr, NULL_CRED_OKAY)))
984263bc
MD
1791 goto out;
1792 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1793 if (error)
1794 break;
1795 error = wi_set_debug(sc, &wreq);
1796 break;
1797 case SIOCG80211:
1798 switch(ireq->i_type) {
1799 case IEEE80211_IOC_SSID:
1800 if(ireq->i_val == -1) {
1801 bzero(tmpssid, IEEE80211_NWID_LEN);
1802 error = wi_get_cur_ssid(sc, tmpssid, &len);
1803 if (error != 0)
1804 break;
1805 error = copyout(tmpssid, ireq->i_data,
1806 IEEE80211_NWID_LEN);
1807 ireq->i_len = len;
1808 } else if (ireq->i_val == 0) {
1809 error = copyout(sc->wi_net_name,
1810 ireq->i_data,
1811 IEEE80211_NWID_LEN);
1812 ireq->i_len = IEEE80211_NWID_LEN;
1813 } else
1814 error = EINVAL;
1815 break;
1816 case IEEE80211_IOC_NUMSSIDS:
1817 ireq->i_val = 1;
1818 break;
1819 case IEEE80211_IOC_WEP:
1820 if(!sc->wi_has_wep) {
1821 ireq->i_val = IEEE80211_WEP_NOSUP;
1822 } else {
1823 if(sc->wi_use_wep) {
1824 ireq->i_val =
1825 IEEE80211_WEP_MIXED;
1826 } else {
1827 ireq->i_val =
1828 IEEE80211_WEP_OFF;
1829 }
1830 }
1831 break;
1832 case IEEE80211_IOC_WEPKEY:
1833 if(!sc->wi_has_wep ||
1834 ireq->i_val < 0 || ireq->i_val > 3) {
1835 error = EINVAL;
1836 break;
1837 }
1838 len = sc->wi_keys.wi_keys[ireq->i_val].wi_keylen;
bd4539cc 1839 if (suser_cred(cr, NULL_CRED_OKAY))
984263bc
MD
1840 bcopy(sc->wi_keys.wi_keys[ireq->i_val].wi_keydat,
1841 tmpkey, len);
1842 else
1843 bzero(tmpkey, len);
1844
1845 ireq->i_len = len;
1846 error = copyout(tmpkey, ireq->i_data, len);
1847
1848 break;
1849 case IEEE80211_IOC_NUMWEPKEYS:
1850 if(!sc->wi_has_wep)
1851 error = EINVAL;
1852 else
1853 ireq->i_val = 4;
1854 break;
1855 case IEEE80211_IOC_WEPTXKEY:
1856 if(!sc->wi_has_wep)
1857 error = EINVAL;
1858 else
1859 ireq->i_val = sc->wi_tx_key;
1860 break;
1861 case IEEE80211_IOC_AUTHMODE:
1862 ireq->i_val = sc->wi_authmode;
1863 break;
1864 case IEEE80211_IOC_STATIONNAME:
1865 error = copyout(sc->wi_node_name,
1866 ireq->i_data, IEEE80211_NWID_LEN);
1867 ireq->i_len = IEEE80211_NWID_LEN;
1868 break;
1869 case IEEE80211_IOC_CHANNEL:
1870 wreq.wi_type = WI_RID_CURRENT_CHAN;
1871 wreq.wi_len = WI_MAX_DATALEN;
1872 if (wi_read_record(sc, (struct wi_ltv_gen *)&wreq))
1873 error = EINVAL;
1874 else {
1875 ireq->i_val = wreq.wi_val[0];
1876 }
1877 break;
1878 case IEEE80211_IOC_POWERSAVE:
1879 if(sc->wi_pm_enabled)
1880 ireq->i_val = IEEE80211_POWERSAVE_ON;
1881 else
1882 ireq->i_val = IEEE80211_POWERSAVE_OFF;
1883 break;
1884 case IEEE80211_IOC_POWERSAVESLEEP:
1885 ireq->i_val = sc->wi_max_sleep;
1886 break;
1887 default:
1888 error = EINVAL;
1889 }
1890 break;
1891 case SIOCS80211:
bd4539cc 1892 if ((error = suser_cred(cr, NULL_CRED_OKAY)))
984263bc
MD
1893 goto out;
1894 switch(ireq->i_type) {
1895 case IEEE80211_IOC_SSID:
1896 if (ireq->i_val != 0 ||
1897 ireq->i_len > IEEE80211_NWID_LEN) {
1898 error = EINVAL;
1899 break;
1900 }
1901 /* We set both of them */
1902 bzero(sc->wi_net_name, IEEE80211_NWID_LEN);
1903 error = copyin(ireq->i_data,
1904 sc->wi_net_name, ireq->i_len);
1905 bcopy(sc->wi_net_name, sc->wi_ibss_name, IEEE80211_NWID_LEN);
1906 break;
1907 case IEEE80211_IOC_WEP:
1908 /*
1909 * These cards only support one mode so
1910 * we just turn wep on what ever is
1911 * passed in if it's not OFF.
1912 */
1913 if (ireq->i_val == IEEE80211_WEP_OFF) {
1914 sc->wi_use_wep = 0;
1915 } else {
1916 sc->wi_use_wep = 1;
1917 }
1918 break;
1919 case IEEE80211_IOC_WEPKEY:
1920 if (ireq->i_val < 0 || ireq->i_val > 3 ||
1921 ireq->i_len > 13) {
1922 error = EINVAL;
1923 break;
1924 }
1925 bzero(sc->wi_keys.wi_keys[ireq->i_val].wi_keydat, 13);
1926 error = copyin(ireq->i_data,
1927 sc->wi_keys.wi_keys[ireq->i_val].wi_keydat,
1928 ireq->i_len);
1929 if(error)
1930 break;
1931 sc->wi_keys.wi_keys[ireq->i_val].wi_keylen =
1932 ireq->i_len;
1933 break;
1934 case IEEE80211_IOC_WEPTXKEY:
1935 if (ireq->i_val < 0 || ireq->i_val > 3) {
1936 error = EINVAL;
1937 break;
1938 }
1939 sc->wi_tx_key = ireq->i_val;
1940 break;
1941 case IEEE80211_IOC_AUTHMODE:
1942 sc->wi_authmode = ireq->i_val;
1943 break;
1944 case IEEE80211_IOC_STATIONNAME:
1945 if (ireq->i_len > 32) {
1946 error = EINVAL;
1947 break;
1948 }
1949 bzero(sc->wi_node_name, 32);
1950 error = copyin(ireq->i_data,
1951 sc->wi_node_name, ireq->i_len);
1952 break;
1953 case IEEE80211_IOC_CHANNEL:
1954 /*
1955 * The actual range is 1-14, but if you
1956 * set it to 0 you get the default. So
1957 * we let that work too.
1958 */
1959 if (ireq->i_val < 0 || ireq->i_val > 14) {
1960 error = EINVAL;
1961 break;
1962 }
1963 sc->wi_channel = ireq->i_val;
1964 break;
1965 case IEEE80211_IOC_POWERSAVE:
1966 switch (ireq->i_val) {
1967 case IEEE80211_POWERSAVE_OFF:
1968 sc->wi_pm_enabled = 0;
1969 break;
1970 case IEEE80211_POWERSAVE_ON:
1971 sc->wi_pm_enabled = 1;
1972 break;
1973 default:
1974 error = EINVAL;
1975 break;
1976 }
1977 break;
1978 case IEEE80211_IOC_POWERSAVESLEEP:
1979 if (ireq->i_val < 0) {
1980 error = EINVAL;
1981 break;
1982 }
1983 sc->wi_max_sleep = ireq->i_val;
1984 break;
1985 default:
1986 error = EINVAL;
1987 break;
1988 }
1989
1990 /* Reinitialize WaveLAN. */
1991 wi_init(sc);
1992
1993 break;
1994 case SIOCHOSTAP_ADD:
1995 case SIOCHOSTAP_DEL:
1996 case SIOCHOSTAP_GET:
1997 case SIOCHOSTAP_GETALL:
1998 case SIOCHOSTAP_GFLAGS:
1999 case SIOCHOSTAP_SFLAGS:
2000 /* Send all Host AP specific ioctl's to Host AP code. */
2001 error = wihap_ioctl(sc, command, data);
2002 break;
2003 default:
2004 error = EINVAL;
2005 break;
2006 }
2007out:
2008 WI_UNLOCK(sc, s);
2009
2010 return(error);
2011}
2012
2013static void
2014wi_init(xsc)
2015 void *xsc;
2016{
2017 struct wi_softc *sc = xsc;
2018 struct ifnet *ifp = &sc->arpcom.ac_if;
2019 struct wi_ltv_macaddr mac;
2020 int id = 0;
2021 int s;
2022
2023 WI_LOCK(sc, s);
2024
2025 if (sc->wi_gone) {
2026 WI_UNLOCK(sc, s);
2027 return;
2028 }
2029
2030 if (ifp->if_flags & IFF_RUNNING)
2031 wi_stop(sc);
2032
2033 wi_reset(sc);
2034
2035 /* Program max data length. */
2036 WI_SETVAL(WI_RID_MAX_DATALEN, sc->wi_max_data_len);
2037
2038 /* Set the port type. */
2039 WI_SETVAL(WI_RID_PORTTYPE, sc->wi_ptype);
2040
2041 /* Enable/disable IBSS creation. */
2042 WI_SETVAL(WI_RID_CREATE_IBSS, sc->wi_create_ibss);
2043
2044 /* Program the RTS/CTS threshold. */
2045 WI_SETVAL(WI_RID_RTS_THRESH, sc->wi_rts_thresh);
2046
2047 /* Program the TX rate */
2048 WI_SETVAL(WI_RID_TX_RATE, sc->wi_tx_rate);
2049
2050 /* Access point density */
2051 WI_SETVAL(WI_RID_SYSTEM_SCALE, sc->wi_ap_density);
2052
2053 /* Power Management Enabled */
2054 WI_SETVAL(WI_RID_PM_ENABLED, sc->wi_pm_enabled);
2055
2056 /* Power Managment Max Sleep */
2057 WI_SETVAL(WI_RID_MAX_SLEEP, sc->wi_max_sleep);
2058
2059 /* Roaming type */
2060 WI_SETVAL(WI_RID_ROAMING_MODE, sc->wi_roaming);
2061
2062 /* Specify the IBSS name */
2063 WI_SETSTR(WI_RID_OWN_SSID, sc->wi_ibss_name);
2064
2065 /* Specify the network name */
2066 WI_SETSTR(WI_RID_DESIRED_SSID, sc->wi_net_name);
2067
2068 /* Specify the frequency to use */
2069 WI_SETVAL(WI_RID_OWN_CHNL, sc->wi_channel);
2070
2071 /* Program the nodename. */
2072 WI_SETSTR(WI_RID_NODENAME, sc->wi_node_name);
2073
2074 /* Specify the authentication mode. */
2075 WI_SETVAL(WI_RID_CNFAUTHMODE, sc->wi_authmode);
2076
2077 /* Set our MAC address. */
2078 mac.wi_len = 4;
2079 mac.wi_type = WI_RID_MAC_NODE;
2080 bcopy((char *)&sc->arpcom.ac_enaddr,
2081 (char *)&mac.wi_mac_addr, ETHER_ADDR_LEN);
2082 wi_write_record(sc, (struct wi_ltv_gen *)&mac);
2083
2084 /*
2085 * Initialize promisc mode.
2086 * Being in the Host-AP mode causes
2087 * great deal of pain if promisc mode is set.
2088 * Therefore we avoid confusing the firmware
2089 * and always reset promisc mode in Host-AP regime,
2090 * it shows us all the packets anyway.
2091 */
2092 if (sc->wi_ptype != WI_PORTTYPE_AP && ifp->if_flags & IFF_PROMISC)
2093 WI_SETVAL(WI_RID_PROMISC, 1);
2094 else
2095 WI_SETVAL(WI_RID_PROMISC, 0);
2096
2097 /* Configure WEP. */
2098 if (sc->wi_has_wep) {
2099 WI_SETVAL(WI_RID_ENCRYPTION, sc->wi_use_wep);
2100 WI_SETVAL(WI_RID_TX_CRYPT_KEY, sc->wi_tx_key);
2101 sc->wi_keys.wi_len = (sizeof(struct wi_ltv_keys) / 2) + 1;
2102 sc->wi_keys.wi_type = WI_RID_DEFLT_CRYPT_KEYS;
2103 wi_write_record(sc, (struct wi_ltv_gen *)&sc->wi_keys);
2104 if (sc->sc_firmware_type != WI_LUCENT && sc->wi_use_wep) {
2105 /*
2106 * ONLY HWB3163 EVAL-CARD Firmware version
2107 * less than 0.8 variant2
2108 *
2109 * If promiscuous mode disable, Prism2 chip
2110 * does not work with WEP.
2111 * It is under investigation for details.
2112 * (ichiro@netbsd.org)
2113 *
2114 * And make sure that we don't need to do it
2115 * in hostap mode, since it interferes with
2116 * the above hostap workaround.
2117 */
2118 if (sc->wi_ptype != WI_PORTTYPE_AP &&
2119 sc->sc_firmware_type == WI_INTERSIL &&
2120 sc->sc_sta_firmware_ver < 802 ) {
2121 /* firm ver < 0.8 variant 2 */
2122 WI_SETVAL(WI_RID_PROMISC, 1);
2123 }
2124 WI_SETVAL(WI_RID_CNFAUTHMODE, sc->wi_authtype);
2125 }
2126 }
2127
2128 /* Set multicast filter. */
2129 wi_setmulti(sc);
2130
2131 /* Enable desired port */
2132 wi_cmd(sc, WI_CMD_ENABLE | sc->wi_portnum, 0, 0, 0);
2133
2134 if (wi_alloc_nicmem(sc, ETHER_MAX_LEN + sizeof(struct wi_frame) + 8, &id))
2135 device_printf(sc->dev, "tx buffer allocation failed\n");
2136 sc->wi_tx_data_id = id;
2137
2138 if (wi_alloc_nicmem(sc, ETHER_MAX_LEN + sizeof(struct wi_frame) + 8, &id))
2139 device_printf(sc->dev, "mgmt. buffer allocation failed\n");
2140 sc->wi_tx_mgmt_id = id;
2141
2142 /* enable interrupts */
2143 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
2144
2145 wihap_init(sc);
2146
2147 ifp->if_flags |= IFF_RUNNING;
2148 ifp->if_flags &= ~IFF_OACTIVE;
2149
2150 sc->wi_stat_ch = timeout(wi_inquire, sc, hz * 60);
2151 WI_UNLOCK(sc, s);
2152
2153 return;
2154}
2155
2156#define RC4STATE 256
2157#define RC4KEYLEN 16
2158#define RC4SWAP(x,y) \
2159 do { u_int8_t t = state[x]; state[x] = state[y]; state[y] = t; } while(0)
2160
2161static void
2162wi_do_hostencrypt(struct wi_softc *sc, caddr_t buf, int len)
2163{
2164 u_int32_t i, crc, klen;
2165 u_int8_t state[RC4STATE], key[RC4KEYLEN];
2166 u_int8_t x, y, *dat;
2167
2168 if (!sc->wi_icv_flag) {
2169 sc->wi_icv = arc4random();
2170 sc->wi_icv_flag++;
2171 } else
2172 sc->wi_icv++;
2173 /*
2174 * Skip 'bad' IVs from Fluhrer/Mantin/Shamir:
2175 * (B, 255, N) with 3 <= B < 8
2176 */
2177 if (sc->wi_icv >= 0x03ff00 &&
2178 (sc->wi_icv & 0xf8ff00) == 0x00ff00)
2179 sc->wi_icv += 0x000100;
2180
2181 /* prepend 24bit IV to tx key, byte order does not matter */
2182 key[0] = sc->wi_icv >> 16;
2183 key[1] = sc->wi_icv >> 8;
2184 key[2] = sc->wi_icv;
2185
2186 klen = sc->wi_keys.wi_keys[sc->wi_tx_key].wi_keylen +
2187 IEEE80211_WEP_IVLEN;
2188 klen = (klen >= RC4KEYLEN) ? RC4KEYLEN : RC4KEYLEN/2;
2189 bcopy((char *)&sc->wi_keys.wi_keys[sc->wi_tx_key].wi_keydat,
2190 (char *)key + IEEE80211_WEP_IVLEN, klen - IEEE80211_WEP_IVLEN);
2191
2192 /* rc4 keysetup */
2193 x = y = 0;
2194 for (i = 0; i < RC4STATE; i++)
2195 state[i] = i;
2196 for (i = 0; i < RC4STATE; i++) {
2197 y = (key[x] + state[i] + y) % RC4STATE;
2198 RC4SWAP(i, y);
2199 x = (x + 1) % klen;
2200 }
2201
2202 /* output: IV, tx keyid, rc4(data), rc4(crc32(data)) */
2203 dat = buf;
2204 dat[0] = key[0];
2205 dat[1] = key[1];
2206 dat[2] = key[2];
2207 dat[3] = sc->wi_tx_key << 6; /* pad and keyid */
2208 dat += 4;
2209
2210 /* compute rc4 over data, crc32 over data */
2211 crc = ~0;
2212 x = y = 0;
2213 for (i = 0; i < len; i++) {
2214 x = (x + 1) % RC4STATE;
2215 y = (state[x] + y) % RC4STATE;
2216 RC4SWAP(x, y);
2217 crc = crc32_tab[(crc ^ dat[i]) & 0xff] ^ (crc >> 8);
2218 dat[i] ^= state[(state[x] + state[y]) % RC4STATE];
2219 }
2220 crc = ~crc;
2221 dat += len;
2222
2223 /* append little-endian crc32 and encrypt */
2224 dat[0] = crc;
2225 dat[1] = crc >> 8;
2226 dat[2] = crc >> 16;
2227 dat[3] = crc >> 24;
2228 for (i = 0; i < IEEE80211_WEP_CRCLEN; i++) {
2229 x = (x + 1) % RC4STATE;
2230 y = (state[x] + y) % RC4STATE;
2231 RC4SWAP(x, y);
2232 dat[i] ^= state[(state[x] + state[y]) % RC4STATE];
2233 }
2234}
2235
2236static void
2237wi_start(ifp)
2238 struct ifnet *ifp;
2239{
2240 struct wi_softc *sc;
2241 struct mbuf *m0;
2242 struct wi_frame tx_frame;
2243 struct ether_header *eh;
2244 int id;
2245 int s;
2246
2247 sc = ifp->if_softc;
2248 WI_LOCK(sc, s);
2249
2250 if (sc->wi_gone) {
2251 WI_UNLOCK(sc, s);
2252 return;
2253 }
2254
2255 if (ifp->if_flags & IFF_OACTIVE) {
2256 WI_UNLOCK(sc, s);
2257 return;
2258 }
2259
2260nextpkt:
2261 IF_DEQUEUE(&ifp->if_snd, m0);
2262 if (m0 == NULL) {
2263 WI_UNLOCK(sc, s);
2264 return;
2265 }
2266
2267 bzero((char *)&tx_frame, sizeof(tx_frame));
2268 tx_frame.wi_frame_ctl = htole16(WI_FTYPE_DATA);
2269 id = sc->wi_tx_data_id;
2270 eh = mtod(m0, struct ether_header *);
2271
2272 if (sc->wi_ptype == WI_PORTTYPE_AP) {
2273 if (!wihap_check_tx(&sc->wi_hostap_info,
2274 eh->ether_dhost, &tx_frame.wi_tx_rate)) {
2275 if (ifp->if_flags & IFF_DEBUG)
2276 printf("wi_start: dropping unassoc "
2277 "dst %6D\n", eh->ether_dhost, ":");
2278 m_freem(m0);
2279 goto nextpkt;
2280 }
2281 }
2282 /*
2283 * Use RFC1042 encoding for IP and ARP datagrams,
2284 * 802.3 for anything else.
2285 */
2286 if (ntohs(eh->ether_type) > ETHER_MAX_LEN) {
2287 bcopy((char *)&eh->ether_dhost,
2288 (char *)&tx_frame.wi_addr1, ETHER_ADDR_LEN);
2289 if (sc->wi_ptype == WI_PORTTYPE_AP) {
2290 tx_frame.wi_tx_ctl = WI_ENC_TX_MGMT; /* XXX */
2291 tx_frame.wi_frame_ctl |= WI_FCTL_FROMDS;
2292 if (sc->wi_use_wep)
2293 tx_frame.wi_frame_ctl |= WI_FCTL_WEP;
2294 bcopy((char *)&sc->arpcom.ac_enaddr,
2295 (char *)&tx_frame.wi_addr2, ETHER_ADDR_LEN);
2296 bcopy((char *)&eh->ether_shost,
2297 (char *)&tx_frame.wi_addr3, ETHER_ADDR_LEN);
2298 }
2299 else
2300 bcopy((char *)&eh->ether_shost,
2301 (char *)&tx_frame.wi_addr2, ETHER_ADDR_LEN);
2302 bcopy((char *)&eh->ether_dhost,
2303 (char *)&tx_frame.wi_dst_addr, ETHER_ADDR_LEN);
2304 bcopy((char *)&eh->ether_shost,
2305 (char *)&tx_frame.wi_src_addr, ETHER_ADDR_LEN);
2306
2307 tx_frame.wi_dat_len = m0->m_pkthdr.len - WI_SNAPHDR_LEN;
2308 tx_frame.wi_dat[0] = htons(WI_SNAP_WORD0);
2309 tx_frame.wi_dat[1] = htons(WI_SNAP_WORD1);
2310 tx_frame.wi_len = htons(m0->m_pkthdr.len - WI_SNAPHDR_LEN);
2311 tx_frame.wi_type = eh->ether_type;
2312
2313 if (sc->wi_ptype == WI_PORTTYPE_AP && sc->wi_use_wep) {
2314 /* Do host encryption. */
2315 bcopy(&tx_frame.wi_dat[0], &sc->wi_txbuf[4], 8);
2316 m_copydata(m0, sizeof(struct ether_header),
2317 m0->m_pkthdr.len - sizeof(struct ether_header),
2318 (caddr_t)&sc->wi_txbuf[12]);
2319 wi_do_hostencrypt(sc, &sc->wi_txbuf[0],
2320 tx_frame.wi_dat_len);
2321 tx_frame.wi_dat_len += IEEE80211_WEP_IVLEN +
2322 IEEE80211_WEP_KIDLEN + IEEE80211_WEP_CRCLEN;
2323 wi_write_data(sc, id, 0, (caddr_t)&tx_frame,
2324 sizeof(struct wi_frame));
2325 wi_write_data(sc, id, WI_802_11_OFFSET_RAW,
2326 (caddr_t)&sc->wi_txbuf, (m0->m_pkthdr.len -
2327 sizeof(struct ether_header)) + 18);
2328 } else {
2329 m_copydata(m0, sizeof(struct ether_header),
2330 m0->m_pkthdr.len - sizeof(struct ether_header),
2331 (caddr_t)&sc->wi_txbuf);
2332 wi_write_data(sc, id, 0, (caddr_t)&tx_frame,
2333 sizeof(struct wi_frame));
2334 wi_write_data(sc, id, WI_802_11_OFFSET,
2335 (caddr_t)&sc->wi_txbuf, (m0->m_pkthdr.len -
2336 sizeof(struct ether_header)) + 2);
2337 }
2338 } else {
2339 tx_frame.wi_dat_len = m0->m_pkthdr.len;
2340
2341 if (sc->wi_ptype == WI_PORTTYPE_AP && sc->wi_use_wep) {
2342 /* Do host encryption. */
2343 printf( "XXX: host encrypt not implemented for 802.3\n" );
2344 } else {
2345 eh->ether_type = htons(m0->m_pkthdr.len -
2346 WI_SNAPHDR_LEN);
2347 m_copydata(m0, 0, m0->m_pkthdr.len,
2348 (caddr_t)&sc->wi_txbuf);
2349
2350 wi_write_data(sc, id, 0, (caddr_t)&tx_frame,
2351 sizeof(struct wi_frame));
2352 wi_write_data(sc, id, WI_802_3_OFFSET,
2353 (caddr_t)&sc->wi_txbuf, m0->m_pkthdr.len + 2);
2354 }
2355 }
2356
2357 /*
2358 * If there's a BPF listner, bounce a copy of
2359 * this frame to him. Also, don't send this to the bpf sniffer
2360 * if we're in procframe or monitor sniffing mode.
2361 */
2362 if (!(sc->wi_procframe || sc->wi_debug.wi_monitor) && ifp->if_bpf)
2363 bpf_mtap(ifp, m0);
2364
2365 m_freem(m0);
2366
2367 if (wi_cmd(sc, WI_CMD_TX|WI_RECLAIM, id, 0, 0))
2368 device_printf(sc->dev, "xmit failed\n");
2369
2370 ifp->if_flags |= IFF_OACTIVE;
2371
2372 /*
2373 * Set a timeout in case the chip goes out to lunch.
2374 */
2375 ifp->if_timer = 5;
2376
2377 WI_UNLOCK(sc, s);
2378 return;
2379}
2380
2381int
2382wi_mgmt_xmit(sc, data, len)
2383 struct wi_softc *sc;
2384 caddr_t data;
2385 int len;
2386{
2387 struct wi_frame tx_frame;
2388 int id;
2389 struct wi_80211_hdr *hdr;
2390 caddr_t dptr;
2391
2392 if (sc->wi_gone)
2393 return(ENODEV);
2394
2395 hdr = (struct wi_80211_hdr *)data;
2396 dptr = data + sizeof(struct wi_80211_hdr);
2397
2398 bzero((char *)&tx_frame, sizeof(tx_frame));
2399 id = sc->wi_tx_mgmt_id;
2400
2401 bcopy((char *)hdr, (char *)&tx_frame.wi_frame_ctl,
2402 sizeof(struct wi_80211_hdr));
2403
2404 tx_frame.wi_tx_ctl = WI_ENC_TX_MGMT;
2405 tx_frame.wi_dat_len = len - sizeof(struct wi_80211_hdr);
2406 tx_frame.wi_len = htons(tx_frame.wi_dat_len);
2407
2408 wi_write_data(sc, id, 0, (caddr_t)&tx_frame, sizeof(struct wi_frame));
2409 wi_write_data(sc, id, WI_802_11_OFFSET_RAW, dptr,
2410 len - sizeof(struct wi_80211_hdr) + 2);
2411
2412 if (wi_cmd(sc, WI_CMD_TX|WI_RECLAIM, id, 0, 0)) {
2413 device_printf(sc->dev, "xmit failed\n");
2414 return(EIO);
2415 }
2416
2417 return(0);
2418}
2419
2420static void
2421wi_stop(sc)
2422 struct wi_softc *sc;
2423{
2424 struct ifnet *ifp;
2425 int s;
2426
2427 WI_LOCK(sc, s);
2428
2429 if (sc->wi_gone) {
2430 WI_UNLOCK(sc, s);
2431 return;
2432 }
2433
2434 wihap_shutdown(sc);
2435
2436 ifp = &sc->arpcom.ac_if;
2437
2438 /*
2439 * If the card is gone and the memory port isn't mapped, we will
2440 * (hopefully) get 0xffff back from the status read, which is not
2441 * a valid status value.
2442 */
2443 if (CSR_READ_2(sc, WI_STATUS) != 0xffff) {
2444 CSR_WRITE_2(sc, WI_INT_EN, 0);
2445 wi_cmd(sc, WI_CMD_DISABLE|sc->wi_portnum, 0, 0, 0);
2446 }
2447
2448 untimeout(wi_inquire, sc, sc->wi_stat_ch);
2449
2450 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
2451
2452 WI_UNLOCK(sc, s);
2453 return;
2454}
2455
2456static void
2457wi_watchdog(ifp)
2458 struct ifnet *ifp;
2459{
2460 struct wi_softc *sc;
2461
2462 sc = ifp->if_softc;
2463
2464 device_printf(sc->dev, "watchdog timeout\n");
2465
2466 wi_init(sc);
2467
2468 ifp->if_oerrors++;
2469
2470 return;
2471}
2472
2473int
2474wi_alloc(dev, rid)
2475 device_t dev;
2476 int rid;
2477{
2478 struct wi_softc *sc = device_get_softc(dev);
2479
2480 if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2481 sc->iobase_rid = rid;
2482 sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2483 &sc->iobase_rid, 0, ~0, (1 << 6),
2484 rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2485 if (!sc->iobase) {
2486 device_printf(dev, "No I/O space?!\n");
2487 return (ENXIO);
2488 }
2489
2490 sc->wi_io_addr = rman_get_start(sc->iobase);
2491 sc->wi_btag = rman_get_bustag(sc->iobase);
2492 sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2493 } else {
2494 sc->mem_rid = rid;
2495 sc->mem = bus_alloc_resource(dev, SYS_RES_MEMORY,
2496 &sc->mem_rid, 0, ~0, 1, RF_ACTIVE);
2497
2498 if (!sc->mem) {
2499 device_printf(dev, "No Mem space on prism2.5?\n");
2500 return (ENXIO);
2501 }
2502
2503 sc->wi_btag = rman_get_bustag(sc->mem);
2504 sc->wi_bhandle = rman_get_bushandle(sc->mem);
2505 }
2506
2507
2508 sc->irq_rid = 0;
2509 sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
2510 0, ~0, 1, RF_ACTIVE |
2511 ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2512
2513 if (!sc->irq) {
2514 wi_free(dev);
2515 device_printf(dev, "No irq?!\n");
2516 return (ENXIO);
2517 }
2518
2519 sc->dev = dev;
2520 sc->wi_unit = device_get_unit(dev);
2521
2522 return (0);
2523}
2524
2525void
2526wi_free(dev)
2527 device_t dev;
2528{
2529 struct wi_softc *sc = device_get_softc(dev);
2530
2531 if (sc->iobase != NULL) {
2532 bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2533 sc->iobase = NULL;
2534 }
2535 if (sc->irq != NULL) {
2536 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2537 sc->irq = NULL;
2538 }
2539 if (sc->mem != NULL) {
2540 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
2541 sc->mem = NULL;
2542 }
2543
2544 return;
2545}
2546
2547void
2548wi_shutdown(dev)
2549 device_t dev;
2550{
2551 struct wi_softc *sc;
2552
2553 sc = device_get_softc(dev);
2554 wi_stop(sc);
2555
2556 return;
2557}
2558
2559#ifdef WICACHE
2560/* wavelan signal strength cache code.
2561 * store signal/noise/quality on per MAC src basis in
2562 * a small fixed cache. The cache wraps if > MAX slots
2563 * used. The cache may be zeroed out to start over.
2564 * Two simple filters exist to reduce computation:
2565 * 1. ip only (literally 0x800) which may be used
2566 * to ignore some packets. It defaults to ip only.
2567 * it could be used to focus on broadcast, non-IP 802.11 beacons.
2568 * 2. multicast/broadcast only. This may be used to
2569 * ignore unicast packets and only cache signal strength
2570 * for multicast/broadcast packets (beacons); e.g., Mobile-IP
2571 * beacons and not unicast traffic.
2572 *
2573 * The cache stores (MAC src(index), IP src (major clue), signal,
2574 * quality, noise)
2575 *
2576 * No apologies for storing IP src here. It's easy and saves much
2577 * trouble elsewhere. The cache is assumed to be INET dependent,
2578 * although it need not be.
2579 */
2580
2581#ifdef documentation
2582
2583int wi_sigitems; /* number of cached entries */
2584struct wi_sigcache wi_sigcache[MAXWICACHE]; /* array of cache entries */
2585int wi_nextitem; /* index/# of entries */
2586
2587
2588#endif
2589
2590/* control variables for cache filtering. Basic idea is
2591 * to reduce cost (e.g., to only Mobile-IP agent beacons
2592 * which are broadcast or multicast). Still you might
2593 * want to measure signal strength with unicast ping packets
2594 * on a pt. to pt. ant. setup.
2595 */
2596/* set true if you want to limit cache items to broadcast/mcast
2597 * only packets (not unicast). Useful for mobile-ip beacons which
2598 * are broadcast/multicast at network layer. Default is all packets
2599 * so ping/unicast will work say with pt. to pt. antennae setup.
2600 */
2601static int wi_cache_mcastonly = 0;
2602SYSCTL_INT(_machdep, OID_AUTO, wi_cache_mcastonly, CTLFLAG_RW,
2603 &wi_cache_mcastonly, 0, "");
2604
2605/* set true if you want to limit cache items to IP packets only
2606*/
2607static int wi_cache_iponly = 1;
2608SYSCTL_INT(_machdep, OID_AUTO, wi_cache_iponly, CTLFLAG_RW,
2609 &wi_cache_iponly, 0, "");
2610
2611/*
2612 * Original comments:
2613 * -----------------
2614 * wi_cache_store, per rx packet store signal
2615 * strength in MAC (src) indexed cache.
2616 *
2617 * follows linux driver in how signal strength is computed.
2618 * In ad hoc mode, we use the rx_quality field.
2619 * signal and noise are trimmed to fit in the range from 47..138.
2620 * rx_quality field MSB is signal strength.
2621 * rx_quality field LSB is noise.
2622 * "quality" is (signal - noise) as is log value.
2623 * note: quality CAN be negative.
2624 *
2625 * In BSS mode, we use the RID for communication quality.
2626 * TBD: BSS mode is currently untested.
2627 *
2628 * Bill's comments:
2629 * ---------------
2630 * Actually, we use the rx_quality field all the time for both "ad-hoc"
2631 * and BSS modes. Why? Because reading an RID is really, really expensive:
2632 * there's a bunch of PIO operations that have to be done to read a record
2633 * from the NIC, and reading the comms quality RID each time a packet is
2634 * received can really hurt performance. We don't have to do this anyway:
2635 * the comms quality field only reflects the values in the rx_quality field
2636 * anyway. The comms quality RID is only meaningful in infrastructure mode,
2637 * but the values it contains are updated based on the rx_quality from
2638 * frames received from the access point.
2639 *
2640 * Also, according to Lucent, the signal strength and noise level values
2641 * can be converted to dBms by subtracting 149, so I've modified the code
2642 * to do that instead of the scaling it did originally.
2643 */
2644static void
3013ac0e 2645wi_cache_store(struct wi_softc *sc, struct mbuf *m, unsigned short rx_quality)
984263bc 2646{
3013ac0e
JS
2647 struct ether_header *eh = mtod(m, struct ether_header *);
2648 struct ip *ip = NULL;
984263bc
MD
2649 int i;
2650 static int cache_slot = 0; /* use this cache entry */
2651 static int wrapindex = 0; /* next "free" cache entry */
2652 int sig, noise;
984263bc
MD
2653
2654 /*
2655 * filters:
2656 * 1. ip only
2657 * 2. configurable filter to throw out unicast packets,
2658 * keep multicast only.
2659 */
2660
3013ac0e
JS
2661 if ((ntohs(eh->ether_type) == ETHERTYPE_IP))
2662 ip = (struct ip *)(mtod(m, uint8_t *) + ETHER_HDR_LEN);
2663 else if (wi_cache_iponly)
984263bc 2664 return;
984263bc
MD
2665
2666 /*
2667 * filter for broadcast/multicast only
2668 */
2669 if (wi_cache_mcastonly && ((eh->ether_dhost[0] & 1) == 0)) {
2670 return;
2671 }
2672
2673#ifdef SIGDEBUG
2674 printf("wi%d: q value %x (MSB=0x%x, LSB=0x%x) \n", sc->wi_unit,
2675 rx_quality & 0xffff, rx_quality >> 8, rx_quality & 0xff);
2676#endif
2677
984263bc
MD
2678
2679 /*
2680 * do a linear search for a matching MAC address
2681 * in the cache table
2682 * . MAC address is 6 bytes,
2683 * . var w_nextitem holds total number of entries already cached
2684 */
2685 for(i = 0; i < sc->wi_nextitem; i++) {
2686 if (! bcmp(eh->ether_shost , sc->wi_sigcache[i].macsrc, 6 )) {
2687 /*
2688 * Match!,
2689 * so we already have this entry,
2690 * update the data
2691 */
2692 break;
2693 }
2694 }
2695
2696 /*
2697 * did we find a matching mac address?
2698 * if yes, then overwrite a previously existing cache entry
2699 */
2700 if (i < sc->wi_nextitem ) {
2701 cache_slot = i;
2702 }
2703 /*
2704 * else, have a new address entry,so
2705 * add this new entry,
2706 * if table full, then we need to replace LRU entry
2707 */
2708 else {
2709
2710 /*
2711 * check for space in cache table
2712 * note: wi_nextitem also holds number of entries
2713 * added in the cache table
2714 */
2715 if ( sc->wi_nextitem < MAXWICACHE ) {
2716 cache_slot = sc->wi_nextitem;
2717 sc->wi_nextitem++;
2718 sc->wi_sigitems = sc->wi_nextitem;
2719 }
2720 /* no space found, so simply wrap with wrap index
2721 * and "zap" the next entry
2722 */
2723 else {
2724 if (wrapindex == MAXWICACHE) {
2725 wrapindex = 0;
2726 }
2727 cache_slot = wrapindex++;
2728 }
2729 }
2730
2731 /*
2732 * invariant: cache_slot now points at some slot
2733 * in cache.
2734 */
2735 if (cache_slot < 0 || cache_slot >= MAXWICACHE) {
2736 log(LOG_ERR, "wi_cache_store, bad index: %d of "
2737 "[0..%d], gross cache error\n",
2738 cache_slot, MAXWICACHE);
2739 return;
2740 }
2741
2742 /*
2743 * store items in cache
2744 * .ip source address
2745 * .mac src
2746 * .signal, etc.
2747 */
3013ac0e 2748 if (ip != NULL)
984263bc
MD
2749 sc->wi_sigcache[cache_slot].ipsrc = ip->ip_src.s_addr;
2750 bcopy( eh->ether_shost, sc->wi_sigcache[cache_slot].macsrc, 6);
2751
2752 sig = (rx_quality >> 8) & 0xFF;
2753 noise = rx_quality & 0xFF;
2754 sc->wi_sigcache[cache_slot].signal = sig - 149;
2755 sc->wi_sigcache[cache_slot].noise = noise - 149;
2756 sc->wi_sigcache[cache_slot].quality = sig - noise;
2757
2758 return;
2759}
2760#endif
2761
2762static int
2763wi_get_cur_ssid(sc, ssid, len)
2764 struct wi_softc *sc;
2765 char *ssid;
2766 int *len;
2767{
2768 int error = 0;
2769 struct wi_req wreq;
2770
2771 wreq.wi_len = WI_MAX_DATALEN;
2772 switch (sc->wi_ptype) {
2773 case WI_PORTTYPE_AP:
2774 *len = IEEE80211_NWID_LEN;
2775 bcopy(sc->wi_net_name, ssid, IEEE80211_NWID_LEN);
2776 break;
2777 case WI_PORTTYPE_ADHOC:
2778 wreq.wi_type = WI_RID_CURRENT_SSID;
2779 error = wi_read_record(sc, (struct wi_ltv_gen *)&wreq);
2780 if (error != 0)
2781 break;
2782 if (wreq.wi_val[0] > IEEE80211_NWID_LEN) {
2783 error = EINVAL;
2784 break;
2785 }
2786 *len = wreq.wi_val[0];
2787 bcopy(&wreq.wi_val[1], ssid, IEEE80211_NWID_LEN);
2788 break;
2789 case WI_PORTTYPE_BSS:
2790 wreq.wi_type = WI_RID_COMMQUAL;
2791 error = wi_read_record(sc, (struct wi_ltv_gen *)&wreq);
2792 if (error != 0)
2793 break;
2794 if (wreq.wi_val[0] != 0) /* associated */ {
2795 wreq.wi_type = WI_RID_CURRENT_SSID;
2796 wreq.wi_len = WI_MAX_DATALEN;
2797 error = wi_read_record(sc, (struct wi_ltv_gen *)&wreq);
2798 if (error != 0)
2799 break;
2800 if (wreq.wi_val[0] > IEEE80211_NWID_LEN) {
2801 error = EINVAL;
2802 break;
2803 }
2804 *len = wreq.wi_val[0];
2805 bcopy(&wreq.wi_val[1], ssid, IEEE80211_NWID_LEN);
2806 } else {
2807 *len = IEEE80211_NWID_LEN;
2808 bcopy(sc->wi_net_name, ssid, IEEE80211_NWID_LEN);
2809 }
2810 break;
2811 default:
2812 error = EINVAL;
2813 break;
2814 }
2815
2816 return error;
2817}
2818
2819static int
2820wi_media_change(ifp)
2821 struct ifnet *ifp;
2822{
2823 struct wi_softc *sc = ifp->if_softc;
2824 int otype = sc->wi_ptype;
2825 int orate = sc->wi_tx_rate;
2826 int ocreate_ibss = sc->wi_create_ibss;
2827
2828 if ((sc->ifmedia.ifm_cur->ifm_media & IFM_IEEE80211_HOSTAP) &&
2829 sc->sc_firmware_type != WI_INTERSIL)
2830 return (EINVAL);
2831
2832 sc->wi_create_ibss = 0;
2833
2834 switch (sc->ifmedia.ifm_cur->ifm_media & IFM_OMASK) {
2835 case 0:
2836 sc->wi_ptype = WI_PORTTYPE_BSS;
2837 break;
2838 case IFM_IEEE80211_ADHOC:
2839 sc->wi_ptype = WI_PORTTYPE_ADHOC;
2840 break;
2841 case IFM_IEEE80211_HOSTAP:
2842 sc->wi_ptype = WI_PORTTYPE_AP;
2843 break;
2844 case IFM_IEEE80211_IBSSMASTER:
2845 case IFM_IEEE80211_IBSSMASTER|IFM_IEEE80211_IBSS:
2846 if (!(sc->wi_flags & WI_FLAGS_HAS_CREATE_IBSS))
2847 return (EINVAL);
2848 sc->wi_create_ibss = 1;
2849 /* FALLTHROUGH */
2850 case IFM_IEEE80211_IBSS:
2851 sc->wi_ptype = WI_PORTTYPE_IBSS;
2852 break;
2853 default:
2854 /* Invalid combination. */
2855 return (EINVAL);
2856 }
2857
2858 switch (IFM_SUBTYPE(sc->ifmedia.ifm_cur->ifm_media)) {
2859 case IFM_IEEE80211_DS1:
2860 sc->wi_tx_rate = 1;
2861 break;
2862 case IFM_IEEE80211_DS2:
2863 sc->wi_tx_rate = 2;
2864 break;
2865 case IFM_IEEE80211_DS5:
2866 sc->wi_tx_rate = 5;
2867 break;
2868 case IFM_IEEE80211_DS11:
2869 sc->wi_tx_rate = 11;
2870 break;
2871 case IFM_AUTO:
2872 sc->wi_tx_rate = 3;
2873 break;
2874 }
2875
2876 if (ocreate_ibss != sc->wi_create_ibss || otype != sc->wi_ptype ||
2877 orate != sc->wi_tx_rate)
2878 wi_init(sc);
2879
2880 return(0);
2881}
2882
2883static void
2884wi_media_status(ifp, imr)
2885 struct ifnet *ifp;
2886 struct ifmediareq *imr;
2887{
2888 struct wi_req wreq;
2889 struct wi_softc *sc = ifp->if_softc;
2890
2891 if (sc->wi_tx_rate == 3) {
2892 imr->ifm_active = IFM_IEEE80211|IFM_AUTO;
2893 if (sc->wi_ptype == WI_PORTTYPE_ADHOC)
2894 imr->ifm_active |= IFM_IEEE80211_ADHOC;
2895 else if (sc->wi_ptype == WI_PORTTYPE_AP)
2896 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
2897 else if (sc->wi_ptype == WI_PORTTYPE_IBSS) {
2898 if (sc->wi_create_ibss)
2899 imr->ifm_active |= IFM_IEEE80211_IBSSMASTER;
2900 else
2901 imr->ifm_active |= IFM_IEEE80211_IBSS;
2902 }
2903 wreq.wi_type = WI_RID_CUR_TX_RATE;
2904 wreq.wi_len = WI_MAX_DATALEN;
2905 if (wi_read_record(sc, (struct wi_ltv_gen *)&wreq) == 0) {
2906 switch(wreq.wi_val[0]) {
2907 case 1:
2908 imr->ifm_active |= IFM_IEEE80211_DS1;
2909 break;
2910 case 2:
2911 imr->ifm_active |= IFM_IEEE80211_DS2;
2912 break;
2913 case 6:
2914 imr->ifm_active |= IFM_IEEE80211_DS5;
2915 break;
2916 case 11:
2917 imr->ifm_active |= IFM_IEEE80211_DS11;
2918 break;
2919 }
2920 }
2921 } else {
2922 imr->ifm_active = sc->ifmedia.ifm_cur->ifm_media;
2923 }
2924
2925 imr->ifm_status = IFM_AVALID;
2926 if (sc->wi_ptype == WI_PORTTYPE_ADHOC ||
2927 sc->wi_ptype == WI_PORTTYPE_IBSS)
2928 /*
2929 * XXX: It would be nice if we could give some actually
2930 * useful status like whether we joined another IBSS or
2931 * created one ourselves.
2932 */
2933 imr->ifm_status |= IFM_ACTIVE;
2934 else if (sc->wi_ptype == WI_PORTTYPE_AP)
2935 imr->ifm_status |= IFM_ACTIVE;
2936 else {
2937 wreq.wi_type = WI_RID_COMMQUAL;
2938 wreq.wi_len = WI_MAX_DATALEN;
2939 if (wi_read_record(sc, (struct wi_ltv_gen *)&wreq) == 0 &&
2940 wreq.wi_val[0] != 0)
2941 imr->ifm_status |= IFM_ACTIVE;
2942 }
2943}
2944
2945static int
2946wi_get_debug(sc, wreq)
2947 struct wi_softc *sc;
2948 struct wi_req *wreq;
2949{
2950 int error = 0;
2951
2952 wreq->wi_len = 1;
2953
2954 switch (wreq->wi_type) {
2955 case WI_DEBUG_SLEEP:
2956 wreq->wi_len++;
2957 wreq->wi_val[0] = sc->wi_debug.wi_sleep;
2958 break;
2959 case WI_DEBUG_DELAYSUPP:
2960 wreq->wi_len++;
2961 wreq->wi_val[0] = sc->wi_debug.wi_delaysupp;
2962 break;
2963 case WI_DEBUG_TXSUPP:
2964 wreq->wi_len++;
2965 wreq->wi_val[0] = sc->wi_debug.wi_txsupp;
2966 break;
2967 case WI_DEBUG_MONITOR:
2968 wreq->wi_len++;
2969 wreq->wi_val[0] = sc->wi_debug.wi_monitor;
2970 break;
2971 case WI_DEBUG_LEDTEST:
2972 wreq->wi_len += 3;
2973 wreq->wi_val[0] = sc->wi_debug.wi_ledtest;
2974 wreq->wi_val[1] = sc->wi_debug.wi_ledtest_param0;
2975 wreq->wi_val[2] = sc->wi_debug.wi_ledtest_param1;
2976 break;
2977 case WI_DEBUG_CONTTX:
2978 wreq->wi_len += 2;
2979 wreq->wi_val[0] = sc->wi_debug.wi_conttx;
2980 wreq->wi_val[1] = sc->wi_debug.wi_conttx_param0;
2981 break;
2982 case WI_DEBUG_CONTRX:
2983 wreq->wi_len++;
2984 wreq->wi_val[0] = sc->wi_debug.wi_contrx;
2985 break;
2986 case WI_DEBUG_SIGSTATE:
2987 wreq->wi_len += 2;
2988 wreq->wi_val[0] = sc->wi_debug.wi_sigstate;
2989 wreq->wi_val[1] = sc->wi_debug.wi_sigstate_param0;
2990 break;
2991 case WI_DEBUG_CONFBITS:
2992 wreq->wi_len += 2;
2993 wreq->wi_val[0] = sc->wi_debug.wi_confbits;
2994 wreq->wi_val[1] = sc->wi_debug.wi_confbits_param0;
2995 break;
2996 default:
2997 error = EIO;
2998 break;
2999 }
3000
3001 return (error);
3002}
3003
3004static int
3005wi_set_debug(sc, wreq)
3006 struct wi_softc *sc;
3007 struct wi_req *wreq;
3008{
3009 int error = 0;
3010 u_int16_t cmd, param0 = 0, param1 = 0;
3011
3012 switch (wreq->wi_type) {
3013 case WI_DEBUG_RESET:
3014 case WI_DEBUG_INIT:
3015 case WI_DEBUG_CALENABLE:
3016 break;
3017 case WI_DEBUG_SLEEP:
3018 sc->wi_debug.wi_sleep = 1;
3019 break;
3020 case WI_DEBUG_WAKE:
3021 sc->wi_debug.wi_sleep = 0;
3022 break;
3023 case WI_DEBUG_CHAN:
3024 param0 = wreq->wi_val[0];
3025 break;
3026 case WI_DEBUG_DELAYSUPP:
3027 sc->wi_debug.wi_delaysupp = 1;
3028 break;
3029 case WI_DEBUG_TXSUPP:
3030 sc->wi_debug.wi_txsupp = 1;
3031 break;
3032 case WI_DEBUG_MONITOR:
3033 sc->wi_debug.wi_monitor = 1;
3034 break;
3035 case WI_DEBUG_LEDTEST:
3036 param0 = wreq->wi_val[0];
3037 param1 = wreq->wi_val[1];
3038 sc->wi_debug.wi_ledtest = 1;
3039 sc->wi_debug.wi_ledtest_param0 = param0;
3040 sc->wi_debug.wi_ledtest_param1 = param1;
3041 break;
3042 case WI_DEBUG_CONTTX:
3043 param0 = wreq->wi_val[0];
3044 sc->wi_debug.wi_conttx = 1;
3045 sc->wi_debug.wi_conttx_param0 = param0;
3046 break;
3047 case WI_DEBUG_STOPTEST:
3048 sc->wi_debug.wi_delaysupp = 0;
3049 sc->wi_debug.wi_txsupp = 0;
3050 sc->wi_debug.wi_monitor = 0;
3051 sc->wi_debug.wi_ledtest = 0;
3052 sc->wi_debug.wi_ledtest_param0 = 0;
3053 sc->wi_debug.wi_ledtest_param1 = 0;
3054 sc->wi_debug.wi_conttx = 0;
3055 sc->wi_debug.wi_conttx_param0 = 0;
3056 sc->wi_debug.wi_contrx = 0;
3057 sc->wi_debug.wi_sigstate = 0;
3058 sc->wi_debug.wi_sigstate_param0 = 0;
3059 break;
3060 case WI_DEBUG_CONTRX:
3061 sc->wi_debug.wi_contrx = 1;
3062 break;
3063 case WI_DEBUG_SIGSTATE:
3064 param0 = wreq->wi_val[0];
3065 sc->wi_debug.wi_sigstate = 1;
3066 sc->wi_debug.wi_sigstate_param0 = param0;
3067 break;
3068 case WI_DEBUG_CONFBITS:
3069 param0 = wreq->wi_val[0];
3070 param1 = wreq->wi_val[1];
3071 sc->wi_debug.wi_confbits = param0;
3072 sc->wi_debug.wi_confbits_param0 = param1;
3073 break;
3074 default:
3075 error = EIO;
3076 break;
3077 }
3078
3079 if (error)
3080 return (error);
3081
3082 cmd = WI_CMD_DEBUG | (wreq->wi_type << 8);
3083 error = wi_cmd(sc, cmd, param0, param1, 0);
3084
3085 return (error);
3086}