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.
20 mt76x2_start(struct ieee80211_hw *hw)
22 struct mt76x2_dev *dev = hw->priv;
25 mutex_lock(&dev->mutex);
27 ret = mt76x2_mac_start(dev);
31 ret = mt76x2_phy_start(dev);
35 ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mac_work,
36 MT_CALIBRATE_INTERVAL);
38 set_bit(MT76_STATE_RUNNING, &dev->mt76.state);
41 mutex_unlock(&dev->mutex);
46 mt76x2_stop(struct ieee80211_hw *hw)
48 struct mt76x2_dev *dev = hw->priv;
50 mutex_lock(&dev->mutex);
51 clear_bit(MT76_STATE_RUNNING, &dev->mt76.state);
52 mt76x2_stop_hardware(dev);
53 mutex_unlock(&dev->mutex);
57 mt76x2_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
59 struct mt76x2_dev *dev = hw->priv;
60 struct mt76x2_vif *mvif = (struct mt76x2_vif *) vif->drv_priv;
63 if (vif->addr[0] & BIT(1))
64 idx = 1 + (((dev->mt76.macaddr[0] ^ vif->addr[0]) >> 2) & 7);
67 * Client mode typically only has one configurable BSSID register,
68 * which is used for bssidx=0. This is linked to the MAC address.
69 * Since mac80211 allows changing interface types, and we cannot
70 * force the use of the primary MAC address for a station mode
71 * interface, we need some other way of configuring a per-interface
73 * The hardware provides an AP-Client feature, where bssidx 0-7 are
74 * used for AP mode and bssidx 8-15 for client mode.
75 * We shift the station interface bss index by 8 to force the
76 * hardware to recognize the BSSID.
77 * The resulting bssidx mismatch for unicast frames is ignored by hw.
79 if (vif->type == NL80211_IFTYPE_STATION)
83 mvif->group_wcid.idx = MT_VIF_WCID(idx);
84 mvif->group_wcid.hw_key_idx = -1;
85 mt76x2_txq_init(dev, vif->txq);
91 mt76x2_set_channel(struct mt76x2_dev *dev, struct cfg80211_chan_def *chandef)
95 cancel_delayed_work_sync(&dev->cal_work);
97 set_bit(MT76_RESET, &dev->mt76.state);
99 mt76_set_channel(&dev->mt76);
101 tasklet_disable(&dev->pre_tbtt_tasklet);
102 tasklet_disable(&dev->dfs_pd.dfs_tasklet);
104 mt76x2_mac_stop(dev, true);
105 ret = mt76x2_phy_set_channel(dev, chandef);
107 /* channel cycle counters read-and-clear */
108 mt76_rr(dev, MT_CH_IDLE);
109 mt76_rr(dev, MT_CH_BUSY);
111 mt76x2_dfs_init_params(dev);
113 mt76x2_mac_resume(dev);
114 tasklet_enable(&dev->dfs_pd.dfs_tasklet);
115 tasklet_enable(&dev->pre_tbtt_tasklet);
117 clear_bit(MT76_RESET, &dev->mt76.state);
119 mt76_txq_schedule_all(&dev->mt76);
125 mt76x2_config(struct ieee80211_hw *hw, u32 changed)
127 struct mt76x2_dev *dev = hw->priv;
130 mutex_lock(&dev->mutex);
132 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
133 if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
134 dev->rxfilter |= MT_RX_FILTR_CFG_PROMISC;
136 dev->rxfilter &= ~MT_RX_FILTR_CFG_PROMISC;
138 mt76_wr(dev, MT_RX_FILTR_CFG, dev->rxfilter);
141 if (changed & IEEE80211_CONF_CHANGE_POWER) {
142 dev->txpower_conf = hw->conf.power_level * 2;
144 /* convert to per-chain power for 2x2 devices */
145 dev->txpower_conf -= 6;
147 if (test_bit(MT76_STATE_RUNNING, &dev->mt76.state)) {
148 mt76x2_phy_set_txpower(dev);
149 mt76x2_tx_set_txpwr_auto(dev, dev->txpower_conf);
153 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
154 ieee80211_stop_queues(hw);
155 ret = mt76x2_set_channel(dev, &hw->conf.chandef);
156 ieee80211_wake_queues(hw);
159 mutex_unlock(&dev->mutex);
165 mt76x2_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
166 struct ieee80211_bss_conf *info, u32 changed)
168 struct mt76x2_dev *dev = hw->priv;
169 struct mt76x2_vif *mvif = (struct mt76x2_vif *) vif->drv_priv;
171 mutex_lock(&dev->mutex);
173 if (changed & BSS_CHANGED_BSSID)
174 mt76x2_mac_set_bssid(dev, mvif->idx, info->bssid);
176 if (changed & BSS_CHANGED_BEACON_INT) {
177 mt76_rmw_field(dev, MT_BEACON_TIME_CFG,
178 MT_BEACON_TIME_CFG_INTVAL,
179 info->beacon_int << 4);
180 dev->beacon_int = info->beacon_int;
184 if (changed & BSS_CHANGED_BEACON_ENABLED) {
185 tasklet_disable(&dev->pre_tbtt_tasklet);
186 mt76x2_mac_set_beacon_enable(dev, mvif->idx,
187 info->enable_beacon);
188 tasklet_enable(&dev->pre_tbtt_tasklet);
191 if (changed & BSS_CHANGED_ERP_SLOT) {
192 int slottime = info->use_short_slot ? 9 : 20;
194 dev->slottime = slottime;
195 mt76x2_set_tx_ackto(dev);
198 mutex_unlock(&dev->mutex);
202 mt76x2_sta_ps(struct mt76_dev *mdev, struct ieee80211_sta *sta, bool ps)
204 struct mt76x2_sta *msta = (struct mt76x2_sta *) sta->drv_priv;
205 struct mt76x2_dev *dev = container_of(mdev, struct mt76x2_dev, mt76);
206 int idx = msta->wcid.idx;
208 mt76_stop_tx_queues(&dev->mt76, sta, true);
209 mt76x2_mac_wcid_set_drop(dev, idx, ps);
213 mt76x2_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
216 struct mt76x2_dev *dev = hw->priv;
218 tasklet_disable(&dev->pre_tbtt_tasklet);
219 set_bit(MT76_SCANNING, &dev->mt76.state);
223 mt76x2_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
225 struct mt76x2_dev *dev = hw->priv;
227 clear_bit(MT76_SCANNING, &dev->mt76.state);
228 tasklet_enable(&dev->pre_tbtt_tasklet);
232 mt76x2_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
233 u32 queues, bool drop)
238 mt76x2_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif, int *dbm)
240 struct mt76x2_dev *dev = hw->priv;
242 *dbm = dev->txpower_cur / 2;
244 /* convert from per-chain power to combined output on 2x2 devices */
250 static void mt76x2_set_coverage_class(struct ieee80211_hw *hw,
253 struct mt76x2_dev *dev = hw->priv;
255 mutex_lock(&dev->mutex);
256 dev->coverage_class = coverage_class;
257 mt76x2_set_tx_ackto(dev);
258 mutex_unlock(&dev->mutex);
262 mt76x2_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set)
267 static int mt76x2_set_antenna(struct ieee80211_hw *hw, u32 tx_ant,
270 struct mt76x2_dev *dev = hw->priv;
272 if (!tx_ant || tx_ant > 3 || tx_ant != rx_ant)
275 mutex_lock(&dev->mutex);
277 dev->chainmask = (tx_ant == 3) ? 0x202 : 0x101;
278 dev->mt76.antenna_mask = tx_ant;
280 mt76_set_stream_caps(&dev->mt76, true);
281 mt76x2_phy_set_antenna(dev);
283 mutex_unlock(&dev->mutex);
288 static int mt76x2_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant,
291 struct mt76x2_dev *dev = hw->priv;
293 mutex_lock(&dev->mutex);
294 *tx_ant = dev->mt76.antenna_mask;
295 *rx_ant = dev->mt76.antenna_mask;
296 mutex_unlock(&dev->mutex);
302 mt76x2_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
304 struct mt76x2_dev *dev = hw->priv;
306 if (val != ~0 && val > 0xffff)
309 mutex_lock(&dev->mutex);
310 mt76x2_mac_set_tx_protection(dev, val);
311 mutex_unlock(&dev->mutex);
316 const struct ieee80211_ops mt76x2_ops = {
318 .start = mt76x2_start,
320 .add_interface = mt76x2_add_interface,
321 .remove_interface = mt76x2_remove_interface,
322 .config = mt76x2_config,
323 .configure_filter = mt76x2_configure_filter,
324 .bss_info_changed = mt76x2_bss_info_changed,
325 .sta_add = mt76x2_sta_add,
326 .sta_remove = mt76x2_sta_remove,
327 .set_key = mt76x2_set_key,
328 .conf_tx = mt76x2_conf_tx,
329 .sw_scan_start = mt76x2_sw_scan,
330 .sw_scan_complete = mt76x2_sw_scan_complete,
331 .flush = mt76x2_flush,
332 .ampdu_action = mt76x2_ampdu_action,
333 .get_txpower = mt76x2_get_txpower,
334 .wake_tx_queue = mt76_wake_tx_queue,
335 .sta_rate_tbl_update = mt76x2_sta_rate_tbl_update,
336 .release_buffered_frames = mt76_release_buffered_frames,
337 .set_coverage_class = mt76x2_set_coverage_class,
338 .get_survey = mt76_get_survey,
339 .set_tim = mt76x2_set_tim,
340 .set_antenna = mt76x2_set_antenna,
341 .get_antenna = mt76x2_get_antenna,
342 .set_rts_threshold = mt76x2_set_rts_threshold,