xref: /linux/drivers/net/wireless/mediatek/mt76/mt76x2/pci_main.c (revision 3d0fe49454652117522f60bfbefb978ba0e5300b)
1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4  */
5 
6 #include "mt76x2.h"
7 #include "../mt76x02_mac.h"
8 
9 static int
10 mt76x2_start(struct ieee80211_hw *hw)
11 {
12 	struct mt76x02_dev *dev = hw->priv;
13 
14 	mt76x02_mac_start(dev);
15 	mt76x2_phy_start(dev);
16 
17 	ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mphy.mac_work,
18 				     MT_MAC_WORK_INTERVAL);
19 	ieee80211_queue_delayed_work(mt76_hw(dev), &dev->wdt_work,
20 				     MT_WATCHDOG_TIME);
21 
22 	set_bit(MT76_STATE_RUNNING, &dev->mphy.state);
23 	return 0;
24 }
25 
26 static void
27 mt76x2_stop(struct ieee80211_hw *hw)
28 {
29 	struct mt76x02_dev *dev = hw->priv;
30 
31 	clear_bit(MT76_STATE_RUNNING, &dev->mphy.state);
32 	mt76x2_stop_hardware(dev);
33 }
34 
35 static void
36 mt76x2_set_channel(struct mt76x02_dev *dev, struct cfg80211_chan_def *chandef)
37 {
38 	cancel_delayed_work_sync(&dev->cal_work);
39 	tasklet_disable(&dev->mt76.pre_tbtt_tasklet);
40 	tasklet_disable(&dev->dfs_pd.dfs_tasklet);
41 
42 	mutex_lock(&dev->mt76.mutex);
43 	set_bit(MT76_RESET, &dev->mphy.state);
44 
45 	mt76_set_channel(&dev->mphy);
46 
47 	mt76x2_mac_stop(dev, true);
48 	mt76x2_phy_set_channel(dev, chandef);
49 
50 	mt76x02_mac_cc_reset(dev);
51 	mt76x02_dfs_init_params(dev);
52 
53 	mt76x2_mac_resume(dev);
54 
55 	clear_bit(MT76_RESET, &dev->mphy.state);
56 	mutex_unlock(&dev->mt76.mutex);
57 
58 	tasklet_enable(&dev->dfs_pd.dfs_tasklet);
59 	tasklet_enable(&dev->mt76.pre_tbtt_tasklet);
60 
61 	mt76_txq_schedule_all(&dev->mphy);
62 }
63 
64 static int
65 mt76x2_config(struct ieee80211_hw *hw, u32 changed)
66 {
67 	struct mt76x02_dev *dev = hw->priv;
68 
69 	mutex_lock(&dev->mt76.mutex);
70 
71 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
72 		if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
73 			dev->mt76.rxfilter |= MT_RX_FILTR_CFG_PROMISC;
74 		else
75 			dev->mt76.rxfilter &= ~MT_RX_FILTR_CFG_PROMISC;
76 
77 		mt76_wr(dev, MT_RX_FILTR_CFG, dev->mt76.rxfilter);
78 	}
79 
80 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
81 		struct mt76_phy *mphy = &dev->mphy;
82 
83 		dev->txpower_conf = hw->conf.power_level * 2;
84 		dev->txpower_conf = mt76_get_sar_power(mphy,
85 						       mphy->chandef.chan,
86 						       dev->txpower_conf);
87 		/* convert to per-chain power for 2x2 devices */
88 		dev->txpower_conf -= 6;
89 
90 		if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state)) {
91 			mt76x2_phy_set_txpower(dev);
92 			mt76x02_tx_set_txpwr_auto(dev, dev->txpower_conf);
93 		}
94 	}
95 
96 	mutex_unlock(&dev->mt76.mutex);
97 
98 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
99 		ieee80211_stop_queues(hw);
100 		mt76x2_set_channel(dev, &hw->conf.chandef);
101 		ieee80211_wake_queues(hw);
102 	}
103 
104 	return 0;
105 }
106 
107 static void
108 mt76x2_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
109 	     u32 queues, bool drop)
110 {
111 }
112 
113 static int mt76x2_set_antenna(struct ieee80211_hw *hw, u32 tx_ant,
114 			      u32 rx_ant)
115 {
116 	struct mt76x02_dev *dev = hw->priv;
117 
118 	if (!tx_ant || tx_ant > 3 || tx_ant != rx_ant)
119 		return -EINVAL;
120 
121 	mutex_lock(&dev->mt76.mutex);
122 
123 	dev->mphy.chainmask = (tx_ant == 3) ? 0x202 : 0x101;
124 	dev->mphy.antenna_mask = tx_ant;
125 
126 	mt76_set_stream_caps(&dev->mphy, true);
127 	mt76x2_phy_set_antenna(dev);
128 
129 	mutex_unlock(&dev->mt76.mutex);
130 
131 	return 0;
132 }
133 
134 const struct ieee80211_ops mt76x2_ops = {
135 	.tx = mt76x02_tx,
136 	.start = mt76x2_start,
137 	.stop = mt76x2_stop,
138 	.add_interface = mt76x02_add_interface,
139 	.remove_interface = mt76x02_remove_interface,
140 	.config = mt76x2_config,
141 	.configure_filter = mt76x02_configure_filter,
142 	.bss_info_changed = mt76x02_bss_info_changed,
143 	.sta_state = mt76_sta_state,
144 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
145 	.set_key = mt76x02_set_key,
146 	.conf_tx = mt76x02_conf_tx,
147 	.sw_scan_start = mt76_sw_scan,
148 	.sw_scan_complete = mt76x02_sw_scan_complete,
149 	.flush = mt76x2_flush,
150 	.ampdu_action = mt76x02_ampdu_action,
151 	.get_txpower = mt76_get_txpower,
152 	.wake_tx_queue = mt76_wake_tx_queue,
153 	.sta_rate_tbl_update = mt76x02_sta_rate_tbl_update,
154 	.release_buffered_frames = mt76_release_buffered_frames,
155 	.set_coverage_class = mt76x02_set_coverage_class,
156 	.get_survey = mt76_get_survey,
157 	.set_tim = mt76_set_tim,
158 	.set_antenna = mt76x2_set_antenna,
159 	.get_antenna = mt76_get_antenna,
160 	.set_rts_threshold = mt76x02_set_rts_threshold,
161 	.reconfig_complete = mt76x02_reconfig_complete,
162 	.set_sar_specs = mt76x2_set_sar_specs,
163 };
164 
165