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/semaphore.h>
24 #include <linux/slab.h>
25
osal_sema_init(osal_semaphore_t * sem,int val)26 int osal_sema_init(osal_semaphore_t *sem, int val)
27 {
28 struct semaphore *p = NULL;
29
30 if (sem == NULL) {
31 osal_trace("%s - parameter invalid!\n", __FUNCTION__);
32 return -1;
33 }
34 p = kmalloc(sizeof(struct semaphore), GFP_KERNEL);
35 if (p == NULL) {
36 osal_trace("%s - kmalloc error!\n", __FUNCTION__);
37 return -1;
38 }
39 sema_init(p, val);
40 sem->sem = p;
41 return 0;
42 }
43 EXPORT_SYMBOL(osal_sema_init);
osal_down(osal_semaphore_t * sem)44 int osal_down(osal_semaphore_t *sem)
45 {
46 struct semaphore *p = NULL;
47
48 if (sem == NULL) {
49 osal_trace("%s - parameter invalid!\n", __FUNCTION__);
50 return -1;
51 }
52 p = (struct semaphore *)(sem->sem);
53 down(p);
54 return 0;
55 }
56 EXPORT_SYMBOL(osal_down);
osal_down_interruptible(osal_semaphore_t * sem)57 int osal_down_interruptible(osal_semaphore_t *sem)
58 {
59 struct semaphore *p = NULL;
60
61 if (sem == NULL) {
62 osal_trace("%s - parameter invalid!\n", __FUNCTION__);
63 return -1;
64 }
65 p = (struct semaphore *)(sem->sem);
66 return down_interruptible(p);
67 }
68 EXPORT_SYMBOL(osal_down_interruptible);
osal_down_trylock(osal_semaphore_t * sem)69 int osal_down_trylock(osal_semaphore_t *sem)
70 {
71 struct semaphore *p = NULL;
72
73 if (sem == NULL) {
74 osal_trace("%s - parameter invalid!\n", __FUNCTION__);
75 return -1;
76 }
77 p = (struct semaphore *)(sem->sem);
78 return down_trylock(p);
79 }
80 EXPORT_SYMBOL(osal_down_trylock);
osal_up(osal_semaphore_t * sem)81 void osal_up(osal_semaphore_t *sem)
82 {
83 struct semaphore *p = NULL;
84
85 p = (struct semaphore *)(sem->sem);
86 up(p);
87 }
88 EXPORT_SYMBOL(osal_up);
osal_sema_destroy(osal_semaphore_t * sem)89 void osal_sema_destroy(osal_semaphore_t *sem)
90 {
91 struct semaphore *p = NULL;
92
93 p = (struct semaphore *)(sem->sem);
94 kfree(p);
95 p = NULL;
96 sem->sem = NULL;
97 }
98 EXPORT_SYMBOL(osal_sema_destroy);
99