3ddeb13c29a2fcfa3be8ecf508e1701a14c61122
1 #ifndef __BACKPORT_KERNEL_H
2 #define __BACKPORT_KERNEL_H
3 #include_next <linux/kernel.h>
4 #include <linux/version.h>
6 * some older kernels don't have this and thus don't
7 * include it from kernel.h like new kernels
9 #include <linux/printk.h>
14 * From a3860c1c5dd1137db23d7786d284939c5761d517 Mon Sep 17 00:00:00 2001
15 * From: Xi Wang <xi.wang@gmail.com>
16 * Date: Thu, 31 May 2012 16:26:04 -0700
17 * Subject: [PATCH] introduce SIZE_MAX
20 #define SIZE_MAX (~(size_t)0)
25 * commit 36a26c69b4c70396ef569c3452690fba0c1dec08
26 * Author: Nicholas Bellinger <nab@linux-iscsi.org>
27 * Date: Tue Jul 26 00:35:26 2011 -0700
29 * kernel.h: Add DIV_ROUND_UP_ULL and DIV_ROUND_UP_SECTOR_T macro usage
31 #ifndef DIV_ROUND_UP_ULL
32 #define DIV_ROUND_UP_ULL(ll,d) \
33 ({ unsigned long long _tmp = (ll)+(d)-1; do_div(_tmp, d); _tmp; })
37 #define USHRT_MAX ((u16)(~0U))
41 #define SHRT_MAX ((s16)(USHRT_MAX>>1))
45 #define SHRT_MIN ((s16)(-SHRT_MAX - 1))
49 #define U8_MAX ((u8)~0U)
53 #define S8_MAX ((s8)(U8_MAX>>1))
57 #define S8_MIN ((s8)(-S8_MAX - 1))
61 #define U16_MAX ((u16)~0U)
65 #define S16_MAX ((s16)(U16_MAX>>1))
69 #define S16_MIN ((s16)(-S16_MAX - 1))
73 #define U32_MAX ((u32)~0U)
77 #define S32_MAX ((s32)(U32_MAX>>1))
81 #define S32_MIN ((s32)(-S32_MAX - 1))
85 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
86 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
87 #define round_down(x, y) ((x) & ~__round_mask(x, y))
90 #ifndef DIV_ROUND_CLOSEST
91 #define DIV_ROUND_CLOSEST(x, divisor)( \
94 typeof(divisor) __d = divisor; \
95 (((typeof(x))-1) > 0 || \
96 ((typeof(divisor))-1) > 0 || (__x) > 0) ? \
97 (((__x) + ((__d) / 2)) / (__d)) : \
98 (((__x) - ((__d) / 2)) / (__d)); \
103 #ifndef DIV_ROUND_CLOSEST_ULL
104 #define DIV_ROUND_CLOSEST_ULL(x, divisor)( \
106 typeof(divisor) __d = divisor; \
107 unsigned long long _tmp = (x) + (__d) / 2; \
116 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
119 #ifndef lower_32_bits
120 #define lower_32_bits(n) ((u32)(n))
124 #define clamp(val, min, max) ({ \
125 typeof(val) __val = (val); \
126 typeof(min) __min = (min); \
127 typeof(max) __max = (max); \
128 (void) (&__val == &__min); \
129 (void) (&__val == &__max); \
130 __val = __val < __min ? __min: __val; \
131 __val > __max ? __max: __val; })
135 #define clamp_t(type, val, min, max) ({ \
136 type __val = (val); \
137 type __min = (min); \
138 type __max = (max); \
139 __val = __val < __min ? __min: __val; \
140 __val > __max ? __max: __val; })
144 #define clamp_val(val, min, max) ({ \
145 typeof(val) __val = (val); \
146 typeof(val) __min = (min); \
147 typeof(val) __max = (max); \
148 __val = __val < __min ? __min: __val; \
149 __val > __max ? __max: __val; })
153 #define rounddown(x, y) ( \
155 typeof(x) __x = (x); \
159 #endif /* rounddown */
161 #if LINUX_VERSION_IS_LESS(3,2,0)
162 #define hex_byte_pack pack_hex_byte
164 /* kernels before 3.2 didn't have error checking for the function */
165 #define hex2bin LINUX_BACKPORT(hex2bin)
166 int __must_check
hex2bin(u8
*dst
, const char *src
, size_t count
);
169 #if LINUX_VERSION_IS_LESS(3,18,0)
171 #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
174 #if LINUX_VERSION_IS_LESS(4,6,0)
175 #define kstrtobool LINUX_BACKPORT(kstrtobool)
176 int __must_check
kstrtobool(const char *s
, bool *res
);
177 #define kstrtobool_from_user LINUX_BACKPORT(kstrtobool_from_user)
178 int __must_check
kstrtobool_from_user(const char __user
*s
, size_t count
, bool *res
);
181 #if LINUX_VERSION_IS_LESS(4,5,0)
185 * abs - return absolute value of an argument
186 * @x: the value. If it is unsigned type, it is converted to signed type first.
187 * char is treated as if it was signed (regardless of whether it really is)
188 * but the macro's return type is preserved as char.
190 * Return: an absolute value of x.
192 #define abs(x) __abs_choose_expr(x, long long, \
193 __abs_choose_expr(x, long, \
194 __abs_choose_expr(x, int, \
195 __abs_choose_expr(x, short, \
196 __abs_choose_expr(x, char, \
197 __builtin_choose_expr( \
198 __builtin_types_compatible_p(typeof(x), char), \
199 (char)({ signed char __x = (x); __x<0?-__x:__x; }), \
202 #define __abs_choose_expr(x, type, other) __builtin_choose_expr( \
203 __builtin_types_compatible_p(typeof(x), signed type) || \
204 __builtin_types_compatible_p(typeof(x), unsigned type), \
205 ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
209 #if LINUX_VERSION_IS_LESS(3,14,0)
210 static inline u32
reciprocal_scale(u32 val
, u32 ep_ro
)
212 return (u32
)(((u64
) val
* ep_ro
) >> 32);
214 #endif /* LINUX_VERSION_IS_LESS(3,14,0) */
216 #if LINUX_VERSION_IS_LESS(3,18,0)
217 #define bin2hex LINUX_BACKPORT(bin2hex)
218 extern char *bin2hex(char *dst
, const void *src
, size_t count
);
221 #endif /* __BACKPORT_KERNEL_H */
224 * We have to do this outside the include guard, because
225 * out own header (linux/export.h) has to include kernel.h
226 * indirectly (through module.h) and then undef's pr_fmt.
227 * Then, when the real kernel.h gets included again, it's
228 * not defined and we get problems ...
231 #define pr_fmt(msg) msg