1 --- a/include/net/cfg80211.h
2 +++ b/include/net/cfg80211.h
3 @@ -3818,6 +3818,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
9 * @rfkill_poll: polls the hw rfkill line, use cfg80211 reporting
10 * functions to adjust rfkill hw state
11 @@ -4142,6 +4143,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,
15 + int (*set_antenna_gain)(struct wiphy *wiphy, int dbi);
17 void (*rfkill_poll)(struct wiphy *wiphy);
19 --- a/include/net/mac80211.h
20 +++ b/include/net/mac80211.h
21 @@ -1564,6 +1564,7 @@ enum ieee80211_smps_mode {
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)
27 * @chandef: the channel definition to tune to
28 * @radar_enabled: whether radar detection is enabled
29 @@ -1584,6 +1585,7 @@ enum ieee80211_smps_mode {
30 struct ieee80211_conf {
32 int power_level, dynamic_ps_timeout;
33 + int max_antenna_gain;
37 --- a/include/uapi/linux/nl80211.h
38 +++ b/include/uapi/linux/nl80211.h
39 @@ -2593,6 +2593,9 @@ enum nl80211_commands {
40 * @NL80211_ATTR_COLOR_CHANGE_ELEMS: Nested set of attributes containing the IE
41 * information for the time while performing a color switch.
43 + * @NL80211_ATTR_WIPHY_ANTENNA_GAIN: Configured antenna gain. Used to reduce
44 + * transmit power to stay within regulatory limits. u32, dBi.
46 * @NUM_NL80211_ATTR: total number of nl80211_attrs available
47 * @NL80211_ATTR_MAX: highest attribute number currently defined
48 * @__NL80211_ATTR_AFTER_LAST: internal use
49 @@ -3096,6 +3099,8 @@ enum nl80211_attrs {
50 NL80211_ATTR_COLOR_CHANGE_COLOR,
51 NL80211_ATTR_COLOR_CHANGE_ELEMS,
53 + NL80211_ATTR_WIPHY_ANTENNA_GAIN,
55 /* add attributes here, update the policy in nl80211.c */
57 __NL80211_ATTR_AFTER_LAST,
58 --- a/net/mac80211/cfg.c
59 +++ b/net/mac80211/cfg.c
60 @@ -2749,6 +2749,19 @@ static int ieee80211_get_tx_power(struct
64 +static int ieee80211_set_antenna_gain(struct wiphy *wiphy, int dbi)
66 + struct ieee80211_local *local = wiphy_priv(wiphy);
71 + local->user_antenna_gain = dbi;
72 + ieee80211_hw_config(local, 0);
77 static void ieee80211_rfkill_poll(struct wiphy *wiphy)
79 struct ieee80211_local *local = wiphy_priv(wiphy);
80 @@ -4384,6 +4397,7 @@ const struct cfg80211_ops mac80211_confi
81 .set_wiphy_params = ieee80211_set_wiphy_params,
82 .set_tx_power = ieee80211_set_tx_power,
83 .get_tx_power = ieee80211_get_tx_power,
84 + .set_antenna_gain = ieee80211_set_antenna_gain,
85 .rfkill_poll = ieee80211_rfkill_poll,
86 CFG80211_TESTMODE_CMD(ieee80211_testmode_cmd)
87 CFG80211_TESTMODE_DUMP(ieee80211_testmode_dump)
88 --- a/net/mac80211/ieee80211_i.h
89 +++ b/net/mac80211/ieee80211_i.h
90 @@ -1428,6 +1428,7 @@ struct ieee80211_local {
91 int dynamic_ps_forced_timeout;
93 int user_power_level; /* in dBm, for all interfaces */
94 + int user_antenna_gain; /* in dBi */
96 enum ieee80211_smps_mode smps_mode;
98 --- a/net/mac80211/main.c
99 +++ b/net/mac80211/main.c
100 @@ -96,7 +96,7 @@ static u32 ieee80211_hw_conf_chan(struct
101 struct ieee80211_sub_if_data *sdata;
102 struct cfg80211_chan_def chandef = {};
105 + int power, max_power;
108 offchannel_flag = local->hw.conf.flags & IEEE80211_CONF_OFFCHANNEL;
109 @@ -157,6 +157,12 @@ static u32 ieee80211_hw_conf_chan(struct
113 + max_power = chandef.chan->max_reg_power;
114 + if (local->user_antenna_gain > 0) {
115 + max_power -= local->user_antenna_gain;
116 + power = min(power, max_power);
119 if (local->hw.conf.power_level != power) {
120 changed |= IEEE80211_CONF_CHANGE_POWER;
121 local->hw.conf.power_level = power;
122 @@ -670,6 +676,7 @@ struct ieee80211_hw *ieee80211_alloc_hw_
123 IEEE80211_RADIOTAP_MCS_HAVE_BW;
124 local->hw.radiotap_vht_details = IEEE80211_RADIOTAP_VHT_KNOWN_GI |
125 IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH;
126 + local->user_antenna_gain = 0;
127 local->hw.uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
128 local->hw.uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
129 local->hw.max_mtu = IEEE80211_MAX_DATA_LEN;
130 --- a/net/wireless/nl80211.c
131 +++ b/net/wireless/nl80211.c
132 @@ -778,6 +778,7 @@ static const struct nla_policy nl80211_p
133 [NL80211_ATTR_COLOR_CHANGE_COUNT] = { .type = NLA_U8 },
134 [NL80211_ATTR_COLOR_CHANGE_COLOR] = { .type = NLA_U8 },
135 [NL80211_ATTR_COLOR_CHANGE_ELEMS] = NLA_POLICY_NESTED(nl80211_policy),
136 + [NL80211_ATTR_WIPHY_ANTENNA_GAIN] = { .type = NLA_U32 },
139 /* policy for the key attributes */
140 @@ -3315,6 +3316,20 @@ static int nl80211_set_wiphy(struct sk_b
144 + if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_GAIN]) {
147 + if (!rdev->ops->set_antenna_gain)
148 + return -EOPNOTSUPP;
150 + idx = NL80211_ATTR_WIPHY_ANTENNA_GAIN;
151 + dbi = nla_get_u32(info->attrs[idx]);
153 + result = rdev->ops->set_antenna_gain(&rdev->wiphy, dbi);
158 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
159 struct wireless_dev *txp_wdev = wdev;
160 enum nl80211_tx_power_setting type;