Subversion Repositories Kolibri OS

Rev

Rev 3031 | Rev 3243 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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