9c9f3e0d6787690d355b7ef040e34069e0948238
[pandora-kernel.git] / drivers / net / wireless / ath / ath9k / htc_drv_main.c
1 /*
2  * Copyright (c) 2010 Atheros Communications Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include "htc.h"
18
19 #ifdef CONFIG_ATH9K_HTC_DEBUGFS
20 static struct dentry *ath9k_debugfs_root;
21 #endif
22
23 /*************/
24 /* Utilities */
25 /*************/
26
27 static void ath_update_txpow(struct ath9k_htc_priv *priv)
28 {
29         struct ath_hw *ah = priv->ah;
30         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         /* Cancel all the running timers/work .. */
1182         cancel_work_sync(&priv->ps_work);
1183         cancel_delayed_work_sync(&priv->ath9k_ani_work);
1184         cancel_delayed_work_sync(&priv->ath9k_aggr_work);
1185         cancel_delayed_work_sync(&priv->ath9k_led_blink_work);
1186         ath9k_led_stop_brightness(priv);
1187
1188         ath9k_htc_ps_wakeup(priv);
1189         htc_stop(priv->htc);
1190         WMI_CMD(WMI_DISABLE_INTR_CMDID);
1191         WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
1192         WMI_CMD(WMI_STOP_RECV_CMDID);
1193         ath9k_hw_phy_disable(ah);
1194         ath9k_hw_disable(ah);
1195         ath9k_hw_configpcipowersave(ah, 1, 1);
1196         ath9k_htc_ps_restore(priv);
1197         ath9k_htc_setpower(priv, ATH9K_PM_FULL_SLEEP);
1198
1199         skb_queue_purge(&priv->tx_queue);
1200
1201         /* Remove monitor interface here */
1202         if (ah->opmode == NL80211_IFTYPE_MONITOR) {
1203                 if (ath9k_htc_remove_monitor_interface(priv))
1204                         ath_print(common, ATH_DBG_FATAL,
1205                                   "Unable to remove monitor interface\n");
1206                 else
1207                         ath_print(common, ATH_DBG_CONFIG,
1208                                   "Monitor interface removed\n");
1209         }
1210
1211         priv->op_flags |= OP_INVALID;
1212         mutex_unlock(&priv->mutex);
1213
1214         ath_print(common, ATH_DBG_CONFIG, "Driver halt\n");
1215 }
1216
1217 static int ath9k_htc_add_interface(struct ieee80211_hw *hw,
1218                                    struct ieee80211_vif *vif)
1219 {
1220         struct ath9k_htc_priv *priv = hw->priv;
1221         struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1222         struct ath_common *common = ath9k_hw_common(priv->ah);
1223         struct ath9k_htc_target_vif hvif;
1224         int ret = 0;
1225         u8 cmd_rsp;
1226
1227         mutex_lock(&priv->mutex);
1228
1229         /* Only one interface for now */
1230         if (priv->nvifs > 0) {
1231                 ret = -ENOBUFS;
1232                 goto out;
1233         }
1234
1235         ath9k_htc_ps_wakeup(priv);
1236         memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
1237         memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
1238
1239         switch (vif->type) {
1240         case NL80211_IFTYPE_STATION:
1241                 hvif.opmode = cpu_to_be32(HTC_M_STA);
1242                 break;
1243         case NL80211_IFTYPE_ADHOC:
1244                 hvif.opmode = cpu_to_be32(HTC_M_IBSS);
1245                 break;
1246         default:
1247                 ath_print(common, ATH_DBG_FATAL,
1248                         "Interface type %d not yet supported\n", vif->type);
1249                 ret = -EOPNOTSUPP;
1250                 goto out;
1251         }
1252
1253         ath_print(common, ATH_DBG_CONFIG,
1254                   "Attach a VIF of type: %d\n", vif->type);
1255
1256         priv->ah->opmode = vif->type;
1257
1258         /* Index starts from zero on the target */
1259         avp->index = hvif.index = priv->nvifs;
1260         hvif.rtsthreshold = cpu_to_be16(2304);
1261         WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
1262         if (ret)
1263                 goto out;
1264
1265         priv->nvifs++;
1266
1267         /*
1268          * We need a node in target to tx mgmt frames
1269          * before association.
1270          */
1271         ret = ath9k_htc_add_station(priv, vif, NULL);
1272         if (ret)
1273                 goto out;
1274
1275         ret = ath9k_htc_update_cap_target(priv);
1276         if (ret)
1277                 ath_print(common, ATH_DBG_CONFIG, "Failed to update"
1278                           " capability in target \n");
1279
1280         priv->vif = vif;
1281 out:
1282         ath9k_htc_ps_restore(priv);
1283         mutex_unlock(&priv->mutex);
1284         return ret;
1285 }
1286
1287 static void ath9k_htc_remove_interface(struct ieee80211_hw *hw,
1288                                        struct ieee80211_vif *vif)
1289 {
1290         struct ath9k_htc_priv *priv = hw->priv;
1291         struct ath_common *common = ath9k_hw_common(priv->ah);
1292         struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1293         struct ath9k_htc_target_vif hvif;
1294         int ret = 0;
1295         u8 cmd_rsp;
1296
1297         ath_print(common, ATH_DBG_CONFIG, "Detach Interface\n");
1298
1299         mutex_lock(&priv->mutex);
1300
1301         memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
1302         memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
1303         hvif.index = avp->index;
1304         WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
1305         priv->nvifs--;
1306
1307         ath9k_htc_remove_station(priv, vif, NULL);
1308
1309         if (vif->type == NL80211_IFTYPE_ADHOC) {
1310                 spin_lock_bh(&priv->beacon_lock);
1311                 if (priv->beacon)
1312                         dev_kfree_skb_any(priv->beacon);
1313                 priv->beacon = NULL;
1314                 spin_unlock_bh(&priv->beacon_lock);
1315         }
1316
1317         priv->vif = NULL;
1318
1319         mutex_unlock(&priv->mutex);
1320 }
1321
1322 static int ath9k_htc_config(struct ieee80211_hw *hw, u32 changed)
1323 {
1324         struct ath9k_htc_priv *priv = hw->priv;
1325         struct ath_common *common = ath9k_hw_common(priv->ah);
1326         struct ieee80211_conf *conf = &hw->conf;
1327
1328         mutex_lock(&priv->mutex);
1329
1330         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
1331                 struct ieee80211_channel *curchan = hw->conf.channel;
1332                 int pos = curchan->hw_value;
1333                 bool is_cw40 = false;
1334
1335                 ath_print(common, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
1336                           curchan->center_freq);
1337
1338                 if (check_rc_update(hw, &is_cw40))
1339                         ath9k_htc_rc_update(priv, is_cw40);
1340
1341                 ath9k_cmn_update_ichannel(hw, &priv->ah->channels[pos]);
1342
1343                 if (ath9k_htc_set_channel(priv, hw, &priv->ah->channels[pos]) < 0) {
1344                         ath_print(common, ATH_DBG_FATAL,
1345                                   "Unable to set channel\n");
1346                         mutex_unlock(&priv->mutex);
1347                         return -EINVAL;
1348                 }
1349
1350         }
1351         if (changed & IEEE80211_CONF_CHANGE_PS) {
1352                 if (conf->flags & IEEE80211_CONF_PS) {
1353                         ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
1354                         priv->ps_enabled = true;
1355                 } else {
1356                         priv->ps_enabled = false;
1357                         cancel_work_sync(&priv->ps_work);
1358                         ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
1359                 }
1360         }
1361
1362         if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1363                 if (conf->flags & IEEE80211_CONF_MONITOR) {
1364                         if (ath9k_htc_add_monitor_interface(priv))
1365                                 ath_print(common, ATH_DBG_FATAL,
1366                                           "Failed to set monitor mode\n");
1367                         else
1368                                 ath_print(common, ATH_DBG_CONFIG,
1369                                           "HW opmode set to Monitor mode\n");
1370                 }
1371         }
1372
1373         mutex_unlock(&priv->mutex);
1374
1375         return 0;
1376 }
1377
1378 #define SUPPORTED_FILTERS                       \
1379         (FIF_PROMISC_IN_BSS |                   \
1380         FIF_ALLMULTI |                          \
1381         FIF_CONTROL |                           \
1382         FIF_PSPOLL |                            \
1383         FIF_OTHER_BSS |                         \
1384         FIF_BCN_PRBRESP_PROMISC |               \
1385         FIF_FCSFAIL)
1386
1387 static void ath9k_htc_configure_filter(struct ieee80211_hw *hw,
1388                                        unsigned int changed_flags,
1389                                        unsigned int *total_flags,
1390                                        u64 multicast)
1391 {
1392         struct ath9k_htc_priv *priv = hw->priv;
1393         u32 rfilt;
1394
1395         mutex_lock(&priv->mutex);
1396
1397         ath9k_htc_ps_wakeup(priv);
1398         changed_flags &= SUPPORTED_FILTERS;
1399         *total_flags &= SUPPORTED_FILTERS;
1400
1401         priv->rxfilter = *total_flags;
1402         rfilt = ath9k_htc_calcrxfilter(priv);
1403         ath9k_hw_setrxfilter(priv->ah, rfilt);
1404
1405         ath_print(ath9k_hw_common(priv->ah), ATH_DBG_CONFIG,
1406                   "Set HW RX filter: 0x%x\n", rfilt);
1407
1408         ath9k_htc_ps_restore(priv);
1409         mutex_unlock(&priv->mutex);
1410 }
1411
1412 static void ath9k_htc_sta_notify(struct ieee80211_hw *hw,
1413                                  struct ieee80211_vif *vif,
1414                                  enum sta_notify_cmd cmd,
1415                                  struct ieee80211_sta *sta)
1416 {
1417         struct ath9k_htc_priv *priv = hw->priv;
1418         int ret;
1419
1420         switch (cmd) {
1421         case STA_NOTIFY_ADD:
1422                 ret = ath9k_htc_add_station(priv, vif, sta);
1423                 if (!ret)
1424                         ath9k_htc_init_rate(priv, vif, sta);
1425                 break;
1426         case STA_NOTIFY_REMOVE:
1427                 ath9k_htc_remove_station(priv, vif, sta);
1428                 break;
1429         default:
1430                 break;
1431         }
1432 }
1433
1434 static int ath9k_htc_conf_tx(struct ieee80211_hw *hw, u16 queue,
1435                              const struct ieee80211_tx_queue_params *params)
1436 {
1437         struct ath9k_htc_priv *priv = hw->priv;
1438         struct ath_common *common = ath9k_hw_common(priv->ah);
1439         struct ath9k_tx_queue_info qi;
1440         int ret = 0, qnum;
1441
1442         if (queue >= WME_NUM_AC)
1443                 return 0;
1444
1445         mutex_lock(&priv->mutex);
1446
1447         memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
1448
1449         qi.tqi_aifs = params->aifs;
1450         qi.tqi_cwmin = params->cw_min;
1451         qi.tqi_cwmax = params->cw_max;
1452         qi.tqi_burstTime = params->txop;
1453
1454         qnum = get_hw_qnum(queue, priv->hwq_map);
1455
1456         ath_print(common, ATH_DBG_CONFIG,
1457                   "Configure tx [queue/hwq] [%d/%d],  "
1458                   "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1459                   queue, qnum, params->aifs, params->cw_min,
1460                   params->cw_max, params->txop);
1461
1462         ret = ath_htc_txq_update(priv, qnum, &qi);
1463         if (ret)
1464                 ath_print(common, ATH_DBG_FATAL, "TXQ Update failed\n");
1465
1466         mutex_unlock(&priv->mutex);
1467
1468         return ret;
1469 }
1470
1471 static int ath9k_htc_set_key(struct ieee80211_hw *hw,
1472                              enum set_key_cmd cmd,
1473                              struct ieee80211_vif *vif,
1474                              struct ieee80211_sta *sta,
1475                              struct ieee80211_key_conf *key)
1476 {
1477         struct ath9k_htc_priv *priv = hw->priv;
1478         struct ath_common *common = ath9k_hw_common(priv->ah);
1479         int ret = 0;
1480
1481         if (htc_modparam_nohwcrypt)
1482                 return -ENOSPC;
1483
1484         mutex_lock(&priv->mutex);
1485         ath_print(common, ATH_DBG_CONFIG, "Set HW Key\n");
1486         ath9k_htc_ps_wakeup(priv);
1487
1488         switch (cmd) {
1489         case SET_KEY:
1490                 ret = ath9k_cmn_key_config(common, vif, sta, key);
1491                 if (ret >= 0) {
1492                         key->hw_key_idx = ret;
1493                         /* push IV and Michael MIC generation to stack */
1494                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1495                         if (key->alg == ALG_TKIP)
1496                                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1497                         if (priv->ah->sw_mgmt_crypto && key->alg == ALG_CCMP)
1498                                 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
1499                         ret = 0;
1500                 }
1501                 break;
1502         case DISABLE_KEY:
1503                 ath9k_cmn_key_delete(common, key);
1504                 break;
1505         default:
1506                 ret = -EINVAL;
1507         }
1508
1509         ath9k_htc_ps_restore(priv);
1510         mutex_unlock(&priv->mutex);
1511
1512         return ret;
1513 }
1514
1515 static void ath9k_htc_bss_info_changed(struct ieee80211_hw *hw,
1516                                        struct ieee80211_vif *vif,
1517                                        struct ieee80211_bss_conf *bss_conf,
1518                                        u32 changed)
1519 {
1520         struct ath9k_htc_priv *priv = hw->priv;
1521         struct ath_hw *ah = priv->ah;
1522         struct ath_common *common = ath9k_hw_common(ah);
1523
1524         mutex_lock(&priv->mutex);
1525         ath9k_htc_ps_wakeup(priv);
1526
1527         if (changed & BSS_CHANGED_ASSOC) {
1528                 common->curaid = bss_conf->assoc ?
1529                                  bss_conf->aid : 0;
1530                 ath_print(common, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
1531                         bss_conf->assoc);
1532
1533                 if (bss_conf->assoc) {
1534                         priv->op_flags |= OP_ASSOCIATED;
1535                         ath_start_ani(priv);
1536                 } else {
1537                         priv->op_flags &= ~OP_ASSOCIATED;
1538                         cancel_work_sync(&priv->ps_work);
1539                         cancel_delayed_work_sync(&priv->ath9k_ani_work);
1540                 }
1541         }
1542
1543         if (changed & BSS_CHANGED_BSSID) {
1544                 /* Set BSSID */
1545                 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1546                 ath9k_hw_write_associd(ah);
1547
1548                 ath_print(common, ATH_DBG_CONFIG,
1549                           "BSSID: %pM aid: 0x%x\n",
1550                           common->curbssid, common->curaid);
1551         }
1552
1553         if ((changed & BSS_CHANGED_BEACON_INT) ||
1554             (changed & BSS_CHANGED_BEACON) ||
1555             ((changed & BSS_CHANGED_BEACON_ENABLED) &&
1556             bss_conf->enable_beacon)) {
1557                 priv->op_flags |= OP_ENABLE_BEACON;
1558                 ath9k_htc_beacon_config(priv, vif);
1559         }
1560
1561         if (changed & BSS_CHANGED_BEACON)
1562                 ath9k_htc_beacon_update(priv, vif);
1563
1564         if ((changed & BSS_CHANGED_BEACON_ENABLED) &&
1565             !bss_conf->enable_beacon) {
1566                 priv->op_flags &= ~OP_ENABLE_BEACON;
1567                 ath9k_htc_beacon_config(priv, vif);
1568         }
1569
1570         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1571                 ath_print(common, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
1572                           bss_conf->use_short_preamble);
1573                 if (bss_conf->use_short_preamble)
1574                         priv->op_flags |= OP_PREAMBLE_SHORT;
1575                 else
1576                         priv->op_flags &= ~OP_PREAMBLE_SHORT;
1577         }
1578
1579         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1580                 ath_print(common, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
1581                           bss_conf->use_cts_prot);
1582                 if (bss_conf->use_cts_prot &&
1583                     hw->conf.channel->band != IEEE80211_BAND_5GHZ)
1584                         priv->op_flags |= OP_PROTECT_ENABLE;
1585                 else
1586                         priv->op_flags &= ~OP_PROTECT_ENABLE;
1587         }
1588
1589         if (changed & BSS_CHANGED_ERP_SLOT) {
1590                 if (bss_conf->use_short_slot)
1591                         ah->slottime = 9;
1592                 else
1593                         ah->slottime = 20;
1594
1595                 ath9k_hw_init_global_settings(ah);
1596         }
1597
1598         ath9k_htc_ps_restore(priv);
1599         mutex_unlock(&priv->mutex);
1600 }
1601
1602 static u64 ath9k_htc_get_tsf(struct ieee80211_hw *hw)
1603 {
1604         struct ath9k_htc_priv *priv = hw->priv;
1605         u64 tsf;
1606
1607         mutex_lock(&priv->mutex);
1608         tsf = ath9k_hw_gettsf64(priv->ah);
1609         mutex_unlock(&priv->mutex);
1610
1611         return tsf;
1612 }
1613
1614 static void ath9k_htc_set_tsf(struct ieee80211_hw *hw, u64 tsf)
1615 {
1616         struct ath9k_htc_priv *priv = hw->priv;
1617
1618         mutex_lock(&priv->mutex);
1619         ath9k_hw_settsf64(priv->ah, tsf);
1620         mutex_unlock(&priv->mutex);
1621 }
1622
1623 static void ath9k_htc_reset_tsf(struct ieee80211_hw *hw)
1624 {
1625         struct ath9k_htc_priv *priv = hw->priv;
1626
1627         ath9k_htc_ps_wakeup(priv);
1628         mutex_lock(&priv->mutex);
1629         ath9k_hw_reset_tsf(priv->ah);
1630         mutex_unlock(&priv->mutex);
1631         ath9k_htc_ps_restore(priv);
1632 }
1633
1634 static int ath9k_htc_ampdu_action(struct ieee80211_hw *hw,
1635                                   struct ieee80211_vif *vif,
1636                                   enum ieee80211_ampdu_mlme_action action,
1637                                   struct ieee80211_sta *sta,
1638                                   u16 tid, u16 *ssn)
1639 {
1640         struct ath9k_htc_priv *priv = hw->priv;
1641         struct ath9k_htc_aggr_work *work = &priv->aggr_work;
1642         struct ath9k_htc_sta *ista;
1643
1644         switch (action) {
1645         case IEEE80211_AMPDU_RX_START:
1646                 break;
1647         case IEEE80211_AMPDU_RX_STOP:
1648                 break;
1649         case IEEE80211_AMPDU_TX_START:
1650         case IEEE80211_AMPDU_TX_STOP:
1651                 if (!(priv->op_flags & OP_TXAGGR))
1652                         return -ENOTSUPP;
1653                 memcpy(work->sta_addr, sta->addr, ETH_ALEN);
1654                 work->hw = hw;
1655                 work->vif = vif;
1656                 work->action = action;
1657                 work->tid = tid;
1658                 ieee80211_queue_delayed_work(hw, &priv->ath9k_aggr_work, 0);
1659                 break;
1660         case IEEE80211_AMPDU_TX_OPERATIONAL:
1661                 ista = (struct ath9k_htc_sta *) sta->drv_priv;
1662                 ista->tid_state[tid] = AGGR_OPERATIONAL;
1663                 break;
1664         default:
1665                 ath_print(ath9k_hw_common(priv->ah), ATH_DBG_FATAL,
1666                           "Unknown AMPDU action\n");
1667         }
1668
1669         return 0;
1670 }
1671
1672 static void ath9k_htc_sw_scan_start(struct ieee80211_hw *hw)
1673 {
1674         struct ath9k_htc_priv *priv = hw->priv;
1675
1676         mutex_lock(&priv->mutex);
1677         spin_lock_bh(&priv->beacon_lock);
1678         priv->op_flags |= OP_SCANNING;
1679         spin_unlock_bh(&priv->beacon_lock);
1680         cancel_work_sync(&priv->ps_work);
1681         cancel_delayed_work_sync(&priv->ath9k_ani_work);
1682         mutex_unlock(&priv->mutex);
1683 }
1684
1685 static void ath9k_htc_sw_scan_complete(struct ieee80211_hw *hw)
1686 {
1687         struct ath9k_htc_priv *priv = hw->priv;
1688
1689         ath9k_htc_ps_wakeup(priv);
1690         mutex_lock(&priv->mutex);
1691         spin_lock_bh(&priv->beacon_lock);
1692         priv->op_flags &= ~OP_SCANNING;
1693         spin_unlock_bh(&priv->beacon_lock);
1694         priv->op_flags |= OP_FULL_RESET;
1695         if (priv->op_flags & OP_ASSOCIATED)
1696                 ath9k_htc_beacon_config(priv, priv->vif);
1697         ath_start_ani(priv);
1698         mutex_unlock(&priv->mutex);
1699         ath9k_htc_ps_restore(priv);
1700 }
1701
1702 static int ath9k_htc_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1703 {
1704         return 0;
1705 }
1706
1707 static void ath9k_htc_set_coverage_class(struct ieee80211_hw *hw,
1708                                          u8 coverage_class)
1709 {
1710         struct ath9k_htc_priv *priv = hw->priv;
1711
1712         mutex_lock(&priv->mutex);
1713         priv->ah->coverage_class = coverage_class;
1714         ath9k_hw_init_global_settings(priv->ah);
1715         mutex_unlock(&priv->mutex);
1716 }
1717
1718 struct ieee80211_ops ath9k_htc_ops = {
1719         .tx                 = ath9k_htc_tx,
1720         .start              = ath9k_htc_start,
1721         .stop               = ath9k_htc_stop,
1722         .add_interface      = ath9k_htc_add_interface,
1723         .remove_interface   = ath9k_htc_remove_interface,
1724         .config             = ath9k_htc_config,
1725         .configure_filter   = ath9k_htc_configure_filter,
1726         .sta_notify         = ath9k_htc_sta_notify,
1727         .conf_tx            = ath9k_htc_conf_tx,
1728         .bss_info_changed   = ath9k_htc_bss_info_changed,
1729         .set_key            = ath9k_htc_set_key,
1730         .get_tsf            = ath9k_htc_get_tsf,
1731         .set_tsf            = ath9k_htc_set_tsf,
1732         .reset_tsf          = ath9k_htc_reset_tsf,
1733         .ampdu_action       = ath9k_htc_ampdu_action,
1734         .sw_scan_start      = ath9k_htc_sw_scan_start,
1735         .sw_scan_complete   = ath9k_htc_sw_scan_complete,
1736         .set_rts_threshold  = ath9k_htc_set_rts_threshold,
1737         .rfkill_poll        = ath9k_htc_rfkill_poll_state,
1738         .set_coverage_class = ath9k_htc_set_coverage_class,
1739 };