generic.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 _LINUX_BYTEORDER_GENERIC_H
  16. #define _LINUX_BYTEORDER_GENERIC_H
  17. /*
  18. * linux/byteorder_generic.h
  19. * Generic Byte-reordering support
  20. *
  21. * Francois-Rene Rideau <fare@tunes.org> 19970707
  22. * gathered all the good ideas from all asm-foo/byteorder.h into one file,
  23. * cleaned them up.
  24. * I hope it is compliant with non-GCC compilers.
  25. * I decided to put __BYTEORDER_HAS_U64__ in byteorder.h,
  26. * because I wasn't sure it would be ok to put it in types.h
  27. * Upgraded it to 2.1.43
  28. * Francois-Rene Rideau <fare@tunes.org> 19971012
  29. * Upgraded it to 2.1.57
  30. * to please Linus T., replaced huge #ifdef's between little/big endian
  31. * by nestedly #include'd files.
  32. * Francois-Rene Rideau <fare@tunes.org> 19971205
  33. * Made it to 2.1.71; now a facelift:
  34. * Put files under include/linux/byteorder/
  35. * Split swab from generic support.
  36. *
  37. * TODO:
  38. * = Regular kernel maintainers could also replace all these manual
  39. * byteswap macros that remain, disseminated among drivers,
  40. * after some grep or the sources...
  41. * = Linus might want to rename all these macros and files to fit his taste,
  42. * to fit his personal naming scheme.
  43. * = it seems that a few drivers would also appreciate
  44. * nybble swapping support...
  45. * = every architecture could add their byteswap macro in asm/byteorder.h
  46. * see how some architectures already do (i386, alpha, ppc, etc)
  47. * = cpu_to_beXX and beXX_to_cpu might some day need to be well
  48. * distinguished throughout the kernel. This is not the case currently,
  49. * since little endian, big endian, and pdp endian machines needn't it.
  50. * But this might be the case for, say, a port of Linux to 20/21 bit
  51. * architectures (and F21 Linux addict around?).
  52. */
  53. /*
  54. * The following macros are to be defined by <asm/byteorder.h>:
  55. *
  56. * Conversion of long and short int between network and host format
  57. * ntohl(__u32 x)
  58. * ntohs(__u16 x)
  59. * htonl(__u32 x)
  60. * htons(__u16 x)
  61. * It seems that some programs (which? where? or perhaps a standard? POSIX?)
  62. * might like the above to be functions, not macros (why?).
  63. * if that's true, then detect them, and take measures.
  64. * Anyway, the measure is: define only ___ntohl as a macro instead,
  65. * and in a separate file, have
  66. * unsigned long inline ntohl(x){return ___ntohl(x);}
  67. *
  68. * The same for constant arguments
  69. * __constant_ntohl(__u32 x)
  70. * __constant_ntohs(__u16 x)
  71. * __constant_htonl(__u32 x)
  72. * __constant_htons(__u16 x)
  73. *
  74. * Conversion of XX-bit integers (16- 32- or 64-)
  75. * between native CPU format and little/big endian format
  76. * 64-bit stuff only defined for proper architectures
  77. * cpu_to_[bl]eXX(__uXX x)
  78. * [bl]eXX_to_cpu(__uXX x)
  79. *
  80. * The same, but takes a pointer to the value to convert
  81. * cpu_to_[bl]eXXp(__uXX x)
  82. * [bl]eXX_to_cpup(__uXX x)
  83. *
  84. * The same, but change in situ
  85. * cpu_to_[bl]eXXs(__uXX x)
  86. * [bl]eXX_to_cpus(__uXX x)
  87. *
  88. * See asm-foo/byteorder.h for examples of how to provide
  89. * architecture-optimized versions
  90. *
  91. */
  92. #if defined(PLATFORM_LINUX) || defined(PLATFORM_WINDOWS) || defined(PLATFORM_MPIXEL) || defined(PLATFORM_FREEBSD)
  93. /*
  94. * inside the kernel, we can use nicknames;
  95. * outside of it, we must avoid POSIX namespace pollution...
  96. */
  97. #define cpu_to_le64 __cpu_to_le64
  98. #define le64_to_cpu __le64_to_cpu
  99. #define cpu_to_le32 __cpu_to_le32
  100. #define le32_to_cpu __le32_to_cpu
  101. #define cpu_to_le16 __cpu_to_le16
  102. #define le16_to_cpu __le16_to_cpu
  103. #define cpu_to_be64 __cpu_to_be64
  104. #define be64_to_cpu __be64_to_cpu
  105. #define cpu_to_be32 __cpu_to_be32
  106. #define be32_to_cpu __be32_to_cpu
  107. #define cpu_to_be16 __cpu_to_be16
  108. #define be16_to_cpu __be16_to_cpu
  109. #define cpu_to_le64p __cpu_to_le64p
  110. #define le64_to_cpup __le64_to_cpup
  111. #define cpu_to_le32p __cpu_to_le32p
  112. #define le32_to_cpup __le32_to_cpup
  113. #define cpu_to_le16p __cpu_to_le16p
  114. #define le16_to_cpup __le16_to_cpup
  115. #define cpu_to_be64p __cpu_to_be64p
  116. #define be64_to_cpup __be64_to_cpup
  117. #define cpu_to_be32p __cpu_to_be32p
  118. #define be32_to_cpup __be32_to_cpup
  119. #define cpu_to_be16p __cpu_to_be16p
  120. #define be16_to_cpup __be16_to_cpup
  121. #define cpu_to_le64s __cpu_to_le64s
  122. #define le64_to_cpus __le64_to_cpus
  123. #define cpu_to_le32s __cpu_to_le32s
  124. #define le32_to_cpus __le32_to_cpus
  125. #define cpu_to_le16s __cpu_to_le16s
  126. #define le16_to_cpus __le16_to_cpus
  127. #define cpu_to_be64s __cpu_to_be64s
  128. #define be64_to_cpus __be64_to_cpus
  129. #define cpu_to_be32s __cpu_to_be32s
  130. #define be32_to_cpus __be32_to_cpus
  131. #define cpu_to_be16s __cpu_to_be16s
  132. #define be16_to_cpus __be16_to_cpus
  133. #endif
  134. /*
  135. * Handle ntohl and suches. These have various compatibility
  136. * issues - like we want to give the prototype even though we
  137. * also have a macro for them in case some strange program
  138. * wants to take the address of the thing or something..
  139. *
  140. * Note that these used to return a "long" in libc5, even though
  141. * long is often 64-bit these days.. Thus the casts.
  142. *
  143. * They have to be macros in order to do the constant folding
  144. * correctly - if the argument passed into a inline function
  145. * it is no longer constant according to gcc..
  146. */
  147. #undef ntohl
  148. #undef ntohs
  149. #undef htonl
  150. #undef htons
  151. /*
  152. * Do the prototypes. Somebody might want to take the
  153. * address or some such sick thing..
  154. */
  155. #if defined(PLATFORM_LINUX) || (defined(__GLIBC__) && __GLIBC__ >= 2)
  156. extern __u32 ntohl(__u32);
  157. extern __u32 htonl(__u32);
  158. #else /* defined(PLATFORM_LINUX) || (defined (__GLIBC__) && __GLIBC__ >= 2) */
  159. #ifndef PLATFORM_FREEBSD
  160. extern unsigned long int ntohl(unsigned long int);
  161. extern unsigned long int htonl(unsigned long int);
  162. #endif
  163. #endif
  164. #ifndef PLATFORM_FREEBSD
  165. extern unsigned short int ntohs(unsigned short int);
  166. extern unsigned short int htons(unsigned short int);
  167. #endif
  168. #if defined(__GNUC__) && (__GNUC__ >= 2) && defined(__OPTIMIZE__) || defined(PLATFORM_MPIXEL)
  169. #define ___htonl(x) __cpu_to_be32(x)
  170. #define ___htons(x) __cpu_to_be16(x)
  171. #define ___ntohl(x) __be32_to_cpu(x)
  172. #define ___ntohs(x) __be16_to_cpu(x)
  173. #if defined(PLATFORM_LINUX) || (defined(__GLIBC__) && __GLIBC__ >= 2)
  174. #define htonl(x) ___htonl(x)
  175. #define ntohl(x) ___ntohl(x)
  176. #else
  177. #define htonl(x) ((unsigned long)___htonl(x))
  178. #define ntohl(x) ((unsigned long)___ntohl(x))
  179. #endif
  180. #define htons(x) ___htons(x)
  181. #define ntohs(x) ___ntohs(x)
  182. #endif /* OPTIMIZE */
  183. #if defined(PLATFORM_WINDOWS)
  184. #define htonl(x) __cpu_to_be32(x)
  185. #define ntohl(x) __be32_to_cpu(x)
  186. #define htons(x) __cpu_to_be16(x)
  187. #define ntohs(x) __be16_to_cpu(x)
  188. #endif
  189. #endif /* _LINUX_BYTEORDER_GENERIC_H */