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