usb_main.c 4.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "mt76x2u.h"
18
#include "../mt76x02_util.h"
19 20 21

static int mt76x2u_start(struct ieee80211_hw *hw)
{
22
	struct mt76x02_dev *dev = hw->priv;
23 24
	int ret;

25
	mutex_lock(&dev->mt76.mutex);
26 27 28 29 30 31 32 33

	ret = mt76x2u_mac_start(dev);
	if (ret)
		goto out;

	set_bit(MT76_STATE_RUNNING, &dev->mt76.state);

out:
34
	mutex_unlock(&dev->mt76.mutex);
35 36 37 38 39
	return ret;
}

static void mt76x2u_stop(struct ieee80211_hw *hw)
{
40
	struct mt76x02_dev *dev = hw->priv;
41

42
	mutex_lock(&dev->mt76.mutex);
43 44
	clear_bit(MT76_STATE_RUNNING, &dev->mt76.state);
	mt76x2u_stop_hw(dev);
45
	mutex_unlock(&dev->mt76.mutex);
46 47 48 49 50
}

static int mt76x2u_add_interface(struct ieee80211_hw *hw,
				 struct ieee80211_vif *vif)
{
51
	struct mt76x02_dev *dev = hw->priv;
52 53

	if (!ether_addr_equal(dev->mt76.macaddr, vif->addr))
54
		mt76x02_mac_setaddr(&dev->mt76, vif->addr);
55

56
	mt76x02_vif_init(&dev->mt76, vif, 0);
57 58 59 60
	return 0;
}

static int
61
mt76x2u_set_channel(struct mt76x02_dev *dev,
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
		    struct cfg80211_chan_def *chandef)
{
	int err;

	cancel_delayed_work_sync(&dev->cal_work);
	set_bit(MT76_RESET, &dev->mt76.state);

	mt76_set_channel(&dev->mt76);

	mt76_clear(dev, MT_TXOP_CTRL_CFG, BIT(20));
	mt76_clear(dev, MT_TXOP_HLDR_ET, BIT(1));
	mt76x2_mac_stop(dev, false);

	err = mt76x2u_phy_set_channel(dev, chandef);

	mt76x2u_mac_resume(dev);

	clear_bit(MT76_RESET, &dev->mt76.state);
	mt76_txq_schedule_all(&dev->mt76);

	return err;
}

static void
mt76x2u_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
			 struct ieee80211_bss_conf *info, u32 changed)
{
89
	struct mt76x02_dev *dev = hw->priv;
90

91
	mutex_lock(&dev->mt76.mutex);
92 93 94 95 96 97 98 99 100 101 102 103 104

	if (changed & BSS_CHANGED_ASSOC) {
		mt76x2u_phy_channel_calibrate(dev);
		mt76x2_apply_gain_adj(dev);
	}

	if (changed & BSS_CHANGED_BSSID) {
		mt76_wr(dev, MT_MAC_BSSID_DW0,
			get_unaligned_le32(info->bssid));
		mt76_wr(dev, MT_MAC_BSSID_DW1,
			get_unaligned_le16(info->bssid + 4));
	}

105
	mutex_unlock(&dev->mt76.mutex);
106 107 108 109 110
}

static int
mt76x2u_config(struct ieee80211_hw *hw, u32 changed)
{
111
	struct mt76x02_dev *dev = hw->priv;
112 113
	int err = 0;

114
	mutex_lock(&dev->mt76.mutex);
115 116 117

	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
		if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
118
			dev->mt76.rxfilter |= MT_RX_FILTR_CFG_PROMISC;
119
		else
120 121
			dev->mt76.rxfilter &= ~MT_RX_FILTR_CFG_PROMISC;
		mt76_wr(dev, MT_RX_FILTR_CFG, dev->mt76.rxfilter);
122 123 124 125 126 127 128 129 130
	}

	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
		ieee80211_stop_queues(hw);
		err = mt76x2u_set_channel(dev, &hw->conf.chandef);
		ieee80211_wake_queues(hw);
	}

	if (changed & IEEE80211_CONF_CHANGE_POWER) {
131
		dev->mt76.txpower_conf = hw->conf.power_level * 2;
132 133

		/* convert to per-chain power for 2x2 devices */
134
		dev->mt76.txpower_conf -= 6;
135 136 137 138 139

		if (test_bit(MT76_STATE_RUNNING, &dev->mt76.state))
			mt76x2_phy_set_txpower(dev);
	}

140
	mutex_unlock(&dev->mt76.mutex);
141 142 143 144 145 146 147 148

	return err;
}

static void
mt76x2u_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
		const u8 *mac)
{
149
	struct mt76x02_dev *dev = hw->priv;
150 151 152 153 154 155 156

	set_bit(MT76_SCANNING, &dev->mt76.state);
}

static void
mt76x2u_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
{
157
	struct mt76x02_dev *dev = hw->priv;
158 159 160 161 162 163 164 165 166

	clear_bit(MT76_SCANNING, &dev->mt76.state);
}

const struct ieee80211_ops mt76x2u_ops = {
	.tx = mt76x2_tx,
	.start = mt76x2u_start,
	.stop = mt76x2u_stop,
	.add_interface = mt76x2u_add_interface,
167
	.remove_interface = mt76x02_remove_interface,
168 169
	.sta_add = mt76x02_sta_add,
	.sta_remove = mt76x02_sta_remove,
Stanislaw Gruszka's avatar
Stanislaw Gruszka committed
170
	.set_key = mt76x02_set_key,
Stanislaw Gruszka's avatar
Stanislaw Gruszka committed
171
	.ampdu_action = mt76x02_ampdu_action,
172 173 174
	.config = mt76x2u_config,
	.wake_tx_queue = mt76_wake_tx_queue,
	.bss_info_changed = mt76x2u_bss_info_changed,
175
	.configure_filter = mt76x02_configure_filter,
Stanislaw Gruszka's avatar
Stanislaw Gruszka committed
176
	.conf_tx = mt76x02_conf_tx,
177 178
	.sw_scan_start = mt76x2u_sw_scan,
	.sw_scan_complete = mt76x2u_sw_scan_complete,
179
	.sta_rate_tbl_update = mt76x02_sta_rate_tbl_update,
180
};