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