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. return -1;
  86. }
  87. new_node->val = new->val;
  88. new_node->irq_src = new->irq_src;
  89. /*printk("%s: new_node %px irq_src %d", __func__, new_node, new->irq_src);*/
  90. INIT_LIST_HEAD(&new_node->list);
  91. list_add_tail(&new_node->list, &head->list); //append to tail
  92. #endif
  93. return 0;
  94. }
  95. //dequeue && release memory
  96. int isp_irq_dequeue(isp_mis_t* data, isp_mis_t* head)
  97. {
  98. #ifdef __KERNEL__
  99. isp_mis_t* entry;
  100. if (data == NULL || head == NULL) {
  101. //printk("%s: input wrong parameter\n", __func__);
  102. return -1;
  103. }
  104. if (list_empty(&head->list)) {
  105. //printk("%s: There is no node\n", __func__);
  106. return -1;
  107. }
  108. entry = list_first_entry(&head->list, isp_mis_t, list);
  109. /*printk("%s: entry %px irq_src %d", __func__, entry, entry->irq_src);*/
  110. data->val = entry->val;
  111. data->irq_src = entry->irq_src;
  112. list_del_init(&entry->list);
  113. kfree(entry);
  114. #endif
  115. return 0;
  116. }
  117. bool isp_irq_is_queue_empty( isp_mis_t* head)
  118. {
  119. #ifdef __KERNEL__
  120. return list_empty(&head->list);
  121. #endif
  122. return 0;
  123. }
  124. int isp_irq_create_circle_queue(isp_mis_list_t* pCList, int number)
  125. {
  126. #ifdef __KERNEL__
  127. int i;
  128. isp_mis_t* pMisNode;
  129. if (pCList == NULL || number <= 0) {
  130. isp_info("%s: create circle queue failed\n", __func__);
  131. return -1;
  132. }
  133. if (pCList->pHead == NULL) {
  134. pCList->pHead = (isp_mis_t*)kmalloc(sizeof(isp_mis_t), GFP_KERNEL);
  135. INIT_LIST_HEAD(&pCList->pHead->list);
  136. pCList->pRead = pCList->pHead;
  137. pCList->pWrite = pCList->pHead;
  138. }
  139. isp_info("%s:pHead %px\n", __func__, pCList->pHead);
  140. for (i = 0; i < number - 1; i++) {
  141. pMisNode = (isp_mis_t*)kmalloc(sizeof(isp_mis_t), GFP_KERNEL);
  142. INIT_LIST_HEAD(&pMisNode->list);
  143. list_add_tail(&pMisNode->list, &pCList->pHead->list);
  144. isp_info("%s:pMisNode %px\n", __func__, pMisNode);
  145. }
  146. #endif
  147. return 0;
  148. }
  149. int isp_irq_destroy_circle_queue(isp_mis_list_t* pCList)
  150. {
  151. #ifdef __KERNEL__
  152. isp_mis_t* pMisNode;
  153. if ((pCList == NULL) || (pCList->pHead == NULL) ) {
  154. isp_err("%s: destroy circle queue failed. pClist %px\n", __func__, pCList);
  155. return -1;
  156. }
  157. while(!list_empty(&pCList->pHead->list)) {
  158. pMisNode = list_first_entry(&pCList->pHead->list, isp_mis_t, list);
  159. isp_info("%s:pMisNode %px\n", __func__, pMisNode);
  160. list_del(&pMisNode->list);
  161. kfree(pMisNode);
  162. pMisNode = NULL;
  163. }
  164. isp_info("%s:pHead %px\n", __func__, pCList->pHead);
  165. kfree(pCList->pHead);
  166. pCList->pHead = NULL;
  167. pCList->pRead = NULL;
  168. pCList->pWrite = NULL;
  169. #endif
  170. return 0;
  171. }
  172. int isp_irq_read_circle_queue(isp_mis_t* data, isp_mis_list_t* pCList)
  173. {
  174. #ifdef __KERNEL__
  175. //isp_mis_t* pReadEntry;
  176. if (pCList == NULL) {
  177. isp_err("%s: can not read circle queue\n", __func__);
  178. return -1;
  179. }
  180. if (pCList->pRead == pCList->pWrite) {
  181. /*printk("%s: There is no irq mis data\n", __func__);*/
  182. return -1;
  183. }
  184. data->val = pCList->pRead->val;
  185. data->irq_src = pCList->pRead->irq_src;
  186. /*printk("%s: entry %px irq_src %d, msi %08x\n", __func__, pCList->pRead, data->irq_src, data->val);*/
  187. /*Get the next entry that link with read entry list*/
  188. /*Update read pointer to next entry*/
  189. pCList->pRead = list_first_entry(&pCList->pRead->list, isp_mis_t, list);
  190. //pCList->pRead = pReadEntry;
  191. #endif
  192. return 0;
  193. }
  194. int isp_irq_write_circle_queue(isp_mis_t* data, isp_mis_list_t* pCList)
  195. {
  196. #ifdef __KERNEL__
  197. isp_mis_t* pWriteEntry;
  198. if (pCList == NULL) {
  199. isp_err("%s: can not read circle queue\n", __func__);
  200. return -1;
  201. }
  202. pCList->pWrite->val = data->val;
  203. pCList->pWrite->irq_src = data->irq_src;
  204. /*printk("%s: entry %px irq_src %d, msi %08x\n", __func__, pCList->pWrite, data->irq_src, data->val);*/
  205. /*get the next write entry pointer that link with the write entry list*/
  206. pWriteEntry = list_first_entry(&pCList->pWrite->list, isp_mis_t, list);
  207. /*Update write pointer to point next entry*/
  208. pCList->pWrite = pWriteEntry;
  209. #endif
  210. return 0;
  211. }
  212. int isp_irq_reset_circle_queue(isp_mis_list_t* pCList)
  213. {
  214. #ifdef __KERNEL__
  215. if (pCList == NULL) {
  216. isp_err("%s: can not reset circle queue\n", __func__);
  217. return -1;
  218. }
  219. pCList->pRead = pCList->pHead;
  220. pCList->pWrite = pCList->pHead;
  221. #endif
  222. return 0;
  223. }