1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
4  * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
5  * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@wp.pl>
6  */
7 
8 #include <linux/etherdevice.h>
9 #include "mt76x0.h"
10 
11 static int
12 mt76x0_set_channel(struct mt76x02_dev *dev, struct cfg80211_chan_def *chandef)
13 {
14 	int ret;
15 
16 	cancel_delayed_work_sync(&dev->cal_work);
17 	dev->beacon_ops->pre_tbtt_enable(dev, false);
18 	if (mt76_is_mmio(dev))
19 		tasklet_disable(&dev->dfs_pd.dfs_tasklet);
20 
21 	mt76_set_channel(&dev->mt76);
22 	ret = mt76x0_phy_set_channel(dev, chandef);
23 
24 	/* channel cycle counters read-and-clear */
25 	mt76_rr(dev, MT_CH_IDLE);
26 	mt76_rr(dev, MT_CH_BUSY);
27 
28 	mt76x02_edcca_init(dev, true);
29 
30 	if (mt76_is_mmio(dev)) {
31 		mt76x02_dfs_init_params(dev);
32 		tasklet_enable(&dev->dfs_pd.dfs_tasklet);
33 	}
34 	dev->beacon_ops->pre_tbtt_enable(dev, true);
35 
36 	mt76_txq_schedule_all(&dev->mt76);
37 
38 	return ret;
39 }
40 
41 int mt76x0_config(struct ieee80211_hw *hw, u32 changed)
42 {
43 	struct mt76x02_dev *dev = hw->priv;
44 	int ret = 0;
45 
46 	mutex_lock(&dev->mt76.mutex);
47 
48 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
49 		ieee80211_stop_queues(hw);
50 		ret = mt76x0_set_channel(dev, &hw->conf.chandef);
51 		ieee80211_wake_queues(hw);
52 	}
53 
54 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
55 		dev->mt76.txpower_conf = hw->conf.power_level * 2;
56 
57 		if (test_bit(MT76_STATE_RUNNING, &dev->mt76.state))
58 			mt76x0_phy_set_txpower(dev);
59 	}
60 
61 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
62 		if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
63 			dev->mt76.rxfilter |= MT_RX_FILTR_CFG_PROMISC;
64 		else
65 			dev->mt76.rxfilter &= ~MT_RX_FILTR_CFG_PROMISC;
66 
67 		mt76_wr(dev, MT_RX_FILTR_CFG, dev->mt76.rxfilter);
68 	}
69 
70 	mutex_unlock(&dev->mt76.mutex);
71 
72 	return ret;
73 }
74 EXPORT_SYMBOL_GPL(mt76x0_config);
75