1 --- a/include/net/mac80211.h
2 +++ b/include/net/mac80211.h
3 @@ -1002,6 +1002,7 @@ enum ieee80211_smps_mode {
5 * @power_level: requested transmit power (in dBm), backward compatibility
6 * value only that is set to the minimum of all interfaces
7 + * @max_antenna_gain: maximum antenna gain adjusted by user config (in dBi)
9 * @chandef: the channel definition to tune to
10 * @radar_enabled: whether radar detection is enabled
11 @@ -1023,6 +1024,7 @@ struct ieee80211_conf {
13 int power_level, dynamic_ps_timeout;
15 + int max_antenna_gain;
19 --- a/net/mac80211/ieee80211_i.h
20 +++ b/net/mac80211/ieee80211_i.h
21 @@ -1130,6 +1130,7 @@ struct ieee80211_local {
22 int dynamic_ps_forced_timeout;
24 int user_power_level; /* in dBm, for all interfaces */
25 + int user_antenna_gain; /* in dBi */
27 enum ieee80211_smps_mode smps_mode;
29 --- a/include/uapi/linux/nl80211.h
30 +++ b/include/uapi/linux/nl80211.h
31 @@ -1460,6 +1460,9 @@ enum nl80211_commands {
32 * @NL80211_ATTR_CSA_C_OFF_PRESP: Offset of the channel switch counter
33 * field in the probe response (%NL80211_ATTR_PROBE_RESP).
35 + * @NL80211_ATTR_WIPHY_ANTENNA_GAIN: Configured antenna gain. Used to reduce
36 + * transmit power to stay within regulatory limits.
38 * @NL80211_ATTR_MAX: highest attribute number currently defined
39 * @__NL80211_ATTR_AFTER_LAST: internal use
41 @@ -1766,6 +1769,8 @@ enum nl80211_attrs {
42 NL80211_ATTR_CSA_C_OFF_BEACON,
43 NL80211_ATTR_CSA_C_OFF_PRESP,
45 + NL80211_ATTR_WIPHY_ANTENNA_GAIN,
47 /* add attributes here, update the policy in nl80211.c */
49 __NL80211_ATTR_AFTER_LAST,
50 --- a/net/wireless/nl80211.c
51 +++ b/net/wireless/nl80211.c
52 @@ -354,6 +354,7 @@ static const struct nla_policy nl80211_p
53 [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
54 [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_U16 },
55 [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_U16 },
56 + [NL80211_ATTR_WIPHY_ANTENNA_GAIN] = { .type = NLA_U32 },
59 /* policy for the key attributes */
60 @@ -2000,6 +2001,22 @@ static int nl80211_set_wiphy(struct sk_b
64 + if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_GAIN]) {
67 + if (!rdev->ops->set_antenna_gain) {
68 + result = -EOPNOTSUPP;
72 + idx = NL80211_ATTR_WIPHY_ANTENNA_GAIN;
73 + dbi = nla_get_u32(info->attrs[idx]);
75 + result = rdev->ops->set_antenna_gain(&rdev->wiphy, dbi);
80 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
81 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
83 --- a/net/mac80211/cfg.c
84 +++ b/net/mac80211/cfg.c
85 @@ -2293,6 +2293,19 @@ static int ieee80211_get_tx_power(struct
89 +static int ieee80211_set_antenna_gain(struct wiphy *wiphy, int dbi)
91 + struct ieee80211_local *local = wiphy_priv(wiphy);
96 + local->user_antenna_gain = dbi;
97 + ieee80211_hw_config(local, 0);
102 static int ieee80211_set_wds_peer(struct wiphy *wiphy, struct net_device *dev,
105 @@ -3656,6 +3669,7 @@ struct cfg80211_ops mac80211_config_ops
106 .set_wiphy_params = ieee80211_set_wiphy_params,
107 .set_tx_power = ieee80211_set_tx_power,
108 .get_tx_power = ieee80211_get_tx_power,
109 + .set_antenna_gain = ieee80211_set_antenna_gain,
110 .set_wds_peer = ieee80211_set_wds_peer,
111 .rfkill_poll = ieee80211_rfkill_poll,
112 CFG80211_TESTMODE_CMD(ieee80211_testmode_cmd)
113 --- a/include/net/cfg80211.h
114 +++ b/include/net/cfg80211.h
115 @@ -1994,6 +1994,7 @@ struct cfg80211_update_ft_ies_params {
116 * (as advertised by the nl80211 feature flag.)
117 * @get_tx_power: store the current TX power into the dbm variable;
118 * return 0 if successful
119 + * @set_antenna_gain: set antenna gain to reduce maximum tx power if necessary
121 * @set_wds_peer: set the WDS peer for a WDS interface
123 @@ -2215,6 +2216,7 @@ struct cfg80211_ops {
124 enum nl80211_tx_power_setting type, int mbm);
125 int (*get_tx_power)(struct wiphy *wiphy, struct wireless_dev *wdev,
127 + int (*set_antenna_gain)(struct wiphy *wiphy, int dbi);
129 int (*set_wds_peer)(struct wiphy *wiphy, struct net_device *dev,
131 --- a/net/mac80211/main.c
132 +++ b/net/mac80211/main.c
133 @@ -101,7 +101,7 @@ static u32 ieee80211_hw_conf_chan(struct
134 struct ieee80211_sub_if_data *sdata;
135 struct cfg80211_chan_def chandef = {};
138 + int power, ant_gain, max_power;
141 offchannel_flag = local->hw.conf.flags & IEEE80211_CONF_OFFCHANNEL;
142 @@ -165,8 +165,21 @@ static u32 ieee80211_hw_conf_chan(struct
146 - if (local->hw.conf.power_level != power) {
147 + max_power = chandef.chan->max_reg_power;
148 + ant_gain = chandef.chan->max_antenna_gain;
149 + if (local->user_antenna_gain > 0) {
150 + if (local->user_antenna_gain > ant_gain) {
151 + max_power -= local->user_antenna_gain - ant_gain;
154 + ant_gain -= local->user_antenna_gain;
155 + power = min(power, max_power);
158 + if (local->hw.conf.power_level != power ||
159 + local->hw.conf.max_antenna_gain != ant_gain) {
160 changed |= IEEE80211_CONF_CHANGE_POWER;
161 + local->hw.conf.max_antenna_gain = ant_gain;
162 local->hw.cur_power_level = power;
163 local->hw.conf.power_level = power;
165 @@ -597,6 +610,7 @@ struct ieee80211_hw *ieee80211_alloc_hw(
166 IEEE80211_RADIOTAP_MCS_HAVE_BW;
167 local->hw.radiotap_vht_details = IEEE80211_RADIOTAP_VHT_KNOWN_GI |
168 IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH;
169 + local->user_antenna_gain = 0;
170 local->hw.uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
171 local->hw.uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
172 local->user_power_level = IEEE80211_UNSET_POWER_LEVEL;