638fa01f9002b30370a9176335e8046c516e6678
[openwrt.git] / package / kernel / mac80211 / patches / 522-mac80211_configure_antenna_gain.patch
1 --- a/include/net/cfg80211.h
2 +++ b/include/net/cfg80211.h
3 @@ -2156,6 +2156,7 @@ struct cfg80211_qos_map {
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 @@ -2380,6 +2381,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 @@ -1033,6 +1033,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 @@ -1054,6 +1055,7 @@ struct ieee80211_conf {
30         u32 flags;
31         int power_level, dynamic_ps_timeout;
32         int max_sleep_period;
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 @@ -1555,6 +1555,9 @@ enum nl80211_commands {
40   *     data is in the format defined for the payload of the QoS Map Set element
41   *     in IEEE Std 802.11-2012, 8.4.2.97.
42   *
43 + * @NL80211_ATTR_WIPHY_ANTENNA_GAIN: Configured antenna gain. Used to reduce
44 + *     transmit power to stay within regulatory limits. u32, dBi.
45 + *
46   * @NL80211_ATTR_MAX: highest attribute number currently defined
47   * @__NL80211_ATTR_AFTER_LAST: internal use
48   */
49 @@ -1883,6 +1886,8 @@ enum nl80211_attrs {
50  
51         NL80211_ATTR_QOS_MAP,
52  
53 +       NL80211_ATTR_WIPHY_ANTENNA_GAIN,
54 +
55         /* add attributes here, update the policy in nl80211.c */
56  
57         __NL80211_ATTR_AFTER_LAST,
58 --- a/net/mac80211/cfg.c
59 +++ b/net/mac80211/cfg.c
60 @@ -2333,6 +2333,19 @@ static int ieee80211_get_tx_power(struct
61         return 0;
62  }
63  
64 +static int ieee80211_set_antenna_gain(struct wiphy *wiphy, int dbi)
65 +{
66 +       struct ieee80211_local *local = wiphy_priv(wiphy);
67 +
68 +       if (dbi < 0)
69 +               return -EINVAL;
70 +
71 +       local->user_antenna_gain = dbi;
72 +       ieee80211_hw_config(local, 0);
73 +
74 +       return 0;
75 +}
76 +
77  static int ieee80211_set_wds_peer(struct wiphy *wiphy, struct net_device *dev,
78                                   const u8 *addr)
79  {
80 @@ -3916,6 +3929,7 @@ struct cfg80211_ops mac80211_config_ops 
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         .set_wds_peer = ieee80211_set_wds_peer,
86         .rfkill_poll = ieee80211_rfkill_poll,
87         CFG80211_TESTMODE_CMD(ieee80211_testmode_cmd)
88 --- a/net/mac80211/ieee80211_i.h
89 +++ b/net/mac80211/ieee80211_i.h
90 @@ -1216,6 +1216,7 @@ struct ieee80211_local {
91         int dynamic_ps_forced_timeout;
92  
93         int user_power_level; /* in dBm, for all interfaces */
94 +       int user_antenna_gain; /* in dBi */
95  
96         enum ieee80211_smps_mode smps_mode;
97  
98 --- a/net/mac80211/main.c
99 +++ b/net/mac80211/main.c
100 @@ -97,7 +97,7 @@ static u32 ieee80211_hw_conf_chan(struct
101         struct ieee80211_sub_if_data *sdata;
102         struct cfg80211_chan_def chandef = {};
103         u32 changed = 0;
104 -       int power;
105 +       int power, ant_gain, max_power;
106         u32 offchannel_flag;
107  
108         offchannel_flag = local->hw.conf.flags & IEEE80211_CONF_OFFCHANNEL;
109 @@ -152,8 +152,21 @@ static u32 ieee80211_hw_conf_chan(struct
110         }
111         rcu_read_unlock();
112  
113 -       if (local->hw.conf.power_level != power) {
114 +       max_power = chandef.chan->max_reg_power;
115 +       ant_gain = chandef.chan->max_antenna_gain;
116 +       if (local->user_antenna_gain > 0) {
117 +               if (local->user_antenna_gain > ant_gain) {
118 +                       max_power -= local->user_antenna_gain - ant_gain;
119 +                       ant_gain = 0;
120 +               } else
121 +                       ant_gain -= local->user_antenna_gain;
122 +               power = min(power, max_power);
123 +       }
124 +
125 +       if (local->hw.conf.power_level != power ||
126 +           local->hw.conf.max_antenna_gain != ant_gain) {
127                 changed |= IEEE80211_CONF_CHANGE_POWER;
128 +               local->hw.conf.max_antenna_gain = ant_gain;
129                 local->hw.cur_power_level = power;
130                 local->hw.conf.power_level = power;
131         }
132 @@ -580,6 +593,7 @@ struct ieee80211_hw *ieee80211_alloc_hw(
133                                          IEEE80211_RADIOTAP_MCS_HAVE_BW;
134         local->hw.radiotap_vht_details = IEEE80211_RADIOTAP_VHT_KNOWN_GI |
135                                          IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH;
136 +       local->user_antenna_gain = 0;
137         local->hw.uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
138         local->hw.uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
139         local->user_power_level = IEEE80211_UNSET_POWER_LEVEL;
140 --- a/net/wireless/nl80211.c
141 +++ b/net/wireless/nl80211.c
142 @@ -384,6 +384,7 @@ static const struct nla_policy nl80211_p
143         [NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
144         [NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
145                                    .len = IEEE80211_QOS_MAP_LEN_MAX },
146 +       [NL80211_ATTR_WIPHY_ANTENNA_GAIN] = { .type = NLA_U32 },
147  };
148  
149  /* policy for the key attributes */
150 @@ -2105,6 +2106,22 @@ static int nl80211_set_wiphy(struct sk_b
151                         goto bad_res;
152         }
153  
154 +       if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_GAIN]) {
155 +               int idx, dbi = 0;
156 +
157 +               if (!rdev->ops->set_antenna_gain) {
158 +                       result = -EOPNOTSUPP;
159 +                       goto bad_res;
160 +               }
161 +
162 +               idx = NL80211_ATTR_WIPHY_ANTENNA_GAIN;
163 +               dbi = nla_get_u32(info->attrs[idx]);
164 +
165 +               result = rdev->ops->set_antenna_gain(&rdev->wiphy, dbi);
166 +               if (result)
167 +                       goto bad_res;
168 +       }
169 +
170         if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
171             info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
172                 u32 tx_ant, rx_ant;