ath9k: clean up some patches
[openwrt.git] / package / kernel / mac80211 / patches / 523-mac80211_configure_antenna_gain.patch
1 --- a/include/net/mac80211.h
2 +++ b/include/net/mac80211.h
3 @@ -1002,6 +1002,7 @@ enum ieee80211_smps_mode {
4   *
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)
8   *
9   * @chandef: the channel definition to tune to
10   * @radar_enabled: whether radar detection is enabled
11 @@ -1023,6 +1024,7 @@ struct ieee80211_conf {
12         u32 flags;
13         int power_level, dynamic_ps_timeout;
14         int max_sleep_period;
15 +       int max_antenna_gain;
16  
17         u16 listen_interval;
18         u8 ps_dtim_period;
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;
23  
24         int user_power_level; /* in dBm, for all interfaces */
25 +       int user_antenna_gain; /* in dBi */
26  
27         enum ieee80211_smps_mode smps_mode;
28  
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).
34   *
35 + * @NL80211_ATTR_WIPHY_ANTENNA_GAIN: Configured antenna gain. Used to reduce
36 + *     transmit power to stay within regulatory limits.
37 + *
38   * @NL80211_ATTR_MAX: highest attribute number currently defined
39   * @__NL80211_ATTR_AFTER_LAST: internal use
40   */
41 @@ -1766,6 +1769,8 @@ enum nl80211_attrs {
42         NL80211_ATTR_CSA_C_OFF_BEACON,
43         NL80211_ATTR_CSA_C_OFF_PRESP,
44  
45 +       NL80211_ATTR_WIPHY_ANTENNA_GAIN,
46 +
47         /* add attributes here, update the policy in nl80211.c */
48  
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 },
57  };
58  
59  /* policy for the key attributes */
60 @@ -2000,6 +2001,22 @@ static int nl80211_set_wiphy(struct sk_b
61                         goto bad_res;
62         }
63  
64 +       if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_GAIN]) {
65 +               int idx, dbi = 0;
66 +
67 +               if (!rdev->ops->set_antenna_gain) {
68 +                       result = -EOPNOTSUPP;
69 +                       goto bad_res;
70 +               }
71 +
72 +               idx = NL80211_ATTR_WIPHY_ANTENNA_GAIN;
73 +               dbi = nla_get_u32(info->attrs[idx]);
74 +
75 +               result = rdev->ops->set_antenna_gain(&rdev->wiphy, dbi);
76 +               if (result)
77 +                       goto bad_res;
78 +       }
79 +
80         if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
81             info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
82                 u32 tx_ant, rx_ant;
83 --- a/net/mac80211/cfg.c
84 +++ b/net/mac80211/cfg.c
85 @@ -2293,6 +2293,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 @@ -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
120   *
121   * @set_wds_peer: set the WDS peer for a WDS interface
122   *
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,
126                                 int *dbm);
127 +       int     (*set_antenna_gain)(struct wiphy *wiphy, int dbi);
128  
129         int     (*set_wds_peer)(struct wiphy *wiphy, struct net_device *dev,
130                                 const u8 *addr);
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 = {};
136         u32 changed = 0;
137 -       int power;
138 +       int power, ant_gain, max_power;
139         u32 offchannel_flag;
140  
141         offchannel_flag = local->hw.conf.flags & IEEE80211_CONF_OFFCHANNEL;
142 @@ -165,8 +165,21 @@ static u32 ieee80211_hw_conf_chan(struct
143         }
144         rcu_read_unlock();
145  
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;
152 +                       ant_gain = 0;
153 +               } else
154 +                       ant_gain -= local->user_antenna_gain;
155 +               power = min(power, max_power);
156 +       }
157 +
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;
164         }
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;