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 
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->mt76.mac_work,
19 				     MT_MAC_WORK_INTERVAL);
20 	set_bit(MT76_STATE_RUNNING, &dev->mphy.state);
21 
22 	return 0;
23 }
24 
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
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
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 		dev->txpower_conf = hw->conf.power_level * 2;
82 
83 		/* convert to per-chain power for 2x2 devices */
84 		dev->txpower_conf -= 6;
85 
86 		if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state))
87 			mt76x2_phy_set_txpower(dev);
88 	}
89 
90 	mutex_unlock(&dev->mt76.mutex);
91 
92 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
93 		ieee80211_stop_queues(hw);
94 		err = mt76x2u_set_channel(dev, &hw->conf.chandef);
95 		ieee80211_wake_queues(hw);
96 	}
97 
98 	return err;
99 }
100 
101 const struct ieee80211_ops mt76x2u_ops = {
102 	.tx = mt76x02_tx,
103 	.start = mt76x2u_start,
104 	.stop = mt76x2u_stop,
105 	.add_interface = mt76x02_add_interface,
106 	.remove_interface = mt76x02_remove_interface,
107 	.sta_state = mt76_sta_state,
108 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
109 	.set_key = mt76x02_set_key,
110 	.ampdu_action = mt76x02_ampdu_action,
111 	.config = mt76x2u_config,
112 	.wake_tx_queue = mt76_wake_tx_queue,
113 	.bss_info_changed = mt76x02_bss_info_changed,
114 	.configure_filter = mt76x02_configure_filter,
115 	.conf_tx = mt76x02_conf_tx,
116 	.sw_scan_start = mt76_sw_scan,
117 	.sw_scan_complete = mt76x02_sw_scan_complete,
118 	.sta_rate_tbl_update = mt76x02_sta_rate_tbl_update,
119 	.get_txpower = mt76_get_txpower,
120 	.get_survey = mt76_get_survey,
121 	.set_tim = mt76_set_tim,
122 	.release_buffered_frames = mt76_release_buffered_frames,
123 	.get_antenna = mt76_get_antenna,
124 };
125