Merge tag 'firewire-fix' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394...
[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 typedef long VMMIO;  /**< Virtual MMIO address (returned from ioremap), which
75     *   is a virtual address pointer to a memory-mapped region.
76     *   These are declared as "long" instead of u32* to force you to
77     *   use readb()/writeb()/memcpy_fromio()/etc to access them.
78     *   (On x86 we could probably get away with treating them as
79     *   pointers.)
80     */
81 typedef long VMMIO8; /**< #VMMIO pointing to  8-bit data */
82 typedef long VMMIO16;/**< #VMMIO pointing to 16-bit data */
83 typedef long VMMIO32;/**< #VMMIO pointing to 32-bit data */
84
85 #define LOCKSEM(sem)                   down_interruptible(sem)
86 #define LOCKSEM_UNINTERRUPTIBLE(sem)   down(sem)
87 #define UNLOCKSEM(sem)                 up(sem)
88
89 /** lock read/write semaphore for reading.
90     Note that all read/write semaphores are of the "uninterruptible" variety.
91     @param sem (rw_semaphore *) points to semaphore to lock
92  */
93 #define LOCKREADSEM(sem)               down_read(sem)
94
95 /** unlock read/write semaphore for reading.
96     Note that all read/write semaphores are of the "uninterruptible" variety.
97     @param sem (rw_semaphore *) points to semaphore to unlock
98  */
99 #define UNLOCKREADSEM(sem)             up_read(sem)
100
101 /** lock read/write semaphore for writing.
102     Note that all read/write semaphores are of the "uninterruptible" variety.
103     @param sem (rw_semaphore *) points to semaphore to lock
104  */
105 #define LOCKWRITESEM(sem)              down_write(sem)
106
107 /** unlock read/write semaphore for writing.
108     Note that all read/write semaphores are of the "uninterruptible" variety.
109     @param sem (rw_semaphore *) points to semaphore to unlock
110  */
111 #define UNLOCKWRITESEM(sem)            up_write(sem)
112
113 #ifdef ENABLE_RETURN_TRACE
114 #define RETTRACE(x)                                            \
115         do {                                                   \
116                 if (1) {                                       \
117                         INFODRV("RET 0x%lx in %s",             \
118                                 (ulong)(x), __func__);     \
119                 }                                          \
120         } while (0)
121 #else
122 #define RETTRACE(x)
123 #endif
124
125 /** Try to evaulate the provided expression, and do a RETINT(x) iff
126  *  the expression evaluates to < 0.
127  *  @param x the expression to try
128  */
129 #define ASSERT(cond)                                           \
130         do { if (!(cond))                                      \
131                         HUHDRV("ASSERT failed - %s",           \
132                                __stringify(cond));             \
133         } while (0)
134
135 #define sizeofmember(TYPE, MEMBER) (sizeof(((TYPE *)0)->MEMBER))
136 /** "Covered quotient" function */
137 #define COVQ(v, d)  (((v) + (d) - 1) / (d))
138 #define SWAPPOINTERS(p1, p2)                            \
139         do {                                            \
140                 void *SWAPPOINTERS_TEMP = (void *)p1;   \
141                 (void *)(p1) = (void *)(p2);            \
142                 (void *)(p2) = SWAPPOINTERS_TEMP;       \
143         } while (0)
144
145 /**
146  *  @addtogroup driverlogging
147  *  @{
148  */
149
150 #define PRINTKDRV(fmt, args...) LOGINF(fmt, ## args)
151 #define TBDDRV(fmt, args...)    LOGERR(fmt, ## args)
152 #define HUHDRV(fmt, args...)    LOGERR(fmt, ## args)
153 #define ERRDRV(fmt, args...)    LOGERR(fmt, ## args)
154 #define WARNDRV(fmt, args...)   LOGWRN(fmt, ## args)
155 #define SECUREDRV(fmt, args...) LOGWRN(fmt, ## args)
156 #define INFODRV(fmt, args...)   LOGINF(fmt, ## args)
157 #define DEBUGDRV(fmt, args...)  DBGINF(fmt, ## args)
158
159 #define PRINTKDEV(devname, fmt, args...)  LOGINFDEV(devname, fmt, ## args)
160 #define TBDDEV(devname, fmt, args...)     LOGERRDEV(devname, fmt, ## args)
161 #define HUHDEV(devname, fmt, args...)     LOGERRDEV(devname, fmt, ## args)
162 #define ERRDEV(devname, fmt, args...)     LOGERRDEV(devname, fmt, ## args)
163 #define ERRDEVX(devno, fmt, args...)      LOGERRDEVX(devno, fmt, ## args)
164 #define WARNDEV(devname, fmt, args...)    LOGWRNDEV(devname, fmt, ## args)
165 #define SECUREDEV(devname, fmt, args...)  LOGWRNDEV(devname, fmt, ## args)
166 #define INFODEV(devname, fmt, args...)    LOGINFDEV(devname, fmt, ## args)
167 #define INFODEVX(devno, fmt, args...)     LOGINFDEVX(devno, fmt, ## args)
168 #define DEBUGDEV(devname, fmt, args...)   DBGINFDEV(devname, fmt, ## args)
169
170
171 /* @} */
172
173 /** Verifies the consistency of your PRIVATEDEVICEDATA 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 DDLOOKSVALID(dd)                                 \
180                 ((dd != NULL)                             &&    \
181                  ((dd)->sig1 == sizeof(PRIVATEDEVICEDATA)) &&   \
182                  ((dd)->sig2 == dd))
183
184 /** Verifies the consistency of your PRIVATEFILEDATA structure using
185  *  conventional "signature" fields:
186  *  <p>
187  *  - sig1 should contain the size of the structure
188  *  - sig2 should contain a pointer to the beginning of the structure
189  */
190 #define FDLOOKSVALID(fd)                               \
191         ((fd != NULL)                           &&     \
192          ((fd)->sig1 == sizeof(PRIVATEFILEDATA)) &&    \
193          ((fd)->sig2 == fd))
194
195 /** Locks dd->lockDev if you havn't already locked it */
196 #define LOCKDEV(dd)                                                    \
197         {                                                              \
198                 if (!lockedDev) {                                      \
199                         spin_lock(&dd->lockDev);                       \
200                         lockedDev = TRUE;                              \
201                 }                                                      \
202         }
203
204 /** Unlocks dd->lockDev if you previously locked it */
205 #define UNLOCKDEV(dd)                                                  \
206         {                                                              \
207                 if (lockedDev) {                                       \
208                         spin_unlock(&dd->lockDev);                     \
209                         lockedDev = FALSE;                             \
210                 }                                                      \
211         }
212
213 /** Locks dd->lockDevISR if you havn't already locked it */
214 #define LOCKDEVISR(dd)                                                 \
215         {                                                              \
216                 if (!lockedDevISR) {                                   \
217                         spin_lock_irqsave(&dd->lockDevISR, flags);     \
218                         lockedDevISR = TRUE;                           \
219                 }                                                      \
220         }
221
222 /** Unlocks dd->lockDevISR if you previously locked it */
223 #define UNLOCKDEVISR(dd)                                                \
224         {                                                               \
225                 if (lockedDevISR) {                                     \
226                         spin_unlock_irqrestore(&dd->lockDevISR, flags); \
227                         lockedDevISR = FALSE;                           \
228                 }                                                       \
229         }
230
231 /** Locks LockGlobalISR if you havn't already locked it */
232 #define LOCKGLOBALISR                                                  \
233         {                                                              \
234                 if (!lockedGlobalISR) {                                \
235                         spin_lock_irqsave(&LockGlobalISR, flags);      \
236                         lockedGlobalISR = TRUE;                        \
237                 }                                                      \
238         }
239
240 /** Unlocks LockGlobalISR if you previously locked it */
241 #define UNLOCKGLOBALISR                                                \
242         {                                                              \
243                 if (lockedGlobalISR) {                                 \
244                         spin_unlock_irqrestore(&LockGlobalISR, flags); \
245                         lockedGlobalISR = FALSE;                       \
246                 }                                                      \
247         }
248
249 /** Locks LockGlobal if you havn't already locked it */
250 #define LOCKGLOBAL                                                     \
251         {                                                              \
252                 if (!lockedGlobal) {                                   \
253                         spin_lock(&LockGlobal);                        \
254                         lockedGlobal = TRUE;                           \
255                 }                                                      \
256         }
257
258 /** Unlocks LockGlobal if you previously locked it */
259 #define UNLOCKGLOBAL                                                   \
260         {                                                              \
261                 if (lockedGlobal) {                                    \
262                         spin_unlock(&LockGlobal);                      \
263                         lockedGlobal = FALSE;                          \
264                 }                                                      \
265         }
266
267 /** Use this at the beginning of functions where you intend to
268  *  use #LOCKDEV/#UNLOCKDEV, #LOCKDEVISR/#UNLOCKDEVISR,
269  *  #LOCKGLOBAL/#UNLOCKGLOBAL, #LOCKGLOBALISR/#UNLOCKGLOBALISR.
270  *
271  *  Note that __attribute__((unused)) is how you tell GNU C to suppress
272  *  any warning messages about the variable being unused.
273  */
274 #define LOCKPREAMBLE                                                    \
275         ulong flags __attribute__((unused)) = 0;                        \
276         BOOL lockedDev __attribute__((unused)) = FALSE;                 \
277         BOOL lockedDevISR __attribute__((unused)) = FALSE;              \
278         BOOL lockedGlobal __attribute__((unused)) = FALSE;              \
279         BOOL lockedGlobalISR __attribute__((unused)) = FALSE
280
281
282
283 /** Sleep for an indicated number of seconds (for use in kernel mode).
284  *  @param x the number of seconds to sleep.
285  */
286 #define SLEEP(x)                                             \
287         do { current->state = TASK_INTERRUPTIBLE;            \
288                 schedule_timeout((x)*HZ);                    \
289         } while (0)
290
291 /** Sleep for an indicated number of jiffies (for use in kernel mode).
292  *  @param x the number of jiffies to sleep.
293  */
294 #define SLEEPJIFFIES(x)                                             \
295         do { current->state = TASK_INTERRUPTIBLE;                   \
296                 schedule_timeout(x);                                \
297         } while (0)
298
299 #ifndef max
300 #define max(a, b) (((a) > (b)) ? (a):(b))
301 #endif
302
303 static inline struct cdev *cdev_alloc_init(struct module *owner,
304                                            const struct file_operations *fops)
305 {
306         struct cdev *cdev = NULL;
307         cdev = cdev_alloc();
308         if (!cdev)
309                 return NULL;
310         cdev->ops = fops;
311         cdev->owner = owner;
312
313         /* Note that the memory allocated for cdev will be deallocated
314          * when the usage count drops to 0, because it is controlled
315          * by a kobject of type ktype_cdev_dynamic.  (This
316          * deallocation could very well happen outside of our kernel
317          * module, like via the cdev_put in __fput() for example.)
318          */
319         return cdev;
320 }
321
322 #include "timskmodutils.h"
323
324 #endif