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