Index: dev/ral/if_ral.c
===================================================================
RCS file: dev/ral/if_ral.c
diff -N dev/ral/if_ral.c
--- dev/ral/if_ral.c	25 Feb 2006 00:50:19 -0000	1.10.2.7
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,2864 +0,0 @@
-/*	$FreeBSD: src/sys/dev/ral/if_ral.c,v 1.10.2.7 2006/02/25 00:50:19 sam Exp $	*/
-
-/*-
- * Copyright (c) 2005, 2006
- *	Damien Bergamini <damien.bergamini@free.fr>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/sys/dev/ral/if_ral.c,v 1.10.2.7 2006/02/25 00:50:19 sam Exp $");
-
-/*-
- * Ralink Technology RT2500 chipset driver
- * http://www.ralinktech.com/
- */
-
-#include <sys/param.h>
-#include <sys/sysctl.h>
-#include <sys/sockio.h>
-#include <sys/mbuf.h>
-#include <sys/kernel.h>
-#include <sys/socket.h>
-#include <sys/systm.h>
-#include <sys/malloc.h>
-#include <sys/module.h>
-#include <sys/bus.h>
-#include <sys/endian.h>
-
-#include <machine/bus.h>
-#include <machine/resource.h>
-#include <machine/clock.h>
-#include <sys/rman.h>
-
-#include <net/bpf.h>
-#include <net/if.h>
-#include <net/if_arp.h>
-#include <net/ethernet.h>
-#include <net/if_dl.h>
-#include <net/if_media.h>
-#include <net/if_types.h>
-
-#include <net80211/ieee80211_var.h>
-#include <net80211/ieee80211_radiotap.h>
-
-#include <netinet/in.h>
-#include <netinet/in_systm.h>
-#include <netinet/in_var.h>
-#include <netinet/ip.h>
-#include <netinet/if_ether.h>
-
-#include <dev/ral/if_ralrate.h>
-#include <dev/ral/if_ralreg.h>
-#include <dev/ral/if_ralvar.h>
-
-#ifdef RAL_DEBUG
-#define DPRINTF(x)	do { if (ral_debug > 0) printf x; } while (0)
-#define DPRINTFN(n, x)	do { if (ral_debug >= (n)) printf x; } while (0)
-int ral_debug = 0;
-SYSCTL_INT(_debug, OID_AUTO, ral, CTLFLAG_RW, &ral_debug, 0, "ral debug level");
-#else
-#define DPRINTF(x)
-#define DPRINTFN(n, x)
-#endif
-
-MODULE_DEPEND(ral, wlan, 1, 1, 1);
-
-static void		ral_dma_map_addr(void *, bus_dma_segment_t *, int, int);
-static int		ral_alloc_tx_ring(struct ral_softc *,
-			    struct ral_tx_ring *, int);
-static void		ral_reset_tx_ring(struct ral_softc *,
-			    struct ral_tx_ring *);
-static void		ral_free_tx_ring(struct ral_softc *,
-			    struct ral_tx_ring *);
-static int		ral_alloc_rx_ring(struct ral_softc *,
-			    struct ral_rx_ring *, int);
-static void		ral_reset_rx_ring(struct ral_softc *,
-			    struct ral_rx_ring *);
-static void		ral_free_rx_ring(struct ral_softc *,
-			    struct ral_rx_ring *);
-static struct		ieee80211_node *ral_node_alloc(
-			    struct ieee80211_node_table *);
-static int		ral_media_change(struct ifnet *);
-static void		ral_next_scan(void *);
-static void		ral_iter_func(void *, struct ieee80211_node *);
-static void		ral_update_rssadapt(void *);
-static int		ral_newstate(struct ieee80211com *,
-			    enum ieee80211_state, int);
-static uint16_t		ral_eeprom_read(struct ral_softc *, uint8_t);
-static void		ral_encryption_intr(struct ral_softc *);
-static void		ral_tx_intr(struct ral_softc *);
-static void		ral_prio_intr(struct ral_softc *);
-static void		ral_decryption_intr(struct ral_softc *);
-static void		ral_rx_intr(struct ral_softc *);
-static void		ral_beacon_expire(struct ral_softc *);
-static void		ral_wakeup_expire(struct ral_softc *);
-static void		ral_intr(void *);
-static uint8_t		ral_rxrate(struct ral_rx_desc *);
-static int		ral_ack_rate(struct ieee80211com *, int);
-static uint16_t		ral_txtime(int, int, uint32_t);
-static uint8_t		ral_plcp_signal(int);
-static void		ral_setup_tx_desc(struct ral_softc *,
-			    struct ral_tx_desc *, uint32_t, int, int, int,
-			    bus_addr_t);
-static int		ral_tx_bcn(struct ral_softc *, struct mbuf *,
-			    struct ieee80211_node *);
-static int		ral_tx_mgt(struct ral_softc *, struct mbuf *,
-			    struct ieee80211_node *);
-static struct		mbuf *ral_get_rts(struct ral_softc *,
-			    struct ieee80211_frame *, uint16_t);
-static int		ral_tx_data(struct ral_softc *, struct mbuf *,
-			    struct ieee80211_node *);
-static void		ral_start(struct ifnet *);
-static void		ral_watchdog(struct ifnet *);
-static int		ral_reset(struct ifnet *);
-static int		ral_ioctl(struct ifnet *, u_long, caddr_t);
-static void		ral_bbp_write(struct ral_softc *, uint8_t, uint8_t);
-static uint8_t		ral_bbp_read(struct ral_softc *, uint8_t);
-static void		ral_rf_write(struct ral_softc *, uint8_t, uint32_t);
-static void		ral_set_chan(struct ral_softc *,
-			    struct ieee80211_channel *);
-#if 0
-static void		ral_disable_rf_tune(struct ral_softc *);
-#endif
-static void		ral_enable_tsf_sync(struct ral_softc *);
-static void		ral_update_plcp(struct ral_softc *);
-static void		ral_update_slot(struct ifnet *);
-static void		ral_set_basicrates(struct ral_softc *);
-static void		ral_update_led(struct ral_softc *, int, int);
-static void		ral_set_bssid(struct ral_softc *, uint8_t *);
-static void		ral_set_macaddr(struct ral_softc *, uint8_t *);
-static void		ral_get_macaddr(struct ral_softc *, uint8_t *);
-static void		ral_update_promisc(struct ral_softc *);
-static const char	*ral_get_rf(int);
-static void		ral_read_eeprom(struct ral_softc *);
-static int		ral_bbp_init(struct ral_softc *);
-static void		ral_set_txantenna(struct ral_softc *, int);
-static void		ral_set_rxantenna(struct ral_softc *, int);
-static void		ral_init(void *);
-
-devclass_t ral_devclass;
-
-/*
- * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
- */
-static const struct ieee80211_rateset ral_rateset_11a =
-	{ 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
-
-static const struct ieee80211_rateset ral_rateset_11b =
-	{ 4, { 2, 4, 11, 22 } };
-
-static const struct ieee80211_rateset ral_rateset_11g =
-	{ 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
-
-/*
- * Default values for MAC registers; values taken from the reference driver.
- */
-static const struct {
-	uint32_t	reg;
-	uint32_t	val;
-} ral_def_mac[] = {
-	{ RAL_PSCSR0,      0x00020002 },
-	{ RAL_PSCSR1,      0x00000002 },
-	{ RAL_PSCSR2,      0x00020002 },
-	{ RAL_PSCSR3,      0x00000002 },
-	{ RAL_TIMECSR,     0x00003f21 },
-	{ RAL_CSR9,        0x00000780 },
-	{ RAL_CSR11,       0x07041483 },
-	{ RAL_CNT3,        0x00000000 },
-	{ RAL_TXCSR1,      0x07614562 },
-	{ RAL_ARSP_PLCP_0, 0x8c8d8b8a },
-	{ RAL_ACKPCTCSR,   0x7038140a },
-	{ RAL_ARTCSR1,     0x1d21252d },
-	{ RAL_ARTCSR2,     0x1919191d },
-	{ RAL_RXCSR0,      0xffffffff },
-	{ RAL_RXCSR3,      0xb3aab3af },
-	{ RAL_PCICSR,      0x000003b8 },
-	{ RAL_PWRCSR0,     0x3f3b3100 },
-	{ RAL_GPIOCSR,     0x0000ff00 },
-	{ RAL_TESTCSR,     0x000000f0 },
-	{ RAL_PWRCSR1,     0x000001ff },
-	{ RAL_MACCSR0,     0x00213223 },
-	{ RAL_MACCSR1,     0x00235518 },
-	{ RAL_RLPWCSR,     0x00000040 },
-	{ RAL_RALINKCSR,   0x9a009a11 },
-	{ RAL_CSR7,        0xffffffff },
-	{ RAL_BBPCSR1,     0x82188200 },
-	{ RAL_TXACKCSR0,   0x00000020 },
-	{ RAL_SECCSR3,     0x0000e78f }
-};
-
-/*
- * Default values for BBP registers; values taken from the reference driver.
- */
-static const struct {
-	uint8_t	reg;
-	uint8_t	val;
-} ral_def_bbp[] = {
-	{  3, 0x02 },
-	{  4, 0x19 },
-	{ 14, 0x1c },
-	{ 15, 0x30 },
-	{ 16, 0xac },
-	{ 17, 0x48 },
-	{ 18, 0x18 },
-	{ 19, 0xff },
-	{ 20, 0x1e },
-	{ 21, 0x08 },
-	{ 22, 0x08 },
-	{ 23, 0x08 },
-	{ 24, 0x80 },
-	{ 25, 0x50 },
-	{ 26, 0x08 },
-	{ 27, 0x23 },
-	{ 30, 0x10 },
-	{ 31, 0x2b },
-	{ 32, 0xb9 },
-	{ 34, 0x12 },
-	{ 35, 0x50 },
-	{ 39, 0xc4 },
-	{ 40, 0x02 },
-	{ 41, 0x60 },
-	{ 53, 0x10 },
-	{ 54, 0x18 },
-	{ 56, 0x08 },
-	{ 57, 0x10 },
-	{ 58, 0x08 },
-	{ 61, 0x60 },
-	{ 62, 0x10 },
-	{ 75, 0xff }
-};
-
-/*
- * Default values for RF register R2 indexed by channel numbers; values taken
- * from the reference driver.
- */
-static const uint32_t ral_rf2522_r2[] = {
-	0x307f6, 0x307fb, 0x30800, 0x30805, 0x3080a, 0x3080f, 0x30814,
-	0x30819, 0x3081e, 0x30823, 0x30828, 0x3082d, 0x30832, 0x3083e
-};
-
-static const uint32_t ral_rf2523_r2[] = {
-	0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
-	0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
-};
-
-static const uint32_t ral_rf2524_r2[] = {
-	0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
-	0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
-};
-
-static const uint32_t ral_rf2525_r2[] = {
-	0x20327, 0x20328, 0x20329, 0x2032a, 0x2032b, 0x2032c, 0x2032d,
-	0x2032e, 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20346
-};
-
-static const uint32_t ral_rf2525_hi_r2[] = {
-	0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20344, 0x20345,
-	0x20346, 0x20347, 0x20348, 0x20349, 0x2034a, 0x2034b, 0x2034e
-};
-
-static const uint32_t ral_rf2525e_r2[] = {
-	0x2044d, 0x2044e, 0x2044f, 0x20460, 0x20461, 0x20462, 0x20463,
-	0x20464, 0x20465, 0x20466, 0x20467, 0x20468, 0x20469, 0x2046b
-};
-
-static const uint32_t ral_rf2526_hi_r2[] = {
-	0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d, 0x0022d,
-	0x0022e, 0x0022e, 0x0022f, 0x0022d, 0x00240, 0x00240, 0x00241
-};
-
-static const uint32_t ral_rf2526_r2[] = {
-	0x00226, 0x00227, 0x00227, 0x00228, 0x00228, 0x00229, 0x00229,
-	0x0022a, 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d
-};
-
-/*
- * For dual-band RF, RF registers R1 and R4 also depend on channel number;
- * values taken from the reference driver.
- */
-static const struct {
-	uint8_t		chan;
-	uint32_t	r1;
-	uint32_t	r2;
-	uint32_t	r4;
-} ral_rf5222[] = {
-	{   1, 0x08808, 0x0044d, 0x00282 },
-	{   2, 0x08808, 0x0044e, 0x00282 },
-	{   3, 0x08808, 0x0044f, 0x00282 },
-	{   4, 0x08808, 0x00460, 0x00282 },
-	{   5, 0x08808, 0x00461, 0x00282 },
-	{   6, 0x08808, 0x00462, 0x00282 },
-	{   7, 0x08808, 0x00463, 0x00282 },
-	{   8, 0x08808, 0x00464, 0x00282 },
-	{   9, 0x08808, 0x00465, 0x00282 },
-	{  10, 0x08808, 0x00466, 0x00282 },
-	{  11, 0x08808, 0x00467, 0x00282 },
-	{  12, 0x08808, 0x00468, 0x00282 },
-	{  13, 0x08808, 0x00469, 0x00282 },
-	{  14, 0x08808, 0x0046b, 0x00286 },
-
-	{  36, 0x08804, 0x06225, 0x00287 },
-	{  40, 0x08804, 0x06226, 0x00287 },
-	{  44, 0x08804, 0x06227, 0x00287 },
-	{  48, 0x08804, 0x06228, 0x00287 },
-	{  52, 0x08804, 0x06229, 0x00287 },
-	{  56, 0x08804, 0x0622a, 0x00287 },
-	{  60, 0x08804, 0x0622b, 0x00287 },
-	{  64, 0x08804, 0x0622c, 0x00287 },
-
-	{ 100, 0x08804, 0x02200, 0x00283 },
-	{ 104, 0x08804, 0x02201, 0x00283 },
-	{ 108, 0x08804, 0x02202, 0x00283 },
-	{ 112, 0x08804, 0x02203, 0x00283 },
-	{ 116, 0x08804, 0x02204, 0x00283 },
-	{ 120, 0x08804, 0x02205, 0x00283 },
-	{ 124, 0x08804, 0x02206, 0x00283 },
-	{ 128, 0x08804, 0x02207, 0x00283 },
-	{ 132, 0x08804, 0x02208, 0x00283 },
-	{ 136, 0x08804, 0x02209, 0x00283 },
-	{ 140, 0x08804, 0x0220a, 0x00283 },
-
-	{ 149, 0x08808, 0x02429, 0x00281 },
-	{ 153, 0x08808, 0x0242b, 0x00281 },
-	{ 157, 0x08808, 0x0242d, 0x00281 },
-	{ 161, 0x08808, 0x0242f, 0x00281 }
-};
-
-int
-ral_attach(device_t dev)
-{
-	struct ral_softc *sc = device_get_softc(dev);
-	struct ifnet *ifp;
-	struct ieee80211com *ic = &sc->sc_ic;
-	int error, i;
-
-	sc->sc_dev = dev;
-
-	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
-	    MTX_DEF | MTX_RECURSE);
-
-	callout_init(&sc->scan_ch, debug_mpsafenet ? CALLOUT_MPSAFE : 0);
-	callout_init(&sc->rssadapt_ch, CALLOUT_MPSAFE);
-
-	/* retrieve RT2560 rev. no */
-	sc->asic_rev = RAL_READ(sc, RAL_CSR0);
-
-	/* retrieve MAC address */
-	ral_get_macaddr(sc, ic->ic_myaddr);
-
-	/* retrieve RF rev. no and various other things from EEPROM */
-	ral_read_eeprom(sc);
-
-	device_printf(dev, "MAC/BBP RT2560 (rev 0x%02x), RF %s\n",
-	    sc->asic_rev, ral_get_rf(sc->rf_rev));
-
-	/*
-	 * Allocate Tx and Rx rings.
-	 */
-	if (ral_alloc_tx_ring(sc, &sc->txq, RAL_TX_RING_COUNT) != 0) {
-		device_printf(sc->sc_dev, "could not allocate Tx ring\n");
-		goto fail1;
-	}
-
-	if (ral_alloc_tx_ring(sc, &sc->atimq, RAL_ATIM_RING_COUNT) != 0) {
-		device_printf(sc->sc_dev, "could not allocate ATIM ring\n");
-		goto fail2;
-	}
-
-	if (ral_alloc_tx_ring(sc, &sc->prioq, RAL_PRIO_RING_COUNT) != 0) {
-		device_printf(sc->sc_dev, "could not allocate Prio ring\n");
-		goto fail3;
-	}
-
-	if (ral_alloc_tx_ring(sc, &sc->bcnq, RAL_BEACON_RING_COUNT) != 0) {
-		device_printf(sc->sc_dev, "could not allocate Beacon ring\n");
-		goto fail4;
-	}
-
-	if (ral_alloc_rx_ring(sc, &sc->rxq, RAL_RX_RING_COUNT) != 0) {
-		device_printf(sc->sc_dev, "could not allocate Rx ring\n");
-		goto fail5;
-	}
-	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
-	if (ifp == NULL) {
-		device_printf(sc->sc_dev, "can not if_alloc()\n");
-		goto fail6;
-	}
-	ifp->if_softc = sc;
-	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
-	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
-	ifp->if_init = ral_init;
-	ifp->if_ioctl = ral_ioctl;
-	ifp->if_start = ral_start;
-	ifp->if_watchdog = ral_watchdog;
-	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
-	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
-	IFQ_SET_READY(&ifp->if_snd);
-
-	ic->ic_ifp = ifp;
-	ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
-	ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
-	ic->ic_state = IEEE80211_S_INIT;
-
-	/* set device capabilities */
-	ic->ic_caps =
-	    IEEE80211_C_IBSS |		/* IBSS mode supported */
-	    IEEE80211_C_MONITOR |	/* monitor mode supported */
-	    IEEE80211_C_HOSTAP |	/* HostAp mode supported */
-	    IEEE80211_C_TXPMGT |	/* tx power management */
-	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
-	    IEEE80211_C_SHSLOT |	/* short slot time supported */
-	    IEEE80211_C_WPA;		/* 802.11i */
-
-	if (sc->rf_rev == RAL_RF_5222) {
-		/* set supported .11a rates */
-		ic->ic_sup_rates[IEEE80211_MODE_11A] = ral_rateset_11a;
-
-		/* set supported .11a channels */
-		for (i = 36; i <= 64; i += 4) {
-			ic->ic_channels[i].ic_freq =
-			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
-			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
-		}
-		for (i = 100; i <= 140; i += 4) {
-			ic->ic_channels[i].ic_freq =
-			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
-			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
-		}
-		for (i = 149; i <= 161; i += 4) {
-			ic->ic_channels[i].ic_freq =
-			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
-			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
-		}
-	}
-
-	/* set supported .11b and .11g rates */
-	ic->ic_sup_rates[IEEE80211_MODE_11B] = ral_rateset_11b;
-	ic->ic_sup_rates[IEEE80211_MODE_11G] = ral_rateset_11g;
-
-	/* set supported .11b and .11g channels (1 through 14) */
-	for (i = 1; i <= 14; i++) {
-		ic->ic_channels[i].ic_freq =
-		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
-		ic->ic_channels[i].ic_flags =
-		    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
-		    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
-	}
-
-	ieee80211_ifattach(ic);
-	ic->ic_node_alloc = ral_node_alloc;
-	ic->ic_updateslot = ral_update_slot;
-	ic->ic_reset = ral_reset;
-	/* enable s/w bmiss handling in sta mode */
-	ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
-
-	/* override state transition machine */
-	sc->sc_newstate = ic->ic_newstate;
-	ic->ic_newstate = ral_newstate;
-	ieee80211_media_init(ic, ral_media_change, ieee80211_media_status);
-
-	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
-	    sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
-
-	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
-	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
-	sc->sc_rxtap.wr_ihdr.it_present = htole32(RAL_RX_RADIOTAP_PRESENT);
-
-	sc->sc_txtap_len = sizeof sc->sc_txtapu;
-	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
-	sc->sc_
