xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/ath/ath9k/tx99.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2013 Qualcomm Atheros, Inc.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Permission to use, copy, modify, and/or distribute this software for any
5*4882a593Smuzhiyun  * purpose with or without fee is hereby granted, provided that the above
6*4882a593Smuzhiyun  * copyright notice and this permission notice appear in all copies.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9*4882a593Smuzhiyun  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10*4882a593Smuzhiyun  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11*4882a593Smuzhiyun  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12*4882a593Smuzhiyun  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13*4882a593Smuzhiyun  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14*4882a593Smuzhiyun  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include "ath9k.h"
18*4882a593Smuzhiyun 
ath9k_tx99_stop(struct ath_softc * sc)19*4882a593Smuzhiyun static void ath9k_tx99_stop(struct ath_softc *sc)
20*4882a593Smuzhiyun {
21*4882a593Smuzhiyun 	struct ath_hw *ah = sc->sc_ah;
22*4882a593Smuzhiyun 	struct ath_common *common = ath9k_hw_common(ah);
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun 	ath_drain_all_txq(sc);
25*4882a593Smuzhiyun 	ath_startrecv(sc);
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun 	ath9k_hw_set_interrupts(ah);
28*4882a593Smuzhiyun 	ath9k_hw_enable_interrupts(ah);
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun 	ieee80211_wake_queues(sc->hw);
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	kfree_skb(sc->tx99_skb);
33*4882a593Smuzhiyun 	sc->tx99_skb = NULL;
34*4882a593Smuzhiyun 	sc->tx99_state = false;
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun 	ath9k_hw_tx99_stop(sc->sc_ah);
37*4882a593Smuzhiyun 	ath_dbg(common, XMIT, "TX99 stopped\n");
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun 
ath9k_build_tx99_skb(struct ath_softc * sc)40*4882a593Smuzhiyun static struct sk_buff *ath9k_build_tx99_skb(struct ath_softc *sc)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	static u8 PN9Data[] = {0xff, 0x87, 0xb8, 0x59, 0xb7, 0xa1, 0xcc, 0x24,
43*4882a593Smuzhiyun 			       0x57, 0x5e, 0x4b, 0x9c, 0x0e, 0xe9, 0xea, 0x50,
44*4882a593Smuzhiyun 			       0x2a, 0xbe, 0xb4, 0x1b, 0xb6, 0xb0, 0x5d, 0xf1,
45*4882a593Smuzhiyun 			       0xe6, 0x9a, 0xe3, 0x45, 0xfd, 0x2c, 0x53, 0x18,
46*4882a593Smuzhiyun 			       0x0c, 0xca, 0xc9, 0xfb, 0x49, 0x37, 0xe5, 0xa8,
47*4882a593Smuzhiyun 			       0x51, 0x3b, 0x2f, 0x61, 0xaa, 0x72, 0x18, 0x84,
48*4882a593Smuzhiyun 			       0x02, 0x23, 0x23, 0xab, 0x63, 0x89, 0x51, 0xb3,
49*4882a593Smuzhiyun 			       0xe7, 0x8b, 0x72, 0x90, 0x4c, 0xe8, 0xfb, 0xc0};
50*4882a593Smuzhiyun 	u32 len = 1200;
51*4882a593Smuzhiyun 	struct ieee80211_tx_rate *rate;
52*4882a593Smuzhiyun 	struct ieee80211_hw *hw = sc->hw;
53*4882a593Smuzhiyun 	struct ath_hw *ah = sc->sc_ah;
54*4882a593Smuzhiyun 	struct ieee80211_hdr *hdr;
55*4882a593Smuzhiyun 	struct ieee80211_tx_info *tx_info;
56*4882a593Smuzhiyun 	struct sk_buff *skb;
57*4882a593Smuzhiyun 	struct ath_vif *avp;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	skb = alloc_skb(len, GFP_KERNEL);
60*4882a593Smuzhiyun 	if (!skb)
61*4882a593Smuzhiyun 		return NULL;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	skb_put(skb, len);
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	memset(skb->data, 0, len);
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	hdr = (struct ieee80211_hdr *)skb->data;
68*4882a593Smuzhiyun 	hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA);
69*4882a593Smuzhiyun 	hdr->duration_id = 0;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	memcpy(hdr->addr1, hw->wiphy->perm_addr, ETH_ALEN);
72*4882a593Smuzhiyun 	memcpy(hdr->addr2, hw->wiphy->perm_addr, ETH_ALEN);
73*4882a593Smuzhiyun 	memcpy(hdr->addr3, hw->wiphy->perm_addr, ETH_ALEN);
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	if (sc->tx99_vif) {
76*4882a593Smuzhiyun 		avp = (struct ath_vif *) sc->tx99_vif->drv_priv;
77*4882a593Smuzhiyun 		hdr->seq_ctrl |= cpu_to_le16(avp->seq_no);
78*4882a593Smuzhiyun 	}
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	tx_info = IEEE80211_SKB_CB(skb);
81*4882a593Smuzhiyun 	memset(tx_info, 0, sizeof(*tx_info));
82*4882a593Smuzhiyun 	rate = &tx_info->control.rates[0];
83*4882a593Smuzhiyun 	tx_info->band = sc->cur_chan->chandef.chan->band;
84*4882a593Smuzhiyun 	tx_info->flags = IEEE80211_TX_CTL_NO_ACK;
85*4882a593Smuzhiyun 	tx_info->control.vif = sc->tx99_vif;
86*4882a593Smuzhiyun 	rate->count = 1;
87*4882a593Smuzhiyun 	if (ah->curchan && IS_CHAN_HT(ah->curchan)) {
88*4882a593Smuzhiyun 		rate->flags |= IEEE80211_TX_RC_MCS;
89*4882a593Smuzhiyun 		if (IS_CHAN_HT40(ah->curchan))
90*4882a593Smuzhiyun 			rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
91*4882a593Smuzhiyun 	}
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	memcpy(skb->data + sizeof(*hdr), PN9Data, sizeof(PN9Data));
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	return skb;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun 
ath9k_tx99_deinit(struct ath_softc * sc)98*4882a593Smuzhiyun static void ath9k_tx99_deinit(struct ath_softc *sc)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun 	ath_reset(sc, NULL);
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	ath9k_ps_wakeup(sc);
103*4882a593Smuzhiyun 	ath9k_tx99_stop(sc);
104*4882a593Smuzhiyun 	ath9k_ps_restore(sc);
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun 
ath9k_tx99_init(struct ath_softc * sc)107*4882a593Smuzhiyun static int ath9k_tx99_init(struct ath_softc *sc)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun 	struct ieee80211_hw *hw = sc->hw;
110*4882a593Smuzhiyun 	struct ath_hw *ah = sc->sc_ah;
111*4882a593Smuzhiyun 	struct ath_common *common = ath9k_hw_common(ah);
112*4882a593Smuzhiyun 	struct ath_tx_control txctl;
113*4882a593Smuzhiyun 	int r;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
116*4882a593Smuzhiyun 		ath_err(common,
117*4882a593Smuzhiyun 			"driver is in invalid state unable to use TX99");
118*4882a593Smuzhiyun 		return -EINVAL;
119*4882a593Smuzhiyun 	}
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	sc->tx99_skb = ath9k_build_tx99_skb(sc);
122*4882a593Smuzhiyun 	if (!sc->tx99_skb)
123*4882a593Smuzhiyun 		return -ENOMEM;
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	memset(&txctl, 0, sizeof(txctl));
126*4882a593Smuzhiyun 	txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	ath_reset(sc, NULL);
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	ath9k_ps_wakeup(sc);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	ath9k_hw_disable_interrupts(ah);
133*4882a593Smuzhiyun 	ath_drain_all_txq(sc);
134*4882a593Smuzhiyun 	ath_stoprecv(sc);
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	sc->tx99_state = true;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	ieee80211_stop_queues(hw);
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	if (sc->tx99_power == MAX_RATE_POWER + 1)
141*4882a593Smuzhiyun 		sc->tx99_power = MAX_RATE_POWER;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	ath9k_hw_tx99_set_txpower(ah, sc->tx99_power);
144*4882a593Smuzhiyun 	r = ath9k_tx99_send(sc, sc->tx99_skb, &txctl);
145*4882a593Smuzhiyun 	if (r) {
146*4882a593Smuzhiyun 		ath_dbg(common, XMIT, "Failed to xmit TX99 skb\n");
147*4882a593Smuzhiyun 		return r;
148*4882a593Smuzhiyun 	}
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	ath_dbg(common, XMIT, "TX99 xmit started using %d ( %ddBm)\n",
151*4882a593Smuzhiyun 		sc->tx99_power,
152*4882a593Smuzhiyun 		sc->tx99_power / 2);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	/* We leave the hardware awake as it will be chugging on */
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	return 0;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun 
read_file_tx99(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)159*4882a593Smuzhiyun static ssize_t read_file_tx99(struct file *file, char __user *user_buf,
160*4882a593Smuzhiyun 			      size_t count, loff_t *ppos)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun 	struct ath_softc *sc = file->private_data;
163*4882a593Smuzhiyun 	char buf[3];
164*4882a593Smuzhiyun 	unsigned int len;
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	len = sprintf(buf, "%d\n", sc->tx99_state);
167*4882a593Smuzhiyun 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun 
write_file_tx99(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)170*4882a593Smuzhiyun static ssize_t write_file_tx99(struct file *file, const char __user *user_buf,
171*4882a593Smuzhiyun 			       size_t count, loff_t *ppos)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun 	struct ath_softc *sc = file->private_data;
174*4882a593Smuzhiyun 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
175*4882a593Smuzhiyun 	char buf[32];
176*4882a593Smuzhiyun 	bool start;
177*4882a593Smuzhiyun 	ssize_t len;
178*4882a593Smuzhiyun 	int r;
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	if (count < 1)
181*4882a593Smuzhiyun 		return -EINVAL;
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	if (sc->cur_chan->nvifs > 1)
184*4882a593Smuzhiyun 		return -EOPNOTSUPP;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	len = min(count, sizeof(buf) - 1);
187*4882a593Smuzhiyun 	if (copy_from_user(buf, user_buf, len))
188*4882a593Smuzhiyun 		return -EFAULT;
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	buf[len] = '\0';
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	if (strtobool(buf, &start))
193*4882a593Smuzhiyun 		return -EINVAL;
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	mutex_lock(&sc->mutex);
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	if (start == sc->tx99_state) {
198*4882a593Smuzhiyun 		if (!start)
199*4882a593Smuzhiyun 			goto out;
200*4882a593Smuzhiyun 		ath_dbg(common, XMIT, "Resetting TX99\n");
201*4882a593Smuzhiyun 		ath9k_tx99_deinit(sc);
202*4882a593Smuzhiyun 	}
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	if (!start) {
205*4882a593Smuzhiyun 		ath9k_tx99_deinit(sc);
206*4882a593Smuzhiyun 		goto out;
207*4882a593Smuzhiyun 	}
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	r = ath9k_tx99_init(sc);
210*4882a593Smuzhiyun 	if (r) {
211*4882a593Smuzhiyun 		mutex_unlock(&sc->mutex);
212*4882a593Smuzhiyun 		return r;
213*4882a593Smuzhiyun 	}
214*4882a593Smuzhiyun out:
215*4882a593Smuzhiyun 	mutex_unlock(&sc->mutex);
216*4882a593Smuzhiyun 	return count;
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun static const struct file_operations fops_tx99 = {
220*4882a593Smuzhiyun 	.read = read_file_tx99,
221*4882a593Smuzhiyun 	.write = write_file_tx99,
222*4882a593Smuzhiyun 	.open = simple_open,
223*4882a593Smuzhiyun 	.owner = THIS_MODULE,
224*4882a593Smuzhiyun 	.llseek = default_llseek,
225*4882a593Smuzhiyun };
226*4882a593Smuzhiyun 
read_file_tx99_power(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)227*4882a593Smuzhiyun static ssize_t read_file_tx99_power(struct file *file,
228*4882a593Smuzhiyun 				    char __user *user_buf,
229*4882a593Smuzhiyun 				    size_t count, loff_t *ppos)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun 	struct ath_softc *sc = file->private_data;
232*4882a593Smuzhiyun 	char buf[32];
233*4882a593Smuzhiyun 	unsigned int len;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	len = sprintf(buf, "%d (%d dBm)\n",
236*4882a593Smuzhiyun 		      sc->tx99_power,
237*4882a593Smuzhiyun 		      sc->tx99_power / 2);
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun 
write_file_tx99_power(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)242*4882a593Smuzhiyun static ssize_t write_file_tx99_power(struct file *file,
243*4882a593Smuzhiyun 				     const char __user *user_buf,
244*4882a593Smuzhiyun 				     size_t count, loff_t *ppos)
245*4882a593Smuzhiyun {
246*4882a593Smuzhiyun 	struct ath_softc *sc = file->private_data;
247*4882a593Smuzhiyun 	int r;
248*4882a593Smuzhiyun 	u8 tx_power;
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	r = kstrtou8_from_user(user_buf, count, 0, &tx_power);
251*4882a593Smuzhiyun 	if (r)
252*4882a593Smuzhiyun 		return r;
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	if (tx_power > MAX_RATE_POWER)
255*4882a593Smuzhiyun 		return -EINVAL;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	sc->tx99_power = tx_power;
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	ath9k_ps_wakeup(sc);
260*4882a593Smuzhiyun 	ath9k_hw_tx99_set_txpower(sc->sc_ah, sc->tx99_power);
261*4882a593Smuzhiyun 	ath9k_ps_restore(sc);
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	return count;
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun static const struct file_operations fops_tx99_power = {
267*4882a593Smuzhiyun 	.read = read_file_tx99_power,
268*4882a593Smuzhiyun 	.write = write_file_tx99_power,
269*4882a593Smuzhiyun 	.open = simple_open,
270*4882a593Smuzhiyun 	.owner = THIS_MODULE,
271*4882a593Smuzhiyun 	.llseek = default_llseek,
272*4882a593Smuzhiyun };
273*4882a593Smuzhiyun 
ath9k_tx99_init_debug(struct ath_softc * sc)274*4882a593Smuzhiyun void ath9k_tx99_init_debug(struct ath_softc *sc)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun 	if (!AR_SREV_9280_20_OR_LATER(sc->sc_ah))
277*4882a593Smuzhiyun 		return;
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	debugfs_create_file("tx99", 0600,
280*4882a593Smuzhiyun 			    sc->debug.debugfs_phy, sc,
281*4882a593Smuzhiyun 			    &fops_tx99);
282*4882a593Smuzhiyun 	debugfs_create_file("tx99_power", 0600,
283*4882a593Smuzhiyun 			    sc->debug.debugfs_phy, sc,
284*4882a593Smuzhiyun 			    &fops_tx99_power);
285*4882a593Smuzhiyun }
286