wi.c revision 1.3 1 /* $NetBSD: wi.c,v 1.3 2001/05/08 16:42:49 ichiro 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 #define WI_HERMES_AUTOINC_WAR /* Work around data write autoinc bug. */
73 #define WI_HERMES_STATS_WAR /* Work around stats counter bug. */
74
75 #include "opt_inet.h"
76 #include "bpfilter.h"
77
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/callout.h>
81 #include <sys/device.h>
82 #include <sys/socket.h>
83 #include <sys/mbuf.h>
84 #include <sys/ioctl.h>
85 #include <sys/kernel.h> /* for hz */
86 #include <sys/proc.h>
87
88 #include <net/if.h>
89 #include <net/if_dl.h>
90 #include <net/if_media.h>
91 #include <net/if_ether.h>
92 #include <net/if_ieee80211.h>
93
94 #ifdef INET
95 #include <netinet/in.h>
96 #include <netinet/in_systm.h>
97 #include <netinet/in_var.h>
98 #include <netinet/ip.h>
99 #include <netinet/if_inarp.h>
100 #endif
101
102 #if NBPFILTER > 0
103 #include <net/bpf.h>
104 #include <net/bpfdesc.h>
105 #endif
106
107 #include <dev/pcmcia/pcmciareg.h>
108 #include <dev/pcmcia/pcmciavar.h>
109 #include <dev/pcmcia/pcmciadevs.h>
110
111 #include <dev/ic/wi_ieee.h>
112 #include <dev/ic/wireg.h>
113 #include <dev/ic/wivar.h>
114
115 static void wi_reset __P((struct wi_softc *));
116 static int wi_ioctl __P((struct ifnet *, u_long, caddr_t));
117 static void wi_start __P((struct ifnet *));
118 static void wi_watchdog __P((struct ifnet *));
119 static int wi_init __P((struct ifnet *));
120 static void wi_stop __P((struct ifnet *, int));
121 static void wi_rxeof __P((struct wi_softc *));
122 static void wi_txeof __P((struct wi_softc *, int));
123 static void wi_update_stats __P((struct wi_softc *));
124 static void wi_setmulti __P((struct wi_softc *));
125
126 static int wi_cmd __P((struct wi_softc *, int, int));
127 static int wi_read_record __P((struct wi_softc *, struct wi_ltv_gen *));
128 static int wi_write_record __P((struct wi_softc *, struct wi_ltv_gen *));
129 static int wi_read_data __P((struct wi_softc *, int,
130 int, caddr_t, int));
131 static int wi_write_data __P((struct wi_softc *, int,
132 int, caddr_t, int));
133 static int wi_seek __P((struct wi_softc *, int, int, int));
134 static int wi_alloc_nicmem __P((struct wi_softc *, int, int *));
135 static void wi_inquire __P((void *));
136 static int wi_setdef __P((struct wi_softc *, struct wi_req *));
137 static int wi_getdef __P((struct wi_softc *, struct wi_req *));
138 static int wi_mgmt_xmit __P((struct wi_softc *, caddr_t, int));
139
140 static int wi_media_change __P((struct ifnet *));
141 static void wi_media_status __P((struct ifnet *, struct ifmediareq *));
142
143 static int wi_set_ssid __P((struct ieee80211_nwid *, u_int8_t *, int));
144 static void wi_request_fill_ssid __P((struct wi_req *,
145 struct ieee80211_nwid *));
146 static int wi_write_ssid __P((struct wi_softc *, int, struct wi_req *,
147 struct ieee80211_nwid *));
148 static int wi_set_nwkey __P((struct wi_softc *, struct ieee80211_nwkey *));
149 static int wi_get_nwkey __P((struct wi_softc *, struct ieee80211_nwkey *));
150 static int wi_sync_media __P((struct wi_softc *, int, int));
151 static int wi_set_pm(struct wi_softc *, struct ieee80211_power *);
152 static int wi_get_pm(struct wi_softc *, struct ieee80211_power *);
153
154 int
155 wi_attach(sc)
156 struct wi_softc *sc;
157 {
158 struct ifnet *ifp = sc->sc_ifp;
159 struct wi_ltv_macaddr mac;
160 struct wi_ltv_gen gen;
161 static const u_int8_t empty_macaddr[ETHER_ADDR_LEN] = {
162 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
163 };
164 int s;
165
166 s = splnet();
167
168 callout_init(&sc->wi_inquire_ch);
169
170 /* Make sure interrupts are disabled. */
171 CSR_WRITE_2(sc, WI_INT_EN, 0);
172 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
173
174 /* Reset the NIC. */
175 wi_reset(sc);
176
177 memset(&mac, 0, sizeof(mac));
178 /* Read the station address. */
179 mac.wi_type = WI_RID_MAC_NODE;
180 mac.wi_len = 4;
181 wi_read_record(sc, (struct wi_ltv_gen *)&mac);
182 memcpy(sc->sc_macaddr, mac.wi_mac_addr, ETHER_ADDR_LEN);
183
184 /*
185 * Check if we got anything meaningful.
186 *
187 * Is it really enough just checking against null ethernet address?
188 * Or, check against possible vendor? XXX.
189 */
190 if (bcmp(sc->sc_macaddr, empty_macaddr, ETHER_ADDR_LEN) == 0) {
191 printf("%s: could not get mac address, attach failed\n",
192 sc->sc_dev.dv_xname);
193 return 1;
194 }
195
196 printf(" 802.11 address %s\n", ether_sprintf(sc->sc_macaddr));
197
198 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
199 ifp->if_softc = sc;
200 ifp->if_start = wi_start;
201 ifp->if_ioctl = wi_ioctl;
202 ifp->if_watchdog = wi_watchdog;
203 ifp->if_init = wi_init;
204 ifp->if_stop = wi_stop;
205 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
206 #ifdef IFF_NOTRAILERS
207 ifp->if_flags |= IFF_NOTRAILERS;
208 #endif
209 IFQ_SET_READY(&ifp->if_snd);
210
211 (void)wi_set_ssid(&sc->wi_nodeid, WI_DEFAULT_NODENAME,
212 sizeof(WI_DEFAULT_NODENAME) - 1);
213 (void)wi_set_ssid(&sc->wi_netid, WI_DEFAULT_NETNAME,
214 sizeof(WI_DEFAULT_NETNAME) - 1);
215 (void)wi_set_ssid(&sc->wi_ibssid, WI_DEFAULT_IBSS,
216 sizeof(WI_DEFAULT_IBSS) - 1);
217
218 sc->wi_portnum = WI_DEFAULT_PORT;
219 sc->wi_ptype = WI_PORTTYPE_BSS;
220 sc->wi_ap_density = WI_DEFAULT_AP_DENSITY;
221 sc->wi_rts_thresh = WI_DEFAULT_RTS_THRESH;
222 sc->wi_tx_rate = WI_DEFAULT_TX_RATE;
223 sc->wi_max_data_len = WI_DEFAULT_DATALEN;
224 sc->wi_create_ibss = WI_DEFAULT_CREATE_IBSS;
225 sc->wi_pm_enabled = WI_DEFAULT_PM_ENABLED;
226 sc->wi_max_sleep = WI_DEFAULT_MAX_SLEEP;
227
228 /*
229 * Read the default channel from the NIC. This may vary
230 * depending on the country where the NIC was purchased, so
231 * we can't hard-code a default and expect it to work for
232 * everyone.
233 */
234 gen.wi_type = WI_RID_OWN_CHNL;
235 gen.wi_len = 2;
236 wi_read_record(sc, &gen);
237 sc->wi_channel = gen.wi_val;
238
239 bzero((char *)&sc->wi_stats, sizeof(sc->wi_stats));
240
241 /*
242 * Find out if we support WEP on this card.
243 */
244 gen.wi_type = WI_RID_WEP_AVAIL;
245 gen.wi_len = 2;
246 wi_read_record(sc, &gen);
247 sc->wi_has_wep = gen.wi_val;
248
249 ifmedia_init(&sc->sc_media, 0, wi_media_change, wi_media_status);
250 #define IFM_AUTOADHOC \
251 IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO, IFM_IEEE80211_ADHOC, 0)
252 #define ADD(m, c) ifmedia_add(&sc->sc_media, (m), (c), NULL)
253 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO, 0, 0), 0);
254 ADD(IFM_AUTOADHOC, 0);
255 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS1, 0, 0), 0);
256 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS1,
257 IFM_IEEE80211_ADHOC, 0), 0);
258 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS2, 0, 0), 0);
259 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS2,
260 IFM_IEEE80211_ADHOC, 0), 0);
261 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS11, 0, 0), 0);
262 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS11,
263 IFM_IEEE80211_ADHOC, 0), 0);
264 ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_MANUAL, 0, 0), 0);
265 #undef ADD
266 ifmedia_set(&sc->sc_media, IFM_AUTOADHOC);
267
268 /*
269 * Call MI attach routines.
270 */
271 if_attach(ifp);
272 ether_ifattach(ifp, mac.wi_mac_addr);
273
274 ifp->if_baudrate = IF_Mbps(2);
275
276 /* Attach is successful. */
277 sc->sc_attached = 1;
278
279 splx(s);
280 return 0;
281 }
282
283 static void wi_rxeof(sc)
284 struct wi_softc *sc;
285 {
286 struct ifnet *ifp;
287 struct ether_header *eh;
288 struct wi_frame rx_frame;
289 struct mbuf *m;
290 int id;
291
292 ifp = sc->sc_ifp;
293
294 id = CSR_READ_2(sc, WI_RX_FID);
295
296 /* First read in the frame header */
297 if (wi_read_data(sc, id, 0, (caddr_t)&rx_frame, sizeof(rx_frame))) {
298 ifp->if_ierrors++;
299 return;
300 }
301
302 if (rx_frame.wi_status & WI_STAT_ERRSTAT) {
303 ifp->if_ierrors++;
304 return;
305 }
306
307 MGETHDR(m, M_DONTWAIT, MT_DATA);
308 if (m == NULL) {
309 ifp->if_ierrors++;
310 return;
311 }
312 MCLGET(m, M_DONTWAIT);
313 if (!(m->m_flags & M_EXT)) {
314 m_freem(m);
315 ifp->if_ierrors++;
316 return;
317 }
318
319 /* Align the data after the ethernet header */
320 m->m_data = (caddr_t) ALIGN(m->m_data + sizeof(struct ether_header))
321 - sizeof(struct ether_header);
322
323 eh = mtod(m, struct ether_header *);
324 m->m_pkthdr.rcvif = ifp;
325
326 if (rx_frame.wi_status == WI_STAT_1042 ||
327 rx_frame.wi_status == WI_STAT_TUNNEL ||
328 rx_frame.wi_status == WI_STAT_WMP_MSG) {
329 if((rx_frame.wi_dat_len + WI_SNAPHDR_LEN) > MCLBYTES) {
330 printf("%s: oversized packet received "
331 "(wi_dat_len=%d, wi_status=0x%x)\n",
332 sc->sc_dev.dv_xname,
333 rx_frame.wi_dat_len, rx_frame.wi_status);
334 m_freem(m);
335 ifp->if_ierrors++;
336 return;
337 }
338 m->m_pkthdr.len = m->m_len =
339 rx_frame.wi_dat_len + WI_SNAPHDR_LEN;
340
341 bcopy((char *)&rx_frame.wi_dst_addr,
342 (char *)&eh->ether_dhost, ETHER_ADDR_LEN);
343 bcopy((char *)&rx_frame.wi_src_addr,
344 (char *)&eh->ether_shost, ETHER_ADDR_LEN);
345 bcopy((char *)&rx_frame.wi_type,
346 (char *)&eh->ether_type, sizeof(u_int16_t));
347
348 if (wi_read_data(sc, id, WI_802_11_OFFSET,
349 mtod(m, caddr_t) + sizeof(struct ether_header),
350 m->m_len + 2)) {
351 m_freem(m);
352 ifp->if_ierrors++;
353 return;
354 }
355 } else {
356 if((rx_frame.wi_dat_len +
357 sizeof(struct ether_header)) > MCLBYTES) {
358 printf("%s: oversized packet received "
359 "(wi_dat_len=%d, wi_status=0x%x)\n",
360 sc->sc_dev.dv_xname,
361 rx_frame.wi_dat_len, rx_frame.wi_status);
362 m_freem(m);
363 ifp->if_ierrors++;
364 return;
365 }
366 m->m_pkthdr.len = m->m_len =
367 rx_frame.wi_dat_len + sizeof(struct ether_header);
368
369 if (wi_read_data(sc, id, WI_802_3_OFFSET,
370 mtod(m, caddr_t), m->m_len + 2)) {
371 m_freem(m);
372 ifp->if_ierrors++;
373 return;
374 }
375 }
376
377 ifp->if_ipackets++;
378
379 #if NBPFILTER > 0
380 /* Handle BPF listeners. */
381 if (ifp->if_bpf)
382 bpf_mtap(ifp->if_bpf, m);
383 #endif
384
385 /* Receive packet. */
386 (*ifp->if_input)(ifp, m);
387 }
388
389 static void wi_txeof(sc, status)
390 struct wi_softc *sc;
391 int status;
392 {
393 struct ifnet *ifp = sc->sc_ifp;
394
395 ifp->if_timer = 0;
396 ifp->if_flags &= ~IFF_OACTIVE;
397
398 if (status & WI_EV_TX_EXC)
399 ifp->if_oerrors++;
400 else
401 ifp->if_opackets++;
402
403 return;
404 }
405
406 void wi_inquire(xsc)
407 void *xsc;
408 {
409 struct wi_softc *sc;
410 struct ifnet *ifp;
411
412 sc = xsc;
413 ifp = &sc->sc_ethercom.ec_if;
414
415 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
416 return;
417
418 callout_reset(&sc->wi_inquire_ch, hz * 60, wi_inquire, sc);
419
420 /* Don't do this while we're transmitting */
421 if (ifp->if_flags & IFF_OACTIVE)
422 return;
423
424 wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_COUNTERS);
425
426 return;
427 }
428
429 void wi_update_stats(sc)
430 struct wi_softc *sc;
431 {
432 struct wi_ltv_gen gen;
433 u_int16_t id;
434 struct ifnet *ifp;
435 u_int32_t *ptr;
436 int len, i;
437 u_int16_t t;
438
439 ifp = &sc->sc_ethercom.ec_if;
440
441 id = CSR_READ_2(sc, WI_INFO_FID);
442
443 wi_read_data(sc, id, 0, (char *)&gen, 4);
444
445 if (gen.wi_type != WI_INFO_COUNTERS)
446 return;
447
448 /* some card versions have a larger stats structure */
449 len = (gen.wi_len - 1 < sizeof(sc->wi_stats) / 4) ?
450 gen.wi_len - 1 : sizeof(sc->wi_stats) / 4;
451 ptr = (u_int32_t *)&sc->wi_stats;
452
453 for (i = 0; i < len; i++) {
454 t = CSR_READ_2(sc, WI_DATA1);
455 #ifdef WI_HERMES_STATS_WAR
456 if (t > 0xF000)
457 t = ~t & 0xFFFF;
458 #endif
459 ptr[i] += t;
460 }
461
462 ifp->if_collisions = sc->wi_stats.wi_tx_single_retries +
463 sc->wi_stats.wi_tx_multi_retries +
464 sc->wi_stats.wi_tx_retry_limit;
465
466 return;
467 }
468
469 int wi_intr(arg)
470 void *arg;
471 {
472 struct wi_softc *sc = arg;
473 struct ifnet *ifp;
474 u_int16_t status;
475
476 if (sc->sc_enabled == 0 ||
477 (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 ||
478 (sc->sc_ethercom.ec_if.if_flags & IFF_RUNNING) == 0)
479 return (0);
480
481 ifp = &sc->sc_ethercom.ec_if;
482
483 if (!(ifp->if_flags & IFF_UP)) {
484 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
485 CSR_WRITE_2(sc, WI_INT_EN, 0);
486 return 1;
487 }
488
489 /* Disable interrupts. */
490 CSR_WRITE_2(sc, WI_INT_EN, 0);
491
492 status = CSR_READ_2(sc, WI_EVENT_STAT);
493 CSR_WRITE_2(sc, WI_EVENT_ACK, ~WI_INTRS);
494
495 if (status & WI_EV_RX) {
496 wi_rxeof(sc);
497 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
498 }
499
500 if (status & WI_EV_TX) {
501 wi_txeof(sc, status);
502 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX);
503 }
504
505 if (status & WI_EV_ALLOC) {
506 int id;
507 id = CSR_READ_2(sc, WI_ALLOC_FID);
508 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
509 if (id == sc->wi_tx_data_id)
510 wi_txeof(sc, status);
511 }
512
513 if (status & WI_EV_INFO) {
514 wi_update_stats(sc);
515 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
516 }
517
518 if (status & WI_EV_TX_EXC) {
519 wi_txeof(sc, status);
520 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
521 }
522
523 if (status & WI_EV_INFO_DROP) {
524 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO_DROP);
525 }
526
527 /* Re-enable interrupts. */
528 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
529
530 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
531 wi_start(ifp);
532
533 return 1;
534 }
535
536 static int
537 wi_cmd(sc, cmd, val)
538 struct wi_softc *sc;
539 int cmd;
540 int val;
541 {
542 int i, s = 0;
543
544 /* wait for the busy bit to clear */
545 for (i = 0; i < WI_TIMEOUT; i++) {
546 if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
547 break;
548 }
549
550 CSR_WRITE_2(sc, WI_PARAM0, val);
551 CSR_WRITE_2(sc, WI_PARAM1, 0);
552 CSR_WRITE_2(sc, WI_PARAM2, 0);
553 CSR_WRITE_2(sc, WI_COMMAND, cmd);
554
555 /* wait for the cmd completed bit */
556 for (i = 0; i < WI_TIMEOUT; i++) {
557 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
558 break;
559 DELAY(1);
560 }
561
562 /* Ack the command */
563 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
564
565 s = CSR_READ_2(sc, WI_STATUS);
566 if (s & WI_STAT_CMD_RESULT)
567 return(EIO);
568
569 if (i == WI_TIMEOUT)
570 return(ETIMEDOUT);
571
572 return(0);
573 }
574
575 static void
576 wi_reset(sc)
577 struct wi_softc *sc;
578 {
579 DELAY(100*1000); /* 100 m sec */
580 if (wi_cmd(sc, WI_CMD_INI, 0))
581 printf("%s: init failed\n", sc->sc_dev.dv_xname);
582 CSR_WRITE_2(sc, WI_INT_EN, 0);
583 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
584
585 /* Calibrate timer. */
586 WI_SETVAL(WI_RID_TICK_TIME, 8);
587
588 return;
589 }
590
591 /*
592 * Read an LTV record from the NIC.
593 */
594 static int wi_read_record(sc, ltv)
595 struct wi_softc *sc;
596 struct wi_ltv_gen *ltv;
597 {
598 u_int16_t *ptr;
599 int i, len, code;
600 struct wi_ltv_gen *oltv, p2ltv;
601
602 if (sc->sc_prism2) {
603 oltv = ltv;
604 switch (ltv->wi_type) {
605 case WI_RID_ENCRYPTION:
606 p2ltv.wi_type = WI_RID_P2_ENCRYPTION;
607 p2ltv.wi_len = 2;
608 ltv = &p2ltv;
609 break;
610 case WI_RID_TX_CRYPT_KEY:
611 p2ltv.wi_type = WI_RID_P2_TX_CRYPT_KEY;
612 p2ltv.wi_len = 2;
613 ltv = &p2ltv;
614 break;
615 }
616 }
617
618 /* Tell the NIC to enter record read mode. */
619 if (wi_cmd(sc, WI_CMD_ACCESS|WI_ACCESS_READ, ltv->wi_type))
620 return(EIO);
621
622 /* Seek to the record. */
623 if (wi_seek(sc, ltv->wi_type, 0, WI_BAP1))
624 return(EIO);
625
626 /*
627 * Read the length and record type and make sure they
628 * match what we expect (this verifies that we have enough
629 * room to hold all of the returned data).
630 */
631 len = CSR_READ_2(sc, WI_DATA1);
632 if (len > ltv->wi_len)
633 return(ENOSPC);
634 code = CSR_READ_2(sc, WI_DATA1);
635 if (code != ltv->wi_type)
636 return(EIO);
637
638 ltv->wi_len = len;
639 ltv->wi_type = code;
640
641 /* Now read the data. */
642 ptr = <v->wi_val;
643 for (i = 0; i < ltv->wi_len - 1; i++)
644 ptr[i] = CSR_READ_2(sc, WI_DATA1);
645
646 if (sc->sc_prism2) {
647 switch (oltv->wi_type) {
648 case WI_RID_TX_RATE:
649 case WI_RID_CUR_TX_RATE:
650 switch (ltv->wi_val) {
651 case 1: oltv->wi_val = 1; break;
652 case 2: oltv->wi_val = 2; break;
653 case 3: oltv->wi_val = 6; break;
654 case 4: oltv->wi_val = 5; break;
655 case 7: oltv->wi_val = 7; break;
656 case 8: oltv->wi_val = 11; break;
657 case 15: oltv->wi_val = 3; break;
658 default: oltv->wi_val = 0x100 + ltv->wi_val; break;
659 }
660 break;
661 case WI_RID_ENCRYPTION:
662 oltv->wi_len = 2;
663 if (ltv->wi_val & 0x01)
664 oltv->wi_val = 1;
665 else
666 oltv->wi_val = 0;
667 break;
668 case WI_RID_TX_CRYPT_KEY:
669 oltv->wi_len = 2;
670 oltv->wi_val = ltv->wi_val;
671 break;
672 }
673 }
674
675 return(0);
676 }
677
678 /*
679 * Same as read, except we inject data instead of reading it.
680 */
681 static int wi_write_record(sc, ltv)
682 struct wi_softc *sc;
683 struct wi_ltv_gen *ltv;
684 {
685 u_int16_t *ptr;
686 int i;
687 struct wi_ltv_gen p2ltv;
688
689 if (sc->sc_prism2) {
690 switch (ltv->wi_type) {
691 case WI_RID_TX_RATE:
692 p2ltv.wi_type = WI_RID_TX_RATE;
693 p2ltv.wi_len = 2;
694 switch (ltv->wi_val) {
695 case 1: p2ltv.wi_val = 1; break;
696 case 2: p2ltv.wi_val = 2; break;
697 case 3: p2ltv.wi_val = 15; break;
698 case 5: p2ltv.wi_val = 4; break;
699 case 6: p2ltv.wi_val = 3; break;
700 case 7: p2ltv.wi_val = 7; break;
701 case 11: p2ltv.wi_val = 8; break;
702 default: return EINVAL;
703 }
704 ltv = &p2ltv;
705 break;
706 case WI_RID_ENCRYPTION:
707 p2ltv.wi_type = WI_RID_P2_ENCRYPTION;
708 p2ltv.wi_len = 2;
709 if (ltv->wi_val)
710 p2ltv.wi_val = 0x03;
711 else
712 p2ltv.wi_val = 0x90;
713 ltv = &p2ltv;
714 break;
715 case WI_RID_TX_CRYPT_KEY:
716 p2ltv.wi_type = WI_RID_P2_TX_CRYPT_KEY;
717 p2ltv.wi_len = 2;
718 p2ltv.wi_val = ltv->wi_val;
719 ltv = &p2ltv;
720 break;
721 case WI_RID_DEFLT_CRYPT_KEYS:
722 {
723 int error;
724 struct wi_ltv_str ws;
725 struct wi_ltv_keys *wk = (struct wi_ltv_keys *)ltv;
726 for (i = 0; i < 4; i++) {
727 ws.wi_len = 4;
728 ws.wi_type = WI_RID_P2_CRYPT_KEY0 + i;
729 memcpy(ws.wi_str, &wk->wi_keys[i].wi_keydat, 5);
730 ws.wi_str[5] = '\0';
731 error = wi_write_record(sc,
732 (struct wi_ltv_gen *)&ws);
733 if (error)
734 return error;
735 }
736 return 0;
737 }
738 }
739 }
740
741 if (wi_seek(sc, ltv->wi_type, 0, WI_BAP1))
742 return(EIO);
743
744 CSR_WRITE_2(sc, WI_DATA1, ltv->wi_len);
745 CSR_WRITE_2(sc, WI_DATA1, ltv->wi_type);
746
747 /* Write data */
748 ptr = <v->wi_val;
749 for (i = 0; i < ltv->wi_len - 1; i++)
750 CSR_WRITE_2(sc, WI_DATA1, ptr[i]);
751
752 if (wi_cmd(sc, WI_CMD_ACCESS|WI_ACCESS_WRITE, ltv->wi_type))
753 return(EIO);
754
755 return(0);
756 }
757
758 static int wi_seek(sc, id, off, chan)
759 struct wi_softc *sc;
760 int id, off, chan;
761 {
762 int i;
763 int selreg, offreg;
764 int status;
765
766 switch (chan) {
767 case WI_BAP0:
768 selreg = WI_SEL0;
769 offreg = WI_OFF0;
770 break;
771 case WI_BAP1:
772 selreg = WI_SEL1;
773 offreg = WI_OFF1;
774 break;
775 default:
776 printf("%s: invalid data path: %x\n",
777 sc->sc_dev.dv_xname, chan);
778 return(EIO);
779 }
780
781 CSR_WRITE_2(sc, selreg, id);
782 CSR_WRITE_2(sc, offreg, off);
783
784 for (i = 0; i < WI_TIMEOUT; i++) {
785 status = CSR_READ_2(sc, offreg);
786 if (!(status & (WI_OFF_BUSY|WI_OFF_ERR)))
787 break;
788 }
789
790 if (i == WI_TIMEOUT) {
791 printf("%s: timeout in wi_seek to %x/%x; last status %x\n",
792 sc->sc_dev.dv_xname, id, off, status);
793 return(ETIMEDOUT);
794 }
795 return(0);
796 }
797
798 static int wi_read_data(sc, id, off, buf, len)
799 struct wi_softc *sc;
800 int id, off;
801 caddr_t buf;
802 int len;
803 {
804 int i;
805 u_int16_t *ptr;
806
807 if (wi_seek(sc, id, off, WI_BAP1))
808 return(EIO);
809
810 ptr = (u_int16_t *)buf;
811 for (i = 0; i < len / 2; i++)
812 ptr[i] = CSR_READ_2(sc, WI_DATA1);
813
814 return(0);
815 }
816
817 /*
818 * According to the comments in the HCF Light code, there is a bug in
819 * the Hermes (or possibly in certain Hermes firmware revisions) where
820 * the chip's internal autoincrement counter gets thrown off during
821 * data writes: the autoincrement is missed, causing one data word to
822 * be overwritten and subsequent words to be written to the wrong memory
823 * locations. The end result is that we could end up transmitting bogus
824 * frames without realizing it. The workaround for this is to write a
825 * couple of extra guard words after the end of the transfer, then
826 * attempt to read then back. If we fail to locate the guard words where
827 * we expect them, we preform the transfer over again.
828 */
829 static int wi_write_data(sc, id, off, buf, len)
830 struct wi_softc *sc;
831 int id, off;
832 caddr_t buf;
833 int len;
834 {
835 int i;
836 u_int16_t *ptr;
837
838 #ifdef WI_HERMES_AUTOINC_WAR
839 again:
840 #endif
841
842 if (wi_seek(sc, id, off, WI_BAP0))
843 return(EIO);
844
845 ptr = (u_int16_t *)buf;
846 for (i = 0; i < (len / 2); i++)
847 CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
848
849 #ifdef WI_HERMES_AUTOINC_WAR
850 CSR_WRITE_2(sc, WI_DATA0, 0x1234);
851 CSR_WRITE_2(sc, WI_DATA0, 0x5678);
852
853 if (wi_seek(sc, id, off + len, WI_BAP0))
854 return(EIO);
855
856 if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
857 CSR_READ_2(sc, WI_DATA0) != 0x5678)
858 goto again;
859 #endif
860
861 return(0);
862 }
863
864 /*
865 * Allocate a region of memory inside the NIC and zero
866 * it out.
867 */
868 static int wi_alloc_nicmem(sc, len, id)
869 struct wi_softc *sc;
870 int len;
871 int *id;
872 {
873 int i;
874
875 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len)) {
876 printf("%s: failed to allocate %d bytes on NIC\n",
877 sc->sc_dev.dv_xname, len);
878 return(ENOMEM);
879 }
880
881 for (i = 0; i < WI_TIMEOUT; i++) {
882 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
883 break;
884 }
885
886 if (i == WI_TIMEOUT) {
887 printf("%s: TIMED OUT in alloc\n", sc->sc_dev.dv_xname);
888 return(ETIMEDOUT);
889 }
890
891 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
892 *id = CSR_READ_2(sc, WI_ALLOC_FID);
893
894 if (wi_seek(sc, *id, 0, WI_BAP0)) {
895 printf("%s: seek failed in alloc\n", sc->sc_dev.dv_xname);
896 return(EIO);
897 }
898
899 for (i = 0; i < len / 2; i++)
900 CSR_WRITE_2(sc, WI_DATA0, 0);
901
902 return(0);
903 }
904
905 static void wi_setmulti(sc)
906 struct wi_softc *sc;
907 {
908 struct ifnet *ifp;
909 int i = 0;
910 struct wi_ltv_mcast mcast;
911 struct ether_multi *enm;
912 struct ether_multistep estep;
913 struct ethercom *ec = &sc->sc_ethercom;
914
915 ifp = &sc->sc_ethercom.ec_if;
916
917 if ((ifp->if_flags & IFF_PROMISC) != 0) {
918 allmulti:
919 ifp->if_flags |= IFF_ALLMULTI;
920 bzero((char *)&mcast, sizeof(mcast));
921 mcast.wi_type = WI_RID_MCAST;
922 mcast.wi_len = ((ETHER_ADDR_LEN / 2) * 16) + 1;
923
924 wi_write_record(sc, (struct wi_ltv_gen *)&mcast);
925 return;
926 }
927
928 i = 0;
929 ETHER_FIRST_MULTI(estep, ec, enm);
930 while (enm != NULL) {
931 /* Punt on ranges or too many multicast addresses. */
932 if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
933 ETHER_ADDR_LEN) != 0 ||
934 i >= 16)
935 goto allmulti;
936
937 bcopy(enm->enm_addrlo,
938 (char *)&mcast.wi_mcast[i], ETHER_ADDR_LEN);
939 i++;
940 ETHER_NEXT_MULTI(estep, enm);
941 }
942
943 ifp->if_flags &= ~IFF_ALLMULTI;
944 mcast.wi_type = WI_RID_MCAST;
945 mcast.wi_len = ((ETHER_ADDR_LEN / 2) * i) + 1;
946 wi_write_record(sc, (struct wi_ltv_gen *)&mcast);
947 }
948
949 static int
950 wi_setdef(sc, wreq)
951 struct wi_softc *sc;
952 struct wi_req *wreq;
953 {
954 struct sockaddr_dl *sdl;
955 struct ifnet *ifp;
956 int error = 0;
957
958 ifp = &sc->sc_ethercom.ec_if;
959
960 switch(wreq->wi_type) {
961 case WI_RID_MAC_NODE:
962 sdl = (struct sockaddr_dl *)ifp->if_sadl;
963 bcopy((char *)&wreq->wi_val, (char *)&sc->sc_macaddr,
964 ETHER_ADDR_LEN);
965 bcopy((char *)&wreq->wi_val, LLADDR(sdl), ETHER_ADDR_LEN);
966 break;
967 case WI_RID_PORTTYPE:
968 error = wi_sync_media(sc, wreq->wi_val[0], sc->wi_tx_rate);
969 break;
970 case WI_RID_TX_RATE:
971 error = wi_sync_media(sc, sc->wi_ptype, wreq->wi_val[0]);
972 break;
973 case WI_RID_MAX_DATALEN:
974 sc->wi_max_data_len = wreq->wi_val[0];
975 break;
976 case WI_RID_RTS_THRESH:
977 sc->wi_rts_thresh = wreq->wi_val[0];
978 break;
979 case WI_RID_SYSTEM_SCALE:
980 sc->wi_ap_density = wreq->wi_val[0];
981 break;
982 case WI_RID_CREATE_IBSS:
983 sc->wi_create_ibss = wreq->wi_val[0];
984 break;
985 case WI_RID_OWN_CHNL:
986 sc->wi_channel = wreq->wi_val[0];
987 break;
988 case WI_RID_NODENAME:
989 error = wi_set_ssid(&sc->wi_nodeid,
990 (u_int8_t *)&wreq->wi_val[1], wreq->wi_val[0]);
991 break;
992 case WI_RID_DESIRED_SSID:
993 error = wi_set_ssid(&sc->wi_netid,
994 (u_int8_t *)&wreq->wi_val[1], wreq->wi_val[0]);
995 break;
996 case WI_RID_OWN_SSID:
997 error = wi_set_ssid(&sc->wi_ibssid,
998 (u_int8_t *)&wreq->wi_val[1], wreq->wi_val[0]);
999 break;
1000 case WI_RID_PM_ENABLED:
1001 sc->wi_pm_enabled = wreq->wi_val[0];
1002 break;
1003 case WI_RID_MICROWAVE_OVEN:
1004 sc->wi_mor_enabled = wreq->wi_val[0];
1005 break;
1006 case WI_RID_MAX_SLEEP:
1007 sc->wi_max_sleep = wreq->wi_val[0];
1008 break;
1009 case WI_RID_ENCRYPTION:
1010 sc->wi_use_wep = wreq->wi_val[0];
1011 break;
1012 case WI_RID_TX_CRYPT_KEY:
1013 sc->wi_tx_key = wreq->wi_val[0];
1014 break;
1015 case WI_RID_DEFLT_CRYPT_KEYS:
1016 bcopy((char *)wreq, (char *)&sc->wi_keys,
1017 sizeof(struct wi_ltv_keys));
1018 break;
1019 default:
1020 error = EINVAL;
1021 break;
1022 }
1023
1024 return (error);
1025 }
1026
1027 static int
1028 wi_getdef(sc, wreq)
1029 struct wi_softc *sc;
1030 struct wi_req *wreq;
1031 {
1032 struct sockaddr_dl *sdl;
1033 struct ifnet *ifp;
1034 int error = 0;
1035
1036 ifp = &sc->sc_ethercom.ec_if;
1037
1038 wreq->wi_len = 2; /* XXX */
1039 switch (wreq->wi_type) {
1040 case WI_RID_MAC_NODE:
1041 wreq->wi_len += ETHER_ADDR_LEN / 2 - 1;
1042 sdl = (struct sockaddr_dl *)ifp->if_sadl;
1043 bcopy(&sc->sc_macaddr, &wreq->wi_val, ETHER_ADDR_LEN);
1044 bcopy(LLADDR(sdl), &wreq->wi_val, ETHER_ADDR_LEN);
1045 break;
1046 case WI_RID_PORTTYPE:
1047 wreq->wi_val[0] = sc->wi_ptype;
1048 break;
1049 case WI_RID_TX_RATE:
1050 wreq->wi_val[0] = sc->wi_tx_rate;
1051 break;
1052 case WI_RID_MAX_DATALEN:
1053 wreq->wi_val[0] = sc->wi_max_data_len;
1054 break;
1055 case WI_RID_RTS_THRESH:
1056 wreq->wi_val[0] = sc->wi_rts_thresh;
1057 break;
1058 case WI_RID_SYSTEM_SCALE:
1059 wreq->wi_val[0] = sc->wi_ap_density;
1060 break;
1061 case WI_RID_CREATE_IBSS:
1062 wreq->wi_val[0] = sc->wi_create_ibss;
1063 break;
1064 case WI_RID_OWN_CHNL:
1065 wreq->wi_val[0] = sc->wi_channel;
1066 break;
1067 case WI_RID_NODENAME:
1068 wi_request_fill_ssid(wreq, &sc->wi_nodeid);
1069 break;
1070 case WI_RID_DESIRED_SSID:
1071 wi_request_fill_ssid(wreq, &sc->wi_netid);
1072 break;
1073 case WI_RID_OWN_SSID:
1074 wi_request_fill_ssid(wreq, &sc->wi_ibssid);
1075 break;
1076 case WI_RID_PM_ENABLED:
1077 wreq->wi_val[0] = sc->wi_pm_enabled;
1078 break;
1079 case WI_RID_MICROWAVE_OVEN:
1080 wreq->wi_val[0] = sc->wi_mor_enabled;
1081 break;
1082 case WI_RID_MAX_SLEEP:
1083 wreq->wi_val[0] = sc->wi_max_sleep;
1084 break;
1085 case WI_RID_WEP_AVAIL:
1086 wreq->wi_val[0] = sc->wi_has_wep;
1087 break;
1088 case WI_RID_ENCRYPTION:
1089 wreq->wi_val[0] = sc->wi_use_wep;
1090 break;
1091 case WI_RID_TX_CRYPT_KEY:
1092 wreq->wi_val[0] = sc->wi_tx_key;
1093 break;
1094 case WI_RID_DEFLT_CRYPT_KEYS:
1095 wreq->wi_len += sizeof(struct wi_ltv_keys) / 2 - 1;
1096 bcopy(&sc->wi_keys, wreq, sizeof(struct wi_ltv_keys));
1097 break;
1098 default:
1099 #if 0
1100 error = EIO;
1101 #else
1102 #ifdef WI_DEBUG
1103 printf("%s: wi_getdef: unknown request %d\n",
1104 sc->sc_dev.dv_xname, wreq->wi_type);
1105 #endif
1106 #endif
1107 break;
1108 }
1109
1110 return (error);
1111 }
1112
1113 static int
1114 wi_ioctl(ifp, command, data)
1115 struct ifnet *ifp;
1116 u_long command;
1117 caddr_t data;
1118 {
1119 int s, error = 0;
1120 struct wi_softc *sc = ifp->if_softc;
1121 struct wi_req wreq;
1122 struct ifreq *ifr;
1123 struct proc *p = curproc;
1124 struct ieee80211_nwid nwid;
1125
1126 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
1127 return (ENXIO);
1128
1129 s = splnet();
1130
1131 ifr = (struct ifreq *)data;
1132 switch (command) {
1133 case SIOCSIFADDR:
1134 case SIOCGIFADDR:
1135 case SIOCSIFMTU:
1136 error = ether_ioctl(ifp, command, data);
1137 break;
1138 case SIOCSIFFLAGS:
1139 if (ifp->if_flags & IFF_UP) {
1140 if (ifp->if_flags & IFF_RUNNING &&
1141 ifp->if_flags & IFF_PROMISC &&
1142 !(sc->wi_if_flags & IFF_PROMISC)) {
1143 WI_SETVAL(WI_RID_PROMISC, 1);
1144 } else if (ifp->if_flags & IFF_RUNNING &&
1145 !(ifp->if_flags & IFF_PROMISC) &&
1146 sc->wi_if_flags & IFF_PROMISC) {
1147 WI_SETVAL(WI_RID_PROMISC, 0);
1148 }
1149 wi_init(ifp);
1150 } else {
1151 if (ifp->if_flags & IFF_RUNNING) {
1152 wi_stop(ifp, 0);
1153 }
1154 }
1155 sc->wi_if_flags = ifp->if_flags;
1156
1157 if (!(ifp->if_flags & IFF_UP)) {
1158 if (sc->sc_enabled) {
1159 if (sc->sc_disable)
1160 (*sc->sc_disable)(sc);
1161 sc->sc_enabled = 0;
1162 ifp->if_flags &= ~IFF_RUNNING;
1163 }
1164 }
1165 error = 0;
1166 break;
1167 case SIOCADDMULTI:
1168 case SIOCDELMULTI:
1169 error = (command == SIOCADDMULTI) ?
1170 ether_addmulti(ifr, &sc->sc_ethercom) :
1171 ether_delmulti(ifr, &sc->sc_ethercom);
1172 if (error == ENETRESET) {
1173 if (sc->sc_enabled != 0) {
1174 /*
1175 * Multicast list has changed. Set the
1176 * hardware filter accordingly.
1177 */
1178 wi_setmulti(sc);
1179 }
1180 error = 0;
1181 }
1182 break;
1183 case SIOCSIFMEDIA:
1184 case SIOCGIFMEDIA:
1185 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
1186 break;
1187 case SIOCGWAVELAN:
1188 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1189 if (error)
1190 break;
1191 if (wreq.wi_type == WI_RID_IFACE_STATS) {
1192 bcopy((char *)&sc->wi_stats, (char *)&wreq.wi_val,
1193 sizeof(sc->wi_stats));
1194 wreq.wi_len = (sizeof(sc->wi_stats) / 2) + 1;
1195 } else if (wreq.wi_type == WI_RID_DEFLT_CRYPT_KEYS) {
1196 /* For non-root user, return all-zeroes keys */
1197 if (suser(p->p_ucred, &p->p_acflag))
1198 bzero((char *)&wreq,
1199 sizeof(struct wi_ltv_keys));
1200 else
1201 bcopy((char *)&sc->wi_keys, (char *)&wreq,
1202 sizeof(struct wi_ltv_keys));
1203 } else {
1204 if (sc->sc_enabled == 0)
1205 error = wi_getdef(sc, &wreq);
1206 else if (wi_read_record(sc, (struct wi_ltv_gen *)&wreq))
1207 error = EINVAL;
1208 }
1209 if (error == 0)
1210 error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
1211 break;
1212 case SIOCSWAVELAN:
1213 error = suser(p->p_ucred, &p->p_acflag);
1214 if (error)
1215 break;
1216 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1217 if (error)
1218 break;
1219 if (wreq.wi_type == WI_RID_IFACE_STATS) {
1220 error = EINVAL;
1221 break;
1222 } else if (wreq.wi_type == WI_RID_MGMT_XMIT) {
1223 error = wi_mgmt_xmit(sc, (caddr_t)&wreq.wi_val,
1224 wreq.wi_len);
1225 } else {
1226 if (sc->sc_enabled != 0)
1227 error = wi_write_record(sc,
1228 (struct wi_ltv_gen *)&wreq);
1229 if (error == 0)
1230 error = wi_setdef(sc, &wreq);
1231 if (error == 0 && sc->sc_enabled != 0)
1232 /* Reinitialize WaveLAN. */
1233 wi_init(ifp);
1234 }
1235 break;
1236 case SIOCG80211NWID:
1237 if (sc->sc_enabled == 0) {
1238 /* Return the desired ID */
1239 error = copyout(&sc->wi_netid, ifr->ifr_data,
1240 sizeof(sc->wi_netid));
1241 } else {
1242 wreq.wi_type = WI_RID_CURRENT_SSID;
1243 wreq.wi_len = WI_MAX_DATALEN;
1244 if (wi_read_record(sc, (struct wi_ltv_gen *)&wreq) ||
1245 wreq.wi_val[0] > IEEE80211_NWID_LEN)
1246 error = EINVAL;
1247 else {
1248 wi_set_ssid(&nwid, (u_int8_t *)&wreq.wi_val[1],
1249 wreq.wi_val[0]);
1250 error = copyout(&nwid, ifr->ifr_data,
1251 sizeof(nwid));
1252 }
1253 }
1254 break;
1255 case SIOCS80211NWID:
1256 error = copyin(ifr->ifr_data, &nwid, sizeof(nwid));
1257 if (error != 0)
1258 break;
1259 if (nwid.i_len > IEEE80211_NWID_LEN) {
1260 error = EINVAL;
1261 break;
1262 }
1263 if (sc->wi_netid.i_len == nwid.i_len &&
1264 memcmp(sc->wi_netid.i_nwid, nwid.i_nwid, nwid.i_len) == 0)
1265 break;
1266 wi_set_ssid(&sc->wi_netid, nwid.i_nwid, nwid.i_len);
1267 if (sc->sc_enabled != 0)
1268 /* Reinitialize WaveLAN. */
1269 wi_init(ifp);
1270 break;
1271 case SIOCS80211NWKEY:
1272 error = wi_set_nwkey(sc, (struct ieee80211_nwkey *)data);
1273 break;
1274 case SIOCG80211NWKEY:
1275 error = wi_get_nwkey(sc, (struct ieee80211_nwkey *)data);
1276 break;
1277 case SIOCS80211POWER:
1278 error = wi_set_pm(sc, (struct ieee80211_power *)data);
1279 break;
1280 case SIOCG80211POWER:
1281 error = wi_get_pm(sc, (struct ieee80211_power *)data);
1282 break;
1283
1284 default:
1285 error = EINVAL;
1286 break;
1287 }
1288
1289 splx(s);
1290 return (error);
1291 }
1292
1293 static int
1294 wi_init(ifp)
1295 struct ifnet *ifp;
1296 {
1297 struct wi_softc *sc = ifp->if_softc;
1298 struct wi_req wreq;
1299 struct wi_ltv_macaddr mac;
1300 int error, id = 0;
1301
1302 if (!sc->sc_enabled) {
1303 if((error = (*sc->sc_enable)(sc)) != 0)
1304 goto out;
1305 sc->sc_enabled = 1;
1306 }
1307
1308 wi_stop(ifp, 0);
1309 wi_reset(sc);
1310
1311 /* Program max data length. */
1312 WI_SETVAL(WI_RID_MAX_DATALEN, sc->wi_max_data_len);
1313
1314 /* Enable/disable IBSS creation. */
1315 WI_SETVAL(WI_RID_CREATE_IBSS, sc->wi_create_ibss);
1316
1317 /* Set the port type. */
1318 WI_SETVAL(WI_RID_PORTTYPE, sc->wi_ptype);
1319
1320 /* Program the RTS/CTS threshold. */
1321 WI_SETVAL(WI_RID_RTS_THRESH, sc->wi_rts_thresh);
1322
1323 /* Program the TX rate */
1324 WI_SETVAL(WI_RID_TX_RATE, sc->wi_tx_rate);
1325
1326 /* Access point density */
1327 WI_SETVAL(WI_RID_SYSTEM_SCALE, sc->wi_ap_density);
1328
1329 /* Power Management Enabled */
1330 WI_SETVAL(WI_RID_PM_ENABLED, sc->wi_pm_enabled);
1331
1332 /* Power Managment Max Sleep */
1333 WI_SETVAL(WI_RID_MAX_SLEEP, sc->wi_max_sleep);
1334
1335 /* Specify the IBSS name */
1336 wi_write_ssid(sc, WI_RID_OWN_SSID, &wreq, &sc->wi_ibssid);
1337
1338 /* Specify the network name */
1339 wi_write_ssid(sc, WI_RID_DESIRED_SSID, &wreq, &sc->wi_netid);
1340
1341 /* Specify the frequency to use */
1342 WI_SETVAL(WI_RID_OWN_CHNL, sc->wi_channel);
1343
1344 /* Program the nodename. */
1345 wi_write_ssid(sc, WI_RID_NODENAME, &wreq, &sc->wi_nodeid);
1346
1347 /* Set our MAC address. */
1348 mac.wi_len = 4;
1349 mac.wi_type = WI_RID_MAC_NODE;
1350 memcpy(&mac.wi_mac_addr, sc->sc_macaddr, ETHER_ADDR_LEN);
1351 wi_write_record(sc, (struct wi_ltv_gen *)&mac);
1352
1353 /* Configure WEP. */
1354 if (sc->wi_has_wep) {
1355 WI_SETVAL(WI_RID_ENCRYPTION, sc->wi_use_wep);
1356 WI_SETVAL(WI_RID_TX_CRYPT_KEY, sc->wi_tx_key);
1357 sc->wi_keys.wi_len = (sizeof(struct wi_ltv_keys) / 2) + 1;
1358 sc->wi_keys.wi_type = WI_RID_DEFLT_CRYPT_KEYS;
1359 wi_write_record(sc, (struct wi_ltv_gen *)&sc->wi_keys);
1360 }
1361
1362 /* Initialize promisc mode. */
1363 if (ifp->if_flags & IFF_PROMISC) {
1364 WI_SETVAL(WI_RID_PROMISC, 1);
1365 } else {
1366 WI_SETVAL(WI_RID_PROMISC, 0);
1367 }
1368
1369 /* Set multicast filter. */
1370 wi_setmulti(sc);
1371
1372 /* Enable desired port */
1373 wi_cmd(sc, WI_CMD_ENABLE | sc->wi_portnum, 0);
1374
1375 if ((error = wi_alloc_nicmem(sc,
1376 1518 + sizeof(struct wi_frame) + 8, &id)) != 0) {
1377 printf("%s: tx buffer allocation failed\n",
1378 sc->sc_dev.dv_xname);
1379 goto out;
1380 }
1381 sc->wi_tx_data_id = id;
1382
1383 if ((error = wi_alloc_nicmem(sc,
1384 1518 + sizeof(struct wi_frame) + 8, &id)) != 0) {
1385 printf("%s: mgmt. buffer allocation failed\n",
1386 sc->sc_dev.dv_xname);
1387 goto out;
1388 }
1389 sc->wi_tx_mgmt_id = id;
1390
1391 /* Enable interrupts */
1392 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
1393
1394 ifp->if_flags |= IFF_RUNNING;
1395 ifp->if_flags &= ~IFF_OACTIVE;
1396
1397 callout_reset(&sc->wi_inquire_ch, hz * 60, wi_inquire, sc);
1398
1399 out:
1400 if (error) {
1401 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1402 ifp->if_timer = 0;
1403 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1404 }
1405 return (error);
1406 }
1407
1408 static void
1409 wi_start(ifp)
1410 struct ifnet *ifp;
1411 {
1412 struct wi_softc *sc;
1413 struct mbuf *m0;
1414 struct wi_frame tx_frame;
1415 struct ether_header *eh;
1416 int id;
1417
1418 sc = ifp->if_softc;
1419
1420 if (ifp->if_flags & IFF_OACTIVE)
1421 return;
1422
1423 IFQ_DEQUEUE(&ifp->if_snd, m0);
1424 if (m0 == NULL)
1425 return;
1426
1427 bzero((char *)&tx_frame, sizeof(tx_frame));
1428 id = sc->wi_tx_data_id;
1429 eh = mtod(m0, struct ether_header *);
1430
1431 /*
1432 * Use RFC1042 encoding for IP and ARP datagrams,
1433 * 802.3 for anything else.
1434 */
1435 if (ntohs(eh->ether_type) == ETHERTYPE_IP ||
1436 ntohs(eh->ether_type) == ETHERTYPE_ARP ||
1437 ntohs(eh->ether_type) == ETHERTYPE_REVARP ||
1438 ntohs(eh->ether_type) == ETHERTYPE_IPV6) {
1439 bcopy((char *)&eh->ether_dhost,
1440 (char *)&tx_frame.wi_addr1, ETHER_ADDR_LEN);
1441 bcopy((char *)&eh->ether_shost,
1442 (char *)&tx_frame.wi_addr2, ETHER_ADDR_LEN);
1443 bcopy((char *)&eh->ether_dhost,
1444 (char *)&tx_frame.wi_dst_addr, ETHER_ADDR_LEN);
1445 bcopy((char *)&eh->ether_shost,
1446 (char *)&tx_frame.wi_src_addr, ETHER_ADDR_LEN);
1447
1448 tx_frame.wi_dat_len = m0->m_pkthdr.len - WI_SNAPHDR_LEN;
1449 tx_frame.wi_frame_ctl = WI_FTYPE_DATA;
1450 tx_frame.wi_dat[0] = htons(WI_SNAP_WORD0);
1451 tx_frame.wi_dat[1] = htons(WI_SNAP_WORD1);
1452 tx_frame.wi_len = htons(m0->m_pkthdr.len - WI_SNAPHDR_LEN);
1453 tx_frame.wi_type = eh->ether_type;
1454
1455 m_copydata(m0, sizeof(struct ether_header),
1456 m0->m_pkthdr.len - sizeof(struct ether_header),
1457 (caddr_t)&sc->wi_txbuf);
1458
1459 wi_write_data(sc, id, 0, (caddr_t)&tx_frame,
1460 sizeof(struct wi_frame));
1461 wi_write_data(sc, id, WI_802_11_OFFSET, (caddr_t)&sc->wi_txbuf,
1462 (m0->m_pkthdr.len - sizeof(struct ether_header)) + 2);
1463 } else {
1464 tx_frame.wi_dat_len = m0->m_pkthdr.len;
1465
1466 m_copydata(m0, 0, m0->m_pkthdr.len, (caddr_t)&sc->wi_txbuf);
1467
1468 wi_write_data(sc, id, 0, (caddr_t)&tx_frame,
1469 sizeof(struct wi_frame));
1470 wi_write_data(sc, id, WI_802_3_OFFSET, (caddr_t)&sc->wi_txbuf,
1471 m0->m_pkthdr.len + 2);
1472 }
1473
1474 #if NBPFILTER > 0
1475 /*
1476 * If there's a BPF listener, bounce a copy of
1477 * this frame to him.
1478 */
1479 if (ifp->if_bpf)
1480 bpf_mtap(ifp->if_bpf, m0);
1481 #endif
1482
1483 m_freem(m0);
1484
1485 if (wi_cmd(sc, WI_CMD_TX|WI_RECLAIM, id))
1486 printf("%s: xmit failed\n", sc->sc_dev.dv_xname);
1487
1488 ifp->if_flags |= IFF_OACTIVE;
1489
1490 /*
1491 * Set a timeout in case the chip goes out to lunch.
1492 */
1493 ifp->if_timer = 5;
1494
1495 return;
1496 }
1497
1498 static int
1499 wi_mgmt_xmit(sc, data, len)
1500 struct wi_softc *sc;
1501 caddr_t data;
1502 int len;
1503 {
1504 struct wi_frame tx_frame;
1505 int id;
1506 struct wi_80211_hdr *hdr;
1507 caddr_t dptr;
1508
1509 hdr = (struct wi_80211_hdr *)data;
1510 dptr = data + sizeof(struct wi_80211_hdr);
1511
1512 bzero((char *)&tx_frame, sizeof(tx_frame));
1513 id = sc->wi_tx_mgmt_id;
1514
1515 bcopy((char *)hdr, (char *)&tx_frame.wi_frame_ctl,
1516 sizeof(struct wi_80211_hdr));
1517
1518 tx_frame.wi_dat_len = len - WI_SNAPHDR_LEN;
1519 tx_frame.wi_len = htons(len - WI_SNAPHDR_LEN);
1520
1521 wi_write_data(sc, id, 0, (caddr_t)&tx_frame, sizeof(struct wi_frame));
1522 wi_write_data(sc, id, WI_802_11_OFFSET_RAW, dptr,
1523 (len - sizeof(struct wi_80211_hdr)) + 2);
1524
1525 if (wi_cmd(sc, WI_CMD_TX|WI_RECLAIM, id)) {
1526 printf("%s: xmit failed\n", sc->sc_dev.dv_xname);
1527 return(EIO);
1528 }
1529
1530 return(0);
1531 }
1532
1533 static void
1534 wi_stop(ifp, disable)
1535 struct ifnet *ifp;
1536 {
1537 struct wi_softc *sc = ifp->if_softc;
1538
1539 CSR_WRITE_2(sc, WI_INT_EN, 0);
1540 wi_cmd(sc, WI_CMD_DISABLE|sc->wi_portnum, 0);
1541
1542 callout_stop(&sc->wi_inquire_ch);
1543
1544 if (disable) {
1545 if (sc->sc_disable)
1546 (*sc->sc_disable)(sc);
1547 sc->sc_enabled = 0;
1548 }
1549
1550 ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
1551 ifp->if_timer = 0;
1552 }
1553
1554 static void
1555 wi_watchdog(ifp)
1556 struct ifnet *ifp;
1557 {
1558 struct wi_softc *sc;
1559
1560 sc = ifp->if_softc;
1561
1562 printf("%s: device timeout\n", sc->sc_dev.dv_xname);
1563
1564 wi_init(ifp);
1565
1566 ifp->if_oerrors++;
1567
1568 return;
1569 }
1570
1571 void
1572 wi_shutdown(sc)
1573 struct wi_softc *sc;
1574 {
1575 int s;
1576
1577 s = splnet();
1578 if (sc->sc_enabled) {
1579 if (sc->sc_disable)
1580 (*sc->sc_disable)(sc);
1581 sc->sc_enabled = 0;
1582 }
1583 splx(s);
1584 }
1585
1586 int
1587 wi_activate(self, act)
1588 struct device *self;
1589 enum devact act;
1590 {
1591 struct wi_softc *sc = (struct wi_softc *)self;
1592 int rv = 0, s;
1593
1594 s = splnet();
1595 switch (act) {
1596 case DVACT_ACTIVATE:
1597 rv = EOPNOTSUPP;
1598 break;
1599
1600 case DVACT_DEACTIVATE:
1601 if_deactivate(&sc->sc_ethercom.ec_if);
1602 break;
1603 }
1604 splx(s);
1605 return (rv);
1606 }
1607
1608 int
1609 wi_detach(sc)
1610 struct wi_softc *sc;
1611 {
1612 struct ifnet *ifp = sc->sc_ifp;
1613 int s;
1614
1615 if (!sc->sc_attached)
1616 return (0);
1617
1618 s = splnet();
1619 callout_stop(&sc->wi_inquire_ch);
1620
1621 /* Delete all remaining media. */
1622 ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY);
1623
1624 ether_ifdetach(ifp);
1625 if_detach(ifp);
1626 if (sc->sc_enabled) {
1627 if (sc->sc_disable)
1628 (*sc->sc_disable)(sc);
1629 sc->sc_enabled = 0;
1630 }
1631 splx(s);
1632 return (0);
1633 }
1634
1635 void
1636 wi_power(sc, why)
1637 struct wi_softc *sc;
1638 int why;
1639 {
1640 int s;
1641
1642 if (!sc->sc_enabled)
1643 return;
1644
1645 s = splnet();
1646 switch (why) {
1647 case PWR_SUSPEND:
1648 case PWR_STANDBY:
1649 wi_stop(sc->sc_ifp, 0);
1650 if (sc->sc_disable)
1651 (*sc->sc_disable)(sc);
1652 break;
1653 case PWR_RESUME:
1654 sc->sc_enabled = 0;
1655 wi_init(sc->sc_ifp);
1656 (void)wi_intr(sc);
1657 break;
1658 case PWR_SOFTSUSPEND:
1659 case PWR_SOFTSTANDBY:
1660 case PWR_SOFTRESUME:
1661 break;
1662 }
1663 splx(s);
1664 }
1665
1666 static int
1667 wi_set_ssid(ws, id, len)
1668 struct ieee80211_nwid *ws;
1669 u_int8_t *id;
1670 int len;
1671 {
1672
1673 if (len > IEEE80211_NWID_LEN)
1674 return (EINVAL);
1675 ws->i_len = len;
1676 memcpy(ws->i_nwid, id, len);
1677 return (0);
1678 }
1679
1680 static void
1681 wi_request_fill_ssid(wreq, ws)
1682 struct wi_req *wreq;
1683 struct ieee80211_nwid *ws;
1684 {
1685
1686 memset(&wreq->wi_val[0], 0, sizeof(wreq->wi_val));
1687 wreq->wi_val[0] = ws->i_len;
1688 wreq->wi_len = roundup(wreq->wi_val[0], 2) / 2 + 2;
1689 memcpy(&wreq->wi_val[1], ws->i_nwid, wreq->wi_val[0]);
1690 }
1691
1692 static int
1693 wi_write_ssid(sc, type, wreq, ws)
1694 struct wi_softc *sc;
1695 int type;
1696 struct wi_req *wreq;
1697 struct ieee80211_nwid *ws;
1698 {
1699
1700 wreq->wi_type = type;
1701 wi_request_fill_ssid(wreq, ws);
1702 return (wi_write_record(sc, (struct wi_ltv_gen *)wreq));
1703 }
1704
1705 static int
1706 wi_sync_media(sc, ptype, txrate)
1707 struct wi_softc *sc;
1708 int ptype;
1709 int txrate;
1710 {
1711 int media = sc->sc_media.ifm_cur->ifm_media;
1712 int options = IFM_OPTIONS(media);
1713 int subtype;
1714
1715 switch (txrate) {
1716 case 1:
1717 subtype = IFM_IEEE80211_DS1;
1718 break;
1719 case 2:
1720 subtype = IFM_IEEE80211_DS2;
1721 break;
1722 case 3:
1723 subtype = IFM_AUTO;
1724 break;
1725 case 11:
1726 subtype = IFM_IEEE80211_DS11;
1727 break;
1728 default:
1729 subtype = IFM_MANUAL; /* Unable to represent */
1730 break;
1731 }
1732 switch (ptype) {
1733 case WI_PORTTYPE_ADHOC:
1734 options |= IFM_IEEE80211_ADHOC;
1735 break;
1736 case WI_PORTTYPE_BSS:
1737 options &= ~IFM_IEEE80211_ADHOC;
1738 break;
1739 default:
1740 subtype = IFM_MANUAL; /* Unable to represent */
1741 break;
1742 }
1743 media = IFM_MAKEWORD(IFM_TYPE(media), subtype, options,
1744 IFM_INST(media));
1745 if (ifmedia_match(&sc->sc_media, media, sc->sc_media.ifm_mask) == NULL)
1746 return (EINVAL);
1747 ifmedia_set(&sc->sc_media, media);
1748 sc->wi_ptype = ptype;
1749 sc->wi_tx_rate = txrate;
1750 return (0);
1751 }
1752
1753 static int
1754 wi_media_change(ifp)
1755 struct ifnet *ifp;
1756 {
1757 struct wi_softc *sc = ifp->if_softc;
1758 int otype = sc->wi_ptype;
1759 int orate = sc->wi_tx_rate;
1760
1761 if ((sc->sc_media.ifm_cur->ifm_media & IFM_IEEE80211_ADHOC) != 0)
1762 sc->wi_ptype = WI_PORTTYPE_ADHOC;
1763 else
1764 sc->wi_ptype = WI_PORTTYPE_BSS;
1765
1766 switch (IFM_SUBTYPE(sc->sc_media.ifm_cur->ifm_media)) {
1767 case IFM_IEEE80211_DS1:
1768 sc->wi_tx_rate = 1;
1769 break;
1770 case IFM_IEEE80211_DS2:
1771 sc->wi_tx_rate = 2;
1772 break;
1773 case IFM_AUTO:
1774 sc->wi_tx_rate = 3;
1775 break;
1776 case IFM_IEEE80211_DS11:
1777 sc->wi_tx_rate = 11;
1778 break;
1779 }
1780
1781 if (sc->sc_enabled != 0) {
1782 if (otype != sc->wi_ptype ||
1783 orate != sc->wi_tx_rate)
1784 wi_init(ifp);
1785 }
1786
1787 ifp->if_baudrate = ifmedia_baudrate(sc->sc_media.ifm_cur->ifm_media);
1788
1789 return (0);
1790 }
1791
1792 static void
1793 wi_media_status(ifp, imr)
1794 struct ifnet *ifp;
1795 struct ifmediareq *imr;
1796 {
1797 struct wi_softc *sc = ifp->if_softc;
1798
1799 if (sc->sc_enabled == 0) {
1800 imr->ifm_active = IFM_IEEE80211|IFM_NONE;
1801 imr->ifm_status = 0;
1802 return;
1803 }
1804
1805 imr->ifm_active = sc->sc_media.ifm_cur->ifm_media;
1806 imr->ifm_status = IFM_AVALID|IFM_ACTIVE;
1807 }
1808
1809 static int
1810 wi_set_nwkey(sc, nwkey)
1811 struct wi_softc *sc;
1812 struct ieee80211_nwkey *nwkey;
1813 {
1814 int i, len, error;
1815 struct wi_req wreq;
1816 struct wi_ltv_keys *wk = (struct wi_ltv_keys *)&wreq;
1817
1818 if (!sc->wi_has_wep)
1819 return ENODEV;
1820 if (nwkey->i_defkid <= 0 ||
1821 nwkey->i_defkid > IEEE80211_WEP_NKID)
1822 return EINVAL;
1823 memcpy(wk, &sc->wi_keys, sizeof(*wk));
1824 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1825 if (nwkey->i_key[i].i_keydat == NULL)
1826 continue;
1827 len = nwkey->i_key[i].i_keylen;
1828 if (len > sizeof(wk->wi_keys[i].wi_keydat))
1829 return EINVAL;
1830 error = copyin(nwkey->i_key[i].i_keydat,
1831 wk->wi_keys[i].wi_keydat, len);
1832 if (error)
1833 return error;
1834 wk->wi_keys[i].wi_keylen = len;
1835 }
1836
1837 wk->wi_len = (sizeof(*wk) / 2) + 1;
1838 wk->wi_type = WI_RID_DEFLT_CRYPT_KEYS;
1839 if (sc->sc_enabled != 0) {
1840 error = wi_write_record(sc, (struct wi_ltv_gen *)&wreq);
1841 if (error)
1842 return error;
1843 }
1844 error = wi_setdef(sc, &wreq);
1845 if (error)
1846 return error;
1847
1848 wreq.wi_len = 2;
1849 wreq.wi_type = WI_RID_TX_CRYPT_KEY;
1850 wreq.wi_val[0] = nwkey->i_defkid - 1;
1851 if (sc->sc_enabled != 0) {
1852 error = wi_write_record(sc, (struct wi_ltv_gen *)&wreq);
1853 if (error)
1854 return error;
1855 }
1856 error = wi_setdef(sc, &wreq);
1857 if (error)
1858 return error;
1859
1860 wreq.wi_type = WI_RID_ENCRYPTION;
1861 wreq.wi_val[0] = nwkey->i_wepon;
1862 if (sc->sc_enabled != 0) {
1863 error = wi_write_record(sc, (struct wi_ltv_gen *)&wreq);
1864 if (error)
1865 return error;
1866 }
1867 error = wi_setdef(sc, &wreq);
1868 if (error)
1869 return error;
1870
1871 if (sc->sc_enabled != 0)
1872 wi_init(&sc->sc_ethercom.ec_if);
1873 return 0;
1874 }
1875
1876 static int
1877 wi_get_nwkey(sc, nwkey)
1878 struct wi_softc *sc;
1879 struct ieee80211_nwkey *nwkey;
1880 {
1881 int i, len, error;
1882 struct wi_ltv_keys *wk = &sc->wi_keys;
1883
1884 if (!sc->wi_has_wep)
1885 return ENODEV;
1886 nwkey->i_wepon = sc->wi_use_wep;
1887 nwkey->i_defkid = sc->wi_tx_key + 1;
1888
1889 /* do not show any keys to non-root user */
1890 error = suser(curproc->p_ucred, &curproc->p_acflag);
1891 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1892 if (nwkey->i_key[i].i_keydat == NULL)
1893 continue;
1894 /* error holds results of suser() for the first time */
1895 if (error)
1896 return error;
1897 len = wk->wi_keys[i].wi_keylen;
1898 if (nwkey->i_key[i].i_keylen < len)
1899 return ENOSPC;
1900 nwkey->i_key[i].i_keylen = len;
1901 error = copyout(wk->wi_keys[i].wi_keydat,
1902 nwkey->i_key[i].i_keydat, len);
1903 if (error)
1904 return error;
1905 }
1906 return 0;
1907 }
1908
1909 static int
1910 wi_set_pm(struct wi_softc *sc, struct ieee80211_power *power)
1911 {
1912
1913 sc->wi_pm_enabled = power->i_enabled;
1914 sc->wi_max_sleep = power->i_maxsleep;
1915
1916 if (sc->sc_enabled)
1917 return (wi_init(&sc->sc_ethercom.ec_if));
1918
1919 return (0);
1920 }
1921
1922 static int
1923 wi_get_pm(struct wi_softc *sc, struct ieee80211_power *power)
1924 {
1925
1926 power->i_enabled = sc->wi_pm_enabled;
1927 power->i_maxsleep = sc->wi_max_sleep;
1928
1929 return (0);
1930 }
1931