chan.c 3.0 KB

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