iwl-helpers.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2008 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 IWL_MASK(lo, hi) ((1 << (hi)) | ((1 << (hi)) - (1 << (lo))))
  126. static inline struct ieee80211_conf *ieee80211_get_hw_conf(
  127. struct ieee80211_hw *hw)
  128. {
  129. return &hw->conf;
  130. }
  131. static inline int iwl_check_bits(unsigned long field, unsigned long mask)
  132. {
  133. return ((field & mask) == mask) ? 1 : 0;
  134. }
  135. static inline unsigned long elapsed_jiffies(unsigned long start,
  136. unsigned long end)
  137. {
  138. if (end >= start)
  139. return end - start;
  140. return end + (MAX_JIFFY_OFFSET - start) + 1;
  141. }
  142. static inline u8 iwl_get_dma_hi_address(dma_addr_t addr)
  143. {
  144. return sizeof(addr) > sizeof(u32) ? (addr >> 16) >> 16 : 0;
  145. }
  146. /**
  147. * iwl_queue_inc_wrap - increment queue index, wrap back to beginning
  148. * @index -- current index
  149. * @n_bd -- total number of entries in queue (must be power of 2)
  150. */
  151. static inline int iwl_queue_inc_wrap(int index, int n_bd)
  152. {
  153. return ++index & (n_bd - 1);
  154. }
  155. /**
  156. * iwl_queue_dec_wrap - decrement queue index, wrap back to end
  157. * @index -- current index
  158. * @n_bd -- total number of entries in queue (must be power of 2)
  159. */
  160. static inline int iwl_queue_dec_wrap(int index, int n_bd)
  161. {
  162. return --index & (n_bd - 1);
  163. }
  164. /* TODO: Move fw_desc functions to iwl-pci.ko */
  165. static inline void iwl_free_fw_desc(struct pci_dev *pci_dev,
  166. struct fw_desc *desc)
  167. {
  168. if (desc->v_addr)
  169. pci_free_consistent(pci_dev, desc->len,
  170. desc->v_addr, desc->p_addr);
  171. desc->v_addr = NULL;
  172. desc->len = 0;
  173. }
  174. static inline int iwl_alloc_fw_desc(struct pci_dev *pci_dev,
  175. struct fw_desc *desc)
  176. {
  177. desc->v_addr = pci_alloc_consistent(pci_dev, desc->len, &desc->p_addr);
  178. return (desc->v_addr != NULL) ? 0 : -ENOMEM;
  179. }
  180. #endif /* __iwl_helpers_h__ */