Rev 2967 | Rev 3039 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1408 | serge | 1 | #ifndef _LINUX_KERNEL_H |
2 | #define _LINUX_KERNEL_H |
||
3 | |||
4 | /* |
||
5 | * 'kernel.h' contains some often-used function prototypes etc |
||
6 | */ |
||
7 | |||
8 | #ifdef __KERNEL__ |
||
9 | |||
10 | #include |
||
11 | #include |
||
12 | #include |
||
13 | #include |
||
1970 | serge | 14 | #include |
1408 | serge | 15 | |
2967 | Serge | 16 | #include |
17 | |||
18 | #define __init |
||
19 | |||
1964 | serge | 20 | #define USHRT_MAX ((u16)(~0U)) |
21 | #define SHRT_MAX ((s16)(USHRT_MAX>>1)) |
||
22 | #define SHRT_MIN ((s16)(-SHRT_MAX - 1)) |
||
1408 | serge | 23 | #define INT_MAX ((int)(~0U>>1)) |
24 | #define INT_MIN (-INT_MAX - 1) |
||
25 | #define UINT_MAX (~0U) |
||
26 | #define LONG_MAX ((long)(~0UL>>1)) |
||
27 | #define LONG_MIN (-LONG_MAX - 1) |
||
28 | #define ULONG_MAX (~0UL) |
||
29 | #define LLONG_MAX ((long long)(~0ULL>>1)) |
||
30 | #define LLONG_MIN (-LLONG_MAX - 1) |
||
31 | #define ULLONG_MAX (~0ULL) |
||
3031 | serge | 32 | #define SIZE_MAX (~(size_t)0) |
1408 | serge | 33 | |
34 | #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) |
||
35 | #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) |
||
36 | #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a))) |
||
37 | #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) |
||
38 | |||
2967 | Serge | 39 | #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) |
40 | |||
41 | #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) |
||
42 | #define DIV_ROUND_CLOSEST(x, divisor)( \ |
||
43 | { \ |
||
44 | typeof(divisor) __divisor = divisor; \ |
||
45 | (((x) + ((__divisor) / 2)) / (__divisor)); \ |
||
46 | } \ |
||
47 | ) |
||
48 | |||
1408 | serge | 49 | /** |
50 | * upper_32_bits - return bits 32-63 of a number |
||
51 | * @n: the number we're accessing |
||
52 | * |
||
53 | * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress |
||
54 | * the "right shift count >= width of type" warning when that quantity is |
||
55 | * 32-bits. |
||
56 | */ |
||
57 | #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) |
||
58 | |||
59 | /** |
||
60 | * lower_32_bits - return bits 0-31 of a number |
||
61 | * @n: the number we're accessing |
||
62 | */ |
||
63 | #define lower_32_bits(n) ((u32)(n)) |
||
64 | |||
65 | #define KERN_EMERG "<0>" /* system is unusable */ |
||
66 | #define KERN_ALERT "<1>" /* action must be taken immediately */ |
||
67 | #define KERN_CRIT "<2>" /* critical conditions */ |
||
68 | #define KERN_ERR "<3>" /* error conditions */ |
||
69 | #define KERN_WARNING "<4>" /* warning conditions */ |
||
70 | #define KERN_NOTICE "<5>" /* normal but significant condition */ |
||
71 | #define KERN_INFO "<6>" /* informational */ |
||
72 | #define KERN_DEBUG "<7>" /* debug-level messages */ |
||
1970 | serge | 73 | extern const char hex_asc[]; |
74 | #define hex_asc_lo(x) hex_asc[((x) & 0x0f)] |
||
75 | #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4] |
||
1408 | serge | 76 | |
1970 | serge | 77 | static inline char *pack_hex_byte(char *buf, u8 byte) |
78 | { |
||
79 | *buf++ = hex_asc_hi(byte); |
||
80 | *buf++ = hex_asc_lo(byte); |
||
81 | return buf; |
||
82 | } |
||
83 | |||
84 | extern int hex_to_bin(char ch); |
||
85 | extern void hex2bin(u8 *dst, const char *src, size_t count); |
||
86 | |||
87 | |||
1408 | serge | 88 | //int printk(const char *fmt, ...); |
89 | |||
90 | #define printk(fmt, arg...) dbgprintf(fmt , ##arg) |
||
91 | |||
92 | |||
93 | /* |
||
94 | * min()/max()/clamp() macros that also do |
||
95 | * strict type-checking.. See the |
||
96 | * "unnecessary" pointer comparison. |
||
97 | */ |
||
98 | #define min(x, y) ({ \ |
||
99 | typeof(x) _min1 = (x); \ |
||
100 | typeof(y) _min2 = (y); \ |
||
101 | (void) (&_min1 == &_min2); \ |
||
102 | _min1 < _min2 ? _min1 : _min2; }) |
||
103 | |||
104 | #define max(x, y) ({ \ |
||
105 | typeof(x) _max1 = (x); \ |
||
106 | typeof(y) _max2 = (y); \ |
||
107 | (void) (&_max1 == &_max2); \ |
||
108 | _max1 > _max2 ? _max1 : _max2; }) |
||
109 | |||
1970 | serge | 110 | #define min3(x, y, z) ({ \ |
111 | typeof(x) _min1 = (x); \ |
||
112 | typeof(y) _min2 = (y); \ |
||
113 | typeof(z) _min3 = (z); \ |
||
114 | (void) (&_min1 == &_min2); \ |
||
115 | (void) (&_min1 == &_min3); \ |
||
116 | _min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \ |
||
117 | (_min2 < _min3 ? _min2 : _min3); }) |
||
118 | |||
119 | #define max3(x, y, z) ({ \ |
||
120 | typeof(x) _max1 = (x); \ |
||
121 | typeof(y) _max2 = (y); \ |
||
122 | typeof(z) _max3 = (z); \ |
||
123 | (void) (&_max1 == &_max2); \ |
||
124 | (void) (&_max1 == &_max3); \ |
||
125 | _max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \ |
||
126 | (_max2 > _max3 ? _max2 : _max3); }) |
||
127 | |||
128 | /** |
||
129 | * min_not_zero - return the minimum that is _not_ zero, unless both are zero |
||
130 | * @x: value1 |
||
131 | * @y: value2 |
||
132 | */ |
||
133 | #define min_not_zero(x, y) ({ \ |
||
134 | typeof(x) __x = (x); \ |
||
135 | typeof(y) __y = (y); \ |
||
136 | __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); }) |
||
137 | |||
138 | /** |
||
139 | * clamp - return a value clamped to a given range with strict typechecking |
||
140 | * @val: current value |
||
141 | * @min: minimum allowable value |
||
142 | * @max: maximum allowable value |
||
143 | * |
||
144 | * This macro does strict typechecking of min/max to make sure they are of the |
||
145 | * same type as val. See the unnecessary pointer comparisons. |
||
146 | */ |
||
147 | #define clamp(val, min, max) ({ \ |
||
148 | typeof(val) __val = (val); \ |
||
149 | typeof(min) __min = (min); \ |
||
150 | typeof(max) __max = (max); \ |
||
151 | (void) (&__val == &__min); \ |
||
152 | (void) (&__val == &__max); \ |
||
153 | __val = __val < __min ? __min: __val; \ |
||
154 | __val > __max ? __max: __val; }) |
||
155 | |||
1408 | serge | 156 | /* |
157 | * ..and if you can't take the strict |
||
158 | * types, you can specify one yourself. |
||
159 | * |
||
160 | * Or not use min/max/clamp at all, of course. |
||
161 | */ |
||
162 | #define min_t(type, x, y) ({ \ |
||
163 | type __min1 = (x); \ |
||
164 | type __min2 = (y); \ |
||
165 | __min1 < __min2 ? __min1: __min2; }) |
||
166 | |||
167 | #define max_t(type, x, y) ({ \ |
||
168 | type __max1 = (x); \ |
||
169 | type __max2 = (y); \ |
||
170 | __max1 > __max2 ? __max1: __max2; }) |
||
171 | |||
172 | /** |
||
173 | * container_of - cast a member of a structure out to the containing structure |
||
174 | * @ptr: the pointer to the member. |
||
175 | * @type: the type of the container struct this is embedded in. |
||
176 | * @member: the name of the member within the struct. |
||
177 | * |
||
178 | */ |
||
179 | #define container_of(ptr, type, member) ({ \ |
||
180 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
||
181 | (type *)( (char *)__mptr - offsetof(type,member) );}) |
||
182 | |||
183 | |||
184 | static inline void *kcalloc(size_t n, size_t size, uint32_t flags) |
||
185 | { |
||
186 | if (n != 0 && size > ULONG_MAX / n) |
||
187 | return NULL; |
||
188 | return kzalloc(n * size, 0); |
||
189 | } |
||
190 | |||
1968 | serge | 191 | |
1412 | serge | 192 | void free (void *ptr); |
193 | |||
1408 | serge | 194 | #endif /* __KERNEL__ */ |
195 | |||
196 | typedef unsigned long pgprotval_t; |
||
197 | |||
198 | typedef struct pgprot { pgprotval_t pgprot; } pgprot_t; |
||
199 | |||
200 | struct file {}; |
||
201 | struct vm_area_struct {}; |
||
202 | struct address_space {}; |
||
203 | |||
1430 | serge | 204 | struct device |
205 | { |
||
206 | struct device *parent; |
||
207 | void *driver_data; |
||
208 | }; |
||
209 | |||
210 | static inline void dev_set_drvdata(struct device *dev, void *data) |
||
211 | { |
||
212 | dev->driver_data = data; |
||
213 | } |
||
214 | |||
215 | static inline void *dev_get_drvdata(struct device *dev) |
||
216 | { |
||
217 | return dev->driver_data; |
||
218 | } |
||
219 | |||
1408 | serge | 220 | #define preempt_disable() do { } while (0) |
221 | #define preempt_enable_no_resched() do { } while (0) |
||
222 | #define preempt_enable() do { } while (0) |
||
223 | #define preempt_check_resched() do { } while (0) |
||
224 | |||
225 | #define preempt_disable_notrace() do { } while (0) |
||
226 | #define preempt_enable_no_resched_notrace() do { } while (0) |
||
227 | #define preempt_enable_notrace() do { } while (0) |
||
228 | |||
1964 | serge | 229 | #define in_dbg_master() (0) |
1408 | serge | 230 | |
2005 | serge | 231 | #define HZ 100 |
232 | |||
233 | #define time_after(a,b) \ |
||
234 | (typecheck(unsigned long, a) && \ |
||
235 | typecheck(unsigned long, b) && \ |
||
236 | ((long)(b) - (long)(a) < 0)) |
||
237 | |||
2967 | Serge | 238 | struct tvec_base; |
2005 | serge | 239 | |
2967 | Serge | 240 | struct timer_list { |
241 | struct list_head entry; |
||
242 | unsigned long expires; |
||
243 | |||
244 | void (*function)(unsigned long); |
||
245 | unsigned long data; |
||
246 | |||
247 | // struct tvec_base *base; |
||
248 | }; |
||
249 | |||
250 | struct timespec { |
||
251 | long tv_sec; /* seconds */ |
||
252 | long tv_nsec; /* nanoseconds */ |
||
253 | }; |
||
254 | |||
255 | |||
256 | #define build_mmio_read(name, size, type, reg, barrier) \ |
||
257 | static inline type name(const volatile void __iomem *addr) \ |
||
258 | { type ret; asm volatile("mov" size " %1,%0":reg (ret) \ |
||
259 | :"m" (*(volatile type __force *)addr) barrier); return ret; } |
||
260 | |||
261 | #define build_mmio_write(name, size, type, reg, barrier) \ |
||
262 | static inline void name(type val, volatile void __iomem *addr) \ |
||
263 | { asm volatile("mov" size " %0,%1": :reg (val), \ |
||
264 | "m" (*(volatile type __force *)addr) barrier); } |
||
265 | |||
266 | build_mmio_read(readb, "b", unsigned char, "=q", :"memory") |
||
267 | build_mmio_read(readw, "w", unsigned short, "=r", :"memory") |
||
268 | build_mmio_read(readl, "l", unsigned int, "=r", :"memory") |
||
269 | |||
270 | build_mmio_read(__readb, "b", unsigned char, "=q", ) |
||
271 | build_mmio_read(__readw, "w", unsigned short, "=r", ) |
||
272 | build_mmio_read(__readl, "l", unsigned int, "=r", ) |
||
273 | |||
274 | build_mmio_write(writeb, "b", unsigned char, "q", :"memory") |
||
275 | build_mmio_write(writew, "w", unsigned short, "r", :"memory") |
||
276 | build_mmio_write(writel, "l", unsigned int, "r", :"memory") |
||
277 | |||
278 | build_mmio_write(__writeb, "b", unsigned char, "q", ) |
||
279 | build_mmio_write(__writew, "w", unsigned short, "r", ) |
||
280 | build_mmio_write(__writel, "l", unsigned int, "r", ) |
||
281 | |||
282 | #define readb_relaxed(a) __readb(a) |
||
283 | #define readw_relaxed(a) __readw(a) |
||
284 | #define readl_relaxed(a) __readl(a) |
||
285 | #define __raw_readb __readb |
||
286 | #define __raw_readw __readw |
||
287 | #define __raw_readl __readl |
||
288 | |||
289 | #define __raw_writeb __writeb |
||
290 | #define __raw_writew __writew |
||
291 | #define __raw_writel __writel |
||
292 | |||
293 | static inline __u64 readq(const volatile void __iomem *addr) |
||
294 | { |
||
295 | const volatile u32 __iomem *p = addr; |
||
296 | u32 low, high; |
||
297 | |||
298 | low = readl(p); |
||
299 | high = readl(p + 1); |
||
300 | |||
301 | return low + ((u64)high << 32); |
||
302 | } |
||
303 | |||
304 | static inline void writeq(__u64 val, volatile void __iomem *addr) |
||
305 | { |
||
306 | writel(val, addr); |
||
307 | writel(val >> 32, addr+4); |
||
308 | } |
||
309 | |||
3031 | serge | 310 | #define swap(a, b) \ |
311 | do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) |
||
2967 | Serge | 312 | |
3031 | serge | 313 | |
2967 | Serge | 314 | #define mmiowb() barrier() |
315 | |||
316 | #define dev_err(dev, format, arg...) \ |
||
317 | printk("Error %s " format, __func__ , ## arg) |
||
318 | |||
319 | #define dev_warn(dev, format, arg...) \ |
||
320 | printk("Warning %s " format, __func__ , ## arg) |
||
321 | |||
322 | #define dev_info(dev, format, arg...) \ |
||
323 | printk("Info %s " format , __func__, ## arg) |
||
324 | |||
325 | #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) |
||
326 | |||
327 | |||
328 | struct scatterlist { |
||
329 | unsigned long page_link; |
||
330 | unsigned int offset; |
||
331 | unsigned int length; |
||
332 | dma_addr_t dma_address; |
||
333 | unsigned int dma_length; |
||
334 | }; |
||
335 | |||
3031 | serge | 336 | struct sg_table { |
337 | struct scatterlist *sgl; /* the list */ |
||
338 | unsigned int nents; /* number of mapped entries */ |
||
339 | unsigned int orig_nents; /* original size of list */ |
||
340 | }; |
||
341 | |||
342 | #define SG_MAX_SINGLE_ALLOC (4096 / sizeof(struct scatterlist)) |
||
343 | |||
344 | struct scatterlist *sg_next(struct scatterlist *sg); |
||
345 | |||
346 | #define sg_dma_address(sg) ((sg)->dma_address) |
||
347 | #define sg_dma_len(sg) ((sg)->length) |
||
348 | |||
349 | #define sg_is_chain(sg) ((sg)->page_link & 0x01) |
||
350 | #define sg_is_last(sg) ((sg)->page_link & 0x02) |
||
351 | #define sg_chain_ptr(sg) \ |
||
352 | ((struct scatterlist *) ((sg)->page_link & ~0x03)) |
||
353 | |||
354 | static inline addr_t sg_page(struct scatterlist *sg) |
||
355 | { |
||
356 | return (addr_t)((sg)->page_link & ~0x3); |
||
357 | } |
||
358 | |||
359 | #define for_each_sg(sglist, sg, nr, __i) \ |
||
360 | for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg)) |
||
361 | |||
362 | |||
363 | |||
2967 | Serge | 364 | struct page |
365 | { |
||
366 | unsigned int addr; |
||
367 | }; |
||
368 | |||
369 | |||
370 | struct vm_fault { |
||
371 | unsigned int flags; /* FAULT_FLAG_xxx flags */ |
||
372 | pgoff_t pgoff; /* Logical page offset based on vma */ |
||
373 | void __user *virtual_address; /* Faulting virtual address */ |
||
374 | |||
375 | struct page *page; /* ->fault handlers should return a |
||
376 | * page here, unless VM_FAULT_NOPAGE |
||
377 | * is set (which is also implied by |
||
378 | * VM_FAULT_ERROR). |
||
379 | */ |
||
380 | }; |
||
381 | |||
3031 | serge | 382 | struct pagelist { |
383 | dma_addr_t *page; |
||
384 | unsigned int nents; |
||
385 | }; |
||
2967 | Serge | 386 | |
1408 | serge | 387 | #endif>><>>>>>>>>7>6>5>4>3>2>1>0> |
388 |