vivdw200_irq_queue.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #ifdef __KERNEL__
  2. #include <asm/io.h>
  3. #include <linux/cdev.h>
  4. #include <linux/module.h>
  5. #include <linux/platform_device.h>
  6. #include <linux/delay.h>
  7. #include <linux/clk.h>
  8. #include <linux/io.h>
  9. #include <linux/mm.h>
  10. #include <linux/timer.h>
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/workqueue.h>
  14. #include <linux/slab.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/interrupt.h>
  20. #else
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <stdbool.h>
  24. #endif
  25. #include "vivdw200_irq_queue.h"
  26. #ifdef __KERNEL__
  27. #define dw_info(...)
  28. #else
  29. #define dw_info(...)
  30. #endif
  31. //enqueue
  32. int vivdw200_enqueue(vivdw200_mis_t *data,vivdw200_mis_t* head)
  33. {
  34. #ifdef __KERNEL__
  35. vivdw200_mis_t* new_node = (vivdw200_mis_t*)kmalloc(sizeof(vivdw200_mis_t), GFP_KERNEL); //create new node
  36. if (data == NULL || head == NULL) {
  37. //dw_info("%s: input wrong parameter\n", __func__);
  38. return -1;
  39. }
  40. new_node->val = data->val;
  41. dw_info("%s: new_node %px", __func__, new_node);
  42. INIT_LIST_HEAD(&new_node->list);
  43. list_add_tail(&new_node->list, &head->list); //append to tail
  44. #endif
  45. return 0;
  46. }
  47. //dequeue && release memory
  48. int vivdw200_dequeue(vivdw200_mis_t* data, vivdw200_mis_t* head)
  49. {
  50. #ifdef __KERNEL__
  51. vivdw200_mis_t* entry;
  52. if (data == NULL || head == NULL) {
  53. //dw_info("%s: input wrong parameter\n", __func__);
  54. return -1;
  55. }
  56. if (list_empty(&head->list)) {
  57. //dw_info("%s: There is no node\n", __func__);
  58. return -1;
  59. }
  60. entry = list_first_entry(&head->list, vivdw200_mis_t, list);
  61. dw_info("%s: entry %px", __func__, entry);
  62. data->val = entry->val;
  63. list_del_init(&entry->list);
  64. kfree(entry);
  65. #endif
  66. return 0;
  67. }
  68. bool vivdw200_is_queue_empty( vivdw200_mis_t* head)
  69. {
  70. #ifdef __KERNEL__
  71. return list_empty(&head->list);
  72. #else
  73. return 0;
  74. #endif
  75. }
  76. int vivdw200_create_circle_queue(vivdw200_mis_list_t* pCList, int number)
  77. {
  78. #ifdef __KERNEL__
  79. int i;
  80. vivdw200_mis_t* pMisNode;
  81. if (pCList == NULL || number <= 0) {
  82. dw_info("%s: create circle queue failed\n", __func__);
  83. return -1;
  84. }
  85. if (pCList->pHead == NULL) {
  86. pCList->pHead = (vivdw200_mis_t*)kmalloc(sizeof(vivdw200_mis_t), GFP_KERNEL);
  87. INIT_LIST_HEAD(&pCList->pHead->list);
  88. pCList->pRead = pCList->pHead;
  89. pCList->pWrite = pCList->pHead;
  90. }
  91. dw_info("%s:pHead %px\n", __func__, pCList->pHead);
  92. for (i = 0; i < number - 1; i++) {
  93. pMisNode = (vivdw200_mis_t*)kmalloc(sizeof(vivdw200_mis_t), GFP_KERNEL);
  94. INIT_LIST_HEAD(&pMisNode->list);
  95. list_add_tail(&pMisNode->list, &pCList->pHead->list);
  96. dw_info("%s:pMisNode %px\n", __func__, pMisNode);
  97. }
  98. #endif
  99. return 0;
  100. }
  101. int vivdw200_destroy_circle_queue(vivdw200_mis_list_t* pCList)
  102. {
  103. #ifdef __KERNEL__
  104. vivdw200_mis_t* pMisNode;
  105. if (pCList == NULL || (pCList->pHead == NULL) ) {
  106. dw_info("%s: destroy circle queue failed. pClist %px\n", __func__, pCList);
  107. return -1;
  108. }
  109. while(!list_empty(&pCList->pHead->list)) {
  110. pMisNode = list_first_entry(&pCList->pHead->list, vivdw200_mis_t, list);
  111. dw_info("%s:pMisNode %px\n", __func__, pMisNode);
  112. list_del(&pMisNode->list);
  113. kfree(pMisNode);
  114. pMisNode = NULL;
  115. }
  116. dw_info("%s:pHead %px\n", __func__, pCList->pHead);
  117. kfree(pCList->pHead);
  118. pCList->pHead = NULL;
  119. pCList->pRead = NULL;
  120. pCList->pWrite = NULL;
  121. #endif
  122. return 0;
  123. }
  124. int vivdw200_read_circle_queue(vivdw200_mis_t* data, vivdw200_mis_list_t* pCList)
  125. {
  126. #ifdef __KERNEL__
  127. //vivdw200_mis_t* pReadEntry;
  128. if (pCList == NULL) {
  129. dw_info("%s: can not read circle queue\n", __func__);
  130. return -1;
  131. }
  132. if (pCList->pRead == pCList->pWrite) {
  133. dw_info("%s: There is no irq mis data\n", __func__);
  134. return -1;
  135. }
  136. data->val = pCList->pRead->val;
  137. dw_info("%s: entry %px, msi %08x\n", __func__, pCList->pRead, data->val);
  138. /*Get the next entry that link with read entry list*/
  139. /*Update read pointer to next entry*/
  140. pCList->pRead = list_first_entry(&pCList->pRead->list, vivdw200_mis_t, list);
  141. //pCList->pRead = pReadEntry;
  142. #endif
  143. return 0;
  144. }
  145. int vivdw200_write_circle_queue(vivdw200_mis_t* data, vivdw200_mis_list_t* pCList)
  146. {
  147. #ifdef __KERNEL__
  148. vivdw200_mis_t* pWriteEntry;
  149. if (pCList == NULL) {
  150. dw_info("%s: can not read circle queue\n", __func__);
  151. return -1;
  152. }
  153. pCList->pWrite->val = data->val;
  154. dw_info("%s: entry %px, msi %08x\n", __func__, pCList->pWrite, data->val);
  155. /*get the next write entry pointer that link with the write entry list*/
  156. pWriteEntry = list_first_entry(&pCList->pWrite->list, vivdw200_mis_t, list);
  157. /*Update write pointer to point next entry*/
  158. pCList->pWrite = pWriteEntry;
  159. #endif
  160. return 0;
  161. }