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