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