mbgtools-lx  4.2.8
mbg_lx.h
Go to the documentation of this file.
1 
2 /**************************************************************************
3  *
4  * $Id: mbg_lx.h 1.13 2018/04/12 09:46:12 martin REL_M $
5  *
6  * Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany
7  *
8  * Description:
9  * OS dependend definitions/redefinitions for Linux.
10  *
11  * -----------------------------------------------------------------------
12  * $Log: mbg_lx.h $
13  * Revision 1.13 2018/04/12 09:46:12 martin
14  * Code to check if the kernel supports testing if a buffer
15  * is DMA capable.
16  * Revision 1.12 2017/07/05 16:54:16 martin
17  * Removed some obsolete code.
18  * Revision 1.11 2013/03/04 15:43:49 martin
19  * Account for __devinit and friends removed around kernel 3.8.
20  * Revision 1.10 2013/02/19 15:56:15 martin
21  * Moved more kernel version compatibility checks here.
22  * Revision 1.9 2012/10/12 11:23:11 martin
23  * Don't include linux/autoconf.h anymore.
24  * Revision 1.8 2009/03/13 09:04:18 martin
25  * Added default definition for _PCPS_USB_FULL_CYCLIC_INTV.
26  * Revision 1.7 2008/12/19 15:22:05 martin
27  * Moved lots of code here which provides compatibility across
28  * kernel versions.
29  * Added preliminary check if kthread API can be used. However,
30  * the exact version number still needs to be verified.
31  * Added a check whether USB timeouts have to be specified
32  * in jiffies (old) or milliseconds (newer).
33  * Revision 1.6 2007/02/22 15:26:56 martin
34  * Include linux/autoconf.h rather than linux/config.h since the latter has been
35  * a stub for autoconf.h which has been removed around kernel version 2.6.19.
36  * Revision 1.5 2006/09/20 12:09:29 martin
37  * Removed port I/O function redefinitions which have been replaced
38  * by generic function definitions in mbggenio.h.
39  * Revision 1.4 2006/07/07 12:06:40 martin
40  * Include modversions.h if required by the kernel.
41  * Revision 1.3 2001/05/31 09:21:09 martin
42  * #include <asm/io.h> instead of <sys/io.h>
43  * Revision 1.2 2001/03/05 16:36:11 MARTIN
44  * Renamed file.
45  *
46  **************************************************************************/
47 
48 #ifndef _MBG_LX_H
49 #define _MBG_LX_H
50 
51 
52 #include <linux/version.h>
53 
54 #if !defined( KERNEL_VERSION ) || ( LINUX_VERSION_CODE < KERNEL_VERSION( 2, 2, 0 ) )
55  #error "You need to use at least a v2.2.x Linux kernel."
56 #endif
57 
58 #if ( LINUX_VERSION_CODE < KERNEL_VERSION( 2, 6, 0 ) )
59  #define __NO_VERSION__ // don't declare kernel_version in <linux/module.h>
60 #endif
61 
62 
63 #if defined( CONFIG_MODVERSIONS ) && !defined( MODVERSIONS )
64  #define MODVERSIONS
65 #endif
66 
67 #if defined( MODVERSIONS )
68  #if ( LINUX_VERSION_CODE < KERNEL_VERSION( 2, 6, 0 ) )
69  #include <linux/modversions.h>
70  #endif
71 #endif
72 
73 #include <linux/module.h>
74 #include <linux/init.h>
75 #include <linux/kernel.h>
76 #include <linux/fs.h>
77 #include <linux/poll.h>
78 #include <linux/interrupt.h>
79 #include <linux/proc_fs.h>
80 #include <linux/init.h>
81 #include <linux/param.h>
82 
83 #include <asm/uaccess.h>
84 #include <asm/io.h>
85 
86 
87 // definitions for clock() support in kernel drivers
88 extern unsigned long volatile jiffies;
89 #define clock() jiffies
90 
91 
92 #define _MBG_INIT_CODE_ATTR __init
93 
94 
95 // Starting around kernel 3.8 __devinit and friends
96 // are not supported anymore.
97 #if !defined( __devinit )
98  #define __devinit
99 #endif
100 
101 #if !defined( __devinitdata )
102  #define __devinitdata
103 #endif
104 
105 #if !defined( __devexit )
106  #define __devexit
107 #endif
108 
109 
110 // Early hotplug systems may have defined __devexit,
111 // but not __devexit_p, and similarly kernel 3.8 and newer
112 // may not have defined __devexit_p anymore.
113 #if !defined( __devexit_p )
114  #define __devexit_p( _x ) _x
115 #endif
116 
117 
118 
119 #define _DEFAULT_MBG_TGT_LINUX_USE_PCI_PNP \
120  ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 4, 0 ) )
121 
122 #define _DEFAULT_MBG_TGT_LINUX_USE_USB \
123  ( ( defined( CONFIG_USB ) || defined( CONFIG_USB_MODULE ) ) && \
124  ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 6, 0 ) ) )
125 
126 #define _USE_LINUX_SYSFS \
127  ( defined( CONFIG_SYSFS ) )
128 
129 #define _USE_LINUX_DEVFS \
130  ( defined( CONFIG_DEVFS_FS ) )
131 
132 #define _USE_LINUX_DEVFS_26 \
133  ( _USE_LINUX_DEVFS && \
134  ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 6, 0 ) ) )
135 
136 #define _USE_LINUX_DEVFS_24 \
137  ( _USE_LINUX_DEVFS && \
138  ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 4, 0 ) ) )
139 
140 
141 // Starting with kernel 4.9 we *have to* make sure that e.g.
142 // USB I/O buffers are DMA capable. Otherwise the kernel will
143 // generate a trap-like message and prevents the driver from
144 // working , even if DMA is not used by the driver.
145 // The inline functions is_vmalloc_addr() and object_is_on_stack()
146 // can be used to check this, but these functions may not be
147 // available in older 2.6 kernels.
148 // is_vmalloc_addr() is supported in kernels ~2.6.25-rc1 and later,
149 // object_is_on_stack() is supported by ~2.6.27-rc1 and later,
150 // so we implement this for kernels which provide at least both
151 // of these functions.
152 #define _PCPS_CHK_BUFFER_DMA_CAPABLE \
153  ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 6, 27 ) )
154 
155 #if _PCPS_CHK_BUFFER_DMA_CAPABLE
156  // Since kernel 4.11 the inline function object_is_on_stack() has
157  // been moved from linux/sched.h to linux/sched/task_stack.h.
158  #if ( LINUX_VERSION_CODE >= KERNEL_VERSION( 4, 11, 0 ) )
159  #include <linux/sched/task_stack.h>
160  #else
161  #include <linux/sched.h>
162  #endif
163 
164  #define _pcps_buffer_is_dma_capable( _b ) \
165  ( !is_vmalloc_addr( _b ) && !object_is_on_stack( _b ) )
166 #endif
167 
168 
169 // Starting at some version 2.6.11 or 2.6.12 time out values for the
170 // USB functions need to be specified in ms, which is the default.
171 #define _PCPS_USB_TIMEOUT_IS_MS \
172  ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 6, 12 ) )
173 
174 // Older Linux kernels expect USB timeout intervals in jiffies, so
175 // we specify a conversion macro for this case.
176 #if !_PCPS_USB_TIMEOUT_IS_MS
177  #define _pcps_ms_to_usb_timeout( _ms ) ( (_ms) * HZ / 1000 )
178 #endif
179 
180 // Early Linux 2.6.x kernels generate a syslog msg on each USB timeout, so the syslog
181 // would be flooded with USB timeout msgs if we used short timeouts with retries.
182 #if !defined( _PCPS_USB_FULL_CYCLIC_INTV )
183  #define _PCPS_USB_FULL_CYCLIC_INTV \
184  ( LINUX_VERSION_CODE < KERNEL_VERSION( 2, 6, 11 ) )
185 #endif
186 
187 #include <linux/cdev.h> // Requires kernel 2.6.0 or newer
188 
189 #if !defined( _PCPS_USE_LINUX_KTHREAD )
190  #define _PCPS_USE_LINUX_KTHREAD \
191  ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 6, 10 ) ) //##++++
192 #endif
193 
194 #if _PCPS_USE_LINUX_KTHREAD
195  #include <linux/kthread.h>
196 #endif
197 
198 
199 #if !defined( NEW_FASYNC )
200  // A third parameter to kill_fasync has been added in kernel 2.3.21,
201  // and is _not_ present in _stock_ 2.2.14 and 2.2.15. However, some
202  // distributions patch 2.2.x kernels to add this parameter.
203  #define NEW_FASYNC ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 2, 18 ) )
204 #endif
205 
206 #if !defined( NEW_FASYNC2 )
207  // Type of first parameter of kill_fasync() changed in kernel 2.4.0-test2
208  #define NEW_FASYNC2 ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 4, 0 ) )
209 #endif
210 
211 #if !defined( NEW_WAIT_QUEUE )
212  // New wait queue types and macros have been defined around kernel 2.2.18.
213  #define NEW_WAIT_QUEUE ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 2, 18 ) )
214 #endif
215 
216 #if !defined( NEW_FILE_OPS )
217  // A field "owner" has been added to struct file_operations in kernel 2.4.0
218  // which needs to be initialized properly.
219  #define NEW_FILE_OPS ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 4, 0 ) )
220 #endif
221 
222 #if !defined( USE_WAIT_EVENT )
223  // Function wait_event_interruptible() has been added in kernel 2.4.20
224  // which replaces the older interruptible_sleep_on() function.
225  #define USE_WAIT_EVENT ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 4, 20 ) )
226 #endif
227 
228 #if !defined( ESPIPE_BY_VFS )
229  // ESPIPE logic has been removed from drivers and is handled by the VFS layer now.
230  #define ESPIPE_BY_VFS ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 6, 8 ) )
231 #endif
232 
233 #if !defined( FLUSH_WITH_LOCK_OWNER_ID )
234  // Starting in kernel 2.6.18 the prototype for the flush function in
235  // struct file_operations in fs.h has been modified to take the POSIX
236  // lock owner ID as additional parameter.
237  // The same patch also defined FL_CLOSE in fs.h, so we check additionally
238  // whether this symbol is defined.
239  // At least for SUSE SLES10 this has been backported to a kernel which
240  // reports to be 2.6.16.
241  #define FLUSH_WITH_LOCK_OWNER_ID \
242  ( ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 6, 18 ) ) \
243  || defined( FL_CLOSE ) )
244 #endif
245 
246 #if !defined( REQUEST_IRQ_WO_REGS )
247  // The prototype of the interrupt handler function has changed around 2.6.19.
248  // The newer version does not take a pointer to a register set.
249  // This also seems to include linux/bottom_half.h from linux/interrupt.h,
250  // so a criteria for this is also if _LINUX_BH_H is defined after
251  // linux/interrupt.h has been included.
252  #define REQUEST_IRQ_WO_REGS \
253  ( ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 6, 19 ) \
254  || defined( _LINUX_BH_H ) ) )
255 #endif
256 
257 
258 #if !defined( IRQ_RETVAL )
259  // defines for compatibility with older kernels
260  typedef void irqreturn_t;
261  #define IRQ_NONE
262  #define IRQ_HANDLED
263 //##++ #define IRQ_RETVAL(x)
264 #endif
265 
266 
267 #if !defined( GENERIC_GPIO_SUPPORTED )
268  //##++++ Exact kernel version to be determined!
269  // At least 2.6.34 provides linux/gpio.h.
270  #define GENERIC_GPIO_SUPPORTED \
271  ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 6, 34 ) )
272 #endif
273 
274 
275 // Care about kernel API changes across kernel versions
276 
277 #define _PCPS_MUST_UPDATE_USE_COUNT \
278  ( LINUX_VERSION_CODE < KERNEL_VERSION( 2, 6, 0 ) )
279 
280 #define _PCPS_HAVE_LINUX_CLASS \
281  ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 6, 0 ) )
282 
283 #define _PCPS_HAVE_LINUX_CLASS_SIMPLE \
284  ( _PCPS_HAVE_LINUX_CLASS && ( LINUX_VERSION_CODE < KERNEL_VERSION( 2, 6, 13 ) ) )
285 
286 #define _PCPS_HAVE_LINUX_CLASS_CREATE \
287  ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 6, 13 ) )
288 
289 #define _PCPS_CLASS_DEVICE_CREATE_WITH_PARENT \
290  ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 6, 14 ) )
291 
292 #define _PCPS_CLASS_DEV_OBSOLETED \
293  ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 6, 26 ) )
294 
295 #define _PCPS_DEVICE_CREATE_WITH_DEV_DATA \
296  ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 6, 27 ) )
297 
298 
299 #if 0 //##+++
300 
301 // 2.6.11 (2.6.0+): (prototypes copied from <linux/device.h>)
302  extern struct class_simple *class_simple_create(struct module *owner, char *name);
303  extern void class_simple_destroy(struct class_simple *cs);
304  extern struct class_device *class_simple_device_add(struct class_simple *cs, dev_t dev,
305  struct device *device, const char *fmt, ...)
306  __attribute__((format(printf,4,5)));
307  extern int class_simple_set_hotplug(struct class_simple *,
308  int (*hotplug)(struct class_device *dev, char **envp, int num_envp, char *buffer, int buffer_size));
309  extern void class_simple_device_remove(dev_t dev);
310 
311 
312 // 2.6.13:
313 extern struct class_device *class_device_create(struct class *cls,
314  dev_t devt,
315  struct device *device,
316  char *fmt, ...)
317  __attribute__((format(printf,4,5)));
318 
319 
320 // 2.6.22:
321 extern struct class *class_create(struct module *owner, const char *name);
322 extern void class_destroy(struct class *cls);
323 extern struct class_device *class_device_create(struct class *cls,
324  struct class_device *parent,
325  dev_t devt,
326  struct device *device,
327  const char *fmt, ...)
328  __attribute__((format(printf,5,6)));
329 extern void class_device_destroy(struct class *cls, dev_t devt);
330 
331 
332 // 2.6.25:
333 extern struct class *class_create(struct module *owner, const char *name);
334 extern void class_destroy(struct class *cls);
335 extern struct class_device *class_device_create(struct class *cls,
336  struct class_device *parent,
337  dev_t devt,
338  struct device *device,
339  const char *fmt, ...)
340  __attribute__((format(printf, 5, 6)));
341 extern void class_device_destroy(struct class *cls, dev_t devt);
342 /*
343  * Easy functions for dynamically creating devices on the fly
344  */
345 extern struct device *device_create(struct class *cls, struct device *parent,
346  dev_t devt, const char *fmt, ...)
347  __attribute__((format(printf, 4, 5)));
348 extern void device_destroy(struct class *cls, dev_t devt);
349 
350 
351 // 2.6.27 (2.6.26+):
352 extern struct class * __must_check __class_create(struct module *owner,
353  const char *name,
354  struct lock_class_key *key);
355 /* This is a #define to keep the compiler from merging different
356  * instances of the __key variable */
357 #define class_create(owner, name) \
358 ({ \
359  static struct lock_class_key __key; \
360  __class_create(owner, name, &__key); \
361 })
362 extern void class_destroy(struct class *cls);
363 
364 extern struct device *device_create(struct class *cls, struct device *parent,
365  dev_t devt, void *drvdata,
366  const char *fmt, ...)
367  __attribute__((format(printf, 5, 6)));
368 #define device_create_drvdata device_create
369 extern void device_destroy(struct class *cls, dev_t devt);
370 
371  - class_device_create() and class_device_destroy() have been removed
372  - use device_create() and device_destroy() instead
373 
374 #endif
375 
376 
377 #if !defined( STRUCT_USB_DEVICE_HAS_SERIAL )
378  #define STRUCT_USB_DEVICE_HAS_SERIAL \
379  ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 6, 12 ) )
380 #endif
381 
382 #if !defined( STRUCT_USB_DEVICE_HAS_STATIC_SERIAL )
383  #define STRUCT_USB_DEVICE_HAS_STATIC_SERIAL 0
384  // There seems to be a patch in SuSE Linux 9.1 / 2.6.4-52 which supports this ...
385 #endif
386 
387 
388 #if !defined( VMA_HAS_VM_PGOFF )
389  #define VMA_HAS_VM_PGOFF \
390  ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 6, 0 ) ) //##++ needs verification !!
391 #endif
392 
393 #if !defined( _PCPS_HAS_REMAP_PFN )
394  // remap_pfn_range() and io_remap_pfn_range() have been introduced around 2.6.10
395  // to replace remap_page_range() and io_remap_page_range(), but in the next kernel
396  // versions the old functions are still supported, so it does not seem critical
397  // at which kernel version exactly we start to use the new functions.
398  #define _PCPS_HAS_REMAP_PFN \
399  ( LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 6, 10 ) )
400 #endif
401 
402 //##++++ Something like this could be used in the Makefile:
403 // VMA_PARAM_IN_REMAP=`grep remap_page_range
404 // $PATH_LINUX_INCLUDE/linux/mm.h|grep vma`
405 // if [ -z "$VMA_PARAM_IN_REMAP" ]; then
406 // export REMAP_PAGE_RANGE_PARAM="4"
407 // else
408 // export REMAP_PAGE_RANGE_PARAM="5"
409 // endif
410 
411 
412 #if !defined( NEED_IMINOR )
413  #define NEED_IMINOR \
414  ( LINUX_VERSION_CODE < KERNEL_VERSION( 2, 4, 0 ) ) //##++ needs verification !!
415 #endif
416 
417 #if NEED_IMINOR
418  #define iminor( _d ) MINOR( (_d)->i_rdev )
419 #endif
420 
421 
422 #endif /* _MBG_LX_H */
423 
unsigned long volatile jiffies
void irqreturn_t
Definition: mbg_lx.h:260