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