1/* 2 * Copyright (c) 2013 Qualcomm Atheros, Inc. 3 * 4 * Permission to use, copy, modify, and/or distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17#include <linux/relay.h> 18#include "ath9k.h" 19 20static s8 fix_rssi_inv_only(u8 rssi_val) 21{ 22 if (rssi_val == 128) 23 rssi_val = 0; 24 return (s8) rssi_val; 25} 26 27static void ath_debug_send_fft_sample(struct ath_spec_scan_priv *spec_priv, 28 struct fft_sample_tlv *fft_sample_tlv) 29{ 30 int length; 31 if (!spec_priv->rfs_chan_spec_scan) 32 return; 33 34 length = __be16_to_cpu(fft_sample_tlv->length) + 35 sizeof(*fft_sample_tlv); 36 relay_write(spec_priv->rfs_chan_spec_scan, fft_sample_tlv, length); 37} 38 39/* returns 1 if this was a spectral frame, even if not handled. */ 40int ath_cmn_process_fft(struct ath_spec_scan_priv *spec_priv, struct ieee80211_hdr *hdr, 41 struct ath_rx_status *rs, u64 tsf) 42{ 43 struct ath_hw *ah = spec_priv->ah; 44 struct ath_common *common = ath9k_hw_common(spec_priv->ah); 45 u8 num_bins, *bins, *vdata = (u8 *)hdr; 46 struct fft_sample_ht20 fft_sample_20; 47 struct fft_sample_ht20_40 fft_sample_40; 48 struct fft_sample_tlv *tlv; 49 struct ath_radar_info *radar_info; 50 int len = rs->rs_datalen; 51 int dc_pos; 52 u16 fft_len, length, freq = ah->curchan->chan->center_freq; 53 enum nl80211_channel_type chan_type; 54 55 /* AR9280 and before report via ATH9K_PHYERR_RADAR, AR93xx and newer 56 * via ATH9K_PHYERR_SPECTRAL. Haven't seen ATH9K_PHYERR_FALSE_RADAR_EXT 57 * yet, but this is supposed to be possible as well. 58 */ 59 if (rs->rs_phyerr != ATH9K_PHYERR_RADAR && 60 rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT && 61 rs->rs_phyerr != ATH9K_PHYERR_SPECTRAL) 62 return 0; 63 64 /* check if spectral scan bit is set. This does not have to be checked 65 * if received through a SPECTRAL phy error, but shouldn't hurt. 66 */ 67 radar_info = ((struct ath_radar_info *)&vdata[len]) - 1; 68 if (!(radar_info->pulse_bw_info & SPECTRAL_SCAN_BITMASK)) 69 return 0; 70 71 chan_type = cfg80211_get_chandef_type(&common->hw->conf.chandef); 72 if ((chan_type == NL80211_CHAN_HT40MINUS) || 73 (chan_type == NL80211_CHAN_HT40PLUS)) { 74 fft_len = SPECTRAL_HT20_40_TOTAL_DATA_LEN; 75 num_bins = SPECTRAL_HT20_40_NUM_BINS; 76 bins = (u8 *)fft_sample_40.data; 77 } else { 78 fft_len = SPECTRAL_HT20_TOTAL_DATA_LEN; 79 num_bins = SPECTRAL_HT20_NUM_BINS; 80 bins = (u8 *)fft_sample_20.data; 81 } 82 83 /* Variation in the data length is possible and will be fixed later */ 84 if ((len > fft_len + 2) || (len < fft_len - 1)) 85 return 1; 86 87 switch (len - fft_len) { 88 case 0: 89 /* length correct, nothing to do. */ 90 memcpy(bins, vdata, num_bins); 91 break; 92 case -1: 93 /* first byte missing, duplicate it. */ 94 memcpy(&bins[1], vdata, num_bins - 1); 95 bins[0] = vdata[0]; 96 break; 97 case 2: 98 /* MAC added 2 extra bytes at bin 30 and 32, remove them. */ 99 memcpy(bins, vdata, 30); 100 bins[30] = vdata[31]; 101 memcpy(&bins[31], &vdata[33], num_bins - 31); 102 break; 103 case 1: 104 /* MAC added 2 extra bytes AND first byte is missing. */ 105 bins[0] = vdata[0]; 106 memcpy(&bins[1], vdata, 30); 107 bins[31] = vdata[31]; 108 memcpy(&bins[32], &vdata[33], num_bins - 32); 109 break; 110 default: 111 return 1; 112 } 113 114 /* DC value (value in the middle) is the blind spot of the spectral 115 * sample and invalid, interpolate it. 116 */ 117 dc_pos = num_bins / 2; 118 bins[dc_pos] = (bins[dc_pos + 1] + bins[dc_pos - 1]) / 2; 119 120 if ((chan_type == NL80211_CHAN_HT40MINUS) || 121 (chan_type == NL80211_CHAN_HT40PLUS)) { 122 s8 lower_rssi, upper_rssi; 123 s16 ext_nf; 124 u8 lower_max_index, upper_max_index; 125 u8 lower_bitmap_w, upper_bitmap_w; 126 u16 lower_mag, upper_mag; 127 struct ath9k_hw_cal_data *caldata = ah->caldata; 128 struct ath_ht20_40_mag_info *mag_info; 129 130 if (caldata) 131 ext_nf = ath9k_hw_getchan_noise(ah, ah->curchan, 132 caldata->nfCalHist[3].privNF); 133 else 134 ext_nf = ATH_DEFAULT_NOISE_FLOOR; 135 136 length = sizeof(fft_sample_40) - sizeof(struct fft_sample_tlv); 137 fft_sample_40.tlv.type = ATH_FFT_SAMPLE_HT20_40; 138 fft_sample_40.tlv.length = __cpu_to_be16(length); 139 fft_sample_40.freq = __cpu_to_be16(freq); 140 fft_sample_40.channel_type = chan_type; 141 142 if (chan_type == NL80211_CHAN_HT40PLUS) { 143 lower_rssi = fix_rssi_inv_only(rs->rs_rssi_ctl[0]); 144 upper_rssi = fix_rssi_inv_only(rs->rs_rssi_ext[0]); 145 146 fft_sample_40.lower_noise = ah->noise; 147 fft_sample_40.upper_noise = ext_nf; 148 } else { 149 lower_rssi = fix_rssi_inv_only(rs->rs_rssi_ext[0]); 150 upper_rssi = fix_rssi_inv_only(rs->rs_rssi_ctl[0]); 151 152 fft_sample_40.lower_noise = ext_nf; 153 fft_sample_40.upper_noise = ah->noise; 154 } 155 fft_sample_40.lower_rssi = lower_rssi; 156 fft_sample_40.upper_rssi = upper_rssi; 157 158 mag_info = ((struct ath_ht20_40_mag_info *)radar_info) - 1; 159 lower_mag = spectral_max_magnitude(mag_info->lower_bins); 160 upper_mag = spectral_max_magnitude(mag_info->upper_bins); 161 fft_sample_40.lower_max_magnitude = __cpu_to_be16(lower_mag); 162 fft_sample_40.upper_max_magnitude = __cpu_to_be16(upper_mag); 163 lower_max_index = spectral_max_index(mag_info->lower_bins); 164 upper_max_index = spectral_max_index(mag_info->upper_bins); 165 fft_sample_40.lower_max_index = lower_max_index; 166 fft_sample_40.upper_max_index = upper_max_index; 167 lower_bitmap_w = spectral_bitmap_weight(mag_info->lower_bins); 168 upper_bitmap_w = spectral_bitmap_weight(mag_info->upper_bins); 169 fft_sample_40.lower_bitmap_weight = lower_bitmap_w; 170 fft_sample_40.upper_bitmap_weight = upper_bitmap_w; 171 fft_sample_40.max_exp = mag_info->max_exp & 0xf; 172 173 fft_sample_40.tsf = __cpu_to_be64(tsf); 174 175 tlv = (struct fft_sample_tlv *)&fft_sample_40; 176 } else { 177 u8 max_index, bitmap_w; 178 u16 magnitude; 179 struct ath_ht20_mag_info *mag_info; 180 181 length = sizeof(fft_sample_20) - sizeof(struct fft_sample_tlv); 182 fft_sample_20.tlv.type = ATH_FFT_SAMPLE_HT20; 183 fft_sample_20.tlv.length = __cpu_to_be16(length); 184 fft_sample_20.freq = __cpu_to_be16(freq); 185 186 fft_sample_20.rssi = fix_rssi_inv_only(rs->rs_rssi_ctl[0]); 187 fft_sample_20.noise = ah->noise; 188 189 mag_info = ((struct ath_ht20_mag_info *)radar_info) - 1; 190 magnitude = spectral_max_magnitude(mag_info->all_bins); 191 fft_sample_20.max_magnitude = __cpu_to_be16(magnitude); 192 max_index = spectral_max_index(mag_info->all_bins); 193 fft_sample_20.max_index = max_index; 194 bitmap_w = spectral_bitmap_weight(mag_info->all_bins); 195 fft_sample_20.bitmap_weight = bitmap_w; 196 fft_sample_20.max_exp = mag_info->max_exp & 0xf; 197 198 fft_sample_20.tsf = __cpu_to_be64(tsf); 199 200 tlv = (struct fft_sample_tlv *)&fft_sample_20; 201 } 202 203 ath_debug_send_fft_sample(spec_priv, tlv); 204 205 return 1; 206} 207EXPORT_SYMBOL(ath_cmn_process_fft); 208 209/*********************/ 210/* spectral_scan_ctl */ 211/*********************/ 212 213static ssize_t read_file_spec_scan_ctl(struct file *file, char __user *user_buf, 214 size_t count, loff_t *ppos) 215{ 216 struct ath_spec_scan_priv *spec_priv = file->private_data; 217 char *mode = ""; 218 unsigned int len; 219 220 switch (spec_priv->spectral_mode) { 221 case SPECTRAL_DISABLED: 222 mode = "disable"; 223 break; 224 case SPECTRAL_BACKGROUND: 225 mode = "background"; 226 break; 227 case SPECTRAL_CHANSCAN: 228 mode = "chanscan"; 229 break; 230 case SPECTRAL_MANUAL: 231 mode = "manual"; 232 break; 233 } 234 len = strlen(mode); 235 return simple_read_from_buffer(user_buf, count, ppos, mode, len); 236} 237 238void ath9k_cmn_spectral_scan_trigger(struct ath_common *common, 239 struct ath_spec_scan_priv *spec_priv) 240{ 241 struct ath_hw *ah = spec_priv->ah; 242 u32 rxfilter; 243 244 if (config_enabled(CONFIG_ATH9K_TX99)) 245 return; 246 247 if (!ath9k_hw_ops(ah)->spectral_scan_trigger) { 248 ath_err(common, "spectrum analyzer not implemented on this hardware\n"); 249 return; 250 } 251 252 ath_ps_ops(common)->wakeup(common); 253 rxfilter = ath9k_hw_getrxfilter(ah); 254 ath9k_hw_setrxfilter(ah, rxfilter | 255 ATH9K_RX_FILTER_PHYRADAR | 256 ATH9K_RX_FILTER_PHYERR); 257 258 /* TODO: usually this should not be neccesary, but for some reason 259 * (or in some mode?) the trigger must be called after the 260 * configuration, otherwise the register will have its values reset 261 * (on my ar9220 to value 0x01002310) 262 */ 263 ath9k_cmn_spectral_scan_config(common, spec_priv, spec_priv->spectral_mode); 264 ath9k_hw_ops(ah)->spectral_scan_trigger(ah); 265 ath_ps_ops(common)->restore(common); 266} 267EXPORT_SYMBOL(ath9k_cmn_spectral_scan_trigger); 268 269int ath9k_cmn_spectral_scan_config(struct ath_common *common, 270 struct ath_spec_scan_priv *spec_priv, 271 enum spectral_mode spectral_mode) 272{ 273 struct ath_hw *ah = spec_priv->ah; 274 275 if (!ath9k_hw_ops(ah)->spectral_scan_trigger) { 276 ath_err(common, "spectrum analyzer not implemented on this hardware\n"); 277 return -1; 278 } 279 280 switch (spectral_mode) { 281 case SPECTRAL_DISABLED: 282 spec_priv->spec_config.enabled = 0; 283 break; 284 case SPECTRAL_BACKGROUND: 285 /* send endless samples. 286 * TODO: is this really useful for "background"? 287 */ 288 spec_priv->spec_config.endless = 1; 289 spec_priv->spec_config.enabled = 1; 290 break; 291 case SPECTRAL_CHANSCAN: 292 case SPECTRAL_MANUAL: 293 spec_priv->spec_config.endless = 0; 294 spec_priv->spec_config.enabled = 1; 295 break; 296 default: 297 return -1; 298 } 299 300 ath_ps_ops(common)->wakeup(common); 301 ath9k_hw_ops(ah)->spectral_scan_config(ah, &spec_priv->spec_config); 302 ath_ps_ops(common)->restore(common); 303 304 spec_priv->spectral_mode = spectral_mode; 305 306 return 0; 307} 308EXPORT_SYMBOL(ath9k_cmn_spectral_scan_config); 309 310static ssize_t write_file_spec_scan_ctl(struct file *file, 311 const char __user *user_buf, 312 size_t count, loff_t *ppos) 313{ 314 struct ath_spec_scan_priv *spec_priv = file->private_data; 315 struct ath_common *common = ath9k_hw_common(spec_priv->ah); 316 char buf[32]; 317 ssize_t len; 318 319 if (config_enabled(CONFIG_ATH9K_TX99)) 320 return -EOPNOTSUPP; 321 322 len = min(count, sizeof(buf) - 1); 323 if (copy_from_user(buf, user_buf, len)) 324 return -EFAULT; 325 326 buf[len] = '\0'; 327 328 if (strncmp("trigger", buf, 7) == 0) { 329 ath9k_cmn_spectral_scan_trigger(common, spec_priv); 330 } else if (strncmp("background", buf, 10) == 0) { 331 ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_BACKGROUND); 332 ath_dbg(common, CONFIG, "spectral scan: background mode enabled\n"); 333 } else if (strncmp("chanscan", buf, 8) == 0) { 334 ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_CHANSCAN); 335 ath_dbg(common, CONFIG, "spectral scan: channel scan mode enabled\n"); 336 } else if (strncmp("manual", buf, 6) == 0) { 337 ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_MANUAL); 338 ath_dbg(common, CONFIG, "spectral scan: manual mode enabled\n"); 339 } else if (strncmp("disable", buf, 7) == 0) { 340 ath9k_cmn_spectral_scan_config(common, spec_priv, SPECTRAL_DISABLED); 341 ath_dbg(common, CONFIG, "spectral scan: disabled\n"); 342 } else { 343 return -EINVAL; 344 } 345 346 return count; 347} 348 349static const struct file_operations fops_spec_scan_ctl = { 350 .read = read_file_spec_scan_ctl, 351 .write = write_file_spec_scan_ctl, 352 .open = simple_open, 353 .owner = THIS_MODULE, 354 .llseek = default_llseek, 355}; 356 357/*************************/ 358/* spectral_short_repeat */ 359/*************************/ 360 361static ssize_t read_file_spectral_short_repeat(struct file *file, 362 char __user *user_buf, 363 size_t count, loff_t *ppos) 364{ 365 struct ath_spec_scan_priv *spec_priv = file->private_data; 366 char buf[32]; 367 unsigned int len; 368 369 len = sprintf(buf, "%d\n", spec_priv->spec_config.short_repeat); 370 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 371} 372 373static ssize_t write_file_spectral_short_repeat(struct file *file, 374 const char __user *user_buf, 375 size_t count, loff_t *ppos) 376{ 377 struct ath_spec_scan_priv *spec_priv = file->private_data; 378 unsigned long val; 379 char buf[32]; 380 ssize_t len; 381 382 len = min(count, sizeof(buf) - 1); 383 if (copy_from_user(buf, user_buf, len)) 384 return -EFAULT; 385 386 buf[len] = '\0'; 387 if (kstrtoul(buf, 0, &val)) 388 return -EINVAL; 389 390 if (val > 1) 391 return -EINVAL; 392 393 spec_priv->spec_config.short_repeat = val; 394 return count; 395} 396 397static const struct file_operations fops_spectral_short_repeat = { 398 .read = read_file_spectral_short_repeat, 399 .write = write_file_spectral_short_repeat, 400 .open = simple_open, 401 .owner = THIS_MODULE, 402 .llseek = default_llseek, 403}; 404 405/******************/ 406/* spectral_count */ 407/******************/ 408 409static ssize_t read_file_spectral_count(struct file *file, 410 char __user *user_buf, 411 size_t count, loff_t *ppos) 412{ 413 struct ath_spec_scan_priv *spec_priv = file->private_data; 414 char buf[32]; 415 unsigned int len; 416 417 len = sprintf(buf, "%d\n", spec_priv->spec_config.count); 418 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 419} 420 421static ssize_t write_file_spectral_count(struct file *file, 422 const char __user *user_buf, 423 size_t count, loff_t *ppos) 424{ 425 struct ath_spec_scan_priv *spec_priv = file->private_data; 426 unsigned long val; 427 char buf[32]; 428 ssize_t len; 429 430 len = min(count, sizeof(buf) - 1); 431 if (copy_from_user(buf, user_buf, len)) 432 return -EFAULT; 433 434 buf[len] = '\0'; 435 if (kstrtoul(buf, 0, &val)) 436 return -EINVAL; 437 438 if (val > 255) 439 return -EINVAL; 440 441 spec_priv->spec_config.count = val; 442 return count; 443} 444 445static const struct file_operations fops_spectral_count = { 446 .read = read_file_spectral_count, 447 .write = write_file_spectral_count, 448 .open = simple_open, 449 .owner = THIS_MODULE, 450 .llseek = default_llseek, 451}; 452 453/*******************/ 454/* spectral_period */ 455/*******************/ 456 457static ssize_t read_file_spectral_period(struct file *file, 458 char __user *user_buf, 459 size_t count, loff_t *ppos) 460{ 461 struct ath_spec_scan_priv *spec_priv = file->private_data; 462 char buf[32]; 463 unsigned int len; 464 465 len = sprintf(buf, "%d\n", spec_priv->spec_config.period); 466 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 467} 468 469static ssize_t write_file_spectral_period(struct file *file, 470 const char __user *user_buf, 471 size_t count, loff_t *ppos) 472{ 473 struct ath_spec_scan_priv *spec_priv = file->private_data; 474 unsigned long val; 475 char buf[32]; 476 ssize_t len; 477 478 len = min(count, sizeof(buf) - 1); 479 if (copy_from_user(buf, user_buf, len)) 480 return -EFAULT; 481 482 buf[len] = '\0'; 483 if (kstrtoul(buf, 0, &val)) 484 return -EINVAL; 485 486 if (val > 255) 487 return -EINVAL; 488 489 spec_priv->spec_config.period = val; 490 return count; 491} 492 493static const struct file_operations fops_spectral_period = { 494 .read = read_file_spectral_period, 495 .write = write_file_spectral_period, 496 .open = simple_open, 497 .owner = THIS_MODULE, 498 .llseek = default_llseek, 499}; 500 501/***********************/ 502/* spectral_fft_period */ 503/***********************/ 504 505static ssize_t read_file_spectral_fft_period(struct file *file, 506 char __user *user_buf, 507 size_t count, loff_t *ppos) 508{ 509 struct ath_spec_scan_priv *spec_priv = file->private_data; 510 char buf[32]; 511 unsigned int len; 512 513 len = sprintf(buf, "%d\n", spec_priv->spec_config.fft_period); 514 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 515} 516 517static ssize_t write_file_spectral_fft_period(struct file *file, 518 const char __user *user_buf, 519 size_t count, loff_t *ppos) 520{ 521 struct ath_spec_scan_priv *spec_priv = file->private_data; 522 unsigned long val; 523 char buf[32]; 524 ssize_t len; 525 526 len = min(count, sizeof(buf) - 1); 527 if (copy_from_user(buf, user_buf, len)) 528 return -EFAULT; 529 530 buf[len] = '\0'; 531 if (kstrtoul(buf, 0, &val)) 532 return -EINVAL; 533 534 if (val > 15) 535 return -EINVAL; 536 537 spec_priv->spec_config.fft_period = val; 538 return count; 539} 540 541static const struct file_operations fops_spectral_fft_period = { 542 .read = read_file_spectral_fft_period, 543 .write = write_file_spectral_fft_period, 544 .open = simple_open, 545 .owner = THIS_MODULE, 546 .llseek = default_llseek, 547}; 548 549/*******************/ 550/* Relay interface */ 551/*******************/ 552 553static struct dentry *create_buf_file_handler(const char *filename, 554 struct dentry *parent, 555 umode_t mode, 556 struct rchan_buf *buf, 557 int *is_global) 558{ 559 struct dentry *buf_file; 560 561 buf_file = debugfs_create_file(filename, mode, parent, buf, 562 &relay_file_operations); 563 *is_global = 1; 564 return buf_file; 565} 566 567static int remove_buf_file_handler(struct dentry *dentry) 568{ 569 debugfs_remove(dentry); 570 571 return 0; 572} 573 574static struct rchan_callbacks rfs_spec_scan_cb = { 575 .create_buf_file = create_buf_file_handler, 576 .remove_buf_file = remove_buf_file_handler, 577}; 578 579/*********************/ 580/* Debug Init/Deinit */ 581/*********************/ 582 583void ath9k_cmn_spectral_deinit_debug(struct ath_spec_scan_priv *spec_priv) 584{ 585 if (config_enabled(CONFIG_ATH9K_DEBUGFS)) { 586 relay_close(spec_priv->rfs_chan_spec_scan); 587 spec_priv->rfs_chan_spec_scan = NULL; 588 } 589} 590EXPORT_SYMBOL(ath9k_cmn_spectral_deinit_debug); 591 592void ath9k_cmn_spectral_init_debug(struct ath_spec_scan_priv *spec_priv, 593 struct dentry *debugfs_phy) 594{ 595 spec_priv->rfs_chan_spec_scan = relay_open("spectral_scan", 596 debugfs_phy, 597 1024, 256, &rfs_spec_scan_cb, 598 NULL); 599 debugfs_create_file("spectral_scan_ctl", 600 S_IRUSR | S_IWUSR, 601 debugfs_phy, spec_priv, 602 &fops_spec_scan_ctl); 603 debugfs_create_file("spectral_short_repeat", 604 S_IRUSR | S_IWUSR, 605 debugfs_phy, spec_priv, 606 &fops_spectral_short_repeat); 607 debugfs_create_file("spectral_count", 608 S_IRUSR | S_IWUSR, 609 debugfs_phy, spec_priv, 610 &fops_spectral_count); 611 debugfs_create_file("spectral_period", 612 S_IRUSR | S_IWUSR, 613 debugfs_phy, spec_priv, 614 &fops_spectral_period); 615 debugfs_create_file("spectral_fft_period", 616 S_IRUSR | S_IWUSR, 617 debugfs_phy, spec_priv, 618 &fops_spectral_fft_period); 619} 620EXPORT_SYMBOL(ath9k_cmn_spectral_init_debug); 621