18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2013 Qualcomm Atheros, Inc.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission to use, copy, modify, and/or distribute this software for any
58c2ecf20Sopenharmony_ci * purpose with or without fee is hereby granted, provided that the above
68c2ecf20Sopenharmony_ci * copyright notice and this permission notice appear in all copies.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
98c2ecf20Sopenharmony_ci * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
108c2ecf20Sopenharmony_ci * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
118c2ecf20Sopenharmony_ci * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
128c2ecf20Sopenharmony_ci * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
138c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
148c2ecf20Sopenharmony_ci * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "ath9k.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic void ath9k_tx99_stop(struct ath_softc *sc)
208c2ecf20Sopenharmony_ci{
218c2ecf20Sopenharmony_ci	struct ath_hw *ah = sc->sc_ah;
228c2ecf20Sopenharmony_ci	struct ath_common *common = ath9k_hw_common(ah);
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	ath_drain_all_txq(sc);
258c2ecf20Sopenharmony_ci	ath_startrecv(sc);
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	ath9k_hw_set_interrupts(ah);
288c2ecf20Sopenharmony_ci	ath9k_hw_enable_interrupts(ah);
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	ieee80211_wake_queues(sc->hw);
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	kfree_skb(sc->tx99_skb);
338c2ecf20Sopenharmony_ci	sc->tx99_skb = NULL;
348c2ecf20Sopenharmony_ci	sc->tx99_state = false;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	ath9k_hw_tx99_stop(sc->sc_ah);
378c2ecf20Sopenharmony_ci	ath_dbg(common, XMIT, "TX99 stopped\n");
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic struct sk_buff *ath9k_build_tx99_skb(struct ath_softc *sc)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	static u8 PN9Data[] = {0xff, 0x87, 0xb8, 0x59, 0xb7, 0xa1, 0xcc, 0x24,
438c2ecf20Sopenharmony_ci			       0x57, 0x5e, 0x4b, 0x9c, 0x0e, 0xe9, 0xea, 0x50,
448c2ecf20Sopenharmony_ci			       0x2a, 0xbe, 0xb4, 0x1b, 0xb6, 0xb0, 0x5d, 0xf1,
458c2ecf20Sopenharmony_ci			       0xe6, 0x9a, 0xe3, 0x45, 0xfd, 0x2c, 0x53, 0x18,
468c2ecf20Sopenharmony_ci			       0x0c, 0xca, 0xc9, 0xfb, 0x49, 0x37, 0xe5, 0xa8,
478c2ecf20Sopenharmony_ci			       0x51, 0x3b, 0x2f, 0x61, 0xaa, 0x72, 0x18, 0x84,
488c2ecf20Sopenharmony_ci			       0x02, 0x23, 0x23, 0xab, 0x63, 0x89, 0x51, 0xb3,
498c2ecf20Sopenharmony_ci			       0xe7, 0x8b, 0x72, 0x90, 0x4c, 0xe8, 0xfb, 0xc0};
508c2ecf20Sopenharmony_ci	u32 len = 1200;
518c2ecf20Sopenharmony_ci	struct ieee80211_tx_rate *rate;
528c2ecf20Sopenharmony_ci	struct ieee80211_hw *hw = sc->hw;
538c2ecf20Sopenharmony_ci	struct ath_hw *ah = sc->sc_ah;
548c2ecf20Sopenharmony_ci	struct ieee80211_hdr *hdr;
558c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *tx_info;
568c2ecf20Sopenharmony_ci	struct sk_buff *skb;
578c2ecf20Sopenharmony_ci	struct ath_vif *avp;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	skb = alloc_skb(len, GFP_KERNEL);
608c2ecf20Sopenharmony_ci	if (!skb)
618c2ecf20Sopenharmony_ci		return NULL;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	skb_put(skb, len);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	memset(skb->data, 0, len);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	hdr = (struct ieee80211_hdr *)skb->data;
688c2ecf20Sopenharmony_ci	hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA);
698c2ecf20Sopenharmony_ci	hdr->duration_id = 0;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	memcpy(hdr->addr1, hw->wiphy->perm_addr, ETH_ALEN);
728c2ecf20Sopenharmony_ci	memcpy(hdr->addr2, hw->wiphy->perm_addr, ETH_ALEN);
738c2ecf20Sopenharmony_ci	memcpy(hdr->addr3, hw->wiphy->perm_addr, ETH_ALEN);
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	if (sc->tx99_vif) {
768c2ecf20Sopenharmony_ci		avp = (struct ath_vif *) sc->tx99_vif->drv_priv;
778c2ecf20Sopenharmony_ci		hdr->seq_ctrl |= cpu_to_le16(avp->seq_no);
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	tx_info = IEEE80211_SKB_CB(skb);
818c2ecf20Sopenharmony_ci	memset(tx_info, 0, sizeof(*tx_info));
828c2ecf20Sopenharmony_ci	rate = &tx_info->control.rates[0];
838c2ecf20Sopenharmony_ci	tx_info->band = sc->cur_chan->chandef.chan->band;
848c2ecf20Sopenharmony_ci	tx_info->flags = IEEE80211_TX_CTL_NO_ACK;
858c2ecf20Sopenharmony_ci	tx_info->control.vif = sc->tx99_vif;
868c2ecf20Sopenharmony_ci	rate->count = 1;
878c2ecf20Sopenharmony_ci	if (ah->curchan && IS_CHAN_HT(ah->curchan)) {
888c2ecf20Sopenharmony_ci		rate->flags |= IEEE80211_TX_RC_MCS;
898c2ecf20Sopenharmony_ci		if (IS_CHAN_HT40(ah->curchan))
908c2ecf20Sopenharmony_ci			rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	memcpy(skb->data + sizeof(*hdr), PN9Data, sizeof(PN9Data));
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	return skb;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic void ath9k_tx99_deinit(struct ath_softc *sc)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	ath_reset(sc, NULL);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	ath9k_ps_wakeup(sc);
1038c2ecf20Sopenharmony_ci	ath9k_tx99_stop(sc);
1048c2ecf20Sopenharmony_ci	ath9k_ps_restore(sc);
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic int ath9k_tx99_init(struct ath_softc *sc)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	struct ieee80211_hw *hw = sc->hw;
1108c2ecf20Sopenharmony_ci	struct ath_hw *ah = sc->sc_ah;
1118c2ecf20Sopenharmony_ci	struct ath_common *common = ath9k_hw_common(ah);
1128c2ecf20Sopenharmony_ci	struct ath_tx_control txctl;
1138c2ecf20Sopenharmony_ci	int r;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
1168c2ecf20Sopenharmony_ci		ath_err(common,
1178c2ecf20Sopenharmony_ci			"driver is in invalid state unable to use TX99");
1188c2ecf20Sopenharmony_ci		return -EINVAL;
1198c2ecf20Sopenharmony_ci	}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	sc->tx99_skb = ath9k_build_tx99_skb(sc);
1228c2ecf20Sopenharmony_ci	if (!sc->tx99_skb)
1238c2ecf20Sopenharmony_ci		return -ENOMEM;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	memset(&txctl, 0, sizeof(txctl));
1268c2ecf20Sopenharmony_ci	txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	ath_reset(sc, NULL);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	ath9k_ps_wakeup(sc);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	ath9k_hw_disable_interrupts(ah);
1338c2ecf20Sopenharmony_ci	ath_drain_all_txq(sc);
1348c2ecf20Sopenharmony_ci	ath_stoprecv(sc);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	sc->tx99_state = true;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	ieee80211_stop_queues(hw);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	if (sc->tx99_power == MAX_RATE_POWER + 1)
1418c2ecf20Sopenharmony_ci		sc->tx99_power = MAX_RATE_POWER;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	ath9k_hw_tx99_set_txpower(ah, sc->tx99_power);
1448c2ecf20Sopenharmony_ci	r = ath9k_tx99_send(sc, sc->tx99_skb, &txctl);
1458c2ecf20Sopenharmony_ci	if (r) {
1468c2ecf20Sopenharmony_ci		ath_dbg(common, XMIT, "Failed to xmit TX99 skb\n");
1478c2ecf20Sopenharmony_ci		return r;
1488c2ecf20Sopenharmony_ci	}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	ath_dbg(common, XMIT, "TX99 xmit started using %d ( %ddBm)\n",
1518c2ecf20Sopenharmony_ci		sc->tx99_power,
1528c2ecf20Sopenharmony_ci		sc->tx99_power / 2);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	/* We leave the hardware awake as it will be chugging on */
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	return 0;
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic ssize_t read_file_tx99(struct file *file, char __user *user_buf,
1608c2ecf20Sopenharmony_ci			      size_t count, loff_t *ppos)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	struct ath_softc *sc = file->private_data;
1638c2ecf20Sopenharmony_ci	char buf[3];
1648c2ecf20Sopenharmony_ci	unsigned int len;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	len = sprintf(buf, "%d\n", sc->tx99_state);
1678c2ecf20Sopenharmony_ci	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic ssize_t write_file_tx99(struct file *file, const char __user *user_buf,
1718c2ecf20Sopenharmony_ci			       size_t count, loff_t *ppos)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	struct ath_softc *sc = file->private_data;
1748c2ecf20Sopenharmony_ci	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1758c2ecf20Sopenharmony_ci	char buf[32];
1768c2ecf20Sopenharmony_ci	bool start;
1778c2ecf20Sopenharmony_ci	ssize_t len;
1788c2ecf20Sopenharmony_ci	int r;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	if (count < 1)
1818c2ecf20Sopenharmony_ci		return -EINVAL;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	if (sc->cur_chan->nvifs > 1)
1848c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	len = min(count, sizeof(buf) - 1);
1878c2ecf20Sopenharmony_ci	if (copy_from_user(buf, user_buf, len))
1888c2ecf20Sopenharmony_ci		return -EFAULT;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	buf[len] = '\0';
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	if (strtobool(buf, &start))
1938c2ecf20Sopenharmony_ci		return -EINVAL;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	mutex_lock(&sc->mutex);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	if (start == sc->tx99_state) {
1988c2ecf20Sopenharmony_ci		if (!start)
1998c2ecf20Sopenharmony_ci			goto out;
2008c2ecf20Sopenharmony_ci		ath_dbg(common, XMIT, "Resetting TX99\n");
2018c2ecf20Sopenharmony_ci		ath9k_tx99_deinit(sc);
2028c2ecf20Sopenharmony_ci	}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	if (!start) {
2058c2ecf20Sopenharmony_ci		ath9k_tx99_deinit(sc);
2068c2ecf20Sopenharmony_ci		goto out;
2078c2ecf20Sopenharmony_ci	}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	r = ath9k_tx99_init(sc);
2108c2ecf20Sopenharmony_ci	if (r) {
2118c2ecf20Sopenharmony_ci		mutex_unlock(&sc->mutex);
2128c2ecf20Sopenharmony_ci		return r;
2138c2ecf20Sopenharmony_ci	}
2148c2ecf20Sopenharmony_ciout:
2158c2ecf20Sopenharmony_ci	mutex_unlock(&sc->mutex);
2168c2ecf20Sopenharmony_ci	return count;
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic const struct file_operations fops_tx99 = {
2208c2ecf20Sopenharmony_ci	.read = read_file_tx99,
2218c2ecf20Sopenharmony_ci	.write = write_file_tx99,
2228c2ecf20Sopenharmony_ci	.open = simple_open,
2238c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
2248c2ecf20Sopenharmony_ci	.llseek = default_llseek,
2258c2ecf20Sopenharmony_ci};
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_cistatic ssize_t read_file_tx99_power(struct file *file,
2288c2ecf20Sopenharmony_ci				    char __user *user_buf,
2298c2ecf20Sopenharmony_ci				    size_t count, loff_t *ppos)
2308c2ecf20Sopenharmony_ci{
2318c2ecf20Sopenharmony_ci	struct ath_softc *sc = file->private_data;
2328c2ecf20Sopenharmony_ci	char buf[32];
2338c2ecf20Sopenharmony_ci	unsigned int len;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	len = sprintf(buf, "%d (%d dBm)\n",
2368c2ecf20Sopenharmony_ci		      sc->tx99_power,
2378c2ecf20Sopenharmony_ci		      sc->tx99_power / 2);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
2408c2ecf20Sopenharmony_ci}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_cistatic ssize_t write_file_tx99_power(struct file *file,
2438c2ecf20Sopenharmony_ci				     const char __user *user_buf,
2448c2ecf20Sopenharmony_ci				     size_t count, loff_t *ppos)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	struct ath_softc *sc = file->private_data;
2478c2ecf20Sopenharmony_ci	int r;
2488c2ecf20Sopenharmony_ci	u8 tx_power;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	r = kstrtou8_from_user(user_buf, count, 0, &tx_power);
2518c2ecf20Sopenharmony_ci	if (r)
2528c2ecf20Sopenharmony_ci		return r;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	if (tx_power > MAX_RATE_POWER)
2558c2ecf20Sopenharmony_ci		return -EINVAL;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	sc->tx99_power = tx_power;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	ath9k_ps_wakeup(sc);
2608c2ecf20Sopenharmony_ci	ath9k_hw_tx99_set_txpower(sc->sc_ah, sc->tx99_power);
2618c2ecf20Sopenharmony_ci	ath9k_ps_restore(sc);
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	return count;
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic const struct file_operations fops_tx99_power = {
2678c2ecf20Sopenharmony_ci	.read = read_file_tx99_power,
2688c2ecf20Sopenharmony_ci	.write = write_file_tx99_power,
2698c2ecf20Sopenharmony_ci	.open = simple_open,
2708c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
2718c2ecf20Sopenharmony_ci	.llseek = default_llseek,
2728c2ecf20Sopenharmony_ci};
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_civoid ath9k_tx99_init_debug(struct ath_softc *sc)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	if (!AR_SREV_9280_20_OR_LATER(sc->sc_ah))
2778c2ecf20Sopenharmony_ci		return;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	debugfs_create_file("tx99", 0600,
2808c2ecf20Sopenharmony_ci			    sc->debug.debugfs_phy, sc,
2818c2ecf20Sopenharmony_ci			    &fops_tx99);
2828c2ecf20Sopenharmony_ci	debugfs_create_file("tx99_power", 0600,
2838c2ecf20Sopenharmony_ci			    sc->debug.debugfs_phy, sc,
2848c2ecf20Sopenharmony_ci			    &fops_tx99_power);
2858c2ecf20Sopenharmony_ci}
286