common-beacon.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2008-2011 Atheros Communications 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. #include <linux/export.h>
  17. #include "common.h"
  18. #define FUDGE 2
  19. static u32 ath9k_get_next_tbtt(struct ath_hw *ah, u64 tsf,
  20. unsigned int interval)
  21. {
  22. unsigned int offset, divisor;
  23. tsf += TU_TO_USEC(FUDGE + ah->config.sw_beacon_response_time);
  24. divisor = TU_TO_USEC(interval);
  25. div_u64_rem(tsf, divisor, &offset);
  26. return (u32) tsf + divisor - offset;
  27. }
  28. /*
  29. * This sets up the beacon timers according to the timestamp of the last
  30. * received beacon and the current TSF, configures PCF and DTIM
  31. * handling, programs the sleep registers so the hardware will wakeup in
  32. * time to receive beacons, and configures the beacon miss handling so
  33. * we'll receive a BMISS interrupt when we stop seeing beacons from the AP
  34. * we've associated with.
  35. */
  36. int ath9k_cmn_beacon_config_sta(struct ath_hw *ah,
  37. struct ath_beacon_config *conf,
  38. struct ath9k_beacon_state *bs)
  39. {
  40. struct ath_common *common = ath9k_hw_common(ah);
  41. int dtim_intval;
  42. u64 tsf;
  43. /* No need to configure beacon if we are not associated */
  44. if (!test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags)) {
  45. ath_dbg(common, BEACON,
  46. "STA is not yet associated..skipping beacon config\n");
  47. return -EPERM;
  48. }
  49. memset(bs, 0, sizeof(*bs));
  50. conf->intval = conf->beacon_interval;
  51. /*
  52. * Setup dtim parameters according to
  53. * last beacon we received (which may be none).
  54. */
  55. dtim_intval = conf->intval * conf->dtim_period;
  56. /*
  57. * Pull nexttbtt forward to reflect the current
  58. * TSF and calculate dtim state for the result.
  59. */
  60. tsf = ath9k_hw_gettsf64(ah);
  61. conf->nexttbtt = ath9k_get_next_tbtt(ah, tsf, conf->intval);
  62. bs->bs_intval = TU_TO_USEC(conf->intval);
  63. bs->bs_dtimperiod = conf->dtim_period * bs->bs_intval;
  64. bs->bs_nexttbtt = conf->nexttbtt;
  65. bs->bs_nextdtim = conf->nexttbtt;
  66. if (conf->dtim_period > 1)
  67. bs->bs_nextdtim = ath9k_get_next_tbtt(ah, tsf, dtim_intval);
  68. /*
  69. * Calculate the number of consecutive beacons to miss* before taking
  70. * a BMISS interrupt. The configuration is specified in TU so we only
  71. * need calculate based on the beacon interval. Note that we clamp the
  72. * result to at most 15 beacons.
  73. */
  74. bs->bs_bmissthreshold = DIV_ROUND_UP(conf->bmiss_timeout, conf->intval);
  75. if (bs->bs_bmissthreshold > 15)
  76. bs->bs_bmissthreshold = 15;
  77. else if (bs->bs_bmissthreshold <= 0)
  78. bs->bs_bmissthreshold = 1;
  79. /*
  80. * Calculate sleep duration. The configuration is given in ms.
  81. * We ensure a multiple of the beacon period is used. Also, if the sleep
  82. * duration is greater than the DTIM period then it makes senses
  83. * to make it a multiple of that.
  84. *
  85. * XXX fixed at 100ms
  86. */
  87. bs->bs_sleepduration = TU_TO_USEC(roundup(IEEE80211_MS_TO_TU(100),
  88. conf->intval));
  89. if (bs->bs_sleepduration > bs->bs_dtimperiod)
  90. bs->bs_sleepduration = bs->bs_dtimperiod;
  91. /* TSF out of range threshold fixed at 1 second */
  92. bs->bs_tsfoor_threshold = ATH9K_TSFOOR_THRESHOLD;
  93. ath_dbg(common, BEACON, "bmiss: %u sleep: %u\n",
  94. bs->bs_bmissthreshold, bs->bs_sleepduration);
  95. return 0;
  96. }
  97. EXPORT_SYMBOL(ath9k_cmn_beacon_config_sta);
  98. void ath9k_cmn_beacon_config_adhoc(struct ath_hw *ah,
  99. struct ath_beacon_config *conf)
  100. {
  101. struct ath_common *common = ath9k_hw_common(ah);
  102. conf->intval = TU_TO_USEC(conf->beacon_interval);
  103. if (conf->ibss_creator)
  104. conf->nexttbtt = conf->intval;
  105. else
  106. conf->nexttbtt = ath9k_get_next_tbtt(ah, ath9k_hw_gettsf64(ah),
  107. conf->beacon_interval);
  108. if (conf->enable_beacon)
  109. ah->imask |= ATH9K_INT_SWBA;
  110. else
  111. ah->imask &= ~ATH9K_INT_SWBA;
  112. ath_dbg(common, BEACON,
  113. "IBSS (%s) nexttbtt: %u intval: %u conf_intval: %u\n",
  114. (conf->enable_beacon) ? "Enable" : "Disable",
  115. conf->nexttbtt, conf->intval, conf->beacon_interval);
  116. }
  117. EXPORT_SYMBOL(ath9k_cmn_beacon_config_adhoc);
  118. /*
  119. * For multi-bss ap support beacons are either staggered evenly over N slots or
  120. * burst together. For the former arrange for the SWBA to be delivered for each
  121. * slot. Slots that are not occupied will generate nothing.
  122. */
  123. void ath9k_cmn_beacon_config_ap(struct ath_hw *ah,
  124. struct ath_beacon_config *conf,
  125. unsigned int bc_buf)
  126. {
  127. struct ath_common *common = ath9k_hw_common(ah);
  128. /* NB: the beacon interval is kept internally in TU's */
  129. conf->intval = TU_TO_USEC(conf->beacon_interval);
  130. conf->intval /= bc_buf;
  131. conf->nexttbtt = ath9k_get_next_tbtt(ah, ath9k_hw_gettsf64(ah),
  132. conf->beacon_interval);
  133. if (conf->enable_beacon)
  134. ah->imask |= ATH9K_INT_SWBA;
  135. else
  136. ah->imask &= ~ATH9K_INT_SWBA;
  137. ath_dbg(common, BEACON,
  138. "AP (%s) nexttbtt: %u intval: %u conf_intval: %u\n",
  139. (conf->enable_beacon) ? "Enable" : "Disable",
  140. conf->nexttbtt, conf->intval, conf->beacon_interval);
  141. }
  142. EXPORT_SYMBOL(ath9k_cmn_beacon_config_ap);