1 /*
2 * Copyright (C) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include "hi_osal.h"
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/printk.h>
23 #include <linux/wait.h>
24 #include <linux/sched.h>
25 #include <linux/slab.h>
26
27 #define OSAL_WAIT_CONDITION_TRUE 1
28
osal_msecs_to_jiffies(const unsigned int m)29 unsigned long osal_msecs_to_jiffies(const unsigned int m)
30 {
31 return msecs_to_jiffies(m);
32 }
33 EXPORT_SYMBOL(osal_msecs_to_jiffies);
34
osal_wait_init(osal_wait * wait)35 int osal_wait_init(osal_wait *wait)
36 {
37 wait_queue_head_t *wq = NULL;
38 if (wait == NULL) {
39 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
40 return -1;
41 }
42 wq = (wait_queue_head_t *)kmalloc(sizeof(wait_queue_head_t), GFP_ATOMIC);
43 if (wq == NULL) {
44 osal_printk("%s - kmalloc error!\n", __FUNCTION__);
45 return -1;
46 }
47 init_waitqueue_head(wq);
48 wait->wait = wq;
49 return 0;
50 }
51 EXPORT_SYMBOL(osal_wait_init);
osal_wait_interruptible(osal_wait * wait,osal_wait_condition_func func,const void * param)52 int osal_wait_interruptible(osal_wait *wait, osal_wait_condition_func func, const void *param)
53 {
54 wait_queue_head_t *wq = NULL;
55
56 if (wait == NULL) {
57 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
58 return -1;
59 }
60
61 wq = (wait_queue_head_t *)(wait->wait);
62 if (wq == NULL) {
63 osal_printk("%s - wait->wait is NULL!\n", __FUNCTION__);
64 return -1;
65 }
66
67 if (func == NULL) {
68 return wait_event_interruptible((*wq), OSAL_WAIT_CONDITION_TRUE);
69 }
70
71 return wait_event_interruptible((*wq), func(param));
72 }
73 EXPORT_SYMBOL(osal_wait_interruptible);
74
osal_wait_uninterruptible(osal_wait * wait,osal_wait_condition_func func,const void * param)75 int osal_wait_uninterruptible(osal_wait *wait, osal_wait_condition_func func, const void *param)
76 {
77 wait_queue_head_t *wq = NULL;
78
79 if (wait == NULL) {
80 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
81 return -1;
82 }
83
84 wq = (wait_queue_head_t *)(wait->wait);
85 if (wq == NULL) {
86 osal_printk("%s - wait->wait is NULL!\n", __FUNCTION__);
87 return -1;
88 }
89
90 if (func == NULL) {
91 wait_event((*wq), OSAL_WAIT_CONDITION_TRUE);
92 } else {
93 wait_event((*wq), func(param));
94 }
95
96 return 0;
97 }
98 EXPORT_SYMBOL(osal_wait_uninterruptible);
99
osal_wait_timeout_interruptible(osal_wait * wait,osal_wait_condition_func func,const void * param,unsigned long ms)100 int osal_wait_timeout_interruptible(osal_wait *wait, osal_wait_condition_func func, const void *param, unsigned long ms)
101 {
102 wait_queue_head_t *wq = NULL;
103
104 if (wait == NULL) {
105 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
106 return -1;
107 }
108
109 wq = (wait_queue_head_t *)(wait->wait);
110 if (wq == NULL) {
111 osal_printk("%s - wait->wait is NULL!\n", __FUNCTION__);
112 return -1;
113 }
114
115 if (func == NULL) {
116 return wait_event_interruptible_timeout((*wq), OSAL_WAIT_CONDITION_TRUE, msecs_to_jiffies(ms));
117 }
118
119 return wait_event_interruptible_timeout((*wq), func(param), msecs_to_jiffies(ms));
120 }
121
122 EXPORT_SYMBOL(osal_wait_timeout_interruptible);
123
osal_wait_timeout_uninterruptible(osal_wait * wait,osal_wait_condition_func func,const void * param,unsigned long ms)124 int osal_wait_timeout_uninterruptible(osal_wait *wait, osal_wait_condition_func func,
125 const void *param, unsigned long ms)
126 {
127 int timeout = 0;
128 wait_queue_head_t *wq = NULL;
129
130 if (wait == NULL) {
131 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
132 return -1;
133 }
134
135 wq = (wait_queue_head_t *)(wait->wait);
136 if (wq == NULL) {
137 osal_printk("%s - wait->wait is NULL!\n", __FUNCTION__);
138 return -1;
139 }
140
141 if (func == NULL) {
142 timeout = wait_event_timeout((*wq), OSAL_WAIT_CONDITION_TRUE, msecs_to_jiffies(ms));
143 } else {
144 timeout = wait_event_timeout((*wq), func(param), msecs_to_jiffies(ms));
145 }
146
147 return timeout;
148 }
149
150 EXPORT_SYMBOL(osal_wait_timeout_uninterruptible);
151
osal_wait_wakeup(osal_wait * wait)152 void osal_wait_wakeup(osal_wait *wait)
153 {
154 wait_queue_head_t *wq = NULL;
155
156 wq = (wait_queue_head_t *)(wait->wait);
157 if (wq == NULL) {
158 osal_printk("%s - wait->wait is NULL!\n", __FUNCTION__);
159 return;
160 }
161
162 wake_up_all(wq);
163 }
164 EXPORT_SYMBOL(osal_wait_wakeup);
osal_wait_destroy(osal_wait * wait)165 void osal_wait_destroy(osal_wait *wait)
166 {
167 wait_queue_head_t *wq = NULL;
168
169 wq = (wait_queue_head_t *)(wait->wait);
170 if (wq == NULL) {
171 osal_printk("%s - wait->wait is NULL!\n", __FUNCTION__);
172 return;
173 }
174 kfree(wq);
175 wait->wait = NULL;
176 }
177 EXPORT_SYMBOL(osal_wait_destroy);
178