wi.c revision 1.1 1 /* $NetBSD: wi.c,v 1.1 2001/05/06 03:26:39 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(10);
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 (sc->sc_disable)
1141 (*sc->sc_disable)(sc);
1142 sc->sc_enabled = 0;
1143 ifp->if_flags &= ~IFF_RUNNING;
1144 }
1145 break;
1146 case SIOCADDMULTI:
1147 case SIOCDELMULTI:
1148 error = (command == SIOCADDMULTI) ?
1149 ether_addmulti(ifr, &sc->sc_ethercom) :
1150 ether_delmulti(ifr, &sc->sc_ethercom);
1151 if (error == ENETRESET) {
1152 if (sc->sc_enabled != 0) {
1153 /*
1154 * Multicast list has changed. Set the
1155 * hardware filter accordingly.
1156 */
1157 wi_setmulti(sc);
1158 }
1159 error = 0;
1160 }
1161 break;
1162 case SIOCSIFMEDIA:
1163 case SIOCGIFMEDIA:
1164 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
1165 break;
1166 case SIOCGWAVELAN:
1167 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1168 if (error)
1169 break;
1170 if (wreq.wi_type == WI_RID_IFACE_STATS) {
1171 bcopy((char *)&sc->wi_stats, (char *)&wreq.wi_val,
1172 sizeof(sc->wi_stats));
1173 wreq.wi_len = (sizeof(sc->wi_stats) / 2) + 1;
1174 } else if (wreq.wi_type == WI_RID_DEFLT_CRYPT_KEYS) {
1175 /* For non-root user, return all-zeroes keys */
1176 if (suser(p->p_ucred, &p->p_acflag))
1177 bzero((char *)&wreq,
1178 sizeof(struct wi_ltv_keys));
1179 else
1180 bcopy((char *)&sc->wi_keys, (char *)&wreq,
1181 sizeof(struct wi_ltv_keys));
1182 } else {
1183 if (sc->sc_enabled == 0)
1184 error = wi_getdef(sc, &wreq);
1185 else if (wi_read_record(sc, (struct wi_ltv_gen *)&wreq))
1186 error = EINVAL;
1187 }
1188 if (error == 0)
1189 error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
1190 break;
1191 case SIOCSWAVELAN:
1192 error = suser(p->p_ucred, &p->p_acflag);
1193 if (error)
1194 break;
1195 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1196 if (error)
1197 break;
1198 if (wreq.wi_type == WI_RID_IFACE_STATS) {
1199 error = EINVAL;
1200 break;
1201 } else if (wreq.wi_type == WI_RID_MGMT_XMIT) {
1202 error = wi_mgmt_xmit(sc, (caddr_t)&wreq.wi_val,
1203 wreq.wi_len);
1204 } else {
1205 if (sc->sc_enabled != 0)
1206 error = wi_write_record(sc,
1207 (struct wi_ltv_gen *)&wreq);
1208 if (error == 0)
1209 error = wi_setdef(sc, &wreq);
1210 if (error == 0 && sc->sc_enabled != 0)
1211 /* Reinitialize WaveLAN. */
1212 wi_init(ifp);
1213 }
1214 break;
1215 case SIOCG80211NWID:
1216 if (sc->sc_enabled == 0) {
1217 /* Return the desired ID */
1218 error = copyout(&sc->wi_netid, ifr->ifr_data,
1219 sizeof(sc->wi_netid));
1220 } else {
1221 wreq.wi_type = WI_RID_CURRENT_SSID;
1222 wreq.wi_len = WI_MAX_DATALEN;
1223 if (wi_read_record(sc, (struct wi_ltv_gen *)&wreq) ||
1224 wreq.wi_val[0] > IEEE80211_NWID_LEN)
1225 error = EINVAL;
1226 else {
1227 wi_set_ssid(&nwid, (u_int8_t *)&wreq.wi_val[1],
1228 wreq.wi_val[0]);
1229 error = copyout(&nwid, ifr->ifr_data,
1230 sizeof(nwid));
1231 }
1232 }
1233 break;
1234 case SIOCS80211NWID:
1235 error = copyin(ifr->ifr_data, &nwid, sizeof(nwid));
1236 if (error != 0)
1237 break;
1238 if (nwid.i_len > IEEE80211_NWID_LEN) {
1239 error = EINVAL;
1240 break;
1241 }
1242 if (sc->wi_netid.i_len == nwid.i_len &&
1243 memcmp(sc->wi_netid.i_nwid, nwid.i_nwid, nwid.i_len) == 0)
1244 break;
1245 wi_set_ssid(&sc->wi_netid, nwid.i_nwid, nwid.i_len);
1246 if (sc->sc_enabled != 0)
1247 /* Reinitialize WaveLAN. */
1248 wi_init(ifp);
1249 break;
1250 case SIOCS80211NWKEY:
1251 error = wi_set_nwkey(sc, (struct ieee80211_nwkey *)data);
1252 break;
1253 case SIOCG80211NWKEY:
1254 error = wi_get_nwkey(sc, (struct ieee80211_nwkey *)data);
1255 break;
1256 case SIOCS80211POWER:
1257 error = wi_set_pm(sc, (struct ieee80211_power *)data);
1258 break;
1259 case SIOCG80211POWER:
1260 error = wi_get_pm(sc, (struct ieee80211_power *)data);
1261 break;
1262
1263 default:
1264 error = EINVAL;
1265 break;
1266 }
1267
1268 splx(s);
1269 return (error);
1270 }
1271
1272 static int
1273 wi_init(ifp)
1274 struct ifnet *ifp;
1275 {
1276 struct wi_softc *sc = ifp->if_softc;
1277 struct wi_req wreq;
1278 struct wi_ltv_macaddr mac;
1279 int error, id = 0;
1280
1281 if (!sc->sc_enabled) {
1282 if((error = (*sc->sc_enable)(sc)) != 0)
1283 goto out;
1284 sc->sc_enabled = 1;
1285 }
1286
1287 wi_stop(ifp, 0);
1288 wi_reset(sc);
1289
1290 /* Program max data length. */
1291 WI_SETVAL(WI_RID_MAX_DATALEN, sc->wi_max_data_len);
1292
1293 /* Enable/disable IBSS creation. */
1294 WI_SETVAL(WI_RID_CREATE_IBSS, sc->wi_create_ibss);
1295
1296 /* Set the port type. */
1297 WI_SETVAL(WI_RID_PORTTYPE, sc->wi_ptype);
1298
1299 /* Program the RTS/CTS threshold. */
1300 WI_SETVAL(WI_RID_RTS_THRESH, sc->wi_rts_thresh);
1301
1302 /* Program the TX rate */
1303 WI_SETVAL(WI_RID_TX_RATE, sc->wi_tx_rate);
1304
1305 /* Access point density */
1306 WI_SETVAL(WI_RID_SYSTEM_SCALE, sc->wi_ap_density);
1307
1308 /* Power Management Enabled */
1309 WI_SETVAL(WI_RID_PM_ENABLED, sc->wi_pm_enabled);
1310
1311 /* Power Managment Max Sleep */
1312 WI_SETVAL(WI_RID_MAX_SLEEP, sc->wi_max_sleep);
1313
1314 /* Specify the IBSS name */
1315 wi_write_ssid(sc, WI_RID_OWN_SSID, &wreq, &sc->wi_ibssid);
1316
1317 /* Specify the network name */
1318 wi_write_ssid(sc, WI_RID_DESIRED_SSID, &wreq, &sc->wi_netid);
1319
1320 /* Specify the frequency to use */
1321 WI_SETVAL(WI_RID_OWN_CHNL, sc->wi_channel);
1322
1323 /* Program the nodename. */
1324 wi_write_ssid(sc, WI_RID_NODENAME, &wreq, &sc->wi_nodeid);
1325
1326 /* Set our MAC address. */
1327 mac.wi_len = 4;
1328 mac.wi_type = WI_RID_MAC_NODE;
1329 memcpy(&mac.wi_mac_addr, sc->sc_macaddr, ETHER_ADDR_LEN);
1330 wi_write_record(sc, (struct wi_ltv_gen *)&mac);
1331
1332 /* Configure WEP. */
1333 if (sc->wi_has_wep) {
1334 WI_SETVAL(WI_RID_ENCRYPTION, sc->wi_use_wep);
1335 WI_SETVAL(WI_RID_TX_CRYPT_KEY, sc->wi_tx_key);
1336 sc->wi_keys.wi_len = (sizeof(struct wi_ltv_keys) / 2) + 1;
1337 sc->wi_keys.wi_type = WI_RID_DEFLT_CRYPT_KEYS;
1338 wi_write_record(sc, (struct wi_ltv_gen *)&sc->wi_keys);
1339 }
1340
1341 /* Initialize promisc mode. */
1342 if (ifp->if_flags & IFF_PROMISC) {
1343 WI_SETVAL(WI_RID_PROMISC, 1);
1344 } else {
1345 WI_SETVAL(WI_RID_PROMISC, 0);
1346 }
1347
1348 /* Set multicast filter. */
1349 wi_setmulti(sc);
1350
1351 /* Enable desired port */
1352 wi_cmd(sc, WI_CMD_ENABLE | sc->wi_portnum, 0);
1353
1354 if ((error = wi_alloc_nicmem(sc,
1355 1518 + sizeof(struct wi_frame) + 8, &id)) != 0) {
1356 printf("%s: tx buffer allocation failed\n",
1357 sc->sc_dev.dv_xname);
1358 goto out;
1359 }
1360 sc->wi_tx_data_id = id;
1361
1362 if ((error = wi_alloc_nicmem(sc,
1363 1518 + sizeof(struct wi_frame) + 8, &id)) != 0) {
1364 printf("%s: mgmt. buffer allocation failed\n",
1365 sc->sc_dev.dv_xname);
1366 goto out;
1367 }
1368 sc->wi_tx_mgmt_id = id;
1369
1370 /* Enable interrupts */
1371 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
1372
1373 ifp->if_flags |= IFF_RUNNING;
1374 ifp->if_flags &= ~IFF_OACTIVE;
1375
1376 callout_reset(&sc->wi_inquire_ch, hz * 60, wi_inquire, sc);
1377
1378 out:
1379 if (error) {
1380 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1381 ifp->if_timer = 0;
1382 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1383 }
1384 return (error);
1385 }
1386
1387 static void
1388 wi_start(ifp)
1389 struct ifnet *ifp;
1390 {
1391 struct wi_softc *sc;
1392 struct mbuf *m0;
1393 struct wi_frame tx_frame;
1394 struct ether_header *eh;
1395 int id;
1396
1397 sc = ifp->if_softc;
1398
1399 if (ifp->if_flags & IFF_OACTIVE)
1400 return;
1401
1402 IFQ_DEQUEUE(&ifp->if_snd, m0);
1403 if (m0 == NULL)
1404 return;
1405
1406 bzero((char *)&tx_frame, sizeof(tx_frame));
1407 id = sc->wi_tx_data_id;
1408 eh = mtod(m0, struct ether_header *);
1409
1410 /*
1411 * Use RFC1042 encoding for IP and ARP datagrams,
1412 * 802.3 for anything else.
1413 */
1414 if (ntohs(eh->ether_type) == ETHERTYPE_IP ||
1415 ntohs(eh->ether_type) == ETHERTYPE_ARP ||
1416 ntohs(eh->ether_type) == ETHERTYPE_REVARP ||
1417 ntohs(eh->ether_type) == ETHERTYPE_IPV6) {
1418 bcopy((char *)&eh->ether_dhost,
1419 (char *)&tx_frame.wi_addr1, ETHER_ADDR_LEN);
1420 bcopy((char *)&eh->ether_shost,
1421 (char *)&tx_frame.wi_addr2, ETHER_ADDR_LEN);
1422 bcopy((char *)&eh->ether_dhost,
1423 (char *)&tx_frame.wi_dst_addr, ETHER_ADDR_LEN);
1424 bcopy((char *)&eh->ether_shost,
1425 (char *)&tx_frame.wi_src_addr, ETHER_ADDR_LEN);
1426
1427 tx_frame.wi_dat_len = m0->m_pkthdr.len - WI_SNAPHDR_LEN;
1428 tx_frame.wi_frame_ctl = WI_FTYPE_DATA;
1429 tx_frame.wi_dat[0] = htons(WI_SNAP_WORD0);
1430 tx_frame.wi_dat[1] = htons(WI_SNAP_WORD1);
1431 tx_frame.wi_len = htons(m0->m_pkthdr.len - WI_SNAPHDR_LEN);
1432 tx_frame.wi_type = eh->ether_type;
1433
1434 m_copydata(m0, sizeof(struct ether_header),
1435 m0->m_pkthdr.len - sizeof(struct ether_header),
1436 (caddr_t)&sc->wi_txbuf);
1437
1438 wi_write_data(sc, id, 0, (caddr_t)&tx_frame,
1439 sizeof(struct wi_frame));
1440 wi_write_data(sc, id, WI_802_11_OFFSET, (caddr_t)&sc->wi_txbuf,
1441 (m0->m_pkthdr.len - sizeof(struct ether_header)) + 2);
1442 } else {
1443 tx_frame.wi_dat_len = m0->m_pkthdr.len;
1444
1445 m_copydata(m0, 0, m0->m_pkthdr.len, (caddr_t)&sc->wi_txbuf);
1446
1447 wi_write_data(sc, id, 0, (caddr_t)&tx_frame,
1448 sizeof(struct wi_frame));
1449 wi_write_data(sc, id, WI_802_3_OFFSET, (caddr_t)&sc->wi_txbuf,
1450 m0->m_pkthdr.len + 2);
1451 }
1452
1453 #if NBPFILTER > 0
1454 /*
1455 * If there's a BPF listener, bounce a copy of
1456 * this frame to him.
1457 */
1458 if (ifp->if_bpf)
1459 bpf_mtap(ifp->if_bpf, m0);
1460 #endif
1461
1462 m_freem(m0);
1463
1464 if (wi_cmd(sc, WI_CMD_TX|WI_RECLAIM, id))
1465 printf("%s: xmit failed\n", sc->sc_dev.dv_xname);
1466
1467 ifp->if_flags |= IFF_OACTIVE;
1468
1469 /*
1470 * Set a timeout in case the chip goes out to lunch.
1471 */
1472 ifp->if_timer = 5;
1473
1474 return;
1475 }
1476
1477 static int
1478 wi_mgmt_xmit(sc, data, len)
1479 struct wi_softc *sc;
1480 caddr_t data;
1481 int len;
1482 {
1483 struct wi_frame tx_frame;
1484 int id;
1485 struct wi_80211_hdr *hdr;
1486 caddr_t dptr;
1487
1488 hdr = (struct wi_80211_hdr *)data;
1489 dptr = data + sizeof(struct wi_80211_hdr);
1490
1491 bzero((char *)&tx_frame, sizeof(tx_frame));
1492 id = sc->wi_tx_mgmt_id;
1493
1494 bcopy((char *)hdr, (char *)&tx_frame.wi_frame_ctl,
1495 sizeof(struct wi_80211_hdr));
1496
1497 tx_frame.wi_dat_len = len - WI_SNAPHDR_LEN;
1498 tx_frame.wi_len = htons(len - WI_SNAPHDR_LEN);
1499
1500 wi_write_data(sc, id, 0, (caddr_t)&tx_frame, sizeof(struct wi_frame));
1501 wi_write_data(sc, id, WI_802_11_OFFSET_RAW, dptr,
1502 (len - sizeof(struct wi_80211_hdr)) + 2);
1503
1504 if (wi_cmd(sc, WI_CMD_TX|WI_RECLAIM, id)) {
1505 printf("%s: xmit failed\n", sc->sc_dev.dv_xname);
1506 return(EIO);
1507 }
1508
1509 return(0);
1510 }
1511
1512 static void
1513 wi_stop(ifp, disable)
1514 struct ifnet *ifp;
1515 {
1516 struct wi_softc *sc = ifp->if_softc;
1517
1518 CSR_WRITE_2(sc, WI_INT_EN, 0);
1519 wi_cmd(sc, WI_CMD_DISABLE|sc->wi_portnum, 0);
1520
1521 callout_stop(&sc->wi_inquire_ch);
1522
1523 if (disable) {
1524 if (sc->sc_disable)
1525 (*sc->sc_disable)(sc);
1526 sc->sc_enabled = 0;
1527 }
1528
1529 ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
1530 ifp->if_timer = 0;
1531 }
1532
1533 static void
1534 wi_watchdog(ifp)
1535 struct ifnet *ifp;
1536 {
1537 struct wi_softc *sc;
1538
1539 sc = ifp->if_softc;
1540
1541 printf("%s: device timeout\n", sc->sc_dev.dv_xname);
1542
1543 wi_init(ifp);
1544
1545 ifp->if_oerrors++;
1546
1547 return;
1548 }
1549
1550 void
1551 wi_shutdown(sc)
1552 struct wi_softc *sc;
1553 {
1554 int s;
1555
1556 s = splnet();
1557 if (sc->sc_enabled) {
1558 if (sc->sc_disable)
1559 (*sc->sc_disable)(sc);
1560 sc->sc_enabled = 0;
1561 }
1562 splx(s);
1563 }
1564
1565 int
1566 wi_activate(self, act)
1567 struct device *self;
1568 enum devact act;
1569 {
1570 struct wi_softc *sc = (struct wi_softc *)self;
1571 int rv = 0, s;
1572
1573 s = splnet();
1574 switch (act) {
1575 case DVACT_ACTIVATE:
1576 rv = EOPNOTSUPP;
1577 break;
1578
1579 case DVACT_DEACTIVATE:
1580 if_deactivate(&sc->sc_ethercom.ec_if);
1581 break;
1582 }
1583 splx(s);
1584 return (rv);
1585 }
1586
1587 int
1588 wi_detach(sc)
1589 struct wi_softc *sc;
1590 {
1591 struct ifnet *ifp = sc->sc_ifp;
1592 int s;
1593
1594 if (!sc->sc_attached)
1595 return (0);
1596
1597 s = splnet();
1598 callout_stop(&sc->wi_inquire_ch);
1599
1600 /* Delete all remaining media. */
1601 ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY);
1602
1603 ether_ifdetach(ifp);
1604 if_detach(ifp);
1605 if (sc->sc_enabled) {
1606 if (sc->sc_disable)
1607 (*sc->sc_disable)(sc);
1608 sc->sc_enabled = 0;
1609 }
1610 splx(s);
1611 return (0);
1612 }
1613
1614 void
1615 wi_power(sc, why)
1616 struct wi_softc *sc;
1617 int why;
1618 {
1619 int s;
1620
1621 if (!sc->sc_enabled)
1622 return;
1623
1624 s = splnet();
1625 switch (why) {
1626 case PWR_SUSPEND:
1627 case PWR_STANDBY:
1628 wi_stop(sc->sc_ifp, 0);
1629 if (sc->sc_disable)
1630 (*sc->sc_disable)(sc);
1631 break;
1632 case PWR_RESUME:
1633 sc->sc_enabled = 0;
1634 wi_init(sc->sc_ifp);
1635 (void)wi_intr(sc);
1636 break;
1637 case PWR_SOFTSUSPEND:
1638 case PWR_SOFTSTANDBY:
1639 case PWR_SOFTRESUME:
1640 break;
1641 }
1642 splx(s);
1643 }
1644
1645 static int
1646 wi_set_ssid(ws, id, len)
1647 struct ieee80211_nwid *ws;
1648 u_int8_t *id;
1649 int len;
1650 {
1651
1652 if (len > IEEE80211_NWID_LEN)
1653 return (EINVAL);
1654 ws->i_len = len;
1655 memcpy(ws->i_nwid, id, len);
1656 return (0);
1657 }
1658
1659 static void
1660 wi_request_fill_ssid(wreq, ws)
1661 struct wi_req *wreq;
1662 struct ieee80211_nwid *ws;
1663 {
1664
1665 memset(&wreq->wi_val[0], 0, sizeof(wreq->wi_val));
1666 wreq->wi_val[0] = ws->i_len;
1667 wreq->wi_len = roundup(wreq->wi_val[0], 2) / 2 + 2;
1668 memcpy(&wreq->wi_val[1], ws->i_nwid, wreq->wi_val[0]);
1669 }
1670
1671 static int
1672 wi_write_ssid(sc, type, wreq, ws)
1673 struct wi_softc *sc;
1674 int type;
1675 struct wi_req *wreq;
1676 struct ieee80211_nwid *ws;
1677 {
1678
1679 wreq->wi_type = type;
1680 wi_request_fill_ssid(wreq, ws);
1681 return (wi_write_record(sc, (struct wi_ltv_gen *)wreq));
1682 }
1683
1684 static int
1685 wi_sync_media(sc, ptype, txrate)
1686 struct wi_softc *sc;
1687 int ptype;
1688 int txrate;
1689 {
1690 int media = sc->sc_media.ifm_cur->ifm_media;
1691 int options = IFM_OPTIONS(media);
1692 int subtype;
1693
1694 switch (txrate) {
1695 case 1:
1696 subtype = IFM_IEEE80211_DS1;
1697 break;
1698 case 2:
1699 subtype = IFM_IEEE80211_DS2;
1700 break;
1701 case 3:
1702 subtype = IFM_AUTO;
1703 break;
1704 case 11:
1705 subtype = IFM_IEEE80211_DS11;
1706 break;
1707 default:
1708 subtype = IFM_MANUAL; /* Unable to represent */
1709 break;
1710 }
1711 switch (ptype) {
1712 case WI_PORTTYPE_ADHOC:
1713 options |= IFM_IEEE80211_ADHOC;
1714 break;
1715 case WI_PORTTYPE_BSS:
1716 options &= ~IFM_IEEE80211_ADHOC;
1717 break;
1718 default:
1719 subtype = IFM_MANUAL; /* Unable to represent */
1720 break;
1721 }
1722 media = IFM_MAKEWORD(IFM_TYPE(media), subtype, options,
1723 IFM_INST(media));
1724 if (ifmedia_match(&sc->sc_media, media, sc->sc_media.ifm_mask) == NULL)
1725 return (EINVAL);
1726 ifmedia_set(&sc->sc_media, media);
1727 sc->wi_ptype = ptype;
1728 sc->wi_tx_rate = txrate;
1729 return (0);
1730 }
1731
1732 static int
1733 wi_media_change(ifp)
1734 struct ifnet *ifp;
1735 {
1736 struct wi_softc *sc = ifp->if_softc;
1737 int otype = sc->wi_ptype;
1738 int orate = sc->wi_tx_rate;
1739
1740 if ((sc->sc_media.ifm_cur->ifm_media & IFM_IEEE80211_ADHOC) != 0)
1741 sc->wi_ptype = WI_PORTTYPE_ADHOC;
1742 else
1743 sc->wi_ptype = WI_PORTTYPE_BSS;
1744
1745 switch (IFM_SUBTYPE(sc->sc_media.ifm_cur->ifm_media)) {
1746 case IFM_IEEE80211_DS1:
1747 sc->wi_tx_rate = 1;
1748 break;
1749 case IFM_IEEE80211_DS2:
1750 sc->wi_tx_rate = 2;
1751 break;
1752 case IFM_AUTO:
1753 sc->wi_tx_rate = 3;
1754 break;
1755 case IFM_IEEE80211_DS11:
1756 sc->wi_tx_rate = 11;
1757 break;
1758 }
1759
1760 if (sc->sc_enabled != 0) {
1761 if (otype != sc->wi_ptype ||
1762 orate != sc->wi_tx_rate)
1763 wi_init(ifp);
1764 }
1765
1766 ifp->if_baudrate = ifmedia_baudrate(sc->sc_media.ifm_cur->ifm_media);
1767
1768 return (0);
1769 }
1770
1771 static void
1772 wi_media_status(ifp, imr)
1773 struct ifnet *ifp;
1774 struct ifmediareq *imr;
1775 {
1776 struct wi_softc *sc = ifp->if_softc;
1777
1778 if (sc->sc_enabled == 0) {
1779 imr->ifm_active = IFM_IEEE80211|IFM_NONE;
1780 imr->ifm_status = 0;
1781 return;
1782 }
1783
1784 imr->ifm_active = sc->sc_media.ifm_cur->ifm_media;
1785 imr->ifm_status = IFM_AVALID|IFM_ACTIVE;
1786 }
1787
1788 static int
1789 wi_set_nwkey(sc, nwkey)
1790 struct wi_softc *sc;
1791 struct ieee80211_nwkey *nwkey;
1792 {
1793 int i, len, error;
1794 struct wi_req wreq;
1795 struct wi_ltv_keys *wk = (struct wi_ltv_keys *)&wreq;
1796
1797 if (!sc->wi_has_wep)
1798 return ENODEV;
1799 if (nwkey->i_defkid <= 0 ||
1800 nwkey->i_defkid > IEEE80211_WEP_NKID)
1801 return EINVAL;
1802 memcpy(wk, &sc->wi_keys, sizeof(*wk));
1803 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1804 if (nwkey->i_key[i].i_keydat == NULL)
1805 continue;
1806 len = nwkey->i_key[i].i_keylen;
1807 if (len > sizeof(wk->wi_keys[i].wi_keydat))
1808 return EINVAL;
1809 error = copyin(nwkey->i_key[i].i_keydat,
1810 wk->wi_keys[i].wi_keydat, len);
1811 if (error)
1812 return error;
1813 wk->wi_keys[i].wi_keylen = len;
1814 }
1815
1816 wk->wi_len = (sizeof(*wk) / 2) + 1;
1817 wk->wi_type = WI_RID_DEFLT_CRYPT_KEYS;
1818 if (sc->sc_enabled != 0) {
1819 error = wi_write_record(sc, (struct wi_ltv_gen *)&wreq);
1820 if (error)
1821 return error;
1822 }
1823 error = wi_setdef(sc, &wreq);
1824 if (error)
1825 return error;
1826
1827 wreq.wi_len = 2;
1828 wreq.wi_type = WI_RID_TX_CRYPT_KEY;
1829 wreq.wi_val[0] = nwkey->i_defkid - 1;
1830 if (sc->sc_enabled != 0) {
1831 error = wi_write_record(sc, (struct wi_ltv_gen *)&wreq);
1832 if (error)
1833 return error;
1834 }
1835 error = wi_setdef(sc, &wreq);
1836 if (error)
1837 return error;
1838
1839 wreq.wi_type = WI_RID_ENCRYPTION;
1840 wreq.wi_val[0] = nwkey->i_wepon;
1841 if (sc->sc_enabled != 0) {
1842 error = wi_write_record(sc, (struct wi_ltv_gen *)&wreq);
1843 if (error)
1844 return error;
1845 }
1846 error = wi_setdef(sc, &wreq);
1847 if (error)
1848 return error;
1849
1850 if (sc->sc_enabled != 0)
1851 wi_init(&sc->sc_ethercom.ec_if);
1852 return 0;
1853 }
1854
1855 static int
1856 wi_get_nwkey(sc, nwkey)
1857 struct wi_softc *sc;
1858 struct ieee80211_nwkey *nwkey;
1859 {
1860 int i, len, error;
1861 struct wi_ltv_keys *wk = &sc->wi_keys;
1862
1863 if (!sc->wi_has_wep)
1864 return ENODEV;
1865 nwkey->i_wepon = sc->wi_use_wep;
1866 nwkey->i_defkid = sc->wi_tx_key + 1;
1867
1868 /* do not show any keys to non-root user */
1869 error = suser(curproc->p_ucred, &curproc->p_acflag);
1870 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1871 if (nwkey->i_key[i].i_keydat == NULL)
1872 continue;
1873 /* error holds results of suser() for the first time */
1874 if (error)
1875 return error;
1876 len = wk->wi_keys[i].wi_keylen;
1877 if (nwkey->i_key[i].i_keylen < len)
1878 return ENOSPC;
1879 nwkey->i_key[i].i_keylen = len;
1880 error = copyout(wk->wi_keys[i].wi_keydat,
1881 nwkey->i_key[i].i_keydat, len);
1882 if (error)
1883 return error;
1884 }
1885 return 0;
1886 }
1887
1888 static int
1889 wi_set_pm(struct wi_softc *sc, struct ieee80211_power *power)
1890 {
1891
1892 sc->wi_pm_enabled = power->i_enabled;
1893 sc->wi_max_sleep = power->i_maxsleep;
1894
1895 if (sc->sc_enabled)
1896 return (wi_init(&sc->sc_ethercom.ec_if));
1897
1898 return (0);
1899 }
1900
1901 static int
1902 wi_get_pm(struct wi_softc *sc, struct ieee80211_power *power)
1903 {
1904
1905 power->i_enabled = sc->wi_pm_enabled;
1906 power->i_maxsleep = sc->wi_max_sleep;
1907
1908 return (0);
1909 }
1910