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