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