1 /*
2 * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
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 "mt76x2u.h"
18
mt76x2u_start(struct ieee80211_hw * hw)19 static int mt76x2u_start(struct ieee80211_hw *hw)
20 {
21 struct mt76x2_dev *dev = hw->priv;
22 int ret;
23
24 mutex_lock(&dev->mutex);
25
26 ret = mt76x2u_mac_start(dev);
27 if (ret)
28 goto out;
29
30 set_bit(MT76_STATE_RUNNING, &dev->mt76.state);
31
32 out:
33 mutex_unlock(&dev->mutex);
34 return ret;
35 }
36
mt76x2u_stop(struct ieee80211_hw * hw)37 static void mt76x2u_stop(struct ieee80211_hw *hw)
38 {
39 struct mt76x2_dev *dev = hw->priv;
40
41 mutex_lock(&dev->mutex);
42 clear_bit(MT76_STATE_RUNNING, &dev->mt76.state);
43 mt76x2u_stop_hw(dev);
44 mutex_unlock(&dev->mutex);
45 }
46
mt76x2u_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)47 static int mt76x2u_add_interface(struct ieee80211_hw *hw,
48 struct ieee80211_vif *vif)
49 {
50 struct mt76x2_dev *dev = hw->priv;
51 struct mt76x2_vif *mvif = (struct mt76x2_vif *)vif->drv_priv;
52 unsigned int idx = 0;
53
54 if (!ether_addr_equal(dev->mt76.macaddr, vif->addr))
55 mt76x2u_mac_setaddr(dev, vif->addr);
56
57 mvif->idx = idx;
58 mvif->group_wcid.idx = MT_VIF_WCID(idx);
59 mvif->group_wcid.hw_key_idx = -1;
60 mt76x2_txq_init(dev, vif->txq);
61
62 return 0;
63 }
64
65 static int
mt76x2u_set_channel(struct mt76x2_dev * dev,struct cfg80211_chan_def * chandef)66 mt76x2u_set_channel(struct mt76x2_dev *dev,
67 struct cfg80211_chan_def *chandef)
68 {
69 int err;
70
71 cancel_delayed_work_sync(&dev->cal_work);
72 set_bit(MT76_RESET, &dev->mt76.state);
73
74 mt76_set_channel(&dev->mt76);
75
76 mt76_clear(dev, MT_TXOP_CTRL_CFG, BIT(20));
77 mt76_clear(dev, MT_TXOP_HLDR_ET, BIT(1));
78 mt76x2_mac_stop(dev, false);
79
80 err = mt76x2u_phy_set_channel(dev, chandef);
81
82 mt76x2u_mac_resume(dev);
83
84 clear_bit(MT76_RESET, &dev->mt76.state);
85 mt76_txq_schedule_all(&dev->mt76);
86
87 return err;
88 }
89
90 static void
mt76x2u_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u32 changed)91 mt76x2u_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
92 struct ieee80211_bss_conf *info, u32 changed)
93 {
94 struct mt76x2_dev *dev = hw->priv;
95
96 mutex_lock(&dev->mutex);
97
98 if (changed & BSS_CHANGED_ASSOC) {
99 mt76x2u_phy_channel_calibrate(dev);
100 mt76x2_apply_gain_adj(dev);
101 }
102
103 if (changed & BSS_CHANGED_BSSID) {
104 mt76_wr(dev, MT_MAC_BSSID_DW0,
105 get_unaligned_le32(info->bssid));
106 mt76_wr(dev, MT_MAC_BSSID_DW1,
107 get_unaligned_le16(info->bssid + 4));
108 }
109
110 mutex_unlock(&dev->mutex);
111 }
112
113 static int
mt76x2u_config(struct ieee80211_hw * hw,u32 changed)114 mt76x2u_config(struct ieee80211_hw *hw, u32 changed)
115 {
116 struct mt76x2_dev *dev = hw->priv;
117 int err = 0;
118
119 mutex_lock(&dev->mutex);
120
121 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
122 if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
123 dev->rxfilter |= MT_RX_FILTR_CFG_PROMISC;
124 else
125 dev->rxfilter &= ~MT_RX_FILTR_CFG_PROMISC;
126 mt76_wr(dev, MT_RX_FILTR_CFG, dev->rxfilter);
127 }
128
129 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
130 ieee80211_stop_queues(hw);
131 err = mt76x2u_set_channel(dev, &hw->conf.chandef);
132 ieee80211_wake_queues(hw);
133 }
134
135 if (changed & IEEE80211_CONF_CHANGE_POWER) {
136 dev->txpower_conf = hw->conf.power_level * 2;
137
138 /* convert to per-chain power for 2x2 devices */
139 dev->txpower_conf -= 6;
140
141 if (test_bit(MT76_STATE_RUNNING, &dev->mt76.state))
142 mt76x2_phy_set_txpower(dev);
143 }
144
145 mutex_unlock(&dev->mutex);
146
147 return err;
148 }
149
150 static void
mt76x2u_sw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const u8 * mac)151 mt76x2u_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
152 const u8 *mac)
153 {
154 struct mt76x2_dev *dev = hw->priv;
155
156 set_bit(MT76_SCANNING, &dev->mt76.state);
157 }
158
159 static void
mt76x2u_sw_scan_complete(struct ieee80211_hw * hw,struct ieee80211_vif * vif)160 mt76x2u_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
161 {
162 struct mt76x2_dev *dev = hw->priv;
163
164 clear_bit(MT76_SCANNING, &dev->mt76.state);
165 }
166
167 const struct ieee80211_ops mt76x2u_ops = {
168 .tx = mt76x2_tx,
169 .start = mt76x2u_start,
170 .stop = mt76x2u_stop,
171 .add_interface = mt76x2u_add_interface,
172 .remove_interface = mt76x2_remove_interface,
173 .sta_add = mt76x2_sta_add,
174 .sta_remove = mt76x2_sta_remove,
175 .set_key = mt76x2_set_key,
176 .ampdu_action = mt76x2_ampdu_action,
177 .config = mt76x2u_config,
178 .wake_tx_queue = mt76_wake_tx_queue,
179 .bss_info_changed = mt76x2u_bss_info_changed,
180 .configure_filter = mt76x2_configure_filter,
181 .conf_tx = mt76x2_conf_tx,
182 .sw_scan_start = mt76x2u_sw_scan,
183 .sw_scan_complete = mt76x2u_sw_scan_complete,
184 .sta_rate_tbl_update = mt76x2_sta_rate_tbl_update,
185 };
186