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.
22 #include "dfs_debug.h"
24 /* internal struct to pass radar data */
25 struct ath_radar_data
{
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 */
44 static const int 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
{
57 struct ath9k_dfs_fft_40
{
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
)
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
)
104 int max_bin
[FFT_NUM_SAMPLES
];
105 struct ath_hw
*ah
= sc
->sc_ah
;
106 struct ath_common
*common
= ath9k_hw_common(ah
);
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)
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");
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
)) {
131 for (i
= 0; i
< FFT_NUM_SAMPLES
; i
++)
132 max_bin
[i
] = ath9k_get_max_index_ht40(fft
+ i
, is_ctl
,
135 struct ath9k_dfs_fft_20
*fft
= (struct ath9k_dfs_fft_20
*) data
;
136 int num_fft_packets
= datalen
/ sizeof(*fft
);
137 if (num_fft_packets
== 0)
139 ath_dbg(common
, DFS
, "HT20: datalen=%d, num_fft_packets=%d\n",
140 datalen
, num_fft_packets
);
141 if (num_fft_packets
< (FFT_NUM_SAMPLES
)) {
142 ath_dbg(common
, DFS
, "not enough packets for chirp\n");
145 /* in ht20, this is a 6-bit signed number => shift it to 0 */
146 for (i
= 0; i
< FFT_NUM_SAMPLES
; i
++)
147 max_bin
[i
] = fft_max_index(fft
[i
].lower_bins
) ^ 0x20;
149 ath_dbg(common
, DFS
, "bin_max = [%d, %d, %d, %d]\n",
150 max_bin
[0], max_bin
[1], max_bin
[2], max_bin
[3]);
152 /* Check for chirp attributes within specs
153 * a) delta of adjacent max_bins is within range
154 * b) delta of adjacent deltas are within tolerance
157 for (i
= 0; i
< NUM_DIFFS
; i
++) {
159 int delta
= max_bin
[i
+ 1] - max_bin
[i
];
161 /* ensure gradient is within valid range */
162 if (abs(delta
) < BIN_DELTA_MIN
|| abs(delta
) > BIN_DELTA_MAX
) {
163 ath_dbg(common
, DFS
, "CHIRP: invalid delta %d "
164 "in sample %d\n", delta
, i
);
169 ddelta
= delta
- prev_delta
;
170 if (abs(ddelta
) > MAX_DIFF
) {
171 ath_dbg(common
, DFS
, "CHIRP: ddelta %d too high\n",
176 ath_dbg(common
, DFS
, "CHIRP - %d: delta=%d, ddelta=%d\n",
182 /**** end: CHIRP **************************************************************/
184 /* convert pulse duration to usecs, considering clock mode */
185 static u32
dur_to_usecs(struct ath_hw
*ah
, u32 dur
)
187 const u32 AR93X_NSECS_PER_DUR
= 800;
188 const u32 AR93X_NSECS_PER_DUR_FAST
= (8000 / 11);
191 if (IS_CHAN_A_FAST_CLOCK(ah
, ah
->curchan
))
192 nsecs
= dur
* AR93X_NSECS_PER_DUR_FAST
;
194 nsecs
= dur
* AR93X_NSECS_PER_DUR
;
196 return (nsecs
+ 500) / 1000;
199 #define PRI_CH_RADAR_FOUND 0x01
200 #define EXT_CH_RADAR_FOUND 0x02
202 ath9k_postprocess_radar_event(struct ath_softc
*sc
,
203 struct ath_radar_data
*ard
,
204 struct pulse_event
*pe
)
210 * Only the last 2 bits of the BW info are relevant, they indicate
211 * which channel the radar was detected in.
213 ard
->pulse_bw_info
&= 0x03;
215 switch (ard
->pulse_bw_info
) {
216 case PRI_CH_RADAR_FOUND
:
217 /* radar in ctrl channel */
218 dur
= ard
->pulse_length_pri
;
219 DFS_STAT_INC(sc
, pri_phy_errors
);
221 * cannot use ctrl channel RSSI
222 * if extension channel is stronger
224 rssi
= (ard
->ext_rssi
>= (ard
->rssi
+ 3)) ? 0 : ard
->rssi
;
226 case EXT_CH_RADAR_FOUND
:
227 /* radar in extension channel */
228 dur
= ard
->pulse_length_ext
;
229 DFS_STAT_INC(sc
, ext_phy_errors
);
231 * cannot use extension channel RSSI
232 * if control channel is stronger
234 rssi
= (ard
->rssi
>= (ard
->ext_rssi
+ 12)) ? 0 : ard
->ext_rssi
;
236 case (PRI_CH_RADAR_FOUND
| EXT_CH_RADAR_FOUND
):
238 * Conducted testing, when pulse is on DC, both pri and ext
239 * durations are reported to be same
241 * Radiated testing, when pulse is on DC, different pri and
242 * ext durations are reported, so take the larger of the two
244 if (ard
->pulse_length_ext
>= ard
->pulse_length_pri
)
245 dur
= ard
->pulse_length_ext
;
247 dur
= ard
->pulse_length_pri
;
248 DFS_STAT_INC(sc
, dc_phy_errors
);
250 /* when both are present use stronger one */
251 rssi
= (ard
->rssi
< ard
->ext_rssi
) ? ard
->ext_rssi
: ard
->rssi
;
255 * Bogus bandwidth info was received in descriptor,
256 * so ignore this PHY error
258 DFS_STAT_INC(sc
, bwinfo_discards
);
263 DFS_STAT_INC(sc
, rssi_discards
);
267 /* convert duration to usecs */
268 pe
->width
= dur_to_usecs(sc
->sc_ah
, dur
);
271 DFS_STAT_INC(sc
, pulses_detected
);
276 ath9k_dfs_process_radar_pulse(struct ath_softc
*sc
, struct pulse_event
*pe
)
278 struct dfs_pattern_detector
*pd
= sc
->dfs_detector
;
279 DFS_STAT_INC(sc
, pulses_processed
);
282 if (!pd
->add_pulse(pd
, pe
))
284 DFS_STAT_INC(sc
, radar_detected
);
285 ieee80211_radar_detected(sc
->hw
);
289 * DFS: check PHY-error for radar pulse and feed the detector
291 void ath9k_dfs_process_phyerr(struct ath_softc
*sc
, void *data
,
292 struct ath_rx_status
*rs
, u64 mactime
)
294 struct ath_radar_data ard
;
297 struct pulse_event pe
;
298 struct ath_hw
*ah
= sc
->sc_ah
;
299 struct ath_common
*common
= ath9k_hw_common(ah
);
301 DFS_STAT_INC(sc
, pulses_total
);
302 if ((rs
->rs_phyerr
!= ATH9K_PHYERR_RADAR
) &&
303 (rs
->rs_phyerr
!= ATH9K_PHYERR_FALSE_RADAR_EXT
)) {
305 "Error: rs_phyer=0x%x not a radar error\n",
307 DFS_STAT_INC(sc
, pulses_no_dfs
);
311 datalen
= rs
->rs_datalen
;
313 DFS_STAT_INC(sc
, datalen_discards
);
317 ard
.rssi
= rs
->rs_rssi_ctl
[0];
318 ard
.ext_rssi
= rs
->rs_rssi_ext
[0];
321 * hardware stores this as 8 bit signed value.
322 * we will cap it at 0 if it is a negative number
326 if (ard
.ext_rssi
& 0x80)
329 vdata_end
= (char *)data
+ datalen
;
330 ard
.pulse_bw_info
= vdata_end
[-1];
331 ard
.pulse_length_ext
= vdata_end
[-2];
332 ard
.pulse_length_pri
= vdata_end
[-3];
333 pe
.freq
= ah
->curchan
->channel
;
335 if (!ath9k_postprocess_radar_event(sc
, &ard
, &pe
))
338 if (pe
.width
> MIN_CHIRP_PULSE_WIDTH
&&
339 pe
.width
< MAX_CHIRP_PULSE_WIDTH
) {
340 bool is_ctl
= !!(ard
.pulse_bw_info
& PRI_CH_RADAR_FOUND
);
341 bool is_ext
= !!(ard
.pulse_bw_info
& EXT_CH_RADAR_FOUND
);
342 int clen
= datalen
- 3;
343 pe
.chirp
= ath9k_check_chirping(sc
, data
, clen
, is_ctl
, is_ext
);
349 "ath9k_dfs_process_phyerr: type=%d, freq=%d, ts=%llu, "
350 "width=%d, rssi=%d, delta_ts=%llu\n",
351 ard
.pulse_bw_info
, pe
.freq
, pe
.ts
, pe
.width
, pe
.rssi
,
352 pe
.ts
- sc
->dfs_prev_pulse_ts
);
353 sc
->dfs_prev_pulse_ts
= pe
.ts
;
354 if (ard
.pulse_bw_info
& PRI_CH_RADAR_FOUND
)
355 ath9k_dfs_process_radar_pulse(sc
, &pe
);
356 if (IS_CHAN_HT40(ah
->curchan
) &&
357 ard
.pulse_bw_info
& EXT_CH_RADAR_FOUND
) {
358 pe
.freq
+= IS_CHAN_HT40PLUS(ah
->curchan
) ? 20 : -20;
359 ath9k_dfs_process_radar_pulse(sc
, &pe
);
362 #undef PRI_CH_RADAR_FOUND
363 #undef EXT_CH_RADAR_FOUND