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