madwifi: fix the autochannel patch
[openwrt/openwrt.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,37 @@ 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 + if (!ic->ic_curchan || (ic->ic_curchan == IEEE80211_CHAN_ANYC))
37 + return;
38 +
39 + rx = OS_REG_READ(ah, AR5K_RXCLEAR);
40 + cc = OS_REG_READ(ah, AR5K_CYCLES);
41 + if (rx > cc)
42 + return; /* wraparound */
43 +
44 + if (sc->sc_last_chan)
45 + sc->sc_last_chan->ic_idletime = 100 * (cc - rx) / cc;
46 + sc->sc_last_chan = ic->ic_curchan;
47 +
48 + OS_REG_WRITE(ah, AR5K_RXCLEAR, 0);
49 + OS_REG_WRITE(ah, AR5K_CYCLES, 0);
50 +}
51 +#undef AR5K_RXCLEAR
52 +#undef AR5K_CYCLES
53 +
54 /*
55 * Reset the hardware w/o losing operational state. This is
56 * basically a more efficient way of doing ath_stop, ath_init,
57 @@ -2939,6 +2972,7 @@ ath_reset(struct net_device *dev)
58 * Convert to a HAL channel description with the flags
59 * constrained to reflect the current operating mode.
60 */
61 + ath_fetch_idle_time(sc);
62 c = ic->ic_curchan;
63 sc->sc_curchan.channel = c->ic_freq;
64 sc->sc_curchan.channelFlags = ath_chan2flags(c);
65 @@ -9019,6 +9053,7 @@ ath_chan_set(struct ath_softc *sc, struc
66 u_int8_t channel_change_required = 0;
67 struct timeval tv;
68
69 + ath_fetch_idle_time(sc);
70 /*
71 * Convert to a HAL channel description with
72 * the flags constrained to reflect the current
73 --- a/ath/if_athvar.h
74 +++ b/ath/if_athvar.h
75 @@ -773,6 +773,7 @@ struct ath_softc {
76 struct ieee80211vap **sc_bslot; /* beacon xmit slots */
77 int sc_bnext; /* next slot for beacon xmit */
78
79 + struct ieee80211_channel *sc_last_chan;
80 int sc_beacon_cal; /* use beacon timer for calibration */
81 u_int64_t sc_lastcal; /* last time the calibration was performed */
82 struct timer_list sc_cal_ch; /* calibration timer */
83 --- a/net80211/_ieee80211.h
84 +++ b/net80211/_ieee80211.h
85 @@ -148,6 +148,7 @@ struct ieee80211_channel {
86 int8_t ic_maxpower; /* maximum tx power in dBm */
87 int8_t ic_minpower; /* minimum tx power in dBm */
88 u_int8_t ic_scanflags;
89 + u_int8_t ic_idletime; /* phy idle time in % */
90 };
91
92 #define IEEE80211_CHAN_MAX 255
93 --- a/net80211/ieee80211_scan_ap.c
94 +++ b/net80211/ieee80211_scan_ap.c
95 @@ -423,6 +423,19 @@ pc_cmp_rssi(struct ap_state *as, struct
96
97 /* This function must be invoked with locks acquired */
98 static int
99 +pc_cmp_idletime(struct ieee80211_channel *a,
100 + struct ieee80211_channel *b)
101 +{
102 + if (!a->ic_idletime || !b->ic_idletime)
103 + return 0;
104 +
105 + /* a is better than b (return < 0) when a has more idle time than b */
106 + return b->ic_idletime - a->ic_idletime;
107 +}
108 +
109 +
110 +/* This function must be invoked with locks acquired */
111 +static int
112 pc_cmp_samechan(struct ieee80211com *ic, struct ieee80211_channel *a,
113 struct ieee80211_channel *b)
114 {
115 @@ -455,6 +468,7 @@ pc_cmp(const void *_a, const void *_b)
116 return res; \
117 } while (0)
118
119 + EVALUATE_CRITERION(idletime, a, b);
120 EVALUATE_CRITERION(radar, a, b);
121 EVALUATE_CRITERION(keepmode, params, a, b);
122 EVALUATE_CRITERION(sc, ic, a, b);