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