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