347-0002-cfg80211-support-ieee80211-freq-limit-DT-property.patch 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
  2. Date: Wed, 4 Jan 2017 18:58:31 +0100
  3. Subject: [PATCH] cfg80211: support ieee80211-freq-limit DT property
  4. MIME-Version: 1.0
  5. Content-Type: text/plain; charset=UTF-8
  6. Content-Transfer-Encoding: 8bit
  7. This patch adds a helper for reading that new property and applying
  8. limitations of supported channels specified this way.
  9. It is used with devices that normally support a wide wireless band but
  10. in a given config are limited to some part of it (usually due to board
  11. design). For example a dual-band chipset may be able to support one band
  12. only because of used antennas.
  13. It's also common that tri-band routers have separated radios for lower
  14. and higher part of 5 GHz band and it may be impossible to say which is
  15. which without a DT info.
  16. Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
  17. [add new function to documentation, fix link]
  18. Signed-off-by: Johannes Berg <johannes.berg@intel.com>
  19. ---
  20. create mode 100644 net/wireless/of.c
  21. --- a/include/net/cfg80211.h
  22. +++ b/include/net/cfg80211.h
  23. @@ -311,6 +311,34 @@ struct ieee80211_supported_band {
  24. struct ieee80211_sta_vht_cap vht_cap;
  25. };
  26. +/**
  27. + * wiphy_read_of_freq_limits - read frequency limits from device tree
  28. + *
  29. + * @wiphy: the wireless device to get extra limits for
  30. + *
  31. + * Some devices may have extra limitations specified in DT. This may be useful
  32. + * for chipsets that normally support more bands but are limited due to board
  33. + * design (e.g. by antennas or external power amplifier).
  34. + *
  35. + * This function reads info from DT and uses it to *modify* channels (disable
  36. + * unavailable ones). It's usually a *bad* idea to use it in drivers with
  37. + * shared channel data as DT limitations are device specific. You should make
  38. + * sure to call it only if channels in wiphy are copied and can be modified
  39. + * without affecting other devices.
  40. + *
  41. + * As this function access device node it has to be called after set_wiphy_dev.
  42. + * It also modifies channels so they have to be set first.
  43. + * If using this helper, call it before wiphy_register().
  44. + */
  45. +#ifdef CONFIG_OF
  46. +void wiphy_read_of_freq_limits(struct wiphy *wiphy);
  47. +#else /* CONFIG_OF */
  48. +static inline void wiphy_read_of_freq_limits(struct wiphy *wiphy)
  49. +{
  50. +}
  51. +#endif /* !CONFIG_OF */
  52. +
  53. +
  54. /*
  55. * Wireless hardware/device configuration structures and methods
  56. */
  57. --- a/net/wireless/Makefile
  58. +++ b/net/wireless/Makefile
  59. @@ -11,6 +11,7 @@ obj-$(CONFIG_WEXT_PRIV) += wext-priv.o
  60. cfg80211-y += core.o sysfs.o radiotap.o util.o reg.o scan.o nl80211.o
  61. cfg80211-y += mlme.o ibss.o sme.o chan.o ethtool.o mesh.o ap.o trace.o ocb.o
  62. +cfg80211-$(CONFIG_OF) += of.o
  63. cfg80211-$(CPTCFG_CFG80211_DEBUGFS) += debugfs.o
  64. cfg80211-$(CPTCFG_CFG80211_WEXT) += wext-compat.o wext-sme.o
  65. cfg80211-$(CPTCFG_CFG80211_INTERNAL_REGDB) += regdb.o
  66. --- /dev/null
  67. +++ b/net/wireless/of.c
  68. @@ -0,0 +1,138 @@
  69. +/*
  70. + * Copyright (C) 2017 Rafał Miłecki <rafal@milecki.pl>
  71. + *
  72. + * Permission to use, copy, modify, and/or distribute this software for any
  73. + * purpose with or without fee is hereby granted, provided that the above
  74. + * copyright notice and this permission notice appear in all copies.
  75. + *
  76. + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  77. + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  78. + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  79. + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  80. + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  81. + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  82. + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  83. + */
  84. +
  85. +#include <linux/of.h>
  86. +#include <net/cfg80211.h>
  87. +#include "core.h"
  88. +
  89. +static bool wiphy_freq_limits_valid_chan(struct wiphy *wiphy,
  90. + struct ieee80211_freq_range *freq_limits,
  91. + unsigned int n_freq_limits,
  92. + struct ieee80211_channel *chan)
  93. +{
  94. + u32 bw = MHZ_TO_KHZ(20);
  95. + int i;
  96. +
  97. + for (i = 0; i < n_freq_limits; i++) {
  98. + struct ieee80211_freq_range *limit = &freq_limits[i];
  99. +
  100. + if (cfg80211_does_bw_fit_range(limit,
  101. + MHZ_TO_KHZ(chan->center_freq),
  102. + bw))
  103. + return true;
  104. + }
  105. +
  106. + return false;
  107. +}
  108. +
  109. +static void wiphy_freq_limits_apply(struct wiphy *wiphy,
  110. + struct ieee80211_freq_range *freq_limits,
  111. + unsigned int n_freq_limits)
  112. +{
  113. + enum nl80211_band band;
  114. + int i;
  115. +
  116. + if (WARN_ON(!n_freq_limits))
  117. + return;
  118. +
  119. + for (band = 0; band < NUM_NL80211_BANDS; band++) {
  120. + struct ieee80211_supported_band *sband = wiphy->bands[band];
  121. +
  122. + if (!sband)
  123. + continue;
  124. +
  125. + for (i = 0; i < sband->n_channels; i++) {
  126. + struct ieee80211_channel *chan = &sband->channels[i];
  127. +
  128. + if (chan->flags & IEEE80211_CHAN_DISABLED)
  129. + continue;
  130. +
  131. + if (!wiphy_freq_limits_valid_chan(wiphy, freq_limits,
  132. + n_freq_limits,
  133. + chan)) {
  134. + pr_debug("Disabling freq %d MHz as it's out of OF limits\n",
  135. + chan->center_freq);
  136. + chan->flags |= IEEE80211_CHAN_DISABLED;
  137. + }
  138. + }
  139. + }
  140. +}
  141. +
  142. +void wiphy_read_of_freq_limits(struct wiphy *wiphy)
  143. +{
  144. + struct device *dev = wiphy_dev(wiphy);
  145. + struct device_node *np;
  146. + struct property *prop;
  147. + struct ieee80211_freq_range *freq_limits;
  148. + unsigned int n_freq_limits;
  149. + const __be32 *p;
  150. + int len, i;
  151. + int err = 0;
  152. +
  153. + if (!dev)
  154. + return;
  155. + np = dev_of_node(dev);
  156. + if (!np)
  157. + return;
  158. +
  159. + prop = of_find_property(np, "ieee80211-freq-limit", &len);
  160. + if (!prop)
  161. + return;
  162. +
  163. + if (!len || len % sizeof(u32) || len / sizeof(u32) % 2) {
  164. + dev_err(dev, "ieee80211-freq-limit wrong format");
  165. + return;
  166. + }
  167. + n_freq_limits = len / sizeof(u32) / 2;
  168. +
  169. + freq_limits = kcalloc(n_freq_limits, sizeof(*freq_limits), GFP_KERNEL);
  170. + if (!freq_limits) {
  171. + err = -ENOMEM;
  172. + goto out_kfree;
  173. + }
  174. +
  175. + p = NULL;
  176. + for (i = 0; i < n_freq_limits; i++) {
  177. + struct ieee80211_freq_range *limit = &freq_limits[i];
  178. +
  179. + p = of_prop_next_u32(prop, p, &limit->start_freq_khz);
  180. + if (!p) {
  181. + err = -EINVAL;
  182. + goto out_kfree;
  183. + }
  184. +
  185. + p = of_prop_next_u32(prop, p, &limit->end_freq_khz);
  186. + if (!p) {
  187. + err = -EINVAL;
  188. + goto out_kfree;
  189. + }
  190. +
  191. + if (!limit->start_freq_khz ||
  192. + !limit->end_freq_khz ||
  193. + limit->start_freq_khz >= limit->end_freq_khz) {
  194. + err = -EINVAL;
  195. + goto out_kfree;
  196. + }
  197. + }
  198. +
  199. + wiphy_freq_limits_apply(wiphy, freq_limits, n_freq_limits);
  200. +
  201. +out_kfree:
  202. + kfree(freq_limits);
  203. + if (err)
  204. + dev_err(dev, "Failed to get limits: %d\n", err);
  205. +}
  206. +EXPORT_SYMBOL(wiphy_read_of_freq_limits);