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