ath9k_htc: Fix beaconing in IBSS mode
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / ath / ath9k / htc_drv_main.c
blob7c9e33ba95ab230900197d8efdb44b3531bf7485
1 /*
2 * Copyright (c) 2010 Atheros Communications Inc.
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.
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.
17 #include "htc.h"
19 #ifdef CONFIG_ATH9K_HTC_DEBUGFS
20 static struct dentry *ath9k_debugfs_root;
21 #endif
23 /*************/
24 /* Utilities */
25 /*************/
27 static void ath_update_txpow(struct ath9k_htc_priv *priv)
29 struct ath_hw *ah = priv->ah;
30 u32 txpow;
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;
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)
44 enum htc_phymode mode;
46 mode = HTC_MODE_AUTO;
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;
65 return mode;
68 static bool ath9k_htc_setpower(struct ath9k_htc_priv *priv,
69 enum ath9k_power_mode mode)
71 bool ret;
73 mutex_lock(&priv->htc_pm_lock);
74 ret = ath9k_hw_setpower(priv->ah, mode);
75 mutex_unlock(&priv->htc_pm_lock);
77 return ret;
80 void ath9k_htc_ps_wakeup(struct ath9k_htc_priv *priv)
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);
87 unlock:
88 mutex_unlock(&priv->htc_pm_lock);
91 void ath9k_htc_ps_restore(struct ath9k_htc_priv *priv)
93 mutex_lock(&priv->htc_pm_lock);
94 if (--priv->ps_usecount != 0)
95 goto unlock;
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);
102 unlock:
103 mutex_unlock(&priv->htc_pm_lock);
106 void ath9k_ps_work(struct work_struct *work)
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);
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.
118 ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
121 static int ath9k_htc_set_channel(struct ath9k_htc_priv *priv,
122 struct ieee80211_hw *hw,
123 struct ath9k_channel *hchan)
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;
135 if (priv->op_flags & OP_INVALID)
136 return -EIO;
138 if (priv->op_flags & OP_FULL_RESET)
139 fastcc = false;
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);
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));
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;
162 ath_update_txpow(priv);
164 WMI_CMD(WMI_START_RECV_CMDID);
165 if (ret)
166 goto err;
168 ath9k_host_rx_init(priv);
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;
176 WMI_CMD(WMI_ENABLE_INTR_CMDID);
177 if (ret)
178 goto err;
180 htc_start(priv->htc);
182 priv->op_flags &= ~OP_FULL_RESET;
183 err:
184 ath9k_htc_ps_restore(priv);
185 return ret;
188 static int ath9k_htc_add_monitor_interface(struct ath9k_htc_priv *priv)
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;
195 if (priv->nvifs > 0)
196 return -ENOBUFS;
198 memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
199 memcpy(&hvif.myaddr, common->macaddr, ETH_ALEN);
201 hvif.opmode = cpu_to_be32(HTC_M_MONITOR);
202 priv->ah->opmode = NL80211_IFTYPE_MONITOR;
203 hvif.index = priv->nvifs;
205 WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
206 if (ret)
207 return ret;
209 priv->nvifs++;
210 return 0;
213 static int ath9k_htc_remove_monitor_interface(struct ath9k_htc_priv *priv)
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;
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--;
226 return ret;
229 static int ath9k_htc_add_station(struct ath9k_htc_priv *priv,
230 struct ieee80211_vif *vif,
231 struct ieee80211_sta *sta)
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;
240 if (priv->nstations >= ATH9K_HTC_MAX_STA)
241 return -ENOBUFS;
243 memset(&tsta, 0, sizeof(struct ath9k_htc_target_sta));
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;
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);
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;
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);
277 priv->nstations++;
278 return 0;
281 static int ath9k_htc_remove_station(struct ath9k_htc_priv *priv,
282 struct ieee80211_vif *vif,
283 struct ieee80211_sta *sta)
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;
290 if (sta) {
291 ista = (struct ath9k_htc_sta *) sta->drv_priv;
292 sta_idx = ista->index;
293 } else {
294 sta_idx = 0;
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;
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);
311 priv->nstations--;
312 return 0;
315 static int ath9k_htc_update_cap_target(struct ath9k_htc_priv *priv)
317 struct ath9k_htc_cap_target tcap;
318 int ret;
319 u8 cmd_rsp;
321 memset(&tcap, 0, sizeof(struct ath9k_htc_cap_target));
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;
332 WMI_CMD_BUF(WMI_TARGET_IC_UPDATE_CMDID, &tcap);
334 return ret;
337 static int ath9k_htc_init_rate(struct ath9k_htc_priv *priv,
338 struct ieee80211_vif *vif,
339 struct ieee80211_sta *sta)
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;
349 memset(&trate, 0, sizeof(trate));
351 /* Only 2GHz is supported */
352 sband = priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ];
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++;
361 priv->tgt_rate.rates.legacy_rates.rs_nrates = j;
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;
370 priv->tgt_rate.rates.ht_rates.rs_nrates = j;
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;
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);
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;
393 ath_print(common, ATH_DBG_CONFIG,
394 "Updated target STA: %pM (caps: 0x%x)\n", sta->addr, caps);
395 return 0;
398 static bool check_rc_update(struct ieee80211_hw *hw, bool *cw40)
400 struct ath9k_htc_priv *priv = hw->priv;
401 struct ieee80211_conf *conf = &hw->conf;
403 if (!conf_is_ht(conf))
404 return false;
406 if (!(priv->op_flags & OP_ASSOCIATED) ||
407 (priv->op_flags & OP_SCANNING))
408 return false;
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;
418 } else { /* ht20 */
419 if (priv->ah->curchan->chanmode & CHANNEL_HT20)
420 return false;
421 else
422 return true;
426 static void ath9k_htc_rc_update(struct ath9k_htc_priv *priv, bool is_cw40)
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;
434 memset(&trate, 0, sizeof(trate));
436 trate = priv->tgt_rate;
438 if (is_cw40)
439 caps |= WLAN_RC_40_FLAG;
440 else
441 caps &= ~WLAN_RC_40_FLAG;
443 priv->tgt_rate.capflags = cpu_to_be32(caps);
444 trate.capflags = cpu_to_be32(caps);
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;
453 ath_print(common, ATH_DBG_CONFIG, "Rate control updated with "
454 "caps:0x%x on target\n", priv->tgt_rate.capflags);
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)
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;
468 if (tid > ATH9K_HTC_MAX_TID)
469 return -EINVAL;
471 memset(&aggr, 0, sizeof(struct ath9k_htc_target_aggr));
473 rcu_read_lock();
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;
482 ista = (struct ath9k_htc_sta *) sta->drv_priv;
484 if (oper)
485 ista->tid_state[tid] = AGGR_START;
486 else
487 ista->tid_state[tid] = AGGR_STOP;
489 aggr.sta_index = ista->index;
491 rcu_read_unlock();
493 aggr.tidno = tid;
494 aggr.aggr_enable = oper;
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);
506 return ret;
509 void ath9k_htc_aggr_work(struct work_struct *work)
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;
517 mutex_lock(&wk->mutex);
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");
537 mutex_unlock(&wk->mutex);
540 /*********/
541 /* DEBUG */
542 /*********/
544 #ifdef CONFIG_ATH9K_HTC_DEBUGFS
546 static int ath9k_debugfs_open(struct inode *inode, struct file *file)
548 file->private_data = inode->i_private;
549 return 0;
552 static ssize_t read_file_tgt_stats(struct file *file, char __user *user_buf,
553 size_t count, loff_t *ppos)
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;
562 memset(&cmd_rsp, 0, sizeof(cmd_rsp));
564 WMI_CMD(WMI_TGT_STATS_CMDID);
565 if (ret)
566 return -EINVAL;
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);
587 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
590 static const struct file_operations fops_tgt_stats = {
591 .read = read_file_tgt_stats,
592 .open = ath9k_debugfs_open,
593 .owner = THIS_MODULE
596 static ssize_t read_file_xmit(struct file *file, char __user *user_buf,
597 size_t count, loff_t *ppos)
599 struct ath9k_htc_priv *priv =
600 (struct ath9k_htc_priv *) file->private_data;
601 char buf[512];
602 unsigned int len = 0;
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);
620 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
623 static const struct file_operations fops_xmit = {
624 .read = read_file_xmit,
625 .open = ath9k_debugfs_open,
626 .owner = THIS_MODULE
629 static ssize_t read_file_recv(struct file *file, char __user *user_buf,
630 size_t count, loff_t *ppos)
632 struct ath9k_htc_priv *priv =
633 (struct ath9k_htc_priv *) file->private_data;
634 char buf[512];
635 unsigned int len = 0;
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);
647 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
650 static const struct file_operations fops_recv = {
651 .read = read_file_recv,
652 .open = ath9k_debugfs_open,
653 .owner = THIS_MODULE
656 int ath9k_htc_init_debug(struct ath_hw *ah)
658 struct ath_common *common = ath9k_hw_common(ah);
659 struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
661 if (!ath9k_debugfs_root)
662 return -ENOENT;
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;
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;
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;
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;
688 return 0;
690 err:
691 ath9k_htc_exit_debug(ah);
692 return -ENOMEM;
695 void ath9k_htc_exit_debug(struct ath_hw *ah)
697 struct ath_common *common = ath9k_hw_common(ah);
698 struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
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);
706 int ath9k_htc_debug_create_root(void)
708 ath9k_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
709 if (!ath9k_debugfs_root)
710 return -ENOENT;
712 return 0;
715 void ath9k_htc_debug_remove_root(void)
717 debugfs_remove(ath9k_debugfs_root);
718 ath9k_debugfs_root = NULL;
721 #endif /* CONFIG_ATH9K_HTC_DEBUGFS */
723 /*******/
724 /* ANI */
725 /*******/
727 static void ath_start_ani(struct ath9k_htc_priv *priv)
729 struct ath_common *common = ath9k_hw_common(priv->ah);
730 unsigned long timestamp = jiffies_to_msecs(jiffies);
732 common->ani.longcal_timer = timestamp;
733 common->ani.shortcal_timer = timestamp;
734 common->ani.checkani_timer = timestamp;
736 ieee80211_queue_delayed_work(common->hw, &priv->ath9k_ani_work,
737 msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
740 void ath9k_ani_work(struct work_struct *work)
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;
753 short_cal_interval = ATH_STA_SHORT_CALINTERVAL;
755 /* Only calibrate if awake */
756 if (ah->power_mode != ATH9K_PM_AWAKE)
757 goto set_timer;
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;
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;
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;
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;
791 /* Skip all processing if there's nothing to do. */
792 if (longcal || shortcal || aniflag) {
794 ath9k_htc_ps_wakeup(priv);
796 /* Call ANI routine if necessary */
797 if (aniflag)
798 ath9k_hw_ani_monitor(ah, ah->curchan);
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);
807 if (longcal)
808 common->ani.noise_floor =
809 ath9k_hw_getchan_noise(ah, ah->curchan);
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);
818 ath9k_htc_ps_restore(priv);
821 set_timer:
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.
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);
833 ieee80211_queue_delayed_work(common->hw, &priv->ath9k_ani_work,
834 msecs_to_jiffies(cal_interval));
837 /*******/
838 /* LED */
839 /*******/
841 static void ath9k_led_blink_work(struct work_struct *work)
843 struct ath9k_htc_priv *priv = container_of(work, struct ath9k_htc_priv,
844 ath9k_led_blink_work.work);
846 if (!(priv->op_flags & OP_LED_ASSOCIATED))
847 return;
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);
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));
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;
870 if (priv->op_flags & OP_LED_ON)
871 priv->op_flags &= ~OP_LED_ON;
872 else
873 priv->op_flags |= OP_LED_ON;
876 static void ath9k_led_brightness_work(struct work_struct *work)
878 struct ath_led *led = container_of(work, struct ath_led,
879 brightness_work.work);
880 struct ath9k_htc_priv *priv = led->priv;
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++;
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++;
906 break;
907 default:
908 break;
912 static void ath9k_led_brightness(struct led_classdev *led_cdev,
913 enum led_brightness brightness)
915 struct ath_led *led = container_of(led_cdev, struct ath_led, led_cdev);
916 struct ath9k_htc_priv *priv = led->priv;
918 led->brightness = brightness;
919 if (!(priv->op_flags & OP_LED_DEINIT))
920 ieee80211_queue_delayed_work(priv->hw,
921 &led->brightness_work, 0);
924 static void ath9k_led_stop_brightness(struct ath9k_htc_priv *priv)
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);
932 static int ath9k_register_led(struct ath9k_htc_priv *priv, struct ath_led *led,
933 char *trigger)
935 int ret;
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;
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;
949 INIT_DELAYED_WORK(&led->brightness_work, ath9k_led_brightness_work);
951 return ret;
954 static void ath9k_unregister_led(struct ath_led *led)
956 if (led->registered) {
957 led_classdev_unregister(&led->led_cdev);
958 led->registered = 0;
962 void ath9k_deinit_leds(struct ath9k_htc_priv *priv)
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);
972 void ath9k_init_leds(struct ath9k_htc_priv *priv)
974 char *trigger;
975 int ret;
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;
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);
990 INIT_DELAYED_WORK(&priv->ath9k_led_blink_work, ath9k_led_blink_work);
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;
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;
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;
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;
1024 priv->op_flags &= ~OP_LED_DEINIT;
1026 return;
1028 fail:
1029 cancel_delayed_work_sync(&priv->ath9k_led_blink_work);
1030 ath9k_deinit_leds(priv);
1033 /*******************/
1034 /* Rfkill */
1035 /*******************/
1037 static bool ath_is_rfkill_set(struct ath9k_htc_priv *priv)
1039 return ath9k_hw_gpio_get(priv->ah, priv->ah->rfkill_gpio) ==
1040 priv->ah->rfkill_polarity;
1043 static void ath9k_htc_rfkill_poll_state(struct ieee80211_hw *hw)
1045 struct ath9k_htc_priv *priv = hw->priv;
1046 bool blocked = !!ath_is_rfkill_set(priv);
1048 wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
1051 void ath9k_start_rfkill_poll(struct ath9k_htc_priv *priv)
1053 if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
1054 wiphy_rfkill_start_polling(priv->hw->wiphy);
1057 /**********************/
1058 /* mac80211 Callbacks */
1059 /**********************/
1061 static int ath9k_htc_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
1063 struct ieee80211_hdr *hdr;
1064 struct ath9k_htc_priv *priv = hw->priv;
1065 int padpos, padsize, ret;
1067 hdr = (struct ieee80211_hdr *) skb->data;
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);
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");
1092 goto fail_tx;
1095 return 0;
1097 fail_tx:
1098 dev_kfree_skb_any(skb);
1099 return 0;
1102 static int ath9k_htc_radio_enable(struct ieee80211_hw *hw)
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;
1114 ath_print(common, ATH_DBG_CONFIG,
1115 "Starting driver with initial channel: %d MHz\n",
1116 curchan->center_freq);
1118 /* setup initial channel */
1119 init_channel = ath9k_cmn_get_curchannel(hw, ah);
1121 /* Reset SERDES registers */
1122 ath9k_hw_configpcipowersave(ah, 0, 0);
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;
1133 ath_update_txpow(priv);
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);
1141 ath9k_host_rx_init(priv);
1143 priv->op_flags &= ~OP_INVALID;
1144 htc_start(priv->htc);
1146 spin_lock_bh(&priv->tx_lock);
1147 priv->tx_queues_stop = false;
1148 spin_unlock_bh(&priv->tx_lock);
1150 ieee80211_wake_queues(hw);
1152 return ret;
1155 static int ath9k_htc_start(struct ieee80211_hw *hw)
1157 struct ath9k_htc_priv *priv = hw->priv;
1158 int ret = 0;
1160 mutex_lock(&priv->mutex);
1161 ret = ath9k_htc_radio_enable(hw);
1162 mutex_unlock(&priv->mutex);
1164 return ret;
1167 static void ath9k_htc_radio_disable(struct ieee80211_hw *hw)
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;
1175 if (priv->op_flags & OP_INVALID) {
1176 ath_print(common, ATH_DBG_ANY, "Device not present\n");
1177 return;
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);
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);
1198 skb_queue_purge(&priv->tx_queue);
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");
1210 priv->op_flags |= OP_INVALID;
1212 ath_print(common, ATH_DBG_CONFIG, "Driver halt\n");
1215 static void ath9k_htc_stop(struct ieee80211_hw *hw)
1217 struct ath9k_htc_priv *priv = hw->priv;
1219 mutex_lock(&priv->mutex);
1220 ath9k_htc_radio_disable(hw);
1221 mutex_unlock(&priv->mutex);
1225 static int ath9k_htc_add_interface(struct ieee80211_hw *hw,
1226 struct ieee80211_vif *vif)
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;
1235 mutex_lock(&priv->mutex);
1237 /* Only one interface for now */
1238 if (priv->nvifs > 0) {
1239 ret = -ENOBUFS;
1240 goto out;
1243 ath9k_htc_ps_wakeup(priv);
1244 memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
1245 memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
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;
1261 ath_print(common, ATH_DBG_CONFIG,
1262 "Attach a VIF of type: %d\n", vif->type);
1264 priv->ah->opmode = vif->type;
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;
1273 priv->nvifs++;
1276 * We need a node in target to tx mgmt frames
1277 * before association.
1279 ret = ath9k_htc_add_station(priv, vif, NULL);
1280 if (ret)
1281 goto out;
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");
1288 priv->vif = vif;
1289 out:
1290 ath9k_htc_ps_restore(priv);
1291 mutex_unlock(&priv->mutex);
1292 return ret;
1295 static void ath9k_htc_remove_interface(struct ieee80211_hw *hw,
1296 struct ieee80211_vif *vif)
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;
1305 ath_print(common, ATH_DBG_CONFIG, "Detach Interface\n");
1307 mutex_lock(&priv->mutex);
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--;
1315 ath9k_htc_remove_station(priv, vif, NULL);
1316 priv->vif = NULL;
1318 mutex_unlock(&priv->mutex);
1321 static int ath9k_htc_config(struct ieee80211_hw *hw, u32 changed)
1323 struct ath9k_htc_priv *priv = hw->priv;
1324 struct ath_common *common = ath9k_hw_common(priv->ah);
1325 struct ieee80211_conf *conf = &hw->conf;
1327 mutex_lock(&priv->mutex);
1329 if (changed & IEEE80211_CONF_CHANGE_IDLE) {
1330 bool enable_radio = false;
1331 bool idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1333 if (!idle && priv->ps_idle)
1334 enable_radio = true;
1336 priv->ps_idle = idle;
1338 if (enable_radio) {
1339 ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
1340 ath9k_htc_radio_enable(hw);
1341 ath_print(common, ATH_DBG_CONFIG,
1342 "not-idle: enabling radio\n");
1346 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
1347 struct ieee80211_channel *curchan = hw->conf.channel;
1348 int pos = curchan->hw_value;
1349 bool is_cw40 = false;
1351 ath_print(common, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
1352 curchan->center_freq);
1354 if (check_rc_update(hw, &is_cw40))
1355 ath9k_htc_rc_update(priv, is_cw40);
1357 ath9k_cmn_update_ichannel(hw, &priv->ah->channels[pos]);
1359 if (ath9k_htc_set_channel(priv, hw, &priv->ah->channels[pos]) < 0) {
1360 ath_print(common, ATH_DBG_FATAL,
1361 "Unable to set channel\n");
1362 mutex_unlock(&priv->mutex);
1363 return -EINVAL;
1367 if (changed & IEEE80211_CONF_CHANGE_PS) {
1368 if (conf->flags & IEEE80211_CONF_PS) {
1369 ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
1370 priv->ps_enabled = true;
1371 } else {
1372 priv->ps_enabled = false;
1373 cancel_work_sync(&priv->ps_work);
1374 ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
1378 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1379 if (conf->flags & IEEE80211_CONF_MONITOR) {
1380 if (ath9k_htc_add_monitor_interface(priv))
1381 ath_print(common, ATH_DBG_FATAL,
1382 "Failed to set monitor mode\n");
1383 else
1384 ath_print(common, ATH_DBG_CONFIG,
1385 "HW opmode set to Monitor mode\n");
1389 if (priv->ps_idle) {
1390 ath_print(common, ATH_DBG_CONFIG,
1391 "idle: disabling radio\n");
1392 ath9k_htc_radio_disable(hw);
1396 mutex_unlock(&priv->mutex);
1398 return 0;
1401 #define SUPPORTED_FILTERS \
1402 (FIF_PROMISC_IN_BSS | \
1403 FIF_ALLMULTI | \
1404 FIF_CONTROL | \
1405 FIF_PSPOLL | \
1406 FIF_OTHER_BSS | \
1407 FIF_BCN_PRBRESP_PROMISC | \
1408 FIF_FCSFAIL)
1410 static void ath9k_htc_configure_filter(struct ieee80211_hw *hw,
1411 unsigned int changed_flags,
1412 unsigned int *total_flags,
1413 u64 multicast)
1415 struct ath9k_htc_priv *priv = hw->priv;
1416 u32 rfilt;
1418 mutex_lock(&priv->mutex);
1420 ath9k_htc_ps_wakeup(priv);
1421 changed_flags &= SUPPORTED_FILTERS;
1422 *total_flags &= SUPPORTED_FILTERS;
1424 priv->rxfilter = *total_flags;
1425 rfilt = ath9k_htc_calcrxfilter(priv);
1426 ath9k_hw_setrxfilter(priv->ah, rfilt);
1428 ath_print(ath9k_hw_common(priv->ah), ATH_DBG_CONFIG,
1429 "Set HW RX filter: 0x%x\n", rfilt);
1431 ath9k_htc_ps_restore(priv);
1432 mutex_unlock(&priv->mutex);
1435 static void ath9k_htc_sta_notify(struct ieee80211_hw *hw,
1436 struct ieee80211_vif *vif,
1437 enum sta_notify_cmd cmd,
1438 struct ieee80211_sta *sta)
1440 struct ath9k_htc_priv *priv = hw->priv;
1441 int ret;
1443 switch (cmd) {
1444 case STA_NOTIFY_ADD:
1445 ret = ath9k_htc_add_station(priv, vif, sta);
1446 if (!ret)
1447 ath9k_htc_init_rate(priv, vif, sta);
1448 break;
1449 case STA_NOTIFY_REMOVE:
1450 ath9k_htc_remove_station(priv, vif, sta);
1451 break;
1452 default:
1453 break;
1457 static int ath9k_htc_conf_tx(struct ieee80211_hw *hw, u16 queue,
1458 const struct ieee80211_tx_queue_params *params)
1460 struct ath9k_htc_priv *priv = hw->priv;
1461 struct ath_common *common = ath9k_hw_common(priv->ah);
1462 struct ath9k_tx_queue_info qi;
1463 int ret = 0, qnum;
1465 if (queue >= WME_NUM_AC)
1466 return 0;
1468 mutex_lock(&priv->mutex);
1470 memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
1472 qi.tqi_aifs = params->aifs;
1473 qi.tqi_cwmin = params->cw_min;
1474 qi.tqi_cwmax = params->cw_max;
1475 qi.tqi_burstTime = params->txop;
1477 qnum = get_hw_qnum(queue, priv->hwq_map);
1479 ath_print(common, ATH_DBG_CONFIG,
1480 "Configure tx [queue/hwq] [%d/%d], "
1481 "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1482 queue, qnum, params->aifs, params->cw_min,
1483 params->cw_max, params->txop);
1485 ret = ath_htc_txq_update(priv, qnum, &qi);
1486 if (ret)
1487 ath_print(common, ATH_DBG_FATAL, "TXQ Update failed\n");
1489 mutex_unlock(&priv->mutex);
1491 return ret;
1494 static int ath9k_htc_set_key(struct ieee80211_hw *hw,
1495 enum set_key_cmd cmd,
1496 struct ieee80211_vif *vif,
1497 struct ieee80211_sta *sta,
1498 struct ieee80211_key_conf *key)
1500 struct ath9k_htc_priv *priv = hw->priv;
1501 struct ath_common *common = ath9k_hw_common(priv->ah);
1502 int ret = 0;
1504 if (htc_modparam_nohwcrypt)
1505 return -ENOSPC;
1507 mutex_lock(&priv->mutex);
1508 ath_print(common, ATH_DBG_CONFIG, "Set HW Key\n");
1509 ath9k_htc_ps_wakeup(priv);
1511 switch (cmd) {
1512 case SET_KEY:
1513 ret = ath9k_cmn_key_config(common, vif, sta, key);
1514 if (ret >= 0) {
1515 key->hw_key_idx = ret;
1516 /* push IV and Michael MIC generation to stack */
1517 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1518 if (key->alg == ALG_TKIP)
1519 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1520 if (priv->ah->sw_mgmt_crypto && key->alg == ALG_CCMP)
1521 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
1522 ret = 0;
1524 break;
1525 case DISABLE_KEY:
1526 ath9k_cmn_key_delete(common, key);
1527 break;
1528 default:
1529 ret = -EINVAL;
1532 ath9k_htc_ps_restore(priv);
1533 mutex_unlock(&priv->mutex);
1535 return ret;
1538 static void ath9k_htc_bss_info_changed(struct ieee80211_hw *hw,
1539 struct ieee80211_vif *vif,
1540 struct ieee80211_bss_conf *bss_conf,
1541 u32 changed)
1543 struct ath9k_htc_priv *priv = hw->priv;
1544 struct ath_hw *ah = priv->ah;
1545 struct ath_common *common = ath9k_hw_common(ah);
1547 mutex_lock(&priv->mutex);
1548 ath9k_htc_ps_wakeup(priv);
1550 if (changed & BSS_CHANGED_ASSOC) {
1551 common->curaid = bss_conf->assoc ?
1552 bss_conf->aid : 0;
1553 ath_print(common, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
1554 bss_conf->assoc);
1556 if (bss_conf->assoc) {
1557 priv->op_flags |= OP_ASSOCIATED;
1558 ath_start_ani(priv);
1559 } else {
1560 priv->op_flags &= ~OP_ASSOCIATED;
1561 cancel_work_sync(&priv->ps_work);
1562 cancel_delayed_work_sync(&priv->ath9k_ani_work);
1566 if (changed & BSS_CHANGED_BSSID) {
1567 /* Set BSSID */
1568 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1569 ath9k_hw_write_associd(ah);
1571 ath_print(common, ATH_DBG_CONFIG,
1572 "BSSID: %pM aid: 0x%x\n",
1573 common->curbssid, common->curaid);
1576 if ((changed & BSS_CHANGED_BEACON_INT) ||
1577 (changed & BSS_CHANGED_BEACON) ||
1578 ((changed & BSS_CHANGED_BEACON_ENABLED) &&
1579 bss_conf->enable_beacon)) {
1580 priv->op_flags |= OP_ENABLE_BEACON;
1581 ath9k_htc_beacon_config(priv, vif);
1584 if ((changed & BSS_CHANGED_BEACON_ENABLED) &&
1585 !bss_conf->enable_beacon) {
1586 priv->op_flags &= ~OP_ENABLE_BEACON;
1587 ath9k_htc_beacon_config(priv, vif);
1590 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1591 ath_print(common, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
1592 bss_conf->use_short_preamble);
1593 if (bss_conf->use_short_preamble)
1594 priv->op_flags |= OP_PREAMBLE_SHORT;
1595 else
1596 priv->op_flags &= ~OP_PREAMBLE_SHORT;
1599 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1600 ath_print(common, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
1601 bss_conf->use_cts_prot);
1602 if (bss_conf->use_cts_prot &&
1603 hw->conf.channel->band != IEEE80211_BAND_5GHZ)
1604 priv->op_flags |= OP_PROTECT_ENABLE;
1605 else
1606 priv->op_flags &= ~OP_PROTECT_ENABLE;
1609 if (changed & BSS_CHANGED_ERP_SLOT) {
1610 if (bss_conf->use_short_slot)
1611 ah->slottime = 9;
1612 else
1613 ah->slottime = 20;
1615 ath9k_hw_init_global_settings(ah);
1618 ath9k_htc_ps_restore(priv);
1619 mutex_unlock(&priv->mutex);
1622 static u64 ath9k_htc_get_tsf(struct ieee80211_hw *hw)
1624 struct ath9k_htc_priv *priv = hw->priv;
1625 u64 tsf;
1627 mutex_lock(&priv->mutex);
1628 tsf = ath9k_hw_gettsf64(priv->ah);
1629 mutex_unlock(&priv->mutex);
1631 return tsf;
1634 static void ath9k_htc_set_tsf(struct ieee80211_hw *hw, u64 tsf)
1636 struct ath9k_htc_priv *priv = hw->priv;
1638 mutex_lock(&priv->mutex);
1639 ath9k_hw_settsf64(priv->ah, tsf);
1640 mutex_unlock(&priv->mutex);
1643 static void ath9k_htc_reset_tsf(struct ieee80211_hw *hw)
1645 struct ath9k_htc_priv *priv = hw->priv;
1647 ath9k_htc_ps_wakeup(priv);
1648 mutex_lock(&priv->mutex);
1649 ath9k_hw_reset_tsf(priv->ah);
1650 mutex_unlock(&priv->mutex);
1651 ath9k_htc_ps_restore(priv);
1654 static int ath9k_htc_ampdu_action(struct ieee80211_hw *hw,
1655 struct ieee80211_vif *vif,
1656 enum ieee80211_ampdu_mlme_action action,
1657 struct ieee80211_sta *sta,
1658 u16 tid, u16 *ssn)
1660 struct ath9k_htc_priv *priv = hw->priv;
1661 struct ath9k_htc_aggr_work *work = &priv->aggr_work;
1662 struct ath9k_htc_sta *ista;
1664 switch (action) {
1665 case IEEE80211_AMPDU_RX_START:
1666 break;
1667 case IEEE80211_AMPDU_RX_STOP:
1668 break;
1669 case IEEE80211_AMPDU_TX_START:
1670 case IEEE80211_AMPDU_TX_STOP:
1671 if (!(priv->op_flags & OP_TXAGGR))
1672 return -ENOTSUPP;
1673 memcpy(work->sta_addr, sta->addr, ETH_ALEN);
1674 work->hw = hw;
1675 work->vif = vif;
1676 work->action = action;
1677 work->tid = tid;
1678 ieee80211_queue_delayed_work(hw, &priv->ath9k_aggr_work, 0);
1679 break;
1680 case IEEE80211_AMPDU_TX_OPERATIONAL:
1681 ista = (struct ath9k_htc_sta *) sta->drv_priv;
1682 ista->tid_state[tid] = AGGR_OPERATIONAL;
1683 break;
1684 default:
1685 ath_print(ath9k_hw_common(priv->ah), ATH_DBG_FATAL,
1686 "Unknown AMPDU action\n");
1689 return 0;
1692 static void ath9k_htc_sw_scan_start(struct ieee80211_hw *hw)
1694 struct ath9k_htc_priv *priv = hw->priv;
1696 mutex_lock(&priv->mutex);
1697 spin_lock_bh(&priv->beacon_lock);
1698 priv->op_flags |= OP_SCANNING;
1699 spin_unlock_bh(&priv->beacon_lock);
1700 cancel_work_sync(&priv->ps_work);
1701 cancel_delayed_work_sync(&priv->ath9k_ani_work);
1702 mutex_unlock(&priv->mutex);
1705 static void ath9k_htc_sw_scan_complete(struct ieee80211_hw *hw)
1707 struct ath9k_htc_priv *priv = hw->priv;
1709 ath9k_htc_ps_wakeup(priv);
1710 mutex_lock(&priv->mutex);
1711 spin_lock_bh(&priv->beacon_lock);
1712 priv->op_flags &= ~OP_SCANNING;
1713 spin_unlock_bh(&priv->beacon_lock);
1714 priv->op_flags |= OP_FULL_RESET;
1715 if (priv->op_flags & OP_ASSOCIATED)
1716 ath9k_htc_beacon_config(priv, priv->vif);
1717 ath_start_ani(priv);
1718 mutex_unlock(&priv->mutex);
1719 ath9k_htc_ps_restore(priv);
1722 static int ath9k_htc_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1724 return 0;
1727 static void ath9k_htc_set_coverage_class(struct ieee80211_hw *hw,
1728 u8 coverage_class)
1730 struct ath9k_htc_priv *priv = hw->priv;
1732 mutex_lock(&priv->mutex);
1733 priv->ah->coverage_class = coverage_class;
1734 ath9k_hw_init_global_settings(priv->ah);
1735 mutex_unlock(&priv->mutex);
1738 struct ieee80211_ops ath9k_htc_ops = {
1739 .tx = ath9k_htc_tx,
1740 .start = ath9k_htc_start,
1741 .stop = ath9k_htc_stop,
1742 .add_interface = ath9k_htc_add_interface,
1743 .remove_interface = ath9k_htc_remove_interface,
1744 .config = ath9k_htc_config,
1745 .configure_filter = ath9k_htc_configure_filter,
1746 .sta_notify = ath9k_htc_sta_notify,
1747 .conf_tx = ath9k_htc_conf_tx,
1748 .bss_info_changed = ath9k_htc_bss_info_changed,
1749 .set_key = ath9k_htc_set_key,
1750 .get_tsf = ath9k_htc_get_tsf,
1751 .set_tsf = ath9k_htc_set_tsf,
1752 .reset_tsf = ath9k_htc_reset_tsf,
1753 .ampdu_action = ath9k_htc_ampdu_action,
1754 .sw_scan_start = ath9k_htc_sw_scan_start,
1755 .sw_scan_complete = ath9k_htc_sw_scan_complete,
1756 .set_rts_threshold = ath9k_htc_set_rts_threshold,
1757 .rfkill_poll = ath9k_htc_rfkill_poll_state,
1758 .set_coverage_class = ath9k_htc_set_coverage_class,