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