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