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