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