| .. | .. |
|---|
| 1 | | -/* SPDX-License-Identifier: GPL-2.0 */ |
|---|
| 2 | 1 | /* |
|---|
| 3 | 2 | * Byte order utilities |
|---|
| 4 | 3 | * |
|---|
| 5 | | - * Copyright (C) 1999-2019, Broadcom Corporation |
|---|
| 6 | | - * |
|---|
| 4 | + * Portions of this code are copyright (c) 2022 Cypress Semiconductor Corporation |
|---|
| 5 | + * |
|---|
| 6 | + * Copyright (C) 1999-2017, Broadcom Corporation |
|---|
| 7 | + * |
|---|
| 7 | 8 | * Unless you and Broadcom execute a separate written software license |
|---|
| 8 | 9 | * agreement governing use of this software, this software is licensed to you |
|---|
| 9 | 10 | * under the terms of the GNU General Public License version 2 (the "GPL"), |
|---|
| 10 | 11 | * available at http://www.broadcom.com/licenses/GPLv2.php, with the |
|---|
| 11 | 12 | * following added to such license: |
|---|
| 12 | | - * |
|---|
| 13 | + * |
|---|
| 13 | 14 | * As a special exception, the copyright holders of this software give you |
|---|
| 14 | 15 | * permission to link this software with independent modules, and to copy and |
|---|
| 15 | 16 | * distribute the resulting executable under terms of your choice, provided that |
|---|
| .. | .. |
|---|
| 17 | 18 | * the license of that module. An independent module is a module which is not |
|---|
| 18 | 19 | * derived from this software. The special exception does not apply to any |
|---|
| 19 | 20 | * modifications of the software. |
|---|
| 20 | | - * |
|---|
| 21 | + * |
|---|
| 21 | 22 | * Notwithstanding the above, under no circumstances may you combine this |
|---|
| 22 | 23 | * software in any way with any other Broadcom software provided under a license |
|---|
| 23 | 24 | * other than the GPL, without Broadcom's express prior written consent. |
|---|
| .. | .. |
|---|
| 25 | 26 | * |
|---|
| 26 | 27 | * <<Broadcom-WL-IPTag/Open:>> |
|---|
| 27 | 28 | * |
|---|
| 28 | | - * $Id: bcmendian.h 514727 2014-11-12 03:02:48Z $ |
|---|
| 29 | + * $Id: bcmendian.h 633810 2016-04-25 16:46:55Z $ |
|---|
| 29 | 30 | * |
|---|
| 30 | 31 | * This file by default provides proper behavior on little-endian architectures. |
|---|
| 31 | 32 | * On big-endian architectures, IL_BIGENDIAN should be defined. |
|---|
| .. | .. |
|---|
| 69 | 70 | ((uint64)((((uint64)(val) & 0x00000000ffffffffULL) << 32) | \ |
|---|
| 70 | 71 | (((uint64)(val) & 0xffffffff00000000ULL) >> 32))) |
|---|
| 71 | 72 | |
|---|
| 72 | | - |
|---|
| 73 | 73 | /* Byte swapping macros |
|---|
| 74 | 74 | * Host <=> Network (Big Endian) for 16- and 32-bit values |
|---|
| 75 | 75 | * Host <=> Little-Endian for 16- and 32-bit values |
|---|
| .. | .. |
|---|
| 97 | 97 | |
|---|
| 98 | 98 | #define ltoh16_buf(buf, i) |
|---|
| 99 | 99 | #define htol16_buf(buf, i) |
|---|
| 100 | +#define ltoh32_buf(buf, i) |
|---|
| 101 | +#define htol32_buf(buf, i) |
|---|
| 102 | +#define ltoh64_buf(buf, i) |
|---|
| 103 | +#define htol64_buf(buf, i) |
|---|
| 100 | 104 | |
|---|
| 101 | 105 | /* Unaligned loads and stores in host byte order */ |
|---|
| 102 | 106 | #define load32_ua(a) ltoh32_ua(a) |
|---|
| 103 | 107 | #define store32_ua(a, v) htol32_ua_store(v, a) |
|---|
| 104 | 108 | #define load16_ua(a) ltoh16_ua(a) |
|---|
| 105 | 109 | #define store16_ua(a, v) htol16_ua_store(v, a) |
|---|
| 110 | +#define load64_ua(a) ltoh64_ua(a) |
|---|
| 111 | +#define store64_ua(a, v) htol64_ua_store(v, a) |
|---|
| 106 | 112 | |
|---|
| 107 | | -#define _LTOH16_UA(cp) ((cp)[0] | ((cp)[1] << 8)) |
|---|
| 108 | | -#define _LTOH32_UA(cp) ((cp)[0] | ((cp)[1] << 8) | ((cp)[2] << 16) | ((cp)[3] << 24)) |
|---|
| 109 | | -#define _NTOH16_UA(cp) (((cp)[0] << 8) | (cp)[1]) |
|---|
| 110 | | -#define _NTOH32_UA(cp) (((cp)[0] << 24) | ((cp)[1] << 16) | ((cp)[2] << 8) | (cp)[3]) |
|---|
| 113 | +#define _LTOH16_UA(cp) (uint16)((cp)[0] | ((cp)[1] << 8)) |
|---|
| 114 | +#define _LTOH32_UA(cp) (uint32)((cp)[0] | ((cp)[1] << 8) | ((cp)[2] << 16) | ((cp)[3] << 24)) |
|---|
| 115 | +#define _NTOH16_UA(cp) (uint16)(((cp)[0] << 8) | (cp)[1]) |
|---|
| 116 | +#define _NTOH32_UA(cp) (uint32)(((cp)[0] << 24) | ((cp)[1] << 16) | ((cp)[2] << 8) | (cp)[3]) |
|---|
| 117 | + |
|---|
| 118 | +#define _LTOH64_UA(cp) ((uint64)(cp)[0] | ((uint64)(cp)[1] << 8) | \ |
|---|
| 119 | + ((uint64)(cp)[2] << 16) | ((uint64)(cp)[3] << 24) | \ |
|---|
| 120 | + ((uint64)(cp)[4] << 32) | ((uint64)(cp)[5] << 40) | \ |
|---|
| 121 | + ((uint64)(cp)[6] << 48) | ((uint64)(cp)[7] << 56)) |
|---|
| 122 | + |
|---|
| 123 | +#define _NTOH64_UA(cp) ((uint64)(cp)[7] | ((uint64)(cp)[6] << 8) | \ |
|---|
| 124 | + ((uint64)(cp)[5] << 16) | ((uint64)(cp)[4] << 24) | \ |
|---|
| 125 | + ((uint64)(cp)[3] << 32) | ((uint64)(cp)[2] << 40) | \ |
|---|
| 126 | + ((uint64)(cp)[1] << 48) | ((uint64)(cp)[0] << 56)) |
|---|
| 111 | 127 | |
|---|
| 112 | 128 | #define ltoh_ua(ptr) \ |
|---|
| 113 | 129 | (sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)(ptr) : \ |
|---|
| .. | .. |
|---|
| 156 | 172 | } \ |
|---|
| 157 | 173 | }) |
|---|
| 158 | 174 | |
|---|
| 175 | +#define bcmswap32_buf(buf, len) ({ \ |
|---|
| 176 | + uint32 *_buf = (uint32 *)(buf); \ |
|---|
| 177 | + uint _wds = (len) / 4; \ |
|---|
| 178 | + while (_wds--) { \ |
|---|
| 179 | + *_buf = bcmswap32(*_buf); \ |
|---|
| 180 | + _buf++; \ |
|---|
| 181 | + } \ |
|---|
| 182 | +}) |
|---|
| 183 | + |
|---|
| 184 | +#define bcmswap64_buf(buf, len) ({ \ |
|---|
| 185 | + uint64 *_buf = (uint64 *)(buf); \ |
|---|
| 186 | + uint _wds = (len) / 8; \ |
|---|
| 187 | + while (_wds--) { \ |
|---|
| 188 | + *_buf = bcmswap64(*_buf); \ |
|---|
| 189 | + _buf++; \ |
|---|
| 190 | + } \ |
|---|
| 191 | +}) |
|---|
| 192 | + |
|---|
| 159 | 193 | #define htol16_ua_store(val, bytes) ({ \ |
|---|
| 160 | 194 | uint16 _val = (val); \ |
|---|
| 161 | 195 | uint8 *_bytes = (uint8 *)(bytes); \ |
|---|
| .. | .. |
|---|
| 170 | 204 | _bytes[1] = (_val >> 8) & 0xff; \ |
|---|
| 171 | 205 | _bytes[2] = (_val >> 16) & 0xff; \ |
|---|
| 172 | 206 | _bytes[3] = _val >> 24; \ |
|---|
| 207 | +}) |
|---|
| 208 | + |
|---|
| 209 | +#define htol64_ua_store(val, bytes) ({ \ |
|---|
| 210 | + uint64 _val = (val); \ |
|---|
| 211 | + uint8 *_bytes = (uint8 *)(bytes); \ |
|---|
| 212 | + int i; \ |
|---|
| 213 | + for (i = 0; i < (int)sizeof(_val); ++i) { \ |
|---|
| 214 | + *_bytes++ = _val & 0xff; \ |
|---|
| 215 | + _val >>= 8; \ |
|---|
| 216 | + } \ |
|---|
| 173 | 217 | }) |
|---|
| 174 | 218 | |
|---|
| 175 | 219 | #define hton16_ua_store(val, bytes) ({ \ |
|---|
| .. | .. |
|---|
| 198 | 242 | _LTOH32_UA(_bytes); \ |
|---|
| 199 | 243 | }) |
|---|
| 200 | 244 | |
|---|
| 245 | +#define ltoh64_ua(bytes) ({ \ |
|---|
| 246 | + const uint8 *_bytes = (const uint8 *)(bytes); \ |
|---|
| 247 | + _LTOH64_UA(_bytes); \ |
|---|
| 248 | +}) |
|---|
| 249 | + |
|---|
| 201 | 250 | #define ntoh16_ua(bytes) ({ \ |
|---|
| 202 | 251 | const uint8 *_bytes = (const uint8 *)(bytes); \ |
|---|
| 203 | 252 | _NTOH16_UA(_bytes); \ |
|---|
| .. | .. |
|---|
| 206 | 255 | #define ntoh32_ua(bytes) ({ \ |
|---|
| 207 | 256 | const uint8 *_bytes = (const uint8 *)(bytes); \ |
|---|
| 208 | 257 | _NTOH32_UA(_bytes); \ |
|---|
| 258 | +}) |
|---|
| 259 | + |
|---|
| 260 | +#define ntoh64_ua(bytes) ({ \ |
|---|
| 261 | + const uint8 *_bytes = (const uint8 *)(bytes); \ |
|---|
| 262 | + _NTOH64_UA(_bytes); \ |
|---|
| 209 | 263 | }) |
|---|
| 210 | 264 | |
|---|
| 211 | 265 | #else /* !__GNUC__ */ |
|---|
| .. | .. |
|---|
| 272 | 326 | } |
|---|
| 273 | 327 | |
|---|
| 274 | 328 | /* |
|---|
| 329 | + * Store 64-bit value to unaligned little-endian byte array. |
|---|
| 330 | + */ |
|---|
| 331 | +static INLINE void |
|---|
| 332 | +htol64_ua_store(uint64 val, uint8 *bytes) |
|---|
| 333 | +{ |
|---|
| 334 | + int i; |
|---|
| 335 | + for (i = 0; i < sizeof(val); ++i) { |
|---|
| 336 | + *bytes++ = (uint8)(val & 0xff); |
|---|
| 337 | + val >>= 8; |
|---|
| 338 | + } |
|---|
| 339 | +} |
|---|
| 340 | + |
|---|
| 341 | +/* |
|---|
| 275 | 342 | * Store 16-bit value to unaligned network-(big-)endian byte array. |
|---|
| 276 | 343 | */ |
|---|
| 277 | 344 | static INLINE void |
|---|
| .. | .. |
|---|
| 312 | 379 | } |
|---|
| 313 | 380 | |
|---|
| 314 | 381 | /* |
|---|
| 382 | + * Load 64-bit value from unaligned little-endian byte array. |
|---|
| 383 | + */ |
|---|
| 384 | +static INLINE uint64 |
|---|
| 385 | +ltoh64_ua(const void *bytes) |
|---|
| 386 | +{ |
|---|
| 387 | + return _LTOH64_UA((const uint8 *)bytes); |
|---|
| 388 | +} |
|---|
| 389 | + |
|---|
| 390 | +/* |
|---|
| 315 | 391 | * Load 16-bit value from unaligned big-(network-)endian byte array. |
|---|
| 316 | 392 | */ |
|---|
| 317 | 393 | static INLINE uint16 |
|---|
| .. | .. |
|---|
| 329 | 405 | return _NTOH32_UA((const uint8 *)bytes); |
|---|
| 330 | 406 | } |
|---|
| 331 | 407 | |
|---|
| 408 | +/* |
|---|
| 409 | + * Load 64-bit value from unaligned big-(network-)endian byte array. |
|---|
| 410 | + */ |
|---|
| 411 | +static INLINE uint64 |
|---|
| 412 | +ntoh64_ua(const void *bytes) |
|---|
| 413 | +{ |
|---|
| 414 | + return _NTOH64_UA((const uint8 *)bytes); |
|---|
| 415 | +} |
|---|
| 416 | + |
|---|
| 332 | 417 | #endif /* !__GNUC__ */ |
|---|
| 333 | 418 | #endif /* !_BCMENDIAN_H_ */ |
|---|