chan.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_fixed_channel(struct cfg80211_registered_device *rdev,
  12. struct wireless_dev *for_wdev)
  13. {
  14. struct wireless_dev *wdev;
  15. struct ieee80211_channel *result = NULL;
  16. WARN_ON(!mutex_is_locked(&rdev->devlist_mtx));
  17. list_for_each_entry(wdev, &rdev->netdev_list, list) {
  18. if (wdev == for_wdev)
  19. continue;
  20. /*
  21. * Lock manually to tell lockdep about allowed
  22. * nesting here if for_wdev->mtx is held already.
  23. * This is ok as it's all under the rdev devlist
  24. * mutex and as such can only be done once at any
  25. * given time.
  26. */
  27. mutex_lock_nested(&wdev->mtx, SINGLE_DEPTH_NESTING);
  28. if (wdev->current_bss)
  29. result = wdev->current_bss->pub.channel;
  30. wdev_unlock(wdev);
  31. if (result)
  32. break;
  33. }
  34. return result;
  35. }
  36. struct ieee80211_channel *
  37. rdev_freq_to_chan(struct cfg80211_registered_device *rdev,
  38. int freq, enum nl80211_channel_type channel_type)
  39. {
  40. struct ieee80211_channel *chan;
  41. struct ieee80211_sta_ht_cap *ht_cap;
  42. chan = ieee80211_get_channel(&rdev->wiphy, freq);
  43. /* Primary channel not allowed */
  44. if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
  45. return NULL;
  46. if (channel_type == NL80211_CHAN_HT40MINUS &&
  47. chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
  48. return NULL;
  49. else if (channel_type == NL80211_CHAN_HT40PLUS &&
  50. chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
  51. return NULL;
  52. ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap;
  53. if (channel_type != NL80211_CHAN_NO_HT) {
  54. if (!ht_cap->ht_supported)
  55. return NULL;
  56. if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
  57. ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
  58. return NULL;
  59. }
  60. return chan;
  61. }
  62. int rdev_set_freq(struct cfg80211_registered_device *rdev,
  63. struct wireless_dev *for_wdev,
  64. int freq, enum nl80211_channel_type channel_type)
  65. {
  66. struct ieee80211_channel *chan;
  67. int result;
  68. if (rdev_fixed_channel(rdev, for_wdev))
  69. return -EBUSY;
  70. if (!rdev->ops->set_channel)
  71. return -EOPNOTSUPP;
  72. chan = rdev_freq_to_chan(rdev, freq, channel_type);
  73. if (!chan)
  74. return -EINVAL;
  75. result = rdev->ops->set_channel(&rdev->wiphy, chan, channel_type);
  76. if (result)
  77. return result;
  78. rdev->channel = chan;
  79. return 0;
  80. }