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