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>
15 #include <linux/freezer.h>
16 #include <linux/semaphore.h>
17 #include <linux/slab.h>
18 #include <linux/bitops.h>
20 #include "csr_framework_ext.h"
21 #include "csr_panic.h"
23 /*----------------------------------------------------------------------------*
28 * Create a mutex and return a handle to the created mutex.
32 * CSR_RESULT_SUCCESS in case of success
33 * CSR_FE_RESULT_NO_MORE_MUTEXES in case of out of mutex resources
34 * CSR_FE_RESULT_INVALID_POINTER in case the mutexHandle pointer is invalid
36 *----------------------------------------------------------------------------*/
37 CsrResult CsrMutexCreate(CsrMutexHandle *mutexHandle)
39 if (mutexHandle == NULL)
41 return CSR_FE_RESULT_INVALID_POINTER;
44 sema_init(mutexHandle, 1);
46 return CSR_RESULT_SUCCESS;
49 /*----------------------------------------------------------------------------*
54 * Destroy the previously created mutex.
59 *----------------------------------------------------------------------------*/
60 void CsrMutexDestroy(CsrMutexHandle *mutexHandle)
64 /*----------------------------------------------------------------------------*
69 * Lock the mutex refered to by the provided handle.
73 * CSR_RESULT_SUCCESS in case of success
74 * CSR_FE_RESULT_INVALID_HANDLE in case the mutexHandle is invalid
76 *----------------------------------------------------------------------------*/
77 CsrResult CsrMutexLock(CsrMutexHandle *mutexHandle)
79 if (mutexHandle == NULL)
81 return CSR_FE_RESULT_INVALID_POINTER;
84 if (down_interruptible(mutexHandle))
86 CsrPanic(CSR_TECH_FW, CSR_PANIC_FW_UNEXPECTED_VALUE, "CsrMutexLock Failed");
87 return CSR_FE_RESULT_INVALID_POINTER;
90 return CSR_RESULT_SUCCESS;
93 /*----------------------------------------------------------------------------*
98 * Unlock the mutex refered to by the provided handle.
102 * CSR_RESULT_SUCCESS in case of success
103 * CSR_FE_RESULT_INVALID_HANDLE in case the mutexHandle is invalid
105 *----------------------------------------------------------------------------*/
106 CsrResult CsrMutexUnlock(CsrMutexHandle *mutexHandle)
108 if (mutexHandle == NULL)
110 return CSR_FE_RESULT_INVALID_POINTER;
115 return CSR_RESULT_SUCCESS;
118 /*----------------------------------------------------------------------------*
123 * Sleep for a given period.
128 *----------------------------------------------------------------------------*/
129 void CsrThreadSleep(u16 sleepTimeInMs)
133 /* Convert t in ms to jiffies and round up */
134 t = ((sleepTimeInMs * HZ) + 999) / 1000;
135 schedule_timeout_uninterruptible(t);
137 EXPORT_SYMBOL_GPL(CsrThreadSleep);