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