Staging: unisys: include: Remove unused macros from timskmod.h
[firefly-linux-kernel-4.4.55.git] / drivers / staging / unisys / include / timskmod.h
1 /* timskmod.h
2  *
3  * Copyright � 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 /** return from a void function, using a common exit point "Away" */
126 #define RETVOID    do { RETTRACE(0); goto Away; } while (0)
127 /** return from an int function, using a common exit point "Away"
128  *  @param x the value to return
129  */
130 #define RETINT(x)  do { rc = (x); RETTRACE(x); goto Away; } while (0)
131 /** return from a void* function, using a common exit point "Away"
132  *  @param x the value to return
133  */
134 #define RETPTR(x)  do { rc = (x); RETTRACE(x); goto Away; } while (0)
135 /** return from a BOOL function, using a common exit point "Away"
136  *  @param x the value to return
137  */
138 #define RETBOOL(x) do { rc = (x); RETTRACE(x); goto Away; } while (0)
139 /** Given a typedef/struct/union and a member field name,
140  *  return the number of bytes occupied by that field.
141  *  @param TYPE     the typedef name, or "struct xx" or "union xx"
142  *  @param MEMBER   the name of the member field whose size is to be determined
143  *  @return         the size of the field in bytes
144  */
145 #define FAIL(msg, status) do {          \
146                 ERRDRV("'%s'"                                         \
147                        ": error (status=%d)\n",                       \
148                        msg, status);                                  \
149                 RETINT(status);                                       \
150         } while (0)
151 #define FAIL_WPOSTCODE_1(msg, status, EVENT_PC) do {          \
152                 ERRDRV("'%s'"                                         \
153                        ": error (status=%d)\n",                       \
154                        msg, status);                                    \
155                 POSTCODE_LINUX_2(EVENT_PC, DIAG_SEVERITY_ERR);          \
156                 RETINT(status);                                         \
157         } while (0)
158 #define FAIL_WPOSTCODE_2(msg, status, EVENT_PC, pcval32bit) do {          \
159                 ERRDRV("'%s'"                                           \
160                        ": error (status=%d)\n",                         \
161                        msg, status);                                    \
162                 POSTCODE_LINUX_3(EVENT_PC, pcval32bit, DIAG_SEVERITY_ERR); \
163                 RETINT(status);                                         \
164         } while (0)
165 #define FAIL_WPOSTCODE_3(msg, status, EVENT_PC, pcval16bit1, pcval16bit2) \
166         do {                                                            \
167                 ERRDRV("'%s'"                                           \
168                        ": error (status=%d)\n",                         \
169                        msg, status);                                    \
170                 POSTCODE_LINUX_4(EVENT_PC, pcval16bit1, pcval16bit2,    \
171                                  DIAG_SEVERITY_ERR);                    \
172                 RETINT(status);                                         \
173         } while (0)
174 /** Try to evaulate the provided expression, and do a RETINT(x) iff
175  *  the expression evaluates to < 0.
176  *  @param x the expression to try
177  */
178 #define TRY(x) do { int status = (x);                          \
179                 if (status < 0)                                \
180                         FAIL(__stringify(x), status);          \
181         } while (0)
182
183 #define TRY_WPOSTCODE_1(x, EVENT_PC) do { \
184                 int status = (x);         \
185                 if (status < 0)                                         \
186                         FAIL_WPOSTCODE_1(__stringify(x), status, EVENT_PC); \
187         } while (0)
188
189 #define TRY_WPOSTCODE_2(x, EVENT_PC, pcval32bit) do { \
190                 int status = (x);                     \
191                 if (status < 0)                                         \
192                         FAIL_WPOSTCODE_2(__stringify(x), status, EVENT_PC, \
193                                          pcval32bit);                   \
194         } while (0)
195
196 #define ASSERT(cond)                                           \
197         do { if (!(cond))                                      \
198                         HUHDRV("ASSERT failed - %s",           \
199                                __stringify(cond));             \
200         } while (0)
201
202 #define sizeofmember(TYPE, MEMBER) (sizeof(((TYPE *)0)->MEMBER))
203 /** "Covered quotient" function */
204 #define COVQ(v, d)  (((v) + (d) - 1) / (d))
205 #define SWAPPOINTERS(p1, p2)                            \
206         do {                                            \
207                 void *SWAPPOINTERS_TEMP = (void *)p1;   \
208                 (void *)(p1) = (void *)(p2);            \
209                 (void *)(p2) = SWAPPOINTERS_TEMP;       \
210         } while (0)
211
212 /**
213  *  @addtogroup driverlogging
214  *  @{
215  */
216
217 #define PRINTKDRV(fmt, args...) LOGINF(fmt, ## args)
218 #define TBDDRV(fmt, args...)    LOGERR(fmt, ## args)
219 #define HUHDRV(fmt, args...)    LOGERR(fmt, ## args)
220 #define ERRDRV(fmt, args...)    LOGERR(fmt, ## args)
221 #define WARNDRV(fmt, args...)   LOGWRN(fmt, ## args)
222 #define SECUREDRV(fmt, args...) LOGWRN(fmt, ## args)
223 #define INFODRV(fmt, args...)   LOGINF(fmt, ## args)
224 #define DEBUGDRV(fmt, args...)  DBGINF(fmt, ## args)
225
226 #define PRINTKDEV(devname, fmt, args...)  LOGINFDEV(devname, fmt, ## args)
227 #define TBDDEV(devname, fmt, args...)     LOGERRDEV(devname, fmt, ## args)
228 #define HUHDEV(devname, fmt, args...)     LOGERRDEV(devname, fmt, ## args)
229 #define ERRDEV(devname, fmt, args...)     LOGERRDEV(devname, fmt, ## args)
230 #define ERRDEVX(devno, fmt, args...)      LOGERRDEVX(devno, fmt, ## args)
231 #define WARNDEV(devname, fmt, args...)    LOGWRNDEV(devname, fmt, ## args)
232 #define SECUREDEV(devname, fmt, args...)  LOGWRNDEV(devname, fmt, ## args)
233 #define INFODEV(devname, fmt, args...)    LOGINFDEV(devname, fmt, ## args)
234 #define INFODEVX(devno, fmt, args...)     LOGINFDEVX(devno, fmt, ## args)
235 #define DEBUGDEV(devname, fmt, args...)   DBGINFDEV(devname, fmt, ## args)
236
237
238 /* @} */
239
240 /** Used to add a single line to the /proc filesystem buffer */
241 #define ADDPROCLINE(buf, bufsize, line, linelen, totallen) \
242         {                                                  \
243                 if ((totallen) + (linelen) >= bufsize)     \
244                         RETINT(totallen);                  \
245                 if (linelen > 0) {                         \
246                         strcat(buf, line);                 \
247                         totallen += linelen;               \
248                 }                                          \
249         }
250
251
252
253 /** Verifies the consistency of your PRIVATEDEVICEDATA structure using
254  *  conventional "signature" fields:
255  *  <p>
256  *  - sig1 should contain the size of the structure
257  *  - sig2 should contain a pointer to the beginning of the structure
258  */
259 #define DDLOOKSVALID(dd)                                 \
260                 ((dd != NULL)                             &&    \
261                  ((dd)->sig1 == sizeof(PRIVATEDEVICEDATA)) &&   \
262                  ((dd)->sig2 == dd))
263
264 /** Verifies the consistency of your PRIVATEFILEDATA structure using
265  *  conventional "signature" fields:
266  *  <p>
267  *  - sig1 should contain the size of the structure
268  *  - sig2 should contain a pointer to the beginning of the structure
269  */
270 #define FDLOOKSVALID(fd)                               \
271         ((fd != NULL)                           &&     \
272          ((fd)->sig1 == sizeof(PRIVATEFILEDATA)) &&    \
273          ((fd)->sig2 == fd))
274
275 /** Verifies the consistency of a PRIVATEDEVICEDATA structure and reacts
276  *  if necessary
277  */
278 #define CHKDDX(dd, x) (                                    \
279                         if (!DDLOOKSVALID((dd))) {         \
280                                 PRINTKDRV("bad device structure");      \
281                                 RETINT(x);                              \
282                         })
283
284 /** Verifies the consistency of a PRIVATEDEVICEDATA structure and reacts
285  *  if necessary
286  */
287 #define CHKDD(dd) (                                                     \
288                         if (!DDLOOKSVALID(dd)) {                        \
289                                 PRINTKDRV("bad device structure");      \
290                                 RETVOID;                                \
291                         })
292
293 /** Verifies the consistency of a PRIVATEFILEDATA structure and reacts
294  *  if necessary
295  */
296 #define CHKFDX(fd, x) (                                    \
297                 if (!FDLOOKSVALID(fd)) {                   \
298                         PRINTKDRV("bad file structure");   \
299                         RETINT(x);                         \
300                 })
301
302 /** Verifies the consistency of a PRIVATEFILEDATA structure and reacts
303  *  if necessary
304  */
305 #define CHKFD(fd) (                                       \
306                 if (!FDLOOKSVALID(fd)) {                  \
307                         PRINTKDRV("bad file structure");  \
308                         RETVOID;                          \
309                 })
310
311 /** Locks dd->lockDev if you havn't already locked it */
312 #define LOCKDEV(dd)                                                    \
313         {                                                              \
314                 if (!lockedDev) {                                      \
315                         spin_lock(&dd->lockDev);                       \
316                         lockedDev = TRUE;                              \
317                 }                                                      \
318         }
319
320 /** Unlocks dd->lockDev if you previously locked it */
321 #define UNLOCKDEV(dd)                                                  \
322         {                                                              \
323                 if (lockedDev) {                                       \
324                         spin_unlock(&dd->lockDev);                     \
325                         lockedDev = FALSE;                             \
326                 }                                                      \
327         }
328
329 /** Locks dd->lockDevISR if you havn't already locked it */
330 #define LOCKDEVISR(dd)                                                 \
331         {                                                              \
332                 if (!lockedDevISR) {                                   \
333                         spin_lock_irqsave(&dd->lockDevISR, flags);     \
334                         lockedDevISR = TRUE;                           \
335                 }                                                      \
336         }
337
338 /** Unlocks dd->lockDevISR if you previously locked it */
339 #define UNLOCKDEVISR(dd)                                                \
340         {                                                               \
341                 if (lockedDevISR) {                                     \
342                         spin_unlock_irqrestore(&dd->lockDevISR, flags); \
343                         lockedDevISR = FALSE;                           \
344                 }                                                       \
345         }
346
347 /** Locks LockGlobalISR if you havn't already locked it */
348 #define LOCKGLOBALISR                                                  \
349         {                                                              \
350                 if (!lockedGlobalISR) {                                \
351                         spin_lock_irqsave(&LockGlobalISR, flags);      \
352                         lockedGlobalISR = TRUE;                        \
353                 }                                                      \
354         }
355
356 /** Unlocks LockGlobalISR if you previously locked it */
357 #define UNLOCKGLOBALISR                                                \
358         {                                                              \
359                 if (lockedGlobalISR) {                                 \
360                         spin_unlock_irqrestore(&LockGlobalISR, flags); \
361                         lockedGlobalISR = FALSE;                       \
362                 }                                                      \
363         }
364
365 /** Locks LockGlobal if you havn't already locked it */
366 #define LOCKGLOBAL                                                     \
367         {                                                              \
368                 if (!lockedGlobal) {                                   \
369                         spin_lock(&LockGlobal);                        \
370                         lockedGlobal = TRUE;                           \
371                 }                                                      \
372         }
373
374 /** Unlocks LockGlobal if you previously locked it */
375 #define UNLOCKGLOBAL                                                   \
376         {                                                              \
377                 if (lockedGlobal) {                                    \
378                         spin_unlock(&LockGlobal);                      \
379                         lockedGlobal = FALSE;                          \
380                 }                                                      \
381         }
382
383 /** Use this at the beginning of functions where you intend to
384  *  use #LOCKDEV/#UNLOCKDEV, #LOCKDEVISR/#UNLOCKDEVISR,
385  *  #LOCKGLOBAL/#UNLOCKGLOBAL, #LOCKGLOBALISR/#UNLOCKGLOBALISR.
386  *
387  *  Note that __attribute__((unused)) is how you tell GNU C to suppress
388  *  any warning messages about the variable being unused.
389  */
390 #define LOCKPREAMBLE                                                    \
391         ulong flags __attribute__((unused)) = 0;                        \
392         BOOL lockedDev __attribute__((unused)) = FALSE;                 \
393         BOOL lockedDevISR __attribute__((unused)) = FALSE;              \
394         BOOL lockedGlobal __attribute__((unused)) = FALSE;              \
395         BOOL lockedGlobalISR __attribute__((unused)) = FALSE
396
397
398
399 /** Sleep for an indicated number of seconds (for use in kernel mode).
400  *  @param x the number of seconds to sleep.
401  */
402 #define SLEEP(x)                                             \
403         do { current->state = TASK_INTERRUPTIBLE;            \
404                 schedule_timeout((x)*HZ);                    \
405         } while (0)
406
407 /** Sleep for an indicated number of jiffies (for use in kernel mode).
408  *  @param x the number of jiffies to sleep.
409  */
410 #define SLEEPJIFFIES(x)                                             \
411         do { current->state = TASK_INTERRUPTIBLE;                   \
412                 schedule_timeout(x);                                \
413         } while (0)
414
415 #ifndef max
416 #define max(a, b) (((a) > (b)) ? (a):(b))
417 #endif
418
419 static inline struct cdev *cdev_alloc_init(struct module *owner,
420                                            const struct file_operations *fops)
421 {
422         struct cdev *cdev = NULL;
423         cdev = cdev_alloc();
424         if (!cdev)
425                 return NULL;
426         cdev->ops = fops;
427         cdev->owner = owner;
428
429         /* Note that the memory allocated for cdev will be deallocated
430          * when the usage count drops to 0, because it is controlled
431          * by a kobject of type ktype_cdev_dynamic.  (This
432          * deallocation could very well happen outside of our kernel
433          * module, like via the cdev_put in __fput() for example.)
434          */
435         return cdev;
436 }
437
438 #include "timskmodutils.h"
439
440 #endif