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