staging: unisys: fix formatting in timskmod.h
[cascardo/linux.git] / drivers / staging / unisys / include / timskmod.h
1 /* timskmod.h
2  *
3  * Copyright (C) 2010 - 2013 UNISYS CORPORATION
4  * All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or (at
9  * your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14  * NON INFRINGEMENT.  See the GNU General Public License for more
15  * details.
16  */
17
18 #ifndef __TIMSKMOD_H__
19 #define __TIMSKMOD_H__
20
21 #include <linux/version.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/device.h>
25 #include <linux/kobject.h>
26 #include <linux/sysfs.h>
27 #include <linux/fs.h>
28 #include <linux/string.h>
29 #include <linux/sched.h>
30 #include <linux/spinlock.h>
31 #include <linux/slab.h>
32 #include <linux/errno.h>
33 #include <linux/interrupt.h>
34 #include <linux/sched.h>
35 #include <linux/wait.h>
36 #include <linux/vmalloc.h>
37 #include <linux/proc_fs.h>
38 #include <linux/cdev.h>
39 #include <linux/types.h>
40 #include <asm/irq.h>
41 #include <linux/io.h>
42 #include <asm/dma.h>
43 #include <linux/uaccess.h>
44 #include <linux/list.h>
45 #include <linux/poll.h>
46 /* #define EXPORT_SYMTAB */
47 #include <linux/module.h>
48 #include <linux/moduleparam.h>
49 #include <linux/fcntl.h>
50 #include <linux/aio.h>
51 #include <linux/workqueue.h>
52 #include <linux/kthread.h>
53 #include <linux/seq_file.h>
54 #include <linux/mm.h>
55
56 /* #define DEBUG */
57 #ifndef BOOL
58 #define BOOL    int
59 #endif
60 #define FALSE   0
61 #define TRUE    1
62 #if !defined SUCCESS
63 #define SUCCESS 0
64 #endif
65 #define FAILURE (-1)
66 #define DRIVERNAMEMAX 50
67 #define MIN(a, b)     (((a) < (b)) ? (a) : (b))
68 #define MAX(a, b)     (((a) > (b)) ? (a) : (b))
69 #define STRUCTSEQUAL(x, y) (memcmp(&x, &y, sizeof(x)) == 0)
70 #ifndef HOSTADDRESS
71 #define HOSTADDRESS unsigned long long
72 #endif
73
74 #define LOCKSEM(sem)                   down_interruptible(sem)
75 #define LOCKSEM_UNINTERRUPTIBLE(sem)   down(sem)
76 #define UNLOCKSEM(sem)                 up(sem)
77
78 /** lock read/write semaphore for reading.
79     Note that all read/write semaphores are of the "uninterruptible" variety.
80     @param sem (rw_semaphore *) points to semaphore to lock
81  */
82 #define LOCKREADSEM(sem)               down_read(sem)
83
84 /** unlock read/write semaphore for reading.
85     Note that all read/write semaphores are of the "uninterruptible" variety.
86     @param sem (rw_semaphore *) points to semaphore to unlock
87  */
88 #define UNLOCKREADSEM(sem)             up_read(sem)
89
90 /** lock read/write semaphore for writing.
91     Note that all read/write semaphores are of the "uninterruptible" variety.
92     @param sem (rw_semaphore *) points to semaphore to lock
93  */
94 #define LOCKWRITESEM(sem)              down_write(sem)
95
96 /** unlock read/write semaphore for writing.
97     Note that all read/write semaphores are of the "uninterruptible" variety.
98     @param sem (rw_semaphore *) points to semaphore to unlock
99  */
100 #define UNLOCKWRITESEM(sem)            up_write(sem)
101
102 #ifdef ENABLE_RETURN_TRACE
103 #define RETTRACE(x)                                            \
104         do {                                                   \
105                 if (1) {                                       \
106                         INFODRV("RET 0x%lx in %s",             \
107                                 (ulong)(x), __func__);     \
108                 }                                          \
109         } while (0)
110 #else
111 #define RETTRACE(x)
112 #endif
113
114 /** Try to evaulate the provided expression, and do a RETINT(x) iff
115  *  the expression evaluates to < 0.
116  *  @param x the expression to try
117  */
118 #define ASSERT(cond)                                           \
119         do { if (!(cond))                                      \
120                         HUHDRV("ASSERT failed - %s",           \
121                                __stringify(cond));             \
122         } while (0)
123
124 #define sizeofmember(TYPE, MEMBER) (sizeof(((TYPE *)0)->MEMBER))
125 /** "Covered quotient" function */
126 #define COVQ(v, d)  (((v) + (d) - 1) / (d))
127 #define SWAPPOINTERS(p1, p2)                            \
128         do {                                            \
129                 void *SWAPPOINTERS_TEMP = (void *)p1;   \
130                 (void *)(p1) = (void *)(p2);            \
131                 (void *)(p2) = SWAPPOINTERS_TEMP;       \
132         } while (0)
133
134 /**
135  *  @addtogroup driverlogging
136  *  @{
137  */
138
139 #define PRINTKDRV(fmt, args...) LOGINF(fmt, ## args)
140 #define TBDDRV(fmt, args...)    LOGERR(fmt, ## args)
141 #define HUHDRV(fmt, args...)    LOGERR(fmt, ## args)
142 #define ERRDRV(fmt, args...)    LOGERR(fmt, ## args)
143 #define WARNDRV(fmt, args...)   LOGWRN(fmt, ## args)
144 #define SECUREDRV(fmt, args...) LOGWRN(fmt, ## args)
145 #define INFODRV(fmt, args...)   LOGINF(fmt, ## args)
146 #define DEBUGDRV(fmt, args...)  DBGINF(fmt, ## args)
147
148 #define PRINTKDEV(devname, fmt, args...)  LOGINFDEV(devname, fmt, ## args)
149 #define TBDDEV(devname, fmt, args...)     LOGERRDEV(devname, fmt, ## args)
150 #define HUHDEV(devname, fmt, args...)     LOGERRDEV(devname, fmt, ## args)
151 #define ERRDEV(devname, fmt, args...)     LOGERRDEV(devname, fmt, ## args)
152 #define ERRDEVX(devno, fmt, args...)      LOGERRDEVX(devno, fmt, ## args)
153 #define WARNDEV(devname, fmt, args...)    LOGWRNDEV(devname, fmt, ## args)
154 #define SECUREDEV(devname, fmt, args...)  LOGWRNDEV(devname, fmt, ## args)
155 #define INFODEV(devname, fmt, args...)    LOGINFDEV(devname, fmt, ## args)
156 #define INFODEVX(devno, fmt, args...)     LOGINFDEVX(devno, fmt, ## args)
157 #define DEBUGDEV(devname, fmt, args...)   DBGINFDEV(devname, fmt, ## args)
158
159
160 /* @} */
161
162 /** Verifies the consistency of your PRIVATEDEVICEDATA structure using
163  *  conventional "signature" fields:
164  *  <p>
165  *  - sig1 should contain the size of the structure
166  *  - sig2 should contain a pointer to the beginning of the structure
167  */
168 #define DDLOOKSVALID(dd)                                 \
169                 ((dd != NULL)                             &&    \
170                  ((dd)->sig1 == sizeof(PRIVATEDEVICEDATA)) &&   \
171                  ((dd)->sig2 == dd))
172
173 /** Verifies the consistency of your PRIVATEFILEDATA structure using
174  *  conventional "signature" fields:
175  *  <p>
176  *  - sig1 should contain the size of the structure
177  *  - sig2 should contain a pointer to the beginning of the structure
178  */
179 #define FDLOOKSVALID(fd)                               \
180         ((fd != NULL)                           &&     \
181          ((fd)->sig1 == sizeof(PRIVATEFILEDATA)) &&    \
182          ((fd)->sig2 == fd))
183
184 /** Locks dd->lockDev if you havn't already locked it */
185 #define LOCKDEV(dd)                                                    \
186         {                                                              \
187                 if (!lockedDev) {                                      \
188                         spin_lock(&dd->lockDev);                       \
189                         lockedDev = TRUE;                              \
190                 }                                                      \
191         }
192
193 /** Unlocks dd->lockDev if you previously locked it */
194 #define UNLOCKDEV(dd)                                                  \
195         {                                                              \
196                 if (lockedDev) {                                       \
197                         spin_unlock(&dd->lockDev);                     \
198                         lockedDev = FALSE;                             \
199                 }                                                      \
200         }
201
202 /** Locks dd->lockDevISR if you havn't already locked it */
203 #define LOCKDEVISR(dd)                                                 \
204         {                                                              \
205                 if (!lockedDevISR) {                                   \
206                         spin_lock_irqsave(&dd->lockDevISR, flags);     \
207                         lockedDevISR = TRUE;                           \
208                 }                                                      \
209         }
210
211 /** Unlocks dd->lockDevISR if you previously locked it */
212 #define UNLOCKDEVISR(dd)                                                \
213         {                                                               \
214                 if (lockedDevISR) {                                     \
215                         spin_unlock_irqrestore(&dd->lockDevISR, flags); \
216                         lockedDevISR = FALSE;                           \
217                 }                                                       \
218         }
219
220 /** Locks LockGlobalISR if you havn't already locked it */
221 #define LOCKGLOBALISR                                                  \
222         {                                                              \
223                 if (!lockedGlobalISR) {                                \
224                         spin_lock_irqsave(&LockGlobalISR, flags);      \
225                         lockedGlobalISR = TRUE;                        \
226                 }                                                      \
227         }
228
229 /** Unlocks LockGlobalISR if you previously locked it */
230 #define UNLOCKGLOBALISR                                                \
231         {                                                              \
232                 if (lockedGlobalISR) {                                 \
233                         spin_unlock_irqrestore(&LockGlobalISR, flags); \
234                         lockedGlobalISR = FALSE;                       \
235                 }                                                      \
236         }
237
238 /** Locks LockGlobal if you havn't already locked it */
239 #define LOCKGLOBAL                                                     \
240         {                                                              \
241                 if (!lockedGlobal) {                                   \
242                         spin_lock(&LockGlobal);                        \
243                         lockedGlobal = TRUE;                           \
244                 }                                                      \
245         }
246
247 /** Unlocks LockGlobal if you previously locked it */
248 #define UNLOCKGLOBAL                                                   \
249         {                                                              \
250                 if (lockedGlobal) {                                    \
251                         spin_unlock(&LockGlobal);                      \
252                         lockedGlobal = FALSE;                          \
253                 }                                                      \
254         }
255
256 /** Use this at the beginning of functions where you intend to
257  *  use #LOCKDEV/#UNLOCKDEV, #LOCKDEVISR/#UNLOCKDEVISR,
258  *  #LOCKGLOBAL/#UNLOCKGLOBAL, #LOCKGLOBALISR/#UNLOCKGLOBALISR.
259  *
260  *  Note that __attribute__((unused)) is how you tell GNU C to suppress
261  *  any warning messages about the variable being unused.
262  */
263 #define LOCKPREAMBLE                                                    \
264         ulong flags __attribute__((unused)) = 0;                        \
265         BOOL lockedDev __attribute__((unused)) = FALSE;                 \
266         BOOL lockedDevISR __attribute__((unused)) = FALSE;              \
267         BOOL lockedGlobal __attribute__((unused)) = FALSE;              \
268         BOOL lockedGlobalISR __attribute__((unused)) = FALSE
269
270
271
272 /** Sleep for an indicated number of seconds (for use in kernel mode).
273  *  @param x the number of seconds to sleep.
274  */
275 #define SLEEP(x)                                             \
276         do { current->state = TASK_INTERRUPTIBLE;            \
277                 schedule_timeout((x)*HZ);                    \
278         } while (0)
279
280 /** Sleep for an indicated number of jiffies (for use in kernel mode).
281  *  @param x the number of jiffies to sleep.
282  */
283 #define SLEEPJIFFIES(x)                                             \
284         do { current->state = TASK_INTERRUPTIBLE;                   \
285                 schedule_timeout(x);                                \
286         } while (0)
287
288 #ifndef max
289 #define max(a, b) (((a) > (b)) ? (a):(b))
290 #endif
291
292 static inline struct cdev *cdev_alloc_init(struct module *owner,
293                                            const struct file_operations *fops)
294 {
295         struct cdev *cdev = NULL;
296
297         cdev = cdev_alloc();
298         if (!cdev)
299                 return NULL;
300         cdev->ops = fops;
301         cdev->owner = owner;
302
303         /* Note that the memory allocated for cdev will be deallocated
304          * when the usage count drops to 0, because it is controlled
305          * by a kobject of type ktype_cdev_dynamic.  (This
306          * deallocation could very well happen outside of our kernel
307          * module, like via the cdev_put in __fput() for example.)
308          */
309         return cdev;
310 }
311
312 #include "timskmodutils.h"
313
314 #endif