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