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