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