1 // SPDX-License-Identifier: BSD-3-Clause-Clear 2 /* Copyright (C) 2025 MediaTek Inc. */ 3 4 #include "mt7925.h" 5 #include "regd.h" 6 #include "mcu.h" 7 8 static bool mt7925_disable_clc; 9 module_param_named(disable_clc, mt7925_disable_clc, bool, 0644); 10 MODULE_PARM_DESC(disable_clc, "disable CLC support"); 11 12 bool mt7925_regd_clc_supported(struct mt792x_dev *dev) 13 { 14 if (mt7925_disable_clc || 15 mt76_is_usb(&dev->mt76)) 16 return false; 17 18 return true; 19 } 20 21 void mt7925_regd_be_ctrl(struct mt792x_dev *dev, u8 *alpha2) 22 { 23 struct mt792x_phy *phy = &dev->phy; 24 struct mt7925_clc_rule_v2 *rule; 25 struct mt7925_clc *clc; 26 bool old = dev->has_eht, new = true; 27 u32 mtcl_conf = mt792x_acpi_get_mtcl_conf(&dev->phy, alpha2); 28 u8 *pos; 29 30 if (mtcl_conf != MT792X_ACPI_MTCL_INVALID && 31 (((mtcl_conf >> 4) & 0x3) == 0)) { 32 new = false; 33 goto out; 34 } 35 36 if (!phy->clc[MT792x_CLC_BE_CTRL]) 37 goto out; 38 39 clc = (struct mt7925_clc *)phy->clc[MT792x_CLC_BE_CTRL]; 40 pos = clc->data; 41 42 while (1) { 43 rule = (struct mt7925_clc_rule_v2 *)pos; 44 45 if (rule->alpha2[0] == alpha2[0] && 46 rule->alpha2[1] == alpha2[1]) { 47 new = false; 48 break; 49 } 50 51 /* Check the last one */ 52 if (rule->flag & BIT(0)) 53 break; 54 55 pos += sizeof(*rule); 56 } 57 58 out: 59 if (old == new) 60 return; 61 62 dev->has_eht = new; 63 mt7925_set_stream_he_eht_caps(phy); 64 } 65 66 static void 67 mt7925_regd_channel_update(struct wiphy *wiphy, struct mt792x_dev *dev) 68 { 69 #define IS_UNII_INVALID(idx, sfreq, efreq, cfreq) \ 70 (!(dev->phy.clc_chan_conf & BIT(idx)) && (cfreq) >= (sfreq) && (cfreq) <= (efreq)) 71 #define MT7925_UNII_59G_IS_VALID 0x1 72 #define MT7925_UNII_6G_IS_VALID 0x1e 73 struct ieee80211_supported_band *sband; 74 struct mt76_dev *mdev = &dev->mt76; 75 struct ieee80211_channel *ch; 76 u32 mtcl_conf = mt792x_acpi_get_mtcl_conf(&dev->phy, mdev->alpha2); 77 int i; 78 79 if (mtcl_conf != MT792X_ACPI_MTCL_INVALID) { 80 if ((mtcl_conf & 0x3) == 0) 81 dev->phy.clc_chan_conf &= ~MT7925_UNII_59G_IS_VALID; 82 if (((mtcl_conf >> 2) & 0x3) == 0) 83 dev->phy.clc_chan_conf &= ~MT7925_UNII_6G_IS_VALID; 84 } 85 86 sband = wiphy->bands[NL80211_BAND_2GHZ]; 87 if (!sband) 88 return; 89 90 for (i = 0; i < sband->n_channels; i++) { 91 ch = &sband->channels[i]; 92 93 if (!dev->has_eht) 94 ch->flags |= IEEE80211_CHAN_NO_EHT; 95 } 96 97 sband = wiphy->bands[NL80211_BAND_5GHZ]; 98 if (!sband) 99 return; 100 101 for (i = 0; i < sband->n_channels; i++) { 102 ch = &sband->channels[i]; 103 104 /* UNII-4 */ 105 if (IS_UNII_INVALID(0, 5845, 5925, ch->center_freq)) 106 ch->flags |= IEEE80211_CHAN_DISABLED; 107 108 if (!dev->has_eht) 109 ch->flags |= IEEE80211_CHAN_NO_EHT; 110 } 111 112 sband = wiphy->bands[NL80211_BAND_6GHZ]; 113 if (!sband) 114 return; 115 116 for (i = 0; i < sband->n_channels; i++) { 117 ch = &sband->channels[i]; 118 119 /* UNII-5/6/7/8 */ 120 if (IS_UNII_INVALID(1, 5925, 6425, ch->center_freq) || 121 IS_UNII_INVALID(2, 6425, 6525, ch->center_freq) || 122 IS_UNII_INVALID(3, 6525, 6875, ch->center_freq) || 123 IS_UNII_INVALID(4, 6875, 7125, ch->center_freq)) 124 ch->flags |= IEEE80211_CHAN_DISABLED; 125 126 if (!dev->has_eht) 127 ch->flags |= IEEE80211_CHAN_NO_EHT; 128 } 129 } 130 131 int mt7925_mcu_regd_update(struct mt792x_dev *dev, u8 *alpha2, 132 enum environment_cap country_ie_env) 133 { 134 struct ieee80211_hw *hw = mt76_hw(dev); 135 struct wiphy *wiphy = hw->wiphy; 136 int ret = 0; 137 138 dev->regd_in_progress = true; 139 140 mt792x_mutex_acquire(dev); 141 if (!dev->regd_change) 142 goto err; 143 144 ret = mt7925_mcu_set_clc(dev, alpha2, country_ie_env); 145 if (ret < 0) 146 goto err; 147 148 mt7925_regd_be_ctrl(dev, alpha2); 149 mt7925_regd_channel_update(wiphy, dev); 150 151 ret = mt7925_mcu_set_channel_domain(hw->priv); 152 if (ret < 0) 153 goto err; 154 155 ret = mt7925_set_tx_sar_pwr(hw, NULL); 156 if (ret < 0) 157 goto err; 158 159 err: 160 mt792x_mutex_release(dev); 161 dev->regd_change = false; 162 dev->regd_in_progress = false; 163 wake_up(&dev->wait); 164 165 return ret; 166 } 167 EXPORT_SYMBOL_GPL(mt7925_mcu_regd_update); 168 169 void mt7925_regd_notifier(struct wiphy *wiphy, struct regulatory_request *req) 170 { 171 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy); 172 struct mt792x_dev *dev = mt792x_hw_dev(hw); 173 struct mt76_connac_pm *pm = &dev->pm; 174 struct mt76_dev *mdev = &dev->mt76; 175 176 if (req->initiator == NL80211_REGDOM_SET_BY_USER && 177 !dev->regd_user) 178 dev->regd_user = true; 179 180 /* allow world regdom at the first boot only */ 181 if (!memcmp(req->alpha2, "00", 2) && 182 mdev->alpha2[0] && mdev->alpha2[1]) 183 return; 184 185 /* do not need to update the same country twice */ 186 if (!memcmp(req->alpha2, mdev->alpha2, 2) && 187 dev->country_ie_env == req->country_ie_env) 188 return; 189 190 memcpy(mdev->alpha2, req->alpha2, 2); 191 mdev->region = req->dfs_region; 192 dev->country_ie_env = req->country_ie_env; 193 194 dev->regd_change = true; 195 196 if (pm->suspended) 197 /* postpone the mcu update to resume */ 198 return; 199 200 mt7925_mcu_regd_update(dev, req->alpha2, 201 req->country_ie_env); 202 return; 203 } 204 205 static bool 206 mt7925_regd_is_valid_alpha2(const char *alpha2) 207 { 208 if (!alpha2) 209 return false; 210 211 if (alpha2[0] == '0' && alpha2[1] == '0') 212 return true; 213 214 if (isalpha(alpha2[0]) && isalpha(alpha2[1])) 215 return true; 216 217 return false; 218 } 219 220 int mt7925_regd_change(struct mt792x_phy *phy, char *alpha2) 221 { 222 struct wiphy *wiphy = phy->mt76->hw->wiphy; 223 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy); 224 struct mt792x_dev *dev = mt792x_hw_dev(hw); 225 struct mt76_dev *mdev = &dev->mt76; 226 227 if (dev->hw_full_reset) 228 return 0; 229 230 if (!mt7925_regd_is_valid_alpha2(alpha2) || 231 !mt7925_regd_clc_supported(dev) || 232 dev->regd_user) 233 return -EINVAL; 234 235 if (mdev->alpha2[0] != '0' && mdev->alpha2[1] != '0') 236 return 0; 237 238 /* do not need to update the same country twice */ 239 if (!memcmp(alpha2, mdev->alpha2, 2)) 240 return 0; 241 242 if (phy->chip_cap & MT792x_CHIP_CAP_11D_EN) { 243 return regulatory_hint(wiphy, alpha2); 244 } else { 245 return mt7925_mcu_set_clc(dev, alpha2, ENVIRON_INDOOR); 246 } 247 } 248 EXPORT_SYMBOL_GPL(mt7925_regd_change); 249 250 int mt7925_regd_init(struct mt792x_phy *phy) 251 { 252 struct wiphy *wiphy = phy->mt76->hw->wiphy; 253 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy); 254 struct mt792x_dev *dev = mt792x_hw_dev(hw); 255 struct mt76_dev *mdev = &dev->mt76; 256 257 if (phy->chip_cap & MT792x_CHIP_CAP_11D_EN) { 258 wiphy->regulatory_flags |= REGULATORY_COUNTRY_IE_IGNORE | 259 REGULATORY_DISABLE_BEACON_HINTS; 260 } else { 261 memzero_explicit(&mdev->alpha2, sizeof(mdev->alpha2)); 262 } 263 264 return 0; 265 } 266