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