iwl-helpers.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. #define IEEE80211_CHAN_W_RADAR_DETECT 0x00000010
  127. static inline struct ieee80211_conf *ieee80211_get_hw_conf(
  128. struct ieee80211_hw *hw)
  129. {
  130. return &hw->conf;
  131. }
  132. #define QOS_CONTROL_LEN 2
  133. static inline int ieee80211_is_management(u16 fc)
  134. {
  135. return (fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT;
  136. }
  137. static inline int ieee80211_is_control(u16 fc)
  138. {
  139. return (fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL;
  140. }
  141. static inline int ieee80211_is_data(u16 fc)
  142. {
  143. return (fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA;
  144. }
  145. static inline int ieee80211_is_back_request(u16 fc)
  146. {
  147. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) &&
  148. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BACK_REQ);
  149. }
  150. static inline int ieee80211_is_probe_response(u16 fc)
  151. {
  152. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  153. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP);
  154. }
  155. static inline int ieee80211_is_probe_request(u16 fc)
  156. {
  157. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  158. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_REQ);
  159. }
  160. static inline int ieee80211_is_beacon(u16 fc)
  161. {
  162. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  163. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON);
  164. }
  165. static inline int ieee80211_is_atim(u16 fc)
  166. {
  167. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  168. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ATIM);
  169. }
  170. static inline int ieee80211_is_assoc_request(u16 fc)
  171. {
  172. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  173. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ);
  174. }
  175. static inline int ieee80211_is_assoc_response(u16 fc)
  176. {
  177. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  178. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_RESP);
  179. }
  180. static inline int ieee80211_is_auth(u16 fc)
  181. {
  182. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  183. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ);
  184. }
  185. static inline int ieee80211_is_deauth(u16 fc)
  186. {
  187. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  188. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ);
  189. }
  190. static inline int ieee80211_is_disassoc(u16 fc)
  191. {
  192. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  193. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ);
  194. }
  195. static inline int ieee80211_is_reassoc_request(u16 fc)
  196. {
  197. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  198. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ);
  199. }
  200. static inline int ieee80211_is_reassoc_response(u16 fc)
  201. {
  202. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
  203. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_RESP);
  204. }
  205. static inline int ieee80211_is_qos_data(u16 fc)
  206. {
  207. return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
  208. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_QOS_DATA);
  209. }
  210. /**
  211. * ieee80211_get_qos_ctrl - get pointer to the QoS control field
  212. *
  213. * This function returns the pointer to 802.11 header QoS field (2 bytes)
  214. * This function doesn't check whether hdr is a QoS hdr, use with care
  215. * @hdr: struct ieee80211_hdr *hdr
  216. * @hdr_len: header length
  217. */
  218. static inline u8 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr, int hdr_len)
  219. {
  220. return ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
  221. }
  222. static inline int iwl_check_bits(unsigned long field, unsigned long mask)
  223. {
  224. return ((field & mask) == mask) ? 1 : 0;
  225. }
  226. static inline unsigned long elapsed_jiffies(unsigned long start,
  227. unsigned long end)
  228. {
  229. if (end >= start)
  230. return end - start;
  231. return end + (MAX_JIFFY_OFFSET - start) + 1;
  232. }
  233. static inline u8 iwl_get_dma_hi_address(dma_addr_t addr)
  234. {
  235. return sizeof(addr) > sizeof(u32) ? (addr >> 16) >> 16 : 0;
  236. }
  237. /**
  238. * iwl_queue_inc_wrap - increment queue index, wrap back to beginning
  239. * @index -- current index
  240. * @n_bd -- total number of entries in queue (must be power of 2)
  241. */
  242. static inline int iwl_queue_inc_wrap(int index, int n_bd)
  243. {
  244. return ++index & (n_bd - 1);
  245. }
  246. /**
  247. * iwl_queue_dec_wrap - decrement queue index, wrap back to end
  248. * @index -- current index
  249. * @n_bd -- total number of entries in queue (must be power of 2)
  250. */
  251. static inline int iwl_queue_dec_wrap(int index, int n_bd)
  252. {
  253. return --index & (n_bd - 1);
  254. }
  255. /* TODO: Move fw_desc functions to iwl-pci.ko */
  256. static inline void iwl_free_fw_desc(struct pci_dev *pci_dev,
  257. struct fw_desc *desc)
  258. {
  259. if (desc->v_addr)
  260. pci_free_consistent(pci_dev, desc->len,
  261. desc->v_addr, desc->p_addr);
  262. desc->v_addr = NULL;
  263. desc->len = 0;
  264. }
  265. static inline int iwl_alloc_fw_desc(struct pci_dev *pci_dev,
  266. struct fw_desc *desc)
  267. {
  268. desc->v_addr = pci_alloc_consistent(pci_dev, desc->len, &desc->p_addr);
  269. return (desc->v_addr != NULL) ? 0 : -ENOMEM;
  270. }
  271. #endif /* __iwl_helpers_h__ */