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