xref: /linux/drivers/net/wireless/mediatek/mt76/mt76x2/pci_main.c (revision bd628c1bed7902ec1f24ba0fe70758949146abbe)
1 /*
2  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
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 "mt76x2.h"
18 
19 static int
20 mt76x2_start(struct ieee80211_hw *hw)
21 {
22 	struct mt76x02_dev *dev = hw->priv;
23 	int ret;
24 
25 	mutex_lock(&dev->mt76.mutex);
26 
27 	ret = mt76x2_mac_start(dev);
28 	if (ret)
29 		goto out;
30 
31 	ret = mt76x2_phy_start(dev);
32 	if (ret)
33 		goto out;
34 
35 	ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mac_work,
36 				     MT_CALIBRATE_INTERVAL);
37 
38 	set_bit(MT76_STATE_RUNNING, &dev->mt76.state);
39 
40 out:
41 	mutex_unlock(&dev->mt76.mutex);
42 	return ret;
43 }
44 
45 static void
46 mt76x2_stop(struct ieee80211_hw *hw)
47 {
48 	struct mt76x02_dev *dev = hw->priv;
49 
50 	mutex_lock(&dev->mt76.mutex);
51 	clear_bit(MT76_STATE_RUNNING, &dev->mt76.state);
52 	mt76x2_stop_hardware(dev);
53 	mutex_unlock(&dev->mt76.mutex);
54 }
55 
56 static int
57 mt76x2_set_channel(struct mt76x02_dev *dev, struct cfg80211_chan_def *chandef)
58 {
59 	int ret;
60 
61 	cancel_delayed_work_sync(&dev->cal_work);
62 
63 	set_bit(MT76_RESET, &dev->mt76.state);
64 
65 	mt76_set_channel(&dev->mt76);
66 
67 	tasklet_disable(&dev->pre_tbtt_tasklet);
68 	tasklet_disable(&dev->dfs_pd.dfs_tasklet);
69 
70 	mt76x2_mac_stop(dev, true);
71 	ret = mt76x2_phy_set_channel(dev, chandef);
72 
73 	/* channel cycle counters read-and-clear */
74 	mt76_rr(dev, MT_CH_IDLE);
75 	mt76_rr(dev, MT_CH_BUSY);
76 
77 	mt76x02_dfs_init_params(dev);
78 
79 	mt76x2_mac_resume(dev);
80 	tasklet_enable(&dev->dfs_pd.dfs_tasklet);
81 	tasklet_enable(&dev->pre_tbtt_tasklet);
82 
83 	clear_bit(MT76_RESET, &dev->mt76.state);
84 
85 	mt76_txq_schedule_all(&dev->mt76);
86 
87 	return ret;
88 }
89 
90 static int
91 mt76x2_config(struct ieee80211_hw *hw, u32 changed)
92 {
93 	struct mt76x02_dev *dev = hw->priv;
94 	int ret = 0;
95 
96 	mutex_lock(&dev->mt76.mutex);
97 
98 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
99 		if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
100 			dev->mt76.rxfilter |= MT_RX_FILTR_CFG_PROMISC;
101 		else
102 			dev->mt76.rxfilter &= ~MT_RX_FILTR_CFG_PROMISC;
103 
104 		mt76_wr(dev, MT_RX_FILTR_CFG, dev->mt76.rxfilter);
105 	}
106 
107 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
108 		dev->mt76.txpower_conf = hw->conf.power_level * 2;
109 
110 		/* convert to per-chain power for 2x2 devices */
111 		dev->mt76.txpower_conf -= 6;
112 
113 		if (test_bit(MT76_STATE_RUNNING, &dev->mt76.state)) {
114 			mt76x2_phy_set_txpower(dev);
115 			mt76x02_tx_set_txpwr_auto(dev, dev->mt76.txpower_conf);
116 		}
117 	}
118 
119 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
120 		ieee80211_stop_queues(hw);
121 		ret = mt76x2_set_channel(dev, &hw->conf.chandef);
122 		ieee80211_wake_queues(hw);
123 	}
124 
125 	mutex_unlock(&dev->mt76.mutex);
126 
127 	return ret;
128 }
129 
130 static void
131 mt76x2_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
132 	     u32 queues, bool drop)
133 {
134 }
135 
136 static int
137 mt76x2_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set)
138 {
139 	return 0;
140 }
141 
142 static int mt76x2_set_antenna(struct ieee80211_hw *hw, u32 tx_ant,
143 			      u32 rx_ant)
144 {
145 	struct mt76x02_dev *dev = hw->priv;
146 
147 	if (!tx_ant || tx_ant > 3 || tx_ant != rx_ant)
148 		return -EINVAL;
149 
150 	mutex_lock(&dev->mt76.mutex);
151 
152 	dev->mt76.chainmask = (tx_ant == 3) ? 0x202 : 0x101;
153 	dev->mt76.antenna_mask = tx_ant;
154 
155 	mt76_set_stream_caps(&dev->mt76, true);
156 	mt76x2_phy_set_antenna(dev);
157 
158 	mutex_unlock(&dev->mt76.mutex);
159 
160 	return 0;
161 }
162 
163 static int mt76x2_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant,
164 			      u32 *rx_ant)
165 {
166 	struct mt76x02_dev *dev = hw->priv;
167 
168 	mutex_lock(&dev->mt76.mutex);
169 	*tx_ant = dev->mt76.antenna_mask;
170 	*rx_ant = dev->mt76.antenna_mask;
171 	mutex_unlock(&dev->mt76.mutex);
172 
173 	return 0;
174 }
175 
176 const struct ieee80211_ops mt76x2_ops = {
177 	.tx = mt76x02_tx,
178 	.start = mt76x2_start,
179 	.stop = mt76x2_stop,
180 	.add_interface = mt76x02_add_interface,
181 	.remove_interface = mt76x02_remove_interface,
182 	.config = mt76x2_config,
183 	.configure_filter = mt76x02_configure_filter,
184 	.bss_info_changed = mt76x02_bss_info_changed,
185 	.sta_state = mt76_sta_state,
186 	.set_key = mt76x02_set_key,
187 	.conf_tx = mt76x02_conf_tx,
188 	.sw_scan_start = mt76x02_sw_scan,
189 	.sw_scan_complete = mt76x02_sw_scan_complete,
190 	.flush = mt76x2_flush,
191 	.ampdu_action = mt76x02_ampdu_action,
192 	.get_txpower = mt76x02_get_txpower,
193 	.wake_tx_queue = mt76_wake_tx_queue,
194 	.sta_rate_tbl_update = mt76x02_sta_rate_tbl_update,
195 	.release_buffered_frames = mt76_release_buffered_frames,
196 	.set_coverage_class = mt76x02_set_coverage_class,
197 	.get_survey = mt76_get_survey,
198 	.set_tim = mt76x2_set_tim,
199 	.set_antenna = mt76x2_set_antenna,
200 	.get_antenna = mt76x2_get_antenna,
201 	.set_rts_threshold = mt76x02_set_rts_threshold,
202 };
203 
204