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