conn_md.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. #include "conn_md.h"
  2. #include "conn_md_dbg.h"
  3. /*global data structure defination*/
  4. CONN_MD_STRUCT g_conn_md;
  5. static int _conn_md_del_msg_by_uid(uint32 u_id);
  6. static int conn_md_dmp_msg(P_CONN_MD_QUEUE p_msg_list, uint32 src_id, uint32 dst_id);
  7. int conn_md_add_user(uint32 u_id, CONN_MD_BRIDGE_OPS *p_ops)
  8. {
  9. P_CONN_MD_USER p_user = NULL;
  10. struct list_head *pos = NULL;
  11. P_CONN_MD_STRUCT p_conn_md = &g_conn_md;
  12. P_CONN_MD_USER_LIST p_user_list = &p_conn_md->user_list;
  13. /*lock user_list_lock */
  14. mutex_lock(&p_user_list->lock);
  15. /*check if earlier uid reged or not */
  16. list_for_each(pos, &p_user_list->list) {
  17. p_user = container_of(pos, CONN_MD_USER, entry);
  18. if (p_user->u_id == u_id) {
  19. /*if yes */
  20. /*print warning information */
  21. CONN_MD_WARN_FUNC("uid (0x%08x) is already registered, updating with newer one\n", u_id);
  22. break;
  23. }
  24. p_user = NULL;
  25. }
  26. if (NULL == p_user) {
  27. /*memory allocation for user information */
  28. p_user = kmalloc(sizeof(CONN_MD_USER), GFP_ATOMIC);
  29. INIT_LIST_HEAD(&p_user->entry);
  30. list_add_tail(&p_user->entry, &p_user_list->list);
  31. p_user->u_id = u_id;
  32. p_user->state = USER_REGED;
  33. }
  34. /*anyway, write user info to target uid */
  35. memcpy(&p_user->ops, p_ops, sizeof(CONN_MD_BRIDGE_OPS));
  36. p_user->state = USER_ENABLED;
  37. /*unlock user_list lock */
  38. mutex_unlock(&p_user_list->lock);
  39. CONN_MD_WARN_FUNC("uid (0x%08x) is added to user list successfully\n", p_user->u_id);
  40. return 0;
  41. }
  42. int conn_md_del_user(uint32 u_id)
  43. {
  44. int i_ret = -1;
  45. P_CONN_MD_USER p_user = NULL;
  46. struct list_head *pos = NULL;
  47. P_CONN_MD_STRUCT p_conn_md = &g_conn_md;
  48. P_CONN_MD_USER_LIST p_user_list = &p_conn_md->user_list;
  49. /*lock user_list_lock */
  50. mutex_lock(&p_user_list->lock);
  51. /*check if earlier uid reged or not */
  52. list_for_each(pos, &p_user_list->list) {
  53. p_user = container_of(pos, CONN_MD_USER, entry);
  54. if (p_user->u_id == u_id) {
  55. /*if yes */
  56. /*print information */
  57. CONN_MD_INFO_FUNC("uid (0x%08x) is registered, delete it\n", u_id);
  58. break;
  59. }
  60. p_user = NULL;
  61. }
  62. if (NULL == p_user) {
  63. i_ret = CONN_MD_ERR_INVALID_PARAM;
  64. /*print warning information */
  65. CONN_MD_WARN_FUNC("uid (0x%08x) not found in user list..\n", u_id);
  66. } else {
  67. /*delete user info from user info list of target uid */
  68. list_del(pos);
  69. kfree(p_user);
  70. p_user_list->counter--;
  71. CONN_MD_INFO_FUNC("uid (0x%08x) is deleted\n", u_id);
  72. i_ret = 0;
  73. }
  74. /*unlock user_list lock */
  75. mutex_unlock(&p_user_list->lock);
  76. /*search all message enquued in p_msg_list and delete them before delete user */
  77. _conn_md_del_msg_by_uid(u_id);
  78. return i_ret;
  79. }
  80. int conn_md_dmp_msg(P_CONN_MD_QUEUE p_msg_list, uint32 src_id, uint32 dst_id)
  81. {
  82. #define MAX_LENGTH_PER_PACKAGE 8
  83. struct list_head *pos = NULL;
  84. P_CONN_MD_MSG p_msg = NULL;
  85. int i = 0;
  86. int counter = 0;
  87. if (NULL == p_msg_list) {
  88. CONN_MD_ERR_FUNC("invalid parameter, p_msg_list:0x%08x\n", p_msg_list);
  89. return CONN_MD_ERR_INVALID_PARAM;
  90. }
  91. mutex_lock(&p_msg_list->lock);
  92. list_for_each(pos, &p_msg_list->list) {
  93. p_msg = container_of(pos, CONN_MD_MSG, entry);
  94. if (((0 == src_id) || (src_id == p_msg->ilm.src_mod_id)) &&
  95. ((0 == dst_id) || (dst_id == p_msg->ilm.dest_mod_id))) {
  96. counter++;
  97. CONN_MD_INFO_FUNC
  98. ("p_msg:0x%08x, src_id:0x%08x, dest_id:0x%08x, msg_len:%d\n", p_msg,
  99. p_msg->ilm.src_mod_id, p_msg->ilm.dest_mod_id, p_msg->local_para.msg_len);
  100. for (i = 0; (i < p_msg->local_para.msg_len) && (i < MAX_LENGTH_PER_PACKAGE); i++) {
  101. CONN_MD_INFO_FUNC("%02x ", p_msg->local_para.data[i]);
  102. if (7 == (i % 8))
  103. CONN_MD_INFO_FUNC("\n");
  104. }
  105. CONN_MD_INFO_FUNC("\n");
  106. }
  107. }
  108. mutex_unlock(&p_msg_list->lock);
  109. CONN_MD_INFO_FUNC("%d messages found in message list\n", counter);
  110. return 0;
  111. }
  112. int _conn_md_del_msg_by_uid(uint32 u_id)
  113. {
  114. /*only delete messaged enqueued in queue list, do not care message in active queue list */
  115. P_CONN_MD_STRUCT p_conn_md = &g_conn_md;
  116. P_CONN_MD_QUEUE p_msg_list = &p_conn_md->msg_queue;
  117. struct list_head *pos = NULL;
  118. P_CONN_MD_MSG p_msg = NULL;
  119. int flag = 1;
  120. CONN_MD_TRC_FUNC();
  121. mutex_lock(&p_msg_list->lock);
  122. while (flag) {
  123. /*set flat to 0 before search message */
  124. flag = 0;
  125. list_for_each(pos, &p_msg_list->list) {
  126. p_msg = container_of(pos, CONN_MD_MSG, entry);
  127. if ((p_msg->ilm.dest_mod_id == u_id) || (p_msg->ilm.src_mod_id == u_id)) {
  128. flag = 1;
  129. CONN_MD_DBG_FUNC
  130. ("message for uid(0x%08x) found in queue list, dest_id(0x%08x), src_id(0x%08x)\n",
  131. u_id, p_msg->ilm.dest_mod_id, p_msg->ilm.src_mod_id);
  132. break;
  133. }
  134. }
  135. if (1 == flag) {
  136. p_msg_list->counter--;
  137. list_del(pos);
  138. kfree(p_msg);
  139. CONN_MD_DBG_FUNC("dequeued in queue list, counter:%d\n", p_msg_list->counter);
  140. }
  141. }
  142. mutex_unlock(&p_msg_list->lock);
  143. CONN_MD_TRC_FUNC();
  144. return 0;
  145. }
  146. int conn_md_send_msg(ipc_ilm_t *ilm)
  147. {
  148. P_CONN_MD_STRUCT p_conn_md = &g_conn_md;
  149. P_CONN_MD_QUEUE p_msg_list = &p_conn_md->msg_queue;
  150. uint32 msg_str_len = 0;
  151. local_para_struct *p_local_para = NULL;
  152. P_CONN_MD_MSG p_new_msg = NULL;
  153. uint32 msg_info_len = ilm->local_para_ptr->msg_len;
  154. CONN_MD_DBG_FUNC("ilm:0x%08x, msg_len:%d\n", ilm, ilm->local_para_ptr->msg_len);
  155. /*malloc message structure for this msg */
  156. msg_str_len = sizeof(CONN_MD_MSG) + msg_info_len;
  157. p_new_msg = kmalloc(msg_str_len, GFP_ATOMIC);
  158. if (NULL != p_new_msg) {
  159. CONN_MD_DBG_FUNC("p_new_msg:0x%08x\n", p_new_msg);
  160. /*copy message from ilm */
  161. memcpy(p_new_msg, ilm, sizeof(ipc_ilm_t));
  162. p_local_para = &p_new_msg->local_para;
  163. p_new_msg->ilm.local_para_ptr = p_local_para;
  164. /*copy local_para_ptr structure */
  165. memcpy(p_local_para, ilm->local_para_ptr, sizeof(local_para_struct));
  166. /*copy data from local_para_ptr structure */
  167. memcpy(p_local_para->data, ilm->local_para_ptr->data, msg_info_len);
  168. CONN_MD_DBG_FUNC("p_local_para:0x%08x, msg_len:%d\n", p_local_para, p_local_para->msg_len);
  169. INIT_LIST_HEAD(&p_new_msg->entry);
  170. /*lock tx queue lock */
  171. mutex_lock(&p_msg_list->lock);
  172. /*enqueue tx message */
  173. list_add_tail(&p_new_msg->entry, &p_msg_list->list);
  174. p_msg_list->counter++;
  175. /*unlock queue lock */
  176. mutex_unlock(&p_msg_list->lock);
  177. CONN_MD_DBG_FUNC
  178. ("enqueue new message to msg queue list succeed, enqueued msg counter:%d\n", p_msg_list->counter);
  179. conn_md_dmp_in(ilm, MSG_ENQUEUE, p_conn_md->p_msg_dmp_sys);
  180. CONN_MD_DBG_FUNC("begin to wake up conn-md-thread\n");
  181. /*wakeup conn-md thread to handle tx message */
  182. complete(&p_conn_md->tx_comp);
  183. CONN_MD_DBG_FUNC("wake up conn-md-thread done\n");
  184. } else {
  185. CONN_MD_ERR_FUNC("kmalloc for new message structure failed\n");
  186. }
  187. return 0;
  188. }
  189. #define ACT_QUEUE_DBG 0
  190. static int conn_md_thread(void *p_data)
  191. {
  192. P_CONN_MD_STRUCT p_conn_md = (P_CONN_MD_STRUCT) p_data;
  193. P_CONN_MD_QUEUE p_act_queue = &p_conn_md->act_queue;
  194. P_CONN_MD_QUEUE p_msg_queue = &p_conn_md->msg_queue;
  195. struct list_head *pos = NULL;
  196. P_CONN_MD_MSG p_msg = NULL;
  197. P_CONN_MD_USER p_user = NULL;
  198. struct list_head *p_user_pos = NULL;
  199. P_CONN_MD_USER_LIST p_user_list = &p_conn_md->user_list;
  200. ipc_ilm_t *p_cur_ilm = NULL;
  201. while (1) {
  202. wait_for_completion_interruptible(&p_conn_md->tx_comp);
  203. if (kthread_should_stop()) {
  204. CONN_MD_WARN_FUNC("conn-md-thread stoping ...\n");
  205. break;
  206. }
  207. /*check if p_conn_md->msg_queue is empty or not */
  208. mutex_lock(&p_msg_queue->lock);
  209. if (!list_empty(&p_msg_queue->list)) {
  210. /*if not empty, remove all list structure to list of p_conn_md->act_queue */
  211. mutex_lock(&p_act_queue->lock);
  212. if (!list_empty(&p_act_queue->list)) {
  213. /*warning message, this should never happen!!! */
  214. CONN_MD_ERR_FUNC
  215. ("p_act_queue list is not empty, this should never happen!!!---*?*---!!!\n");
  216. }
  217. /*ignore case of p_act_queue is not empty */
  218. list_replace_init(&p_msg_queue->list, &p_act_queue->list);
  219. mutex_unlock(&p_act_queue->lock);
  220. } else {
  221. /*warning message */
  222. CONN_MD_DBG_FUNC("no msg queued in msg queue...\n");
  223. }
  224. mutex_unlock(&p_msg_queue->lock);
  225. mutex_lock(&p_act_queue->lock);
  226. /*dequeue from p_act_queue */
  227. list_for_each(pos, &p_act_queue->list) {
  228. p_msg = container_of(pos, CONN_MD_MSG, entry);
  229. p_cur_ilm = &p_msg->ilm;
  230. if (NULL == p_cur_ilm) {
  231. #if (ACT_QUEUE_DBG == 1)
  232. /*free message structure */
  233. list_del(pos);
  234. kfree(p_msg);
  235. p_act_queue->counter++;
  236. CONN_MD_DBG_FUNC("dequeued in act queue counter:%d\n", p_act_queue->counter);
  237. #endif
  238. continue;
  239. }
  240. conn_md_dmp_in(p_cur_ilm, MSG_DEQUEUE, p_conn_md->p_msg_dmp_sys);
  241. mutex_lock(&p_user_list->lock);
  242. /*check if src module is enabled or not */
  243. list_for_each(p_user_pos, &p_user_list->list) {
  244. p_user = container_of(p_user_pos, CONN_MD_USER, entry);
  245. if (p_user->u_id == p_cur_ilm->src_mod_id && p_user->state == USER_ENABLED) {
  246. /*src module id is enabled already */
  247. CONN_MD_DBG_FUNC("source user id (0x%08x) found\n", p_cur_ilm->src_mod_id);
  248. break;
  249. }
  250. p_user = NULL;
  251. }
  252. if (NULL == p_user) {
  253. mutex_unlock(&p_user_list->lock);
  254. CONN_MD_WARN_FUNC
  255. ("source user id (0x%08x) is not registered or not enabled yet, drop ilm msg\n",
  256. p_cur_ilm->src_mod_id);
  257. #if (ACT_QUEUE_DBG == 1)
  258. /*free message structure */
  259. list_del(pos);
  260. kfree(p_msg);
  261. p_act_queue->counter++;
  262. CONN_MD_DBG_FUNC("dequeued in act queue counter:%d\n", p_act_queue->counter);
  263. #endif
  264. continue;
  265. }
  266. /*check if destination module is enabled or not */
  267. list_for_each(p_user_pos, &p_user_list->list) {
  268. p_user = container_of(p_user_pos, CONN_MD_USER, entry);
  269. if (p_user->u_id == p_cur_ilm->dest_mod_id && p_user->state == USER_ENABLED) {
  270. CONN_MD_DBG_FUNC("target user id (0x%08x) found\n", p_cur_ilm->dest_mod_id);
  271. /*src module id is enabled already */
  272. break;
  273. }
  274. p_user = NULL;
  275. }
  276. if (NULL == p_user) {
  277. mutex_unlock(&p_user_list->lock);
  278. CONN_MD_WARN_FUNC
  279. ("target user id (0x%08x) is not registered or enabled yet, drop ilm msg\n",
  280. p_cur_ilm->dest_mod_id);
  281. #if (ACT_QUEUE_DBG == 1)
  282. /*free message structure */
  283. list_del(pos);
  284. kfree(p_msg);
  285. p_act_queue->counter++;
  286. CONN_MD_DBG_FUNC("dequeued in act queue counter:%d\n", p_act_queue->counter);
  287. #endif
  288. continue;
  289. }
  290. CONN_MD_DBG_FUNC("p_cur_ilm:0x%08x, local_para_ptr:0x%08x, msg_len:%d\n",
  291. p_cur_ilm, &p_cur_ilm->local_para_ptr, p_cur_ilm->local_para_ptr->msg_len);
  292. CONN_MD_DBG_FUNC("sending message to user id (0x%08x)\n", p_cur_ilm->dest_mod_id);
  293. /*send package to dest module by call corresponding rx callback function */
  294. (*(p_user->ops.rx_cb)) (p_cur_ilm);
  295. CONN_MD_DBG_FUNC("message sent to user id (0x%08x) done\n", p_cur_ilm->dest_mod_id);
  296. mutex_unlock(&p_user_list->lock);
  297. #if (ACT_QUEUE_DBG == 1)
  298. /*free message structure */
  299. list_del(pos);
  300. kfree(p_msg);
  301. CONN_MD_DBG_FUNC("message structure freed\n");
  302. p_act_queue->counter++;
  303. CONN_MD_DBG_FUNC("dequeued in act queue counter:%d\n", p_act_queue->counter);
  304. #endif
  305. }
  306. p_msg = NULL;
  307. while (!list_empty(&p_act_queue->list)) {
  308. list_for_each(pos, &p_act_queue->list) {
  309. p_msg = container_of(pos, CONN_MD_MSG, entry);
  310. /*free message structure */
  311. list_del(pos);
  312. kfree(p_msg);
  313. p_msg = NULL;
  314. CONN_MD_DBG_FUNC("message structure freed\n");
  315. p_act_queue->counter++;
  316. CONN_MD_DBG_FUNC("dequeued in act queue counter:%d\n", p_act_queue->counter);
  317. break;
  318. }
  319. }
  320. mutex_unlock(&p_act_queue->lock);
  321. }
  322. CONN_MD_WARN_FUNC("conn-md-thread stopped.\n");
  323. return 0;
  324. }
  325. int conn_md_dmp_msg_queued(uint32 src_id, uint32 dst_id)
  326. {
  327. return conn_md_dmp_msg(&g_conn_md.msg_queue, src_id, dst_id);
  328. }
  329. int conn_md_dmp_msg_active(uint32 src_id, uint32 dst_id)
  330. {
  331. return conn_md_dmp_msg(&g_conn_md.act_queue, src_id, dst_id);
  332. }
  333. int conn_md_dmp_msg_logged(uint32 src_id, uint32 dst_id)
  334. {
  335. return conn_md_dmp_out(g_conn_md.p_msg_dmp_sys, src_id, dst_id);
  336. }
  337. static int conn_md_init(void)
  338. {
  339. int i_ret = -1;
  340. P_CONN_MD_QUEUE p_queue = NULL;
  341. P_CONN_MD_USER_LIST p_user_list = NULL;
  342. CONN_MD_TRC_FUNC();
  343. /*init message queue structure */
  344. p_queue = &g_conn_md.msg_queue;
  345. INIT_LIST_HEAD(&(p_queue->list));
  346. mutex_init(&(p_queue->lock));
  347. p_queue->counter = 0;
  348. CONN_MD_INFO_FUNC("init message queue list succeed\n");
  349. /*init active queue structure */
  350. p_queue = &g_conn_md.act_queue;
  351. INIT_LIST_HEAD(&(p_queue->list));
  352. mutex_init(&(p_queue->lock));
  353. p_queue->counter = 0;
  354. CONN_MD_INFO_FUNC("init active queue list succeed\n");
  355. /*init user_list structure */
  356. p_user_list = &g_conn_md.user_list;
  357. INIT_LIST_HEAD(&(p_user_list->list));
  358. mutex_init(&(p_user_list->lock));
  359. p_user_list->counter = 0;
  360. CONN_MD_INFO_FUNC("init user information list succeed\n");
  361. g_conn_md.p_msg_dmp_sys = conn_md_dmp_init();
  362. if (NULL == g_conn_md.p_msg_dmp_sys)
  363. CONN_MD_WARN_FUNC("conn_md_dmp_init failed\n");
  364. else
  365. CONN_MD_INFO_FUNC("conn_md_dmp_init succeed\n");
  366. /*init proc interface */
  367. conn_md_dbg_init();
  368. /*create conn-md thread */
  369. init_completion(&g_conn_md.tx_comp);
  370. g_conn_md.p_task = kthread_create(conn_md_thread, &g_conn_md, "conn-md-thread");
  371. if (NULL == g_conn_md.p_task) {
  372. CONN_MD_ERR_FUNC("create conn_md_thread fail\n");
  373. i_ret = -ENOMEM;
  374. goto conn_md_err;
  375. }
  376. CONN_MD_INFO_FUNC("create conn_md_thread succeed, wakeup it\n");
  377. /*wakeup conn_md_thread */
  378. wake_up_process(g_conn_md.p_task);
  379. conn_md_err:
  380. CONN_MD_TRC_FUNC();
  381. return i_ret;
  382. }
  383. static void conn_md_exit(void)
  384. {
  385. P_CONN_MD_STRUCT p_conn_md = &g_conn_md;
  386. P_CONN_MD_QUEUE p_queue = NULL;
  387. P_CONN_MD_USER_LIST p_user_list = &p_conn_md->user_list;
  388. struct list_head *pos = NULL;
  389. P_CONN_MD_USER p_user = NULL;
  390. P_CONN_MD_MSG p_msg = NULL;
  391. CONN_MD_TRC_FUNC();
  392. /*terminate conn-md thread */
  393. if (NULL != p_conn_md->p_task) {
  394. CONN_MD_INFO_FUNC("signaling conn-md-thread to stop ...\n");
  395. kthread_stop(p_conn_md->p_task);
  396. }
  397. /*delete user_list structure if user list is not empty */
  398. mutex_lock(&p_user_list->lock);
  399. list_for_each(pos, &p_user_list->list) {
  400. p_user = container_of(pos, CONN_MD_USER, entry);
  401. list_del(pos);
  402. kfree(p_user);
  403. }
  404. p_user_list->counter = 0;
  405. mutex_unlock(&p_user_list->lock);
  406. mutex_destroy(&p_user_list->lock);
  407. /*delete queue structure if message queue list is empty */
  408. p_queue = &p_conn_md->msg_queue;
  409. mutex_lock(&p_queue->lock);
  410. list_for_each(pos, &p_queue->list) {
  411. p_msg = container_of(pos, CONN_MD_MSG, entry);
  412. list_del(pos);
  413. kfree(p_msg);
  414. }
  415. p_queue->counter = 0;
  416. mutex_unlock(&p_queue->lock);
  417. mutex_destroy(&p_queue->lock);
  418. /*delete queue structure if active queue list is empty */
  419. p_queue = &p_conn_md->act_queue;
  420. mutex_lock(&p_queue->lock);
  421. list_for_each(pos, &p_queue->list) {
  422. p_msg = container_of(pos, CONN_MD_MSG, entry);
  423. list_del(pos);
  424. kfree(p_msg);
  425. }
  426. p_queue->counter = 0;
  427. mutex_unlock(&p_queue->lock);
  428. mutex_destroy(&p_queue->lock);
  429. if (NULL != p_conn_md->p_msg_dmp_sys)
  430. conn_md_dmp_deinit(p_conn_md->p_msg_dmp_sys);
  431. CONN_MD_TRC_FUNC();
  432. }
  433. /*---------------------------------------------------------------------------*/
  434. /*
  435. module_init(conn_md_init);
  436. module_exit(conn_md_exit);
  437. */
  438. subsys_initcall(conn_md_init);
  439. module_exit(conn_md_exit);
  440. /*---------------------------------------------------------------------------*/
  441. MODULE_AUTHOR("MBJ/WCN/SE/SS1/Chaozhong.Liang");
  442. MODULE_DESCRIPTION("MTK CONN-MD Bridge Driver$1.0$");
  443. MODULE_LICENSE("GPL");
  444. /*---------------------------------------------------------------------------*/