1 /*****************************************************************************
3 (c) Cambridge Silicon Radio Limited 2010
4 All rights reserved and confidential information of CSR
6 Refer to LICENSE.txt included with this source for details
9 *****************************************************************************/
11 #include <linux/kernel.h>
12 #include <linux/version.h>
13 #include <linux/kthread.h>
14 #include <linux/module.h>
16 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34)
17 #include <linux/slab.h>
20 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 19)
21 #include <linux/freezer.h>
23 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
24 #include <asm/semaphore.h>
26 #include <linux/semaphore.h>
29 #include <linux/bitops.h>
31 #include "csr_framework_ext.h"
32 #include "csr_panic.h"
34 /*----------------------------------------------------------------------------*
39 * Create a mutex and return a handle to the created mutex.
43 * CSR_RESULT_SUCCESS in case of success
44 * CSR_FE_RESULT_NO_MORE_MUTEXES in case of out of mutex resources
45 * CSR_FE_RESULT_INVALID_POINTER in case the mutexHandle pointer is invalid
47 *----------------------------------------------------------------------------*/
48 CsrResult
CsrMutexCreate(CsrMutexHandle
*mutexHandle
)
50 if (mutexHandle
== NULL
)
52 return CSR_FE_RESULT_INVALID_POINTER
;
55 sema_init(mutexHandle
, 1);
57 return CSR_RESULT_SUCCESS
;
60 /*----------------------------------------------------------------------------*
65 * Destroy the previously created mutex.
70 *----------------------------------------------------------------------------*/
71 void CsrMutexDestroy(CsrMutexHandle
*mutexHandle
)
75 /*----------------------------------------------------------------------------*
80 * Lock the mutex refered to by the provided handle.
84 * CSR_RESULT_SUCCESS in case of success
85 * CSR_FE_RESULT_INVALID_HANDLE in case the mutexHandle is invalid
87 *----------------------------------------------------------------------------*/
88 CsrResult
CsrMutexLock(CsrMutexHandle
*mutexHandle
)
90 if (mutexHandle
== NULL
)
92 return CSR_FE_RESULT_INVALID_POINTER
;
95 if (down_interruptible(mutexHandle
))
97 CsrPanic(CSR_TECH_FW
, CSR_PANIC_FW_UNEXPECTED_VALUE
, "CsrMutexLock Failed");
98 return CSR_FE_RESULT_INVALID_POINTER
;
101 return CSR_RESULT_SUCCESS
;
104 /*----------------------------------------------------------------------------*
109 * Unlock the mutex refered to by the provided handle.
113 * CSR_RESULT_SUCCESS in case of success
114 * CSR_FE_RESULT_INVALID_HANDLE in case the mutexHandle is invalid
116 *----------------------------------------------------------------------------*/
117 CsrResult
CsrMutexUnlock(CsrMutexHandle
*mutexHandle
)
119 if (mutexHandle
== NULL
)
121 return CSR_FE_RESULT_INVALID_POINTER
;
126 return CSR_RESULT_SUCCESS
;
129 /*----------------------------------------------------------------------------*
134 * Sleep for a given period.
139 *----------------------------------------------------------------------------*/
140 void CsrThreadSleep(u16 sleepTimeInMs
)
144 /* Convert t in ms to jiffies and round up */
145 t
= ((sleepTimeInMs
* HZ
) + 999) / 1000;
146 schedule_timeout_uninterruptible(t
);
148 EXPORT_SYMBOL_GPL(CsrThreadSleep
);