]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/net/wireless/ath/ath9k/htc_drv_beacon.c
netfilter: xtables: combine built-in extension structs
[net-next-2.6.git] / drivers / net / wireless / ath / ath9k / htc_drv_beacon.c
1 /*
2  * Copyright (c) 2010 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 "htc.h"
18
19 #define FUDGE 2
20
21 static void ath9k_htc_beacon_config_sta(struct ath9k_htc_priv *priv,
22                                         struct htc_beacon_config *bss_conf)
23 {
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, htc_imask = 0;
30         u64 tsf;
31         int num_beacons, offset, dtim_dec_count, cfp_dec_count;
32         int ret;
33         u8 cmd_rsp;
34
35         memset(&bs, 0, sizeof(bs));
36
37         intval = bss_conf->beacon_interval & ATH9K_BEACON_PERIOD;
38         bmiss_timeout = (ATH_DEFAULT_BMISS_LIMIT * bss_conf->beacon_interval);
39
40         /*
41          * Setup dtim and cfp parameters according to
42          * last beacon we received (which may be none).
43          */
44         dtimperiod = bss_conf->dtim_period;
45         if (dtimperiod <= 0)            /* NB: 0 if not known */
46                 dtimperiod = 1;
47         dtimcount = 1;
48         if (dtimcount >= dtimperiod)    /* NB: sanity check */
49                 dtimcount = 0;
50         cfpperiod = 1;                  /* NB: no PCF support yet */
51         cfpcount = 0;
52
53         sleepduration = intval;
54         if (sleepduration <= 0)
55                 sleepduration = intval;
56
57         /*
58          * Pull nexttbtt forward to reflect the current
59          * TSF and calculate dtim+cfp state for the result.
60          */
61         tsf = ath9k_hw_gettsf64(priv->ah);
62         tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
63
64         num_beacons = tsftu / intval + 1;
65         offset = tsftu % intval;
66         nexttbtt = tsftu - offset;
67         if (offset)
68                 nexttbtt += intval;
69
70         /* DTIM Beacon every dtimperiod Beacon */
71         dtim_dec_count = num_beacons % dtimperiod;
72         /* CFP every cfpperiod DTIM Beacon */
73         cfp_dec_count = (num_beacons / dtimperiod) % cfpperiod;
74         if (dtim_dec_count)
75                 cfp_dec_count++;
76
77         dtimcount -= dtim_dec_count;
78         if (dtimcount < 0)
79                 dtimcount += dtimperiod;
80
81         cfpcount -= cfp_dec_count;
82         if (cfpcount < 0)
83                 cfpcount += cfpperiod;
84
85         bs.bs_intval = intval;
86         bs.bs_nexttbtt = nexttbtt;
87         bs.bs_dtimperiod = dtimperiod*intval;
88         bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
89         bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
90         bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
91         bs.bs_cfpmaxduration = 0;
92
93         /*
94          * Calculate the number of consecutive beacons to miss* before taking
95          * a BMISS interrupt. The configuration is specified in TU so we only
96          * need calculate based on the beacon interval.  Note that we clamp the
97          * result to at most 15 beacons.
98          */
99         if (sleepduration > intval) {
100                 bs.bs_bmissthreshold = ATH_DEFAULT_BMISS_LIMIT / 2;
101         } else {
102                 bs.bs_bmissthreshold = DIV_ROUND_UP(bmiss_timeout, intval);
103                 if (bs.bs_bmissthreshold > 15)
104                         bs.bs_bmissthreshold = 15;
105                 else if (bs.bs_bmissthreshold <= 0)
106                         bs.bs_bmissthreshold = 1;
107         }
108
109         /*
110          * Calculate sleep duration. The configuration is given in ms.
111          * We ensure a multiple of the beacon period is used. Also, if the sleep
112          * duration is greater than the DTIM period then it makes senses
113          * to make it a multiple of that.
114          *
115          * XXX fixed at 100ms
116          */
117
118         bs.bs_sleepduration = roundup(IEEE80211_MS_TO_TU(100), sleepduration);
119         if (bs.bs_sleepduration > bs.bs_dtimperiod)
120                 bs.bs_sleepduration = bs.bs_dtimperiod;
121
122         /* TSF out of range threshold fixed at 1 second */
123         bs.bs_tsfoor_threshold = ATH9K_TSFOOR_THRESHOLD;
124
125         ath_print(common, ATH_DBG_BEACON, "tsf: %llu tsftu: %u\n", tsf, tsftu);
126         ath_print(common, ATH_DBG_BEACON,
127                   "bmiss: %u sleep: %u cfp-period: %u maxdur: %u next: %u\n",
128                   bs.bs_bmissthreshold, bs.bs_sleepduration,
129                   bs.bs_cfpperiod, bs.bs_cfpmaxduration, bs.bs_cfpnext);
130
131         /* Set the computed STA beacon timers */
132
133         WMI_CMD(WMI_DISABLE_INTR_CMDID);
134         ath9k_hw_set_sta_beacon_timers(priv->ah, &bs);
135         imask |= ATH9K_INT_BMISS;
136         htc_imask = cpu_to_be32(imask);
137         WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
138 }
139
140 static void ath9k_htc_beacon_config_adhoc(struct ath9k_htc_priv *priv,
141                                           struct htc_beacon_config *bss_conf)
142 {
143         struct ath_common *common = ath9k_hw_common(priv->ah);
144         enum ath9k_int imask = 0;
145         u32 nexttbtt, intval, htc_imask = 0;
146         int ret;
147         u8 cmd_rsp;
148
149         intval = bss_conf->beacon_interval & ATH9K_BEACON_PERIOD;
150         nexttbtt = intval;
151         intval |= ATH9K_BEACON_ENA;
152         if (priv->op_flags & OP_ENABLE_BEACON)
153                 imask |= ATH9K_INT_SWBA;
154
155         ath_print(common, ATH_DBG_BEACON,
156                   "IBSS Beacon config, intval: %d, imask: 0x%x\n",
157                   bss_conf->beacon_interval, imask);
158
159         WMI_CMD(WMI_DISABLE_INTR_CMDID);
160         ath9k_hw_beaconinit(priv->ah, nexttbtt, intval);
161         priv->bmiss_cnt = 0;
162         htc_imask = cpu_to_be32(imask);
163         WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
164 }
165
166 void ath9k_htc_beacon_update(struct ath9k_htc_priv *priv,
167                              struct ieee80211_vif *vif)
168 {
169         struct ath_common *common = ath9k_hw_common(priv->ah);
170
171         spin_lock_bh(&priv->beacon_lock);
172
173         if (priv->beacon)
174                 dev_kfree_skb_any(priv->beacon);
175
176         priv->beacon = ieee80211_beacon_get(priv->hw, vif);
177         if (!priv->beacon)
178                 ath_print(common, ATH_DBG_BEACON,
179                           "Unable to allocate beacon\n");
180
181         spin_unlock_bh(&priv->beacon_lock);
182 }
183
184 void ath9k_htc_swba(struct ath9k_htc_priv *priv, u8 beacon_pending)
185 {
186         struct ath9k_htc_vif *avp = (void *)priv->vif->drv_priv;
187         struct tx_beacon_header beacon_hdr;
188         struct ath9k_htc_tx_ctl tx_ctl;
189         struct ieee80211_tx_info *info;
190         u8 *tx_fhdr;
191
192         memset(&beacon_hdr, 0, sizeof(struct tx_beacon_header));
193         memset(&tx_ctl, 0, sizeof(struct ath9k_htc_tx_ctl));
194
195         /* FIXME: Handle BMISS */
196         if (beacon_pending != 0) {
197                 priv->bmiss_cnt++;
198                 return;
199         }
200
201         spin_lock_bh(&priv->beacon_lock);
202
203         if (unlikely(priv->op_flags & OP_SCANNING)) {
204                 spin_unlock_bh(&priv->beacon_lock);
205                 return;
206         }
207
208         if (unlikely(priv->beacon == NULL)) {
209                 spin_unlock_bh(&priv->beacon_lock);
210                 return;
211         }
212
213         /* Free the old SKB first */
214         dev_kfree_skb_any(priv->beacon);
215
216         /* Get a new beacon */
217         priv->beacon = ieee80211_beacon_get(priv->hw, priv->vif);
218         if (!priv->beacon) {
219                 spin_unlock_bh(&priv->beacon_lock);
220                 return;
221         }
222
223         info = IEEE80211_SKB_CB(priv->beacon);
224         if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
225                 struct ieee80211_hdr *hdr =
226                         (struct ieee80211_hdr *) priv->beacon->data;
227                 priv->seq_no += 0x10;
228                 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
229                 hdr->seq_ctrl |= cpu_to_le16(priv->seq_no);
230         }
231
232         tx_ctl.type = ATH9K_HTC_NORMAL;
233         beacon_hdr.vif_index = avp->index;
234         tx_fhdr = skb_push(priv->beacon, sizeof(beacon_hdr));
235         memcpy(tx_fhdr, (u8 *) &beacon_hdr, sizeof(beacon_hdr));
236
237         htc_send(priv->htc, priv->beacon, priv->beacon_ep, &tx_ctl);
238
239         spin_unlock_bh(&priv->beacon_lock);
240 }
241
242
243 void ath9k_htc_beacon_config(struct ath9k_htc_priv *priv,
244                              struct ieee80211_vif *vif)
245 {
246         struct ath_common *common = ath9k_hw_common(priv->ah);
247         enum nl80211_iftype iftype;
248         struct htc_beacon_config *cur_conf = &priv->cur_beacon_conf;
249
250         if (vif) {
251                 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
252                 iftype = vif->type;
253                 cur_conf->beacon_interval = bss_conf->beacon_int;
254                 cur_conf->dtim_period = bss_conf->dtim_period;
255                 cur_conf->listen_interval = 1;
256                 cur_conf->dtim_count = 1;
257                 cur_conf->bmiss_timeout =
258                         ATH_DEFAULT_BMISS_LIMIT * cur_conf->beacon_interval;
259         } else
260                 iftype = priv->ah->opmode;
261
262         if (cur_conf->beacon_interval == 0)
263                 cur_conf->beacon_interval = 100;
264
265         switch (iftype) {
266         case NL80211_IFTYPE_STATION:
267                 ath9k_htc_beacon_config_sta(priv, cur_conf);
268                 break;
269         case NL80211_IFTYPE_ADHOC:
270                 ath9k_htc_beacon_config_adhoc(priv, cur_conf);
271                 break;
272         default:
273                 ath_print(common, ATH_DBG_CONFIG,
274                           "Unsupported beaconing mode\n");
275                 return;
276         }
277 }