522-mac80211_configure_antenna_gain.patch 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. --- a/include/net/cfg80211.h
  2. +++ b/include/net/cfg80211.h
  3. @@ -2363,6 +2363,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. @@ -2624,6 +2625,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. int (*set_wds_peer)(struct wiphy *wiphy, struct net_device *dev,
  17. const u8 *addr);
  18. --- a/include/net/mac80211.h
  19. +++ b/include/net/mac80211.h
  20. @@ -1286,6 +1286,7 @@ enum ieee80211_smps_mode {
  21. *
  22. * @power_level: requested transmit power (in dBm), backward compatibility
  23. * value only that is set to the minimum of all interfaces
  24. + * @max_antenna_gain: maximum antenna gain adjusted by user config (in dBi)
  25. *
  26. * @chandef: the channel definition to tune to
  27. * @radar_enabled: whether radar detection is enabled
  28. @@ -1306,6 +1307,7 @@ enum ieee80211_smps_mode {
  29. struct ieee80211_conf {
  30. u32 flags;
  31. int power_level, dynamic_ps_timeout;
  32. + int max_antenna_gain;
  33. u16 listen_interval;
  34. u8 ps_dtim_period;
  35. --- a/include/uapi/linux/nl80211.h
  36. +++ b/include/uapi/linux/nl80211.h
  37. @@ -1790,6 +1790,9 @@ enum nl80211_commands {
  38. * between scans. The scan plans are executed sequentially.
  39. * Each scan plan is a nested attribute of &enum nl80211_sched_scan_plan.
  40. *
  41. + * @NL80211_ATTR_WIPHY_ANTENNA_GAIN: Configured antenna gain. Used to reduce
  42. + * transmit power to stay within regulatory limits. u32, dBi.
  43. + *
  44. * @NUM_NL80211_ATTR: total number of nl80211_attrs available
  45. * @NL80211_ATTR_MAX: highest attribute number currently defined
  46. * @__NL80211_ATTR_AFTER_LAST: internal use
  47. @@ -2164,6 +2167,8 @@ enum nl80211_attrs {
  48. NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
  49. NL80211_ATTR_SCHED_SCAN_PLANS,
  50. + NL80211_ATTR_WIPHY_ANTENNA_GAIN,
  51. +
  52. /* add attributes here, update the policy in nl80211.c */
  53. __NL80211_ATTR_AFTER_LAST,
  54. --- a/net/mac80211/cfg.c
  55. +++ b/net/mac80211/cfg.c
  56. @@ -2229,6 +2229,19 @@ static int ieee80211_get_tx_power(struct
  57. return 0;
  58. }
  59. +static int ieee80211_set_antenna_gain(struct wiphy *wiphy, int dbi)
  60. +{
  61. + struct ieee80211_local *local = wiphy_priv(wiphy);
  62. +
  63. + if (dbi < 0)
  64. + return -EINVAL;
  65. +
  66. + local->user_antenna_gain = dbi;
  67. + ieee80211_hw_config(local, 0);
  68. +
  69. + return 0;
  70. +}
  71. +
  72. static int ieee80211_set_wds_peer(struct wiphy *wiphy, struct net_device *dev,
  73. const u8 *addr)
  74. {
  75. @@ -3403,6 +3416,7 @@ const struct cfg80211_ops mac80211_confi
  76. .set_wiphy_params = ieee80211_set_wiphy_params,
  77. .set_tx_power = ieee80211_set_tx_power,
  78. .get_tx_power = ieee80211_get_tx_power,
  79. + .set_antenna_gain = ieee80211_set_antenna_gain,
  80. .set_wds_peer = ieee80211_set_wds_peer,
  81. .rfkill_poll = ieee80211_rfkill_poll,
  82. CFG80211_TESTMODE_CMD(ieee80211_testmode_cmd)
  83. --- a/net/mac80211/ieee80211_i.h
  84. +++ b/net/mac80211/ieee80211_i.h
  85. @@ -1318,6 +1318,7 @@ struct ieee80211_local {
  86. int dynamic_ps_forced_timeout;
  87. int user_power_level; /* in dBm, for all interfaces */
  88. + int user_antenna_gain; /* in dBi */
  89. enum ieee80211_smps_mode smps_mode;
  90. --- a/net/mac80211/main.c
  91. +++ b/net/mac80211/main.c
  92. @@ -93,7 +93,7 @@ static u32 ieee80211_hw_conf_chan(struct
  93. struct ieee80211_sub_if_data *sdata;
  94. struct cfg80211_chan_def chandef = {};
  95. u32 changed = 0;
  96. - int power;
  97. + int power, max_power;
  98. u32 offchannel_flag;
  99. offchannel_flag = local->hw.conf.flags & IEEE80211_CONF_OFFCHANNEL;
  100. @@ -150,6 +150,12 @@ static u32 ieee80211_hw_conf_chan(struct
  101. }
  102. rcu_read_unlock();
  103. + max_power = chandef.chan->max_reg_power;
  104. + if (local->user_antenna_gain > 0) {
  105. + max_power -= local->user_antenna_gain;
  106. + power = min(power, max_power);
  107. + }
  108. +
  109. if (local->hw.conf.power_level != power) {
  110. changed |= IEEE80211_CONF_CHANGE_POWER;
  111. local->hw.conf.power_level = power;
  112. @@ -586,6 +592,7 @@ struct ieee80211_hw *ieee80211_alloc_hw_
  113. IEEE80211_RADIOTAP_MCS_HAVE_BW;
  114. local->hw.radiotap_vht_details = IEEE80211_RADIOTAP_VHT_KNOWN_GI |
  115. IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH;
  116. + local->user_antenna_gain = 0;
  117. local->hw.uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
  118. local->hw.uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
  119. local->user_power_level = IEEE80211_UNSET_POWER_LEVEL;
  120. --- a/net/wireless/nl80211.c
  121. +++ b/net/wireless/nl80211.c
  122. @@ -403,6 +403,7 @@ static const struct nla_policy nl80211_p
  123. [NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
  124. [NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
  125. [NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
  126. + [NL80211_ATTR_WIPHY_ANTENNA_GAIN] = { .type = NLA_U32 },
  127. };
  128. /* policy for the key attributes */
  129. @@ -2220,6 +2221,20 @@ static int nl80211_set_wiphy(struct sk_b
  130. if (result)
  131. return result;
  132. }
  133. +
  134. + if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_GAIN]) {
  135. + int idx, dbi = 0;
  136. +
  137. + if (!rdev->ops->set_antenna_gain)
  138. + return -EOPNOTSUPP;
  139. +
  140. + idx = NL80211_ATTR_WIPHY_ANTENNA_GAIN;
  141. + dbi = nla_get_u32(info->attrs[idx]);
  142. +
  143. + result = rdev->ops->set_antenna_gain(&rdev->wiphy, dbi);
  144. + if (result)
  145. + return result;
  146. + }
  147. if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
  148. info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {