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