1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/statfs.h>
33 #include <sys/mount.h>
34 #include "proc_fs.h"
35 #include "internal.h"
36 #ifdef LOSCFG_KERNEL_PM
37 #include "los_pm.h"
38
PowerLockWrite(struct ProcFile * pf,const char * buf,size_t count,loff_t * ppos)39 static int PowerLockWrite(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
40 {
41 (void)pf;
42 (void)count;
43 (void)ppos;
44 return -LOS_PmLockRequest(buf);
45 }
46
PowerLockRead(struct SeqBuf * m,void * v)47 static int PowerLockRead(struct SeqBuf *m, void *v)
48 {
49 (void)v;
50
51 LOS_PmLockInfoShow(m);
52 return 0;
53 }
54
55 static const struct ProcFileOperations PowerLock = {
56 .write = PowerLockWrite,
57 .read = PowerLockRead,
58 };
59
PowerUnlockWrite(struct ProcFile * pf,const char * buf,size_t count,loff_t * ppos)60 static int PowerUnlockWrite(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
61 {
62 (void)pf;
63 (void)count;
64 (void)ppos;
65 return -LOS_PmLockRelease(buf);
66 }
67
68 static const struct ProcFileOperations PowerUnlock = {
69 .write = PowerUnlockWrite,
70 .read = PowerLockRead,
71 };
72
PowerModeWrite(struct ProcFile * pf,const char * buf,size_t count,loff_t * ppos)73 static int PowerModeWrite(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
74 {
75 (void)pf;
76 (void)count;
77 (void)ppos;
78
79 LOS_SysSleepEnum mode;
80
81 if (buf == NULL) {
82 return 0;
83 }
84
85 if (strcmp(buf, "normal") == 0) {
86 mode = LOS_SYS_NORMAL_SLEEP;
87 } else if (strcmp(buf, "light") == 0) {
88 mode = LOS_SYS_LIGHT_SLEEP;
89 } else if (strcmp(buf, "deep") == 0) {
90 mode = LOS_SYS_DEEP_SLEEP;
91 } else if (strcmp(buf, "shutdown") == 0) {
92 mode = LOS_SYS_SHUTDOWN;
93 } else {
94 PRINT_ERR("Unsupported hibernation mode: %s\n", buf);
95 return -EINVAL;
96 }
97
98 return -LOS_PmModeSet(mode);
99 }
100
PowerModeRead(struct SeqBuf * m,void * v)101 static int PowerModeRead(struct SeqBuf *m, void *v)
102 {
103 (void)v;
104
105 LosBufPrintf(m, "normal light deep shutdown\n");
106 return 0;
107 }
108
109 static const struct ProcFileOperations PowerMode = {
110 .write = PowerModeWrite,
111 .read = PowerModeRead,
112 };
113
PowerCountRead(struct SeqBuf * m,void * v)114 static int PowerCountRead(struct SeqBuf *m, void *v)
115 {
116 (void)v;
117 UINT32 count = LOS_PmReadLock();
118
119 LosBufPrintf(m, "%u\n", count);
120 return 0;
121 }
122
PowerCountWrite(struct ProcFile * pf,const char * buf,size_t count,loff_t * ppos)123 static int PowerCountWrite(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
124 {
125 (void)pf;
126 (void)count;
127 (void)ppos;
128
129 int weakCount;
130
131 if (buf == NULL) {
132 return 0;
133 }
134
135 weakCount = atoi(buf);
136 return -LOS_PmSuspend(weakCount);
137 }
138
139 static const struct ProcFileOperations PowerCount = {
140 .write = PowerCountWrite,
141 .read = PowerCountRead,
142 };
143
144 #define POWER_FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)
145 #define OS_POWER_PRIVILEGE 7
146
ProcPmInit(void)147 void ProcPmInit(void)
148 {
149 struct ProcDirEntry *power = CreateProcEntry("power", S_IFDIR | S_IRWXU | S_IRWXG | S_IROTH, NULL);
150 if (power == NULL) {
151 PRINT_ERR("create /proc/power error!\n");
152 return;
153 }
154 power->uid = OS_POWER_PRIVILEGE;
155 power->gid = OS_POWER_PRIVILEGE;
156
157 struct ProcDirEntry *mode = CreateProcEntry("power/power_mode", POWER_FILE_MODE, NULL);
158 if (mode == NULL) {
159 PRINT_ERR("create /proc/power/power_mode error!\n");
160 goto FREE_POWER;
161 }
162 mode->procFileOps = &PowerMode;
163 mode->uid = OS_POWER_PRIVILEGE;
164 mode->gid = OS_POWER_PRIVILEGE;
165
166 struct ProcDirEntry *lock = CreateProcEntry("power/power_lock", POWER_FILE_MODE, NULL);
167 if (lock == NULL) {
168 PRINT_ERR("create /proc/power/power_lock error!\n");
169 goto FREE_MODE;
170 }
171 lock->procFileOps = &PowerLock;
172 lock->uid = OS_POWER_PRIVILEGE;
173 lock->gid = OS_POWER_PRIVILEGE;
174
175 struct ProcDirEntry *unlock = CreateProcEntry("power/power_unlock", POWER_FILE_MODE, NULL);
176 if (unlock == NULL) {
177 PRINT_ERR("create /proc/power/power_unlock error!\n");
178 goto FREE_LOCK;
179 }
180 unlock->procFileOps = &PowerUnlock;
181 unlock->uid = OS_POWER_PRIVILEGE;
182 unlock->gid = OS_POWER_PRIVILEGE;
183
184 struct ProcDirEntry *count = CreateProcEntry("power/power_count", S_IRUSR | S_IRGRP | S_IROTH, NULL);
185 if (count == NULL) {
186 PRINT_ERR("create /proc/power/power_count error!\n");
187 goto FREE_UNLOCK;
188 }
189 count->procFileOps = &PowerCount;
190 count->uid = OS_POWER_PRIVILEGE;
191 count->gid = OS_POWER_PRIVILEGE;
192
193 return;
194
195 FREE_UNLOCK:
196 ProcFreeEntry(unlock);
197 FREE_LOCK:
198 ProcFreeEntry(lock);
199 FREE_MODE:
200 ProcFreeEntry(mode);
201 FREE_POWER:
202 ProcFreeEntry(power);
203 return;
204 }
205 #endif
206