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