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