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