iwl-spectrum.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2009 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/pci.h>
  33. #include <linux/delay.h>
  34. #include <linux/skbuff.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/wireless.h>
  37. #include <net/mac80211.h>
  38. #include "iwl-eeprom.h"
  39. #include "iwl-dev.h"
  40. #include "iwl-core.h"
  41. #include "iwl-io.h"
  42. #include "iwl-spectrum.h"
  43. #define BEACON_TIME_MASK_LOW 0x00FFFFFF
  44. #define BEACON_TIME_MASK_HIGH 0xFF000000
  45. #define TIME_UNIT 1024
  46. /*
  47. * extended beacon time format
  48. * time in usec will be changed into a 32-bit value in 8:24 format
  49. * the high 1 byte is the beacon counts
  50. * the lower 3 bytes is the time in usec within one beacon interval
  51. */
  52. /* TOOD: was used in sysfs debug interface need to add to mac */
  53. #if 0
  54. static u32 iwl_usecs_to_beacons(u32 usec, u32 beacon_interval)
  55. {
  56. u32 quot;
  57. u32 rem;
  58. u32 interval = beacon_interval * 1024;
  59. if (!interval || !usec)
  60. return 0;
  61. quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
  62. rem = (usec % interval) & BEACON_TIME_MASK_LOW;
  63. return (quot << 24) + rem;
  64. }
  65. /* base is usually what we get from ucode with each received frame,
  66. * the same as HW timer counter counting down
  67. */
  68. static __le32 iwl_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
  69. {
  70. u32 base_low = base & BEACON_TIME_MASK_LOW;
  71. u32 addon_low = addon & BEACON_TIME_MASK_LOW;
  72. u32 interval = beacon_interval * TIME_UNIT;
  73. u32 res = (base & BEACON_TIME_MASK_HIGH) +
  74. (addon & BEACON_TIME_MASK_HIGH);
  75. if (base_low > addon_low)
  76. res += base_low - addon_low;
  77. else if (base_low < addon_low) {
  78. res += interval + base_low - addon_low;
  79. res += (1 << 24);
  80. } else
  81. res += (1 << 24);
  82. return cpu_to_le32(res);
  83. }
  84. static int iwl_get_measurement(struct iwl_priv *priv,
  85. struct ieee80211_measurement_params *params,
  86. u8 type)
  87. {
  88. struct iwl4965_spectrum_cmd spectrum;
  89. struct iwl_rx_packet *res;
  90. struct iwl_host_cmd cmd = {
  91. .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
  92. .data = (void *)&spectrum,
  93. .meta.flags = CMD_WANT_SKB,
  94. };
  95. u32 add_time = le64_to_cpu(params->start_time);
  96. int rc;
  97. int spectrum_resp_status;
  98. int duration = le16_to_cpu(params->duration);
  99. if (iwl_is_associated(priv))
  100. add_time =
  101. iwl_usecs_to_beacons(
  102. le64_to_cpu(params->start_time) - priv->last_tsf,
  103. le16_to_cpu(priv->rxon_timing.beacon_interval));
  104. memset(&spectrum, 0, sizeof(spectrum));
  105. spectrum.channel_count = cpu_to_le16(1);
  106. spectrum.flags =
  107. RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
  108. spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
  109. cmd.len = sizeof(spectrum);
  110. spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
  111. if (iwl_is_associated(priv))
  112. spectrum.start_time =
  113. iwl_add_beacon_time(priv->last_beacon_time,
  114. add_time,
  115. le16_to_cpu(priv->rxon_timing.beacon_interval));
  116. else
  117. spectrum.start_time = 0;
  118. spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
  119. spectrum.channels[0].channel = params->channel;
  120. spectrum.channels[0].type = type;
  121. if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
  122. spectrum.flags |= RXON_FLG_BAND_24G_MSK |
  123. RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
  124. rc = iwl_send_cmd_sync(priv, &cmd);
  125. if (rc)
  126. return rc;
  127. res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
  128. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  129. IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
  130. rc = -EIO;
  131. }
  132. spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
  133. switch (spectrum_resp_status) {
  134. case 0: /* Command will be handled */
  135. if (res->u.spectrum.id != 0xff) {
  136. IWL_DEBUG_INFO(priv,
  137. "Replaced existing measurement: %d\n",
  138. res->u.spectrum.id);
  139. priv->measurement_status &= ~MEASUREMENT_READY;
  140. }
  141. priv->measurement_status |= MEASUREMENT_ACTIVE;
  142. rc = 0;
  143. break;
  144. case 1: /* Command will not be handled */
  145. rc = -EAGAIN;
  146. break;
  147. }
  148. dev_kfree_skb_any(cmd.meta.u.skb);
  149. return rc;
  150. }
  151. #endif
  152. static void iwl_rx_spectrum_measure_notif(struct iwl_priv *priv,
  153. struct iwl_rx_mem_buffer *rxb)
  154. {
  155. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  156. struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
  157. if (!report->state) {
  158. IWL_DEBUG_11H(priv,
  159. "Spectrum Measure Notification: Start\n");
  160. return;
  161. }
  162. memcpy(&priv->measure_report, report, sizeof(*report));
  163. priv->measurement_status |= MEASUREMENT_READY;
  164. }
  165. void iwl_setup_spectrum_handlers(struct iwl_priv *priv)
  166. {
  167. priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
  168. iwl_rx_spectrum_measure_notif;
  169. }
  170. EXPORT_SYMBOL(iwl_setup_spectrum_handlers);