3 * Copyright � 2010 - 2013 UNISYS CORPORATION
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.
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
18 #ifndef __TIMSKMOD_H__
19 #define __TIMSKMOD_H__
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>
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>
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>
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)
71 #define HOSTADDRESS unsigned long long
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
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 */
85 #define LOCKSEM(sem) down_interruptible(sem)
86 #define LOCKSEM_UNINTERRUPTIBLE(sem) down(sem)
87 #define UNLOCKSEM(sem) up(sem)
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
93 #define LOCKREADSEM(sem) down_read(sem)
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
99 #define UNLOCKREADSEM(sem) up_read(sem)
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
105 #define LOCKWRITESEM(sem) down_write(sem)
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
111 #define UNLOCKWRITESEM(sem) up_write(sem)
113 #ifdef ENABLE_RETURN_TRACE
114 #define RETTRACE(x) \
117 INFODRV("RET 0x%lx in %s", \
118 (ulong)(x), __func__); \
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
129 #define ASSERT(cond) \
131 HUHDRV("ASSERT failed - %s", \
132 __stringify(cond)); \
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) \
140 void *SWAPPOINTERS_TEMP = (void *)p1; \
141 (void *)(p1) = (void *)(p2); \
142 (void *)(p2) = SWAPPOINTERS_TEMP; \
146 * @addtogroup driverlogging
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)
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)
173 /** Verifies the consistency of your PRIVATEDEVICEDATA structure using
174 * conventional "signature" fields:
176 * - sig1 should contain the size of the structure
177 * - sig2 should contain a pointer to the beginning of the structure
179 #define DDLOOKSVALID(dd) \
181 ((dd)->sig1 == sizeof(PRIVATEDEVICEDATA)) && \
184 /** Verifies the consistency of your PRIVATEFILEDATA structure using
185 * conventional "signature" fields:
187 * - sig1 should contain the size of the structure
188 * - sig2 should contain a pointer to the beginning of the structure
190 #define FDLOOKSVALID(fd) \
192 ((fd)->sig1 == sizeof(PRIVATEFILEDATA)) && \
195 /** Locks dd->lockDev if you havn't already locked it */
196 #define LOCKDEV(dd) \
199 spin_lock(&dd->lockDev); \
204 /** Unlocks dd->lockDev if you previously locked it */
205 #define UNLOCKDEV(dd) \
208 spin_unlock(&dd->lockDev); \
213 /** Locks dd->lockDevISR if you havn't already locked it */
214 #define LOCKDEVISR(dd) \
216 if (!lockedDevISR) { \
217 spin_lock_irqsave(&dd->lockDevISR, flags); \
218 lockedDevISR = TRUE; \
222 /** Unlocks dd->lockDevISR if you previously locked it */
223 #define UNLOCKDEVISR(dd) \
225 if (lockedDevISR) { \
226 spin_unlock_irqrestore(&dd->lockDevISR, flags); \
227 lockedDevISR = FALSE; \
231 /** Locks LockGlobalISR if you havn't already locked it */
232 #define LOCKGLOBALISR \
234 if (!lockedGlobalISR) { \
235 spin_lock_irqsave(&LockGlobalISR, flags); \
236 lockedGlobalISR = TRUE; \
240 /** Unlocks LockGlobalISR if you previously locked it */
241 #define UNLOCKGLOBALISR \
243 if (lockedGlobalISR) { \
244 spin_unlock_irqrestore(&LockGlobalISR, flags); \
245 lockedGlobalISR = FALSE; \
249 /** Locks LockGlobal if you havn't already locked it */
252 if (!lockedGlobal) { \
253 spin_lock(&LockGlobal); \
254 lockedGlobal = TRUE; \
258 /** Unlocks LockGlobal if you previously locked it */
259 #define UNLOCKGLOBAL \
261 if (lockedGlobal) { \
262 spin_unlock(&LockGlobal); \
263 lockedGlobal = FALSE; \
267 /** Use this at the beginning of functions where you intend to
268 * use #LOCKDEV/#UNLOCKDEV, #LOCKDEVISR/#UNLOCKDEVISR,
269 * #LOCKGLOBAL/#UNLOCKGLOBAL, #LOCKGLOBALISR/#UNLOCKGLOBALISR.
271 * Note that __attribute__((unused)) is how you tell GNU C to suppress
272 * any warning messages about the variable being unused.
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
283 /** Sleep for an indicated number of seconds (for use in kernel mode).
284 * @param x the number of seconds to sleep.
287 do { current->state = TASK_INTERRUPTIBLE; \
288 schedule_timeout((x)*HZ); \
291 /** Sleep for an indicated number of jiffies (for use in kernel mode).
292 * @param x the number of jiffies to sleep.
294 #define SLEEPJIFFIES(x) \
295 do { current->state = TASK_INTERRUPTIBLE; \
296 schedule_timeout(x); \
300 #define max(a, b) (((a) > (b)) ? (a):(b))
303 static inline struct cdev *cdev_alloc_init(struct module *owner,
304 const struct file_operations *fops)
306 struct cdev *cdev = NULL;
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.)
322 #include "timskmodutils.h"