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/kthread.h>
13 #include <linux/module.h>
14 #include <linux/freezer.h>
15 #include <linux/semaphore.h>
16 #include <linux/slab.h>
17 #include <linux/bitops.h>
19 #include "csr_framework_ext.h"
20 #include "csr_panic.h"
22 /*----------------------------------------------------------------------------*
27 * Create a mutex and return a handle to the created mutex.
31 * CSR_RESULT_SUCCESS in case of success
32 * CSR_FE_RESULT_NO_MORE_MUTEXES in case of out of mutex resources
33 * CSR_FE_RESULT_INVALID_POINTER in case the mutexHandle pointer is invalid
35 *----------------------------------------------------------------------------*/
36 CsrResult CsrMutexCreate(CsrMutexHandle *mutexHandle)
38 if (mutexHandle == NULL)
40 return CSR_FE_RESULT_INVALID_POINTER;
43 sema_init(mutexHandle, 1);
45 return CSR_RESULT_SUCCESS;
48 /*----------------------------------------------------------------------------*
53 * Destroy the previously created mutex.
58 *----------------------------------------------------------------------------*/
59 void CsrMutexDestroy(CsrMutexHandle *mutexHandle)
63 /*----------------------------------------------------------------------------*
68 * Lock the mutex refered to by the provided handle.
72 * CSR_RESULT_SUCCESS in case of success
73 * CSR_FE_RESULT_INVALID_HANDLE in case the mutexHandle is invalid
75 *----------------------------------------------------------------------------*/
76 CsrResult CsrMutexLock(CsrMutexHandle *mutexHandle)
78 if (mutexHandle == NULL)
80 return CSR_FE_RESULT_INVALID_POINTER;
83 if (down_interruptible(mutexHandle))
85 CsrPanic(CSR_TECH_FW, CSR_PANIC_FW_UNEXPECTED_VALUE, "CsrMutexLock Failed");
86 return CSR_FE_RESULT_INVALID_POINTER;
89 return CSR_RESULT_SUCCESS;
92 /*----------------------------------------------------------------------------*
97 * Unlock the mutex refered to by the provided handle.
101 * CSR_RESULT_SUCCESS in case of success
102 * CSR_FE_RESULT_INVALID_HANDLE in case the mutexHandle is invalid
104 *----------------------------------------------------------------------------*/
105 CsrResult CsrMutexUnlock(CsrMutexHandle *mutexHandle)
107 if (mutexHandle == NULL)
109 return CSR_FE_RESULT_INVALID_POINTER;
114 return CSR_RESULT_SUCCESS;
117 /*----------------------------------------------------------------------------*
122 * Sleep for a given period.
127 *----------------------------------------------------------------------------*/
128 void CsrThreadSleep(u16 sleepTimeInMs)
132 /* Convert t in ms to jiffies and round up */
133 t = ((sleepTimeInMs * HZ) + 999) / 1000;
134 schedule_timeout_uninterruptible(t);
136 EXPORT_SYMBOL_GPL(CsrThreadSleep);