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