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