xref: /linux/drivers/staging/rtl8723bs/include/basic_types.h (revision 8be98d2f2a0a262f8bf8a0bc1fdf522b3c7aab17)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 #ifndef __BASIC_TYPES_H__
8 #define __BASIC_TYPES_H__
9 
10 
11 #define SUCCESS	0
12 #define FAIL	(-1)
13 
14 #include <linux/types.h>
15 
16 #define FIELD_OFFSET(s, field)	((__kernel_ssize_t)&((s *)(0))->field)
17 
18 #define SIZE_PTR __kernel_size_t
19 #define SSIZE_PTR __kernel_ssize_t
20 
21 /* port from fw by thomas */
22 /*  TODO: Belows are Sync from SD7-Driver. It is necessary to check correctness */
23 
24 /*
25  *Call endian free function when
26  *	1. Read/write packet content.
27  *	2. Before write integer to IO.
28  *	3. After read integer from IO.
29 */
30 
31 /*  */
32 /*  Byte Swapping routine. */
33 /*  */
34 #define EF1Byte	(u8)
35 #define EF2Byte		le16_to_cpu
36 #define EF4Byte	le32_to_cpu
37 
38 /* Convert little data endian to host ordering */
39 #define EF1BYTE(_val)		\
40 	((u8)(_val))
41 #define EF2BYTE(_val)		\
42 	(le16_to_cpu(_val))
43 #define EF4BYTE(_val)		\
44 	(le32_to_cpu(_val))
45 
46 /* Read data from memory */
47 #define READEF1BYTE(_ptr)	\
48 	EF1BYTE(*((u8 *)(_ptr)))
49 /* Read le16 data from memory and convert to host ordering */
50 #define READEF2BYTE(_ptr)	\
51 	EF2BYTE(*(_ptr))
52 #define READEF4BYTE(_ptr)	\
53 	EF4BYTE(*(_ptr))
54 
55 /* Write data to memory */
56 #define WRITEEF1BYTE(_ptr, _val)			\
57 	do {						\
58 		(*((u8 *)(_ptr))) = EF1BYTE(_val);	\
59 	} while (0)
60 /* Write le data to memory in host ordering */
61 #define WRITEEF2BYTE(_ptr, _val)			\
62 	do {						\
63 		(*((u16 *)(_ptr))) = EF2BYTE(_val);	\
64 	} while (0)
65 
66 #define WRITEEF4BYTE(_ptr, _val)			\
67 	do {						\
68 		(*((u32 *)(_ptr))) = EF2BYTE(_val);	\
69 	} while (0)
70 
71 /* Create a bit mask
72  * Examples:
73  * BIT_LEN_MASK_32(0) => 0x00000000
74  * BIT_LEN_MASK_32(1) => 0x00000001
75  * BIT_LEN_MASK_32(2) => 0x00000003
76  * BIT_LEN_MASK_32(32) => 0xFFFFFFFF
77  */
78 #define BIT_LEN_MASK_32(__bitlen)	 \
79 	(0xFFFFFFFF >> (32 - (__bitlen)))
80 #define BIT_LEN_MASK_16(__bitlen)	 \
81 	(0xFFFF >> (16 - (__bitlen)))
82 #define BIT_LEN_MASK_8(__bitlen) \
83 	(0xFF >> (8 - (__bitlen)))
84 
85 /* Create an offset bit mask
86  * Examples:
87  * BIT_OFFSET_LEN_MASK_32(0, 2) => 0x00000003
88  * BIT_OFFSET_LEN_MASK_32(16, 2) => 0x00030000
89  */
90 #define BIT_OFFSET_LEN_MASK_32(__bitoffset, __bitlen) \
91 	(BIT_LEN_MASK_32(__bitlen) << (__bitoffset))
92 #define BIT_OFFSET_LEN_MASK_16(__bitoffset, __bitlen) \
93 	(BIT_LEN_MASK_16(__bitlen) << (__bitoffset))
94 #define BIT_OFFSET_LEN_MASK_8(__bitoffset, __bitlen) \
95 	(BIT_LEN_MASK_8(__bitlen) << (__bitoffset))
96 
97 /*Description:
98  * Return 4-byte value in host byte ordering from
99  * 4-byte pointer in little-endian system.
100  */
101 #define LE_P4BYTE_TO_HOST_4BYTE(__pstart) \
102 	(EF4BYTE(*((__le32 *)(__pstart))))
103 #define LE_P2BYTE_TO_HOST_2BYTE(__pstart) \
104 	(EF2BYTE(*((__le16 *)(__pstart))))
105 #define LE_P1BYTE_TO_HOST_1BYTE(__pstart) \
106 	(EF1BYTE(*((u8 *)(__pstart))))
107 
108 /*  */
109 /* 	Description: */
110 /* 		Translate subfield (continuous bits in little-endian) of 4-byte value in litten byte to */
111 /* 		4-byte value in host byte ordering. */
112 /*  */
113 #define LE_BITS_TO_4BYTE(__pstart, __bitoffset, __bitlen) \
114 	(\
115 		(LE_P4BYTE_TO_HOST_4BYTE(__pstart) >> (__bitoffset))  & \
116 		BIT_LEN_MASK_32(__bitlen) \
117 	)
118 #define LE_BITS_TO_2BYTE(__pstart, __bitoffset, __bitlen) \
119 	(\
120 		(LE_P2BYTE_TO_HOST_2BYTE(__pstart) >> (__bitoffset)) & \
121 		BIT_LEN_MASK_16(__bitlen) \
122 	)
123 #define LE_BITS_TO_1BYTE(__pstart, __bitoffset, __bitlen) \
124 	(\
125 		(LE_P1BYTE_TO_HOST_1BYTE(__pstart) >> (__bitoffset)) & \
126 		BIT_LEN_MASK_8(__bitlen) \
127 	)
128 
129 /*  */
130 /* 	Description: */
131 /* 		Mask subfield (continuous bits in little-endian) of 4-byte value in litten byte oredering */
132 /* 		and return the result in 4-byte value in host byte ordering. */
133 /*  */
134 #define LE_BITS_CLEARED_TO_4BYTE(__pstart, __bitoffset, __bitlen) \
135 	(\
136 		LE_P4BYTE_TO_HOST_4BYTE(__pstart)  & \
137 		(~BIT_OFFSET_LEN_MASK_32(__bitoffset, __bitlen)) \
138 	)
139 #define LE_BITS_CLEARED_TO_2BYTE(__pstart, __bitoffset, __bitlen) \
140 	(\
141 		LE_P2BYTE_TO_HOST_2BYTE(__pstart) & \
142 		(~BIT_OFFSET_LEN_MASK_16(__bitoffset, __bitlen)) \
143 	)
144 #define LE_BITS_CLEARED_TO_1BYTE(__pstart, __bitoffset, __bitlen) \
145 	(\
146 		LE_P1BYTE_TO_HOST_1BYTE(__pstart) & \
147 		(~BIT_OFFSET_LEN_MASK_8(__bitoffset, __bitlen)) \
148 	)
149 
150 /*  */
151 /* 	Description: */
152 /* 		Set subfield of little-endian 4-byte value to specified value. */
153 /*  */
154 #define SET_BITS_TO_LE_4BYTE(__pstart, __bitoffset, __bitlen, __val) \
155 		*((u32 *)(__pstart)) =				\
156 		(						\
157 		LE_BITS_CLEARED_TO_4BYTE(__pstart, __bitoffset, __bitlen) | \
158 		((((u32)__val) & BIT_LEN_MASK_32(__bitlen)) << (__bitoffset)) \
159 		)
160 
161 #define SET_BITS_TO_LE_2BYTE(__pstart, __bitoffset, __bitlen, __val) \
162 		*((u16 *)(__pstart)) =				\
163 		(					\
164 		LE_BITS_CLEARED_TO_2BYTE(__pstart, __bitoffset, __bitlen) | \
165 		((((u16)__val) & BIT_LEN_MASK_16(__bitlen)) << (__bitoffset)) \
166 		);
167 
168 #define SET_BITS_TO_LE_1BYTE(__pstart, __bitoffset, __bitlen, __val) \
169 		*((u8 *)(__pstart)) = EF1BYTE			\
170 		(					\
171 		LE_BITS_CLEARED_TO_1BYTE(__pstart, __bitoffset, __bitlen) | \
172 		((((u8)__val) & BIT_LEN_MASK_8(__bitlen)) << (__bitoffset)) \
173 		)
174 
175 #define LE_BITS_CLEARED_TO_1BYTE_8BIT(__pStart, __BitOffset, __BitLen) \
176 	(\
177 		LE_P1BYTE_TO_HOST_1BYTE(__pStart) \
178 	)
179 
180 #define SET_BITS_TO_LE_1BYTE_8BIT(__pStart, __BitOffset, __BitLen, __Value) \
181 { \
182 	*((u8 *)(__pStart)) = \
183 		EF1Byte(\
184 			LE_BITS_CLEARED_TO_1BYTE_8BIT(__pStart, __BitOffset, __BitLen) \
185 			| \
186 			((u8)__Value) \
187 		); \
188 }
189 
190 /*  Get the N-bytes alignent offset from the current length */
191 #define N_BYTE_ALIGMENT(__Value, __Aligment) ((__Aligment == 1) ? (__Value) : (((__Value + __Aligment - 1) / __Aligment) * __Aligment))
192 
193 #define TEST_FLAG(__Flag, __testFlag)		(((__Flag) & (__testFlag)) != 0)
194 #define SET_FLAG(__Flag, __setFlag)			((__Flag) |= __setFlag)
195 #define CLEAR_FLAG(__Flag, __clearFlag)		((__Flag) &= ~(__clearFlag))
196 #define CLEAR_FLAGS(__Flag)					((__Flag) = 0)
197 #define TEST_FLAGS(__Flag, __testFlags)		(((__Flag) & (__testFlags)) == (__testFlags))
198 
199 #endif /* __BASIC_TYPES_H__ */
200