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