fm_utils.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. #include <linux/sched.h>
  2. #include <linux/slab.h>
  3. #include <linux/version.h>
  4. #include <linux/interrupt.h>
  5. #include <linux/wait.h>
  6. #include <asm/uaccess.h>
  7. #include <linux/semaphore.h>
  8. #include <linux/timer.h>
  9. #include <linux/delay.h>
  10. #include "fm_typedef.h"
  11. #include "fm_dbg.h"
  12. #include "fm_err.h"
  13. #include "fm_stdlib.h"
  14. #include "fm_utils.h"
  15. static fm_u32 fm_event_send(struct fm_flag_event *thiz, fm_u32 mask)
  16. {
  17. thiz->flag |= mask;
  18. /* WCN_DBG(FM_DBG|MAIN, "%s set 0x%08x\n", thiz->name, thiz->flag); */
  19. wake_up((wait_queue_head_t *) (thiz->priv));
  20. return thiz->flag;
  21. }
  22. static fm_s32 fm_event_wait(struct fm_flag_event *thiz, fm_u32 mask)
  23. {
  24. return wait_event_interruptible(*(wait_queue_head_t *) (thiz->priv), ((thiz->flag & mask) == mask));
  25. }
  26. /**
  27. * fm_event_check - sleep until a condition gets true or a timeout elapses
  28. * @thiz: the pointer of current object
  29. * @mask: bitmap in fm_u32
  30. * @timeout: timeout, in jiffies
  31. *
  32. * fm_event_set() has to be called after changing any variable that could
  33. * change the result of the wait condition.
  34. *
  35. * The function returns 0 if the @timeout elapsed, and the remaining
  36. * jiffies if the condition evaluated to true before the timeout elapsed.
  37. */
  38. long fm_event_wait_timeout(struct fm_flag_event *thiz, fm_u32 mask, long timeout)
  39. {
  40. return wait_event_timeout(*((wait_queue_head_t *) (thiz->priv)), ((thiz->flag & mask) == mask), timeout * HZ);
  41. }
  42. static fm_u32 fm_event_clr(struct fm_flag_event *thiz, fm_u32 mask)
  43. {
  44. thiz->flag &= ~mask;
  45. /* WCN_DBG(FM_DBG|MAIN, "%s clr 0x%08x\n", thiz->name, thiz->flag); */
  46. return thiz->flag;
  47. }
  48. static fm_u32 fm_event_get(struct fm_flag_event *thiz)
  49. {
  50. return thiz->flag;
  51. }
  52. static fm_u32 fm_event_rst(struct fm_flag_event *thiz)
  53. {
  54. return thiz->flag = 0;
  55. }
  56. struct fm_flag_event *fm_flag_event_create(const fm_s8 *name)
  57. {
  58. struct fm_flag_event *tmp;
  59. wait_queue_head_t *wq;
  60. tmp = fm_zalloc(sizeof(struct fm_flag_event));
  61. if (!tmp) {
  62. WCN_DBG(FM_ALT | MAIN, "fm_zalloc(fm_event) -ENOMEM\n");
  63. return NULL;
  64. }
  65. wq = fm_zalloc(sizeof(wait_queue_head_t));
  66. if (!wq) {
  67. WCN_DBG(FM_ALT | MAIN, "fm_zalloc(wait_queue_head_t) -ENOMEM\n");
  68. fm_free(tmp);
  69. return NULL;
  70. }
  71. fm_memcpy(tmp->name, name, (strlen(name) > FM_NAME_MAX) ? (FM_NAME_MAX) : (strlen(name)));
  72. tmp->priv = wq;
  73. init_waitqueue_head(wq);
  74. tmp->ref = 0;
  75. tmp->send = fm_event_send;
  76. tmp->wait = fm_event_wait;
  77. tmp->wait_timeout = fm_event_wait_timeout;
  78. tmp->clr = fm_event_clr;
  79. tmp->get = fm_event_get;
  80. tmp->rst = fm_event_rst;
  81. tmp->rst(tmp); /* set flag to 0x00000000 */
  82. return tmp;
  83. }
  84. fm_s32 fm_flag_event_get(struct fm_flag_event *thiz)
  85. {
  86. if (thiz == NULL) {
  87. pr_err("%s,invalid pointer\n", __func__);
  88. return -FM_EPARA;
  89. }
  90. thiz->ref++;
  91. return 0;
  92. }
  93. fm_s32 fm_flag_event_put(struct fm_flag_event *thiz)
  94. {
  95. if (thiz == NULL) {
  96. pr_err("%s,invalid pointer\n", __func__);
  97. return -FM_EPARA;
  98. }
  99. thiz->ref--;
  100. if (thiz->ref == 0) {
  101. fm_free(thiz->priv);
  102. fm_free(thiz);
  103. return 0;
  104. } else if (thiz->ref > 0) {
  105. return -FM_EINUSE;
  106. } else {
  107. return -FM_EPARA;
  108. }
  109. }
  110. /* fm lock methods */
  111. static fm_s32 fm_lock_try(struct fm_lock *thiz, fm_s32 retryCnt)
  112. {
  113. fm_s32 retry_cnt = 0;
  114. struct semaphore *sem;
  115. struct task_struct *task = current;
  116. if (thiz == NULL) {
  117. pr_err("%s,invalid pointer\n", __func__);
  118. return -FM_EPARA;
  119. }
  120. if (thiz->priv == NULL) {
  121. pr_err("%s,invalid pointer\n", __func__);
  122. return -FM_EPARA;
  123. }
  124. while (down_trylock((struct semaphore *)thiz->priv)) {
  125. WCN_DBG(FM_WAR | MAIN, "down_trylock failed\n");
  126. if (++retry_cnt < retryCnt) {
  127. WCN_DBG(FM_WAR | MAIN, "[retryCnt=%d]\n", retry_cnt);
  128. msleep_interruptible(50);
  129. continue;
  130. } else {
  131. WCN_DBG(FM_CRT | MAIN, "down_trylock retry failed\n");
  132. return -FM_ELOCK;
  133. }
  134. }
  135. sem = (struct semaphore *)thiz->priv;
  136. FM_LOG_DBG(MAIN, "%s --->trylock, cnt=%d, pid=%d\n", thiz->name, (int)sem->count, task->pid);
  137. return 0;
  138. }
  139. /* fm try lock methods */
  140. static fm_s32 fm_lock_lock(struct fm_lock *thiz)
  141. {
  142. struct semaphore *sem;
  143. struct task_struct *task = current;
  144. if (thiz == NULL) {
  145. pr_err("%s,invalid pointer\n", __func__);
  146. return -FM_EPARA;
  147. }
  148. if (thiz->priv == NULL) {
  149. pr_err("%s,invalid pointer\n", __func__);
  150. return -FM_EPARA;
  151. }
  152. if (down_interruptible((struct semaphore *)thiz->priv)) {
  153. WCN_DBG(FM_CRT | MAIN, "get mutex failed\n");
  154. return -FM_ELOCK;
  155. }
  156. sem = (struct semaphore *)thiz->priv;
  157. FM_LOG_DBG(MAIN, "%s --->lock, cnt=%d, pid=%d\n", thiz->name, (int)sem->count, task->pid);
  158. return 0;
  159. }
  160. static fm_s32 fm_lock_unlock(struct fm_lock *thiz)
  161. {
  162. struct semaphore *sem;
  163. struct task_struct *task = current;
  164. if (thiz == NULL) {
  165. pr_err("%s,invalid pointer\n", __func__);
  166. return -FM_EPARA;
  167. }
  168. if (thiz->priv == NULL) {
  169. pr_err("%s,invalid pointer\n", __func__);
  170. return -FM_EPARA;
  171. }
  172. sem = (struct semaphore *)thiz->priv;
  173. FM_LOG_DBG(MAIN, "%s <---unlock, cnt=%d, pid=%d\n", thiz->name, (int)sem->count + 1, task->pid);
  174. up((struct semaphore *)thiz->priv);
  175. return 0;
  176. }
  177. struct fm_lock *fm_lock_create(const fm_s8 *name)
  178. {
  179. struct fm_lock *tmp;
  180. struct semaphore *mutex;
  181. tmp = fm_zalloc(sizeof(struct fm_lock));
  182. if (!tmp) {
  183. WCN_DBG(FM_ALT | MAIN, "fm_zalloc(fm_lock) -ENOMEM\n");
  184. return NULL;
  185. }
  186. mutex = fm_zalloc(sizeof(struct semaphore));
  187. if (!mutex) {
  188. WCN_DBG(FM_ALT | MAIN, "fm_zalloc(struct semaphore) -ENOMEM\n");
  189. fm_free(tmp);
  190. return NULL;
  191. }
  192. tmp->priv = mutex;
  193. sema_init(mutex, 1);
  194. tmp->ref = 0;
  195. fm_memcpy(tmp->name, name, (strlen(name) > FM_NAME_MAX) ? (FM_NAME_MAX) : (strlen(name)));
  196. tmp->lock = fm_lock_lock;
  197. tmp->trylock = fm_lock_try;
  198. tmp->unlock = fm_lock_unlock;
  199. return tmp;
  200. }
  201. fm_s32 fm_lock_get(struct fm_lock *thiz)
  202. {
  203. if (thiz == NULL) {
  204. pr_err("%s,invalid pointer\n", __func__);
  205. return -FM_EPARA;
  206. }
  207. thiz->ref++;
  208. return 0;
  209. }
  210. fm_s32 fm_lock_put(struct fm_lock *thiz)
  211. {
  212. if (thiz == NULL) {
  213. pr_err("%s,invalid pointer\n", __func__);
  214. return -FM_EPARA;
  215. }
  216. thiz->ref--;
  217. if (thiz->ref == 0) {
  218. fm_free(thiz->priv);
  219. fm_free(thiz);
  220. return 0;
  221. } else if (thiz->ref > 0) {
  222. return -FM_EINUSE;
  223. } else {
  224. return -FM_EPARA;
  225. }
  226. }
  227. /* fm lock methods */
  228. static fm_s32 fm_spin_lock_lock(struct fm_lock *thiz)
  229. {
  230. struct task_struct *task = current;
  231. if (thiz == NULL) {
  232. pr_err("%s,invalid pointer\n", __func__);
  233. return -FM_EPARA;
  234. }
  235. if (thiz->priv == NULL) {
  236. pr_err("%s,invalid pointer\n", __func__);
  237. return -FM_EPARA;
  238. }
  239. spin_lock_bh((spinlock_t *) thiz->priv);
  240. FM_LOG_DBG(MAIN, "%s --->lock pid=%d\n", thiz->name, task->pid);
  241. return 0;
  242. }
  243. static fm_s32 fm_spin_lock_unlock(struct fm_lock *thiz)
  244. {
  245. struct task_struct *task = current;
  246. if (thiz == NULL) {
  247. pr_err("%s,invalid pointer\n", __func__);
  248. return -FM_EPARA;
  249. }
  250. if (thiz->priv == NULL) {
  251. pr_err("%s,invalid pointer\n", __func__);
  252. return -FM_EPARA;
  253. }
  254. FM_LOG_DBG(MAIN, "%s <---unlock, pid=%d\n", thiz->name, task->pid);
  255. spin_unlock_bh((spinlock_t *) thiz->priv);
  256. return 0;
  257. }
  258. struct fm_lock *fm_spin_lock_create(const fm_s8 *name)
  259. {
  260. struct fm_lock *tmp;
  261. spinlock_t *spin_lock;
  262. tmp = fm_zalloc(sizeof(struct fm_lock));
  263. if (!tmp) {
  264. WCN_DBG(FM_ALT | MAIN, "fm_zalloc(fm_lock) -ENOMEM\n");
  265. return NULL;
  266. }
  267. spin_lock = fm_zalloc(sizeof(spinlock_t));
  268. if (!spin_lock) {
  269. WCN_DBG(FM_ALT | MAIN, "fm_zalloc(spinlock_t) -ENOMEM\n");
  270. fm_free(tmp);
  271. return NULL;
  272. }
  273. tmp->priv = spin_lock;
  274. spin_lock_init(spin_lock);
  275. tmp->ref = 0;
  276. fm_memcpy(tmp->name, name, (strlen(name) > FM_NAME_MAX) ? (FM_NAME_MAX) : (strlen(name)));
  277. tmp->lock = fm_spin_lock_lock;
  278. tmp->unlock = fm_spin_lock_unlock;
  279. return tmp;
  280. }
  281. fm_s32 fm_spin_lock_get(struct fm_lock *thiz)
  282. {
  283. if (thiz == NULL) {
  284. pr_err("%s,invalid pointer\n", __func__);
  285. return -FM_EPARA;
  286. }
  287. thiz->ref++;
  288. return 0;
  289. }
  290. fm_s32 fm_spin_lock_put(struct fm_lock *thiz)
  291. {
  292. if (thiz == NULL) {
  293. pr_err("%s,invalid pointer\n", __func__);
  294. return -FM_EPARA;
  295. }
  296. thiz->ref--;
  297. if (thiz->ref == 0) {
  298. fm_free(thiz->priv);
  299. fm_free(thiz);
  300. return 0;
  301. } else if (thiz->ref > 0) {
  302. return -FM_EINUSE;
  303. } else {
  304. return -FM_EPARA;
  305. }
  306. }
  307. /*
  308. * fm timer
  309. *
  310. */
  311. static fm_s32 fm_timer_init(struct fm_timer *thiz, void (*timeout) (unsigned long data),
  312. unsigned long data, signed long time, fm_s32 flag)
  313. {
  314. struct timer_list *timerlist = (struct timer_list *)thiz->priv;
  315. thiz->flag = flag;
  316. thiz->flag &= ~FM_TIMER_FLAG_ACTIVATED;
  317. thiz->timeout_func = timeout;
  318. thiz->data = data;
  319. thiz->timeout_ms = time;
  320. timerlist->expires = jiffies + (thiz->timeout_ms) / (1000 / HZ);
  321. timerlist->function = thiz->timeout_func;
  322. timerlist->data = (unsigned long)thiz->data;
  323. return 0;
  324. }
  325. static fm_s32 fm_timer_start(struct fm_timer *thiz)
  326. {
  327. struct timer_list *timerlist = (struct timer_list *)thiz->priv;
  328. thiz->flag |= FM_TIMER_FLAG_ACTIVATED;
  329. mod_timer(timerlist, jiffies + (thiz->timeout_ms) / (1000 / HZ));
  330. return 0;
  331. }
  332. static fm_s32 fm_timer_update(struct fm_timer *thiz)
  333. {
  334. struct timer_list *timerlist = (struct timer_list *)thiz->priv;
  335. if (thiz->flag & FM_TIMER_FLAG_ACTIVATED) {
  336. mod_timer(timerlist, jiffies + (thiz->timeout_ms) / (1000 / HZ));
  337. return 0;
  338. } else {
  339. return 1;
  340. }
  341. }
  342. static fm_s32 fm_timer_stop(struct fm_timer *thiz)
  343. {
  344. struct timer_list *timerlist = (struct timer_list *)thiz->priv;
  345. thiz->flag &= ~FM_TIMER_FLAG_ACTIVATED;
  346. del_timer(timerlist);
  347. return 0;
  348. }
  349. static fm_s32 fm_timer_control(struct fm_timer *thiz, enum fm_timer_ctrl cmd, void *arg)
  350. {
  351. return 0;
  352. }
  353. struct fm_timer *fm_timer_create(const fm_s8 *name)
  354. {
  355. struct fm_timer *tmp;
  356. struct timer_list *timerlist;
  357. tmp = fm_zalloc(sizeof(struct fm_timer));
  358. if (!tmp) {
  359. WCN_DBG(FM_ALT | MAIN, "fm_zalloc(fm_timer) -ENOMEM\n");
  360. return NULL;
  361. }
  362. timerlist = fm_zalloc(sizeof(struct timer_list));
  363. if (!timerlist) {
  364. WCN_DBG(FM_ALT | MAIN, "fm_zalloc(struct timer_list) -ENOMEM\n");
  365. fm_free(tmp);
  366. return NULL;
  367. }
  368. init_timer(timerlist);
  369. fm_memcpy(tmp->name, name, (strlen(name) > FM_NAME_MAX) ? (FM_NAME_MAX) : (strlen(name)));
  370. tmp->priv = timerlist;
  371. tmp->ref = 0;
  372. tmp->init = fm_timer_init;
  373. tmp->start = fm_timer_start;
  374. tmp->stop = fm_timer_stop;
  375. tmp->update = fm_timer_update;
  376. tmp->control = fm_timer_control;
  377. return tmp;
  378. }
  379. fm_s32 fm_timer_get(struct fm_timer *thiz)
  380. {
  381. if (thiz == NULL) {
  382. pr_err("%s,invalid pointer\n", __func__);
  383. return -FM_EPARA;
  384. }
  385. thiz->ref++;
  386. return 0;
  387. }
  388. fm_s32 fm_timer_put(struct fm_timer *thiz)
  389. {
  390. if (thiz == NULL) {
  391. pr_err("%s,invalid pointer\n", __func__);
  392. return -FM_EPARA;
  393. }
  394. thiz->ref--;
  395. if (thiz->ref == 0) {
  396. fm_free(thiz->priv);
  397. fm_free(thiz);
  398. return 0;
  399. } else if (thiz->ref > 0) {
  400. return -FM_EINUSE;
  401. } else {
  402. return -FM_EPARA;
  403. }
  404. }
  405. /*
  406. * FM work thread mechanism
  407. */
  408. static fm_s32 fm_work_init(struct fm_work *thiz, void (*work_func) (unsigned long data), unsigned long data)
  409. {
  410. struct work_struct *sys_work = (struct work_struct *)thiz->priv;
  411. work_func_t func;
  412. thiz->work_func = work_func;
  413. thiz->data = data;
  414. func = (work_func_t) thiz->work_func;
  415. INIT_WORK(sys_work, func);
  416. return 0;
  417. }
  418. struct fm_work *fm_work_create(const fm_s8 *name)
  419. {
  420. struct fm_work *my_work;
  421. struct work_struct *sys_work;
  422. my_work = fm_zalloc(sizeof(struct fm_work));
  423. if (!my_work) {
  424. WCN_DBG(FM_ALT | MAIN, "fm_zalloc(fm_work) -ENOMEM\n");
  425. return NULL;
  426. }
  427. sys_work = fm_zalloc(sizeof(struct work_struct));
  428. if (!sys_work) {
  429. WCN_DBG(FM_ALT | MAIN, "fm_zalloc(struct work_struct) -ENOMEM\n");
  430. fm_free(my_work);
  431. return NULL;
  432. }
  433. fm_memcpy(my_work->name, name, (strlen(name) > FM_NAME_MAX) ? (FM_NAME_MAX) : (strlen(name)));
  434. my_work->priv = sys_work;
  435. my_work->init = fm_work_init;
  436. return my_work;
  437. }
  438. fm_s32 fm_work_get(struct fm_work *thiz)
  439. {
  440. if (thiz == NULL) {
  441. pr_err("%s,invalid pointer\n", __func__);
  442. return -FM_EPARA;
  443. }
  444. thiz->ref++;
  445. return 0;
  446. }
  447. fm_s32 fm_work_put(struct fm_work *thiz)
  448. {
  449. if (thiz == NULL) {
  450. pr_err("%s,invalid pointer\n", __func__);
  451. return -FM_EPARA;
  452. }
  453. thiz->ref--;
  454. if (thiz->ref == 0) {
  455. fm_free(thiz->priv);
  456. fm_free(thiz);
  457. return 0;
  458. } else if (thiz->ref > 0) {
  459. return -FM_EINUSE;
  460. } else {
  461. return -FM_EPARA;
  462. }
  463. }
  464. static fm_s32 fm_workthread_add_work(struct fm_workthread *thiz, struct fm_work *work)
  465. {
  466. if (thiz == NULL) {
  467. pr_err("%s,invalid pointer\n", __func__);
  468. return -FM_EPARA;
  469. }
  470. if (work == NULL) {
  471. pr_err("%s,invalid pointer\n", __func__);
  472. return -FM_EPARA;
  473. }
  474. queue_work((struct workqueue_struct *)thiz->priv, (struct work_struct *)work->priv);
  475. return 0;
  476. }
  477. struct fm_workthread *fm_workthread_create(const fm_s8 *name)
  478. {
  479. struct fm_workthread *my_thread;
  480. struct workqueue_struct *sys_thread;
  481. my_thread = fm_zalloc(sizeof(struct fm_workthread));
  482. if (!my_thread) {
  483. WCN_DBG(FM_ALT | MAIN, "fm_zalloc(fm_workthread) -ENOMEM\n");
  484. return NULL;
  485. }
  486. sys_thread = create_singlethread_workqueue(name);
  487. fm_memcpy(my_thread->name, name, (strlen(name) > FM_NAME_MAX) ? (FM_NAME_MAX) : (strlen(name)));
  488. my_thread->priv = sys_thread;
  489. my_thread->add_work = fm_workthread_add_work;
  490. return my_thread;
  491. }
  492. fm_s32 fm_workthread_get(struct fm_workthread *thiz)
  493. {
  494. if (thiz == NULL) {
  495. pr_err("%s,invalid pointer\n", __func__);
  496. return -FM_EPARA;
  497. }
  498. thiz->ref++;
  499. return 0;
  500. }
  501. fm_s32 fm_workthread_put(struct fm_workthread *thiz)
  502. {
  503. if (thiz == NULL) {
  504. pr_err("%s,invalid pointer\n", __func__);
  505. return -FM_EPARA;
  506. }
  507. thiz->ref--;
  508. if (thiz->ref == 0) {
  509. destroy_workqueue((struct workqueue_struct *)thiz->priv);
  510. fm_free(thiz);
  511. return 0;
  512. } else if (thiz->ref > 0) {
  513. return -FM_EINUSE;
  514. } else {
  515. return -FM_EPARA;
  516. }
  517. }
  518. fm_s32 fm_fifo_in(struct fm_fifo *thiz, void *item)
  519. {
  520. if (item == NULL) {
  521. pr_err("%s,invalid pointer\n", __func__);
  522. return -FM_EPARA;
  523. }
  524. if (thiz->len < thiz->size) {
  525. fm_memcpy((thiz->obj.priv + (thiz->item_size * thiz->in)), item, thiz->item_size);
  526. thiz->in = (thiz->in + 1) % thiz->size;
  527. thiz->len++;
  528. /* WCN_DBG(FM_DBG | MAIN, "add a new item[len=%d]\n", thiz->len); */
  529. } else {
  530. WCN_DBG(FM_WAR | MAIN, "%s fifo is full\n", thiz->obj.name);
  531. return -FM_ENOMEM;
  532. }
  533. return 0;
  534. }
  535. fm_s32 fm_fifo_out(struct fm_fifo *thiz, void *item)
  536. {
  537. if (thiz->len > 0) {
  538. if (item) {
  539. fm_memcpy(item, (thiz->obj.priv + (thiz->item_size * thiz->out)), thiz->item_size);
  540. fm_memset((thiz->obj.priv + (thiz->item_size * thiz->out)), 0, thiz->item_size);
  541. }
  542. thiz->out = (thiz->out + 1) % thiz->size;
  543. thiz->len--;
  544. /* WCN_DBG(FM_DBG | MAIN, "del an item[len=%d]\n", thiz->len); */
  545. } else {
  546. WCN_DBG(FM_WAR | MAIN, "%s fifo is empty\n", thiz->obj.name);
  547. }
  548. return 0;
  549. }
  550. fm_bool fm_fifo_is_full(struct fm_fifo *thiz)
  551. {
  552. return (thiz->len == thiz->size) ? fm_true : fm_false;
  553. }
  554. fm_bool fm_fifo_is_empty(struct fm_fifo *thiz)
  555. {
  556. return (thiz->len == 0) ? fm_true : fm_false;
  557. }
  558. fm_s32 fm_fifo_get_total_len(struct fm_fifo *thiz)
  559. {
  560. return thiz->size;
  561. }
  562. fm_s32 fm_fifo_get_valid_len(struct fm_fifo *thiz)
  563. {
  564. return thiz->len;
  565. }
  566. fm_s32 fm_fifo_reset(struct fm_fifo *thiz)
  567. {
  568. fm_memset(thiz->obj.priv, 0, thiz->item_size * thiz->size);
  569. thiz->in = 0;
  570. thiz->out = 0;
  571. thiz->len = 0;
  572. return 0;
  573. }
  574. struct fm_fifo *fm_fifo_init(struct fm_fifo *fifo, void *buf, const fm_s8 *name, fm_s32 item_size, fm_s32 item_num)
  575. {
  576. fm_memcpy(fifo->obj.name, name, 20);
  577. fifo->size = item_num;
  578. fifo->in = 0;
  579. fifo->out = 0;
  580. fifo->len = 0;
  581. fifo->item_size = item_size;
  582. fifo->obj.priv = buf;
  583. fifo->input = fm_fifo_in;
  584. fifo->output = fm_fifo_out;
  585. fifo->is_full = fm_fifo_is_full;
  586. fifo->is_empty = fm_fifo_is_empty;
  587. fifo->get_total_len = fm_fifo_get_total_len;
  588. fifo->get_valid_len = fm_fifo_get_valid_len;
  589. fifo->reset = fm_fifo_reset;
  590. WCN_DBG(FM_NTC | LINK, "%s inited\n", fifo->obj.name);
  591. return fifo;
  592. }
  593. struct fm_fifo *fm_fifo_create(const fm_s8 *name, fm_s32 item_size, fm_s32 item_num)
  594. {
  595. struct fm_fifo *tmp;
  596. void *buf;
  597. tmp = fm_zalloc(sizeof(struct fm_fifo));
  598. if (!tmp) {
  599. WCN_DBG(FM_ALT | MAIN, "fm_zalloc(fm_fifo) -ENOMEM\n");
  600. return NULL;
  601. }
  602. buf = fm_zalloc(item_size * item_num);
  603. if (!buf) {
  604. WCN_DBG(FM_ALT | MAIN, "fm_zalloc(fm_fifo) -ENOMEM\n");
  605. fm_free(tmp);
  606. return NULL;
  607. }
  608. tmp = fm_fifo_init(tmp, buf, name, item_size, item_num);
  609. WCN_DBG(FM_NTC | LINK, "%s created\n", tmp->obj.name);
  610. return tmp;
  611. }
  612. fm_s32 fm_fifo_release(struct fm_fifo *fifo)
  613. {
  614. if (fifo) {
  615. WCN_DBG(FM_NTC | LINK, "%s released\n", fifo->obj.name);
  616. if (fifo->obj.priv)
  617. fm_free(fifo->obj.priv);
  618. fm_free(fifo);
  619. }
  620. return 0;
  621. }