summaryrefslogtreecommitdiff
path: root/package/madwifi/patches/389-autochannel.patch
blob: 65e03e73814a100712fe5a1a8226dbb28cf62a4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
--- a/ath/if_ath.c
+++ b/ath/if_ath.c
@@ -384,6 +384,7 @@ static u_int32_t ath_get_real_maxtxpower
 
 static void ath_poll_disable(struct net_device *dev);
 static void ath_poll_enable(struct net_device *dev);
+static void ath_fetch_idle_time(struct ath_softc *sc);
 
 /* calibrate every 30 secs in steady state but check every second at first. */
 static int ath_calinterval = ATH_SHORT_CALINTERVAL;
@@ -2580,6 +2581,7 @@ ath_init(struct net_device *dev)
 	 * be followed by initialization of the appropriate bits
 	 * and then setup of the interrupt mask.
 	 */
+	ath_fetch_idle_time(sc);
 	sc->sc_curchan.channel = ic->ic_curchan->ic_freq;
 	sc->sc_curchan.channelFlags = ath_chan2flags(ic->ic_curchan);
 	if (!ath_hal_reset(ah, sc->sc_opmode, &sc->sc_curchan, AH_FALSE, &status)) {
@@ -2914,6 +2916,40 @@ ath_hw_check_atim(struct ath_softc *sc, 
 }
 
 
+#define AR5K_RXCLEAR	0x80f4
+#define AR5K_CYCLES		0x80f8
+static void
+ath_fetch_idle_time(struct ath_softc *sc)
+{
+	struct ieee80211com *ic = &sc->sc_ic;
+	struct ath_hal *ah = sc->sc_ah;
+	u_int32_t cc, rx;
+	u_int32_t time = 0;
+
+	if (sc->sc_ah->ah_macType < 5212)
+		return;
+
+	if (!ic->ic_curchan || (ic->ic_curchan == IEEE80211_CHAN_ANYC))
+		return;
+
+	rx = OS_REG_READ(ah, AR5K_RXCLEAR);
+	cc = OS_REG_READ(ah, AR5K_CYCLES);
+	if (!cc)
+		return;
+
+	if (rx > cc)
+		return; /* wraparound */
+
+	if (sc->sc_last_chan)
+		sc->sc_last_chan->ic_idletime = 100 * (cc - rx) / cc;
+	sc->sc_last_chan = ic->ic_curchan;
+
+	OS_REG_WRITE(ah, AR5K_RXCLEAR, 0);
+	OS_REG_WRITE(ah, AR5K_CYCLES, 0);
+}
+#undef AR5K_RXCLEAR
+#undef AR5K_CYCLES
+
 /*
  * Reset the hardware w/o losing operational state.  This is
  * basically a more efficient way of doing ath_stop, ath_init,
@@ -2940,6 +2976,7 @@ ath_reset(struct net_device *dev)
 	 * Convert to a HAL channel description with the flags
 	 * constrained to reflect the current operating mode.
 	 */
+	ath_fetch_idle_time(sc);
 	c = ic->ic_curchan;
 	sc->sc_curchan.channel = c->ic_freq;
 	sc->sc_curchan.channelFlags = ath_chan2flags(c);
@@ -9022,6 +9059,7 @@ ath_chan_set(struct ath_softc *sc, struc
 	u_int8_t channel_change_required = 0;
 	struct timeval tv;
 
+	ath_fetch_idle_time(sc);
 	/*
 	 * Convert to a HAL channel description with
 	 * the flags constrained to reflect the current
--- a/ath/if_athvar.h
+++ b/ath/if_athvar.h
@@ -773,6 +773,7 @@ struct ath_softc {
 	struct ieee80211vap **sc_bslot;		/* beacon xmit slots */
 	int sc_bnext;				/* next slot for beacon xmit */
 
+	struct ieee80211_channel *sc_last_chan;
 	int sc_beacon_cal;			/* use beacon timer for calibration */
 	u_int64_t sc_lastcal;			/* last time the calibration was performed */
 	struct timer_list sc_cal_ch;		/* calibration timer */
--- a/net80211/_ieee80211.h
+++ b/net80211/_ieee80211.h
@@ -148,6 +148,7 @@ struct ieee80211_channel {
 	int8_t ic_maxpower;	/* maximum tx power in dBm */
 	int8_t ic_minpower;	/* minimum tx power in dBm */
 	u_int8_t ic_scanflags;
+	u_int8_t ic_idletime; /* phy idle time in % */
 };
 
 #define	IEEE80211_CHAN_MAX	255
--- a/net80211/ieee80211_scan_ap.c
+++ b/net80211/ieee80211_scan_ap.c
@@ -417,6 +417,19 @@ pc_cmp_rssi(struct ap_state *as, struct 
 
 /* This function must be invoked with locks acquired */
 static int
+pc_cmp_idletime(struct ieee80211_channel *a,
+		struct ieee80211_channel *b)
+{
+	if (!a->ic_idletime || !b->ic_idletime)
+		return 0;
+
+	/* a is better than b (return < 0) when a has more idle time than b */
+	return b->ic_idletime - a->ic_idletime;
+}
+
+
+/* This function must be invoked with locks acquired */
+static int
 pc_cmp_samechan(struct ieee80211com *ic, struct ieee80211_channel *a,
 		struct ieee80211_channel *b)
 {
@@ -451,6 +464,7 @@ pc_cmp(const void *_a, const void *_b)
 
 	EVALUATE_CRITERION(radar, a, b);
 	EVALUATE_CRITERION(keepmode, params, a, b);
+	EVALUATE_CRITERION(idletime, a, b);
 	EVALUATE_CRITERION(sc, ic, a, b);
 	/* XXX: rssi useless? pick_channel evaluates it anyway */
 	EVALUATE_CRITERION(rssi, params->ss->ss_priv, a, b);