Rebase from upstream commit : 3bb9dcf44627ffdd313fe92c563ae454b6ff8aa6
[librecmc/librecmc.git] / package / kernel / mac80211 / patches / subsys / 522-mac80211_configure_antenna_gain.patch
1 --- a/include/net/cfg80211.h
2 +++ b/include/net/cfg80211.h
3 @@ -2959,6 +2959,7 @@ struct cfg80211_external_auth_params {
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   * @set_wds_peer: set the WDS peer for a WDS interface
10   *
11 @@ -3259,6 +3260,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         int     (*set_wds_peer)(struct wiphy *wiphy, struct net_device *dev,
18                                 const u8 *addr);
19 --- a/include/net/mac80211.h
20 +++ b/include/net/mac80211.h
21 @@ -1389,6 +1389,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 @@ -1409,6 +1410,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 @@ -2241,6 +2241,26 @@ enum nl80211_commands {
40   *     association request when used with NL80211_CMD_NEW_STATION). Can be set
41   *     only if %NL80211_STA_FLAG_WME is set.
42   *
43 + * @NL80211_ATTR_FTM_RESPONDER: nested attribute which user-space can include
44 + *     in %NL80211_CMD_START_AP or %NL80211_CMD_SET_BEACON for fine timing
45 + *     measurement (FTM) responder functionality and containing parameters as
46 + *     possible, see &enum nl80211_ftm_responder_attr
47 + *
48 + * @NL80211_ATTR_FTM_RESPONDER_STATS: Nested attribute with FTM responder
49 + *     statistics, see &enum nl80211_ftm_responder_stats.
50 + *
51 + * @NL80211_ATTR_TIMEOUT: Timeout for the given operation in milliseconds (u32),
52 + *     if the attribute is not given no timeout is requested. Note that 0 is an
53 + *     invalid value.
54 + *
55 + * @NL80211_ATTR_PEER_MEASUREMENTS: peer measurements request (and result)
56 + *     data, uses nested attributes specified in
57 + *     &enum nl80211_peer_measurement_attrs.
58 + *     This is also used for capability advertisement in the wiphy information,
59 + *     with the appropriate sub-attributes.
60 + * @NL80211_ATTR_WIPHY_ANTENNA_GAIN: Configured antenna gain. Used to reduce
61 + *     transmit power to stay within regulatory limits. u32, dBi.
62 + *
63   * @NUM_NL80211_ATTR: total number of nl80211_attrs available
64   * @NL80211_ATTR_MAX: highest attribute number currently defined
65   * @__NL80211_ATTR_AFTER_LAST: internal use
66 @@ -2682,6 +2702,16 @@ enum nl80211_attrs {
67  
68         NL80211_ATTR_HE_CAPABILITY,
69  
70 +       NL80211_ATTR_FTM_RESPONDER,
71 +
72 +       NL80211_ATTR_FTM_RESPONDER_STATS,
73 +
74 +       NL80211_ATTR_TIMEOUT,
75 +
76 +       NL80211_ATTR_PEER_MEASUREMENTS,
77 +
78 +       NL80211_ATTR_WIPHY_ANTENNA_GAIN,
79 +
80         /* add attributes here, update the policy in nl80211.c */
81  
82         __NL80211_ATTR_AFTER_LAST,
83 --- a/net/mac80211/cfg.c
84 +++ b/net/mac80211/cfg.c
85 @@ -2494,6 +2494,19 @@ static int ieee80211_get_tx_power(struct
86         return 0;
87  }
88  
89 +static int ieee80211_set_antenna_gain(struct wiphy *wiphy, int dbi)
90 +{
91 +       struct ieee80211_local *local = wiphy_priv(wiphy);
92 +
93 +       if (dbi < 0)
94 +               return -EINVAL;
95 +
96 +       local->user_antenna_gain = dbi;
97 +       ieee80211_hw_config(local, 0);
98 +
99 +       return 0;
100 +}
101 +
102  static int ieee80211_set_wds_peer(struct wiphy *wiphy, struct net_device *dev,
103                                   const u8 *addr)
104  {
105 @@ -3861,6 +3874,7 @@ const struct cfg80211_ops mac80211_confi
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/net/mac80211/ieee80211_i.h
114 +++ b/net/mac80211/ieee80211_i.h
115 @@ -1354,6 +1354,7 @@ struct ieee80211_local {
116         int dynamic_ps_forced_timeout;
117  
118         int user_power_level; /* in dBm, for all interfaces */
119 +       int user_antenna_gain; /* in dBi */
120  
121         enum ieee80211_smps_mode smps_mode;
122  
123 --- a/net/mac80211/main.c
124 +++ b/net/mac80211/main.c
125 @@ -94,7 +94,7 @@ static u32 ieee80211_hw_conf_chan(struct
126         struct ieee80211_sub_if_data *sdata;
127         struct cfg80211_chan_def chandef = {};
128         u32 changed = 0;
129 -       int power;
130 +       int power, max_power;
131         u32 offchannel_flag;
132  
133         offchannel_flag = local->hw.conf.flags & IEEE80211_CONF_OFFCHANNEL;
134 @@ -151,6 +151,12 @@ static u32 ieee80211_hw_conf_chan(struct
135         }
136         rcu_read_unlock();
137  
138 +       max_power = chandef.chan->max_reg_power;
139 +       if (local->user_antenna_gain > 0) {
140 +               max_power -= local->user_antenna_gain;
141 +               power = min(power, max_power);
142 +       }
143 +
144         if (local->hw.conf.power_level != power) {
145                 changed |= IEEE80211_CONF_CHANGE_POWER;
146                 local->hw.conf.power_level = power;
147 @@ -626,6 +632,7 @@ struct ieee80211_hw *ieee80211_alloc_hw_
148                                          IEEE80211_RADIOTAP_MCS_HAVE_BW;
149         local->hw.radiotap_vht_details = IEEE80211_RADIOTAP_VHT_KNOWN_GI |
150                                          IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH;
151 +       local->user_antenna_gain = 0;
152         local->hw.uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
153         local->hw.uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
154         local->user_power_level = IEEE80211_UNSET_POWER_LEVEL;
155 --- a/net/wireless/nl80211.c
156 +++ b/net/wireless/nl80211.c
157 @@ -430,6 +430,7 @@ static const struct nla_policy nl80211_p
158         [NL80211_ATTR_TXQ_QUANTUM] = { .type = NLA_U32 },
159         [NL80211_ATTR_HE_CAPABILITY] = { .type = NLA_BINARY,
160                                          .len = NL80211_HE_MAX_CAPABILITY_LEN },
161 +       [NL80211_ATTR_WIPHY_ANTENNA_GAIN] = { .type = NLA_U32 },
162  };
163  
164  /* policy for the key attributes */
165 @@ -2587,6 +2588,20 @@ static int nl80211_set_wiphy(struct sk_b
166                 if (result)
167                         return result;
168         }
169 +
170 +       if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_GAIN]) {
171 +               int idx, dbi = 0;
172 +
173 +               if (!rdev->ops->set_antenna_gain)
174 +                       return -EOPNOTSUPP;
175 +
176 +               idx = NL80211_ATTR_WIPHY_ANTENNA_GAIN;
177 +               dbi = nla_get_u32(info->attrs[idx]);
178 +
179 +               result = rdev->ops->set_antenna_gain(&rdev->wiphy, dbi);
180 +               if (result)
181 +                       return result;
182 +       }
183  
184         if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
185             info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {