Path: blob/main/sys/contrib/dev/mediatek/mt76/mt7925/regd.c
96365 views
// SPDX-License-Identifier: BSD-3-Clause-Clear1/* Copyright (C) 2025 MediaTek Inc. */23#include "mt7925.h"4#include "regd.h"5#include "mcu.h"67static bool mt7925_disable_clc;8module_param_named(disable_clc, mt7925_disable_clc, bool, 0644);9MODULE_PARM_DESC(disable_clc, "disable CLC support");1011bool mt7925_regd_clc_supported(struct mt792x_dev *dev)12{13if (mt7925_disable_clc ||14mt76_is_usb(&dev->mt76))15return false;1617return true;18}1920void mt7925_regd_be_ctrl(struct mt792x_dev *dev, u8 *alpha2)21{22struct mt792x_phy *phy = &dev->phy;23struct mt7925_clc_rule_v2 *rule;24struct mt7925_clc *clc;25bool old = dev->has_eht, new = true;26u32 mtcl_conf = mt792x_acpi_get_mtcl_conf(&dev->phy, alpha2);27u8 *pos;2829if (mtcl_conf != MT792X_ACPI_MTCL_INVALID &&30(((mtcl_conf >> 4) & 0x3) == 0)) {31new = false;32goto out;33}3435if (!phy->clc[MT792x_CLC_BE_CTRL])36goto out;3738clc = (struct mt7925_clc *)phy->clc[MT792x_CLC_BE_CTRL];39pos = clc->data;4041while (1) {42rule = (struct mt7925_clc_rule_v2 *)pos;4344if (rule->alpha2[0] == alpha2[0] &&45rule->alpha2[1] == alpha2[1]) {46new = false;47break;48}4950/* Check the last one */51if (rule->flag & BIT(0))52break;5354pos += sizeof(*rule);55}5657out:58if (old == new)59return;6061dev->has_eht = new;62mt7925_set_stream_he_eht_caps(phy);63}6465static void66mt7925_regd_channel_update(struct wiphy *wiphy, struct mt792x_dev *dev)67{68#define IS_UNII_INVALID(idx, sfreq, efreq, cfreq) \69(!(dev->phy.clc_chan_conf & BIT(idx)) && (cfreq) >= (sfreq) && (cfreq) <= (efreq))70#define MT7925_UNII_59G_IS_VALID 0x171#define MT7925_UNII_6G_IS_VALID 0x1e72struct ieee80211_supported_band *sband;73struct mt76_dev *mdev = &dev->mt76;74struct ieee80211_channel *ch;75u32 mtcl_conf = mt792x_acpi_get_mtcl_conf(&dev->phy, mdev->alpha2);76int i;7778if (mtcl_conf != MT792X_ACPI_MTCL_INVALID) {79if ((mtcl_conf & 0x3) == 0)80dev->phy.clc_chan_conf &= ~MT7925_UNII_59G_IS_VALID;81if (((mtcl_conf >> 2) & 0x3) == 0)82dev->phy.clc_chan_conf &= ~MT7925_UNII_6G_IS_VALID;83}8485sband = wiphy->bands[NL80211_BAND_2GHZ];86if (!sband)87return;8889for (i = 0; i < sband->n_channels; i++) {90ch = &sband->channels[i];9192if (!dev->has_eht)93ch->flags |= IEEE80211_CHAN_NO_EHT;94}9596sband = wiphy->bands[NL80211_BAND_5GHZ];97if (!sband)98return;99100for (i = 0; i < sband->n_channels; i++) {101ch = &sband->channels[i];102103/* UNII-4 */104if (IS_UNII_INVALID(0, 5845, 5925, ch->center_freq))105ch->flags |= IEEE80211_CHAN_DISABLED;106107if (!dev->has_eht)108ch->flags |= IEEE80211_CHAN_NO_EHT;109}110111sband = wiphy->bands[NL80211_BAND_6GHZ];112if (!sband)113return;114115for (i = 0; i < sband->n_channels; i++) {116ch = &sband->channels[i];117118/* UNII-5/6/7/8 */119if (IS_UNII_INVALID(1, 5925, 6425, ch->center_freq) ||120IS_UNII_INVALID(2, 6425, 6525, ch->center_freq) ||121IS_UNII_INVALID(3, 6525, 6875, ch->center_freq) ||122IS_UNII_INVALID(4, 6875, 7125, ch->center_freq))123ch->flags |= IEEE80211_CHAN_DISABLED;124125if (!dev->has_eht)126ch->flags |= IEEE80211_CHAN_NO_EHT;127}128}129130int mt7925_mcu_regd_update(struct mt792x_dev *dev, u8 *alpha2,131enum environment_cap country_ie_env)132{133struct ieee80211_hw *hw = mt76_hw(dev);134struct wiphy *wiphy = hw->wiphy;135int ret = 0;136137dev->regd_in_progress = true;138139mt792x_mutex_acquire(dev);140if (!dev->regd_change)141goto err;142143ret = mt7925_mcu_set_clc(dev, alpha2, country_ie_env);144if (ret < 0)145goto err;146147mt7925_regd_be_ctrl(dev, alpha2);148mt7925_regd_channel_update(wiphy, dev);149150ret = mt7925_mcu_set_channel_domain(hw->priv);151if (ret < 0)152goto err;153154ret = mt7925_set_tx_sar_pwr(hw, NULL);155if (ret < 0)156goto err;157158err:159mt792x_mutex_release(dev);160dev->regd_change = false;161dev->regd_in_progress = false;162wake_up(&dev->wait);163164return ret;165}166EXPORT_SYMBOL_GPL(mt7925_mcu_regd_update);167168void mt7925_regd_notifier(struct wiphy *wiphy, struct regulatory_request *req)169{170struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);171struct mt792x_dev *dev = mt792x_hw_dev(hw);172struct mt76_connac_pm *pm = &dev->pm;173struct mt76_dev *mdev = &dev->mt76;174175if (req->initiator == NL80211_REGDOM_SET_BY_USER &&176!dev->regd_user)177dev->regd_user = true;178179/* allow world regdom at the first boot only */180if (!memcmp(req->alpha2, "00", 2) &&181mdev->alpha2[0] && mdev->alpha2[1])182return;183184/* do not need to update the same country twice */185if (!memcmp(req->alpha2, mdev->alpha2, 2) &&186dev->country_ie_env == req->country_ie_env)187return;188189memcpy(mdev->alpha2, req->alpha2, 2);190mdev->region = req->dfs_region;191dev->country_ie_env = req->country_ie_env;192193dev->regd_change = true;194195if (pm->suspended)196/* postpone the mcu update to resume */197return;198199mt7925_mcu_regd_update(dev, req->alpha2,200req->country_ie_env);201return;202}203204static bool205mt7925_regd_is_valid_alpha2(const char *alpha2)206{207if (!alpha2)208return false;209210if (alpha2[0] == '0' && alpha2[1] == '0')211return true;212213if (isalpha(alpha2[0]) && isalpha(alpha2[1]))214return true;215216return false;217}218219int mt7925_regd_change(struct mt792x_phy *phy, char *alpha2)220{221struct wiphy *wiphy = phy->mt76->hw->wiphy;222struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);223struct mt792x_dev *dev = mt792x_hw_dev(hw);224struct mt76_dev *mdev = &dev->mt76;225226if (dev->hw_full_reset)227return 0;228229if (!mt7925_regd_is_valid_alpha2(alpha2) ||230!mt7925_regd_clc_supported(dev) ||231dev->regd_user)232return -EINVAL;233234if (mdev->alpha2[0] != '0' && mdev->alpha2[1] != '0')235return 0;236237/* do not need to update the same country twice */238if (!memcmp(alpha2, mdev->alpha2, 2))239return 0;240241if (phy->chip_cap & MT792x_CHIP_CAP_11D_EN) {242return regulatory_hint(wiphy, alpha2);243} else {244return mt7925_mcu_set_clc(dev, alpha2, ENVIRON_INDOOR);245}246}247EXPORT_SYMBOL_GPL(mt7925_regd_change);248249int mt7925_regd_init(struct mt792x_phy *phy)250{251struct wiphy *wiphy = phy->mt76->hw->wiphy;252struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);253struct mt792x_dev *dev = mt792x_hw_dev(hw);254struct mt76_dev *mdev = &dev->mt76;255256if (phy->chip_cap & MT792x_CHIP_CAP_11D_EN) {257wiphy->regulatory_flags |= REGULATORY_COUNTRY_IE_IGNORE |258REGULATORY_DISABLE_BEACON_HINTS;259} else {260memzero_explicit(&mdev->alpha2, sizeof(mdev->alpha2));261}262263return 0;264}265266267