osal.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright (C) 2011-2014 MediaTek Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify it under the terms of the
  5. * GNU General Public License version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  8. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. * See the GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License along with this program.
  12. * If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /*! \file
  15. \brief Declaration of library functions
  16. Any definitions in this file will be shared among GLUE Layer and internal Driver Stack.
  17. */
  18. #ifndef _OSAL_H_
  19. #define _OSAL_H_
  20. #include <osal_typedef.h>
  21. /*******************************************************************************
  22. * C O M P I L E R F L A G S
  23. ********************************************************************************
  24. */
  25. /*******************************************************************************
  26. * M A C R O S
  27. ********************************************************************************
  28. */
  29. #define OS_BIT_OPS_SUPPORT 1
  30. #define _osal_inline_ inline
  31. #define MAX_THREAD_NAME_LEN 16
  32. #define MAX_WAKE_LOCK_NAME_LEN 16
  33. #define OSAL_OP_BUF_SIZE 64
  34. #if (defined(CONFIG_MTK_GMO_RAM_OPTIMIZE) && !defined(CONFIG_MT_ENG_BUILD))
  35. #define OSAL_OP_DATA_SIZE 8
  36. #else
  37. #define OSAL_OP_DATA_SIZE 32
  38. #endif
  39. #define DBG_LOG_STR_SIZE 512
  40. #define osal_sizeof(x) sizeof(x)
  41. #define osal_array_size(x) (sizeof(x)/sizeof(x[0]))
  42. #ifndef NAME_MAX
  43. #define NAME_MAX 256
  44. #endif
  45. #define WMT_OP_BIT(x) (0x1UL << x)
  46. #define WMT_OP_HIF_BIT WMT_OP_BIT(0)
  47. #define RB_SIZE(prb) ((prb)->size)
  48. #define RB_MASK(prb) (RB_SIZE(prb) - 1)
  49. #define RB_COUNT(prb) ((prb)->write - (prb)->read)
  50. #define RB_FULL(prb) (RB_COUNT(prb) >= RB_SIZE(prb))
  51. #define RB_EMPTY(prb) ((prb)->write == (prb)->read)
  52. #define RB_INIT(prb, qsize) \
  53. do { \
  54. (prb)->read = (prb)->write = 0; \
  55. (prb)->size = (qsize); \
  56. } while (0)
  57. #define RB_PUT(prb, value) \
  58. do { \
  59. if (!RB_FULL(prb)) { \
  60. (prb)->queue[(prb)->write & RB_MASK(prb)] = value; \
  61. ++((prb)->write); \
  62. } \
  63. else { \
  64. osal_assert(!RB_FULL(prb)); \
  65. } \
  66. } while (0)
  67. #define RB_GET(prb, value) \
  68. do { \
  69. if (!RB_EMPTY(prb)) { \
  70. value = (prb)->queue[(prb)->read & RB_MASK(prb)]; \
  71. ++((prb)->read); \
  72. if (RB_EMPTY(prb)) { \
  73. (prb)->read = (prb)->write = 0; \
  74. } \
  75. } \
  76. else { \
  77. value = NULL; \
  78. osal_assert(!RB_EMPTY(prb)); \
  79. } \
  80. } while (0)
  81. /*******************************************************************************
  82. * E X T E R N A L R E F E R E N C E S
  83. ********************************************************************************
  84. */
  85. /*******************************************************************************
  86. * C O N S T A N T S
  87. ********************************************************************************
  88. */
  89. /*******************************************************************************
  90. * D A T A T Y P E S
  91. ********************************************************************************
  92. */
  93. typedef VOID(*P_TIMEOUT_HANDLER) (unsigned long);
  94. typedef INT32(*P_COND) (VOID *);
  95. typedef struct _OSAL_TIMER_ {
  96. struct timer_list timer;
  97. P_TIMEOUT_HANDLER timeoutHandler;
  98. unsigned long timeroutHandlerData;
  99. } OSAL_TIMER, *P_OSAL_TIMER;
  100. typedef struct _OSAL_UNSLEEPABLE_LOCK_ {
  101. spinlock_t lock;
  102. unsigned long flag;
  103. } OSAL_UNSLEEPABLE_LOCK, *P_OSAL_UNSLEEPABLE_LOCK;
  104. typedef struct _OSAL_SLEEPABLE_LOCK_ {
  105. struct mutex lock;
  106. } OSAL_SLEEPABLE_LOCK, *P_OSAL_SLEEPABLE_LOCK;
  107. typedef struct _OSAL_SIGNAL_ {
  108. struct completion comp;
  109. UINT32 timeoutValue;
  110. } OSAL_SIGNAL, *P_OSAL_SIGNAL;
  111. typedef struct _OSAL_EVENT_ {
  112. wait_queue_head_t waitQueue;
  113. /* VOID *pWaitQueueData; */
  114. UINT32 timeoutValue;
  115. INT32 waitFlag;
  116. } OSAL_EVENT, *P_OSAL_EVENT;
  117. typedef struct _OSAL_THREAD_ {
  118. struct task_struct *pThread;
  119. VOID *pThreadFunc;
  120. VOID *pThreadData;
  121. char threadName[MAX_THREAD_NAME_LEN];
  122. } OSAL_THREAD, *P_OSAL_THREAD;
  123. typedef struct _OSAL_FIFO_ {
  124. /*fifo definition */
  125. VOID *pFifoBody;
  126. spinlock_t fifoSpinlock;
  127. /*fifo operations */
  128. INT32 (*FifoInit)(struct _OSAL_FIFO_ *pFifo, UINT8 *buf, UINT32);
  129. INT32 (*FifoDeInit)(struct _OSAL_FIFO_ *pFifo);
  130. INT32 (*FifoReset)(struct _OSAL_FIFO_ *pFifo);
  131. INT32 (*FifoSz)(struct _OSAL_FIFO_ *pFifo);
  132. INT32 (*FifoAvailSz)(struct _OSAL_FIFO_ *pFifo);
  133. INT32 (*FifoLen)(struct _OSAL_FIFO_ *pFifo);
  134. INT32 (*FifoIsEmpty)(struct _OSAL_FIFO_ *pFifo);
  135. INT32 (*FifoIsFull)(struct _OSAL_FIFO_ *pFifo);
  136. INT32 (*FifoDataIn)(struct _OSAL_FIFO_ *pFifo, const VOID *buf, UINT32 len);
  137. INT32 (*FifoDataOut)(struct _OSAL_FIFO_ *pFifo, void *buf, UINT32 len);
  138. } OSAL_FIFO, *P_OSAL_FIFO;
  139. typedef struct firmware osal_firmware;
  140. typedef struct _OSAL_OP_DAT {
  141. UINT32 opId; /* Event ID */
  142. UINT32 u4InfoBit; /* Reserved */
  143. SIZE_T au4OpData[OSAL_OP_DATA_SIZE]; /* OP Data */
  144. } OSAL_OP_DAT, *P_OSAL_OP_DAT;
  145. typedef struct _OSAL_LXOP_ {
  146. OSAL_OP_DAT op;
  147. OSAL_SIGNAL signal;
  148. INT32 result;
  149. } OSAL_OP, *P_OSAL_OP;
  150. typedef struct _OSAL_LXOP_Q {
  151. OSAL_SLEEPABLE_LOCK sLock;
  152. UINT32 write;
  153. UINT32 read;
  154. UINT32 size;
  155. P_OSAL_OP queue[OSAL_OP_BUF_SIZE];
  156. } OSAL_OP_Q, *P_OSAL_OP_Q;
  157. typedef struct _OSAL_WAKE_LOCK_ {
  158. #ifdef CONFIG_PM_WAKELOCKS
  159. struct wakeup_source wake_lock;
  160. #else
  161. struct wake_lock wake_lock;
  162. #endif
  163. UINT8 name[MAX_WAKE_LOCK_NAME_LEN];
  164. } OSAL_WAKE_LOCK, *P_OSAL_WAKE_LOCK;
  165. #if 1
  166. typedef struct _OSAL_BIT_OP_VAR_ {
  167. unsigned long data;
  168. OSAL_UNSLEEPABLE_LOCK opLock;
  169. } OSAL_BIT_OP_VAR, *P_OSAL_BIT_OP_VAR;
  170. #else
  171. #define OSAL_BIT_OP_VAR unsigned long
  172. #define P_OSAL_BIT_OP_VAR unsigned long *
  173. #endif
  174. typedef UINT32(*P_OSAL_EVENT_CHECKER) (P_OSAL_THREAD pThread);
  175. /*******************************************************************************
  176. * P U B L I C D A T A
  177. ********************************************************************************
  178. */
  179. /*******************************************************************************
  180. * P R I V A T E D A T A
  181. ********************************************************************************
  182. */
  183. /*******************************************************************************
  184. * F U N C T I O N D E C L A R A T I O N S
  185. ********************************************************************************
  186. */
  187. extern UINT32 osal_strlen(const char *str);
  188. extern INT32 osal_strcmp(const char *dst, const char *src);
  189. extern INT32 osal_strncmp(const char *dst, const char *src, UINT32 len);
  190. extern char *osal_strcpy(char *dst, const char *src);
  191. extern char *osal_strncpy(char *dst, const char *src, UINT32 len);
  192. extern char *osal_strcat(char *dst, const char *src);
  193. extern char *osal_strncat(char *dst, const char *src, UINT32 len);
  194. extern char *osal_strchr(const char *str, UINT8 c);
  195. extern char *osal_strsep(char **str, const char *c);
  196. extern int osal_strtol(const char *str, UINT32 adecimal, long *res);
  197. extern INT32 osal_snprintf(char *buf, UINT32 len, const char *fmt, ...);
  198. extern char *osal_strstr(char *str1, const char *str2);
  199. extern INT32 osal_err_print(const char *str, ...);
  200. extern INT32 osal_dbg_print(const char *str, ...);
  201. extern INT32 osal_warn_print(const char *str, ...);
  202. extern INT32 osal_dbg_assert(INT32 expr, const char *file, INT32 line);
  203. extern INT32 osal_sprintf(char *str, const char *format, ...);
  204. extern VOID *osal_malloc(UINT32 size);
  205. extern VOID osal_free(const VOID *dst);
  206. extern VOID *osal_memset(VOID *buf, INT32 i, UINT32 len);
  207. extern VOID *osal_memcpy(VOID *dst, const VOID *src, UINT32 len);
  208. extern INT32 osal_memcmp(const VOID *buf1, const VOID *buf2, UINT32 len);
  209. extern INT32 osal_sleep_ms(UINT32 ms);
  210. extern INT32 osal_udelay(UINT32 us);
  211. extern INT32 osal_timer_create(P_OSAL_TIMER);
  212. extern INT32 osal_timer_start(P_OSAL_TIMER, UINT32);
  213. extern INT32 osal_timer_stop(P_OSAL_TIMER);
  214. extern INT32 osal_timer_stop_sync(P_OSAL_TIMER pTimer);
  215. extern INT32 osal_timer_modify(P_OSAL_TIMER, UINT32);
  216. extern INT32 osal_timer_delete(P_OSAL_TIMER);
  217. extern INT32 osal_fifo_init(P_OSAL_FIFO pFifo, UINT8 *buffer, UINT32 size);
  218. extern VOID osal_fifo_deinit(P_OSAL_FIFO pFifo);
  219. extern INT32 osal_fifo_reset(P_OSAL_FIFO pFifo);
  220. extern UINT32 osal_fifo_in(P_OSAL_FIFO pFifo, PUINT8 buffer, UINT32 size);
  221. extern UINT32 osal_fifo_out(P_OSAL_FIFO pFifo, PUINT8 buffer, UINT32 size);
  222. extern UINT32 osal_fifo_len(P_OSAL_FIFO pFifo);
  223. extern UINT32 osal_fifo_sz(P_OSAL_FIFO pFifo);
  224. extern UINT32 osal_fifo_avail(P_OSAL_FIFO pFifo);
  225. extern UINT32 osal_fifo_is_empty(P_OSAL_FIFO pFifo);
  226. extern UINT32 osal_fifo_is_full(P_OSAL_FIFO pFifo);
  227. extern INT32 osal_wake_lock_init(P_OSAL_WAKE_LOCK plock);
  228. extern INT32 osal_wake_lock(P_OSAL_WAKE_LOCK plock);
  229. extern INT32 osal_wake_unlock(P_OSAL_WAKE_LOCK plock);
  230. extern INT32 osal_wake_lock_count(P_OSAL_WAKE_LOCK plock);
  231. extern INT32 osal_wake_lock_deinit(P_OSAL_WAKE_LOCK plock);
  232. #if defined(CONFIG_PROVE_LOCKING)
  233. #define osal_unsleepable_lock_init(l) { spin_lock_init(&((l)->lock)); }
  234. #else
  235. extern INT32 osal_unsleepable_lock_init(P_OSAL_UNSLEEPABLE_LOCK);
  236. #endif
  237. extern INT32 osal_lock_unsleepable_lock(P_OSAL_UNSLEEPABLE_LOCK);
  238. extern INT32 osal_unlock_unsleepable_lock(P_OSAL_UNSLEEPABLE_LOCK);
  239. extern INT32 osal_unsleepable_lock_deinit(P_OSAL_UNSLEEPABLE_LOCK);
  240. #if defined(CONFIG_PROVE_LOCKING)
  241. #define osal_sleepable_lock_init(l) { mutex_init(&((l)->lock)); }
  242. #else
  243. extern INT32 osal_sleepable_lock_init(P_OSAL_SLEEPABLE_LOCK);
  244. #endif
  245. extern INT32 osal_lock_sleepable_lock(P_OSAL_SLEEPABLE_LOCK);
  246. extern INT32 osal_unlock_sleepable_lock(P_OSAL_SLEEPABLE_LOCK);
  247. extern INT32 osal_sleepable_lock_deinit(P_OSAL_SLEEPABLE_LOCK);
  248. extern INT32 osal_signal_init(P_OSAL_SIGNAL);
  249. extern INT32 osal_wait_for_signal(P_OSAL_SIGNAL);
  250. extern INT32 osal_wait_for_signal_timeout(P_OSAL_SIGNAL);
  251. extern INT32 osal_raise_signal(P_OSAL_SIGNAL);
  252. extern INT32 osal_signal_deinit(P_OSAL_SIGNAL);
  253. extern INT32 osal_event_init(P_OSAL_EVENT);
  254. extern INT32 osal_wait_for_event(P_OSAL_EVENT, P_COND, void *);
  255. extern INT32 osal_wait_for_event_timeout(P_OSAL_EVENT, P_COND, void *);
  256. extern INT32 osal_trigger_event(P_OSAL_EVENT);
  257. extern INT32 osal_event_deinit(P_OSAL_EVENT);
  258. extern INT32 osal_thread_create(P_OSAL_THREAD);
  259. extern INT32 osal_thread_run(P_OSAL_THREAD);
  260. extern INT32 osal_thread_should_stop(P_OSAL_THREAD);
  261. extern INT32 osal_thread_stop(P_OSAL_THREAD);
  262. /*extern INT32 osal_thread_wait_for_event(P_OSAL_THREAD, P_OSAL_EVENT);*/
  263. extern INT32 osal_thread_wait_for_event(P_OSAL_THREAD, P_OSAL_EVENT, P_OSAL_EVENT_CHECKER);
  264. /*check pOsalLxOp and OSAL_THREAD_SHOULD_STOP*/
  265. extern INT32 osal_thread_destroy(P_OSAL_THREAD);
  266. extern INT32 osal_clear_bit(UINT32 bitOffset, P_OSAL_BIT_OP_VAR pData);
  267. extern INT32 osal_set_bit(UINT32 bitOffset, P_OSAL_BIT_OP_VAR pData);
  268. extern INT32 osal_test_bit(UINT32 bitOffset, P_OSAL_BIT_OP_VAR pData);
  269. extern INT32 osal_test_and_clear_bit(UINT32 bitOffset, P_OSAL_BIT_OP_VAR pData);
  270. extern INT32 osal_test_and_set_bit(UINT32 bitOffset, P_OSAL_BIT_OP_VAR pData);
  271. extern INT32 osal_dbg_assert_aee(const char *module, const char *detail_description);
  272. extern INT32 osal_gettimeofday(PINT32 sec, PINT32 usec);
  273. extern INT32 osal_printtimeofday(const PUINT8 prefix);
  274. extern VOID osal_buffer_dump(const UINT8 *buf, const UINT8 *title, UINT32 len, UINT32 limit);
  275. extern UINT32 osal_op_get_id(P_OSAL_OP pOp);
  276. extern MTK_WCN_BOOL osal_op_is_wait_for_signal(P_OSAL_OP pOp);
  277. extern VOID osal_op_raise_signal(P_OSAL_OP pOp, INT32 result);
  278. extern VOID osal_set_op_result(P_OSAL_OP pOp, INT32 result);
  279. extern UINT16 osal_crc16(const UINT8 *buffer, const UINT32 length);
  280. /*******************************************************************************
  281. * F U N C T I O N S
  282. ********************************************************************************
  283. */
  284. #define osal_assert(condition) \
  285. do { \
  286. if (!(condition)) \
  287. osal_err_print("%s, %d, (%s)\n", __FILE__, __LINE__, #condition); \
  288. } while (0)
  289. #endif /* _OSAL_H_ */