ad3dd3186ad27b79315a0c7a517b5d327932890f
[pandora-kernel.git] / drivers / net / wireless / ath / ath9k / htc_drv_main.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 #ifdef CONFIG_ATH9K_HTC_DEBUGFS
20 static struct dentry *ath9k_debugfs_root;
21 #endif
22
23 /*************/
24 /* Utilities */
25 /*************/
26
27 void ath_update_txpow(struct ath9k_htc_priv *priv)
28 {
29         struct ath_hw *ah = priv->ah;
30
31         if (priv->curtxpow != priv->txpowlimit) {
32                 ath9k_hw_set_txpowerlimit(ah, priv->txpowlimit, false);
33                 /* read back in case value is clamped */
34                 priv->curtxpow = ath9k_hw_regulatory(ah)->power_limit;
35         }
36 }
37
38 /* HACK Alert: Use 11NG for 2.4, use 11NA for 5 */
39 static enum htc_phymode ath9k_htc_get_curmode(struct ath9k_htc_priv *priv,
40                                               struct ath9k_channel *ichan)
41 {
42         enum htc_phymode mode;
43
44         mode = HTC_MODE_AUTO;
45
46         switch (ichan->chanmode) {
47         case CHANNEL_G:
48         case CHANNEL_G_HT20:
49         case CHANNEL_G_HT40PLUS:
50         case CHANNEL_G_HT40MINUS:
51                 mode = HTC_MODE_11NG;
52                 break;
53         case CHANNEL_A:
54         case CHANNEL_A_HT20:
55         case CHANNEL_A_HT40PLUS:
56         case CHANNEL_A_HT40MINUS:
57                 mode = HTC_MODE_11NA;
58                 break;
59         default:
60                 break;
61         }
62
63         return mode;
64 }
65
66 bool ath9k_htc_setpower(struct ath9k_htc_priv *priv,
67                         enum ath9k_power_mode mode)
68 {
69         bool ret;
70
71         mutex_lock(&priv->htc_pm_lock);
72         ret = ath9k_hw_setpower(priv->ah, mode);
73         mutex_unlock(&priv->htc_pm_lock);
74
75         return ret;
76 }
77
78 void ath9k_htc_ps_wakeup(struct ath9k_htc_priv *priv)
79 {
80         mutex_lock(&priv->htc_pm_lock);
81         if (++priv->ps_usecount != 1)
82                 goto unlock;
83         ath9k_hw_setpower(priv->ah, ATH9K_PM_AWAKE);
84
85 unlock:
86         mutex_unlock(&priv->htc_pm_lock);
87 }
88
89 void ath9k_htc_ps_restore(struct ath9k_htc_priv *priv)
90 {
91         mutex_lock(&priv->htc_pm_lock);
92         if (--priv->ps_usecount != 0)
93                 goto unlock;
94
95         if (priv->ps_idle)
96                 ath9k_hw_setpower(priv->ah, ATH9K_PM_FULL_SLEEP);
97         else if (priv->ps_enabled)
98                 ath9k_hw_setpower(priv->ah, ATH9K_PM_NETWORK_SLEEP);
99
100 unlock:
101         mutex_unlock(&priv->htc_pm_lock);
102 }
103
104 void ath9k_ps_work(struct work_struct *work)
105 {
106         struct ath9k_htc_priv *priv =
107                 container_of(work, struct ath9k_htc_priv,
108                              ps_work);
109         ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
110
111         /* The chip wakes up after receiving the first beacon
112            while network sleep is enabled. For the driver to
113            be in sync with the hw, set the chip to awake and
114            only then set it to sleep.
115          */
116         ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
117 }
118
119 void ath9k_htc_reset(struct ath9k_htc_priv *priv)
120 {
121         struct ath_hw *ah = priv->ah;
122         struct ath_common *common = ath9k_hw_common(ah);
123         struct ieee80211_channel *channel = priv->hw->conf.channel;
124         struct ath9k_hw_cal_data *caldata;
125         enum htc_phymode mode;
126         __be16 htc_mode;
127         u8 cmd_rsp;
128         int ret;
129
130         mutex_lock(&priv->mutex);
131         ath9k_htc_ps_wakeup(priv);
132
133         if (priv->op_flags & OP_ASSOCIATED)
134                 cancel_delayed_work_sync(&priv->ath9k_ani_work);
135
136         ieee80211_stop_queues(priv->hw);
137         htc_stop(priv->htc);
138         WMI_CMD(WMI_DISABLE_INTR_CMDID);
139         WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
140         WMI_CMD(WMI_STOP_RECV_CMDID);
141
142         caldata = &priv->caldata[channel->hw_value];
143         ret = ath9k_hw_reset(ah, ah->curchan, caldata, false);
144         if (ret) {
145                 ath_err(common,
146                         "Unable to reset device (%u Mhz) reset status %d\n",
147                         channel->center_freq, ret);
148         }
149
150         ath_update_txpow(priv);
151
152         WMI_CMD(WMI_START_RECV_CMDID);
153         ath9k_host_rx_init(priv);
154
155         mode = ath9k_htc_get_curmode(priv, ah->curchan);
156         htc_mode = cpu_to_be16(mode);
157         WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
158
159         WMI_CMD(WMI_ENABLE_INTR_CMDID);
160         htc_start(priv->htc);
161
162         if (priv->op_flags & OP_ASSOCIATED) {
163                 ath9k_htc_beacon_config(priv, priv->vif);
164                 ath_start_ani(priv);
165         }
166
167         ieee80211_wake_queues(priv->hw);
168
169         ath9k_htc_ps_restore(priv);
170         mutex_unlock(&priv->mutex);
171 }
172
173 static int ath9k_htc_set_channel(struct ath9k_htc_priv *priv,
174                                  struct ieee80211_hw *hw,
175                                  struct ath9k_channel *hchan)
176 {
177         struct ath_hw *ah = priv->ah;
178         struct ath_common *common = ath9k_hw_common(ah);
179         struct ieee80211_conf *conf = &common->hw->conf;
180         bool fastcc;
181         struct ieee80211_channel *channel = hw->conf.channel;
182         struct ath9k_hw_cal_data *caldata;
183         enum htc_phymode mode;
184         __be16 htc_mode;
185         u8 cmd_rsp;
186         int ret;
187
188         if (priv->op_flags & OP_INVALID)
189                 return -EIO;
190
191         fastcc = !!(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL);
192
193         ath9k_htc_ps_wakeup(priv);
194         htc_stop(priv->htc);
195         WMI_CMD(WMI_DISABLE_INTR_CMDID);
196         WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
197         WMI_CMD(WMI_STOP_RECV_CMDID);
198
199         ath_dbg(common, ATH_DBG_CONFIG,
200                 "(%u MHz) -> (%u MHz), HT: %d, HT40: %d fastcc: %d\n",
201                 priv->ah->curchan->channel,
202                 channel->center_freq, conf_is_ht(conf), conf_is_ht40(conf),
203                 fastcc);
204
205         caldata = &priv->caldata[channel->hw_value];
206         ret = ath9k_hw_reset(ah, hchan, caldata, fastcc);
207         if (ret) {
208                 ath_err(common,
209                         "Unable to reset channel (%u Mhz) reset status %d\n",
210                         channel->center_freq, ret);
211                 goto err;
212         }
213
214         ath_update_txpow(priv);
215
216         WMI_CMD(WMI_START_RECV_CMDID);
217         if (ret)
218                 goto err;
219
220         ath9k_host_rx_init(priv);
221
222         mode = ath9k_htc_get_curmode(priv, hchan);
223         htc_mode = cpu_to_be16(mode);
224         WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
225         if (ret)
226                 goto err;
227
228         WMI_CMD(WMI_ENABLE_INTR_CMDID);
229         if (ret)
230                 goto err;
231
232         htc_start(priv->htc);
233 err:
234         ath9k_htc_ps_restore(priv);
235         return ret;
236 }
237
238 static int ath9k_htc_add_monitor_interface(struct ath9k_htc_priv *priv)
239 {
240         struct ath_common *common = ath9k_hw_common(priv->ah);
241         struct ath9k_htc_target_vif hvif;
242         int ret = 0;
243         u8 cmd_rsp;
244
245         if (priv->nvifs > 0)
246                 return -ENOBUFS;
247
248         memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
249         memcpy(&hvif.myaddr, common->macaddr, ETH_ALEN);
250
251         hvif.opmode = cpu_to_be32(HTC_M_MONITOR);
252         priv->ah->opmode = NL80211_IFTYPE_MONITOR;
253         hvif.index = priv->nvifs;
254
255         WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
256         if (ret)
257                 return ret;
258
259         priv->nvifs++;
260         return 0;
261 }
262
263 static int ath9k_htc_remove_monitor_interface(struct ath9k_htc_priv *priv)
264 {
265         struct ath_common *common = ath9k_hw_common(priv->ah);
266         struct ath9k_htc_target_vif hvif;
267         int ret = 0;
268         u8 cmd_rsp;
269
270         memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
271         memcpy(&hvif.myaddr, common->macaddr, ETH_ALEN);
272         hvif.index = 0; /* Should do for now */
273         WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
274         priv->nvifs--;
275
276         return ret;
277 }
278
279 static int ath9k_htc_add_station(struct ath9k_htc_priv *priv,
280                                  struct ieee80211_vif *vif,
281                                  struct ieee80211_sta *sta)
282 {
283         struct ath_common *common = ath9k_hw_common(priv->ah);
284         struct ath9k_htc_target_sta tsta;
285         struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
286         struct ath9k_htc_sta *ista;
287         int ret;
288         u8 cmd_rsp;
289
290         if (priv->nstations >= ATH9K_HTC_MAX_STA)
291                 return -ENOBUFS;
292
293         memset(&tsta, 0, sizeof(struct ath9k_htc_target_sta));
294
295         if (sta) {
296                 ista = (struct ath9k_htc_sta *) sta->drv_priv;
297                 memcpy(&tsta.macaddr, sta->addr, ETH_ALEN);
298                 memcpy(&tsta.bssid, common->curbssid, ETH_ALEN);
299                 tsta.associd = common->curaid;
300                 tsta.is_vif_sta = 0;
301                 tsta.valid = true;
302                 ista->index = priv->nstations;
303         } else {
304                 memcpy(&tsta.macaddr, vif->addr, ETH_ALEN);
305                 tsta.is_vif_sta = 1;
306         }
307
308         tsta.sta_index = priv->nstations;
309         tsta.vif_index = avp->index;
310         tsta.maxampdu = 0xffff;
311         if (sta && sta->ht_cap.ht_supported)
312                 tsta.flags = cpu_to_be16(ATH_HTC_STA_HT);
313
314         WMI_CMD_BUF(WMI_NODE_CREATE_CMDID, &tsta);
315         if (ret) {
316                 if (sta)
317                         ath_err(common,
318                                 "Unable to add station entry for: %pM\n",
319                                 sta->addr);
320                 return ret;
321         }
322
323         if (sta)
324                 ath_dbg(common, ATH_DBG_CONFIG,
325                         "Added a station entry for: %pM (idx: %d)\n",
326                         sta->addr, tsta.sta_index);
327
328         priv->nstations++;
329         return 0;
330 }
331
332 static int ath9k_htc_remove_station(struct ath9k_htc_priv *priv,
333                                     struct ieee80211_vif *vif,
334                                     struct ieee80211_sta *sta)
335 {
336         struct ath_common *common = ath9k_hw_common(priv->ah);
337         struct ath9k_htc_sta *ista;
338         int ret;
339         u8 cmd_rsp, sta_idx;
340
341         if (sta) {
342                 ista = (struct ath9k_htc_sta *) sta->drv_priv;
343                 sta_idx = ista->index;
344         } else {
345                 sta_idx = 0;
346         }
347
348         WMI_CMD_BUF(WMI_NODE_REMOVE_CMDID, &sta_idx);
349         if (ret) {
350                 if (sta)
351                         ath_err(common,
352                                 "Unable to remove station entry for: %pM\n",
353                                 sta->addr);
354                 return ret;
355         }
356
357         if (sta)
358                 ath_dbg(common, ATH_DBG_CONFIG,
359                         "Removed a station entry for: %pM (idx: %d)\n",
360                         sta->addr, sta_idx);
361
362         priv->nstations--;
363         return 0;
364 }
365
366 static int ath9k_htc_update_cap_target(struct ath9k_htc_priv *priv)
367 {
368         struct ath9k_htc_cap_target tcap;
369         int ret;
370         u8 cmd_rsp;
371
372         memset(&tcap, 0, sizeof(struct ath9k_htc_cap_target));
373
374         /* FIXME: Values are hardcoded */
375         tcap.flags = 0x240c40;
376         tcap.flags_ext = 0x80601000;
377         tcap.ampdu_limit = 0xffff0000;
378         tcap.ampdu_subframes = 20;
379         tcap.tx_chainmask_legacy = priv->ah->caps.tx_chainmask;
380         tcap.protmode = 1;
381         tcap.tx_chainmask = priv->ah->caps.tx_chainmask;
382
383         WMI_CMD_BUF(WMI_TARGET_IC_UPDATE_CMDID, &tcap);
384
385         return ret;
386 }
387
388 static void ath9k_htc_setup_rate(struct ath9k_htc_priv *priv,
389                                  struct ieee80211_sta *sta,
390                                  struct ath9k_htc_target_rate *trate)
391 {
392         struct ath9k_htc_sta *ista = (struct ath9k_htc_sta *) sta->drv_priv;
393         struct ieee80211_supported_band *sband;
394         u32 caps = 0;
395         int i, j;
396
397         sband = priv->hw->wiphy->bands[priv->hw->conf.channel->band];
398
399         for (i = 0, j = 0; i < sband->n_bitrates; i++) {
400                 if (sta->supp_rates[sband->band] & BIT(i)) {
401                         trate->rates.legacy_rates.rs_rates[j]
402                                 = (sband->bitrates[i].bitrate * 2) / 10;
403                         j++;
404                 }
405         }
406         trate->rates.legacy_rates.rs_nrates = j;
407
408         if (sta->ht_cap.ht_supported) {
409                 for (i = 0, j = 0; i < 77; i++) {
410                         if (sta->ht_cap.mcs.rx_mask[i/8] & (1<<(i%8)))
411                                 trate->rates.ht_rates.rs_rates[j++] = i;
412                         if (j == ATH_HTC_RATE_MAX)
413                                 break;
414                 }
415                 trate->rates.ht_rates.rs_nrates = j;
416
417                 caps = WLAN_RC_HT_FLAG;
418                 if (sta->ht_cap.mcs.rx_mask[1])
419                         caps |= WLAN_RC_DS_FLAG;
420                 if ((sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
421                      (conf_is_ht40(&priv->hw->conf)))
422                         caps |= WLAN_RC_40_FLAG;
423                 if (conf_is_ht40(&priv->hw->conf) &&
424                     (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40))
425                         caps |= WLAN_RC_SGI_FLAG;
426                 else if (conf_is_ht20(&priv->hw->conf) &&
427                          (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20))
428                         caps |= WLAN_RC_SGI_FLAG;
429         }
430
431         trate->sta_index = ista->index;
432         trate->isnew = 1;
433         trate->capflags = cpu_to_be32(caps);
434 }
435
436 static int ath9k_htc_send_rate_cmd(struct ath9k_htc_priv *priv,
437                                     struct ath9k_htc_target_rate *trate)
438 {
439         struct ath_common *common = ath9k_hw_common(priv->ah);
440         int ret;
441         u8 cmd_rsp;
442
443         WMI_CMD_BUF(WMI_RC_RATE_UPDATE_CMDID, trate);
444         if (ret) {
445                 ath_err(common,
446                         "Unable to initialize Rate information on target\n");
447         }
448
449         return ret;
450 }
451
452 static void ath9k_htc_init_rate(struct ath9k_htc_priv *priv,
453                                 struct ieee80211_sta *sta)
454 {
455         struct ath_common *common = ath9k_hw_common(priv->ah);
456         struct ath9k_htc_target_rate trate;
457         int ret;
458
459         memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
460         ath9k_htc_setup_rate(priv, sta, &trate);
461         ret = ath9k_htc_send_rate_cmd(priv, &trate);
462         if (!ret)
463                 ath_dbg(common, ATH_DBG_CONFIG,
464                         "Updated target sta: %pM, rate caps: 0x%X\n",
465                         sta->addr, be32_to_cpu(trate.capflags));
466 }
467
468 static void ath9k_htc_update_rate(struct ath9k_htc_priv *priv,
469                                   struct ieee80211_vif *vif,
470                                   struct ieee80211_bss_conf *bss_conf)
471 {
472         struct ath_common *common = ath9k_hw_common(priv->ah);
473         struct ath9k_htc_target_rate trate;
474         struct ieee80211_sta *sta;
475         int ret;
476
477         memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
478
479         rcu_read_lock();
480         sta = ieee80211_find_sta(vif, bss_conf->bssid);
481         if (!sta) {
482                 rcu_read_unlock();
483                 return;
484         }
485         ath9k_htc_setup_rate(priv, sta, &trate);
486         rcu_read_unlock();
487
488         ret = ath9k_htc_send_rate_cmd(priv, &trate);
489         if (!ret)
490                 ath_dbg(common, ATH_DBG_CONFIG,
491                         "Updated target sta: %pM, rate caps: 0x%X\n",
492                         bss_conf->bssid, be32_to_cpu(trate.capflags));
493 }
494
495 static int ath9k_htc_tx_aggr_oper(struct ath9k_htc_priv *priv,
496                                   struct ieee80211_vif *vif,
497                                   struct ieee80211_sta *sta,
498                                   enum ieee80211_ampdu_mlme_action action,
499                                   u16 tid)
500 {
501         struct ath_common *common = ath9k_hw_common(priv->ah);
502         struct ath9k_htc_target_aggr aggr;
503         struct ath9k_htc_sta *ista;
504         int ret = 0;
505         u8 cmd_rsp;
506
507         if (tid >= ATH9K_HTC_MAX_TID)
508                 return -EINVAL;
509
510         memset(&aggr, 0, sizeof(struct ath9k_htc_target_aggr));
511         ista = (struct ath9k_htc_sta *) sta->drv_priv;
512
513         aggr.sta_index = ista->index;
514         aggr.tidno = tid & 0xf;
515         aggr.aggr_enable = (action == IEEE80211_AMPDU_TX_START) ? true : false;
516
517         WMI_CMD_BUF(WMI_TX_AGGR_ENABLE_CMDID, &aggr);
518         if (ret)
519                 ath_dbg(common, ATH_DBG_CONFIG,
520                         "Unable to %s TX aggregation for (%pM, %d)\n",
521                         (aggr.aggr_enable) ? "start" : "stop", sta->addr, tid);
522         else
523                 ath_dbg(common, ATH_DBG_CONFIG,
524                         "%s TX aggregation for (%pM, %d)\n",
525                         (aggr.aggr_enable) ? "Starting" : "Stopping",
526                         sta->addr, tid);
527
528         spin_lock_bh(&priv->tx_lock);
529         ista->tid_state[tid] = (aggr.aggr_enable && !ret) ? AGGR_START : AGGR_STOP;
530         spin_unlock_bh(&priv->tx_lock);
531
532         return ret;
533 }
534
535 /*********/
536 /* DEBUG */
537 /*********/
538
539 #ifdef CONFIG_ATH9K_HTC_DEBUGFS
540
541 static int ath9k_debugfs_open(struct inode *inode, struct file *file)
542 {
543         file->private_data = inode->i_private;
544         return 0;
545 }
546
547 static ssize_t read_file_tgt_stats(struct file *file, char __user *user_buf,
548                                    size_t count, loff_t *ppos)
549 {
550         struct ath9k_htc_priv *priv = file->private_data;
551         struct ath9k_htc_target_stats cmd_rsp;
552         char buf[512];
553         unsigned int len = 0;
554         int ret = 0;
555
556         memset(&cmd_rsp, 0, sizeof(cmd_rsp));
557
558         WMI_CMD(WMI_TGT_STATS_CMDID);
559         if (ret)
560                 return -EINVAL;
561
562
563         len += snprintf(buf + len, sizeof(buf) - len,
564                         "%19s : %10u\n", "TX Short Retries",
565                         be32_to_cpu(cmd_rsp.tx_shortretry));
566         len += snprintf(buf + len, sizeof(buf) - len,
567                         "%19s : %10u\n", "TX Long Retries",
568                         be32_to_cpu(cmd_rsp.tx_longretry));
569         len += snprintf(buf + len, sizeof(buf) - len,
570                         "%19s : %10u\n", "TX Xretries",
571                         be32_to_cpu(cmd_rsp.tx_xretries));
572         len += snprintf(buf + len, sizeof(buf) - len,
573                         "%19s : %10u\n", "TX Unaggr. Xretries",
574                         be32_to_cpu(cmd_rsp.ht_txunaggr_xretry));
575         len += snprintf(buf + len, sizeof(buf) - len,
576                         "%19s : %10u\n", "TX Xretries (HT)",
577                         be32_to_cpu(cmd_rsp.ht_tx_xretries));
578         len += snprintf(buf + len, sizeof(buf) - len,
579                         "%19s : %10u\n", "TX Rate", priv->debug.txrate);
580
581         if (len > sizeof(buf))
582                 len = sizeof(buf);
583
584         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
585 }
586
587 static const struct file_operations fops_tgt_stats = {
588         .read = read_file_tgt_stats,
589         .open = ath9k_debugfs_open,
590         .owner = THIS_MODULE,
591         .llseek = default_llseek,
592 };
593
594 static ssize_t read_file_xmit(struct file *file, char __user *user_buf,
595                               size_t count, loff_t *ppos)
596 {
597         struct ath9k_htc_priv *priv = file->private_data;
598         char buf[512];
599         unsigned int len = 0;
600
601         len += snprintf(buf + len, sizeof(buf) - len,
602                         "%20s : %10u\n", "Buffers queued",
603                         priv->debug.tx_stats.buf_queued);
604         len += snprintf(buf + len, sizeof(buf) - len,
605                         "%20s : %10u\n", "Buffers completed",
606                         priv->debug.tx_stats.buf_completed);
607         len += snprintf(buf + len, sizeof(buf) - len,
608                         "%20s : %10u\n", "SKBs queued",
609                         priv->debug.tx_stats.skb_queued);
610         len += snprintf(buf + len, sizeof(buf) - len,
611                         "%20s : %10u\n", "SKBs completed",
612                         priv->debug.tx_stats.skb_completed);
613         len += snprintf(buf + len, sizeof(buf) - len,
614                         "%20s : %10u\n", "SKBs dropped",
615                         priv->debug.tx_stats.skb_dropped);
616
617         len += snprintf(buf + len, sizeof(buf) - len,
618                         "%20s : %10u\n", "BE queued",
619                         priv->debug.tx_stats.queue_stats[WME_AC_BE]);
620         len += snprintf(buf + len, sizeof(buf) - len,
621                         "%20s : %10u\n", "BK queued",
622                         priv->debug.tx_stats.queue_stats[WME_AC_BK]);
623         len += snprintf(buf + len, sizeof(buf) - len,
624                         "%20s : %10u\n", "VI queued",
625                         priv->debug.tx_stats.queue_stats[WME_AC_VI]);
626         len += snprintf(buf + len, sizeof(buf) - len,
627                         "%20s : %10u\n", "VO queued",
628                         priv->debug.tx_stats.queue_stats[WME_AC_VO]);
629
630         if (len > sizeof(buf))
631                 len = sizeof(buf);
632
633         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
634 }
635
636 static const struct file_operations fops_xmit = {
637         .read = read_file_xmit,
638         .open = ath9k_debugfs_open,
639         .owner = THIS_MODULE,
640         .llseek = default_llseek,
641 };
642
643 static ssize_t read_file_recv(struct file *file, char __user *user_buf,
644                               size_t count, loff_t *ppos)
645 {
646         struct ath9k_htc_priv *priv = file->private_data;
647         char buf[512];
648         unsigned int len = 0;
649
650         len += snprintf(buf + len, sizeof(buf) - len,
651                         "%20s : %10u\n", "SKBs allocated",
652                         priv->debug.rx_stats.skb_allocated);
653         len += snprintf(buf + len, sizeof(buf) - len,
654                         "%20s : %10u\n", "SKBs completed",
655                         priv->debug.rx_stats.skb_completed);
656         len += snprintf(buf + len, sizeof(buf) - len,
657                         "%20s : %10u\n", "SKBs Dropped",
658                         priv->debug.rx_stats.skb_dropped);
659
660         if (len > sizeof(buf))
661                 len = sizeof(buf);
662
663         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
664 }
665
666 static const struct file_operations fops_recv = {
667         .read = read_file_recv,
668         .open = ath9k_debugfs_open,
669         .owner = THIS_MODULE,
670         .llseek = default_llseek,
671 };
672
673 int ath9k_htc_init_debug(struct ath_hw *ah)
674 {
675         struct ath_common *common = ath9k_hw_common(ah);
676         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
677
678         if (!ath9k_debugfs_root)
679                 return -ENOENT;
680
681         priv->debug.debugfs_phy = debugfs_create_dir(wiphy_name(priv->hw->wiphy),
682                                                      ath9k_debugfs_root);
683         if (!priv->debug.debugfs_phy)
684                 goto err;
685
686         priv->debug.debugfs_tgt_stats = debugfs_create_file("tgt_stats", S_IRUSR,
687                                                     priv->debug.debugfs_phy,
688                                                     priv, &fops_tgt_stats);
689         if (!priv->debug.debugfs_tgt_stats)
690                 goto err;
691
692
693         priv->debug.debugfs_xmit = debugfs_create_file("xmit", S_IRUSR,
694                                                        priv->debug.debugfs_phy,
695                                                        priv, &fops_xmit);
696         if (!priv->debug.debugfs_xmit)
697                 goto err;
698
699         priv->debug.debugfs_recv = debugfs_create_file("recv", S_IRUSR,
700                                                        priv->debug.debugfs_phy,
701                                                        priv, &fops_recv);
702         if (!priv->debug.debugfs_recv)
703                 goto err;
704
705         return 0;
706
707 err:
708         ath9k_htc_exit_debug(ah);
709         return -ENOMEM;
710 }
711
712 void ath9k_htc_exit_debug(struct ath_hw *ah)
713 {
714         struct ath_common *common = ath9k_hw_common(ah);
715         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
716
717         debugfs_remove(priv->debug.debugfs_recv);
718         debugfs_remove(priv->debug.debugfs_xmit);
719         debugfs_remove(priv->debug.debugfs_tgt_stats);
720         debugfs_remove(priv->debug.debugfs_phy);
721 }
722
723 int ath9k_htc_debug_create_root(void)
724 {
725         ath9k_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
726         if (!ath9k_debugfs_root)
727                 return -ENOENT;
728
729         return 0;
730 }
731
732 void ath9k_htc_debug_remove_root(void)
733 {
734         debugfs_remove(ath9k_debugfs_root);
735         ath9k_debugfs_root = NULL;
736 }
737
738 #endif /* CONFIG_ATH9K_HTC_DEBUGFS */
739
740 /*******/
741 /* ANI */
742 /*******/
743
744 void ath_start_ani(struct ath9k_htc_priv *priv)
745 {
746         struct ath_common *common = ath9k_hw_common(priv->ah);
747         unsigned long timestamp = jiffies_to_msecs(jiffies);
748
749         common->ani.longcal_timer = timestamp;
750         common->ani.shortcal_timer = timestamp;
751         common->ani.checkani_timer = timestamp;
752
753         ieee80211_queue_delayed_work(common->hw, &priv->ath9k_ani_work,
754                                      msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
755 }
756
757 void ath9k_ani_work(struct work_struct *work)
758 {
759         struct ath9k_htc_priv *priv =
760                 container_of(work, struct ath9k_htc_priv,
761                              ath9k_ani_work.work);
762         struct ath_hw *ah = priv->ah;
763         struct ath_common *common = ath9k_hw_common(ah);
764         bool longcal = false;
765         bool shortcal = false;
766         bool aniflag = false;
767         unsigned int timestamp = jiffies_to_msecs(jiffies);
768         u32 cal_interval, short_cal_interval;
769
770         short_cal_interval = ATH_STA_SHORT_CALINTERVAL;
771
772         /* Only calibrate if awake */
773         if (ah->power_mode != ATH9K_PM_AWAKE)
774                 goto set_timer;
775
776         /* Long calibration runs independently of short calibration. */
777         if ((timestamp - common->ani.longcal_timer) >= ATH_LONG_CALINTERVAL) {
778                 longcal = true;
779                 ath_dbg(common, ATH_DBG_ANI, "longcal @%lu\n", jiffies);
780                 common->ani.longcal_timer = timestamp;
781         }
782
783         /* Short calibration applies only while caldone is false */
784         if (!common->ani.caldone) {
785                 if ((timestamp - common->ani.shortcal_timer) >=
786                     short_cal_interval) {
787                         shortcal = true;
788                         ath_dbg(common, ATH_DBG_ANI,
789                                 "shortcal @%lu\n", jiffies);
790                         common->ani.shortcal_timer = timestamp;
791                         common->ani.resetcal_timer = timestamp;
792                 }
793         } else {
794                 if ((timestamp - common->ani.resetcal_timer) >=
795                     ATH_RESTART_CALINTERVAL) {
796                         common->ani.caldone = ath9k_hw_reset_calvalid(ah);
797                         if (common->ani.caldone)
798                                 common->ani.resetcal_timer = timestamp;
799                 }
800         }
801
802         /* Verify whether we must check ANI */
803         if ((timestamp - common->ani.checkani_timer) >= ATH_ANI_POLLINTERVAL) {
804                 aniflag = true;
805                 common->ani.checkani_timer = timestamp;
806         }
807
808         /* Skip all processing if there's nothing to do. */
809         if (longcal || shortcal || aniflag) {
810
811                 ath9k_htc_ps_wakeup(priv);
812
813                 /* Call ANI routine if necessary */
814                 if (aniflag)
815                         ath9k_hw_ani_monitor(ah, ah->curchan);
816
817                 /* Perform calibration if necessary */
818                 if (longcal || shortcal)
819                         common->ani.caldone =
820                                 ath9k_hw_calibrate(ah, ah->curchan,
821                                                    common->rx_chainmask,
822                                                    longcal);
823
824                 ath9k_htc_ps_restore(priv);
825         }
826
827 set_timer:
828         /*
829         * Set timer interval based on previous results.
830         * The interval must be the shortest necessary to satisfy ANI,
831         * short calibration and long calibration.
832         */
833         cal_interval = ATH_LONG_CALINTERVAL;
834         if (priv->ah->config.enable_ani)
835                 cal_interval = min(cal_interval, (u32)ATH_ANI_POLLINTERVAL);
836         if (!common->ani.caldone)
837                 cal_interval = min(cal_interval, (u32)short_cal_interval);
838
839         ieee80211_queue_delayed_work(common->hw, &priv->ath9k_ani_work,
840                                      msecs_to_jiffies(cal_interval));
841 }
842
843 /**********************/
844 /* mac80211 Callbacks */
845 /**********************/
846
847 static int ath9k_htc_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
848 {
849         struct ieee80211_hdr *hdr;
850         struct ath9k_htc_priv *priv = hw->priv;
851         int padpos, padsize, ret;
852
853         hdr = (struct ieee80211_hdr *) skb->data;
854
855         /* Add the padding after the header if this is not already done */
856         padpos = ath9k_cmn_padpos(hdr->frame_control);
857         padsize = padpos & 3;
858         if (padsize && skb->len > padpos) {
859                 if (skb_headroom(skb) < padsize)
860                         return -1;
861                 skb_push(skb, padsize);
862                 memmove(skb->data, skb->data + padsize, padpos);
863         }
864
865         ret = ath9k_htc_tx_start(priv, skb);
866         if (ret != 0) {
867                 if (ret == -ENOMEM) {
868                         ath_dbg(ath9k_hw_common(priv->ah), ATH_DBG_XMIT,
869                                 "Stopping TX queues\n");
870                         ieee80211_stop_queues(hw);
871                         spin_lock_bh(&priv->tx_lock);
872                         priv->tx_queues_stop = true;
873                         spin_unlock_bh(&priv->tx_lock);
874                 } else {
875                         ath_dbg(ath9k_hw_common(priv->ah), ATH_DBG_XMIT,
876                                 "Tx failed\n");
877                 }
878                 goto fail_tx;
879         }
880
881         return 0;
882
883 fail_tx:
884         dev_kfree_skb_any(skb);
885         return 0;
886 }
887
888 static int ath9k_htc_start(struct ieee80211_hw *hw)
889 {
890         struct ath9k_htc_priv *priv = hw->priv;
891         struct ath_hw *ah = priv->ah;
892         struct ath_common *common = ath9k_hw_common(ah);
893         struct ieee80211_channel *curchan = hw->conf.channel;
894         struct ath9k_channel *init_channel;
895         int ret = 0;
896         enum htc_phymode mode;
897         __be16 htc_mode;
898         u8 cmd_rsp;
899
900         mutex_lock(&priv->mutex);
901
902         ath_dbg(common, ATH_DBG_CONFIG,
903                 "Starting driver with initial channel: %d MHz\n",
904                 curchan->center_freq);
905
906         /* Ensure that HW is awake before flushing RX */
907         ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
908         WMI_CMD(WMI_FLUSH_RECV_CMDID);
909
910         /* setup initial channel */
911         init_channel = ath9k_cmn_get_curchannel(hw, ah);
912
913         ath9k_hw_htc_resetinit(ah);
914         ret = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
915         if (ret) {
916                 ath_err(common,
917                         "Unable to reset hardware; reset status %d (freq %u MHz)\n",
918                         ret, curchan->center_freq);
919                 mutex_unlock(&priv->mutex);
920                 return ret;
921         }
922
923         ath_update_txpow(priv);
924
925         mode = ath9k_htc_get_curmode(priv, init_channel);
926         htc_mode = cpu_to_be16(mode);
927         WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
928         WMI_CMD(WMI_ATH_INIT_CMDID);
929         WMI_CMD(WMI_START_RECV_CMDID);
930
931         ath9k_host_rx_init(priv);
932
933         priv->op_flags &= ~OP_INVALID;
934         htc_start(priv->htc);
935
936         spin_lock_bh(&priv->tx_lock);
937         priv->tx_queues_stop = false;
938         spin_unlock_bh(&priv->tx_lock);
939
940         ieee80211_wake_queues(hw);
941
942         if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE) {
943                 ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
944                                            AR_STOMP_LOW_WLAN_WGHT);
945                 ath9k_hw_btcoex_enable(ah);
946                 ath_htc_resume_btcoex_work(priv);
947         }
948         mutex_unlock(&priv->mutex);
949
950         return ret;
951 }
952
953 static void ath9k_htc_stop(struct ieee80211_hw *hw)
954 {
955         struct ath9k_htc_priv *priv = hw->priv;
956         struct ath_hw *ah = priv->ah;
957         struct ath_common *common = ath9k_hw_common(ah);
958         int ret = 0;
959         u8 cmd_rsp;
960
961         /* Cancel all the running timers/work .. */
962         cancel_work_sync(&priv->fatal_work);
963         cancel_work_sync(&priv->ps_work);
964         cancel_delayed_work_sync(&priv->ath9k_led_blink_work);
965         ath9k_led_stop_brightness(priv);
966
967         mutex_lock(&priv->mutex);
968
969         if (priv->op_flags & OP_INVALID) {
970                 ath_dbg(common, ATH_DBG_ANY, "Device not present\n");
971                 mutex_unlock(&priv->mutex);
972                 return;
973         }
974
975         ath9k_htc_ps_wakeup(priv);
976         htc_stop(priv->htc);
977         WMI_CMD(WMI_DISABLE_INTR_CMDID);
978         WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
979         WMI_CMD(WMI_STOP_RECV_CMDID);
980         skb_queue_purge(&priv->tx_queue);
981
982         /* Remove monitor interface here */
983         if (ah->opmode == NL80211_IFTYPE_MONITOR) {
984                 if (ath9k_htc_remove_monitor_interface(priv))
985                         ath_err(common, "Unable to remove monitor interface\n");
986                 else
987                         ath_dbg(common, ATH_DBG_CONFIG,
988                                 "Monitor interface removed\n");
989         }
990
991         if (ah->btcoex_hw.enabled) {
992                 ath9k_hw_btcoex_disable(ah);
993                 if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
994                         ath_htc_cancel_btcoex_work(priv);
995         }
996
997         ath9k_hw_phy_disable(ah);
998         ath9k_hw_disable(ah);
999         ath9k_htc_ps_restore(priv);
1000         ath9k_htc_setpower(priv, ATH9K_PM_FULL_SLEEP);
1001
1002         priv->op_flags |= OP_INVALID;
1003
1004         ath_dbg(common, ATH_DBG_CONFIG, "Driver halt\n");
1005         mutex_unlock(&priv->mutex);
1006 }
1007
1008 static int ath9k_htc_add_interface(struct ieee80211_hw *hw,
1009                                    struct ieee80211_vif *vif)
1010 {
1011         struct ath9k_htc_priv *priv = hw->priv;
1012         struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1013         struct ath_common *common = ath9k_hw_common(priv->ah);
1014         struct ath9k_htc_target_vif hvif;
1015         int ret = 0;
1016         u8 cmd_rsp;
1017
1018         mutex_lock(&priv->mutex);
1019
1020         /* Only one interface for now */
1021         if (priv->nvifs > 0) {
1022                 ret = -ENOBUFS;
1023                 goto out;
1024         }
1025
1026         ath9k_htc_ps_wakeup(priv);
1027         memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
1028         memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
1029
1030         switch (vif->type) {
1031         case NL80211_IFTYPE_STATION:
1032                 hvif.opmode = cpu_to_be32(HTC_M_STA);
1033                 break;
1034         case NL80211_IFTYPE_ADHOC:
1035                 hvif.opmode = cpu_to_be32(HTC_M_IBSS);
1036                 break;
1037         default:
1038                 ath_err(common,
1039                         "Interface type %d not yet supported\n", vif->type);
1040                 ret = -EOPNOTSUPP;
1041                 goto out;
1042         }
1043
1044         ath_dbg(common, ATH_DBG_CONFIG,
1045                 "Attach a VIF of type: %d\n", vif->type);
1046
1047         priv->ah->opmode = vif->type;
1048
1049         /* Index starts from zero on the target */
1050         avp->index = hvif.index = priv->nvifs;
1051         hvif.rtsthreshold = cpu_to_be16(2304);
1052         WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
1053         if (ret)
1054                 goto out;
1055
1056         priv->nvifs++;
1057
1058         /*
1059          * We need a node in target to tx mgmt frames
1060          * before association.
1061          */
1062         ret = ath9k_htc_add_station(priv, vif, NULL);
1063         if (ret)
1064                 goto out;
1065
1066         ret = ath9k_htc_update_cap_target(priv);
1067         if (ret)
1068                 ath_dbg(common, ATH_DBG_CONFIG,
1069                         "Failed to update capability in target\n");
1070
1071         priv->vif = vif;
1072 out:
1073         ath9k_htc_ps_restore(priv);
1074         mutex_unlock(&priv->mutex);
1075
1076         return ret;
1077 }
1078
1079 static void ath9k_htc_remove_interface(struct ieee80211_hw *hw,
1080                                        struct ieee80211_vif *vif)
1081 {
1082         struct ath9k_htc_priv *priv = hw->priv;
1083         struct ath_common *common = ath9k_hw_common(priv->ah);
1084         struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1085         struct ath9k_htc_target_vif hvif;
1086         int ret = 0;
1087         u8 cmd_rsp;
1088
1089         ath_dbg(common, ATH_DBG_CONFIG, "Detach Interface\n");
1090
1091         mutex_lock(&priv->mutex);
1092         ath9k_htc_ps_wakeup(priv);
1093
1094         memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
1095         memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
1096         hvif.index = avp->index;
1097         WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
1098         priv->nvifs--;
1099
1100         ath9k_htc_remove_station(priv, vif, NULL);
1101         priv->vif = NULL;
1102
1103         ath9k_htc_ps_restore(priv);
1104         mutex_unlock(&priv->mutex);
1105 }
1106
1107 static int ath9k_htc_config(struct ieee80211_hw *hw, u32 changed)
1108 {
1109         struct ath9k_htc_priv *priv = hw->priv;
1110         struct ath_common *common = ath9k_hw_common(priv->ah);
1111         struct ieee80211_conf *conf = &hw->conf;
1112
1113         mutex_lock(&priv->mutex);
1114
1115         if (changed & IEEE80211_CONF_CHANGE_IDLE) {
1116                 bool enable_radio = false;
1117                 bool idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1118
1119                 mutex_lock(&priv->htc_pm_lock);
1120                 if (!idle && priv->ps_idle)
1121                         enable_radio = true;
1122                 priv->ps_idle = idle;
1123                 mutex_unlock(&priv->htc_pm_lock);
1124
1125                 if (enable_radio) {
1126                         ath_dbg(common, ATH_DBG_CONFIG,
1127                                 "not-idle: enabling radio\n");
1128                         ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
1129                         ath9k_htc_radio_enable(hw);
1130                 }
1131         }
1132
1133         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
1134                 struct ieee80211_channel *curchan = hw->conf.channel;
1135                 int pos = curchan->hw_value;
1136
1137                 ath_dbg(common, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
1138                         curchan->center_freq);
1139
1140                 ath9k_cmn_update_ichannel(&priv->ah->channels[pos],
1141                                           hw->conf.channel,
1142                                           hw->conf.channel_type);
1143
1144                 if (ath9k_htc_set_channel(priv, hw, &priv->ah->channels[pos]) < 0) {
1145                         ath_err(common, "Unable to set channel\n");
1146                         mutex_unlock(&priv->mutex);
1147                         return -EINVAL;
1148                 }
1149
1150         }
1151
1152         if (changed & IEEE80211_CONF_CHANGE_PS) {
1153                 if (conf->flags & IEEE80211_CONF_PS) {
1154                         ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
1155                         priv->ps_enabled = true;
1156                 } else {
1157                         priv->ps_enabled = false;
1158                         cancel_work_sync(&priv->ps_work);
1159                         ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
1160                 }
1161         }
1162
1163         if (changed & IEEE80211_CONF_CHANGE_POWER) {
1164                 priv->txpowlimit = 2 * conf->power_level;
1165                 ath_update_txpow(priv);
1166         }
1167
1168         if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1169                 if (conf->flags & IEEE80211_CONF_MONITOR) {
1170                         if (ath9k_htc_add_monitor_interface(priv))
1171                                 ath_err(common, "Failed to set monitor mode\n");
1172                         else
1173                                 ath_dbg(common, ATH_DBG_CONFIG,
1174                                         "HW opmode set to Monitor mode\n");
1175                 }
1176         }
1177
1178         if (changed & IEEE80211_CONF_CHANGE_IDLE) {
1179                 mutex_lock(&priv->htc_pm_lock);
1180                 if (!priv->ps_idle) {
1181                         mutex_unlock(&priv->htc_pm_lock);
1182                         goto out;
1183                 }
1184                 mutex_unlock(&priv->htc_pm_lock);
1185
1186                 ath_dbg(common, ATH_DBG_CONFIG,
1187                         "idle: disabling radio\n");
1188                 ath9k_htc_radio_disable(hw);
1189         }
1190
1191 out:
1192         mutex_unlock(&priv->mutex);
1193         return 0;
1194 }
1195
1196 #define SUPPORTED_FILTERS                       \
1197         (FIF_PROMISC_IN_BSS |                   \
1198         FIF_ALLMULTI |                          \
1199         FIF_CONTROL |                           \
1200         FIF_PSPOLL |                            \
1201         FIF_OTHER_BSS |                         \
1202         FIF_BCN_PRBRESP_PROMISC |               \
1203         FIF_PROBE_REQ |                         \
1204         FIF_FCSFAIL)
1205
1206 static void ath9k_htc_configure_filter(struct ieee80211_hw *hw,
1207                                        unsigned int changed_flags,
1208                                        unsigned int *total_flags,
1209                                        u64 multicast)
1210 {
1211         struct ath9k_htc_priv *priv = hw->priv;
1212         u32 rfilt;
1213
1214         mutex_lock(&priv->mutex);
1215         ath9k_htc_ps_wakeup(priv);
1216
1217         changed_flags &= SUPPORTED_FILTERS;
1218         *total_flags &= SUPPORTED_FILTERS;
1219
1220         priv->rxfilter = *total_flags;
1221         rfilt = ath9k_htc_calcrxfilter(priv);
1222         ath9k_hw_setrxfilter(priv->ah, rfilt);
1223
1224         ath_dbg(ath9k_hw_common(priv->ah), ATH_DBG_CONFIG,
1225                 "Set HW RX filter: 0x%x\n", rfilt);
1226
1227         ath9k_htc_ps_restore(priv);
1228         mutex_unlock(&priv->mutex);
1229 }
1230
1231 static int ath9k_htc_sta_add(struct ieee80211_hw *hw,
1232                              struct ieee80211_vif *vif,
1233                              struct ieee80211_sta *sta)
1234 {
1235         struct ath9k_htc_priv *priv = hw->priv;
1236         int ret;
1237
1238         mutex_lock(&priv->mutex);
1239         ath9k_htc_ps_wakeup(priv);
1240         ret = ath9k_htc_add_station(priv, vif, sta);
1241         if (!ret)
1242                 ath9k_htc_init_rate(priv, sta);
1243         ath9k_htc_ps_restore(priv);
1244         mutex_unlock(&priv->mutex);
1245
1246         return ret;
1247 }
1248
1249 static int ath9k_htc_sta_remove(struct ieee80211_hw *hw,
1250                                 struct ieee80211_vif *vif,
1251                                 struct ieee80211_sta *sta)
1252 {
1253         struct ath9k_htc_priv *priv = hw->priv;
1254         int ret;
1255
1256         mutex_lock(&priv->mutex);
1257         ath9k_htc_ps_wakeup(priv);
1258         ret = ath9k_htc_remove_station(priv, vif, sta);
1259         ath9k_htc_ps_restore(priv);
1260         mutex_unlock(&priv->mutex);
1261
1262         return ret;
1263 }
1264
1265 static int ath9k_htc_conf_tx(struct ieee80211_hw *hw, u16 queue,
1266                              const struct ieee80211_tx_queue_params *params)
1267 {
1268         struct ath9k_htc_priv *priv = hw->priv;
1269         struct ath_common *common = ath9k_hw_common(priv->ah);
1270         struct ath9k_tx_queue_info qi;
1271         int ret = 0, qnum;
1272
1273         if (queue >= WME_NUM_AC)
1274                 return 0;
1275
1276         mutex_lock(&priv->mutex);
1277         ath9k_htc_ps_wakeup(priv);
1278
1279         memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
1280
1281         qi.tqi_aifs = params->aifs;
1282         qi.tqi_cwmin = params->cw_min;
1283         qi.tqi_cwmax = params->cw_max;
1284         qi.tqi_burstTime = params->txop;
1285
1286         qnum = get_hw_qnum(queue, priv->hwq_map);
1287
1288         ath_dbg(common, ATH_DBG_CONFIG,
1289                 "Configure tx [queue/hwq] [%d/%d],  aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1290                 queue, qnum, params->aifs, params->cw_min,
1291                 params->cw_max, params->txop);
1292
1293         ret = ath_htc_txq_update(priv, qnum, &qi);
1294         if (ret) {
1295                 ath_err(common, "TXQ Update failed\n");
1296                 goto out;
1297         }
1298
1299         if ((priv->ah->opmode == NL80211_IFTYPE_ADHOC) &&
1300             (qnum == priv->hwq_map[WME_AC_BE]))
1301                     ath9k_htc_beaconq_config(priv);
1302 out:
1303         ath9k_htc_ps_restore(priv);
1304         mutex_unlock(&priv->mutex);
1305
1306         return ret;
1307 }
1308
1309 static int ath9k_htc_set_key(struct ieee80211_hw *hw,
1310                              enum set_key_cmd cmd,
1311                              struct ieee80211_vif *vif,
1312                              struct ieee80211_sta *sta,
1313                              struct ieee80211_key_conf *key)
1314 {
1315         struct ath9k_htc_priv *priv = hw->priv;
1316         struct ath_common *common = ath9k_hw_common(priv->ah);
1317         int ret = 0;
1318
1319         if (htc_modparam_nohwcrypt)
1320                 return -ENOSPC;
1321
1322         mutex_lock(&priv->mutex);
1323         ath_dbg(common, ATH_DBG_CONFIG, "Set HW Key\n");
1324         ath9k_htc_ps_wakeup(priv);
1325
1326         switch (cmd) {
1327         case SET_KEY:
1328                 ret = ath_key_config(common, vif, sta, key);
1329                 if (ret >= 0) {
1330                         key->hw_key_idx = ret;
1331                         /* push IV and Michael MIC generation to stack */
1332                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1333                         if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
1334                                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1335                         if (priv->ah->sw_mgmt_crypto &&
1336                             key->cipher == WLAN_CIPHER_SUITE_CCMP)
1337                                 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
1338                         ret = 0;
1339                 }
1340                 break;
1341         case DISABLE_KEY:
1342                 ath_key_delete(common, key);
1343                 break;
1344         default:
1345                 ret = -EINVAL;
1346         }
1347
1348         ath9k_htc_ps_restore(priv);
1349         mutex_unlock(&priv->mutex);
1350
1351         return ret;
1352 }
1353
1354 static void ath9k_htc_bss_info_changed(struct ieee80211_hw *hw,
1355                                        struct ieee80211_vif *vif,
1356                                        struct ieee80211_bss_conf *bss_conf,
1357                                        u32 changed)
1358 {
1359         struct ath9k_htc_priv *priv = hw->priv;
1360         struct ath_hw *ah = priv->ah;
1361         struct ath_common *common = ath9k_hw_common(ah);
1362
1363         mutex_lock(&priv->mutex);
1364         ath9k_htc_ps_wakeup(priv);
1365
1366         if (changed & BSS_CHANGED_ASSOC) {
1367                 common->curaid = bss_conf->assoc ?
1368                                  bss_conf->aid : 0;
1369                 ath_dbg(common, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
1370                         bss_conf->assoc);
1371
1372                 if (bss_conf->assoc) {
1373                         priv->op_flags |= OP_ASSOCIATED;
1374                         ath_start_ani(priv);
1375                 } else {
1376                         priv->op_flags &= ~OP_ASSOCIATED;
1377                         cancel_delayed_work_sync(&priv->ath9k_ani_work);
1378                 }
1379         }
1380
1381         if (changed & BSS_CHANGED_BSSID) {
1382                 /* Set BSSID */
1383                 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1384                 ath9k_hw_write_associd(ah);
1385
1386                 ath_dbg(common, ATH_DBG_CONFIG,
1387                         "BSSID: %pM aid: 0x%x\n",
1388                         common->curbssid, common->curaid);
1389         }
1390
1391         if ((changed & BSS_CHANGED_BEACON_INT) ||
1392             (changed & BSS_CHANGED_BEACON) ||
1393             ((changed & BSS_CHANGED_BEACON_ENABLED) &&
1394             bss_conf->enable_beacon)) {
1395                 priv->op_flags |= OP_ENABLE_BEACON;
1396                 ath9k_htc_beacon_config(priv, vif);
1397         }
1398
1399         if ((changed & BSS_CHANGED_BEACON_ENABLED) &&
1400             !bss_conf->enable_beacon) {
1401                 priv->op_flags &= ~OP_ENABLE_BEACON;
1402                 ath9k_htc_beacon_config(priv, vif);
1403         }
1404
1405         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1406                 ath_dbg(common, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
1407                         bss_conf->use_short_preamble);
1408                 if (bss_conf->use_short_preamble)
1409                         priv->op_flags |= OP_PREAMBLE_SHORT;
1410                 else
1411                         priv->op_flags &= ~OP_PREAMBLE_SHORT;
1412         }
1413
1414         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1415                 ath_dbg(common, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
1416                         bss_conf->use_cts_prot);
1417                 if (bss_conf->use_cts_prot &&
1418                     hw->conf.channel->band != IEEE80211_BAND_5GHZ)
1419                         priv->op_flags |= OP_PROTECT_ENABLE;
1420                 else
1421                         priv->op_flags &= ~OP_PROTECT_ENABLE;
1422         }
1423
1424         if (changed & BSS_CHANGED_ERP_SLOT) {
1425                 if (bss_conf->use_short_slot)
1426                         ah->slottime = 9;
1427                 else
1428                         ah->slottime = 20;
1429
1430                 ath9k_hw_init_global_settings(ah);
1431         }
1432
1433         if (changed & BSS_CHANGED_HT)
1434                 ath9k_htc_update_rate(priv, vif, bss_conf);
1435
1436         ath9k_htc_ps_restore(priv);
1437         mutex_unlock(&priv->mutex);
1438 }
1439
1440 static u64 ath9k_htc_get_tsf(struct ieee80211_hw *hw)
1441 {
1442         struct ath9k_htc_priv *priv = hw->priv;
1443         u64 tsf;
1444
1445         mutex_lock(&priv->mutex);
1446         ath9k_htc_ps_wakeup(priv);
1447         tsf = ath9k_hw_gettsf64(priv->ah);
1448         ath9k_htc_ps_restore(priv);
1449         mutex_unlock(&priv->mutex);
1450
1451         return tsf;
1452 }
1453
1454 static void ath9k_htc_set_tsf(struct ieee80211_hw *hw, u64 tsf)
1455 {
1456         struct ath9k_htc_priv *priv = hw->priv;
1457
1458         mutex_lock(&priv->mutex);
1459         ath9k_htc_ps_wakeup(priv);
1460         ath9k_hw_settsf64(priv->ah, tsf);
1461         ath9k_htc_ps_restore(priv);
1462         mutex_unlock(&priv->mutex);
1463 }
1464
1465 static void ath9k_htc_reset_tsf(struct ieee80211_hw *hw)
1466 {
1467         struct ath9k_htc_priv *priv = hw->priv;
1468
1469         mutex_lock(&priv->mutex);
1470         ath9k_htc_ps_wakeup(priv);
1471         ath9k_hw_reset_tsf(priv->ah);
1472         ath9k_htc_ps_restore(priv);
1473         mutex_unlock(&priv->mutex);
1474 }
1475
1476 static int ath9k_htc_ampdu_action(struct ieee80211_hw *hw,
1477                                   struct ieee80211_vif *vif,
1478                                   enum ieee80211_ampdu_mlme_action action,
1479                                   struct ieee80211_sta *sta,
1480                                   u16 tid, u16 *ssn)
1481 {
1482         struct ath9k_htc_priv *priv = hw->priv;
1483         struct ath9k_htc_sta *ista;
1484         int ret = 0;
1485
1486         switch (action) {
1487         case IEEE80211_AMPDU_RX_START:
1488                 break;
1489         case IEEE80211_AMPDU_RX_STOP:
1490                 break;
1491         case IEEE80211_AMPDU_TX_START:
1492                 ret = ath9k_htc_tx_aggr_oper(priv, vif, sta, action, tid);
1493                 if (!ret)
1494                         ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1495                 break;
1496         case IEEE80211_AMPDU_TX_STOP:
1497                 ath9k_htc_tx_aggr_oper(priv, vif, sta, action, tid);
1498                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1499                 break;
1500         case IEEE80211_AMPDU_TX_OPERATIONAL:
1501                 ista = (struct ath9k_htc_sta *) sta->drv_priv;
1502                 spin_lock_bh(&priv->tx_lock);
1503                 ista->tid_state[tid] = AGGR_OPERATIONAL;
1504                 spin_unlock_bh(&priv->tx_lock);
1505                 break;
1506         default:
1507                 ath_err(ath9k_hw_common(priv->ah), "Unknown AMPDU action\n");
1508         }
1509
1510         return ret;
1511 }
1512
1513 static void ath9k_htc_sw_scan_start(struct ieee80211_hw *hw)
1514 {
1515         struct ath9k_htc_priv *priv = hw->priv;
1516
1517         mutex_lock(&priv->mutex);
1518         spin_lock_bh(&priv->beacon_lock);
1519         priv->op_flags |= OP_SCANNING;
1520         spin_unlock_bh(&priv->beacon_lock);
1521         cancel_work_sync(&priv->ps_work);
1522         if (priv->op_flags & OP_ASSOCIATED)
1523                 cancel_delayed_work_sync(&priv->ath9k_ani_work);
1524         mutex_unlock(&priv->mutex);
1525 }
1526
1527 static void ath9k_htc_sw_scan_complete(struct ieee80211_hw *hw)
1528 {
1529         struct ath9k_htc_priv *priv = hw->priv;
1530
1531         mutex_lock(&priv->mutex);
1532         ath9k_htc_ps_wakeup(priv);
1533         spin_lock_bh(&priv->beacon_lock);
1534         priv->op_flags &= ~OP_SCANNING;
1535         spin_unlock_bh(&priv->beacon_lock);
1536         if (priv->op_flags & OP_ASSOCIATED) {
1537                 ath9k_htc_beacon_config(priv, priv->vif);
1538                 ath_start_ani(priv);
1539         }
1540         ath9k_htc_ps_restore(priv);
1541         mutex_unlock(&priv->mutex);
1542 }
1543
1544 static int ath9k_htc_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1545 {
1546         return 0;
1547 }
1548
1549 static void ath9k_htc_set_coverage_class(struct ieee80211_hw *hw,
1550                                          u8 coverage_class)
1551 {
1552         struct ath9k_htc_priv *priv = hw->priv;
1553
1554         mutex_lock(&priv->mutex);
1555         ath9k_htc_ps_wakeup(priv);
1556         priv->ah->coverage_class = coverage_class;
1557         ath9k_hw_init_global_settings(priv->ah);
1558         ath9k_htc_ps_restore(priv);
1559         mutex_unlock(&priv->mutex);
1560 }
1561
1562 struct ieee80211_ops ath9k_htc_ops = {
1563         .tx                 = ath9k_htc_tx,
1564         .start              = ath9k_htc_start,
1565         .stop               = ath9k_htc_stop,
1566         .add_interface      = ath9k_htc_add_interface,
1567         .remove_interface   = ath9k_htc_remove_interface,
1568         .config             = ath9k_htc_config,
1569         .configure_filter   = ath9k_htc_configure_filter,
1570         .sta_add            = ath9k_htc_sta_add,
1571         .sta_remove         = ath9k_htc_sta_remove,
1572         .conf_tx            = ath9k_htc_conf_tx,
1573         .bss_info_changed   = ath9k_htc_bss_info_changed,
1574         .set_key            = ath9k_htc_set_key,
1575         .get_tsf            = ath9k_htc_get_tsf,
1576         .set_tsf            = ath9k_htc_set_tsf,
1577         .reset_tsf          = ath9k_htc_reset_tsf,
1578         .ampdu_action       = ath9k_htc_ampdu_action,
1579         .sw_scan_start      = ath9k_htc_sw_scan_start,
1580         .sw_scan_complete   = ath9k_htc_sw_scan_complete,
1581         .set_rts_threshold  = ath9k_htc_set_rts_threshold,
1582         .rfkill_poll        = ath9k_htc_rfkill_poll_state,
1583         .set_coverage_class = ath9k_htc_set_coverage_class,
1584 };