common-beacon.c 5.3 KB

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