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