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