4 * @brief header file with helpers for both Linux and Lynx
6 * Copyright (c) 2006-2009 CERN
7 * @author Yury Georgievskiy <yury.georgievskiy@cern.ch>
9 * Copyright (c) 2009 CERN
10 * @author Emilio G. Cota <emilio.garcia.cota@cern.ch>
12 * @section license_sec License
13 * Released under the GPL v2. (and only v2, not any later version)
16 #ifndef _CDCM_BOTH_H_INCLUDE_
17 #define _CDCM_BOTH_H_INCLUDE_
21 #include <linux/spinlock.h>
22 #include <linux/mutex.h>
25 #include <asm/byteorder.h>
31 * Endianness definitions
33 #if defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
34 # error CDCM: Fix asm/byteorder.h to define one endianness.
37 #if !defined(__LITTLE_ENDIAN) && !defined(__BIG_ENDIAN)
38 # error CDCM: Fix asm/byteorder.h to define one endianness.
41 #if defined(__LITTLE_ENDIAN)
42 #define CDCM_LITTLE_ENDIAN 1234
43 #else /* __BIG_ENDIAN */
44 #define CDCM_BIG_ENDIAN 4321
48 * ms_to_ticks, ticks_to_ms conversion
50 #define cdcm_ticks_to_ms(x) ((x) * 1000 / HZ)
51 #define cdcm_ms_to_ticks(x) ((x) * HZ / 1000)
54 * CDCM Spinlocks: wrap Linux' spinlocks
60 #define cdcm_spin_lock_init(cdcm) spin_lock_init(&(cdcm)->lock)
62 #define cdcm_spin_lock_irqsave(cdcm, flags) \
63 spin_lock_irqsave(&(cdcm)->lock, flags)
65 #define cdcm_spin_unlock_irqrestore(cdcm, flags) \
66 spin_unlock_irqrestore(&(cdcm)->lock, flags)
68 #define __CDCM_SPINLOCK_UNLOCKED(name) { \
69 .lock = __SPIN_LOCK_UNLOCKED(name.lock), }
72 * Mutexes: wrap Linux' mutexes
78 #define __CDCM_MUTEX_INIT(name) \
79 (struct cdcm_mutex) { .mutex = DEFINE_MUTEX(name.mutex) }
80 #define cdcm_mutex_init(cdcm) mutex_init(&(cdcm)->mutex)
81 #define cdcm_mutex_lock(cdcm) mutex_lock(&(cdcm)->mutex)
82 #define cdcm_mutex_unlock(cdcm) mutex_unlock(&(cdcm)->mutex)
83 #define cdcm_mutex_lock_interruptible(cdcm) \
84 mutex_lock_interruptible(&(cdcm)->mutex)
95 * Endianness definitions
97 #if defined(__LITTLE_ENDIAN__) && defined(__BIG_ENDIAN__)
98 # error CDCM: Conflicting endianities defined in this machine.
101 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
102 # error CDCM: Conflicting endianities defined in this machine.
105 #if defined(__LITTLE_ENDIAN__)
106 #define CDCM_LITTLE_ENDIAN 1234
107 #else /* __BIG_ENDIAN__ */
108 #define CDCM_BIG_ENDIAN 4321
112 * ms_to_ticks, ticks_to_ms conversion
114 #define cdcm_ticks_to_ms(x) ((x) * 1000 / TICKSPERSEC)
115 #define cdcm_ms_to_ticks(x) ((x) * TICKSPERSEC / 1000)
118 * CDCM Spinlocks: mimic Linux' spinlocks
119 * In Lynx, we just use the only available primitives (disable/restore)
120 * We use 'int foo' in Lynx to avoid compiler warnings (i.e. unused struct)
123 /* no individual locks in LynxOS */
128 * Note: foo is just used to actually do something on the struct,
129 * but it has no logical significance. As it is now, 'foo' refers
130 * to the lock's state: 0 (unlocked), 1 (locked), but really, there's
131 * no code that should ever need to rely on this value. So simply ignore it.
133 #define _lynx_lock_init(cdcm) \
138 #define __CDCM_SPINLOCK_UNLOCKED(lockname) { \
141 #define _lynx_lock(cdcm, flags) \
147 #define _lynx_unlock(cdcm, flags) \
153 #define cdcm_spin_lock_init(cdcm) _lynx_lock_init(cdcm)
154 #define cdcm_spin_lock_irqsave(cdcm, flags) _lynx_lock(cdcm, flags)
155 #define cdcm_spin_unlock_irqrestore(cdcm, flags) _lynx_unlock(cdcm, flags)
158 * Mutexes: make them Linux-like, calling ssignal/swait in Lynx
164 #define __CDCM_MUTEX_INIT(name) \
165 (struct cdcm_mutex) { .sem = 1 }
166 #define cdcm_mutex_init(cdcm) do { (cdcm)->sem = 1; } while (0)
167 #define cdcm_mutex_lock(cdcm) swait(&(cdcm)->sem, SEM_SIGIGNORE)
168 #define cdcm_mutex_unlock(cdcm) ssignal(&(cdcm)->sem)
169 #define cdcm_mutex_lock_interruptible(cdcm) \
170 swait(&(cdcm)->sem, SEM_SIGABORT)
172 #endif /* !__linux__ */
177 * Common Linux and Lynx
180 #define CDCM_DEFINE_SPINLOCK(x) \
181 cdcm_spinlock_t x = __CDCM_SPINLOCK_UNLOCKED(x)
182 /* statically define a mutex */
183 #define CDCM_MUTEX(x) struct cdcm_mutex x = __CDCM_MUTEX_INIT(x)
186 * The extern declaration of these functions is mainly for Lynx, since For Linux,
187 * they need to be declared as extern before being used. For Linux,
188 * they're declared (as non-extern) in cdcmLynxAPI.h.
190 extern int ksprintf(char *buf
, char *format
, ...);
191 extern void usec_sleep(unsigned long usecs
);
192 extern int scount(int *semaphore
);
195 * Common function signatures
197 int cdcm_copy_from_user(void *, void *, int);
198 int cdcm_copy_to_user(void *, void *, int);
201 #define CDCM_LOOP_AGAIN
204 #endif /* _CDCM_BOTH_H_INCLUDE_ */