2 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
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.
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.
18 #include "mt76x02_util.h"
21 mt76x2_start(struct ieee80211_hw *hw)
23 struct mt76x2_dev *dev = hw->priv;
26 mutex_lock(&dev->mt76.mutex);
28 ret = mt76x2_mac_start(dev);
32 ret = mt76x2_phy_start(dev);
36 ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mac_work,
37 MT_CALIBRATE_INTERVAL);
39 set_bit(MT76_STATE_RUNNING, &dev->mt76.state);
42 mutex_unlock(&dev->mt76.mutex);
47 mt76x2_stop(struct ieee80211_hw *hw)
49 struct mt76x2_dev *dev = hw->priv;
51 mutex_lock(&dev->mt76.mutex);
52 clear_bit(MT76_STATE_RUNNING, &dev->mt76.state);
53 mt76x2_stop_hardware(dev);
54 mutex_unlock(&dev->mt76.mutex);
58 mt76x2_set_channel(struct mt76x2_dev *dev, struct cfg80211_chan_def *chandef)
62 cancel_delayed_work_sync(&dev->cal_work);
64 set_bit(MT76_RESET, &dev->mt76.state);
66 mt76_set_channel(&dev->mt76);
68 tasklet_disable(&dev->pre_tbtt_tasklet);
69 tasklet_disable(&dev->dfs_pd.dfs_tasklet);
71 mt76x2_mac_stop(dev, true);
72 ret = mt76x2_phy_set_channel(dev, chandef);
74 /* channel cycle counters read-and-clear */
75 mt76_rr(dev, MT_CH_IDLE);
76 mt76_rr(dev, MT_CH_BUSY);
78 mt76x2_dfs_init_params(dev);
80 mt76x2_mac_resume(dev);
81 tasklet_enable(&dev->dfs_pd.dfs_tasklet);
82 tasklet_enable(&dev->pre_tbtt_tasklet);
84 clear_bit(MT76_RESET, &dev->mt76.state);
86 mt76_txq_schedule_all(&dev->mt76);
92 mt76x2_config(struct ieee80211_hw *hw, u32 changed)
94 struct mt76x2_dev *dev = hw->priv;
97 mutex_lock(&dev->mt76.mutex);
99 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
100 if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
101 dev->mt76.rxfilter |= MT_RX_FILTR_CFG_PROMISC;
103 dev->mt76.rxfilter &= ~MT_RX_FILTR_CFG_PROMISC;
105 mt76_wr(dev, MT_RX_FILTR_CFG, dev->mt76.rxfilter);
108 if (changed & IEEE80211_CONF_CHANGE_POWER) {
109 dev->mt76.txpower_conf = hw->conf.power_level * 2;
111 /* convert to per-chain power for 2x2 devices */
112 dev->mt76.txpower_conf -= 6;
114 if (test_bit(MT76_STATE_RUNNING, &dev->mt76.state)) {
115 mt76x2_phy_set_txpower(dev);
116 mt76x2_tx_set_txpwr_auto(dev, dev->mt76.txpower_conf);
120 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
121 ieee80211_stop_queues(hw);
122 ret = mt76x2_set_channel(dev, &hw->conf.chandef);
123 ieee80211_wake_queues(hw);
126 mutex_unlock(&dev->mt76.mutex);
132 mt76x2_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
133 struct ieee80211_bss_conf *info, u32 changed)
135 struct mt76x2_dev *dev = hw->priv;
136 struct mt76x02_vif *mvif = (struct mt76x02_vif *) vif->drv_priv;
138 mutex_lock(&dev->mt76.mutex);
140 if (changed & BSS_CHANGED_BSSID)
141 mt76x2_mac_set_bssid(dev, mvif->idx, info->bssid);
143 if (changed & BSS_CHANGED_BEACON_INT) {
144 mt76_rmw_field(dev, MT_BEACON_TIME_CFG,
145 MT_BEACON_TIME_CFG_INTVAL,
146 info->beacon_int << 4);
147 dev->beacon_int = info->beacon_int;
151 if (changed & BSS_CHANGED_BEACON_ENABLED) {
152 tasklet_disable(&dev->pre_tbtt_tasklet);
153 mt76x2_mac_set_beacon_enable(dev, mvif->idx,
154 info->enable_beacon);
155 tasklet_enable(&dev->pre_tbtt_tasklet);
158 if (changed & BSS_CHANGED_ERP_SLOT) {
159 int slottime = info->use_short_slot ? 9 : 20;
161 dev->slottime = slottime;
162 mt76x2_set_tx_ackto(dev);
165 mutex_unlock(&dev->mt76.mutex);
169 mt76x2_sta_ps(struct mt76_dev *mdev, struct ieee80211_sta *sta, bool ps)
171 struct mt76x02_sta *msta = (struct mt76x02_sta *) sta->drv_priv;
172 struct mt76x2_dev *dev = container_of(mdev, struct mt76x2_dev, mt76);
173 int idx = msta->wcid.idx;
175 mt76_stop_tx_queues(&dev->mt76, sta, true);
176 mt76x02_mac_wcid_set_drop(&dev->mt76, idx, ps);
180 mt76x2_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
183 struct mt76x2_dev *dev = hw->priv;
185 tasklet_disable(&dev->pre_tbtt_tasklet);
186 set_bit(MT76_SCANNING, &dev->mt76.state);
190 mt76x2_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
192 struct mt76x2_dev *dev = hw->priv;
194 clear_bit(MT76_SCANNING, &dev->mt76.state);
195 tasklet_enable(&dev->pre_tbtt_tasklet);
199 mt76x2_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
200 u32 queues, bool drop)
205 mt76x2_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif, int *dbm)
207 struct mt76x2_dev *dev = hw->priv;
209 *dbm = dev->mt76.txpower_cur / 2;
211 /* convert from per-chain power to combined output on 2x2 devices */
217 static void mt76x2_set_coverage_class(struct ieee80211_hw *hw,
220 struct mt76x2_dev *dev = hw->priv;
222 mutex_lock(&dev->mt76.mutex);
223 dev->coverage_class = coverage_class;
224 mt76x2_set_tx_ackto(dev);
225 mutex_unlock(&dev->mt76.mutex);
229 mt76x2_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set)
234 static int mt76x2_set_antenna(struct ieee80211_hw *hw, u32 tx_ant,
237 struct mt76x2_dev *dev = hw->priv;
239 if (!tx_ant || tx_ant > 3 || tx_ant != rx_ant)
242 mutex_lock(&dev->mt76.mutex);
244 dev->mt76.chainmask = (tx_ant == 3) ? 0x202 : 0x101;
245 dev->mt76.antenna_mask = tx_ant;
247 mt76_set_stream_caps(&dev->mt76, true);
248 mt76x2_phy_set_antenna(dev);
250 mutex_unlock(&dev->mt76.mutex);
255 static int mt76x2_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant,
258 struct mt76x2_dev *dev = hw->priv;
260 mutex_lock(&dev->mt76.mutex);
261 *tx_ant = dev->mt76.antenna_mask;
262 *rx_ant = dev->mt76.antenna_mask;
263 mutex_unlock(&dev->mt76.mutex);
269 mt76x2_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
271 struct mt76x2_dev *dev = hw->priv;
273 if (val != ~0 && val > 0xffff)
276 mutex_lock(&dev->mutex);
277 mt76x2_mac_set_tx_protection(dev, val);
278 mutex_unlock(&dev->mutex);
283 const struct ieee80211_ops mt76x2_ops = {
285 .start = mt76x2_start,
287 .add_interface = mt76x02_add_interface,
288 .remove_interface = mt76x02_remove_interface,
289 .config = mt76x2_config,
290 .configure_filter = mt76x02_configure_filter,
291 .bss_info_changed = mt76x2_bss_info_changed,
292 .sta_add = mt76x02_sta_add,
293 .sta_remove = mt76x02_sta_remove,
294 .set_key = mt76x02_set_key,
295 .conf_tx = mt76x02_conf_tx,
296 .sw_scan_start = mt76x2_sw_scan,
297 .sw_scan_complete = mt76x2_sw_scan_complete,
298 .flush = mt76x2_flush,
299 .ampdu_action = mt76x02_ampdu_action,
300 .get_txpower = mt76x2_get_txpower,
301 .wake_tx_queue = mt76_wake_tx_queue,
302 .sta_rate_tbl_update = mt76x02_sta_rate_tbl_update,
303 .release_buffered_frames = mt76_release_buffered_frames,
304 .set_coverage_class = mt76x2_set_coverage_class,
305 .get_survey = mt76_get_survey,
306 .set_tim = mt76x2_set_tim,
307 .set_antenna = mt76x2_set_antenna,
308 .get_antenna = mt76x2_get_antenna,
309 .set_rts_threshold = mt76x2_set_rts_threshold,