]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/include/can_sysdep.h
77f9d8afdc20e20e4ebe4e2d6d6b6ff2cd088696
[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 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
133   #define CAN_IRQ_HANDLER_ARGS(irq_number, dev_id) \
134                 int irq_number, void *dev_id, struct pt_regs *regs
135 #else /* < 2.6.19 */
136   #define CAN_IRQ_HANDLER_ARGS(irq_number, dev_id) \
137                 int irq_number, void *dev_id
138 #endif /* < 2.6.19 */
139 #else /*CAN_WITH_RTL*/
140   typedef int can_irqreturn_t;
141   #define CAN_IRQ_NONE        0
142   #define CAN_IRQ_HANDLED     1
143   #define CAN_IRQ_RETVAL(x)   ((x) != 0)
144   #define CAN_IRQ_HANDLER_ARGS(irq_number, dev_id) \
145                 int irq_number, void *dev_id, struct pt_regs *regs
146 #endif /*CAN_WITH_RTL*/
147
148 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,33))
149    #define can_synchronize_irq(irqnum) synchronize_irq()
150 #else /* >=2.5.33 */
151    #define can_synchronize_irq synchronize_irq
152 #endif
153
154 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
155   #define del_timer_sync del_timer
156 #endif /* <2.4.0 */
157
158 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,9))
159    typedef unsigned long can_ioptr_t;
160    #define can_ioptr2ulong(ioaddr) ((unsigned long)(ioaddr))
161    #define can_ulong2ioptr(addr)   ((unsigned long)(addr))
162    #define can_inb(ioaddr) inb(ioaddr)
163    #define can_outb(data,ioaddr) outb(data,ioaddr)
164    #define can_inw(ioaddr) inw(ioaddr)
165    #define can_outw(data,ioaddr) outw(data,ioaddr)
166    #define can_inl(ioaddr) inl(ioaddr)
167    #define can_outl(data,ioaddr) outl(data,ioaddr)
168 #else /* >=2.6.9 */
169    typedef void __iomem * can_ioptr_t;
170    #define can_ioptr2ulong(ioaddr) ((unsigned long __force)(ioaddr))
171    #define can_ulong2ioptr(addr)   ((can_ioptr_t)(addr))
172    #define can_inb(ioaddr) inb(can_ioptr2ulong(ioaddr))
173    #define can_outb(data,ioaddr) outb(data,can_ioptr2ulong(ioaddr))
174    #define can_inw(ioaddr) inw(can_ioptr2ulong(ioaddr))
175    #define can_outw(data,ioaddr) outw(data,can_ioptr2ulong(ioaddr))
176    #define can_inl(ioaddr) inl(can_ioptr2ulong(ioaddr))
177    #define can_outl(data,ioaddr) outl(data,can_ioptr2ulong(ioaddr))
178 #endif
179
180 #define can_readb  readb
181 #define can_writeb writeb
182 #define can_readw  readw
183 #define can_writew writew
184 #define can_readl  readl
185 #define can_writel writel
186
187 #define can_ioport2ioptr can_ulong2ioptr
188
189 #ifdef __HAVE_ARCH_CMPXCHG
190   #define CAN_HAVE_ARCH_CMPXCHG
191 #endif
192
193 #ifndef CAN_WITH_RTL
194 /* Standard LINUX kernel */
195
196 #define can_spinlock_t             spinlock_t
197 #define can_spin_irqflags_t        unsigned long
198 #define can_spin_lock              spin_lock
199 #define can_spin_unlock            spin_unlock
200 #define can_spin_lock_irqsave      spin_lock_irqsave
201 #define can_spin_unlock_irqrestore spin_unlock_irqrestore
202 #define can_spin_lock_init         spin_lock_init
203
204 #ifndef DEFINE_SPINLOCK
205 #define CAN_DEFINE_SPINLOCK(x)     can_spinlock_t x = SPIN_LOCK_UNLOCKED
206 #else /*DEFINE_SPINLOCK*/
207 #define CAN_DEFINE_SPINLOCK        DEFINE_SPINLOCK
208 #endif /*DEFINE_SPINLOCK*/
209
210 #if !defined(CONFIG_PREEMPT_RT) && ( defined(CONFIG_PREEMPT) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)) )
211 #define can_preempt_disable        preempt_disable
212 #define can_preempt_enable         preempt_enable
213 #else /*CONFIG_PREEMPT*/
214 #define can_preempt_disable()      do { } while (0)
215 #define can_preempt_enable()       do { } while (0)
216 #endif /*CONFIG_PREEMPT*/
217
218 #define can_enable_irq             enable_irq
219 #define can_disable_irq            disable_irq
220
221 #define can_printk                 printk
222
223 /* CAN message timestamp source, it is called from interrupt context */
224 #define can_gettimeofday do_gettimeofday
225
226 #else /*CAN_WITH_RTL*/
227
228 #define can_spinlock_t             rtl_spinlock_t
229 #define can_spin_irqflags_t        rtl_irqstate_t
230 #define can_spin_lock              rtl_spin_lock
231 #define can_spin_unlock            rtl_spin_unlock
232 #define can_spin_lock_irqsave      rtl_spin_lock_irqsave
233 #define can_spin_unlock_irqrestore rtl_spin_unlock_irqrestore
234 #define can_spin_lock_init         rtl_spin_lock_init
235
236 #define CAN_DEFINE_SPINLOCK(x)     can_spinlock_t x = SPIN_LOCK_UNLOCKED
237
238 #define can_preempt_disable()      do { } while (0)
239 #define can_preempt_enable()       do { } while (0)
240
241 #define can_enable_irq             rtl_hard_enable_irq
242 #define can_disable_irq            rtl_hard_disable_irq
243
244 #define can_printk                 rtl_printf
245
246 /*
247  * terrible hack to test rtl_file private_data concept, ugh !!!
248  * this would result in crash on architectures,  where
249  * sizeof(int) < sizeof(void *)
250  */
251 #define can_set_rtl_file_private_data(fptr, p) do{ fptr->f_minor=(long)(p); } while(0)
252 #define can_get_rtl_file_private_data(fptr) ((void*)((fptr)->f_minor))
253
254 extern can_spinlock_t can_irq_manipulation_lock;
255
256 /* CAN message timestamp source, it is called from interrupt context */
257 #define can_gettimeofday(ptr) do {\
258           struct timespec temp_timespec;\
259           clock_gettime(CLOCK_REALTIME,&temp_timespec);\
260           ptr->tv_usec=temp_timespec.tv_nsec/1000;\
261           ptr->tv_sec=temp_timespec.tv_sec;\
262         } while(0)
263
264 #endif /*CAN_WITH_RTL*/
265
266 #endif /*_CAN_SYSDEP_H*/