xref: /linux/drivers/net/wireless/mediatek/mt76/mt76x2/usb_main.c (revision 9410645520e9b820069761f3450ef6661418e279)
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,bool suspend)25 static void mt76x2u_stop(struct ieee80211_hw *hw, bool suspend)
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 
mt76x2u_set_channel(struct mt76_phy * mphy)34 int mt76x2u_set_channel(struct mt76_phy *mphy)
35 {
36 	struct mt76x02_dev *dev = container_of(mphy->dev, struct mt76x02_dev, mt76);
37 	int err;
38 
39 	mt76x02_pre_tbtt_enable(dev, false);
40 	mt76x2_mac_stop(dev, false);
41 
42 	err = mt76x2u_phy_set_channel(dev, &mphy->chandef);
43 
44 	mt76x02_mac_cc_reset(dev);
45 	mt76x2_mac_resume(dev);
46 
47 	mt76x02_pre_tbtt_enable(dev, true);
48 
49 	return err;
50 }
51 
52 static int
mt76x2u_config(struct ieee80211_hw * hw,u32 changed)53 mt76x2u_config(struct ieee80211_hw *hw, u32 changed)
54 {
55 	struct mt76x02_dev *dev = hw->priv;
56 	int err = 0;
57 
58 	mutex_lock(&dev->mt76.mutex);
59 
60 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
61 		if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
62 			dev->mt76.rxfilter |= MT_RX_FILTR_CFG_PROMISC;
63 		else
64 			dev->mt76.rxfilter &= ~MT_RX_FILTR_CFG_PROMISC;
65 		mt76_wr(dev, MT_RX_FILTR_CFG, dev->mt76.rxfilter);
66 	}
67 
68 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
69 		struct mt76_phy *mphy = &dev->mphy;
70 
71 		dev->txpower_conf = hw->conf.power_level * 2;
72 		dev->txpower_conf = mt76_get_sar_power(mphy,
73 						       mphy->chandef.chan,
74 						       dev->txpower_conf);
75 		/* convert to per-chain power for 2x2 devices */
76 		dev->txpower_conf -= 6;
77 
78 		if (test_bit(MT76_STATE_RUNNING, &mphy->state))
79 			mt76x2_phy_set_txpower(dev);
80 	}
81 
82 	mutex_unlock(&dev->mt76.mutex);
83 
84 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL)
85 		mt76_update_channel(&dev->mphy);
86 
87 	return err;
88 }
89 
90 const struct ieee80211_ops mt76x2u_ops = {
91 	.add_chanctx = ieee80211_emulate_add_chanctx,
92 	.remove_chanctx = ieee80211_emulate_remove_chanctx,
93 	.change_chanctx = ieee80211_emulate_change_chanctx,
94 	.switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx,
95 	.tx = mt76x02_tx,
96 	.start = mt76x2u_start,
97 	.stop = mt76x2u_stop,
98 	.add_interface = mt76x02_add_interface,
99 	.remove_interface = mt76x02_remove_interface,
100 	.sta_state = mt76_sta_state,
101 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
102 	.set_key = mt76x02_set_key,
103 	.ampdu_action = mt76x02_ampdu_action,
104 	.config = mt76x2u_config,
105 	.wake_tx_queue = mt76_wake_tx_queue,
106 	.bss_info_changed = mt76x02_bss_info_changed,
107 	.configure_filter = mt76x02_configure_filter,
108 	.conf_tx = mt76x02_conf_tx,
109 	.sw_scan_start = mt76_sw_scan,
110 	.sw_scan_complete = mt76x02_sw_scan_complete,
111 	.sta_rate_tbl_update = mt76x02_sta_rate_tbl_update,
112 	.get_txpower = mt76_get_txpower,
113 	.get_survey = mt76_get_survey,
114 	.set_tim = mt76_set_tim,
115 	.release_buffered_frames = mt76_release_buffered_frames,
116 	.get_antenna = mt76_get_antenna,
117 	.set_sar_specs = mt76x2_set_sar_specs,
118 };
119