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