usb_main.c 3.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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"

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

24
	mutex_lock(&dev->mt76.mutex);
25 26 27 28 29

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

30
	ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mac_work,
31
				     MT_MAC_WORK_INTERVAL);
32 33 34
	set_bit(MT76_STATE_RUNNING, &dev->mt76.state);

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

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

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

static int
50
mt76x2u_set_channel(struct mt76x02_dev *dev,
51 52 53 54 55 56 57 58 59 60 61 62 63 64
		    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);

	mt76x2_mac_stop(dev, false);

	err = mt76x2u_phy_set_channel(dev, chandef);

	mt76x2u_mac_resume(dev);
65
	mt76x02_edcca_init(dev, true);
66 67 68 69 70 71 72 73 74 75

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

	return err;
}

static int
mt76x2u_config(struct ieee80211_hw *hw, u32 changed)
{
76
	struct mt76x02_dev *dev = hw->priv;
77 78
	int err = 0;

79
	mutex_lock(&dev->mt76.mutex);
80 81 82

	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
		if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
83
			dev->mt76.rxfilter |= MT_RX_FILTR_CFG_PROMISC;
84
		else
85 86
			dev->mt76.rxfilter &= ~MT_RX_FILTR_CFG_PROMISC;
		mt76_wr(dev, MT_RX_FILTR_CFG, dev->mt76.rxfilter);
87 88 89 90 91 92 93 94 95
	}

	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) {
96
		dev->mt76.txpower_conf = hw->conf.power_level * 2;
97 98

		/* convert to per-chain power for 2x2 devices */
99
		dev->mt76.txpower_conf -= 6;
100 101 102 103 104

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

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

	return err;
}

const struct ieee80211_ops mt76x2u_ops = {
111
	.tx = mt76x02_tx,
112 113
	.start = mt76x2u_start,
	.stop = mt76x2u_stop,
114
	.add_interface = mt76x02_add_interface,
115
	.remove_interface = mt76x02_remove_interface,
116
	.sta_state = mt76_sta_state,
Stanislaw Gruszka's avatar
Stanislaw Gruszka committed
117
	.set_key = mt76x02_set_key,
Stanislaw Gruszka's avatar
Stanislaw Gruszka committed
118
	.ampdu_action = mt76x02_ampdu_action,
119 120
	.config = mt76x2u_config,
	.wake_tx_queue = mt76_wake_tx_queue,
121
	.bss_info_changed = mt76x02_bss_info_changed,
122
	.configure_filter = mt76x02_configure_filter,
Stanislaw Gruszka's avatar
Stanislaw Gruszka committed
123
	.conf_tx = mt76x02_conf_tx,
124 125
	.sw_scan_start = mt76x02_sw_scan,
	.sw_scan_complete = mt76x02_sw_scan_complete,
126
	.sta_rate_tbl_update = mt76x02_sta_rate_tbl_update,
127
	.get_txpower = mt76_get_txpower,
128
};