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