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/semaphore.h>
24 #include <linux/slab.h>
25
osal_sem_init(osal_semaphore * sem,int val)26 int osal_sem_init(osal_semaphore *sem, int val)
27 {
28 struct semaphore *p = NULL;
29
30 if (sem == NULL) {
31 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
32 return -1;
33 }
34
35 p = kmalloc(sizeof(struct semaphore), GFP_KERNEL);
36 if (p == NULL) {
37 osal_printk("%s - kmalloc error!\n", __FUNCTION__);
38 return -1;
39 }
40
41 sema_init(p, val);
42 sem->sem = p;
43
44 return 0;
45 }
46 EXPORT_SYMBOL(osal_sem_init);
47
osal_sem_down(osal_semaphore * sem)48 int osal_sem_down(osal_semaphore *sem)
49 {
50 struct semaphore *p = NULL;
51
52 if (sem == NULL) {
53 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
54 return -1;
55 }
56
57 p = (struct semaphore *)(sem->sem);
58 down(p);
59
60 return 0;
61 }
62 EXPORT_SYMBOL(osal_sem_down);
63
osal_sem_down_interruptible(osal_semaphore * sem)64 int osal_sem_down_interruptible(osal_semaphore *sem)
65 {
66 struct semaphore *p = NULL;
67
68 if (sem == NULL) {
69 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
70 return -1;
71 }
72
73 p = (struct semaphore *)(sem->sem);
74
75 return down_interruptible(p);
76 }
77 EXPORT_SYMBOL(osal_sem_down_interruptible);
78
osal_sem_trydown(osal_semaphore * sem)79 int osal_sem_trydown(osal_semaphore *sem)
80 {
81 struct semaphore *p = NULL;
82
83 if (sem == NULL) {
84 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
85 return -1;
86 }
87
88 p = (struct semaphore *)(sem->sem);
89
90 return down_trylock(p);
91 }
92 EXPORT_SYMBOL(osal_sem_trydown);
93
osal_sem_up(osal_semaphore * sem)94 void osal_sem_up(osal_semaphore *sem)
95 {
96 struct semaphore *p = NULL;
97
98 if (sem == NULL) {
99 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
100 return;
101 }
102
103 p = (struct semaphore *)(sem->sem);
104 up(p);
105 }
106 EXPORT_SYMBOL(osal_sem_up);
osal_sem_destory(osal_semaphore * sem)107 void osal_sem_destory(osal_semaphore *sem)
108 {
109 struct semaphore *p = NULL;
110
111 if (sem == NULL) {
112 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
113 return;
114 }
115
116 p = (struct semaphore *)(sem->sem);
117 kfree(p);
118 sem->sem = NULL;
119 }
120 EXPORT_SYMBOL(osal_sem_destory);
121