• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/mutex.h>
24 #include <linux/slab.h>
25 
osal_mutex_init(osal_mutex * mutex)26 int osal_mutex_init(osal_mutex *mutex)
27 {
28     struct mutex *p = NULL;
29 
30     if (mutex == NULL) {
31         osal_printk("%s - parameter invalid!\n", __FUNCTION__);
32         return -1;
33     }
34 
35     p = kmalloc(sizeof(struct mutex), GFP_KERNEL);
36     if (p == NULL) {
37         osal_printk("%s - kmalloc error!\n", __FUNCTION__);
38         return -1;
39     }
40 
41     mutex_init(p);
42     mutex->mutex = p;
43 
44     return 0;
45 }
46 EXPORT_SYMBOL(osal_mutex_init);
47 
osal_mutex_lock(osal_mutex * mutex)48 int osal_mutex_lock(osal_mutex *mutex)
49 {
50     struct mutex *p = NULL;
51 
52     if (mutex == NULL) {
53         osal_printk("%s - parameter invalid!\n", __FUNCTION__);
54         return -1;
55     }
56 
57     p = (struct mutex *)(mutex->mutex);
58     mutex_lock(p);
59 
60     return 0;
61 }
62 EXPORT_SYMBOL(osal_mutex_lock);
63 
osal_mutex_lock_interruptible(osal_mutex * mutex)64 int osal_mutex_lock_interruptible(osal_mutex *mutex)
65 {
66     struct mutex *p = NULL;
67 
68     if (mutex == NULL) {
69         osal_printk("%s - parameter invalid!\n", __FUNCTION__);
70         return -1;
71     }
72 
73     p = (struct mutex *)(mutex->mutex);
74 
75     return mutex_lock_interruptible(p);
76 }
77 EXPORT_SYMBOL(osal_mutex_lock_interruptible);
78 
osal_mutex_trylock(osal_mutex * mutex)79 int osal_mutex_trylock(osal_mutex *mutex)
80 {
81     struct mutex *p = NULL;
82 
83     if (mutex == NULL) {
84         osal_printk("%s - parameter invalid!\n", __FUNCTION__);
85         return -1;
86     }
87 
88     p = (struct mutex *)(mutex->mutex);
89 
90     return mutex_trylock(p);
91 }
92 EXPORT_SYMBOL(osal_mutex_trylock);
93 
osal_mutex_unlock(osal_mutex * mutex)94 void osal_mutex_unlock(osal_mutex *mutex)
95 {
96     struct mutex *p = NULL;
97 
98     p = (struct mutex *)(mutex->mutex);
99 
100     mutex_unlock(p);
101 }
102 EXPORT_SYMBOL(osal_mutex_unlock);
103 
osal_mutex_destory(osal_mutex * mutex)104 void osal_mutex_destory(osal_mutex *mutex)
105 {
106     struct mutex *p = NULL;
107 
108     p = (struct mutex *)(mutex->mutex);
109     kfree(p);
110     mutex->mutex = NULL;
111 }
112 EXPORT_SYMBOL(osal_mutex_destory);
113