• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef __OAL_LINUX_WAKE_LOCK_H__
20 #define __OAL_LINUX_WAKE_LOCK_H__
21 
22 /* ****************************************************************************
23   1 其他头文件包含
24 **************************************************************************** */
25 #include "oal_mutex.h"
26 
27 #ifdef __cplusplus
28 #if __cplusplus
29 extern "C" {
30 #endif
31 #endif
32 
33 /* ****************************************************************************
34   2 STRUCT定义
35 **************************************************************************** */
36 typedef struct _oal_wakelock_stru_ {
37 #ifdef CONFIG_HAS_WAKELOCK
38     struct wake_lock st_wakelock; /* wakelock锁 */
39     oal_spin_lock_stru lock;      /* wakelock锁操作spinlock锁 */
40 #endif
41     unsigned long lock_count;  /* 持有wakelock锁的次数 */
42     unsigned long locked_addr; /* the locked address */
43 } oal_wakelock_stru;
44 
45 /* ****************************************************************************
46   3 枚举定义
47 **************************************************************************** */
48 /* ****************************************************************************
49   4 全局变量声明
50 **************************************************************************** */
51 /* ****************************************************************************
52   5 消息头定义
53 **************************************************************************** */
54 /* ****************************************************************************
55   6 消息定义
56 **************************************************************************** */
57 /* ****************************************************************************
58   7 宏定义
59 **************************************************************************** */
60 /* ****************************************************************************
61   8 UNION定义
62 **************************************************************************** */
63 /* ****************************************************************************
64   9 OTHERS定义
65 **************************************************************************** */
66 /* ****************************************************************************
67   10 函数声明
68 **************************************************************************** */
oal_wake_lock_init(oal_wakelock_stru * pst_wakelock,char * name)69 static inline hi_void oal_wake_lock_init(oal_wakelock_stru *pst_wakelock, char *name)
70 {
71 #ifdef CONFIG_HAS_WAKELOCK
72     memset_s((hi_u8 *)pst_wakelock, sizeof(oal_wakelock_stru), 0, sizeof(oal_wakelock_stru));
73 
74     wake_lock_init(&pst_wakelock->st_wakelock, WAKE_LOCK_SUSPEND, name ? name : "wake_lock_null");
75     oal_spin_lock_init(&pst_wakelock->lock);
76     pst_wakelock->lock_count = 0;
77     pst_wakelock->locked_addr = 0;
78 #else
79     hi_unref_param(pst_wakelock);
80     hi_unref_param(name);
81 #endif
82 }
83 
oal_wake_lock_exit(oal_wakelock_stru * pst_wakelock)84 static inline hi_void oal_wake_lock_exit(oal_wakelock_stru *pst_wakelock)
85 {
86 #ifdef CONFIG_HAS_WAKELOCK
87     wake_lock_destroy(&pst_wakelock->st_wakelock);
88 #else
89     hi_unref_param(pst_wakelock);
90 #endif
91 }
92 
oal_wake_lock(oal_wakelock_stru * pst_wakelock)93 static inline void oal_wake_lock(oal_wakelock_stru *pst_wakelock)
94 {
95 #ifdef CONFIG_HAS_WAKELOCK
96     unsigned long ul_flags;
97 
98     oal_spin_lock_irq_save(&pst_wakelock->lock, &ul_flags);
99     if (!pst_wakelock->lock_count) {
100         wake_lock(&pst_wakelock->st_wakelock);
101         pst_wakelock->locked_addr = (unsigned long)_RET_IP_;
102     }
103     pst_wakelock->lock_count++;
104     oal_spin_unlock_irq_restore(&pst_wakelock->lock, &ul_flags);
105 #else
106     hi_unref_param(pst_wakelock);
107 #endif
108 }
109 
oal_wake_unlock(oal_wakelock_stru * pst_wakelock)110 static inline void oal_wake_unlock(oal_wakelock_stru *pst_wakelock)
111 {
112 #ifdef CONFIG_HAS_WAKELOCK
113     unsigned long ul_flags;
114 
115     oal_spin_lock_irq_save(&pst_wakelock->lock, &ul_flags);
116     if (pst_wakelock->lock_count) {
117         pst_wakelock->lock_count--;
118         if (!pst_wakelock->lock_count) {
119             wake_unlock(&pst_wakelock->st_wakelock);
120             pst_wakelock->locked_addr = (unsigned long)0x0;
121         }
122     }
123     oal_spin_unlock_irq_restore(&pst_wakelock->lock, &ul_flags);
124 #else
125     hi_unref_param(pst_wakelock);
126 #endif
127 }
128 
oal_wakelock_active(oal_wakelock_stru * pst_wakelock)129 static inline unsigned long oal_wakelock_active(oal_wakelock_stru *pst_wakelock)
130 {
131 #ifdef CONFIG_HAS_WAKELOCK
132     return (unsigned long)wake_lock_active(&pst_wakelock->st_wakelock);
133 #else
134     hi_unref_param(pst_wakelock);
135     return 0;
136 #endif
137 }
138 
139 #ifdef __cplusplus
140 #if __cplusplus
141 }
142 #endif
143 #endif
144 
145 #endif /* end of oal_wakelock.h */
146