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