]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/include/can_sysdep.h
LinCAN driver updated to work on Real-Time Preemption enabled kernel.
[lincan.git] / lincan / include / can_sysdep.h
1 /* can_sysdep.h - hides differences between individual Linux kernel 
2  *                versions and RT extensions 
3  * Linux CAN-bus device driver.
4  * Written by Pavel Pisa - OCERA team member
5  * email:pisa@cmp.felk.cvut.cz
6  * This software is released under the GPL-License.
7  * Version lincan-0.3  17 Jun 2004
8  */
9
10 #ifndef _CAN_SYSDEP_H
11 #define _CAN_SYSDEP_H
12
13 #ifdef CAN_WITH_RTL
14 #include <rtl.h>
15 #include <rtl_sync.h>
16 #include <rtl_core.h>
17 #include <rtl_mutex.h>
18 #include <rtl_sched.h>
19 #include <time.h>
20 #endif /*CAN_WITH_RTL*/
21
22 /*#define __NO_VERSION__*/
23 /*#include <linux/module.h>*/
24
25 #include <linux/version.h>
26 #include <linux/wait.h>
27 #include <linux/list.h>
28 #include <linux/fs.h>
29 #include <linux/ioport.h>
30 #include <linux/delay.h>
31 #include <linux/sched.h>
32 #include <linux/interrupt.h>
33 #include <asm/errno.h>
34
35 #include <asm/io.h>
36 #include <asm/atomic.h>
37 #include <asm/irq.h>
38 #include <asm/uaccess.h>
39
40 #include "lincan_config.h"
41
42 /*optional features*/
43 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0))
44 #define CAN_ENABLE_KERN_FASYNC
45 #ifdef CONFIG_PCI
46 #define CAN_ENABLE_PCI_SUPPORT
47 #endif
48 #ifdef CONFIG_OC_LINCANVME
49 #define CAN_ENABLE_VME_SUPPORT
50 #endif
51 #endif
52
53 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
54 #include <linux/malloc.h>
55 #else
56 #include <linux/slab.h>
57 #endif
58
59 #ifdef CAN_ENABLE_PCI_SUPPORT
60 #include "linux/pci.h"
61 #endif /*CAN_ENABLE_PCI_SUPPORT*/
62
63 /* Next is not sctrictly correct, because of 2.3.0, 2.3.1, 2.3.2
64    kernels need next definitions  too */
65 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,2,19)) /* may need correction */
66   #define wait_queue_head_t struct wait_queue *
67   #define wait_queue_t      struct wait_queue
68   #define init_waitqueue_head(queue_head) (*queue_head=NULL)
69   #define init_waitqueue_entry(qentry,qtask) \
70                         (qentry->next=NULL,qentry->task=qtask)
71   #define DECLARE_WAIT_QUEUE_HEAD(name) \
72         struct wait_queue * name=NULL
73   #define DECLARE_WAITQUEUE(wait, current) \
74         struct wait_queue wait = { current, NULL }
75   #define init_MUTEX(sem) (*sem=MUTEX)
76   #define DECLARE_MUTEX(name) struct semaphore name=MUTEX
77 #endif /* 2.2.19 */
78
79 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)) && !defined(DECLARE_TASKLET)
80   #define tasklet_struct tq_struct
81   #define DECLARE_TASKLET(_name, _func, _data) \
82                 struct tq_struct _name = { sync: 0, routine: _func, data: (void*)_data }
83
84   /* void tasklet_init(struct tasklet_struct *t, void (*func)(unsigned long), unsigned long data); */
85   #define tasklet_init(_tasklet, _func, _data) \
86     do{ \
87        /* (_tasklet)->next=NULL; */ \
88        /* Above not needed for 2.2.x and buggy for 2.4.x */ \
89        (_tasklet)->sync=0; \
90        (_tasklet)->routine=_func; \
91        (_tasklet)->data=(void*)_data; \
92     }while(0)
93
94   /* void tasklet_schedule(struct tasklet_struct *t) */
95   #define tasklet_schedule(_tasklet) \
96     do{ \
97        queue_task(_tasklet,&tq_immediate); \
98        mark_bh(IMMEDIATE_BH); \
99     }while(0)
100
101   /* void tasklet_kill(struct tasklet_struct *t); */
102   #define tasklet_kill(_tasklet) \
103                 synchronize_irq()
104
105 #endif /* 2.4.0 */
106
107
108 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,7)) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
109
110 #define MINOR_NR \
111         (MINOR(file->f_dentry->d_inode->i_rdev))
112
113 #else /* Linux kernel < 2.5.7 or >= 2.6.0 */
114
115 #define MINOR_NR \
116         (minor(file->f_dentry->d_inode->i_rdev))
117
118 #endif /* Linux kernel < 2.5.7 or >= 2.6.0 */
119
120 #ifndef CAN_WITH_RTL
121 #if ((LINUX_VERSION_CODE < KERNEL_VERSION(2,5,68)) && !defined(IRQ_RETVAL))
122   typedef void can_irqreturn_t;
123   #define CAN_IRQ_NONE
124   #define CAN_IRQ_HANDLED
125   #define CAN_IRQ_RETVAL(x)
126 #else /* <=2.5.67 */
127   typedef irqreturn_t can_irqreturn_t;
128   #define CAN_IRQ_NONE    IRQ_NONE
129   #define CAN_IRQ_HANDLED IRQ_HANDLED
130   #define CAN_IRQ_RETVAL  IRQ_RETVAL
131 #endif /* <=2.5.67 */
132 #else /*CAN_WITH_RTL*/
133   typedef int can_irqreturn_t;
134   #define CAN_IRQ_NONE        0
135   #define CAN_IRQ_HANDLED     1
136   #define CAN_IRQ_RETVAL(x)   ((x) != 0)
137 #endif /*CAN_WITH_RTL*/
138
139 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,33))
140    #define can_synchronize_irq(irqnum) synchronize_irq()
141 #else /* >=2.5.33 */
142    #define can_synchronize_irq synchronize_irq
143 #endif
144
145 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
146   #define del_timer_sync del_timer
147 #endif /* <2.4.0 */
148
149 #ifdef __HAVE_ARCH_CMPXCHG
150   #define CAN_HAVE_ARCH_CMPXCHG
151 #endif
152
153 #ifndef CAN_WITH_RTL
154 /* Standard LINUX kernel */
155
156 #define can_spinlock_t             spinlock_t
157 #define can_spin_irqflags_t        unsigned long
158 #define can_spin_lock              spin_lock
159 #define can_spin_unlock            spin_unlock
160 #define can_spin_lock_irqsave      spin_lock_irqsave
161 #define can_spin_unlock_irqrestore spin_unlock_irqrestore
162 #define can_spin_lock_init         spin_lock_init
163
164 #ifndef DEFINE_SPINLOCK
165 #define CAN_DEFINE_SPINLOCK(x)     can_spinlock_t x = SPIN_LOCK_UNLOCKED
166 #else /*DEFINE_SPINLOCK*/
167 #define CAN_DEFINE_SPINLOCK        DEFINE_SPINLOCK
168 #endif /*DEFINE_SPINLOCK*/
169
170 #if defined(CONFIG_PREEMPT) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
171 #define can_preempt_disable        preempt_disable
172 #define can_preempt_enable         preempt_enable
173 #else /*CONFIG_PREEMPT*/
174 #define can_preempt_disable()      do { } while (0)
175 #define can_preempt_enable()       do { } while (0)
176 #endif /*CONFIG_PREEMPT*/
177
178 #define can_enable_irq             enable_irq
179 #define can_disable_irq            disable_irq
180
181 #define can_printk                 printk
182
183 /* CAN message timestamp source, it is called from interrupt context */
184 #define can_gettimeofday do_gettimeofday
185
186 #else /*CAN_WITH_RTL*/
187
188 #define can_spinlock_t             rtl_spinlock_t
189 #define can_spin_irqflags_t        rtl_irqstate_t
190 #define can_spin_lock              rtl_spin_lock
191 #define can_spin_unlock            rtl_spin_unlock
192 #define can_spin_lock_irqsave      rtl_spin_lock_irqsave
193 #define can_spin_unlock_irqrestore rtl_spin_unlock_irqrestore
194 #define can_spin_lock_init         rtl_spin_lock_init
195
196 #define CAN_DEFINE_SPINLOCK(x)     can_spinlock_t x = SPIN_LOCK_UNLOCKED
197
198 #define can_preempt_disable()      do { } while (0)
199 #define can_preempt_enable()       do { } while (0)
200
201 #define can_enable_irq             rtl_hard_enable_irq
202 #define can_disable_irq            rtl_hard_disable_irq
203
204 #define can_printk                 rtl_printf
205
206 /*
207  * terrible hack to test rtl_file private_data concept, ugh !!!
208  * this would result in crash on architectures,  where 
209  * sizeof(int) < sizeof(void *)
210  */
211 #define can_set_rtl_file_private_data(fptr, p) do{ fptr->f_minor=(long)(p); } while(0)
212 #define can_get_rtl_file_private_data(fptr) ((void*)((fptr)->f_minor))
213
214 extern can_spinlock_t can_irq_manipulation_lock;
215
216 /* CAN message timestamp source, it is called from interrupt context */
217 #define can_gettimeofday(ptr) do {\
218           struct timespec temp_timespec;\
219           clock_gettime(CLOCK_REALTIME,&temp_timespec);\
220           ptr->tv_usec=temp_timespec.tv_nsec/1000;\
221           ptr->tv_sec=temp_timespec.tv_sec;\
222         } while(0)
223
224 #endif /*CAN_WITH_RTL*/
225
226 #endif /*_CAN_SYSDEP_H*/