Ken Cox | 9d9baad | 2014-03-04 07:58:05 -0600 | [diff] [blame] | 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) |
Ken Cox | 9d9baad | 2014-03-04 07:58:05 -0600 | [diff] [blame] | 135 | /** Given a typedef/struct/union and a member field name, |
| 136 | * return the number of bytes occupied by that field. |
| 137 | * @param TYPE the typedef name, or "struct xx" or "union xx" |
| 138 | * @param MEMBER the name of the member field whose size is to be determined |
| 139 | * @return the size of the field in bytes |
| 140 | */ |
| 141 | #define FAIL(msg, status) do { \ |
| 142 | ERRDRV("'%s'" \ |
| 143 | ": error (status=%d)\n", \ |
| 144 | msg, status); \ |
| 145 | RETINT(status); \ |
| 146 | } while (0) |
Ken Cox | 9d9baad | 2014-03-04 07:58:05 -0600 | [diff] [blame] | 147 | /** Try to evaulate the provided expression, and do a RETINT(x) iff |
| 148 | * the expression evaluates to < 0. |
| 149 | * @param x the expression to try |
| 150 | */ |
Ken Cox | 9d9baad | 2014-03-04 07:58:05 -0600 | [diff] [blame] | 151 | #define ASSERT(cond) \ |
| 152 | do { if (!(cond)) \ |
| 153 | HUHDRV("ASSERT failed - %s", \ |
| 154 | __stringify(cond)); \ |
| 155 | } while (0) |
| 156 | |
| 157 | #define sizeofmember(TYPE, MEMBER) (sizeof(((TYPE *)0)->MEMBER)) |
| 158 | /** "Covered quotient" function */ |
| 159 | #define COVQ(v, d) (((v) + (d) - 1) / (d)) |
| 160 | #define SWAPPOINTERS(p1, p2) \ |
| 161 | do { \ |
| 162 | void *SWAPPOINTERS_TEMP = (void *)p1; \ |
| 163 | (void *)(p1) = (void *)(p2); \ |
| 164 | (void *)(p2) = SWAPPOINTERS_TEMP; \ |
| 165 | } while (0) |
| 166 | |
| 167 | /** |
| 168 | * @addtogroup driverlogging |
| 169 | * @{ |
| 170 | */ |
| 171 | |
| 172 | #define PRINTKDRV(fmt, args...) LOGINF(fmt, ## args) |
| 173 | #define TBDDRV(fmt, args...) LOGERR(fmt, ## args) |
| 174 | #define HUHDRV(fmt, args...) LOGERR(fmt, ## args) |
| 175 | #define ERRDRV(fmt, args...) LOGERR(fmt, ## args) |
| 176 | #define WARNDRV(fmt, args...) LOGWRN(fmt, ## args) |
| 177 | #define SECUREDRV(fmt, args...) LOGWRN(fmt, ## args) |
| 178 | #define INFODRV(fmt, args...) LOGINF(fmt, ## args) |
| 179 | #define DEBUGDRV(fmt, args...) DBGINF(fmt, ## args) |
| 180 | |
| 181 | #define PRINTKDEV(devname, fmt, args...) LOGINFDEV(devname, fmt, ## args) |
| 182 | #define TBDDEV(devname, fmt, args...) LOGERRDEV(devname, fmt, ## args) |
| 183 | #define HUHDEV(devname, fmt, args...) LOGERRDEV(devname, fmt, ## args) |
| 184 | #define ERRDEV(devname, fmt, args...) LOGERRDEV(devname, fmt, ## args) |
| 185 | #define ERRDEVX(devno, fmt, args...) LOGERRDEVX(devno, fmt, ## args) |
| 186 | #define WARNDEV(devname, fmt, args...) LOGWRNDEV(devname, fmt, ## args) |
| 187 | #define SECUREDEV(devname, fmt, args...) LOGWRNDEV(devname, fmt, ## args) |
| 188 | #define INFODEV(devname, fmt, args...) LOGINFDEV(devname, fmt, ## args) |
| 189 | #define INFODEVX(devno, fmt, args...) LOGINFDEVX(devno, fmt, ## args) |
| 190 | #define DEBUGDEV(devname, fmt, args...) DBGINFDEV(devname, fmt, ## args) |
| 191 | |
| 192 | |
| 193 | /* @} */ |
| 194 | |
Ken Cox | 9d9baad | 2014-03-04 07:58:05 -0600 | [diff] [blame] | 195 | /** Verifies the consistency of your PRIVATEDEVICEDATA structure using |
| 196 | * conventional "signature" fields: |
| 197 | * <p> |
| 198 | * - sig1 should contain the size of the structure |
| 199 | * - sig2 should contain a pointer to the beginning of the structure |
| 200 | */ |
| 201 | #define DDLOOKSVALID(dd) \ |
| 202 | ((dd != NULL) && \ |
| 203 | ((dd)->sig1 == sizeof(PRIVATEDEVICEDATA)) && \ |
| 204 | ((dd)->sig2 == dd)) |
| 205 | |
| 206 | /** Verifies the consistency of your PRIVATEFILEDATA structure using |
| 207 | * conventional "signature" fields: |
| 208 | * <p> |
| 209 | * - sig1 should contain the size of the structure |
| 210 | * - sig2 should contain a pointer to the beginning of the structure |
| 211 | */ |
| 212 | #define FDLOOKSVALID(fd) \ |
| 213 | ((fd != NULL) && \ |
| 214 | ((fd)->sig1 == sizeof(PRIVATEFILEDATA)) && \ |
| 215 | ((fd)->sig2 == fd)) |
| 216 | |
Ken Cox | 9d9baad | 2014-03-04 07:58:05 -0600 | [diff] [blame] | 217 | /** Locks dd->lockDev if you havn't already locked it */ |
| 218 | #define LOCKDEV(dd) \ |
| 219 | { \ |
| 220 | if (!lockedDev) { \ |
| 221 | spin_lock(&dd->lockDev); \ |
| 222 | lockedDev = TRUE; \ |
| 223 | } \ |
| 224 | } |
| 225 | |
| 226 | /** Unlocks dd->lockDev if you previously locked it */ |
| 227 | #define UNLOCKDEV(dd) \ |
| 228 | { \ |
| 229 | if (lockedDev) { \ |
| 230 | spin_unlock(&dd->lockDev); \ |
| 231 | lockedDev = FALSE; \ |
| 232 | } \ |
| 233 | } |
| 234 | |
| 235 | /** Locks dd->lockDevISR if you havn't already locked it */ |
| 236 | #define LOCKDEVISR(dd) \ |
| 237 | { \ |
| 238 | if (!lockedDevISR) { \ |
| 239 | spin_lock_irqsave(&dd->lockDevISR, flags); \ |
| 240 | lockedDevISR = TRUE; \ |
| 241 | } \ |
| 242 | } |
| 243 | |
| 244 | /** Unlocks dd->lockDevISR if you previously locked it */ |
| 245 | #define UNLOCKDEVISR(dd) \ |
| 246 | { \ |
| 247 | if (lockedDevISR) { \ |
| 248 | spin_unlock_irqrestore(&dd->lockDevISR, flags); \ |
| 249 | lockedDevISR = FALSE; \ |
| 250 | } \ |
| 251 | } |
| 252 | |
| 253 | /** Locks LockGlobalISR if you havn't already locked it */ |
| 254 | #define LOCKGLOBALISR \ |
| 255 | { \ |
| 256 | if (!lockedGlobalISR) { \ |
| 257 | spin_lock_irqsave(&LockGlobalISR, flags); \ |
| 258 | lockedGlobalISR = TRUE; \ |
| 259 | } \ |
| 260 | } |
| 261 | |
| 262 | /** Unlocks LockGlobalISR if you previously locked it */ |
| 263 | #define UNLOCKGLOBALISR \ |
| 264 | { \ |
| 265 | if (lockedGlobalISR) { \ |
| 266 | spin_unlock_irqrestore(&LockGlobalISR, flags); \ |
| 267 | lockedGlobalISR = FALSE; \ |
| 268 | } \ |
| 269 | } |
| 270 | |
| 271 | /** Locks LockGlobal if you havn't already locked it */ |
| 272 | #define LOCKGLOBAL \ |
| 273 | { \ |
| 274 | if (!lockedGlobal) { \ |
| 275 | spin_lock(&LockGlobal); \ |
| 276 | lockedGlobal = TRUE; \ |
| 277 | } \ |
| 278 | } |
| 279 | |
| 280 | /** Unlocks LockGlobal if you previously locked it */ |
| 281 | #define UNLOCKGLOBAL \ |
| 282 | { \ |
| 283 | if (lockedGlobal) { \ |
| 284 | spin_unlock(&LockGlobal); \ |
| 285 | lockedGlobal = FALSE; \ |
| 286 | } \ |
| 287 | } |
| 288 | |
| 289 | /** Use this at the beginning of functions where you intend to |
| 290 | * use #LOCKDEV/#UNLOCKDEV, #LOCKDEVISR/#UNLOCKDEVISR, |
| 291 | * #LOCKGLOBAL/#UNLOCKGLOBAL, #LOCKGLOBALISR/#UNLOCKGLOBALISR. |
| 292 | * |
| 293 | * Note that __attribute__((unused)) is how you tell GNU C to suppress |
| 294 | * any warning messages about the variable being unused. |
| 295 | */ |
| 296 | #define LOCKPREAMBLE \ |
| 297 | ulong flags __attribute__((unused)) = 0; \ |
| 298 | BOOL lockedDev __attribute__((unused)) = FALSE; \ |
| 299 | BOOL lockedDevISR __attribute__((unused)) = FALSE; \ |
| 300 | BOOL lockedGlobal __attribute__((unused)) = FALSE; \ |
| 301 | BOOL lockedGlobalISR __attribute__((unused)) = FALSE |
| 302 | |
| 303 | |
| 304 | |
| 305 | /** Sleep for an indicated number of seconds (for use in kernel mode). |
| 306 | * @param x the number of seconds to sleep. |
| 307 | */ |
| 308 | #define SLEEP(x) \ |
| 309 | do { current->state = TASK_INTERRUPTIBLE; \ |
| 310 | schedule_timeout((x)*HZ); \ |
| 311 | } while (0) |
| 312 | |
| 313 | /** Sleep for an indicated number of jiffies (for use in kernel mode). |
| 314 | * @param x the number of jiffies to sleep. |
| 315 | */ |
| 316 | #define SLEEPJIFFIES(x) \ |
| 317 | do { current->state = TASK_INTERRUPTIBLE; \ |
| 318 | schedule_timeout(x); \ |
| 319 | } while (0) |
| 320 | |
| 321 | #ifndef max |
| 322 | #define max(a, b) (((a) > (b)) ? (a):(b)) |
| 323 | #endif |
| 324 | |
| 325 | static inline struct cdev *cdev_alloc_init(struct module *owner, |
| 326 | const struct file_operations *fops) |
| 327 | { |
| 328 | struct cdev *cdev = NULL; |
| 329 | cdev = cdev_alloc(); |
| 330 | if (!cdev) |
| 331 | return NULL; |
| 332 | cdev->ops = fops; |
| 333 | cdev->owner = owner; |
| 334 | |
| 335 | /* Note that the memory allocated for cdev will be deallocated |
| 336 | * when the usage count drops to 0, because it is controlled |
| 337 | * by a kobject of type ktype_cdev_dynamic. (This |
| 338 | * deallocation could very well happen outside of our kernel |
| 339 | * module, like via the cdev_put in __fput() for example.) |
| 340 | */ |
| 341 | return cdev; |
| 342 | } |
| 343 | |
| 344 | #include "timskmodutils.h" |
| 345 | |
| 346 | #endif |