Merge commit 'v3.17' into next
[cascardo/linux.git] / drivers / net / wireless / ath / ath9k / tx99.c
1 /*
2  * Copyright (c) 2013 Qualcomm Atheros, 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 "ath9k.h"
18
19 static void ath9k_tx99_stop(struct ath_softc *sc)
20 {
21         struct ath_hw *ah = sc->sc_ah;
22         struct ath_common *common = ath9k_hw_common(ah);
23
24         ath_drain_all_txq(sc);
25         ath_startrecv(sc);
26
27         ath9k_hw_set_interrupts(ah);
28         ath9k_hw_enable_interrupts(ah);
29
30         ieee80211_wake_queues(sc->hw);
31
32         kfree_skb(sc->tx99_skb);
33         sc->tx99_skb = NULL;
34         sc->tx99_state = false;
35
36         ath9k_hw_tx99_stop(sc->sc_ah);
37         ath_dbg(common, XMIT, "TX99 stopped\n");
38 }
39
40 static struct sk_buff *ath9k_build_tx99_skb(struct ath_softc *sc)
41 {
42         static u8 PN9Data[] = {0xff, 0x87, 0xb8, 0x59, 0xb7, 0xa1, 0xcc, 0x24,
43                                0x57, 0x5e, 0x4b, 0x9c, 0x0e, 0xe9, 0xea, 0x50,
44                                0x2a, 0xbe, 0xb4, 0x1b, 0xb6, 0xb0, 0x5d, 0xf1,
45                                0xe6, 0x9a, 0xe3, 0x45, 0xfd, 0x2c, 0x53, 0x18,
46                                0x0c, 0xca, 0xc9, 0xfb, 0x49, 0x37, 0xe5, 0xa8,
47                                0x51, 0x3b, 0x2f, 0x61, 0xaa, 0x72, 0x18, 0x84,
48                                0x02, 0x23, 0x23, 0xab, 0x63, 0x89, 0x51, 0xb3,
49                                0xe7, 0x8b, 0x72, 0x90, 0x4c, 0xe8, 0xfb, 0xc0};
50         u32 len = 1200;
51         struct ieee80211_tx_rate *rate;
52         struct ieee80211_hw *hw = sc->hw;
53         struct ath_hw *ah = sc->sc_ah;
54         struct ieee80211_hdr *hdr;
55         struct ieee80211_tx_info *tx_info;
56         struct sk_buff *skb;
57
58         skb = alloc_skb(len, GFP_KERNEL);
59         if (!skb)
60                 return NULL;
61
62         skb_put(skb, len);
63
64         memset(skb->data, 0, len);
65
66         hdr = (struct ieee80211_hdr *)skb->data;
67         hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA);
68         hdr->duration_id = 0;
69
70         memcpy(hdr->addr1, hw->wiphy->perm_addr, ETH_ALEN);
71         memcpy(hdr->addr2, hw->wiphy->perm_addr, ETH_ALEN);
72         memcpy(hdr->addr3, hw->wiphy->perm_addr, ETH_ALEN);
73
74         hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
75
76         tx_info = IEEE80211_SKB_CB(skb);
77         memset(tx_info, 0, sizeof(*tx_info));
78         rate = &tx_info->control.rates[0];
79         tx_info->band = sc->cur_chan->chandef.chan->band;
80         tx_info->flags = IEEE80211_TX_CTL_NO_ACK;
81         tx_info->control.vif = sc->tx99_vif;
82         rate->count = 1;
83         if (ah->curchan && IS_CHAN_HT(ah->curchan)) {
84                 rate->flags |= IEEE80211_TX_RC_MCS;
85                 if (IS_CHAN_HT40(ah->curchan))
86                         rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
87         }
88
89         memcpy(skb->data + sizeof(*hdr), PN9Data, sizeof(PN9Data));
90
91         return skb;
92 }
93
94 static void ath9k_tx99_deinit(struct ath_softc *sc)
95 {
96         ath_reset(sc);
97
98         ath9k_ps_wakeup(sc);
99         ath9k_tx99_stop(sc);
100         ath9k_ps_restore(sc);
101 }
102
103 static int ath9k_tx99_init(struct ath_softc *sc)
104 {
105         struct ieee80211_hw *hw = sc->hw;
106         struct ath_hw *ah = sc->sc_ah;
107         struct ath_common *common = ath9k_hw_common(ah);
108         struct ath_tx_control txctl;
109         int r;
110
111         if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
112                 ath_err(common,
113                         "driver is in invalid state unable to use TX99");
114                 return -EINVAL;
115         }
116
117         sc->tx99_skb = ath9k_build_tx99_skb(sc);
118         if (!sc->tx99_skb)
119                 return -ENOMEM;
120
121         memset(&txctl, 0, sizeof(txctl));
122         txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
123
124         ath_reset(sc);
125
126         ath9k_ps_wakeup(sc);
127
128         ath9k_hw_disable_interrupts(ah);
129         atomic_set(&ah->intr_ref_cnt, -1);
130         ath_drain_all_txq(sc);
131         ath_stoprecv(sc);
132
133         sc->tx99_state = true;
134
135         ieee80211_stop_queues(hw);
136
137         if (sc->tx99_power == MAX_RATE_POWER + 1)
138                 sc->tx99_power = MAX_RATE_POWER;
139
140         ath9k_hw_tx99_set_txpower(ah, sc->tx99_power);
141         r = ath9k_tx99_send(sc, sc->tx99_skb, &txctl);
142         if (r) {
143                 ath_dbg(common, XMIT, "Failed to xmit TX99 skb\n");
144                 return r;
145         }
146
147         ath_dbg(common, XMIT, "TX99 xmit started using %d ( %ddBm)\n",
148                 sc->tx99_power,
149                 sc->tx99_power / 2);
150
151         /* We leave the harware awake as it will be chugging on */
152
153         return 0;
154 }
155
156 static ssize_t read_file_tx99(struct file *file, char __user *user_buf,
157                               size_t count, loff_t *ppos)
158 {
159         struct ath_softc *sc = file->private_data;
160         char buf[3];
161         unsigned int len;
162
163         len = sprintf(buf, "%d\n", sc->tx99_state);
164         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
165 }
166
167 static ssize_t write_file_tx99(struct file *file, const char __user *user_buf,
168                                size_t count, loff_t *ppos)
169 {
170         struct ath_softc *sc = file->private_data;
171         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
172         char buf[32];
173         bool start;
174         ssize_t len;
175         int r;
176
177         if (sc->nvifs > 1)
178                 return -EOPNOTSUPP;
179
180         len = min(count, sizeof(buf) - 1);
181         if (copy_from_user(buf, user_buf, len))
182                 return -EFAULT;
183
184         if (strtobool(buf, &start))
185                 return -EINVAL;
186
187         if (start == sc->tx99_state) {
188                 if (!start)
189                         return count;
190                 ath_dbg(common, XMIT, "Resetting TX99\n");
191                 ath9k_tx99_deinit(sc);
192         }
193
194         if (!start) {
195                 ath9k_tx99_deinit(sc);
196                 return count;
197         }
198
199         r = ath9k_tx99_init(sc);
200         if (r)
201                 return r;
202
203         return count;
204 }
205
206 static const struct file_operations fops_tx99 = {
207         .read = read_file_tx99,
208         .write = write_file_tx99,
209         .open = simple_open,
210         .owner = THIS_MODULE,
211         .llseek = default_llseek,
212 };
213
214 static ssize_t read_file_tx99_power(struct file *file,
215                                     char __user *user_buf,
216                                     size_t count, loff_t *ppos)
217 {
218         struct ath_softc *sc = file->private_data;
219         char buf[32];
220         unsigned int len;
221
222         len = sprintf(buf, "%d (%d dBm)\n",
223                       sc->tx99_power,
224                       sc->tx99_power / 2);
225
226         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
227 }
228
229 static ssize_t write_file_tx99_power(struct file *file,
230                                      const char __user *user_buf,
231                                      size_t count, loff_t *ppos)
232 {
233         struct ath_softc *sc = file->private_data;
234         int r;
235         u8 tx_power;
236
237         r = kstrtou8_from_user(user_buf, count, 0, &tx_power);
238         if (r)
239                 return r;
240
241         if (tx_power > MAX_RATE_POWER)
242                 return -EINVAL;
243
244         sc->tx99_power = tx_power;
245
246         ath9k_ps_wakeup(sc);
247         ath9k_hw_tx99_set_txpower(sc->sc_ah, sc->tx99_power);
248         ath9k_ps_restore(sc);
249
250         return count;
251 }
252
253 static const struct file_operations fops_tx99_power = {
254         .read = read_file_tx99_power,
255         .write = write_file_tx99_power,
256         .open = simple_open,
257         .owner = THIS_MODULE,
258         .llseek = default_llseek,
259 };
260
261 void ath9k_tx99_init_debug(struct ath_softc *sc)
262 {
263         if (!AR_SREV_9300_20_OR_LATER(sc->sc_ah))
264                 return;
265
266         debugfs_create_file("tx99", S_IRUSR | S_IWUSR,
267                             sc->debug.debugfs_phy, sc,
268                             &fops_tx99);
269         debugfs_create_file("tx99_power", S_IRUSR | S_IWUSR,
270                             sc->debug.debugfs_phy, sc,
271                             &fops_tx99_power);
272 }