Merge branch 'stable/cleanups-3.2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / rtl8192e / rtllib_endianfree.h
1 #ifndef __INC_ENDIANFREE_H
2 #define __INC_ENDIANFREE_H
3
4 /*
5 * Call endian free function when
6 * 1. Read/write packet content.
7 * 2. Before write integer to IO.
8 * 3. After read integer from IO.
9 */
10
11 #define __MACHINE_LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
12 #define __MACHINE_BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net, ppc */
13
14 #define BYTE_ORDER __MACHINE_LITTLE_ENDIAN
15
16 #if BYTE_ORDER == __MACHINE_LITTLE_ENDIAN
17 #define EF1Byte(_val) ((u8)(_val))
18 #define EF2Byte(_val) ((u16)(_val))
19 #define EF4Byte(_val) ((u32)(_val))
20
21 #else
22 #define EF1Byte(_val) ((u8)(_val))
23 #define EF2Byte(_val) \
24 (((((u16)(_val))&0x00ff)<<8)|((((u16)(_val))&0xff00)>>8))
25 #define EF4Byte(_val) \
26 (((((u32)(_val))&0x000000ff)<<24)|\
27 ((((u32)(_val))&0x0000ff00)<<8)|\
28 ((((u32)(_val))&0x00ff0000)>>8)|\
29 ((((u32)(_val))&0xff000000)>>24))
30 #endif
31
32 #define ReadEF1Byte(_ptr) EF1Byte(*((u8 *)(_ptr)))
33 #define ReadEF2Byte(_ptr) EF2Byte(*((u16 *)(_ptr)))
34 #define ReadEF4Byte(_ptr) EF4Byte(*((u32 *)(_ptr)))
35
36 #define WriteEF1Byte(_ptr, _val) (*((u8 *)(_ptr))) = EF1Byte(_val)
37 #define WriteEF2Byte(_ptr, _val) (*((u16 *)(_ptr))) = EF2Byte(_val)
38 #define WriteEF4Byte(_ptr, _val) (*((u32 *)(_ptr))) = EF4Byte(_val)
39 #if BYTE_ORDER == __MACHINE_LITTLE_ENDIAN
40 #define H2N1BYTE(_val) ((u8)(_val))
41 #define H2N2BYTE(_val) (((((u16)(_val))&0x00ff)<<8)|\
42 ((((u16)(_val))&0xff00)>>8))
43 #define H2N4BYTE(_val) (((((u32)(_val))&0x000000ff)<<24)|\
44 ((((u32)(_val))&0x0000ff00)<<8) |\
45 ((((u32)(_val))&0x00ff0000)>>8) |\
46 ((((u32)(_val))&0xff000000)>>24))
47 #else
48 #define H2N1BYTE(_val) ((u8)(_val))
49 #define H2N2BYTE(_val) ((u16)(_val))
50 #define H2N4BYTE(_val) ((u32)(_val))
51 #endif
52
53 #if BYTE_ORDER == __MACHINE_LITTLE_ENDIAN
54 #define N2H1BYTE(_val) ((u8)(_val))
55 #define N2H2BYTE(_val) (((((u16)(_val))&0x00ff)<<8)|\
56 ((((u16)(_val))&0xff00)>>8))
57 #define N2H4BYTE(_val) (((((u32)(_val))&0x000000ff)<<24)|\
58 ((((u32)(_val))&0x0000ff00)<<8) |\
59 ((((u32)(_val))&0x00ff0000)>>8) |\
60 ((((u32)(_val))&0xff000000)>>24))
61 #else
62 #define N2H1BYTE(_val) ((u8)(_val))
63 #define N2H2BYTE(_val) ((u16)(_val))
64 #define N2H4BYTE(_val) ((u32)(_val))
65 #endif
66
67 #define BIT_LEN_MASK_32(__BitLen) (0xFFFFFFFF >> (32 - (__BitLen)))
68 #define BIT_OFFSET_LEN_MASK_32(__BitOffset, __BitLen) \
69 (BIT_LEN_MASK_32(__BitLen) << (__BitOffset))
70
71 #define LE_P4BYTE_TO_HOST_4BYTE(__pStart) (EF4Byte(*((u32 *)(__pStart))))
72
73 #define LE_BITS_TO_4BYTE(__pStart, __BitOffset, __BitLen) \
74 ( \
75 (LE_P4BYTE_TO_HOST_4BYTE(__pStart) >> (__BitOffset)) \
76 & \
77 BIT_LEN_MASK_32(__BitLen) \
78 )
79
80 #define LE_BITS_CLEARED_TO_4BYTE(__pStart, __BitOffset, __BitLen) \
81 ( \
82 LE_P4BYTE_TO_HOST_4BYTE(__pStart) \
83 & \
84 (~BIT_OFFSET_LEN_MASK_32(__BitOffset, __BitLen)) \
85 )
86
87 #define SET_BITS_TO_LE_4BYTE(__pStart, __BitOffset, __BitLen, __Value) \
88 *((u32 *)(__pStart)) = \
89 EF4Byte( \
90 LE_BITS_CLEARED_TO_4BYTE(__pStart, __BitOffset, __BitLen) \
91 | \
92 ((((u32)__Value) & BIT_LEN_MASK_32(__BitLen)) << (__BitOffset)) \
93 );
94
95
96 #define BIT_LEN_MASK_16(__BitLen) \
97 (0xFFFF >> (16 - (__BitLen)))
98
99 #define BIT_OFFSET_LEN_MASK_16(__BitOffset, __BitLen) \
100 (BIT_LEN_MASK_16(__BitLen) << (__BitOffset))
101
102 #define LE_P2BYTE_TO_HOST_2BYTE(__pStart) \
103 (EF2Byte(*((u16 *)(__pStart))))
104
105 #define LE_BITS_TO_2BYTE(__pStart, __BitOffset, __BitLen) \
106 ( \
107 (LE_P2BYTE_TO_HOST_2BYTE(__pStart) >> (__BitOffset)) \
108 & \
109 BIT_LEN_MASK_16(__BitLen) \
110 )
111
112 #define LE_BITS_CLEARED_TO_2BYTE(__pStart, __BitOffset, __BitLen) \
113 ( \
114 LE_P2BYTE_TO_HOST_2BYTE(__pStart) \
115 & \
116 (~BIT_OFFSET_LEN_MASK_16(__BitOffset, __BitLen)) \
117 )
118
119 #define SET_BITS_TO_LE_2BYTE(__pStart, __BitOffset, __BitLen, __Value) \
120 *((u16 *)(__pStart)) = \
121 EF2Byte( \
122 LE_BITS_CLEARED_TO_2BYTE(__pStart, __BitOffset, __BitLen) \
123 | ((((u16)__Value) & BIT_LEN_MASK_16(__BitLen)) << \
124 (__BitOffset)) \
125 );
126
127 #define BIT_LEN_MASK_8(__BitLen) \
128 (0xFF >> (8 - (__BitLen)))
129
130 #define BIT_OFFSET_LEN_MASK_8(__BitOffset, __BitLen) \
131 (BIT_LEN_MASK_8(__BitLen) << (__BitOffset))
132
133 #define LE_P1BYTE_TO_HOST_1BYTE(__pStart) \
134 (EF1Byte(*((u8 *)(__pStart))))
135
136 #define LE_BITS_TO_1BYTE(__pStart, __BitOffset, __BitLen) \
137 ( \
138 (LE_P1BYTE_TO_HOST_1BYTE(__pStart) >> (__BitOffset)) \
139 & \
140 BIT_LEN_MASK_8(__BitLen) \
141 )
142
143 #define LE_BITS_CLEARED_TO_1BYTE(__pStart, __BitOffset, __BitLen) \
144 ( \
145 LE_P1BYTE_TO_HOST_1BYTE(__pStart) \
146 & \
147 (~BIT_OFFSET_LEN_MASK_8(__BitOffset, __BitLen)) \
148 )
149
150 #define SET_BITS_TO_LE_1BYTE(__pStart, __BitOffset, __BitLen, __Value) \
151 *((u8 *)(__pStart)) = EF1Byte( \
152 LE_BITS_CLEARED_TO_1BYTE(__pStart, __BitOffset, __BitLen) \
153 | ((((u8)__Value) & BIT_LEN_MASK_8(__BitLen)) << \
154 (__BitOffset)) \
155 );
156
157 #define N_BYTE_ALIGMENT(__Value, __Aligment) \
158 ((__Aligment == 1) ? (__Value) : (((__Value + __Aligment - 1) / \
159 __Aligment) * __Aligment))
160 #endif