generic.h 7.2 KB

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