basic_types.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2007 - 2017 Realtek Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. *****************************************************************************/
  15. #ifndef __BASIC_TYPES_H__
  16. #define __BASIC_TYPES_H__
  17. #define SUCCESS 0
  18. #define FAIL (-1)
  19. #ifndef TRUE
  20. #define _TRUE 1
  21. #else
  22. #define _TRUE TRUE
  23. #endif
  24. #ifndef FALSE
  25. #define _FALSE 0
  26. #else
  27. #define _FALSE FALSE
  28. #endif
  29. #ifdef PLATFORM_WINDOWS
  30. typedef signed char s8;
  31. typedef unsigned char u8;
  32. typedef signed short s16;
  33. typedef unsigned short u16;
  34. typedef signed long s32;
  35. typedef unsigned long u32;
  36. typedef unsigned int uint;
  37. typedef signed int sint;
  38. typedef signed long long s64;
  39. typedef unsigned long long u64;
  40. #ifdef NDIS50_MINIPORT
  41. #define NDIS_MAJOR_VERSION 5
  42. #define NDIS_MINOR_VERSION 0
  43. #endif
  44. #ifdef NDIS51_MINIPORT
  45. #define NDIS_MAJOR_VERSION 5
  46. #define NDIS_MINOR_VERSION 1
  47. #endif
  48. typedef NDIS_PROC proc_t;
  49. typedef LONG atomic_t;
  50. #endif
  51. #ifdef PLATFORM_LINUX
  52. #include <linux/version.h>
  53. #include <linux/types.h>
  54. #include <linux/module.h>
  55. #include <linux/kernel.h>
  56. #include <linux/init.h>
  57. #include <linux/utsname.h>
  58. #define IN
  59. #define OUT
  60. #define VOID void
  61. #define NDIS_OID uint
  62. #define NDIS_STATUS uint
  63. typedef signed int sint;
  64. #ifndef PVOID
  65. typedef void *PVOID;
  66. /* #define PVOID (void *) */
  67. #endif
  68. #define UCHAR u8
  69. #define USHORT u16
  70. #define UINT u32
  71. #define ULONG u32
  72. #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
  73. typedef _Bool bool;
  74. enum {
  75. false = 0,
  76. true = 1
  77. };
  78. #endif
  79. typedef void (*proc_t)(void *);
  80. typedef __kernel_size_t SIZE_T;
  81. typedef __kernel_ssize_t SSIZE_T;
  82. #define FIELD_OFFSET(s, field) ((SSIZE_T)&((s *)(0))->field)
  83. #define u1Byte u8
  84. #define pu1Byte u8*
  85. #define u2Byte u16
  86. #define pu2Byte u16*
  87. #define u4Byte u32
  88. #define pu4Byte u32*
  89. #define u8Byte u64
  90. #define pu8Byte u64*
  91. #define s1Byte s8
  92. #define ps1Byte s8*
  93. #define s2Byte s16
  94. #define ps2Byte s16*
  95. #define s4Byte s32
  96. #define ps4Byte s32*
  97. #define s8Byte s64
  98. #define ps8Byte s64*
  99. #define UCHAR u8
  100. #define USHORT u16
  101. #define UINT u32
  102. #define ULONG u32
  103. #define PULONG u32*
  104. #endif
  105. #ifdef PLATFORM_FREEBSD
  106. typedef signed char s8;
  107. typedef unsigned char u8;
  108. typedef signed short s16;
  109. typedef unsigned short u16;
  110. typedef signed int s32;
  111. typedef unsigned int u32;
  112. typedef unsigned int uint;
  113. typedef signed int sint;
  114. typedef long atomic_t;
  115. typedef signed long long s64;
  116. typedef unsigned long long u64;
  117. #define IN
  118. #define OUT
  119. #define VOID void
  120. #define NDIS_OID uint
  121. #define NDIS_STATUS uint
  122. #ifndef PVOID
  123. typedef void *PVOID;
  124. /* #define PVOID (void *) */
  125. #endif
  126. typedef u32 dma_addr_t;
  127. #define UCHAR u8
  128. #define USHORT u16
  129. #define UINT u32
  130. #define ULONG u32
  131. typedef void (*proc_t)(void *);
  132. typedef unsigned int __kernel_size_t;
  133. typedef int __kernel_ssize_t;
  134. typedef __kernel_size_t SIZE_T;
  135. typedef __kernel_ssize_t SSIZE_T;
  136. #define FIELD_OFFSET(s, field) ((SSIZE_T)&((s *)(0))->field)
  137. #endif
  138. #define MEM_ALIGNMENT_OFFSET (sizeof (SIZE_T))
  139. #define MEM_ALIGNMENT_PADDING (sizeof(SIZE_T) - 1)
  140. #define SIZE_PTR SIZE_T
  141. #define SSIZE_PTR SSIZE_T
  142. /*
  143. * Continuous bits starting from least significant bit
  144. * Example:
  145. * BIT_LEN_MASK_32(0) => 0x00000000
  146. * BIT_LEN_MASK_32(1) => 0x00000001
  147. * BIT_LEN_MASK_32(2) => 0x00000003
  148. * BIT_LEN_MASK_32(32) => 0xFFFFFFFF
  149. */
  150. #define BIT_LEN_MASK_32(__BitLen) ((u32)(0xFFFFFFFF >> (32 - (__BitLen))))
  151. #define BIT_LEN_MASK_16(__BitLen) ((u16)(0xFFFF >> (16 - (__BitLen))))
  152. #define BIT_LEN_MASK_8(__BitLen) ((u8)(0xFF >> (8 - (__BitLen))))
  153. /*
  154. * Continuous bits starting from least significant bit
  155. * Example:
  156. * BIT_OFFSET_LEN_MASK_32(0, 2) => 0x00000003
  157. * BIT_OFFSET_LEN_MASK_32(16, 2) => 0x00030000
  158. */
  159. #define BIT_OFFSET_LEN_MASK_32(__BitOffset, __BitLen) ((u32)(BIT_LEN_MASK_32(__BitLen) << (__BitOffset)))
  160. #define BIT_OFFSET_LEN_MASK_16(__BitOffset, __BitLen) ((u16)(BIT_LEN_MASK_16(__BitLen) << (__BitOffset)))
  161. #define BIT_OFFSET_LEN_MASK_8(__BitOffset, __BitLen) ((u8)(BIT_LEN_MASK_8(__BitLen) << (__BitOffset)))
  162. /*
  163. * Convert LE data to host byte order
  164. */
  165. #define EF1Byte (u8)
  166. #define EF2Byte le16_to_cpu
  167. #define EF4Byte le32_to_cpu
  168. /*
  169. * Read LE data from memory to host byte order
  170. */
  171. #define ReadLE4Byte(_ptr) le32_to_cpu(*((u32 *)(_ptr)))
  172. #define ReadLE2Byte(_ptr) le16_to_cpu(*((u16 *)(_ptr)))
  173. #define ReadLE1Byte(_ptr) (*((u8 *)(_ptr)))
  174. /*
  175. * Read BE data from memory to host byte order
  176. */
  177. #define ReadBEE4Byte(_ptr) be32_to_cpu(*((u32 *)(_ptr)))
  178. #define ReadBE2Byte(_ptr) be16_to_cpu(*((u16 *)(_ptr)))
  179. #define ReadBE1Byte(_ptr) (*((u8 *)(_ptr)))
  180. /*
  181. * Write host byte order data to memory in LE order
  182. */
  183. #define WriteLE4Byte(_ptr, _val) ((*((u32 *)(_ptr))) = cpu_to_le32(_val))
  184. #define WriteLE2Byte(_ptr, _val) ((*((u16 *)(_ptr))) = cpu_to_le16(_val))
  185. #define WriteLE1Byte(_ptr, _val) ((*((u8 *)(_ptr))) = ((u8)(_val)))
  186. /*
  187. * Write host byte order data to memory in BE order
  188. */
  189. #define WriteBE4Byte(_ptr, _val) ((*((u32 *)(_ptr))) = cpu_to_be32(_val))
  190. #define WriteBE2Byte(_ptr, _val) ((*((u16 *)(_ptr))) = cpu_to_be16(_val))
  191. #define WriteBE1Byte(_ptr, _val) ((*((u8 *)(_ptr))) = ((u8)(_val)))
  192. /*
  193. * Return 4-byte value in host byte ordering from 4-byte pointer in litten-endian system.
  194. */
  195. #define LE_P4BYTE_TO_HOST_4BYTE(__pStart) (le32_to_cpu(*((u32 *)(__pStart))))
  196. #define LE_P2BYTE_TO_HOST_2BYTE(__pStart) (le16_to_cpu(*((u16 *)(__pStart))))
  197. #define LE_P1BYTE_TO_HOST_1BYTE(__pStart) ((*((u8 *)(__pStart))))
  198. /*
  199. * Return 4-byte value in host byte ordering from 4-byte pointer in big-endian system.
  200. */
  201. #define BE_P4BYTE_TO_HOST_4BYTE(__pStart) (be32_to_cpu(*((u32 *)(__pStart))))
  202. #define BE_P2BYTE_TO_HOST_2BYTE(__pStart) (be16_to_cpu(*((u16 *)(__pStart))))
  203. #define BE_P1BYTE_TO_HOST_1BYTE(__pStart) ((*((u8 *)(__pStart))))
  204. /*
  205. * Translate subfield (continuous bits in little-endian) of 4-byte value in LE byte to
  206. * 4-byte value in host byte ordering.
  207. */
  208. #define LE_BITS_TO_4BYTE(__pStart, __BitOffset, __BitLen) \
  209. ((LE_P4BYTE_TO_HOST_4BYTE(__pStart) >> (__BitOffset)) & BIT_LEN_MASK_32(__BitLen))
  210. #define LE_BITS_TO_2BYTE(__pStart, __BitOffset, __BitLen) \
  211. ((LE_P2BYTE_TO_HOST_2BYTE(__pStart) >> (__BitOffset)) & BIT_LEN_MASK_16(__BitLen))
  212. #define LE_BITS_TO_1BYTE(__pStart, __BitOffset, __BitLen) \
  213. ((LE_P1BYTE_TO_HOST_1BYTE(__pStart) >> (__BitOffset)) & BIT_LEN_MASK_8(__BitLen))
  214. /*
  215. * Translate subfield (continuous bits in big-endian) of 4-byte value in BE byte to
  216. * 4-byte value in host byte ordering.
  217. */
  218. #define BE_BITS_TO_4BYTE(__pStart, __BitOffset, __BitLen) \
  219. ((BE_P4BYTE_TO_HOST_4BYTE(__pStart) >> (__BitOffset)) & BIT_LEN_MASK_32(__BitLen))
  220. #define BE_BITS_TO_2BYTE(__pStart, __BitOffset, __BitLen) \
  221. ((BE_P2BYTE_TO_HOST_2BYTE(__pStart) >> (__BitOffset)) & BIT_LEN_MASK_16(__BitLen))
  222. #define BE_BITS_TO_1BYTE(__pStart, __BitOffset, __BitLen) \
  223. ((BE_P1BYTE_TO_HOST_1BYTE(__pStart) >> (__BitOffset)) & BIT_LEN_MASK_8(__BitLen))
  224. /*
  225. * Mask subfield (continuous bits in little-endian) of 4-byte value in LE byte oredering
  226. * and return the result in 4-byte value in host byte ordering.
  227. */
  228. #define LE_BITS_CLEARED_TO_4BYTE(__pStart, __BitOffset, __BitLen) \
  229. (LE_P4BYTE_TO_HOST_4BYTE(__pStart) & (~BIT_OFFSET_LEN_MASK_32(__BitOffset, __BitLen)))
  230. #define LE_BITS_CLEARED_TO_2BYTE(__pStart, __BitOffset, __BitLen) \
  231. (LE_P2BYTE_TO_HOST_2BYTE(__pStart) & (~BIT_OFFSET_LEN_MASK_16(__BitOffset, __BitLen)))
  232. #define LE_BITS_CLEARED_TO_1BYTE(__pStart, __BitOffset, __BitLen) \
  233. (LE_P1BYTE_TO_HOST_1BYTE(__pStart) & ((u8)(~BIT_OFFSET_LEN_MASK_8(__BitOffset, __BitLen))))
  234. /*
  235. * Mask subfield (continuous bits in big-endian) of 4-byte value in BE byte oredering
  236. * and return the result in 4-byte value in host byte ordering.
  237. */
  238. #define BE_BITS_CLEARED_TO_4BYTE(__pStart, __BitOffset, __BitLen) \
  239. (BE_P4BYTE_TO_HOST_4BYTE(__pStart) & (~BIT_OFFSET_LEN_MASK_32(__BitOffset, __BitLen)))
  240. #define BE_BITS_CLEARED_TO_2BYTE(__pStart, __BitOffset, __BitLen) \
  241. (BE_P2BYTE_TO_HOST_2BYTE(__pStart) & (~BIT_OFFSET_LEN_MASK_16(__BitOffset, __BitLen)))
  242. #define BE_BITS_CLEARED_TO_1BYTE(__pStart, __BitOffset, __BitLen) \
  243. (BE_P1BYTE_TO_HOST_1BYTE(__pStart) & (~BIT_OFFSET_LEN_MASK_8(__BitOffset, __BitLen)))
  244. /*
  245. * Set subfield of little-endian 4-byte value to specified value.
  246. */
  247. #define SET_BITS_TO_LE_4BYTE(__pStart, __BitOffset, __BitLen, __Value) \
  248. do { \
  249. if (__BitOffset == 0 && __BitLen == 32) \
  250. WriteLE4Byte(__pStart, __Value); \
  251. else { \
  252. WriteLE4Byte(__pStart, \
  253. LE_BITS_CLEARED_TO_4BYTE(__pStart, __BitOffset, __BitLen) \
  254. | \
  255. ((((u32)__Value) & BIT_LEN_MASK_32(__BitLen)) << (__BitOffset)) \
  256. ); \
  257. } \
  258. } while (0)
  259. #define SET_BITS_TO_LE_2BYTE(__pStart, __BitOffset, __BitLen, __Value) \
  260. do { \
  261. if (__BitOffset == 0 && __BitLen == 16) \
  262. WriteLE2Byte(__pStart, __Value); \
  263. else { \
  264. WriteLE2Byte(__pStart, \
  265. LE_BITS_CLEARED_TO_2BYTE(__pStart, __BitOffset, __BitLen) \
  266. | \
  267. ((((u16)__Value) & BIT_LEN_MASK_16(__BitLen)) << (__BitOffset)) \
  268. ); \
  269. } \
  270. } while (0)
  271. #define SET_BITS_TO_LE_1BYTE(__pStart, __BitOffset, __BitLen, __Value) \
  272. do { \
  273. if (__BitOffset == 0 && __BitLen == 8) \
  274. WriteLE1Byte(__pStart, __Value); \
  275. else { \
  276. WriteLE1Byte(__pStart, \
  277. LE_BITS_CLEARED_TO_1BYTE(__pStart, __BitOffset, __BitLen) \
  278. | \
  279. ((((u8)__Value) & BIT_LEN_MASK_8(__BitLen)) << (__BitOffset)) \
  280. ); \
  281. } \
  282. } while (0)
  283. /*
  284. * Set subfield of big-endian 4-byte value to specified value.
  285. */
  286. #define SET_BITS_TO_BE_4BYTE(__pStart, __BitOffset, __BitLen, __Value) \
  287. do { \
  288. if (__BitOffset == 0 && __BitLen == 32) \
  289. WriteBE4Byte(__pStart, __Value); \
  290. else { \
  291. WriteBE4Byte(__pStart, \
  292. BE_BITS_CLEARED_TO_4BYTE(__pStart, __BitOffset, __BitLen) \
  293. | \
  294. ((((u32)__Value) & BIT_LEN_MASK_32(__BitLen)) << (__BitOffset)) \
  295. ); \
  296. } \
  297. } while (0)
  298. #define SET_BITS_TO_BE_2BYTE(__pStart, __BitOffset, __BitLen, __Value) \
  299. do { \
  300. if (__BitOffset == 0 && __BitLen == 16) \
  301. WriteBE2Byte(__pStart, __Value); \
  302. else { \
  303. WriteBE2Byte(__pStart, \
  304. BE_BITS_CLEARED_TO_2BYTE(__pStart, __BitOffset, __BitLen) \
  305. | \
  306. ((((u16)__Value) & BIT_LEN_MASK_16(__BitLen)) << (__BitOffset)) \
  307. ); \
  308. } \
  309. } while (0)
  310. #define SET_BITS_TO_BE_1BYTE(__pStart, __BitOffset, __BitLen, __Value) \
  311. do { \
  312. if (__BitOffset == 0 && __BitLen == 8) \
  313. WriteBE1Byte(__pStart, __Value); \
  314. else { \
  315. WriteBE1Byte(__pStart, \
  316. BE_BITS_CLEARED_TO_1BYTE(__pStart, __BitOffset, __BitLen) \
  317. | \
  318. ((((u8)__Value) & BIT_LEN_MASK_8(__BitLen)) << (__BitOffset)) \
  319. ); \
  320. } \
  321. } while (0)
  322. /* Get the N-bytes aligment offset from the current length */
  323. #define N_BYTE_ALIGMENT(__Value, __Aligment) ((__Aligment == 1) ? (__Value) : (((__Value + __Aligment - 1) / __Aligment) * __Aligment))
  324. typedef unsigned char BOOLEAN, *PBOOLEAN, boolean;
  325. #define TEST_FLAG(__Flag, __testFlag) (((__Flag) & (__testFlag)) != 0)
  326. #define SET_FLAG(__Flag, __setFlag) ((__Flag) |= __setFlag)
  327. #define CLEAR_FLAG(__Flag, __clearFlag) ((__Flag) &= ~(__clearFlag))
  328. #define CLEAR_FLAGS(__Flag) ((__Flag) = 0)
  329. #define TEST_FLAGS(__Flag, __testFlags) (((__Flag) & (__testFlags)) == (__testFlags))
  330. #endif /* __BASIC_TYPES_H__ */