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