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