developer | 3abe1ad | 2022-01-24 11:13:32 +0800 | [diff] [blame] | 1 | /* Copyright (C) 2021-2022 Mediatek Inc. */ |
| 2 | #ifndef __ATENL_UTIL_H |
| 3 | #define __ATENL_UTIL_H |
| 4 | |
| 5 | #include <linux/const.h> |
| 6 | #include <linux/if_arp.h> |
| 7 | #include <linux/if_ether.h> |
| 8 | #include <linux/if_packet.h> |
| 9 | #include <stdint.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | typedef uint8_t u8; |
| 13 | typedef uint16_t u16; |
| 14 | typedef uint32_t u32; |
| 15 | typedef uint64_t u64; |
| 16 | typedef int8_t s8; |
| 17 | typedef int16_t s16; |
| 18 | typedef int32_t s32; |
| 19 | typedef int64_t s64, ktime_t; |
| 20 | |
| 21 | #ifndef __WORDSIZE |
| 22 | #define __WORDSIZE (__SIZEOF_LONG__ * 8) |
| 23 | #endif |
| 24 | |
| 25 | #ifndef BITS_PER_LONG |
| 26 | #define BITS_PER_LONG __WORDSIZE |
| 27 | #endif |
| 28 | |
| 29 | #define UL(x) (_UL(x)) |
| 30 | #define ULL(x) (_ULL(x)) |
| 31 | |
| 32 | #define BIT(nr) (1UL << (nr)) |
| 33 | |
| 34 | #define GENMASK_INPUT_CHECK(h, l) 0 |
| 35 | #define __GENMASK(h, l) \ |
| 36 | (((~UL(0)) - (UL(1) << (l)) + 1) & \ |
| 37 | (~UL(0) >> (BITS_PER_LONG - 1 - (h)))) |
| 38 | #define GENMASK(h, l) \ |
| 39 | (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l)) |
| 40 | |
| 41 | #define __bf_shf(x) (__builtin_ffsll(x) - 1) |
| 42 | #define FIELD_GET(_mask, _reg) \ |
| 43 | ({ \ |
| 44 | (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \ |
| 45 | }) |
| 46 | |
| 47 | #ifndef ARRAY_SIZE |
| 48 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 49 | #endif |
| 50 | |
| 51 | #ifndef DIV_ROUND_UP |
| 52 | #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) |
| 53 | #endif |
| 54 | |
| 55 | #define PIPE_READ 0 |
| 56 | #define PIPE_WRITE 1 |
| 57 | |
| 58 | #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] |
| 59 | #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x" |
| 60 | |
| 61 | static inline bool ether_addr_equal(const u8 *addr1, const u8 *addr2) |
| 62 | { |
| 63 | const u16 *a = (const u16 *)addr1; |
| 64 | const u16 *b = (const u16 *)addr2; |
| 65 | |
| 66 | return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2])) == 0; |
| 67 | } |
| 68 | |
| 69 | static inline bool is_broadcast_ether_addr(const u8 *addr) |
| 70 | { |
| 71 | return (*(const u16 *)(addr + 0) & |
| 72 | *(const u16 *)(addr + 2) & |
| 73 | *(const u16 *)(addr + 4)) == 0xffff; |
| 74 | } |
| 75 | |
| 76 | static inline bool is_multicast_ether_addr(const u8 *addr) |
| 77 | { |
| 78 | return 0x01 & addr[0]; |
| 79 | } |
| 80 | |
| 81 | static inline void eth_broadcast_addr(u8 *addr) |
| 82 | { |
| 83 | memset(addr, 0xff, ETH_ALEN); |
| 84 | } |
| 85 | |
| 86 | static inline void ether_addr_copy(u8 *dst, const u8 *src) |
| 87 | { |
| 88 | u16 *a = (u16 *)dst; |
| 89 | const u16 *b = (const u16 *)src; |
| 90 | |
| 91 | a[0] = b[0]; |
| 92 | a[1] = b[1]; |
| 93 | a[2] = b[2]; |
| 94 | } |
| 95 | |
| 96 | static inline int snprintf_error(size_t size, int res) |
| 97 | { |
| 98 | return res < 0 || (unsigned int) res >= size; |
| 99 | } |
| 100 | |
| 101 | #endif |