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