1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
4  */
5 
6 #include "mt76x2u.h"
7 #include "../mt76x02_usb.h"
8 
mt76x2u_start(struct ieee80211_hw * hw)9 static int mt76x2u_start(struct ieee80211_hw *hw)
10 {
11 	struct mt76x02_dev *dev = hw->priv;
12 	int ret;
13 
14 	ret = mt76x02u_mac_start(dev);
15 	if (ret)
16 		return ret;
17 
18 	ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mphy.mac_work,
19 				     MT_MAC_WORK_INTERVAL);
20 	set_bit(MT76_STATE_RUNNING, &dev->mphy.state);
21 
22 	return 0;
23 }
24 
mt76x2u_stop(struct ieee80211_hw * hw)25 static void mt76x2u_stop(struct ieee80211_hw *hw)
26 {
27 	struct mt76x02_dev *dev = hw->priv;
28 
29 	clear_bit(MT76_STATE_RUNNING, &dev->mphy.state);
30 	mt76u_stop_tx(&dev->mt76);
31 	mt76x2u_stop_hw(dev);
32 }
33 
34 static int
mt76x2u_set_channel(struct mt76x02_dev * dev,struct cfg80211_chan_def * chandef)35 mt76x2u_set_channel(struct mt76x02_dev *dev,
36 		    struct cfg80211_chan_def *chandef)
37 {
38 	int err;
39 
40 	cancel_delayed_work_sync(&dev->cal_work);
41 	mt76x02_pre_tbtt_enable(dev, false);
42 
43 	mutex_lock(&dev->mt76.mutex);
44 	set_bit(MT76_RESET, &dev->mphy.state);
45 
46 	mt76_set_channel(&dev->mphy);
47 
48 	mt76x2_mac_stop(dev, false);
49 
50 	err = mt76x2u_phy_set_channel(dev, chandef);
51 
52 	mt76x02_mac_cc_reset(dev);
53 	mt76x2_mac_resume(dev);
54 
55 	clear_bit(MT76_RESET, &dev->mphy.state);
56 	mutex_unlock(&dev->mt76.mutex);
57 
58 	mt76x02_pre_tbtt_enable(dev, true);
59 	mt76_txq_schedule_all(&dev->mphy);
60 
61 	return err;
62 }
63 
64 static int
mt76x2u_config(struct ieee80211_hw * hw,u32 changed)65 mt76x2u_config(struct ieee80211_hw *hw, u32 changed)
66 {
67 	struct mt76x02_dev *dev = hw->priv;
68 	int err = 0;
69 
70 	mutex_lock(&dev->mt76.mutex);
71 
72 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
73 		if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
74 			dev->mt76.rxfilter |= MT_RX_FILTR_CFG_PROMISC;
75 		else
76 			dev->mt76.rxfilter &= ~MT_RX_FILTR_CFG_PROMISC;
77 		mt76_wr(dev, MT_RX_FILTR_CFG, dev->mt76.rxfilter);
78 	}
79 
80 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
81 		struct mt76_phy *mphy = &dev->mphy;
82 
83 		dev->txpower_conf = hw->conf.power_level * 2;
84 		dev->txpower_conf = mt76_get_sar_power(mphy,
85 						       mphy->chandef.chan,
86 						       dev->txpower_conf);
87 		/* convert to per-chain power for 2x2 devices */
88 		dev->txpower_conf -= 6;
89 
90 		if (test_bit(MT76_STATE_RUNNING, &mphy->state))
91 			mt76x2_phy_set_txpower(dev);
92 	}
93 
94 	mutex_unlock(&dev->mt76.mutex);
95 
96 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
97 		ieee80211_stop_queues(hw);
98 		err = mt76x2u_set_channel(dev, &hw->conf.chandef);
99 		ieee80211_wake_queues(hw);
100 	}
101 
102 	return err;
103 }
104 
105 const struct ieee80211_ops mt76x2u_ops = {
106 	.tx = mt76x02_tx,
107 	.start = mt76x2u_start,
108 	.stop = mt76x2u_stop,
109 	.add_interface = mt76x02_add_interface,
110 	.remove_interface = mt76x02_remove_interface,
111 	.sta_state = mt76_sta_state,
112 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
113 	.set_key = mt76x02_set_key,
114 	.ampdu_action = mt76x02_ampdu_action,
115 	.config = mt76x2u_config,
116 	.wake_tx_queue = mt76_wake_tx_queue,
117 	.bss_info_changed = mt76x02_bss_info_changed,
118 	.configure_filter = mt76x02_configure_filter,
119 	.conf_tx = mt76x02_conf_tx,
120 	.sw_scan_start = mt76_sw_scan,
121 	.sw_scan_complete = mt76x02_sw_scan_complete,
122 	.sta_rate_tbl_update = mt76x02_sta_rate_tbl_update,
123 	.get_txpower = mt76_get_txpower,
124 	.get_survey = mt76_get_survey,
125 	.set_tim = mt76_set_tim,
126 	.release_buffered_frames = mt76_release_buffered_frames,
127 	.get_antenna = mt76_get_antenna,
128 	.set_sar_specs = mt76x2_set_sar_specs,
129 };
130