3546b1d439cc799e0784130e38f6f438bdb25e08
[openwrt/staging/dedeckeh.git] / package / kernel / mac80211 / patches / subsys / 522-mac80211_configure_antenna_gain.patch
1 Index: backports-v4.18-rc7/include/net/cfg80211.h
2 ===================================================================
3 --- backports-v4.18-rc7.orig/include/net/cfg80211.h
4 +++ backports-v4.18-rc7/include/net/cfg80211.h
5 @@ -2857,6 +2857,7 @@ struct cfg80211_external_auth_params {
6 * (as advertised by the nl80211 feature flag.)
7 * @get_tx_power: store the current TX power into the dbm variable;
8 * return 0 if successful
9 + * @set_antenna_gain: set antenna gain to reduce maximum tx power if necessary
10 *
11 * @set_wds_peer: set the WDS peer for a WDS interface
12 *
13 @@ -3157,6 +3158,7 @@ struct cfg80211_ops {
14 enum nl80211_tx_power_setting type, int mbm);
15 int (*get_tx_power)(struct wiphy *wiphy, struct wireless_dev *wdev,
16 int *dbm);
17 + int (*set_antenna_gain)(struct wiphy *wiphy, int dbi);
18
19 int (*set_wds_peer)(struct wiphy *wiphy, struct net_device *dev,
20 const u8 *addr);
21 Index: backports-v4.18-rc7/include/net/mac80211.h
22 ===================================================================
23 --- backports-v4.18-rc7.orig/include/net/mac80211.h
24 +++ backports-v4.18-rc7/include/net/mac80211.h
25 @@ -1348,6 +1348,7 @@ enum ieee80211_smps_mode {
26 *
27 * @power_level: requested transmit power (in dBm), backward compatibility
28 * value only that is set to the minimum of all interfaces
29 + * @max_antenna_gain: maximum antenna gain adjusted by user config (in dBi)
30 *
31 * @chandef: the channel definition to tune to
32 * @radar_enabled: whether radar detection is enabled
33 @@ -1368,6 +1369,7 @@ enum ieee80211_smps_mode {
34 struct ieee80211_conf {
35 u32 flags;
36 int power_level, dynamic_ps_timeout;
37 + int max_antenna_gain;
38
39 u16 listen_interval;
40 u8 ps_dtim_period;
41 Index: backports-v4.18-rc7/include/uapi/linux/nl80211.h
42 ===================================================================
43 --- backports-v4.18-rc7.orig/include/uapi/linux/nl80211.h
44 +++ backports-v4.18-rc7/include/uapi/linux/nl80211.h
45 @@ -2238,6 +2238,9 @@ enum nl80211_commands {
46 * @NL80211_ATTR_TXQ_QUANTUM: TXQ scheduler quantum (bytes). Number of bytes
47 * a flow is assigned on each round of the DRR scheduler.
48 *
49 + * @NL80211_ATTR_WIPHY_ANTENNA_GAIN: Configured antenna gain. Used to reduce
50 + * transmit power to stay within regulatory limits. u32, dBi.
51 + *
52 * @NUM_NL80211_ATTR: total number of nl80211_attrs available
53 * @NL80211_ATTR_MAX: highest attribute number currently defined
54 * @__NL80211_ATTR_AFTER_LAST: internal use
55 @@ -2677,6 +2680,8 @@ enum nl80211_attrs {
56 NL80211_ATTR_TXQ_MEMORY_LIMIT,
57 NL80211_ATTR_TXQ_QUANTUM,
58
59 + NL80211_ATTR_WIPHY_ANTENNA_GAIN,
60 +
61 /* add attributes here, update the policy in nl80211.c */
62
63 __NL80211_ATTR_AFTER_LAST,
64 Index: backports-v4.18-rc7/net/mac80211/cfg.c
65 ===================================================================
66 --- backports-v4.18-rc7.orig/net/mac80211/cfg.c
67 +++ backports-v4.18-rc7/net/mac80211/cfg.c
68 @@ -2489,6 +2489,19 @@ static int ieee80211_get_tx_power(struct
69 return 0;
70 }
71
72 +static int ieee80211_set_antenna_gain(struct wiphy *wiphy, int dbi)
73 +{
74 + struct ieee80211_local *local = wiphy_priv(wiphy);
75 +
76 + if (dbi < 0)
77 + return -EINVAL;
78 +
79 + local->user_antenna_gain = dbi;
80 + ieee80211_hw_config(local, 0);
81 +
82 + return 0;
83 +}
84 +
85 static int ieee80211_set_wds_peer(struct wiphy *wiphy, struct net_device *dev,
86 const u8 *addr)
87 {
88 @@ -3856,6 +3869,7 @@ const struct cfg80211_ops mac80211_confi
89 .set_wiphy_params = ieee80211_set_wiphy_params,
90 .set_tx_power = ieee80211_set_tx_power,
91 .get_tx_power = ieee80211_get_tx_power,
92 + .set_antenna_gain = ieee80211_set_antenna_gain,
93 .set_wds_peer = ieee80211_set_wds_peer,
94 .rfkill_poll = ieee80211_rfkill_poll,
95 CFG80211_TESTMODE_CMD(ieee80211_testmode_cmd)
96 Index: backports-v4.18-rc7/net/mac80211/ieee80211_i.h
97 ===================================================================
98 --- backports-v4.18-rc7.orig/net/mac80211/ieee80211_i.h
99 +++ backports-v4.18-rc7/net/mac80211/ieee80211_i.h
100 @@ -1350,6 +1350,7 @@ struct ieee80211_local {
101 int dynamic_ps_forced_timeout;
102
103 int user_power_level; /* in dBm, for all interfaces */
104 + int user_antenna_gain; /* in dBi */
105
106 enum ieee80211_smps_mode smps_mode;
107
108 Index: backports-v4.18-rc7/net/mac80211/main.c
109 ===================================================================
110 --- backports-v4.18-rc7.orig/net/mac80211/main.c
111 +++ backports-v4.18-rc7/net/mac80211/main.c
112 @@ -93,7 +93,7 @@ static u32 ieee80211_hw_conf_chan(struct
113 struct ieee80211_sub_if_data *sdata;
114 struct cfg80211_chan_def chandef = {};
115 u32 changed = 0;
116 - int power;
117 + int power, max_power;
118 u32 offchannel_flag;
119
120 offchannel_flag = local->hw.conf.flags & IEEE80211_CONF_OFFCHANNEL;
121 @@ -150,6 +150,12 @@ static u32 ieee80211_hw_conf_chan(struct
122 }
123 rcu_read_unlock();
124
125 + max_power = chandef.chan->max_reg_power;
126 + if (local->user_antenna_gain > 0) {
127 + max_power -= local->user_antenna_gain;
128 + power = min(power, max_power);
129 + }
130 +
131 if (local->hw.conf.power_level != power) {
132 changed |= IEEE80211_CONF_CHANGE_POWER;
133 local->hw.conf.power_level = power;
134 @@ -611,6 +617,7 @@ struct ieee80211_hw *ieee80211_alloc_hw_
135 IEEE80211_RADIOTAP_MCS_HAVE_BW;
136 local->hw.radiotap_vht_details = IEEE80211_RADIOTAP_VHT_KNOWN_GI |
137 IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH;
138 + local->user_antenna_gain = 0;
139 local->hw.uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
140 local->hw.uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
141 local->user_power_level = IEEE80211_UNSET_POWER_LEVEL;
142 Index: backports-v4.18-rc7/net/wireless/nl80211.c
143 ===================================================================
144 --- backports-v4.18-rc7.orig/net/wireless/nl80211.c
145 +++ backports-v4.18-rc7/net/wireless/nl80211.c
146 @@ -428,6 +428,7 @@ static const struct nla_policy nl80211_p
147 [NL80211_ATTR_TXQ_LIMIT] = { .type = NLA_U32 },
148 [NL80211_ATTR_TXQ_MEMORY_LIMIT] = { .type = NLA_U32 },
149 [NL80211_ATTR_TXQ_QUANTUM] = { .type = NLA_U32 },
150 + [NL80211_ATTR_WIPHY_ANTENNA_GAIN] = { .type = NLA_U32 },
151 };
152
153 /* policy for the key attributes */
154 @@ -2531,6 +2532,20 @@ static int nl80211_set_wiphy(struct sk_b
155 if (result)
156 return result;
157 }
158 +
159 + if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_GAIN]) {
160 + int idx, dbi = 0;
161 +
162 + if (!rdev->ops->set_antenna_gain)
163 + return -EOPNOTSUPP;
164 +
165 + idx = NL80211_ATTR_WIPHY_ANTENNA_GAIN;
166 + dbi = nla_get_u32(info->attrs[idx]);
167 +
168 + result = rdev->ops->set_antenna_gain(&rdev->wiphy, dbi);
169 + if (result)
170 + return result;
171 + }
172
173 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
174 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {