hostapd: backport package from trunk r39765
[12.09/openwrt.git] / package / mac80211 / patches / 523-mac80211_configure_antenna_gain.patch
1 --- a/include/net/mac80211.h
2 +++ b/include/net/mac80211.h
3 @@ -999,6 +999,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 @@ -1020,6 +1021,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 @@ -1126,6 +1126,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 @@ -1736,6 +1736,8 @@ enum nl80211_attrs {
32  
33         NL80211_ATTR_PEER_AID,
34  
35 +       NL80211_ATTR_WIPHY_ANTENNA_GAIN,
36 +
37         /* add attributes here, update the policy in nl80211.c */
38  
39         __NL80211_ATTR_AFTER_LAST,
40 --- a/net/wireless/nl80211.c
41 +++ b/net/wireless/nl80211.c
42 @@ -349,6 +349,7 @@ static const struct nla_policy nl80211_p
43         [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
44                                   .len = IEEE80211_MAX_DATA_LEN },
45         [NL80211_ATTR_PEER_AID] = { .type = NLA_U16 },
46 +       [NL80211_ATTR_WIPHY_ANTENNA_GAIN] = { .type = NLA_U32 },
47  };
48  
49  /* policy for the key attributes */
50 @@ -1993,6 +1994,22 @@ static int nl80211_set_wiphy(struct sk_b
51                         goto bad_res;
52         }
53  
54 +       if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_GAIN]) {
55 +               int idx, dbi = 0;
56 +
57 +               if (!rdev->ops->set_antenna_gain) {
58 +                       result = -EOPNOTSUPP;
59 +                       goto bad_res;
60 +               }
61 +
62 +               idx = NL80211_ATTR_WIPHY_ANTENNA_GAIN;
63 +               dbi = nla_get_u32(info->attrs[idx]);
64 +
65 +               result = rdev->ops->set_antenna_gain(&rdev->wiphy, dbi);
66 +               if (result)
67 +                       goto bad_res;
68 +       }
69 +
70         if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
71             info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
72                 u32 tx_ant, rx_ant;
73 --- a/net/mac80211/cfg.c
74 +++ b/net/mac80211/cfg.c
75 @@ -2283,6 +2283,19 @@ static int ieee80211_get_tx_power(struct
76         return 0;
77  }
78  
79 +static int ieee80211_set_antenna_gain(struct wiphy *wiphy, int dbi)
80 +{
81 +       struct ieee80211_local *local = wiphy_priv(wiphy);
82 +
83 +       if (dbi < 0)
84 +               return -EINVAL;
85 +
86 +       local->user_antenna_gain = dbi;
87 +       ieee80211_hw_config(local, 0);
88 +
89 +       return 0;
90 +}
91 +
92  static int ieee80211_set_wds_peer(struct wiphy *wiphy, struct net_device *dev,
93                                   const u8 *addr)
94  {
95 @@ -3474,6 +3487,7 @@ struct cfg80211_ops mac80211_config_ops 
96         .set_wiphy_params = ieee80211_set_wiphy_params,
97         .set_tx_power = ieee80211_set_tx_power,
98         .get_tx_power = ieee80211_get_tx_power,
99 +       .set_antenna_gain = ieee80211_set_antenna_gain,
100         .set_wds_peer = ieee80211_set_wds_peer,
101         .rfkill_poll = ieee80211_rfkill_poll,
102         CFG80211_TESTMODE_CMD(ieee80211_testmode_cmd)
103 --- a/include/net/cfg80211.h
104 +++ b/include/net/cfg80211.h
105 @@ -1970,6 +1970,7 @@ struct cfg80211_update_ft_ies_params {
106   *     (as advertised by the nl80211 feature flag.)
107   * @get_tx_power: store the current TX power into the dbm variable;
108   *     return 0 if successful
109 + * @set_antenna_gain: set antenna gain to reduce maximum tx power if necessary
110   *
111   * @set_wds_peer: set the WDS peer for a WDS interface
112   *
113 @@ -2189,6 +2190,7 @@ struct cfg80211_ops {
114                                 enum nl80211_tx_power_setting type, int mbm);
115         int     (*get_tx_power)(struct wiphy *wiphy, struct wireless_dev *wdev,
116                                 int *dbm);
117 +       int     (*set_antenna_gain)(struct wiphy *wiphy, int dbi);
118  
119         int     (*set_wds_peer)(struct wiphy *wiphy, struct net_device *dev,
120                                 const u8 *addr);
121 --- a/net/mac80211/main.c
122 +++ b/net/mac80211/main.c
123 @@ -102,6 +102,7 @@ static u32 ieee80211_hw_conf_chan(struct
124         struct cfg80211_chan_def chandef = {};
125         u32 changed = 0;
126         int power = 0;
127 +       int ant_gain, max_power;
128         u32 offchannel_flag;
129  
130         offchannel_flag = local->hw.conf.flags & IEEE80211_CONF_OFFCHANNEL;
131 @@ -165,8 +166,21 @@ static u32 ieee80211_hw_conf_chan(struct
132  
133         power = min(power, chandef.chan->max_power);
134  
135 -       if (local->hw.conf.power_level != power) {
136 +       max_power = chandef.chan->max_reg_power;
137 +       ant_gain = chandef.chan->max_antenna_gain;
138 +       if (local->user_antenna_gain > 0) {
139 +               if (local->user_antenna_gain > ant_gain) {
140 +                       max_power -= local->user_antenna_gain - ant_gain;
141 +                       ant_gain = 0;
142 +               } else
143 +                       ant_gain -= local->user_antenna_gain;
144 +               power = min(power, max_power);
145 +       }
146 +
147 +       if (local->hw.conf.power_level != power ||
148 +               local->hw.conf.max_antenna_gain != ant_gain) {
149                 changed |= IEEE80211_CONF_CHANGE_POWER;
150 +               local->hw.conf.max_antenna_gain = ant_gain;
151                 local->hw.cur_power_level = power;
152                 local->hw.conf.power_level = power;
153         }
154 @@ -597,6 +611,7 @@ struct ieee80211_hw *ieee80211_alloc_hw(
155                                          IEEE80211_RADIOTAP_MCS_HAVE_BW;
156         local->hw.radiotap_vht_details = IEEE80211_RADIOTAP_VHT_KNOWN_GI |
157                                          IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH;
158 +       local->user_antenna_gain = 0;
159         local->hw.uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
160         local->hw.uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
161         local->user_power_level = IEEE80211_UNSET_POWER_LEVEL;