osal.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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. /*******************************************************************************
  19. * C O M P I L E R F L A G S
  20. ********************************************************************************
  21. */
  22. /*******************************************************************************
  23. * M A C R O S
  24. ********************************************************************************
  25. */
  26. /*******************************************************************************
  27. * E X T E R N A L R E F E R E N C E S
  28. ********************************************************************************
  29. */
  30. #include "osal_typedef.h"
  31. #include "osal.h"
  32. /*******************************************************************************
  33. * C O N S T A N T S
  34. ********************************************************************************
  35. */
  36. /*******************************************************************************
  37. * D A T A T Y P E S
  38. ********************************************************************************
  39. */
  40. /*******************************************************************************
  41. * P U B L I C D A T A
  42. ********************************************************************************
  43. */
  44. /*******************************************************************************
  45. * P R I V A T E D A T A
  46. ********************************************************************************
  47. */
  48. /* CRC table for the CRC-16. The poly is 0x8005 (x^16 + x^15 + x^2 + 1) */
  49. static UINT16 const crc16_table[256] = {
  50. 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
  51. 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
  52. 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
  53. 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
  54. 0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
  55. 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
  56. 0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
  57. 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
  58. 0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
  59. 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
  60. 0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
  61. 0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
  62. 0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
  63. 0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
  64. 0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
  65. 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
  66. 0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
  67. 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
  68. 0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
  69. 0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
  70. 0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
  71. 0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
  72. 0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
  73. 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
  74. 0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
  75. 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
  76. 0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
  77. 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
  78. 0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
  79. 0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
  80. 0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
  81. 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
  82. };
  83. /*******************************************************************************
  84. * F U N C T I O N D E C L A R A T I O N S
  85. ********************************************************************************
  86. */
  87. /*******************************************************************************
  88. * F U N C T I O N S
  89. ********************************************************************************
  90. */
  91. /*string operations*/
  92. _osal_inline_ UINT32 osal_strlen(const char *str)
  93. {
  94. return strlen(str);
  95. }
  96. _osal_inline_ INT32 osal_strcmp(const char *dst, const char *src)
  97. {
  98. return strcmp(dst, src);
  99. }
  100. _osal_inline_ INT32 osal_strncmp(const char *dst, const char *src, UINT32 len)
  101. {
  102. return strncmp(dst, src, len);
  103. }
  104. _osal_inline_ char *osal_strcpy(char *dst, const char *src)
  105. {
  106. return strcpy(dst, src);
  107. }
  108. _osal_inline_ char *osal_strncpy(char *dst, const char *src, UINT32 len)
  109. {
  110. return strncpy(dst, src, len);
  111. }
  112. _osal_inline_ char *osal_strcat(char *dst, const char *src)
  113. {
  114. return strcat(dst, src);
  115. }
  116. _osal_inline_ char *osal_strncat(char *dst, const char *src, UINT32 len)
  117. {
  118. return strncat(dst, src, len);
  119. }
  120. _osal_inline_ char *osal_strchr(const char *str, UINT8 c)
  121. {
  122. return strchr(str, c);
  123. }
  124. _osal_inline_ char *osal_strsep(char **str, const char *c)
  125. {
  126. return strsep(str, c);
  127. }
  128. _osal_inline_ int osal_strtol(const char *str, UINT32 adecimal, long *res)
  129. {
  130. if (4 == sizeof(long))
  131. return kstrtou32(str, adecimal, (UINT32 *) res);
  132. else
  133. return kstrtol(str, adecimal, res);
  134. }
  135. _osal_inline_ char *osal_strstr(char *str1, const char *str2)
  136. {
  137. return strstr(str1, str2);
  138. }
  139. INT32 osal_snprintf(char *buf, UINT32 len, const char *fmt, ...)
  140. {
  141. INT32 iRet = 0;
  142. va_list args;
  143. /*va_start(args, fmt); */
  144. va_start(args, fmt);
  145. /*iRet = snprintf(buf, len, fmt, args); */
  146. iRet = vsnprintf(buf, len, fmt, args);
  147. va_end(args);
  148. return iRet;
  149. }
  150. INT32 osal_err_print(const char *str, ...)
  151. {
  152. va_list args;
  153. char tempString[DBG_LOG_STR_SIZE];
  154. va_start(args, str);
  155. vsnprintf(tempString, DBG_LOG_STR_SIZE, str, args);
  156. va_end(args);
  157. pr_err("%s", tempString);
  158. return 0;
  159. }
  160. INT32 osal_dbg_print(const char *str, ...)
  161. {
  162. va_list args;
  163. char tempString[DBG_LOG_STR_SIZE];
  164. va_start(args, str);
  165. vsnprintf(tempString, DBG_LOG_STR_SIZE, str, args);
  166. va_end(args);
  167. pr_debug("%s", tempString);
  168. return 0;
  169. }
  170. INT32 osal_warn_print(const char *str, ...)
  171. {
  172. va_list args;
  173. char tempString[DBG_LOG_STR_SIZE];
  174. va_start(args, str);
  175. vsnprintf(tempString, DBG_LOG_STR_SIZE, str, args);
  176. va_end(args);
  177. pr_warn("%s", tempString);
  178. return 0;
  179. }
  180. INT32 osal_dbg_assert(INT32 expr, const char *file, INT32 line)
  181. {
  182. if (!expr) {
  183. pr_warn("%s (%d)\n", file, line);
  184. /*BUG_ON(!expr); */
  185. #ifdef CFG_COMMON_GPIO_DBG_PIN
  186. /* package this part */
  187. mt_set_gpio_out(GPIO70, GPIO_OUT_ZERO);
  188. pr_warn("toggle GPIO70\n");
  189. udelay(10);
  190. mt_set_gpio_out(GPIO70, GPIO_OUT_ONE);
  191. #endif
  192. return 1;
  193. }
  194. return 0;
  195. }
  196. INT32 osal_dbg_assert_aee(const char *module, const char *detail_description)
  197. {
  198. osal_err_print("[WMT-ASSERT]" "[E][Module]:%s, [INFO]%s\n", module, detail_description);
  199. #ifdef WMT_PLAT_ALPS
  200. /* aee_kernel_warning(module,detail_description); */
  201. aee_kernel_warning_api(__FILE__, __LINE__, DB_OPT_WCN_ISSUE_INFO, module, detail_description);
  202. #endif
  203. return 0;
  204. }
  205. INT32 osal_sprintf(char *str, const char *format, ...)
  206. {
  207. INT32 iRet = 0;
  208. va_list args;
  209. va_start(args, format);
  210. iRet = vsnprintf(str, DBG_LOG_STR_SIZE, format, args);
  211. va_end(args);
  212. return iRet;
  213. }
  214. _osal_inline_ VOID *osal_malloc(UINT32 size)
  215. {
  216. return vmalloc(size);
  217. }
  218. _osal_inline_ VOID osal_free(const VOID *dst)
  219. {
  220. vfree(dst);
  221. }
  222. _osal_inline_ VOID *osal_memset(VOID *buf, INT32 i, UINT32 len)
  223. {
  224. return memset(buf, i, len);
  225. }
  226. _osal_inline_ VOID *osal_memcpy(VOID *dst, const VOID *src, UINT32 len)
  227. {
  228. #ifdef CONFIG_MTK_WCN_ARM64
  229. char *tmp;
  230. const char *s;
  231. size_t i;
  232. tmp = dst;
  233. s = src;
  234. for (i = 0; i < len; i++)
  235. tmp[i] = s[i];
  236. return dst;
  237. #else
  238. return memcpy(dst, src, len);
  239. #endif
  240. }
  241. _osal_inline_ INT32 osal_memcmp(const VOID *buf1, const VOID *buf2, UINT32 len)
  242. {
  243. return memcmp(buf1, buf2, len);
  244. }
  245. _osal_inline_ UINT16 osal_crc16(const UINT8 *buffer, const UINT32 length)
  246. {
  247. UINT16 crc = 0;
  248. UINT32 i = 0;
  249. /* FIXME: Add STP checksum feature */
  250. crc = 0;
  251. for (i = 0; i < length; i++, buffer++)
  252. crc = (crc >> 8) ^ crc16_table[(crc ^ (*buffer)) & 0xff];
  253. return crc;
  254. }
  255. /*
  256. *OSAL layer Thread Opeartion related APIs
  257. *
  258. *
  259. */
  260. _osal_inline_ INT32 osal_thread_create(P_OSAL_THREAD pThread)
  261. {
  262. pThread->pThread = kthread_create(pThread->pThreadFunc, pThread->pThreadData, pThread->threadName);
  263. if (NULL == pThread->pThread)
  264. return -1;
  265. return 0;
  266. }
  267. _osal_inline_ INT32 osal_thread_run(P_OSAL_THREAD pThread)
  268. {
  269. if (pThread->pThread) {
  270. wake_up_process(pThread->pThread);
  271. return 0;
  272. } else {
  273. return -1;
  274. }
  275. }
  276. _osal_inline_ INT32 osal_thread_stop(P_OSAL_THREAD pThread)
  277. {
  278. INT32 iRet;
  279. if ((pThread) && (pThread->pThread)) {
  280. iRet = kthread_stop(pThread->pThread);
  281. /* pThread->pThread = NULL; */
  282. return iRet;
  283. }
  284. return -1;
  285. }
  286. _osal_inline_ INT32 osal_thread_should_stop(P_OSAL_THREAD pThread)
  287. {
  288. if ((pThread) && (pThread->pThread))
  289. return kthread_should_stop();
  290. else
  291. return 1;
  292. }
  293. _osal_inline_ INT32
  294. osal_thread_wait_for_event(P_OSAL_THREAD pThread, P_OSAL_EVENT pEvent, P_OSAL_EVENT_CHECKER pChecker)
  295. {
  296. /* P_DEV_WMT pDevWmt;*/
  297. if ((pThread) && (pThread->pThread) && (pEvent) && (pChecker)) {
  298. /* pDevWmt = (P_DEV_WMT)(pThread->pThreadData);*/
  299. return wait_event_interruptible(pEvent->waitQueue, (/*!RB_EMPTY(&pDevWmt->rActiveOpQ) || */
  300. osal_thread_should_stop(pThread)
  301. || (*pChecker) (pThread)));
  302. }
  303. return -1;
  304. }
  305. _osal_inline_ INT32 osal_thread_destroy(P_OSAL_THREAD pThread)
  306. {
  307. if (pThread && (pThread->pThread)) {
  308. kthread_stop(pThread->pThread);
  309. pThread->pThread = NULL;
  310. }
  311. return 0;
  312. }
  313. /*
  314. *OSAL layer Signal Opeartion related APIs
  315. *initialization
  316. *wait for signal
  317. *wait for signal timerout
  318. *raise signal
  319. *destroy a signal
  320. *
  321. */
  322. _osal_inline_ INT32 osal_signal_init(P_OSAL_SIGNAL pSignal)
  323. {
  324. if (pSignal) {
  325. init_completion(&pSignal->comp);
  326. return 0;
  327. } else {
  328. return -1;
  329. }
  330. }
  331. _osal_inline_ INT32 osal_wait_for_signal(P_OSAL_SIGNAL pSignal)
  332. {
  333. if (pSignal) {
  334. wait_for_completion_interruptible(&pSignal->comp);
  335. return 0;
  336. } else {
  337. return -1;
  338. }
  339. }
  340. _osal_inline_ INT32 osal_wait_for_signal_timeout(P_OSAL_SIGNAL pSignal)
  341. {
  342. /* return wait_for_completion_interruptible_timeout(&pSignal->comp, msecs_to_jiffies(pSignal->timeoutValue)); */
  343. /* [ChangeFeature][George] gps driver may be closed by -ERESTARTSYS.
  344. * Avoid using *interruptible" version in order to complete our jobs, such
  345. * as function off gracefully.
  346. */
  347. return wait_for_completion_timeout(&pSignal->comp, msecs_to_jiffies(pSignal->timeoutValue));
  348. }
  349. _osal_inline_ INT32 osal_raise_signal(P_OSAL_SIGNAL pSignal)
  350. {
  351. /* TODO:[FixMe][GeorgeKuo]: DO sanity check here!!! */
  352. complete(&pSignal->comp);
  353. return 0;
  354. }
  355. _osal_inline_ INT32 osal_signal_deinit(P_OSAL_SIGNAL pSignal)
  356. {
  357. /* TODO:[FixMe][GeorgeKuo]: DO sanity check here!!! */
  358. pSignal->timeoutValue = 0;
  359. return 0;
  360. }
  361. /*
  362. *OSAL layer Event Opeartion related APIs
  363. *initialization
  364. *wait for signal
  365. *wait for signal timerout
  366. *raise signal
  367. *destroy a signal
  368. *
  369. */
  370. INT32 osal_event_init(P_OSAL_EVENT pEvent)
  371. {
  372. init_waitqueue_head(&pEvent->waitQueue);
  373. return 0;
  374. }
  375. INT32 osal_wait_for_event(P_OSAL_EVENT pEvent, INT32(*condition) (PVOID), void *cond_pa)
  376. {
  377. return wait_event_interruptible(pEvent->waitQueue, condition(cond_pa));
  378. }
  379. INT32 osal_wait_for_event_timeout(P_OSAL_EVENT pEvent, INT32(*condition) (PVOID), void *cond_pa)
  380. {
  381. return wait_event_interruptible_timeout(pEvent->waitQueue, condition(cond_pa),
  382. msecs_to_jiffies(pEvent->timeoutValue));
  383. }
  384. INT32 osal_trigger_event(P_OSAL_EVENT pEvent)
  385. {
  386. INT32 ret = 0;
  387. wake_up_interruptible(&pEvent->waitQueue);
  388. return ret;
  389. }
  390. INT32 osal_event_deinit(P_OSAL_EVENT pEvent)
  391. {
  392. return 0;
  393. }
  394. _osal_inline_ long osal_wait_for_event_bit_set(P_OSAL_EVENT pEvent, unsigned long *pState, UINT32 bitOffset)
  395. {
  396. UINT32 ms = pEvent->timeoutValue;
  397. if (ms != 0) {
  398. return wait_event_interruptible_timeout(pEvent->waitQueue, test_bit(bitOffset, pState),
  399. msecs_to_jiffies(ms));
  400. } else {
  401. return wait_event_interruptible(pEvent->waitQueue, test_bit(bitOffset, pState));
  402. }
  403. }
  404. _osal_inline_ long osal_wait_for_event_bit_clr(P_OSAL_EVENT pEvent, unsigned long *pState, UINT32 bitOffset)
  405. {
  406. UINT32 ms = pEvent->timeoutValue;
  407. if (ms != 0) {
  408. return wait_event_interruptible_timeout(pEvent->waitQueue, !test_bit(bitOffset, pState),
  409. msecs_to_jiffies(ms));
  410. } else {
  411. return wait_event_interruptible(pEvent->waitQueue, !test_bit(bitOffset, pState));
  412. }
  413. }
  414. /*
  415. *bit test and set/clear operations APIs
  416. *
  417. *
  418. */
  419. #if OS_BIT_OPS_SUPPORT
  420. #define osal_bit_op_lock(x)
  421. #define osal_bit_op_unlock(x)
  422. #else
  423. _osal_inline_ INT32 osal_bit_op_lock(P_OSAL_UNSLEEPABLE_LOCK pLock)
  424. {
  425. return 0;
  426. }
  427. _osal_inline_ INT32 osal_bit_op_unlock(P_OSAL_UNSLEEPABLE_LOCK pLock)
  428. {
  429. return 0;
  430. }
  431. #endif
  432. _osal_inline_ INT32 osal_clear_bit(UINT32 bitOffset, P_OSAL_BIT_OP_VAR pData)
  433. {
  434. osal_bit_op_lock(&(pData->opLock));
  435. clear_bit(bitOffset, &pData->data);
  436. osal_bit_op_unlock(&(pData->opLock));
  437. return 0;
  438. }
  439. _osal_inline_ INT32 osal_set_bit(UINT32 bitOffset, P_OSAL_BIT_OP_VAR pData)
  440. {
  441. osal_bit_op_lock(&(pData->opLock));
  442. set_bit(bitOffset, &pData->data);
  443. osal_bit_op_unlock(&(pData->opLock));
  444. return 0;
  445. }
  446. _osal_inline_ INT32 osal_test_bit(UINT32 bitOffset, P_OSAL_BIT_OP_VAR pData)
  447. {
  448. UINT32 iRet = 0;
  449. osal_bit_op_lock(&(pData->opLock));
  450. iRet = test_bit(bitOffset, &pData->data);
  451. osal_bit_op_unlock(&(pData->opLock));
  452. return iRet;
  453. }
  454. _osal_inline_ INT32 osal_test_and_clear_bit(UINT32 bitOffset, P_OSAL_BIT_OP_VAR pData)
  455. {
  456. UINT32 iRet = 0;
  457. osal_bit_op_lock(&(pData->opLock));
  458. iRet = test_and_clear_bit(bitOffset, &pData->data);
  459. osal_bit_op_unlock(&(pData->opLock));
  460. return iRet;
  461. }
  462. _osal_inline_ INT32 osal_test_and_set_bit(UINT32 bitOffset, P_OSAL_BIT_OP_VAR pData)
  463. {
  464. UINT32 iRet = 0;
  465. osal_bit_op_lock(&(pData->opLock));
  466. iRet = test_and_set_bit(bitOffset, &pData->data);
  467. osal_bit_op_unlock(&(pData->opLock));
  468. return iRet;
  469. }
  470. /*
  471. *tiemr operations APIs
  472. *create
  473. *stop
  474. * modify
  475. *create
  476. *delete
  477. *
  478. */
  479. INT32 osal_timer_create(P_OSAL_TIMER pTimer)
  480. {
  481. struct timer_list *timer = &pTimer->timer;
  482. init_timer(timer);
  483. timer->function = pTimer->timeoutHandler;
  484. timer->data = (unsigned long)pTimer->timeroutHandlerData;
  485. return 0;
  486. }
  487. INT32 osal_timer_start(P_OSAL_TIMER pTimer, UINT32 ms)
  488. {
  489. struct timer_list *timer = &pTimer->timer;
  490. timer->expires = jiffies + (ms / (1000 / HZ));
  491. add_timer(timer);
  492. return 0;
  493. }
  494. INT32 osal_timer_stop(P_OSAL_TIMER pTimer)
  495. {
  496. struct timer_list *timer = &pTimer->timer;
  497. del_timer(timer);
  498. return 0;
  499. }
  500. INT32 osal_timer_stop_sync(P_OSAL_TIMER pTimer)
  501. {
  502. struct timer_list *timer = &pTimer->timer;
  503. del_timer_sync(timer);
  504. return 0;
  505. }
  506. INT32 osal_timer_modify(P_OSAL_TIMER pTimer, UINT32 ms)
  507. {
  508. mod_timer(&pTimer->timer, jiffies + (ms) / (1000 / HZ));
  509. return 0;
  510. }
  511. INT32 _osal_fifo_init(OSAL_FIFO *pFifo, UINT8 *buf, UINT32 size)
  512. {
  513. struct kfifo *fifo = NULL;
  514. INT32 ret = -1;
  515. if (!pFifo) {
  516. pr_err("pFifo must be !NULL\n");
  517. return -1;
  518. }
  519. if (pFifo->pFifoBody) {
  520. pr_err("pFifo->pFifoBody must be NULL\n");
  521. pr_err("pFifo(0x%p), pFifo->pFifoBody(0x%p)\n", pFifo, pFifo->pFifoBody);
  522. return -1;
  523. }
  524. fifo = kzalloc(sizeof(struct kfifo), GFP_ATOMIC);
  525. if (!buf) {
  526. /*fifo's buffer is not ready, we allocate automatically */
  527. ret = kfifo_alloc(fifo, size, /*GFP_KERNEL */ GFP_ATOMIC);
  528. } else {
  529. if (is_power_of_2(size)) {
  530. kfifo_init(fifo, buf, size);
  531. ret = 0;
  532. } else {
  533. kfifo_free(fifo);
  534. fifo = NULL;
  535. ret = -1;
  536. }
  537. }
  538. pFifo->pFifoBody = fifo;
  539. return (ret < 0) ? (-1) : (0);
  540. }
  541. INT32 _osal_fifo_deinit(OSAL_FIFO *pFifo)
  542. {
  543. struct kfifo *fifo = NULL;
  544. if (!pFifo || !pFifo->pFifoBody) {
  545. pr_err("%s:pFifo = NULL or pFifo->pFifoBody = NULL, error\n", __func__);
  546. return -1;
  547. }
  548. fifo = (struct kfifo *)pFifo->pFifoBody;
  549. if (fifo)
  550. kfifo_free(fifo);
  551. return 0;
  552. }
  553. INT32 _osal_fifo_size(OSAL_FIFO *pFifo)
  554. {
  555. struct kfifo *fifo = NULL;
  556. INT32 ret = 0;
  557. if (!pFifo || !pFifo->pFifoBody) {
  558. pr_err("%s:pFifo = NULL or pFifo->pFifoBody = NULL, error\n", __func__);
  559. return -1;
  560. }
  561. fifo = (struct kfifo *)pFifo->pFifoBody;
  562. if (fifo)
  563. ret = kfifo_size(fifo);
  564. return ret;
  565. }
  566. /*returns unused bytes in fifo*/
  567. INT32 _osal_fifo_avail_size(OSAL_FIFO *pFifo)
  568. {
  569. struct kfifo *fifo = NULL;
  570. INT32 ret = 0;
  571. if (!pFifo || !pFifo->pFifoBody) {
  572. pr_err("%s:pFifo = NULL or pFifo->pFifoBody = NULL, error\n", __func__);
  573. return -1;
  574. }
  575. fifo = (struct kfifo *)pFifo->pFifoBody;
  576. if (fifo)
  577. ret = kfifo_avail(fifo);
  578. return ret;
  579. }
  580. /*returns used bytes in fifo*/
  581. INT32 _osal_fifo_len(OSAL_FIFO *pFifo)
  582. {
  583. struct kfifo *fifo = NULL;
  584. INT32 ret = 0;
  585. if (!pFifo || !pFifo->pFifoBody) {
  586. pr_err("%s:pFifo = NULL or pFifo->pFifoBody = NULL, error\n", __func__);
  587. return -1;
  588. }
  589. fifo = (struct kfifo *)pFifo->pFifoBody;
  590. if (fifo)
  591. ret = kfifo_len(fifo);
  592. return ret;
  593. }
  594. INT32 _osal_fifo_is_empty(OSAL_FIFO *pFifo)
  595. {
  596. struct kfifo *fifo = NULL;
  597. INT32 ret = 0;
  598. if (!pFifo || !pFifo->pFifoBody) {
  599. pr_err("%s:pFifo = NULL or pFifo->pFifoBody = NULL, error\n", __func__);
  600. return -1;
  601. }
  602. fifo = (struct kfifo *)pFifo->pFifoBody;
  603. if (fifo)
  604. ret = kfifo_is_empty(fifo);
  605. return ret;
  606. }
  607. INT32 _osal_fifo_is_full(OSAL_FIFO *pFifo)
  608. {
  609. struct kfifo *fifo = NULL;
  610. INT32 ret = 0;
  611. if (!pFifo || !pFifo->pFifoBody) {
  612. pr_err("%s:pFifo = NULL or pFifo->pFifoBody = NULL, error\n", __func__);
  613. return -1;
  614. }
  615. fifo = (struct kfifo *)pFifo->pFifoBody;
  616. if (fifo)
  617. ret = kfifo_is_full(fifo);
  618. return ret;
  619. }
  620. INT32 _osal_fifo_data_in(OSAL_FIFO *pFifo, const VOID *buf, UINT32 len)
  621. {
  622. struct kfifo *fifo = NULL;
  623. INT32 ret = 0;
  624. if (!pFifo || !pFifo->pFifoBody) {
  625. pr_err("%s:pFifo = NULL or pFifo->pFifoBody = NULL, error\n", __func__);
  626. return -1;
  627. }
  628. fifo = (struct kfifo *)pFifo->pFifoBody;
  629. if (fifo && buf && (len <= _osal_fifo_avail_size(pFifo))) {
  630. ret = kfifo_in(fifo, buf, len);
  631. } else {
  632. pr_err("%s: kfifo_in, error, len = %d, _osal_fifo_avail_size = %d, buf=%p\n",
  633. __func__, len, _osal_fifo_avail_size(pFifo), buf);
  634. ret = 0;
  635. }
  636. return ret;
  637. }
  638. INT32 _osal_fifo_data_out(OSAL_FIFO *pFifo, void *buf, UINT32 len)
  639. {
  640. struct kfifo *fifo = NULL;
  641. INT32 ret = 0;
  642. if (!pFifo || !pFifo->pFifoBody) {
  643. pr_err("%s:pFifo = NULL or pFifo->pFifoBody = NULL, error\n", __func__);
  644. return -1;
  645. }
  646. fifo = (struct kfifo *)pFifo->pFifoBody;
  647. if (fifo && buf && (len <= _osal_fifo_len(pFifo))) {
  648. ret = kfifo_out(fifo, buf, len);
  649. } else {
  650. pr_err("%s: kfifo_out, error, len = %d, osal_fifo_len = %d, buf=%p\n",
  651. __func__, len, _osal_fifo_len(pFifo), buf);
  652. ret = 0;
  653. }
  654. return ret;
  655. }
  656. INT32 _osal_fifo_reset(OSAL_FIFO *pFifo)
  657. {
  658. struct kfifo *fifo = NULL;
  659. if (!pFifo || !pFifo->pFifoBody) {
  660. pr_err("%s:pFifo = NULL or pFifo->pFifoBody = NULL, error\n", __func__);
  661. return -1;
  662. }
  663. fifo = (struct kfifo *)pFifo->pFifoBody;
  664. if (fifo)
  665. kfifo_reset(fifo);
  666. return 0;
  667. }
  668. INT32 osal_fifo_init(P_OSAL_FIFO pFifo, UINT8 *buffer, UINT32 size)
  669. {
  670. if (!pFifo) {
  671. pr_err("%s:pFifo = NULL, error\n", __func__);
  672. return -1;
  673. }
  674. pFifo->FifoInit = _osal_fifo_init;
  675. pFifo->FifoDeInit = _osal_fifo_deinit;
  676. pFifo->FifoSz = _osal_fifo_size;
  677. pFifo->FifoAvailSz = _osal_fifo_avail_size;
  678. pFifo->FifoLen = _osal_fifo_len;
  679. pFifo->FifoIsEmpty = _osal_fifo_is_empty;
  680. pFifo->FifoIsFull = _osal_fifo_is_full;
  681. pFifo->FifoDataIn = _osal_fifo_data_in;
  682. pFifo->FifoDataOut = _osal_fifo_data_out;
  683. pFifo->FifoReset = _osal_fifo_reset;
  684. if (NULL != pFifo->pFifoBody) {
  685. pr_err("%s:Because pFifo room is avialable, we clear the room and allocate them again.\n", __func__);
  686. pFifo->FifoDeInit(pFifo->pFifoBody);
  687. pFifo->pFifoBody = NULL;
  688. }
  689. pFifo->FifoInit(pFifo, buffer, size);
  690. return 0;
  691. }
  692. VOID osal_fifo_deinit(P_OSAL_FIFO pFifo)
  693. {
  694. if (pFifo)
  695. pFifo->FifoDeInit(pFifo);
  696. else {
  697. pr_err("%s:pFifo = NULL, error\n", __func__);
  698. return;
  699. }
  700. kfree(pFifo->pFifoBody);
  701. }
  702. INT32 osal_fifo_reset(P_OSAL_FIFO pFifo)
  703. {
  704. INT32 ret = -1;
  705. if (pFifo) {
  706. ret = pFifo->FifoReset(pFifo);
  707. } else {
  708. pr_err("%s:pFifo = NULL, error\n", __func__);
  709. ret = -1;
  710. }
  711. return ret;
  712. }
  713. UINT32 osal_fifo_in(P_OSAL_FIFO pFifo, PUINT8 buffer, UINT32 size)
  714. {
  715. UINT32 ret = 0;
  716. if (pFifo) {
  717. ret = pFifo->FifoDataIn(pFifo, buffer, size);
  718. } else {
  719. pr_err("%s:pFifo = NULL, error\n", __func__);
  720. ret = 0;
  721. }
  722. return ret;
  723. }
  724. UINT32 osal_fifo_out(P_OSAL_FIFO pFifo, PUINT8 buffer, UINT32 size)
  725. {
  726. UINT32 ret = 0;
  727. if (pFifo) {
  728. ret = pFifo->FifoDataOut(pFifo, buffer, size);
  729. } else {
  730. pr_err("%s:pFifo = NULL, error\n", __func__);
  731. ret = 0;
  732. }
  733. return ret;
  734. }
  735. UINT32 osal_fifo_len(P_OSAL_FIFO pFifo)
  736. {
  737. UINT32 ret = 0;
  738. if (pFifo) {
  739. ret = pFifo->FifoLen(pFifo);
  740. } else {
  741. pr_err("%s:pFifo = NULL, error\n", __func__);
  742. ret = 0;
  743. }
  744. return ret;
  745. }
  746. UINT32 osal_fifo_sz(P_OSAL_FIFO pFifo)
  747. {
  748. UINT32 ret = 0;
  749. if (pFifo) {
  750. ret = pFifo->FifoSz(pFifo);
  751. } else {
  752. pr_err("%s:pFifo = NULL, error\n", __func__);
  753. ret = 0;
  754. }
  755. return ret;
  756. }
  757. UINT32 osal_fifo_avail(P_OSAL_FIFO pFifo)
  758. {
  759. UINT32 ret = 0;
  760. if (pFifo) {
  761. ret = pFifo->FifoAvailSz(pFifo);
  762. } else {
  763. pr_err("%s:pFifo = NULL, error\n", __func__);
  764. ret = 0;
  765. }
  766. return ret;
  767. }
  768. UINT32 osal_fifo_is_empty(P_OSAL_FIFO pFifo)
  769. {
  770. UINT32 ret = 0;
  771. if (pFifo) {
  772. ret = pFifo->FifoIsEmpty(pFifo);
  773. } else {
  774. pr_err("%s:pFifo = NULL, error\n", __func__);
  775. ret = 0;
  776. }
  777. return ret;
  778. }
  779. UINT32 osal_fifo_is_full(P_OSAL_FIFO pFifo)
  780. {
  781. UINT32 ret = 0;
  782. if (pFifo) {
  783. ret = pFifo->FifoIsFull(pFifo);
  784. } else {
  785. pr_err("%s:pFifo = NULL, error\n", __func__);
  786. ret = 0;
  787. }
  788. return ret;
  789. }
  790. INT32 osal_wake_lock_init(P_OSAL_WAKE_LOCK pLock)
  791. {
  792. if (!pLock)
  793. return -1;
  794. #ifdef CONFIG_PM_WAKELOCKS
  795. wakeup_source_init(&pLock->wake_lock, pLock->name);
  796. #else
  797. wake_lock_init(&pLock->wake_lock, WAKE_LOCK_SUSPEND, pLock->name);
  798. #endif
  799. return 0;
  800. }
  801. INT32 osal_wake_lock_deinit(P_OSAL_WAKE_LOCK pLock)
  802. {
  803. if (!pLock)
  804. return -1;
  805. #ifdef CONFIG_PM_WAKELOCKS
  806. wakeup_source_trash(&pLock->wake_lock);
  807. #else
  808. wake_lock_destroy(&pLock->wake_lock);
  809. #endif
  810. return 0;
  811. }
  812. INT32 osal_wake_lock(P_OSAL_WAKE_LOCK pLock)
  813. {
  814. if (!pLock)
  815. return -1;
  816. #ifdef CONFIG_PM_WAKELOCKS
  817. __pm_stay_awake(&pLock->wake_lock);
  818. #else
  819. wake_lock(&pLock->wake_lock);
  820. #endif
  821. return 0;
  822. }
  823. INT32 osal_wake_unlock(P_OSAL_WAKE_LOCK pLock)
  824. {
  825. if (!pLock)
  826. return -1;
  827. #ifdef CONFIG_PM_WAKELOCKS
  828. __pm_relax(&pLock->wake_lock);
  829. #else
  830. wake_unlock(&pLock->wake_lock);
  831. #endif
  832. return 0;
  833. }
  834. INT32 osal_wake_lock_count(P_OSAL_WAKE_LOCK pLock)
  835. {
  836. INT32 count = 0;
  837. if (!pLock)
  838. return -1;
  839. #ifdef CONFIG_PM_WAKELOCKS
  840. count = pLock->wake_lock.active;
  841. #else
  842. count = wake_lock_active(&pLock->wake_lock);
  843. #endif
  844. return count;
  845. }
  846. /*
  847. *sleepable lock operations APIs
  848. *init
  849. *lock
  850. *unlock
  851. *destroy
  852. *
  853. */
  854. #if !defined(CONFIG_PROVE_LOCKING)
  855. INT32 osal_unsleepable_lock_init(P_OSAL_UNSLEEPABLE_LOCK pUSL)
  856. {
  857. spin_lock_init(&(pUSL->lock));
  858. return 0;
  859. }
  860. #endif
  861. INT32 osal_lock_unsleepable_lock(P_OSAL_UNSLEEPABLE_LOCK pUSL)
  862. {
  863. spin_lock_irqsave(&(pUSL->lock), pUSL->flag);
  864. return 0;
  865. }
  866. INT32 osal_unlock_unsleepable_lock(P_OSAL_UNSLEEPABLE_LOCK pUSL)
  867. {
  868. spin_unlock_irqrestore(&(pUSL->lock), pUSL->flag);
  869. return 0;
  870. }
  871. INT32 osal_unsleepable_lock_deinit(P_OSAL_UNSLEEPABLE_LOCK pUSL)
  872. {
  873. return 0;
  874. }
  875. /*
  876. *unsleepable operations APIs
  877. *init
  878. *lock
  879. *unlock
  880. *destroy
  881. *
  882. */
  883. #if !defined(CONFIG_PROVE_LOCKING)
  884. INT32 osal_sleepable_lock_init(P_OSAL_SLEEPABLE_LOCK pSL)
  885. {
  886. mutex_init(&pSL->lock);
  887. return 0;
  888. }
  889. #endif
  890. INT32 osal_lock_sleepable_lock(P_OSAL_SLEEPABLE_LOCK pSL)
  891. {
  892. return mutex_lock_killable(&pSL->lock);
  893. }
  894. INT32 osal_unlock_sleepable_lock(P_OSAL_SLEEPABLE_LOCK pSL)
  895. {
  896. mutex_unlock(&pSL->lock);
  897. return 0;
  898. }
  899. INT32 osal_sleepable_lock_deinit(P_OSAL_SLEEPABLE_LOCK pSL)
  900. {
  901. mutex_destroy(&pSL->lock);
  902. return 0;
  903. }
  904. INT32 osal_sleep_ms(UINT32 ms)
  905. {
  906. msleep(ms);
  907. return 0;
  908. }
  909. INT32 osal_udelay(UINT32 us)
  910. {
  911. udelay(us);
  912. return 0;
  913. }
  914. INT32 osal_gettimeofday(PINT32 sec, PINT32 usec)
  915. {
  916. INT32 ret = 0;
  917. struct timeval now;
  918. do_gettimeofday(&now);
  919. if (sec != NULL)
  920. *sec = now.tv_sec;
  921. else
  922. ret = -1;
  923. if (usec != NULL)
  924. *usec = now.tv_usec;
  925. else
  926. ret = -1;
  927. return ret;
  928. }
  929. INT32 osal_printtimeofday(const PUINT8 prefix)
  930. {
  931. INT32 ret;
  932. INT32 sec;
  933. INT32 usec;
  934. ret = osal_gettimeofday(&sec, &usec);
  935. ret += osal_dbg_print("%s>sec=%d, usec=%d\n", prefix, sec, usec);
  936. return ret;
  937. }
  938. VOID osal_buffer_dump(const UINT8 *buf, const UINT8 *title, const UINT32 len, const UINT32 limit)
  939. {
  940. INT32 k;
  941. UINT32 dump_len;
  942. pr_warn("start of dump>[%s] len=%d, limit=%d,", title, len, limit);
  943. dump_len = ((0 != limit) && (len > limit)) ? limit : len;
  944. #if 0
  945. if (limit != 0)
  946. len = (len > limit) ? (limit) : (len);
  947. #endif
  948. for (k = 0; k < dump_len; k++) {
  949. if ((k != 0) && (k % 16 == 0))
  950. pr_cont("\n");
  951. pr_cont("0x%02x ", buf[k]);
  952. }
  953. pr_warn("<end of dump\n");
  954. }
  955. UINT32 osal_op_get_id(P_OSAL_OP pOp)
  956. {
  957. return (pOp) ? pOp->op.opId : 0xFFFFFFFF;
  958. }
  959. MTK_WCN_BOOL osal_op_is_wait_for_signal(P_OSAL_OP pOp)
  960. {
  961. return (pOp && pOp->signal.timeoutValue) ? MTK_WCN_BOOL_TRUE : MTK_WCN_BOOL_FALSE;
  962. }
  963. VOID osal_op_raise_signal(P_OSAL_OP pOp, INT32 result)
  964. {
  965. if (pOp) {
  966. pOp->result = result;
  967. osal_raise_signal(&pOp->signal);
  968. }
  969. }
  970. VOID osal_set_op_result(P_OSAL_OP pOp, INT32 result)
  971. {
  972. if (pOp)
  973. pOp->result = result;
  974. }