3ddeb13c29a2fcfa3be8ecf508e1701a14c61122
[openwrt/staging/blogic.git] / backport / backport-include / linux / kernel.h
1 #ifndef __BACKPORT_KERNEL_H
2 #define __BACKPORT_KERNEL_H
3 #include_next <linux/kernel.h>
4 #include <linux/version.h>
5 /*
6 * some older kernels don't have this and thus don't
7 * include it from kernel.h like new kernels
8 */
9 #include <linux/printk.h>
10
11 /*
12 * This backports:
13 *
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
18 */
19 #ifndef SIZE_MAX
20 #define SIZE_MAX (~(size_t)0)
21 #endif
22
23 /* This backports:
24 *
25 * commit 36a26c69b4c70396ef569c3452690fba0c1dec08
26 * Author: Nicholas Bellinger <nab@linux-iscsi.org>
27 * Date: Tue Jul 26 00:35:26 2011 -0700
28 *
29 * kernel.h: Add DIV_ROUND_UP_ULL and DIV_ROUND_UP_SECTOR_T macro usage
30 */
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; })
34 #endif
35
36 #ifndef USHRT_MAX
37 #define USHRT_MAX ((u16)(~0U))
38 #endif
39
40 #ifndef SHRT_MAX
41 #define SHRT_MAX ((s16)(USHRT_MAX>>1))
42 #endif
43
44 #ifndef SHRT_MIN
45 #define SHRT_MIN ((s16)(-SHRT_MAX - 1))
46 #endif
47
48 #ifndef U8_MAX
49 #define U8_MAX ((u8)~0U)
50 #endif
51
52 #ifndef S8_MAX
53 #define S8_MAX ((s8)(U8_MAX>>1))
54 #endif
55
56 #ifndef S8_MIN
57 #define S8_MIN ((s8)(-S8_MAX - 1))
58 #endif
59
60 #ifndef U16_MAX
61 #define U16_MAX ((u16)~0U)
62 #endif
63
64 #ifndef S16_MAX
65 #define S16_MAX ((s16)(U16_MAX>>1))
66 #endif
67
68 #ifndef S16_MIN
69 #define S16_MIN ((s16)(-S16_MAX - 1))
70 #endif
71
72 #ifndef U32_MAX
73 #define U32_MAX ((u32)~0U)
74 #endif
75
76 #ifndef S32_MAX
77 #define S32_MAX ((s32)(U32_MAX>>1))
78 #endif
79
80 #ifndef S32_MIN
81 #define S32_MIN ((s32)(-S32_MAX - 1))
82 #endif
83
84 #ifndef __round_mask
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))
88 #endif
89
90 #ifndef DIV_ROUND_CLOSEST
91 #define DIV_ROUND_CLOSEST(x, divisor)( \
92 { \
93 typeof(x) __x = x; \
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)); \
99 } \
100 )
101 #endif
102
103 #ifndef DIV_ROUND_CLOSEST_ULL
104 #define DIV_ROUND_CLOSEST_ULL(x, divisor)( \
105 { \
106 typeof(divisor) __d = divisor; \
107 unsigned long long _tmp = (x) + (__d) / 2; \
108 do_div(_tmp, __d); \
109 _tmp; \
110 } \
111 )
112 #endif
113
114 #ifndef swap
115 #define swap(a, b) \
116 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
117 #endif
118
119 #ifndef lower_32_bits
120 #define lower_32_bits(n) ((u32)(n))
121 #endif
122
123 #ifndef clamp
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; })
132 #endif
133
134 #ifndef clamp_t
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; })
141 #endif
142
143 #ifndef clamp_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; })
150 #endif
151
152 #ifndef rounddown
153 #define rounddown(x, y) ( \
154 { \
155 typeof(x) __x = (x); \
156 __x - (__x % (y)); \
157 } \
158 )
159 #endif /* rounddown */
160
161 #if LINUX_VERSION_IS_LESS(3,2,0)
162 #define hex_byte_pack pack_hex_byte
163
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);
167 #endif /* < 3.2 */
168
169 #if LINUX_VERSION_IS_LESS(3,18,0)
170 #undef clamp
171 #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
172 #endif /* < 3.18 */
173
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);
179 #endif
180
181 #if LINUX_VERSION_IS_LESS(4,5,0)
182
183 #undef abs
184 /**
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.
189 *
190 * Return: an absolute value of x.
191 */
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; }), \
200 ((void)0)))))))
201
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)
206
207 #endif
208
209 #if LINUX_VERSION_IS_LESS(3,14,0)
210 static inline u32 reciprocal_scale(u32 val, u32 ep_ro)
211 {
212 return (u32)(((u64) val * ep_ro) >> 32);
213 }
214 #endif /* LINUX_VERSION_IS_LESS(3,14,0) */
215
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);
219 #endif
220
221 #endif /* __BACKPORT_KERNEL_H */
222
223 /*
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 ...
229 */
230 #ifndef pr_fmt
231 #define pr_fmt(msg) msg
232 #endif