wi.c revision 1.17.2.12 1 /* $NetBSD: wi.c,v 1.17.2.12 2002/04/17 00:05:53 nathanw Exp $ */
2
3 /*
4 * Copyright (c) 1997, 1998, 1999
5 * Bill Paul <wpaul (at) 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
35 /*
36 * Lucent WaveLAN/IEEE 802.11 PCMCIA driver for NetBSD.
37 *
38 * Original FreeBSD driver written by Bill Paul <wpaul (at) ctr.columbia.edu>
39 * Electrical Engineering Department
40 * Columbia University, New York City
41 */
42
43 /*
44 * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
45 * from Lucent. Unlike the older cards, the new ones are programmed
46 * entirely via a firmware-driven controller called the Hermes.
47 * Unfortunately, Lucent will not release the Hermes programming manual
48 * without an NDA (if at all). What they do release is an API library
49 * called the HCF (Hardware Control Functions) which is supposed to
50 * do the device-specific operations of a device driver for you. The
51 * publically available version of the HCF library (the 'HCF Light') is
52 * a) extremely gross, b) lacks certain features, particularly support
53 * for 802.11 frames, and c) is contaminated by the GNU Public License.
54 *
55 * This driver does not use the HCF or HCF Light at all. Instead, it
56 * programs the Hermes controller directly, using information gleaned
57 * from the HCF Light code and corresponding documentation.
58 *
59 * This driver supports both the PCMCIA and ISA versions of the
60 * WaveLAN/IEEE cards. Note however that the ISA card isn't really
61 * anything of the sort: it's actually a PCMCIA bridge adapter
62 * that fits into an ISA slot, into which a PCMCIA WaveLAN card is
63 * inserted. Consequently, you need to use the pccard support for
64 * both the ISA and PCMCIA adapters.
65 */
66
67 /*
68 * FreeBSD driver ported to NetBSD by Bill Sommerfeld in the back of the
69 * Oslo IETF plenary meeting.
70 */
71
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: wi.c,v 1.17.2.12 2002/04/17 00:05:53 nathanw Exp $");
74
75 #define WI_HERMES_AUTOINC_WAR /* Work around data write autoinc bug. */
76 #define WI_HERMES_STATS_WAR /* Work around stats counter bug. */
77
78 #include "bpfilter.h"
79
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/callout.h>
83 #include <sys/device.h>
84 #include <sys/socket.h>
85 #include <sys/mbuf.h>
86 #include <sys/ioctl.h>
87 #include <sys/kernel.h> /* for hz */
88 #include <sys/lwp.h>
89 #include <sys/proc.h>
90
91 #include <net/if.h>
92 #include <net/if_dl.h>
93 #include <net/if_media.h>
94 #include <net/if_ether.h>
95 #include <net/if_ieee80211.h>
96
97 #if NBPFILTER > 0
98 #include <net/bpf.h>
99 #include <net/bpfdesc.h>
100 #endif
101
102 #include <machine/bus.h>
103
104 #include <dev/ic/wi_ieee.h>
105 #include <dev/ic/wireg.h>
106 #include <dev/ic/wivar.h>
107
108 static void wi_reset __P((struct wi_softc *));
109 static int wi_ioctl __P((struct ifnet *, u_long, caddr_t));
110 static void wi_start __P((struct ifnet *));
111 static void wi_watchdog __P((struct ifnet *));
112 static int wi_init __P((struct ifnet *));
113 static void wi_stop __P((struct ifnet *, int));
114 static void wi_rxeof __P((struct wi_softc *));
115 static void wi_txeof __P((struct wi_softc *, int));
116 static void wi_update_stats __P((struct wi_softc *));
117 static void wi_setmulti __P((struct wi_softc *));
118
119 static int wi_cmd __P((struct wi_softc *, int, int));
120 static int wi_read_record __P((struct wi_softc *, struct wi_ltv_gen *));
121 static int wi_write_record __P((struct wi_softc *, struct wi_ltv_gen *));
122 static int wi_read_data __P((struct wi_softc *, int,
123 int, caddr_t, int));
124 static int wi_write_data __P((struct wi_softc *, int,
125 int, caddr_t, int));
126 static int wi_seek __P((struct wi_softc *, int, int, int));
127 static int wi_alloc_nicmem __P((struct wi_softc *, int, int *));
128 static void wi_inquire __P((void *));
129 static void wi_wait_scan __P((void *));
130 static int wi_setdef __P((struct wi_softc *, struct wi_req *));
131 static int wi_getdef __P((struct wi_softc *, struct wi_req *));
132 static int wi_mgmt_xmit __P((struct wi_softc *, caddr_t, int));
133
134 static int wi_media_change __P((struct ifnet *));
135 static void wi_media_status __P((struct ifnet *, struct ifmediareq *));
136
137 static void wi_get_id __P((struct wi_softc *));
138
139 static int wi_set_ssid __P((struct ieee80211_nwid *, u_int8_t *, int));
140 static void wi_request_fill_ssid __P((struct wi_req *,
141 struct ieee80211_nwid *));
142 static int wi_write_ssid __P((struct wi_softc *, int, struct wi_req *,
143 struct ieee80211_nwid *));
144 static int wi_set_nwkey __P((struct wi_softc *, struct ieee80211_nwkey *));
145 static int wi_get_nwkey __P((struct wi_softc *, struct ieee80211_nwkey *));
146 static int wi_sync_media __P((struct wi_softc *, int, int));
147 static int wi_set_pm(struct wi_softc *, struct ieee80211_power *);
148 static int wi_get_pm(struct wi_softc *, struct ieee80211_power *);
149
150 struct wi_card_ident wi_card_ident[] = {
151 /* CARD_ID CARD_NAME FIRM_TYPE */
152 { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT },
153 { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT },
154 { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT },
155 { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL },
156 { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL },
157 { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL },
158 { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL },
159 { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL },
160 { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL },
161 { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL },
162 { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL },
163 { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL },
164 { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
165 { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
166 { WI_NIC_3842_PCMCIA_ATM_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
167 { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
168 { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
169 { WI_NIC_3842_MINI_ATM_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
170 { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
171 { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
172 { WI_NIC_3842_PCI_ATM_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
173 { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
174 { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
175 { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
176 { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
177 { 0, NULL, 0 },
178 };
179
180 int
181 wi_attach(sc)
182 struct wi_softc *sc;
183 {
184 struct ifnet *ifp = sc->sc_ifp;
185 struct wi_ltv_macaddr mac;
186 struct wi_ltv_gen gen;
187 static const u_int8_t empty_macaddr[ETHER_ADDR_LEN] = {
188 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
189 };
190 int s;
191
192 s = splnet();
193
194 callout_init(&sc->wi_inquire_ch);
195 callout_init(&sc->wi_scan_sh);
196
197 /* Make sure interrupts are disabled. */
198 CSR_WRITE_2(sc, WI_INT_EN, 0);
199 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
200
201 /* Reset the NIC. */
202 wi_reset(sc);
203
204 memset(&mac, 0, sizeof(mac));
205 /* Read the station address. */
206 mac.wi_type = WI_RID_MAC_NODE;
207 mac.wi_len = 4;
208 wi_read_record(sc, (struct wi_ltv_gen *)&mac);
209 memcpy(sc->sc_macaddr, mac.wi_mac_addr, ETHER_ADDR_LEN);
210
211 /*
212 * Check if we got anything meaningful.
213 *
214 * Is it really enough just checking against null ethernet address?
215 * Or, check against possible vendor? XXX.
216 */
217 if (memcmp(sc->sc_macaddr, empty_macaddr, ETHER_ADDR_LEN) == 0) {
218 printf("could not get mac address, attach failed\n");
219 splx(s);
220 return 1;
221 }
222
223 printf(" 802.11 address %s\n", ether_sprintf(sc->sc_macaddr));
224
225 /* Read NIC identification */
226 wi_get_id(sc);
227
228 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
229 ifp->if_softc = sc;
230 ifp->if_start = wi_start;
231 ifp->if_ioctl = wi_ioctl;
232 ifp->if_watchdog = wi_watchdog;
233 ifp->if_init = wi_init;
234 ifp->if_stop = wi_stop;
235 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
236 #ifdef IFF_NOTRAILERS
237 ifp->if_flags |= IFF_NOTRAILERS;
238 #endif
239 IFQ_SET_READY(&ifp->if_snd);
240
241 (void)wi_set_ssid(&sc->wi_nodeid, WI_DEFAULT_NODENAME,
242 sizeof(WI_DEFAULT_NODENAME) - 1);
243 (void)wi_set_ssid(&sc->wi_netid, WI_DEFAULT_NETNAME,
244 sizeof(WI_DEFAULT_NETNAME) - 1);
245 (void)wi_set_ssid(&sc->wi_ibssid, WI_DEFAULT_IBSS,
246 sizeof(WI_DEFAULT_IBSS) - 1);
247
248 sc->wi_portnum = WI_DEFAULT_PORT;
249 sc->wi_ptype = WI_PORTTYPE_BSS;
250 sc->wi_ap_density = WI_DEFAULT_AP_DENSITY;
251 sc->wi_rts_thresh = WI_DEFAULT_RTS_THRESH;
252 sc->wi_tx_rate = WI_DEFAULT_TX_RATE;
253 sc->wi_max_data_len = WI_DEFAULT_DATALEN;
254 sc->wi_create_ibss = WI_DEFAULT_CREATE_IBSS;
255 sc->wi_pm_enabled = WI_DEFAULT_PM_ENABLED;
256 sc->wi_max_sleep = WI_DEFAULT_MAX_SLEEP;
257 sc->wi_roaming = WI_DEFAULT_ROAMING;
258 sc->wi_authtype = WI_DEFAULT_AUTHTYPE;
259
260 /*
261 * Read the default channel from the NIC. This may vary
262 * depending on the country where the NIC was purchased, so
263 * we can't hard-code a default and expect it to work for
264 * everyone.
265 */
266 gen.wi_type = WI_RID_OWN_CHNL;
267 gen.wi_len = 2;
268 wi_read_record(sc, &gen);
269 sc->wi_channel = le16toh(gen.wi_val);
270
271 memset((char *)&sc->wi_stats, 0, sizeof(sc->wi_stats));
272
273 /* AP info was filled with 0 */
274 memset((char *)&sc->wi_aps, 0, sizeof(sc->wi_aps));
275 sc->wi_scanning=0;
276 sc->wi_naps=0;
277
278 /*
279 * Find out if we support WEP on this card.
280 */
281 gen.wi_type = WI_RID_WEP_AVAIL;
282 gen.wi_len = 2;
283 wi_read_record(sc, &gen);
284 sc->wi_has_wep = le16toh(gen.wi_val);
285
286 ifmedia_init(&sc->sc_media, 0, wi_media_change, wi_media_status);
287 #define ADD(m, c) ifmedia_add(&sc->sc_media, (m), (c), NULL)
288 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO, 0, 0), 0);
289 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO, IFM_IEEE80211_ADHOC, 0), 0);
290 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS1, 0, 0), 0);
291 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS1,
292 IFM_IEEE80211_ADHOC, 0), 0);
293 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS2, 0, 0), 0);
294 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS2,
295 IFM_IEEE80211_ADHOC, 0), 0);
296 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS5, 0, 0), 0);
297 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS5,
298 IFM_IEEE80211_ADHOC, 0), 0);
299 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS11, 0, 0), 0);
300 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS11,
301 IFM_IEEE80211_ADHOC, 0), 0);
302 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_MANUAL, 0, 0), 0);
303 #undef ADD
304 ifmedia_set(&sc->sc_media, IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO, 0, 0));
305
306 /*
307 * Call MI attach routines.
308 */
309 if_attach(ifp);
310 ether_ifattach(ifp, mac.wi_mac_addr);
311
312 ifp->if_baudrate = IF_Mbps(2);
313
314 /* Attach is successful. */
315 sc->sc_attached = 1;
316
317 splx(s);
318 return 0;
319 }
320
321 static void wi_rxeof(sc)
322 struct wi_softc *sc;
323 {
324 struct ifnet *ifp;
325 struct ether_header *eh;
326 struct wi_frame rx_frame;
327 struct mbuf *m;
328 int id;
329
330 ifp = sc->sc_ifp;
331
332 id = CSR_READ_2(sc, WI_RX_FID);
333
334 /* First read in the frame header */
335 if (wi_read_data(sc, id, 0, (caddr_t)&rx_frame, sizeof(rx_frame))) {
336 ifp->if_ierrors++;
337 return;
338 }
339
340 /*
341 * Drop undecryptable or packets with receive errors here
342 */
343 if (le16toh(rx_frame.wi_status) & WI_STAT_ERRSTAT) {
344 ifp->if_ierrors++;
345 return;
346 }
347
348 MGETHDR(m, M_DONTWAIT, MT_DATA);
349 if (m == NULL) {
350 ifp->if_ierrors++;
351 return;
352 }
353 MCLGET(m, M_DONTWAIT);
354 if (!(m->m_flags & M_EXT)) {
355 m_freem(m);
356 ifp->if_ierrors++;
357 return;
358 }
359
360 /* Align the data after the ethernet header */
361 m->m_data = (caddr_t) ALIGN(m->m_data + sizeof(struct ether_header))
362 - sizeof(struct ether_header);
363
364 eh = mtod(m, struct ether_header *);
365 m->m_pkthdr.rcvif = ifp;
366
367 if (le16toh(rx_frame.wi_status) == WI_STAT_1042 ||
368 le16toh(rx_frame.wi_status) == WI_STAT_TUNNEL ||
369 le16toh(rx_frame.wi_status) == WI_STAT_WMP_MSG) {
370 if ((le16toh(rx_frame.wi_dat_len) + WI_SNAPHDR_LEN) > MCLBYTES) {
371 printf("%s: oversized packet received "
372 "(wi_dat_len=%d, wi_status=0x%x)\n",
373 sc->sc_dev.dv_xname,
374 le16toh(rx_frame.wi_dat_len), le16toh(rx_frame.wi_status));
375 m_freem(m);
376 ifp->if_ierrors++;
377 return;
378 }
379 m->m_pkthdr.len = m->m_len =
380 le16toh(rx_frame.wi_dat_len) + WI_SNAPHDR_LEN;
381
382 memcpy((char *)&eh->ether_dhost, (char *)&rx_frame.wi_dst_addr,
383 ETHER_ADDR_LEN);
384 memcpy((char *)&eh->ether_shost, (char *)&rx_frame.wi_src_addr,
385 ETHER_ADDR_LEN);
386 memcpy((char *)&eh->ether_type, (char *)&rx_frame.wi_type,
387 sizeof(u_int16_t));
388
389 if (wi_read_data(sc, id, WI_802_11_OFFSET,
390 mtod(m, caddr_t) + sizeof(struct ether_header),
391 m->m_len + 2)) {
392 m_freem(m);
393 ifp->if_ierrors++;
394 return;
395 }
396 } else {
397 if ((le16toh(rx_frame.wi_dat_len) +
398 sizeof(struct ether_header)) > MCLBYTES) {
399 printf("%s: oversized packet received "
400 "(wi_dat_len=%d, wi_status=0x%x)\n",
401 sc->sc_dev.dv_xname,
402 le16toh(rx_frame.wi_dat_len), le16toh(rx_frame.wi_status));
403 m_freem(m);
404 ifp->if_ierrors++;
405 return;
406 }
407 m->m_pkthdr.len = m->m_len =
408 le16toh(rx_frame.wi_dat_len) + sizeof(struct ether_header);
409
410 if (wi_read_data(sc, id, WI_802_3_OFFSET,
411 mtod(m, caddr_t), m->m_len + 2)) {
412 m_freem(m);
413 ifp->if_ierrors++;
414 return;
415 }
416 }
417
418 ifp->if_ipackets++;
419
420 #if NBPFILTER > 0
421 /* Handle BPF listeners. */
422 if (ifp->if_bpf)
423 bpf_mtap(ifp->if_bpf, m);
424 #endif
425
426 /* Receive packet. */
427 (*ifp->if_input)(ifp, m);
428 }
429
430 static void wi_txeof(sc, status)
431 struct wi_softc *sc;
432 int status;
433 {
434 struct ifnet *ifp = sc->sc_ifp;
435
436 ifp->if_timer = 0;
437 ifp->if_flags &= ~IFF_OACTIVE;
438
439 if (status & WI_EV_TX_EXC)
440 ifp->if_oerrors++;
441 else
442 ifp->if_opackets++;
443
444 return;
445 }
446
447 void wi_inquire(xsc)
448 void *xsc;
449 {
450 struct wi_softc *sc;
451 struct ifnet *ifp;
452 int s;
453
454 sc = xsc;
455 ifp = &sc->sc_ethercom.ec_if;
456
457 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
458 return;
459
460 KASSERT(sc->sc_enabled);
461
462 callout_reset(&sc->wi_inquire_ch, hz * 60, wi_inquire, sc);
463
464 /* Don't do this while we're transmitting */
465 if (ifp->if_flags & IFF_OACTIVE)
466 return;
467
468 s = splnet();
469 wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_COUNTERS);
470 splx(s);
471 }
472
473 void wi_wait_scan(xsc)
474 void *xsc;
475 {
476 struct wi_softc *sc;
477 struct ifnet *ifp;
478 int s, result;
479
480 sc = xsc;
481 ifp = &sc->sc_ethercom.ec_if;
482
483 /* If not scanning, ignore */
484 if (!sc->wi_scanning)
485 return;
486
487 s = splnet();
488
489 /* Wait for sending complete to make INQUIRE */
490 if (ifp->if_flags & IFF_OACTIVE) {
491 callout_reset(&sc->wi_scan_sh, hz * 1, wi_wait_scan, sc);
492 splx(s);
493 return;
494 }
495
496 /* try INQUIRE */
497 result = wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS);
498 if (result == ETIMEDOUT)
499 callout_reset(&sc->wi_scan_sh, hz * 1, wi_wait_scan, sc);
500
501 splx(s);
502 }
503
504 void wi_update_stats(sc)
505 struct wi_softc *sc;
506 {
507 struct wi_ltv_gen gen;
508 struct wi_scan_header ap2_header; /* Prism2 header */
509 struct wi_scan_data_p2 ap2; /* Prism2 scantable*/
510 struct wi_scan_data ap; /* Lucent scantable */
511 struct wi_assoc assoc; /* Association Status */
512 u_int16_t id;
513 struct ifnet *ifp;
514 u_int32_t *ptr;
515 int len, naps, i, j;
516 u_int16_t t;
517
518 ifp = &sc->sc_ethercom.ec_if;
519
520 id = CSR_READ_2(sc, WI_INFO_FID);
521
522 if (wi_seek(sc, id, 0, WI_BAP1)) {
523 return;
524 }
525
526 gen.wi_len = CSR_READ_2(sc, WI_DATA1);
527 gen.wi_type = CSR_READ_2(sc, WI_DATA1);
528
529 switch (gen.wi_type) {
530 case WI_INFO_SCAN_RESULTS:
531 case WI_INFO_HOST_SCAN_RESULTS:
532 if (gen.wi_len <= 3) {
533 sc->wi_naps = 0;
534 sc->wi_scanning = 0;
535 break;
536 }
537 switch (sc->sc_firmware_type) {
538 case WI_INTERSIL:
539 case WI_SYMBOL:
540 if (sc->sc_firmware_type == WI_INTERSIL) {
541 naps = 2 * (gen.wi_len - 3) / sizeof(ap2);
542 /* Read Header */
543 for(j=0; j < sizeof(ap2_header) / 2; j++)
544 ((u_int16_t *)&ap2_header)[j] =
545 CSR_READ_2(sc, WI_DATA1);
546 } else { /* WI_SYMBOL */
547 naps = 2 * (gen.wi_len - 1) / (sizeof(ap2) + 6);
548 ap2_header.wi_reason = 0;
549 }
550 naps = naps > MAXAPINFO ? MAXAPINFO : naps;
551 sc->wi_naps = naps;
552 /* Read Data */
553 for (i=0; i < naps; i++) {
554 for(j=0; j < sizeof(ap2) / 2; j++)
555 ((u_int16_t *)&ap2)[j] =
556 CSR_READ_2(sc, WI_DATA1);
557 if (sc->sc_firmware_type == WI_SYMBOL) {
558 /* 3 more words */
559 for (j = 0; j < 3; j++)
560 CSR_READ_2(sc, WI_DATA1);
561 }
562 /* unswap 8 bit data fields: */
563 for(j=0;j<sizeof(ap.wi_bssid)/2;j++)
564 LE16TOH(((u_int16_t *)&ap.wi_bssid[0])[j]);
565 for(j=0;j<sizeof(ap.wi_name)/2;j++)
566 LE16TOH(((u_int16_t *)&ap.wi_name[0])[j]);
567 sc->wi_aps[i].scanreason = ap2_header.wi_reason;
568 memcpy(sc->wi_aps[i].bssid, ap2.wi_bssid, 6);
569 sc->wi_aps[i].channel = ap2.wi_chid;
570 sc->wi_aps[i].signal = ap2.wi_signal;
571 sc->wi_aps[i].noise = ap2.wi_noise;
572 sc->wi_aps[i].quality = ap2.wi_signal - ap2.wi_noise;
573 sc->wi_aps[i].capinfo = ap2.wi_capinfo;
574 sc->wi_aps[i].interval = ap2.wi_interval;
575 sc->wi_aps[i].rate = ap2.wi_rate;
576 if (ap2.wi_namelen > 32)
577 ap2.wi_namelen = 32;
578 sc->wi_aps[i].namelen = ap2.wi_namelen;
579 memcpy(sc->wi_aps[i].name, ap2.wi_name,
580 ap2.wi_namelen);
581 }
582 break;
583
584 case WI_LUCENT:
585 naps = 2 * gen.wi_len / sizeof(ap);
586 naps = naps > MAXAPINFO ? MAXAPINFO : naps;
587 sc->wi_naps = naps;
588 /* Read Data*/
589 for (i=0; i < naps; i++) {
590 for(j=0; j < sizeof(ap) / 2; j++)
591 ((u_int16_t *)&ap)[j] =
592 CSR_READ_2(sc, WI_DATA1);
593 /* unswap 8 bit data fields: */
594 for(j=0;j<sizeof(ap.wi_bssid)/2;j++)
595 HTOLE16(((u_int16_t *)&ap.wi_bssid[0])[j]);
596 for(j=0;j<sizeof(ap.wi_name)/2;j++)
597 HTOLE16(((u_int16_t *)&ap.wi_name[0])[j]);
598 memcpy(sc->wi_aps[i].bssid, ap.wi_bssid, 6);
599 sc->wi_aps[i].channel = ap.wi_chid;
600 sc->wi_aps[i].signal = ap.wi_signal;
601 sc->wi_aps[i].noise = ap.wi_noise;
602 sc->wi_aps[i].quality = ap.wi_signal - ap.wi_noise;
603 sc->wi_aps[i].capinfo = ap.wi_capinfo;
604 sc->wi_aps[i].interval = ap.wi_interval;
605 if (ap.wi_namelen > 32)
606 ap.wi_namelen = 32;
607 sc->wi_aps[i].namelen = ap.wi_namelen;
608 memcpy(sc->wi_aps[i].name, ap.wi_name,
609 ap.wi_namelen);
610 }
611 break;
612 }
613 /* Done scanning */
614 sc->wi_scanning = 0;
615 break;
616
617 case WI_INFO_COUNTERS:
618 /* some card versions have a larger stats structure */
619 len = (gen.wi_len - 1 < sizeof(sc->wi_stats) / 4) ?
620 gen.wi_len - 1 : sizeof(sc->wi_stats) / 4;
621 ptr = (u_int32_t *)&sc->wi_stats;
622
623 for (i = 0; i < len; i++) {
624 t = CSR_READ_2(sc, WI_DATA1);
625 #ifdef WI_HERMES_STATS_WAR
626 if (t > 0xF000)
627 t = ~t & 0xFFFF;
628 #endif
629 ptr[i] += t;
630 }
631
632 ifp->if_collisions = sc->wi_stats.wi_tx_single_retries +
633 sc->wi_stats.wi_tx_multi_retries +
634 sc->wi_stats.wi_tx_retry_limit;
635 break;
636
637 case WI_INFO_LINK_STAT: {
638 static char *msg[] = {
639 "connected",
640 "disconnected",
641 "AP change",
642 "AP out of range",
643 "AP in range",
644 "Association Failed"
645 };
646
647 if (gen.wi_len != 2) {
648 #ifdef WI_DEBUG
649 printf("WI_INFO_LINK_STAT: len=%d\n", gen.wi_len);
650 #endif
651 break;
652 }
653 t = CSR_READ_2(sc, WI_DATA1);
654 if ((t < 1) || (t > 6)) {
655 #ifdef WI_DEBUG
656 printf("WI_INFO_LINK_STAT: status %d\n", t);
657 #endif
658 break;
659 }
660 if (sc->sc_firmware_type == WI_SYMBOL && t == 4) {
661 wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_HOST_SCAN_RESULTS);
662 break;
663 }
664 /*
665 * Some cards issue streams of "connected" messages while
666 * trying to find a peer. Don't bother the user with this
667 * unless he is debugging.
668 */
669 if (ifp->if_flags & IFF_DEBUG)
670 printf("%s: %s\n", sc->sc_dev.dv_xname, msg[t - 1]);
671 break;
672 }
673
674 case WI_INFO_ASSOC_STAT: {
675 static char *msg[] = {
676 "STA Associated",
677 "STA Reassociated",
678 "STA Disassociated",
679 "Association Failure",
680 "Authentication Failed"
681 };
682 if (gen.wi_len != 10)
683 break;
684 for (i=0; i < gen.wi_len - 1; i++)
685 ((u_int16_t *)&assoc)[i] = CSR_READ_2(sc, WI_DATA1);
686 /* unswap 8 bit data fields: */
687 for(j=0;j<sizeof(assoc.wi_assoc_sta)/2;j++)
688 HTOLE16(((u_int16_t *)&assoc.wi_assoc_sta[0])[j]);
689 for(j=0;j<sizeof(assoc.wi_assoc_osta)/2;j++)
690 HTOLE16(((u_int16_t *)&assoc.wi_assoc_osta[0])[j]);
691 switch (assoc.wi_assoc_stat) {
692 case ASSOC:
693 case DISASSOC:
694 case ASSOCFAIL:
695 case AUTHFAIL:
696 printf("%s: %s, AP = %02x:%02x:%02x:%02x:%02x:%02x\n",
697 sc->sc_dev.dv_xname,
698 msg[assoc.wi_assoc_stat - 1],
699 assoc.wi_assoc_sta[0]&0xff, assoc.wi_assoc_sta[1]&0xff,
700 assoc.wi_assoc_sta[2]&0xff, assoc.wi_assoc_sta[3]&0xff,
701 assoc.wi_assoc_sta[4]&0xff, assoc.wi_assoc_sta[5]&0xff);
702 break;
703 case REASSOC:
704 printf("%s: %s, AP = %02x:%02x:%02x:%02x:%02x:%02x, "
705 "OldAP = %02x:%02x:%02x:%02x:%02x:%02x\n",
706 sc->sc_dev.dv_xname, msg[assoc.wi_assoc_stat - 1],
707 assoc.wi_assoc_sta[0]&0xff, assoc.wi_assoc_sta[1]&0xff,
708 assoc.wi_assoc_sta[2]&0xff, assoc.wi_assoc_sta[3]&0xff,
709 assoc.wi_assoc_sta[4]&0xff, assoc.wi_assoc_sta[5]&0xff,
710 assoc.wi_assoc_osta[0]&0xff, assoc.wi_assoc_osta[1]&0xff,
711 assoc.wi_assoc_osta[2]&0xff, assoc.wi_assoc_osta[3]&0xff,
712 assoc.wi_assoc_osta[4]&0xff, assoc.wi_assoc_osta[5]&0xff);
713 break;
714 }
715 }
716
717 default:
718 #ifdef WI_DEBUG
719 printf("%s: got info type: 0x%04x len=0x%04x\n",
720 sc->sc_dev.dv_xname, gen.wi_type,gen.wi_len);
721 #endif
722 #if 0
723 for (i = 0; i < gen.wi_len; i++) {
724 t = CSR_READ_2(sc, WI_DATA1);
725 printf("[0x%02x] = 0x%04x\n", i, t);
726 }
727 #endif
728 break;
729 }
730 }
731
732 int wi_intr(arg)
733 void *arg;
734 {
735 struct wi_softc *sc = arg;
736 struct ifnet *ifp;
737 u_int16_t status;
738
739 if (sc->sc_enabled == 0 ||
740 (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 ||
741 (sc->sc_ethercom.ec_if.if_flags & IFF_RUNNING) == 0)
742 return (0);
743
744 ifp = &sc->sc_ethercom.ec_if;
745
746 if (!(ifp->if_flags & IFF_UP)) {
747 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
748 CSR_WRITE_2(sc, WI_INT_EN, 0);
749 return 1;
750 }
751
752 /* Disable interrupts. */
753 CSR_WRITE_2(sc, WI_INT_EN, 0);
754
755 status = CSR_READ_2(sc, WI_EVENT_STAT);
756 CSR_WRITE_2(sc, WI_EVENT_ACK, ~WI_INTRS);
757
758 if (status & WI_EV_RX) {
759 wi_rxeof(sc);
760 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
761 }
762
763 if (status & WI_EV_TX) {
764 wi_txeof(sc, status);
765 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX);
766 }
767
768 if (status & WI_EV_ALLOC) {
769 int id;
770 id = CSR_READ_2(sc, WI_ALLOC_FID);
771 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
772 if (id == sc->wi_tx_data_id)
773 wi_txeof(sc, status);
774 }
775
776 if (status & WI_EV_INFO) {
777 wi_update_stats(sc);
778 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
779 }
780
781 if (status & WI_EV_TX_EXC) {
782 wi_txeof(sc, status);
783 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
784 }
785
786 if (status & WI_EV_INFO_DROP) {
787 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO_DROP);
788 }
789
790 /* Re-enable interrupts. */
791 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
792
793 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
794 wi_start(ifp);
795
796 return 1;
797 }
798
799 /* Must be called at proper protection level! */
800 static int
801 wi_cmd(sc, cmd, val)
802 struct wi_softc *sc;
803 int cmd;
804 int val;
805 {
806 int i, s = 0;
807
808 /* wait for the busy bit to clear */
809 for (i = 0; i < WI_TIMEOUT; i++) {
810 if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
811 break;
812 }
813
814 if (i == WI_TIMEOUT) {
815 printf("%s: wi_cmd: BUSY did not clear, cmd=0x%x\n",
816 sc->sc_dev.dv_xname, cmd);
817 return EIO;
818 }
819
820 CSR_WRITE_2(sc, WI_PARAM0, val);
821 CSR_WRITE_2(sc, WI_PARAM1, 0);
822 CSR_WRITE_2(sc, WI_PARAM2, 0);
823 CSR_WRITE_2(sc, WI_COMMAND, cmd);
824
825 /* wait for the cmd completed bit */
826 for (i = 0; i < WI_TIMEOUT; i++) {
827 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
828 break;
829 DELAY(1);
830 }
831
832 /* Ack the command */
833 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
834
835 s = CSR_READ_2(sc, WI_STATUS);
836 if (s & WI_STAT_CMD_RESULT)
837 return(EIO);
838
839 if (i == WI_TIMEOUT) {
840 if (!sc->wi_scanning)
841 printf("%s: command timed out, cmd=0x%x\n",
842 sc->sc_dev.dv_xname, cmd);
843 return(ETIMEDOUT);
844 }
845
846 return(0);
847 }
848
849 static void
850 wi_reset(sc)
851 struct wi_softc *sc;
852 {
853
854 DELAY(100*1000); /* 100 m sec */
855 if (wi_cmd(sc, WI_CMD_INI, 0))
856 printf("%s: init failed\n", sc->sc_dev.dv_xname);
857 CSR_WRITE_2(sc, WI_INT_EN, 0);
858 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
859
860 /* Calibrate timer. */
861 WI_SETVAL(WI_RID_TICK_TIME, 8);
862
863 return;
864 }
865
866 /*
867 * Read an LTV record from the NIC.
868 */
869 static int wi_read_record(sc, ltv)
870 struct wi_softc *sc;
871 struct wi_ltv_gen *ltv;
872 {
873 u_int16_t *ptr;
874 int len, code;
875 struct wi_ltv_gen *oltv, p2ltv;
876
877 if (sc->sc_firmware_type != WI_LUCENT) {
878 oltv = ltv;
879 switch (ltv->wi_type) {
880 case WI_RID_ENCRYPTION:
881 p2ltv.wi_type = WI_RID_P2_ENCRYPTION;
882 p2ltv.wi_len = 2;
883 ltv = &p2ltv;
884 break;
885 case WI_RID_TX_CRYPT_KEY:
886 p2ltv.wi_type = WI_RID_P2_TX_CRYPT_KEY;
887 p2ltv.wi_len = 2;
888 ltv = &p2ltv;
889 break;
890 case WI_RID_ROAMING_MODE:
891 if (sc->sc_firmware_type == WI_INTERSIL)
892 break;
893 /* not supported */
894 ltv->wi_len = 1;
895 return 0;
896 case WI_RID_MICROWAVE_OVEN:
897 /* not supported */
898 ltv->wi_len = 1;
899 return 0;
900 }
901 }
902
903 /* Tell the NIC to enter record read mode. */
904 if (wi_cmd(sc, WI_CMD_ACCESS|WI_ACCESS_READ, ltv->wi_type))
905 return(EIO);
906
907 /* Seek to the record. */
908 if (wi_seek(sc, ltv->wi_type, 0, WI_BAP1))
909 return(EIO);
910
911 /*
912 * Read the length and record type and make sure they
913 * match what we expect (this verifies that we have enough
914 * room to hold all of the returned data).
915 */
916 len = CSR_READ_2(sc, WI_DATA1);
917 if (len > ltv->wi_len)
918 return(ENOSPC);
919 code = CSR_READ_2(sc, WI_DATA1);
920 if (code != ltv->wi_type)
921 return(EIO);
922
923 ltv->wi_len = len;
924 ltv->wi_type = code;
925
926 /* Now read the data. */
927 ptr = <v->wi_val;
928 if (ltv->wi_len > 1)
929 CSR_READ_MULTI_STREAM_2(sc, WI_DATA1, ptr, ltv->wi_len - 1);
930
931 if (sc->sc_firmware_type != WI_LUCENT) {
932 int v;
933
934 switch (oltv->wi_type) {
935 case WI_RID_TX_RATE:
936 case WI_RID_CUR_TX_RATE:
937 switch (le16toh(ltv->wi_val)) {
938 case 1: v = 1; break;
939 case 2: v = 2; break;
940 case 3: v = 6; break;
941 case 4: v = 5; break;
942 case 7: v = 7; break;
943 case 8: v = 11; break;
944 case 15: v = 3; break;
945 default: v = 0x100 + le16toh(ltv->wi_val); break;
946 }
947 oltv->wi_val = htole16(v);
948 break;
949 case WI_RID_ENCRYPTION:
950 oltv->wi_len = 2;
951 if (le16toh(ltv->wi_val) & 0x01)
952 oltv->wi_val = htole16(1);
953 else
954 oltv->wi_val = htole16(0);
955 break;
956 case WI_RID_TX_CRYPT_KEY:
957 oltv->wi_len = 2;
958 oltv->wi_val = ltv->wi_val;
959 break;
960 case WI_RID_AUTH_CNTL:
961 oltv->wi_len = 2;
962 if (le16toh(ltv->wi_val) & 0x01)
963 oltv->wi_val = htole16(1);
964 else if (le16toh(ltv->wi_val) & 0x02)
965 oltv->wi_val = htole16(2);
966 break;
967 }
968 }
969
970 return(0);
971 }
972
973 /*
974 * Same as read, except we inject data instead of reading it.
975 */
976 static int wi_write_record(sc, ltv)
977 struct wi_softc *sc;
978 struct wi_ltv_gen *ltv;
979 {
980 u_int16_t *ptr;
981 int i;
982 struct wi_ltv_gen p2ltv;
983
984 if (sc->sc_firmware_type != WI_LUCENT) {
985 int v;
986
987 switch (ltv->wi_type) {
988 case WI_RID_TX_RATE:
989 p2ltv.wi_type = WI_RID_TX_RATE;
990 p2ltv.wi_len = 2;
991 switch (le16toh(ltv->wi_val)) {
992 case 1: v = 1; break;
993 case 2: v = 2; break;
994 case 3: v = 15; break;
995 case 5: v = 4; break;
996 case 6: v = 3; break;
997 case 7: v = 7; break;
998 case 11: v = 8; break;
999 default: return EINVAL;
1000 }
1001 p2ltv.wi_val = htole16(v);
1002 ltv = &p2ltv;
1003 break;
1004 case WI_RID_ENCRYPTION:
1005 p2ltv.wi_type = WI_RID_P2_ENCRYPTION;
1006 p2ltv.wi_len = 2;
1007 if (le16toh(ltv->wi_val))
1008 p2ltv.wi_val = htole16(PRIVACY_INVOKED |
1009 EXCLUDE_UNENCRYPTED);
1010 else
1011 p2ltv.wi_val =
1012 htole16(HOST_ENCRYPT | HOST_DECRYPT);
1013 ltv = &p2ltv;
1014 break;
1015 case WI_RID_TX_CRYPT_KEY:
1016 p2ltv.wi_type = WI_RID_P2_TX_CRYPT_KEY;
1017 p2ltv.wi_len = 2;
1018 p2ltv.wi_val = ltv->wi_val;
1019 ltv = &p2ltv;
1020 break;
1021 case WI_RID_DEFLT_CRYPT_KEYS:
1022 {
1023 int error;
1024 int keylen;
1025 struct wi_ltv_str ws;
1026 struct wi_ltv_keys *wk = (struct wi_ltv_keys *)ltv;
1027
1028 keylen = wk->wi_keys[sc->wi_tx_key].wi_keylen;
1029
1030 for (i = 0; i < 4; i++) {
1031 memset(&ws, 0, sizeof(ws));
1032 ws.wi_len = (keylen > 5) ? 8 : 4;
1033 ws.wi_type = WI_RID_P2_CRYPT_KEY0 + i;
1034 memcpy(ws.wi_str,
1035 &wk->wi_keys[i].wi_keydat, keylen);
1036 error = wi_write_record(sc,
1037 (struct wi_ltv_gen *)&ws);
1038 if (error)
1039 return error;
1040 }
1041 return 0;
1042 }
1043 case WI_RID_AUTH_CNTL:
1044 p2ltv.wi_type = WI_RID_AUTH_CNTL;
1045 p2ltv.wi_len = 2;
1046 if (le16toh(ltv->wi_val) == 1)
1047 p2ltv.wi_val = htole16(0x01);
1048 else if (le16toh(ltv->wi_val) == 2)
1049 p2ltv.wi_val = htole16(0x02);
1050 ltv = &p2ltv;
1051 break;
1052
1053 case WI_RID_ROAMING_MODE:
1054 if (sc->sc_firmware_type == WI_INTERSIL)
1055 break;
1056 /* not supported */
1057 return 0;
1058
1059 case WI_RID_MICROWAVE_OVEN:
1060 /* not supported */
1061 return 0;
1062 }
1063 }
1064
1065 if (wi_seek(sc, ltv->wi_type, 0, WI_BAP1))
1066 return(EIO);
1067
1068 CSR_WRITE_2(sc, WI_DATA1, ltv->wi_len);
1069 CSR_WRITE_2(sc, WI_DATA1, ltv->wi_type);
1070
1071 /* Write data */
1072 ptr = <v->wi_val;
1073 if (ltv->wi_len > 1)
1074 CSR_WRITE_MULTI_STREAM_2(sc, WI_DATA1, ptr, ltv->wi_len - 1);
1075
1076 if (wi_cmd(sc, WI_CMD_ACCESS|WI_ACCESS_WRITE, ltv->wi_type))
1077 return(EIO);
1078
1079 return(0);
1080 }
1081
1082 static int wi_seek(sc, id, off, chan)
1083 struct wi_softc *sc;
1084 int id, off, chan;
1085 {
1086 int i;
1087 int selreg, offreg;
1088 int status;
1089
1090 switch (chan) {
1091 case WI_BAP0:
1092 selreg = WI_SEL0;
1093 offreg = WI_OFF0;
1094 break;
1095 case WI_BAP1:
1096 selreg = WI_SEL1;
1097 offreg = WI_OFF1;
1098 break;
1099 default:
1100 printf("%s: invalid data path: %x\n",
1101 sc->sc_dev.dv_xname, chan);
1102 return(EIO);
1103 }
1104
1105 CSR_WRITE_2(sc, selreg, id);
1106 CSR_WRITE_2(sc, offreg, off);
1107
1108 for (i = 0; i < WI_TIMEOUT; i++) {
1109 status = CSR_READ_2(sc, offreg);
1110 if (!(status & (WI_OFF_BUSY|WI_OFF_ERR)))
1111 break;
1112 }
1113
1114 if (i == WI_TIMEOUT) {
1115 printf("%s: timeout in wi_seek to %x/%x; last status %x\n",
1116 sc->sc_dev.dv_xname, id, off, status);
1117 return(ETIMEDOUT);
1118 }
1119 return(0);
1120 }
1121
1122 static int wi_read_data(sc, id, off, buf, len)
1123 struct wi_softc *sc;
1124 int id, off;
1125 caddr_t buf;
1126 int len;
1127 {
1128 u_int16_t *ptr;
1129
1130 if (wi_seek(sc, id, off, WI_BAP1))
1131 return(EIO);
1132
1133 ptr = (u_int16_t *)buf;
1134 CSR_READ_MULTI_STREAM_2(sc, WI_DATA1, ptr, len / 2);
1135
1136 return(0);
1137 }
1138
1139 /*
1140 * According to the comments in the HCF Light code, there is a bug in
1141 * the Hermes (or possibly in certain Hermes firmware revisions) where
1142 * the chip's internal autoincrement counter gets thrown off during
1143 * data writes: the autoincrement is missed, causing one data word to
1144 * be overwritten and subsequent words to be written to the wrong memory
1145 * locations. The end result is that we could end up transmitting bogus
1146 * frames without realizing it. The workaround for this is to write a
1147 * couple of extra guard words after the end of the transfer, then
1148 * attempt to read then back. If we fail to locate the guard words where
1149 * we expect them, we preform the transfer over again.
1150 */
1151 static int wi_write_data(sc, id, off, buf, len)
1152 struct wi_softc *sc;
1153 int id, off;
1154 caddr_t buf;
1155 int len;
1156 {
1157 u_int16_t *ptr;
1158
1159 #ifdef WI_HERMES_AUTOINC_WAR
1160 again:
1161 #endif
1162
1163 if (wi_seek(sc, id, off, WI_BAP0))
1164 return(EIO);
1165
1166 ptr = (u_int16_t *)buf;
1167 CSR_WRITE_MULTI_STREAM_2(sc, WI_DATA0, ptr, len / 2);
1168
1169 #ifdef WI_HERMES_AUTOINC_WAR
1170 CSR_WRITE_2(sc, WI_DATA0, 0x1234);
1171 CSR_WRITE_2(sc, WI_DATA0, 0x5678);
1172
1173 if (wi_seek(sc, id, off + len, WI_BAP0))
1174 return(EIO);
1175
1176 if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
1177 CSR_READ_2(sc, WI_DATA0) != 0x5678)
1178 goto again;
1179 #endif
1180
1181 return(0);
1182 }
1183
1184 /*
1185 * Allocate a region of memory inside the NIC and zero
1186 * it out.
1187 */
1188 static int wi_alloc_nicmem(sc, len, id)
1189 struct wi_softc *sc;
1190 int len;
1191 int *id;
1192 {
1193 int i;
1194
1195 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len)) {
1196 printf("%s: failed to allocate %d bytes on NIC\n",
1197 sc->sc_dev.dv_xname, len);
1198 return(ENOMEM);
1199 }
1200
1201 for (i = 0; i < WI_TIMEOUT; i++) {
1202 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
1203 break;
1204 }
1205
1206 if (i == WI_TIMEOUT) {
1207 printf("%s: TIMED OUT in alloc\n", sc->sc_dev.dv_xname);
1208 return(ETIMEDOUT);
1209 }
1210
1211 *id = CSR_READ_2(sc, WI_ALLOC_FID);
1212 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1213
1214 if (wi_seek(sc, *id, 0, WI_BAP0)) {
1215 printf("%s: seek failed in alloc\n", sc->sc_dev.dv_xname);
1216 return(EIO);
1217 }
1218
1219 for (i = 0; i < len / 2; i++)
1220 CSR_WRITE_2(sc, WI_DATA0, 0);
1221
1222 return(0);
1223 }
1224
1225 static void wi_setmulti(sc)
1226 struct wi_softc *sc;
1227 {
1228 struct ifnet *ifp;
1229 int i = 0;
1230 struct wi_ltv_mcast mcast;
1231 struct ether_multi *enm;
1232 struct ether_multistep estep;
1233 struct ethercom *ec = &sc->sc_ethercom;
1234
1235 ifp = &sc->sc_ethercom.ec_if;
1236
1237 if ((ifp->if_flags & IFF_PROMISC) != 0) {
1238 allmulti:
1239 ifp->if_flags |= IFF_ALLMULTI;
1240 memset((char *)&mcast, 0, sizeof(mcast));
1241 mcast.wi_type = WI_RID_MCAST_LIST;
1242 mcast.wi_len = ((ETHER_ADDR_LEN / 2) * 16) + 1;
1243
1244 wi_write_record(sc, (struct wi_ltv_gen *)&mcast);
1245 return;
1246 }
1247
1248 i = 0;
1249 ETHER_FIRST_MULTI(estep, ec, enm);
1250 while (enm != NULL) {
1251 /* Punt on ranges or too many multicast addresses. */
1252 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
1253 ETHER_ADDR_LEN) != 0 ||
1254 i >= 16)
1255 goto allmulti;
1256
1257 memcpy((char *)&mcast.wi_mcast[i], enm->enm_addrlo,
1258 ETHER_ADDR_LEN);
1259 i++;
1260 ETHER_NEXT_MULTI(estep, enm);
1261 }
1262
1263 ifp->if_flags &= ~IFF_ALLMULTI;
1264 mcast.wi_type = WI_RID_MCAST_LIST;
1265 mcast.wi_len = ((ETHER_ADDR_LEN / 2) * i) + 1;
1266 wi_write_record(sc, (struct wi_ltv_gen *)&mcast);
1267 }
1268
1269 static int
1270 wi_setdef(sc, wreq)
1271 struct wi_softc *sc;
1272 struct wi_req *wreq;
1273 {
1274 struct sockaddr_dl *sdl;
1275 struct ifnet *ifp;
1276 int error = 0;
1277
1278 ifp = &sc->sc_ethercom.ec_if;
1279
1280 switch(wreq->wi_type) {
1281 case WI_RID_MAC_NODE:
1282 sdl = (struct sockaddr_dl *)ifp->if_sadl;
1283 memcpy((char *)&sc->sc_macaddr, (char *)&wreq->wi_val,
1284 ETHER_ADDR_LEN);
1285 memcpy(LLADDR(sdl), (char *)&wreq->wi_val, ETHER_ADDR_LEN);
1286 break;
1287 case WI_RID_PORTTYPE:
1288 error = wi_sync_media(sc, le16toh(wreq->wi_val[0]), sc->wi_tx_rate);
1289 break;
1290 case WI_RID_TX_RATE:
1291 error = wi_sync_media(sc, sc->wi_ptype, le16toh(wreq->wi_val[0]));
1292 break;
1293 case WI_RID_MAX_DATALEN:
1294 sc->wi_max_data_len = le16toh(wreq->wi_val[0]);
1295 break;
1296 case WI_RID_RTS_THRESH:
1297 sc->wi_rts_thresh = le16toh(wreq->wi_val[0]);
1298 break;
1299 case WI_RID_SYSTEM_SCALE:
1300 sc->wi_ap_density = le16toh(wreq->wi_val[0]);
1301 break;
1302 case WI_RID_CREATE_IBSS:
1303 if (sc->sc_firmware_type != WI_INTERSIL)
1304 sc->wi_create_ibss = le16toh(wreq->wi_val[0]);
1305 break;
1306 case WI_RID_OWN_CHNL:
1307 sc->wi_channel = le16toh(wreq->wi_val[0]);
1308 break;
1309 case WI_RID_NODENAME:
1310 error = wi_set_ssid(&sc->wi_nodeid,
1311 (u_int8_t *)&wreq->wi_val[1], le16toh(wreq->wi_val[0]));
1312 break;
1313 case WI_RID_DESIRED_SSID:
1314 error = wi_set_ssid(&sc->wi_netid,
1315 (u_int8_t *)&wreq->wi_val[1], le16toh(wreq->wi_val[0]));
1316 break;
1317 case WI_RID_OWN_SSID:
1318 error = wi_set_ssid(&sc->wi_ibssid,
1319 (u_int8_t *)&wreq->wi_val[1], le16toh(wreq->wi_val[0]));
1320 break;
1321 case WI_RID_PM_ENABLED:
1322 sc->wi_pm_enabled = le16toh(wreq->wi_val[0]);
1323 break;
1324 case WI_RID_MICROWAVE_OVEN:
1325 sc->wi_mor_enabled = le16toh(wreq->wi_val[0]);
1326 break;
1327 case WI_RID_MAX_SLEEP:
1328 sc->wi_max_sleep = le16toh(wreq->wi_val[0]);
1329 break;
1330 case WI_RID_AUTH_CNTL:
1331 sc->wi_authtype = le16toh(wreq->wi_val[0]);
1332 break;
1333 case WI_RID_ROAMING_MODE:
1334 sc->wi_roaming = le16toh(wreq->wi_val[0]);
1335 break;
1336 case WI_RID_ENCRYPTION:
1337 sc->wi_use_wep = le16toh(wreq->wi_val[0]);
1338 break;
1339 case WI_RID_TX_CRYPT_KEY:
1340 sc->wi_tx_key = le16toh(wreq->wi_val[0]);
1341 break;
1342 case WI_RID_DEFLT_CRYPT_KEYS:
1343 memcpy((char *)&sc->wi_keys, (char *)wreq,
1344 sizeof(struct wi_ltv_keys));
1345 break;
1346 default:
1347 error = EINVAL;
1348 break;
1349 }
1350
1351 return (error);
1352 }
1353
1354 static int
1355 wi_getdef(sc, wreq)
1356 struct wi_softc *sc;
1357 struct wi_req *wreq;
1358 {
1359 struct sockaddr_dl *sdl;
1360 struct ifnet *ifp;
1361 int error = 0;
1362
1363 ifp = &sc->sc_ethercom.ec_if;
1364
1365 wreq->wi_len = 2; /* XXX */
1366 switch (wreq->wi_type) {
1367 case WI_RID_MAC_NODE:
1368 wreq->wi_len += ETHER_ADDR_LEN / 2 - 1;
1369 sdl = (struct sockaddr_dl *)ifp->if_sadl;
1370 memcpy(&wreq->wi_val, &sc->sc_macaddr, ETHER_ADDR_LEN);
1371 memcpy(&wreq->wi_val, LLADDR(sdl), ETHER_ADDR_LEN);
1372 break;
1373 case WI_RID_PORTTYPE:
1374 wreq->wi_val[0] = htole16(sc->wi_ptype);
1375 break;
1376 case WI_RID_TX_RATE:
1377 wreq->wi_val[0] = htole16(sc->wi_tx_rate);
1378 break;
1379 case WI_RID_MAX_DATALEN:
1380 wreq->wi_val[0] = htole16(sc->wi_max_data_len);
1381 break;
1382 case WI_RID_RTS_THRESH:
1383 wreq->wi_val[0] = htole16(sc->wi_rts_thresh);
1384 break;
1385 case WI_RID_SYSTEM_SCALE:
1386 wreq->wi_val[0] = htole16(sc->wi_ap_density);
1387 break;
1388 case WI_RID_CREATE_IBSS:
1389 if (sc->sc_firmware_type != WI_INTERSIL)
1390 wreq->wi_val[0] = htole16(sc->wi_create_ibss);
1391 break;
1392 case WI_RID_OWN_CHNL:
1393 wreq->wi_val[0] = htole16(sc->wi_channel);
1394 break;
1395 case WI_RID_NODENAME:
1396 wi_request_fill_ssid(wreq, &sc->wi_nodeid);
1397 break;
1398 case WI_RID_DESIRED_SSID:
1399 wi_request_fill_ssid(wreq, &sc->wi_netid);
1400 break;
1401 case WI_RID_OWN_SSID:
1402 wi_request_fill_ssid(wreq, &sc->wi_ibssid);
1403 break;
1404 case WI_RID_PM_ENABLED:
1405 wreq->wi_val[0] = htole16(sc->wi_pm_enabled);
1406 break;
1407 case WI_RID_MICROWAVE_OVEN:
1408 wreq->wi_val[0] = htole16(sc->wi_mor_enabled);
1409 break;
1410 case WI_RID_MAX_SLEEP:
1411 wreq->wi_val[0] = htole16(sc->wi_max_sleep);
1412 break;
1413 case WI_RID_AUTH_CNTL:
1414 wreq->wi_val[0] = htole16(sc->wi_authtype);
1415 break;
1416 case WI_RID_ROAMING_MODE:
1417 wreq->wi_val[0] = htole16(sc->wi_roaming);
1418 break;
1419 case WI_RID_WEP_AVAIL:
1420 wreq->wi_val[0] = htole16(sc->wi_has_wep);
1421 break;
1422 case WI_RID_ENCRYPTION:
1423 wreq->wi_val[0] = htole16(sc->wi_use_wep);
1424 break;
1425 case WI_RID_TX_CRYPT_KEY:
1426 wreq->wi_val[0] = htole16(sc->wi_tx_key);
1427 break;
1428 case WI_RID_DEFLT_CRYPT_KEYS:
1429 wreq->wi_len += sizeof(struct wi_ltv_keys) / 2 - 1;
1430 memcpy(wreq, &sc->wi_keys, sizeof(struct wi_ltv_keys));
1431 break;
1432 default:
1433 #if 0
1434 error = EIO;
1435 #else
1436 #ifdef WI_DEBUG
1437 printf("%s: wi_getdef: unknown request %d\n",
1438 sc->sc_dev.dv_xname, wreq->wi_type);
1439 #endif
1440 #endif
1441 break;
1442 }
1443
1444 return (error);
1445 }
1446
1447 static int
1448 wi_ioctl(ifp, command, data)
1449 struct ifnet *ifp;
1450 u_long command;
1451 caddr_t data;
1452 {
1453 int s, error = 0;
1454 int len;
1455 struct wi_softc *sc = ifp->if_softc;
1456 struct wi_req wreq;
1457 struct ifreq *ifr;
1458 struct proc *p = curproc->l_proc;
1459 struct ieee80211_nwid nwid;
1460
1461 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
1462 return (ENXIO);
1463
1464 s = splnet();
1465
1466 ifr = (struct ifreq *)data;
1467 switch (command) {
1468 case SIOCSIFADDR:
1469 case SIOCGIFADDR:
1470 case SIOCSIFMTU:
1471 error = ether_ioctl(ifp, command, data);
1472 break;
1473 case SIOCSIFFLAGS:
1474 if (ifp->if_flags & IFF_UP) {
1475 if (ifp->if_flags & IFF_RUNNING &&
1476 ifp->if_flags & IFF_PROMISC &&
1477 !(sc->wi_if_flags & IFF_PROMISC)) {
1478 WI_SETVAL(WI_RID_PROMISC, 1);
1479 } else if (ifp->if_flags & IFF_RUNNING &&
1480 !(ifp->if_flags & IFF_PROMISC) &&
1481 sc->wi_if_flags & IFF_PROMISC) {
1482 WI_SETVAL(WI_RID_PROMISC, 0);
1483 }
1484 wi_init(ifp);
1485 } else {
1486 if (ifp->if_flags & IFF_RUNNING) {
1487 wi_stop(ifp, 0);
1488 }
1489 }
1490 sc->wi_if_flags = ifp->if_flags;
1491
1492 if (!(ifp->if_flags & IFF_UP)) {
1493 if (sc->sc_enabled) {
1494 if (sc->sc_disable)
1495 (*sc->sc_disable)(sc);
1496 sc->sc_enabled = 0;
1497 ifp->if_flags &= ~IFF_RUNNING;
1498 }
1499 }
1500 error = 0;
1501 break;
1502 case SIOCADDMULTI:
1503 case SIOCDELMULTI:
1504 error = (command == SIOCADDMULTI) ?
1505 ether_addmulti(ifr, &sc->sc_ethercom) :
1506 ether_delmulti(ifr, &sc->sc_ethercom);
1507 if (error == ENETRESET) {
1508 if (sc->sc_enabled != 0) {
1509 /*
1510 * Multicast list has changed. Set the
1511 * hardware filter accordingly.
1512 */
1513 wi_setmulti(sc);
1514 }
1515 error = 0;
1516 }
1517 break;
1518 case SIOCSIFMEDIA:
1519 case SIOCGIFMEDIA:
1520 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
1521 break;
1522 case SIOCGWAVELAN:
1523 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1524 if (error)
1525 break;
1526 if (wreq.wi_type == WI_RID_IFACE_STATS) {
1527 memcpy((char *)&wreq.wi_val, (char *)&sc->wi_stats,
1528 sizeof(sc->wi_stats));
1529 wreq.wi_len = (sizeof(sc->wi_stats) / 2) + 1;
1530 } else if (wreq.wi_type == WI_RID_READ_APS) {
1531 if (sc->wi_scanning) {
1532 error = EINPROGRESS;
1533 break;
1534 } else {
1535 len = sc->wi_naps * sizeof(struct wi_apinfo);
1536 len = len > WI_MAX_DATALEN ? WI_MAX_DATALEN : len;
1537 len = len / sizeof(struct wi_apinfo);
1538 memcpy((char *)&wreq.wi_val, (char *)&len, sizeof(len));
1539 memcpy((char *)&wreq.wi_val + sizeof(len),
1540 (char *)&sc->wi_aps,
1541 len * sizeof(struct wi_apinfo));
1542 }
1543 } else if (wreq.wi_type == WI_RID_DEFLT_CRYPT_KEYS) {
1544 /* For non-root user, return all-zeroes keys */
1545 if (suser(p->p_ucred, &p->p_acflag))
1546 memset((char *)&wreq, 0,
1547 sizeof(struct wi_ltv_keys));
1548 else
1549 memcpy((char *)&wreq, (char *)&sc->wi_keys,
1550 sizeof(struct wi_ltv_keys));
1551 } else {
1552 if (sc->sc_enabled == 0)
1553 error = wi_getdef(sc, &wreq);
1554 else if (wreq.wi_len > WI_MAX_DATALEN)
1555 error = EINVAL;
1556 else if (wi_read_record(sc, (struct wi_ltv_gen *)&wreq))
1557 error = EINVAL;
1558 }
1559 if (error == 0)
1560 error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
1561 break;
1562 case SIOCSWAVELAN:
1563 error = suser(p->p_ucred, &p->p_acflag);
1564 if (error)
1565 break;
1566 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1567 if (error)
1568 break;
1569 if (wreq.wi_type == WI_RID_IFACE_STATS) {
1570 if (sc->sc_enabled)
1571 wi_inquire(sc);
1572 break;
1573 } else if (wreq.wi_type == WI_RID_MGMT_XMIT) {
1574 error = wi_mgmt_xmit(sc, (caddr_t)&wreq.wi_val,
1575 wreq.wi_len);
1576 } else if (wreq.wi_type == WI_RID_SCAN_APS) {
1577 if (wreq.wi_len != 4) {
1578 error = EINVAL;
1579 break;
1580 }
1581 if (!sc->wi_scanning) {
1582 switch (sc->sc_firmware_type) {
1583 case WI_LUCENT:
1584 break;
1585 case WI_INTERSIL:
1586 wreq.wi_type = WI_RID_SCAN_REQ;
1587 error = wi_write_record(sc,
1588 (struct wi_ltv_gen *)&wreq);
1589 break;
1590 case WI_SYMBOL:
1591 /*
1592 * XXX only supported on 3.x ?
1593 */
1594 wreq.wi_type = WI_RID_BCAST_SCAN_REQ;
1595 wreq.wi_val[0] =
1596 BSCAN_BCAST | BSCAN_ONETIME;
1597 wreq.wi_len = 2;
1598 error = wi_write_record(sc,
1599 (struct wi_ltv_gen *)&wreq);
1600 break;
1601 }
1602 if (!error) {
1603 sc->wi_scanning = 1;
1604 callout_reset(&sc->wi_scan_sh, hz * 1,
1605 wi_wait_scan, sc);
1606 }
1607 }
1608 } else {
1609 if (wreq.wi_len > WI_MAX_DATALEN)
1610 error = EINVAL;
1611 else if (sc->sc_enabled != 0)
1612 error = wi_write_record(sc,
1613 (struct wi_ltv_gen *)&wreq);
1614 if (error == 0)
1615 error = wi_setdef(sc, &wreq);
1616 if (error == 0 && sc->sc_enabled != 0)
1617 /* Reinitialize WaveLAN. */
1618 wi_init(ifp);
1619 }
1620 break;
1621 case SIOCG80211NWID:
1622 if (sc->sc_enabled == 0) {
1623 /* Return the desired ID */
1624 error = copyout(&sc->wi_netid, ifr->ifr_data,
1625 sizeof(sc->wi_netid));
1626 } else {
1627 wreq.wi_type = WI_RID_CURRENT_SSID;
1628 wreq.wi_len = WI_MAX_DATALEN;
1629 if (wi_read_record(sc, (struct wi_ltv_gen *)&wreq) ||
1630 le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN)
1631 error = EINVAL;
1632 else {
1633 wi_set_ssid(&nwid, (u_int8_t *)&wreq.wi_val[1],
1634 le16toh(wreq.wi_val[0]));
1635 error = copyout(&nwid, ifr->ifr_data,
1636 sizeof(nwid));
1637 }
1638 }
1639 break;
1640 case SIOCS80211NWID:
1641 error = copyin(ifr->ifr_data, &nwid, sizeof(nwid));
1642 if (error != 0)
1643 break;
1644 if (nwid.i_len > IEEE80211_NWID_LEN) {
1645 error = EINVAL;
1646 break;
1647 }
1648 if (sc->wi_netid.i_len == nwid.i_len &&
1649 memcmp(sc->wi_netid.i_nwid, nwid.i_nwid, nwid.i_len) == 0)
1650 break;
1651 wi_set_ssid(&sc->wi_netid, nwid.i_nwid, nwid.i_len);
1652 if (sc->sc_enabled != 0)
1653 /* Reinitialize WaveLAN. */
1654 wi_init(ifp);
1655 break;
1656 case SIOCS80211NWKEY:
1657 error = wi_set_nwkey(sc, (struct ieee80211_nwkey *)data);
1658 break;
1659 case SIOCG80211NWKEY:
1660 error = wi_get_nwkey(sc, (struct ieee80211_nwkey *)data);
1661 break;
1662 case SIOCS80211POWER:
1663 error = wi_set_pm(sc, (struct ieee80211_power *)data);
1664 break;
1665 case SIOCG80211POWER:
1666 error = wi_get_pm(sc, (struct ieee80211_power *)data);
1667 break;
1668
1669 default:
1670 error = EINVAL;
1671 break;
1672 }
1673
1674 splx(s);
1675 return (error);
1676 }
1677
1678 static int
1679 wi_init(ifp)
1680 struct ifnet *ifp;
1681 {
1682 struct wi_softc *sc = ifp->if_softc;
1683 struct wi_req wreq;
1684 struct wi_ltv_macaddr mac;
1685 int error, id = 0, wasenabled;
1686
1687 wasenabled = sc->sc_enabled;
1688 if (!sc->sc_enabled) {
1689 if ((error = (*sc->sc_enable)(sc)) != 0)
1690 goto out;
1691 sc->sc_enabled = 1;
1692 }
1693
1694 wi_stop(ifp, 0);
1695 /* Symbol firmware cannot be initialized more than once */
1696 if (!(sc->sc_firmware_type == WI_SYMBOL && wasenabled))
1697 wi_reset(sc);
1698
1699 /* Program max data length. */
1700 WI_SETVAL(WI_RID_MAX_DATALEN, sc->wi_max_data_len);
1701
1702 /* Enable/disable IBSS creation. */
1703 if (sc->sc_firmware_type != WI_INTERSIL)
1704 WI_SETVAL(WI_RID_CREATE_IBSS, sc->wi_create_ibss);
1705
1706 /* Set the port type. */
1707 WI_SETVAL(WI_RID_PORTTYPE, sc->wi_ptype);
1708
1709 /* Program the RTS/CTS threshold. */
1710 WI_SETVAL(WI_RID_RTS_THRESH, sc->wi_rts_thresh);
1711
1712 /* Program the TX rate */
1713 WI_SETVAL(WI_RID_TX_RATE, sc->wi_tx_rate);
1714
1715 /* Access point density */
1716 WI_SETVAL(WI_RID_SYSTEM_SCALE, sc->wi_ap_density);
1717
1718 /* Power Management Enabled */
1719 WI_SETVAL(WI_RID_PM_ENABLED, sc->wi_pm_enabled);
1720
1721 /* Power Managment Max Sleep */
1722 WI_SETVAL(WI_RID_MAX_SLEEP, sc->wi_max_sleep);
1723
1724 /* Roaming type */
1725 WI_SETVAL(WI_RID_ROAMING_MODE, sc->wi_roaming);
1726
1727 /* Specify the IBSS name */
1728 wi_write_ssid(sc, WI_RID_OWN_SSID, &wreq, &sc->wi_ibssid);
1729
1730 /* Specify the network name */
1731 wi_write_ssid(sc, WI_RID_DESIRED_SSID, &wreq, &sc->wi_netid);
1732
1733 /* Specify the frequency to use */
1734 WI_SETVAL(WI_RID_OWN_CHNL, sc->wi_channel);
1735
1736 /* Program the nodename. */
1737 wi_write_ssid(sc, WI_RID_NODENAME, &wreq, &sc->wi_nodeid);
1738
1739 /* Set our MAC address. */
1740 mac.wi_len = 4;
1741 mac.wi_type = WI_RID_MAC_NODE;
1742 memcpy(&mac.wi_mac_addr, sc->sc_macaddr, ETHER_ADDR_LEN);
1743 wi_write_record(sc, (struct wi_ltv_gen *)&mac);
1744
1745 /* Initialize promisc mode. */
1746 if (ifp->if_flags & IFF_PROMISC) {
1747 WI_SETVAL(WI_RID_PROMISC, 1);
1748 } else {
1749 WI_SETVAL(WI_RID_PROMISC, 0);
1750 }
1751
1752 /* Configure WEP. */
1753 if (sc->wi_has_wep) {
1754 WI_SETVAL(WI_RID_ENCRYPTION, sc->wi_use_wep);
1755 WI_SETVAL(WI_RID_TX_CRYPT_KEY, sc->wi_tx_key);
1756 sc->wi_keys.wi_len = (sizeof(struct wi_ltv_keys) / 2) + 1;
1757 sc->wi_keys.wi_type = WI_RID_DEFLT_CRYPT_KEYS;
1758 wi_write_record(sc, (struct wi_ltv_gen *)&sc->wi_keys);
1759 if (sc->sc_firmware_type != WI_LUCENT && sc->wi_use_wep) {
1760 /*
1761 * ONLY HWB3163 EVAL-CARD Firmware version
1762 * less than 0.8 variant2
1763 *
1764 * If promiscuous mode disable, Prism2 chip
1765 * does not work with WEP .
1766 * It is under investigation for details.
1767 * (ichiro (at) netbsd.org)
1768 */
1769 if (sc->sc_firmware_type == WI_INTERSIL &&
1770 sc->sc_sta_firmware_ver < 802 ) {
1771 /* firm ver < 0.8 variant 2 */
1772 WI_SETVAL(WI_RID_PROMISC, 1);
1773 }
1774 WI_SETVAL(WI_RID_AUTH_CNTL, sc->wi_authtype);
1775 }
1776 }
1777
1778 /* Set multicast filter. */
1779 wi_setmulti(sc);
1780
1781 /* Enable desired port */
1782 wi_cmd(sc, WI_CMD_ENABLE | sc->wi_portnum, 0);
1783
1784 /* scanning variable is modal, therefore reinit to OFF, in case it was on. */
1785 sc->wi_scanning=0;
1786 sc->wi_naps=0;
1787
1788 if ((error = wi_alloc_nicmem(sc,
1789 1518 + sizeof(struct wi_frame) + 8, &id)) != 0) {
1790 printf("%s: tx buffer allocation failed\n",
1791 sc->sc_dev.dv_xname);
1792 goto out;
1793 }
1794 sc->wi_tx_data_id = id;
1795
1796 if ((error = wi_alloc_nicmem(sc,
1797 1518 + sizeof(struct wi_frame) + 8, &id)) != 0) {
1798 printf("%s: mgmt. buffer allocation failed\n",
1799 sc->sc_dev.dv_xname);
1800 goto out;
1801 }
1802 sc->wi_tx_mgmt_id = id;
1803
1804 /* Enable interrupts */
1805 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
1806
1807 ifp->if_flags |= IFF_RUNNING;
1808 ifp->if_flags &= ~IFF_OACTIVE;
1809
1810 callout_reset(&sc->wi_inquire_ch, hz * 60, wi_inquire, sc);
1811
1812 out:
1813 if (error) {
1814 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1815 ifp->if_timer = 0;
1816 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1817 }
1818 return (error);
1819 }
1820
1821 static void
1822 wi_start(ifp)
1823 struct ifnet *ifp;
1824 {
1825 struct wi_softc *sc;
1826 struct mbuf *m0;
1827 struct wi_frame tx_frame;
1828 struct ether_header *eh;
1829 int id;
1830
1831 sc = ifp->if_softc;
1832
1833 if (ifp->if_flags & IFF_OACTIVE)
1834 return;
1835
1836 IFQ_DEQUEUE(&ifp->if_snd, m0);
1837 if (m0 == NULL)
1838 return;
1839
1840 memset((char *)&tx_frame, 0, sizeof(tx_frame));
1841 id = sc->wi_tx_data_id;
1842 eh = mtod(m0, struct ether_header *);
1843
1844 /*
1845 * Use RFC1042 encoding for IP and ARP datagrams,
1846 * 802.3 for anything else.
1847 */
1848 if (ntohs(eh->ether_type) == ETHERTYPE_IP ||
1849 ntohs(eh->ether_type) == ETHERTYPE_ARP ||
1850 ntohs(eh->ether_type) == ETHERTYPE_REVARP ||
1851 ntohs(eh->ether_type) == ETHERTYPE_IPV6) {
1852 memcpy((char *)&tx_frame.wi_addr1, (char *)&eh->ether_dhost,
1853 ETHER_ADDR_LEN);
1854 memcpy((char *)&tx_frame.wi_addr2, (char *)&eh->ether_shost,
1855 ETHER_ADDR_LEN);
1856 memcpy((char *)&tx_frame.wi_dst_addr, (char *)&eh->ether_dhost,
1857 ETHER_ADDR_LEN);
1858 memcpy((char *)&tx_frame.wi_src_addr, (char *)&eh->ether_shost,
1859 ETHER_ADDR_LEN);
1860
1861 tx_frame.wi_dat_len = htole16(m0->m_pkthdr.len - WI_SNAPHDR_LEN);
1862 tx_frame.wi_frame_ctl = htole16(WI_FTYPE_DATA);
1863 tx_frame.wi_dat[0] = htons(WI_SNAP_WORD0);
1864 tx_frame.wi_dat[1] = htons(WI_SNAP_WORD1);
1865 tx_frame.wi_len = htons(m0->m_pkthdr.len - WI_SNAPHDR_LEN);
1866 tx_frame.wi_type = eh->ether_type;
1867
1868 m_copydata(m0, sizeof(struct ether_header),
1869 m0->m_pkthdr.len - sizeof(struct ether_header),
1870 (caddr_t)&sc->wi_txbuf);
1871
1872 wi_write_data(sc, id, 0, (caddr_t)&tx_frame,
1873 sizeof(struct wi_frame));
1874 wi_write_data(sc, id, WI_802_11_OFFSET, (caddr_t)&sc->wi_txbuf,
1875 (m0->m_pkthdr.len - sizeof(struct ether_header)) + 2);
1876 } else {
1877 tx_frame.wi_dat_len = htole16(m0->m_pkthdr.len);
1878
1879 m_copydata(m0, 0, m0->m_pkthdr.len, (caddr_t)&sc->wi_txbuf);
1880
1881 wi_write_data(sc, id, 0, (caddr_t)&tx_frame,
1882 sizeof(struct wi_frame));
1883 wi_write_data(sc, id, WI_802_3_OFFSET, (caddr_t)&sc->wi_txbuf,
1884 m0->m_pkthdr.len + 2);
1885 }
1886
1887 #if NBPFILTER > 0
1888 /*
1889 * If there's a BPF listener, bounce a copy of
1890 * this frame to him.
1891 */
1892 if (ifp->if_bpf)
1893 bpf_mtap(ifp->if_bpf, m0);
1894 #endif
1895
1896 m_freem(m0);
1897
1898 if (wi_cmd(sc, WI_CMD_TX|WI_RECLAIM, id))
1899 printf("%s: xmit failed\n", sc->sc_dev.dv_xname);
1900
1901 ifp->if_flags |= IFF_OACTIVE;
1902
1903 /*
1904 * Set a timeout in case the chip goes out to lunch.
1905 */
1906 ifp->if_timer = 5;
1907
1908 return;
1909 }
1910
1911 static int
1912 wi_mgmt_xmit(sc, data, len)
1913 struct wi_softc *sc;
1914 caddr_t data;
1915 int len;
1916 {
1917 struct wi_frame tx_frame;
1918 int id;
1919 struct wi_80211_hdr *hdr;
1920 caddr_t dptr;
1921
1922 hdr = (struct wi_80211_hdr *)data;
1923 dptr = data + sizeof(struct wi_80211_hdr);
1924
1925 memset((char *)&tx_frame, 0, sizeof(tx_frame));
1926 id = sc->wi_tx_mgmt_id;
1927
1928 memcpy((char *)&tx_frame.wi_frame_ctl, (char *)hdr,
1929 sizeof(struct wi_80211_hdr));
1930
1931 tx_frame.wi_dat_len = htole16(len - WI_SNAPHDR_LEN);
1932 tx_frame.wi_len = htons(len - WI_SNAPHDR_LEN);
1933
1934 wi_write_data(sc, id, 0, (caddr_t)&tx_frame, sizeof(struct wi_frame));
1935 wi_write_data(sc, id, WI_802_11_OFFSET_RAW, dptr,
1936 (len - sizeof(struct wi_80211_hdr)) + 2);
1937
1938 if (wi_cmd(sc, WI_CMD_TX|WI_RECLAIM, id)) {
1939 printf("%s: xmit failed\n", sc->sc_dev.dv_xname);
1940 return(EIO);
1941 }
1942
1943 return(0);
1944 }
1945
1946 static void
1947 wi_stop(ifp, disable)
1948 struct ifnet *ifp;
1949 {
1950 struct wi_softc *sc = ifp->if_softc;
1951
1952 CSR_WRITE_2(sc, WI_INT_EN, 0);
1953 wi_cmd(sc, WI_CMD_DISABLE|sc->wi_portnum, 0);
1954
1955 callout_stop(&sc->wi_inquire_ch);
1956 callout_stop(&sc->wi_scan_sh);
1957
1958 if (disable) {
1959 if (sc->sc_enabled) {
1960 if (sc->sc_disable)
1961 (*sc->sc_disable)(sc);
1962 sc->sc_enabled = 0;
1963 }
1964 }
1965
1966 ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
1967 ifp->if_timer = 0;
1968 }
1969
1970 static void
1971 wi_watchdog(ifp)
1972 struct ifnet *ifp;
1973 {
1974 struct wi_softc *sc;
1975
1976 sc = ifp->if_softc;
1977
1978 printf("%s: device timeout\n", sc->sc_dev.dv_xname);
1979
1980 wi_init(ifp);
1981
1982 ifp->if_oerrors++;
1983
1984 return;
1985 }
1986
1987 void
1988 wi_shutdown(sc)
1989 struct wi_softc *sc;
1990 {
1991 int s;
1992
1993 s = splnet();
1994 if (sc->sc_enabled) {
1995 if (sc->sc_disable)
1996 (*sc->sc_disable)(sc);
1997 sc->sc_enabled = 0;
1998 }
1999 splx(s);
2000 }
2001
2002 int
2003 wi_activate(self, act)
2004 struct device *self;
2005 enum devact act;
2006 {
2007 struct wi_softc *sc = (struct wi_softc *)self;
2008 int rv = 0, s;
2009
2010 s = splnet();
2011 switch (act) {
2012 case DVACT_ACTIVATE:
2013 rv = EOPNOTSUPP;
2014 break;
2015
2016 case DVACT_DEACTIVATE:
2017 if_deactivate(&sc->sc_ethercom.ec_if);
2018 break;
2019 }
2020 splx(s);
2021 return (rv);
2022 }
2023
2024 static void
2025 wi_get_id(sc)
2026 struct wi_softc *sc;
2027 {
2028 struct wi_ltv_ver ver;
2029 struct wi_card_ident *id;
2030
2031 /* getting chip identity */
2032 memset(&ver, 0, sizeof(ver));
2033 ver.wi_type = WI_RID_CARD_ID;
2034 ver.wi_len = 5;
2035 wi_read_record(sc, (struct wi_ltv_gen *)&ver);
2036 printf("%s: using ", sc->sc_dev.dv_xname);
2037
2038 sc->sc_firmware_type = WI_NOTYPE;
2039 for (id = wi_card_ident; id->card_name != NULL; id++) {
2040 if (le16toh(ver.wi_ver[0]) == id->card_id) {
2041 printf("%s", id->card_name);
2042 sc->sc_firmware_type = id->firm_type;
2043 break;
2044 }
2045 }
2046 if (sc->sc_firmware_type == WI_NOTYPE) {
2047 if (le16toh(ver.wi_ver[0]) & 0x8000) {
2048 printf("Unknown PRISM2 chip");
2049 sc->sc_firmware_type = WI_INTERSIL;
2050 } else {
2051 printf("Unknown Lucent chip");
2052 sc->sc_firmware_type = WI_LUCENT;
2053 }
2054 }
2055
2056 /* get primary firmware version (Only Prism chips) */
2057 if (sc->sc_firmware_type != WI_LUCENT) {
2058 memset(&ver, 0, sizeof(ver));
2059 ver.wi_type = WI_RID_PRI_IDENTITY;
2060 ver.wi_len = 5;
2061 wi_read_record(sc, (struct wi_ltv_gen *)&ver);
2062 LE16TOH(ver.wi_ver[1]);
2063 LE16TOH(ver.wi_ver[2]);
2064 LE16TOH(ver.wi_ver[3]);
2065 sc->sc_pri_firmware_ver = ver.wi_ver[2] * 10000 +
2066 ver.wi_ver[3] * 100 + ver.wi_ver[1];
2067 }
2068
2069 /* get station firmware version */
2070 memset(&ver, 0, sizeof(ver));
2071 ver.wi_type = WI_RID_STA_IDENTITY;
2072 ver.wi_len = 5;
2073 wi_read_record(sc, (struct wi_ltv_gen *)&ver);
2074 LE16TOH(ver.wi_ver[1]);
2075 LE16TOH(ver.wi_ver[2]);
2076 LE16TOH(ver.wi_ver[3]);
2077 sc->sc_sta_firmware_ver = ver.wi_ver[2] * 10000 +
2078 ver.wi_ver[3] * 100 + ver.wi_ver[1];
2079 if (sc->sc_firmware_type == WI_INTERSIL &&
2080 (sc->sc_sta_firmware_ver == 10102 || sc->sc_sta_firmware_ver == 20102)) {
2081 struct wi_ltv_str sver;
2082 char *p;
2083
2084 memset(&sver, 0, sizeof(sver));
2085 sver.wi_type = WI_RID_SYMBOL_IDENTITY;
2086 sver.wi_len = 7;
2087 /* value should be the format like "V2.00-11" */
2088 if (wi_read_record(sc, (struct wi_ltv_gen *)&sver) == 0 &&
2089 *(p = (char *)sver.wi_str) >= 'A' &&
2090 p[2] == '.' && p[5] == '-' && p[8] == '\0') {
2091 sc->sc_firmware_type = WI_SYMBOL;
2092 sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
2093 (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
2094 (p[6] - '0') * 10 + (p[7] - '0');
2095 }
2096 }
2097
2098 printf("\n%s: %s Firmware: ", sc->sc_dev.dv_xname,
2099 sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
2100 (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
2101 if (sc->sc_firmware_type != WI_LUCENT) /* XXX */
2102 printf("Primary (%u.%u.%u), ", sc->sc_pri_firmware_ver / 10000,
2103 (sc->sc_pri_firmware_ver % 10000) / 100,
2104 sc->sc_pri_firmware_ver % 100);
2105 printf("Station (%u.%u.%u)\n",
2106 sc->sc_sta_firmware_ver / 10000, (sc->sc_sta_firmware_ver % 10000) / 100,
2107 sc->sc_sta_firmware_ver % 100);
2108
2109 return;
2110 }
2111
2112 int
2113 wi_detach(sc)
2114 struct wi_softc *sc;
2115 {
2116 struct ifnet *ifp = sc->sc_ifp;
2117 int s;
2118
2119 if (!sc->sc_attached)
2120 return (0);
2121
2122 s = splnet();
2123 callout_stop(&sc->wi_inquire_ch);
2124
2125 /* Delete all remaining media. */
2126 ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY);
2127
2128 ether_ifdetach(ifp);
2129 if_detach(ifp);
2130 if (sc->sc_enabled) {
2131 if (sc->sc_disable)
2132 (*sc->sc_disable)(sc);
2133 sc->sc_enabled = 0;
2134 }
2135 splx(s);
2136 return (0);
2137 }
2138
2139 void
2140 wi_power(sc, why)
2141 struct wi_softc *sc;
2142 int why;
2143 {
2144 int s;
2145
2146 if (!sc->sc_enabled)
2147 return;
2148
2149 s = splnet();
2150 switch (why) {
2151 case PWR_SUSPEND:
2152 case PWR_STANDBY:
2153 wi_stop(sc->sc_ifp, 0);
2154 if (sc->sc_enabled) {
2155 if (sc->sc_disable)
2156 (*sc->sc_disable)(sc);
2157 }
2158 break;
2159 case PWR_RESUME:
2160 sc->sc_enabled = 0;
2161 wi_init(sc->sc_ifp);
2162 (void)wi_intr(sc);
2163 break;
2164 case PWR_SOFTSUSPEND:
2165 case PWR_SOFTSTANDBY:
2166 case PWR_SOFTRESUME:
2167 break;
2168 }
2169 splx(s);
2170 }
2171
2172 static int
2173 wi_set_ssid(ws, id, len)
2174 struct ieee80211_nwid *ws;
2175 u_int8_t *id;
2176 int len;
2177 {
2178
2179 if (len > IEEE80211_NWID_LEN)
2180 return (EINVAL);
2181 ws->i_len = len;
2182 memcpy(ws->i_nwid, id, len);
2183 return (0);
2184 }
2185
2186 static void
2187 wi_request_fill_ssid(wreq, ws)
2188 struct wi_req *wreq;
2189 struct ieee80211_nwid *ws;
2190 {
2191 int len = ws->i_len;
2192
2193 memset(&wreq->wi_val[0], 0, sizeof(wreq->wi_val));
2194 wreq->wi_val[0] = htole16(len);
2195 wreq->wi_len = roundup(len, 2) / 2 + 2;
2196 memcpy(&wreq->wi_val[1], ws->i_nwid, len);
2197 }
2198
2199 static int
2200 wi_write_ssid(sc, type, wreq, ws)
2201 struct wi_softc *sc;
2202 int type;
2203 struct wi_req *wreq;
2204 struct ieee80211_nwid *ws;
2205 {
2206
2207 wreq->wi_type = type;
2208 wi_request_fill_ssid(wreq, ws);
2209 return (wi_write_record(sc, (struct wi_ltv_gen *)wreq));
2210 }
2211
2212 static int
2213 wi_sync_media(sc, ptype, txrate)
2214 struct wi_softc *sc;
2215 int ptype;
2216 int txrate;
2217 {
2218 int media = sc->sc_media.ifm_cur->ifm_media;
2219 int options = IFM_OPTIONS(media);
2220 int subtype;
2221
2222 switch (txrate) {
2223 case 1:
2224 subtype = IFM_IEEE80211_DS1;
2225 break;
2226 case 2:
2227 subtype = IFM_IEEE80211_DS2;
2228 break;
2229 case 3:
2230 subtype = IFM_AUTO;
2231 break;
2232 case 5:
2233 subtype = IFM_IEEE80211_DS5;
2234 break;
2235 case 11:
2236 subtype = IFM_IEEE80211_DS11;
2237 break;
2238 default:
2239 subtype = IFM_MANUAL; /* Unable to represent */
2240 break;
2241 }
2242 switch (ptype) {
2243 case WI_PORTTYPE_ADHOC:
2244 options |= IFM_IEEE80211_ADHOC;
2245 break;
2246 case WI_PORTTYPE_BSS:
2247 options &= ~IFM_IEEE80211_ADHOC;
2248 break;
2249 default:
2250 subtype = IFM_MANUAL; /* Unable to represent */
2251 break;
2252 }
2253 media = IFM_MAKEWORD(IFM_TYPE(media), subtype, options,
2254 IFM_INST(media));
2255 if (ifmedia_match(&sc->sc_media, media, sc->sc_media.ifm_mask) == NULL)
2256 return (EINVAL);
2257 ifmedia_set(&sc->sc_media, media);
2258 sc->wi_ptype = ptype;
2259 sc->wi_tx_rate = txrate;
2260 return (0);
2261 }
2262
2263 static int
2264 wi_media_change(ifp)
2265 struct ifnet *ifp;
2266 {
2267 struct wi_softc *sc = ifp->if_softc;
2268 int otype = sc->wi_ptype;
2269 int orate = sc->wi_tx_rate;
2270
2271 if ((sc->sc_media.ifm_cur->ifm_media & IFM_IEEE80211_ADHOC) != 0)
2272 sc->wi_ptype = WI_PORTTYPE_ADHOC;
2273 else
2274 sc->wi_ptype = WI_PORTTYPE_BSS;
2275
2276 switch (IFM_SUBTYPE(sc->sc_media.ifm_cur->ifm_media)) {
2277 case IFM_IEEE80211_DS1:
2278 sc->wi_tx_rate = 1;
2279 break;
2280 case IFM_IEEE80211_DS2:
2281 sc->wi_tx_rate = 2;
2282 break;
2283 case IFM_AUTO:
2284 sc->wi_tx_rate = 3;
2285 break;
2286 case IFM_IEEE80211_DS5:
2287 sc->wi_tx_rate = 5;
2288 break;
2289 case IFM_IEEE80211_DS11:
2290 sc->wi_tx_rate = 11;
2291 break;
2292 }
2293
2294 if (sc->sc_enabled != 0) {
2295 if (otype != sc->wi_ptype ||
2296 orate != sc->wi_tx_rate)
2297 wi_init(ifp);
2298 }
2299
2300 ifp->if_baudrate = ifmedia_baudrate(sc->sc_media.ifm_cur->ifm_media);
2301
2302 return (0);
2303 }
2304
2305 static void
2306 wi_media_status(ifp, imr)
2307 struct ifnet *ifp;
2308 struct ifmediareq *imr;
2309 {
2310 struct wi_softc *sc = ifp->if_softc;
2311
2312 if (sc->sc_enabled == 0) {
2313 imr->ifm_active = IFM_IEEE80211|IFM_NONE;
2314 imr->ifm_status = 0;
2315 return;
2316 }
2317
2318 imr->ifm_active = sc->sc_media.ifm_cur->ifm_media;
2319 imr->ifm_status = IFM_AVALID|IFM_ACTIVE;
2320 }
2321
2322 static int
2323 wi_set_nwkey(sc, nwkey)
2324 struct wi_softc *sc;
2325 struct ieee80211_nwkey *nwkey;
2326 {
2327 int i, error;
2328 size_t len;
2329 struct wi_req wreq;
2330 struct wi_ltv_keys *wk = (struct wi_ltv_keys *)&wreq;
2331
2332 if (!sc->wi_has_wep)
2333 return ENODEV;
2334 if (nwkey->i_defkid <= 0 ||
2335 nwkey->i_defkid > IEEE80211_WEP_NKID)
2336 return EINVAL;
2337 memcpy(wk, &sc->wi_keys, sizeof(*wk));
2338 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2339 if (nwkey->i_key[i].i_keydat == NULL)
2340 continue;
2341 len = nwkey->i_key[i].i_keylen;
2342 if (len > sizeof(wk->wi_keys[i].wi_keydat))
2343 return EINVAL;
2344 error = copyin(nwkey->i_key[i].i_keydat,
2345 wk->wi_keys[i].wi_keydat, len);
2346 if (error)
2347 return error;
2348 wk->wi_keys[i].wi_keylen = htole16(len);
2349 }
2350
2351 wk->wi_len = (sizeof(*wk) / 2) + 1;
2352 wk->wi_type = WI_RID_DEFLT_CRYPT_KEYS;
2353 if (sc->sc_enabled != 0) {
2354 error = wi_write_record(sc, (struct wi_ltv_gen *)&wreq);
2355 if (error)
2356 return error;
2357 }
2358 error = wi_setdef(sc, &wreq);
2359 if (error)
2360 return error;
2361
2362 wreq.wi_len = 2;
2363 wreq.wi_type = WI_RID_TX_CRYPT_KEY;
2364 wreq.wi_val[0] = htole16(nwkey->i_defkid - 1);
2365 if (sc->sc_enabled != 0) {
2366 error = wi_write_record(sc, (struct wi_ltv_gen *)&wreq);
2367 if (error)
2368 return error;
2369 }
2370 error = wi_setdef(sc, &wreq);
2371 if (error)
2372 return error;
2373
2374 wreq.wi_type = WI_RID_ENCRYPTION;
2375 wreq.wi_val[0] = htole16(nwkey->i_wepon);
2376 if (sc->sc_enabled != 0) {
2377 error = wi_write_record(sc, (struct wi_ltv_gen *)&wreq);
2378 if (error)
2379 return error;
2380 }
2381 error = wi_setdef(sc, &wreq);
2382 if (error)
2383 return error;
2384
2385 if (sc->sc_enabled != 0)
2386 wi_init(&sc->sc_ethercom.ec_if);
2387 return 0;
2388 }
2389
2390 static int
2391 wi_get_nwkey(sc, nwkey)
2392 struct wi_softc *sc;
2393 struct ieee80211_nwkey *nwkey;
2394 {
2395 int i, len, error;
2396 struct wi_ltv_keys *wk = &sc->wi_keys;
2397
2398 if (!sc->wi_has_wep)
2399 return ENODEV;
2400 nwkey->i_wepon = sc->wi_use_wep;
2401 nwkey->i_defkid = sc->wi_tx_key + 1;
2402
2403 /* do not show any keys to non-root user */
2404 error = suser(curproc->l_proc->p_ucred, &curproc->l_proc->p_acflag);
2405 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2406 if (nwkey->i_key[i].i_keydat == NULL)
2407 continue;
2408 /* error holds results of suser() for the first time */
2409 if (error)
2410 return error;
2411 len = le16toh(wk->wi_keys[i].wi_keylen);
2412 if (nwkey->i_key[i].i_keylen < len)
2413 return ENOSPC;
2414 nwkey->i_key[i].i_keylen = len;
2415 error = copyout(wk->wi_keys[i].wi_keydat,
2416 nwkey->i_key[i].i_keydat, len);
2417 if (error)
2418 return error;
2419 }
2420 return 0;
2421 }
2422
2423 static int
2424 wi_set_pm(struct wi_softc *sc, struct ieee80211_power *power)
2425 {
2426
2427 sc->wi_pm_enabled = power->i_enabled;
2428 sc->wi_max_sleep = power->i_maxsleep;
2429
2430 if (sc->sc_enabled)
2431 return (wi_init(&sc->sc_ethercom.ec_if));
2432
2433 return (0);
2434 }
2435
2436 static int
2437 wi_get_pm(struct wi_softc *sc, struct ieee80211_power *power)
2438 {
2439
2440 power->i_enabled = sc->wi_pm_enabled;
2441 power->i_maxsleep = sc->wi_max_sleep;
2442
2443 return (0);
2444 }
2445