]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/include/can_sysdep.h
Merge branch 'master' into can-usb1
[lincan.git] / lincan / include / can_sysdep.h
1 /**************************************************************************/
2 /* File: can_sysdep.h - hides differences between individual Linux kernel */
3 /*                      versions and RT extensions                        */
4 /*                                                                        */
5 /* LinCAN - (Not only) Linux CAN bus driver                               */
6 /* Copyright (C) 2002-2009 DCE FEE CTU Prague <http://dce.felk.cvut.cz>   */
7 /* Copyright (C) 2002-2009 Pavel Pisa <pisa@cmp.felk.cvut.cz>             */
8 /* Funded by OCERA and FRESCOR IST projects                               */
9 /*                                                                        */
10 /* LinCAN is free software; you can redistribute it and/or modify it      */
11 /* under terms of the GNU General Public License as published by the      */
12 /* Free Software Foundation; either version 2, or (at your option) any    */
13 /* later version.  LinCAN is distributed in the hope that it will be      */
14 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
15 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
16 /* General Public License for more details. You should have received a    */
17 /* copy of the GNU General Public License along with LinCAN; see file     */
18 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
19 /* Cambridge, MA 02139, USA.                                              */
20 /*                                                                        */
21 /* To allow use of LinCAN in the compact embedded systems firmware        */
22 /* and RT-executives (RTEMS for example), main authors agree with next    */
23 /* special exception:                                                     */
24 /*                                                                        */
25 /* Including LinCAN header files in a file, instantiating LinCAN generics */
26 /* or templates, or linking other files with LinCAN objects to produce    */
27 /* an application image/executable, does not by itself cause the          */
28 /* resulting application image/executable to be covered by                */
29 /* the GNU General Public License.                                        */
30 /* This exception does not however invalidate any other reasons           */
31 /* why the executable file might be covered by the GNU Public License.    */
32 /* Publication of enhanced or derived LinCAN files is required although.  */
33 /**************************************************************************/
34
35 #ifndef _CAN_SYSDEP_H
36 #define _CAN_SYSDEP_H
37
38 #ifdef CAN_WITH_RTL
39 #include <rtl.h>
40 #include <rtl_sync.h>
41 #include <rtl_core.h>
42 #include <rtl_mutex.h>
43 #include <rtl_sched.h>
44 #include <time.h>
45 #endif /*CAN_WITH_RTL*/
46
47 /*#define __NO_VERSION__*/
48 /*#include <linux/module.h>*/
49
50 #include <linux/version.h>
51 #include <linux/wait.h>
52 #include <linux/list.h>
53 #include <linux/fs.h>
54 #include <linux/ioport.h>
55 #include <linux/delay.h>
56 #include <linux/sched.h>
57 #include <linux/interrupt.h>
58 #include <asm/errno.h>
59
60 #include <asm/io.h>
61 #include <asm/atomic.h>
62 #include <asm/irq.h>
63 #include <asm/uaccess.h>
64
65 #include "lincan_config.h"
66
67 /*optional features*/
68 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0))
69 #define CAN_ENABLE_KERN_FASYNC
70 #ifdef CONFIG_PCI
71 #define CAN_ENABLE_PCI_SUPPORT
72 #endif
73 #ifdef CONFIG_OC_LINCANVME
74 #define CAN_ENABLE_VME_SUPPORT
75 #endif
76 #endif
77
78 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
79 #include <linux/malloc.h>
80 #else
81 #include <linux/slab.h>
82 #endif
83
84 #ifdef CAN_ENABLE_PCI_SUPPORT
85 #include "linux/pci.h"
86 #endif /*CAN_ENABLE_PCI_SUPPORT*/
87
88 /* Next is not sctrictly correct, because of 2.3.0, 2.3.1, 2.3.2
89    kernels need next definitions  too */
90 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,2,19)) /* may need correction */
91   #define wait_queue_head_t struct wait_queue *
92   #define wait_queue_t      struct wait_queue
93   #define init_waitqueue_head(queue_head) (*queue_head=NULL)
94   #define init_waitqueue_entry(qentry,qtask) \
95                         (qentry->next=NULL,qentry->task=qtask)
96   #define DECLARE_WAIT_QUEUE_HEAD(name) \
97         struct wait_queue * name=NULL
98   #define DECLARE_WAITQUEUE(wait, current) \
99         struct wait_queue wait = { current, NULL }
100   #define init_MUTEX(sem) (*sem=MUTEX)
101   #define DECLARE_MUTEX(name) struct semaphore name=MUTEX
102 #endif /* 2.2.19 */
103
104 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)) && !defined(DECLARE_TASKLET)
105   #define tasklet_struct tq_struct
106   #define DECLARE_TASKLET(_name, _func, _data) \
107                 struct tq_struct _name = { sync: 0, routine: _func, data: (void*)_data }
108
109   /* void tasklet_init(struct tasklet_struct *t, void (*func)(unsigned long), unsigned long data); */
110   #define tasklet_init(_tasklet, _func, _data) \
111     do{ \
112        /* (_tasklet)->next=NULL; */ \
113        /* Above not needed for 2.2.x and buggy for 2.4.x */ \
114        (_tasklet)->sync=0; \
115        (_tasklet)->routine=_func; \
116        (_tasklet)->data=(void*)_data; \
117     }while(0)
118
119   /* void tasklet_schedule(struct tasklet_struct *t) */
120   #define tasklet_schedule(_tasklet) \
121     do{ \
122        queue_task(_tasklet,&tq_immediate); \
123        mark_bh(IMMEDIATE_BH); \
124     }while(0)
125
126   /* void tasklet_kill(struct tasklet_struct *t); */
127   #define tasklet_kill(_tasklet) \
128                 synchronize_irq()
129
130 #endif /* 2.4.0 */
131
132
133 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,7)) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
134
135 #define MINOR_NR \
136         (MINOR(file->f_dentry->d_inode->i_rdev))
137
138 #else /* Linux kernel < 2.5.7 or >= 2.6.0 */
139
140 #define MINOR_NR \
141         (minor(file->f_dentry->d_inode->i_rdev))
142
143 #endif /* Linux kernel < 2.5.7 or >= 2.6.0 */
144
145 #ifndef CAN_WITH_RTL
146 #if ((LINUX_VERSION_CODE < KERNEL_VERSION(2,5,68)) && !defined(IRQ_RETVAL))
147   typedef void can_irqreturn_t;
148   #define CAN_IRQ_NONE
149   #define CAN_IRQ_HANDLED
150   #define CAN_IRQ_RETVAL(x)
151 #else /* <=2.5.67 */
152   typedef irqreturn_t can_irqreturn_t;
153   #define CAN_IRQ_NONE    IRQ_NONE
154   #define CAN_IRQ_HANDLED IRQ_HANDLED
155   #define CAN_IRQ_RETVAL  IRQ_RETVAL
156 #endif /* <=2.5.67 */
157 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
158   #define CAN_IRQ_HANDLER_ARGS(irq_number, dev_id) \
159                 int irq_number, void *dev_id, struct pt_regs *regs
160 #else /* < 2.6.19 */
161   #define CAN_IRQ_HANDLER_ARGS(irq_number, dev_id) \
162                 int irq_number, void *dev_id
163 #endif /* < 2.6.19 */
164 #else /*CAN_WITH_RTL*/
165   typedef int can_irqreturn_t;
166   #define CAN_IRQ_NONE        0
167   #define CAN_IRQ_HANDLED     1
168   #define CAN_IRQ_RETVAL(x)   ((x) != 0)
169   #define CAN_IRQ_HANDLER_ARGS(irq_number, dev_id) \
170                 int irq_number, void *dev_id, struct pt_regs *regs
171 #endif /*CAN_WITH_RTL*/
172
173 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,33))
174    #define can_synchronize_irq(irqnum) synchronize_irq()
175 #else /* >=2.5.33 */
176    #define can_synchronize_irq synchronize_irq
177 #endif
178
179 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
180   #define del_timer_sync del_timer
181 #endif /* <2.4.0 */
182
183 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,9))
184    typedef unsigned long can_ioptr_t;
185    #define can_ioptr2ulong(ioaddr) ((unsigned long)(ioaddr))
186    #define can_ulong2ioptr(addr)   ((unsigned long)(addr))
187    #define can_inb(ioaddr) inb(ioaddr)
188    #define can_outb(data,ioaddr) outb(data,ioaddr)
189    #define can_inw(ioaddr) inw(ioaddr)
190    #define can_outw(data,ioaddr) outw(data,ioaddr)
191    #define can_inl(ioaddr) inl(ioaddr)
192    #define can_outl(data,ioaddr) outl(data,ioaddr)
193 #else /* >=2.6.9 */
194    typedef void __iomem * can_ioptr_t;
195    #define can_ioptr2ulong(ioaddr) ((unsigned long __force)(ioaddr))
196    #define can_ulong2ioptr(addr)   ((can_ioptr_t)(addr))
197    #define can_inb(ioaddr) inb(can_ioptr2ulong(ioaddr))
198    #define can_outb(data,ioaddr) outb(data,can_ioptr2ulong(ioaddr))
199    #define can_inw(ioaddr) inw(can_ioptr2ulong(ioaddr))
200    #define can_outw(data,ioaddr) outw(data,can_ioptr2ulong(ioaddr))
201    #define can_inl(ioaddr) inl(can_ioptr2ulong(ioaddr))
202    #define can_outl(data,ioaddr) outl(data,can_ioptr2ulong(ioaddr))
203 #endif
204
205 #define can_readb  readb
206 #define can_writeb writeb
207 #define can_readw  readw
208 #define can_writew writew
209 #define can_readl  readl
210 #define can_writel writel
211
212 #define can_ioport2ioptr can_ulong2ioptr
213
214 #ifdef __HAVE_ARCH_CMPXCHG
215   #define CAN_HAVE_ARCH_CMPXCHG
216   #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
217     #define CAN_HAVE_ATOMIC_CMPXCHG
218   #endif
219 #endif
220
221 #ifndef CAN_WITH_RTL
222 /* Standard LINUX kernel */
223
224 #define can_spinlock_t             spinlock_t
225 #define can_spin_irqflags_t        unsigned long
226 #define can_spin_lock              spin_lock
227 #define can_spin_unlock            spin_unlock
228 #define can_spin_lock_irqsave      spin_lock_irqsave
229 #define can_spin_unlock_irqrestore spin_unlock_irqrestore
230 #define can_spin_lock_init         spin_lock_init
231
232 #ifndef DEFINE_SPINLOCK
233 #define CAN_DEFINE_SPINLOCK(x)     can_spinlock_t x = SPIN_LOCK_UNLOCKED
234 #else /*DEFINE_SPINLOCK*/
235 #define CAN_DEFINE_SPINLOCK        DEFINE_SPINLOCK
236 #endif /*DEFINE_SPINLOCK*/
237
238 #if !defined(CONFIG_PREEMPT_RT) && ( defined(CONFIG_PREEMPT) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)) )
239 #define can_preempt_disable        preempt_disable
240 #define can_preempt_enable         preempt_enable
241 #else /*CONFIG_PREEMPT*/
242 #define can_preempt_disable()      do { } while (0)
243 #define can_preempt_enable()       do { } while (0)
244 #endif /*CONFIG_PREEMPT*/
245
246 #define can_enable_irq             enable_irq
247 #define can_disable_irq            disable_irq
248
249 #define can_printk                 printk
250
251 /* CAN message timestamp source, it is called from interrupt context */
252 #define can_gettimeofday do_gettimeofday
253
254 #else /*CAN_WITH_RTL*/
255
256 #define can_spinlock_t             rtl_spinlock_t
257 #define can_spin_irqflags_t        rtl_irqstate_t
258 #define can_spin_lock              rtl_spin_lock
259 #define can_spin_unlock            rtl_spin_unlock
260 #define can_spin_lock_irqsave      rtl_spin_lock_irqsave
261 #define can_spin_unlock_irqrestore rtl_spin_unlock_irqrestore
262 #define can_spin_lock_init         rtl_spin_lock_init
263
264 #define CAN_DEFINE_SPINLOCK(x)     can_spinlock_t x = SPIN_LOCK_UNLOCKED
265
266 #define can_preempt_disable()      do { } while (0)
267 #define can_preempt_enable()       do { } while (0)
268
269 #define can_enable_irq             rtl_hard_enable_irq
270 #define can_disable_irq            rtl_hard_disable_irq
271
272 #define can_printk                 rtl_printf
273
274 /*
275  * terrible hack to test rtl_file private_data concept, ugh !!!
276  * this would result in crash on architectures,  where
277  * sizeof(int) < sizeof(void *)
278  */
279 #define can_set_rtl_file_private_data(fptr, p) do{ fptr->f_minor=(long)(p); } while(0)
280 #define can_get_rtl_file_private_data(fptr) ((void*)((fptr)->f_minor))
281
282 extern can_spinlock_t can_irq_manipulation_lock;
283
284 /* CAN message timestamp source, it is called from interrupt context */
285 #define can_gettimeofday(ptr) do {\
286           struct timespec temp_timespec;\
287           clock_gettime(CLOCK_REALTIME,&temp_timespec);\
288           ptr->tv_usec=temp_timespec.tv_nsec/1000;\
289           ptr->tv_sec=temp_timespec.tv_sec;\
290         } while(0)
291
292 #endif /*CAN_WITH_RTL*/
293
294 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4))
295         #include <linux/kthread.h>
296         #define can_kthread_create      kthread_create
297         #define can_kthread_run kthread_run
298         #define can_kthread_bind        kthread_bind
299         #define can_kthread_stop        kthread_stop
300         #define can_kthread_should_stop kthread_should_stop
301 #else
302         #define can_kthread_create
303         #define can_kthread_run
304         #define can_kthread_bind
305         #define can_kthread_stop
306         #define can_kthread_should_stop
307 #endif
308
309
310 #endif /*_CAN_SYSDEP_H*/