Subversion Repositories Kolibri OS

Rev

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