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 /** 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
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
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
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
145 #define FAIL(msg, status) do { \
147 ": error (status=%d)\n", \
151 #define FAIL_WPOSTCODE_1(msg, status, EVENT_PC) do { \
153 ": error (status=%d)\n", \
155 POSTCODE_LINUX_2(EVENT_PC, DIAG_SEVERITY_ERR); \
158 #define FAIL_WPOSTCODE_2(msg, status, EVENT_PC, pcval32bit) do { \
160 ": error (status=%d)\n", \
162 POSTCODE_LINUX_3(EVENT_PC, pcval32bit, DIAG_SEVERITY_ERR); \
165 #define FAIL_WPOSTCODE_3(msg, status, EVENT_PC, pcval16bit1, pcval16bit2) \
168 ": error (status=%d)\n", \
170 POSTCODE_LINUX_4(EVENT_PC, pcval16bit1, pcval16bit2, \
171 DIAG_SEVERITY_ERR); \
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
178 #define TRY(x) do { int status = (x); \
180 FAIL(__stringify(x), status); \
183 #define TRY_WPOSTCODE_1(x, EVENT_PC) do { \
186 FAIL_WPOSTCODE_1(__stringify(x), status, EVENT_PC); \
189 #define TRY_WPOSTCODE_2(x, EVENT_PC, pcval32bit) do { \
192 FAIL_WPOSTCODE_2(__stringify(x), status, EVENT_PC, \
196 #define ASSERT(cond) \
198 HUHDRV("ASSERT failed - %s", \
199 __stringify(cond)); \
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) \
207 void *SWAPPOINTERS_TEMP = (void *)p1; \
208 (void *)(p1) = (void *)(p2); \
209 (void *)(p2) = SWAPPOINTERS_TEMP; \
213 * @addtogroup driverlogging
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)
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)
240 /** Used to add a single line to the /proc filesystem buffer */
241 #define ADDPROCLINE(buf, bufsize, line, linelen, totallen) \
243 if ((totallen) + (linelen) >= bufsize) \
247 totallen += linelen; \
253 /** Verifies the consistency of your PRIVATEDEVICEDATA structure using
254 * conventional "signature" fields:
256 * - sig1 should contain the size of the structure
257 * - sig2 should contain a pointer to the beginning of the structure
259 #define DDLOOKSVALID(dd) \
261 ((dd)->sig1 == sizeof(PRIVATEDEVICEDATA)) && \
264 /** Verifies the consistency of your PRIVATEFILEDATA structure using
265 * conventional "signature" fields:
267 * - sig1 should contain the size of the structure
268 * - sig2 should contain a pointer to the beginning of the structure
270 #define FDLOOKSVALID(fd) \
272 ((fd)->sig1 == sizeof(PRIVATEFILEDATA)) && \
275 /** Verifies the consistency of a PRIVATEDEVICEDATA structure and reacts
278 #define CHKDDX(dd, x) ( \
279 if (!DDLOOKSVALID((dd))) { \
280 PRINTKDRV("bad device structure"); \
284 /** Verifies the consistency of a PRIVATEDEVICEDATA structure and reacts
287 #define CHKDD(dd) ( \
288 if (!DDLOOKSVALID(dd)) { \
289 PRINTKDRV("bad device structure"); \
293 /** Verifies the consistency of a PRIVATEFILEDATA structure and reacts
296 #define CHKFDX(fd, x) ( \
297 if (!FDLOOKSVALID(fd)) { \
298 PRINTKDRV("bad file structure"); \
302 /** Verifies the consistency of a PRIVATEFILEDATA structure and reacts
305 #define CHKFD(fd) ( \
306 if (!FDLOOKSVALID(fd)) { \
307 PRINTKDRV("bad file structure"); \
311 /** Locks dd->lockDev if you havn't already locked it */
312 #define LOCKDEV(dd) \
315 spin_lock(&dd->lockDev); \
320 /** Unlocks dd->lockDev if you previously locked it */
321 #define UNLOCKDEV(dd) \
324 spin_unlock(&dd->lockDev); \
329 /** Locks dd->lockDevISR if you havn't already locked it */
330 #define LOCKDEVISR(dd) \
332 if (!lockedDevISR) { \
333 spin_lock_irqsave(&dd->lockDevISR, flags); \
334 lockedDevISR = TRUE; \
338 /** Unlocks dd->lockDevISR if you previously locked it */
339 #define UNLOCKDEVISR(dd) \
341 if (lockedDevISR) { \
342 spin_unlock_irqrestore(&dd->lockDevISR, flags); \
343 lockedDevISR = FALSE; \
347 /** Locks LockGlobalISR if you havn't already locked it */
348 #define LOCKGLOBALISR \
350 if (!lockedGlobalISR) { \
351 spin_lock_irqsave(&LockGlobalISR, flags); \
352 lockedGlobalISR = TRUE; \
356 /** Unlocks LockGlobalISR if you previously locked it */
357 #define UNLOCKGLOBALISR \
359 if (lockedGlobalISR) { \
360 spin_unlock_irqrestore(&LockGlobalISR, flags); \
361 lockedGlobalISR = FALSE; \
365 /** Locks LockGlobal if you havn't already locked it */
368 if (!lockedGlobal) { \
369 spin_lock(&LockGlobal); \
370 lockedGlobal = TRUE; \
374 /** Unlocks LockGlobal if you previously locked it */
375 #define UNLOCKGLOBAL \
377 if (lockedGlobal) { \
378 spin_unlock(&LockGlobal); \
379 lockedGlobal = FALSE; \
383 /** Use this at the beginning of functions where you intend to
384 * use #LOCKDEV/#UNLOCKDEV, #LOCKDEVISR/#UNLOCKDEVISR,
385 * #LOCKGLOBAL/#UNLOCKGLOBAL, #LOCKGLOBALISR/#UNLOCKGLOBALISR.
387 * Note that __attribute__((unused)) is how you tell GNU C to suppress
388 * any warning messages about the variable being unused.
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
399 /** Sleep for an indicated number of seconds (for use in kernel mode).
400 * @param x the number of seconds to sleep.
403 do { current->state = TASK_INTERRUPTIBLE; \
404 schedule_timeout((x)*HZ); \
407 /** Sleep for an indicated number of jiffies (for use in kernel mode).
408 * @param x the number of jiffies to sleep.
410 #define SLEEPJIFFIES(x) \
411 do { current->state = TASK_INTERRUPTIBLE; \
412 schedule_timeout(x); \
416 #define max(a, b) (((a) > (b)) ? (a):(b))
419 static inline struct cdev *cdev_alloc_init(struct module *owner,
420 const struct file_operations *fops)
422 struct cdev *cdev = NULL;
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.)
438 #include "timskmodutils.h"