2 * Copyright (c) 2010 Atheros Communications Inc.
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.
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.
21 static void ath9k_htc_beacon_config_sta(struct ath9k_htc_priv *priv,
22 struct htc_beacon_config *bss_conf)
24 struct ath_common *common = ath9k_hw_common(priv->ah);
25 struct ath9k_beacon_state bs;
26 enum ath9k_int imask = 0;
27 int dtimperiod, dtimcount, sleepduration;
28 int cfpperiod, cfpcount, bmiss_timeout;
29 u32 nexttbtt = 0, intval, tsftu;
32 int num_beacons, offset, dtim_dec_count, cfp_dec_count;
36 memset(&bs, 0, sizeof(bs));
38 intval = bss_conf->beacon_interval & ATH9K_BEACON_PERIOD;
39 bmiss_timeout = (ATH_DEFAULT_BMISS_LIMIT * bss_conf->beacon_interval);
42 * Setup dtim and cfp parameters according to
43 * last beacon we received (which may be none).
45 dtimperiod = bss_conf->dtim_period;
46 if (dtimperiod <= 0) /* NB: 0 if not known */
49 if (dtimcount >= dtimperiod) /* NB: sanity check */
51 cfpperiod = 1; /* NB: no PCF support yet */
54 sleepduration = intval;
55 if (sleepduration <= 0)
56 sleepduration = intval;
59 * Pull nexttbtt forward to reflect the current
60 * TSF and calculate dtim+cfp state for the result.
62 tsf = ath9k_hw_gettsf64(priv->ah);
63 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
65 num_beacons = tsftu / intval + 1;
66 offset = tsftu % intval;
67 nexttbtt = tsftu - offset;
71 /* DTIM Beacon every dtimperiod Beacon */
72 dtim_dec_count = num_beacons % dtimperiod;
73 /* CFP every cfpperiod DTIM Beacon */
74 cfp_dec_count = (num_beacons / dtimperiod) % cfpperiod;
78 dtimcount -= dtim_dec_count;
80 dtimcount += dtimperiod;
82 cfpcount -= cfp_dec_count;
84 cfpcount += cfpperiod;
86 bs.bs_intval = intval;
87 bs.bs_nexttbtt = nexttbtt;
88 bs.bs_dtimperiod = dtimperiod*intval;
89 bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
90 bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
91 bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
92 bs.bs_cfpmaxduration = 0;
95 * Calculate the number of consecutive beacons to miss* before taking
96 * a BMISS interrupt. The configuration is specified in TU so we only
97 * need calculate based on the beacon interval. Note that we clamp the
98 * result to at most 15 beacons.
100 if (sleepduration > intval) {
101 bs.bs_bmissthreshold = ATH_DEFAULT_BMISS_LIMIT / 2;
103 bs.bs_bmissthreshold = DIV_ROUND_UP(bmiss_timeout, intval);
104 if (bs.bs_bmissthreshold > 15)
105 bs.bs_bmissthreshold = 15;
106 else if (bs.bs_bmissthreshold <= 0)
107 bs.bs_bmissthreshold = 1;
111 * Calculate sleep duration. The configuration is given in ms.
112 * We ensure a multiple of the beacon period is used. Also, if the sleep
113 * duration is greater than the DTIM period then it makes senses
114 * to make it a multiple of that.
119 bs.bs_sleepduration = roundup(IEEE80211_MS_TO_TU(100), sleepduration);
120 if (bs.bs_sleepduration > bs.bs_dtimperiod)
121 bs.bs_sleepduration = bs.bs_dtimperiod;
123 /* TSF out of range threshold fixed at 1 second */
124 bs.bs_tsfoor_threshold = ATH9K_TSFOOR_THRESHOLD;
126 ath_print(common, ATH_DBG_BEACON, "tsf: %llu tsftu: %u\n", tsf, tsftu);
127 ath_print(common, ATH_DBG_BEACON,
128 "bmiss: %u sleep: %u cfp-period: %u maxdur: %u next: %u\n",
129 bs.bs_bmissthreshold, bs.bs_sleepduration,
130 bs.bs_cfpperiod, bs.bs_cfpmaxduration, bs.bs_cfpnext);
132 /* Set the computed STA beacon timers */
134 WMI_CMD(WMI_DISABLE_INTR_CMDID);
135 ath9k_hw_set_sta_beacon_timers(priv->ah, &bs);
136 imask |= ATH9K_INT_BMISS;
137 htc_imask = cpu_to_be32(imask);
138 WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
141 static void ath9k_htc_beacon_config_adhoc(struct ath9k_htc_priv *priv,
142 struct htc_beacon_config *bss_conf)
144 struct ath_common *common = ath9k_hw_common(priv->ah);
145 enum ath9k_int imask = 0;
146 u32 nexttbtt, intval;
147 __be32 htc_imask = 0;
151 intval = bss_conf->beacon_interval & ATH9K_BEACON_PERIOD;
153 intval |= ATH9K_BEACON_ENA;
154 if (priv->op_flags & OP_ENABLE_BEACON)
155 imask |= ATH9K_INT_SWBA;
157 ath_print(common, ATH_DBG_BEACON,
158 "IBSS Beacon config, intval: %d, imask: 0x%x\n",
159 bss_conf->beacon_interval, imask);
161 WMI_CMD(WMI_DISABLE_INTR_CMDID);
162 ath9k_hw_beaconinit(priv->ah, nexttbtt, intval);
164 htc_imask = cpu_to_be32(imask);
165 WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
168 void ath9k_htc_beacon_update(struct ath9k_htc_priv *priv,
169 struct ieee80211_vif *vif)
171 struct ath_common *common = ath9k_hw_common(priv->ah);
173 spin_lock_bh(&priv->beacon_lock);
176 dev_kfree_skb_any(priv->beacon);
178 priv->beacon = ieee80211_beacon_get(priv->hw, vif);
180 ath_print(common, ATH_DBG_BEACON,
181 "Unable to allocate beacon\n");
183 spin_unlock_bh(&priv->beacon_lock);
186 void ath9k_htc_swba(struct ath9k_htc_priv *priv, u8 beacon_pending)
188 struct ath9k_htc_vif *avp = (void *)priv->vif->drv_priv;
189 struct tx_beacon_header beacon_hdr;
190 struct ath9k_htc_tx_ctl tx_ctl;
191 struct ieee80211_tx_info *info;
194 memset(&beacon_hdr, 0, sizeof(struct tx_beacon_header));
195 memset(&tx_ctl, 0, sizeof(struct ath9k_htc_tx_ctl));
197 /* FIXME: Handle BMISS */
198 if (beacon_pending != 0) {
203 spin_lock_bh(&priv->beacon_lock);
205 if (unlikely(priv->op_flags & OP_SCANNING)) {
206 spin_unlock_bh(&priv->beacon_lock);
210 if (unlikely(priv->beacon == NULL)) {
211 spin_unlock_bh(&priv->beacon_lock);
215 /* Free the old SKB first */
216 dev_kfree_skb_any(priv->beacon);
218 /* Get a new beacon */
219 priv->beacon = ieee80211_beacon_get(priv->hw, priv->vif);
221 spin_unlock_bh(&priv->beacon_lock);
225 info = IEEE80211_SKB_CB(priv->beacon);
226 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
227 struct ieee80211_hdr *hdr =
228 (struct ieee80211_hdr *) priv->beacon->data;
229 priv->seq_no += 0x10;
230 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
231 hdr->seq_ctrl |= cpu_to_le16(priv->seq_no);
234 tx_ctl.type = ATH9K_HTC_NORMAL;
235 beacon_hdr.vif_index = avp->index;
236 tx_fhdr = skb_push(priv->beacon, sizeof(beacon_hdr));
237 memcpy(tx_fhdr, (u8 *) &beacon_hdr, sizeof(beacon_hdr));
239 htc_send(priv->htc, priv->beacon, priv->beacon_ep, &tx_ctl);
241 spin_unlock_bh(&priv->beacon_lock);
245 void ath9k_htc_beacon_config(struct ath9k_htc_priv *priv,
246 struct ieee80211_vif *vif)
248 struct ath_common *common = ath9k_hw_common(priv->ah);
249 struct htc_beacon_config *cur_conf = &priv->cur_beacon_conf;
250 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
252 cur_conf->beacon_interval = bss_conf->beacon_int;
253 if (cur_conf->beacon_interval == 0)
254 cur_conf->beacon_interval = 100;
256 cur_conf->dtim_period = bss_conf->dtim_period;
257 cur_conf->listen_interval = 1;
258 cur_conf->dtim_count = 1;
259 cur_conf->bmiss_timeout =
260 ATH_DEFAULT_BMISS_LIMIT * cur_conf->beacon_interval;
263 case NL80211_IFTYPE_STATION:
264 ath9k_htc_beacon_config_sta(priv, cur_conf);
266 case NL80211_IFTYPE_ADHOC:
267 ath9k_htc_beacon_config_adhoc(priv, cur_conf);
270 ath_print(common, ATH_DBG_CONFIG,
271 "Unsupported beaconing mode\n");