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 	tasklet_disable(&dev->mt76.pre_tbtt_tasklet);
58 	tasklet_disable(&dev->dfs_pd.dfs_tasklet);
59 
60 	mutex_lock(&dev->mt76.mutex);
61 	set_bit(MT76_RESET, &dev->mt76.state);
62 
63 	mt76_set_channel(&dev->mt76);
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 
76 	clear_bit(MT76_RESET, &dev->mt76.state);
77 	mutex_unlock(&dev->mt76.mutex);
78 
79 	tasklet_enable(&dev->dfs_pd.dfs_tasklet);
80 	tasklet_enable(&dev->mt76.pre_tbtt_tasklet);
81 
82 	mt76_txq_schedule_all(&dev->mt76);
83 
84 	return ret;
85 }
86 
87 static int
88 mt76x2_config(struct ieee80211_hw *hw, u32 changed)
89 {
90 	struct mt76x02_dev *dev = hw->priv;
91 	int ret = 0;
92 
93 	mutex_lock(&dev->mt76.mutex);
94 
95 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
96 		if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
97 			dev->mt76.rxfilter |= MT_RX_FILTR_CFG_PROMISC;
98 		else
99 			dev->mt76.rxfilter &= ~MT_RX_FILTR_CFG_PROMISC;
100 
101 		mt76_wr(dev, MT_RX_FILTR_CFG, dev->mt76.rxfilter);
102 	}
103 
104 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
105 		dev->mt76.txpower_conf = hw->conf.power_level * 2;
106 
107 		/* convert to per-chain power for 2x2 devices */
108 		dev->mt76.txpower_conf -= 6;
109 
110 		if (test_bit(MT76_STATE_RUNNING, &dev->mt76.state)) {
111 			mt76x2_phy_set_txpower(dev);
112 			mt76x02_tx_set_txpwr_auto(dev, dev->mt76.txpower_conf);
113 		}
114 	}
115 
116 	mutex_unlock(&dev->mt76.mutex);
117 
118 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
119 		ieee80211_stop_queues(hw);
120 		ret = mt76x2_set_channel(dev, &hw->conf.chandef);
121 		ieee80211_wake_queues(hw);
122 	}
123 
124 	return ret;
125 }
126 
127 static void
128 mt76x2_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
129 	     u32 queues, bool drop)
130 {
131 }
132 
133 static int mt76x2_set_antenna(struct ieee80211_hw *hw, u32 tx_ant,
134 			      u32 rx_ant)
135 {
136 	struct mt76x02_dev *dev = hw->priv;
137 
138 	if (!tx_ant || tx_ant > 3 || tx_ant != rx_ant)
139 		return -EINVAL;
140 
141 	mutex_lock(&dev->mt76.mutex);
142 
143 	dev->mt76.chainmask = (tx_ant == 3) ? 0x202 : 0x101;
144 	dev->mt76.antenna_mask = tx_ant;
145 
146 	mt76_set_stream_caps(&dev->mt76, true);
147 	mt76x2_phy_set_antenna(dev);
148 
149 	mutex_unlock(&dev->mt76.mutex);
150 
151 	return 0;
152 }
153 
154 static int mt76x2_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant,
155 			      u32 *rx_ant)
156 {
157 	struct mt76x02_dev *dev = hw->priv;
158 
159 	mutex_lock(&dev->mt76.mutex);
160 	*tx_ant = dev->mt76.antenna_mask;
161 	*rx_ant = dev->mt76.antenna_mask;
162 	mutex_unlock(&dev->mt76.mutex);
163 
164 	return 0;
165 }
166 
167 const struct ieee80211_ops mt76x2_ops = {
168 	.tx = mt76x02_tx,
169 	.start = mt76x2_start,
170 	.stop = mt76x2_stop,
171 	.add_interface = mt76x02_add_interface,
172 	.remove_interface = mt76x02_remove_interface,
173 	.config = mt76x2_config,
174 	.configure_filter = mt76x02_configure_filter,
175 	.bss_info_changed = mt76x02_bss_info_changed,
176 	.sta_state = mt76_sta_state,
177 	.set_key = mt76x02_set_key,
178 	.conf_tx = mt76x02_conf_tx,
179 	.sw_scan_start = mt76x02_sw_scan,
180 	.sw_scan_complete = mt76x02_sw_scan_complete,
181 	.flush = mt76x2_flush,
182 	.ampdu_action = mt76x02_ampdu_action,
183 	.get_txpower = mt76_get_txpower,
184 	.wake_tx_queue = mt76_wake_tx_queue,
185 	.sta_rate_tbl_update = mt76x02_sta_rate_tbl_update,
186 	.release_buffered_frames = mt76_release_buffered_frames,
187 	.set_coverage_class = mt76x02_set_coverage_class,
188 	.get_survey = mt76_get_survey,
189 	.set_tim = mt76_set_tim,
190 	.set_antenna = mt76x2_set_antenna,
191 	.get_antenna = mt76x2_get_antenna,
192 	.set_rts_threshold = mt76x02_set_rts_threshold,
193 };
194 
195