Path: blob/main/sys/contrib/dev/mediatek/mt76/mt7925/regd.c
106882 views
// SPDX-License-Identifier: BSD-3-Clause-Clear1/* Copyright (C) 2025 MediaTek Inc. */23#if defined(__FreeBSD__)4#define LINUXKPI_PARAM_PREFIX mt7925_5#endif67#include "mt7925.h"8#include "regd.h"9#include "mcu.h"1011static bool mt7925_disable_clc;12module_param_named(disable_clc, mt7925_disable_clc, bool, 0644);13MODULE_PARM_DESC(disable_clc, "disable CLC support");1415bool mt7925_regd_clc_supported(struct mt792x_dev *dev)16{17if (mt7925_disable_clc ||18mt76_is_usb(&dev->mt76))19return false;2021return true;22}2324void mt7925_regd_be_ctrl(struct mt792x_dev *dev, u8 *alpha2)25{26struct mt792x_phy *phy = &dev->phy;27struct mt7925_clc_rule_v2 *rule;28struct mt7925_clc *clc;29bool old = dev->has_eht, new = true;30u32 mtcl_conf = mt792x_acpi_get_mtcl_conf(&dev->phy, alpha2);31u8 *pos;3233if (mtcl_conf != MT792X_ACPI_MTCL_INVALID &&34(((mtcl_conf >> 4) & 0x3) == 0)) {35new = false;36goto out;37}3839if (!phy->clc[MT792x_CLC_BE_CTRL])40goto out;4142clc = (struct mt7925_clc *)phy->clc[MT792x_CLC_BE_CTRL];43pos = clc->data;4445while (1) {46rule = (struct mt7925_clc_rule_v2 *)pos;4748if (rule->alpha2[0] == alpha2[0] &&49rule->alpha2[1] == alpha2[1]) {50new = false;51break;52}5354/* Check the last one */55if (rule->flag & BIT(0))56break;5758pos += sizeof(*rule);59}6061out:62if (old == new)63return;6465dev->has_eht = new;66mt7925_set_stream_he_eht_caps(phy);67}6869static void70mt7925_regd_channel_update(struct wiphy *wiphy, struct mt792x_dev *dev)71{72#define IS_UNII_INVALID(idx, sfreq, efreq, cfreq) \73(!(dev->phy.clc_chan_conf & BIT(idx)) && (cfreq) >= (sfreq) && (cfreq) <= (efreq))74#define MT7925_UNII_59G_IS_VALID 0x175#define MT7925_UNII_6G_IS_VALID 0x1e76struct ieee80211_supported_band *sband;77struct mt76_dev *mdev = &dev->mt76;78struct ieee80211_channel *ch;79u32 mtcl_conf = mt792x_acpi_get_mtcl_conf(&dev->phy, mdev->alpha2);80int i;8182if (mtcl_conf != MT792X_ACPI_MTCL_INVALID) {83if ((mtcl_conf & 0x3) == 0)84dev->phy.clc_chan_conf &= ~MT7925_UNII_59G_IS_VALID;85if (((mtcl_conf >> 2) & 0x3) == 0)86dev->phy.clc_chan_conf &= ~MT7925_UNII_6G_IS_VALID;87}8889sband = wiphy->bands[NL80211_BAND_2GHZ];90if (!sband)91return;9293for (i = 0; i < sband->n_channels; i++) {94ch = &sband->channels[i];9596if (!dev->has_eht)97ch->flags |= IEEE80211_CHAN_NO_EHT;98}99100sband = wiphy->bands[NL80211_BAND_5GHZ];101if (!sband)102return;103104for (i = 0; i < sband->n_channels; i++) {105ch = &sband->channels[i];106107/* UNII-4 */108if (IS_UNII_INVALID(0, 5845, 5925, ch->center_freq))109ch->flags |= IEEE80211_CHAN_DISABLED;110111if (!dev->has_eht)112ch->flags |= IEEE80211_CHAN_NO_EHT;113}114115sband = wiphy->bands[NL80211_BAND_6GHZ];116if (!sband)117return;118119for (i = 0; i < sband->n_channels; i++) {120ch = &sband->channels[i];121122/* UNII-5/6/7/8 */123if (IS_UNII_INVALID(1, 5925, 6425, ch->center_freq) ||124IS_UNII_INVALID(2, 6425, 6525, ch->center_freq) ||125IS_UNII_INVALID(3, 6525, 6875, ch->center_freq) ||126IS_UNII_INVALID(4, 6875, 7125, ch->center_freq))127ch->flags |= IEEE80211_CHAN_DISABLED;128129if (!dev->has_eht)130ch->flags |= IEEE80211_CHAN_NO_EHT;131}132}133134int mt7925_mcu_regd_update(struct mt792x_dev *dev, u8 *alpha2,135enum environment_cap country_ie_env)136{137struct ieee80211_hw *hw = mt76_hw(dev);138struct wiphy *wiphy = hw->wiphy;139int ret = 0;140141dev->regd_in_progress = true;142143mt792x_mutex_acquire(dev);144if (!dev->regd_change)145goto err;146147ret = mt7925_mcu_set_clc(dev, alpha2, country_ie_env);148if (ret < 0)149goto err;150151mt7925_regd_be_ctrl(dev, alpha2);152mt7925_regd_channel_update(wiphy, dev);153154ret = mt7925_mcu_set_channel_domain(hw->priv);155if (ret < 0)156goto err;157158ret = mt7925_set_tx_sar_pwr(hw, NULL);159if (ret < 0)160goto err;161162err:163mt792x_mutex_release(dev);164dev->regd_change = false;165dev->regd_in_progress = false;166wake_up(&dev->wait);167168return ret;169}170EXPORT_SYMBOL_GPL(mt7925_mcu_regd_update);171172void mt7925_regd_notifier(struct wiphy *wiphy, struct regulatory_request *req)173{174struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);175struct mt792x_dev *dev = mt792x_hw_dev(hw);176struct mt76_connac_pm *pm = &dev->pm;177struct mt76_dev *mdev = &dev->mt76;178179if (req->initiator == NL80211_REGDOM_SET_BY_USER &&180!dev->regd_user)181dev->regd_user = true;182183/* allow world regdom at the first boot only */184if (!memcmp(req->alpha2, "00", 2) &&185mdev->alpha2[0] && mdev->alpha2[1])186return;187188/* do not need to update the same country twice */189if (!memcmp(req->alpha2, mdev->alpha2, 2) &&190dev->country_ie_env == req->country_ie_env)191return;192193memcpy(mdev->alpha2, req->alpha2, 2);194mdev->region = req->dfs_region;195dev->country_ie_env = req->country_ie_env;196197dev->regd_change = true;198199if (pm->suspended)200/* postpone the mcu update to resume */201return;202203mt7925_mcu_regd_update(dev, req->alpha2,204req->country_ie_env);205return;206}207208static bool209mt7925_regd_is_valid_alpha2(const char *alpha2)210{211if (!alpha2)212return false;213214if (alpha2[0] == '0' && alpha2[1] == '0')215return true;216217if (isalpha(alpha2[0]) && isalpha(alpha2[1]))218return true;219220return false;221}222223int mt7925_regd_change(struct mt792x_phy *phy, char *alpha2)224{225struct wiphy *wiphy = phy->mt76->hw->wiphy;226struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);227struct mt792x_dev *dev = mt792x_hw_dev(hw);228struct mt76_dev *mdev = &dev->mt76;229230if (dev->hw_full_reset)231return 0;232233if (!mt7925_regd_is_valid_alpha2(alpha2) ||234!mt7925_regd_clc_supported(dev) ||235dev->regd_user)236return -EINVAL;237238if (mdev->alpha2[0] != '0' && mdev->alpha2[1] != '0')239return 0;240241/* do not need to update the same country twice */242if (!memcmp(alpha2, mdev->alpha2, 2))243return 0;244245if (phy->chip_cap & MT792x_CHIP_CAP_11D_EN) {246return regulatory_hint(wiphy, alpha2);247} else {248return mt7925_mcu_set_clc(dev, alpha2, ENVIRON_INDOOR);249}250}251EXPORT_SYMBOL_GPL(mt7925_regd_change);252253int mt7925_regd_init(struct mt792x_phy *phy)254{255struct wiphy *wiphy = phy->mt76->hw->wiphy;256struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);257struct mt792x_dev *dev = mt792x_hw_dev(hw);258struct mt76_dev *mdev = &dev->mt76;259260if (phy->chip_cap & MT792x_CHIP_CAP_11D_EN) {261wiphy->regulatory_flags |= REGULATORY_COUNTRY_IE_IGNORE |262REGULATORY_DISABLE_BEACON_HINTS;263} else {264memzero_explicit(&mdev->alpha2, sizeof(mdev->alpha2));265}266267return 0;268}269270271