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