From fd0837f7e6524483dd773aed785d1cf8b5ab50ef Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Tue, 1 Aug 2023 21:25:10 +0200 Subject: [PATCH] can-calc-bit-timing: move kernel compatibility stuff into own header --- GNUmakefile.am | 3 +- calc-bit-timing/can-calc-bit-timing.c | 152 +----------------------- calc-bit-timing/compat.h | 163 ++++++++++++++++++++++++++ 3 files changed, 166 insertions(+), 152 deletions(-) create mode 100644 calc-bit-timing/compat.h diff --git a/GNUmakefile.am b/GNUmakefile.am index f1b4189..4c595ab 100644 --- a/GNUmakefile.am +++ b/GNUmakefile.am @@ -42,7 +42,8 @@ libj1939_la_SOURCES = \ libj1939.c can_calc_bit_timing_SOURCES = \ - calc-bit-timing/can-calc-bit-timing.c + calc-bit-timing/can-calc-bit-timing.c \ + calc-bit-timing/compat.h EXTRA_DIST += \ calc-bit-timing/can-calc-bit-timing-v2_6_31.c \ diff --git a/calc-bit-timing/can-calc-bit-timing.c b/calc-bit-timing/can-calc-bit-timing.c index 5e64801..1b6f932 100644 --- a/calc-bit-timing/can-calc-bit-timing.c +++ b/calc-bit-timing/can-calc-bit-timing.c @@ -26,8 +26,7 @@ #include #include -#include -#include +#include "compat.h" enum { OPT_TQ = UCHAR_MAX + 1, @@ -41,144 +40,6 @@ enum { OPT_ALG, }; -/* imported from kernel */ - -/* define in-kernel-types */ -typedef __u64 u64; -typedef __u32 u32; - -#define NSEC_PER_SEC 1000000000L - -/** - * abs - return absolute value of an argument - * @x: the value. If it is unsigned type, it is converted to signed type first. - * char is treated as if it was signed (regardless of whether it really is) - * but the macro's return type is preserved as char. - * - * Return: an absolute value of x. - */ -#define abs(x) __abs_choose_expr(x, long long, \ - __abs_choose_expr(x, long, \ - __abs_choose_expr(x, int, \ - __abs_choose_expr(x, short, \ - __abs_choose_expr(x, char, \ - __builtin_choose_expr( \ - __builtin_types_compatible_p(typeof(x), char), \ - (char)({ signed char __x = (x); __x < 0 ? -__x:__x; }), \ - ((void)0))))))) - -#define __abs_choose_expr(x, type, other) __builtin_choose_expr( \ - __builtin_types_compatible_p(typeof(x), signed type) || \ - __builtin_types_compatible_p(typeof(x), unsigned type), \ - ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other) - -/* - * min()/max()/clamp() macros that also do - * strict type-checking.. See the - * "unnecessary" pointer comparison. - */ -#define min(x, y) ({ \ - typeof(x) _min1 = (x); \ - typeof(y) _min2 = (y); \ - (void) (&_min1 == &_min2); \ - _min1 < _min2 ? _min1 : _min2; }) - -#define max(x, y) ({ \ - typeof(x) _max1 = (x); \ - typeof(y) _max2 = (y); \ - (void) (&_max1 == &_max2); \ - _max1 > _max2 ? _max1 : _max2; }) - -/** - * clamp - return a value clamped to a given range with strict typechecking - * @val: current value - * @lo: lowest allowable value - * @hi: highest allowable value - * - * This macro does strict typechecking of lo/hi to make sure they are of the - * same type as val. See the unnecessary pointer comparisons. - */ -#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi) - -#define do_div(n, base) ({ \ - uint32_t __base = (base); \ - uint32_t __rem; \ - __rem = ((uint64_t)(n)) % __base; \ - (n) = ((uint64_t)(n)) / __base; \ - __rem; \ -}) - - -/** - * DIV_U64_ROUND_CLOSEST - unsigned 64bit divide with 32bit divisor rounded to nearest integer - * @dividend: unsigned 64bit dividend - * @divisor: unsigned 32bit divisor - * - * Divide unsigned 64bit dividend by unsigned 32bit divisor - * and round to closest integer. - * - * Return: dividend / divisor rounded to nearest integer - */ -#define DIV_U64_ROUND_CLOSEST(dividend, divisor) \ - ({ u32 _tmp = (divisor); div_u64((u64)(dividend) + _tmp / 2, _tmp); }) - -/** - * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder - * @dividend: unsigned 64bit dividend - * @divisor: unsigned 32bit divisor - * @remainder: pointer to unsigned 32bit remainder - * - * Return: sets ``*remainder``, then returns dividend / divisor - * - * This is commonly provided by 32bit archs to provide an optimized 64bit - * divide. - */ -static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder) -{ - *remainder = dividend % divisor; - return dividend / divisor; -} - -static inline u64 div_u64(u64 dividend, u32 divisor) -{ - u32 remainder; - return div_u64_rem(dividend, divisor, &remainder); -} - -static inline u64 mul_u32_u32(u32 a, u32 b) -{ - return (u64)a * b; -} - -/* */ - -#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) - -/* we don't want to see these prints */ -#define netdev_err(dev, format, arg...) do { } while (0) -#define netdev_warn(dev, format, arg...) do { } while (0) -#define NL_SET_ERR_MSG_FMT(dev, format, arg...) do { } while (0) - -struct calc_ref_clk { - __u32 clk; /* CAN system clock frequency in Hz */ - const char *name; -}; - -/* - * minimal structs, just enough to be source level compatible - */ -struct can_priv { - struct can_clock clock; -}; - -struct net_device { - struct can_priv priv; -}; - -struct netlink_ext_ack { - u32 __dummy; -}; - struct calc_bittiming_const { const struct can_bittiming_const bittiming_const; const struct can_bittiming_const data_bittiming_const; @@ -231,11 +92,6 @@ struct calc_data { bool fd_mode; }; -static inline void *netdev_priv(const struct net_device *dev) -{ - return (void *)&dev->priv; -} - static void print_usage(char *cmd) { printf("%s - calculate CAN bit timing parameters.\n", cmd); @@ -1236,12 +1092,6 @@ static const unsigned int common_data_bitrates[] = { 0 }; -#define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */ -#define CAN_CALC_SYNC_SEG 1 -#define CAN_SYNC_SEG 1 -#define CAN_KBPS 1000 -#define KILO 1000UL - #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wsign-compare" diff --git a/calc-bit-timing/compat.h b/calc-bit-timing/compat.h new file mode 100644 index 0000000..2186ac9 --- /dev/null +++ b/calc-bit-timing/compat.h @@ -0,0 +1,163 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _COMPAT_H +#define _COMPAT_H + +#include +#include +#include +#include + +#include +#include + +/* imported from kernel */ + +/* define in-kernel-types */ +typedef __u64 u64; +typedef __u32 u32; + +#define NSEC_PER_SEC 1000000000L + +#define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */ +#define CAN_CALC_SYNC_SEG 1 +#define CAN_SYNC_SEG 1 +#define CAN_KBPS 1000 +#define KILO 1000UL + +/** + * abs - return absolute value of an argument + * @x: the value. If it is unsigned type, it is converted to signed type first. + * char is treated as if it was signed (regardless of whether it really is) + * but the macro's return type is preserved as char. + * + * Return: an absolute value of x. + */ +#define abs(x) __abs_choose_expr(x, long long, \ + __abs_choose_expr(x, long, \ + __abs_choose_expr(x, int, \ + __abs_choose_expr(x, short, \ + __abs_choose_expr(x, char, \ + __builtin_choose_expr( \ + __builtin_types_compatible_p(typeof(x), char), \ + (char)({ signed char __x = (x); __x < 0 ? -__x:__x; }), \ + ((void)0))))))) + +#define __abs_choose_expr(x, type, other) __builtin_choose_expr( \ + __builtin_types_compatible_p(typeof(x), signed type) || \ + __builtin_types_compatible_p(typeof(x), unsigned type), \ + ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other) + +/* + * min()/max()/clamp() macros that also do + * strict type-checking.. See the + * "unnecessary" pointer comparison. + */ +#define min(x, y) ({ \ + typeof(x) _min1 = (x); \ + typeof(y) _min2 = (y); \ + (void) (&_min1 == &_min2); \ + _min1 < _min2 ? _min1 : _min2; }) + +#define max(x, y) ({ \ + typeof(x) _max1 = (x); \ + typeof(y) _max2 = (y); \ + (void) (&_max1 == &_max2); \ + _max1 > _max2 ? _max1 : _max2; }) + +/** + * clamp - return a value clamped to a given range with strict typechecking + * @val: current value + * @lo: lowest allowable value + * @hi: highest allowable value + * + * This macro does strict typechecking of lo/hi to make sure they are of the + * same type as val. See the unnecessary pointer comparisons. + */ +#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi) + +#define do_div(n, base) ({ \ + uint32_t __base = (base); \ + uint32_t __rem; \ + __rem = ((uint64_t)(n)) % __base; \ + (n) = ((uint64_t)(n)) / __base; \ + __rem; \ +}) + + +/** + * DIV_U64_ROUND_CLOSEST - unsigned 64bit divide with 32bit divisor rounded to nearest integer + * @dividend: unsigned 64bit dividend + * @divisor: unsigned 32bit divisor + * + * Divide unsigned 64bit dividend by unsigned 32bit divisor + * and round to closest integer. + * + * Return: dividend / divisor rounded to nearest integer + */ +#define DIV_U64_ROUND_CLOSEST(dividend, divisor) \ + ({ u32 _tmp = (divisor); div_u64((u64)(dividend) + _tmp / 2, _tmp); }) + +/** + * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder + * @dividend: unsigned 64bit dividend + * @divisor: unsigned 32bit divisor + * @remainder: pointer to unsigned 32bit remainder + * + * Return: sets ``*remainder``, then returns dividend / divisor + * + * This is commonly provided by 32bit archs to provide an optimized 64bit + * divide. + */ +static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder) +{ + *remainder = dividend % divisor; + return dividend / divisor; +} + +static inline u64 div_u64(u64 dividend, u32 divisor) +{ + u32 remainder; + return div_u64_rem(dividend, divisor, &remainder); +} + +static inline u64 mul_u32_u32(u32 a, u32 b) +{ + return (u64)a * b; +} + +/* */ + +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) + +/* we don't want to see these prints */ +#define netdev_err(dev, format, arg...) do { } while (0) +#define netdev_warn(dev, format, arg...) do { } while (0) +#define NL_SET_ERR_MSG_FMT(dev, format, arg...) do { } while (0) + +struct calc_ref_clk { + __u32 clk; /* CAN system clock frequency in Hz */ + const char *name; +}; + +/* + * minimal structs, just enough to be source level compatible + */ +struct can_priv { + struct can_clock clock; +}; + +struct net_device { + struct can_priv priv; +}; + +struct netlink_ext_ack { + u32 __dummy; +}; + +static inline void *netdev_priv(const struct net_device *dev) +{ + return (void *)&dev->priv; +} + +#endif /*_COMPAT_H */