/drivers/staging/rtl8192u/ieee80211/EndianFree.h

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t · C Header · 194 lines · 122 code · 27 blank · 45 comment · 3 complexity · 2028bf33d0356ae533138d8b09fabb1f MD5 · raw file

  1. #ifndef __INC_ENDIANFREE_H
  2. #define __INC_ENDIANFREE_H
  3. /*
  4. * Call endian free function when
  5. * 1. Read/write packet content.
  6. * 2. Before write integer to IO.
  7. * 3. After read integer from IO.
  8. */
  9. #define __MACHINE_LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
  10. #define __MACHINE_BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net, ppc */
  11. #define BYTE_ORDER __MACHINE_LITTLE_ENDIAN
  12. #if BYTE_ORDER == __MACHINE_LITTLE_ENDIAN
  13. // Convert data
  14. #define EF1Byte(_val) ((u8)(_val))
  15. #define EF2Byte(_val) ((u16)(_val))
  16. #define EF4Byte(_val) ((u32)(_val))
  17. #else
  18. // Convert data
  19. #define EF1Byte(_val) ((u8)(_val))
  20. #define EF2Byte(_val) (((((u16)(_val))&0x00ff)<<8)|((((u16)(_val))&0xff00)>>8))
  21. #define EF4Byte(_val) (((((u32)(_val))&0x000000ff)<<24)|\
  22. ((((u32)(_val))&0x0000ff00)<<8)|\
  23. ((((u32)(_val))&0x00ff0000)>>8)|\
  24. ((((u32)(_val))&0xff000000)>>24))
  25. #endif
  26. // Read data from memory
  27. #define ReadEF1Byte(_ptr) EF1Byte(*((u8 *)(_ptr)))
  28. #define ReadEF2Byte(_ptr) EF2Byte(*((u16 *)(_ptr)))
  29. #define ReadEF4Byte(_ptr) EF4Byte(*((u32 *)(_ptr)))
  30. // Write data to memory
  31. #define WriteEF1Byte(_ptr, _val) (*((u8 *)(_ptr)))=EF1Byte(_val)
  32. #define WriteEF2Byte(_ptr, _val) (*((u16 *)(_ptr)))=EF2Byte(_val)
  33. #define WriteEF4Byte(_ptr, _val) (*((u32 *)(_ptr)))=EF4Byte(_val)
  34. // Convert Host system specific byte ording (litten or big endia) to Network byte ording (big endian).
  35. // 2006.05.07, by rcnjko.
  36. #if BYTE_ORDER == __MACHINE_LITTLE_ENDIAN
  37. #define H2N1BYTE(_val) ((u8)(_val))
  38. #define H2N2BYTE(_val) (((((u16)(_val))&0x00ff)<<8)|\
  39. ((((u16)(_val))&0xff00)>>8))
  40. #define H2N4BYTE(_val) (((((u32)(_val))&0x000000ff)<<24)|\
  41. ((((u32)(_val))&0x0000ff00)<<8) |\
  42. ((((u32)(_val))&0x00ff0000)>>8) |\
  43. ((((u32)(_val))&0xff000000)>>24))
  44. #else
  45. #define H2N1BYTE(_val) ((u8)(_val))
  46. #define H2N2BYTE(_val) ((u16)(_val))
  47. #define H2N4BYTE(_val) ((u32)(_val))
  48. #endif
  49. // Convert from Network byte ording (big endian) to Host system specific byte ording (litten or big endia).
  50. // 2006.05.07, by rcnjko.
  51. #if BYTE_ORDER == __MACHINE_LITTLE_ENDIAN
  52. #define N2H1BYTE(_val) ((u8)(_val))
  53. #define N2H2BYTE(_val) (((((u16)(_val))&0x00ff)<<8)|\
  54. ((((u16)(_val))&0xff00)>>8))
  55. #define N2H4BYTE(_val) (((((u32)(_val))&0x000000ff)<<24)|\
  56. ((((u32)(_val))&0x0000ff00)<<8) |\
  57. ((((u32)(_val))&0x00ff0000)>>8) |\
  58. ((((u32)(_val))&0xff000000)>>24))
  59. #else
  60. #define N2H1BYTE(_val) ((u8)(_val))
  61. #define N2H2BYTE(_val) ((u16)(_val))
  62. #define N2H4BYTE(_val) ((u32)(_val))
  63. #endif
  64. //
  65. // Example:
  66. // BIT_LEN_MASK_32(0) => 0x00000000
  67. // BIT_LEN_MASK_32(1) => 0x00000001
  68. // BIT_LEN_MASK_32(2) => 0x00000003
  69. // BIT_LEN_MASK_32(32) => 0xFFFFFFFF
  70. //
  71. #define BIT_LEN_MASK_32(__BitLen) (0xFFFFFFFF >> (32 - (__BitLen)))
  72. //
  73. // Example:
  74. // BIT_OFFSET_LEN_MASK_32(0, 2) => 0x00000003
  75. // BIT_OFFSET_LEN_MASK_32(16, 2) => 0x00030000
  76. //
  77. #define BIT_OFFSET_LEN_MASK_32(__BitOffset, __BitLen) (BIT_LEN_MASK_32(__BitLen) << (__BitOffset))
  78. //
  79. // Description:
  80. // Return 4-byte value in host byte ordering from
  81. // 4-byte pointer in litten-endian system.
  82. //
  83. #define LE_P4BYTE_TO_HOST_4BYTE(__pStart) (EF4Byte(*((u32 *)(__pStart))))
  84. //
  85. // Description:
  86. // Translate subfield (continuous bits in little-endian) of 4-byte value in litten byte to
  87. // 4-byte value in host byte ordering.
  88. //
  89. #define LE_BITS_TO_4BYTE(__pStart, __BitOffset, __BitLen) \
  90. ( \
  91. ( LE_P4BYTE_TO_HOST_4BYTE(__pStart) >> (__BitOffset) ) \
  92. & \
  93. BIT_LEN_MASK_32(__BitLen) \
  94. )
  95. //
  96. // Description:
  97. // Mask subfield (continuous bits in little-endian) of 4-byte value in litten byte oredering
  98. // and return the result in 4-byte value in host byte ordering.
  99. //
  100. #define LE_BITS_CLEARED_TO_4BYTE(__pStart, __BitOffset, __BitLen) \
  101. ( \
  102. LE_P4BYTE_TO_HOST_4BYTE(__pStart) \
  103. & \
  104. ( ~BIT_OFFSET_LEN_MASK_32(__BitOffset, __BitLen) ) \
  105. )
  106. //
  107. // Description:
  108. // Set subfield of little-endian 4-byte value to specified value.
  109. //
  110. #define SET_BITS_TO_LE_4BYTE(__pStart, __BitOffset, __BitLen, __Value) \
  111. *((u32 *)(__pStart)) = \
  112. EF4Byte( \
  113. LE_BITS_CLEARED_TO_4BYTE(__pStart, __BitOffset, __BitLen) \
  114. | \
  115. ( (((u32)__Value) & BIT_LEN_MASK_32(__BitLen)) << (__BitOffset) ) \
  116. );
  117. #define BIT_LEN_MASK_16(__BitLen) \
  118. (0xFFFF >> (16 - (__BitLen)))
  119. #define BIT_OFFSET_LEN_MASK_16(__BitOffset, __BitLen) \
  120. (BIT_LEN_MASK_16(__BitLen) << (__BitOffset))
  121. #define LE_P2BYTE_TO_HOST_2BYTE(__pStart) \
  122. (EF2Byte(*((u16 *)(__pStart))))
  123. #define LE_BITS_TO_2BYTE(__pStart, __BitOffset, __BitLen) \
  124. ( \
  125. ( LE_P2BYTE_TO_HOST_2BYTE(__pStart) >> (__BitOffset) ) \
  126. & \
  127. BIT_LEN_MASK_16(__BitLen) \
  128. )
  129. #define LE_BITS_CLEARED_TO_2BYTE(__pStart, __BitOffset, __BitLen) \
  130. ( \
  131. LE_P2BYTE_TO_HOST_2BYTE(__pStart) \
  132. & \
  133. ( ~BIT_OFFSET_LEN_MASK_16(__BitOffset, __BitLen) ) \
  134. )
  135. #define SET_BITS_TO_LE_2BYTE(__pStart, __BitOffset, __BitLen, __Value) \
  136. *((u16 *)(__pStart)) = \
  137. EF2Byte( \
  138. LE_BITS_CLEARED_TO_2BYTE(__pStart, __BitOffset, __BitLen) \
  139. | \
  140. ( (((u16)__Value) & BIT_LEN_MASK_16(__BitLen)) << (__BitOffset) ) \
  141. );
  142. #define BIT_LEN_MASK_8(__BitLen) \
  143. (0xFF >> (8 - (__BitLen)))
  144. #define BIT_OFFSET_LEN_MASK_8(__BitOffset, __BitLen) \
  145. (BIT_LEN_MASK_8(__BitLen) << (__BitOffset))
  146. #define LE_P1BYTE_TO_HOST_1BYTE(__pStart) \
  147. (EF1Byte(*((u8 *)(__pStart))))
  148. #define LE_BITS_TO_1BYTE(__pStart, __BitOffset, __BitLen) \
  149. ( \
  150. ( LE_P1BYTE_TO_HOST_1BYTE(__pStart) >> (__BitOffset) ) \
  151. & \
  152. BIT_LEN_MASK_8(__BitLen) \
  153. )
  154. #define LE_BITS_CLEARED_TO_1BYTE(__pStart, __BitOffset, __BitLen) \
  155. ( \
  156. LE_P1BYTE_TO_HOST_1BYTE(__pStart) \
  157. & \
  158. ( ~BIT_OFFSET_LEN_MASK_8(__BitOffset, __BitLen) ) \
  159. )
  160. #define SET_BITS_TO_LE_1BYTE(__pStart, __BitOffset, __BitLen, __Value) \
  161. *((u8 *)(__pStart)) = \
  162. EF1Byte( \
  163. LE_BITS_CLEARED_TO_1BYTE(__pStart, __BitOffset, __BitLen) \
  164. | \
  165. ( (((u8)__Value) & BIT_LEN_MASK_8(__BitLen)) << (__BitOffset) ) \
  166. );
  167. #endif // #ifndef __INC_ENDIANFREE_H