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