]> 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 <linux/time.h>
59 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
60         #include <linux/kref.h>
61 #endif
62 #include <asm/errno.h>
63
64 #include <asm/io.h>
65 #include <asm/atomic.h>
66 #include <asm/irq.h>
67 #include <asm/uaccess.h>
68
69 #include "lincan_config.h"
70
71 /*optional features*/
72 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0))
73 #define CAN_ENABLE_KERN_FASYNC
74 #ifdef CONFIG_PCI
75 #define CAN_ENABLE_PCI_SUPPORT
76 #endif
77 #ifdef CONFIG_OC_LINCANVME
78 #define CAN_ENABLE_VME_SUPPORT
79 #endif
80 #endif
81
82 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
83 #include <linux/malloc.h>
84 #else
85 #include <linux/slab.h>
86 #endif
87
88 #ifdef CAN_ENABLE_PCI_SUPPORT
89 #include "linux/pci.h"
90 #endif /*CAN_ENABLE_PCI_SUPPORT*/
91
92 /* Next is not sctrictly correct, because of 2.3.0, 2.3.1, 2.3.2
93    kernels need next definitions  too */
94 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,2,19)) /* may need correction */
95   #define wait_queue_head_t struct wait_queue *
96   #define wait_queue_t      struct wait_queue
97   #define init_waitqueue_head(queue_head) (*queue_head=NULL)
98   #define init_waitqueue_entry(qentry,qtask) \
99                         (qentry->next=NULL,qentry->task=qtask)
100   #define DECLARE_WAIT_QUEUE_HEAD(name) \
101         struct wait_queue * name=NULL
102   #define DECLARE_WAITQUEUE(wait, current) \
103         struct wait_queue wait = { current, NULL }
104   #define init_MUTEX(sem) (*sem=MUTEX)
105   #define DECLARE_MUTEX(name) struct semaphore name=MUTEX
106 #endif /* 2.2.19 */
107
108 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)) && !defined(DECLARE_TASKLET)
109   #define tasklet_struct tq_struct
110   #define DECLARE_TASKLET(_name, _func, _data) \
111                 struct tq_struct _name = { sync: 0, routine: _func, data: (void*)_data }
112
113   /* void tasklet_init(struct tasklet_struct *t, void (*func)(unsigned long), unsigned long data); */
114   #define tasklet_init(_tasklet, _func, _data) \
115     do{ \
116        /* (_tasklet)->next=NULL; */ \
117        /* Above not needed for 2.2.x and buggy for 2.4.x */ \
118        (_tasklet)->sync=0; \
119        (_tasklet)->routine=_func; \
120        (_tasklet)->data=(void*)_data; \
121     }while(0)
122
123   /* void tasklet_schedule(struct tasklet_struct *t) */
124   #define tasklet_schedule(_tasklet) \
125     do{ \
126        queue_task(_tasklet,&tq_immediate); \
127        mark_bh(IMMEDIATE_BH); \
128     }while(0)
129
130   /* void tasklet_kill(struct tasklet_struct *t); */
131   #define tasklet_kill(_tasklet) \
132                 synchronize_irq()
133
134 #endif /* 2.4.0 */
135
136
137 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,7)) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
138
139 #define MINOR_NR \
140         (MINOR(file->f_dentry->d_inode->i_rdev))
141
142 #else /* Linux kernel < 2.5.7 or >= 2.6.0 */
143
144 #define MINOR_NR \
145         (minor(file->f_dentry->d_inode->i_rdev))
146
147 #endif /* Linux kernel < 2.5.7 or >= 2.6.0 */
148
149 #ifndef CAN_WITH_RTL
150 #if ((LINUX_VERSION_CODE < KERNEL_VERSION(2,5,68)) && !defined(IRQ_RETVAL))
151   typedef void can_irqreturn_t;
152   #define CAN_IRQ_NONE
153   #define CAN_IRQ_HANDLED
154   #define CAN_IRQ_RETVAL(x)
155 #else /* <=2.5.67 */
156   typedef irqreturn_t can_irqreturn_t;
157   #define CAN_IRQ_NONE    IRQ_NONE
158   #define CAN_IRQ_HANDLED IRQ_HANDLED
159   #define CAN_IRQ_RETVAL  IRQ_RETVAL
160 #endif /* <=2.5.67 */
161 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
162   #define CAN_IRQ_HANDLER_ARGS(irq_number, dev_id) \
163                 int irq_number, void *dev_id, struct pt_regs *regs
164 #else /* < 2.6.19 */
165   #define CAN_IRQ_HANDLER_ARGS(irq_number, dev_id) \
166                 int irq_number, void *dev_id
167 #endif /* < 2.6.19 */
168 #else /*CAN_WITH_RTL*/
169   typedef int can_irqreturn_t;
170   #define CAN_IRQ_NONE        0
171   #define CAN_IRQ_HANDLED     1
172   #define CAN_IRQ_RETVAL(x)   ((x) != 0)
173   #define CAN_IRQ_HANDLER_ARGS(irq_number, dev_id) \
174                 int irq_number, void *dev_id, struct pt_regs *regs
175 #endif /*CAN_WITH_RTL*/
176
177 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,33))
178    #define can_synchronize_irq(irqnum) synchronize_irq()
179 #else /* >=2.5.33 */
180    #define can_synchronize_irq synchronize_irq
181 #endif
182
183 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
184   #define del_timer_sync del_timer
185 #endif /* <2.4.0 */
186
187 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,9))
188    typedef unsigned long can_ioptr_t;
189    #define can_ioptr2ulong(ioaddr) ((unsigned long)(ioaddr))
190    #define can_ulong2ioptr(addr)   ((unsigned long)(addr))
191    #define can_inb(ioaddr) inb(ioaddr)
192    #define can_outb(data,ioaddr) outb(data,ioaddr)
193    #define can_inw(ioaddr) inw(ioaddr)
194    #define can_outw(data,ioaddr) outw(data,ioaddr)
195    #define can_inl(ioaddr) inl(ioaddr)
196    #define can_outl(data,ioaddr) outl(data,ioaddr)
197 #else /* >=2.6.9 */
198    typedef void __iomem * can_ioptr_t;
199    #define can_ioptr2ulong(ioaddr) ((unsigned long __force)(ioaddr))
200    #define can_ulong2ioptr(addr)   ((can_ioptr_t)(addr))
201    #define can_inb(ioaddr) inb(can_ioptr2ulong(ioaddr))
202    #define can_outb(data,ioaddr) outb(data,can_ioptr2ulong(ioaddr))
203    #define can_inw(ioaddr) inw(can_ioptr2ulong(ioaddr))
204    #define can_outw(data,ioaddr) outw(data,can_ioptr2ulong(ioaddr))
205    #define can_inl(ioaddr) inl(can_ioptr2ulong(ioaddr))
206    #define can_outl(data,ioaddr) outl(data,can_ioptr2ulong(ioaddr))
207 #endif
208
209 #define can_readb  readb
210 #define can_writeb writeb
211 #define can_readw  readw
212 #define can_writew writew
213 #define can_readl  readl
214 #define can_writel writel
215
216 #define can_ioport2ioptr can_ulong2ioptr
217
218 #ifdef __HAVE_ARCH_CMPXCHG
219   #define CAN_HAVE_ARCH_CMPXCHG
220   #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
221     #define CAN_HAVE_ATOMIC_CMPXCHG
222   #endif
223 #endif
224
225 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
226   #define can_pci_dev_get(_M_pdev) (_M_pdev)
227   #define can_pci_dev_put(_M_pdev) do {} while(0)
228   #define can_pci_get_device pci_find_device
229   #define can_pci_get_subsys pci_find_subsys
230 #else /* > 2.6.0 */
231   #define can_pci_dev_get pci_dev_get
232   #define can_pci_dev_put pci_dev_put
233   #define can_pci_get_device pci_get_device
234   #define can_pci_get_subsys pci_get_subsys
235 #endif
236
237 #ifndef CAN_WITH_RTL
238 /* Standard LINUX kernel */
239
240 #define can_spinlock_t             spinlock_t
241 #define can_spin_irqflags_t        unsigned long
242 #define can_spin_lock              spin_lock
243 #define can_spin_unlock            spin_unlock
244 #define can_spin_lock_irqsave      spin_lock_irqsave
245 #define can_spin_unlock_irqrestore spin_unlock_irqrestore
246 #define can_spin_lock_init         spin_lock_init
247
248 #ifndef DEFINE_SPINLOCK
249 #define CAN_DEFINE_SPINLOCK(x)     can_spinlock_t x = SPIN_LOCK_UNLOCKED
250 #else /*DEFINE_SPINLOCK*/
251 #define CAN_DEFINE_SPINLOCK        DEFINE_SPINLOCK
252 #endif /*DEFINE_SPINLOCK*/
253
254 #if !defined(CONFIG_PREEMPT_RT) && ( defined(CONFIG_PREEMPT) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)) )
255 #define can_preempt_disable        preempt_disable
256 #define can_preempt_enable         preempt_enable
257 #else /*CONFIG_PREEMPT*/
258 #define can_preempt_disable()      do { } while (0)
259 #define can_preempt_enable()       do { } while (0)
260 #endif /*CONFIG_PREEMPT*/
261
262 #define can_enable_irq             enable_irq
263 #define can_disable_irq            disable_irq
264
265 #define can_printk                 printk
266
267 /* CAN message timestamp source, it is called from interrupt context */
268 #define can_gettimeofday do_gettimeofday
269
270 #else /*CAN_WITH_RTL*/
271
272 #define can_spinlock_t             rtl_spinlock_t
273 #define can_spin_irqflags_t        rtl_irqstate_t
274 #define can_spin_lock              rtl_spin_lock
275 #define can_spin_unlock            rtl_spin_unlock
276 #define can_spin_lock_irqsave      rtl_spin_lock_irqsave
277 #define can_spin_unlock_irqrestore rtl_spin_unlock_irqrestore
278 #define can_spin_lock_init         rtl_spin_lock_init
279
280 #define CAN_DEFINE_SPINLOCK(x)     can_spinlock_t x = SPIN_LOCK_UNLOCKED
281
282 #define can_preempt_disable()      do { } while (0)
283 #define can_preempt_enable()       do { } while (0)
284
285 #define can_enable_irq             rtl_hard_enable_irq
286 #define can_disable_irq            rtl_hard_disable_irq
287
288 #define can_printk                 rtl_printf
289
290 /*
291  * terrible hack to test rtl_file private_data concept, ugh !!!
292  * this would result in crash on architectures,  where
293  * sizeof(int) < sizeof(void *)
294  */
295 #define can_set_rtl_file_private_data(fptr, p) do{ fptr->f_minor=(long)(p); } while(0)
296 #define can_get_rtl_file_private_data(fptr) ((void*)((fptr)->f_minor))
297
298 extern can_spinlock_t can_irq_manipulation_lock;
299
300 /* CAN message timestamp source, it is called from interrupt context */
301 #define can_gettimeofday(ptr) do {\
302           struct timespec temp_timespec;\
303           clock_gettime(CLOCK_REALTIME,&temp_timespec);\
304           ptr->tv_usec=temp_timespec.tv_nsec/1000;\
305           ptr->tv_sec=temp_timespec.tv_sec;\
306         } while(0)
307
308 #endif /*CAN_WITH_RTL*/
309
310 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4))
311         #include <linux/kthread.h>
312         #define can_kthread_create      kthread_create
313         #define can_kthread_run kthread_run
314         #define can_kthread_bind        kthread_bind
315         #define can_kthread_stop        kthread_stop
316         #define can_kthread_should_stop kthread_should_stop
317 #else
318         #define can_kthread_create
319         #define can_kthread_run
320         #define can_kthread_bind
321         #define can_kthread_stop
322         #define can_kthread_should_stop
323 #endif
324
325
326 #endif /*_CAN_SYSDEP_H*/