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