libipw_geo.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /******************************************************************************
  2. Copyright(c) 2005 Intel Corporation. All rights reserved.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of version 2 of the GNU General Public License as
  5. published by the Free Software Foundation.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  8. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  9. more details.
  10. You should have received a copy of the GNU General Public License along with
  11. this program; if not, write to the Free Software Foundation, Inc., 59
  12. Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  13. The full GNU General Public License is included in this distribution in the
  14. file called LICENSE.
  15. Contact Information:
  16. Intel Linux Wireless <ilw@linux.intel.com>
  17. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  18. ******************************************************************************/
  19. #include <linux/compiler.h>
  20. #include <linux/errno.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/in6.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/proc_fs.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/tcp.h>
  31. #include <linux/types.h>
  32. #include <linux/wireless.h>
  33. #include <linux/etherdevice.h>
  34. #include <asm/uaccess.h>
  35. #include "libipw.h"
  36. int libipw_is_valid_channel(struct libipw_device *ieee, u8 channel)
  37. {
  38. int i;
  39. /* Driver needs to initialize the geography map before using
  40. * these helper functions */
  41. if (ieee->geo.bg_channels == 0 && ieee->geo.a_channels == 0)
  42. return 0;
  43. if (ieee->freq_band & LIBIPW_24GHZ_BAND)
  44. for (i = 0; i < ieee->geo.bg_channels; i++)
  45. /* NOTE: If G mode is currently supported but
  46. * this is a B only channel, we don't see it
  47. * as valid. */
  48. if ((ieee->geo.bg[i].channel == channel) &&
  49. !(ieee->geo.bg[i].flags & LIBIPW_CH_INVALID) &&
  50. (!(ieee->mode & IEEE_G) ||
  51. !(ieee->geo.bg[i].flags & LIBIPW_CH_B_ONLY)))
  52. return LIBIPW_24GHZ_BAND;
  53. if (ieee->freq_band & LIBIPW_52GHZ_BAND)
  54. for (i = 0; i < ieee->geo.a_channels; i++)
  55. if ((ieee->geo.a[i].channel == channel) &&
  56. !(ieee->geo.a[i].flags & LIBIPW_CH_INVALID))
  57. return LIBIPW_52GHZ_BAND;
  58. return 0;
  59. }
  60. int libipw_channel_to_index(struct libipw_device *ieee, u8 channel)
  61. {
  62. int i;
  63. /* Driver needs to initialize the geography map before using
  64. * these helper functions */
  65. if (ieee->geo.bg_channels == 0 && ieee->geo.a_channels == 0)
  66. return -1;
  67. if (ieee->freq_band & LIBIPW_24GHZ_BAND)
  68. for (i = 0; i < ieee->geo.bg_channels; i++)
  69. if (ieee->geo.bg[i].channel == channel)
  70. return i;
  71. if (ieee->freq_band & LIBIPW_52GHZ_BAND)
  72. for (i = 0; i < ieee->geo.a_channels; i++)
  73. if (ieee->geo.a[i].channel == channel)
  74. return i;
  75. return -1;
  76. }
  77. u32 libipw_channel_to_freq(struct libipw_device * ieee, u8 channel)
  78. {
  79. const struct libipw_channel * ch;
  80. /* Driver needs to initialize the geography map before using
  81. * these helper functions */
  82. if (ieee->geo.bg_channels == 0 && ieee->geo.a_channels == 0)
  83. return 0;
  84. ch = libipw_get_channel(ieee, channel);
  85. if (!ch->channel)
  86. return 0;
  87. return ch->freq;
  88. }
  89. u8 libipw_freq_to_channel(struct libipw_device * ieee, u32 freq)
  90. {
  91. int i;
  92. /* Driver needs to initialize the geography map before using
  93. * these helper functions */
  94. if (ieee->geo.bg_channels == 0 && ieee->geo.a_channels == 0)
  95. return 0;
  96. freq /= 100000;
  97. if (ieee->freq_band & LIBIPW_24GHZ_BAND)
  98. for (i = 0; i < ieee->geo.bg_channels; i++)
  99. if (ieee->geo.bg[i].freq == freq)
  100. return ieee->geo.bg[i].channel;
  101. if (ieee->freq_band & LIBIPW_52GHZ_BAND)
  102. for (i = 0; i < ieee->geo.a_channels; i++)
  103. if (ieee->geo.a[i].freq == freq)
  104. return ieee->geo.a[i].channel;
  105. return 0;
  106. }
  107. void libipw_set_geo(struct libipw_device *ieee,
  108. const struct libipw_geo *geo)
  109. {
  110. memcpy(ieee->geo.name, geo->name, 3);
  111. ieee->geo.name[3] = '\0';
  112. ieee->geo.bg_channels = geo->bg_channels;
  113. ieee->geo.a_channels = geo->a_channels;
  114. memcpy(ieee->geo.bg, geo->bg, geo->bg_channels *
  115. sizeof(struct libipw_channel));
  116. memcpy(ieee->geo.a, geo->a, ieee->geo.a_channels *
  117. sizeof(struct libipw_channel));
  118. }
  119. const struct libipw_geo *libipw_get_geo(struct libipw_device *ieee)
  120. {
  121. return &ieee->geo;
  122. }
  123. u8 libipw_get_channel_flags(struct libipw_device * ieee, u8 channel)
  124. {
  125. int index = libipw_channel_to_index(ieee, channel);
  126. if (index == -1)
  127. return LIBIPW_CH_INVALID;
  128. if (channel <= LIBIPW_24GHZ_CHANNELS)
  129. return ieee->geo.bg[index].flags;
  130. return ieee->geo.a[index].flags;
  131. }
  132. static const struct libipw_channel bad_channel = {
  133. .channel = 0,
  134. .flags = LIBIPW_CH_INVALID,
  135. .max_power = 0,
  136. };
  137. const struct libipw_channel *libipw_get_channel(struct libipw_device
  138. *ieee, u8 channel)
  139. {
  140. int index = libipw_channel_to_index(ieee, channel);
  141. if (index == -1)
  142. return &bad_channel;
  143. if (channel <= LIBIPW_24GHZ_CHANNELS)
  144. return &ieee->geo.bg[index];
  145. return &ieee->geo.a[index];
  146. }
  147. EXPORT_SYMBOL(libipw_get_channel);
  148. EXPORT_SYMBOL(libipw_get_channel_flags);
  149. EXPORT_SYMBOL(libipw_is_valid_channel);
  150. EXPORT_SYMBOL(libipw_freq_to_channel);
  151. EXPORT_SYMBOL(libipw_channel_to_freq);
  152. EXPORT_SYMBOL(libipw_channel_to_index);
  153. EXPORT_SYMBOL(libipw_set_geo);
  154. EXPORT_SYMBOL(libipw_get_geo);