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