1 /**************************************************************************/
2 /* File: can_sysdep.h - hides differences between individual Linux kernel */
3 /* versions and RT extensions */
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 */
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. */
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: */
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 /**************************************************************************/
42 #include <rtl_mutex.h>
43 #include <rtl_sched.h>
45 #endif /*CAN_WITH_RTL*/
47 /*#define __NO_VERSION__*/
48 /*#include <linux/module.h>*/
50 #include <linux/version.h>
51 #include <linux/wait.h>
52 #include <linux/list.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>
61 #include <asm/atomic.h>
63 #include <asm/uaccess.h>
65 #include "lincan_config.h"
68 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0))
69 #define CAN_ENABLE_KERN_FASYNC
71 #define CAN_ENABLE_PCI_SUPPORT
73 #ifdef CONFIG_OC_LINCANVME
74 #define CAN_ENABLE_VME_SUPPORT
78 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
79 #include <linux/malloc.h>
81 #include <linux/slab.h>
84 #ifdef CAN_ENABLE_PCI_SUPPORT
85 #include "linux/pci.h"
86 #endif /*CAN_ENABLE_PCI_SUPPORT*/
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
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 }
109 /* void tasklet_init(struct tasklet_struct *t, void (*func)(unsigned long), unsigned long data); */
110 #define tasklet_init(_tasklet, _func, _data) \
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; \
119 /* void tasklet_schedule(struct tasklet_struct *t) */
120 #define tasklet_schedule(_tasklet) \
122 queue_task(_tasklet,&tq_immediate); \
123 mark_bh(IMMEDIATE_BH); \
126 /* void tasklet_kill(struct tasklet_struct *t); */
127 #define tasklet_kill(_tasklet) \
133 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,7)) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
136 (MINOR(file->f_dentry->d_inode->i_rdev))
138 #else /* Linux kernel < 2.5.7 or >= 2.6.0 */
141 (minor(file->f_dentry->d_inode->i_rdev))
143 #endif /* Linux kernel < 2.5.7 or >= 2.6.0 */
146 #if ((LINUX_VERSION_CODE < KERNEL_VERSION(2,5,68)) && !defined(IRQ_RETVAL))
147 typedef void can_irqreturn_t;
149 #define CAN_IRQ_HANDLED
150 #define CAN_IRQ_RETVAL(x)
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
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*/
173 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,33))
174 #define can_synchronize_irq(irqnum) synchronize_irq()
176 #define can_synchronize_irq synchronize_irq
179 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
180 #define del_timer_sync del_timer
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)
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))
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
212 #define can_ioport2ioptr can_ulong2ioptr
214 #ifdef __HAVE_ARCH_CMPXCHG
215 #define CAN_HAVE_ARCH_CMPXCHG
219 /* Standard LINUX kernel */
221 #define can_spinlock_t spinlock_t
222 #define can_spin_irqflags_t unsigned long
223 #define can_spin_lock spin_lock
224 #define can_spin_unlock spin_unlock
225 #define can_spin_lock_irqsave spin_lock_irqsave
226 #define can_spin_unlock_irqrestore spin_unlock_irqrestore
227 #define can_spin_lock_init spin_lock_init
229 #ifndef DEFINE_SPINLOCK
230 #define CAN_DEFINE_SPINLOCK(x) can_spinlock_t x = SPIN_LOCK_UNLOCKED
231 #else /*DEFINE_SPINLOCK*/
232 #define CAN_DEFINE_SPINLOCK DEFINE_SPINLOCK
233 #endif /*DEFINE_SPINLOCK*/
235 #if !defined(CONFIG_PREEMPT_RT) && ( defined(CONFIG_PREEMPT) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)) )
236 #define can_preempt_disable preempt_disable
237 #define can_preempt_enable preempt_enable
238 #else /*CONFIG_PREEMPT*/
239 #define can_preempt_disable() do { } while (0)
240 #define can_preempt_enable() do { } while (0)
241 #endif /*CONFIG_PREEMPT*/
243 #define can_enable_irq enable_irq
244 #define can_disable_irq disable_irq
246 #define can_printk printk
248 /* CAN message timestamp source, it is called from interrupt context */
249 #define can_gettimeofday do_gettimeofday
251 #else /*CAN_WITH_RTL*/
253 #define can_spinlock_t rtl_spinlock_t
254 #define can_spin_irqflags_t rtl_irqstate_t
255 #define can_spin_lock rtl_spin_lock
256 #define can_spin_unlock rtl_spin_unlock
257 #define can_spin_lock_irqsave rtl_spin_lock_irqsave
258 #define can_spin_unlock_irqrestore rtl_spin_unlock_irqrestore
259 #define can_spin_lock_init rtl_spin_lock_init
261 #define CAN_DEFINE_SPINLOCK(x) can_spinlock_t x = SPIN_LOCK_UNLOCKED
263 #define can_preempt_disable() do { } while (0)
264 #define can_preempt_enable() do { } while (0)
266 #define can_enable_irq rtl_hard_enable_irq
267 #define can_disable_irq rtl_hard_disable_irq
269 #define can_printk rtl_printf
272 * terrible hack to test rtl_file private_data concept, ugh !!!
273 * this would result in crash on architectures, where
274 * sizeof(int) < sizeof(void *)
276 #define can_set_rtl_file_private_data(fptr, p) do{ fptr->f_minor=(long)(p); } while(0)
277 #define can_get_rtl_file_private_data(fptr) ((void*)((fptr)->f_minor))
279 extern can_spinlock_t can_irq_manipulation_lock;
281 /* CAN message timestamp source, it is called from interrupt context */
282 #define can_gettimeofday(ptr) do {\
283 struct timespec temp_timespec;\
284 clock_gettime(CLOCK_REALTIME,&temp_timespec);\
285 ptr->tv_usec=temp_timespec.tv_nsec/1000;\
286 ptr->tv_sec=temp_timespec.tv_sec;\
289 #endif /*CAN_WITH_RTL*/
291 #endif /*_CAN_SYSDEP_H*/