iwl-helpers.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2007 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * James P. Ketrenos <ipw2100-admin@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #ifndef __iwl_helpers_h__
  30. #define __iwl_helpers_h__
  31. #include <linux/ctype.h>
  32. /*
  33. * The structures defined by the hardware/uCode interface
  34. * have bit-wise operations. For each bit-field there is
  35. * a data symbol in the structure, the start bit position
  36. * and the length of the bit-field.
  37. *
  38. * iwl_get_bits and iwl_set_bits will return or set the
  39. * appropriate bits on a 32-bit value.
  40. *
  41. * IWL_GET_BITS and IWL_SET_BITS use symbol expansion to
  42. * expand out to the appropriate call to iwl_get_bits
  43. * and iwl_set_bits without having to reference all of the
  44. * numerical constants and defines provided in the hardware
  45. * definition
  46. */
  47. /**
  48. * iwl_get_bits - Extract a hardware bit-field value
  49. * @src: source hardware value (__le32)
  50. * @pos: bit-position (0-based) of first bit of value
  51. * @len: length of bit-field
  52. *
  53. * iwl_get_bits will return the bit-field in cpu endian ordering.
  54. *
  55. * NOTE: If used from IWL_GET_BITS then pos and len are compile-constants and
  56. * will collapse to minimal code by the compiler.
  57. */
  58. static inline u32 iwl_get_bits(__le32 src, u8 pos, u8 len)
  59. {
  60. u32 tmp = le32_to_cpu(src);
  61. tmp >>= pos;
  62. tmp &= (1UL << len) - 1;
  63. return tmp;
  64. }
  65. /**
  66. * iwl_set_bits - Set a hardware bit-field value
  67. * @dst: Address of __le32 hardware value
  68. * @pos: bit-position (0-based) of first bit of value
  69. * @len: length of bit-field
  70. * @val: cpu endian value to encode into the bit-field
  71. *
  72. * iwl_set_bits will encode val into dst, masked to be len bits long at bit
  73. * position pos.
  74. *
  75. * NOTE: If used IWL_SET_BITS pos and len will be compile-constants and
  76. * will collapse to minimal code by the compiler.
  77. */
  78. static inline void iwl_set_bits(__le32 *dst, u8 pos, u8 len, int val)
  79. {
  80. u32 tmp = le32_to_cpu(*dst);
  81. tmp &= ~(((1UL << len) - 1) << pos);
  82. tmp |= (val & ((1UL << len) - 1)) << pos;
  83. *dst = cpu_to_le32(tmp);
  84. }
  85. static inline void iwl_set_bits16(__le16 *dst, u8 pos, u8 len, int val)
  86. {
  87. u16 tmp = le16_to_cpu(*dst);
  88. tmp &= ~((1UL << (pos + len)) - (1UL << pos));
  89. tmp |= (val & ((1UL << len) - 1)) << pos;
  90. *dst = cpu_to_le16(tmp);
  91. }
  92. /*
  93. * The bit-field definitions in iwl-xxxx-hw.h are in the form of:
  94. *
  95. * struct example {
  96. * __le32 val1;
  97. * #define IWL_name_POS 8
  98. * #define IWL_name_LEN 4
  99. * #define IWL_name_SYM val1
  100. * };
  101. *
  102. * The IWL_SET_BITS and IWL_GET_BITS macros are provided to allow the driver
  103. * to call:
  104. *
  105. * struct example bar;
  106. * u32 val = IWL_GET_BITS(bar, name);
  107. * val = val * 2;
  108. * IWL_SET_BITS(bar, name, val);
  109. *
  110. * All cpu / host ordering, masking, and shifts are performed by the macros
  111. * and iwl_{get,set}_bits.
  112. *
  113. */
  114. #define IWL_SET_BITS(s, sym, v) \
  115. iwl_set_bits(&(s).IWL_ ## sym ## _SYM, IWL_ ## sym ## _POS, \
  116. IWL_ ## sym ## _LEN, (v))
  117. #define IWL_SET_BITS16(s, sym, v) \
  118. iwl_set_bits16(&(s).IWL_ ## sym ## _SYM, IWL_ ## sym ## _POS, \
  119. IWL_ ## sym ## _LEN, (v))
  120. #define IWL_GET_BITS(s, sym) \
  121. iwl_get_bits((s).IWL_ ## sym ## _SYM, IWL_ ## sym ## _POS, \
  122. IWL_ ## sym ## _LEN)
  123. #define KELVIN_TO_CELSIUS(x) ((x)-273)
  124. #define CELSIUS_TO_KELVIN(x) ((x)+273)
  125. #define IEEE80211_CHAN_W_RADAR_DETECT 0x00000010
  126. static inline struct ieee80211_conf *ieee80211_get_hw_conf(
  127. struct ieee80211_hw *hw)
  128. {
  129. return &hw->conf;
  130. }
  131. #define QOS_CONTROL_LEN 2
  132. #define IEEE80211_STYPE_BACK_REQ 0x0080
  133. #define IEEE80211_STYPE_BACK 0x0090
  134. static inline int ieee80211_is_management(u16 fc)
  135. {
  136. return (fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT;
  137. }
  138. static inline int ieee80211_is_control(u16 fc)
  139. {
  140. return (fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL;
  141. }
  142. static inline int ieee80211_is_data(u16 fc)
  143. {
  144. return (fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA;
  145. }
  146. static inline int ieee80211_is_back_request(u16 fc)
  147. {
  148. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) &&
  149. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BACK_REQ);
  150. }
  151. static inline int ieee80211_is_probe_response(u16 fc)
  152. {
  153. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  154. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP);
  155. }
  156. static inline int ieee80211_is_probe_request(u16 fc)
  157. {
  158. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  159. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_REQ);
  160. }
  161. static inline int ieee80211_is_beacon(u16 fc)
  162. {
  163. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  164. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON);
  165. }
  166. static inline int ieee80211_is_atim(u16 fc)
  167. {
  168. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  169. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ATIM);
  170. }
  171. static inline int ieee80211_is_assoc_request(u16 fc)
  172. {
  173. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  174. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ);
  175. }
  176. static inline int ieee80211_is_assoc_response(u16 fc)
  177. {
  178. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  179. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_RESP);
  180. }
  181. static inline int ieee80211_is_auth(u16 fc)
  182. {
  183. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  184. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ);
  185. }
  186. static inline int ieee80211_is_deauth(u16 fc)
  187. {
  188. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  189. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ);
  190. }
  191. static inline int ieee80211_is_disassoc(u16 fc)
  192. {
  193. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  194. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ);
  195. }
  196. static inline int ieee80211_is_reassoc_request(u16 fc)
  197. {
  198. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  199. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ);
  200. }
  201. static inline int ieee80211_is_reassoc_response(u16 fc)
  202. {
  203. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  204. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_RESP);
  205. }
  206. static inline int iwl_check_bits(unsigned long field, unsigned long mask)
  207. {
  208. return ((field & mask) == mask) ? 1 : 0;
  209. }
  210. static inline unsigned long elapsed_jiffies(unsigned long start,
  211. unsigned long end)
  212. {
  213. if (end > start)
  214. return end - start;
  215. return end + (MAX_JIFFY_OFFSET - start);
  216. }
  217. #endif /* __iwl_helpers_h__ */