upgrade wireless-tools and iproute2
[15.05/openwrt.git] / package / linux / kernel-source / include / bcmendian.h
1 /*
2  * local version of endian.h - byte order defines
3  *
4  * Copyright 2004, Broadcom Corporation   
5  * All Rights Reserved.   
6  *    
7  * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY   
8  * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM   
9  * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS   
10  * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.   
11  *
12  *  $Id$
13 */
14
15 #ifndef _BCMENDIAN_H_
16 #define _BCMENDIAN_H_
17
18 #include <typedefs.h>
19
20 /* Byte swap a 16 bit value */
21 #define BCMSWAP16(val) \
22         ((uint16)( \
23                 (((uint16)(val) & (uint16)0x00ffU) << 8) | \
24                 (((uint16)(val) & (uint16)0xff00U) >> 8) ))
25         
26 /* Byte swap a 32 bit value */
27 #define BCMSWAP32(val) \
28         ((uint32)( \
29                 (((uint32)(val) & (uint32)0x000000ffUL) << 24) | \
30                 (((uint32)(val) & (uint32)0x0000ff00UL) <<  8) | \
31                 (((uint32)(val) & (uint32)0x00ff0000UL) >>  8) | \
32                 (((uint32)(val) & (uint32)0xff000000UL) >> 24) ))
33
34 static INLINE uint16
35 bcmswap16(uint16 val)
36 {
37         return BCMSWAP16(val);
38 }
39
40 static INLINE uint32
41 bcmswap32(uint32 val)
42 {
43         return BCMSWAP32(val);
44 }
45
46 /* buf  - start of buffer of shorts to swap */
47 /* len  - byte length of buffer */
48 static INLINE void
49 bcmswap16_buf(uint16 *buf, uint len)
50 {
51         len = len/2;
52
53         while(len--){
54                 *buf = bcmswap16(*buf);
55                 buf++;
56         }
57 }
58
59 #ifndef hton16
60 #ifndef IL_BIGENDIAN
61 #define HTON16(i) BCMSWAP16(i)
62 #define hton16(i) bcmswap16(i)
63 #define hton32(i) bcmswap32(i)
64 #define ntoh16(i) bcmswap16(i)
65 #define ntoh32(i) bcmswap32(i)
66 #define ltoh16(i) (i)
67 #define ltoh32(i) (i)
68 #define htol16(i) (i)
69 #define htol32(i) (i)
70 #else
71 #define HTON16(i) (i)
72 #define hton16(i) (i)
73 #define hton32(i) (i)
74 #define ntoh16(i) (i)
75 #define ntoh32(i) (i)
76 #define ltoh16(i) bcmswap16(i)
77 #define ltoh32(i) bcmswap32(i)
78 #define htol16(i) bcmswap16(i)
79 #define htol32(i) bcmswap32(i)
80 #endif
81 #endif
82
83 #ifndef IL_BIGENDIAN
84 #define ltoh16_buf(buf, i)
85 #define htol16_buf(buf, i)
86 #else
87 #define ltoh16_buf(buf, i) bcmswap16_buf((uint16*)buf, i)
88 #define htol16_buf(buf, i) bcmswap16_buf((uint16*)buf, i)
89 #endif
90
91 /*
92 * load 16-bit value from unaligned little endian byte array.
93 */
94 static INLINE uint16
95 ltoh16_ua(uint8 *bytes)
96 {
97         return (bytes[1]<<8)+bytes[0];
98 }
99
100 /*
101 * load 32-bit value from unaligned little endian byte array.
102 */
103 static INLINE uint32
104 ltoh32_ua(uint8 *bytes)
105 {
106         return (bytes[3]<<24)+(bytes[2]<<16)+(bytes[1]<<8)+bytes[0];
107 }
108
109 /*
110 * load 16-bit value from unaligned big(network) endian byte array.
111 */
112 static INLINE uint16
113 ntoh16_ua(uint8 *bytes)
114 {
115         return (bytes[0]<<8)+bytes[1];
116 }
117
118 /*
119 * load 32-bit value from unaligned big(network) endian byte array.
120 */
121 static INLINE uint32
122 ntoh32_ua(uint8 *bytes)
123 {
124         return (bytes[0]<<24)+(bytes[1]<<16)+(bytes[2]<<8)+bytes[3];
125 }
126
127 #endif /* _BCMENDIAN_H_ */