of.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C) 2017 Rafał Miłecki <rafal@milecki.pl>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/of.h>
  17. #include <net/cfg80211.h>
  18. #include "core.h"
  19. static bool wiphy_freq_limits_valid_chan(struct wiphy *wiphy,
  20. struct ieee80211_freq_range *freq_limits,
  21. unsigned int n_freq_limits,
  22. struct ieee80211_channel *chan)
  23. {
  24. u32 bw = MHZ_TO_KHZ(20);
  25. int i;
  26. for (i = 0; i < n_freq_limits; i++) {
  27. struct ieee80211_freq_range *limit = &freq_limits[i];
  28. if (cfg80211_does_bw_fit_range(limit,
  29. MHZ_TO_KHZ(chan->center_freq),
  30. bw))
  31. return true;
  32. }
  33. return false;
  34. }
  35. static void wiphy_freq_limits_apply(struct wiphy *wiphy,
  36. struct ieee80211_freq_range *freq_limits,
  37. unsigned int n_freq_limits)
  38. {
  39. enum nl80211_band band;
  40. int i;
  41. if (WARN_ON(!n_freq_limits))
  42. return;
  43. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  44. struct ieee80211_supported_band *sband = wiphy->bands[band];
  45. if (!sband)
  46. continue;
  47. for (i = 0; i < sband->n_channels; i++) {
  48. struct ieee80211_channel *chan = &sband->channels[i];
  49. if (chan->flags & IEEE80211_CHAN_DISABLED)
  50. continue;
  51. if (!wiphy_freq_limits_valid_chan(wiphy, freq_limits,
  52. n_freq_limits,
  53. chan)) {
  54. pr_debug("Disabling freq %d MHz as it's out of OF limits\n",
  55. chan->center_freq);
  56. chan->flags |= IEEE80211_CHAN_DISABLED;
  57. }
  58. }
  59. }
  60. }
  61. void wiphy_read_of_freq_limits(struct wiphy *wiphy)
  62. {
  63. struct device *dev = wiphy_dev(wiphy);
  64. struct device_node *np;
  65. struct property *prop;
  66. struct ieee80211_freq_range *freq_limits;
  67. unsigned int n_freq_limits;
  68. const __be32 *p;
  69. int len, i;
  70. int err = 0;
  71. if (!dev)
  72. return;
  73. np = dev_of_node(dev);
  74. if (!np)
  75. return;
  76. prop = of_find_property(np, "ieee80211-freq-limit", &len);
  77. if (!prop)
  78. return;
  79. if (!len || len % sizeof(u32) || len / sizeof(u32) % 2) {
  80. dev_err(dev, "ieee80211-freq-limit wrong format");
  81. return;
  82. }
  83. n_freq_limits = len / sizeof(u32) / 2;
  84. freq_limits = kcalloc(n_freq_limits, sizeof(*freq_limits), GFP_KERNEL);
  85. if (!freq_limits) {
  86. err = -ENOMEM;
  87. goto out_kfree;
  88. }
  89. p = NULL;
  90. for (i = 0; i < n_freq_limits; i++) {
  91. struct ieee80211_freq_range *limit = &freq_limits[i];
  92. p = of_prop_next_u32(prop, p, &limit->start_freq_khz);
  93. if (!p) {
  94. err = -EINVAL;
  95. goto out_kfree;
  96. }
  97. p = of_prop_next_u32(prop, p, &limit->end_freq_khz);
  98. if (!p) {
  99. err = -EINVAL;
  100. goto out_kfree;
  101. }
  102. if (!limit->start_freq_khz ||
  103. !limit->end_freq_khz ||
  104. limit->start_freq_khz >= limit->end_freq_khz) {
  105. err = -EINVAL;
  106. goto out_kfree;
  107. }
  108. }
  109. wiphy_freq_limits_apply(wiphy, freq_limits, n_freq_limits);
  110. out_kfree:
  111. kfree(freq_limits);
  112. if (err)
  113. dev_err(dev, "Failed to get limits: %d\n", err);
  114. }
  115. EXPORT_SYMBOL(wiphy_read_of_freq_limits);