chan.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * This file contains helper code to handle channel
  3. * settings and keeping track of what is possible at
  4. * any point in time.
  5. *
  6. * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
  7. */
  8. #include <net/cfg80211.h>
  9. #include "core.h"
  10. struct ieee80211_channel *
  11. rdev_freq_to_chan(struct cfg80211_registered_device *rdev,
  12. int freq, enum nl80211_channel_type channel_type)
  13. {
  14. struct ieee80211_channel *chan;
  15. struct ieee80211_sta_ht_cap *ht_cap;
  16. chan = ieee80211_get_channel(&rdev->wiphy, freq);
  17. /* Primary channel not allowed */
  18. if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
  19. return NULL;
  20. if (channel_type == NL80211_CHAN_HT40MINUS &&
  21. chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
  22. return NULL;
  23. else if (channel_type == NL80211_CHAN_HT40PLUS &&
  24. chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
  25. return NULL;
  26. ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap;
  27. if (channel_type != NL80211_CHAN_NO_HT) {
  28. if (!ht_cap->ht_supported)
  29. return NULL;
  30. if (channel_type != NL80211_CHAN_HT20 &&
  31. (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
  32. ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT))
  33. return NULL;
  34. }
  35. return chan;
  36. }
  37. static bool can_beacon_sec_chan(struct wiphy *wiphy,
  38. struct ieee80211_channel *chan,
  39. enum nl80211_channel_type channel_type)
  40. {
  41. struct ieee80211_channel *sec_chan;
  42. int diff;
  43. switch (channel_type) {
  44. case NL80211_CHAN_HT40PLUS:
  45. diff = 20;
  46. break;
  47. case NL80211_CHAN_HT40MINUS:
  48. diff = -20;
  49. break;
  50. default:
  51. return false;
  52. }
  53. sec_chan = ieee80211_get_channel(wiphy, chan->center_freq + diff);
  54. if (!sec_chan)
  55. return false;
  56. /* we'll need a DFS capability later */
  57. if (sec_chan->flags & (IEEE80211_CHAN_DISABLED |
  58. IEEE80211_CHAN_PASSIVE_SCAN |
  59. IEEE80211_CHAN_NO_IBSS |
  60. IEEE80211_CHAN_RADAR))
  61. return false;
  62. return true;
  63. }
  64. int cfg80211_set_freq(struct cfg80211_registered_device *rdev,
  65. struct wireless_dev *wdev, int freq,
  66. enum nl80211_channel_type channel_type)
  67. {
  68. struct ieee80211_channel *chan;
  69. int result;
  70. if (wdev && wdev->iftype == NL80211_IFTYPE_MONITOR)
  71. wdev = NULL;
  72. if (wdev) {
  73. ASSERT_WDEV_LOCK(wdev);
  74. if (!netif_running(wdev->netdev))
  75. return -ENETDOWN;
  76. }
  77. if (!rdev->ops->set_channel)
  78. return -EOPNOTSUPP;
  79. chan = rdev_freq_to_chan(rdev, freq, channel_type);
  80. if (!chan)
  81. return -EINVAL;
  82. /* Both channels should be able to initiate communication */
  83. if (wdev && (wdev->iftype == NL80211_IFTYPE_ADHOC ||
  84. wdev->iftype == NL80211_IFTYPE_AP ||
  85. wdev->iftype == NL80211_IFTYPE_AP_VLAN ||
  86. wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
  87. wdev->iftype == NL80211_IFTYPE_P2P_GO)) {
  88. switch (channel_type) {
  89. case NL80211_CHAN_HT40PLUS:
  90. case NL80211_CHAN_HT40MINUS:
  91. if (!can_beacon_sec_chan(&rdev->wiphy, chan,
  92. channel_type)) {
  93. printk(KERN_DEBUG
  94. "cfg80211: Secondary channel not "
  95. "allowed to initiate communication\n");
  96. return -EINVAL;
  97. }
  98. break;
  99. default:
  100. break;
  101. }
  102. }
  103. result = rdev->ops->set_channel(&rdev->wiphy,
  104. wdev ? wdev->netdev : NULL,
  105. chan, channel_type);
  106. if (result)
  107. return result;
  108. if (wdev)
  109. wdev->channel = chan;
  110. return 0;
  111. }