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