mac80211: backport some upstream EHT patches
[openwrt/staging/blocktrron.git] / package / kernel / mac80211 / patches / subsys / 500-mac80211_configure_antenna_gain.patch
1 --- a/include/net/cfg80211.h
2 +++ b/include/net/cfg80211.h
3 @@ -4092,6 +4092,7 @@ struct mgmt_frame_regs {
4 * (as advertised by the nl80211 feature flag.)
5 * @get_tx_power: store the current TX power into the dbm variable;
6 * return 0 if successful
7 + * @set_antenna_gain: set antenna gain to reduce maximum tx power if necessary
8 *
9 * @rfkill_poll: polls the hw rfkill line, use cfg80211 reporting
10 * functions to adjust rfkill hw state
11 @@ -4442,6 +4443,7 @@ struct cfg80211_ops {
12 enum nl80211_tx_power_setting type, int mbm);
13 int (*get_tx_power)(struct wiphy *wiphy, struct wireless_dev *wdev,
14 int *dbm);
15 + int (*set_antenna_gain)(struct wiphy *wiphy, int dbi);
16
17 void (*rfkill_poll)(struct wiphy *wiphy);
18
19 --- a/include/net/mac80211.h
20 +++ b/include/net/mac80211.h
21 @@ -1705,6 +1705,7 @@ enum ieee80211_smps_mode {
22 *
23 * @power_level: requested transmit power (in dBm), backward compatibility
24 * value only that is set to the minimum of all interfaces
25 + * @max_antenna_gain: maximum antenna gain adjusted by user config (in dBi)
26 *
27 * @chandef: the channel definition to tune to
28 * @radar_enabled: whether radar detection is enabled
29 @@ -1725,6 +1726,7 @@ enum ieee80211_smps_mode {
30 struct ieee80211_conf {
31 u32 flags;
32 int power_level, dynamic_ps_timeout;
33 + int max_antenna_gain;
34
35 u16 listen_interval;
36 u8 ps_dtim_period;
37 --- a/include/uapi/linux/nl80211.h
38 +++ b/include/uapi/linux/nl80211.h
39 @@ -2756,6 +2756,8 @@ enum nl80211_commands {
40 * bit corresponds to the lowest 20 MHz channel. Each bit set to 1
41 * indicates that the sub-channel is punctured. Higher 16 bits are
42 * reserved.
43 + * @NL80211_ATTR_WIPHY_ANTENNA_GAIN: Configured antenna gain. Used to reduce
44 + * transmit power to stay within regulatory limits. u32, dBi.
45 *
46 * @NUM_NL80211_ATTR: total number of nl80211_attrs available
47 * @NL80211_ATTR_MAX: highest attribute number currently defined
48 @@ -3288,6 +3290,8 @@ enum nl80211_attrs {
49
50 NL80211_ATTR_PUNCT_BITMAP,
51
52 + NL80211_ATTR_WIPHY_ANTENNA_GAIN,
53 +
54 /* add attributes here, update the policy in nl80211.c */
55
56 __NL80211_ATTR_AFTER_LAST,
57 --- a/net/mac80211/cfg.c
58 +++ b/net/mac80211/cfg.c
59 @@ -3067,6 +3067,19 @@ static int ieee80211_get_tx_power(struct
60 return 0;
61 }
62
63 +static int ieee80211_set_antenna_gain(struct wiphy *wiphy, int dbi)
64 +{
65 + struct ieee80211_local *local = wiphy_priv(wiphy);
66 +
67 + if (dbi < 0)
68 + return -EINVAL;
69 +
70 + local->user_antenna_gain = dbi;
71 + ieee80211_hw_config(local, 0);
72 +
73 + return 0;
74 +}
75 +
76 static void ieee80211_rfkill_poll(struct wiphy *wiphy)
77 {
78 struct ieee80211_local *local = wiphy_priv(wiphy);
79 @@ -4989,6 +5002,7 @@ const struct cfg80211_ops mac80211_confi
80 .set_wiphy_params = ieee80211_set_wiphy_params,
81 .set_tx_power = ieee80211_set_tx_power,
82 .get_tx_power = ieee80211_get_tx_power,
83 + .set_antenna_gain = ieee80211_set_antenna_gain,
84 .rfkill_poll = ieee80211_rfkill_poll,
85 CFG80211_TESTMODE_CMD(ieee80211_testmode_cmd)
86 CFG80211_TESTMODE_DUMP(ieee80211_testmode_dump)
87 --- a/net/mac80211/ieee80211_i.h
88 +++ b/net/mac80211/ieee80211_i.h
89 @@ -1542,6 +1542,7 @@ struct ieee80211_local {
90 int dynamic_ps_forced_timeout;
91
92 int user_power_level; /* in dBm, for all interfaces */
93 + int user_antenna_gain; /* in dBi */
94
95 enum ieee80211_smps_mode smps_mode;
96
97 --- a/net/mac80211/main.c
98 +++ b/net/mac80211/main.c
99 @@ -96,7 +96,7 @@ static u32 ieee80211_hw_conf_chan(struct
100 struct ieee80211_sub_if_data *sdata;
101 struct cfg80211_chan_def chandef = {};
102 u32 changed = 0;
103 - int power;
104 + int power, max_power;
105 u32 offchannel_flag;
106
107 offchannel_flag = local->hw.conf.flags & IEEE80211_CONF_OFFCHANNEL;
108 @@ -157,6 +157,12 @@ static u32 ieee80211_hw_conf_chan(struct
109 }
110 rcu_read_unlock();
111
112 + max_power = chandef.chan->max_reg_power;
113 + if (local->user_antenna_gain > 0) {
114 + max_power -= local->user_antenna_gain;
115 + power = min(power, max_power);
116 + }
117 +
118 if (local->hw.conf.power_level != power) {
119 changed |= IEEE80211_CONF_CHANGE_POWER;
120 local->hw.conf.power_level = power;
121 @@ -762,6 +768,7 @@ struct ieee80211_hw *ieee80211_alloc_hw_
122 IEEE80211_RADIOTAP_MCS_HAVE_BW;
123 local->hw.radiotap_vht_details = IEEE80211_RADIOTAP_VHT_KNOWN_GI |
124 IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH;
125 + local->user_antenna_gain = 0;
126 local->hw.uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
127 local->hw.uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
128 local->hw.max_mtu = IEEE80211_MAX_DATA_LEN;
129 --- a/net/wireless/nl80211.c
130 +++ b/net/wireless/nl80211.c
131 @@ -800,6 +800,7 @@ static const struct nla_policy nl80211_p
132 [NL80211_ATTR_MLO_SUPPORT] = { .type = NLA_FLAG },
133 [NL80211_ATTR_MAX_NUM_AKM_SUITES] = { .type = NLA_REJECT },
134 [NL80211_ATTR_PUNCT_BITMAP] = NLA_POLICY_RANGE(NLA_U8, 0, 0xffff),
135 + [NL80211_ATTR_WIPHY_ANTENNA_GAIN] = { .type = NLA_U32 },
136 };
137
138 /* policy for the key attributes */
139 @@ -3527,6 +3528,22 @@ static int nl80211_set_wiphy(struct sk_b
140 if (result)
141 goto out;
142 }
143 +
144 + if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_GAIN]) {
145 + int idx, dbi = 0;
146 +
147 + if (!rdev->ops->set_antenna_gain) {
148 + result = -EOPNOTSUPP;
149 + goto out;
150 + }
151 +
152 + idx = NL80211_ATTR_WIPHY_ANTENNA_GAIN;
153 + dbi = nla_get_u32(info->attrs[idx]);
154 +
155 + result = rdev->ops->set_antenna_gain(&rdev->wiphy, dbi);
156 + if (result)
157 + goto out;
158 + }
159
160 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
161 struct wireless_dev *txp_wdev = wdev;