reg.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. /*
  11. * This regulatory domain control implementation is highly incomplete, it
  12. * only exists for the purpose of not regressing mac80211.
  13. *
  14. * For now, drivers can restrict the set of allowed channels by either
  15. * not registering those channels or setting the IEEE80211_CHAN_DISABLED
  16. * flag; that flag will only be *set* by this code, never *cleared.
  17. *
  18. * The usual implementation is for a driver to read a device EEPROM to
  19. * determine which regulatory domain it should be operating under, then
  20. * looking up the allowable channels in a driver-local table and finally
  21. * registering those channels in the wiphy structure.
  22. *
  23. * Alternatively, drivers that trust the regulatory domain control here
  24. * will register a complete set of capabilities and the control code
  25. * will restrict the set by setting the IEEE80211_CHAN_* flags.
  26. */
  27. #include <linux/kernel.h>
  28. #include <net/wireless.h>
  29. #include "core.h"
  30. static char *ieee80211_regdom = "US";
  31. module_param(ieee80211_regdom, charp, 0444);
  32. MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
  33. struct ieee80211_channel_range {
  34. short start_freq;
  35. short end_freq;
  36. int max_power;
  37. int max_antenna_gain;
  38. u32 flags;
  39. };
  40. struct ieee80211_regdomain {
  41. const char *code;
  42. const struct ieee80211_channel_range *ranges;
  43. int n_ranges;
  44. };
  45. #define RANGE_PWR(_start, _end, _pwr, _ag, _flags) \
  46. { _start, _end, _pwr, _ag, _flags }
  47. /*
  48. * Ideally, in the future, these definitions will be loaded from a
  49. * userspace table via some daemon.
  50. */
  51. static const struct ieee80211_channel_range ieee80211_US_channels[] = {
  52. /* IEEE 802.11b/g, channels 1..11 */
  53. RANGE_PWR(2412, 2462, 27, 6, 0),
  54. /* IEEE 802.11a, channel 36*/
  55. RANGE_PWR(5180, 5180, 23, 6, 0),
  56. /* IEEE 802.11a, channel 40*/
  57. RANGE_PWR(5200, 5200, 23, 6, 0),
  58. /* IEEE 802.11a, channel 44*/
  59. RANGE_PWR(5220, 5220, 23, 6, 0),
  60. /* IEEE 802.11a, channels 48..64 */
  61. RANGE_PWR(5240, 5320, 23, 6, 0),
  62. /* IEEE 802.11a, channels 149..165, outdoor */
  63. RANGE_PWR(5745, 5825, 30, 6, 0),
  64. };
  65. static const struct ieee80211_channel_range ieee80211_JP_channels[] = {
  66. /* IEEE 802.11b/g, channels 1..14 */
  67. RANGE_PWR(2412, 2484, 20, 6, 0),
  68. /* IEEE 802.11a, channels 34..48 */
  69. RANGE_PWR(5170, 5240, 20, 6, IEEE80211_CHAN_PASSIVE_SCAN),
  70. /* IEEE 802.11a, channels 52..64 */
  71. RANGE_PWR(5260, 5320, 20, 6, IEEE80211_CHAN_NO_IBSS |
  72. IEEE80211_CHAN_RADAR),
  73. };
  74. static const struct ieee80211_channel_range ieee80211_EU_channels[] = {
  75. /* IEEE 802.11b/g, channels 1..13 */
  76. RANGE_PWR(2412, 2472, 20, 6, 0),
  77. /* IEEE 802.11a, channel 36*/
  78. RANGE_PWR(5180, 5180, 23, 6, IEEE80211_CHAN_PASSIVE_SCAN),
  79. /* IEEE 802.11a, channel 40*/
  80. RANGE_PWR(5200, 5200, 23, 6, IEEE80211_CHAN_PASSIVE_SCAN),
  81. /* IEEE 802.11a, channel 44*/
  82. RANGE_PWR(5220, 5220, 23, 6, IEEE80211_CHAN_PASSIVE_SCAN),
  83. /* IEEE 802.11a, channels 48..64 */
  84. RANGE_PWR(5240, 5320, 23, 6, IEEE80211_CHAN_NO_IBSS |
  85. IEEE80211_CHAN_RADAR),
  86. /* IEEE 802.11a, channels 100..140 */
  87. RANGE_PWR(5500, 5700, 30, 6, IEEE80211_CHAN_NO_IBSS |
  88. IEEE80211_CHAN_RADAR),
  89. };
  90. #define REGDOM(_code) \
  91. { \
  92. .code = __stringify(_code), \
  93. .ranges = ieee80211_ ##_code## _channels, \
  94. .n_ranges = ARRAY_SIZE(ieee80211_ ##_code## _channels), \
  95. }
  96. static const struct ieee80211_regdomain ieee80211_regdoms[] = {
  97. REGDOM(US),
  98. REGDOM(JP),
  99. REGDOM(EU),
  100. };
  101. static const struct ieee80211_regdomain *get_regdom(void)
  102. {
  103. static const struct ieee80211_channel_range
  104. ieee80211_world_channels[] = {
  105. /* IEEE 802.11b/g, channels 1..11 */
  106. RANGE_PWR(2412, 2462, 27, 6, 0),
  107. };
  108. static const struct ieee80211_regdomain regdom_world = REGDOM(world);
  109. int i;
  110. for (i = 0; i < ARRAY_SIZE(ieee80211_regdoms); i++)
  111. if (strcmp(ieee80211_regdom, ieee80211_regdoms[i].code) == 0)
  112. return &ieee80211_regdoms[i];
  113. return &regdom_world;
  114. }
  115. static void handle_channel(struct ieee80211_channel *chan,
  116. const struct ieee80211_regdomain *rd)
  117. {
  118. int i;
  119. u32 flags = chan->orig_flags;
  120. const struct ieee80211_channel_range *rg = NULL;
  121. for (i = 0; i < rd->n_ranges; i++) {
  122. if (rd->ranges[i].start_freq <= chan->center_freq &&
  123. chan->center_freq <= rd->ranges[i].end_freq) {
  124. rg = &rd->ranges[i];
  125. break;
  126. }
  127. }
  128. if (!rg) {
  129. /* not found */
  130. flags |= IEEE80211_CHAN_DISABLED;
  131. chan->flags = flags;
  132. return;
  133. }
  134. chan->flags = flags;
  135. chan->max_antenna_gain = min(chan->orig_mag,
  136. rg->max_antenna_gain);
  137. if (chan->orig_mpwr)
  138. chan->max_power = min(chan->orig_mpwr, rg->max_power);
  139. else
  140. chan->max_power = rg->max_power;
  141. }
  142. static void handle_band(struct ieee80211_supported_band *sband,
  143. const struct ieee80211_regdomain *rd)
  144. {
  145. int i;
  146. for (i = 0; i < sband->n_channels; i++)
  147. handle_channel(&sband->channels[i], rd);
  148. }
  149. void wiphy_update_regulatory(struct wiphy *wiphy)
  150. {
  151. enum ieee80211_band band;
  152. const struct ieee80211_regdomain *rd = get_regdom();
  153. for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  154. if (wiphy->bands[band])
  155. handle_band(wiphy->bands[band], rd);
  156. }