isp_irq_queue.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /****************************************************************************
  2. *
  3. * The MIT License (MIT)
  4. *
  5. * Copyright (c) 2020 VeriSilicon Holdings Co., Ltd.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. *
  25. *****************************************************************************
  26. *
  27. * The GPL License (GPL)
  28. *
  29. * Copyright (c) 2020 VeriSilicon Holdings Co., Ltd.
  30. *
  31. * This program is free software; you can redistribute it and/or
  32. * modify it under the terms of the GNU General Public License
  33. * as published by the Free Software Foundation; either version 2
  34. * of the License, or (at your option) any later version.
  35. *
  36. * This program is distributed in the hope that it will be useful,
  37. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. * GNU General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU General Public License
  42. * along with this program;
  43. *
  44. *****************************************************************************
  45. *
  46. * Note: This software is released under dual MIT and GPL licenses. A
  47. * recipient may use this file under the terms of either the MIT license or
  48. * GPL License. If you wish to use only one license not the other, you can
  49. * indicate your decision by deleting one of the above license notices in your
  50. * version of this file.
  51. *
  52. *****************************************************************************/
  53. #ifdef __KERNEL__
  54. #include <asm/io.h>
  55. #include <linux/cdev.h>
  56. #include <linux/module.h>
  57. #include <linux/platform_device.h>
  58. #include <linux/delay.h>
  59. #include <linux/clk.h>
  60. #include <linux/io.h>
  61. #include <linux/mm.h>
  62. #include <linux/timer.h>
  63. #include <linux/kernel.h>
  64. #include <linux/init.h>
  65. #include <linux/workqueue.h>
  66. #include <linux/slab.h>
  67. #include <linux/proc_fs.h>
  68. #include <linux/debugfs.h>
  69. #include <linux/miscdevice.h>
  70. #include <linux/uaccess.h>
  71. #include <linux/interrupt.h>
  72. #else
  73. #include <stdio.h>
  74. #include <stdlib.h>
  75. #include <stdbool.h>
  76. #endif
  77. #include "isp_irq_queue.h"
  78. #include "isp_ioctl.h"
  79. //enqueue
  80. int isp_irq_enqueue(isp_mis_t *new,isp_mis_t* head)
  81. {
  82. #ifdef __KERNEL__
  83. isp_mis_t* new_node = (isp_mis_t*)kmalloc(sizeof(isp_mis_t), GFP_KERNEL); //create new node
  84. if (new == NULL || head == NULL) {
  85. //isp_info("%s: input wrong parameter\n", __func__);
  86. return -1;
  87. }
  88. new_node->val = new->val;
  89. new_node->irq_src = new->irq_src;
  90. /*isp_info("%s: new_node %px irq_src %d", __func__, new_node, new->irq_src);*/
  91. INIT_LIST_HEAD(&new_node->list);
  92. list_add_tail(&new_node->list, &head->list); //append to tail
  93. #endif
  94. return 0;
  95. }
  96. //dequeue && release memory
  97. int isp_irq_dequeue(isp_mis_t* data, isp_mis_t* head)
  98. {
  99. #ifdef __KERNEL__
  100. isp_mis_t* entry;
  101. if (data == NULL || head == NULL) {
  102. //isp_info("%s: input wrong parameter\n", __func__);
  103. return -1;
  104. }
  105. if (list_empty(&head->list)) {
  106. //isp_info("%s: There is no node\n", __func__);
  107. return -1;
  108. }
  109. entry = list_first_entry(&head->list, isp_mis_t, list);
  110. /*isp_info("%s: entry %px irq_src %d", __func__, entry, entry->irq_src);*/
  111. data->val = entry->val;
  112. data->irq_src = entry->irq_src;
  113. list_del_init(&entry->list);
  114. kfree(entry);
  115. #endif
  116. return 0;
  117. }
  118. bool isp_irq_is_queue_empty( isp_mis_t* head)
  119. {
  120. #ifdef __KERNEL__
  121. return list_empty(&head->list);
  122. #endif
  123. return 0;
  124. }
  125. int isp_irq_create_circle_queue(isp_mis_list_t* pCList, int number)
  126. {
  127. #ifdef __KERNEL__
  128. int i;
  129. isp_mis_t* pMisNode;
  130. if (pCList == NULL || number <= 0) {
  131. isp_info("%s: create circle queue failed\n", __func__);
  132. return -1;
  133. }
  134. if (pCList->pHead == NULL) {
  135. pCList->pHead = (isp_mis_t*)kmalloc(sizeof(isp_mis_t), GFP_KERNEL);
  136. INIT_LIST_HEAD(&pCList->pHead->list);
  137. pCList->pRead = pCList->pHead;
  138. pCList->pWrite = pCList->pHead;
  139. }
  140. isp_info("%s:pHead %px\n", __func__, pCList->pHead);
  141. for (i = 0; i < number - 1; i++) {
  142. pMisNode = (isp_mis_t*)kmalloc(sizeof(isp_mis_t), GFP_KERNEL);
  143. INIT_LIST_HEAD(&pMisNode->list);
  144. list_add_tail(&pMisNode->list, &pCList->pHead->list);
  145. isp_info("%s:pMisNode %px\n", __func__, pMisNode);
  146. }
  147. #endif
  148. return 0;
  149. }
  150. int isp_irq_destroy_circle_queue(isp_mis_list_t* pCList)
  151. {
  152. #ifdef __KERNEL__
  153. isp_mis_t* pMisNode;
  154. if ((pCList == NULL) || (pCList->pHead == NULL) ) {
  155. isp_info("%s: destroy circle queue failed. pClist %px\n", __func__, pCList);
  156. return -1;
  157. }
  158. while(!list_empty(&pCList->pHead->list)) {
  159. pMisNode = list_first_entry(&pCList->pHead->list, isp_mis_t, list);
  160. isp_info("%s:pMisNode %px\n", __func__, pMisNode);
  161. list_del(&pMisNode->list);
  162. kfree(pMisNode);
  163. pMisNode = NULL;
  164. }
  165. isp_info("%s:pHead %px\n", __func__, pCList->pHead);
  166. kfree(pCList->pHead);
  167. pCList->pHead = NULL;
  168. pCList->pRead = NULL;
  169. pCList->pWrite = NULL;
  170. #endif
  171. return 0;
  172. }
  173. int isp_irq_read_circle_queue(isp_mis_t* data, isp_mis_list_t* pCList)
  174. {
  175. #ifdef __KERNEL__
  176. //isp_mis_t* pReadEntry;
  177. if (pCList == NULL) {
  178. isp_info("%s: can not read circle queue\n", __func__);
  179. return -1;
  180. }
  181. if (pCList->pRead == pCList->pWrite) {
  182. /*isp_info("%s: There is no irq mis data\n", __func__);*/
  183. return -1;
  184. }
  185. data->val = pCList->pRead->val;
  186. data->irq_src = pCList->pRead->irq_src;
  187. /*isp_info("%s: entry %px irq_src %d, msi %08x\n", __func__, pCList->pRead, data->irq_src, data->val);*/
  188. /*Get the next entry that link with read entry list*/
  189. /*Update read pointer to next entry*/
  190. pCList->pRead = list_first_entry(&pCList->pRead->list, isp_mis_t, list);
  191. //pCList->pRead = pReadEntry;
  192. #endif
  193. return 0;
  194. }
  195. int isp_irq_write_circle_queue(isp_mis_t* data, isp_mis_list_t* pCList)
  196. {
  197. #ifdef __KERNEL__
  198. isp_mis_t* pWriteEntry;
  199. if (pCList == NULL) {
  200. isp_info("%s: can not read circle queue\n", __func__);
  201. return -1;
  202. }
  203. pCList->pWrite->val = data->val;
  204. pCList->pWrite->irq_src = data->irq_src;
  205. /*isp_info("%s: entry %px irq_src %d, msi %08x\n", __func__, pCList->pWrite, data->irq_src, data->val);*/
  206. /*get the next write entry pointer that link with the write entry list*/
  207. pWriteEntry = list_first_entry(&pCList->pWrite->list, isp_mis_t, list);
  208. /*Update write pointer to point next entry*/
  209. pCList->pWrite = pWriteEntry;
  210. #endif
  211. return 0;
  212. }
  213. int isp_irq_reset_circle_queue(isp_mis_list_t* pCList)
  214. {
  215. #ifdef __KERNEL__
  216. if (pCList == NULL) {
  217. isp_err("%s: can not reset circle queue\n", __func__);
  218. return -1;
  219. }
  220. pCList->pRead = pCList->pHead;
  221. pCList->pWrite = pCList->pHead;
  222. #endif
  223. return 0;
  224. }