gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / net / wireless / ath / ath9k / dfs.c
blobacb9602aa464feaeceec0469b3f07f43767329ac
1 /*
2 * Copyright (c) 2008-2011 Atheros Communications Inc.
3 * Copyright (c) 2011 Neratec Solutions AG
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include "hw.h"
19 #include "hw-ops.h"
20 #include "ath9k.h"
21 #include "dfs.h"
22 #include "dfs_debug.h"
24 /* internal struct to pass radar data */
25 struct ath_radar_data {
26 u8 pulse_bw_info;
27 u8 rssi;
28 u8 ext_rssi;
29 u8 pulse_length_ext;
30 u8 pulse_length_pri;
33 /**** begin: CHIRP ************************************************************/
35 /* min and max gradients for defined FCC chirping pulses, given by
36 * - 20MHz chirp width over a pulse width of 50us
37 * - 5MHz chirp width over a pulse width of 100us
39 static const int BIN_DELTA_MIN = 1;
40 static const int BIN_DELTA_MAX = 10;
42 /* we need at least 3 deltas / 4 samples for a reliable chirp detection */
43 #define NUM_DIFFS 3
44 #define FFT_NUM_SAMPLES (NUM_DIFFS + 1)
46 /* Threshold for difference of delta peaks */
47 static const int MAX_DIFF = 2;
49 /* width range to be checked for chirping */
50 static const int MIN_CHIRP_PULSE_WIDTH = 20;
51 static const int MAX_CHIRP_PULSE_WIDTH = 110;
53 struct ath9k_dfs_fft_20 {
54 u8 bin[28];
55 u8 lower_bins[3];
56 } __packed;
57 struct ath9k_dfs_fft_40 {
58 u8 bin[64];
59 u8 lower_bins[3];
60 u8 upper_bins[3];
61 } __packed;
63 static inline int fft_max_index(u8 *bins)
65 return (bins[2] & 0xfc) >> 2;
67 static inline int fft_max_magnitude(u8 *bins)
69 return (bins[0] & 0xc0) >> 6 | bins[1] << 2 | (bins[2] & 0x03) << 10;
71 static inline u8 fft_bitmap_weight(u8 *bins)
73 return bins[0] & 0x3f;
76 static int ath9k_get_max_index_ht40(struct ath9k_dfs_fft_40 *fft,
77 bool is_ctl, bool is_ext)
79 const int DFS_UPPER_BIN_OFFSET = 64;
80 /* if detected radar on both channels, select the significant one */
81 if (is_ctl && is_ext) {
82 /* first check wether channels have 'strong' bins */
83 is_ctl = fft_bitmap_weight(fft->lower_bins) != 0;
84 is_ext = fft_bitmap_weight(fft->upper_bins) != 0;
86 /* if still unclear, take higher magnitude */
87 if (is_ctl && is_ext) {
88 int mag_lower = fft_max_magnitude(fft->lower_bins);
89 int mag_upper = fft_max_magnitude(fft->upper_bins);
90 if (mag_upper > mag_lower)
91 is_ctl = false;
92 else
93 is_ext = false;
96 if (is_ctl)
97 return fft_max_index(fft->lower_bins);
98 return fft_max_index(fft->upper_bins) + DFS_UPPER_BIN_OFFSET;
100 static bool ath9k_check_chirping(struct ath_softc *sc, u8 *data,
101 int datalen, bool is_ctl, bool is_ext)
103 int i;
104 int max_bin[FFT_NUM_SAMPLES];
105 struct ath_hw *ah = sc->sc_ah;
106 struct ath_common *common = ath9k_hw_common(ah);
107 int prev_delta;
109 if (IS_CHAN_HT40(ah->curchan)) {
110 struct ath9k_dfs_fft_40 *fft = (struct ath9k_dfs_fft_40 *) data;
111 int num_fft_packets = datalen / sizeof(*fft);
112 if (num_fft_packets == 0)
113 return false;
115 ath_dbg(common, DFS, "HT40: datalen=%d, num_fft_packets=%d\n",
116 datalen, num_fft_packets);
117 if (num_fft_packets < FFT_NUM_SAMPLES) {
118 ath_dbg(common, DFS, "not enough packets for chirp\n");
119 return false;
121 /* HW sometimes adds 2 garbage bytes in front of FFT samples */
122 if ((datalen % sizeof(*fft)) == 2) {
123 fft = (struct ath9k_dfs_fft_40 *) (data + 2);
124 ath_dbg(common, DFS, "fixing datalen by 2\n");
126 if (IS_CHAN_HT40MINUS(ah->curchan))
127 swap(is_ctl, is_ext);
129 for (i = 0; i < FFT_NUM_SAMPLES; i++)
130 max_bin[i] = ath9k_get_max_index_ht40(fft + i, is_ctl,
131 is_ext);
132 } else {
133 struct ath9k_dfs_fft_20 *fft = (struct ath9k_dfs_fft_20 *) data;
134 int num_fft_packets = datalen / sizeof(*fft);
135 if (num_fft_packets == 0)
136 return false;
137 ath_dbg(common, DFS, "HT20: datalen=%d, num_fft_packets=%d\n",
138 datalen, num_fft_packets);
139 if (num_fft_packets < FFT_NUM_SAMPLES) {
140 ath_dbg(common, DFS, "not enough packets for chirp\n");
141 return false;
143 /* in ht20, this is a 6-bit signed number => shift it to 0 */
144 for (i = 0; i < FFT_NUM_SAMPLES; i++)
145 max_bin[i] = fft_max_index(fft[i].lower_bins) ^ 0x20;
147 ath_dbg(common, DFS, "bin_max = [%d, %d, %d, %d]\n",
148 max_bin[0], max_bin[1], max_bin[2], max_bin[3]);
150 /* Check for chirp attributes within specs
151 * a) delta of adjacent max_bins is within range
152 * b) delta of adjacent deltas are within tolerance
154 prev_delta = 0;
155 for (i = 0; i < NUM_DIFFS; i++) {
156 int ddelta = -1;
157 int delta = max_bin[i + 1] - max_bin[i];
159 /* ensure gradient is within valid range */
160 if (abs(delta) < BIN_DELTA_MIN || abs(delta) > BIN_DELTA_MAX) {
161 ath_dbg(common, DFS, "CHIRP: invalid delta %d "
162 "in sample %d\n", delta, i);
163 return false;
165 if (i == 0)
166 goto done;
167 ddelta = delta - prev_delta;
168 if (abs(ddelta) > MAX_DIFF) {
169 ath_dbg(common, DFS, "CHIRP: ddelta %d too high\n",
170 ddelta);
171 return false;
173 done:
174 ath_dbg(common, DFS, "CHIRP - %d: delta=%d, ddelta=%d\n",
175 i, delta, ddelta);
176 prev_delta = delta;
178 return true;
180 /**** end: CHIRP **************************************************************/
182 /* convert pulse duration to usecs, considering clock mode */
183 static u32 dur_to_usecs(struct ath_hw *ah, u32 dur)
185 const u32 AR93X_NSECS_PER_DUR = 800;
186 const u32 AR93X_NSECS_PER_DUR_FAST = (8000 / 11);
187 u32 nsecs;
189 if (IS_CHAN_A_FAST_CLOCK(ah, ah->curchan))
190 nsecs = dur * AR93X_NSECS_PER_DUR_FAST;
191 else
192 nsecs = dur * AR93X_NSECS_PER_DUR;
194 return (nsecs + 500) / 1000;
197 #define PRI_CH_RADAR_FOUND 0x01
198 #define EXT_CH_RADAR_FOUND 0x02
199 static bool
200 ath9k_postprocess_radar_event(struct ath_softc *sc,
201 struct ath_radar_data *ard,
202 struct pulse_event *pe)
204 u8 rssi;
205 u16 dur;
208 * Only the last 2 bits of the BW info are relevant, they indicate
209 * which channel the radar was detected in.
211 ard->pulse_bw_info &= 0x03;
213 switch (ard->pulse_bw_info) {
214 case PRI_CH_RADAR_FOUND:
215 /* radar in ctrl channel */
216 dur = ard->pulse_length_pri;
217 DFS_STAT_INC(sc, pri_phy_errors);
219 * cannot use ctrl channel RSSI
220 * if extension channel is stronger
222 rssi = (ard->ext_rssi >= (ard->rssi + 3)) ? 0 : ard->rssi;
223 break;
224 case EXT_CH_RADAR_FOUND:
225 /* radar in extension channel */
226 dur = ard->pulse_length_ext;
227 DFS_STAT_INC(sc, ext_phy_errors);
229 * cannot use extension channel RSSI
230 * if control channel is stronger
232 rssi = (ard->rssi >= (ard->ext_rssi + 12)) ? 0 : ard->ext_rssi;
233 break;
234 case (PRI_CH_RADAR_FOUND | EXT_CH_RADAR_FOUND):
236 * Conducted testing, when pulse is on DC, both pri and ext
237 * durations are reported to be same
239 * Radiated testing, when pulse is on DC, different pri and
240 * ext durations are reported, so take the larger of the two
242 if (ard->pulse_length_ext >= ard->pulse_length_pri)
243 dur = ard->pulse_length_ext;
244 else
245 dur = ard->pulse_length_pri;
246 DFS_STAT_INC(sc, dc_phy_errors);
248 /* when both are present use stronger one */
249 rssi = (ard->rssi < ard->ext_rssi) ? ard->ext_rssi : ard->rssi;
250 break;
251 default:
253 * Bogus bandwidth info was received in descriptor,
254 * so ignore this PHY error
256 DFS_STAT_INC(sc, bwinfo_discards);
257 return false;
260 if (rssi == 0) {
261 DFS_STAT_INC(sc, rssi_discards);
262 return false;
265 /* convert duration to usecs */
266 pe->width = dur_to_usecs(sc->sc_ah, dur);
267 pe->rssi = rssi;
269 DFS_STAT_INC(sc, pulses_detected);
270 return true;
273 static void
274 ath9k_dfs_process_radar_pulse(struct ath_softc *sc, struct pulse_event *pe)
276 struct dfs_pattern_detector *pd = sc->dfs_detector;
277 DFS_STAT_INC(sc, pulses_processed);
278 if (pd == NULL)
279 return;
280 if (!pd->add_pulse(pd, pe, NULL))
281 return;
282 DFS_STAT_INC(sc, radar_detected);
283 ieee80211_radar_detected(sc->hw);
287 * DFS: check PHY-error for radar pulse and feed the detector
289 void ath9k_dfs_process_phyerr(struct ath_softc *sc, void *data,
290 struct ath_rx_status *rs, u64 mactime)
292 struct ath_radar_data ard;
293 u16 datalen;
294 char *vdata_end;
295 struct pulse_event pe;
296 struct ath_hw *ah = sc->sc_ah;
297 struct ath_common *common = ath9k_hw_common(ah);
299 DFS_STAT_INC(sc, pulses_total);
300 if ((rs->rs_phyerr != ATH9K_PHYERR_RADAR) &&
301 (rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT)) {
302 ath_dbg(common, DFS,
303 "Error: rs_phyer=0x%x not a radar error\n",
304 rs->rs_phyerr);
305 DFS_STAT_INC(sc, pulses_no_dfs);
306 return;
309 datalen = rs->rs_datalen;
310 if (datalen == 0) {
311 DFS_STAT_INC(sc, datalen_discards);
312 return;
315 ard.rssi = rs->rs_rssi_ctl[0];
316 ard.ext_rssi = rs->rs_rssi_ext[0];
319 * hardware stores this as 8 bit signed value.
320 * we will cap it at 0 if it is a negative number
322 if (ard.rssi & 0x80)
323 ard.rssi = 0;
324 if (ard.ext_rssi & 0x80)
325 ard.ext_rssi = 0;
327 vdata_end = data + datalen;
328 ard.pulse_bw_info = vdata_end[-1];
329 ard.pulse_length_ext = vdata_end[-2];
330 ard.pulse_length_pri = vdata_end[-3];
331 pe.freq = ah->curchan->channel;
332 pe.ts = mactime;
333 if (!ath9k_postprocess_radar_event(sc, &ard, &pe))
334 return;
336 if (pe.width > MIN_CHIRP_PULSE_WIDTH &&
337 pe.width < MAX_CHIRP_PULSE_WIDTH) {
338 bool is_ctl = !!(ard.pulse_bw_info & PRI_CH_RADAR_FOUND);
339 bool is_ext = !!(ard.pulse_bw_info & EXT_CH_RADAR_FOUND);
340 int clen = datalen - 3;
341 pe.chirp = ath9k_check_chirping(sc, data, clen, is_ctl, is_ext);
342 } else {
343 pe.chirp = false;
346 ath_dbg(common, DFS,
347 "ath9k_dfs_process_phyerr: type=%d, freq=%d, ts=%llu, "
348 "width=%d, rssi=%d, delta_ts=%llu\n",
349 ard.pulse_bw_info, pe.freq, pe.ts, pe.width, pe.rssi,
350 pe.ts - sc->dfs_prev_pulse_ts);
351 sc->dfs_prev_pulse_ts = pe.ts;
352 if (ard.pulse_bw_info & PRI_CH_RADAR_FOUND)
353 ath9k_dfs_process_radar_pulse(sc, &pe);
354 if (IS_CHAN_HT40(ah->curchan) &&
355 ard.pulse_bw_info & EXT_CH_RADAR_FOUND) {
356 pe.freq += IS_CHAN_HT40PLUS(ah->curchan) ? 20 : -20;
357 ath9k_dfs_process_radar_pulse(sc, &pe);
360 #undef PRI_CH_RADAR_FOUND
361 #undef EXT_CH_RADAR_FOUND